@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.
Files changed (162) hide show
  1. package/CHANGELOG.md +520 -0
  2. package/README.md +68 -0
  3. package/dist/build-info.json +8 -8
  4. package/dist/cli/command-inputs.js +305 -0
  5. package/dist/cli/help.js +89 -13
  6. package/dist/cli/hook-ingress.js +77 -0
  7. package/dist/cli/input-preparation.js +116 -0
  8. package/dist/cli/native-hook-ingress.js +48 -0
  9. package/dist/cli/run-cli.js +1465 -384
  10. package/dist/cli.js +8 -2
  11. package/dist/harness-runtime/DD-ZCODE.md +99 -0
  12. package/dist/harness-runtime/bin/dd-agy.mjs +52 -0
  13. package/dist/harness-runtime/bin/dd-codex.mjs +26 -0
  14. package/dist/harness-runtime/bin/dd-droid.mjs +33 -0
  15. package/dist/harness-runtime/bin/dd-grok.mjs +30 -0
  16. package/dist/harness-runtime/bin/dd-opencode.mjs +21 -0
  17. package/dist/harness-runtime/bin/dd-zcode.mjs +83 -0
  18. package/dist/harness-runtime/lib/agy-hooks.d.mts +1 -0
  19. package/dist/harness-runtime/lib/agy-hooks.mjs +9 -0
  20. package/dist/harness-runtime/lib/daemon-operations.mjs +226 -0
  21. package/dist/harness-runtime/lib/dd-agy-daemon.d.mts +6 -0
  22. package/dist/harness-runtime/lib/dd-agy-daemon.mjs +500 -0
  23. package/dist/harness-runtime/lib/dd-agy.mjs +59 -0
  24. package/dist/harness-runtime/lib/dd-codex-daemon.d.mts +1 -0
  25. package/dist/harness-runtime/lib/dd-codex-daemon.mjs +149 -0
  26. package/dist/harness-runtime/lib/dd-codex.mjs +563 -0
  27. package/dist/harness-runtime/lib/dd-droid-daemon.mjs +125 -0
  28. package/dist/harness-runtime/lib/dd-droid.mjs +475 -0
  29. package/dist/harness-runtime/lib/dd-grok-daemon.mjs +388 -0
  30. package/dist/harness-runtime/lib/dd-grok.mjs +174 -0
  31. package/dist/harness-runtime/lib/dd-opencode-daemon.mjs +219 -0
  32. package/dist/harness-runtime/lib/dd-opencode.mjs +98 -0
  33. package/dist/harness-runtime/lib/dd-zcode-daemon.mjs +645 -0
  34. package/dist/harness-runtime/lib/dd-zcode.mjs +982 -0
  35. package/dist/harness-runtime/lib/delegation-instructions.d.mts +13 -0
  36. package/dist/harness-runtime/lib/delegation-instructions.mjs +144 -0
  37. package/dist/harness-runtime/lib/dispatch-fence.mjs +14 -0
  38. package/dist/harness-runtime/lib/driver-recovery.mjs +129 -0
  39. package/dist/harness-runtime/lib/droid-observation.mjs +66 -0
  40. package/dist/harness-runtime/lib/managed-daemon.mjs +327 -0
  41. package/dist/harness-runtime/lib/model-observations.mjs +77 -0
  42. package/dist/harness-runtime/lib/native-hook-command.d.mts +3 -0
  43. package/dist/harness-runtime/lib/native-hook-command.mjs +108 -0
  44. package/dist/harness-runtime/lib/observation-clock.mjs +36 -0
  45. package/dist/harness-runtime/lib/operation-context.mjs +5 -0
  46. package/dist/harness-runtime/lib/operation-errors.mjs +19 -0
  47. package/dist/harness-runtime/lib/process-json.mjs +71 -0
  48. package/dist/harness-runtime/lib/process-snapshot.mjs +10 -0
  49. package/dist/harness-runtime/lib/runner-events.mjs +200 -0
  50. package/dist/harness-runtime/lib/runner-lock.mjs +42 -0
  51. package/dist/harness-runtime/lib/session-settlement.mjs +30 -0
  52. package/dist/harness-runtime/lib/tool-observations.d.mts +17 -0
  53. package/dist/harness-runtime/lib/tool-observations.mjs +153 -0
  54. package/dist/harness-runtime/lib/zcode-usage.mjs +54 -0
  55. package/dist/protocol/local-files.js +18 -9
  56. package/dist/runtime/context.js +2 -2
  57. package/dist/schemas/agent-profile.schema.json +1 -1
  58. package/dist/schemas/code-review-result.schema.json +1 -1
  59. package/dist/schemas/code-work-batch.schema.json +3 -3
  60. package/dist/schemas/code-work-result.schema.json +3 -3
  61. package/dist/schemas/plan-review-result.schema.json +2 -2
  62. package/dist/schemas/run-control-receipt.schema.json +99 -0
  63. package/dist/schemas/run-control-request.schema.json +36 -0
  64. package/dist/schemas/vnext-protocol-plan.schema.json +2 -2
  65. package/dist/services/canon.js +11 -8
  66. package/dist/services/cleanup.js +132 -39
  67. package/dist/services/cli-operation-classifier.js +22 -24
  68. package/dist/services/code-checks.js +589 -106
  69. package/dist/services/codex-hook-delivery.js +28 -0
  70. package/dist/services/command-context.js +80 -0
  71. package/dist/services/config.js +11 -8
  72. package/dist/services/continuation-outcome.js +20 -0
  73. package/dist/services/controller-fanout.js +173 -0
  74. package/dist/services/dashboard.js +103 -31
  75. package/dist/services/engines.js +121 -76
  76. package/dist/services/eval-snapshots.js +994 -97
  77. package/dist/services/execution-policy.js +181 -0
  78. package/dist/services/external-work-launch.js +166 -0
  79. package/dist/services/harness-adapter.js +147 -24
  80. package/dist/services/harness-config.js +2 -0
  81. package/dist/services/hooks.js +702 -327
  82. package/dist/services/lanes.js +78 -65
  83. package/dist/services/lifecycle-command.js +113 -18
  84. package/dist/services/lifecycle-contract.js +30 -0
  85. package/dist/services/lifecycle-invocations.js +1379 -0
  86. package/dist/services/managed-daemon-binding.js +38 -0
  87. package/dist/services/managed-processes.js +302 -34
  88. package/dist/services/merge-queue.js +228 -149
  89. package/dist/services/merge-server.js +66 -28
  90. package/dist/services/migrations.js +37 -16
  91. package/dist/services/native-daemon-history.js +49 -0
  92. package/dist/services/native-session-control.js +39 -0
  93. package/dist/services/plan-runtime.js +26 -11
  94. package/dist/services/plans.js +28 -25
  95. package/dist/services/projects.js +20 -15
  96. package/dist/services/prompts.js +25 -16
  97. package/dist/services/protocols.js +160 -86
  98. package/dist/services/recovery-observation-budget.js +61 -0
  99. package/dist/services/recovery-snapshot-database.js +142 -0
  100. package/dist/services/repair-intents.js +40 -0
  101. package/dist/services/review-copy.js +136 -0
  102. package/dist/services/run-control-receipt.js +56 -0
  103. package/dist/services/run-control-worker.js +351 -0
  104. package/dist/services/run-control.js +888 -0
  105. package/dist/services/run-controller-adapter.js +231 -0
  106. package/dist/services/run-controller-capture.js +167 -0
  107. package/dist/services/run-controller-process.js +180 -0
  108. package/dist/services/run-controller-recovery.js +221 -0
  109. package/dist/services/run-controller-state.js +36 -0
  110. package/dist/services/run-controller.js +798 -0
  111. package/dist/services/run-engine-bindings.js +1 -1
  112. package/dist/services/run-fork.js +142 -0
  113. package/dist/services/run-observations.js +112 -0
  114. package/dist/services/run-projection.js +22 -64
  115. package/dist/services/run-recovery-runtime.js +80 -0
  116. package/dist/services/run-recovery.js +345 -0
  117. package/dist/services/runs.js +536 -181
  118. package/dist/services/runtime-budget.js +413 -0
  119. package/dist/services/runtime-command.js +43 -0
  120. package/dist/services/runtime-scope-capture.js +52 -0
  121. package/dist/services/runtime-scope-control.js +460 -0
  122. package/dist/services/runtime-scope-resume.js +577 -0
  123. package/dist/services/runtime-scope-stop.js +159 -0
  124. package/dist/services/runtime-scope-worker.js +242 -0
  125. package/dist/services/runtime-service.js +97 -0
  126. package/dist/services/schema-validation.js +10 -8
  127. package/dist/services/scope-writer-cleanup.js +26 -0
  128. package/dist/services/sessions.js +35 -69
  129. package/dist/services/stage-blocker.js +17 -7
  130. package/dist/services/stage-context.js +50 -19
  131. package/dist/services/stage-lifecycle.js +100 -103
  132. package/dist/services/stage-pause.js +88 -75
  133. package/dist/services/stage-report-renderer.js +116 -6
  134. package/dist/services/stage-work-graph.js +35 -0
  135. package/dist/services/usage.js +35 -98
  136. package/dist/services/vnext-code-review.js +197 -84
  137. package/dist/services/vnext-code.js +278 -251
  138. package/dist/services/vnext-execution-profile.js +5 -3
  139. package/dist/services/vnext-fanout.js +172 -17
  140. package/dist/services/vnext-merge.js +345 -111
  141. package/dist/services/vnext-plan-review.js +171 -111
  142. package/dist/services/vnext-plan.js +180 -87
  143. package/dist/services/vnext-protocolize.js +320 -74
  144. package/dist/services/vnext-specify.js +115 -77
  145. package/dist/services/work-registry.js +1001 -187
  146. package/dist/services/workspace-bootstrap.js +76 -0
  147. package/dist/services/workspace-files.js +83 -0
  148. package/dist/services/worktrees.js +88 -36
  149. package/dist/shared/entity-references.js +8 -0
  150. package/dist/shared/errors.js +12 -0
  151. package/dist/storage/database.js +692 -35
  152. package/dist/storage/foreign-key-transaction.js +39 -0
  153. package/dist/storage/immutable-input.js +45 -0
  154. package/dist/storage/import-guard.js +102 -0
  155. package/dist/storage/native-cleanup.js +52 -0
  156. package/dist/storage/paths.js +17 -4
  157. package/dist/storage/projection-lock.js +73 -0
  158. package/dist/storage/work-references.js +23 -0
  159. package/dist/storage/writer-contract.js +77 -0
  160. package/dist/storage/writer-migration.js +85 -0
  161. package/package.json +24 -13
  162. package/tools/repair-paused-run-status.mjs +84 -0
@@ -1,15 +1,27 @@
1
1
  import crypto from "node:crypto";
2
+ import { isGitlessWorkspace, ownedWorkspaceHookPath, projectServicePathFilter, workspaceGitFiles } from "./workspace-files.js";
2
3
  import { spawn, spawnSync } from "node:child_process";
3
4
  import fs from "node:fs";
4
5
  import path from "node:path";
6
+ import { parse } from "shell-quote";
7
+ import { assertRunMutationAllowed } from "./run-recovery.js";
5
8
  import { AppError } from "../shared/errors.js";
6
- import { confirmManagedProcess, finishManagedProcess, heartbeatManagedProcess, managedProcessStatus, processIsAlive, registerManagedProcess, reservePorts } from "./managed-processes.js";
9
+ import { confirmManagedProcess, finishManagedProcess, heartbeatManagedProcess, managedProcessStatus, managedProcessRuntimeHome, managedOwnerIsAlive, processTreeIsAlive, registerManagedProcess, reservePorts } from "./managed-processes.js";
10
+ import { admitRuntimeCheck, runtimeBudgetForRun } from "./runtime-budget.js";
7
11
  const profileRelativePath = path.join(".memory-bank", "spec", "engineering", "code-check-profile.json");
8
12
  // Receipt reservation precedes child spawn so an interrupted CLI leaves an
9
13
  // auditable attempt. A second caller must not mistake that small handoff window
10
14
  // for a dead check and overwrite its outcome.
11
15
  const receiptStartingGraceMs = 30_000;
12
- export function codeExecutionEnvironment(workspaceRoot) { const suffix = crypto.createHash("sha256").update(path.resolve(workspaceRoot)).digest("hex").slice(0, 12); return { ...process.env, DD_FLOW_LOCAL_DATABASE_SUFFIX: suffix }; }
16
+ export function codeExecutionEnvironment(workspaceRoot) {
17
+ const suffix = crypto.createHash("sha256").update(path.resolve(workspaceRoot)).digest("hex").slice(0, 12);
18
+ // Managed checks retain the launcher's executable resolution. A login shell
19
+ // or NODE_PATH can silently replace it, including with another architecture.
20
+ const environment = { ...process.env, DD_FLOW_LOCAL_DATABASE_SUFFIX: suffix };
21
+ delete environment.NODE_PATH;
22
+ return environment;
23
+ }
24
+ export function workspaceOperationId(workspaceRoot) { return `workspace:${crypto.createHash("sha256").update(path.resolve(workspaceRoot)).digest("hex")}`; }
13
25
  export function readCodeCheckProfile(workspaceRoot) {
14
26
  const file = path.join(workspaceRoot, profileRelativePath);
15
27
  if (!fs.existsSync(file))
@@ -18,8 +30,8 @@ export function readCodeCheckProfile(workspaceRoot) {
18
30
  const value = JSON.parse(bytes.toString("utf8"));
19
31
  if (value.schema_id !== "dd-flow/code-check-profile@6")
20
32
  throw new AppError("invalid_code_check_profile", "CODE check profile has an unsupported schema", 2, { file, schema_id: value.schema_id ?? null, required: "dd-flow/code-check-profile@6" });
21
- if (!isStringRecord(value.aliases) || !isStringRecord(value.require_alias_for) || !isGateRecord(value.mandatory_by_gate) || !isPortAliasRecord(value.ports_by_alias))
22
- throw new AppError("invalid_code_check_profile", "CODE check profile aliases, require_alias_for, mandatory_by_gate or ports_by_alias are invalid", 2, { file });
33
+ if (!isStringRecord(value.aliases) || !isStringRecord(value.require_alias_for) || !isGateRecord(value.mandatory_by_gate) || !isPortAliasRecord(value.ports_by_alias) || !isInputAliasRecord(workspaceRoot, value.inputs_by_alias))
34
+ throw new AppError("invalid_code_check_profile", "CODE check profile aliases, require_alias_for, mandatory_by_gate, ports_by_alias or inputs_by_alias are invalid", 2, { file });
23
35
  for (const [gate, aliases] of Object.entries(value.mandatory_by_gate))
24
36
  for (const alias of aliases ?? [])
25
37
  if (!value.aliases[alias])
@@ -27,19 +39,46 @@ export function readCodeCheckProfile(workspaceRoot) {
27
39
  for (const alias of Object.keys(value.ports_by_alias ?? {}))
28
40
  if (!value.aliases[alias])
29
41
  throw new AppError("invalid_code_check_profile", "A port declaration references an unknown alias", 2, { file, alias });
42
+ for (const alias of Object.keys(value.inputs_by_alias ?? {}))
43
+ if (!value.aliases[alias])
44
+ throw new AppError("invalid_code_check_profile", "An input declaration references an unknown alias", 2, { file, alias });
30
45
  return { profile: value, hash: crypto.createHash("sha256").update(bytes).digest("hex"), file };
31
46
  }
47
+ /**
48
+ * Planned aliases are qualification-owned extensions: their provider Work is
49
+ * expected to materialize them after PLAN. Freeze the project policy and
50
+ * existing aliases, while still validating every planned alias definition at
51
+ * execution time. Unknown edits and changes to existing policy remain part
52
+ * of the hash and therefore fail closed.
53
+ */
54
+ export function codeCheckProfileHash(workspaceRoot, checks = []) {
55
+ const { profile } = readCodeCheckProfile(workspaceRoot);
56
+ if (!profile)
57
+ return null;
58
+ const planned = new Set(checks.filter(check => check.availability === "planned" && check.command.startsWith("@check/")).map(check => check.command));
59
+ if (!planned.size)
60
+ return crypto.createHash("sha256").update(JSON.stringify(profile)).digest("hex");
61
+ const canonical = {
62
+ ...profile,
63
+ aliases: Object.fromEntries(Object.entries(profile.aliases).filter(([alias]) => !planned.has(alias))),
64
+ ports_by_alias: Object.fromEntries(Object.entries(profile.ports_by_alias ?? {}).filter(([alias]) => !planned.has(alias))),
65
+ inputs_by_alias: Object.fromEntries(Object.entries(profile.inputs_by_alias ?? {}).filter(([alias]) => !planned.has(alias)))
66
+ };
67
+ return crypto.createHash("sha256").update(JSON.stringify(canonical)).digest("hex");
68
+ }
32
69
  export function validateCodeCheckCommands(workspaceRoot, commands) {
33
70
  const { profile, file } = readCodeCheckProfile(workspaceRoot);
34
- if (!profile)
35
- return [...commands];
36
- return commands.map((command) => { if (command.startsWith("@check/")) {
37
- if (!profile.aliases[command]?.trim())
38
- throw new AppError("unknown_code_check_alias", "CODE check alias is not defined by the project profile", 2, { alias: command, file });
71
+ return commands.map((command) => {
72
+ if (command.startsWith("@check/")) {
73
+ if (!profile?.aliases[command]?.trim())
74
+ throw new AppError("unknown_code_check_alias", "CODE check alias is not defined by the project profile", 2, { alias: command, file });
75
+ return command;
76
+ }
77
+ for (const [prefix, alias] of Object.entries(profile?.require_alias_for ?? {}))
78
+ if (command === prefix || command.startsWith(`${prefix} `))
79
+ throw new AppError("raw_code_check_forbidden", "CODE check must use the project alias instead of a raw guarded command", 2, { command, required_alias: alias, file });
39
80
  return command;
40
- } for (const [prefix, alias] of Object.entries(profile.require_alias_for))
41
- if (command === prefix || command.startsWith(`${prefix} `))
42
- throw new AppError("raw_code_check_forbidden", "CODE check must use the project alias instead of a raw guarded command", 2, { command, required_alias: alias, file }); return command; });
81
+ });
43
82
  }
44
83
  export function resolveCodeCheckCommands(workspaceRoot, runId, commands) {
45
84
  const validated = validateCodeCheckCommands(workspaceRoot, commands);
@@ -50,7 +89,7 @@ export function resolveCodeCheckCommands(workspaceRoot, runId, commands) {
50
89
  }
51
90
  export function projectPolicyCheckDeclarations(workspaceRoot, gate) {
52
91
  const { profile } = readCodeCheckProfile(workspaceRoot);
53
- return (profile?.mandatory_by_gate[gate] ?? []).map((alias) => { const ref = `POLICY/${gate}/${alias.slice(7).replace(/[^a-z0-9]+/gi, "-").replace(/^-|-$/g, "").toLowerCase()}`; return { id: ref, canonical_ref: ref, source: "project_policy", command: alias, purpose: `Mandatory project ${gate} gate.`, run_at: gate, availability: "available", ...(profile?.ports_by_alias?.[alias] ? { ports: profile.ports_by_alias[alias] } : {}) }; });
92
+ return (profile?.mandatory_by_gate[gate] ?? []).map((alias) => { const ref = `POLICY/${gate}/${alias.slice(7).replace(/[^a-z0-9]+/gi, "-").replace(/^-|-$/g, "").toLowerCase()}`; return { id: ref, canonical_ref: ref, source: "project_policy", command: alias, purpose: `Mandatory project ${gate} gate.`, run_at: gate, availability: "available", ...(profile?.ports_by_alias?.[alias] ? { ports: profile.ports_by_alias[alias] } : {}), ...(profile?.inputs_by_alias?.[alias] ? { inputs: profile.inputs_by_alias[alias] } : {}) }; });
54
93
  }
55
94
  export function effectiveCheckDeclarations(workspaceRoot, declared, gates) { return gates.flatMap((gate) => [...declared.filter((check) => check.run_at === gate), ...projectPolicyCheckDeclarations(workspaceRoot, gate)]); }
56
95
  export function aggregateCheckDeclarations(workspaceRoot) { return projectPolicyCheckDeclarations(workspaceRoot, "code"); }
@@ -60,12 +99,15 @@ export function validateCheckPlacement(workspaceRoot, checks) { const { profile
60
99
  if (check.run_at === "work" && aggregateCommands.has(check.command))
61
100
  throw new AppError("aggregate_check_requires_code_gate", "A project aggregate check must run at CODE or readiness, not inside one scoped Work", 2, { check_id: check.id, command: check.command, run_at: check.run_at }); }
62
101
  export function validateCheckDeclaration(workspaceRoot, check) {
63
- if (check.run_at === "external")
64
- return;
102
+ if (!["work", "code", "readiness", "merge", "release"].includes(check.run_at))
103
+ throw new AppError("unsupported_check_gate", "Check requires an autonomous executable gate; replan the incompatible declaration", 2, { check_id: check.id, run_at: check.run_at });
65
104
  const { profile } = readCodeCheckProfile(workspaceRoot);
66
105
  if (check.availability === "planned" && check.command.startsWith("@check/") && !profile?.aliases[check.command])
67
106
  throw new AppError("planned_check_not_materialized", "A planned check alias was not materialized by its provider Work", 2, { check_id: check.id, command: check.command, provider: check.provided_by ?? null });
68
107
  validateCodeCheckCommands(workspaceRoot, [check.command]);
108
+ if (check.availability === "available" && !check.command.startsWith("@check/"))
109
+ assertDirectCheckExecutable(workspaceRoot, check.command, check.id);
110
+ validateCheckInputs(workspaceRoot, check.inputs ?? []);
69
111
  if (check.command.startsWith("@check/") && check.definition && profile?.aliases[check.command] !== check.definition)
70
112
  throw new AppError("check_definition_drift", "The accepted check alias definition differs from the current project profile", 2, { check_id: check.id, alias: check.command, expected: check.definition, actual: profile?.aliases[check.command] ?? null });
71
113
  if (check.availability !== "planned")
@@ -77,50 +119,134 @@ export function validateCheckDeclaration(workspaceRoot, check) {
77
119
  if (!profile?.aliases[check.command])
78
120
  throw new AppError("planned_check_not_materialized", "A planned check alias was not materialized by its provider Work", 2, { check_id: check.id, command: check.command, provider: check.provided_by ?? null });
79
121
  }
122
+ /** Reject a plan-time typo or a fictitious "manual" command before it becomes
123
+ * a late shell exit. Compound shell programs remain valid: only a simple,
124
+ * directly named executable is preflighted here. */
125
+ function assertDirectCheckExecutable(workspaceRoot, command, checkId) {
126
+ const trimmed = command.trim();
127
+ // A shell operator or expansion makes the launch path dynamic. Do not
128
+ // guess there; this preflight is deliberately limited to a direct program
129
+ // with ordinary arguments and optional literal environment assignments.
130
+ if (/[|&;<>()`$\n\r~]/.test(trimmed))
131
+ return;
132
+ const parsed = parse(trimmed);
133
+ if (parsed.some(word => typeof word !== "string"))
134
+ return;
135
+ const words = parsed;
136
+ let index = 0;
137
+ const env = codeExecutionEnvironment(workspaceRoot);
138
+ while (/^[A-Za-z_][A-Za-z0-9_]*=/.test(words[index] ?? "")) {
139
+ const assignment = words[index++];
140
+ const equals = assignment.indexOf("=");
141
+ env[assignment.slice(0, equals)] = assignment.slice(equals + 1);
142
+ }
143
+ const program = words[index];
144
+ if (!program)
145
+ return;
146
+ const probe = spawnSync("/bin/sh", ["-c", "command -v -- \"$1\" >/dev/null", "dd-flow-check", program], { cwd: workspaceRoot, env, encoding: "utf8", timeout: 5000 });
147
+ if (probe.status !== 0)
148
+ throw new AppError("check_command_unavailable", "Available CODE check names an executable that is unavailable in this environment", 2, { check_id: checkId, command, executable: program });
149
+ }
80
150
  export async function runCodeChecks(context, input) {
81
151
  reconcileUnfinishedChecks(context, input);
152
+ reconcileWorkspaceOwnership(context, input.workspaceRoot);
153
+ const gate = acquireWorkspaceGate(context, input);
154
+ try {
155
+ return await runCodeChecksOwned(context, input, gate.id);
156
+ }
157
+ finally {
158
+ finishManagedProcess(context, { id: gate.id, leaseToken: gate.lease_token, state: "stopped", reason: "workspace_gate_settled" });
159
+ }
160
+ }
161
+ async function runCodeChecksOwned(context, input, workspaceGateId) {
162
+ const generation = assertRunMutationAllowed(context, input.projectId, input.runId);
82
163
  for (const check of input.checks)
83
164
  validateCheckDeclaration(input.workspaceRoot, check);
84
- const { hash: profileHash, profile } = readCodeCheckProfile(input.workspaceRoot);
85
- const checks = input.checks.map((check) => { const ports = profile?.ports_by_alias?.[check.command]; return check.ports || !ports ? check : { ...check, ports }; });
165
+ const { profile } = readCodeCheckProfile(input.workspaceRoot);
166
+ const profileHash = codeCheckProfileHash(input.workspaceRoot, input.checks);
167
+ if (input.expectedProfileHash !== undefined && input.expectedProfileHash !== profileHash)
168
+ throw new AppError("check_profile_drift", "The frozen check profile differs from the workspace profile", 1, { expected_profile_hash: input.expectedProfileHash, actual_profile_hash: profileHash });
169
+ const checks = input.checks.map((check) => { const ports = profile?.ports_by_alias?.[check.command]; const inputs = profile?.inputs_by_alias?.[check.command]; return { ...check, ...(check.ports || !ports ? {} : { ports }), ...(check.inputs || !inputs ? {} : { inputs }) }; });
86
170
  const resolved = resolveCodeCheckCommands(input.workspaceRoot, input.runId, checks.map((check) => check.command));
87
- const groups = deduplicate(checks, resolved);
88
- const receipts = [];
171
+ assertConsistentDeclarations(checks, resolved);
172
+ const groups = deduplicate(checks, resolved), receipts = [];
173
+ const retryIds = [...new Set([...(input.retryReceiptId ? [input.retryReceiptId] : []), ...(input.retryReceiptIds ?? [])])];
174
+ const retries = new Map();
175
+ for (const receiptId of retryIds) {
176
+ const predecessor = validateFailedCheckRetry(context, { projectId: input.projectId, runId: input.runId, receiptId, declarations: checks, reason: input.retryReason });
177
+ const snapshot = workspaceState(input.workspaceRoot, [input.runHome]);
178
+ const group = groups.find(candidate => candidate.declarations.some(check => check.run_at === predecessor.gate)
179
+ && checkInputHash(input.workspaceRoot, { ...candidate, gate: predecessor.gate }, profileHash, workspaceInputFingerprint(input.workspaceRoot, snapshot.files, candidate.inputs, [input.runHome])) === predecessor.input_hash);
180
+ if (!group || retries.has(group))
181
+ throw new AppError("retry_check_invalid", "Retry must match exactly one current execution contract", 2, { check_receipt_id: receiptId });
182
+ // Preserve the identity of a pre-deduplication failed execution when an
183
+ // upgraded RUN retries it; the new receipt still proves every grouped ref.
184
+ group.gate = predecessor.gate;
185
+ retries.set(group, receiptId);
186
+ }
89
187
  for (let index = 0; index < groups.length; index += 1) {
90
188
  const group = groups[index];
91
189
  const before = workspaceState(input.workspaceRoot, [input.runHome]);
92
190
  const startedAt = context.now();
93
- const inputHash = crypto.createHash("sha256").update(JSON.stringify({ gate: group.gate, command: group.command, environment: String(codeExecutionEnvironment(input.workspaceRoot).DD_FLOW_LOCAL_DATABASE_SUFFIX), required_artifacts: group.requiredArtifacts, ports: [...group.ports].sort() })).digest("hex");
191
+ const inputFingerprint = workspaceInputFingerprint(input.workspaceRoot, before.files, group.inputs, [input.runHome]);
192
+ const inputHash = checkInputHash(input.workspaceRoot, group, profileHash, inputFingerprint);
94
193
  const epoch = crypto.createHash("sha256").update(`${group.gate}\0${before.fingerprint}`).digest("hex");
95
- const reserved = reserveReceipt(context, { ...input, group, before, profileHash, inputHash, epoch, startedAt });
194
+ const reservationInput = { ...input, retryReceiptIds: retries.has(group) ? [retries.get(group)] : [], generation, group, before, profileHash, inputHash, epoch, startedAt };
195
+ delete reservationInput.retryReceiptId;
196
+ const decision = reserveOrReuseReceipt(context, reservationInput);
197
+ if (decision.receipt) {
198
+ receipts.push(bindReceipt(decision.receipt, group));
199
+ input.progress?.(`check ${index + 1}/${groups.length} ${decision.receipt.status === "passed" ? "reused" : "requires repair"}: ${group.command}`);
200
+ continue;
201
+ }
202
+ const reserved = decision.reserved;
96
203
  const { id, evidenceDir, stdoutPath, stderrPath, receiptPath, completionPath } = reserved;
97
204
  let resources = {};
98
205
  let result = { exitCode: null, error: "check did not start", aborted: true };
99
206
  try {
100
207
  fs.mkdirSync(evidenceDir, { recursive: true });
101
208
  writeReceipt(receiptPath, receiptFor({ ...reserved, resources, result, status: "running", after: before, mutationPaths: [], artifacts: [] }));
102
- const managed = registerManagedProcess(context, { kind: "check", ownerId: `check:${id}`, projectId: input.projectId, runId: input.runId, workId: input.workId ?? null, checkId: id, stdoutPath, stderrPath, metadata: { command: group.command } });
209
+ const budget = runtimeBudgetForRun(context, input.projectId, input.runId);
210
+ const managed = registerManagedProcess(context, { kind: "check", ownerPid: process.pid, ownerId: `check:${id}`, operationId: workspaceOperationId(input.workspaceRoot), projectId: input.projectId, runId: input.runId, workId: input.workId ?? null, checkId: id, stdoutPath, stderrPath, metadata: { command: group.command, role: "check", workspace_root: input.workspaceRoot, workspace_gate_id: workspaceGateId, budget } });
103
211
  const allocation = await reservePorts(context, { ownerId: managed.owner_id, processId: managed.id, names: group.ports });
104
212
  resources = allocation.ports;
105
- input.progress?.(`check ${index + 1}/${groups.length} started: ${group.command}`);
213
+ try {
214
+ input.progress?.(`check ${index + 1}/${groups.length} preparing: ${group.command}`);
215
+ assertRunMutationAllowed(context, input.projectId, input.runId, generation);
216
+ }
217
+ catch (error) {
218
+ allocation.release();
219
+ finishManagedProcess(context, { id: managed.id, leaseToken: managed.lease_token, state: "failed", reason: "dispatch_fenced_before_spawn" });
220
+ throw error;
221
+ }
106
222
  const stdout = fs.openSync(stdoutPath, "a");
107
223
  const stderr = fs.openSync(stderrPath, "a");
108
224
  try {
109
- result = await runCheck(group.command, input.workspaceRoot, { ...codeExecutionEnvironment(input.workspaceRoot), DD_FLOW_EVIDENCE_DIR: evidenceDir, DD_FLOW_CHECK_COMPLETION_FILE: completionPath, ...portEnvironment(resources) }, stdout, stderr, (elapsed) => input.progress?.(`check ${index + 1}/${groups.length} still running (${elapsed}s): ${group.command}`), (pid) => confirmManagedProcess(context, { id: managed.id, leaseToken: managed.lease_token, pid, processGroupId: process.platform === "win32" ? null : pid }), () => heartbeatManagedProcess(context, { id: managed.id, leaseToken: managed.lease_token }));
225
+ result = await runCheck(group.command, input.workspaceRoot, { ...codeExecutionEnvironment(input.workspaceRoot), DD_FLOW_EVIDENCE_DIR: evidenceDir, DD_FLOW_CHECK_COMPLETION_FILE: completionPath, ...portEnvironment(resources) }, stdout, stderr, (elapsed) => input.progress?.(`check ${index + 1}/${groups.length} still running (${elapsed}s): ${group.command}`), (pid) => {
226
+ confirmManagedProcess(context, { id: managed.id, leaseToken: managed.lease_token, pid, processGroupId: process.platform === "win32" ? null : pid });
227
+ assertRunMutationAllowed(context, input.projectId, input.runId, generation);
228
+ if (budget)
229
+ admitRuntimeCheck(context, managed.id, managed.lease_token);
230
+ }, () => heartbeatManagedProcess(context, { id: managed.id, leaseToken: managed.lease_token }));
110
231
  }
111
232
  finally {
112
- allocation.release();
113
233
  fs.closeSync(stdout);
114
234
  fs.closeSync(stderr);
115
- finishManagedProcess(context, { id: managed.id, leaseToken: managed.lease_token, state: result.aborted || result.exitCode !== 0 || Boolean(result.error) ? "failed" : "stopped", reason: result.error });
116
235
  }
236
+ const current = managedProcessStatus(context).find(item => item.id === managed.id);
237
+ if (processTreeIsAlive(current))
238
+ throw new AppError("check_in_progress", "The check shell exited but its process tree is still live; retain this receipt and its resources until settlement", 2, { process_id: managed.id, check_receipt_id: id });
239
+ allocation.release();
240
+ finishManagedProcess(context, { id: managed.id, leaseToken: managed.lease_token, state: result.aborted || result.exitCode !== 0 || Boolean(result.error) ? "failed" : "stopped", reason: result.error });
117
241
  }
118
242
  catch (error) {
243
+ if (error instanceof AppError && error.code === "check_in_progress")
244
+ throw error;
119
245
  result = { exitCode: null, error: error instanceof Error ? error.message : String(error), aborted: true };
120
246
  }
121
247
  const after = workspaceState(input.workspaceRoot, [input.runHome]);
122
248
  const mutationPaths = changedPaths(before.files, after.files);
123
- const artifacts = collectRequiredArtifacts(evidenceDir, group.requiredArtifacts);
249
+ const artifacts = collectCheckArtifacts(evidenceDir, group.requiredArtifacts);
124
250
  if (fs.existsSync(stderrPath)) {
125
251
  if (!artifacts.complete)
126
252
  fs.appendFileSync(stderrPath, `\nmissing required evidence artifacts: ${artifacts.missing.join(", ")}\n`);
@@ -136,6 +262,16 @@ export async function runCodeChecks(context, input) {
136
262
  receipts.push(receipt);
137
263
  input.progress?.(`check ${index + 1}/${groups.length} ${status}: ${group.command}`);
138
264
  }
265
+ if (receipts.every(receipt => receipt.status === "passed")) {
266
+ const current = workspaceState(input.workspaceRoot, [input.runHome]), currentProfileHash = codeCheckProfileHash(input.workspaceRoot, input.checks);
267
+ for (const receipt of receipts) {
268
+ const group = groups.find(candidate => candidate.declarations.some(check => receipt.check_refs.includes(checkRef(check))));
269
+ const currentHash = group ? checkInputHash(input.workspaceRoot, group, currentProfileHash, workspaceInputFingerprint(input.workspaceRoot, current.files, group.inputs, [input.runHome])) : null;
270
+ if (!group || !checkReceiptEvidenceValid(receipt) || receipt.input_hash !== currentHash)
271
+ throw new AppError("check_evidence_stale", "Check evidence changed before the gate could be accepted", 1, { check_receipt_id: receipt.id });
272
+ }
273
+ assertRunMutationAllowed(context, input.projectId, input.runId, generation);
274
+ }
139
275
  return receipts;
140
276
  }
141
277
  /**
@@ -144,25 +280,36 @@ export async function runCodeChecks(context, input) {
144
280
  * a dead/terminal process becomes an explicit aborted receipt that can retry.
145
281
  */
146
282
  export function reconcileUnfinishedChecks(context, input) {
147
- const where = input.workId ? "project_id = ? AND run_id = ? AND work_id = ? AND status = 'running'" : "project_id = ? AND run_id = ? AND status = 'running'";
283
+ let where = input.workId ? "project_id = ? AND run_id = ? AND work_id = ? AND status = 'running'" : "project_id = ? AND run_id = ? AND status = 'running'";
148
284
  const params = input.workId ? [input.projectId, input.runId, input.workId] : [input.projectId, input.runId];
285
+ if (input.receiptId) {
286
+ where += " AND id = ?";
287
+ params.push(input.receiptId);
288
+ }
149
289
  const pending = context.db.all(`SELECT id, receipt_path, started_at FROM check_receipts WHERE ${where}`, params);
150
290
  if (pending.length === 0)
151
291
  return [];
152
292
  const processes = managedProcessStatus(context);
153
293
  const reconciled = [];
154
294
  for (const pendingReceipt of pending) {
155
- const process = processes.filter((item) => item.check_id === pendingReceipt.id).at(-1);
295
+ const process = processes.filter((item) => item.check_id === pendingReceipt.id && item.project_id === input.projectId && item.run_id === input.runId).at(-1);
156
296
  const receipt = readReceipt(pendingReceipt.receipt_path);
157
297
  const completion = receipt ? readCompletion(receipt.completion_path || path.join(path.dirname(pendingReceipt.receipt_path), "completion.json")) : null;
158
- if (process && ["starting", "running", "stopping"].includes(process.state) && processLeaseIsCurrent(process) && (process.state === "starting" || processIsAlive(process))) {
298
+ // A stale lease after host sleep is not permission to duplicate a living
299
+ // check. Its durable completion marker or explicit reconciliation decides
300
+ // the outcome before another invocation may start.
301
+ // A confirmed PID plus its birth time is sufficient to prove that this
302
+ // exact check writer is gone. Do not retain a dead receipt merely because
303
+ // older registrations lack the optional parent-owner identity.
304
+ const processProvenDead = Boolean(process?.pid && process.pid_started_at && !processTreeIsAlive(process));
305
+ if (process && !processProvenDead && (processTreeIsAlive(process) || (["starting", "running", "stopping", "orphaned"].includes(process.state) && managedOwnerIsAlive(process) !== false))) {
159
306
  throw new AppError("check_in_progress", "A previous check invocation is still running", 1, { check_receipt_id: pendingReceipt.id, process_id: process.id, pid: process.pid });
160
307
  }
161
308
  if (!process && !completion && Date.now() - Date.parse(pendingReceipt.started_at) < receiptStartingGraceMs) {
162
309
  throw new AppError("check_in_progress", "A previous check invocation is still initializing", 1, { check_receipt_id: pendingReceipt.id, initializing: true });
163
310
  }
164
311
  const after = workspaceState(input.workspaceRoot, input.runHome ? [input.runHome] : []);
165
- const artifacts = receipt ? collectRequiredArtifacts(path.join(path.dirname(receipt.completion_path || pendingReceipt.receipt_path), "artifacts"), receipt.required_artifacts ?? []) : { complete: false, missing: [], items: [] };
312
+ const artifacts = receipt ? collectCheckArtifacts(path.join(path.dirname(receipt.completion_path || pendingReceipt.receipt_path), "artifacts"), receipt.required_artifacts ?? []) : { complete: false, missing: [], items: [] };
166
313
  const workspaceUnchanged = receipt?.before_fingerprint === after.fingerprint;
167
314
  const status = completion === null ? "aborted" : completion.exit_code === 0 && workspaceUnchanged && artifacts.complete ? "passed" : "failed";
168
315
  const exitCode = completion?.exit_code ?? null;
@@ -178,25 +325,64 @@ export function reconcileUnfinishedChecks(context, input) {
178
325
  }
179
326
  return reconciled;
180
327
  }
181
- function reserveReceipt(context, input) {
182
- context.db.exec("BEGIN IMMEDIATE");
183
- try {
184
- const rows = context.db.all("SELECT id FROM check_receipts WHERE work_id IS ? AND run_id = ?", [input.workId ?? null, input.runId]);
185
- const ordinal = rows.reduce((max, row) => Math.max(max, Number(/\/RCP-(\d+)$/.exec(row.id)?.[1] ?? 0)), 0) + 1;
186
- const localId = `RCP-${String(ordinal).padStart(3, "0")}`;
187
- const id = `${input.workId ?? input.runId}/${localId}`;
328
+ function reserveOrReuseReceipt(context, input) {
329
+ // Receipt files and artifact hashes are prepared before reserving SQLite.
330
+ // The compact SQL snapshot below fences races with another check settlement.
331
+ const snapshotSql = "SELECT id, status, exit_code, input_hash, profile_hash, command, gate, retry_of_receipt_id, receipt_path FROM check_receipts WHERE project_id = ? AND run_id = ? ORDER BY reservation_sequence, rowid";
332
+ const snapshot = JSON.stringify(context.db.all(snapshotSql, [input.projectId, input.runId]));
333
+ const receipts = checkReceipts(context, { projectId: input.projectId, runId: input.runId });
334
+ if (JSON.stringify(context.db.all(snapshotSql, [input.projectId, input.runId])) !== snapshot)
335
+ throw new AppError("check_receipt_changed", "Check receipts changed during evidence preparation", 1);
336
+ const prior = receipts.filter((receipt) => receipt.input_hash === input.inputHash && receipt.profile_hash === input.profileHash).at(-1) ?? null;
337
+ const reusablePrior = prior?.status === "passed" && checkReceiptEvidenceValid(prior);
338
+ const retryIds = new Set([...(input.retryReceiptId ? [input.retryReceiptId] : []), ...(input.retryReceiptIds ?? [])]);
339
+ const successor = retryIds.size === 1 ? receipts.find((receipt) => receipt.retry_of_receipt_id === [...retryIds][0] && receipt.input_hash === input.inputHash && receipt.profile_hash === input.profileHash) : null;
340
+ const reusableSuccessor = successor?.status === "passed" && checkReceiptEvidenceValid(successor);
341
+ return context.db.writeTransaction(() => {
342
+ assertRunMutationAllowed(context, input.projectId, input.runId, input.generation);
343
+ if (JSON.stringify(context.db.all(snapshotSql, [input.projectId, input.runId])) !== snapshot)
344
+ throw new AppError("check_receipt_changed", "Check receipts changed before reservation", 1);
345
+ if (prior?.status === "running")
346
+ throw new AppError("check_in_progress", "A matching check invocation is still running", 1, { check_receipt_id: prior.id });
347
+ if (retryIds.size > 1)
348
+ throw new AppError("retry_check_invalid", "A check execution accepts exactly one retry predecessor", 2);
349
+ const retryId = [...retryIds][0];
350
+ if (retryId) {
351
+ if (!input.retryReason?.trim())
352
+ throw new AppError("retry_check_invalid", "A check retry requires the restored-condition reason", 2, { check_receipt_id: retryId });
353
+ const predecessor = receipts.find(receipt => receipt.id === retryId);
354
+ if (!predecessor || !["failed", "aborted"].includes(predecessor.status) || predecessor.input_hash !== input.inputHash || predecessor.profile_hash !== input.profileHash || predecessor.command !== input.group.command || predecessor.gate !== input.group.gate)
355
+ throw new AppError("retry_check_invalid", "Retry predecessor does not match this exact check contract", 2, { check_receipt_id: retryId });
356
+ if (prior?.id !== (successor?.id ?? predecessor.id))
357
+ throw new AppError("retry_check_invalid", "This retry was superseded by a newer check outcome; inspect the latest receipt", 2, { check_receipt_id: retryId, latest_check_receipt_id: prior?.id ?? null, latest_status: prior?.status ?? null });
358
+ if (successor?.status === "running")
359
+ throw new AppError("check_in_progress", "The requested retry is already running", 1, { check_receipt_id: successor.id, retry_of_receipt_id: retryId });
360
+ if (successor) {
361
+ if (reusableSuccessor)
362
+ return { receipt: successor };
363
+ throw new AppError("retry_check_invalid", "The requested retry already has a terminal outcome; choose its failed receipt for another retry", 2, { check_receipt_id: successor.id, retry_of_receipt_id: retryId, status: successor.status });
364
+ }
365
+ }
366
+ // Only success is reusable evidence. A new finish attempt must execute a
367
+ // failed check again: repairing dependencies/services need not change any
368
+ // tracked input. Lifecycle invocation replay owns command idempotency;
369
+ // the running-receipt guard above still prevents concurrent execution.
370
+ if (prior?.status === "passed" && !retryIds.has(prior.id) && input.group.reusable && reusablePrior)
371
+ return { receipt: prior };
372
+ let ordinal = (context.db.get("SELECT COALESCE(MAX(reservation_sequence), 0) + 1 AS sequence FROM check_receipts WHERE project_id = ? AND run_id = ?", [input.projectId, input.runId])?.sequence ?? 1);
373
+ let localId = `RCP-${String(ordinal).padStart(3, "0")}`, id = `${input.workId ?? input.runId}/${localId}`;
374
+ while (context.db.get("SELECT 1 FROM check_receipts WHERE id = ?", [id])) {
375
+ ordinal += 1;
376
+ localId = `RCP-${String(ordinal).padStart(3, "0")}`;
377
+ id = `${input.workId ?? input.runId}/${localId}`;
378
+ }
188
379
  const directory = path.join(input.runHome, input.artifactDir ?? "05-code", "checks", localId);
189
- const reserved = { id, localId, directory, evidenceDir: path.join(directory, "artifacts"), stdoutPath: path.join(directory, "stdout.log"), stderrPath: path.join(directory, "stderr.log"), receiptPath: path.join(directory, "receipt.json"), completionPath: path.join(directory, "completion.json"), group: input.group, before: input.before, profileHash: input.profileHash, inputHash: input.inputHash, epoch: input.epoch, startedAt: input.startedAt, scope: input.scope, ...(input.workId ? { workId: input.workId } : {}), projectId: input.projectId, runId: input.runId };
190
- context.db.run("INSERT INTO check_receipts (id, project_id, run_id, work_id, scope, declaration_id, check_refs_json, gate, command, input_hash, verification_epoch, status, exit_code, stdout_path, stderr_path, receipt_path, workspace_fingerprint, before_fingerprint, after_fingerprint, profile_hash, mutation_paths_json, artifacts_json, started_at, finished_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'running', NULL, ?, ?, ?, ?, ?, ?, ?, '[]', '[]', ?, ?)", [id, input.projectId, input.runId, input.workId ?? null, input.scope, input.group.declarations[0].id, JSON.stringify(input.group.declarations.map(checkRef)), input.group.gate, input.group.command, input.inputHash, input.epoch, reserved.stdoutPath, reserved.stderrPath, reserved.receiptPath, input.before.fingerprint, input.before.fingerprint, input.before.fingerprint, input.profileHash, input.startedAt, input.startedAt]);
191
- context.db.exec("COMMIT");
192
- return reserved;
193
- }
194
- catch (error) {
195
- context.db.exec("ROLLBACK");
196
- throw error;
197
- }
380
+ const reserved = { id, localId, directory, evidenceDir: path.join(directory, "artifacts"), stdoutPath: path.join(directory, "stdout.log"), stderrPath: path.join(directory, "stderr.log"), receiptPath: path.join(directory, "receipt.json"), completionPath: path.join(directory, "completion.json"), group: input.group, before: input.before, profileHash: input.profileHash, inputHash: input.inputHash, epoch: input.epoch, startedAt: input.startedAt, scope: input.scope, ...(retryId ? { retryOf: retryId, retryReason: input.retryReason } : {}), ...(input.workId ? { workId: input.workId } : {}), projectId: input.projectId, runId: input.runId };
381
+ context.db.run("INSERT INTO check_receipts (id, project_id, run_id, work_id, scope, declaration_id, check_refs_json, gate, command, input_hash, verification_epoch, reservation_sequence, retry_of_receipt_id, retry_reason, status, exit_code, stdout_path, stderr_path, receipt_path, workspace_fingerprint, before_fingerprint, after_fingerprint, profile_hash, mutation_paths_json, artifacts_json, started_at, finished_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'running', NULL, ?, ?, ?, ?, ?, ?, ?, '[]', '[]', ?, ?)", [id, input.projectId, input.runId, input.workId ?? null, input.scope, input.group.declarations[0].id, JSON.stringify(input.group.declarations.map(checkRef).sort()), input.group.gate, input.group.command, input.inputHash, input.epoch, ordinal, reserved.retryOf ?? null, reserved.retryReason ?? null, reserved.stdoutPath, reserved.stderrPath, reserved.receiptPath, input.before.fingerprint, input.before.fingerprint, input.before.fingerprint, input.profileHash, input.startedAt, input.startedAt]);
382
+ return { reserved };
383
+ }, "checks.reserveOrReuseReceipt");
198
384
  }
199
- function receiptFor(input) { return { id: input.id, local_id: input.localId, declaration_id: input.group.declarations[0].id, check_refs: input.group.declarations.map(checkRef), gate: input.group.gate, scope: input.scope, work_id: input.workId ?? null, command: input.group.command, input_hash: input.inputHash, verification_epoch: input.epoch, status: input.status, exit_code: input.status === "running" ? null : input.result.exitCode, stdout_path: input.stdoutPath, stderr_path: input.stderrPath, receipt_path: input.receiptPath, completion_path: input.completionPath, required_artifacts: input.group.requiredArtifacts, workspace_fingerprint: input.before.fingerprint, before_fingerprint: input.before.fingerprint, after_fingerprint: input.after.fingerprint, profile_hash: input.profileHash, mutation_paths: input.mutationPaths, artifacts: input.artifacts, resources: { ports: input.resources }, started_at: input.startedAt, finished_at: input.status === "running" ? input.startedAt : new Date().toISOString() }; }
385
+ function receiptFor(input) { return { id: input.id, local_id: input.localId, declaration_id: input.group.declarations[0].id, check_refs: input.group.declarations.map(checkRef), gate: input.group.gate, scope: input.scope, work_id: input.workId ?? null, command: input.group.command, input_hash: input.inputHash, verification_epoch: input.epoch, retry_of_receipt_id: input.retryOf ?? null, retry_reason: input.retryReason ?? null, status: input.status, exit_code: input.status === "running" ? null : input.result.exitCode, stdout_path: input.stdoutPath, stderr_path: input.stderrPath, receipt_path: input.receiptPath, completion_path: input.completionPath, required_artifacts: input.group.requiredArtifacts, inputs: input.group.inputs, workspace_fingerprint: input.before.fingerprint, before_fingerprint: input.before.fingerprint, after_fingerprint: input.after.fingerprint, profile_hash: input.profileHash, mutation_paths: input.mutationPaths, artifacts: input.artifacts, resources: { ports: input.resources }, started_at: input.startedAt, finished_at: input.status === "running" ? input.startedAt : new Date().toISOString() }; }
200
386
  function writeReceipt(file, receipt) { const temporary = `${file}.tmp-${process.pid}-${crypto.randomUUID()}`; fs.writeFileSync(temporary, `${JSON.stringify(receipt, null, 2)}\n`); fs.renameSync(temporary, file); }
201
387
  function readReceipt(file) { try {
202
388
  return JSON.parse(fs.readFileSync(file, "utf8"));
@@ -212,31 +398,264 @@ function readCompletion(file) { try {
212
398
  catch {
213
399
  return null;
214
400
  } }
215
- function processLeaseIsCurrent(process) { return Date.parse(process.lease_expires_at ?? "") >= Date.now(); }
216
- export function unchangedFinalGateFailures(context, input) { const wanted = new Set(input.declarations.flatMap((check) => [check.id, checkRef(check)])); const current = workspaceFingerprint(input.workspaceRoot); const latest = new Map(); for (const receipt of checkReceipts(context, { projectId: input.projectId, runId: input.runId }).filter((item) => item.scope === "aggregate" && item.check_refs.some((ref) => wanted.has(ref))))
217
- for (const ref of receipt.check_refs)
218
- latest.set(ref, receipt); return [...new Set(latest.values())].filter((receipt) => receipt.status === "failed" && receipt.before_fingerprint === current); }
401
+ /** Validate an explicit retry; the caller still executes a fresh real gate and retains a new receipt. */
402
+ export function validateFailedCheckRetry(context, input) {
403
+ if (!input.reason?.trim())
404
+ throw new AppError("retry_check_invalid", "--retry-check requires --reason describing the restored environment", 2);
405
+ const receipt = checkReceipts(context, { projectId: input.projectId, runId: input.runId }).find((item) => item.id === input.receiptId);
406
+ if (!receipt || !["failed", "aborted"].includes(receipt.status))
407
+ throw new AppError("retry_check_invalid", "--retry-check must name a failed or aborted receipt in this RUN", 2, { check_receipt_id: input.receiptId });
408
+ const declared = new Set(input.declarations.flatMap((check) => [check.id, checkRef(check)]));
409
+ if (!receipt.check_refs.some((ref) => declared.has(ref)))
410
+ throw new AppError("retry_check_invalid", "--retry-check does not belong to this stage gate", 2, { check_receipt_id: input.receiptId, check_refs: receipt.check_refs });
411
+ return receipt;
412
+ }
219
413
  export function checkReceipts(context, input) {
220
414
  const where = input.workId ? "project_id = ? AND run_id = ? AND work_id = ?" : "project_id = ? AND run_id = ?";
221
415
  const params = input.workId ? [input.projectId, input.runId, input.workId] : [input.projectId, input.runId];
222
- return context.db.all(`SELECT id, declaration_id, check_refs_json, gate, scope, work_id, command, input_hash, verification_epoch, status, exit_code, stdout_path, stderr_path, receipt_path, workspace_fingerprint, before_fingerprint, after_fingerprint, profile_hash, mutation_paths_json, artifacts_json, started_at, finished_at FROM check_receipts WHERE ${where} ORDER BY started_at, id`, params).map((row) => ({ ...row, local_id: row.id.slice(row.id.lastIndexOf("/") + 1), check_refs: parseArray(row.check_refs_json, [row.declaration_id]), mutation_paths: parseArray(row.mutation_paths_json, []), artifacts: JSON.parse(row.artifacts_json), resources: readReceiptResources(row.receipt_path) }));
416
+ return context.db.all(`SELECT id, declaration_id, check_refs_json, gate, scope, work_id, command, input_hash, verification_epoch, retry_of_receipt_id, retry_reason, status, exit_code, stdout_path, stderr_path, receipt_path, workspace_fingerprint, before_fingerprint, after_fingerprint, profile_hash, mutation_paths_json, artifacts_json, started_at, finished_at FROM check_receipts WHERE ${where} ORDER BY reservation_sequence, rowid`, params).map((row) => ({ ...row, local_id: row.id.slice(row.id.lastIndexOf("/") + 1), check_refs: parseArray(row.check_refs_json, [row.declaration_id]), mutation_paths: parseArray(row.mutation_paths_json, []), resources: readReceiptResources(row.receipt_path), ...resolvedReceiptArtifacts(row.receipt_path, JSON.parse(row.artifacts_json)) }));
417
+ }
418
+ /** File bytes remain immutable; only the reader projects their current location. */
419
+ function resolvedReceiptArtifacts(receiptPath, artifacts) {
420
+ let original = {};
421
+ try {
422
+ original = JSON.parse(fs.readFileSync(receiptPath, "utf8"));
423
+ }
424
+ catch { /* Historical missing receipts remain visibly incomplete. */ }
425
+ const currentRoot = path.dirname(receiptPath);
426
+ const oldRoot = original.receipt_path ? path.dirname(original.receipt_path) : currentRoot;
427
+ const relocate = (file) => {
428
+ const relative = path.relative(oldRoot, file);
429
+ return !path.isAbsolute(relative) && relative !== ".." && !relative.startsWith(`..${path.sep}`) ? path.join(currentRoot, relative) : file;
430
+ };
431
+ return { completion_path: path.join(currentRoot, "completion.json"), required_artifacts: original.required_artifacts ?? [], inputs: original.inputs ?? [], artifacts: artifacts.map((artifact) => ({ ...artifact, path: relocate(artifact.path) })) };
223
432
  }
224
433
  export function workspaceFingerprint(root) { return workspaceState(root).fingerprint; }
225
- export function workspaceChangedPaths(root) { const status = spawnSync("git", ["status", "--porcelain=v1", "-z", "--untracked-files=all"], { cwd: root, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 }); if (status.status !== 0)
226
- return null; return status.stdout.split("\0").filter(Boolean).map((entry) => entry.slice(3)).filter(Boolean).sort(); }
227
- function deduplicate(checks, commands) { const groups = new Map(); checks.forEach((check, index) => { const artifacts = [...(check.required_artifacts ?? [])].sort(); const ports = [...(check.ports ?? [])].sort(); const key = JSON.stringify([check.run_at, commands[index], artifacts, ports]); const current = groups.get(key); if (current)
228
- current.declarations.push(check);
229
- else
230
- groups.set(key, { gate: check.run_at, command: commands[index], declarations: [check], requiredArtifacts: artifacts, ports }); }); return [...groups.values()]; }
231
- function workspaceState(root, ignoredRoots = []) { const git = spawnSync("git", ["ls-files", "-co", "--exclude-standard", "-z"], { cwd: root, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 }); const ignored = ignoredRoots.map((entry) => path.resolve(entry)); const listing = (git.status === 0 ? git.stdout.split("\0").filter(Boolean).sort() : listFiles(root)).filter((relative) => { const file = path.resolve(root, relative); return !ignored.some((ignoredRoot) => file === ignoredRoot || file.startsWith(`${ignoredRoot}${path.sep}`)); }); const files = new Map(); const hash = crypto.createHash("sha256"); for (const relative of listing) {
232
- const file = path.join(root, relative);
233
- const content = fs.existsSync(file) && fs.statSync(file).isFile() ? fs.readFileSync(file) : Buffer.from("<missing>");
234
- const digest = crypto.createHash("sha256").update(content).digest("hex");
235
- files.set(relative, digest);
236
- hash.update(relative).update("\0").update(digest).update("\0");
237
- } return { fingerprint: hash.digest("hex"), files }; }
434
+ /** One NUL-aware source of truth for paths differing from HEAD. Rename/copy
435
+ * records carry a second path; retain both because the old side is a deletion. */
436
+ export function workspaceChangedPaths(root) {
437
+ const status = spawnSync("git", ["status", "--porcelain=v1", "-z", "--untracked-files=all"], { cwd: root, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
438
+ // A workspace can be intentionally gitless. Keep the same conservative
439
+ // file inventory used for fingerprints instead of making result validation
440
+ // fail before the work contract can report its actual error.
441
+ if (isGitlessWorkspace(root, status))
442
+ return listFiles(root);
443
+ if (status.status !== 0 || status.error)
444
+ throw new AppError("workspace_status_failed", "Could not determine changed workspace paths", 1, { workspace_root: root, stderr: status.stderr || null, cause: status.error?.message ?? null });
445
+ const records = status.stdout.split("\0");
446
+ const paths = [];
447
+ for (let index = 0; index < records.length; index += 1) {
448
+ const record = records[index];
449
+ if (!record)
450
+ continue;
451
+ if (record.length < 4 || record[2] !== " ")
452
+ throw new AppError("workspace_status_invalid", "Git returned an invalid porcelain-v1 record", 1, { workspace_root: root, record });
453
+ paths.push(normalizeWorkspacePath(root, record.slice(3)));
454
+ if (record[0] === "R" || record[0] === "C" || record[1] === "R" || record[1] === "C") {
455
+ const prior = records[++index];
456
+ if (!prior)
457
+ throw new AppError("workspace_status_invalid", "Git rename/copy record is missing its second path", 1, { workspace_root: root, record });
458
+ paths.push(normalizeWorkspacePath(root, prior));
459
+ }
460
+ }
461
+ const ownedHook = paths.includes(".agents/hooks.json") ? ownedWorkspaceHookPath(root) : null;
462
+ return [...new Set(paths)].filter(file => file !== ownedHook).sort();
463
+ }
464
+ /** Ignored machine state cannot be reported as a product change. Tracked paths
465
+ * remain eligible regardless of their directory name. */
466
+ export function ignoredWorkspacePaths(root, paths) {
467
+ const normalized = [...new Set(paths.map((item) => normalizeWorkspacePath(root, item)))];
468
+ if (!normalized.length)
469
+ return [];
470
+ const status = spawnSync("git", ["check-ignore", "-z", "--stdin"], { cwd: root, encoding: "utf8", input: `${normalized.join("\0")}\0`, maxBuffer: 64 * 1024 * 1024 });
471
+ // The canonical owned hook has one meaning across Git status, check cache
472
+ // and MERGE, without changing a sibling worktree's shared Git excludes.
473
+ const gitless = isGitlessWorkspace(root, status);
474
+ if (!gitless && (![0, 1].includes(status.status ?? -1) || status.error))
475
+ throw new AppError("workspace_ignore_check_failed", "Could not classify reported workspace paths", 1, { workspace_root: root, stderr: status.stderr || null, cause: status.error?.message ?? null });
476
+ const ownedHook = normalized.includes(".agents/hooks.json") ? ownedWorkspaceHookPath(root, gitless ? [] : undefined) : null;
477
+ if (gitless)
478
+ return ownedHook ? [ownedHook] : [];
479
+ return [...new Set([...status.stdout.split("\0").filter(Boolean).map((item) => normalizeWorkspacePath(root, item)), ...(ownedHook ? [ownedHook] : [])])].sort();
480
+ }
481
+ export function normalizeWorkspacePath(root, value) {
482
+ if (typeof value !== "string" || !value || value.includes("\0") || path.isAbsolute(value))
483
+ throw new AppError("workspace_path_invalid", "Workspace path must be a non-empty relative path", 2, { path: value });
484
+ const resolvedRoot = path.resolve(root);
485
+ const resolved = path.resolve(resolvedRoot, value);
486
+ const relative = path.relative(resolvedRoot, resolved);
487
+ if (!relative || relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative))
488
+ throw new AppError("workspace_path_invalid", "Workspace path escapes its root", 2, { path: value, workspace_root: root });
489
+ return relative.split(path.sep).join("/");
490
+ }
491
+ function deduplicate(checks, commands) {
492
+ const groups = new Map();
493
+ checks.forEach((check, index) => {
494
+ const artifacts = [...(check.required_artifacts ?? [])].sort(), ports = [...(check.ports ?? [])].sort(), inputs = normalizedInputs(check.inputs ?? []);
495
+ const reusable = check.reuse === "deterministic";
496
+ // Stage labels identify obligations, not different processes in this gate.
497
+ // Cross-invocation reuse still requires the explicit deterministic contract.
498
+ const key = JSON.stringify([commands[index], artifacts, ports, inputs, reusable]);
499
+ const current = groups.get(key);
500
+ if (current)
501
+ current.declarations.push(check);
502
+ else
503
+ groups.set(key, { gate: check.run_at, command: commands[index], declarations: [check], requiredArtifacts: artifacts, ports, inputs, reusable });
504
+ });
505
+ return [...groups.values()].map(group => ({ ...group, gate: [...group.declarations].map(check => check.run_at).sort()[0], declarations: [...group.declarations].sort((left, right) => checkRef(left).localeCompare(checkRef(right))) }));
506
+ }
507
+ function workspaceState(root, ignoredRoots = []) {
508
+ const git = workspaceGitFiles(root, ["-co", "--exclude-standard"]);
509
+ const ignored = ignoredRoots.map(entry => path.resolve(entry));
510
+ const listing = [...new Set(git ?? listFiles(root))].sort();
511
+ const ownedHook = listing.includes(".agents/hooks.json") ? ownedWorkspaceHookPath(root, git ? undefined : []) : null;
512
+ const files = new Map(), hash = crypto.createHash("sha256");
513
+ for (const relative of listing) {
514
+ if (relative === ownedHook)
515
+ continue;
516
+ const file = path.resolve(root, relative);
517
+ if (ignored.some(ignoredRoot => file === ignoredRoot || file.startsWith(`${ignoredRoot}${path.sep}`)))
518
+ continue;
519
+ // Deleted tracked paths are absent content, both before and after commit.
520
+ // Do not mask other IO errors as an unchanged workspace.
521
+ let stat;
522
+ try {
523
+ stat = fs.lstatSync(file);
524
+ }
525
+ catch (error) {
526
+ if (error.code === "ENOENT")
527
+ continue;
528
+ throw error;
529
+ }
530
+ const content = stat.isSymbolicLink() ? Buffer.from(`symlink\0${fs.readlinkSync(file)}`) : stat.isFile() ? fs.readFileSync(file) : Buffer.from("<directory>");
531
+ const digest = crypto.createHash("sha256").update(content).digest("hex");
532
+ files.set(relative, digest);
533
+ hash.update(relative).update("\0").update(digest).update("\0");
534
+ }
535
+ return { fingerprint: hash.digest("hex"), files };
536
+ }
537
+ function workspaceInputFingerprint(root, files, inputs, ignoredRoots) {
538
+ const selected = new Map(inputs.length === 0 || inputs.includes(".") ? files : []);
539
+ for (const [file, digest] of explicitInputFiles(root, inputs.filter(input => input !== "."), ignoredRoots))
540
+ selected.set(file, digest);
541
+ const hash = crypto.createHash("sha256");
542
+ for (const [file, digest] of [...selected].sort(([left], [right]) => left.localeCompare(right)))
543
+ hash.update(file).update("\0").update(digest).update("\0");
544
+ return hash.digest("hex");
545
+ }
238
546
  function changedPaths(before, after) { return [...new Set([...before.keys(), ...after.keys()])].filter((key) => before.get(key) !== after.get(key)).sort(); }
239
547
  function checkRef(check) { return check.canonical_ref ?? check.id; }
548
+ function assertConsistentDeclarations(checks, commands) {
549
+ const seen = new Map();
550
+ for (const [index, check] of checks.entries()) {
551
+ const contract = JSON.stringify([check.run_at, commands[index], [...(check.required_artifacts ?? [])].sort(), [...(check.ports ?? [])].sort(), normalizedInputs(check.inputs ?? []), check.reuse ?? null]);
552
+ const ref = checkRef(check), prior = seen.get(ref);
553
+ if (prior && prior !== contract)
554
+ throw new AppError("check_declaration_conflict", "The same check reference has conflicting execution definitions", 2, { check_ref: ref });
555
+ seen.set(ref, contract);
556
+ }
557
+ }
558
+ function environmentFingerprint(workspaceRoot) {
559
+ const environment = codeExecutionEnvironment(workspaceRoot);
560
+ return crypto.createHash("sha256").update(JSON.stringify(Object.entries(environment).filter(([, value]) => value !== undefined).sort(([left], [right]) => left.localeCompare(right)))).digest("hex");
561
+ }
562
+ function acquireWorkspaceGate(context, input) {
563
+ const operationId = workspaceOperationId(input.workspaceRoot);
564
+ const active = managedProcessStatus(context).find(item => item.operation_id === operationId && ["starting", "running", "stopping"].includes(item.state));
565
+ if (active)
566
+ throw new AppError("check_in_progress", "The workspace is owned by another bootstrap or check; observe it before starting a new check", 1, { process_id: active.id, operation_id: operationId });
567
+ return registerManagedProcess(context, { kind: "workspace-gate", ownerPid: process.pid, ownerId: `workspace-gate:${crypto.randomUUID()}`, operationId, uniqueActiveOperation: true, projectId: input.projectId, runId: input.runId, metadata: { workspace_root: input.workspaceRoot, role: "check_gate" } });
568
+ }
569
+ export function reconcileWorkspaceOwnership(context, workspaceRoot) {
570
+ const operationId = workspaceOperationId(workspaceRoot);
571
+ const active = managedProcessStatus(context).filter(item => item.operation_id === operationId && ["starting", "running", "stopping", "orphaned"].includes(item.state));
572
+ if (active.some(item => processTreeIsAlive(item) || managedOwnerIsAlive(item) !== false))
573
+ return;
574
+ // Registry IDs are host-wide, whereas receipts belong to one runtime DB.
575
+ // An unknown/foreign home must not be settled against this client's rows.
576
+ if (active.some(item => managedProcessRuntimeHome(item, context) !== context.ddFlowHome))
577
+ return;
578
+ for (const child of active.filter(item => item.kind !== "workspace-gate")) {
579
+ if (child.kind !== "check" || !child.project_id || !child.run_id || !child.check_id)
580
+ return;
581
+ const run = context.db.get("SELECT run_root FROM runs WHERE project_id = ? AND id = ?", [child.project_id, child.run_id]);
582
+ if (!run?.run_root || !context.db.get("SELECT 1 FROM check_receipts WHERE id = ? AND project_id = ? AND run_id = ?", [child.check_id, child.project_id, child.run_id]))
583
+ return;
584
+ reconcileUnfinishedChecks(context, { projectId: child.project_id, runId: child.run_id, workspaceRoot, runHome: run.run_root, receiptId: child.check_id });
585
+ // A crash may occur after receipt settlement but before registry settlement.
586
+ const current = managedProcessStatus(context).find(item => item.id === child.id);
587
+ if (!current || current.lease_token !== child.lease_token)
588
+ return;
589
+ if (!["stopped", "failed"].includes(current.state) && !finishManagedProcess(context, { id: child.id, leaseToken: child.lease_token, state: "stopped", reason: "workspace_check_owner_reconciled" }))
590
+ return;
591
+ }
592
+ for (const gate of active.filter(item => item.kind === "workspace-gate"))
593
+ finishManagedProcess(context, { id: gate.id, leaseToken: gate.lease_token, state: "stopped", reason: "workspace_gate_owner_reconciled" });
594
+ }
595
+ function normalizedInputs(inputs) { for (const input of inputs)
596
+ validateCheckInputSyntax(input); return [...new Set(inputs.map((input) => { const normalized = path.posix.normalize(input.replaceAll("\\", "/")).replace(/^\.\/$/, ""); return normalized === "" || normalized === "." ? "." : normalized.replace(/\/$/, ""); }))].sort(); }
597
+ function validateCheckInputs(workspaceRoot, inputs) { const root = fs.realpathSync.native(workspaceRoot); for (const input of normalizedInputs(inputs)) {
598
+ if (input === ".git" || input.startsWith(".git/"))
599
+ throw new AppError("invalid_check_input", "Check inputs cannot include Git internals", 2, { input });
600
+ const candidate = path.resolve(workspaceRoot, input);
601
+ if (candidate !== workspaceRoot && !candidate.startsWith(`${workspaceRoot}${path.sep}`))
602
+ throw new AppError("invalid_check_input", "Check inputs must stay within the workspace", 2, { input });
603
+ if (fs.existsSync(candidate))
604
+ validateInputTree(root, candidate, input, new Set());
605
+ else
606
+ validateMissingInputParent(root, candidate, input);
607
+ } }
608
+ function validateCheckInputSyntax(input) { if (typeof input !== "string" || !input.trim() || path.isAbsolute(input) || input.split(/[\\/]/).includes(".."))
609
+ throw new AppError("invalid_check_input", "Check inputs must be non-empty relative workspace paths", 2, { input }); }
610
+ function validateMissingInputParent(root, candidate, input) { let parent = candidate; while (!fs.existsSync(parent))
611
+ parent = path.dirname(parent); const resolved = fs.realpathSync.native(parent); if (resolved !== root && !resolved.startsWith(`${root}${path.sep}`))
612
+ throw new AppError("invalid_check_input", "Check input parent symlink escapes the workspace", 2, { input }); }
613
+ function validateInputTree(root, candidate, input, seen) {
614
+ const resolved = fs.realpathSync.native(candidate);
615
+ if (resolved !== root && !resolved.startsWith(`${root}${path.sep}`))
616
+ throw new AppError("invalid_check_input", "Check input symlink escapes the workspace", 2, { input });
617
+ if (seen.has(resolved))
618
+ throw new AppError("invalid_check_input", "Check input contains a symbolic-link cycle", 2, { input });
619
+ seen.add(resolved);
620
+ const stat = fs.statSync(candidate);
621
+ if (stat.isDirectory())
622
+ for (const entry of fs.readdirSync(candidate))
623
+ validateInputTree(root, path.join(candidate, entry), input, seen);
624
+ seen.delete(resolved);
625
+ }
626
+ function explicitInputFiles(root, inputs, ignoredRoots) {
627
+ const rootReal = fs.realpathSync.native(root), ignored = ignoredRoots.map(entry => path.resolve(entry)), result = new Map();
628
+ const visit = (file, relative, seen) => {
629
+ if (ignored.some(entry => file === entry || file.startsWith(`${entry}${path.sep}`)))
630
+ return;
631
+ if (!fs.existsSync(file)) {
632
+ result.set(relative, "<missing>");
633
+ return;
634
+ }
635
+ const resolved = fs.realpathSync.native(file);
636
+ if (resolved !== rootReal && !resolved.startsWith(`${rootReal}${path.sep}`))
637
+ throw new AppError("invalid_check_input", "Check input symlink escapes the workspace", 2, { input: relative });
638
+ if (seen.has(resolved))
639
+ throw new AppError("invalid_check_input", "Check input contains a symbolic-link cycle", 2, { input: relative });
640
+ const stat = fs.statSync(file);
641
+ if (stat.isDirectory()) {
642
+ seen.add(resolved);
643
+ for (const entry of fs.readdirSync(file).sort())
644
+ visit(path.join(file, entry), path.posix.join(relative, entry), seen);
645
+ seen.delete(resolved);
646
+ }
647
+ else if (stat.isFile())
648
+ result.set(relative, crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"));
649
+ };
650
+ for (const input of inputs)
651
+ visit(path.resolve(root, input), input, new Set());
652
+ return [...result.entries()];
653
+ }
654
+ function checkInputHash(workspaceRoot, group, profileHash, inputFingerprint) { return crypto.createHash("sha256").update(JSON.stringify({ workspace_root: path.resolve(workspaceRoot), gate: group.reusable ? null : group.gate, command: group.command, required_artifacts: group.requiredArtifacts, ports: group.ports, inputs: group.inputs, profile_hash: profileHash, environment: environmentFingerprint(workspaceRoot), input_fingerprint: inputFingerprint })).digest("hex"); }
655
+ function bindReceipt(receipt, group) { return { ...receipt, gate: group.gate, declaration_id: group.declarations[0].id, check_refs: group.declarations.map(checkRef), required_artifacts: group.requiredArtifacts, inputs: group.inputs }; }
656
+ export function checkReceiptEvidenceValid(receipt) { if (receipt.status !== "passed" || receipt.exit_code !== 0 || receipt.before_fingerprint !== receipt.after_fingerprint || !fs.existsSync(receipt.completion_path) || readCompletion(receipt.completion_path)?.exit_code !== 0)
657
+ return false; const stored = readReceipt(receipt.receipt_path); if (!stored || stored.status !== "passed" || stored.input_hash !== receipt.input_hash || stored.profile_hash !== receipt.profile_hash)
658
+ return false; const artifacts = collectCheckArtifacts(path.join(path.dirname(receipt.receipt_path), "artifacts"), stored.required_artifacts ?? []); return artifacts.complete && JSON.stringify(artifacts.items) === JSON.stringify(stored.artifacts ?? []); }
240
659
  function parseArray(value, fallback) { try {
241
660
  const parsed = JSON.parse(value);
242
661
  return Array.isArray(parsed) && parsed.every((item) => typeof item === "string") ? parsed : fallback;
@@ -245,8 +664,14 @@ catch {
245
664
  return fallback;
246
665
  } }
247
666
  function isStringRecord(value) { return Boolean(value) && typeof value === "object" && !Array.isArray(value) && Object.values(value).every((item) => typeof item === "string" && item.trim().length > 0); }
248
- function isGateRecord(value) { return Boolean(value) && typeof value === "object" && !Array.isArray(value) && Object.entries(value).every(([gate, aliases]) => ["work", "code", "readiness", "merge", "release", "external"].includes(gate) && Array.isArray(aliases) && aliases.every((item) => typeof item === "string" && item.startsWith("@check/"))); }
667
+ function isGateRecord(value) { return Boolean(value) && typeof value === "object" && !Array.isArray(value) && Object.entries(value).every(([gate, aliases]) => ["work", "code", "readiness", "merge", "release"].includes(gate) && Array.isArray(aliases) && aliases.every((item) => typeof item === "string" && item.startsWith("@check/"))); }
249
668
  function isPortAliasRecord(value) { return value === undefined || (Boolean(value) && typeof value === "object" && !Array.isArray(value) && Object.entries(value).every(([alias, ports]) => alias.startsWith("@check/") && Array.isArray(ports) && ports.length > 0 && new Set(ports).size === ports.length && ports.every((port) => typeof port === "string" && /^[A-Za-z][A-Za-z0-9_]*$/.test(port)))); }
669
+ function isInputAliasRecord(workspaceRoot, value) { try {
670
+ return value === undefined || (Boolean(value) && typeof value === "object" && !Array.isArray(value) && Object.entries(value).every(([alias, inputs]) => alias.startsWith("@check/") && Array.isArray(inputs) && (validateCheckInputs(workspaceRoot, inputs), true)));
671
+ }
672
+ catch {
673
+ return false;
674
+ } }
250
675
  function portEnvironment(ports) { return Object.fromEntries(Object.entries(ports).map(([name, value]) => [`DD_FLOW_PORT_${name.toUpperCase()}`, String(value)])); }
251
676
  function readReceiptResources(file) { try {
252
677
  const value = JSON.parse(fs.readFileSync(file, "utf8"));
@@ -255,50 +680,108 @@ function readReceiptResources(file) { try {
255
680
  catch {
256
681
  return { ports: {} };
257
682
  } }
258
- function collectRequiredArtifacts(root, required) { const missing = []; const items = []; for (const relative of required) {
259
- if (path.isAbsolute(relative) || relative.split(/[\\/]/).includes(".."))
260
- throw new AppError("invalid_evidence_path", "Required evidence paths must be relative to DD_FLOW_EVIDENCE_DIR", 2, { path: relative });
261
- const file = path.join(root, relative);
262
- if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {
263
- missing.push(relative);
264
- continue;
683
+ function collectCheckArtifacts(root, required) {
684
+ for (const relative of required) {
685
+ if (path.isAbsolute(relative) || relative.split(/[\\/]/).includes(".."))
686
+ throw new AppError("invalid_evidence_path", "Required evidence paths must be relative to DD_FLOW_EVIDENCE_DIR", 2, { path: relative });
265
687
  }
266
- items.push({ path: relative, sha256: crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex") });
267
- } return { complete: missing.length === 0, missing, items }; }
268
- function listFiles(root, current = root) { return fs.readdirSync(current, { withFileTypes: true }).flatMap((entry) => { if ([".git", "node_modules"].includes(entry.name))
269
- return []; const absolute = path.join(current, entry.name); return entry.isDirectory() ? listFiles(root, absolute) : [path.relative(root, absolute)]; }).sort(); }
270
- function runCheck(command, cwd, environment, stdout, stderr, heartbeat, onSpawn, renewLease) { return new Promise((resolve) => { const started = Date.now(); const child = spawn("/bin/sh", ["-lc", checkShellScript()], { cwd, env: { ...environment, DD_FLOW_CHECK_COMMAND: command }, stdio: ["ignore", stdout, stderr], detached: process.platform !== "win32" }); if (!child.pid) {
271
- resolve({ exitCode: null, error: "check process did not provide a PID", aborted: true });
272
- return;
273
- } try {
274
- onSpawn(child.pid);
275
- }
276
- catch (error) {
277
- terminateProcessGroup(child.pid, "SIGTERM");
278
- resolve({ exitCode: null, error: error instanceof Error ? error.message : String(error), aborted: true });
279
- return;
280
- } const inactivityMs = 15 * 60 * 1000; let timeout; const lastOutputSize = new Map(); const armInactivityTimeout = () => { if (timeout)
281
- clearTimeout(timeout); timeout = setTimeout(() => { timedOut = true; terminateProcessGroup(child.pid, "SIGTERM"); escalation = setTimeout(() => terminateProcessGroup(child.pid, "SIGKILL"), 5_000); }, inactivityMs); }; const progress = setInterval(() => { const elapsed = Math.floor((Date.now() - started) / 1000); if (!renewLease()) {
282
- error = "managed process lease lost";
283
- timedOut = true;
284
- terminateProcessGroup(child.pid, "SIGTERM");
285
- return;
286
- } for (const file of [stdout, stderr]) {
287
- try {
288
- const size = fs.fstatSync(file).size, previous = lastOutputSize.get(file) ?? 0;
289
- if (size > previous) {
290
- lastOutputSize.set(file, size);
291
- armInactivityTimeout();
688
+ // Required paths are minimum obligations; retain all explicit check evidence.
689
+ // Never follow symlinks, including a replaced evidence root.
690
+ const items = [];
691
+ const visit = (directory) => {
692
+ for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
693
+ const file = path.join(directory, entry.name);
694
+ if (entry.isDirectory())
695
+ visit(file);
696
+ else if (entry.isFile())
697
+ items.push({ path: path.relative(root, file), sha256: crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex") });
292
698
  }
293
- }
294
- catch { /* terminal stream */ }
295
- } heartbeat(elapsed); }, 15_000); let timedOut = false; let error = null; let escalation; armInactivityTimeout(); child.once("error", (value) => { error = value.message; }); child.once("close", (exitCode, signal) => { clearInterval(progress); if (timeout)
296
- clearTimeout(timeout); if (escalation)
297
- clearTimeout(escalation); resolve({ exitCode, error: error ?? (timedOut ? "timed out after 900 seconds without process output" : signal ? `terminated by ${signal}` : null), aborted: timedOut || Boolean(signal) || Boolean(error) }); }); }); }
699
+ };
700
+ if (fs.existsSync(root) && fs.lstatSync(root).isDirectory())
701
+ visit(root);
702
+ items.sort((a, b) => a.path.localeCompare(b.path));
703
+ const retained = new Set(items.map((item) => item.path));
704
+ const missing = required.filter((relative) => !retained.has(path.normalize(relative)));
705
+ return { complete: missing.length === 0, missing, items };
706
+ }
707
+ function listFiles(root) {
708
+ const allowed = projectServicePathFilter(root, [], []);
709
+ const ownedHook = ownedWorkspaceHookPath(root, []);
710
+ const visit = (current) => fs.readdirSync(current, { withFileTypes: true }).flatMap(entry => {
711
+ const absolute = path.join(current, entry.name);
712
+ if ([".git", "node_modules"].includes(entry.name) || !allowed(absolute))
713
+ return [];
714
+ return entry.isDirectory() ? visit(absolute) : [path.relative(root, absolute)];
715
+ });
716
+ return visit(root).filter(file => file.split(path.sep).join("/") !== ownedHook).sort();
717
+ }
718
+ export function runCheck(command, cwd, environment, stdout, stderr, heartbeat, onSpawn, renewLease) {
719
+ return new Promise((resolve) => {
720
+ const started = Date.now();
721
+ const child = spawn("/bin/sh", ["-c", checkShellScript()], { cwd, env: { ...environment, DD_FLOW_CHECK_COMMAND: command }, stdio: ["pipe", stdout, stderr], detached: process.platform !== "win32" });
722
+ let error = null;
723
+ let stopping = false;
724
+ let progress;
725
+ let escalation;
726
+ const stop = (cause) => {
727
+ if (stopping)
728
+ return;
729
+ stopping = true;
730
+ error = cause instanceof Error ? cause.message : String(cause);
731
+ if (progress) {
732
+ clearInterval(progress);
733
+ progress = undefined;
734
+ }
735
+ if (child.pid) {
736
+ terminateProcessGroup(child.pid, "SIGTERM");
737
+ escalation = setTimeout(() => terminateProcessGroup(child.pid, "SIGKILL"), 5_000);
738
+ }
739
+ };
740
+ // Register handlers before checking pid or calling persistence callbacks.
741
+ // Failure is not completion until the spawned process has actually closed.
742
+ child.once("error", (value) => { error = value.message; });
743
+ child.stdin.on("error", stop);
744
+ child.once("close", (exitCode, signal) => {
745
+ if (progress)
746
+ clearInterval(progress);
747
+ if (escalation)
748
+ clearTimeout(escalation);
749
+ if (stopping)
750
+ terminateProcessGroup(child.pid, "SIGKILL");
751
+ resolve({ exitCode, error: error ?? (signal ? `terminated by ${signal}` : null), aborted: stopping || Boolean(signal) || Boolean(error) });
752
+ });
753
+ if (!child.pid) {
754
+ error = "check process did not provide a PID";
755
+ return;
756
+ }
757
+ try {
758
+ onSpawn(child.pid);
759
+ }
760
+ catch (cause) {
761
+ stop(cause);
762
+ return;
763
+ }
764
+ child.stdin.end("admitted\n");
765
+ progress = setInterval(() => {
766
+ try {
767
+ if (!renewLease()) {
768
+ stop("managed process lease lost");
769
+ return;
770
+ }
771
+ heartbeat(Math.floor((Date.now() - started) / 1000));
772
+ }
773
+ catch (cause) {
774
+ stop(cause);
775
+ }
776
+ }, 15_000);
777
+ });
778
+ }
298
779
  function checkShellScript() {
299
780
  return [
781
+ // The actual command cannot run before PID ownership and admission persist.
782
+ 'IFS= read -r admission && [ "$admission" = admitted ] || exit 125',
300
783
  'completion="${DD_FLOW_CHECK_COMPLETION_FILE:-}"',
301
- '/bin/sh -lc "$DD_FLOW_CHECK_COMMAND"',
784
+ '/bin/sh -c "$DD_FLOW_CHECK_COMMAND"',
302
785
  "code=$?",
303
786
  'if [ -n "$completion" ]; then temporary="$completion.tmp.$$"; printf \'{"exit_code":%s,"finished_at":"%s"}\\n\' "$code" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$temporary" && mv "$temporary" "$completion"; fi',
304
787
  'exit "$code"'