@deksden-com/dd-flow-cli 0.9.0-beta.14 → 0.9.0-beta.141

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 (493) hide show
  1. package/CHANGELOG.md +740 -0
  2. package/README.md +143 -1
  3. package/dist/build-info.json +8 -8
  4. package/dist/cli/argument-values.js +38 -0
  5. package/dist/cli/command-inputs.js +376 -0
  6. package/dist/cli/command-metadata.js +197 -0
  7. package/dist/cli/command-option-descriptions.js +191 -0
  8. package/dist/cli/completion-cli.js +70 -0
  9. package/dist/cli/completion.js +57 -0
  10. package/dist/cli/error-output.js +52 -0
  11. package/dist/cli/executor-onboarding.js +76 -0
  12. package/dist/cli/flows.js +29 -0
  13. package/dist/cli/harnesses.js +84 -0
  14. package/dist/cli/help.js +328 -18
  15. package/dist/cli/hook-ingress.js +78 -0
  16. package/dist/cli/input-preparation.js +165 -0
  17. package/dist/cli/memory-flow-admission.js +44 -0
  18. package/dist/cli/memory-init-invocation.js +78 -0
  19. package/dist/cli/memory-init.js +240 -0
  20. package/dist/cli/memory-storage-admission.js +53 -0
  21. package/dist/cli/models.js +39 -0
  22. package/dist/cli/native-hook-ingress.js +80 -0
  23. package/dist/cli/operator-profiles.js +61 -0
  24. package/dist/cli/output-options.js +35 -0
  25. package/dist/cli/response-file.js +71 -0
  26. package/dist/cli/run-cli.js +1689 -545
  27. package/dist/cli/runtime-maintenance-diagnostics.js +46 -0
  28. package/dist/cli/runtime-maintenance-ingress.js +52 -0
  29. package/dist/cli/runtime-process.js +153 -0
  30. package/dist/cli/setup.js +149 -0
  31. package/dist/cli.js +71 -2
  32. package/dist/domain/stage-catalog.js +1 -1
  33. package/dist/domain/stage-handler-identities.js +20 -0
  34. package/dist/harness-runtime/DD-ZCODE.md +112 -0
  35. package/dist/harness-runtime/OPENCODE-LICENSE.txt +24 -0
  36. package/dist/harness-runtime/bin/dd-agy.mjs +56 -0
  37. package/dist/harness-runtime/bin/dd-codex.mjs +27 -0
  38. package/dist/harness-runtime/bin/dd-droid.mjs +34 -0
  39. package/dist/harness-runtime/bin/dd-grok.mjs +40 -0
  40. package/dist/harness-runtime/bin/dd-opencode.mjs +22 -0
  41. package/dist/harness-runtime/bin/dd-zcode.mjs +87 -0
  42. package/dist/harness-runtime/lib/adapter-timeouts.d.mts +5 -0
  43. package/dist/harness-runtime/lib/adapter-timeouts.mjs +34 -0
  44. package/dist/harness-runtime/lib/agy-hooks.d.mts +1 -0
  45. package/dist/harness-runtime/lib/agy-hooks.mjs +9 -0
  46. package/dist/harness-runtime/lib/daemon-observation.d.mts +2 -0
  47. package/dist/harness-runtime/lib/daemon-observation.mjs +163 -0
  48. package/dist/harness-runtime/lib/daemon-operations.d.mts +1 -0
  49. package/dist/harness-runtime/lib/daemon-operations.mjs +371 -0
  50. package/dist/harness-runtime/lib/dd-agy-daemon.d.mts +6 -0
  51. package/dist/harness-runtime/lib/dd-agy-daemon.mjs +728 -0
  52. package/dist/harness-runtime/lib/dd-agy.mjs +70 -0
  53. package/dist/harness-runtime/lib/dd-codex-daemon.d.mts +1 -0
  54. package/dist/harness-runtime/lib/dd-codex-daemon.mjs +196 -0
  55. package/dist/harness-runtime/lib/dd-codex.d.mts +1 -0
  56. package/dist/harness-runtime/lib/dd-codex.mjs +817 -0
  57. package/dist/harness-runtime/lib/dd-droid-daemon.mjs +140 -0
  58. package/dist/harness-runtime/lib/dd-droid.mjs +584 -0
  59. package/dist/harness-runtime/lib/dd-grok-daemon.mjs +498 -0
  60. package/dist/harness-runtime/lib/dd-grok.mjs +177 -0
  61. package/dist/harness-runtime/lib/dd-opencode-daemon.mjs +557 -0
  62. package/dist/harness-runtime/lib/dd-opencode.mjs +363 -0
  63. package/dist/harness-runtime/lib/dd-zcode-daemon.mjs +751 -0
  64. package/dist/harness-runtime/lib/dd-zcode.mjs +1145 -0
  65. package/dist/harness-runtime/lib/delegation-instructions.d.mts +14 -0
  66. package/dist/harness-runtime/lib/delegation-instructions.mjs +157 -0
  67. package/dist/harness-runtime/lib/dispatch-fence.mjs +14 -0
  68. package/dist/harness-runtime/lib/driver-recovery.mjs +139 -0
  69. package/dist/harness-runtime/lib/droid-observation.mjs +66 -0
  70. package/dist/harness-runtime/lib/lease-renewal.d.mts +8 -0
  71. package/dist/harness-runtime/lib/lease-renewal.mjs +37 -0
  72. package/dist/harness-runtime/lib/lifecycle-command.d.mts +20 -0
  73. package/dist/harness-runtime/lib/lifecycle-command.mjs +273 -0
  74. package/dist/harness-runtime/lib/lifecycle-contract.d.mts +10 -0
  75. package/dist/harness-runtime/lib/lifecycle-contract.mjs +19 -0
  76. package/dist/harness-runtime/lib/managed-daemon.d.mts +16 -0
  77. package/dist/harness-runtime/lib/managed-daemon.mjs +683 -0
  78. package/dist/harness-runtime/lib/managed-flow-env.mjs +15 -0
  79. package/dist/harness-runtime/lib/model-observations.mjs +101 -0
  80. package/dist/harness-runtime/lib/native-children.d.mts +16 -0
  81. package/dist/harness-runtime/lib/native-children.mjs +113 -0
  82. package/dist/harness-runtime/lib/native-hook-command.d.mts +7 -0
  83. package/dist/harness-runtime/lib/native-hook-command.mjs +251 -0
  84. package/dist/harness-runtime/lib/native-request-proof.d.mts +2 -0
  85. package/dist/harness-runtime/lib/native-request-proof.mjs +65 -0
  86. package/dist/harness-runtime/lib/native-streams.mjs +45 -0
  87. package/dist/harness-runtime/lib/observation-clock.d.mts +18 -0
  88. package/dist/harness-runtime/lib/observation-clock.mjs +92 -0
  89. package/dist/harness-runtime/lib/opencode-plugin.mjs +85 -0
  90. package/dist/harness-runtime/lib/operation-context.mjs +5 -0
  91. package/dist/harness-runtime/lib/operation-errors.d.mts +16 -0
  92. package/dist/harness-runtime/lib/operation-errors.mjs +127 -0
  93. package/dist/harness-runtime/lib/operation-progress.d.mts +7 -0
  94. package/dist/harness-runtime/lib/operation-progress.mjs +114 -0
  95. package/dist/harness-runtime/lib/process-json.d.mts +5 -0
  96. package/dist/harness-runtime/lib/process-json.mjs +201 -0
  97. package/dist/harness-runtime/lib/process-snapshot.mjs +10 -0
  98. package/dist/harness-runtime/lib/provider-overload-policy.d.mts +13 -0
  99. package/dist/harness-runtime/lib/provider-overload-policy.mjs +138 -0
  100. package/dist/harness-runtime/lib/runner-events.mjs +200 -0
  101. package/dist/harness-runtime/lib/runner-lock.mjs +42 -0
  102. package/dist/harness-runtime/lib/session-settlement.mjs +30 -0
  103. package/dist/harness-runtime/lib/setup-progress.d.mts +9 -0
  104. package/dist/harness-runtime/lib/setup-progress.mjs +51 -0
  105. package/dist/harness-runtime/lib/tool-observations.d.mts +21 -0
  106. package/dist/harness-runtime/lib/tool-observations.mjs +228 -0
  107. package/dist/harness-runtime/lib/zcode-usage.mjs +55 -0
  108. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-1y4a697q.js +81 -0
  109. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-2ny4vxct.js +92 -0
  110. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-6mptkzbc.js +0 -0
  111. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-7x67cf1q.js +52 -0
  112. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-9rqn6x4v.js +16 -0
  113. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-9vdz0zwn.js +1661 -0
  114. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-ecs5ftka.js +1466 -0
  115. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-frbwqjmf.js +145 -0
  116. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-gne7h7mq.js +50 -0
  117. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-gx9wnkzr.js +105 -0
  118. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-m2eywc8w.js +0 -0
  119. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-nwcpakzx.js +12 -0
  120. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-q4g3afnw.js +623 -0
  121. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-qav6mfdz.js +0 -0
  122. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-sxt88q69.js +12 -0
  123. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-t1xa31vm.js +39 -0
  124. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-ve4aws36.js +103 -0
  125. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-vmytr4q8.js +4 -0
  126. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-vt9jbch8.js +36 -0
  127. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-vyas3vgt.js +0 -0
  128. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-wjyeq25y.js +30 -0
  129. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-xgnj2w4v.js +20 -0
  130. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-y2etg5bs.js +9 -0
  131. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-ydhz1zrh.js +238 -0
  132. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-z6qdwcsf.js +62 -0
  133. package/dist/harness-runtime/vendor/opencode-client/contract.d.ts +1 -0
  134. package/dist/harness-runtime/vendor/opencode-client/contract.js +12 -0
  135. package/dist/harness-runtime/vendor/opencode-client/effect/api/api.d.ts +2806 -0
  136. package/dist/harness-runtime/vendor/opencode-client/effect/api/api.js +0 -0
  137. package/dist/harness-runtime/vendor/opencode-client/effect/api.d.ts +4 -0
  138. package/dist/harness-runtime/vendor/opencode-client/effect/api.js +0 -0
  139. package/dist/harness-runtime/vendor/opencode-client/effect/client.d.ts +2530 -0
  140. package/dist/harness-runtime/vendor/opencode-client/effect/client.js +15 -0
  141. package/dist/harness-runtime/vendor/opencode-client/effect/generated/client-error.d.ts +7 -0
  142. package/dist/harness-runtime/vendor/opencode-client/effect/generated/client-error.js +6 -0
  143. package/dist/harness-runtime/vendor/opencode-client/effect/generated/client.d.ts +2529 -0
  144. package/dist/harness-runtime/vendor/opencode-client/effect/generated/client.js +8 -0
  145. package/dist/harness-runtime/vendor/opencode-client/effect/generated/index.d.ts +2 -0
  146. package/dist/harness-runtime/vendor/opencode-client/effect/generated/index.js +12 -0
  147. package/dist/harness-runtime/vendor/opencode-client/effect/index.d.ts +35 -0
  148. package/dist/harness-runtime/vendor/opencode-client/effect/index.js +75 -0
  149. package/dist/harness-runtime/vendor/opencode-client/effect/rpc.d.ts +20 -0
  150. package/dist/harness-runtime/vendor/opencode-client/effect/rpc.js +11 -0
  151. package/dist/harness-runtime/vendor/opencode-client/effect/service.d.ts +90 -0
  152. package/dist/harness-runtime/vendor/opencode-client/effect/service.js +187 -0
  153. package/dist/harness-runtime/vendor/opencode-client/package.json +84 -0
  154. package/dist/harness-runtime/vendor/opencode-client/promise/api.d.ts +21 -0
  155. package/dist/harness-runtime/vendor/opencode-client/promise/api.js +0 -0
  156. package/dist/harness-runtime/vendor/opencode-client/promise/client.d.ts +252 -0
  157. package/dist/harness-runtime/vendor/opencode-client/promise/client.js +14 -0
  158. package/dist/harness-runtime/vendor/opencode-client/promise/generated/client-error.d.ts +8 -0
  159. package/dist/harness-runtime/vendor/opencode-client/promise/generated/client-error.js +6 -0
  160. package/dist/harness-runtime/vendor/opencode-client/promise/generated/client.d.ts +259 -0
  161. package/dist/harness-runtime/vendor/opencode-client/promise/generated/client.js +7 -0
  162. package/dist/harness-runtime/vendor/opencode-client/promise/generated/index.d.ts +3 -0
  163. package/dist/harness-runtime/vendor/opencode-client/promise/generated/index.js +79 -0
  164. package/dist/harness-runtime/vendor/opencode-client/promise/generated/types.d.ts +8932 -0
  165. package/dist/harness-runtime/vendor/opencode-client/promise/generated/types.js +70 -0
  166. package/dist/harness-runtime/vendor/opencode-client/promise/index.d.ts +6 -0
  167. package/dist/harness-runtime/vendor/opencode-client/promise/index.js +83 -0
  168. package/dist/harness-runtime/vendor/opencode-client/promise/rpc.d.ts +32 -0
  169. package/dist/harness-runtime/vendor/opencode-client/promise/rpc.js +7 -0
  170. package/dist/harness-runtime/vendor/opencode-client/promise/service.d.ts +24 -0
  171. package/dist/harness-runtime/vendor/opencode-client/promise/service.js +187 -0
  172. package/dist/harness-runtime/vendor/opencode-client/pty-handoff.d.ts +12 -0
  173. package/dist/harness-runtime/vendor/opencode-client/pty-handoff.js +14 -0
  174. package/dist/harness-runtime/vendor/opencode-client/rpc-runtime.d.ts +20 -0
  175. package/dist/harness-runtime/vendor/opencode-client/rpc-runtime.js +14 -0
  176. package/dist/harness-runtime/vendor/opencode-client/service-contender.d.ts +37 -0
  177. package/dist/harness-runtime/vendor/opencode-client/service-contender.js +10 -0
  178. package/dist/harness-runtime/vendor/opencode-client/service-probe.d.ts +70 -0
  179. package/dist/harness-runtime/vendor/opencode-client/service-probe.js +16 -0
  180. package/dist/harness-runtime/vendor/opencode-client/service-timing.d.ts +12 -0
  181. package/dist/harness-runtime/vendor/opencode-client/service-timing.js +10 -0
  182. package/dist/harness-runtime/vendor/opencode-client/service-version.d.ts +2 -0
  183. package/dist/harness-runtime/vendor/opencode-client/service-version.js +6 -0
  184. package/dist/harness-runtime/vendor/opencode-client/service.d.ts +52 -0
  185. package/dist/harness-runtime/vendor/opencode-client/service.js +1 -0
  186. package/dist/harness-runtime/vendor/opencode-client/shared-events.d.ts +11 -0
  187. package/dist/harness-runtime/vendor/opencode-client/shared-events.js +8 -0
  188. package/dist/harness-runtime/vendor/opencode-client/solid/connection.d.ts +37 -0
  189. package/dist/harness-runtime/vendor/opencode-client/solid/connection.js +10 -0
  190. package/dist/harness-runtime/vendor/opencode-client/solid/data.d.ts +211 -0
  191. package/dist/harness-runtime/vendor/opencode-client/solid/data.js +18 -0
  192. package/dist/harness-runtime/vendor/opencode-client/solid/index.d.ts +3 -0
  193. package/dist/harness-runtime/vendor/opencode-client/solid/index.js +32 -0
  194. package/dist/harness-runtime/vendor/opencode-client/solid/pty.d.ts +22 -0
  195. package/dist/harness-runtime/vendor/opencode-client/solid/pty.js +8 -0
  196. package/dist/harness-runtime/vendor/opencode-plugin.mjs +3 -0
  197. package/dist/protocol/local-files.js +18 -9
  198. package/dist/runtime/context.js +8 -4
  199. package/dist/schemas/agent-profile.schema.json +1 -1
  200. package/dist/schemas/code-review-result.schema.json +1 -1
  201. package/dist/schemas/code-verification.schema.json +7 -5
  202. package/dist/schemas/code-work-batch.schema.json +2 -2
  203. package/dist/schemas/discovery-result.schema.json +184 -0
  204. package/dist/schemas/execution-bundle.schema.json +205 -0
  205. package/dist/schemas/execution-settings.schema.json +162 -0
  206. package/dist/schemas/flow-descriptor.schema.json +54 -0
  207. package/dist/schemas/flow-run.schema.json +28 -0
  208. package/dist/schemas/harness-catalog.schema.json +80 -0
  209. package/dist/schemas/mb-init-compose-result.schema.json +457 -0
  210. package/dist/schemas/mb-init-discover-result.schema.json +500 -0
  211. package/dist/schemas/mb-init-finish-result.schema.json +422 -0
  212. package/dist/schemas/mb-init-preflight-result.schema.json +367 -0
  213. package/dist/schemas/mb-init-review-result.schema.json +396 -0
  214. package/dist/schemas/mb-init-verify-result.schema.json +391 -0
  215. package/dist/schemas/memory-bootstrap-receipt-v2.schema.json +67 -0
  216. package/dist/schemas/memory-bootstrap-receipt.schema.json +354 -0
  217. package/dist/schemas/memory-composition-work-result.schema.json +38 -0
  218. package/dist/schemas/memory-repair-work-result.schema.json +49 -0
  219. package/dist/schemas/memory-review-correction-plan.schema.json +30 -0
  220. package/dist/schemas/memory-review-work-result.schema.json +43 -0
  221. package/dist/schemas/model-catalog.schema.json +153 -0
  222. package/dist/schemas/plan-review-decision.schema.json +3 -3
  223. package/dist/schemas/plan-review-result.schema.json +2 -2
  224. package/dist/schemas/profile-settings.schema.json +122 -0
  225. package/dist/schemas/release-manifest.schema.json +291 -0
  226. package/dist/schemas/run-control-receipt.schema.json +118 -0
  227. package/dist/schemas/run-control-request.schema.json +36 -0
  228. package/dist/schemas/run-engine-binding.schema.json +1 -1
  229. package/dist/schemas/vnext-protocol-plan.schema.json +2 -2
  230. package/dist/services/branch-context.js +3 -3
  231. package/dist/services/canon.js +11 -8
  232. package/dist/services/check-runner-evidence.js +250 -0
  233. package/dist/services/cleanup.js +133 -40
  234. package/dist/services/cli-operation-classifier.js +25 -62
  235. package/dist/services/cli-upgrade-allowlist.js +27 -0
  236. package/dist/services/code-checks.js +1168 -107
  237. package/dist/services/codex-hook-delivery.js +32 -0
  238. package/dist/services/command-context.js +80 -0
  239. package/dist/services/compatibility-preflight.js +13 -3
  240. package/dist/services/config.js +11 -8
  241. package/dist/services/continuation-outcome.js +20 -0
  242. package/dist/services/controller-child-capacity.js +13 -0
  243. package/dist/services/controller-child-observer.js +115 -0
  244. package/dist/services/controller-execution.js +88 -0
  245. package/dist/services/controller-fanout.js +202 -0
  246. package/dist/services/dashboard.js +143 -58
  247. package/dist/services/descriptor-coordinator-session.js +35 -0
  248. package/dist/services/descriptor-root-work.js +53 -0
  249. package/dist/services/descriptor-run-settlement.js +105 -0
  250. package/dist/services/descriptor-run-start.js +449 -0
  251. package/dist/services/descriptor-stage-coordinator.js +197 -0
  252. package/dist/services/descriptor-stage-settlement.js +222 -0
  253. package/dist/services/descriptor-stage-start.js +139 -0
  254. package/dist/services/discovery/agy-discovery.js +2 -0
  255. package/dist/services/discovery/codex-discovery.js +2 -0
  256. package/dist/services/discovery/droid-discovery.js +2 -0
  257. package/dist/services/discovery/grok-discovery.js +2 -0
  258. package/dist/services/discovery/opencode-discovery.js +2 -0
  259. package/dist/services/discovery/static.js +31 -0
  260. package/dist/services/discovery/zcode-discovery.js +2 -0
  261. package/dist/services/engines.js +294 -153
  262. package/dist/services/eval-snapshots.js +1215 -116
  263. package/dist/services/execution-bundles.js +263 -0
  264. package/dist/services/execution-policy.js +185 -0
  265. package/dist/services/executor-onboarding.js +56 -0
  266. package/dist/services/external-work-launch.js +308 -0
  267. package/dist/services/flow-catalog.js +21 -0
  268. package/dist/services/flow-descriptors.js +82 -0
  269. package/dist/services/frozen-engine.js +250 -0
  270. package/dist/services/generic-execution.js +164 -0
  271. package/dist/services/harness-adapter.js +355 -36
  272. package/dist/services/harness-config.js +2 -0
  273. package/dist/services/harness-discovery.js +29 -0
  274. package/dist/services/harness-doctor.js +75 -0
  275. package/dist/services/harness-probe-effects.js +51 -0
  276. package/dist/services/harness-version-probe.js +100 -0
  277. package/dist/services/hooks.js +771 -325
  278. package/dist/services/hybrid-contracts.js +483 -0
  279. package/dist/services/ids.js +29 -1
  280. package/dist/services/installation-lock.js +95 -0
  281. package/dist/services/installed-flow-source.js +183 -0
  282. package/dist/services/lanes.js +78 -65
  283. package/dist/services/lifecycle-command.js +1 -288
  284. package/dist/services/lifecycle-contract.js +1 -0
  285. package/dist/services/lifecycle-invocations.js +2007 -0
  286. package/dist/services/managed-daemon-binding.js +38 -0
  287. package/dist/services/managed-lease-monitor.js +133 -0
  288. package/dist/services/managed-processes.js +475 -55
  289. package/dist/services/memory-accepted-composition.js +77 -0
  290. package/dist/services/memory-baseline-preservation.js +65 -0
  291. package/dist/services/memory-completed-correction-wave.js +320 -0
  292. package/dist/services/memory-compose-handler.js +258 -0
  293. package/dist/services/memory-composition-acceptance.js +199 -0
  294. package/dist/services/memory-composition-graph.js +134 -0
  295. package/dist/services/memory-composition-registration.js +146 -0
  296. package/dist/services/memory-composition-work-custody.js +151 -0
  297. package/dist/services/memory-composition-work-results.js +96 -0
  298. package/dist/services/memory-corrected-review-acceptance.js +129 -0
  299. package/dist/services/memory-corrected-review-batch.js +30 -0
  300. package/dist/services/memory-corrected-review-findings.js +106 -0
  301. package/dist/services/memory-corrected-review-inputs.js +210 -0
  302. package/dist/services/memory-corrected-review-projection.js +78 -0
  303. package/dist/services/memory-corrected-review-receipt.js +170 -0
  304. package/dist/services/memory-corrected-review-registration.js +191 -0
  305. package/dist/services/memory-correction-check-receipt.js +219 -0
  306. package/dist/services/memory-correction-input-owner.js +6 -0
  307. package/dist/services/memory-discover-handler.js +104 -0
  308. package/dist/services/memory-finish-evidence.js +435 -0
  309. package/dist/services/memory-finish-handler.js +215 -0
  310. package/dist/services/memory-finish-integration.js +280 -0
  311. package/dist/services/memory-finish-readiness.js +122 -0
  312. package/dist/services/memory-git-integration.js +151 -0
  313. package/dist/services/memory-historical-corrected-review.js +206 -0
  314. package/dist/services/memory-historical-original-review.js +130 -0
  315. package/dist/services/memory-historical-repair-registration.js +99 -0
  316. package/dist/services/memory-historical-second-review.js +128 -0
  317. package/dist/services/memory-init-bootstrap-readiness.js +210 -0
  318. package/dist/services/memory-init-contexts.js +89 -0
  319. package/dist/services/memory-init-controller-start.js +84 -0
  320. package/dist/services/memory-init-engine-release.js +223 -0
  321. package/dist/services/memory-init-git-policy.js +136 -0
  322. package/dist/services/memory-init-lint-capability.js +185 -0
  323. package/dist/services/memory-init-native-capacity.js +296 -0
  324. package/dist/services/memory-init-preflight.js +330 -0
  325. package/dist/services/memory-init-project-bootstrap.js +599 -0
  326. package/dist/services/memory-init-request-candidate.js +157 -0
  327. package/dist/services/memory-init-request-input.js +66 -0
  328. package/dist/services/memory-init-resource-storage.js +125 -0
  329. package/dist/services/memory-init-run-allocation.js +81 -0
  330. package/dist/services/memory-init-run-bundle.js +98 -0
  331. package/dist/services/memory-init-start.js +181 -0
  332. package/dist/services/memory-init-startup-custody.js +80 -0
  333. package/dist/services/memory-init-startup-input.js +240 -0
  334. package/dist/services/memory-init-storage.js +81 -0
  335. package/dist/services/memory-init-stores.js +359 -0
  336. package/dist/services/memory-init-worktrunk-capability.js +178 -0
  337. package/dist/services/memory-layer-publication.js +611 -0
  338. package/dist/services/memory-layer.js +156 -0
  339. package/dist/services/memory-lint-config.js +85 -0
  340. package/dist/services/memory-native-review-copy.js +165 -0
  341. package/dist/services/memory-normal-flow-entry.js +75 -0
  342. package/dist/services/memory-ordinary-defaults.js +112 -0
  343. package/dist/services/memory-preflight-bootstrap-custody.js +115 -0
  344. package/dist/services/memory-preflight-handler.js +259 -0
  345. package/dist/services/memory-repair-current-view.js +299 -0
  346. package/dist/services/memory-repair-document-delta.js +451 -0
  347. package/dist/services/memory-repair-history.js +491 -0
  348. package/dist/services/memory-repair-physical-binding.js +312 -0
  349. package/dist/services/memory-repair-publication-files.js +152 -0
  350. package/dist/services/memory-repair-publication-intent.js +27 -0
  351. package/dist/services/memory-repair-publication-plan.js +160 -0
  352. package/dist/services/memory-repair-publication-source.js +225 -0
  353. package/dist/services/memory-repair-publication.js +794 -0
  354. package/dist/services/memory-repair-registration.js +239 -0
  355. package/dist/services/memory-repair-work-baseline.js +413 -0
  356. package/dist/services/memory-repair-work-completion.js +97 -0
  357. package/dist/services/memory-repair-work-custody.js +73 -0
  358. package/dist/services/memory-repair-work-results.js +89 -0
  359. package/dist/services/memory-repair-workspace.js +252 -0
  360. package/dist/services/memory-repair-writer-copy.js +735 -0
  361. package/dist/services/memory-result-binding.js +173 -0
  362. package/dist/services/memory-retained-review-authority.js +129 -0
  363. package/dist/services/memory-retained-review-results.js +70 -0
  364. package/dist/services/memory-retained-second-baseline.js +102 -0
  365. package/dist/services/memory-retained-second-repairs.js +156 -0
  366. package/dist/services/memory-review-acceptance.js +214 -0
  367. package/dist/services/memory-review-correction-plan.js +171 -0
  368. package/dist/services/memory-review-handler.js +585 -0
  369. package/dist/services/memory-review-inputs.js +200 -0
  370. package/dist/services/memory-review-receipt.js +180 -0
  371. package/dist/services/memory-review-registration.js +108 -0
  372. package/dist/services/memory-review-work-custody.js +124 -0
  373. package/dist/services/memory-review-work-input.js +72 -0
  374. package/dist/services/memory-review-work-results.js +73 -0
  375. package/dist/services/memory-second-corrected-review-inputs.js +165 -0
  376. package/dist/services/memory-second-correction-plan.js +76 -0
  377. package/dist/services/memory-second-repair-baseline.js +91 -0
  378. package/dist/services/memory-second-repair-launch.js +614 -0
  379. package/dist/services/memory-second-repair-registration.js +88 -0
  380. package/dist/services/memory-second-repair-source.js +187 -0
  381. package/dist/services/memory-second-review-acceptance.js +115 -0
  382. package/dist/services/memory-source-observations.js +117 -0
  383. package/dist/services/memory-stage-dispatch.js +28 -0
  384. package/dist/services/memory-stage-results.js +199 -0
  385. package/dist/services/memory-verification-acceptance.js +135 -0
  386. package/dist/services/memory-verification-checks.js +252 -0
  387. package/dist/services/memory-verification-receipt.js +159 -0
  388. package/dist/services/memory-verify-handler.js +135 -0
  389. package/dist/services/memory-work-result-preparation.js +63 -0
  390. package/dist/services/merge-queue.js +228 -149
  391. package/dist/services/merge-server.js +161 -35
  392. package/dist/services/migrations.js +37 -16
  393. package/dist/services/model-catalog.js +109 -0
  394. package/dist/services/model-discovery.js +46 -0
  395. package/dist/services/native-daemon-history.js +49 -0
  396. package/dist/services/native-session-control.js +39 -0
  397. package/dist/services/operator-profiles.js +236 -0
  398. package/dist/services/plan-runtime.js +26 -11
  399. package/dist/services/plans.js +28 -25
  400. package/dist/services/projects.js +37 -17
  401. package/dist/services/prompts.js +28 -17
  402. package/dist/services/protocols.js +163 -87
  403. package/dist/services/provider-capacity.js +385 -0
  404. package/dist/services/recovery-observation-budget.js +121 -0
  405. package/dist/services/recovery-snapshot-database.js +560 -0
  406. package/dist/services/release-archive-producer.js +52 -0
  407. package/dist/services/release-archive.js +143 -0
  408. package/dist/services/release-cache.js +142 -0
  409. package/dist/services/release-compatibility.js +18 -0
  410. package/dist/services/release-download.js +67 -0
  411. package/dist/services/release-envelope.js +104 -0
  412. package/dist/services/release-manifest.js +45 -0
  413. package/dist/services/release-payload.js +114 -0
  414. package/dist/services/release-producer.js +63 -0
  415. package/dist/services/release-provenance.js +160 -0
  416. package/dist/services/release-registry.js +66 -0
  417. package/dist/services/release-source.js +194 -0
  418. package/dist/services/repair-intents.js +42 -0
  419. package/dist/services/review-copy.js +165 -0
  420. package/dist/services/review-deferral.js +23 -0
  421. package/dist/services/run-control-receipt.js +56 -0
  422. package/dist/services/run-control-worker.js +411 -0
  423. package/dist/services/run-control.js +1056 -0
  424. package/dist/services/run-controller-adapter.js +399 -0
  425. package/dist/services/run-controller-capture.js +186 -0
  426. package/dist/services/run-controller-process.js +208 -0
  427. package/dist/services/run-controller-recovery.js +289 -0
  428. package/dist/services/run-controller-state.js +97 -0
  429. package/dist/services/run-controller.js +1205 -0
  430. package/dist/services/run-engine-bindings.js +1 -1
  431. package/dist/services/run-fork.js +147 -0
  432. package/dist/services/run-health.js +149 -0
  433. package/dist/services/run-observations.js +152 -0
  434. package/dist/services/run-projection.js +22 -64
  435. package/dist/services/run-recovery-guard.js +77 -0
  436. package/dist/services/run-recovery-runtime.js +112 -0
  437. package/dist/services/run-recovery.js +489 -0
  438. package/dist/services/run-start-custody.js +177 -0
  439. package/dist/services/runs.js +1019 -247
  440. package/dist/services/runtime-budget.js +430 -0
  441. package/dist/services/runtime-command.js +43 -0
  442. package/dist/services/runtime-scope-capture.js +52 -0
  443. package/dist/services/runtime-scope-control.js +468 -0
  444. package/dist/services/runtime-scope-resume.js +599 -0
  445. package/dist/services/runtime-scope-stop.js +210 -0
  446. package/dist/services/runtime-scope-worker.js +287 -0
  447. package/dist/services/runtime-service.js +96 -0
  448. package/dist/services/schema-validation.js +10 -8
  449. package/dist/services/scope-writer-cleanup.js +27 -0
  450. package/dist/services/sessions.js +35 -69
  451. package/dist/services/setup.js +377 -0
  452. package/dist/services/stage-blocker.js +17 -7
  453. package/dist/services/stage-context.js +193 -19
  454. package/dist/services/stage-handlers.js +74 -0
  455. package/dist/services/stage-lifecycle.js +152 -116
  456. package/dist/services/stage-pause.js +89 -75
  457. package/dist/services/stage-report-renderer.js +234 -6
  458. package/dist/services/stage-work-graph.js +52 -0
  459. package/dist/services/tooling.js +41 -30
  460. package/dist/services/usage.js +49 -101
  461. package/dist/services/verification-matrix.js +375 -0
  462. package/dist/services/vnext-code-review.js +223 -88
  463. package/dist/services/vnext-code.js +327 -226
  464. package/dist/services/vnext-contracts.js +10 -1
  465. package/dist/services/vnext-execution-profile.js +80 -5
  466. package/dist/services/vnext-fanout.js +311 -19
  467. package/dist/services/vnext-merge.js +332 -111
  468. package/dist/services/vnext-plan-review.js +279 -127
  469. package/dist/services/vnext-plan.js +228 -104
  470. package/dist/services/vnext-protocolize.js +531 -95
  471. package/dist/services/vnext-specify.js +119 -81
  472. package/dist/services/vnext-workspace-policy.js +7 -2
  473. package/dist/services/work-registry.js +1627 -199
  474. package/dist/services/workspace-bootstrap.js +52 -15
  475. package/dist/services/workspace-files.js +85 -0
  476. package/dist/services/worktrees.js +105 -48
  477. package/dist/shared/entity-references.js +8 -0
  478. package/dist/shared/errors.js +27 -0
  479. package/dist/storage/agent-output.js +26 -0
  480. package/dist/storage/database.js +891 -39
  481. package/dist/storage/foreign-key-transaction.js +39 -0
  482. package/dist/storage/immutable-input.js +45 -0
  483. package/dist/storage/import-guard.js +102 -0
  484. package/dist/storage/native-cleanup.js +52 -0
  485. package/dist/storage/paths.js +17 -4
  486. package/dist/storage/projection-lock.js +73 -0
  487. package/dist/storage/work-references.js +23 -0
  488. package/dist/storage/writer-contract.js +89 -0
  489. package/dist/storage/writer-migration.js +85 -0
  490. package/package.json +43 -13
  491. package/tools/create-canon-release.mjs +33 -0
  492. package/tools/produce-hybrid-release.mjs +41 -0
  493. package/tools/repair-paused-run-status.mjs +84 -0
@@ -2,18 +2,30 @@ import crypto from "node:crypto";
2
2
  import fs from "node:fs";
3
3
  import os from "node:os";
4
4
  import path from "node:path";
5
- import { parse, stringify } from "smol-toml";
5
+ import { parse, stringify, TomlError } from "smol-toml";
6
6
  import { AppError } from "../shared/errors.js";
7
+ import { entityReferenceVariants } from "../shared/entity-references.js";
7
8
  import { parseJsonObject } from "../shared/json.js";
8
- import { ensureDir, resolveProjectRoot } from "../storage/paths.js";
9
+ import { canonicalPath, ensureDir, resolveProjectRoot } from "../storage/paths.js";
10
+ import { resolveWorkReference } from "../storage/work-references.js";
9
11
  import { appendAudit } from "./audit.js";
12
+ import { memoryRepairAssignedHookCwd } from "./memory-repair-physical-binding.js";
13
+ import { memoryNativeReviewAssignedHookCwd } from "./memory-native-review-copy.js";
14
+ import { prepareTextFile, prepareJsonText, prepareError } from "../cli/input-preparation.js";
10
15
  import { nativeSessionIdentity, storageSessionId } from "./session-identity.js";
11
- import { commandHasOption, commandOption, commandPosition, parseLifecycleCommand, unwrapShellCommand } from "./lifecycle-command.js";
16
+ import { assertRecoveryHookCurrent } from "./run-recovery.js";
17
+ import { observeLifecycleInvocation, observedLifecycleInvocation, managedNativeInvocationScope } from "./lifecycle-invocations.js";
18
+ import { commandHasOption, commandOption, commandPosition, shellSuffixOffset, parseLifecycleCommand, lifecycleCommandFromPayload, continuationCommand } from "./lifecycle-command.js";
12
19
  import { registerProject, requireProjectByRoot } from "./projects.js";
13
- import { activeFlowSessionsForProject, bindObservedFlowSession, flowSessionPayloadFromRegisterCommand, recordFlowSessionObservation } from "./sessions.js";
20
+ import { activeFlowSessionsForProject, recordFlowSessionObservation } from "./sessions.js";
14
21
  const defaultProfileName = "default";
15
22
  const maxSanitizedSummaryLength = 1600;
16
- const sharedEntries = [
23
+ function lifecycleObservationResult(receipt) {
24
+ return { observed: receipt.disposition === "admitted", duplicate: receipt.duplicate,
25
+ event_key: receipt.authoritativeEventKey, current_event_key: receipt.currentEventKey,
26
+ authoritative_event_key: receipt.authoritativeEventKey, receipt_disposition: receipt.disposition };
27
+ }
28
+ export const codexHomeSharedEntries = [
17
29
  "auth.json",
18
30
  "auth.bk",
19
31
  "sessions",
@@ -50,22 +62,28 @@ export function planCodexHome(context, input) {
50
62
  config_path: paths.configPath,
51
63
  hooks_path: paths.hooksPath,
52
64
  owned_files: ["config.toml", "hooks.json", ".dd-flow-home.json", "log/", ".tmp/", "tmp/"],
53
- shared_entries: sharedEntries,
65
+ shared_entries: codexHomeSharedEntries,
54
66
  exists: fs.existsSync(paths.targetHome),
55
67
  codex_env: { CODEX_HOME: paths.targetHome }
56
68
  };
57
69
  }
58
- export function initCodexHome(context, input) {
70
+ export function prepareCodexHome(context, input) {
59
71
  const project = requireProjectByRoot(context, resolveProjectRoot(input.projectRoot));
60
72
  const profile = resolveProfileName(input.profile);
61
73
  const paths = resolveHomePaths(context, project, profile, input);
74
+ if (canonicalPath(paths.sourceHome, false) === canonicalPath(paths.targetHome, false))
75
+ throw new AppError("validation", "Managed Codex home must differ from its source home", 2);
76
+ const config = readTomlIfExists(path.join(paths.sourceHome, "config.toml"));
77
+ return { project, profile, paths, config };
78
+ }
79
+ export function initCodexHome(context, input, prepared = prepareCodexHome(context, input)) {
80
+ const { project, profile, paths, config } = prepared;
62
81
  const now = context.now();
63
82
  ensureDir(paths.targetHome);
64
83
  ensureDir(path.join(paths.targetHome, "log"));
65
84
  ensureDir(path.join(paths.targetHome, ".tmp"));
66
85
  ensureDir(path.join(paths.targetHome, "tmp"));
67
86
  const linked = createSharedLinks(paths.sourceHome, paths.targetHome);
68
- const config = readTomlIfExists(path.join(paths.sourceHome, "config.toml"));
69
87
  fs.writeFileSync(paths.configPath, stringify(withManagedConfig(config, paths.sourceHome, paths.targetHome)));
70
88
  writeJsonFile(paths.hooksPath, expectedHooksConfig(project.root));
71
89
  writeJsonFile(path.join(paths.targetHome, ".dd-flow-home.json"), {
@@ -136,7 +154,7 @@ export function printCodexHomeEnv(context, input) {
136
154
  command_prefix: `CODEX_HOME=${JSON.stringify(record.target_home)}`
137
155
  };
138
156
  }
139
- export function removeCodexHome(context, input) {
157
+ export function prepareCodexHomeRemoval(context, input) {
140
158
  if (!["keep-shared", "remove-owned"].includes(input.mode)) {
141
159
  throw new AppError("validation", "--mode must be keep-shared or remove-owned", 2);
142
160
  }
@@ -144,9 +162,30 @@ export function removeCodexHome(context, input) {
144
162
  const project = requireProjectByRoot(context, resolveProjectRoot(input.projectRoot));
145
163
  const profile = resolveProfileName(input.profile);
146
164
  const record = homeProfile(context, project.id, profile);
165
+ if (record && record.status !== "removed" && fs.existsSync(record.target_home)) {
166
+ let marker;
167
+ try {
168
+ const value = JSON.parse(fs.readFileSync(path.join(record.target_home, ".dd-flow-home.json"), "utf8"));
169
+ if (!value || typeof value !== "object" || Array.isArray(value))
170
+ throw new Error("Invalid marker");
171
+ marker = value;
172
+ }
173
+ catch {
174
+ throw new AppError("runtime_artifact_invalid", "Managed Codex home ownership marker is missing or invalid; refusing removal", 1, { target_home: record.target_home });
175
+ }
176
+ if (marker.managed_by !== "dd-flow-cli" || marker.project_id !== project.id || marker.profile !== profile || marker.target_home !== record.target_home || marker.source_home !== record.source_home || canonicalPath(record.target_home, false) === canonicalPath(record.source_home, false)) {
177
+ throw new AppError("runtime_artifact_invalid", "Managed Codex home ownership does not match its registration; refusing removal", 1, { target_home: record.target_home });
178
+ }
179
+ }
180
+ return { project, profile, record, mode };
181
+ }
182
+ export function removeCodexHome(context, input) {
183
+ const { project, profile, record, mode } = prepareCodexHomeRemoval(context, input);
147
184
  if (!record) {
148
185
  return { ok: true, removed: false, profile, status: "not_initialized" };
149
186
  }
187
+ if (record.status === "removed")
188
+ return { ok: true, removed: false, profile, status: "already_removed" };
150
189
  const removedEntries = [];
151
190
  for (const relative of ["config.toml", "hooks.json", ".dd-flow-home.json", "log", ".tmp", "tmp"]) {
152
191
  const target = path.join(record.target_home, relative);
@@ -207,7 +246,7 @@ export function getCodexHooksStatus(context, input) {
207
246
  recorded
208
247
  };
209
248
  }
210
- export function installCodexHooks(context, input) {
249
+ export function prepareCodexHooks(context, input) {
211
250
  const project = requireProjectByRoot(context, resolveProjectRoot(input.projectRoot));
212
251
  const target = resolveHookTarget(input.target);
213
252
  const location = requireHookLocation(context, project, target, resolveProfileName(input.profile));
@@ -217,9 +256,16 @@ export function installCodexHooks(context, input) {
217
256
  hooks_path: location.hooksPath
218
257
  });
219
258
  }
220
- const existing = readJsonIfExists(location.hooksPath) ?? {};
221
- const backup = target === "default" && fs.existsSync(location.hooksPath) ? backupFile(location.hooksPath, context.now()) : null;
259
+ const text = fs.existsSync(location.hooksPath) ? prepareTextFile(location.hooksPath, "hooks configuration").text : null;
260
+ const existing = text === null ? {} : prepareJsonText(text, { file: location.hooksPath }).value;
261
+ if (!existing || typeof existing !== "object" || Array.isArray(existing) || (existing.hooks !== undefined && (!existing.hooks || typeof existing.hooks !== "object" || Array.isArray(existing.hooks) || Object.values(existing.hooks).some(entries => !Array.isArray(entries)))))
262
+ return prepareError(new AppError("validation", "Hooks configuration requires an object with event arrays", 2), { file: location.hooksPath });
263
+ return { project, target, location, existing, text };
264
+ }
265
+ export function installCodexHooks(context, input, prepared = prepareCodexHooks(context, input)) {
266
+ const { project, target, location, existing, text } = prepared;
222
267
  const nextConfig = mergeHooksConfig(existing, project.root);
268
+ const backup = target === "default" && text !== null ? backupFile(location.hooksPath, context.now(), text) : null;
223
269
  ensureDir(path.dirname(location.hooksPath));
224
270
  writeJsonFile(location.hooksPath, nextConfig);
225
271
  recordHookInstallation(context, project.id, installationScope(target, location.profile, location.hooksPath), location.hooksPath, true, "none");
@@ -238,20 +284,11 @@ export function installCodexHooks(context, input) {
238
284
  expected_hash: hashObject(expectedHooksConfig(project.root))
239
285
  };
240
286
  }
241
- export function removeCodexHooks(context, input) {
242
- const project = requireProjectByRoot(context, resolveProjectRoot(input.projectRoot));
243
- const target = resolveHookTarget(input.target);
244
- const location = requireHookLocation(context, project, target, resolveProfileName(input.profile));
245
- if (target === "default" && !input.yes) {
246
- throw new AppError("confirmation_required", "Direct default Codex home mutation requires --yes", 1, {
247
- target,
248
- hooks_path: location.hooksPath
249
- });
250
- }
251
- const existing = readJsonIfExists(location.hooksPath) ?? {};
287
+ export function removeCodexHooks(context, input, prepared = prepareCodexHooks(context, input)) {
288
+ const { project, target, location, existing, text } = prepared;
252
289
  const hadManaged = hasManagedHooks(existing, project.root);
253
- const backup = target === "default" && fs.existsSync(location.hooksPath) ? backupFile(location.hooksPath, context.now()) : null;
254
290
  const nextConfig = removeManagedHooks(existing, project.root);
291
+ const backup = target === "default" && text !== null ? backupFile(location.hooksPath, context.now(), text) : null;
255
292
  ensureDir(path.dirname(location.hooksPath));
256
293
  writeJsonFile(location.hooksPath, nextConfig);
257
294
  recordHookInstallation(context, project.id, installationScope(target, location.profile, location.hooksPath), location.hooksPath, false, "not_installed");
@@ -274,98 +311,148 @@ export function handleCodexHook(context, input) {
274
311
  // therefore the only valid identity for a fresh Work.
275
312
  const rawChildProviderSessionId = stringValue(payload.agent_id) ?? null;
276
313
  const providerSessionId = rawChildProviderSessionId ?? rootProviderSessionId;
277
- const parentProviderSessionId = rawChildProviderSessionId ? codexNativeParentId(payload, rawChildProviderSessionId) : null;
278
314
  const turnId = stringValue(payload.turn_id);
279
315
  const toolName = stringValue(payload.tool_name) ?? toolNameFromPayload(payload);
280
316
  const command = lifecycleCommandFromPayload(payload);
281
317
  if (!command)
282
318
  return { ok: true, observed: false, reason: "non_bash_tool" };
283
319
  const lifecycle = lifecycleFacts(command);
284
- if (!lifecycle)
320
+ if (!lifecycle && !continuationCommand(command))
285
321
  return { ok: true, observed: false, reason: "event_not_participating", event: eventName };
322
+ const parentProviderSessionId = rawChildProviderSessionId ? codexNativeParentId(payload, rawChildProviderSessionId) : null;
286
323
  // Resume legitimately receives an answer through stdin. It is matched by
287
324
  // immutable lifecycle arguments below; never reject or rewrite that pipe.
288
- if (lifecycle.analysis.kind === "compound") {
289
- return {
290
- ok: false,
291
- observed: false,
292
- reason: "compound_lifecycle_command",
293
- message: "Run the dd-flow lifecycle invocation as the first technical action and as one standalone Bash command in this same Session.",
294
- standalone_command: lifecycle.invocation.command,
295
- hookSpecificOutput: {
296
- hookEventName: "PreToolUse",
297
- permissionDecision: "deny",
298
- permissionDecisionReason: `dd-flow lifecycle commands must be standalone. Retry in this same Session: ${lifecycle.invocation.command}`
299
- }
300
- };
325
+ const bootstrapStageStart = lifecycle?.bootstrapStageStart;
326
+ const invocationId = lifecycle ? commandOption(lifecycle.invocation, "invocation-id") : undefined;
327
+ if (invocationId || context.env.DD_FLOW_DAEMON_ID) {
328
+ const proof = context.codexHookProof;
329
+ if (!proof || proof.daemonId !== context.env.DD_FLOW_DAEMON_ID || proof.rootSessionId !== rootProviderSessionId
330
+ || proof.sessionId !== providerSessionId || proof.parentSessionId !== parentProviderSessionId
331
+ || proof.toolCallId !== payload.tool_use_id || proof.command !== command) {
332
+ throw new AppError("native_hook_unproven", "Managed Codex admission requires its active native hook delivery, not a manual handler call", 1, { ...(invocationId ? { invocation_id: invocationId } : {}), effect: "no_effect" });
333
+ }
301
334
  }
302
- const { flowPayload, stageStart, stageResume, workStart, bootstrapStageStart, commandProjectRoot } = lifecycle;
303
- const matchKey = commandProjectRoot ? lifecycleMatchKey(lifecycle, commandProjectRoot) : null;
335
+ const nativeProjectRoot = context.codexHookProof?.projectRoot ?? input.projectRoot;
304
336
  // A bootstrap stage is the first stateful command for a new materialized project.
305
337
  // Its PreToolUse event arrives before stage start can register that project.
306
- if (bootstrapStageStart && commandProjectRoot) {
307
- registerProject(context, { root: commandProjectRoot });
338
+ const bootstrapRoot = nativeProjectRoot ?? stringValue(payload.cwd);
339
+ if (bootstrapStageStart && bootstrapRoot) {
340
+ registerProject(context, { root: bootstrapRoot });
308
341
  }
309
- const project = projectForHook(context, input.projectRoot ?? commandProjectRoot, stringValue(payload.cwd));
310
- if (!project)
342
+ const project = projectForHook(context, nativeProjectRoot, stringValue(payload.cwd));
343
+ if (!project) {
344
+ if (invocationId)
345
+ throw new AppError("project_not_registered", "Managed lifecycle invocation has no registered project", 1, { project_root: nativeProjectRoot ?? null });
311
346
  return { ok: true, observed: false, reason: "unrelated_cwd" };
312
- const binding = rootProviderSessionId ? upsertSessionBindingFromPayload(context, project, rootProviderSessionId, payload) : undefined;
347
+ }
348
+ let matchKey = lifecycle ? lifecycleMatchKey(lifecycle, project.root) : null;
349
+ // Codex retains the root session_id for a thread-spawned child, while the
350
+ // payload transcript belongs to the current agent_id. Keep lineage separate
351
+ // from ownership: otherwise a child overwrites the root transcript binding.
352
+ const eventKey = hookEventKey(payload, eventName, toolName, command);
313
353
  const storageId = providerSessionId ? storageSessionId(nativeSessionIdentity("codex-desktop", providerSessionId)) : null;
314
354
  const parentSessionId = parentProviderSessionId ? storageSessionId(nativeSessionIdentity("codex-desktop", parentProviderSessionId)) : null;
315
- const observedSession = flowPayload
316
- ? bindObservedFlowSession(context, project, {
317
- ...flowPayload,
318
- harness: "codex-desktop",
319
- provider_session_id: providerSessionId ?? flowPayload.provider_session_id ?? null,
320
- parent_session_id: parentSessionId
321
- }, storageId ?? undefined)
322
- : undefined;
323
- const effectiveSessionId = observedSession?.session_id ?? storageId;
324
- const protocolId = observedSession?.protocol_id ?? binding?.protocol_id ?? null;
325
- const eventKey = hookEventKey(payload, eventName, toolName, command);
326
- const inserted = recordHookEvent(context, {
327
- projectId: project.id,
328
- protocolId,
329
- harness: "codex-desktop",
330
- providerSessionId: providerSessionId ?? null,
331
- parentSessionId,
332
- sessionId: effectiveSessionId,
333
- agentId: rawChildProviderSessionId ?? null,
334
- turnId: turnId ?? null,
335
- eventName,
336
- toolName: toolName ?? null,
337
- status: "observed",
338
- payload,
339
- eventKey,
340
- matchKey,
341
- transcriptPath: binding?.transcript_path ?? null,
342
- cwd: stringValue(payload.cwd) ?? null
343
- });
344
- if (effectiveSessionId) {
345
- recordFlowSessionObservation(context, {
355
+ const eventCwd = stringValue(payload.cwd) ?? project.root;
356
+ assertHookEventReplay(context, { projectId: project.id, eventKey, harness: "codex-desktop", providerSessionId: providerSessionId ?? null, parentSessionId, daemonId: stringValue(context.env.DD_FLOW_DAEMON_ID) ?? null, sessionId: storageId, turnId: turnId ?? null, eventName, toolName: toolName ?? null, matchKey, cwd: eventCwd });
357
+ const bindingOwnerId = providerSessionId ?? rootProviderSessionId;
358
+ // Resolve legacy transcript discovery before acquiring the receipt write lock.
359
+ const transcriptPath = stringValue(payload.transcript_path)
360
+ ?? (bindingOwnerId ? sessionBinding(context, project.id, bindingOwnerId)?.transcript_path ?? locateCodexTranscript(bindingOwnerId, context.env.CODEX_HOME) : null);
361
+ const persist = (scope) => {
362
+ if (scope)
363
+ matchKey = lifecycleMatchKey(lifecycleFacts(scope.command), project.root, scope);
364
+ const binding = bindingOwnerId ? upsertSessionBindingFromPayload(context, project, bindingOwnerId, payload, transcriptPath) : undefined;
365
+ const effectiveSessionId = storageId;
366
+ const protocolId = binding?.protocol_id ?? null;
367
+ const inserted = recordHookEvent(context, {
346
368
  projectId: project.id,
347
- sessionId: effectiveSessionId,
348
- runId: observedSession?.run_id ?? null,
349
369
  protocolId,
350
- cwd: stringValue(payload.cwd) ?? null,
370
+ harness: "codex-desktop",
371
+ providerSessionId: providerSessionId ?? null,
372
+ parentSessionId,
373
+ // Pinned by the owning app-server process, never derived from current RUN
374
+ // state or an agent-supplied shell command/payload.
375
+ daemonId: stringValue(context.env.DD_FLOW_DAEMON_ID) ?? null,
376
+ sessionId: effectiveSessionId,
377
+ agentId: rawChildProviderSessionId ?? null,
378
+ turnId: turnId ?? null,
379
+ eventName,
351
380
  toolName: toolName ?? null,
352
- eventKey
381
+ status: "observed",
382
+ payload,
383
+ eventKey,
384
+ matchKey,
385
+ transcriptPath: binding?.transcript_path ?? null,
386
+ cwd: stringValue(payload.cwd) ?? project.root
353
387
  });
354
- }
355
- return {
388
+ if (effectiveSessionId) {
389
+ recordFlowSessionObservation(context, {
390
+ projectId: project.id,
391
+ sessionId: effectiveSessionId,
392
+ protocolId,
393
+ cwd: eventCwd,
394
+ toolName: toolName ?? null,
395
+ eventKey
396
+ });
397
+ }
398
+ return { inserted, protocolId };
399
+ };
400
+ const common = {
356
401
  ok: true,
357
- observed: inserted,
358
- duplicate: !inserted,
359
402
  event_key: eventKey,
360
403
  session: providerSessionId ? nativeSessionIdentity("codex-desktop", providerSessionId) : null,
361
404
  ...(parentProviderSessionId ? { parent_session: nativeSessionIdentity("codex-desktop", parentProviderSessionId) } : {}),
362
- protocol_id: protocolId,
363
- ...(providerSessionId && (flowPayload || stageStart || stageResume || workStart)
405
+ protocol_id: null
406
+ };
407
+ if (!lifecycle) {
408
+ if (providerSessionId)
409
+ assignedLifecycleCwd(context, project.root, null, providerSessionId, command, stringValue(payload.cwd));
410
+ const persisted = context.db.writeTransaction(() => persist());
411
+ return { ...common, observed: persisted.inserted, duplicate: !persisted.inserted };
412
+ }
413
+ if (invocationId || context.env.DD_FLOW_DAEMON_ID) {
414
+ const daemonId = stringValue(context.env.DD_FLOW_DAEMON_ID);
415
+ const toolCallId = stringValue(payload.tool_use_id) ?? stringValue(payload.event_id) ?? stringValue(payload.delivery_id) ?? stringValue(payload.id);
416
+ if (!daemonId || !rootProviderSessionId || !providerSessionId || !toolCallId)
417
+ throw new AppError("codex_identity_missing", "Managed Codex lifecycle hook has no complete native invocation identity", 1, { invocation_id: invocationId });
418
+ const executionCwd = assignedLifecycleCwd(context, project.root, lifecycle, providerSessionId, command, stringValue(payload.cwd), { daemonId, rootSessionId: rootProviderSessionId, sessionId: providerSessionId, parentSessionId: parentProviderSessionId, toolCallId }, "codex-desktop");
419
+ const receipt = observeLifecycleInvocation(context, {
420
+ currentEventKey: eventKey,
421
+ ...(invocationId ? { id: invocationId } : {}),
422
+ command,
423
+ harness: "codex-desktop",
424
+ projectId: project.id,
425
+ ...(executionCwd ? { executionCwd } : {}),
426
+ identity: { daemonId, rootSessionId: rootProviderSessionId, sessionId: providerSessionId, parentSessionId: parentProviderSessionId, toolCallId },
427
+ recordReceipt: scope => { persist(scope); return eventKey; }
428
+ });
429
+ const stored = context.db.get("SELECT protocol_id FROM hook_events WHERE project_id = ? AND event_key = ?", [project.id, receipt.eventKey]);
430
+ const admittedInvocationId = receipt.invocationId ?? invocationId;
431
+ if (admittedInvocationId)
432
+ return { ...common, protocol_id: stored?.protocol_id ?? null, ...lifecycleObservationResult(receipt), invocation_id: admittedInvocationId };
433
+ return {
434
+ ...common,
435
+ protocol_id: stored?.protocol_id ?? null,
436
+ ...lifecycleObservationResult(receipt),
437
+ hookSpecificOutput: {
438
+ hookEventName: "PreToolUse",
439
+ permissionDecision: "allow",
440
+ updatedInput: { ...objectRecord(payload.tool_input), command: commandWithHookEvent(command, eventKey) }
441
+ }
442
+ };
443
+ }
444
+ const persisted = persist();
445
+ return {
446
+ ...common,
447
+ protocol_id: persisted.protocolId,
448
+ observed: persisted.inserted,
449
+ duplicate: !persisted.inserted,
450
+ ...(providerSessionId
364
451
  ? {
365
452
  hookSpecificOutput: {
366
453
  hookEventName: "PreToolUse",
367
454
  permissionDecision: "allow",
368
- updatedInput: { command: commandWithHookEvent(command, eventKey) }
455
+ updatedInput: { ...objectRecord(payload.tool_input), command: commandWithHookEvent(command, eventKey) }
369
456
  }
370
457
  }
371
458
  : {})
@@ -377,7 +464,24 @@ function codexNativeParentId(payload, childId) {
377
464
  if (!transcript || !path.isAbsolute(transcript))
378
465
  return null;
379
466
  try {
380
- const firstLine = fs.readFileSync(transcript, "utf8").split("\n", 1)[0];
467
+ const descriptor = fs.openSync(transcript, "r");
468
+ const chunks = [];
469
+ try {
470
+ for (;;) {
471
+ const buffer = Buffer.alloc(4096);
472
+ const length = fs.readSync(descriptor, buffer, 0, buffer.length, null);
473
+ if (!length)
474
+ break;
475
+ const newline = buffer.subarray(0, length).indexOf(10);
476
+ chunks.push(buffer.subarray(0, newline < 0 ? length : newline));
477
+ if (newline >= 0)
478
+ break;
479
+ }
480
+ }
481
+ finally {
482
+ fs.closeSync(descriptor);
483
+ }
484
+ const firstLine = Buffer.concat(chunks).toString("utf8");
381
485
  if (!firstLine)
382
486
  return null;
383
487
  const record = JSON.parse(firstLine);
@@ -389,117 +493,141 @@ function codexNativeParentId(payload, childId) {
389
493
  return null;
390
494
  }
391
495
  }
392
- /** Convert a zcode-acp Bash tool notification into the same trusted lifecycle receipt used by Codex hooks. */
496
+ /** Qualified ACP admission binds public commands to an exact issued assignment.
497
+ * Its provenance is an observation, never a synchronous PreToolUse assertion. */
393
498
  export function handleZcodeEvent(context, input) {
394
499
  const notification = parseJsonObject(input.stdin || "{}", "zcode ACP notification");
395
- if (notification.method !== "session/update")
500
+ const params = objectRecord(notification.params), update = objectRecord(params.update);
501
+ if (notification.method !== "session/update" || update.sessionUpdate !== "tool_call")
396
502
  return { ok: true, observed: false, reason: "event_not_participating" };
397
- const params = objectRecord(notification.params);
398
- const update = objectRecord(params.update);
399
- if (update.sessionUpdate !== "tool_call")
400
- return { ok: true, observed: false, reason: "event_not_participating" };
401
- const updateMeta = objectRecord(update._meta);
402
- const claudeCode = objectRecord(updateMeta.claudeCode);
403
- const runtime = objectRecord(updateMeta.zcodeRuntime);
404
- const toolName = stringValue(claudeCode.toolName) ?? stringValue(update.kind);
405
- const rawInput = objectRecord(update.rawInput);
503
+ const rawInput = objectRecord(update.rawInput), meta = objectRecord(update._meta), native = objectRecord(meta.zcodeRuntime);
504
+ const toolName = stringValue(objectRecord(meta.claudeCode).toolName);
505
+ if (toolName !== "Bash" && toolName !== "bash" && toolName !== "execute")
506
+ return { ok: true, observed: false, reason: "non_bash_tool" };
406
507
  const command = stringValue(rawInput.command) ?? stringValue(rawInput.cmd);
407
- if (!command || (toolName && toolName !== "Bash" && toolName !== "bash" && update.kind !== "execute")) {
508
+ const parsed = command ? parseLifecycleCommand(command) : { kind: "none", participation: "none" };
509
+ if (parsed.participation === "none" || parsed.participation === "unproven")
510
+ return { ok: true, observed: false, reason: "acp_evidence_only" };
511
+ if (parsed.kind === "none" && !continuationCommand(command))
512
+ throw new AppError("lifecycle_shell_syntax_invalid", "Known lifecycle shell execution is unsupported", 1, { reason: parsed.reason, effect: "unknown", recoverable: false });
513
+ const controlled = objectRecord(objectRecord(notification._meta).ddZcode);
514
+ const providerSessionId = stringValue(native.childSessionId) ?? stringValue(controlled.rootProviderSessionId);
515
+ return recordZcodeLifecycle(context, { projectRoot: input.projectRoot, stdin: JSON.stringify({
516
+ session_id: providerSessionId, tool_use_id: update.toolCallId, tool_name: toolName, tool_input: rawInput,
517
+ agent_id: native.agentId, _meta: { ddZcode: controlled }
518
+ }) }, parsed.kind !== "none" ? commandOption(parsed.invocation, "invocation-id") : undefined);
519
+ }
520
+ /**
521
+ * Accept the synchronous native ZCode PreToolUse callback. Invocation-ID
522
+ * commands instead rendezvous with their ACP event while the CLI awaits
523
+ * admission; the native hook must not create a competing receipt for them.
524
+ */
525
+ export function handleZcodeHook(context, input) {
526
+ return recordZcodeLifecycle(context, input, undefined, true);
527
+ }
528
+ function recordZcodeLifecycle(context, input, invocationId, nativePreTool = false) {
529
+ const hook = parseJsonObject(input.stdin || "{}", "ZCode hook event");
530
+ const eventName = stringValue(hook.hook_event_name) ?? stringValue(hook.hookEventName);
531
+ if (eventName && eventName !== "PreToolUse" && eventName !== "pre_tool_use")
532
+ return { ok: true, observed: false, reason: "event_not_participating" };
533
+ const toolName = stringValue(hook.tool_name) ?? stringValue(hook.toolName);
534
+ const rawInput = objectRecord(hook.tool_input ?? hook.toolInput);
535
+ const command = stringValue(rawInput.command) ?? stringValue(rawInput.cmd) ?? stringValue(hook.command);
536
+ if (!command || (toolName && toolName !== "Bash" && toolName !== "bash" && toolName !== "execute"))
408
537
  return { ok: true, observed: false, reason: "non_bash_tool" };
409
- }
410
538
  const lifecycle = lifecycleFacts(command);
411
- if (!lifecycle)
539
+ if (!lifecycle && !continuationCommand(command))
412
540
  return { ok: true, observed: false, reason: "event_not_participating" };
413
- if (lifecycle.analysis.kind === "compound") {
414
- throw new AppError("compound_lifecycle_command", "dd-flow lifecycle commands must be a standalone ZCode Bash tool call", 1, {
415
- standalone_command: lifecycle.invocation.command
416
- });
541
+ // ZCode can execute background Bash without emitting the ACP tool_call that admits an issued CLI invocation.
542
+ if (nativePreTool && lifecycle && rawInput.run_in_background === true) {
543
+ const reason = "Run dd-flow lifecycle commands in the foreground; ZCode background Bash does not provide the required admission receipt.";
544
+ return { continue: false, reason, hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "deny", permissionDecisionReason: reason } };
417
545
  }
418
- const { flowPayload, bootstrapStageStart, commandProjectRoot } = lifecycle;
546
+ if (!invocationId && lifecycle && commandOption(lifecycle.invocation, "invocation-id"))
547
+ return { ok: true, observed: false, reason: "cli_invocation_owns_admission" };
419
548
  const expectedRoot = resolveProjectRoot(input.projectRoot);
420
- if (commandProjectRoot && resolveProjectRoot(commandProjectRoot) !== expectedRoot) {
421
- throw new AppError("project_mismatch", "ZCode lifecycle command project root does not match the controlled workspace", 1, {
422
- expected: expectedRoot,
423
- actual: commandProjectRoot
424
- });
549
+ const meta = objectRecord(hook._meta);
550
+ const ddZcode = objectRecord(meta.ddZcode);
551
+ const providerSessionId = stringValue(hook.session_id) ?? stringValue(hook.sessionId);
552
+ const rootProviderSessionId = stringValue(ddZcode.rootProviderSessionId);
553
+ const parentProviderSessionId = stringValue(ddZcode.parentProviderSessionId);
554
+ const daemonId = stringValue(ddZcode.daemonId);
555
+ if (!providerSessionId || !rootProviderSessionId || !daemonId) {
556
+ throw new AppError("zcode_identity_missing", "ZCode native hook has no controlled Session identity", 1);
425
557
  }
426
- const matchKey = lifecycleMatchKey(lifecycle, expectedRoot);
427
- if (bootstrapStageStart)
558
+ if (providerSessionId === rootProviderSessionId ? parentProviderSessionId : !parentProviderSessionId) {
559
+ throw new AppError("zcode_identity_missing", "ZCode native hook has inconsistent Session ancestry", 1);
560
+ }
561
+ const toolUseId = stringValue(hook.tool_use_id) ?? stringValue(hook.toolUseId) ?? stringValue(hook.event_id) ?? stringValue(hook.eventId);
562
+ if (!toolUseId)
563
+ throw new AppError("zcode_hook_event_missing", "ZCode native hook has no tool-use ID", 1);
564
+ const eventKey = crypto.createHash("sha256").update(JSON.stringify(["zcode-acp", daemonId, providerSessionId, toolUseId])).digest("hex");
565
+ const rewritten = invocationId ? command : commandWithHookEvent(command, eventKey);
566
+ if (lifecycle?.bootstrapStageStart)
428
567
  registerProject(context, { root: expectedRoot });
429
568
  const project = requireProjectByRoot(context, expectedRoot);
430
- const envelopeMeta = objectRecord(notification._meta);
431
- const ddZcode = objectRecord(envelopeMeta.ddZcode);
432
- const observedProfile = objectRecord(ddZcode.observedProfile);
433
- const rootProviderSessionId = stringValue(ddZcode.rootProviderSessionId) ?? stringValue(params.sessionId);
434
- const daemonId = stringValue(ddZcode.daemonId);
435
- if (!rootProviderSessionId)
436
- throw new AppError("zcode_identity_missing", "ZCode ACP event has no root provider Session ID", 1);
437
- const childProviderSessionId = stringValue(runtime.childSessionId);
438
- const providerSessionId = childProviderSessionId ?? rootProviderSessionId;
439
569
  const sessionId = storageSessionId(nativeSessionIdentity("zcode-acp", providerSessionId));
440
- const parentSessionId = childProviderSessionId ? storageSessionId(nativeSessionIdentity("zcode-acp", rootProviderSessionId)) : null;
441
- const agentId = stringValue(runtime.agentId);
442
- const observedSession = flowPayload ? bindObservedFlowSession(context, project, {
443
- ...flowPayload,
444
- harness: "zcode-acp",
445
- provider_session_id: providerSessionId,
446
- agent_id: agentId ?? null,
447
- parent_session_id: parentSessionId
448
- }, sessionId) : undefined;
449
- const eventKey = stringValue(update.toolCallId) ?? crypto.createHash("sha256").update(JSON.stringify({
450
- harness: "zcode-acp", providerSessionId, command
451
- })).digest("hex");
452
- const payload = {
453
- session_id: providerSessionId,
454
- agent_id: agentId,
455
- cwd: expectedRoot,
456
- tool_name: toolName ?? "Bash",
457
- tool_input: rawInput,
458
- command,
459
- provider: stringValue(observedProfile.provider),
460
- model: stringValue(observedProfile.model),
461
- reasoning: stringValue(observedProfile.reasoning),
462
- mode: stringValue(observedProfile.mode),
463
- agent_type: stringValue(runtime.source)
570
+ const parentSessionId = parentProviderSessionId ? storageSessionId(nativeSessionIdentity("zcode-acp", parentProviderSessionId)) : null;
571
+ const observedProfile = objectRecord(ddZcode.observedProfile);
572
+ const observedCwd = stringValue(hook.cwd) && path.isAbsolute(String(hook.cwd)) ? resolveProjectRoot(String(hook.cwd)) : null;
573
+ let matchKey = lifecycle ? lifecycleMatchKey(lifecycle, expectedRoot) : null;
574
+ const eventNameForReceipt = nativePreTool ? "PreToolUse" : "ToolCallObserved";
575
+ const persist = (scope) => {
576
+ if (scope)
577
+ matchKey = lifecycleMatchKey(lifecycleFacts(scope.command), expectedRoot, scope);
578
+ assertHookEventReplay(context, { projectId: project.id, eventKey, harness: "zcode-acp", providerSessionId, parentSessionId, daemonId, sessionId, turnId: null, eventName: eventNameForReceipt, toolName: toolName ?? "Bash", matchKey, cwd: observedCwd });
579
+ const payload = { ...hook, session_id: providerSessionId, cwd: observedCwd, tool_name: toolName ?? "Bash", tool_input: rawInput,
580
+ command, provider: stringValue(observedProfile.provider), model: stringValue(observedProfile.model), reasoning: stringValue(observedProfile.reasoning), mode: stringValue(observedProfile.mode) };
581
+ const inserted = recordHookEvent(context, {
582
+ projectId: project.id, protocolId: null, harness: "zcode-acp", providerSessionId, parentSessionId,
583
+ daemonId, sessionId, agentId: stringValue(hook.agent_id) ?? stringValue(hook.agentId) ?? null, turnId: null,
584
+ eventName: eventNameForReceipt, toolName: toolName ?? "Bash", status: "observed", payload, eventKey, matchKey,
585
+ transcriptPath: stringValue(hook.transcript_path) ?? null, cwd: observedCwd
586
+ });
587
+ recordFlowSessionObservation(context, { projectId: project.id, sessionId, cwd: observedCwd, toolName: toolName ?? "Bash", eventKey });
588
+ 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 };
589
+ return { ...result, hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...rawInput, command: rewritten } } };
464
590
  };
465
- const inserted = recordHookEvent(context, {
466
- projectId: project.id,
467
- protocolId: observedSession?.protocol_id ?? null,
591
+ if (!lifecycle) {
592
+ assignedLifecycleCwd(context, expectedRoot, null, providerSessionId, command, observedCwd ?? undefined);
593
+ return context.db.writeTransaction(() => persist());
594
+ }
595
+ let persistedResult;
596
+ const executionCwd = assignedLifecycleCwd(context, expectedRoot, lifecycle, providerSessionId, command, observedCwd ?? undefined, { daemonId, rootSessionId: rootProviderSessionId, sessionId: providerSessionId, parentSessionId: parentProviderSessionId ?? null, toolCallId: toolUseId }, "zcode-acp");
597
+ const receipt = observeLifecycleInvocation(context, {
598
+ currentEventKey: eventKey,
599
+ ...(invocationId ? { id: invocationId } : {}), command,
468
600
  harness: "zcode-acp",
469
- providerSessionId,
470
- parentSessionId,
471
- daemonId: daemonId ?? null,
472
- sessionId,
473
- agentId: agentId ?? null,
474
- turnId: null,
475
- eventName: "PreToolUse",
476
- toolName: toolName ?? "Bash",
477
- status: "observed",
478
- payload,
479
- eventKey,
480
- matchKey,
481
- transcriptPath: null,
482
- cwd: expectedRoot
483
- });
484
- recordFlowSessionObservation(context, {
485
601
  projectId: project.id,
486
- sessionId,
487
- runId: observedSession?.run_id ?? null,
488
- protocolId: observedSession?.protocol_id ?? null,
489
- cwd: expectedRoot,
490
- toolName: toolName ?? "Bash",
491
- eventKey
602
+ ...(executionCwd ? { executionCwd } : {}),
603
+ identity: { daemonId, rootSessionId: rootProviderSessionId, sessionId: providerSessionId, parentSessionId: parentProviderSessionId ?? null, toolCallId: toolUseId },
604
+ recordReceipt: scope => { persistedResult = persist(scope); return eventKey; }
492
605
  });
493
- return {
494
- ok: true,
495
- observed: inserted,
496
- duplicate: !inserted,
497
- event_key: eventKey,
498
- harness: "zcode-acp",
499
- session: nativeSessionIdentity("zcode-acp", providerSessionId),
500
- ...(childProviderSessionId ? { parent_session: nativeSessionIdentity("zcode-acp", rootProviderSessionId) } : {}),
501
- daemon_id: daemonId ?? null
502
- };
606
+ if (receipt.disposition === "replay") {
607
+ // Keep first admission immutable. A second transport is independently
608
+ // retained corroboration, never another claim or an invented ACP cwd.
609
+ const retained = context.db.get("SELECT event_name, match_key, harness, provider_session_id, parent_session_id, daemon_id FROM hook_events WHERE project_id = ? AND event_key = ?", [project.id, receipt.eventKey]);
610
+ if (retained && retained.event_name !== eventNameForReceipt) {
611
+ if (retained.harness !== "zcode-acp" || retained.provider_session_id !== providerSessionId || retained.parent_session_id !== parentSessionId || retained.daemon_id !== daemonId) {
612
+ throw new AppError("hook_event_replay_mismatch", "Corroborating transport has different native ownership", 1, { event_key: receipt.eventKey });
613
+ }
614
+ context.db.writeTransaction(() => recordHookEvent(context, { projectId: project.id, protocolId: null, harness: "zcode-acp", providerSessionId, parentSessionId,
615
+ daemonId, sessionId, agentId: stringValue(hook.agent_id) ?? null, turnId: null, eventName: eventNameForReceipt, toolName: toolName ?? "Bash", status: "corroborating",
616
+ payload: { ...hook, command, corroborates_event_key: receipt.eventKey, admission_transport: nativePreTool ? "native_pre_tool" : "acp_observation" },
617
+ eventKey: `${receipt.eventKey}:${eventNameForReceipt}`, matchKey: null, transcriptPath: stringValue(hook.transcript_path) ?? null, cwd: observedCwd }));
618
+ }
619
+ else if (retained) {
620
+ assertHookEventReplay(context, { projectId: project.id, eventKey: receipt.eventKey, harness: "zcode-acp", providerSessionId, parentSessionId, daemonId,
621
+ sessionId, turnId: null, eventName: eventNameForReceipt, toolName: toolName ?? "Bash", matchKey: retained.match_key, cwd: observedCwd });
622
+ }
623
+ }
624
+ const admittedInvocationId = receipt.invocationId ?? invocationId;
625
+ if (admittedInvocationId)
626
+ return { ok: true, ...lifecycleObservationResult(receipt), invocation_id: admittedInvocationId };
627
+ // Without an issued managed attempt this is still ordinary hook evidence.
628
+ // Preserve persist()'s replay result instead of overwriting it with the
629
+ // admission layer's non-authoritative duplicate flag.
630
+ return persistedResult ?? persist();
503
631
  }
504
632
  /** Convert a Grok Build PreToolUse hook into a trusted lifecycle receipt. */
505
633
  export function handleGrokEvent(context, input) {
@@ -515,27 +643,18 @@ export function handleGrokEvent(context, input) {
515
643
  return { ok: true, observed: false, reason: "non_bash_tool" };
516
644
  }
517
645
  const lifecycle = lifecycleFacts(command);
518
- if (!lifecycle)
646
+ if (!lifecycle && !continuationCommand(command))
519
647
  return { ok: true, observed: false, reason: "event_not_participating" };
520
- if (lifecycle.analysis.kind === "compound") {
521
- throw new AppError("compound_lifecycle_command", "dd-flow lifecycle commands must be a standalone Grok Build tool call", 1, {
522
- standalone_command: lifecycle.invocation.command
523
- });
524
- }
525
- const { flowPayload, stageStart, stageResume, workStart, bootstrapStageStart, commandProjectRoot } = lifecycle;
648
+ const bootstrapStageStart = lifecycle?.bootstrapStageStart;
526
649
  const expectedRoot = resolveProjectRoot(input.projectRoot);
527
- if (commandProjectRoot && resolveProjectRoot(commandProjectRoot) !== expectedRoot) {
528
- throw new AppError("project_mismatch", "Grok Build lifecycle command project root does not match the controlled workspace", 1, {
529
- expected: expectedRoot, actual: commandProjectRoot
530
- });
531
- }
532
- const matchKey = lifecycleMatchKey(lifecycle, expectedRoot);
650
+ let matchKey = lifecycle ? lifecycleMatchKey(lifecycle, expectedRoot) : null;
533
651
  if (bootstrapStageStart)
534
652
  registerProject(context, { root: expectedRoot });
535
653
  const project = requireProjectByRoot(context, expectedRoot);
536
654
  const meta = objectRecord(hook._meta);
537
655
  const ddGrok = objectRecord(meta.ddGrok);
538
656
  const observedProfile = objectRecord(ddGrok.observedProfile);
657
+ const observedCwd = stringValue(hook.cwd) && path.isAbsolute(String(hook.cwd)) ? resolveProjectRoot(String(hook.cwd)) : null;
539
658
  const rootProviderSessionId = stringValue(ddGrok.rootProviderSessionId);
540
659
  const providerSessionId = stringValue(hook.session_id) ?? stringValue(hook.sessionId);
541
660
  if (!rootProviderSessionId || !providerSessionId)
@@ -545,41 +664,171 @@ export function handleGrokEvent(context, input) {
545
664
  const parentSessionId = isChild ? storageSessionId(nativeSessionIdentity("grok-acp", stringValue(ddGrok.parentProviderSessionId) ?? rootProviderSessionId)) : null;
546
665
  const daemonId = stringValue(ddGrok.daemonId);
547
666
  const agentId = stringValue(hook.agent_id) ?? stringValue(hook.agentId);
548
- const observedSession = flowPayload ? bindObservedFlowSession(context, project, {
549
- ...flowPayload, harness: "grok-acp", provider_session_id: providerSessionId, agent_id: agentId ?? null, parent_session_id: parentSessionId
550
- }, sessionId) : undefined;
551
667
  const eventKey = stringValue(hook.event_id) ?? stringValue(hook.eventId) ?? crypto.createHash("sha256").update(JSON.stringify({
552
- harness: "grok-acp", providerSessionId, command, turn: hook.turn_id ?? hook.turnId ?? null
668
+ harness: "grok-acp", providerSessionId, toolUseId: hook.tool_use_id ?? hook.toolUseId ?? null,
669
+ promptId: hook.prompt_id ?? hook.promptId ?? null, command, turn: hook.turn_id ?? hook.turnId ?? null
553
670
  })).digest("hex");
554
- const payload = {
555
- ...hook, session_id: providerSessionId, agent_id: agentId, cwd: expectedRoot, tool_name: toolName ?? "Bash", tool_input: rawInput,
556
- 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)
671
+ const persist = (scope) => {
672
+ if (scope)
673
+ matchKey = lifecycleMatchKey(lifecycleFacts(scope.command), expectedRoot, scope);
674
+ 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 });
675
+ const payload = {
676
+ ...hook, session_id: providerSessionId, agent_id: agentId, cwd: observedCwd, tool_name: toolName ?? "Bash", tool_input: rawInput,
677
+ 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)
678
+ };
679
+ const inserted = recordHookEvent(context, {
680
+ projectId: project.id, protocolId: null, harness: "grok-acp", providerSessionId, parentSessionId,
681
+ daemonId: daemonId ?? null, sessionId, agentId: agentId ?? null, turnId: stringValue(hook.turn_id) ?? stringValue(hook.turnId) ?? null,
682
+ eventName: "PreToolUse", toolName: toolName ?? "Bash", status: "observed", payload, eventKey, matchKey, transcriptPath: null, cwd: observedCwd
683
+ });
684
+ recordFlowSessionObservation(context, { projectId: project.id, sessionId, cwd: observedCwd, toolName: toolName ?? "Bash", eventKey });
685
+ return inserted;
557
686
  };
558
- const inserted = recordHookEvent(context, {
559
- projectId: project.id, protocolId: observedSession?.protocol_id ?? null, harness: "grok-acp", providerSessionId, parentSessionId,
560
- daemonId: daemonId ?? null, sessionId, agentId: agentId ?? null, turnId: stringValue(hook.turn_id) ?? stringValue(hook.turnId) ?? null,
561
- eventName: "PreToolUse", toolName: toolName ?? "Bash", status: "observed", payload, eventKey, matchKey, transcriptPath: null, cwd: expectedRoot
562
- });
563
- recordFlowSessionObservation(context, {
564
- projectId: project.id, sessionId, runId: observedSession?.run_id ?? null, protocolId: observedSession?.protocol_id ?? null,
565
- cwd: expectedRoot, toolName: toolName ?? "Bash", eventKey
687
+ const result = (inserted) => ({ ok: true, observed: inserted, duplicate: !inserted, event_key: eventKey, session: nativeSessionIdentity("grok-acp", providerSessionId),
688
+ ...(isChild ? { parent_session: nativeSessionIdentity("grok-acp", stringValue(ddGrok.parentProviderSessionId) ?? rootProviderSessionId) } : {}), daemon_id: daemonId ?? null });
689
+ if (!lifecycle) {
690
+ assignedLifecycleCwd(context, expectedRoot, null, providerSessionId, command, observedCwd ?? undefined);
691
+ return { ...result(context.db.writeTransaction(() => persist())), hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...rawInput, command: commandWithHookEvent(command, eventKey) } } };
692
+ }
693
+ let inserted = false;
694
+ const executionCwd = assignedLifecycleCwd(context, expectedRoot, lifecycle, providerSessionId, command, observedCwd ?? undefined, { daemonId: daemonId ?? "", rootSessionId: rootProviderSessionId, sessionId: providerSessionId, parentSessionId, toolCallId: eventKey }, "grok-acp");
695
+ const receipt = observeLifecycleInvocation(context, {
696
+ currentEventKey: eventKey,
697
+ command, harness: "grok-acp", projectId: project.id,
698
+ ...(executionCwd ? { executionCwd } : {}),
699
+ identity: { daemonId: daemonId ?? "", rootSessionId: rootProviderSessionId, sessionId: providerSessionId, parentSessionId, toolCallId: eventKey },
700
+ recordReceipt: scope => { inserted = persist(scope); return eventKey; }
566
701
  });
567
- const result = { ok: true, observed: inserted, duplicate: !inserted, event_key: eventKey, session: nativeSessionIdentity("grok-acp", providerSessionId),
568
- ...(isChild ? { parent_session: nativeSessionIdentity("grok-acp", stringValue(ddGrok.parentProviderSessionId) ?? rootProviderSessionId) } : {}), daemon_id: daemonId ?? null };
569
- return (flowPayload || stageStart || stageResume || workStart)
570
- ? { ...result, hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...(rawInput ?? {}), command: commandWithHookEvent(command, eventKey) } } }
571
- : result;
702
+ return { ...result(inserted), ...lifecycleObservationResult(receipt), ...(receipt.invocationId ? { invocation_id: receipt.invocationId } : {}), hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...rawInput, command: receipt.eventKey ? commandWithHookEvent(command, receipt.eventKey) : command } } };
572
703
  }
573
704
  /** Convert a controlled OpenCode tool hook into the shared lifecycle receipt. */
574
705
  export function handleOpenCodeEvent(context, input) {
575
706
  const event = parseJsonObject(input.stdin || "{}", "OpenCode tool event");
576
- const agy = event.source_harness === "antigravity-cli";
577
- const harness = agy ? "antigravity-cli" : "opencode-server";
578
707
  if (event.schema_id !== "dd-flow/opencode-tool-event@1")
579
708
  throw new AppError("unsupported_opencode_event_schema", "Unsupported OpenCode event schema", 1);
709
+ return handleControlledToolEvent(context, input, event, event.source_harness === "antigravity-cli" ? "antigravity-cli" : "opencode-server");
710
+ }
711
+ /** Accept adapter-observed Factory identity, never a model-supplied Session ID. */
712
+ export function handleDroidEvent(context, input) {
713
+ const event = parseJsonObject(input.stdin || "{}", "Droid tool event");
714
+ if (event.schema_id !== "dd-flow/droid-tool-event@1")
715
+ throw new AppError("unsupported_droid_event_schema", "Unsupported Droid event schema", 1);
716
+ return handleControlledToolEvent(context, input, event, "droid-cli");
717
+ }
718
+ /** Stable project identity is not the native Session's execution workspace. */
719
+ function requiresAssignedLifecycleCwd(facts) {
720
+ // Settlement and user interaction address the retained RUN explicitly;
721
+ // their tools/checks receive owned paths, not the shell's working directory.
722
+ return !facts || !["stage_finish", "stage_pause", "stage_resume", "work_finish", "work_fail"].includes(facts.invocation.operation);
723
+ }
724
+ function assignedLifecycleCwd(context, projectRoot, facts, providerSessionId, command, observedCwd, identity, harness) {
725
+ const auxiliary = continuationCommand(command);
726
+ if ((!facts && !auxiliary) || facts?.analysis.supported === false)
727
+ return undefined;
728
+ const proven = identity && harness ? managedNativeInvocationScope(context, projectRoot, identity, harness) : null;
729
+ const constraint = proven ?? (identity ? { projectRoot, daemonId: identity.daemonId, rootSessionId: identity.rootSessionId } : undefined);
730
+ const issuedRunId = facts && !facts.runId && !facts.workId && !facts.bootstrapStageStart && constraint ? observedLifecycleInvocation(context, command, constraint, identity ? { identity } : undefined)?.scope.runId : null;
731
+ if (facts?.invocation.operation.startsWith("stage_") && !facts.bootstrapStageStart && !facts.runId && !issuedRunId)
732
+ throw new AppError("invocation_assignment_missing", "Short lifecycle call has no unique issued RUN assignment; its workspace cannot be inferred", 1, { phase: "prepare", effect: "no_effect", recoverable: false });
733
+ if (!requiresAssignedLifecycleCwd(facts))
734
+ return undefined;
735
+ const expected = controlledLifecycleCwd(context, projectRoot, facts, providerSessionId, auxiliary, issuedRunId, observedCwd);
736
+ // Unknown CLI targets have no assigned workspace. Recording their native
737
+ // observation must not turn the stable identity root into cwd authority.
738
+ if (!expected)
739
+ return undefined;
740
+ const route = facts?.invocation.expectedCwd ?? auxiliary?.expectedCwd;
741
+ if (route && resolveProjectRoot(route) !== expected)
742
+ throw new AppError("invocation_directory_mismatch", "Literal lifecycle route differs from the assigned workspace", 1, { expected, actual: route, actual_provenance: "shell_literal_route", phase: "prepare", effect: "no_effect", recoverable: false });
743
+ if (!route && observedCwd && resolveProjectRoot(observedCwd) !== expected)
744
+ throw new AppError("invocation_directory_mismatch", "Native lifecycle directory differs from the assigned workspace", 1, { expected, actual: observedCwd, actual_provenance: "native_tool_cwd", phase: "prepare", effect: "no_effect", recoverable: false });
745
+ // ACP without native cwd still binds assigned authority; CLI confirms the
746
+ // physical process directory, rather than inventing native observation.
747
+ return expected;
748
+ }
749
+ function controlledLifecycleCwd(context, projectRoot, facts, providerSessionId, auxiliary, issuedRunId, observedCwd) {
750
+ if (facts?.bootstrapStageStart)
751
+ return projectRoot;
752
+ const project = context.db.get("SELECT id FROM projects WHERE root = ?", [projectRoot]);
753
+ if (!project)
754
+ return undefined;
755
+ if (facts?.invocation.operation === "merge_apply" || facts?.invocation.operation === "merge_repair") {
756
+ const requestId = commandPosition(facts.invocation, 0);
757
+ const request = requestId && context.db.get("SELECT target_workspace FROM merge_requests WHERE project_id = ? AND merge_request_id = ?", [project.id, requestId]);
758
+ if (request)
759
+ return resolveProjectRoot(request.target_workspace);
760
+ }
761
+ let work;
762
+ if (facts?.workId) {
763
+ try {
764
+ work = resolveWorkReference(context, facts.workId, { projectId: project.id });
765
+ }
766
+ catch (error) {
767
+ if (!(error instanceof AppError) || !["not_found", "ambiguous_work_alias"].includes(error.code))
768
+ throw error;
769
+ }
770
+ if (!work)
771
+ return undefined;
772
+ }
773
+ const reference = work?.run_id ?? facts?.runId ?? auxiliary?.args.options.get("run")?.[0] ?? auxiliary?.args.positional[2] ?? issuedRunId;
774
+ if (!reference)
775
+ return undefined;
776
+ 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]);
777
+ if (!run)
778
+ return undefined; // Hook ingress does not validate a mistyped CLI target.
779
+ if (work) {
780
+ const repair = memoryRepairAssignedHookCwd(context, { projectRoot, projectId: project.id, runId: run.id,
781
+ workId: work.work_id, providerSessionId, ...(observedCwd ? { observedCwd: resolveProjectRoot(observedCwd) } : {}),
782
+ ...(facts?.invocation.expectedCwd ? { literalExecutionCwd: resolveProjectRoot(facts.invocation.expectedCwd) } : {}) });
783
+ if (repair)
784
+ return resolveProjectRoot(repair.root);
785
+ const nativeReview = memoryNativeReviewAssignedHookCwd(context, { projectId: project.id, runId: run.id, workId: work.work_id, providerSessionId });
786
+ if (nativeReview)
787
+ return resolveProjectRoot(nativeReview);
788
+ // PROTOCOLIZE provisions the feature checkout after its coordinator
789
+ // Session has launched in the stable project root. Its transition Work
790
+ // must keep that physical route until the Stage is complete. Child Works
791
+ // share stage membership, not the root coordinator's execution directory.
792
+ if (work.parent_work_id === null && context.db.get("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = 'stage_work_memberships'")
793
+ && 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]))
794
+ return projectRoot;
795
+ const merge = context.db.get("SELECT target_workspace FROM merge_requests WHERE executor_work_id = ? AND run_id = ?", [work.work_id, run.id]);
796
+ if (merge)
797
+ return resolveProjectRoot(merge.target_workspace);
798
+ if (context.db.get("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = 'work_launches'")) {
799
+ 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]);
800
+ if (launch?.provider_session_id === providerSessionId && run.run_root) {
801
+ const copy = JSON.parse(launch.profile_json).review_copy?.root;
802
+ if (copy) {
803
+ const target = path.resolve(run.run_root, copy);
804
+ const relative = path.relative(run.run_root, target);
805
+ if (relative === ".." || relative.startsWith(`..${path.sep}`))
806
+ throw new AppError("read_only_workspace_unbound", "Review copy escapes its owning RUN", 1);
807
+ return resolveProjectRoot(target);
808
+ }
809
+ }
810
+ }
811
+ }
812
+ if (facts?.stageName === "specify" || facts?.stageName === "protocolize")
813
+ return projectRoot;
814
+ if (facts?.stageName === "merge" || facts?.invocation.operation === "merge_apply" || facts?.invocation.operation === "merge_repair") {
815
+ 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]);
816
+ const targets = [...new Set(pending.map(item => resolveProjectRoot(item.target_workspace)))];
817
+ if (targets.length > 1)
818
+ throw new AppError("merge_workspace_ambiguous", "Current MERGE requests disagree on the native execution workspace", 1, { run_id: run.id, target_workspaces: targets });
819
+ if (targets.length === 1)
820
+ return targets[0];
821
+ }
822
+ return resolveProjectRoot(run.workspace_root);
823
+ }
824
+ function handleControlledToolEvent(context, input, event, harness) {
825
+ const agy = harness === "antigravity-cli";
826
+ const droid = harness === "droid-cli";
827
+ const label = droid ? "Droid" : agy ? "Antigravity" : "OpenCode";
828
+ const errorPrefix = droid ? "droid" : agy ? "agy" : "opencode";
580
829
  const phase = stringValue(event.phase);
581
830
  if (phase !== "before" && phase !== "after")
582
- throw new AppError("unsupported_opencode_event_schema", "OpenCode event phase must be before or after", 1);
831
+ throw new AppError(`unsupported_${errorPrefix}_event_schema`, `${label} event phase must be before or after`, 1);
583
832
  const session = objectRecord(event.session);
584
833
  const profile = objectRecord(event.profile);
585
834
  const rawInput = objectRecord(event.input);
@@ -587,16 +836,16 @@ export function handleOpenCodeEvent(context, input) {
587
836
  const nativeParentId = stringValue(session.parent_provider_session_id);
588
837
  const daemonId = stringValue(event.daemon_id);
589
838
  const toolCallId = stringValue(event.tool_call_id);
590
- const eventId = stringValue(event.event_id) ?? toolCallId;
839
+ const eventId = stringValue(event.event_id) ?? (droid ? undefined : toolCallId);
591
840
  const toolName = stringValue(event.tool);
592
841
  const directory = stringValue(session.directory);
593
- if (!providerSessionId || !daemonId || !toolCallId || !eventId || !toolName || !directory || !path.isAbsolute(directory)) {
594
- throw new AppError("opencode_session_identity_invalid", "OpenCode event is missing trusted physical identity", 1);
842
+ if (!providerSessionId || !daemonId || (!droid && !toolCallId) || !eventId || !toolName || !directory || !path.isAbsolute(directory)) {
843
+ throw new AppError(`${errorPrefix}_session_identity_invalid`, `${label} event is missing trusted physical identity`, 1);
595
844
  }
596
845
  const expectedRoot = resolveProjectRoot(input.projectRoot);
597
- if (resolveProjectRoot(directory) !== expectedRoot) {
598
- throw new AppError(agy ? "agy_directory_mismatch" : "opencode_directory_mismatch", `${agy ? "Antigravity" : "OpenCode"} Session directory does not match the controlled workspace`, 1, { expected: expectedRoot, actual: directory });
599
- }
846
+ const observedCwd = resolveProjectRoot(directory);
847
+ if (droid && nativeParentId === providerSessionId)
848
+ throw new AppError("droid_session_identity_invalid", "A Droid Session cannot parent itself", 1);
600
849
  const sessionId = storageSessionId(nativeSessionIdentity(harness, providerSessionId));
601
850
  const parentSessionId = nativeParentId ? storageSessionId(nativeSessionIdentity(harness, nativeParentId)) : null;
602
851
  const command = stringValue(rawInput.command) ?? stringValue(rawInput.cmd) ?? stringValue(rawInput.CommandLine);
@@ -606,67 +855,125 @@ export function handleOpenCodeEvent(context, input) {
606
855
  const inserted = recordHookEvent(context, {
607
856
  projectId: project.id, protocolId: null, harness, providerSessionId, parentSessionId,
608
857
  daemonId, sessionId, agentId: stringValue(session.agent_id) ?? null, turnId: stringValue(event.message_id) ?? null,
609
- eventName: "PostToolUse", toolName, status: "observed", payload: { session_id: providerSessionId, cwd: expectedRoot, tool_name: toolName, status: objectRecord(event.outcome).status },
610
- eventKey: baseKey, matchKey: null, transcriptPath: null, cwd: expectedRoot
858
+ eventName: "PostToolUse", toolName, status: "observed", payload: { session_id: providerSessionId, cwd: observedCwd, tool_name: toolName, status: objectRecord(event.outcome).status },
859
+ eventKey: baseKey, matchKey: null, transcriptPath: droid ? stringValue(event.transcript_path) ?? null : null, cwd: observedCwd
611
860
  });
612
861
  return { ok: true, observed: inserted, duplicate: !inserted, event_key: baseKey, phase, harness, session: nativeSessionIdentity(harness, providerSessionId), ...(nativeParentId ? { parent_session: nativeSessionIdentity(harness, nativeParentId) } : {}) };
613
862
  }
614
- if (!command || !["bash", "Bash", "run_command", "run_terminal_command", "RunTerminalCommand"].includes(toolName)) {
863
+ if (!command || !(droid ? ["Execute"] : ["bash", "Bash", "shell", "run_command", "run_terminal_command", "RunTerminalCommand"]).includes(toolName)) {
615
864
  return { ok: true, observed: false, reason: command ? "non_bash_tool" : "event_not_participating" };
616
865
  }
617
866
  const lifecycle = lifecycleFacts(command);
618
- if (!lifecycle)
867
+ const auxiliary = continuationCommand(command);
868
+ if (!lifecycle && !auxiliary)
619
869
  return { ok: true, observed: false, reason: "event_not_participating" };
620
- if (lifecycle.analysis.kind === "compound") {
621
- throw new AppError("compound_lifecycle_command", `dd-flow lifecycle commands must be a standalone ${agy ? "Antigravity" : "OpenCode"} shell tool call`, 1, { standalone_command: lifecycle.invocation.command });
870
+ // Syntax refusal precedes assignment/cwd lookup: an unsupported AST cannot
871
+ // supply a route or execution authority, including a short public Stage call.
872
+ const shellRefusal = lifecycle?.analysis.supported === false;
873
+ const nativeIdentity = { daemonId, rootSessionId: nativeParentId ?? providerSessionId, sessionId: providerSessionId, parentSessionId: nativeParentId ?? null, toolCallId: toolCallId };
874
+ const provenScope = lifecycle && !shellRefusal ? managedNativeInvocationScope(context, expectedRoot, nativeIdentity, harness) : null;
875
+ const issuedRunId = lifecycle && !lifecycle.runId && !lifecycle.workId && !lifecycle.bootstrapStageStart
876
+ ? observedLifecycleInvocation(context, command, provenScope ?? { projectRoot: expectedRoot, daemonId, rootSessionId: nativeIdentity.rootSessionId }, { identity: nativeIdentity })?.scope.runId : null;
877
+ if (!shellRefusal && lifecycle && lifecycle.invocation.operation.startsWith("stage_") && !lifecycle.bootstrapStageStart && !lifecycle.runId && !issuedRunId) {
878
+ throw new AppError("invocation_assignment_missing", "Short lifecycle call has no unique issued RUN assignment; its workspace cannot be inferred", 1, { effect: "no_effect", recoverable: false });
879
+ }
880
+ // These controlled platforms additionally qualify their native launch
881
+ // route against the stable project root when no CLI target is assigned.
882
+ const workspaceBound = requiresAssignedLifecycleCwd(lifecycle);
883
+ const expectedCwd = shellRefusal || !workspaceBound ? observedCwd : controlledLifecycleCwd(context, expectedRoot, lifecycle, providerSessionId, auxiliary, issuedRunId, observedCwd) ?? expectedRoot;
884
+ const executionRoute = shellRefusal || !workspaceBound ? undefined : lifecycle?.invocation.expectedCwd ?? auxiliary?.expectedCwd;
885
+ if (executionRoute && resolveProjectRoot(executionRoute) !== expectedCwd) {
886
+ throw new AppError(`${errorPrefix}_directory_mismatch`, "Literal shell cd route does not match the assigned lifecycle workspace", 1, { expected: expectedCwd, actual: executionRoute, actual_provenance: "shell_literal_route", effect: "no_effect", recoverable: false });
622
887
  }
623
- const { flowPayload, bootstrapStageStart, commandProjectRoot } = lifecycle;
624
- if (commandProjectRoot && resolveProjectRoot(commandProjectRoot) !== expectedRoot) {
625
- throw new AppError("project_mismatch", "OpenCode lifecycle command project root does not match the controlled workspace", 1, { expected: expectedRoot, actual: commandProjectRoot });
888
+ if (workspaceBound && !executionRoute && observedCwd !== expectedCwd) {
889
+ throw new AppError(`${errorPrefix}_directory_mismatch`, `${label} ${agy ? "launch route" : "Session directory"} does not match the assigned execution workspace`, 1, { expected: expectedCwd, actual: observedCwd, effect: "no_effect", recoverable: false, ...(agy ? { actual_provenance: stringValue(event.cwd_provenance) ?? "unknown" } : {}) });
626
890
  }
627
- const matchKey = lifecycleMatchKey(lifecycle, expectedRoot);
891
+ const bootstrapStageStart = !shellRefusal && lifecycle?.bootstrapStageStart;
892
+ let matchKey = lifecycle && !shellRefusal ? lifecycleMatchKey(lifecycle, expectedRoot) : null;
628
893
  if (bootstrapStageStart)
629
894
  registerProject(context, { root: expectedRoot });
630
895
  const project = requireProjectByRoot(context, expectedRoot);
896
+ if (droid) {
897
+ const existing = context.db.get("SELECT harness, provider_session_id, parent_session_id, daemon_id, match_key, tool_name FROM hook_events WHERE project_id = ? AND event_key = ?", [project.id, baseKey]);
898
+ if (existing && (existing.harness !== harness || existing.provider_session_id !== providerSessionId || existing.parent_session_id !== parentSessionId || existing.daemon_id !== daemonId || existing.match_key !== matchKey || existing.tool_name !== toolName)) {
899
+ throw new AppError("droid_event_replay_mismatch", "Droid event ID was already observed with different lifecycle identity", 1);
900
+ }
901
+ }
631
902
  const agentId = stringValue(session.agent_id);
632
- const observedSession = flowPayload ? bindObservedFlowSession(context, project, {
633
- ...flowPayload, harness, provider_session_id: providerSessionId, agent_id: agentId ?? null,
634
- parent_session_id: parentSessionId, provider: stringValue(profile.provider) ?? null, model: stringValue(profile.model) ?? null,
635
- reasoning: stringValue(profile.variant) ?? null, mode: stringValue(profile.agent) ?? null, agent_type: stringValue(profile.agent) ?? null,
636
- metadata: { opencode_version: stringValue(event.server_version) ?? null, api_fingerprint: stringValue(event.api_fingerprint) ?? null, plugin_sha256: stringValue(event.plugin_sha256) ?? null, native_project_id: stringValue(session.project_id) ?? null }
637
- }, sessionId) : undefined;
638
- const payload = {
639
- session_id: providerSessionId, agent_id: agentId, cwd: expectedRoot, tool_name: toolName, tool_input: rawInput, command,
640
- provider: stringValue(profile.provider), model: stringValue(profile.model), reasoning: stringValue(profile.variant),
641
- mode: stringValue(profile.agent), agent_type: stringValue(profile.agent)
903
+ const persist = (scope) => {
904
+ if (scope)
905
+ matchKey = lifecycleMatchKey(lifecycleFacts(scope.command), expectedRoot, scope);
906
+ assertHookEventReplay(context, { projectId: project.id, eventKey: baseKey, harness, providerSessionId, parentSessionId, daemonId, sessionId, turnId: stringValue(event.message_id) ?? null, eventName: "PreToolUse", toolName, matchKey, cwd: observedCwd });
907
+ const payload = {
908
+ session_id: providerSessionId, agent_id: agentId, cwd: observedCwd, tool_name: toolName, tool_input: rawInput, command,
909
+ ...(agy ? { cwd_provenance: stringValue(event.cwd_provenance) ?? "unknown" } : {}),
910
+ ...(executionRoute ? { lifecycle_execution_cwd: expectedCwd, lifecycle_execution_cwd_provenance: "literal_cd_intent_requires_cli_confirmation" } : {}),
911
+ provider: stringValue(profile.provider), model: stringValue(profile.model), reasoning: stringValue(droid ? profile.reasoning : profile.variant),
912
+ mode: stringValue(droid ? profile.mode : profile.agent), agent_type: stringValue(droid ? profile.agent_type : profile.agent)
913
+ };
914
+ const inserted = recordHookEvent(context, {
915
+ projectId: project.id, protocolId: null, harness, providerSessionId, parentSessionId,
916
+ daemonId, sessionId, agentId: agentId ?? null, turnId: stringValue(event.message_id) ?? null, eventName: "PreToolUse", toolName,
917
+ status: "observed", payload, eventKey: baseKey, matchKey, transcriptPath: droid ? stringValue(event.transcript_path) ?? null : null, cwd: observedCwd
918
+ });
919
+ recordFlowSessionObservation(context, { projectId: project.id, sessionId, cwd: observedCwd, toolName, eventKey: baseKey });
920
+ return inserted;
642
921
  };
643
- const inserted = recordHookEvent(context, {
644
- projectId: project.id, protocolId: observedSession?.protocol_id ?? null, harness, providerSessionId, parentSessionId,
645
- daemonId, sessionId, agentId: agentId ?? null, turnId: stringValue(event.message_id) ?? null, eventName: "PreToolUse", toolName,
646
- status: "observed", payload, eventKey: baseKey, matchKey, transcriptPath: null, cwd: expectedRoot
647
- });
648
- recordFlowSessionObservation(context, { projectId: project.id, sessionId, runId: observedSession?.run_id ?? null, protocolId: observedSession?.protocol_id ?? null, cwd: expectedRoot, toolName, eventKey: baseKey });
649
- 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 };
650
- return agy ? result : { ...result, hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...rawInput, command: commandWithHookEvent(command, baseKey) } } };
922
+ 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 });
923
+ if (lifecycle) {
924
+ let inserted = false;
925
+ const receipt = observeLifecycleInvocation(context, {
926
+ currentEventKey: baseKey,
927
+ command, harness, projectId: project.id, ...(!shellRefusal && workspaceBound ? { executionCwd: expectedCwd } : {}), identity: { daemonId, rootSessionId: nativeParentId ?? providerSessionId, sessionId: providerSessionId, parentSessionId: nativeParentId ?? null, toolCallId: toolCallId },
928
+ recordReceipt: scope => { inserted = persist(scope); return baseKey; }
929
+ });
930
+ const admitted = { ...result(inserted), ...lifecycleObservationResult(receipt), ...(receipt.invocationId ? { invocation_id: receipt.invocationId } : {}) };
931
+ return agy || shellRefusal ? admitted : { ...admitted, hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...rawInput, command: receipt.eventKey ? commandWithHookEvent(command, receipt.eventKey) : command } } };
932
+ }
933
+ const inserted = context.db.writeTransaction(() => persist());
934
+ return agy ? result(inserted) : { ...result(inserted), hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", updatedInput: { ...rawInput, command: commandWithHookEvent(command, baseKey) } } };
651
935
  }
652
936
  /** Convert an Antigravity CLI tool hook into the shared lifecycle receipt. */
653
937
  export function handleAgyEvent(context, input) {
654
938
  const event = parseJsonObject(input.stdin || "{}", "Antigravity tool event");
655
939
  if (event.schema_id !== "dd-flow/agy-tool-event@1")
656
940
  throw new AppError("unsupported_agy_event_schema", "Unsupported Antigravity event schema", 1);
657
- const workspacePaths = Array.isArray(event.workspace_paths) ? event.workspace_paths : [];
658
- const directory = workspacePaths.find((value) => typeof value === "string" && path.isAbsolute(value));
941
+ // workspacePaths lists available workspaces; its first element is not a
942
+ // native selected-cwd observation. The daemon's spawn cwd is a separate,
943
+ // launch-bound route, and the list only corroborates its membership.
944
+ const workspacePaths = Array.isArray(event.workspace_paths)
945
+ ? event.workspace_paths.filter((value) => typeof value === "string" && path.isAbsolute(value)) : [];
946
+ const launchCwd = stringValue(event.launch_cwd);
947
+ const qualifiedLaunchCwd = launchCwd && path.isAbsolute(launchCwd) ? launchCwd : undefined;
948
+ const directory = qualifiedLaunchCwd ?? (workspacePaths.length === 1 ? workspacePaths[0] : undefined);
949
+ if (!directory && workspacePaths.length > 1)
950
+ throw new AppError("agy_workspace_route_unproven", "Antigravity did not identify the active route among multiple workspaces", 1, { workspace_paths: workspacePaths });
951
+ if (directory && qualifiedLaunchCwd && workspacePaths.length
952
+ && !workspacePaths.some(value => resolveProjectRoot(value) === resolveProjectRoot(directory))) {
953
+ throw new AppError("agy_workspace_route_unproven", "Antigravity launch cwd is absent from the native workspace list", 1, { launch_cwd: directory, workspace_paths: workspacePaths });
954
+ }
659
955
  const providerSessionId = stringValue(event.conversation_id);
660
956
  const daemonId = stringValue(event.daemon_id);
661
957
  const eventId = stringValue(event.event_id);
662
958
  const toolName = stringValue(event.tool);
663
959
  if (!directory || !providerSessionId || !daemonId || !eventId || !toolName)
664
960
  throw new AppError("agy_session_identity_invalid", "Antigravity event is missing trusted physical identity", 1);
961
+ const rawInput = objectRecord(event.input);
962
+ const command = stringValue(rawInput.command) ?? stringValue(rawInput.cmd) ?? stringValue(rawInput.CommandLine);
963
+ if (command && (lifecycleFacts(command) || continuationCommand(command)) && !qualifiedLaunchCwd) {
964
+ 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 });
965
+ }
966
+ const invalidStep = !Number.isSafeInteger(event.step_index) || Number(event.step_index) < 0;
967
+ const invalidExecution = event.execution_num !== undefined && (!Number.isSafeInteger(event.execution_num) || Number(event.execution_num) < 0);
968
+ if (event.phase === "before" && command && (lifecycleFacts(command) || continuationCommand(command)) && (invalidStep || invalidExecution)) {
969
+ throw new AppError("agy_native_call_identity_missing", "Participating Antigravity lifecycle calls require a native step identity and a valid execution identity when provided", 1, { phase: "prepare", effect: "no_effect", recoverable: false });
970
+ }
665
971
  const translated = {
666
972
  schema_id: "dd-flow/opencode-tool-event@1", source_harness: "antigravity-cli", phase: event.phase, event_id: eventId,
667
973
  daemon_id: daemonId, tool_call_id: eventId, tool: toolName, input: event.input,
668
974
  message_id: event.step_index === undefined ? null : String(event.step_index),
669
975
  session: { provider_session_id: providerSessionId, parent_provider_session_id: event.parent_conversation_id, directory },
976
+ cwd_provenance: qualifiedLaunchCwd ? "launch_bound" : "workspace_paths_singleton",
670
977
  profile: { provider: objectRecord(event.profile).provider, model: objectRecord(event.profile).model, variant: objectRecord(event.profile).reasoning, agent: objectRecord(event.profile).mode },
671
978
  outcome: { status: stringValue(event.error) ? "error" : "completed" }
672
979
  };
@@ -694,15 +1001,25 @@ export function isolatedDdFlowHomeFromHookStdin(stdin) {
694
1001
  * hook contract intentionally narrow: unwrap only that whole-command form,
695
1002
  * then apply the same lifecycle parser to its actual shell payload.
696
1003
  */
697
- function lifecycleCommandFromPayload(payload) {
698
- const command = commandFromPayload(payload);
699
- return command ? unwrapShellCommand(command) : undefined;
700
- }
701
- function commandWithHookEvent(command, eventKey) {
1004
+ /**
1005
+ * Adds the receipt to the command that the shell will execute. Heredoc input
1006
+ * belongs to that command too, but an option appended after its delimiter is a
1007
+ * second shell command rather than an argument, so insert it before `<<`.
1008
+ */
1009
+ export function commandWithHookEvent(command, eventKey) {
702
1010
  const analysis = parseLifecycleCommand(command);
703
- if (analysis.kind !== "standalone" || !analysis.invocation.rewriteSafe || commandHasOption(analysis.invocation, "hook-event-id"))
1011
+ if (analysis.kind === "none" || analysis.supported === false)
704
1012
  return command;
705
- return `${command.trim()} --hook-event-id ${JSON.stringify(eventKey)}`;
1013
+ if (commandHasOption(analysis.invocation, "hook-event-id")) {
1014
+ return command;
1015
+ }
1016
+ const option = `--hook-event-id '${eventKey.replaceAll("'", "'\\''")}'`;
1017
+ if (analysis.invocation.rewriteSafe) {
1018
+ const index = analysis.invocation.sourceRange?.end ?? shellSuffixOffset(command);
1019
+ const suffix = command.slice(index);
1020
+ return `${command.slice(0, index)} ${option}${suffix ? /^\s/.test(suffix) ? suffix : ` ${suffix}` : ""}`;
1021
+ }
1022
+ return command;
706
1023
  }
707
1024
  /**
708
1025
  * Recognise the small shell surface used by generated dd-flow lifecycle
@@ -719,29 +1036,46 @@ export function analyzeLifecycleShellCommand(command, includeSessionRegister = f
719
1036
  }
720
1037
  function lifecycleFacts(command) {
721
1038
  const analysis = parseLifecycleCommand(command);
1039
+ if (analysis.kind === "none" && analysis.unresolved && ["lifecycle", "auxiliary"].includes(analysis.participation))
1040
+ throw new AppError("lifecycle_shell_syntax_invalid", "Known lifecycle execution is unsupported; use a literal issued command", 1, { phase: "prepare", effect: "no_effect", recoverable: true, disposition: "pre_cli_shell_refusal", reason: analysis.reason });
722
1041
  if (analysis.kind === "none")
723
1042
  return null;
724
1043
  const invocation = analysis.invocation;
725
1044
  const stageStart = invocation.operation === "stage_start";
1045
+ const stageFinish = invocation.operation === "stage_finish";
1046
+ const stagePause = invocation.operation === "stage_pause";
726
1047
  const stageResume = invocation.operation === "stage_resume";
727
1048
  const workStart = invocation.operation === "work_start";
1049
+ const workFinish = invocation.operation === "work_finish";
1050
+ const workFail = invocation.operation === "work_fail";
728
1051
  return {
729
1052
  analysis,
730
1053
  invocation,
731
- flowPayload: flowSessionPayloadFromRegisterCommand(invocation),
732
1054
  stageStart,
1055
+ stageFinish,
1056
+ stagePause,
733
1057
  stageResume,
734
- workStart,
1058
+ workStart, workFinish, workFail,
735
1059
  bootstrapStageStart: stageStart && commandHasOption(invocation, "bootstrap"),
736
1060
  commandProjectRoot: commandOption(invocation, "project-root"),
737
- stageName: stageStart || stageResume ? commandOption(invocation, "stage") : undefined,
738
- runId: stageStart || stageResume ? commandPosition(invocation, 0) : undefined,
739
- workId: stageResume ? commandOption(invocation, "work") : workStart ? commandPosition(invocation, 0) : undefined,
1061
+ stageName: stageStart || stageFinish || stagePause || stageResume ? commandOption(invocation, "stage") : undefined,
1062
+ runId: stageStart || stageFinish || stagePause || stageResume ? commandPosition(invocation, 0) : undefined,
1063
+ workId: stagePause || stageResume ? commandOption(invocation, "work") : workStart || workFinish || workFail ? commandPosition(invocation, 0) : undefined,
740
1064
  contextSha256: commandOption(invocation, "context-sha256")
741
1065
  };
742
1066
  }
743
- function lifecycleMatchKey(facts, projectRoot) {
744
- const { invocation, bootstrapStageStart, stageStart, stageResume, workStart, stageName, runId, workId, contextSha256 } = facts;
1067
+ function lifecycleMatchKey(facts, projectRoot, scope) {
1068
+ // Observe the actual argument without validating it. A mistyped project
1069
+ // must not leave a receipt matching a later, corrected non-rewriting call.
1070
+ if (!scope && facts.commandProjectRoot && canonicalPath(facts.commandProjectRoot, false) !== canonicalPath(projectRoot, false))
1071
+ return null;
1072
+ const { invocation, bootstrapStageStart, stageStart, stageFinish, stagePause, stageResume, workStart, workFinish, workFail, stageName, runId, workId, contextSha256 } = facts;
1073
+ const effectiveRoot = scope?.projectRoot ?? projectRoot;
1074
+ const effectiveRunId = scope?.runId ?? runId;
1075
+ if (invocation.operation === "recovery_accept") {
1076
+ const run = commandPosition(invocation, 0), recovery = commandOption(invocation, "recovery-id");
1077
+ return (scope?.runId ?? run) && recovery ? recoveryAcceptMatchKey((scope?.runId ?? run), recovery, effectiveRoot) : null;
1078
+ }
745
1079
  if (bootstrapStageStart)
746
1080
  return bootstrapMatchKey({
747
1081
  projectRoot,
@@ -749,14 +1083,100 @@ function lifecycleMatchKey(facts, projectRoot) {
749
1083
  intakeMode: contextSha256 ? "context" : commandHasOption(invocation, "intake-stdin") ? "stdin" : "file",
750
1084
  ...(contextSha256 ? { contextSha256 } : {})
751
1085
  });
752
- if (stageStart && runId && stageName)
753
- return stageStartMatchKey(runId, stageName, projectRoot, contextSha256);
754
- if (stageResume && runId && stageName && workId)
755
- return stageResumeMatchKey(runId, stageName, workId, projectRoot);
1086
+ if (stageStart && effectiveRunId && stageName)
1087
+ return stageStartMatchKey(effectiveRunId, stageName, effectiveRoot, contextSha256);
1088
+ if (stageFinish && effectiveRunId && stageName)
1089
+ return stageLifecycleMatchKey("stage_finish", effectiveRunId, stageName, effectiveRoot);
1090
+ if (stagePause && effectiveRunId && stageName && workId)
1091
+ return stageLifecycleMatchKey("stage_pause", effectiveRunId, stageName, effectiveRoot, workId);
1092
+ if (stageResume && effectiveRunId && stageName && workId)
1093
+ return stageResumeMatchKey(effectiveRunId, stageName, workId, effectiveRoot);
756
1094
  if (workStart && workId)
757
- return workStartMatchKey(workId, projectRoot);
1095
+ return workStartMatchKey(workId, effectiveRoot, commandOption(invocation, "recovery-id"));
1096
+ if (workFinish && workId)
1097
+ return workLifecycleMatchKey("work_finish", workId, effectiveRoot);
1098
+ if (workFail && workId)
1099
+ return workLifecycleMatchKey("work_fail", workId, effectiveRoot);
758
1100
  return null;
759
1101
  }
1102
+ /** Observe exact immutable command attribution before reading model-supplied
1103
+ * result files. This does not claim or authorize the lifecycle mutation. */
1104
+ export function observeCommandHook(context, command) {
1105
+ const facts = lifecycleFacts(command);
1106
+ if (!facts || facts.analysis.kind !== "standalone" || !facts.commandProjectRoot)
1107
+ return;
1108
+ const project = context.db.get("SELECT id FROM projects WHERE root = ?", [resolveProjectRoot(facts.commandProjectRoot)]);
1109
+ if (!project)
1110
+ return; // Bootstrap has not registered a project yet.
1111
+ const matchKey = lifecycleMatchKey(facts, facts.commandProjectRoot);
1112
+ if (!matchKey)
1113
+ return;
1114
+ let key = commandOption(facts.invocation, "hook-event-id");
1115
+ if (!key && commandOption(facts.invocation, "invocation-id"))
1116
+ return;
1117
+ if (!key) {
1118
+ try {
1119
+ key = findRecentMatchingHookEvent(context, { projectId: project.id, matchKey, errorCode: "trusted_session_binding_required", operation: facts.invocation.operation }).eventKey;
1120
+ }
1121
+ catch {
1122
+ return;
1123
+ } // Dispatch still owns its mandatory admission error.
1124
+ }
1125
+ const event = context.db.get("SELECT id FROM hook_events WHERE project_id = ? AND event_key = ? AND match_key = ? AND status = 'observed'", [project.id, key, matchKey]);
1126
+ if (event)
1127
+ context.observeLifecycleHook?.(project.id, event.id);
1128
+ }
1129
+ export function stageLifecycleMatchKey(operation, runId, stage, projectRoot, workId) {
1130
+ return crypto.createHash("sha256").update(JSON.stringify({ operation, run_id: runId, stage, project_root: resolveProjectRoot(projectRoot), ...(workId ? { work_id: workId } : {}) })).digest("hex");
1131
+ }
1132
+ export function recoveryAcceptMatchKey(runId, recoveryId, projectRoot) {
1133
+ return crypto.createHash("sha256").update(JSON.stringify({ operation: "recovery_accept", run_id: runId, recovery_id: recoveryId, project_root: resolveProjectRoot(projectRoot) })).digest("hex");
1134
+ }
1135
+ export function workLifecycleMatchKey(operation, workId, projectRoot) {
1136
+ return crypto.createHash("sha256").update(JSON.stringify({ operation, work_id: workId, project_root: resolveProjectRoot(projectRoot) })).digest("hex");
1137
+ }
1138
+ function workLifecycleMatchKeys(operation, workId, projectRoot) {
1139
+ return entityReferenceVariants(workId).map((reference) => workLifecycleMatchKey(operation, reference, projectRoot));
1140
+ }
1141
+ function stageLifecycleMatchKeys(operation, runId, stage, projectRoot, workId) {
1142
+ const runReferences = entityReferenceVariants(runId);
1143
+ const workReferences = workId ? entityReferenceVariants(workId) : [undefined];
1144
+ return [...new Set(runReferences.flatMap((runReference) => workReferences.map((workReference) => stageLifecycleMatchKey(operation, runReference, stage, projectRoot, workReference))))];
1145
+ }
1146
+ /** Claims a Work terminal command only after its exact hook receipt has proved
1147
+ * the physical Session that issued it. Work code performs the owner check. */
1148
+ export function claimWorkLifecycleHookEvent(context, input) {
1149
+ const matchKeys = workLifecycleMatchKeys(input.operation, input.workId, input.projectRoot);
1150
+ const eventKey = input.eventKey ?? findRecentMatchingHookEvent(context, {
1151
+ projectId: input.projectId, matchKey: matchKeys, errorCode: "trusted_session_binding_required", operation: input.operation.replace("_", " ")
1152
+ }).eventKey;
1153
+ const event = context.db.get("SELECT id, session_id, match_key, status FROM hook_events WHERE project_id = ? AND event_key = ?", [input.projectId, eventKey]);
1154
+ if (!event?.session_id || event.status !== "observed" || !event.match_key || !matchKeys.includes(event.match_key)) {
1155
+ const reason = !event ? "event_missing" : !event.session_id ? "identity_missing" : event.status !== "observed" ? "event_consumed" : "target_mismatch";
1156
+ throw new AppError("trusted_session_binding_required", `${input.operation.replace("_", " ")} requires one fresh matching PreToolUse hook event`, 1, { work_id: input.workId, event_key: eventKey, reason, observed_status: event?.status ?? null });
1157
+ }
1158
+ const claimed = context.db.run("UPDATE hook_events SET status = 'claimed', claimed_at = ? WHERE id = ? AND status = 'observed'", [context.now(), event.id]);
1159
+ if (claimed.changes !== 1)
1160
+ throw new AppError("trusted_session_binding_required", "matching Work lifecycle hook event was already claimed", 1, { work_id: input.workId });
1161
+ return hookSessionIdentity(context, input.projectId, eventKey);
1162
+ }
1163
+ /** Claims a lifecycle command only after its exact hook receipt has proved the
1164
+ * physical Session that issued it. Stage code performs the Work-owner check. */
1165
+ export function claimStageLifecycleHookEvent(context, input) {
1166
+ const matchKeys = stageLifecycleMatchKeys(input.operation, input.runId, input.stage, input.projectRoot, input.workId);
1167
+ const eventKey = input.eventKey ?? findRecentMatchingHookEvent(context, {
1168
+ projectId: input.projectId, matchKey: matchKeys, errorCode: "trusted_session_binding_required", operation: input.operation.replace("_", " ")
1169
+ }).eventKey;
1170
+ const event = context.db.get("SELECT id, session_id, match_key, status FROM hook_events WHERE project_id = ? AND event_key = ?", [input.projectId, eventKey]);
1171
+ if (!event?.session_id || event.status !== "observed" || !event.match_key || !matchKeys.includes(event.match_key)) {
1172
+ const reason = !event ? "event_missing" : !event.session_id ? "identity_missing" : event.status !== "observed" ? "event_consumed" : "target_mismatch";
1173
+ throw new AppError("trusted_session_binding_required", `${input.operation.replace("_", " ")} requires one fresh matching PreToolUse hook event`, 1, { run_id: input.runId, stage: input.stage, event_key: eventKey, reason, observed_status: event?.status ?? null });
1174
+ }
1175
+ const claimed = context.db.run("UPDATE hook_events SET status = 'claimed', claimed_at = ? WHERE id = ? AND status = 'observed'", [context.now(), event.id]);
1176
+ if (claimed.changes !== 1)
1177
+ throw new AppError("trusted_session_binding_required", "matching lifecycle hook event was already claimed", 1, { run_id: input.runId, stage: input.stage });
1178
+ return hookSessionIdentity(context, input.projectId, eventKey);
1179
+ }
760
1180
  /** Trusted session identity is created by PreToolUse, never supplied by an agent. */
761
1181
  export function sessionIdForHookEvent(context, projectId, eventKey) {
762
1182
  const event = context.db.get("SELECT session_id FROM hook_events WHERE project_id = ? AND event_key = ?", [projectId, eventKey]);
@@ -772,6 +1192,7 @@ export function hookSessionIdentity(context, projectId, eventKey) {
772
1192
  WHERE he.project_id = ? AND he.event_key = ?`, [projectId, eventKey]);
773
1193
  if (!event?.session_id)
774
1194
  throw new AppError("hook_event_not_found", "stage start requires a trusted PreToolUse hook event", 1, { event_key: eventKey });
1195
+ context.observeLifecycleHook?.(projectId, event.id);
775
1196
  return {
776
1197
  hookEventId: event.id,
777
1198
  harness: event.harness,
@@ -808,21 +1229,30 @@ export function claimRecentMatchingHookEvent(context, input) {
808
1229
  /** Finds the trusted event that a stage- or Work-specific claimant will atomically claim. */
809
1230
  export function findRecentMatchingHookEvent(context, input) {
810
1231
  const freshAfter = new Date(Date.parse(context.now()) - 60_000).toISOString();
811
- const find = () => context.db.get(`SELECT id, event_key FROM hook_events
812
- WHERE project_id = ? AND match_key = ? AND status = 'observed' AND created_at >= ?
813
- ORDER BY id DESC LIMIT 1`, [input.projectId, input.matchKey, freshAfter]);
814
- let event = find();
815
- // Some harnesses publish nested-agent tool events concurrently with command
816
- // execution. Give the trusted event a short bounded window to reach SQLite.
1232
+ const matchKeys = Array.isArray(input.matchKey) ? [...new Set(input.matchKey)] : [input.matchKey];
1233
+ const placeholders = matchKeys.map(() => "?").join(", ");
1234
+ const find = () => context.db.all(`SELECT id, event_key FROM hook_events
1235
+ WHERE project_id = ? AND match_key IN (${placeholders}) AND status = 'observed' AND created_at >= ?
1236
+ AND NOT (COALESCE(json_extract(outcome_json, '$.effect'), '') = 'no_effect'
1237
+ AND COALESCE(json_extract(outcome_json, '$.disposition'), '') IN ('correctable', 'fatal'))
1238
+ AND harness IN ('codex-desktop', 'antigravity-cli')
1239
+ AND (? IS NULL OR session_id = ?)
1240
+ ORDER BY id DESC LIMIT 2`, [input.projectId, ...matchKeys, freshAfter, input.sessionId ?? null, input.sessionId ?? null]);
1241
+ let events = find();
1242
+ // Only the qualified non-rewriting transports may correlate the original
1243
+ // command: Codex Desktop and Antigravity. New harnesses fail closed by default.
817
1244
  const deadline = Date.now() + 250;
818
- while (!event?.event_key && Date.now() < deadline) {
1245
+ while (events.length === 0 && Date.now() < deadline) {
819
1246
  Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 10);
820
- event = find();
1247
+ events = find();
821
1248
  }
822
- if (!event?.event_key) {
823
- throw new AppError(input.errorCode, `${input.operation} requires one matching harness lifecycle event. Stay in this Session and retry the exact standalone lifecycle command; do not create a new Session.`, 1);
1249
+ if (events.length === 0 || !events[0]?.event_key) {
1250
+ throw new AppError(input.errorCode, `${input.operation} requires a trusted lifecycle receipt. Check native hook delivery; managed commands are admitted from committed native identity, not from model-supplied invocation IDs or updatedInput.`, 1, { reason: "event_missing" });
824
1251
  }
825
- return { id: event.id, eventKey: event.event_key };
1252
+ if (events.length > 1) {
1253
+ throw new AppError("ambiguous_lifecycle_receipt", `${input.operation} has more than one fresh matching lifecycle event; stop and let the controller reconcile native delivery`, 1, { reason: "multiple_live_events", match_keys: matchKeys, event_keys: events.map((event) => event.event_key) });
1254
+ }
1255
+ return { id: events[0].id, eventKey: events[0].event_key };
826
1256
  }
827
1257
  /** Fallback for Desktop paths that execute the original command after a hook rewrite. */
828
1258
  export function claimBootstrapHookEvent(context, input) {
@@ -845,9 +1275,9 @@ export function bootstrapMatchKey(input) {
845
1275
  })).digest("hex");
846
1276
  }
847
1277
  export function claimWorkStartHookEvent(context, input) {
848
- const expected = new Set([workStartMatchKey(input.workId, input.projectRoot), workStartMatchKey(shortWorkReference(input.workId), input.projectRoot)]);
1278
+ const expected = entityReferenceVariants(input.workId).map((reference) => workStartMatchKey(reference, input.projectRoot, input.recoveryId));
849
1279
  const event = context.db.get("SELECT id, session_id, agent_id, turn_id, transcript_path, model, agent_type, match_key, status FROM hook_events WHERE project_id = ? AND event_key = ?", [input.projectId, input.eventKey]);
850
- if (!event?.session_id || event.status !== "observed" || !event.match_key || !expected.has(event.match_key)) {
1280
+ if (!event?.session_id || event.status !== "observed" || !event.match_key || !expected.includes(event.match_key)) {
851
1281
  throw new AppError("trusted_work_launch_required", "work start requires one fresh matching PreToolUse event", 1, { work_id: input.workId, event_key: input.eventKey });
852
1282
  }
853
1283
  const claimed = context.db.run("UPDATE hook_events SET status = 'claimed', claimed_at = ? WHERE id = ? AND status = 'observed'", [context.now(), event.id]);
@@ -855,15 +1285,16 @@ export function claimWorkStartHookEvent(context, input) {
855
1285
  throw new AppError("trusted_work_launch_required", "matching work-start hook event was already claimed", 1, { work_id: input.workId });
856
1286
  return hookSessionIdentity(context, input.projectId, input.eventKey);
857
1287
  }
858
- function shortWorkReference(id) { return /^WRK-\d{3,}(?:-|$)/.exec(id)?.[0]?.replace(/-$/, "") ?? id; }
859
1288
  /** Claims the exact non-bootstrap stage entry which starts a coordinator Work. */
860
1289
  export function assertStageStartHookEvent(context, input) {
861
- const expected = stageStartMatchKey(input.runId, input.stage, input.projectRoot, input.contextSha256);
1290
+ const expected = entityReferenceVariants(input.runId).map((reference) => stageStartMatchKey(reference, input.stage, input.projectRoot, input.contextSha256));
862
1291
  const event = context.db.get("SELECT session_id, match_key, status FROM hook_events WHERE project_id = ? AND event_key = ?", [input.projectId, input.eventKey]);
863
- if (!event?.session_id || event.status !== "observed" || event.match_key !== expected) {
864
- throw new AppError("trusted_stage_launch_required", "stage start requires one fresh matching PreToolUse event", 1, { run_id: input.runId, stage: input.stage, event_key: input.eventKey, expected_match_key: expected, observed_match_key: event?.match_key ?? null, observed_status: event?.status ?? null });
1292
+ if (!event?.session_id || event.status !== "observed" || !event.match_key || !expected.includes(event.match_key)) {
1293
+ throw new AppError("trusted_stage_launch_required", "stage start requires one fresh matching PreToolUse event", 1, { run_id: input.runId, stage: input.stage, event_key: input.eventKey, expected_match_keys: expected, observed_match_key: event?.match_key ?? null, observed_status: event?.status ?? null });
865
1294
  }
866
- return hookSessionIdentity(context, input.projectId, input.eventKey);
1295
+ const identity = hookSessionIdentity(context, input.projectId, input.eventKey);
1296
+ assertRecoveryHookCurrent(context, input.projectId, input.runId, identity);
1297
+ return identity;
867
1298
  }
868
1299
  export function claimStageStartHookEvent(context, input) {
869
1300
  assertStageStartHookEvent(context, input);
@@ -893,24 +1324,25 @@ export function stageResumeMatchKey(runId, stage, workId, projectRoot) {
893
1324
  }
894
1325
  /** Claims the resume command observed in the Session that received the user answer. */
895
1326
  export function claimStageResumeHookEvent(context, input) {
896
- const matchKey = stageResumeMatchKey(input.runId, input.stage, input.workId, input.projectRoot);
897
- const freshAfter = new Date(Date.parse(context.now()) - 60_000).toISOString();
898
- const event = input.eventKey
899
- ? context.db.get("SELECT id, event_key, session_id, match_key, status FROM hook_events WHERE project_id = ? AND event_key = ?", [input.projectId, input.eventKey])
900
- : context.db.get("SELECT id, event_key, session_id, match_key, status FROM hook_events WHERE project_id = ? AND session_id = ? AND match_key = ? AND status = 'observed' AND created_at >= ? ORDER BY id DESC LIMIT 1", [input.projectId, input.sessionId ?? "", matchKey, freshAfter]);
901
- if (!event?.event_key || !event.session_id || event.match_key !== matchKey || event.status !== "observed") {
1327
+ const matchKeys = entityReferenceVariants(input.runId).flatMap((runReference) => entityReferenceVariants(input.workId).map((workReference) => stageResumeMatchKey(runReference, input.stage, workReference, input.projectRoot)));
1328
+ const eventKey = input.eventKey ?? findRecentMatchingHookEvent(context, { projectId: input.projectId, matchKey: matchKeys, sessionId: input.sessionId ?? "", errorCode: "trusted_stage_resume_required", operation: "stage resume" }).eventKey;
1329
+ const event = context.db.get("SELECT id, event_key, session_id, match_key, status FROM hook_events WHERE project_id = ? AND event_key = ?", [input.projectId, eventKey]);
1330
+ if (!event?.event_key || !event.session_id || !event.match_key || !matchKeys.includes(event.match_key) || event.status !== "observed") {
902
1331
  throw new AppError("trusted_stage_resume_required", "stage resume requires one fresh matching PreToolUse event", 1, { run_id: input.runId, stage: input.stage, work_id: input.workId });
903
1332
  }
1333
+ const identity = hookSessionIdentity(context, input.projectId, event.event_key);
1334
+ assertRecoveryHookCurrent(context, input.projectId, input.runId, identity);
904
1335
  const claimed = context.db.run("UPDATE hook_events SET status = 'claimed', claimed_at = ? WHERE id = ? AND status = 'observed'", [context.now(), event.id]);
905
1336
  if (claimed.changes !== 1)
906
1337
  throw new AppError("trusted_stage_resume_required", "matching stage-resume hook event was already claimed", 1, { run_id: input.runId, stage: input.stage });
907
- return hookSessionIdentity(context, input.projectId, event.event_key);
1338
+ return identity;
908
1339
  }
909
- export function workStartMatchKey(workId, projectRoot) {
1340
+ export function workStartMatchKey(workId, projectRoot, recoveryId) {
910
1341
  return crypto.createHash("sha256").update(JSON.stringify({
911
1342
  operation: "work_start",
912
1343
  work_id: workId,
913
- project_root: resolveProjectRoot(projectRoot)
1344
+ project_root: resolveProjectRoot(projectRoot),
1345
+ ...(recoveryId ? { recovery_id: recoveryId } : {})
914
1346
  })).digest("hex");
915
1347
  }
916
1348
  export function hookStatusForProject(context, projectId) {
@@ -950,6 +1382,8 @@ export function codexHookEventsForProject(context, projectId) {
950
1382
  FROM hook_events WHERE project_id = ? ORDER BY created_at DESC, id DESC LIMIT 20`, [projectId]);
951
1383
  }
952
1384
  function resolveProfileName(profile) {
1385
+ if (profile !== undefined && (typeof profile !== "string" || !profile.trim()))
1386
+ throw new AppError("validation", "--profile must be nonempty text", 2);
953
1387
  return sanitizeProfile(profile ?? defaultProfileName);
954
1388
  }
955
1389
  function sanitizeProfile(value) {
@@ -977,11 +1411,12 @@ function readTomlIfExists(file) {
977
1411
  if (!fs.existsSync(file)) {
978
1412
  return {};
979
1413
  }
1414
+ const { text } = prepareTextFile(file, "Codex config");
980
1415
  try {
981
- return parse(fs.readFileSync(file, "utf8"));
1416
+ return parse(text);
982
1417
  }
983
1418
  catch (error) {
984
- throw new AppError("validation", `Invalid TOML in ${file}: ${String(error)}`, 2);
1419
+ throw new AppError("validation", `Invalid TOML in ${file}`, 2, { file, ...(error instanceof TomlError ? { line: error.line, column: error.column } : {}) });
985
1420
  }
986
1421
  }
987
1422
  function withManagedConfig(config, sourceHome, targetHome) {
@@ -1000,7 +1435,7 @@ function objectTable(value) {
1000
1435
  function createSharedLinks(sourceHome, targetHome) {
1001
1436
  const linked = [];
1002
1437
  const skipped = [];
1003
- for (const entry of sharedEntries) {
1438
+ for (const entry of codexHomeSharedEntries) {
1004
1439
  const source = path.join(sourceHome, entry);
1005
1440
  const target = path.join(targetHome, entry);
1006
1441
  if (!fs.existsSync(source)) {
@@ -1023,7 +1458,7 @@ function createSharedLinks(sourceHome, targetHome) {
1023
1458
  return { linked, skipped };
1024
1459
  }
1025
1460
  function removeSharedSymlinks(targetHome) {
1026
- for (const entry of sharedEntries) {
1461
+ for (const entry of codexHomeSharedEntries) {
1027
1462
  const target = path.join(targetHome, entry);
1028
1463
  if (fs.existsSync(target) && fs.lstatSync(target).isSymbolicLink()) {
1029
1464
  fs.rmSync(target);
@@ -1036,7 +1471,7 @@ function removeDirIfEmpty(dir) {
1036
1471
  }
1037
1472
  }
1038
1473
  function sharedEntryStatus(sourceHome, targetHome) {
1039
- return sharedEntries.map((entry) => {
1474
+ return codexHomeSharedEntries.map((entry) => {
1040
1475
  const source = path.join(sourceHome, entry);
1041
1476
  const target = path.join(targetHome, entry);
1042
1477
  return {
@@ -1057,11 +1492,15 @@ function hookEntry(matcher, command, statusMessage) {
1057
1492
  return { matcher, hooks: [commandHook(command, statusMessage)] };
1058
1493
  }
1059
1494
  function commandHook(command, statusMessage) {
1060
- return { type: "command", command, timeout: 5, statusMessage };
1495
+ // Hook admission is synchronous (the CLI must see a committed receipt), but
1496
+ // the old 5s budget was shorter than a cold CLI/store open under fan-out.
1497
+ // Startup owns migrations; this budget only covers the bounded hot path.
1498
+ return { type: "command", command, timeout: 30, statusMessage };
1061
1499
  }
1062
1500
  function hookCommand(event) {
1063
- const handler = `PATH="\${HOME}/Library/pnpm:/opt/homebrew/bin:/usr/local/bin:\${PATH}" dd-flow codex hook handle --event ${event} --json`;
1064
- return `/bin/bash -c 'log_dir="\${DD_FLOW_HOME:-\${HOME}/.dd-flow}/logs"; mkdir -p "$log_dir"; log="$log_dir/codex-hook.log"; printf "%s event=${event} phase=start pid=%s\\n" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$$" >>"$log"; ${handler} 2> >(tee -a "$log" >&2); status=$?; printf "%s event=${event} phase=finish pid=%s exit=%s\\n" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$$" "$status" >>"$log"; exit "$status"'`;
1501
+ const args = `codex hook handle --event ${event} --json`;
1502
+ const handler = `if [ -n "\${DD_FLOW_BIN:-}" ]; then "$DD_FLOW_BIN" ${args}; else dd-flow ${args}; fi`;
1503
+ return `/bin/bash -c 'export PATH="\${DD_FLOW_HOME:+\${DD_FLOW_HOME}/bin:}\${PATH}:\${HOME}/Library/pnpm:/opt/homebrew/bin:/usr/local/bin"; log_dir="\${DD_FLOW_HOME:-\${HOME}/.dd-flow}/logs"; mkdir -p "$log_dir"; log="$log_dir/codex-hook.log"; printf "%s event=${event} phase=start pid=%s\\n" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$$" >>"$log"; ${handler} 2> >(tee -a "$log" >&2); status=$?; printf "%s event=${event} phase=finish pid=%s exit=%s\\n" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$$" "$status" >>"$log"; exit "$status"'`;
1065
1504
  }
1066
1505
  function resolveHookTarget(target) {
1067
1506
  if (!target || target === "isolated") {
@@ -1236,11 +1675,10 @@ function recordHookInstallation(context, projectId, scope, configPath, installed
1236
1675
  function installationScope(target, profile, hooksPath) {
1237
1676
  return target === "isolated" ? `isolated:${profile}` : `default:${path.dirname(hooksPath)}`;
1238
1677
  }
1239
- function upsertSessionBindingFromPayload(context, project, sessionId, payload) {
1678
+ function upsertSessionBindingFromPayload(context, project, sessionId, payload, transcriptPath) {
1240
1679
  const existing = sessionBinding(context, project.id, sessionId);
1241
1680
  const now = context.now();
1242
1681
  const cwd = stringValue(payload.cwd) ?? existing?.cwd ?? null;
1243
- const transcriptPath = stringValue(payload.transcript_path) ?? existing?.transcript_path ?? locateCodexTranscript(sessionId, context.env.CODEX_HOME);
1244
1682
  context.db.run(`INSERT INTO codex_session_bindings
1245
1683
  (session_id, project_id, protocol_id, handshake_id, cwd, transcript_path, status, created_at, updated_at)
1246
1684
  VALUES (?, ?, NULL, NULL, ?, ?, 'active', ?, ?)
@@ -1294,12 +1732,11 @@ function sessionBinding(context, projectId, sessionId) {
1294
1732
  return context.db.get("SELECT * FROM codex_session_bindings WHERE project_id = ? AND session_id = ?", [projectId, sessionId]);
1295
1733
  }
1296
1734
  function recordHookEvent(context, input) {
1297
- const existing = context.db.get("SELECT id FROM hook_events WHERE project_id = ? AND event_key = ?", [input.projectId, input.eventKey]);
1298
- if (existing)
1735
+ if (assertHookEventReplay(context, input))
1299
1736
  return false;
1300
1737
  context.db.run(`INSERT INTO hook_events
1301
- (project_id, protocol_id, harness, provider_session_id, parent_session_id, daemon_id, session_id, agent_id, turn_id, transcript_path, provider, model, reasoning, mode, agent_type, cwd, event_key, match_key, event_name, tool_name, status, sanitized_summary, created_at)
1302
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
1738
+ (project_id, protocol_id, harness, provider_session_id, parent_session_id, daemon_id, session_id, agent_id, turn_id, transcript_path, provider, model, reasoning, mode, agent_type, cwd, event_key, match_key, event_name, tool_name, status, sanitized_summary, created_at)
1739
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
1303
1740
  input.projectId,
1304
1741
  input.protocolId,
1305
1742
  input.harness,
@@ -1326,6 +1763,23 @@ function recordHookEvent(context, input) {
1326
1763
  ]);
1327
1764
  return true;
1328
1765
  }
1766
+ /** A duplicate delivery is harmless only when all durable identity facts match. */
1767
+ function assertHookEventReplay(context, input) {
1768
+ const existing = context.db.get(`SELECT harness, provider_session_id, parent_session_id, daemon_id, session_id, turn_id, event_name, tool_name, match_key, cwd
1769
+ FROM hook_events WHERE project_id = ? AND event_key = ?`, [input.projectId, input.eventKey]);
1770
+ if (existing) {
1771
+ const observed = {
1772
+ harness: input.harness, provider_session_id: input.providerSessionId, parent_session_id: input.parentSessionId,
1773
+ daemon_id: input.daemonId ?? null, session_id: input.sessionId, turn_id: input.turnId,
1774
+ event_name: input.eventName, tool_name: input.toolName, match_key: input.matchKey, cwd: input.cwd
1775
+ };
1776
+ if (Object.entries(observed).some(([key, value]) => existing[key] !== value)) {
1777
+ throw new AppError("hook_event_replay_mismatch", "Lifecycle event ID was already observed with different immutable identity", 1, { event_key: input.eventKey });
1778
+ }
1779
+ return true;
1780
+ }
1781
+ return false;
1782
+ }
1329
1783
  function projectForHook(context, explicitRoot, cwd) {
1330
1784
  if (explicitRoot)
1331
1785
  return requireProjectByRoot(context, resolveProjectRoot(explicitRoot));
@@ -1352,14 +1806,6 @@ function hookEventKey(payload, eventName, toolName, command) {
1352
1806
  command
1353
1807
  })).digest("hex");
1354
1808
  }
1355
- function commandFromPayload(payload) {
1356
- const direct = stringValue(payload.command);
1357
- if (direct) {
1358
- return direct;
1359
- }
1360
- const toolInput = objectRecord(payload.tool_input);
1361
- return stringValue(toolInput.command) ?? stringValue(toolInput.cmd);
1362
- }
1363
1809
  function toolNameFromPayload(payload) {
1364
1810
  const toolInput = objectRecord(payload.tool_input);
1365
1811
  return stringValue(toolInput.name);
@@ -1402,10 +1848,10 @@ function writeJsonFile(file, value) {
1402
1848
  ensureDir(path.dirname(file));
1403
1849
  fs.writeFileSync(file, `${JSON.stringify(value, null, 2)}\n`);
1404
1850
  }
1405
- function backupFile(file, now) {
1851
+ function backupFile(file, now, text) {
1406
1852
  const suffix = now.replace(/[:.]/g, "-");
1407
1853
  const backup = `${file}.${suffix}.bak`;
1408
- fs.copyFileSync(file, backup);
1854
+ fs.writeFileSync(backup, text, { mode: 0o600 });
1409
1855
  return backup;
1410
1856
  }
1411
1857
  function hashObject(value) {