@aexol/spectral 0.4.3 → 0.4.8

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,951 @@
1
+ /**
2
+ * Subagent Delegation Extension for Spectral
3
+ *
4
+ * Registers a `subagent` tool that allows the coding agent to delegate tasks
5
+ * to specialized sub-agents running in isolated `pi` / `spectral` processes.
6
+ *
7
+ * Each subagent invocation spawns a separate process with its own context
8
+ * window, keeping the main conversation lean and focused.
9
+ *
10
+ * Three execution modes:
11
+ * - Single: { agent: "name", task: "..." }
12
+ * - Parallel: { tasks: [{ agent, task }, ...] } (max 8, 4 concurrent)
13
+ * - Chain: { chain: [{ agent, task: "... {previous} ..." }, ...] }
14
+ *
15
+ * Agent definitions live as markdown files with YAML frontmatter:
16
+ * - ~/.pi/agent/agents/*.md (user-level, always loaded)
17
+ * - .pi/agents/*.md (project-level, opt-in via agentScope)
18
+ *
19
+ * The subagent binary is auto-detected:
20
+ * - If running via `spectral`, uses the same invocation path
21
+ * - Falls back to `pi` otherwise
22
+ */
23
+ import { spawn } from "node:child_process";
24
+ import * as fs from "node:fs";
25
+ import * as os from "node:os";
26
+ import * as path from "node:path";
27
+ import { StringEnum } from "@mariozechner/pi-ai";
28
+ import { getMarkdownTheme, withFileMutationQueue, } from "@mariozechner/pi-coding-agent";
29
+ import { Container, Markdown, Spacer, Text } from "@mariozechner/pi-tui";
30
+ import { Type } from "typebox";
31
+ import { discoverAgents } from "./agents.js";
32
+ // ---------------------------------------------------------------------------
33
+ // Constants
34
+ // ---------------------------------------------------------------------------
35
+ const MAX_PARALLEL_TASKS = 8;
36
+ const MAX_CONCURRENCY = 4;
37
+ const COLLAPSED_ITEM_COUNT = 10;
38
+ // ---------------------------------------------------------------------------
39
+ // Helpers — formatting
40
+ // ---------------------------------------------------------------------------
41
+ function formatTokens(count) {
42
+ if (count < 1000)
43
+ return count.toString();
44
+ if (count < 10000)
45
+ return `${(count / 1000).toFixed(1)}k`;
46
+ if (count < 1000000)
47
+ return `${Math.round(count / 1000)}k`;
48
+ return `${(count / 1000000).toFixed(1)}M`;
49
+ }
50
+ function formatUsageStats(usage, model) {
51
+ const parts = [];
52
+ if (usage.turns)
53
+ parts.push(`${usage.turns} turn${usage.turns > 1 ? "s" : ""}`);
54
+ if (usage.input)
55
+ parts.push(`↑${formatTokens(usage.input)}`);
56
+ if (usage.output)
57
+ parts.push(`↓${formatTokens(usage.output)}`);
58
+ if (usage.cacheRead)
59
+ parts.push(`R${formatTokens(usage.cacheRead)}`);
60
+ if (usage.cacheWrite)
61
+ parts.push(`W${formatTokens(usage.cacheWrite)}`);
62
+ if (usage.cost)
63
+ parts.push(`$${usage.cost.toFixed(4)}`);
64
+ if (usage.contextTokens && usage.contextTokens > 0) {
65
+ parts.push(`ctx:${formatTokens(usage.contextTokens)}`);
66
+ }
67
+ if (model)
68
+ parts.push(model);
69
+ return parts.join(" ");
70
+ }
71
+ function formatToolCall(toolName, args, themeFg) {
72
+ const shortenPath = (p) => {
73
+ const home = os.homedir();
74
+ return p.startsWith(home) ? `~${p.slice(home.length)}` : p;
75
+ };
76
+ switch (toolName) {
77
+ case "bash": {
78
+ const command = args.command || "...";
79
+ const preview = command.length > 60 ? `${command.slice(0, 60)}...` : command;
80
+ return themeFg("muted", "$ ") + themeFg("toolOutput", preview);
81
+ }
82
+ case "read": {
83
+ const rawPath = (args.file_path || args.path || "...");
84
+ const filePath = shortenPath(rawPath);
85
+ const offset = args.offset;
86
+ const limit = args.limit;
87
+ let text = themeFg("accent", filePath);
88
+ if (offset !== undefined || limit !== undefined) {
89
+ const startLine = offset ?? 1;
90
+ const endLine = limit !== undefined ? startLine + limit - 1 : "";
91
+ text += themeFg("warning", `:${startLine}${endLine ? `-${endLine}` : ""}`);
92
+ }
93
+ return themeFg("muted", "read ") + text;
94
+ }
95
+ case "write": {
96
+ const rawPath = (args.file_path || args.path || "...");
97
+ const filePath = shortenPath(rawPath);
98
+ const content = (args.content || "");
99
+ const lines = content.split("\n").length;
100
+ let text = themeFg("muted", "write ") + themeFg("accent", filePath);
101
+ if (lines > 1)
102
+ text += themeFg("dim", ` (${lines} lines)`);
103
+ return text;
104
+ }
105
+ case "edit": {
106
+ const rawPath = (args.file_path || args.path || "...");
107
+ return themeFg("muted", "edit ") + themeFg("accent", shortenPath(rawPath));
108
+ }
109
+ case "ls": {
110
+ const rawPath = (args.path || ".");
111
+ return themeFg("muted", "ls ") + themeFg("accent", shortenPath(rawPath));
112
+ }
113
+ case "find": {
114
+ const pattern = (args.pattern || "*");
115
+ const rawPath = (args.path || ".");
116
+ return (themeFg("muted", "find ") +
117
+ themeFg("accent", pattern) +
118
+ themeFg("dim", ` in ${shortenPath(rawPath)}`));
119
+ }
120
+ case "grep": {
121
+ const pattern = (args.pattern || "");
122
+ const rawPath = (args.path || ".");
123
+ return (themeFg("muted", "grep ") +
124
+ themeFg("accent", `/${pattern}/`) +
125
+ themeFg("dim", ` in ${shortenPath(rawPath)}`));
126
+ }
127
+ default: {
128
+ const argsStr = JSON.stringify(args);
129
+ const preview = argsStr.length > 50 ? `${argsStr.slice(0, 50)}...` : argsStr;
130
+ return themeFg("accent", toolName) + themeFg("dim", ` ${preview}`);
131
+ }
132
+ }
133
+ }
134
+ // ---------------------------------------------------------------------------
135
+ // Helpers — message extraction
136
+ // ---------------------------------------------------------------------------
137
+ function getFinalOutput(messages) {
138
+ for (let i = messages.length - 1; i >= 0; i--) {
139
+ const msg = messages[i];
140
+ if (msg.role === "assistant") {
141
+ for (const part of msg.content) {
142
+ if (part.type === "text")
143
+ return part.text;
144
+ }
145
+ }
146
+ }
147
+ return "";
148
+ }
149
+ function getDisplayItems(messages) {
150
+ const items = [];
151
+ for (const msg of messages) {
152
+ if (msg.role === "assistant") {
153
+ for (const part of msg.content) {
154
+ if (part.type === "text")
155
+ items.push({ type: "text", text: part.text });
156
+ else if (part.type === "toolCall")
157
+ items.push({ type: "toolCall", name: part.name, args: part.arguments });
158
+ }
159
+ }
160
+ }
161
+ return items;
162
+ }
163
+ // ---------------------------------------------------------------------------
164
+ // Helpers — concurrency, temp files, binary detection
165
+ // ---------------------------------------------------------------------------
166
+ async function mapWithConcurrencyLimit(items, concurrency, fn) {
167
+ if (items.length === 0)
168
+ return [];
169
+ const limit = Math.max(1, Math.min(concurrency, items.length));
170
+ const results = new Array(items.length);
171
+ let nextIndex = 0;
172
+ const workers = new Array(limit).fill(null).map(async () => {
173
+ while (true) {
174
+ const current = nextIndex++;
175
+ if (current >= items.length)
176
+ return;
177
+ results[current] = await fn(items[current], current);
178
+ }
179
+ });
180
+ await Promise.all(workers);
181
+ return results;
182
+ }
183
+ async function writePromptToTempFile(agentName, prompt) {
184
+ const tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "spectral-subagent-"));
185
+ const safeName = agentName.replace(/[^\w.-]+/g, "_");
186
+ const filePath = path.join(tmpDir, `prompt-${safeName}.md`);
187
+ await withFileMutationQueue(filePath, async () => {
188
+ await fs.promises.writeFile(filePath, prompt, { encoding: "utf-8", mode: 0o600 });
189
+ });
190
+ return { dir: tmpDir, filePath };
191
+ }
192
+ /**
193
+ * Detect the correct binary for spawning subagent processes.
194
+ *
195
+ * Strategy:
196
+ * 1. If `process.argv[1]` points to an existing script (unlikely in bundled
197
+ * env but possible during dev with tsx), use that script with node.
198
+ * 2. If the executor is named `spectral` or `aexol`, use it directly (the
199
+ * wrapper ensures MCP + memory extensions are auto-loaded).
200
+ * 3. Otherwise fall back to `pi`.
201
+ */
202
+ function getPiInvocation(subagentArgs) {
203
+ const currentScript = process.argv[1];
204
+ // Case 1: running via tsx with an existing script file
205
+ if (currentScript && fs.existsSync(currentScript)) {
206
+ return { command: process.execPath, args: [currentScript, ...subagentArgs] };
207
+ }
208
+ // Case 2: check if this is the spectral/aexol wrapper binary
209
+ const execName = path.basename(process.execPath).toLowerCase();
210
+ if (execName === "spectral" || execName === "aexol") {
211
+ return { command: process.execPath, args: subagentArgs };
212
+ }
213
+ // Case 3: generic node — try spectral first, then pi
214
+ function hasBin(name) {
215
+ const PATH = process.env.PATH || "";
216
+ const envPathSep = process.platform === "win32" ? ";" : ":";
217
+ for (const dir of PATH.split(envPathSep)) {
218
+ const candidate = path.join(dir, name);
219
+ try {
220
+ if (fs.existsSync(candidate))
221
+ return true;
222
+ }
223
+ catch {
224
+ /* skip */
225
+ }
226
+ }
227
+ return false;
228
+ }
229
+ if (hasBin("spectral")) {
230
+ return { command: "spectral", args: subagentArgs };
231
+ }
232
+ if (hasBin("pi")) {
233
+ return { command: "pi", args: subagentArgs };
234
+ }
235
+ // Last resort: use node with pi from node_modules (matches pi's own fallback)
236
+ return { command: "pi", args: subagentArgs };
237
+ }
238
+ // ---------------------------------------------------------------------------
239
+ // Core — run a single subagent
240
+ // ---------------------------------------------------------------------------
241
+ async function runSingleAgent(defaultCwd, agents, agentName, task, cwd, step, signal, onUpdate, makeDetails) {
242
+ const agent = agents.find((a) => a.name === agentName);
243
+ if (!agent) {
244
+ const available = agents.map((a) => `"${a.name}"`).join(", ") || "none";
245
+ return {
246
+ agent: agentName,
247
+ agentSource: "unknown",
248
+ task,
249
+ exitCode: 1,
250
+ messages: [],
251
+ stderr: `Unknown agent: "${agentName}". Available agents: ${available}.`,
252
+ usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
253
+ step,
254
+ };
255
+ }
256
+ const args = ["--mode", "json", "-p", "--no-session"];
257
+ if (agent.model)
258
+ args.push("--model", agent.model);
259
+ if (agent.tools && agent.tools.length > 0)
260
+ args.push("--tools", agent.tools.join(","));
261
+ let tmpPromptDir = null;
262
+ let tmpPromptPath = null;
263
+ const currentResult = {
264
+ agent: agentName,
265
+ agentSource: agent.source,
266
+ task,
267
+ exitCode: 0,
268
+ messages: [],
269
+ stderr: "",
270
+ usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
271
+ model: agent.model,
272
+ step,
273
+ };
274
+ const emitUpdate = () => {
275
+ if (onUpdate) {
276
+ onUpdate({
277
+ content: [{ type: "text", text: getFinalOutput(currentResult.messages) || "(running...)" }],
278
+ details: makeDetails([currentResult]),
279
+ });
280
+ }
281
+ };
282
+ try {
283
+ if (agent.systemPrompt.trim()) {
284
+ const tmp = await writePromptToTempFile(agent.name, agent.systemPrompt);
285
+ tmpPromptDir = tmp.dir;
286
+ tmpPromptPath = tmp.filePath;
287
+ args.push("--append-system-prompt", tmpPromptPath);
288
+ }
289
+ args.push(`Task: ${task}`);
290
+ let wasAborted = false;
291
+ const exitCode = await new Promise((resolve) => {
292
+ const invocation = getPiInvocation(args);
293
+ const proc = spawn(invocation.command, invocation.args, {
294
+ cwd: cwd ?? defaultCwd,
295
+ shell: false,
296
+ stdio: ["ignore", "pipe", "pipe"],
297
+ });
298
+ let buffer = "";
299
+ const processLine = (line) => {
300
+ if (!line.trim())
301
+ return;
302
+ let event;
303
+ try {
304
+ event = JSON.parse(line);
305
+ }
306
+ catch {
307
+ return;
308
+ }
309
+ if (event.type === "message_end" && event.message) {
310
+ const msg = event.message;
311
+ currentResult.messages.push(msg);
312
+ if (msg.role === "assistant") {
313
+ currentResult.usage.turns++;
314
+ const usage = msg.usage;
315
+ if (usage) {
316
+ currentResult.usage.input += usage.input || 0;
317
+ currentResult.usage.output += usage.output || 0;
318
+ currentResult.usage.cacheRead += usage.cacheRead || 0;
319
+ currentResult.usage.cacheWrite += usage.cacheWrite || 0;
320
+ currentResult.usage.cost += usage.cost?.total || 0;
321
+ currentResult.usage.contextTokens = usage.totalTokens || 0;
322
+ }
323
+ if (!currentResult.model && msg.model)
324
+ currentResult.model = msg.model;
325
+ if (msg.stopReason)
326
+ currentResult.stopReason = msg.stopReason;
327
+ if (msg.errorMessage)
328
+ currentResult.errorMessage = msg.errorMessage;
329
+ }
330
+ emitUpdate();
331
+ }
332
+ if (event.type === "tool_result_end" && event.message) {
333
+ currentResult.messages.push(event.message);
334
+ emitUpdate();
335
+ }
336
+ };
337
+ proc.stdout.on("data", (data) => {
338
+ buffer += data.toString();
339
+ const lines = buffer.split("\n");
340
+ buffer = lines.pop() || "";
341
+ for (const line of lines)
342
+ processLine(line);
343
+ });
344
+ proc.stderr.on("data", (data) => {
345
+ currentResult.stderr += data.toString();
346
+ });
347
+ proc.on("close", (code) => {
348
+ if (buffer.trim())
349
+ processLine(buffer);
350
+ resolve(code ?? 0);
351
+ });
352
+ proc.on("error", () => {
353
+ resolve(1);
354
+ });
355
+ if (signal) {
356
+ const killProc = () => {
357
+ wasAborted = true;
358
+ proc.kill("SIGTERM");
359
+ setTimeout(() => {
360
+ if (!proc.killed)
361
+ proc.kill("SIGKILL");
362
+ }, 5000);
363
+ };
364
+ if (signal.aborted)
365
+ killProc();
366
+ else
367
+ signal.addEventListener("abort", killProc, { once: true });
368
+ }
369
+ });
370
+ currentResult.exitCode = exitCode;
371
+ if (wasAborted)
372
+ throw new Error("Subagent was aborted");
373
+ return currentResult;
374
+ }
375
+ finally {
376
+ if (tmpPromptPath)
377
+ try {
378
+ fs.unlinkSync(tmpPromptPath);
379
+ }
380
+ catch {
381
+ /* ignore */
382
+ }
383
+ if (tmpPromptDir)
384
+ try {
385
+ fs.rmdirSync(tmpPromptDir);
386
+ }
387
+ catch {
388
+ /* ignore */
389
+ }
390
+ }
391
+ }
392
+ // ---------------------------------------------------------------------------
393
+ // Tool parameter schemas
394
+ // ---------------------------------------------------------------------------
395
+ const TaskItem = Type.Object({
396
+ agent: Type.String({ description: "Name of the agent to invoke" }),
397
+ task: Type.String({ description: "Task to delegate to the agent" }),
398
+ cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
399
+ });
400
+ const ChainItem = Type.Object({
401
+ agent: Type.String({ description: "Name of the agent to invoke" }),
402
+ task: Type.String({
403
+ description: "Task with optional {previous} placeholder for prior output",
404
+ }),
405
+ cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
406
+ });
407
+ const AgentScopeSchema = StringEnum(["user", "project", "both"], {
408
+ description: 'Which agent directories to use. Default: "user". Use "both" to include project-local agents.',
409
+ default: "user",
410
+ });
411
+ const SubagentParams = Type.Object({
412
+ agent: Type.Optional(Type.String({ description: "Name of the agent to invoke (for single mode)" })),
413
+ task: Type.Optional(Type.String({ description: "Task to delegate (for single mode)" })),
414
+ tasks: Type.Optional(Type.Array(TaskItem, {
415
+ description: "Array of {agent, task} for parallel execution",
416
+ })),
417
+ chain: Type.Optional(Type.Array(ChainItem, {
418
+ description: "Array of {agent, task} for sequential execution",
419
+ })),
420
+ agentScope: Type.Optional(AgentScopeSchema),
421
+ confirmProjectAgents: Type.Optional(Type.Boolean({
422
+ description: "Prompt before running project-local agents. Default: true.",
423
+ default: true,
424
+ })),
425
+ cwd: Type.Optional(Type.String({
426
+ description: "Working directory for the agent process (single mode)",
427
+ })),
428
+ });
429
+ // ---------------------------------------------------------------------------
430
+ // Extension entry point
431
+ // ---------------------------------------------------------------------------
432
+ export default function subagentExtension(pi) {
433
+ pi.registerTool({
434
+ name: "subagent",
435
+ label: "Subagent",
436
+ description: [
437
+ "Delegate tasks to specialized subagents with isolated context.",
438
+ "Modes: single (agent + task), parallel (tasks array), chain (sequential with {previous} placeholder).",
439
+ 'Default agent scope is "user" (from ~/.pi/agent/agents).',
440
+ 'To enable project-local agents in .pi/agents, set agentScope: "both" (or "project").',
441
+ ].join(" "),
442
+ promptSnippet: "Run a subagent to investigate, plan, review, or implement code changes",
443
+ promptGuidelines: [
444
+ "Use the subagent tool to delegate complex investigations to specialized subagents instead of reading many files in the main conversation. Each subagent runs in an isolated context window.",
445
+ ],
446
+ parameters: SubagentParams,
447
+ async execute(_toolCallId, params, signal, onUpdate, ctx) {
448
+ const agentScope = params.agentScope ?? "user";
449
+ const discovery = discoverAgents(ctx.cwd, agentScope);
450
+ const agents = discovery.agents;
451
+ const confirmProjectAgents = params.confirmProjectAgents ?? true;
452
+ const hasChain = (params.chain?.length ?? 0) > 0;
453
+ const hasTasks = (params.tasks?.length ?? 0) > 0;
454
+ const hasSingle = Boolean(params.agent && params.task);
455
+ const modeCount = Number(hasChain) + Number(hasTasks) + Number(hasSingle);
456
+ const makeDetails = (mode) => (results) => ({
457
+ mode,
458
+ agentScope,
459
+ projectAgentsDir: discovery.projectAgentsDir,
460
+ results,
461
+ });
462
+ if (modeCount !== 1) {
463
+ const available = agents.map((a) => `${a.name} (${a.source})`).join(", ") || "none";
464
+ return {
465
+ content: [
466
+ {
467
+ type: "text",
468
+ text: `Invalid parameters. Provide exactly one mode.\nAvailable agents: ${available}`,
469
+ },
470
+ ],
471
+ details: makeDetails("single")([]),
472
+ };
473
+ }
474
+ // Security: if project-local agents are enabled, confirm with user
475
+ if ((agentScope === "project" || agentScope === "both") &&
476
+ confirmProjectAgents &&
477
+ ctx.hasUI) {
478
+ const requestedAgentNames = new Set();
479
+ if (params.chain)
480
+ for (const step of params.chain)
481
+ requestedAgentNames.add(step.agent);
482
+ if (params.tasks)
483
+ for (const t of params.tasks)
484
+ requestedAgentNames.add(t.agent);
485
+ if (params.agent)
486
+ requestedAgentNames.add(params.agent);
487
+ const projectAgentsRequested = Array.from(requestedAgentNames)
488
+ .map((name) => agents.find((a) => a.name === name))
489
+ .filter((a) => a?.source === "project");
490
+ if (projectAgentsRequested.length > 0) {
491
+ const names = projectAgentsRequested.map((a) => a.name).join(", ");
492
+ const dir = discovery.projectAgentsDir ?? "(unknown)";
493
+ const ok = await ctx.ui.confirm("Run project-local agents?", `Agents: ${names}\nSource: ${dir}\n\nProject agents are repo-controlled. Only continue for trusted repositories.`);
494
+ if (!ok)
495
+ return {
496
+ content: [
497
+ { type: "text", text: "Canceled: project-local agents not approved." },
498
+ ],
499
+ details: makeDetails(hasChain ? "chain" : hasTasks ? "parallel" : "single")([]),
500
+ };
501
+ }
502
+ }
503
+ // ── Chain mode ──────────────────────────────────────────────
504
+ if (params.chain && params.chain.length > 0) {
505
+ const results = [];
506
+ let previousOutput = "";
507
+ for (let i = 0; i < params.chain.length; i++) {
508
+ const step = params.chain[i];
509
+ const taskWithContext = step.task.replace(/\{previous\}/g, previousOutput);
510
+ const chainUpdate = onUpdate
511
+ ? (partial) => {
512
+ const currentResult = partial.details?.results[0];
513
+ if (currentResult) {
514
+ const allResults = [...results, currentResult];
515
+ onUpdate({
516
+ content: partial.content,
517
+ details: makeDetails("chain")(allResults),
518
+ });
519
+ }
520
+ }
521
+ : undefined;
522
+ const result = await runSingleAgent(ctx.cwd, agents, step.agent, taskWithContext, step.cwd, i + 1, signal, chainUpdate, makeDetails("chain"));
523
+ results.push(result);
524
+ const isError = result.exitCode !== 0 ||
525
+ result.stopReason === "error" ||
526
+ result.stopReason === "aborted";
527
+ if (isError) {
528
+ const errorMsg = result.errorMessage ||
529
+ result.stderr ||
530
+ getFinalOutput(result.messages) ||
531
+ "(no output)";
532
+ return {
533
+ content: [
534
+ {
535
+ type: "text",
536
+ text: `Chain stopped at step ${i + 1} (${step.agent}): ${errorMsg}`,
537
+ },
538
+ ],
539
+ details: makeDetails("chain")(results),
540
+ isError: true,
541
+ };
542
+ }
543
+ previousOutput = getFinalOutput(result.messages);
544
+ }
545
+ return {
546
+ content: [
547
+ {
548
+ type: "text",
549
+ text: getFinalOutput(results[results.length - 1].messages) || "(no output)",
550
+ },
551
+ ],
552
+ details: makeDetails("chain")(results),
553
+ };
554
+ }
555
+ // ── Parallel mode ───────────────────────────────────────────
556
+ if (params.tasks && params.tasks.length > 0) {
557
+ if (params.tasks.length > MAX_PARALLEL_TASKS)
558
+ return {
559
+ content: [
560
+ {
561
+ type: "text",
562
+ text: `Too many parallel tasks (${params.tasks.length}). Max is ${MAX_PARALLEL_TASKS}.`,
563
+ },
564
+ ],
565
+ details: makeDetails("parallel")([]),
566
+ };
567
+ const allResults = new Array(params.tasks.length);
568
+ for (let i = 0; i < params.tasks.length; i++) {
569
+ allResults[i] = {
570
+ agent: params.tasks[i].agent,
571
+ agentSource: "unknown",
572
+ task: params.tasks[i].task,
573
+ exitCode: -1,
574
+ messages: [],
575
+ stderr: "",
576
+ usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
577
+ };
578
+ }
579
+ const emitParallelUpdate = () => {
580
+ if (onUpdate) {
581
+ const running = allResults.filter((r) => r.exitCode === -1).length;
582
+ const done = allResults.filter((r) => r.exitCode !== -1).length;
583
+ onUpdate({
584
+ content: [
585
+ {
586
+ type: "text",
587
+ text: `Parallel: ${done}/${allResults.length} done, ${running} running...`,
588
+ },
589
+ ],
590
+ details: makeDetails("parallel")([...allResults]),
591
+ });
592
+ }
593
+ };
594
+ const results = await mapWithConcurrencyLimit(params.tasks, MAX_CONCURRENCY, async (t, index) => {
595
+ const result = await runSingleAgent(ctx.cwd, agents, t.agent, t.task, t.cwd, undefined, signal, (partial) => {
596
+ if (partial.details?.results[0]) {
597
+ allResults[index] = partial.details.results[0];
598
+ emitParallelUpdate();
599
+ }
600
+ }, makeDetails("parallel"));
601
+ allResults[index] = result;
602
+ emitParallelUpdate();
603
+ return result;
604
+ });
605
+ const successCount = results.filter((r) => r.exitCode === 0).length;
606
+ const summaries = results.map((r) => {
607
+ const output = getFinalOutput(r.messages);
608
+ const preview = output.slice(0, 100) + (output.length > 100 ? "..." : "");
609
+ return `[${r.agent}] ${r.exitCode === 0 ? "completed" : "failed"}: ${preview || "(no output)"}`;
610
+ });
611
+ return {
612
+ content: [
613
+ {
614
+ type: "text",
615
+ text: `Parallel: ${successCount}/${results.length} succeeded\n\n${summaries.join("\n\n")}`,
616
+ },
617
+ ],
618
+ details: makeDetails("parallel")(results),
619
+ };
620
+ }
621
+ // ── Single mode ─────────────────────────────────────────────
622
+ if (params.agent && params.task) {
623
+ const result = await runSingleAgent(ctx.cwd, agents, params.agent, params.task, params.cwd, undefined, signal, onUpdate, makeDetails("single"));
624
+ const isError = result.exitCode !== 0 ||
625
+ result.stopReason === "error" ||
626
+ result.stopReason === "aborted";
627
+ if (isError) {
628
+ const errorMsg = result.errorMessage ||
629
+ result.stderr ||
630
+ getFinalOutput(result.messages) ||
631
+ "(no output)";
632
+ return {
633
+ content: [
634
+ {
635
+ type: "text",
636
+ text: `Agent ${result.stopReason || "failed"}: ${errorMsg}`,
637
+ },
638
+ ],
639
+ details: makeDetails("single")([result]),
640
+ isError: true,
641
+ };
642
+ }
643
+ return {
644
+ content: [
645
+ {
646
+ type: "text",
647
+ text: getFinalOutput(result.messages) || "(no output)",
648
+ },
649
+ ],
650
+ details: makeDetails("single")([result]),
651
+ };
652
+ }
653
+ const available = agents.map((a) => `${a.name} (${a.source})`).join(", ") || "none";
654
+ return {
655
+ content: [
656
+ {
657
+ type: "text",
658
+ text: `Invalid parameters. Available agents: ${available}`,
659
+ },
660
+ ],
661
+ details: makeDetails("single")([]),
662
+ };
663
+ },
664
+ // ── Rendering ─────────────────────────────────────────────────
665
+ renderCall(args, theme, _context) {
666
+ const scope = args.agentScope ?? "user";
667
+ if (args.chain && args.chain.length > 0) {
668
+ let text = theme.fg("toolTitle", theme.bold("subagent ")) +
669
+ theme.fg("accent", `chain (${args.chain.length} steps)`) +
670
+ theme.fg("muted", ` [${scope}]`);
671
+ for (let i = 0; i < Math.min(args.chain.length, 3); i++) {
672
+ const step = args.chain[i];
673
+ const cleanTask = step.task.replace(/\{previous\}/g, "").trim();
674
+ const preview = cleanTask.length > 40 ? `${cleanTask.slice(0, 40)}...` : cleanTask;
675
+ text +=
676
+ "\n " +
677
+ theme.fg("muted", `${i + 1}.`) +
678
+ " " +
679
+ theme.fg("accent", step.agent) +
680
+ theme.fg("dim", ` ${preview}`);
681
+ }
682
+ if (args.chain.length > 3)
683
+ text += `\n ${theme.fg("muted", `... +${args.chain.length - 3} more`)}`;
684
+ return new Text(text, 0, 0);
685
+ }
686
+ if (args.tasks && args.tasks.length > 0) {
687
+ let text = theme.fg("toolTitle", theme.bold("subagent ")) +
688
+ theme.fg("accent", `parallel (${args.tasks.length} tasks)`) +
689
+ theme.fg("muted", ` [${scope}]`);
690
+ for (const t of args.tasks.slice(0, 3)) {
691
+ const preview = t.task.length > 40 ? `${t.task.slice(0, 40)}...` : t.task;
692
+ text += `\n ${theme.fg("accent", t.agent)}${theme.fg("dim", ` ${preview}`)}`;
693
+ }
694
+ if (args.tasks.length > 3)
695
+ text += `\n ${theme.fg("muted", `... +${args.tasks.length - 3} more`)}`;
696
+ return new Text(text, 0, 0);
697
+ }
698
+ const agentName = args.agent || "...";
699
+ const preview = args.task
700
+ ? args.task.length > 60
701
+ ? `${args.task.slice(0, 60)}...`
702
+ : args.task
703
+ : "...";
704
+ let text = theme.fg("toolTitle", theme.bold("subagent ")) +
705
+ theme.fg("accent", agentName) +
706
+ theme.fg("muted", ` [${scope}]`);
707
+ text += `\n ${theme.fg("dim", preview)}`;
708
+ return new Text(text, 0, 0);
709
+ },
710
+ renderResult(result, { expanded }, theme, _context) {
711
+ const details = result.details;
712
+ if (!details || details.results.length === 0) {
713
+ const text = result.content[0];
714
+ return new Text(text?.type === "text" ? text.text : "(no output)", 0, 0);
715
+ }
716
+ const mdTheme = getMarkdownTheme();
717
+ const renderDisplayItems = (items, limit) => {
718
+ const toShow = limit ? items.slice(-limit) : items;
719
+ const skipped = limit && items.length > limit ? items.length - limit : 0;
720
+ let text = "";
721
+ if (skipped > 0)
722
+ text += theme.fg("muted", `... ${skipped} earlier items\n`);
723
+ for (const item of toShow) {
724
+ if (item.type === "text") {
725
+ const preview = expanded
726
+ ? item.text
727
+ : item.text.split("\n").slice(0, 3).join("\n");
728
+ text += `${theme.fg("toolOutput", preview)}\n`;
729
+ }
730
+ else {
731
+ text += `${theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme))}\n`;
732
+ }
733
+ }
734
+ return text.trimEnd();
735
+ };
736
+ if (details.mode === "single" && details.results.length === 1) {
737
+ const r = details.results[0];
738
+ const isError = r.exitCode !== 0 ||
739
+ r.stopReason === "error" ||
740
+ r.stopReason === "aborted";
741
+ const icon = isError
742
+ ? theme.fg("error", "✗")
743
+ : theme.fg("success", "✓");
744
+ const displayItems = getDisplayItems(r.messages);
745
+ const finalOutput = getFinalOutput(r.messages);
746
+ if (expanded) {
747
+ const container = new Container();
748
+ let header = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
749
+ if (isError && r.stopReason)
750
+ header += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
751
+ container.addChild(new Text(header, 0, 0));
752
+ if (isError && r.errorMessage)
753
+ container.addChild(new Text(theme.fg("error", `Error: ${r.errorMessage}`), 0, 0));
754
+ container.addChild(new Spacer(1));
755
+ container.addChild(new Text(theme.fg("muted", "─── Task ───"), 0, 0));
756
+ container.addChild(new Text(theme.fg("dim", r.task), 0, 0));
757
+ container.addChild(new Spacer(1));
758
+ container.addChild(new Text(theme.fg("muted", "─── Output ───"), 0, 0));
759
+ if (displayItems.length === 0 && !finalOutput) {
760
+ container.addChild(new Text(theme.fg("muted", "(no output)"), 0, 0));
761
+ }
762
+ else {
763
+ for (const item of displayItems) {
764
+ if (item.type === "toolCall")
765
+ container.addChild(new Text(theme.fg("muted", "→ ") +
766
+ formatToolCall(item.name, item.args, theme.fg.bind(theme)), 0, 0));
767
+ }
768
+ if (finalOutput) {
769
+ container.addChild(new Spacer(1));
770
+ container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
771
+ }
772
+ }
773
+ const usageStr = formatUsageStats(r.usage, r.model);
774
+ if (usageStr) {
775
+ container.addChild(new Spacer(1));
776
+ container.addChild(new Text(theme.fg("dim", usageStr), 0, 0));
777
+ }
778
+ return container;
779
+ }
780
+ let text = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
781
+ if (isError && r.stopReason)
782
+ text += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
783
+ if (isError && r.errorMessage)
784
+ text += `\n${theme.fg("error", `Error: ${r.errorMessage}`)}`;
785
+ else if (displayItems.length === 0)
786
+ text += `\n${theme.fg("muted", "(no output)")}`;
787
+ else {
788
+ text += `\n${renderDisplayItems(displayItems, COLLAPSED_ITEM_COUNT)}`;
789
+ if (displayItems.length > COLLAPSED_ITEM_COUNT)
790
+ text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
791
+ }
792
+ const usageStr = formatUsageStats(r.usage, r.model);
793
+ if (usageStr)
794
+ text += `\n${theme.fg("dim", usageStr)}`;
795
+ return new Text(text, 0, 0);
796
+ }
797
+ const aggregateUsage = (results) => {
798
+ const total = {
799
+ input: 0,
800
+ output: 0,
801
+ cacheRead: 0,
802
+ cacheWrite: 0,
803
+ cost: 0,
804
+ turns: 0,
805
+ };
806
+ for (const r of results) {
807
+ total.input += r.usage.input;
808
+ total.output += r.usage.output;
809
+ total.cacheRead += r.usage.cacheRead;
810
+ total.cacheWrite += r.usage.cacheWrite;
811
+ total.cost += r.usage.cost;
812
+ total.turns += r.usage.turns;
813
+ }
814
+ return total;
815
+ };
816
+ if (details.mode === "chain") {
817
+ const successCount = details.results.filter((r) => r.exitCode === 0).length;
818
+ const icon = successCount === details.results.length
819
+ ? theme.fg("success", "✓")
820
+ : theme.fg("error", "✗");
821
+ if (expanded) {
822
+ const container = new Container();
823
+ container.addChild(new Text(icon +
824
+ " " +
825
+ theme.fg("toolTitle", theme.bold("chain ")) +
826
+ theme.fg("accent", `${successCount}/${details.results.length} steps`), 0, 0));
827
+ for (const r of details.results) {
828
+ const rIcon = r.exitCode === 0
829
+ ? theme.fg("success", "✓")
830
+ : theme.fg("error", "✗");
831
+ const displayItems = getDisplayItems(r.messages);
832
+ const finalOutput = getFinalOutput(r.messages);
833
+ container.addChild(new Spacer(1));
834
+ container.addChild(new Text(`${theme.fg("muted", `─── Step ${r.step}: `) + theme.fg("accent", r.agent)} ${rIcon}`, 0, 0));
835
+ container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0));
836
+ for (const item of displayItems) {
837
+ if (item.type === "toolCall") {
838
+ container.addChild(new Text(theme.fg("muted", "→ ") +
839
+ formatToolCall(item.name, item.args, theme.fg.bind(theme)), 0, 0));
840
+ }
841
+ }
842
+ if (finalOutput) {
843
+ container.addChild(new Spacer(1));
844
+ container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
845
+ }
846
+ const stepUsage = formatUsageStats(r.usage, r.model);
847
+ if (stepUsage)
848
+ container.addChild(new Text(theme.fg("dim", stepUsage), 0, 0));
849
+ }
850
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
851
+ if (usageStr) {
852
+ container.addChild(new Spacer(1));
853
+ container.addChild(new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0));
854
+ }
855
+ return container;
856
+ }
857
+ let text = icon +
858
+ " " +
859
+ theme.fg("toolTitle", theme.bold("chain ")) +
860
+ theme.fg("accent", `${successCount}/${details.results.length} steps`);
861
+ for (const r of details.results) {
862
+ const rIcon = r.exitCode === 0
863
+ ? theme.fg("success", "✓")
864
+ : theme.fg("error", "✗");
865
+ const displayItems = getDisplayItems(r.messages);
866
+ text += `\n\n${theme.fg("muted", `─── Step ${r.step}: `)}${theme.fg("accent", r.agent)} ${rIcon}`;
867
+ if (displayItems.length === 0)
868
+ text += `\n${theme.fg("muted", "(no output)")}`;
869
+ else
870
+ text += `\n${renderDisplayItems(displayItems, 5)}`;
871
+ }
872
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
873
+ if (usageStr)
874
+ text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
875
+ text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
876
+ return new Text(text, 0, 0);
877
+ }
878
+ if (details.mode === "parallel") {
879
+ const running = details.results.filter((r) => r.exitCode === -1).length;
880
+ const successCount = details.results.filter((r) => r.exitCode === 0).length;
881
+ const failCount = details.results.filter((r) => r.exitCode > 0).length;
882
+ const isRunning = running > 0;
883
+ const icon = isRunning
884
+ ? theme.fg("warning", "⏳")
885
+ : failCount > 0
886
+ ? theme.fg("warning", "◐")
887
+ : theme.fg("success", "✓");
888
+ const status = isRunning
889
+ ? `${successCount + failCount}/${details.results.length} done, ${running} running`
890
+ : `${successCount}/${details.results.length} tasks`;
891
+ if (expanded && !isRunning) {
892
+ const container = new Container();
893
+ container.addChild(new Text(`${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`, 0, 0));
894
+ for (const r of details.results) {
895
+ const rIcon = r.exitCode === 0
896
+ ? theme.fg("success", "✓")
897
+ : theme.fg("error", "✗");
898
+ const displayItems = getDisplayItems(r.messages);
899
+ const finalOutput = getFinalOutput(r.messages);
900
+ container.addChild(new Spacer(1));
901
+ container.addChild(new Text(`${theme.fg("muted", "─── ") + theme.fg("accent", r.agent)} ${rIcon}`, 0, 0));
902
+ container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0));
903
+ for (const item of displayItems) {
904
+ if (item.type === "toolCall") {
905
+ container.addChild(new Text(theme.fg("muted", "→ ") +
906
+ formatToolCall(item.name, item.args, theme.fg.bind(theme)), 0, 0));
907
+ }
908
+ }
909
+ if (finalOutput) {
910
+ container.addChild(new Spacer(1));
911
+ container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
912
+ }
913
+ const taskUsage = formatUsageStats(r.usage, r.model);
914
+ if (taskUsage)
915
+ container.addChild(new Text(theme.fg("dim", taskUsage), 0, 0));
916
+ }
917
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
918
+ if (usageStr) {
919
+ container.addChild(new Spacer(1));
920
+ container.addChild(new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0));
921
+ }
922
+ return container;
923
+ }
924
+ let text = `${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`;
925
+ for (const r of details.results) {
926
+ const rIcon = r.exitCode === -1
927
+ ? theme.fg("warning", "⏳")
928
+ : r.exitCode === 0
929
+ ? theme.fg("success", "✓")
930
+ : theme.fg("error", "✗");
931
+ const displayItems = getDisplayItems(r.messages);
932
+ text += `\n\n${theme.fg("muted", "─── ")}${theme.fg("accent", r.agent)} ${rIcon}`;
933
+ if (displayItems.length === 0)
934
+ text += `\n${theme.fg("muted", r.exitCode === -1 ? "(running...)" : "(no output)")}`;
935
+ else
936
+ text += `\n${renderDisplayItems(displayItems, 5)}`;
937
+ }
938
+ if (!isRunning) {
939
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
940
+ if (usageStr)
941
+ text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
942
+ }
943
+ if (!expanded)
944
+ text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
945
+ return new Text(text, 0, 0);
946
+ }
947
+ const text = result.content[0];
948
+ return new Text(text?.type === "text" ? text.text : "(no output)", 0, 0);
949
+ },
950
+ });
951
+ }