@sema-agent/core 7.58.0 → 7.59.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 (284) hide show
  1. package/CHANGELOG.md +776 -0
  2. package/README.md +6 -4
  3. package/dist/agents/cc-peer-directory.js +8 -8
  4. package/dist/agents/child-model-seat.d.ts +4 -2
  5. package/dist/agents/child-model-seat.js +5 -3
  6. package/dist/agents/peer-directory.d.ts +9 -0
  7. package/dist/agents/peer-directory.js +6 -2
  8. package/dist/agents/send-message-tool.js +21 -12
  9. package/dist/agents/subagent.js +33 -34
  10. package/dist/brain/anthropic.js +161 -45
  11. package/dist/brain/errors.d.ts +9 -3
  12. package/dist/brain/errors.js +7 -1
  13. package/dist/brain/failover.js +2 -1
  14. package/dist/brain/open-responses.js +3 -3
  15. package/dist/brain/openai.js +103 -53
  16. package/dist/brain/provider-dialect.d.ts +11 -0
  17. package/dist/brain/provider-dialect.js +5 -2
  18. package/dist/brain/reasoning-request.js +5 -4
  19. package/dist/brain/reasoning.d.ts +19 -0
  20. package/dist/brain/reasoning.js +17 -2
  21. package/dist/brain/request-route.d.ts +4 -1
  22. package/dist/brain/request-route.js +4 -1
  23. package/dist/brain/route-adjudicator.d.ts +84 -18
  24. package/dist/brain/route-adjudicator.js +87 -21
  25. package/dist/brain/route-conformance.js +1 -1
  26. package/dist/brain/stream-engine.js +84 -49
  27. package/dist/brain/stream-shared.d.ts +30 -0
  28. package/dist/brain/stream-shared.js +34 -1
  29. package/dist/brain/timeout.d.ts +62 -5
  30. package/dist/brain/timeout.js +93 -6
  31. package/dist/core/account-row.d.ts +61 -0
  32. package/dist/core/account-row.js +51 -0
  33. package/dist/core/advisor-verdict.d.ts +2 -1
  34. package/dist/core/advisor-verdict.js +1 -1
  35. package/dist/core/allow-entry.d.ts +18 -6
  36. package/dist/core/allow-entry.js +16 -8
  37. package/dist/core/approval-object.d.ts +29 -15
  38. package/dist/core/approval-object.js +47 -19
  39. package/dist/core/auto-compaction.js +1 -1
  40. package/dist/core/background-agent-store.d.ts +16 -3
  41. package/dist/core/checkpoint-requirements.d.ts +13 -1
  42. package/dist/core/checkpoint-requirements.js +3 -1
  43. package/dist/core/checkpoint-store.d.ts +58 -12
  44. package/dist/core/checkpoint-store.js +18 -6
  45. package/dist/core/child-clamp-seats.d.ts +29 -0
  46. package/dist/core/child-clamp-seats.js +56 -1
  47. package/dist/core/compliance.d.ts +2 -2
  48. package/dist/core/compliance.js +1 -1
  49. package/dist/core/effective-path-target.d.ts +8 -2
  50. package/dist/core/effective-path-target.js +8 -3
  51. package/dist/core/engine-notice.d.ts +98 -8
  52. package/dist/core/engine-notice.js +19 -5
  53. package/dist/core/gate-fold.js +1 -1
  54. package/dist/core/gate-lanes.js +8 -4
  55. package/dist/core/gate-outcome.d.ts +32 -3
  56. package/dist/core/gate-outcome.js +19 -1
  57. package/dist/core/governance-codes.d.ts +9 -1
  58. package/dist/core/governance-codes.js +13 -0
  59. package/dist/core/hooks.d.ts +31 -3
  60. package/dist/core/hooks.js +5 -6
  61. package/dist/core/image-admission.d.ts +6 -5
  62. package/dist/core/image-admission.js +18 -14
  63. package/dist/core/locked-config.d.ts +5 -2
  64. package/dist/core/locked-config.js +2 -1
  65. package/dist/core/mailbox-store.d.ts +45 -1
  66. package/dist/core/mailbox-store.js +53 -9
  67. package/dist/core/mcp-failure.d.ts +114 -18
  68. package/dist/core/mcp-failure.js +63 -5
  69. package/dist/core/mcp-server-link.d.ts +22 -1
  70. package/dist/core/mcp-server-link.js +25 -1
  71. package/dist/core/mcp.d.ts +12 -8
  72. package/dist/core/mcp.js +42 -25
  73. package/dist/core/memory-engine/consolidation-driver.d.ts +2 -4
  74. package/dist/core/memory-engine/consolidation-driver.js +3 -3
  75. package/dist/core/memory-engine/consolidation.d.ts +4 -0
  76. package/dist/core/memory-engine/consolidation.js +1 -0
  77. package/dist/core/memory-engine/delegation-settlement.js +5 -12
  78. package/dist/core/memory-engine/distiller.js +2 -2
  79. package/dist/core/memory-engine/dual-root.d.ts +50 -1
  80. package/dist/core/memory-engine/dual-root.js +44 -1
  81. package/dist/core/memory-engine/engine.d.ts +67 -14
  82. package/dist/core/memory-engine/engine.js +384 -212
  83. package/dist/core/memory-engine/export-bundle.d.ts +23 -0
  84. package/dist/core/memory-engine/export-bundle.js +23 -4
  85. package/dist/core/memory-engine/file-backend.d.ts +128 -55
  86. package/dist/core/memory-engine/file-backend.js +290 -117
  87. package/dist/core/memory-engine/index.d.ts +1 -1
  88. package/dist/core/memory-engine/layout.d.ts +59 -11
  89. package/dist/core/memory-engine/layout.js +190 -40
  90. package/dist/core/memory-engine/memory-backend-contract.d.ts +1 -1
  91. package/dist/core/memory-engine/memory-backend-contract.js +39 -2
  92. package/dist/core/memory-engine/sync-client.d.ts +9 -1
  93. package/dist/core/memory-engine/sync-client.js +31 -8
  94. package/dist/core/memory-engine/sync.d.ts +5 -0
  95. package/dist/core/memory-engine/types.d.ts +6 -0
  96. package/dist/core/mods/classic-events.d.ts +5 -1
  97. package/dist/core/mods/kernel/dispatch.js +2 -1
  98. package/dist/core/mods/kernel/events.d.ts +2 -1
  99. package/dist/core/mods/kernel/events.js +2 -2
  100. package/dist/core/mods/kernel/registration.d.ts +4 -0
  101. package/dist/core/mods/kernel/registration.js +3 -0
  102. package/dist/core/mods/stations.d.ts +31 -10
  103. package/dist/core/mods/stations.js +44 -10
  104. package/dist/core/mods/tool-call.js +1 -1
  105. package/dist/core/parent-spec-seat.d.ts +8 -3
  106. package/dist/core/parent-spec-seat.js +2 -0
  107. package/dist/core/park-selfcheck.js +6 -0
  108. package/dist/core/peer-seat.d.ts +70 -8
  109. package/dist/core/peer-seat.js +143 -11
  110. package/dist/core/permission-rule-consent.js +10 -4
  111. package/dist/core/permission-rule-model.d.ts +16 -0
  112. package/dist/core/permission-rule-model.js +1 -1
  113. package/dist/core/present-plan-tool.d.ts +11 -3
  114. package/dist/core/present-plan-tool.js +2 -2
  115. package/dist/core/resume-startup-cause.js +1 -0
  116. package/dist/core/rewrite-rejudge.d.ts +1 -0
  117. package/dist/core/rewrite-rejudge.js +2 -2
  118. package/dist/core/roles.d.ts +34 -2
  119. package/dist/core/roles.js +43 -4
  120. package/dist/core/runner/announce-once-ledger.d.ts +5 -4
  121. package/dist/core/runner/announce-once-ledger.js +4 -2
  122. package/dist/core/runner/call-hook-stations.js +2 -2
  123. package/dist/core/runner/contracts.d.ts +20 -16
  124. package/dist/core/runner/credential-origin-seat.js +4 -3
  125. package/dist/core/runner/cwd-alignment-probe.d.ts +20 -0
  126. package/dist/core/runner/cwd-alignment-probe.js +21 -0
  127. package/dist/core/runner/decide-continuation.d.ts +2 -2
  128. package/dist/core/runner/gate-exit.d.ts +12 -4
  129. package/dist/core/runner/gate-exit.js +10 -7
  130. package/dist/core/runner/image-removed-seat.d.ts +12 -0
  131. package/dist/core/runner/image-removed-seat.js +19 -0
  132. package/dist/core/runner/inline-image.js +1 -1
  133. package/dist/core/runner/memory-consolidation-driver.js +3 -3
  134. package/dist/core/runner/park-commit.d.ts +1 -1
  135. package/dist/core/runner/park-commit.js +3 -0
  136. package/dist/core/runner/prepare-ask-lane.d.ts +1 -1
  137. package/dist/core/runner/prepare-ask-lane.js +2 -1
  138. package/dist/core/runner/prepare-boundary-parks.js +2 -2
  139. package/dist/core/runner/prepare-caps-and-workflow.d.ts +3 -5
  140. package/dist/core/runner/prepare-caps-and-workflow.js +2 -0
  141. package/dist/core/runner/prepare-config-doors.js +2 -0
  142. package/dist/core/runner/prepare-context-lane.js +2 -0
  143. package/dist/core/runner/prepare-delegation-surface.d.ts +4 -0
  144. package/dist/core/runner/prepare-delegation-surface.js +1 -0
  145. package/dist/core/runner/prepare-hands-readface.d.ts +4 -0
  146. package/dist/core/runner/prepare-hands-readface.js +5 -0
  147. package/dist/core/runner/prepare-inherited-gate.d.ts +1 -3
  148. package/dist/core/runner/prepare-inherited-gate.js +14 -27
  149. package/dist/core/runner/prepare-memory.js +10 -17
  150. package/dist/core/runner/prepare-park-ask.d.ts +1 -1
  151. package/dist/core/runner/prepare-park-ask.js +2 -1
  152. package/dist/core/runner/prepare-policy-chain.d.ts +3 -3
  153. package/dist/core/runner/prepare-policy-chain.js +16 -13
  154. package/dist/core/runner/prepare-project-context.d.ts +2 -2
  155. package/dist/core/runner/prepare-project-context.js +2 -2
  156. package/dist/core/runner/prepare-prompt-inputs.d.ts +1 -1
  157. package/dist/core/runner/prepare-prompt-inputs.js +8 -19
  158. package/dist/core/runner/prepare-protocol-tools.js +9 -1
  159. package/dist/core/runner/prepare-run-refs.d.ts +2 -3
  160. package/dist/core/runner/prepare-run-refs.js +4 -1
  161. package/dist/core/runner/prepare-task.js +4 -2
  162. package/dist/core/runner/prepare-wiring-manifest.d.ts +1 -1
  163. package/dist/core/runner/prepare-wiring-manifest.js +2 -1
  164. package/dist/core/runner/reply-pricing.d.ts +30 -0
  165. package/dist/core/runner/reply-pricing.js +19 -0
  166. package/dist/core/runner/resume-admission.js +4 -2
  167. package/dist/core/runner/resume-apply.js +6 -1
  168. package/dist/core/runner/resume-policy-outcome.js +5 -5
  169. package/dist/core/runner/role-seat-defaulted.d.ts +44 -0
  170. package/dist/core/runner/role-seat-defaulted.js +40 -0
  171. package/dist/core/runner/run-brain-sinks.js +2 -0
  172. package/dist/core/runner/run-identity-wiring.d.ts +2 -5
  173. package/dist/core/runner/run-identity-wiring.js +5 -4
  174. package/dist/core/runner/run-leg.d.ts +2 -0
  175. package/dist/core/runner/run-leg.js +37 -28
  176. package/dist/core/runner/run-stop-and-final-verify.js +2 -2
  177. package/dist/core/runner/run-terminal-adoption.d.ts +2 -5
  178. package/dist/core/runner/run-terminal-adoption.js +9 -3
  179. package/dist/core/runner/run-turn-boundary.js +77 -70
  180. package/dist/core/runner/runtask.d.ts +1 -1
  181. package/dist/core/runner/runtask.js +2 -2
  182. package/dist/core/runner/side-call-account.d.ts +1 -1
  183. package/dist/core/runner/side-call-account.js +3 -18
  184. package/dist/core/runner/sub-agent-deny-lists.d.ts +24 -0
  185. package/dist/core/runner/sub-agent-deny-lists.js +10 -0
  186. package/dist/core/runner/tool-end-body.js +2 -2
  187. package/dist/core/runner/tool-occurrence.d.ts +1 -1
  188. package/dist/core/runner/tool-occurrence.js +2 -2
  189. package/dist/core/runner/turn-attachments.d.ts +26 -22
  190. package/dist/core/runner/turn-attachments.js +24 -26
  191. package/dist/core/runner-deps.d.ts +1 -1
  192. package/dist/core/secret-env.d.ts +32 -2
  193. package/dist/core/secret-env.js +65 -3
  194. package/dist/core/sensitive-path-policy.js +3 -3
  195. package/dist/core/session-policy-store.d.ts +3 -1
  196. package/dist/core/session-policy-store.js +13 -9
  197. package/dist/core/session-store.d.ts +2 -5
  198. package/dist/core/side-query.d.ts +11 -1
  199. package/dist/core/side-query.js +9 -0
  200. package/dist/core/skill-grant-binding.d.ts +31 -2
  201. package/dist/core/skill-grant-binding.js +31 -1
  202. package/dist/core/skill-grant.d.ts +8 -5
  203. package/dist/core/skill-grant.js +6 -6
  204. package/dist/core/store-contracts/background-agent-store-contract.js +27 -0
  205. package/dist/core/store-contracts/checkpoint-store-contract.js +3 -2
  206. package/dist/core/store-contracts/mailbox-store-contract.js +31 -1
  207. package/dist/core/synthetic-read-verdict.js +2 -1
  208. package/dist/core/task-event.d.ts +32 -8
  209. package/dist/core/task-notification.js +16 -10
  210. package/dist/core/task-registry-agent.js +1 -0
  211. package/dist/core/task-registry-shared.d.ts +5 -0
  212. package/dist/core/task-spec.d.ts +12 -1
  213. package/dist/core/terminal-cause.d.ts +10 -0
  214. package/dist/core/tool-check-reading.d.ts +1 -0
  215. package/dist/core/tool-check-reading.js +3 -3
  216. package/dist/core/tool-policy.d.ts +16 -0
  217. package/dist/core/tool-roster.d.ts +112 -5
  218. package/dist/core/tool-roster.js +42 -1
  219. package/dist/core/tool-spec.d.ts +3 -1
  220. package/dist/core/types.d.ts +2 -2
  221. package/dist/core/types.js +1 -1
  222. package/dist/core/user-attachment.js +1 -1
  223. package/dist/core/wiring-manifest.d.ts +6 -3
  224. package/dist/engine/compaction/compaction.js +29 -3
  225. package/dist/engine/execution-env/node-execution-env.d.ts +4 -3
  226. package/dist/engine/execution-env/node-execution-env.js +9 -12
  227. package/dist/engine/harness/agent-harness.d.ts +25 -2
  228. package/dist/engine/harness/agent-harness.js +41 -12
  229. package/dist/engine/harness/messages.d.ts +3 -1
  230. package/dist/engine/harness/messages.js +3 -2
  231. package/dist/engine/harness/types.d.ts +8 -0
  232. package/dist/engine/llm/rejected-images.d.ts +46 -0
  233. package/dist/engine/llm/rejected-images.js +54 -0
  234. package/dist/engine/llm/tool-turns.d.ts +2 -1
  235. package/dist/engine/llm/types.d.ts +18 -7
  236. package/dist/engine/loop/agent-loop.d.ts +8 -0
  237. package/dist/engine/loop/agent-loop.js +52 -15
  238. package/dist/engine/loop/types.d.ts +3 -1
  239. package/dist/engine/session/path-to-root.d.ts +38 -0
  240. package/dist/engine/session/path-to-root.js +39 -0
  241. package/dist/engine/session/storage-base.js +4 -21
  242. package/dist/index.d.ts +16 -10
  243. package/dist/index.js +11 -7
  244. package/dist/internal/harness-types.d.ts +1 -1
  245. package/dist/internal/harness.d.ts +1 -0
  246. package/dist/internal/llm.d.ts +1 -0
  247. package/dist/orchestration/workflow-observe.d.ts +17 -1
  248. package/dist/orchestration/workflow-observe.js +20 -0
  249. package/dist/orchestration/workflow-types.d.ts +32 -7
  250. package/dist/orchestration/workflow.js +739 -489
  251. package/dist/shell/counting-pass.d.ts +50 -0
  252. package/dist/shell/counting-pass.js +502 -0
  253. package/dist/shell/lex.d.ts +4 -2
  254. package/dist/shell/lex.js +93 -34
  255. package/dist/shell/shell-words.d.ts +1 -1
  256. package/dist/shell/shell-words.js +7 -6
  257. package/dist/stores/file/mailbox-store.js +6 -10
  258. package/dist/tools/fs/fs-bash.d.ts +6 -0
  259. package/dist/tools/fs/fs-bash.js +6 -3
  260. package/dist/tools/fs/index.js +6 -2
  261. package/dist/tools/fs/program-read-model.d.ts +13 -0
  262. package/dist/tools/fs/program-read-model.js +15 -0
  263. package/dist/tools/fs/program-read-model.rows.js +1 -1
  264. package/dist/tools/fs/read-deny.d.ts +13 -10
  265. package/dist/tools/fs/read-deny.js +25 -5
  266. package/dist/tools/fs/read-floor.d.ts +19 -0
  267. package/dist/tools/fs/read-floor.js +23 -0
  268. package/dist/tools/fs/shell-read-check.d.ts +1 -1
  269. package/dist/tools/fs/shell-read-check.js +2 -1
  270. package/dist/tools/fs/shell-read-judge.d.ts +16 -8
  271. package/dist/tools/fs/shell-read-judge.js +10 -6
  272. package/dist/tools/fs/shell-read-plan.d.ts +6 -1
  273. package/dist/tools/fs/shell-read-plan.js +22 -15
  274. package/dist/tools/fs/shell-read-start.js +1 -0
  275. package/dist/tools/fs/shell-reentry-text.d.ts +15 -1
  276. package/dist/tools/fs/shell-reentry-text.js +25 -3
  277. package/dist/tools/monitor.d.ts +4 -0
  278. package/dist/tools/monitor.js +2 -0
  279. package/package.json +3 -2
  280. package/test/export-surface.snapshot.json +87 -1
  281. package/wire-changes/core-stores-sql@7.59.0.json +29 -0
  282. package/wire-changes/core@7.59.0.json +2459 -0
  283. package/wire-changes/mods-kernel@7.59.0.json +29 -0
  284. package/wire-changes/primitives@7.59.0.json +29 -0
@@ -1,6 +1,6 @@
1
1
  import { createHash } from "node:crypto";
2
- import { accessSync, closeSync, constants as fsConstants, existsSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, renameSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
3
- import { dirname, join, relative, sep } from "node:path";
2
+ import { accessSync, closeSync, constants as fsConstants, existsSync, fsyncSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, renameSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
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";
@@ -17,8 +17,8 @@ import { noReplaceRestore, retireStagingToCustody, stagingPathBeside } from "./d
17
17
  import { sessionWriteHomeOf } from "./session-write-home.js";
18
18
  import { landedUndecided, layerDigest, layerObservationLookup, readOnlyLayerCandidates, readOnlyLayerCensus, recordLayerObservations, settleUndecidedLayerObservations, observationBacked, sameObservation, undecidedLayerObservations } from "./layer-observations.js";
19
19
  import { markOriginClearanceTombstoned, openOriginClearance, readOriginClearances, settleOriginClearance } from "./origin-clearance.js";
20
- import { DEFAULT_MAX_ENTRY_DEPTH, MEMORY_INDEX_FILENAME, captureErasureInput, erasureRequestInvalid, erasureSelectHash, scanEntryFiles, } from "./file-backend.js";
21
- import { assembleMemoryExportBundle, computeMemoryBundleHash, memoryBundleInvalid, } from "./export-bundle.js";
20
+ import { DEFAULT_MAX_ENTRY_DEPTH, MEMORY_INDEX_FILENAME, captureErasureInput, erasureRequestInvalid, erasureScopeRefusal, erasureSelectHash, erasureStatusOf, sweepUnderErasureRecord, scanEntryFiles, } from "./file-backend.js";
21
+ import { assembleMemoryExportBundle, canonicalExpectedScopes, computeMemoryBundleHash, expectedScopesInvalid, firstOutsideExpectedScopes, memoryBundleInvalid, } from "./export-bundle.js";
22
22
  import { QUARANTINE_DIR, SCAN_FUSE_THRESHOLD, quarantineAndTombstone, restoreContainedBaseline, mintStagingName, indexLineIsStale, readIndexRevs, writeIndexRevs, bumpScanFuse, scanFuseKey, canonicalize, anchorStoreRoot, landingPathOf, landsOutsideStore, storeActRefusal, storeRootMovedRefusal, adoptRootScopeLayout, clearScanFuse, effectiveControlRootOf, 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";
23
23
  import { createSessionIncarnationSeat, recalledForAnyIdentity, recalledForIdentity, rememberForIdentity } from "./session-incarnation.js";
24
24
  import { scanMemoryFileName, scanMemoryWrite, scanRemediation } from "./scan.js";
@@ -314,6 +314,10 @@ const INDEX_WRITE_REFUSED = "the derived memory index could NOT be written";
314
314
  function unexpectedLayerFile(handle, c) {
315
315
  return { path: relative(handle.memoryDir, c.path), code: "readonly_layer", reason: "a file appeared in a read-only inherited memory layer during this session — it is recorded as an unexpected write and never adopted into memory" };
316
316
  }
317
+ function noticeScopesOf(handle) {
318
+ return handle.writeScope !== null && !handle.scopes.includes(handle.writeScope) ? [...handle.scopes, handle.writeScope] : [...handle.scopes];
319
+ }
320
+ const ANNOUNCEMENT_ATTRIBUTION_WAIT_MS = 5_000;
317
321
  const STALE_LEG_NOTE = "memory session account: this harvest belongs to an earlier leg of the session (a later leg re-opened the account while it ran) — its account close and leg end were not written over the later leg";
318
322
  export class MemoryEngine {
319
323
  backend;
@@ -453,7 +457,7 @@ export class MemoryEngine {
453
457
  }
454
458
  if (writers.unscoped > 0 || writers.scopes.has(c.scope)) {
455
459
  c.rejection.reason = `a read-only inherited memory layer's file ${c.stood ? "differed from its committed content when this session started (not this session's write)" : "differs from what this session was served"}; a session that writes this layer is open (the bytes may be its uncommitted edit), so it was not restored`;
456
- report.warnings.push(`read-only layer file ${quoteId(c.relPath)} was not restored: a session that writes this layer is open, and the bytes may be its uncommitted edit`);
460
+ this.warnScoped(report, c.scope, `read-only layer file ${quoteId(c.relPath)} was not restored: a session that writes this layer is open, and the bytes may be its uncommitted edit`);
457
461
  continue;
458
462
  }
459
463
  try {
@@ -462,7 +466,7 @@ export class MemoryEngine {
462
466
  report.restored.push(c.relPath);
463
467
  }
464
468
  catch (err) {
465
- report.warnings.push(`read-only layer file ${quoteId(c.relPath)} could NOT be restored to its committed content — the modified bytes are still on disk: ${this.writeFailureText(err)}`);
469
+ this.warnScoped(report, c.scope, `read-only layer file ${quoteId(c.relPath)} could NOT be restored to its committed content — the modified bytes are still on disk: ${this.writeFailureText(err)}`);
466
470
  }
467
471
  }
468
472
  }
@@ -507,13 +511,18 @@ export class MemoryEngine {
507
511
  }
508
512
  concludeObservations(batch, report) {
509
513
  if (batch !== undefined && !batch.landed) {
510
- report.warnings.push(`${batch.rows.length} unexpected write(s) into read-only memory layers could NOT be recorded — a later session's read may adopt them: ${batch.failure ?? "unknown failure"}`);
514
+ const perLayer = new Map();
515
+ for (const row of batch.rows)
516
+ perLayer.set(row.scope, (perLayer.get(row.scope) ?? 0) + 1);
517
+ for (const [scope, n] of perLayer) {
518
+ this.warnScoped(report, scope, `${n} unexpected write(s) into a read-only memory layer could NOT be recorded — a later session's read may adopt them: ${batch.failure ?? "unknown failure"}`);
519
+ }
511
520
  }
512
521
  this.discloseUndecidedObservations(landedUndecided(batch).map((u) => u.candidate), report);
513
522
  }
514
523
  discloseUndecidedObservations(undecided, report) {
515
524
  for (const c of undecided) {
516
- report.warnings.push(`${quoteId(relative(this.memoryDir, c.path))}: changed in a read-only memory layer during this session, and whether a commit backs it could not be established (a memory transaction was pending, or the committed account could not be read) — recorded as undecided (not adopted), settled once a writing session can read the committed account`);
525
+ this.warnScoped(report, c.scope, `${quoteId(relative(this.memoryDir, c.path))}: changed in a read-only memory layer during this session, and whether a commit backs it could not be established (a memory transaction was pending, or the committed account could not be read) — recorded as undecided (not adopted), settled once a writing session can read the committed account`);
517
526
  }
518
527
  }
519
528
  async settleUndecidedObservations(handle, report, own) {
@@ -522,7 +531,7 @@ export class MemoryEngine {
522
531
  rows = undecidedLayerObservations(this.controlDir);
523
532
  }
524
533
  catch (err) {
525
- report.warnings.push(`undecided read-only-layer observations could not be read for settlement — they stay undecided (not adopted): ${this.writeFailureText(err)}`);
534
+ this.warnScoped(report, undefined, `undecided read-only-layer observations could not be read for settlement — they stay undecided (not adopted): ${this.writeFailureText(err)}`);
526
535
  return;
527
536
  }
528
537
  const verdicts = new Map();
@@ -535,7 +544,7 @@ export class MemoryEngine {
535
544
  settleUndecidedLayerObservations(this.controlDir, verdicts);
536
545
  }
537
546
  catch (err) {
538
- report.warnings.push(`undecided read-only-layer observations could not be settled — they stay undecided (not adopted): ${this.writeFailureText(err)}`);
547
+ this.warnScoped(report, undefined, `undecided read-only-layer observations could not be settled — they stay undecided (not adopted): ${this.writeFailureText(err)}`);
539
548
  return;
540
549
  }
541
550
  const served = new Set(handle.materialized.filter((m) => m.readonly).map((m) => m.path));
@@ -547,7 +556,7 @@ export class MemoryEngine {
547
556
  if (refused === true)
548
557
  report.rejections.push(unexpectedLayerFile(handle, c));
549
558
  else if (refused === undefined)
550
- report.warnings.push(`${quoteId(relative(this.memoryDir, c.path))}: changed in a read-only memory layer during this session; another writing session settled its record first, and whether memory refuses these bytes now could not be read`);
559
+ this.warnScoped(report, c.scope, `${quoteId(relative(this.memoryDir, c.path))}: changed in a read-only memory layer during this session; another writing session settled its record first, and whether memory refuses these bytes now could not be read`);
551
560
  continue;
552
561
  }
553
562
  const verdict = verdicts.get(row);
@@ -614,6 +623,82 @@ export class MemoryEngine {
614
623
  catch {
615
624
  }
616
625
  }
626
+ announce(scope, ann, stage, opts) {
627
+ if (scope === undefined || scope === null || scope === "") {
628
+ this.withholdAnnouncement(stage, ann.items);
629
+ return;
630
+ }
631
+ try {
632
+ enqueueMemoryAnnouncement(this.controlDir, { ...ann, scope }, opts);
633
+ }
634
+ catch (err) {
635
+ this.discloseAnnounceFailure(stage, err);
636
+ }
637
+ }
638
+ withholdAnnouncement(stage, items) {
639
+ if (items.length === 0)
640
+ return;
641
+ const text = `memory notice (${stage}) names no single scope, so it was kept off every session's memory notices: ${items.join(" | ")}`;
642
+ const sink = this.onIncident;
643
+ if (sink === undefined) {
644
+ console.warn(`[sema] ${text}`);
645
+ return;
646
+ }
647
+ try {
648
+ const err = new Error(text);
649
+ err.code = "memory.announcement_withheld";
650
+ sink(err);
651
+ }
652
+ catch {
653
+ }
654
+ }
655
+ announceByScope(byScope, ann, stage) {
656
+ for (const [scope, items] of byScope)
657
+ if (items.length > 0)
658
+ this.announce(scope, { ...ann, items: capItems(items) }, stage);
659
+ }
660
+ scopeOwningPath(absPath, candidates) {
661
+ try {
662
+ const target = join(canonicalize(dirname(absPath)), basename(absPath));
663
+ for (const scope of candidates) {
664
+ const home = canonicalize(scopeDirFor(this.memoryDir, this.controlDir, scope));
665
+ if (target === home || target.startsWith(`${home}${sep}`))
666
+ return scope;
667
+ }
668
+ }
669
+ catch {
670
+ }
671
+ return undefined;
672
+ }
673
+ scopeOfPlaneRel(handle, rel) {
674
+ if (rel === MEMORY_INDEX_FILENAME)
675
+ return handle.writeScope ?? undefined;
676
+ const spelled = join(handle.memoryDir, rel);
677
+ const abs = join(canonicalize(dirname(spelled)), basename(spelled));
678
+ return handle.fileToScope.get(abs) ?? handle.fileToScope.get(spelled) ?? this.scopeOwningPath(abs, noticeScopesOf(handle));
679
+ }
680
+ announcementWork = Promise.resolve();
681
+ layerWarningScopes = new WeakMap();
682
+ warnScoped(report, scope, text) {
683
+ report.warnings.push(text);
684
+ const own = this.layerWarningScopes.get(report) ?? new Map();
685
+ own.set(text, own.has(text) && own.get(text) !== scope ? undefined : scope);
686
+ this.layerWarningScopes.set(report, own);
687
+ }
688
+ async settleAnnouncementWork() {
689
+ const pending = this.announcementWork;
690
+ let timer;
691
+ const outcome = await Promise.race([
692
+ pending.then(() => "settled"),
693
+ new Promise((resolve) => {
694
+ timer = setTimeout(() => resolve("timed_out"), ANNOUNCEMENT_ATTRIBUTION_WAIT_MS);
695
+ }),
696
+ ]);
697
+ if (timer !== undefined)
698
+ clearTimeout(timer);
699
+ if (outcome === "timed_out")
700
+ this.discloseAnnounceFailure("challenge attribution", new Error(`notices still being attributed to their scopes after ${ANNOUNCEMENT_ATTRIBUTION_WAIT_MS} ms at mount — they are queued when the attribution settles`));
701
+ }
617
702
  recordRetrieved(ids) {
618
703
  for (const id of ids)
619
704
  this.retrievedThisSession.add(id);
@@ -885,20 +970,45 @@ export class MemoryEngine {
885
970
  if (eventCount !== undefined)
886
971
  this.discloseChallengeLedgerSize(eventCount);
887
972
  const fresh = assigned.filter((a) => !a.replayed);
888
- if (fresh.length > 0) {
889
- try {
890
- enqueueMemoryAnnouncement(this.controlDir, {
891
- kind: "gate",
892
- at: this.now(),
893
- items: fresh.map((a) => `memory entry challenged: id ${quoteId(a.entryId)} (generation ${a.generation}) — its content is withheld from memory tools and the injected index. If the fact is still needed, verify it with the user, record a fresh entry from their current statement, and tombstone the old one.`),
894
- });
895
- }
896
- catch (err) {
897
- this.discloseAnnounceFailure("challenge enqueue", err);
898
- }
899
- }
973
+ if (fresh.length > 0)
974
+ this.announceChallenged(fresh, this.now());
900
975
  return assigned;
901
976
  }
977
+ announceChallenged(fresh, at) {
978
+ const line = (a) => `memory entry challenged: id ${quoteId(a.entryId)} (generation ${a.generation}) — its content is withheld from memory tools and the injected index. If the fact is still needed, verify it with the user, record a fresh entry from their current statement, and tombstone the old one.`;
979
+ const work = async () => {
980
+ let timer;
981
+ const answered = await Promise.race([
982
+ this.committedAuditFace().getByIds(fresh.map((a) => a.entryId)),
983
+ new Promise((resolve) => {
984
+ timer = setTimeout(() => resolve("timed_out"), ANNOUNCEMENT_ATTRIBUTION_WAIT_MS);
985
+ }),
986
+ ]).finally(() => {
987
+ if (timer !== undefined)
988
+ clearTimeout(timer);
989
+ });
990
+ if (answered === "timed_out") {
991
+ this.discloseAnnounceFailure("challenge attribution", new Error(`the store did not answer which scope the challenged entries are in within ${ANNOUNCEMENT_ATTRIBUTION_WAIT_MS} ms — their notices went to the host`));
992
+ this.withholdAnnouncement("challenge attribution", fresh.map(line));
993
+ return;
994
+ }
995
+ const entries = answered;
996
+ const scopeOf = new Map(entries.map((e) => [e.id, e.scope]));
997
+ const byScope = new Map();
998
+ for (const a of fresh) {
999
+ const scope = scopeOf.get(a.entryId);
1000
+ const items = byScope.get(scope) ?? [];
1001
+ items.push(line(a));
1002
+ byScope.set(scope, items);
1003
+ }
1004
+ for (const [scope, items] of byScope)
1005
+ this.announce(scope, { kind: "gate", at, items }, "challenge enqueue");
1006
+ };
1007
+ this.announcementWork = this.announcementWork.then(work).catch((err) => {
1008
+ this.discloseAnnounceFailure("challenge attribution", err);
1009
+ this.withholdAnnouncement("challenge attribution", fresh.map(line));
1010
+ });
1011
+ }
902
1012
  challengeLedgerSizeDisclosed = false;
903
1013
  discloseChallengeLedgerSize(eventCount) {
904
1014
  if (this.challengeLedgerSizeDisclosed || eventCount <= CHALLENGE_LEDGER_MAX_EVENTS)
@@ -929,15 +1039,25 @@ export class MemoryEngine {
929
1039
  e.code = "memory.control_plane_not_corrupt";
930
1040
  throw e;
931
1041
  }
932
- enqueueMemoryAnnouncement(this.controlDir, {
933
- kind: "gate",
934
- at: this.now(),
935
- items: [
936
- ledger === "challenges"
937
- ? `memory control plane: the challenge ledger was found CORRUPT and a deployment-initiated reset to empty was requested — entries that were withheld by an unresolved challenge may no longer be withheld. Treat facts recalled from memory as unverified until confirmed with the user.`
938
- : `memory control plane: the harvest lineage ledger was found CORRUPT and a deployment-initiated reset to empty was requested — which session contributed which entry may no longer be recorded for anything written before this point.`,
939
- ],
940
- });
1042
+ let everyScope;
1043
+ try {
1044
+ everyScope = Object.keys(registeredScopes(this.controlDir));
1045
+ }
1046
+ catch {
1047
+ everyScope = [];
1048
+ }
1049
+ const resetLine = ledger === "challenges"
1050
+ ? `memory control plane: the challenge ledger was found CORRUPT and a deployment-initiated reset to empty was requested — entries that were withheld by an unresolved challenge may no longer be withheld. Treat facts recalled from memory as unverified until confirmed with the user.`
1051
+ : `memory control plane: the harvest lineage ledger was found CORRUPT and a deployment-initiated reset to empty was requested — which session contributed which entry may no longer be recorded for anything written before this point.`;
1052
+ if (everyScope.length === 0)
1053
+ this.withholdAnnouncement("ledger reset", [resetLine]);
1054
+ for (const scope of everyScope)
1055
+ enqueueMemoryAnnouncement(this.controlDir, {
1056
+ kind: "gate",
1057
+ scope,
1058
+ at: this.now(),
1059
+ items: [resetLine],
1060
+ });
941
1061
  const receipt = rebuildStrictControlPlaneLedger(this.controlDir, ledger, this.now);
942
1062
  const sink = this.onIncident;
943
1063
  if (sink !== undefined) {
@@ -980,9 +1100,13 @@ export class MemoryEngine {
980
1100
  const requestId = captured.requestId;
981
1101
  const face = this.backend.eraseWithEvidence;
982
1102
  if (typeof face === "function") {
1103
+ if (captured.expectedScopes !== undefined && this.backend.judgesErasureScopes !== true) {
1104
+ const e = new Error("eraseMemoryEntries: this backend's erasure-evidence face does not declare judgesErasureScopes — a grant-bearing erasure (expectedScopes) is refused rather than handed to a face that may ignore the set; nothing was deleted");
1105
+ e.code = "config.memory_erasure_scope";
1106
+ throw e;
1107
+ }
983
1108
  const att = await face.call(this.backend, captured);
984
- this.sweepErasedIndexLines(att);
985
- return att;
1109
+ 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)] : [])) }));
986
1110
  }
987
1111
  if (captured.allowUnevidenced !== true) {
988
1112
  const e = new Error("eraseMemoryEntries: this backend has no erasure-evidence capability (eraseWithEvidence) — an unevidenced erasure is refused by default. " +
@@ -992,6 +1116,9 @@ export class MemoryEngine {
992
1116
  }
993
1117
  const at = this.now();
994
1118
  const select = captured.select;
1119
+ const grant = captured.expectedScopes !== undefined ? canonicalExpectedScopes(captured.expectedScopes) : undefined;
1120
+ if (grant !== undefined && "scope" in select && firstOutsideExpectedScopes(grant, [select.scope]) >= 0)
1121
+ throw erasureScopeRefusal("eraseMemoryEntries");
995
1122
  let resolvedIds;
996
1123
  if ("ids" in select) {
997
1124
  resolvedIds = [...select.ids];
@@ -1005,8 +1132,11 @@ export class MemoryEngine {
1005
1132
  .map((c) => c.entryId)
1006
1133
  .sort();
1007
1134
  }
1008
- const present = await this.backend.getByIds(resolvedIds);
1135
+ const requested = new Set(resolvedIds);
1136
+ const present = (await this.backend.getByIds(resolvedIds)).filter((e) => requested.has(e.id));
1009
1137
  const byId = new Map(present.map((e) => [e.id, e]));
1138
+ if (grant !== undefined && firstOutsideExpectedScopes(grant, resolvedIds.map((id) => byId.get(id)?.scope)) >= 0)
1139
+ throw erasureScopeRefusal("eraseMemoryEntries");
1010
1140
  const patches = [...byId.values()].map((e) => ({ op: "delete", id: e.id, baseRev: e.rev }));
1011
1141
  const report = patches.length > 0 ? await this.backend.applyPatches(patches) : { applied: [], conflicts: [] };
1012
1142
  const appliedDeletes = new Set(report.applied.filter((a) => a.op === "delete").map((a) => a.id));
@@ -1017,19 +1147,19 @@ export class MemoryEngine {
1017
1147
  return {
1018
1148
  id,
1019
1149
  rev: e?.rev ?? "",
1020
- binding: e !== undefined ? { state: "bound", scope: e.scope, slug: e.slug } : { state: "unbound" },
1150
+ binding: e !== undefined ? { state: "bound", scope: e.scope } : { state: "unbound" },
1021
1151
  projectionsRemoved: 1,
1022
1152
  sessions: [],
1023
1153
  };
1024
1154
  });
1025
1155
  const notFound = resolvedIds.filter((id) => !byId.has(id)).map((id) => ({ id, historyUnknown: true }));
1026
1156
  const conflicts = report.conflicts.map((c) => ({ id: c.id, reason: c.reason }));
1027
- const status = conflicts.length === 0 && resolvedIds.every((id) => appliedDeletes.has(id)) ? "complete" : "partial";
1157
+ const unverified = { unverified: true };
1028
1158
  const att = {
1029
- v: 1,
1159
+ v: 2,
1030
1160
  requestId,
1031
1161
  at,
1032
- status,
1162
+ status: "partial",
1033
1163
  evidenceCapability: "none",
1034
1164
  custodyState: "capability-absent",
1035
1165
  select,
@@ -1038,119 +1168,156 @@ export class MemoryEngine {
1038
1168
  erased,
1039
1169
  notFound,
1040
1170
  conflicts,
1041
- residuals: { quarantineHits: [], quarantineOpaque: 0, propagation: "local-store-only" },
1171
+ residuals: {
1172
+ carriers: { projection: unverified, shadow: unverified, indexLine: { removed: 0, remaining: 0 }, quarantineCopy: unverified, planArchive: unverified, planWorkbook: unverified, evidenceBinding: unverified, backendDerived: unverified },
1173
+ quarantineUnattributed: unverified,
1174
+ derivedEntries: unverified,
1175
+ replay: "unsupported",
1176
+ propagation: "local-store-only",
1177
+ },
1042
1178
  };
1043
- this.sweepErasedIndexLines(att);
1044
- return att;
1179
+ const deleted = [...appliedDeletes].flatMap((id) => {
1180
+ const e = byId.get(id);
1181
+ return e !== undefined ? [e] : [];
1182
+ });
1183
+ att.status = erasureStatusOf(att);
1184
+ return this.sweepErasedIndexLines(att, undefined, () => {
1185
+ const homes = deleted.map((e) => scopeDirFor(this.memoryDir, this.controlDir, e.scope));
1186
+ return { paths: deleted.map((e, i) => join(homes[i] ?? "", `${e.slug}.md`)), homes };
1187
+ });
1045
1188
  }
1046
- sweepErasedIndexLines(att) {
1189
+ async sweepErasedIndexLines(att, requestId, owedOf) {
1047
1190
  const uncleared = [];
1191
+ const indexScope = new Map();
1192
+ let dropped = 0;
1193
+ let owesAny = false;
1048
1194
  try {
1049
- const erasedPaths = new Set();
1050
- const boundScopes = new Set();
1051
- for (const row of att.erased) {
1052
- if (row.binding.state !== "bound")
1053
- continue;
1054
- boundScopes.add(row.binding.scope);
1055
- erasedPaths.add(canonicalize(join(scopeDirFor(this.memoryDir, this.controlDir, row.binding.scope), `${row.binding.slug}.md`)));
1195
+ await sweepUnderErasureRecord(this.controlDir, this.memoryDir, requestId, (recorded) => {
1196
+ const owed = owedOf(recorded);
1197
+ const erasedPaths = new Set(owed.paths.map((p) => canonicalize(p)));
1198
+ if (erasedPaths.size === 0)
1199
+ return false;
1200
+ owesAny = true;
1201
+ const homeScopes = new Map();
1202
+ for (const row of att.erased)
1203
+ if (row.binding.state === "bound")
1204
+ homeScopes.set(scopeDirFor(this.memoryDir, this.controlDir, row.binding.scope), row.binding.scope);
1205
+ this.dropIndexLinesNaming(erasedPaths, owed.homes, uncleared, (n) => (dropped += n), indexScope, homeScopes);
1206
+ this.assertRootAnchored();
1207
+ return uncleared.length === 0;
1208
+ });
1209
+ }
1210
+ catch (err) {
1211
+ owesAny = true;
1212
+ uncleared.push(`${join(this.memoryDir, "<scope homes>", MEMORY_INDEX_FILENAME)} (index sweep failed: ${this.writeFailureText(err)})`);
1213
+ }
1214
+ if (!owesAny)
1215
+ return att;
1216
+ try {
1217
+ const out = { ...att, residuals: { ...att.residuals, carriers: { ...att.residuals.carriers, indexLine: uncleared.length === 0 ? { removed: dropped, remaining: 0 } : { unverified: true } } } };
1218
+ out.status = erasureStatusOf(out);
1219
+ return out;
1220
+ }
1221
+ catch (err) {
1222
+ if (uncleared.length > 0)
1223
+ this.discloseUncarriedIndexResidue(err, uncleared, indexScope);
1224
+ return att;
1225
+ }
1226
+ }
1227
+ dropIndexLinesNaming(erasedPaths, homes, uncleared, onDropped, indexScope, homeScopes) {
1228
+ const dirs = new Map();
1229
+ for (const scope of Object.keys(registeredScopes(this.controlDir)))
1230
+ dirs.set(scopeDirFor(this.memoryDir, this.controlDir, scope), scope);
1231
+ for (const home of homes)
1232
+ if (!dirs.has(home))
1233
+ dirs.set(home, homeScopes.get(home));
1234
+ const seen = new Set();
1235
+ for (const [dir, scope] of dirs) {
1236
+ const indexPath = join(dir, MEMORY_INDEX_FILENAME);
1237
+ if (scope !== undefined)
1238
+ indexScope.set(indexPath, scope);
1239
+ const canon = canonicalize(indexPath);
1240
+ if (seen.has(canon))
1241
+ continue;
1242
+ seen.add(canon);
1243
+ let isFile;
1244
+ try {
1245
+ isFile = lstatSync(indexPath).isFile();
1056
1246
  }
1057
- for (const row of att.erasedPreviously ?? []) {
1058
- if (row.from === undefined)
1247
+ catch (err) {
1248
+ if (err.code === "ENOENT")
1059
1249
  continue;
1060
- boundScopes.add(row.from.scope);
1061
- erasedPaths.add(canonicalize(join(scopeDirFor(this.memoryDir, this.controlDir, row.from.scope), `${row.from.slug}.md`)));
1250
+ uncleared.push(indexPath);
1251
+ continue;
1062
1252
  }
1063
- if (erasedPaths.size === 0)
1064
- return;
1065
- const dirs = new Set();
1066
- for (const scope of Object.keys(registeredScopes(this.controlDir)))
1067
- dirs.add(scopeDirFor(this.memoryDir, this.controlDir, scope));
1068
- for (const scope of boundScopes)
1069
- dirs.add(scopeDirFor(this.memoryDir, this.controlDir, scope));
1070
- const seen = new Set();
1071
- for (const dir of dirs) {
1072
- const indexPath = join(dir, MEMORY_INDEX_FILENAME);
1073
- const canon = canonicalize(indexPath);
1074
- if (seen.has(canon))
1075
- continue;
1076
- seen.add(canon);
1077
- let isFile;
1078
- try {
1079
- isFile = lstatSync(indexPath).isFile();
1080
- }
1081
- catch (err) {
1082
- if (err.code === "ENOENT")
1083
- continue;
1084
- uncleared.push(indexPath);
1085
- continue;
1086
- }
1087
- if (!isFile) {
1088
- uncleared.push(indexPath);
1089
- continue;
1090
- }
1091
- const text = readNoFollowSafe(indexPath);
1092
- if (text === undefined) {
1093
- uncleared.push(indexPath);
1094
- continue;
1095
- }
1096
- const lines = text.split("\n");
1097
- const kept = lines.filter((line) => {
1098
- const target = indexLineTarget(line);
1099
- if (target === undefined)
1100
- return true;
1101
- return !erasedPaths.has(canonicalize(join(dir, target)));
1102
- });
1103
- if (kept.length === lines.length)
1104
- continue;
1253
+ if (!isFile) {
1254
+ uncleared.push(indexPath);
1255
+ continue;
1256
+ }
1257
+ const text = readNoFollowSafe(indexPath);
1258
+ if (text === undefined) {
1259
+ uncleared.push(indexPath);
1260
+ continue;
1261
+ }
1262
+ const lines = text.split("\n");
1263
+ const kept = lines.filter((line) => {
1264
+ const target = indexLineTarget(line);
1265
+ if (target === undefined)
1266
+ return true;
1267
+ return !erasedPaths.has(canonicalize(join(dir, target)));
1268
+ });
1269
+ if (kept.length === lines.length)
1270
+ continue;
1271
+ try {
1272
+ this.refuseOutsideStore(indexPath, "rewriting the index");
1273
+ writeFileNoFollow(indexPath, kept.join("\n"));
1274
+ const fd = openSync(indexPath, fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW);
1105
1275
  try {
1106
- this.refuseOutsideStore(indexPath, "rewriting the index");
1107
- writeFileNoFollow(indexPath, kept.join("\n"));
1276
+ fsyncSync(fd);
1108
1277
  }
1109
- catch {
1110
- uncleared.push(indexPath);
1278
+ finally {
1279
+ closeSync(fd);
1111
1280
  }
1281
+ onDropped(lines.length - kept.length);
1282
+ }
1283
+ catch {
1284
+ uncleared.push(indexPath);
1112
1285
  }
1113
1286
  }
1114
- catch (err) {
1115
- uncleared.push(`${join(this.memoryDir, "<scope homes>", MEMORY_INDEX_FILENAME)} (index sweep failed: ${this.writeFailureText(err)})`);
1116
- }
1117
- if (uncleared.length > 0) {
1287
+ }
1288
+ discloseUncarriedIndexResidue(err, uncleared, indexScope) {
1289
+ try {
1290
+ let relayed;
1118
1291
  try {
1119
- att.residuals.indexUncleared = [...new Set(uncleared)].sort();
1292
+ relayed = String(err instanceof Error ? err.message : err);
1120
1293
  }
1121
- catch (err) {
1294
+ catch {
1295
+ relayed = thrownText(err);
1296
+ }
1297
+ const lineOf = (paths) => `memory erasure: the attestation could not carry its index-residue disclosure ` +
1298
+ `(${inlineUntrusted(relayed, 200)}) — MEMORY.md files that may still name erased entries: ` +
1299
+ `${paths.map((p) => inlineUntrusted(String(p), 300)).join(", ")}`;
1300
+ const detail = lineOf(uncleared);
1301
+ const pathsByScope = new Map();
1302
+ for (const p of uncleared) {
1303
+ const scope = indexScope.get(String(p));
1304
+ pathsByScope.set(scope, [...(pathsByScope.get(scope) ?? []), String(p)]);
1305
+ }
1306
+ for (const [scope, paths] of pathsByScope)
1307
+ this.announce(scope, { kind: "external", at: this.now(), items: [lineOf(paths)] }, "erasure index-residue enqueue");
1308
+ const sink = this.onIncident;
1309
+ if (sink !== undefined) {
1122
1310
  try {
1123
- let relayed;
1124
- try {
1125
- relayed = String(err instanceof Error ? err.message : err);
1126
- }
1127
- catch {
1128
- relayed = thrownText(err);
1129
- }
1130
- const detail = `memory erasure: the attestation could not carry its index-residue disclosure ` +
1131
- `(${inlineUntrusted(relayed, 200)}) — MEMORY.md files that may still name erased entries: ` +
1132
- `${uncleared.map((p) => inlineUntrusted(String(p), 300)).join(", ")}`;
1133
- try {
1134
- enqueueMemoryAnnouncement(this.controlDir, { kind: "external", at: this.now(), items: [detail] });
1135
- }
1136
- catch (enqueueErr) {
1137
- this.discloseAnnounceFailure("erasure index-residue enqueue", enqueueErr);
1138
- }
1139
- const sink = this.onIncident;
1140
- if (sink !== undefined) {
1141
- try {
1142
- const e = new Error(detail);
1143
- e.code = "memory.erasure_index_residue";
1144
- sink(e);
1145
- }
1146
- catch {
1147
- }
1148
- }
1311
+ const e = new Error(detail);
1312
+ e.code = "memory.erasure_index_residue";
1313
+ sink(e);
1149
1314
  }
1150
1315
  catch {
1151
1316
  }
1152
1317
  }
1153
1318
  }
1319
+ catch {
1320
+ }
1154
1321
  }
1155
1322
  async provenanceOf(entryId) {
1156
1323
  const snapshotFn = this.backend.committedSnapshotOf;
@@ -1305,7 +1472,15 @@ export class MemoryEngine {
1305
1472
  e.code = "config.memory_import_request";
1306
1473
  throw e;
1307
1474
  }
1308
- const bad = memoryBundleInvalid(inert, { ...(opts?.expectedScopes !== undefined ? { expectedScopes: opts.expectedScopes } : {}) });
1475
+ const rawGrant = opts?.expectedScopes;
1476
+ const grant = Array.isArray(rawGrant) ? [...rawGrant] : rawGrant;
1477
+ const grantBad = grant !== undefined ? expectedScopesInvalid(grant) : undefined;
1478
+ if (grantBad !== undefined) {
1479
+ const e = new Error(`importMemoryBundle: ${grantBad} — nothing was imported`);
1480
+ e.code = "config.memory_import_request";
1481
+ throw e;
1482
+ }
1483
+ const bad = memoryBundleInvalid(inert, grant !== undefined ? { expectedScopes: grant } : {});
1309
1484
  if (bad !== undefined) {
1310
1485
  const e = new Error(`importMemoryBundle: ${bad} — the whole package is refused, nothing landed`);
1311
1486
  e.code = "memory.import_rejected";
@@ -1415,6 +1590,7 @@ export class MemoryEngine {
1415
1590
  }
1416
1591
  async materialize(scopes, writeScope, opts) {
1417
1592
  const legSeat = {};
1593
+ await this.settleAnnouncementWork();
1418
1594
  try {
1419
1595
  return await this.materializeCore(scopes, writeScope, opts, legSeat);
1420
1596
  }
@@ -1441,8 +1617,8 @@ export class MemoryEngine {
1441
1617
  scopeDirs.set(scope, registerScope(this.memoryDir, this.controlDir, scope));
1442
1618
  if (writeScope !== null && !scopeDirs.has(writeScope))
1443
1619
  scopeDirs.set(writeScope, registerScope(this.memoryDir, this.controlDir, writeScope));
1444
- for (const dir of scopeDirs.values())
1445
- this.censusScopeTree(dir);
1620
+ for (const [scope, dir] of scopeDirs)
1621
+ this.censusScopeTree(scope, dir);
1446
1622
  const layerBaseline = this.zeroCopy
1447
1623
  ? readOnlyLayerCensus([...scopeDirs].filter(([scope]) => scope !== writeScope).map(([, dir]) => dir))
1448
1624
  : undefined;
@@ -1548,6 +1724,7 @@ export class MemoryEngine {
1548
1724
  try {
1549
1725
  enqueueMemoryAnnouncement(this.controlDir, {
1550
1726
  kind: "external",
1727
+ scope: writeScope,
1551
1728
  at: this.now(),
1552
1729
  items: [
1553
1730
  restoredCommitted
@@ -1675,16 +1852,11 @@ export class MemoryEngine {
1675
1852
  if (indexGate !== undefined) {
1676
1853
  if (!indexGate.contained)
1677
1854
  handle.indexOnDiskUntrusted = true;
1678
- try {
1679
- enqueueMemoryAnnouncement(this.controlDir, {
1680
- kind: "gate",
1681
- at: this.now(),
1682
- items: [`memory index rejected: ${inlineUntrusted(indexGate.rejection.reason)}`],
1683
- });
1684
- }
1685
- catch (err) {
1686
- this.discloseAnnounceFailure("index-gate enqueue", err);
1687
- }
1855
+ this.announce(handle.writeScope, {
1856
+ kind: "gate",
1857
+ at: this.now(),
1858
+ items: [`memory index rejected: ${inlineUntrusted(indexGate.rejection.reason)}`],
1859
+ }, "index-gate enqueue");
1688
1860
  }
1689
1861
  const materializeHealWarnings = [];
1690
1862
  const indexWrite = writeScope !== null && !restricted;
@@ -1711,12 +1883,7 @@ export class MemoryEngine {
1711
1883
  catch {
1712
1884
  }
1713
1885
  }
1714
- try {
1715
- enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", at: this.now(), items: [detail] });
1716
- }
1717
- catch (err) {
1718
- this.discloseAnnounceFailure("active-set collapse enqueue", err);
1719
- }
1886
+ this.announce(scope, { kind: "gate", at: this.now(), items: [detail] }, "active-set collapse enqueue");
1720
1887
  }
1721
1888
  }
1722
1889
  }
@@ -1725,22 +1892,12 @@ export class MemoryEngine {
1725
1892
  }
1726
1893
  const indexRefusals = materializeHealWarnings.filter((w) => w.startsWith(INDEX_WRITE_REFUSED));
1727
1894
  if (indexRefusals.length > 0) {
1728
- try {
1729
- enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", at: this.now(), items: capItems(indexRefusals.map((w) => inlineUntrusted(w, 500))) });
1730
- }
1731
- catch (err) {
1732
- this.discloseAnnounceFailure("materialize index-write enqueue", err);
1733
- }
1895
+ this.announce(writeScope, { kind: "gate", at: this.now(), items: capItems(indexRefusals.map((w) => inlineUntrusted(w, 500))) }, "materialize index-write enqueue");
1734
1896
  }
1735
1897
  if (indexWrite && handle.indexOnDiskUntrusted !== true) {
1736
1898
  const dropNotes = materializeHealWarnings.filter((w) => w.includes("opaque external-origin handle"));
1737
1899
  if (dropNotes.length > 0) {
1738
- try {
1739
- enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", at: this.now(), items: capItems(dropNotes.map((w) => `index note: ${inlineUntrusted(w, 200)}`)) });
1740
- }
1741
- catch (err) {
1742
- this.discloseAnnounceFailure("materialize index-heal enqueue", err);
1743
- }
1900
+ this.announce(writeScope, { kind: "gate", at: this.now(), items: capItems(dropNotes.map((w) => `index note: ${inlineUntrusted(w, 200)}`)) }, "materialize index-heal enqueue");
1744
1901
  }
1745
1902
  }
1746
1903
  if (handle.writableRoot !== null) {
@@ -1771,12 +1928,7 @@ export class MemoryEngine {
1771
1928
  if (firstRefused !== undefined) {
1772
1929
  const [path, err] = firstRefused;
1773
1930
  const remedy = permissionRemedyOf(Object.assign(new Error("write refused"), { code: err?.code, path }), this.memoryDir);
1774
- try {
1775
- enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", at: this.now(), items: [inlineUntrusted(`this session's memory write home cannot be written at ${refused.size} path(s) (${path}${refused.size > 1 ? ", …" : ""}): ${remedy ?? thrownText(err)}`, 700)] });
1776
- }
1777
- catch (err) {
1778
- this.discloseAnnounceFailure("write-home permission enqueue", err);
1779
- }
1931
+ this.announce(handle.writeScope, { kind: "gate", at: this.now(), items: [inlineUntrusted(`this session's memory write home cannot be written at ${refused.size} path(s) (${path}${refused.size > 1 ? ", …" : ""}): ${remedy ?? thrownText(err)}`, 700)] }, "write-home permission enqueue");
1780
1932
  }
1781
1933
  const indexOnDisk = readSafe(join(writableRoot, MEMORY_INDEX_FILENAME));
1782
1934
  handle.planeBaseline = { files, ...(indexOnDisk !== undefined ? { index: indexOnDisk } : {}) };
@@ -1806,7 +1958,10 @@ export class MemoryEngine {
1806
1958
  let announcements;
1807
1959
  let announceBlock;
1808
1960
  try {
1809
- const drained = drainMemoryAnnouncements(this.controlDir);
1961
+ const drained = drainMemoryAnnouncements(this.controlDir, noticeScopesOf(handle));
1962
+ if (drained.unscopedDiscarded > 0) {
1963
+ this.withholdAnnouncement("queue upgrade", [`${drained.unscopedDiscarded} queued memory notice(s) without a scope (written before notices were filed per scope) were discarded unread`]);
1964
+ }
1810
1965
  if (drained.queue.length > 0 || drained.folded > 0) {
1811
1966
  announcements = drained.queue;
1812
1967
  announceBlock = renderAnnouncements(drained.queue, drained.folded, opts?.reminderMark);
@@ -1877,12 +2032,20 @@ export class MemoryEngine {
1877
2032
  const report = out.report;
1878
2033
  try {
1879
2034
  const named = report.reportId !== undefined ? { reportId: report.reportId } : {};
1880
- const gateItems = gateAnnouncementItems(report);
1881
- if (gateItems.length > 0)
1882
- enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", at: this.now(), ...named, items: gateItems });
2035
+ const layerWarnings = this.layerWarningScopes.get(report);
2036
+ const owner = {
2037
+ writeScope: handle.writeScope,
2038
+ ofRel: (rel) => this.scopeOfPlaneRel(handle, rel),
2039
+ ofWarning: (w) => (layerWarnings !== undefined && layerWarnings.has(w) ? layerWarnings.get(w) : handle.writeScope ?? undefined),
2040
+ };
2041
+ this.announceByScope(gateAnnouncementItems(report, owner), { kind: "gate", at: this.now(), ...named }, "harvest enqueue");
1883
2042
  if (report.inboundFindings !== undefined && report.inboundFindings.length > 0) {
1884
- const items = report.inboundFindings.map((r) => `out-of-session memory change rejected [${r.code}] ${quoteId(r.path)} — ${inlineUntrusted(r.reason, 240)}`);
1885
- enqueueMemoryAnnouncement(this.controlDir, { kind: "external", at: this.now(), ...named, items: capItems(items) });
2043
+ const byScope = new Map();
2044
+ for (const r of report.inboundFindings) {
2045
+ const scope = owner.ofRel(r.path);
2046
+ byScope.set(scope, [...(byScope.get(scope) ?? []), `out-of-session memory change rejected [${r.code}] ${quoteId(r.path)} — ${inlineUntrusted(r.reason, 240)}`]);
2047
+ }
2048
+ this.announceByScope(byScope, { kind: "external", at: this.now(), ...named }, "harvest enqueue");
1886
2049
  }
1887
2050
  }
1888
2051
  catch (err) {
@@ -1940,7 +2103,7 @@ export class MemoryEngine {
1940
2103
  observed = await this.observeReadOnlyLayers(handle, report, opts?.sessionId);
1941
2104
  }
1942
2105
  catch (err) {
1943
- report.warnings.push(`read-only memory layers could not be examined for unexpected writes (nothing was recorded): ${this.writeFailureText(err)}`);
2106
+ this.warnScoped(report, undefined, `read-only memory layers could not be examined for unexpected writes (nothing was recorded): ${this.writeFailureText(err)}`);
1944
2107
  }
1945
2108
  if (admitsNothing) {
1946
2109
  try {
@@ -2004,7 +2167,7 @@ export class MemoryEngine {
2004
2167
  reconcileHolds(this.controlDir, { homeOf: (scope) => scopeDirFor(this.memoryDir, this.controlDir, scope), now: this.now });
2005
2168
  const newlyExpired = expireOverdueSettlements(this.controlDir, { timeoutMs: this.holdSettleTimeoutMs, now: this.now });
2006
2169
  if (newlyExpired.length > 0) {
2007
- report.warnings.push(`${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`);
2170
+ 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`);
2008
2171
  }
2009
2172
  replayExternalSettlementEffects(this.controlDir, { carry, now: this.now });
2010
2173
  const settled = await this.settleDueHolds(handle, report);
@@ -2031,7 +2194,7 @@ export class MemoryEngine {
2031
2194
  const rec = reconcileLineage(this.controlDir);
2032
2195
  this.settlePromotions(rec.promoted);
2033
2196
  for (const u of rec.undecidable) {
2034
- report.warnings.push(u.kind === "latch-only"
2197
+ this.warnScoped(report, undefined, u.kind === "latch-only"
2035
2198
  ? `memory bundle-import latch ${u.txnId} is unsettled (an import did not run to completion) — ${u.entryIds.length} entr${u.entryIds.length === 1 ? "y is" : "ies are"} latched (memory reads refuse them) until the SAME bundle is imported again to completion (importMemoryBundle; adjudication is refused on import latches)`
2036
2199
  : `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)`);
2037
2200
  }
@@ -2975,6 +3138,7 @@ export class MemoryEngine {
2975
3138
  try {
2976
3139
  enqueueMemoryAnnouncement(this.controlDir, {
2977
3140
  kind: "gate",
3141
+ scope: handle.writeScope,
2978
3142
  at: this.now(),
2979
3143
  items: [`memory consolidation recommended for scope ${inlineUntrusted(handle.writeScope, 80)}: ${rec.sessionsSince} distinct session(s) since the last run — the host decides (nothing runs automatically)`],
2980
3144
  });
@@ -3007,7 +3171,7 @@ export class MemoryEngine {
3007
3171
  const dispose = (row, terminal, why) => {
3008
3172
  const d = disposeHold(this.controlDir, { holdId: row.holdId, terminal, now: this.now });
3009
3173
  out.disposed.push({ relPath: this.holdPlaneRel(row), terminal });
3010
- report.warnings.push(`held instruction entry ${this.holdPlaneRel(row)} DISPOSED (${terminal}): ${why}${d.quarantineName !== undefined ? ` — captured bytes moved to control-plane quarantine (${d.quarantineName})` : ""}`);
3174
+ 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})` : ""}`);
3011
3175
  };
3012
3176
  for (const row of readHolds(this.controlDir)) {
3013
3177
  const valveRelease = row.resolved === "release" && (row.status === "held" || (row.status === "disposed" && row.disposition?.terminal === "expired"));
@@ -3050,10 +3214,10 @@ export class MemoryEngine {
3050
3214
  }
3051
3215
  catch {
3052
3216
  }
3053
- report.warnings.push(`held instruction entry ${this.holdPlaneRel(row)} valve release FAILED (${terminal}): ${why} — the standing "release" verdict was consumed (the hold keeps its earlier disposition; resolveHold can re-arm it after the cause is reconciled)`);
3217
+ this.warnScoped(report, row.scope, `held instruction entry ${this.holdPlaneRel(row)} valve release FAILED (${terminal}): ${why} — the standing "release" verdict was consumed (the hold keeps its earlier disposition; resolveHold can re-arm it after the cause is reconciled)`);
3054
3218
  return undefined;
3055
3219
  }
3056
- report.warnings.push(`held instruction entry ${this.holdPlaneRel(row)} DISPOSED (${terminal}): ${why}${d.quarantineName !== undefined ? ` — captured bytes moved to control-plane quarantine (${d.quarantineName})` : ""}`);
3220
+ 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})` : ""}`);
3057
3221
  opts.disposed.push({ relPath: this.holdPlaneRel(row), terminal });
3058
3222
  return undefined;
3059
3223
  };
@@ -3076,10 +3240,10 @@ export class MemoryEngine {
3076
3240
  return disposeOut("discarded", UNCLOSED_MEMORY_FILE_REASON);
3077
3241
  const fm = { ...parsed.frontmatter };
3078
3242
  if (stripModelWrittenOrigin(fm)) {
3079
- report.warnings.push(`${this.holdPlaneRel(row)}: origin-form frontmatter in the held bytes was not adopted — the external-origin marker is engine-authored`);
3243
+ this.warnScoped(report, row.scope, `${this.holdPlaneRel(row)}: origin-form frontmatter in the held bytes was not adopted — the external-origin marker is engine-authored`);
3080
3244
  }
3081
3245
  if (stripModelWrittenDistilled(fm)) {
3082
- report.warnings.push(`${this.holdPlaneRel(row)}: distilled-form frontmatter in the held bytes was not adopted — the consolidation lineage block is engine-authored`);
3246
+ this.warnScoped(report, row.scope, `${this.holdPlaneRel(row)}: distilled-form frontmatter in the held bytes was not adopted — the consolidation lineage block is engine-authored`);
3083
3247
  }
3084
3248
  const committedFm = row.entryId !== undefined ? await this.committedFrontmatterFor(row.entryId) : undefined;
3085
3249
  const committedOrigin = committedFm !== undefined ? committedOriginOf(committedFm) : undefined;
@@ -3107,7 +3271,7 @@ export class MemoryEngine {
3107
3271
  stageLineagePending(this.controlDir, txnId, row.sessionId, [{ entryId: patch.id, rev: entry.rev, ...(fm.origin !== undefined ? { marked: true } : {}) }], this.now);
3108
3272
  }
3109
3273
  catch (err) {
3110
- report.warnings.push(`held instruction entry ${this.holdPlaneRel(row)} could not stage its lineage row — kept HELD for the next harvest: ${thrownText(err)}`);
3274
+ this.warnScoped(report, row.scope, `held instruction entry ${this.holdPlaneRel(row)} could not stage its lineage row — kept HELD for the next harvest: ${thrownText(err)}`);
3111
3275
  return undefined;
3112
3276
  }
3113
3277
  let patchReport;
@@ -3120,7 +3284,7 @@ export class MemoryEngine {
3120
3284
  }
3121
3285
  catch {
3122
3286
  }
3123
- report.warnings.push(`held instruction entry ${this.holdPlaneRel(row)} could not commit (backend refused) — kept HELD for the next harvest: ${thrownText(err)}`);
3287
+ this.warnScoped(report, row.scope, `held instruction entry ${this.holdPlaneRel(row)} could not commit (backend refused) — kept HELD for the next harvest: ${thrownText(err)}`);
3124
3288
  return undefined;
3125
3289
  }
3126
3290
  if (patchReport.conflicts.length > 0) {
@@ -3142,7 +3306,7 @@ export class MemoryEngine {
3142
3306
  }
3143
3307
  if (alreadyApplied) {
3144
3308
  markHoldReleased(this.controlDir, { holdId: row.holdId, now: this.now });
3145
- report.warnings.push(`held instruction entry ${this.holdPlaneRel(row)} was already committed by an earlier release attempt (crash-idempotent retry) — marked released`);
3309
+ this.warnScoped(report, row.scope, `held instruction entry ${this.holdPlaneRel(row)} was already committed by an earlier release attempt (crash-idempotent retry) — marked released`);
3146
3310
  return this.holdPlaneRel(row);
3147
3311
  }
3148
3312
  return disposeOut("conflict", `the entry moved since capture (${patchReport.conflicts[0].reason}) — never blind-written; the captured bytes stay in quarantine for the host to reconcile`);
@@ -3153,7 +3317,7 @@ export class MemoryEngine {
3153
3317
  this.settlePromotions(promoted);
3154
3318
  }
3155
3319
  catch (err) {
3156
- report.warnings.push(`released hold ${this.holdPlaneRel(row)}: lineage settlement failed after commit — the entry stays latched until a later harvest reconciles: ${thrownText(err)}`);
3320
+ 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)}`);
3157
3321
  }
3158
3322
  markHoldReleased(this.controlDir, { holdId: row.holdId, now: this.now });
3159
3323
  const landedSlug = patchReport.applied.find((a) => a.id === patch.id)?.slug ?? row.slug;
@@ -3171,7 +3335,7 @@ export class MemoryEngine {
3171
3335
  }
3172
3336
  }
3173
3337
  }
3174
- report.warnings.push(`held instruction entry ${this.holdPlaneRel(row)} RELEASED and committed${opts.valve ? ' with a "static"-cause external-origin marker (a host-valve release is not a cleanliness proof)' : " unmarked (every delegation of its writer session settled clean)"}`);
3338
+ this.warnScoped(report, row.scope, `held instruction entry ${this.holdPlaneRel(row)} RELEASED and committed${opts.valve ? ' with a "static"-cause external-origin marker (a host-valve release is not a cleanliness proof)' : " unmarked (every delegation of its writer session settled clean)"}`);
3175
3339
  return this.holdPlaneRel(row);
3176
3340
  }
3177
3341
  resolveSettlement(settleId, to, requestId) {
@@ -3343,7 +3507,7 @@ export class MemoryEngine {
3343
3507
  });
3344
3508
  if (opts.force !== undefined && announceForce) {
3345
3509
  try {
3346
- enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", at, items: [`memory consolidation snapshot FORCED past the time/session gates for scope ${inlineUntrusted(scope, 80)} (requestId ${inlineUntrusted(opts.force.requestId, 80)})`] });
3510
+ enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", scope, at, items: [`memory consolidation snapshot FORCED past the time/session gates for scope ${inlineUntrusted(scope, 80)} (requestId ${inlineUntrusted(opts.force.requestId, 80)})`] });
3347
3511
  }
3348
3512
  catch (err) {
3349
3513
  this.discloseAnnounceFailure("consolidation force audit enqueue", err);
@@ -3455,7 +3619,7 @@ export class MemoryEngine {
3455
3619
  });
3456
3620
  if (err.code === "memory.consolidation_refused") {
3457
3621
  try {
3458
- enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", at: this.now(), items: [`memory consolidation plan for scope ${inlineUntrusted(scope, 80)} REFUSED: ${inlineUntrusted(err.reasons[0] ?? err.message, 200)}`] });
3622
+ enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", scope, at: this.now(), items: [`memory consolidation plan for scope ${inlineUntrusted(scope, 80)} REFUSED:${inlineUntrusted(err.reasons[0] ?? err.message, 200)}`] });
3459
3623
  }
3460
3624
  catch (annErr) {
3461
3625
  this.discloseAnnounceFailure("consolidation refusal enqueue", annErr);
@@ -3592,7 +3756,7 @@ export class MemoryEngine {
3592
3756
  if (edgeTargets.size > ceiling) {
3593
3757
  wholeReasons.push(`supersession fuse: ${edgeTargets.size} distinct targets > ceiling ${ceiling} (active set ${activeSetSize}, ratio ${screened.supersedeRatioCap}, floor ${screened.supersedeAbsoluteFloor}, N-1 unconditional)`);
3594
3758
  try {
3595
- enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", at, items: [`memory consolidation plan for scope ${inlineUntrusted(scope, 80)} REFUSED by the supersession fuse (${edgeTargets.size} targets over the ${ceiling} ceiling)`] });
3759
+ enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", scope, at, items: [`memory consolidation plan for scope ${inlineUntrusted(scope, 80)} REFUSED by the supersession fuse (${edgeTargets.size} targets over the ${ceiling} ceiling)`] });
3596
3760
  }
3597
3761
  catch (err) {
3598
3762
  this.discloseAnnounceFailure("consolidation fuse enqueue", err);
@@ -3943,7 +4107,7 @@ export class MemoryEngine {
3943
4107
  plan.audit.push({ at, event: "conflict" });
3944
4108
  this.writePlanOwned(plan);
3945
4109
  try {
3946
- enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", at, items: [`memory consolidation plan ${plan.planId} for scope ${inlineUntrusted(plan.scope, 80)} parked CONFLICT — resolveConsolidationPlan(planId, "retry"|"discard") is the host valve`] });
4110
+ enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", scope: plan.scope, at, items: [`memory consolidation plan ${plan.planId} for scope ${inlineUntrusted(plan.scope, 80)} parked CONFLICT — resolveConsolidationPlan(planId, "retry"|"discard") is the host valve`] });
3947
4111
  }
3948
4112
  catch (err) {
3949
4113
  this.discloseAnnounceFailure("consolidation conflict enqueue", err);
@@ -4012,6 +4176,7 @@ export class MemoryEngine {
4012
4176
  try {
4013
4177
  enqueueMemoryAnnouncement(this.controlDir, {
4014
4178
  kind: "gate",
4179
+ scope: plan.scope,
4015
4180
  at,
4016
4181
  items: [`memory consolidation plan ${plan.planId} for scope ${inlineUntrusted(plan.scope, 80)} COMPLETED: ${productCount} product(s) committed, ${supersededCount} entr${supersededCount === 1 ? "y" : "ies"} superseded (evidence retained), ${plan.intents.length} intent(s) settled`],
4017
4182
  });
@@ -4087,7 +4252,7 @@ export class MemoryEngine {
4087
4252
  return { result: undefined };
4088
4253
  });
4089
4254
  try {
4090
- enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", at: this.now(), items: [`memory consolidation seat for scope ${inlineUntrusted(scope, 80)} released: plan ${seated} was lost before freeze (crash window) — nothing was applied`] });
4255
+ enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", scope, at: this.now(), items: [`memory consolidation seat for scope ${inlineUntrusted(scope, 80)} released: plan ${seated} was lost before freeze (crash window) — nothing was applied`] });
4091
4256
  }
4092
4257
  catch (err) {
4093
4258
  this.discloseAnnounceFailure("consolidation orphan-seat enqueue", err);
@@ -4174,6 +4339,7 @@ export class MemoryEngine {
4174
4339
  throw new ConsolidationRefusedError("memory.consolidation_refused", `memory consolidation plan ${planId} is corrupt (${read.detail}) — retry has no readable intents; discard it (the bytes move to custody).`);
4175
4340
  }
4176
4341
  const custody = quarantineCorruptPlan(this.controlDir, planId);
4342
+ let heldBy;
4177
4343
  updateConsolidationGate(this.controlDir, (file) => {
4178
4344
  for (const [scope, row] of Object.entries(file.scopes)) {
4179
4345
  if (row.openPlanId === planId) {
@@ -4181,17 +4347,13 @@ export class MemoryEngine {
4181
4347
  row.epoch += 1;
4182
4348
  delete row.snapshot;
4183
4349
  file.scopes[scope] = row;
4350
+ heldBy = scope;
4184
4351
  return { next: file, result: undefined };
4185
4352
  }
4186
4353
  }
4187
4354
  return { result: undefined };
4188
4355
  });
4189
- try {
4190
- enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", at, items: [`memory consolidation plan ${planId} DISCARDED as corrupt (requestId ${inlineUntrusted(opts.requestId, 80)})${custody !== undefined ? ` — bytes retained at ${custody}` : ""}`] });
4191
- }
4192
- catch (err) {
4193
- this.discloseAnnounceFailure("consolidation corrupt-discard enqueue", err);
4194
- }
4356
+ this.announce(heldBy, { kind: "gate", at, items: [`memory consolidation plan ${planId} DISCARDED as corrupt (requestId ${inlineUntrusted(opts.requestId, 80)})${custody !== undefined ? ` — bytes retained at ${custody}` : ""}`] }, "consolidation corrupt-discard enqueue");
4195
4357
  return { planId, action, state: "discarded", ...(custody !== undefined ? { custody } : {}) };
4196
4358
  }
4197
4359
  if (read.state === "absent") {
@@ -4297,7 +4459,7 @@ export class MemoryEngine {
4297
4459
  }
4298
4460
  await this.settleGateForTerminalPlan(plan);
4299
4461
  try {
4300
- enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", at, items: [`memory consolidation plan ${planId} DISCARDED ${by} — ${abandoned.length} intent(s) settled ABANDONED (given up, not fulfilled)`] });
4462
+ enqueueMemoryAnnouncement(this.controlDir, { kind: "gate", scope: plan.scope, at, items: [`memory consolidation plan ${planId} DISCARDED ${by} —${abandoned.length} intent(s) settled ABANDONED (given up, not fulfilled)`] });
4301
4463
  }
4302
4464
  catch (err) {
4303
4465
  this.discloseAnnounceFailure("consolidation discard enqueue", err);
@@ -4701,6 +4863,7 @@ export class MemoryEngine {
4701
4863
  try {
4702
4864
  enqueueMemoryAnnouncement(this.controlDir, {
4703
4865
  kind: "gate",
4866
+ scope: row.scope,
4704
4867
  at: this.now(),
4705
4868
  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)})`],
4706
4869
  });
@@ -5143,7 +5306,7 @@ export class MemoryEngine {
5143
5306
  return { state: "unknown" };
5144
5307
  }
5145
5308
  }
5146
- censusScopeTree(dir) {
5309
+ censusScopeTree(scope, dir) {
5147
5310
  if (!existsSync(dir))
5148
5311
  return;
5149
5312
  const unreadable = [];
@@ -5179,6 +5342,7 @@ export class MemoryEngine {
5179
5342
  try {
5180
5343
  enqueueMemoryAnnouncement(this.controlDir, {
5181
5344
  kind: "gate",
5345
+ scope,
5182
5346
  at: this.now(),
5183
5347
  items: unreadable
5184
5348
  .slice(0, 12)
@@ -5206,28 +5370,36 @@ function capItems(items) {
5206
5370
  const extra = items.length - MAX_ANNOUNCEMENT_ITEMS;
5207
5371
  return [...items.slice(0, MAX_ANNOUNCEMENT_ITEMS), `…and ${extra} more event(s) — see the harvest report`];
5208
5372
  }
5209
- function gateAnnouncementItems(report) {
5210
- const items = [];
5373
+ function gateAnnouncementItems(report, owner) {
5374
+ const out = new Map();
5375
+ const put = (scope, item) => {
5376
+ const items = out.get(scope) ?? [];
5377
+ items.push(item);
5378
+ out.set(scope, items);
5379
+ };
5380
+ const ws = owner.writeScope ?? undefined;
5211
5381
  const total = report.patches.add + report.patches.update + report.patches.delete;
5212
5382
  if (total > 0) {
5213
- items.push(`memory updated by the previous session's harvest: ${report.patches.add} added, ${report.patches.update} updated, ${report.patches.delete} deleted`);
5383
+ put(ws, `memory updated by the previous session's harvest: ${report.patches.add} added, ${report.patches.update} updated, ${report.patches.delete} deleted`);
5214
5384
  }
5215
5385
  if (report.incident !== undefined)
5216
- items.push(`harvest refused (${report.incident.kind}): ${inlineUntrusted(report.incident.detail, 300)}`);
5386
+ put(report.incident.kind === "sidecar_corrupt" ? undefined : ws, `harvest refused (${report.incident.kind}): ${inlineUntrusted(report.incident.detail, 300)}`);
5217
5387
  for (const r of report.rejections)
5218
- items.push(`write rejected [${r.code}] ${quoteId(r.path)} — ${inlineUntrusted(r.reason, 240)}`);
5388
+ put(owner.ofRel(r.path), `write rejected [${r.code}] ${quoteId(r.path)} — ${inlineUntrusted(r.reason, 240)}`);
5219
5389
  for (const p of report.restored)
5220
- items.push(`restored from the committed copy: ${quoteId(p)}`);
5390
+ put(owner.ofRel(p), `restored from the committed copy: ${quoteId(p)}`);
5221
5391
  for (const c of report.conflicts)
5222
- items.push(`concurrent-change conflict (${c.op}) for entry id ${quoteId(c.id)} — this session's version was NOT committed`);
5392
+ put(ws, `concurrent-change conflict (${c.op}) for entry id ${quoteId(c.id)} — this session's version was NOT committed`);
5223
5393
  if (report.degraded !== undefined)
5224
- items.push(`harvest degraded (${report.degraded.reason}): ${report.degraded.pending.length} file(s) deferred to the next harvest`);
5394
+ put(ws, `harvest degraded (${report.degraded.reason}): ${report.degraded.pending.length} file(s) deferred to the next harvest`);
5225
5395
  for (const x of report.quarantineFailures ?? []) {
5226
- items.push(`quarantine escalation ${quoteId(x.path)} (${x.contained ? "contained" : "NOT contained"}): ${inlineUntrusted(x.detail, 200)}`);
5396
+ put(owner.ofRel(x.path), `quarantine escalation ${quoteId(x.path)} (${x.contained ? "contained" : "NOT contained"}): ${inlineUntrusted(x.detail, 200)}`);
5227
5397
  }
5228
5398
  for (const w of report.warnings)
5229
- items.push(`index note: ${inlineUntrusted(w, 200)}`);
5230
- return capItems(items);
5399
+ put(owner.ofWarning(w), `index note: ${inlineUntrusted(w, 200)}`);
5400
+ for (const [scope, items] of out)
5401
+ out.set(scope, capItems(items));
5402
+ return out;
5231
5403
  }
5232
5404
  export function renderAnnouncements(queue, folded, reminderMark) {
5233
5405
  const lines = [];