@sema-agent/core 7.62.0 → 7.64.0

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 (320) hide show
  1. package/CHANGELOG.md +936 -0
  2. package/dist/agents/cc-peer-directory.js +1 -0
  3. package/dist/agents/cc-peer-wire.d.ts +4 -0
  4. package/dist/agents/cc-peer-wire.js +1 -0
  5. package/dist/agents/cc-uds-peer-channel.js +5 -0
  6. package/dist/agents/cross-session-judge.d.ts +3 -1
  7. package/dist/agents/cross-session-judge.js +4 -1
  8. package/dist/agents/peer-directory.d.ts +8 -0
  9. package/dist/agents/peer-directory.js +4 -0
  10. package/dist/agents/peer-held-queue.d.ts +9 -3
  11. package/dist/agents/peer-held-queue.js +12 -9
  12. package/dist/agents/peer-idle.d.ts +20 -1
  13. package/dist/agents/peer-idle.js +23 -12
  14. package/dist/agents/peer-notice-route.d.ts +16 -5
  15. package/dist/agents/peer-notice-route.js +11 -9
  16. package/dist/agents/peer-notices.d.ts +25 -8
  17. package/dist/agents/peer-notices.js +6 -1
  18. package/dist/agents/peer-session-drain.d.ts +32 -6
  19. package/dist/agents/peer-session-drain.js +100 -26
  20. package/dist/agents/send-message-tool.d.ts +7 -0
  21. package/dist/agents/send-message-tool.js +18 -6
  22. package/dist/agents/subagent-transcript-placement.d.ts +23 -0
  23. package/dist/agents/subagent-transcript-placement.js +14 -0
  24. package/dist/agents/subagent.js +102 -54
  25. package/dist/brain/route-adjudicator.js +9 -4
  26. package/dist/core/allow-entry.d.ts +4 -0
  27. package/dist/core/approval-object.js +1 -1
  28. package/dist/core/arg-summary.js +1 -1
  29. package/dist/core/ask-origin.d.ts +1 -1
  30. package/dist/core/ask-origin.js +2 -2
  31. package/dist/core/ask-question.d.ts +2 -2
  32. package/dist/core/ask-unresolvable-notice.d.ts +13 -4
  33. package/dist/core/ask-unresolvable-notice.js +10 -3
  34. package/dist/core/auto-compaction.d.ts +7 -5
  35. package/dist/core/background-agent-store.d.ts +7 -0
  36. package/dist/core/background-agent-store.js +3 -0
  37. package/dist/core/canonical-json.d.ts +23 -13
  38. package/dist/core/canonical-json.js +4 -4
  39. package/dist/core/checkpoint-queries.d.ts +50 -4
  40. package/dist/core/checkpoint-queries.js +42 -17
  41. package/dist/core/checkpoint-requirements.d.ts +3 -3
  42. package/dist/core/checkpoint-requirements.js +2 -1
  43. package/dist/core/checkpoint-row-verdict.d.ts +15 -4
  44. package/dist/core/checkpoint-row-verdict.js +22 -2
  45. package/dist/core/checkpoint-store.d.ts +28 -11
  46. package/dist/core/checkpoint-store.js +45 -4
  47. package/dist/core/compliance.d.ts +6 -2
  48. package/dist/core/compliance.js +3 -3
  49. package/dist/core/context-settings.d.ts +40 -9
  50. package/dist/core/context-settings.js +32 -10
  51. package/dist/core/engine-attestation.d.ts +27 -7
  52. package/dist/core/engine-attestation.js +5 -1
  53. package/dist/core/engine-notice.d.ts +43 -5
  54. package/dist/core/extension-requester.d.ts +9 -0
  55. package/dist/core/extension-requester.js +7 -0
  56. package/dist/core/file-history-store.d.ts +28 -0
  57. package/dist/core/file-history-store.js +48 -5
  58. package/dist/core/fs-write-gate-policy.js +1 -1
  59. package/dist/core/gate-fold.js +2 -0
  60. package/dist/core/gate-lanes.js +2 -0
  61. package/dist/core/gate-outcome.d.ts +39 -2
  62. package/dist/core/gate-outcome.js +20 -1
  63. package/dist/core/git-worktree-env.d.ts +62 -3
  64. package/dist/core/git-worktree-env.js +91 -30
  65. package/dist/core/governance-codes.d.ts +9 -3
  66. package/dist/core/governance-codes.js +17 -3
  67. package/dist/core/leg-identity.d.ts +19 -1
  68. package/dist/core/leg-identity.js +12 -0
  69. package/dist/core/mailbox-store.d.ts +39 -4
  70. package/dist/core/mailbox-store.js +14 -2
  71. package/dist/core/memory-engine/consolidation-driver.d.ts +32 -17
  72. package/dist/core/memory-engine/consolidation-driver.js +99 -98
  73. package/dist/core/memory-engine/consolidation.d.ts +74 -10
  74. package/dist/core/memory-engine/consolidation.js +183 -11
  75. package/dist/core/memory-engine/control-objects.d.ts +25 -0
  76. package/dist/core/memory-engine/control-objects.js +151 -0
  77. package/dist/core/memory-engine/control-plane-adoption.d.ts +48 -0
  78. package/dist/core/memory-engine/control-plane-adoption.js +159 -0
  79. package/dist/core/memory-engine/data-plane.d.ts +8 -4
  80. package/dist/core/memory-engine/data-plane.js +11 -6
  81. package/dist/core/memory-engine/delegation-settlement.d.ts +58 -40
  82. package/dist/core/memory-engine/delegation-settlement.js +270 -85
  83. package/dist/core/memory-engine/dual-root.d.ts +12 -6
  84. package/dist/core/memory-engine/dual-root.js +11 -7
  85. package/dist/core/memory-engine/engine.d.ts +136 -58
  86. package/dist/core/memory-engine/engine.js +491 -394
  87. package/dist/core/memory-engine/entry-scan.d.ts +8 -0
  88. package/dist/core/memory-engine/entry-scan.js +3 -0
  89. package/dist/core/memory-engine/file-backend.d.ts +62 -60
  90. package/dist/core/memory-engine/file-backend.js +122 -198
  91. package/dist/core/memory-engine/index.d.ts +8 -7
  92. package/dist/core/memory-engine/index.js +7 -6
  93. package/dist/core/memory-engine/landing-admission.d.ts +5 -3
  94. package/dist/core/memory-engine/landing-admission.js +3 -3
  95. package/dist/core/memory-engine/layout.d.ts +127 -46
  96. package/dist/core/memory-engine/layout.js +264 -109
  97. package/dist/core/memory-engine/memory-backend-contract.d.ts +26 -1
  98. package/dist/core/memory-engine/memory-backend-contract.js +476 -0
  99. package/dist/core/memory-engine/memory-deployment.d.ts +42 -11
  100. package/dist/core/memory-engine/memory-deployment.js +54 -15
  101. package/dist/core/memory-engine/origin-clearance.d.ts +12 -3
  102. package/dist/core/memory-engine/origin-clearance.js +68 -9
  103. package/dist/core/memory-engine/scope-contract.d.ts +52 -11
  104. package/dist/core/memory-engine/scope-contract.js +68 -19
  105. package/dist/core/memory-engine/session-write-home.d.ts +37 -2
  106. package/dist/core/memory-engine/session-write-home.js +38 -11
  107. package/dist/core/memory-engine/sync-client.d.ts +3 -1
  108. package/dist/core/memory-engine/sync-client.js +7 -0
  109. package/dist/core/memory-engine/sync-report.d.ts +3 -2
  110. package/dist/core/memory-engine/sync-report.js +6 -2
  111. package/dist/core/memory-engine/tools.d.ts +2 -2
  112. package/dist/core/memory-engine/tools.js +10 -10
  113. package/dist/core/memory-engine/types.d.ts +155 -19
  114. package/dist/core/memory-engine/types.js +2 -0
  115. package/dist/core/memory.js +15 -6
  116. package/dist/core/mode-policy.js +3 -3
  117. package/dist/core/mods/agent-stations.d.ts +18 -0
  118. package/dist/core/mods/agent-stations.js +8 -6
  119. package/dist/core/mods/extension-call.d.ts +4 -0
  120. package/dist/core/mods/extension-call.js +8 -0
  121. package/dist/core/mods/stations.d.ts +20 -1
  122. package/dist/core/mods/stations.js +21 -2
  123. package/dist/core/mods/tool-call.js +3 -4
  124. package/dist/core/parent-spec-seat.d.ts +2 -2
  125. package/dist/core/park-selfcheck.js +2 -2
  126. package/dist/core/pause-registry.d.ts +2 -0
  127. package/dist/core/pause-registry.js +3 -0
  128. package/dist/core/peer-channel.d.ts +5 -0
  129. package/dist/core/permission-rule-consent.d.ts +26 -8
  130. package/dist/core/permission-rule-consent.js +76 -36
  131. package/dist/core/permission-rule-store.d.ts +72 -13
  132. package/dist/core/permission-rule-store.js +29 -8
  133. package/dist/core/read-only-shell.js +3 -2
  134. package/dist/core/redaction-marks.d.ts +8 -0
  135. package/dist/core/redaction-marks.js +6 -0
  136. package/dist/core/refusal-echo.d.ts +20 -0
  137. package/dist/core/refusal-echo.js +33 -0
  138. package/dist/core/remote-env.d.ts +12 -1
  139. package/dist/core/remote-env.js +3 -0
  140. package/dist/core/retention.d.ts +180 -10
  141. package/dist/core/retention.js +45 -0
  142. package/dist/core/retired-keys.d.ts +5 -0
  143. package/dist/core/retired-keys.js +3 -0
  144. package/dist/core/rule-offer-read.d.ts +0 -4
  145. package/dist/core/rule-offer-read.js +2 -18
  146. package/dist/core/runner/child-tree.d.ts +29 -3
  147. package/dist/core/runner/child-tree.js +39 -16
  148. package/dist/core/runner/classifier-ladder.d.ts +7 -1
  149. package/dist/core/runner/classifier-ladder.js +4 -2
  150. package/dist/core/runner/compact-session.js +10 -7
  151. package/dist/core/runner/compaction-call-options.d.ts +7 -7
  152. package/dist/core/runner/compaction-call-options.js +3 -2
  153. package/dist/core/runner/contracts.d.ts +39 -11
  154. package/dist/core/runner/delegation-owner.d.ts +10 -3
  155. package/dist/core/runner/delegation-owner.js +4 -1
  156. package/dist/core/runner/gate-exit.d.ts +6 -0
  157. package/dist/core/runner/gate-exit.js +13 -4
  158. package/dist/core/runner/inherited-ask-grants.js +1 -1
  159. package/dist/core/runner/memory-consolidation-driver.d.ts +10 -5
  160. package/dist/core/runner/memory-consolidation-driver.js +12 -9
  161. package/dist/core/runner/memory-consolidation-write-judge.d.ts +2 -1
  162. package/dist/core/runner/memory-consolidation-write-judge.js +3 -2
  163. package/dist/core/runner/person-path-rows.d.ts +8 -3
  164. package/dist/core/runner/person-path-rows.js +5 -1
  165. package/dist/core/runner/placement-fence.d.ts +12 -0
  166. package/dist/core/runner/placement-fence.js +20 -6
  167. package/dist/core/runner/prepare-ask-lane.js +5 -5
  168. package/dist/core/runner/prepare-caps-and-workflow.d.ts +7 -2
  169. package/dist/core/runner/prepare-caps-and-workflow.js +7 -4
  170. package/dist/core/runner/prepare-config-doors.js +4 -0
  171. package/dist/core/runner/prepare-delegation-surface.d.ts +3 -0
  172. package/dist/core/runner/prepare-delegation-surface.js +2 -2
  173. package/dist/core/runner/prepare-execution-env.js +17 -6
  174. package/dist/core/runner/prepare-gate-stations.d.ts +5 -0
  175. package/dist/core/runner/prepare-gate-stations.js +5 -5
  176. package/dist/core/runner/prepare-hands-readface.d.ts +2 -1
  177. package/dist/core/runner/prepare-hands-readface.js +5 -4
  178. package/dist/core/runner/prepare-inherited-gate.d.ts +13 -9
  179. package/dist/core/runner/prepare-inherited-gate.js +12 -6
  180. package/dist/core/runner/prepare-memory-engine-session.d.ts +2 -4
  181. package/dist/core/runner/prepare-memory-engine-session.js +4 -4
  182. package/dist/core/runner/prepare-memory.d.ts +7 -2
  183. package/dist/core/runner/prepare-memory.js +41 -39
  184. package/dist/core/runner/prepare-park-ask.js +4 -3
  185. package/dist/core/runner/prepare-policy-chain.js +7 -7
  186. package/dist/core/runner/prepare-prompt-inputs.js +1 -1
  187. package/dist/core/runner/prepare-task.js +26 -18
  188. package/dist/core/runner/prepare-wiring-manifest.d.ts +3 -1
  189. package/dist/core/runner/prepare-wiring-manifest.js +3 -2
  190. package/dist/core/runner/prepare-workspace-restore.js +3 -6
  191. package/dist/core/runner/resume-admission.d.ts +2 -2
  192. package/dist/core/runner/resume-admission.js +2 -23
  193. package/dist/core/runner/role-resolution-table.d.ts +98 -0
  194. package/dist/core/runner/role-resolution-table.js +62 -0
  195. package/dist/core/runner/run-compaction-machinery.js +2 -2
  196. package/dist/core/runner/run-identity-wiring.js +12 -9
  197. package/dist/core/runner/run-leg.js +9 -5
  198. package/dist/core/runner/run-notification-lane.js +22 -22
  199. package/dist/core/runner/run-recovery-lanes.js +3 -3
  200. package/dist/core/runner/run-settle-and-teardown.js +3 -2
  201. package/dist/core/runner/run-terminal-adoption.js +1 -1
  202. package/dist/core/runner/run-turn-boundary.js +14 -15
  203. package/dist/core/runner/runtask.js +1 -1
  204. package/dist/core/runner/terminal-account.js +1 -1
  205. package/dist/core/runner/tool-occurrence.d.ts +4 -2
  206. package/dist/core/runner/tool-occurrence.js +2 -2
  207. package/dist/core/runner-deps.d.ts +7 -3
  208. package/dist/core/secret-env.d.ts +1 -1
  209. package/dist/core/secret-env.js +4 -4
  210. package/dist/core/session-checkout.d.ts +168 -0
  211. package/dist/core/session-checkout.js +579 -0
  212. package/dist/core/session-store.d.ts +12 -4
  213. package/dist/core/session-store.js +23 -3
  214. package/dist/core/session.d.ts +16 -2
  215. package/dist/core/session.js +3 -0
  216. package/dist/core/shell-read-silence.js +8 -0
  217. package/dist/core/store-contracts/checkpoint-store-contract.d.ts +0 -12
  218. package/dist/core/store-contracts/checkpoint-store-contract.js +6 -7
  219. package/dist/core/store-contracts/peer-directory-contract.js +8 -1
  220. package/dist/core/store-contracts/session-store-contract.js +39 -0
  221. package/dist/core/task-event.d.ts +30 -5
  222. package/dist/core/task-notification.d.ts +119 -32
  223. package/dist/core/task-notification.js +186 -100
  224. package/dist/core/task-registry-agent.d.ts +25 -2
  225. package/dist/core/task-registry-agent.js +56 -22
  226. package/dist/core/task-registry-shared.d.ts +33 -11
  227. package/dist/core/task-registry.d.ts +33 -9
  228. package/dist/core/task-registry.js +36 -18
  229. package/dist/core/task-spec-members.js +1 -1
  230. package/dist/core/task-spec.d.ts +27 -2
  231. package/dist/core/tool-errors.d.ts +14 -6
  232. package/dist/core/tool-errors.js +1 -1
  233. package/dist/core/tool-policy.d.ts +32 -13
  234. package/dist/core/tool-policy.js +27 -17
  235. package/dist/core/tool-result-store.d.ts +20 -0
  236. package/dist/core/tool-spec.d.ts +42 -19
  237. package/dist/core/trace.d.ts +3 -2
  238. package/dist/core/types.d.ts +1 -1
  239. package/dist/core/untrusted-egress.d.ts +15 -4
  240. package/dist/core/untrusted-egress.js +23 -22
  241. package/dist/core/workflow-journal-store.d.ts +11 -0
  242. package/dist/core/workflow-journal-store.js +7 -0
  243. package/dist/core/workflow-run-store.d.ts +9 -0
  244. package/dist/core/workflow-run-store.js +6 -0
  245. package/dist/core/workspace-snapshot.d.ts +3 -1
  246. package/dist/core/workspace-snapshot.js +1 -0
  247. package/dist/engine/compaction/compaction.d.ts +2 -1
  248. package/dist/engine/harness/types.d.ts +67 -1
  249. package/dist/engine/harness/types.js +108 -0
  250. package/dist/engine/session/import-validate.js +6 -1
  251. package/dist/engine/session/session.d.ts +30 -1
  252. package/dist/engine/session/session.js +48 -1
  253. package/dist/index.d.ts +12 -7
  254. package/dist/index.js +12 -7
  255. package/dist/internal/harness-types.d.ts +1 -1
  256. package/dist/orchestration/run-workflow-tool.d.ts +8 -1
  257. package/dist/orchestration/run-workflow-tool.js +8 -0
  258. package/dist/orchestration/workflow-types.d.ts +2 -1
  259. package/dist/orchestration/workflow.d.ts +6 -0
  260. package/dist/orchestration/workflow.js +5 -2
  261. package/dist/stores/file/checkpoint-store.d.ts +6 -0
  262. package/dist/stores/file/checkpoint-store.js +4 -1
  263. package/dist/stores/file/file-history-store.d.ts +13 -0
  264. package/dist/stores/file/file-history-store.js +75 -13
  265. package/dist/stores/file/index.d.ts +11 -1
  266. package/dist/stores/file/index.js +7 -11
  267. package/dist/stores/file/permission-rule-store.d.ts +23 -0
  268. package/dist/stores/file/permission-rule-store.js +75 -7
  269. package/dist/stores/file/session-store.js +2 -1
  270. package/dist/stores/file/tool-result-store.d.ts +14 -0
  271. package/dist/stores/file/tool-result-store.js +39 -2
  272. package/dist/stores/file/workflow-journal-store.d.ts +4 -0
  273. package/dist/stores/file/workflow-journal-store.js +17 -0
  274. package/dist/stores/file/workflow-run-store.d.ts +3 -0
  275. package/dist/stores/file/workflow-run-store.js +15 -0
  276. package/dist/tools/fs/bash-program-position.js +3 -3
  277. package/dist/tools/fs/program-read-model.d.ts +34 -10
  278. package/dist/tools/fs/program-read-model.js +37 -7
  279. package/dist/tools/fs/program-read-model.rows.js +13 -2
  280. package/dist/tools/fs/read-deny.d.ts +28 -3
  281. package/dist/tools/fs/read-deny.js +18 -2
  282. package/dist/tools/fs/removal-targets.js +3 -3
  283. package/dist/tools/fs/safety.d.ts +2 -0
  284. package/dist/tools/fs/safety.js +1 -1
  285. package/dist/tools/fs/shell-read-argv.d.ts +8 -1
  286. package/dist/tools/fs/shell-read-argv.js +34 -29
  287. package/dist/tools/fs/shell-read-bindings.d.ts +2 -0
  288. package/dist/tools/fs/shell-read-bindings.js +5 -1
  289. package/dist/tools/fs/shell-read-brace.d.ts +18 -0
  290. package/dist/tools/fs/shell-read-brace.js +79 -0
  291. package/dist/tools/fs/shell-read-check.d.ts +8 -2
  292. package/dist/tools/fs/shell-read-check.js +16 -14
  293. package/dist/tools/fs/shell-read-establish.js +23 -16
  294. package/dist/tools/fs/shell-read-judge.d.ts +11 -0
  295. package/dist/tools/fs/shell-read-judge.js +3 -3
  296. package/dist/tools/fs/shell-read-plan.d.ts +10 -0
  297. package/dist/tools/fs/shell-read-plan.js +189 -93
  298. package/dist/tools/fs/shell-read-start.d.ts +19 -9
  299. package/dist/tools/fs/shell-read-start.js +20 -12
  300. package/dist/tools/fs/shell-read-word.d.ts +10 -0
  301. package/dist/tools/fs/shell-read-word.js +9 -5
  302. package/dist/tools/fs/shell-read-workdir.d.ts +26 -7
  303. package/dist/tools/fs/shell-read-workdir.js +73 -12
  304. package/dist/tools/fs/shell-read-write-targets.js +13 -8
  305. package/dist/tools/worktree.d.ts +3 -2
  306. package/dist/tools/worktree.js +7 -35
  307. package/package.json +3 -2
  308. package/test/export-surface.snapshot.json +135 -11
  309. package/wire-changes/core-stores-sql@7.63.0.json +30 -0
  310. package/wire-changes/core-stores-sql@7.64.0.json +30 -0
  311. package/wire-changes/core@7.63.0.json +3523 -0
  312. package/wire-changes/core@7.64.0.json +2653 -0
  313. package/wire-changes/mods-kernel@7.63.0.json +30 -0
  314. package/wire-changes/mods-kernel@7.64.0.json +30 -0
  315. package/wire-changes/primitives@7.63.0.json +445 -0
  316. package/wire-changes/primitives@7.64.0.json +30 -0
  317. package/dist/core/credential-vocabulary.d.ts +0 -176
  318. package/dist/core/credential-vocabulary.js +0 -190
  319. package/dist/core/redaction-passes.d.ts +0 -82
  320. package/dist/core/redaction-passes.js +0 -377
@@ -8,10 +8,10 @@ import { inlineUntrusted } from "../untrusted-text.js";
8
8
  import { ambiguousOriginRepresentation, committedDistilledOf, committedOriginOf, computeEntryRev, distilledEquals, entryFromFile, isValidEntryId, originEquals, parseEntryFile, serializeEntryFile } from "./frontmatter.js";
9
9
  import { convertV1SidecarKeys } from "./delegation-settlement.js";
10
10
  import { layerObservationLookup, observedDivergenceNotice } from "./layer-observations.js";
11
- import { DEFAULT_MAX_ENTRY_DEPTH, MEMORY_INDEX_FILENAME, scanEntryFiles } from "./entry-scan.js";
12
- import { ControlPlaneCorruptError, CURSORS_FILE, QUARANTINE_DIR, CHALLENGES_FILE, IMPORT_RECEIPTS_DIR, LINEAGE_FILE, SESSION_POLLUTION_DIR, appendChallengeEvents, atomicWriteFileSync, quarantineAndTombstone, captureAndClearLineageForEntries, adoptRootScopeLayout, anchorStoreRoot, effectiveControlRootOf, ensureDirExists, LEDGER_FILE, enqueueMemoryAnnouncement, importLatchTxnId, importLineageCommitted, importSessionPollution, isContainedIn, lockedStrictUpdate, readStrictSidecar, permissionRemedyOf, withPermissionRemedy, lineageContributionsOfSession, listSessionPollution, readChallengeEvents, readLineageRecord, registerScope, registeredScopes, releaseImportLatch, layoutNeedsAdoption, scopeDirFor, scopeDirName, scopeRegistryShape, stageImportLatch, landsOutsideStore, storeActRefusal, storeRootMovedRefusal, writeAllSync, STORE_JOURNAL_FILE, ERASURE_SWEEP_RECORD_FILE, ERASURE_SWEEP_RECORD_WHAT, coerceErasureSweepRecord, erasureSweepOwedAt, erasureSweepRecordPathOf, STORE_TXN_LOCK_DIR, TXN_LOCK_STALE_MS, acquireStoreTxnLock, assertStoreTxnLockOwnership, renewStoreTxnLock, tryStoreTxnLock, } from "./layout.js";
11
+ import { DEFAULT_MAX_ENTRY_DEPTH, MEMORY_INDEX_FILENAME, carriesEntry, scanEntryFiles } from "./entry-scan.js";
12
+ import { ControlPlaneCorruptError, CURSORS_FILE, QUARANTINE_DIR, STRICT_CONTROL_PLANE_LEDGERS, fileGovernanceDocOf, IMPORT_RECEIPTS_DIR, SESSION_POLLUTION_DIR, appendChallengeEvents, atomicWriteFileSync, quarantineAndTombstone, captureAndClearLineageForEntries, adoptRootScopeLayout, anchorStoreRoot, effectiveControlRootOf, ensureDirExists, LEDGER_FILE, sayAnnounceFailure, enqueueMemoryAnnouncement, importLatchTxnId, importLineageCommitted, importSessionPollution, isContainedIn, lockedStrictUpdate, readStrictSidecar, permissionRemedyOf, withPermissionRemedy, lineageContributionsOfSession, listSessionPollution, readChallengeEvents, readLineageRecord, registerScope, registeredScopes, releaseImportLatch, layoutNeedsAdoption, scopeDirFor, scopeDirName, scopeRegistryShape, stageImportLatch, landsOutsideStore, storeActRefusal, storeRootMovedRefusal, writeAllSync, STORE_JOURNAL_FILE, ERASURE_SWEEP_RECORD_FILE, ERASURE_SWEEP_RECORD_WHAT, coerceErasureSweepRecord, erasureSweepOwedAt, erasureSweepRecordPathOf, STORE_TXN_LOCK_DIR, TXN_LOCK_STALE_MS, acquireStoreTxnLock, assertStoreTxnLockOwnership, renewStoreTxnLock, tryStoreTxnLock, } from "./layout.js";
13
13
  import { screenInboundEntries } from "./data-plane.js";
14
- import { CONSOLIDATION_DRIVER_PLANS_DIR, CONSOLIDATION_PLANS_DIR } from "./consolidation.js";
14
+ import { CONSOLIDATION_DRIVER_PLANS_DIR, CONSOLIDATION_PLANS_DIR, fileGovernanceStore } from "./consolidation.js";
15
15
  import { canonicalExpectedScopes, expectedScopesInvalid, firstOutsideExpectedScopes } from "./export-bundle.js";
16
16
  import { scanMemoryFileName, scanMemoryWrite } from "./scan.js";
17
17
  import { admittingLandings, cutPerLanding, LandingsNeedAdmission, requireLandingsAdmitted } from "./landing-admission.js";
@@ -169,7 +169,6 @@ class AuditSnapshotContendedError extends Error {
169
169
  class AuditObservationUnstableError extends Error {
170
170
  }
171
171
  const LEDGER_SCHEMA_VERSION = 2;
172
- const LEDGER_V1_BACKUP_FILE = "revs.v1.json.bak";
173
172
  const TRANSFERS_FILE = "transfers.jsonl";
174
173
  const CHAIN_DEGRADED_FILE = "transfers.chain-degraded.json";
175
174
  const SHADOW_DIR = "shadow";
@@ -281,33 +280,22 @@ function parseJournal(raw, jp) {
281
280
  throw new ControlPlaneCorruptError(`transaction journal is unparseable: ${jp}`);
282
281
  }
283
282
  }
283
+ const V1_LEDGER_FORM_REFUSED = "the pre-binding v1 form (a bare id → rev map), which this engine does not read — open the store once with an engine at or below 7.63.0, whose locked migration upgrades it to the v2 envelope";
284
284
  function journalLedgerSnapshot(journal, jp) {
285
- const hasV2 = journal.ledger2 !== undefined;
286
- const hasV1 = journal.ledger !== undefined;
287
- if (hasV2 === hasV1) {
288
- throw new ControlPlaneCorruptError(`transaction journal has ${hasV2 ? "both ledger snapshots" : "no ledger snapshot"} (wrong shape): ${jp}`);
289
- }
290
- if (hasV2) {
291
- const env = journal.ledger2;
292
- if (!env || typeof env !== "object" || Array.isArray(env) || env.v !== LEDGER_SCHEMA_VERSION) {
293
- throw new ControlPlaneCorruptError(`transaction journal ledger2 snapshot has the wrong shape: ${jp}`);
294
- }
295
- const extras = {};
296
- for (const [k, val] of Object.entries(env))
297
- if (k !== "v" && k !== "rows")
298
- extras[k] = val;
299
- return { form: "v2", rows: validateLedgerRowsV2(env.rows, jp), extras };
300
- }
301
- const map = journal.ledger;
302
- if (!map || typeof map !== "object" || Array.isArray(map))
303
- throw new ControlPlaneCorruptError(`transaction journal ledger snapshot has the wrong shape: ${jp}`);
304
- const rows = Object.create(null);
305
- for (const [id, rev] of Object.entries(map)) {
306
- if (typeof rev !== "string")
307
- throw new ControlPlaneCorruptError(`transaction journal v1 ledger snapshot entry ${JSON.stringify(id)} is not a string: ${jp}`);
308
- rows[id] = { rev };
309
- }
310
- return { form: "v1", rows };
285
+ if (journal.ledger !== undefined) {
286
+ throw new ControlPlaneCorruptError(`transaction journal carries its ledger snapshot in ${V1_LEDGER_FORM_REFUSED}: ${jp}`);
287
+ }
288
+ const env = journal.ledger2;
289
+ if (env === undefined)
290
+ throw new ControlPlaneCorruptError(`transaction journal has no ledger snapshot (wrong shape): ${jp}`);
291
+ if (!env || typeof env !== "object" || Array.isArray(env) || env.v !== LEDGER_SCHEMA_VERSION) {
292
+ throw new ControlPlaneCorruptError(`transaction journal ledger2 snapshot has the wrong shape: ${jp}`);
293
+ }
294
+ const extras = {};
295
+ for (const [k, val] of Object.entries(env))
296
+ if (k !== "v" && k !== "rows")
297
+ extras[k] = val;
298
+ return { rows: validateLedgerRowsV2(env.rows, jp), extras };
311
299
  }
312
300
  function parseLedgerText(raw, path) {
313
301
  let parsed;
@@ -321,33 +309,47 @@ function parseLedgerText(raw, path) {
321
309
  throw new ControlPlaneCorruptError(`committed-rev ledger has the wrong shape: ${path}`);
322
310
  }
323
311
  const rec = parsed;
324
- if ("v" in rec) {
325
- const v = rec.v;
326
- if (typeof v !== "number" || !Number.isInteger(v)) {
327
- throw new ControlPlaneCorruptError(`committed-rev ledger schema version is not an integer: ${path}`);
328
- }
329
- if (v > LEDGER_SCHEMA_VERSION) {
330
- throw new ControlPlaneCorruptError(`committed-rev ledger schema v${v} is newer than this engine supports (v${LEDGER_SCHEMA_VERSION}) — refusing to reinterpret it: ${path}`);
331
- }
332
- if (v !== LEDGER_SCHEMA_VERSION) {
333
- throw new ControlPlaneCorruptError(`committed-rev ledger has an unknown enveloped schema v${v}: ${path}`);
334
- }
335
- const rows = validateLedgerRowsV2(rec.rows, path);
336
- const envelopeExtras = {};
337
- for (const [k, val] of Object.entries(rec)) {
338
- if (k !== "v" && k !== "rows")
339
- envelopeExtras[k] = val;
340
- }
341
- return { form: "v2", rows, envelopeExtras };
312
+ if (!("v" in rec)) {
313
+ throw new ControlPlaneCorruptError(`committed-rev ledger has no schema version: it is ${V1_LEDGER_FORM_REFUSED}: ${path}`);
342
314
  }
343
- const rows = Object.create(null);
344
- for (const [k, v] of Object.entries(rec)) {
345
- if (typeof v !== "string") {
346
- throw new ControlPlaneCorruptError(`committed-rev ledger entry ${JSON.stringify(k)} is not a string: ${path}`);
347
- }
348
- rows[k] = { rev: v };
315
+ const v = rec.v;
316
+ if (typeof v !== "number" || !Number.isInteger(v)) {
317
+ throw new ControlPlaneCorruptError(`committed-rev ledger schema version is not an integer: ${path}`);
318
+ }
319
+ if (v > LEDGER_SCHEMA_VERSION) {
320
+ throw new ControlPlaneCorruptError(`committed-rev ledger schema v${v} is newer than this engine supports (v${LEDGER_SCHEMA_VERSION}) — refusing to reinterpret it: ${path}`);
321
+ }
322
+ if (v !== LEDGER_SCHEMA_VERSION) {
323
+ throw new ControlPlaneCorruptError(`committed-rev ledger has an unknown enveloped schema v${v}: ${path}`);
324
+ }
325
+ const rows = validateLedgerRowsV2(rec.rows, path);
326
+ const envelopeExtras = {};
327
+ for (const [k, val] of Object.entries(rec)) {
328
+ if (k !== "v" && k !== "rows")
329
+ envelopeExtras[k] = val;
349
330
  }
350
- return { form: "v1", rows };
331
+ return { rows, envelopeExtras };
332
+ }
333
+ function ledgerFormOnDisk(controlDir) {
334
+ const path = join(controlDir, LEDGER_FILE);
335
+ const raw = readControlFileOrAbsent(path, "committed-rev ledger");
336
+ if (raw === undefined)
337
+ return "absent";
338
+ try {
339
+ parseLedgerText(raw, path);
340
+ return "v2";
341
+ }
342
+ catch (err) {
343
+ if (err instanceof ControlPlaneCorruptError)
344
+ return "malformed";
345
+ throw err;
346
+ }
347
+ }
348
+ export async function readMemoryLedgerForm(controlDir) {
349
+ if (typeof controlDir !== "string" || controlDir === "") {
350
+ throw new TypeError(`readMemoryLedgerForm: controlDir is a non-empty directory path (got ${controlDir === "" ? "an empty string" : typeof controlDir})`);
351
+ }
352
+ return ledgerFormOnDisk(controlDir);
351
353
  }
352
354
  const KNOWN_TRANSFER_CHANNELS = new Set(["adopted-move", "applyPatches-move", "migration-bind", "migration", "delete", "erasure-anchor", "erasure-request"]);
353
355
  function transferEventInvalid(raw) {
@@ -597,12 +599,11 @@ const FILE_MEMORY_ENGINE_BACKEND_OPTION_KEYS = optionKeys()({ controlDir: 0, now
597
599
  export class FileMemoryEngineBackend {
598
600
  directoryRoot;
599
601
  controlPlaneRoot;
602
+ governance;
600
603
  now;
601
604
  onIncident;
602
605
  ledger;
603
606
  envelopeExtras;
604
- persistedSchemaVersion;
605
- announcedV1Compat = false;
606
607
  unboundRowsKnown = false;
607
608
  adoptionNoticeKeys = new Set();
608
609
  transfersAppendFault;
@@ -619,6 +620,7 @@ export class FileMemoryEngineBackend {
619
620
  this.onIncident = o.onIncident;
620
621
  this.directoryRoot = dir;
621
622
  this.controlPlaneRoot = effectiveControlRootOf(dir, { controlDir: o.controlDir, onIncident: o.onIncident });
623
+ this.governance = fileGovernanceStore(this.controlPlaneRoot);
622
624
  this.assertRootAnchored();
623
625
  ensureDirExists(dir);
624
626
  ensureDirExists(this.controlPlaneRoot);
@@ -628,7 +630,7 @@ export class FileMemoryEngineBackend {
628
630
  }
629
631
  catch {
630
632
  }
631
- if (this.persistedSchemaVersion === "v1" || (this.ledger !== undefined && hasUnboundRows(this.ledger))) {
633
+ if (this.ledger !== undefined && hasUnboundRows(this.ledger)) {
632
634
  let layoutDerivesPaths = false;
633
635
  try {
634
636
  const shape = scopeRegistryShape(this.controlPlaneRoot);
@@ -683,10 +685,9 @@ export class FileMemoryEngineBackend {
683
685
  this.ledger = undefined;
684
686
  this.loadLedger();
685
687
  registeredScopes(this.controlPlaneRoot);
686
- if (this.persistedSchemaVersion === "v1" || hasUnboundRows(this.loadLedger()))
688
+ if (hasUnboundRows(this.loadLedger()))
687
689
  this.tryOpportunisticMigrate();
688
- if (this.persistedSchemaVersion === "v2")
689
- this.announceUnboundRows(this.loadLedger());
690
+ this.announceUnboundRows(this.loadLedger());
690
691
  }
691
692
  drainInboundFindings() {
692
693
  const out = this.inboundFindings;
@@ -706,37 +707,21 @@ export class FileMemoryEngineBackend {
706
707
  const raw = readControlFileOrAbsent(path, "committed-rev ledger");
707
708
  if (raw === undefined) {
708
709
  this.ledger = Object.create(null);
709
- this.persistedSchemaVersion = "v2";
710
710
  this.unboundRowsKnown = false;
711
711
  return this.ledger;
712
712
  }
713
713
  const parsed = parseLedgerText(raw, path);
714
714
  this.ledger = parsed.rows;
715
715
  this.envelopeExtras = parsed.envelopeExtras;
716
- this.persistedSchemaVersion = parsed.form;
717
716
  this.unboundRowsKnown = hasUnboundRows(parsed.rows);
718
- if (parsed.form === "v1" && !this.announcedV1Compat) {
719
- this.announcedV1Compat = true;
720
- this.enqueueExternalItems("every-scope", ["memory ledger v1-compat active — v2 binding guarantees not yet in force (the migration runs at the next uncontended entry)"]);
721
- }
722
717
  return this.ledger;
723
718
  }
724
719
  saveLedger() {
725
720
  if (!this.ledger)
726
721
  return;
727
- if (this.persistedSchemaVersion !== "v2") {
728
- throw new ControlPlaneCorruptError("committed-rev ledger write refused: the on-disk ledger is still schema v1 and the migration has not run (mutations must migrate first)");
729
- }
730
722
  atomicWriteFileSync(join(this.controlPlaneRoot, LEDGER_FILE), `${JSON.stringify(ledgerEnvelope(this.ledger, this.envelopeExtras), null, 2)}\n`);
731
723
  this.unboundRowsKnown = hasUnboundRows(this.ledger);
732
724
  }
733
- persistLedgerV1(rows) {
734
- const map = Object.create(null);
735
- for (const [id, row] of rowsEntries(rows))
736
- map[id] = row.rev;
737
- atomicWriteFileSync(join(this.controlPlaneRoot, LEDGER_FILE), `${JSON.stringify(map, null, 2)}\n`);
738
- this.unboundRowsKnown = Object.keys(map).length > 0;
739
- }
740
725
  enqueueExternalItems(to, items, opts) {
741
726
  if (items.length === 0 && opts?.supersedesPrefix === undefined)
742
727
  return;
@@ -760,13 +745,8 @@ export class FileMemoryEngineBackend {
760
745
  else {
761
746
  scopes = [to.scope];
762
747
  }
763
- for (const scope of scopes) {
764
- try {
765
- enqueueMemoryAnnouncement(this.controlPlaneRoot, { kind: "external", scope, at: this.now(), items }, opts);
766
- }
767
- catch {
768
- }
769
- }
748
+ for (const scope of scopes)
749
+ void enqueueMemoryAnnouncement(this.governance, { kind: "external", scope, at: this.now(), items }, opts).catch((err) => sayAnnounceFailure(this.onIncident, "store-notice enqueue", err));
770
750
  }
771
751
  withholdNotice(items) {
772
752
  if (items.length === 0)
@@ -872,28 +852,6 @@ export class FileMemoryEngineBackend {
872
852
  }
873
853
  return { complete, byId, distilledFrom };
874
854
  }
875
- writeV1BackupOnce() {
876
- const raw = readControlFileOrAbsent(join(this.controlPlaneRoot, LEDGER_FILE), "committed-rev ledger");
877
- if (raw === undefined)
878
- throw new ControlPlaneCorruptError(`committed-rev ledger vanished under the migration lock: ${join(this.controlPlaneRoot, LEDGER_FILE)}`);
879
- const bak = join(this.controlPlaneRoot, LEDGER_V1_BACKUP_FILE);
880
- let fd;
881
- try {
882
- fd = openSync(bak, "wx", 0o600);
883
- }
884
- catch (err) {
885
- if (err.code === "EEXIST")
886
- return;
887
- throw err;
888
- }
889
- try {
890
- writeAllSync(fd, raw);
891
- fsyncSync(fd);
892
- }
893
- finally {
894
- closeSync(fd);
895
- }
896
- }
897
855
  tryAcquireTxnLockSync() {
898
856
  return tryStoreTxnLock(this.controlPlaneRoot, { now: this.now });
899
857
  }
@@ -913,21 +871,19 @@ export class FileMemoryEngineBackend {
913
871
  migrateLockedIfNeeded(lockToken) {
914
872
  this.ledger = undefined;
915
873
  const rows = this.loadLedger();
916
- if (this.persistedSchemaVersion === "v2" && !hasUnboundRows(rows))
874
+ if (!hasUnboundRows(rows))
917
875
  return;
918
- const fullMigration = this.persistedSchemaVersion === "v1";
919
876
  const census = this.censusProjectionsLocked();
920
877
  if (!census.complete) {
921
- this.announceAdoptionNotice("migration-incomplete", "every-scope", `memory ledger ${fullMigration ? "v1→v2 migration" : "unbound-row convergence"} abandoned this pass — the projection census could not read every scope (bindings derived from a partial view could be wrong); it retries at the next entry`);
878
+ this.announceAdoptionNotice("migration-incomplete", "every-scope", "memory ledger unbound-row convergence abandoned this pass — the projection census could not read every scope (bindings derived from a partial view could be wrong); it retries at the next entry");
922
879
  return;
923
880
  }
924
881
  const now = this.now();
925
882
  const next = Object.create(null);
926
883
  const bound = [];
927
- const unbound = [];
928
884
  const events = [];
929
885
  for (const [id, row] of rowsEntries(rows)) {
930
- if (!fullMigration && rowsBinding(rows, id) !== undefined) {
886
+ if (rowsBinding(rows, id) !== undefined) {
931
887
  next[id] = { ...row };
932
888
  continue;
933
889
  }
@@ -936,35 +892,23 @@ export class FileMemoryEngineBackend {
936
892
  if (projections.length === 1 && sole !== undefined) {
937
893
  next[id] = { ...row, scope: sole.scope, slug: sole.slug };
938
894
  bound.push(id);
939
- if (!fullMigration)
940
- events.push({ ev: randomUUID(), channel: "migration-bind", id, to: sole, at: now });
895
+ events.push({ ev: randomUUID(), channel: "migration-bind", id, to: sole, at: now });
941
896
  }
942
897
  else {
943
898
  next[id] = { ...row };
944
- unbound.push(`${id} (${projections.length} projection(s))`);
945
899
  }
946
900
  }
947
- if (fullMigration) {
948
- this.writeV1BackupOnce();
949
- events.push({ ev: randomUUID(), channel: "migration", boundRows: bound.length, unboundRows: unbound.length, at: now });
950
- }
951
- else if (bound.length === 0) {
901
+ if (bound.length === 0)
952
902
  return;
953
- }
954
903
  this.assertTxnLockOwnership(lockToken);
955
904
  this.precheckTransfersAppendable();
956
905
  const journal = { txn: `${Date.now()}-migrate`, ops: [], ledger2: ledgerEnvelope(next, this.envelopeExtras), transfers: events };
957
906
  atomicWriteFileSync(join(this.controlPlaneRoot, STORE_JOURNAL_FILE), JSON.stringify(journal));
958
907
  this.ledger = next;
959
- this.persistedSchemaVersion = "v2";
960
908
  this.saveLedger();
961
909
  this.appendTransfers(events);
962
910
  rmSync(join(this.controlPlaneRoot, STORE_JOURNAL_FILE), { force: true });
963
- this.enqueueExternalItems("operator", [
964
- fullMigration
965
- ? `memory ledger upgraded to v2 (${bound.length} row(s) bound, ${unbound.length} unbound${unbound.length > 0 ? `: ${unbound.slice(0, 8).join(", ")}${unbound.length > 8 ? ", …" : ""}` : ""})`
966
- : `memory ledger unbound row(s) converged to bound: ${bound.slice(0, 8).join(", ")}${bound.length > 8 ? ", …" : ""}`,
967
- ]);
911
+ this.enqueueExternalItems("operator", [`memory ledger unbound row(s) converged to bound: ${bound.slice(0, 8).join(", ")}${bound.length > 8 ? ", …" : ""}`]);
968
912
  }
969
913
  chainDegradedMarkerPresent() {
970
914
  return readControlFileOrAbsent(join(this.controlPlaneRoot, CHAIN_DEGRADED_FILE), "evidence-chain degradation marker") !== undefined;
@@ -1167,8 +1111,7 @@ export class FileMemoryEngineBackend {
1167
1111
  const raw = readControlFileOrAbsent(join(this.controlPlaneRoot, LEDGER_FILE), "committed-rev ledger");
1168
1112
  if (raw === undefined)
1169
1113
  return Object.create(null);
1170
- const parsed = parseLedgerText(raw, join(this.controlPlaneRoot, LEDGER_FILE));
1171
- return parsed.form === "v2" ? parsed.rows : Object.create(null);
1114
+ return parseLedgerText(raw, join(this.controlPlaneRoot, LEDGER_FILE)).rows;
1172
1115
  }
1173
1116
  catch (err) {
1174
1117
  const code = err.code ?? (err instanceof ControlPlaneCorruptError ? "corrupt" : "io error");
@@ -1246,20 +1189,7 @@ export class FileMemoryEngineBackend {
1246
1189
  const raw = readControlFileOrAbsent(jp, "transaction journal");
1247
1190
  if (raw !== undefined) {
1248
1191
  const journal = parseJournal(raw, jp);
1249
- const { rows: snapshotRows, extras: snapshotExtras, form: snapshotForm } = journalLedgerSnapshot(journal, jp);
1250
- if (snapshotForm === "v1") {
1251
- try {
1252
- const diskRaw = readControlFileOrAbsent(join(this.controlPlaneRoot, LEDGER_FILE), "committed-rev ledger");
1253
- const diskParsed = diskRaw !== undefined ? JSON.parse(diskRaw) : undefined;
1254
- if (diskParsed !== null && typeof diskParsed === "object" && "v" in diskParsed && diskParsed.v === LEDGER_SCHEMA_VERSION) {
1255
- this.enqueueExternalItems("every-scope", [
1256
- "memory ledger: recovering a v1-format journal over a v2 ledger — a pre-v2 process wrote AFTER this store migrated (mixed-version deployment window). Retire old readers/writers before further writes; see the v2 rollback runbook.",
1257
- ]);
1258
- }
1259
- }
1260
- catch {
1261
- }
1262
- }
1192
+ const { rows: snapshotRows, extras: snapshotExtras } = journalLedgerSnapshot(journal, jp);
1263
1193
  for (const op of journal.ops) {
1264
1194
  const invalid = journalOpInvalid(op, this.directoryRoot);
1265
1195
  if (invalid !== undefined) {
@@ -1286,8 +1216,6 @@ export class FileMemoryEngineBackend {
1286
1216
  journalEvs.set(e.ev, canonical);
1287
1217
  journalChainForms.set(e.ev, canonicalJsonStringify(chainFormOf(e)));
1288
1218
  }
1289
- if (snapshotForm === "v1")
1290
- throw new ControlPlaneCorruptError(`transaction journal carries transfers with a v1 snapshot (impossible form): ${jp}`);
1291
1219
  if (journalEvs.size > 0) {
1292
1220
  for (const row of this.readTransferChain(fenceToken)) {
1293
1221
  const j = journalEvs.get(row.ev);
@@ -1444,19 +1372,11 @@ export class FileMemoryEngineBackend {
1444
1372
  }
1445
1373
  this.recoveryCommitProbe?.();
1446
1374
  this.assertTxnLockOwnership(fenceToken);
1447
- if (snapshotForm === "v2") {
1448
- this.ledger = snapshotRows;
1449
- this.envelopeExtras = snapshotExtras;
1450
- this.persistedSchemaVersion = "v2";
1451
- this.saveLedger();
1452
- this.recordJournalSweep(journal);
1453
- this.appendTransfers(journal.transfers ?? []);
1454
- }
1455
- else {
1456
- this.ledger = snapshotRows;
1457
- this.persistedSchemaVersion = "v1";
1458
- this.persistLedgerV1(snapshotRows);
1459
- }
1375
+ this.ledger = snapshotRows;
1376
+ this.envelopeExtras = snapshotExtras;
1377
+ this.saveLedger();
1378
+ this.recordJournalSweep(journal);
1379
+ this.appendTransfers(journal.transfers ?? []);
1460
1380
  if (typeof journal.txn === "string") {
1461
1381
  this.closeJournalIfOurs(journal.txn);
1462
1382
  }
@@ -1570,7 +1490,7 @@ export class FileMemoryEngineBackend {
1570
1490
  if (memo !== undefined)
1571
1491
  return memo;
1572
1492
  }
1573
- if (this.persistedSchemaVersion === "v1" || this.unboundRowsKnown)
1493
+ if (this.unboundRowsKnown)
1574
1494
  this.tryOpportunisticMigrate();
1575
1495
  const dir = this.scopeDir(scope);
1576
1496
  const files = scanEntryFiles(dir);
@@ -1595,6 +1515,8 @@ export class FileMemoryEngineBackend {
1595
1515
  skippedUnreadable.push(relative(this.directoryRoot, f.path));
1596
1516
  continue;
1597
1517
  }
1518
+ if (!carriesEntry(text))
1519
+ continue;
1598
1520
  const parsed = parseEntryFile(text);
1599
1521
  if (parsed.id === undefined)
1600
1522
  continue;
@@ -1920,7 +1842,7 @@ export class FileMemoryEngineBackend {
1920
1842
  restrictedIdlessScope;
1921
1843
  restrictedIdlessBaseline;
1922
1844
  readScopeCommitted(scope, audit) {
1923
- if (this.persistedSchemaVersion === "v1" || this.unboundRowsKnown)
1845
+ if (this.unboundRowsKnown)
1924
1846
  this.tryOpportunisticMigrate();
1925
1847
  const dir = this.scopeDir(scope);
1926
1848
  const files = scanEntryFiles(dir, {
@@ -2115,11 +2037,10 @@ export class FileMemoryEngineBackend {
2115
2037
  this.readScopeCommitted(scope, true);
2116
2038
  const idlessBaseline = opts?.idlessWriteScope !== undefined && opts.idlessWriteScope === this.restrictedIdlessScope ? this.restrictedIdlessBaseline : undefined;
2117
2039
  const idlessScope = idlessBaseline !== undefined ? opts?.idlessWriteScope : undefined;
2118
- if (this.persistedSchemaVersion === "v1" || hasUnboundRows(this.loadLedger()))
2040
+ if (hasUnboundRows(this.loadLedger()))
2119
2041
  this.tryOpportunisticMigrate();
2120
2042
  const rows = { ...this.loadLedger() };
2121
- if (this.persistedSchemaVersion === "v2")
2122
- this.announceUnboundRows(rows);
2043
+ this.announceUnboundRows(rows);
2123
2044
  const present = new Map();
2124
2045
  let complete = true;
2125
2046
  for (const scope of Object.keys(registeredScopes(this.controlPlaneRoot))) {
@@ -2472,10 +2393,10 @@ export class FileMemoryEngineBackend {
2472
2393
  let slug = entry.slug;
2473
2394
  let target = join(dir, `${slug}.md`);
2474
2395
  for (let n = 2;; n++) {
2475
- const existing = plannedTargets.has(target) ? "" : readSafe(target);
2476
- if (existing === undefined)
2477
- break;
2478
- if (existing !== "") {
2396
+ if (!plannedTargets.has(target)) {
2397
+ const existing = readSafe(target);
2398
+ if (existing === undefined || !carriesEntry(existing))
2399
+ break;
2479
2400
  const parsed = parseEntryFile(existing);
2480
2401
  if (parsed.id === undefined || parsed.id === entry.id)
2481
2402
  break;
@@ -2499,7 +2420,7 @@ export class FileMemoryEngineBackend {
2499
2420
  report.applied.push({ op: "add", id: entry.id, slug });
2500
2421
  return;
2501
2422
  }
2502
- const found = this.locateById(patch.id, patch.op === "update" ? patch.entry?.scope : undefined);
2423
+ const found = this.locateForPatch(patch.id, rows, patch.op === "update" ? patch.entry?.scope : undefined);
2503
2424
  if (!found) {
2504
2425
  report.conflicts.push({ op: patch.op, id: patch.id, reason: "unknown id (entry not found in backend)" });
2505
2426
  return;
@@ -2543,7 +2464,7 @@ export class FileMemoryEngineBackend {
2543
2464
  if (patch.baseRev !== undefined && patch.baseRev !== currentRev) {
2544
2465
  report.conflicts.push({ op: patch.op, id: patch.id, reason: "rev mismatch (concurrent change)", baseRev: patch.baseRev, currentRev });
2545
2466
  const shadowText = this.readCommittedShadow(patch.id);
2546
- if (shadowText !== undefined && shadowText !== serializeEntryFile(found.entry)) {
2467
+ if (shadowText !== undefined && (!found.onPlane || shadowText !== serializeEntryFile(found.entry))) {
2547
2468
  ops.push({ kind: "write", target: found.path, content: shadowText });
2548
2469
  landings.push({ id: patch.id, scope: found.entry.scope, slug: found.entry.slug });
2549
2470
  }
@@ -2593,6 +2514,20 @@ export class FileMemoryEngineBackend {
2593
2514
  bindRowTracked(rows, entry.id, { rev: computeEntryRev(entry), scope: entry.scope, slug: entry.slug, at: this.now() }, transfers, "applyPatches-move");
2594
2515
  report.applied.push({ op: "update", id: entry.id, slug: entry.slug });
2595
2516
  }
2517
+ locateForPatch(id, rows, preferScope) {
2518
+ const onPlane = this.locateById(id, preferScope);
2519
+ if (onPlane !== undefined)
2520
+ return { ...onPlane, onPlane: true };
2521
+ const binding = rowsBinding(rows, id);
2522
+ const shadowText = binding !== undefined ? this.readCommittedShadow(id) : undefined;
2523
+ if (binding === undefined || shadowText === undefined)
2524
+ return undefined;
2525
+ const path = this.bindingAbsPath(binding);
2526
+ const standing = readSafe(path);
2527
+ if (standing !== undefined && parseEntryFile(standing).id !== undefined)
2528
+ return undefined;
2529
+ return { entry: entryFromFile(shadowText, id, binding.slug, binding.scope), path, onPlane: false };
2530
+ }
2596
2531
  locateById(id, preferScope) {
2597
2532
  const scopes = Object.keys(registeredScopes(this.controlPlaneRoot));
2598
2533
  const ordered = preferScope !== undefined ? [preferScope, ...scopes.filter((s) => s !== preferScope)] : scopes;
@@ -2738,7 +2673,7 @@ export class FileMemoryEngineBackend {
2738
2673
  resolvedIds = [...new Set([...ledgerIds, ...ghostIds])].sort();
2739
2674
  }
2740
2675
  else {
2741
- resolvedIds = lineageContributionsOfSession(this.controlPlaneRoot, select.sessionId)
2676
+ resolvedIds = (await lineageContributionsOfSession(this.governance, select.sessionId))
2742
2677
  .map((c) => c.entryId)
2743
2678
  .sort();
2744
2679
  }
@@ -2789,7 +2724,7 @@ export class FileMemoryEngineBackend {
2789
2724
  transfers.push({ ev: randomUUID(), channel: "erasure-request", req: requestId, scope, selectHash, ids: byScope.get(scope) ?? [], at: now });
2790
2725
  }
2791
2726
  }
2792
- const lineageCommitted = resolvedIds.length > 0 ? readLineageRecord(this.controlPlaneRoot).committed : {};
2727
+ const lineageCommitted = resolvedIds.length > 0 ? (await readLineageRecord(this.governance)).committed : {};
2793
2728
  const preSessionsOf = (id) => Object.prototype.hasOwnProperty.call(lineageCommitted, id) ? Object.keys(lineageCommitted[id] ?? {}).sort() : [];
2794
2729
  for (const id of rowIds) {
2795
2730
  const row = rows[id];
@@ -2888,7 +2823,7 @@ export class FileMemoryEngineBackend {
2888
2823
  this.appendTransfers(transfers);
2889
2824
  this.closeJournalIfOurs(txn);
2890
2825
  }
2891
- const late = captureAndClearLineageForEntries(this.controlPlaneRoot, resolvedIds);
2826
+ const late = await captureAndClearLineageForEntries(this.governance, resolvedIds);
2892
2827
  for (const e of erased) {
2893
2828
  const lateSessions = late.get(e.id);
2894
2829
  if (lateSessions !== undefined && lateSessions.length > 0)
@@ -2948,13 +2883,7 @@ export class FileMemoryEngineBackend {
2948
2883
  const raw = readControlFileOrAbsent(path, "committed-rev ledger");
2949
2884
  if (raw === undefined)
2950
2885
  return { raw, rows: Object.create(null) };
2951
- const parsed = parseLedgerText(raw, path);
2952
- if (parsed.form === "v1") {
2953
- throw new Error(`memory audit read refused: the committed-rev ledger at ${path} is still schema v1 (pre-binding) — ` +
2954
- `migrate first (the store migrates itself at its next uncontended write/read entry or checkControlPlane); ` +
2955
- `an audit read never triggers the migration`);
2956
- }
2957
- return { raw, rows: parsed.rows };
2886
+ return { raw, rows: parseLedgerText(raw, path).rows };
2958
2887
  }
2959
2888
  async auditStable(read) {
2960
2889
  for (let attempt = 0; attempt < FileMemoryEngineBackend.AUDIT_FENCE_ATTEMPTS; attempt++) {
@@ -3101,7 +3030,7 @@ export class FileMemoryEngineBackend {
3101
3030
  const ledgerRaw = readControlFileOrAbsent(join(this.controlPlaneRoot, LEDGER_FILE), "committed-rev ledger");
3102
3031
  if (ledgerRaw !== undefined) {
3103
3032
  const parsed = parseLedgerText(ledgerRaw, join(this.controlPlaneRoot, LEDGER_FILE));
3104
- if (parsed.form === "v2" && Object.prototype.hasOwnProperty.call(parsed.rows, id) && parsed.rows[id]?.prev !== undefined) {
3033
+ if (Object.prototype.hasOwnProperty.call(parsed.rows, id) && parsed.rows[id]?.prev !== undefined) {
3105
3034
  out.state = "damaged";
3106
3035
  }
3107
3036
  }
@@ -3215,7 +3144,7 @@ export class FileMemoryEngineBackend {
3215
3144
  const feed = (label, text) => {
3216
3145
  h.update(JSON.stringify([label, text]));
3217
3146
  };
3218
- for (const file of [LINEAGE_FILE, CHALLENGES_FILE]) {
3147
+ for (const file of STRICT_CONTROL_PLANE_LEDGERS.map((ledger) => fileGovernanceDocOf(ledger).file)) {
3219
3148
  feed(file, readControlFileOrAbsent(join(this.controlPlaneRoot, file), `${file} (export fence)`) ?? "<absent>");
3220
3149
  let journalRaw;
3221
3150
  try {
@@ -3389,9 +3318,6 @@ export class FileMemoryEngineBackend {
3389
3318
  this.recoverJournal({ token: lock.token });
3390
3319
  this.adoptLayoutLocked(lock.token);
3391
3320
  const rows = this.loadLedger();
3392
- if (this.persistedSchemaVersion !== "v2") {
3393
- throw erasureCodedError("memory export refused: the committed-rev ledger is still schema v1 (pre-binding) — the store migrates itself at its next write/read entry or checkControlPlane; an export never triggers the migration", FileMemoryEngineBackend.EXPORT_INCOMPLETE);
3394
- }
3395
3321
  if (this.chainDegradedMarkerPresent()) {
3396
3322
  throw erasureCodedError("memory export refused: the custody chain carries the durable degradation marker (transfers.chain-degraded.json) — evidence rows were lost to an out-of-band tear and a bundle cannot testify to a complete deletion history; resolve the marker deliberately before exporting", FileMemoryEngineBackend.EXPORT_INCOMPLETE);
3397
3323
  }
@@ -3400,11 +3326,11 @@ export class FileMemoryEngineBackend {
3400
3326
  const fullStore = Object.keys(registered).every((s) => scopeSet.has(s));
3401
3327
  for (let round = 0; round < 3; round++) {
3402
3328
  const before = this.exportGovernanceFingerprint();
3403
- const lineageRec = readLineageRecord(this.controlPlaneRoot);
3404
- const challenges = readChallengeEvents(this.controlPlaneRoot);
3329
+ const lineageRec = await readLineageRecord(this.governance);
3330
+ const challenges = await readChallengeEvents(this.governance);
3405
3331
  const polluted = listSessionPollution(this.controlPlaneRoot);
3406
3332
  const chainRows = this.readExportChainRowsPure();
3407
- this.exportFaceProbe?.(round);
3333
+ await this.exportFaceProbe?.(round);
3408
3334
  const after = this.exportGovernanceFingerprint();
3409
3335
  if (before !== after)
3410
3336
  continue;
@@ -3473,8 +3399,6 @@ export class FileMemoryEngineBackend {
3473
3399
  this.recoverJournal({ token: lock.token });
3474
3400
  this.adoptLayoutLocked(lock.token);
3475
3401
  const rows = this.loadLedger();
3476
- if (this.persistedSchemaVersion !== "v2")
3477
- return { custody: [], unbound: [], complete: false, unsliceableCustody: 0, reason: "the committed-rev ledger is still schema v1 (migrate first)" };
3478
3402
  if (this.chainDegradedMarkerPresent()) {
3479
3403
  return { custody: [], unbound: [], complete: false, unsliceableCustody: 0, reason: "the custody chain carries the durable degradation marker (transfers.chain-degraded.json) — evidence rows were lost to an out-of-band tear" };
3480
3404
  }
@@ -3546,15 +3470,15 @@ export class FileMemoryEngineBackend {
3546
3470
  this.appendTransfers(toAppend);
3547
3471
  return { appended: toAppend.length, withheld };
3548
3472
  }
3549
- appendImportChallengesVerified(events) {
3473
+ async appendImportChallengesVerified(events) {
3550
3474
  if (events.length === 0)
3551
3475
  return;
3552
- const { assignments } = appendChallengeEvents(this.controlPlaneRoot, events, this.now);
3476
+ const { assignments } = await appendChallengeEvents(this.governance, events, this.now);
3553
3477
  const replayed = assignments.filter((a) => a.replayed);
3554
3478
  if (replayed.length === 0)
3555
3479
  return;
3556
3480
  const byEventId = new Map();
3557
- for (const e of readChallengeEvents(this.controlPlaneRoot))
3481
+ for (const e of await readChallengeEvents(this.governance))
3558
3482
  if (!byEventId.has(e.eventId))
3559
3483
  byEventId.set(e.eventId, e);
3560
3484
  for (const a of replayed) {
@@ -3620,14 +3544,14 @@ export class FileMemoryEngineBackend {
3620
3544
  catch {
3621
3545
  throw new ControlPlaneCorruptError(`bundle import completion receipt is unreadable/malformed at ${receiptPath} — it cannot prove completion and cannot be re-minted; repair or remove it deliberately (fail-closed)`);
3622
3546
  }
3623
- releaseImportLatch(ctl, txnId);
3547
+ await releaseImportLatch(this.governance, ctl, txnId);
3624
3548
  return report;
3625
3549
  }
3626
3550
  const digest = this.deletedIdsDigestLocked();
3627
3551
  if (digest.state === "damaged" || this.chainDegradedMarkerPresent()) {
3628
3552
  throw erasureCodedError("memory bundle import refused: the destination evidence chain is damaged or durably marked degraded — deletion history cannot be proven, so the anti-resurrection judgment cannot run; heal/rebuild the chain (locked mutation entry / explicit rebuild), then import again (nothing was landed, no latch was set)", "memory.import_rejected");
3629
3553
  }
3630
- stageImportLatch(ctl, bundleHash, plan.entries.map((e) => ({ entryId: e.id, rev: e.rev })), this.now);
3554
+ await stageImportLatch(this.governance, bundleHash, plan.entries.map((e) => ({ entryId: e.id, rev: e.rev })), this.now);
3631
3555
  const rows = this.loadLedger();
3632
3556
  const rowPresent = (id) => Object.prototype.hasOwnProperty.call(rows, id);
3633
3557
  const report = {
@@ -3729,16 +3653,16 @@ export class FileMemoryEngineBackend {
3729
3653
  }
3730
3654
  report.governanceWithheld = [...withheldIds].sort();
3731
3655
  report.referentialOrphans = [...orphanIds].sort();
3732
- report.lineageDivergence = importLineageCommitted(ctl, lineageToApply);
3733
- this.appendImportChallengesVerified(challengesToApply.map((c) => ({
3656
+ report.lineageDivergence = await importLineageCommitted(this.governance, lineageToApply);
3657
+ await this.appendImportChallengesVerified(challengesToApply.map((c) => ({
3734
3658
  eventId: `imp:${bundleHash}:${c.eventId}`,
3735
3659
  entryId: c.entryId,
3736
3660
  reason: c.reason,
3737
3661
  ...(c.challengedRev !== undefined ? { challengedRev: c.challengedRev } : {}),
3738
3662
  })));
3739
3663
  for (const p of [...plan.pollutedSessions].sort((a, b) => (a.sessionId < b.sessionId ? -1 : 1))) {
3740
- const contributions = lineageContributionsOfSession(ctl, p.sessionId).filter((c) => c.marked !== true);
3741
- this.appendImportChallengesVerified(contributions.map((c) => ({
3664
+ const contributions = (await lineageContributionsOfSession(this.governance, p.sessionId)).filter((c) => c.marked !== true);
3665
+ await this.appendImportChallengesVerified(contributions.map((c) => ({
3742
3666
  eventId: `imp-sweep:${bundleHash}:pollution:${p.sessionId}:${p.at}:${c.entryId}`,
3743
3667
  entryId: c.entryId,
3744
3668
  reason: `challenged: the contributing session was marked polluted`,
@@ -3776,7 +3700,7 @@ export class FileMemoryEngineBackend {
3776
3700
  }
3777
3701
  throw err;
3778
3702
  }
3779
- releaseImportLatch(ctl, txnId);
3703
+ await releaseImportLatch(this.governance, ctl, txnId);
3780
3704
  return report;
3781
3705
  }
3782
3706
  catch (err) {