@restranger/repi-subagents 0.0.0-stage → 0.0.1-92401bd
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/README.md +284 -2
- package/package.json +41 -5
- package/src/agent-file.ts +297 -0
- package/src/agent-schema.ts +90 -0
- package/src/child-guard.ts +151 -0
- package/src/command.ts +255 -0
- package/src/default-agents.ts +409 -0
- package/src/discovery.ts +354 -0
- package/src/extensions.ts +119 -0
- package/src/fleet-ui.ts +617 -0
- package/src/format.ts +974 -0
- package/src/frontmatter.ts +302 -0
- package/src/index.ts +112 -0
- package/src/manager.ts +1192 -0
- package/src/model-resolver.ts +58 -0
- package/src/overrides.ts +693 -0
- package/src/plugin.ts +568 -0
- package/src/prompt.ts +184 -0
- package/src/registry.ts +71 -0
- package/src/session.ts +304 -0
- package/src/status-widget.ts +65 -0
- package/src/tool-policy.ts +77 -0
- package/src/tools.ts +554 -0
- package/src/types.ts +163 -0
package/src/fleet-ui.ts
ADDED
|
@@ -0,0 +1,617 @@
|
|
|
1
|
+
import { ListOverlay, mouseWheelDirection } from "@restranger/repi-ui";
|
|
2
|
+
import {
|
|
3
|
+
alignRight,
|
|
4
|
+
boldText,
|
|
5
|
+
displayWidth,
|
|
6
|
+
fitWidth,
|
|
7
|
+
formatActivity,
|
|
8
|
+
formatFleetElapsed,
|
|
9
|
+
paintIdentity,
|
|
10
|
+
sanitize,
|
|
11
|
+
statusColor,
|
|
12
|
+
truncate,
|
|
13
|
+
wrapDisplay,
|
|
14
|
+
} from "./format.js";
|
|
15
|
+
import type { SubagentRunSnapshot } from "./types.js";
|
|
16
|
+
|
|
17
|
+
export interface FleetViewOptions {
|
|
18
|
+
/** Always read fresh state; the manager owns the run table. */
|
|
19
|
+
readonly runs: () => readonly SubagentRunSnapshot[];
|
|
20
|
+
readonly now?: () => number;
|
|
21
|
+
readonly onCancel?: (id: string) => void;
|
|
22
|
+
/** Rows the component may occupy; defaults to the attached terminal height. */
|
|
23
|
+
readonly maxRows?: number;
|
|
24
|
+
/** Initial roster selection, used when the widget opens the inspector. */
|
|
25
|
+
readonly initialId?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Two panes plus the divider need room; below this the inspector degrades to a notice. */
|
|
29
|
+
const MIN_WIDTH = 48;
|
|
30
|
+
/** Below this the roster and the detail pane cannot both show a meaningful body. */
|
|
31
|
+
const MIN_ROWS = 10;
|
|
32
|
+
/** Top rule, title, subtitle, header rule, footer rule, hints, bottom rule. */
|
|
33
|
+
const FRAME_ROWS = 7;
|
|
34
|
+
const ROSTER_MIN_WIDTH = 24;
|
|
35
|
+
const ROSTER_MAX_WIDTH = 44;
|
|
36
|
+
const ROSTER_RATIO = 0.34;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Memoized detail pane. Sanitizing, truncating and wrapping the whole transcript on every animation
|
|
40
|
+
* tick (12.5 Hz) froze the host on large child results, so the wrapped body is only rebuilt when one
|
|
41
|
+
* of its inputs actually changes.
|
|
42
|
+
*/
|
|
43
|
+
interface DetailCache {
|
|
44
|
+
readonly runId: string;
|
|
45
|
+
readonly width: number;
|
|
46
|
+
readonly showTools: boolean;
|
|
47
|
+
readonly pendingStop?: string;
|
|
48
|
+
readonly notice?: string;
|
|
49
|
+
readonly status: string;
|
|
50
|
+
readonly steps: number;
|
|
51
|
+
readonly activity: string;
|
|
52
|
+
readonly result?: string;
|
|
53
|
+
readonly error?: string;
|
|
54
|
+
readonly wrapped: string[];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Full-screen fleet inspector for `/subagents fleet`.
|
|
59
|
+
*
|
|
60
|
+
* The layout is a native Pi frame — `─` rules in the border colour, an accent+bold title, muted
|
|
61
|
+
* metadata, `→` list rows and dim/muted key hints — split into two panes by a single `│` divider
|
|
62
|
+
* stitched into the rules with `┬`/`┴` joints. There are no box corners and no title bar; the
|
|
63
|
+
* component fills the whole terminal and renders exactly `terminal.rows` lines so the host overlay
|
|
64
|
+
* never has to clip it:
|
|
65
|
+
*
|
|
66
|
+
* ```text
|
|
67
|
+
* ──────────────────────────────────────────────────────────────────────────
|
|
68
|
+
* Subagent fleet ● scout · running · 12s
|
|
69
|
+
* 3 runs · 1 running · 1 queued · 1 completed
|
|
70
|
+
* ────────────────────────────────────┬─────────────────────────────────────
|
|
71
|
+
* → ● scout running │ ● scout running
|
|
72
|
+
* ◦ worker queued │ background · a1b2c3d4 · single
|
|
73
|
+
* ✓ review completed │ openai/gpt-5 · 4.2k tok · 12 tools · 1m
|
|
74
|
+
* │ Task: inspect the parser
|
|
75
|
+
* │
|
|
76
|
+
* │ Conversation · running
|
|
77
|
+
* │ ↳ bash bun run test (2.1s)
|
|
78
|
+
* │ ✓ read src/app.ts (12ms)
|
|
79
|
+
* ────────────────────────────────────┴─────────────────────────────────────
|
|
80
|
+
* ↑↓ agent Enter tools D stop K/J scroll r refresh Esc close 1/3
|
|
81
|
+
* ──────────────────────────────────────────────────────────────────────────
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* The component only needs the structural theme/TUI contract, so it works in tests and in Pi
|
|
85
|
+
* without importing host packages.
|
|
86
|
+
*/
|
|
87
|
+
export class FleetView extends ListOverlay {
|
|
88
|
+
private showTools = false;
|
|
89
|
+
private pendingStop?: string;
|
|
90
|
+
private notice?: string;
|
|
91
|
+
private detailCache?: DetailCache;
|
|
92
|
+
/** Visible transcript rows from the last render, used by PageUp/PageDown. */
|
|
93
|
+
private lastVisibleRows = 1;
|
|
94
|
+
/** Wrapped transcript lines by source line, so a streaming result only re-wraps what changed. */
|
|
95
|
+
private readonly lineCache = new Map<string, string[]>();
|
|
96
|
+
|
|
97
|
+
constructor(private readonly options: FleetViewOptions) {
|
|
98
|
+
super({ maxRows: options.maxRows });
|
|
99
|
+
const initial = options.initialId ? options.runs().findIndex((run) => run.id === options.initialId) : -1;
|
|
100
|
+
if (initial >= 0) {
|
|
101
|
+
this.selected = initial;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
protected itemCount(): number {
|
|
106
|
+
return this.options.runs().length;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Changing the selected run cancels a pending stop confirmation. */
|
|
110
|
+
protected onSelectionChanged(): void {
|
|
111
|
+
this.pendingStop = undefined;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** PageUp/PageDown scroll the transcript by one visible pane, not by the 85% budget. */
|
|
115
|
+
protected bodyRows(): number {
|
|
116
|
+
return this.lastVisibleRows;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
handleInput(data: string): void {
|
|
120
|
+
const runs = this.options.runs();
|
|
121
|
+
const current = runs[this.selected];
|
|
122
|
+
|
|
123
|
+
if (this.pendingStop) {
|
|
124
|
+
if (data.toLowerCase() === "y" || data === "\r" || data === "\n" || this.matches(data, "tui.select.confirm")) {
|
|
125
|
+
const id = this.pendingStop;
|
|
126
|
+
this.pendingStop = undefined;
|
|
127
|
+
this.notice = undefined;
|
|
128
|
+
try {
|
|
129
|
+
this.options.onCancel?.(id);
|
|
130
|
+
} catch {
|
|
131
|
+
// Cancellation is best-effort; the next manager change repairs the row.
|
|
132
|
+
}
|
|
133
|
+
this.tui?.requestRender();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (data.toLowerCase() === "n" || data === "\x1b" || this.matches(data, "tui.select.cancel")) {
|
|
137
|
+
this.pendingStop = undefined;
|
|
138
|
+
this.tui?.requestRender();
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (data === "q" || this.matches(data, "tui.select.cancel")) {
|
|
145
|
+
this.close();
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const wheel = mouseWheelDirection(data);
|
|
149
|
+
if (wheel !== undefined) {
|
|
150
|
+
this.move(wheel);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (this.matches(data, "tui.select.up") || data === "\x1b[A" || data === "k") {
|
|
154
|
+
this.move(-1);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (this.matches(data, "tui.select.down") || data === "\x1b[B" || data === "j") {
|
|
158
|
+
this.move(1);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (data === "\x1b[H" || data === "g") {
|
|
162
|
+
this.select(0);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (data === "\x1b[F" || data === "G") {
|
|
166
|
+
this.select(runs.length - 1);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (data === "K") {
|
|
170
|
+
this.scrollDetail(-1);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (data === "J") {
|
|
174
|
+
this.scrollDetail(1);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (data === "\x1b[5~" || this.matches(data, "tui.select.pageUp")) {
|
|
178
|
+
this.scrollDetail(-this.bodyRows());
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (data === "\x1b[6~" || this.matches(data, "tui.select.pageDown")) {
|
|
182
|
+
this.scrollDetail(this.bodyRows());
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (data === "r" || data === "R") {
|
|
186
|
+
this.refresh();
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (data === "x" || data === "X" || data === "\x0f") {
|
|
190
|
+
this.showTools = !this.showTools;
|
|
191
|
+
this.detailScroll = 0;
|
|
192
|
+
this.tui?.requestRender();
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (data === "\r" || data === "\n" || this.matches(data, "tui.select.confirm")) {
|
|
196
|
+
this.showTools = !this.showTools;
|
|
197
|
+
this.detailScroll = 0;
|
|
198
|
+
this.tui?.requestRender();
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (data === "D") {
|
|
202
|
+
if (current && (current.status === "running" || current.status === "queued")) {
|
|
203
|
+
this.pendingStop = current.id;
|
|
204
|
+
this.notice = undefined;
|
|
205
|
+
this.detailScroll = 0;
|
|
206
|
+
this.tui?.requestRender();
|
|
207
|
+
}
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
render(width: number): string[] {
|
|
213
|
+
const runs = this.options.runs();
|
|
214
|
+
this.clampSelection();
|
|
215
|
+
this.clearStaleStop(runs);
|
|
216
|
+
const terminalWidth = Math.max(1, Math.floor(width));
|
|
217
|
+
const lines = this.renderFrame(runs, terminalWidth);
|
|
218
|
+
return lines.map((line) => truncate(line, terminalWidth));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private renderFrame(runs: readonly SubagentRunSnapshot[], width: number): string[] {
|
|
222
|
+
if (width < MIN_WIDTH) {
|
|
223
|
+
return [truncate(`Subagent fleet needs at least ${MIN_WIDTH} columns. Esc closes.`, width)];
|
|
224
|
+
}
|
|
225
|
+
const rows = this.rowsBudget();
|
|
226
|
+
if (rows < MIN_ROWS) {
|
|
227
|
+
return [truncate(`Subagent fleet needs at least ${MIN_ROWS} terminal rows. Esc closes.`, width)];
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const bodyHeight = Math.max(1, rows - FRAME_ROWS);
|
|
231
|
+
const selected = runs[this.selected];
|
|
232
|
+
const body =
|
|
233
|
+
runs.length === 0 ? this.emptyBody(width, bodyHeight) : this.paneBody(runs, selected, width, bodyHeight);
|
|
234
|
+
// Where the divider meets the rules: a `┬` above the body and a `┴` below it stitch the panes together.
|
|
235
|
+
const joint = runs.length === 0 ? undefined : this.rosterWidth(width) + 1;
|
|
236
|
+
|
|
237
|
+
return [
|
|
238
|
+
this.rule(width),
|
|
239
|
+
this.titleLine(selected, width),
|
|
240
|
+
this.subtitleLine(runs, width),
|
|
241
|
+
this.rule(width, joint, "┬"),
|
|
242
|
+
...body,
|
|
243
|
+
this.rule(width, joint, "┴"),
|
|
244
|
+
this.hintsLine(runs, width),
|
|
245
|
+
this.rule(width),
|
|
246
|
+
];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Two panes: the roster on the left, the selected run on the right, one divider column between. */
|
|
250
|
+
private paneBody(
|
|
251
|
+
runs: readonly SubagentRunSnapshot[],
|
|
252
|
+
selected: SubagentRunSnapshot | undefined,
|
|
253
|
+
width: number,
|
|
254
|
+
bodyHeight: number,
|
|
255
|
+
): string[] {
|
|
256
|
+
const rosterWidth = this.rosterWidth(width);
|
|
257
|
+
const detailWidth = Math.max(1, width - rosterWidth - 3);
|
|
258
|
+
const header = this.detailHeader(selected, detailWidth).slice(0, Math.max(0, bodyHeight - 1));
|
|
259
|
+
const visibleRows = Math.max(1, bodyHeight - header.length);
|
|
260
|
+
this.lastVisibleRows = visibleRows;
|
|
261
|
+
const wrapped = this.detailWrapped(selected, detailWidth);
|
|
262
|
+
const detail = [...header, ...this.windowBody(wrapped, visibleRows)];
|
|
263
|
+
const roster = this.rosterLines(runs, rosterWidth, bodyHeight);
|
|
264
|
+
const divider = this.border("│");
|
|
265
|
+
|
|
266
|
+
const lines: string[] = [];
|
|
267
|
+
for (let index = 0; index < bodyHeight; index++) {
|
|
268
|
+
lines.push(
|
|
269
|
+
`${fitWidth(roster[index] ?? "", rosterWidth)} ${divider} ${fitWidth(detail[index] ?? "", detailWidth)}`,
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
return lines;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** Empty state: a centered message instead of an empty roster pane and a blank transcript. */
|
|
276
|
+
private emptyBody(width: number, bodyHeight: number): string[] {
|
|
277
|
+
this.lastVisibleRows = bodyHeight;
|
|
278
|
+
const message = this.paint("muted", "No tracked children");
|
|
279
|
+
const hint = this.paint("dim", "Background runs appear here automatically.");
|
|
280
|
+
const block = ["", message, "", hint, ""];
|
|
281
|
+
const top = Math.max(0, Math.floor((bodyHeight - block.length) / 2));
|
|
282
|
+
const lines: string[] = Array.from({ length: top }, () => "");
|
|
283
|
+
for (const line of block) {
|
|
284
|
+
lines.push(this.center(line, width));
|
|
285
|
+
}
|
|
286
|
+
while (lines.length < bodyHeight) {
|
|
287
|
+
lines.push("");
|
|
288
|
+
}
|
|
289
|
+
return lines.slice(0, bodyHeight);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
private center(text: string, width: number): string {
|
|
293
|
+
if (!text) {
|
|
294
|
+
return "";
|
|
295
|
+
}
|
|
296
|
+
const pad = Math.max(0, Math.floor((width - displayWidth(text)) / 2));
|
|
297
|
+
return `${" ".repeat(pad)}${text}`;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
private rule(width: number, joint?: number, glyph = "─"): string {
|
|
301
|
+
const size = Math.max(0, width);
|
|
302
|
+
if (joint === undefined || joint < 0 || joint >= size) {
|
|
303
|
+
return this.border("─".repeat(size));
|
|
304
|
+
}
|
|
305
|
+
return this.border(`${"─".repeat(joint)}${glyph}${"─".repeat(Math.max(0, size - joint - 1))}`);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
private rosterWidth(width: number): number {
|
|
309
|
+
return Math.max(ROSTER_MIN_WIDTH, Math.min(ROSTER_MAX_WIDTH, Math.floor(width * ROSTER_RATIO)));
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private titleLine(run: SubagentRunSnapshot | undefined, width: number): string {
|
|
313
|
+
const title = ` ${boldText(this.theme, this.paint("accent", "Subagent fleet"))}`;
|
|
314
|
+
if (!run) {
|
|
315
|
+
return alignRight(title, this.paint("dim", "no children "), width);
|
|
316
|
+
}
|
|
317
|
+
const now = this.options.now?.() ?? Date.now();
|
|
318
|
+
const elapsed = formatFleetElapsed(Math.max(0, (run.finishedAt ?? now) - (run.startedAt ?? run.createdAt)));
|
|
319
|
+
const right = `${this.glyph(run)} ${paintIdentity(run.agent, this.theme)} ${this.paint("dim", `· ${run.status} · ${elapsed}`)} `;
|
|
320
|
+
return alignRight(title, right, width);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private subtitleLine(runs: readonly SubagentRunSnapshot[], width: number): string {
|
|
324
|
+
if (runs.length === 0) {
|
|
325
|
+
return truncate(` ${this.paint("dim", "0 runs · waiting for a background subagent")}`, width);
|
|
326
|
+
}
|
|
327
|
+
const counts = new Map<string, number>();
|
|
328
|
+
for (const run of runs) {
|
|
329
|
+
counts.set(run.status, (counts.get(run.status) ?? 0) + 1);
|
|
330
|
+
}
|
|
331
|
+
const parts = [`${runs.length} run${runs.length === 1 ? "" : "s"}`];
|
|
332
|
+
for (const status of ["running", "queued", "completed", "failed", "aborted"]) {
|
|
333
|
+
const count = counts.get(status);
|
|
334
|
+
if (count) {
|
|
335
|
+
parts.push(`${count} ${status}`);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return truncate(` ${this.paint("dim", parts.join(" · "))}`, width);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
private hintsLine(runs: readonly SubagentRunSnapshot[], width: number): string {
|
|
342
|
+
if (this.pendingStop) {
|
|
343
|
+
return truncate(
|
|
344
|
+
` ${this.paint("warning", `Stop ${sanitize(this.pendingStop)}? Enter/Y confirm · N/Esc cancel`)} `,
|
|
345
|
+
width,
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
const close = this.hintLabel("tui.select.cancel", "Esc");
|
|
349
|
+
if (runs.length === 0) {
|
|
350
|
+
return truncate(` ${this.hint("r", "refresh")} ${this.hint(close, "close")}`, width);
|
|
351
|
+
}
|
|
352
|
+
const up = this.primaryKey("tui.select.up", "↑");
|
|
353
|
+
const down = this.primaryKey("tui.select.down", "↓");
|
|
354
|
+
const confirm = this.hintLabel("tui.select.confirm", "Enter");
|
|
355
|
+
// Pi's own selector prints the arrow pair as `↑↓`; a remapped pair keeps the `p/n` shape.
|
|
356
|
+
const navigation = up === "↑" && down === "↓" ? "↑↓" : `${up}/${down}`;
|
|
357
|
+
const hints = [
|
|
358
|
+
this.hint(navigation, "agent"),
|
|
359
|
+
this.hint(confirm, "tools"),
|
|
360
|
+
this.hint("D", "stop"),
|
|
361
|
+
this.hint("K/J", "scroll"),
|
|
362
|
+
this.hint("r", "refresh"),
|
|
363
|
+
this.hint(close, "close"),
|
|
364
|
+
].join(" ");
|
|
365
|
+
const position = `${this.selected + 1}/${runs.length}`;
|
|
366
|
+
return alignRight(` ${hints}`, this.paint("dim", `${position} `), width);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
private hint(keys: string, description: string): string {
|
|
370
|
+
return `${this.paint("dim", keys)}${this.paint("muted", ` ${description}`)}`;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** First key bound to an action: hints stay short even when Pi binds several keys. */
|
|
374
|
+
private primaryKey(key: string, fallback: string): string {
|
|
375
|
+
const first = this.keyLabel(key, fallback).split("/")[0];
|
|
376
|
+
return first && first.length > 0 ? first : fallback;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** `keyLabel`, capitalised the way Pi's own `keyDisplayText` does it (`ctrl+c` → `Ctrl+c`). */
|
|
380
|
+
private hintLabel(key: string, fallback: string): string {
|
|
381
|
+
const primary = this.primaryKey(key, fallback);
|
|
382
|
+
return primary.replace(
|
|
383
|
+
/(^|\+)([a-z])/g,
|
|
384
|
+
(_match, prefix: string, char: string) => `${prefix}${char.toUpperCase()}`,
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
private rosterLines(runs: readonly SubagentRunSnapshot[], width: number, bodyHeight: number): string[] {
|
|
389
|
+
if (runs.length === 0) {
|
|
390
|
+
return [this.paint("dim", "No tracked children")];
|
|
391
|
+
}
|
|
392
|
+
const start = Math.max(
|
|
393
|
+
0,
|
|
394
|
+
Math.min(this.selected - Math.floor(bodyHeight / 2), Math.max(0, runs.length - bodyHeight)),
|
|
395
|
+
);
|
|
396
|
+
const lines: string[] = [];
|
|
397
|
+
for (let offset = 0; offset < bodyHeight; offset++) {
|
|
398
|
+
const index = start + offset;
|
|
399
|
+
const run = runs[index];
|
|
400
|
+
if (!run) {
|
|
401
|
+
lines.push("");
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
const selected = index === this.selected;
|
|
405
|
+
const cursor = selected ? this.paint("accent", "→ ") : " ";
|
|
406
|
+
const label = selected
|
|
407
|
+
? boldText(this.theme, this.paint("accent", sanitize(run.agent)))
|
|
408
|
+
: paintIdentity(run.agent, this.theme);
|
|
409
|
+
const status = this.paint(statusColor(run.status), sanitize(run.status));
|
|
410
|
+
lines.push(alignRight(`${cursor}${this.glyph(run)} ${label}`, status, width));
|
|
411
|
+
}
|
|
412
|
+
return lines;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** The host asks for a fresh frame when a child pushes an update; drop the memoized body. */
|
|
416
|
+
invalidate(): void {
|
|
417
|
+
this.detailCache = undefined;
|
|
418
|
+
this.lineCache.clear();
|
|
419
|
+
super.invalidate();
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Sanitize + wrap one source line, memoized.
|
|
424
|
+
*
|
|
425
|
+
* A streaming result keeps every line but the last unchanged, so without this cache the whole
|
|
426
|
+
* transcript was sanitized and wrapped again on every animation tick (12.5 Hz), freezing the host.
|
|
427
|
+
*/
|
|
428
|
+
private wrapCached(line: string, width: number): string[] {
|
|
429
|
+
const key = `${width}\u0000${line}`;
|
|
430
|
+
const cached = this.lineCache.get(key);
|
|
431
|
+
if (cached) {
|
|
432
|
+
return cached;
|
|
433
|
+
}
|
|
434
|
+
const wrapped = wrapDisplay(sanitize(line), width);
|
|
435
|
+
if (this.lineCache.size > 4000) {
|
|
436
|
+
this.lineCache.clear();
|
|
437
|
+
}
|
|
438
|
+
this.lineCache.set(key, wrapped);
|
|
439
|
+
return wrapped;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** Wrapped transcript for the detail pane; rebuilt only when an input changed. */
|
|
443
|
+
private detailWrapped(run: SubagentRunSnapshot | undefined, width: number): string[] {
|
|
444
|
+
const cache = this.detailCache;
|
|
445
|
+
const activity = run?.activity ? `${run.activity.tool}|${run.activity.detail ?? ""}` : "";
|
|
446
|
+
if (
|
|
447
|
+
cache &&
|
|
448
|
+
run &&
|
|
449
|
+
cache.runId === run.id &&
|
|
450
|
+
cache.width === width &&
|
|
451
|
+
cache.showTools === this.showTools &&
|
|
452
|
+
cache.pendingStop === this.pendingStop &&
|
|
453
|
+
cache.notice === this.notice &&
|
|
454
|
+
cache.status === run.status &&
|
|
455
|
+
cache.steps === (run.steps?.length ?? 0) &&
|
|
456
|
+
cache.activity === activity &&
|
|
457
|
+
cache.result === run.result &&
|
|
458
|
+
cache.error === run.error
|
|
459
|
+
) {
|
|
460
|
+
return cache.wrapped;
|
|
461
|
+
}
|
|
462
|
+
const wrapped = this.detailBody(run).flatMap((line) => this.wrapCached(line, Math.max(1, width)));
|
|
463
|
+
this.detailCache = {
|
|
464
|
+
runId: run?.id ?? "",
|
|
465
|
+
width,
|
|
466
|
+
showTools: this.showTools,
|
|
467
|
+
pendingStop: this.pendingStop,
|
|
468
|
+
notice: this.notice,
|
|
469
|
+
status: run?.status ?? "",
|
|
470
|
+
steps: run?.steps?.length ?? 0,
|
|
471
|
+
activity,
|
|
472
|
+
result: run?.result,
|
|
473
|
+
error: run?.error,
|
|
474
|
+
wrapped,
|
|
475
|
+
};
|
|
476
|
+
return wrapped;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
private detailHeader(run: SubagentRunSnapshot | undefined, width: number): string[] {
|
|
480
|
+
if (!run) {
|
|
481
|
+
return [];
|
|
482
|
+
}
|
|
483
|
+
const now = this.options.now?.() ?? Date.now();
|
|
484
|
+
const source = run.background
|
|
485
|
+
? "background"
|
|
486
|
+
: run.status === "running" || run.status === "queued"
|
|
487
|
+
? "foreground · live"
|
|
488
|
+
: "foreground · recent";
|
|
489
|
+
const identity = `${source} · ${sanitize(run.id).slice(0, 8)} · single`;
|
|
490
|
+
const stats = this.stats(run, now);
|
|
491
|
+
const summary = run.task.replace(/\s+/g, " ").trim();
|
|
492
|
+
return [
|
|
493
|
+
` ${this.glyph(run)} ${boldText(this.theme, paintIdentity(run.agent, this.theme))} ${this.paint("dim", "·")} ${this.paint(statusColor(run.status), sanitize(run.status))}`,
|
|
494
|
+
` ${this.paint("dim", identity)}`,
|
|
495
|
+
` ${this.paint("muted", stats.join(" · "))}`,
|
|
496
|
+
...(summary
|
|
497
|
+
? [` ${this.paint("muted", `Task: ${truncate(summary, Math.min(Math.max(8, width - 8), 120))}`)}`]
|
|
498
|
+
: []),
|
|
499
|
+
"",
|
|
500
|
+
`${this.paint("accent", "Conversation")} ${this.paint("dim", `· ${sanitize(run.status)}`)}`,
|
|
501
|
+
].map((line) => truncate(line, width));
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/** Logical (unwrapped) transcript lines for the detail pane. */
|
|
505
|
+
private detailBody(run: SubagentRunSnapshot | undefined): string[] {
|
|
506
|
+
if (!run) {
|
|
507
|
+
return [
|
|
508
|
+
"No current-session Fleet jobs.",
|
|
509
|
+
"",
|
|
510
|
+
"New jobs appear here automatically while this inspector remains open.",
|
|
511
|
+
];
|
|
512
|
+
}
|
|
513
|
+
const now = this.options.now?.() ?? Date.now();
|
|
514
|
+
const body: string[] = [];
|
|
515
|
+
if (this.pendingStop === run.id) {
|
|
516
|
+
body.push(
|
|
517
|
+
this.paint("warning", `Confirm stop for async run ${sanitize(run.id)}?`),
|
|
518
|
+
this.paint("dim", "Stop ends the run. Enter/Y confirms · N returns · Esc cancels"),
|
|
519
|
+
"",
|
|
520
|
+
);
|
|
521
|
+
} else if (this.notice) {
|
|
522
|
+
body.push(this.paint("dim", this.notice), "");
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
const activity = run.status === "running" ? formatActivity(run.activity, now) : undefined;
|
|
526
|
+
if (activity) {
|
|
527
|
+
body.push(` ${activity}`);
|
|
528
|
+
}
|
|
529
|
+
if (this.showTools) {
|
|
530
|
+
for (const step of (run.steps ?? []).slice(-200)) {
|
|
531
|
+
const detail = step.detail ? ` ${sanitize(step.detail)}` : "";
|
|
532
|
+
const duration = step.durationMs !== undefined ? ` (${formatFleetElapsed(step.durationMs)})` : "";
|
|
533
|
+
const icon = step.isError ? "✗" : step.finishedAt !== undefined ? "✓" : "↳";
|
|
534
|
+
body.push(` ${icon} ${sanitize(step.tool)}${detail}${duration}`);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
if (run.error) {
|
|
538
|
+
body.push(
|
|
539
|
+
this.paint("error", "Error:"),
|
|
540
|
+
...sanitize(run.error)
|
|
541
|
+
.split("\n")
|
|
542
|
+
.slice(0, 40)
|
|
543
|
+
.map((line) => ` ${line}`),
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
if (run.result) {
|
|
547
|
+
const resultLines = run.result.split("\n");
|
|
548
|
+
body.push(this.paint("muted", "Output:"));
|
|
549
|
+
// Sanitizing and wrapping happen per line in `wrapCached`, where unchanged lines are reused.
|
|
550
|
+
body.push(...resultLines.slice(0, 400).map((line) => ` ${line.slice(0, 8_000)}`));
|
|
551
|
+
if (resultLines.length > 400) {
|
|
552
|
+
body.push(` ${this.paint("dim", `… ${resultLines.length - 400} more lines`)}`);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
if (body.length === 0) {
|
|
556
|
+
body.push(
|
|
557
|
+
this.paint("dim", run.status === "running" ? "Waiting for the first transcript event…" : "(no output)"),
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
return body;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
private stats(run: SubagentRunSnapshot, now: number): string[] {
|
|
564
|
+
const tokens = run.usage?.total ?? ((run.usage?.input ?? 0) + (run.usage?.output ?? 0) || undefined);
|
|
565
|
+
const stats = [
|
|
566
|
+
run.model ? sanitize(run.model) : undefined,
|
|
567
|
+
tokens !== undefined ? `${formatTokenValue(tokens)} tok` : undefined,
|
|
568
|
+
`${run.toolCalls} tool${run.toolCalls === 1 ? "" : "s"}`,
|
|
569
|
+
formatStatDuration(Math.max(0, (run.finishedAt ?? now) - (run.startedAt ?? run.createdAt))),
|
|
570
|
+
];
|
|
571
|
+
return stats.filter((value): value is string => Boolean(value));
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
private clearStaleStop(runs: readonly SubagentRunSnapshot[]): void {
|
|
575
|
+
if (!this.pendingStop) {
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
const run = runs.find((candidate) => candidate.id === this.pendingStop);
|
|
579
|
+
if (!run || (run.status !== "running" && run.status !== "queued")) {
|
|
580
|
+
this.pendingStop = undefined;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
private glyph(run: SubagentRunSnapshot): string {
|
|
585
|
+
const glyph =
|
|
586
|
+
run.status === "running"
|
|
587
|
+
? "●"
|
|
588
|
+
: run.status === "queued"
|
|
589
|
+
? "◦"
|
|
590
|
+
: run.status === "completed"
|
|
591
|
+
? "✓"
|
|
592
|
+
: run.status === "aborted"
|
|
593
|
+
? "■"
|
|
594
|
+
: "✗";
|
|
595
|
+
return this.paint(statusColor(run.status), glyph);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
function formatTokenValue(total: number): string {
|
|
600
|
+
if (total < 1_000) {
|
|
601
|
+
return `${total}`;
|
|
602
|
+
}
|
|
603
|
+
if (total < 1_000_000) {
|
|
604
|
+
return `${(total / 1_000).toFixed(total < 10_000 ? 1 : 0)}k`;
|
|
605
|
+
}
|
|
606
|
+
return `${(total / 1_000_000).toFixed(1)}M`;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
function formatStatDuration(ms: number): string {
|
|
610
|
+
if (ms < 1_000) {
|
|
611
|
+
return `${Math.round(ms)}ms`;
|
|
612
|
+
}
|
|
613
|
+
if (ms < 60_000) {
|
|
614
|
+
return `${(ms / 1_000).toFixed(1)}s`;
|
|
615
|
+
}
|
|
616
|
+
return `${Math.floor(ms / 60_000)}m${Math.floor((ms % 60_000) / 1_000)}s`;
|
|
617
|
+
}
|