@sema-agent/core 7.64.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 (103) hide show
  1. package/CHANGELOG.md +317 -0
  2. package/dist/agents/peer-held-queue.js +1 -1
  3. package/dist/agents/peer-idle.js +1 -1
  4. package/dist/agents/peer-session-drain.js +3 -3
  5. package/dist/agents/subagent.js +5 -2
  6. package/dist/core/auto-promote.d.ts +3 -2
  7. package/dist/core/background-agent-store.d.ts +7 -0
  8. package/dist/core/checkpoint-store.d.ts +2 -3
  9. package/dist/core/context-settings.d.ts +1 -2
  10. package/dist/core/context-settings.js +1 -1
  11. package/dist/core/engine-attestation.d.ts +2 -2
  12. package/dist/core/engine-notice.d.ts +13 -1
  13. package/dist/core/engine-notice.js +3 -1
  14. package/dist/core/file-history-store.d.ts +2 -2
  15. package/dist/core/file-history-store.js +0 -4
  16. package/dist/core/gate-fold.js +3 -1
  17. package/dist/core/governance-codes.d.ts +6 -4
  18. package/dist/core/governance-codes.js +4 -3
  19. package/dist/core/leg-identity.d.ts +18 -2
  20. package/dist/core/leg-identity.js +5 -7
  21. package/dist/core/mailbox-store.d.ts +1 -1
  22. package/dist/core/memory-engine/consolidation.d.ts +10 -4
  23. package/dist/core/memory-engine/consolidation.js +11 -1
  24. package/dist/core/memory-engine/control-objects.d.ts +36 -3
  25. package/dist/core/memory-engine/control-objects.js +134 -12
  26. package/dist/core/memory-engine/control-plane-adoption.d.ts +5 -2
  27. package/dist/core/memory-engine/control-plane-adoption.js +5 -3
  28. package/dist/core/memory-engine/delegation-settlement.d.ts +59 -42
  29. package/dist/core/memory-engine/delegation-settlement.js +175 -202
  30. package/dist/core/memory-engine/dual-root.d.ts +3 -2
  31. package/dist/core/memory-engine/dual-root.js +5 -1
  32. package/dist/core/memory-engine/engine.d.ts +16 -10
  33. package/dist/core/memory-engine/engine.js +138 -145
  34. package/dist/core/memory-engine/file-backend.d.ts +17 -11
  35. package/dist/core/memory-engine/file-backend.js +53 -103
  36. package/dist/core/memory-engine/layout.d.ts +129 -49
  37. package/dist/core/memory-engine/layout.js +202 -82
  38. package/dist/core/memory-engine/memory-age.d.ts +12 -0
  39. package/dist/core/memory-engine/memory-age.js +17 -0
  40. package/dist/core/memory-engine/memory-backend-contract.js +5 -4
  41. package/dist/core/memory-engine/memory-deployment.d.ts +1 -1
  42. package/dist/core/memory-engine/origin-clearance.js +1 -5
  43. package/dist/core/memory-engine/tools.d.ts +2 -2
  44. package/dist/core/memory-engine/tools.js +7 -7
  45. package/dist/core/memory-engine/types.d.ts +16 -10
  46. package/dist/core/memory-vector.d.ts +6 -14
  47. package/dist/core/memory.d.ts +0 -462
  48. package/dist/core/memory.js +0 -333
  49. package/dist/core/permission-rule-consent.d.ts +49 -18
  50. package/dist/core/permission-rule-consent.js +59 -38
  51. package/dist/core/permission-rule-store.d.ts +24 -14
  52. package/dist/core/retention.d.ts +2 -7
  53. package/dist/core/retention.js +0 -1
  54. package/dist/core/runner/classifier-ladder.d.ts +3 -1
  55. package/dist/core/runner/classifier-ladder.js +2 -2
  56. package/dist/core/runner/contracts.d.ts +9 -9
  57. package/dist/core/runner/delegation-owner.d.ts +0 -8
  58. package/dist/core/runner/delegation-owner.js +1 -3
  59. package/dist/core/runner/prepare-gate-stations.d.ts +8 -5
  60. package/dist/core/runner/prepare-gate-stations.js +13 -5
  61. package/dist/core/runner/prepare-memory-engine-session.js +1 -1
  62. package/dist/core/runner/prepare-memory.js +9 -9
  63. package/dist/core/runner/prepare-policy-chain.js +11 -3
  64. package/dist/core/runner/prepare-task.d.ts +5 -1
  65. package/dist/core/runner/prepare-task.js +3 -3
  66. package/dist/core/runner/run-notification-lane.js +1 -1
  67. package/dist/core/runner/sub-agent-deny-lists.d.ts +19 -0
  68. package/dist/core/runner/sub-agent-deny-lists.js +25 -0
  69. package/dist/core/secret-env.d.ts +2 -2
  70. package/dist/core/secret-env.js +6 -20
  71. package/dist/core/session-store.d.ts +6 -6
  72. package/dist/core/session-store.js +3 -3
  73. package/dist/core/store-contracts/background-agent-store-contract.js +13 -0
  74. package/dist/core/task-notification.d.ts +35 -8
  75. package/dist/core/task-notification.js +31 -13
  76. package/dist/core/task-registry-agent.js +1 -0
  77. package/dist/core/task-registry-shared.d.ts +4 -0
  78. package/dist/core/tool-errors.d.ts +3 -3
  79. package/dist/core/tool-spec.d.ts +7 -7
  80. package/dist/core/write-protect.d.ts +16 -0
  81. package/dist/core/write-protect.js +10 -0
  82. package/dist/engine/session/repo-utils.d.ts +14 -4
  83. package/dist/engine/session/repo-utils.js +5 -4
  84. package/dist/index.d.ts +6 -6
  85. package/dist/index.js +6 -6
  86. package/dist/prompts/default.d.ts +1 -2
  87. package/dist/scenarios/full-body.d.ts +1 -1
  88. package/dist/stores/file/adoption/adopt.d.ts +1 -1
  89. package/dist/stores/file/index.d.ts +1 -15
  90. package/dist/stores/file/index.js +1 -15
  91. package/dist/stores/file/permission-rule-store.d.ts +12 -1
  92. package/dist/stores/file/permission-rule-store.js +31 -4
  93. package/dist/stores/file/session-store.js +1 -1
  94. package/package.json +2 -2
  95. package/test/export-surface.snapshot.json +9 -73
  96. package/wire-changes/core-stores-sql@7.65.0.json +30 -0
  97. package/wire-changes/core@7.65.0.json +1650 -0
  98. package/wire-changes/mods-kernel@7.65.0.json +30 -0
  99. package/wire-changes/primitives@7.65.0.json +337 -0
  100. package/dist/core/memory-recall.d.ts +0 -208
  101. package/dist/core/memory-recall.js +0 -136
  102. package/dist/stores/file/memory-store.d.ts +0 -124
  103. package/dist/stores/file/memory-store.js +0 -353
@@ -1,9 +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 } from "./control-objects.js";
6
+ import { fileControlObjectDirOf, fileControlObjectFaultCode } from "./control-objects.js";
7
7
  function reqStr(v) {
8
8
  return typeof v === "string" && v.length > 0;
9
9
  }
@@ -190,7 +190,7 @@ export async function expireOverdueSettlements(docs, input) {
190
190
  await settleDelegation(docs, { settleId: s.row.settleId, status: "expired", now: input.now });
191
191
  return overdue.map((s) => s.row.settleId);
192
192
  }
193
- export async function replayExternalSettlementEffects(docs, controlDir, input) {
193
+ export async function replayExternalSettlementEffects(docs, input) {
194
194
  const applied = [];
195
195
  const folded = (await effectiveSettlements(docs)).filter((s) => s.effective === "external" && s.terminalEventId !== undefined);
196
196
  if (folded.length === 0)
@@ -200,7 +200,7 @@ export async function replayExternalSettlementEffects(docs, controlDir, input) {
200
200
  if (rec.effects[s.row.settleId] === s.terminalEventId)
201
201
  continue;
202
202
  const sessionId = s.row.sessionId;
203
- 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");
204
204
  if (outcome === "unpersisted")
205
205
  continue;
206
206
  try {
@@ -299,7 +299,11 @@ function convertSessionAccounts(read, controlDir) {
299
299
  }
300
300
  export function convertV1SidecarKeys(controlDir) {
301
301
  readProjectionDebts(controlDir);
302
- 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);
303
307
  const accounts = fileGovernanceDocOf("session-accounts");
304
308
  if (coerceSessionAccounts(readStrictSidecar(controlDir, accounts.file, accounts.what)).version !== 2) {
305
309
  lockedStrictUpdate(controlDir, accounts.file, accounts.what, coerceSessionAccounts, (again) => {
@@ -484,11 +488,11 @@ function coerceHolds(raw) {
484
488
  function holdsOnDiskAreV1(raw) {
485
489
  return typeof raw === "object" && raw !== null && raw.version === 1;
486
490
  }
487
- export function readHolds(controlDir) {
488
- const raw = readStrictSidecar(controlDir, HOLDS_FILE, HOLDS_WHAT);
491
+ export async function readHolds(docs) {
492
+ const raw = await docs.readDoc("holds");
489
493
  if (!holdsOnDiskAreV1(raw))
490
494
  return coerceHolds(raw).rows;
491
- return lockedStrictUpdate(controlDir, HOLDS_FILE, HOLDS_WHAT, coerceHolds, (again) => ({ next: again, result: again.rows }));
495
+ return docs.updateDoc("holds", coerceHolds, (again) => ({ next: again, result: again.rows }));
492
496
  }
493
497
  function sha256(buf) {
494
498
  return createHash("sha256").update(buf).digest("hex");
@@ -503,41 +507,56 @@ function dropStaging(claimedPath) {
503
507
  export function stagingPathBeside(absPath, stagingName) {
504
508
  return join(dirname(absPath), stagingName);
505
509
  }
506
- function writeCustodyVerified(dest, bytes) {
510
+ async function heldBytesAt(objects, kind, name, digest) {
511
+ let bytes;
507
512
  try {
508
- const fd = openSync(dest, "wx", 0o600);
509
- try {
510
- writeFileSync(fd, bytes);
511
- fsyncSync(fd);
512
- }
513
- finally {
514
- closeSync(fd);
515
- }
516
- 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);
517
525
  }
518
526
  catch {
519
527
  return false;
520
528
  }
529
+ return (await heldBytesAt(objects, "hold", captureName, sha256(bytes))) === "holds";
521
530
  }
522
- export function retireStagingToCustody(claimedPath, custodyDir, stagingName) {
531
+ export async function retireStagingToObject(objects, claimedPath, kind, name, refusedAt) {
523
532
  const unsettled = (why) => ({ settled: false, at: `${claimedPath} (${why})` });
524
533
  let bytes;
525
534
  try {
526
535
  bytes = readFileSync(claimedPath);
527
- ensureDirExists(custodyDir);
528
536
  }
529
537
  catch {
530
538
  return unsettled("the staged bytes could not be read into the control plane");
531
539
  }
532
- 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 {
533
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}`);
534
553
  try {
535
554
  unlinkSync(claimedPath);
536
555
  }
537
556
  catch {
538
557
  return unsettled("a copy reached the control plane; this one could not be removed");
539
558
  }
540
- 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` };
541
560
  }
542
561
  export function noReplaceRestore(stagingPath, destPath) {
543
562
  try {
@@ -567,24 +586,23 @@ export function noReplaceRestore(stagingPath, destPath) {
567
586
  }
568
587
  return "restored";
569
588
  }
570
- export function openInstructionHold(controlDir, input) {
589
+ export async function openInstructionHold(store, input) {
571
590
  const holdId = `hold-${input.now()}-${createHash("sha256").update(`${input.sessionId}\n${input.rel}\n${input.content}`).digest("hex").slice(0, 12)}`;
572
591
  const buf = Buffer.from(input.content, "utf8");
573
592
  const digest = sha256(buf);
574
593
  const captureName = `${holdId}.md`;
575
- const holdDir = join(controlDir, HOLD_DIR);
576
594
  let thirdWriterStrandedAt;
577
- const refuse = (rawReason, terminal) => {
595
+ const refuse = async (rawReason, terminal) => {
578
596
  const reason = thirdWriterStrandedAt === undefined ? rawReason : `${rawReason}; a concurrent writer's bytes are preserved at ${thirdWriterStrandedAt}`;
579
597
  try {
580
- disposeHold(controlDir, { holdId, terminal });
598
+ await disposeHold(store, { holdId, terminal });
581
599
  }
582
600
  catch {
583
601
  }
584
602
  return { ok: false, holdId, reason };
585
603
  };
586
604
  try {
587
- lockedStrictUpdate(controlDir, HOLDS_FILE, HOLDS_WHAT, coerceHolds, (rec) => {
605
+ await store.updateDoc("holds", coerceHolds, (rec) => {
588
606
  if (rec.rows.some((r) => r.holdId === holdId))
589
607
  return { result: undefined };
590
608
  rec.rows.push({
@@ -608,15 +626,12 @@ export function openInstructionHold(controlDir, input) {
608
626
  catch (err) {
609
627
  return { ok: false, reason: `hold ledger refused: ${thrownText(err)}` };
610
628
  }
611
- try {
612
- ensureDirExists(holdDir);
613
- }
614
- catch (err) {
615
- return refuse(`hold capture failed: ${thrownText(err)}`, "capture_lost");
616
- }
617
- if (!writeCustodyVerified(join(holdDir, captureName), buf)) {
629
+ if (!(await holdCustodyVerified(store, captureName, buf))) {
618
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");
619
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");
620
635
  const stagingName = mintStagingName(holdId, input.now);
621
636
  const stagingPath = stagingPathBeside(input.absPath, stagingName);
622
637
  try {
@@ -639,14 +654,14 @@ export function openInstructionHold(controlDir, input) {
639
654
  else {
640
655
  const restore = noReplaceRestore(claimedPath, input.absPath);
641
656
  if (restore !== "restored") {
642
- const retired = retireStagingToCustody(claimedPath, holdDir, stagingName);
657
+ const retired = await retireStagingToObject(store, claimedPath, "hold", stagingName, () => input.actRefused(claimedPath, "removing a hold's staged copy"));
643
658
  if (restore === "stranded" || !retired.settled)
644
659
  thirdWriterStrandedAt = retired.at;
645
660
  }
646
661
  }
647
662
  }
648
663
  try {
649
- lockedStrictUpdate(controlDir, HOLDS_FILE, HOLDS_WHAT, coerceHolds, (rec) => {
664
+ await store.updateDoc("holds", coerceHolds, (rec) => {
650
665
  const row = rec.rows.find((r) => r.holdId === holdId);
651
666
  if (row === undefined || row.status !== "capturing")
652
667
  return { result: undefined };
@@ -659,12 +674,14 @@ export function openInstructionHold(controlDir, input) {
659
674
  }
660
675
  return { ok: true, holdId, ...(thirdWriterStrandedAt !== undefined ? { thirdWriterStrandedAt } : {}) };
661
676
  }
662
- export function reconcileHolds(controlDir, input) {
663
- const holdDir = join(controlDir, HOLD_DIR);
677
+ export async function reconcileHolds(store, input) {
664
678
  const discloseStranded = (holdId, scope, rel, at) => {
665
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`);
666
680
  };
667
- 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);
668
685
  const stagingName = mintStagingName(row.holdId, input.now);
669
686
  renameSync(plane, stagingPathBeside(plane, stagingName));
670
687
  const staging = claimStaging(stagingPathBeside(plane, stagingName));
@@ -675,7 +692,7 @@ export function reconcileHolds(controlDir, input) {
675
692
  const restore = noReplaceRestore(staging, plane);
676
693
  if (restore === "restored")
677
694
  return;
678
- const retired = retireStagingToCustody(staging, holdDir, stagingName);
695
+ const retired = await retireStagingToObject(store, staging, "hold", stagingName, () => input.actRefused(staging, "removing a hold's staged copy"));
679
696
  if (restore === "stranded" || !retired.settled)
680
697
  discloseStranded(row.holdId, row.scope, row.rel, retired.at);
681
698
  };
@@ -694,7 +711,7 @@ export function reconcileHolds(controlDir, input) {
694
711
  seatListing.set(seatDir, names);
695
712
  return names;
696
713
  };
697
- const sweepStagingGenerations = (row, plane, custody) => {
714
+ const sweepStagingGenerations = async (row, plane, custody) => {
698
715
  const seatDir = dirname(plane);
699
716
  const pattern = stagingNamePattern(row.holdId);
700
717
  let seat;
@@ -707,6 +724,8 @@ export function reconcileHolds(controlDir, input) {
707
724
  for (const name of listSeat(seatDir)) {
708
725
  if (!pattern.test(name))
709
726
  continue;
727
+ if (input.actRefused(join(seatDir, name), "recovering a hold's staged copy") !== undefined)
728
+ continue;
710
729
  const staged = claimStaging(join(seatDir, name));
711
730
  let bytes;
712
731
  try {
@@ -718,48 +737,44 @@ export function reconcileHolds(controlDir, input) {
718
737
  continue;
719
738
  }
720
739
  if (sha256(bytes) === row.contentDigest) {
721
- if (custody === "holds" || (custody === "absent" && writeCustodyVerified(join(holdDir, row.captureName), bytes))) {
740
+ if (custody === "holds") {
722
741
  dropStaging(staged);
723
742
  continue;
724
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
+ }
725
749
  }
726
750
  const onSeat = seat !== undefined && isSameFile(staged, seat);
727
- const retired = retireStagingToCustody(staged, holdDir, name);
751
+ const retired = await retireStagingToObject(store, staged, "hold", name, () => input.actRefused(staged, "removing a hold's staged copy"));
728
752
  if (!(onSeat && retired.settled && isSameFile(plane, seat)))
729
753
  discloseStranded(row.holdId, row.scope, row.rel, retired.at);
730
754
  }
731
755
  };
732
- const ledger = readHolds(controlDir);
756
+ const ledger = await readHolds(store);
733
757
  const rows = ledger.filter((r) => r.status === "capturing");
734
758
  for (const row of rows) {
735
- const custody = join(holdDir, row.captureName);
736
- let custodyBytes;
737
- try {
738
- custodyBytes = readFileSync(custody);
739
- }
740
- catch {
741
- custodyBytes = undefined;
742
- }
743
- const custodyState = custodyBytes === undefined ? "absent" : sha256(custodyBytes) === row.contentDigest ? "holds" : "mismatch";
759
+ let custodyState = await heldBytesAt(store, "hold", row.captureName, row.contentDigest);
744
760
  try {
745
- sweepStagingGenerations(row, join(input.homeOf(row.scope), row.rel), custodyState);
761
+ await sweepStagingGenerations(row, join(input.homeOf(row.scope), row.rel), custodyState);
746
762
  }
747
763
  catch {
748
764
  }
765
+ if (custodyState === "absent")
766
+ custodyState = await heldBytesAt(store, "hold", row.captureName, row.contentDigest);
749
767
  let terminalLost = false;
750
- if (existsSync(custody)) {
751
- const bytes = custodyBytes ?? readFileSync(custody);
752
- if (sha256(bytes) !== row.contentDigest) {
753
- terminalLost = true;
754
- }
755
- else {
756
- const plane = join(input.homeOf(row.scope), row.rel);
757
- if (existsSync(plane)) {
758
- try {
759
- finishRemoval(row, plane);
760
- }
761
- catch {
762
- }
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 {
763
778
  }
764
779
  }
765
780
  }
@@ -769,10 +784,9 @@ export function reconcileHolds(controlDir, input) {
769
784
  const bytes = readFileSync(plane);
770
785
  if (sha256(bytes) === row.contentDigest) {
771
786
  try {
772
- ensureDirExists(holdDir);
773
- if (!writeCustodyVerified(custody, bytes))
774
- throw new Error("the custody copy could not be written or made durable");
775
- 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);
776
790
  }
777
791
  catch {
778
792
  terminalLost = true;
@@ -786,7 +800,7 @@ export function reconcileHolds(controlDir, input) {
786
800
  terminalLost = true;
787
801
  }
788
802
  }
789
- lockedStrictUpdate(controlDir, HOLDS_FILE, HOLDS_WHAT, coerceHolds, (rec) => {
803
+ await store.updateDoc("holds", coerceHolds, (rec) => {
790
804
  const r = rec.rows.find((x) => x.holdId === row.holdId);
791
805
  if (r === undefined || r.status !== "capturing")
792
806
  return { result: undefined };
@@ -802,7 +816,7 @@ export function reconcileHolds(controlDir, input) {
802
816
  }
803
817
  let custodyNames;
804
818
  try {
805
- custodyNames = new Set(readdirSync(holdDir));
819
+ custodyNames = new Set(await store.listObjects("hold"));
806
820
  }
807
821
  catch {
808
822
  custodyNames = new Set();
@@ -812,23 +826,21 @@ export function reconcileHolds(controlDir, input) {
812
826
  if (row.status !== "disposed" || quarantineName === undefined || !custodyNames.has(row.captureName))
813
827
  continue;
814
828
  try {
815
- const moved = moveCustodyToSeat(controlDir, row, quarantineName);
829
+ const moved = await moveCustodyToSeat(store, row, quarantineName);
816
830
  if (moved.at === "custody") {
817
- 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 ${join(holdDir, row.captureName)} for host recovery, and the move is retried at every harvest`);
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`);
818
832
  }
819
833
  }
820
834
  catch (err) {
821
835
  input.onStranded(row.scope, `instruction hold ${row.holdId} (disposed, ${row.disposition.terminal}): ${thrownText(err)}`);
822
836
  }
823
837
  }
824
- adoptOrphanedQuarantineBytes(controlDir, ledger);
838
+ await adoptOrphanedQuarantineBytes(store, ledger);
825
839
  }
826
- function adoptOrphanedQuarantineBytes(controlDir, snapshot) {
827
- const holdDir = join(controlDir, HOLD_DIR);
828
- const qDir = join(controlDir, QUARANTINE_DIR);
840
+ async function adoptOrphanedQuarantineBytes(store, snapshot) {
829
841
  let listing;
830
842
  try {
831
- listing = readdirSync(qDir).filter((n) => n.includes("-hold-") && safeBasename(n)).sort();
843
+ listing = (await store.listObjects("quarantine")).filter((n) => n.includes("-hold-") && safeBasename(n));
832
844
  }
833
845
  catch {
834
846
  return;
@@ -839,35 +851,50 @@ function adoptOrphanedQuarantineBytes(controlDir, snapshot) {
839
851
  seats.add(r.disposition.quarantineName);
840
852
  if (listing.every((n) => seats.has(n)))
841
853
  return;
842
- lockedStrictUpdate(controlDir, HOLDS_FILE, HOLDS_WHAT, coerceHolds, (rec) => {
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) => {
843
883
  const claimed = new Set();
844
884
  for (const r of rec.rows)
845
885
  if (r.disposition?.quarantineName !== undefined)
846
886
  claimed.add(r.disposition.quarantineName);
847
- const digests = new Map();
848
- const digestOf = (name) => {
849
- if (!digests.has(name)) {
850
- let digest;
851
- try {
852
- const path = join(qDir, name);
853
- digest = lstatSync(path).isFile() ? sha256(readFileSync(path)) : undefined;
854
- }
855
- catch {
856
- digest = undefined;
857
- }
858
- digests.set(name, digest);
859
- }
860
- return digests.get(name);
861
- };
887
+ const digestOf = (name) => digests.get(name);
862
888
  const wants = (r) => {
863
- if (existsSync(join(holdDir, r.captureName)))
889
+ const custody = custodyStands.get(r.holdId);
890
+ if (custody !== false)
864
891
  return false;
865
892
  if (r.status === "held")
866
893
  return true;
867
894
  if (r.status !== "disposed")
868
895
  return false;
869
896
  const seat = r.disposition?.quarantineName;
870
- return seat === undefined || !existsSync(join(qDir, seat));
897
+ return seat === undefined || seatStands.get(seat) === false;
871
898
  };
872
899
  const endsWithCapture = (name, r) => name.endsWith(`-hold-${r.captureName}`);
873
900
  const reservedForOther = (name) => rec.rows.some((r) => endsWithCapture(name, r));
@@ -890,134 +917,80 @@ function adoptOrphanedQuarantineBytes(controlDir, snapshot) {
890
917
  }
891
918
  }
892
919
  let changed = false;
920
+ const back = [];
893
921
  for (const [r, f] of found) {
894
922
  if (r.status === "disposed") {
895
923
  r.disposition = { ...r.disposition, quarantineName: f };
896
924
  changed = true;
897
925
  continue;
898
926
  }
899
- try {
900
- ensureDirExists(holdDir);
901
- moveNoReplace(join(qDir, f), join(holdDir, r.captureName));
902
- }
903
- catch {
904
- }
927
+ back.push({ file: f, captureName: r.captureName });
905
928
  }
906
- return changed ? { next: rec, result: undefined } : { result: undefined };
929
+ return changed ? { next: rec, result: back } : { result: back };
907
930
  });
908
- }
909
- export function holdQuarantineNameOf(row) {
910
- return `${row.contentDigest.slice(0, 16)}-hold-${row.captureName}`;
911
- }
912
- function quarantineSeatState(seat, digest) {
913
- let isFile;
914
- try {
915
- isFile = lstatSync(seat).isFile();
916
- }
917
- catch (err) {
918
- return err.code === "ENOENT" ? "absent" : "foreign";
919
- }
920
- if (!isFile)
921
- return "foreign";
922
- try {
923
- return sha256(readFileSync(seat)) === digest ? "holds" : "foreign";
924
- }
925
- catch {
926
- return "foreign";
927
- }
928
- }
929
- function foreignSeatRefusal(holdId, seat) {
930
- return new ControlPlaneCorruptError(`memory instruction hold ${holdId}: the quarantine seat ${seat} 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)`);
931
- }
932
- function moveCustodyToSeat(controlDir, row, seatName) {
933
- const custody = join(controlDir, HOLD_DIR, row.captureName);
934
- const qDir = join(controlDir, QUARANTINE_DIR);
935
- const seat = join(qDir, seatName);
936
- const atSeat = () => {
937
- const state = quarantineSeatState(seat, row.contentDigest);
938
- if (state === "foreign")
939
- throw foreignSeatRefusal(row.holdId, seat);
940
- if (state === "absent")
941
- return undefined;
931
+ for (const m of moves) {
942
932
  try {
943
- if (sha256(readFileSync(custody)) === row.contentDigest)
944
- unlinkSync(custody);
933
+ await store.moveObject({ kind: "quarantine", name: m.file }, { kind: "hold", name: m.captureName });
945
934
  }
946
935
  catch {
947
936
  }
948
- return { at: "seat" };
949
- };
950
- const there = atSeat();
951
- if (there !== undefined)
952
- return there;
953
- if (!existsSync(custody))
954
- return { at: "none" };
955
- try {
956
- ensureDirExists(qDir);
957
937
  }
958
- catch (err) {
959
- return { at: "custody", code: err.code ?? "mkdir error" };
960
- }
961
- if (moveNoReplace(custody, seat) === "moved")
962
- return { at: "seat" };
963
- return atSeat() ?? { at: "custody", code: "move refused" };
964
938
  }
965
- function moveNoReplace(from, to) {
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);
966
948
  try {
967
- linkSync(from, to);
949
+ await objects.moveObject({ kind: "hold", name: row.captureName }, { kind: "quarantine", name: seatName });
950
+ return { at: "seat" };
968
951
  }
969
952
  catch (err) {
970
- const code = err.code;
971
- if (code === "EEXIST")
972
- return "occupied";
973
- if (code === "ENOENT")
974
- return "refused";
975
- try {
976
- renameSync(from, to);
977
- return "moved";
978
- }
979
- catch {
980
- return "refused";
981
- }
982
- }
983
- try {
984
- unlinkSync(from);
985
- }
986
- catch {
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" };
987
962
  }
988
- return "moved";
989
963
  }
990
- export function disposeHold(controlDir, input) {
991
- const row = readHolds(controlDir).find((r) => r.holdId === input.holdId);
964
+ export async function disposeHold(store, input) {
965
+ const row = (await readHolds(store)).find((r) => r.holdId === input.holdId);
992
966
  if (row === undefined || row.status === "disposed" || row.status === "released")
993
967
  return { ok: false };
994
968
  const quarantineName = holdQuarantineNameOf(row);
995
969
  if (!safeBasename(quarantineName)) {
996
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)`);
997
971
  }
998
- const seatPath = join(controlDir, QUARANTINE_DIR, quarantineName);
999
- const verdict = lockedStrictUpdate(controlDir, HOLDS_FILE, HOLDS_WHAT, coerceHolds, (rec) => {
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) => {
1000
975
  const r = rec.rows.find((x) => x.holdId === input.holdId);
1001
976
  if (r === undefined || r.status === "disposed" || r.status === "released")
1002
977
  return { result: undefined };
1003
- const custodyHere = existsSync(join(controlDir, HOLD_DIR, r.captureName));
1004
- const seatState = quarantineSeatState(seatPath, r.contentDigest);
1005
978
  const named = seatState === "holds" || (custodyHere && seatState === "absent");
1006
979
  r.status = "disposed";
1007
980
  r.disposition = { terminal: input.terminal, ...(named ? { quarantineName } : {}) };
1008
- return { next: rec, result: { named, refused: custodyHere && seatState === "foreign" } };
981
+ return { next: rec, result: { named, refused: custodyHere && seatState === "other" } };
1009
982
  });
1010
983
  if (verdict === undefined)
1011
984
  return { ok: false };
1012
985
  if (verdict.refused)
1013
- throw foreignSeatRefusal(row.holdId, seatPath);
986
+ throw foreignSeatRefusal(row.holdId, quarantineName);
1014
987
  if (!verdict.named)
1015
988
  return { ok: true };
1016
- const moved = moveCustodyToSeat(controlDir, row, quarantineName);
989
+ const moved = await moveCustodyToSeat(store, row, quarantineName);
1017
990
  return { ok: true, ...(moved.at === "seat" ? { quarantineName } : {}) };
1018
991
  }
1019
- export function markHoldReleased(controlDir, input) {
1020
- const captureName = lockedStrictUpdate(controlDir, HOLDS_FILE, HOLDS_WHAT, coerceHolds, (rec) => {
992
+ export async function markHoldReleased(store, input) {
993
+ const captureName = await store.updateDoc("holds", coerceHolds, (rec) => {
1021
994
  const r = rec.rows.find((x) => x.holdId === input.holdId);
1022
995
  if (r === undefined || (r.status !== "held" && !(r.status === "disposed" && r.disposition?.terminal === "expired")))
1023
996
  return { result: undefined };
@@ -1029,12 +1002,12 @@ export function markHoldReleased(controlDir, input) {
1029
1002
  if (captureName === undefined)
1030
1003
  return;
1031
1004
  try {
1032
- unlinkSync(join(controlDir, HOLD_DIR, captureName));
1005
+ await store.deleteObject("hold", captureName);
1033
1006
  }
1034
1007
  catch {
1035
1008
  }
1036
1009
  }
1037
- export function resolveHoldRecord(controlDir, input) {
1010
+ export async function resolveHoldRecord(docs, input) {
1038
1011
  if (typeof input.requestId !== "string" || input.requestId.length === 0) {
1039
1012
  const e = new Error("resolveHold requires a non-empty requestId (the audit attribution of who resolved) — refused, never defaulted.");
1040
1013
  e.code = "memory.hold_resolve_unattributed";
@@ -1045,7 +1018,7 @@ export function resolveHoldRecord(controlDir, input) {
1045
1018
  e.code = "memory.hold_resolve_invalid";
1046
1019
  throw e;
1047
1020
  }
1048
- lockedStrictUpdate(controlDir, HOLDS_FILE, HOLDS_WHAT, coerceHolds, (rec) => {
1021
+ await docs.updateDoc("holds", coerceHolds, (rec) => {
1049
1022
  const r = rec.rows.find((x) => x.holdId === input.holdId);
1050
1023
  if (r === undefined) {
1051
1024
  const e = new Error(`resolveHold: unknown holdId ${JSON.stringify(input.holdId)}`);
@@ -1068,8 +1041,8 @@ export function resolveHoldRecord(controlDir, input) {
1068
1041
  return { next: rec, result: undefined };
1069
1042
  });
1070
1043
  }
1071
- export function clearHoldResolution(controlDir, input) {
1072
- lockedStrictUpdate(controlDir, HOLDS_FILE, HOLDS_WHAT, coerceHolds, (rec) => {
1044
+ export async function clearHoldResolution(docs, input) {
1045
+ await docs.updateDoc("holds", coerceHolds, (rec) => {
1073
1046
  const r = rec.rows.find((x) => x.holdId === input.holdId);
1074
1047
  if (r === undefined || r.resolved === undefined || r.resolvedAt !== input.ifResolvedAt)
1075
1048
  return { result: undefined };
@@ -1078,21 +1051,21 @@ export function clearHoldResolution(controlDir, input) {
1078
1051
  return { next: rec, result: undefined };
1079
1052
  });
1080
1053
  }
1081
- export function readHoldCustody(controlDir, row) {
1082
- const seats = [join(controlDir, HOLD_DIR, row.captureName), ...(row.disposition?.quarantineName !== undefined ? [join(controlDir, QUARANTINE_DIR, row.disposition.quarantineName)] : [])];
1083
- 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) {
1084
1057
  try {
1085
- const bytes = readFileSync(seat);
1086
- if (sha256(bytes) === row.contentDigest)
1087
- 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");
1088
1061
  }
1089
1062
  catch {
1090
1063
  }
1091
1064
  }
1092
1065
  return undefined;
1093
1066
  }
1094
- export async function sessionBlocksCleanRelease(docs, controlDir, sessionId) {
1095
- if (readSessionPollution(controlDir, sessionId) !== undefined)
1067
+ export async function sessionBlocksCleanRelease(docs, sessionId) {
1068
+ if ((await readSessionPollution(docs, sessionId)) !== undefined)
1096
1069
  return true;
1097
1070
  return (await sessionSettlements(docs, sessionId)).some((s) => s.effective !== "clean" && s.effective !== "void");
1098
1071
  }