pi-durable-subagents 1.0.39 → 1.0.41
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 +53 -0
- package/README.md +38 -2
- package/dist/agent/child.js +65 -22
- package/dist/agent/main/snapshots.js +63 -74
- package/dist/agent/main.js +17 -16
- package/dist/cli/control.js +1 -1
- package/dist/cli/main.js +7 -4
- package/dist/cli/restart.js +2 -2
- package/dist/compat/model.js +12 -0
- package/dist/kernel/cache.js +40 -0
- package/dist/kernel/journal.js +85 -11
- package/dist/orchestrator/config.js +6 -1
- package/dist/orchestrator/executor/hibernate.js +11 -0
- package/dist/orchestrator/executor/index.js +202 -22
- package/dist/orchestrator/executor/observe.js +25 -4
- package/dist/orchestrator/executor/pool.js +39 -0
- package/dist/orchestrator/executor/session.js +4 -0
- package/dist/orchestrator/reader-ledger.js +110 -0
- package/dist/orchestrator/snapshot.js +440 -42
- package/dist/orchestrator/store.js +9 -1
- package/dist/paths.js +6 -0
- package/dist/requests.js +2 -2
- package/dist/types.js +2 -0
- package/dist/ui/data.js +150 -44
- package/dist/ui/index.js +14 -5
- package/dist/ui/session.js +18 -3
- package/dist/ui/view.js +3 -3
- package/package.json +1 -1
|
@@ -5,7 +5,7 @@ import { join } from "node:path";
|
|
|
5
5
|
import { stat as fileStat } from "node:fs/promises";
|
|
6
6
|
import { performance } from "node:perf_hooks";
|
|
7
7
|
import { createInterface } from "node:readline";
|
|
8
|
-
import { JT, attentionEntries } from "../../types.js";
|
|
8
|
+
import { CT, JT, attentionEntries } from "../../types.js";
|
|
9
9
|
import { callDir, callSession } from "../../paths.js";
|
|
10
10
|
import { monotoneTime } from "../../kernel/guards.js";
|
|
11
11
|
import { readSession } from "./session.js";
|
|
@@ -35,7 +35,7 @@ export async function observeExecution(d) {
|
|
|
35
35
|
let size = (await fileStat(session).catch(() => ({ size: 0 }))).size, checkpoint = performance.now();
|
|
36
36
|
let signal;
|
|
37
37
|
const boundary = new Promise(resolve => { signal = resolve; d.setWake(resolve); });
|
|
38
|
-
let failure, pending = Promise.resolve(), ending = false;
|
|
38
|
+
let failure, pending = Promise.resolve(), ending = false, modelBlocked = false;
|
|
39
39
|
const has = (type) => entriesOf(t.journal, type, exec).length > 0;
|
|
40
40
|
const enqueue = (fn) => { if (!ending)
|
|
41
41
|
pending = pending.then(fn).catch(error => { failure = error; signal(); }); };
|
|
@@ -145,14 +145,26 @@ export async function observeExecution(d) {
|
|
|
145
145
|
if (event.type === "message_update" || event.type === "tool_execution_update")
|
|
146
146
|
return;
|
|
147
147
|
enqueue(async () => {
|
|
148
|
+
if (modelBlocked)
|
|
149
|
+
return;
|
|
150
|
+
if (event.type === CT.modelReady || event.type === CT.modelUnavailable) {
|
|
151
|
+
if (await d.modelControl?.(event) === false) {
|
|
152
|
+
modelBlocked = true;
|
|
153
|
+
signal();
|
|
154
|
+
}
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if ((event.type === "message_start" || event.type === "message_end") && await d.switched(event) === false) {
|
|
158
|
+
modelBlocked = true;
|
|
159
|
+
signal();
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
148
162
|
const args = event.args;
|
|
149
163
|
if (event.type === "tool_execution_start" && (event.toolName === "edit" || event.toolName === "write") && typeof args?.path === "string")
|
|
150
164
|
await d.wrote?.(args.path);
|
|
151
165
|
const slim = observation(event);
|
|
152
166
|
if (slim) {
|
|
153
167
|
await serial(() => t.journal.append("observation", { exec, event: slim }));
|
|
154
|
-
if (event.type === "message_start")
|
|
155
|
-
await d.switched(event);
|
|
156
168
|
if (event.type === "message_end") {
|
|
157
169
|
await d.recordUsage([{ id: String(slim.id), usage: slim.usage }]);
|
|
158
170
|
await d.answered?.(event);
|
|
@@ -200,6 +212,15 @@ export async function observeExecution(d) {
|
|
|
200
212
|
size = nextSize;
|
|
201
213
|
}
|
|
202
214
|
const entries = await (d.read ? d.read() : readSession(session));
|
|
215
|
+
for (const e of entries)
|
|
216
|
+
if (e.type === "custom" && (e.customType === CT.modelReady || e.customType === CT.modelUnavailable) && e.data?.exec === exec)
|
|
217
|
+
if (await d.modelControl?.({ type: e.customType, ...e.data }) === false) {
|
|
218
|
+
modelBlocked = true;
|
|
219
|
+
signal();
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (modelBlocked)
|
|
223
|
+
return;
|
|
203
224
|
await d.questions(entries);
|
|
204
225
|
const values = d.usage ? d.usage(entries) : sessionUsage(entries, t.callId);
|
|
205
226
|
if (values !== usageList) {
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { parseModel, sameModel } from "../../compat/model.js";
|
|
2
|
+
/** A parent model request keeps its pin after it was applied, including in continued generations. */
|
|
3
|
+
export function parentPinned(t) {
|
|
4
|
+
if (t.model)
|
|
5
|
+
return true;
|
|
6
|
+
const log = t.journal.entries(), calls = new Set([t.callId]);
|
|
7
|
+
if (t.continueFrom)
|
|
8
|
+
calls.add(t.continueFrom);
|
|
9
|
+
for (let i = log.length - 1; i >= 0; i--) {
|
|
10
|
+
const e = log[i];
|
|
11
|
+
if (e.type === "generation" && calls.has(`${t.wid}@${e.revision}/${e.key}@${e.gen}`)) {
|
|
12
|
+
if (typeof e.model === "string")
|
|
13
|
+
return true;
|
|
14
|
+
calls.add(String(e.from));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return log.some(e => {
|
|
18
|
+
if (e.type !== "forward" || !calls.has(String(e.dest)) || e.failover || e.envelope?.kind !== "model")
|
|
19
|
+
return false;
|
|
20
|
+
if (log.some(r => r.type === "forward-delivered" && r.rid2 === e.rid2 && r.reason !== undefined))
|
|
21
|
+
return false;
|
|
22
|
+
return !log.some(r => r.type === "forward" && r.dest === e.dest && r.envelope?.kind === "withdraw" &&
|
|
23
|
+
r.envelope.body?.rids?.includes(String(e.rid2)));
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** Other providers with the same id first, then pool order; admission still checks afterMs and slots. */
|
|
27
|
+
export function spillCandidates(pinned, candidates) {
|
|
28
|
+
const others = candidates.filter(m => m.provider !== pinned.provider);
|
|
29
|
+
const name = (m) => parseModel(m.id).id.split("/").at(-1).toLowerCase();
|
|
30
|
+
return [...others.filter(m => name(m) === name(pinned)), ...others.filter(m => name(m) !== name(pinned))];
|
|
31
|
+
}
|
|
32
|
+
export const MODEL_RETRY_MS = 600_000;
|
|
33
|
+
export function modelFailures(journal, call) {
|
|
34
|
+
const execs = new Set(journal.entries().filter(e => e.type === "exec" && e.call === call).map(e => e.exec));
|
|
35
|
+
return journal.entries().filter(e => e.type === "model-unavailable" && execs.has(e.exec));
|
|
36
|
+
}
|
|
37
|
+
export function failedModel(failures, model) {
|
|
38
|
+
return failures.some(e => sameModel(e.selected, model) && Number(e.until) > Date.now());
|
|
39
|
+
}
|
|
@@ -166,6 +166,10 @@ export function refusedByProvider(text) {
|
|
|
166
166
|
export function sessionModel(entries) {
|
|
167
167
|
// pi restores the model of the last model change or assistant message: a relaunch with `--model` on an existing
|
|
168
168
|
// session records no model change, so only the answer tells which model the session went on with.
|
|
169
|
+
// Virtual answers name the routed physical model; only a later selection replaces the virtual one.
|
|
170
|
+
const selection = entries.findLast(e => e.type === "model_change" || e.type === "custom" && e.customType === CT.modelReady);
|
|
171
|
+
if (selection?.customType === CT.modelReady && selection.data?.virtual === true)
|
|
172
|
+
return selection.data.model;
|
|
169
173
|
const last = entries.findLast(e => e.type === "model_change" && e.provider && e.modelId
|
|
170
174
|
|| e.type === "message" && e.message?.role === "assistant" && !!e.message.provider && !!e.message.model);
|
|
171
175
|
if (!last)
|
|
@@ -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}`;
|