@deksden-com/dd-flow-cli 0.9.0-beta.13 → 0.9.0-beta.134
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.
- package/CHANGELOG.md +687 -0
- package/README.md +101 -1
- package/dist/build-info.json +10 -10
- package/dist/cli/argument-values.js +38 -0
- package/dist/cli/command-inputs.js +306 -0
- package/dist/cli/help.js +88 -12
- package/dist/cli/hook-ingress.js +78 -0
- package/dist/cli/input-preparation.js +116 -0
- package/dist/cli/native-hook-ingress.js +80 -0
- package/dist/cli/run-cli.js +1465 -450
- package/dist/cli/runtime-maintenance-diagnostics.js +46 -0
- package/dist/cli/runtime-maintenance-ingress.js +52 -0
- package/dist/cli/runtime-process.js +153 -0
- package/dist/cli.js +17 -2
- package/dist/domain/stage-catalog.js +1 -1
- package/dist/harness-runtime/DD-ZCODE.md +99 -0
- package/dist/harness-runtime/bin/dd-agy.mjs +56 -0
- package/dist/harness-runtime/bin/dd-codex.mjs +27 -0
- package/dist/harness-runtime/bin/dd-droid.mjs +34 -0
- package/dist/harness-runtime/bin/dd-grok.mjs +40 -0
- package/dist/harness-runtime/bin/dd-opencode.mjs +22 -0
- package/dist/harness-runtime/bin/dd-zcode.mjs +87 -0
- package/dist/harness-runtime/lib/adapter-timeouts.d.mts +5 -0
- package/dist/harness-runtime/lib/adapter-timeouts.mjs +34 -0
- package/dist/harness-runtime/lib/agy-hooks.d.mts +1 -0
- package/dist/harness-runtime/lib/agy-hooks.mjs +9 -0
- package/dist/harness-runtime/lib/codex-capacity-policy.d.mts +10 -0
- package/dist/harness-runtime/lib/codex-capacity-policy.mjs +89 -0
- package/dist/harness-runtime/lib/daemon-observation.d.mts +2 -0
- package/dist/harness-runtime/lib/daemon-observation.mjs +163 -0
- package/dist/harness-runtime/lib/daemon-operations.mjs +370 -0
- package/dist/harness-runtime/lib/dd-agy-daemon.d.mts +6 -0
- package/dist/harness-runtime/lib/dd-agy-daemon.mjs +693 -0
- package/dist/harness-runtime/lib/dd-agy.mjs +59 -0
- package/dist/harness-runtime/lib/dd-codex-daemon.d.mts +1 -0
- package/dist/harness-runtime/lib/dd-codex-daemon.mjs +190 -0
- package/dist/harness-runtime/lib/dd-codex.d.mts +1 -0
- package/dist/harness-runtime/lib/dd-codex.mjs +805 -0
- package/dist/harness-runtime/lib/dd-droid-daemon.mjs +128 -0
- package/dist/harness-runtime/lib/dd-droid.mjs +561 -0
- package/dist/harness-runtime/lib/dd-grok-daemon.mjs +490 -0
- package/dist/harness-runtime/lib/dd-grok.mjs +177 -0
- package/dist/harness-runtime/lib/dd-opencode-daemon.mjs +255 -0
- package/dist/harness-runtime/lib/dd-opencode.mjs +128 -0
- package/dist/harness-runtime/lib/dd-zcode-daemon.mjs +703 -0
- package/dist/harness-runtime/lib/dd-zcode.mjs +1128 -0
- package/dist/harness-runtime/lib/delegation-instructions.d.mts +13 -0
- package/dist/harness-runtime/lib/delegation-instructions.mjs +144 -0
- package/dist/harness-runtime/lib/dispatch-fence.mjs +14 -0
- package/dist/harness-runtime/lib/driver-recovery.mjs +139 -0
- package/dist/harness-runtime/lib/droid-observation.mjs +66 -0
- package/dist/harness-runtime/lib/lease-renewal.d.mts +8 -0
- package/dist/harness-runtime/lib/lease-renewal.mjs +36 -0
- package/dist/harness-runtime/lib/lifecycle-command.d.mts +20 -0
- package/dist/harness-runtime/lib/lifecycle-command.mjs +272 -0
- package/dist/harness-runtime/lib/lifecycle-contract.d.mts +10 -0
- package/dist/harness-runtime/lib/lifecycle-contract.mjs +19 -0
- package/dist/harness-runtime/lib/managed-daemon.d.mts +12 -0
- package/dist/harness-runtime/lib/managed-daemon.mjs +619 -0
- package/dist/harness-runtime/lib/model-observations.mjs +101 -0
- package/dist/harness-runtime/lib/native-children.d.mts +14 -0
- package/dist/harness-runtime/lib/native-children.mjs +94 -0
- package/dist/harness-runtime/lib/native-hook-command.d.mts +7 -0
- package/dist/harness-runtime/lib/native-hook-command.mjs +251 -0
- package/dist/harness-runtime/lib/native-streams.mjs +45 -0
- package/dist/harness-runtime/lib/observation-clock.d.mts +18 -0
- package/dist/harness-runtime/lib/observation-clock.mjs +92 -0
- package/dist/harness-runtime/lib/operation-context.mjs +5 -0
- package/dist/harness-runtime/lib/operation-errors.d.mts +14 -0
- package/dist/harness-runtime/lib/operation-errors.mjs +116 -0
- package/dist/harness-runtime/lib/operation-progress.d.mts +7 -0
- package/dist/harness-runtime/lib/operation-progress.mjs +112 -0
- package/dist/harness-runtime/lib/process-json.d.mts +5 -0
- package/dist/harness-runtime/lib/process-json.mjs +201 -0
- package/dist/harness-runtime/lib/process-snapshot.mjs +10 -0
- package/dist/harness-runtime/lib/runner-events.mjs +200 -0
- package/dist/harness-runtime/lib/runner-lock.mjs +42 -0
- package/dist/harness-runtime/lib/session-settlement.mjs +30 -0
- package/dist/harness-runtime/lib/setup-progress.d.mts +9 -0
- package/dist/harness-runtime/lib/setup-progress.mjs +51 -0
- package/dist/harness-runtime/lib/tool-observations.d.mts +21 -0
- package/dist/harness-runtime/lib/tool-observations.mjs +228 -0
- package/dist/harness-runtime/lib/zcode-usage.mjs +55 -0
- package/dist/protocol/local-files.js +18 -9
- package/dist/runtime/context.js +8 -4
- package/dist/schemas/agent-profile.schema.json +1 -1
- package/dist/schemas/code-review-result.schema.json +1 -1
- package/dist/schemas/code-verification.schema.json +7 -5
- package/dist/schemas/code-work-batch.schema.json +2 -2
- package/dist/schemas/flow-run.schema.json +2 -0
- package/dist/schemas/plan-review-decision.schema.json +3 -3
- package/dist/schemas/plan-review-result.schema.json +2 -2
- package/dist/schemas/run-control-receipt.schema.json +118 -0
- package/dist/schemas/run-control-request.schema.json +36 -0
- package/dist/schemas/vnext-protocol-plan.schema.json +2 -2
- package/dist/services/branch-context.js +3 -3
- package/dist/services/canon.js +11 -8
- package/dist/services/check-runner-evidence.js +121 -0
- package/dist/services/cleanup.js +133 -40
- package/dist/services/cli-operation-classifier.js +22 -24
- package/dist/services/code-checks.js +851 -109
- package/dist/services/codex-capacity.js +367 -0
- package/dist/services/codex-hook-delivery.js +32 -0
- package/dist/services/command-context.js +80 -0
- package/dist/services/compatibility-preflight.js +13 -3
- package/dist/services/config.js +11 -8
- package/dist/services/continuation-outcome.js +20 -0
- package/dist/services/controller-fanout.js +136 -0
- package/dist/services/dashboard.js +135 -56
- package/dist/services/engines.js +245 -119
- package/dist/services/eval-snapshots.js +1146 -105
- package/dist/services/execution-policy.js +181 -0
- package/dist/services/external-work-launch.js +230 -0
- package/dist/services/harness-adapter.js +329 -36
- package/dist/services/harness-config.js +2 -0
- package/dist/services/hooks.js +753 -325
- package/dist/services/lanes.js +78 -65
- package/dist/services/lifecycle-command.js +1 -288
- package/dist/services/lifecycle-contract.js +1 -0
- package/dist/services/lifecycle-invocations.js +1828 -0
- package/dist/services/managed-daemon-binding.js +38 -0
- package/dist/services/managed-lease-monitor.js +133 -0
- package/dist/services/managed-processes.js +409 -51
- package/dist/services/merge-queue.js +228 -149
- package/dist/services/merge-server.js +163 -35
- package/dist/services/migrations.js +37 -16
- package/dist/services/native-daemon-history.js +49 -0
- package/dist/services/native-session-control.js +39 -0
- package/dist/services/plan-runtime.js +26 -11
- package/dist/services/plans.js +28 -25
- package/dist/services/projects.js +20 -15
- package/dist/services/prompts.js +28 -17
- package/dist/services/protocols.js +163 -87
- package/dist/services/recovery-observation-budget.js +121 -0
- package/dist/services/recovery-snapshot-database.js +145 -0
- package/dist/services/repair-intents.js +42 -0
- package/dist/services/review-copy.js +157 -0
- package/dist/services/review-deferral.js +23 -0
- package/dist/services/run-control-receipt.js +56 -0
- package/dist/services/run-control-worker.js +411 -0
- package/dist/services/run-control.js +985 -0
- package/dist/services/run-controller-adapter.js +399 -0
- package/dist/services/run-controller-capture.js +179 -0
- package/dist/services/run-controller-process.js +187 -0
- package/dist/services/run-controller-recovery.js +289 -0
- package/dist/services/run-controller-state.js +97 -0
- package/dist/services/run-controller.js +1035 -0
- package/dist/services/run-engine-bindings.js +1 -1
- package/dist/services/run-fork.js +147 -0
- package/dist/services/run-observations.js +152 -0
- package/dist/services/run-projection.js +22 -64
- package/dist/services/run-recovery-guard.js +75 -0
- package/dist/services/run-recovery-runtime.js +112 -0
- package/dist/services/run-recovery.js +349 -0
- package/dist/services/runs.js +558 -217
- package/dist/services/runtime-budget.js +430 -0
- package/dist/services/runtime-command.js +43 -0
- package/dist/services/runtime-scope-capture.js +52 -0
- package/dist/services/runtime-scope-control.js +468 -0
- package/dist/services/runtime-scope-resume.js +599 -0
- package/dist/services/runtime-scope-stop.js +180 -0
- package/dist/services/runtime-scope-worker.js +287 -0
- package/dist/services/runtime-service.js +96 -0
- package/dist/services/schema-validation.js +10 -8
- package/dist/services/scope-writer-cleanup.js +27 -0
- package/dist/services/sessions.js +35 -69
- package/dist/services/stage-blocker.js +17 -7
- package/dist/services/stage-context.js +185 -19
- package/dist/services/stage-lifecycle.js +98 -107
- package/dist/services/stage-pause.js +89 -75
- package/dist/services/stage-report-renderer.js +158 -6
- package/dist/services/stage-work-graph.js +35 -0
- package/dist/services/tooling.js +32 -23
- package/dist/services/usage.js +49 -101
- package/dist/services/verification-matrix.js +374 -0
- package/dist/services/vnext-code-review.js +222 -87
- package/dist/services/vnext-code.js +324 -251
- package/dist/services/vnext-contracts.js +10 -1
- package/dist/services/vnext-execution-profile.js +79 -4
- package/dist/services/vnext-fanout.js +182 -17
- package/dist/services/vnext-merge.js +335 -113
- package/dist/services/vnext-plan-review.js +276 -126
- package/dist/services/vnext-plan.js +227 -102
- package/dist/services/vnext-protocolize.js +335 -82
- package/dist/services/vnext-specify.js +119 -81
- package/dist/services/work-registry.js +1083 -194
- package/dist/services/workspace-bootstrap.js +87 -0
- package/dist/services/workspace-files.js +85 -0
- package/dist/services/worktrees.js +98 -44
- package/dist/shared/entity-references.js +8 -0
- package/dist/shared/errors.js +17 -0
- package/dist/storage/agent-output.js +26 -0
- package/dist/storage/database.js +785 -37
- package/dist/storage/foreign-key-transaction.js +39 -0
- package/dist/storage/immutable-input.js +45 -0
- package/dist/storage/import-guard.js +102 -0
- package/dist/storage/native-cleanup.js +52 -0
- package/dist/storage/paths.js +17 -4
- package/dist/storage/projection-lock.js +73 -0
- package/dist/storage/work-references.js +23 -0
- package/dist/storage/writer-contract.js +77 -0
- package/dist/storage/writer-migration.js +85 -0
- package/package.json +30 -13
- package/tools/repair-paused-run-status.mjs +84 -0
package/dist/storage/database.js
CHANGED
|
@@ -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, STORE_WRITER_VERSION } 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.
|
|
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,235 @@ 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}
|
|
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 workColumns = db.all("PRAGMA table_info(works)");
|
|
305
|
+
if (!workColumns.some(column => column.name === "current_start_invocation_id"))
|
|
306
|
+
db.execSchema("ALTER TABLE works ADD COLUMN current_start_invocation_id TEXT");
|
|
307
|
+
const columns = db.all("PRAGMA table_info(work_sessions)");
|
|
308
|
+
if (!columns.some((column) => column.name === "start_receipt_json"))
|
|
309
|
+
db.execSchema("ALTER TABLE work_sessions ADD COLUMN start_receipt_json TEXT");
|
|
310
|
+
if (!columns.some((column) => column.name === "start_publication_status"))
|
|
311
|
+
db.execSchema("ALTER TABLE work_sessions ADD COLUMN start_publication_status TEXT");
|
|
312
|
+
if (!columns.some((column) => column.name === "result_receipt_json"))
|
|
313
|
+
db.execSchema("ALTER TABLE work_sessions ADD COLUMN result_receipt_json TEXT");
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
/** Durable intent for a provider-root external Work. It is deliberately
|
|
317
|
+
* separate from `work_sessions`: only a trusted hook may bind that Session. */
|
|
318
|
+
export function ensureWorkLaunchStorage(db) {
|
|
319
|
+
db.execSchema(`
|
|
320
|
+
CREATE TABLE IF NOT EXISTS work_launches (
|
|
321
|
+
operation_id TEXT PRIMARY KEY,
|
|
322
|
+
work_id TEXT NOT NULL UNIQUE,
|
|
323
|
+
profile_json TEXT NOT NULL,
|
|
324
|
+
profile_hash TEXT NOT NULL,
|
|
325
|
+
status TEXT NOT NULL,
|
|
326
|
+
state_dir TEXT NOT NULL,
|
|
327
|
+
journal_path TEXT NOT NULL,
|
|
328
|
+
provider_session_id TEXT,
|
|
329
|
+
adapter_receipt_json TEXT,
|
|
330
|
+
created_at TEXT NOT NULL,
|
|
331
|
+
updated_at TEXT NOT NULL,
|
|
332
|
+
FOREIGN KEY(work_id) REFERENCES works(work_id)
|
|
333
|
+
);
|
|
334
|
+
CREATE INDEX IF NOT EXISTS idx_work_launches_status ON work_launches(status, updated_at DESC);
|
|
84
335
|
`);
|
|
85
336
|
}
|
|
86
|
-
export function getDatabase(ddFlowHome, mode = "initialize") {
|
|
337
|
+
export function getDatabase(ddFlowHome, mode = "initialize", importPermitId, hookDeadlineAt) {
|
|
338
|
+
channel("dd-flow.sqlite.ingress").publish({ phase: "store_open", store_kind: "run" });
|
|
339
|
+
if (hookDeadlineAt !== undefined && !Number.isSafeInteger(hookDeadlineAt))
|
|
340
|
+
throw new AppError("hook_deadline_invalid", "Deadline must be a finite absolute millisecond deadline", 1, { effect: "unknown" });
|
|
341
|
+
const deadline = hookDeadlineAt === undefined ? undefined : (phase) => {
|
|
342
|
+
const remaining = hookDeadlineAt - Date.now();
|
|
343
|
+
if (remaining <= 0)
|
|
344
|
+
throw new AppError("native_hook_timeout", "Native hook deadline expired before durable admission", 1, { phase, deadline_at: hookDeadlineAt, effect: "unknown" });
|
|
345
|
+
return Math.max(1, Math.min(4000, remaining));
|
|
346
|
+
};
|
|
347
|
+
deadline?.("store_open");
|
|
348
|
+
if (mode !== "read_existing")
|
|
349
|
+
assertImportAdmission(ddFlowHome, importPermitId);
|
|
87
350
|
const dbPath = path.join(ddFlowHome, "db.sqlite");
|
|
88
351
|
const exists = fs.existsSync(dbPath);
|
|
89
352
|
if (mode === "read_existing" && !exists) {
|
|
90
353
|
return emptyReadOnlyDatabase(dbPath);
|
|
91
354
|
}
|
|
355
|
+
if (mode === "hook" && !exists)
|
|
356
|
+
throw new AppError("hook_storage_unprepared", "Prepare the runtime store before starting native hooks", 1);
|
|
92
357
|
if (!exists || mode === "migrate")
|
|
93
358
|
ensureDir(ddFlowHome);
|
|
94
359
|
const db = mode === "read_existing" ? new DatabaseSync(dbPath, { readOnly: true }) : new DatabaseSync(dbPath);
|
|
95
|
-
|
|
360
|
+
try {
|
|
361
|
+
db.exec(`PRAGMA busy_timeout = ${deadline?.("store_open") ?? 4000}`);
|
|
362
|
+
}
|
|
363
|
+
catch (error) {
|
|
364
|
+
return closeAfterOpenFailure(db, error);
|
|
365
|
+
}
|
|
366
|
+
if (mode !== "read_existing") {
|
|
367
|
+
try {
|
|
368
|
+
registerStoreWriter(db);
|
|
369
|
+
}
|
|
370
|
+
catch (error) {
|
|
371
|
+
return closeAfterOpenFailure(db, error);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
try {
|
|
375
|
+
configureDatabase(db, mode, deadline);
|
|
376
|
+
}
|
|
377
|
+
catch (error) {
|
|
378
|
+
return closeAfterOpenFailure(db, error);
|
|
379
|
+
}
|
|
96
380
|
// Internal additive schema changes must be available to every write command.
|
|
97
381
|
// Higher-level Memory Bank migrations remain explicit in services/migrations.
|
|
98
|
-
if (mode !== "read_existing")
|
|
99
|
-
|
|
382
|
+
if (mode !== "read_existing") {
|
|
383
|
+
try {
|
|
384
|
+
const initialize = () => { migrate(db); ensureRecoveryStorage(db); ensureLifecycleInvocationStorage(db); };
|
|
385
|
+
if (mode !== "hook") {
|
|
386
|
+
const fresh = !db.prepare("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%' LIMIT 1").get();
|
|
387
|
+
if (fresh || readStoreWriterContract(db) !== null)
|
|
388
|
+
executeStoreSchema(db, initialize, fresh);
|
|
389
|
+
else
|
|
390
|
+
initialize();
|
|
391
|
+
// This migration owns a VACUUM backup and a separate data transaction.
|
|
392
|
+
migrateSessionStorageKeys(db, dbPath);
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
deadline?.("store_validation");
|
|
396
|
+
assertHookStoreReady(db);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
catch (error) {
|
|
400
|
+
return closeAfterOpenFailure(db, error);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
100
403
|
if (mode === "read_existing")
|
|
101
404
|
db.exec("PRAGMA query_only = ON");
|
|
102
405
|
db.exec("PRAGMA foreign_keys = ON");
|
|
103
|
-
return
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
406
|
+
return databaseFacade(dbPath, mode !== "read_existing", db, undefined, deadline);
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Hook ingress is a hot, synchronous path. It may write receipts, but it
|
|
410
|
+
* must never perform schema migration or VACUUM while the native host waits.
|
|
411
|
+
* Startup/migration owns this contract; a missing table is an explicit
|
|
412
|
+
* preparation error instead of an implicit migration race.
|
|
413
|
+
*/
|
|
414
|
+
function assertHookStoreReady(db) {
|
|
415
|
+
const required = ["projects", "hook_events", "codex_session_bindings", "sessions", "flow_session_segments", "lifecycle_invocations", "lifecycle_packet_assignments"];
|
|
416
|
+
const missing = required.filter((table) => !db.prepare("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = ?").get(table));
|
|
417
|
+
if (missing.length)
|
|
418
|
+
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." });
|
|
419
|
+
// Table presence alone does not establish compatibility with an older home.
|
|
420
|
+
const receiptColumns = {
|
|
421
|
+
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",
|
|
422
|
+
lifecycle_invocations: "id, scope_json, command, fingerprint, status, event_key, identity_json, deadline, outcome_json, created_at, updated_at",
|
|
423
|
+
codex_session_bindings: "session_id, project_id, protocol_id, handshake_id, cwd, transcript_path, status, created_at, updated_at"
|
|
111
424
|
};
|
|
425
|
+
for (const [table, columns] of Object.entries(receiptColumns)) {
|
|
426
|
+
try {
|
|
427
|
+
db.prepare(`SELECT ${columns} FROM ${table} LIMIT 0`);
|
|
428
|
+
}
|
|
429
|
+
catch {
|
|
430
|
+
throw new AppError("hook_storage_unprepared", "Managed receipt schema needs preparation before native hooks start", 1, { table });
|
|
431
|
+
}
|
|
432
|
+
}
|
|
112
433
|
}
|
|
113
434
|
/**
|
|
114
435
|
* Opens the small host-wide resource registry. It is intentionally separate
|
|
115
436
|
* from a RUN database: a process or a port can outlive the CLI client that
|
|
116
437
|
* created it, while a RUN database belongs to one flow home.
|
|
117
438
|
*/
|
|
118
|
-
export function
|
|
119
|
-
ensureDir(resourceHome);
|
|
439
|
+
export function withResourceReadSnapshot(resourceHome, read) {
|
|
120
440
|
const dbPath = path.join(resourceHome, "runtime.sqlite");
|
|
121
441
|
const cached = resourceDatabases.get(dbPath);
|
|
442
|
+
if (!cached && !fs.existsSync(dbPath))
|
|
443
|
+
return read(emptyReadOnlyDatabase(dbPath));
|
|
444
|
+
const db = cached ?? new DatabaseSync(dbPath, { readOnly: true });
|
|
445
|
+
const operation = () => {
|
|
446
|
+
if (!cached)
|
|
447
|
+
db.exec("PRAGMA busy_timeout = 4000; PRAGMA query_only = ON");
|
|
448
|
+
// A second process can observe SQLite's file before the initializing
|
|
449
|
+
// transaction publishes its schema. Treat that pre-commit view exactly
|
|
450
|
+
// like an absent registry; the writer path remains the serialization
|
|
451
|
+
// boundary and will reopen the committed schema.
|
|
452
|
+
if (!cached && !db.prepare("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%' LIMIT 1").get()) {
|
|
453
|
+
return read(emptyReadOnlyDatabase(dbPath));
|
|
454
|
+
}
|
|
455
|
+
const reader = cached
|
|
456
|
+
? { ...emptyReadOnlyDatabase(dbPath), get: cached.get, all: cached.all }
|
|
457
|
+
: databaseFacade(dbPath, false, db);
|
|
458
|
+
// The facade owns savepoint cleanup and poisons a connection when rollback
|
|
459
|
+
// fails; the callback only receives the read-only view.
|
|
460
|
+
return (cached ?? reader).readSnapshot(() => read(reader));
|
|
461
|
+
};
|
|
122
462
|
if (cached)
|
|
123
|
-
return
|
|
463
|
+
return operation();
|
|
464
|
+
const native = db;
|
|
465
|
+
return withStoreCleanup(operation, () => { if (native.isOpen)
|
|
466
|
+
native.close(); });
|
|
467
|
+
}
|
|
468
|
+
export function getResourceDatabase(resourceHome, mode = "initialize", deadlineAt) {
|
|
469
|
+
channel("dd-flow.sqlite.ingress").publish({ phase: "store_open", store_kind: "resource" });
|
|
470
|
+
const deadline = requestDeadline(deadlineAt);
|
|
471
|
+
deadline?.("resource_open");
|
|
472
|
+
const dbPath = path.join(resourceHome, "runtime.sqlite");
|
|
473
|
+
const cached = resourceDatabases.get(dbPath);
|
|
474
|
+
if (mode === "read_existing" || mode === "read_isolated") {
|
|
475
|
+
// Reuse a current transaction's view without exposing its write capability.
|
|
476
|
+
if (cached && mode === "read_existing") {
|
|
477
|
+
const bounded = deadlineAt === undefined ? cached : cached.withDeadline(deadlineAt);
|
|
478
|
+
return { ...emptyReadOnlyDatabase(dbPath), get: bounded.get, all: bounded.all };
|
|
479
|
+
}
|
|
480
|
+
if (!fs.existsSync(dbPath))
|
|
481
|
+
return emptyReadOnlyDatabase(dbPath);
|
|
482
|
+
const db = new DatabaseSync(dbPath, { readOnly: true });
|
|
483
|
+
try {
|
|
484
|
+
db.exec(`PRAGMA busy_timeout = ${deadline?.("resource_open") ?? 4000}; PRAGMA query_only = ON`);
|
|
485
|
+
if (!db.prepare("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%' LIMIT 1").get()) {
|
|
486
|
+
db.close();
|
|
487
|
+
return emptyReadOnlyDatabase(dbPath);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
catch (error) {
|
|
491
|
+
return closeAfterOpenFailure(db, error);
|
|
492
|
+
}
|
|
493
|
+
return databaseFacade(dbPath, false, db, undefined, deadline);
|
|
494
|
+
}
|
|
495
|
+
if (mode === "prepared_existing" && !fs.existsSync(dbPath))
|
|
496
|
+
throw new AppError("resource_storage_unprepared", "Prepare the resource registry before maintenance", 1);
|
|
497
|
+
if (cached) {
|
|
498
|
+
const bounded = deadlineAt === undefined ? cached : cached.withDeadline(deadlineAt);
|
|
499
|
+
if (mode === "prepared_existing")
|
|
500
|
+
assertResourceStoreReady({ prepare: sql => ({ get: (...params) => bounded.get(sql, params), all: (...params) => bounded.all(sql, params), run: (...params) => bounded.run(sql, params) }) });
|
|
501
|
+
return bounded;
|
|
502
|
+
}
|
|
503
|
+
if (mode !== "prepared_existing")
|
|
504
|
+
ensureDir(resourceHome);
|
|
124
505
|
const db = new DatabaseSync(dbPath);
|
|
125
|
-
|
|
506
|
+
db.exec(`PRAGMA busy_timeout = ${deadline?.("resource_open") ?? 4000}`);
|
|
507
|
+
try {
|
|
508
|
+
registerStoreWriter(db);
|
|
509
|
+
}
|
|
510
|
+
catch (error) {
|
|
511
|
+
return closeAfterOpenFailure(db, error);
|
|
512
|
+
}
|
|
513
|
+
channel("dd-flow.sqlite.ingress").publish({ phase: "store_validation", store_kind: "resource" });
|
|
514
|
+
try {
|
|
515
|
+
if (mode === "prepared_existing")
|
|
516
|
+
assertResourceStoreReady(db);
|
|
517
|
+
else
|
|
518
|
+
configureDatabase(db, "initialize", deadline);
|
|
519
|
+
}
|
|
520
|
+
catch (error) {
|
|
521
|
+
return closeAfterOpenFailure(db, error);
|
|
522
|
+
}
|
|
126
523
|
db.exec("PRAGMA foreign_keys = ON");
|
|
127
|
-
db.
|
|
524
|
+
const fresh = !db.prepare("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%' LIMIT 1").get();
|
|
525
|
+
try {
|
|
526
|
+
if (mode !== "prepared_existing")
|
|
527
|
+
executeStoreSchema(db, `
|
|
128
528
|
CREATE TABLE IF NOT EXISTS managed_processes (
|
|
129
529
|
id TEXT PRIMARY KEY,
|
|
130
530
|
kind TEXT NOT NULL,
|
|
@@ -163,37 +563,356 @@ export function getResourceDatabase(resourceHome) {
|
|
|
163
563
|
);
|
|
164
564
|
CREATE INDEX IF NOT EXISTS idx_managed_resources_lease
|
|
165
565
|
ON managed_resources(lease_expires_at, updated_at);
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
};
|
|
566
|
+
`, fresh);
|
|
567
|
+
}
|
|
568
|
+
catch (error) {
|
|
569
|
+
return closeAfterOpenFailure(db, error);
|
|
570
|
+
}
|
|
571
|
+
const database = databaseFacade(dbPath, true, db, () => {
|
|
572
|
+
resourceDatabases.delete(dbPath);
|
|
573
|
+
db.close?.();
|
|
574
|
+
});
|
|
176
575
|
resourceDatabases.set(dbPath, database);
|
|
177
|
-
return database;
|
|
576
|
+
return deadlineAt === undefined ? database : database.withDeadline(deadlineAt);
|
|
577
|
+
}
|
|
578
|
+
function requestDeadline(at) {
|
|
579
|
+
if (at === undefined)
|
|
580
|
+
return undefined;
|
|
581
|
+
if (!Number.isSafeInteger(at))
|
|
582
|
+
throw new AppError("maintenance_deadline_invalid", "Maintenance deadline must be an absolute millisecond timestamp", 1);
|
|
583
|
+
return phase => { const remaining = at - Date.now(); if (remaining <= 0)
|
|
584
|
+
throw new AppError("process_heartbeat_timeout", "Maintenance storage deadline expired", 1, { phase, deadline_at: at, effect: "unknown" }); return Math.max(1, Math.min(4000, remaining)); };
|
|
585
|
+
}
|
|
586
|
+
function assertResourceStoreReady(db) {
|
|
587
|
+
const version = readStoreWriterContract(db);
|
|
588
|
+
if (version === null)
|
|
589
|
+
throw new AppError("resource_storage_unprepared", "Prepare the resource registry writer contract before maintenance", 1);
|
|
590
|
+
if (version !== STORE_WRITER_VERSION)
|
|
591
|
+
throw new AppError("store_writer_incompatible", "This CLI cannot maintain the resource registry's required contract", 1, { required: version, supported: STORE_WRITER_VERSION });
|
|
592
|
+
for (const [table, columns] of Object.entries({ managed_processes: "id,kind,pid,pid_started_at,owner_id,lease_token,lease_expires_at,project_id,run_id,work_id,check_id,operation_id,stdout_path,stderr_path,state,started_at,updated_at,finished_at,termination_reason,metadata_json", managed_resources: "resource_kind,resource_key,owner_id,lease_token,lease_expires_at,process_id,metadata_json,created_at,updated_at" })) {
|
|
593
|
+
try {
|
|
594
|
+
db.prepare(`SELECT ${columns} FROM ${table} LIMIT 0`);
|
|
595
|
+
}
|
|
596
|
+
catch {
|
|
597
|
+
throw new AppError("resource_storage_unprepared", "Prepare the resource registry schema before maintenance", 1, { table });
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
for (const name of ["idx_managed_processes_lease", "idx_managed_resources_lease"])
|
|
601
|
+
if (!db.prepare("SELECT 1 FROM sqlite_schema WHERE type='index' AND name=?").get(name))
|
|
602
|
+
throw new AppError("resource_storage_unprepared", "Prepare resource registry indexes before maintenance", 1, { index: name });
|
|
178
603
|
}
|
|
179
604
|
function emptyReadOnlyDatabase(dbPath) {
|
|
180
605
|
return {
|
|
181
606
|
path: dbPath,
|
|
182
607
|
writable: false,
|
|
183
608
|
exec: () => { throw new AppError("read_only_storage", "Read-only access cannot write absent dd-flow storage", 1, { path: dbPath }); },
|
|
609
|
+
execSchema: () => { throw new AppError("read_only_storage", "Read-only access cannot write absent dd-flow storage", 1, { path: dbPath }); },
|
|
610
|
+
beginWriteTransaction: () => { throw new AppError("read_only_storage", "Read-only access cannot start a write transaction", 1, { path: dbPath }); },
|
|
611
|
+
writeTransaction: () => { throw new AppError("read_only_storage", "Read-only access cannot start a write transaction", 1, { path: dbPath }); },
|
|
612
|
+
readSnapshot: (operation) => operation(),
|
|
613
|
+
afterCommit: () => { throw new AppError("read_only_storage", "Read-only access cannot publish absent dd-flow storage", 1, { path: dbPath }); },
|
|
184
614
|
run: () => { throw new AppError("read_only_storage", "Read-only access cannot write absent dd-flow storage", 1, { path: dbPath }); },
|
|
185
615
|
get: () => undefined,
|
|
186
616
|
all: () => []
|
|
187
617
|
};
|
|
188
618
|
}
|
|
189
|
-
function
|
|
190
|
-
|
|
619
|
+
function databaseFacade(dbPath, writable, db, close = () => db.close?.(), deadline) {
|
|
620
|
+
let scopedDeadline = deadline;
|
|
621
|
+
let reserved = false;
|
|
622
|
+
let poisoned = false;
|
|
623
|
+
let closed = false;
|
|
624
|
+
const closeOnce = () => { if (closed || !db.isOpen) {
|
|
625
|
+
closed = true;
|
|
626
|
+
return;
|
|
627
|
+
} closed = true; close(); };
|
|
628
|
+
let savepoint = 0;
|
|
629
|
+
let afterCommit = [];
|
|
630
|
+
let manualFrames = [];
|
|
631
|
+
let readDepth = 0;
|
|
632
|
+
const writerTiming = channel("dd-flow.sqlite.writer");
|
|
633
|
+
const ingressPhase = channel("dd-flow.sqlite.ingress");
|
|
634
|
+
const storeKind = path.basename(dbPath) === "runtime.sqlite" ? "resource" : "run";
|
|
635
|
+
const bound = (phase) => {
|
|
636
|
+
const remaining = scopedDeadline?.(phase);
|
|
637
|
+
if (remaining !== undefined)
|
|
638
|
+
db.exec(`PRAGMA busy_timeout = ${remaining}`);
|
|
639
|
+
if (ingressPhase.hasSubscribers)
|
|
640
|
+
ingressPhase.publish({ phase, store_kind: storeKind, ...(remaining !== undefined ? { remaining_ms: remaining } : {}) });
|
|
641
|
+
};
|
|
642
|
+
const publishCommitted = () => {
|
|
643
|
+
const pending = afterCommit;
|
|
644
|
+
afterCommit = [];
|
|
645
|
+
const scope = publicationScopes.at(-1);
|
|
646
|
+
if (scope)
|
|
647
|
+
scope.push(...pending);
|
|
648
|
+
else
|
|
649
|
+
executePublications(pending);
|
|
650
|
+
};
|
|
651
|
+
const assertUsable = () => { if (poisoned || closed || !db.isOpen)
|
|
652
|
+
throw new AppError("write_transaction_unresolved", "Transaction cleanup failed or the connection is closed; reopen the connection before continuing", 1); };
|
|
653
|
+
const beginWriteTransaction = () => {
|
|
654
|
+
assertUsable();
|
|
655
|
+
if (readDepth)
|
|
656
|
+
throw new AppError("read_snapshot_write", "Cannot reserve a writer inside a read snapshot", 1);
|
|
657
|
+
if (!writable)
|
|
658
|
+
throw new AppError("read_only_storage", "Read-only access cannot start a write transaction", 1, { path: dbPath });
|
|
659
|
+
bound("writer_reservation");
|
|
660
|
+
db.exec("BEGIN IMMEDIATE");
|
|
661
|
+
reserved = true;
|
|
662
|
+
if (ingressPhase.hasSubscribers)
|
|
663
|
+
ingressPhase.publish({ phase: "writer_reserved", store_kind: storeKind });
|
|
664
|
+
};
|
|
665
|
+
const rollback = (sql, original) => {
|
|
666
|
+
try {
|
|
667
|
+
db.exec(sql);
|
|
668
|
+
}
|
|
669
|
+
catch (cleanup) {
|
|
670
|
+
poisoned = true;
|
|
671
|
+
let closeError;
|
|
672
|
+
try {
|
|
673
|
+
closeOnce();
|
|
674
|
+
}
|
|
675
|
+
catch (error) {
|
|
676
|
+
closeError = errorDetails(error);
|
|
677
|
+
}
|
|
678
|
+
const secondary = { transaction_cleanup_error: errorDetails(cleanup), ...(closeError ? { transaction_close_error: closeError } : {}) };
|
|
679
|
+
if (original instanceof AppError)
|
|
680
|
+
throw new AppError(original.code, original.message, original.exitCode, { ...original.details, ...secondary });
|
|
681
|
+
throw new AppError("write_transaction_failed", "Write transaction failed and SQLite cleanup also failed", 1, { cause: errorDetails(original), ...secondary });
|
|
682
|
+
}
|
|
683
|
+
throw original;
|
|
684
|
+
};
|
|
685
|
+
const writeTransaction = (operation, operationName = "writeTransaction") => {
|
|
686
|
+
assertUsable();
|
|
687
|
+
if (readDepth)
|
|
688
|
+
throw new AppError("read_snapshot_write", "Cannot reserve a writer inside a read snapshot", 1);
|
|
689
|
+
if (!writable)
|
|
690
|
+
throw new AppError("read_only_storage", "Read-only access cannot start a write transaction", 1, { path: dbPath });
|
|
691
|
+
if (db.isTransaction && !reserved)
|
|
692
|
+
throw new AppError("write_transaction_unowned", "Cannot write inside an unowned transaction or read snapshot", 1);
|
|
693
|
+
const nested = db.isTransaction;
|
|
694
|
+
const callbackLength = afterCommit.length;
|
|
695
|
+
const marker = `dd_flow_write_${++savepoint}`;
|
|
696
|
+
const startedAt = performance.now();
|
|
697
|
+
try {
|
|
698
|
+
if (nested)
|
|
699
|
+
db.exec(`SAVEPOINT ${marker}`);
|
|
700
|
+
else
|
|
701
|
+
beginWriteTransaction();
|
|
702
|
+
}
|
|
703
|
+
catch (error) {
|
|
704
|
+
if (error instanceof AppError)
|
|
705
|
+
throw error;
|
|
706
|
+
throw new AppError("storage_write_failed", "Cannot reserve SQLite writer", 1, { cause: errorDetails(error), transaction_phase: "begin", transaction_rolled_back: true });
|
|
707
|
+
}
|
|
708
|
+
let phase = "body";
|
|
709
|
+
try {
|
|
710
|
+
const value = operation();
|
|
711
|
+
if (value && typeof value.then === "function") {
|
|
712
|
+
void Promise.resolve(value).catch(() => { });
|
|
713
|
+
throw new AppError("write_transaction_async", "Write transaction callback must be synchronous", 1);
|
|
714
|
+
}
|
|
715
|
+
phase = "commit";
|
|
716
|
+
bound("commit");
|
|
717
|
+
if (nested)
|
|
718
|
+
db.exec(`RELEASE ${marker}`);
|
|
719
|
+
else
|
|
720
|
+
db.exec("COMMIT");
|
|
721
|
+
phase = "committed";
|
|
722
|
+
if (!nested && ingressPhase.hasSubscribers)
|
|
723
|
+
ingressPhase.publish({ phase: "committed", store_kind: storeKind });
|
|
724
|
+
if (!nested && writerTiming.hasSubscribers)
|
|
725
|
+
writerTiming.publish({ operation: operationName, wall_time_ms: performance.now() - startedAt, path: dbPath });
|
|
726
|
+
if (!nested)
|
|
727
|
+
publishCommitted();
|
|
728
|
+
return value;
|
|
729
|
+
}
|
|
730
|
+
catch (error) {
|
|
731
|
+
if (phase === "committed")
|
|
732
|
+
throw error;
|
|
733
|
+
afterCommit.length = callbackLength;
|
|
734
|
+
try {
|
|
735
|
+
return rollback(nested ? `ROLLBACK TO ${marker}; RELEASE ${marker}` : "ROLLBACK", error);
|
|
736
|
+
}
|
|
737
|
+
catch (failure) {
|
|
738
|
+
if (failure === error && !(error instanceof AppError) && typeof error === "object" && error !== null && "code" in error && String(error.code).startsWith("ERR_SQLITE"))
|
|
739
|
+
throw new AppError("storage_write_failed", "SQLite write failed", 1, { cause: errorDetails(error), transaction_phase: phase, transaction_rolled_back: true });
|
|
740
|
+
throw failure;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
finally {
|
|
744
|
+
if (closed || !db.isOpen || !db.isTransaction) {
|
|
745
|
+
reserved = false;
|
|
746
|
+
if (phase !== "committed") {
|
|
747
|
+
afterCommit = [];
|
|
748
|
+
manualFrames = [];
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
const readSnapshot = (operation) => {
|
|
754
|
+
assertUsable();
|
|
755
|
+
const nested = db.isTransaction;
|
|
756
|
+
const marker = `dd_flow_read_${++savepoint}`;
|
|
757
|
+
const callbackLength = afterCommit.length;
|
|
758
|
+
try {
|
|
759
|
+
db.exec(nested ? `SAVEPOINT ${marker}` : "BEGIN");
|
|
760
|
+
}
|
|
761
|
+
catch (error) {
|
|
762
|
+
throw new AppError("read_snapshot_failed", "Cannot start SQLite read snapshot", 1, { cause: errorDetails(error) });
|
|
763
|
+
}
|
|
764
|
+
readDepth += 1;
|
|
765
|
+
let phase = "body";
|
|
766
|
+
try {
|
|
767
|
+
const value = operation();
|
|
768
|
+
if (value && typeof value.then === "function") {
|
|
769
|
+
void Promise.resolve(value).catch(() => { });
|
|
770
|
+
throw new AppError("read_snapshot_async", "Read snapshot callback must be synchronous", 1);
|
|
771
|
+
}
|
|
772
|
+
phase = "commit";
|
|
773
|
+
db.exec(nested ? `RELEASE ${marker}` : "COMMIT");
|
|
774
|
+
phase = "committed";
|
|
775
|
+
return value;
|
|
776
|
+
}
|
|
777
|
+
catch (error) {
|
|
778
|
+
if (phase === "committed")
|
|
779
|
+
throw error;
|
|
780
|
+
afterCommit.length = callbackLength;
|
|
781
|
+
return rollback(nested ? `ROLLBACK TO ${marker}; RELEASE ${marker}` : "ROLLBACK", error);
|
|
782
|
+
}
|
|
783
|
+
finally {
|
|
784
|
+
readDepth -= 1;
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
const applyManualBoundary = (sql) => {
|
|
788
|
+
for (const statement of sql.split(";")) {
|
|
789
|
+
const save = /^\s*SAVEPOINT\s+([\w-]+)/i.exec(statement);
|
|
790
|
+
if (save) {
|
|
791
|
+
manualFrames.push({ name: save[1], length: afterCommit.length });
|
|
792
|
+
continue;
|
|
793
|
+
}
|
|
794
|
+
const rollbackTo = /^\s*ROLLBACK\s+TO(?:\s+SAVEPOINT)?\s+([\w-]+)/i.exec(statement);
|
|
795
|
+
if (rollbackTo) {
|
|
796
|
+
const frame = manualFrames.findLastIndex((item) => item.name === rollbackTo[1]);
|
|
797
|
+
if (frame >= 0) {
|
|
798
|
+
afterCommit.length = manualFrames[frame].length;
|
|
799
|
+
manualFrames.length = frame + 1;
|
|
800
|
+
}
|
|
801
|
+
continue;
|
|
802
|
+
}
|
|
803
|
+
const release = /^\s*RELEASE(?:\s+SAVEPOINT)?\s+([\w-]+)/i.exec(statement);
|
|
804
|
+
if (release) {
|
|
805
|
+
const frame = manualFrames.findLastIndex((item) => item.name === release[1]);
|
|
806
|
+
if (frame >= 0)
|
|
807
|
+
manualFrames.length = frame;
|
|
808
|
+
continue;
|
|
809
|
+
}
|
|
810
|
+
if (/^\s*ROLLBACK\b/i.test(statement)) {
|
|
811
|
+
afterCommit = [];
|
|
812
|
+
manualFrames = [];
|
|
813
|
+
continue;
|
|
814
|
+
}
|
|
815
|
+
if (/^\s*COMMIT\b/i.test(statement)) {
|
|
816
|
+
manualFrames = [];
|
|
817
|
+
publishCommitted();
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
};
|
|
821
|
+
const facade = {
|
|
822
|
+
path: dbPath,
|
|
823
|
+
writable,
|
|
824
|
+
close: closeOnce,
|
|
825
|
+
exec: (sql) => {
|
|
826
|
+
assertUsable();
|
|
827
|
+
if (readDepth)
|
|
828
|
+
throw new AppError("read_snapshot_write", "Cannot execute SQL inside a read snapshot", 1);
|
|
829
|
+
try {
|
|
830
|
+
db.exec(sql);
|
|
831
|
+
applyManualBoundary(sql);
|
|
832
|
+
}
|
|
833
|
+
catch (error) {
|
|
834
|
+
if (/^\s*ROLLBACK\b/i.test(sql)) {
|
|
835
|
+
poisoned = true;
|
|
836
|
+
try {
|
|
837
|
+
closeOnce();
|
|
838
|
+
}
|
|
839
|
+
catch { /* Keep the primary SQLite failure. */ }
|
|
840
|
+
}
|
|
841
|
+
throw error;
|
|
842
|
+
}
|
|
843
|
+
finally {
|
|
844
|
+
if (closed || !db.isOpen || !db.isTransaction) {
|
|
845
|
+
reserved = false;
|
|
846
|
+
if (!/^\s*COMMIT\b/i.test(sql)) {
|
|
847
|
+
afterCommit = [];
|
|
848
|
+
manualFrames = [];
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
},
|
|
853
|
+
execSchema: (sql) => { assertUsable(); if (db.isTransaction && !reserved)
|
|
854
|
+
throw new AppError("write_transaction_unowned", "Cannot change schema inside an unowned transaction", 1); executeStoreSchema(db, sql); },
|
|
855
|
+
beginWriteTransaction,
|
|
856
|
+
writeTransaction,
|
|
857
|
+
readSnapshot,
|
|
858
|
+
afterCommit: (operation) => { assertUsable(); if (readDepth)
|
|
859
|
+
throw new AppError("read_snapshot_write", "Cannot queue publication inside a read snapshot", 1); if (db.isTransaction)
|
|
860
|
+
afterCommit.push(operation);
|
|
861
|
+
else {
|
|
862
|
+
const scope = publicationScopes.at(-1);
|
|
863
|
+
if (scope)
|
|
864
|
+
scope.push(operation);
|
|
865
|
+
else
|
|
866
|
+
executePublications([operation]);
|
|
867
|
+
} },
|
|
868
|
+
run: (sql, params = []) => { assertUsable(); if (readDepth)
|
|
869
|
+
throw new AppError("read_snapshot_write", "Cannot write inside a read snapshot", 1); bound("write"); return db.prepare(sql).run(...params); },
|
|
870
|
+
get: (sql, params = []) => { assertUsable(); bound("lookup"); return db.prepare(sql).get(...params); },
|
|
871
|
+
all: (sql, params = []) => { assertUsable(); bound("lookup"); return db.prepare(sql).all(...params); }
|
|
872
|
+
};
|
|
873
|
+
facade.withDeadline = (at) => {
|
|
874
|
+
const bounded = requestDeadline(at);
|
|
875
|
+
const scoped = (operation) => { const previous = scopedDeadline; scopedDeadline = phase => Math.min(bounded(phase), previous?.(phase) ?? 4000); try {
|
|
876
|
+
return operation();
|
|
877
|
+
}
|
|
878
|
+
finally {
|
|
879
|
+
scopedDeadline = previous;
|
|
880
|
+
if (db.isOpen)
|
|
881
|
+
db.exec("PRAGMA busy_timeout = 4000");
|
|
882
|
+
} };
|
|
883
|
+
return { ...facade, exec: sql => scoped(() => facade.exec(sql)), execSchema: sql => scoped(() => facade.execSchema(sql)), beginWriteTransaction: () => scoped(() => facade.beginWriteTransaction()), writeTransaction: (operation, name) => scoped(() => facade.writeTransaction(operation, name)), readSnapshot: operation => scoped(() => facade.readSnapshot(operation)), get: (sql, params) => scoped(() => facade.get(sql, params)), all: (sql, params) => scoped(() => facade.all(sql, params)), run: (sql, params) => scoped(() => facade.run(sql, params)) };
|
|
884
|
+
};
|
|
885
|
+
return facade;
|
|
886
|
+
}
|
|
887
|
+
function errorDetails(error) {
|
|
888
|
+
const value = error;
|
|
889
|
+
return { message: String(value?.message ?? error), ...(typeof value?.code === "string" ? { code: value.code } : {}), ...(typeof value?.errcode === "number" ? { sqlite_extended_code: value.errcode } : {}) };
|
|
890
|
+
}
|
|
891
|
+
function configureDatabase(db, mode, ingressDeadline) {
|
|
191
892
|
if (mode !== "read_existing") {
|
|
192
|
-
|
|
893
|
+
// Concurrent first openers can collide while changing journal mode, even
|
|
894
|
+
// with busy_timeout set. Retrying this configuration does not replay writes.
|
|
895
|
+
const deadline = performance.now() + 4000, pause = new Int32Array(new SharedArrayBuffer(4));
|
|
896
|
+
for (;;) {
|
|
897
|
+
try {
|
|
898
|
+
const remaining = ingressDeadline?.("store_configuration");
|
|
899
|
+
if (remaining !== undefined)
|
|
900
|
+
db.exec(`PRAGMA busy_timeout = ${remaining}`);
|
|
901
|
+
const current = db.prepare("PRAGMA journal_mode").get();
|
|
902
|
+
if (current.journal_mode !== "wal")
|
|
903
|
+
db.exec("PRAGMA journal_mode = WAL");
|
|
904
|
+
break;
|
|
905
|
+
}
|
|
906
|
+
catch (error) {
|
|
907
|
+
if ((error.errcode ?? 0) % 256 !== 5 || performance.now() >= deadline)
|
|
908
|
+
throw error;
|
|
909
|
+
Atomics.wait(pause, 0, 0, Math.min(25, Math.max(1, deadline - performance.now())));
|
|
910
|
+
}
|
|
911
|
+
}
|
|
193
912
|
db.exec("PRAGMA synchronous = NORMAL");
|
|
194
913
|
}
|
|
195
914
|
}
|
|
196
|
-
function migrate(db
|
|
915
|
+
function migrate(db) {
|
|
197
916
|
db.exec(`
|
|
198
917
|
CREATE TABLE IF NOT EXISTS projects (
|
|
199
918
|
id TEXT PRIMARY KEY,
|
|
@@ -229,6 +948,7 @@ function migrate(db, dbPath) {
|
|
|
229
948
|
CREATE TABLE IF NOT EXISTS plan_bindings (
|
|
230
949
|
project_id TEXT NOT NULL,
|
|
231
950
|
protocol_id TEXT NOT NULL,
|
|
951
|
+
plan_id TEXT,
|
|
232
952
|
plan_revision INTEGER NOT NULL,
|
|
233
953
|
plan_sha256 TEXT NOT NULL,
|
|
234
954
|
plan_path TEXT NOT NULL,
|
|
@@ -551,6 +1271,8 @@ function migrate(db, dbPath) {
|
|
|
551
1271
|
CREATE INDEX IF NOT EXISTS idx_check_receipts_run
|
|
552
1272
|
ON check_receipts(project_id, run_id, started_at);
|
|
553
1273
|
|
|
1274
|
+
${workContinuationStorageSql}
|
|
1275
|
+
|
|
554
1276
|
CREATE TABLE IF NOT EXISTS merge_requests (
|
|
555
1277
|
merge_request_id TEXT PRIMARY KEY,
|
|
556
1278
|
project_id TEXT NOT NULL,
|
|
@@ -799,7 +1521,6 @@ function migrate(db, dbPath) {
|
|
|
799
1521
|
ensureColumn(db, "sessions", "plan_item_id", "ALTER TABLE sessions ADD COLUMN plan_item_id TEXT");
|
|
800
1522
|
ensureColumn(db, "sessions", "session_kind", "ALTER TABLE sessions ADD COLUMN session_kind TEXT");
|
|
801
1523
|
ensureColumn(db, "sessions", "coverage_units_json", "ALTER TABLE sessions ADD COLUMN coverage_units_json TEXT NOT NULL DEFAULT '[]'");
|
|
802
|
-
migrateSessionStorageKeys(db, dbPath);
|
|
803
1524
|
ensureColumn(db, "usage", "cache_read_input_tokens", "ALTER TABLE usage ADD COLUMN cache_read_input_tokens INTEGER");
|
|
804
1525
|
ensureColumn(db, "usage", "cache_write_input_tokens", "ALTER TABLE usage ADD COLUMN cache_write_input_tokens INTEGER");
|
|
805
1526
|
ensureColumn(db, "usage", "uncached_input_tokens", "ALTER TABLE usage ADD COLUMN uncached_input_tokens INTEGER");
|
|
@@ -809,17 +1530,41 @@ function migrate(db, dbPath) {
|
|
|
809
1530
|
ensureColumn(db, "usage", "source_mtime_ms", "ALTER TABLE usage ADD COLUMN source_mtime_ms INTEGER");
|
|
810
1531
|
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();
|
|
811
1532
|
ensureColumn(db, "runs", "run_home_path", "ALTER TABLE runs ADD COLUMN run_home_path TEXT");
|
|
1533
|
+
ensureColumn(db, "plan_bindings", "plan_id", "ALTER TABLE plan_bindings ADD COLUMN plan_id TEXT");
|
|
1534
|
+
// Historical RUN projections already retain the canonical identifier. Use
|
|
1535
|
+
// those SQL bytes to backfill without opening plan files under the writer.
|
|
1536
|
+
for (const row of db.prepare("SELECT project_id, protocol_id FROM plan_bindings WHERE plan_id IS NULL").all()) {
|
|
1537
|
+
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);
|
|
1538
|
+
for (const run of runs) {
|
|
1539
|
+
try {
|
|
1540
|
+
const planId = JSON.parse(run.index_json).plan_ref?.plan_id;
|
|
1541
|
+
if (typeof planId !== "string" || !planId || planId === "unknown-plan")
|
|
1542
|
+
continue;
|
|
1543
|
+
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);
|
|
1544
|
+
break;
|
|
1545
|
+
}
|
|
1546
|
+
catch { /* A malformed historical RUN cannot establish plan identity. */ }
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
812
1549
|
ensureColumn(db, "runs", "run_root", "ALTER TABLE runs ADD COLUMN run_root TEXT");
|
|
813
1550
|
db.prepare("UPDATE runs SET run_root = COALESCE(run_root, run_home_path, run_dir) WHERE run_root IS NULL OR run_root = ''").run();
|
|
814
1551
|
ensureColumn(db, "merge_requests", "accepted_tree", "ALTER TABLE merge_requests ADD COLUMN accepted_tree TEXT");
|
|
815
1552
|
ensureColumn(db, "merge_requests", "replacement_of_merge_request_id", "ALTER TABLE merge_requests ADD COLUMN replacement_of_merge_request_id TEXT");
|
|
816
1553
|
// A failed integration gate may create a new immutable source candidate in
|
|
817
1554
|
// the same RUN. The historical unique index made that repair path impossible.
|
|
818
|
-
|
|
1555
|
+
// Rebuilding an already-current index changes schema/data_version on every
|
|
1556
|
+
// CLI observation and prevents a controller's writer barrier from settling.
|
|
1557
|
+
const mergeRunIndex = db.prepare("PRAGMA index_list(merge_requests)").all().find(index => index.name === "idx_merge_requests_run");
|
|
1558
|
+
if (mergeRunIndex?.unique)
|
|
1559
|
+
db.exec("DROP INDEX idx_merge_requests_run");
|
|
819
1560
|
db.exec("CREATE INDEX IF NOT EXISTS idx_merge_requests_run ON merge_requests(project_id, run_id, created_at)");
|
|
820
1561
|
ensureColumn(db, "runs", "layout_version", "ALTER TABLE runs ADD COLUMN layout_version TEXT");
|
|
821
1562
|
ensureColumn(db, "runs", "artifact_root_kind", "ALTER TABLE runs ADD COLUMN artifact_root_kind TEXT");
|
|
822
1563
|
ensureColumn(db, "works", "payload_json", "ALTER TABLE works ADD COLUMN payload_json TEXT");
|
|
1564
|
+
ensureColumn(db, "works", "current_start_invocation_id", "ALTER TABLE works ADD COLUMN current_start_invocation_id TEXT");
|
|
1565
|
+
ensureColumn(db, "work_sessions", "start_receipt_json", "ALTER TABLE work_sessions ADD COLUMN start_receipt_json TEXT");
|
|
1566
|
+
ensureColumn(db, "work_sessions", "start_publication_status", "ALTER TABLE work_sessions ADD COLUMN start_publication_status TEXT");
|
|
1567
|
+
ensureColumn(db, "work_sessions", "result_receipt_json", "ALTER TABLE work_sessions ADD COLUMN result_receipt_json TEXT");
|
|
823
1568
|
ensureColumn(db, "check_receipts", "declaration_id", "ALTER TABLE check_receipts ADD COLUMN declaration_id TEXT NOT NULL DEFAULT 'CHK-LEGACY'");
|
|
824
1569
|
ensureColumn(db, "check_receipts", "workspace_fingerprint", "ALTER TABLE check_receipts ADD COLUMN workspace_fingerprint TEXT NOT NULL DEFAULT ''");
|
|
825
1570
|
ensureColumn(db, "check_receipts", "artifacts_json", "ALTER TABLE check_receipts ADD COLUMN artifacts_json TEXT NOT NULL DEFAULT '[]'");
|
|
@@ -827,6 +1572,9 @@ function migrate(db, dbPath) {
|
|
|
827
1572
|
ensureColumn(db, "check_receipts", "gate", "ALTER TABLE check_receipts ADD COLUMN gate TEXT NOT NULL DEFAULT 'work'");
|
|
828
1573
|
ensureColumn(db, "check_receipts", "input_hash", "ALTER TABLE check_receipts ADD COLUMN input_hash TEXT NOT NULL DEFAULT ''");
|
|
829
1574
|
ensureColumn(db, "check_receipts", "verification_epoch", "ALTER TABLE check_receipts ADD COLUMN verification_epoch TEXT NOT NULL DEFAULT ''");
|
|
1575
|
+
ensureColumn(db, "check_receipts", "reservation_sequence", "ALTER TABLE check_receipts ADD COLUMN reservation_sequence INTEGER NOT NULL DEFAULT 0");
|
|
1576
|
+
ensureColumn(db, "check_receipts", "retry_of_receipt_id", "ALTER TABLE check_receipts ADD COLUMN retry_of_receipt_id TEXT");
|
|
1577
|
+
ensureColumn(db, "check_receipts", "retry_reason", "ALTER TABLE check_receipts ADD COLUMN retry_reason TEXT");
|
|
830
1578
|
ensureColumn(db, "check_receipts", "before_fingerprint", "ALTER TABLE check_receipts ADD COLUMN before_fingerprint TEXT NOT NULL DEFAULT ''");
|
|
831
1579
|
ensureColumn(db, "check_receipts", "after_fingerprint", "ALTER TABLE check_receipts ADD COLUMN after_fingerprint TEXT NOT NULL DEFAULT ''");
|
|
832
1580
|
ensureColumn(db, "check_receipts", "profile_hash", "ALTER TABLE check_receipts ADD COLUMN profile_hash TEXT");
|
|
@@ -846,6 +1594,7 @@ function migrate(db, dbPath) {
|
|
|
846
1594
|
ensureColumn(db, "hook_events", "parent_session_id", "ALTER TABLE hook_events ADD COLUMN parent_session_id TEXT");
|
|
847
1595
|
ensureColumn(db, "hook_events", "daemon_id", "ALTER TABLE hook_events ADD COLUMN daemon_id TEXT");
|
|
848
1596
|
ensureColumn(db, "hook_events", "cwd", "ALTER TABLE hook_events ADD COLUMN cwd TEXT");
|
|
1597
|
+
ensureColumn(db, "hook_events", "outcome_json", "ALTER TABLE hook_events ADD COLUMN outcome_json TEXT");
|
|
849
1598
|
ensureColumn(db, "harness_usage_snapshots", "tool_calls", "ALTER TABLE harness_usage_snapshots ADD COLUMN tool_calls INTEGER");
|
|
850
1599
|
ensureColumn(db, "harness_usage_snapshots", "tool_failures", "ALTER TABLE harness_usage_snapshots ADD COLUMN tool_failures INTEGER");
|
|
851
1600
|
ensureColumn(db, "harness_usage_snapshots", "tool_by_name_json", "ALTER TABLE harness_usage_snapshots ADD COLUMN tool_by_name_json TEXT");
|
|
@@ -908,8 +1657,7 @@ function migrateSessionStorageKeys(db, dbPath) {
|
|
|
908
1657
|
db.exec("COMMIT");
|
|
909
1658
|
}
|
|
910
1659
|
catch (error) {
|
|
911
|
-
db
|
|
912
|
-
throw error;
|
|
1660
|
+
return rollbackStoreTransaction(db, "ROLLBACK", error);
|
|
913
1661
|
}
|
|
914
1662
|
}
|
|
915
1663
|
function storageSessionKey(harness, native) {
|