@tangle-network/agent-knowledge 22.0.7 → 22.0.9
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 +8 -0
- package/dist/cli.js +1 -1
- package/dist/index.d.ts +7 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -4
- package/dist/index.js.map +1 -1
- package/dist/{inspect-DOQ_h1p6.js → inspect-CTpCsUB2.js} +311 -95
- package/dist/inspect-CTpCsUB2.js.map +1 -0
- package/docs/architecture.md +9 -2
- package/package.json +1 -1
- package/dist/inspect-DOQ_h1p6.js.map +0 -1
|
@@ -5,10 +5,10 @@ import { contentHash } from "@tangle-network/agent-eval";
|
|
|
5
5
|
import { z } from "zod";
|
|
6
6
|
import { createHash, randomUUID } from "node:crypto";
|
|
7
7
|
import { closeSync, constants, fstatSync, lstatSync, openSync, readFileSync } from "node:fs";
|
|
8
|
-
import { lstat, mkdir, mkdtemp, open, readFile, readdir, realpath, rename, rm } from "node:fs/promises";
|
|
8
|
+
import { lstat, mkdir, mkdtemp, open, readFile, readdir, readlink, realpath, rename, rm, rmdir } from "node:fs/promises";
|
|
9
9
|
import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
10
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
11
10
|
import { hostname } from "node:os";
|
|
11
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
12
12
|
//#region src/adapters.ts
|
|
13
13
|
const textSourceAdapter = {
|
|
14
14
|
id: "text",
|
|
@@ -401,6 +401,253 @@ function normalizePagesDirectory(value = DEFAULT_PAGES_DIRECTORY) {
|
|
|
401
401
|
return normalized;
|
|
402
402
|
}
|
|
403
403
|
//#endregion
|
|
404
|
+
//#region src/lock-holder.ts
|
|
405
|
+
const DEFAULT_STALE_MS = 9e5;
|
|
406
|
+
/** The lease a caller's `staleMs` resolves to: the default, floored at 5 s. */
|
|
407
|
+
function staleWindow(staleMs) {
|
|
408
|
+
return Math.max(5e3, staleMs ?? 9e5);
|
|
409
|
+
}
|
|
410
|
+
/** The store's cross-process lock, inside the cache directory. */
|
|
411
|
+
const STORE_LOCK = {
|
|
412
|
+
directory: "mutation.lock.durable",
|
|
413
|
+
owner: "mutation.lock.owner.json",
|
|
414
|
+
guard: "mutation.lock.takeover",
|
|
415
|
+
noun: "knowledge store lock"
|
|
416
|
+
};
|
|
417
|
+
/** One knowledge-improvement run's lease, inside its run directory. */
|
|
418
|
+
const RUN_LEASE = {
|
|
419
|
+
directory: "run.lock.durable",
|
|
420
|
+
owner: "run.lock.owner.json",
|
|
421
|
+
guard: "run.lock.takeover",
|
|
422
|
+
noun: "knowledge improvement run lease"
|
|
423
|
+
};
|
|
424
|
+
/** A broken lock directory is renamed to `<lock>.dead-<uuid>` beside it, then removed. */
|
|
425
|
+
const TOMBSTONE_INFIX = ".dead-";
|
|
426
|
+
/** How far before `staleMs` a takeover yields to proper-lockfile's own stale removal. */
|
|
427
|
+
const STALE_MARGIN_MS = 6e4;
|
|
428
|
+
function describeHolder(holder, now = Date.now()) {
|
|
429
|
+
const heldS = Math.max(0, Math.round((now - holder.since) / 1e3));
|
|
430
|
+
return `${holder.label} (pid ${holder.pid} on ${holder.host}, holding since ${new Date(holder.since).toISOString()}, ${heldS} s)`;
|
|
431
|
+
}
|
|
432
|
+
let lockfileModule;
|
|
433
|
+
function loadLockfile() {
|
|
434
|
+
lockfileModule ??= import("proper-lockfile");
|
|
435
|
+
return lockfileModule;
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* The OS start token of a live pid, the way agent-runtime's `readProcessStart` reads it: the same
|
|
439
|
+
* for a process's whole life and different for every later holder of its pid. `undefined` when
|
|
440
|
+
* the host cannot report one. `node:child_process` loads lazily because the package root, which
|
|
441
|
+
* Cloudflare consumers bundle, imports this module.
|
|
442
|
+
*/
|
|
443
|
+
async function readProcessStart(pid) {
|
|
444
|
+
if (!Number.isSafeInteger(pid) || pid <= 0 || process.platform === "win32") return void 0;
|
|
445
|
+
if (process.platform === "linux") {
|
|
446
|
+
let stat;
|
|
447
|
+
try {
|
|
448
|
+
stat = await readFile(`/proc/${pid}/stat`, "utf8");
|
|
449
|
+
} catch {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
const startTime = stat.slice(stat.lastIndexOf(")") + 1).trim().split(/\s+/u)[19];
|
|
453
|
+
return startTime !== void 0 && /^\d+$/u.test(startTime) ? startTime : void 0;
|
|
454
|
+
}
|
|
455
|
+
try {
|
|
456
|
+
const { execFile } = await import("node:child_process");
|
|
457
|
+
return (await new Promise((resolve, reject) => execFile("ps", [
|
|
458
|
+
"-o",
|
|
459
|
+
"lstart=",
|
|
460
|
+
"-p",
|
|
461
|
+
String(pid)
|
|
462
|
+
], (error, out) => error ? reject(error) : resolve(out)))).trim() || void 0;
|
|
463
|
+
} catch {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
let ownIdentity;
|
|
468
|
+
function readOwnIdentity() {
|
|
469
|
+
ownIdentity ??= (async () => {
|
|
470
|
+
const [processStart, bootId, pidNamespace] = await Promise.all([
|
|
471
|
+
readProcessStart(process.pid),
|
|
472
|
+
readFile("/proc/sys/kernel/random/boot_id", "utf8").then((text) => text.trim() || void 0, () => void 0),
|
|
473
|
+
readlink("/proc/self/ns/pid").catch(() => void 0)
|
|
474
|
+
]);
|
|
475
|
+
return {
|
|
476
|
+
...processStart ? { processStart } : {},
|
|
477
|
+
...bootId ? { bootId } : {},
|
|
478
|
+
...pidNamespace ? { pidNamespace } : {}
|
|
479
|
+
};
|
|
480
|
+
})();
|
|
481
|
+
return ownIdentity;
|
|
482
|
+
}
|
|
483
|
+
async function readLockDirectoryIdentity(cacheDir, held) {
|
|
484
|
+
try {
|
|
485
|
+
const entry = await lstat(join(cacheDir, held.directory), { bigint: true });
|
|
486
|
+
if (!entry.isDirectory()) return void 0;
|
|
487
|
+
return {
|
|
488
|
+
dev: entry.dev.toString(),
|
|
489
|
+
ino: entry.ino.toString(),
|
|
490
|
+
birthtimeNs: entry.birthtimeNs.toString()
|
|
491
|
+
};
|
|
492
|
+
} catch {
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Record who holds the file lock, and which lock directory, inside the directory that holds it.
|
|
498
|
+
* Call it right after acquiring. Exported for the confinement tests; src/index.ts does not
|
|
499
|
+
* re-export it.
|
|
500
|
+
*/
|
|
501
|
+
async function recordLockOwner(cacheDir, label, held = STORE_LOCK) {
|
|
502
|
+
try {
|
|
503
|
+
const [identity, lock] = await Promise.all([readOwnIdentity(), readLockDirectoryIdentity(cacheDir, held)]);
|
|
504
|
+
const owner = {
|
|
505
|
+
label,
|
|
506
|
+
pid: process.pid,
|
|
507
|
+
host: hostname(),
|
|
508
|
+
since: Date.now(),
|
|
509
|
+
...identity,
|
|
510
|
+
...lock ? { lock } : {}
|
|
511
|
+
};
|
|
512
|
+
await writeJsonDurableWithinRoot(cacheDir, held.owner, owner);
|
|
513
|
+
} catch {}
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* `cacheDir` is withSafeDirectory's anchored path, so only the owner file itself can be swapped.
|
|
517
|
+
* Anyone who can write the cache directory can plant a FIFO there, so the open adds O_NONBLOCK
|
|
518
|
+
* and fstat refuses it. The shared readRegularFileNoFollow stays blocking: the event store and
|
|
519
|
+
* file transactions read through it, and their descriptor use must not change.
|
|
520
|
+
*
|
|
521
|
+
* The old shape `{ label, pid, host, since }` still parses. A newer field that is missing or
|
|
522
|
+
* malformed is dropped, which leaves the holder named but never provably dead.
|
|
523
|
+
*/
|
|
524
|
+
async function readLockOwner(cacheDir, held = STORE_LOCK) {
|
|
525
|
+
let handle;
|
|
526
|
+
try {
|
|
527
|
+
handle = await open(join(cacheDir, held.owner), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
528
|
+
if (!(await handle.stat()).isFile()) return void 0;
|
|
529
|
+
const value = JSON.parse(await handle.readFile("utf8"));
|
|
530
|
+
if (typeof value?.label !== "string" || !Number.isSafeInteger(value.pid) || typeof value.host !== "string" || typeof value.since !== "number" || Number.isNaN(new Date(value.since).getTime())) return;
|
|
531
|
+
const text = (field) => typeof field === "string" && field.length > 0 ? field : void 0;
|
|
532
|
+
const lock = value.lock;
|
|
533
|
+
const decimal = (field) => typeof field === "string" && /^\d+$/u.test(field);
|
|
534
|
+
const processStart = text(value.processStart);
|
|
535
|
+
const bootId = text(value.bootId);
|
|
536
|
+
const pidNamespace = text(value.pidNamespace);
|
|
537
|
+
return {
|
|
538
|
+
label: value.label,
|
|
539
|
+
pid: value.pid,
|
|
540
|
+
host: value.host,
|
|
541
|
+
since: value.since,
|
|
542
|
+
...processStart ? { processStart } : {},
|
|
543
|
+
...bootId ? { bootId } : {},
|
|
544
|
+
...pidNamespace ? { pidNamespace } : {},
|
|
545
|
+
...lock && decimal(lock.dev) && decimal(lock.ino) && decimal(lock.birthtimeNs) ? { lock: {
|
|
546
|
+
dev: lock.dev,
|
|
547
|
+
ino: lock.ino,
|
|
548
|
+
birthtimeNs: lock.birthtimeNs
|
|
549
|
+
} } : {}
|
|
550
|
+
};
|
|
551
|
+
} catch {
|
|
552
|
+
return;
|
|
553
|
+
} finally {
|
|
554
|
+
await handle?.close().catch(() => void 0);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
/** Only ESRCH proves a pid gone; EPERM means it exists under another user. */
|
|
558
|
+
function pidIsGone(pid) {
|
|
559
|
+
try {
|
|
560
|
+
process.kill(pid, 0);
|
|
561
|
+
return false;
|
|
562
|
+
} catch (error) {
|
|
563
|
+
return error.code === "ESRCH";
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* The holder of the current lock, when its record proves it dead; otherwise `undefined`,
|
|
568
|
+
* and the caller keeps the `staleMs` lease. Proof needs a record with every identity field,
|
|
569
|
+
* written for this very lock directory (dev, ino and a nonzero birthtime), on this host and pid
|
|
570
|
+
* namespace, and then a rebooted host, a gone pid, or a pid whose start token changed. Anything
|
|
571
|
+
* else is unknown, so a live holder's lease is never shortened.
|
|
572
|
+
*/
|
|
573
|
+
async function holderIsProvablyDead(cacheDir, held = STORE_LOCK) {
|
|
574
|
+
const holder = await readLockOwner(cacheDir, held);
|
|
575
|
+
if (!holder?.processStart || !holder.lock || holder.pid <= 0) return void 0;
|
|
576
|
+
const lock = await readLockDirectoryIdentity(cacheDir, held);
|
|
577
|
+
if (!lock || lock.birthtimeNs === "0" || lock.dev !== holder.lock.dev || lock.ino !== holder.lock.ino || lock.birthtimeNs !== holder.lock.birthtimeNs || holder.host !== hostname()) return;
|
|
578
|
+
const own = await readOwnIdentity();
|
|
579
|
+
if (holder.pidNamespace !== own.pidNamespace) return void 0;
|
|
580
|
+
if (holder.bootId === void 0 !== (own.bootId === void 0)) return void 0;
|
|
581
|
+
if (holder.bootId !== own.bootId) return {
|
|
582
|
+
holder,
|
|
583
|
+
lock,
|
|
584
|
+
reason: "host rebooted"
|
|
585
|
+
};
|
|
586
|
+
if (pidIsGone(holder.pid)) return {
|
|
587
|
+
holder,
|
|
588
|
+
lock,
|
|
589
|
+
reason: "pid gone"
|
|
590
|
+
};
|
|
591
|
+
const start = await readProcessStart(holder.pid);
|
|
592
|
+
if (start !== void 0 && start !== holder.processStart) return {
|
|
593
|
+
holder,
|
|
594
|
+
lock,
|
|
595
|
+
reason: "pid reused"
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Break the lock of a provably dead holder, and report whether this call broke it. The
|
|
600
|
+
* guard is proper-lockfile itself: a taker that finds it held breaks nothing and returns to its
|
|
601
|
+
* wait, because another taker is already breaking the lock. Its target is the guard path, not the
|
|
602
|
+
* locked target, because proper-lockfile keys its held locks by target and a second lock on that
|
|
603
|
+
* target would replace this process's entry for it and break that lock's heartbeat and release.
|
|
604
|
+
*
|
|
605
|
+
* proper-lockfile's own stale removal does not take the guard. Once the lock directory's mtime is
|
|
606
|
+
* near `staleMs`, another process may remove it as stale and create its own between our re-check
|
|
607
|
+
* and our rename, so that lock is left to the stale path, which frees it within the margin.
|
|
608
|
+
*/
|
|
609
|
+
async function takeOverDeadHolder(cacheDir, staleMs, held = STORE_LOCK) {
|
|
610
|
+
if (!await holderIsProvablyDead(cacheDir, held)) return false;
|
|
611
|
+
const { lock } = await loadLockfile();
|
|
612
|
+
const guard = join(cacheDir, held.guard);
|
|
613
|
+
let release;
|
|
614
|
+
try {
|
|
615
|
+
release = await lock(guard, {
|
|
616
|
+
lockfilePath: guard,
|
|
617
|
+
realpath: false,
|
|
618
|
+
stale: 3e4,
|
|
619
|
+
retries: 0,
|
|
620
|
+
onCompromised: () => void 0
|
|
621
|
+
});
|
|
622
|
+
} catch (error) {
|
|
623
|
+
if (error.code === "ELOCKED") return false;
|
|
624
|
+
throw error;
|
|
625
|
+
}
|
|
626
|
+
try {
|
|
627
|
+
const dead = await holderIsProvablyDead(cacheDir, held);
|
|
628
|
+
if (!dead) return false;
|
|
629
|
+
const lockDirectory = join(cacheDir, held.directory);
|
|
630
|
+
const { mtimeMs } = await lstat(lockDirectory);
|
|
631
|
+
const stale = staleWindow(staleMs);
|
|
632
|
+
if (Date.now() - mtimeMs >= stale - Math.min(STALE_MARGIN_MS, stale / 3)) return false;
|
|
633
|
+
const tombstone = `${lockDirectory}${TOMBSTONE_INFIX}${randomUUID()}`;
|
|
634
|
+
try {
|
|
635
|
+
await rename(lockDirectory, tombstone);
|
|
636
|
+
} catch (error) {
|
|
637
|
+
if (error.code === "ENOENT") return false;
|
|
638
|
+
throw error;
|
|
639
|
+
}
|
|
640
|
+
for (const entry of await readdir(cacheDir).catch(() => [])) if (entry.startsWith(`${held.directory}${TOMBSTONE_INFIX}`)) await rmdir(join(cacheDir, entry)).catch(() => void 0);
|
|
641
|
+
process.emitWarning(`took over the ${held.noun} of a dead holder, ${describeHolder(dead.holder)}: ${dead.reason}`, {
|
|
642
|
+
type: "AgentKnowledgeRecovery",
|
|
643
|
+
code: "AGENT_KNOWLEDGE_LOCK_TAKEOVER"
|
|
644
|
+
});
|
|
645
|
+
return true;
|
|
646
|
+
} finally {
|
|
647
|
+
await release().catch(() => void 0);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
//#endregion
|
|
404
651
|
//#region src/bounded-file-io.ts
|
|
405
652
|
/** Keep filesystem descriptors bounded and drain a failed batch before its owner releases handles. */
|
|
406
653
|
async function mapFileIo(values, operation) {
|
|
@@ -884,7 +1131,6 @@ function fileMatches(file, hash, mode) {
|
|
|
884
1131
|
}
|
|
885
1132
|
//#endregion
|
|
886
1133
|
//#region src/mutation-lock.ts
|
|
887
|
-
const DEFAULT_STALE_MS = 9e5;
|
|
888
1134
|
const DEFAULT_READ_WAIT_MS = 25;
|
|
889
1135
|
const MAX_POLL_MS = 250;
|
|
890
1136
|
function sleep(ms) {
|
|
@@ -907,18 +1153,18 @@ const WRITER_WAIT_MS = DEFAULT_STALE_MS;
|
|
|
907
1153
|
* A read changes nothing, so failing one loses no work, which is why it waits less than a writer.
|
|
908
1154
|
*/
|
|
909
1155
|
const READER_WAIT_MS = 12e4;
|
|
1156
|
+
/** Purpose prefix of a proposal write; its purpose hashes the exact proposal. */
|
|
1157
|
+
const KNOWLEDGE_PROPOSAL_PURPOSE_PREFIX = "knowledge-proposal:";
|
|
910
1158
|
/**
|
|
911
|
-
*
|
|
912
|
-
*
|
|
913
|
-
*
|
|
914
|
-
*
|
|
1159
|
+
* A pending transaction with no recovery owner, found by a caller that holds the exclusive lock
|
|
1160
|
+
* and is not resuming that purpose. The transaction belongs to the lock hold that opened it, and
|
|
1161
|
+
* holding the lock now proves that hold ended: released on an error or exit, expired, or broken
|
|
1162
|
+
* because its holder was provably dead. Its caller never saw success, so rolling it back leaves
|
|
1163
|
+
* the store as that caller last saw it, and an identical retry still writes cleanly.
|
|
915
1164
|
*/
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
return pending.purpose.startsWith("knowledge-proposal:") && !pending.recoveryOwner;
|
|
1165
|
+
function isAbandonedTransaction(pending, resumingPurpose) {
|
|
1166
|
+
return !pending.recoveryOwner && pending.purpose !== resumingPurpose;
|
|
919
1167
|
}
|
|
920
|
-
/** Diagnostic record of the current cross-process holder, read only to name it in a timeout. */
|
|
921
|
-
const LOCK_OWNER_FILE = "mutation.lock.owner.json";
|
|
922
1168
|
const holderLabels = new AsyncLocalStorage();
|
|
923
1169
|
/**
|
|
924
1170
|
* Name the store mutations `body` performs, so a writer or reader that times out behind them can
|
|
@@ -927,18 +1173,9 @@ const holderLabels = new AsyncLocalStorage();
|
|
|
927
1173
|
function withKnowledgeMutationLabel(label, body) {
|
|
928
1174
|
return holderLabels.run(label, body);
|
|
929
1175
|
}
|
|
930
|
-
function describeHolder(holder, now = Date.now()) {
|
|
931
|
-
const heldS = Math.max(0, Math.round((now - holder.since) / 1e3));
|
|
932
|
-
return `${holder.label} (pid ${holder.pid} on ${holder.host}, holding since ${new Date(holder.since).toISOString()}, ${heldS} s)`;
|
|
933
|
-
}
|
|
934
1176
|
function seconds(ms) {
|
|
935
1177
|
return `${Math.round(ms / 100) / 10} s`;
|
|
936
1178
|
}
|
|
937
|
-
let lockfileModule;
|
|
938
|
-
function loadLockfile() {
|
|
939
|
-
lockfileModule ??= import("proper-lockfile");
|
|
940
|
-
return lockfileModule;
|
|
941
|
-
}
|
|
942
1179
|
const activeRoots = new AsyncLocalStorage();
|
|
943
1180
|
const activeReadRoots = new AsyncLocalStorage();
|
|
944
1181
|
const gates = /* @__PURE__ */ new Map();
|
|
@@ -1092,7 +1329,7 @@ async function withKnowledgeMutation(root, mutate, options = {}) {
|
|
|
1092
1329
|
* fixed number of attempts delays a write instead of failing it.
|
|
1093
1330
|
*/
|
|
1094
1331
|
async function acquireStoreLock(root, cacheDir, options) {
|
|
1095
|
-
const lockfilePath = join(cacheDir,
|
|
1332
|
+
const lockfilePath = join(cacheDir, STORE_LOCK.directory);
|
|
1096
1333
|
const deadline = options.started + WRITER_WAIT_MS;
|
|
1097
1334
|
let pause = 10;
|
|
1098
1335
|
for (;;) try {
|
|
@@ -1102,6 +1339,7 @@ async function acquireStoreLock(root, cacheDir, options) {
|
|
|
1102
1339
|
retries: options.retries ?? 0
|
|
1103
1340
|
});
|
|
1104
1341
|
} catch (error) {
|
|
1342
|
+
if (isLocked(error) && await takeOverDeadHolder(cacheDir, options.staleMs)) continue;
|
|
1105
1343
|
const remaining = deadline - performance.now();
|
|
1106
1344
|
if (options.retries !== void 0 || error.code !== "ELOCKED" || remaining <= 0) throw await namedLockError(error, root, cacheDir, performance.now() - options.started);
|
|
1107
1345
|
await sleep(Math.min(remaining, pause * (.5 + Math.random())));
|
|
@@ -1110,43 +1348,12 @@ async function acquireStoreLock(root, cacheDir, options) {
|
|
|
1110
1348
|
}
|
|
1111
1349
|
/** A contended file lock, restated with the holder its owner record names. */
|
|
1112
1350
|
async function namedLockError(error, root, cacheDir, waitedMs) {
|
|
1113
|
-
if (error
|
|
1351
|
+
if (!isLocked(error)) return error;
|
|
1114
1352
|
const owner = await readLockOwner(cacheDir);
|
|
1115
1353
|
return Object.assign(new Error(`knowledge store is locked by another process: ${root}; waited ${seconds(waitedMs)} for ${owner ? describeHolder(owner) : "a holder that recorded no owner"}`, { cause: error }), { code: "ELOCKED" });
|
|
1116
1354
|
}
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
* Exported for the confinement tests only; src/index.ts does not re-export it.
|
|
1120
|
-
*/
|
|
1121
|
-
async function recordLockOwner(cacheDir, label) {
|
|
1122
|
-
const owner = {
|
|
1123
|
-
label,
|
|
1124
|
-
pid: process.pid,
|
|
1125
|
-
host: hostname(),
|
|
1126
|
-
since: Date.now()
|
|
1127
|
-
};
|
|
1128
|
-
try {
|
|
1129
|
-
await writeJsonDurableWithinRoot(cacheDir, LOCK_OWNER_FILE, owner);
|
|
1130
|
-
} catch {}
|
|
1131
|
-
}
|
|
1132
|
-
/**
|
|
1133
|
-
* `cacheDir` is withSafeDirectory's anchored path, so only the owner file itself can be swapped.
|
|
1134
|
-
* Anyone who can write the cache directory can plant a FIFO there, so the open adds O_NONBLOCK
|
|
1135
|
-
* and fstat refuses it. The shared readRegularFileNoFollow stays blocking: the event store and
|
|
1136
|
-
* file transactions read through it, and their descriptor use must not change.
|
|
1137
|
-
*/
|
|
1138
|
-
async function readLockOwner(cacheDir) {
|
|
1139
|
-
let handle;
|
|
1140
|
-
try {
|
|
1141
|
-
handle = await open(join(cacheDir, LOCK_OWNER_FILE), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
1142
|
-
if (!(await handle.stat()).isFile()) return void 0;
|
|
1143
|
-
const value = JSON.parse(await handle.readFile("utf8"));
|
|
1144
|
-
return typeof value.label === "string" && Number.isSafeInteger(value.pid) && typeof value.host === "string" && Number.isFinite(value.since) && !Number.isNaN(new Date(value.since).getTime()) ? value : void 0;
|
|
1145
|
-
} catch {
|
|
1146
|
-
return;
|
|
1147
|
-
} finally {
|
|
1148
|
-
await handle?.close().catch(() => void 0);
|
|
1149
|
-
}
|
|
1355
|
+
function isLocked(error) {
|
|
1356
|
+
return error?.code === "ELOCKED";
|
|
1150
1357
|
}
|
|
1151
1358
|
function noRetries(retries) {
|
|
1152
1359
|
return retries === 0 || typeof retries === "object" && !Array.isArray(retries) && retries.retries === 0;
|
|
@@ -1163,6 +1370,8 @@ function noRetries(retries) {
|
|
|
1163
1370
|
* an externally held lock carries the same guarantee and not a weaker one. Entering a scope for a
|
|
1164
1371
|
* lock the caller does not actually hold is the one way to defeat the store's single-writer rule;
|
|
1165
1372
|
* the hold is required, rather than assumed, so that is a statement the caller has to make.
|
|
1373
|
+
* Holding it also authorizes rolling back a pending transaction with no recovery owner, as every
|
|
1374
|
+
* holder of the store lock does.
|
|
1166
1375
|
*/
|
|
1167
1376
|
async function runInKnowledgeMutationScope(root, hold, body, options = {}) {
|
|
1168
1377
|
const resolvedRoot = resolve(root);
|
|
@@ -1197,7 +1406,8 @@ async function runOwnedMutation(resolvedRoot, cacheDir, hold, options, mutate) {
|
|
|
1197
1406
|
transactionRoot: mutationLock.transactionRoot
|
|
1198
1407
|
});
|
|
1199
1408
|
const pending = pendingState?.transaction ?? null;
|
|
1200
|
-
const
|
|
1409
|
+
const abandoned = pending !== null && isAbandonedTransaction(pending, options.resumeTransaction?.purpose);
|
|
1410
|
+
const resume = pending && !abandoned ? assertKnowledgeTransactionResume(pending, options.resumeTransaction) : void 0;
|
|
1201
1411
|
const locks = new Map(activeRoots.getStore());
|
|
1202
1412
|
locks.set(resolvedRoot, scope);
|
|
1203
1413
|
return await activeRoots.run(locks, async () => {
|
|
@@ -1212,8 +1422,8 @@ async function runOwnedMutation(resolvedRoot, cacheDir, hold, options, mutate) {
|
|
|
1212
1422
|
if (!stillPending) await finishMutationEpoch(cacheDir, epoch);
|
|
1213
1423
|
};
|
|
1214
1424
|
try {
|
|
1215
|
-
if (pending)
|
|
1216
|
-
|
|
1425
|
+
if (pending && abandoned) await rollBackAbandonedTransaction(resolvedRoot, mutationLock.transactionRoot, pending, mutationLock.assertOwned);
|
|
1426
|
+
else if (pending && resume) {
|
|
1217
1427
|
await recoverKnowledgeFileTransaction({
|
|
1218
1428
|
root: resolvedRoot,
|
|
1219
1429
|
transactionRoot: mutationLock.transactionRoot,
|
|
@@ -1265,20 +1475,32 @@ async function inspectPendingKnowledgeMutation(root) {
|
|
|
1265
1475
|
paths: transaction.entries.map((entry) => entry.path)
|
|
1266
1476
|
};
|
|
1267
1477
|
}
|
|
1478
|
+
/** Called once the abandoned case is excluded, so a mismatched purpose names a recovery owner. */
|
|
1268
1479
|
function assertKnowledgeTransactionResume(pending, resume) {
|
|
1269
|
-
if (!resume || pending.purpose !== resume.purpose)
|
|
1270
|
-
|
|
1271
|
-
purpose: pending.purpose,
|
|
1272
|
-
direction: "rollback"
|
|
1273
|
-
};
|
|
1274
|
-
throw new Error(`knowledge transaction '${pending.purpose}' requires its owner to resume`);
|
|
1275
|
-
}
|
|
1276
|
-
if (pending.recoveryOwner !== resume.recoveryOwner) {
|
|
1277
|
-
if (pending.recoveryOwner) throw new Error(`knowledge transaction '${pending.purpose}' must be resumed by '${pending.recoveryOwner}'`);
|
|
1278
|
-
throw new Error(`knowledge transaction '${pending.purpose}' has no recovery owner`);
|
|
1279
|
-
}
|
|
1480
|
+
if (!resume || pending.purpose !== resume.purpose || pending.recoveryOwner && pending.recoveryOwner !== resume.recoveryOwner) throw ownerResumeRefusal(pending);
|
|
1481
|
+
if (pending.recoveryOwner !== resume.recoveryOwner) throw new Error(`knowledge transaction '${pending.purpose}' has no recovery owner`);
|
|
1280
1482
|
return resume;
|
|
1281
1483
|
}
|
|
1484
|
+
/** The refusal every reader and writer meets while an owned transaction is pending. */
|
|
1485
|
+
function ownerResumeRefusal(pending) {
|
|
1486
|
+
const owner = pending.recoveryOwner ?? "its recovery owner";
|
|
1487
|
+
const transition = pending.purpose.startsWith("knowledge-promotion:") ? "promoteKnowledgeCandidate" : pending.purpose.startsWith("knowledge-restore:") ? "restoreKnowledgeCandidateBaseline" : void 0;
|
|
1488
|
+
const resumeCall = transition ? `re-run ${transition} with the same candidate${owner.startsWith("knowledge-improvement-activation:") ? " and the same activation" : ""}` : `re-run the call that owns '${owner}' with the same purpose`;
|
|
1489
|
+
return /* @__PURE__ */ new Error(`knowledge transaction '${pending.purpose}' requires its owner to resume: it must be resumed by '${owner}' (transaction ${pending.transactionId}, created ${pending.createdAt}); ${resumeCall}`);
|
|
1490
|
+
}
|
|
1491
|
+
async function rollBackAbandonedTransaction(root, transactionRoot, pending, assertOwned) {
|
|
1492
|
+
await recoverKnowledgeFileTransaction({
|
|
1493
|
+
root,
|
|
1494
|
+
transactionRoot,
|
|
1495
|
+
expectedPurpose: pending.purpose,
|
|
1496
|
+
direction: "rollback",
|
|
1497
|
+
assertOwned
|
|
1498
|
+
});
|
|
1499
|
+
process.emitWarning(`rolled back abandoned knowledge transaction ${pending.transactionId} ('${pending.purpose}', created ${pending.createdAt}): its writer ended without success and named no recovery owner`, {
|
|
1500
|
+
type: "AgentKnowledgeRecovery",
|
|
1501
|
+
code: "AGENT_KNOWLEDGE_TRANSACTION_ROLLBACK"
|
|
1502
|
+
});
|
|
1503
|
+
}
|
|
1282
1504
|
async function recoverPendingKnowledgeMutation(root, options) {
|
|
1283
1505
|
const pending = await inspectPendingKnowledgeMutation(root);
|
|
1284
1506
|
if (!pending) throw new Error("knowledge base has no pending mutation");
|
|
@@ -1341,7 +1563,7 @@ async function describeWriter(root) {
|
|
|
1341
1563
|
return owner ? describeHolder(owner) : "a writer that recorded no owner";
|
|
1342
1564
|
}
|
|
1343
1565
|
async function acquireDurableFileLock(target, options) {
|
|
1344
|
-
const stale =
|
|
1566
|
+
const stale = staleWindow(options.staleMs);
|
|
1345
1567
|
const update = Math.max(1e3, Math.floor(stale / 3));
|
|
1346
1568
|
let compromised;
|
|
1347
1569
|
let released = false;
|
|
@@ -1417,17 +1639,17 @@ async function waitForActiveMutationEpoch(root, epoch, options, pauseMs) {
|
|
|
1417
1639
|
}
|
|
1418
1640
|
async function healAbandonedMutationEpoch(root, expectedEpoch, options) {
|
|
1419
1641
|
await withSafeDirectory(root, ".agent-knowledge", false, async (cacheDir) => {
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
});
|
|
1427
|
-
} catch (error) {
|
|
1428
|
-
if (error.code === "ELOCKED") return;
|
|
1642
|
+
const acquire = () => acquireDurableFileLock(root, {
|
|
1643
|
+
lockfilePath: join(cacheDir, STORE_LOCK.directory),
|
|
1644
|
+
staleMs: options.staleMs,
|
|
1645
|
+
retries: 0
|
|
1646
|
+
}).catch((error) => {
|
|
1647
|
+
if (isLocked(error)) return void 0;
|
|
1429
1648
|
throw error;
|
|
1430
|
-
}
|
|
1649
|
+
});
|
|
1650
|
+
const acquired = await acquire() ?? (await takeOverDeadHolder(cacheDir, options.staleMs) ? await acquire() : void 0);
|
|
1651
|
+
if (!acquired) return;
|
|
1652
|
+
await recordLockOwner(cacheDir, "heal of an abandoned mutation epoch");
|
|
1431
1653
|
try {
|
|
1432
1654
|
const currentEpoch = await readMutationEpochFromCache(cacheDir);
|
|
1433
1655
|
if (currentEpoch !== expectedEpoch || !isOdd(currentEpoch)) return;
|
|
@@ -1435,14 +1657,8 @@ async function healAbandonedMutationEpoch(root, expectedEpoch, options) {
|
|
|
1435
1657
|
root,
|
|
1436
1658
|
transactionRoot: join(cacheDir, "file-transactions")
|
|
1437
1659
|
});
|
|
1438
|
-
if (pending &&
|
|
1439
|
-
|
|
1440
|
-
transactionRoot: join(cacheDir, "file-transactions"),
|
|
1441
|
-
expectedPurpose: pending.transaction.purpose,
|
|
1442
|
-
direction: "rollback",
|
|
1443
|
-
assertOwned: () => acquired.assertOwned()
|
|
1444
|
-
});
|
|
1445
|
-
else if (pending) throw new Error(`knowledge transaction '${pending.transaction.purpose}' requires its owner to resume`);
|
|
1660
|
+
if (pending && isAbandonedTransaction(pending.transaction)) await rollBackAbandonedTransaction(root, join(cacheDir, "file-transactions"), pending.transaction, () => acquired.assertOwned());
|
|
1661
|
+
else if (pending) throw ownerResumeRefusal(pending.transaction);
|
|
1446
1662
|
await finishMutationEpoch(cacheDir, currentEpoch);
|
|
1447
1663
|
} finally {
|
|
1448
1664
|
await acquired.release();
|
|
@@ -1452,11 +1668,11 @@ async function healAbandonedMutationEpoch(root, expectedEpoch, options) {
|
|
|
1452
1668
|
async function hasActiveMutationLock(root, options) {
|
|
1453
1669
|
try {
|
|
1454
1670
|
const { check } = await loadLockfile();
|
|
1455
|
-
return await withSafeDirectory(root, ".agent-knowledge", false, (cacheDir) => check(root, {
|
|
1456
|
-
lockfilePath: join(cacheDir,
|
|
1671
|
+
return await withSafeDirectory(root, ".agent-knowledge", false, async (cacheDir) => await check(root, {
|
|
1672
|
+
lockfilePath: join(cacheDir, STORE_LOCK.directory),
|
|
1457
1673
|
realpath: false,
|
|
1458
|
-
stale:
|
|
1459
|
-
}));
|
|
1674
|
+
stale: staleWindow(options.staleMs)
|
|
1675
|
+
}) && !await holderIsProvablyDead(cacheDir));
|
|
1460
1676
|
} catch (error) {
|
|
1461
1677
|
if (isMissingFile(error)) return false;
|
|
1462
1678
|
throw error;
|
|
@@ -5042,6 +5258,6 @@ function explainKnowledgeTarget(index, target) {
|
|
|
5042
5258
|
};
|
|
5043
5259
|
}
|
|
5044
5260
|
//#endregion
|
|
5045
|
-
export { loadSourceRegistry as $,
|
|
5261
|
+
export { loadSourceRegistry as $, writeFileDurableWithinRoot as $n, KNOWLEDGE_EVENT_TYPES as $t, validateKnowledgePage as A, finishKnowledgeFileTransaction as An, isScaffoldPath as At, CHECKABLE_RUNG_THRESHOLD as B, canonicalRelativeWithinRoot as Bn, KnowledgeEventSchema as Bt, immutablePageDigest as C, runInKnowledgeMutationScope as Cn, resolveKnowledgeCitation as Ct, normalizeVisibilityArtifact as D, applyKnowledgeFileTransaction as Dn, SCAFFOLD_PAGE_BASENAMES as Dt, nonEmpty as E, withKnowledgeRead as En, resolveRunScopedCitations as Et, detectNearDuplicatePages as F, recordLockOwner as Fn, WIKILINK_REGEX as Ft, gradeClaims as G, readRegularFileWithinRoot as Gn, KnowledgePageSchema as Gt, UncheckableClaimError as H, isMissingFile as Hn, KnowledgeGraphNodeSchema as Ht, normalizePageText as I, takeOverDeadHolder as In, extractWikilinks as It, verifyGradeableEvidence as J, renameDurable as Jn, ResearchClaimLedgerSchema as Jt, gradeFor as K, relativeWithinRoot as Kn, KnowledgeRelationSchema as Kt, prepareKnowledgeValidation as L, DEFAULT_PAGES_DIRECTORY as Ln, normalizeLinkTarget as Lt, verifyKnowledgeVisibilitySnapshot as M, prepareKnowledgeFileTransaction as Mn, layoutFor as Mt, visibilityIdentity as N, rollbackKnowledgeFileTransaction as Nn, loadKnowledgePages as Nt, normalizeVisibilityRef as O, assertKnowledgeMutationPath as On, initKnowledgeBase as Ot, visibilityIndexOf as P, RUN_LEASE as Pn, writeJson as Pt, addSourceText as Q, writeFileDurable as Qn, SourceRecordSchema as Qt, validateKnowledgeIndex as R, normalizePagesDirectory as Rn, DeepQuestionSchema as Rt, encodeKnowledgeVisibilitySnapshot as S, recoverPendingKnowledgeMutation as Sn, parseKnowledgeCitationReference as St, knowledgeVisibilityArtifactRef as T, withKnowledgeMutationLabel as Tn, resolveRunScopedCitation as Tt, assertGradeableEvidence as U, listRegularFilesWithinRoot as Un, KnowledgeIndexSchema as Ut, DEADLINE_EXIT_CODE as V, isKernelAnchoredPath as Vn, KnowledgeGraphEdgeSchema as Vt, claimCheckKey as W, readRegularFileNoFollow as Wn, KnowledgePageInvalidationSchema as Wt, writeKnowledgeIndex as X, withSafeDescendant as Xn, ResearchSourceVersionSchema as Xt, buildKnowledgeIndex as Y, syncDirectory as Yn, ResearchClaimRecordSchema as Yt, addSourcePath as Z, withSafeDirectory as Zn, SourceAnchorSchema as Zt, KNOWLEDGE_USE_RECEIPT_SCHEMA_VERSION as _, parseFrontmatter as _n, assertKnowledgeCitationsResolved as _t, applyKnowledgeWriteBlocksFile as a, claimEvidenceId as an, KB_CLAIM_LEDGER_DIR as at, decodeKnowledgeVisibilitySnapshot as b, isKnowledgeMutationHeld as bn, auditKnowledgeCitations as bt, KnowledgeDuplicateIntakeError as c, deepQuestionId as cn, KB_STORE_DIR as ct, createFileRunLineageAuthority as d, materializeRegisteredClaimEvidence as dn, buildKnowledgeGraph as dt, ClaimLedgerGoalConflictError as en, writeJsonDurable as er, snapshotSourceTextInput as et, createRunScopedStores as f, mergeClaimLedgers as fn, knowledgePageRelations as ft, KNOWLEDGE_RECEIPT_DIGEST_ALGORITHM as g, formatFrontmatter as gn, assertKnowledgeCitationAudit as gt, originatedPages as h, researchSourceVersionKey as hn, assertCurrentRunCitationsResolved as ht, applyKnowledgeWriteBlocks as i, assertTrackedClaimIntegrity as in, FileSystemKbStore as it, validateOrigin as j, knowledgeFileTransactionPlanHash as jn, knowledgePageFromMarkdown as jt, snapshotImmutableVisibility as k, commitKnowledgeFileMutations as kn, isKnowledgePagePath as kt, assertKnowledgeWriteIntake as l, emptyClaimLedger as ln, MemoryKbStore as lt, loadKnowledgeToolChain as m, normalizeClaimText as mn, KnowledgeCitationResolutionError as mt, inspectKnowledgeIndex as n, assertResearchClaimEvidenceIntegrity as nn, mediaTypeFor$1 as nr, writeSourceRegistry as nt, isSafeKnowledgePath as o, claimId as on, KB_EVENTS_PATH as ot, isKnowledgeToolChain as p, mergeTrackedClaims as pn, KnowledgeCitationAuditError as pt, verdictFor as q, removeDurable as qn, ResearchClaimEvidenceSchema as qt, stringMetadata as r, assertResearchClaimLedgerIntegrity as rn, textSourceAdapter as rr, ClaimLedgerMigrationRequiredError as rt, parseKnowledgeWriteBlocks as s, claimSourceHost as sn, KB_INDEX_PATH as st, explainKnowledgeTarget as t, assertDeepQuestionIntegrity as tn, writeJsonDurableWithinRoot as tr, sourceRegistryPath as tt, RUN_LINEAGE_BASENAME as u, linkClaimContradictions as un, assertClaimLedgerId as ut, assertSchemaVersion as v, acquireDurableFileLock as vn, assertRunScopedCitationsResolved as vt, knowledgePageDigest as w, withKnowledgeMutation as wn, resolveKnowledgeCitations as wt, digest as x, measureKnowledgeMutation as xn, formatKnowledgeCitationReference as xt, createKnowledgeVisibilitySnapshot as y, inspectPendingKnowledgeMutation as yn, auditCurrentRunCitations as yt, lintKnowledgeIndex as z, canonicalPathsEqual as zn, KnowledgeBaseCandidateSchema as zt };
|
|
5046
5262
|
|
|
5047
|
-
//# sourceMappingURL=inspect-
|
|
5263
|
+
//# sourceMappingURL=inspect-CTpCsUB2.js.map
|