-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathsubagent.ts
More file actions
687 lines (621 loc) · 24.1 KB
/
Copy pathsubagent.ts
File metadata and controls
687 lines (621 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
import { spawnSync } from "node:child_process";
import { randomUUID } from "node:crypto";
import { existsSync } from "node:fs";
import { mkdir, readFile, rename, stat, writeFile } from "node:fs/promises";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { StringEnum } from "@earendil-works/pi-ai";
import {
DEFAULT_MAX_BYTES,
DEFAULT_MAX_LINES,
getAgentDir,
truncateHead,
type ExtensionAPI,
type ExtensionContext,
} from "@earendil-works/pi-coding-agent";
import { Text } from "@earendil-works/pi-tui";
import { Type } from "typebox";
const ATTACH_FLAG = "attach-subagent";
const CHILD_ENV = "PI_TMUX_SUBAGENT_CHILD";
const RESULT_ENV = "PI_TMUX_SUBAGENT_RESULT";
const RUNS_DIR = "tmux-subagents";
const POLL_INTERVAL_MS = 500;
const PANE_PREVIEW_LINES = 18;
const THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"] as const;
const EXTENSION_PATH = fileURLToPath(import.meta.url);
type RunStatus = "queued" | "running" | "completed" | "failed";
interface ChildResult {
version: 1;
status: "completed" | "failed";
output: string;
error?: string;
stopReason?: string;
sessionFile?: string;
provider?: string;
model?: string;
thinking?: string;
finishedAt: number;
}
interface RunDetails {
status: RunStatus;
task: string;
cwd: string;
tmuxSession: string;
attachCommand: string;
captureCommand: string;
killCommand: string;
provider: string;
model: string;
thinking: string;
pane?: string;
output?: string;
sessionFile?: string;
startedAt?: number;
finishedAt?: number;
}
interface RunSpec {
task: string;
cwd: string;
attachmentId: string;
tmuxSession: string;
tmuxTarget: string;
attachCommand: string;
captureCommand: string;
killCommand: string;
provider: string;
model: string;
thinking: string;
trusted: boolean;
}
function shellQuote(value: string): string {
if (value.length === 0) return "''";
return `'${value.replace(/'/g, `'"'"'`)}'`;
}
function tmuxSocketPath(): string {
return path.join(getAgentDir(), "tmux-subagents.sock");
}
function tmuxSessionName(sessionId: string): string {
return `pi-agent-${sessionId}`;
}
function currentTmuxSocket(): string | undefined {
const socket = process.env.TMUX?.split(",", 1)[0]?.trim();
return socket || undefined;
}
function attachFlagValue(argv: string[]): string | undefined {
const flag = `--${ATTACH_FLAG}`;
for (let index = 2; index < argv.length; index++) {
const argument = argv[index];
if (argument === "--") break;
if (argument === flag) {
const value = argv[index + 1];
return !value || value.startsWith("--") ? "" : value;
}
if (argument.startsWith(`${flag}=`)) return argument.slice(flag.length + 1);
}
return undefined;
}
function tmuxCommandPrefix(): string {
return `tmux -S ${shellQuote(tmuxSocketPath())}`;
}
function tmuxArgs(...args: string[]): string[] {
return ["-S", tmuxSocketPath(), ...args];
}
function updateTmuxCommands(spec: RunSpec): void {
const tmux = tmuxCommandPrefix();
spec.attachCommand = `pi --${ATTACH_FLAG} ${shellQuote(spec.attachmentId)}`;
spec.captureCommand = `${tmux} capture-pane -p -J -t ${shellQuote(spec.tmuxTarget)}`;
spec.killCommand = `${tmux} kill-session -t ${shellQuote(spec.tmuxSession)}`;
}
function attachToSubagentAndExit(rawTarget: string): never {
const target = rawTarget.trim();
if (!target) {
console.error(`Error: --${ATTACH_FLAG} requires the session id printed by the subagent tool.`);
process.exit(2);
}
let socket = tmuxSocketPath();
let session: string;
if (target.startsWith("v1.")) {
// Keep attachment working for sessions started before session-id targets.
try {
const legacy = JSON.parse(Buffer.from(target.slice(3), "base64url").toString("utf8")) as {
s?: unknown;
p?: unknown;
};
if (typeof legacy.s !== "string" || !legacy.s || typeof legacy.p !== "string" || !legacy.p) {
throw new Error("missing tmux session or socket");
}
session = legacy.s;
socket = legacy.p;
} catch (error) {
console.error(`Error: invalid legacy subagent target: ${error instanceof Error ? error.message : String(error)}`);
process.exit(2);
}
} else {
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(target)) {
console.error(`Error: invalid subagent session id: ${target}`);
process.exit(2);
}
session = tmuxSessionName(target);
}
const sameServer = currentTmuxSocket() === socket;
const args = ["-S", socket, sameServer ? "switch-client" : "attach-session", "-t", session];
const env = { ...process.env };
if (!sameServer) {
delete env.TMUX;
delete env.TMUX_PANE;
}
const result = spawnSync("tmux", args, { stdio: "inherit", env });
if (result.error) console.error(`Failed to run tmux: ${result.error.message}`);
process.exit(result.status ?? 1);
}
function getPiInvocationParts(): string[] {
const currentScript = process.argv[1];
if (currentScript && existsSync(currentScript)) {
return [process.execPath, currentScript];
}
const execName = path.basename(process.execPath).toLowerCase();
if (!/^(node|bun)(\.exe)?$/.test(execName)) {
return [process.execPath];
}
return ["pi"];
}
function textFromAssistant(message: Record<string, unknown>): string {
const content = message.content;
if (typeof content === "string") return content;
if (!Array.isArray(content)) return "";
return content
.filter((part): part is { type: "text"; text: string } => {
return Boolean(part && typeof part === "object" && part.type === "text" && typeof part.text === "string");
})
.map((part) => part.text)
.join("\n");
}
function findLastAssistant(ctx: ExtensionContext): Record<string, unknown> | undefined {
const branch = ctx.sessionManager.getBranch();
for (let index = branch.length - 1; index >= 0; index--) {
const entry = branch[index];
if (entry.type !== "message") continue;
const message = entry.message as unknown as Record<string, unknown>;
if (message.role === "assistant") return message;
}
return undefined;
}
async function writeJsonAtomic(filePath: string, value: unknown): Promise<void> {
const temporaryPath = `${filePath}.${process.pid}.${randomUUID()}.tmp`;
await writeFile(temporaryPath, `${JSON.stringify(value)}\n`, { encoding: "utf8", mode: 0o600 });
await rename(temporaryPath, filePath);
}
function registerChildReporter(pi: ExtensionAPI, resultPath: string): void {
let reported = false;
const report = async (ctx: ExtensionContext, fallbackError?: string): Promise<void> => {
if (reported) return;
reported = true;
const assistant = findLastAssistant(ctx);
const stopReason = typeof assistant?.stopReason === "string" ? assistant.stopReason : undefined;
const assistantError = typeof assistant?.errorMessage === "string" ? assistant.errorMessage : undefined;
const failed = !assistant || stopReason === "error" || stopReason === "aborted" || Boolean(fallbackError);
const output = assistant ? textFromAssistant(assistant) : "";
const result: ChildResult = {
version: 1,
status: failed ? "failed" : "completed",
output,
error: fallbackError ?? assistantError ?? (!assistant ? "Subagent exited without an assistant response." : undefined),
stopReason,
sessionFile: ctx.sessionManager.getSessionFile(),
provider: typeof assistant?.provider === "string" ? assistant.provider : ctx.model?.provider,
model: typeof assistant?.model === "string" ? assistant.model : ctx.model?.id,
thinking: pi.getThinkingLevel(),
finishedAt: Date.now(),
};
try {
await writeJsonAtomic(resultPath, result);
} catch (error) {
console.error(`[tmux-subagent] Failed to write result: ${error instanceof Error ? error.message : String(error)}`);
}
};
// agent_settled was added after older peer type declarations but is present
// in the Pi runtime this extension targets.
(
pi.on as unknown as (
event: "agent_settled",
handler: (event: unknown, ctx: ExtensionContext) => void | Promise<void>,
) => void
)("agent_settled", async (_event, ctx) => {
await report(ctx);
ctx.shutdown();
});
pi.on("session_shutdown", async (_event, ctx) => {
if (!reported) await report(ctx, "Subagent session shut down before the task settled.");
});
}
function trimPane(output: string): string {
const lines = output.replace(/\r/g, "").split("\n");
while (lines.length > 0 && !lines[0]?.trim()) lines.shift();
while (lines.length > 0 && !lines[lines.length - 1]?.trim()) lines.pop();
return lines.slice(-PANE_PREVIEW_LINES).join("\n");
}
function formatDuration(startedAt: number | undefined, finishedAt = Date.now()): string | undefined {
if (startedAt === undefined) return undefined;
const seconds = Math.max(0, Math.round((finishedAt - startedAt) / 1000));
if (seconds < 60) return `${seconds}s`;
const minutes = Math.floor(seconds / 60);
return `${minutes}m ${seconds % 60}s`;
}
function detailsFor(spec: RunSpec, status: RunStatus, extra: Partial<RunDetails> = {}): RunDetails {
return {
status,
task: spec.task,
cwd: spec.cwd,
tmuxSession: spec.tmuxSession,
attachCommand: spec.attachCommand,
captureCommand: spec.captureCommand,
killCommand: spec.killCommand,
provider: spec.provider,
model: spec.model,
thinking: spec.thinking,
...extra,
};
}
function partialText(details: RunDetails): string {
const lines = [
`Subagent ${details.status} in tmux session ${details.tmuxSession}.`,
`Attach: ${details.attachCommand}`,
`Capture: ${details.captureCommand}`,
];
if (details.pane) lines.push("", details.pane);
return lines.join("\n");
}
function truncateToolText(text: string): string {
const truncated = truncateHead(text, { maxBytes: DEFAULT_MAX_BYTES, maxLines: DEFAULT_MAX_LINES });
if (!truncated.truncated) return truncated.content;
return `${truncated.content}\n\n[Output truncated. Full output is available in the child session file.]`;
}
function resultText(details: RunDetails): string {
const duration = formatDuration(details.startedAt, details.finishedAt);
const lines = [
`Subagent ${details.status}${duration ? ` after ${duration}` : ""}.`,
`Model: ${details.provider}/${details.model} (${details.thinking})`,
`tmux: ${details.tmuxSession}`,
`Attach: ${details.attachCommand}`,
`Capture: ${details.captureCommand}`,
`Clean up: ${details.killCommand}`,
];
if (details.sessionFile) lines.push(`Child session: ${details.sessionFile}`);
if (details.output) lines.push("", details.output);
return truncateToolText(lines.join("\n"));
}
async function abortableDelay(ms: number, signal: AbortSignal | undefined): Promise<void> {
if (signal?.aborted) throw new Error("Subagent aborted.");
await new Promise<void>((resolve, reject) => {
const cleanup = () => signal?.removeEventListener("abort", onAbort);
const timer = setTimeout(() => {
cleanup();
resolve();
}, ms);
const onAbort = () => {
clearTimeout(timer);
cleanup();
reject(new Error("Subagent aborted."));
};
signal?.addEventListener("abort", onAbort, { once: true });
});
}
async function validateCwd(cwd: string): Promise<void> {
let info;
try {
info = await stat(cwd);
} catch {
throw new Error(`Subagent working directory does not exist: ${cwd}`);
}
if (!info.isDirectory()) throw new Error(`Subagent working directory is not a directory: ${cwd}`);
}
function isSameOrDescendant(base: string, candidate: string): boolean {
const relative = path.relative(base, candidate);
return relative === "" || (relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative));
}
function resolveModel(
ctx: ExtensionContext,
providerOverride: string | undefined,
modelOverride: string | undefined,
): { provider: string; model: string } {
const explicitProvider = providerOverride?.trim();
const explicitModel = modelOverride?.trim();
let provider = explicitProvider || ctx.model?.provider || "";
let model = explicitModel || ctx.model?.id || "";
// A slash in an inherited id can be part of the id itself (for example,
// OpenRouter's openai/gpt-* models). Only interpret an explicit model as
// provider/model when no separate provider was supplied. If both are given
// and the prefixes agree, accept the redundant canonical provider/model form.
const slashIndex = explicitModel?.indexOf("/") ?? -1;
if (explicitModel && slashIndex > 0) {
const modelProvider = explicitModel.slice(0, slashIndex);
if (!explicitProvider) {
provider = modelProvider;
model = explicitModel.slice(slashIndex + 1);
} else if (explicitProvider === modelProvider) {
model = explicitModel.slice(slashIndex + 1);
}
}
if (!provider || !model) {
throw new Error("No model is active. Pass both provider and model to the subagent tool.");
}
return { provider, model };
}
export default function subagentExtension(pi: ExtensionAPI): void {
pi.registerFlag(ATTACH_FLAG, {
description: "Attach using the child session id printed by the subagent tool",
type: "string",
});
const attachTarget = attachFlagValue(process.argv);
if (attachTarget !== undefined) attachToSubagentAndExit(attachTarget);
if (process.env[CHILD_ENV] === "1") {
const resultPath = process.env[RESULT_ENV];
if (!resultPath) {
console.error(`[tmux-subagent] ${RESULT_ENV} is required in child mode.`);
return;
}
registerChildReporter(pi, resultPath);
return;
}
let queueTail: Promise<void> = Promise.resolve();
let queueDepth = 0;
let activeSession: string | undefined;
const withSerialExecution = async <T>(
signal: AbortSignal | undefined,
onQueued: () => void,
fn: () => Promise<T>,
): Promise<T> => {
const queued = queueDepth > 0;
queueDepth++;
const previous = queueTail;
let release!: () => void;
queueTail = new Promise<void>((resolve) => {
release = resolve;
});
if (queued) onQueued();
try {
await previous;
if (signal?.aborted) throw new Error("Subagent aborted while waiting in the serial queue.");
return await fn();
} finally {
queueDepth--;
release();
}
};
pi.on("session_shutdown", async () => {
if (!activeSession) return;
await pi.exec("tmux", tmuxArgs("kill-session", "-t", activeSession));
activeSession = undefined;
});
pi.registerTool({
name: "subagent",
label: "Subagent",
description:
"Run one delegated task in a separate interactive Pi process inside tmux. Calls are serialized: only one child works at a time, even if several calls are requested together. The child inherits the current provider, model, and thinking level unless overridden. Live pane output and a copy/paste pi --attach-subagent command are shown while it runs. Output is capped at 50KB or 2000 lines; the complete child session is preserved on disk.",
promptSnippet: "Run one delegated task in an observable, tmux-backed Pi session",
promptGuidelines: [
"Use subagent once per delegated task; subagent calls are serialized automatically, so prefer multiple simple calls over asking one child to orchestrate other children.",
],
parameters: Type.Object({
task: Type.String({ description: "The complete task for the child Pi process" }),
cwd: Type.Optional(Type.String({ description: "Working directory. Defaults to the current project." })),
provider: Type.Optional(Type.String({ description: "Provider override. Defaults to the current provider." })),
model: Type.Optional(
Type.String({ description: "Model id or provider/model override. Defaults to the current model." }),
),
thinking: Type.Optional(
StringEnum(THINKING_LEVELS, {
description: "Thinking level override. Defaults to the current thinking level.",
}),
),
}),
async execute(_toolCallId, params, signal, onUpdate, ctx) {
if (!params.task.trim()) throw new Error("Subagent task must not be empty.");
const cwd = path.resolve(ctx.cwd, params.cwd?.trim() || ".");
const selectedModel = resolveModel(ctx, params.provider, params.model);
const thinking = params.thinking ?? pi.getThinkingLevel();
const childSessionId = randomUUID();
const runDir = path.join(getAgentDir(), RUNS_DIR, ctx.sessionManager.getSessionId(), childSessionId);
const resultPath = path.join(runDir, "result.json");
const tmuxSession = tmuxSessionName(childSessionId);
const tmuxTarget = `${tmuxSession}:0.0`;
const spec: RunSpec = {
task: params.task,
cwd,
attachmentId: childSessionId,
tmuxSession,
tmuxTarget,
attachCommand: "",
captureCommand: "",
killCommand: "",
provider: selectedModel.provider,
model: selectedModel.model,
thinking,
trusted: isSameOrDescendant(path.resolve(ctx.cwd), cwd) && ctx.isProjectTrusted(),
};
updateTmuxCommands(spec);
return withSerialExecution(
signal,
() => {
const details = detailsFor(spec, "queued");
onUpdate?.({ content: [{ type: "text", text: "Waiting for the active subagent to finish..." }], details });
},
async () => {
await validateCwd(cwd);
await mkdir(runDir, { recursive: true, mode: 0o700 });
const promptPath = path.join(runDir, "task.md");
const sessionDir = path.join(runDir, "session");
await mkdir(sessionDir, { recursive: true, mode: 0o700 });
await writeFile(promptPath, `# Delegated task\n\n${params.task}\n`, {
encoding: "utf8",
mode: 0o600,
});
const piArgs = [
...getPiInvocationParts(),
"--provider",
selectedModel.provider,
"--model",
selectedModel.model,
"--thinking",
thinking,
"--session-dir",
sessionDir,
"--session-id",
childSessionId,
"--name",
tmuxSession,
spec.trusted ? "--approve" : "--no-approve",
"--extension",
EXTENSION_PATH,
`@${promptPath}`,
];
const childCommand = [
"exec env",
`${CHILD_ENV}=1`,
`${RESULT_ENV}=${shellQuote(resultPath)}`,
piArgs.map(shellQuote).join(" "),
].join(" ");
const tmuxVersion = await pi.exec("tmux", ["-V"], { timeout: 5_000 });
if (tmuxVersion.code !== 0) {
throw new Error(`tmux is required for subagents: ${tmuxVersion.stderr.trim() || "tmux not found"}`);
}
const startedAt = Date.now();
const created = await pi.exec(
"tmux",
tmuxArgs("new-session", "-d", "-s", tmuxSession, "-n", "pi", "-c", cwd),
);
if (created.code !== 0) {
throw new Error(`Failed to create tmux session: ${created.stderr.trim() || created.stdout.trim()}`);
}
activeSession = tmuxSession;
try {
const remain = await pi.exec(
"tmux",
tmuxArgs("set-window-option", "-t", `${tmuxSession}:0`, "remain-on-exit", "on"),
);
if (remain.code !== 0) throw new Error(remain.stderr.trim() || "Failed to set remain-on-exit.");
const initialDetails = detailsFor(spec, "running", { startedAt });
onUpdate?.({ content: [{ type: "text", text: partialText(initialDetails) }], details: initialDetails });
const sent = await pi.exec("tmux", tmuxArgs("send-keys", "-t", tmuxTarget, "-l", "--", childCommand));
if (sent.code !== 0) throw new Error(sent.stderr.trim() || "Failed to start child Pi.");
const entered = await pi.exec("tmux", tmuxArgs("send-keys", "-t", tmuxTarget, "Enter"));
if (entered.code !== 0) throw new Error(entered.stderr.trim() || "Failed to submit child command.");
let lastPane = "";
let childResult: ChildResult | undefined;
while (!childResult) {
if (signal?.aborted) throw new Error("Subagent aborted.");
try {
childResult = JSON.parse(await readFile(resultPath, "utf8")) as ChildResult;
break;
} catch {
// The result file is created atomically when the child settles.
}
const paneResult = await pi.exec("tmux", tmuxArgs("capture-pane", "-p", "-J", "-t", tmuxTarget), {
timeout: 5_000,
});
if (paneResult.code === 0) {
const pane = trimPane(paneResult.stdout);
if (pane && pane !== lastPane) {
lastPane = pane;
const details = detailsFor(spec, "running", { pane, startedAt });
onUpdate?.({ content: [{ type: "text", text: partialText(details) }], details });
}
}
const dead = await pi.exec(
"tmux",
tmuxArgs("display-message", "-p", "-t", tmuxTarget, "#{pane_dead}"),
);
if (dead.code === 0 && dead.stdout.trim() === "1") {
await abortableDelay(100, signal);
try {
childResult = JSON.parse(await readFile(resultPath, "utf8")) as ChildResult;
break;
} catch {
throw new Error(
`Child Pi exited before reporting a result.\n\n${lastPane || "No pane output."}\n\nInspect: ${spec.captureCommand}`,
);
}
}
await abortableDelay(POLL_INTERVAL_MS, signal);
}
const finalPaneResult = await pi.exec("tmux", tmuxArgs("capture-pane", "-p", "-J", "-t", tmuxTarget), {
timeout: 5_000,
});
const finalPane = finalPaneResult.code === 0 ? trimPane(finalPaneResult.stdout) : lastPane;
const status: RunStatus = childResult.status === "completed" ? "completed" : "failed";
let rawOutput = childResult.output.trim();
if (childResult.status === "failed" && childResult.error?.trim()) {
rawOutput += `${rawOutput ? "\n\n" : ""}Error: ${childResult.error.trim()}`;
}
const output = truncateToolText(rawOutput || "(no text output)");
const details = detailsFor(spec, status, {
pane: finalPane,
output,
sessionFile: childResult.sessionFile,
provider: childResult.provider ?? spec.provider,
model: childResult.model ?? spec.model,
thinking: childResult.thinking ?? spec.thinking,
startedAt,
finishedAt: childResult.finishedAt,
});
if (childResult.status === "failed") {
throw new Error(resultText(details));
}
return {
content: [{ type: "text", text: resultText(details) }],
details,
};
} catch (error) {
if (signal?.aborted) {
await pi.exec("tmux", tmuxArgs("kill-session", "-t", tmuxSession));
activeSession = undefined;
}
throw error;
} finally {
if (activeSession === tmuxSession) activeSession = undefined;
}
},
);
},
renderCall(args, theme) {
const task = args.task?.trim() || "...";
const firstLine = task.split("\n", 1)[0] ?? task;
const preview = firstLine.length > 100 ? `${firstLine.slice(0, 100)}…` : firstLine;
let text = theme.fg("toolTitle", theme.bold("subagent ")) + theme.fg("dim", preview);
const overrides = [args.provider, args.model, args.thinking].filter(Boolean);
if (overrides.length > 0) text += `\n ${theme.fg("muted", overrides.join(" · "))}`;
return new Text(text, 0, 0);
},
renderResult(result, { expanded, isPartial }, theme) {
const details = result.details as RunDetails | undefined;
if (!details) {
const content = result.content.find((part) => part.type === "text");
return new Text(content?.type === "text" ? content.text : "(no output)", 0, 0);
}
const running = isPartial || details.status === "queued" || details.status === "running";
const icon = running
? theme.fg("warning", details.status === "queued" ? "◦" : "●")
: details.status === "completed"
? theme.fg("success", "✓")
: theme.fg("error", "✗");
const duration = formatDuration(details.startedAt, details.finishedAt);
let text = `${icon} ${theme.fg("toolTitle", theme.bold(details.tmuxSession))}`;
text += theme.fg("muted", ` · ${details.status}${duration ? ` · ${duration}` : ""}`);
text += `\n ${theme.fg("accent", details.attachCommand)}`;
text += `\n ${theme.fg("dim", `${details.provider}/${details.model} (${details.thinking})`)}`;
if (running && details.pane) {
const paneLines = details.pane.split("\n");
const visible = expanded ? paneLines : paneLines.slice(-8);
text += `\n\n${visible.map((line) => theme.fg("dim", line)).join("\n")}`;
} else if (!running && details.output) {
const outputLines = details.output.split("\n");
const visible = expanded ? outputLines : outputLines.slice(0, 8);
text += `\n\n${visible.map((line) => theme.fg("toolOutput", line)).join("\n")}`;
if (!expanded && outputLines.length > visible.length) text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
text += `\n\n ${theme.fg("dim", `capture: ${details.captureCommand}`)}`;
text += `\n ${theme.fg("dim", `cleanup: ${details.killCommand}`)}`;
}
return new Text(text, 0, 0);
},
});
}