@deksden-com/dd-flow-cli 0.9.0-beta.104 → 0.9.0-beta.105

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 (68) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/build-info.json +3 -3
  3. package/dist/cli/run-cli.js +23 -8
  4. package/dist/harness-runtime/bin/dd-agy.mjs +1 -1
  5. package/dist/harness-runtime/lib/dd-agy-daemon.mjs +2 -2
  6. package/dist/harness-runtime/lib/dd-grok-daemon.mjs +4 -3
  7. package/dist/harness-runtime/lib/delegation-instructions.d.mts +2 -0
  8. package/dist/harness-runtime/lib/delegation-instructions.mjs +8 -1
  9. package/dist/protocol/local-files.js +18 -9
  10. package/dist/runtime/context.js +2 -2
  11. package/dist/services/cleanup.js +81 -32
  12. package/dist/services/code-checks.js +18 -19
  13. package/dist/services/continuation-outcome.js +1 -1
  14. package/dist/services/controller-fanout.js +3 -3
  15. package/dist/services/eval-snapshots.js +69 -41
  16. package/dist/services/execution-policy.js +12 -1
  17. package/dist/services/hooks.js +150 -42
  18. package/dist/services/lanes.js +23 -18
  19. package/dist/services/lifecycle-command.js +11 -2
  20. package/dist/services/lifecycle-invocations.js +291 -54
  21. package/dist/services/managed-processes.js +35 -23
  22. package/dist/services/merge-queue.js +71 -67
  23. package/dist/services/merge-server.js +3 -11
  24. package/dist/services/migrations.js +28 -12
  25. package/dist/services/plan-runtime.js +8 -4
  26. package/dist/services/projects.js +3 -11
  27. package/dist/services/protocols.js +98 -68
  28. package/dist/services/recovery-snapshot-database.js +43 -8
  29. package/dist/services/run-control-worker.js +15 -32
  30. package/dist/services/run-control.js +3 -17
  31. package/dist/services/run-controller-capture.js +58 -27
  32. package/dist/services/run-controller-process.js +21 -30
  33. package/dist/services/run-controller-recovery.js +36 -37
  34. package/dist/services/run-controller.js +81 -86
  35. package/dist/services/run-fork.js +10 -6
  36. package/dist/services/run-projection.js +19 -64
  37. package/dist/services/run-recovery-runtime.js +24 -14
  38. package/dist/services/run-recovery.js +56 -54
  39. package/dist/services/runs.js +218 -160
  40. package/dist/services/runtime-budget.js +19 -45
  41. package/dist/services/runtime-scope-capture.js +8 -6
  42. package/dist/services/runtime-scope-control.js +49 -39
  43. package/dist/services/runtime-scope-resume.js +134 -122
  44. package/dist/services/runtime-scope-worker.js +48 -49
  45. package/dist/services/scope-writer-cleanup.js +26 -0
  46. package/dist/services/stage-context.js +6 -1
  47. package/dist/services/stage-lifecycle.js +1 -1
  48. package/dist/services/stage-pause.js +13 -29
  49. package/dist/services/stage-report-renderer.js +116 -6
  50. package/dist/services/usage.js +2 -9
  51. package/dist/services/vnext-code-review.js +10 -7
  52. package/dist/services/vnext-code.js +9 -17
  53. package/dist/services/vnext-merge.js +64 -21
  54. package/dist/services/vnext-plan-review.js +43 -30
  55. package/dist/services/vnext-plan.js +28 -24
  56. package/dist/services/vnext-protocolize.js +183 -20
  57. package/dist/services/vnext-specify.js +27 -19
  58. package/dist/services/work-registry.js +229 -150
  59. package/dist/storage/database.js +286 -56
  60. package/dist/storage/foreign-key-transaction.js +39 -0
  61. package/dist/storage/immutable-input.js +45 -0
  62. package/dist/storage/import-guard.js +102 -0
  63. package/dist/storage/native-cleanup.js +52 -0
  64. package/dist/storage/projection-lock.js +73 -0
  65. package/dist/storage/writer-contract.js +3 -3
  66. package/dist/storage/writer-migration.js +24 -41
  67. package/package.json +1 -1
  68. package/tools/repair-paused-run-status.mjs +48 -23
@@ -4,6 +4,8 @@ import fs from "node:fs";
4
4
  import path from "node:path";
5
5
  import { DatabaseSync } from "node:sqlite";
6
6
  import { registerStoreWriter } from "../storage/writer-contract.js";
7
+ import { beginSnapshotImport, completeSnapshotImport } from "../storage/import-guard.js";
8
+ import { foreignKeyTransaction } from "../storage/foreign-key-transaction.js";
7
9
  import { createContext } from "../runtime/context.js";
8
10
  import { AppError } from "../shared/errors.js";
9
11
  import { resolveProjectRoot, runtimeProtocolDir, runtimeRunDir } from "../storage/paths.js";
@@ -16,6 +18,7 @@ import { resourceHome } from "./managed-processes.js";
16
18
  import { codexHomeSharedEntries } from "./hooks.js";
17
19
  import { prepareOutputFile } from "../cli/input-preparation.js";
18
20
  import { localEnvironmentAllowed, projectServicePathFilter } from "./workspace-files.js";
21
+ import { cleanupScopeWriters } from "./scope-writer-cleanup.js";
19
22
  /** Capture the only flow boundary which exists before `stage start --bootstrap` creates a RUN. */
20
23
  export function prepareEvalBootstrapSnapshot(context, input) {
21
24
  const projectRoot = resolveProjectRoot(input.projectRoot);
@@ -387,9 +390,12 @@ function assertRunRestoreTargets(context, input, snapshot, projectRoot, manifest
387
390
  if (context.db.get("SELECT 1 FROM projects LIMIT 1") || context.db.get("SELECT 1 FROM runs LIMIT 1"))
388
391
  throw new AppError("restore_home_not_empty", "Snapshot restore cannot replace an initialized runtime", 1);
389
392
  }
390
- export function restoreEvalRunSnapshot(context, input, prepared = prepareEvalRunRestore(context, input)) {
393
+ export function restoreEvalRunSnapshot(context, input, prepared = prepareEvalRunRestore(context, input), prearmedImportId) {
391
394
  const { snapshot, manifest, recovery, projectRoot, sourceRuntime, sourceProject, routingInput, selectedProfile } = prepared;
392
395
  assertRunRestoreTargets(context, input, snapshot, projectRoot, manifest);
396
+ const sourceManifestSha256 = fileHash(path.join(snapshot, "snapshot.json"));
397
+ const importId = prearmedImportId ?? beginSnapshotImport({ home: context.ddFlowHome, projectRoot,
398
+ workspaceRoot: restoredWorkspaceRoot(manifest, projectRoot, context.ddFlowHome), sourceManifestSha256 });
393
399
  clearProjectTree(projectRoot, false);
394
400
  restoreProjectGit(snapshot, manifest.project_git, projectRoot);
395
401
  replaceProjectTree(projectRoot, sourceProject);
@@ -399,7 +405,8 @@ export function restoreEvalRunSnapshot(context, input, prepared = prepareEvalRun
399
405
  const workspaceRoot = restoreWorkspace(snapshot, manifest, projectRoot, context.ddFlowHome);
400
406
  // The dispatch context was intentionally closed before replacing db.sqlite.
401
407
  // A fresh process owns all subsequent normal commands.
402
- const restored = createContext({ ...context.env, DD_FLOW_HOME: context.ddFlowHome });
408
+ const restored = createContext({ ...context.env, DD_FLOW_HOME: context.ddFlowHome }, "initialize", importId);
409
+ let result, primary, failed = false;
403
410
  try {
404
411
  const runtimeLineage = recovery ? null : detachSnapshotRuntimeOwners(restored, snapshot, manifest);
405
412
  rebaseRuntime(restored, manifest.project_root, projectRoot, sourceWorkspaceRoot(manifest), workspaceRoot, manifest.dd_flow_home, context.ddFlowHome, recovery);
@@ -433,7 +440,11 @@ export function restoreEvalRunSnapshot(context, input, prepared = prepareEvalRun
433
440
  fs.mkdirSync(path.dirname(relocationFile), { recursive: true });
434
441
  writeJson(relocationFile, relocation);
435
442
  }
436
- return {
443
+ const expectedWorkspaceRoot = restoredWorkspaceRoot(manifest, projectRoot, context.ddFlowHome);
444
+ if (physicalPath(workspaceRoot) !== physicalPath(expectedWorkspaceRoot))
445
+ throw new AppError("snapshot_workspace_restore_failed", "Restored workspace differs from the import target", 1, { workspace_root: workspaceRoot, expected_workspace_root: expectedWorkspaceRoot });
446
+ completeSnapshotImport(context.ddFlowHome, importId, { projectRoot, workspaceRoot: expectedWorkspaceRoot, sourceManifestSha256 });
447
+ result = {
437
448
  ok: true,
438
449
  schema_id: "dd-flow/eval-run-restore@1",
439
450
  snapshot,
@@ -448,9 +459,19 @@ export function restoreEvalRunSnapshot(context, input, prepared = prepareEvalRun
448
459
  next_command: recovery ? `dd-flow run recovery inspect ${status.run.id} --project-root ${JSON.stringify(projectRoot)} --json` : `dd-flow stage start ${status.run.id} --stage ${manifest.stage_entry} --project-root ${JSON.stringify(projectRoot)} --json`
449
460
  };
450
461
  }
451
- finally {
452
- restored.db.close?.();
462
+ catch (error) {
463
+ primary = error;
464
+ failed = true;
453
465
  }
466
+ const cleanup = cleanupScopeWriters([{ name: restored.ddFlowHome, db: restored.db, rollback: false, close: "always" }]);
467
+ if (failed) {
468
+ if (primary instanceof AppError && cleanup.length)
469
+ primary.details.import_cleanup_errors = cleanup;
470
+ throw primary;
471
+ }
472
+ if (cleanup.length)
473
+ throw new AppError("snapshot_import_cleanup_failed", "Snapshot imported but its connection could not be closed", 1, { effect: "committed", import_complete: true, import_cleanup_errors: cleanup });
474
+ return result;
454
475
  }
455
476
  export function prepareVnextSpecifyRun(context, input) {
456
477
  const projectRoot = resolveProjectRoot(input.projectRoot);
@@ -498,10 +519,11 @@ function assertSnapshotRuntimeDetachAllowed(get) {
498
519
  function detachSnapshotRuntimeOwners(context, snapshot, manifest) {
499
520
  const hasTable = (table) => Boolean(context.db.get("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = ?", [table]));
500
521
  assertSnapshotRuntimeDetachAllowed(sql => context.db.get(sql));
501
- const detachedTables = ["lifecycle_invocations", "run_control_admissions", "run_control_stage_admissions", "run_control_operations", "run_control_workers", "run_controls", "run_controller_events", "run_controller_operations", "run_controllers", "run_recovery_bindings", "work_recovery_segments", "run_recovery_guards", "work_launches", "merge_servers", "merge_sessions", "lane_locks", "lane_waiters"].filter(hasTable);
522
+ const detachedTables = ["lifecycle_packet_assignments", "lifecycle_invocations", "run_control_admissions", "run_control_stage_admissions", "run_control_operations", "run_control_workers", "run_controls", "run_controller_events", "run_controller_operations", "run_controllers", "run_recovery_bindings", "work_recovery_segments", "run_recovery_guards", "work_launches", "merge_servers", "merge_sessions", "lane_locks", "lane_waiters"].filter(hasTable);
502
523
  const runTables = Object.fromEntries([...detachedTables, "runs", "merge_requests", "sessions", "work_sessions"].map(table => [table, context.db.all(`SELECT * FROM ${table}`)]));
503
524
  const resourceFile = path.join(context.ddFlowHome, "runtime.sqlite");
504
525
  const resources = fs.existsSync(resourceFile) ? new DatabaseSync(resourceFile) : null;
526
+ let resourceHeld = false, resourceCommitted = false, result, primary, failed = false;
505
527
  try {
506
528
  if (resources)
507
529
  registerStoreWriter(resources);
@@ -512,42 +534,56 @@ function detachSnapshotRuntimeOwners(context, snapshot, manifest) {
512
534
  fs.writeFileSync(file, `${JSON.stringify({ schema_id: "dd-flow/snapshot-runtime-lineage@1", source_snapshot: snapshot, source_manifest_sha256: fileHash(path.join(snapshot, "snapshot.json")), source_runtime: manifest.dd_flow_home, restored_runtime: context.ddFlowHome, run_id: manifest.run_id, run_tables: runTables, resource_tables: resourceTables }, null, 2)}\n`, { flag: "wx", mode: 0o600 });
513
535
  syncPath(file);
514
536
  syncPath(directory);
515
- context.db.beginWriteTransaction();
516
- try {
517
- context.db.run("UPDATE sessions SET metadata_json = json_set(metadata_json, '$.snapshot_import', json(?))", [JSON.stringify({ lineage_file: file, lineage_sha256: fileHash(file) })]);
518
- for (const table of detachedTables)
519
- context.db.exec(`DELETE FROM ${table}`);
537
+ const lineage = JSON.stringify({ lineage_file: file, lineage_sha256: fileHash(file) });
538
+ context.db.writeTransaction(() => {
539
+ assertSnapshotRuntimeDetachAllowed(sql => context.db.get(sql));
540
+ for (const [table, rows] of Object.entries(runTables))
541
+ if (JSON.stringify(context.db.all(`SELECT * FROM ${table}`)) !== JSON.stringify(rows))
542
+ throw new AppError("snapshot_runtime_changed", "Imported runtime changed during lineage preparation", 1, { table });
543
+ context.db.run("UPDATE sessions SET metadata_json = json_set(metadata_json, '$.snapshot_import', json(?))", [lineage]);
544
+ for (const table of detachedTables) {
545
+ try {
546
+ context.db.exec(`DELETE FROM ${table}`);
547
+ }
548
+ catch (error) {
549
+ throw new AppError("snapshot_runtime_detach_failed", `Could not detach imported ${table} authority`, 1, { table, cause: String(error) });
550
+ }
551
+ }
520
552
  context.db.run("UPDATE merge_requests SET dispatch_owner = NULL, dispatch_lease_token = NULL, dispatch_lease_expires_at = NULL, lock_acquired_at = NULL, adapter_receipt_json = NULL");
521
553
  // Only the copied dedicated registry is touched. Source/external homes
522
554
  // are historical references in the archive and are never opened here.
523
555
  if (resources) {
524
556
  resources.exec("BEGIN IMMEDIATE");
525
- try {
526
- for (const table of Object.keys(resourceTables))
527
- resources.exec(`DELETE FROM ${table}`);
528
- resources.exec("COMMIT");
529
- }
530
- catch (error) {
531
- resources.exec("ROLLBACK");
532
- throw error;
557
+ resourceHeld = true;
558
+ for (const [table, rows] of Object.entries(resourceTables)) {
559
+ if (JSON.stringify(resources.prepare(`SELECT * FROM ${table}`).all()) !== JSON.stringify(rows))
560
+ throw new AppError("snapshot_runtime_changed", "Imported resource evidence changed during lineage preparation", 1, { table });
561
+ resources.exec(`DELETE FROM ${table}`);
533
562
  }
563
+ resources.exec("COMMIT");
564
+ resourceHeld = false;
565
+ resourceCommitted = true;
534
566
  }
535
- context.db.exec("COMMIT");
536
- }
537
- catch (error) {
538
- context.db.exec("ROLLBACK");
539
- throw error;
540
- }
541
- return file;
567
+ });
568
+ result = file;
542
569
  }
543
- finally {
544
- resources?.close();
570
+ catch (error) {
571
+ primary = error;
572
+ failed = true;
545
573
  }
574
+ const cleanup = resources ? cleanupScopeWriters([{ name: resourceFile, db: resources, rollback: resourceHeld, close: "always" }]) : [];
575
+ if (failed || cleanup.length) {
576
+ const details = { effect: resourceCommitted ? "partial" : "unknown", ...(cleanup.length ? { import_cleanup_errors: cleanup } : {}) };
577
+ if (primary instanceof AppError) {
578
+ Object.assign(primary.details, details);
579
+ throw primary;
580
+ }
581
+ throw new AppError("snapshot_runtime_detach_failed", primary instanceof Error ? primary.message : "Imported runtime authority could not be detached", 1, { ...details, ...(failed ? { cause: String(primary) } : {}) });
582
+ }
583
+ return result;
546
584
  }
547
585
  function rebaseRuntime(context, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome, recovery = false) {
548
- context.db.exec("PRAGMA foreign_keys = OFF");
549
- context.db.beginWriteTransaction();
550
- try {
586
+ foreignKeyTransaction(context.db, () => {
551
587
  context.db.run("UPDATE projects SET root = ?", [newRoot]);
552
588
  context.db.run("UPDATE runs SET project_root = ?, workspace_root = ?", [newRoot, newWorkspace]);
553
589
  for (const column of ["runtime_path", "run_dir", "run_index_path", "run_home_path", "run_root", "index_json"])
@@ -575,15 +611,7 @@ function rebaseRuntime(context, oldRoot, newRoot, oldWorkspace, newWorkspace, ol
575
611
  if (!recovery)
576
612
  context.db.run("UPDATE work_sessions SET status = CASE WHEN status = 'running' THEN 'completed' ELSE status END, completed_at = COALESCE(completed_at, ?), updated_at = ?", [context.now(), context.now()]);
577
613
  context.db.run("DELETE FROM codex_session_bindings");
578
- context.db.run("COMMIT");
579
- }
580
- catch (error) {
581
- context.db.exec("ROLLBACK");
582
- throw error;
583
- }
584
- finally {
585
- context.db.exec("PRAGMA foreign_keys = ON");
586
- }
614
+ });
587
615
  // Only operational database paths move. Imported packets, results and
588
616
  // journals retain their historical bytes, including interrupted JSONL tails.
589
617
  // The frozen workspace route is an operational receipt consumed by later
@@ -660,7 +688,7 @@ function restoreWorkspace(snapshot, manifest, projectRoot, ddFlowHome) {
660
688
  restoreIndex(snapshot, "workspace", manifest.workspace, workspaceRoot);
661
689
  return fs.realpathSync(workspaceRoot);
662
690
  }
663
- function restoredWorkspaceRoot(manifest, projectRoot, ddFlowHome) {
691
+ export function restoredWorkspaceRoot(manifest, projectRoot, ddFlowHome) {
664
692
  if (manifest.workspace.kind === "project")
665
693
  return projectRoot;
666
694
  const managedRoot = physicalPath(manifest.dd_flow_home), sourceRoot = physicalPath(manifest.workspace.source_root);
@@ -132,16 +132,27 @@ export function assertSameSessionMerge(input) {
132
132
  throw new AppError("merge_profile_session_conflict", "Same-session MERGE cannot switch execution profiles; select a compatible profile or server MERGE", 2, { previous_stage: stage, previous_profile_id: previous.id, merge_profile_id: merge.id });
133
133
  }
134
134
  }
135
+ /** The RUN identity root and a native Session's fixed launch directory are distinct. */
136
+ export function stageExecutionCwd(input) {
137
+ return path.resolve(input.stage === "specify" || input.stage === "protocolize" ? input.projectRoot
138
+ : input.stage === "merge" ? input.mergeTargetWorkspace ?? input.projectRoot
139
+ : input.workspaceRoot);
140
+ }
135
141
  export function resolveStageTransition(input) {
136
142
  const target = resolveExecutionPolicy(input);
137
143
  const previous = input.fromStage ? resolveExecutionPolicy({ ...input, stage: input.fromStage }).coordinator : null;
138
144
  const compatible = !previous || sameSessionProfile(previous, target.coordinator);
145
+ const previousCwd = input.fromStage ? stageExecutionCwd({ ...input, stage: input.fromStage }) : null;
146
+ const targetCwd = stageExecutionCwd(input);
147
+ const sameWorkspace = previousCwd === null || previousCwd === targetCwd;
139
148
  if (input.stage === "merge" && input.mergeMode === "same_session") {
140
149
  if (!compatible)
141
150
  throw new AppError("merge_profile_session_conflict", "Same-session MERGE cannot replace its current coordinator", 2);
151
+ if (!sameWorkspace)
152
+ throw new AppError("merge_session_workspace_conflict", "Same-session MERGE cannot change the native coordinator workspace", 2, { source_workspace: previousCwd, target_workspace: targetCwd });
142
153
  return { ...target, session_mode: "same_session" };
143
154
  }
144
- return { ...target, session_mode: !compatible || (input.stage === "merge" && input.mergeMode === "server") ? "new_session" : input.stageSessionMode };
155
+ return { ...target, session_mode: !compatible || !sameWorkspace || (input.stage === "merge" && input.mergeMode === "server") ? "new_session" : input.stageSessionMode };
145
156
  }
146
157
  function validateDelegation(value) {
147
158
  if (!value || typeof value !== "object" || Array.isArray(value) || Object.keys(value).some(key => !["mode", "agent_profile_id", "max_parallel"].includes(key)))
@@ -7,6 +7,7 @@ import { AppError } from "../shared/errors.js";
7
7
  import { entityReferenceVariants } from "../shared/entity-references.js";
8
8
  import { parseJsonObject } from "../shared/json.js";
9
9
  import { canonicalPath, ensureDir, resolveProjectRoot } from "../storage/paths.js";
10
+ import { resolveWorkReference } from "../storage/work-references.js";
10
11
  import { appendAudit } from "./audit.js";
11
12
  import { prepareTextFile, prepareJsonText, prepareError } from "../cli/input-preparation.js";
12
13
  import { nativeSessionIdentity, storageSessionId } from "./session-identity.js";
@@ -549,21 +550,22 @@ function recordZcodeLifecycle(context, input, invocationId) {
549
550
  const sessionId = storageSessionId(nativeSessionIdentity("zcode-acp", providerSessionId));
550
551
  const parentSessionId = parentProviderSessionId ? storageSessionId(nativeSessionIdentity("zcode-acp", parentProviderSessionId)) : null;
551
552
  const observedProfile = objectRecord(ddZcode.observedProfile);
553
+ const observedCwd = stringValue(hook.cwd) && path.isAbsolute(String(hook.cwd)) ? resolveProjectRoot(String(hook.cwd)) : null;
552
554
  let matchKey = lifecycle ? lifecycleMatchKey(lifecycle, expectedRoot) : null;
553
555
  const eventNameForReceipt = invocationId ? "ToolCallObserved" : "PreToolUse";
554
556
  const persist = (scope) => {
555
557
  if (scope)
556
558
  matchKey = lifecycleMatchKey(lifecycleFacts(scope.command), expectedRoot, scope);
557
- assertHookEventReplay(context, { projectId: project.id, eventKey, harness: "zcode-acp", providerSessionId, parentSessionId, daemonId, sessionId, turnId: null, eventName: eventNameForReceipt, toolName: toolName ?? "Bash", matchKey, cwd: expectedRoot });
558
- const payload = { ...hook, session_id: providerSessionId, cwd: expectedRoot, tool_name: toolName ?? "Bash", tool_input: rawInput,
559
+ assertHookEventReplay(context, { projectId: project.id, eventKey, harness: "zcode-acp", providerSessionId, parentSessionId, daemonId, sessionId, turnId: null, eventName: eventNameForReceipt, toolName: toolName ?? "Bash", matchKey, cwd: observedCwd });
560
+ const payload = { ...hook, session_id: providerSessionId, cwd: observedCwd, tool_name: toolName ?? "Bash", tool_input: rawInput,
559
561
  command, provider: stringValue(observedProfile.provider), model: stringValue(observedProfile.model), reasoning: stringValue(observedProfile.reasoning), mode: stringValue(observedProfile.mode) };
560
562
  const inserted = recordHookEvent(context, {
561
563
  projectId: project.id, protocolId: null, harness: "zcode-acp", providerSessionId, parentSessionId,
562
564
  daemonId, sessionId, agentId: stringValue(hook.agent_id) ?? stringValue(hook.agentId) ?? null, turnId: null,
563
565
  eventName: eventNameForReceipt, toolName: toolName ?? "Bash", status: "observed", payload, eventKey, matchKey,
564
- transcriptPath: stringValue(hook.transcript_path) ?? null, cwd: expectedRoot
566
+ transcriptPath: stringValue(hook.transcript_path) ?? null, cwd: observedCwd
565
567
  });
566
- recordFlowSessionObservation(context, { projectId: project.id, sessionId, cwd: expectedRoot, toolName: toolName ?? "Bash", eventKey });
568
+ recordFlowSessionObservation(context, { projectId: project.id, sessionId, cwd: observedCwd, toolName: toolName ?? "Bash", eventKey });
567
569
  const result = { ok: true, observed: inserted, duplicate: !inserted, event_key: eventKey, harness: "zcode-acp", session: nativeSessionIdentity("zcode-acp", providerSessionId), ...(parentProviderSessionId ? { parent_session: nativeSessionIdentity("zcode-acp", parentProviderSessionId) } : {}), daemon_id: daemonId };
568
570
  return { ...result, hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...rawInput, command: rewritten } } };
569
571
  };
@@ -602,13 +604,14 @@ export function handleGrokEvent(context, input) {
602
604
  return { ok: true, observed: false, reason: "event_not_participating" };
603
605
  const bootstrapStageStart = lifecycle?.bootstrapStageStart;
604
606
  const expectedRoot = resolveProjectRoot(input.projectRoot);
605
- const matchKey = lifecycle ? lifecycleMatchKey(lifecycle, expectedRoot) : null;
607
+ let matchKey = lifecycle ? lifecycleMatchKey(lifecycle, expectedRoot) : null;
606
608
  if (bootstrapStageStart)
607
609
  registerProject(context, { root: expectedRoot });
608
610
  const project = requireProjectByRoot(context, expectedRoot);
609
611
  const meta = objectRecord(hook._meta);
610
612
  const ddGrok = objectRecord(meta.ddGrok);
611
613
  const observedProfile = objectRecord(ddGrok.observedProfile);
614
+ const observedCwd = stringValue(hook.cwd) && path.isAbsolute(String(hook.cwd)) ? resolveProjectRoot(String(hook.cwd)) : null;
612
615
  const rootProviderSessionId = stringValue(ddGrok.rootProviderSessionId);
613
616
  const providerSessionId = stringValue(hook.session_id) ?? stringValue(hook.sessionId);
614
617
  if (!rootProviderSessionId || !providerSessionId)
@@ -622,23 +625,35 @@ export function handleGrokEvent(context, input) {
622
625
  harness: "grok-acp", providerSessionId, toolUseId: hook.tool_use_id ?? hook.toolUseId ?? null,
623
626
  promptId: hook.prompt_id ?? hook.promptId ?? null, command, turn: hook.turn_id ?? hook.turnId ?? null
624
627
  })).digest("hex");
625
- assertHookEventReplay(context, { projectId: project.id, eventKey, harness: "grok-acp", providerSessionId, parentSessionId, daemonId: daemonId ?? null, sessionId, turnId: stringValue(hook.turn_id) ?? stringValue(hook.turnId) ?? null, eventName: "PreToolUse", toolName: toolName ?? "Bash", matchKey, cwd: expectedRoot });
626
- const payload = {
627
- ...hook, session_id: providerSessionId, agent_id: agentId, cwd: expectedRoot, tool_name: toolName ?? "Bash", tool_input: rawInput,
628
- command, provider: stringValue(observedProfile.provider), model: stringValue(observedProfile.model), reasoning: stringValue(observedProfile.reasoning), mode: stringValue(observedProfile.mode), agent_type: stringValue(hook.subagent_type) ?? stringValue(hook.subagentType)
628
+ const persist = (scope) => {
629
+ if (scope)
630
+ matchKey = lifecycleMatchKey(lifecycleFacts(scope.command), expectedRoot, scope);
631
+ assertHookEventReplay(context, { projectId: project.id, eventKey, harness: "grok-acp", providerSessionId, parentSessionId, daemonId: daemonId ?? null, sessionId, turnId: stringValue(hook.turn_id) ?? stringValue(hook.turnId) ?? null, eventName: "PreToolUse", toolName: toolName ?? "Bash", matchKey, cwd: observedCwd });
632
+ const payload = {
633
+ ...hook, session_id: providerSessionId, agent_id: agentId, cwd: observedCwd, tool_name: toolName ?? "Bash", tool_input: rawInput,
634
+ command, provider: stringValue(observedProfile.provider), model: stringValue(observedProfile.model), reasoning: stringValue(observedProfile.reasoning), mode: stringValue(observedProfile.mode), agent_type: stringValue(hook.subagent_type) ?? stringValue(hook.subagentType)
635
+ };
636
+ const inserted = recordHookEvent(context, {
637
+ projectId: project.id, protocolId: null, harness: "grok-acp", providerSessionId, parentSessionId,
638
+ daemonId: daemonId ?? null, sessionId, agentId: agentId ?? null, turnId: stringValue(hook.turn_id) ?? stringValue(hook.turnId) ?? null,
639
+ eventName: "PreToolUse", toolName: toolName ?? "Bash", status: "observed", payload, eventKey, matchKey, transcriptPath: null, cwd: observedCwd
640
+ });
641
+ recordFlowSessionObservation(context, { projectId: project.id, sessionId, cwd: observedCwd, toolName: toolName ?? "Bash", eventKey });
642
+ return inserted;
629
643
  };
630
- const inserted = recordHookEvent(context, {
631
- projectId: project.id, protocolId: null, harness: "grok-acp", providerSessionId, parentSessionId,
632
- daemonId: daemonId ?? null, sessionId, agentId: agentId ?? null, turnId: stringValue(hook.turn_id) ?? stringValue(hook.turnId) ?? null,
633
- eventName: "PreToolUse", toolName: toolName ?? "Bash", status: "observed", payload, eventKey, matchKey, transcriptPath: null, cwd: expectedRoot
634
- });
635
- recordFlowSessionObservation(context, {
636
- projectId: project.id, sessionId,
637
- cwd: expectedRoot, toolName: toolName ?? "Bash", eventKey
644
+ const result = (inserted) => ({ ok: true, observed: inserted, duplicate: !inserted, event_key: eventKey, session: nativeSessionIdentity("grok-acp", providerSessionId),
645
+ ...(isChild ? { parent_session: nativeSessionIdentity("grok-acp", stringValue(ddGrok.parentProviderSessionId) ?? rootProviderSessionId) } : {}), daemon_id: daemonId ?? null });
646
+ if (!lifecycle)
647
+ return { ...result(context.db.writeTransaction(() => persist())), hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...rawInput, command: commandWithHookEvent(command, eventKey) } } };
648
+ let inserted = false;
649
+ const receipt = observeLifecycleInvocation(context, {
650
+ command, harness: "grok-acp", projectId: project.id,
651
+ identity: { daemonId: daemonId ?? "", rootSessionId: rootProviderSessionId, sessionId: providerSessionId, parentSessionId, toolCallId: eventKey },
652
+ recordReceipt: scope => { inserted = persist(scope); return eventKey; }
638
653
  });
639
- const result = { ok: true, observed: inserted, duplicate: !inserted, event_key: eventKey, session: nativeSessionIdentity("grok-acp", providerSessionId),
640
- ...(isChild ? { parent_session: nativeSessionIdentity("grok-acp", stringValue(ddGrok.parentProviderSessionId) ?? rootProviderSessionId) } : {}), daemon_id: daemonId ?? null };
641
- return { ...result, hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...rawInput, command: commandWithHookEvent(command, eventKey) } } };
654
+ if (receipt.invocationId)
655
+ return { ...result(inserted), invocation_id: receipt.invocationId, hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...rawInput, command: commandWithHookEvent(command, eventKey) } } };
656
+ return { ...result(inserted), hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...rawInput, command: commandWithHookEvent(command, eventKey) } } };
642
657
  }
643
658
  /** Convert a controlled OpenCode tool hook into the shared lifecycle receipt. */
644
659
  export function handleOpenCodeEvent(context, input) {
@@ -654,6 +669,65 @@ export function handleDroidEvent(context, input) {
654
669
  throw new AppError("unsupported_droid_event_schema", "Unsupported Droid event schema", 1);
655
670
  return handleControlledToolEvent(context, input, event, "droid-cli");
656
671
  }
672
+ /** Stable project identity is not the native Session's execution workspace. */
673
+ function controlledLifecycleCwd(context, projectRoot, facts, providerSessionId, auxiliary) {
674
+ if (facts?.bootstrapStageStart)
675
+ return projectRoot;
676
+ const project = context.db.get("SELECT id FROM projects WHERE root = ?", [projectRoot]);
677
+ if (!project)
678
+ return projectRoot;
679
+ let work;
680
+ if (facts?.workId) {
681
+ try {
682
+ work = resolveWorkReference(context, facts.workId, { projectId: project.id });
683
+ }
684
+ catch (error) {
685
+ if (!(error instanceof AppError) || !["not_found", "ambiguous_work_alias"].includes(error.code))
686
+ throw error;
687
+ }
688
+ }
689
+ const reference = work?.run_id ?? facts?.runId ?? auxiliary?.args.options.get("run")?.[0] ?? auxiliary?.args.positional[2];
690
+ if (!reference)
691
+ return projectRoot;
692
+ const run = context.db.get("SELECT id, run_root, workspace_root FROM runs WHERE project_id = ? AND (id = ? OR short_id = ?)", [project.id, reference, reference]);
693
+ if (!run)
694
+ return projectRoot; // Hook ingress does not validate a mistyped CLI target.
695
+ if (work) {
696
+ // PROTOCOLIZE provisions the feature checkout after its coordinator
697
+ // Session has launched in the stable project root. Its transition Work
698
+ // must keep that physical route until the Stage is complete.
699
+ if (context.db.get("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = 'stage_work_memberships'")
700
+ && context.db.get("SELECT 1 FROM stage_work_memberships WHERE project_id = ? AND run_id = ? AND work_id = ? AND stage IN ('specify','protocolize') LIMIT 1", [project.id, run.id, work.work_id]))
701
+ return projectRoot;
702
+ const merge = context.db.get("SELECT target_workspace FROM merge_requests WHERE executor_work_id = ? AND run_id = ?", [work.work_id, run.id]);
703
+ if (merge)
704
+ return resolveProjectRoot(merge.target_workspace);
705
+ if (context.db.get("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = 'work_launches'")) {
706
+ const launch = context.db.get("SELECT provider_session_id, profile_json FROM work_launches WHERE work_id = ? AND status IN ('launching', 'start_requested')", [work.work_id]);
707
+ if (launch?.provider_session_id === providerSessionId && run.run_root) {
708
+ const copy = JSON.parse(launch.profile_json).review_copy?.root;
709
+ if (copy) {
710
+ const target = path.resolve(run.run_root, copy);
711
+ const relative = path.relative(run.run_root, target);
712
+ if (relative === ".." || relative.startsWith(`..${path.sep}`))
713
+ throw new AppError("read_only_workspace_unbound", "Review copy escapes its owning RUN", 1);
714
+ return resolveProjectRoot(target);
715
+ }
716
+ }
717
+ }
718
+ }
719
+ if (facts?.stageName === "specify" || facts?.stageName === "protocolize")
720
+ return projectRoot;
721
+ if (facts?.stageName === "merge") {
722
+ const pending = context.db.all("SELECT target_workspace FROM merge_requests WHERE project_id = ? AND run_id = ? AND status IN ('dispatching','active','action_required') ORDER BY updated_at DESC", [project.id, run.id]);
723
+ const targets = [...new Set(pending.map(item => resolveProjectRoot(item.target_workspace)))];
724
+ if (targets.length > 1)
725
+ throw new AppError("merge_workspace_ambiguous", "Current MERGE requests disagree on the native execution workspace", 1, { run_id: run.id, target_workspaces: targets });
726
+ if (targets.length === 1)
727
+ return targets[0];
728
+ }
729
+ return resolveProjectRoot(run.workspace_root);
730
+ }
657
731
  function handleControlledToolEvent(context, input, event, harness) {
658
732
  const agy = harness === "antigravity-cli";
659
733
  const droid = harness === "droid-cli";
@@ -676,9 +750,7 @@ function handleControlledToolEvent(context, input, event, harness) {
676
750
  throw new AppError(`${errorPrefix}_session_identity_invalid`, `${label} event is missing trusted physical identity`, 1);
677
751
  }
678
752
  const expectedRoot = resolveProjectRoot(input.projectRoot);
679
- if (resolveProjectRoot(directory) !== expectedRoot) {
680
- throw new AppError(`${errorPrefix}_directory_mismatch`, `${label} Session directory does not match the controlled workspace`, 1, { expected: expectedRoot, actual: directory });
681
- }
753
+ const observedCwd = resolveProjectRoot(directory);
682
754
  if (droid && nativeParentId === providerSessionId)
683
755
  throw new AppError("droid_session_identity_invalid", "A Droid Session cannot parent itself", 1);
684
756
  const sessionId = storageSessionId(nativeSessionIdentity(harness, providerSessionId));
@@ -690,8 +762,8 @@ function handleControlledToolEvent(context, input, event, harness) {
690
762
  const inserted = recordHookEvent(context, {
691
763
  projectId: project.id, protocolId: null, harness, providerSessionId, parentSessionId,
692
764
  daemonId, sessionId, agentId: stringValue(session.agent_id) ?? null, turnId: stringValue(event.message_id) ?? null,
693
- eventName: "PostToolUse", toolName, status: "observed", payload: { session_id: providerSessionId, cwd: expectedRoot, tool_name: toolName, status: objectRecord(event.outcome).status },
694
- eventKey: baseKey, matchKey: null, transcriptPath: droid ? stringValue(event.transcript_path) ?? null : null, cwd: expectedRoot
765
+ eventName: "PostToolUse", toolName, status: "observed", payload: { session_id: providerSessionId, cwd: observedCwd, tool_name: toolName, status: objectRecord(event.outcome).status },
766
+ eventKey: baseKey, matchKey: null, transcriptPath: droid ? stringValue(event.transcript_path) ?? null : null, cwd: observedCwd
695
767
  });
696
768
  return { ok: true, observed: inserted, duplicate: !inserted, event_key: baseKey, phase, harness, session: nativeSessionIdentity(harness, providerSessionId), ...(nativeParentId ? { parent_session: nativeSessionIdentity(harness, nativeParentId) } : {}) };
697
769
  }
@@ -699,10 +771,15 @@ function handleControlledToolEvent(context, input, event, harness) {
699
771
  return { ok: true, observed: false, reason: command ? "non_bash_tool" : "event_not_participating" };
700
772
  }
701
773
  const lifecycle = lifecycleFacts(command);
702
- if (!lifecycle && !continuationCommand(command))
774
+ const auxiliary = continuationCommand(command);
775
+ if (!lifecycle && !auxiliary)
703
776
  return { ok: true, observed: false, reason: "event_not_participating" };
777
+ const expectedCwd = controlledLifecycleCwd(context, expectedRoot, lifecycle, providerSessionId, auxiliary);
778
+ if (observedCwd !== expectedCwd) {
779
+ throw new AppError(`${errorPrefix}_directory_mismatch`, `${label} ${agy ? "launch route" : "Session directory"} does not match the assigned execution workspace`, 1, { expected: expectedCwd, actual: observedCwd, ...(agy ? { actual_provenance: stringValue(event.cwd_provenance) ?? "unknown" } : {}) });
780
+ }
704
781
  const bootstrapStageStart = lifecycle?.bootstrapStageStart;
705
- const matchKey = lifecycle ? lifecycleMatchKey(lifecycle, expectedRoot) : null;
782
+ let matchKey = lifecycle ? lifecycleMatchKey(lifecycle, expectedRoot) : null;
706
783
  if (bootstrapStageStart)
707
784
  registerProject(context, { root: expectedRoot });
708
785
  const project = requireProjectByRoot(context, expectedRoot);
@@ -713,28 +790,55 @@ function handleControlledToolEvent(context, input, event, harness) {
713
790
  }
714
791
  }
715
792
  const agentId = stringValue(session.agent_id);
716
- assertHookEventReplay(context, { projectId: project.id, eventKey: baseKey, harness, providerSessionId, parentSessionId, daemonId, sessionId, turnId: stringValue(event.message_id) ?? null, eventName: "PreToolUse", toolName, matchKey, cwd: expectedRoot });
717
- const payload = {
718
- session_id: providerSessionId, agent_id: agentId, cwd: expectedRoot, tool_name: toolName, tool_input: rawInput, command,
719
- provider: stringValue(profile.provider), model: stringValue(profile.model), reasoning: stringValue(droid ? profile.reasoning : profile.variant),
720
- mode: stringValue(droid ? profile.mode : profile.agent), agent_type: stringValue(droid ? profile.agent_type : profile.agent)
793
+ const persist = (scope) => {
794
+ if (scope)
795
+ matchKey = lifecycleMatchKey(lifecycleFacts(scope.command), expectedRoot, scope);
796
+ assertHookEventReplay(context, { projectId: project.id, eventKey: baseKey, harness, providerSessionId, parentSessionId, daemonId, sessionId, turnId: stringValue(event.message_id) ?? null, eventName: "PreToolUse", toolName, matchKey, cwd: observedCwd });
797
+ const payload = {
798
+ session_id: providerSessionId, agent_id: agentId, cwd: observedCwd, tool_name: toolName, tool_input: rawInput, command,
799
+ ...(agy ? { cwd_provenance: stringValue(event.cwd_provenance) ?? "unknown" } : {}),
800
+ provider: stringValue(profile.provider), model: stringValue(profile.model), reasoning: stringValue(droid ? profile.reasoning : profile.variant),
801
+ mode: stringValue(droid ? profile.mode : profile.agent), agent_type: stringValue(droid ? profile.agent_type : profile.agent)
802
+ };
803
+ const inserted = recordHookEvent(context, {
804
+ projectId: project.id, protocolId: null, harness, providerSessionId, parentSessionId,
805
+ daemonId, sessionId, agentId: agentId ?? null, turnId: stringValue(event.message_id) ?? null, eventName: "PreToolUse", toolName,
806
+ status: "observed", payload, eventKey: baseKey, matchKey, transcriptPath: droid ? stringValue(event.transcript_path) ?? null : null, cwd: observedCwd
807
+ });
808
+ recordFlowSessionObservation(context, { projectId: project.id, sessionId, cwd: observedCwd, toolName, eventKey: baseKey });
809
+ return inserted;
721
810
  };
722
- const inserted = recordHookEvent(context, {
723
- projectId: project.id, protocolId: null, harness, providerSessionId, parentSessionId,
724
- daemonId, sessionId, agentId: agentId ?? null, turnId: stringValue(event.message_id) ?? null, eventName: "PreToolUse", toolName,
725
- status: "observed", payload, eventKey: baseKey, matchKey, transcriptPath: droid ? stringValue(event.transcript_path) ?? null : null, cwd: expectedRoot
726
- });
727
- recordFlowSessionObservation(context, { projectId: project.id, sessionId, cwd: expectedRoot, toolName, eventKey: baseKey });
728
- const result = { ok: true, observed: inserted, duplicate: !inserted, event_key: baseKey, phase, harness, session: nativeSessionIdentity(harness, providerSessionId), ...(nativeParentId ? { parent_session: nativeSessionIdentity(harness, nativeParentId) } : {}), daemon_id: daemonId };
729
- return agy ? result : { ...result, hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...rawInput, command: commandWithHookEvent(command, baseKey) } } };
811
+ const result = (inserted) => ({ ok: true, observed: inserted, duplicate: !inserted, event_key: baseKey, phase, harness, session: nativeSessionIdentity(harness, providerSessionId), ...(nativeParentId ? { parent_session: nativeSessionIdentity(harness, nativeParentId) } : {}), daemon_id: daemonId });
812
+ if (agy && lifecycle) {
813
+ let inserted = false;
814
+ const receipt = observeLifecycleInvocation(context, {
815
+ command, harness, projectId: project.id, identity: { daemonId, rootSessionId: nativeParentId ?? providerSessionId, sessionId: providerSessionId, parentSessionId: nativeParentId ?? null, toolCallId: toolCallId },
816
+ recordReceipt: scope => { inserted = persist(scope); return baseKey; }
817
+ });
818
+ return { ...result(inserted), ...(receipt.invocationId ? { invocation_id: receipt.invocationId } : {}) };
819
+ }
820
+ const inserted = context.db.writeTransaction(() => persist());
821
+ return agy ? result(inserted) : { ...result(inserted), hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...rawInput, command: commandWithHookEvent(command, baseKey) } } };
730
822
  }
731
823
  /** Convert an Antigravity CLI tool hook into the shared lifecycle receipt. */
732
824
  export function handleAgyEvent(context, input) {
733
825
  const event = parseJsonObject(input.stdin || "{}", "Antigravity tool event");
734
826
  if (event.schema_id !== "dd-flow/agy-tool-event@1")
735
827
  throw new AppError("unsupported_agy_event_schema", "Unsupported Antigravity event schema", 1);
736
- const workspacePaths = Array.isArray(event.workspace_paths) ? event.workspace_paths : [];
737
- const directory = workspacePaths.find((value) => typeof value === "string" && path.isAbsolute(value));
828
+ // workspacePaths lists available workspaces; its first element is not a
829
+ // native selected-cwd observation. The daemon's spawn cwd is a separate,
830
+ // launch-bound route, and the list only corroborates its membership.
831
+ const workspacePaths = Array.isArray(event.workspace_paths)
832
+ ? event.workspace_paths.filter((value) => typeof value === "string" && path.isAbsolute(value)) : [];
833
+ const launchCwd = stringValue(event.launch_cwd);
834
+ const qualifiedLaunchCwd = launchCwd && path.isAbsolute(launchCwd) ? launchCwd : undefined;
835
+ const directory = qualifiedLaunchCwd ?? (workspacePaths.length === 1 ? workspacePaths[0] : undefined);
836
+ if (!directory && workspacePaths.length > 1)
837
+ throw new AppError("agy_workspace_route_unproven", "Antigravity did not identify the active route among multiple workspaces", 1, { workspace_paths: workspacePaths });
838
+ if (directory && qualifiedLaunchCwd && workspacePaths.length
839
+ && !workspacePaths.some(value => resolveProjectRoot(value) === resolveProjectRoot(directory))) {
840
+ throw new AppError("agy_workspace_route_unproven", "Antigravity launch cwd is absent from the native workspace list", 1, { launch_cwd: directory, workspace_paths: workspacePaths });
841
+ }
738
842
  const providerSessionId = stringValue(event.conversation_id);
739
843
  const daemonId = stringValue(event.daemon_id);
740
844
  const eventId = stringValue(event.event_id);
@@ -743,6 +847,9 @@ export function handleAgyEvent(context, input) {
743
847
  throw new AppError("agy_session_identity_invalid", "Antigravity event is missing trusted physical identity", 1);
744
848
  const rawInput = objectRecord(event.input);
745
849
  const command = stringValue(rawInput.command) ?? stringValue(rawInput.cmd) ?? stringValue(rawInput.CommandLine);
850
+ if (command && (lifecycleFacts(command) || continuationCommand(command)) && !qualifiedLaunchCwd) {
851
+ throw new AppError("agy_workspace_route_unproven", "Managed Antigravity call has no launch-bound execution cwd", 1, { phase: "prepare", effect: "no_effect", recoverable: false, workspace_paths: workspacePaths });
852
+ }
746
853
  const invalidStep = !Number.isSafeInteger(event.step_index) || Number(event.step_index) < 0;
747
854
  const invalidExecution = event.execution_num !== undefined && (!Number.isSafeInteger(event.execution_num) || Number(event.execution_num) < 0);
748
855
  if (event.phase === "before" && command && (lifecycleFacts(command) || continuationCommand(command)) && (invalidStep || invalidExecution)) {
@@ -753,6 +860,7 @@ export function handleAgyEvent(context, input) {
753
860
  daemon_id: daemonId, tool_call_id: eventId, tool: toolName, input: event.input,
754
861
  message_id: event.step_index === undefined ? null : String(event.step_index),
755
862
  session: { provider_session_id: providerSessionId, parent_provider_session_id: event.parent_conversation_id, directory },
863
+ cwd_provenance: qualifiedLaunchCwd ? "launch_bound" : "workspace_paths_singleton",
756
864
  profile: { provider: objectRecord(event.profile).provider, model: objectRecord(event.profile).model, variant: objectRecord(event.profile).reasoning, agent: objectRecord(event.profile).mode },
757
865
  outcome: { status: stringValue(event.error) ? "error" : "completed" }
758
866
  };