@maheidem/pi-delegate 0.8.0 → 0.10.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.
@@ -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, and a bounded scrolling feed of the child's
6
- * activity (tool starts/ends, assistant lines, provider errors, handoff).
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 twice (armed). Refresh cadence is owned by the host.
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 = 4;
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 ×3 + feed window + message/footer + nav + bottom) minus
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
- const footer = this.cancelArmed ? fg("error", "ESC again: CONFIRM cancel") : "esc/q cancel (press twice)";
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 3 (header, progress,
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 footer its message line. padToFrame
168
- // is a no-op while the builder invariant holds (window exactly
169
- // FEED_ROWS) and is the tripwire the moment any slot count drifts.
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, feed block, footer.
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(footer, this.cancelArmed ? "error" : "dim"),
286
+ pad(inputText, inputColor),
184
287
  bottom,
185
288
  ];
186
289
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maheidem/pi-delegate",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "type": "module",
5
5
  "description": "Foreground isolated delegation and strict coordinator mode for the Pi coding agent.",
6
6
  "license": "MIT",
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