@deksden-com/dd-flow-cli 0.9.0-beta.144 → 0.9.0-beta.149

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 (254) hide show
  1. package/CHANGELOG.md +130 -0
  2. package/README.md +13 -8
  3. package/dist/build-info.json +7 -7
  4. package/dist/cli/command-inputs.js +13 -3
  5. package/dist/cli/command-metadata.js +51 -16
  6. package/dist/cli/diagnostic-invocation.js +46 -0
  7. package/dist/cli/diagnostic.js +120 -0
  8. package/dist/cli/help.js +67 -1
  9. package/dist/cli/hook-ingress.js +27 -3
  10. package/dist/cli/memory-flow-admission.js +9 -3
  11. package/dist/cli/memory-init-invocation.js +10 -3
  12. package/dist/cli/memory-init.js +77 -68
  13. package/dist/cli/memory-storage-admission.js +3 -2
  14. package/dist/cli/memory-storage-qualification.js +105 -0
  15. package/dist/cli/memory-storage-transition.js +123 -0
  16. package/dist/cli/memory-upgrade.js +189 -0
  17. package/dist/cli/native-hook-ingress.js +14 -4
  18. package/dist/cli/native-hook-preparation.js +55 -0
  19. package/dist/cli/response-file.js +2 -1
  20. package/dist/cli/run-cli.js +79 -40
  21. package/dist/cli/setup.js +2 -1
  22. package/dist/cli.js +18 -0
  23. package/dist/domain/diagnostic-aspects.js +103 -0
  24. package/dist/domain/diagnostic-flows.js +8 -0
  25. package/dist/domain/stage-handler-identities.js +8 -0
  26. package/dist/harness-runtime/bin/dd-agy.mjs +3 -3
  27. package/dist/harness-runtime/bin/dd-grok.mjs +3 -3
  28. package/dist/harness-runtime/bin/dd-zcode.mjs +5 -5
  29. package/dist/harness-runtime/lib/agy-error-boundary.mjs +58 -0
  30. package/dist/harness-runtime/lib/agy-native-inspection.mjs +216 -0
  31. package/dist/harness-runtime/lib/agy-native_pb.js +117 -0
  32. package/dist/harness-runtime/lib/daemon-operations.d.mts +1 -0
  33. package/dist/harness-runtime/lib/daemon-operations.mjs +84 -10
  34. package/dist/harness-runtime/lib/dd-agy-daemon.mjs +366 -52
  35. package/dist/harness-runtime/lib/dd-agy.mjs +17 -3
  36. package/dist/harness-runtime/lib/dd-codex-daemon.mjs +27 -13
  37. package/dist/harness-runtime/lib/dd-codex.mjs +4 -0
  38. package/dist/harness-runtime/lib/dd-droid-daemon.mjs +4 -3
  39. package/dist/harness-runtime/lib/dd-droid.mjs +71 -29
  40. package/dist/harness-runtime/lib/dd-grok-daemon.mjs +73 -16
  41. package/dist/harness-runtime/lib/dd-grok.mjs +6 -3
  42. package/dist/harness-runtime/lib/dd-opencode-daemon.mjs +66 -20
  43. package/dist/harness-runtime/lib/dd-opencode.mjs +30 -3
  44. package/dist/harness-runtime/lib/dd-zcode-daemon.mjs +7 -6
  45. package/dist/harness-runtime/lib/dd-zcode.mjs +44 -14
  46. package/dist/harness-runtime/lib/delegation-instructions.mjs +1 -1
  47. package/dist/harness-runtime/lib/driver-recovery.mjs +9 -8
  48. package/dist/harness-runtime/lib/entity-references.d.mts +2 -0
  49. package/dist/harness-runtime/lib/entity-references.mjs +9 -0
  50. package/dist/harness-runtime/lib/managed-daemon.d.mts +1 -1
  51. package/dist/harness-runtime/lib/managed-daemon.mjs +24 -9
  52. package/dist/harness-runtime/lib/native-children.d.mts +8 -1
  53. package/dist/harness-runtime/lib/native-children.mjs +114 -8
  54. package/dist/harness-runtime/lib/native-hook-command.mjs +4 -4
  55. package/dist/harness-runtime/lib/native-request-proof.mjs +1 -1
  56. package/dist/harness-runtime/lib/observation-clock.d.mts +2 -2
  57. package/dist/harness-runtime/lib/observation-clock.mjs +7 -4
  58. package/dist/harness-runtime/lib/opencode-plugin.mjs +3 -3
  59. package/dist/harness-runtime/lib/operation-errors.mjs +1 -1
  60. package/dist/harness-runtime/lib/provider-overload-policy.d.mts +5 -3
  61. package/dist/harness-runtime/lib/provider-overload-policy.mjs +20 -7
  62. package/dist/harness-runtime/lib/runner-events.mjs +3 -1
  63. package/dist/harness-runtime/lib/setup-progress.mjs +8 -6
  64. package/dist/harness-runtime/lib/tool-observations.mjs +1 -1
  65. package/dist/schemas/diagnostic-stage-result.schema.json +354 -0
  66. package/dist/schemas/diagnostic-work-result.schema.json +296 -0
  67. package/dist/schemas/execution-bundle.schema.json +7 -1
  68. package/dist/schemas/mb-upgrade-stage-result.schema.json +863 -0
  69. package/dist/schemas/memory-repair-work-result.schema.json +9 -2
  70. package/dist/schemas/project-flow-pack-manifest.schema.json +1 -1
  71. package/dist/schemas/release-impact.schema.json +3 -3
  72. package/dist/schemas/stage-start-response.schema.json +4 -0
  73. package/dist/services/accepted-intent.js +13 -1
  74. package/dist/services/canon.js +43 -7
  75. package/dist/services/check-environment.js +21 -0
  76. package/dist/services/check-runner-evidence.js +267 -15
  77. package/dist/services/cli-operation-classifier.js +1 -1
  78. package/dist/services/cli-upgrade-allowlist.js +1 -1
  79. package/dist/services/code-checks.js +34 -19
  80. package/dist/services/compatibility-preflight.js +5 -2
  81. package/dist/services/controller-execution.js +10 -4
  82. package/dist/services/controller-fanout.js +34 -10
  83. package/dist/services/controller-memory-task.js +45 -0
  84. package/dist/services/controller-memory-worker.js +49 -0
  85. package/dist/services/dashboard.js +2 -2
  86. package/dist/services/descriptor-run-settlement.js +5 -3
  87. package/dist/services/descriptor-run-start.js +46 -20
  88. package/dist/services/descriptor-stage-coordinator.js +66 -13
  89. package/dist/services/descriptor-stage-settlement.js +2 -1
  90. package/dist/services/descriptor-stage-start.js +40 -4
  91. package/dist/services/diagnostic-evidence.js +73 -0
  92. package/dist/services/diagnostic-report-publication.js +72 -0
  93. package/dist/services/diagnostic-report.js +67 -0
  94. package/dist/services/diagnostic-run-bundle.js +74 -0
  95. package/dist/services/diagnostic-snapshot.js +159 -0
  96. package/dist/services/diagnostic-stage-handlers.js +108 -0
  97. package/dist/services/diagnostic-stage-results.js +132 -0
  98. package/dist/services/diagnostic-start.js +84 -0
  99. package/dist/services/diagnostic-work-batch.js +34 -0
  100. package/dist/services/diagnostic-work-collection.js +97 -0
  101. package/dist/services/diagnostic-work-registration.js +101 -0
  102. package/dist/services/diagnostic-work-results.js +93 -0
  103. package/dist/services/engines.js +64 -14
  104. package/dist/services/eval-snapshots.js +186 -119
  105. package/dist/services/execution-bundles.js +19 -7
  106. package/dist/services/external-work-launch.js +5 -3
  107. package/dist/services/flow-catalog.js +13 -4
  108. package/dist/services/frozen-engine.js +148 -11
  109. package/dist/services/hook-memory-routing.js +188 -0
  110. package/dist/services/hook-recovery-guard.js +14 -0
  111. package/dist/services/hook-session-observation.js +24 -0
  112. package/dist/services/hooks.js +27 -8
  113. package/dist/services/hybrid-contracts.js +4 -13
  114. package/dist/services/hybrid-digest.js +14 -0
  115. package/dist/services/installed-flow-source.js +47 -4
  116. package/dist/services/lifecycle-invocations.js +179 -38
  117. package/dist/services/memory-accepted-composition.js +5 -4
  118. package/dist/services/memory-completed-correction-wave.js +23 -15
  119. package/dist/services/memory-compose-handler.js +163 -21
  120. package/dist/services/memory-composition-acceptance.js +186 -10
  121. package/dist/services/memory-composition-graph.js +42 -11
  122. package/dist/services/memory-composition-registration.js +23 -8
  123. package/dist/services/memory-composition-work-custody.js +14 -9
  124. package/dist/services/memory-composition-work-results.js +15 -3
  125. package/dist/services/memory-corrected-review-batch.js +10 -3
  126. package/dist/services/memory-corrected-review-findings.js +4 -2
  127. package/dist/services/memory-corrected-review-inputs.js +10 -5
  128. package/dist/services/memory-corrected-review-receipt.js +17 -9
  129. package/dist/services/memory-corrected-review-registration.js +3 -2
  130. package/dist/services/memory-correction-check-receipt.js +20 -4
  131. package/dist/services/memory-custody-snapshot.js +36 -0
  132. package/dist/services/memory-finish-evidence.js +44 -22
  133. package/dist/services/memory-finish-handler.js +37 -23
  134. package/dist/services/memory-finish-integration.js +218 -48
  135. package/dist/services/memory-finish-readiness.js +66 -19
  136. package/dist/services/memory-git-integration.js +87 -18
  137. package/dist/services/memory-historical-corrected-review.js +21 -15
  138. package/dist/services/memory-historical-original-review.js +22 -9
  139. package/dist/services/memory-historical-repair-registration.js +14 -8
  140. package/dist/services/memory-historical-second-review.js +30 -11
  141. package/dist/services/memory-init-contexts.js +44 -2
  142. package/dist/services/memory-init-controller-start.js +27 -8
  143. package/dist/services/memory-init-git-policy.js +50 -21
  144. package/dist/services/memory-init-lint-capability.js +24 -6
  145. package/dist/services/memory-init-native-capacity.js +17 -4
  146. package/dist/services/memory-init-preflight.js +21 -17
  147. package/dist/services/memory-init-request-candidate.js +73 -17
  148. package/dist/services/memory-init-request-input.js +23 -7
  149. package/dist/services/memory-init-run-bundle.js +49 -12
  150. package/dist/services/memory-init-storage.js +17 -7
  151. package/dist/services/memory-init-worktrunk-capability.js +3 -7
  152. package/dist/services/memory-layer.js +71 -6
  153. package/dist/services/memory-lint-config.js +111 -2
  154. package/dist/services/memory-native-review-copy.js +5 -67
  155. package/dist/services/memory-normal-flow-entry.js +12 -0
  156. package/dist/services/memory-ordinary-defaults.js +22 -5
  157. package/dist/services/memory-preflight-handler.js +15 -0
  158. package/dist/services/memory-repair-current-view.js +1 -1
  159. package/dist/services/memory-repair-document-delta.js +70 -33
  160. package/dist/services/memory-repair-history.js +12 -7
  161. package/dist/services/memory-repair-physical-binding.js +1 -119
  162. package/dist/services/memory-repair-publication-plan.js +5 -3
  163. package/dist/services/memory-repair-publication-source.js +1 -1
  164. package/dist/services/memory-repair-publication.js +3 -2
  165. package/dist/services/memory-repair-registration.js +2 -1
  166. package/dist/services/memory-repair-work-baseline.js +4 -2
  167. package/dist/services/memory-repair-work-completion.js +32 -12
  168. package/dist/services/memory-repair-work-results.js +21 -2
  169. package/dist/services/memory-repair-workspace.js +11 -2
  170. package/dist/services/memory-repair-writer-copy.js +2 -1
  171. package/dist/services/memory-result-binding.js +78 -12
  172. package/dist/services/memory-retained-review-authority.js +17 -8
  173. package/dist/services/memory-retained-second-baseline.js +1 -1
  174. package/dist/services/memory-retained-second-repairs.js +12 -6
  175. package/dist/services/memory-review-correction-plan.js +47 -12
  176. package/dist/services/memory-review-handler.js +17 -17
  177. package/dist/services/memory-review-inputs.js +55 -9
  178. package/dist/services/memory-review-limits.js +10 -0
  179. package/dist/services/memory-review-receipt.js +2 -2
  180. package/dist/services/memory-review-registration.js +13 -4
  181. package/dist/services/memory-review-work-custody.js +3 -1
  182. package/dist/services/memory-review-work-input.js +26 -7
  183. package/dist/services/memory-review-work-results.js +26 -4
  184. package/dist/services/memory-second-corrected-review-inputs.js +5 -3
  185. package/dist/services/memory-second-correction-plan.js +2 -1
  186. package/dist/services/memory-second-repair-launch.js +4 -4
  187. package/dist/services/memory-second-repair-source.js +1 -1
  188. package/dist/services/memory-stage-dispatch.js +38 -1
  189. package/dist/services/memory-stage-results.js +113 -22
  190. package/dist/services/memory-tool-probe.js +25 -0
  191. package/dist/services/memory-upgrade-analysis-handler.js +243 -0
  192. package/dist/services/memory-upgrade-bootstrap-readiness.js +251 -0
  193. package/dist/services/memory-upgrade-evidence.js +100 -0
  194. package/dist/services/memory-upgrade-inventory.js +442 -0
  195. package/dist/services/memory-upgrade-publication-custody.js +73 -0
  196. package/dist/services/memory-upgrade-publication.js +413 -0
  197. package/dist/services/memory-upgrade-selection.js +430 -0
  198. package/dist/services/memory-upgrade-start.js +60 -0
  199. package/dist/services/memory-upgrade-storage-readiness.js +340 -0
  200. package/dist/services/memory-verification-acceptance.js +38 -17
  201. package/dist/services/memory-verification-checks.js +92 -27
  202. package/dist/services/memory-verification-receipt.js +6 -5
  203. package/dist/services/memory-verify-handler.js +15 -13
  204. package/dist/services/memory-work-result-json.js +12 -0
  205. package/dist/services/memory-work-result-preparation.js +5 -4
  206. package/dist/services/merge-server.js +4 -2
  207. package/dist/services/migrations.js +127 -98
  208. package/dist/services/native-daemon-history.js +2 -1
  209. package/dist/services/physical-startup.js +45 -0
  210. package/dist/services/project-registry.js +97 -0
  211. package/dist/services/project-summary.js +1 -1
  212. package/dist/services/projects.js +3 -93
  213. package/dist/services/prompt-delivery.js +71 -0
  214. package/dist/services/provider-capacity.js +112 -46
  215. package/dist/services/recovery-snapshot-database.js +32 -4
  216. package/dist/services/release-cache.js +8 -0
  217. package/dist/services/repair-intents.js +1 -12
  218. package/dist/services/repair-receipt.js +13 -0
  219. package/dist/services/run-control-worker.js +35 -18
  220. package/dist/services/run-control.js +2 -1
  221. package/dist/services/run-controller-adapter.js +2 -1
  222. package/dist/services/run-controller-process.js +4 -7
  223. package/dist/services/run-controller-recovery.js +8 -2
  224. package/dist/services/run-controller.js +215 -94
  225. package/dist/services/run-fork.js +1 -1
  226. package/dist/services/run-recovery-guard.js +30 -0
  227. package/dist/services/run-recovery.js +11 -44
  228. package/dist/services/run-start-custody.js +17 -5
  229. package/dist/services/runs.js +6 -1
  230. package/dist/services/runtime-scope-resume.js +12 -6
  231. package/dist/services/runtime-scope-stop.js +3 -2
  232. package/dist/services/runtime-scope-worker.js +46 -19
  233. package/dist/services/runtime-service.js +2 -1
  234. package/dist/services/sessions.js +3 -24
  235. package/dist/services/setup.js +63 -13
  236. package/dist/services/stage-handlers.js +22 -7
  237. package/dist/services/stage-pause.js +111 -44
  238. package/dist/services/version-status.js +1 -1
  239. package/dist/services/vnext-code.js +2 -2
  240. package/dist/services/vnext-fanout.js +51 -9
  241. package/dist/services/vnext-plan-review.js +3 -5
  242. package/dist/services/work-registry.js +117 -39
  243. package/dist/services/workspace-bootstrap.js +2 -1
  244. package/dist/services/workspace-files.js +1 -1
  245. package/dist/shared/entity-references.js +1 -8
  246. package/dist/storage/data-preservation.js +65 -0
  247. package/dist/storage/database.js +53 -19
  248. package/dist/storage/import-guard.js +84 -27
  249. package/dist/storage/memory-upgrade-schema-bootstrap.js +38 -0
  250. package/dist/storage/paired-writer-migration.js +424 -0
  251. package/dist/storage/writer-contract.js +11 -4
  252. package/dist/storage/writer-migration.js +313 -52
  253. package/dist/storage/writer-restore.js +217 -0
  254. package/package.json +13 -6
@@ -13,14 +13,23 @@ import { ObservedMemorySources } from "./memory-source-observations.js";
13
13
  import { memorySourceKey, PreparedMemoryStageResult } from "./memory-stage-results.js";
14
14
  import { PreparedDescriptorStageSettlement } from "./descriptor-stage-settlement.js";
15
15
  import { assertPreparedRunBundleSqlAuthority } from "./runs.js";
16
- import { readMemoryInitBootstrapReadiness } from "./memory-init-bootstrap-readiness.js";
17
- import { readMemoryInitGitPolicy } from "./memory-init-git-policy.js";
18
16
  import { reviewedMemoryGitFiles } from "./memory-git-integration.js";
17
+ import { readMemoryFinishGitSelection, readMemoryUpgradeFinishInventory } from "./memory-finish-integration.js";
19
18
  const token = Symbol("memory-finish-evidence"), event = "memory.finish_evidence";
20
19
  const hash = (bytes) => createHash("sha256").update(bytes).digest("hex");
21
20
  function refuse(reason) {
22
21
  throw new AppError("memory_finish_evidence_invalid", "Finish evidence lacks independently prepared acceptance custody", 2, { reason, effect: "no_effect" });
23
22
  }
23
+ export function parseMemoryFinishProposal(bytes) {
24
+ if (!(bytes instanceof Uint8Array) || !bytes.length || bytes.length > 1024 * 1024)
25
+ refuse("invalid_result_bytes");
26
+ try {
27
+ return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
28
+ }
29
+ catch {
30
+ throw new AppError("memory_finish_evidence_invalid", "Finish proposal must be valid UTF-8 JSON. Correct only the proposal syntax, then use retry_command; do not rerun integration or settled checks", 2, { reason: "invalid_result_json", phase: "prepare", effect: "no_effect", recoverable: true, result_sha256: hash(bytes) });
31
+ }
32
+ }
24
33
  /** Terminal inventory and receipts belong to the accepted review/evidence
25
34
  * owners. Optional echoes cannot introduce new references or semantic claims. */
26
35
  export function projectMemoryFinishResult(value, owner, resultSha256) {
@@ -77,11 +86,24 @@ function acceptance(context, scope) {
77
86
  refuse("ambiguous_finish_acceptance");
78
87
  return rows[0] ? JSON.parse(rows[0].payload_json) : null;
79
88
  }
89
+ /** Terminal parsing inherits the accepted scope and named old-data criteria;
90
+ * no fresh version inference or mutable project sources are consulted. */
91
+ function upgradeBinding(context, scope, bundle) {
92
+ if (bundle.descriptor.descriptor.id !== "mb-upgrade")
93
+ refuse("upgrade_preservation_binding_changed");
94
+ const preflight = readAcceptedStageData(context, { ...scope, stage: "preflight" });
95
+ const diff = readAcceptedStageData(context, { ...scope, stage: "diff-analysis" });
96
+ const preservationInvariants = JSON.parse(preflight.bytes.toString("utf8")).details.preservation_invariants;
97
+ const minimumMode = JSON.parse(diff.bytes.toString("utf8")).details.mode;
98
+ if (!["patch", "targeted_minor", "full_migration"].includes(minimumMode) || !Array.isArray(preservationInvariants))
99
+ refuse("upgrade_preservation_binding_changed");
100
+ return { minimumMode, preservationInvariants };
101
+ }
80
102
  /** SQL-only historical byte/projection custody. Callers must independently
81
103
  * prepare the genuine accepted review owner outside their writer. This does
82
104
  * not re-observe today's integration checkout or grant RUN closure. */
83
105
  export function readAcceptedMemoryFinishEvidence(context, input) {
84
- if (!(input.bundle instanceof RetainedExecutionBundle) || input.bundle.descriptor.descriptor.id !== "mb-init"
106
+ if (!(input.bundle instanceof RetainedExecutionBundle) || !["mb-init", "mb-upgrade"].includes(input.bundle.descriptor.descriptor.id)
85
107
  || !PreparedMemoryFinishReview.isPrepared(input.review))
86
108
  refuse("registered_memory_review_required");
87
109
  const scope = { ...input.scope, stage: "finish" };
@@ -96,7 +118,7 @@ export function readAcceptedMemoryFinishEvidence(context, input) {
96
118
  || evidence.attempt !== stage.attempt || evidence.input_sha256 !== stage.input_sha256
97
119
  || evidence.bundle_sha256 !== input.bundle.sha256 || evidence.review_sha256 !== reviewData.result.sha256
98
120
  || !input.review.bytes().equals(reviewData.bytes)
99
- || data.data_schema_id !== "dd-flow/mb-init-finish-result@1" || data.input_sha256 !== stage.input_sha256
121
+ || data.data_schema_id !== (input.bundle.descriptor.descriptor.id === "mb-upgrade" ? "dd-flow/mb-upgrade-stage-result@1" : "dd-flow/mb-init-finish-result@1") || data.input_sha256 !== stage.input_sha256
100
122
  || hybridDigest(evidence.checks) !== hybridDigest(input.review.finalChecks(context)))
101
123
  refuse("accepted_finish_evidence_changed");
102
124
  const expectedAcceptance = { schema_id: "dd-flow/memory-finish-acceptance@1", project_id: scope.projectId, run_id: scope.runId,
@@ -134,16 +156,18 @@ export function readAcceptedMemoryFinishEvidence(context, input) {
134
156
  refuse("historical_evidence_projection_changed");
135
157
  const frozen = input.bundle.execution.snapshot().scope;
136
158
  const request = { requestId: frozen.requestId, projectRoot: frozen.projectRoot, workspaceRoot: frozen.workspace };
137
- const bootstrap = readMemoryInitBootstrapReadiness(context, request), policy = readMemoryInitGitPolicy(context, request);
138
- if (!bootstrap || !policy || bootstrap.projectId !== scope.projectId
139
- || bootstrap.authority.binding.execution_sha256 !== input.bundle.execution.sha256
140
- || bootstrap.authority.binding.git_policy_sha256 !== hybridDigest(policy)
141
- || policy.target_sha256 !== hybridDigest(bootstrap.authority.binding.target)
142
- || readiness.value.bootstrap_authority_sha256 !== hybridDigest(bootstrap.authority)
143
- || readiness.value.pack_sha256 !== bootstrap.authority.binding.pack_sha256)
159
+ const selected = readMemoryFinishGitSelection(context, scope, input.bundle), policy = selected.policy;
160
+ const packSha256 = selected.kind === "init" ? selected.readiness.authority.binding.pack_sha256 : selected.readiness.authority.receipt.upgrade_target.pack_sha256;
161
+ if (readiness.value.bootstrap_authority_sha256 !== hybridDigest(selected.readiness.authority)
162
+ || readiness.value.pack_sha256 !== packSha256)
144
163
  refuse("historical_terminal_bootstrap_policy_changed");
145
164
  const integrated = integration.value;
146
- const reviewedFiles = reviewedMemoryGitFiles(input.review.reviewedSources(context));
165
+ const upgrade = selected.kind === "upgrade" ? readMemoryUpgradeFinishInventory(context, scope, input.bundle, input.review) : null;
166
+ const reviewedFiles = upgrade?.references ?? reviewedMemoryGitFiles(input.review.reviewedSources(context));
167
+ if (upgrade && (integrated.upgrade_inventory_sha256 !== hybridDigest(upgrade)
168
+ || integrated.removed_files_sha256 !== (upgrade.removals.length ? hybridDigest(upgrade.removals) : undefined)
169
+ || hybridDigest([readiness.value.migration_record]) !== hybridDigest([upgrade.migrationRecord])))
170
+ refuse("historical_upgrade_inventory_changed");
147
171
  if (integrated.schema_id !== "dd-flow/memory-git-integration-observation@1"
148
172
  || integrated.project_root !== request.projectRoot || integrated.workspace_root !== request.workspaceRoot
149
173
  || integrated.repository !== policy.repository || integrated.workspace_branch !== policy.workspace_branch
@@ -167,11 +191,13 @@ export function readAcceptedMemoryFinishEvidence(context, input) {
167
191
  runId: scope.runId, stage: "finish", inputSha256: stage.input_sha256,
168
192
  predecessors: [{ stage: "review", result: reviewData.result, input_sha256: reviewData.input_sha256 }],
169
193
  unitIds: reviewed.units.map((unit) => unit.id), sources,
194
+ ...(upgrade ? { upgrade: upgradeBinding(context, scope, input.bundle) } : {}),
170
195
  historicalEvidence: new Set(reviewed.units.flatMap((unit) => unit.evidence.map(ref => `${memorySourceKey(ref.root, ref.path)}\0${ref.sha256}`)))
171
196
  });
172
197
  const value = result.value(), references = { coverage: evidence.coverage,
173
198
  checks: evidence.checks.map(({ path, sha256 }) => ({ path, sha256 })),
174
- integration: evidence.integration, normal_flow_readiness: evidence.normal_flow_readiness };
199
+ integration: evidence.integration, normal_flow_readiness: evidence.normal_flow_readiness,
200
+ ...(upgrade ? { migration_record: upgrade.migrationRecord } : {}) };
175
201
  if (value.outcome !== "accepted" || hybridDigest(value.units) !== hybridDigest(reviewed.units)
176
202
  || hybridDigest(value.findings) !== hybridDigest(reviewed.findings) || hybridDigest(value.details) !== hybridDigest(references))
177
203
  refuse("historical_finish_result_projection_changed");
@@ -209,7 +235,7 @@ export class PreparedMemoryFinishEvidence {
209
235
  Object.freeze(this);
210
236
  }
211
237
  static prepare(context, input) {
212
- if (!(input.bundle instanceof RetainedExecutionBundle) || input.bundle.descriptor.descriptor.id !== "mb-init")
238
+ if (!(input.bundle instanceof RetainedExecutionBundle) || !["mb-init", "mb-upgrade"].includes(input.bundle.descriptor.descriptor.id))
213
239
  refuse("registered_memory_bundle_required");
214
240
  if (context.db.isOwnedWriteTransaction?.())
215
241
  refuse("preparation_outside_writer_required");
@@ -258,7 +284,8 @@ export class PreparedMemoryFinishEvidence {
258
284
  references() {
259
285
  const value = this.#receipt;
260
286
  return { coverage: { ...value.coverage }, checks: value.checks.map(({ path, sha256 }) => ({ path, sha256 })),
261
- integration: { ...value.integration }, normal_flow_readiness: { ...value.normal_flow_readiness } };
287
+ integration: { ...value.integration }, normal_flow_readiness: { ...value.normal_flow_readiness },
288
+ ...(this.#bundle.descriptor.descriptor.id === "mb-upgrade" ? { migration_record: this.#readiness.value().migration_record } : {}) };
262
289
  }
263
290
  /** Fresh terminal result preparation, never a historical replay reader.
264
291
  * The proposal cannot replace accepted review coverage or CLI evidence. */
@@ -274,18 +301,13 @@ export class PreparedMemoryFinishEvidence {
274
301
  if (typeof review.input_sha256 !== "string")
275
302
  refuse("accepted_review_input_missing");
276
303
  const reviewed = JSON.parse(this.#review.bytes().toString("utf8"));
277
- let proposal;
278
- try {
279
- proposal = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
280
- }
281
- catch {
282
- refuse("invalid_result_bytes");
283
- }
304
+ const proposal = parseMemoryFinishProposal(bytes);
284
305
  const canonical = projectMemoryFinishResult(proposal, { units: reviewed.units, findings: reviewed.findings, details: this.references() }, hash(bytes));
285
306
  const result = PreparedMemoryStageResult.prepare(Buffer.from(JSON.stringify(canonical)), bundle.descriptor, {
286
307
  runId: this.#scope.runId, stage: "finish", inputSha256: this.#input.inputSha256,
287
308
  predecessors: [{ stage: "review", result: review.result, input_sha256: review.input_sha256 }],
288
309
  unitIds: reviewed.units.map((unit) => unit.id), sources: sources.hashes(),
310
+ ...(bundle.descriptor.descriptor.id === "mb-upgrade" ? { upgrade: upgradeBinding(context, this.#scope, bundle) } : {}),
289
311
  historicalEvidence: new Set(reviewed.units.flatMap((unit) => unit.evidence.map(ref => `${memorySourceKey(ref.root, ref.path)}\0${ref.sha256}`)))
290
312
  });
291
313
  const value = result.value();
@@ -1,3 +1,4 @@
1
+ import { assertMemorySqlCustody, withMemoryCustodySnapshot } from "./memory-custody-snapshot.js";
1
2
  import { AppError } from "../shared/errors.js";
2
3
  import { RetainedExecutionBundle } from "./execution-bundles.js";
3
4
  import { readAcceptedStageData } from "./stage-report-renderer.js";
@@ -32,6 +33,7 @@ function selected(context, scope) {
32
33
  /** Closed review predecessor proof only. It does not establish integration,
33
34
  * normal-flow readiness, terminal Stage acceptance or RUN completion. */
34
35
  export class PreparedMemoryFinishReview {
36
+ #custodyKey = {};
35
37
  static isPrepared(value) {
36
38
  return typeof value === "object" && value !== null && #review in value;
37
39
  }
@@ -51,40 +53,52 @@ export class PreparedMemoryFinishReview {
51
53
  Object.freeze(this);
52
54
  }
53
55
  static prepare(context, input) {
54
- if (!(input.bundle instanceof RetainedExecutionBundle) || input.bundle.descriptor.descriptor.id !== "mb-init"
56
+ if (!(input.bundle instanceof RetainedExecutionBundle) || !["mb-init", "mb-upgrade"].includes(input.bundle.descriptor.descriptor.id)
55
57
  || input.bundle.descriptor.successor("review", "accepted") !== "finish")
56
58
  refuse("registered_memory_bundle_required");
57
59
  if (context.db.isOwnedWriteTransaction?.())
58
60
  refuse("preparation_outside_writer_required");
59
- assertPreparedRunBundleSqlAuthority(context, input.scope, input.bundle);
60
- const data = context.db.readSnapshot(() => selected(context, input.scope));
61
- const review = data.corrected
62
- ? PreparedAcceptedMemoryCorrectedReview.prepare(context, { ...input, bytes: data.data.bytes })
63
- : PreparedAcceptedMemoryInitialReview.prepare(context, { ...input, bytes: data.data.bytes });
64
- const prepared = new PreparedMemoryFinishReview(token, input.scope, review, data);
65
- prepared.revalidate(context);
66
- return prepared;
61
+ return withMemoryCustodySnapshot(context, () => {
62
+ assertPreparedRunBundleSqlAuthority(context, input.scope, input.bundle);
63
+ const data = context.db.readSnapshot(() => selected(context, input.scope));
64
+ const review = data.corrected
65
+ ? PreparedAcceptedMemoryCorrectedReview.prepare(context, { ...input, bytes: data.data.bytes })
66
+ : PreparedAcceptedMemoryInitialReview.prepare(context, { ...input, bytes: data.data.bytes });
67
+ const prepared = new PreparedMemoryFinishReview(token, input.scope, review, data);
68
+ prepared.revalidate(context);
69
+ return prepared;
70
+ });
67
71
  }
68
72
  bytes() { return Buffer.from(this.#bytes); }
69
73
  reviewedSources(context) { this.assertCustody(context); return this.#review.reviewedSources(context); }
74
+ reviewedDocumentInventory(context) {
75
+ this.assertCustody(context);
76
+ return this.#review instanceof PreparedAcceptedMemoryCorrectedReview ? this.#review.settledDocumentBaseline(context).entries() : null;
77
+ }
70
78
  finalChecks(context) { this.assertCustody(context); return this.#review.finalChecks(context); }
71
79
  assertCustody(context) {
72
- this.#review.assertCustody(context);
73
- const data = selected(context, this.#scope);
74
- if (data.corrected !== this.#corrected || data.data.authority_sha256 !== this.#authority
75
- || !data.data.bytes.equals(this.#bytes))
76
- refuse("accepted_review_changed");
80
+ const review = this.#review;
81
+ assertMemorySqlCustody(context, this.#custodyKey, () => {
82
+ review.assertCustody(context);
83
+ const data = selected(context, this.#scope);
84
+ if (data.corrected !== this.#corrected || data.data.authority_sha256 !== this.#authority
85
+ || !data.data.bytes.equals(this.#bytes))
86
+ refuse("accepted_review_changed");
87
+ });
88
+ }
89
+ revalidate(context) {
90
+ const review = this.#review;
91
+ withMemoryCustodySnapshot(context, () => { review.revalidate(context); this.assertCustody(context); });
77
92
  }
78
- revalidate(context) { this.#review.revalidate(context); this.assertCustody(context); }
79
93
  }
80
- /** Productive terminal entry awaiting the complete six-stage registry. Opens
81
- * only after historical review proof; no provider dispatch or Git mutation.
94
+ /** Shared terminal entry for registered Memory flows. Opens only after
95
+ * historical review proof; no provider dispatch or Git mutation.
82
96
  * Terminal settlement must separately prove integration and readiness. */
83
97
  export function startMemoryFinish(context, input) {
84
98
  if (!(input.bytes instanceof Uint8Array) || input.bytes.length > 1024 * 1024)
85
99
  refuse("invalid_input_bytes");
86
100
  const bundle = readFlowRunExecutionBundle(context, input);
87
- if (!bundle || bundle.descriptor.descriptor.id !== "mb-init" || !bundle.descriptor.descriptor.terminal_stages.includes("finish")
101
+ if (!bundle || !["mb-init", "mb-upgrade"].includes(bundle.descriptor.descriptor.id) || !bundle.descriptor.descriptor.terminal_stages.includes("finish")
88
102
  || bundle.descriptor.successor("review", "accepted") !== "finish")
89
103
  refuse("registered_memory_bundle_required");
90
104
  const project = requireProjectByRoot(context, resolveProjectRoot(input.projectRoot)), scope = { projectId: project.id, runId: input.runId };
@@ -107,7 +121,7 @@ export function replayAcceptedMemoryFinish(context, input) {
107
121
  if (context.db.isOwnedWriteTransaction?.())
108
122
  refuse("preparation_outside_writer_required");
109
123
  const bundle = readFlowRunExecutionBundle(context, input);
110
- if (!bundle || bundle.descriptor.descriptor.id !== "mb-init")
124
+ if (!bundle || !["mb-init", "mb-upgrade"].includes(bundle.descriptor.descriptor.id))
111
125
  refuse("registered_memory_bundle_required");
112
126
  const project = requireProjectByRoot(context, resolveProjectRoot(input.projectRoot)), scope = { projectId: project.id, runId: input.runId };
113
127
  const review = PreparedMemoryFinishReview.prepare(context, { scope, bundle });
@@ -149,7 +163,7 @@ export function settleAcceptedMemoryRun(context, input) {
149
163
  if (context.db.isOwnedWriteTransaction?.())
150
164
  refuse("preparation_outside_writer_required");
151
165
  const bundle = readFlowRunExecutionBundle(context, input);
152
- if (!bundle || bundle.descriptor.descriptor.id !== "mb-init"
166
+ if (!bundle || !["mb-init", "mb-upgrade"].includes(bundle.descriptor.descriptor.id)
153
167
  || !bundle.descriptor.descriptor.terminal_stages.includes("finish"))
154
168
  refuse("registered_memory_bundle_required");
155
169
  const project = requireProjectByRoot(context, resolveProjectRoot(input.projectRoot)), scope = { projectId: project.id, runId: input.runId };
@@ -175,14 +189,14 @@ export function settleAcceptedMemoryRun(context, input) {
175
189
  * Publication has its own exact receipt and does not accept the Stage. */
176
190
  export function registerMemoryFinishEvidenceFromStage(context, input) {
177
191
  const bundle = readFlowRunExecutionBundle(context, input);
178
- if (!bundle || bundle.descriptor.descriptor.id !== "mb-init")
192
+ if (!bundle || !["mb-init", "mb-upgrade"].includes(bundle.descriptor.descriptor.id))
179
193
  refuse("registered_memory_bundle_required");
180
194
  const project = requireProjectByRoot(context, resolveProjectRoot(input.projectRoot));
181
195
  return PreparedMemoryFinishEvidence.prepare(context, { scope: { projectId: project.id, runId: input.runId }, bundle }).register(context);
182
196
  }
183
197
  export function integrateMemoryFinishFromStage(context, input) {
184
198
  const bundle = readFlowRunExecutionBundle(context, input);
185
- if (!bundle || bundle.descriptor.descriptor.id !== "mb-init")
199
+ if (!bundle || !["mb-init", "mb-upgrade"].includes(bundle.descriptor.descriptor.id))
186
200
  refuse("registered_memory_bundle_required");
187
201
  const project = requireProjectByRoot(context, resolveProjectRoot(input.projectRoot));
188
202
  return integrateMemoryFinish(context, { scope: { projectId: project.id, runId: input.runId }, bundle });
@@ -196,7 +210,7 @@ export function finishMemoryFinish(context, input) {
196
210
  if (context.db.isOwnedWriteTransaction?.())
197
211
  refuse("preparation_outside_writer_required");
198
212
  const bundle = readFlowRunExecutionBundle(context, input);
199
- if (!bundle || bundle.descriptor.descriptor.id !== "mb-init")
213
+ if (!bundle || !["mb-init", "mb-upgrade"].includes(bundle.descriptor.descriptor.id))
200
214
  refuse("registered_memory_bundle_required");
201
215
  const project = requireProjectByRoot(context, resolveProjectRoot(input.projectRoot)), scope = { projectId: project.id, runId: input.runId };
202
216
  const run = assertPreparedRunBundleSqlAuthority(context, scope, bundle);