@deksden-com/dd-flow-cli 0.9.0-beta.123 → 0.9.0-beta.127

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 (122) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +33 -1
  3. package/dist/build-info.json +7 -7
  4. package/dist/cli/argument-values.js +38 -0
  5. package/dist/cli/command-inputs.js +4 -3
  6. package/dist/cli/help.js +1 -0
  7. package/dist/cli/hook-ingress.js +1 -1
  8. package/dist/cli/run-cli.js +126 -193
  9. package/dist/cli/runtime-maintenance-diagnostics.js +46 -0
  10. package/dist/cli/runtime-maintenance-ingress.js +51 -0
  11. package/dist/cli/runtime-process.js +128 -0
  12. package/dist/cli.js +8 -5
  13. package/dist/domain/stage-catalog.js +1 -1
  14. package/dist/harness-runtime/bin/dd-agy.mjs +3 -2
  15. package/dist/harness-runtime/bin/dd-codex.mjs +3 -2
  16. package/dist/harness-runtime/bin/dd-droid.mjs +3 -2
  17. package/dist/harness-runtime/bin/dd-grok.mjs +16 -6
  18. package/dist/harness-runtime/bin/dd-opencode.mjs +3 -2
  19. package/dist/harness-runtime/bin/dd-zcode.mjs +20 -16
  20. package/dist/harness-runtime/lib/adapter-timeouts.d.mts +5 -0
  21. package/dist/harness-runtime/lib/adapter-timeouts.mjs +34 -0
  22. package/dist/harness-runtime/lib/codex-capacity-policy.d.mts +10 -0
  23. package/dist/harness-runtime/lib/codex-capacity-policy.mjs +89 -0
  24. package/dist/harness-runtime/lib/daemon-observation.d.mts +2 -0
  25. package/dist/harness-runtime/lib/daemon-observation.mjs +163 -0
  26. package/dist/harness-runtime/lib/daemon-operations.mjs +159 -15
  27. package/dist/harness-runtime/lib/dd-agy-daemon.mjs +215 -86
  28. package/dist/harness-runtime/lib/dd-agy.mjs +11 -13
  29. package/dist/harness-runtime/lib/dd-codex-daemon.mjs +47 -23
  30. package/dist/harness-runtime/lib/dd-codex.mjs +257 -99
  31. package/dist/harness-runtime/lib/dd-droid-daemon.mjs +32 -30
  32. package/dist/harness-runtime/lib/dd-droid.mjs +117 -31
  33. package/dist/harness-runtime/lib/dd-grok-daemon.mjs +96 -38
  34. package/dist/harness-runtime/lib/dd-grok.mjs +15 -13
  35. package/dist/harness-runtime/lib/dd-opencode-daemon.mjs +54 -20
  36. package/dist/harness-runtime/lib/dd-opencode.mjs +44 -14
  37. package/dist/harness-runtime/lib/dd-zcode-daemon.mjs +81 -76
  38. package/dist/harness-runtime/lib/dd-zcode.mjs +204 -116
  39. package/dist/harness-runtime/lib/driver-recovery.mjs +12 -2
  40. package/dist/harness-runtime/lib/lease-renewal.d.mts +8 -0
  41. package/dist/harness-runtime/lib/lease-renewal.mjs +23 -0
  42. package/dist/harness-runtime/lib/lifecycle-command.d.mts +3 -3
  43. package/dist/harness-runtime/lib/lifecycle-command.mjs +74 -20
  44. package/dist/harness-runtime/lib/managed-daemon.d.mts +11 -0
  45. package/dist/harness-runtime/lib/managed-daemon.mjs +321 -65
  46. package/dist/harness-runtime/lib/native-children.d.mts +13 -0
  47. package/dist/harness-runtime/lib/native-children.mjs +78 -0
  48. package/dist/harness-runtime/lib/native-hook-command.d.mts +4 -0
  49. package/dist/harness-runtime/lib/native-hook-command.mjs +115 -11
  50. package/dist/harness-runtime/lib/native-streams.mjs +45 -0
  51. package/dist/harness-runtime/lib/observation-clock.d.mts +18 -0
  52. package/dist/harness-runtime/lib/observation-clock.mjs +73 -17
  53. package/dist/harness-runtime/lib/operation-errors.d.mts +14 -0
  54. package/dist/harness-runtime/lib/operation-errors.mjs +105 -8
  55. package/dist/harness-runtime/lib/operation-progress.d.mts +7 -0
  56. package/dist/harness-runtime/lib/operation-progress.mjs +112 -0
  57. package/dist/harness-runtime/lib/process-json.mjs +136 -38
  58. package/dist/harness-runtime/lib/tool-observations.d.mts +1 -0
  59. package/dist/harness-runtime/lib/tool-observations.mjs +5 -2
  60. package/dist/harness-runtime/lib/zcode-usage.mjs +3 -2
  61. package/dist/runtime/context.js +8 -4
  62. package/dist/schemas/flow-run.schema.json +1 -0
  63. package/dist/schemas/plan-review-decision.schema.json +3 -3
  64. package/dist/schemas/run-control-receipt.schema.json +19 -0
  65. package/dist/services/code-checks.js +93 -27
  66. package/dist/services/codex-capacity.js +251 -74
  67. package/dist/services/codex-hook-delivery.js +2 -2
  68. package/dist/services/compatibility-preflight.js +4 -1
  69. package/dist/services/controller-fanout.js +9 -75
  70. package/dist/services/engines.js +70 -20
  71. package/dist/services/eval-snapshots.js +179 -40
  72. package/dist/services/external-work-launch.js +21 -9
  73. package/dist/services/harness-adapter.js +195 -25
  74. package/dist/services/hooks.js +65 -21
  75. package/dist/services/lifecycle-invocations.js +404 -32
  76. package/dist/services/managed-lease-monitor.js +128 -0
  77. package/dist/services/managed-processes.js +117 -53
  78. package/dist/services/merge-server.js +66 -23
  79. package/dist/services/recovery-observation-budget.js +67 -7
  80. package/dist/services/recovery-snapshot-database.js +7 -4
  81. package/dist/services/review-deferral.js +23 -0
  82. package/dist/services/run-control-receipt.js +1 -1
  83. package/dist/services/run-control-worker.js +177 -118
  84. package/dist/services/run-control.js +99 -8
  85. package/dist/services/run-controller-adapter.js +177 -51
  86. package/dist/services/run-controller-capture.js +14 -2
  87. package/dist/services/run-controller-process.js +11 -4
  88. package/dist/services/run-controller-recovery.js +17 -0
  89. package/dist/services/run-controller-state.js +68 -7
  90. package/dist/services/run-controller.js +207 -107
  91. package/dist/services/run-fork.js +8 -3
  92. package/dist/services/run-recovery-guard.js +75 -0
  93. package/dist/services/run-recovery-runtime.js +21 -30
  94. package/dist/services/run-recovery.js +17 -13
  95. package/dist/services/runs.js +40 -7
  96. package/dist/services/runtime-budget.js +32 -15
  97. package/dist/services/runtime-scope-control.js +8 -0
  98. package/dist/services/runtime-scope-resume.js +30 -8
  99. package/dist/services/runtime-scope-worker.js +118 -73
  100. package/dist/services/runtime-service.js +6 -7
  101. package/dist/services/scope-writer-cleanup.js +3 -2
  102. package/dist/services/stage-context.js +135 -0
  103. package/dist/services/stage-lifecycle.js +3 -15
  104. package/dist/services/stage-pause.js +3 -2
  105. package/dist/services/usage.js +14 -3
  106. package/dist/services/verification-matrix.js +30 -1
  107. package/dist/services/vnext-code-review.js +26 -13
  108. package/dist/services/vnext-code.js +10 -8
  109. package/dist/services/vnext-contracts.js +10 -1
  110. package/dist/services/vnext-execution-profile.js +75 -2
  111. package/dist/services/vnext-fanout.js +5 -0
  112. package/dist/services/vnext-merge.js +30 -11
  113. package/dist/services/vnext-plan-review.js +102 -35
  114. package/dist/services/vnext-plan.js +9 -7
  115. package/dist/services/vnext-protocolize.js +15 -9
  116. package/dist/services/vnext-specify.js +6 -6
  117. package/dist/services/work-registry.js +43 -6
  118. package/dist/services/workspace-bootstrap.js +14 -3
  119. package/dist/shared/errors.js +11 -6
  120. package/dist/storage/agent-output.js +26 -0
  121. package/dist/storage/database.js +80 -21
  122. package/package.json +7 -4
@@ -0,0 +1,112 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
+ import { createHash } from 'node:crypto';
3
+ import { open, readFile } from 'node:fs/promises';
4
+ import { constants } from 'node:fs';
5
+ import path from 'node:path';
6
+ import { writeJsonAtomic } from './runner-events.mjs';
7
+
8
+ export const OPERATION_PROGRESS_CONTRACT = 'dd-flow/operation-progress@1';
9
+ const context = new AsyncLocalStorage(), flights = new Map();
10
+ const fields = ['operation_id', 'daemon_id', 'operation', 'params_sha256', 'owner_id', 'generation'];
11
+ const phases = new Set(['accepted','ownership_confirmed','admission_confirmed','native_dispatched','native_progress','native_outcome_retained','settlement_started','settlement_completed','settlement_pending']);
12
+ export function operationDirectory(root,id) {
13
+ if (typeof root!=='string' || !path.isAbsolute(root) || typeof id!=='string' || !id || id.length>1_024) throw Object.assign(new Error('invalid operation observation binding'),{code:'operation_progress_invalid'});
14
+ return path.join(root, 'operations', createHash('sha256').update(id).digest('hex'));
15
+ }
16
+ export async function readOperationRecord(root, id, name) {
17
+ try {
18
+ const handle = await open(path.join(operationDirectory(root, id), name), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
19
+ try {
20
+ const stat = await handle.stat();
21
+ if (!stat.isFile() || name === 'progress.json' && stat.size > 16_384) throw Object.assign(new Error('invalid operation record file'), {code:'operation_progress_invalid'});
22
+ const bytes = name === 'progress.json'
23
+ ? await handle.read({buffer:Buffer.alloc(16_385),position:0}).then(({buffer,bytesRead})=>buffer.subarray(0,bytesRead))
24
+ : await handle.readFile();
25
+ if (name === 'progress.json' && bytes.length > 16_384) throw Object.assign(new Error('invalid operation record file'), {code:'operation_progress_invalid'});
26
+ try {
27
+ const value = JSON.parse(bytes.toString('utf8'));
28
+ if (name === 'progress.json' && (!value || typeof value !== 'object' || Array.isArray(value))) throw new Error('operation progress must be an object');
29
+ return value;
30
+ }
31
+ catch (cause) { if (name !== 'progress.json') throw cause; throw Object.assign(new Error('invalid operation progress JSON'), {code:'operation_progress_invalid',cause}); }
32
+ } finally { await handle.close(); }
33
+ } catch (error) { if (error.code === 'ENOENT') return null; throw error; }
34
+ }
35
+ export async function readOperationProgress(root, id, expected = {}) {
36
+ const requested = await readOperationRecord(root, id, 'requested.json');
37
+ const progress = await readOperationRecord(root, id, 'progress.json');
38
+ if (!progress) return null;
39
+ const invalid = () => Object.assign(new Error('operation progress binding is invalid'), { code: 'operation_progress_invalid', details: { operation_id: id } });
40
+ if (!requested || progress.schema_id !== OPERATION_PROGRESS_CONTRACT || progress.operation_id !== id
41
+ || typeof progress.operation!=='string' || !progress.operation || progress.operation.length>128
42
+ || !/^[a-f0-9]{64}$/.test(progress.params_sha256 ?? '')
43
+ || ['daemon_id','owner_id'].some(field=>progress[field]!==null && (typeof progress[field]!=='string' || !progress[field] || progress[field].length>1_024))
44
+ || progress.generation!==null && (!Number.isSafeInteger(progress.generation) || progress.generation<0)
45
+ || !Number.isSafeInteger(progress.sequence) || progress.sequence < 1 || !phases.has(progress.phase)
46
+ || !Number.isFinite(Date.parse(requested.requested_at))
47
+ || !Number.isFinite(Date.parse(progress.observed_at)) || Date.parse(progress.observed_at) > Date.now()
48
+ || Date.parse(progress.observed_at) < Date.parse(requested.requested_at)
49
+ || fields.some(field => (progress[field] ?? null) !== (requested[field] ?? null))) throw invalid();
50
+ assertExpectedBinding(requested, expected, id);
51
+ return progress;
52
+ }
53
+ function assertExpectedBinding(requested, expected, id) {
54
+ if (fields.some(field => Object.hasOwn(expected, field) && (expected[field] ?? null) !== (requested[field] ?? null))) {
55
+ throw Object.assign(new Error('retained operation differs from dispatched request'), {code:'operation_conflict',details:{operation_id:id}});
56
+ }
57
+ }
58
+ export function publishOperationProgress(root, id, phase, metadata = {}) {
59
+ if (!phases.has(phase)) return Promise.reject(Object.assign(new Error('unknown operation progress phase'), {code:'operation_progress_invalid'}));
60
+ const key = `${root}\0${id}`;
61
+ const next = (flights.get(key) ?? Promise.resolve()).catch(() => {}).then(async () => {
62
+ const requested = await readOperationRecord(root, id, 'requested.json');
63
+ if (!requested) return null;
64
+ const prior = await readOperationProgress(root, id);
65
+ const observedAt = metadata.observed_at ?? new Date().toISOString();
66
+ if (prior && Date.parse(observedAt)<Date.parse(prior.observed_at)) return prior;
67
+ if (prior && phase === prior.phase && phase !== 'native_progress') return prior;
68
+ if (prior && phase === 'native_progress' && Date.parse(observedAt) - Date.parse(prior.observed_at) < 1_000) return prior;
69
+ const value = { schema_id: OPERATION_PROGRESS_CONTRACT,
70
+ ...Object.fromEntries(fields.map(field => [field, requested[field] ?? null])),
71
+ sequence: (prior?.sequence ?? 0) + 1, phase, observed_at: observedAt,
72
+ ...Object.fromEntries(['native_request_id', 'session_id'].filter(field => typeof metadata[field] === 'string').map(field => [field, metadata[field].slice(0, 256)])) };
73
+ if (!Number.isFinite(Date.parse(observedAt)) || Date.parse(observedAt) > Date.now() || Date.parse(observedAt) < Date.parse(requested.requested_at)) throw Object.assign(new Error('invalid progress event timestamp'), { code: 'operation_progress_invalid' });
74
+ await writeJsonAtomic(path.join(operationDirectory(root, id), 'progress.json'), value);
75
+ return value;
76
+ });
77
+ flights.set(key, next);
78
+ void next.finally(() => { if (flights.get(key) === next) flights.delete(key); }).catch(() => {});
79
+ return next;
80
+ }
81
+ export const withOperationProgress = (root, id, action) => context.run({ root, id }, action);
82
+ export function operationProgressReporter() {
83
+ const binding = context.getStore();
84
+ return (phase, metadata) => binding ? publishOperationProgress(binding.root, binding.id, phase, metadata) : Promise.resolve(null);
85
+ }
86
+ export const noteOperationProgress = (phase, metadata) => operationProgressReporter()(phase, metadata);
87
+
88
+ /** Confirmed admission waiting is ownership evidence, not native activity. */
89
+ export async function publishOperationWaiting(root,id,receipt) {
90
+ const requested = await readOperationRecord(root,id,'requested.json');
91
+ if (!requested) return;
92
+ const prior = await readOperationRecord(root,id,'admission-wait.json');
93
+ if (prior?.state === (receipt.admitted ? 'admitted' : 'provider_capacity') && Date.now()-Date.parse(prior.observed_at)<1_000) return;
94
+ const value = { schema_id:'dd-flow/operation-waiting@1', ...Object.fromEntries(fields.map(field=>[field,requested[field] ?? null])), observed_at:new Date().toISOString(), lease_expires_at:receipt.lease_expires_at, process_id:receipt.process_id, state:receipt.admitted ? 'admitted' : 'provider_capacity' };
95
+ await writeJsonAtomic(path.join(operationDirectory(root,id),'admission-wait.json'),value);
96
+ }
97
+ export async function readOperationWaiting(root,id,expected={}) {
98
+ const requested = await readOperationRecord(root,id,'requested.json'), waiting = await readOperationRecord(root,id,'admission-wait.json');
99
+ if (!waiting || waiting.state!=='provider_capacity') return false;
100
+ if (!requested || waiting.schema_id!=='dd-flow/operation-waiting@1' || fields.some(field=>(waiting[field] ?? null)!==(requested[field] ?? null))) throw Object.assign(new Error('admission waiting binding differs'),{code:'operation_progress_invalid'});
101
+ assertExpectedBinding(requested, expected, id);
102
+ if (!Number.isFinite(Date.parse(waiting.observed_at)) || Date.parse(waiting.observed_at)>Date.now() || Date.parse(waiting.observed_at)<Date.parse(requested.requested_at) || typeof waiting.process_id!=='string' || !waiting.process_id) throw Object.assign(new Error('invalid admission observation'),{code:'operation_progress_invalid'});
103
+ if (Date.now()-Date.parse(waiting.observed_at)>30_000) return false;
104
+ if (!Number.isFinite(Date.parse(waiting.lease_expires_at)) || Date.parse(waiting.lease_expires_at)<=Date.now()) return false;
105
+ const daemon = JSON.parse(await readFile(path.join(root,'daemon.json'),'utf8'));
106
+ if ((daemon.daemon_id ?? daemon.config?.daemonId)!==waiting.daemon_id || daemon.config?.runtime_owner?.owner_id!==waiting.owner_id || ['stopping','clean','unclean','cleanup_failed','failed'].includes(daemon.shutdown_state)) return false;
107
+ if (waiting.process_id !== daemon.resource_process?.id) return false;
108
+ try { const fence = JSON.parse(await readFile(path.join(root,'operation-fence.json'),'utf8')); if (fence.daemon_id===waiting.daemon_id && fence.generation>waiting.generation) return false; } catch(error) { if(error.code!=='ENOENT') throw error; }
109
+ try { process.kill(daemon.pid,0); } catch { return false; }
110
+ return true;
111
+ }
112
+ export function suspendOperationClock(clock) { clock.lastWall = clock.wall(); clock.lastMono = clock.monotonic(); }
@@ -1,21 +1,32 @@
1
1
  import { spawn } from "node:child_process";
2
- import { reportedError } from "./operation-errors.mjs";
2
+ import { errorRecord, reportedError } from "./operation-errors.mjs";
3
+ import { ObservationClock, validateObservationDuration } from "./observation-clock.mjs";
4
+ import { terminateCommandTree } from "./native-hook-command.mjs";
3
5
 
4
6
  function failure(message, code) {
5
7
  return Object.assign(new Error(message), { code });
6
8
  }
7
9
 
8
- export function reportedFailure(stdout, stderr, fallback, fallbackCode = "flow_reconciliation_failed") {
10
+ export function reportedFailure(stdout, stderr, fallback, fallbackCode = "flow_reconciliation_failed", retainedError = null) {
9
11
  for (const text of [stdout, stderr]) {
10
- for (const line of [text.trim(), ...text.trim().split(/\n/).reverse()]) {
12
+ // Bounded CLI transport may prepend JSONL phase records to the usual
13
+ // pretty-printed error envelope. Remove only our diagnostics, not arbitrary
14
+ // text or a substring that might masquerade as an authoritative receipt.
15
+ const envelope = text.split(/\n/).filter(line => {
16
+ try { return !["native_hook_phase", "process_maintenance_phase"].includes(JSON.parse(line)?.kind); }
17
+ catch { return true; }
18
+ }).join("\n").trim();
19
+ for (const line of [text.trim(), envelope, ...text.trim().split(/\n/).reverse()]) {
11
20
  try {
12
21
  const value = JSON.parse(line);
22
+ if (["native_hook_phase", "process_maintenance_phase"].includes(value?.kind)) continue;
13
23
  const error = value?.error;
14
24
  if (typeof error?.code === "string" && error.code) return reportedError(error, fallback);
15
25
  if (typeof value?.code === "string" && value.code) return reportedError({ ...value, message: typeof error === "string" ? error : value.message }, fallback);
16
26
  } catch { /* Not a structured CLI error line. */ }
17
27
  }
18
28
  }
29
+ if (retainedError) return reportedError(retainedError, fallback);
19
30
  return failure(stderr.trim() || stdout.trim() || fallback, fallbackCode);
20
31
  }
21
32
 
@@ -23,49 +34,136 @@ function processTarget(executable, args) {
23
34
  return /\.[cm]?js$/.test(executable) ? { command: process.execPath, args: [executable, ...args] } : { command: executable, args };
24
35
  }
25
36
 
26
- export async function commandJson(bin, args, { cwd, env = {}, input = null, onProgress = null } = {}) {
37
+ // These are utility/transport children, never a second productive-work owner.
38
+ async function command(bin, args, json, { cwd, env = {}, input = null, onProgress = null,
39
+ timeoutMs = 30_000, phase = "utility", signal = null, onDiagnostic = null } = {}) {
40
+ if (timeoutMs !== null) validateObservationDuration(timeoutMs);
41
+ if (timeoutMs === null && phase !== "native-work") throw new RangeError("only native-work may omit the utility quiet timeout");
42
+ if (signal?.aborted) throw signal.reason ?? failure("command aborted", "command_aborted");
43
+ const executable = bin === "dd-flow" && typeof env.DD_FLOW_BIN === "string" ? env.DD_FLOW_BIN : bin;
44
+ const target = processTarget(executable, json ? [...args, "--json"] : args);
27
45
  return await new Promise((resolve, reject) => {
28
- // A canonical runtime may intentionally shadow the ambient `dd-flow`
29
- // executable with its captured engine. Keep that choice in the supplied
30
- // environment rather than letting a host-global shim leak into a run.
31
- const executable = bin === "dd-flow" && typeof env.DD_FLOW_BIN === "string" ? env.DD_FLOW_BIN : bin;
32
- const target = processTarget(executable, [...args, "--json"]);
33
- const child = spawn(target.command, target.args, { cwd, env: { ...process.env, ...env }, stdio: [input === null ? "ignore" : "pipe", "pipe", "pipe"] });
34
- let stdout = ""; let stderr = ""; let pendingStderr = ""; let settled = false; let progressError = null;
35
- const finish = (error, value) => { if (settled) return; settled = true; error ? reject(error) : resolve(value); };
36
- const deliverProgress = (line) => {
37
- let value; try { value = JSON.parse(line); } catch { return; }
38
- if (typeof onProgress !== "function" || progressError) return;
39
- try { const returned = onProgress(value); if (returned && typeof returned.then === "function") { Promise.resolve(returned).catch(() => {}); throw failure("commandJson onProgress must be synchronous", "progress_callback_async"); } }
40
- catch (error) { progressError = error; child.kill("SIGKILL"); }
46
+ const child = spawn(target.command, target.args, { cwd, env: { ...process.env, ...env },
47
+ detached: process.platform !== "win32", stdio: [input === null ? "ignore" : "pipe", "pipe", "pipe"] });
48
+ const maxOutput = 8 * 1024 * 1024, maxTail = 64 * 1024, maxLine = 1024 * 1024;
49
+ let stdout = "", stderr = "", pendingStderr = "", settled = false, primary = null, cursor = 0, droppingStructured = false;
50
+ let outputBytes = 0, cleanupTimer = null, inputError = null, termination = null, retainedError = null, prettyError = "";
51
+ const phases = new Set();
52
+ const clock = timeoutMs === null ? null : new ObservationClock({ timeoutMs });
53
+ const productive = () => { cursor++; clock?.sample(cursor, Date.now()); };
54
+ const finish = (error, value) => {
55
+ if (settled) return; settled = true;
56
+ clearInterval(timer); clearTimeout(cleanupTimer); signal?.removeEventListener("abort", abort);
57
+ error ? reject(error) : resolve(value);
41
58
  };
42
- child.stdout.setEncoding("utf8").on("data", (data) => { stdout += data; });
43
- child.stderr.setEncoding("utf8").on("data", (data) => {
44
- stderr += data; pendingStderr += data;
59
+ const stop = error => {
60
+ if (primary || settled) return;
61
+ primary = error;
62
+ termination = terminateCommandTree(child).catch(cause => { if (cause.code !== "ESRCH") primary.cleanup_error = cause; });
63
+ // A missing close after owned SIGKILL is cleanup uncertainty, not success.
64
+ cleanupTimer = setTimeout(() => {
65
+ primary.cleanup_error ??= failure("owned utility child did not close", "command_cleanup_unconfirmed");
66
+ finish(primary);
67
+ }, 5_000);
68
+ };
69
+ const abort = () => stop(signal.reason ?? failure("command aborted", "command_aborted"));
70
+ const deliverProgress = line => {
71
+ let value;
72
+ // Parse a bounded whole pretty envelope before its diagnostic bytes leave
73
+ // the tail. An oversized envelope cannot become plain-text keepalive.
74
+ if (prettyError === null) { if (line.trimEnd() === "}") prettyError = ""; return; }
75
+ const candidate = prettyError ? `${prettyError}\n${line}` : line;
76
+ if (prettyError && candidate.length > maxTail) { prettyError = null; return; }
77
+ try { value = JSON.parse(candidate); prettyError = ""; }
78
+ catch {
79
+ if (!prettyError && !line.trimStart().startsWith("{")) { productive(); return; }
80
+ prettyError = candidate.length > maxTail ? null : candidate; return;
81
+ }
82
+ const kind = value?.kind;
83
+ const passive = ["heartbeat", "poll", "status", "elapsed", "wait", "waiting"].some(marker => [kind, value?.phase, value?.action].includes(marker));
84
+ if (!["native_hook_phase", "process_maintenance_phase"].includes(kind)) {
85
+ const error = typeof value?.error?.code === "string" && value.error.code ? value.error
86
+ : typeof value?.code === "string" && value.code ? { ...value, message: typeof value.error === "string" ? value.error : value.message } : null;
87
+ // Retain one bounded typed summary independently of the diagnostic tail.
88
+ // It is consulted only on failed exit, after authoritative stdout.
89
+ if (error) retainedError = errorRecord(error);
90
+ }
91
+ if (["native_hook_phase", "process_maintenance_phase"].includes(kind)) {
92
+ const marker = JSON.stringify([kind, value.phase, value.operation_id ?? value.process_id]);
93
+ if (typeof value.phase === "string" && !passive && !phases.has(marker)) { phases.add(marker); productive(); }
94
+ } else if (!passive) productive();
95
+ if (typeof onProgress !== "function" || primary) return;
96
+ try {
97
+ const returned = onProgress(value);
98
+ if (returned && typeof returned.then === "function") {
99
+ Promise.resolve(returned).catch(() => {});
100
+ throw failure("command onProgress must be synchronous", "progress_callback_async");
101
+ }
102
+ } catch (error) { stop(error); }
103
+ };
104
+ const diagnostic = (data, stream) => {
105
+ try {
106
+ const returned = onDiagnostic?.(data, stream);
107
+ if (returned && typeof returned.then === "function") {
108
+ Promise.resolve(returned).catch(() => {});
109
+ throw failure("command onDiagnostic must be synchronous", "progress_callback_async");
110
+ }
111
+ } catch (error) { stop(error); }
112
+ };
113
+ const timer = clock ? setInterval(() => {
114
+ if (clock.sample(cursor)) stop(Object.assign(failure("command observation became quiet", "command_observation_lost"),
115
+ { details: { phase, timeout_ms: timeoutMs, observation_lost: clock.observationLost } }));
116
+ }, Math.max(1, Math.min(1_000, timeoutMs / 4))) : null;
117
+ clock?.sample(cursor);
118
+ signal?.addEventListener("abort", abort, { once: true });
119
+ child.stdout.setEncoding("utf8").on("data", data => {
120
+ productive();
121
+ diagnostic(data, "stdout"); outputBytes += Buffer.byteLength(data);
122
+ if (outputBytes > maxOutput) stop(failure("command final output exceeds 8 MiB", "command_output_limit"));
123
+ else stdout += data;
124
+ });
125
+ child.stderr.setEncoding("utf8").on("data", data => {
126
+ diagnostic(data, "stderr"); stderr = (stderr + data).slice(-maxTail);
127
+ if (droppingStructured) {
128
+ const end = data.indexOf("\n");
129
+ if (end < 0) return;
130
+ data = data.slice(end + 1); droppingStructured = false;
131
+ }
132
+ pendingStderr += data;
45
133
  for (;;) {
46
134
  const end = pendingStderr.indexOf("\n"); if (end < 0) break;
47
135
  const line = pendingStderr.slice(0, end); pendingStderr = pendingStderr.slice(end + 1);
48
- deliverProgress(line);
136
+ if (line.length <= maxLine) deliverProgress(line);
137
+ }
138
+ if (pendingStderr && prettyError === "" && !pendingStderr.trimStart().startsWith("{")) productive();
139
+ // Plain streaming diagnostics are not the final reply and cannot kill a
140
+ // productive operation merely because their cumulative size is large.
141
+ if (pendingStderr.length > maxLine) {
142
+ droppingStructured = prettyError !== "" || pendingStderr.trimStart().startsWith("{");
143
+ pendingStderr = droppingStructured ? "" : pendingStderr.slice(-maxTail);
49
144
  }
50
145
  });
51
- child.once("error", (error) => finish(progressError ?? error));
52
- if (input !== null) child.stdin.end(input);
53
- child.on("close", (status) => {
146
+ child.once("error", error => { if (!child.pid) finish(primary ?? error); else stop(error); });
147
+ if (input !== null) {
148
+ child.stdin.on("error", error => { inputError = error; if (error.code !== "EPIPE") stop(error); });
149
+ child.stdin.end(input);
150
+ }
151
+ child.once("close", status => {
54
152
  if (pendingStderr) deliverProgress(pendingStderr);
55
- if (progressError) return finish(progressError);
56
- if (status !== 0) return finish(reportedFailure(stdout, stderr, `${executable} exited ${status}`));
57
- try { const value = JSON.parse(stdout.trim()); if (value?.ok === false) { const error = value.error && typeof value.error === "object" ? value.error : value; if (typeof error.code === "string") return finish(reportedError(error, `${bin} returned an error envelope`)); } finish(null, value); }
58
- catch (error) { finish(failure(`${bin} returned invalid JSON: ${error.message}`, "flow_reconciliation_failed")); }
153
+ if (primary) { void (termination ?? Promise.resolve()).then(() => finish(primary)); return; }
154
+ if (status !== 0) return finish(reportedFailure(stdout, stderr, `${executable} exited ${status}`, json ? "flow_reconciliation_failed" : "command_failed", retainedError));
155
+ if (!json) return finish(inputError, stdout.trim());
156
+ try {
157
+ const value = JSON.parse(stdout.trim());
158
+ if (value?.ok === false) {
159
+ const error = value.error && typeof value.error === "object" ? value.error : value;
160
+ return finish(reportedError(error, `${bin} returned an error envelope`));
161
+ }
162
+ if (inputError) return finish(inputError);
163
+ finish(null, value);
164
+ } catch (error) { finish(failure(`${bin} returned invalid JSON: ${error.message}`, "flow_reconciliation_failed")); }
59
165
  });
60
166
  });
61
167
  }
62
-
63
- export async function commandText(bin, args, { cwd, env = {} } = {}) {
64
- return await new Promise((resolve, reject) => {
65
- const executable = bin === "dd-flow" && typeof env.DD_FLOW_BIN === "string" ? env.DD_FLOW_BIN : bin;
66
- const target = processTarget(executable, args);
67
- const child = spawn(target.command, target.args, { cwd, env: { ...process.env, ...env }, stdio: ["ignore", "pipe", "pipe"] }); let stdout = ""; let stderr = "";
68
- child.stdout.setEncoding("utf8").on("data", (data) => { stdout += data; }); child.stderr.setEncoding("utf8").on("data", (data) => { stderr += data; }); child.on("error", reject);
69
- child.on("close", (status) => status === 0 ? resolve(stdout.trim()) : reject(failure(stderr.trim() || stdout.trim() || `${executable} exited ${status}`, "command_failed")));
70
- });
71
- }
168
+ export async function commandJson(bin, args, options = {}) { return await command(bin, args, true, options); }
169
+ export async function commandText(bin, args, options = {}) { return await command(bin, args, false, options); }
@@ -6,6 +6,7 @@ export interface ToolEvent {
6
6
  reason?: string;
7
7
  [key: string]: unknown;
8
8
  }
9
+ export function classifyCodexItem(type: unknown): "tool" | "non_tool" | "unknown";
9
10
  export class ToolObservations {
10
11
  calls: Map<string, ToolEvent>;
11
12
  turns: Map<string, { session: { harness_id: string; session_id: string }; turn_id: string; started: boolean; completed: boolean }>;
@@ -128,11 +128,14 @@ function structuredFailure(value) {
128
128
 
129
129
  const codexTools = new Set(["commandExecution", "fileChange", "mcpToolCall", "dynamicToolCall", "collabAgentToolCall", "webSearch", "imageView", "imageGeneration", "sleep"]);
130
130
  const codexNonTools = new Set(["userMessage", "hookPrompt", "agentMessage", "functionCallOutput", "plan", "reasoning", "subAgentActivity", "enteredReviewMode", "exitedReviewMode", "contextCompaction"]);
131
+ export function classifyCodexItem(type) {
132
+ return codexTools.has(type) ? "tool" : codexNonTools.has(type) ? "non_tool" : "unknown";
133
+ }
131
134
  export function codexAppServerObservation(message) {
132
135
  if (!["item/started", "item/completed"].includes(message?.method)) return null;
133
136
  const { threadId, turnId, item } = message.params ?? {};
134
- if (!item || codexNonTools.has(item.type)) return null;
135
- if (!codexTools.has(item.type)) return { reason: "codex_item_type_unsupported" };
137
+ if (!item || classifyCodexItem(item.type) === "non_tool") return null;
138
+ if (classifyCodexItem(item.type) === "unknown") return { reason: "codex_item_type_unsupported" };
136
139
  if (!threadId || !item.id) return { reason: "tool_identity_missing" };
137
140
  const native = item.status;
138
141
  const failed = ["failed", "declined", "interrupted"].includes(native) || item.type === "commandExecution" && typeof item.exitCode === "number" && item.exitCode !== 0;
@@ -26,8 +26,9 @@ export function normalizeZcodeUsage(usage, read) {
26
26
  totals.requestOutputTokens += tokens.output;
27
27
  for (const [key, value] of [["requestReasoningTokens", tokens.reasoning],
28
28
  ["requestCacheCreationTokens", tokens.cache?.write], ["requestCacheReadTokens", tokens.cache?.read]]) {
29
- if (value !== undefined && !valid(value)) incomplete = true;
30
- else totals[key] += value ?? 0;
29
+ if (value == null) totals[key] = null;
30
+ else if (!valid(value)) incomplete = true;
31
+ else if (totals[key] !== null) totals[key] += value;
31
32
  }
32
33
  totals.requestCount++;
33
34
  }
@@ -1,18 +1,22 @@
1
1
  import { getDatabase } from "../storage/database.js";
2
2
  import { resolveDdFlowHome } from "../storage/paths.js";
3
- export function createRouterContext(env) {
3
+ import { assertImportAdmission } from "../storage/import-guard.js";
4
+ export function createRouterContext(env, deadlineAt) {
4
5
  const ddFlowHome = resolveDdFlowHome(env);
5
6
  return {
6
7
  ddFlowHome,
7
- db: getDatabase(ddFlowHome, "read_existing"),
8
+ db: getDatabase(ddFlowHome, "read_existing", undefined, deadlineAt),
8
9
  now: () => new Date().toISOString()
9
10
  };
10
11
  }
11
- export function createContext(env, mode = "initialize", importPermitId) {
12
+ export function createContext(env, mode = "initialize", importPermitId, options = {}) {
12
13
  const ddFlowHome = resolveDdFlowHome(env);
14
+ if (options.resourceDatabaseMode === "prepared_existing" || options.resourceDatabaseMode === "initialize")
15
+ assertImportAdmission(ddFlowHome, importPermitId);
13
16
  return {
14
17
  ddFlowHome,
15
- db: getDatabase(ddFlowHome, mode, importPermitId, mode === "hook" && env.DD_FLOW_HOOK_DEADLINE_AT ? Number(env.DD_FLOW_HOOK_DEADLINE_AT) : undefined),
18
+ db: getDatabase(ddFlowHome, mode, importPermitId, options.databaseDeadlineAt ?? (mode === "hook" && env.DD_FLOW_HOOK_DEADLINE_AT ? Number(env.DD_FLOW_HOOK_DEADLINE_AT) : undefined)),
19
+ ...options,
16
20
  env,
17
21
  now: () => new Date().toISOString()
18
22
  };
@@ -13,6 +13,7 @@
13
13
  "subject": {"$ref": "#/definitions/identity"},
14
14
  "flow_kind": {"type": "string", "minLength": 1},
15
15
  "verification_matrix_contract": {"const": "dd-flow/verification-matrix@1"},
16
+ "final_check_coverage_contract": {"const": "dd-flow/final-check-coverage@1"},
16
17
  "status": {"enum": ["created", "running", "paused", "waiting_for_user", "done", "failed", "blocked", "cancelled", "discarded"]},
17
18
  "verdict": {"type": ["string", "null"]},
18
19
  "started_at": {"type": "string", "format": "date-time"},
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "$schema": "http://json-schema.org/draft-07/schema#",
3
- "$id": "dd-flow/plan-review-decision@3",
3
+ "$id": "dd-flow/plan-review-decision@4",
4
4
  "type": "object",
5
5
  "additionalProperties": false,
6
6
  "required": ["schema_id", "outcome", "summary", "finding_decisions", "correction"],
7
7
  "properties": {
8
- "schema_id": {"const": "dd-flow/plan-review-decision@3"},
8
+ "schema_id": {"const": "dd-flow/plan-review-decision@4"},
9
9
  "outcome": {"enum": ["accepted", "failed", "cancelled"]},
10
10
  "summary": {"type": "string", "minLength": 1},
11
- "finding_decisions": {"type": "array", "items": {"type": "object", "additionalProperties": false, "required": ["finding_ref", "decision", "reason"], "properties": {"finding_ref": {"type": "string", "pattern": "^WRK-[^/]+/FIND-[0-9]{3}$"}, "decision": {"enum": ["accepted_fix", "rejected", "deferred_as_DEF", "requires_user", "duplicate"]}, "reason": {"type": "string", "minLength": 1}}}},
11
+ "finding_decisions": {"type": "array", "items": {"type": "object", "additionalProperties": false, "required": ["finding_ref", "decision", "reason"], "properties": {"finding_ref": {"type": "string", "pattern": "^WRK-[^/]+/FIND-[0-9]{3}$"}, "decision": {"enum": ["accepted_fix", "rejected", "deferred_as_DEF", "requires_user", "duplicate"]}, "reason": {"type": "string", "minLength": 1}, "duplicate_of": {"type": "string", "pattern": "^WRK-[^/]+/FIND-[0-9]{3}$"}, "def_id": {"type": "string", "pattern": "^DEF-[A-Za-z0-9][A-Za-z0-9_-]*$"}}}},
12
12
  "correction": {"type": "object", "additionalProperties": false, "required": ["status", "previous_plan_revision", "changed_paths", "summary"], "properties": {"status": {"enum": ["applied", "not_required"]}, "previous_plan_revision": {"type": "integer", "minimum": 1}, "changed_paths": {"type": "array", "items": {"type": "string", "minLength": 1}}, "summary": {"type": "string", "minLength": 1}}}
13
13
  }
14
14
  }
@@ -49,6 +49,25 @@
49
49
  "reused": { "type": "boolean" },
50
50
  "next_cursor": { "type": "integer", "minimum": 0 },
51
51
  "run_status": { "type": "string" },
52
+ "finalization": {
53
+ "type": ["object", "null"], "additionalProperties": false,
54
+ "required": ["schema_id", "issuer", "phase", "project_root", "run_id", "controller_id", "generation", "controller_status", "run_status", "status", "owner", "capture", "error"],
55
+ "properties": {
56
+ "schema_id": { "const": "dd-flow/terminal-finalization@1" },
57
+ "issuer": { "const": "dd-flow" },
58
+ "phase": { "const": "terminal_finalization" },
59
+ "project_root": { "type": "string", "minLength": 1 },
60
+ "run_id": { "type": "string", "minLength": 1 },
61
+ "controller_id": { "type": "string", "minLength": 1 },
62
+ "generation": { "type": "integer", "minimum": 0 },
63
+ "controller_status": { "enum": ["recovery_required", "capture_failed"] },
64
+ "run_status": { "enum": ["done", "failed", "cancelled"] },
65
+ "status": { "enum": ["pending", "blocked"] },
66
+ "owner": { "enum": ["live", "absent", "unknown"] },
67
+ "capture": { "enum": ["recorded", "missing"] },
68
+ "error": { "type": "object" }
69
+ }
70
+ },
52
71
  "control": { "type": ["object", "null"] },
53
72
  "operations": { "type": "array", "items": { "type": "object" } },
54
73
  "worker": { "type": ["object", "null"] },