@ferris1225/pi-subagents 5.0.6 → 6.0.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.
Files changed (43) hide show
  1. package/CHANGELOG.md +89 -0
  2. package/README.md +187 -97
  3. package/index.ts +1 -1
  4. package/package.json +4 -9
  5. package/src/configuration/role-migration.ts +2 -2
  6. package/src/configuration/setup.ts +4 -2
  7. package/src/configuration/ui.ts +144 -132
  8. package/src/delegation/agents.ts +15 -22
  9. package/src/delegation/dispatch.ts +17 -122
  10. package/src/delegation/prompt.ts +13 -12
  11. package/src/execution/cli-run.ts +112 -39
  12. package/src/execution/process-tree.ts +12 -8
  13. package/src/execution/run-control.ts +11 -3
  14. package/src/execution/spawn.ts +24 -7
  15. package/src/isolation/cleanup.ts +31 -12
  16. package/src/isolation/managed-paths.ts +6 -4
  17. package/src/isolation/manifest-mutation.ts +89 -0
  18. package/src/isolation/recovery.ts +33 -29
  19. package/src/isolation/temp-hygiene.ts +1 -1
  20. package/src/lifecycle/await-results.ts +114 -0
  21. package/src/lifecycle/dispatch-core.ts +7 -1
  22. package/src/lifecycle/durable.ts +83 -39
  23. package/src/lifecycle/runtime.ts +12 -4
  24. package/src/lifecycle/stop.ts +2 -2
  25. package/src/lifecycle/thread-lifecycle.ts +10 -5
  26. package/src/lifecycle/thread-restore.ts +26 -25
  27. package/src/lifecycle/thread-shared.ts +15 -3
  28. package/src/lifecycle/tools.ts +90 -33
  29. package/src/presentation/announcements.ts +8 -2
  30. package/src/presentation/monitor.ts +5 -3
  31. package/src/presentation/widget.ts +157 -113
  32. package/src/workflow/acceptance.ts +67 -0
  33. package/src/workflow/durable.ts +34 -14
  34. package/src/workflow/engine.ts +161 -51
  35. package/src/workflow/plan.ts +25 -4
  36. package/src/workflow/state.ts +6 -0
  37. package/src/workflow/templates.ts +156 -0
  38. package/src/workflow/tool.ts +65 -109
  39. package/skills/workflow/SKILL.md +0 -268
  40. package/src/workflow/files.ts +0 -236
  41. package/workflows/code-review.md +0 -38
  42. package/workflows/migration.md +0 -42
  43. package/workflows/research.md +0 -48
@@ -1,34 +1,32 @@
1
- /** Searchable pickers and the /subagents-setup role grid. */
1
+ /** Searchable pickers and the /subagents-setup role register. */
2
2
 
3
3
  import {
4
- Input,
5
- fuzzyFilter,
6
- matchesKey,
7
- truncateToWidth,
8
- visibleWidth,
9
- wrapTextWithAnsi,
10
- type Component,
11
- type Focusable,
12
- type KeybindingsManager,
13
- type SelectItem,
14
- type TUI,
4
+ Input, fuzzyFilter, matchesKey, truncateToWidth, visibleWidth, wrapTextWithAnsi,
5
+ type Component, type Focusable, type KeybindingsManager, type SelectItem, type TUI,
15
6
  } from "@earendil-works/pi-tui";
16
7
  import type { ExtensionCommandContext, Theme } from "@earendil-works/pi-coding-agent";
17
8
 
18
9
  type PickerContext = Pick<ExtensionCommandContext, "mode" | "ui">;
19
10
  const PAGE_SIZE = 8;
20
11
 
12
+ /** Leave room for the ten-line widget, its leading spacer, the two-line
13
+ * host footer and one transcript row. Tiny terminals retain an input slot. */
14
+ function setupHeight(tui: TUI): number {
15
+ return Math.max(3, (tui.terminal?.rows ?? 28) - 14);
16
+ }
17
+
21
18
  interface PickerOptions {
22
19
  title: string;
23
20
  hint: string;
24
21
  initialValue?: string;
25
22
  }
26
23
 
27
- /** One keyboard path for settings and their nested pickers. Input supplies
28
- * paste, Unicode editing, horizontal scrolling, and IME cursor placement. */
24
+ /** Input owns Unicode editing, paste, horizontal scrolling and the IME cursor.
25
+ * Selection and paging use the host's configurable keybindings. */
29
26
  class Picker implements Component, Focusable {
30
27
  private _focused = false;
31
28
  private cursor = 0;
29
+ private pageRows = PAGE_SIZE;
32
30
  private filtered: SelectItem[];
33
31
  private readonly search = new Input({ prompt: "Search: ", placeholder: "type to filter" });
34
32
 
@@ -40,12 +38,8 @@ class Picker implements Component, Focusable {
40
38
  private readonly done: (value: string | undefined) => void;
41
39
 
42
40
  constructor(
43
- items: SelectItem[],
44
- options: PickerOptions,
45
- tui: TUI,
46
- theme: Theme,
47
- keybindings: KeybindingsManager,
48
- done: (value: string | undefined) => void,
41
+ items: SelectItem[], options: PickerOptions, tui: TUI, theme: Theme,
42
+ keybindings: KeybindingsManager, done: (value: string | undefined) => void,
49
43
  ) {
50
44
  this.items = items;
51
45
  this.options = options;
@@ -59,46 +53,43 @@ class Picker implements Component, Focusable {
59
53
  }
60
54
 
61
55
  get focused(): boolean { return this._focused; }
62
- set focused(value: boolean) {
63
- this._focused = value;
64
- this.search.focused = value;
65
- }
66
-
67
- private pageSize(): number {
68
- // Leave room for headings, help, and the selected item's details.
69
- return Math.max(1, Math.min(PAGE_SIZE, (this.tui.terminal?.rows ?? 28) - 13));
70
- }
56
+ set focused(value: boolean) { this._focused = value; this.search.focused = value; }
71
57
 
72
58
  render(width: number): string[] {
73
- if (width <= 0) return [""];
59
+ if (width <= 0) return [];
74
60
  const theme = this.theme;
75
61
  const dim = (text: string) => theme.fg("dim", text);
76
- const fit = (text: string) => truncateToWidth(text, width, "");
77
- const border = theme.fg("border", "─".repeat(width));
78
- const lines = [border, theme.fg("accent", theme.bold(this.options.title)), dim(this.options.hint)];
79
- lines.push(...this.search.render(width));
80
- const pageSize = this.pageSize();
81
- const start = Math.max(0, Math.min(this.cursor - Math.floor(pageSize / 2), this.filtered.length - pageSize));
82
- const visible = this.filtered.slice(start, start + pageSize);
83
- if (!visible.length) lines.push("", theme.fg("warning", " No matches. Change or clear the search."));
84
- for (const [index, item] of visible.entries()) {
85
- const selected = start + index === this.cursor;
86
- const prefix = selected ? theme.fg("accent", "› ") : " ";
87
- const label = selected ? theme.fg("accent", theme.bold(item.label)) : item.label;
88
- lines.push(prefix + label);
62
+ const height = setupHeight(this.tui);
63
+ if (height < 8) {
64
+ this.pageRows = 1;
65
+ const item = this.filtered[this.cursor];
66
+ return [theme.bold(this.options.title), ...this.search.render(width),
67
+ item ? theme.fg("accent", "› ") + theme.underline(item.label) : "No matches. Clear or change the search."]
68
+ .map((line) => truncateToWidth(line, width, "…"));
89
69
  }
70
+ const lines = [theme.bold("Subagents · " + this.options.title)];
71
+ if (this.options.hint && height >= 16) lines.push(...wrapTextWithAnsi(this.options.hint, width).slice(0, 2).map(dim));
72
+ lines.push(...this.search.render(width), "");
73
+ const footer = [dim("↑/↓ move · PgUp/PgDn page"), dim("Enter select · Esc back")];
90
74
  const item = this.filtered[this.cursor];
91
- lines.push("", border);
92
- if (item) {
93
- // Details have their own lines, so long model ids cannot hide capabilities.
94
- const detail = item.description ?? "";
95
- lines.push(...wrapTextWithAnsi(detail, Math.max(1, width)).slice(0, 3).map(dim));
96
- lines.push(dim(
97
- (this.cursor + 1) + "/" + this.filtered.length + " matches · ↑/↓ move · PgUp/PgDn page",
98
- ));
75
+ // The complete selected identity lives beside its capabilities, even when
76
+ // a long provider/model id was truncated in the list above.
77
+ const details = item ? ["", ...wrapTextWithAnsi(item.label, width).slice(0, 2),
78
+ ...wrapTextWithAnsi(item.description ?? "", width).slice(0, 3).map(dim)] : [];
79
+ const detailBudget = Math.max(0, Math.min(details.length, height - lines.length - footer.length - 2));
80
+ const visibleDetails = details.slice(0, detailBudget);
81
+ this.pageRows = Math.max(1, Math.min(PAGE_SIZE, height - lines.length - footer.length - visibleDetails.length - 1));
82
+ const start = Math.max(0, Math.min(this.cursor - Math.floor(this.pageRows / 2), this.filtered.length - this.pageRows));
83
+ const visible = this.filtered.slice(start, start + this.pageRows);
84
+ if (!visible.length) lines.push(dim(this.search.getValue() ? "No matches. Clear or change the search." : "No choices available."));
85
+ for (const [index, choice] of visible.entries()) {
86
+ const selected = start + index === this.cursor;
87
+ const label = truncateToWidth(choice.label, Math.max(0, width - 2), "…");
88
+ lines.push(selected ? theme.fg("accent", "› ") + theme.underline(theme.bold(label)) : " " + label);
99
89
  }
100
- lines.push(dim("Enter select · Esc back"));
101
- return lines.map(fit);
90
+ if (item) lines.push(dim((this.cursor + 1) + "/" + this.filtered.length + " matches"));
91
+ lines.push(...visibleDetails, ...footer);
92
+ return lines.map((line) => truncateToWidth(line, width, "…"));
102
93
  }
103
94
 
104
95
  handleInput(data: string): void {
@@ -109,9 +100,9 @@ class Picker implements Component, Focusable {
109
100
  } else if (kb.matches(data, "tui.select.down")) {
110
101
  if (count) this.cursor = (this.cursor + 1) % count;
111
102
  } else if (kb.matches(data, "tui.select.pageUp")) {
112
- this.cursor = Math.max(0, this.cursor - this.pageSize());
103
+ this.cursor = Math.max(0, this.cursor - this.pageRows);
113
104
  } else if (kb.matches(data, "tui.select.pageDown")) {
114
- this.cursor = Math.max(0, Math.min(count - 1, this.cursor + this.pageSize()));
105
+ this.cursor = Math.max(0, Math.min(count - 1, this.cursor + this.pageRows));
115
106
  } else if (kb.matches(data, "tui.select.confirm")) {
116
107
  const item = this.filtered[this.cursor];
117
108
  if (item) this.done(item.value);
@@ -124,8 +115,7 @@ class Picker implements Component, Focusable {
124
115
  this.search.handleInput(data);
125
116
  if (before !== this.search.getValue()) {
126
117
  const query = this.search.getValue().trim();
127
- this.filtered = query ? fuzzyFilter(this.items, query,
128
- (item) => [item.value, item.label, item.description].join(" ")) : this.items;
118
+ this.filtered = query ? fuzzyFilter(this.items, query, (item) => [item.value, item.label, item.description].join(" ")) : this.items;
129
119
  this.cursor = 0;
130
120
  }
131
121
  }
@@ -137,11 +127,7 @@ class Picker implements Component, Focusable {
137
127
 
138
128
  /** Single-select with fuzzy search and paging. Esc returns without a change. */
139
129
  export function promptSelectOne(
140
- ctx: PickerContext,
141
- title: string,
142
- hint: string,
143
- items: SelectItem[],
144
- initialValue?: string,
130
+ ctx: PickerContext, title: string, hint: string, items: SelectItem[], initialValue?: string,
145
131
  ): Promise<string | undefined> {
146
132
  if (ctx.mode !== "tui") {
147
133
  ctx.ui.notify("/subagents-setup requires Pi's interactive TUI.", "error");
@@ -151,37 +137,29 @@ export function promptSelectOne(
151
137
  new Picker(items, { title, hint, initialValue }, tui, theme, keybindings, done));
152
138
  }
153
139
 
154
- /** One role per row, with Model, Thinking and Reset side by side: the whole
155
- * configuration is visible at once, so a model choice and its clamped thinking
156
- * level are compared in place instead of through nested screens. */
157
140
  export interface RoleGridRow {
158
141
  role: string;
159
142
  model: string;
143
+ /** Effective level, optionally followed by the requested level when clamped. */
160
144
  thinking: string;
161
- /** Whether the thinking level is a user override (shown with a marker). */
162
145
  thinkingOverridden?: boolean;
163
- /** Why this row matters; shown for the focused row only. */
164
146
  description: string;
165
147
  }
166
-
167
- export interface RoleGridFocus {
168
- row?: number;
169
- column?: number;
170
- }
171
-
148
+ export interface RoleGridFocus { row?: number; column?: number; }
172
149
  const GRID_COLUMNS = ["model", "thinking", "reset"] as const;
173
150
  export type RoleGridColumn = (typeof GRID_COLUMNS)[number];
151
+ export interface RoleGridChoice { row: number; column: number; role: string; field: RoleGridColumn; }
174
152
 
175
- export interface RoleGridChoice {
176
- row: number;
177
- column: number;
178
- role: string;
179
- field: RoleGridColumn;
180
- }
181
-
153
+ /** A role register beside the selected role's settings; narrow terminals stack
154
+ * the same regions. Full effective values are never squeezed into grid cells. */
182
155
  class RoleGrid implements Component {
183
156
  private row: number;
184
157
  private column: number;
158
+ private scrollTop = 0;
159
+ private pageRows = 1;
160
+ private viewport = "";
161
+ private revealField = true;
162
+
185
163
  private readonly rows: RoleGridRow[];
186
164
  private readonly status: string;
187
165
  private readonly tui: TUI;
@@ -190,13 +168,8 @@ class RoleGrid implements Component {
190
168
  private readonly done: (choice: RoleGridChoice | undefined) => void;
191
169
 
192
170
  constructor(
193
- rows: RoleGridRow[],
194
- status: string,
195
- focus: RoleGridFocus,
196
- tui: TUI,
197
- theme: Theme,
198
- keybindings: KeybindingsManager,
199
- done: (choice: RoleGridChoice | undefined) => void,
171
+ rows: RoleGridRow[], status: string, focus: RoleGridFocus, tui: TUI, theme: Theme,
172
+ keybindings: KeybindingsManager, done: (choice: RoleGridChoice | undefined) => void,
200
173
  ) {
201
174
  this.rows = rows;
202
175
  this.status = status;
@@ -208,56 +181,99 @@ class RoleGrid implements Component {
208
181
  this.column = Math.min(Math.max(0, focus.column ?? 0), GRID_COLUMNS.length - 1);
209
182
  }
210
183
 
211
- /** Column budget: the role name and the action stay readable, the two
212
- * measured values share what is left. */
213
- private widths(width: number): number[] {
214
- const role = Math.min(12, Math.max(4, Math.floor(width * 0.16)));
215
- const reset = 6;
216
- const gap = 2;
217
- // The row prefix ("› ") and the three gaps also spend columns.
218
- const available = Math.max(0, width - 2 - role - reset - gap * 3);
219
- const model = Math.max(0, Math.floor(available * 0.55));
220
- const thinking = Math.max(0, available - model);
221
- return [role, model, thinking, reset];
184
+ private settings(width: number): { lines: string[]; start: number; end: number } {
185
+ const focused = this.rows[this.row];
186
+ if (!focused) return { lines: [this.theme.fg("dim", "No roles configured.")], start: 0, end: 1 };
187
+ const theme = this.theme;
188
+ const dim = (text: string) => theme.fg("dim", text);
189
+ const field = GRID_COLUMNS[this.column]!;
190
+ const label = field === "thinking" ? "Thinking (reasoning effort)" : field === "reset" ? "Reset" : "Model";
191
+ const lines = [theme.bold(focused.role + " · " + label), ""];
192
+ const starts: number[] = [];
193
+ const setting = (column: number, name: string, hint: string): void => {
194
+ starts.push(lines.length);
195
+ const active = this.column === column;
196
+ lines.push((active ? theme.fg("accent", "› ") : " ") + (active ? theme.underline(theme.bold(name)) : name) + dim(" · " + hint));
197
+ };
198
+ const value = (text: string): void => {
199
+ lines.push(...wrapTextWithAnsi(text, Math.max(1, width - 2)).map((line) => " " + line));
200
+ };
201
+ setting(0, "Model", "Enter edit");
202
+ value(focused.model);
203
+ setting(1, "Thinking", "effective");
204
+ value(focused.thinking);
205
+ lines.push(dim(" Source: " + (focused.thinkingOverridden ? "override *" : "role default")));
206
+ setting(2, "Reset", "Enter restore defaults");
207
+ const end = lines.length;
208
+ lines.push("", ...wrapTextWithAnsi(focused.description, width).map(dim));
209
+ return { lines, start: starts[this.column]!, end: starts[this.column + 1] ?? end };
222
210
  }
223
211
 
224
212
  render(width: number): string[] {
225
- if (width <= 0) return [""];
213
+ if (width <= 0) return [];
226
214
  const theme = this.theme;
227
215
  const dim = (text: string) => theme.fg("dim", text);
228
- const [roleWidth, modelWidth, thinkingWidth, resetWidth] = this.widths(width);
229
- const cell = (text: string, size: number, active: boolean): string => {
230
- const fitted = truncateToWidth(text, size, "…");
231
- const pad = " ".repeat(Math.max(0, size - visibleWidth(fitted)));
232
- return active ? theme.bg("selectedBg", theme.fg("accent", theme.bold(fitted + pad))) : fitted + pad;
233
- };
234
- const lines = [theme.fg("border", "─".repeat(width)), theme.fg("accent", theme.bold("Subagents · Roles")), dim("Model · Thinking · Reset per role · " + this.status), ""];
235
- const header = ["Role", "Model", "Thinking", ""]
236
- .map((title, index) => cell(title, [roleWidth, modelWidth, thinkingWidth, resetWidth][index]!, false))
237
- .join(" ");
238
- lines.push(" " + dim(header));
239
- for (const [index, row] of this.rows.entries()) {
216
+ const lines = [theme.bold("Subagents · Roles"), dim(this.status), ""];
217
+ const height = setupHeight(this.tui);
218
+ const wide = width >= 88;
219
+ // The two panes close exactly: roleWidth + 3-column gutter + detailWidth = width.
220
+ const roleWidth = wide ? Math.min(24, Math.floor(width * 0.24)) : width;
221
+ const roles: string[] = [];
222
+ for (const [index, role] of this.rows.entries()) {
240
223
  const selected = index === this.row;
241
- const cells = [row.role, row.model, row.thinking + (row.thinkingOverridden ? "*" : ""), "reset"]
242
- .map((text, column) => cell(text, [roleWidth, modelWidth, thinkingWidth, resetWidth][column]!, selected && this.column === column - 1));
243
- lines.push((selected ? theme.fg("accent", "› ") : " ") + cells.join(" "));
224
+ const name = selected ? theme.bold(role.role) : role.role;
225
+ roles.push((selected ? "› " : " ") + name);
226
+ if (wide) roles.push(dim(" " + truncateToWidth(role.model, roleWidth - 2, "…")));
244
227
  }
245
- const focused = this.rows[this.row];
246
- lines.push("", theme.fg("border", "─".repeat(width)));
247
- if (focused) {
248
- const field = GRID_COLUMNS[this.column]!;
249
- const label = field === "model" ? "Model" : field === "thinking" ? "Thinking (reasoning effort)" : "Reset";
250
- lines.push(theme.fg("accent", `${focused.role} · ${label} · Enter ${field === "reset" ? "restore defaults" : "edit"}`));
251
- lines.push(...wrapTextWithAnsi(focused.description, Math.max(1, width)).slice(0, 3).map(dim));
228
+ const details = this.settings(wide ? width - roleWidth - 3 : width);
229
+ const detailOffset = wide ? 0 : roles.length + 1;
230
+ const body: string[] = [];
231
+ if (wide) {
232
+ for (let i = 0; i < Math.max(roles.length, details.lines.length); i++) {
233
+ const left = truncateToWidth(roles[i] ?? "", roleWidth, "…");
234
+ body.push(left + " ".repeat(roleWidth - visibleWidth(left) + 3) + (details.lines[i] ?? ""));
235
+ }
236
+ } else {
237
+ body.push(...roles, "", ...details.lines);
252
238
  }
253
- lines.push(dim("↑/↓ role · ←/→ column · Enter edit · Esc close"));
254
- return lines.map((line) => truncateToWidth(line, width, "…"));
239
+ const footer = [dim("↑/↓ role · ←/→ field"), dim("Enter edit · Esc close")];
240
+ if (lines.length + body.length + footer.length + 1 <= height) {
241
+ this.scrollTop = 0;
242
+ this.revealField = true;
243
+ return [...lines, ...body, "", ...footer].map((line) => truncateToWidth(line, width, "…"));
244
+ }
245
+ // Pin the selected identity while scrolling the existing pane content.
246
+ // The field's complete value and provenance remain reachable by paging.
247
+ const compact = height < 8;
248
+ const header = compact ? [details.lines[0]!] : [...lines.slice(0, 2), details.lines[0]!];
249
+ const controls = compact ? [dim("PgUp/Dn scroll · Enter edit · Esc")]
250
+ : [footer[0]!, dim("PgUp/Dn scroll · Enter edit · Esc")];
251
+ this.pageRows = Math.max(1, height - header.length - controls.length);
252
+ const viewport = width + ":" + this.pageRows;
253
+ if (this.revealField || this.viewport !== viewport) {
254
+ const start = detailOffset + details.start;
255
+ const end = detailOffset + details.end;
256
+ // One-line viewports show the value rather than repeating its label.
257
+ this.scrollTop = end - start > this.pageRows ? start + Number(this.pageRows === 1 && end > start + 1)
258
+ : Math.max(0, end - this.pageRows);
259
+ }
260
+ this.viewport = viewport;
261
+ this.revealField = false;
262
+ this.scrollTop = Math.max(0, Math.min(this.scrollTop, body.length - this.pageRows));
263
+ return [...header, ...body.slice(this.scrollTop, this.scrollTop + this.pageRows), ...controls]
264
+ .map((line) => truncateToWidth(line, width, "…"));
255
265
  }
256
266
 
257
267
  handleInput(data: string): void {
258
268
  const kb = this.keybindings;
259
269
  const rows = this.rows.length;
260
- if (kb.matches(data, "tui.select.up")) {
270
+ const previousRow = this.row;
271
+ const previousColumn = this.column;
272
+ if (kb.matches(data, "tui.select.pageUp")) {
273
+ this.scrollTop -= this.pageRows;
274
+ } else if (kb.matches(data, "tui.select.pageDown")) {
275
+ this.scrollTop += this.pageRows;
276
+ } else if (kb.matches(data, "tui.select.up")) {
261
277
  if (rows) this.row = (this.row - 1 + rows) % rows;
262
278
  } else if (kb.matches(data, "tui.select.down")) {
263
279
  if (rows) this.row = (this.row + 1) % rows;
@@ -274,21 +290,17 @@ class RoleGrid implements Component {
274
290
  } else if (kb.matches(data, "tui.select.cancel")) {
275
291
  this.done(undefined);
276
292
  return;
277
- } else {
278
- return;
279
- }
293
+ } else return;
294
+ if (this.row !== previousRow || this.column !== previousColumn) this.revealField = true;
280
295
  this.tui.requestRender();
281
296
  }
282
297
 
283
298
  invalidate(): void {}
284
299
  }
285
300
 
286
- /** Role × setting grid. Esc closes without a change. */
301
+ /** Role × setting navigation. Esc closes without a change. */
287
302
  export function promptRoleGrid(
288
- ctx: PickerContext,
289
- rows: RoleGridRow[],
290
- focus: RoleGridFocus = {},
291
- status = "Changes save immediately",
303
+ ctx: PickerContext, rows: RoleGridRow[], focus: RoleGridFocus = {}, status = "Changes save immediately",
292
304
  ): Promise<RoleGridChoice | undefined> {
293
305
  if (ctx.mode !== "tui") {
294
306
  ctx.ui.notify("/subagents-setup requires Pi's interactive TUI.", "error");
@@ -40,8 +40,7 @@ export type ToolMetadata = Pick<ToolInfo, "name" | "annotations" | "reachesTools
40
40
  exposure?: ToolInfo["exposure"];
41
41
  };
42
42
 
43
- /** Exposures reached through a script or `tool_search` instead of a declaration.
44
- * The session can call these, so a child can be given them directly. */
43
+ /** Exposures callable through a script or `tool_search` without activation. */
45
44
  const INDIRECT_EXPOSURES: ReadonlySet<string> = new Set(["codemode", "deferred"]);
46
45
  /** Live tool metadata keyed by name. Read-only and write capability come from it
47
46
  * instead of a maintained list of extension or MCP tool names. */
@@ -66,11 +65,9 @@ function isReadOnlyToolName(name: string, index: ToolInfoIndex): boolean {
66
65
  return info ? isReadOnlyTool(info) : BUILTIN_READ_ONLY_TOOL_SET.has(name);
67
66
  }
68
67
 
69
- /** Resolve a child against the parent's live tool selection. Broad roles
70
- * inherit the complete active set. A read-only role keeps every active tool
71
- * that is proven read-only, so retrieval extensions and MCP servers work
72
- * without being named anywhere. pi-subagents controls are always removed so
73
- * children stay leaves. */
68
+ /** Resolve a child against the parent's active and callable indirect tools.
69
+ * Inactive direct tools stay excluded. Scout promotes only tools marked
70
+ * read-only; pi-subagents controls are removed so children stay leaves. */
74
71
  export function resolveAgentTools(
75
72
  agent: AgentConfig,
76
73
  activeToolNames: readonly string[],
@@ -78,23 +75,19 @@ export function resolveAgentTools(
78
75
  ): AgentConfig {
79
76
  const active = [...new Set(activeToolNames)].filter((tool) => !SUBAGENT_TOOL_NAME_SET.has(tool));
80
77
  const toolIndex = indexToolInfos(toolInfos);
81
- if (agent.name !== "scout") return { ...agent, tools: active };
82
- // Scout is a hard read-only boundary, so its surface is derived from the
83
- // live catalog alone — the role can never widen past what the host proves
84
- // read-only. Codemode and tool_search are excluded because they reach other
85
- // tools.
78
+ const indirect = [...toolIndex.values()]
79
+ .filter((tool) => tool.exposure && INDIRECT_EXPOSURES.has(tool.exposure) && !SUBAGENT_TOOL_NAME_SET.has(tool.name))
80
+ .map((tool) => tool.name)
81
+ .filter((name) => !active.includes(name))
82
+ .sort();
83
+ if (agent.name !== "scout") return { ...agent, tools: [...active, ...indirect] };
84
+ // Scout uses the live catalog's read-only classification. Codemode and
85
+ // tool_search are excluded because they can reach tools outside that set.
86
86
  const tools = active.filter((tool) => isReadOnlyToolName(tool, toolIndex));
87
- // A read-only tool the session reaches through codemode or tool_search is
88
- // part of the same capability, and the child can be handed it directly.
89
- // Declaring it is the only way to give scout an MCP server it cannot
90
- // reconfigure, without also giving it a tool that reaches a writer.
87
+ // The child explicitly activates these after registration, without giving
88
+ // scout an orchestrating tool that could also reach writers.
91
89
  if (active.some((tool) => toolIndex.get(tool)?.reachesTools)) {
92
- const indirect = [...toolIndex.values()]
93
- .filter((tool) => typeof tool.exposure === "string" && INDIRECT_EXPOSURES.has(tool.exposure) && isReadOnlyTool(tool))
94
- .map((tool) => tool.name)
95
- .filter((name) => !tools.includes(name))
96
- .sort();
97
- tools.push(...indirect);
90
+ tools.push(...indirect.filter((name) => isReadOnlyToolName(name, toolIndex)));
98
91
  }
99
92
  return { ...agent, tools };
100
93
  }