@deksden-com/dd-flow-cli 0.9.0-beta.11 → 0.9.0-beta.111

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 (162) hide show
  1. package/CHANGELOG.md +526 -0
  2. package/README.md +68 -0
  3. package/dist/build-info.json +8 -8
  4. package/dist/cli/command-inputs.js +305 -0
  5. package/dist/cli/help.js +89 -13
  6. package/dist/cli/hook-ingress.js +77 -0
  7. package/dist/cli/input-preparation.js +116 -0
  8. package/dist/cli/native-hook-ingress.js +48 -0
  9. package/dist/cli/run-cli.js +1465 -384
  10. package/dist/cli.js +8 -2
  11. package/dist/harness-runtime/DD-ZCODE.md +99 -0
  12. package/dist/harness-runtime/bin/dd-agy.mjs +52 -0
  13. package/dist/harness-runtime/bin/dd-codex.mjs +26 -0
  14. package/dist/harness-runtime/bin/dd-droid.mjs +33 -0
  15. package/dist/harness-runtime/bin/dd-grok.mjs +30 -0
  16. package/dist/harness-runtime/bin/dd-opencode.mjs +21 -0
  17. package/dist/harness-runtime/bin/dd-zcode.mjs +83 -0
  18. package/dist/harness-runtime/lib/agy-hooks.d.mts +1 -0
  19. package/dist/harness-runtime/lib/agy-hooks.mjs +9 -0
  20. package/dist/harness-runtime/lib/daemon-operations.mjs +226 -0
  21. package/dist/harness-runtime/lib/dd-agy-daemon.d.mts +6 -0
  22. package/dist/harness-runtime/lib/dd-agy-daemon.mjs +500 -0
  23. package/dist/harness-runtime/lib/dd-agy.mjs +59 -0
  24. package/dist/harness-runtime/lib/dd-codex-daemon.d.mts +1 -0
  25. package/dist/harness-runtime/lib/dd-codex-daemon.mjs +149 -0
  26. package/dist/harness-runtime/lib/dd-codex.mjs +563 -0
  27. package/dist/harness-runtime/lib/dd-droid-daemon.mjs +125 -0
  28. package/dist/harness-runtime/lib/dd-droid.mjs +475 -0
  29. package/dist/harness-runtime/lib/dd-grok-daemon.mjs +394 -0
  30. package/dist/harness-runtime/lib/dd-grok.mjs +174 -0
  31. package/dist/harness-runtime/lib/dd-opencode-daemon.mjs +219 -0
  32. package/dist/harness-runtime/lib/dd-opencode.mjs +98 -0
  33. package/dist/harness-runtime/lib/dd-zcode-daemon.mjs +645 -0
  34. package/dist/harness-runtime/lib/dd-zcode.mjs +982 -0
  35. package/dist/harness-runtime/lib/delegation-instructions.d.mts +13 -0
  36. package/dist/harness-runtime/lib/delegation-instructions.mjs +144 -0
  37. package/dist/harness-runtime/lib/dispatch-fence.mjs +14 -0
  38. package/dist/harness-runtime/lib/driver-recovery.mjs +129 -0
  39. package/dist/harness-runtime/lib/droid-observation.mjs +66 -0
  40. package/dist/harness-runtime/lib/managed-daemon.mjs +327 -0
  41. package/dist/harness-runtime/lib/model-observations.mjs +77 -0
  42. package/dist/harness-runtime/lib/native-hook-command.d.mts +3 -0
  43. package/dist/harness-runtime/lib/native-hook-command.mjs +108 -0
  44. package/dist/harness-runtime/lib/observation-clock.mjs +36 -0
  45. package/dist/harness-runtime/lib/operation-context.mjs +5 -0
  46. package/dist/harness-runtime/lib/operation-errors.mjs +19 -0
  47. package/dist/harness-runtime/lib/process-json.mjs +71 -0
  48. package/dist/harness-runtime/lib/process-snapshot.mjs +10 -0
  49. package/dist/harness-runtime/lib/runner-events.mjs +200 -0
  50. package/dist/harness-runtime/lib/runner-lock.mjs +42 -0
  51. package/dist/harness-runtime/lib/session-settlement.mjs +30 -0
  52. package/dist/harness-runtime/lib/tool-observations.d.mts +17 -0
  53. package/dist/harness-runtime/lib/tool-observations.mjs +153 -0
  54. package/dist/harness-runtime/lib/zcode-usage.mjs +54 -0
  55. package/dist/protocol/local-files.js +18 -9
  56. package/dist/runtime/context.js +2 -2
  57. package/dist/schemas/agent-profile.schema.json +1 -1
  58. package/dist/schemas/code-review-result.schema.json +1 -1
  59. package/dist/schemas/code-work-batch.schema.json +3 -3
  60. package/dist/schemas/code-work-result.schema.json +3 -3
  61. package/dist/schemas/plan-review-result.schema.json +2 -2
  62. package/dist/schemas/run-control-receipt.schema.json +99 -0
  63. package/dist/schemas/run-control-request.schema.json +36 -0
  64. package/dist/schemas/vnext-protocol-plan.schema.json +2 -2
  65. package/dist/services/canon.js +11 -8
  66. package/dist/services/cleanup.js +132 -39
  67. package/dist/services/cli-operation-classifier.js +22 -24
  68. package/dist/services/code-checks.js +589 -106
  69. package/dist/services/codex-hook-delivery.js +28 -0
  70. package/dist/services/command-context.js +80 -0
  71. package/dist/services/config.js +11 -8
  72. package/dist/services/continuation-outcome.js +20 -0
  73. package/dist/services/controller-fanout.js +173 -0
  74. package/dist/services/dashboard.js +103 -31
  75. package/dist/services/engines.js +121 -76
  76. package/dist/services/eval-snapshots.js +994 -97
  77. package/dist/services/execution-policy.js +181 -0
  78. package/dist/services/external-work-launch.js +166 -0
  79. package/dist/services/harness-adapter.js +147 -24
  80. package/dist/services/harness-config.js +2 -0
  81. package/dist/services/hooks.js +702 -327
  82. package/dist/services/lanes.js +78 -65
  83. package/dist/services/lifecycle-command.js +113 -18
  84. package/dist/services/lifecycle-contract.js +30 -0
  85. package/dist/services/lifecycle-invocations.js +1379 -0
  86. package/dist/services/managed-daemon-binding.js +38 -0
  87. package/dist/services/managed-processes.js +302 -34
  88. package/dist/services/merge-queue.js +228 -149
  89. package/dist/services/merge-server.js +66 -28
  90. package/dist/services/migrations.js +37 -16
  91. package/dist/services/native-daemon-history.js +49 -0
  92. package/dist/services/native-session-control.js +39 -0
  93. package/dist/services/plan-runtime.js +26 -11
  94. package/dist/services/plans.js +28 -25
  95. package/dist/services/projects.js +20 -15
  96. package/dist/services/prompts.js +25 -16
  97. package/dist/services/protocols.js +160 -86
  98. package/dist/services/recovery-observation-budget.js +61 -0
  99. package/dist/services/recovery-snapshot-database.js +142 -0
  100. package/dist/services/repair-intents.js +40 -0
  101. package/dist/services/review-copy.js +136 -0
  102. package/dist/services/run-control-receipt.js +56 -0
  103. package/dist/services/run-control-worker.js +351 -0
  104. package/dist/services/run-control.js +888 -0
  105. package/dist/services/run-controller-adapter.js +231 -0
  106. package/dist/services/run-controller-capture.js +167 -0
  107. package/dist/services/run-controller-process.js +180 -0
  108. package/dist/services/run-controller-recovery.js +221 -0
  109. package/dist/services/run-controller-state.js +36 -0
  110. package/dist/services/run-controller.js +798 -0
  111. package/dist/services/run-engine-bindings.js +1 -1
  112. package/dist/services/run-fork.js +142 -0
  113. package/dist/services/run-observations.js +112 -0
  114. package/dist/services/run-projection.js +22 -64
  115. package/dist/services/run-recovery-runtime.js +80 -0
  116. package/dist/services/run-recovery.js +345 -0
  117. package/dist/services/runs.js +536 -181
  118. package/dist/services/runtime-budget.js +413 -0
  119. package/dist/services/runtime-command.js +43 -0
  120. package/dist/services/runtime-scope-capture.js +52 -0
  121. package/dist/services/runtime-scope-control.js +460 -0
  122. package/dist/services/runtime-scope-resume.js +577 -0
  123. package/dist/services/runtime-scope-stop.js +159 -0
  124. package/dist/services/runtime-scope-worker.js +242 -0
  125. package/dist/services/runtime-service.js +97 -0
  126. package/dist/services/schema-validation.js +10 -8
  127. package/dist/services/scope-writer-cleanup.js +26 -0
  128. package/dist/services/sessions.js +35 -69
  129. package/dist/services/stage-blocker.js +17 -7
  130. package/dist/services/stage-context.js +50 -19
  131. package/dist/services/stage-lifecycle.js +100 -103
  132. package/dist/services/stage-pause.js +88 -75
  133. package/dist/services/stage-report-renderer.js +116 -6
  134. package/dist/services/stage-work-graph.js +35 -0
  135. package/dist/services/usage.js +35 -98
  136. package/dist/services/vnext-code-review.js +197 -84
  137. package/dist/services/vnext-code.js +278 -251
  138. package/dist/services/vnext-execution-profile.js +5 -3
  139. package/dist/services/vnext-fanout.js +172 -17
  140. package/dist/services/vnext-merge.js +345 -111
  141. package/dist/services/vnext-plan-review.js +171 -111
  142. package/dist/services/vnext-plan.js +180 -87
  143. package/dist/services/vnext-protocolize.js +320 -74
  144. package/dist/services/vnext-specify.js +115 -77
  145. package/dist/services/work-registry.js +1001 -187
  146. package/dist/services/workspace-bootstrap.js +76 -0
  147. package/dist/services/workspace-files.js +83 -0
  148. package/dist/services/worktrees.js +88 -36
  149. package/dist/shared/entity-references.js +8 -0
  150. package/dist/shared/errors.js +12 -0
  151. package/dist/storage/database.js +692 -35
  152. package/dist/storage/foreign-key-transaction.js +39 -0
  153. package/dist/storage/immutable-input.js +45 -0
  154. package/dist/storage/import-guard.js +102 -0
  155. package/dist/storage/native-cleanup.js +52 -0
  156. package/dist/storage/paths.js +17 -4
  157. package/dist/storage/projection-lock.js +73 -0
  158. package/dist/storage/work-references.js +23 -0
  159. package/dist/storage/writer-contract.js +77 -0
  160. package/dist/storage/writer-migration.js +85 -0
  161. package/package.json +24 -13
  162. package/tools/repair-paused-run-status.mjs +84 -0
@@ -1,12 +1,211 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import crypto from "node:crypto";
4
+ import { channel } from "node:diagnostics_channel";
4
5
  import { createRequire } from "node:module";
5
6
  import { ensureDir } from "./paths.js";
6
7
  import { AppError } from "../shared/errors.js";
8
+ import { executeStoreSchema, readStoreWriterContract, registerStoreWriter } from "./writer-contract.js";
9
+ import { assertImportAdmission } from "./import-guard.js";
10
+ import { withStoreCleanup, rollbackStoreTransaction } from "./native-cleanup.js";
11
+ const workContinuationStorageSql = `
12
+ CREATE TABLE IF NOT EXISTS stage_work_memberships (
13
+ project_id TEXT NOT NULL, run_id TEXT NOT NULL, work_id TEXT NOT NULL REFERENCES works(work_id),
14
+ stage TEXT NOT NULL, attempt TEXT NOT NULL, cycle TEXT NOT NULL DEFAULT '',
15
+ PRIMARY KEY(project_id, run_id, work_id, stage, attempt, cycle)
16
+ );
17
+ CREATE TABLE IF NOT EXISTS repair_intents (
18
+ identity TEXT PRIMARY KEY, project_id TEXT NOT NULL, run_id TEXT NOT NULL,
19
+ stage TEXT NOT NULL, attempt TEXT NOT NULL, payload_hash TEXT NOT NULL,
20
+ payload_json TEXT NOT NULL, work_id TEXT NOT NULL REFERENCES works(work_id), created_at TEXT NOT NULL
21
+ );
22
+ `;
23
+ function closeAfterOpenFailure(db, error) {
24
+ return withStoreCleanup(() => { throw error; }, () => { if (db.isOpen)
25
+ db.close(); });
26
+ }
7
27
  const require = createRequire(import.meta.url);
8
28
  const { DatabaseSync } = require("node:sqlite");
9
29
  const resourceDatabases = new Map();
30
+ const publicationScopes = [];
31
+ /** Defer committed cross-store projections until the surrounding writer exits. */
32
+ export function withDeferredPublications(operation) {
33
+ const pending = [];
34
+ publicationScopes.push(pending);
35
+ let value;
36
+ let primary;
37
+ let failed = false;
38
+ try {
39
+ value = operation();
40
+ if (value && typeof value.then === "function")
41
+ throw new AppError("write_transaction_async", "Deferred publication scope must be synchronous", 1);
42
+ }
43
+ catch (error) {
44
+ primary = error;
45
+ failed = true;
46
+ }
47
+ publicationScopes.pop();
48
+ const parent = publicationScopes.at(-1);
49
+ if (parent)
50
+ parent.push(...pending);
51
+ else {
52
+ try {
53
+ executePublications(pending);
54
+ }
55
+ catch (publicationError) {
56
+ if (failed && primary instanceof AppError)
57
+ throw new AppError(primary.code, primary.message, primary.exitCode, { ...primary.details, effect: "partial", committed_publications: pending.length, publication_pending: true, publication_error: errorDetails(publicationError) });
58
+ if (failed)
59
+ throw new AppError("cross_store_failed", "Cross-store mutation failed and committed projections need reconciliation", 1, { effect: "partial", cause: errorDetails(primary), committed_publications: pending.length, publication_pending: true, publication_error: errorDetails(publicationError) });
60
+ throw publicationError;
61
+ }
62
+ }
63
+ if (failed && pending.length && primary instanceof AppError)
64
+ throw new AppError(primary.code, primary.message, primary.exitCode, { ...primary.details, effect: "partial", committed_publications: pending.length });
65
+ if (failed && pending.length)
66
+ throw new AppError("cross_store_failed", "Cross-store mutation failed after a committed inner write", 1, { effect: "partial", cause: errorDetails(primary), committed_publications: pending.length });
67
+ if (failed)
68
+ throw primary;
69
+ return value;
70
+ }
71
+ function executePublications(pending) {
72
+ let firstError;
73
+ let failed = false;
74
+ for (const operation of pending) {
75
+ try {
76
+ operation();
77
+ }
78
+ catch (error) {
79
+ if (!failed)
80
+ firstError = error;
81
+ failed = true;
82
+ }
83
+ }
84
+ if (failed) {
85
+ if (firstError instanceof AppError && firstError.details.effect === "committed")
86
+ throw firstError;
87
+ throw new AppError("publication_pending", "SQL committed but a required projection needs reconciliation", 1, { effect: "committed", publication_pending: true, cause: errorDetails(firstError) });
88
+ }
89
+ }
90
+ /** Invocation authority is prepared once with the runtime schema. Native
91
+ * hooks must only verify this table; they never create it on the hot path. */
92
+ export function ensureLifecycleInvocationStorage(db) {
93
+ db.exec(`
94
+ CREATE TABLE IF NOT EXISTS lifecycle_invocations (
95
+ id TEXT PRIMARY KEY, scope_json TEXT NOT NULL, command TEXT NOT NULL,
96
+ fingerprint TEXT NOT NULL, status TEXT NOT NULL,
97
+ event_key TEXT UNIQUE, identity_json TEXT, deadline INTEGER, outcome_json TEXT,
98
+ created_at TEXT NOT NULL, updated_at TEXT NOT NULL
99
+ );
100
+ CREATE INDEX IF NOT EXISTS idx_lifecycle_invocations_scope
101
+ ON lifecycle_invocations(scope_json, fingerprint, created_at);
102
+ CREATE TABLE IF NOT EXISTS lifecycle_packet_assignments (
103
+ packet_sha256 TEXT NOT NULL,
104
+ invocation_id TEXT NOT NULL REFERENCES lifecycle_invocations(id),
105
+ PRIMARY KEY(packet_sha256, invocation_id)
106
+ );
107
+ `);
108
+ }
109
+ /** RUN authority stays in its existing store; process leases stay in runtime.sqlite. */
110
+ export function ensureRunControlStorage(db) {
111
+ db.execSchema(`
112
+ CREATE TABLE IF NOT EXISTS run_controls (
113
+ control_id TEXT PRIMARY KEY,
114
+ project_id TEXT NOT NULL, run_id TEXT NOT NULL,
115
+ request_id TEXT NOT NULL, request_hash TEXT NOT NULL, request_json TEXT NOT NULL,
116
+ recovery_id TEXT NOT NULL REFERENCES run_recovery_guards(recovery_id),
117
+ action TEXT NOT NULL, status TEXT NOT NULL,
118
+ created_at TEXT NOT NULL, updated_at TEXT NOT NULL,
119
+ UNIQUE(project_id, run_id, request_id),
120
+ FOREIGN KEY(project_id, run_id) REFERENCES runs(project_id, id)
121
+ );
122
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_run_control_active
123
+ ON run_controls(project_id, run_id) WHERE status IN ('requested','settled','resuming');
124
+ CREATE TABLE IF NOT EXISTS run_control_admissions (
125
+ control_id TEXT NOT NULL REFERENCES run_controls(control_id),
126
+ work_session_id TEXT NOT NULL REFERENCES work_sessions(id),
127
+ work_id TEXT NOT NULL REFERENCES works(work_id),
128
+ session_id TEXT NOT NULL, admitted_at TEXT NOT NULL,
129
+ PRIMARY KEY(control_id, work_session_id)
130
+ );
131
+ CREATE TABLE IF NOT EXISTS run_control_stage_admissions (
132
+ control_id TEXT NOT NULL REFERENCES run_controls(control_id),
133
+ stage TEXT NOT NULL, attempt TEXT NOT NULL,
134
+ work_session_id TEXT NOT NULL REFERENCES work_sessions(id),
135
+ PRIMARY KEY(control_id, stage)
136
+ );
137
+ CREATE TABLE IF NOT EXISTS run_control_operations (
138
+ operation_id TEXT PRIMARY KEY, control_id TEXT NOT NULL REFERENCES run_controls(control_id),
139
+ target_json TEXT NOT NULL, status TEXT NOT NULL, receipt_json TEXT, error_json TEXT,
140
+ created_at TEXT NOT NULL, updated_at TEXT NOT NULL
141
+ );
142
+ CREATE TABLE IF NOT EXISTS run_control_workers (
143
+ control_id TEXT PRIMARY KEY REFERENCES run_controls(control_id),
144
+ owner_token TEXT NOT NULL, owner_pid INTEGER,
145
+ status TEXT NOT NULL, process_id TEXT, process_lease_token TEXT,
146
+ resource_home TEXT NOT NULL, state_dir TEXT NOT NULL,
147
+ snapshot_json TEXT, error_json TEXT,
148
+ created_at TEXT NOT NULL, updated_at TEXT NOT NULL
149
+ );
150
+ `);
151
+ }
152
+ export function ensureRunControllerStorage(db) {
153
+ db.execSchema(`
154
+ CREATE TABLE IF NOT EXISTS run_controllers (
155
+ controller_id TEXT PRIMARY KEY,
156
+ project_id TEXT NOT NULL, run_id TEXT NOT NULL,
157
+ request_id TEXT NOT NULL, request_hash TEXT NOT NULL,
158
+ generation INTEGER NOT NULL, status TEXT NOT NULL,
159
+ owner_token TEXT NOT NULL, process_id TEXT, process_lease_token TEXT,
160
+ owner_pid INTEGER, manifest_json TEXT NOT NULL, state_json TEXT NOT NULL,
161
+ state_dir TEXT NOT NULL, last_error_json TEXT,
162
+ created_at TEXT NOT NULL, updated_at TEXT NOT NULL,
163
+ FOREIGN KEY(project_id, run_id) REFERENCES runs(project_id, id),
164
+ UNIQUE(project_id, run_id, request_id)
165
+ );
166
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_run_controller_owner
167
+ ON run_controllers(project_id, run_id)
168
+ WHERE status NOT IN ('completed', 'cancelled', 'superseded');
169
+ CREATE TABLE IF NOT EXISTS run_controller_events (
170
+ sequence INTEGER PRIMARY KEY AUTOINCREMENT,
171
+ controller_id TEXT NOT NULL REFERENCES run_controllers(controller_id),
172
+ event_type TEXT NOT NULL, data_json TEXT NOT NULL, created_at TEXT NOT NULL
173
+ );
174
+ CREATE INDEX IF NOT EXISTS idx_run_controller_events
175
+ ON run_controller_events(controller_id, sequence);
176
+ CREATE TABLE IF NOT EXISTS run_controller_operations (
177
+ operation_id TEXT PRIMARY KEY,
178
+ controller_id TEXT NOT NULL REFERENCES run_controllers(controller_id),
179
+ generation INTEGER NOT NULL, input_hash TEXT NOT NULL,
180
+ input_json TEXT NOT NULL, status TEXT NOT NULL,
181
+ receipt_json TEXT, error_json TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL
182
+ );
183
+ `);
184
+ }
185
+ /** Recovery guards apply to every RUN kind, including custom eval fixtures. */
186
+ export function ensureRecoveryStorage(db) {
187
+ db.exec(`
188
+ CREATE TABLE IF NOT EXISTS run_recovery_guards (
189
+ recovery_id TEXT PRIMARY KEY, project_id TEXT NOT NULL, run_id TEXT NOT NULL, generation INTEGER NOT NULL, status TEXT NOT NULL, interruption_json TEXT NOT NULL, settlement_json TEXT, capture_path TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL, sealed_at TEXT, resumed_at TEXT, UNIQUE(project_id, run_id, generation), FOREIGN KEY(project_id, run_id) REFERENCES runs(project_id, id)
190
+ );
191
+ CREATE INDEX IF NOT EXISTS idx_run_recovery_guards_run ON run_recovery_guards(project_id, run_id, generation DESC);
192
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_run_recovery_guards_active ON run_recovery_guards(project_id, run_id) WHERE status IN ('draining', 'sealed', 'resuming');
193
+ CREATE TABLE IF NOT EXISTS work_recovery_segments (
194
+ recovery_id TEXT NOT NULL, work_id TEXT NOT NULL, generation INTEGER NOT NULL, prior_started_at TEXT, evidence_path TEXT NOT NULL, status TEXT NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL, PRIMARY KEY(recovery_id, work_id), FOREIGN KEY(recovery_id) REFERENCES run_recovery_guards(recovery_id), FOREIGN KEY(work_id) REFERENCES works(work_id)
195
+ );
196
+ CREATE INDEX IF NOT EXISTS idx_work_recovery_segments_work ON work_recovery_segments(work_id, generation DESC);
197
+ CREATE TABLE IF NOT EXISTS run_recovery_bindings (
198
+ recovery_id TEXT PRIMARY KEY REFERENCES run_recovery_guards(recovery_id),
199
+ work_id TEXT NOT NULL REFERENCES works(work_id), prior_work_session_id TEXT NOT NULL,
200
+ session_id TEXT NOT NULL, harness TEXT NOT NULL, provider_session_id TEXT NOT NULL,
201
+ daemon_id TEXT NOT NULL, status TEXT NOT NULL, hook_event_key TEXT,
202
+ created_at TEXT NOT NULL, accepted_at TEXT
203
+ );
204
+ `);
205
+ ensureColumn(db, "run_recovery_bindings", "packet_json", "ALTER TABLE run_recovery_bindings ADD COLUMN packet_json TEXT");
206
+ ensureColumn(db, "run_recovery_bindings", "packet_sha256", "ALTER TABLE run_recovery_bindings ADD COLUMN packet_sha256 TEXT");
207
+ ensureColumn(db, "run_recovery_bindings", "runtime_dispatch_json", "ALTER TABLE run_recovery_bindings ADD COLUMN runtime_dispatch_json TEXT");
208
+ }
10
209
  /** Creates the single Work/Session authority used by a fresh vNext beta runtime. */
11
210
  export function ensureVnextWorkStorage(db) {
12
211
  const legacy = db.get("SELECT name FROM sqlite_master WHERE type = 'table' AND name IN ('vnext_works', 'vnext_agent_turns', 'flow_agent_turns') LIMIT 1");
@@ -16,7 +215,7 @@ export function ensureVnextWorkStorage(db) {
16
215
  if (existing && (!/task TEXT NOT NULL/.test(existing.sql) || /launch_token|adapter_session_id/.test(existing.sql))) {
17
216
  throw new AppError("beta_storage_recreate_required", "This beta requires a fresh DD_FLOW_HOME; the existing works schema is not compatible", 1);
18
217
  }
19
- db.exec(`
218
+ db.execSchema(`
20
219
  CREATE TABLE IF NOT EXISTS works (
21
220
  work_id TEXT PRIMARY KEY,
22
221
  project_id TEXT NOT NULL,
@@ -46,6 +245,9 @@ export function ensureVnextWorkStorage(db) {
46
245
  status TEXT NOT NULL,
47
246
  prompt_path TEXT NOT NULL,
48
247
  result_path TEXT,
248
+ start_receipt_json TEXT,
249
+ start_publication_status TEXT,
250
+ result_receipt_json TEXT,
49
251
  created_at TEXT NOT NULL,
50
252
  updated_at TEXT NOT NULL,
51
253
  completed_at TEXT,
@@ -53,6 +255,16 @@ export function ensureVnextWorkStorage(db) {
53
255
  );
54
256
  CREATE INDEX IF NOT EXISTS idx_work_sessions_work ON work_sessions(work_id, created_at DESC);
55
257
  CREATE UNIQUE INDEX IF NOT EXISTS idx_work_sessions_open ON work_sessions(work_id) WHERE status = 'running';
258
+
259
+ CREATE TABLE IF NOT EXISTS run_recovery_guards (
260
+ recovery_id TEXT PRIMARY KEY, project_id TEXT NOT NULL, run_id TEXT NOT NULL, generation INTEGER NOT NULL, status TEXT NOT NULL, interruption_json TEXT NOT NULL, settlement_json TEXT, capture_path TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL, sealed_at TEXT, resumed_at TEXT, UNIQUE(project_id, run_id, generation), FOREIGN KEY(project_id, run_id) REFERENCES runs(project_id, id)
261
+ );
262
+ CREATE INDEX IF NOT EXISTS idx_run_recovery_guards_run ON run_recovery_guards(project_id, run_id, generation DESC);
263
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_run_recovery_guards_active ON run_recovery_guards(project_id, run_id) WHERE status IN ('draining', 'sealed', 'resuming');
264
+ CREATE TABLE IF NOT EXISTS work_recovery_segments (
265
+ recovery_id TEXT NOT NULL, work_id TEXT NOT NULL, generation INTEGER NOT NULL, prior_started_at TEXT, evidence_path TEXT NOT NULL, status TEXT NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL, PRIMARY KEY(recovery_id, work_id), FOREIGN KEY(recovery_id) REFERENCES run_recovery_guards(recovery_id), FOREIGN KEY(work_id) REFERENCES works(work_id)
266
+ );
267
+ CREATE INDEX IF NOT EXISTS idx_work_recovery_segments_work ON work_recovery_segments(work_id, generation DESC);
56
268
  CREATE TABLE IF NOT EXISTS check_receipts (
57
269
  id TEXT PRIMARY KEY,
58
270
  project_id TEXT NOT NULL,
@@ -65,6 +277,9 @@ export function ensureVnextWorkStorage(db) {
65
277
  command TEXT NOT NULL,
66
278
  input_hash TEXT NOT NULL DEFAULT '',
67
279
  verification_epoch TEXT NOT NULL DEFAULT '',
280
+ reservation_sequence INTEGER NOT NULL DEFAULT 0,
281
+ retry_of_receipt_id TEXT,
282
+ retry_reason TEXT,
68
283
  status TEXT NOT NULL,
69
284
  exit_code INTEGER,
70
285
  stdout_path TEXT NOT NULL,
@@ -81,50 +296,199 @@ export function ensureVnextWorkStorage(db) {
81
296
  FOREIGN KEY(work_id) REFERENCES works(work_id)
82
297
  );
83
298
  CREATE INDEX IF NOT EXISTS idx_check_receipts_run ON check_receipts(project_id, run_id, started_at);
299
+ ${workContinuationStorageSql}
84
300
  `);
301
+ // A committed Work start is authoritative even if publishing its prompt
302
+ // files is interrupted after commit. Keep the exact bytes for repair.
303
+ db.writeTransaction(() => {
304
+ const columns = db.all("PRAGMA table_info(work_sessions)");
305
+ if (!columns.some((column) => column.name === "start_receipt_json"))
306
+ db.execSchema("ALTER TABLE work_sessions ADD COLUMN start_receipt_json TEXT");
307
+ if (!columns.some((column) => column.name === "start_publication_status"))
308
+ db.execSchema("ALTER TABLE work_sessions ADD COLUMN start_publication_status TEXT");
309
+ if (!columns.some((column) => column.name === "result_receipt_json"))
310
+ db.execSchema("ALTER TABLE work_sessions ADD COLUMN result_receipt_json TEXT");
311
+ });
85
312
  }
86
- export function getDatabase(ddFlowHome, mode = "initialize") {
313
+ /** Durable intent for a provider-root external Work. It is deliberately
314
+ * separate from `work_sessions`: only a trusted hook may bind that Session. */
315
+ export function ensureWorkLaunchStorage(db) {
316
+ db.execSchema(`
317
+ CREATE TABLE IF NOT EXISTS work_launches (
318
+ operation_id TEXT PRIMARY KEY,
319
+ work_id TEXT NOT NULL UNIQUE,
320
+ profile_json TEXT NOT NULL,
321
+ profile_hash TEXT NOT NULL,
322
+ status TEXT NOT NULL,
323
+ state_dir TEXT NOT NULL,
324
+ journal_path TEXT NOT NULL,
325
+ provider_session_id TEXT,
326
+ adapter_receipt_json TEXT,
327
+ created_at TEXT NOT NULL,
328
+ updated_at TEXT NOT NULL,
329
+ FOREIGN KEY(work_id) REFERENCES works(work_id)
330
+ );
331
+ CREATE INDEX IF NOT EXISTS idx_work_launches_status ON work_launches(status, updated_at DESC);
332
+ `);
333
+ }
334
+ export function getDatabase(ddFlowHome, mode = "initialize", importPermitId) {
335
+ if (mode !== "read_existing")
336
+ assertImportAdmission(ddFlowHome, importPermitId);
87
337
  const dbPath = path.join(ddFlowHome, "db.sqlite");
88
338
  const exists = fs.existsSync(dbPath);
89
339
  if (mode === "read_existing" && !exists) {
90
340
  return emptyReadOnlyDatabase(dbPath);
91
341
  }
342
+ if (mode === "hook" && !exists)
343
+ throw new AppError("hook_storage_unprepared", "Prepare the runtime store before starting native hooks", 1);
92
344
  if (!exists || mode === "migrate")
93
345
  ensureDir(ddFlowHome);
94
346
  const db = mode === "read_existing" ? new DatabaseSync(dbPath, { readOnly: true }) : new DatabaseSync(dbPath);
95
- configureDatabase(db, mode);
347
+ db.exec("PRAGMA busy_timeout = 4000");
348
+ if (mode !== "read_existing") {
349
+ try {
350
+ registerStoreWriter(db);
351
+ }
352
+ catch (error) {
353
+ return closeAfterOpenFailure(db, error);
354
+ }
355
+ }
356
+ try {
357
+ configureDatabase(db, mode);
358
+ }
359
+ catch (error) {
360
+ return closeAfterOpenFailure(db, error);
361
+ }
96
362
  // Internal additive schema changes must be available to every write command.
97
363
  // Higher-level Memory Bank migrations remain explicit in services/migrations.
98
- if (mode !== "read_existing")
99
- migrate(db, dbPath);
364
+ if (mode !== "read_existing") {
365
+ try {
366
+ const initialize = () => { migrate(db); ensureRecoveryStorage(db); ensureLifecycleInvocationStorage(db); };
367
+ if (mode !== "hook") {
368
+ const fresh = !db.prepare("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%' LIMIT 1").get();
369
+ if (fresh || readStoreWriterContract(db) !== null)
370
+ executeStoreSchema(db, initialize, fresh);
371
+ else
372
+ initialize();
373
+ // This migration owns a VACUUM backup and a separate data transaction.
374
+ migrateSessionStorageKeys(db, dbPath);
375
+ }
376
+ else {
377
+ assertHookStoreReady(db);
378
+ }
379
+ }
380
+ catch (error) {
381
+ return closeAfterOpenFailure(db, error);
382
+ }
383
+ }
100
384
  if (mode === "read_existing")
101
385
  db.exec("PRAGMA query_only = ON");
102
386
  db.exec("PRAGMA foreign_keys = ON");
103
- return {
104
- path: dbPath,
105
- writable: mode !== "read_existing",
106
- close: () => db.close?.(),
107
- exec: (sql) => db.exec(sql),
108
- run: (sql, params = []) => db.prepare(sql).run(...params),
109
- get: (sql, params = []) => db.prepare(sql).get(...params),
110
- all: (sql, params = []) => db.prepare(sql).all(...params)
387
+ return databaseFacade(dbPath, mode !== "read_existing", db);
388
+ }
389
+ /**
390
+ * Hook ingress is a hot, synchronous path. It may write receipts, but it
391
+ * must never perform schema migration or VACUUM while the native host waits.
392
+ * Startup/migration owns this contract; a missing table is an explicit
393
+ * preparation error instead of an implicit migration race.
394
+ */
395
+ function assertHookStoreReady(db) {
396
+ const required = ["projects", "hook_events", "codex_session_bindings", "sessions", "flow_session_segments", "lifecycle_invocations", "lifecycle_packet_assignments"];
397
+ const missing = required.filter((table) => !db.prepare("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = ?").get(table));
398
+ if (missing.length)
399
+ throw new AppError("hook_storage_unprepared", "Managed store is not prepared for synchronous hook admission", 1, { missing_tables: missing, next_action: "Open the runtime with a normal dd-flow command and complete migration before starting the native Session." });
400
+ // Table presence alone does not establish compatibility with an older home.
401
+ const receiptColumns = {
402
+ hook_events: "id, project_id, harness, provider_session_id, parent_session_id, daemon_id, session_id, agent_id, turn_id, event_name, tool_name, event_key, cwd, match_key, transcript_path, status, sanitized_summary, outcome_json, created_at",
403
+ lifecycle_invocations: "id, scope_json, command, fingerprint, status, event_key, identity_json, deadline, outcome_json, created_at, updated_at",
404
+ codex_session_bindings: "session_id, project_id, protocol_id, handshake_id, cwd, transcript_path, status, created_at, updated_at"
111
405
  };
406
+ for (const [table, columns] of Object.entries(receiptColumns)) {
407
+ try {
408
+ db.prepare(`SELECT ${columns} FROM ${table} LIMIT 0`);
409
+ }
410
+ catch {
411
+ throw new AppError("hook_storage_unprepared", "Managed receipt schema needs preparation before native hooks start", 1, { table });
412
+ }
413
+ }
112
414
  }
113
415
  /**
114
416
  * Opens the small host-wide resource registry. It is intentionally separate
115
417
  * from a RUN database: a process or a port can outlive the CLI client that
116
418
  * created it, while a RUN database belongs to one flow home.
117
419
  */
118
- export function getResourceDatabase(resourceHome) {
119
- ensureDir(resourceHome);
420
+ export function withResourceReadSnapshot(resourceHome, read) {
120
421
  const dbPath = path.join(resourceHome, "runtime.sqlite");
121
422
  const cached = resourceDatabases.get(dbPath);
423
+ if (!cached && !fs.existsSync(dbPath))
424
+ return read(emptyReadOnlyDatabase(dbPath));
425
+ const db = cached ?? new DatabaseSync(dbPath, { readOnly: true });
426
+ const operation = () => {
427
+ if (!cached)
428
+ db.exec("PRAGMA busy_timeout = 4000; PRAGMA query_only = ON");
429
+ // A second process can observe SQLite's file before the initializing
430
+ // transaction publishes its schema. Treat that pre-commit view exactly
431
+ // like an absent registry; the writer path remains the serialization
432
+ // boundary and will reopen the committed schema.
433
+ if (!cached && !db.prepare("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%' LIMIT 1").get()) {
434
+ return read(emptyReadOnlyDatabase(dbPath));
435
+ }
436
+ const reader = cached
437
+ ? { ...emptyReadOnlyDatabase(dbPath), get: cached.get, all: cached.all }
438
+ : databaseFacade(dbPath, false, db);
439
+ // The facade owns savepoint cleanup and poisons a connection when rollback
440
+ // fails; the callback only receives the read-only view.
441
+ return (cached ?? reader).readSnapshot(() => read(reader));
442
+ };
443
+ if (cached)
444
+ return operation();
445
+ const native = db;
446
+ return withStoreCleanup(operation, () => { if (native.isOpen)
447
+ native.close(); });
448
+ }
449
+ export function getResourceDatabase(resourceHome, mode = "initialize") {
450
+ const dbPath = path.join(resourceHome, "runtime.sqlite");
451
+ const cached = resourceDatabases.get(dbPath);
452
+ if (mode === "read_existing" || mode === "read_isolated") {
453
+ // Reuse a current transaction's view without exposing its write capability.
454
+ if (cached && mode === "read_existing")
455
+ return { ...emptyReadOnlyDatabase(dbPath), get: cached.get, all: cached.all };
456
+ if (!fs.existsSync(dbPath))
457
+ return emptyReadOnlyDatabase(dbPath);
458
+ const db = new DatabaseSync(dbPath, { readOnly: true });
459
+ try {
460
+ db.exec("PRAGMA busy_timeout = 4000; PRAGMA query_only = ON");
461
+ if (!db.prepare("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%' LIMIT 1").get()) {
462
+ db.close();
463
+ return emptyReadOnlyDatabase(dbPath);
464
+ }
465
+ }
466
+ catch (error) {
467
+ return closeAfterOpenFailure(db, error);
468
+ }
469
+ return databaseFacade(dbPath, false, db);
470
+ }
471
+ ensureDir(resourceHome);
122
472
  if (cached)
123
473
  return cached;
124
474
  const db = new DatabaseSync(dbPath);
125
- configureDatabase(db, "initialize");
475
+ db.exec("PRAGMA busy_timeout = 4000");
476
+ try {
477
+ registerStoreWriter(db);
478
+ }
479
+ catch (error) {
480
+ return closeAfterOpenFailure(db, error);
481
+ }
482
+ try {
483
+ configureDatabase(db, "initialize");
484
+ }
485
+ catch (error) {
486
+ return closeAfterOpenFailure(db, error);
487
+ }
126
488
  db.exec("PRAGMA foreign_keys = ON");
127
- db.exec(`
489
+ const fresh = !db.prepare("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%' LIMIT 1").get();
490
+ try {
491
+ executeStoreSchema(db, `
128
492
  CREATE TABLE IF NOT EXISTS managed_processes (
129
493
  id TEXT PRIMARY KEY,
130
494
  kind TEXT NOT NULL,
@@ -163,16 +527,15 @@ export function getResourceDatabase(resourceHome) {
163
527
  );
164
528
  CREATE INDEX IF NOT EXISTS idx_managed_resources_lease
165
529
  ON managed_resources(lease_expires_at, updated_at);
166
- `);
167
- const database = {
168
- path: dbPath,
169
- writable: true,
170
- close: () => { resourceDatabases.delete(dbPath); db.close?.(); },
171
- exec: (sql) => db.exec(sql),
172
- run: (sql, params = []) => db.prepare(sql).run(...params),
173
- get: (sql, params = []) => db.prepare(sql).get(...params),
174
- all: (sql, params = []) => db.prepare(sql).all(...params)
175
- };
530
+ `, fresh);
531
+ }
532
+ catch (error) {
533
+ return closeAfterOpenFailure(db, error);
534
+ }
535
+ const database = databaseFacade(dbPath, true, db, () => {
536
+ resourceDatabases.delete(dbPath);
537
+ db.close?.();
538
+ });
176
539
  resourceDatabases.set(dbPath, database);
177
540
  return database;
178
541
  }
@@ -181,19 +544,279 @@ function emptyReadOnlyDatabase(dbPath) {
181
544
  path: dbPath,
182
545
  writable: false,
183
546
  exec: () => { throw new AppError("read_only_storage", "Read-only access cannot write absent dd-flow storage", 1, { path: dbPath }); },
547
+ execSchema: () => { throw new AppError("read_only_storage", "Read-only access cannot write absent dd-flow storage", 1, { path: dbPath }); },
548
+ beginWriteTransaction: () => { throw new AppError("read_only_storage", "Read-only access cannot start a write transaction", 1, { path: dbPath }); },
549
+ writeTransaction: () => { throw new AppError("read_only_storage", "Read-only access cannot start a write transaction", 1, { path: dbPath }); },
550
+ readSnapshot: (operation) => operation(),
551
+ afterCommit: () => { throw new AppError("read_only_storage", "Read-only access cannot publish absent dd-flow storage", 1, { path: dbPath }); },
184
552
  run: () => { throw new AppError("read_only_storage", "Read-only access cannot write absent dd-flow storage", 1, { path: dbPath }); },
185
553
  get: () => undefined,
186
554
  all: () => []
187
555
  };
188
556
  }
557
+ function databaseFacade(dbPath, writable, db, close = () => db.close?.()) {
558
+ let reserved = false;
559
+ let poisoned = false;
560
+ let closed = false;
561
+ const closeOnce = () => { if (closed || !db.isOpen) {
562
+ closed = true;
563
+ return;
564
+ } closed = true; close(); };
565
+ let savepoint = 0;
566
+ let afterCommit = [];
567
+ let manualFrames = [];
568
+ let readDepth = 0;
569
+ const writerTiming = channel("dd-flow.sqlite.writer");
570
+ const publishCommitted = () => {
571
+ const pending = afterCommit;
572
+ afterCommit = [];
573
+ const scope = publicationScopes.at(-1);
574
+ if (scope)
575
+ scope.push(...pending);
576
+ else
577
+ executePublications(pending);
578
+ };
579
+ const assertUsable = () => { if (poisoned || closed || !db.isOpen)
580
+ throw new AppError("write_transaction_unresolved", "Transaction cleanup failed or the connection is closed; reopen the connection before continuing", 1); };
581
+ const beginWriteTransaction = () => {
582
+ assertUsable();
583
+ if (readDepth)
584
+ throw new AppError("read_snapshot_write", "Cannot reserve a writer inside a read snapshot", 1);
585
+ if (!writable)
586
+ throw new AppError("read_only_storage", "Read-only access cannot start a write transaction", 1, { path: dbPath });
587
+ db.exec("BEGIN IMMEDIATE");
588
+ reserved = true;
589
+ };
590
+ const rollback = (sql, original) => {
591
+ try {
592
+ db.exec(sql);
593
+ }
594
+ catch (cleanup) {
595
+ poisoned = true;
596
+ let closeError;
597
+ try {
598
+ closeOnce();
599
+ }
600
+ catch (error) {
601
+ closeError = errorDetails(error);
602
+ }
603
+ const secondary = { transaction_cleanup_error: errorDetails(cleanup), ...(closeError ? { transaction_close_error: closeError } : {}) };
604
+ if (original instanceof AppError)
605
+ throw new AppError(original.code, original.message, original.exitCode, { ...original.details, ...secondary });
606
+ throw new AppError("write_transaction_failed", "Write transaction failed and SQLite cleanup also failed", 1, { cause: errorDetails(original), ...secondary });
607
+ }
608
+ throw original;
609
+ };
610
+ const writeTransaction = (operation, operationName = "writeTransaction") => {
611
+ assertUsable();
612
+ if (readDepth)
613
+ throw new AppError("read_snapshot_write", "Cannot reserve a writer inside a read snapshot", 1);
614
+ if (!writable)
615
+ throw new AppError("read_only_storage", "Read-only access cannot start a write transaction", 1, { path: dbPath });
616
+ if (db.isTransaction && !reserved)
617
+ throw new AppError("write_transaction_unowned", "Cannot write inside an unowned transaction or read snapshot", 1);
618
+ const nested = db.isTransaction;
619
+ const callbackLength = afterCommit.length;
620
+ const marker = `dd_flow_write_${++savepoint}`;
621
+ const startedAt = performance.now();
622
+ try {
623
+ if (nested)
624
+ db.exec(`SAVEPOINT ${marker}`);
625
+ else
626
+ beginWriteTransaction();
627
+ }
628
+ catch (error) {
629
+ throw new AppError("storage_write_failed", "Cannot reserve SQLite writer", 1, { cause: errorDetails(error), transaction_phase: "begin", transaction_rolled_back: true });
630
+ }
631
+ let phase = "body";
632
+ try {
633
+ const value = operation();
634
+ if (value && typeof value.then === "function") {
635
+ void Promise.resolve(value).catch(() => { });
636
+ throw new AppError("write_transaction_async", "Write transaction callback must be synchronous", 1);
637
+ }
638
+ phase = "commit";
639
+ if (nested)
640
+ db.exec(`RELEASE ${marker}`);
641
+ else
642
+ db.exec("COMMIT");
643
+ phase = "committed";
644
+ if (!nested && writerTiming.hasSubscribers)
645
+ writerTiming.publish({ operation: operationName, wall_time_ms: performance.now() - startedAt, path: dbPath });
646
+ if (!nested)
647
+ publishCommitted();
648
+ return value;
649
+ }
650
+ catch (error) {
651
+ if (phase === "committed")
652
+ throw error;
653
+ afterCommit.length = callbackLength;
654
+ try {
655
+ return rollback(nested ? `ROLLBACK TO ${marker}; RELEASE ${marker}` : "ROLLBACK", error);
656
+ }
657
+ catch (failure) {
658
+ if (failure === error && !(error instanceof AppError) && typeof error === "object" && error !== null && "code" in error && String(error.code).startsWith("ERR_SQLITE"))
659
+ throw new AppError("storage_write_failed", "SQLite write failed", 1, { cause: errorDetails(error), transaction_phase: phase, transaction_rolled_back: true });
660
+ throw failure;
661
+ }
662
+ }
663
+ finally {
664
+ if (closed || !db.isOpen || !db.isTransaction) {
665
+ reserved = false;
666
+ if (phase !== "committed") {
667
+ afterCommit = [];
668
+ manualFrames = [];
669
+ }
670
+ }
671
+ }
672
+ };
673
+ const readSnapshot = (operation) => {
674
+ assertUsable();
675
+ const nested = db.isTransaction;
676
+ const marker = `dd_flow_read_${++savepoint}`;
677
+ const callbackLength = afterCommit.length;
678
+ try {
679
+ db.exec(nested ? `SAVEPOINT ${marker}` : "BEGIN");
680
+ }
681
+ catch (error) {
682
+ throw new AppError("read_snapshot_failed", "Cannot start SQLite read snapshot", 1, { cause: errorDetails(error) });
683
+ }
684
+ readDepth += 1;
685
+ let phase = "body";
686
+ try {
687
+ const value = operation();
688
+ if (value && typeof value.then === "function") {
689
+ void Promise.resolve(value).catch(() => { });
690
+ throw new AppError("read_snapshot_async", "Read snapshot callback must be synchronous", 1);
691
+ }
692
+ phase = "commit";
693
+ db.exec(nested ? `RELEASE ${marker}` : "COMMIT");
694
+ phase = "committed";
695
+ return value;
696
+ }
697
+ catch (error) {
698
+ if (phase === "committed")
699
+ throw error;
700
+ afterCommit.length = callbackLength;
701
+ return rollback(nested ? `ROLLBACK TO ${marker}; RELEASE ${marker}` : "ROLLBACK", error);
702
+ }
703
+ finally {
704
+ readDepth -= 1;
705
+ }
706
+ };
707
+ const applyManualBoundary = (sql) => {
708
+ for (const statement of sql.split(";")) {
709
+ const save = /^\s*SAVEPOINT\s+([\w-]+)/i.exec(statement);
710
+ if (save) {
711
+ manualFrames.push({ name: save[1], length: afterCommit.length });
712
+ continue;
713
+ }
714
+ const rollbackTo = /^\s*ROLLBACK\s+TO(?:\s+SAVEPOINT)?\s+([\w-]+)/i.exec(statement);
715
+ if (rollbackTo) {
716
+ const frame = manualFrames.findLastIndex((item) => item.name === rollbackTo[1]);
717
+ if (frame >= 0) {
718
+ afterCommit.length = manualFrames[frame].length;
719
+ manualFrames.length = frame + 1;
720
+ }
721
+ continue;
722
+ }
723
+ const release = /^\s*RELEASE(?:\s+SAVEPOINT)?\s+([\w-]+)/i.exec(statement);
724
+ if (release) {
725
+ const frame = manualFrames.findLastIndex((item) => item.name === release[1]);
726
+ if (frame >= 0)
727
+ manualFrames.length = frame;
728
+ continue;
729
+ }
730
+ if (/^\s*ROLLBACK\b/i.test(statement)) {
731
+ afterCommit = [];
732
+ manualFrames = [];
733
+ continue;
734
+ }
735
+ if (/^\s*COMMIT\b/i.test(statement)) {
736
+ manualFrames = [];
737
+ publishCommitted();
738
+ }
739
+ }
740
+ };
741
+ return {
742
+ path: dbPath,
743
+ writable,
744
+ close: closeOnce,
745
+ exec: (sql) => {
746
+ assertUsable();
747
+ if (readDepth)
748
+ throw new AppError("read_snapshot_write", "Cannot execute SQL inside a read snapshot", 1);
749
+ try {
750
+ db.exec(sql);
751
+ applyManualBoundary(sql);
752
+ }
753
+ catch (error) {
754
+ if (/^\s*ROLLBACK\b/i.test(sql)) {
755
+ poisoned = true;
756
+ try {
757
+ closeOnce();
758
+ }
759
+ catch { /* Keep the primary SQLite failure. */ }
760
+ }
761
+ throw error;
762
+ }
763
+ finally {
764
+ if (closed || !db.isOpen || !db.isTransaction) {
765
+ reserved = false;
766
+ if (!/^\s*COMMIT\b/i.test(sql)) {
767
+ afterCommit = [];
768
+ manualFrames = [];
769
+ }
770
+ }
771
+ }
772
+ },
773
+ execSchema: (sql) => { assertUsable(); if (db.isTransaction && !reserved)
774
+ throw new AppError("write_transaction_unowned", "Cannot change schema inside an unowned transaction", 1); executeStoreSchema(db, sql); },
775
+ beginWriteTransaction,
776
+ writeTransaction,
777
+ readSnapshot,
778
+ afterCommit: (operation) => { assertUsable(); if (readDepth)
779
+ throw new AppError("read_snapshot_write", "Cannot queue publication inside a read snapshot", 1); if (db.isTransaction)
780
+ afterCommit.push(operation);
781
+ else {
782
+ const scope = publicationScopes.at(-1);
783
+ if (scope)
784
+ scope.push(operation);
785
+ else
786
+ executePublications([operation]);
787
+ } },
788
+ run: (sql, params = []) => { assertUsable(); if (readDepth)
789
+ throw new AppError("read_snapshot_write", "Cannot write inside a read snapshot", 1); return db.prepare(sql).run(...params); },
790
+ get: (sql, params = []) => { assertUsable(); return db.prepare(sql).get(...params); },
791
+ all: (sql, params = []) => { assertUsable(); return db.prepare(sql).all(...params); }
792
+ };
793
+ }
794
+ function errorDetails(error) {
795
+ const value = error;
796
+ return { message: String(value?.message ?? error), ...(typeof value?.code === "string" ? { code: value.code } : {}), ...(typeof value?.errcode === "number" ? { sqlite_extended_code: value.errcode } : {}) };
797
+ }
189
798
  function configureDatabase(db, mode) {
190
- db.exec("PRAGMA busy_timeout = 4000");
191
799
  if (mode !== "read_existing") {
192
- db.exec("PRAGMA journal_mode = WAL");
800
+ // Concurrent first openers can collide while changing journal mode, even
801
+ // with busy_timeout set. Retrying this configuration does not replay writes.
802
+ const deadline = performance.now() + 4000, pause = new Int32Array(new SharedArrayBuffer(4));
803
+ for (;;) {
804
+ try {
805
+ const current = db.prepare("PRAGMA journal_mode").get();
806
+ if (current.journal_mode !== "wal")
807
+ db.exec("PRAGMA journal_mode = WAL");
808
+ break;
809
+ }
810
+ catch (error) {
811
+ if ((error.errcode ?? 0) % 256 !== 5 || performance.now() >= deadline)
812
+ throw error;
813
+ Atomics.wait(pause, 0, 0, Math.min(25, Math.max(1, deadline - performance.now())));
814
+ }
815
+ }
193
816
  db.exec("PRAGMA synchronous = NORMAL");
194
817
  }
195
818
  }
196
- function migrate(db, dbPath) {
819
+ function migrate(db) {
197
820
  db.exec(`
198
821
  CREATE TABLE IF NOT EXISTS projects (
199
822
  id TEXT PRIMARY KEY,
@@ -229,6 +852,7 @@ function migrate(db, dbPath) {
229
852
  CREATE TABLE IF NOT EXISTS plan_bindings (
230
853
  project_id TEXT NOT NULL,
231
854
  protocol_id TEXT NOT NULL,
855
+ plan_id TEXT,
232
856
  plan_revision INTEGER NOT NULL,
233
857
  plan_sha256 TEXT NOT NULL,
234
858
  plan_path TEXT NOT NULL,
@@ -551,6 +1175,8 @@ function migrate(db, dbPath) {
551
1175
  CREATE INDEX IF NOT EXISTS idx_check_receipts_run
552
1176
  ON check_receipts(project_id, run_id, started_at);
553
1177
 
1178
+ ${workContinuationStorageSql}
1179
+
554
1180
  CREATE TABLE IF NOT EXISTS merge_requests (
555
1181
  merge_request_id TEXT PRIMARY KEY,
556
1182
  project_id TEXT NOT NULL,
@@ -574,6 +1200,7 @@ function migrate(db, dbPath) {
574
1200
  lock_acquired_at TEXT,
575
1201
  checkpoint TEXT NOT NULL DEFAULT 'queued',
576
1202
  profile_hash TEXT,
1203
+ replacement_of_merge_request_id TEXT,
577
1204
  adapter_receipt_json TEXT,
578
1205
  result_json TEXT,
579
1206
  last_error_json TEXT,
@@ -585,8 +1212,8 @@ function migrate(db, dbPath) {
585
1212
  );
586
1213
  CREATE INDEX IF NOT EXISTS idx_merge_requests_fifo
587
1214
  ON merge_requests(project_id, status, created_at, merge_request_id);
588
- CREATE UNIQUE INDEX IF NOT EXISTS idx_merge_requests_run
589
- ON merge_requests(project_id, run_id);
1215
+ CREATE INDEX IF NOT EXISTS idx_merge_requests_run
1216
+ ON merge_requests(project_id, run_id, created_at);
590
1217
  CREATE UNIQUE INDEX IF NOT EXISTS idx_merge_requests_active_project
591
1218
  ON merge_requests(project_id)
592
1219
  WHERE status IN ('active', 'waiting_user', 'action_required', 'recovery_required');
@@ -798,7 +1425,6 @@ function migrate(db, dbPath) {
798
1425
  ensureColumn(db, "sessions", "plan_item_id", "ALTER TABLE sessions ADD COLUMN plan_item_id TEXT");
799
1426
  ensureColumn(db, "sessions", "session_kind", "ALTER TABLE sessions ADD COLUMN session_kind TEXT");
800
1427
  ensureColumn(db, "sessions", "coverage_units_json", "ALTER TABLE sessions ADD COLUMN coverage_units_json TEXT NOT NULL DEFAULT '[]'");
801
- migrateSessionStorageKeys(db, dbPath);
802
1428
  ensureColumn(db, "usage", "cache_read_input_tokens", "ALTER TABLE usage ADD COLUMN cache_read_input_tokens INTEGER");
803
1429
  ensureColumn(db, "usage", "cache_write_input_tokens", "ALTER TABLE usage ADD COLUMN cache_write_input_tokens INTEGER");
804
1430
  ensureColumn(db, "usage", "uncached_input_tokens", "ALTER TABLE usage ADD COLUMN uncached_input_tokens INTEGER");
@@ -808,12 +1434,40 @@ function migrate(db, dbPath) {
808
1434
  ensureColumn(db, "usage", "source_mtime_ms", "ALTER TABLE usage ADD COLUMN source_mtime_ms INTEGER");
809
1435
  db.prepare("UPDATE usage SET cache_read_input_tokens = cached_input_tokens WHERE cache_read_input_tokens IS NULL AND cached_input_tokens IS NOT NULL").run();
810
1436
  ensureColumn(db, "runs", "run_home_path", "ALTER TABLE runs ADD COLUMN run_home_path TEXT");
1437
+ ensureColumn(db, "plan_bindings", "plan_id", "ALTER TABLE plan_bindings ADD COLUMN plan_id TEXT");
1438
+ // Historical RUN projections already retain the canonical identifier. Use
1439
+ // those SQL bytes to backfill without opening plan files under the writer.
1440
+ for (const row of db.prepare("SELECT project_id, protocol_id FROM plan_bindings WHERE plan_id IS NULL").all()) {
1441
+ const runs = db.prepare("SELECT index_json FROM runs WHERE project_id = ? AND subject_type = 'protocol' AND subject_id = ? ORDER BY updated_at DESC").all(row.project_id, row.protocol_id);
1442
+ for (const run of runs) {
1443
+ try {
1444
+ const planId = JSON.parse(run.index_json).plan_ref?.plan_id;
1445
+ if (typeof planId !== "string" || !planId || planId === "unknown-plan")
1446
+ continue;
1447
+ db.prepare("UPDATE plan_bindings SET plan_id = ? WHERE project_id = ? AND protocol_id = ? AND plan_id IS NULL").run(planId, row.project_id, row.protocol_id);
1448
+ break;
1449
+ }
1450
+ catch { /* A malformed historical RUN cannot establish plan identity. */ }
1451
+ }
1452
+ }
811
1453
  ensureColumn(db, "runs", "run_root", "ALTER TABLE runs ADD COLUMN run_root TEXT");
812
1454
  db.prepare("UPDATE runs SET run_root = COALESCE(run_root, run_home_path, run_dir) WHERE run_root IS NULL OR run_root = ''").run();
813
1455
  ensureColumn(db, "merge_requests", "accepted_tree", "ALTER TABLE merge_requests ADD COLUMN accepted_tree TEXT");
1456
+ ensureColumn(db, "merge_requests", "replacement_of_merge_request_id", "ALTER TABLE merge_requests ADD COLUMN replacement_of_merge_request_id TEXT");
1457
+ // A failed integration gate may create a new immutable source candidate in
1458
+ // the same RUN. The historical unique index made that repair path impossible.
1459
+ // Rebuilding an already-current index changes schema/data_version on every
1460
+ // CLI observation and prevents a controller's writer barrier from settling.
1461
+ const mergeRunIndex = db.prepare("PRAGMA index_list(merge_requests)").all().find(index => index.name === "idx_merge_requests_run");
1462
+ if (mergeRunIndex?.unique)
1463
+ db.exec("DROP INDEX idx_merge_requests_run");
1464
+ db.exec("CREATE INDEX IF NOT EXISTS idx_merge_requests_run ON merge_requests(project_id, run_id, created_at)");
814
1465
  ensureColumn(db, "runs", "layout_version", "ALTER TABLE runs ADD COLUMN layout_version TEXT");
815
1466
  ensureColumn(db, "runs", "artifact_root_kind", "ALTER TABLE runs ADD COLUMN artifact_root_kind TEXT");
816
1467
  ensureColumn(db, "works", "payload_json", "ALTER TABLE works ADD COLUMN payload_json TEXT");
1468
+ ensureColumn(db, "work_sessions", "start_receipt_json", "ALTER TABLE work_sessions ADD COLUMN start_receipt_json TEXT");
1469
+ ensureColumn(db, "work_sessions", "start_publication_status", "ALTER TABLE work_sessions ADD COLUMN start_publication_status TEXT");
1470
+ ensureColumn(db, "work_sessions", "result_receipt_json", "ALTER TABLE work_sessions ADD COLUMN result_receipt_json TEXT");
817
1471
  ensureColumn(db, "check_receipts", "declaration_id", "ALTER TABLE check_receipts ADD COLUMN declaration_id TEXT NOT NULL DEFAULT 'CHK-LEGACY'");
818
1472
  ensureColumn(db, "check_receipts", "workspace_fingerprint", "ALTER TABLE check_receipts ADD COLUMN workspace_fingerprint TEXT NOT NULL DEFAULT ''");
819
1473
  ensureColumn(db, "check_receipts", "artifacts_json", "ALTER TABLE check_receipts ADD COLUMN artifacts_json TEXT NOT NULL DEFAULT '[]'");
@@ -821,6 +1475,9 @@ function migrate(db, dbPath) {
821
1475
  ensureColumn(db, "check_receipts", "gate", "ALTER TABLE check_receipts ADD COLUMN gate TEXT NOT NULL DEFAULT 'work'");
822
1476
  ensureColumn(db, "check_receipts", "input_hash", "ALTER TABLE check_receipts ADD COLUMN input_hash TEXT NOT NULL DEFAULT ''");
823
1477
  ensureColumn(db, "check_receipts", "verification_epoch", "ALTER TABLE check_receipts ADD COLUMN verification_epoch TEXT NOT NULL DEFAULT ''");
1478
+ ensureColumn(db, "check_receipts", "reservation_sequence", "ALTER TABLE check_receipts ADD COLUMN reservation_sequence INTEGER NOT NULL DEFAULT 0");
1479
+ ensureColumn(db, "check_receipts", "retry_of_receipt_id", "ALTER TABLE check_receipts ADD COLUMN retry_of_receipt_id TEXT");
1480
+ ensureColumn(db, "check_receipts", "retry_reason", "ALTER TABLE check_receipts ADD COLUMN retry_reason TEXT");
824
1481
  ensureColumn(db, "check_receipts", "before_fingerprint", "ALTER TABLE check_receipts ADD COLUMN before_fingerprint TEXT NOT NULL DEFAULT ''");
825
1482
  ensureColumn(db, "check_receipts", "after_fingerprint", "ALTER TABLE check_receipts ADD COLUMN after_fingerprint TEXT NOT NULL DEFAULT ''");
826
1483
  ensureColumn(db, "check_receipts", "profile_hash", "ALTER TABLE check_receipts ADD COLUMN profile_hash TEXT");
@@ -840,6 +1497,7 @@ function migrate(db, dbPath) {
840
1497
  ensureColumn(db, "hook_events", "parent_session_id", "ALTER TABLE hook_events ADD COLUMN parent_session_id TEXT");
841
1498
  ensureColumn(db, "hook_events", "daemon_id", "ALTER TABLE hook_events ADD COLUMN daemon_id TEXT");
842
1499
  ensureColumn(db, "hook_events", "cwd", "ALTER TABLE hook_events ADD COLUMN cwd TEXT");
1500
+ ensureColumn(db, "hook_events", "outcome_json", "ALTER TABLE hook_events ADD COLUMN outcome_json TEXT");
843
1501
  ensureColumn(db, "harness_usage_snapshots", "tool_calls", "ALTER TABLE harness_usage_snapshots ADD COLUMN tool_calls INTEGER");
844
1502
  ensureColumn(db, "harness_usage_snapshots", "tool_failures", "ALTER TABLE harness_usage_snapshots ADD COLUMN tool_failures INTEGER");
845
1503
  ensureColumn(db, "harness_usage_snapshots", "tool_by_name_json", "ALTER TABLE harness_usage_snapshots ADD COLUMN tool_by_name_json TEXT");
@@ -902,8 +1560,7 @@ function migrateSessionStorageKeys(db, dbPath) {
902
1560
  db.exec("COMMIT");
903
1561
  }
904
1562
  catch (error) {
905
- db.exec("ROLLBACK");
906
- throw error;
1563
+ return rollbackStoreTransaction(db, "ROLLBACK", error);
907
1564
  }
908
1565
  }
909
1566
  function storageSessionKey(harness, native) {