migrations.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. import type { SqliteMigration } from "@zcode/shared/sqlite-migrations";
  2. export type { SqliteMigration } from "@zcode/shared/sqlite-migrations";
  3. import { PROVIDER_MODEL_SELECTION_MIGRATION_SQL } from "./migrations/0020-provider-model-selection.js";
  4. import { SESSION_DATABASE_IDENTITY_MIGRATION } from "@zcode/shared/sqlite-migrations";
  5. import { OFFICIAL_GLM_SELECTION_MIGRATION_SQL } from "./migrations/0021-official-glm-selection.js";
  6. import { BACKFILLED_SESSION_REASONING_MIGRATION_SQL } from "./migrations/0022-backfilled-session-reasoning.js";
  7. export const SQLITE_MIGRATIONS: readonly SqliteMigration[] = [
  8. {
  9. appVersion: "0.2.0",
  10. id: "0001_base_session_store",
  11. touchedTables: [
  12. "input_history",
  13. "message",
  14. "part",
  15. "permission",
  16. "session",
  17. "session_entry",
  18. "todo",
  19. ],
  20. sql: `
  21. create table if not exists session (
  22. id text primary key,
  23. project_id text not null,
  24. workspace_id text,
  25. parent_id text,
  26. slug text not null,
  27. directory text not null,
  28. path text,
  29. title text not null,
  30. version text not null,
  31. share_url text,
  32. summary_additions integer,
  33. summary_deletions integer,
  34. summary_files integer,
  35. summary_diffs text,
  36. revert text,
  37. permission text,
  38. time_created integer not null,
  39. time_updated integer not null,
  40. time_compacting integer,
  41. time_archived integer
  42. );
  43. create index if not exists session_project_idx on session(project_id);
  44. create index if not exists session_workspace_idx on session(workspace_id);
  45. create index if not exists session_parent_idx on session(parent_id);
  46. create table if not exists message (
  47. id text primary key,
  48. session_id text not null references session(id) on delete cascade,
  49. time_created integer not null,
  50. time_updated integer not null,
  51. data text not null
  52. );
  53. create index if not exists message_session_time_created_id_idx
  54. on message(session_id, time_created, id);
  55. create table if not exists part (
  56. id text primary key,
  57. message_id text not null references message(id) on delete cascade,
  58. session_id text not null,
  59. time_created integer not null,
  60. time_updated integer not null,
  61. data text not null
  62. );
  63. create index if not exists part_message_id_id_idx on part(message_id, id);
  64. create index if not exists part_session_idx on part(session_id);
  65. create table if not exists todo (
  66. session_id text not null references session(id) on delete cascade,
  67. content text not null,
  68. status text not null,
  69. priority text not null,
  70. position integer not null,
  71. time_created integer not null,
  72. time_updated integer not null,
  73. primary key(session_id, position)
  74. );
  75. create index if not exists todo_session_idx on todo(session_id);
  76. create table if not exists session_entry (
  77. id text primary key,
  78. session_id text not null references session(id) on delete cascade,
  79. type text not null,
  80. time_created integer not null,
  81. time_updated integer not null,
  82. data text not null
  83. );
  84. create index if not exists session_entry_session_idx on session_entry(session_id);
  85. create index if not exists session_entry_session_type_idx on session_entry(session_id, type);
  86. create index if not exists session_entry_time_created_idx on session_entry(time_created);
  87. create table if not exists permission (
  88. project_id text primary key,
  89. time_created integer not null,
  90. time_updated integer not null,
  91. data text not null
  92. );
  93. create table if not exists input_history (
  94. id text primary key,
  95. project_id text not null,
  96. session_id text,
  97. text text not null,
  98. kind text not null,
  99. time_created integer not null
  100. );
  101. create index if not exists input_history_project_time_idx
  102. on input_history(project_id, time_created desc, id desc);
  103. create index if not exists input_history_time_idx
  104. on input_history(time_created desc, id desc);
  105. `,
  106. },
  107. {
  108. appVersion: "0.2.0",
  109. id: "0002_local_setting",
  110. touchedTables: ["local_setting"],
  111. sql: `
  112. create table if not exists local_setting (
  113. scope text not null,
  114. scope_id text not null,
  115. namespace text not null,
  116. key text not null,
  117. value text not null,
  118. schema_version integer not null,
  119. time_created integer not null,
  120. time_updated integer not null,
  121. primary key(scope, scope_id, namespace, key)
  122. );
  123. create index if not exists local_setting_scope_idx
  124. on local_setting(scope, scope_id);
  125. create index if not exists local_setting_namespace_key_idx
  126. on local_setting(namespace, key);
  127. `,
  128. },
  129. {
  130. appVersion: "0.2.0",
  131. id: "0003_backfill_permission_local_setting",
  132. touchedTables: ["local_setting"],
  133. sql: `
  134. insert or ignore into local_setting (
  135. scope,
  136. scope_id,
  137. namespace,
  138. key,
  139. value,
  140. schema_version,
  141. time_created,
  142. time_updated
  143. )
  144. select
  145. 'project',
  146. project_id,
  147. 'permission',
  148. 'ruleset',
  149. data,
  150. 1,
  151. time_created,
  152. time_updated
  153. from permission
  154. where data is not null;
  155. `,
  156. },
  157. {
  158. appVersion: "0.7.0",
  159. id: "0004_session_target",
  160. touchedTables: ["session_target"],
  161. sql: `
  162. create table if not exists session_target (
  163. session_id text primary key references session(id) on delete cascade,
  164. target_id text not null,
  165. objective text not null,
  166. status text not null check(status in ('active', 'paused', 'complete')),
  167. time_created integer not null,
  168. time_updated integer not null
  169. );
  170. `,
  171. },
  172. {
  173. appVersion: "0.7.0",
  174. id: "0005_session_target_accounting",
  175. touchedTables: ["session_target", "session_target_next"],
  176. sql: `
  177. create table if not exists session_target_next (
  178. session_id text primary key references session(id) on delete cascade,
  179. target_id text not null,
  180. objective text not null,
  181. status text not null check(status in ('active', 'paused', 'budget_limited', 'complete')),
  182. token_budget integer,
  183. tokens_used integer not null default 0,
  184. time_used_seconds integer not null default 0,
  185. time_created integer not null,
  186. time_updated integer not null
  187. );
  188. insert into session_target_next (
  189. session_id,
  190. target_id,
  191. objective,
  192. status,
  193. token_budget,
  194. tokens_used,
  195. time_used_seconds,
  196. time_created,
  197. time_updated
  198. )
  199. select
  200. session_id,
  201. target_id,
  202. objective,
  203. status,
  204. null,
  205. 0,
  206. 0,
  207. time_created,
  208. time_updated
  209. from session_target;
  210. drop table session_target;
  211. alter table session_target_next rename to session_target;
  212. `,
  213. },
  214. {
  215. appVersion: "0.11.0",
  216. id: "0006_input_history_attachments",
  217. touchedTables: ["input_history"],
  218. sql: `
  219. alter table input_history add column attachments text;
  220. `,
  221. },
  222. {
  223. appVersion: "0.13.0",
  224. id: "0007_workflow_script_runtime",
  225. touchedTables: [
  226. "session",
  227. "session_task_link",
  228. "workflow_activity",
  229. "workflow_definition",
  230. "workflow_event",
  231. "workflow_run",
  232. ],
  233. sql: `
  234. alter table session add column task_type text not null default 'interactive';
  235. create index if not exists session_task_type_idx on session(task_type);
  236. create table if not exists workflow_definition (
  237. id text primary key,
  238. name text not null,
  239. source text not null check(source in ('builtin', 'user')),
  240. trusted integer not null default 0 check(trusted in (0, 1)),
  241. enabled integer not null default 1 check(enabled in (0, 1)),
  242. script_path text,
  243. script_hash text not null,
  244. meta_json text not null,
  245. time_created integer not null,
  246. time_updated integer not null
  247. );
  248. create index if not exists workflow_definition_source_idx
  249. on workflow_definition(source, enabled);
  250. create table if not exists workflow_run (
  251. id text primary key,
  252. definition_id text,
  253. name text not null,
  254. kind text not null default 'script',
  255. parent_session_id text references session(id) on delete set null,
  256. cwd text not null,
  257. script_path text,
  258. script_hash text not null,
  259. args_json text,
  260. args_hash text,
  261. status text not null check(status in (
  262. 'pending',
  263. 'running',
  264. 'paused',
  265. 'completed',
  266. 'failed',
  267. 'cancelled'
  268. )),
  269. current_phase text,
  270. budget_total integer,
  271. budget_spent integer not null default 0,
  272. stats_json text,
  273. failure_json text,
  274. time_created integer not null,
  275. time_started integer,
  276. time_updated integer not null,
  277. time_completed integer
  278. );
  279. create index if not exists workflow_run_parent_session_idx
  280. on workflow_run(parent_session_id);
  281. create index if not exists workflow_run_cwd_status_idx
  282. on workflow_run(cwd, status, time_updated desc);
  283. create index if not exists workflow_run_definition_idx
  284. on workflow_run(definition_id);
  285. create table if not exists workflow_activity (
  286. id text primary key,
  287. run_id text not null references workflow_run(id) on delete cascade,
  288. parent_activity_id text,
  289. call_index integer not null,
  290. call_path text not null,
  291. attempt integer not null default 1,
  292. type text not null,
  293. phase text,
  294. label text,
  295. input_hash text not null,
  296. prompt text,
  297. opts_json text,
  298. status text not null check(status in (
  299. 'queued',
  300. 'running',
  301. 'completed',
  302. 'failed',
  303. 'skipped',
  304. 'cancelled',
  305. 'cached',
  306. 'lost'
  307. )),
  308. child_session_id text references session(id) on delete set null,
  309. result_json text,
  310. error_json text,
  311. time_created integer not null,
  312. time_started integer,
  313. time_updated integer not null,
  314. time_completed integer,
  315. unique(run_id, call_path, attempt)
  316. );
  317. create index if not exists workflow_activity_run_status_idx
  318. on workflow_activity(run_id, status, call_index);
  319. create index if not exists workflow_activity_child_session_idx
  320. on workflow_activity(child_session_id);
  321. create table if not exists workflow_event (
  322. id text primary key,
  323. run_id text not null references workflow_run(id) on delete cascade,
  324. sequence integer not null,
  325. type text not null,
  326. phase text,
  327. activity_id text references workflow_activity(id) on delete set null,
  328. payload_json text,
  329. time_created integer not null,
  330. unique(run_id, sequence)
  331. );
  332. create index if not exists workflow_event_run_sequence_idx
  333. on workflow_event(run_id, sequence);
  334. create table if not exists session_task_link (
  335. id text primary key,
  336. root_workflow_run_id text references workflow_run(id) on delete cascade,
  337. parent_link_id text references session_task_link(id) on delete cascade,
  338. activity_id text references workflow_activity(id) on delete set null,
  339. parent_session_id text references session(id) on delete set null,
  340. child_session_id text not null references session(id) on delete cascade,
  341. role text not null,
  342. depth integer not null default 0,
  343. path text not null,
  344. phase text,
  345. label text,
  346. agent_type text,
  347. model text,
  348. status text not null,
  349. time_created integer not null,
  350. time_updated integer not null,
  351. unique(child_session_id)
  352. );
  353. create index if not exists session_task_link_root_workflow_idx
  354. on session_task_link(root_workflow_run_id, depth, path);
  355. create index if not exists session_task_link_parent_idx
  356. on session_task_link(parent_link_id);
  357. create index if not exists session_task_link_activity_idx
  358. on session_task_link(activity_id);
  359. `,
  360. },
  361. {
  362. appVersion: "0.13.0",
  363. id: "0008_workflow_definition_scope",
  364. touchedTables: ["workflow_definition"],
  365. sql: `
  366. alter table workflow_definition
  367. add column scope text not null default 'explicit'
  368. check(scope in ('builtin', 'explicit', 'project', 'user'));
  369. `,
  370. },
  371. {
  372. appVersion: "0.14.0",
  373. id: "0009_session_title_metadata",
  374. touchedTables: ["session"],
  375. sql: `
  376. alter table session
  377. add column title_source text not null default 'first_input'
  378. check(title_source in ('default', 'first_input', 'generated', 'custom'));
  379. alter table session
  380. add column title_message_id text;
  381. alter table session
  382. add column time_title_updated integer;
  383. `,
  384. },
  385. {
  386. appVersion: "0.15.0",
  387. id: "0010_usage_observability",
  388. touchedTables: ["model_usage", "tool_usage", "turn_usage"],
  389. sql: `
  390. create table if not exists model_usage (
  391. id text primary key,
  392. logical_request_id text not null,
  393. attempt_index integer not null default 0,
  394. session_id text not null references session(id) on delete cascade,
  395. turn_id text,
  396. trace_id text,
  397. span_id text,
  398. assistant_message_id text,
  399. parent_user_message_id text,
  400. query_source text not null,
  401. provider_id text not null,
  402. model_id text not null,
  403. variant text,
  404. agent text,
  405. mode text,
  406. task_type text,
  407. status text not null check(status in ('running', 'completed', 'error', 'cancelled')),
  408. started_at integer not null,
  409. first_token_at integer,
  410. completed_at integer,
  411. duration_ms integer,
  412. time_to_first_token_ms integer,
  413. finish_reason text,
  414. tool_call_count integer not null default 0,
  415. input_tokens integer not null default 0,
  416. output_tokens integer not null default 0,
  417. reasoning_tokens integer not null default 0,
  418. cache_creation_input_tokens integer not null default 0,
  419. cache_read_input_tokens integer not null default 0,
  420. provider_total_tokens integer,
  421. computed_total_tokens integer not null default 0,
  422. retry_count integer not null default 0,
  423. retryable integer not null default 0 check(retryable in (0, 1)),
  424. cancelled_by_user integer not null default 0 check(cancelled_by_user in (0, 1)),
  425. context_exceeded integer not null default 0 check(context_exceeded in (0, 1)),
  426. error_type text,
  427. error_code text,
  428. error_message text,
  429. raw_usage_json text,
  430. provider_metadata_json text
  431. );
  432. create index if not exists model_usage_started_model_idx
  433. on model_usage(started_at, provider_id, model_id);
  434. create index if not exists model_usage_session_turn_idx
  435. on model_usage(session_id, turn_id);
  436. create index if not exists model_usage_trace_idx
  437. on model_usage(trace_id);
  438. create index if not exists model_usage_query_source_idx
  439. on model_usage(query_source);
  440. create table if not exists turn_usage (
  441. session_id text not null references session(id) on delete cascade,
  442. turn_id text not null,
  443. trace_id text,
  444. user_message_id text,
  445. status text not null check(status in ('running', 'completed', 'error', 'cancelled')),
  446. started_at integer not null,
  447. first_model_start_at integer,
  448. first_token_at integer,
  449. completed_at integer,
  450. duration_ms integer,
  451. time_to_first_token_ms integer,
  452. model_request_count integer not null default 0,
  453. model_retry_count integer not null default 0,
  454. tool_call_count integer not null default 0,
  455. tool_error_count integer not null default 0,
  456. input_tokens integer not null default 0,
  457. output_tokens integer not null default 0,
  458. reasoning_tokens integer not null default 0,
  459. cache_creation_input_tokens integer not null default 0,
  460. cache_read_input_tokens integer not null default 0,
  461. computed_total_tokens integer not null default 0,
  462. retryable integer not null default 0 check(retryable in (0, 1)),
  463. cancelled_by_user integer not null default 0 check(cancelled_by_user in (0, 1)),
  464. context_exceeded integer not null default 0 check(context_exceeded in (0, 1)),
  465. error_type text,
  466. error_code text,
  467. primary key(session_id, turn_id)
  468. );
  469. create index if not exists turn_usage_started_idx
  470. on turn_usage(started_at);
  471. create table if not exists tool_usage (
  472. id text primary key,
  473. session_id text not null references session(id) on delete cascade,
  474. turn_id text,
  475. trace_id text,
  476. tool_call_id text not null,
  477. tool_name text not null,
  478. side_effect_scope text,
  479. read_only integer check(read_only in (0, 1)),
  480. destructive integer check(destructive in (0, 1)),
  481. approval_status text,
  482. status text not null check(status in ('running', 'completed', 'error', 'cancelled')),
  483. started_at integer not null,
  484. first_output_at integer,
  485. completed_at integer,
  486. duration_ms integer,
  487. time_to_first_output_ms integer,
  488. exit_code integer,
  489. output_bytes integer not null default 0,
  490. stdout_bytes integer not null default 0,
  491. stderr_bytes integer not null default 0,
  492. truncated integer not null default 0 check(truncated in (0, 1)),
  493. retry_count integer not null default 0,
  494. retryable integer not null default 0 check(retryable in (0, 1)),
  495. cancelled_by_user integer not null default 0 check(cancelled_by_user in (0, 1)),
  496. error_type text,
  497. error_code text,
  498. error_message text
  499. );
  500. create unique index if not exists tool_usage_session_tool_call_idx
  501. on tool_usage(session_id, tool_call_id);
  502. create index if not exists tool_usage_started_tool_idx
  503. on tool_usage(started_at, tool_name);
  504. create index if not exists tool_usage_session_turn_idx
  505. on tool_usage(session_id, turn_id);
  506. `,
  507. },
  508. {
  509. appVersion: "0.15.0",
  510. id: "0011_session_target_summary_title",
  511. touchedTables: ["session_target"],
  512. sql: `
  513. alter table session_target add column summary_title text;
  514. `,
  515. },
  516. {
  517. appVersion: "0.15.0",
  518. id: "0012_session_trace_id",
  519. touchedTables: ["session"],
  520. sql: `
  521. alter table session add column trace_id text;
  522. create index if not exists session_trace_idx on session(trace_id);
  523. `,
  524. },
  525. {
  526. appVersion: "0.15.0",
  527. id: "0013_session_target_active_run_accounting",
  528. touchedTables: ["session_target"],
  529. sql: `
  530. alter table session_target add column active_input_id text;
  531. alter table session_target add column active_run_started_at integer;
  532. alter table session_target add column active_run_last_seen_at integer;
  533. `,
  534. },
  535. {
  536. appVersion: "0.15.0",
  537. id: "0014_message_part_sequence",
  538. touchedTables: ["message", "part"],
  539. sql: `
  540. alter table message add column sequence integer;
  541. alter table part add column sequence integer;
  542. with ordered_message as (
  543. select
  544. id,
  545. row_number() over (
  546. partition by session_id
  547. order by time_created, rowid
  548. ) - 1 as stable_sequence
  549. from message
  550. )
  551. update message
  552. set sequence = (
  553. select stable_sequence
  554. from ordered_message
  555. where ordered_message.id = message.id
  556. )
  557. where sequence is null;
  558. with ordered_part as (
  559. select
  560. id,
  561. row_number() over (
  562. partition by message_id
  563. order by time_created, rowid
  564. ) - 1 as stable_sequence
  565. from part
  566. )
  567. update part
  568. set sequence = (
  569. select stable_sequence
  570. from ordered_part
  571. where ordered_part.id = part.id
  572. )
  573. where sequence is null;
  574. create index if not exists message_session_sequence_idx
  575. on message(session_id, sequence, time_created, id);
  576. create index if not exists part_message_sequence_idx
  577. on part(message_id, sequence, time_created, id);
  578. create index if not exists part_session_message_sequence_idx
  579. on part(session_id, message_id, sequence);
  580. `,
  581. },
  582. {
  583. appVersion: "0.15.2",
  584. id: "0015_message_part_sequence_backfill_and_guard",
  585. touchedTables: ["message", "part"],
  586. // 背景(16-timeline-authority-plan P0):0014 backfill 之后仍持续出现 NULL sequence
  587. // (本机观测 message 1,690 / part 5,933 行,跨 331 sessions),主要嫌疑是旧版本二进制
  588. // 并存写同一 DB(其 INSERT 不含 sequence 列)。本迁移做两件事:
  589. // 1. 增量 backfill:只补 NULL 行,序号从各 scope 现有 max(sequence)+1 起、按
  590. // time_created/rowid 排——与读路径 fallback(sequence is null 排在非空之后)完全
  591. // 一致,backfill 前后 hydrate 顺序不变。不能复用 0014 的 row_number-1 写法:
  592. // 它按全量行编号,混排数据下会与既有 sequence 撞号并把 NULL 行重排到前面。
  593. // 2. AFTER INSERT 触发器兜底:旧二进制再写入 NULL sequence 时自动补当前 scope 队尾,
  594. // 从源头阻止新的 NULL 产生;新代码路径 sequence 恒非空,触发器不生效。
  595. sql: `
  596. with session_max as (
  597. select session_id, coalesce(max(sequence), -1) as max_sequence
  598. from message
  599. group by session_id
  600. ),
  601. ordered_null_message as (
  602. select
  603. m.id as id,
  604. sm.max_sequence + row_number() over (
  605. partition by m.session_id
  606. order by m.time_created, m.rowid
  607. ) as stable_sequence
  608. from message m
  609. join session_max sm on sm.session_id = m.session_id
  610. where m.sequence is null
  611. )
  612. update message
  613. set sequence = (
  614. select stable_sequence
  615. from ordered_null_message
  616. where ordered_null_message.id = message.id
  617. )
  618. where sequence is null;
  619. with message_max as (
  620. select message_id, coalesce(max(sequence), -1) as max_sequence
  621. from part
  622. group by message_id
  623. ),
  624. ordered_null_part as (
  625. select
  626. p.id as id,
  627. mm.max_sequence + row_number() over (
  628. partition by p.message_id
  629. order by p.time_created, p.rowid
  630. ) as stable_sequence
  631. from part p
  632. join message_max mm on mm.message_id = p.message_id
  633. where p.sequence is null
  634. )
  635. update part
  636. set sequence = (
  637. select stable_sequence
  638. from ordered_null_part
  639. where ordered_null_part.id = part.id
  640. )
  641. where sequence is null;
  642. create trigger if not exists message_sequence_autofill
  643. after insert on message
  644. when new.sequence is null
  645. begin
  646. update message
  647. set sequence = (
  648. select coalesce(max(sequence), -1) + 1
  649. from message
  650. where session_id = new.session_id
  651. )
  652. where id = new.id;
  653. end;
  654. create trigger if not exists part_sequence_autofill
  655. after insert on part
  656. when new.sequence is null
  657. begin
  658. update part
  659. set sequence = (
  660. select coalesce(max(sequence), -1) + 1
  661. from part
  662. where message_id = new.message_id
  663. )
  664. where id = new.id;
  665. end;
  666. `,
  667. },
  668. {
  669. appVersion: "0.15.2",
  670. id: "0016_session_input_ledger",
  671. touchedTables: ["session_input"],
  672. // session_input 账本(16-timeline-authority-plan P3):输入的 durable 生命周期
  673. // admitted -> promoted / cancelled / discarded。旧实现里队列/唤醒的存在性只在
  674. // 进程内存(事件日志也是内存的),崩溃即静默丢;账本让「queue 消失但不进
  675. // history」不可能静默发生,并为输入类 command 提供 durable 幂等。
  676. sql: `
  677. create table if not exists session_input (
  678. id text primary key,
  679. session_id text not null references session(id) on delete cascade,
  680. kind text not null,
  681. delivery text not null check(delivery in ('guide', 'queue')),
  682. payload text not null,
  683. admitted_sequence integer not null,
  684. promoted_sequence integer,
  685. promoted_message_id text,
  686. status text not null check(status in ('admitted', 'promoted', 'cancelled', 'discarded')),
  687. status_reason text,
  688. time_created integer not null,
  689. time_updated integer not null
  690. );
  691. create index if not exists session_input_session_admitted_idx
  692. on session_input(session_id, admitted_sequence);
  693. create index if not exists session_input_session_status_idx
  694. on session_input(session_id, status);
  695. `,
  696. },
  697. {
  698. appVersion: "0.15.2",
  699. id: "0017_session_input_start_now_delivery",
  700. touchedTables: ["session_input", "session_input_before_start_now"],
  701. // 05C:所有 input command 都在执行前落 durable admission,startNow 也需要独立
  702. // delivery,不能伪装成 queue。SQLite 不能原地修改 CHECK,必须重建表并保全账本。
  703. sql: `
  704. alter table session_input rename to session_input_before_start_now;
  705. create table session_input (
  706. id text primary key,
  707. session_id text not null references session(id) on delete cascade,
  708. kind text not null,
  709. delivery text not null check(delivery in ('startNow', 'guide', 'queue')),
  710. payload text not null,
  711. admitted_sequence integer not null,
  712. promoted_sequence integer,
  713. promoted_message_id text,
  714. status text not null check(status in ('admitted', 'promoted', 'cancelled', 'discarded')),
  715. status_reason text,
  716. time_created integer not null,
  717. time_updated integer not null
  718. );
  719. insert into session_input (
  720. id, session_id, kind, delivery, payload, admitted_sequence,
  721. promoted_sequence, promoted_message_id, status, status_reason,
  722. time_created, time_updated
  723. )
  724. select
  725. id, session_id, kind, delivery, payload, admitted_sequence,
  726. promoted_sequence, promoted_message_id, status, status_reason,
  727. time_created, time_updated
  728. from session_input_before_start_now;
  729. drop table session_input_before_start_now;
  730. create index session_input_session_admitted_idx
  731. on session_input(session_id, admitted_sequence);
  732. create index session_input_session_status_idx
  733. on session_input(session_id, status);
  734. `,
  735. },
  736. {
  737. appVersion: "0.15.2",
  738. id: "0018_session_input_failed_status",
  739. touchedTables: ["session_input", "session_input_before_failed_status"],
  740. // Bugfix:fork bundle 提交后 child runtime 仍可能同步启动失败。该输入已经被 parent
  741. // accepted fact 接受,不能伪装成 cancelled/discarded;新增 durable failed 终态,并通过
  742. // 重建 CHECK 保证旧库升级后也能写入,重启不会再次消费或改写它。
  743. sql: `
  744. alter table session_input rename to session_input_before_failed_status;
  745. create table session_input (
  746. id text primary key,
  747. session_id text not null references session(id) on delete cascade,
  748. kind text not null,
  749. delivery text not null check(delivery in ('startNow', 'guide', 'queue')),
  750. payload text not null,
  751. admitted_sequence integer not null,
  752. promoted_sequence integer,
  753. promoted_message_id text,
  754. status text not null check(status in ('admitted', 'promoted', 'cancelled', 'discarded', 'failed')),
  755. status_reason text,
  756. time_created integer not null,
  757. time_updated integer not null
  758. );
  759. insert into session_input (
  760. id, session_id, kind, delivery, payload, admitted_sequence,
  761. promoted_sequence, promoted_message_id, status, status_reason,
  762. time_created, time_updated
  763. )
  764. select
  765. id, session_id, kind, delivery, payload, admitted_sequence,
  766. promoted_sequence, promoted_message_id, status, status_reason,
  767. time_created, time_updated
  768. from session_input_before_failed_status;
  769. drop table session_input_before_failed_status;
  770. create index session_input_session_admitted_idx
  771. on session_input(session_id, admitted_sequence);
  772. create index session_input_session_status_idx
  773. on session_input(session_id, status);
  774. `,
  775. },
  776. {
  777. appVersion: "0.16.5",
  778. id: "0019_dwf_journal",
  779. touchedTables: ["dwf_run", "dwf_actor", "dwf_artifact", "dwf_node", "dwf_event"],
  780. // dynamic-workflow 执行引擎的 durable journal(docs/execution-engine.md 的 "Journal & storage")。
  781. // legacy 的 workflow_* 表只是模板不是家:dwf_* 自成一套,与既有 workflow 机制彼此独立。
  782. //
  783. // 本条是 beta 前把开发期 0019–0030 十二条迁移**压成的单一基线**:四张表一次建齐、形状即
  784. // 0030 之后的终态。中间态(三次为放宽 CHECK 的整表重建、0028 的删列改名)只存在于
  785. // 内部预览库里,收敛办法是删掉四张 dwf_* 表并清掉 schema_migration 里的 dwf 记账行,
  786. // 下次启动由本条重建。beta 之后本条
  787. // 不可再改:runner 按 checksum 记账,历史迁移只能追加。
  788. //
  789. // 只为占住 0019 这个槽位,让
  790. // staging 后续迁移从 0020 起编号,功能分支合回时 ledger 不会撞号。四张表在功能落地前闲置无害。
  791. //
  792. // 几条刻意为之的设计:
  793. // 1) parent_session_id / session_id 是纯 text,不加 FOREIGN KEY——子代理会话跑在内存
  794. // event store 上、没有 session 行,而 runner 开着 pragma foreign_keys = on,真加 FK
  795. // 会把合法的 journal 记录挡在门外。dwf_* 之外的任何表都不被引用,也不引用它们。
  796. // 2) dwf_run 没有节点上限 / token 预算列:run 级
  797. // token 用量只作观察面,即 spent_tokens。
  798. // 3) dwf_node 的 unique(run_id, actor_id, actor_seq) 不可实现:putNode 是准入→结算→统计
  799. // 回填的 upsert,actor 坐标分散在三个可空列上;每子代理的 actor_seq 唯一性由引擎守。
  800. // report / artifact 行有行无节点:一次写入、status 恒为 completed、actor 三列全空。
  801. // 4) 可空列一律「NULL 即缺席」:result_json / name / tool_call_id / args_json /
  802. // resumed_from / resolved_model / message_boundary / artifact_id / input_json 都解码成
  803. // 缺席的键(args_json 解成 `{}`),不存哑值。
  804. // 5) 索引即查询形状(列序反了就只能全表扫):
  805. // - dwf_run_cwd_idx:按 cwd 枚举历史 run,「cwd 等值 + time_updated 倒序 + limit」。
  806. // - dwf_node_artifact_idx:本 run 的产物行与按 id 取带标签的 report 行。
  807. // - dwf_event_artifact_idx:看板取数按 journal sequence 分页,取数源是 dwf_event 而不是
  808. // dwf_node;表达式索引(SQLite ≥ 3.9)让它不必扫整条 journal。第三列 sequence 不是
  809. // 装饰——没有它规划器宁可走 unique(run_id, sequence) 的自动索引再逐行筛产物。
  810. sql: `
  811. create table if not exists dwf_run (
  812. id text primary key,
  813. parent_session_id text,
  814. cwd text,
  815. name text,
  816. script_text text,
  817. script_hash text,
  818. args_json text,
  819. tool_call_id text,
  820. resumed_from text,
  821. caps_max_concurrency integer not null,
  822. spent_tokens integer not null default 0,
  823. status text not null check(status in (
  824. 'pending',
  825. 'running',
  826. 'completed',
  827. 'failed',
  828. 'cancelled'
  829. )),
  830. result_json text,
  831. failure_json text,
  832. time_created integer not null,
  833. time_updated integer not null
  834. );
  835. create index if not exists dwf_run_cwd_idx on dwf_run(cwd, time_updated);
  836. create table if not exists dwf_actor (
  837. id integer primary key autoincrement,
  838. run_id text not null references dwf_run(id) on delete cascade,
  839. site_id text not null,
  840. ordinal integer not null,
  841. name text,
  842. persona_json text,
  843. resolved_model text,
  844. session_id text,
  845. time_created integer not null,
  846. time_updated integer not null,
  847. unique(run_id, site_id, ordinal)
  848. );
  849. create index if not exists dwf_actor_run_idx on dwf_actor(run_id);
  850. create table if not exists dwf_node (
  851. id integer primary key autoincrement,
  852. run_id text not null references dwf_run(id) on delete cascade,
  853. site_id text not null,
  854. ordinal integer not null,
  855. kind text not null check(kind in ('ask', 'world-read', 'world-run', 'report', 'artifact')),
  856. actor_site_id text,
  857. actor_ordinal integer,
  858. actor_seq integer,
  859. input_hash text not null,
  860. input_json text,
  861. status text not null check(status in ('running', 'completed', 'failed')),
  862. result_json text,
  863. error_json text,
  864. stats_json text,
  865. message_boundary integer,
  866. artifact_id text,
  867. time_created integer not null,
  868. time_updated integer not null,
  869. unique(run_id, site_id, ordinal)
  870. );
  871. create index if not exists dwf_node_run_idx on dwf_node(run_id);
  872. create index if not exists dwf_node_artifact_idx on dwf_node(run_id, artifact_id);
  873. create table if not exists dwf_event (
  874. id integer primary key autoincrement,
  875. run_id text not null references dwf_run(id) on delete cascade,
  876. sequence integer not null,
  877. type text not null,
  878. payload_json text not null,
  879. time_created integer not null,
  880. unique(run_id, sequence)
  881. );
  882. create index if not exists dwf_event_artifact_idx
  883. on dwf_event(run_id, json_extract(payload_json, '$.artifactId'), sequence);
  884. `,
  885. },
  886. {
  887. appVersion: "0.16.5",
  888. id: "0020_provider_model_selection",
  889. sql: PROVIDER_MODEL_SELECTION_MIGRATION_SQL,
  890. touchedTables: ["message", "part", "session_entry"],
  891. },
  892. {
  893. appVersion: "0.16.5",
  894. id: "0021_official_glm_selection",
  895. sql: OFFICIAL_GLM_SELECTION_MIGRATION_SQL,
  896. touchedTables: ["session_entry"],
  897. },
  898. {
  899. appVersion: "0.16.5",
  900. id: "0022_backfilled_session_reasoning",
  901. sql: BACKFILLED_SESSION_REASONING_MIGRATION_SQL,
  902. touchedTables: ["session_entry"],
  903. },
  904. {
  905. appVersion: "0.16.9",
  906. id: "0023_session_soft_delete_outcome",
  907. previousIds: ["0020_session_soft_delete_outcome"],
  908. touchedTables: ["session"],
  909. apply: (db) => {
  910. const columns = new Set(
  911. db
  912. .prepare("pragma table_info(session)")
  913. .all()
  914. .map((row) => row.name),
  915. );
  916. if (!columns.has("time_deleted"))
  917. db.exec("alter table session add column time_deleted integer");
  918. if (!columns.has("last_outcome")) db.exec("alter table session add column last_outcome text");
  919. },
  920. sql: `
  921. alter table session add column time_deleted integer;
  922. alter table session add column last_outcome text;
  923. `,
  924. },
  925. { ...SESSION_DATABASE_IDENTITY_MIGRATION, touchedTables: ["zcode_database_identity"] },
  926. ];