Implement Canvas V2 subgraphs, assets and asynchronous AI execution
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
BEGIN;
|
||||
CREATE TABLE canvas_subgraphs (
|
||||
project text NOT NULL,
|
||||
workspace_id uuid NOT NULL,
|
||||
parent_board_id uuid NOT NULL,
|
||||
frame_id text NOT NULL CHECK (frame_id ~ '^[A-Za-z0-9_-]{1,128}$'),
|
||||
child_board_id uuid NOT NULL,
|
||||
archived_at timestamptz,
|
||||
migration_source jsonb,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY(project,workspace_id,child_board_id),
|
||||
FOREIGN KEY(project,workspace_id,parent_board_id) REFERENCES materials ON DELETE RESTRICT,
|
||||
FOREIGN KEY(project,workspace_id,child_board_id) REFERENCES materials ON DELETE RESTRICT,
|
||||
CHECK(parent_board_id<>child_board_id)
|
||||
);
|
||||
CREATE UNIQUE INDEX canvas_subgraph_active_frame ON canvas_subgraphs(project,workspace_id,parent_board_id,frame_id) WHERE archived_at IS NULL;
|
||||
-- Workspace row locks serialize ownership changes. A database trigger also rejects
|
||||
-- cycles and excessive depth for maintenance callers outside the HTTP service.
|
||||
CREATE FUNCTION foundry_check_subgraph() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
DECLARE depth integer;
|
||||
BEGIN
|
||||
PERFORM 1 FROM workspaces WHERE project=NEW.project AND id=NEW.workspace_id FOR UPDATE;
|
||||
IF EXISTS(SELECT 1 FROM materials WHERE project=NEW.project AND workspace_id=NEW.workspace_id AND id IN(NEW.parent_board_id,NEW.child_board_id) AND kind<>'board') THEN
|
||||
RAISE EXCEPTION 'subgraph requires boards' USING ERRCODE='23514';
|
||||
END IF;
|
||||
WITH RECURSIVE ancestors(id,path) AS (
|
||||
SELECT NEW.parent_board_id,ARRAY[NEW.child_board_id,NEW.parent_board_id]
|
||||
UNION ALL
|
||||
SELECT s.parent_board_id,a.path||s.parent_board_id FROM ancestors a JOIN canvas_subgraphs s
|
||||
ON s.project=NEW.project AND s.workspace_id=NEW.workspace_id AND s.child_board_id=a.id
|
||||
WHERE NOT s.parent_board_id=ANY(a.path) AND s.archived_at IS NULL
|
||||
) SELECT max(cardinality(path)-1) INTO depth FROM ancestors;
|
||||
IF depth>8 OR EXISTS(WITH RECURSIVE descendants(id,path) AS (
|
||||
SELECT NEW.child_board_id,ARRAY[NEW.child_board_id]
|
||||
UNION ALL SELECT s.child_board_id,d.path||s.child_board_id FROM descendants d JOIN canvas_subgraphs s
|
||||
ON s.project=NEW.project AND s.workspace_id=NEW.workspace_id AND s.parent_board_id=d.id
|
||||
WHERE NOT s.child_board_id=ANY(d.path) AND s.archived_at IS NULL
|
||||
) SELECT 1 FROM descendants WHERE id=NEW.parent_board_id OR cardinality(path)+depth-1>8) THEN
|
||||
RAISE EXCEPTION 'subgraph cycle or depth exceeded' USING ERRCODE='23514';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END $$;
|
||||
CREATE TRIGGER canvas_subgraph_integrity BEFORE INSERT OR UPDATE ON canvas_subgraphs FOR EACH ROW EXECUTE FUNCTION foundry_check_subgraph();
|
||||
CREATE TABLE canvas_transactions (
|
||||
project text NOT NULL,
|
||||
workspace_id uuid NOT NULL,
|
||||
board_id uuid NOT NULL,
|
||||
id uuid NOT NULL DEFAULT gen_random_uuid(),
|
||||
account_id uuid NOT NULL,
|
||||
action text NOT NULL,
|
||||
idempotency_key uuid NOT NULL,
|
||||
payload_hash text NOT NULL,
|
||||
response_status integer NOT NULL,
|
||||
response_body jsonb NOT NULL,
|
||||
before_state jsonb NOT NULL,
|
||||
after_versions jsonb NOT NULL,
|
||||
relation_changes jsonb NOT NULL DEFAULT '[]'::jsonb,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
expires_at timestamptz NOT NULL DEFAULT now()+interval '7 days',
|
||||
undone_at timestamptz,
|
||||
PRIMARY KEY(project,workspace_id,id),
|
||||
UNIQUE(project,workspace_id,account_id,board_id,action,idempotency_key),
|
||||
FOREIGN KEY(project,workspace_id,board_id) REFERENCES materials ON DELETE RESTRICT,
|
||||
FOREIGN KEY(project,account_id) REFERENCES project_accounts
|
||||
);
|
||||
COMMIT;
|
||||
@@ -0,0 +1,32 @@
|
||||
BEGIN;
|
||||
-- Keeping the encoded object in PostgreSQL makes object creation, bindings and
|
||||
-- idempotency atomic. A future blob backend can replace bytes without changing
|
||||
-- stable application URLs or canvas references.
|
||||
CREATE TABLE canvas_assets (
|
||||
project text NOT NULL,
|
||||
workspace_id uuid NOT NULL,
|
||||
id uuid NOT NULL DEFAULT gen_random_uuid(),
|
||||
uploaded_by uuid NOT NULL,
|
||||
mime_type text NOT NULL CHECK(mime_type IN('image/png','image/jpeg','image/gif','image/webp')),
|
||||
bytes bytea NOT NULL CHECK(octet_length(bytes) BETWEEN 1 AND 20971520),
|
||||
width integer NOT NULL CHECK(width>0),
|
||||
height integer NOT NULL CHECK(height>0),
|
||||
digest text NOT NULL,
|
||||
idempotency_key uuid NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY(project,workspace_id,id),
|
||||
UNIQUE(project,workspace_id,uploaded_by,idempotency_key),
|
||||
FOREIGN KEY(project,workspace_id) REFERENCES workspaces ON DELETE CASCADE,
|
||||
FOREIGN KEY(project,uploaded_by) REFERENCES project_accounts
|
||||
);
|
||||
CREATE TABLE canvas_asset_bindings (
|
||||
project text NOT NULL,
|
||||
workspace_id uuid NOT NULL,
|
||||
asset_id uuid NOT NULL,
|
||||
board_id uuid NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY(project,workspace_id,asset_id,board_id),
|
||||
FOREIGN KEY(project,workspace_id,asset_id) REFERENCES canvas_assets ON DELETE RESTRICT,
|
||||
FOREIGN KEY(project,workspace_id,board_id) REFERENCES materials ON DELETE CASCADE
|
||||
);
|
||||
COMMIT;
|
||||
@@ -0,0 +1,80 @@
|
||||
BEGIN;
|
||||
CREATE TABLE canvas_ai_runs (
|
||||
project text NOT NULL,
|
||||
workspace_id uuid NOT NULL,
|
||||
board_id uuid NOT NULL,
|
||||
id uuid NOT NULL DEFAULT gen_random_uuid(),
|
||||
account_id uuid NOT NULL,
|
||||
idempotency_key uuid NOT NULL,
|
||||
payload_hash text NOT NULL,
|
||||
status text NOT NULL CHECK(status IN('queued','running','succeeded','partially_succeeded','failed','cancelled','conflict')),
|
||||
plan jsonb NOT NULL,
|
||||
create_response jsonb NOT NULL,
|
||||
error_code text,
|
||||
cancel_requested boolean NOT NULL DEFAULT false,
|
||||
lease_id uuid,
|
||||
lease_until timestamptz,
|
||||
last_seq bigint NOT NULL DEFAULT 0 CHECK(last_seq>=0),
|
||||
available_at timestamptz NOT NULL DEFAULT now(),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
completed_at timestamptz,
|
||||
expires_at timestamptz NOT NULL DEFAULT now()+interval '30 days',
|
||||
PRIMARY KEY(project,workspace_id,id),
|
||||
UNIQUE(project,workspace_id,account_id,board_id,idempotency_key),
|
||||
FOREIGN KEY(project,workspace_id,board_id) REFERENCES materials ON DELETE RESTRICT,
|
||||
FOREIGN KEY(project,account_id) REFERENCES project_accounts
|
||||
);
|
||||
CREATE INDEX canvas_ai_pending ON canvas_ai_runs(available_at,created_at,id) WHERE status='queued';
|
||||
CREATE INDEX canvas_ai_active ON canvas_ai_runs(project,workspace_id,board_id,created_at,id) WHERE status IN('queued','running');
|
||||
CREATE TABLE canvas_ai_steps (
|
||||
project text NOT NULL,
|
||||
workspace_id uuid NOT NULL,
|
||||
run_id uuid NOT NULL,
|
||||
id text NOT NULL,
|
||||
board_id uuid NOT NULL,
|
||||
node_id text NOT NULL,
|
||||
ordinal integer NOT NULL CHECK(ordinal>=0),
|
||||
status text NOT NULL CHECK(status IN('queued','running','succeeded','failed','cancelled','conflict','skipped')),
|
||||
model_id text NOT NULL,
|
||||
frozen_input jsonb NOT NULL,
|
||||
target_fingerprint text NOT NULL,
|
||||
output jsonb,
|
||||
error_code text,
|
||||
applied_version bigint,
|
||||
started_at timestamptz,
|
||||
completed_at timestamptz,
|
||||
PRIMARY KEY(project,workspace_id,run_id,id),
|
||||
UNIQUE(project,workspace_id,run_id,board_id,node_id),
|
||||
FOREIGN KEY(project,workspace_id,run_id) REFERENCES canvas_ai_runs ON DELETE CASCADE,
|
||||
FOREIGN KEY(project,workspace_id,board_id) REFERENCES materials ON DELETE RESTRICT
|
||||
);
|
||||
CREATE UNIQUE INDEX canvas_ai_node_busy ON canvas_ai_steps(project,workspace_id,board_id,node_id) WHERE status IN('queued','running');
|
||||
CREATE TABLE canvas_ai_events (
|
||||
project text NOT NULL,
|
||||
workspace_id uuid NOT NULL,
|
||||
run_id uuid NOT NULL,
|
||||
seq bigint NOT NULL CHECK(seq>0),
|
||||
type text NOT NULL,
|
||||
data jsonb NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY(project,workspace_id,run_id,seq),
|
||||
FOREIGN KEY(project,workspace_id,run_id) REFERENCES canvas_ai_runs ON DELETE CASCADE
|
||||
);
|
||||
-- Per-run serialization increments last_seq and inserts an event in one statement.
|
||||
-- A lease which expires after a worker started is not automatically re-executed:
|
||||
-- recovery marks the uncertain step failed, preventing duplicate model invocations.
|
||||
CREATE TABLE canvas_ai_mutations (
|
||||
project text NOT NULL,
|
||||
workspace_id uuid NOT NULL,
|
||||
run_id uuid NOT NULL,
|
||||
account_id uuid NOT NULL,
|
||||
action text NOT NULL,
|
||||
idempotency_key uuid NOT NULL,
|
||||
payload_hash text NOT NULL,
|
||||
response_body jsonb NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY(project,workspace_id,run_id,account_id,action,idempotency_key),
|
||||
FOREIGN KEY(project,workspace_id,run_id) REFERENCES canvas_ai_runs ON DELETE CASCADE,
|
||||
FOREIGN KEY(project,account_id) REFERENCES project_accounts
|
||||
);
|
||||
COMMIT;
|
||||
Reference in New Issue
Block a user