@maheidem/pi-delegate 0.9.0 → 0.11.0
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 +15 -0
- package/README.md +59 -0
- package/extensions/application.ts +136 -17
- package/extensions/config.ts +152 -5
- package/extensions/index.ts +200 -40
- package/extensions/rpc-jsonl.ts +25 -0
- package/extensions/run-store.ts +4 -0
- package/extensions/runner.ts +67 -6
- package/extensions/types.ts +55 -0
- package/extensions/ui/event-card.ts +96 -0
- package/extensions/ui/peek-view.ts +24 -2
- package/extensions/ui/running-view.ts +119 -16
- package/package.json +1 -1
- package/roles/general.md +5 -0
- package/roles/research.md +5 -0
package/extensions/runner.ts
CHANGED
|
@@ -16,11 +16,13 @@ import * as childProcess from "node:child_process";
|
|
|
16
16
|
import * as fs from "node:fs";
|
|
17
17
|
import * as path from "node:path";
|
|
18
18
|
import type {
|
|
19
|
+
ChildLiveState,
|
|
19
20
|
DelegateDisplayItem,
|
|
20
21
|
DelegateError,
|
|
21
22
|
DelegateErrorCode,
|
|
22
23
|
DelegateRole,
|
|
23
24
|
DelegateUsage,
|
|
25
|
+
ResolvedChildConfig,
|
|
24
26
|
RunState,
|
|
25
27
|
RunTerminalState,
|
|
26
28
|
RunHooks,
|
|
@@ -144,6 +146,8 @@ export interface RunnerSpawnRequest {
|
|
|
144
146
|
/** R17: ask budget (env to the child; 0/undefined disables ask_parent). */
|
|
145
147
|
askTimeoutMs?: number;
|
|
146
148
|
askMaxQuestions?: number;
|
|
149
|
+
/** Resolved child configuration (project overlay + per-invocation + parent). */
|
|
150
|
+
childConfig?: ResolvedChildConfig;
|
|
147
151
|
}
|
|
148
152
|
|
|
149
153
|
/**
|
|
@@ -183,7 +187,15 @@ export function buildChildArgs(
|
|
|
183
187
|
// ceiling; the delegate extension registers it in child mode. Background
|
|
184
188
|
// children (R17/R18) also get ask_parent when the ask channel is enabled.
|
|
185
189
|
const askEnabled = Boolean(req.background && req.askDir && req.askTimeoutMs && req.askTimeoutMs > 0);
|
|
186
|
-
const
|
|
190
|
+
const child = req.childConfig;
|
|
191
|
+
// Resolved configuration: per-invocation > project overlay > parent.
|
|
192
|
+
const model = child?.model?.trim() || req.parentModel;
|
|
193
|
+
const thinking = child?.thinking?.trim() || req.thinkingLevel;
|
|
194
|
+
// Tool ceiling: the project's child.tools allowlist (intersected with the
|
|
195
|
+
// registered parent tools by the resolver) replaces the role's tools;
|
|
196
|
+
// otherwise the role ceiling applies. handoff is always mandatory.
|
|
197
|
+
const roleTools = child?.tools !== undefined ? [...child.tools] : [...role.tools];
|
|
198
|
+
const toolList = [...new Set([...roleTools, "handoff", ...(askEnabled ? ["ask_parent"] : [])])].join(",");
|
|
187
199
|
// R3: the child's session is DURABLE. New runs persist it under the run
|
|
188
200
|
// store (--session-dir); resumed runs re-enter the prior session file
|
|
189
201
|
// directly (--session <file>) so a killed run can be continued in the
|
|
@@ -194,13 +206,18 @@ export function buildChildArgs(
|
|
|
194
206
|
const args: string[] = [
|
|
195
207
|
"--mode", "rpc",
|
|
196
208
|
...sessionArgs,
|
|
197
|
-
"--model",
|
|
209
|
+
"--model", model,
|
|
198
210
|
];
|
|
199
|
-
if (
|
|
211
|
+
if (thinking) args.push("--thinking", thinking);
|
|
200
212
|
args.push("--tools", toolList);
|
|
201
|
-
|
|
213
|
+
// standard context (the default) keeps the child's skills, prompt
|
|
214
|
+
// templates and themes — only an explicit child.context: "minimal" disables them.
|
|
215
|
+
if ((child?.context ?? "standard") === "minimal") {
|
|
216
|
+
args.push("--no-skills", "--no-prompt-templates", "--no-themes");
|
|
217
|
+
}
|
|
202
218
|
args.push("--append-system-prompt", role.promptPath);
|
|
203
|
-
|
|
219
|
+
const trust = child?.trustProject ?? req.projectTrusted;
|
|
220
|
+
args.push(trust ? "--approve" : "--no-approve");
|
|
204
221
|
const env = buildChildEnv(req, askEnabled);
|
|
205
222
|
return { args, env, promptId: `delegate:${req.runId}:prompt` };
|
|
206
223
|
}
|
|
@@ -371,6 +388,12 @@ export class DelegateRunner {
|
|
|
371
388
|
private readonly promptId: string;
|
|
372
389
|
/** R16: steering sequence (follow_up record ids). */
|
|
373
390
|
private steerSeq = 0;
|
|
391
|
+
/** get_state request sequence (state record ids). */
|
|
392
|
+
private stateSeq = 0;
|
|
393
|
+
/** get_state ids we sent and are waiting on (stale ids are ignored). */
|
|
394
|
+
private pendingStateIds = new Set<string>();
|
|
395
|
+
/** Cached live child state from the last answered get_state (best-effort). */
|
|
396
|
+
private liveChildState: ChildLiveState | undefined;
|
|
374
397
|
/** R23: progress-report throttle state + heartbeat timer. */
|
|
375
398
|
private progressState: ProgressThrottleState = { reportsSent: 0, lastReportAt: 0, startedMs: 0 };
|
|
376
399
|
private progressTimer: NodeJS.Timeout | null = null;
|
|
@@ -396,6 +419,32 @@ export class DelegateRunner {
|
|
|
396
419
|
return this.child?.pid;
|
|
397
420
|
}
|
|
398
421
|
|
|
422
|
+
/**
|
|
423
|
+
* Host/TUI hook: ask the live child for its current state (get_state RPC).
|
|
424
|
+
* Best-effort: no child, a finished run, or a failed write simply no-ops.
|
|
425
|
+
* The answer is correlated by id and cached; stale ids are ignored.
|
|
426
|
+
*/
|
|
427
|
+
requestChildState(): void {
|
|
428
|
+
if (this.finalizing || this.cancelRequested || this.settled) return;
|
|
429
|
+
const child = this.child;
|
|
430
|
+
if (!child || child.killed || child.exitCode !== null) return;
|
|
431
|
+
this.stateSeq += 1;
|
|
432
|
+
const id = `delegate:${this.runId}:state:${this.stateSeq}`;
|
|
433
|
+
if (this.pendingStateIds.size >= 20) this.pendingStateIds.clear();
|
|
434
|
+
this.pendingStateIds.add(id);
|
|
435
|
+
try {
|
|
436
|
+
child.stdin?.write(`${JSON.stringify({ id, type: "get_state" })}\n`);
|
|
437
|
+
this.noteActivity();
|
|
438
|
+
} catch {
|
|
439
|
+
this.pendingStateIds.delete(id);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** Cached live child state from the last answered get_state (undefined until answered). */
|
|
444
|
+
getChildState(): ChildLiveState | undefined {
|
|
445
|
+
return this.liveChildState;
|
|
446
|
+
}
|
|
447
|
+
|
|
399
448
|
/** Spawn and drive the run to a terminal state. */
|
|
400
449
|
async run(): Promise<RunnerOutcome> {
|
|
401
450
|
const { metadata, paths, stdout, stderr } = this.opened;
|
|
@@ -599,6 +648,13 @@ export class DelegateRunner {
|
|
|
599
648
|
}
|
|
600
649
|
const cls = classifyRpcRecord(record);
|
|
601
650
|
switch (cls.kind) {
|
|
651
|
+
case "state_response":
|
|
652
|
+
// get_state answer — correlated by id; stale/unknown ids ignored.
|
|
653
|
+
if (cls.id && this.pendingStateIds.has(cls.id)) {
|
|
654
|
+
this.pendingStateIds.delete(cls.id);
|
|
655
|
+
if (cls.ok) this.liveChildState = cls.state;
|
|
656
|
+
}
|
|
657
|
+
break;
|
|
602
658
|
case "prompt_response":
|
|
603
659
|
if (cls.id === this.promptId || cls.id === "") {
|
|
604
660
|
if (cls.ok) {
|
|
@@ -1147,6 +1203,8 @@ export class DelegateRunner {
|
|
|
1147
1203
|
private finish(state: RunTerminalState, error?: DelegateError, extra?: { handoff?: string; truncated?: boolean; stopReason?: string; usage?: DelegateUsage; partialHandoff?: string }): void {
|
|
1148
1204
|
if (this.finalizing) return;
|
|
1149
1205
|
this.finalizing = true;
|
|
1206
|
+
// A terminal run can never answer get_state — drop the pending ids.
|
|
1207
|
+
this.pendingStateIds.clear();
|
|
1150
1208
|
const finishedAt = new Date().toISOString();
|
|
1151
1209
|
const durationMs = this.startedAtMs ? Date.now() - this.startedAtMs : 0;
|
|
1152
1210
|
// P3: timeout/cancel terminal states are reached without an explicit
|
|
@@ -1340,7 +1398,10 @@ export class DelegateRunner {
|
|
|
1340
1398
|
runId: this.runId,
|
|
1341
1399
|
role: this.req.role.name,
|
|
1342
1400
|
phase,
|
|
1343
|
-
model
|
|
1401
|
+
// The RESOLVED child model (per-invocation > project > parent), so
|
|
1402
|
+
// the head/status rows can never show the parent's model for a
|
|
1403
|
+
// pinned child. The receipt's `model` stays the parent model.
|
|
1404
|
+
model: this.req.childConfig?.model?.trim() || this.req.parentModel,
|
|
1344
1405
|
openTools: this.openToolNames(),
|
|
1345
1406
|
elapsedMs: this.startedAtMs ? now - this.startedAtMs : 0,
|
|
1346
1407
|
lastActions: this.lastActions.slice(-5),
|
package/extensions/types.ts
CHANGED
|
@@ -97,6 +97,49 @@ export const EMPTY_USAGE: DelegateUsage = {
|
|
|
97
97
|
turns: 0,
|
|
98
98
|
};
|
|
99
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Child configuration keys accepted by the project overlay
|
|
102
|
+
* (`<projectRoot>/.pi/delegate/config.json`). Validated field-by-field
|
|
103
|
+
* in config.ts; unknown keys are diagnosed and dropped.
|
|
104
|
+
*/
|
|
105
|
+
export interface ChildConfig {
|
|
106
|
+
/** Resolved child model ("provider/model-id"). */
|
|
107
|
+
model?: string;
|
|
108
|
+
/** Child thinking level (e.g. "high"). Inherits the parent when absent. */
|
|
109
|
+
thinking?: string;
|
|
110
|
+
/** Tool allowlist (restricted to registered parent tools at resolution time). */
|
|
111
|
+
tools?: string[];
|
|
112
|
+
/** Approve the project trust prompt (default: inherit the parent's trust). */
|
|
113
|
+
trustProject?: boolean;
|
|
114
|
+
/** Execution context tier. "standard" (default) keeps skills/templates/themes. */
|
|
115
|
+
context?: "standard" | "minimal";
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The child config as resolved for one run: every value the runner needs,
|
|
120
|
+
* plus where the model came from and any field-level diagnostics.
|
|
121
|
+
* Recorded in the receipt metadata so the resolved shape is inspectable
|
|
122
|
+
* after the fact.
|
|
123
|
+
*/
|
|
124
|
+
export interface ResolvedChildConfig extends ChildConfig {
|
|
125
|
+
/** Where the resolved child model came from. */
|
|
126
|
+
modelSource?: "per-invocation" | "project" | "parent";
|
|
127
|
+
/** Execution context tier (defaults to "standard"). */
|
|
128
|
+
context: "standard" | "minimal";
|
|
129
|
+
/** Field-level diagnostics from validation/resolution (unknown tool names, dropped values). */
|
|
130
|
+
diagnostics?: string[];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Live child state observed from the child's `get_state` RPC response. */
|
|
134
|
+
export interface ChildLiveState {
|
|
135
|
+
/** Live child model as "provider/id" (null when the child reports no model). */
|
|
136
|
+
model?: string;
|
|
137
|
+
/** Live child thinking level (from get_state, tolerating effort aliases). */
|
|
138
|
+
thinking?: string;
|
|
139
|
+
/** Live child streaming flag. */
|
|
140
|
+
isStreaming?: boolean;
|
|
141
|
+
}
|
|
142
|
+
|
|
100
143
|
export type RunSource = "tool" | "command";
|
|
101
144
|
|
|
102
145
|
/** Project-wide config path: `<projectRoot>/.pi/delegate/config.json`. */
|
|
@@ -121,6 +164,8 @@ export interface DelegateRequest {
|
|
|
121
164
|
projectRoot?: string;
|
|
122
165
|
/** R3: resume a prior run's child session (its session file is re-entered). */
|
|
123
166
|
resumeFrom?: string;
|
|
167
|
+
/** Resolved child config (project overlay + per-invocation + parent), filled by the adapter. */
|
|
168
|
+
childConfig?: ResolvedChildConfig;
|
|
124
169
|
/** R8: run as a background delegation (non-blocking tool call). */
|
|
125
170
|
background?: boolean;
|
|
126
171
|
/** R8: 3–6-word purpose summary, required when background. */
|
|
@@ -152,6 +197,8 @@ sessionPath?: string;
|
|
|
152
197
|
/** R22: validated 3–6-word purpose summary (required for every delegation;
|
|
153
198
|
* returned inline in the foreground result header). */
|
|
154
199
|
description?: string;
|
|
200
|
+
/** Resolved child configuration the run was launched with (receipt copy). */
|
|
201
|
+
childConfig?: ResolvedChildConfig;
|
|
155
202
|
transcriptPath: string;
|
|
156
203
|
stderrPath: string;
|
|
157
204
|
displayItems: DelegateDisplayItem[];
|
|
@@ -241,6 +288,8 @@ export interface RunMetadataV1 {
|
|
|
241
288
|
background?: boolean;
|
|
242
289
|
/** R8: validated 3–6-word purpose summary (background runs). */
|
|
243
290
|
description?: string;
|
|
291
|
+
/** Resolved child configuration this run was launched with. */
|
|
292
|
+
childConfig?: ResolvedChildConfig;
|
|
244
293
|
}
|
|
245
294
|
|
|
246
295
|
export interface TranscriptRecordV1 {
|
|
@@ -496,6 +545,12 @@ export interface DelegateApplication {
|
|
|
496
545
|
* the terminal state. */
|
|
497
546
|
runBackground(request: DelegateRequest): { error: DelegateRunResult } | { handle: BackgroundRunHandle };
|
|
498
547
|
cancel(runId?: string): Promise<CancelResult>;
|
|
548
|
+
/** Host/TUI hooks: live-child steering + state (get_state) — all best-effort. */
|
|
549
|
+
requestChildState(): void;
|
|
550
|
+
activeRunChildState(): ChildLiveState | undefined;
|
|
551
|
+
steerActiveRun(message: string): { ok: true } | { ok: false; error: string };
|
|
552
|
+
/** The session's active foreground run id (for peek/view wiring). */
|
|
553
|
+
activeRunId(): string | null;
|
|
499
554
|
inspect(runId?: string, includeTranscript?: boolean): RunInspection;
|
|
500
555
|
doctor(): DoctorReport;
|
|
501
556
|
getStatus(activeTools: string[] | "degraded", projectRoot?: string): DelegateStatus;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event-card — the shared "event card" block for middle-of-run informational
|
|
3
|
+
* messages (background progress lines, the blocked-waiting question block,
|
|
4
|
+
* delegate start/terminal messages). Every one of them used to render as
|
|
5
|
+
* bare gray text; they now get the same styled treatment as the panels:
|
|
6
|
+
* a rounded box on the custom-message background, a bold accent label on
|
|
7
|
+
* the header band, dimmed body lines. The words are unchanged — only the
|
|
8
|
+
* layout changes; no new text is added or removed.
|
|
9
|
+
*
|
|
10
|
+
* Pure layout (theme-driven): the pure lines function is unit-testable
|
|
11
|
+
* without a Pi session, and the component wrapper is the only Pi-facing
|
|
12
|
+
* piece. Headless fallbacks return plain text — output never disappears.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { truncateToWidth, visibleWidth, type Component } from "@earendil-works/pi-tui";
|
|
16
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
17
|
+
|
|
18
|
+
export interface EventCardSpec {
|
|
19
|
+
/** Bold header line (e.g. `? background del_abc…: asks (topic)`). */
|
|
20
|
+
label: string;
|
|
21
|
+
/** Body lines, one per row, verbatim (blank lines kept as blank rows). */
|
|
22
|
+
bodyLines: string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Render the card body rows for `width` (never wider than `width`).
|
|
27
|
+
* `theme` may be undefined — headless renderers then get plain text.
|
|
28
|
+
* Returns `[topBorder, ...bodyRows, bottomBorder]` (or just the label when
|
|
29
|
+
* the width is degenerate).
|
|
30
|
+
*/
|
|
31
|
+
export function eventCardLines(theme: Theme | undefined, width: number, spec: EventCardSpec): string[] {
|
|
32
|
+
const w = Math.max(1, Math.floor(width));
|
|
33
|
+
if (!theme?.fg) return [spec.label, ...spec.bodyLines];
|
|
34
|
+
if (w <= 2) return [truncateToWidth(spec.label, w)];
|
|
35
|
+
|
|
36
|
+
const inner = Math.max(1, w - 4); // │ + space on each side
|
|
37
|
+
const bgLine = (line: string): string => {
|
|
38
|
+
const visLen = visibleWidth(line);
|
|
39
|
+
const padded = visLen < w ? line + " ".repeat(w - visLen) : line;
|
|
40
|
+
if (theme.bg) return theme.bg("customMessageBg", truncateToWidth(padded, w));
|
|
41
|
+
return truncateToWidth(padded, w);
|
|
42
|
+
};
|
|
43
|
+
const borderColor = (s: string) => theme.fg("border", s);
|
|
44
|
+
|
|
45
|
+
// Header band: rounded corner + bold accent label + tail + rounded corner.
|
|
46
|
+
// The space between the corners is `w - 2` (not `inner`): the label lives
|
|
47
|
+
// on the top border line itself, not inside a side-bar row.
|
|
48
|
+
const label = theme.bold(theme.fg("accent", ` ${spec.label} `));
|
|
49
|
+
const clippedLabel = truncateToWidth(label, Math.max(1, w - 2), "", false);
|
|
50
|
+
const tail = "─".repeat(Math.max(0, w - 2 - visibleWidth(clippedLabel)));
|
|
51
|
+
const top = borderColor("╭") + clippedLabel + borderColor(tail + "╮");
|
|
52
|
+
const bottom = borderColor(`╰${"─".repeat(Math.max(0, w - 2))}╯`);
|
|
53
|
+
|
|
54
|
+
const rows: string[] = [bgLine(top)];
|
|
55
|
+
for (const line of spec.bodyLines) {
|
|
56
|
+
const body = theme.fg("dim", truncateToWidth(line, inner - 2));
|
|
57
|
+
const visLen = visibleWidth(body);
|
|
58
|
+
const padded = body + " ".repeat(Math.max(0, inner - visLen));
|
|
59
|
+
rows.push(bgLine(borderColor("│") + " " + padded + " " + borderColor("│")));
|
|
60
|
+
}
|
|
61
|
+
rows.push(bgLine(bottom));
|
|
62
|
+
return rows;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Custom-message component wrapper for the event card. Stateless; `render`
|
|
67
|
+
* is pure so it can be called with any width.
|
|
68
|
+
*/
|
|
69
|
+
export class EventCard implements Component {
|
|
70
|
+
private readonly theme: Theme | undefined;
|
|
71
|
+
private readonly spec: EventCardSpec;
|
|
72
|
+
|
|
73
|
+
constructor(theme: Theme | undefined, spec: EventCardSpec) {
|
|
74
|
+
this.theme = theme;
|
|
75
|
+
this.spec = spec;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
invalidate(): void {}
|
|
79
|
+
|
|
80
|
+
render(width: number): string[] {
|
|
81
|
+
return eventCardLines(this.theme, width, this.spec);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Split a message's text into (label, bodyLines) — the label is the first
|
|
87
|
+
* line, the body is every line after it (blank separator lines kept; the
|
|
88
|
+
* card drops nothing). Returns undefined label/body for empty text.
|
|
89
|
+
*/
|
|
90
|
+
export function eventCardSpec(text: string): EventCardSpec | undefined {
|
|
91
|
+
const lines = text.split("\n");
|
|
92
|
+
if (lines.length === 0 || lines.every((l) => l.length === 0)) return undefined;
|
|
93
|
+
const label = lines[0] ?? "";
|
|
94
|
+
const bodyLines = lines.slice(1);
|
|
95
|
+
return { label, bodyLines };
|
|
96
|
+
}
|
|
@@ -9,6 +9,7 @@ import { truncateToWidth, visibleWidth, matchesKey, type Component } from "@eare
|
|
|
9
9
|
import type { Theme, KeybindingsManager, ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
10
10
|
import { frameHeight, padToFrame } from "@maheidem/pi-panel-kit";
|
|
11
11
|
import type { PanelSnapshot } from "@maheidem/pi-panel-kit";
|
|
12
|
+
import type { ChildLiveState, ResolvedChildConfig } from "../types.ts";
|
|
12
13
|
|
|
13
14
|
export interface PeekViewState {
|
|
14
15
|
/** Header: run id + state/model. */
|
|
@@ -19,6 +20,10 @@ export interface PeekViewState {
|
|
|
19
20
|
lines: string[];
|
|
20
21
|
/** True while the run is still active (enables follow). */
|
|
21
22
|
live: boolean;
|
|
23
|
+
/** Live child state from the last answered get_state (best-effort). */
|
|
24
|
+
childLive?: ChildLiveState;
|
|
25
|
+
/** The resolved pin this run was launched with (receipt copy). */
|
|
26
|
+
childPinned?: ResolvedChildConfig;
|
|
22
27
|
}
|
|
23
28
|
|
|
24
29
|
export interface PeekViewHost {
|
|
@@ -112,9 +117,26 @@ export class PeekView implements Component {
|
|
|
112
117
|
const content = [...Array<string>(Math.max(0, CONTENT_ROWS - window.length)).fill(""), ...window];
|
|
113
118
|
|
|
114
119
|
const counts = [start > 0 ? `↑${start}` : "", end < total ? `↓${total - end}` : ""].filter(Boolean).join(" ");
|
|
120
|
+
const childLine = ((): string => {
|
|
121
|
+
const live = s.childLive;
|
|
122
|
+
const pinned = s.childPinned;
|
|
123
|
+
if (live?.model) {
|
|
124
|
+
const parts = [`child: ${live.model}`, "(live)"];
|
|
125
|
+
if (live.thinking) parts.push(`thinking ${live.thinking}`);
|
|
126
|
+
if (live.isStreaming) parts.push("streaming");
|
|
127
|
+
return parts.join(" · ");
|
|
128
|
+
}
|
|
129
|
+
if (pinned?.model) {
|
|
130
|
+
const parts = [`child: ${pinned.model}`];
|
|
131
|
+
parts.push(pinned.modelSource === "per-invocation" || pinned.modelSource === "project" ? "(pinned)" : "(inherited)");
|
|
132
|
+
if (pinned.thinking) parts.push(`thinking ${pinned.thinking}`);
|
|
133
|
+
return parts.join(" · ");
|
|
134
|
+
}
|
|
135
|
+
return "";
|
|
136
|
+
})();
|
|
115
137
|
const status = s.live
|
|
116
|
-
? `● live — following · j/k scroll · f re-follow · q close${counts ? ` · ${counts}` : ""}`
|
|
117
|
-
: `final — ${total} event(s)
|
|
138
|
+
? `● live — ${childLine ? `${childLine} · ` : ""}following · j/k scroll · f re-follow · q close${counts ? ` · ${counts}` : ""}`
|
|
139
|
+
: `final — ${total} event(s)${childLine ? ` · ${childLine}` : ""}${counts ? ` · ${counts}` : ""} · j/k scroll · q close`;
|
|
118
140
|
|
|
119
141
|
// Frame arithmetic through the canonical helper: the window is the
|
|
120
142
|
// grammar's detail block; the summary row occupies the grammar's
|
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
* delegate — live child feed panel for command-driven foreground runs.
|
|
3
3
|
*
|
|
4
4
|
* A window into the running child: identity + budgets header, elapsed-vs-hard
|
|
5
|
-
* progress, in-flight tools,
|
|
6
|
-
*
|
|
5
|
+
* progress, in-flight tools, the child's actual provider/model/thinking
|
|
6
|
+
* (live via get_state, else the receipt pin labeled `(pinned)`), and a
|
|
7
|
+
* bounded scrolling feed of the child's activity (tool starts/ends, assistant
|
|
8
|
+
* lines, provider errors, handoff).
|
|
7
9
|
*
|
|
8
10
|
* RENDER CONTRACT (hard-won): this runs as an inline `ctx.ui.custom`
|
|
9
11
|
* component, which REPLACES the composer region while a run is active. The
|
|
@@ -11,7 +13,9 @@
|
|
|
11
13
|
* changes between refreshes smears the previous frame over the input bar.
|
|
12
14
|
* So: constant height, blank-padded, and color ONLY inner text (never the
|
|
13
15
|
* border/padding structure, whose width Pi recomputes every frame).
|
|
14
|
-
* Cancels via Escape
|
|
16
|
+
* Cancels via Escape or x (armed), enters text via a single input row
|
|
17
|
+
* (enter → steer the child; x → arm cancel). Refresh cadence is owned by
|
|
18
|
+
* the host.
|
|
15
19
|
*/
|
|
16
20
|
|
|
17
21
|
import { truncateToWidth, visibleWidth, matchesKey, type Component } from "@earendil-works/pi-tui";
|
|
@@ -19,11 +23,12 @@ import type { Theme, KeybindingsManager, ThemeColor } from "@earendil-works/pi-c
|
|
|
19
23
|
import { formatDuration, formatTokens, shortModel } from "@maheidem/pi-panel-kit";
|
|
20
24
|
import { frameHeight, padToFrame } from "@maheidem/pi-panel-kit";
|
|
21
25
|
import type { PanelSnapshot } from "@maheidem/pi-panel-kit";
|
|
26
|
+
import type { ChildLiveState, ResolvedChildConfig } from "../types.ts";
|
|
22
27
|
|
|
23
28
|
export interface RunningViewState {
|
|
24
29
|
runId: string;
|
|
25
30
|
role: string;
|
|
26
|
-
/** Resolved child model ("provider/model"). */
|
|
31
|
+
/** Resolved child model ("provider/model") — kept for the head line. */
|
|
27
32
|
model?: string;
|
|
28
33
|
phase: string;
|
|
29
34
|
elapsedMs: number;
|
|
@@ -36,12 +41,18 @@ export interface RunningViewState {
|
|
|
36
41
|
openTools?: string[];
|
|
37
42
|
/** Pre-formatted feed lines (newest last); the panel slices/pads itself. */
|
|
38
43
|
feedLines: string[];
|
|
44
|
+
/** Live child state from the last answered get_state (best-effort). */
|
|
45
|
+
childLive?: ChildLiveState;
|
|
46
|
+
/** The resolved pin this run was launched with (shown when live is absent). */
|
|
47
|
+
childPinned?: ResolvedChildConfig;
|
|
39
48
|
}
|
|
40
49
|
|
|
41
50
|
export interface RunningViewHost {
|
|
42
51
|
theme: Theme;
|
|
43
52
|
keybindings: KeybindingsManager;
|
|
44
53
|
state(): RunningViewState;
|
|
54
|
+
/** Steer the live child (follow-up message). Absent → input is display-only. */
|
|
55
|
+
onSteer?(message: string): { ok: true } | { ok: false; error: string };
|
|
45
56
|
done(result: { cancelled?: boolean }): void;
|
|
46
57
|
}
|
|
47
58
|
|
|
@@ -49,9 +60,10 @@ export interface RunningViewHost {
|
|
|
49
60
|
* Feed rows inside the fixed frame; the rest are the fixed scaffold. The
|
|
50
61
|
* feed is a windowed content block — newest last, blank-padded on top,
|
|
51
62
|
* ALWAYS exactly FEED_ROWS tall (panel-frame rule: the builder windows the
|
|
52
|
-
* content; the frame never grows).
|
|
63
|
+
* content; the frame never grows). Reduced from 4 to 3 to make room for
|
|
64
|
+
* the child-state line and the input row while keeping PANEL_ROWS at 9.
|
|
53
65
|
*/
|
|
54
|
-
const FEED_ROWS =
|
|
66
|
+
const FEED_ROWS = 3;
|
|
55
67
|
|
|
56
68
|
/**
|
|
57
69
|
* Grammar slots the canonical panel-frame has that this composer-inline
|
|
@@ -62,7 +74,7 @@ const CLIPPED_GRAMMAR_ROWS = 2;
|
|
|
62
74
|
|
|
63
75
|
/**
|
|
64
76
|
* Constant panel height, DERIVED from the canonical panel-frame grammar
|
|
65
|
-
* (top + summary ×
|
|
77
|
+
* (top + summary ×4 + feed window + message/footer + nav + bottom) minus
|
|
66
78
|
* the clipped slots. Must evaluate to 9: the composer-region component has
|
|
67
79
|
* to render a fixed height every frame (see RENDER CONTRACT above).
|
|
68
80
|
*/
|
|
@@ -70,13 +82,17 @@ const PANEL_ROWS =
|
|
|
70
82
|
frameHeight({
|
|
71
83
|
title: "",
|
|
72
84
|
sections: [],
|
|
73
|
-
summaryLines: ["", "", ""],
|
|
85
|
+
summaryLines: ["", "", "", ""],
|
|
74
86
|
detailLines: Array.from({ length: FEED_ROWS }, () => ""),
|
|
75
87
|
}) - CLIPPED_GRAMMAR_ROWS;
|
|
76
88
|
|
|
89
|
+
const ERROR_FLASH_MS = 3000;
|
|
90
|
+
|
|
77
91
|
export class RunningView implements Component {
|
|
78
92
|
private readonly host: RunningViewHost;
|
|
79
93
|
private cancelArmed = false;
|
|
94
|
+
private draft = "";
|
|
95
|
+
private lastError: { message: string; at: number } | undefined;
|
|
80
96
|
|
|
81
97
|
constructor(host: RunningViewHost) {
|
|
82
98
|
this.host = host;
|
|
@@ -89,6 +105,13 @@ export class RunningView implements Component {
|
|
|
89
105
|
matchesKey(data, "escape") ||
|
|
90
106
|
matchesKey(data, "q");
|
|
91
107
|
if (isCancelKey) {
|
|
108
|
+
// Discard an in-progress draft first — the cancel is destructive
|
|
109
|
+
// to the child, not to the text the user typed.
|
|
110
|
+
if (this.draft.length > 0) {
|
|
111
|
+
this.draft = "";
|
|
112
|
+
this.invalidate();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
92
115
|
// Cancel is destructive to the child — require a second press.
|
|
93
116
|
if (this.cancelArmed) {
|
|
94
117
|
this.host.done({ cancelled: true });
|
|
@@ -96,6 +119,44 @@ export class RunningView implements Component {
|
|
|
96
119
|
this.cancelArmed = true;
|
|
97
120
|
this.invalidate();
|
|
98
121
|
}
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (matchesKey(data, "backspace") || matchesKey(data, "delete")) {
|
|
125
|
+
if (this.draft.length > 0) {
|
|
126
|
+
this.draft = this.draft.slice(0, -1);
|
|
127
|
+
this.invalidate();
|
|
128
|
+
}
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (matchesKey(data, "enter") || matchesKey(data, "return")) {
|
|
132
|
+
const message = this.draft.trim();
|
|
133
|
+
if (message && this.host.onSteer) {
|
|
134
|
+
const res = this.host.onSteer(message);
|
|
135
|
+
if (res.ok) {
|
|
136
|
+
this.draft = "";
|
|
137
|
+
this.lastError = undefined;
|
|
138
|
+
} else {
|
|
139
|
+
this.lastError = { message: res.error, at: Date.now() };
|
|
140
|
+
}
|
|
141
|
+
this.invalidate();
|
|
142
|
+
}
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
// x arms cancel — but only while the user has not started composing
|
|
146
|
+
// (a message containing "x" is a char, not a cancel gesture).
|
|
147
|
+
if (this.draft.length === 0 && matchesKey(data, "x")) {
|
|
148
|
+
if (this.cancelArmed) {
|
|
149
|
+
this.host.done({ cancelled: true });
|
|
150
|
+
} else {
|
|
151
|
+
this.cancelArmed = true;
|
|
152
|
+
this.invalidate();
|
|
153
|
+
}
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
// Printable editing: append the char to the draft.
|
|
157
|
+
if (data.length === 1 && (data >= " ")) {
|
|
158
|
+
this.draft += data;
|
|
159
|
+
this.invalidate();
|
|
99
160
|
}
|
|
100
161
|
}
|
|
101
162
|
|
|
@@ -103,6 +164,25 @@ export class RunningView implements Component {
|
|
|
103
164
|
// No internal cache: state() is cheap and host-driven refresh is 1 s.
|
|
104
165
|
}
|
|
105
166
|
|
|
167
|
+
private childStateLine(s: RunningViewState): string {
|
|
168
|
+
const live = s.childLive;
|
|
169
|
+
const pinned = s.childPinned;
|
|
170
|
+
if (live?.model) {
|
|
171
|
+
const parts = [`child: ${live.model}`, "(live)"];
|
|
172
|
+
if (live.thinking) parts.push(`thinking ${live.thinking}`);
|
|
173
|
+
if (live.isStreaming) parts.push("streaming");
|
|
174
|
+
return parts.join(" · ");
|
|
175
|
+
}
|
|
176
|
+
if (pinned?.model) {
|
|
177
|
+
const parts = [`child: ${pinned.model}`];
|
|
178
|
+
parts.push(pinned.modelSource === "per-invocation" || pinned.modelSource === "project" ? "(pinned)" : "(inherited)");
|
|
179
|
+
if (pinned.thinking) parts.push(`thinking ${pinned.thinking}`);
|
|
180
|
+
return parts.join(" · ");
|
|
181
|
+
}
|
|
182
|
+
if (s.model) return `child: ${s.model}`;
|
|
183
|
+
return "child: —";
|
|
184
|
+
}
|
|
185
|
+
|
|
106
186
|
render(width: number): string[] {
|
|
107
187
|
const s = this.host.state();
|
|
108
188
|
const theme = this.host.theme;
|
|
@@ -147,7 +227,27 @@ export class RunningView implements Component {
|
|
|
147
227
|
const feed = s.feedLines.slice(-FEED_ROWS);
|
|
148
228
|
const feedRows: string[] = [...Array<string>(Math.max(0, FEED_ROWS - feed.length)).fill(""), ...feed];
|
|
149
229
|
|
|
150
|
-
|
|
230
|
+
// Child-state line (live over pinned over inherited).
|
|
231
|
+
const childLine = this.childStateLine(s);
|
|
232
|
+
const childColor: ThemeColor = s.childLive?.model ? "success" : "dim";
|
|
233
|
+
|
|
234
|
+
// Input row: draft + hint (and a transient error flash).
|
|
235
|
+
const flash = this.lastError && Date.now() - this.lastError.at < ERROR_FLASH_MS ? this.lastError : undefined;
|
|
236
|
+
let inputText: string;
|
|
237
|
+
let inputColor: ThemeColor = "accent";
|
|
238
|
+
if (flash) {
|
|
239
|
+
inputText = `» ${flash.message}`;
|
|
240
|
+
inputColor = "error";
|
|
241
|
+
} else {
|
|
242
|
+
const hint = this.cancelArmed
|
|
243
|
+
? "ESC/x again: CONFIRM cancel"
|
|
244
|
+
: this.draft.length
|
|
245
|
+
? "enter steer · esc discard"
|
|
246
|
+
: "enter steer · x arm cancel";
|
|
247
|
+
inputText = this.draft.length ? `» ${this.draft}` : `» ${hint}`;
|
|
248
|
+
if (this.draft.length === 0) inputColor = this.cancelArmed ? "error" : "dim";
|
|
249
|
+
}
|
|
250
|
+
if (this.cancelArmed && !flash) inputColor = "error";
|
|
151
251
|
|
|
152
252
|
// In-flight tools are the NORMAL state of a running child, not a
|
|
153
253
|
// problem: amber ("warning") is read by humans as trouble, so this row
|
|
@@ -155,18 +255,20 @@ export class RunningView implements Component {
|
|
|
155
255
|
// failed (see skills/pi-extension-builder OPERATIONAL-EXTENSIONS.md).
|
|
156
256
|
const inFlightColor: ThemeColor | undefined = openTools.length > 0 ? "dim" : undefined;
|
|
157
257
|
|
|
158
|
-
// Fixed scaffold summary rows — always exactly
|
|
159
|
-
// in-flight), the canonical frame's summaryLines slots.
|
|
258
|
+
// Fixed scaffold summary rows — always exactly 4 (header, progress,
|
|
259
|
+
// in-flight, child state), the canonical frame's summaryLines slots.
|
|
160
260
|
const summary: Array<[string, ThemeColor?]> = [
|
|
161
261
|
[head],
|
|
162
262
|
[progress, "accent"],
|
|
163
263
|
[inFlight, inFlightColor],
|
|
264
|
+
[childLine, childColor],
|
|
164
265
|
];
|
|
165
266
|
|
|
166
267
|
// Frame arithmetic through the canonical helper: the feed window is
|
|
167
|
-
// the grammar's detail block, the
|
|
168
|
-
// is a no-op while the builder invariant holds
|
|
169
|
-
// FEED_ROWS) and is the tripwire the moment any slot
|
|
268
|
+
// the grammar's detail block, the input row rides the grammar's
|
|
269
|
+
// message slot. padToFrame is a no-op while the builder invariant holds
|
|
270
|
+
// (window exactly FEED_ROWS) and is the tripwire the moment any slot
|
|
271
|
+
// count drifts.
|
|
170
272
|
const snapshot: PanelSnapshot = {
|
|
171
273
|
title,
|
|
172
274
|
sections: [],
|
|
@@ -175,12 +277,13 @@ export class RunningView implements Component {
|
|
|
175
277
|
};
|
|
176
278
|
const windowRows = padToFrame(snapshot, PANEL_ROWS + CLIPPED_GRAMMAR_ROWS).detailLines ?? [];
|
|
177
279
|
|
|
178
|
-
// Assemble a fixed-height body: header, progress, in-flight,
|
|
280
|
+
// Assemble a fixed-height body: header, progress, in-flight, child
|
|
281
|
+
// state, feed block, input row.
|
|
179
282
|
const lines: string[] = [
|
|
180
283
|
top,
|
|
181
284
|
...summary.map(([t, c]) => pad(t, c)),
|
|
182
285
|
...windowRows.map((l) => pad(l, l ? "text" : undefined)),
|
|
183
|
-
pad(
|
|
286
|
+
pad(inputText, inputColor),
|
|
184
287
|
bottom,
|
|
185
288
|
];
|
|
186
289
|
|
package/package.json
CHANGED
package/roles/general.md
CHANGED
|
@@ -20,6 +20,11 @@ message is the only thing returned to it, bounded and summarized.
|
|
|
20
20
|
treat this role's instructions as the parent's conversation.
|
|
21
21
|
- Your tool ceiling is closed: read, bash, edit, write, grep, find, ls.
|
|
22
22
|
There is no browser, no network tool, and no delegate tool.
|
|
23
|
+
- Execution mode: your parent may launch you in the foreground (interactive —
|
|
24
|
+
it may steer you with follow-up messages mid-run; treat those as
|
|
25
|
+
corrections to the delegated task, keeping the task's goal) or in the
|
|
26
|
+
background (fire-and-forget — you will not be steered; your final handoff
|
|
27
|
+
is the only hand-back). Either way, work only on the delegated task.
|
|
23
28
|
|
|
24
29
|
## Output contract — the mandatory handoff tool
|
|
25
30
|
|
package/roles/research.md
CHANGED
|
@@ -24,6 +24,11 @@ message is the only thing returned to it, bounded and summarized.
|
|
|
24
24
|
- Your tool ceiling is closed: read, grep, find, ls, mcp, mcpScript,
|
|
25
25
|
mcp__firecrawl, mcp__reddit. There is no bash, no write/edit, and no
|
|
26
26
|
delegate tool.
|
|
27
|
+
- Execution mode: your parent may launch you in the foreground (interactive —
|
|
28
|
+
it may steer you with follow-up messages mid-run; treat those as
|
|
29
|
+
corrections to the delegated task, keeping the task's goal) or in the
|
|
30
|
+
background (fire-and-forget — you will not be steered; your final handoff
|
|
31
|
+
is the only hand-back). Either way, work only on the delegated task.
|
|
27
32
|
|
|
28
33
|
## Output contract — the mandatory handoff tool
|
|
29
34
|
|