-- SPDX-License-Identifier: LicenseRef-ConusAI-Template-1.0 create table "agent_run" ( "id" uuid primary key, "version" integer not null, "created_at" timestamptz not null, "updated_at" timestamptz not null, "org_id" uuid not null, "workspace_id" uuid not null, "project_id" uuid not null, "goal" text not null, "plan" jsonb not null, "status" text not null, "failure_code" text, "failure_detail" text, "actor_id" text not null, "budget_cost_microcents" integer not null, "budget_steps" integer not null, "budget_seconds" integer not null, "usage_cost_microcents" integer not null, "usage_steps" integer not null, "cancel_requested_at" text, "started_at" text, "finished_at" text ); create index "agent_run_org_id_workspace_id_project_id_status_idx" on "agent_run" ("org_id", "workspace_id", "project_id", "status"); create table "agent_step" ( "id" uuid primary key, "version" integer not null, "created_at" timestamptz not null, "updated_at" timestamptz not null, "org_id" uuid not null, "workspace_id" uuid not null, "project_id" uuid not null, "run_id" uuid not null, "key" text not null, "index" integer not null, "label" text not null, "status" text not null, "attempts" integer not null, "usage_cost_microcents" integer not null, "usage_tokens" integer not null, "error" text, "external_effect" boolean not null, "artifacts" jsonb not null, "approval" jsonb, "started_at" text, "finished_at" text ); alter table "agent_step" add constraint "agent_step_org_id_workspace_id_project_id_run_id_key_key" unique nulls not distinct ("org_id", "workspace_id", "project_id", "run_id", "key"); create index "agent_step_org_id_workspace_id_project_id_run_id_idx" on "agent_step" ("org_id", "workspace_id", "project_id", "run_id"); alter table "agent_run" enable row level security; alter table "agent_run" force row level security; create policy "agent_run_tenant" on "agent_run" using ("org_id" = nullif(current_setting('app.org_id', true), '')::uuid) with check ("org_id" = nullif(current_setting('app.org_id', true), '')::uuid); alter table "agent_step" enable row level security; alter table "agent_step" force row level security; create policy "agent_step_tenant" on "agent_step" using ("org_id" = nullif(current_setting('app.org_id', true), '')::uuid) with check ("org_id" = nullif(current_setting('app.org_id', true), '')::uuid);