@sema-agent/core 7.63.0 → 7.65.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 (239) hide show
  1. package/CHANGELOG.md +725 -0
  2. package/dist/agents/cc-peer-directory.js +1 -0
  3. package/dist/agents/cc-peer-wire.d.ts +4 -0
  4. package/dist/agents/cc-peer-wire.js +1 -0
  5. package/dist/agents/cc-uds-peer-channel.js +5 -0
  6. package/dist/agents/cross-session-judge.d.ts +3 -1
  7. package/dist/agents/cross-session-judge.js +4 -1
  8. package/dist/agents/peer-directory.d.ts +8 -0
  9. package/dist/agents/peer-directory.js +4 -0
  10. package/dist/agents/peer-held-queue.d.ts +9 -3
  11. package/dist/agents/peer-held-queue.js +12 -9
  12. package/dist/agents/peer-idle.d.ts +20 -1
  13. package/dist/agents/peer-idle.js +23 -12
  14. package/dist/agents/peer-notice-route.d.ts +16 -5
  15. package/dist/agents/peer-notice-route.js +11 -9
  16. package/dist/agents/peer-notices.d.ts +25 -8
  17. package/dist/agents/peer-notices.js +6 -1
  18. package/dist/agents/peer-session-drain.d.ts +32 -6
  19. package/dist/agents/peer-session-drain.js +100 -26
  20. package/dist/agents/send-message-tool.d.ts +7 -0
  21. package/dist/agents/send-message-tool.js +18 -6
  22. package/dist/agents/subagent.js +25 -5
  23. package/dist/core/allow-entry.d.ts +4 -0
  24. package/dist/core/ask-origin.d.ts +1 -1
  25. package/dist/core/ask-origin.js +2 -2
  26. package/dist/core/ask-unresolvable-notice.d.ts +13 -4
  27. package/dist/core/ask-unresolvable-notice.js +10 -3
  28. package/dist/core/auto-compaction.d.ts +7 -5
  29. package/dist/core/auto-promote.d.ts +3 -2
  30. package/dist/core/background-agent-store.d.ts +14 -0
  31. package/dist/core/background-agent-store.js +3 -0
  32. package/dist/core/checkpoint-queries.d.ts +9 -6
  33. package/dist/core/checkpoint-queries.js +8 -3
  34. package/dist/core/checkpoint-store.d.ts +7 -4
  35. package/dist/core/context-settings.d.ts +39 -9
  36. package/dist/core/context-settings.js +32 -10
  37. package/dist/core/engine-attestation.d.ts +26 -6
  38. package/dist/core/engine-attestation.js +5 -1
  39. package/dist/core/engine-notice.d.ts +37 -2
  40. package/dist/core/engine-notice.js +3 -1
  41. package/dist/core/extension-requester.d.ts +9 -0
  42. package/dist/core/extension-requester.js +7 -0
  43. package/dist/core/file-history-store.d.ts +30 -2
  44. package/dist/core/file-history-store.js +48 -9
  45. package/dist/core/fs-write-gate-policy.js +1 -1
  46. package/dist/core/gate-fold.js +5 -1
  47. package/dist/core/gate-lanes.js +2 -0
  48. package/dist/core/gate-outcome.d.ts +39 -2
  49. package/dist/core/gate-outcome.js +20 -1
  50. package/dist/core/governance-codes.d.ts +8 -4
  51. package/dist/core/governance-codes.js +8 -3
  52. package/dist/core/leg-identity.d.ts +35 -1
  53. package/dist/core/leg-identity.js +10 -0
  54. package/dist/core/mailbox-store.d.ts +39 -4
  55. package/dist/core/mailbox-store.js +14 -2
  56. package/dist/core/memory-engine/consolidation.d.ts +13 -6
  57. package/dist/core/memory-engine/consolidation.js +26 -0
  58. package/dist/core/memory-engine/control-objects.d.ts +58 -0
  59. package/dist/core/memory-engine/control-objects.js +273 -0
  60. package/dist/core/memory-engine/control-plane-adoption.d.ts +51 -0
  61. package/dist/core/memory-engine/control-plane-adoption.js +161 -0
  62. package/dist/core/memory-engine/delegation-settlement.d.ts +73 -40
  63. package/dist/core/memory-engine/delegation-settlement.js +271 -107
  64. package/dist/core/memory-engine/dual-root.d.ts +3 -2
  65. package/dist/core/memory-engine/dual-root.js +7 -3
  66. package/dist/core/memory-engine/engine.d.ts +54 -33
  67. package/dist/core/memory-engine/engine.js +150 -157
  68. package/dist/core/memory-engine/file-backend.d.ts +58 -76
  69. package/dist/core/memory-engine/file-backend.js +111 -268
  70. package/dist/core/memory-engine/index.d.ts +3 -2
  71. package/dist/core/memory-engine/index.js +2 -1
  72. package/dist/core/memory-engine/layout.d.ts +135 -50
  73. package/dist/core/memory-engine/layout.js +214 -89
  74. package/dist/core/memory-engine/memory-age.d.ts +12 -0
  75. package/dist/core/memory-engine/memory-age.js +17 -0
  76. package/dist/core/memory-engine/memory-backend-contract.d.ts +10 -3
  77. package/dist/core/memory-engine/memory-backend-contract.js +159 -2
  78. package/dist/core/memory-engine/memory-deployment.d.ts +12 -4
  79. package/dist/core/memory-engine/memory-deployment.js +31 -6
  80. package/dist/core/memory-engine/origin-clearance.d.ts +12 -3
  81. package/dist/core/memory-engine/origin-clearance.js +64 -9
  82. package/dist/core/memory-engine/session-write-home.d.ts +34 -1
  83. package/dist/core/memory-engine/session-write-home.js +37 -10
  84. package/dist/core/memory-engine/tools.d.ts +2 -2
  85. package/dist/core/memory-engine/tools.js +7 -7
  86. package/dist/core/memory-engine/types.d.ts +55 -12
  87. package/dist/core/memory-engine/types.js +2 -1
  88. package/dist/core/memory-vector.d.ts +6 -14
  89. package/dist/core/memory.d.ts +0 -462
  90. package/dist/core/memory.js +0 -333
  91. package/dist/core/mode-policy.js +3 -3
  92. package/dist/core/mods/agent-stations.d.ts +18 -0
  93. package/dist/core/mods/agent-stations.js +8 -6
  94. package/dist/core/mods/extension-call.d.ts +4 -0
  95. package/dist/core/mods/extension-call.js +8 -0
  96. package/dist/core/mods/tool-call.js +3 -4
  97. package/dist/core/parent-spec-seat.d.ts +2 -2
  98. package/dist/core/peer-channel.d.ts +5 -0
  99. package/dist/core/permission-rule-consent.d.ts +64 -22
  100. package/dist/core/permission-rule-consent.js +106 -58
  101. package/dist/core/permission-rule-store.d.ts +56 -21
  102. package/dist/core/permission-rule-store.js +2 -0
  103. package/dist/core/refusal-echo.d.ts +20 -0
  104. package/dist/core/refusal-echo.js +33 -0
  105. package/dist/core/retention.d.ts +175 -10
  106. package/dist/core/retention.js +44 -0
  107. package/dist/core/retired-keys.d.ts +5 -0
  108. package/dist/core/retired-keys.js +3 -0
  109. package/dist/core/rule-offer-read.d.ts +0 -4
  110. package/dist/core/rule-offer-read.js +2 -18
  111. package/dist/core/runner/classifier-ladder.d.ts +10 -2
  112. package/dist/core/runner/classifier-ladder.js +4 -2
  113. package/dist/core/runner/compact-session.js +10 -7
  114. package/dist/core/runner/compaction-call-options.d.ts +7 -7
  115. package/dist/core/runner/compaction-call-options.js +3 -2
  116. package/dist/core/runner/contracts.d.ts +28 -9
  117. package/dist/core/runner/delegation-owner.d.ts +2 -3
  118. package/dist/core/runner/delegation-owner.js +2 -1
  119. package/dist/core/runner/gate-exit.d.ts +6 -0
  120. package/dist/core/runner/gate-exit.js +11 -2
  121. package/dist/core/runner/memory-consolidation-driver.d.ts +4 -2
  122. package/dist/core/runner/memory-consolidation-driver.js +8 -5
  123. package/dist/core/runner/prepare-ask-lane.js +3 -3
  124. package/dist/core/runner/prepare-caps-and-workflow.d.ts +3 -1
  125. package/dist/core/runner/prepare-caps-and-workflow.js +2 -2
  126. package/dist/core/runner/prepare-config-doors.js +4 -0
  127. package/dist/core/runner/prepare-delegation-surface.d.ts +3 -0
  128. package/dist/core/runner/prepare-delegation-surface.js +2 -2
  129. package/dist/core/runner/prepare-gate-stations.d.ts +8 -0
  130. package/dist/core/runner/prepare-gate-stations.js +15 -7
  131. package/dist/core/runner/prepare-hands-readface.js +1 -1
  132. package/dist/core/runner/prepare-inherited-gate.d.ts +3 -1
  133. package/dist/core/runner/prepare-inherited-gate.js +4 -2
  134. package/dist/core/runner/prepare-memory-engine-session.js +1 -1
  135. package/dist/core/runner/prepare-memory.d.ts +4 -0
  136. package/dist/core/runner/prepare-memory.js +22 -28
  137. package/dist/core/runner/prepare-policy-chain.js +18 -10
  138. package/dist/core/runner/prepare-prompt-inputs.js +1 -1
  139. package/dist/core/runner/prepare-task.d.ts +5 -1
  140. package/dist/core/runner/prepare-task.js +16 -11
  141. package/dist/core/runner/prepare-wiring-manifest.d.ts +3 -1
  142. package/dist/core/runner/prepare-wiring-manifest.js +3 -2
  143. package/dist/core/runner/resume-admission.d.ts +2 -2
  144. package/dist/core/runner/resume-admission.js +2 -23
  145. package/dist/core/runner/role-resolution-table.d.ts +98 -0
  146. package/dist/core/runner/role-resolution-table.js +62 -0
  147. package/dist/core/runner/run-compaction-machinery.js +2 -2
  148. package/dist/core/runner/run-identity-wiring.js +12 -9
  149. package/dist/core/runner/run-leg.js +8 -4
  150. package/dist/core/runner/run-notification-lane.js +23 -23
  151. package/dist/core/runner/run-recovery-lanes.js +3 -3
  152. package/dist/core/runner/run-terminal-adoption.js +1 -1
  153. package/dist/core/runner/run-turn-boundary.js +14 -15
  154. package/dist/core/runner/runtask.js +1 -1
  155. package/dist/core/runner/sub-agent-deny-lists.d.ts +19 -0
  156. package/dist/core/runner/sub-agent-deny-lists.js +25 -0
  157. package/dist/core/runner/tool-occurrence.d.ts +4 -2
  158. package/dist/core/runner/tool-occurrence.js +2 -2
  159. package/dist/core/runner-deps.d.ts +1 -1
  160. package/dist/core/secret-env.d.ts +2 -2
  161. package/dist/core/secret-env.js +6 -20
  162. package/dist/core/session-checkout.d.ts +63 -3
  163. package/dist/core/session-checkout.js +152 -58
  164. package/dist/core/session-store.d.ts +6 -6
  165. package/dist/core/session-store.js +3 -3
  166. package/dist/core/shell-read-silence.js +8 -0
  167. package/dist/core/store-contracts/background-agent-store-contract.js +13 -0
  168. package/dist/core/store-contracts/peer-directory-contract.js +8 -1
  169. package/dist/core/task-event.d.ts +9 -5
  170. package/dist/core/task-notification.d.ts +137 -31
  171. package/dist/core/task-notification.js +198 -100
  172. package/dist/core/task-registry-agent.d.ts +11 -0
  173. package/dist/core/task-registry-agent.js +12 -3
  174. package/dist/core/task-registry-shared.d.ts +11 -0
  175. package/dist/core/task-registry.d.ts +18 -0
  176. package/dist/core/task-registry.js +27 -8
  177. package/dist/core/task-spec.d.ts +7 -2
  178. package/dist/core/tool-errors.d.ts +17 -9
  179. package/dist/core/tool-errors.js +1 -1
  180. package/dist/core/tool-policy.d.ts +18 -1
  181. package/dist/core/tool-policy.js +16 -7
  182. package/dist/core/tool-result-store.d.ts +20 -0
  183. package/dist/core/tool-spec.d.ts +10 -6
  184. package/dist/core/trace.d.ts +3 -2
  185. package/dist/core/workflow-journal-store.d.ts +11 -0
  186. package/dist/core/workflow-journal-store.js +7 -0
  187. package/dist/core/workflow-run-store.d.ts +9 -0
  188. package/dist/core/workflow-run-store.js +6 -0
  189. package/dist/core/write-protect.d.ts +16 -0
  190. package/dist/core/write-protect.js +10 -0
  191. package/dist/engine/compaction/compaction.d.ts +2 -1
  192. package/dist/engine/harness/types.d.ts +1 -0
  193. package/dist/engine/session/repo-utils.d.ts +14 -4
  194. package/dist/engine/session/repo-utils.js +5 -4
  195. package/dist/engine/session/session.d.ts +7 -1
  196. package/dist/engine/session/session.js +5 -2
  197. package/dist/index.d.ts +13 -11
  198. package/dist/index.js +13 -11
  199. package/dist/orchestration/run-workflow-tool.d.ts +4 -0
  200. package/dist/orchestration/run-workflow-tool.js +7 -0
  201. package/dist/orchestration/workflow-types.d.ts +2 -1
  202. package/dist/orchestration/workflow.d.ts +3 -0
  203. package/dist/orchestration/workflow.js +2 -1
  204. package/dist/prompts/default.d.ts +1 -2
  205. package/dist/scenarios/full-body.d.ts +1 -1
  206. package/dist/stores/file/adoption/adopt.d.ts +1 -1
  207. package/dist/stores/file/checkpoint-store.d.ts +6 -0
  208. package/dist/stores/file/checkpoint-store.js +4 -1
  209. package/dist/stores/file/file-history-store.d.ts +13 -0
  210. package/dist/stores/file/file-history-store.js +75 -13
  211. package/dist/stores/file/index.d.ts +12 -16
  212. package/dist/stores/file/index.js +7 -25
  213. package/dist/stores/file/permission-rule-store.d.ts +34 -0
  214. package/dist/stores/file/permission-rule-store.js +104 -9
  215. package/dist/stores/file/session-store.js +1 -1
  216. package/dist/stores/file/tool-result-store.d.ts +14 -0
  217. package/dist/stores/file/tool-result-store.js +39 -2
  218. package/dist/stores/file/workflow-journal-store.d.ts +4 -0
  219. package/dist/stores/file/workflow-journal-store.js +17 -0
  220. package/dist/stores/file/workflow-run-store.d.ts +3 -0
  221. package/dist/stores/file/workflow-run-store.js +15 -0
  222. package/dist/tools/fs/read-deny.d.ts +15 -2
  223. package/dist/tools/fs/read-deny.js +10 -2
  224. package/dist/tools/fs/shell-read-check.d.ts +8 -2
  225. package/dist/tools/fs/shell-read-check.js +7 -7
  226. package/package.json +2 -2
  227. package/test/export-surface.snapshot.json +71 -73
  228. package/wire-changes/core-stores-sql@7.64.0.json +30 -0
  229. package/wire-changes/core-stores-sql@7.65.0.json +30 -0
  230. package/wire-changes/core@7.64.0.json +2653 -0
  231. package/wire-changes/core@7.65.0.json +1650 -0
  232. package/wire-changes/mods-kernel@7.64.0.json +30 -0
  233. package/wire-changes/mods-kernel@7.65.0.json +30 -0
  234. package/wire-changes/primitives@7.64.0.json +30 -0
  235. package/wire-changes/primitives@7.65.0.json +337 -0
  236. package/dist/core/memory-recall.d.ts +0 -208
  237. package/dist/core/memory-recall.js +0 -136
  238. package/dist/stores/file/memory-store.d.ts +0 -124
  239. package/dist/stores/file/memory-store.js +0 -353
@@ -1,8 +1,9 @@
1
1
  import { createHash } from "node:crypto";
2
- import { closeSync, existsSync, fsyncSync, linkSync, lstatSync, openSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
3
- import { basename, dirname, join } from "node:path";
4
- import { QUARANTINE_DIR, appendChallengeEvents, claimStaging, ensureDirExists, fileGovernanceDocOf, isSameFile, lockedStrictUpdate, mintStagingName, readStrictSidecar, stagingNamePattern, v1MemoryPathKeyResolver, lineageContributionsOfSession, markSessionPolluted, readProjectionDebts, readSessionPollution, ControlPlaneCorruptError } from "./layout.js";
2
+ import { existsSync, linkSync, lstatSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
3
+ import { dirname, join } from "node:path";
4
+ import { appendChallengeEvents, claimStaging, fileGovernanceDocOf, isSameFile, lockedStrictUpdate, mintStagingName, readStrictSidecar, stagingNamePattern, v1MemoryPathKeyResolver, lineageContributionsOfSession, markSessionPolluted, readProjectionDebts, readSessionPollution, putControlObjectOnce, ControlPlaneCorruptError } from "./layout.js";
5
5
  import { thrownText } from "../thrown-value.js";
6
+ import { fileControlObjectDirOf, fileControlObjectFaultCode } from "./control-objects.js";
6
7
  function reqStr(v) {
7
8
  return typeof v === "string" && v.length > 0;
8
9
  }
@@ -189,7 +190,7 @@ export async function expireOverdueSettlements(docs, input) {
189
190
  await settleDelegation(docs, { settleId: s.row.settleId, status: "expired", now: input.now });
190
191
  return overdue.map((s) => s.row.settleId);
191
192
  }
192
- export async function replayExternalSettlementEffects(docs, controlDir, input) {
193
+ export async function replayExternalSettlementEffects(docs, input) {
193
194
  const applied = [];
194
195
  const folded = (await effectiveSettlements(docs)).filter((s) => s.effective === "external" && s.terminalEventId !== undefined);
195
196
  if (folded.length === 0)
@@ -199,7 +200,7 @@ export async function replayExternalSettlementEffects(docs, controlDir, input) {
199
200
  if (rec.effects[s.row.settleId] === s.terminalEventId)
200
201
  continue;
201
202
  const sessionId = s.row.sessionId;
202
- const outcome = markSessionPolluted(controlDir, sessionId, "a delegated child of this session settled external after launch (delegation settlement account)", input.now, "observed");
203
+ const outcome = await markSessionPolluted(docs, sessionId, "a delegated child of this session settled external after launch (delegation settlement account)", input.now, "observed");
203
204
  if (outcome === "unpersisted")
204
205
  continue;
205
206
  try {
@@ -298,7 +299,11 @@ function convertSessionAccounts(read, controlDir) {
298
299
  }
299
300
  export function convertV1SidecarKeys(controlDir) {
300
301
  readProjectionDebts(controlDir);
301
- readHolds(controlDir);
302
+ const holds = readStrictSidecar(controlDir, HOLDS_FILE, HOLDS_WHAT);
303
+ if (holdsOnDiskAreV1(holds))
304
+ lockedStrictUpdate(controlDir, HOLDS_FILE, HOLDS_WHAT, coerceHolds, (again) => ({ next: again, result: undefined }));
305
+ else
306
+ coerceHolds(holds);
302
307
  const accounts = fileGovernanceDocOf("session-accounts");
303
308
  if (coerceSessionAccounts(readStrictSidecar(controlDir, accounts.file, accounts.what)).version !== 2) {
304
309
  lockedStrictUpdate(controlDir, accounts.file, accounts.what, coerceSessionAccounts, (again) => {
@@ -428,8 +433,9 @@ export async function resolveSessionAccountRecord(docs, controlDir, input) {
428
433
  return { next: rec, result: undefined };
429
434
  });
430
435
  }
431
- export const HOLDS_FILE = "holds.json";
432
- export const HOLD_DIR = "hold";
436
+ export const HOLDS_FILE = fileGovernanceDocOf("holds").file;
437
+ const HOLDS_WHAT = fileGovernanceDocOf("holds").what;
438
+ export const HOLD_DIR = fileControlObjectDirOf("hold");
433
439
  const HOLD_STATUSES = new Set(["capturing", "held", "released", "disposed"]);
434
440
  const HOLD_TERMINALS = new Set(["dirty", "expired", "conflict", "discarded", "capture_lost"]);
435
441
  function coerceHolds(raw) {
@@ -482,11 +488,11 @@ function coerceHolds(raw) {
482
488
  function holdsOnDiskAreV1(raw) {
483
489
  return typeof raw === "object" && raw !== null && raw.version === 1;
484
490
  }
485
- export function readHolds(controlDir) {
486
- const raw = readStrictSidecar(controlDir, HOLDS_FILE, "memory instruction-hold ledger");
491
+ export async function readHolds(docs) {
492
+ const raw = await docs.readDoc("holds");
487
493
  if (!holdsOnDiskAreV1(raw))
488
494
  return coerceHolds(raw).rows;
489
- return lockedStrictUpdate(controlDir, HOLDS_FILE, "memory instruction-hold ledger", coerceHolds, (again) => ({ next: again, result: again.rows }));
495
+ return docs.updateDoc("holds", coerceHolds, (again) => ({ next: again, result: again.rows }));
490
496
  }
491
497
  function sha256(buf) {
492
498
  return createHash("sha256").update(buf).digest("hex");
@@ -501,41 +507,56 @@ function dropStaging(claimedPath) {
501
507
  export function stagingPathBeside(absPath, stagingName) {
502
508
  return join(dirname(absPath), stagingName);
503
509
  }
504
- function writeCustodyVerified(dest, bytes) {
510
+ async function heldBytesAt(objects, kind, name, digest) {
511
+ let bytes;
505
512
  try {
506
- const fd = openSync(dest, "wx", 0o600);
507
- try {
508
- writeFileSync(fd, bytes);
509
- fsyncSync(fd);
510
- }
511
- finally {
512
- closeSync(fd);
513
- }
514
- return sha256(readFileSync(dest)) === sha256(bytes);
513
+ bytes = await objects.getObject(kind, name);
514
+ }
515
+ catch {
516
+ return "other";
517
+ }
518
+ if (bytes === undefined)
519
+ return "absent";
520
+ return sha256(Buffer.from(bytes)) === digest ? "holds" : "other";
521
+ }
522
+ async function holdCustodyVerified(objects, captureName, bytes) {
523
+ try {
524
+ await putControlObjectOnce(objects, "hold", captureName, bytes);
515
525
  }
516
526
  catch {
517
527
  return false;
518
528
  }
529
+ return (await heldBytesAt(objects, "hold", captureName, sha256(bytes))) === "holds";
519
530
  }
520
- export function retireStagingToCustody(claimedPath, custodyDir, stagingName) {
531
+ export async function retireStagingToObject(objects, claimedPath, kind, name, refusedAt) {
521
532
  const unsettled = (why) => ({ settled: false, at: `${claimedPath} (${why})` });
522
533
  let bytes;
523
534
  try {
524
535
  bytes = readFileSync(claimedPath);
525
- ensureDirExists(custodyDir);
526
536
  }
527
537
  catch {
528
538
  return unsettled("the staged bytes could not be read into the control plane");
529
539
  }
530
- if (!writeCustodyVerified(join(custodyDir, stagingName), bytes))
540
+ try {
541
+ if ((await putControlObjectOnce(objects, kind, name, bytes)) !== "created")
542
+ return unsettled(`a control-plane ${kind} object named ${name} already stands`);
543
+ const back = await objects.getObject(kind, name);
544
+ if (back === undefined || sha256(Buffer.from(back)) !== sha256(bytes))
545
+ return unsettled("the control-plane custody copy could not be read back");
546
+ }
547
+ catch {
531
548
  return unsettled("the control-plane custody copy could not be written");
549
+ }
550
+ const refused = refusedAt();
551
+ if (refused !== undefined)
552
+ return unsettled(`a copy reached the control plane; this one was not removed: ${refused}`);
532
553
  try {
533
554
  unlinkSync(claimedPath);
534
555
  }
535
556
  catch {
536
557
  return unsettled("a copy reached the control plane; this one could not be removed");
537
558
  }
538
- return { settled: true, at: `${basename(custodyDir)}/${stagingName} in the memory control plane` };
559
+ return { settled: true, at: `${kind} object ${name} in the memory control plane` };
539
560
  }
540
561
  export function noReplaceRestore(stagingPath, destPath) {
541
562
  try {
@@ -565,24 +586,23 @@ export function noReplaceRestore(stagingPath, destPath) {
565
586
  }
566
587
  return "restored";
567
588
  }
568
- export function openInstructionHold(controlDir, input) {
589
+ export async function openInstructionHold(store, input) {
569
590
  const holdId = `hold-${input.now()}-${createHash("sha256").update(`${input.sessionId}\n${input.rel}\n${input.content}`).digest("hex").slice(0, 12)}`;
570
591
  const buf = Buffer.from(input.content, "utf8");
571
592
  const digest = sha256(buf);
572
593
  const captureName = `${holdId}.md`;
573
- const holdDir = join(controlDir, HOLD_DIR);
574
594
  let thirdWriterStrandedAt;
575
- const refuse = (rawReason, terminal) => {
595
+ const refuse = async (rawReason, terminal) => {
576
596
  const reason = thirdWriterStrandedAt === undefined ? rawReason : `${rawReason}; a concurrent writer's bytes are preserved at ${thirdWriterStrandedAt}`;
577
597
  try {
578
- disposeHold(controlDir, { holdId, terminal, now: input.now });
598
+ await disposeHold(store, { holdId, terminal });
579
599
  }
580
600
  catch {
581
601
  }
582
602
  return { ok: false, holdId, reason };
583
603
  };
584
604
  try {
585
- lockedStrictUpdate(controlDir, HOLDS_FILE, "memory instruction-hold ledger", coerceHolds, (rec) => {
605
+ await store.updateDoc("holds", coerceHolds, (rec) => {
586
606
  if (rec.rows.some((r) => r.holdId === holdId))
587
607
  return { result: undefined };
588
608
  rec.rows.push({
@@ -606,15 +626,12 @@ export function openInstructionHold(controlDir, input) {
606
626
  catch (err) {
607
627
  return { ok: false, reason: `hold ledger refused: ${thrownText(err)}` };
608
628
  }
609
- try {
610
- ensureDirExists(holdDir);
611
- }
612
- catch (err) {
613
- return refuse(`hold capture failed: ${thrownText(err)}`, "capture_lost");
614
- }
615
- if (!writeCustodyVerified(join(holdDir, captureName), buf)) {
629
+ if (!(await holdCustodyVerified(store, captureName, buf))) {
616
630
  return refuse("hold capture verification failed (the custody copy could not be written, made durable, or hashed back to the captured digest)", "capture_lost");
617
631
  }
632
+ const refused = input.actRefused(input.absPath, "holding an instruction-form file");
633
+ if (refused !== undefined)
634
+ return refuse(`hold removal refused: ${refused}`, "discarded");
618
635
  const stagingName = mintStagingName(holdId, input.now);
619
636
  const stagingPath = stagingPathBeside(input.absPath, stagingName);
620
637
  try {
@@ -637,14 +654,14 @@ export function openInstructionHold(controlDir, input) {
637
654
  else {
638
655
  const restore = noReplaceRestore(claimedPath, input.absPath);
639
656
  if (restore !== "restored") {
640
- const retired = retireStagingToCustody(claimedPath, holdDir, stagingName);
657
+ const retired = await retireStagingToObject(store, claimedPath, "hold", stagingName, () => input.actRefused(claimedPath, "removing a hold's staged copy"));
641
658
  if (restore === "stranded" || !retired.settled)
642
659
  thirdWriterStrandedAt = retired.at;
643
660
  }
644
661
  }
645
662
  }
646
663
  try {
647
- lockedStrictUpdate(controlDir, HOLDS_FILE, "memory instruction-hold ledger", coerceHolds, (rec) => {
664
+ await store.updateDoc("holds", coerceHolds, (rec) => {
648
665
  const row = rec.rows.find((r) => r.holdId === holdId);
649
666
  if (row === undefined || row.status !== "capturing")
650
667
  return { result: undefined };
@@ -657,12 +674,14 @@ export function openInstructionHold(controlDir, input) {
657
674
  }
658
675
  return { ok: true, holdId, ...(thirdWriterStrandedAt !== undefined ? { thirdWriterStrandedAt } : {}) };
659
676
  }
660
- export function reconcileHolds(controlDir, input) {
661
- const holdDir = join(controlDir, HOLD_DIR);
677
+ export async function reconcileHolds(store, input) {
662
678
  const discloseStranded = (holdId, scope, rel, at) => {
663
679
  input.onStranded(scope, `instruction hold ${holdId}: a concurrent writer's bytes for ${JSON.stringify(rel)} could not be restored onto the plane — they are preserved at ${at} for host recovery`);
664
680
  };
665
- const finishRemoval = (row, plane) => {
681
+ const finishRemoval = async (row, plane) => {
682
+ const refused = input.actRefused(plane, "removing a held file from the plane");
683
+ if (refused !== undefined)
684
+ throw new Error(refused);
666
685
  const stagingName = mintStagingName(row.holdId, input.now);
667
686
  renameSync(plane, stagingPathBeside(plane, stagingName));
668
687
  const staging = claimStaging(stagingPathBeside(plane, stagingName));
@@ -673,7 +692,7 @@ export function reconcileHolds(controlDir, input) {
673
692
  const restore = noReplaceRestore(staging, plane);
674
693
  if (restore === "restored")
675
694
  return;
676
- const retired = retireStagingToCustody(staging, holdDir, stagingName);
695
+ const retired = await retireStagingToObject(store, staging, "hold", stagingName, () => input.actRefused(staging, "removing a hold's staged copy"));
677
696
  if (restore === "stranded" || !retired.settled)
678
697
  discloseStranded(row.holdId, row.scope, row.rel, retired.at);
679
698
  };
@@ -692,7 +711,7 @@ export function reconcileHolds(controlDir, input) {
692
711
  seatListing.set(seatDir, names);
693
712
  return names;
694
713
  };
695
- const sweepStagingGenerations = (row, plane, custody) => {
714
+ const sweepStagingGenerations = async (row, plane, custody) => {
696
715
  const seatDir = dirname(plane);
697
716
  const pattern = stagingNamePattern(row.holdId);
698
717
  let seat;
@@ -705,6 +724,8 @@ export function reconcileHolds(controlDir, input) {
705
724
  for (const name of listSeat(seatDir)) {
706
725
  if (!pattern.test(name))
707
726
  continue;
727
+ if (input.actRefused(join(seatDir, name), "recovering a hold's staged copy") !== undefined)
728
+ continue;
708
729
  const staged = claimStaging(join(seatDir, name));
709
730
  let bytes;
710
731
  try {
@@ -716,47 +737,44 @@ export function reconcileHolds(controlDir, input) {
716
737
  continue;
717
738
  }
718
739
  if (sha256(bytes) === row.contentDigest) {
719
- if (custody === "holds" || (custody === "absent" && writeCustodyVerified(join(holdDir, row.captureName), bytes))) {
740
+ if (custody === "holds") {
720
741
  dropStaging(staged);
721
742
  continue;
722
743
  }
744
+ if (custody === "absent" && (await holdCustodyVerified(store, row.captureName, bytes))) {
745
+ if (input.actRefused(staged, "recovering a hold's staged copy") === undefined)
746
+ dropStaging(staged);
747
+ continue;
748
+ }
723
749
  }
724
750
  const onSeat = seat !== undefined && isSameFile(staged, seat);
725
- const retired = retireStagingToCustody(staged, holdDir, name);
751
+ const retired = await retireStagingToObject(store, staged, "hold", name, () => input.actRefused(staged, "removing a hold's staged copy"));
726
752
  if (!(onSeat && retired.settled && isSameFile(plane, seat)))
727
753
  discloseStranded(row.holdId, row.scope, row.rel, retired.at);
728
754
  }
729
755
  };
730
- const rows = readHolds(controlDir).filter((r) => r.status === "capturing");
756
+ const ledger = await readHolds(store);
757
+ const rows = ledger.filter((r) => r.status === "capturing");
731
758
  for (const row of rows) {
732
- const custody = join(holdDir, row.captureName);
733
- let custodyBytes;
759
+ let custodyState = await heldBytesAt(store, "hold", row.captureName, row.contentDigest);
734
760
  try {
735
- custodyBytes = readFileSync(custody);
736
- }
737
- catch {
738
- custodyBytes = undefined;
739
- }
740
- const custodyState = custodyBytes === undefined ? "absent" : sha256(custodyBytes) === row.contentDigest ? "holds" : "mismatch";
741
- try {
742
- sweepStagingGenerations(row, join(input.homeOf(row.scope), row.rel), custodyState);
761
+ await sweepStagingGenerations(row, join(input.homeOf(row.scope), row.rel), custodyState);
743
762
  }
744
763
  catch {
745
764
  }
765
+ if (custodyState === "absent")
766
+ custodyState = await heldBytesAt(store, "hold", row.captureName, row.contentDigest);
746
767
  let terminalLost = false;
747
- if (existsSync(custody)) {
748
- const bytes = custodyBytes ?? readFileSync(custody);
749
- if (sha256(bytes) !== row.contentDigest) {
750
- terminalLost = true;
751
- }
752
- else {
753
- const plane = join(input.homeOf(row.scope), row.rel);
754
- if (existsSync(plane)) {
755
- try {
756
- finishRemoval(row, plane);
757
- }
758
- catch {
759
- }
768
+ if (custodyState === "other") {
769
+ terminalLost = true;
770
+ }
771
+ else if (custodyState === "holds") {
772
+ const plane = join(input.homeOf(row.scope), row.rel);
773
+ if (existsSync(plane)) {
774
+ try {
775
+ await finishRemoval(row, plane);
776
+ }
777
+ catch {
760
778
  }
761
779
  }
762
780
  }
@@ -766,10 +784,9 @@ export function reconcileHolds(controlDir, input) {
766
784
  const bytes = readFileSync(plane);
767
785
  if (sha256(bytes) === row.contentDigest) {
768
786
  try {
769
- ensureDirExists(holdDir);
770
- if (!writeCustodyVerified(custody, bytes))
771
- throw new Error("the custody copy could not be written or made durable");
772
- finishRemoval(row, plane);
787
+ if (!(await holdCustodyVerified(store, row.captureName, bytes)))
788
+ throw new Error("the custody copy could not be written or read back");
789
+ await finishRemoval(row, plane);
773
790
  }
774
791
  catch {
775
792
  terminalLost = true;
@@ -783,7 +800,7 @@ export function reconcileHolds(controlDir, input) {
783
800
  terminalLost = true;
784
801
  }
785
802
  }
786
- lockedStrictUpdate(controlDir, HOLDS_FILE, "memory instruction-hold ledger", coerceHolds, (rec) => {
803
+ await store.updateDoc("holds", coerceHolds, (rec) => {
787
804
  const r = rec.rows.find((x) => x.holdId === row.holdId);
788
805
  if (r === undefined || r.status !== "capturing")
789
806
  return { result: undefined };
@@ -797,36 +814,183 @@ export function reconcileHolds(controlDir, input) {
797
814
  return { next: rec, result: undefined };
798
815
  });
799
816
  }
817
+ let custodyNames;
818
+ try {
819
+ custodyNames = new Set(await store.listObjects("hold"));
820
+ }
821
+ catch {
822
+ custodyNames = new Set();
823
+ }
824
+ for (const row of ledger) {
825
+ const quarantineName = row.disposition?.quarantineName;
826
+ if (row.status !== "disposed" || quarantineName === undefined || !custodyNames.has(row.captureName))
827
+ continue;
828
+ try {
829
+ const moved = await moveCustodyToSeat(store, row, quarantineName);
830
+ if (moved.at === "custody") {
831
+ input.onStranded(row.scope, `instruction hold ${row.holdId} (disposed, ${row.disposition.terminal}): its custody bytes could not be moved to the quarantine seat ${quarantineName} (${moved.code}) — they stay at the custody seat (hold object ${row.captureName} in the memory control plane) for host recovery, and the move is retried at every harvest`);
832
+ }
833
+ }
834
+ catch (err) {
835
+ input.onStranded(row.scope, `instruction hold ${row.holdId} (disposed, ${row.disposition.terminal}): ${thrownText(err)}`);
836
+ }
837
+ }
838
+ await adoptOrphanedQuarantineBytes(store, ledger);
800
839
  }
801
- export function disposeHold(controlDir, input) {
802
- const rows = readHolds(controlDir);
803
- const row = rows.find((r) => r.holdId === input.holdId);
804
- if (row === undefined || row.status === "disposed" || row.status === "released")
805
- return { ok: false };
806
- const custody = join(controlDir, HOLD_DIR, row.captureName);
807
- const quarantineName = `${input.now()}-hold-${row.captureName}`;
808
- const qDir = join(controlDir, QUARANTINE_DIR);
809
- let moved = false;
840
+ async function adoptOrphanedQuarantineBytes(store, snapshot) {
841
+ let listing;
810
842
  try {
811
- ensureDirExists(qDir);
812
- renameSync(custody, join(qDir, quarantineName));
813
- moved = true;
843
+ listing = (await store.listObjects("quarantine")).filter((n) => n.includes("-hold-") && safeBasename(n));
814
844
  }
815
845
  catch {
816
- moved = existsSync(join(qDir, quarantineName));
846
+ return;
817
847
  }
818
- lockedStrictUpdate(controlDir, HOLDS_FILE, "memory instruction-hold ledger", coerceHolds, (rec) => {
848
+ const seats = new Set();
849
+ for (const r of snapshot)
850
+ if (r.disposition?.quarantineName !== undefined)
851
+ seats.add(r.disposition.quarantineName);
852
+ if (listing.every((n) => seats.has(n)))
853
+ return;
854
+ const standsAt = async (kind, name) => {
855
+ try {
856
+ return (await store.getObject(kind, name)) !== undefined;
857
+ }
858
+ catch {
859
+ return true;
860
+ }
861
+ };
862
+ const digests = new Map();
863
+ for (const n of listing) {
864
+ if (seats.has(n))
865
+ continue;
866
+ try {
867
+ const bytes = await store.getObject("quarantine", n);
868
+ digests.set(n, bytes === undefined ? undefined : sha256(Buffer.from(bytes)));
869
+ }
870
+ catch {
871
+ digests.set(n, undefined);
872
+ }
873
+ }
874
+ const custodyStands = new Map();
875
+ const seatStands = new Map();
876
+ for (const r of snapshot) {
877
+ custodyStands.set(r.holdId, await standsAt("hold", r.captureName));
878
+ const seat = r.disposition?.quarantineName;
879
+ if (seat !== undefined && !seatStands.has(seat))
880
+ seatStands.set(seat, await standsAt("quarantine", seat));
881
+ }
882
+ const moves = await store.updateDoc("holds", coerceHolds, (rec) => {
883
+ const claimed = new Set();
884
+ for (const r of rec.rows)
885
+ if (r.disposition?.quarantineName !== undefined)
886
+ claimed.add(r.disposition.quarantineName);
887
+ const digestOf = (name) => digests.get(name);
888
+ const wants = (r) => {
889
+ const custody = custodyStands.get(r.holdId);
890
+ if (custody !== false)
891
+ return false;
892
+ if (r.status === "held")
893
+ return true;
894
+ if (r.status !== "disposed")
895
+ return false;
896
+ const seat = r.disposition?.quarantineName;
897
+ return seat === undefined || seatStands.get(seat) === false;
898
+ };
899
+ const endsWithCapture = (name, r) => name.endsWith(`-hold-${r.captureName}`);
900
+ const reservedForOther = (name) => rec.rows.some((r) => endsWithCapture(name, r));
901
+ const wanting = rec.rows.filter(wants);
902
+ const found = new Map();
903
+ for (const r of wanting) {
904
+ const f = listing.find((n) => !claimed.has(n) && endsWithCapture(n, r) && digestOf(n) === r.contentDigest);
905
+ if (f !== undefined) {
906
+ found.set(r, f);
907
+ claimed.add(f);
908
+ }
909
+ }
910
+ for (const r of wanting) {
911
+ if (found.has(r))
912
+ continue;
913
+ const f = listing.find((n) => !claimed.has(n) && !reservedForOther(n) && digestOf(n) === r.contentDigest);
914
+ if (f !== undefined) {
915
+ found.set(r, f);
916
+ claimed.add(f);
917
+ }
918
+ }
919
+ let changed = false;
920
+ const back = [];
921
+ for (const [r, f] of found) {
922
+ if (r.status === "disposed") {
923
+ r.disposition = { ...r.disposition, quarantineName: f };
924
+ changed = true;
925
+ continue;
926
+ }
927
+ back.push({ file: f, captureName: r.captureName });
928
+ }
929
+ return changed ? { next: rec, result: back } : { result: back };
930
+ });
931
+ for (const m of moves) {
932
+ try {
933
+ await store.moveObject({ kind: "quarantine", name: m.file }, { kind: "hold", name: m.captureName });
934
+ }
935
+ catch {
936
+ }
937
+ }
938
+ }
939
+ export function holdQuarantineNameOf(row) {
940
+ return `${row.contentDigest.slice(0, 16)}-hold-${row.captureName}`;
941
+ }
942
+ function foreignSeatRefusal(holdId, seatName) {
943
+ return new ControlPlaneCorruptError(`memory instruction hold ${holdId}: the quarantine seat ${seatName} is occupied by something that is not the hold's captured bytes — refusing to replace or adopt it (fail-closed; inspect or remove it, and the next harvest converges)`);
944
+ }
945
+ async function moveCustodyToSeat(objects, row, seatName) {
946
+ if ((await heldBytesAt(objects, "quarantine", seatName, row.contentDigest)) === "other")
947
+ throw foreignSeatRefusal(row.holdId, seatName);
948
+ try {
949
+ await objects.moveObject({ kind: "hold", name: row.captureName }, { kind: "quarantine", name: seatName });
950
+ return { at: "seat" };
951
+ }
952
+ catch (err) {
953
+ const seat = await heldBytesAt(objects, "quarantine", seatName, row.contentDigest);
954
+ if (seat === "other")
955
+ throw foreignSeatRefusal(row.holdId, seatName);
956
+ const custody = await heldBytesAt(objects, "hold", row.captureName, row.contentDigest);
957
+ if (custody === "absent")
958
+ return seat === "holds" ? { at: "seat" } : { at: "none" };
959
+ if (seat === "holds" && custody === "other")
960
+ return { at: "seat" };
961
+ return { at: "custody", code: fileControlObjectFaultCode(err) ?? "move refused" };
962
+ }
963
+ }
964
+ export async function disposeHold(store, input) {
965
+ const row = (await readHolds(store)).find((r) => r.holdId === input.holdId);
966
+ if (row === undefined || row.status === "disposed" || row.status === "released")
967
+ return { ok: false };
968
+ const quarantineName = holdQuarantineNameOf(row);
969
+ if (!safeBasename(quarantineName)) {
970
+ throw new ControlPlaneCorruptError(`memory instruction hold ${row.holdId}: its digest and capture name do not make a safe quarantine file name — refusing (fail-closed)`);
971
+ }
972
+ const custodyHere = (await heldBytesAt(store, "hold", row.captureName, row.contentDigest)) !== "absent";
973
+ const seatState = await heldBytesAt(store, "quarantine", quarantineName, row.contentDigest);
974
+ const verdict = await store.updateDoc("holds", coerceHolds, (rec) => {
819
975
  const r = rec.rows.find((x) => x.holdId === input.holdId);
820
976
  if (r === undefined || r.status === "disposed" || r.status === "released")
821
977
  return { result: undefined };
978
+ const named = seatState === "holds" || (custodyHere && seatState === "absent");
822
979
  r.status = "disposed";
823
- r.disposition = { terminal: input.terminal, ...(moved ? { quarantineName } : {}) };
824
- return { next: rec, result: undefined };
980
+ r.disposition = { terminal: input.terminal, ...(named ? { quarantineName } : {}) };
981
+ return { next: rec, result: { named, refused: custodyHere && seatState === "other" } };
825
982
  });
826
- return { ok: true, ...(moved ? { quarantineName } : {}) };
983
+ if (verdict === undefined)
984
+ return { ok: false };
985
+ if (verdict.refused)
986
+ throw foreignSeatRefusal(row.holdId, quarantineName);
987
+ if (!verdict.named)
988
+ return { ok: true };
989
+ const moved = await moveCustodyToSeat(store, row, quarantineName);
990
+ return { ok: true, ...(moved.at === "seat" ? { quarantineName } : {}) };
827
991
  }
828
- export function markHoldReleased(controlDir, input) {
829
- const captureName = lockedStrictUpdate(controlDir, HOLDS_FILE, "memory instruction-hold ledger", coerceHolds, (rec) => {
992
+ export async function markHoldReleased(store, input) {
993
+ const captureName = await store.updateDoc("holds", coerceHolds, (rec) => {
830
994
  const r = rec.rows.find((x) => x.holdId === input.holdId);
831
995
  if (r === undefined || (r.status !== "held" && !(r.status === "disposed" && r.disposition?.terminal === "expired")))
832
996
  return { result: undefined };
@@ -838,12 +1002,12 @@ export function markHoldReleased(controlDir, input) {
838
1002
  if (captureName === undefined)
839
1003
  return;
840
1004
  try {
841
- unlinkSync(join(controlDir, HOLD_DIR, captureName));
1005
+ await store.deleteObject("hold", captureName);
842
1006
  }
843
1007
  catch {
844
1008
  }
845
1009
  }
846
- export function resolveHoldRecord(controlDir, input) {
1010
+ export async function resolveHoldRecord(docs, input) {
847
1011
  if (typeof input.requestId !== "string" || input.requestId.length === 0) {
848
1012
  const e = new Error("resolveHold requires a non-empty requestId (the audit attribution of who resolved) — refused, never defaulted.");
849
1013
  e.code = "memory.hold_resolve_unattributed";
@@ -854,7 +1018,7 @@ export function resolveHoldRecord(controlDir, input) {
854
1018
  e.code = "memory.hold_resolve_invalid";
855
1019
  throw e;
856
1020
  }
857
- lockedStrictUpdate(controlDir, HOLDS_FILE, "memory instruction-hold ledger", coerceHolds, (rec) => {
1021
+ await docs.updateDoc("holds", coerceHolds, (rec) => {
858
1022
  const r = rec.rows.find((x) => x.holdId === input.holdId);
859
1023
  if (r === undefined) {
860
1024
  const e = new Error(`resolveHold: unknown holdId ${JSON.stringify(input.holdId)}`);
@@ -877,8 +1041,8 @@ export function resolveHoldRecord(controlDir, input) {
877
1041
  return { next: rec, result: undefined };
878
1042
  });
879
1043
  }
880
- export function clearHoldResolution(controlDir, input) {
881
- lockedStrictUpdate(controlDir, HOLDS_FILE, "memory instruction-hold ledger", coerceHolds, (rec) => {
1044
+ export async function clearHoldResolution(docs, input) {
1045
+ await docs.updateDoc("holds", coerceHolds, (rec) => {
882
1046
  const r = rec.rows.find((x) => x.holdId === input.holdId);
883
1047
  if (r === undefined || r.resolved === undefined || r.resolvedAt !== input.ifResolvedAt)
884
1048
  return { result: undefined };
@@ -887,21 +1051,21 @@ export function clearHoldResolution(controlDir, input) {
887
1051
  return { next: rec, result: undefined };
888
1052
  });
889
1053
  }
890
- export function readHoldCustody(controlDir, row) {
891
- const seats = [join(controlDir, HOLD_DIR, row.captureName), ...(row.disposition?.quarantineName !== undefined ? [join(controlDir, QUARANTINE_DIR, row.disposition.quarantineName)] : [])];
892
- for (const seat of seats) {
1054
+ export async function readHoldCustody(objects, row) {
1055
+ const seats = [["hold", row.captureName], ...(row.disposition?.quarantineName !== undefined ? [["quarantine", row.disposition.quarantineName]] : [])];
1056
+ for (const [kind, name] of seats) {
893
1057
  try {
894
- const bytes = readFileSync(seat);
895
- if (sha256(bytes) === row.contentDigest)
896
- return bytes.toString("utf8");
1058
+ const bytes = await objects.getObject(kind, name);
1059
+ if (bytes !== undefined && sha256(Buffer.from(bytes)) === row.contentDigest)
1060
+ return Buffer.from(bytes).toString("utf8");
897
1061
  }
898
1062
  catch {
899
1063
  }
900
1064
  }
901
1065
  return undefined;
902
1066
  }
903
- export async function sessionBlocksCleanRelease(docs, controlDir, sessionId) {
904
- if (readSessionPollution(controlDir, sessionId) !== undefined)
1067
+ export async function sessionBlocksCleanRelease(docs, sessionId) {
1068
+ if ((await readSessionPollution(docs, sessionId)) !== undefined)
905
1069
  return true;
906
1070
  return (await sessionSettlements(docs, sessionId)).some((s) => s.effective !== "clean" && s.effective !== "void");
907
1071
  }
@@ -118,8 +118,9 @@ export declare function mergeHarvestReports(a: HarvestReport, b: HarvestReport):
118
118
  * otherwise, and carries `historyUnknown` when either store that resolved it could not judge its history;
119
119
  * - `evidenceCapability` is `"journal"` only when both are; `custodyState` is the worse of the two
120
120
  * (`damaged` ≻ `capability-absent` ≻ `complete`);
121
- * - each carrier class adds `removed` / `remaining`, and is `unverified` when either store's answer is (or is
122
- * missing); `quarantineUnattributed` / `derivedEntries` add their counts the same way; `replay` is
121
+ * - each carrier class adds `removed` / `remaining` (and `unreadable` when both stores answer it), and is
122
+ * `unverified` when either store's answer is (or is missing); `quarantineUnattributed` / `derivedEntries` add their
123
+ * counts the same way; `replay` is
123
124
  * `"converges"` only when both are; `propagation` stays `"local-store-only"` (every store merged is local).
124
125
  */
125
126
  export declare function mergeErasureAttestations(a: MemoryErasureAttestation, b: MemoryErasureAttestation): MemoryErasureAttestation;
@@ -89,14 +89,14 @@ export function mergeInjections(project, personal) {
89
89
  const indexParts = [project.index, personal.index].filter((s) => Boolean(s && s.trim()));
90
90
  const announcements = [...(project.announcements ?? []), ...(personal.announcements ?? [])];
91
91
  const announceParts = [project.announceBlock, personal.announceBlock].filter((s) => Boolean(s && s.trim()));
92
- const blockParts = [instruction || readOnlyNotice, ...indexParts, ...announceParts];
92
+ const blockParts = [instruction || readOnlyNotice, ...indexParts];
93
93
  const indexSeed = project.indexSeed ?? personal.indexSeed;
94
94
  return {
95
95
  instruction,
96
96
  ...(readOnlyNotice !== undefined ? { readOnlyNotice } : {}),
97
97
  ...(indexParts.length > 0 ? { index: indexParts.join("\n\n") } : {}),
98
98
  ...(announcements.length > 0 ? { announcements } : {}),
99
- ...(announceParts.length > 0 ? { announceBlock: announceParts.join("\n\n") } : {}),
99
+ ...(announceParts.length > 0 ? { announceBlock: announceParts.join("\n") } : {}),
100
100
  ...(indexSeed !== undefined ? { indexSeed } : {}),
101
101
  block: blockParts.filter((s) => s && s.trim()).join("\n\n"),
102
102
  };
@@ -161,7 +161,11 @@ export function mergeErasureAttestations(a, b) {
161
161
  notFound.set(row.id, unknown ? { id: row.id, historyUnknown: true } : { id: row.id });
162
162
  }
163
163
  const answered = (x) => typeof x === "object" && x !== null && typeof x.removed === "number" && typeof x.remaining === "number";
164
- const carrier = (x, y) => (answered(x) && answered(y) ? { removed: x.removed + y.removed, remaining: x.remaining + y.remaining } : { unverified: true });
164
+ const unreadable = (x, y) => {
165
+ const [p, q] = [Reflect.get(x, "unreadable"), Reflect.get(y, "unreadable")];
166
+ return typeof p === "number" && typeof q === "number" ? { unreadable: p + q } : {};
167
+ };
168
+ const carrier = (x, y) => (answered(x) && answered(y) ? { removed: x.removed + y.removed, remaining: x.remaining + y.remaining, ...unreadable(x, y) } : { unverified: true });
165
169
  const counted = (x) => typeof x === "object" && x !== null && typeof x.count === "number";
166
170
  const count = (x, y) => (counted(x) && counted(y) ? { count: x.count + y.count } : { unverified: true });
167
171
  const custodyRank = { complete: 0, "capability-absent": 1, damaged: 2 };