opencastle 0.26.1 → 0.27.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (226) hide show
  1. package/README.md +7 -1
  2. package/bin/cli.mjs +10 -0
  3. package/dist/cli/agents.d.ts +3 -0
  4. package/dist/cli/agents.d.ts.map +1 -0
  5. package/dist/cli/agents.js +161 -0
  6. package/dist/cli/agents.js.map +1 -0
  7. package/dist/cli/baselines.d.ts +3 -0
  8. package/dist/cli/baselines.d.ts.map +1 -0
  9. package/dist/cli/baselines.js +128 -0
  10. package/dist/cli/baselines.js.map +1 -0
  11. package/dist/cli/convoy/engine.d.ts +68 -2
  12. package/dist/cli/convoy/engine.d.ts.map +1 -1
  13. package/dist/cli/convoy/engine.js +2102 -26
  14. package/dist/cli/convoy/engine.js.map +1 -1
  15. package/dist/cli/convoy/engine.test.js +1572 -70
  16. package/dist/cli/convoy/engine.test.js.map +1 -1
  17. package/dist/cli/convoy/events.d.ts +4 -1
  18. package/dist/cli/convoy/events.d.ts.map +1 -1
  19. package/dist/cli/convoy/events.js +74 -13
  20. package/dist/cli/convoy/events.js.map +1 -1
  21. package/dist/cli/convoy/events.test.js +154 -27
  22. package/dist/cli/convoy/events.test.js.map +1 -1
  23. package/dist/cli/convoy/expertise.d.ts +16 -0
  24. package/dist/cli/convoy/expertise.d.ts.map +1 -0
  25. package/dist/cli/convoy/expertise.js +121 -0
  26. package/dist/cli/convoy/expertise.js.map +1 -0
  27. package/dist/cli/convoy/expertise.test.d.ts +2 -0
  28. package/dist/cli/convoy/expertise.test.d.ts.map +1 -0
  29. package/dist/cli/convoy/expertise.test.js +96 -0
  30. package/dist/cli/convoy/expertise.test.js.map +1 -0
  31. package/dist/cli/convoy/export.test.js +1 -0
  32. package/dist/cli/convoy/export.test.js.map +1 -1
  33. package/dist/cli/convoy/formula.d.ts +19 -0
  34. package/dist/cli/convoy/formula.d.ts.map +1 -0
  35. package/dist/cli/convoy/formula.js +142 -0
  36. package/dist/cli/convoy/formula.js.map +1 -0
  37. package/dist/cli/convoy/formula.test.d.ts +2 -0
  38. package/dist/cli/convoy/formula.test.d.ts.map +1 -0
  39. package/dist/cli/convoy/formula.test.js +342 -0
  40. package/dist/cli/convoy/formula.test.js.map +1 -0
  41. package/dist/cli/convoy/gates.d.ts +128 -0
  42. package/dist/cli/convoy/gates.d.ts.map +1 -0
  43. package/dist/cli/convoy/gates.js +606 -0
  44. package/dist/cli/convoy/gates.js.map +1 -0
  45. package/dist/cli/convoy/gates.test.d.ts +2 -0
  46. package/dist/cli/convoy/gates.test.d.ts.map +1 -0
  47. package/dist/cli/convoy/gates.test.js +976 -0
  48. package/dist/cli/convoy/gates.test.js.map +1 -0
  49. package/dist/cli/convoy/health.d.ts +11 -0
  50. package/dist/cli/convoy/health.d.ts.map +1 -1
  51. package/dist/cli/convoy/health.js +54 -0
  52. package/dist/cli/convoy/health.js.map +1 -1
  53. package/dist/cli/convoy/health.test.js +56 -1
  54. package/dist/cli/convoy/health.test.js.map +1 -1
  55. package/dist/cli/convoy/issues.d.ts +8 -0
  56. package/dist/cli/convoy/issues.d.ts.map +1 -0
  57. package/dist/cli/convoy/issues.js +98 -0
  58. package/dist/cli/convoy/issues.js.map +1 -0
  59. package/dist/cli/convoy/issues.test.d.ts +2 -0
  60. package/dist/cli/convoy/issues.test.d.ts.map +1 -0
  61. package/dist/cli/convoy/issues.test.js +107 -0
  62. package/dist/cli/convoy/issues.test.js.map +1 -0
  63. package/dist/cli/convoy/knowledge.d.ts +5 -0
  64. package/dist/cli/convoy/knowledge.d.ts.map +1 -0
  65. package/dist/cli/convoy/knowledge.js +116 -0
  66. package/dist/cli/convoy/knowledge.js.map +1 -0
  67. package/dist/cli/convoy/knowledge.test.d.ts +2 -0
  68. package/dist/cli/convoy/knowledge.test.d.ts.map +1 -0
  69. package/dist/cli/convoy/knowledge.test.js +87 -0
  70. package/dist/cli/convoy/knowledge.test.js.map +1 -0
  71. package/dist/cli/convoy/lessons.d.ts +17 -0
  72. package/dist/cli/convoy/lessons.d.ts.map +1 -0
  73. package/dist/cli/convoy/lessons.js +149 -0
  74. package/dist/cli/convoy/lessons.js.map +1 -0
  75. package/dist/cli/convoy/lessons.test.d.ts +2 -0
  76. package/dist/cli/convoy/lessons.test.d.ts.map +1 -0
  77. package/dist/cli/convoy/lessons.test.js +135 -0
  78. package/dist/cli/convoy/lessons.test.js.map +1 -0
  79. package/dist/cli/convoy/lock.d.ts +13 -0
  80. package/dist/cli/convoy/lock.d.ts.map +1 -0
  81. package/dist/cli/convoy/lock.js +88 -0
  82. package/dist/cli/convoy/lock.js.map +1 -0
  83. package/dist/cli/convoy/lock.test.d.ts +2 -0
  84. package/dist/cli/convoy/lock.test.d.ts.map +1 -0
  85. package/dist/cli/convoy/lock.test.js +136 -0
  86. package/dist/cli/convoy/lock.test.js.map +1 -0
  87. package/dist/cli/convoy/merge.d.ts +4 -0
  88. package/dist/cli/convoy/merge.d.ts.map +1 -1
  89. package/dist/cli/convoy/merge.js +18 -1
  90. package/dist/cli/convoy/merge.js.map +1 -1
  91. package/dist/cli/convoy/merge.test.js +6 -7
  92. package/dist/cli/convoy/merge.test.js.map +1 -1
  93. package/dist/cli/convoy/partition.d.ts +51 -0
  94. package/dist/cli/convoy/partition.d.ts.map +1 -0
  95. package/dist/cli/convoy/partition.js +186 -0
  96. package/dist/cli/convoy/partition.js.map +1 -0
  97. package/dist/cli/convoy/partition.test.d.ts +2 -0
  98. package/dist/cli/convoy/partition.test.d.ts.map +1 -0
  99. package/dist/cli/convoy/partition.test.js +315 -0
  100. package/dist/cli/convoy/partition.test.js.map +1 -0
  101. package/dist/cli/convoy/pipeline.test.js +6 -0
  102. package/dist/cli/convoy/pipeline.test.js.map +1 -1
  103. package/dist/cli/convoy/store.d.ts +47 -5
  104. package/dist/cli/convoy/store.d.ts.map +1 -1
  105. package/dist/cli/convoy/store.js +525 -19
  106. package/dist/cli/convoy/store.js.map +1 -1
  107. package/dist/cli/convoy/store.test.js +1345 -12
  108. package/dist/cli/convoy/store.test.js.map +1 -1
  109. package/dist/cli/convoy/types.d.ts +156 -2
  110. package/dist/cli/convoy/types.d.ts.map +1 -1
  111. package/dist/cli/destroy.d.ts +3 -0
  112. package/dist/cli/destroy.d.ts.map +1 -0
  113. package/dist/cli/destroy.js +69 -0
  114. package/dist/cli/destroy.js.map +1 -0
  115. package/dist/cli/destroy.test.d.ts +2 -0
  116. package/dist/cli/destroy.test.d.ts.map +1 -0
  117. package/dist/cli/destroy.test.js +116 -0
  118. package/dist/cli/destroy.test.js.map +1 -0
  119. package/dist/cli/gitignore.d.ts +9 -0
  120. package/dist/cli/gitignore.d.ts.map +1 -1
  121. package/dist/cli/gitignore.js +29 -0
  122. package/dist/cli/gitignore.js.map +1 -1
  123. package/dist/cli/plan.d.ts +3 -0
  124. package/dist/cli/plan.d.ts.map +1 -0
  125. package/dist/cli/plan.js +288 -0
  126. package/dist/cli/plan.js.map +1 -0
  127. package/dist/cli/run/adapters/claude.d.ts +2 -0
  128. package/dist/cli/run/adapters/claude.d.ts.map +1 -1
  129. package/dist/cli/run/adapters/claude.js +89 -49
  130. package/dist/cli/run/adapters/claude.js.map +1 -1
  131. package/dist/cli/run/adapters/claude.test.d.ts +2 -0
  132. package/dist/cli/run/adapters/claude.test.d.ts.map +1 -0
  133. package/dist/cli/run/adapters/claude.test.js +205 -0
  134. package/dist/cli/run/adapters/claude.test.js.map +1 -0
  135. package/dist/cli/run/adapters/copilot.d.ts +1 -0
  136. package/dist/cli/run/adapters/copilot.d.ts.map +1 -1
  137. package/dist/cli/run/adapters/copilot.js +84 -46
  138. package/dist/cli/run/adapters/copilot.js.map +1 -1
  139. package/dist/cli/run/adapters/copilot.test.d.ts +2 -0
  140. package/dist/cli/run/adapters/copilot.test.d.ts.map +1 -0
  141. package/dist/cli/run/adapters/copilot.test.js +195 -0
  142. package/dist/cli/run/adapters/copilot.test.js.map +1 -0
  143. package/dist/cli/run/adapters/cursor.d.ts +1 -0
  144. package/dist/cli/run/adapters/cursor.d.ts.map +1 -1
  145. package/dist/cli/run/adapters/cursor.js +83 -47
  146. package/dist/cli/run/adapters/cursor.js.map +1 -1
  147. package/dist/cli/run/adapters/cursor.test.d.ts +2 -0
  148. package/dist/cli/run/adapters/cursor.test.d.ts.map +1 -0
  149. package/dist/cli/run/adapters/cursor.test.js +129 -0
  150. package/dist/cli/run/adapters/cursor.test.js.map +1 -0
  151. package/dist/cli/run/adapters/opencode.d.ts +1 -0
  152. package/dist/cli/run/adapters/opencode.d.ts.map +1 -1
  153. package/dist/cli/run/adapters/opencode.js +81 -47
  154. package/dist/cli/run/adapters/opencode.js.map +1 -1
  155. package/dist/cli/run/adapters/opencode.test.d.ts +2 -0
  156. package/dist/cli/run/adapters/opencode.test.d.ts.map +1 -0
  157. package/dist/cli/run/adapters/opencode.test.js +119 -0
  158. package/dist/cli/run/adapters/opencode.test.js.map +1 -0
  159. package/dist/cli/run/executor.js +1 -1
  160. package/dist/cli/run/executor.js.map +1 -1
  161. package/dist/cli/run/schema.d.ts.map +1 -1
  162. package/dist/cli/run/schema.js +245 -4
  163. package/dist/cli/run/schema.js.map +1 -1
  164. package/dist/cli/run/schema.test.js +669 -0
  165. package/dist/cli/run/schema.test.js.map +1 -1
  166. package/dist/cli/run.d.ts.map +1 -1
  167. package/dist/cli/run.js +362 -22
  168. package/dist/cli/run.js.map +1 -1
  169. package/dist/cli/types.d.ts +85 -2
  170. package/dist/cli/types.d.ts.map +1 -1
  171. package/dist/cli/types.js.map +1 -1
  172. package/dist/cli/watch.d.ts +15 -0
  173. package/dist/cli/watch.d.ts.map +1 -0
  174. package/dist/cli/watch.js +279 -0
  175. package/dist/cli/watch.js.map +1 -0
  176. package/package.json +1 -1
  177. package/src/cli/agents.ts +177 -0
  178. package/src/cli/baselines.ts +143 -0
  179. package/src/cli/convoy/engine.test.ts +1839 -70
  180. package/src/cli/convoy/engine.ts +2417 -38
  181. package/src/cli/convoy/events.test.ts +179 -38
  182. package/src/cli/convoy/events.ts +88 -16
  183. package/src/cli/convoy/expertise.test.ts +128 -0
  184. package/src/cli/convoy/expertise.ts +163 -0
  185. package/src/cli/convoy/export.test.ts +1 -0
  186. package/src/cli/convoy/formula.test.ts +405 -0
  187. package/src/cli/convoy/formula.ts +174 -0
  188. package/src/cli/convoy/gates.test.ts +1169 -0
  189. package/src/cli/convoy/gates.ts +774 -0
  190. package/src/cli/convoy/health.test.ts +64 -2
  191. package/src/cli/convoy/health.ts +80 -2
  192. package/src/cli/convoy/issues.test.ts +143 -0
  193. package/src/cli/convoy/issues.ts +136 -0
  194. package/src/cli/convoy/knowledge.test.ts +101 -0
  195. package/src/cli/convoy/knowledge.ts +132 -0
  196. package/src/cli/convoy/lessons.test.ts +188 -0
  197. package/src/cli/convoy/lessons.ts +164 -0
  198. package/src/cli/convoy/lock.test.ts +181 -0
  199. package/src/cli/convoy/lock.ts +103 -0
  200. package/src/cli/convoy/merge.test.ts +6 -7
  201. package/src/cli/convoy/merge.ts +19 -1
  202. package/src/cli/convoy/partition.test.ts +423 -0
  203. package/src/cli/convoy/partition.ts +232 -0
  204. package/src/cli/convoy/pipeline.test.ts +6 -0
  205. package/src/cli/convoy/store.test.ts +1512 -14
  206. package/src/cli/convoy/store.ts +676 -30
  207. package/src/cli/convoy/types.ts +170 -1
  208. package/src/cli/destroy.test.ts +141 -0
  209. package/src/cli/destroy.ts +88 -0
  210. package/src/cli/gitignore.ts +36 -0
  211. package/src/cli/plan.ts +316 -0
  212. package/src/cli/run/adapters/claude.test.ts +234 -0
  213. package/src/cli/run/adapters/claude.ts +45 -5
  214. package/src/cli/run/adapters/copilot.test.ts +224 -0
  215. package/src/cli/run/adapters/copilot.ts +34 -4
  216. package/src/cli/run/adapters/cursor.test.ts +144 -0
  217. package/src/cli/run/adapters/cursor.ts +33 -2
  218. package/src/cli/run/adapters/opencode.test.ts +135 -0
  219. package/src/cli/run/adapters/opencode.ts +30 -2
  220. package/src/cli/run/executor.ts +1 -1
  221. package/src/cli/run/schema.test.ts +758 -0
  222. package/src/cli/run/schema.ts +300 -25
  223. package/src/cli/run.ts +341 -21
  224. package/src/cli/types.ts +86 -1
  225. package/src/cli/watch.ts +298 -0
  226. package/src/dashboard/node_modules/.vite/deps/_metadata.json +6 -6
@@ -1,15 +1,15 @@
1
- import { mkdtempSync, rmSync } from 'node:fs';
1
+ import { mkdtempSync, rmSync, realpathSync, existsSync } from 'node:fs';
2
2
  import { tmpdir } from 'node:os';
3
3
  import { join } from 'node:path';
4
4
  import { DatabaseSync } from 'node:sqlite';
5
5
  import { describe, it, expect, beforeEach, afterEach } from 'vitest';
6
- import { createConvoyStore } from './store.js';
6
+ import { createConvoyStore, migrateSchema, FieldSizeLimitError } from './store.js';
7
7
  // ── helpers ───────────────────────────────────────────────────────────────────
8
8
  let tmpDir;
9
9
  let dbPath;
10
10
  let store;
11
11
  beforeEach(() => {
12
- tmpDir = mkdtempSync(join(tmpdir(), 'convoy-test-'));
12
+ tmpDir = realpathSync(mkdtempSync(join(tmpdir(), 'convoy-test-')));
13
13
  dbPath = join(tmpDir, 'test.db');
14
14
  store = createConvoyStore(dbPath);
15
15
  });
@@ -45,6 +45,7 @@ function makeTask(overrides = {}) {
45
45
  max_retries: 1,
46
46
  files: null,
47
47
  depends_on: null,
48
+ gates: null,
48
49
  ...overrides,
49
50
  };
50
51
  }
@@ -85,11 +86,11 @@ describe('DB creation', () => {
85
86
  db.close();
86
87
  expect(row.journal_mode).toBe('wal');
87
88
  });
88
- it('sets schema version to 4', () => {
89
+ it('sets schema version to 9', () => {
89
90
  const db = new DatabaseSync(dbPath);
90
91
  const row = db.prepare('PRAGMA user_version').get();
91
92
  db.close();
92
- expect(row.user_version).toBe(4);
93
+ expect(row.user_version).toBe(9);
93
94
  });
94
95
  it('creates all required tables', () => {
95
96
  const db = new DatabaseSync(dbPath);
@@ -103,6 +104,8 @@ describe('DB creation', () => {
103
104
  expect(names).toContain('worker');
104
105
  expect(names).toContain('event');
105
106
  expect(names).toContain('pipeline');
107
+ expect(names).toContain('artifact');
108
+ expect(names).toContain('agent_identity');
106
109
  });
107
110
  it('reopening an existing DB does not reset schema version', () => {
108
111
  store.close();
@@ -113,10 +116,9 @@ describe('DB creation', () => {
113
116
  store2.close();
114
117
  // Reassign so afterEach does not double-close
115
118
  store = createConvoyStore(dbPath);
116
- expect(row.user_version).toBe(4);
119
+ expect(row.user_version).toBe(9);
117
120
  });
118
121
  });
119
- // ── schema migration ─────────────────────────────────────────────────────────
120
122
  describe('schema migration', () => {
121
123
  it('schema migration v1 to v2 adds adapter column', () => {
122
124
  // Create a v1 database manually: task table without adapter column
@@ -187,8 +189,8 @@ describe('schema migration', () => {
187
189
  const version = verifyDb.prepare('PRAGMA user_version').get();
188
190
  verifyDb.close();
189
191
  expect(cols.map(c => c.name)).toContain('adapter');
190
- // v1 chains through v2→v3→v4 in one init, so final version is 4
191
- expect(version.user_version).toBe(4);
192
+ // v1 chains through v2→v3→v4→...→v7→v8→v9 in one init, so final version is 9
193
+ expect(version.user_version).toBe(9);
192
194
  });
193
195
  it('schema migration v2 to v3 adds cost columns', () => {
194
196
  // Create a v2 database manually (has adapter column but no cost columns)
@@ -268,7 +270,7 @@ describe('schema migration', () => {
268
270
  const convoyColNames = convoyCols.map(c => c.name);
269
271
  expect(convoyColNames).toContain('total_tokens');
270
272
  expect(convoyColNames).toContain('total_cost_usd');
271
- expect(version.user_version).toBe(4);
273
+ expect(version.user_version).toBe(9);
272
274
  });
273
275
  it('schema migration v1 to v3 chains correctly in a single init', () => {
274
276
  // Create a v1 database (task table without adapter or cost columns)
@@ -348,7 +350,7 @@ describe('schema migration', () => {
348
350
  const convoyColNames = convoyCols.map(c => c.name);
349
351
  expect(convoyColNames).toContain('total_tokens');
350
352
  expect(convoyColNames).toContain('total_cost_usd');
351
- expect(version.user_version).toBe(4);
353
+ expect(version.user_version).toBe(9);
352
354
  });
353
355
  it('schema migration v3 to v4 creates pipeline table and adds pipeline_id to convoy', () => {
354
356
  const v3DbPath = join(tmpDir, 'v3.db');
@@ -427,7 +429,7 @@ describe('schema migration', () => {
427
429
  verifyDb.close();
428
430
  expect(convoyCols.map(c => c.name)).toContain('pipeline_id');
429
431
  expect(tables.map(t => t.name)).toContain('pipeline');
430
- expect(version.user_version).toBe(4);
432
+ expect(version.user_version).toBe(9);
431
433
  });
432
434
  });
433
435
  // ── convoy CRUD ───────────────────────────────────────────────────────────────
@@ -873,4 +875,1335 @@ describe('close', () => {
873
875
  expect(() => freshStore.close()).not.toThrow();
874
876
  });
875
877
  });
878
+ // ── v4 schema helper ──────────────────────────────────────────────────────────
879
+ function createV4Db(path) {
880
+ const db = new DatabaseSync(path);
881
+ db.exec(`
882
+ CREATE TABLE convoy (
883
+ id TEXT PRIMARY KEY,
884
+ name TEXT NOT NULL,
885
+ spec_hash TEXT NOT NULL,
886
+ status TEXT NOT NULL DEFAULT 'pending',
887
+ branch TEXT,
888
+ created_at TEXT NOT NULL,
889
+ started_at TEXT,
890
+ finished_at TEXT,
891
+ spec_yaml TEXT NOT NULL,
892
+ total_tokens INTEGER,
893
+ total_cost_usd TEXT,
894
+ pipeline_id TEXT
895
+ );
896
+ CREATE TABLE pipeline (
897
+ id TEXT PRIMARY KEY,
898
+ name TEXT NOT NULL,
899
+ status TEXT NOT NULL DEFAULT 'pending',
900
+ branch TEXT,
901
+ spec_yaml TEXT NOT NULL,
902
+ convoy_specs TEXT NOT NULL,
903
+ created_at TEXT NOT NULL,
904
+ started_at TEXT,
905
+ finished_at TEXT,
906
+ total_tokens INTEGER,
907
+ total_cost_usd TEXT
908
+ );
909
+ CREATE TABLE task (
910
+ id TEXT PRIMARY KEY,
911
+ convoy_id TEXT NOT NULL REFERENCES convoy(id),
912
+ phase INTEGER NOT NULL,
913
+ prompt TEXT NOT NULL,
914
+ agent TEXT NOT NULL DEFAULT 'developer',
915
+ adapter TEXT,
916
+ model TEXT,
917
+ timeout_ms INTEGER NOT NULL DEFAULT 1800000,
918
+ status TEXT NOT NULL DEFAULT 'pending',
919
+ worker_id TEXT,
920
+ worktree TEXT,
921
+ output TEXT,
922
+ exit_code INTEGER,
923
+ started_at TEXT,
924
+ finished_at TEXT,
925
+ retries INTEGER NOT NULL DEFAULT 0,
926
+ max_retries INTEGER NOT NULL DEFAULT 1,
927
+ files TEXT,
928
+ depends_on TEXT,
929
+ prompt_tokens INTEGER,
930
+ completion_tokens INTEGER,
931
+ total_tokens INTEGER,
932
+ cost_usd TEXT
933
+ );
934
+ CREATE TABLE worker (
935
+ id TEXT PRIMARY KEY,
936
+ task_id TEXT REFERENCES task(id),
937
+ adapter TEXT NOT NULL,
938
+ pid INTEGER,
939
+ session_id TEXT,
940
+ status TEXT NOT NULL DEFAULT 'spawned',
941
+ worktree TEXT,
942
+ created_at TEXT NOT NULL,
943
+ finished_at TEXT,
944
+ last_heartbeat TEXT
945
+ );
946
+ CREATE TABLE event (
947
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
948
+ convoy_id TEXT REFERENCES convoy(id),
949
+ task_id TEXT,
950
+ worker_id TEXT,
951
+ type TEXT NOT NULL,
952
+ data TEXT,
953
+ created_at TEXT NOT NULL
954
+ );
955
+ `);
956
+ db.exec('PRAGMA user_version = 4');
957
+ return db;
958
+ }
959
+ // ── schema migration v4 → v5 ──────────────────────────────────────────────────
960
+ describe('schema migration v4 → v5', () => {
961
+ it('happy path: migrates from v4 to v5 and sets user_version to 5', () => {
962
+ const v4DbPath = join(tmpDir, 'v4-happy.db');
963
+ const db = createV4Db(v4DbPath);
964
+ migrateSchema(db, v4DbPath, 4, 5);
965
+ const row = db.prepare('PRAGMA user_version').get();
966
+ db.close();
967
+ expect(row.user_version).toBe(5);
968
+ });
969
+ it('new task columns exist after migration', () => {
970
+ const v4DbPath = join(tmpDir, 'v4-task-cols.db');
971
+ const db = createV4Db(v4DbPath);
972
+ migrateSchema(db, v4DbPath, 4, 5);
973
+ const cols = db.prepare('PRAGMA table_info(task)').all();
974
+ db.close();
975
+ const names = cols.map(c => c.name);
976
+ expect(names).toContain('gates');
977
+ expect(names).toContain('on_exhausted');
978
+ expect(names).toContain('injected');
979
+ expect(names).toContain('provenance');
980
+ expect(names).toContain('idempotency_key');
981
+ });
982
+ it('circuit_state column exists on convoy after migration', () => {
983
+ const v4DbPath = join(tmpDir, 'v4-convoy-col.db');
984
+ const db = createV4Db(v4DbPath);
985
+ migrateSchema(db, v4DbPath, 4, 5);
986
+ const cols = db.prepare('PRAGMA table_info(convoy)').all();
987
+ db.close();
988
+ expect(cols.map(c => c.name)).toContain('circuit_state');
989
+ });
990
+ it('dlq table created with correct columns after migration', () => {
991
+ const v4DbPath = join(tmpDir, 'v4-dlq.db');
992
+ const db = createV4Db(v4DbPath);
993
+ migrateSchema(db, v4DbPath, 4, 5);
994
+ const tables = db
995
+ .prepare("SELECT name FROM sqlite_master WHERE type='table'")
996
+ .all();
997
+ const dlqCols = db.prepare('PRAGMA table_info(dlq)').all();
998
+ db.close();
999
+ expect(tables.map(t => t.name)).toContain('dlq');
1000
+ const colNames = dlqCols.map(c => c.name);
1001
+ expect(colNames).toContain('id');
1002
+ expect(colNames).toContain('convoy_id');
1003
+ expect(colNames).toContain('task_id');
1004
+ expect(colNames).toContain('agent');
1005
+ expect(colNames).toContain('failure_type');
1006
+ expect(colNames).toContain('error_output');
1007
+ expect(colNames).toContain('attempts');
1008
+ expect(colNames).toContain('tokens_spent');
1009
+ expect(colNames).toContain('resolved');
1010
+ expect(colNames).toContain('resolution');
1011
+ expect(colNames).toContain('created_at');
1012
+ expect(colNames).toContain('resolved_at');
1013
+ });
1014
+ it('idx_task_idempotency partial unique index created after migration', () => {
1015
+ const v4DbPath = join(tmpDir, 'v4-index.db');
1016
+ const db = createV4Db(v4DbPath);
1017
+ migrateSchema(db, v4DbPath, 4, 5);
1018
+ const indexes = db
1019
+ .prepare("SELECT name FROM sqlite_master WHERE type='index' AND name='idx_task_idempotency'")
1020
+ .all();
1021
+ db.close();
1022
+ expect(indexes).toHaveLength(1);
1023
+ expect(indexes[0].name).toBe('idx_task_idempotency');
1024
+ });
1025
+ it('existing data intact after migration', () => {
1026
+ const v4DbPath = join(tmpDir, 'v4-data.db');
1027
+ const db = createV4Db(v4DbPath);
1028
+ db.prepare(`INSERT INTO convoy (id, name, spec_hash, status, branch, created_at, spec_yaml)
1029
+ VALUES ('convoy-test', 'Test', 'hash', 'pending', NULL, '2026-01-01T00:00:00.000Z', 'name: test')`).run();
1030
+ db.prepare(`INSERT INTO task (id, convoy_id, phase, prompt, agent, timeout_ms, status, retries, max_retries)
1031
+ VALUES ('task-test', 'convoy-test', 0, 'Do something', 'developer', 1800000, 'pending', 0, 1)`).run();
1032
+ migrateSchema(db, v4DbPath, 4, 5);
1033
+ const convoyCount = db.prepare('SELECT COUNT(*) as cnt FROM convoy').get();
1034
+ const taskCount = db.prepare('SELECT COUNT(*) as cnt FROM task').get();
1035
+ const convoy = db.prepare('SELECT id FROM convoy WHERE id = :id').get({ id: 'convoy-test' });
1036
+ db.close();
1037
+ expect(convoyCount.cnt).toBe(1);
1038
+ expect(taskCount.cnt).toBe(1);
1039
+ expect(convoy.id).toBe('convoy-test');
1040
+ });
1041
+ it('backup file created before migration', () => {
1042
+ const v4DbPath = join(tmpDir, 'v4-backup.db');
1043
+ const db = createV4Db(v4DbPath);
1044
+ migrateSchema(db, v4DbPath, 4, 5);
1045
+ db.close();
1046
+ expect(existsSync(`${v4DbPath}.v4.bak`)).toBe(true);
1047
+ });
1048
+ it('failure mode: rolls back on error, version stays at 4 and backup exists', () => {
1049
+ const v4DbPath = join(tmpDir, 'v4-fail.db');
1050
+ const db = createV4Db(v4DbPath);
1051
+ // Pre-add gates column so the first ALTER in migration will fail with duplicate column
1052
+ db.exec('ALTER TABLE task ADD COLUMN gates TEXT');
1053
+ expect(() => migrateSchema(db, v4DbPath, 4, 5)).toThrow();
1054
+ const row = db.prepare('PRAGMA user_version').get();
1055
+ expect(row.user_version).toBe(4);
1056
+ expect(existsSync(`${v4DbPath}.v4.bak`)).toBe(true);
1057
+ db.close();
1058
+ });
1059
+ it('rolls back and preserves backup on mid-migration failure', () => {
1060
+ const v4Path = join(tmpDir, 'v4-fail-rb.db');
1061
+ const v4db = createV4Db(v4Path);
1062
+ // Insert a test row to verify data integrity after rollback
1063
+ v4db
1064
+ .prepare("INSERT INTO convoy (id, name, spec_hash, status, created_at, spec_yaml) VALUES ('test-c', 'Test', 'hash', 'pending', '2026-01-01', 'yaml')")
1065
+ .run();
1066
+ // Sabotage: add a column that migration v4→v5 will try to add, causing it to fail
1067
+ v4db.exec('ALTER TABLE task ADD COLUMN gates TEXT');
1068
+ // Attempt migration — should fail because 'gates' column already exists
1069
+ expect(() => migrateSchema(v4db, v4Path, 4, 5)).toThrow(/Migration v4→v5 failed/);
1070
+ // Verify: user_version unchanged (still 4)
1071
+ const version = v4db.prepare('PRAGMA user_version').get().user_version;
1072
+ expect(version).toBe(4);
1073
+ // Verify: backup file exists and is a valid SQLite database
1074
+ const backupPath = `${v4Path}.v4.bak`;
1075
+ expect(existsSync(backupPath)).toBe(true);
1076
+ const backupDb = new DatabaseSync(backupPath);
1077
+ const backupRow = backupDb
1078
+ .prepare("SELECT id FROM convoy WHERE id = 'test-c'")
1079
+ .get();
1080
+ expect(backupRow?.id).toBe('test-c');
1081
+ backupDb.close();
1082
+ // Verify: original data intact in main DB (rollback preserved it)
1083
+ const origRow = v4db
1084
+ .prepare("SELECT id FROM convoy WHERE id = 'test-c'")
1085
+ .get();
1086
+ expect(origRow?.id).toBe('test-c');
1087
+ v4db.close();
1088
+ });
1089
+ });
1090
+ // ── helper: build a v5 database ───────────────────────────────────────────────
1091
+ function createV5Db(path) {
1092
+ // v5 = v4 schema + gates/on_exhausted/injected/provenance/idempotency_key on task
1093
+ // + circuit_state on convoy + dlq table
1094
+ const db = new DatabaseSync(path);
1095
+ db.exec(`
1096
+ CREATE TABLE convoy (
1097
+ id TEXT PRIMARY KEY,
1098
+ name TEXT NOT NULL,
1099
+ spec_hash TEXT NOT NULL,
1100
+ status TEXT NOT NULL DEFAULT 'pending',
1101
+ branch TEXT,
1102
+ created_at TEXT NOT NULL,
1103
+ started_at TEXT,
1104
+ finished_at TEXT,
1105
+ spec_yaml TEXT NOT NULL,
1106
+ total_tokens INTEGER,
1107
+ total_cost_usd TEXT,
1108
+ pipeline_id TEXT,
1109
+ circuit_state TEXT
1110
+ );
1111
+ CREATE TABLE pipeline (
1112
+ id TEXT PRIMARY KEY,
1113
+ name TEXT NOT NULL,
1114
+ status TEXT NOT NULL DEFAULT 'pending',
1115
+ branch TEXT,
1116
+ spec_yaml TEXT NOT NULL,
1117
+ convoy_specs TEXT NOT NULL,
1118
+ created_at TEXT NOT NULL,
1119
+ started_at TEXT,
1120
+ finished_at TEXT,
1121
+ total_tokens INTEGER,
1122
+ total_cost_usd TEXT
1123
+ );
1124
+ CREATE TABLE task (
1125
+ id TEXT PRIMARY KEY,
1126
+ convoy_id TEXT NOT NULL REFERENCES convoy(id),
1127
+ phase INTEGER NOT NULL,
1128
+ prompt TEXT NOT NULL,
1129
+ agent TEXT NOT NULL DEFAULT 'developer',
1130
+ adapter TEXT,
1131
+ model TEXT,
1132
+ timeout_ms INTEGER NOT NULL DEFAULT 1800000,
1133
+ status TEXT NOT NULL DEFAULT 'pending',
1134
+ worker_id TEXT,
1135
+ worktree TEXT,
1136
+ output TEXT,
1137
+ exit_code INTEGER,
1138
+ started_at TEXT,
1139
+ finished_at TEXT,
1140
+ retries INTEGER NOT NULL DEFAULT 0,
1141
+ max_retries INTEGER NOT NULL DEFAULT 1,
1142
+ files TEXT,
1143
+ depends_on TEXT,
1144
+ prompt_tokens INTEGER,
1145
+ completion_tokens INTEGER,
1146
+ total_tokens INTEGER,
1147
+ cost_usd TEXT,
1148
+ gates TEXT,
1149
+ on_exhausted TEXT NOT NULL DEFAULT 'dlq',
1150
+ injected INTEGER NOT NULL DEFAULT 0,
1151
+ provenance TEXT,
1152
+ idempotency_key TEXT
1153
+ );
1154
+ CREATE UNIQUE INDEX idx_task_idempotency ON task(convoy_id, idempotency_key)
1155
+ WHERE idempotency_key IS NOT NULL;
1156
+ CREATE TABLE worker (
1157
+ id TEXT PRIMARY KEY,
1158
+ task_id TEXT REFERENCES task(id),
1159
+ adapter TEXT NOT NULL,
1160
+ pid INTEGER,
1161
+ session_id TEXT,
1162
+ status TEXT NOT NULL DEFAULT 'spawned',
1163
+ worktree TEXT,
1164
+ created_at TEXT NOT NULL,
1165
+ finished_at TEXT,
1166
+ last_heartbeat TEXT
1167
+ );
1168
+ CREATE TABLE event (
1169
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
1170
+ convoy_id TEXT REFERENCES convoy(id),
1171
+ task_id TEXT,
1172
+ worker_id TEXT,
1173
+ type TEXT NOT NULL,
1174
+ data TEXT,
1175
+ created_at TEXT NOT NULL
1176
+ );
1177
+ CREATE TABLE dlq (
1178
+ id TEXT PRIMARY KEY,
1179
+ convoy_id TEXT NOT NULL REFERENCES convoy(id),
1180
+ task_id TEXT NOT NULL REFERENCES task(id),
1181
+ agent TEXT NOT NULL,
1182
+ failure_type TEXT NOT NULL,
1183
+ error_output TEXT,
1184
+ attempts INTEGER NOT NULL,
1185
+ tokens_spent INTEGER,
1186
+ escalation_task_id TEXT,
1187
+ resolved INTEGER NOT NULL DEFAULT 0,
1188
+ resolution TEXT,
1189
+ created_at TEXT NOT NULL,
1190
+ resolved_at TEXT
1191
+ );
1192
+ `);
1193
+ db.exec('PRAGMA user_version = 5');
1194
+ return db;
1195
+ }
1196
+ // ── schema migration v5 → v6 ──────────────────────────────────────────────────
1197
+ describe('schema migration v5 → v6', () => {
1198
+ it('happy path: migrates from v5 to v6 and sets user_version to 6', () => {
1199
+ const v5DbPath = join(tmpDir, 'v5-happy.db');
1200
+ const db = createV5Db(v5DbPath);
1201
+ migrateSchema(db, v5DbPath, 5, 6);
1202
+ const row = db.prepare('PRAGMA user_version').get();
1203
+ db.close();
1204
+ expect(row.user_version).toBe(6);
1205
+ });
1206
+ it('task_step table created with correct columns after migration', () => {
1207
+ const v5DbPath = join(tmpDir, 'v5-task-step.db');
1208
+ const db = createV5Db(v5DbPath);
1209
+ migrateSchema(db, v5DbPath, 5, 6);
1210
+ const tables = db
1211
+ .prepare("SELECT name FROM sqlite_master WHERE type='table'")
1212
+ .all();
1213
+ const stepCols = db.prepare('PRAGMA table_info(task_step)').all();
1214
+ db.close();
1215
+ expect(tables.map(t => t.name)).toContain('task_step');
1216
+ const colNames = stepCols.map(c => c.name);
1217
+ expect(colNames).toContain('id');
1218
+ expect(colNames).toContain('task_id');
1219
+ expect(colNames).toContain('step_index');
1220
+ expect(colNames).toContain('prompt');
1221
+ expect(colNames).toContain('gates');
1222
+ expect(colNames).toContain('status');
1223
+ expect(colNames).toContain('exit_code');
1224
+ expect(colNames).toContain('output');
1225
+ expect(colNames).toContain('started_at');
1226
+ expect(colNames).toContain('finished_at');
1227
+ });
1228
+ it('new task columns added after migration', () => {
1229
+ const v5DbPath = join(tmpDir, 'v5-task-cols.db');
1230
+ const db = createV5Db(v5DbPath);
1231
+ migrateSchema(db, v5DbPath, 5, 6);
1232
+ const cols = db.prepare('PRAGMA table_info(task)').all();
1233
+ db.close();
1234
+ const names = cols.map(c => c.name);
1235
+ expect(names).toContain('current_step');
1236
+ expect(names).toContain('total_steps');
1237
+ expect(names).toContain('review_level');
1238
+ expect(names).toContain('review_verdict');
1239
+ expect(names).toContain('review_tokens');
1240
+ expect(names).toContain('review_model');
1241
+ expect(names).toContain('panel_attempts');
1242
+ expect(names).toContain('dispute_id');
1243
+ });
1244
+ it('new convoy columns added after migration', () => {
1245
+ const v5DbPath = join(tmpDir, 'v5-convoy-cols.db');
1246
+ const db = createV5Db(v5DbPath);
1247
+ migrateSchema(db, v5DbPath, 5, 6);
1248
+ const cols = db.prepare('PRAGMA table_info(convoy)').all();
1249
+ db.close();
1250
+ const names = cols.map(c => c.name);
1251
+ expect(names).toContain('review_tokens_total');
1252
+ expect(names).toContain('review_budget');
1253
+ });
1254
+ it('existing data survives migration intact', () => {
1255
+ const v5DbPath = join(tmpDir, 'v5-data.db');
1256
+ const db = createV5Db(v5DbPath);
1257
+ db.prepare(`INSERT INTO convoy (id, name, spec_hash, status, branch, created_at, spec_yaml)
1258
+ VALUES ('convoy-v5', 'Test V5', 'hash5', 'pending', NULL, '2026-01-01T00:00:00.000Z', 'name: test')`).run();
1259
+ db.prepare(`INSERT INTO task (id, convoy_id, phase, prompt, agent, timeout_ms, status, retries, max_retries)
1260
+ VALUES ('task-v5', 'convoy-v5', 0, 'Do something', 'developer', 1800000, 'pending', 0, 1)`).run();
1261
+ migrateSchema(db, v5DbPath, 5, 6);
1262
+ const convoyCount = db.prepare('SELECT COUNT(*) as cnt FROM convoy').get();
1263
+ const taskCount = db.prepare('SELECT COUNT(*) as cnt FROM task').get();
1264
+ const convoy = db.prepare('SELECT id FROM convoy WHERE id = :id').get({ id: 'convoy-v5' });
1265
+ const task = db.prepare('SELECT id FROM task WHERE id = :id').get({ id: 'task-v5' });
1266
+ db.close();
1267
+ expect(convoyCount.cnt).toBe(1);
1268
+ expect(taskCount.cnt).toBe(1);
1269
+ expect(convoy.id).toBe('convoy-v5');
1270
+ expect(task.id).toBe('task-v5');
1271
+ });
1272
+ it('backup file created before migration', () => {
1273
+ const v5DbPath = join(tmpDir, 'v5-backup.db');
1274
+ const db = createV5Db(v5DbPath);
1275
+ migrateSchema(db, v5DbPath, 5, 6);
1276
+ db.close();
1277
+ expect(existsSync(`${v5DbPath}.v5.bak`)).toBe(true);
1278
+ });
1279
+ it('createConvoyStore on v5 database auto-migrates to v6', () => {
1280
+ const v5DbPath = join(tmpDir, 'v5-auto.db');
1281
+ const v5Db = createV5Db(v5DbPath);
1282
+ v5Db.prepare(`INSERT INTO convoy (id, name, spec_hash, status, branch, created_at, spec_yaml)
1283
+ VALUES ('convoy-auto', 'Auto', 'hash', 'pending', NULL, '2026-01-01T00:00:00.000Z', 'name: auto')`).run();
1284
+ v5Db.prepare(`INSERT INTO task (id, convoy_id, phase, prompt, agent, timeout_ms, status, retries, max_retries)
1285
+ VALUES ('task-auto', 'convoy-auto', 0, 'Do it', 'developer', 1800000, 'pending', 0, 1)`).run();
1286
+ v5Db.close();
1287
+ const migratedStore = createConvoyStore(v5DbPath);
1288
+ const v5Verify = new DatabaseSync(v5DbPath);
1289
+ const row = v5Verify.prepare('PRAGMA user_version').get();
1290
+ const taskStepTable = v5Verify.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='task_step'").get();
1291
+ const convoy = migratedStore.getConvoy('convoy-auto');
1292
+ const task = migratedStore.getTask('task-auto', 'convoy-auto');
1293
+ v5Verify.close();
1294
+ migratedStore.close();
1295
+ expect(row.user_version).toBe(9);
1296
+ expect(taskStepTable?.name).toBe('task_step');
1297
+ expect(convoy?.id).toBe('convoy-auto');
1298
+ expect(task?.id).toBe('task-auto');
1299
+ });
1300
+ it('failure mode: rolls back on error and version stays at 5', () => {
1301
+ const v5DbPath = join(tmpDir, 'v5-fail.db');
1302
+ const db = createV5Db(v5DbPath);
1303
+ // Pre-add current_step to trigger duplicate column error
1304
+ db.exec('ALTER TABLE task ADD COLUMN current_step INTEGER');
1305
+ expect(() => migrateSchema(db, v5DbPath, 5, 6)).toThrow();
1306
+ const row = db.prepare('PRAGMA user_version').get();
1307
+ expect(row.user_version).toBe(5);
1308
+ expect(existsSync(`${v5DbPath}.v5.bak`)).toBe(true);
1309
+ db.close();
1310
+ });
1311
+ });
1312
+ // ── updateTaskReview ──────────────────────────────────────────────────────────
1313
+ describe('updateTaskReview', () => {
1314
+ beforeEach(() => {
1315
+ store.insertConvoy(makeConvoy());
1316
+ store.insertTask(makeTask());
1317
+ });
1318
+ it('persists review_level and review_verdict', () => {
1319
+ store.updateTaskReview('task-1', 'convoy-1', {
1320
+ review_level: 'fast',
1321
+ review_verdict: 'pass',
1322
+ });
1323
+ const task = store.getTask('task-1', 'convoy-1');
1324
+ expect(task.review_level).toBe('fast');
1325
+ expect(task.review_verdict).toBe('pass');
1326
+ });
1327
+ it('persists review_tokens and review_model', () => {
1328
+ store.updateTaskReview('task-1', 'convoy-1', {
1329
+ review_tokens: 123,
1330
+ review_model: 'gpt-4',
1331
+ });
1332
+ const task = store.getTask('task-1', 'convoy-1');
1333
+ expect(task.review_tokens).toBe(123);
1334
+ expect(task.review_model).toBe('gpt-4');
1335
+ });
1336
+ it('increments panel_attempts correctly', () => {
1337
+ store.updateTaskReview('task-1', 'convoy-1', { panel_attempts: 1 });
1338
+ const task = store.getTask('task-1', 'convoy-1');
1339
+ expect(task.panel_attempts).toBe(1);
1340
+ });
1341
+ it('persists dispute_id', () => {
1342
+ store.updateTaskReview('task-1', 'convoy-1', { dispute_id: 'dispute-42' });
1343
+ const task = store.getTask('task-1', 'convoy-1');
1344
+ expect(task.dispute_id).toBe('dispute-42');
1345
+ });
1346
+ it('is a no-op when updates is empty', () => {
1347
+ store.updateTaskReview('task-1', 'convoy-1', {});
1348
+ const task = store.getTask('task-1', 'convoy-1');
1349
+ expect(task.review_level).toBeNull();
1350
+ });
1351
+ it('does not throw for non-existent task', () => {
1352
+ expect(() => store.updateTaskReview('ghost', 'convoy-1', { review_level: 'fast' })).not.toThrow();
1353
+ });
1354
+ });
1355
+ // ── updateConvoyReviewTokens ──────────────────────────────────────────────────
1356
+ describe('updateConvoyReviewTokens', () => {
1357
+ beforeEach(() => {
1358
+ store.insertConvoy(makeConvoy());
1359
+ });
1360
+ it('sets review_tokens_total on the convoy', () => {
1361
+ store.updateConvoyReviewTokens('convoy-1', 500);
1362
+ const convoy = store.getConvoy('convoy-1');
1363
+ expect(convoy.review_tokens_total).toBe(500);
1364
+ });
1365
+ it('overwrites with updated total on subsequent calls', () => {
1366
+ store.updateConvoyReviewTokens('convoy-1', 100);
1367
+ store.updateConvoyReviewTokens('convoy-1', 350);
1368
+ const convoy = store.getConvoy('convoy-1');
1369
+ expect(convoy.review_tokens_total).toBe(350);
1370
+ });
1371
+ it('review_tokens_total starts as null before any call', () => {
1372
+ const convoy = store.getConvoy('convoy-1');
1373
+ expect(convoy.review_tokens_total).toBeNull();
1374
+ });
1375
+ });
1376
+ // ── schema migration v6→v7 ────────────────────────────────────────────────────
1377
+ describe('schema migration v6→v7 (drift detection columns)', () => {
1378
+ function createV6Db(dbPath) {
1379
+ const db = new DatabaseSync(dbPath);
1380
+ db.exec(`
1381
+ CREATE TABLE convoy (
1382
+ id TEXT PRIMARY KEY, name TEXT NOT NULL, spec_hash TEXT NOT NULL,
1383
+ status TEXT NOT NULL DEFAULT 'pending', branch TEXT, created_at TEXT NOT NULL,
1384
+ started_at TEXT, finished_at TEXT, spec_yaml TEXT NOT NULL,
1385
+ total_tokens INTEGER, total_cost_usd TEXT, pipeline_id TEXT,
1386
+ circuit_state TEXT, review_tokens_total INTEGER, review_budget INTEGER
1387
+ );
1388
+ CREATE TABLE task (
1389
+ id TEXT PRIMARY KEY, convoy_id TEXT NOT NULL, phase INTEGER NOT NULL,
1390
+ prompt TEXT NOT NULL, agent TEXT NOT NULL DEFAULT 'developer', adapter TEXT,
1391
+ model TEXT, timeout_ms INTEGER NOT NULL DEFAULT 1800000,
1392
+ status TEXT NOT NULL DEFAULT 'pending', worker_id TEXT, worktree TEXT,
1393
+ output TEXT, exit_code INTEGER, started_at TEXT, finished_at TEXT,
1394
+ retries INTEGER NOT NULL DEFAULT 0, max_retries INTEGER NOT NULL DEFAULT 1,
1395
+ files TEXT, depends_on TEXT, prompt_tokens INTEGER, completion_tokens INTEGER,
1396
+ total_tokens INTEGER, cost_usd TEXT, gates TEXT,
1397
+ on_exhausted TEXT NOT NULL DEFAULT 'dlq', injected INTEGER NOT NULL DEFAULT 0,
1398
+ provenance TEXT, idempotency_key TEXT, current_step INTEGER, total_steps INTEGER,
1399
+ review_level TEXT, review_verdict TEXT, review_tokens INTEGER, review_model TEXT,
1400
+ panel_attempts INTEGER NOT NULL DEFAULT 0, dispute_id TEXT
1401
+ );
1402
+ CREATE TABLE worker (
1403
+ id TEXT PRIMARY KEY, task_id TEXT, adapter TEXT NOT NULL, pid INTEGER,
1404
+ session_id TEXT, status TEXT NOT NULL DEFAULT 'spawned', worktree TEXT,
1405
+ created_at TEXT NOT NULL, finished_at TEXT, last_heartbeat TEXT
1406
+ );
1407
+ CREATE TABLE event (
1408
+ id INTEGER PRIMARY KEY AUTOINCREMENT, convoy_id TEXT, task_id TEXT,
1409
+ worker_id TEXT, type TEXT NOT NULL, data TEXT, created_at TEXT NOT NULL
1410
+ );
1411
+ CREATE TABLE task_step (
1412
+ id INTEGER PRIMARY KEY AUTOINCREMENT, task_id TEXT NOT NULL, step_index INTEGER NOT NULL,
1413
+ prompt TEXT NOT NULL, gates TEXT, status TEXT NOT NULL DEFAULT 'pending',
1414
+ exit_code INTEGER, output TEXT, started_at TEXT, finished_at TEXT
1415
+ );
1416
+ CREATE TABLE dlq (
1417
+ id TEXT PRIMARY KEY, convoy_id TEXT NOT NULL, task_id TEXT NOT NULL,
1418
+ agent TEXT NOT NULL, failure_type TEXT NOT NULL, error_output TEXT,
1419
+ attempts INTEGER NOT NULL, tokens_spent INTEGER, escalation_task_id TEXT,
1420
+ resolved INTEGER NOT NULL DEFAULT 0, resolution TEXT, created_at TEXT NOT NULL,
1421
+ resolved_at TEXT
1422
+ );
1423
+ CREATE TABLE pipeline (
1424
+ id TEXT PRIMARY KEY, name TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'pending',
1425
+ branch TEXT, spec_yaml TEXT NOT NULL, convoy_specs TEXT NOT NULL,
1426
+ created_at TEXT NOT NULL, started_at TEXT, finished_at TEXT,
1427
+ total_tokens INTEGER, total_cost_usd TEXT
1428
+ );
1429
+ `);
1430
+ db.exec('PRAGMA user_version = 6');
1431
+ return db;
1432
+ }
1433
+ it('migration v6→v7 adds drift_score and drift_retried columns to task table', () => {
1434
+ const v6DbPath = join(tmpDir, 'v6.db');
1435
+ const db = createV6Db(v6DbPath);
1436
+ db.close();
1437
+ const migratedStore = createConvoyStore(v6DbPath);
1438
+ migratedStore.close();
1439
+ const verifyDb = new DatabaseSync(v6DbPath);
1440
+ const cols = verifyDb.prepare('PRAGMA table_info(task)').all();
1441
+ const version = verifyDb.prepare('PRAGMA user_version').get();
1442
+ verifyDb.close();
1443
+ expect(cols.map(c => c.name)).toContain('drift_score');
1444
+ expect(cols.map(c => c.name)).toContain('drift_retried');
1445
+ expect(version.user_version).toBe(9);
1446
+ });
1447
+ it('new databases include drift_score and drift_retried in CREATE TABLE', () => {
1448
+ const cols = new DatabaseSync(dbPath)
1449
+ .prepare('PRAGMA table_info(task)')
1450
+ .all();
1451
+ const driftScore = cols.find(c => c.name === 'drift_score');
1452
+ const driftRetried = cols.find(c => c.name === 'drift_retried');
1453
+ expect(driftScore).toBeDefined();
1454
+ expect(driftRetried).toBeDefined();
1455
+ });
1456
+ it('failure mode: rolls back on error, version stays at 6 and backup exists', () => {
1457
+ const v6DbPath = join(tmpDir, 'v6-fail.db');
1458
+ const db = createV6Db(v6DbPath);
1459
+ // Pre-add drift_score to cause duplicate column error on migration
1460
+ db.exec('ALTER TABLE task ADD COLUMN drift_score REAL');
1461
+ expect(() => migrateSchema(db, v6DbPath, 6, 7)).toThrow();
1462
+ const row = db.prepare('PRAGMA user_version').get();
1463
+ expect(row.user_version).toBe(6);
1464
+ expect(existsSync(`${v6DbPath}.v6.bak`)).toBe(true);
1465
+ db.close();
1466
+ });
1467
+ });
1468
+ // ── updateTaskDrift ───────────────────────────────────────────────────────────
1469
+ describe('updateTaskDrift', () => {
1470
+ beforeEach(() => {
1471
+ store.insertConvoy(makeConvoy());
1472
+ store.insertTask(makeTask());
1473
+ });
1474
+ it('sets drift_score on the task', () => {
1475
+ store.updateTaskDrift('task-1', 'convoy-1', { drift_score: 0.72 });
1476
+ const task = store.getTask('task-1', 'convoy-1');
1477
+ expect(task.drift_score).toBeCloseTo(0.72);
1478
+ });
1479
+ it('sets drift_retried on the task', () => {
1480
+ store.updateTaskDrift('task-1', 'convoy-1', { drift_retried: 1 });
1481
+ const task = store.getTask('task-1', 'convoy-1');
1482
+ expect(task.drift_retried).toBe(1);
1483
+ });
1484
+ it('updates both fields at once', () => {
1485
+ store.updateTaskDrift('task-1', 'convoy-1', { drift_score: 0.4, drift_retried: 1 });
1486
+ const task = store.getTask('task-1', 'convoy-1');
1487
+ expect(task.drift_score).toBeCloseTo(0.4);
1488
+ expect(task.drift_retried).toBe(1);
1489
+ });
1490
+ it('no-op when called with empty updates', () => {
1491
+ const before = store.getTask('task-1', 'convoy-1');
1492
+ expect(() => store.updateTaskDrift('task-1', 'convoy-1', {})).not.toThrow();
1493
+ const after = store.getTask('task-1', 'convoy-1');
1494
+ expect(after.drift_score).toBe(before.drift_score);
1495
+ });
1496
+ it('drift_score and drift_retried start at NULL/0 for new tasks', () => {
1497
+ const task = store.getTask('task-1', 'convoy-1');
1498
+ expect(task.drift_score).toBeNull();
1499
+ expect(task.drift_retried).toBe(0);
1500
+ });
1501
+ });
1502
+ // ── updateTaskDisputeStatus ───────────────────────────────────────────────────
1503
+ describe('updateTaskDisputeStatus', () => {
1504
+ beforeEach(() => {
1505
+ store.insertConvoy(makeConvoy());
1506
+ store.insertTask(makeTask());
1507
+ });
1508
+ it('sets task status to disputed', () => {
1509
+ store.updateTaskDisputeStatus('task-1', 'convoy-1', 'disputed', 'dispute-task-1-123');
1510
+ const task = store.getTask('task-1', 'convoy-1');
1511
+ expect(task.status).toBe('disputed');
1512
+ });
1513
+ it('sets dispute_id on the task', () => {
1514
+ store.updateTaskDisputeStatus('task-1', 'convoy-1', 'disputed', 'dispute-task-1-123');
1515
+ const task = store.getTask('task-1', 'convoy-1');
1516
+ expect(task.dispute_id).toBe('dispute-task-1-123');
1517
+ });
1518
+ it('is idempotent when called twice with same dispute_id', () => {
1519
+ store.updateTaskDisputeStatus('task-1', 'convoy-1', 'disputed', 'dispute-task-1-abc');
1520
+ store.updateTaskDisputeStatus('task-1', 'convoy-1', 'disputed', 'dispute-task-1-abc');
1521
+ const task = store.getTask('task-1', 'convoy-1');
1522
+ expect(task.status).toBe('disputed');
1523
+ expect(task.dispute_id).toBe('dispute-task-1-abc');
1524
+ });
1525
+ });
1526
+ // ── Artifact CRUD ──────────────────────────────────────────────────────────────
1527
+ describe('artifact CRUD', () => {
1528
+ it('inserts and retrieves an artifact', () => {
1529
+ store.insertConvoy(makeConvoy());
1530
+ store.insertTask(makeTask());
1531
+ store.insertArtifact({
1532
+ id: 'art-1',
1533
+ convoy_id: 'convoy-1',
1534
+ task_id: 'task-1',
1535
+ name: 'migration-sql',
1536
+ type: 'file',
1537
+ content: 'CREATE TABLE foo (id INT);',
1538
+ created_at: new Date().toISOString(),
1539
+ });
1540
+ const art = store.getArtifact('convoy-1', 'migration-sql');
1541
+ expect(art).toBeDefined();
1542
+ expect(art.name).toBe('migration-sql');
1543
+ expect(art.content).toBe('CREATE TABLE foo (id INT);');
1544
+ });
1545
+ it('enforces unique artifact name per convoy', () => {
1546
+ store.insertConvoy(makeConvoy());
1547
+ store.insertTask(makeTask());
1548
+ store.insertArtifact({
1549
+ id: 'art-1',
1550
+ convoy_id: 'convoy-1',
1551
+ task_id: 'task-1',
1552
+ name: 'dup-name',
1553
+ type: 'file',
1554
+ content: 'first',
1555
+ created_at: new Date().toISOString(),
1556
+ });
1557
+ expect(() => store.insertArtifact({
1558
+ id: 'art-2',
1559
+ convoy_id: 'convoy-1',
1560
+ task_id: 'task-1',
1561
+ name: 'dup-name',
1562
+ type: 'file',
1563
+ content: 'second',
1564
+ created_at: new Date().toISOString(),
1565
+ })).toThrow();
1566
+ });
1567
+ it('enforces max 50 artifacts per convoy', () => {
1568
+ store.insertConvoy(makeConvoy());
1569
+ store.insertTask(makeTask());
1570
+ for (let i = 0; i < 50; i++) {
1571
+ store.insertArtifact({
1572
+ id: `art-${i}`,
1573
+ convoy_id: 'convoy-1',
1574
+ task_id: 'task-1',
1575
+ name: `artifact-${i}`,
1576
+ type: 'summary',
1577
+ content: `content-${i}`,
1578
+ created_at: new Date().toISOString(),
1579
+ });
1580
+ }
1581
+ expect(() => store.insertArtifact({
1582
+ id: 'art-51',
1583
+ convoy_id: 'convoy-1',
1584
+ task_id: 'task-1',
1585
+ name: 'artifact-50',
1586
+ type: 'summary',
1587
+ content: 'over limit',
1588
+ created_at: new Date().toISOString(),
1589
+ })).toThrow(/maximum of 50 artifacts/);
1590
+ });
1591
+ it('retrieves artifacts by task', () => {
1592
+ store.insertConvoy(makeConvoy());
1593
+ store.insertTask(makeTask());
1594
+ store.insertArtifact({
1595
+ id: 'art-1',
1596
+ convoy_id: 'convoy-1',
1597
+ task_id: 'task-1',
1598
+ name: 'a',
1599
+ type: 'file',
1600
+ content: 'file content',
1601
+ created_at: new Date().toISOString(),
1602
+ });
1603
+ const arts = store.getArtifactsByTask('task-1');
1604
+ expect(arts).toHaveLength(1);
1605
+ });
1606
+ it('deletes artifacts older than N days', () => {
1607
+ store.insertConvoy(makeConvoy());
1608
+ store.insertTask(makeTask());
1609
+ store.insertArtifact({
1610
+ id: 'art-old',
1611
+ convoy_id: 'convoy-1',
1612
+ task_id: 'task-1',
1613
+ name: 'old-artifact',
1614
+ type: 'summary',
1615
+ content: 'old',
1616
+ created_at: new Date().toISOString(),
1617
+ });
1618
+ // Mark convoy as done with a finished_at in the past
1619
+ store.updateConvoyStatus('convoy-1', 'done', {
1620
+ finished_at: new Date(Date.now() - 60 * 24 * 60 * 60 * 1000).toISOString(),
1621
+ });
1622
+ const deleted = store.deleteArtifactsOlderThan(30);
1623
+ expect(deleted).toBe(1);
1624
+ });
1625
+ });
1626
+ // ── migration full chain v4→v9 ────────────────────────────────────────────────
1627
+ describe('migration full chain v4→v9', () => {
1628
+ it('migrates a seeded v4 database to v9, preserving data and adding all tables/columns', () => {
1629
+ const chainDbPath = join(tmpDir, 'v4-chain.db');
1630
+ const v4Db = createV4Db(chainDbPath);
1631
+ // Seed realistic v4 data
1632
+ v4Db.prepare(`INSERT INTO convoy (id, name, spec_hash, status, branch, created_at, spec_yaml)
1633
+ VALUES ('convoy-chain', 'Chain Test', 'hash-chain', 'pending', NULL, '2026-01-01T00:00:00.000Z', 'name: chain')`).run();
1634
+ v4Db.prepare(`INSERT INTO task (id, convoy_id, phase, prompt, agent, timeout_ms, status, retries, max_retries)
1635
+ VALUES ('task-chain', 'convoy-chain', 0, 'Do chain work', 'developer', 1800000, 'pending', 0, 1)`).run();
1636
+ v4Db.prepare(`INSERT INTO worker (id, task_id, adapter, status, created_at)
1637
+ VALUES ('worker-chain', 'task-chain', 'vscode', 'spawned', '2026-01-01T00:00:00.000Z')`).run();
1638
+ v4Db.prepare(`INSERT INTO event (convoy_id, task_id, type, created_at)
1639
+ VALUES ('convoy-chain', 'task-chain', 'task_started', '2026-01-01T00:00:00.000Z')`).run();
1640
+ v4Db.close();
1641
+ // Trigger the full v4→v9 migration chain
1642
+ const migratedStore = createConvoyStore(chainDbPath);
1643
+ migratedStore.close();
1644
+ const verifyDb = new DatabaseSync(chainDbPath);
1645
+ // Verify user_version = 9
1646
+ const version = verifyDb.prepare('PRAGMA user_version').get().user_version;
1647
+ expect(version).toBe(9);
1648
+ // Verify all new tables exist
1649
+ const tables = verifyDb.prepare("SELECT name FROM sqlite_master WHERE type='table'").all().map(t => t.name);
1650
+ for (const table of ['task_step', 'dlq', 'artifact', 'agent_identity', 'scratchpad']) {
1651
+ expect(tables).toContain(table);
1652
+ }
1653
+ // Verify all new columns on task
1654
+ const taskCols = verifyDb.prepare('PRAGMA table_info(task)').all().map(c => c.name);
1655
+ for (const col of [
1656
+ 'gates', 'on_exhausted', 'injected', 'provenance', 'idempotency_key',
1657
+ 'current_step', 'total_steps', 'review_level', 'review_verdict', 'review_tokens',
1658
+ 'review_model', 'panel_attempts', 'dispute_id', 'drift_score', 'drift_retried',
1659
+ 'outputs', 'inputs', 'discovered_issues',
1660
+ ]) {
1661
+ expect(taskCols).toContain(col);
1662
+ }
1663
+ // Verify all new columns on convoy
1664
+ const convoyCols = verifyDb.prepare('PRAGMA table_info(convoy)').all().map(c => c.name);
1665
+ for (const col of ['circuit_state', 'review_tokens_total', 'review_budget']) {
1666
+ expect(convoyCols).toContain(col);
1667
+ }
1668
+ // Verify seed data is intact
1669
+ const convoy = verifyDb.prepare('SELECT id FROM convoy WHERE id = :id').get({ id: 'convoy-chain' });
1670
+ expect(convoy?.id).toBe('convoy-chain');
1671
+ const task = verifyDb.prepare('SELECT id FROM task WHERE id = :id').get({ id: 'task-chain' });
1672
+ expect(task?.id).toBe('task-chain');
1673
+ const worker = verifyDb.prepare('SELECT id FROM worker WHERE id = :id').get({ id: 'worker-chain' });
1674
+ expect(worker?.id).toBe('worker-chain');
1675
+ const eventCount = verifyDb.prepare('SELECT COUNT(*) AS cnt FROM event WHERE convoy_id = :id').get({ id: 'convoy-chain' }).cnt;
1676
+ expect(eventCount).toBe(1);
1677
+ // Verify FK constraints work: insert a task_step referencing the seeded task_id
1678
+ expect(() => {
1679
+ verifyDb.prepare(`INSERT INTO task_step (task_id, step_index, prompt, gates, status)
1680
+ VALUES ('task-chain', 0, 'Step prompt', NULL, 'pending')`).run();
1681
+ }).not.toThrow();
1682
+ verifyDb.close();
1683
+ });
1684
+ });
1685
+ // ── Agent Identity ────────────────────────────────────────────────────────────
1686
+ describe('agent identity', () => {
1687
+ it('inserts an agent identity record without error', () => {
1688
+ store.insertConvoy(makeConvoy());
1689
+ store.insertTask(makeTask());
1690
+ expect(() => store.insertAgentIdentity({
1691
+ id: 'ai-1',
1692
+ agent: 'developer',
1693
+ convoy_id: 'convoy-1',
1694
+ task_id: 'task-1',
1695
+ summary: 'Implemented the feature successfully',
1696
+ created_at: new Date().toISOString(),
1697
+ retention_days: 90,
1698
+ })).not.toThrow();
1699
+ });
1700
+ });
1701
+ describe('agent identity persistence', () => {
1702
+ it('deleteAgentIdentitiesOlderThan removes expired identities', () => {
1703
+ store.insertConvoy(makeConvoy());
1704
+ store.insertTask(makeTask());
1705
+ const oldCreatedAt = new Date(Date.now() - 100 * 24 * 60 * 60 * 1000).toISOString();
1706
+ const recentCreatedAt = new Date(Date.now() - 10 * 24 * 60 * 60 * 1000).toISOString();
1707
+ store.insertAgentIdentity({
1708
+ id: 'ai-old',
1709
+ agent: 'Developer',
1710
+ convoy_id: 'convoy-1',
1711
+ task_id: 'task-1',
1712
+ summary: 'old identity',
1713
+ created_at: oldCreatedAt,
1714
+ retention_days: 90,
1715
+ });
1716
+ store.insertAgentIdentity({
1717
+ id: 'ai-recent',
1718
+ agent: 'Developer',
1719
+ convoy_id: 'convoy-1',
1720
+ task_id: 'task-1',
1721
+ summary: 'recent identity',
1722
+ created_at: recentCreatedAt,
1723
+ retention_days: 90,
1724
+ });
1725
+ const deleted = store.deleteAgentIdentitiesOlderThan(90);
1726
+ expect(deleted).toBe(1);
1727
+ const identities = store.getAgentIdentities('Developer', 10);
1728
+ expect(identities).toHaveLength(1);
1729
+ expect(identities[0].id).toBe('ai-recent');
1730
+ });
1731
+ it('deleteAgentIdentitiesOlderThan respects per-record retention_days', () => {
1732
+ store.insertConvoy(makeConvoy());
1733
+ store.insertTask(makeTask());
1734
+ const createdAt35DaysAgo = new Date(Date.now() - 35 * 24 * 60 * 60 * 1000).toISOString();
1735
+ store.insertAgentIdentity({
1736
+ id: 'ai-30d',
1737
+ agent: 'Developer',
1738
+ convoy_id: 'convoy-1',
1739
+ task_id: 'task-1',
1740
+ summary: 'expires early',
1741
+ created_at: createdAt35DaysAgo,
1742
+ retention_days: 30,
1743
+ });
1744
+ store.insertAgentIdentity({
1745
+ id: 'ai-180d',
1746
+ agent: 'Developer',
1747
+ convoy_id: 'convoy-1',
1748
+ task_id: 'task-1',
1749
+ summary: 'kept longer',
1750
+ created_at: createdAt35DaysAgo,
1751
+ retention_days: 180,
1752
+ });
1753
+ const deleted = store.deleteAgentIdentitiesOlderThan(90);
1754
+ expect(deleted).toBe(1);
1755
+ const identities = store.getAgentIdentities('Developer', 10);
1756
+ const ids = identities.map((i) => i.id);
1757
+ expect(ids).not.toContain('ai-30d');
1758
+ expect(ids).toContain('ai-180d');
1759
+ });
1760
+ it('stores summaries up to 4096 characters', () => {
1761
+ store.insertConvoy(makeConvoy());
1762
+ store.insertTask(makeTask());
1763
+ const summary = 'x'.repeat(4096);
1764
+ store.insertAgentIdentity({
1765
+ id: 'ai-4kb',
1766
+ agent: 'Developer',
1767
+ convoy_id: 'convoy-1',
1768
+ task_id: 'task-1',
1769
+ summary,
1770
+ created_at: new Date().toISOString(),
1771
+ retention_days: 90,
1772
+ });
1773
+ const identities = store.getAgentIdentities('Developer', 10);
1774
+ expect(identities).toHaveLength(1);
1775
+ expect(identities[0].summary).toBe(summary);
1776
+ expect(identities[0].summary.length).toBe(4096);
1777
+ });
1778
+ it('listAgentIdentitySummary returns counts per agent', () => {
1779
+ store.insertConvoy(makeConvoy());
1780
+ store.insertTask(makeTask());
1781
+ for (let i = 0; i < 3; i++) {
1782
+ store.insertAgentIdentity({
1783
+ id: `ai-dev-${i}`,
1784
+ agent: 'Developer',
1785
+ convoy_id: 'convoy-1',
1786
+ task_id: 'task-1',
1787
+ summary: `dev-${i}`,
1788
+ created_at: new Date(Date.now() + i).toISOString(),
1789
+ retention_days: 90,
1790
+ });
1791
+ }
1792
+ for (let i = 0; i < 2; i++) {
1793
+ store.insertAgentIdentity({
1794
+ id: `ai-rev-${i}`,
1795
+ agent: 'Reviewer',
1796
+ convoy_id: 'convoy-1',
1797
+ task_id: 'task-1',
1798
+ summary: `review-${i}`,
1799
+ created_at: new Date(Date.now() + 10 + i).toISOString(),
1800
+ retention_days: 90,
1801
+ });
1802
+ }
1803
+ const summary = store.listAgentIdentitySummary();
1804
+ const byAgent = Object.fromEntries(summary.map((row) => [row.agent, row]));
1805
+ expect(byAgent.Developer).toBeDefined();
1806
+ expect(byAgent.Developer.task_count).toBe(3);
1807
+ expect(byAgent.Reviewer).toBeDefined();
1808
+ expect(byAgent.Reviewer.task_count).toBe(2);
1809
+ });
1810
+ it('purgeAgentIdentities removes all identities for a specific agent', () => {
1811
+ store.insertConvoy(makeConvoy());
1812
+ store.insertTask(makeTask());
1813
+ store.insertAgentIdentity({
1814
+ id: 'ai-dev-1',
1815
+ agent: 'Developer',
1816
+ convoy_id: 'convoy-1',
1817
+ task_id: 'task-1',
1818
+ summary: 'dev one',
1819
+ created_at: new Date().toISOString(),
1820
+ retention_days: 90,
1821
+ });
1822
+ store.insertAgentIdentity({
1823
+ id: 'ai-dev-2',
1824
+ agent: 'Developer',
1825
+ convoy_id: 'convoy-1',
1826
+ task_id: 'task-1',
1827
+ summary: 'dev two',
1828
+ created_at: new Date().toISOString(),
1829
+ retention_days: 90,
1830
+ });
1831
+ store.insertAgentIdentity({
1832
+ id: 'ai-rev-1',
1833
+ agent: 'Reviewer',
1834
+ convoy_id: 'convoy-1',
1835
+ task_id: 'task-1',
1836
+ summary: 'review one',
1837
+ created_at: new Date().toISOString(),
1838
+ retention_days: 90,
1839
+ });
1840
+ const deleted = store.purgeAgentIdentities('Developer');
1841
+ expect(deleted).toBe(2);
1842
+ expect(store.getAgentIdentities('Developer', 10)).toHaveLength(0);
1843
+ expect(store.getAgentIdentities('Reviewer', 10)).toHaveLength(1);
1844
+ });
1845
+ it('truncates summaries longer than 4096 characters to exactly 4096 chars', () => {
1846
+ store.insertConvoy(makeConvoy());
1847
+ store.insertTask(makeTask());
1848
+ const longSummary = 'a'.repeat(5000);
1849
+ store.insertAgentIdentity({
1850
+ id: 'ai-trunc',
1851
+ agent: 'Developer',
1852
+ convoy_id: 'convoy-1',
1853
+ task_id: 'task-1',
1854
+ summary: longSummary,
1855
+ created_at: new Date().toISOString(),
1856
+ retention_days: 90,
1857
+ });
1858
+ const identities = store.getAgentIdentities('Developer', 10);
1859
+ const stored = identities.find(i => i.id === 'ai-trunc');
1860
+ expect(stored).toBeDefined();
1861
+ expect(stored.summary.length).toBe(4096);
1862
+ });
1863
+ });
1864
+ // ── Schema v7→v8 migration ──────────────────────────────────────────────────
1865
+ function createV7Db(dbPath) {
1866
+ // v7 = v6 schema + drift_score/drift_retried columns on task (before outputs/inputs/discovered_issues)
1867
+ const db = new DatabaseSync(dbPath);
1868
+ db.exec(`
1869
+ CREATE TABLE convoy (
1870
+ id TEXT PRIMARY KEY, name TEXT NOT NULL, spec_hash TEXT NOT NULL,
1871
+ status TEXT NOT NULL DEFAULT 'pending', branch TEXT, created_at TEXT NOT NULL,
1872
+ started_at TEXT, finished_at TEXT, spec_yaml TEXT NOT NULL,
1873
+ total_tokens INTEGER, total_cost_usd TEXT, pipeline_id TEXT,
1874
+ circuit_state TEXT, review_tokens_total INTEGER, review_budget INTEGER
1875
+ );
1876
+ CREATE TABLE task (
1877
+ id TEXT PRIMARY KEY, convoy_id TEXT NOT NULL REFERENCES convoy(id),
1878
+ phase INTEGER NOT NULL, prompt TEXT NOT NULL,
1879
+ agent TEXT NOT NULL DEFAULT 'developer', adapter TEXT,
1880
+ model TEXT, timeout_ms INTEGER NOT NULL DEFAULT 1800000,
1881
+ status TEXT NOT NULL DEFAULT 'pending', worker_id TEXT, worktree TEXT,
1882
+ output TEXT, exit_code INTEGER, started_at TEXT, finished_at TEXT,
1883
+ retries INTEGER NOT NULL DEFAULT 0, max_retries INTEGER NOT NULL DEFAULT 1,
1884
+ files TEXT, depends_on TEXT, prompt_tokens INTEGER, completion_tokens INTEGER,
1885
+ total_tokens INTEGER, cost_usd TEXT, gates TEXT,
1886
+ on_exhausted TEXT NOT NULL DEFAULT 'dlq', injected INTEGER NOT NULL DEFAULT 0,
1887
+ provenance TEXT, idempotency_key TEXT, current_step INTEGER, total_steps INTEGER,
1888
+ review_level TEXT, review_verdict TEXT, review_tokens INTEGER, review_model TEXT,
1889
+ panel_attempts INTEGER NOT NULL DEFAULT 0, dispute_id TEXT,
1890
+ drift_score REAL, drift_retried INTEGER NOT NULL DEFAULT 0
1891
+ );
1892
+ PRAGMA user_version = 7;
1893
+ `);
1894
+ return db;
1895
+ }
1896
+ describe('v7→v8 migration', () => {
1897
+ it('creates artifact and agent_identity tables idempotently', () => {
1898
+ const migDir = realpathSync(mkdtempSync(join(tmpdir(), 'mig-test-')));
1899
+ const migDb = join(migDir, 'mig.db');
1900
+ const db = new DatabaseSync(migDb);
1901
+ db.exec('PRAGMA journal_mode = WAL');
1902
+ db.exec(`
1903
+ CREATE TABLE convoy (
1904
+ id TEXT PRIMARY KEY,
1905
+ name TEXT NOT NULL,
1906
+ spec_hash TEXT NOT NULL,
1907
+ status TEXT NOT NULL DEFAULT 'pending',
1908
+ branch TEXT,
1909
+ created_at TEXT NOT NULL,
1910
+ started_at TEXT,
1911
+ finished_at TEXT,
1912
+ spec_yaml TEXT NOT NULL,
1913
+ total_tokens INTEGER,
1914
+ total_cost_usd TEXT,
1915
+ pipeline_id TEXT,
1916
+ circuit_state TEXT,
1917
+ review_tokens_total INTEGER,
1918
+ review_budget INTEGER
1919
+ );
1920
+ CREATE TABLE task (
1921
+ id TEXT PRIMARY KEY,
1922
+ convoy_id TEXT NOT NULL REFERENCES convoy(id),
1923
+ phase INTEGER NOT NULL,
1924
+ prompt TEXT NOT NULL,
1925
+ agent TEXT NOT NULL DEFAULT 'developer',
1926
+ adapter TEXT,
1927
+ model TEXT,
1928
+ timeout_ms INTEGER NOT NULL DEFAULT 1800000,
1929
+ status TEXT NOT NULL DEFAULT 'pending',
1930
+ worker_id TEXT,
1931
+ worktree TEXT,
1932
+ output TEXT,
1933
+ exit_code INTEGER,
1934
+ started_at TEXT,
1935
+ finished_at TEXT,
1936
+ retries INTEGER NOT NULL DEFAULT 0,
1937
+ max_retries INTEGER NOT NULL DEFAULT 1,
1938
+ files TEXT,
1939
+ depends_on TEXT,
1940
+ prompt_tokens INTEGER,
1941
+ completion_tokens INTEGER,
1942
+ total_tokens INTEGER,
1943
+ cost_usd TEXT,
1944
+ gates TEXT,
1945
+ on_exhausted TEXT NOT NULL DEFAULT 'dlq',
1946
+ injected INTEGER NOT NULL DEFAULT 0,
1947
+ provenance TEXT,
1948
+ idempotency_key TEXT,
1949
+ current_step INTEGER,
1950
+ total_steps INTEGER,
1951
+ review_level TEXT,
1952
+ review_verdict TEXT,
1953
+ review_tokens INTEGER,
1954
+ review_model TEXT,
1955
+ panel_attempts INTEGER NOT NULL DEFAULT 0,
1956
+ dispute_id TEXT,
1957
+ drift_score REAL DEFAULT NULL,
1958
+ drift_retried INTEGER NOT NULL DEFAULT 0
1959
+ );
1960
+ PRAGMA user_version = 7;
1961
+ `);
1962
+ migrateSchema(db, migDb, 7, 8);
1963
+ // Verify artifact table exists
1964
+ const artInfo = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='artifact'").get();
1965
+ expect(artInfo).toBeDefined();
1966
+ // Verify agent_identity table exists
1967
+ const aiInfo = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='agent_identity'").get();
1968
+ expect(aiInfo).toBeDefined();
1969
+ // Verify new task columns exist
1970
+ const taskCols = db.prepare('PRAGMA table_info(task)').all();
1971
+ const colNames = taskCols.map(c => c.name);
1972
+ expect(colNames).toContain('outputs');
1973
+ expect(colNames).toContain('inputs');
1974
+ expect(colNames).toContain('discovered_issues');
1975
+ const artCols = db.prepare('PRAGMA table_info(artifact)').all();
1976
+ const idCol = artCols.find(c => c.name === 'id');
1977
+ expect(idCol).toBeDefined();
1978
+ expect(idCol.type.toUpperCase()).toBe('TEXT');
1979
+ expect(idCol.pk).toBe(1);
1980
+ // Verify version bumped to 8
1981
+ const version = db.prepare('PRAGMA user_version').get().user_version;
1982
+ expect(version).toBe(8);
1983
+ db.close();
1984
+ rmSync(migDir, { recursive: true, force: true });
1985
+ });
1986
+ it('failure mode: rolls back on error, version stays at 7 and backup exists', () => {
1987
+ const v7DbPath = join(tmpDir, 'v7-fail.db');
1988
+ const db = createV7Db(v7DbPath);
1989
+ // Pre-add outputs column to cause duplicate column error on migration
1990
+ db.exec('ALTER TABLE task ADD COLUMN outputs TEXT');
1991
+ expect(() => migrateSchema(db, v7DbPath, 7, 8)).toThrow();
1992
+ const row = db.prepare('PRAGMA user_version').get();
1993
+ expect(row.user_version).toBe(7);
1994
+ expect(existsSync(`${v7DbPath}.v7.bak`)).toBe(true);
1995
+ db.close();
1996
+ });
1997
+ });
1998
+ describe('v8→v9 migration', () => {
1999
+ it('creates scratchpad table', () => {
2000
+ const migDir = realpathSync(mkdtempSync(join(tmpdir(), 'mig-v9-test-')));
2001
+ const migDb = join(migDir, 'mig.db');
2002
+ const db = new DatabaseSync(migDb);
2003
+ db.exec('PRAGMA journal_mode = WAL');
2004
+ db.exec(`
2005
+ CREATE TABLE convoy (
2006
+ id TEXT PRIMARY KEY,
2007
+ name TEXT NOT NULL,
2008
+ spec_hash TEXT NOT NULL,
2009
+ status TEXT NOT NULL DEFAULT 'pending',
2010
+ branch TEXT,
2011
+ created_at TEXT NOT NULL,
2012
+ started_at TEXT,
2013
+ finished_at TEXT,
2014
+ spec_yaml TEXT NOT NULL,
2015
+ total_tokens INTEGER,
2016
+ total_cost_usd TEXT,
2017
+ pipeline_id TEXT,
2018
+ circuit_state TEXT,
2019
+ review_tokens_total INTEGER,
2020
+ review_budget INTEGER
2021
+ );
2022
+ CREATE TABLE task (
2023
+ id TEXT PRIMARY KEY,
2024
+ convoy_id TEXT NOT NULL REFERENCES convoy(id),
2025
+ phase INTEGER NOT NULL,
2026
+ prompt TEXT NOT NULL,
2027
+ agent TEXT NOT NULL DEFAULT 'developer',
2028
+ adapter TEXT,
2029
+ model TEXT,
2030
+ timeout_ms INTEGER NOT NULL DEFAULT 1800000,
2031
+ status TEXT NOT NULL DEFAULT 'pending',
2032
+ worker_id TEXT,
2033
+ worktree TEXT,
2034
+ output TEXT,
2035
+ exit_code INTEGER,
2036
+ started_at TEXT,
2037
+ finished_at TEXT,
2038
+ retries INTEGER NOT NULL DEFAULT 0,
2039
+ max_retries INTEGER NOT NULL DEFAULT 1,
2040
+ files TEXT,
2041
+ depends_on TEXT,
2042
+ prompt_tokens INTEGER,
2043
+ completion_tokens INTEGER,
2044
+ total_tokens INTEGER,
2045
+ cost_usd TEXT,
2046
+ gates TEXT,
2047
+ on_exhausted TEXT NOT NULL DEFAULT 'dlq',
2048
+ injected INTEGER NOT NULL DEFAULT 0,
2049
+ provenance TEXT,
2050
+ idempotency_key TEXT,
2051
+ current_step INTEGER,
2052
+ total_steps INTEGER,
2053
+ review_level TEXT,
2054
+ review_verdict TEXT,
2055
+ review_tokens INTEGER,
2056
+ review_model TEXT,
2057
+ panel_attempts INTEGER NOT NULL DEFAULT 0,
2058
+ dispute_id TEXT,
2059
+ drift_score REAL,
2060
+ drift_retried INTEGER NOT NULL DEFAULT 0,
2061
+ outputs TEXT,
2062
+ inputs TEXT,
2063
+ discovered_issues TEXT
2064
+ );
2065
+ CREATE TABLE worker (
2066
+ id TEXT PRIMARY KEY,
2067
+ task_id TEXT REFERENCES task(id),
2068
+ adapter TEXT NOT NULL,
2069
+ pid INTEGER,
2070
+ session_id TEXT,
2071
+ status TEXT NOT NULL DEFAULT 'spawned',
2072
+ worktree TEXT,
2073
+ created_at TEXT NOT NULL,
2074
+ finished_at TEXT,
2075
+ last_heartbeat TEXT
2076
+ );
2077
+ CREATE TABLE event (
2078
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
2079
+ convoy_id TEXT REFERENCES convoy(id),
2080
+ task_id TEXT,
2081
+ worker_id TEXT,
2082
+ type TEXT NOT NULL,
2083
+ data TEXT,
2084
+ created_at TEXT NOT NULL
2085
+ );
2086
+ CREATE TABLE dlq (
2087
+ id TEXT PRIMARY KEY,
2088
+ convoy_id TEXT NOT NULL REFERENCES convoy(id),
2089
+ task_id TEXT NOT NULL REFERENCES task(id),
2090
+ agent TEXT NOT NULL,
2091
+ failure_type TEXT NOT NULL,
2092
+ error_output TEXT,
2093
+ attempts INTEGER NOT NULL,
2094
+ tokens_spent INTEGER,
2095
+ escalation_task_id TEXT,
2096
+ resolved INTEGER NOT NULL DEFAULT 0,
2097
+ resolution TEXT,
2098
+ created_at TEXT NOT NULL,
2099
+ resolved_at TEXT
2100
+ );
2101
+ CREATE TABLE task_step (
2102
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
2103
+ task_id TEXT NOT NULL REFERENCES task(id),
2104
+ step_index INTEGER NOT NULL,
2105
+ prompt TEXT NOT NULL,
2106
+ gates TEXT,
2107
+ status TEXT NOT NULL DEFAULT 'pending',
2108
+ exit_code INTEGER,
2109
+ output TEXT,
2110
+ started_at TEXT,
2111
+ finished_at TEXT
2112
+ );
2113
+ CREATE TABLE artifact (
2114
+ id TEXT PRIMARY KEY,
2115
+ convoy_id TEXT NOT NULL REFERENCES convoy(id),
2116
+ task_id TEXT NOT NULL REFERENCES task(id),
2117
+ name TEXT NOT NULL,
2118
+ type TEXT NOT NULL,
2119
+ content TEXT NOT NULL,
2120
+ created_at TEXT NOT NULL,
2121
+ UNIQUE(convoy_id, name)
2122
+ );
2123
+ CREATE TABLE agent_identity (
2124
+ id TEXT PRIMARY KEY,
2125
+ agent TEXT NOT NULL,
2126
+ convoy_id TEXT NOT NULL,
2127
+ task_id TEXT NOT NULL,
2128
+ summary TEXT NOT NULL,
2129
+ created_at TEXT NOT NULL,
2130
+ retention_days INTEGER NOT NULL DEFAULT 90
2131
+ );
2132
+ PRAGMA user_version = 8;
2133
+ `);
2134
+ migrateSchema(db, migDb, 8, 9);
2135
+ const scratchpadInfo = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='scratchpad'").get();
2136
+ expect(scratchpadInfo).toBeDefined();
2137
+ const scratchpadCols = db.prepare('PRAGMA table_info(scratchpad)').all();
2138
+ const colNames = scratchpadCols.map(c => c.name);
2139
+ expect(colNames).toContain('key');
2140
+ expect(colNames).toContain('value');
2141
+ expect(colNames).toContain('updated_at');
2142
+ const version = db.prepare('PRAGMA user_version').get().user_version;
2143
+ expect(version).toBe(9);
2144
+ db.close();
2145
+ rmSync(migDir, { recursive: true, force: true });
2146
+ });
2147
+ });
2148
+ describe('size limit enforcement', () => {
2149
+ it('insertConvoy rejects spec_yaml exceeding 256KB', () => {
2150
+ const bigSpecYaml = 'x'.repeat(256 * 1024 + 1);
2151
+ expect(() => store.insertConvoy(makeConvoy({ spec_yaml: bigSpecYaml })))
2152
+ .toThrow(FieldSizeLimitError);
2153
+ });
2154
+ it('insertConvoy accepts spec_yaml at exactly 256KB', () => {
2155
+ const exactSpecYaml = 'x'.repeat(256 * 1024);
2156
+ expect(() => store.insertConvoy(makeConvoy({ id: 'convoy-exact', spec_yaml: exactSpecYaml }))).not.toThrow();
2157
+ });
2158
+ it('insertEvent rejects data exceeding 64KB', () => {
2159
+ store.insertConvoy(makeConvoy());
2160
+ const bigData = 'y'.repeat(64 * 1024 + 1);
2161
+ expect(() => store.insertEvent({
2162
+ convoy_id: 'convoy-1',
2163
+ task_id: null,
2164
+ worker_id: null,
2165
+ type: 'test',
2166
+ data: bigData,
2167
+ created_at: new Date().toISOString(),
2168
+ })).toThrow(FieldSizeLimitError);
2169
+ });
2170
+ it('updateTaskStatus truncates output exceeding 1MB', () => {
2171
+ store.insertConvoy(makeConvoy());
2172
+ store.insertTask(makeTask());
2173
+ const bigOutput = 'z'.repeat(3 * 1024 * 1024); // 3 MB — well over the 1 MB limit
2174
+ store.updateTaskStatus('task-1', 'convoy-1', 'done', { output: bigOutput });
2175
+ const task = store.getTask('task-1', 'convoy-1');
2176
+ expect(task?.output).toBeDefined();
2177
+ expect(task.output.length).toBeLessThan(bigOutput.length);
2178
+ expect(task.output).toContain('[truncated:');
2179
+ });
2180
+ it('insertAgentIdentity truncates summary exceeding 4KB', () => {
2181
+ store.insertConvoy(makeConvoy());
2182
+ store.insertTask(makeTask());
2183
+ const bigSummary = 's'.repeat(4097);
2184
+ store.insertAgentIdentity({
2185
+ id: 'identity-1',
2186
+ agent: 'developer',
2187
+ convoy_id: 'convoy-1',
2188
+ task_id: 'task-1',
2189
+ summary: bigSummary,
2190
+ created_at: new Date().toISOString(),
2191
+ retention_days: 90,
2192
+ });
2193
+ const identities = store.getAgentIdentities('developer', 10);
2194
+ expect(identities[0].summary.length).toBeLessThanOrEqual(4096);
2195
+ });
2196
+ it('insertPipeline rejects spec_yaml exceeding 256KB', () => {
2197
+ const bigSpecYaml = 'p'.repeat(256 * 1024 + 1);
2198
+ expect(() => store.insertPipeline({
2199
+ id: 'pipeline-1',
2200
+ name: 'Test Pipeline',
2201
+ status: 'pending',
2202
+ branch: null,
2203
+ spec_yaml: bigSpecYaml,
2204
+ convoy_specs: '[]',
2205
+ created_at: new Date().toISOString(),
2206
+ })).toThrow(FieldSizeLimitError);
2207
+ });
2208
+ });
876
2209
  //# sourceMappingURL=store.test.js.map