@sema-agent/core 7.62.0 → 7.63.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.
- package/CHANGELOG.md +528 -0
- package/dist/agents/subagent-transcript-placement.d.ts +23 -0
- package/dist/agents/subagent-transcript-placement.js +14 -0
- package/dist/agents/subagent.js +82 -51
- package/dist/brain/route-adjudicator.js +9 -4
- package/dist/core/approval-object.js +1 -1
- package/dist/core/arg-summary.js +1 -1
- package/dist/core/ask-question.d.ts +2 -2
- package/dist/core/canonical-json.d.ts +23 -13
- package/dist/core/canonical-json.js +4 -4
- package/dist/core/checkpoint-queries.d.ts +47 -4
- package/dist/core/checkpoint-queries.js +36 -16
- package/dist/core/checkpoint-requirements.d.ts +3 -3
- package/dist/core/checkpoint-requirements.js +2 -1
- package/dist/core/checkpoint-row-verdict.d.ts +15 -4
- package/dist/core/checkpoint-row-verdict.js +22 -2
- package/dist/core/checkpoint-store.d.ts +23 -10
- package/dist/core/checkpoint-store.js +45 -4
- package/dist/core/compliance.d.ts +6 -2
- package/dist/core/compliance.js +3 -3
- package/dist/core/engine-attestation.d.ts +1 -1
- package/dist/core/engine-notice.d.ts +19 -4
- package/dist/core/git-worktree-env.d.ts +62 -3
- package/dist/core/git-worktree-env.js +91 -30
- package/dist/core/governance-codes.d.ts +7 -3
- package/dist/core/governance-codes.js +13 -3
- package/dist/core/memory-engine/consolidation-driver.d.ts +32 -17
- package/dist/core/memory-engine/consolidation-driver.js +99 -98
- package/dist/core/memory-engine/consolidation.d.ts +73 -10
- package/dist/core/memory-engine/consolidation.js +167 -11
- package/dist/core/memory-engine/data-plane.d.ts +8 -4
- package/dist/core/memory-engine/data-plane.js +11 -6
- package/dist/core/memory-engine/delegation-settlement.d.ts +37 -35
- package/dist/core/memory-engine/delegation-settlement.js +53 -59
- package/dist/core/memory-engine/dual-root.d.ts +12 -6
- package/dist/core/memory-engine/dual-root.js +9 -5
- package/dist/core/memory-engine/engine.d.ts +98 -35
- package/dist/core/memory-engine/engine.js +477 -380
- package/dist/core/memory-engine/entry-scan.d.ts +8 -0
- package/dist/core/memory-engine/entry-scan.js +3 -0
- package/dist/core/memory-engine/file-backend.d.ts +29 -3
- package/dist/core/memory-engine/file-backend.js +66 -35
- package/dist/core/memory-engine/index.d.ts +7 -7
- package/dist/core/memory-engine/index.js +6 -6
- package/dist/core/memory-engine/landing-admission.d.ts +5 -3
- package/dist/core/memory-engine/landing-admission.js +3 -3
- package/dist/core/memory-engine/layout.d.ts +121 -45
- package/dist/core/memory-engine/layout.js +252 -102
- package/dist/core/memory-engine/memory-backend-contract.d.ts +19 -1
- package/dist/core/memory-engine/memory-backend-contract.js +320 -0
- package/dist/core/memory-engine/memory-deployment.d.ts +31 -8
- package/dist/core/memory-engine/memory-deployment.js +26 -12
- package/dist/core/memory-engine/scope-contract.d.ts +52 -11
- package/dist/core/memory-engine/scope-contract.js +68 -19
- package/dist/core/memory-engine/session-write-home.d.ts +3 -1
- package/dist/core/memory-engine/session-write-home.js +1 -1
- package/dist/core/memory-engine/sync-client.d.ts +3 -1
- package/dist/core/memory-engine/sync-client.js +7 -0
- package/dist/core/memory-engine/sync-report.d.ts +3 -2
- package/dist/core/memory-engine/sync-report.js +6 -2
- package/dist/core/memory-engine/tools.d.ts +2 -2
- package/dist/core/memory-engine/tools.js +10 -10
- package/dist/core/memory-engine/types.d.ts +118 -19
- package/dist/core/memory-engine/types.js +1 -0
- package/dist/core/memory.js +15 -6
- package/dist/core/mods/stations.d.ts +20 -1
- package/dist/core/mods/stations.js +21 -2
- package/dist/core/park-selfcheck.js +2 -2
- package/dist/core/pause-registry.d.ts +2 -0
- package/dist/core/pause-registry.js +3 -0
- package/dist/core/permission-rule-consent.d.ts +10 -3
- package/dist/core/permission-rule-consent.js +37 -24
- package/dist/core/permission-rule-store.d.ts +35 -1
- package/dist/core/permission-rule-store.js +27 -8
- package/dist/core/read-only-shell.js +3 -2
- package/dist/core/redaction-marks.d.ts +8 -0
- package/dist/core/redaction-marks.js +6 -0
- package/dist/core/remote-env.d.ts +12 -1
- package/dist/core/remote-env.js +3 -0
- package/dist/core/runner/child-tree.d.ts +29 -3
- package/dist/core/runner/child-tree.js +39 -16
- package/dist/core/runner/contracts.d.ts +18 -9
- package/dist/core/runner/gate-exit.js +2 -2
- package/dist/core/runner/inherited-ask-grants.js +1 -1
- package/dist/core/runner/memory-consolidation-driver.d.ts +6 -3
- package/dist/core/runner/memory-consolidation-driver.js +4 -4
- package/dist/core/runner/memory-consolidation-write-judge.d.ts +2 -1
- package/dist/core/runner/memory-consolidation-write-judge.js +3 -2
- package/dist/core/runner/person-path-rows.d.ts +8 -3
- package/dist/core/runner/person-path-rows.js +5 -1
- package/dist/core/runner/placement-fence.d.ts +12 -0
- package/dist/core/runner/placement-fence.js +20 -6
- package/dist/core/runner/prepare-ask-lane.js +2 -2
- package/dist/core/runner/prepare-caps-and-workflow.d.ts +4 -1
- package/dist/core/runner/prepare-caps-and-workflow.js +5 -2
- package/dist/core/runner/prepare-execution-env.js +17 -6
- package/dist/core/runner/prepare-hands-readface.d.ts +2 -1
- package/dist/core/runner/prepare-hands-readface.js +4 -3
- package/dist/core/runner/prepare-inherited-gate.d.ts +10 -8
- package/dist/core/runner/prepare-inherited-gate.js +8 -4
- package/dist/core/runner/prepare-memory-engine-session.d.ts +2 -4
- package/dist/core/runner/prepare-memory-engine-session.js +4 -4
- package/dist/core/runner/prepare-memory.d.ts +3 -2
- package/dist/core/runner/prepare-memory.js +28 -20
- package/dist/core/runner/prepare-park-ask.js +4 -3
- package/dist/core/runner/prepare-task.js +15 -12
- package/dist/core/runner/prepare-workspace-restore.js +3 -6
- package/dist/core/runner/run-leg.js +1 -1
- package/dist/core/runner/run-settle-and-teardown.js +3 -2
- package/dist/core/runner/terminal-account.js +1 -1
- package/dist/core/runner-deps.d.ts +6 -2
- package/dist/core/secret-env.d.ts +1 -1
- package/dist/core/secret-env.js +4 -4
- package/dist/core/session-checkout.d.ts +108 -0
- package/dist/core/session-checkout.js +485 -0
- package/dist/core/session-store.d.ts +12 -4
- package/dist/core/session-store.js +23 -3
- package/dist/core/session.d.ts +16 -2
- package/dist/core/session.js +3 -0
- package/dist/core/store-contracts/checkpoint-store-contract.d.ts +0 -12
- package/dist/core/store-contracts/checkpoint-store-contract.js +6 -7
- package/dist/core/store-contracts/session-store-contract.js +39 -0
- package/dist/core/task-event.d.ts +21 -0
- package/dist/core/task-notification.d.ts +9 -1
- package/dist/core/task-notification.js +6 -0
- package/dist/core/task-registry-agent.d.ts +14 -2
- package/dist/core/task-registry-agent.js +45 -19
- package/dist/core/task-registry-shared.d.ts +26 -11
- package/dist/core/task-registry.d.ts +15 -9
- package/dist/core/task-registry.js +9 -10
- package/dist/core/task-spec-members.js +1 -1
- package/dist/core/task-spec.d.ts +20 -0
- package/dist/core/tool-policy.d.ts +14 -12
- package/dist/core/tool-policy.js +11 -10
- package/dist/core/tool-spec.d.ts +38 -19
- package/dist/core/types.d.ts +1 -1
- package/dist/core/untrusted-egress.d.ts +15 -4
- package/dist/core/untrusted-egress.js +23 -22
- package/dist/core/workspace-snapshot.d.ts +3 -1
- package/dist/core/workspace-snapshot.js +1 -0
- package/dist/engine/harness/types.d.ts +66 -1
- package/dist/engine/harness/types.js +108 -0
- package/dist/engine/session/import-validate.js +6 -1
- package/dist/engine/session/session.d.ts +24 -1
- package/dist/engine/session/session.js +45 -1
- package/dist/index.d.ts +7 -4
- package/dist/index.js +7 -4
- package/dist/internal/harness-types.d.ts +1 -1
- package/dist/orchestration/run-workflow-tool.d.ts +4 -1
- package/dist/orchestration/run-workflow-tool.js +1 -0
- package/dist/orchestration/workflow.d.ts +3 -0
- package/dist/orchestration/workflow.js +3 -1
- package/dist/stores/file/session-store.js +2 -1
- package/dist/tools/fs/bash-program-position.js +3 -3
- package/dist/tools/fs/program-read-model.d.ts +34 -10
- package/dist/tools/fs/program-read-model.js +37 -7
- package/dist/tools/fs/program-read-model.rows.js +13 -2
- package/dist/tools/fs/read-deny.d.ts +13 -1
- package/dist/tools/fs/read-deny.js +10 -2
- package/dist/tools/fs/removal-targets.js +3 -3
- package/dist/tools/fs/safety.d.ts +2 -0
- package/dist/tools/fs/safety.js +1 -1
- package/dist/tools/fs/shell-read-argv.d.ts +8 -1
- package/dist/tools/fs/shell-read-argv.js +34 -29
- package/dist/tools/fs/shell-read-bindings.d.ts +2 -0
- package/dist/tools/fs/shell-read-bindings.js +5 -1
- package/dist/tools/fs/shell-read-brace.d.ts +18 -0
- package/dist/tools/fs/shell-read-brace.js +79 -0
- package/dist/tools/fs/shell-read-check.js +10 -8
- package/dist/tools/fs/shell-read-establish.js +23 -16
- package/dist/tools/fs/shell-read-judge.d.ts +11 -0
- package/dist/tools/fs/shell-read-judge.js +3 -3
- package/dist/tools/fs/shell-read-plan.d.ts +10 -0
- package/dist/tools/fs/shell-read-plan.js +189 -93
- package/dist/tools/fs/shell-read-start.d.ts +19 -9
- package/dist/tools/fs/shell-read-start.js +20 -12
- package/dist/tools/fs/shell-read-word.d.ts +10 -0
- package/dist/tools/fs/shell-read-word.js +9 -5
- package/dist/tools/fs/shell-read-workdir.d.ts +26 -7
- package/dist/tools/fs/shell-read-workdir.js +73 -12
- package/dist/tools/fs/shell-read-write-targets.js +13 -8
- package/dist/tools/worktree.d.ts +3 -2
- package/dist/tools/worktree.js +7 -35
- package/package.json +3 -2
- package/test/export-surface.snapshot.json +73 -11
- package/wire-changes/core-stores-sql@7.63.0.json +30 -0
- package/wire-changes/core@7.63.0.json +3523 -0
- package/wire-changes/mods-kernel@7.63.0.json +30 -0
- package/wire-changes/primitives@7.63.0.json +445 -0
- package/dist/core/credential-vocabulary.d.ts +0 -176
- package/dist/core/credential-vocabulary.js +0 -190
- package/dist/core/redaction-passes.d.ts +0 -82
- package/dist/core/redaction-passes.js +0 -377
|
@@ -10,16 +10,17 @@ import { formatMemoryAge } from "../memory-recall.js";
|
|
|
10
10
|
import { committedDistilledOf, committedOriginOf, computeEntryRev, consolidationExposedFrontmatter, distilledEquals, hasDistilledFormExtra, hasOriginFormExtra, isValidEntryId, originEquals, parseEntryFile, serializeEntryFile, stripModelWrittenDistilled, stripModelWrittenOrigin, UNCLOSED_MEMORY_FILE_REASON } from "./frontmatter.js";
|
|
11
11
|
import { MEMORY_PROVENANCE_RECALL_SENTENCE, memoryExposureIndexRow, parseMemoryExposureIndexRow } from "./provenance-wording.js";
|
|
12
12
|
import { isInstructionEntry } from "./header-hints.js";
|
|
13
|
-
import { ConsolidationRefusedError, CONSOLIDATION_WRITE_REFUSED_CODE, CONSOLIDATION_FORCE_THROTTLE_FLOOR_MS, CONSOLIDATION_SEAT_FREEZE_GRACE_MS, consolidationTypeEligible, deriveProductSlug, deriveSupersededSet,
|
|
13
|
+
import { ConsolidationRefusedError, CONSOLIDATION_WRITE_REFUSED_CODE, CONSOLIDATION_FORCE_THROTTLE_FLOOR_MS, CONSOLIDATION_SEAT_FREEZE_GRACE_MS, consolidationTypeEligible, deriveProductSlug, deriveSupersededSet, fileGovernanceStore, fileGovernanceStoreDir, isValidConsolidationPlanId, productAddPatch, recordActiveSetBaseline, recordConsolidationSession, sessionsSinceExcludingCurrent, scanRefusalCeiling, screenConsolidationOptions, memoryGovernanceDurability, supersessionFuseCeiling, deletionFuseCeiling, MEMORY_CHALLENGED_ENTRY_GUIDANCE, } from "./consolidation.js";
|
|
14
14
|
import { deriveShadowedSet } from "./shadowing.js";
|
|
15
15
|
import { classifySessionSettlements, clearHoldResolution, closeSessionAccount, disposeHold, effectiveSettlements, expireOverdueSettlements, foreignDanglingSessionAccounts, openLayerWriters, terminateSessionAccount, sessionAccountLeg, markHoldReleased, openInstructionHold, openSessionAccount, readHoldCustody, readHolds, reconcileHolds, replayExternalSettlementEffects, resolveHoldRecord, resolveSessionAccountRecord, resolveSettlementRecord, sessionSettlements, sessionUnattributedSet, emptyUnattributedSet, convertV1SidecarKeys, } from "./delegation-settlement.js";
|
|
16
16
|
import { noReplaceRestore, retireStagingToCustody, stagingPathBeside } from "./delegation-settlement.js";
|
|
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 { carriesEntry } from "./entry-scan.js";
|
|
20
21
|
import { DEFAULT_MAX_ENTRY_DEPTH, MEMORY_INDEX_FILENAME, captureErasureInput, erasureRequestInvalid, erasureScopeRefusal, erasureSelectHash, erasureStatusOf, sweepUnderErasureRecord, scanEntryFiles, } from "./file-backend.js";
|
|
21
22
|
import { assembleMemoryExportBundle, canonicalExpectedScopes, computeMemoryBundleHash, expectedScopesInvalid, firstOutsideExpectedScopes, memoryBundleInvalid, } from "./export-bundle.js";
|
|
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
|
+
import { QUARANTINE_DIR, SCAN_FUSE_THRESHOLD, quarantineAndTombstone, restoreContainedBaseline, mintStagingName, indexLineIsStale, readIndexRevs, writeIndexRevs, bumpScanFuse, scanFuseKey, canonicalize, anchorStoreRoot, landingPathOf, landsOutsideStore, storeActRefusal, storeRootMovedRefusal, adoptRootScopeLayout, clearScanFuse, effectiveControlRootOf, sayAnnounceFailure, drainMemoryAnnouncements, enqueueMemoryAnnouncement, ensureDirExists, markSessionPolluted, readSessionPollution, fileSessionCaptureRecordStore, recordRetrievedAccount, writeFileNoFollow, writeFileNoReplace, withPermissionRemedy, permissionRemedyOf, readRetrievedAccount, registerScope, registeredScopes, scopeDirFor, appendChallengeEvents, appendLineageAudit, rebuildStrictControlPlaneLedger, isStrictControlPlaneLedgerCorrupt, CHALLENGE_LEDGER_MAX_EVENTS, adjudicateLineagePending, challengedEntryIds, clearLineageForEntries, discardLineagePending, lineageAccountOfEntry, lineageContributionsOfSession, lineageLatchedIds, promoteLineagePending, readChallengeEvents, readChallengedHistory, readLineageRecord, recordChallengedHistory, recordLineageCredential, reconcileLineage, readProjectionDebts, resolveChallengeEvent, settleProjectionDebts, stageLineagePending, stageProjectionDebts, } from "./layout.js";
|
|
23
24
|
import { createSessionIncarnationSeat, recalledForAnyIdentity, recalledForIdentity, rememberForIdentity } from "./session-incarnation.js";
|
|
24
25
|
import { scanMemoryFileName, scanMemoryWrite, scanRemediation } from "./scan.js";
|
|
25
26
|
import { writableRootOf } from "./types.js";
|
|
@@ -190,6 +191,52 @@ export function pollutionContainmentCounts(report) {
|
|
|
190
191
|
escalated: distinct((report.quarantineFailures ?? []).map((q) => q.path)),
|
|
191
192
|
};
|
|
192
193
|
}
|
|
194
|
+
export function harvestConflictSentence(c) {
|
|
195
|
+
const what = c.op === "delete" ? "deletion" : "version";
|
|
196
|
+
return c.baseRev !== undefined && c.currentRev !== undefined
|
|
197
|
+
? `concurrent-change conflict (${c.op}) for entry id ${quoteId(c.id)} — this session's ${what} was NOT committed${c.op === "delete" ? "; the entry stays" : ""}`
|
|
198
|
+
: `write refused (${c.op}) for entry id ${quoteId(c.id)} — the memory store refused this change; this session's ${what} was NOT committed`;
|
|
199
|
+
}
|
|
200
|
+
function harvestRefusedReason(incident) {
|
|
201
|
+
switch (incident.kind) {
|
|
202
|
+
case "mass_deletion":
|
|
203
|
+
return inlineUntrusted(incident.detail, 300);
|
|
204
|
+
case "dir_missing":
|
|
205
|
+
return "the memory directory of this session's write scope is gone";
|
|
206
|
+
case "sidecar_corrupt":
|
|
207
|
+
return "the memory store's control plane could not be read or written (the host's logs carry the detail)";
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
export function memoryHarvestRefusedNotice(report, sessionId) {
|
|
211
|
+
if (report.ok || report.incident === undefined)
|
|
212
|
+
return undefined;
|
|
213
|
+
const committed = report.patches.add + report.patches.update + report.patches.delete;
|
|
214
|
+
return {
|
|
215
|
+
code: "memory.harvest_refused",
|
|
216
|
+
message: `Memory harvest refused (${report.incident.kind}): ${harvestRefusedReason(report.incident)}. ` +
|
|
217
|
+
(committed === 0
|
|
218
|
+
? `No memory change from this session was saved — anything this session said it remembered or forgot did not take effect.`
|
|
219
|
+
: `Only ${committed} change(s) from the other memory plane were saved; the refused plane's changes did not take effect.`),
|
|
220
|
+
detail: { kind: report.incident.kind, committed, ...(report.reportId !== undefined ? { reportId: report.reportId } : {}), ...(sessionId !== undefined ? { sessionId } : {}) },
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
export function memoryHarvestConflictNotice(report, sessionId) {
|
|
224
|
+
if (report.conflicts.length === 0)
|
|
225
|
+
return undefined;
|
|
226
|
+
const shown = report.conflicts.slice(0, 3).map(harvestConflictSentence);
|
|
227
|
+
const more = report.conflicts.length - shown.length;
|
|
228
|
+
return {
|
|
229
|
+
code: "memory.harvest_conflict",
|
|
230
|
+
message: `Memory harvest could not commit ${report.conflicts.length} change(s) from this session — those changes did not take effect: ` +
|
|
231
|
+
`${shown.join("; ")}${more > 0 ? `; …and ${more} more` : ""}.`,
|
|
232
|
+
detail: {
|
|
233
|
+
count: report.conflicts.length,
|
|
234
|
+
conflicts: report.conflicts.slice(0, 20).map((c) => ({ op: c.op, id: inlineUntrusted(c.id, 120), concurrent: c.baseRev !== undefined && c.currentRev !== undefined })),
|
|
235
|
+
...(report.reportId !== undefined ? { reportId: report.reportId } : {}),
|
|
236
|
+
...(sessionId !== undefined ? { sessionId } : {}),
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
}
|
|
193
240
|
export function memoryHarvestQuarantinedNotice(input) {
|
|
194
241
|
const reason = input.reason !== undefined ? inlineUntrusted(input.reason, 200) : undefined;
|
|
195
242
|
const reportId = input.reportId !== undefined ? { reportId: input.reportId } : {};
|
|
@@ -325,6 +372,7 @@ export class MemoryEngine {
|
|
|
325
372
|
backend;
|
|
326
373
|
memoryDir;
|
|
327
374
|
controlDir;
|
|
375
|
+
governanceStore;
|
|
328
376
|
now;
|
|
329
377
|
perScopeBudgetBytes;
|
|
330
378
|
perFileBytes;
|
|
@@ -356,6 +404,7 @@ export class MemoryEngine {
|
|
|
356
404
|
}
|
|
357
405
|
this.zeroCopy = typeof pinned === "string" && pinned !== "";
|
|
358
406
|
this.controlDir = effectiveControlRootOf(o.memoryDir, { pinnedBy: o.backend, controlDir: o.controlDir, onIncident: o.onIncident });
|
|
407
|
+
this.governanceStore = o.backend.governance ?? fileGovernanceStore(this.controlDir);
|
|
359
408
|
this.now = o.now ?? Date.now;
|
|
360
409
|
this.perScopeBudgetBytes = o.perScopeBudgetBytes ?? Number.MAX_SAFE_INTEGER;
|
|
361
410
|
this.perFileBytes = o.perFileBytes ?? MAX_MEMORY_BYTES;
|
|
@@ -372,7 +421,7 @@ export class MemoryEngine {
|
|
|
372
421
|
this.holdSettleTimeoutMs = o.holdSettleTimeoutMs ?? DEFAULT_HOLD_SETTLE_TIMEOUT_MS;
|
|
373
422
|
this.onIncident = o.onIncident;
|
|
374
423
|
if (o.consolidation !== undefined) {
|
|
375
|
-
this.consolidation = screenConsolidationOptions(o.consolidation, this.provenance);
|
|
424
|
+
this.consolidation = screenConsolidationOptions(o.consolidation, this.provenance, memoryGovernanceDurability(o.backend));
|
|
376
425
|
}
|
|
377
426
|
this.captureRecords = o.captureRecordStore ?? fileSessionCaptureRecordStore(this.controlDir);
|
|
378
427
|
this.incarnation = createSessionIncarnationSeat({ resolve: o.sessionIncarnation, onIncident: o.onIncident });
|
|
@@ -382,12 +431,12 @@ export class MemoryEngine {
|
|
|
382
431
|
return this.provenance;
|
|
383
432
|
}
|
|
384
433
|
consolidation;
|
|
385
|
-
endSessionLeg(handle, sessionId) {
|
|
386
|
-
return handle.accountLeg !== undefined ? this.endLeg(sessionId, handle.accountLeg) : undefined;
|
|
434
|
+
async endSessionLeg(handle, sessionId) {
|
|
435
|
+
return handle.accountLeg !== undefined ? await this.endLeg(sessionId, handle.accountLeg) : undefined;
|
|
387
436
|
}
|
|
388
|
-
endLeg(sessionId, leg) {
|
|
437
|
+
async endLeg(sessionId, leg) {
|
|
389
438
|
try {
|
|
390
|
-
return terminateSessionAccount(this.controlDir, { sessionId, now: this.now, leg });
|
|
439
|
+
return await terminateSessionAccount(this.governanceStore, this.controlDir, { sessionId, now: this.now, leg });
|
|
391
440
|
}
|
|
392
441
|
catch (err) {
|
|
393
442
|
this.discloseSessionAccountIncident("the end of this session's leg could not be recorded — it keeps reading as a live writer of its layer (read-only restores there are withheld)", err);
|
|
@@ -453,7 +502,7 @@ export class MemoryEngine {
|
|
|
453
502
|
if (committed === undefined)
|
|
454
503
|
continue;
|
|
455
504
|
try {
|
|
456
|
-
writers ??= openLayerWriters(this.controlDir, selfLeg);
|
|
505
|
+
writers ??= await openLayerWriters(this.governanceStore, this.controlDir, selfLeg);
|
|
457
506
|
}
|
|
458
507
|
catch {
|
|
459
508
|
writers = { scopes: new Set(), unscoped: 1 };
|
|
@@ -610,29 +659,34 @@ export class MemoryEngine {
|
|
|
610
659
|
return "undecidable";
|
|
611
660
|
}
|
|
612
661
|
}
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
662
|
+
async committedPlacementOf(id) {
|
|
663
|
+
if (this.zeroCopy) {
|
|
664
|
+
const row = await this.committedRowOf(id);
|
|
665
|
+
if (row === undefined)
|
|
666
|
+
return { state: "absent" };
|
|
667
|
+
return row === "undecidable" || row.scope === undefined ? { state: "unknown" } : { state: "held", scope: row.scope };
|
|
668
|
+
}
|
|
620
669
|
try {
|
|
621
|
-
const
|
|
622
|
-
|
|
623
|
-
err.code = "memory.announce_failed";
|
|
624
|
-
sink(err);
|
|
670
|
+
const [entry] = await this.backend.getByIds([id]);
|
|
671
|
+
return entry === undefined || entry.id !== id ? { state: "absent" } : { state: "held", scope: entry.scope, frontmatter: entry.frontmatter };
|
|
625
672
|
}
|
|
626
673
|
catch {
|
|
674
|
+
return { state: "unknown" };
|
|
627
675
|
}
|
|
628
676
|
}
|
|
629
|
-
|
|
677
|
+
writeFailureText(err) {
|
|
678
|
+
return thrownText(withPermissionRemedy(err, this.memoryDir));
|
|
679
|
+
}
|
|
680
|
+
discloseAnnounceFailure(stage, cause) {
|
|
681
|
+
sayAnnounceFailure(this.onIncident, stage, cause);
|
|
682
|
+
}
|
|
683
|
+
async announce(scope, ann, stage, opts) {
|
|
630
684
|
if (scope === undefined || scope === null || scope === "") {
|
|
631
685
|
this.withholdAnnouncement(stage, ann.items);
|
|
632
686
|
return;
|
|
633
687
|
}
|
|
634
688
|
try {
|
|
635
|
-
enqueueMemoryAnnouncement(this.
|
|
689
|
+
await enqueueMemoryAnnouncement(this.governanceStore, { ...ann, scope }, opts);
|
|
636
690
|
}
|
|
637
691
|
catch (err) {
|
|
638
692
|
this.discloseAnnounceFailure(stage, err);
|
|
@@ -655,10 +709,10 @@ export class MemoryEngine {
|
|
|
655
709
|
catch {
|
|
656
710
|
}
|
|
657
711
|
}
|
|
658
|
-
announceByScope(byScope, ann, stage) {
|
|
712
|
+
async announceByScope(byScope, ann, stage) {
|
|
659
713
|
for (const [scope, items] of byScope)
|
|
660
714
|
if (items.length > 0)
|
|
661
|
-
this.announce(scope, { ...ann, items: capItems(items) }, stage);
|
|
715
|
+
await this.announce(scope, { ...ann, items: capItems(items) }, stage);
|
|
662
716
|
}
|
|
663
717
|
scopeOwningPath(absPath, candidates) {
|
|
664
718
|
try {
|
|
@@ -720,7 +774,8 @@ export class MemoryEngine {
|
|
|
720
774
|
}
|
|
721
775
|
}
|
|
722
776
|
pollutedSessions = new Map();
|
|
723
|
-
|
|
777
|
+
pendingPollutionSweeps = new Set();
|
|
778
|
+
async markSessionPolluted(sessionId, reason, cause) {
|
|
724
779
|
const incarnation = this.incarnation.incarnationOf(sessionId);
|
|
725
780
|
rememberForIdentity(this.pollutedSessions, sessionId, { at: this.now(), reason, ...(cause !== undefined ? { cause } : {}), ...(incarnation !== undefined ? { incarnation } : {}) });
|
|
726
781
|
let outcome;
|
|
@@ -742,28 +797,37 @@ export class MemoryEngine {
|
|
|
742
797
|
}
|
|
743
798
|
}
|
|
744
799
|
}
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
800
|
+
const sweep = (async () => {
|
|
801
|
+
try {
|
|
802
|
+
const rec = this.sessionPollution(sessionId) ?? { at: this.now(), reason };
|
|
803
|
+
const swept = await lineageContributionsOfSession(this.governanceStore, sessionId, incarnation);
|
|
804
|
+
const contributions = this.provenance === "carry" ? swept.filter((c) => c.marked !== true) : swept;
|
|
805
|
+
await this.challengeAndAnnounce(contributions.map((c) => ({
|
|
806
|
+
eventId: `pollution:${sessionId}:${rec.at}:${c.entryId}`,
|
|
807
|
+
entryId: c.entryId,
|
|
808
|
+
reason: `challenged: the contributing session was marked polluted`,
|
|
809
|
+
challengedRev: c.lastRev,
|
|
810
|
+
})));
|
|
811
|
+
}
|
|
812
|
+
catch (err) {
|
|
813
|
+
const sink = this.onIncident;
|
|
814
|
+
if (sink !== undefined) {
|
|
815
|
+
try {
|
|
816
|
+
const e = new Error(`memory challenge sweep for a polluted session failed: ${thrownText(err)}`);
|
|
817
|
+
e.code = "memory.challenge_sweep_failed";
|
|
818
|
+
sink(e);
|
|
819
|
+
}
|
|
820
|
+
catch {
|
|
821
|
+
}
|
|
765
822
|
}
|
|
766
823
|
}
|
|
824
|
+
})();
|
|
825
|
+
this.pendingPollutionSweeps.add(sweep);
|
|
826
|
+
try {
|
|
827
|
+
await sweep;
|
|
828
|
+
}
|
|
829
|
+
finally {
|
|
830
|
+
this.pendingPollutionSweeps.delete(sweep);
|
|
767
831
|
}
|
|
768
832
|
return outcome;
|
|
769
833
|
}
|
|
@@ -785,20 +849,20 @@ export class MemoryEngine {
|
|
|
785
849
|
const incarnation = this.incarnation.incarnationOf(sessionId);
|
|
786
850
|
const record = { at: this.now(), reason, ...(incarnation !== undefined ? { incarnation } : {}) };
|
|
787
851
|
if (this.consolidation !== undefined) {
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
852
|
+
return this.governanceStore
|
|
853
|
+
.transact([], (file) => {
|
|
854
|
+
const rows = Object.values(file.scopes);
|
|
855
|
+
if (rows.length === 0)
|
|
856
|
+
return { writeGate: false, result: undefined };
|
|
857
|
+
for (const row of rows)
|
|
858
|
+
row.epoch += 1;
|
|
859
|
+
return { writeGate: true, result: undefined };
|
|
860
|
+
})
|
|
861
|
+
.then(() => this.markCaptureRecord(sessionId, record), () => "unpersisted");
|
|
862
|
+
}
|
|
863
|
+
return this.markCaptureRecord(sessionId, record);
|
|
864
|
+
}
|
|
865
|
+
markCaptureRecord(sessionId, record) {
|
|
802
866
|
let raw;
|
|
803
867
|
try {
|
|
804
868
|
raw = this.captureRecords.mark(sessionId, record);
|
|
@@ -958,18 +1022,20 @@ export class MemoryEngine {
|
|
|
958
1022
|
return out;
|
|
959
1023
|
}
|
|
960
1024
|
retrievedThisSession = new Set();
|
|
961
|
-
readChallengeExclusions() {
|
|
1025
|
+
async readChallengeExclusions() {
|
|
1026
|
+
if (this.pendingPollutionSweeps.size > 0)
|
|
1027
|
+
await Promise.all(this.pendingPollutionSweeps);
|
|
962
1028
|
const out = new Map();
|
|
963
|
-
for (const id of lineageLatchedIds(this.
|
|
1029
|
+
for (const id of await lineageLatchedIds(this.governanceStore))
|
|
964
1030
|
out.set(id, { code: "lineage_pending" });
|
|
965
|
-
for (const [id, g] of challengedEntryIds(this.
|
|
1031
|
+
for (const [id, g] of await challengedEntryIds(this.governanceStore))
|
|
966
1032
|
out.set(id, { code: "challenged", generation: g.generation, at: g.at });
|
|
967
1033
|
return out;
|
|
968
1034
|
}
|
|
969
|
-
challengeAndAnnounce(events) {
|
|
1035
|
+
async challengeAndAnnounce(events) {
|
|
970
1036
|
if (events.length === 0)
|
|
971
1037
|
return [];
|
|
972
|
-
const { assignments: assigned, eventCount } = appendChallengeEvents(this.
|
|
1038
|
+
const { assignments: assigned, eventCount } = await appendChallengeEvents(this.governanceStore, events, this.now);
|
|
973
1039
|
if (eventCount !== undefined)
|
|
974
1040
|
this.discloseChallengeLedgerSize(eventCount);
|
|
975
1041
|
const fresh = assigned.filter((a) => !a.replayed);
|
|
@@ -978,7 +1044,7 @@ export class MemoryEngine {
|
|
|
978
1044
|
return assigned;
|
|
979
1045
|
}
|
|
980
1046
|
announceChallenged(fresh, at) {
|
|
981
|
-
const line = (a) => `memory entry challenged: id ${quoteId(a.entryId)} (generation ${a.generation}) — its content is withheld from memory tools and the injected index.
|
|
1047
|
+
const line = (a) => `memory entry challenged: id ${quoteId(a.entryId)} (generation ${a.generation}) — its content is withheld from memory tools and the injected index. ${MEMORY_CHALLENGED_ENTRY_GUIDANCE}`;
|
|
982
1048
|
const work = async () => {
|
|
983
1049
|
let timer;
|
|
984
1050
|
const answered = await Promise.race([
|
|
@@ -1005,7 +1071,7 @@ export class MemoryEngine {
|
|
|
1005
1071
|
byScope.set(scope, items);
|
|
1006
1072
|
}
|
|
1007
1073
|
for (const [scope, items] of byScope)
|
|
1008
|
-
this.announce(scope, { kind: "gate", at, items }, "challenge enqueue");
|
|
1074
|
+
await this.announce(scope, { kind: "gate", at, items }, "challenge enqueue");
|
|
1009
1075
|
};
|
|
1010
1076
|
this.announcementWork = this.announcementWork.then(work).catch((err) => {
|
|
1011
1077
|
this.discloseAnnounceFailure("challenge attribution", err);
|
|
@@ -1031,13 +1097,19 @@ export class MemoryEngine {
|
|
|
1031
1097
|
catch {
|
|
1032
1098
|
}
|
|
1033
1099
|
}
|
|
1034
|
-
rebuildCorruptControlPlaneLedger(ledger, reason) {
|
|
1100
|
+
async rebuildCorruptControlPlaneLedger(ledger, reason) {
|
|
1035
1101
|
if (typeof reason !== "string" || reason.trim() === "") {
|
|
1036
1102
|
const e = new Error("rebuildCorruptControlPlaneLedger: a non-empty reason is required (this call drops taint evidence — it is recorded, never implicit)");
|
|
1037
1103
|
e.code = "config.memory_rebuild_reason";
|
|
1038
1104
|
throw e;
|
|
1039
1105
|
}
|
|
1040
|
-
|
|
1106
|
+
const ledgerDir = fileGovernanceStoreDir(this.governanceStore);
|
|
1107
|
+
if (ledgerDir === undefined) {
|
|
1108
|
+
const e = new Error("rebuildCorruptControlPlaneLedger: this engine's governance documents are kept by its backend's store, not as control-plane files — the rebuild (which quarantines the corrupt file's bytes beside it) is the File store's recovery; recover the document through the store that keeps it.");
|
|
1109
|
+
e.code = "memory.control_plane_rebuild_unsupported";
|
|
1110
|
+
throw e;
|
|
1111
|
+
}
|
|
1112
|
+
if (!isStrictControlPlaneLedgerCorrupt(ledgerDir, ledger)) {
|
|
1041
1113
|
const e = new Error(`${ledger === "challenges" ? "memory challenge ledger" : "memory lineage ledger"} reads cleanly — refusing to rebuild it. This call exists to recover a ledger whose corruption already refuses every read; resetting a healthy one would drop taint evidence that is still doing its job.`);
|
|
1042
1114
|
e.code = "memory.control_plane_not_corrupt";
|
|
1043
1115
|
throw e;
|
|
@@ -1055,13 +1127,13 @@ export class MemoryEngine {
|
|
|
1055
1127
|
if (everyScope.length === 0)
|
|
1056
1128
|
this.withholdAnnouncement("ledger reset", [resetLine]);
|
|
1057
1129
|
for (const scope of everyScope)
|
|
1058
|
-
enqueueMemoryAnnouncement(this.
|
|
1130
|
+
await enqueueMemoryAnnouncement(this.governanceStore, {
|
|
1059
1131
|
kind: "gate",
|
|
1060
1132
|
scope,
|
|
1061
1133
|
at: this.now(),
|
|
1062
1134
|
items: [resetLine],
|
|
1063
1135
|
});
|
|
1064
|
-
const receipt = rebuildStrictControlPlaneLedger(
|
|
1136
|
+
const receipt = rebuildStrictControlPlaneLedger(ledgerDir, ledger, this.now);
|
|
1065
1137
|
const sink = this.onIncident;
|
|
1066
1138
|
if (sink !== undefined) {
|
|
1067
1139
|
try {
|
|
@@ -1076,13 +1148,13 @@ export class MemoryEngine {
|
|
|
1076
1148
|
}
|
|
1077
1149
|
return receipt;
|
|
1078
1150
|
}
|
|
1079
|
-
challengeEntries(ids, reason, requestId) {
|
|
1151
|
+
async challengeEntries(ids, reason, requestId) {
|
|
1080
1152
|
if (typeof requestId !== "string" || requestId === "") {
|
|
1081
1153
|
const e = new Error("challengeEntries: requestId is required (idempotency identity — retries must reuse it; the engine does not mint one)");
|
|
1082
1154
|
e.code = "config.memory_challenge_request";
|
|
1083
1155
|
throw e;
|
|
1084
1156
|
}
|
|
1085
|
-
return this.challengeAndAnnounce(ids.map((entryId) => ({ eventId: `${requestId}:${entryId}`, entryId, reason })));
|
|
1157
|
+
return await this.challengeAndAnnounce(ids.map((entryId) => ({ eventId: `${requestId}:${entryId}`, entryId, reason })));
|
|
1086
1158
|
}
|
|
1087
1159
|
async eraseMemoryEntries(input) {
|
|
1088
1160
|
try {
|
|
@@ -1131,7 +1203,7 @@ export class MemoryEngine {
|
|
|
1131
1203
|
resolvedIds = [...new Set(headers.map((h) => h.id))].sort();
|
|
1132
1204
|
}
|
|
1133
1205
|
else {
|
|
1134
|
-
resolvedIds = lineageContributionsOfSession(this.
|
|
1206
|
+
resolvedIds = (await lineageContributionsOfSession(this.governanceStore, select.sessionId))
|
|
1135
1207
|
.map((c) => c.entryId)
|
|
1136
1208
|
.sort();
|
|
1137
1209
|
}
|
|
@@ -1144,7 +1216,7 @@ export class MemoryEngine {
|
|
|
1144
1216
|
const report = patches.length > 0 ? await this.backend.applyPatches(patches) : { applied: [], conflicts: [] };
|
|
1145
1217
|
const appliedDeletes = new Set(report.applied.filter((a) => a.op === "delete").map((a) => a.id));
|
|
1146
1218
|
if (appliedDeletes.size > 0)
|
|
1147
|
-
clearLineageForEntries(this.
|
|
1219
|
+
await clearLineageForEntries(this.governanceStore, [...appliedDeletes]);
|
|
1148
1220
|
const erased = [...appliedDeletes].map((id) => {
|
|
1149
1221
|
const e = byId.get(id);
|
|
1150
1222
|
return {
|
|
@@ -1223,7 +1295,7 @@ export class MemoryEngine {
|
|
|
1223
1295
|
}
|
|
1224
1296
|
catch (err) {
|
|
1225
1297
|
if (uncleared.length > 0)
|
|
1226
|
-
this.discloseUncarriedIndexResidue(err, uncleared, indexScope);
|
|
1298
|
+
await this.discloseUncarriedIndexResidue(err, uncleared, indexScope);
|
|
1227
1299
|
return att;
|
|
1228
1300
|
}
|
|
1229
1301
|
}
|
|
@@ -1288,7 +1360,7 @@ export class MemoryEngine {
|
|
|
1288
1360
|
}
|
|
1289
1361
|
}
|
|
1290
1362
|
}
|
|
1291
|
-
discloseUncarriedIndexResidue(err, uncleared, indexScope) {
|
|
1363
|
+
async discloseUncarriedIndexResidue(err, uncleared, indexScope) {
|
|
1292
1364
|
try {
|
|
1293
1365
|
let relayed;
|
|
1294
1366
|
try {
|
|
@@ -1307,7 +1379,7 @@ export class MemoryEngine {
|
|
|
1307
1379
|
pathsByScope.set(scope, [...(pathsByScope.get(scope) ?? []), String(p)]);
|
|
1308
1380
|
}
|
|
1309
1381
|
for (const [scope, paths] of pathsByScope)
|
|
1310
|
-
this.announce(scope, { kind: "external", at: this.now(), items: [lineOf(paths)] }, "erasure index-residue enqueue");
|
|
1382
|
+
await this.announce(scope, { kind: "external", at: this.now(), items: [lineOf(paths)] }, "erasure index-residue enqueue");
|
|
1311
1383
|
const sink = this.onIncident;
|
|
1312
1384
|
if (sink !== undefined) {
|
|
1313
1385
|
try {
|
|
@@ -1325,9 +1397,9 @@ export class MemoryEngine {
|
|
|
1325
1397
|
async provenanceOf(entryId) {
|
|
1326
1398
|
const snapshotFn = this.backend.committedSnapshotOf;
|
|
1327
1399
|
const custodyFn = this.backend.custodyOf;
|
|
1328
|
-
const readEnginePlane = () => {
|
|
1329
|
-
const challenged = challengedEntryIds(this.
|
|
1330
|
-
const lineage = lineageAccountOfEntry(this.
|
|
1400
|
+
const readEnginePlane = async () => {
|
|
1401
|
+
const challenged = (await challengedEntryIds(this.governanceStore)).get(entryId);
|
|
1402
|
+
const lineage = await lineageAccountOfEntry(this.governanceStore, entryId);
|
|
1331
1403
|
return {
|
|
1332
1404
|
contributors: lineage.contributors.map((c) => {
|
|
1333
1405
|
const polluted = this.sessionPollution(c.sessionId);
|
|
@@ -1382,7 +1454,7 @@ export class MemoryEngine {
|
|
|
1382
1454
|
`every snapshot/custody bracket — the two faces cannot be joined without tearing while the store is this hot; retry later.`);
|
|
1383
1455
|
}
|
|
1384
1456
|
}
|
|
1385
|
-
const plane = readEnginePlane();
|
|
1457
|
+
const plane = await readEnginePlane();
|
|
1386
1458
|
let binding;
|
|
1387
1459
|
let contentState;
|
|
1388
1460
|
let ingest;
|
|
@@ -1516,31 +1588,31 @@ export class MemoryEngine {
|
|
|
1516
1588
|
};
|
|
1517
1589
|
return face.call(this.backend, plan);
|
|
1518
1590
|
}
|
|
1519
|
-
challengeSession(sessionId, reason, requestId) {
|
|
1591
|
+
async challengeSession(sessionId, reason, requestId) {
|
|
1520
1592
|
if (typeof requestId !== "string" || requestId === "") {
|
|
1521
1593
|
const e = new Error("challengeSession: requestId is required (idempotency identity — retries must reuse it; the engine does not mint one)");
|
|
1522
1594
|
e.code = "config.memory_challenge_request";
|
|
1523
1595
|
throw e;
|
|
1524
1596
|
}
|
|
1525
|
-
const contributions = lineageContributionsOfSession(this.
|
|
1526
|
-
return this.challengeAndAnnounce(contributions.map((c) => ({ eventId: `${requestId}:${c.entryId}`, entryId: c.entryId, reason, challengedRev: c.lastRev })));
|
|
1597
|
+
const contributions = await lineageContributionsOfSession(this.governanceStore, sessionId);
|
|
1598
|
+
return await this.challengeAndAnnounce(contributions.map((c) => ({ eventId: `${requestId}:${c.entryId}`, entryId: c.entryId, reason, challengedRev: c.lastRev })));
|
|
1527
1599
|
}
|
|
1528
|
-
resolveChallenge(entryId, generation, reason, requestId) {
|
|
1529
|
-
const { resolved, eventCount } = resolveChallengeEvent(this.
|
|
1600
|
+
async resolveChallenge(entryId, generation, reason, requestId) {
|
|
1601
|
+
const { resolved, eventCount } = await resolveChallengeEvent(this.governanceStore, entryId, generation, reason, this.now, requestId);
|
|
1530
1602
|
if (eventCount !== undefined)
|
|
1531
1603
|
this.discloseChallengeLedgerSize(eventCount);
|
|
1532
1604
|
return resolved;
|
|
1533
1605
|
}
|
|
1534
|
-
adjudicatePendingLineage(txnId, action) {
|
|
1535
|
-
const promoted = adjudicateLineagePending(this.
|
|
1536
|
-
this.settlePromotions(promoted);
|
|
1606
|
+
async adjudicatePendingLineage(txnId, action) {
|
|
1607
|
+
const promoted = await adjudicateLineagePending(this.governanceStore, txnId, action, this.now);
|
|
1608
|
+
await this.settlePromotions(promoted);
|
|
1537
1609
|
return promoted;
|
|
1538
1610
|
}
|
|
1539
|
-
listPendingLineage() {
|
|
1540
|
-
return readLineageRecord(this.
|
|
1611
|
+
async listPendingLineage() {
|
|
1612
|
+
return (await readLineageRecord(this.governanceStore)).pending;
|
|
1541
1613
|
}
|
|
1542
|
-
readChallengeLedger() {
|
|
1543
|
-
return readChallengeEvents(this.
|
|
1614
|
+
async readChallengeLedger() {
|
|
1615
|
+
return await readChallengeEvents(this.governanceStore);
|
|
1544
1616
|
}
|
|
1545
1617
|
readChallengedHistoryAccount() {
|
|
1546
1618
|
try {
|
|
@@ -1550,7 +1622,7 @@ export class MemoryEngine {
|
|
|
1550
1622
|
return {};
|
|
1551
1623
|
}
|
|
1552
1624
|
}
|
|
1553
|
-
settlePromotions(promoted) {
|
|
1625
|
+
async settlePromotions(promoted) {
|
|
1554
1626
|
if (promoted.length === 0)
|
|
1555
1627
|
return;
|
|
1556
1628
|
const events = [];
|
|
@@ -1575,7 +1647,7 @@ export class MemoryEngine {
|
|
|
1575
1647
|
challengedRev: p.rev,
|
|
1576
1648
|
});
|
|
1577
1649
|
}
|
|
1578
|
-
this.challengeAndAnnounce(events);
|
|
1650
|
+
await this.challengeAndAnnounce(events);
|
|
1579
1651
|
try {
|
|
1580
1652
|
appendLineageAudit(this.controlDir, promoted.map((p) => ({ entryId: p.entryId, sessionId: p.sessionId, rev: p.rev, at: this.now() })));
|
|
1581
1653
|
}
|
|
@@ -1599,7 +1671,7 @@ export class MemoryEngine {
|
|
|
1599
1671
|
}
|
|
1600
1672
|
catch (err) {
|
|
1601
1673
|
if (legSeat.leg !== undefined && opts?.sessionId !== undefined)
|
|
1602
|
-
this.endLeg(opts.sessionId, legSeat.leg);
|
|
1674
|
+
await this.endLeg(opts.sessionId, legSeat.leg);
|
|
1603
1675
|
throw withPermissionRemedy(err, this.memoryDir);
|
|
1604
1676
|
}
|
|
1605
1677
|
}
|
|
@@ -1621,7 +1693,7 @@ export class MemoryEngine {
|
|
|
1621
1693
|
if (writeScope !== null && !scopeDirs.has(writeScope))
|
|
1622
1694
|
scopeDirs.set(writeScope, registerScope(this.memoryDir, this.controlDir, writeScope));
|
|
1623
1695
|
for (const [scope, dir] of scopeDirs)
|
|
1624
|
-
this.censusScopeTree(scope, dir);
|
|
1696
|
+
await this.censusScopeTree(scope, dir);
|
|
1625
1697
|
const layerBaseline = this.zeroCopy
|
|
1626
1698
|
? readOnlyLayerCensus([...scopeDirs].filter(([scope]) => scope !== writeScope).map(([, dir]) => dir))
|
|
1627
1699
|
: undefined;
|
|
@@ -1630,7 +1702,7 @@ export class MemoryEngine {
|
|
|
1630
1702
|
if (this.provenance === "carry" && opts?.sessionId !== undefined && writeScope !== null && !restricted) {
|
|
1631
1703
|
let accountFailed = false;
|
|
1632
1704
|
try {
|
|
1633
|
-
const foreign = foreignDanglingSessionAccounts(this.controlDir, opts.sessionId);
|
|
1705
|
+
const foreign = await foreignDanglingSessionAccounts(this.governanceStore, this.controlDir, opts.sessionId);
|
|
1634
1706
|
const unattributed = [];
|
|
1635
1707
|
if (foreign.length > 0) {
|
|
1636
1708
|
const wroot = canonicalize(scopeDirFor(this.memoryDir, this.controlDir, writeScope));
|
|
@@ -1724,34 +1796,28 @@ export class MemoryEngine {
|
|
|
1724
1796
|
else {
|
|
1725
1797
|
this.discloseSessionAccountIncident(`the crash-residue edit of ${rel} could not be captured to quarantine — the divergent bytes were left in place (the inbound gate adjudicates them)`, undefined);
|
|
1726
1798
|
}
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
],
|
|
1737
|
-
});
|
|
1738
|
-
}
|
|
1739
|
-
catch (annErr) {
|
|
1740
|
-
this.discloseAnnounceFailure("session-account residue", annErr);
|
|
1741
|
-
}
|
|
1799
|
+
await this.announce(writeScope, {
|
|
1800
|
+
kind: "external",
|
|
1801
|
+
at: this.now(),
|
|
1802
|
+
items: [
|
|
1803
|
+
restoredCommitted
|
|
1804
|
+
? `crash residue: an edit of committed memory entry ${quoteId(rel)} pre-existed this session under an unresolved foreign session account — its writer cannot be attributed, so the edit bytes were captured to control-plane quarantine (${captureName}) and the committed content restored (host review adjudicates the edit)`
|
|
1805
|
+
: `crash residue: an edit of committed memory entry ${quoteId(rel)} pre-existed this session under an unresolved foreign session account and could NOT be fully contained — the divergent/newer bytes were left in place (or preserved beside the capture) for the ordinary inbound gate`,
|
|
1806
|
+
],
|
|
1807
|
+
}, "session-account residue");
|
|
1742
1808
|
}
|
|
1743
1809
|
}
|
|
1744
1810
|
}
|
|
1745
1811
|
}
|
|
1746
1812
|
}
|
|
1747
1813
|
accountLeg = legSeat.leg = uuidv7();
|
|
1748
|
-
openSessionAccount(this.controlDir, { sessionId: opts.sessionId, now: this.now, unattributed, writeScope, leg: accountLeg });
|
|
1814
|
+
await openSessionAccount(this.governanceStore, this.controlDir, { sessionId: opts.sessionId, now: this.now, unattributed, writeScope, leg: accountLeg });
|
|
1749
1815
|
}
|
|
1750
1816
|
catch (err) {
|
|
1751
1817
|
accountFailed = true;
|
|
1752
1818
|
if (accountLeg !== undefined) {
|
|
1753
1819
|
try {
|
|
1754
|
-
if (sessionAccountLeg(this.controlDir, opts.sessionId) !== accountLeg)
|
|
1820
|
+
if (await sessionAccountLeg(this.governanceStore, this.controlDir, opts.sessionId) !== accountLeg)
|
|
1755
1821
|
accountLeg = legSeat.leg = undefined;
|
|
1756
1822
|
}
|
|
1757
1823
|
catch {
|
|
@@ -1782,7 +1848,6 @@ export class MemoryEngine {
|
|
|
1782
1848
|
baseRevs: new Map(),
|
|
1783
1849
|
baseIds: new Map(),
|
|
1784
1850
|
repoIndexTargets: new Map(),
|
|
1785
|
-
indexBaselineLines: 0,
|
|
1786
1851
|
indexText: "",
|
|
1787
1852
|
...(opts?.shadowNames !== undefined ? { shadowNames: opts.shadowNames } : {}),
|
|
1788
1853
|
...(restricted ? { adoptionRestricted: true } : {}),
|
|
@@ -1855,7 +1920,7 @@ export class MemoryEngine {
|
|
|
1855
1920
|
if (indexGate !== undefined) {
|
|
1856
1921
|
if (!indexGate.contained)
|
|
1857
1922
|
handle.indexOnDiskUntrusted = true;
|
|
1858
|
-
this.announce(handle.writeScope, {
|
|
1923
|
+
await this.announce(handle.writeScope, {
|
|
1859
1924
|
kind: "gate",
|
|
1860
1925
|
at: this.now(),
|
|
1861
1926
|
items: [`memory index rejected: ${inlineUntrusted(indexGate.rejection.reason)}`],
|
|
@@ -1863,17 +1928,16 @@ export class MemoryEngine {
|
|
|
1863
1928
|
}
|
|
1864
1929
|
const materializeHealWarnings = [];
|
|
1865
1930
|
const indexWrite = writeScope !== null && !restricted;
|
|
1866
|
-
const indexText = this.rebuildIndex(handle, headers, { write: indexWrite, ignoreOnDisk: indexGate !== undefined || restricted }, materializeHealWarnings);
|
|
1867
|
-
handle.indexBaselineLines = countIndexLines(indexText);
|
|
1931
|
+
const indexText = await this.rebuildIndex(handle, headers, { write: indexWrite, ignoreOnDisk: indexGate !== undefined || restricted }, materializeHealWarnings);
|
|
1868
1932
|
handle.indexText = indexText;
|
|
1869
1933
|
handle.markedEntriesPresent = headers.some((h) => h.exposure === "external");
|
|
1870
1934
|
if (this.consolidation !== undefined) {
|
|
1871
1935
|
try {
|
|
1872
|
-
const fuseExclusions = this.readChallengeExclusions();
|
|
1936
|
+
const fuseExclusions = await this.readChallengeExclusions();
|
|
1873
1937
|
const fuseSuperseded = deriveSupersededSet(headers, { excluded: (id) => fuseExclusions.has(id) });
|
|
1874
1938
|
for (const scope of new Set(headers.map((h) => h.scope))) {
|
|
1875
1939
|
const size = headers.filter((h) => h.scope === scope && !fuseSuperseded.has(h.id) && !fuseExclusions.has(h.id)).length;
|
|
1876
|
-
const rec = recordActiveSetBaseline(this.
|
|
1940
|
+
const rec = await recordActiveSetBaseline(this.governanceStore, scope, size, this.consolidation.supersedeRatioCap);
|
|
1877
1941
|
if (rec.collapsed !== undefined) {
|
|
1878
1942
|
const detail = `memory active set for scope ${inlineUntrusted(scope, 80)} collapsed ${rec.collapsed.from} → ${rec.collapsed.to} in one window (over the ${this.consolidation.supersedeRatioCap} ratio cap) — superseded entries stay readable as evidence; audit the recent consolidation plans and adoptions`;
|
|
1879
1943
|
const sink = this.onIncident;
|
|
@@ -1886,7 +1950,7 @@ export class MemoryEngine {
|
|
|
1886
1950
|
catch {
|
|
1887
1951
|
}
|
|
1888
1952
|
}
|
|
1889
|
-
this.announce(scope, { kind: "gate", at: this.now(), items: [detail] }, "active-set collapse enqueue");
|
|
1953
|
+
await this.announce(scope, { kind: "gate", at: this.now(), items: [detail] }, "active-set collapse enqueue");
|
|
1890
1954
|
}
|
|
1891
1955
|
}
|
|
1892
1956
|
}
|
|
@@ -1895,12 +1959,12 @@ export class MemoryEngine {
|
|
|
1895
1959
|
}
|
|
1896
1960
|
const indexRefusals = materializeHealWarnings.filter((w) => w.startsWith(INDEX_WRITE_REFUSED));
|
|
1897
1961
|
if (indexRefusals.length > 0) {
|
|
1898
|
-
this.announce(writeScope, { kind: "gate", at: this.now(), items: capItems(indexRefusals.map((w) => inlineUntrusted(w, 500))) }, "materialize index-write enqueue");
|
|
1962
|
+
await this.announce(writeScope, { kind: "gate", at: this.now(), items: capItems(indexRefusals.map((w) => inlineUntrusted(w, 500))) }, "materialize index-write enqueue");
|
|
1899
1963
|
}
|
|
1900
1964
|
if (indexWrite && handle.indexOnDiskUntrusted !== true) {
|
|
1901
1965
|
const dropNotes = materializeHealWarnings.filter((w) => w.includes("opaque external-origin handle"));
|
|
1902
1966
|
if (dropNotes.length > 0) {
|
|
1903
|
-
this.announce(writeScope, { kind: "gate", at: this.now(), items: capItems(dropNotes.map((w) => `index note: ${inlineUntrusted(w, 200)}`)) }, "materialize index-heal enqueue");
|
|
1967
|
+
await this.announce(writeScope, { kind: "gate", at: this.now(), items: capItems(dropNotes.map((w) => `index note: ${inlineUntrusted(w, 200)}`)) }, "materialize index-heal enqueue");
|
|
1904
1968
|
}
|
|
1905
1969
|
}
|
|
1906
1970
|
if (handle.writableRoot !== null) {
|
|
@@ -1931,14 +1995,14 @@ export class MemoryEngine {
|
|
|
1931
1995
|
if (firstRefused !== undefined) {
|
|
1932
1996
|
const [path, err] = firstRefused;
|
|
1933
1997
|
const remedy = permissionRemedyOf(Object.assign(new Error("write refused"), { code: err?.code, path }), this.memoryDir);
|
|
1934
|
-
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");
|
|
1998
|
+
await 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");
|
|
1935
1999
|
}
|
|
1936
2000
|
const indexOnDisk = readSafe(join(writableRoot, MEMORY_INDEX_FILENAME));
|
|
1937
2001
|
handle.planeBaseline = { files, ...(indexOnDisk !== undefined ? { index: indexOnDisk } : {}) };
|
|
1938
2002
|
}
|
|
1939
2003
|
return handle;
|
|
1940
2004
|
}
|
|
1941
|
-
inject(handle, opts) {
|
|
2005
|
+
async inject(handle, opts) {
|
|
1942
2006
|
const writeChannel = handle.writeScope !== null && opts?.writeToolMounted !== false;
|
|
1943
2007
|
const instruction = writeChannel ? buildMemoryInstruction(writableRootOf(handle)) : "";
|
|
1944
2008
|
const readOnlyNotice = handle.writeScope === null ? MEMORY_READONLY_NOTICE : undefined;
|
|
@@ -1961,7 +2025,7 @@ export class MemoryEngine {
|
|
|
1961
2025
|
let announcements;
|
|
1962
2026
|
let announceBlock;
|
|
1963
2027
|
try {
|
|
1964
|
-
const drained = drainMemoryAnnouncements(this.
|
|
2028
|
+
const drained = await drainMemoryAnnouncements(this.governanceStore, noticeScopesOf(handle));
|
|
1965
2029
|
if (drained.unscopedDiscarded > 0) {
|
|
1966
2030
|
this.withholdAnnouncement("queue upgrade", [`${drained.unscopedDiscarded} queued memory notice(s) without a scope (written before notices were filed per scope) were discarded unread`]);
|
|
1967
2031
|
}
|
|
@@ -2029,7 +2093,7 @@ export class MemoryEngine {
|
|
|
2029
2093
|
throw withPermissionRemedy(err, this.memoryDir);
|
|
2030
2094
|
}
|
|
2031
2095
|
finally {
|
|
2032
|
-
if (opts?.phase === "terminal" && opts.sessionId !== undefined && this.endSessionLeg(handle, opts.sessionId) === "stale" && out.report !== undefined && !out.report.warnings.includes(STALE_LEG_NOTE))
|
|
2096
|
+
if (opts?.phase === "terminal" && opts.sessionId !== undefined && await this.endSessionLeg(handle, opts.sessionId) === "stale" && out.report !== undefined && !out.report.warnings.includes(STALE_LEG_NOTE))
|
|
2033
2097
|
out.report.warnings.push(STALE_LEG_NOTE);
|
|
2034
2098
|
}
|
|
2035
2099
|
const report = out.report;
|
|
@@ -2041,14 +2105,14 @@ export class MemoryEngine {
|
|
|
2041
2105
|
ofRel: (rel) => this.scopeOfPlaneRel(handle, rel),
|
|
2042
2106
|
ofWarning: (w) => (layerWarnings !== undefined && layerWarnings.has(w) ? layerWarnings.get(w) : handle.writeScope ?? undefined),
|
|
2043
2107
|
};
|
|
2044
|
-
this.announceByScope(gateAnnouncementItems(report, owner), { kind: "gate", at: this.now(), ...named }, "harvest enqueue");
|
|
2108
|
+
await this.announceByScope(gateAnnouncementItems(report, owner), { kind: "gate", at: this.now(), ...named }, "harvest enqueue");
|
|
2045
2109
|
if (report.inboundFindings !== undefined && report.inboundFindings.length > 0) {
|
|
2046
2110
|
const byScope = new Map();
|
|
2047
2111
|
for (const r of report.inboundFindings) {
|
|
2048
2112
|
const scope = owner.ofRel(r.path);
|
|
2049
2113
|
byScope.set(scope, [...(byScope.get(scope) ?? []), `out-of-session memory change rejected [${r.code}] ${quoteId(r.path)} — ${inlineUntrusted(r.reason, 240)}`]);
|
|
2050
2114
|
}
|
|
2051
|
-
this.announceByScope(byScope, { kind: "external", at: this.now(), ...named }, "harvest enqueue");
|
|
2115
|
+
await this.announceByScope(byScope, { kind: "external", at: this.now(), ...named }, "harvest enqueue");
|
|
2052
2116
|
}
|
|
2053
2117
|
}
|
|
2054
2118
|
catch (err) {
|
|
@@ -2167,19 +2231,26 @@ export class MemoryEngine {
|
|
|
2167
2231
|
try {
|
|
2168
2232
|
if (carry) {
|
|
2169
2233
|
this.assertRootAnchored(handle);
|
|
2170
|
-
|
|
2171
|
-
|
|
2234
|
+
const stranded = [];
|
|
2235
|
+
try {
|
|
2236
|
+
reconcileHolds(this.controlDir, { homeOf: (scope) => scopeDirFor(this.memoryDir, this.controlDir, scope), now: this.now, onStranded: (scope, line) => stranded.push({ scope, line }) });
|
|
2237
|
+
}
|
|
2238
|
+
finally {
|
|
2239
|
+
for (const s of stranded)
|
|
2240
|
+
await this.announce(s.scope, { kind: "gate", at: this.now(), items: [s.line] }, "instruction-hold enqueue");
|
|
2241
|
+
}
|
|
2242
|
+
const newlyExpired = await expireOverdueSettlements(this.governanceStore, { timeoutMs: this.holdSettleTimeoutMs, now: this.now });
|
|
2172
2243
|
if (newlyExpired.length > 0) {
|
|
2173
2244
|
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`);
|
|
2174
2245
|
}
|
|
2175
|
-
replayExternalSettlementEffects(this.controlDir, { carry, now: this.now });
|
|
2246
|
+
await replayExternalSettlementEffects(this.governanceStore, this.controlDir, { carry, now: this.now });
|
|
2176
2247
|
const settled = await this.settleDueHolds(handle, report);
|
|
2177
2248
|
for (const rel of settled.released)
|
|
2178
2249
|
signalContainment((c) => pushDistinct(c.releasedHolds, rel));
|
|
2179
2250
|
for (const d of settled.disposed)
|
|
2180
2251
|
signalContainment((c) => c.disposedHolds.push(d));
|
|
2181
2252
|
if (lineageSessionId !== undefined) {
|
|
2182
|
-
const rows = sessionSettlements(this.
|
|
2253
|
+
const rows = await sessionSettlements(this.governanceStore, lineageSessionId);
|
|
2183
2254
|
const cls = classifySessionSettlements(rows);
|
|
2184
2255
|
if (cls.state === "exposed") {
|
|
2185
2256
|
if (pollutedReason === undefined) {
|
|
@@ -2191,11 +2262,11 @@ export class MemoryEngine {
|
|
|
2191
2262
|
sessionPending = true;
|
|
2192
2263
|
pendingSettleIds = rows.filter((r) => r.effective === "pending").map((r) => r.row.settleId);
|
|
2193
2264
|
}
|
|
2194
|
-
unattributedSet = sessionUnattributedSet(this.controlDir, lineageSessionId);
|
|
2265
|
+
unattributedSet = await sessionUnattributedSet(this.governanceStore, this.controlDir, lineageSessionId);
|
|
2195
2266
|
}
|
|
2196
2267
|
}
|
|
2197
|
-
const rec = reconcileLineage(this.
|
|
2198
|
-
this.settlePromotions(rec.promoted);
|
|
2268
|
+
const rec = await reconcileLineage(this.governanceStore);
|
|
2269
|
+
await this.settlePromotions(rec.promoted);
|
|
2199
2270
|
for (const u of rec.undecidable) {
|
|
2200
2271
|
this.warnScoped(report, undefined, u.kind === "latch-only"
|
|
2201
2272
|
? `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)`
|
|
@@ -2204,9 +2275,9 @@ export class MemoryEngine {
|
|
|
2204
2275
|
if (pollutedReason !== undefined && lineageSessionId !== undefined) {
|
|
2205
2276
|
const rec2 = this.sessionPollution(lineageSessionId);
|
|
2206
2277
|
if (rec2 !== undefined) {
|
|
2207
|
-
const replaySwept = lineageContributionsOfSession(this.
|
|
2278
|
+
const replaySwept = await lineageContributionsOfSession(this.governanceStore, lineageSessionId, this.incarnation.incarnationOf(lineageSessionId));
|
|
2208
2279
|
const contributions = carry ? replaySwept.filter((c) => c.marked !== true) : replaySwept;
|
|
2209
|
-
this.challengeAndAnnounce(contributions.map((c) => ({
|
|
2280
|
+
await this.challengeAndAnnounce(contributions.map((c) => ({
|
|
2210
2281
|
eventId: `pollution:${lineageSessionId}:${rec2.at}:${c.entryId}`,
|
|
2211
2282
|
entryId: c.entryId,
|
|
2212
2283
|
reason: `challenged: the contributing session was marked polluted`,
|
|
@@ -2222,9 +2293,12 @@ export class MemoryEngine {
|
|
|
2222
2293
|
}
|
|
2223
2294
|
const writableRoot = writableRootOf(handle);
|
|
2224
2295
|
const seatOf = (canonical) => ({ scope: writeScope, rel: homeRel(writableRoot, canonical) });
|
|
2225
|
-
|
|
2296
|
+
const insideWriteRoot = (canonical) => canonical === writableRoot || canonical.startsWith(`${writableRoot}${sep}`);
|
|
2297
|
+
const homeStat = lstatSync(writableRoot, { throwIfNoEntry: false });
|
|
2298
|
+
if (homeStat?.isDirectory() !== true) {
|
|
2299
|
+
const outside = homeStat?.isSymbolicLink() === true ? this.storeActRefusedText(join(writableRoot, MEMORY_INDEX_FILENAME), "harvesting into the memory write home", handle) : undefined;
|
|
2226
2300
|
report.ok = false;
|
|
2227
|
-
report.incident = { kind: "dir_missing", detail: `memory writable root is gone: ${writableRoot}` };
|
|
2301
|
+
report.incident = { kind: "dir_missing", detail: outside !== undefined ? `memory writable root is a link: ${outside}` : `memory writable root is gone: ${writableRoot}` };
|
|
2228
2302
|
report.missing = handle.materialized.filter((m) => !m.readonly).map((m) => m.relPath);
|
|
2229
2303
|
return report;
|
|
2230
2304
|
}
|
|
@@ -2244,11 +2318,16 @@ export class MemoryEngine {
|
|
|
2244
2318
|
},
|
|
2245
2319
|
});
|
|
2246
2320
|
const records = [];
|
|
2321
|
+
const blankAtScan = new Set();
|
|
2247
2322
|
for (const f of scanned) {
|
|
2248
2323
|
const canonical = canonicalize(f.path);
|
|
2249
2324
|
const text = readSafe(canonical);
|
|
2250
2325
|
if (text === undefined)
|
|
2251
2326
|
continue;
|
|
2327
|
+
if (!carriesEntry(text)) {
|
|
2328
|
+
blankAtScan.add(canonical);
|
|
2329
|
+
continue;
|
|
2330
|
+
}
|
|
2252
2331
|
records.push({ canonical, rel: relative(handle.memoryDir, canonical), slug: f.slug, text, parsed: parseEntryFile(text), sizeBytes: Buffer.byteLength(text, "utf8") });
|
|
2253
2332
|
}
|
|
2254
2333
|
const presentIds = new Set();
|
|
@@ -2263,7 +2342,7 @@ export class MemoryEngine {
|
|
|
2263
2342
|
const containPollutedRecord = async (f, whyOverride) => {
|
|
2264
2343
|
const why = whyOverride ?? withheldWhy;
|
|
2265
2344
|
const rel = f.rel;
|
|
2266
|
-
if (
|
|
2345
|
+
if (!insideWriteRoot(f.canonical)) {
|
|
2267
2346
|
report.rejections.push({ path: rel, code: "outside_root", reason: "file resolves outside the writable memory root (containment gate, fail-closed)" });
|
|
2268
2347
|
return;
|
|
2269
2348
|
}
|
|
@@ -2386,41 +2465,49 @@ export class MemoryEngine {
|
|
|
2386
2465
|
report.warnings.push(`memory harvest refused by an incident while this session is marked externally exposed — no entries were committed; instruction-form files were withheld and the derived index rolled back: ${inlineUntrusted(pollutedReason, 200)}`);
|
|
2387
2466
|
};
|
|
2388
2467
|
await this.restoreReadOnlyLayers(readOnlyRestorable, report, handle.accountLeg, handle);
|
|
2389
|
-
const
|
|
2468
|
+
const removedOnPlane = (p) => {
|
|
2469
|
+
if (blankAtScan.has(p))
|
|
2470
|
+
return true;
|
|
2471
|
+
if (!insideWriteRoot(canonicalize(p)))
|
|
2472
|
+
return false;
|
|
2473
|
+
try {
|
|
2474
|
+
lstatSync(p);
|
|
2475
|
+
return false;
|
|
2476
|
+
}
|
|
2477
|
+
catch (err) {
|
|
2478
|
+
const code = err?.code;
|
|
2479
|
+
return code === "ENOENT" || code === "ENOTDIR";
|
|
2480
|
+
}
|
|
2481
|
+
};
|
|
2482
|
+
const missingWritable = writable.filter((m) => !recordByPath.has(m.path) && !presentIds.has(m.id) && removedOnPlane(m.path));
|
|
2390
2483
|
report.missing.push(...missingWritable.map((m) => m.relPath));
|
|
2391
|
-
|
|
2484
|
+
const deletionCeiling = deletionFuseCeiling(writable.length, MASS_DELETION_FUSE_RATIO);
|
|
2485
|
+
if (missingWritable.length > deletionCeiling) {
|
|
2392
2486
|
report.ok = false;
|
|
2393
2487
|
report.incident = {
|
|
2394
2488
|
kind: "mass_deletion",
|
|
2395
|
-
detail: `${missingWritable.length}/${writable.length} materialized memory files are missing —
|
|
2489
|
+
detail: `${missingWritable.length}/${writable.length} materialized memory files are missing — more than one harvest may remove (${deletionCeiling}), judged an accident: harvest refused, nothing deleted or restored`,
|
|
2396
2490
|
};
|
|
2397
2491
|
if (pollutedReason !== undefined)
|
|
2398
2492
|
await (carry ? containExposedInstructionDomain() : containPollutedDomain());
|
|
2399
2493
|
return report;
|
|
2400
2494
|
}
|
|
2401
|
-
const
|
|
2402
|
-
const indexNow = readSafe(indexPath);
|
|
2403
|
-
if (handle.indexBaselineLines > 0 && indexNow !== undefined && indexNow.trim() === "" && missingWritable.length > 0) {
|
|
2404
|
-
report.ok = false;
|
|
2405
|
-
report.incident = { kind: "index_cleared", detail: "MEMORY.md was emptied alongside missing memory files — judged an accident, harvest refused" };
|
|
2406
|
-
if (pollutedReason !== undefined)
|
|
2407
|
-
await (carry ? containExposedInstructionDomain() : containPollutedDomain());
|
|
2408
|
-
return report;
|
|
2409
|
-
}
|
|
2410
|
-
for (const m of missingWritable) {
|
|
2495
|
+
const restoreRemoved = async (m) => {
|
|
2411
2496
|
const content = await this.committedContentFor(m.id);
|
|
2412
2497
|
if (content === undefined)
|
|
2413
|
-
|
|
2498
|
+
return "no committed copy could be read back, so the file stays removed (the entry stays committed)";
|
|
2414
2499
|
try {
|
|
2415
2500
|
this.refuseOutsideStore(m.path, "restoring a memory file", handle);
|
|
2416
2501
|
mkdirSync(dirname(m.path), { recursive: true });
|
|
2417
2502
|
writeFileNoFollow(m.path, content);
|
|
2418
2503
|
report.restored.push(m.relPath);
|
|
2504
|
+
return "the removed file's committed copy was put back";
|
|
2419
2505
|
}
|
|
2420
2506
|
catch (err) {
|
|
2421
|
-
report.warnings.push(`${m.relPath}: the committed copy of this
|
|
2507
|
+
report.warnings.push(`${m.relPath}: the committed copy of this removed memory file could NOT be restored: ${this.writeFailureText(err)}`);
|
|
2508
|
+
return "putting the committed copy back failed (warned)";
|
|
2422
2509
|
}
|
|
2423
|
-
}
|
|
2510
|
+
};
|
|
2424
2511
|
const baselinePaths = new Set(writable.map((m) => m.path));
|
|
2425
2512
|
const inDomain = records
|
|
2426
2513
|
.slice()
|
|
@@ -2457,6 +2544,49 @@ export class MemoryEngine {
|
|
|
2457
2544
|
const modelIdAdds = new Set();
|
|
2458
2545
|
const stoodAsFound = new Set();
|
|
2459
2546
|
let observedTable;
|
|
2547
|
+
const removalDeletes = new Map();
|
|
2548
|
+
for (const m of missingWritable) {
|
|
2549
|
+
const placement = await this.committedPlacementOf(m.id);
|
|
2550
|
+
if (placement.state === "absent")
|
|
2551
|
+
continue;
|
|
2552
|
+
if (placement.state === "unknown") {
|
|
2553
|
+
report.warnings.push(`${m.relPath}: not deleted and not put back — where the store holds this entry could not be read, so the removal is left as found (the entry stays committed)`);
|
|
2554
|
+
continue;
|
|
2555
|
+
}
|
|
2556
|
+
if (placement.scope !== m.scope) {
|
|
2557
|
+
report.warnings.push(`${m.relPath}: not deleted — the entry was moved to scope ${JSON.stringify(placement.scope)} since this session was served it; a removal deletes only in this session's write scope`);
|
|
2558
|
+
continue;
|
|
2559
|
+
}
|
|
2560
|
+
if (m.stub) {
|
|
2561
|
+
report.rejections.push({ path: m.relPath, code: "stub_modified", reason: "this file is an archived-body stub; removing a stub does not delete its entry in this version (the next session projects the stub again)" });
|
|
2562
|
+
continue;
|
|
2563
|
+
}
|
|
2564
|
+
if (pollutedReason !== undefined && !carry) {
|
|
2565
|
+
await restoreRemoved(m);
|
|
2566
|
+
continue;
|
|
2567
|
+
}
|
|
2568
|
+
if (carry && (pollutedReason !== undefined || (sessionPending && lineageSessionId !== undefined))) {
|
|
2569
|
+
const committedFm = placement.frontmatter ?? (await this.committedFrontmatterFor(m.id));
|
|
2570
|
+
if (committedFm === undefined || isInstructionEntry(committedFm)) {
|
|
2571
|
+
const back = await restoreRemoved(m);
|
|
2572
|
+
const unread = committedFm === undefined ? " (its committed form could not be read, so it is withheld as one)" : "";
|
|
2573
|
+
if (pollutedReason !== undefined) {
|
|
2574
|
+
report.rejections.push({ path: m.relPath, code: "polluted", reason: `memory deletion withheld${unread}: ${withheldWhy} — ${back}` });
|
|
2575
|
+
}
|
|
2576
|
+
else {
|
|
2577
|
+
report.warnings.push(`${m.relPath}: instruction-form entry NOT deleted${unread} — this session's delegation outcome is still pending (attested-only evidence standard); ${back}, and the entry can be removed again once the session settles clean`);
|
|
2578
|
+
}
|
|
2579
|
+
continue;
|
|
2580
|
+
}
|
|
2581
|
+
}
|
|
2582
|
+
const refusedHere = this.storeActRefusedText(m.path, "deleting a removed memory file's entry", handle);
|
|
2583
|
+
if (refusedHere !== undefined) {
|
|
2584
|
+
report.warnings.push(`${m.relPath}: not deleted — ${refusedHere}`);
|
|
2585
|
+
continue;
|
|
2586
|
+
}
|
|
2587
|
+
patches.push({ op: "delete", id: m.id, baseRev: m.rev });
|
|
2588
|
+
removalDeletes.set(m.id, { file: m, ...(placement.frontmatter !== undefined ? { form: placement.frontmatter } : {}) });
|
|
2589
|
+
}
|
|
2460
2590
|
let processed = 0;
|
|
2461
2591
|
for (let i = 0; i < kept.length; i++) {
|
|
2462
2592
|
const f = kept[i];
|
|
@@ -2468,7 +2598,7 @@ export class MemoryEngine {
|
|
|
2468
2598
|
break;
|
|
2469
2599
|
}
|
|
2470
2600
|
const rel = f.rel;
|
|
2471
|
-
if (
|
|
2601
|
+
if (!insideWriteRoot(f.canonical)) {
|
|
2472
2602
|
report.rejections.push({ path: rel, code: "outside_root", reason: "file resolves outside the writable memory root (containment gate, fail-closed)" });
|
|
2473
2603
|
continue;
|
|
2474
2604
|
}
|
|
@@ -2763,7 +2893,7 @@ export class MemoryEngine {
|
|
|
2763
2893
|
let lineageArmed = lineageSessionId !== undefined && pendingRows.length > 0;
|
|
2764
2894
|
if (lineageArmed) {
|
|
2765
2895
|
try {
|
|
2766
|
-
stageLineagePending(this.
|
|
2896
|
+
await stageLineagePending(this.governanceStore, txnId, lineageSessionId, pendingRows, this.now, this.incarnation.incarnationOf(lineageSessionId));
|
|
2767
2897
|
}
|
|
2768
2898
|
catch (err) {
|
|
2769
2899
|
report.ok = false;
|
|
@@ -2775,7 +2905,7 @@ export class MemoryEngine {
|
|
|
2775
2905
|
if (preCommitRead.record !== undefined || preCommitRead.fault) {
|
|
2776
2906
|
if (lineageArmed) {
|
|
2777
2907
|
try {
|
|
2778
|
-
discardLineagePending(this.
|
|
2908
|
+
await discardLineagePending(this.governanceStore, txnId);
|
|
2779
2909
|
}
|
|
2780
2910
|
catch {
|
|
2781
2911
|
}
|
|
@@ -2792,7 +2922,7 @@ export class MemoryEngine {
|
|
|
2792
2922
|
if (preCommit !== undefined && !carry) {
|
|
2793
2923
|
if (lineageArmed) {
|
|
2794
2924
|
try {
|
|
2795
|
-
discardLineagePending(this.
|
|
2925
|
+
await discardLineagePending(this.governanceStore, txnId);
|
|
2796
2926
|
}
|
|
2797
2927
|
catch {
|
|
2798
2928
|
}
|
|
@@ -2800,6 +2930,8 @@ export class MemoryEngine {
|
|
|
2800
2930
|
pollutedReason = preCommit.reason;
|
|
2801
2931
|
for (const f of records)
|
|
2802
2932
|
await containPollutedRecord(f);
|
|
2933
|
+
for (const r of removalDeletes.values())
|
|
2934
|
+
await restoreRemoved(r.file);
|
|
2803
2935
|
patchReport = { applied: [], conflicts: [] };
|
|
2804
2936
|
}
|
|
2805
2937
|
else {
|
|
@@ -2810,13 +2942,19 @@ export class MemoryEngine {
|
|
|
2810
2942
|
const droppedIds = new Set();
|
|
2811
2943
|
for (const p of patches) {
|
|
2812
2944
|
if (p.op === "delete") {
|
|
2813
|
-
const
|
|
2814
|
-
|
|
2945
|
+
const removed = removalDeletes.get(p.id);
|
|
2946
|
+
const committedFm = removed?.form ?? (await this.committedFrontmatterFor(p.id));
|
|
2947
|
+
if (committedFm !== undefined ? isInstructionEntry(committedFm) : removed !== undefined) {
|
|
2815
2948
|
const rec = records.find((r) => (handle.baseIds.get(r.canonical) ?? r.parsed.id) === p.id && r.parsed.frontmatter.deleted === true);
|
|
2816
2949
|
if (rec !== undefined) {
|
|
2817
2950
|
await containPollutedRecord(rec);
|
|
2818
2951
|
signalContainment((c) => pushDistinct(c.quarantinedInstruction, rec.rel));
|
|
2819
2952
|
}
|
|
2953
|
+
else if (removed !== undefined) {
|
|
2954
|
+
const back = await restoreRemoved(removed.file);
|
|
2955
|
+
const unread = committedFm === undefined ? " (its committed form could not be read, so it is withheld as one)" : "";
|
|
2956
|
+
report.rejections.push({ path: removed.file.relPath, code: "polluted", reason: `memory deletion withheld${unread}: ${withheldWhy} — ${back}` });
|
|
2957
|
+
}
|
|
2820
2958
|
continue;
|
|
2821
2959
|
}
|
|
2822
2960
|
keptPatches.push(p);
|
|
@@ -2884,7 +3022,7 @@ export class MemoryEngine {
|
|
|
2884
3022
|
if (lineageArmed) {
|
|
2885
3023
|
if (restagedRows.length > 0) {
|
|
2886
3024
|
try {
|
|
2887
|
-
stageLineagePending(this.
|
|
3025
|
+
await stageLineagePending(this.governanceStore, txnId, lineageSessionId, restagedRows, this.now, this.incarnation.incarnationOf(lineageSessionId));
|
|
2888
3026
|
}
|
|
2889
3027
|
catch (err) {
|
|
2890
3028
|
report.ok = false;
|
|
@@ -2894,7 +3032,7 @@ export class MemoryEngine {
|
|
|
2894
3032
|
}
|
|
2895
3033
|
else {
|
|
2896
3034
|
try {
|
|
2897
|
-
discardLineagePending(this.
|
|
3035
|
+
await discardLineagePending(this.governanceStore, txnId);
|
|
2898
3036
|
}
|
|
2899
3037
|
catch (err) {
|
|
2900
3038
|
report.warnings.push(`memory lineage stage for withdrawn instruction entries could not be discarded — their ids stay latched (memory reads refuse them) until the host adjudicates pending transaction ${txnId}: ${thrownText(err)}`);
|
|
@@ -2945,7 +3083,7 @@ export class MemoryEngine {
|
|
|
2945
3083
|
const remaining = patches.filter((p) => p.op !== "delete" && p.entry !== undefined).map((p) => ({ entryId: p.id, rev: p.entry.rev, ...(p.entry.frontmatter.origin !== undefined ? { marked: true } : {}) }));
|
|
2946
3084
|
if (remaining.length > 0) {
|
|
2947
3085
|
try {
|
|
2948
|
-
stageLineagePending(this.
|
|
3086
|
+
await stageLineagePending(this.governanceStore, txnId, lineageSessionId, remaining, this.now, this.incarnation.incarnationOf(lineageSessionId));
|
|
2949
3087
|
}
|
|
2950
3088
|
catch (err) {
|
|
2951
3089
|
report.ok = false;
|
|
@@ -2955,7 +3093,7 @@ export class MemoryEngine {
|
|
|
2955
3093
|
}
|
|
2956
3094
|
else {
|
|
2957
3095
|
try {
|
|
2958
|
-
discardLineagePending(this.
|
|
3096
|
+
await discardLineagePending(this.governanceStore, txnId);
|
|
2959
3097
|
}
|
|
2960
3098
|
catch (err) {
|
|
2961
3099
|
report.warnings.push(`memory lineage stage for withheld entries could not be discarded — their ids stay latched until the host adjudicates pending transaction ${txnId}: ${thrownText(err)}`);
|
|
@@ -2971,7 +3109,7 @@ export class MemoryEngine {
|
|
|
2971
3109
|
catch (err) {
|
|
2972
3110
|
if (lineageArmed) {
|
|
2973
3111
|
try {
|
|
2974
|
-
discardLineagePending(this.
|
|
3112
|
+
await discardLineagePending(this.governanceStore, txnId);
|
|
2975
3113
|
}
|
|
2976
3114
|
catch {
|
|
2977
3115
|
}
|
|
@@ -2982,9 +3120,9 @@ export class MemoryEngine {
|
|
|
2982
3120
|
}
|
|
2983
3121
|
if (lineageArmed) {
|
|
2984
3122
|
try {
|
|
2985
|
-
recordLineageCredential(this.
|
|
2986
|
-
const promoted = promoteLineagePending(this.
|
|
2987
|
-
this.settlePromotions(promoted);
|
|
3123
|
+
await recordLineageCredential(this.governanceStore, txnId, patchReport.applied.filter((a) => a.op !== "delete").map((a) => a.id), this.now);
|
|
3124
|
+
const promoted = await promoteLineagePending(this.governanceStore, txnId);
|
|
3125
|
+
await this.settlePromotions(promoted);
|
|
2988
3126
|
}
|
|
2989
3127
|
catch (err) {
|
|
2990
3128
|
const detail = `memory lineage settlement failed after commit — the affected entries stay latched (memory reads refuse them) until a later harvest reconciles or the host adjudicates: ${thrownText(err)}`;
|
|
@@ -3004,7 +3142,7 @@ export class MemoryEngine {
|
|
|
3004
3142
|
const deletedIds = patchReport.applied.filter((a) => a.op === "delete").map((a) => a.id);
|
|
3005
3143
|
if (deletedIds.length > 0) {
|
|
3006
3144
|
try {
|
|
3007
|
-
clearLineageForEntries(this.
|
|
3145
|
+
await clearLineageForEntries(this.governanceStore, deletedIds);
|
|
3008
3146
|
}
|
|
3009
3147
|
catch {
|
|
3010
3148
|
}
|
|
@@ -3020,7 +3158,7 @@ export class MemoryEngine {
|
|
|
3020
3158
|
if (postCommit !== undefined) {
|
|
3021
3159
|
const appliedNonDelete = patchReport.applied.filter((a) => a.op !== "delete").map((a) => a.id);
|
|
3022
3160
|
try {
|
|
3023
|
-
this.challengeAndAnnounce(appliedNonDelete.map((id) => ({
|
|
3161
|
+
await this.challengeAndAnnounce(appliedNonDelete.map((id) => ({
|
|
3024
3162
|
eventId: `pollution:${lineageSessionId}:${postCommit.at}:${id}`,
|
|
3025
3163
|
entryId: id,
|
|
3026
3164
|
reason: `challenged: the contributing session was marked polluted`,
|
|
@@ -3114,7 +3252,7 @@ export class MemoryEngine {
|
|
|
3114
3252
|
handle.indexOnDiskUntrusted = true;
|
|
3115
3253
|
}
|
|
3116
3254
|
const headers = await this.backend.listHeaders([...new Set([...handle.scopes, writeScope])]);
|
|
3117
|
-
handle.indexText = this.rebuildIndex(handle, headers, { write: true, ignoreOnDisk: indexGate !== undefined }, report.warnings);
|
|
3255
|
+
handle.indexText = await this.rebuildIndex(handle, headers, { write: true, ignoreOnDisk: indexGate !== undefined }, report.warnings);
|
|
3118
3256
|
await this.rebaseline(handle, new Set([...(report.degraded?.pending ?? []), ...stoodAsFound, ...records.filter((r) => r.parsed.unclosed === true).map((r) => r.rel)]), new Set(report.rejections.map((r) => r.path)));
|
|
3119
3257
|
if (carry && lineageSessionId !== undefined && report.degraded === undefined) {
|
|
3120
3258
|
const deferredSeats = [...new Set(report.rejections.filter((r) => r.code === "deferred").map((r) => r.path))];
|
|
@@ -3122,7 +3260,7 @@ export class MemoryEngine {
|
|
|
3122
3260
|
const rest = deferredSeats.length - shown.length;
|
|
3123
3261
|
const seatList = `${shown.join(", ")}${rest > 0 ? ` (+${rest} more — see the rejections)` : ""}`;
|
|
3124
3262
|
try {
|
|
3125
|
-
const closed = handle.accountLeg !== undefined ? closeSessionAccount(this.controlDir, { sessionId: lineageSessionId, now: this.now, leg: handle.accountLeg }) : "absent";
|
|
3263
|
+
const closed = handle.accountLeg !== undefined ? await closeSessionAccount(this.governanceStore, this.controlDir, { sessionId: lineageSessionId, now: this.now, leg: handle.accountLeg }) : "absent";
|
|
3126
3264
|
if (closed === "stale" && !report.warnings.includes(STALE_LEG_NOTE))
|
|
3127
3265
|
report.warnings.push(STALE_LEG_NOTE);
|
|
3128
3266
|
if (closed === "written" && deferredSeats.length > 0) {
|
|
@@ -3135,20 +3273,10 @@ export class MemoryEngine {
|
|
|
3135
3273
|
}
|
|
3136
3274
|
if (this.consolidation !== undefined && handle.writeScope !== null && opts?.sessionId !== undefined && report.ok) {
|
|
3137
3275
|
try {
|
|
3138
|
-
const rec = recordConsolidationSession(this.
|
|
3276
|
+
const rec = await recordConsolidationSession(this.governanceStore, handle.writeScope, opts.sessionId, this.consolidation, this.now);
|
|
3139
3277
|
if (rec.recommended) {
|
|
3140
3278
|
report.consolidationRecommended = { scope: handle.writeScope, sessionsSince: rec.sessionsSince };
|
|
3141
|
-
|
|
3142
|
-
enqueueMemoryAnnouncement(this.controlDir, {
|
|
3143
|
-
kind: "gate",
|
|
3144
|
-
scope: handle.writeScope,
|
|
3145
|
-
at: this.now(),
|
|
3146
|
-
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)`],
|
|
3147
|
-
});
|
|
3148
|
-
}
|
|
3149
|
-
catch (err) {
|
|
3150
|
-
this.discloseAnnounceFailure("consolidation recommendation enqueue", err);
|
|
3151
|
-
}
|
|
3279
|
+
await this.announce(handle.writeScope, { kind: "gate", at: this.now(), 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)`] }, "consolidation recommendation enqueue");
|
|
3152
3280
|
}
|
|
3153
3281
|
}
|
|
3154
3282
|
catch (err) {
|
|
@@ -3191,7 +3319,7 @@ export class MemoryEngine {
|
|
|
3191
3319
|
continue;
|
|
3192
3320
|
}
|
|
3193
3321
|
const marker = this.sessionPollution(row.sessionId);
|
|
3194
|
-
const rows = sessionSettlements(this.
|
|
3322
|
+
const rows = await sessionSettlements(this.governanceStore, row.sessionId);
|
|
3195
3323
|
if (marker !== undefined || rows.some((r) => r.effective === "external")) {
|
|
3196
3324
|
dispose(row, "dirty", marker !== undefined ? "the writer session is marked externally exposed" : "a delegation of the writer session settled with an observed external attestation");
|
|
3197
3325
|
continue;
|
|
@@ -3271,7 +3399,7 @@ export class MemoryEngine {
|
|
|
3271
3399
|
: { op: "add", id: entry.id, entry, guard: "absent" };
|
|
3272
3400
|
const txnId = uuidv7();
|
|
3273
3401
|
try {
|
|
3274
|
-
stageLineagePending(this.
|
|
3402
|
+
await stageLineagePending(this.governanceStore, txnId, row.sessionId, [{ entryId: patch.id, rev: entry.rev, ...(fm.origin !== undefined ? { marked: true } : {}) }], this.now);
|
|
3275
3403
|
}
|
|
3276
3404
|
catch (err) {
|
|
3277
3405
|
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)}`);
|
|
@@ -3283,7 +3411,7 @@ export class MemoryEngine {
|
|
|
3283
3411
|
}
|
|
3284
3412
|
catch (err) {
|
|
3285
3413
|
try {
|
|
3286
|
-
discardLineagePending(this.
|
|
3414
|
+
await discardLineagePending(this.governanceStore, txnId);
|
|
3287
3415
|
}
|
|
3288
3416
|
catch {
|
|
3289
3417
|
}
|
|
@@ -3292,7 +3420,7 @@ export class MemoryEngine {
|
|
|
3292
3420
|
}
|
|
3293
3421
|
if (patchReport.conflicts.length > 0) {
|
|
3294
3422
|
try {
|
|
3295
|
-
discardLineagePending(this.
|
|
3423
|
+
await discardLineagePending(this.governanceStore, txnId);
|
|
3296
3424
|
}
|
|
3297
3425
|
catch {
|
|
3298
3426
|
}
|
|
@@ -3315,9 +3443,9 @@ export class MemoryEngine {
|
|
|
3315
3443
|
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`);
|
|
3316
3444
|
}
|
|
3317
3445
|
try {
|
|
3318
|
-
recordLineageCredential(this.
|
|
3319
|
-
const promoted = promoteLineagePending(this.
|
|
3320
|
-
this.settlePromotions(promoted);
|
|
3446
|
+
await recordLineageCredential(this.governanceStore, txnId, patchReport.applied.filter((a) => a.op !== "delete").map((a) => a.id), this.now);
|
|
3447
|
+
const promoted = await promoteLineagePending(this.governanceStore, txnId);
|
|
3448
|
+
await this.settlePromotions(promoted);
|
|
3321
3449
|
}
|
|
3322
3450
|
catch (err) {
|
|
3323
3451
|
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)}`);
|
|
@@ -3341,26 +3469,26 @@ export class MemoryEngine {
|
|
|
3341
3469
|
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)"}`);
|
|
3342
3470
|
return this.holdPlaneRel(row);
|
|
3343
3471
|
}
|
|
3344
|
-
resolveSettlement(settleId, to, requestId) {
|
|
3345
|
-
resolveSettlementRecord(this.
|
|
3472
|
+
async resolveSettlement(settleId, to, requestId) {
|
|
3473
|
+
await resolveSettlementRecord(this.governanceStore, { settleId, to, requestId, now: this.now });
|
|
3346
3474
|
}
|
|
3347
3475
|
resolveHold(holdId, action, requestId) {
|
|
3348
3476
|
resolveHoldRecord(this.controlDir, { holdId, action, requestId, now: this.now });
|
|
3349
3477
|
}
|
|
3350
|
-
resolveSessionAccount(sessionId, requestId) {
|
|
3351
|
-
resolveSessionAccountRecord(this.controlDir, { sessionId, requestId, now: this.now });
|
|
3478
|
+
async resolveSessionAccount(sessionId, requestId) {
|
|
3479
|
+
await resolveSessionAccountRecord(this.governanceStore, this.controlDir, { sessionId, requestId, now: this.now });
|
|
3352
3480
|
}
|
|
3353
3481
|
get controlPlaneDir() {
|
|
3354
3482
|
return this.controlDir;
|
|
3355
3483
|
}
|
|
3356
|
-
readDelegationSettlements() {
|
|
3357
|
-
return effectiveSettlements(this.
|
|
3484
|
+
async readDelegationSettlements() {
|
|
3485
|
+
return await effectiveSettlements(this.governanceStore);
|
|
3358
3486
|
}
|
|
3359
3487
|
readInstructionHolds() {
|
|
3360
3488
|
return readHolds(this.controlDir);
|
|
3361
3489
|
}
|
|
3362
|
-
readDanglingSessionAccounts(selfSessionId) {
|
|
3363
|
-
return foreignDanglingSessionAccounts(this.controlDir, selfSessionId);
|
|
3490
|
+
async readDanglingSessionAccounts(selfSessionId) {
|
|
3491
|
+
return await foreignDanglingSessionAccounts(this.governanceStore, this.controlDir, selfSessionId);
|
|
3364
3492
|
}
|
|
3365
3493
|
requireConsolidation() {
|
|
3366
3494
|
if (this.consolidation === undefined) {
|
|
@@ -3368,9 +3496,9 @@ export class MemoryEngine {
|
|
|
3368
3496
|
}
|
|
3369
3497
|
return this.consolidation;
|
|
3370
3498
|
}
|
|
3371
|
-
readConsolidationGateOrRefuse() {
|
|
3499
|
+
async readConsolidationGateOrRefuse() {
|
|
3372
3500
|
try {
|
|
3373
|
-
return
|
|
3501
|
+
return { scopes: await this.governanceStore.readGate() };
|
|
3374
3502
|
}
|
|
3375
3503
|
catch (err) {
|
|
3376
3504
|
throw new ConsolidationRefusedError("memory.consolidation_gate_corrupt", `memory consolidation gate store cannot be trusted (fail-closed): ${thrownText(err)}`);
|
|
@@ -3395,7 +3523,7 @@ export class MemoryEngine {
|
|
|
3395
3523
|
async consolidationEligibility(headers) {
|
|
3396
3524
|
let exclusions;
|
|
3397
3525
|
try {
|
|
3398
|
-
exclusions = this.readChallengeExclusions();
|
|
3526
|
+
exclusions = await this.readChallengeExclusions();
|
|
3399
3527
|
}
|
|
3400
3528
|
catch (err) {
|
|
3401
3529
|
throw new ConsolidationRefusedError("memory.consolidation_governance_unreadable", `memory consolidation refused: the challenge/lineage ledger cannot be trusted (fail-closed): ${thrownText(err)}`);
|
|
@@ -3403,7 +3531,7 @@ export class MemoryEngine {
|
|
|
3403
3531
|
const superseded = deriveSupersededSet(headers, { excluded: (id) => exclusions.has(id) });
|
|
3404
3532
|
const pendingSessions = new Set();
|
|
3405
3533
|
try {
|
|
3406
|
-
for (const s of effectiveSettlements(this.
|
|
3534
|
+
for (const s of await effectiveSettlements(this.governanceStore))
|
|
3407
3535
|
if (s.effective === "pending")
|
|
3408
3536
|
pendingSessions.add(s.row.sessionId);
|
|
3409
3537
|
}
|
|
@@ -3412,7 +3540,7 @@ export class MemoryEngine {
|
|
|
3412
3540
|
}
|
|
3413
3541
|
let lineageByEntry = {};
|
|
3414
3542
|
try {
|
|
3415
|
-
lineageByEntry = readLineageRecord(this.
|
|
3543
|
+
lineageByEntry = (await readLineageRecord(this.governanceStore)).committed;
|
|
3416
3544
|
}
|
|
3417
3545
|
catch (err) {
|
|
3418
3546
|
throw new ConsolidationRefusedError("memory.consolidation_governance_unreadable", `memory consolidation refused: the lineage ledger cannot be trusted (fail-closed): ${thrownText(err)}`);
|
|
@@ -3450,7 +3578,7 @@ export class MemoryEngine {
|
|
|
3450
3578
|
}
|
|
3451
3579
|
async snapshotConsolidationCandidates(scope, opts = {}) {
|
|
3452
3580
|
const screened = this.requireConsolidation();
|
|
3453
|
-
const gate = this.readConsolidationGateOrRefuse();
|
|
3581
|
+
const gate = await this.readConsolidationGateOrRefuse();
|
|
3454
3582
|
const row = gate.scopes[scope];
|
|
3455
3583
|
const at = this.now();
|
|
3456
3584
|
if (row?.openPlanId !== undefined) {
|
|
@@ -3491,7 +3619,7 @@ export class MemoryEngine {
|
|
|
3491
3619
|
const marked = candidates.some((c) => c.marked);
|
|
3492
3620
|
const epoch = row?.epoch ?? 0;
|
|
3493
3621
|
const cycleToken = createHash("sha256").update(JSON.stringify([scope, epoch, at, Object.entries(candidateRevs).sort()]), "utf8").digest("hex").slice(0, 32);
|
|
3494
|
-
const announceForce =
|
|
3622
|
+
const announceForce = await this.governanceStore.transact([], (file) => {
|
|
3495
3623
|
const fresh = file.scopes[scope] ?? { sessions: { count: 0, ring: [] }, epoch: 0 };
|
|
3496
3624
|
if (fresh.openPlanId !== undefined) {
|
|
3497
3625
|
throw new ConsolidationRefusedError("memory.consolidation_busy", `memory consolidation refused: plan ${fresh.openPlanId} claimed the ${JSON.stringify(scope)} seat during the snapshot read.`);
|
|
@@ -3506,15 +3634,10 @@ export class MemoryEngine {
|
|
|
3506
3634
|
announce = true;
|
|
3507
3635
|
}
|
|
3508
3636
|
file.scopes[scope] = fresh;
|
|
3509
|
-
return {
|
|
3637
|
+
return { writeGate: true, result: announce };
|
|
3510
3638
|
});
|
|
3511
3639
|
if (opts.force !== undefined && announceForce) {
|
|
3512
|
-
|
|
3513
|
-
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)})`] });
|
|
3514
|
-
}
|
|
3515
|
-
catch (err) {
|
|
3516
|
-
this.discloseAnnounceFailure("consolidation force audit enqueue", err);
|
|
3517
|
-
}
|
|
3640
|
+
await this.announce(scope, { kind: "gate", at, items: [`memory consolidation snapshot FORCED past the time/session gates for scope ${inlineUntrusted(scope, 80)} (requestId ${inlineUntrusted(opts.force.requestId, 80)})`] }, "consolidation force audit enqueue");
|
|
3518
3641
|
}
|
|
3519
3642
|
return { cycleToken, watermark: { epoch, at }, candidates };
|
|
3520
3643
|
}
|
|
@@ -3541,7 +3664,7 @@ export class MemoryEngine {
|
|
|
3541
3664
|
}
|
|
3542
3665
|
let credentials = {};
|
|
3543
3666
|
try {
|
|
3544
|
-
credentials =
|
|
3667
|
+
credentials = await this.governanceStore.readIntents();
|
|
3545
3668
|
}
|
|
3546
3669
|
catch (err) {
|
|
3547
3670
|
throw new ConsolidationRefusedError("memory.consolidation_gate_corrupt", `memory consolidation refused: the intent-credential account cannot be trusted (fail-closed): ${thrownText(err)}`);
|
|
@@ -3569,7 +3692,7 @@ export class MemoryEngine {
|
|
|
3569
3692
|
if (reasons.length > 0)
|
|
3570
3693
|
throw new ConsolidationRefusedError("memory.consolidation_refused", "memory consolidation plan refused (structural)", reasons);
|
|
3571
3694
|
const planId = uuidv7();
|
|
3572
|
-
const snapshot =
|
|
3695
|
+
const snapshot = await this.governanceStore.transact([], (file) => {
|
|
3573
3696
|
const row = file.scopes[scope] ?? { sessions: { count: 0, ring: [] }, epoch: 0 };
|
|
3574
3697
|
if (row.openPlanId !== undefined) {
|
|
3575
3698
|
throw new ConsolidationRefusedError("memory.consolidation_busy", `memory consolidation refused: plan ${row.openPlanId} already holds the ${JSON.stringify(scope)} seat (one plan at a time; resolve or reconcile it first).`);
|
|
@@ -3580,20 +3703,20 @@ export class MemoryEngine {
|
|
|
3580
3703
|
row.openPlanId = planId;
|
|
3581
3704
|
row.seatClaimedAt = this.now();
|
|
3582
3705
|
file.scopes[scope] = row;
|
|
3583
|
-
return {
|
|
3706
|
+
return { writeGate: true, result: row.snapshot };
|
|
3584
3707
|
});
|
|
3585
3708
|
let frozenLeaseToken;
|
|
3586
3709
|
if (screened.lease !== undefined) {
|
|
3587
3710
|
const held = (await screened.lease.acquire(scope)) ?? undefined;
|
|
3588
3711
|
if (held === undefined) {
|
|
3589
|
-
|
|
3712
|
+
await this.governanceStore.transact([], (file) => {
|
|
3590
3713
|
const row = file.scopes[scope];
|
|
3591
3714
|
if (row?.openPlanId === planId) {
|
|
3592
3715
|
delete row.openPlanId;
|
|
3593
3716
|
file.scopes[scope] = row;
|
|
3594
|
-
return {
|
|
3717
|
+
return { writeGate: true, result: undefined };
|
|
3595
3718
|
}
|
|
3596
|
-
return { result: undefined };
|
|
3719
|
+
return { writeGate: false, result: undefined };
|
|
3597
3720
|
});
|
|
3598
3721
|
throw new ConsolidationRefusedError("memory.consolidation_lease_unavailable", `memory consolidation refused: the global lease for scope ${JSON.stringify(scope)} could not be acquired.`);
|
|
3599
3722
|
}
|
|
@@ -3611,22 +3734,17 @@ export class MemoryEngine {
|
|
|
3611
3734
|
catch (err) {
|
|
3612
3735
|
await this.releaseConsolidationLease(scope);
|
|
3613
3736
|
if (!frozen && err instanceof ConsolidationRefusedError) {
|
|
3614
|
-
|
|
3737
|
+
await this.governanceStore.transact([], (file) => {
|
|
3615
3738
|
const row = file.scopes[scope];
|
|
3616
3739
|
if (row?.openPlanId === planId) {
|
|
3617
3740
|
delete row.openPlanId;
|
|
3618
3741
|
file.scopes[scope] = row;
|
|
3619
|
-
return {
|
|
3742
|
+
return { writeGate: true, result: undefined };
|
|
3620
3743
|
}
|
|
3621
|
-
return { result: undefined };
|
|
3744
|
+
return { writeGate: false, result: undefined };
|
|
3622
3745
|
});
|
|
3623
3746
|
if (err.code === "memory.consolidation_refused") {
|
|
3624
|
-
|
|
3625
|
-
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)}`] });
|
|
3626
|
-
}
|
|
3627
|
-
catch (annErr) {
|
|
3628
|
-
this.discloseAnnounceFailure("consolidation refusal enqueue", annErr);
|
|
3629
|
-
}
|
|
3747
|
+
await this.announce(scope, { kind: "gate", at: this.now(), items: [`memory consolidation plan for scope ${inlineUntrusted(scope, 80)} REFUSED:${inlineUntrusted(err.reasons[0] ?? err.message, 200)}`] }, "consolidation refusal enqueue");
|
|
3630
3748
|
}
|
|
3631
3749
|
}
|
|
3632
3750
|
throw err;
|
|
@@ -3758,12 +3876,7 @@ export class MemoryEngine {
|
|
|
3758
3876
|
edgeTargets.add(i2.id);
|
|
3759
3877
|
if (edgeTargets.size > ceiling) {
|
|
3760
3878
|
wholeReasons.push(`supersession fuse: ${edgeTargets.size} distinct targets > ceiling ${ceiling} (active set ${activeSetSize}, ratio ${screened.supersedeRatioCap}, floor ${screened.supersedeAbsoluteFloor}, N-1 unconditional)`);
|
|
3761
|
-
|
|
3762
|
-
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)`] });
|
|
3763
|
-
}
|
|
3764
|
-
catch (err) {
|
|
3765
|
-
this.discloseAnnounceFailure("consolidation fuse enqueue", err);
|
|
3766
|
-
}
|
|
3879
|
+
await this.announce(scope, { kind: "gate", at, items: [`memory consolidation plan for scope ${inlineUntrusted(scope, 80)} REFUSED by the supersession fuse (${edgeTargets.size} targets over the ${ceiling} ceiling)`] }, "consolidation fuse enqueue");
|
|
3767
3880
|
}
|
|
3768
3881
|
const scanCeiling = scanRefusalCeiling(proposal.products.length, screened);
|
|
3769
3882
|
if (scanRefused.length > scanCeiling) {
|
|
@@ -3832,7 +3945,7 @@ export class MemoryEngine {
|
|
|
3832
3945
|
if (wholeReasons.length > 0)
|
|
3833
3946
|
throw new ConsolidationRefusedError("memory.consolidation_refused", "memory consolidation plan refused", wholeReasons);
|
|
3834
3947
|
try {
|
|
3835
|
-
const seatNow =
|
|
3948
|
+
const seatNow = (await this.governanceStore.readGate())[scope]?.openPlanId;
|
|
3836
3949
|
if (seatNow !== planId) {
|
|
3837
3950
|
throw new ConsolidationRefusedError("memory.consolidation_busy", `memory consolidation refused: the ${JSON.stringify(scope)} seat was reclaimed during the freeze (a reconcile judged the claim stale) — retake the snapshot and commit again.`);
|
|
3838
3951
|
}
|
|
@@ -3878,15 +3991,15 @@ export class MemoryEngine {
|
|
|
3878
3991
|
},
|
|
3879
3992
|
],
|
|
3880
3993
|
};
|
|
3881
|
-
|
|
3994
|
+
await this.writePlan(plan);
|
|
3882
3995
|
return { plan, refusedProducts };
|
|
3883
3996
|
}
|
|
3884
3997
|
async applyConsolidationPlan(plan, frozenLeaseToken, writeGate) {
|
|
3885
|
-
this.claimPlanForApply(plan);
|
|
3998
|
+
await this.claimPlanForApply(plan);
|
|
3886
3999
|
if (plan.state === "open") {
|
|
3887
4000
|
plan.state = "applying";
|
|
3888
4001
|
plan.audit.push({ at: this.now(), event: "applying" });
|
|
3889
|
-
this.writePlanOwned(plan);
|
|
4002
|
+
await this.writePlanOwned(plan);
|
|
3890
4003
|
}
|
|
3891
4004
|
const face = this.committedAuditFace();
|
|
3892
4005
|
const admit = admitLandingsThrough(writeGate, (scope) => this.consolidationWriteHome(scope));
|
|
@@ -3908,7 +4021,7 @@ export class MemoryEngine {
|
|
|
3908
4021
|
const lineageRows = pendingProducts.map((p) => ({ entryId: p.id, rev: p.rev, ...(p.frontmatter.origin !== undefined ? { marked: true } : {}) }));
|
|
3909
4022
|
let lineageArmed = false;
|
|
3910
4023
|
try {
|
|
3911
|
-
stageLineagePending(this.
|
|
4024
|
+
await stageLineagePending(this.governanceStore, txnId, `consolidation:${plan.planId}`, lineageRows, this.now);
|
|
3912
4025
|
lineageArmed = true;
|
|
3913
4026
|
}
|
|
3914
4027
|
catch (err) {
|
|
@@ -3946,8 +4059,8 @@ export class MemoryEngine {
|
|
|
3946
4059
|
}
|
|
3947
4060
|
if (lineageArmed) {
|
|
3948
4061
|
try {
|
|
3949
|
-
recordLineageCredential(this.
|
|
3950
|
-
this.settlePromotions(promoteLineagePending(this.
|
|
4062
|
+
await recordLineageCredential(this.governanceStore, txnId, report.applied.filter((a) => a.op !== "delete").map((a) => a.id), this.now);
|
|
4063
|
+
await this.settlePromotions(await promoteLineagePending(this.governanceStore, txnId));
|
|
3951
4064
|
}
|
|
3952
4065
|
catch (err) {
|
|
3953
4066
|
plan.audit.push({ at: this.now(), event: "lineage_settle_failed", detail: thrownText(err) });
|
|
@@ -3963,7 +4076,7 @@ export class MemoryEngine {
|
|
|
3963
4076
|
}
|
|
3964
4077
|
}
|
|
3965
4078
|
}
|
|
3966
|
-
this.writePlanOwned(plan);
|
|
4079
|
+
await this.writePlanOwned(plan);
|
|
3967
4080
|
}
|
|
3968
4081
|
if (pendingDirected.length > 0) {
|
|
3969
4082
|
if (!(await this.consolidationLeaseCurrent(plan.scope, frozenLeaseToken))) {
|
|
@@ -4017,10 +4130,10 @@ export class MemoryEngine {
|
|
|
4017
4130
|
plan.audit.push({ at: this.now(), event: "directed_conflict", detail: `${d.id}: ${conflict.reason}` });
|
|
4018
4131
|
}
|
|
4019
4132
|
}
|
|
4020
|
-
this.writePlanOwned(plan);
|
|
4133
|
+
await this.writePlanOwned(plan);
|
|
4021
4134
|
}
|
|
4022
4135
|
try {
|
|
4023
|
-
const post = this.readChallengeExclusions();
|
|
4136
|
+
const post = await this.readChallengeExclusions();
|
|
4024
4137
|
for (const p of plan.products) {
|
|
4025
4138
|
if (plan.productStates[p.id] !== "applied")
|
|
4026
4139
|
continue;
|
|
@@ -4028,7 +4141,7 @@ export class MemoryEngine {
|
|
|
4028
4141
|
continue;
|
|
4029
4142
|
const hitInputs = (p.frontmatter.distilled?.inputs ?? []).filter((i) => post.has(i.id)).map((i) => i.id);
|
|
4030
4143
|
if (hitInputs.length > 0) {
|
|
4031
|
-
this.challengeEntries([p.id], `consolidation input ${hitInputs[0]} was challenged in the freeze→apply window (retroactive lineage propagation)`, `consolidation:${plan.planId}:recheck`);
|
|
4144
|
+
await this.challengeEntries([p.id], `consolidation input ${hitInputs[0]} was challenged in the freeze→apply window (retroactive lineage propagation)`, `consolidation:${plan.planId}:recheck`);
|
|
4032
4145
|
plan.audit.push({ at: this.now(), event: "post_apply_propagation", detail: `${p.id} challenged over inputs ${hitInputs.join(", ")}` });
|
|
4033
4146
|
}
|
|
4034
4147
|
}
|
|
@@ -4053,41 +4166,59 @@ export class MemoryEngine {
|
|
|
4053
4166
|
return refusal;
|
|
4054
4167
|
}
|
|
4055
4168
|
async applyPlanPatches(plan, patches, admit, frozen) {
|
|
4056
|
-
const
|
|
4057
|
-
|
|
4169
|
+
const read = () => this.governanceStore.readPlan(plan.planId);
|
|
4170
|
+
const first = read();
|
|
4171
|
+
const admits = this.backend.admitsLandings === true;
|
|
4172
|
+
if (admits && !(first instanceof Promise)) {
|
|
4173
|
+
this.assertPlanOwner(plan, first);
|
|
4174
|
+
const held = () => {
|
|
4175
|
+
const cur = read();
|
|
4176
|
+
if (cur instanceof Promise)
|
|
4177
|
+
throw new Error("the consolidation store's plan read changed form mid-apply (a synchronous store answered with a Promise) — the holder cannot be confirmed under the backend's lock");
|
|
4178
|
+
this.assertPlanOwner(plan, cur);
|
|
4179
|
+
};
|
|
4058
4180
|
return this.backend.applyPatches(patches, { admit, confirm: held });
|
|
4059
|
-
|
|
4060
|
-
|
|
4181
|
+
}
|
|
4182
|
+
if (first instanceof Promise)
|
|
4183
|
+
void first.catch(() => undefined);
|
|
4184
|
+
if (!admits)
|
|
4185
|
+
await admit(frozen);
|
|
4186
|
+
return applyEachAlone(this.backend, patches, async () => this.assertPlanOwner(plan, await read()), admits ? admit : undefined);
|
|
4061
4187
|
}
|
|
4062
4188
|
async settleThrownBatchLineage(txnId, products) {
|
|
4063
4189
|
const landed = (await planWritten({ products, directed: [] }, this.backend, this.committedAuditFace()))?.products;
|
|
4064
4190
|
if (landed === undefined)
|
|
4065
4191
|
return;
|
|
4066
4192
|
if (landed.length === 0)
|
|
4067
|
-
return discardLineagePending(this.
|
|
4068
|
-
recordLineageCredential(this.
|
|
4069
|
-
this.settlePromotions(promoteLineagePending(this.
|
|
4193
|
+
return await discardLineagePending(this.governanceStore, txnId);
|
|
4194
|
+
await recordLineageCredential(this.governanceStore, txnId, landed.map((l) => l.id), this.now);
|
|
4195
|
+
await this.settlePromotions(await promoteLineagePending(this.governanceStore, txnId));
|
|
4070
4196
|
}
|
|
4071
|
-
claimPlanForApply(plan) {
|
|
4197
|
+
async claimPlanForApply(plan) {
|
|
4072
4198
|
const token = uuidv7();
|
|
4073
|
-
|
|
4074
|
-
const cur =
|
|
4199
|
+
await this.governanceStore.transact([plan.planId], (tx) => {
|
|
4200
|
+
const cur = tx.plan(plan.planId);
|
|
4075
4201
|
if (cur.state !== "ok" || (cur.plan.state !== "open" && cur.plan.state !== "applying"))
|
|
4076
4202
|
this.throwPlanNotHeld(plan);
|
|
4077
4203
|
plan.applier = token;
|
|
4078
|
-
|
|
4079
|
-
return { result: undefined };
|
|
4204
|
+
tx.writePlan(plan);
|
|
4205
|
+
return { writeGate: false, result: undefined };
|
|
4206
|
+
});
|
|
4207
|
+
}
|
|
4208
|
+
async writePlanOwned(plan) {
|
|
4209
|
+
await this.governanceStore.transact([plan.planId], (tx) => {
|
|
4210
|
+
this.assertPlanOwner(plan, tx.plan(plan.planId));
|
|
4211
|
+
tx.writePlan(plan);
|
|
4212
|
+
return { writeGate: false, result: undefined };
|
|
4080
4213
|
});
|
|
4081
4214
|
}
|
|
4082
|
-
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
|
|
4086
|
-
return { result: undefined };
|
|
4215
|
+
async writePlan(plan) {
|
|
4216
|
+
await this.governanceStore.transact([], (tx) => {
|
|
4217
|
+
tx.writePlan(plan);
|
|
4218
|
+
return { writeGate: false, result: undefined };
|
|
4087
4219
|
});
|
|
4088
4220
|
}
|
|
4089
|
-
assertPlanOwner(plan) {
|
|
4090
|
-
const cur = readConsolidationPlan(this.controlDir, plan.planId);
|
|
4221
|
+
assertPlanOwner(plan, cur) {
|
|
4091
4222
|
const settled = cur.state === "ok" && (cur.plan.state === "completed" || cur.plan.state === "discarded" || cur.plan.state === "conflict");
|
|
4092
4223
|
if (cur.state !== "ok" || cur.plan.applier !== plan.applier || (settled && cur.plan.state !== plan.state))
|
|
4093
4224
|
this.throwPlanNotHeld(plan);
|
|
@@ -4100,7 +4231,7 @@ export class MemoryEngine {
|
|
|
4100
4231
|
const anyConflict = productStates.includes("conflict") || plan.directed.some((d) => d.state === "conflict");
|
|
4101
4232
|
const allTerminal = productStates.every((s) => s !== "pending") && plan.directed.every((d) => d.state !== "pending");
|
|
4102
4233
|
if (!allTerminal) {
|
|
4103
|
-
this.writePlanOwned(plan);
|
|
4234
|
+
await this.writePlanOwned(plan);
|
|
4104
4235
|
throw new ConsolidationRefusedError("memory.consolidation_backend_failed", `memory consolidation plan ${plan.planId} did not reach terminal state this pass — it stays durable and replays (reconcileConsolidation).`);
|
|
4105
4236
|
}
|
|
4106
4237
|
const at = this.now();
|
|
@@ -4108,13 +4239,8 @@ export class MemoryEngine {
|
|
|
4108
4239
|
if (anyConflict) {
|
|
4109
4240
|
plan.state = "conflict";
|
|
4110
4241
|
plan.audit.push({ at, event: "conflict" });
|
|
4111
|
-
this.writePlanOwned(plan);
|
|
4112
|
-
|
|
4113
|
-
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`] });
|
|
4114
|
-
}
|
|
4115
|
-
catch (err) {
|
|
4116
|
-
this.discloseAnnounceFailure("consolidation conflict enqueue", err);
|
|
4117
|
-
}
|
|
4242
|
+
await this.writePlanOwned(plan);
|
|
4243
|
+
await this.announce(plan.scope, { 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`] }, "consolidation conflict enqueue");
|
|
4118
4244
|
notices.push(memoryConsolidationConflictNotice({ planId: plan.planId, scope: plan.scope }));
|
|
4119
4245
|
return this.consolidationReceipt(plan, notices);
|
|
4120
4246
|
}
|
|
@@ -4128,7 +4254,7 @@ export class MemoryEngine {
|
|
|
4128
4254
|
patches: plan.directed.filter((d) => d.intentRequestId === it.requestId).map((d) => ({ op: d.op, id: d.id, ...(d.attribution !== undefined ? { attribution: d.attribution } : {}) })),
|
|
4129
4255
|
}));
|
|
4130
4256
|
try {
|
|
4131
|
-
const written = new Set(
|
|
4257
|
+
const written = new Set(await this.governanceStore.recordIntents(credentialRows));
|
|
4132
4258
|
for (const it of plan.intents)
|
|
4133
4259
|
it.state = "settled";
|
|
4134
4260
|
for (const row2 of credentialRows) {
|
|
@@ -4140,16 +4266,16 @@ export class MemoryEngine {
|
|
|
4140
4266
|
catch (err) {
|
|
4141
4267
|
plan.state = "applying";
|
|
4142
4268
|
plan.audit.push({ at, event: "credential_write_failed", detail: thrownText(err) });
|
|
4143
|
-
this.writePlanOwned(plan);
|
|
4269
|
+
await this.writePlanOwned(plan);
|
|
4144
4270
|
throw new ConsolidationRefusedError("memory.consolidation_governance_unreadable", `memory consolidation plan ${plan.planId} committed its writes but could not record the intent credentials — the plan stays durable and replays (reconcileConsolidation): ${thrownText(err)}`);
|
|
4145
4271
|
}
|
|
4146
|
-
this.writePlanOwned(plan);
|
|
4272
|
+
await this.writePlanOwned(plan);
|
|
4147
4273
|
const currentlyEligible = await this.settleFingerprintFilter(plan.scope);
|
|
4148
4274
|
const freezeRefused = new Set(plan.freezeRefusedInputIds ?? []);
|
|
4149
|
-
|
|
4275
|
+
await this.governanceStore.transact([], (file) => {
|
|
4150
4276
|
const row = file.scopes[plan.scope];
|
|
4151
4277
|
if (row?.openPlanId !== plan.planId)
|
|
4152
|
-
return { result: undefined };
|
|
4278
|
+
return { writeGate: false, result: undefined };
|
|
4153
4279
|
delete row.openPlanId;
|
|
4154
4280
|
row.lastRunAt = at;
|
|
4155
4281
|
row.epoch += 1;
|
|
@@ -4172,21 +4298,11 @@ export class MemoryEngine {
|
|
|
4172
4298
|
delete row.snapshot;
|
|
4173
4299
|
}
|
|
4174
4300
|
file.scopes[plan.scope] = row;
|
|
4175
|
-
return {
|
|
4301
|
+
return { writeGate: true, result: undefined };
|
|
4176
4302
|
});
|
|
4177
4303
|
const productCount = Object.values(plan.productStates).filter((s) => s === "applied").length;
|
|
4178
4304
|
const supersededCount = new Set(plan.products.flatMap((p) => (p.frontmatter.distilled?.inputs ?? []).filter((i) => i.superseded === true).map((i) => i.id))).size;
|
|
4179
|
-
|
|
4180
|
-
enqueueMemoryAnnouncement(this.controlDir, {
|
|
4181
|
-
kind: "gate",
|
|
4182
|
-
scope: plan.scope,
|
|
4183
|
-
at,
|
|
4184
|
-
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`],
|
|
4185
|
-
});
|
|
4186
|
-
}
|
|
4187
|
-
catch (err) {
|
|
4188
|
-
this.discloseAnnounceFailure("consolidation completion enqueue", err);
|
|
4189
|
-
}
|
|
4305
|
+
await this.announce(plan.scope, { kind: "gate", at, 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`] }, "consolidation completion enqueue");
|
|
4190
4306
|
notices.push(memoryConsolidationCommittedNotice({ planId: plan.planId, scope: plan.scope, products: productCount, superseded: supersededCount, intents: plan.intents.length }));
|
|
4191
4307
|
return this.consolidationReceipt(plan, notices);
|
|
4192
4308
|
}
|
|
@@ -4210,18 +4326,18 @@ export class MemoryEngine {
|
|
|
4210
4326
|
async reconcileConsolidation(scope, opts) {
|
|
4211
4327
|
requireWriteGate(opts?.writeGate, "reconcileConsolidation");
|
|
4212
4328
|
this.requireConsolidation();
|
|
4213
|
-
const gate = this.readConsolidationGateOrRefuse();
|
|
4329
|
+
const gate = await this.readConsolidationGateOrRefuse();
|
|
4214
4330
|
const row = gate.scopes[scope];
|
|
4215
4331
|
const out = { propagatedChallenges: [] };
|
|
4216
4332
|
if (row?.openPlanId !== undefined) {
|
|
4217
4333
|
const seated = row.openPlanId;
|
|
4218
|
-
const read =
|
|
4334
|
+
const read = await this.governanceStore.readPlan(seated);
|
|
4219
4335
|
if (read.state === "absent") {
|
|
4220
4336
|
if (this.now() - (row.seatClaimedAt ?? 0) < CONSOLIDATION_SEAT_FREEZE_GRACE_MS) {
|
|
4221
4337
|
out.seatInFlight = seated;
|
|
4222
4338
|
}
|
|
4223
4339
|
else {
|
|
4224
|
-
this.releaseOrphanConsolidationSeat(scope, seated, out);
|
|
4340
|
+
await this.releaseOrphanConsolidationSeat(scope, seated, out);
|
|
4225
4341
|
}
|
|
4226
4342
|
}
|
|
4227
4343
|
else if (read.state === "corrupt") {
|
|
@@ -4244,29 +4360,24 @@ export class MemoryEngine {
|
|
|
4244
4360
|
await this.runConsolidationPropagationSweep(scope, out);
|
|
4245
4361
|
return out;
|
|
4246
4362
|
}
|
|
4247
|
-
releaseOrphanConsolidationSeat(scope, seated, out) {
|
|
4248
|
-
|
|
4363
|
+
async releaseOrphanConsolidationSeat(scope, seated, out) {
|
|
4364
|
+
await this.governanceStore.transact([], (file) => {
|
|
4249
4365
|
const r = file.scopes[scope];
|
|
4250
4366
|
if (r?.openPlanId === seated) {
|
|
4251
4367
|
delete r.openPlanId;
|
|
4252
4368
|
file.scopes[scope] = r;
|
|
4253
|
-
return {
|
|
4369
|
+
return { writeGate: true, result: undefined };
|
|
4254
4370
|
}
|
|
4255
|
-
return { result: undefined };
|
|
4371
|
+
return { writeGate: false, result: undefined };
|
|
4256
4372
|
});
|
|
4257
|
-
|
|
4258
|
-
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`] });
|
|
4259
|
-
}
|
|
4260
|
-
catch (err) {
|
|
4261
|
-
this.discloseAnnounceFailure("consolidation orphan-seat enqueue", err);
|
|
4262
|
-
}
|
|
4373
|
+
await this.announce(scope, { 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`] }, "consolidation orphan-seat enqueue");
|
|
4263
4374
|
out.releasedOrphanSeat = seated;
|
|
4264
4375
|
}
|
|
4265
4376
|
async runConsolidationPropagationSweep(scope, out) {
|
|
4266
4377
|
try {
|
|
4267
4378
|
const face = this.committedAuditFace();
|
|
4268
4379
|
const headers = await face.listHeaders([scope]);
|
|
4269
|
-
const exclusions = this.readChallengeExclusions();
|
|
4380
|
+
const exclusions = await this.readChallengeExclusions();
|
|
4270
4381
|
const productHeaders = headers.filter((h) => h.distilled !== undefined && !exclusions.has(h.id));
|
|
4271
4382
|
if (productHeaders.length > 0) {
|
|
4272
4383
|
const products = await face.getByIds(productHeaders.map((h) => h.id));
|
|
@@ -4274,7 +4385,7 @@ export class MemoryEngine {
|
|
|
4274
4385
|
const challengedInputs = (p.frontmatter.distilled?.inputs ?? []).filter((i) => exclusions.get(i.id)?.code === "challenged").map((i) => i.id);
|
|
4275
4386
|
if (challengedInputs.length === 0)
|
|
4276
4387
|
continue;
|
|
4277
|
-
this.challengeEntries([p.id], `consolidation input ${challengedInputs[0]} is challenged (retroactive lineage propagation — the product distilled from it)`, `consolidation-reconcile:${uuidv7()}`);
|
|
4388
|
+
await this.challengeEntries([p.id], `consolidation input ${challengedInputs[0]} is challenged (retroactive lineage propagation — the product distilled from it)`, `consolidation-reconcile:${uuidv7()}`);
|
|
4278
4389
|
out.propagatedChallenges.push(p.id);
|
|
4279
4390
|
}
|
|
4280
4391
|
}
|
|
@@ -4286,10 +4397,10 @@ export class MemoryEngine {
|
|
|
4286
4397
|
async settleGateForTerminalPlan(plan) {
|
|
4287
4398
|
const currentlyEligible = await this.settleFingerprintFilter(plan.scope);
|
|
4288
4399
|
const freezeRefused = new Set(plan.freezeRefusedInputIds ?? []);
|
|
4289
|
-
|
|
4400
|
+
await this.governanceStore.transact([], (file) => {
|
|
4290
4401
|
const row = file.scopes[plan.scope];
|
|
4291
4402
|
if (row?.openPlanId !== plan.planId)
|
|
4292
|
-
return { result: undefined };
|
|
4403
|
+
return { writeGate: false, result: undefined };
|
|
4293
4404
|
delete row.openPlanId;
|
|
4294
4405
|
row.epoch += 1;
|
|
4295
4406
|
if (plan.state === "completed") {
|
|
@@ -4314,13 +4425,13 @@ export class MemoryEngine {
|
|
|
4314
4425
|
}
|
|
4315
4426
|
delete row.snapshot;
|
|
4316
4427
|
file.scopes[plan.scope] = row;
|
|
4317
|
-
return {
|
|
4428
|
+
return { writeGate: true, result: undefined };
|
|
4318
4429
|
});
|
|
4319
4430
|
}
|
|
4320
4431
|
async settleFingerprintFilter(scope) {
|
|
4321
4432
|
try {
|
|
4322
4433
|
const headers = await this.committedAuditFace().listHeaders([scope]);
|
|
4323
|
-
const excl = this.readChallengeExclusions();
|
|
4434
|
+
const excl = await this.readChallengeExclusions();
|
|
4324
4435
|
const sup = deriveSupersededSet(headers, { excluded: (id) => excl.has(id) });
|
|
4325
4436
|
const revById = new Map(headers.map((h) => [h.id, h.rev]));
|
|
4326
4437
|
return (id, rev) => !excl.has(id) && !sup.has(id) && revById.get(id) === rev;
|
|
@@ -4335,15 +4446,15 @@ export class MemoryEngine {
|
|
|
4335
4446
|
if (!isValidConsolidationPlanId(planId)) {
|
|
4336
4447
|
throw new ConsolidationRefusedError("memory.consolidation_refused", `memory consolidation plan id ${JSON.stringify(planId.slice(0, 80))} fails the plan-id grammar — refused before any path use.`);
|
|
4337
4448
|
}
|
|
4338
|
-
const read =
|
|
4449
|
+
const read = await this.governanceStore.readPlan(planId);
|
|
4339
4450
|
const at = this.now();
|
|
4340
4451
|
if (read.state === "corrupt") {
|
|
4341
4452
|
if (action === "retry") {
|
|
4342
4453
|
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).`);
|
|
4343
4454
|
}
|
|
4344
|
-
const custody =
|
|
4455
|
+
const custody = await this.governanceStore.quarantinePlan(planId);
|
|
4345
4456
|
let heldBy;
|
|
4346
|
-
|
|
4457
|
+
await this.governanceStore.transact([], (file) => {
|
|
4347
4458
|
for (const [scope, row] of Object.entries(file.scopes)) {
|
|
4348
4459
|
if (row.openPlanId === planId) {
|
|
4349
4460
|
delete row.openPlanId;
|
|
@@ -4351,12 +4462,12 @@ export class MemoryEngine {
|
|
|
4351
4462
|
delete row.snapshot;
|
|
4352
4463
|
file.scopes[scope] = row;
|
|
4353
4464
|
heldBy = scope;
|
|
4354
|
-
return {
|
|
4465
|
+
return { writeGate: true, result: undefined };
|
|
4355
4466
|
}
|
|
4356
4467
|
}
|
|
4357
|
-
return { result: undefined };
|
|
4468
|
+
return { writeGate: false, result: undefined };
|
|
4358
4469
|
});
|
|
4359
|
-
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");
|
|
4470
|
+
await 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");
|
|
4360
4471
|
return { planId, action, state: "discarded", ...(custody !== undefined ? { custody } : {}) };
|
|
4361
4472
|
}
|
|
4362
4473
|
if (read.state === "absent") {
|
|
@@ -4375,7 +4486,7 @@ export class MemoryEngine {
|
|
|
4375
4486
|
}
|
|
4376
4487
|
let standingCredentials = {};
|
|
4377
4488
|
try {
|
|
4378
|
-
standingCredentials =
|
|
4489
|
+
standingCredentials = await this.governanceStore.readIntents();
|
|
4379
4490
|
}
|
|
4380
4491
|
catch (err) {
|
|
4381
4492
|
throw new ConsolidationRefusedError("memory.consolidation_gate_corrupt", `memory consolidation retry refused: the intent-credential account cannot be trusted (fail-closed): ${thrownText(err)}`);
|
|
@@ -4402,7 +4513,7 @@ export class MemoryEngine {
|
|
|
4402
4513
|
const face = this.committedAuditFace();
|
|
4403
4514
|
const originalIntents = await this.reconstructIntentsForRetry(plan, pendingIntents, face);
|
|
4404
4515
|
const retryPlanId = uuidv7();
|
|
4405
|
-
|
|
4516
|
+
await this.governanceStore.transact([], (file) => {
|
|
4406
4517
|
const row = file.scopes[plan.scope] ?? { sessions: { count: 0, ring: [] }, epoch: 0 };
|
|
4407
4518
|
if (row.openPlanId !== plan.planId && row.openPlanId !== undefined) {
|
|
4408
4519
|
throw new ConsolidationRefusedError("memory.consolidation_busy", `memory consolidation retry refused: plan ${row.openPlanId} holds the ${JSON.stringify(plan.scope)} seat.`);
|
|
@@ -4410,7 +4521,7 @@ export class MemoryEngine {
|
|
|
4410
4521
|
row.openPlanId = retryPlanId;
|
|
4411
4522
|
row.seatClaimedAt = at;
|
|
4412
4523
|
file.scopes[plan.scope] = row;
|
|
4413
|
-
return {
|
|
4524
|
+
return { writeGate: true, result: undefined };
|
|
4414
4525
|
});
|
|
4415
4526
|
const inheritedRefusals = [...new Set([...(plan.freezeRefusedInputIds ?? []), ...conflictProductInputIds])].sort();
|
|
4416
4527
|
const retryPlan = {
|
|
@@ -4432,10 +4543,10 @@ export class MemoryEngine {
|
|
|
4432
4543
|
...originalIntents.reasons.map((r) => ({ at, event: "directed_conflict", detail: `${r.id}: ${r.reason}` })),
|
|
4433
4544
|
],
|
|
4434
4545
|
};
|
|
4435
|
-
|
|
4546
|
+
await this.writePlan(retryPlan);
|
|
4436
4547
|
plan.state = "discarded";
|
|
4437
4548
|
plan.audit.push({ at, event: "superseded_by_retry", requestId: opts.requestId, detail: `retried as ${retryPlanId}` });
|
|
4438
|
-
|
|
4549
|
+
await this.writePlan(plan);
|
|
4439
4550
|
try {
|
|
4440
4551
|
const receipt = await this.applyConsolidationPlan(retryPlan, undefined, opts.writeGate);
|
|
4441
4552
|
return { planId, action, state: receipt.state, retryPlanId, retryReceipt: receipt };
|
|
@@ -4446,27 +4557,22 @@ export class MemoryEngine {
|
|
|
4446
4557
|
}
|
|
4447
4558
|
async discardConsolidationPlan(plan, at, requestId, by, detail, owned = false) {
|
|
4448
4559
|
const planId = plan.planId;
|
|
4449
|
-
const write = () => (owned ? this.writePlanOwned(plan) :
|
|
4560
|
+
const write = () => (owned ? this.writePlanOwned(plan) : this.writePlan(plan));
|
|
4450
4561
|
plan.state = "discarded";
|
|
4451
4562
|
plan.audit.push({ at, event: "discarded", requestId, ...(detail !== undefined ? { detail } : {}) });
|
|
4452
4563
|
const abandoned = plan.intents.filter((i) => i.state === "pending");
|
|
4453
4564
|
for (const it of abandoned)
|
|
4454
4565
|
it.state = "abandoned";
|
|
4455
|
-
write();
|
|
4566
|
+
await write();
|
|
4456
4567
|
try {
|
|
4457
|
-
|
|
4568
|
+
await this.governanceStore.recordIntents(abandoned.map((it) => ({ requestId: it.requestId, state: "abandoned", planId, at, patches: [] })));
|
|
4458
4569
|
}
|
|
4459
4570
|
catch (err) {
|
|
4460
4571
|
plan.audit.push({ at, event: "abandon_credential_failed", detail: thrownText(err) });
|
|
4461
|
-
write();
|
|
4572
|
+
await write();
|
|
4462
4573
|
}
|
|
4463
4574
|
await this.settleGateForTerminalPlan(plan);
|
|
4464
|
-
|
|
4465
|
-
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)`] });
|
|
4466
|
-
}
|
|
4467
|
-
catch (err) {
|
|
4468
|
-
this.discloseAnnounceFailure("consolidation discard enqueue", err);
|
|
4469
|
-
}
|
|
4575
|
+
await this.announce(plan.scope, { kind: "gate", at, items: [`memory consolidation plan ${planId} DISCARDED ${by} —${abandoned.length} intent(s) settled ABANDONED (given up, not fulfilled)`] }, "consolidation discard enqueue");
|
|
4470
4576
|
return abandoned.map((i) => i.requestId);
|
|
4471
4577
|
}
|
|
4472
4578
|
async reconstructIntentsForRetry(plan, pendingIntentIds, face) {
|
|
@@ -4513,10 +4619,10 @@ export class MemoryEngine {
|
|
|
4513
4619
|
}
|
|
4514
4620
|
return { directed, reasons };
|
|
4515
4621
|
}
|
|
4516
|
-
listConsolidationPlans() {
|
|
4622
|
+
async listConsolidationPlans() {
|
|
4517
4623
|
const out = [];
|
|
4518
|
-
for (const planId of
|
|
4519
|
-
const read =
|
|
4624
|
+
for (const planId of await this.governanceStore.listPlanIds()) {
|
|
4625
|
+
const read = await this.governanceStore.readPlan(planId);
|
|
4520
4626
|
if (read.state === "corrupt") {
|
|
4521
4627
|
out.push({ planId, corrupt: true });
|
|
4522
4628
|
continue;
|
|
@@ -4528,8 +4634,8 @@ export class MemoryEngine {
|
|
|
4528
4634
|
}
|
|
4529
4635
|
return out;
|
|
4530
4636
|
}
|
|
4531
|
-
readConsolidationPlanFoldEvidence(planId) {
|
|
4532
|
-
const read =
|
|
4637
|
+
async readConsolidationPlanFoldEvidence(planId) {
|
|
4638
|
+
const read = await this.governanceStore.readPlan(planId);
|
|
4533
4639
|
if (read.state === "absent")
|
|
4534
4640
|
return { state: "absent" };
|
|
4535
4641
|
if (read.state === "corrupt")
|
|
@@ -4557,18 +4663,18 @@ export class MemoryEngine {
|
|
|
4557
4663
|
}
|
|
4558
4664
|
return { state: "ok", scope: p.scope, requestId: p.requestId, planState: p.state, supersededTargets: [...targets].sort() };
|
|
4559
4665
|
}
|
|
4560
|
-
readConsolidationGate(scope) {
|
|
4666
|
+
async readConsolidationGate(scope) {
|
|
4561
4667
|
try {
|
|
4562
|
-
return
|
|
4668
|
+
return (await this.governanceStore.readGate())[scope];
|
|
4563
4669
|
}
|
|
4564
4670
|
catch {
|
|
4565
4671
|
return undefined;
|
|
4566
4672
|
}
|
|
4567
4673
|
}
|
|
4568
|
-
readConsolidationGateState(scope) {
|
|
4674
|
+
async readConsolidationGateState(scope) {
|
|
4569
4675
|
let row;
|
|
4570
4676
|
try {
|
|
4571
|
-
row =
|
|
4677
|
+
row = (await this.governanceStore.readGate())[scope];
|
|
4572
4678
|
}
|
|
4573
4679
|
catch (err) {
|
|
4574
4680
|
return { state: "corrupt", detail: thrownText(err) };
|
|
@@ -4580,7 +4686,7 @@ export class MemoryEngine {
|
|
|
4580
4686
|
const headers = await face.listHeaders([scope]);
|
|
4581
4687
|
let exclusions;
|
|
4582
4688
|
try {
|
|
4583
|
-
exclusions = this.readChallengeExclusions();
|
|
4689
|
+
exclusions = await this.readChallengeExclusions();
|
|
4584
4690
|
}
|
|
4585
4691
|
catch (err) {
|
|
4586
4692
|
throw new ConsolidationRefusedError("memory.consolidation_governance_unreadable", `memory consolidation fold-state read refused: the challenge/lineage ledger cannot be trusted (fail-closed): ${thrownText(err)}`);
|
|
@@ -4595,6 +4701,12 @@ export class MemoryEngine {
|
|
|
4595
4701
|
get controlPlaneRoot() {
|
|
4596
4702
|
return this.controlDir;
|
|
4597
4703
|
}
|
|
4704
|
+
get governance() {
|
|
4705
|
+
return this.governanceStore;
|
|
4706
|
+
}
|
|
4707
|
+
async readConsolidationDriverRun(scope) {
|
|
4708
|
+
return this.governanceStore.readRun(scope);
|
|
4709
|
+
}
|
|
4598
4710
|
async listMemoryScopes() {
|
|
4599
4711
|
const b = this.backend;
|
|
4600
4712
|
if (typeof b.listScopes !== "function") {
|
|
@@ -4617,7 +4729,7 @@ export class MemoryEngine {
|
|
|
4617
4729
|
}
|
|
4618
4730
|
let mine;
|
|
4619
4731
|
try {
|
|
4620
|
-
const committed = readLineageRecord(this.
|
|
4732
|
+
const committed = (await readLineageRecord(this.governanceStore)).committed;
|
|
4621
4733
|
const ids = new Map();
|
|
4622
4734
|
let lastAt;
|
|
4623
4735
|
for (const [entryId, sessions] of Object.entries(committed)) {
|
|
@@ -4635,7 +4747,7 @@ export class MemoryEngine {
|
|
|
4635
4747
|
}
|
|
4636
4748
|
catch {
|
|
4637
4749
|
}
|
|
4638
|
-
const writeHome = await sessionWriteHomeOf({ memoryDir: this.memoryDir, controlDir: this.controlDir, sessionId, contributions: mine, readEntries: (ids) => this.committedAuditFace().getByIds(ids) });
|
|
4750
|
+
const writeHome = await sessionWriteHomeOf({ memoryDir: this.memoryDir, controlDir: this.controlDir, docs: this.governanceStore, sessionId, contributions: mine, readEntries: (ids) => this.committedAuditFace().getByIds(ids) });
|
|
4639
4751
|
if (writeHome !== undefined)
|
|
4640
4752
|
out.writeHome = writeHome;
|
|
4641
4753
|
if (mine !== undefined) {
|
|
@@ -4744,7 +4856,7 @@ export class MemoryEngine {
|
|
|
4744
4856
|
MemoryEngine.originClearRefusal("memory.origin_clear_not_marked", `clearEntryOrigin: entry ${JSON.stringify(entryId.slice(0, 80))} carries no external-origin marker — nothing to clear.` +
|
|
4745
4857
|
(lastClearance !== undefined ? ` A clearance for this entry already settled at ${MemoryEngine.spellSettledAt(lastClearance.settledAt)} (requestId ${JSON.stringify(lastClearance.requestId)}) — this reads as a re-send of it.` : ""), lastClearance !== undefined ? { lastClearance } : undefined);
|
|
4746
4858
|
}
|
|
4747
|
-
if (this.readChallengeExclusions().has(entryId)) {
|
|
4859
|
+
if ((await this.readChallengeExclusions()).has(entryId)) {
|
|
4748
4860
|
MemoryEngine.originClearRefusal("memory.origin_clear_challenged", `clearEntryOrigin: entry ${JSON.stringify(entryId.slice(0, 80))} is challenged/latched — adjudicate the challenge first (the clear valve is not a challenge exit).`);
|
|
4749
4861
|
}
|
|
4750
4862
|
const fm = { ...committed.frontmatter, ...(committed.frontmatter.extra !== undefined ? { extra: [...committed.frontmatter.extra] } : {}) };
|
|
@@ -4791,7 +4903,7 @@ export class MemoryEngine {
|
|
|
4791
4903
|
let landedSlug = row.slug;
|
|
4792
4904
|
let detail;
|
|
4793
4905
|
if (current !== undefined && committedOriginOf(current.frontmatter) !== undefined) {
|
|
4794
|
-
if (this.readChallengeExclusions().has(row.entryId)) {
|
|
4906
|
+
if ((await this.readChallengeExclusions()).has(row.entryId)) {
|
|
4795
4907
|
fail("memory.origin_clear_challenged", "the entry is challenged/latched — adjudicate the challenge first (the clear valve is not a challenge exit); the marked entry stands untouched and this row is terminal.", false);
|
|
4796
4908
|
}
|
|
4797
4909
|
if (current.rev !== row.baseRev) {
|
|
@@ -4863,17 +4975,7 @@ export class MemoryEngine {
|
|
|
4863
4975
|
detail = current.rev === cleared.rev ? "already complete (terminal event replay)" : "marker already absent (a later committed generation stands; custody retained on this row)";
|
|
4864
4976
|
}
|
|
4865
4977
|
settleOriginClearance(this.controlDir, { clearanceId: row.clearanceId, to: "done", requestId, eventId: uuidv7(), now: this.now, detail });
|
|
4866
|
-
|
|
4867
|
-
enqueueMemoryAnnouncement(this.controlDir, {
|
|
4868
|
-
kind: "gate",
|
|
4869
|
-
scope: row.scope,
|
|
4870
|
-
at: this.now(),
|
|
4871
|
-
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)})`],
|
|
4872
|
-
});
|
|
4873
|
-
}
|
|
4874
|
-
catch (err) {
|
|
4875
|
-
this.discloseAnnounceFailure("origin-clearance enqueue", err);
|
|
4876
|
-
}
|
|
4978
|
+
await this.announce(row.scope, { kind: "gate", at: this.now(), items: [`memory entry ${inlineUntrusted(row.entryId, 80)} external-origin marker CLEARED by host request ${JSON.stringify(inlineUntrusted(requestId, 80))} — re-recorded unmarked (${inlineUntrusted(detail, 160)})`] }, "origin-clearance enqueue");
|
|
4877
4979
|
return { entryId: row.entryId, clearanceId: row.clearanceId, origin: row.origin, landedSlug };
|
|
4878
4980
|
}
|
|
4879
4981
|
async rebaseline(handle, keepBaseline = new Set(), uncommittedPaths = new Set()) {
|
|
@@ -4906,6 +5008,8 @@ export class MemoryEngine {
|
|
|
4906
5008
|
const digest = uncommitted ? priorDigest : planeDigest(text);
|
|
4907
5009
|
if (digest !== undefined)
|
|
4908
5010
|
planeFiles.set(path, digest);
|
|
5011
|
+
if (!carriesEntry(text))
|
|
5012
|
+
continue;
|
|
4909
5013
|
const parsed = parseEntryFile(text);
|
|
4910
5014
|
const id = parsed.id ?? handle.baseIds.get(path);
|
|
4911
5015
|
if (id === undefined)
|
|
@@ -4937,14 +5041,13 @@ export class MemoryEngine {
|
|
|
4937
5041
|
handle.baseIds = baseIds;
|
|
4938
5042
|
const indexOnDisk = readSafe(join(writableRoot, MEMORY_INDEX_FILENAME));
|
|
4939
5043
|
handle.planeBaseline = { files: planeFiles, ...(indexOnDisk !== undefined ? { index: indexOnDisk } : {}) };
|
|
4940
|
-
handle.indexBaselineLines = countIndexLines(indexOnDisk ?? handle.indexText);
|
|
4941
5044
|
}
|
|
4942
|
-
rebuildIndex(handle, headers, opts, warnings) {
|
|
5045
|
+
async rebuildIndex(handle, headers, opts, warnings) {
|
|
4943
5046
|
const indexBase = handle.writableRoot ?? handle.memoryDir;
|
|
4944
5047
|
const indexPath = join(indexBase, MEMORY_INDEX_FILENAME);
|
|
4945
5048
|
let excludedIds;
|
|
4946
5049
|
try {
|
|
4947
|
-
excludedIds = this.readChallengeExclusions();
|
|
5050
|
+
excludedIds = await this.readChallengeExclusions();
|
|
4948
5051
|
}
|
|
4949
5052
|
catch (err) {
|
|
4950
5053
|
const detail = `memory index withheld: the challenge/lineage ledger cannot be trusted (${thrownText(err)}) — fail-closed, nothing injected`;
|
|
@@ -5309,7 +5412,7 @@ export class MemoryEngine {
|
|
|
5309
5412
|
return { state: "unknown" };
|
|
5310
5413
|
}
|
|
5311
5414
|
}
|
|
5312
|
-
censusScopeTree(scope, dir) {
|
|
5415
|
+
async censusScopeTree(scope, dir) {
|
|
5313
5416
|
if (!existsSync(dir))
|
|
5314
5417
|
return;
|
|
5315
5418
|
const unreadable = [];
|
|
@@ -5342,24 +5445,18 @@ export class MemoryEngine {
|
|
|
5342
5445
|
walk(dir);
|
|
5343
5446
|
if (unreadable.length + links.length === 0)
|
|
5344
5447
|
return;
|
|
5345
|
-
|
|
5346
|
-
|
|
5347
|
-
|
|
5348
|
-
|
|
5349
|
-
|
|
5350
|
-
|
|
5351
|
-
|
|
5352
|
-
|
|
5353
|
-
|
|
5354
|
-
|
|
5355
|
-
|
|
5356
|
-
|
|
5357
|
-
.concat(links.length > 12 ? [`…and ${links.length - 12} more skipped symlink(s)`] : []),
|
|
5358
|
-
});
|
|
5359
|
-
}
|
|
5360
|
-
catch (err) {
|
|
5361
|
-
this.discloseAnnounceFailure("layer-census enqueue", err);
|
|
5362
|
-
}
|
|
5448
|
+
await this.announce(scope, {
|
|
5449
|
+
kind: "gate",
|
|
5450
|
+
at: this.now(),
|
|
5451
|
+
items: unreadable
|
|
5452
|
+
.slice(0, 12)
|
|
5453
|
+
.map((p) => `layer census could not read ${JSON.stringify(inlineUntrusted(relative(dir, p) || "."))} — nothing under it was examined`)
|
|
5454
|
+
.concat(unreadable.length > 12 ? [`…and ${unreadable.length - 12} more unreadable path(s)`] : [])
|
|
5455
|
+
.concat(links
|
|
5456
|
+
.slice(0, 12)
|
|
5457
|
+
.map((p) => `layer census skipped the symlink ${JSON.stringify(inlineUntrusted(relative(dir, p) || "."))} — links are not part of a memory scope tree and are never followed`))
|
|
5458
|
+
.concat(links.length > 12 ? [`…and ${links.length - 12} more skipped symlink(s)`] : []),
|
|
5459
|
+
}, "layer-census enqueue");
|
|
5363
5460
|
}
|
|
5364
5461
|
}
|
|
5365
5462
|
const MAX_ANNOUNCEMENT_ITEMS = 12;
|
|
@@ -5392,7 +5489,7 @@ function gateAnnouncementItems(report, owner) {
|
|
|
5392
5489
|
for (const p of report.restored)
|
|
5393
5490
|
put(owner.ofRel(p), `restored from the committed copy: ${quoteId(p)}`);
|
|
5394
5491
|
for (const c of report.conflicts)
|
|
5395
|
-
put(ws,
|
|
5492
|
+
put(ws, harvestConflictSentence(c));
|
|
5396
5493
|
if (report.degraded !== undefined)
|
|
5397
5494
|
put(ws, `harvest degraded (${report.degraded.reason}): ${report.degraded.pending.length} file(s) deferred to the next harvest`);
|
|
5398
5495
|
for (const x of report.quarantineFailures ?? []) {
|