@maheidem/pi-delegate 0.1.5 → 0.2.1
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 +98 -1
- package/application.ts +54 -4
- package/commands.ts +21 -2
- package/config.ts +26 -3
- package/handoff.ts +265 -0
- package/index.ts +212 -16
- package/package.json +69 -66
- package/roles/general.md +24 -18
- package/roles/research.md +9 -19
- package/rpc-jsonl.ts +18 -2
- package/runner.ts +534 -22
- package/transcript-feed.ts +234 -0
- package/types.ts +38 -0
- package/ui/peek-view.ts +108 -0
- package/ui/running-view.ts +90 -28
- package/version.ts +28 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* delegate — child-activity feed formatting (Pi-free).
|
|
3
|
+
*
|
|
4
|
+
* One formatter, two consumers: the live running panel (in-memory events
|
|
5
|
+
* pushed by the runner) and `/delegate peek` (events decoded from the
|
|
6
|
+
* captured RPC transcript on disk). Everything bounded and width-safe by
|
|
7
|
+
* character count; components truncate to terminal width themselves.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import * as fs from "node:fs";
|
|
11
|
+
import { decodeTranscriptRecord, type TranscriptRecordV1 } from "./types.ts";
|
|
12
|
+
|
|
13
|
+
/** One feed event, whether captured live or decoded from a transcript. */
|
|
14
|
+
export interface FeedEvent {
|
|
15
|
+
/** Epoch ms (live) or ISO string (transcript) — normalized to ms. */
|
|
16
|
+
atMs: number;
|
|
17
|
+
kind: "tool_start" | "tool_end" | "assistant" | "provider_error" | "handoff" | "settled" | "info";
|
|
18
|
+
tool?: string;
|
|
19
|
+
detail: string;
|
|
20
|
+
/** tool_end: failure marker. */
|
|
21
|
+
isError?: boolean;
|
|
22
|
+
/** tool_end: duration ms when known. */
|
|
23
|
+
durationMs?: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface FeedRenderOptions {
|
|
27
|
+
/** Feed window start (epoch ms) — events are stamped +Ns relative to it. */
|
|
28
|
+
startMs: number;
|
|
29
|
+
/** Maximum events kept/rendered. */
|
|
30
|
+
maxEvents?: number;
|
|
31
|
+
/** Maximum characters per line. */
|
|
32
|
+
maxChars?: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const DEFAULT_MAX_EVENTS = 200;
|
|
36
|
+
const DEFAULT_MAX_CHARS = 160;
|
|
37
|
+
|
|
38
|
+
function clip(text: string, max: number): string {
|
|
39
|
+
const s = text.replace(/\s+/g, " ").trim();
|
|
40
|
+
return s.length > max ? `${s.slice(0, max - 1)}…` : s;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Render feed events as bounded one-liners: `+42s ▶ bash npm test`. */
|
|
44
|
+
export function renderFeedEvents(events: FeedEvent[], opts: FeedRenderOptions): string[] {
|
|
45
|
+
const maxChars = opts.maxChars ?? DEFAULT_MAX_CHARS;
|
|
46
|
+
const maxEvents = opts.maxEvents ?? DEFAULT_MAX_EVENTS;
|
|
47
|
+
const lines: string[] = [];
|
|
48
|
+
for (const e of events.slice(-maxEvents)) {
|
|
49
|
+
const rel = Math.max(0, Math.round((e.atMs - opts.startMs) / 1000));
|
|
50
|
+
const ts = rel >= 60 ? `${Math.floor(rel / 60)}m${String(rel % 60).padStart(2, "0")}s` : `${rel}s`;
|
|
51
|
+
let mark = "·";
|
|
52
|
+
let text = e.detail;
|
|
53
|
+
switch (e.kind) {
|
|
54
|
+
case "tool_start":
|
|
55
|
+
mark = "▶";
|
|
56
|
+
text = e.tool ? `${e.tool} ${e.detail}` : e.detail;
|
|
57
|
+
break;
|
|
58
|
+
case "tool_end":
|
|
59
|
+
mark = e.isError ? "✗" : "✓";
|
|
60
|
+
text = e.tool ? `${e.tool} ${e.detail}` : e.detail;
|
|
61
|
+
if (e.durationMs !== undefined) text += ` (${Math.round(e.durationMs / 1000)}s)`;
|
|
62
|
+
break;
|
|
63
|
+
case "assistant":
|
|
64
|
+
mark = "✎";
|
|
65
|
+
break;
|
|
66
|
+
case "provider_error":
|
|
67
|
+
mark = "⚠";
|
|
68
|
+
break;
|
|
69
|
+
case "handoff":
|
|
70
|
+
mark = "▣";
|
|
71
|
+
break;
|
|
72
|
+
case "settled":
|
|
73
|
+
mark = "◉";
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
lines.push(clip(`+${ts} ${mark} ${text}`, maxChars));
|
|
77
|
+
}
|
|
78
|
+
return lines;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Live feed ring: bounded append with drop-oldest. */
|
|
82
|
+
export class FeedRing {
|
|
83
|
+
private readonly events: FeedEvent[] = [];
|
|
84
|
+
private readonly capacity: number;
|
|
85
|
+
constructor(capacity = 120) {
|
|
86
|
+
this.capacity = capacity;
|
|
87
|
+
}
|
|
88
|
+
push(event: FeedEvent): void {
|
|
89
|
+
this.events.push(event);
|
|
90
|
+
if (this.events.length > this.capacity) this.events.shift();
|
|
91
|
+
}
|
|
92
|
+
all(): readonly FeedEvent[] {
|
|
93
|
+
return this.events;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface ParsedRecord {
|
|
98
|
+
receivedAtMs: number;
|
|
99
|
+
type?: string;
|
|
100
|
+
toolCallId?: string;
|
|
101
|
+
toolName?: string;
|
|
102
|
+
args?: unknown;
|
|
103
|
+
result?: { content?: Array<{ type?: string; text?: string }>; isError?: boolean; details?: { delegateHandoff?: { outcome?: string } } };
|
|
104
|
+
message?: {
|
|
105
|
+
role?: string;
|
|
106
|
+
stopReason?: string;
|
|
107
|
+
errorMessage?: string;
|
|
108
|
+
content?: Array<{ type?: string; text?: string; thinking?: string }>;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function parseEnvelope(line: string): { atMs: number; rec: ParsedRecord | null } {
|
|
113
|
+
try {
|
|
114
|
+
const env = JSON.parse(line) as TranscriptRecordV1;
|
|
115
|
+
const atMs = Date.parse(env.receivedAt);
|
|
116
|
+
let rec: ParsedRecord | null = null;
|
|
117
|
+
try {
|
|
118
|
+
rec = JSON.parse(decodeTranscriptRecord(env)) as ParsedRecord;
|
|
119
|
+
} catch {
|
|
120
|
+
rec = null;
|
|
121
|
+
}
|
|
122
|
+
return { atMs: Number.isFinite(atMs) ? atMs : 0, rec };
|
|
123
|
+
} catch {
|
|
124
|
+
return { atMs: 0, rec: null };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function resultTextHead(result: ParsedRecord["result"]): string {
|
|
129
|
+
const blocks = result?.content ?? [];
|
|
130
|
+
for (const b of blocks) {
|
|
131
|
+
if (b?.type === "text" && typeof b.text === "string" && b.text.trim()) return b.text;
|
|
132
|
+
}
|
|
133
|
+
return "";
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Decode the tail of a captured child transcript into feed events — the
|
|
138
|
+
* backing store for `/delegate peek` (live runs grow the file in real time).
|
|
139
|
+
*/
|
|
140
|
+
export function feedEventsFromTranscript(
|
|
141
|
+
transcriptPath: string,
|
|
142
|
+
opts?: { maxEvents?: number; maxBytes?: number },
|
|
143
|
+
): { startMs: number; events: FeedEvent[] } {
|
|
144
|
+
const maxEvents = opts?.maxEvents ?? 120;
|
|
145
|
+
const maxBytes = opts?.maxBytes ?? 1_500_000;
|
|
146
|
+
const events: FeedEvent[] = [];
|
|
147
|
+
let startMs = 0;
|
|
148
|
+
try {
|
|
149
|
+
const stat = fs.statSync(transcriptPath);
|
|
150
|
+
const fd = fs.openSync(transcriptPath, "r");
|
|
151
|
+
try {
|
|
152
|
+
const readFrom = Math.max(0, stat.size - maxBytes);
|
|
153
|
+
const buf = Buffer.alloc(stat.size - readFrom);
|
|
154
|
+
fs.readSync(fd, buf, 0, buf.length, readFrom);
|
|
155
|
+
const lines = buf.toString("utf8").split("\n").filter((l) => l.trim());
|
|
156
|
+
// A tail cut can start mid-line; drop a partial first line.
|
|
157
|
+
if (readFrom > 0 && lines.length > 0) lines.shift();
|
|
158
|
+
const openTools = new Map<string, { name?: string; atMs: number; detail?: string }>();
|
|
159
|
+
for (const line of lines) {
|
|
160
|
+
const { atMs, rec } = parseEnvelope(line);
|
|
161
|
+
if (!rec || !atMs) continue;
|
|
162
|
+
if (startMs === 0) startMs = atMs;
|
|
163
|
+
switch (rec.type) {
|
|
164
|
+
case "tool_execution_start": {
|
|
165
|
+
const name = rec.toolName ?? "tool";
|
|
166
|
+
const detail = describeArgs(name, rec.args);
|
|
167
|
+
openTools.set(rec.toolCallId ?? String(events.length), { name, atMs, detail });
|
|
168
|
+
events.push({ atMs, kind: "tool_start", tool: name, detail });
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
case "tool_execution_end": {
|
|
172
|
+
const name = rec.toolName ?? "tool";
|
|
173
|
+
const open = openTools.get(rec.toolCallId ?? "");
|
|
174
|
+
openTools.delete(rec.toolCallId ?? "");
|
|
175
|
+
// End lines echo the start's arg detail (path/command) —
|
|
176
|
+
// file-content heads are noise; bash keeps its output head.
|
|
177
|
+
const head = resultTextHead(rec.result);
|
|
178
|
+
const detail =
|
|
179
|
+
name === "bash" && head
|
|
180
|
+
? clip(head, 70)
|
|
181
|
+
: (open?.detail || (head ? clip(head, 70) : "done"));
|
|
182
|
+
events.push({
|
|
183
|
+
atMs,
|
|
184
|
+
kind: "tool_end",
|
|
185
|
+
tool: name,
|
|
186
|
+
detail,
|
|
187
|
+
isError: rec.result?.isError === true || /\berror\b/i.test(head.slice(0, 40)),
|
|
188
|
+
...(open ? { durationMs: atMs - open.atMs } : {}),
|
|
189
|
+
});
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
case "message_end": {
|
|
193
|
+
const m = rec.message ?? {};
|
|
194
|
+
if (m.role !== "assistant") break;
|
|
195
|
+
if (m.stopReason === "error") {
|
|
196
|
+
events.push({
|
|
197
|
+
atMs,
|
|
198
|
+
kind: "provider_error",
|
|
199
|
+
detail: m.errorMessage?.trim() || "provider error (no message)",
|
|
200
|
+
});
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
const blocks = m.content ?? [];
|
|
204
|
+
const handoff = rec as unknown as { toolName?: string };
|
|
205
|
+
void handoff;
|
|
206
|
+
let text = "";
|
|
207
|
+
for (const b of blocks) if (b?.type === "text" && b.text?.trim()) text = b.text;
|
|
208
|
+
if (text.trim()) events.push({ atMs, kind: "assistant", detail: text.split("\n").filter(Boolean)[0] ?? text });
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
case "agent_settled":
|
|
212
|
+
events.push({ atMs, kind: "settled", detail: "agent settled" });
|
|
213
|
+
break;
|
|
214
|
+
default:
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
} finally {
|
|
219
|
+
fs.closeSync(fd);
|
|
220
|
+
}
|
|
221
|
+
} catch {
|
|
222
|
+
// unreadable/missing transcript → empty feed
|
|
223
|
+
}
|
|
224
|
+
return { startMs, events: events.slice(-maxEvents) };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function describeArgs(tool: string, args: unknown): string {
|
|
228
|
+
if (!args || typeof args !== "object") return "";
|
|
229
|
+
const a = args as Record<string, unknown>;
|
|
230
|
+
const v = a.command ?? a.path ?? a.pattern ?? a.query;
|
|
231
|
+
if (typeof v === "string") return clip(v, 90);
|
|
232
|
+
if (tool === "handoff") return "structured submission";
|
|
233
|
+
return "";
|
|
234
|
+
}
|
package/types.ts
CHANGED
|
@@ -21,6 +21,9 @@ export interface DelegateRole {
|
|
|
21
21
|
writeCapable: boolean;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
import type { HandoffSubmission } from "./handoff.ts";
|
|
25
|
+
import type { FeedEvent } from "./transcript-feed.ts";
|
|
26
|
+
|
|
24
27
|
// ── Errors ────────────────────────────────────────────────────────────────
|
|
25
28
|
|
|
26
29
|
export const DELEGATE_ERROR_CODES = [
|
|
@@ -37,6 +40,7 @@ export const DELEGATE_ERROR_CODES = [
|
|
|
37
40
|
"E_RPC_PROMPT_REJECTED",
|
|
38
41
|
"E_CHILD_EXIT",
|
|
39
42
|
"E_CHILD_MODEL",
|
|
43
|
+
"E_PROVIDER_ERROR",
|
|
40
44
|
"E_NO_HANDOFF",
|
|
41
45
|
"E_CANCELLED",
|
|
42
46
|
"E_TIMEOUT_IDLE",
|
|
@@ -105,6 +109,8 @@ export interface DelegateRequest {
|
|
|
105
109
|
cwd: string;
|
|
106
110
|
/** "provider/model-id" of the parent model. */
|
|
107
111
|
parentModel: string;
|
|
112
|
+
/** R5: explicit child model pin ("provider/model-id"); overrides inheritance. */
|
|
113
|
+
model?: string;
|
|
108
114
|
/** Parent thinking level, when available. */
|
|
109
115
|
thinkingLevel?: string;
|
|
110
116
|
projectTrusted: boolean;
|
|
@@ -112,6 +118,8 @@ export interface DelegateRequest {
|
|
|
112
118
|
timeoutMs?: number;
|
|
113
119
|
/** Project root for the project-wide config overlay (optional). */
|
|
114
120
|
projectRoot?: string;
|
|
121
|
+
/** R3: resume a prior run's child session (its session file is re-entered). */
|
|
122
|
+
resumeFrom?: string;
|
|
115
123
|
}
|
|
116
124
|
|
|
117
125
|
/** Bounded, parent-visible tool result details (schema v1). */
|
|
@@ -130,6 +138,12 @@ export interface DelegateDetails {
|
|
|
130
138
|
usage: DelegateUsage;
|
|
131
139
|
outputBytes: number;
|
|
132
140
|
outputTruncated: boolean;
|
|
141
|
+
/** R2: partial handoff captured from a killed child (timeouts). */
|
|
142
|
+
partialHandoff?: string;
|
|
143
|
+
/** R3: the child's persisted session file, when session capture is on. */
|
|
144
|
+
sessionPath?: string;
|
|
145
|
+
/** R7: resolved timeout provenance, e.g. "hard 45m (per-run) · idle 22m30s". */
|
|
146
|
+
timeoutInfo?: string;
|
|
133
147
|
transcriptPath: string;
|
|
134
148
|
stderrPath: string;
|
|
135
149
|
displayItems: DelegateDisplayItem[];
|
|
@@ -144,6 +158,8 @@ export interface DelegateRunResult {
|
|
|
144
158
|
/** Bounded final handoff for the parent context ("" on failure). */
|
|
145
159
|
handoff: string;
|
|
146
160
|
details: DelegateDetails;
|
|
161
|
+
/** R5/R7: provenance notes (model pin, partial-handoff availability). */
|
|
162
|
+
modelNote?: string;
|
|
147
163
|
error?: DelegateError;
|
|
148
164
|
}
|
|
149
165
|
|
|
@@ -152,6 +168,8 @@ export interface RunnerOutcome {
|
|
|
152
168
|
runId: string;
|
|
153
169
|
state: RunTerminalState;
|
|
154
170
|
handoff: string;
|
|
171
|
+
/** R2: bounded partial handoff captured at timeout kill. */
|
|
172
|
+
partialHandoff?: string;
|
|
155
173
|
outputBytes: number;
|
|
156
174
|
outputTruncated: boolean;
|
|
157
175
|
startedAt: string;
|
|
@@ -193,6 +211,20 @@ export interface RunMetadataV1 {
|
|
|
193
211
|
outputBytes?: number;
|
|
194
212
|
outputTruncated?: boolean;
|
|
195
213
|
finalHandoff?: string;
|
|
214
|
+
/** R2: partial handoff captured from a killed child (timeouts). */
|
|
215
|
+
partialHandoff?: string;
|
|
216
|
+
/** Structured handoff submitted through the child's handoff tool. */
|
|
217
|
+
handoffData?: HandoffSubmission;
|
|
218
|
+
/** R3: the child's persisted session file (durable resume anchor). */
|
|
219
|
+
sessionPath?: string;
|
|
220
|
+
/** R3: prior run whose child session this run resumed. */
|
|
221
|
+
resumeOf?: string;
|
|
222
|
+
/** R6: git HEAD at run start ("<sha>" when the cwd is a git worktree). */
|
|
223
|
+
gitBase?: string;
|
|
224
|
+
/** R6: bounded `git status --porcelain` at run start. */
|
|
225
|
+
gitStatus?: string;
|
|
226
|
+
/** R6: bounded post-run `git diff --stat` (worktree vs. run start). */
|
|
227
|
+
gitDelta?: string;
|
|
196
228
|
transcriptPath: string;
|
|
197
229
|
stderrPath: string;
|
|
198
230
|
/** Present only on manually curated receipts (one-time data repair). */
|
|
@@ -342,6 +374,10 @@ export interface RunStreamUpdate {
|
|
|
342
374
|
runId: string;
|
|
343
375
|
role: RoleName;
|
|
344
376
|
phase: "starting" | "running" | `tool:${string}` | "finalizing";
|
|
377
|
+
/** R5: the resolved child model, echoed in live updates. */
|
|
378
|
+
model?: string;
|
|
379
|
+
/** Live feed: tool names with a start but no end yet. */
|
|
380
|
+
openTools?: string[];
|
|
345
381
|
elapsedMs: number;
|
|
346
382
|
lastActions: string[];
|
|
347
383
|
usage: DelegateUsage;
|
|
@@ -349,6 +385,8 @@ export interface RunStreamUpdate {
|
|
|
349
385
|
|
|
350
386
|
export type RunHooks = {
|
|
351
387
|
onUpdate?: (update: RunStreamUpdate) => void;
|
|
388
|
+
/** Live feed events for the running panel and peek views (every event, unthrottled). */
|
|
389
|
+
onEvent?: (event: FeedEvent) => void;
|
|
352
390
|
abortSignal?: AbortSignal;
|
|
353
391
|
};
|
|
354
392
|
|
package/ui/peek-view.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* delegate — `/delegate peek` overlay: a scrollable, live-following view of
|
|
3
|
+
* a child's activity, decoded from its captured RPC transcript (works for
|
|
4
|
+
* the active run AND post-mortem). The host owns the refresh timer (1 s
|
|
5
|
+
* while following) and must clear it when the overlay closes.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { truncateToWidth, visibleWidth, matchesKey, type Component } from "@earendil-works/pi-tui";
|
|
9
|
+
import type { Theme, KeybindingsManager, ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
10
|
+
|
|
11
|
+
export interface PeekViewState {
|
|
12
|
+
/** Header: run id + state/model. */
|
|
13
|
+
title: string;
|
|
14
|
+
/** One muted summary line (path, size, role, finished state…). */
|
|
15
|
+
summary: string;
|
|
16
|
+
/** Formatted feed lines, oldest first (newest at the bottom). */
|
|
17
|
+
lines: string[];
|
|
18
|
+
/** True while the run is still active (enables follow). */
|
|
19
|
+
live: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface PeekViewHost {
|
|
23
|
+
theme: Theme;
|
|
24
|
+
keybindings: KeybindingsManager;
|
|
25
|
+
state(): PeekViewState;
|
|
26
|
+
done(result: { closed?: boolean }): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class PeekView implements Component {
|
|
30
|
+
private readonly host: PeekViewHost;
|
|
31
|
+
private offsetFromEnd = 0;
|
|
32
|
+
private following = true;
|
|
33
|
+
private lastLineCount = 0;
|
|
34
|
+
|
|
35
|
+
constructor(host: PeekViewHost) {
|
|
36
|
+
this.host = host;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
handleInput(data: string): void {
|
|
40
|
+
const kb = this.host.keybindings;
|
|
41
|
+
const up = (kb?.matches?.(data, "tui.select.up") ?? false) || matchesKey(data, "j");
|
|
42
|
+
const down = (kb?.matches?.(data, "tui.select.down") ?? false) || matchesKey(data, "k");
|
|
43
|
+
const cancel =
|
|
44
|
+
(kb?.matches?.(data, "tui.select.cancel") ?? false) ||
|
|
45
|
+
matchesKey(data, "escape") ||
|
|
46
|
+
matchesKey(data, "q");
|
|
47
|
+
if (cancel) {
|
|
48
|
+
this.host.done({ closed: true });
|
|
49
|
+
} else if (up) {
|
|
50
|
+
this.following = false;
|
|
51
|
+
this.offsetFromEnd += 1;
|
|
52
|
+
} else if (down) {
|
|
53
|
+
this.offsetFromEnd = Math.max(0, this.offsetFromEnd - 1);
|
|
54
|
+
if (this.offsetFromEnd === 0) this.following = true;
|
|
55
|
+
} else if (matchesKey(data, "f")) {
|
|
56
|
+
this.following = true;
|
|
57
|
+
this.offsetFromEnd = 0;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
invalidate(): void {
|
|
62
|
+
// Rendered from host state each frame; no cache to clear.
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
render(width: number): string[] {
|
|
66
|
+
const s = this.host.state();
|
|
67
|
+
const theme = this.host.theme;
|
|
68
|
+
const w = Math.max(1, Math.floor(width));
|
|
69
|
+
const fg = (kind: ThemeColor, text: string) => (theme?.fg ? theme.fg(kind, text) : text);
|
|
70
|
+
const title = ` Peek · ${s.title} `;
|
|
71
|
+
const top = `╭─${truncateToWidth(title, Math.max(4, w - 4))}${"─".repeat(Math.max(0, w - 3 - Math.min(title.length, Math.max(4, w - 4))))}─╮`;
|
|
72
|
+
const bottom = `╰${"─".repeat(Math.max(0, w - 2))}╯`;
|
|
73
|
+
const inner = Math.max(1, w - 4);
|
|
74
|
+
const pad = (text: string, color?: ThemeColor) => {
|
|
75
|
+
const line = `│ ${truncateToWidth(text, inner)} │`;
|
|
76
|
+
return color && theme?.fg ? theme.fg(color, line) : line;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// Viewport: keep the overlay in the 20–24 line budget.
|
|
80
|
+
const maxFeed = Math.max(1, 20 - 4);
|
|
81
|
+
const total = s.lines.length;
|
|
82
|
+
if (total !== this.lastLineCount && this.following) this.offsetFromEnd = 0;
|
|
83
|
+
this.lastLineCount = total;
|
|
84
|
+
const end = Math.max(0, total - this.offsetFromEnd);
|
|
85
|
+
const start = Math.max(0, end - maxFeed);
|
|
86
|
+
const window = s.lines.slice(start, end);
|
|
87
|
+
|
|
88
|
+
const lines: string[] = [
|
|
89
|
+
truncateToWidth(top, w),
|
|
90
|
+
pad(s.summary, "muted"),
|
|
91
|
+
pad(
|
|
92
|
+
s.live
|
|
93
|
+
? fg("success", `● live — following (j/k scroll, f re-follow, q close)`)
|
|
94
|
+
: `final — ${total} events (j/k scroll, q close)`,
|
|
95
|
+
s.live ? "success" : "dim",
|
|
96
|
+
),
|
|
97
|
+
];
|
|
98
|
+
if (total === 0) {
|
|
99
|
+
lines.push(pad("no activity captured", "muted"));
|
|
100
|
+
} else {
|
|
101
|
+
for (const line of window) lines.push(pad(line));
|
|
102
|
+
if (start > 0) lines.push(pad(`… ${start} earlier event(s) — j to scroll up`, "dim"));
|
|
103
|
+
if (end < total) lines.push(pad(`… ${total - end} newer event(s) — k to scroll down`, "dim"));
|
|
104
|
+
}
|
|
105
|
+
lines.push(truncateToWidth(bottom, w));
|
|
106
|
+
return lines.map((l) => (visibleWidth(l) > w ? truncateToWidth(l, w) : l));
|
|
107
|
+
}
|
|
108
|
+
}
|
package/ui/running-view.ts
CHANGED
|
@@ -1,21 +1,32 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* delegate —
|
|
2
|
+
* delegate — live child feed panel for command-driven foreground runs.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* A window into the running child: identity + budgets header, elapsed-vs-hard
|
|
5
|
+
* progress, in-flight tools, and a bounded scrolling feed of the child's
|
|
6
|
+
* activity (tool starts/ends, assistant lines, provider errors, handoff).
|
|
7
|
+
* Cancels via Escape twice (armed). Refresh cadence is owned by the host
|
|
8
|
+
* (1 s); feed lines arrive pre-formatted from the adapter's event ring.
|
|
8
9
|
*/
|
|
9
10
|
|
|
10
11
|
import { truncateToWidth, visibleWidth, matchesKey, type Component } from "@earendil-works/pi-tui";
|
|
11
|
-
import type { Theme, KeybindingsManager } from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import type { Theme, KeybindingsManager, ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
12
13
|
|
|
13
14
|
export interface RunningViewState {
|
|
14
15
|
runId: string;
|
|
15
16
|
role: string;
|
|
17
|
+
/** Resolved child model ("provider/model"). */
|
|
18
|
+
model?: string;
|
|
16
19
|
phase: string;
|
|
17
20
|
elapsedMs: number;
|
|
18
|
-
|
|
21
|
+
/** Effective hard cap — drives the progress bar. */
|
|
22
|
+
hardMs?: number;
|
|
23
|
+
turns?: number;
|
|
24
|
+
/** Tokens observed so far. */
|
|
25
|
+
tokens?: { input?: number; output?: number };
|
|
26
|
+
/** Tools currently in flight. */
|
|
27
|
+
openTools?: string[];
|
|
28
|
+
/** Pre-formatted feed lines (newest last), already clipped. */
|
|
29
|
+
feedLines: string[];
|
|
19
30
|
}
|
|
20
31
|
|
|
21
32
|
export interface RunningViewHost {
|
|
@@ -25,9 +36,21 @@ export interface RunningViewHost {
|
|
|
25
36
|
done(result: { cancelled?: boolean }): void;
|
|
26
37
|
}
|
|
27
38
|
|
|
39
|
+
function fmtDuration(ms: number): string {
|
|
40
|
+
const s = Math.max(0, Math.round(ms / 1000));
|
|
41
|
+
if (s < 60) return `${s}s`;
|
|
42
|
+
return `${Math.floor(s / 60)}m${String(s % 60).padStart(2, "0")}s`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function fmtTokens(n?: number): string {
|
|
46
|
+
if (!n) return "0";
|
|
47
|
+
if (n < 1000) return String(n);
|
|
48
|
+
if (n < 1_000_000) return `${Math.round(n / 100) / 10}k`;
|
|
49
|
+
return `${Math.round(n / 100_000) / 10}M`;
|
|
50
|
+
}
|
|
51
|
+
|
|
28
52
|
export class RunningView implements Component {
|
|
29
53
|
private readonly host: RunningViewHost;
|
|
30
|
-
private cachedLines: string[] | undefined;
|
|
31
54
|
private cancelArmed = false;
|
|
32
55
|
|
|
33
56
|
constructor(host: RunningViewHost) {
|
|
@@ -41,7 +64,7 @@ export class RunningView implements Component {
|
|
|
41
64
|
matchesKey(data, "escape") ||
|
|
42
65
|
matchesKey(data, "q");
|
|
43
66
|
if (isCancelKey) {
|
|
44
|
-
//
|
|
67
|
+
// Cancel is destructive to the child — require a second press.
|
|
45
68
|
if (this.cancelArmed) {
|
|
46
69
|
this.host.done({ cancelled: true });
|
|
47
70
|
} else {
|
|
@@ -52,34 +75,73 @@ export class RunningView implements Component {
|
|
|
52
75
|
}
|
|
53
76
|
|
|
54
77
|
invalidate(): void {
|
|
55
|
-
|
|
78
|
+
// No internal cache: state() is cheap and host-driven refresh is 1 s.
|
|
56
79
|
}
|
|
57
80
|
|
|
58
81
|
render(width: number): string[] {
|
|
59
82
|
const s = this.host.state();
|
|
60
83
|
const theme = this.host.theme;
|
|
61
84
|
const w = Math.max(1, Math.floor(width));
|
|
62
|
-
const
|
|
63
|
-
const title = truncateToWidth(" Delegating ", Math.max(4, w - 4));
|
|
85
|
+
const title = " Delegating ";
|
|
64
86
|
const border = "─".repeat(Math.max(0, w - title.length - 2));
|
|
65
|
-
const top = `╭─${title}${border}
|
|
66
|
-
const bottom = `╰${"─".repeat(Math.max(0, w - 2))}
|
|
87
|
+
const top = `╭─${title}${border.slice(0, Math.max(0, border.length))}─╮`.slice(0, w);
|
|
88
|
+
const bottom = `╰${"─".repeat(Math.max(0, w - 2))}╯`.slice(0, w);
|
|
67
89
|
const inner = Math.max(1, w - 4);
|
|
68
|
-
const pad = (text: string) =>
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
90
|
+
const pad = (text: string, color?: ThemeColor) => {
|
|
91
|
+
const line = `│ ${truncateToWidth(text, inner)} │`;
|
|
92
|
+
return color && theme?.fg ? theme.fg(color, line) : line;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const fg = (kind: ThemeColor, text: string) => (theme?.fg ? theme.fg(kind, text) : text);
|
|
96
|
+
const lines: string[] = [top];
|
|
97
|
+
|
|
98
|
+
// Header: identity + elapsed + turns + tokens.
|
|
99
|
+
const head = [
|
|
100
|
+
s.role,
|
|
101
|
+
s.model,
|
|
102
|
+
fg("accent", fmtDuration(s.elapsedMs)),
|
|
103
|
+
...(s.turns !== undefined ? [`turn ${s.turns}`] : []),
|
|
104
|
+
...(s.tokens ? [`↑${fmtTokens(s.tokens.input)} ↓${fmtTokens(s.tokens.output)}`] : []),
|
|
105
|
+
]
|
|
106
|
+
.filter(Boolean)
|
|
107
|
+
.join(" · ");
|
|
108
|
+
lines.push(pad(head));
|
|
109
|
+
|
|
110
|
+
// Progress: elapsed vs hard cap (never color-only — text carries %).
|
|
111
|
+
if (s.hardMs && s.hardMs > 0) {
|
|
112
|
+
const frac = Math.min(1, s.elapsedMs / s.hardMs);
|
|
113
|
+
const barWidth = Math.max(4, Math.min(inner - 14, 24));
|
|
114
|
+
const filled = Math.round(frac * barWidth);
|
|
115
|
+
const bar = `${"█".repeat(filled)}${"░".repeat(Math.max(0, barWidth - filled))}`;
|
|
116
|
+
lines.push(pad(`${bar} ${Math.round(frac * 100)}% of ${fmtDuration(s.hardMs)} hard`));
|
|
72
117
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
118
|
+
|
|
119
|
+
// In-flight tools.
|
|
120
|
+
if (s.openTools && s.openTools.length > 0) {
|
|
121
|
+
lines.push(pad(fg("warning", `in flight: ${s.openTools.join(", ")}`)));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Feed: newest last; take as many as fit the budget.
|
|
125
|
+
const feedBudget = Math.max(0, 16 - lines.length - 2);
|
|
126
|
+
const feed = s.feedLines.slice(-feedBudget);
|
|
127
|
+
if (feed.length > 0) {
|
|
128
|
+
for (const line of feed) lines.push(pad(line, "text"));
|
|
129
|
+
} else {
|
|
130
|
+
lines.push(pad("starting — waiting for the child's first events…", "muted"));
|
|
81
131
|
}
|
|
82
|
-
|
|
83
|
-
|
|
132
|
+
|
|
133
|
+
// Footer.
|
|
134
|
+
lines.push(
|
|
135
|
+
pad(
|
|
136
|
+
this.cancelArmed
|
|
137
|
+
? fg("error", "ESC again: CONFIRM cancel")
|
|
138
|
+
: "esc/q cancel (press twice)",
|
|
139
|
+
this.cancelArmed ? "error" : "dim",
|
|
140
|
+
),
|
|
141
|
+
);
|
|
142
|
+
lines.push(bottom);
|
|
143
|
+
|
|
144
|
+
// Width safety: hard-clip rather than corrupt layout.
|
|
145
|
+
return lines.map((l) => (visibleWidth(l) > w ? truncateToWidth(l, w) : l));
|
|
84
146
|
}
|
|
85
147
|
}
|
package/version.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* delegate — loaded-version provenance (R7).
|
|
3
|
+
*
|
|
4
|
+
* A running Pi session keeps whatever extension code it loaded at startup;
|
|
5
|
+
* newer npm-store installs only apply after /reload. Every result header
|
|
6
|
+
* and /delegate status therefore carries the version actually executing,
|
|
7
|
+
* so "stale copy in this process" is self-evident instead of mysterious.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import * as fs from "node:fs";
|
|
11
|
+
import * as path from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
|
|
14
|
+
let cached: string | null = null;
|
|
15
|
+
|
|
16
|
+
export function delegateVersion(): string {
|
|
17
|
+
if (cached) return cached;
|
|
18
|
+
try {
|
|
19
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
20
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(here, "package.json"), "utf8")) as {
|
|
21
|
+
version?: string;
|
|
22
|
+
};
|
|
23
|
+
cached = pkg.version ?? "unknown";
|
|
24
|
+
} catch {
|
|
25
|
+
cached = "unknown";
|
|
26
|
+
}
|
|
27
|
+
return cached;
|
|
28
|
+
}
|