pi-durable-subagents 1.0.38 → 1.0.40

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.
@@ -0,0 +1,110 @@
1
+ // Pi-side ledger state: only projections used by readers, never decoded ledger history or run bodies.
2
+ import { forgetJournalSnapshot, readJournalFold } from "../kernel/journal.js";
3
+ import { orchLedger } from "../paths.js";
4
+ import { JT } from "../types.js";
5
+ import { applyLedger, emptyLedger } from "./ledger.js";
6
+ import { labelsOf } from "../events/derive.js";
7
+ export function emptyReaderLedger() {
8
+ return { created: new Map(), origins: new Map(), intents: new Map(), revisions: new Map(), pruned: new Set(), labels: new Map(), runLabels: new Map(), sessions: new Map(), runSessions: new Map(),
9
+ byWid: new Map(), byOrigin: new Map(), state: emptyLedger(), createdRids: new Map(), decisions: new Map(), resolutions: new Map(), sendNotes: new Map() };
10
+ }
11
+ export function applyReaderLedger(ix, e) {
12
+ if (e.type === JT.created) {
13
+ if (!ix.created.has(e.wid))
14
+ ix.created.set(e.wid, { seq: e.seq, ts: e.ts, wid: e.wid, rid: e.rid, origin: e.origin });
15
+ ix.origins.set(String(e.wid), e.origin);
16
+ if (!ix.createdRids.has(e.rid))
17
+ ix.createdRids.set(e.rid, { wid: e.wid });
18
+ const labels = ix.runLabels.get(String(e.rid)), session = ix.runSessions.get(String(e.rid));
19
+ if (labels) {
20
+ if (!ix.labels.has(String(e.wid)))
21
+ ix.labels.set(String(e.wid), labels);
22
+ ix.runLabels.delete(String(e.rid));
23
+ }
24
+ if (session) {
25
+ if (!ix.sessions.has(String(e.wid)))
26
+ ix.sessions.set(String(e.wid), session);
27
+ ix.runSessions.delete(String(e.rid));
28
+ }
29
+ }
30
+ else if (e.type === "create-intent") {
31
+ if (!ix.intents.has(String(e.wid)))
32
+ ix.intents.set(String(e.wid), e.origin);
33
+ }
34
+ else if (e.type === "revise-intent") {
35
+ const revision = Number(e.revision), wid = String(e.wid);
36
+ if (Number.isFinite(revision))
37
+ ix.revisions.set(wid, Math.max(revision, ix.revisions.get(wid) ?? 0));
38
+ }
39
+ else if (e.type === "request") {
40
+ const r = e.request;
41
+ const labels = r?.kind === "run" ? labelsOf(r.body) : undefined;
42
+ if (labels && typeof r?.rid === "string")
43
+ ix.runLabels.set(r.rid, labels);
44
+ const session = r?.kind === "run" ? r.body?.session : undefined;
45
+ if (typeof session === "string" && session && typeof r?.rid === "string")
46
+ ix.runSessions.set(r.rid, session);
47
+ }
48
+ else if (e.type === "pruned") {
49
+ ix.pruned.add(String(e.wid));
50
+ ix.labels.delete(String(e.wid));
51
+ ix.sessions.delete(String(e.wid));
52
+ }
53
+ else if (e.type === "drain" || e.type === "undrain") {
54
+ const drain = { seq: e.seq, ts: e.ts, type: e.type };
55
+ if (e.type === "drain")
56
+ ix.lastDrain = drain;
57
+ if (e.wid === undefined && e.origin === undefined)
58
+ ix.global = drain;
59
+ if (e.wid !== undefined)
60
+ ix.byWid.set(e.wid, drain);
61
+ if (e.origin !== undefined)
62
+ ix.byOrigin.set(e.origin, drain);
63
+ }
64
+ if (e.type === JT.rejected) {
65
+ ix.runLabels.delete(String(e.rid));
66
+ ix.runSessions.delete(String(e.rid));
67
+ }
68
+ if (e.type === JT.applied || e.type === JT.rejected) {
69
+ const resolution = { type: e.type, ...(e.type === JT.rejected ? { reason: String(e.reason) } : {}) };
70
+ if (!ix.resolutions.has(e.rid))
71
+ ix.resolutions.set(e.rid, { seq: e.seq, ...resolution });
72
+ if (!(e.type === JT.rejected && e.reason === "identity-conflict") && !ix.decisions.has(e.rid))
73
+ ix.decisions.set(e.rid, resolution);
74
+ }
75
+ if (e.type === "send-note")
76
+ ix.sendNotes.set(e.rid, { seq: e.seq, ts: e.ts, type: e.type, rid: e.rid, model: e.model, effect: e.effect, pool: e.pool, delivery: e.delivery });
77
+ // applyLedger is shared with the orchestrator; reader projections discard execution-only switch history/skips.
78
+ // A holder needs only pool/slot/exec, not any extra payload the entry might carry.
79
+ if (e.type === "hold")
80
+ applyLedger(ix.state, { seq: e.seq, ts: e.ts, type: e.type, pool: e.pool, slot: e.slot, exec: e.exec });
81
+ else if (e.type !== "switch-observed" && e.type !== "skip" && e.type !== "writer-hold" && e.type !== "writer-release")
82
+ applyLedger(ix.state, e);
83
+ ix.state.seen++;
84
+ }
85
+ export function readerHold(ix, wid, origin) {
86
+ const candidates = [ix.global, ix.byWid.get(wid), origin !== undefined ? ix.byOrigin.get(origin) : undefined];
87
+ const last = candidates.reduce((a, b) => b && (!a || b.seq > a.seq) ? b : a, undefined);
88
+ const created = ix.created.get(wid);
89
+ return last?.type === "drain" && (!created || created.seq < last.seq) ? last : undefined;
90
+ }
91
+ const readers = new Map();
92
+ export function readerLedger(home) {
93
+ const path = orchLedger(home);
94
+ // Drop any raw snapshot loaded by another read path in this process. This reader never creates one itself.
95
+ forgetJournalSnapshot(path);
96
+ try {
97
+ const record = readJournalFold(path, readers.get(path), emptyReaderLedger, applyReaderLedger);
98
+ if (record) {
99
+ readers.set(path, record);
100
+ return record;
101
+ }
102
+ readers.delete(path);
103
+ return { dev: 0, ino: 0, size: 0, length: 0, seen: 0, state: emptyReaderLedger() };
104
+ }
105
+ catch (error) {
106
+ readers.delete(path);
107
+ throw error;
108
+ } // a partial fold is never retried as an accepted prefix
109
+ }
110
+ export const readerLedgerIdentity = (record) => `${record.dev}:${record.ino}:${record.size}`;