pi-durable-subagents 1.0.27 → 1.0.29
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 +135 -10
- package/dist/agent/child.js +7 -2
- package/dist/agent/main/schema.js +4 -1
- package/dist/agent/main/tool.js +42 -8
- package/dist/agent/main.js +62 -12
- package/dist/cli/doctor.js +4 -3
- package/dist/cli/drill/index.js +287 -0
- package/dist/cli/drill/provider.js +25 -0
- package/dist/cli/hold.js +28 -14
- package/dist/cli/main.js +50 -11
- package/dist/cli/requests.js +102 -18
- package/dist/kernel/journal.js +99 -15
- package/dist/kernel/mailbox.js +1 -1
- package/dist/orchestrator/engine.js +157 -12
- package/dist/orchestrator/evaluator-client.js +18 -3
- package/dist/orchestrator/executor/digest.js +80 -0
- package/dist/orchestrator/executor/index.js +47 -37
- package/dist/orchestrator/executor/observe.js +21 -7
- package/dist/orchestrator/notes.js +20 -0
- package/dist/orchestrator/restart.js +5 -3
- package/dist/orchestrator/snapshot.js +144 -21
- package/dist/orchestrator/stats.js +49 -0
- package/dist/platform/containment.js +35 -11
- package/dist/platform/lease.js +37 -11
- package/dist/platform/proctable.js +16 -3
- package/dist/requests.js +27 -0
- package/dist/ui/data.js +10 -0
- package/dist/ui/index.js +9 -6
- package/dist/ui/screen.js +53 -10
- package/dist/ui/view.js +92 -12
- package/package.json +1 -1
|
@@ -2,11 +2,24 @@
|
|
|
2
2
|
// Known-id filtering assumes argv/other values do not impersonate the sole known
|
|
3
3
|
// execution id. Multiple known ids are ambiguous; macOS verification awaits CI.
|
|
4
4
|
import { readFile, readdir } from "node:fs/promises";
|
|
5
|
-
import { readFileSync } from "node:fs";
|
|
5
|
+
import { closeSync, openSync, readFileSync, readSync } from "node:fs";
|
|
6
6
|
import { execFile } from "node:child_process";
|
|
7
7
|
import { promisify } from "node:util";
|
|
8
8
|
const exec = promisify(execFile);
|
|
9
9
|
const vanished = (error) => ["ENOENT", "ESRCH", "EACCES", "EPERM"].includes(error.code ?? "");
|
|
10
|
+
// /proc/<pid>/stat is one short line: one open/read/close into a reused buffer, not readFileSync's fstat, sized read
|
|
11
|
+
// and EOF read (a scan reads it for every process of the machine every second).
|
|
12
|
+
const statBuffer = Buffer.alloc(4096);
|
|
13
|
+
function readStat(pid) {
|
|
14
|
+
const fd = openSync(`/proc/${pid}/stat`, "r");
|
|
15
|
+
try {
|
|
16
|
+
const n = readSync(fd, statBuffer, 0, statBuffer.length, 0);
|
|
17
|
+
return n < statBuffer.length ? statBuffer.toString("latin1", 0, n) : readFileSync(`/proc/${pid}/stat`, "latin1");
|
|
18
|
+
}
|
|
19
|
+
finally {
|
|
20
|
+
closeSync(fd);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
10
23
|
/** C2, C3: Capture only the newly spawned pid; an absent or dead process has no token. */
|
|
11
24
|
export async function captureStart(pid) {
|
|
12
25
|
if (process.platform === "linux") {
|
|
@@ -94,7 +107,7 @@ export class ProcessTable {
|
|
|
94
107
|
continue;
|
|
95
108
|
try {
|
|
96
109
|
// /proc stat is a tiny kernel snapshot; avoid four libuv round trips per cached identity.
|
|
97
|
-
const stat =
|
|
110
|
+
const stat = readStat(name);
|
|
98
111
|
const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
99
112
|
// Zombies cannot act and may remain indefinitely under a non-reaping init.
|
|
100
113
|
if (fields[0] === "Z" || fields[0] === "X")
|
|
@@ -112,7 +125,7 @@ export class ProcessTable {
|
|
|
112
125
|
throw error;
|
|
113
126
|
}
|
|
114
127
|
// Do not attach an old environment to a reused pid; cached identities already include the start token.
|
|
115
|
-
const check =
|
|
128
|
+
const check = readStat(name);
|
|
116
129
|
if (check.slice(check.lastIndexOf(")") + 2).split(" ")[19] !== start)
|
|
117
130
|
continue;
|
|
118
131
|
}
|
package/dist/requests.js
CHANGED
|
@@ -21,6 +21,33 @@ export function requestRid(id) {
|
|
|
21
21
|
}
|
|
22
22
|
/** The request id of a `req:<id>` rid; undefined for any other rid (ULIDs never contain ':'). */
|
|
23
23
|
export function requestId(rid) { return rid.startsWith(PREFIX) ? rid.slice(PREFIX.length) : undefined; }
|
|
24
|
+
/** The request ids of one send to `n` calls under `<id>`: `<id>:1` ... `<id>:n`, one per target in the order given
|
|
25
|
+
* (each is an ordinary request id, so a retry with the same list gets the same outcomes). Each body carries the list's
|
|
26
|
+
* digest (`batch`, see batchDigest), so another list under the id is a request-conflict (checked by batchConflict
|
|
27
|
+
* before anything is sent). */
|
|
28
|
+
export function manyIds(id, n) {
|
|
29
|
+
const ids = Array.from({ length: n }, (_, i) => `${id}:${i + 1}`);
|
|
30
|
+
for (const derived of ids)
|
|
31
|
+
if (!REQUEST_ID.test(derived))
|
|
32
|
+
throw new Error(`request id ${JSON.stringify(id)} is too long for a send to ${n} calls (${derived} exceeds 124 characters)`);
|
|
33
|
+
requestRid(id);
|
|
34
|
+
return ids;
|
|
35
|
+
}
|
|
36
|
+
/** The `batch` of every request of a send to the calls `targets` (in the order given). */
|
|
37
|
+
export function batchDigest(targets) { return contentHash({ to: targets }); }
|
|
38
|
+
/** Whether `<id>` names other content than a send to several calls with this `batch`: a single request under `<id>`, or
|
|
39
|
+
* a send to several calls with another list (its first request `<id>:1` has another batch). */
|
|
40
|
+
export async function batchConflict(home, id, batch) {
|
|
41
|
+
if (await findRequest(home, requestRid(id)))
|
|
42
|
+
return true;
|
|
43
|
+
const first = `${id}:1`, prior = REQUEST_ID.test(first) ? await findRequest(home, requestRid(first)) : undefined;
|
|
44
|
+
return prior !== undefined && prior.request.body?.batch !== batch;
|
|
45
|
+
}
|
|
46
|
+
/** Whether `<id>` already names a send to several calls (its first derived id `<id>:1` is recorded). */
|
|
47
|
+
export async function namesMany(home, id) {
|
|
48
|
+
const first = `${id}:1`;
|
|
49
|
+
return REQUEST_ID.test(first) && await findRequest(home, requestRid(first)) !== undefined;
|
|
50
|
+
}
|
|
24
51
|
/** Spec_digest = contentHash({kind, body, cond}) (cond omitted when absent). A run's body.origin (the pi session
|
|
25
52
|
* branch offered for context:"fork") is delivery metadata, not spec: it differs on every turn, so it is not hashed. */
|
|
26
53
|
export function specDigest(req) {
|
package/dist/ui/data.js
CHANGED
|
@@ -51,6 +51,13 @@ const FINAL_MS = 10_000;
|
|
|
51
51
|
/** A1, P25: Read the UI's data from durable workflow snapshots and native session tails only. */
|
|
52
52
|
export class UiData {
|
|
53
53
|
workflows = [];
|
|
54
|
+
/** Every workflow read by the last refresh, before the owning session splits them (a watched call of another
|
|
55
|
+
* origin stays watchable after its workflow settles). */
|
|
56
|
+
all = [];
|
|
57
|
+
/** Live workflows of other origins (other pi sessions, the CLI), split off by the session that owns this data. */
|
|
58
|
+
others = [];
|
|
59
|
+
/** Whether other origins' live work is shown (the dock's elsewhere count, the list's Other sessions group). */
|
|
60
|
+
otherSessions = true;
|
|
54
61
|
/** Facts and session branch per call id; a finished call's are read on first `get` (see refresh). */
|
|
55
62
|
facts = new Map();
|
|
56
63
|
sessions = new Map();
|
|
@@ -153,6 +160,7 @@ export class UiData {
|
|
|
153
160
|
load();
|
|
154
161
|
}
|
|
155
162
|
this.workflows = workflows;
|
|
163
|
+
this.all = workflows;
|
|
156
164
|
this.facts = facts;
|
|
157
165
|
this.sessions = sessions;
|
|
158
166
|
// UI-only optional aliases; absence or invalid config must not break execution (P21).
|
|
@@ -161,11 +169,13 @@ export class UiData {
|
|
|
161
169
|
this.aliases = Object.fromEntries(Object.entries(config.ui?.modelAliases ?? {}).filter(([, v]) => typeof v === "string"));
|
|
162
170
|
this.dock = ["auto", "line", "off"].includes(config.ui?.dock) ? config.ui.dock : "auto";
|
|
163
171
|
this.dockAt = config.ui?.dockAt === "below" ? "below" : "above";
|
|
172
|
+
this.otherSessions = config.ui?.otherSessions !== false;
|
|
164
173
|
}
|
|
165
174
|
catch {
|
|
166
175
|
this.aliases = {};
|
|
167
176
|
this.dock = "auto";
|
|
168
177
|
this.dockAt = "above";
|
|
178
|
+
this.otherSessions = true;
|
|
169
179
|
}
|
|
170
180
|
}
|
|
171
181
|
}
|
package/dist/ui/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { matchesKey, truncateToWidth, visibleWidth } from "@earendil-works/pi-tu
|
|
|
2
2
|
import { UiActions, UiData } from "./data.js";
|
|
3
3
|
import { SubagentScreen } from "./screen.js";
|
|
4
4
|
import { oneLine } from "./frame.js";
|
|
5
|
-
import { dockLines, mainLine, modelLabel, orderWorkflows } from "./view.js";
|
|
5
|
+
import { dockLines, mainLine, modelLabel, orderWorkflows, otherWorkflows, othersLive } from "./view.js";
|
|
6
6
|
import { registerCards } from "./cards.js";
|
|
7
7
|
import { toolRenderers } from "./tool.js";
|
|
8
8
|
/** UI §1–3, P16, P21: Register journal-backed list/watch surfaces only in interactive pi. */
|
|
@@ -67,16 +67,19 @@ export function registerUi(pi, deps) {
|
|
|
67
67
|
if (stopped)
|
|
68
68
|
return;
|
|
69
69
|
try {
|
|
70
|
+
// Split by origin at once: nothing between the read and the split may leave other sessions' workflows among
|
|
71
|
+
// this session's (the screen also checks the origin itself before any change).
|
|
70
72
|
data.refresh();
|
|
73
|
+
const own = `main:${ctx.sessionManager.getSessionId()}`;
|
|
74
|
+
data.others = data.otherSessions ? otherWorkflows(data.workflows, own) : [];
|
|
75
|
+
data.workflows = orderWorkflows(data.workflows, own);
|
|
71
76
|
actions.reconcile();
|
|
72
77
|
for (const result of actions.resolutions.splice(0))
|
|
73
78
|
screen?.controlResult(result);
|
|
74
|
-
const own = `main:${ctx.sessionManager.getSessionId()}`;
|
|
75
|
-
data.workflows = orderWorkflows(data.workflows, own);
|
|
76
79
|
// UI §1: the dock — live rows per active agent plus a summary line ("auto"), only the summary ("line"), or nothing.
|
|
77
80
|
const name = (model) => modelLabel(model, (p, id) => ctx.modelRegistry.find(p, id), data.aliases);
|
|
78
81
|
const now = Date.now(), dock = data.dock;
|
|
79
|
-
const lines = (width) => dock === "off" ? [] : dock === "line" ? [mainLine(data.workflows) ?? ""].filter(Boolean) : dockLines(data.workflows, data.facts, name, width, now);
|
|
82
|
+
const lines = (width) => dock === "off" ? [] : dock === "line" ? [mainLine(data.workflows, data.others, width) ?? ""].filter(Boolean) : dockLines(data.workflows, data.facts, name, width, now, 3, data.others);
|
|
80
83
|
dockRows = lines;
|
|
81
84
|
// The widget is installed once, at session start (and again only when its placement changes). pi stacks
|
|
82
85
|
// widgets in the order they were set, so installing once keeps the dock where the extension load order puts
|
|
@@ -117,7 +120,7 @@ export function registerUi(pi, deps) {
|
|
|
117
120
|
const open = () => {
|
|
118
121
|
if (stopped || opening)
|
|
119
122
|
return "busy";
|
|
120
|
-
if (!data.workflows.length)
|
|
123
|
+
if (!data.workflows.length && !othersLive(data.others))
|
|
121
124
|
return "empty";
|
|
122
125
|
opening = true;
|
|
123
126
|
void (async () => {
|
|
@@ -131,7 +134,7 @@ export function registerUi(pi, deps) {
|
|
|
131
134
|
};
|
|
132
135
|
openList = open;
|
|
133
136
|
unsubscribe = ctx.ui.onTerminalInput(key => {
|
|
134
|
-
if (stopped || opening || !matchesKey(key, "down") || ctx.ui.getEditorText() !== "" || !editorFocused() || !data.workflows.length)
|
|
137
|
+
if (stopped || opening || !matchesKey(key, "down") || ctx.ui.getEditorText() !== "" || !editorFocused() || !data.workflows.length && !othersLive(data.others))
|
|
135
138
|
return;
|
|
136
139
|
open();
|
|
137
140
|
return { consume: true };
|
package/dist/ui/screen.js
CHANGED
|
@@ -2,7 +2,7 @@ import { AssistantMessageComponent, UserMessageComponent, ToolExecutionComponent
|
|
|
2
2
|
import { Input, SelectList, fuzzyFilter, matchesKey, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
3
3
|
import { CT } from "../types.js";
|
|
4
4
|
import { UiActions, UiData } from "./data.js";
|
|
5
|
-
import { doneOrder, isOpen, plainReason, toggleOpen, duration, keepSelection, summaryText, label, listRows, modelLabel, pendingText, resultPhrase, rowText, toolCount } from "./view.js";
|
|
5
|
+
import { doneOrder, isOpen, originText, plainReason, toggleOpen, duration, keepSelection, summaryText, label, listRows, modelLabel, pendingText, resultPhrase, rowText, toolCount } from "./view.js";
|
|
6
6
|
import { fitWidth, frame, inner } from "./frame.js";
|
|
7
7
|
import { thinkingElapsed } from "./thinking.js";
|
|
8
8
|
import { thoughtSummary } from "./session.js";
|
|
@@ -101,8 +101,17 @@ export class SubagentScreen {
|
|
|
101
101
|
refresh() { if (!this.disposed)
|
|
102
102
|
this.tui.requestRender(); }
|
|
103
103
|
name = (model) => modelLabel(model, (p, id) => this.ctx.modelRegistry.find(p, id), this.data.aliases);
|
|
104
|
+
/** A workflow another session or the CLI started: watchable here, never changed from here. */
|
|
105
|
+
foreign(w) {
|
|
106
|
+
// Decided by origin, not by list membership: a refresh interrupted before the split must not make them editable.
|
|
107
|
+
const id = this.ctx.sessionManager?.getSessionId?.();
|
|
108
|
+
return !!w && (id === undefined || w.origin !== `main:${id}`);
|
|
109
|
+
}
|
|
110
|
+
readOnly(w) {
|
|
111
|
+
return `read-only: started by ${originText(w.origin)}; change it from that session or the CLI (or send with an explicit address)`;
|
|
112
|
+
}
|
|
104
113
|
current() {
|
|
105
|
-
const w = this.data.workflows.find(w => w.wid === this.workflow);
|
|
114
|
+
const w = this.data.workflows.find(w => w.wid === this.workflow) ?? this.data.all.find(w => w.wid === this.workflow);
|
|
106
115
|
let c = w?.calls.find(c => c.callId === this.watching);
|
|
107
116
|
// P37: watching a key follows its newest generation (a continued call reopens on the same session).
|
|
108
117
|
const newest = c && w.calls.filter(x => x.key === c.key && x.gen > c.gen).sort((a, b) => b.gen - a.gen)[0];
|
|
@@ -145,8 +154,15 @@ export class SubagentScreen {
|
|
|
145
154
|
return;
|
|
146
155
|
if (row.kind === "call")
|
|
147
156
|
this.open(row.workflow, row.call);
|
|
157
|
+
else if (row.kind === "workflow" && row.other) {
|
|
158
|
+
const wid = row.workflow.wid;
|
|
159
|
+
if (!this.state.folded.delete(wid))
|
|
160
|
+
this.state.folded.add(wid);
|
|
161
|
+
}
|
|
148
162
|
else if (row.kind === "workflow")
|
|
149
163
|
toggleOpen(row.workflow, this.state);
|
|
164
|
+
else if (row.kind === "others")
|
|
165
|
+
this.state.others = !this.state.others;
|
|
150
166
|
else if (row.kind === "finished")
|
|
151
167
|
this.state.finished = !this.state.finished;
|
|
152
168
|
else if (row.workflow) {
|
|
@@ -174,6 +190,10 @@ export class SubagentScreen {
|
|
|
174
190
|
const c = target ?? this.current().c;
|
|
175
191
|
if (!c)
|
|
176
192
|
return;
|
|
193
|
+
if (!target && this.foreign(this.current().w)) {
|
|
194
|
+
this.notice = this.readOnly(this.current().w);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
177
197
|
this.menuTitle = `Model for ${c.key}`;
|
|
178
198
|
const models = this.ctx.modelRegistry.getAvailable();
|
|
179
199
|
this.menuItems = models.map(m => ({ value: `${m.provider}/${m.id}`, label: this.name(`${m.provider}/${m.id}`) }));
|
|
@@ -203,9 +223,13 @@ export class SubagentScreen {
|
|
|
203
223
|
this.menu.onCancel = onCancel;
|
|
204
224
|
}
|
|
205
225
|
thinkingMenu() {
|
|
206
|
-
const { c } = this.current();
|
|
226
|
+
const { c, w } = this.current();
|
|
207
227
|
if (!c)
|
|
208
228
|
return;
|
|
229
|
+
if (this.foreign(w)) {
|
|
230
|
+
this.notice = this.readOnly(w);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
209
233
|
const model = this.data.facts.get(c.callId)?.model ?? c.model;
|
|
210
234
|
if (!model)
|
|
211
235
|
return;
|
|
@@ -221,9 +245,13 @@ export class SubagentScreen {
|
|
|
221
245
|
};
|
|
222
246
|
}
|
|
223
247
|
cycleThinking() {
|
|
224
|
-
const { c } = this.current();
|
|
248
|
+
const { c, w } = this.current();
|
|
225
249
|
if (!c)
|
|
226
250
|
return;
|
|
251
|
+
if (this.foreign(w)) {
|
|
252
|
+
this.notice = this.readOnly(w);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
227
255
|
const facts = this.data.facts.get(c.callId), model = facts?.model ?? c.model;
|
|
228
256
|
if (!model) {
|
|
229
257
|
this.notice = "Model not yet recorded";
|
|
@@ -239,6 +267,10 @@ export class SubagentScreen {
|
|
|
239
267
|
const message = this.input.getValue().trim();
|
|
240
268
|
if (!message)
|
|
241
269
|
return;
|
|
270
|
+
if (this.foreign(w)) {
|
|
271
|
+
this.notice = this.readOnly(w);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
242
274
|
if (message === "/model") {
|
|
243
275
|
this.modelMenu();
|
|
244
276
|
this.input.setValue("");
|
|
@@ -315,7 +347,9 @@ export class SubagentScreen {
|
|
|
315
347
|
else if (!this.busy) {
|
|
316
348
|
const row = this.selectedRow(), c = row?.kind === "call" ? row.call : undefined, w = row?.workflow;
|
|
317
349
|
const asking = c && w?.attention.some(a => a.kind === "question" && a.call === c.callId);
|
|
318
|
-
if (key === "
|
|
350
|
+
if (key.length === 1 && "sfamxr".includes(key) && (row?.other || this.foreign(w)))
|
|
351
|
+
this.listNotice = { text: this.readOnly(w), until: Date.now() + 6000 };
|
|
352
|
+
else if (key === "s" && c && c.phase !== "sealed")
|
|
319
353
|
this.beginListInput("steer", row);
|
|
320
354
|
else if (key === "f" && c?.phase === "sealed")
|
|
321
355
|
this.beginListInput("follow-up", row);
|
|
@@ -529,13 +563,20 @@ export class SubagentScreen {
|
|
|
529
563
|
// A two-column gutter carries the selection marker: a background alone is invisible in some themes and in
|
|
530
564
|
// plain-text captures, and x must show which row it would stop.
|
|
531
565
|
const rowWidth = Math.max(1, size.width - 2);
|
|
566
|
+
const before = this.rows.find(r => r.id === this.selectedId), wasOther = Boolean(before?.other || before?.kind === "others");
|
|
532
567
|
this.rows = this.doneTab && w ? doneOrder(w.calls).map(c => {
|
|
533
568
|
const f = this.data.facts.get(c.callId);
|
|
534
569
|
return { id: c.callId, kind: "call", workflow: w, call: c, failed: !c.result?.ok, text: rowText(" ", label(c), this.name(f?.model ?? c.model), resultPhrase(c), [toolCount(f?.tools)], rowWidth) };
|
|
535
|
-
}) : listRows(this.data.workflows, this.state, this.data.facts, this.name, rowWidth);
|
|
570
|
+
}) : listRows(this.data.workflows, this.state, this.data.facts, this.name, rowWidth, Date.now(), this.data.others);
|
|
536
571
|
height = this.height(Math.max(1, this.rows.length) + footerRows);
|
|
537
572
|
size = inner(width, height); // inner width does not depend on height
|
|
538
|
-
|
|
573
|
+
// A vanished row of another session (its workflow ended, or the whole group went) never hands the selection to
|
|
574
|
+
// a different row of this session by position, where x or r would act on it: the group header takes it, or no
|
|
575
|
+
// row is selected (-1) until ↑/↓ chooses one.
|
|
576
|
+
if (wasOther && !this.rows.some(r => r.id === this.selectedId))
|
|
577
|
+
this.selected = this.rows.findIndex(r => r.kind === "others");
|
|
578
|
+
else if (this.selected >= 0 || this.selectedId !== undefined)
|
|
579
|
+
this.selected = keepSelection(this.rows, this.selectedId, this.selected);
|
|
539
580
|
while (this.rows[this.selected]?.kind === "preview" && this.selected > 0)
|
|
540
581
|
this.selected--;
|
|
541
582
|
this.selectedId = this.rows[this.selected]?.id;
|
|
@@ -547,8 +588,9 @@ export class SubagentScreen {
|
|
|
547
588
|
});
|
|
548
589
|
const row = this.selectedRow(), c = row?.kind === "call" ? row.call : undefined;
|
|
549
590
|
const asking = c && row?.workflow?.attention.some(a => a.kind === "question" && a.call === c.callId);
|
|
550
|
-
const narrow = size.width < 72;
|
|
551
|
-
const keys = [narrow ? "↑↓" : "↑ ↓ select", row
|
|
591
|
+
const narrow = size.width < 72, readOnly = row?.other || this.foreign(row?.workflow) || this.doneTab && this.foreign(w);
|
|
592
|
+
const keys = readOnly ? [narrow ? "↑↓" : "↑ ↓ select", row?.kind === "workflow" ? `Enter ${narrow ? "" : this.state.folded.has(row.workflow.wid) ? "expand" : "collapse"}`.trim() : row?.kind === "call" ? (narrow ? "Enter" : "Enter watch") : "",
|
|
593
|
+
narrow ? "read-only" : `read-only: started by ${originText(row?.workflow?.origin ?? w?.origin)}; change it there`, "Esc back"].filter(Boolean).join(" · ") : [narrow ? "↑↓" : "↑ ↓ select", row && row.kind !== "preview" ? `Enter ${narrow ? "" : row.kind === "workflow" ? (isOpen(row.workflow, this.state) ? "collapse" : "expand") : row.kind === "call" ? "watch" : row.kind === "others" ? (this.state.others ? "collapse" : "expand") : "open"}`.trim() : "",
|
|
552
594
|
c && c.phase !== "sealed" ? (narrow ? "s" : "s steer") : "", c?.phase === "sealed" ? (narrow ? "f" : "f follow-up") : "",
|
|
553
595
|
c && c.phase !== "sealed" || row?.kind === "workflow" && row.workflow?.status === "running" ? (narrow ? "x" : "x stop") : "",
|
|
554
596
|
c ? (narrow ? "m" : "m model") : "", asking ? (narrow ? "a" : "a answer") : "", row?.workflow?.paused ? (narrow ? "r" : "r resume") : "", "Esc back"].filter(Boolean).join(" · ");
|
|
@@ -569,7 +611,8 @@ export class SubagentScreen {
|
|
|
569
611
|
const head = [tabs, `${label(c)} · ${this.name(facts?.model ?? c.model)} ▾${switching} · ${facts?.thinking ?? "off"} ▾${tools ? ` · ${tools}` : ""}${pending ? ` · ${pending}` : ""}`,
|
|
570
612
|
this.theme.fg("dim", this.spend(c, facts)), rule];
|
|
571
613
|
const asking = w.attention.some(a => a.kind === "question" && a.call === c.callId);
|
|
572
|
-
const placeholder =
|
|
614
|
+
const placeholder = this.foreign(w) ? `Read-only: started by ${originText(w.origin)}`
|
|
615
|
+
: `${c.phase === "sealed" ? "Continue" : asking ? "Reply to" : "Steer"} ${c.key}…${this.uses < 3 ? " / for commands" : ""}`;
|
|
573
616
|
const empty = new Input({ prompt: "", placeholder, placeholderStyle: text => this.theme.fg("dim", text) });
|
|
574
617
|
empty.focused = this.focused;
|
|
575
618
|
const editor = this.input.getValue() ? this.input.render(size.width) : empty.render(size.width);
|
package/dist/ui/view.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
2
|
-
import { isLive, liveCalls, progressOf } from "../orchestrator/snapshot.js";
|
|
2
|
+
import { isLive, labelsText, liveCalls, progressOf } from "../orchestrator/snapshot.js";
|
|
3
3
|
import { oneLine } from "./frame.js";
|
|
4
4
|
/** UI §2: Render compact wall-clock durations; never present these as charged active time. */
|
|
5
5
|
export function duration(ms) {
|
|
@@ -34,6 +34,50 @@ export function orderWorkflows(workflows, own) {
|
|
|
34
34
|
return workflows.filter(w => own === undefined || w.origin === own)
|
|
35
35
|
.sort((a, b) => (b.startedAt ?? 0) - (a.startedAt ?? 0) || (a.wid < b.wid ? 1 : a.wid > b.wid ? -1 : 0));
|
|
36
36
|
}
|
|
37
|
+
/** Live workflows started elsewhere (another pi session, the CLI or an unknown origin), newest first. A workflow of
|
|
38
|
+
* another origin counts while it is not settled (as in the status tool's otherSessions): running, parked, or
|
|
39
|
+
* finished with an open follow-up. */
|
|
40
|
+
export function otherWorkflows(workflows, own) {
|
|
41
|
+
return orderWorkflows(workflows.filter(w => w.origin !== own && (isLive(w) || w.status === "parked")));
|
|
42
|
+
}
|
|
43
|
+
/** Who started a workflow, short: `cli:<user>@<host>` as is, a pi session by the head of its id. */
|
|
44
|
+
export function originText(origin) {
|
|
45
|
+
if (!origin)
|
|
46
|
+
return "unknown origin";
|
|
47
|
+
return origin.startsWith("main:") ? `session ${origin.slice(5, 13)}` : origin;
|
|
48
|
+
}
|
|
49
|
+
const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
50
|
+
/** Live calls of other origins in a few words, e.g. "elsewhere: 4 running (cli 2, 1 session 2)"; undefined when none. */
|
|
51
|
+
export function elsewhereText(others) {
|
|
52
|
+
let cli = 0, unknown = 0, inSessions = 0;
|
|
53
|
+
const sessions = new Set();
|
|
54
|
+
for (const w of others) {
|
|
55
|
+
const n = liveCalls(w).length;
|
|
56
|
+
if (!n)
|
|
57
|
+
continue;
|
|
58
|
+
if (w.origin?.startsWith("cli:"))
|
|
59
|
+
cli += n;
|
|
60
|
+
else if (w.origin?.startsWith("main:")) {
|
|
61
|
+
inSessions += n;
|
|
62
|
+
sessions.add(w.origin);
|
|
63
|
+
}
|
|
64
|
+
else
|
|
65
|
+
unknown += n;
|
|
66
|
+
}
|
|
67
|
+
const total = cli + inSessions + unknown;
|
|
68
|
+
if (!total)
|
|
69
|
+
return undefined;
|
|
70
|
+
const parts = [cli ? `cli ${cli}` : "", inSessions ? `${plural(sessions.size, "session")} ${inSessions}` : "", unknown ? `other ${unknown}` : ""].filter(Boolean);
|
|
71
|
+
return `elsewhere: ${total} running (${parts.join(", ")})`;
|
|
72
|
+
}
|
|
73
|
+
/** Whether other origins have live calls: the one rule for the dock's elsewhere count and for opening the list. */
|
|
74
|
+
export const othersLive = (others) => others.some(w => liveCalls(w).length > 0);
|
|
75
|
+
/** A summary line with the elsewhere count appended when it fits; it is the first part dropped when the width is tight. */
|
|
76
|
+
function withElsewhere(line, elsewhere, width) {
|
|
77
|
+
const both = elsewhere ? `${line} · ${elsewhere}` : line;
|
|
78
|
+
const fit = visibleWidth(both) <= width ? both : line;
|
|
79
|
+
return Number.isFinite(width) ? truncateToWidth(fit, Math.max(1, width)) : fit;
|
|
80
|
+
}
|
|
37
81
|
/** UI §2: Done rows newest result first by immutable end time; ties keep snapshot order, so rows never reshuffle. */
|
|
38
82
|
export function doneOrder(calls) {
|
|
39
83
|
return calls.map((c, i) => ({ c, i })).filter(x => x.c.phase === "sealed").sort((a, b) => (b.c.endedAt ?? 0) - (a.c.endedAt ?? 0) || a.i - b.i).map(x => x.c);
|
|
@@ -162,7 +206,14 @@ export function summaryText(workflows) {
|
|
|
162
206
|
return finished ? `${finished} finished` : "nothing running";
|
|
163
207
|
return [s.asking ? `${s.asking} asking` : "", s.working ? `${s.working} working` : "", s.queued ? `${s.queued} queued` : "", s.paused ? `${s.paused} paused` : "", `${s.done}/${s.total}${s.plus ? "+" : ""} done`].filter(Boolean).join(" · ");
|
|
164
208
|
}
|
|
165
|
-
|
|
209
|
+
/** The one-line dock: this session's summary (or completion sentence), plus the elsewhere count of other origins. */
|
|
210
|
+
export function mainLine(workflows, others = [], width = Infinity) {
|
|
211
|
+
const own = ownLine(workflows), elsewhere = elsewhereText(others);
|
|
212
|
+
if (own)
|
|
213
|
+
return withElsewhere(own, elsewhere, width);
|
|
214
|
+
return elsewhere ? withElsewhere(`${elsewhere} · ↓ subagents`, undefined, width) : undefined;
|
|
215
|
+
}
|
|
216
|
+
function ownLine(workflows) {
|
|
166
217
|
if (!workflows.length)
|
|
167
218
|
return undefined;
|
|
168
219
|
const s = summary(workflows);
|
|
@@ -187,7 +238,10 @@ const SPIN = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "
|
|
|
187
238
|
/** UI §1: The dock above the editor. While agents work: one row per active agent (questions first, at most `rows`),
|
|
188
239
|
* then one summary line; afterwards only the completion sentence for ten minutes; otherwise nothing. Activity spins
|
|
189
240
|
* only while there is fresh evidence, so a quiet agent visibly stops moving. */
|
|
190
|
-
|
|
241
|
+
/** Other origins' live calls never become rows here: they add an "elsewhere" count to the summary line (alone when this
|
|
242
|
+
* session has nothing to show). */
|
|
243
|
+
export function dockLines(workflows, facts, name, width, now = Date.now(), rows = 3, others = []) {
|
|
244
|
+
const elsewhere = elsewhereText(others);
|
|
191
245
|
const live = workflows.filter(isLive);
|
|
192
246
|
const active = live.flatMap(w => {
|
|
193
247
|
const latest = new Map();
|
|
@@ -198,8 +252,10 @@ export function dockLines(workflows, facts, name, width, now = Date.now(), rows
|
|
|
198
252
|
}).sort((a, b) => Number(b.asking) - Number(a.asking));
|
|
199
253
|
if (!active.length) {
|
|
200
254
|
const ended = workflows.filter(w => !isLive(w)).sort((a, b) => (b.endedAt ?? 0) - (a.endedAt ?? 0))[0];
|
|
201
|
-
const line = ended && now - (ended.endedAt ?? 0) < 10 * 60_000 ?
|
|
202
|
-
|
|
255
|
+
const line = ended && now - (ended.endedAt ?? 0) < 10 * 60_000 ? ownLine(workflows) : undefined;
|
|
256
|
+
if (line)
|
|
257
|
+
return [withElsewhere(line, elsewhere, width)];
|
|
258
|
+
return elsewhere ? [truncateToWidth(`${elsewhere} · ↓ subagents`, Math.max(1, width))] : [];
|
|
203
259
|
}
|
|
204
260
|
const cols = { key: Math.min(14, Math.max(...active.map(a => visibleWidth(label(a.c))))), model: Math.min(20, Math.max(...active.map(a => visibleWidth(name(facts.get(a.c.callId)?.model ?? a.c.model))))) };
|
|
205
261
|
const shown = active.slice(0, rows);
|
|
@@ -211,7 +267,7 @@ export function dockLines(workflows, facts, name, width, now = Date.now(), rows
|
|
|
211
267
|
return rowText(`${mark} `, label(c), name(f?.model ?? c.model), phrase, [], width, cols);
|
|
212
268
|
});
|
|
213
269
|
const more = active.length - shown.length;
|
|
214
|
-
lines.push(
|
|
270
|
+
lines.push(withElsewhere(`${more ? `+${more} more · ` : ""}${summaryText(workflows)} · ↓ subagents`, elsewhere, width));
|
|
215
271
|
return lines;
|
|
216
272
|
}
|
|
217
273
|
/** UI §2, v12 §5: Group workflows (finished ones stay listed, dimmed and expandable — never hidden behind a toggle),
|
|
@@ -230,7 +286,9 @@ export function toggleOpen(w, state) {
|
|
|
230
286
|
state.done.set(w.wid, Math.max(8, state.done.get(w.wid) ?? 0));
|
|
231
287
|
}
|
|
232
288
|
}
|
|
233
|
-
|
|
289
|
+
/** `others` (live workflows of other origins) follow in one "Other sessions" group at the end, folded until opened;
|
|
290
|
+
* their rows are marked `other` and never feed this session's reopen-on-failure state. */
|
|
291
|
+
export function listRows(workflows, state, facts, name, width, now = Date.now(), others = []) {
|
|
234
292
|
const rows = [];
|
|
235
293
|
state.observed ??= new Map();
|
|
236
294
|
for (const w of workflows) {
|
|
@@ -249,18 +307,18 @@ export function listRows(workflows, state, facts, name, width, now = Date.now())
|
|
|
249
307
|
const unviewed = (w) => w.calls.some(c => failed(c) && !state.viewed.has(c.callId));
|
|
250
308
|
const visible = workflows; // caller order (newest first), unchanged when a workflow finishes
|
|
251
309
|
// Aligned columns across the whole list (UI §2): key and model start at the same column on every row.
|
|
252
|
-
const shownCalls = visible.flatMap(w => w.calls);
|
|
310
|
+
const shownCalls = [...visible.flatMap(w => w.calls), ...(state.others ? others.flatMap(liveCalls) : [])];
|
|
253
311
|
const cols = { key: Math.min(18, Math.max(0, ...shownCalls.map(c => visibleWidth(label(c))))),
|
|
254
312
|
model: Math.min(26, Math.max(0, ...shownCalls.map(c => visibleWidth(name(facts.get(c.callId)?.model ?? c.model))))) };
|
|
255
|
-
const callRow = (w, c, indent, preview, dim = false) => {
|
|
313
|
+
const callRow = (w, c, indent, preview, dim = false, other = false) => {
|
|
256
314
|
const f = facts.get(c.callId), age = c.phase === "sealed" ? `${duration(now - (c.endedAt ?? now))} ago` : c.startedAt ? duration(now - c.startedAt) : "";
|
|
257
315
|
// A requested switch shows at once (it applies when the current step ends; until then it would look as if it had failed).
|
|
258
316
|
const shown = c.switching ? `${name(f?.model ?? c.model)} → ${name(c.switching)}` : name(f?.model ?? c.model);
|
|
259
317
|
const text = rowText(indent, label(c), shown, statusPhrase(c, w, f, now), [pendingMarker(c.pending), toolCount(f?.tools), age], width, cols);
|
|
260
|
-
rows.push({ id: c.callId, kind: "call", workflow: w, call: c, failed: Boolean(failed(c)), dim, text });
|
|
318
|
+
rows.push({ id: c.callId, kind: "call", workflow: w, call: c, failed: Boolean(failed(c)), dim, text, ...(other ? { other } : {}) });
|
|
261
319
|
// Overview (UI §2): every active agent shows what it last said, thought or saw, without opening it.
|
|
262
320
|
if (preview !== undefined && c.phase !== "sealed" && f?.latest)
|
|
263
|
-
rows.push({ id: `${c.callId}:preview`, kind: "preview", workflow: w, call: c, dim, text: truncateToWidth(`${preview}${f.latest}`, Math.max(1, width)).replaceAll("\x1b[0m", "") });
|
|
321
|
+
rows.push({ id: `${c.callId}:preview`, kind: "preview", workflow: w, call: c, dim, text: truncateToWidth(`${preview}${f.latest}`, Math.max(1, width)).replaceAll("\x1b[0m", ""), ...(other ? { other } : {}) });
|
|
264
322
|
};
|
|
265
323
|
for (const v of visible) {
|
|
266
324
|
// One row per key: the newest generation (key@2) stands for the agent; older ones open from its watch view (← →).
|
|
@@ -276,7 +334,10 @@ export function listRows(workflows, state, facts, name, width, now = Date.now())
|
|
|
276
334
|
// Stable order (UI §2): active rows keep proposal (snapshot) order; done rows by immutable end time.
|
|
277
335
|
const done = doneOrder(w.calls), active = liveCalls(w), folded = !isOpen(w, state);
|
|
278
336
|
const progress = progressOf(w); // v12 §4: done/planned, `n+` while a script workflow keeps proposing
|
|
279
|
-
|
|
337
|
+
const head = `${folded ? "▸" : "▾"} ${w.name ?? w.wid} · ${progress.done}/${progress.total}${progress.plus ? "+" : ""} · ${duration((w.endedAt ?? now) - (w.startedAt ?? now))}`;
|
|
338
|
+
// The run's labels follow when the row has room for a useful part of them (clipped to at most 60 characters).
|
|
339
|
+
const room = Math.min(60, width - visibleWidth(head) - 3);
|
|
340
|
+
rows.push({ id: w.wid, kind: "workflow", workflow: w, dim, text: w.labels && room >= 12 ? `${head} · ${labelsText(w.labels, room)}` : head });
|
|
280
341
|
if (folded)
|
|
281
342
|
continue;
|
|
282
343
|
if (dim) { // a finished workflow lists its agents directly (no nested "done" node)
|
|
@@ -293,5 +354,24 @@ export function listRows(workflows, state, facts, name, width, now = Date.now())
|
|
|
293
354
|
if (more)
|
|
294
355
|
rows.push({ id: `${w.wid}:more`, kind: "more", workflow: w, dim, text: ` └ … ${done.length - count} more` });
|
|
295
356
|
}
|
|
357
|
+
if (!others.length)
|
|
358
|
+
return rows;
|
|
359
|
+
// Other sessions: one folded group; inside, each workflow (wid, name, origin, labels) and its live calls only.
|
|
360
|
+
const running = others.reduce((n, w) => n + liveCalls(w).length, 0);
|
|
361
|
+
rows.push({ id: "others", kind: "others", text: `${state.others ? "▾" : "▸"} Other sessions (${plural(others.length, "workflow")}, ${running} running)` });
|
|
362
|
+
if (!state.others)
|
|
363
|
+
return rows;
|
|
364
|
+
for (const w of others) {
|
|
365
|
+
const folded = state.folded.has(w.wid), open = liveCalls(w), latest = new Map();
|
|
366
|
+
for (const c of open)
|
|
367
|
+
latest.set(c.key, c);
|
|
368
|
+
const calls = [...latest.values()];
|
|
369
|
+
const head = ` ${folded ? "▸" : "▾"} ${[w.wid, w.name, originText(w.origin)].filter(Boolean).join(" · ")}`;
|
|
370
|
+
const room = Math.min(60, width - visibleWidth(head) - 3);
|
|
371
|
+
rows.push({ id: w.wid, kind: "workflow", workflow: w, other: true, dim: !calls.length, text: w.labels && room >= 12 ? `${head} · ${labelsText(w.labels, room)}` : head });
|
|
372
|
+
if (folded)
|
|
373
|
+
continue;
|
|
374
|
+
calls.forEach((c, i) => callRow(w, c, i === calls.length - 1 ? " └ " : " ├ ", undefined, false, true));
|
|
375
|
+
}
|
|
296
376
|
return rows;
|
|
297
377
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-durable-subagents",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.29",
|
|
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",
|