ultimate-pi 0.12.0 → 0.13.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.
@@ -0,0 +1,1463 @@
1
+ /**
2
+ * Subagent Tool - Delegate tasks to specialized agents
3
+ *
4
+ * Spawns a separate `pi` process for each subagent invocation,
5
+ * giving it an isolated context window.
6
+ *
7
+ * Supports three modes:
8
+ * - Single: { agent: "name", task: "..." }
9
+ * - Parallel: { tasks: [{ agent: "name", task: "..." }, ...] }
10
+ * - Chain: { chain: [{ agent: "name", task: "... {previous} ..." }, ...] }
11
+ *
12
+ * Uses JSON mode to capture structured output from subagents.
13
+ */
14
+
15
+ import { spawn } from "node:child_process";
16
+ import * as fs from "node:fs";
17
+ import * as os from "node:os";
18
+ import * as path from "node:path";
19
+ import type { AgentToolResult } from "@earendil-works/pi-agent-core";
20
+ import type { Message } from "@earendil-works/pi-ai";
21
+ import {
22
+ type ExtensionAPI,
23
+ type ExtensionContext,
24
+ getMarkdownTheme,
25
+ withFileMutationQueue,
26
+ } from "@earendil-works/pi-coding-agent";
27
+ import { Container, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
28
+ import { Type } from "@sinclair/typebox";
29
+ import {
30
+ type AgentConfig,
31
+ type AgentScope,
32
+ type AgentSource,
33
+ discoverAgents,
34
+ } from "./agents.js";
35
+
36
+ /** Resolved parent-session credentials forwarded to subprocess `pi` via `--api-key`. */
37
+ export interface SpawnAuthForward {
38
+ provider: string;
39
+ modelRef: string;
40
+ apiKey: string;
41
+ }
42
+
43
+ export interface HarnessSubagentsOptions {
44
+ packageRoot?: string;
45
+ defaultAgentScope?: AgentScope;
46
+ defaultConfirmProjectAgents?: boolean;
47
+ beforeExecute?: (
48
+ params: Record<string, unknown>,
49
+ agents: AgentConfig[],
50
+ ctx: ExtensionContext,
51
+ ) => Promise<{ ok: boolean; message?: string }> | { ok: boolean; message?: string };
52
+ /** Forward parent ModelRegistry auth (incl. runtime overrides) into each subprocess. */
53
+ resolveSpawnAuth?: (
54
+ ctx: ExtensionContext,
55
+ agent: AgentConfig,
56
+ ) => Promise<SpawnAuthForward | undefined>;
57
+ onSpawnStart?: (harnessAgentCount: number) => void;
58
+ onSpawnEnd?: (harnessAgentCount: number) => void;
59
+ onCompleted?: (details: {
60
+ agents: string[];
61
+ mode: string;
62
+ durationMs: number;
63
+ }) => void;
64
+ truncateDetails?: boolean;
65
+ }
66
+
67
+ const MAX_PARALLEL_TASKS = 8;
68
+ const MAX_CONCURRENCY = 4;
69
+ const COLLAPSED_ITEM_COUNT = 10;
70
+ /** Optional backstop from env only; omit PI_SUBAGENT_TIMEOUT_MS to wait for natural subprocess exit. */
71
+ const ENV_TIMEOUT_MS = parsePositiveInteger(process.env.PI_SUBAGENT_TIMEOUT_MS);
72
+ const KILL_GRACE_MS = 5000;
73
+ const STATUS_KEY = "subagents";
74
+ const activeStatuses = new Map<string, string>();
75
+
76
+ function parsePositiveInteger(value: string | undefined): number | undefined {
77
+ if (!value) return undefined;
78
+ const parsed = Number.parseInt(value, 10);
79
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
80
+ }
81
+
82
+ interface StatusContext {
83
+ ui: { setStatus: (key: string, value: string | undefined) => void };
84
+ }
85
+
86
+ function startSubagentStatus(ctx: StatusContext, toolCallId: string, status: string) {
87
+ let cleared = false;
88
+
89
+ const update = (nextStatus: string) => {
90
+ if (cleared) return;
91
+ activeStatuses.set(toolCallId, nextStatus);
92
+ publishSubagentStatus(ctx);
93
+ };
94
+
95
+ update(status);
96
+
97
+ return {
98
+ update,
99
+ clear() {
100
+ if (cleared) return;
101
+ cleared = true;
102
+ activeStatuses.delete(toolCallId);
103
+ publishSubagentStatus(ctx);
104
+ },
105
+ };
106
+ }
107
+
108
+ function publishSubagentStatus(ctx: StatusContext) {
109
+ const statuses = [...activeStatuses.values()];
110
+ if (statuses.length === 0) {
111
+ ctx.ui.setStatus(STATUS_KEY, undefined);
112
+ return;
113
+ }
114
+
115
+ const suffix = statuses.length > 1 ? ` +${statuses.length - 1}` : "";
116
+ ctx.ui.setStatus(STATUS_KEY, `${statuses[0]}${suffix}`);
117
+ }
118
+
119
+ function singleStatus(agent: string): string {
120
+ return `🧑‍🤝‍🧑 ${agent}`;
121
+ }
122
+
123
+ function chainStatus(step: number, total: number, agent?: string): string {
124
+ return `🧑‍🤝‍🧑 chain ${step}/${total}${agent ? ` ${agent}` : ""}`;
125
+ }
126
+
127
+ function parallelStatus(done: number, total: number, running: number): string {
128
+ return `🧑‍🤝‍🧑 parallel ${done}/${total} done${running > 0 ? ` ${running} running` : ""}`;
129
+ }
130
+
131
+ function fanInStatus(agent: string): string {
132
+ return `🧑‍🤝‍🧑 fan-in ${agent}`;
133
+ }
134
+
135
+ function formatTokens(count: number): string {
136
+ if (count < 1000) return count.toString();
137
+ if (count < 10000) return `${(count / 1000).toFixed(1)}k`;
138
+ if (count < 1000000) return `${Math.round(count / 1000)}k`;
139
+ return `${(count / 1000000).toFixed(1)}M`;
140
+ }
141
+
142
+ function formatUsageStats(
143
+ usage: {
144
+ input: number;
145
+ output: number;
146
+ cacheRead: number;
147
+ cacheWrite: number;
148
+ cost: number;
149
+ contextTokens?: number;
150
+ turns?: number;
151
+ },
152
+ model?: string,
153
+ ): string {
154
+ const parts: string[] = [];
155
+ if (usage.turns) parts.push(`${usage.turns} turn${usage.turns > 1 ? "s" : ""}`);
156
+ if (usage.input) parts.push(`↑${formatTokens(usage.input)}`);
157
+ if (usage.output) parts.push(`↓${formatTokens(usage.output)}`);
158
+ if (usage.cacheRead) parts.push(`R${formatTokens(usage.cacheRead)}`);
159
+ if (usage.cacheWrite) parts.push(`W${formatTokens(usage.cacheWrite)}`);
160
+ if (usage.cost) parts.push(`$${usage.cost.toFixed(4)}`);
161
+ if (usage.contextTokens && usage.contextTokens > 0) {
162
+ parts.push(`ctx:${formatTokens(usage.contextTokens)}`);
163
+ }
164
+ if (model) parts.push(model);
165
+ return parts.join(" ");
166
+ }
167
+
168
+ function formatToolCall(
169
+ toolName: string,
170
+ args: Record<string, unknown>,
171
+ themeFg: (color: any, text: string) => string,
172
+ ): string {
173
+ const shortenPath = (p: string) => {
174
+ const home = os.homedir();
175
+ return p.startsWith(home) ? `~${p.slice(home.length)}` : p;
176
+ };
177
+
178
+ switch (toolName) {
179
+ case "bash": {
180
+ const command = (args.command as string) || "...";
181
+ const preview = command.length > 60 ? `${command.slice(0, 60)}...` : command;
182
+ return themeFg("muted", "$ ") + themeFg("toolOutput", preview);
183
+ }
184
+ case "read": {
185
+ const rawPath = (args.file_path || args.path || "...") as string;
186
+ const filePath = shortenPath(rawPath);
187
+ const offset = args.offset as number | undefined;
188
+ const limit = args.limit as number | undefined;
189
+ let text = themeFg("accent", filePath);
190
+ if (offset !== undefined || limit !== undefined) {
191
+ const startLine = offset ?? 1;
192
+ const endLine = limit !== undefined ? startLine + limit - 1 : "";
193
+ text += themeFg("warning", `:${startLine}${endLine ? `-${endLine}` : ""}`);
194
+ }
195
+ return themeFg("muted", "read ") + text;
196
+ }
197
+ case "write": {
198
+ const rawPath = (args.file_path || args.path || "...") as string;
199
+ const filePath = shortenPath(rawPath);
200
+ const content = (args.content || "") as string;
201
+ const lines = content.split("\n").length;
202
+ let text = themeFg("muted", "write ") + themeFg("accent", filePath);
203
+ if (lines > 1) text += themeFg("dim", ` (${lines} lines)`);
204
+ return text;
205
+ }
206
+ case "edit": {
207
+ const rawPath = (args.file_path || args.path || "...") as string;
208
+ return themeFg("muted", "edit ") + themeFg("accent", shortenPath(rawPath));
209
+ }
210
+ case "ls": {
211
+ const rawPath = (args.path || ".") as string;
212
+ return themeFg("muted", "ls ") + themeFg("accent", shortenPath(rawPath));
213
+ }
214
+ case "find": {
215
+ const pattern = (args.pattern || "*") as string;
216
+ const rawPath = (args.path || ".") as string;
217
+ return themeFg("muted", "find ") + themeFg("accent", pattern) + themeFg("dim", ` in ${shortenPath(rawPath)}`);
218
+ }
219
+ case "grep": {
220
+ const pattern = (args.pattern || "") as string;
221
+ const rawPath = (args.path || ".") as string;
222
+ return (
223
+ themeFg("muted", "grep ") +
224
+ themeFg("accent", `/${pattern}/`) +
225
+ themeFg("dim", ` in ${shortenPath(rawPath)}`)
226
+ );
227
+ }
228
+ default: {
229
+ const argsStr = JSON.stringify(args);
230
+ const preview = argsStr.length > 50 ? `${argsStr.slice(0, 50)}...` : argsStr;
231
+ return themeFg("accent", toolName) + themeFg("dim", ` ${preview}`);
232
+ }
233
+ }
234
+ }
235
+
236
+ interface UsageStats {
237
+ input: number;
238
+ output: number;
239
+ cacheRead: number;
240
+ cacheWrite: number;
241
+ cost: number;
242
+ contextTokens: number;
243
+ turns: number;
244
+ }
245
+
246
+ interface SingleResult {
247
+ agent: string;
248
+ agentSource: AgentSource | "unknown";
249
+ task: string;
250
+ exitCode: number;
251
+ messages: Message[];
252
+ stderr: string;
253
+ usage: UsageStats;
254
+ model?: string;
255
+ stopReason?: string;
256
+ errorMessage?: string;
257
+ step?: number;
258
+ finalOutput?: string;
259
+ timedOut?: boolean;
260
+ timeoutMs?: number;
261
+ }
262
+
263
+ interface SubagentDetails {
264
+ mode: "single" | "parallel" | "chain";
265
+ agentScope: AgentScope;
266
+ projectAgentsDir: string | null;
267
+ results: SingleResult[];
268
+ aggregator?: SingleResult;
269
+ }
270
+
271
+ function getFinalOutput(messages: Message[]): string {
272
+ for (let i = messages.length - 1; i >= 0; i--) {
273
+ const msg = messages[i];
274
+ if (msg.role === "assistant") {
275
+ for (const part of msg.content) {
276
+ if (part.type === "text") return part.text;
277
+ }
278
+ }
279
+ }
280
+ return "";
281
+ }
282
+
283
+ function getResultFinalOutput(result: SingleResult): string {
284
+ return result.finalOutput ?? getFinalOutput(result.messages);
285
+ }
286
+
287
+ function buildFanInContext(results: SingleResult[]): string {
288
+ return results
289
+ .map((result, index) => {
290
+ const status = result.exitCode === 0 ? "completed" : result.exitCode === -1 ? "running" : "failed";
291
+ const output = getResultFinalOutput(result);
292
+ const error = result.errorMessage || result.stderr.trim();
293
+ return [
294
+ `## Result ${index + 1}: ${result.agent} (${status})`,
295
+ `Task: ${result.task}`,
296
+ output ? `Output:\n${output}` : error ? `Error:\n${error}` : "Output: (no output)",
297
+ ].join("\n\n");
298
+ })
299
+ .join("\n\n---\n\n");
300
+ }
301
+
302
+ type DisplayItem = { type: "text"; text: string } | { type: "toolCall"; name: string; args: Record<string, any> };
303
+
304
+ function getDisplayItems(messages: Message[]): DisplayItem[] {
305
+ const items: DisplayItem[] = [];
306
+ for (const msg of messages) {
307
+ if (msg.role === "assistant") {
308
+ for (const part of msg.content) {
309
+ if (part.type === "text") items.push({ type: "text", text: part.text });
310
+ else if (part.type === "toolCall") items.push({ type: "toolCall", name: part.name, args: part.arguments });
311
+ }
312
+ }
313
+ }
314
+ return items;
315
+ }
316
+
317
+ async function mapWithConcurrencyLimit<TIn, TOut>(
318
+ items: TIn[],
319
+ concurrency: number,
320
+ fn: (item: TIn, index: number) => Promise<TOut>,
321
+ ): Promise<TOut[]> {
322
+ if (items.length === 0) return [];
323
+ const limit = Math.max(1, Math.min(concurrency, items.length));
324
+ const results: TOut[] = new Array(items.length);
325
+ let nextIndex = 0;
326
+ const workers = new Array(limit).fill(null).map(async () => {
327
+ while (true) {
328
+ const current = nextIndex++;
329
+ if (current >= items.length) return;
330
+ results[current] = await fn(items[current], current);
331
+ }
332
+ });
333
+ await Promise.all(workers);
334
+ return results;
335
+ }
336
+
337
+ async function writePromptToTempFile(agentName: string, prompt: string): Promise<{ dir: string; filePath: string }> {
338
+ const tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "pi-subagent-"));
339
+ const safeName = agentName.replace(/[^\w.-]+/g, "_");
340
+ const filePath = path.join(tmpDir, `prompt-${safeName}.md`);
341
+ await withFileMutationQueue(filePath, async () => {
342
+ await fs.promises.writeFile(filePath, prompt, { encoding: "utf-8", mode: 0o600 });
343
+ });
344
+ return { dir: tmpDir, filePath };
345
+ }
346
+
347
+ function getPiInvocation(args: string[]): { command: string; args: string[] } {
348
+ const currentScript = process.argv[1];
349
+ const isBunVirtualScript = currentScript?.startsWith("/$bunfs/root/");
350
+ if (currentScript && !isBunVirtualScript && fs.existsSync(currentScript)) {
351
+ return { command: process.execPath, args: [currentScript, ...args] };
352
+ }
353
+
354
+ const execName = path.basename(process.execPath).toLowerCase();
355
+ const isGenericRuntime = /^(node|bun)(\.exe)?$/.test(execName);
356
+ if (!isGenericRuntime) {
357
+ return { command: process.execPath, args };
358
+ }
359
+
360
+ return { command: "pi", args };
361
+ }
362
+
363
+ function terminateProcess(proc: ReturnType<typeof spawn>) {
364
+ if (proc.killed) return;
365
+ if (process.platform !== "win32" && proc.pid) {
366
+ try {
367
+ process.kill(-proc.pid, "SIGTERM");
368
+ } catch {
369
+ proc.kill("SIGTERM");
370
+ }
371
+ } else {
372
+ proc.kill("SIGTERM");
373
+ }
374
+
375
+ setTimeout(() => {
376
+ if (proc.killed) return;
377
+ if (process.platform !== "win32" && proc.pid) {
378
+ try {
379
+ process.kill(-proc.pid, "SIGKILL");
380
+ } catch {
381
+ proc.kill("SIGKILL");
382
+ }
383
+ } else {
384
+ proc.kill("SIGKILL");
385
+ }
386
+ }, KILL_GRACE_MS).unref();
387
+ }
388
+
389
+ type OnUpdateCallback = (partial: AgentToolResult<SubagentDetails>) => void;
390
+
391
+ function buildSpawnEnv(packageRoot?: string): NodeJS.ProcessEnv {
392
+ const env = { ...process.env };
393
+ env.PI_HARNESS_SUBPROCESS = "1";
394
+ if (packageRoot) {
395
+ env.UP_PKG = packageRoot;
396
+ env.HARNESS_PKG_ROOT = packageRoot;
397
+ }
398
+ return env;
399
+ }
400
+
401
+ async function runSingleAgent(
402
+ defaultCwd: string,
403
+ agents: AgentConfig[],
404
+ agentName: string,
405
+ task: string,
406
+ cwd: string | undefined,
407
+ step: number | undefined,
408
+ signal: AbortSignal | undefined,
409
+ timeoutMs: number | undefined,
410
+ onUpdate: OnUpdateCallback | undefined,
411
+ makeDetails: (results: SingleResult[]) => SubagentDetails,
412
+ packageRoot?: string,
413
+ spawnAuth?: SpawnAuthForward,
414
+ ): Promise<SingleResult> {
415
+ const agent = agents.find((a) => a.name === agentName);
416
+
417
+ if (!agent) {
418
+ const available = agents.map((a) => `"${a.name}"`).join(", ") || "none";
419
+ return {
420
+ agent: agentName,
421
+ agentSource: "unknown",
422
+ task,
423
+ exitCode: 1,
424
+ messages: [],
425
+ stderr: `Unknown agent: "${agentName}". Available agents: ${available}.`,
426
+ usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
427
+ step,
428
+ finalOutput: "",
429
+ };
430
+ }
431
+
432
+ const args: string[] = ["--mode", "json", "-p", "--no-session"];
433
+ if (agent.model) args.push("--model", agent.model);
434
+ else if (spawnAuth) args.push("--model", spawnAuth.modelRef);
435
+ if (spawnAuth?.apiKey) args.push("--api-key", spawnAuth.apiKey);
436
+ if (agent.thinking) args.push("--thinking", agent.thinking);
437
+ if (agent.extensionsOff) {
438
+ args.push("--no-extensions");
439
+ if (agent.skillsOff) args.push("--no-skills");
440
+ }
441
+ if (agent.tools && agent.tools.length > 0) {
442
+ args.push("--tools", agent.tools.join(","));
443
+ } else if (agent.extensionsOff) {
444
+ args.push("--no-tools");
445
+ }
446
+ const spawnEnv = buildSpawnEnv(packageRoot);
447
+
448
+ let tmpPromptDir: string | null = null;
449
+ let tmpPromptPath: string | null = null;
450
+
451
+ const currentResult: SingleResult = {
452
+ agent: agentName,
453
+ agentSource: agent.source,
454
+ task,
455
+ exitCode: 0,
456
+ messages: [],
457
+ stderr: "",
458
+ usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
459
+ model: agent.model,
460
+ step,
461
+ timeoutMs,
462
+ };
463
+
464
+ const emitUpdate = () => {
465
+ currentResult.finalOutput = getFinalOutput(currentResult.messages);
466
+ if (onUpdate) {
467
+ onUpdate({
468
+ content: [{ type: "text", text: currentResult.finalOutput || "(running...)" }],
469
+ details: makeDetails([currentResult]),
470
+ });
471
+ }
472
+ };
473
+
474
+ try {
475
+ if (agent.systemPrompt.trim()) {
476
+ const tmp = await writePromptToTempFile(agent.name, agent.systemPrompt);
477
+ tmpPromptDir = tmp.dir;
478
+ tmpPromptPath = tmp.filePath;
479
+ args.push("--append-system-prompt", tmpPromptPath);
480
+ }
481
+
482
+ args.push(`Task: ${task}`);
483
+ let wasAborted = false;
484
+ let timedOut = false;
485
+
486
+ const exitCode = await new Promise<number>((resolve) => {
487
+ const invocation = getPiInvocation(args);
488
+ let settled = false;
489
+ let timeout: ReturnType<typeof setTimeout> | undefined;
490
+ const finish = (code: number) => {
491
+ if (settled) return;
492
+ settled = true;
493
+ if (timeout) clearTimeout(timeout);
494
+ resolve(code);
495
+ };
496
+ const proc = spawn(invocation.command, invocation.args, {
497
+ cwd: cwd ?? defaultCwd,
498
+ env: spawnEnv,
499
+ detached: process.platform !== "win32",
500
+ shell: false,
501
+ stdio: ["ignore", "pipe", "pipe"],
502
+ });
503
+ let buffer = "";
504
+ if (timeoutMs != null) {
505
+ timeout = setTimeout(() => {
506
+ timedOut = true;
507
+ currentResult.timedOut = true;
508
+ currentResult.stopReason = "timeout";
509
+ currentResult.errorMessage = `Subagent timed out after ${timeoutMs}ms`;
510
+ currentResult.stderr += `${currentResult.stderr ? "\n" : ""}Subagent timed out after ${timeoutMs}ms.`;
511
+ emitUpdate();
512
+ terminateProcess(proc);
513
+ }, timeoutMs);
514
+ timeout.unref();
515
+ }
516
+
517
+ const processLine = (line: string) => {
518
+ if (!line.trim()) return;
519
+ let event: any;
520
+ try {
521
+ event = JSON.parse(line);
522
+ } catch {
523
+ return;
524
+ }
525
+
526
+ if (event.type === "message_end" && event.message) {
527
+ const msg = event.message as Message;
528
+ currentResult.messages.push(msg);
529
+
530
+ if (msg.role === "assistant") {
531
+ currentResult.usage.turns++;
532
+ const usage = msg.usage;
533
+ if (usage) {
534
+ currentResult.usage.input += usage.input || 0;
535
+ currentResult.usage.output += usage.output || 0;
536
+ currentResult.usage.cacheRead += usage.cacheRead || 0;
537
+ currentResult.usage.cacheWrite += usage.cacheWrite || 0;
538
+ currentResult.usage.cost += usage.cost?.total || 0;
539
+ currentResult.usage.contextTokens = usage.totalTokens || 0;
540
+ }
541
+ if (!currentResult.model && msg.model) currentResult.model = msg.model;
542
+ if (msg.stopReason) currentResult.stopReason = msg.stopReason;
543
+ if (msg.errorMessage) currentResult.errorMessage = msg.errorMessage;
544
+ }
545
+ emitUpdate();
546
+ }
547
+
548
+ if (event.type === "tool_result_end" && event.message) {
549
+ currentResult.messages.push(event.message as Message);
550
+ emitUpdate();
551
+ }
552
+ };
553
+
554
+ proc.stdout.on("data", (data) => {
555
+ buffer += data.toString();
556
+ const lines = buffer.split("\n");
557
+ buffer = lines.pop() || "";
558
+ for (const line of lines) processLine(line);
559
+ });
560
+
561
+ proc.stderr.on("data", (data) => {
562
+ currentResult.stderr += data.toString();
563
+ });
564
+
565
+ proc.on("close", (code) => {
566
+ if (buffer.trim()) processLine(buffer);
567
+ finish(timedOut ? 124 : (code ?? 0));
568
+ });
569
+
570
+ proc.on("error", (error) => {
571
+ currentResult.errorMessage = error.message;
572
+ currentResult.stderr += `${currentResult.stderr ? "\n" : ""}${error.message}`;
573
+ finish(1);
574
+ });
575
+
576
+ if (signal) {
577
+ const killProc = () => {
578
+ wasAborted = true;
579
+ currentResult.stopReason = "aborted";
580
+ currentResult.errorMessage = "Subagent was aborted";
581
+ terminateProcess(proc);
582
+ };
583
+ if (signal.aborted) killProc();
584
+ else signal.addEventListener("abort", killProc, { once: true });
585
+ }
586
+ });
587
+
588
+ currentResult.exitCode = exitCode;
589
+ currentResult.finalOutput = getFinalOutput(currentResult.messages);
590
+ if (wasAborted && !timedOut) throw new Error("Subagent was aborted");
591
+ return currentResult;
592
+ } finally {
593
+ if (tmpPromptPath)
594
+ try {
595
+ fs.unlinkSync(tmpPromptPath);
596
+ } catch {
597
+ /* ignore */
598
+ }
599
+ if (tmpPromptDir)
600
+ try {
601
+ fs.rmdirSync(tmpPromptDir);
602
+ } catch {
603
+ /* ignore */
604
+ }
605
+ }
606
+ }
607
+
608
+ const TimeoutMs = Type.Number({
609
+ description:
610
+ "Optional hard timeout in milliseconds for each subagent subprocess. When omitted, waits until the subprocess exits naturally. Set PI_SUBAGENT_TIMEOUT_MS for a session-wide backstop.",
611
+ minimum: 1,
612
+ });
613
+
614
+ const TaskItem = Type.Object({
615
+ agent: Type.String({ description: "Name of the agent to invoke" }),
616
+ task: Type.String({ description: "Task to delegate to the agent" }),
617
+ cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
618
+ timeoutMs: Type.Optional(TimeoutMs),
619
+ });
620
+
621
+ const ChainItem = Type.Object({
622
+ agent: Type.String({ description: "Name of the agent to invoke" }),
623
+ task: Type.String({ description: "Task with optional {previous} placeholder for prior output" }),
624
+ cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
625
+ timeoutMs: Type.Optional(TimeoutMs),
626
+ });
627
+
628
+ const AggregatorItem = Type.Object({
629
+ agent: Type.String({ description: "Name of the fan-in agent to invoke after parallel tasks complete" }),
630
+ task: Type.String({ description: "Fan-in task. Use {previous} to include all parallel outputs." }),
631
+ cwd: Type.Optional(Type.String({ description: "Working directory for the aggregator process" })),
632
+ timeoutMs: Type.Optional(TimeoutMs),
633
+ });
634
+
635
+ const AgentScopeSchema = Type.Union(
636
+ [
637
+ Type.Literal("user"),
638
+ Type.Literal("project"),
639
+ Type.Literal("both"),
640
+ ],
641
+ {
642
+ description:
643
+ 'Which agent directories to use. Default: "user". Use "both" to include project-local agents.',
644
+ default: "user",
645
+ },
646
+ );
647
+
648
+ const SubagentParams = Type.Object({
649
+ agent: Type.Optional(Type.String({ description: "Name of the agent to invoke (for single mode)" })),
650
+ task: Type.Optional(Type.String({ description: "Task to delegate (for single mode)" })),
651
+ tasks: Type.Optional(Type.Array(TaskItem, { description: "Array of {agent, task} for parallel execution" })),
652
+ chain: Type.Optional(Type.Array(ChainItem, { description: "Array of {agent, task} for sequential execution" })),
653
+ aggregator: Type.Optional(AggregatorItem),
654
+ agentScope: Type.Optional(AgentScopeSchema),
655
+ confirmProjectAgents: Type.Optional(
656
+ Type.Boolean({ description: "Prompt before running project-local agents. Default: true.", default: true }),
657
+ ),
658
+ cwd: Type.Optional(Type.String({ description: "Working directory for the agent process (single mode)" })),
659
+ timeoutMs: Type.Optional(TimeoutMs),
660
+ });
661
+
662
+ function truncateSubagentDetails(
663
+ details: SubagentDetails,
664
+ ): SubagentDetails {
665
+ return {
666
+ ...details,
667
+ results: details.results.map((r) => ({
668
+ ...r,
669
+ messages: [],
670
+ })),
671
+ aggregator: details.aggregator
672
+ ? { ...details.aggregator, messages: [] }
673
+ : undefined,
674
+ };
675
+ }
676
+
677
+ export function createSubagentsExtension(
678
+ pi: ExtensionAPI,
679
+ options: HarnessSubagentsOptions = {},
680
+ ) {
681
+ const packageRoot = options.packageRoot;
682
+ const defaultScope: AgentScope = options.defaultAgentScope ?? "both";
683
+ const defaultConfirm = options.defaultConfirmProjectAgents ?? false;
684
+
685
+ pi.registerTool({
686
+ name: "subagent",
687
+ label: "Subagent",
688
+ description: [
689
+ "Delegate tasks to specialized subagents with isolated context.",
690
+ "Modes: single (agent + task), parallel (tasks array), chain (sequential with {previous} placeholder).",
691
+ "Parallel mode may include an aggregator fan-in step that receives all task outputs.",
692
+ 'Default agent scope is "user" (from ~/.pi/agent/agents).',
693
+ 'To enable project-local agents in .pi/agents, set agentScope: "both" (or "project").',
694
+ ].join(" "),
695
+ promptSnippet:
696
+ "Delegate independent research, review, verification, or multi-step work to isolated Pi subagents.",
697
+ promptGuidelines: [
698
+ "Use subagent for independent read-only research, broad codebase reconnaissance, high-volume command output, multi-domain parallel investigation, or an independent reviewer after implementation.",
699
+ "Use subagent parallel mode when work splits into independent tasks; prefer read-only agents such as scout or reviewer for fan-out and serialize write-heavy implementation that touches the same files.",
700
+ "Do not use subagent for simple answers, quick targeted edits, latency-sensitive one-step work, or tasks requiring frequent user back-and-forth.",
701
+ 'Do not use subagent with project-local agents unless the user explicitly wants project agents or sets agentScope to "project" or "both"; keep confirmation enabled for untrusted repositories.',
702
+ "When using subagent, write self-contained tasks with file paths, context, expected output, and whether the subagent may edit files.",
703
+ ],
704
+ parameters: SubagentParams,
705
+
706
+ async execute(toolCallId, params, signal, onUpdate, ctx) {
707
+ const startedAt = Date.now();
708
+ const agentScope: AgentScope =
709
+ (params.agentScope as AgentScope | undefined) ?? defaultScope;
710
+ const discovery = discoverAgents(ctx.cwd, agentScope, packageRoot);
711
+ const agents = discovery.agents;
712
+ const confirmProjectAgents =
713
+ params.confirmProjectAgents ?? defaultConfirm;
714
+ const defaultTimeoutMs = params.timeoutMs ?? ENV_TIMEOUT_MS;
715
+
716
+ const resolveSpawnAuth = async (agentName: string): Promise<SpawnAuthForward | undefined> => {
717
+ if (!options.resolveSpawnAuth) return undefined;
718
+ const agent = agents.find((a) => a.name === agentName);
719
+ if (!agent) return undefined;
720
+ const forward = await options.resolveSpawnAuth(ctx, agent);
721
+ return forward;
722
+ };
723
+
724
+ if (options.beforeExecute) {
725
+ const gate = await options.beforeExecute(
726
+ params as Record<string, unknown>,
727
+ agents,
728
+ ctx,
729
+ );
730
+ if (!gate.ok) {
731
+ return {
732
+ content: [
733
+ {
734
+ type: "text",
735
+ text: gate.message ?? "Subagent spawn blocked by harness policy.",
736
+ },
737
+ ],
738
+ details: {
739
+ mode: "single",
740
+ agentScope,
741
+ projectAgentsDir: discovery.projectAgentsDir,
742
+ results: [],
743
+ },
744
+ isError: true,
745
+ };
746
+ }
747
+ }
748
+
749
+ const harnessAgents: string[] = [];
750
+ if (params.agent?.startsWith("harness/")) harnessAgents.push(params.agent);
751
+ if (params.tasks)
752
+ for (const t of params.tasks)
753
+ if (t.agent.startsWith("harness/")) harnessAgents.push(t.agent);
754
+ if (params.chain)
755
+ for (const c of params.chain)
756
+ if (c.agent.startsWith("harness/")) harnessAgents.push(c.agent);
757
+ if (params.aggregator?.agent.startsWith("harness/"))
758
+ harnessAgents.push(params.aggregator.agent);
759
+ options.onSpawnStart?.(harnessAgents.length);
760
+
761
+ try {
762
+ const hasChain = (params.chain?.length ?? 0) > 0;
763
+ const hasTasks = (params.tasks?.length ?? 0) > 0;
764
+ const hasSingle = Boolean(params.agent && params.task);
765
+ const modeCount = Number(hasChain) + Number(hasTasks) + Number(hasSingle);
766
+
767
+ const makeDetails =
768
+ (mode: "single" | "parallel" | "chain") =>
769
+ (results: SingleResult[], aggregator?: SingleResult): SubagentDetails => ({
770
+ mode,
771
+ agentScope,
772
+ projectAgentsDir: discovery.projectAgentsDir,
773
+ results,
774
+ aggregator,
775
+ });
776
+
777
+ if (modeCount !== 1 || (params.aggregator && !hasTasks)) {
778
+ const available = agents.map((a) => `${a.name} (${a.source})`).join(", ") || "none";
779
+ const reason =
780
+ modeCount !== 1
781
+ ? "Provide exactly one mode."
782
+ : "Aggregator is only valid with parallel tasks.";
783
+ return {
784
+ content: [
785
+ {
786
+ type: "text",
787
+ text: `Invalid parameters. ${reason}\nAvailable agents: ${available}`,
788
+ },
789
+ ],
790
+ details: makeDetails("single")([]),
791
+ };
792
+ }
793
+
794
+ if ((agentScope === "project" || agentScope === "both") && confirmProjectAgents && ctx.hasUI) {
795
+ const requestedAgentNames = new Set<string>();
796
+ if (params.chain) for (const step of params.chain) requestedAgentNames.add(step.agent);
797
+ if (params.tasks) for (const t of params.tasks) requestedAgentNames.add(t.agent);
798
+ if (params.aggregator) requestedAgentNames.add(params.aggregator.agent);
799
+ if (params.agent) requestedAgentNames.add(params.agent);
800
+
801
+ const projectAgentsRequested = Array.from(requestedAgentNames)
802
+ .map((name) => agents.find((a) => a.name === name))
803
+ .filter((a): a is AgentConfig => a?.source === "project");
804
+
805
+ if (projectAgentsRequested.length > 0) {
806
+ const names = projectAgentsRequested.map((a) => a.name).join(", ");
807
+ const dir = discovery.projectAgentsDir ?? "(unknown)";
808
+ const ok = await ctx.ui.confirm(
809
+ "Run project-local agents?",
810
+ `Agents: ${names}\nSource: ${dir}\n\nProject agents are repo-controlled. Only continue for trusted repositories.`,
811
+ );
812
+ if (!ok)
813
+ return {
814
+ content: [{ type: "text", text: "Canceled: project-local agents not approved." }],
815
+ details: makeDetails(hasChain ? "chain" : hasTasks ? "parallel" : "single")([]),
816
+ };
817
+ }
818
+ }
819
+
820
+ if (params.chain && params.chain.length > 0) {
821
+ const results: SingleResult[] = [];
822
+ let previousOutput = "";
823
+ const status = startSubagentStatus(ctx, toolCallId, chainStatus(0, params.chain.length));
824
+
825
+ try {
826
+ for (let i = 0; i < params.chain.length; i++) {
827
+ const step = params.chain[i];
828
+ status.update(chainStatus(i + 1, params.chain.length, step.agent));
829
+ const taskWithContext = step.task.replace(/\{previous\}/g, previousOutput);
830
+
831
+ // Create update callback that includes all previous results
832
+ const chainUpdate: OnUpdateCallback | undefined = onUpdate
833
+ ? (partial) => {
834
+ // Combine completed results with current streaming result
835
+ const currentResult = partial.details?.results[0];
836
+ if (currentResult) {
837
+ const allResults = [...results, currentResult];
838
+ onUpdate({
839
+ content: partial.content,
840
+ details: makeDetails("chain")(allResults),
841
+ });
842
+ }
843
+ }
844
+ : undefined;
845
+
846
+ const result = await runSingleAgent(
847
+ ctx.cwd,
848
+ agents,
849
+ step.agent,
850
+ taskWithContext,
851
+ step.cwd,
852
+ i + 1,
853
+ signal,
854
+ step.timeoutMs ?? defaultTimeoutMs,
855
+ chainUpdate,
856
+ makeDetails("chain"),
857
+ packageRoot,
858
+ await resolveSpawnAuth(step.agent),
859
+ );
860
+ results.push(result);
861
+
862
+ const isError =
863
+ result.exitCode !== 0 || result.stopReason === "error" || result.stopReason === "aborted";
864
+ if (isError) {
865
+ const errorMsg = result.errorMessage || result.stderr || getResultFinalOutput(result) || "(no output)";
866
+ return {
867
+ content: [{ type: "text", text: `Chain stopped at step ${i + 1} (${step.agent}): ${errorMsg}` }],
868
+ details: makeDetails("chain")(results),
869
+ isError: true,
870
+ };
871
+ }
872
+ previousOutput = getResultFinalOutput(result);
873
+ }
874
+ return {
875
+ content: [{ type: "text", text: getResultFinalOutput(results[results.length - 1]) || "(no output)" }],
876
+ details: makeDetails("chain")(results),
877
+ };
878
+ } finally {
879
+ status.clear();
880
+ }
881
+ }
882
+
883
+ if (params.tasks && params.tasks.length > 0) {
884
+ if (params.tasks.length > MAX_PARALLEL_TASKS)
885
+ return {
886
+ content: [
887
+ {
888
+ type: "text",
889
+ text: `Too many parallel tasks (${params.tasks.length}). Max is ${MAX_PARALLEL_TASKS}.`,
890
+ },
891
+ ],
892
+ details: makeDetails("parallel")([]),
893
+ };
894
+
895
+ const status = startSubagentStatus(ctx, toolCallId, parallelStatus(0, params.tasks.length, params.tasks.length));
896
+
897
+ try {
898
+ // Track all results for streaming updates
899
+ const allResults: SingleResult[] = new Array(params.tasks.length);
900
+
901
+ // Initialize placeholder results
902
+ for (let i = 0; i < params.tasks.length; i++) {
903
+ allResults[i] = {
904
+ agent: params.tasks[i].agent,
905
+ agentSource: "unknown",
906
+ task: params.tasks[i].task,
907
+ exitCode: -1, // -1 = still running
908
+ messages: [],
909
+ stderr: "",
910
+ usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
911
+ finalOutput: "",
912
+ };
913
+ }
914
+
915
+ let doneCount = 0;
916
+ let runningCount = params.tasks.length;
917
+
918
+ const emitParallelUpdate = () => {
919
+ status.update(parallelStatus(doneCount, allResults.length, runningCount));
920
+ if (onUpdate) {
921
+ onUpdate({
922
+ content: [
923
+ {
924
+ type: "text",
925
+ text: `Parallel: ${doneCount}/${allResults.length} done, ${runningCount} running...`,
926
+ },
927
+ ],
928
+ details: makeDetails("parallel")([...allResults]),
929
+ });
930
+ }
931
+ };
932
+
933
+ const results = await mapWithConcurrencyLimit(params.tasks, MAX_CONCURRENCY, async (t, index) => {
934
+ const result = await runSingleAgent(
935
+ ctx.cwd,
936
+ agents,
937
+ t.agent,
938
+ t.task,
939
+ t.cwd,
940
+ undefined,
941
+ signal,
942
+ t.timeoutMs ?? defaultTimeoutMs,
943
+ // Per-task update callback
944
+ (partial) => {
945
+ if (partial.details?.results[0]) {
946
+ allResults[index] = { ...partial.details.results[0], exitCode: -1 };
947
+ emitParallelUpdate();
948
+ }
949
+ },
950
+ makeDetails("parallel"),
951
+ packageRoot,
952
+ await resolveSpawnAuth(t.agent),
953
+ );
954
+ allResults[index] = result;
955
+ doneCount += 1;
956
+ runningCount -= 1;
957
+ emitParallelUpdate();
958
+ return result;
959
+ });
960
+
961
+ let aggregatorResult: SingleResult | undefined;
962
+ if (params.aggregator) {
963
+ const aggregator = params.aggregator;
964
+ status.update(fanInStatus(aggregator.agent));
965
+ const fanInContext = buildFanInContext(results);
966
+ const aggregatorTask = aggregator.task.includes("{previous}")
967
+ ? aggregator.task.replace(/\{previous\}/g, fanInContext)
968
+ : `${aggregator.task}\n\nParallel task outputs:\n\n${fanInContext}`;
969
+ aggregatorResult = await runSingleAgent(
970
+ ctx.cwd,
971
+ agents,
972
+ aggregator.agent,
973
+ aggregatorTask,
974
+ aggregator.cwd,
975
+ undefined,
976
+ signal,
977
+ aggregator.timeoutMs ?? defaultTimeoutMs,
978
+ (partial) => {
979
+ status.update(fanInStatus(aggregator.agent));
980
+ if (onUpdate && partial.details?.results[0]) {
981
+ onUpdate({
982
+ content: partial.content,
983
+ details: makeDetails("parallel")(results, partial.details.results[0]),
984
+ });
985
+ }
986
+ },
987
+ makeDetails("parallel"),
988
+ packageRoot,
989
+ await resolveSpawnAuth(aggregator.agent),
990
+ );
991
+ }
992
+
993
+ const successCount = results.filter((r) => r.exitCode === 0).length;
994
+ const summaries = results.map((r) => {
995
+ const output = getResultFinalOutput(r);
996
+ const error = r.errorMessage || r.stderr.trim();
997
+ const summaryText = output || error;
998
+ const preview = summaryText.slice(0, 160) + (summaryText.length > 160 ? "..." : "");
999
+ return `[${r.agent}] ${r.exitCode === 0 ? "completed" : "failed"}: ${preview || "(no output)"}`;
1000
+ });
1001
+ const aggregatorOutput = aggregatorResult ? getResultFinalOutput(aggregatorResult) : "";
1002
+ const aggregatorError = aggregatorResult?.errorMessage || aggregatorResult?.stderr.trim() || "";
1003
+ return {
1004
+ content: [
1005
+ {
1006
+ type: "text",
1007
+ text: aggregatorResult
1008
+ ? aggregatorOutput || aggregatorError || `(aggregator ${aggregatorResult.agent} produced no output)`
1009
+ : `Parallel: ${successCount}/${results.length} succeeded\n\n${summaries.join("\n\n")}`,
1010
+ },
1011
+ ],
1012
+ details: makeDetails("parallel")(results, aggregatorResult),
1013
+ isError: aggregatorResult
1014
+ ? aggregatorResult.exitCode !== 0 ||
1015
+ aggregatorResult.stopReason === "error" ||
1016
+ aggregatorResult.stopReason === "aborted"
1017
+ : undefined,
1018
+ };
1019
+ } finally {
1020
+ status.clear();
1021
+ }
1022
+ }
1023
+
1024
+ if (params.agent && params.task) {
1025
+ const status = startSubagentStatus(ctx, toolCallId, singleStatus(params.agent));
1026
+
1027
+ try {
1028
+ const result = await runSingleAgent(
1029
+ ctx.cwd,
1030
+ agents,
1031
+ params.agent,
1032
+ params.task,
1033
+ params.cwd,
1034
+ undefined,
1035
+ signal,
1036
+ params.timeoutMs ?? defaultTimeoutMs,
1037
+ onUpdate,
1038
+ makeDetails("single"),
1039
+ packageRoot,
1040
+ await resolveSpawnAuth(params.agent),
1041
+ );
1042
+ const isError = result.exitCode !== 0 || result.stopReason === "error" || result.stopReason === "aborted";
1043
+ if (isError) {
1044
+ const errorMsg = result.errorMessage || result.stderr || getResultFinalOutput(result) || "(no output)";
1045
+ return {
1046
+ content: [{ type: "text", text: `Agent ${result.stopReason || "failed"}: ${errorMsg}` }],
1047
+ details: makeDetails("single")([result]),
1048
+ isError: true,
1049
+ };
1050
+ }
1051
+ return {
1052
+ content: [{ type: "text", text: getResultFinalOutput(result) || "(no output)" }],
1053
+ details: makeDetails("single")([result]),
1054
+ };
1055
+ } finally {
1056
+ status.clear();
1057
+ }
1058
+ }
1059
+
1060
+ const available = agents.map((a) => `${a.name} (${a.source})`).join(", ") || "none";
1061
+ return {
1062
+ content: [{ type: "text", text: `Invalid parameters. Available agents: ${available}` }],
1063
+ details: makeDetails("single")([]),
1064
+ };
1065
+ } finally {
1066
+ options.onSpawnEnd?.(harnessAgents.length);
1067
+ const mode = params.chain?.length
1068
+ ? "chain"
1069
+ : params.tasks?.length
1070
+ ? "parallel"
1071
+ : "single";
1072
+ options.onCompleted?.({
1073
+ agents: harnessAgents,
1074
+ mode,
1075
+ durationMs: Date.now() - startedAt,
1076
+ });
1077
+ }
1078
+ },
1079
+
1080
+ renderCall(args, theme, _context) {
1081
+ const scope: AgentScope =
1082
+ (args.agentScope as AgentScope | undefined) ?? defaultScope;
1083
+ if (args.chain && args.chain.length > 0) {
1084
+ let text =
1085
+ theme.fg("toolTitle", theme.bold("subagent ")) +
1086
+ theme.fg("accent", `chain (${args.chain.length} steps)`) +
1087
+ theme.fg("muted", ` [${scope}]`);
1088
+ for (let i = 0; i < Math.min(args.chain.length, 3); i++) {
1089
+ const step = args.chain[i];
1090
+ // Clean up {previous} placeholder for display
1091
+ const cleanTask = step.task.replace(/\{previous\}/g, "").trim();
1092
+ const preview = cleanTask.length > 40 ? `${cleanTask.slice(0, 40)}...` : cleanTask;
1093
+ text +=
1094
+ "\n " +
1095
+ theme.fg("muted", `${i + 1}.`) +
1096
+ " " +
1097
+ theme.fg("accent", step.agent) +
1098
+ theme.fg("dim", ` ${preview}`);
1099
+ }
1100
+ if (args.chain.length > 3) text += `\n ${theme.fg("muted", `... +${args.chain.length - 3} more`)}`;
1101
+ return new Text(text, 0, 0);
1102
+ }
1103
+ if (args.tasks && args.tasks.length > 0) {
1104
+ let text =
1105
+ theme.fg("toolTitle", theme.bold("subagent ")) +
1106
+ theme.fg("accent", `parallel (${args.tasks.length} tasks)`) +
1107
+ theme.fg("muted", ` [${scope}]`);
1108
+ for (const t of args.tasks.slice(0, 3)) {
1109
+ const preview = t.task.length > 40 ? `${t.task.slice(0, 40)}...` : t.task;
1110
+ text += `\n ${theme.fg("accent", t.agent)}${theme.fg("dim", ` ${preview}`)}`;
1111
+ }
1112
+ if (args.tasks.length > 3) text += `\n ${theme.fg("muted", `... +${args.tasks.length - 3} more`)}`;
1113
+ if (args.aggregator) {
1114
+ const preview =
1115
+ args.aggregator.task.length > 40 ? `${args.aggregator.task.slice(0, 40)}...` : args.aggregator.task;
1116
+ text += `\n ${theme.fg("muted", "fan-in → ")}${theme.fg("accent", args.aggregator.agent)}${theme.fg(
1117
+ "dim",
1118
+ ` ${preview}`,
1119
+ )}`;
1120
+ }
1121
+ return new Text(text, 0, 0);
1122
+ }
1123
+ const agentName = args.agent || "...";
1124
+ const preview = args.task ? (args.task.length > 60 ? `${args.task.slice(0, 60)}...` : args.task) : "...";
1125
+ let text =
1126
+ theme.fg("toolTitle", theme.bold("subagent ")) +
1127
+ theme.fg("accent", agentName) +
1128
+ theme.fg("muted", ` [${scope}]`);
1129
+ text += `\n ${theme.fg("dim", preview)}`;
1130
+ return new Text(text, 0, 0);
1131
+ },
1132
+
1133
+ renderResult(result, { expanded }, theme, _context) {
1134
+ const details = result.details as SubagentDetails | undefined;
1135
+ if (!details || details.results.length === 0) {
1136
+ const text = result.content[0];
1137
+ return new Text(text?.type === "text" ? text.text : "(no output)", 0, 0);
1138
+ }
1139
+
1140
+ const mdTheme = getMarkdownTheme();
1141
+
1142
+ const renderDisplayItems = (items: DisplayItem[], limit?: number) => {
1143
+ const toShow = limit ? items.slice(-limit) : items;
1144
+ const skipped = limit && items.length > limit ? items.length - limit : 0;
1145
+ let text = "";
1146
+ if (skipped > 0) text += theme.fg("muted", `... ${skipped} earlier items\n`);
1147
+ for (const item of toShow) {
1148
+ if (item.type === "text") {
1149
+ const preview = expanded ? item.text : item.text.split("\n").slice(0, 3).join("\n");
1150
+ text += `${theme.fg("toolOutput", preview)}\n`;
1151
+ } else {
1152
+ text += `${theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme))}\n`;
1153
+ }
1154
+ }
1155
+ return text.trimEnd();
1156
+ };
1157
+
1158
+ if (details.mode === "single" && details.results.length === 1) {
1159
+ const r = details.results[0];
1160
+ const isError = r.exitCode !== 0 || r.stopReason === "error" || r.stopReason === "aborted";
1161
+ const icon = isError ? theme.fg("error", "✗") : theme.fg("success", "✓");
1162
+ const displayItems = getDisplayItems(r.messages);
1163
+ const finalOutput = getResultFinalOutput(r);
1164
+
1165
+ if (expanded) {
1166
+ const container = new Container();
1167
+ let header = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
1168
+ if (isError && r.stopReason) header += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
1169
+ container.addChild(new Text(header, 0, 0));
1170
+ if (isError && r.errorMessage)
1171
+ container.addChild(new Text(theme.fg("error", `Error: ${r.errorMessage}`), 0, 0));
1172
+ container.addChild(new Spacer(1));
1173
+ container.addChild(new Text(theme.fg("muted", "─── Task ───"), 0, 0));
1174
+ container.addChild(new Text(theme.fg("dim", r.task), 0, 0));
1175
+ container.addChild(new Spacer(1));
1176
+ container.addChild(new Text(theme.fg("muted", "─── Output ───"), 0, 0));
1177
+ if (displayItems.length === 0 && !finalOutput) {
1178
+ container.addChild(new Text(theme.fg("muted", "(no output)"), 0, 0));
1179
+ } else {
1180
+ for (const item of displayItems) {
1181
+ if (item.type === "toolCall")
1182
+ container.addChild(
1183
+ new Text(
1184
+ theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
1185
+ 0,
1186
+ 0,
1187
+ ),
1188
+ );
1189
+ }
1190
+ if (finalOutput) {
1191
+ container.addChild(new Spacer(1));
1192
+ container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
1193
+ }
1194
+ }
1195
+ const usageStr = formatUsageStats(r.usage, r.model);
1196
+ if (usageStr) {
1197
+ container.addChild(new Spacer(1));
1198
+ container.addChild(new Text(theme.fg("dim", usageStr), 0, 0));
1199
+ }
1200
+ return container;
1201
+ }
1202
+
1203
+ let text = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
1204
+ if (isError && r.stopReason) text += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
1205
+ if (isError && r.errorMessage) text += `\n${theme.fg("error", `Error: ${r.errorMessage}`)}`;
1206
+ else if (displayItems.length === 0) text += `\n${theme.fg("muted", "(no output)")}`;
1207
+ else {
1208
+ text += `\n${renderDisplayItems(displayItems, COLLAPSED_ITEM_COUNT)}`;
1209
+ if (displayItems.length > COLLAPSED_ITEM_COUNT) text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
1210
+ }
1211
+ const usageStr = formatUsageStats(r.usage, r.model);
1212
+ if (usageStr) text += `\n${theme.fg("dim", usageStr)}`;
1213
+ return new Text(text, 0, 0);
1214
+ }
1215
+
1216
+ const aggregateUsage = (results: SingleResult[]) => {
1217
+ const total = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, turns: 0 };
1218
+ for (const r of results) {
1219
+ total.input += r.usage.input;
1220
+ total.output += r.usage.output;
1221
+ total.cacheRead += r.usage.cacheRead;
1222
+ total.cacheWrite += r.usage.cacheWrite;
1223
+ total.cost += r.usage.cost;
1224
+ total.turns += r.usage.turns;
1225
+ }
1226
+ return total;
1227
+ };
1228
+
1229
+ if (details.mode === "chain") {
1230
+ const successCount = details.results.filter((r) => r.exitCode === 0).length;
1231
+ const icon = successCount === details.results.length ? theme.fg("success", "✓") : theme.fg("error", "✗");
1232
+
1233
+ if (expanded) {
1234
+ const container = new Container();
1235
+ container.addChild(
1236
+ new Text(
1237
+ icon +
1238
+ " " +
1239
+ theme.fg("toolTitle", theme.bold("chain ")) +
1240
+ theme.fg("accent", `${successCount}/${details.results.length} steps`),
1241
+ 0,
1242
+ 0,
1243
+ ),
1244
+ );
1245
+
1246
+ for (const r of details.results) {
1247
+ const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
1248
+ const displayItems = getDisplayItems(r.messages);
1249
+ const finalOutput = getResultFinalOutput(r);
1250
+
1251
+ container.addChild(new Spacer(1));
1252
+ container.addChild(
1253
+ new Text(
1254
+ `${theme.fg("muted", `─── Step ${r.step}: `) + theme.fg("accent", r.agent)} ${rIcon}`,
1255
+ 0,
1256
+ 0,
1257
+ ),
1258
+ );
1259
+ container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0));
1260
+
1261
+ // Show tool calls
1262
+ for (const item of displayItems) {
1263
+ if (item.type === "toolCall") {
1264
+ container.addChild(
1265
+ new Text(
1266
+ theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
1267
+ 0,
1268
+ 0,
1269
+ ),
1270
+ );
1271
+ }
1272
+ }
1273
+
1274
+ // Show final output as markdown
1275
+ if (finalOutput) {
1276
+ container.addChild(new Spacer(1));
1277
+ container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
1278
+ }
1279
+
1280
+ const stepUsage = formatUsageStats(r.usage, r.model);
1281
+ if (stepUsage) container.addChild(new Text(theme.fg("dim", stepUsage), 0, 0));
1282
+ }
1283
+
1284
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
1285
+ if (usageStr) {
1286
+ container.addChild(new Spacer(1));
1287
+ container.addChild(new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0));
1288
+ }
1289
+ return container;
1290
+ }
1291
+
1292
+ // Collapsed view
1293
+ let text =
1294
+ icon +
1295
+ " " +
1296
+ theme.fg("toolTitle", theme.bold("chain ")) +
1297
+ theme.fg("accent", `${successCount}/${details.results.length} steps`);
1298
+ for (const r of details.results) {
1299
+ const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
1300
+ const displayItems = getDisplayItems(r.messages);
1301
+ text += `\n\n${theme.fg("muted", `─── Step ${r.step}: `)}${theme.fg("accent", r.agent)} ${rIcon}`;
1302
+ if (displayItems.length === 0) text += `\n${theme.fg("muted", "(no output)")}`;
1303
+ else text += `\n${renderDisplayItems(displayItems, 5)}`;
1304
+ }
1305
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
1306
+ if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
1307
+ text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
1308
+ return new Text(text, 0, 0);
1309
+ }
1310
+
1311
+ if (details.mode === "parallel") {
1312
+ const running = details.results.filter((r) => r.exitCode === -1).length;
1313
+ const successCount = details.results.filter((r) => r.exitCode === 0).length;
1314
+ const failCount = details.results.filter((r) => r.exitCode > 0).length;
1315
+ const aggregator = details.aggregator;
1316
+ const aggregatorRunning = aggregator?.exitCode === -1;
1317
+ const aggregatorFailed = aggregator ? aggregator.exitCode > 0 || aggregator.stopReason === "error" : false;
1318
+ const isRunning = running > 0 || aggregatorRunning;
1319
+ const icon = isRunning
1320
+ ? theme.fg("warning", "⏳")
1321
+ : failCount > 0 || aggregatorFailed
1322
+ ? theme.fg("warning", "◐")
1323
+ : theme.fg("success", "✓");
1324
+ const status = isRunning
1325
+ ? aggregatorRunning
1326
+ ? `${successCount + failCount}/${details.results.length} done, fan-in running`
1327
+ : `${successCount + failCount}/${details.results.length} done, ${running} running`
1328
+ : aggregator
1329
+ ? `${successCount}/${details.results.length} tasks + fan-in`
1330
+ : `${successCount}/${details.results.length} tasks`;
1331
+
1332
+ if (expanded && !isRunning) {
1333
+ const container = new Container();
1334
+ container.addChild(
1335
+ new Text(
1336
+ `${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`,
1337
+ 0,
1338
+ 0,
1339
+ ),
1340
+ );
1341
+
1342
+ for (const r of details.results) {
1343
+ const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
1344
+ const displayItems = getDisplayItems(r.messages);
1345
+ const finalOutput = getResultFinalOutput(r);
1346
+
1347
+ container.addChild(new Spacer(1));
1348
+ container.addChild(
1349
+ new Text(`${theme.fg("muted", "─── ") + theme.fg("accent", r.agent)} ${rIcon}`, 0, 0),
1350
+ );
1351
+ container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0));
1352
+
1353
+ // Show tool calls
1354
+ for (const item of displayItems) {
1355
+ if (item.type === "toolCall") {
1356
+ container.addChild(
1357
+ new Text(
1358
+ theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
1359
+ 0,
1360
+ 0,
1361
+ ),
1362
+ );
1363
+ }
1364
+ }
1365
+
1366
+ // Show final output as markdown
1367
+ if (finalOutput) {
1368
+ container.addChild(new Spacer(1));
1369
+ container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
1370
+ }
1371
+
1372
+ const taskUsage = formatUsageStats(r.usage, r.model);
1373
+ if (taskUsage) container.addChild(new Text(theme.fg("dim", taskUsage), 0, 0));
1374
+ }
1375
+
1376
+ if (aggregator) {
1377
+ const rIcon = aggregator.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
1378
+ const displayItems = getDisplayItems(aggregator.messages);
1379
+ const finalOutput = getResultFinalOutput(aggregator);
1380
+
1381
+ container.addChild(new Spacer(1));
1382
+ container.addChild(
1383
+ new Text(
1384
+ `${theme.fg("muted", "─── fan-in → ") + theme.fg("accent", aggregator.agent)} ${rIcon}`,
1385
+ 0,
1386
+ 0,
1387
+ ),
1388
+ );
1389
+ container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", aggregator.task), 0, 0));
1390
+ for (const item of displayItems) {
1391
+ if (item.type === "toolCall") {
1392
+ container.addChild(
1393
+ new Text(
1394
+ theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
1395
+ 0,
1396
+ 0,
1397
+ ),
1398
+ );
1399
+ }
1400
+ }
1401
+ if (finalOutput) {
1402
+ container.addChild(new Spacer(1));
1403
+ container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
1404
+ }
1405
+ const fanInUsage = formatUsageStats(aggregator.usage, aggregator.model);
1406
+ if (fanInUsage) container.addChild(new Text(theme.fg("dim", fanInUsage), 0, 0));
1407
+ }
1408
+
1409
+ const usageResults = aggregator ? [...details.results, aggregator] : details.results;
1410
+ const usageStr = formatUsageStats(aggregateUsage(usageResults));
1411
+ if (usageStr) {
1412
+ container.addChild(new Spacer(1));
1413
+ container.addChild(new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0));
1414
+ }
1415
+ return container;
1416
+ }
1417
+
1418
+ // Collapsed view (or still running)
1419
+ let text = `${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`;
1420
+ for (const r of details.results) {
1421
+ const rIcon =
1422
+ r.exitCode === -1
1423
+ ? theme.fg("warning", "⏳")
1424
+ : r.exitCode === 0
1425
+ ? theme.fg("success", "✓")
1426
+ : theme.fg("error", "✗");
1427
+ const displayItems = getDisplayItems(r.messages);
1428
+ text += `\n\n${theme.fg("muted", "─── ")}${theme.fg("accent", r.agent)} ${rIcon}`;
1429
+ if (displayItems.length === 0)
1430
+ text += `\n${theme.fg("muted", r.exitCode === -1 ? "(running...)" : "(no output)")}`;
1431
+ else text += `\n${renderDisplayItems(displayItems, 5)}`;
1432
+ }
1433
+ if (aggregator) {
1434
+ const rIcon =
1435
+ aggregator.exitCode === -1
1436
+ ? theme.fg("warning", "⏳")
1437
+ : aggregator.exitCode === 0
1438
+ ? theme.fg("success", "✓")
1439
+ : theme.fg("error", "✗");
1440
+ const displayItems = getDisplayItems(aggregator.messages);
1441
+ text += `\n\n${theme.fg("muted", "─── fan-in → ")}${theme.fg("accent", aggregator.agent)} ${rIcon}`;
1442
+ if (displayItems.length === 0)
1443
+ text += `\n${theme.fg("muted", aggregator.exitCode === -1 ? "(running...)" : "(no output)")}`;
1444
+ else text += `\n${renderDisplayItems(displayItems, 5)}`;
1445
+ }
1446
+ if (!isRunning) {
1447
+ const usageResults = aggregator ? [...details.results, aggregator] : details.results;
1448
+ const usageStr = formatUsageStats(aggregateUsage(usageResults));
1449
+ if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
1450
+ }
1451
+ if (!expanded) text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
1452
+ return new Text(text, 0, 0);
1453
+ }
1454
+
1455
+ const text = result.content[0];
1456
+ return new Text(text?.type === "text" ? text.text : "(no output)", 0, 0);
1457
+ },
1458
+ });
1459
+ }
1460
+
1461
+ export default function harnessSubagentsExtension(pi: ExtensionAPI) {
1462
+ return createSubagentsExtension(pi, {});
1463
+ }