@sema-agent/core 7.64.0 → 7.66.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 (192) hide show
  1. package/CHANGELOG.md +571 -0
  2. package/dist/agents/delegation-registry-seat.d.ts +15 -0
  3. package/dist/agents/delegation-registry-seat.js +13 -0
  4. package/dist/agents/org-delivery-gate.d.ts +8 -1
  5. package/dist/agents/org-delivery-gate.js +9 -3
  6. package/dist/agents/peer-held-queue.js +1 -1
  7. package/dist/agents/peer-idle.js +1 -1
  8. package/dist/agents/peer-session-drain.d.ts +51 -12
  9. package/dist/agents/peer-session-drain.js +47 -34
  10. package/dist/agents/read-inbox-tool.d.ts +6 -2
  11. package/dist/agents/read-inbox-tool.js +18 -5
  12. package/dist/agents/subagent.d.ts +3 -1
  13. package/dist/agents/subagent.js +84 -77
  14. package/dist/brain/reasoning.d.ts +2 -0
  15. package/dist/brain/reasoning.js +1 -0
  16. package/dist/core/allow-entry.d.ts +9 -0
  17. package/dist/core/allow-entry.js +7 -1
  18. package/dist/core/ask-origin.d.ts +23 -3
  19. package/dist/core/ask-origin.js +11 -1
  20. package/dist/core/auto-promote.d.ts +3 -2
  21. package/dist/core/background-agent-store.d.ts +7 -0
  22. package/dist/core/checkpoint-store.d.ts +2 -3
  23. package/dist/core/context-settings.d.ts +1 -2
  24. package/dist/core/context-settings.js +1 -1
  25. package/dist/core/engine-attestation.d.ts +2 -2
  26. package/dist/core/engine-notice.d.ts +13 -1
  27. package/dist/core/engine-notice.js +3 -1
  28. package/dist/core/file-history-store.d.ts +2 -2
  29. package/dist/core/file-history-store.js +0 -4
  30. package/dist/core/gate-fold.js +3 -1
  31. package/dist/core/governance-codes.d.ts +6 -4
  32. package/dist/core/governance-codes.js +5 -3
  33. package/dist/core/human-input-projection.d.ts +9 -0
  34. package/dist/core/human-input-projection.js +13 -0
  35. package/dist/core/input-author.d.ts +66 -6
  36. package/dist/core/input-author.js +21 -3
  37. package/dist/core/leg-identity.d.ts +18 -2
  38. package/dist/core/leg-identity.js +5 -7
  39. package/dist/core/mailbox-store.d.ts +1 -1
  40. package/dist/core/memory-engine/consolidation.d.ts +30 -10
  41. package/dist/core/memory-engine/consolidation.js +41 -6
  42. package/dist/core/memory-engine/control-objects.d.ts +36 -3
  43. package/dist/core/memory-engine/control-objects.js +134 -12
  44. package/dist/core/memory-engine/control-plane-adoption.d.ts +13 -9
  45. package/dist/core/memory-engine/control-plane-adoption.js +58 -31
  46. package/dist/core/memory-engine/delegation-settlement.d.ts +75 -52
  47. package/dist/core/memory-engine/delegation-settlement.js +211 -228
  48. package/dist/core/memory-engine/dual-root.d.ts +3 -2
  49. package/dist/core/memory-engine/dual-root.js +5 -1
  50. package/dist/core/memory-engine/engine.d.ts +33 -16
  51. package/dist/core/memory-engine/engine.js +213 -177
  52. package/dist/core/memory-engine/file-backend.d.ts +21 -12
  53. package/dist/core/memory-engine/file-backend.js +58 -104
  54. package/dist/core/memory-engine/index.d.ts +2 -2
  55. package/dist/core/memory-engine/index.js +2 -2
  56. package/dist/core/memory-engine/layout.d.ts +184 -83
  57. package/dist/core/memory-engine/layout.js +267 -172
  58. package/dist/core/memory-engine/memory-age.d.ts +12 -0
  59. package/dist/core/memory-engine/memory-age.js +17 -0
  60. package/dist/core/memory-engine/memory-backend-contract.js +12 -11
  61. package/dist/core/memory-engine/memory-deployment.d.ts +4 -3
  62. package/dist/core/memory-engine/memory-deployment.js +6 -3
  63. package/dist/core/memory-engine/origin-clearance.d.ts +43 -15
  64. package/dist/core/memory-engine/origin-clearance.js +142 -58
  65. package/dist/core/memory-engine/tools.d.ts +2 -2
  66. package/dist/core/memory-engine/tools.js +7 -7
  67. package/dist/core/memory-engine/types.d.ts +16 -10
  68. package/dist/core/memory-vector.d.ts +6 -14
  69. package/dist/core/memory.d.ts +0 -462
  70. package/dist/core/memory.js +0 -333
  71. package/dist/core/model-receipt.d.ts +2 -1
  72. package/dist/core/model-receipt.js +5 -0
  73. package/dist/core/mods/dollar.d.ts +12 -0
  74. package/dist/core/mods/kernel/dispatch.d.ts +3 -1
  75. package/dist/core/mods/kernel/dispatch.js +5 -2
  76. package/dist/core/mods/kernel/events.d.ts +3 -1
  77. package/dist/core/mods/kernel/events.js +2 -2
  78. package/dist/core/mods/prompt-submit.d.ts +4 -0
  79. package/dist/core/mods/prompt-submit.js +24 -0
  80. package/dist/core/mods/stations.d.ts +23 -4
  81. package/dist/core/mods/stations.js +14 -7
  82. package/dist/core/permission-rule-consent.d.ts +49 -18
  83. package/dist/core/permission-rule-consent.js +59 -38
  84. package/dist/core/permission-rule-store.d.ts +24 -14
  85. package/dist/core/retention.d.ts +2 -7
  86. package/dist/core/retention.js +0 -1
  87. package/dist/core/retired-host-table-account.d.ts +19 -0
  88. package/dist/core/retired-host-table-account.js +28 -0
  89. package/dist/core/retired-tool-name-account.d.ts +60 -0
  90. package/dist/core/retired-tool-name-account.js +32 -0
  91. package/dist/core/roles.d.ts +3 -1
  92. package/dist/core/roles.js +5 -2
  93. package/dist/core/runner/classifier-ladder.d.ts +3 -1
  94. package/dist/core/runner/classifier-ladder.js +2 -2
  95. package/dist/core/runner/compact-session.js +2 -1
  96. package/dist/core/runner/contracts.d.ts +9 -9
  97. package/dist/core/runner/delegation-owner.d.ts +0 -8
  98. package/dist/core/runner/delegation-owner.js +1 -3
  99. package/dist/core/runner/gate-exit.js +9 -2
  100. package/dist/core/runner/memory-consolidation-driver.js +2 -2
  101. package/dist/core/runner/prepare-config-doors.js +1 -1
  102. package/dist/core/runner/prepare-delegation-surface.d.ts +1 -1
  103. package/dist/core/runner/prepare-delegation-surface.js +5 -3
  104. package/dist/core/runner/prepare-gate-stations.d.ts +8 -5
  105. package/dist/core/runner/prepare-gate-stations.js +13 -5
  106. package/dist/core/runner/prepare-inherited-gate.js +2 -0
  107. package/dist/core/runner/prepare-memory-engine-session.js +1 -1
  108. package/dist/core/runner/prepare-memory.js +35 -14
  109. package/dist/core/runner/prepare-policy-chain.js +12 -3
  110. package/dist/core/runner/prepare-project-context.js +2 -2
  111. package/dist/core/runner/prepare-task.d.ts +9 -1
  112. package/dist/core/runner/prepare-task.js +6 -5
  113. package/dist/core/runner/prepare-wiring-manifest.js +5 -4
  114. package/dist/core/runner/prompt-suggestions.js +1 -1
  115. package/dist/core/runner/run-identity-wiring.js +2 -2
  116. package/dist/core/runner/run-leg.js +3 -3
  117. package/dist/core/runner/run-notification-lane.d.ts +3 -0
  118. package/dist/core/runner/run-notification-lane.js +2 -2
  119. package/dist/core/runner/runtask.js +6 -3
  120. package/dist/core/runner/steer-admission.d.ts +11 -2
  121. package/dist/core/runner/stream-steer-verb.d.ts +5 -2
  122. package/dist/core/runner/stream-steer-verb.js +67 -14
  123. package/dist/core/runner/sub-agent-deny-lists.d.ts +8 -8
  124. package/dist/core/runner/sub-agent-deny-lists.js +22 -8
  125. package/dist/core/runner-deps.d.ts +4 -4
  126. package/dist/core/secret-env.d.ts +2 -2
  127. package/dist/core/secret-env.js +6 -20
  128. package/dist/core/session-store.d.ts +6 -6
  129. package/dist/core/session-store.js +3 -3
  130. package/dist/core/shell-run-candidates.js +3 -3
  131. package/dist/core/side-query.js +1 -1
  132. package/dist/core/skill-grant.d.ts +7 -0
  133. package/dist/core/skill-grant.js +14 -5
  134. package/dist/core/store-contracts/background-agent-store-contract.js +13 -0
  135. package/dist/core/task-event.d.ts +14 -0
  136. package/dist/core/task-notification.d.ts +35 -8
  137. package/dist/core/task-notification.js +31 -13
  138. package/dist/core/task-registry-agent.js +1 -0
  139. package/dist/core/task-registry-shared.d.ts +4 -0
  140. package/dist/core/tool-errors.d.ts +3 -3
  141. package/dist/core/tool-policy.js +2 -3
  142. package/dist/core/tool-spec.d.ts +20 -10
  143. package/dist/core/user-attachment.d.ts +5 -1
  144. package/dist/core/user-attachment.js +11 -5
  145. package/dist/core/write-protect.d.ts +16 -0
  146. package/dist/core/write-protect.js +10 -0
  147. package/dist/engine/harness/agent-harness.d.ts +19 -11
  148. package/dist/engine/harness/agent-harness.js +7 -4
  149. package/dist/engine/llm/types.d.ts +22 -10
  150. package/dist/engine/session/repo-utils.d.ts +14 -4
  151. package/dist/engine/session/repo-utils.js +5 -4
  152. package/dist/index.d.ts +10 -8
  153. package/dist/index.js +9 -7
  154. package/dist/prompts/default.d.ts +1 -2
  155. package/dist/scenarios/full-body.d.ts +1 -1
  156. package/dist/shell/lex.d.ts +50 -15
  157. package/dist/shell/lex.js +43 -9
  158. package/dist/stores/file/adoption/adopt.d.ts +1 -1
  159. package/dist/stores/file/index.d.ts +1 -15
  160. package/dist/stores/file/index.js +1 -15
  161. package/dist/stores/file/permission-rule-store.d.ts +12 -1
  162. package/dist/stores/file/permission-rule-store.js +31 -4
  163. package/dist/stores/file/session-store.js +1 -1
  164. package/dist/tools/fs/fs-bash.js +3 -3
  165. package/dist/tools/fs/removal-targets.d.ts +8 -2
  166. package/dist/tools/fs/removal-targets.js +31 -4
  167. package/dist/tools/fs/shell-read-argv.js +7 -1
  168. package/dist/tools/fs/shell-read-bindings.d.ts +21 -5
  169. package/dist/tools/fs/shell-read-bindings.js +130 -44
  170. package/dist/tools/fs/shell-read-plan.js +95 -29
  171. package/dist/tools/fs/shell-read-sourced.d.ts +25 -0
  172. package/dist/tools/fs/shell-read-sourced.js +119 -0
  173. package/dist/tools/fs/shell-read-word.d.ts +2 -2
  174. package/dist/tools/fs/shell-read-word.js +4 -5
  175. package/dist/tools/fs/shell-read-workdir.d.ts +19 -3
  176. package/dist/tools/fs/shell-read-workdir.js +60 -15
  177. package/dist/tools/fs/shell-reentry-text.d.ts +10 -2
  178. package/dist/tools/fs/shell-reentry-text.js +11 -4
  179. package/package.json +2 -2
  180. package/test/export-surface.snapshot.json +21 -79
  181. package/wire-changes/core-stores-sql@7.65.0.json +30 -0
  182. package/wire-changes/core-stores-sql@7.66.0.json +30 -0
  183. package/wire-changes/core@7.65.0.json +1650 -0
  184. package/wire-changes/core@7.66.0.json +1218 -0
  185. package/wire-changes/mods-kernel@7.65.0.json +30 -0
  186. package/wire-changes/mods-kernel@7.66.0.json +65 -0
  187. package/wire-changes/primitives@7.65.0.json +337 -0
  188. package/wire-changes/primitives@7.66.0.json +30 -0
  189. package/dist/core/memory-recall.d.ts +0 -208
  190. package/dist/core/memory-recall.js +0 -136
  191. package/dist/stores/file/memory-store.d.ts +0 -124
  192. package/dist/stores/file/memory-store.js +0 -353
@@ -1,26 +1,28 @@
1
1
  import { createHash } from "node:crypto";
2
- import { accessSync, closeSync, constants as fsConstants, existsSync, fsyncSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, renameSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
2
+ import { accessSync, closeSync, constants as fsConstants, existsSync, fsyncSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, renameSync, rmSync, unlinkSync } from "node:fs";
3
3
  import { basename, dirname, join, relative, sep } from "node:path";
4
4
  import { uuidv7 } from "../../internal/harness.js";
5
5
  import { MAX_MEMORY_BYTES, composeMemoryBlock, firstSentence } from "../memory.js";
6
6
  import { inlineUntrusted } from "../untrusted-text.js";
7
7
  import { canonicalJsonStringify } from "../canonical-json.js";
8
8
  import { mintSystemReminder } from "../reminder-mint.js";
9
- import { formatMemoryAge } from "../memory-recall.js";
9
+ import { formatMemoryAge } from "./memory-age.js";
10
10
  import { committedDistilledOf, committedOriginOf, computeEntryRev, consolidationExposedFrontmatter, distilledEquals, hasDistilledFormExtra, hasOriginFormExtra, isValidEntryId, originEquals, parseEntryFile, serializeEntryFile, stripModelWrittenDistilled, stripModelWrittenOrigin, UNCLOSED_MEMORY_FILE_REASON } from "./frontmatter.js";
11
11
  import { MEMORY_PROVENANCE_RECALL_SENTENCE, memoryExposureIndexRow, parseMemoryExposureIndexRow } from "./provenance-wording.js";
12
12
  import { isInstructionEntry } from "./header-hints.js";
13
- import { ConsolidationRefusedError, CONSOLIDATION_WRITE_REFUSED_CODE, CONSOLIDATION_FORCE_THROTTLE_FLOOR_MS, CONSOLIDATION_SEAT_FREEZE_GRACE_MS, consolidationTypeEligible, deriveProductSlug, deriveSupersededSet, fileGovernanceStore, fileGovernanceStoreDir, isValidConsolidationPlanId, productAddPatch, recordActiveSetBaseline, recordConsolidationSession, sessionsSinceExcludingCurrent, scanRefusalCeiling, screenConsolidationOptions, memoryGovernanceDurability, supersessionFuseCeiling, deletionFuseCeiling, MEMORY_CHALLENGED_ENTRY_GUIDANCE, } from "./consolidation.js";
13
+ import { ConsolidationRefusedError, CONSOLIDATION_WRITE_REFUSED_CODE, CONSOLIDATION_FORCE_THROTTLE_FLOOR_MS, CONSOLIDATION_SEAT_FREEZE_GRACE_MS, consolidationTypeEligible, deriveProductSlug, deriveSupersededSet, fileGovernanceStore, governanceCaptureRecordStore, fileGovernanceStoreDir, governanceStoreIsFileOver, standingControlObjectsOf, isValidConsolidationPlanId, productAddPatch, recordActiveSetBaseline, recordConsolidationSession, sessionsSinceExcludingCurrent, scanRefusalCeiling, screenConsolidationOptions, memoryGovernanceDurability, supersessionFuseCeiling, deletionFuseCeiling, MEMORY_CHALLENGED_ENTRY_GUIDANCE, } from "./consolidation.js";
14
14
  import { deriveShadowedSet } from "./shadowing.js";
15
15
  import { classifySessionSettlements, clearHoldResolution, closeSessionAccount, disposeHold, effectiveSettlements, expireOverdueSettlements, foreignDanglingSessionAccounts, openLayerWriters, terminateSessionAccount, sessionAccountLeg, markHoldReleased, openInstructionHold, openSessionAccount, readHoldCustody, readHolds, reconcileHolds, replayExternalSettlementEffects, resolveHoldRecord, resolveSessionAccountRecord, resolveSettlementRecord, sessionSettlements, sessionUnattributedSet, emptyUnattributedSet, convertV1SidecarKeys, } from "./delegation-settlement.js";
16
- import { noReplaceRestore, retireStagingToCustody, stagingPathBeside } from "./delegation-settlement.js";
16
+ import { noReplaceRestore, retireStagingToObject, stagingPathBeside } from "./delegation-settlement.js";
17
+ import { probeControlPlaneLinks } from "./control-objects.js";
18
+ import { replicaControlPlaneStrays } from "./control-plane-adoption.js";
17
19
  import { sessionWriteHomeOf } from "./session-write-home.js";
18
20
  import { landedUndecided, layerDigest, layerObservationLookup, readOnlyLayerCandidates, readOnlyLayerCensus, recordLayerObservations, settleUndecidedLayerObservations, observationBacked, sameObservation, undecidedLayerObservations } from "./layer-observations.js";
19
- import { markOriginClearanceTombstoned, openOriginClearance, readOriginClearances, settleOriginClearance } from "./origin-clearance.js";
21
+ import { clearanceCustodyErased, eraseOriginClearanceCustody, markOriginClearanceTombstoned, openOriginClearance, readOriginClearances, settleOriginClearance } from "./origin-clearance.js";
20
22
  import { carriesEntry } from "./entry-scan.js";
21
23
  import { DEFAULT_MAX_ENTRY_DEPTH, MEMORY_INDEX_FILENAME, captureErasureInput, erasureRequestInvalid, erasureScopeRefusal, erasureSelectHash, erasureStatusOf, sweepUnderErasureRecord, scanEntryFiles, } from "./file-backend.js";
22
24
  import { assembleMemoryExportBundle, canonicalExpectedScopes, computeMemoryBundleHash, expectedScopesInvalid, firstOutsideExpectedScopes, memoryBundleInvalid, } from "./export-bundle.js";
23
- import { QUARANTINE_DIR, SCAN_FUSE_THRESHOLD, quarantineAndTombstone, restoreContainedBaseline, mintStagingName, indexLineIsStale, readIndexRevs, writeIndexRevs, bumpScanFuse, scanFuseKey, canonicalize, anchorStoreRoot, landingPathOf, landsOutsideStore, storeActRefusal, storeRootMovedRefusal, adoptRootScopeLayout, clearScanFuse, effectiveControlRootOf, sayAnnounceFailure, drainMemoryAnnouncements, enqueueMemoryAnnouncement, ensureDirExists, markSessionPolluted, readSessionPollution, fileSessionCaptureRecordStore, recordRetrievedAccount, writeFileNoFollow, writeFileNoReplace, withPermissionRemedy, permissionRemedyOf, readRetrievedAccount, registerScope, registeredScopes, scopeDirFor, appendChallengeEvents, appendLineageAudit, rebuildStrictControlPlaneLedger, isStrictControlPlaneLedgerCorrupt, CHALLENGE_LEDGER_MAX_EVENTS, adjudicateLineagePending, challengedEntryIds, clearLineageForEntries, discardLineagePending, lineageAccountOfEntry, lineageContributionsOfSession, lineageLatchedIds, promoteLineagePending, readChallengeEvents, readChallengedHistory, readLineageRecord, recordChallengedHistory, recordLineageCredential, reconcileLineage, readProjectionDebts, resolveChallengeEvent, settleProjectionDebts, stageLineagePending, stageProjectionDebts, } from "./layout.js";
25
+ import { SCAN_FUSE_THRESHOLD, captureIntoQuarantine, clearContainedPath, eraseQuarantineCopies, keepCopiesBeforeOverwrite, putControlObjectOnce, restoreContainedBaseline, mintStagingName, indexLineIsStale, readIndexRevs, writeIndexRevs, bumpScanFuse, scanFuseKey, canonicalize, anchorStoreRoot, landingPathOf, landsOutsideStore, storeActRefusal, storeRootMovedRefusal, adoptRootScopeLayout, clearScanFuse, effectiveControlRootOf, sayAnnounceFailure, drainMemoryAnnouncements, enqueueMemoryAnnouncement, ensureDirExists, markSessionPolluted, readSessionPollution, recordRetrievedAccount, writeFileNoFollow, writeFileNoReplace, withPermissionRemedy, permissionRemedyOf, readRetrievedAccount, registerScope, registeredScopes, scopeDirFor, appendChallengeEvents, appendLineageAudit, rebuildStrictControlPlaneLedger, isStrictControlPlaneLedgerCorrupt, CHALLENGE_LEDGER_MAX_EVENTS, adjudicateLineagePending, challengedEntryIds, clearLineageForEntries, discardLineagePending, lineageAccountOfEntry, lineageContributionsOfSession, lineageLatchedIds, promoteLineagePending, readChallengeEvents, readChallengedHistory, readLineageRecord, recordChallengedHistory, recordLineageCredential, reconcileLineage, readProjectionDebts, resolveChallengeEvent, settleProjectionDebts, stageLineagePending, stageProjectionDebts, controlPlaneStraysRefusal, } from "./layout.js";
24
26
  import { createSessionIncarnationSeat, recalledForAnyIdentity, recalledForIdentity, rememberForIdentity } from "./session-incarnation.js";
25
27
  import { scanMemoryFileName, scanMemoryWrite, scanRemediation } from "./scan.js";
26
28
  import { writableRootOf } from "./types.js";
@@ -120,7 +122,6 @@ function homeRel(home, canonical) {
120
122
  export const DEFAULT_HOLD_SETTLE_TIMEOUT_MS = 72 * 60 * 60 * 1000;
121
123
  export const MASS_DELETION_FUSE_RATIO = 0.5;
122
124
  const MEMORY_ENGINE_OPTION_KEYS = optionKeys()({ backend: 0, memoryDir: 0, controlDir: 0, now: 0, perScopeBudgetBytes: 0, perFileBytes: 0, maxFiles: 0, maxDepth: 0, provenance: 0, captureRecordStore: 0, sessionIncarnation: 0, holdSettleTimeoutMs: 0, harvestDeadlineMs: 0, harvestFileBudget: 0, onIncident: 0, consolidation: 0 });
123
- let indexCaptureSeq = 0;
124
125
  export function memorySessionPollutedNotice(input) {
125
126
  const reason = inlineUntrusted(input.reason, 200);
126
127
  const message = input.provenance === "carry"
@@ -373,6 +374,7 @@ export class MemoryEngine {
373
374
  memoryDir;
374
375
  controlDir;
375
376
  governanceStore;
377
+ ownsControlPlaneRecords;
376
378
  now;
377
379
  perScopeBudgetBytes;
378
380
  perFileBytes;
@@ -405,6 +407,12 @@ export class MemoryEngine {
405
407
  this.zeroCopy = typeof pinned === "string" && pinned !== "";
406
408
  this.controlDir = effectiveControlRootOf(o.memoryDir, { pinnedBy: o.backend, controlDir: o.controlDir, onIncident: o.onIncident });
407
409
  this.governanceStore = o.backend.governance ?? fileGovernanceStore(this.controlDir);
410
+ this.ownsControlPlaneRecords = governanceStoreIsFileOver(this.governanceStore, this.controlDir);
411
+ if (!this.ownsControlPlaneRecords) {
412
+ const strays = replicaControlPlaneStrays(this.controlDir);
413
+ if (strays.docs.length > 0 || Object.keys(strays.objects).length > 0)
414
+ throw controlPlaneStraysRefusal(this.controlDir, strays);
415
+ }
408
416
  this.now = o.now ?? Date.now;
409
417
  this.perScopeBudgetBytes = o.perScopeBudgetBytes ?? Number.MAX_SAFE_INTEGER;
410
418
  this.perFileBytes = o.perFileBytes ?? MAX_MEMORY_BYTES;
@@ -423,7 +431,7 @@ export class MemoryEngine {
423
431
  if (o.consolidation !== undefined) {
424
432
  this.consolidation = screenConsolidationOptions(o.consolidation, this.provenance, memoryGovernanceDurability(o.backend));
425
433
  }
426
- this.captureRecords = o.captureRecordStore ?? fileSessionCaptureRecordStore(this.controlDir);
434
+ this.captureRecords = o.captureRecordStore ?? governanceCaptureRecordStore(this.governanceStore);
427
435
  this.incarnation = createSessionIncarnationSeat({ resolve: o.sessionIncarnation, onIncident: o.onIncident });
428
436
  }
429
437
  incarnation;
@@ -436,7 +444,7 @@ export class MemoryEngine {
436
444
  }
437
445
  async endLeg(sessionId, leg) {
438
446
  try {
439
- return await terminateSessionAccount(this.governanceStore, this.controlDir, { sessionId, now: this.now, leg });
447
+ return await terminateSessionAccount(this.governanceStore, { sessionId, now: this.now, leg });
440
448
  }
441
449
  catch (err) {
442
450
  this.discloseSessionAccountIncident("the end of this session's leg could not be recorded — it keeps reading as a live writer of its layer (read-only restores there are withheld)", err);
@@ -488,7 +496,7 @@ export class MemoryEngine {
488
496
  }
489
497
  catch {
490
498
  }
491
- return this.backend.readCommittedShadow?.(id) ?? readSafe(join(this.controlDir, "shadow", `${id}.md`));
499
+ return this.backend.readCommittedShadow?.(id);
492
500
  }
493
501
  async restoreReadOnlyLayers(candidates, report, selfLeg, handle) {
494
502
  let writers;
@@ -502,7 +510,7 @@ export class MemoryEngine {
502
510
  if (committed === undefined)
503
511
  continue;
504
512
  try {
505
- writers ??= await openLayerWriters(this.governanceStore, this.controlDir, selfLeg);
513
+ writers ??= await openLayerWriters(this.governanceStore, selfLeg);
506
514
  }
507
515
  catch {
508
516
  writers = { scopes: new Set(), unscoped: 1 };
@@ -778,28 +786,22 @@ export class MemoryEngine {
778
786
  async markSessionPolluted(sessionId, reason, cause) {
779
787
  const incarnation = this.incarnation.incarnationOf(sessionId);
780
788
  rememberForIdentity(this.pollutedSessions, sessionId, { at: this.now(), reason, ...(cause !== undefined ? { cause } : {}), ...(incarnation !== undefined ? { incarnation } : {}) });
781
- let outcome;
782
- try {
783
- outcome = markSessionPolluted(this.controlDir, sessionId, reason, this.now, cause, incarnation);
784
- }
785
- catch {
786
- outcome = "unpersisted";
787
- }
788
- if (outcome === "unpersisted") {
789
- const sink = this.onIncident;
790
- if (sink !== undefined) {
791
- try {
792
- const err = new Error(`memory pollution marker for session could not be persisted — the mark holds in-process only until it succeeds`);
793
- err.code = "memory.pollution_mark_failed";
794
- sink(err);
795
- }
796
- catch {
789
+ const written = markSessionPolluted(this.governanceStore, sessionId, reason, this.now, cause, incarnation).catch(() => "unpersisted");
790
+ const sweep = (async () => {
791
+ if ((await written) === "unpersisted") {
792
+ const sink = this.onIncident;
793
+ if (sink !== undefined) {
794
+ try {
795
+ const err = new Error(`memory pollution marker for session could not be persisted — the mark holds in-process only until it succeeds`);
796
+ err.code = "memory.pollution_mark_failed";
797
+ sink(err);
798
+ }
799
+ catch {
800
+ }
797
801
  }
798
802
  }
799
- }
800
- const sweep = (async () => {
801
803
  try {
802
- const rec = this.sessionPollution(sessionId) ?? { at: this.now(), reason };
804
+ const rec = (await this.sessionPollution(sessionId)) ?? { at: this.now(), reason };
803
805
  const swept = await lineageContributionsOfSession(this.governanceStore, sessionId, incarnation);
804
806
  const contributions = this.provenance === "carry" ? swept.filter((c) => c.marked !== true) : swept;
805
807
  await this.challengeAndAnnounce(contributions.map((c) => ({
@@ -829,19 +831,14 @@ export class MemoryEngine {
829
831
  finally {
830
832
  this.pendingPollutionSweeps.delete(sweep);
831
833
  }
832
- return outcome;
834
+ return written;
833
835
  }
834
- sessionPollution(sessionId) {
836
+ async sessionPollution(sessionId) {
835
837
  const identity = this.incarnation.identityRead(sessionId);
836
838
  const inProcess = recalledForIdentity(this.pollutedSessions, sessionId, identity.incarnation);
837
839
  if (inProcess !== undefined)
838
840
  return inProcess;
839
- try {
840
- return readSessionPollution(this.controlDir, sessionId, identity);
841
- }
842
- catch {
843
- return undefined;
844
- }
841
+ return readSessionPollution(this.governanceStore, sessionId, identity);
845
842
  }
846
843
  captureRecords;
847
844
  captureOptOutSessions = new Map();
@@ -921,13 +918,15 @@ export class MemoryEngine {
921
918
  if (first !== undefined && first !== baseline) {
922
919
  const onDisk = readSafe(indexPath);
923
920
  if (onDisk !== undefined && onDisk !== baseline) {
924
- const q = quarantineAndTombstone(indexPath, onDisk, join(this.controlDir, QUARANTINE_DIR), this.now);
921
+ const q = await this.containSuspect(indexPath, onDisk, "sweeping the index", handle);
925
922
  let contained = q.removed;
926
923
  if (baseline !== undefined) {
927
924
  try {
928
- writeFileNoFollow(indexPath, baseline);
929
- out.restored.push(MEMORY_INDEX_FILENAME);
930
- contained = true;
925
+ this.refuseOutsideStore(indexPath, "restoring the index", handle);
926
+ if (restoreContainedBaseline(indexPath, baseline, q.leftOnPlane) === "written") {
927
+ out.restored.push(MEMORY_INDEX_FILENAME);
928
+ contained = true;
929
+ }
931
930
  }
932
931
  catch {
933
932
  }
@@ -1002,9 +1001,10 @@ export class MemoryEngine {
1002
1001
  out.failures.push({ path: rel, detail: `${refusedHere} — not swept` });
1003
1002
  continue;
1004
1003
  }
1005
- const q = quarantineAndTombstone(canonical, text2, join(this.controlDir, QUARANTINE_DIR), this.now);
1004
+ const q = await this.containSuspect(canonical, text2, "sweeping a residue file", handle);
1006
1005
  let contained = q.removed;
1007
- if (committed !== undefined) {
1006
+ const refusedRestore = committed !== undefined ? this.storeActRefusedText(canonical, "restoring a residue file's baseline", handle) : undefined;
1007
+ if (committed !== undefined && refusedRestore === undefined) {
1008
1008
  const put = restoreContainedBaseline(canonical, committed, q.leftOnPlane);
1009
1009
  if (put === "written") {
1010
1010
  out.restored.push(rel);
@@ -1014,10 +1014,10 @@ export class MemoryEngine {
1014
1014
  out.withheld.push({ path: rel, ...(parsedId !== undefined ? { entryId: parsedId, rev: revOfText(committed, parsedId) } : {}) });
1015
1015
  }
1016
1016
  }
1017
- if (q.dest !== undefined && contained)
1017
+ if (q.dest !== undefined && contained && refusedRestore === undefined)
1018
1018
  out.swept.push(rel);
1019
1019
  else
1020
- out.failures.push({ path: rel, detail: q.detail ?? (q.dest === undefined ? "quarantine capture failed" : "removal from the authoritative plane incomplete") });
1020
+ out.failures.push({ path: rel, detail: refusedRestore !== undefined ? `${refusedRestore} — the committed baseline was not restored` : q.detail ?? (q.dest === undefined ? "quarantine capture failed" : "removal from the authoritative plane incomplete") });
1021
1021
  }
1022
1022
  return out;
1023
1023
  }
@@ -1180,7 +1180,7 @@ export class MemoryEngine {
1180
1180
  e.code = "config.memory_erasure_scope";
1181
1181
  throw e;
1182
1182
  }
1183
- const att = await face.call(this.backend, captured);
1183
+ const att = await this.answerClearanceCustody(await face.call(this.backend, captured));
1184
1184
  return this.sweepErasedIndexLines(att, requestId, (recorded) => ({ paths: recorded, homes: att.erased.flatMap((row) => (row.binding.state === "bound" ? [scopeDirFor(this.memoryDir, this.controlDir, row.binding.scope)] : [])) }));
1185
1185
  }
1186
1186
  if (captured.allowUnevidenced !== true) {
@@ -1230,6 +1230,9 @@ export class MemoryEngine {
1230
1230
  const notFound = resolvedIds.filter((id) => !byId.has(id)).map((id) => ({ id, historyUnknown: true }));
1231
1231
  const conflicts = report.conflicts.map((c) => ({ id: c.id, reason: c.reason }));
1232
1232
  const unverified = { unverified: true };
1233
+ const quarantine = await eraseQuarantineCopies(standingControlObjectsOf(this.governanceStore), new Set(resolvedIds)).catch(() => undefined);
1234
+ const conflicted = new Set(conflicts.map((c) => c.id));
1235
+ const custody = await eraseOriginClearanceCustody(this.governanceStore, new Set(resolvedIds.filter((id) => !conflicted.has(id))), this.now()).catch(() => undefined);
1233
1236
  const att = {
1234
1237
  v: 2,
1235
1238
  requestId,
@@ -1244,8 +1247,8 @@ export class MemoryEngine {
1244
1247
  notFound,
1245
1248
  conflicts,
1246
1249
  residuals: {
1247
- carriers: { projection: unverified, shadow: unverified, indexLine: { removed: 0, remaining: 0 }, quarantineCopy: unverified, planArchive: unverified, planWorkbook: unverified, evidenceBinding: unverified, backendDerived: unverified },
1248
- quarantineUnattributed: unverified,
1250
+ carriers: { projection: unverified, shadow: unverified, indexLine: { removed: 0, remaining: 0 }, quarantineCopy: quarantine !== undefined ? { removed: quarantine.removed, remaining: quarantine.remaining, unreadable: quarantine.unreadable } : unverified, clearanceCustody: custody !== undefined ? { removed: custody.removed, remaining: custody.remaining } : unverified, planArchive: unverified, planWorkbook: unverified, evidenceBinding: unverified, backendDerived: unverified },
1251
+ quarantineUnattributed: quarantine !== undefined ? { count: quarantine.unattributed } : unverified,
1249
1252
  derivedEntries: unverified,
1250
1253
  replay: "unsupported",
1251
1254
  propagation: "local-store-only",
@@ -1261,6 +1264,28 @@ export class MemoryEngine {
1261
1264
  return { paths: deleted.map((e, i) => join(homes[i] ?? "", `${e.slug}.md`)), homes };
1262
1265
  });
1263
1266
  }
1267
+ async answerClearanceCustody(att) {
1268
+ let resolved;
1269
+ try {
1270
+ const counted = att.residuals.carriers.clearanceCustody;
1271
+ if (typeof counted === "object" && counted !== null && typeof counted.remaining === "number")
1272
+ return att;
1273
+ const conflicted = new Set(att.conflicts.map((c) => String(c.id)));
1274
+ resolved = att.resolvedIds.map(String).filter((id) => !conflicted.has(id));
1275
+ }
1276
+ catch {
1277
+ return att;
1278
+ }
1279
+ const custody = await eraseOriginClearanceCustody(this.governanceStore, new Set(resolved), this.now()).catch(() => undefined);
1280
+ try {
1281
+ const out = { ...att, residuals: { ...att.residuals, carriers: { ...att.residuals.carriers, clearanceCustody: custody !== undefined ? { removed: custody.removed, remaining: custody.remaining } : { unverified: true } } } };
1282
+ out.status = erasureStatusOf(out);
1283
+ return out;
1284
+ }
1285
+ catch {
1286
+ return att;
1287
+ }
1288
+ }
1264
1289
  async sweepErasedIndexLines(att, requestId, owedOf) {
1265
1290
  const uncleared = [];
1266
1291
  const indexScope = new Map();
@@ -1400,16 +1425,18 @@ export class MemoryEngine {
1400
1425
  const readEnginePlane = async () => {
1401
1426
  const challenged = (await challengedEntryIds(this.governanceStore)).get(entryId);
1402
1427
  const lineage = await lineageAccountOfEntry(this.governanceStore, entryId);
1428
+ const contributors = [];
1429
+ for (const c of lineage.contributors) {
1430
+ const polluted = await this.sessionPollution(c.sessionId);
1431
+ contributors.push({
1432
+ sessionId: c.sessionId,
1433
+ lastRev: c.lastRev,
1434
+ lastAt: c.lastAt,
1435
+ ...(polluted !== undefined ? { polluted: { at: polluted.at, reason: polluted.reason } } : {}),
1436
+ });
1437
+ }
1403
1438
  return {
1404
- contributors: lineage.contributors.map((c) => {
1405
- const polluted = this.sessionPollution(c.sessionId);
1406
- return {
1407
- sessionId: c.sessionId,
1408
- lastRev: c.lastRev,
1409
- lastAt: c.lastAt,
1410
- ...(polluted !== undefined ? { polluted: { at: polluted.at, reason: polluted.reason } } : {}),
1411
- };
1412
- }),
1439
+ contributors,
1413
1440
  exclusion: challenged !== undefined
1414
1441
  ? { code: "challenged", generation: challenged.generation, at: challenged.at }
1415
1442
  : lineage.pendingLatched
@@ -1629,13 +1656,7 @@ export class MemoryEngine {
1629
1656
  for (const p of promoted) {
1630
1657
  if (this.provenance === "carry" && p.marked === true)
1631
1658
  continue;
1632
- let rec;
1633
- try {
1634
- rec = readSessionPollution(this.controlDir, p.sessionId);
1635
- }
1636
- catch {
1637
- rec = undefined;
1638
- }
1659
+ let rec = await readSessionPollution(this.governanceStore, p.sessionId);
1639
1660
  if (rec === undefined)
1640
1661
  rec = recalledForAnyIdentity(this.pollutedSessions, p.sessionId);
1641
1662
  if (rec === undefined)
@@ -1661,7 +1682,7 @@ export class MemoryEngine {
1661
1682
  this.assertRootAnchored();
1662
1683
  }
1663
1684
  if (adopt === undefined || !this.zeroCopy)
1664
- adoptRootScopeLayout(this.memoryDir, this.controlDir, { ownerCandidate: writeScope, onIncident: this.onIncident, convertSidecars: convertV1SidecarKeys });
1685
+ adoptRootScopeLayout(this.memoryDir, this.controlDir, { ownerCandidate: writeScope, onIncident: this.onIncident, ...(this.ownsControlPlaneRecords ? { convertSidecars: convertV1SidecarKeys } : {}) });
1665
1686
  }
1666
1687
  async materialize(scopes, writeScope, opts) {
1667
1688
  const legSeat = {};
@@ -1679,6 +1700,8 @@ export class MemoryEngine {
1679
1700
  this.assertRootAnchored();
1680
1701
  ensureDirExists(this.memoryDir);
1681
1702
  ensureDirExists(this.controlDir);
1703
+ if (this.governanceStore !== this.backend.governance)
1704
+ probeControlPlaneLinks(this.controlDir);
1682
1705
  await this.adoptLayoutBeforeMount(writeScope);
1683
1706
  this.assertRootAnchored();
1684
1707
  const restricted = opts?.adoptionRestricted === true;
@@ -1702,7 +1725,7 @@ export class MemoryEngine {
1702
1725
  if (this.provenance === "carry" && opts?.sessionId !== undefined && writeScope !== null && !restricted) {
1703
1726
  let accountFailed = false;
1704
1727
  try {
1705
- const foreign = await foreignDanglingSessionAccounts(this.governanceStore, this.controlDir, opts.sessionId);
1728
+ const foreign = await foreignDanglingSessionAccounts(this.governanceStore, opts.sessionId);
1706
1729
  const unattributed = [];
1707
1730
  if (foreign.length > 0) {
1708
1731
  const wroot = canonicalize(scopeDirFor(this.memoryDir, this.controlDir, writeScope));
@@ -1756,11 +1779,11 @@ export class MemoryEngine {
1756
1779
  if (committed !== undefined && committed !== text) {
1757
1780
  let captured = false;
1758
1781
  const captureName = `${this.now()}-residue-${parsed.id}.md`;
1759
- const capturePath = join(this.controlDir, QUARANTINE_DIR, captureName);
1760
1782
  try {
1761
- ensureDirExists(join(this.controlDir, QUARANTINE_DIR));
1762
- writeFileSync(capturePath, text, { encoding: "utf8", flag: "wx" });
1763
- captured = readFileSync(capturePath, "utf8") === text;
1783
+ if ((await putControlObjectOnce(this.governanceStore, "quarantine", captureName, new TextEncoder().encode(text))) === "created") {
1784
+ const back = await this.governanceStore.getObject("quarantine", captureName);
1785
+ captured = back !== undefined && new TextDecoder().decode(back) === text;
1786
+ }
1764
1787
  }
1765
1788
  catch {
1766
1789
  captured = false;
@@ -1784,7 +1807,7 @@ export class MemoryEngine {
1784
1807
  else {
1785
1808
  const restore = noReplaceRestore(stagingPath, canonical);
1786
1809
  if (restore !== "restored") {
1787
- const retired = retireStagingToCustody(stagingPath, join(this.controlDir, QUARANTINE_DIR), stagingName);
1810
+ const retired = await retireStagingToObject(this.governanceStore, stagingPath, "quarantine", stagingName, () => this.storeActRefusedText(stagingPath, "removing a contained staging copy"));
1788
1811
  if (restore === "stranded" || !retired.settled)
1789
1812
  this.discloseSessionAccountIncident(`a concurrent write to ${rel} landed during residue containment and could not be restored — its bytes are preserved at ${retired.at}`, undefined);
1790
1813
  }
@@ -1811,13 +1834,13 @@ export class MemoryEngine {
1811
1834
  }
1812
1835
  }
1813
1836
  accountLeg = legSeat.leg = uuidv7();
1814
- await openSessionAccount(this.governanceStore, this.controlDir, { sessionId: opts.sessionId, now: this.now, unattributed, writeScope, leg: accountLeg });
1837
+ await openSessionAccount(this.governanceStore, { sessionId: opts.sessionId, now: this.now, unattributed, writeScope, leg: accountLeg });
1815
1838
  }
1816
1839
  catch (err) {
1817
1840
  accountFailed = true;
1818
1841
  if (accountLeg !== undefined) {
1819
1842
  try {
1820
- if (await sessionAccountLeg(this.governanceStore, this.controlDir, opts.sessionId) !== accountLeg)
1843
+ if (await sessionAccountLeg(this.governanceStore, opts.sessionId) !== accountLeg)
1821
1844
  accountLeg = legSeat.leg = undefined;
1822
1845
  }
1823
1846
  catch {
@@ -1916,7 +1939,7 @@ export class MemoryEngine {
1916
1939
  }
1917
1940
  }
1918
1941
  }
1919
- const indexGate = this.gateDerivedIndex(handle);
1942
+ const indexGate = await this.gateDerivedIndex(handle);
1920
1943
  if (indexGate !== undefined) {
1921
1944
  if (!indexGate.contained)
1922
1945
  handle.indexOnDiskUntrusted = true;
@@ -2121,7 +2144,7 @@ export class MemoryEngine {
2121
2144
  return report;
2122
2145
  }
2123
2146
  async harvestCore(handle, opts) {
2124
- const openingRecord = opts?.sessionId !== undefined ? this.sessionPollution(opts.sessionId) : undefined;
2147
+ const openingRecord = opts?.sessionId !== undefined ? await this.sessionPollution(opts.sessionId) : undefined;
2125
2148
  let pollutedReason = opts?.polluted?.reason ?? openingRecord?.reason;
2126
2149
  let pollutionCause = opts?.polluted?.cause ?? openingRecord?.cause;
2127
2150
  const carry = this.provenance === "carry";
@@ -2233,7 +2256,12 @@ export class MemoryEngine {
2233
2256
  this.assertRootAnchored(handle);
2234
2257
  const stranded = [];
2235
2258
  try {
2236
- reconcileHolds(this.controlDir, { homeOf: (scope) => scopeDirFor(this.memoryDir, this.controlDir, scope), now: this.now, onStranded: (scope, line) => stranded.push({ scope, line }) });
2259
+ await reconcileHolds(this.governanceStore, {
2260
+ homeOf: (scope) => scopeDirFor(this.memoryDir, this.controlDir, scope),
2261
+ now: this.now,
2262
+ onStranded: (scope, line) => stranded.push({ scope, line }),
2263
+ actRefused: (path, act) => this.storeActRefusedText(path, act, handle),
2264
+ });
2237
2265
  }
2238
2266
  finally {
2239
2267
  for (const s of stranded)
@@ -2243,7 +2271,7 @@ export class MemoryEngine {
2243
2271
  if (newlyExpired.length > 0) {
2244
2272
  this.warnScoped(report, undefined, `${newlyExpired.length} delegation settlement row(s) passed the ${this.holdSettleTimeoutMs}ms window without settling and are now EXPIRED — a timeout, not a conviction: the affected sessions' unprovable windows close exposed, their held instruction entries dispose to quarantine (resolveHold can still release one with a "static"-cause marker), and resolveSettlement can overturn the expiry with an audited ruling`);
2245
2273
  }
2246
- await replayExternalSettlementEffects(this.governanceStore, this.controlDir, { carry, now: this.now });
2274
+ await replayExternalSettlementEffects(this.governanceStore, { carry, now: this.now });
2247
2275
  const settled = await this.settleDueHolds(handle, report);
2248
2276
  for (const rel of settled.released)
2249
2277
  signalContainment((c) => pushDistinct(c.releasedHolds, rel));
@@ -2262,7 +2290,7 @@ export class MemoryEngine {
2262
2290
  sessionPending = true;
2263
2291
  pendingSettleIds = rows.filter((r) => r.effective === "pending").map((r) => r.row.settleId);
2264
2292
  }
2265
- unattributedSet = await sessionUnattributedSet(this.governanceStore, this.controlDir, lineageSessionId);
2293
+ unattributedSet = await sessionUnattributedSet(this.governanceStore, lineageSessionId);
2266
2294
  }
2267
2295
  }
2268
2296
  const rec = await reconcileLineage(this.governanceStore);
@@ -2273,7 +2301,7 @@ export class MemoryEngine {
2273
2301
  : `memory lineage transaction ${u.txnId} is unsettled (a crash landed between commit and its durable credential) — ${u.entryIds.length} entr${u.entryIds.length === 1 ? "y is" : "ies are"} latched (memory reads refuse them) until the host adjudicates it (adjudicatePendingLineage)`);
2274
2302
  }
2275
2303
  if (pollutedReason !== undefined && lineageSessionId !== undefined) {
2276
- const rec2 = this.sessionPollution(lineageSessionId);
2304
+ const rec2 = await this.sessionPollution(lineageSessionId);
2277
2305
  if (rec2 !== undefined) {
2278
2306
  const replaySwept = await lineageContributionsOfSession(this.governanceStore, lineageSessionId, this.incarnation.incarnationOf(lineageSessionId));
2279
2307
  const contributions = carry ? replaySwept.filter((c) => c.marked !== true) : replaySwept;
@@ -2357,7 +2385,7 @@ export class MemoryEngine {
2357
2385
  const pollutedStub = pollutedStubs.get(f.canonical);
2358
2386
  if (pollutedStub) {
2359
2387
  if (revOfText(f.text, pollutedStub.id) !== pollutedStub.rev) {
2360
- const sq = quarantineAndTombstone(f.canonical, f.text, join(this.controlDir, QUARANTINE_DIR), this.now);
2388
+ const sq = await this.containSuspect(f.canonical, f.text, "containing a withheld file", handle);
2361
2389
  const stubCaptured = sq.dest !== undefined;
2362
2390
  if (stubCaptured && sq.removed)
2363
2391
  report.movedToQuarantine.push(rel);
@@ -2374,7 +2402,7 @@ export class MemoryEngine {
2374
2402
  }
2375
2403
  return;
2376
2404
  }
2377
- const q = quarantineAndTombstone(f.canonical, f.text, join(this.controlDir, QUARANTINE_DIR), this.now);
2405
+ const q = await this.containSuspect(f.canonical, f.text, "containing a withheld file", handle);
2378
2406
  let contained = q.removed;
2379
2407
  const priorId = handle.baseIds.get(f.canonical);
2380
2408
  if (priorId !== undefined) {
@@ -2408,7 +2436,7 @@ export class MemoryEngine {
2408
2436
  report.warnings.push(`quarantine escalation for ${rel}: ${detail}`);
2409
2437
  }
2410
2438
  };
2411
- const restorePollutedIndex = () => {
2439
+ const restorePollutedIndex = async () => {
2412
2440
  const pollutedIndexPath = join(writableRoot, MEMORY_INDEX_FILENAME);
2413
2441
  const canonicalIndex = canonicalize(pollutedIndexPath);
2414
2442
  if (canonicalIndex !== pollutedIndexPath && !canonicalIndex.startsWith(`${writableRoot}${sep}`)) {
@@ -2418,19 +2446,22 @@ export class MemoryEngine {
2418
2446
  const indexNow = readSafe(pollutedIndexPath);
2419
2447
  if (indexNow === undefined || indexNow === handle.indexText)
2420
2448
  return false;
2421
- let indexCaptureLanded = false;
2422
- try {
2423
- const dest = join(this.controlDir, QUARANTINE_DIR, `${this.now()}-polluted-${MEMORY_INDEX_FILENAME}`);
2424
- ensureDirExists(dirname(dest));
2425
- writeFileSync(dest, indexNow, { encoding: "utf8", flag: "wx" });
2426
- indexCaptureLanded = true;
2427
- }
2428
- catch {
2429
- }
2449
+ const kept = await keepCopiesBeforeOverwrite(indexNow, async (bytes) => {
2450
+ try {
2451
+ return (await putControlObjectOnce(this.governanceStore, "quarantine", `${this.now()}-polluted-${MEMORY_INDEX_FILENAME}`, new TextEncoder().encode(bytes))) === "created" ? true : "an earlier capture stands under this name";
2452
+ }
2453
+ catch (err) {
2454
+ return thrownText(err);
2455
+ }
2456
+ }, () => {
2457
+ this.refuseOutsideStore(pollutedIndexPath, "reading the index again after its copy", handle);
2458
+ return readNoFollowSafe(pollutedIndexPath);
2459
+ });
2460
+ const indexCaptureLanded = kept.landed;
2430
2461
  try {
2431
2462
  this.refuseOutsideStore(pollutedIndexPath, "restoring the index", handle);
2432
2463
  writeFileNoFollow(pollutedIndexPath, handle.indexText);
2433
- report.warnings.push(`memory index restored to its pre-session state — this session's index additions were not retained (session polluted; ${indexCaptureLanded ? "the removed text was captured to quarantine" : "the quarantine capture did NOT land — the removed text is gone"})`);
2464
+ report.warnings.push(`memory index restored to its pre-session state — this session's index additions were not retained (session polluted; ${indexCaptureLanded ? "the removed text was captured to quarantine" : `the quarantine capture did NOT land for all of it (${kept.failed.join("; ")}) — the text it did not capture is gone`})`);
2434
2465
  return true;
2435
2466
  }
2436
2467
  catch (err) {
@@ -2441,7 +2472,7 @@ export class MemoryEngine {
2441
2472
  const containPollutedDomain = async () => {
2442
2473
  for (const f of records)
2443
2474
  await containPollutedRecord(f);
2444
- if (restorePollutedIndex())
2475
+ if (await restorePollutedIndex())
2445
2476
  signalContainment((c) => (c.indexRolledBack = true));
2446
2477
  report.warnings.push(`memory harvest committed nothing this session: ${inlineUntrusted(pollutedReason, 200)}`);
2447
2478
  };
@@ -2460,7 +2491,7 @@ export class MemoryEngine {
2460
2491
  signalContainment((c) => pushDistinct(c.quarantinedInstruction, f.rel));
2461
2492
  }
2462
2493
  }
2463
- if (restorePollutedIndex())
2494
+ if (await restorePollutedIndex())
2464
2495
  signalContainment((c) => (c.indexRolledBack = true));
2465
2496
  report.warnings.push(`memory harvest refused by an incident while this session is marked externally exposed — no entries were committed; instruction-form files were withheld and the derived index rolled back: ${inlineUntrusted(pollutedReason, 200)}`);
2466
2497
  };
@@ -2637,7 +2668,7 @@ export class MemoryEngine {
2637
2668
  const finding = scanFindings[0];
2638
2669
  if (finding !== undefined) {
2639
2670
  const refusedHere = this.storeActRefusedText(f.canonical, "quarantining a rejected file", handle);
2640
- const q = refusedHere === undefined ? quarantineAndTombstone(f.canonical, f.text, join(this.controlDir, QUARANTINE_DIR), this.now) : { removed: false, leftOnPlane: f.text, detail: refusedHere };
2671
+ const q = refusedHere === undefined ? await this.containSuspect(f.canonical, f.text, "quarantining a rejected file", handle) : { removed: false, leftOnPlane: f.text, detail: refusedHere };
2641
2672
  let contained = q.removed;
2642
2673
  const priorId = idByBasePath.get(f.canonical);
2643
2674
  if (priorId !== undefined) {
@@ -2645,9 +2676,14 @@ export class MemoryEngine {
2645
2676
  if (committed !== undefined) {
2646
2677
  try {
2647
2678
  this.refuseOutsideStore(f.canonical, "restoring a rejected file's committed content", handle);
2648
- writeFileNoFollow(f.canonical, committed);
2649
- report.restored.push(rel);
2650
- contained = true;
2679
+ const put = restoreContainedBaseline(f.canonical, committed, q.leftOnPlane);
2680
+ if (put === "written") {
2681
+ report.restored.push(rel);
2682
+ contained = true;
2683
+ }
2684
+ else if (put === "kept") {
2685
+ (report.restoreWithheld ??= []).push({ path: rel, entryId: priorId, rev: revOfText(committed, priorId) });
2686
+ }
2651
2687
  }
2652
2688
  catch {
2653
2689
  }
@@ -2747,7 +2783,7 @@ export class MemoryEngine {
2747
2783
  }
2748
2784
  if (instructionForm && sessionPending && lineageSessionId !== undefined) {
2749
2785
  const holdRefused = this.storeActRefusedText(f.canonical, "holding an instruction-form file", handle);
2750
- const held = holdRefused !== undefined ? { ok: false, reason: holdRefused } : openInstructionHold(this.controlDir, {
2786
+ const held = holdRefused !== undefined ? { ok: false, reason: holdRefused } : await openInstructionHold(this.governanceStore, {
2751
2787
  sessionId: lineageSessionId,
2752
2788
  rel: homeRel(writableRoot, f.canonical),
2753
2789
  absPath: f.canonical,
@@ -2759,6 +2795,7 @@ export class MemoryEngine {
2759
2795
  scope: writeScope,
2760
2796
  settleIds: pendingSettleIds,
2761
2797
  now: this.now,
2798
+ actRefused: (path, act) => this.storeActRefusedText(path, act, handle),
2762
2799
  });
2763
2800
  if (held.ok) {
2764
2801
  report.warnings.push(`${rel}: instruction-form entry HELD — this session's delegation outcome is still pending (attested-only evidence standard); the file was captured off the model-visible plane and commits automatically when every delegation settles clean, disposes to quarantine on a dirty or expired settlement (resolveHold is the host valve)`);
@@ -2918,7 +2955,7 @@ export class MemoryEngine {
2918
2955
  report.inboundFindings = preFindings;
2919
2956
  return report;
2920
2957
  }
2921
- const preCommit = lineageSessionId !== undefined ? this.sessionPollution(lineageSessionId) : undefined;
2958
+ const preCommit = lineageSessionId !== undefined ? await this.sessionPollution(lineageSessionId) : undefined;
2922
2959
  if (preCommit !== undefined && !carry) {
2923
2960
  if (lineageArmed) {
2924
2961
  try {
@@ -3154,7 +3191,7 @@ export class MemoryEngine {
3154
3191
  }
3155
3192
  catch {
3156
3193
  }
3157
- const postCommit = lineageSessionId !== undefined && pollutedReason === undefined ? this.sessionPollution(lineageSessionId) : undefined;
3194
+ const postCommit = lineageSessionId !== undefined && pollutedReason === undefined ? await this.sessionPollution(lineageSessionId) : undefined;
3158
3195
  if (postCommit !== undefined) {
3159
3196
  const appliedNonDelete = patchReport.applied.filter((a) => a.op !== "delete").map((a) => a.id);
3160
3197
  try {
@@ -3233,19 +3270,19 @@ export class MemoryEngine {
3233
3270
  if (drained !== undefined && drained.length > 0)
3234
3271
  report.inboundFindings = drained;
3235
3272
  if (pollutedReason !== undefined) {
3236
- if (restorePollutedIndex())
3273
+ if (await restorePollutedIndex())
3237
3274
  signalContainment((c) => (c.indexRolledBack = true));
3238
3275
  report.warnings.push(carry
3239
3276
  ? `memory entries from this externally exposed session were committed with an external-origin marker; its index prose additions were not retained: ${inlineUntrusted(pollutedReason, 200)}`
3240
3277
  : `memory harvest committed nothing this session: ${inlineUntrusted(pollutedReason, 200)}`);
3241
3278
  }
3242
3279
  else if (carry && sessionPending) {
3243
- if (restorePollutedIndex()) {
3280
+ if (await restorePollutedIndex()) {
3244
3281
  signalContainment((c) => (c.indexRolledBack = true));
3245
3282
  report.warnings.push("this session's index prose additions were not retained: its delegation outcome is still pending (prose has no entry anchor for the settlement account to govern; committed entries and mechanical index lines are unaffected)");
3246
3283
  }
3247
3284
  }
3248
- const indexGate = this.gateDerivedIndex(handle);
3285
+ const indexGate = await this.gateDerivedIndex(handle);
3249
3286
  if (indexGate !== undefined) {
3250
3287
  report.rejections.push(indexGate.rejection);
3251
3288
  if (!indexGate.contained)
@@ -3260,7 +3297,7 @@ export class MemoryEngine {
3260
3297
  const rest = deferredSeats.length - shown.length;
3261
3298
  const seatList = `${shown.join(", ")}${rest > 0 ? ` (+${rest} more — see the rejections)` : ""}`;
3262
3299
  try {
3263
- const closed = handle.accountLeg !== undefined ? await closeSessionAccount(this.governanceStore, this.controlDir, { sessionId: lineageSessionId, now: this.now, leg: handle.accountLeg }) : "absent";
3300
+ const closed = handle.accountLeg !== undefined ? await closeSessionAccount(this.governanceStore, { sessionId: lineageSessionId, now: this.now, leg: handle.accountLeg }) : "absent";
3264
3301
  if (closed === "stale" && !report.warnings.includes(STALE_LEG_NOTE))
3265
3302
  report.warnings.push(STALE_LEG_NOTE);
3266
3303
  if (closed === "written" && deferredSeats.length > 0) {
@@ -3299,12 +3336,12 @@ export class MemoryEngine {
3299
3336
  }
3300
3337
  async settleDueHolds(handle, report) {
3301
3338
  const out = { released: [], disposed: [] };
3302
- const dispose = (row, terminal, why) => {
3303
- const d = disposeHold(this.controlDir, { holdId: row.holdId, terminal });
3339
+ const dispose = async (row, terminal, why) => {
3340
+ const d = await disposeHold(this.governanceStore, { holdId: row.holdId, terminal });
3304
3341
  out.disposed.push({ relPath: this.holdPlaneRel(row), terminal });
3305
3342
  this.warnScoped(report, row.scope, `held instruction entry ${this.holdPlaneRel(row)} DISPOSED (${terminal}): ${why}${d.quarantineName !== undefined ? ` — captured bytes moved to control-plane quarantine (${d.quarantineName})` : ""}`);
3306
3343
  };
3307
- for (const row of readHolds(this.controlDir)) {
3344
+ for (const row of await readHolds(this.governanceStore)) {
3308
3345
  const valveRelease = row.resolved === "release" && (row.status === "held" || (row.status === "disposed" && row.disposition?.terminal === "expired"));
3309
3346
  if (valveRelease) {
3310
3347
  const released = await this.releaseHold(handle, report, row, { valve: true, disposed: out.disposed });
@@ -3315,17 +3352,17 @@ export class MemoryEngine {
3315
3352
  if (row.status !== "held")
3316
3353
  continue;
3317
3354
  if (row.resolved === "discard") {
3318
- dispose(row, "discarded", "the host valve resolved this hold as discard");
3355
+ await dispose(row, "discarded", "the host valve resolved this hold as discard");
3319
3356
  continue;
3320
3357
  }
3321
- const marker = this.sessionPollution(row.sessionId);
3358
+ const marker = await this.sessionPollution(row.sessionId);
3322
3359
  const rows = await sessionSettlements(this.governanceStore, row.sessionId);
3323
3360
  if (marker !== undefined || rows.some((r) => r.effective === "external")) {
3324
- dispose(row, "dirty", marker !== undefined ? "the writer session is marked externally exposed" : "a delegation of the writer session settled with an observed external attestation");
3361
+ await dispose(row, "dirty", marker !== undefined ? "the writer session is marked externally exposed" : "a delegation of the writer session settled with an observed external attestation");
3325
3362
  continue;
3326
3363
  }
3327
3364
  if (rows.some((r) => r.effective === "unattestable" || r.effective === "expired")) {
3328
- dispose(row, "expired", "the writer session's delegation window closed UNPROVABLE (a timeout or an unattestable delivery — not a conviction); resolveHold(holdId, \"release\") commits the entry with a \"static\"-cause marker after host review");
3365
+ await dispose(row, "expired", "the writer session's delegation window closed UNPROVABLE (a timeout or an unattestable delivery — not a conviction); resolveHold(holdId, \"release\") commits the entry with a \"static\"-cause marker after host review");
3329
3366
  continue;
3330
3367
  }
3331
3368
  if (rows.some((r) => r.effective === "pending"))
@@ -3337,11 +3374,11 @@ export class MemoryEngine {
3337
3374
  return out;
3338
3375
  }
3339
3376
  async releaseHold(handle, report, row, opts) {
3340
- const disposeOut = (terminal, why) => {
3341
- const d = disposeHold(this.controlDir, { holdId: row.holdId, terminal });
3377
+ const disposeOut = async (terminal, why) => {
3378
+ const d = await disposeHold(this.governanceStore, { holdId: row.holdId, terminal });
3342
3379
  if (!d.ok) {
3343
3380
  try {
3344
- clearHoldResolution(this.controlDir, { holdId: row.holdId, ifResolvedAt: row.resolvedAt });
3381
+ await clearHoldResolution(this.governanceStore, { holdId: row.holdId, ifResolvedAt: row.resolvedAt });
3345
3382
  }
3346
3383
  catch {
3347
3384
  }
@@ -3352,7 +3389,7 @@ export class MemoryEngine {
3352
3389
  opts.disposed.push({ relPath: this.holdPlaneRel(row), terminal });
3353
3390
  return undefined;
3354
3391
  };
3355
- const content = readHoldCustody(this.controlDir, row);
3392
+ const content = await readHoldCustody(this.governanceStore, row);
3356
3393
  if (content === undefined)
3357
3394
  return disposeOut("capture_lost", `the captured bytes are missing or fail their digest (sha256 ${row.contentDigest}; no custody file or quarantine file hashes to it) — nothing verifiable to commit`);
3358
3395
  const findings = [];
@@ -3364,7 +3401,7 @@ export class MemoryEngine {
3364
3401
  return disposeOut("discarded", `the release re-scan refused it (${findings[0].code}: ${findings[0].reason})`);
3365
3402
  if (Buffer.byteLength(content, "utf8") > this.perFileBytes)
3366
3403
  return disposeOut("discarded", `the captured file is over the ${this.perFileBytes}-byte cap`);
3367
- if (this.sessionPollution(row.sessionId) !== undefined)
3404
+ if ((await this.sessionPollution(row.sessionId)) !== undefined)
3368
3405
  return disposeOut("discarded", "the writer session's pollution marker landed before the release could commit");
3369
3406
  const parsed = parseEntryFile(content);
3370
3407
  if (parsed.unclosed === true)
@@ -3436,7 +3473,7 @@ export class MemoryEngine {
3436
3473
  }
3437
3474
  }
3438
3475
  if (alreadyApplied) {
3439
- markHoldReleased(this.controlDir, { holdId: row.holdId, now: this.now });
3476
+ await markHoldReleased(this.governanceStore, { holdId: row.holdId, now: this.now });
3440
3477
  this.warnScoped(report, row.scope, `held instruction entry ${this.holdPlaneRel(row)} was already committed by an earlier release attempt (crash-idempotent retry) — marked released`);
3441
3478
  return this.holdPlaneRel(row);
3442
3479
  }
@@ -3450,7 +3487,7 @@ export class MemoryEngine {
3450
3487
  catch (err) {
3451
3488
  this.warnScoped(report, row.scope, `released hold ${this.holdPlaneRel(row)}: lineage settlement failed after commit — the entry stays latched until a later harvest reconciles: ${thrownText(err)}`);
3452
3489
  }
3453
- markHoldReleased(this.controlDir, { holdId: row.holdId, now: this.now });
3490
+ await markHoldReleased(this.governanceStore, { holdId: row.holdId, now: this.now });
3454
3491
  const landedSlug = patchReport.applied.find((a) => a.id === patch.id)?.slug ?? row.slug;
3455
3492
  const landedRel = landedSlug === row.slug ? row.rel : `${landedSlug}.md`;
3456
3493
  const abs = join(scopeDirFor(this.memoryDir, this.controlDir, row.scope), landedRel);
@@ -3472,20 +3509,20 @@ export class MemoryEngine {
3472
3509
  async resolveSettlement(settleId, to, requestId) {
3473
3510
  await resolveSettlementRecord(this.governanceStore, { settleId, to, requestId, now: this.now });
3474
3511
  }
3475
- resolveHold(holdId, action, requestId) {
3476
- resolveHoldRecord(this.controlDir, { holdId, action, requestId, now: this.now });
3512
+ async resolveHold(holdId, action, requestId) {
3513
+ await resolveHoldRecord(this.governanceStore, { holdId, action, requestId, now: this.now });
3477
3514
  }
3478
3515
  async resolveSessionAccount(sessionId, requestId) {
3479
- await resolveSessionAccountRecord(this.governanceStore, this.controlDir, { sessionId, requestId, now: this.now });
3516
+ await resolveSessionAccountRecord(this.governanceStore, { sessionId, requestId, now: this.now });
3480
3517
  }
3481
3518
  async readDelegationSettlements() {
3482
3519
  return await effectiveSettlements(this.governanceStore);
3483
3520
  }
3484
- readInstructionHolds() {
3485
- return readHolds(this.controlDir);
3521
+ async readInstructionHolds() {
3522
+ return await readHolds(this.governanceStore);
3486
3523
  }
3487
3524
  async readDanglingSessionAccounts(selfSessionId) {
3488
- return await foreignDanglingSessionAccounts(this.governanceStore, this.controlDir, selfSessionId);
3525
+ return await foreignDanglingSessionAccounts(this.governanceStore, selfSessionId);
3489
3526
  }
3490
3527
  requireConsolidation() {
3491
3528
  if (this.consolidation === undefined) {
@@ -4831,8 +4868,8 @@ export class MemoryEngine {
4831
4868
  }
4832
4869
  return out;
4833
4870
  }
4834
- listOriginClearances() {
4835
- return readOriginClearances(this.controlDir);
4871
+ async listOriginClearances() {
4872
+ return readOriginClearances(this.governanceStore);
4836
4873
  }
4837
4874
  async clearEntryOrigin(entryId, input) {
4838
4875
  if (typeof input.requestId !== "string" || input.requestId.length === 0) {
@@ -4841,7 +4878,7 @@ export class MemoryEngine {
4841
4878
  if (typeof input.reason !== "string" || input.reason.length === 0) {
4842
4879
  MemoryEngine.originClearRefusal("memory.origin_clear_invalid", "clearEntryOrigin requires a non-empty reason (the host's stated ground rides the audit row) — refused, never defaulted.");
4843
4880
  }
4844
- const clearances = readOriginClearances(this.controlDir);
4881
+ const clearances = await readOriginClearances(this.governanceStore);
4845
4882
  const pending = clearances.find((r) => r.entryId === entryId && r.status === "pending");
4846
4883
  if (pending !== undefined)
4847
4884
  return await this.completeOriginClearance(pending, input.requestId);
@@ -4875,13 +4912,16 @@ export class MemoryEngine {
4875
4912
  at: this.now(),
4876
4913
  entryText: serializeEntryFile(cleared),
4877
4914
  };
4878
- openOriginClearance(this.controlDir, row);
4915
+ await openOriginClearance(this.governanceStore, row);
4879
4916
  return await this.completeOriginClearance({ ...row, status: "pending", events: [] }, input.requestId);
4880
4917
  }
4881
4918
  async completeOriginClearance(row, requestId) {
4882
- const fail = (code, detail, keepPending) => {
4919
+ if (row.custodyErased === true) {
4920
+ MemoryEngine.originClearRefusal("memory.origin_clear_unknown", `clearEntryOrigin ${JSON.stringify(row.entryId.slice(0, 80))}: the entry was erased, and the custody of its pending clearance ${JSON.stringify(row.clearanceId)} was erased with it — nothing is resumed (the row keeps who cleared, why and when).`);
4921
+ }
4922
+ const fail = async (code, detail, keepPending) => {
4883
4923
  try {
4884
- settleOriginClearance(this.controlDir, { clearanceId: row.clearanceId, to: "failed", requestId, eventId: uuidv7(), now: this.now, detail, ...(keepPending ? { keepPending: true } : {}) });
4924
+ await settleOriginClearance(this.governanceStore, { clearanceId: row.clearanceId, to: "failed", requestId, eventId: uuidv7(), now: this.now, detail, ...(keepPending ? { keepPending: true } : {}) });
4885
4925
  }
4886
4926
  catch {
4887
4927
  }
@@ -4890,11 +4930,16 @@ export class MemoryEngine {
4890
4930
  const parsed = parseEntryFile(row.entryText);
4891
4931
  const cleared = { id: row.entryId, slug: row.slug, scope: row.scope, frontmatter: parsed.frontmatter, body: parsed.body, rev: "" };
4892
4932
  cleared.rev = computeEntryRev(cleared);
4933
+ const refuseIfErasedSince = async () => {
4934
+ if (await clearanceCustodyErased(this.governanceStore, row.clearanceId)) {
4935
+ MemoryEngine.originClearRefusal("memory.origin_clear_unknown", `clearEntryOrigin ${JSON.stringify(row.entryId.slice(0, 80))}: the entry was erased while its clearance ${JSON.stringify(row.clearanceId)} ran, and the custody went with it — the entry is not re-recorded.`);
4936
+ }
4937
+ };
4893
4938
  const [current] = await this.committedAuditFace().getByIds([row.entryId]);
4894
4939
  if (current === undefined || committedOriginOf(current.frontmatter) !== undefined) {
4895
4940
  if (parsed.id !== row.entryId || committedOriginOf(parsed.frontmatter) !== undefined) {
4896
4941
  const what = parsed.id !== row.entryId ? "the custody bytes carry a different entry id" : "an origin form survived in the custody bytes";
4897
- fail("memory.origin_clear_failed", `the clearance row's custody text failed its identity/cleared-state validation (${what}) — refusing to act on it. ` +
4942
+ return await fail("memory.origin_clear_failed", `the clearance row's custody text failed its identity/cleared-state validation (${what}) — refusing to act on it. ` +
4898
4943
  (current === undefined
4899
4944
  ? "The row stays PENDING (the tombstone has committed and the row's entryText is the recovery seat — repair it, or recover the bytes by hand)."
4900
4945
  : "Re-judge with a fresh clearEntryOrigin call (this row is terminal; the marked entry stands untouched)."), current === undefined);
@@ -4904,13 +4949,13 @@ export class MemoryEngine {
4904
4949
  let detail;
4905
4950
  if (current !== undefined && committedOriginOf(current.frontmatter) !== undefined) {
4906
4951
  if ((await this.readChallengeExclusions()).has(row.entryId)) {
4907
- fail("memory.origin_clear_challenged", "the entry is challenged/latched — adjudicate the challenge first (the clear valve is not a challenge exit); the marked entry stands untouched and this row is terminal.", false);
4952
+ return await fail("memory.origin_clear_challenged", "the entry is challenged/latched — adjudicate the challenge first (the clear valve is not a challenge exit); the marked entry stands untouched and this row is terminal.", false);
4908
4953
  }
4909
4954
  if (current.rev !== row.baseRev) {
4910
- fail("memory.origin_clear_conflict", `the entry moved since the clearance was judged (rev ${row.baseRev.slice(0, 12)}… → ${current.rev.slice(0, 12)}…) — call clearEntryOrigin again to re-judge (the marked entry stands untouched; this row is terminal).`, false);
4955
+ return await fail("memory.origin_clear_conflict", `the entry moved since the clearance was judged (rev ${row.baseRev.slice(0, 12)}… → ${current.rev.slice(0, 12)}…) — call clearEntryOrigin again to re-judge (the marked entry stands untouched; this row is terminal).`, false);
4911
4956
  }
4912
4957
  if (current.scope !== row.scope || current.slug !== row.slug) {
4913
- fail("memory.origin_clear_conflict", `the entry's projection moved since the clearance was judged (${JSON.stringify(row.scope)}/${JSON.stringify(row.slug.slice(0, 80))} → ${JSON.stringify(current.scope)}/${JSON.stringify(current.slug.slice(0, 80))}) — call clearEntryOrigin again to re-judge (the marked entry stands untouched; this row is terminal).`, false);
4958
+ return await fail("memory.origin_clear_conflict", `the entry's projection moved since the clearance was judged (${JSON.stringify(row.scope)}/${JSON.stringify(row.slug.slice(0, 80))} → ${JSON.stringify(current.scope)}/${JSON.stringify(current.slug.slice(0, 80))}) — call clearEntryOrigin again to re-judge (the marked entry stands untouched; this row is terminal).`, false);
4914
4959
  }
4915
4960
  const projText = readNoFollowSafe(join(scopeDirFor(this.memoryDir, this.controlDir, current.scope), `${current.slug}.md`));
4916
4961
  let projIsPristineStub = false;
@@ -4919,23 +4964,24 @@ export class MemoryEngine {
4919
4964
  projIsPristineStub = projText === serializeEntryFile({ id: current.id, frontmatter: currentStubFm, body: STUB_ARCHIVED_LINE });
4920
4965
  }
4921
4966
  if (projText !== undefined && revOfText(projText, current.id) !== current.rev && !projIsPristineStub) {
4922
- fail("memory.origin_clear_conflict", "the entry's on-disk projection diverges from its committed state (an unadopted out-of-session edit stands on the plane) — the clear will not overwrite it; adopt or reconcile the divergence first, then call clearEntryOrigin again (the marked entry and the on-disk bytes both stand untouched; this row is terminal).", false);
4967
+ return await fail("memory.origin_clear_conflict", "the entry's on-disk projection diverges from its committed state (an unadopted out-of-session edit stands on the plane) — the clear will not overwrite it; adopt or reconcile the divergence first, then call clearEntryOrigin again (the marked entry and the on-disk bytes both stand untouched; this row is terminal).", false);
4923
4968
  }
4924
4969
  const del = await this.backend.applyPatches([{ op: "delete", id: row.entryId, baseRev: row.baseRev }]);
4925
4970
  if (del.conflicts.length > 0) {
4926
4971
  const [reprobe] = await this.committedAuditFace().getByIds([row.entryId]);
4927
4972
  if (reprobe !== undefined && committedOriginOf(reprobe.frontmatter) !== undefined) {
4928
- fail("memory.origin_clear_conflict", `the tombstone leg was refused (${(del.conflicts[0]?.reason ?? "conflict").slice(0, 160)}) — call clearEntryOrigin again to re-judge (the marked entry stands untouched; this row is terminal).`, false);
4973
+ return await fail("memory.origin_clear_conflict", `the tombstone leg was refused (${(del.conflicts[0]?.reason ?? "conflict").slice(0, 160)}) — call clearEntryOrigin again to re-judge (the marked entry stands untouched; this row is terminal).`, false);
4929
4974
  }
4930
4975
  if (reprobe === undefined) {
4931
4976
  try {
4932
- markOriginClearanceTombstoned(this.controlDir, { clearanceId: row.clearanceId, now: this.now });
4977
+ await markOriginClearanceTombstoned(this.governanceStore, { clearanceId: row.clearanceId, now: this.now });
4933
4978
  }
4934
4979
  catch {
4935
4980
  }
4981
+ await refuseIfErasedSince();
4936
4982
  const add = await this.backend.applyPatches([{ op: "add", id: row.entryId, entry: cleared, guard: "absent" }]);
4937
4983
  if (add.conflicts.length > 0) {
4938
- fail("memory.origin_clear_failed", `the re-record leg was refused (${(add.conflicts[0]?.reason ?? "conflict").slice(0, 160)}) — the row stays PENDING with the cleared bytes in its custody (entryText); call clearEntryOrigin again to resume.`, true);
4984
+ return await fail("memory.origin_clear_failed", `the re-record leg was refused (${(add.conflicts[0]?.reason ?? "conflict").slice(0, 160)}) — the row stays PENDING with the cleared bytes in its custody (entryText); call clearEntryOrigin again to resume.`, true);
4939
4985
  }
4940
4986
  landedSlug = add.applied.find((a) => a.id === row.entryId)?.slug ?? row.slug;
4941
4987
  detail = `cleared and re-recorded at slug ${JSON.stringify(landedSlug)} (tombstone raced by a concurrent resumer)`;
@@ -4947,13 +4993,14 @@ export class MemoryEngine {
4947
4993
  }
4948
4994
  else {
4949
4995
  try {
4950
- markOriginClearanceTombstoned(this.controlDir, { clearanceId: row.clearanceId, now: this.now });
4996
+ await markOriginClearanceTombstoned(this.governanceStore, { clearanceId: row.clearanceId, now: this.now });
4951
4997
  }
4952
4998
  catch {
4953
4999
  }
5000
+ await refuseIfErasedSince();
4954
5001
  const add = await this.backend.applyPatches([{ op: "add", id: row.entryId, entry: cleared, guard: "absent" }]);
4955
5002
  if (add.conflicts.length > 0) {
4956
- fail("memory.origin_clear_failed", `the re-record leg was refused (${(add.conflicts[0]?.reason ?? "conflict").slice(0, 160)}) — the row stays PENDING with the cleared bytes in its custody (entryText); call clearEntryOrigin again to resume.`, true);
5003
+ return await fail("memory.origin_clear_failed", `the re-record leg was refused (${(add.conflicts[0]?.reason ?? "conflict").slice(0, 160)}) — the row stays PENDING with the cleared bytes in its custody (entryText); call clearEntryOrigin again to resume.`, true);
4957
5004
  }
4958
5005
  landedSlug = add.applied.find((a) => a.id === row.entryId)?.slug ?? row.slug;
4959
5006
  detail = `cleared and re-recorded at slug ${JSON.stringify(landedSlug)}`;
@@ -4961,11 +5008,12 @@ export class MemoryEngine {
4961
5008
  }
4962
5009
  else if (current === undefined) {
4963
5010
  if (row.tombstonedAt === undefined) {
4964
- fail("memory.origin_clear_unknown", "the entry is no longer committed and this clearance never recorded a committed tombstone — the deletion is not this clearance's and stands (nothing was resurrected). The cleared bytes remain in this row's custody (entryText) for host review; this row is terminal.", false);
5011
+ return await fail("memory.origin_clear_unknown", "the entry is no longer committed and this clearance never recorded a committed tombstone — the deletion is not this clearance's and stands (nothing was resurrected). The cleared bytes remain in this row's custody (entryText) for host review; this row is terminal.", false);
4965
5012
  }
5013
+ await refuseIfErasedSince();
4966
5014
  const add = await this.backend.applyPatches([{ op: "add", id: row.entryId, entry: cleared, guard: "absent" }]);
4967
5015
  if (add.conflicts.length > 0) {
4968
- fail("memory.origin_clear_failed", `the resumed re-record leg was refused (${(add.conflicts[0]?.reason ?? "conflict").slice(0, 160)}) — the row stays PENDING with the cleared bytes in its custody (entryText); call clearEntryOrigin again to resume.`, true);
5016
+ return await fail("memory.origin_clear_failed", `the resumed re-record leg was refused (${(add.conflicts[0]?.reason ?? "conflict").slice(0, 160)}) — the row stays PENDING with the cleared bytes in its custody (entryText); call clearEntryOrigin again to resume.`, true);
4969
5017
  }
4970
5018
  landedSlug = add.applied.find((a) => a.id === row.entryId)?.slug ?? row.slug;
4971
5019
  detail = `resumed after interruption: re-recorded at slug ${JSON.stringify(landedSlug)}`;
@@ -4974,7 +5022,7 @@ export class MemoryEngine {
4974
5022
  landedSlug = current.slug;
4975
5023
  detail = current.rev === cleared.rev ? "already complete (terminal event replay)" : "marker already absent (a later committed generation stands; custody retained on this row)";
4976
5024
  }
4977
- settleOriginClearance(this.controlDir, { clearanceId: row.clearanceId, to: "done", requestId, eventId: uuidv7(), now: this.now, detail });
5025
+ await settleOriginClearance(this.governanceStore, { clearanceId: row.clearanceId, to: "done", requestId, eventId: uuidv7(), now: this.now, detail });
4978
5026
  await this.announce(row.scope, { kind: "gate", at: this.now(), items: [`memory entry ${inlineUntrusted(row.entryId, 80)} external-origin marker CLEARED by host request ${JSON.stringify(inlineUntrusted(requestId, 80))} — re-recorded unmarked (${inlineUntrusted(detail, 160)})`] }, "origin-clearance enqueue");
4979
5027
  return { entryId: row.entryId, clearanceId: row.clearanceId, origin: row.origin, landedSlug };
4980
5028
  }
@@ -5261,10 +5309,7 @@ export class MemoryEngine {
5261
5309
  fault = true;
5262
5310
  }
5263
5311
  }
5264
- const viaBackend = this.backend.readCommittedShadow?.(id);
5265
- if (viaBackend !== undefined)
5266
- return { content: viaBackend, fault: false };
5267
- const viaShadow = readSafe(join(this.controlDir, "shadow", `${id}.md`));
5312
+ const viaShadow = this.backend.readCommittedShadow?.(id);
5268
5313
  if (viaShadow !== undefined)
5269
5314
  return { content: viaShadow, fault: false };
5270
5315
  return { fault };
@@ -5280,11 +5325,10 @@ export class MemoryEngine {
5280
5325
  catch {
5281
5326
  }
5282
5327
  }
5283
- const viaBackend = this.backend.readCommittedShadow?.(id);
5284
- const text = viaBackend ?? readSafe(join(this.controlDir, "shadow", `${id}.md`));
5328
+ const text = this.backend.readCommittedShadow?.(id);
5285
5329
  return text !== undefined ? parseEntryFile(text).frontmatter : undefined;
5286
5330
  }
5287
- gateDerivedIndex(handle) {
5331
+ async gateDerivedIndex(handle) {
5288
5332
  if (handle.writableRoot === null)
5289
5333
  return undefined;
5290
5334
  const indexPath = join(handle.writableRoot, MEMORY_INDEX_FILENAME);
@@ -5294,30 +5338,15 @@ export class MemoryEngine {
5294
5338
  const finding = scanMemoryWrite(text).find((f) => f.code === "secret");
5295
5339
  if (finding === undefined)
5296
5340
  return undefined;
5297
- let detail = "";
5298
- let captured = false;
5299
- try {
5300
- const dest = join(this.controlDir, QUARANTINE_DIR, `${this.now()}-${MEMORY_INDEX_FILENAME}`);
5301
- ensureDirExists(dirname(dest));
5302
- let landed = false;
5303
- for (let attempt = 0; !landed && attempt < 10; attempt++) {
5304
- const candidate = attempt === 0 ? dest : `${dest}.${attempt}`;
5305
- try {
5306
- writeFileSync(candidate, text, { encoding: "utf8", flag: "wx" });
5307
- landed = true;
5308
- }
5309
- catch (err) {
5310
- if (!(err instanceof Error && "code" in err && err.code === "EEXIST"))
5311
- throw err;
5312
- }
5313
- }
5314
- if (!landed)
5315
- writeFileSync(`${dest}.${process.pid}.${indexCaptureSeq++}`, text, { encoding: "utf8", flag: "wx" });
5316
- captured = true;
5317
- }
5318
- catch (err) {
5319
- detail = `; quarantine capture failed: ${thrownText(err)}`;
5320
- }
5341
+ const kept = await keepCopiesBeforeOverwrite(text, async (bytes) => {
5342
+ const capture = await captureIntoQuarantine(this.governanceStore, indexPath, bytes, this.now);
5343
+ return "name" in capture ? true : capture.failed;
5344
+ }, () => {
5345
+ this.refuseOutsideStore(indexPath, "reading the index again after its copy", handle);
5346
+ return readNoFollowSafe(indexPath);
5347
+ });
5348
+ const captured = kept.landed;
5349
+ let detail = kept.failed.map((f) => `; ${f}`).join("");
5321
5350
  let cleared = false;
5322
5351
  try {
5323
5352
  this.refuseOutsideStore(indexPath, "clearing the index", handle);
@@ -5328,7 +5357,7 @@ export class MemoryEngine {
5328
5357
  detail = `${detail}; the derived index could NOT be cleared: ${this.writeFailureText(err)}`;
5329
5358
  }
5330
5359
  const outcome = [
5331
- captured ? "the offending prose was captured to quarantine" : "the quarantine capture FAILED — the prose was not preserved",
5360
+ captured ? "the offending prose was captured to quarantine" : "the quarantine capture FAILED — prose it did not capture was not preserved",
5332
5361
  cleared ? "the index file was cleared" : "the index file could NOT be cleared, so it still carries the offending content — it is excluded from the injected index and from the rebuild",
5333
5362
  ].join("; ");
5334
5363
  return {
@@ -5353,6 +5382,13 @@ export class MemoryEngine {
5353
5382
  return this.writeFailureText(err);
5354
5383
  }
5355
5384
  }
5385
+ async containSuspect(path, content, act, handle) {
5386
+ const capture = await captureIntoQuarantine(this.governanceStore, path, content, this.now);
5387
+ const refused = this.storeActRefusedText(path, act, handle);
5388
+ if (refused !== undefined)
5389
+ return { ...("name" in capture ? { dest: capture.name } : {}), removed: false, leftOnPlane: content, detail: "failed" in capture ? `${capture.failed}; ${refused}` : refused };
5390
+ return clearContainedPath(path, content, capture);
5391
+ }
5356
5392
  assertRootAnchored(handle) {
5357
5393
  const root = anchorStoreRoot(this.memoryDir, this.controlDir, { onIncident: this.onIncident, now: this.now });
5358
5394
  if (handle !== undefined && handle.memoryDir !== root)