@deksden-com/dd-flow-cli 0.9.0-beta.15 → 0.9.0-beta.151

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 (560) hide show
  1. package/CHANGELOG.md +907 -0
  2. package/README.md +139 -10
  3. package/dist/build-info.json +8 -8
  4. package/dist/cli/argument-values.js +38 -0
  5. package/dist/cli/command-inputs.js +386 -0
  6. package/dist/cli/command-metadata.js +232 -0
  7. package/dist/cli/command-option-descriptions.js +192 -0
  8. package/dist/cli/completion-cli.js +70 -0
  9. package/dist/cli/completion.js +57 -0
  10. package/dist/cli/diagnostic-invocation.js +46 -0
  11. package/dist/cli/diagnostic.js +120 -0
  12. package/dist/cli/error-output.js +52 -0
  13. package/dist/cli/executor-onboarding.js +76 -0
  14. package/dist/cli/flows.js +29 -0
  15. package/dist/cli/harnesses.js +84 -0
  16. package/dist/cli/help.js +396 -19
  17. package/dist/cli/hook-ingress.js +102 -0
  18. package/dist/cli/input-preparation.js +165 -0
  19. package/dist/cli/memory-flow-admission.js +50 -0
  20. package/dist/cli/memory-init-invocation.js +85 -0
  21. package/dist/cli/memory-init.js +249 -0
  22. package/dist/cli/memory-storage-admission.js +54 -0
  23. package/dist/cli/memory-storage-qualification.js +105 -0
  24. package/dist/cli/memory-storage-transition.js +123 -0
  25. package/dist/cli/memory-upgrade.js +189 -0
  26. package/dist/cli/models.js +39 -0
  27. package/dist/cli/native-hook-ingress.js +90 -0
  28. package/dist/cli/native-hook-preparation.js +55 -0
  29. package/dist/cli/operator-profiles.js +61 -0
  30. package/dist/cli/output-options.js +35 -0
  31. package/dist/cli/response-file.js +72 -0
  32. package/dist/cli/run-cli.js +1734 -550
  33. package/dist/cli/runtime-maintenance-diagnostics.js +46 -0
  34. package/dist/cli/runtime-maintenance-ingress.js +52 -0
  35. package/dist/cli/runtime-process.js +153 -0
  36. package/dist/cli/setup.js +150 -0
  37. package/dist/cli.js +89 -2
  38. package/dist/domain/diagnostic-aspects.js +103 -0
  39. package/dist/domain/diagnostic-flows.js +8 -0
  40. package/dist/domain/stage-catalog.js +1 -1
  41. package/dist/domain/stage-handler-identities.js +28 -0
  42. package/dist/harness-runtime/DD-ZCODE.md +112 -0
  43. package/dist/harness-runtime/OPENCODE-LICENSE.txt +24 -0
  44. package/dist/harness-runtime/bin/dd-agy.mjs +56 -0
  45. package/dist/harness-runtime/bin/dd-codex.mjs +27 -0
  46. package/dist/harness-runtime/bin/dd-droid.mjs +34 -0
  47. package/dist/harness-runtime/bin/dd-grok.mjs +40 -0
  48. package/dist/harness-runtime/bin/dd-opencode.mjs +22 -0
  49. package/dist/harness-runtime/bin/dd-zcode.mjs +87 -0
  50. package/dist/harness-runtime/lib/adapter-timeouts.d.mts +5 -0
  51. package/dist/harness-runtime/lib/adapter-timeouts.mjs +34 -0
  52. package/dist/harness-runtime/lib/agy-error-boundary.mjs +79 -0
  53. package/dist/harness-runtime/lib/agy-hooks.d.mts +1 -0
  54. package/dist/harness-runtime/lib/agy-hooks.mjs +9 -0
  55. package/dist/harness-runtime/lib/agy-native-inspection.mjs +216 -0
  56. package/dist/harness-runtime/lib/agy-native_pb.js +117 -0
  57. package/dist/harness-runtime/lib/agy-terminal-envelope.mjs +67 -0
  58. package/dist/harness-runtime/lib/daemon-observation.d.mts +2 -0
  59. package/dist/harness-runtime/lib/daemon-observation.mjs +163 -0
  60. package/dist/harness-runtime/lib/daemon-operations.d.mts +2 -0
  61. package/dist/harness-runtime/lib/daemon-operations.mjs +462 -0
  62. package/dist/harness-runtime/lib/dd-agy-daemon.d.mts +6 -0
  63. package/dist/harness-runtime/lib/dd-agy-daemon.mjs +1092 -0
  64. package/dist/harness-runtime/lib/dd-agy.mjs +86 -0
  65. package/dist/harness-runtime/lib/dd-codex-daemon.d.mts +1 -0
  66. package/dist/harness-runtime/lib/dd-codex-daemon.mjs +231 -0
  67. package/dist/harness-runtime/lib/dd-codex.d.mts +1 -0
  68. package/dist/harness-runtime/lib/dd-codex.mjs +954 -0
  69. package/dist/harness-runtime/lib/dd-droid-daemon.mjs +141 -0
  70. package/dist/harness-runtime/lib/dd-droid.mjs +626 -0
  71. package/dist/harness-runtime/lib/dd-grok-daemon.mjs +555 -0
  72. package/dist/harness-runtime/lib/dd-grok.mjs +180 -0
  73. package/dist/harness-runtime/lib/dd-opencode-daemon.mjs +627 -0
  74. package/dist/harness-runtime/lib/dd-opencode.mjs +390 -0
  75. package/dist/harness-runtime/lib/dd-zcode-daemon.mjs +752 -0
  76. package/dist/harness-runtime/lib/dd-zcode.mjs +1200 -0
  77. package/dist/harness-runtime/lib/delegation-instructions.d.mts +14 -0
  78. package/dist/harness-runtime/lib/delegation-instructions.mjs +166 -0
  79. package/dist/harness-runtime/lib/dispatch-fence.mjs +14 -0
  80. package/dist/harness-runtime/lib/driver-recovery.mjs +140 -0
  81. package/dist/harness-runtime/lib/droid-observation.mjs +66 -0
  82. package/dist/harness-runtime/lib/entity-references.d.mts +2 -0
  83. package/dist/harness-runtime/lib/entity-references.mjs +9 -0
  84. package/dist/harness-runtime/lib/lease-renewal.d.mts +8 -0
  85. package/dist/harness-runtime/lib/lease-renewal.mjs +37 -0
  86. package/dist/harness-runtime/lib/lifecycle-command.d.mts +20 -0
  87. package/dist/harness-runtime/lib/lifecycle-command.mjs +273 -0
  88. package/dist/harness-runtime/lib/lifecycle-contract.d.mts +10 -0
  89. package/dist/harness-runtime/lib/lifecycle-contract.mjs +19 -0
  90. package/dist/harness-runtime/lib/managed-daemon.d.mts +16 -0
  91. package/dist/harness-runtime/lib/managed-daemon.mjs +698 -0
  92. package/dist/harness-runtime/lib/managed-flow-env.mjs +15 -0
  93. package/dist/harness-runtime/lib/model-observations.mjs +113 -0
  94. package/dist/harness-runtime/lib/native-children.d.mts +34 -0
  95. package/dist/harness-runtime/lib/native-children.mjs +265 -0
  96. package/dist/harness-runtime/lib/native-hook-command.d.mts +7 -0
  97. package/dist/harness-runtime/lib/native-hook-command.mjs +251 -0
  98. package/dist/harness-runtime/lib/native-request-proof.d.mts +2 -0
  99. package/dist/harness-runtime/lib/native-request-proof.mjs +65 -0
  100. package/dist/harness-runtime/lib/native-streams.mjs +45 -0
  101. package/dist/harness-runtime/lib/observation-clock.d.mts +18 -0
  102. package/dist/harness-runtime/lib/observation-clock.mjs +95 -0
  103. package/dist/harness-runtime/lib/opencode-plugin.mjs +85 -0
  104. package/dist/harness-runtime/lib/operation-context.mjs +5 -0
  105. package/dist/harness-runtime/lib/operation-errors.d.mts +16 -0
  106. package/dist/harness-runtime/lib/operation-errors.mjs +127 -0
  107. package/dist/harness-runtime/lib/operation-progress.d.mts +7 -0
  108. package/dist/harness-runtime/lib/operation-progress.mjs +114 -0
  109. package/dist/harness-runtime/lib/process-json.d.mts +5 -0
  110. package/dist/harness-runtime/lib/process-json.mjs +201 -0
  111. package/dist/harness-runtime/lib/process-snapshot.mjs +10 -0
  112. package/dist/harness-runtime/lib/provider-overload-policy.d.mts +23 -0
  113. package/dist/harness-runtime/lib/provider-overload-policy.mjs +242 -0
  114. package/dist/harness-runtime/lib/runner-events.mjs +202 -0
  115. package/dist/harness-runtime/lib/runner-lock.mjs +42 -0
  116. package/dist/harness-runtime/lib/session-settlement.mjs +30 -0
  117. package/dist/harness-runtime/lib/setup-progress.d.mts +9 -0
  118. package/dist/harness-runtime/lib/setup-progress.mjs +53 -0
  119. package/dist/harness-runtime/lib/tool-observations.d.mts +21 -0
  120. package/dist/harness-runtime/lib/tool-observations.mjs +228 -0
  121. package/dist/harness-runtime/lib/zcode-usage.mjs +55 -0
  122. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-1y4a697q.js +81 -0
  123. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-2ny4vxct.js +92 -0
  124. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-6mptkzbc.js +0 -0
  125. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-7x67cf1q.js +52 -0
  126. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-9rqn6x4v.js +16 -0
  127. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-9vdz0zwn.js +1661 -0
  128. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-ecs5ftka.js +1466 -0
  129. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-frbwqjmf.js +145 -0
  130. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-gne7h7mq.js +50 -0
  131. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-gx9wnkzr.js +105 -0
  132. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-m2eywc8w.js +0 -0
  133. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-nwcpakzx.js +12 -0
  134. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-q4g3afnw.js +623 -0
  135. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-qav6mfdz.js +0 -0
  136. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-sxt88q69.js +12 -0
  137. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-t1xa31vm.js +39 -0
  138. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-ve4aws36.js +103 -0
  139. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-vmytr4q8.js +4 -0
  140. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-vt9jbch8.js +36 -0
  141. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-vyas3vgt.js +0 -0
  142. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-wjyeq25y.js +30 -0
  143. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-xgnj2w4v.js +20 -0
  144. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-y2etg5bs.js +9 -0
  145. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-ydhz1zrh.js +238 -0
  146. package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-z6qdwcsf.js +62 -0
  147. package/dist/harness-runtime/vendor/opencode-client/contract.d.ts +1 -0
  148. package/dist/harness-runtime/vendor/opencode-client/contract.js +12 -0
  149. package/dist/harness-runtime/vendor/opencode-client/effect/api/api.d.ts +2806 -0
  150. package/dist/harness-runtime/vendor/opencode-client/effect/api/api.js +0 -0
  151. package/dist/harness-runtime/vendor/opencode-client/effect/api.d.ts +4 -0
  152. package/dist/harness-runtime/vendor/opencode-client/effect/api.js +0 -0
  153. package/dist/harness-runtime/vendor/opencode-client/effect/client.d.ts +2530 -0
  154. package/dist/harness-runtime/vendor/opencode-client/effect/client.js +15 -0
  155. package/dist/harness-runtime/vendor/opencode-client/effect/generated/client-error.d.ts +7 -0
  156. package/dist/harness-runtime/vendor/opencode-client/effect/generated/client-error.js +6 -0
  157. package/dist/harness-runtime/vendor/opencode-client/effect/generated/client.d.ts +2529 -0
  158. package/dist/harness-runtime/vendor/opencode-client/effect/generated/client.js +8 -0
  159. package/dist/harness-runtime/vendor/opencode-client/effect/generated/index.d.ts +2 -0
  160. package/dist/harness-runtime/vendor/opencode-client/effect/generated/index.js +12 -0
  161. package/dist/harness-runtime/vendor/opencode-client/effect/index.d.ts +35 -0
  162. package/dist/harness-runtime/vendor/opencode-client/effect/index.js +75 -0
  163. package/dist/harness-runtime/vendor/opencode-client/effect/rpc.d.ts +20 -0
  164. package/dist/harness-runtime/vendor/opencode-client/effect/rpc.js +11 -0
  165. package/dist/harness-runtime/vendor/opencode-client/effect/service.d.ts +90 -0
  166. package/dist/harness-runtime/vendor/opencode-client/effect/service.js +187 -0
  167. package/dist/harness-runtime/vendor/opencode-client/package.json +84 -0
  168. package/dist/harness-runtime/vendor/opencode-client/promise/api.d.ts +21 -0
  169. package/dist/harness-runtime/vendor/opencode-client/promise/api.js +0 -0
  170. package/dist/harness-runtime/vendor/opencode-client/promise/client.d.ts +252 -0
  171. package/dist/harness-runtime/vendor/opencode-client/promise/client.js +14 -0
  172. package/dist/harness-runtime/vendor/opencode-client/promise/generated/client-error.d.ts +8 -0
  173. package/dist/harness-runtime/vendor/opencode-client/promise/generated/client-error.js +6 -0
  174. package/dist/harness-runtime/vendor/opencode-client/promise/generated/client.d.ts +259 -0
  175. package/dist/harness-runtime/vendor/opencode-client/promise/generated/client.js +7 -0
  176. package/dist/harness-runtime/vendor/opencode-client/promise/generated/index.d.ts +3 -0
  177. package/dist/harness-runtime/vendor/opencode-client/promise/generated/index.js +79 -0
  178. package/dist/harness-runtime/vendor/opencode-client/promise/generated/types.d.ts +8932 -0
  179. package/dist/harness-runtime/vendor/opencode-client/promise/generated/types.js +70 -0
  180. package/dist/harness-runtime/vendor/opencode-client/promise/index.d.ts +6 -0
  181. package/dist/harness-runtime/vendor/opencode-client/promise/index.js +83 -0
  182. package/dist/harness-runtime/vendor/opencode-client/promise/rpc.d.ts +32 -0
  183. package/dist/harness-runtime/vendor/opencode-client/promise/rpc.js +7 -0
  184. package/dist/harness-runtime/vendor/opencode-client/promise/service.d.ts +24 -0
  185. package/dist/harness-runtime/vendor/opencode-client/promise/service.js +187 -0
  186. package/dist/harness-runtime/vendor/opencode-client/pty-handoff.d.ts +12 -0
  187. package/dist/harness-runtime/vendor/opencode-client/pty-handoff.js +14 -0
  188. package/dist/harness-runtime/vendor/opencode-client/rpc-runtime.d.ts +20 -0
  189. package/dist/harness-runtime/vendor/opencode-client/rpc-runtime.js +14 -0
  190. package/dist/harness-runtime/vendor/opencode-client/service-contender.d.ts +37 -0
  191. package/dist/harness-runtime/vendor/opencode-client/service-contender.js +10 -0
  192. package/dist/harness-runtime/vendor/opencode-client/service-probe.d.ts +70 -0
  193. package/dist/harness-runtime/vendor/opencode-client/service-probe.js +16 -0
  194. package/dist/harness-runtime/vendor/opencode-client/service-timing.d.ts +12 -0
  195. package/dist/harness-runtime/vendor/opencode-client/service-timing.js +10 -0
  196. package/dist/harness-runtime/vendor/opencode-client/service-version.d.ts +2 -0
  197. package/dist/harness-runtime/vendor/opencode-client/service-version.js +6 -0
  198. package/dist/harness-runtime/vendor/opencode-client/service.d.ts +52 -0
  199. package/dist/harness-runtime/vendor/opencode-client/service.js +1 -0
  200. package/dist/harness-runtime/vendor/opencode-client/shared-events.d.ts +11 -0
  201. package/dist/harness-runtime/vendor/opencode-client/shared-events.js +8 -0
  202. package/dist/harness-runtime/vendor/opencode-client/solid/connection.d.ts +37 -0
  203. package/dist/harness-runtime/vendor/opencode-client/solid/connection.js +10 -0
  204. package/dist/harness-runtime/vendor/opencode-client/solid/data.d.ts +211 -0
  205. package/dist/harness-runtime/vendor/opencode-client/solid/data.js +18 -0
  206. package/dist/harness-runtime/vendor/opencode-client/solid/index.d.ts +3 -0
  207. package/dist/harness-runtime/vendor/opencode-client/solid/index.js +32 -0
  208. package/dist/harness-runtime/vendor/opencode-client/solid/pty.d.ts +22 -0
  209. package/dist/harness-runtime/vendor/opencode-client/solid/pty.js +8 -0
  210. package/dist/harness-runtime/vendor/opencode-plugin.mjs +3 -0
  211. package/dist/protocol/local-files.js +18 -9
  212. package/dist/runtime/context.js +8 -4
  213. package/dist/schemas/agent-profile.schema.json +1 -1
  214. package/dist/schemas/code-review-decision.schema.json +9 -0
  215. package/dist/schemas/code-review-result.schema.json +1 -1
  216. package/dist/schemas/code-verification.schema.json +7 -5
  217. package/dist/schemas/code-work-batch.schema.json +2 -2
  218. package/dist/schemas/diagnostic-stage-result.schema.json +354 -0
  219. package/dist/schemas/diagnostic-work-result.schema.json +296 -0
  220. package/dist/schemas/discovery-result.schema.json +184 -0
  221. package/dist/schemas/execution-bundle.schema.json +211 -0
  222. package/dist/schemas/execution-settings.schema.json +162 -0
  223. package/dist/schemas/flow-descriptor.schema.json +54 -0
  224. package/dist/schemas/flow-run.schema.json +29 -0
  225. package/dist/schemas/harness-catalog.schema.json +80 -0
  226. package/dist/schemas/mb-init-compose-result.schema.json +457 -0
  227. package/dist/schemas/mb-init-discover-result.schema.json +500 -0
  228. package/dist/schemas/mb-init-finish-result.schema.json +422 -0
  229. package/dist/schemas/mb-init-preflight-result.schema.json +367 -0
  230. package/dist/schemas/mb-init-review-result.schema.json +396 -0
  231. package/dist/schemas/mb-init-verify-result.schema.json +391 -0
  232. package/dist/schemas/mb-upgrade-stage-result.schema.json +863 -0
  233. package/dist/schemas/memory-bootstrap-receipt-v2.schema.json +67 -0
  234. package/dist/schemas/memory-bootstrap-receipt.schema.json +354 -0
  235. package/dist/schemas/memory-composition-work-result.schema.json +38 -0
  236. package/dist/schemas/memory-repair-work-result.schema.json +56 -0
  237. package/dist/schemas/memory-review-correction-plan.schema.json +30 -0
  238. package/dist/schemas/memory-review-work-result.schema.json +43 -0
  239. package/dist/schemas/model-catalog.schema.json +153 -0
  240. package/dist/schemas/plan-review-decision.schema.json +12 -3
  241. package/dist/schemas/plan-review-result.schema.json +2 -2
  242. package/dist/schemas/profile-settings.schema.json +122 -0
  243. package/dist/schemas/project-flow-pack-manifest.schema.json +1 -1
  244. package/dist/schemas/release-impact.schema.json +3 -3
  245. package/dist/schemas/release-manifest.schema.json +291 -0
  246. package/dist/schemas/run-control-receipt.schema.json +118 -0
  247. package/dist/schemas/run-control-request.schema.json +36 -0
  248. package/dist/schemas/run-engine-binding.schema.json +1 -1
  249. package/dist/schemas/stage-start-response.schema.json +4 -0
  250. package/dist/schemas/vnext-protocol-plan.schema.json +2 -2
  251. package/dist/services/accepted-intent.js +139 -0
  252. package/dist/services/branch-context.js +3 -3
  253. package/dist/services/canon.js +54 -15
  254. package/dist/services/check-environment.js +21 -0
  255. package/dist/services/check-runner-evidence.js +277 -0
  256. package/dist/services/check-runner-proof.js +305 -0
  257. package/dist/services/cleanup.js +133 -40
  258. package/dist/services/cli-operation-classifier.js +26 -63
  259. package/dist/services/cli-upgrade-allowlist.js +27 -0
  260. package/dist/services/code-checks.js +1401 -119
  261. package/dist/services/codex-hook-delivery.js +32 -0
  262. package/dist/services/command-context.js +80 -0
  263. package/dist/services/compatibility-preflight.js +16 -3
  264. package/dist/services/config.js +11 -8
  265. package/dist/services/continuation-outcome.js +20 -0
  266. package/dist/services/controller-child-capacity.js +71 -0
  267. package/dist/services/controller-child-observer.js +121 -0
  268. package/dist/services/controller-execution.js +94 -0
  269. package/dist/services/controller-fanout.js +226 -0
  270. package/dist/services/controller-memory-task.js +45 -0
  271. package/dist/services/controller-memory-worker.js +49 -0
  272. package/dist/services/dashboard.js +143 -58
  273. package/dist/services/descriptor-coordinator-session.js +35 -0
  274. package/dist/services/descriptor-root-work.js +53 -0
  275. package/dist/services/descriptor-run-settlement.js +107 -0
  276. package/dist/services/descriptor-run-start.js +475 -0
  277. package/dist/services/descriptor-stage-coordinator.js +250 -0
  278. package/dist/services/descriptor-stage-settlement.js +223 -0
  279. package/dist/services/descriptor-stage-start.js +175 -0
  280. package/dist/services/diagnostic-evidence.js +73 -0
  281. package/dist/services/diagnostic-report-publication.js +72 -0
  282. package/dist/services/diagnostic-report.js +67 -0
  283. package/dist/services/diagnostic-run-bundle.js +74 -0
  284. package/dist/services/diagnostic-snapshot.js +159 -0
  285. package/dist/services/diagnostic-stage-handlers.js +108 -0
  286. package/dist/services/diagnostic-stage-results.js +132 -0
  287. package/dist/services/diagnostic-start.js +84 -0
  288. package/dist/services/diagnostic-work-batch.js +34 -0
  289. package/dist/services/diagnostic-work-collection.js +97 -0
  290. package/dist/services/diagnostic-work-registration.js +101 -0
  291. package/dist/services/diagnostic-work-results.js +93 -0
  292. package/dist/services/discovery/agy-discovery.js +2 -0
  293. package/dist/services/discovery/codex-discovery.js +2 -0
  294. package/dist/services/discovery/droid-discovery.js +2 -0
  295. package/dist/services/discovery/grok-discovery.js +2 -0
  296. package/dist/services/discovery/opencode-discovery.js +2 -0
  297. package/dist/services/discovery/static.js +31 -0
  298. package/dist/services/discovery/zcode-discovery.js +2 -0
  299. package/dist/services/engines.js +354 -163
  300. package/dist/services/eval-snapshots.js +1316 -150
  301. package/dist/services/execution-bundles.js +275 -0
  302. package/dist/services/execution-policy.js +185 -0
  303. package/dist/services/executor-onboarding.js +56 -0
  304. package/dist/services/external-work-launch.js +310 -0
  305. package/dist/services/flow-catalog.js +30 -0
  306. package/dist/services/flow-descriptors.js +82 -0
  307. package/dist/services/frozen-engine.js +387 -0
  308. package/dist/services/generic-execution.js +164 -0
  309. package/dist/services/harness-adapter.js +355 -36
  310. package/dist/services/harness-config.js +2 -0
  311. package/dist/services/harness-discovery.js +29 -0
  312. package/dist/services/harness-doctor.js +75 -0
  313. package/dist/services/harness-probe-effects.js +51 -0
  314. package/dist/services/harness-version-probe.js +100 -0
  315. package/dist/services/hook-memory-routing.js +188 -0
  316. package/dist/services/hook-recovery-guard.js +14 -0
  317. package/dist/services/hook-session-observation.js +24 -0
  318. package/dist/services/hooks.js +791 -326
  319. package/dist/services/hybrid-contracts.js +474 -0
  320. package/dist/services/hybrid-digest.js +14 -0
  321. package/dist/services/ids.js +29 -1
  322. package/dist/services/installation-lock.js +95 -0
  323. package/dist/services/installed-flow-source.js +226 -0
  324. package/dist/services/lanes.js +78 -65
  325. package/dist/services/lifecycle-command.js +1 -288
  326. package/dist/services/lifecycle-contract.js +1 -0
  327. package/dist/services/lifecycle-invocations.js +2148 -0
  328. package/dist/services/managed-daemon-binding.js +38 -0
  329. package/dist/services/managed-lease-monitor.js +133 -0
  330. package/dist/services/managed-processes.js +449 -69
  331. package/dist/services/memory-accepted-composition.js +78 -0
  332. package/dist/services/memory-baseline-preservation.js +65 -0
  333. package/dist/services/memory-completed-correction-wave.js +328 -0
  334. package/dist/services/memory-compose-handler.js +400 -0
  335. package/dist/services/memory-composition-acceptance.js +375 -0
  336. package/dist/services/memory-composition-graph.js +165 -0
  337. package/dist/services/memory-composition-registration.js +161 -0
  338. package/dist/services/memory-composition-work-custody.js +156 -0
  339. package/dist/services/memory-composition-work-results.js +108 -0
  340. package/dist/services/memory-corrected-review-acceptance.js +129 -0
  341. package/dist/services/memory-corrected-review-batch.js +37 -0
  342. package/dist/services/memory-corrected-review-findings.js +108 -0
  343. package/dist/services/memory-corrected-review-inputs.js +215 -0
  344. package/dist/services/memory-corrected-review-projection.js +78 -0
  345. package/dist/services/memory-corrected-review-receipt.js +178 -0
  346. package/dist/services/memory-corrected-review-registration.js +192 -0
  347. package/dist/services/memory-correction-check-receipt.js +235 -0
  348. package/dist/services/memory-correction-input-owner.js +6 -0
  349. package/dist/services/memory-custody-snapshot.js +36 -0
  350. package/dist/services/memory-discover-handler.js +104 -0
  351. package/dist/services/memory-finish-evidence.js +457 -0
  352. package/dist/services/memory-finish-handler.js +229 -0
  353. package/dist/services/memory-finish-integration.js +450 -0
  354. package/dist/services/memory-finish-readiness.js +169 -0
  355. package/dist/services/memory-git-integration.js +220 -0
  356. package/dist/services/memory-historical-corrected-review.js +212 -0
  357. package/dist/services/memory-historical-original-review.js +143 -0
  358. package/dist/services/memory-historical-repair-registration.js +105 -0
  359. package/dist/services/memory-historical-second-review.js +147 -0
  360. package/dist/services/memory-init-bootstrap-readiness.js +210 -0
  361. package/dist/services/memory-init-contexts.js +131 -0
  362. package/dist/services/memory-init-controller-start.js +103 -0
  363. package/dist/services/memory-init-engine-release.js +223 -0
  364. package/dist/services/memory-init-git-policy.js +165 -0
  365. package/dist/services/memory-init-lint-capability.js +203 -0
  366. package/dist/services/memory-init-native-capacity.js +309 -0
  367. package/dist/services/memory-init-preflight.js +334 -0
  368. package/dist/services/memory-init-project-bootstrap.js +599 -0
  369. package/dist/services/memory-init-request-candidate.js +213 -0
  370. package/dist/services/memory-init-request-input.js +82 -0
  371. package/dist/services/memory-init-resource-storage.js +125 -0
  372. package/dist/services/memory-init-run-allocation.js +81 -0
  373. package/dist/services/memory-init-run-bundle.js +135 -0
  374. package/dist/services/memory-init-start.js +181 -0
  375. package/dist/services/memory-init-startup-custody.js +80 -0
  376. package/dist/services/memory-init-startup-input.js +240 -0
  377. package/dist/services/memory-init-storage.js +91 -0
  378. package/dist/services/memory-init-stores.js +359 -0
  379. package/dist/services/memory-init-worktrunk-capability.js +174 -0
  380. package/dist/services/memory-layer-publication.js +611 -0
  381. package/dist/services/memory-layer.js +221 -0
  382. package/dist/services/memory-lint-config.js +194 -0
  383. package/dist/services/memory-native-review-copy.js +103 -0
  384. package/dist/services/memory-normal-flow-entry.js +87 -0
  385. package/dist/services/memory-ordinary-defaults.js +129 -0
  386. package/dist/services/memory-preflight-bootstrap-custody.js +115 -0
  387. package/dist/services/memory-preflight-handler.js +274 -0
  388. package/dist/services/memory-repair-current-view.js +299 -0
  389. package/dist/services/memory-repair-document-delta.js +488 -0
  390. package/dist/services/memory-repair-history.js +496 -0
  391. package/dist/services/memory-repair-physical-binding.js +194 -0
  392. package/dist/services/memory-repair-publication-files.js +152 -0
  393. package/dist/services/memory-repair-publication-intent.js +27 -0
  394. package/dist/services/memory-repair-publication-plan.js +162 -0
  395. package/dist/services/memory-repair-publication-source.js +225 -0
  396. package/dist/services/memory-repair-publication.js +795 -0
  397. package/dist/services/memory-repair-registration.js +240 -0
  398. package/dist/services/memory-repair-work-baseline.js +415 -0
  399. package/dist/services/memory-repair-work-completion.js +117 -0
  400. package/dist/services/memory-repair-work-custody.js +73 -0
  401. package/dist/services/memory-repair-work-results.js +108 -0
  402. package/dist/services/memory-repair-workspace.js +261 -0
  403. package/dist/services/memory-repair-writer-copy.js +736 -0
  404. package/dist/services/memory-result-binding.js +239 -0
  405. package/dist/services/memory-retained-review-authority.js +138 -0
  406. package/dist/services/memory-retained-review-results.js +70 -0
  407. package/dist/services/memory-retained-second-baseline.js +102 -0
  408. package/dist/services/memory-retained-second-repairs.js +162 -0
  409. package/dist/services/memory-review-acceptance.js +214 -0
  410. package/dist/services/memory-review-correction-plan.js +206 -0
  411. package/dist/services/memory-review-handler.js +585 -0
  412. package/dist/services/memory-review-inputs.js +246 -0
  413. package/dist/services/memory-review-limits.js +10 -0
  414. package/dist/services/memory-review-receipt.js +180 -0
  415. package/dist/services/memory-review-registration.js +117 -0
  416. package/dist/services/memory-review-work-custody.js +126 -0
  417. package/dist/services/memory-review-work-input.js +91 -0
  418. package/dist/services/memory-review-work-results.js +95 -0
  419. package/dist/services/memory-second-corrected-review-inputs.js +167 -0
  420. package/dist/services/memory-second-correction-plan.js +77 -0
  421. package/dist/services/memory-second-repair-baseline.js +91 -0
  422. package/dist/services/memory-second-repair-launch.js +614 -0
  423. package/dist/services/memory-second-repair-registration.js +88 -0
  424. package/dist/services/memory-second-repair-source.js +187 -0
  425. package/dist/services/memory-second-review-acceptance.js +115 -0
  426. package/dist/services/memory-source-observations.js +117 -0
  427. package/dist/services/memory-stage-dispatch.js +65 -0
  428. package/dist/services/memory-stage-results.js +290 -0
  429. package/dist/services/memory-tool-probe.js +25 -0
  430. package/dist/services/memory-upgrade-analysis-handler.js +243 -0
  431. package/dist/services/memory-upgrade-bootstrap-readiness.js +251 -0
  432. package/dist/services/memory-upgrade-evidence.js +100 -0
  433. package/dist/services/memory-upgrade-inventory.js +442 -0
  434. package/dist/services/memory-upgrade-publication-custody.js +73 -0
  435. package/dist/services/memory-upgrade-publication.js +413 -0
  436. package/dist/services/memory-upgrade-selection.js +430 -0
  437. package/dist/services/memory-upgrade-start.js +60 -0
  438. package/dist/services/memory-upgrade-storage-readiness.js +340 -0
  439. package/dist/services/memory-verification-acceptance.js +156 -0
  440. package/dist/services/memory-verification-checks.js +317 -0
  441. package/dist/services/memory-verification-receipt.js +160 -0
  442. package/dist/services/memory-verify-handler.js +137 -0
  443. package/dist/services/memory-work-result-json.js +12 -0
  444. package/dist/services/memory-work-result-preparation.js +64 -0
  445. package/dist/services/merge-queue.js +228 -149
  446. package/dist/services/merge-server.js +163 -35
  447. package/dist/services/migrations.js +164 -114
  448. package/dist/services/model-catalog.js +109 -0
  449. package/dist/services/model-discovery.js +46 -0
  450. package/dist/services/native-daemon-history.js +50 -0
  451. package/dist/services/native-session-control.js +39 -0
  452. package/dist/services/operator-profiles.js +236 -0
  453. package/dist/services/physical-startup.js +45 -0
  454. package/dist/services/plan-runtime.js +26 -11
  455. package/dist/services/plans.js +28 -25
  456. package/dist/services/project-registry.js +97 -0
  457. package/dist/services/project-summary.js +1 -1
  458. package/dist/services/projects.js +17 -87
  459. package/dist/services/prompt-delivery.js +71 -0
  460. package/dist/services/prompts.js +28 -17
  461. package/dist/services/protocols.js +163 -87
  462. package/dist/services/provider-capacity.js +463 -0
  463. package/dist/services/recovery-observation-budget.js +121 -0
  464. package/dist/services/recovery-snapshot-database.js +588 -0
  465. package/dist/services/release-archive-producer.js +52 -0
  466. package/dist/services/release-archive.js +143 -0
  467. package/dist/services/release-cache.js +150 -0
  468. package/dist/services/release-compatibility.js +18 -0
  469. package/dist/services/release-download.js +67 -0
  470. package/dist/services/release-envelope.js +104 -0
  471. package/dist/services/release-manifest.js +45 -0
  472. package/dist/services/release-payload.js +114 -0
  473. package/dist/services/release-producer.js +63 -0
  474. package/dist/services/release-provenance.js +160 -0
  475. package/dist/services/release-registry.js +66 -0
  476. package/dist/services/release-source.js +194 -0
  477. package/dist/services/repair-intents.js +31 -0
  478. package/dist/services/repair-receipt.js +13 -0
  479. package/dist/services/review-copy.js +165 -0
  480. package/dist/services/review-deferral.js +23 -0
  481. package/dist/services/run-control-receipt.js +56 -0
  482. package/dist/services/run-control-worker.js +428 -0
  483. package/dist/services/run-control.js +1092 -0
  484. package/dist/services/run-controller-adapter.js +400 -0
  485. package/dist/services/run-controller-capture.js +186 -0
  486. package/dist/services/run-controller-process.js +205 -0
  487. package/dist/services/run-controller-recovery.js +295 -0
  488. package/dist/services/run-controller-state.js +97 -0
  489. package/dist/services/run-controller.js +1395 -0
  490. package/dist/services/run-engine-bindings.js +1 -1
  491. package/dist/services/run-fork.js +147 -0
  492. package/dist/services/run-health.js +149 -0
  493. package/dist/services/run-observations.js +152 -0
  494. package/dist/services/run-projection.js +22 -64
  495. package/dist/services/run-recovery-guard.js +107 -0
  496. package/dist/services/run-recovery-runtime.js +112 -0
  497. package/dist/services/run-recovery.js +456 -0
  498. package/dist/services/run-start-custody.js +189 -0
  499. package/dist/services/runs.js +1023 -246
  500. package/dist/services/runtime-budget.js +446 -0
  501. package/dist/services/runtime-command.js +43 -0
  502. package/dist/services/runtime-scope-capture.js +52 -0
  503. package/dist/services/runtime-scope-control.js +468 -0
  504. package/dist/services/runtime-scope-resume.js +605 -0
  505. package/dist/services/runtime-scope-stop.js +211 -0
  506. package/dist/services/runtime-scope-worker.js +314 -0
  507. package/dist/services/runtime-service.js +19 -14
  508. package/dist/services/schema-validation.js +10 -8
  509. package/dist/services/scope-writer-cleanup.js +27 -0
  510. package/dist/services/sessions.js +36 -91
  511. package/dist/services/setup.js +427 -0
  512. package/dist/services/stage-blocker.js +17 -7
  513. package/dist/services/stage-context.js +240 -18
  514. package/dist/services/stage-handlers.js +89 -0
  515. package/dist/services/stage-lifecycle.js +152 -116
  516. package/dist/services/stage-pause.js +202 -91
  517. package/dist/services/stage-report-renderer.js +234 -6
  518. package/dist/services/stage-work-graph.js +52 -0
  519. package/dist/services/tooling.js +41 -30
  520. package/dist/services/usage.js +49 -101
  521. package/dist/services/verification-matrix.js +393 -0
  522. package/dist/services/version-status.js +1 -1
  523. package/dist/services/vnext-code-review.js +235 -88
  524. package/dist/services/vnext-code.js +327 -226
  525. package/dist/services/vnext-contracts.js +10 -1
  526. package/dist/services/vnext-execution-profile.js +80 -5
  527. package/dist/services/vnext-fanout.js +355 -21
  528. package/dist/services/vnext-merge.js +332 -111
  529. package/dist/services/vnext-plan-review.js +287 -132
  530. package/dist/services/vnext-plan.js +228 -104
  531. package/dist/services/vnext-protocolize.js +533 -95
  532. package/dist/services/vnext-specify.js +123 -81
  533. package/dist/services/vnext-workspace-policy.js +7 -2
  534. package/dist/services/work-registry.js +1706 -200
  535. package/dist/services/workspace-bootstrap.js +53 -15
  536. package/dist/services/workspace-files.js +85 -0
  537. package/dist/services/worktrees.js +105 -48
  538. package/dist/shared/entity-references.js +1 -0
  539. package/dist/shared/errors.js +27 -0
  540. package/dist/storage/agent-output.js +26 -0
  541. package/dist/storage/data-preservation.js +65 -0
  542. package/dist/storage/database.js +930 -40
  543. package/dist/storage/foreign-key-transaction.js +39 -0
  544. package/dist/storage/immutable-input.js +45 -0
  545. package/dist/storage/import-guard.js +159 -0
  546. package/dist/storage/memory-upgrade-schema-bootstrap.js +38 -0
  547. package/dist/storage/native-cleanup.js +52 -0
  548. package/dist/storage/paired-writer-migration.js +424 -0
  549. package/dist/storage/paths.js +17 -4
  550. package/dist/storage/projection-lock.js +73 -0
  551. package/dist/storage/work-references.js +23 -0
  552. package/dist/storage/writer-contract.js +96 -0
  553. package/dist/storage/writer-migration.js +346 -0
  554. package/dist/storage/writer-restore.js +217 -0
  555. package/dist/verification-components.json +299 -0
  556. package/package.json +51 -13
  557. package/tools/create-canon-release.mjs +33 -0
  558. package/tools/probe-codex-history.mjs +37 -0
  559. package/tools/produce-hybrid-release.mjs +41 -0
  560. package/tools/repair-paused-run-status.mjs +84 -0
@@ -0,0 +1,415 @@
1
+ import { memoryRepairCanonicalPaths } from "./memory-review-correction-plan.js";
2
+ import { createHash } from "node:crypto";
3
+ import { AppError } from "../shared/errors.js";
4
+ import { appendAudit } from "./audit.js";
5
+ import { hybridDigest } from "./hybrid-contracts.js";
6
+ import { assertRunMutationAllowed } from "./run-recovery-guard.js";
7
+ import { readWorkBatchCustody } from "./work-registry.js";
8
+ import { PreparedRegisteredMemoryCorrections } from "./memory-repair-registration.js";
9
+ import { MemoryRepairDocumentBaseline } from "./memory-repair-document-delta.js";
10
+ import { reviewSourceHash, reviewSourcePaths } from "./review-copy.js";
11
+ import { memoryRepairCurrentReferences, validateMemoryRepairWorkResult } from "./memory-repair-work-results.js";
12
+ import { PreparedMemoryRepairCurrentView } from "./memory-repair-current-view.js";
13
+ import { readMemoryRepairPhysicalBinding } from "./memory-repair-physical-binding.js";
14
+ const event = "memory.repair_work_baseline";
15
+ const preparationToken = Symbol("memory-repair-work-baseline-preparation");
16
+ function refuse(reason) {
17
+ throw new AppError("memory_repair_work_baseline_invalid", "Writer baseline lacks registered repair Work and current source custody", 1, { reason, effect: "no_effect" });
18
+ }
19
+ function select(context, scope) {
20
+ const rows = context.db.all("SELECT payload_json FROM audit_events WHERE project_id=? AND event_type=? AND json_extract(payload_json,'$.run_id')=? AND json_extract(payload_json,'$.work_id')=? LIMIT 2", [scope.project_id, event, scope.run_id, scope.work_id]);
21
+ if (!rows.length)
22
+ return null;
23
+ try {
24
+ if (rows.length !== 1 || Buffer.byteLength(rows[0].payload_json) > 24 * 1024 * 1024)
25
+ refuse("ambiguous_or_unbounded_baseline");
26
+ const receipt = JSON.parse(rows[0].payload_json);
27
+ if (!["dd-flow/memory-repair-work-baseline@2", "dd-flow/memory-repair-work-baseline@3"].includes(receipt.schema_id) || receipt.export_policy !== "memory-repair-export@1"
28
+ || receipt.project_id !== scope.project_id
29
+ || receipt.run_id !== scope.run_id || receipt.work_id !== scope.work_id || receipt.generation !== scope.generation
30
+ || typeof receipt.baseline_base64 !== "string")
31
+ refuse("foreign_baseline_receipt");
32
+ if (receipt.schema_id === "dd-flow/memory-repair-work-baseline@3"
33
+ ? [receipt.current_view_sha256, receipt.current_view_receipt_sha256].some(value => typeof value !== "string" || !/^[a-f0-9]{64}$/.test(value))
34
+ : Object.hasOwn(receipt, "current_view_sha256") || Object.hasOwn(receipt, "current_view_receipt_sha256"))
35
+ refuse("invalid_current_view_binding");
36
+ const bytes = Buffer.from(receipt.baseline_base64, "base64");
37
+ if (bytes.toString("base64") !== receipt.baseline_base64)
38
+ refuse("changed_baseline_bytes");
39
+ MemoryRepairDocumentBaseline.restore(bytes, receipt.baseline_sha256);
40
+ if (typeof receipt.exported_baseline_base64 !== "string")
41
+ refuse("exported_baseline_receipt_required");
42
+ const exportedBytes = Buffer.from(receipt.exported_baseline_base64, "base64");
43
+ if (exportedBytes.toString("base64") !== receipt.exported_baseline_base64)
44
+ refuse("changed_exported_baseline_bytes");
45
+ const exported = MemoryRepairDocumentBaseline.restore(exportedBytes, receipt.exported_baseline_sha256);
46
+ const source = MemoryRepairDocumentBaseline.restore(bytes, receipt.baseline_sha256);
47
+ if (source.exported(exported.entries().filter(entry => entry.kind === "file").map(entry => entry.path)).sha256 !== exported.sha256)
48
+ refuse("foreign_exported_baseline");
49
+ return receipt;
50
+ }
51
+ catch (error) {
52
+ if (error instanceof AppError && error.code === "memory_repair_work_baseline_invalid")
53
+ throw error;
54
+ return refuse("invalid_baseline_receipt");
55
+ }
56
+ }
57
+ /** Pre-launch original-document admission. Its independent receipt is not a
58
+ * WorkSession/start/copy or publication receipt. Dependent/previously repaired
59
+ * document views must come from registered physical repair settlements, never
60
+ * from current workspace bytes alone. Those views remain a separate owner.
61
+ */
62
+ export class PreparedMemoryRepairWorkBaseline {
63
+ static isPrepared(value) {
64
+ return typeof value === "object" && value !== null && #receipt in value;
65
+ }
66
+ workId;
67
+ sha256;
68
+ #registration;
69
+ #allocation;
70
+ #scope;
71
+ #receipt;
72
+ #baseline;
73
+ #launch;
74
+ #startedWorkSessionId;
75
+ #currentView;
76
+ constructor(token, registration, allocation, receipt, baseline, launch = null, startedWorkSessionId = null, currentView = null) {
77
+ if (token !== preparationToken)
78
+ refuse("prepared_baseline_token_required");
79
+ this.#registration = registration;
80
+ this.#allocation = structuredClone(allocation);
81
+ this.#scope = {
82
+ project_id: receipt.project_id, run_id: receipt.run_id, work_id: receipt.work_id, generation: receipt.generation
83
+ };
84
+ this.#receipt = structuredClone(receipt);
85
+ this.#baseline = baseline;
86
+ this.#launch = launch ? structuredClone(launch) : null;
87
+ this.#startedWorkSessionId = startedWorkSessionId;
88
+ this.#currentView = currentView;
89
+ this.workId = receipt.work_id;
90
+ this.sha256 = hybridDigest(receipt);
91
+ Object.freeze(this);
92
+ }
93
+ static prepare(context, registration, workId) {
94
+ if (!(registration instanceof PreparedRegisteredMemoryCorrections))
95
+ refuse("registered_corrections_required");
96
+ registration.revalidate(context);
97
+ const owner = registration.receipt(), assigned = registration.batch().works.find(item => owner.work_ids[item.key] === workId);
98
+ if (!assigned)
99
+ refuse("foreign_repair_work");
100
+ const scope = { project_id: owner.scope.project_id, run_id: owner.run_id, work_id: workId,
101
+ generation: assertRunMutationAllowed(context, owner.scope.project_id, owner.run_id) };
102
+ const allocation = readWorkBatchCustody(context, workId);
103
+ // Never rebase original evidence onto an unexplained shared-workspace edit.
104
+ // The next document-view owner will derive these inputs from settled deltas.
105
+ if (assigned.depends_on.length)
106
+ refuse("registered_dependency_document_view_required");
107
+ const view = registration.review().original().registration().view;
108
+ const workspace = registration.review().original().bundle().execution.snapshot().scope.workspace;
109
+ const sourceHash = () => reviewSourceHash(workspace, view.run_inputs.filter(ref => ref.root === "workspace").map(ref => ref.path), view.base);
110
+ if (sourceHash() !== view.source_sha256)
111
+ refuse("original_source_changed_before_writer");
112
+ const baseline = MemoryRepairDocumentBaseline.capture(workspace, assigned.payload.memory_repair.write_paths, memoryRepairCanonicalPaths(context, registration.review().original().bundle()));
113
+ const selected = reviewSourcePaths(workspace, view.run_inputs.filter(ref => ref.root === "workspace").map(ref => ref.path), view.base);
114
+ if (selected === null)
115
+ refuse("export_source_inventory_unavailable");
116
+ const available = new Set(baseline.entries().filter(entry => entry.kind === "file").map(entry => entry.path));
117
+ const exported = baseline.exported(selected.filter(logical => logical.startsWith(".memory-bank/") && available.has(logical)));
118
+ if (sourceHash() !== view.source_sha256)
119
+ refuse("original_source_changed_during_baseline");
120
+ baseline.revalidate(workspace);
121
+ const receipt = { schema_id: "dd-flow/memory-repair-work-baseline@2", ...scope,
122
+ registration_sha256: registration.sha256, allocation_sha256: hybridDigest(allocation),
123
+ packet_sha256: assigned.payload.memory_repair.packet_sha256, workspace_root: workspace,
124
+ baseline_sha256: baseline.sha256, baseline_base64: baseline.bytes().toString("base64"),
125
+ export_policy: "memory-repair-export@1", exported_baseline_sha256: exported.sha256,
126
+ exported_baseline_base64: exported.bytes().toString("base64") };
127
+ const prepared = new PreparedMemoryRepairWorkBaseline(preparationToken, registration, allocation, receipt, baseline);
128
+ prepared.assertCustody(context);
129
+ return prepared;
130
+ }
131
+ /** Consume a separately registered current-view receipt. Neither a caller
132
+ * snapshot nor a current filesystem capture can substitute for this owner.
133
+ */
134
+ static prepareFromCurrentView(context, registration, workId, view) {
135
+ if (!(registration instanceof PreparedRegisteredMemoryCorrections))
136
+ refuse("registered_corrections_required");
137
+ if (!PreparedMemoryRepairCurrentView.isPrepared(view) || view.workId !== workId)
138
+ refuse("registered_current_view_required");
139
+ const retained = PreparedMemoryRepairCurrentView.restore(context, registration, workId);
140
+ if (retained.sha256 !== view.sha256)
141
+ refuse("current_view_registration_changed");
142
+ retained.revalidate(context);
143
+ const owner = registration.receipt(), assigned = registration.batch().works.find(item => owner.work_ids[item.key] === workId);
144
+ if (!assigned)
145
+ refuse("foreign_repair_work");
146
+ const generation = assertRunMutationAllowed(context, owner.scope.project_id, owner.run_id);
147
+ const allocation = readWorkBatchCustody(context, workId), baseline = retained.baseline(), exported = retained.exportedBaseline();
148
+ const receipt = { schema_id: "dd-flow/memory-repair-work-baseline@3", project_id: owner.scope.project_id,
149
+ run_id: owner.run_id, work_id: workId, generation, registration_sha256: registration.sha256,
150
+ allocation_sha256: hybridDigest(allocation), packet_sha256: assigned.payload.memory_repair.packet_sha256,
151
+ workspace_root: registration.review().original().bundle().execution.snapshot().scope.workspace,
152
+ baseline_sha256: baseline.sha256, baseline_base64: baseline.bytes().toString("base64"), export_policy: "memory-repair-export@1",
153
+ exported_baseline_sha256: exported.sha256, exported_baseline_base64: exported.bytes().toString("base64"),
154
+ current_view_sha256: retained.sha256, current_view_receipt_sha256: hybridDigest(retained.receipt()) };
155
+ const prepared = new PreparedMemoryRepairWorkBaseline(preparationToken, registration, allocation, receipt, baseline, null, null, retained);
156
+ prepared.assertCustody(context);
157
+ return prepared;
158
+ }
159
+ receipt() { return structuredClone(this.#receipt); }
160
+ validateResult(context, bytes) {
161
+ this.assertRegisteredCustody(context);
162
+ const owner = this.#registration.receipt();
163
+ const assigned = this.#registration.batch().works.find(item => owner.work_ids[item.key] === this.workId);
164
+ if (!assigned || assigned.payload.memory_repair.packet_sha256 !== this.#receipt.packet_sha256)
165
+ refuse("repair_packet_changed");
166
+ return validateMemoryRepairWorkResult(bytes, this.#registration.review().original().bundle().engine(), { workId: this.workId, packet: assigned.payload.memory_repair,
167
+ ...(this.#currentView ? { effectiveInputs: memoryRepairCurrentReferences(assigned.payload.memory_repair, this.#currentView.workspaceManifest()) } : {}) });
168
+ }
169
+ currentInput() {
170
+ if (!this.#currentView)
171
+ return null;
172
+ const owner = this.#registration.receipt(), assigned = this.#registration.batch().works.find(item => owner.work_ids[item.key] === this.workId);
173
+ if (!assigned)
174
+ refuse("repair_packet_changed");
175
+ return { current_view_sha256: this.#currentView.sha256, current_view_receipt_sha256: hybridDigest(this.#currentView.receipt()),
176
+ effective_read_inputs: memoryRepairCurrentReferences(assigned.payload.memory_repair, this.#currentView.workspaceManifest()) };
177
+ }
178
+ /** Retain the registered original baseline after a physical launch exists;
179
+ * never capture today's documents as a replacement for its SQL receipt.
180
+ */
181
+ static restoreForLaunch(context, registration, workId) {
182
+ return this.#restoreRegistered(context, registration, workId, null);
183
+ }
184
+ /** Restore original registered bytes for an existing physical WorkSession.
185
+ * This grants neither another claim nor shared-workspace publication.
186
+ */
187
+ static restoreForStartedWork(context, registration, workId, workSessionId) {
188
+ if (typeof workSessionId !== "string" || !workSessionId || workSessionId.length > 256)
189
+ refuse("registered_start_session_required");
190
+ return this.#restoreRegistered(context, registration, workId, workSessionId);
191
+ }
192
+ /** SQL restoration for an already reserved publication, not new input
193
+ * observation. Partly published source must be classified by its intent.
194
+ */
195
+ static restoreForPublication(context, registration, workId, workSessionId) {
196
+ if (typeof workSessionId !== "string" || !workSessionId || workSessionId.length > 256)
197
+ refuse("registered_start_session_required");
198
+ return this.#restoreRegistered(context, registration, workId, workSessionId, true);
199
+ }
200
+ /** Restore only retained start custody for output observation. Shared input
201
+ * admission belongs to a separate publication source-view owner. */
202
+ static restoreForOutput(context, registration, workId, workSessionId) {
203
+ if (typeof workSessionId !== "string" || !workSessionId || workSessionId.length > 256)
204
+ refuse("registered_start_session_required");
205
+ return this.#restoreRegistered(context, registration, workId, workSessionId, false, true);
206
+ }
207
+ static #restoreRegistered(context, registration, workId, workSessionId, publicationOnly = false, outputOnly = false) {
208
+ if (!(registration instanceof PreparedRegisteredMemoryCorrections))
209
+ refuse("registered_corrections_required");
210
+ registration.assertCustody(context);
211
+ const owner = registration.receipt(), assigned = registration.batch().works.find(item => owner.work_ids[item.key] === workId);
212
+ if (!assigned)
213
+ refuse("foreign_repair_work");
214
+ const scope = { project_id: owner.scope.project_id, run_id: owner.run_id, work_id: workId,
215
+ generation: assertRunMutationAllowed(context, owner.scope.project_id, owner.run_id) };
216
+ const receipt = select(context, scope), allocation = readWorkBatchCustody(context, workId);
217
+ const route = registration.review().original().bundle().execution.work("review", assigned.payload.execution_override?.agent_profile_id);
218
+ const launch = readMemoryRepairPhysicalBinding(context, workId, route);
219
+ if (!receipt || receipt.registration_sha256 !== registration.sha256 || receipt.allocation_sha256 !== hybridDigest(allocation)
220
+ || receipt.packet_sha256 !== assigned.payload.memory_repair.packet_sha256
221
+ || receipt.workspace_root !== registration.review().original().bundle().execution.snapshot().scope.workspace)
222
+ refuse("registered_baseline_receipt_required");
223
+ if (assigned.depends_on.length && receipt.schema_id !== "dd-flow/memory-repair-work-baseline@3")
224
+ refuse("registered_dependency_document_view_required");
225
+ const currentView = receipt.schema_id === "dd-flow/memory-repair-work-baseline@3"
226
+ ? PreparedMemoryRepairCurrentView.restore(context, registration, workId) : null;
227
+ if (currentView && (currentView.sha256 !== receipt.current_view_sha256 || hybridDigest(currentView.receipt()) !== receipt.current_view_receipt_sha256
228
+ || currentView.baseline().sha256 !== receipt.baseline_sha256 || currentView.exportedBaseline().sha256 !== receipt.exported_baseline_sha256))
229
+ refuse("registered_current_view_changed");
230
+ if (!launch?.provider_session_id || !["launching", "start_requested"].includes(launch.status))
231
+ refuse("registered_physical_launch_required");
232
+ try {
233
+ if (Buffer.byteLength(launch.profile_json) > 1024 * 1024)
234
+ refuse("invalid_launch_profile");
235
+ const profile = JSON.parse(launch.profile_json);
236
+ const frozen = { coordinator: route.coordinator, worker: route.worker,
237
+ delegation: { mode: route.delegation.mode, max_parallel: route.delegation.mode === "external" ? route.delegation.max_parallel : null } };
238
+ if (hybridDigest({ coordinator: profile.coordinator, worker: profile.worker, delegation: profile.delegation }) !== hybridDigest(frozen)
239
+ || launch.profile_hash !== createHash("sha256").update(JSON.stringify(frozen)).digest("hex"))
240
+ refuse("launch_profile_not_frozen");
241
+ }
242
+ catch (error) {
243
+ if (error instanceof AppError && error.code === "memory_repair_work_baseline_invalid")
244
+ throw error;
245
+ return refuse("invalid_launch_profile");
246
+ }
247
+ const baseline = MemoryRepairDocumentBaseline.restore(Buffer.from(receipt.baseline_base64, "base64"), receipt.baseline_sha256);
248
+ const prepared = new PreparedMemoryRepairWorkBaseline(preparationToken, registration, allocation, receipt, baseline, launch, workSessionId, currentView);
249
+ prepared.assertRegisteredCustody(context);
250
+ if (publicationOnly)
251
+ prepared.registeredPublicationIntent(context, workSessionId);
252
+ else if (outputOnly)
253
+ prepared.assertRegisteredStartCustody(context, workSessionId);
254
+ else
255
+ prepared.revalidate(context);
256
+ return prepared;
257
+ }
258
+ registeredPublicationIntent(context, workSessionId) {
259
+ this.assertRegisteredStartCustody(context, workSessionId);
260
+ const rows = context.db.all("SELECT payload_json FROM audit_events WHERE project_id=? AND event_type='memory.repair_publication_intent' AND json_extract(payload_json,'$.run_id')=? AND json_extract(payload_json,'$.work_id')=? AND json_extract(payload_json,'$.generation')=? LIMIT 2", [this.#scope.project_id, this.#scope.run_id, this.workId, this.#scope.generation]);
261
+ const starts = context.db.all("SELECT payload_json FROM audit_events WHERE project_id=? AND event_type='memory.repair_work_start' AND json_extract(payload_json,'$.run_id')=? AND json_extract(payload_json,'$.work_id')=? AND json_extract(payload_json,'$.generation')=? LIMIT 2", [this.#scope.project_id, this.#scope.run_id, this.workId, this.#scope.generation]);
262
+ try {
263
+ if (rows.length !== 1 || starts.length !== 1 || Buffer.byteLength(rows[0].payload_json) > 160 * 1024 * 1024
264
+ || Buffer.byteLength(starts[0].payload_json) > 1024 * 1024)
265
+ refuse("registered_publication_intent_required");
266
+ const value = JSON.parse(rows[0].payload_json);
267
+ const start = JSON.parse(starts[0].payload_json);
268
+ if (!["dd-flow/memory-repair-publication-intent@1", "dd-flow/memory-repair-publication-intent@2"].includes(value.schema_id) || value.project_id !== this.#scope.project_id
269
+ || value.run_id !== this.#scope.run_id || value.work_id !== this.workId || value.generation !== this.#scope.generation
270
+ || value.work_session_id !== workSessionId || start.work_session_id !== workSessionId
271
+ || value.start_receipt_sha256 !== hybridDigest(start) || value.source_root !== this.#receipt.workspace_root)
272
+ refuse("publication_baseline_or_start_changed");
273
+ if (value.schema_id === "dd-flow/memory-repair-publication-intent@1") {
274
+ if (value.before_sha256 !== this.#baseline.sha256 || value.before_base64 !== this.#baseline.bytes().toString("base64"))
275
+ refuse("publication_baseline_or_start_changed");
276
+ }
277
+ else {
278
+ // This SQL binding is not source authority. The publication owner must
279
+ // independently restore the graph/histories and private/shared plans.
280
+ const sources = context.db.all("SELECT payload_json FROM audit_events WHERE project_id=? AND event_type='memory.repair_publication_source' AND json_extract(payload_json,'$.run_id')=? AND json_extract(payload_json,'$.work_id')=? AND json_extract(payload_json,'$.generation')=? LIMIT 2", [this.#scope.project_id, this.#scope.run_id, this.workId, this.#scope.generation]);
281
+ if (sources.length !== 1 || Buffer.byteLength(sources[0].payload_json) > 40 * 1024 * 1024)
282
+ refuse("registered_publication_source_required");
283
+ const source = JSON.parse(sources[0].payload_json), { source_view_sha256: sourceSha256, ...body } = source;
284
+ const privatePlan = value.private_plan;
285
+ if (source.schema_id !== "dd-flow/memory-repair-publication-source@1" || source.project_id !== this.#scope.project_id
286
+ || source.run_id !== this.#scope.run_id || source.work_id !== this.workId || source.generation !== this.#scope.generation
287
+ || source.work_session_id !== workSessionId || source.registration_sha256 !== this.#registration.sha256
288
+ || source.baseline_registration_sha256 !== this.sha256 || source.start_receipt_sha256 !== hybridDigest(start)
289
+ || source.source_root !== this.#receipt.workspace_root || sourceSha256 !== hybridDigest(body)
290
+ || value.source_view_sha256 !== sourceSha256 || value.source_receipt_sha256 !== hybridDigest(source)
291
+ || value.before_sha256 !== source.before_sha256 || value.before_base64 !== source.before_base64
292
+ || !privatePlan || privatePlan.before_sha256 !== this.#baseline.sha256
293
+ || privatePlan.before_base64 !== this.#baseline.bytes().toString("base64")
294
+ || value.private_plan_sha256 !== hybridDigest(privatePlan))
295
+ refuse("publication_source_or_private_start_changed");
296
+ }
297
+ return structuredClone(value);
298
+ }
299
+ catch (error) {
300
+ if (error instanceof AppError && error.code === "memory_repair_work_baseline_invalid")
301
+ throw error;
302
+ return refuse("invalid_publication_intent");
303
+ }
304
+ }
305
+ launch() { return this.#launch ? structuredClone(this.#launch) : null; }
306
+ workerRoute() {
307
+ const receipt = this.#registration.receipt();
308
+ const assigned = this.#registration.batch().works.find(item => receipt.work_ids[item.key] === this.workId);
309
+ if (!assigned)
310
+ refuse("foreign_repair_work");
311
+ return this.#registration.review().original().bundle().execution.work("review", assigned.payload.execution_override?.agent_profile_id);
312
+ }
313
+ assertPrelaunchCustody(context) {
314
+ if (this.#launch)
315
+ refuse("use_registered_launch_baseline");
316
+ this.assertCustody(context);
317
+ }
318
+ /** SQL-only post-claim fence for the same prepared physical start. No new
319
+ * baseline can be registered from this phase or from mutable source bytes.
320
+ */
321
+ assertRegisteredStartCustody(context, workSessionId) {
322
+ if (!this.#launch?.provider_session_id)
323
+ refuse("registered_physical_launch_required");
324
+ this.#registration.assertCustody(context);
325
+ this.#assertCurrentView(context);
326
+ assertRunMutationAllowed(context, this.#scope.project_id, this.#scope.run_id, this.#scope.generation);
327
+ if (hybridDigest(readWorkBatchCustody(context, this.workId)) !== hybridDigest(this.#allocation))
328
+ refuse("repair_allocation_changed");
329
+ const launch = readMemoryRepairPhysicalBinding(context, this.workId, this.workerRoute(), this.#launch.operation_id);
330
+ const baseline = select(context, this.#scope);
331
+ if (!launch || hybridDigest(launch) !== hybridDigest(this.#launch) || !baseline || hybridDigest(baseline) !== this.sha256)
332
+ refuse("registered_start_custody_changed");
333
+ const rows = context.db.all("SELECT ws.id,ws.session_id,ws.hook_event_id,ws.start_receipt_json,s.harness,s.provider_session_id,s.cwd FROM work_sessions ws JOIN works w ON w.work_id=ws.work_id JOIN runs r ON r.project_id=w.project_id AND r.id=w.run_id JOIN sessions s ON s.project_id=w.project_id AND s.session_id=ws.session_id WHERE w.project_id=? AND w.run_id=? AND w.work_id=? AND w.status='running' AND r.status='running' AND r.workspace_root=? AND ws.status='running' AND s.status='active' AND s.current_stage='work' AND s.worker_id=w.work_id AND s.run_id=w.run_id AND s.workspace_path=r.workspace_root AND s.project_root=r.project_root LIMIT 2", [this.#scope.project_id, this.#scope.run_id, this.workId, this.#receipt.workspace_root]);
334
+ if (rows.length !== 1 || rows[0].id !== workSessionId || rows[0].provider_session_id !== this.#launch.provider_session_id)
335
+ refuse("registered_start_session_required");
336
+ return { ...rows[0] };
337
+ }
338
+ baseline() { return MemoryRepairDocumentBaseline.restore(Buffer.from(this.#receipt.baseline_base64, "base64"), this.#receipt.baseline_sha256); }
339
+ exportedBaseline() { return MemoryRepairDocumentBaseline.restore(Buffer.from(this.#receipt.exported_baseline_base64, "base64"), this.#receipt.exported_baseline_sha256); }
340
+ copyInput() {
341
+ if (this.#currentView)
342
+ return this.#currentView.copyInput();
343
+ const view = this.#registration.review().original().registration().view;
344
+ return { source_sha256: view.source_sha256, base: view.base,
345
+ explicit_inputs: view.run_inputs.filter(ref => ref.root === "workspace").map(ref => ref.path) };
346
+ }
347
+ /** Preparation alone is not an independently committed baseline receipt. */
348
+ assertRegisteredCustody(context) {
349
+ this.assertCustody(context);
350
+ const receipt = select(context, this.#scope);
351
+ if (!receipt || hybridDigest(receipt) !== this.sha256)
352
+ refuse("registered_baseline_receipt_required");
353
+ }
354
+ assertCustody(context) {
355
+ if (this.#startedWorkSessionId) {
356
+ // The registered start owner also rechecks this baseline's current view.
357
+ this.assertRegisteredStartCustody(context, this.#startedWorkSessionId);
358
+ return;
359
+ }
360
+ this.#assertCurrentView(context);
361
+ this.#registration.assertCustody(context);
362
+ assertRunMutationAllowed(context, this.#scope.project_id, this.#scope.run_id, this.#scope.generation);
363
+ if (hybridDigest(readWorkBatchCustody(context, this.workId)) !== hybridDigest(this.#allocation))
364
+ refuse("repair_allocation_changed");
365
+ const work = context.db.get("SELECT * FROM works WHERE work_id=?", [this.workId]);
366
+ const run = context.db.get("SELECT workspace_root,status FROM runs WHERE project_id=? AND id=?", [this.#scope.project_id, this.#scope.run_id]);
367
+ if (!work || work.status !== "created" || !run || run.status !== "running" || run.workspace_root !== this.#receipt.workspace_root
368
+ || context.db.get("SELECT 1 FROM work_sessions WHERE work_id=? LIMIT 1", [this.workId]))
369
+ refuse("unstarted_repair_work_required");
370
+ const launch = this.#launch ? readMemoryRepairPhysicalBinding(context, this.workId, this.workerRoute(), this.#launch.operation_id) : context.db.get("SELECT 1 FROM sqlite_master WHERE type='table' AND name='work_launches'")
371
+ ? context.db.get("SELECT operation_id,status,profile_json,profile_hash,provider_session_id FROM work_launches WHERE work_id=?", [this.workId]) : undefined;
372
+ if (this.#launch ? !launch || hybridDigest(launch) !== hybridDigest(this.#launch) : Boolean(launch))
373
+ refuse("use_registered_launch_baseline");
374
+ const prior = select(context, this.#scope);
375
+ if (prior && hybridDigest(prior) !== this.sha256)
376
+ refuse("baseline_registration_changed");
377
+ }
378
+ revalidateSource(context) {
379
+ if (this.#currentView) {
380
+ this.#currentView.revalidate(context);
381
+ return;
382
+ }
383
+ this.#registration.revalidate(context);
384
+ this.#baseline.revalidate(this.#receipt.workspace_root);
385
+ const view = this.#registration.review().original().registration().view;
386
+ if (reviewSourceHash(this.#receipt.workspace_root, view.run_inputs.filter(ref => ref.root === "workspace").map(ref => ref.path), view.base)
387
+ !== view.source_sha256)
388
+ refuse("original_source_changed_before_writer");
389
+ }
390
+ revalidate(context) {
391
+ this.revalidateSource(context);
392
+ this.assertCustody(context);
393
+ }
394
+ register(context) {
395
+ this.assertPrelaunchCustody(context);
396
+ this.revalidate(context);
397
+ return context.db.writeTransaction(() => {
398
+ this.assertCustody(context);
399
+ const prior = select(context, this.#scope);
400
+ if (!prior)
401
+ appendAudit(context, { projectId: this.#scope.project_id, eventType: event, payload: this.#receipt });
402
+ if (hybridDigest(select(context, this.#scope)) !== this.sha256)
403
+ refuse("baseline_registration_readback_failed");
404
+ return { work_id: this.workId, baseline_sha256: this.#baseline.sha256, registration_sha256: this.sha256, replayed: Boolean(prior) };
405
+ });
406
+ }
407
+ #assertCurrentView(context) {
408
+ if (this.#receipt.schema_id !== "dd-flow/memory-repair-work-baseline@3")
409
+ return;
410
+ if (!this.#currentView || this.#currentView.sha256 !== this.#receipt.current_view_sha256
411
+ || hybridDigest(this.#currentView.receipt()) !== this.#receipt.current_view_receipt_sha256)
412
+ refuse("registered_current_view_required");
413
+ this.#currentView.assertCustody(context);
414
+ }
415
+ }
@@ -0,0 +1,117 @@
1
+ import { AppError } from "../shared/errors.js";
2
+ import { PreparedPublishedMemoryRepairWorkResult } from "./memory-repair-work-custody.js";
3
+ import { prepareMemoryRepairPrivateOutputFromStage, prepareMemoryRepairPublicationSourceFromStage, restoreMemoryRepairPublicationSourceFromStage, reserveMemoryRepairPublicationAgainstSourceFromStage, reserveMemorySecondRepairPublicationFromStage, restoreMemoryRepairPublicationFromStage, prepareMemorySecondRepairPublicationFromStage, restoreStartedMemorySecondRepairLaunchFromStage } from "./memory-review-handler.js";
4
+ function refuse(reason) {
5
+ throw new AppError("memory_work_result_preparation_required", "Repair completion requires registered private output and physical start custody", 1, { reason, effect: "no_effect" });
6
+ }
7
+ /** Read-only preparation. Existing intents are restored, never reconstructed
8
+ * from today's partially published shared workspace. */
9
+ export function prepareMemoryRepairWorkCompletion(context, work, result) {
10
+ if (context.db.isOwnedWriteTransaction?.())
11
+ refuse("preparation_outside_writer_required");
12
+ const bytes = Buffer.from(result);
13
+ if (!bytes.length || bytes.length > 1024 * 1024)
14
+ refuse("bounded_result_required");
15
+ const run = context.db.get("SELECT project_root FROM runs WHERE project_id=? AND id=?", [work.project_id, work.run_id]);
16
+ const links = context.db.all("SELECT id,start_receipt_json FROM work_sessions WHERE work_id=? AND status='running' LIMIT 2", [work.work_id]);
17
+ if (!run || links.length !== 1)
18
+ refuse("exact_active_work_session_required");
19
+ const link = links[0];
20
+ let wave, workspaceRoot;
21
+ try {
22
+ const payload = JSON.parse(work.payload_json ?? "null");
23
+ if (work.result_schema !== "dd-flow/memory-repair-work-result@1" || payload?.kind !== "memory-repair" || payload.read_only !== false)
24
+ refuse("registered_repair_required");
25
+ wave = payload.memory_repair?.wave;
26
+ if (!link.start_receipt_json || Buffer.byteLength(link.start_receipt_json) > 32 * 1024 * 1024)
27
+ refuse("bounded_start_required");
28
+ const receipt = JSON.parse(link.start_receipt_json);
29
+ if (typeof receipt.context !== "string" || Buffer.byteLength(receipt.context) > 16 * 1024 * 1024)
30
+ refuse("bounded_context_required");
31
+ workspaceRoot = JSON.parse(receipt.context).workspace_root;
32
+ }
33
+ catch (error) {
34
+ if (error instanceof AppError)
35
+ throw error;
36
+ refuse("invalid_start_scope");
37
+ }
38
+ if (wave !== 1 && wave !== 2 || typeof workspaceRoot !== "string" || !workspaceRoot)
39
+ refuse("exact_wave_workspace_required");
40
+ const scope = { projectRoot: run.project_root, runId: work.run_id, workId: work.work_id, workspaceRoot, workSessionId: link.id };
41
+ const restore = () => wave === 1 ? restoreMemoryRepairPublicationFromStage(context, scope) : prepareMemorySecondRepairPublicationFromStage(context, scope);
42
+ // Presence selects only restoration; the real owner admits generation,
43
+ // exact receipt multiplicity and physical scope. Corrupt intents never
44
+ // fall back to a fresh reservation.
45
+ const retained = context.db.get("SELECT 1 FROM audit_events WHERE project_id=? AND event_type='memory.repair_publication_intent' AND json_extract(payload_json,'$.run_id')=? AND json_extract(payload_json,'$.work_id')=? LIMIT 1", [work.project_id, work.run_id, work.work_id]);
46
+ if (retained) {
47
+ const publication = restore();
48
+ if (!publication.bytes().equals(bytes))
49
+ refuse("retained_result_changed");
50
+ return { scope, bytes, wave, publication, source: null };
51
+ }
52
+ const outcome = wave === 1 ? prepareMemoryRepairPrivateOutputFromStage(context, { ...scope, bytes }).result().outcome
53
+ : restoreStartedMemorySecondRepairLaunchFromStage(context, scope).observeOutput(context, bytes).result.outcome;
54
+ if (outcome !== "accepted" && outcome !== "reconciled")
55
+ refuse("accepted_private_output_required_use_managed_fail_for_blocker");
56
+ let source = null;
57
+ if (wave === 1) {
58
+ try {
59
+ source = restoreMemoryRepairPublicationSourceFromStage(context, scope);
60
+ }
61
+ catch (error) {
62
+ if (!(error instanceof AppError) || error.code !== "memory_repair_publication_source_invalid" || error.details.reason !== "registered_source_required")
63
+ throw error;
64
+ source = prepareMemoryRepairPublicationSourceFromStage(context, scope);
65
+ }
66
+ }
67
+ return { scope, bytes, wave, publication: null, source };
68
+ }
69
+ /** Called by managed Work finish after its normal Hook/actor admission.
70
+ * Sealed owners revalidate before effects; no caller-supplied object grants
71
+ * authority. Every action is terminal and advances the retained finite cursor. */
72
+ export function publishMemoryRepairWorkCompletion(context, work, result, progress) {
73
+ progress?.(`Memory repair ${work.work_id}: preparing publication`);
74
+ const prepared = prepareMemoryRepairWorkCompletion(context, work, result);
75
+ const { scope, bytes, wave } = prepared;
76
+ progress?.(`Memory repair ${work.work_id}: publication input verified`);
77
+ try {
78
+ if (!prepared.publication) {
79
+ if (wave === 1) {
80
+ // Registration revalidates this invocation's source before and after
81
+ // its SQL effect; do not rebuild the entire same history graph again.
82
+ prepared.source.register(context);
83
+ progress?.(`Memory repair ${work.work_id}: publication source registered`);
84
+ reserveMemoryRepairPublicationAgainstSourceFromStage(context, { ...scope, bytes });
85
+ }
86
+ else
87
+ reserveMemorySecondRepairPublicationFromStage(context, { ...scope, bytes });
88
+ progress?.(`Memory repair ${work.work_id}: publication reserved`);
89
+ }
90
+ const publication = prepared.publication ?? (wave === 1 ? restoreMemoryRepairPublicationFromStage(context, scope) : prepareMemorySecondRepairPublicationFromStage(context, scope));
91
+ progress?.(`Memory repair ${work.work_id}: publication restored`);
92
+ const initial = publication.revalidate(context);
93
+ let completed = initial.completed;
94
+ progress?.(`Memory repair ${work.work_id}: publication ready (${completed}/${initial.total})`);
95
+ // The actual retained plan, not agent output or a guessed retry count,
96
+ // bounds execution. Even an already applied plan reconciles completion.
97
+ do {
98
+ const step = publication.applyNextAction(context);
99
+ if (step.total !== initial.total || step.completed < completed || step.completed > initial.total)
100
+ refuse("publication_cursor_changed");
101
+ if (step.status !== "applied" && step.completed !== completed + 1)
102
+ refuse("publication_did_not_advance");
103
+ progress?.(`Memory repair ${work.work_id}: publication advanced (${step.completed}/${step.total})`);
104
+ if (step.status === "applied")
105
+ break;
106
+ completed = step.completed;
107
+ } while (completed < initial.total);
108
+ const published = PreparedPublishedMemoryRepairWorkResult.prepare(context, publication);
109
+ progress?.(`Memory repair ${work.work_id}: published result verified`);
110
+ return published;
111
+ }
112
+ catch (error) {
113
+ throw new AppError("memory_repair_completion_pending", "Repair publication requires exact-request reconciliation before another completion attempt", 1, { effect: error instanceof AppError && error.details.effect === "unknown" ? "unknown" : "partial",
114
+ run_id: scope.runId, work_id: scope.workId, work_session_id: scope.workSessionId,
115
+ cause: error instanceof Error ? error.message : String(error), next_action: "Reconcile retained publication and lifecycle invocation; do not start another Session or replace result bytes." });
116
+ }
117
+ }
@@ -0,0 +1,73 @@
1
+ import { AppError } from "../shared/errors.js";
2
+ import { hybridDigest } from "./hybrid-contracts.js";
3
+ import { appendAudit } from "./audit.js";
4
+ import { PreparedRetainedMemoryRepairPublication } from "./memory-repair-publication.js";
5
+ const token = Symbol("published-memory-repair-work-result");
6
+ function refuse(reason) {
7
+ throw new AppError("memory_repair_work_custody_invalid", "Repair settlement requires independently completed publication and its active physical WorkSession", 1, { reason, effect: "no_effect" });
8
+ }
9
+ /** Publication proves file effects, not semantic review. This sealed owner
10
+ * bridges that proof to the existing atomic Work/WorkSession settlement writer.
11
+ * It cannot launch a provider, accept a review finding or close the Stage.
12
+ */
13
+ export class PreparedPublishedMemoryRepairWorkResult {
14
+ workId;
15
+ sha256;
16
+ #publication;
17
+ #binding;
18
+ constructor(key, publication, binding) {
19
+ if (key !== token)
20
+ refuse("published_result_token_required");
21
+ this.#publication = publication;
22
+ this.#binding = structuredClone(binding);
23
+ this.workId = binding.work_id;
24
+ this.sha256 = binding.result_sha256;
25
+ Object.freeze(this);
26
+ }
27
+ static isPrepared(value) {
28
+ return value instanceof PreparedPublishedMemoryRepairWorkResult && #publication in value;
29
+ }
30
+ static prepare(context, publication) {
31
+ if (!PreparedRetainedMemoryRepairPublication.isPrepared(publication))
32
+ refuse("registered_publication_required");
33
+ publication.revalidateApplied(context);
34
+ const binding = publication.settlementBinding(context);
35
+ const prepared = new PreparedPublishedMemoryRepairWorkResult(token, publication, binding);
36
+ prepared.assertSettlementScope(context, binding.session_id, binding.work_session_id);
37
+ return prepared;
38
+ }
39
+ bytes() { return this.#publication.bytes(); }
40
+ revalidate(context) {
41
+ this.#publication.revalidateApplied(context);
42
+ this.assertSettlementScope(context, this.#binding.session_id, this.#binding.work_session_id);
43
+ }
44
+ /** SQL-only check. The publication owner checks original review/allocation,
45
+ * generation, active Session/provider/CWD, start and completion custody.
46
+ */
47
+ assertSettlementScope(context, sessionId, workSessionId) {
48
+ if (sessionId !== this.#binding.session_id || workSessionId !== this.#binding.work_session_id
49
+ || hybridDigest(this.#publication.settlementBinding(context)) !== hybridDigest(this.#binding))
50
+ refuse("repair_settlement_scope_changed");
51
+ }
52
+ recordSettlement(context, sessionId, workSessionId) {
53
+ if (context.db.isOwnedWriteTransaction?.() !== true)
54
+ refuse("owned_settlement_writer_required");
55
+ this.assertSettlementScope(context, sessionId, workSessionId);
56
+ const event = "memory.repair_work_settlement", receipt = { schema_id: "dd-flow/memory-repair-work-settlement@1", ...this.#binding };
57
+ const select = () => context.db.all("SELECT payload_json FROM audit_events WHERE project_id=? AND event_type=? AND json_extract(payload_json,'$.run_id')=? AND json_extract(payload_json,'$.work_id')=? AND json_extract(payload_json,'$.generation')=? LIMIT 2", [this.#binding.project_id, event, this.#binding.run_id, this.workId, this.#binding.generation]);
58
+ if (select().length)
59
+ refuse("repair_settlement_already_recorded");
60
+ appendAudit(context, { projectId: this.#binding.project_id, eventType: event, payload: receipt });
61
+ const rows = select();
62
+ try {
63
+ if (rows.length !== 1 || Buffer.byteLength(rows[0].payload_json) > 16 * 1024
64
+ || hybridDigest(JSON.parse(rows[0].payload_json)) !== hybridDigest(receipt))
65
+ refuse("repair_settlement_readback_failed");
66
+ }
67
+ catch (error) {
68
+ if (error instanceof AppError)
69
+ throw error;
70
+ refuse("invalid_repair_settlement_readback");
71
+ }
72
+ }
73
+ }