pi-durable-subagents 1.4.1 → 1.4.2
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 +12 -0
- package/README.md +3 -0
- package/dist/agent/main/snapshots.js +12 -6
- package/dist/orchestrator/snapshot.js +87 -11
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.4.2
|
|
4
|
+
|
|
5
|
+
- Memory: a main session's polling no longer keeps every earlier pass alive. Each workflow entry's lazy `entries`
|
|
6
|
+
getter was created inside the polling function and closed over that call's context, which held the preceding pass;
|
|
7
|
+
so each pass kept the one before it, with its listing of the workflows directory, ledger state and digests. A
|
|
8
|
+
long-running pi grew by about 0.25 GB per hour while its subagents worked and ran out of heap after about 12 hours.
|
|
9
|
+
A pass now keeps only the latest result.
|
|
10
|
+
- Memory: `status`, every `send` and the resume check no longer keep a digest of every settled workflow on the
|
|
11
|
+
machine. They keep only each settled journal's identity and owner, and read the few finished workflows they show
|
|
12
|
+
from the cache. On a home with 7,800 workflows a pi process kept about 220 MB after its first `status`; it keeps
|
|
13
|
+
about 26 MB. The output is unchanged.
|
|
14
|
+
|
|
3
15
|
## 1.4.1
|
|
4
16
|
|
|
5
17
|
- Docs, tests and fixtures use neutral example names and paths; no behaviour change.
|
package/README.md
CHANGED
|
@@ -1034,6 +1034,9 @@ for at most five seconds. Turn boundaries check immediately for late attention.
|
|
|
1034
1034
|
Ledger changes force a journal check;
|
|
1035
1035
|
final sessions are also rechecked within five seconds. Only an explicitly
|
|
1036
1036
|
opened final call keeps its transcript. Prune removes the workflow's caches.
|
|
1037
|
+
A `status` or `send` covers every workflow of the machine but keeps in memory
|
|
1038
|
+
only the identity of a settled journal that no poller holds; the few finished
|
|
1039
|
+
workflows it shows are read from the cache each time.
|
|
1037
1040
|
`DSA_CACHE_HOME=<directory>` places the derived cache under that directory
|
|
1038
1041
|
instead of `DSA_HOME`, without changing where authoritative data is read.
|
|
1039
1042
|
|
|
@@ -31,6 +31,17 @@ export class PendingOutboxReader {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
const passes = new Map();
|
|
34
|
+
/** One workflow of a pass. Built here, not inside workflows(): a getter created there closes over that call's whole
|
|
35
|
+
* context, which holds the preceding pass (`prior`, `old`), so every result entry kept the pass before it alive and,
|
|
36
|
+
* through that pass's own entries, every earlier one (each with its listing of w/, ledger state and digests). */
|
|
37
|
+
function mainWorkflow(wid, origin, digest, path) {
|
|
38
|
+
return { wid, origin, ...(digest.settled ? { settled: digest.settled } : {}), get entries() {
|
|
39
|
+
const entries = readJournalSnapshot(path);
|
|
40
|
+
if (digest.settled)
|
|
41
|
+
forgetJournalSnapshot(path);
|
|
42
|
+
return entries;
|
|
43
|
+
} };
|
|
44
|
+
}
|
|
34
45
|
export function workflows(home, origin, fresh = false) {
|
|
35
46
|
const { record, names, checked: listed } = readerWorkflowListing(home, fresh), ix = record.state, mark = readerLedgerIdentity(record), now = Date.now();
|
|
36
47
|
let byOrigin = passes.get(home);
|
|
@@ -59,12 +70,7 @@ export function workflows(home, origin, fresh = false) {
|
|
|
59
70
|
continue;
|
|
60
71
|
}
|
|
61
72
|
digests.set(wid, digest);
|
|
62
|
-
changes.set(wid,
|
|
63
|
-
const entries = readJournalSnapshot(path);
|
|
64
|
-
if (digest.settled)
|
|
65
|
-
forgetJournalSnapshot(path);
|
|
66
|
-
return entries;
|
|
67
|
-
} });
|
|
73
|
+
changes.set(wid, mainWorkflow(wid, from, digest, path));
|
|
68
74
|
}
|
|
69
75
|
const result = ids.map(wid => changes.get(wid) ?? old.get(wid)).filter((w) => !!w);
|
|
70
76
|
const same = prior && prior.result.length === result.length && result.every((w, i) => w === prior.result[i]);
|
|
@@ -766,6 +766,69 @@ export function workflowDigest(home, wid, ledger) {
|
|
|
766
766
|
}
|
|
767
767
|
return digest;
|
|
768
768
|
}
|
|
769
|
+
/** Settled journals a status listing checked without keeping their digest: the journal's identity and who owns the
|
|
770
|
+
* workflow. A listing covers every workflow of the machine (thousands) but shows only a few finished ones; keeping
|
|
771
|
+
* all their digests in `digests` cost each pi process hundreds of MB from its first `status` or `send`. */
|
|
772
|
+
const settledMarks = new Map();
|
|
773
|
+
/** Replace a settled digest that no poller held before (`held`) by its mark: the next reader takes it from disk. */
|
|
774
|
+
function markSettled(path) {
|
|
775
|
+
const memo = digests.get(path);
|
|
776
|
+
if (!memo?.digest.settled) {
|
|
777
|
+
settledMarks.delete(path);
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
const { origin, session } = memo.digest.snapshot;
|
|
781
|
+
settledMarks.set(path, { dev: memo.dev, ino: memo.ino, size: memo.size, ...(origin !== undefined ? { origin } : {}), ...(session !== undefined ? { session } : {}) });
|
|
782
|
+
digests.delete(path);
|
|
783
|
+
folds.delete(path);
|
|
784
|
+
forgetJournalSnapshot(path);
|
|
785
|
+
releasable.delete(path);
|
|
786
|
+
}
|
|
787
|
+
/** Whether a workflow's journal is settled as it is now, with the origin and session of its snapshot; decided without
|
|
788
|
+
* keeping its digest in memory unless a poller already holds it. Undefined: not settled (or not readable). */
|
|
789
|
+
function settledFacts(home, wid) {
|
|
790
|
+
const path = pathsOf(home, wid).journal;
|
|
791
|
+
if (digests.has(path)) {
|
|
792
|
+
const digest = workflowDigest(home, wid);
|
|
793
|
+
settledMarks.delete(path);
|
|
794
|
+
return digest.settled ? { origin: digest.snapshot.origin, session: digest.snapshot.session } : undefined;
|
|
795
|
+
}
|
|
796
|
+
let dev, ino, size;
|
|
797
|
+
try {
|
|
798
|
+
({ dev, ino, size } = statSync(path));
|
|
799
|
+
}
|
|
800
|
+
catch (error) {
|
|
801
|
+
if (error.code !== "ENOENT")
|
|
802
|
+
throw error;
|
|
803
|
+
settledMarks.delete(path);
|
|
804
|
+
return undefined;
|
|
805
|
+
}
|
|
806
|
+
const mark = settledMarks.get(path);
|
|
807
|
+
if (mark && mark.dev === dev && mark.ino === ino && mark.size === size)
|
|
808
|
+
return mark;
|
|
809
|
+
const stored = storedDigest(home, wid, dev, ino, size);
|
|
810
|
+
if (stored) {
|
|
811
|
+
const { origin, session } = stored.snapshot;
|
|
812
|
+
const facts = { ...(origin !== undefined ? { origin } : {}), ...(session !== undefined ? { session } : {}) };
|
|
813
|
+
settledMarks.set(path, { dev, ino, size, ...facts });
|
|
814
|
+
return facts;
|
|
815
|
+
}
|
|
816
|
+
// No usable stored digest: fold the journal once (this also stores its digest when settled), then keep only the mark.
|
|
817
|
+
const digest = workflowDigest(home, wid);
|
|
818
|
+
markSettled(path);
|
|
819
|
+
return digest.settled ? { origin: digest.snapshot.origin, session: digest.snapshot.session } : undefined;
|
|
820
|
+
}
|
|
821
|
+
/** `read` (one snapshot of a settled workflow) without keeping its digest in memory unless a poller already held it. */
|
|
822
|
+
function unretained(home, wid, read) {
|
|
823
|
+
const path = pathsOf(home, wid).journal, held = digests.has(path);
|
|
824
|
+
try {
|
|
825
|
+
return read();
|
|
826
|
+
}
|
|
827
|
+
finally {
|
|
828
|
+
if (!held)
|
|
829
|
+
markSettled(path);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
769
832
|
/** Last journal identity check of the current digest. Reuse never renews this timestamp. */
|
|
770
833
|
export function workflowDigestChecked(home, wid) {
|
|
771
834
|
return digests.get(pathsOf(home, wid).journal)?.checked ?? -Infinity;
|
|
@@ -906,6 +969,7 @@ function forgetRemovedWorkflows(home, names, pruned) {
|
|
|
906
969
|
for (const [wid, p] of paths)
|
|
907
970
|
if (pruned.has(wid) || !present.has(wid)) {
|
|
908
971
|
digests.delete(p.journal);
|
|
972
|
+
settledMarks.delete(p.journal);
|
|
909
973
|
folds.delete(p.journal);
|
|
910
974
|
forgetJournalSnapshot(p.journal);
|
|
911
975
|
firstOrigins.delete(p.journal);
|
|
@@ -1291,12 +1355,22 @@ function origins(home) {
|
|
|
1291
1355
|
}
|
|
1292
1356
|
/** Every workflow with its origin and hold, own session first, then newest first. */
|
|
1293
1357
|
function snapshots(home, origin) {
|
|
1294
|
-
const
|
|
1358
|
+
const owned = (w) => !!origin && ownedBy(w, origin);
|
|
1295
1359
|
const { since, held, labels, session } = heldWorkflows(home);
|
|
1296
|
-
const
|
|
1297
|
-
const wf = labelled(workflowSnapshot(home, wid), labels(wid), session(wid)), withOrigin = wf.origin === undefined &&
|
|
1360
|
+
const full = (wid, ledgerOrigin) => {
|
|
1361
|
+
const wf = labelled(workflowSnapshot(home, wid), labels(wid), session(wid)), withOrigin = wf.origin === undefined && ledgerOrigin !== undefined ? { ...wf, origin: ledgerOrigin } : wf;
|
|
1298
1362
|
return isLive(withOrigin) && held(wid) ? { ...withOrigin, paused: true } : withOrigin;
|
|
1299
|
-
}
|
|
1363
|
+
};
|
|
1364
|
+
const all = [...origins(home)].sort(([a], [b]) => a < b ? 1 : a > b ? -1 : 0).map(([wid, ledgerOrigin]) => {
|
|
1365
|
+
const facts = settledFacts(home, wid);
|
|
1366
|
+
if (facts) {
|
|
1367
|
+
let wf;
|
|
1368
|
+
return { wid, settled: true, own: owned({ origin: facts.origin ?? ledgerOrigin, session: session(wid) || facts.session }),
|
|
1369
|
+
snapshot: () => wf ??= unretained(home, wid, () => full(wid, ledgerOrigin)) };
|
|
1370
|
+
}
|
|
1371
|
+
const wf = full(wid, ledgerOrigin);
|
|
1372
|
+
return { wid, settled: settled(wf), own: owned(wf), snapshot: () => wf };
|
|
1373
|
+
}).sort((a, b) => Number(b.own) - Number(a.own));
|
|
1300
1374
|
return { all, ...(since !== undefined ? { since } : {}) };
|
|
1301
1375
|
}
|
|
1302
1376
|
/** P25, T10: Compact status of all workflows: own session first, then newest first; finished ones beyond the first `keep` collapse into a count. */
|
|
@@ -1304,9 +1378,9 @@ export function statusView(home, options = {}) {
|
|
|
1304
1378
|
const keep = options.keep ?? 10;
|
|
1305
1379
|
const { all, since } = snapshots(home, options.origin), leases = leaseState(home), byCall = leaseCalls(leases);
|
|
1306
1380
|
let finished = 0;
|
|
1307
|
-
const shown = all.filter(w => !settled
|
|
1381
|
+
const shown = all.filter(w => !w.settled || ++finished <= keep).map(w => w.snapshot());
|
|
1308
1382
|
const hidden = all.length - shown.length;
|
|
1309
|
-
const paused = all.filter(w => w.paused).length;
|
|
1383
|
+
const paused = all.filter(w => !w.settled && w.snapshot().paused).length;
|
|
1310
1384
|
return { workflows: shown.map(w => compactWorkflow(w, byCall)), ...(hidden ? { olderFinished: hidden, hint: "status wid=<wid> shows any workflow in detail" } : {}),
|
|
1311
1385
|
...(paused ? { paused: `${paused} workflow${paused > 1 ? "s" : ""} paused (stop-all, drain or a quit pi) since ${new Date(since).toISOString()}; resume continues them (new runs are not affected)` } : {}),
|
|
1312
1386
|
...slotsView(home), ...(leases.length ? { leases: leaseLines(leases) } : {}), ...runningStats(home) };
|
|
@@ -1460,7 +1534,7 @@ export function slotsView(home, now = Date.now()) {
|
|
|
1460
1534
|
* The full view (every call's last line and usage) ran to tens of thousands of tokens on a busy home. */
|
|
1461
1535
|
export function statusBrief(home, options = {}) {
|
|
1462
1536
|
const keep = options.keep ?? 5, now = options.now ?? Date.now(), origin = options.origin;
|
|
1463
|
-
const { all } = snapshots(home, origin), leases = leaseState(home), byCall = leaseCalls(leases, now);
|
|
1537
|
+
const { all: listed } = snapshots(home, origin), leases = leaseState(home), byCall = leaseCalls(leases, now);
|
|
1464
1538
|
const mine = (w) => !origin || ownedBy(w, origin);
|
|
1465
1539
|
const line = (w) => {
|
|
1466
1540
|
const p = progressOf(w), notOk = latestCalls(w).filter(c => c.result && !c.result.ok).length;
|
|
@@ -1492,15 +1566,16 @@ export function statusBrief(home, options = {}) {
|
|
|
1492
1566
|
progress: `${p.done}/${p.total}${p.plus ? "+" : ""}`, tokens: tokens(w.usage), calls,
|
|
1493
1567
|
...(asking.length ? { asking } : {}), ...(alerts.length ? { alerts } : {}) };
|
|
1494
1568
|
};
|
|
1495
|
-
const active =
|
|
1496
|
-
|
|
1569
|
+
const active = listed.filter(w => !w.settled).map(w => w.snapshot());
|
|
1570
|
+
// Snapshotted for its line only when shown (the first `keep`); the rest is a count.
|
|
1571
|
+
const finished = listed.filter(w => w.settled && (!origin || w.own));
|
|
1497
1572
|
const others = active.filter(w => !mine(w));
|
|
1498
1573
|
const heldMine = active.filter(w => mine(w) && w.paused).length, heldOthers = others.filter(w => w.paused);
|
|
1499
1574
|
const paused = [heldMine ? `${heldMine} workflow${heldMine > 1 ? "s" : ""} of this session ${heldMine > 1 ? "are" : "is"} paused (stop-all, drain or a quit pi); resume continues ${heldMine > 1 ? "them" : "it"}` : "",
|
|
1500
1575
|
heldOthers.length ? `${heldOthers.length} workflow${heldOthers.length > 1 ? "s" : ""} of other sessions paused (${heldOthers.slice(0, 8).map(w => w.wid).join(", ")}); resume wid=<wid> continues one` : ""].filter(Boolean).join("; ");
|
|
1501
1576
|
return {
|
|
1502
1577
|
active: active.filter(mine).map(brief), ...(others.length ? { otherSessions: others.slice(0, 10).map(line) } : {}),
|
|
1503
|
-
finished: finished.slice(0, keep).map(line), ...(finished.length > keep ? { olderFinished: finished.length - keep } : {}),
|
|
1578
|
+
finished: finished.slice(0, keep).map(w => line(w.snapshot())), ...(finished.length > keep ? { olderFinished: finished.length - keep } : {}),
|
|
1504
1579
|
...(paused ? { paused } : {}), ...slotsView(home, now), ...(leases.length ? { leases: leaseLines(leases, now) } : {}),
|
|
1505
1580
|
hint: "status wid=<wid> shows one workflow (outputs clipped); add key=<key> for one call's full result, or full:true for everything",
|
|
1506
1581
|
};
|
|
@@ -1535,7 +1610,8 @@ export function statusText(home, value) {
|
|
|
1535
1610
|
}
|
|
1536
1611
|
/** Running workflows of other sessions that a pause holds, as "wid" or "wid (name)": a session's own resume skips them. */
|
|
1537
1612
|
export function pausedElsewhere(home, origin) {
|
|
1538
|
-
return snapshots(home, origin).all.filter(w => w.
|
|
1613
|
+
return snapshots(home, origin).all.filter(w => !w.settled).map(w => w.snapshot()).filter(w => w.paused && w.origin !== origin)
|
|
1614
|
+
.map(w => w.name ? `${w.wid} (${w.name})` : w.wid);
|
|
1539
1615
|
}
|
|
1540
1616
|
/** "<rid>" or "<rid>/<rest>" of a run request that created a workflow → the same address with its wid; else unchanged.
|
|
1541
1617
|
* A run replies {submitted:{rid}} when its workflow is not created within 10 s, so the rid is all the caller has. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-durable-subagents",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
4
4
|
"description": "Subagents for pi that never lose work and never do it twice. Crash-safe workflows, automatic recovery, and a live view just like the main agent.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|