@deksden-com/dd-flow-cli 0.9.0-beta.1 → 0.9.0-beta.100

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 (153) hide show
  1. package/CHANGELOG.md +561 -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 +91 -15
  6. package/dist/cli/hook-ingress.js +82 -0
  7. package/dist/cli/input-preparation.js +116 -0
  8. package/dist/cli/run-cli.js +1506 -334
  9. package/dist/cli.js +6 -2
  10. package/dist/harness-runtime/DD-ZCODE.md +99 -0
  11. package/dist/harness-runtime/bin/dd-agy.mjs +39 -0
  12. package/dist/harness-runtime/bin/dd-codex.mjs +25 -0
  13. package/dist/harness-runtime/bin/dd-droid.mjs +33 -0
  14. package/dist/harness-runtime/bin/dd-grok.mjs +30 -0
  15. package/dist/harness-runtime/bin/dd-opencode.mjs +21 -0
  16. package/dist/harness-runtime/bin/dd-zcode.mjs +83 -0
  17. package/dist/harness-runtime/lib/daemon-operations.mjs +215 -0
  18. package/dist/harness-runtime/lib/dd-agy-daemon.mjs +449 -0
  19. package/dist/harness-runtime/lib/dd-agy.mjs +59 -0
  20. package/dist/harness-runtime/lib/dd-codex-daemon.d.mts +1 -0
  21. package/dist/harness-runtime/lib/dd-codex-daemon.mjs +146 -0
  22. package/dist/harness-runtime/lib/dd-codex.mjs +562 -0
  23. package/dist/harness-runtime/lib/dd-droid-daemon.mjs +125 -0
  24. package/dist/harness-runtime/lib/dd-droid.mjs +468 -0
  25. package/dist/harness-runtime/lib/dd-grok-daemon.mjs +300 -0
  26. package/dist/harness-runtime/lib/dd-grok.mjs +174 -0
  27. package/dist/harness-runtime/lib/dd-opencode-daemon.mjs +217 -0
  28. package/dist/harness-runtime/lib/dd-opencode.mjs +98 -0
  29. package/dist/harness-runtime/lib/dd-zcode-daemon.mjs +644 -0
  30. package/dist/harness-runtime/lib/dd-zcode.mjs +941 -0
  31. package/dist/harness-runtime/lib/delegation-instructions.d.mts +10 -0
  32. package/dist/harness-runtime/lib/delegation-instructions.mjs +130 -0
  33. package/dist/harness-runtime/lib/dispatch-fence.mjs +14 -0
  34. package/dist/harness-runtime/lib/driver-recovery.mjs +129 -0
  35. package/dist/harness-runtime/lib/droid-observation.mjs +66 -0
  36. package/dist/harness-runtime/lib/managed-daemon.mjs +267 -0
  37. package/dist/harness-runtime/lib/model-observations.mjs +77 -0
  38. package/dist/harness-runtime/lib/native-hook-command.d.mts +1 -0
  39. package/dist/harness-runtime/lib/native-hook-command.mjs +34 -0
  40. package/dist/harness-runtime/lib/observation-clock.mjs +36 -0
  41. package/dist/harness-runtime/lib/operation-context.mjs +5 -0
  42. package/dist/harness-runtime/lib/operation-errors.mjs +19 -0
  43. package/dist/harness-runtime/lib/process-json.mjs +70 -0
  44. package/dist/harness-runtime/lib/process-snapshot.mjs +10 -0
  45. package/dist/harness-runtime/lib/runner-events.mjs +200 -0
  46. package/dist/harness-runtime/lib/runner-lock.mjs +42 -0
  47. package/dist/harness-runtime/lib/session-settlement.mjs +30 -0
  48. package/dist/harness-runtime/lib/tool-observations.d.mts +17 -0
  49. package/dist/harness-runtime/lib/tool-observations.mjs +153 -0
  50. package/dist/harness-runtime/lib/zcode-usage.mjs +54 -0
  51. package/dist/runtime/context.js +3 -1
  52. package/dist/schemas/agent-profile.schema.json +1 -1
  53. package/dist/schemas/code-review-result.schema.json +2 -2
  54. package/dist/schemas/code-work-batch.schema.json +4 -3
  55. package/dist/schemas/code-work-result.schema.json +4 -4
  56. package/dist/schemas/harness-config.schema.json +23 -0
  57. package/dist/schemas/plan-review-decision.schema.json +1 -1
  58. package/dist/schemas/plan-review-result.schema.json +2 -2
  59. package/dist/schemas/run-control-receipt.schema.json +99 -0
  60. package/dist/schemas/run-control-request.schema.json +36 -0
  61. package/dist/schemas/vnext-protocol-plan.schema.json +2 -2
  62. package/dist/services/canon.js +11 -8
  63. package/dist/services/cleanup.js +75 -21
  64. package/dist/services/cli-operation-classifier.js +22 -24
  65. package/dist/services/code-checks.js +676 -79
  66. package/dist/services/codex-hook-delivery.js +28 -0
  67. package/dist/services/command-context.js +80 -0
  68. package/dist/services/config.js +11 -8
  69. package/dist/services/continuation-outcome.js +20 -0
  70. package/dist/services/controller-fanout.js +134 -0
  71. package/dist/services/dashboard.js +103 -31
  72. package/dist/services/engines.js +125 -80
  73. package/dist/services/eval-snapshots.js +838 -74
  74. package/dist/services/execution-policy.js +170 -0
  75. package/dist/services/external-work-launch.js +110 -0
  76. package/dist/services/harness-adapter.js +147 -24
  77. package/dist/services/harness-config.js +68 -0
  78. package/dist/services/hooks.js +609 -310
  79. package/dist/services/lanes.js +62 -53
  80. package/dist/services/lifecycle-command.js +103 -17
  81. package/dist/services/lifecycle-contract.js +30 -0
  82. package/dist/services/lifecycle-invocations.js +1128 -0
  83. package/dist/services/managed-daemon-binding.js +38 -0
  84. package/dist/services/managed-processes.js +425 -0
  85. package/dist/services/merge-queue.js +179 -104
  86. package/dist/services/merge-server.js +78 -27
  87. package/dist/services/migrations.js +14 -9
  88. package/dist/services/native-daemon-history.js +49 -0
  89. package/dist/services/native-session-control.js +39 -0
  90. package/dist/services/plan-runtime.js +19 -8
  91. package/dist/services/plans.js +28 -25
  92. package/dist/services/portable-refs.js +57 -0
  93. package/dist/services/projects.js +18 -5
  94. package/dist/services/prompts.js +27 -18
  95. package/dist/services/protocols.js +64 -20
  96. package/dist/services/recovery-observation-budget.js +61 -0
  97. package/dist/services/recovery-snapshot-database.js +107 -0
  98. package/dist/services/repair-intents.js +40 -0
  99. package/dist/services/run-control-receipt.js +56 -0
  100. package/dist/services/run-control-worker.js +363 -0
  101. package/dist/services/run-control.js +876 -0
  102. package/dist/services/run-controller-adapter.js +222 -0
  103. package/dist/services/run-controller-capture.js +136 -0
  104. package/dist/services/run-controller-process.js +189 -0
  105. package/dist/services/run-controller-recovery.js +222 -0
  106. package/dist/services/run-controller-state.js +36 -0
  107. package/dist/services/run-controller.js +803 -0
  108. package/dist/services/run-engine-bindings.js +20 -62
  109. package/dist/services/run-fork.js +138 -0
  110. package/dist/services/run-observations.js +112 -0
  111. package/dist/services/run-projection.js +10 -8
  112. package/dist/services/run-recovery-runtime.js +69 -0
  113. package/dist/services/run-recovery.js +343 -0
  114. package/dist/services/runs.js +437 -81
  115. package/dist/services/runtime-budget.js +439 -0
  116. package/dist/services/runtime-command.js +43 -0
  117. package/dist/services/runtime-scope-capture.js +50 -0
  118. package/dist/services/runtime-scope-control.js +450 -0
  119. package/dist/services/runtime-scope-resume.js +565 -0
  120. package/dist/services/runtime-scope-stop.js +99 -0
  121. package/dist/services/runtime-scope-worker.js +243 -0
  122. package/dist/services/runtime-service.js +97 -0
  123. package/dist/services/schema-validation.js +21 -19
  124. package/dist/services/session-identity.js +19 -0
  125. package/dist/services/sessions.js +61 -80
  126. package/dist/services/stage-blocker.js +17 -7
  127. package/dist/services/stage-context.js +44 -18
  128. package/dist/services/stage-lifecycle.js +112 -108
  129. package/dist/services/stage-pause.js +117 -73
  130. package/dist/services/stage-work-graph.js +35 -0
  131. package/dist/services/usage.js +108 -132
  132. package/dist/services/vnext-code-review.js +245 -98
  133. package/dist/services/vnext-code.js +355 -257
  134. package/dist/services/vnext-execution-profile.js +5 -3
  135. package/dist/services/vnext-fanout.js +166 -26
  136. package/dist/services/vnext-merge.js +413 -147
  137. package/dist/services/vnext-plan-review.js +182 -109
  138. package/dist/services/vnext-plan.js +199 -82
  139. package/dist/services/vnext-protocolize.js +133 -60
  140. package/dist/services/vnext-specify.js +94 -71
  141. package/dist/services/work-registry.js +813 -160
  142. package/dist/services/workspace-bootstrap.js +76 -0
  143. package/dist/services/worktrees.js +85 -35
  144. package/dist/shared/entity-references.js +8 -0
  145. package/dist/shared/errors.js +12 -0
  146. package/dist/storage/database.js +569 -16
  147. package/dist/storage/paths.js +17 -4
  148. package/dist/storage/work-references.js +23 -0
  149. package/dist/storage/writer-contract.js +77 -0
  150. package/dist/storage/writer-migration.js +102 -0
  151. package/package.json +24 -13
  152. package/tools/audit-runtime-fix-boundaries.mjs +96 -0
  153. package/tools/repair-paused-run-status.mjs +59 -0
@@ -1,10 +1,14 @@
1
+ import { managedInvocationContext, managedLifecycleCommand } from "./lifecycle-invocations.js";
1
2
  import crypto from "node:crypto";
2
3
  import fs from "node:fs";
3
4
  import path from "node:path";
4
5
  import { AppError } from "../shared/errors.js";
6
+ import { runtimeBudgetForRun } from "./runtime-budget.js";
5
7
  import { flowCommand } from "./stage-pause.js";
6
- import { validateSchema } from "./schema-validation.js";
7
- import { adapterSessionId, runHarnessAdapter } from "./harness-adapter.js";
8
+ import { adapterSessionId, prepareHarnessFlowExecutable, runHarnessAdapter } from "./harness-adapter.js";
9
+ import { harnessRuntimeArguments, resolveHarnessCommand } from "./harness-config.js";
10
+ import { assertRunMutationAllowed, recoveryGuard } from "./run-recovery.js";
11
+ import { loadAgentProfile, resolveExecutionPolicy, runtimeHarness } from "./execution-policy.js";
8
12
  /** A deterministic dispatcher; only the launched harness Session performs agent work. */
9
13
  export async function serveMergeRequests(context, input) {
10
14
  const running = context.db.get("SELECT server_id, pid FROM merge_servers WHERE status IN ('running','stopping') ORDER BY started_at LIMIT 1");
@@ -57,62 +61,109 @@ export async function serveMergeRequests(context, input) {
57
61
  export function mergeServerStatus(context) { return { ok: true, servers: context.db.all("SELECT server_id, profile_id, status, pid, started_at, heartbeat_at, stopped_at, journal_path FROM merge_servers ORDER BY started_at DESC") }; }
58
62
  export function stopMergeServer(context, input) { const changed = context.db.run("UPDATE merge_servers SET status = 'stopping', heartbeat_at = ? WHERE server_id = ? AND status = 'running'", [context.now(), input.serverId]); if (changed.changes !== 1)
59
63
  throw new AppError("not_found", "Running merge-server was not found", 1, { server_id: input.serverId }); return { ok: true, server_id: input.serverId, status: "stopping" }; }
64
+ /** Both dispatchers enter the same project FIFO before creating any provider Session. */
65
+ export function claimQueuedMergeRequest(context, input) {
66
+ context.db.beginWriteTransaction();
67
+ try {
68
+ const request = context.db.get("SELECT merge_request_id, project_id, run_id, executor_work_id, target_workspace, execution_route, status, created_at FROM merge_requests WHERE merge_request_id = ?", [input.requestId]);
69
+ if (!request)
70
+ throw new AppError("not_found", "MERGE request was not found", 1);
71
+ const generation = assertRunMutationAllowed(context, request.project_id, request.run_id, input.expectedGeneration);
72
+ const earlier = context.db.get("SELECT 1 FROM merge_requests WHERE project_id = ? AND status NOT IN ('completed','failed','cancelled','superseded') AND (created_at < ? OR (created_at = ? AND merge_request_id < ?)) LIMIT 1", [request.project_id, request.created_at, request.created_at, request.merge_request_id]);
73
+ if (request.status !== "queued" || request.execution_route !== "server" || earlier) {
74
+ context.db.exec("COMMIT");
75
+ return null;
76
+ }
77
+ const token = crypto.randomUUID();
78
+ context.db.run("UPDATE merge_requests SET status = 'dispatching', dispatch_owner = ?, dispatch_lease_token = ?, dispatch_lease_expires_at = ?, updated_at = ? WHERE merge_request_id = ? AND status = 'queued'", [input.ownerId, token, new Date(Date.now() + 120_000).toISOString(), context.now(), request.merge_request_id]);
79
+ context.db.exec("COMMIT");
80
+ return { request, token, generation };
81
+ }
82
+ catch (error) {
83
+ context.db.exec("ROLLBACK");
84
+ throw error;
85
+ }
86
+ }
60
87
  async function dispatch(context, input) {
61
- const token = crypto.randomUUID();
62
- const leaseUntil = new Date(Date.now() + 120_000).toISOString();
63
- const claimed = context.db.run("UPDATE merge_requests SET status = 'dispatching', dispatch_owner = ?, dispatch_lease_token = ?, dispatch_lease_expires_at = ?, updated_at = ? WHERE merge_request_id = ? AND status = 'queued' AND execution_route = 'server'", [input.serverId, token, leaseUntil, context.now(), input.request.merge_request_id]);
64
- if (claimed.changes !== 1)
88
+ const run = context.db.get("SELECT index_json, project_root FROM runs WHERE project_id = ? AND id = ?", [input.request.project_id, input.request.run_id]);
89
+ if (!run)
90
+ throw new AppError("not_found", "MERGE RUN was not found", 1, { run_id: input.request.run_id });
91
+ const snapshot = JSON.parse(run.index_json).execution_profile;
92
+ if (snapshot?.settings?.execution) {
93
+ if (!snapshot.agent_profiles)
94
+ throw new AppError("execution_profile_not_frozen", "MERGE dispatch requires frozen RUN agent profiles", 1, { run_id: input.request.run_id });
95
+ const resolved = resolveExecutionPolicy({ ddFlowHome: context.ddFlowHome, policy: snapshot.settings.execution, profiles: snapshot.agent_profiles, stage: "merge" });
96
+ input = { ...input, profile: resolved.coordinator };
97
+ }
98
+ const claim = claimQueuedMergeRequest(context, { requestId: input.request.merge_request_id, ownerId: input.serverId });
99
+ if (!claim)
65
100
  return;
101
+ const { generation, token } = claim;
66
102
  const stateDir = path.join(input.root, input.request.merge_request_id);
67
103
  fs.mkdirSync(stateDir, { recursive: true });
68
104
  const promptFile = path.join(stateDir, "launch.md");
69
105
  const adapterJournal = path.join(stateDir, "adapter.events.jsonl");
70
- const stageCommand = `${flowCommand(context)} stage start ${input.request.run_id} --stage merge --project-root ${JSON.stringify(input.request.target_workspace)} --json --progress-jsonl`;
71
- fs.writeFileSync(promptFile, `Start the assigned MERGE stage now. Your first tool call must be this exact standalone command:\n\n${stageCommand}\n\nTrust and follow the complete stage packet it returns. Continue through merge apply and stage finish. Stop only when the stage reaches a terminal result or explicitly requests user/operator action.\n`);
106
+ const flowExecutable = prepareHarnessFlowExecutable(context, stateDir);
107
+ let runtimeContext = { ...context, env: { ...context.env, DD_FLOW_BIN: flowExecutable } };
108
+ const runtimeOwner = { schema_id: "dd-flow/runtime-owner@1", owner_id: `run:${input.request.run_id}:merge:${input.request.merge_request_id}`, role: "merge", project_id: input.request.project_id, run_id: input.request.run_id, work_id: input.request.executor_work_id, operation_id: `${input.request.merge_request_id}:${token}`, state_dir: stateDir, dd_flow_home: context.ddFlowHome, dd_flow_bin: flowExecutable, resource_home: context.env.DD_FLOW_RESOURCE_HOME ?? context.ddFlowHome, budget: runtimeBudgetForRun(context, input.request.project_id, input.request.run_id) };
72
109
  event(input.journal, { type: "launch_intent", server_id: input.serverId, request_id: input.request.merge_request_id, token, profile: input.profile, prompt_file: promptFile, at: context.now() });
73
110
  input.progress?.(`launching ${input.request.merge_request_id} with ${input.profile.id}`);
74
- const executable = context.env[`DD_FLOW_${input.profile.harness.toUpperCase()}_ADAPTER`] ?? `dd-${input.profile.harness}`;
75
- const common = ["--state-dir", stateDir, "--journal", adapterJournal, "--cwd", input.request.target_workspace, "--provider", input.profile.provider, "--model", input.profile.model, "--reasoning", input.profile.reasoning, "--mode", input.profile.mode, "--permission", input.profile.permission, "--project-root", input.request.target_workspace, "--dd-flow-home", context.ddFlowHome, "--json"];
111
+ const executable = resolveHarnessCommand(context.ddFlowHome, input.profile.harness, "adapter");
112
+ const common = ["--dd-flow-bin", flowExecutable, "--state-dir", stateDir, "--journal", adapterJournal, "--cwd", input.request.target_workspace, "--provider", input.profile.provider, "--model", input.profile.model, "--reasoning", input.profile.reasoning, "--mode", input.profile.mode, "--permission", input.profile.permission, "--project-root", run.project_root, "--dd-flow-home", context.ddFlowHome, ...harnessRuntimeArguments(context.ddFlowHome, input.profile.harness), "--json"];
113
+ const retained = { profile: input.profile, executable, owner: runtimeOwner, session_id: null, state_dir: stateDir, create_operation_id: `${input.request.merge_request_id}:${token}:session.create`, prompt_operation_id: `${input.request.merge_request_id}:${token}:session.prompt` };
114
+ const retain = () => { context.db.run("UPDATE merge_requests SET adapter_receipt_json = ?, updated_at = ? WHERE merge_request_id = ? AND dispatch_owner = ? AND dispatch_lease_token = ?", [JSON.stringify(retained), context.now(), input.request.merge_request_id, input.serverId, token]); };
115
+ retain();
116
+ const leaseHeartbeat = setInterval(() => { if (guarded(context, input.request) || (recoveryGuard(context, input.request.project_id, input.request.run_id)?.generation ?? 0) !== generation)
117
+ return; const now = context.now(); context.db.run("UPDATE merge_requests SET dispatch_lease_expires_at = ?, updated_at = ? WHERE merge_request_id = ? AND status = 'dispatching' AND dispatch_owner = ? AND dispatch_lease_token = ?", [new Date(Date.now() + 120_000).toISOString(), now, input.request.merge_request_id, input.serverId, token]); }, 30_000);
118
+ leaseHeartbeat.unref();
76
119
  try {
77
- const adapterCall = (args, timeoutMs, progressMessage) => runHarnessAdapter({ executable, args, env: context.env, ...(timeoutMs ? { timeoutMs } : {}), ...(input.progress ? { progress: input.progress } : {}), ...(progressMessage ? { progressMessage } : {}), onEvidence: (value) => event(input.journal, { type: "adapter_call", ...value }) });
120
+ const adapterCall = (args, timeoutMs, progressMessage) => { assertRunMutationAllowed(context, input.request.project_id, input.request.run_id, generation); return runHarnessAdapter({ executable, args, env: runtimeContext.env, runtimeOwner, operationId: `${input.request.merge_request_id}:${token}:${args[0]}.${args[1]}`, ...(timeoutMs ? { timeoutMs } : {}), ...(input.progress ? { progress: input.progress } : {}), ...(progressMessage ? { progressMessage } : {}), onEvidence: (value) => event(input.journal, { type: "adapter_call", ...value }) }); };
78
121
  await adapterCall(["daemon", "start", ...common]);
79
122
  const created = await adapterCall(["session", "create", ...common]);
80
123
  const sessionId = adapterSessionId(created);
81
124
  if (!sessionId)
82
125
  throw new AppError("merge_adapter_invalid", "Harness adapter did not return a Session id", 1, { harness: input.profile.harness, response: created });
126
+ Object.assign(retained, { session_id: sessionId, create: created });
127
+ retain();
128
+ runtimeContext = managedInvocationContext(runtimeContext, { harness: runtimeHarness(input.profile), stateDir, sessionId, projectRoot: run.project_root, runId: input.request.run_id, generation });
129
+ const stageCommand = managedLifecycleCommand(runtimeContext, `${flowCommand(runtimeContext)} stage start ${input.request.run_id} --stage merge --project-root ${JSON.stringify(run.project_root)} --json --progress-jsonl`);
130
+ fs.writeFileSync(promptFile, `Start the assigned MERGE stage now. Your first tool call must be this exact standalone command:\n\n${stageCommand}\n\nTrust and follow the complete stage packet it returns. Continue through merge apply and stage finish. Stop only when the stage reaches a terminal result or explicitly requests user/operator action.\n`);
83
131
  const prompted = await adapterCall(["session", "prompt", "--session-id", sessionId, "--prompt-file", promptFile, ...common], 45 * 60 * 1000, `MERGE ${input.request.merge_request_id} Session ${sessionId} is still running`);
132
+ Object.assign(retained, { response: prompted });
133
+ retain();
134
+ assertRunMutationAllowed(context, input.request.project_id, input.request.run_id, generation);
84
135
  const request = context.db.get("SELECT status FROM merge_requests WHERE merge_request_id = ?", [input.request.merge_request_id]);
85
- context.db.run("UPDATE merge_requests SET adapter_receipt_json = ?, updated_at = ? WHERE merge_request_id = ?", [JSON.stringify({ profile: input.profile, executable, session_id: sessionId, response: prompted }), context.now(), input.request.merge_request_id]);
86
136
  event(input.journal, { type: "adapter_settled", request_id: input.request.merge_request_id, session_id: sessionId, request_status: request?.status ?? null, at: context.now() });
87
137
  if (request?.status === "dispatching")
88
- context.db.run("UPDATE merge_requests SET status = 'queued', dispatch_owner = NULL, dispatch_lease_token = NULL, dispatch_lease_expires_at = NULL, last_error_json = ?, updated_at = ? WHERE merge_request_id = ?", [JSON.stringify({ code: "adapter_did_not_start_stage", response: prompted }), context.now(), input.request.merge_request_id]);
138
+ context.db.run("UPDATE merge_requests SET status = 'recovery_required', last_error_json = ?, updated_at = ? WHERE merge_request_id = ?", [JSON.stringify({ code: "adapter_did_not_start_stage", response: prompted }), context.now(), input.request.merge_request_id]);
89
139
  }
90
140
  catch (error) {
141
+ if (error instanceof AppError && ["run_recovery_guarded", "run_recovery_generation_stale"].includes(error.code)) {
142
+ event(input.journal, { type: "recovery_dispatch_blocked", request_id: input.request.merge_request_id, code: error.code, at: context.now() });
143
+ throw error;
144
+ }
91
145
  const request = context.db.get("SELECT status FROM merge_requests WHERE merge_request_id = ?", [input.request.merge_request_id]);
92
146
  if (request?.status === "dispatching")
93
- context.db.run("UPDATE merge_requests SET status = 'queued', dispatch_owner = NULL, dispatch_lease_token = NULL, dispatch_lease_expires_at = NULL, last_error_json = ?, updated_at = ? WHERE merge_request_id = ?", [JSON.stringify({ code: "adapter_launch_failed", error: String(error) }), context.now(), input.request.merge_request_id]);
147
+ context.db.run("UPDATE merge_requests SET status = 'recovery_required', last_error_json = ?, updated_at = ? WHERE merge_request_id = ?", [JSON.stringify({ code: "adapter_launch_failed", error: String(error) }), context.now(), input.request.merge_request_id]);
94
148
  else if (request?.status === "active")
95
149
  context.db.run("UPDATE merge_requests SET status = 'recovery_required', last_error_json = ?, updated_at = ? WHERE merge_request_id = ?", [JSON.stringify({ code: "adapter_lost_after_stage_start", error: String(error) }), context.now(), input.request.merge_request_id]);
96
150
  throw error;
97
151
  }
152
+ finally {
153
+ clearInterval(leaseHeartbeat);
154
+ }
98
155
  }
99
- function nextServerRequests(context, limit) { const selected = new Set(); return context.db.all("SELECT merge_request_id, project_id, run_id, executor_work_id, target_workspace, execution_route, status, created_at FROM merge_requests WHERE status = 'queued' AND execution_route = 'server' ORDER BY created_at, merge_request_id").filter((request) => { if (selected.has(request.project_id) || context.db.get("SELECT 1 FROM merge_requests WHERE project_id = ? AND status NOT IN ('completed','failed','cancelled') AND (created_at < ? OR (created_at = ? AND merge_request_id < ?)) LIMIT 1", [request.project_id, request.created_at, request.created_at, request.merge_request_id]))
156
+ function nextServerRequests(context, limit) { const selected = new Set(); return context.db.all("SELECT merge_request_id, project_id, run_id, executor_work_id, target_workspace, execution_route, status, created_at FROM merge_requests WHERE status = 'queued' AND execution_route = 'server' ORDER BY created_at, merge_request_id").filter((request) => { if (guarded(context, request) || selected.has(request.project_id) || context.db.get("SELECT 1 FROM merge_requests WHERE project_id = ? AND status NOT IN ('completed','failed','cancelled','superseded') AND (created_at < ? OR (created_at = ? AND merge_request_id < ?)) LIMIT 1", [request.project_id, request.created_at, request.created_at, request.merge_request_id]))
100
157
  return false; selected.add(request.project_id); return true; }).slice(0, limit); }
101
- function reconcileExpiredDispatches(context) { const expired = context.db.all("SELECT merge_request_id, executor_work_id FROM merge_requests WHERE status = 'dispatching' AND dispatch_lease_expires_at < ?", [context.now()]); for (const request of expired) {
158
+ function reconcileExpiredDispatches(context) { const expired = context.db.all("SELECT merge_request_id, executor_work_id, project_id, run_id FROM merge_requests WHERE status = 'dispatching' AND dispatch_lease_expires_at < ?", [context.now()]); for (const request of expired) {
159
+ if (guarded(context, request))
160
+ continue;
102
161
  const work = context.db.get("SELECT status FROM works WHERE work_id = ?", [request.executor_work_id]);
103
- if (work?.status === "created")
104
- context.db.run("UPDATE merge_requests SET status = 'queued', dispatch_owner = NULL, dispatch_lease_token = NULL, dispatch_lease_expires_at = NULL, last_error_json = ?, updated_at = ? WHERE merge_request_id = ?", [JSON.stringify({ code: "expired_dispatch_requeued" }), context.now(), request.merge_request_id]);
105
- else
106
- context.db.run("UPDATE merge_requests SET status = 'recovery_required', last_error_json = ?, updated_at = ? WHERE merge_request_id = ?", [JSON.stringify({ code: "expired_dispatch_with_started_work", work_status: work?.status ?? null }), context.now(), request.merge_request_id]);
162
+ context.db.run("UPDATE merge_requests SET status = 'recovery_required', last_error_json = ?, updated_at = ? WHERE merge_request_id = ?", [JSON.stringify({ code: "expired_dispatch_outcome_unknown", work_status: work?.status ?? null }), context.now(), request.merge_request_id]);
107
163
  } }
108
- function loadProfile(context, id) { const file = path.join(context.ddFlowHome, "agent-profiles", `${id}.json`); let value; try {
109
- value = JSON.parse(fs.readFileSync(file, "utf8"));
110
- }
111
- catch (error) {
112
- throw new AppError("agent_profile_missing", "Merge-server agent profile is unavailable", 1, { id, file, cause: String(error) });
113
- } validateSchema({ schemaName: "agent-profile", file }); if (value.id !== id)
114
- throw new AppError("agent_profile_invalid", "Agent profile id does not match its requested filename", 2, { file, expected: id, actual: value.id ?? null }); return value; }
164
+ function loadProfile(context, id) { return loadAgentProfile(context.ddFlowHome, id); }
115
165
  function event(file, value) { fs.appendFileSync(file, `${JSON.stringify(value)}\n`); }
166
+ function guarded(context, request) { const guard = recoveryGuard(context, request.project_id, request.run_id); return Boolean(guard && ["draining", "sealed", "resuming"].includes(guard.status)); }
116
167
  function delay(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); }
117
168
  function processAlive(pid) { if (!pid)
118
169
  return false; try {
@@ -10,6 +10,7 @@ import { resolveProjectRoot } from "../storage/paths.js";
10
10
  import { classifyCliOperation } from "./cli-operation-classifier.js";
11
11
  import { compatibilityContexts, selectEngine } from "./engines.js";
12
12
  import { getCliBuildInfo } from "./build-info.js";
13
+ import { prepareJsonFile } from "../cli/input-preparation.js";
13
14
  export function assessMigrationImpact(context, input) {
14
15
  const root = resolveStatusProjectRoot({ requestedRoot: input.projectRoot, cwd: process.cwd() });
15
16
  if (!root.root)
@@ -147,7 +148,7 @@ export function planMigration(context, input) {
147
148
  }
148
149
  };
149
150
  }
150
- export function applyMigration(context, input) {
151
+ export function prepareMigrationApply(context, input) {
151
152
  const classification = input.classification ?? classifyCliOperation(["migration", "apply"], context.env);
152
153
  if (classification.mode !== "mb_upgrade") {
153
154
  throw new AppError("upgrade_authorization_required", "Migration apply requires explicit mb-upgrade authorization", 1, {
@@ -197,6 +198,13 @@ export function applyMigration(context, input) {
197
198
  });
198
199
  }
199
200
  const project = requireProjectByRoot(context, resolveProjectRoot(input.projectRoot));
201
+ const current = activeStateSummary(context, project.id);
202
+ if (activeStateBlockers(current).length)
203
+ throw new AppError("migration_active_state", "Migration apply requires a quiescent shared home", 1, { active_state: current });
204
+ return { project, sourceVersion, targetVersion, verifiedBackup, reportEngine, chain };
205
+ }
206
+ export function applyMigration(context, input, prepared = prepareMigrationApply(context, input)) {
207
+ const { project, sourceVersion, targetVersion, verifiedBackup, reportEngine, chain } = prepared;
200
208
  const writeDb = getDatabase(context.ddFlowHome, "migrate");
201
209
  const already = writeDb.get("SELECT value_json FROM runtime_config WHERE key = ?", [applyKey(project.id, sourceVersion, targetVersion)]);
202
210
  if (already) {
@@ -217,7 +225,10 @@ export function applyMigration(context, input) {
217
225
  chain: chain.steps.map((step) => ({ ...step, status: "applied" }))
218
226
  };
219
227
  try {
220
- writeDb.exec("BEGIN IMMEDIATE");
228
+ writeDb.beginWriteTransaction();
229
+ const current = activeStateSummary({ ...context, db: writeDb }, project.id);
230
+ if (activeStateBlockers(current).length)
231
+ throw new AppError("migration_active_state", "Migration apply requires a quiescent shared home", 1, { active_state: current });
221
232
  writeDb.run(`INSERT INTO runtime_config (key, value_json, created_at, updated_at) VALUES (?, ?, ?, ?)`, [applyKey(project.id, sourceVersion, targetVersion), JSON.stringify(result), context.now(), context.now()]);
222
233
  writeDb.exec("COMMIT");
223
234
  }
@@ -513,13 +524,7 @@ function activeStateBlockers(active) {
513
524
  return blockers;
514
525
  }
515
526
  function readJsonObject(filePath) {
516
- let parsed;
517
- try {
518
- parsed = JSON.parse(fs.readFileSync(filePath, "utf8"));
519
- }
520
- catch (error) {
521
- throw new AppError("invalid_json", `Cannot read migration report: ${filePath}`, 2, { cause: String(error) });
522
- }
527
+ const { value: parsed } = prepareJsonFile(filePath, "Migration report");
523
528
  if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
524
529
  throw new AppError("validation", "Migration report must be a JSON object", 2, { file: filePath });
525
530
  }
@@ -0,0 +1,49 @@
1
+ import crypto from "node:crypto";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { AppError } from "../shared/errors.js";
5
+ import { managedProcessBinding, managedProcessStatus, processTreeIsAlive, resourceHome } from "./managed-processes.js";
6
+ import { retainedDaemonOperations } from "./run-control.js";
7
+ import { managedProviderBelongsToDaemon } from "./managed-daemon-binding.js";
8
+ /** A replaced daemon is a settled resource, never another Session to resume. */
9
+ export function historicalDaemonSettlement(context, scopeId, record) {
10
+ const metadata = JSON.parse(record.metadata_json);
11
+ if (!record.kind.endsWith("-daemon") || !metadata.state_dir || !path.isAbsolute(metadata.state_dir))
12
+ throw new AppError("runtime_scope_owner_unknown", "Native history requires an absolute daemon directory", 1);
13
+ const current = JSON.parse(fs.readFileSync(path.join(metadata.state_dir, "daemon.json"), "utf8"));
14
+ if (current.resource_process?.id === record.id)
15
+ return null;
16
+ if (record.state !== "stopped" || record.termination_reason !== "daemon_stopped" || !record.pid || !record.pid_started_at || processTreeIsAlive(record))
17
+ throw new AppError("runtime_scope_owner_unknown", "Previous daemon has no physical completion proof", 1);
18
+ const directory = path.join(metadata.state_dir, "daemon-history");
19
+ const matches = fs.readdirSync(directory).filter(name => /^[a-zA-Z0-9-]+\.json$/.test(name)).map(name => {
20
+ const file = path.join(directory, name);
21
+ if (!fs.lstatSync(file).isFile())
22
+ throw new AppError("runtime_scope_owner_unknown", "Daemon history must contain regular receipts", 1);
23
+ const bytes = fs.readFileSync(file, "utf8");
24
+ return { file, bytes, state: JSON.parse(bytes) };
25
+ }).filter(candidate => candidate.state.resource_process?.id === record.id);
26
+ if (matches.length !== 1)
27
+ throw new AppError("runtime_scope_owner_unknown", "Previous daemon has no unique retained receipt", 1);
28
+ const { file, bytes, state } = matches[0], owner = state.config?.runtime_owner;
29
+ if (!state.daemon_id || path.basename(file) !== `${state.daemon_id}.json` || !owner || owner.budget?.scope_id !== scopeId || owner.owner_id !== record.owner_id || owner.role !== metadata.role || owner.state_dir !== metadata.state_dir || owner.adapter_executable !== metadata.adapter_executable || owner.dd_flow_home !== metadata.dd_flow_home || owner.resource_home !== resourceHome(context) || (owner.project_id ?? null) !== record.project_id || (owner.run_id ?? null) !== record.run_id || (owner.work_id ?? null) !== record.work_id || !record.operation_id?.startsWith(`${owner.operation_id}:`) || state.resource_process?.lease_token !== record.lease_token || state.pid !== record.pid)
30
+ throw new AppError("runtime_scope_owner_unknown", "Archived daemon does not match its registration", 1);
31
+ const closed = state.shutdown_state === "clean" && state.active_tree === false
32
+ ? retainedDaemonOperations(metadata.state_dir).find(operation => operation.daemon_id === state.daemon_id && operation.operation === "daemon.stop" && operation.state === "completed" && operation.result?.stopped === true && operation.result?.clean === true) : undefined;
33
+ const fresh = managedProcessStatus(context).find(item => item.id === record.id);
34
+ if (!closed || !fresh || fresh.lease_token !== record.lease_token || fresh.pid !== record.pid || fresh.pid_started_at !== record.pid_started_at || fresh.metadata_json !== record.metadata_json || fresh.state !== "stopped" || fresh.termination_reason !== "daemon_stopped" || processTreeIsAlive(fresh) || fs.readFileSync(file, "utf8") !== bytes)
35
+ throw new AppError("runtime_scope_outcome_unknown", "Previous daemon lacks stable clean shutdown evidence", 1);
36
+ const providers = managedProcessStatus(context).filter(item => managedProviderBelongsToDaemon(item, fresh, state));
37
+ if (state.provider_process && (providers.length !== 1 || providers[0].state !== "stopped" || providers[0].termination_reason !== "daemon_stopped" || !providers[0].operation_id?.startsWith(`${owner.operation_id}:`) || processTreeIsAlive(providers[0])))
38
+ throw new AppError("runtime_scope_outcome_unknown", "Archived provider lacks its exact physical completion proof", 1);
39
+ return { historical: true, process_id: record.id, role: metadata.role, state_dir: metadata.state_dir, daemon_id: state.daemon_id,
40
+ processes: providers.map(item => ({ process_id: item.id, native_settled: true, binding: managedProcessBinding(item, resourceHome(context)) })),
41
+ settled: true, native_settled: true, operation_id: String(closed.operation_id), owner_operation_id: owner.operation_id,
42
+ inventory_version: crypto.createHash("sha256").update(JSON.stringify([bytes, closed])).digest("hex") };
43
+ }
44
+ export function assertHistoricalDaemonSettlement(context, scopeId, proof) {
45
+ const record = managedProcessStatus(context).find(item => item.id === proof.process_id);
46
+ const fresh = record && historicalDaemonSettlement(context, scopeId, record);
47
+ if (!fresh || proof.historical !== true || proof.native_settled !== true || fresh.inventory_version !== proof.inventory_version)
48
+ throw new AppError("runtime_scope_inventory_changed", "Archived daemon settlement changed after observation", 1);
49
+ }
@@ -0,0 +1,39 @@
1
+ import { AppError } from "../shared/errors.js";
2
+ import { adapterSessionId, runHarnessAdapter } from "./harness-adapter.js";
3
+ export async function cancelControlledNativeSession(input) {
4
+ input.assertCurrent();
5
+ const reply = await runHarnessAdapter({ executable: input.executable,
6
+ args: input.observe ? ["daemon", "operation", "--operation-id", input.operationId, "--state-dir", input.stateDir, "--json"] : ["session", "cancel", "--session-id", input.sessionId, "--state-dir", input.stateDir, "--json"],
7
+ env: input.env, operationId: input.observe ? `${input.operationId}:observe` : input.operationId, timeoutMs: 30_000 });
8
+ if (input.observe && (reply.operation_id !== input.operationId || reply.operation !== "session.cancel" || reply.session_id !== input.sessionId))
9
+ throw new AppError("controller_operation_mismatch", "Cancellation observation belongs to another operation or Session", 1);
10
+ if (input.observe && reply.state === "failed") {
11
+ if (!reply.error || typeof reply.error !== "object" || Array.isArray(reply.error))
12
+ throw new AppError("controller_operation_mismatch", "Cancellation failure has no terminal error", 1);
13
+ input.assertCurrent();
14
+ return { status: "failed", receipt: reply };
15
+ }
16
+ const result = input.observe ? reply.state === "completed" ? reply.result : null : reply;
17
+ // A completed cancel command is an immutable ACK, not proof that the
18
+ // provider tree has stopped. Callers must use a separate fresh inspection.
19
+ if (!input.observe || reply.state === "completed")
20
+ assertNativeCancellationReceipt(result, input.sessionId);
21
+ input.assertCurrent();
22
+ return { status: result ? "completed" : "outcome_unknown", receipt: (result ?? reply) };
23
+ }
24
+ export function assertNativeCancellationReceipt(value, sessionId) {
25
+ const identity = adapterSessionId(value);
26
+ if (!value || typeof value !== "object" || Array.isArray(value) || identity && identity !== sessionId)
27
+ throw new AppError("controller_operation_mismatch", "Cancellation receipt has no valid result for its target Session", 1);
28
+ }
29
+ /** Native observation is shared by RUN and non-Work EVAL owners. */
30
+ export async function inspectControlledNativeSession(input) {
31
+ input.assertCurrent();
32
+ const receipt = await runHarnessAdapter({ executable: input.executable, args: ["session", "inspect", "--session-id", input.sessionId, "--state-dir", input.stateDir, ...(input.settlementObservation ? ["--no-flow"] : []), "--json"], env: input.env, operationId: input.operationId, timeoutMs: 30_000 });
33
+ if (adapterSessionId(receipt) !== input.sessionId)
34
+ throw new AppError("controller_operation_mismatch", "Session observation belongs to another target", 1);
35
+ if (input.harness && receipt.harness !== undefined && receipt.harness !== input.harness)
36
+ throw new AppError("controller_operation_mismatch", "Session observation belongs to another harness", 1);
37
+ input.assertCurrent();
38
+ return receipt;
39
+ }
@@ -6,17 +6,23 @@ import { parseJsonObject } from "../shared/json.js";
6
6
  import { refreshRunSessionProjection } from "./run-projection.js";
7
7
  export function readCanonicalPlan(planPath, protocolId) {
8
8
  if (!fs.existsSync(planPath) || !fs.statSync(planPath).isFile()) {
9
- throw new AppError("not_found", `Canonical protocol plan is missing: ${planPath}`, 1, { plan_path: planPath });
9
+ throw new AppError("runtime_artifact_missing", `Canonical protocol plan is missing: ${planPath}`, 1, { plan_path: planPath });
10
10
  }
11
11
  const raw = fs.readFileSync(planPath, "utf8");
12
- const value = parseJsonObject(raw, planPath);
13
- const plan = validatePlan(value, protocolId);
14
- const revisionValue = value.revision;
15
- const revision = typeof revisionValue === "number" && Number.isInteger(revisionValue) && revisionValue > 0 ? revisionValue : 1;
16
- return { plan, raw, sha256: crypto.createHash("sha256").update(raw).digest("hex"), revision };
12
+ try {
13
+ const value = parseJsonObject(raw, planPath);
14
+ const plan = validatePlan(value, protocolId);
15
+ const revisionValue = value.revision;
16
+ const revision = typeof revisionValue === "number" && Number.isInteger(revisionValue) && revisionValue > 0 ? revisionValue : 1;
17
+ return { plan, raw, sha256: crypto.createHash("sha256").update(raw).digest("hex"), revision };
18
+ }
19
+ catch (error) {
20
+ if (!(error instanceof AppError) || error.code !== "validation")
21
+ throw error;
22
+ throw new AppError("runtime_artifact_invalid", "Canonical protocol plan is invalid", 1, { plan_path: planPath, cause_code: error.code });
23
+ }
17
24
  }
18
- export function boundCanonicalPlan(context, input) {
19
- const canonical = readCanonicalPlan(input.planPath, input.protocolId);
25
+ export function prepareCanonicalPlan(context, input, canonical = readCanonicalPlan(input.planPath, input.protocolId)) {
20
26
  const binding = context.db.get("SELECT plan_revision, plan_sha256, plan_path FROM plan_bindings WHERE project_id = ? AND protocol_id = ?", [input.projectId, input.protocolId]);
21
27
  if (binding && (binding.plan_revision !== canonical.revision || binding.plan_sha256 !== canonical.sha256)) {
22
28
  throw new AppError("plan_stale", "Canonical plan changed after the runtime binding was created", 1, {
@@ -28,6 +34,11 @@ export function boundCanonicalPlan(context, input) {
28
34
  plan_path: input.planPath
29
35
  });
30
36
  }
37
+ return canonical;
38
+ }
39
+ export function boundCanonicalPlan(context, input, prepared) {
40
+ const canonical = prepareCanonicalPlan(context, input, prepared);
41
+ const binding = context.db.get("SELECT plan_revision, plan_sha256, plan_path FROM plan_bindings WHERE project_id = ? AND protocol_id = ?", [input.projectId, input.protocolId]);
31
42
  const now = context.now();
32
43
  if (!binding) {
33
44
  context.db.run(`INSERT INTO plan_bindings (project_id, protocol_id, plan_revision, plan_sha256, plan_path, bound_at, updated_at)
@@ -4,7 +4,7 @@ import { appendAudit } from "./audit.js";
4
4
  import { persistProtocolState, readProtocolRuntimeState, requireProtocol } from "./protocols.js";
5
5
  import { requireProjectByRoot } from "./projects.js";
6
6
  import { resolveProjectRoot } from "../storage/paths.js";
7
- import { planSummary, planWithProgress, updatePlanProgress } from "./plan-runtime.js";
7
+ import { planSummary, planWithProgress, prepareCanonicalPlan, updatePlanProgress } from "./plan-runtime.js";
8
8
  export function getPlanStatus(context, input) {
9
9
  const protocol = scopedProtocol(context, input.projectRoot, input.protocolId);
10
10
  const current = planWithProgress(context, { projectId: protocol.project_id, protocolId: protocol.id, planPath: protocol.plan_path }, { bind: false });
@@ -20,40 +20,43 @@ export function getPlanStatus(context, input) {
20
20
  };
21
21
  }
22
22
  export function startPlanItem(context, input) {
23
- return updatePlanItem(context, input.projectRoot, input.protocolId, input.itemId, (item, plan) => {
24
- assertDependenciesClosed(plan, item);
25
- return { ...item, status: "in_progress", summary: item.summary || "Started." };
26
- });
23
+ return updatePlanItem(context, { ...input, action: "start" });
27
24
  }
28
25
  export function completePlanItem(context, input) {
29
- return updatePlanItem(context, input.projectRoot, input.protocolId, input.itemId, (item, plan) => {
30
- assertDependenciesClosed(plan, item);
31
- return { ...item, status: "done", summary: input.summary, evidence: [...new Set([...item.evidence, ...input.evidence])] };
32
- });
26
+ return updatePlanItem(context, { ...input, action: "done" });
33
27
  }
34
28
  export function blockPlanItem(context, input) {
35
- return updatePlanItem(context, input.projectRoot, input.protocolId, input.itemId, (item) => ({
36
- ...item,
37
- status: "blocked",
38
- summary: input.reason,
39
- block_reason: input.reason,
40
- user_required: input.userRequired
41
- }));
29
+ return updatePlanItem(context, { ...input, action: "block" });
42
30
  }
43
31
  export function skipPlanItem(context, input) {
44
- return updatePlanItem(context, input.projectRoot, input.protocolId, input.itemId, (item) => ({
45
- ...item,
46
- status: "skipped",
47
- summary: input.reason
48
- }));
32
+ return updatePlanItem(context, { ...input, action: "skip" });
49
33
  }
50
- function updatePlanItem(context, projectRoot, protocolId, itemId, transform) {
34
+ export function preparePlanItemMutation(context, input) {
35
+ const { projectRoot, protocolId, itemId } = input;
36
+ const text = (value) => typeof value === "string" && value.trim().length > 0;
37
+ if (!text(itemId) || !["start", "done", "block", "skip"].includes(input.action) ||
38
+ (input.action === "done" && (!text(input.summary) || !Array.isArray(input.evidence) || !input.evidence.every(text))) ||
39
+ ((input.action === "block" || input.action === "skip") && !text(input.reason)) ||
40
+ (input.action === "block" && typeof input.userRequired !== "boolean")) {
41
+ throw new AppError("validation", "Plan item requires a valid action, item ID, nonempty summary/reason, text evidence and boolean userRequired", 2, { phase: "prepare", effect: "no_effect", recoverable: true });
42
+ }
51
43
  const protocol = scopedProtocol(context, projectRoot, protocolId);
52
- const current = planWithProgress(context, { projectId: protocol.project_id, protocolId: protocol.id, planPath: protocol.plan_path });
44
+ const scope = { projectId: protocol.project_id, protocolId: protocol.id, planPath: protocol.plan_path };
45
+ const current = planWithProgress(context, scope, { bind: false });
46
+ prepareCanonicalPlan(context, scope, current.canonical);
53
47
  const item = current.plan.items.find((candidate) => candidate.id === itemId);
54
48
  if (!item)
55
49
  throw new AppError("not_found", `Plan item is not found: ${itemId}`, 1);
56
- const nextItem = transform(item, current.plan);
50
+ if (input.action === "start" || input.action === "done")
51
+ assertDependenciesClosed(current.plan, item);
52
+ const nextItem = input.action === "start" ? { ...item, status: "in_progress", summary: item.summary || "Started." }
53
+ : input.action === "done" ? { ...item, status: "done", summary: input.summary, evidence: [...new Set([...item.evidence, ...input.evidence])] }
54
+ : input.action === "block" ? { ...item, status: "blocked", summary: input.reason, block_reason: input.reason, user_required: input.userRequired }
55
+ : { ...item, status: "skipped", summary: input.reason };
56
+ return { protocol, item, nextItem };
57
+ }
58
+ export function updatePlanItem(context, input) {
59
+ const { protocol, item, nextItem } = preparePlanItemMutation(context, input);
57
60
  updatePlanProgress(context, { projectId: protocol.project_id, protocolId: protocol.id, planPath: protocol.plan_path, item: nextItem });
58
61
  if (nextItem.status === "done") {
59
62
  context.db.run("UPDATE flow_jobs SET status = 'done', finished_at = COALESCE(finished_at, ?), updated_at = ? WHERE project_id = ? AND protocol_id = ? AND plan_item_id = ?", [context.now(), context.now(), protocol.project_id, protocol.id, nextItem.id]);
@@ -63,7 +66,7 @@ function updatePlanItem(context, projectRoot, protocolId, itemId, transform) {
63
66
  protocolId: protocol.id,
64
67
  projectId: protocol.project_id,
65
68
  eventType: "plan.progress.updated",
66
- payload: { protocol_id: protocol.id, item_id: itemId, from: item.status, to: nextItem.status }
69
+ payload: { protocol_id: protocol.id, item_id: input.itemId, from: item.status, to: nextItem.status }
67
70
  });
68
71
  return { ok: true, protocol_id: protocol.id, item: nextItem, plan: planSummary(context, { projectId: protocol.project_id, protocolId: protocol.id, planPath: protocol.plan_path }) };
69
72
  }
@@ -0,0 +1,57 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { AppError } from "../shared/errors.js";
4
+ export function assertPortableArtifactRef(value, input) {
5
+ const parsed = parseArtifactRef(value);
6
+ let file;
7
+ let root;
8
+ if (parsed.path.startsWith("run://")) {
9
+ const prefix = `run://${input.runId}/`;
10
+ if (!parsed.path.startsWith(prefix) || parsed.path.slice(prefix.length).split("/").includes(".."))
11
+ throw new AppError("invalid_evidence_ref", "Artifact RUN reference must stay in the current RUN", 2, { ref: value });
12
+ root = input.runHome;
13
+ file = path.join(root, parsed.path.slice(prefix.length));
14
+ }
15
+ else {
16
+ root = input.workspaceRoot;
17
+ if (!parsed.path)
18
+ throw new AppError("invalid_evidence_ref", "Artifact reference must name a file", 2, { ref: value });
19
+ file = path.isAbsolute(parsed.path) ? parsed.path : path.join(root, parsed.path);
20
+ }
21
+ if (!fs.existsSync(file))
22
+ throw new AppError("evidence_ref_missing", "Artifact reference does not exist", 2, { ref: value, resolved_path: file });
23
+ const realRoot = fs.realpathSync(root);
24
+ const realFile = fs.realpathSync(file);
25
+ if (!contained(realRoot, realFile))
26
+ throw new AppError("invalid_evidence_ref", "Artifact reference must stay inside its declared workspace", 2, { ref: value, resolved_path: realFile, root: realRoot });
27
+ if (parsed.lines)
28
+ validateLineRanges(realFile, parsed.lines, value);
29
+ return realFile;
30
+ }
31
+ function parseArtifactRef(value) {
32
+ if (typeof value !== "string" || !value.trim())
33
+ throw new AppError("invalid_evidence_ref", "Artifact reference must be a non-empty string", 2, { ref: value });
34
+ const [rawPath, ...fragments] = value.split("#");
35
+ if (fragments.length > 1 || (fragments.length === 1 && !fragments[0]))
36
+ throw new AppError("invalid_evidence_ref", "Artifact reference has an invalid fragment", 2, { ref: value });
37
+ const match = rawPath.match(/^(.*):(\d+(?:-\d+)?(?:,\d+(?:-\d+)?)*)$/);
38
+ if (!match)
39
+ return { path: rawPath, fragment: fragments[0] ?? null, lines: null };
40
+ const lines = match[2].split(",").map((item) => {
41
+ const [startText, endText] = item.split("-");
42
+ const start = Number(startText);
43
+ const end = Number(endText ?? startText);
44
+ if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start < 1 || end < start)
45
+ throw new AppError("invalid_evidence_ref", "Artifact line ranges must be positive and ordered", 2, { ref: value, range: item });
46
+ return { start, end };
47
+ });
48
+ return { path: match[1], fragment: fragments[0] ?? null, lines };
49
+ }
50
+ function contained(root, file) { const relative = path.relative(root, file); return relative === "" || (!relative.startsWith(`..${path.sep}`) && relative !== ".." && !path.isAbsolute(relative)); }
51
+ function validateLineRanges(file, ranges, ref) {
52
+ const source = fs.readFileSync(file, "utf8");
53
+ const lines = source === "" ? 0 : source.replace(/\r?\n$/, "").split(/\r?\n/).length;
54
+ const invalid = ranges.find((range) => range.end > lines);
55
+ if (invalid)
56
+ throw new AppError("evidence_line_out_of_range", "Artifact line range exceeds the referenced file", 2, { ref, file, line_count: lines, invalid_range: invalid });
57
+ }
@@ -37,8 +37,13 @@ export function registerProject(context, input) {
37
37
  }
38
38
  return { ok: true, project: { ...project, status: "active", updated_at: now }, db_path: context.db.path };
39
39
  }
40
- export function archiveProject(context, input) {
41
- const reason = input.reason.trim();
40
+ export function prepareProjectArchive(context, input) {
41
+ if (Boolean(input.idOrAlias) === Boolean(input.root))
42
+ throw new AppError("usage", "Project archive requires exactly one project ID/alias or --root", 2);
43
+ for (const value of [input.idOrAlias, input.root])
44
+ if (value !== undefined && (typeof value !== "string" || !value.trim()))
45
+ throw new AppError("validation", "Project archive target must be a non-empty string", 2);
46
+ const reason = typeof input.reason === "string" ? input.reason.trim() : "";
42
47
  if (!reason) {
43
48
  throw new AppError("validation", "project archive requires --reason", 2);
44
49
  }
@@ -50,6 +55,10 @@ export function archiveProject(context, input) {
50
55
  if (!project) {
51
56
  throw new AppError("not_found", "Project is not registered.", 1, { root: input.root, input: input.idOrAlias });
52
57
  }
58
+ return { project, reason };
59
+ }
60
+ export function archiveProject(context, input) {
61
+ const { project, reason } = prepareProjectArchive(context, input);
53
62
  const now = context.now();
54
63
  const result = context.db.run("UPDATE projects SET status = 'archived', updated_at = ? WHERE id = ? AND status <> 'archived'", [now, project.id]);
55
64
  if (result.changes === 1) {
@@ -140,7 +149,7 @@ export function migrateProjectIds(context, input) {
140
149
  throw new AppError("conflict", `Target project id already exists: ${target.id}`, 1, plan);
141
150
  }
142
151
  context.db.exec("PRAGMA foreign_keys = OFF");
143
- context.db.exec("BEGIN IMMEDIATE");
152
+ context.db.beginWriteTransaction();
144
153
  try {
145
154
  context.db.run("UPDATE projects SET id = ?, short_id = ?, slug = ?, state_root = ?, updated_at = ? WHERE id = ?", [target.id, target.short_id, target.slug, target.state_root, now, oldId]);
146
155
  for (const table of projectReferenceTables()) {
@@ -232,9 +241,13 @@ export function findProjectByRoot(context, root) {
232
241
  return context.db.get("SELECT * FROM projects WHERE root = ?", [root]);
233
242
  }
234
243
  export function requireProjectByRoot(context, root) {
235
- const project = findProjectByRoot(context, root);
244
+ // Callers may receive a path through a native adapter or shell. Resolve
245
+ // aliases such as macOS `/var` -> `/private/var` before comparing with the
246
+ // canonical root persisted in the project registry.
247
+ const canonical = normalizeStoredRoot(root);
248
+ const project = findProjectByRoot(context, canonical);
236
249
  if (!project) {
237
- throw new AppError("not_found", `Project is not registered: ${root}`, 1);
250
+ throw new AppError("not_found", `Project is not registered: ${canonical}`, 1);
238
251
  }
239
252
  return project;
240
253
  }