pi-durable-subagents 1.0.41 → 1.0.43
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 +43 -0
- package/README.md +29 -1
- package/dist/agent/main/snapshots.js +24 -1
- package/dist/agent/main/tool.js +3 -2
- package/dist/agent/main.js +5 -11
- package/dist/cli/control.js +20 -10
- package/dist/cli/main.js +13 -3
- package/dist/cli/requests.js +51 -32
- package/dist/events/fence.js +2 -0
- package/dist/events/pump.js +6 -2
- package/dist/events/waiting.js +12 -2
- package/dist/kernel/journal.js +294 -52
- package/dist/kernel/lifecycle.js +66 -23
- package/dist/kernel/mailbox.js +51 -32
- package/dist/kernel/overlay.js +47 -0
- package/dist/orchestrator/engine.js +228 -35
- package/dist/orchestrator/executor/effects/gate.js +10 -3
- package/dist/orchestrator/executor/effects/index.js +2 -1
- package/dist/orchestrator/executor/index.js +114 -40
- package/dist/orchestrator/executor/indexes.js +34 -0
- package/dist/orchestrator/executor/recovered.js +85 -0
- package/dist/orchestrator/executor/session.js +4 -0
- package/dist/orchestrator/executor/sweep.js +41 -9
- package/dist/orchestrator/ledger.js +12 -4
- package/dist/orchestrator/main.js +5 -2
- package/dist/orchestrator/providers.js +2 -0
- package/dist/orchestrator/reader-ledger.js +13 -3
- package/dist/orchestrator/settled.js +142 -0
- package/dist/orchestrator/snapshot.js +91 -11
- package/dist/orchestrator/store.js +248 -39
- package/dist/requests.js +41 -5
- package/dist/ui/tool.js +5 -3
- package/dist/ui/view.js +20 -8
- package/package.json +1 -1
|
@@ -2,12 +2,16 @@
|
|
|
2
2
|
import { forgetJournalSnapshot, readJournalFold } from "../kernel/journal.js";
|
|
3
3
|
import { orchLedger } from "../paths.js";
|
|
4
4
|
import { JT } from "../types.js";
|
|
5
|
-
import { applyLedger, emptyLedger } from "./ledger.js";
|
|
5
|
+
import { LEDGER_ENTRY_TYPES, applyLedger, emptyLedger } from "./ledger.js";
|
|
6
6
|
import { labelsOf } from "../events/derive.js";
|
|
7
7
|
export function emptyReaderLedger() {
|
|
8
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
9
|
byWid: new Map(), byOrigin: new Map(), state: emptyLedger(), createdRids: new Map(), decisions: new Map(), resolutions: new Map(), sendNotes: new Map() };
|
|
10
10
|
}
|
|
11
|
+
/** Reader-only projections, combined with applyLedger's type list by the byte filter. */
|
|
12
|
+
export const READER_ENTRY_TYPES = [JT.created, 'create-intent', 'revise-intent', 'request', 'pruned', 'drain', 'undrain',
|
|
13
|
+
JT.applied, JT.rejected, 'send-note'];
|
|
14
|
+
const discardedLedgerTypes = ['switch-observed', 'skip', 'writer-hold', 'writer-release'];
|
|
11
15
|
export function applyReaderLedger(ix, e) {
|
|
12
16
|
if (e.type === JT.created) {
|
|
13
17
|
if (!ix.created.has(e.wid))
|
|
@@ -78,7 +82,7 @@ export function applyReaderLedger(ix, e) {
|
|
|
78
82
|
// A holder needs only pool/slot/exec, not any extra payload the entry might carry.
|
|
79
83
|
if (e.type === "hold")
|
|
80
84
|
applyLedger(ix.state, { seq: e.seq, ts: e.ts, type: e.type, pool: e.pool, slot: e.slot, exec: e.exec });
|
|
81
|
-
else if (
|
|
85
|
+
else if (!discardedLedgerTypes.includes(e.type))
|
|
82
86
|
applyLedger(ix.state, e);
|
|
83
87
|
ix.state.seen++;
|
|
84
88
|
}
|
|
@@ -89,13 +93,19 @@ export function readerHold(ix, wid, origin) {
|
|
|
89
93
|
return last?.type === "drain" && (!created || created.seq < last.seq) ? last : undefined;
|
|
90
94
|
}
|
|
91
95
|
const readers = new Map();
|
|
96
|
+
// Canonical writer JSON: these are exactly the types that affect reader projections or applyLedger below.
|
|
97
|
+
const needles = [...new Set([...READER_ENTRY_TYPES.filter(type => type !== 'request'),
|
|
98
|
+
...LEDGER_ENTRY_TYPES.filter(type => !discardedLedgerTypes.includes(type))])].map(type => `"type":${JSON.stringify(type)}`);
|
|
99
|
+
// Request records only affect labels/session for runs; the other request kinds have no reader projection.
|
|
100
|
+
needles.push('"kind":"run"');
|
|
92
101
|
export function readerLedger(home) {
|
|
93
102
|
const path = orchLedger(home);
|
|
94
103
|
// Drop any raw snapshot loaded by another read path in this process. This reader never creates one itself.
|
|
95
104
|
forgetJournalSnapshot(path);
|
|
96
105
|
try {
|
|
97
|
-
const record = readJournalFold(path, readers.get(path), emptyReaderLedger, applyReaderLedger);
|
|
106
|
+
const record = readJournalFold(path, readers.get(path), emptyReaderLedger, applyReaderLedger, { needles });
|
|
98
107
|
if (record) {
|
|
108
|
+
record.state.state.seen = record.seen;
|
|
99
109
|
readers.set(path, record);
|
|
100
110
|
return record;
|
|
101
111
|
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Settled workflows: a rebuildable cache (DSA_HOME/cache/settled/<wid>.json, never authority) that lets a start open
|
|
2
|
+
// the journal of a finished workflow without decoding it. The record is valid only for the journal bytes it names
|
|
3
|
+
// (inode, size, entry count and last line, see JournalExpectation); anything else opens the journal as before.
|
|
4
|
+
// It holds exactly what a start and the orchestrator's loops over every workflow read of such a workflow; every other
|
|
5
|
+
// use decodes the journal first (a lazy handle decodes on its first entries() or append), so code that does not know
|
|
6
|
+
// about it stays correct.
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { rmSync } from "node:fs";
|
|
9
|
+
import { readCache, writeCache } from "../kernel/cache.js";
|
|
10
|
+
import { journalExpectation } from "../kernel/journal.js";
|
|
11
|
+
import { cacheDir, journalPath } from "../paths.js";
|
|
12
|
+
import { JT, isEntry } from "../types.js";
|
|
13
|
+
import { quiet } from "../events/pump.js";
|
|
14
|
+
import { foldWaits, liveWaitCalls } from "../events/waiting.js";
|
|
15
|
+
import { pendingFollowUps } from "./notes.js";
|
|
16
|
+
import { revisionEntries, terminalEntry } from "./store.js";
|
|
17
|
+
import { RECOVERY_VERSION } from "./executor/recovered.js";
|
|
18
|
+
import { sweepSeed } from "./executor/sweep.js";
|
|
19
|
+
import { callEntries } from "./executor/indexes.js";
|
|
20
|
+
/** Bump whenever the record gains a field or settledFacts a condition: every record is then rebuilt once. */
|
|
21
|
+
export const SETTLED_VERSION = 2;
|
|
22
|
+
export const settledPath = (home, wid) => join(cacheDir(home), "settled", `${wid}.json`);
|
|
23
|
+
/** Why a workflow is not settled, or its facts. Settled: it ended (its current revision is terminal) and nothing a start
|
|
24
|
+
* or a later request would act on is left: every call with an execution is fenced, ended and recovered (`recovered`:
|
|
25
|
+
* its receipt is current and it holds no writer lock), no follow-up is open or pending, every forward is retired or delivered, no
|
|
26
|
+
* worktree cleanup or reminder, gate, writer wait or fence failure is open, its finished attention is recorded (for the workflow
|
|
27
|
+
* and each generation), and no call is live for the waiting tracker. */
|
|
28
|
+
export function settledFacts(wf, recovered) {
|
|
29
|
+
const log = wf.journal.entries(), rev = revisionEntries(wf), done = terminalEntry(rev);
|
|
30
|
+
if (!done)
|
|
31
|
+
return "running";
|
|
32
|
+
const has = (type, field, value) => log.some(e => e.type === type && e[field] === value);
|
|
33
|
+
const sealedCalls = new Set(log.filter(e => e.type === JT.sealed).map(e => String(e.call)));
|
|
34
|
+
const ended = new Set([...sealedCalls, ...log.filter(e => e.type === "retired").map(e => String(e.call))]);
|
|
35
|
+
const fenced = new Set(log.filter(e => e.type === JT.fenced).map(e => String(e.exec)));
|
|
36
|
+
const calls = new Set();
|
|
37
|
+
for (const e of log)
|
|
38
|
+
if (e.type === JT.exec) {
|
|
39
|
+
const call = String(e.call);
|
|
40
|
+
calls.add(call);
|
|
41
|
+
if (!fenced.has(String(e.exec)))
|
|
42
|
+
return `unfenced:${String(e.exec)}`;
|
|
43
|
+
}
|
|
44
|
+
for (const call of calls) {
|
|
45
|
+
if (!ended.has(call))
|
|
46
|
+
return `live:${call}`;
|
|
47
|
+
if (!recovered(call))
|
|
48
|
+
return `unrecovered:${call}`;
|
|
49
|
+
}
|
|
50
|
+
// Follow-ups: every generation of the current revision ended, its finished attention recorded; none pending.
|
|
51
|
+
const attention = new Set(log.filter(e => isEntry(e, JT.attention)).map(e => `${e.item.id}\0${e.item.rev}`));
|
|
52
|
+
const items = new Set(log.filter(e => isEntry(e, JT.attention)).map(e => String(e.item.id)));
|
|
53
|
+
const finished = (id) => items.has(id);
|
|
54
|
+
for (const e of rev)
|
|
55
|
+
if (e.type === "generation") {
|
|
56
|
+
const id = `${wf.wid}@${wf.revision}/${e.key}@${e.gen}`;
|
|
57
|
+
if (!ended.has(id))
|
|
58
|
+
return `open-generation:${id}`;
|
|
59
|
+
if (sealedCalls.has(id) && !finished(`finished:${id}`))
|
|
60
|
+
return `unfinished-generation:${id}`;
|
|
61
|
+
}
|
|
62
|
+
if (rev.some(e => e.type === "forward-retired" && e.reason === "undelivered-follow-up"))
|
|
63
|
+
for (const key of new Set(rev.filter(e => e.type === "call" || e.type === "generation").map(e => String(e.key))))
|
|
64
|
+
if (pendingFollowUps(rev, key).length)
|
|
65
|
+
return `pending-follow-up:${key}`;
|
|
66
|
+
// A forward is settled when it was retired or its call received it (a replay then only republishes an envelope the
|
|
67
|
+
// outbox still has pending, which the start's republication does for every envelope anyway).
|
|
68
|
+
const settledForwards = new Set(log.filter(e => e.type === "forward-retired" || e.type === "forward-delivered").map(e => String(e.rid2)));
|
|
69
|
+
for (const e of log)
|
|
70
|
+
if (e.type === "forward" && !settledForwards.has(String(e.rid2)))
|
|
71
|
+
return `open-forward:${String(e.rid2)}`;
|
|
72
|
+
// Worktrees: a successful call's cleanup recorded; no open reminder.
|
|
73
|
+
for (const call of sealedCalls) {
|
|
74
|
+
const own = callEntries(wf.journal, call);
|
|
75
|
+
const result = own.find(e => e.type === JT.sealed)?.result;
|
|
76
|
+
if (result?.status === "ok" && own.some(e => e.type === "wt-intent") && !own.some(e => e.type === "wt-removed" || e.type === "wt-kept"))
|
|
77
|
+
return `worktree:${call}`;
|
|
78
|
+
}
|
|
79
|
+
const resolved = new Set(log.filter(e => e.type === JT.attentionResolved).map(e => `${String(e.id)}\0${String(e.rev)}`));
|
|
80
|
+
for (const e of log)
|
|
81
|
+
if (isEntry(e, JT.attention) && e.item.kind === "conflict" && String(e.item.id).startsWith("worktree:") && !resolved.has(`${e.item.id}\0${e.item.rev}`))
|
|
82
|
+
return `reminder:${e.item.id}`;
|
|
83
|
+
for (const e of log)
|
|
84
|
+
if (e.type === "gate-intent" && !has("gate", "id", e.id))
|
|
85
|
+
return `gate:${String(e.id)}`;
|
|
86
|
+
// A writer wait not followed by its acquisition or its call's end: recovery restores it.
|
|
87
|
+
for (const e of log)
|
|
88
|
+
if (e.type === "writer-wait" && !log.some(r => r.seq > e.seq && r.call === e.call && (r.type === "writer-acquired" || r.type === JT.sealed || r.type === "retired")))
|
|
89
|
+
return `writer-wait:${String(e.call)}`;
|
|
90
|
+
if (log.some(e => e.type === "fence-failed" && !log.some(r => r.seq > e.seq && ((r.type === JT.fenced && r.exec === e.exec) || (r.type === "gate" && r.id === e.exec) ||
|
|
91
|
+
(r.type === JT.attentionResolved && r.id === `fence:${String(e.exec)}`)))))
|
|
92
|
+
return "fence-failed";
|
|
93
|
+
// The workflow's own finished attention (Engine.attention) is recorded for its latest end.
|
|
94
|
+
if (!attention.has(`finished:${wf.wid}\0${log.filter(e => e.type === JT.done).length}`))
|
|
95
|
+
return "finished-attention";
|
|
96
|
+
if (liveWaitCalls(foldWaits(wf.wid, log)).length)
|
|
97
|
+
return "waits";
|
|
98
|
+
const revised = log.findLast(e => e.type === "revised");
|
|
99
|
+
return { revision: wf.revision, ...(revised ? { revised } : {}), done: { seq: done.seq, ts: done.ts, type: done.type, status: done.status }, resumed: log.filter(e => e.type === "resumed" && e.rid !== undefined).map(e => String(e.rid)),
|
|
100
|
+
quiet: quiet(wf.wid, log), calls: calls.size, sweep: sweepSeed(wf.journal) };
|
|
101
|
+
}
|
|
102
|
+
/** The settled record of `wid` when it is of this version and was written for the same workflow; not yet checked
|
|
103
|
+
* against the journal (openLazyJournal does). */
|
|
104
|
+
export function readSettled(home, wid) {
|
|
105
|
+
const record = readCache(settledPath(home, wid));
|
|
106
|
+
if (!record || record.v !== SETTLED_VERSION || record.recovery !== RECOVERY_VERSION || record.wid !== wid)
|
|
107
|
+
return undefined;
|
|
108
|
+
if (!record.journal || typeof record.journal.size !== "number" || !record.done || typeof record.revision !== "number" || !Array.isArray(record.sweep) || !Array.isArray(record.resumed))
|
|
109
|
+
return undefined;
|
|
110
|
+
// Version 2 records held fenced tags with obsolete pid/start lineage. Keep their tags only; no rebuild is needed.
|
|
111
|
+
const tags = [];
|
|
112
|
+
for (const s of record.sweep) {
|
|
113
|
+
if (typeof s === "string")
|
|
114
|
+
tags.push(s);
|
|
115
|
+
else if (s && typeof s === "object" && typeof s.exec === "string")
|
|
116
|
+
tags.push(s.exec);
|
|
117
|
+
else
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
record.sweep = tags;
|
|
121
|
+
return record;
|
|
122
|
+
}
|
|
123
|
+
/** Write the record of a settled workflow from its decoded journal (best effort: a failure only costs a decode at the
|
|
124
|
+
* next start). Returns whether it was written; a workflow that is not settled has its record removed. */
|
|
125
|
+
export function writeSettled(home, wf, recovered) {
|
|
126
|
+
const path = settledPath(home, wf.wid), facts = settledFacts(wf, recovered);
|
|
127
|
+
const journal = typeof facts === "string" ? undefined : journalExpectation(wf.journal);
|
|
128
|
+
if (typeof facts === "string" || !journal) {
|
|
129
|
+
try {
|
|
130
|
+
rmSync(path, { force: true });
|
|
131
|
+
}
|
|
132
|
+
catch { /* a cache */ }
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
const record = { v: SETTLED_VERSION, recovery: RECOVERY_VERSION, wid: wf.wid, journal, ...facts };
|
|
136
|
+
writeCache(path, record, journalPath(home, wf.wid));
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
/** Whether a workflow's journal is open lazily and not decoded yet: its settled record still describes it. */
|
|
140
|
+
export function lazy(wf) {
|
|
141
|
+
return wf.journal.loaded === false && wf.settled !== undefined;
|
|
142
|
+
}
|
|
@@ -8,7 +8,7 @@ import { crc32, forgetJournalSnapshot, forgetJournalSnapshotsIf, journalAppends,
|
|
|
8
8
|
import { readCache, writeCache } from "../kernel/cache.js";
|
|
9
9
|
import { digestDir, journalPath, pinnedDir, workflowDir } from "../paths.js";
|
|
10
10
|
import { JT, attentionEntries, isEntry } from "../types.js";
|
|
11
|
-
import { resolveModel } from "../compat/model.js";
|
|
11
|
+
import { parseModel, resolveModel } from "../compat/model.js";
|
|
12
12
|
import {} from "./ledger.js";
|
|
13
13
|
import { readerHold, readerLedger, readerLedgerIdentity } from "./reader-ledger.js";
|
|
14
14
|
import { packageVersion } from "../version.js";
|
|
@@ -433,7 +433,7 @@ const folds = new Map();
|
|
|
433
433
|
const derived = new WeakMap();
|
|
434
434
|
export function workflowSnapshot(home, wid, ledger) {
|
|
435
435
|
const base = workflowDigest(home, wid, ledger).snapshot;
|
|
436
|
-
const wf = base.calls.some(c => c.poolWait) ?
|
|
436
|
+
const wf = base.calls.some(c => c.poolWait || c.phase === "queued") ? withLedgerFacts(base, readerLedger(home).state.state) : base;
|
|
437
437
|
const planned = plannedTotal(home, wid, wf.rev);
|
|
438
438
|
return planned === undefined ? wf : { ...wf, planned };
|
|
439
439
|
}
|
|
@@ -771,6 +771,8 @@ function forgetRemovedWorkflows(home, names, pruned) {
|
|
|
771
771
|
paths.delete(wid);
|
|
772
772
|
}
|
|
773
773
|
}
|
|
774
|
+
/** Ledger types that determine holdOf/heldIn (other ledgerIndex projections do not affect holds). */
|
|
775
|
+
export const HOLD_ENTRY_TYPES = [JT.created, 'drain', 'undrain'];
|
|
774
776
|
/** Drain: the one hold rule. drain/undrain entries apply to every workflow (no scope), one session's (`origin`) or one
|
|
775
777
|
* workflow (`wid`). A workflow is held when the newest entry that applies to it is a drain recorded after it was created. */
|
|
776
778
|
export function holdOf(ledger, wid, origin) {
|
|
@@ -856,15 +858,91 @@ export function labelsText(labels, width = 60) {
|
|
|
856
858
|
return text ? `[${clip(text, Math.max(1, width - 3))}]` : "";
|
|
857
859
|
}
|
|
858
860
|
const decorated = new WeakMap();
|
|
859
|
-
/**
|
|
860
|
-
|
|
861
|
-
|
|
861
|
+
/** The orchestrator that is still recovering the home's workflows: its start entry says it recovers first, it has not
|
|
862
|
+
* recorded `recovered` and its process lives. Undefined for an orchestrator that does not say so (older versions). */
|
|
863
|
+
export function orchestratorStarting(state) {
|
|
864
|
+
const o = state.orchestrator;
|
|
865
|
+
if (!o?.recovery || o.recovered || o.exited)
|
|
866
|
+
return undefined;
|
|
867
|
+
// Readers ask on every refresh; the process check is kept for a second per start entry.
|
|
868
|
+
const now = Date.now(), hit = startingChecks.get(o);
|
|
869
|
+
const alive = hit && now >= hit.at && now - hit.at < 1000 ? hit.alive : processAlive(o.pid, o.start);
|
|
870
|
+
if (!hit || hit.alive !== alive || now < hit.at || now - hit.at >= 1000)
|
|
871
|
+
startingChecks.set(o, { at: now, alive });
|
|
872
|
+
return alive ? { pid: o.pid, since: o.ts } : undefined;
|
|
873
|
+
}
|
|
874
|
+
const startingChecks = new WeakMap();
|
|
875
|
+
/** A queued call's wait as one phrase: "orchestrator starting (12s)", "waiting for a free slot (p 2/2)", ... */
|
|
876
|
+
export function queuedText(w, now = Date.now(), span = age) {
|
|
877
|
+
return w.reason === "starting" ? `orchestrator starting (${span(now - (w.since ?? now))})` : w.detail;
|
|
878
|
+
}
|
|
879
|
+
const providerOf = (model) => { try {
|
|
880
|
+
return parseModel(model).provider;
|
|
881
|
+
}
|
|
882
|
+
catch {
|
|
883
|
+
return undefined;
|
|
884
|
+
} };
|
|
885
|
+
/** Why a queued call waits, from what the orchestrator ledger records (precedence as events/waiting.ts): the
|
|
886
|
+
* orchestrator still recovering; every candidate provider used up and not admitted now; the writer lock of its worktree;
|
|
887
|
+
* every candidate provider full. Undefined when none is known: a call is never said to wait for a slot without a full
|
|
888
|
+
* provider (memory headroom and an admission in progress look the same from here). Candidates: the provider it waits
|
|
889
|
+
* for to resume on, else those of the model or pool it asked for (else the configured default). */
|
|
890
|
+
export function queueWait(c, state, now = Date.now(), starting = orchestratorStarting(state)) {
|
|
891
|
+
if (c.phase !== "queued")
|
|
892
|
+
return undefined;
|
|
893
|
+
if (starting)
|
|
894
|
+
return { reason: "starting", detail: "orchestrator starting", since: starting.since };
|
|
895
|
+
const settings = state.config?.settings ?? {}, pools = settings.pools ?? {}, wanted = c.poolWait?.model ?? c.model ?? settings.defaultModel;
|
|
896
|
+
let providers = [];
|
|
897
|
+
if (wanted && Object.hasOwn(pools, wanted)) {
|
|
898
|
+
try {
|
|
899
|
+
providers = [...new Set(resolveModel(wanted, pools).map(m => m.provider).filter((p) => !!p))];
|
|
900
|
+
}
|
|
901
|
+
catch { /* an invalid pool: unknown */ }
|
|
902
|
+
}
|
|
903
|
+
else if (wanted) {
|
|
904
|
+
const p = providerOf(wanted);
|
|
905
|
+
if (p)
|
|
906
|
+
providers = [p];
|
|
907
|
+
}
|
|
908
|
+
const used = providers.map(p => [p, state.exhausted.get(p)]);
|
|
909
|
+
if (used.length && used.every(([, x]) => x && (now < x.nextTry || x.probe !== undefined)))
|
|
910
|
+
return { reason: "provider-exhausted", detail: `${providers.join(", ")} usage window${providers.length > 1 ? "s" : ""} used up` };
|
|
911
|
+
if (c.writerWait)
|
|
912
|
+
return { reason: "writer-lock", detail: `waiting for the writer lock of ${c.writerWait.root}` };
|
|
913
|
+
if (!providers.length)
|
|
914
|
+
return undefined;
|
|
915
|
+
let full = true;
|
|
916
|
+
const lines = providers.map(p => {
|
|
917
|
+
let held = 0;
|
|
918
|
+
for (const h of state.held.values())
|
|
919
|
+
if (h.pool === p)
|
|
920
|
+
held++;
|
|
921
|
+
const limit = settings.providers?.[p]?.slots;
|
|
922
|
+
if (limit === undefined || held < limit)
|
|
923
|
+
full = false;
|
|
924
|
+
return slotLine(p, held, limit);
|
|
925
|
+
});
|
|
926
|
+
return full ? { reason: "slot", detail: `waiting for a free slot (${lines.join(", ")})` } : undefined;
|
|
927
|
+
}
|
|
928
|
+
/** A mark of the ledger facts a workflow's queued calls show: they are derived again when it changes. */
|
|
929
|
+
function queueMark(wf, state, mark) {
|
|
930
|
+
if (!wf.calls.some(c => c.poolWait || c.phase === "queued"))
|
|
931
|
+
return undefined;
|
|
932
|
+
return `${mark}:${[...state.exhausted.values()].map(x => x.nextTry > Date.now()).join(",")}:${orchestratorStarting(state)?.since ?? ""}`;
|
|
933
|
+
}
|
|
934
|
+
/** Free slots and why queued calls wait are live ledger state, not repeated workflow journal records. */
|
|
935
|
+
function withLedgerFacts(wf, state) {
|
|
936
|
+
if (!wf.calls.some(c => c.poolWait || c.phase === "queued"))
|
|
862
937
|
return wf;
|
|
863
|
-
const held = new Map();
|
|
938
|
+
const held = new Map(), now = Date.now(), starting = orchestratorStarting(state);
|
|
864
939
|
for (const h of state.held.values())
|
|
865
940
|
held.set(h.pool, (held.get(h.pool) ?? 0) + 1);
|
|
866
941
|
const pools = state.config?.settings.pools, limits = state.config?.settings.providers;
|
|
867
942
|
return { ...wf, calls: wf.calls.map(c => {
|
|
943
|
+
const why = queueWait(c, state, now, starting);
|
|
944
|
+
if (why)
|
|
945
|
+
c = { ...c, queuedFor: why };
|
|
868
946
|
const wait = c.poolWait;
|
|
869
947
|
if (!wait)
|
|
870
948
|
return c;
|
|
@@ -892,10 +970,10 @@ function snapshotWithLedger(home, wid, ix, mark) {
|
|
|
892
970
|
const pendingRevision = (ix.revisions.get(wid) ?? 0) > (digests.get(pathsOf(home, wid).journal)?.digest.snapshot.rev ?? 0);
|
|
893
971
|
const base = workflowDigest(home, wid, pendingRevision ? undefined : mark).snapshot, planned = plannedTotal(home, wid, base.rev), l = ix.labels.get(wid), s = ix.sessions.get(wid);
|
|
894
972
|
const paused = isLive(base) && !!readerHold(ix, wid, ix.created.get(wid)?.origin), hit = decorated.get(base);
|
|
895
|
-
const slotMark = base
|
|
973
|
+
const slotMark = queueMark(base, ix.state, mark);
|
|
896
974
|
if (hit && hit.planned === planned && hit.labels === l && hit.session === s && hit.paused === paused && hit.slotMark === slotMark)
|
|
897
975
|
return hit.wf;
|
|
898
|
-
const live =
|
|
976
|
+
const live = withLedgerFacts(base, ix.state);
|
|
899
977
|
const planned1 = planned === undefined ? live : { ...live, planned }, labelled1 = labelled(planned1, l, s);
|
|
900
978
|
const wf = paused ? { ...labelled1, paused: true } : labelled1;
|
|
901
979
|
decorated.set(base, { planned, labels: l, session: s, paused, slotMark, wf });
|
|
@@ -1014,7 +1092,7 @@ export function compactWorkflow(wf, leases) {
|
|
|
1014
1092
|
...(c.model ? { model: c.model } : {}), ...(c.tools ? { tools: c.tools } : {}), ...(c.pending ? { pending: c.pending } : {}), ...(c.switching ? { switching: c.switching } : {}), ...(c.switchFailed ? { switchFailed: c.switchFailed } : {}), ...(nonzero(c.usage) ? { usage: c.usage } : {}),
|
|
1015
1093
|
...(last ? { lastLine: clip(last, 200) } : {}), ...(r?.error ? { error: clip(r.error, 300) } : {}), ...(c.hibernated ? { hibernated: true } : {}),
|
|
1016
1094
|
...(c.answered ? { answered: c.answered } : {}), ...(c.answerForwarded ? { answerForwarded: c.answerForwarded } : {}), ...(c.writerWait && !r ? { writerWait: c.writerWait } : {}), ...(!r && leases?.get(c.callId) ? { lease: leases.get(c.callId) } : {}),
|
|
1017
|
-
...(c.notesPending ? { notesPending: c.notesPending } : {}) };
|
|
1095
|
+
...(c.notesPending ? { notesPending: c.notesPending } : {}), ...(c.queuedFor && !r && !wf.paused ? { queued: queuedText(c.queuedFor) } : {}) };
|
|
1018
1096
|
}),
|
|
1019
1097
|
attention: wf.attention.map(a => ({ id: a.id, rev: a.rev, kind: a.kind, text: clip(a.text, 300), ...(a.call ? { call: a.call } : {}), ...(a.qid ? { qid: a.qid } : {}) })),
|
|
1020
1098
|
...(wf.paused ? { paused: true } : {}), ...(wf.followUps ? { followUps: wf.followUps } : {}), ...(wf.labels ? { labels: wf.labels } : {}),
|
|
@@ -1078,7 +1156,9 @@ export function orchestratorView(state, loaded = packageVersion(), now = Date.no
|
|
|
1078
1156
|
return {};
|
|
1079
1157
|
const r = o.forceRestart, note = r && now - r.ts < 24 * 60 * 60_000
|
|
1080
1158
|
? `; restarted by force ${age(now - r.ts)} ago by ${initiatorSummary(r.initiator, r.from)}: ${restartLine(r.reason ?? "no reason recorded")}` : "";
|
|
1081
|
-
|
|
1159
|
+
// Still recovering the home's workflows: nothing is dispatched yet, whatever the slots say.
|
|
1160
|
+
const starting = orchestratorStarting(state), phase = starting ? `, starting ${age(now - starting.since)}` : "";
|
|
1161
|
+
return { orchestrator: `${o.version} (pid ${o.pid}${phase})${note}`, ...(o.version === loaded ? {} : { versionNote: versionNote(o.version, loaded) }) };
|
|
1082
1162
|
}
|
|
1083
1163
|
/** Whether the recorded orchestrator still runs. On Linux its start time also tells it from a later process given the
|
|
1084
1164
|
* same pid after a crash (elsewhere the pid alone is checked). */
|
|
@@ -1204,7 +1284,7 @@ export function statusBrief(home, options = {}) {
|
|
|
1204
1284
|
...(c.hibernated ? { hibernated: true } : {}), ...(c.answered ? { answered: c.answered } : {}), ...(c.answerForwarded ? { answerForwarded: c.answerForwarded } : {}), ...(live && c.writerWait ? { writerWait: c.writerWait } : {}),
|
|
1205
1285
|
...(live && byCall.get(c.callId) ? { lease: byCall.get(c.callId) } : {}),
|
|
1206
1286
|
...(live && c.switching ? { switching: c.switching } : {}), ...(live && c.switchFailed ? { switchFailed: c.switchFailed } : {}),
|
|
1207
|
-
...(c.notesPending ? { notesPending: c.notesPending } : {}) };
|
|
1287
|
+
...(c.notesPending ? { notesPending: c.notesPending } : {}), ...(live && c.queuedFor && !w.paused ? { queued: queuedText(c.queuedFor, now) } : {}) };
|
|
1208
1288
|
});
|
|
1209
1289
|
const asking = open.filter(a => a.kind === "question" && a.call).map(a => ({ to: `${w.wid}/${callKey(a.call)}`, ...(a.qid ? { qid: a.qid } : {}),
|
|
1210
1290
|
...(w.calls.some(c => c.callId === a.call && c.hibernated) ? { hibernated: true } : {}), question: clip(a.text, 300) }));
|