@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
|
@@ -3,7 +3,7 @@ const { O_WRONLY, O_RDONLY, O_CREAT, O_NOFOLLOW, O_NONBLOCK, O_EXCL } = fsConsta
|
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
4
|
import { createHash, randomUUID } from "node:crypto";
|
|
5
5
|
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
6
|
-
import { MEMORY_ORIGIN_CAUSES } from "./types.js";
|
|
6
|
+
import { MEMORY_GOVERNANCE_DOC_NAMES, MEMORY_ORIGIN_CAUSES } from "./types.js";
|
|
7
7
|
import { physicalPathOf, physicalPathOfExisting, priorCallerCasedPathOf } from "../physical-path.js";
|
|
8
8
|
import { thrownLabel, thrownText } from "../thrown-value.js";
|
|
9
9
|
import { egressText } from "../untrusted-egress.js";
|
|
@@ -214,6 +214,9 @@ export function scopeDirName(scope) {
|
|
|
214
214
|
}
|
|
215
215
|
const caseFoldByDirIdentity = new Map();
|
|
216
216
|
let caseFoldProbeSeq = 0;
|
|
217
|
+
export function forgetCaseFoldVerdicts() {
|
|
218
|
+
caseFoldByDirIdentity.clear();
|
|
219
|
+
}
|
|
217
220
|
function lstatOrAbsent(p) {
|
|
218
221
|
try {
|
|
219
222
|
return lstatSync(p);
|
|
@@ -736,7 +739,7 @@ function moveLooseContent(root, controlDir, rec, owner, label, renew, sink, resu
|
|
|
736
739
|
rmSync(join(root, p), { force: true });
|
|
737
740
|
}
|
|
738
741
|
renew();
|
|
739
|
-
|
|
742
|
+
clearIndexRevs(controlDir);
|
|
740
743
|
discloseLayoutIncident(sink, "memory.layout_migrated", `memory layout adopted at ${root}: ${moved} loose entr${moved === 1 ? "y" : "ies"} moved into the home of ${JSON.stringify(owner)} (${label})${resumed ? " — an interrupted move was finished" : ""}`);
|
|
741
744
|
}
|
|
742
745
|
export const canonicalize = physicalPathOfExisting;
|
|
@@ -970,7 +973,7 @@ export function tryStoreTxnLock(controlDir, opts) {
|
|
|
970
973
|
}
|
|
971
974
|
}
|
|
972
975
|
if (tokenPresent) {
|
|
973
|
-
const tomb = join(controlDir, `${STORE_TXN_LOCK_DIR}
|
|
976
|
+
const tomb = join(controlDir, `${STORE_TXN_LOCK_DIR}${LOCK_TOMB_INFIX}${randomUUID()}`);
|
|
974
977
|
let stolen = false;
|
|
975
978
|
try {
|
|
976
979
|
renameSync(lockDir, tomb);
|
|
@@ -1361,6 +1364,112 @@ export function quarantineAndTombstone(path, content, quarantineDir, now) {
|
|
|
1361
1364
|
...(detail !== undefined ? { detail } : {}),
|
|
1362
1365
|
};
|
|
1363
1366
|
}
|
|
1367
|
+
export const ATOMIC_WRITE_TMP_PREFIX = ".tmp-";
|
|
1368
|
+
export const LOCK_TOMB_INFIX = ".stale-";
|
|
1369
|
+
export const STORE_OPEN_ARTIFACTS = Object.freeze({
|
|
1370
|
+
files: Object.freeze([ROOT_ANCHOR_FILE, SCOPES_FILE, `${SCOPES_FILE}.journal`]),
|
|
1371
|
+
filePrefixes: Object.freeze([ATOMIC_WRITE_TMP_PREFIX]),
|
|
1372
|
+
lockDirs: Object.freeze([`${ROOT_ANCHOR_FILE}.lock`, `${SCOPES_FILE}.lock`, STORE_TXN_LOCK_DIR]),
|
|
1373
|
+
memoryFilePrefixes: Object.freeze([CASEFOLD_PROBE_PREFIX]),
|
|
1374
|
+
});
|
|
1375
|
+
export function isStoreOpenArtifact(where, rel) {
|
|
1376
|
+
const [top] = rel;
|
|
1377
|
+
if (top === undefined)
|
|
1378
|
+
return false;
|
|
1379
|
+
const starts = (prefixes) => prefixes.some((p) => top.startsWith(p));
|
|
1380
|
+
if (where === "memory")
|
|
1381
|
+
return rel.length === 1 && starts(STORE_OPEN_ARTIFACTS.memoryFilePrefixes);
|
|
1382
|
+
if (rel.length === 1)
|
|
1383
|
+
return STORE_OPEN_ARTIFACTS.files.includes(top) || starts(STORE_OPEN_ARTIFACTS.filePrefixes);
|
|
1384
|
+
return isLockTreeName(top);
|
|
1385
|
+
}
|
|
1386
|
+
function isLockTreeName(name) {
|
|
1387
|
+
return STORE_OPEN_ARTIFACTS.lockDirs.some((lock) => name === lock || name.startsWith(`${lock}${LOCK_TOMB_INFIX}`));
|
|
1388
|
+
}
|
|
1389
|
+
export function storeHoldsRecord(memoryDir, controlDir) {
|
|
1390
|
+
return treeHoldsRecord(controlDir, "control", (name) => namedArtifactDamaged(memoryDir, controlDir, name)) || treeHoldsRecord(memoryDir, "memory");
|
|
1391
|
+
}
|
|
1392
|
+
function namedArtifactDamaged(memoryDir, controlDir, name) {
|
|
1393
|
+
try {
|
|
1394
|
+
if (name === ROOT_ANCHOR_FILE)
|
|
1395
|
+
readStoreRootAnchor(controlDir, memoryDir);
|
|
1396
|
+
else if (name === SCOPES_FILE || name === `${SCOPES_FILE}.journal`) {
|
|
1397
|
+
for (const file of [`${SCOPES_FILE}.journal`, SCOPES_FILE])
|
|
1398
|
+
if (!absentOrRegularFile(join(controlDir, file)))
|
|
1399
|
+
return true;
|
|
1400
|
+
readScopesRecord(controlDir);
|
|
1401
|
+
}
|
|
1402
|
+
return false;
|
|
1403
|
+
}
|
|
1404
|
+
catch {
|
|
1405
|
+
return true;
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
function absentOrRegularFile(path) {
|
|
1409
|
+
const entry = lstatOrAbsent(path);
|
|
1410
|
+
if (entry === undefined)
|
|
1411
|
+
return true;
|
|
1412
|
+
return entry.isSymbolicLink() ? statSync(path).isFile() : entry.isFile();
|
|
1413
|
+
}
|
|
1414
|
+
const STORE_CENSUS_WALKS = 4;
|
|
1415
|
+
function treeHoldsRecord(dir, where, damaged) {
|
|
1416
|
+
const walk = (path, rel) => {
|
|
1417
|
+
let items;
|
|
1418
|
+
try {
|
|
1419
|
+
items = readdirSync(path, { withFileTypes: true });
|
|
1420
|
+
}
|
|
1421
|
+
catch (err) {
|
|
1422
|
+
if (err.code !== "ENOENT")
|
|
1423
|
+
return true;
|
|
1424
|
+
return rel.length === 0 ? absentDirReading(path) : "moved";
|
|
1425
|
+
}
|
|
1426
|
+
for (const item of items) {
|
|
1427
|
+
const at = [...rel, item.name];
|
|
1428
|
+
if (rel.length === 0 && damaged?.(item.name) === true)
|
|
1429
|
+
return true;
|
|
1430
|
+
const reading = !item.isDirectory() ? !isStoreOpenArtifact(where, at) : where === "control" && rel.length === 0 && isLockTreeName(item.name) ? false : walk(join(path, item.name), at);
|
|
1431
|
+
if (reading !== false)
|
|
1432
|
+
return reading;
|
|
1433
|
+
}
|
|
1434
|
+
return false;
|
|
1435
|
+
};
|
|
1436
|
+
let quiet = 0;
|
|
1437
|
+
for (let n = 0; n < STORE_CENSUS_WALKS; n += 1) {
|
|
1438
|
+
const reading = walk(dir, []);
|
|
1439
|
+
if (reading === true)
|
|
1440
|
+
return true;
|
|
1441
|
+
quiet = reading === false ? quiet + 1 : 0;
|
|
1442
|
+
if (quiet === 2)
|
|
1443
|
+
return false;
|
|
1444
|
+
}
|
|
1445
|
+
return true;
|
|
1446
|
+
}
|
|
1447
|
+
function absentDirReading(dir) {
|
|
1448
|
+
for (let at = dir;; at = dirname(at)) {
|
|
1449
|
+
let item;
|
|
1450
|
+
try {
|
|
1451
|
+
item = lstatSync(at, { throwIfNoEntry: false });
|
|
1452
|
+
}
|
|
1453
|
+
catch {
|
|
1454
|
+
return true;
|
|
1455
|
+
}
|
|
1456
|
+
if (item === undefined) {
|
|
1457
|
+
if (dirname(at) === at)
|
|
1458
|
+
return false;
|
|
1459
|
+
continue;
|
|
1460
|
+
}
|
|
1461
|
+
if (item.isSymbolicLink()) {
|
|
1462
|
+
try {
|
|
1463
|
+
if (statSync(at, { throwIfNoEntry: false }) === undefined)
|
|
1464
|
+
return true;
|
|
1465
|
+
}
|
|
1466
|
+
catch {
|
|
1467
|
+
return true;
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
return at === dir ? "moved" : false;
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1364
1473
|
export const ANNOUNCEMENTS_FILE = "announcements.json";
|
|
1365
1474
|
export const MEMORY_ANNOUNCEMENTS_MAX = 20;
|
|
1366
1475
|
export const SCAN_FUSE_FILE = "scan-fuse.json";
|
|
@@ -1378,7 +1487,7 @@ export function acquireSidecarLock(lockDir, opts = {}) {
|
|
|
1378
1487
|
const mintToken = () => `${process.pid}-${Math.random().toString(36).slice(2, 10)}`;
|
|
1379
1488
|
let deadlineRounds = 0;
|
|
1380
1489
|
const stealOnce = () => {
|
|
1381
|
-
const tomb = `${lockDir}
|
|
1490
|
+
const tomb = `${lockDir}${LOCK_TOMB_INFIX}${process.pid}-${Math.random().toString(36).slice(2, 8)}`;
|
|
1382
1491
|
try {
|
|
1383
1492
|
renameSync(lockDir, tomb);
|
|
1384
1493
|
}
|
|
@@ -1514,7 +1623,7 @@ function readSidecarJson(controlDir, fileName) {
|
|
|
1514
1623
|
}
|
|
1515
1624
|
const ANNOUNCEMENTS_FORM = 2;
|
|
1516
1625
|
function emptyAnnouncements() {
|
|
1517
|
-
return { scopes: Object.create(null)
|
|
1626
|
+
return { v: ANNOUNCEMENTS_FORM, scopes: Object.create(null) };
|
|
1518
1627
|
}
|
|
1519
1628
|
function wholeCount(v) {
|
|
1520
1629
|
return typeof v === "number" && Number.isFinite(v) && v > 0 ? Math.floor(v) : 0;
|
|
@@ -1545,13 +1654,11 @@ function coerceAnnouncements(raw) {
|
|
|
1545
1654
|
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
1546
1655
|
return rec;
|
|
1547
1656
|
const r = raw;
|
|
1548
|
-
if (r.v !== ANNOUNCEMENTS_FORM)
|
|
1549
|
-
rec
|
|
1550
|
-
|
|
1551
|
-
}
|
|
1552
|
-
rec.unscopedDiscarded = wholeCount(r.unscopedDiscarded);
|
|
1657
|
+
if (r.v !== ANNOUNCEMENTS_FORM)
|
|
1658
|
+
return withUnscoped(rec, (Array.isArray(r.queue) ? r.queue.length : 0) + wholeCount(r.folded));
|
|
1659
|
+
let unscoped = wholeCount(r.unscopedDiscarded);
|
|
1553
1660
|
if (!r.scopes || typeof r.scopes !== "object" || Array.isArray(r.scopes))
|
|
1554
|
-
return rec;
|
|
1661
|
+
return withUnscoped(rec, unscoped);
|
|
1555
1662
|
for (const scope of Object.keys(r.scopes)) {
|
|
1556
1663
|
const q = r.scopes[scope];
|
|
1557
1664
|
if (scope === "" || !q || typeof q !== "object")
|
|
@@ -1563,17 +1670,36 @@ function coerceAnnouncements(raw) {
|
|
|
1563
1670
|
if (a !== undefined)
|
|
1564
1671
|
queue.push(a);
|
|
1565
1672
|
else
|
|
1566
|
-
|
|
1673
|
+
unscoped += 1;
|
|
1567
1674
|
}
|
|
1568
1675
|
}
|
|
1569
1676
|
const folded = wholeCount(q.folded);
|
|
1570
1677
|
if (queue.length > 0 || folded > 0)
|
|
1571
1678
|
rec.scopes[scope] = { folded, queue };
|
|
1572
1679
|
}
|
|
1680
|
+
return withUnscoped(rec, unscoped);
|
|
1681
|
+
}
|
|
1682
|
+
function withUnscoped(rec, n) {
|
|
1683
|
+
if (n > 0)
|
|
1684
|
+
rec.unscopedDiscarded = n;
|
|
1685
|
+
else
|
|
1686
|
+
delete rec.unscopedDiscarded;
|
|
1573
1687
|
return rec;
|
|
1574
1688
|
}
|
|
1575
1689
|
function persistedAnnouncements(rec) {
|
|
1576
|
-
return { v: ANNOUNCEMENTS_FORM, scopes: rec.scopes, ...(rec.unscopedDiscarded > 0 ? { unscopedDiscarded: rec.unscopedDiscarded } : {}) };
|
|
1690
|
+
return { v: ANNOUNCEMENTS_FORM, scopes: rec.scopes, ...((rec.unscopedDiscarded ?? 0) > 0 ? { unscopedDiscarded: rec.unscopedDiscarded } : {}) };
|
|
1691
|
+
}
|
|
1692
|
+
const ANNOUNCEMENTS_DOC = "announcements";
|
|
1693
|
+
export function sayAnnounceFailure(sink, stage, cause) {
|
|
1694
|
+
if (sink === undefined)
|
|
1695
|
+
return;
|
|
1696
|
+
try {
|
|
1697
|
+
const err = new Error(`memory announcement queue ${stage} failed: ${thrownText(cause)}`);
|
|
1698
|
+
err.code = "memory.announce_failed";
|
|
1699
|
+
sink(err);
|
|
1700
|
+
}
|
|
1701
|
+
catch {
|
|
1702
|
+
}
|
|
1577
1703
|
}
|
|
1578
1704
|
function askedScopes(scopes, verb) {
|
|
1579
1705
|
if (!Array.isArray(scopes) || !scopes.every((s) => typeof s === "string" && s !== "")) {
|
|
@@ -1594,7 +1720,7 @@ function mergeScopeQueues(rec, scopes) {
|
|
|
1594
1720
|
lines.sort((x, y) => x.a.at - y.a.at || x.s - y.s || x.i - y.i);
|
|
1595
1721
|
return { folded, queue: lines.map((l) => l.a) };
|
|
1596
1722
|
}
|
|
1597
|
-
export function enqueueMemoryAnnouncement(
|
|
1723
|
+
export async function enqueueMemoryAnnouncement(docs, ann, opts) {
|
|
1598
1724
|
const o = opts === undefined ? undefined : receiveMembers(opts, { subject: "enqueueMemoryAnnouncement options", unit: "option", moment: "at enqueueMemoryAnnouncement" }, ANNOUNCEMENT_OPTION_KEYS);
|
|
1599
1725
|
if (typeof ann.scope !== "string" || ann.scope === "") {
|
|
1600
1726
|
throw new TypeError("enqueueMemoryAnnouncement: announcement.scope is required — each notice is filed under the one scope it reports on");
|
|
@@ -1604,19 +1730,19 @@ export function enqueueMemoryAnnouncement(controlDir, ann, opts) {
|
|
|
1604
1730
|
const family = o?.supersedesPrefix === undefined ? undefined : egressText("same-principal", o.supersedesPrefix);
|
|
1605
1731
|
if (ann.items.length === 0 && family === undefined)
|
|
1606
1732
|
return;
|
|
1607
|
-
|
|
1608
|
-
|
|
1733
|
+
const line = { ...ann, scope };
|
|
1734
|
+
await docs.updateDoc(ANNOUNCEMENTS_DOC, coerceAnnouncements, (rec) => {
|
|
1609
1735
|
const own = Object.prototype.hasOwnProperty.call(rec.scopes, scope) ? rec.scopes[scope] : { folded: 0, queue: [] };
|
|
1610
1736
|
if (family !== undefined) {
|
|
1611
1737
|
const queued = own.queue.flatMap((a) => a.items.filter((i) => i.startsWith(family)));
|
|
1612
1738
|
if (queued.length === ann.items.length && ann.items.every((i) => queued.includes(i)))
|
|
1613
|
-
return undefined;
|
|
1739
|
+
return { result: undefined };
|
|
1614
1740
|
for (const a of own.queue)
|
|
1615
1741
|
a.items = a.items.filter((i) => !i.startsWith(family));
|
|
1616
1742
|
own.queue = own.queue.filter((a) => a.items.length > 0);
|
|
1617
1743
|
}
|
|
1618
|
-
if (
|
|
1619
|
-
own.queue.push({ ...
|
|
1744
|
+
if (line.items.length > 0)
|
|
1745
|
+
own.queue.push({ ...line });
|
|
1620
1746
|
if (own.queue.length > MEMORY_ANNOUNCEMENTS_MAX) {
|
|
1621
1747
|
own.folded += own.queue.length - MEMORY_ANNOUNCEMENTS_MAX;
|
|
1622
1748
|
own.queue = own.queue.slice(-MEMORY_ANNOUNCEMENTS_MAX);
|
|
@@ -1625,65 +1751,63 @@ export function enqueueMemoryAnnouncement(controlDir, ann, opts) {
|
|
|
1625
1751
|
rec.scopes[scope] = own;
|
|
1626
1752
|
else
|
|
1627
1753
|
delete rec.scopes[scope];
|
|
1628
|
-
return persistedAnnouncements(rec);
|
|
1754
|
+
return { next: persistedAnnouncements(rec), result: undefined };
|
|
1629
1755
|
});
|
|
1630
1756
|
}
|
|
1631
|
-
export function drainMemoryAnnouncements(
|
|
1757
|
+
export async function drainMemoryAnnouncements(docs, scopes) {
|
|
1632
1758
|
const asked = askedScopes(scopes, "drainMemoryAnnouncements");
|
|
1633
|
-
|
|
1634
|
-
if (!existsSync(file) && !existsSync(`${file}.journal`))
|
|
1759
|
+
if ((await docs.readDoc(ANNOUNCEMENTS_DOC)) === undefined)
|
|
1635
1760
|
return { folded: 0, queue: [], unscopedDiscarded: 0 };
|
|
1636
|
-
let
|
|
1637
|
-
|
|
1638
|
-
|
|
1761
|
+
let otherForm = false;
|
|
1762
|
+
return docs.updateDoc(ANNOUNCEMENTS_DOC, (raw) => {
|
|
1763
|
+
otherForm = !raw || typeof raw !== "object" || raw.v !== ANNOUNCEMENTS_FORM;
|
|
1764
|
+
return coerceAnnouncements(raw);
|
|
1765
|
+
}, (rec) => {
|
|
1639
1766
|
const mine = mergeScopeQueues(rec, asked);
|
|
1640
|
-
drained = { ...mine, unscopedDiscarded: rec.unscopedDiscarded };
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
return undefined;
|
|
1767
|
+
const drained = { ...mine, unscopedDiscarded: rec.unscopedDiscarded ?? 0 };
|
|
1768
|
+
if (mine.queue.length === 0 && mine.folded === 0 && drained.unscopedDiscarded === 0 && !otherForm)
|
|
1769
|
+
return { result: drained };
|
|
1644
1770
|
for (const scope of asked)
|
|
1645
1771
|
delete rec.scopes[scope];
|
|
1646
|
-
rec
|
|
1647
|
-
return persistedAnnouncements(rec);
|
|
1772
|
+
return { next: persistedAnnouncements(withUnscoped(rec, 0)), result: drained };
|
|
1648
1773
|
});
|
|
1649
|
-
return drained;
|
|
1650
1774
|
}
|
|
1651
|
-
export function peekMemoryAnnouncements(
|
|
1775
|
+
export async function peekMemoryAnnouncements(docs, scopes) {
|
|
1652
1776
|
const asked = askedScopes(scopes, "peekMemoryAnnouncements");
|
|
1653
|
-
const rec = coerceAnnouncements(
|
|
1654
|
-
return { ...mergeScopeQueues(rec, asked), unscopedDiscarded: rec.unscopedDiscarded };
|
|
1777
|
+
const rec = coerceAnnouncements(await docs.readDoc(ANNOUNCEMENTS_DOC));
|
|
1778
|
+
return { ...mergeScopeQueues(rec, asked), unscopedDiscarded: rec.unscopedDiscarded ?? 0 };
|
|
1655
1779
|
}
|
|
1656
|
-
export function transferMemoryAnnouncements(
|
|
1657
|
-
const
|
|
1658
|
-
if (
|
|
1780
|
+
export async function transferMemoryAnnouncements(from, to) {
|
|
1781
|
+
const raw = await from.readDoc(ANNOUNCEMENTS_DOC);
|
|
1782
|
+
if (raw === undefined)
|
|
1659
1783
|
return;
|
|
1660
|
-
const taken = coerceAnnouncements(
|
|
1784
|
+
const taken = coerceAnnouncements(raw);
|
|
1785
|
+
const takenUnscoped = taken.unscopedDiscarded ?? 0;
|
|
1661
1786
|
const carried = Object.keys(taken.scopes).flatMap((scope) => taken.scopes[scope].queue);
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
own.
|
|
1672
|
-
rec.scopes[scope] = own;
|
|
1787
|
+
const lineKey = (a) => JSON.stringify(a);
|
|
1788
|
+
await to.updateDoc(ANNOUNCEMENTS_DOC, coerceAnnouncements, (rec) => {
|
|
1789
|
+
for (const scope of Object.keys(taken.scopes)) {
|
|
1790
|
+
const src = taken.scopes[scope];
|
|
1791
|
+
const own = Object.prototype.hasOwnProperty.call(rec.scopes, scope) ? rec.scopes[scope] : { folded: 0, queue: [] };
|
|
1792
|
+
own.queue.push(...[...src.queue].sort((x, y) => x.at - y.at));
|
|
1793
|
+
own.folded += src.folded;
|
|
1794
|
+
if (own.queue.length > MEMORY_ANNOUNCEMENTS_MAX) {
|
|
1795
|
+
own.folded += own.queue.length - MEMORY_ANNOUNCEMENTS_MAX;
|
|
1796
|
+
own.queue = own.queue.slice(-MEMORY_ANNOUNCEMENTS_MAX);
|
|
1673
1797
|
}
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
}
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1798
|
+
if (own.queue.length > 0 || own.folded > 0)
|
|
1799
|
+
rec.scopes[scope] = own;
|
|
1800
|
+
}
|
|
1801
|
+
return { next: persistedAnnouncements(withUnscoped(rec, (rec.unscopedDiscarded ?? 0) + takenUnscoped)), result: undefined };
|
|
1802
|
+
});
|
|
1803
|
+
await from.updateDoc(ANNOUNCEMENTS_DOC, coerceAnnouncements, (rec) => {
|
|
1804
|
+
const moved = new Map();
|
|
1805
|
+
for (const ann of carried)
|
|
1806
|
+
moved.set(lineKey(ann), (moved.get(lineKey(ann)) ?? 0) + 1);
|
|
1683
1807
|
for (const scope of Object.keys(rec.scopes)) {
|
|
1684
1808
|
const own = rec.scopes[scope];
|
|
1685
1809
|
own.queue = own.queue.filter((a) => {
|
|
1686
|
-
const key =
|
|
1810
|
+
const key = lineKey(a);
|
|
1687
1811
|
const n = moved.get(key) ?? 0;
|
|
1688
1812
|
if (n === 0)
|
|
1689
1813
|
return true;
|
|
@@ -1694,8 +1818,7 @@ export function transferMemoryAnnouncements(fromControlDir, toControlDir) {
|
|
|
1694
1818
|
if (own.queue.length === 0 && own.folded === 0)
|
|
1695
1819
|
delete rec.scopes[scope];
|
|
1696
1820
|
}
|
|
1697
|
-
rec
|
|
1698
|
-
return persistedAnnouncements(rec);
|
|
1821
|
+
return { next: persistedAnnouncements(withUnscoped(rec, Math.max(0, (rec.unscopedDiscarded ?? 0) - takenUnscoped))), result: undefined };
|
|
1699
1822
|
});
|
|
1700
1823
|
}
|
|
1701
1824
|
export function scanFuseKey(scope, rel) {
|
|
@@ -1755,6 +1878,16 @@ export function writeIndexRevs(controlDir, revs) {
|
|
|
1755
1878
|
catch {
|
|
1756
1879
|
}
|
|
1757
1880
|
}
|
|
1881
|
+
function clearIndexRevs(controlDir) {
|
|
1882
|
+
let present = true;
|
|
1883
|
+
try {
|
|
1884
|
+
present = lstatSync(join(controlDir, INDEX_REVS_FILE), { throwIfNoEntry: false }) !== undefined;
|
|
1885
|
+
}
|
|
1886
|
+
catch {
|
|
1887
|
+
}
|
|
1888
|
+
if (present)
|
|
1889
|
+
writeIndexRevs(controlDir, {});
|
|
1890
|
+
}
|
|
1758
1891
|
export function scanFuseCount(controlDir, key) {
|
|
1759
1892
|
return coerceFuse(readSidecarJson(controlDir, SCAN_FUSE_FILE))[key] ?? 0;
|
|
1760
1893
|
}
|
|
@@ -2087,6 +2220,23 @@ export function clearScanFuse(controlDir, keys) {
|
|
|
2087
2220
|
}
|
|
2088
2221
|
export const LINEAGE_FILE = "lineage.json";
|
|
2089
2222
|
export const CHALLENGES_FILE = "challenges.json";
|
|
2223
|
+
export const DELEGATION_SETTLEMENT_FILE = "delegation-settlement.json";
|
|
2224
|
+
export const SESSION_ACCOUNTS_FILE = "session-accounts.json";
|
|
2225
|
+
const FILE_GOVERNANCE_DOCS = Object.freeze({
|
|
2226
|
+
lineage: Object.freeze({ file: LINEAGE_FILE, what: "memory lineage ledger" }),
|
|
2227
|
+
challenges: Object.freeze({ file: CHALLENGES_FILE, what: "memory challenge ledger" }),
|
|
2228
|
+
settlements: Object.freeze({ file: DELEGATION_SETTLEMENT_FILE, what: "memory delegation-settlement ledger" }),
|
|
2229
|
+
"session-accounts": Object.freeze({ file: SESSION_ACCOUNTS_FILE, what: "memory session-account ledger" }),
|
|
2230
|
+
announcements: Object.freeze({ file: ANNOUNCEMENTS_FILE, what: "memory announcement queue" }),
|
|
2231
|
+
});
|
|
2232
|
+
export function fileGovernanceDocOf(name) {
|
|
2233
|
+
if (typeof name !== "string" || !Object.prototype.hasOwnProperty.call(FILE_GOVERNANCE_DOCS, name)) {
|
|
2234
|
+
const e = new Error(`memory governance store: ${JSON.stringify(String(name))} is not a governance document (one of ${MEMORY_GOVERNANCE_DOC_NAMES.join(", ")})`);
|
|
2235
|
+
e.code = "memory.governance_doc_unknown";
|
|
2236
|
+
throw e;
|
|
2237
|
+
}
|
|
2238
|
+
return FILE_GOVERNANCE_DOCS[name];
|
|
2239
|
+
}
|
|
2090
2240
|
export const LINEAGE_AUDIT_FILE = "lineage-audit.json";
|
|
2091
2241
|
export const LINEAGE_AUDIT_MAX_ROWS = 2048;
|
|
2092
2242
|
export const CHALLENGED_HISTORY_FILE = "usage-challenged-history.json";
|
|
@@ -2241,22 +2391,22 @@ function coerceLineage(raw) {
|
|
|
2241
2391
|
pending[txnId] = t;
|
|
2242
2392
|
return { version: 1, committed, pending };
|
|
2243
2393
|
}
|
|
2244
|
-
export function stageLineagePending(
|
|
2245
|
-
|
|
2394
|
+
export async function stageLineagePending(docs, txnId, sessionId, rows, now, incarnation) {
|
|
2395
|
+
await docs.updateDoc("lineage", coerceLineage, (rec) => {
|
|
2246
2396
|
rec.pending[txnId] = { sessionId, at: now(), rows: [...rows], ...(incarnation !== undefined ? { incarnation } : {}) };
|
|
2247
2397
|
return { next: rec, result: undefined };
|
|
2248
2398
|
});
|
|
2249
2399
|
}
|
|
2250
|
-
export function discardLineagePending(
|
|
2251
|
-
|
|
2400
|
+
export async function discardLineagePending(docs, txnId) {
|
|
2401
|
+
await docs.updateDoc("lineage", coerceLineage, (rec) => {
|
|
2252
2402
|
if (rec.pending[txnId] === undefined)
|
|
2253
2403
|
return { result: undefined };
|
|
2254
2404
|
delete rec.pending[txnId];
|
|
2255
2405
|
return { next: rec, result: undefined };
|
|
2256
2406
|
});
|
|
2257
2407
|
}
|
|
2258
|
-
export function recordLineageCredential(
|
|
2259
|
-
|
|
2408
|
+
export async function recordLineageCredential(docs, txnId, appliedIds, now) {
|
|
2409
|
+
await docs.updateDoc("lineage", coerceLineage, (rec) => {
|
|
2260
2410
|
const txn = rec.pending[txnId];
|
|
2261
2411
|
if (txn === undefined)
|
|
2262
2412
|
throw new ControlPlaneCorruptError(`memory lineage ledger: credential for unknown pending transaction ${txnId}`);
|
|
@@ -2264,8 +2414,8 @@ export function recordLineageCredential(controlDir, txnId, appliedIds, now) {
|
|
|
2264
2414
|
return { next: rec, result: undefined };
|
|
2265
2415
|
});
|
|
2266
2416
|
}
|
|
2267
|
-
export function promoteLineagePending(
|
|
2268
|
-
return
|
|
2417
|
+
export async function promoteLineagePending(docs, txnId) {
|
|
2418
|
+
return docs.updateDoc("lineage", coerceLineage, (rec) => {
|
|
2269
2419
|
const txn = rec.pending[txnId];
|
|
2270
2420
|
if (txn === undefined)
|
|
2271
2421
|
return { result: [] };
|
|
@@ -2286,8 +2436,8 @@ export function promoteLineagePending(controlDir, txnId) {
|
|
|
2286
2436
|
return { next: rec, result: promoted };
|
|
2287
2437
|
});
|
|
2288
2438
|
}
|
|
2289
|
-
export function adjudicateLineagePending(
|
|
2290
|
-
return
|
|
2439
|
+
export async function adjudicateLineagePending(docs, txnId, action, now) {
|
|
2440
|
+
return docs.updateDoc("lineage", coerceLineage, (rec) => {
|
|
2291
2441
|
const txn = rec.pending[txnId];
|
|
2292
2442
|
if (txn === undefined)
|
|
2293
2443
|
return { result: [] };
|
|
@@ -2312,8 +2462,8 @@ export function adjudicateLineagePending(controlDir, txnId, action, now) {
|
|
|
2312
2462
|
return { next: rec, result: promoted };
|
|
2313
2463
|
});
|
|
2314
2464
|
}
|
|
2315
|
-
export function reconcileLineage(
|
|
2316
|
-
return
|
|
2465
|
+
export async function reconcileLineage(docs) {
|
|
2466
|
+
return docs.updateDoc("lineage", coerceLineage, (rec) => {
|
|
2317
2467
|
const promoted = [];
|
|
2318
2468
|
const undecidable = [];
|
|
2319
2469
|
let changed = false;
|
|
@@ -2348,9 +2498,9 @@ export function importLatchTxnId(bundleHash) {
|
|
|
2348
2498
|
function importReceiptPath(controlDir, bundleHash) {
|
|
2349
2499
|
return join(controlDir, IMPORT_RECEIPTS_DIR, `${bundleHash}.json`);
|
|
2350
2500
|
}
|
|
2351
|
-
export function stageImportLatch(
|
|
2501
|
+
export async function stageImportLatch(docs, bundleHash, rows, now) {
|
|
2352
2502
|
const txnId = importLatchTxnId(bundleHash);
|
|
2353
|
-
|
|
2503
|
+
await docs.updateDoc("lineage", coerceLineage, (rec) => {
|
|
2354
2504
|
const existing = rec.pending[txnId];
|
|
2355
2505
|
if (existing !== undefined && !isLatchOnlyTxn(existing)) {
|
|
2356
2506
|
throw new ControlPlaneCorruptError(`memory lineage ledger: pending transaction ${txnId} exists and is NOT an import latch — refusing to overwrite a real staged transaction`);
|
|
@@ -2365,7 +2515,7 @@ export function stageImportLatch(controlDir, bundleHash, rows, now) {
|
|
|
2365
2515
|
});
|
|
2366
2516
|
return txnId;
|
|
2367
2517
|
}
|
|
2368
|
-
export function releaseImportLatch(controlDir, txnId) {
|
|
2518
|
+
export async function releaseImportLatch(docs, controlDir, txnId) {
|
|
2369
2519
|
const prefix = "import:";
|
|
2370
2520
|
if (!txnId.startsWith(prefix) || !/^[0-9a-f]{64}$/.test(txnId.slice(prefix.length))) {
|
|
2371
2521
|
throw new ControlPlaneCorruptError(`releaseImportLatch: ${JSON.stringify(txnId)} is not an import-latch transaction id (import:<64-hex bundleHash>)`);
|
|
@@ -2385,7 +2535,7 @@ export function releaseImportLatch(controlDir, txnId) {
|
|
|
2385
2535
|
if (!receiptPresent) {
|
|
2386
2536
|
throw new ControlPlaneCorruptError(`releaseImportLatch: no completion receipt at ${receipt} — the import's governance legs are not proven landed, the latch stays; re-run the same bundle import to converge`);
|
|
2387
2537
|
}
|
|
2388
|
-
|
|
2538
|
+
await docs.updateDoc("lineage", coerceLineage, (rec) => {
|
|
2389
2539
|
const txn = rec.pending[txnId];
|
|
2390
2540
|
if (txn === undefined)
|
|
2391
2541
|
return { result: undefined };
|
|
@@ -2396,16 +2546,16 @@ export function releaseImportLatch(controlDir, txnId) {
|
|
|
2396
2546
|
return { next: rec, result: undefined };
|
|
2397
2547
|
});
|
|
2398
2548
|
}
|
|
2399
|
-
export function lineageLatchedIds(
|
|
2400
|
-
const rec = coerceLineage(
|
|
2549
|
+
export async function lineageLatchedIds(docs) {
|
|
2550
|
+
const rec = coerceLineage(await docs.readDoc("lineage"));
|
|
2401
2551
|
const out = new Set();
|
|
2402
2552
|
for (const txn of Object.values(rec.pending))
|
|
2403
2553
|
for (const row of txn.rows)
|
|
2404
2554
|
out.add(row.entryId);
|
|
2405
2555
|
return out;
|
|
2406
2556
|
}
|
|
2407
|
-
export function lineageContributionsOfSession(
|
|
2408
|
-
const rec = coerceLineage(
|
|
2557
|
+
export async function lineageContributionsOfSession(docs, sessionId, asking) {
|
|
2558
|
+
const rec = coerceLineage(await docs.readDoc("lineage"));
|
|
2409
2559
|
const out = [];
|
|
2410
2560
|
for (const [entryId, sessions] of Object.entries(rec.committed)) {
|
|
2411
2561
|
const c = Object.prototype.hasOwnProperty.call(sessions, sessionId) ? sessions[sessionId] : undefined;
|
|
@@ -2415,8 +2565,8 @@ export function lineageContributionsOfSession(controlDir, sessionId, asking) {
|
|
|
2415
2565
|
}
|
|
2416
2566
|
return out;
|
|
2417
2567
|
}
|
|
2418
|
-
export function lineageAccountOfEntry(
|
|
2419
|
-
const rec = coerceLineage(
|
|
2568
|
+
export async function lineageAccountOfEntry(docs, entryId) {
|
|
2569
|
+
const rec = coerceLineage(await docs.readDoc("lineage"));
|
|
2420
2570
|
const sessions = Object.prototype.hasOwnProperty.call(rec.committed, entryId) ? rec.committed[entryId] : undefined;
|
|
2421
2571
|
const contributors = sessions === undefined ? [] : Object.entries(sessions).map(([sessionId, c]) => ({ sessionId, lastRev: c.lastRev, lastAt: c.lastAt }));
|
|
2422
2572
|
let pendingLatched = false;
|
|
@@ -2428,10 +2578,10 @@ export function lineageAccountOfEntry(controlDir, entryId) {
|
|
|
2428
2578
|
}
|
|
2429
2579
|
return { contributors, pendingLatched };
|
|
2430
2580
|
}
|
|
2431
|
-
export function clearLineageForEntries(
|
|
2581
|
+
export async function clearLineageForEntries(docs, entryIds) {
|
|
2432
2582
|
if (entryIds.length === 0)
|
|
2433
2583
|
return;
|
|
2434
|
-
|
|
2584
|
+
await docs.updateDoc("lineage", coerceLineage, (rec) => {
|
|
2435
2585
|
let changed = false;
|
|
2436
2586
|
for (const id of entryIds) {
|
|
2437
2587
|
if (rec.committed[id] !== undefined) {
|
|
@@ -2442,10 +2592,10 @@ export function clearLineageForEntries(controlDir, entryIds) {
|
|
|
2442
2592
|
return { ...(changed ? { next: rec } : {}), result: undefined };
|
|
2443
2593
|
});
|
|
2444
2594
|
}
|
|
2445
|
-
export function captureAndClearLineageForEntries(
|
|
2595
|
+
export async function captureAndClearLineageForEntries(docs, entryIds) {
|
|
2446
2596
|
if (entryIds.length === 0)
|
|
2447
2597
|
return new Map();
|
|
2448
|
-
return
|
|
2598
|
+
return docs.updateDoc("lineage", coerceLineage, (rec) => {
|
|
2449
2599
|
const want = new Set(entryIds);
|
|
2450
2600
|
const captured = new Map();
|
|
2451
2601
|
const add = (id, sessionId) => {
|
|
@@ -2481,10 +2631,10 @@ export function captureAndClearLineageForEntries(controlDir, entryIds) {
|
|
|
2481
2631
|
return { ...(changed ? { next: rec } : {}), result };
|
|
2482
2632
|
});
|
|
2483
2633
|
}
|
|
2484
|
-
export function importLineageCommitted(
|
|
2634
|
+
export async function importLineageCommitted(docs, rows) {
|
|
2485
2635
|
if (rows.length === 0)
|
|
2486
2636
|
return [];
|
|
2487
|
-
return
|
|
2637
|
+
return docs.updateDoc("lineage", coerceLineage, (rec) => {
|
|
2488
2638
|
const divergence = [];
|
|
2489
2639
|
let changed = false;
|
|
2490
2640
|
for (const row of rows) {
|
|
@@ -2508,8 +2658,8 @@ export function importLineageCommitted(controlDir, rows) {
|
|
|
2508
2658
|
return { ...(changed ? { next: rec } : {}), result: divergence };
|
|
2509
2659
|
});
|
|
2510
2660
|
}
|
|
2511
|
-
export function readLineageRecord(
|
|
2512
|
-
const rec = coerceLineage(
|
|
2661
|
+
export async function readLineageRecord(docs) {
|
|
2662
|
+
const rec = coerceLineage(await docs.readDoc("lineage"));
|
|
2513
2663
|
return { committed: rec.committed, pending: rec.pending };
|
|
2514
2664
|
}
|
|
2515
2665
|
export function appendLineageAudit(controlDir, rows) {
|
|
@@ -2547,10 +2697,10 @@ function coerceChallenges(raw) {
|
|
|
2547
2697
|
return raw;
|
|
2548
2698
|
}
|
|
2549
2699
|
export const CHALLENGE_LEDGER_MAX_EVENTS = 10_000;
|
|
2550
|
-
export function appendChallengeEvents(
|
|
2700
|
+
export async function appendChallengeEvents(docs, events, now) {
|
|
2551
2701
|
if (events.length === 0)
|
|
2552
2702
|
return { assignments: [], eventCount: undefined };
|
|
2553
|
-
return
|
|
2703
|
+
return docs.updateDoc("challenges", coerceChallenges, (rec) => {
|
|
2554
2704
|
const byEventId = new Map(rec.events.map((e) => [e.eventId, e]));
|
|
2555
2705
|
const maxGen = new Map();
|
|
2556
2706
|
for (const e of rec.events) {
|
|
@@ -2586,8 +2736,8 @@ export function appendChallengeEvents(controlDir, events, now) {
|
|
|
2586
2736
|
return { ...(changed ? { next: rec } : {}), result: { assignments: out, eventCount: rec.events.length } };
|
|
2587
2737
|
});
|
|
2588
2738
|
}
|
|
2589
|
-
export function resolveChallengeEvent(
|
|
2590
|
-
return
|
|
2739
|
+
export async function resolveChallengeEvent(docs, entryId, generation, reason, now, requestId) {
|
|
2740
|
+
return docs.updateDoc("challenges", coerceChallenges, (rec) => {
|
|
2591
2741
|
const opened = rec.events.some((e) => e.kind === "challenge" && e.entryId === entryId && e.generation === generation);
|
|
2592
2742
|
if (!opened)
|
|
2593
2743
|
return { result: { resolved: false, eventCount: undefined } };
|
|
@@ -2605,8 +2755,8 @@ export function resolveChallengeEvent(controlDir, entryId, generation, reason, n
|
|
|
2605
2755
|
return { next: rec, result: { resolved: true, eventCount: rec.events.length } };
|
|
2606
2756
|
});
|
|
2607
2757
|
}
|
|
2608
|
-
export function challengedEntryIds(
|
|
2609
|
-
const rec = coerceChallenges(
|
|
2758
|
+
export async function challengedEntryIds(docs) {
|
|
2759
|
+
const rec = coerceChallenges(await docs.readDoc("challenges"));
|
|
2610
2760
|
const resolved = new Set();
|
|
2611
2761
|
for (const e of rec.events)
|
|
2612
2762
|
if (e.kind === "resolve")
|
|
@@ -2621,8 +2771,8 @@ export function challengedEntryIds(controlDir) {
|
|
|
2621
2771
|
}
|
|
2622
2772
|
return out;
|
|
2623
2773
|
}
|
|
2624
|
-
export function readChallengeEvents(
|
|
2625
|
-
return coerceChallenges(
|
|
2774
|
+
export async function readChallengeEvents(docs) {
|
|
2775
|
+
return coerceChallenges(await docs.readDoc("challenges")).events;
|
|
2626
2776
|
}
|
|
2627
2777
|
export function isStrictControlPlaneLedgerCorrupt(controlDir, ledger) {
|
|
2628
2778
|
const fileName = ledger === "lineage" ? LINEAGE_FILE : CHALLENGES_FILE;
|
|
@@ -2849,7 +2999,7 @@ export function writeAllSync(fd, data) {
|
|
|
2849
2999
|
}
|
|
2850
3000
|
export function atomicWriteFileSync(path, data) {
|
|
2851
3001
|
ensureDirExists(dirname(path));
|
|
2852
|
-
const tmp = join(dirname(path),
|
|
3002
|
+
const tmp = join(dirname(path), `${ATOMIC_WRITE_TMP_PREFIX}${process.pid}-${Math.random().toString(36).slice(2, 10)}`);
|
|
2853
3003
|
const fd = openSync(tmp, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0o600);
|
|
2854
3004
|
try {
|
|
2855
3005
|
writeAllSync(fd, data);
|