import type { Kysely } from "kysely"; import { dropTables, entityTable, execDdl, oneOf } from "../src/migration-helpers.ts"; // W2-D Agent 装配(REQ §10.3、§10.6、§10.7、§11): // - capability_invocation:有副作用能力的操作账本。幂等键由服务端按能力 ID/版本/运行/规范化输入生成(唯一); // succeeded 行直接返回已记录的输出,running 行(进程在调用中崩溃)按同一幂等键重跑处理器对账—— // 处理器自身必须幂等(下载提交按计划状态 + qB hash、整理按记录状态)。 // - agent_run_message:会话追加的用户消息(纠正/补充),runAgent 任务按顺序消费; // - agent_run 增加 mode(execute | plan_only)与 dedupe_key(后台触发按根因去重)。 // biome-ignore lint/suspicious/noExplicitAny: 迁移不依赖应用表类型 export async function up(db: Kysely): Promise { await execDdl( db, ` alter table agent_run add column mode text not null default 'execute' ${oneOf("mode", ["execute", "plan_only"])}; alter table agent_run add column dedupe_key text; create unique index agent_run_dedupe_key on agent_run (dedupe_key) where dedupe_key is not null; ${entityTable( "capability_invocation", ` idempotency_key text not null, capability_id text not null, capability_version integer not null, agent_run_id uuid references agent_run (id) on delete set null, actor_id text not null, source text not null, status text not null default 'running' ${oneOf("status", ["running", "succeeded", "failed"])}, input jsonb not null default '{}'::jsonb, output jsonb, error jsonb, grant_id uuid, approval_id uuid, attempts integer not null default 1, finished_at timestamptz, constraint capability_invocation_idempotency_key unique (idempotency_key)`, )} create index capability_invocation_run_idx on capability_invocation (agent_run_id, created_at) where agent_run_id is not null; create index capability_invocation_running_idx on capability_invocation (created_at) where status = 'running'; ${entityTable( "agent_run_message", ` run_id uuid not null references agent_run (id) on delete cascade, text text not null, status text not null default 'pending' ${oneOf("status", ["pending", "consumed"])}, created_by text, consumed_at timestamptz`, )} create index agent_run_message_pending_idx on agent_run_message (run_id, created_at) where status = 'pending'; `, ); } // biome-ignore lint/suspicious/noExplicitAny: 同上 export async function down(db: Kysely): Promise { await execDdl( db, `${dropTables(["agent_run_message", "capability_invocation"])} drop index if exists agent_run_dedupe_key; alter table agent_run drop column if exists dedupe_key; alter table agent_run drop column if exists mode;`, ); }