@deksden-com/dd-flow-cli 0.9.0-beta.11 → 0.9.0-beta.110
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 +520 -0
- package/README.md +68 -0
- package/dist/build-info.json +8 -8
- package/dist/cli/command-inputs.js +305 -0
- package/dist/cli/help.js +89 -13
- package/dist/cli/hook-ingress.js +77 -0
- package/dist/cli/input-preparation.js +116 -0
- package/dist/cli/native-hook-ingress.js +48 -0
- package/dist/cli/run-cli.js +1465 -384
- package/dist/cli.js +8 -2
- package/dist/harness-runtime/DD-ZCODE.md +99 -0
- package/dist/harness-runtime/bin/dd-agy.mjs +52 -0
- package/dist/harness-runtime/bin/dd-codex.mjs +26 -0
- package/dist/harness-runtime/bin/dd-droid.mjs +33 -0
- package/dist/harness-runtime/bin/dd-grok.mjs +30 -0
- package/dist/harness-runtime/bin/dd-opencode.mjs +21 -0
- package/dist/harness-runtime/bin/dd-zcode.mjs +83 -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/daemon-operations.mjs +226 -0
- package/dist/harness-runtime/lib/dd-agy-daemon.d.mts +6 -0
- package/dist/harness-runtime/lib/dd-agy-daemon.mjs +500 -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 +149 -0
- package/dist/harness-runtime/lib/dd-codex.mjs +563 -0
- package/dist/harness-runtime/lib/dd-droid-daemon.mjs +125 -0
- package/dist/harness-runtime/lib/dd-droid.mjs +475 -0
- package/dist/harness-runtime/lib/dd-grok-daemon.mjs +388 -0
- package/dist/harness-runtime/lib/dd-grok.mjs +174 -0
- package/dist/harness-runtime/lib/dd-opencode-daemon.mjs +219 -0
- package/dist/harness-runtime/lib/dd-opencode.mjs +98 -0
- package/dist/harness-runtime/lib/dd-zcode-daemon.mjs +645 -0
- package/dist/harness-runtime/lib/dd-zcode.mjs +982 -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 +129 -0
- package/dist/harness-runtime/lib/droid-observation.mjs +66 -0
- package/dist/harness-runtime/lib/managed-daemon.mjs +327 -0
- package/dist/harness-runtime/lib/model-observations.mjs +77 -0
- package/dist/harness-runtime/lib/native-hook-command.d.mts +3 -0
- package/dist/harness-runtime/lib/native-hook-command.mjs +108 -0
- package/dist/harness-runtime/lib/observation-clock.mjs +36 -0
- package/dist/harness-runtime/lib/operation-context.mjs +5 -0
- package/dist/harness-runtime/lib/operation-errors.mjs +19 -0
- package/dist/harness-runtime/lib/process-json.mjs +71 -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/tool-observations.d.mts +17 -0
- package/dist/harness-runtime/lib/tool-observations.mjs +153 -0
- package/dist/harness-runtime/lib/zcode-usage.mjs +54 -0
- package/dist/protocol/local-files.js +18 -9
- package/dist/runtime/context.js +2 -2
- package/dist/schemas/agent-profile.schema.json +1 -1
- package/dist/schemas/code-review-result.schema.json +1 -1
- package/dist/schemas/code-work-batch.schema.json +3 -3
- package/dist/schemas/code-work-result.schema.json +3 -3
- package/dist/schemas/plan-review-result.schema.json +2 -2
- package/dist/schemas/run-control-receipt.schema.json +99 -0
- package/dist/schemas/run-control-request.schema.json +36 -0
- package/dist/schemas/vnext-protocol-plan.schema.json +2 -2
- package/dist/services/canon.js +11 -8
- package/dist/services/cleanup.js +132 -39
- package/dist/services/cli-operation-classifier.js +22 -24
- package/dist/services/code-checks.js +589 -106
- package/dist/services/codex-hook-delivery.js +28 -0
- package/dist/services/command-context.js +80 -0
- package/dist/services/config.js +11 -8
- package/dist/services/continuation-outcome.js +20 -0
- package/dist/services/controller-fanout.js +173 -0
- package/dist/services/dashboard.js +103 -31
- package/dist/services/engines.js +121 -76
- package/dist/services/eval-snapshots.js +994 -97
- package/dist/services/execution-policy.js +181 -0
- package/dist/services/external-work-launch.js +166 -0
- package/dist/services/harness-adapter.js +147 -24
- package/dist/services/harness-config.js +2 -0
- package/dist/services/hooks.js +702 -327
- package/dist/services/lanes.js +78 -65
- package/dist/services/lifecycle-command.js +113 -18
- package/dist/services/lifecycle-contract.js +30 -0
- package/dist/services/lifecycle-invocations.js +1379 -0
- package/dist/services/managed-daemon-binding.js +38 -0
- package/dist/services/managed-processes.js +302 -34
- package/dist/services/merge-queue.js +228 -149
- package/dist/services/merge-server.js +66 -28
- 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 +25 -16
- package/dist/services/protocols.js +160 -86
- package/dist/services/recovery-observation-budget.js +61 -0
- package/dist/services/recovery-snapshot-database.js +142 -0
- package/dist/services/repair-intents.js +40 -0
- package/dist/services/review-copy.js +136 -0
- package/dist/services/run-control-receipt.js +56 -0
- package/dist/services/run-control-worker.js +351 -0
- package/dist/services/run-control.js +888 -0
- package/dist/services/run-controller-adapter.js +231 -0
- package/dist/services/run-controller-capture.js +167 -0
- package/dist/services/run-controller-process.js +180 -0
- package/dist/services/run-controller-recovery.js +221 -0
- package/dist/services/run-controller-state.js +36 -0
- package/dist/services/run-controller.js +798 -0
- package/dist/services/run-engine-bindings.js +1 -1
- package/dist/services/run-fork.js +142 -0
- package/dist/services/run-observations.js +112 -0
- package/dist/services/run-projection.js +22 -64
- package/dist/services/run-recovery-runtime.js +80 -0
- package/dist/services/run-recovery.js +345 -0
- package/dist/services/runs.js +536 -181
- package/dist/services/runtime-budget.js +413 -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 +460 -0
- package/dist/services/runtime-scope-resume.js +577 -0
- package/dist/services/runtime-scope-stop.js +159 -0
- package/dist/services/runtime-scope-worker.js +242 -0
- package/dist/services/runtime-service.js +97 -0
- package/dist/services/schema-validation.js +10 -8
- package/dist/services/scope-writer-cleanup.js +26 -0
- package/dist/services/sessions.js +35 -69
- package/dist/services/stage-blocker.js +17 -7
- package/dist/services/stage-context.js +50 -19
- package/dist/services/stage-lifecycle.js +100 -103
- package/dist/services/stage-pause.js +88 -75
- package/dist/services/stage-report-renderer.js +116 -6
- package/dist/services/stage-work-graph.js +35 -0
- package/dist/services/usage.js +35 -98
- package/dist/services/vnext-code-review.js +197 -84
- package/dist/services/vnext-code.js +278 -251
- package/dist/services/vnext-execution-profile.js +5 -3
- package/dist/services/vnext-fanout.js +172 -17
- package/dist/services/vnext-merge.js +345 -111
- package/dist/services/vnext-plan-review.js +171 -111
- package/dist/services/vnext-plan.js +180 -87
- package/dist/services/vnext-protocolize.js +320 -74
- package/dist/services/vnext-specify.js +115 -77
- package/dist/services/work-registry.js +1001 -187
- package/dist/services/workspace-bootstrap.js +76 -0
- package/dist/services/workspace-files.js +83 -0
- package/dist/services/worktrees.js +88 -36
- package/dist/shared/entity-references.js +8 -0
- package/dist/shared/errors.js +12 -0
- package/dist/storage/database.js +692 -35
- 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 +24 -13
- package/tools/repair-paused-run-status.mjs +84 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { DatabaseSync } from "node:sqlite";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { AppError } from "../shared/errors.js";
|
|
5
|
+
import { registerStoreWriter } from "../storage/writer-contract.js";
|
|
6
|
+
import { managedProcessRuntimeHome } from "./managed-processes.js";
|
|
7
|
+
import { cleanupScopeWriters } from "./scope-writer-cleanup.js";
|
|
8
|
+
/** Export one RUN's relational evidence, never mutate the live authority. */
|
|
9
|
+
export function captureRecoveryDatabase(source, output, projectId, runId, protocolIds = []) {
|
|
10
|
+
source.exec(`VACUUM INTO '${output.replace(/'/g, "''")}'`);
|
|
11
|
+
const db = new DatabaseSync(output);
|
|
12
|
+
let complete = false, held = false, failed = false, primary;
|
|
13
|
+
try {
|
|
14
|
+
registerStoreWriter(db);
|
|
15
|
+
db.exec("PRAGMA foreign_keys = OFF; PRAGMA secure_delete = ON; BEGIN IMMEDIATE");
|
|
16
|
+
held = true;
|
|
17
|
+
db.exec("CREATE TEMP TABLE capture_scope (project_id TEXT, run_id TEXT)");
|
|
18
|
+
db.prepare("INSERT INTO capture_scope VALUES (?, ?)").run(projectId, runId);
|
|
19
|
+
db.exec("CREATE TEMP TABLE capture_protocols (id TEXT)");
|
|
20
|
+
for (const id of protocolIds)
|
|
21
|
+
db.prepare("INSERT INTO capture_protocols VALUES (?)").run(id);
|
|
22
|
+
if (!db.prepare("SELECT 1 FROM runs WHERE (project_id, id) IN (SELECT * FROM capture_scope)").get())
|
|
23
|
+
throw new AppError("snapshot_run_missing", "Recovery export requires the selected RUN", 1);
|
|
24
|
+
const ownRun = "(project_id, run_id) IN (SELECT * FROM capture_scope)";
|
|
25
|
+
const ownProject = "project_id IN (SELECT project_id FROM capture_scope)";
|
|
26
|
+
const works = `SELECT work_id FROM works WHERE ${ownRun}`;
|
|
27
|
+
if (db.prepare(`SELECT 1 FROM sessions WHERE ${ownProject} AND run_id IS NOT NULL AND run_id NOT IN (SELECT run_id FROM capture_scope) AND session_id IN (SELECT session_id FROM work_sessions WHERE work_id IN (${works})) LIMIT 1`).get())
|
|
28
|
+
throw new AppError("snapshot_scope_conflict", "Selected Work refers to a Session currently bound to another RUN", 1);
|
|
29
|
+
const sessions = `SELECT session_id FROM sessions WHERE ${ownProject} AND (run_id IN (SELECT run_id FROM capture_scope) OR session_id IN (SELECT session_id FROM work_sessions WHERE work_id IN (${works})))`;
|
|
30
|
+
const nativeSessions = `SELECT harness, provider_session_id FROM sessions WHERE ${ownProject} AND session_id IN (${sessions})`;
|
|
31
|
+
const invocations = "json_extract(scope_json, '$.runId') IN (SELECT run_id FROM capture_scope) AND json_extract(scope_json, '$.projectRoot') IN (SELECT root FROM projects WHERE id IN (SELECT project_id FROM capture_scope))";
|
|
32
|
+
const invocationReceipts = db.prepare("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = 'lifecycle_invocations'").get()
|
|
33
|
+
? ` OR event_key IN (SELECT event_key FROM lifecycle_invocations WHERE ${invocations})` : "";
|
|
34
|
+
const protocols = `SELECT id FROM capture_protocols UNION SELECT subject_id FROM runs WHERE (project_id, id) IN (SELECT * FROM capture_scope) UNION SELECT value FROM merge_requests, json_each(merge_requests.protocol_ids_json) WHERE ${ownRun} UNION SELECT protocol_id FROM flow_jobs WHERE ${ownRun}`;
|
|
35
|
+
const rules = {
|
|
36
|
+
dd_flow_writer_contract: "1",
|
|
37
|
+
dd_flow_writer_migrations: "0",
|
|
38
|
+
projects: "id IN (SELECT project_id FROM capture_scope)",
|
|
39
|
+
runs: "(project_id, id) IN (SELECT * FROM capture_scope)",
|
|
40
|
+
sessions: `${ownProject} AND session_id IN (${sessions})`,
|
|
41
|
+
work_sessions: `work_id IN (${works})`, work_launches: `work_id IN (${works})`,
|
|
42
|
+
work_recovery_segments: `work_id IN (${works})`,
|
|
43
|
+
run_recovery_bindings: `recovery_id IN (SELECT recovery_id FROM run_recovery_guards WHERE ${ownRun})`,
|
|
44
|
+
lifecycle_invocations: invocations,
|
|
45
|
+
lifecycle_packet_assignments: `invocation_id IN (SELECT id FROM lifecycle_invocations WHERE ${invocations})`,
|
|
46
|
+
hook_events: `${ownProject} AND (session_id IN (${sessions}) OR (harness, provider_session_id) IN (${nativeSessions})${invocationReceipts})`,
|
|
47
|
+
harness_usage_snapshots: `${ownProject} AND (harness, provider_session_id) IN (${nativeSessions})`,
|
|
48
|
+
protocols: `${ownProject} AND id IN (${protocols})`,
|
|
49
|
+
project_config: ownProject,
|
|
50
|
+
// Host installations, caches, queue owners and leases are not portable RUN authority.
|
|
51
|
+
runtime_config: "0", hook_installations: "0", codex_home_profiles: "0", codex_session_bindings: "0",
|
|
52
|
+
merge_servers: "0", merge_sessions: "0", merge_queue: "0", lanes: "0", lane_locks: "0", lane_waiters: "0",
|
|
53
|
+
sqlite_sequence: "0"
|
|
54
|
+
};
|
|
55
|
+
for (const table of ["works", "stage_work_memberships", "repair_intents", "check_receipts", "merge_requests", "flow_run_flag_mutations", "usage", "flow_session_segments", "flow_jobs", "run_recovery_guards", "run_controllers", "run_controls"])
|
|
56
|
+
rules[table] = ownRun;
|
|
57
|
+
for (const table of ["plan_bindings", "plan_progress", "worktree_records", "audit_events"])
|
|
58
|
+
rules[table] = `${ownProject} AND protocol_id IN (${protocols})`;
|
|
59
|
+
for (const table of ["run_controller_events", "run_controller_operations"])
|
|
60
|
+
rules[table] = `controller_id IN (SELECT controller_id FROM run_controllers WHERE ${ownRun})`;
|
|
61
|
+
for (const table of ["run_control_admissions", "run_control_stage_admissions", "run_control_operations", "run_control_workers"])
|
|
62
|
+
rules[table] = `control_id IN (SELECT control_id FROM run_controls WHERE ${ownRun})`;
|
|
63
|
+
const tables = db.prepare("SELECT name FROM sqlite_schema WHERE type = 'table'").all();
|
|
64
|
+
const quote = (name) => `"${name.replaceAll('"', '""')}"`;
|
|
65
|
+
// Evaluate every relationship before deleting parents, including historical bindings.
|
|
66
|
+
for (const { name } of tables) {
|
|
67
|
+
const predicate = rules[name];
|
|
68
|
+
if (predicate === undefined && db.prepare(`SELECT 1 FROM ${quote(name)} LIMIT 1`).get())
|
|
69
|
+
throw new AppError("snapshot_scope_unknown", "Recovery export has no scope rule for a populated table", 1, { table: name });
|
|
70
|
+
db.exec(`CREATE TEMP TABLE ${quote(`keep_${name}`)} AS SELECT rowid AS retained_id FROM ${quote(name)} WHERE ${predicate ?? "0"}`);
|
|
71
|
+
}
|
|
72
|
+
for (const { name } of tables)
|
|
73
|
+
db.exec(`DELETE FROM ${quote(name)} WHERE rowid NOT IN (SELECT retained_id FROM ${quote(`keep_${name}`)})`);
|
|
74
|
+
const issues = db.prepare("PRAGMA foreign_key_check").all();
|
|
75
|
+
if (issues.length)
|
|
76
|
+
throw new AppError("snapshot_scope_inconsistent", "Scoped recovery export has unresolved relational references", 1, { issues });
|
|
77
|
+
db.exec("COMMIT");
|
|
78
|
+
held = false;
|
|
79
|
+
db.exec("VACUUM");
|
|
80
|
+
complete = true;
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
primary = error;
|
|
84
|
+
failed = true;
|
|
85
|
+
}
|
|
86
|
+
const cleanup = cleanupScopeWriters([{ name: output, db, rollback: held, close: "always" }]);
|
|
87
|
+
if (!complete) {
|
|
88
|
+
try {
|
|
89
|
+
fs.rmSync(output, { force: true });
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
cleanup.push({ target: output, phase: "remove_incomplete_output", message: String(error) });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (failed || cleanup.length) {
|
|
96
|
+
if (primary instanceof AppError) {
|
|
97
|
+
if (cleanup.length)
|
|
98
|
+
primary.details.snapshot_cleanup_errors = cleanup;
|
|
99
|
+
throw primary;
|
|
100
|
+
}
|
|
101
|
+
throw new AppError("snapshot_database_failed", primary instanceof Error ? primary.message : "Snapshot database cleanup failed", 1, { ...(failed ? { cause: String(primary) } : {}), snapshot_cleanup_errors: cleanup });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** Physical leases are evidence, not authority to resurrect on another host. */
|
|
105
|
+
export function readRecoveryResourceEvidence(resourceHome, projectId, runId, authority) {
|
|
106
|
+
const file = path.join(resourceHome, "runtime.sqlite");
|
|
107
|
+
const scope = { schema_id: "dd-flow/recovery-resource-evidence@1", source_home: resourceHome, dd_flow_home: authority.ddFlowHome, project_id: projectId, run_id: runId };
|
|
108
|
+
if (!fs.existsSync(file))
|
|
109
|
+
return { ...scope, database_present: false, processes: [], resources: [] };
|
|
110
|
+
const db = new DatabaseSync(file, { readOnly: true });
|
|
111
|
+
let held = false, failed = false, primary, result;
|
|
112
|
+
try {
|
|
113
|
+
db.exec("PRAGMA busy_timeout = 4000; BEGIN");
|
|
114
|
+
held = true;
|
|
115
|
+
const candidates = db.prepare("SELECT * FROM managed_processes WHERE project_id = ? AND run_id = ? ORDER BY id").all(projectId, runId).map(record => ({ record, home: managedProcessRuntimeHome(record, authority) }));
|
|
116
|
+
if (candidates.some(item => item.home === null))
|
|
117
|
+
throw new AppError("snapshot_resource_scope_unproven", "A retained process has no proven runtime-home identity", 1, { resource_home: resourceHome });
|
|
118
|
+
const processes = candidates.filter(item => item.home === authority.ddFlowHome).map(item => item.record);
|
|
119
|
+
const ids = new Set(processes.map(record => record.id)), owners = new Set(processes.map(record => record.owner_id));
|
|
120
|
+
const resources = db.prepare("SELECT r.* FROM managed_resources r JOIN managed_processes p ON p.id = r.process_id WHERE p.project_id = ? AND p.run_id = ? ORDER BY r.resource_kind, r.resource_key").all(projectId, runId).filter(row => typeof row.process_id === "string" && ids.has(row.process_id));
|
|
121
|
+
const unbound = db.prepare("SELECT owner_id FROM managed_resources WHERE process_id IS NULL AND owner_id IN (SELECT owner_id FROM managed_processes WHERE project_id = ? AND run_id = ?)").all(projectId, runId).some(row => typeof row.owner_id === "string" && owners.has(row.owner_id));
|
|
122
|
+
if (unbound)
|
|
123
|
+
throw new AppError("snapshot_resource_scope_unproven", "A retained resource has no process binding proving its RUN scope", 1, { resource_home: resourceHome });
|
|
124
|
+
db.exec("COMMIT");
|
|
125
|
+
held = false;
|
|
126
|
+
result = { ...scope, database_present: true, processes, resources };
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
primary = error;
|
|
130
|
+
failed = true;
|
|
131
|
+
}
|
|
132
|
+
const cleanup = cleanupScopeWriters([{ name: file, db, rollback: held, close: "always" }]);
|
|
133
|
+
if (failed || cleanup.length) {
|
|
134
|
+
if (primary instanceof AppError) {
|
|
135
|
+
if (cleanup.length)
|
|
136
|
+
primary.details.snapshot_cleanup_errors = cleanup;
|
|
137
|
+
throw primary;
|
|
138
|
+
}
|
|
139
|
+
throw new AppError("snapshot_resource_evidence_failed", primary instanceof Error ? primary.message : "Resource evidence cleanup failed", 1, { ...(failed ? { cause: String(primary) } : {}), snapshot_cleanup_errors: cleanup });
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { stageAttempt, stageCycle, bindStageWork } from "./stage-work-graph.js";
|
|
3
|
+
import { assertRunMutationAllowed } from "./run-recovery.js";
|
|
4
|
+
import { AppError } from "../shared/errors.js";
|
|
5
|
+
/** Checks finish before entering this synchronous registration transaction. */
|
|
6
|
+
export function ensureAggregateRepair(context, input, create) {
|
|
7
|
+
if (!input.failures.length || input.failures.some(receipt => receipt.status !== "failed" && receipt.status !== "aborted"))
|
|
8
|
+
throw new AppError("repair_intent_invalid", "Repair requires settled failed receipts", 1);
|
|
9
|
+
const causes = input.failures.map(receipt => ({ id: receipt.id, epoch: receipt.verification_epoch, fingerprint: receipt.workspace_fingerprint })).sort((a, b) => a.id.localeCompare(b.id));
|
|
10
|
+
return ensureRepairIntent(context, { ...input, causes: { receipts: causes, origins: [...(input.origins ?? [])].sort() } }, create);
|
|
11
|
+
}
|
|
12
|
+
export function ensureRepairIntent(context, input, create) {
|
|
13
|
+
return context.db.writeTransaction(() => {
|
|
14
|
+
assertRunMutationAllowed(context, input.projectId, input.runId, input.generation);
|
|
15
|
+
const attempt = stageAttempt(context, input.projectId, input.runId, input.stage);
|
|
16
|
+
const payload = JSON.stringify({ project_id: input.projectId, run_id: input.runId, stage: input.stage, attempt, cycle: stageCycle(context, input.projectId, input.runId, input.stage), causes: input.causes });
|
|
17
|
+
const identity = crypto.createHash("sha256").update(payload).digest("hex");
|
|
18
|
+
const prior = context.db.get("SELECT work_id, payload_json FROM repair_intents WHERE identity = ?", [identity]);
|
|
19
|
+
if (prior && prior.payload_json !== payload)
|
|
20
|
+
throw new AppError("repair_intent_conflict", "Repair identity has different causes", 1);
|
|
21
|
+
const workId = prior?.work_id ?? create();
|
|
22
|
+
if (!prior) {
|
|
23
|
+
bindStageWork(context, { projectId: input.projectId, runId: input.runId, workId, stage: input.stage, attempt });
|
|
24
|
+
context.db.run("INSERT INTO repair_intents (identity, project_id, run_id, stage, attempt, payload_hash, payload_json, work_id, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", [identity, input.projectId, input.runId, input.stage, attempt, identity, payload, workId, context.now()]);
|
|
25
|
+
}
|
|
26
|
+
return { schema_id: "dd-flow/continuation@1", kind: "repair_required", project_id: input.projectId, run_id: input.runId, stage: input.stage, attempt, identity, work_id: workId };
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
export function isRegisteredRepair(context, value, scope) {
|
|
30
|
+
if (!value || typeof value !== "object")
|
|
31
|
+
return false;
|
|
32
|
+
const receipt = value;
|
|
33
|
+
if (receipt.schema_id !== "dd-flow/continuation@1" || receipt.kind !== "repair_required")
|
|
34
|
+
return false;
|
|
35
|
+
if (scope && (receipt.project_id !== scope.projectId || receipt.run_id !== scope.runId))
|
|
36
|
+
return false;
|
|
37
|
+
if (!context.db.get("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'repair_intents'"))
|
|
38
|
+
return false;
|
|
39
|
+
return Boolean(context.db.get("SELECT 1 FROM repair_intents i JOIN works w ON w.work_id = i.work_id AND w.project_id = i.project_id AND w.run_id = i.run_id WHERE i.identity = ? AND i.project_id = ? AND i.run_id = ? AND i.stage = ? AND i.attempt = ? AND i.work_id = ?", [receipt.identity, receipt.project_id, receipt.run_id, receipt.stage, receipt.attempt, receipt.work_id]));
|
|
40
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
5
|
+
import { AppError } from "../shared/errors.js";
|
|
6
|
+
import { ignoredWorkspacePaths } from "./code-checks.js";
|
|
7
|
+
import { localEnvironmentAllowed, ownedWorkspaceHookPath, projectServicePathFilter, workspaceGitFiles } from "./workspace-files.js";
|
|
8
|
+
export function reviewEnvironmentAllowed(file) {
|
|
9
|
+
return localEnvironmentAllowed(file);
|
|
10
|
+
}
|
|
11
|
+
export function reviewSourcePaths(root, explicitInputs = [], base = "HEAD") {
|
|
12
|
+
const inventory = workspaceGitFiles(root, ["--cached", "--others", "--exclude-standard"]);
|
|
13
|
+
if (inventory === null)
|
|
14
|
+
return null;
|
|
15
|
+
const serviceAllowed = projectServicePathFilter(root, explicitInputs);
|
|
16
|
+
const hasBase = base !== "HEAD" || spawnSync("git", ["-C", root, "rev-parse", "--verify", "HEAD"], { encoding: "utf8" }).status === 0;
|
|
17
|
+
const changes = hasBase ? spawnSync("git", ["-C", root, "diff", "--name-status", "-z", "--find-renames", base, "--"], { encoding: "utf8", maxBuffer: 64 * 1024 * 1024 }) : null;
|
|
18
|
+
if (changes && (changes.status !== 0 || changes.error))
|
|
19
|
+
throw new AppError("review_git_evidence_failed", "Could not classify renamed review paths", 1, { workspace_root: root, cause: changes.error?.message ?? changes.stderr });
|
|
20
|
+
const fields = changes?.stdout.split("\0").filter(Boolean) ?? [], tainted = new Set();
|
|
21
|
+
for (let index = 0; index < fields.length;) {
|
|
22
|
+
const status = fields[index++];
|
|
23
|
+
if (/^[RC]/.test(status)) {
|
|
24
|
+
const before = fields[index++], after = fields[index++];
|
|
25
|
+
if (!reviewEnvironmentAllowed(before) || !reviewEnvironmentAllowed(after)) {
|
|
26
|
+
tainted.add(before);
|
|
27
|
+
tainted.add(after);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else
|
|
31
|
+
index += 1;
|
|
32
|
+
}
|
|
33
|
+
return [...new Set([...inventory, ...explicitInputs])]
|
|
34
|
+
.filter(relative => reviewEnvironmentAllowed(relative) && !tainted.has(relative) && serviceAllowed(path.join(root, relative)))
|
|
35
|
+
.sort();
|
|
36
|
+
}
|
|
37
|
+
export function reviewSourceFilter(root, explicitInputs = [], base = "HEAD") {
|
|
38
|
+
const selected = reviewSourcePaths(root, explicitInputs, base);
|
|
39
|
+
const serviceAllowed = projectServicePathFilter(root, explicitInputs);
|
|
40
|
+
const retained = new Set(selected?.map(relative => path.resolve(root, relative)) ?? []);
|
|
41
|
+
const branches = new Set();
|
|
42
|
+
for (const file of retained)
|
|
43
|
+
for (let directory = path.dirname(file); directory !== path.dirname(directory); directory = path.dirname(directory))
|
|
44
|
+
branches.add(directory);
|
|
45
|
+
return file => {
|
|
46
|
+
const parts = path.relative(root, file).split(path.sep);
|
|
47
|
+
if (parts.some(part => [".git", "node_modules", ".dd-flow"].includes(part)) || !reviewEnvironmentAllowed(file))
|
|
48
|
+
return false;
|
|
49
|
+
return selected === null ? serviceAllowed(file) : retained.has(path.resolve(file)) || branches.has(path.resolve(file));
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export function reviewSourceHash(root, explicitInputs = [], base = "HEAD") {
|
|
53
|
+
const allowed = reviewSourceFilter(root, explicitInputs, base), ownedHook = ownedWorkspaceHookPath(root);
|
|
54
|
+
return reviewCopyHash(root, file => path.relative(root, file) !== ownedHook && allowed(file));
|
|
55
|
+
}
|
|
56
|
+
/** The copied project has no Git index. Apply the source's exact service-file
|
|
57
|
+
* policy to it, while retaining only our frozen Git evidence under .dd-flow. */
|
|
58
|
+
export function reviewExecutionCopyHash(copyRoot, sourceRoot, includeEvidence = true, explicitInputs = [], base = "HEAD") {
|
|
59
|
+
if (ownedWorkspaceHookPath(sourceRoot) && !ownedWorkspaceHookPath(copyRoot))
|
|
60
|
+
throw new AppError("read_only_input_changed", "Qualified review hook differs between execution copy and source", 2, { copy_root: copyRoot });
|
|
61
|
+
const sourceAllowed = reviewSourceFilter(sourceRoot, explicitInputs, base);
|
|
62
|
+
const serviceAllowed = projectServicePathFilter(sourceRoot, explicitInputs);
|
|
63
|
+
const ownedHook = ownedWorkspaceHookPath(sourceRoot);
|
|
64
|
+
const verifySelection = (directory) => {
|
|
65
|
+
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
|
66
|
+
const file = path.join(directory, entry.name), relative = path.relative(copyRoot, file);
|
|
67
|
+
const parts = relative.split(path.sep);
|
|
68
|
+
if (parts[0] === ".dd-flow" || parts.some(part => [".git", "node_modules"].includes(part)))
|
|
69
|
+
continue;
|
|
70
|
+
if (!reviewEnvironmentAllowed(file))
|
|
71
|
+
throw new AppError("read_only_input_changed", "Read-only execution copy contains a local environment file", 2, { path: relative });
|
|
72
|
+
if (!serviceAllowed(path.join(sourceRoot, relative)))
|
|
73
|
+
continue;
|
|
74
|
+
if (!sourceAllowed(path.join(sourceRoot, relative)) && ignoredWorkspacePaths(sourceRoot, [relative]).includes(relative.split(path.sep).join("/")))
|
|
75
|
+
continue;
|
|
76
|
+
if (!sourceAllowed(path.join(sourceRoot, relative)))
|
|
77
|
+
throw new AppError("read_only_input_changed", "Read-only execution copy contains an unselected product input", 2, { path: relative });
|
|
78
|
+
if (entry.isDirectory())
|
|
79
|
+
verifySelection(file);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
verifySelection(copyRoot);
|
|
83
|
+
return reviewCopyHash(copyRoot, file => {
|
|
84
|
+
const relative = path.relative(copyRoot, file);
|
|
85
|
+
const parts = relative.split(path.sep);
|
|
86
|
+
if (parts[0] === ".dd-flow")
|
|
87
|
+
return includeEvidence && (parts.length === 1 || parts[1] === "review-evidence");
|
|
88
|
+
return relative !== ownedHook && sourceAllowed(path.join(sourceRoot, relative));
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/** Exact bytes visible in a read-only execution copy, including symlink targets. */
|
|
92
|
+
export function reviewCopyHash(root, allowed = () => true) {
|
|
93
|
+
const hash = crypto.createHash("sha256");
|
|
94
|
+
let canonicalRoot;
|
|
95
|
+
try {
|
|
96
|
+
canonicalRoot = fs.realpathSync(root);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
throw new AppError("read_only_input_changed", "Read-only execution copy is missing", 2, { root });
|
|
100
|
+
}
|
|
101
|
+
const visit = (directory) => {
|
|
102
|
+
for (const entry of fs.readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
103
|
+
const file = path.join(directory, entry.name);
|
|
104
|
+
if (!allowed(file))
|
|
105
|
+
continue;
|
|
106
|
+
const relative = path.relative(root, file).split(path.sep).join("/");
|
|
107
|
+
const stat = fs.lstatSync(file);
|
|
108
|
+
const record = (kind, value = "") => hash.update(`${JSON.stringify([relative, kind, stat.mode & 0o111, value])}\n`);
|
|
109
|
+
if (entry.isDirectory()) {
|
|
110
|
+
record("directory");
|
|
111
|
+
visit(file);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (entry.isSymbolicLink()) {
|
|
115
|
+
const link = fs.readlinkSync(file);
|
|
116
|
+
let target;
|
|
117
|
+
try {
|
|
118
|
+
target = fs.realpathSync(file);
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
throw new AppError("read_only_workspace_escape", "Read-only copy contains an unresolved symlink", 1, { path: relative });
|
|
122
|
+
}
|
|
123
|
+
const inside = path.relative(canonicalRoot, target);
|
|
124
|
+
if (path.isAbsolute(link) || inside === ".." || inside.startsWith(`..${path.sep}`))
|
|
125
|
+
throw new AppError("read_only_workspace_escape", "Read-only copy contains a symlink outside its workspace", 1, { path: relative });
|
|
126
|
+
record("symlink", link);
|
|
127
|
+
}
|
|
128
|
+
else if (entry.isFile())
|
|
129
|
+
record("file", crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"));
|
|
130
|
+
else
|
|
131
|
+
throw new AppError("read_only_workspace_unsupported", "Read-only copy contains an unsupported file type", 1, { path: relative });
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
visit(root);
|
|
135
|
+
return hash.digest("hex");
|
|
136
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
const object = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
3
|
+
const records = (value) => Array.isArray(value) ? value.map(object) : [];
|
|
4
|
+
const text = (value) => typeof value === "string" && value.length ? value : null;
|
|
5
|
+
/** Pure projection of durable evidence. Never infer quiescence from an ACK,
|
|
6
|
+
* a terminal RUN label, or a settlement captured before a subsequent resume. */
|
|
7
|
+
export function controlStatusReceipt(observation, request = {}) {
|
|
8
|
+
const control = object(observation.control), worker = object(observation.worker), resume = object(observation.resume_operation);
|
|
9
|
+
const present = Boolean(control.control_id), current = control.current === true;
|
|
10
|
+
const activeResume = Boolean(resume.operation_id);
|
|
11
|
+
const historical = !current || activeResume || ["resuming", "resumed"].includes(String(control.status));
|
|
12
|
+
const settlement = object(control.settlement);
|
|
13
|
+
const snapshot = historical ? {} : control.admission === "sealed" ? settlement : object(worker.snapshot);
|
|
14
|
+
const nodes = records(snapshot.nodes).map((node, index) => {
|
|
15
|
+
const identity = Object.fromEntries(["operation_id", "controller_id", "process_id", "check_id", "merge_request_id", "work_id", "work_session_id", "session_id", "harness", "state_dir", "resource_home"].filter(key => node[key] !== undefined).map(key => [key, node[key]]));
|
|
16
|
+
const settled = node.status === "completed";
|
|
17
|
+
const role = ["subject", "worker", "merge", "judge", "probe", "check", "process"].includes(String(node.role)) ? node.role : node.check_id ? "check" : node.merge_request_id ? "merge" : node.process_id ? "process" : "unknown";
|
|
18
|
+
return { id: `node:${crypto.createHash("sha256").update(JSON.stringify(Object.keys(identity).length ? identity : { index, reason: node.reason })).digest("hex")}`, role, status: settled ? "completed" : node.status === "outcome_unknown" ? "unknown" : "pending", acknowledged: settled || object(node.receipt).cancelled === true, settled, reason: settled ? null : text(node.reason) ?? "node_settlement_required", details: node };
|
|
19
|
+
});
|
|
20
|
+
const settled = present && current && !historical && control.status === "settled" && control.admission === "sealed" && Boolean(control.capture_path) && settlement.settled === true && Array.isArray(settlement.nodes) && nodes.every(node => node.settled);
|
|
21
|
+
let state;
|
|
22
|
+
if (!present)
|
|
23
|
+
state = request.action && ["done", "failed", "cancelled"].includes(String(observation.run_status)) ? "terminal_noop" : "not_requested";
|
|
24
|
+
else if (!current)
|
|
25
|
+
state = "superseded";
|
|
26
|
+
else if (resume.status === "failed" || worker.status === "failed")
|
|
27
|
+
state = "recovery_required";
|
|
28
|
+
else if (control.status === "resumed" && control.admission === "recovered" && resume.status === "completed")
|
|
29
|
+
state = "running";
|
|
30
|
+
else if (activeResume || control.status === "resuming")
|
|
31
|
+
state = "resuming";
|
|
32
|
+
else if (settled)
|
|
33
|
+
state = control.requested_mode === "pause" ? "paused" : "stopped";
|
|
34
|
+
else
|
|
35
|
+
state = control.requested_mode === "pause" ? "pause_requested" : "stop_requested";
|
|
36
|
+
const reasons = settled ? [] : [...new Set([
|
|
37
|
+
...nodes.filter(node => !node.settled).map(node => node.reason),
|
|
38
|
+
...(Array.isArray(snapshot.pending_reasons) ? snapshot.pending_reasons.filter((reason) => typeof reason === "string" && Boolean(reason)) : []),
|
|
39
|
+
...(!present ? [state === "terminal_noop" ? "terminal_settlement_not_observed" : "control_not_requested"]
|
|
40
|
+
: !current ? ["control_superseded"]
|
|
41
|
+
: state === "running" ? ["run_active"]
|
|
42
|
+
: state === "resuming" ? ["recovery_acceptance_pending"]
|
|
43
|
+
: state === "recovery_required" ? [text(object(resume.error).code) ?? text(object(worker.error).code) ?? "control_recovery_required"]
|
|
44
|
+
: control.admission === "sealed" ? ["recovery_capture_required"] : ["settlement_observation_required"])
|
|
45
|
+
])];
|
|
46
|
+
return {
|
|
47
|
+
...observation, schema_id: "dd-flow/run-control-receipt@1", ok: true,
|
|
48
|
+
request_id: request.requestId ?? text(resume.request_id) ?? text(control.request_id), control_id: text(control.control_id),
|
|
49
|
+
generation: Number.isSafeInteger(control.generation) ? control.generation : 0,
|
|
50
|
+
requested_mode: request.action ?? (activeResume ? "resume" : text(control.requested_mode)),
|
|
51
|
+
applied_mode: settled ? control.requested_mode : state === "running" ? "resume" : null,
|
|
52
|
+
state, settled, nodes, unsettled_reasons: reasons,
|
|
53
|
+
resumability: settled || state === "recovery_required" ? "recovery_required" : ["running", "not_requested", "terminal_noop", "superseded"].includes(state) ? "not_applicable" : "pending",
|
|
54
|
+
recovery_id: text(control.recovery_id)
|
|
55
|
+
};
|
|
56
|
+
}
|