@pi-agents/orchid 0.1.0-beta.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.
Files changed (163) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/LICENSE +21 -0
  3. package/README.md +246 -0
  4. package/agents/AGENTS-MANIFEST.md +42 -0
  5. package/agents/brain.md +42 -0
  6. package/agents/context-builder.md +46 -0
  7. package/agents/delegate.md +12 -0
  8. package/agents/dev-1.md +42 -0
  9. package/agents/oracle.md +73 -0
  10. package/agents/planner.md +55 -0
  11. package/agents/researcher.md +52 -0
  12. package/agents/reviewer.md +79 -0
  13. package/agents/scout.md +50 -0
  14. package/agents/tester.md +45 -0
  15. package/agents/worker.md +55 -0
  16. package/extensions/ralph.ts +1 -0
  17. package/extensions/reviewer-extension.ts +125 -0
  18. package/extensions/task-orchestrator.ts +28 -0
  19. package/package.json +63 -0
  20. package/prompts/gather-context-and-clarify.md +13 -0
  21. package/prompts/parallel-cleanup.md +59 -0
  22. package/prompts/parallel-context-build.md +53 -0
  23. package/prompts/parallel-handoff-plan.md +59 -0
  24. package/prompts/parallel-research.md +50 -0
  25. package/prompts/parallel-review.md +54 -0
  26. package/prompts/review-loop.md +41 -0
  27. package/skills/orchid/SKILL.md +214 -0
  28. package/skills/orchid/orchid-cleanup/SKILL.md +122 -0
  29. package/skills/orchid/orchid-converge/SKILL.md +124 -0
  30. package/skills/orchid/orchid-decompose/SKILL.md +201 -0
  31. package/skills/orchid/orchid-doctor/SKILL.md +162 -0
  32. package/skills/orchid/orchid-investigate/SKILL.md +102 -0
  33. package/skills/orchid/orchid-launch/SKILL.md +147 -0
  34. package/skills/ralph/SKILL.md +73 -0
  35. package/skills/subagents/pi-subagents/SKILL.md +813 -0
  36. package/src/index.ts +7 -0
  37. package/src/orchestrator/abort.ts +534 -0
  38. package/src/orchestrator/agent-bridge-extension.ts +1020 -0
  39. package/src/orchestrator/agent-host.ts +954 -0
  40. package/src/orchestrator/cleanup.ts +776 -0
  41. package/src/orchestrator/config-loader.ts +1412 -0
  42. package/src/orchestrator/config-schema.ts +690 -0
  43. package/src/orchestrator/config.ts +81 -0
  44. package/src/orchestrator/context-window.ts +66 -0
  45. package/src/orchestrator/diagnostic-reports.ts +475 -0
  46. package/src/orchestrator/diagnostics.ts +394 -0
  47. package/src/orchestrator/discovery.ts +1833 -0
  48. package/src/orchestrator/engine-worker.ts +415 -0
  49. package/src/orchestrator/engine.ts +5940 -0
  50. package/src/orchestrator/execution.ts +3104 -0
  51. package/src/orchestrator/extension.ts +5934 -0
  52. package/src/orchestrator/formatting.ts +785 -0
  53. package/src/orchestrator/git.ts +88 -0
  54. package/src/orchestrator/index.ts +28 -0
  55. package/src/orchestrator/lane-runner.ts +1787 -0
  56. package/src/orchestrator/mailbox.ts +780 -0
  57. package/src/orchestrator/merge.ts +3414 -0
  58. package/src/orchestrator/messages.ts +1062 -0
  59. package/src/orchestrator/migrations.ts +278 -0
  60. package/src/orchestrator/naming.ts +117 -0
  61. package/src/orchestrator/path-resolver.ts +275 -0
  62. package/src/orchestrator/persistence.ts +2625 -0
  63. package/src/orchestrator/process-registry.ts +452 -0
  64. package/src/orchestrator/quality-gate.ts +1085 -0
  65. package/src/orchestrator/resume.ts +3488 -0
  66. package/src/orchestrator/sessions.ts +57 -0
  67. package/src/orchestrator/settings-loader.ts +136 -0
  68. package/src/orchestrator/settings-tui.ts +2208 -0
  69. package/src/orchestrator/sidecar-telemetry.ts +267 -0
  70. package/src/orchestrator/supervisor.ts +4548 -0
  71. package/src/orchestrator/task-executor-core.ts +675 -0
  72. package/src/orchestrator/tmux-compat.ts +37 -0
  73. package/src/orchestrator/tool-allowlist-constants.ts +37 -0
  74. package/src/orchestrator/types.ts +4465 -0
  75. package/src/orchestrator/verification.ts +547 -0
  76. package/src/orchestrator/waves.ts +1564 -0
  77. package/src/orchestrator/workspace.ts +707 -0
  78. package/src/orchestrator/worktree.ts +2725 -0
  79. package/src/ralph/index.ts +825 -0
  80. package/src/subagents/agents/agent-management.ts +648 -0
  81. package/src/subagents/agents/agent-scope.ts +6 -0
  82. package/src/subagents/agents/agent-selection.ts +23 -0
  83. package/src/subagents/agents/agent-serializer.ts +86 -0
  84. package/src/subagents/agents/agents.ts +832 -0
  85. package/src/subagents/agents/chain-serializer.ts +137 -0
  86. package/src/subagents/agents/frontmatter.ts +29 -0
  87. package/src/subagents/agents/identity.ts +30 -0
  88. package/src/subagents/agents/skills.ts +632 -0
  89. package/src/subagents/extension/config.ts +16 -0
  90. package/src/subagents/extension/control-notices.ts +92 -0
  91. package/src/subagents/extension/doctor.ts +199 -0
  92. package/src/subagents/extension/fanout-child.ts +170 -0
  93. package/src/subagents/extension/index.ts +573 -0
  94. package/src/subagents/extension/schemas.ts +168 -0
  95. package/src/subagents/intercom/intercom-bridge.ts +379 -0
  96. package/src/subagents/intercom/result-intercom.ts +377 -0
  97. package/src/subagents/runs/background/async-execution.ts +712 -0
  98. package/src/subagents/runs/background/async-job-tracker.ts +310 -0
  99. package/src/subagents/runs/background/async-resume.ts +345 -0
  100. package/src/subagents/runs/background/async-status.ts +325 -0
  101. package/src/subagents/runs/background/completion-dedupe.ts +63 -0
  102. package/src/subagents/runs/background/notify.ts +108 -0
  103. package/src/subagents/runs/background/parallel-groups.ts +45 -0
  104. package/src/subagents/runs/background/result-watcher.ts +307 -0
  105. package/src/subagents/runs/background/run-id-resolver.ts +83 -0
  106. package/src/subagents/runs/background/run-status.ts +269 -0
  107. package/src/subagents/runs/background/stale-run-reconciler.ts +336 -0
  108. package/src/subagents/runs/background/subagent-runner.ts +1808 -0
  109. package/src/subagents/runs/background/top-level-async.ts +13 -0
  110. package/src/subagents/runs/foreground/chain-clarify.ts +1333 -0
  111. package/src/subagents/runs/foreground/chain-execution.ts +938 -0
  112. package/src/subagents/runs/foreground/execution.ts +918 -0
  113. package/src/subagents/runs/foreground/subagent-executor.ts +2527 -0
  114. package/src/subagents/runs/shared/completion-guard.ts +147 -0
  115. package/src/subagents/runs/shared/long-running-guard.ts +175 -0
  116. package/src/subagents/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
  117. package/src/subagents/runs/shared/model-fallback.ts +103 -0
  118. package/src/subagents/runs/shared/nested-events.ts +819 -0
  119. package/src/subagents/runs/shared/nested-path.ts +52 -0
  120. package/src/subagents/runs/shared/nested-render.ts +115 -0
  121. package/src/subagents/runs/shared/parallel-utils.ts +109 -0
  122. package/src/subagents/runs/shared/pi-args.ts +220 -0
  123. package/src/subagents/runs/shared/pi-spawn.ts +115 -0
  124. package/src/subagents/runs/shared/run-history.ts +60 -0
  125. package/src/subagents/runs/shared/single-output.ts +164 -0
  126. package/src/subagents/runs/shared/subagent-control.ts +226 -0
  127. package/src/subagents/runs/shared/subagent-prompt-runtime.ts +170 -0
  128. package/src/subagents/runs/shared/worktree.ts +577 -0
  129. package/src/subagents/shared/artifacts.ts +98 -0
  130. package/src/subagents/shared/atomic-json.ts +16 -0
  131. package/src/subagents/shared/file-coalescer.ts +40 -0
  132. package/src/subagents/shared/fork-context.ts +76 -0
  133. package/src/subagents/shared/formatters.ts +133 -0
  134. package/src/subagents/shared/jsonl-writer.ts +81 -0
  135. package/src/subagents/shared/model-info.ts +78 -0
  136. package/src/subagents/shared/post-exit-stdio-guard.ts +85 -0
  137. package/src/subagents/shared/session-identity.ts +10 -0
  138. package/src/subagents/shared/session-tokens.ts +44 -0
  139. package/src/subagents/shared/settings.ts +397 -0
  140. package/src/subagents/shared/status-format.ts +49 -0
  141. package/src/subagents/shared/types.ts +822 -0
  142. package/src/subagents/shared/utils.ts +450 -0
  143. package/src/subagents/slash/prompt-template-bridge.ts +397 -0
  144. package/src/subagents/slash/slash-bridge.ts +174 -0
  145. package/src/subagents/slash/slash-commands.ts +528 -0
  146. package/src/subagents/slash/slash-live-state.ts +292 -0
  147. package/src/subagents/tui/render-helpers.ts +80 -0
  148. package/src/subagents/tui/render.ts +1358 -0
  149. package/templates/agents/local/supervisor.md +33 -0
  150. package/templates/agents/local/task-merger.md +27 -0
  151. package/templates/agents/local/task-reviewer.md +30 -0
  152. package/templates/agents/local/task-worker.md +34 -0
  153. package/templates/agents/supervisor-routing.md +92 -0
  154. package/templates/agents/supervisor.md +229 -0
  155. package/templates/agents/task-merger.md +214 -0
  156. package/templates/agents/task-reviewer.md +260 -0
  157. package/templates/agents/task-worker-segment.md +44 -0
  158. package/templates/agents/task-worker.md +557 -0
  159. package/templates/tasks/CONTEXT.md +30 -0
  160. package/templates/tasks/EXAMPLE-001-hello-world/PROMPT.md +98 -0
  161. package/templates/tasks/EXAMPLE-001-hello-world/STATUS.md +73 -0
  162. package/templates/tasks/EXAMPLE-002-parallel-smoke/PROMPT.md +97 -0
  163. package/templates/tasks/EXAMPLE-002-parallel-smoke/STATUS.md +73 -0
@@ -0,0 +1,1358 @@
1
+ /**
2
+ * Rendering functions for subagent results
3
+ */
4
+
5
+ import * as path from "node:path";
6
+ import type { AgentToolResult } from "@earendil-works/pi-agent-core";
7
+ import { getMarkdownTheme, type ExtensionContext } from "@earendil-works/pi-coding-agent";
8
+ import { Container, Markdown, Spacer, Text, visibleWidth, type Component } from "@earendil-works/pi-tui";
9
+ import {
10
+ type AgentProgress,
11
+ type AsyncJobState,
12
+ type AsyncJobStep,
13
+ type AsyncParallelGroupStatus,
14
+ type Details,
15
+ type NestedRunSummary,
16
+ type NestedStepSummary,
17
+ MAX_WIDGET_JOBS,
18
+ WIDGET_KEY,
19
+ } from "../shared/types.ts";
20
+ import { formatTokens, formatUsage, formatDuration, formatModelThinking, formatToolCall, shortenPath } from "../shared/formatters.ts";
21
+ import { getDisplayItems, getSingleResultOutput } from "../shared/utils.ts";
22
+ import { flatToLogicalStepIndex } from "../runs/background/parallel-groups.ts";
23
+ import { formatNestedAggregate } from "../runs/shared/nested-render.ts";
24
+ import { aggregateStepStatus, formatActivityLabel, formatAgentRunningLabel, formatParallelOutcome } from "../shared/status-format.ts";
25
+
26
+ type Theme = ExtensionContext["ui"]["theme"];
27
+
28
+ function getTermWidth(): number {
29
+ return process.stdout.columns || 120;
30
+ }
31
+
32
+ const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
33
+
34
+ /**
35
+ * Truncate a line to maxWidth, preserving ANSI styling through the ellipsis.
36
+ *
37
+ * pi-tui's truncateToWidth adds \x1b[0m before ellipsis which resets all styling,
38
+ * causing background color bleed in the TUI. This implementation tracks active
39
+ * ANSI styles and re-applies them before the ellipsis.
40
+ *
41
+ * Uses Intl.Segmenter for proper Unicode/emoji handling (not char-by-char).
42
+ */
43
+ function truncLine(text: string, maxWidth: number): string {
44
+ if (visibleWidth(text) <= maxWidth) return text;
45
+
46
+ const targetWidth = maxWidth - 1;
47
+ let result = "";
48
+ let currentWidth = 0;
49
+ let activeStyles: string[] = [];
50
+ let i = 0;
51
+
52
+ while (i < text.length) {
53
+ const ansiMatch = text.slice(i).match(/^\x1b\[[0-9;]*m/);
54
+ if (ansiMatch) {
55
+ const code = ansiMatch[0];
56
+ result += code;
57
+
58
+ if (code === "\x1b[0m" || code === "\x1b[m") {
59
+ activeStyles = [];
60
+ } else {
61
+ activeStyles.push(code);
62
+ }
63
+ i += code.length;
64
+ continue;
65
+ }
66
+
67
+ let end = i;
68
+ while (end < text.length && !text.slice(end).match(/^\x1b\[[0-9;]*m/)) {
69
+ end++;
70
+ }
71
+
72
+ const textPortion = text.slice(i, end);
73
+ for (const seg of segmenter.segment(textPortion)) {
74
+ const grapheme = seg.segment;
75
+ const graphemeWidth = visibleWidth(grapheme);
76
+
77
+ if (currentWidth + graphemeWidth > targetWidth) {
78
+ return result + activeStyles.join("") + "…";
79
+ }
80
+
81
+ result += grapheme;
82
+ currentWidth += graphemeWidth;
83
+ }
84
+ i = end;
85
+ }
86
+
87
+ return result + activeStyles.join("") + "…";
88
+ }
89
+
90
+ const RUNNING_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
91
+ const STATIC_RUNNING_GLYPH = "●";
92
+
93
+ type ProgressSeedSource = Partial<Pick<AgentProgress, "index" | "toolCount" | "tokens" | "durationMs" | "lastActivityAt" | "currentToolStartedAt" | "turnCount">>;
94
+
95
+ function runningSeed(...values: Array<number | undefined>): number | undefined {
96
+ let seed: number | undefined;
97
+ for (const value of values) {
98
+ if (value === undefined || !Number.isFinite(value)) continue;
99
+ seed = (seed ?? 0) + Math.trunc(value);
100
+ }
101
+ return seed;
102
+ }
103
+
104
+ function runningGlyph(seed?: number): string {
105
+ if (seed === undefined) return STATIC_RUNNING_GLYPH;
106
+ return RUNNING_FRAMES[Math.abs(seed) % RUNNING_FRAMES.length]!;
107
+ }
108
+
109
+ function progressRunningSeed(progress: ProgressSeedSource | undefined): number | undefined {
110
+ if (!progress) return undefined;
111
+ return runningSeed(
112
+ progress.index,
113
+ progress.toolCount,
114
+ progress.tokens,
115
+ progress.durationMs,
116
+ progress.lastActivityAt,
117
+ progress.currentToolStartedAt,
118
+ progress.turnCount,
119
+ );
120
+ }
121
+
122
+ interface LegacyResultAnimationContext {
123
+ state: { subagentResultAnimationTimer?: ReturnType<typeof setInterval> };
124
+ }
125
+
126
+ export function clearLegacyResultAnimationTimer(context: LegacyResultAnimationContext): void {
127
+ const timer = context.state.subagentResultAnimationTimer;
128
+ if (!timer) return;
129
+ clearInterval(timer);
130
+ context.state.subagentResultAnimationTimer = undefined;
131
+ }
132
+
133
+ function extractOutputTarget(task: string): string | undefined {
134
+ const writeToMatch = task.match(/\[Write to:\s*([^\]\n]+)\]/i);
135
+ if (writeToMatch?.[1]?.trim()) return writeToMatch[1].trim();
136
+ const findingsMatch = task.match(/Write your findings to:\s*(\S+)/i);
137
+ if (findingsMatch?.[1]?.trim()) return findingsMatch[1].trim();
138
+ const outputMatch = task.match(/[Oo]utput(?:\s+to)?\s*:\s*(\S+)/i);
139
+ if (outputMatch?.[1]?.trim()) return outputMatch[1].trim();
140
+ return undefined;
141
+ }
142
+
143
+ function hasEmptyTextOutputWithoutOutputTarget(task: string, output: string): boolean {
144
+ if (output.trim()) return false;
145
+ return !extractOutputTarget(task);
146
+ }
147
+
148
+ function getToolCallLines(
149
+ result: Pick<Details["results"][number], "messages" | "toolCalls">,
150
+ expanded: boolean,
151
+ ): string[] {
152
+ if (result.messages) {
153
+ return getDisplayItems(result.messages)
154
+ .filter((item): item is { type: "tool"; name: string; args: Record<string, unknown> } => item.type === "tool")
155
+ .map((item) => formatToolCall(item.name, item.args, expanded));
156
+ }
157
+ return result.toolCalls?.map((toolCall) => expanded ? toolCall.expandedText : toolCall.text) ?? [];
158
+ }
159
+
160
+
161
+ function snapshotNowForProgress(progress: Pick<AgentProgress, "currentToolStartedAt" | "durationMs" | "lastActivityAt">): number | undefined {
162
+ if (progress.currentToolStartedAt !== undefined && progress.durationMs !== undefined) return progress.currentToolStartedAt + progress.durationMs;
163
+ return progress.lastActivityAt;
164
+ }
165
+
166
+ function formatCurrentToolLine(
167
+ progress: Pick<AgentProgress, "currentTool" | "currentToolArgs" | "currentToolStartedAt">,
168
+ availableWidth: number,
169
+ expanded: boolean,
170
+ snapshotNow?: number,
171
+ ): string | undefined {
172
+ if (!progress.currentTool) return undefined;
173
+ const maxToolArgsLen = Math.max(50, availableWidth - 20);
174
+ const toolArgsPreview = progress.currentToolArgs
175
+ ? (expanded || progress.currentToolArgs.length <= maxToolArgsLen
176
+ ? progress.currentToolArgs
177
+ : `${progress.currentToolArgs.slice(0, maxToolArgsLen)}...`)
178
+ : "";
179
+ const durationSuffix = progress.currentToolStartedAt !== undefined && snapshotNow !== undefined
180
+ ? ` | ${formatDuration(Math.max(0, snapshotNow - progress.currentToolStartedAt))}`
181
+ : "";
182
+ return toolArgsPreview
183
+ ? `${progress.currentTool}: ${toolArgsPreview}${durationSuffix}`
184
+ : `${progress.currentTool}${durationSuffix}`;
185
+ }
186
+
187
+ function buildLiveStatusLine(progress: Pick<AgentProgress, "activityState" | "lastActivityAt">, snapshotNow?: number): string | undefined {
188
+ if (progress.lastActivityAt !== undefined && snapshotNow !== undefined) return formatActivityLabel(progress.lastActivityAt, progress.activityState, snapshotNow);
189
+ if (progress.activityState === "needs_attention") return "needs attention";
190
+ if (progress.activityState === "active_long_running") return "active but long-running";
191
+ if (progress.lastActivityAt !== undefined) return "active";
192
+ return undefined;
193
+ }
194
+
195
+ function themeBold(theme: Theme, text: string): string {
196
+ return ((theme as { bold?: (value: string) => string }).bold?.(text)) ?? text;
197
+ }
198
+
199
+ function statJoin(theme: Theme, parts: string[]): string {
200
+ return parts.filter(Boolean).map((part) => theme.fg("dim", part)).join(` ${theme.fg("dim", "·")} `);
201
+ }
202
+
203
+ function formatTokenStat(tokens: number): string {
204
+ return `${formatTokens(tokens)} token`;
205
+ }
206
+
207
+ function formatToolUseStat(count: number): string {
208
+ return `${count} tool use${count === 1 ? "" : "s"}`;
209
+ }
210
+
211
+ function formatProgressStats(theme: Theme, progress: Pick<AgentProgress, "toolCount" | "tokens" | "durationMs"> | undefined, includeDuration = true): string {
212
+ if (!progress) return "";
213
+ const parts: string[] = [];
214
+ if (progress.toolCount > 0) parts.push(formatToolUseStat(progress.toolCount));
215
+ if (progress.tokens > 0) parts.push(formatTokenStat(progress.tokens));
216
+ if (includeDuration && progress.durationMs > 0) parts.push(formatDuration(progress.durationMs));
217
+ return statJoin(theme, parts);
218
+ }
219
+
220
+ function firstOutputLine(text: string): string {
221
+ return text.split("\n").find((line) => line.trim())?.trim() ?? "";
222
+ }
223
+
224
+ function resultStatusLine(result: Details["results"][number], output: string): string {
225
+ if (result.detached) return result.detachedReason ? `Detached: ${result.detachedReason}` : "Detached";
226
+ if (result.interrupted) return "Paused";
227
+ if (result.exitCode !== 0) return `Error: ${result.error ?? (firstOutputLine(output) || `exit ${result.exitCode}`)}`;
228
+ if (hasEmptyTextOutputWithoutOutputTarget(result.task, output)) return "Done (no text output)";
229
+ return "Done";
230
+ }
231
+
232
+ function resultGlyph(result: Details["results"][number], output: string, theme: Theme, running = result.progress?.status === "running", seed = progressRunningSeed(result.progress ?? result.progressSummary)): string {
233
+ if (running) return theme.fg("accent", runningGlyph(seed));
234
+ if (result.detached) return theme.fg("warning", "■");
235
+ if (result.interrupted) return theme.fg("warning", "■");
236
+ if (result.exitCode !== 0) return theme.fg("error", "✗");
237
+ if (hasEmptyTextOutputWithoutOutputTarget(result.task, output)) return theme.fg("warning", "✓");
238
+ return theme.fg("success", "✓");
239
+ }
240
+
241
+ function compactCurrentActivity(progress: AgentProgress): string {
242
+ const snapshotNow = snapshotNowForProgress(progress);
243
+ return formatCurrentToolLine(progress, getTermWidth() - 4, false, snapshotNow) ?? buildLiveStatusLine(progress, snapshotNow) ?? "thinking…";
244
+ }
245
+
246
+ export function widgetRenderKey(job: AsyncJobState): string {
247
+ return JSON.stringify({
248
+ asyncDir: job.asyncDir,
249
+ status: job.status,
250
+ activityState: job.activityState,
251
+ lastActivityAt: job.lastActivityAt,
252
+ currentTool: job.currentTool,
253
+ currentToolStartedAt: job.currentToolStartedAt,
254
+ currentPath: job.currentPath,
255
+ turnCount: job.turnCount,
256
+ toolCount: job.toolCount,
257
+ mode: job.mode,
258
+ agents: job.agents,
259
+ currentStep: job.currentStep,
260
+ chainStepCount: job.chainStepCount,
261
+ parallelGroups: job.parallelGroups,
262
+ steps: job.steps,
263
+ nestedChildren: job.nestedChildren,
264
+ stepsTotal: job.stepsTotal,
265
+ runningSteps: job.runningSteps,
266
+ completedSteps: job.completedSteps,
267
+ activeParallelGroup: job.activeParallelGroup,
268
+ startedAt: job.startedAt,
269
+ updatedAt: job.updatedAt,
270
+ totalTokens: job.totalTokens,
271
+ });
272
+ }
273
+
274
+ function formatWidgetAgents(agents: string[]): string {
275
+ const distinct = [...new Set(agents)];
276
+ if (distinct.length === 1 && agents.length > 1) return `${distinct[0]} ×${agents.length}`;
277
+ if (agents.length > 3) return `${agents.slice(0, 2).join(", ")} +${agents.length - 2} more`;
278
+ return agents.join(", ");
279
+ }
280
+
281
+ function widgetJobName(job: AsyncJobState): string {
282
+ if (job.mode === "parallel") return "parallel";
283
+ if (job.mode === "chain") return "chain";
284
+ if (job.mode === "single" && job.agents?.length === 1) return job.agents[0]!;
285
+ if (job.agents?.length) return formatWidgetAgents(job.agents);
286
+ return job.mode ?? "subagent";
287
+ }
288
+
289
+ function widgetActivity(job: AsyncJobState): string {
290
+ const facts: string[] = [];
291
+ if (job.currentTool && job.currentToolStartedAt !== undefined && job.updatedAt !== undefined) facts.push(`${job.currentTool} ${formatDuration(Math.max(0, job.updatedAt - job.currentToolStartedAt))}`);
292
+ else if (job.currentTool) facts.push(job.currentTool);
293
+ if (job.currentPath) facts.push(shortenPath(job.currentPath));
294
+ if (job.turnCount !== undefined) facts.push(`${job.turnCount} turns`);
295
+ if (job.toolCount !== undefined) facts.push(`${job.toolCount} tools`);
296
+ const activity = buildLiveStatusLine(job, job.updatedAt);
297
+ if (activity && facts.length) return `${activity} · ${facts.join(" · ")}`;
298
+ if (activity) return activity;
299
+ if (facts.length) return facts.join(" · ");
300
+ if (job.status === "running") return "thinking…";
301
+ if (job.status === "queued") return "queued…";
302
+ if (job.status === "paused") return "Paused";
303
+ if (job.status === "failed") return "Failed";
304
+ return "Done";
305
+ }
306
+
307
+ function widgetStepRunningSeed(step: NonNullable<AsyncJobState["steps"]>[number], fallbackIndex?: number): number | undefined {
308
+ return runningSeed(
309
+ fallbackIndex,
310
+ step.index,
311
+ step.toolCount,
312
+ step.turnCount,
313
+ step.tokens?.total,
314
+ step.lastActivityAt,
315
+ step.currentToolStartedAt,
316
+ step.durationMs,
317
+ );
318
+ }
319
+
320
+ function widgetStepsRunningSeed(steps: Array<NonNullable<AsyncJobState["steps"]>[number]> | undefined): number | undefined {
321
+ let seed: number | undefined;
322
+ for (const [index, step] of (steps ?? []).entries()) seed = runningSeed(seed, widgetStepRunningSeed(step, index));
323
+ return seed;
324
+ }
325
+
326
+ function widgetJobRunningSeed(job: AsyncJobState): number | undefined {
327
+ return runningSeed(
328
+ job.updatedAt,
329
+ job.lastActivityAt,
330
+ job.toolCount,
331
+ job.turnCount,
332
+ job.totalTokens?.total,
333
+ job.currentStep,
334
+ job.runningSteps,
335
+ job.completedSteps,
336
+ widgetStepsRunningSeed(job.steps),
337
+ );
338
+ }
339
+
340
+ function widgetJobsRunningSeed(jobs: AsyncJobState[]): number | undefined {
341
+ let seed: number | undefined;
342
+ for (const job of jobs) seed = runningSeed(seed, widgetJobRunningSeed(job));
343
+ return seed;
344
+ }
345
+
346
+ function widgetStatusGlyph(job: AsyncJobState, theme: Theme): string {
347
+ if (job.status === "running") return theme.fg("accent", runningGlyph(widgetJobRunningSeed(job)));
348
+ if (job.status === "queued") return theme.fg("muted", "◦");
349
+ if (job.status === "complete") return theme.fg("success", "✓");
350
+ if (job.status === "paused") return theme.fg("warning", "■");
351
+ return theme.fg("error", "✗");
352
+ }
353
+
354
+ function widgetStepGlyph(status: AsyncJobStep["status"], theme: Theme, seed?: number): string {
355
+ if (status === "running") return theme.fg("accent", runningGlyph(seed));
356
+ if (status === "complete" || status === "completed") return theme.fg("success", "✓");
357
+ if (status === "failed") return theme.fg("error", "✗");
358
+ if (status === "paused") return theme.fg("warning", "■");
359
+ return theme.fg("muted", "◦");
360
+ }
361
+
362
+ function widgetStepStatus(status: AsyncJobStep["status"], theme: Theme): string {
363
+ if (status === "running") return theme.fg("accent", "running");
364
+ if (status === "complete" || status === "completed") return theme.fg("success", "complete");
365
+ if (status === "failed") return theme.fg("error", "failed");
366
+ if (status === "paused") return theme.fg("warning", "paused");
367
+ return theme.fg("dim", status);
368
+ }
369
+
370
+ function widgetStepActivity(step: NonNullable<AsyncJobState["steps"]>[number], snapshotNow?: number): string {
371
+ const facts: string[] = [];
372
+ if (step.currentTool && step.currentToolStartedAt !== undefined && snapshotNow !== undefined) facts.push(`${step.currentTool} ${formatDuration(Math.max(0, snapshotNow - step.currentToolStartedAt))}`);
373
+ else if (step.currentTool) facts.push(step.currentTool);
374
+ if (step.currentPath) facts.push(shortenPath(step.currentPath));
375
+ if (step.turnCount !== undefined) facts.push(`${step.turnCount} turns`);
376
+ if (step.toolCount !== undefined) facts.push(`${step.toolCount} tools`);
377
+ if (step.tokens?.total) facts.push(formatTokenStat(step.tokens.total));
378
+ const activity = buildLiveStatusLine(step, snapshotNow);
379
+ if (activity && facts.length) return `${activity} · ${facts.join(" · ")}`;
380
+ if (activity) return activity;
381
+ return facts.join(" · ");
382
+ }
383
+
384
+
385
+ function widgetChainDetails(job: AsyncJobState, theme: Theme, expanded = false, width = getTermWidth()): string[] {
386
+ if (!job.steps?.length) return [];
387
+ const total = job.chainStepCount ?? job.steps.length;
388
+ const lines: string[] = [];
389
+ for (const span of buildAsyncChainStepSpans(total, job.steps.length, job.parallelGroups)) {
390
+ const steps = job.steps.slice(span.start, span.start + span.count);
391
+ if (span.isParallel) {
392
+ const status = aggregateStepStatus(steps);
393
+ lines.push(` ${widgetStepGlyph(status, theme, widgetStepsRunningSeed(steps))} Step ${span.stepIndex + 1}/${total}: ${themeBold(theme, "parallel group")} ${theme.fg("dim", "·")} ${theme.fg("dim", formatParallelOutcome(steps, span.count))}`);
394
+ continue;
395
+ }
396
+ const step = steps[0];
397
+ if (!step) {
398
+ lines.push(` ${theme.fg("dim", `◦ Step ${span.stepIndex + 1}/${total}: pending`)}`);
399
+ continue;
400
+ }
401
+ lines.push(...foregroundStyleWidgetStepLines(job, theme, step, "Step", span.stepIndex + 1, total, expanded, width));
402
+ }
403
+ return lines;
404
+ }
405
+
406
+ function widgetParallelAgentDetails(job: AsyncJobState, theme: Theme, expanded = false, width = getTermWidth()): string[] {
407
+ if (!job.steps?.length) return [];
408
+ if (job.mode !== "parallel" && job.mode !== "chain") return [];
409
+ if (job.mode === "chain" && !job.activeParallelGroup && job.parallelGroups?.length) return widgetChainDetails(job, theme, expanded, width);
410
+ const total = job.stepsTotal ?? job.steps.length;
411
+ const lines: string[] = [];
412
+ for (const [index, step] of job.steps.entries()) {
413
+ const marker = index === job.steps.length - 1 ? "└" : "├";
414
+ const activity = widgetStepActivity(step, job.updatedAt);
415
+ const itemTitle = job.mode === "parallel" || job.activeParallelGroup ? "Agent" : "Step";
416
+ const modelDisplay = modelThinkingBadge(theme, step.model, step.thinking);
417
+ lines.push(` ${theme.fg("dim", `${marker} ${widgetStepGlyph(step.status, theme, widgetStepRunningSeed(step, index))} ${itemTitle} ${index + 1}/${total}: ${step.agent} · ${widgetStepStatus(step.status, theme)}${modelDisplay}${activity ? ` · ${activity}` : ""}`)}`);
418
+ for (const nestedLine of formatNestedWidgetLines(step.children, theme, width, expanded, job.updatedAt, expanded ? 8 : 1)) lines.push(` ${nestedLine}`);
419
+ }
420
+ return lines;
421
+ }
422
+
423
+ function parseParallelGroupAgentCount(label: string | undefined): number | undefined {
424
+ if (!label || !label.startsWith("[") || !label.endsWith("]")) return undefined;
425
+ const inner = label.slice(1, -1).trim();
426
+ if (!inner) return 0;
427
+ return inner.split("+").map((part) => part.trim()).filter(Boolean).length;
428
+ }
429
+
430
+ function isChainParallelGroupActive(details: Pick<Details, "mode" | "chainAgents" | "currentStepIndex">): boolean {
431
+ if (details.mode !== "chain") return false;
432
+ if (details.currentStepIndex === undefined) return false;
433
+ const currentLabel = details.chainAgents?.[details.currentStepIndex];
434
+ return parseParallelGroupAgentCount(currentLabel) !== undefined;
435
+ }
436
+
437
+ interface ChainStepSpan {
438
+ stepIndex: number;
439
+ start: number;
440
+ count: number;
441
+ isParallel: boolean;
442
+ }
443
+
444
+ function buildChainStepSpans(chainAgents: string[] | undefined): ChainStepSpan[] {
445
+ if (!chainAgents?.length) return [];
446
+ const spans: ChainStepSpan[] = [];
447
+ let start = 0;
448
+ for (let stepIndex = 0; stepIndex < chainAgents.length; stepIndex++) {
449
+ const label = chainAgents[stepIndex]!;
450
+ const parsedCount = parseParallelGroupAgentCount(label);
451
+ const count = parsedCount ?? 1;
452
+ spans.push({ stepIndex, start, count, isParallel: parsedCount !== undefined });
453
+ start += count;
454
+ }
455
+ return spans;
456
+ }
457
+
458
+ function buildAsyncChainStepSpans(total: number, stepCount: number, parallelGroups: AsyncParallelGroupStatus[] = []): ChainStepSpan[] {
459
+ const spans: ChainStepSpan[] = [];
460
+ let flatIndex = 0;
461
+ for (let stepIndex = 0; stepIndex < total; stepIndex++) {
462
+ const group = parallelGroups.find((candidate) => candidate.stepIndex === stepIndex);
463
+ if (group) {
464
+ spans.push({ stepIndex, start: group.start, count: group.count, isParallel: true });
465
+ flatIndex = Math.max(flatIndex, group.start + group.count);
466
+ continue;
467
+ }
468
+ spans.push({ stepIndex, start: flatIndex, count: flatIndex < stepCount ? 1 : 0, isParallel: false });
469
+ flatIndex++;
470
+ }
471
+ return spans;
472
+ }
473
+
474
+ function isDoneResult(result: Details["results"][number]): boolean {
475
+ const status = result.progress?.status;
476
+ if (status === "completed") return true;
477
+ if (status === "running" || status === "pending") return false;
478
+ if (result.interrupted || result.detached) return false;
479
+ return result.exitCode === 0;
480
+ }
481
+
482
+ interface MultiProgressLabel {
483
+ headerLabel: string;
484
+ itemTitle: "Step" | "Agent";
485
+ totalCount: number;
486
+ hasParallelInChain: boolean;
487
+ activeParallelGroup: boolean;
488
+ groupStartIndex: number;
489
+ groupEndIndex: number;
490
+ showActiveGroupOnly: boolean;
491
+ }
492
+
493
+ function buildMultiProgressLabel(details: Pick<Details, "mode" | "results" | "progress" | "totalSteps" | "currentStepIndex" | "chainAgents">, hasRunning: boolean): MultiProgressLabel {
494
+ const stepSpans = buildChainStepSpans(details.chainAgents);
495
+ const hasParallelInChain = details.mode === "chain" && stepSpans.some((span) => span.isParallel);
496
+ const activeParallelGroup = isChainParallelGroupActive(details);
497
+ const itemTitle: "Step" | "Agent" = details.mode === "parallel" || activeParallelGroup ? "Agent" : "Step";
498
+
499
+ if (details.mode === "parallel") {
500
+ const totalCount = details.totalSteps ?? details.results.length;
501
+ const statuses = new Array(totalCount).fill("pending") as Array<"pending" | "running" | "completed" | "failed" | "detached">;
502
+ for (const progress of details.progress ?? []) {
503
+ if (progress.index >= 0 && progress.index < totalCount) statuses[progress.index] = progress.status;
504
+ }
505
+ for (let i = 0; i < details.results.length; i++) {
506
+ const result = details.results[i]!;
507
+ const progressFromArray = details.progress?.find((progress) => progress.index === i)
508
+ || details.progress?.find((progress) => progress.agent === result.agent && progress.status === "running");
509
+ const index = result.progress?.index ?? progressFromArray?.index ?? i;
510
+ if (index < 0 || index >= totalCount) continue;
511
+ const status = result.progress?.status
512
+ ?? (result.interrupted || result.detached
513
+ ? "detached"
514
+ : result.exitCode === 0
515
+ ? "completed"
516
+ : "failed");
517
+ statuses[index] = status;
518
+ }
519
+ const running = statuses.filter((status) => status === "running").length;
520
+ const done = statuses.filter((status) => status === "completed").length;
521
+ const headerLabel = hasRunning
522
+ ? `${formatAgentRunningLabel(running)} · ${done}/${totalCount} done`
523
+ : `${done}/${totalCount} done`;
524
+ return { headerLabel, itemTitle, totalCount, hasParallelInChain, activeParallelGroup, groupStartIndex: 0, groupEndIndex: totalCount, showActiveGroupOnly: false };
525
+ }
526
+
527
+ if (activeParallelGroup) {
528
+ const currentStepIndex = details.currentStepIndex!;
529
+ const span = stepSpans[currentStepIndex];
530
+ const groupSize = span?.count ?? 1;
531
+ const groupStart = span?.start ?? 0;
532
+ const groupEnd = groupStart + groupSize;
533
+ let running = 0;
534
+ let done = 0;
535
+ for (let index = groupStart; index < groupEnd; index++) {
536
+ const progressEntry = details.progress?.find((progress) => progress.index === index);
537
+ const resultEntry = details.results.find((result) => result.progress?.index === index);
538
+ if (progressEntry?.status === "running") {
539
+ running++;
540
+ continue;
541
+ }
542
+ if (progressEntry?.status === "completed") {
543
+ done++;
544
+ continue;
545
+ }
546
+ if (resultEntry && isDoneResult(resultEntry)) done++;
547
+ }
548
+ const totalSteps = details.totalSteps ?? details.chainAgents?.length ?? 1;
549
+ const headerLabel = hasRunning
550
+ ? `step ${currentStepIndex + 1}/${totalSteps} · parallel group: ${formatAgentRunningLabel(running)} · ${done}/${groupSize} done`
551
+ : `step ${currentStepIndex + 1}/${totalSteps} · parallel group: ${done}/${groupSize} done`;
552
+ return { headerLabel, itemTitle, totalCount: groupSize, hasParallelInChain, activeParallelGroup, groupStartIndex: groupStart, groupEndIndex: groupEnd, showActiveGroupOnly: true };
553
+ }
554
+
555
+ if (details.mode === "chain" && details.chainAgents?.length) {
556
+ const totalCount = details.totalSteps ?? details.chainAgents.length;
557
+ const doneLogical = stepSpans.filter((span) => {
558
+ for (let index = span.start; index < span.start + span.count; index++) {
559
+ const progressEntry = details.progress?.find((progress) => progress.index === index);
560
+ const resultEntry = details.results.find((result) => result.progress?.index === index) ?? details.results[index];
561
+ if (progressEntry?.status === "running" || progressEntry?.status === "pending") return false;
562
+ if (resultEntry && !isDoneResult(resultEntry)) return false;
563
+ }
564
+ return true;
565
+ }).length;
566
+ const currentStep = details.currentStepIndex !== undefined ? details.currentStepIndex + 1 : Math.min(totalCount, doneLogical + (hasRunning ? 1 : 0));
567
+ const headerLabel = hasRunning ? `step ${currentStep}/${totalCount}` : `step ${doneLogical}/${totalCount}`;
568
+ return { headerLabel, itemTitle, totalCount, hasParallelInChain, activeParallelGroup, groupStartIndex: 0, groupEndIndex: details.results.length, showActiveGroupOnly: false };
569
+ }
570
+
571
+ const totalCount = details.totalSteps ?? details.results.length;
572
+ const currentStep = details.currentStepIndex !== undefined ? details.currentStepIndex + 1 : Math.min(totalCount, details.results.filter(isDoneResult).length + (hasRunning ? 1 : 0));
573
+ const done = details.results.filter(isDoneResult).length;
574
+ const headerLabel = hasRunning ? `step ${currentStep}/${totalCount}` : `step ${done}/${totalCount}`;
575
+ return { headerLabel, itemTitle, totalCount, hasParallelInChain, activeParallelGroup, groupStartIndex: 0, groupEndIndex: details.results.length, showActiveGroupOnly: false };
576
+ }
577
+
578
+ function resultRowLabel(details: Pick<Details, "mode" | "chainAgents">, label: MultiProgressLabel, resultIndex: number, stepNumber: number): string {
579
+ if (details.mode === "chain" && label.hasParallelInChain) {
580
+ const span = buildChainStepSpans(details.chainAgents).find((candidate) => resultIndex >= candidate.start && resultIndex < candidate.start + candidate.count);
581
+ if (span?.isParallel) return `Agent ${resultIndex - span.start + 1}/${span.count}`;
582
+ if (span) return `Step ${span.stepIndex + 1}`;
583
+ }
584
+ if (label.itemTitle === "Agent") {
585
+ const localStepNumber = label.activeParallelGroup
586
+ ? Math.max(1, stepNumber - label.groupStartIndex)
587
+ : stepNumber;
588
+ return `Agent ${localStepNumber}/${label.totalCount}`;
589
+ }
590
+ return `Step ${stepNumber}`;
591
+ }
592
+
593
+ function widgetStats(job: AsyncJobState, theme: Theme): string {
594
+ const parts: string[] = [];
595
+ const stepsTotal = job.stepsTotal ?? (job.agents?.length ?? 1);
596
+ if (job.activeParallelGroup) {
597
+ const running = job.runningSteps ?? (job.status === "running" ? 1 : 0);
598
+ const done = job.completedSteps ?? (job.status === "complete" ? stepsTotal : 0);
599
+ if (job.mode === "parallel") {
600
+ if (job.status === "running" && running > 0) parts.push(formatAgentRunningLabel(running));
601
+ if (stepsTotal > 0) parts.push(`${done}/${stepsTotal} done`);
602
+ } else {
603
+ const activeGroup = job.currentStep !== undefined
604
+ ? job.parallelGroups?.find((group) => job.currentStep! >= group.start && job.currentStep! < group.start + group.count)
605
+ : job.parallelGroups?.find((group) => group.start === 0);
606
+ const logicalStep = activeGroup?.stepIndex ?? job.currentStep ?? 0;
607
+ const total = job.chainStepCount ?? stepsTotal;
608
+ const groupParts = [`${done}/${stepsTotal} done`];
609
+ if (job.status === "running" && running > 0) groupParts.unshift(formatAgentRunningLabel(running));
610
+ parts.push(`step ${logicalStep + 1}/${total} · parallel group: ${groupParts.join(" · ")}`);
611
+ }
612
+ } else if (job.currentStep !== undefined) {
613
+ if (job.mode === "chain" && job.parallelGroups?.length) {
614
+ const total = job.chainStepCount ?? stepsTotal;
615
+ parts.push(`step ${flatToLogicalStepIndex(job.currentStep, total, job.parallelGroups) + 1}/${total}`);
616
+ } else {
617
+ parts.push(`step ${job.currentStep + 1}/${stepsTotal}`);
618
+ }
619
+ } else if (stepsTotal > 1) {
620
+ parts.push(`steps ${stepsTotal}`);
621
+ }
622
+ if (job.toolCount !== undefined) parts.push(formatToolUseStat(job.toolCount));
623
+ if (job.totalTokens?.total) parts.push(formatTokenStat(job.totalTokens.total));
624
+ if (job.startedAt !== undefined && job.updatedAt !== undefined) parts.push(formatDuration(Math.max(0, job.updatedAt - job.startedAt)));
625
+ return statJoin(theme, parts);
626
+ }
627
+
628
+ function widgetStepStats(theme: Theme, step: NonNullable<AsyncJobState["steps"]>[number]): string {
629
+ return statJoin(theme, [
630
+ step.turnCount !== undefined ? `${step.turnCount} turns` : "",
631
+ step.toolCount !== undefined ? formatToolUseStat(step.toolCount) : "",
632
+ step.tokens?.total ? formatTokenStat(step.tokens.total) : "",
633
+ step.durationMs !== undefined ? formatDuration(step.durationMs) : "",
634
+ ]);
635
+ }
636
+
637
+ function modelThinkingBadge(theme: Theme, model?: string, thinking?: string): string {
638
+ const label = formatModelThinking(model, thinking);
639
+ return label ? theme.fg("dim", ` (${label})`) : "";
640
+ }
641
+
642
+ function widgetStepActivityLine(step: NonNullable<AsyncJobState["steps"]>[number], width: number, expanded: boolean, snapshotNow?: number): string {
643
+ const toolLine = formatCurrentToolLine(step, width, expanded, snapshotNow);
644
+ if (toolLine) return toolLine;
645
+ const activity = buildLiveStatusLine(step, snapshotNow);
646
+ if (activity) return activity;
647
+ if (step.status === "running") return "thinking…";
648
+ return "";
649
+ }
650
+
651
+ function widgetOutputPath(job: AsyncJobState, step: NonNullable<AsyncJobState["steps"]>[number]): string | undefined {
652
+ if (typeof step.index !== "number") return undefined;
653
+ return path.join(job.asyncDir, `output-${step.index}.log`);
654
+ }
655
+
656
+ function nestedRunName(run: NestedRunSummary): string {
657
+ if (run.agent) return run.agent;
658
+ if (run.agents?.length) return formatWidgetAgents(run.agents);
659
+ return run.id;
660
+ }
661
+
662
+ function nestedStatusGlyph(state: NestedRunSummary["state"] | NestedStepSummary["status"], theme: Theme, seed?: number): string {
663
+ if (state === "running") return theme.fg("accent", runningGlyph(seed));
664
+ if (state === "complete" || state === "completed") return theme.fg("success", "✓");
665
+ if (state === "failed") return theme.fg("error", "✗");
666
+ if (state === "paused") return theme.fg("warning", "■");
667
+ return theme.fg("muted", "◦");
668
+ }
669
+
670
+ function nestedRunSeed(run: NestedRunSummary): number | undefined {
671
+ return runningSeed(run.lastUpdate, run.lastActivityAt, run.currentStep, run.toolCount, run.turnCount, run.totalTokens?.total, run.currentToolStartedAt);
672
+ }
673
+
674
+ function nestedActivity(input: Pick<NestedRunSummary | NestedStepSummary, "activityState" | "lastActivityAt" | "currentTool" | "currentToolStartedAt" | "currentPath" | "turnCount" | "toolCount">, state: NestedRunSummary["state"] | NestedStepSummary["status"], snapshotNow?: number): string {
675
+ const facts: string[] = [];
676
+ if (input.currentTool && input.currentToolStartedAt !== undefined && snapshotNow !== undefined) facts.push(`${input.currentTool} ${formatDuration(Math.max(0, snapshotNow - input.currentToolStartedAt))}`);
677
+ else if (input.currentTool) facts.push(input.currentTool);
678
+ if (input.currentPath) facts.push(shortenPath(input.currentPath));
679
+ if (input.turnCount !== undefined) facts.push(`${input.turnCount} turns`);
680
+ if (input.toolCount !== undefined) facts.push(`${input.toolCount} tools`);
681
+ const activity = buildLiveStatusLine(input, snapshotNow);
682
+ if (activity && facts.length) return `${activity} · ${facts.join(" · ")}`;
683
+ if (activity) return activity;
684
+ if (facts.length) return facts.join(" · ");
685
+ if (state === "running") return "thinking…";
686
+ if (state === "queued" || state === "pending") return "queued…";
687
+ if (state === "paused") return "Paused";
688
+ if (state === "failed") return "Failed";
689
+ return "Done";
690
+ }
691
+
692
+ function formatNestedWidgetLines(children: NestedRunSummary[] | undefined, theme: Theme, width: number, expanded: boolean, snapshotNow?: number, lineBudget = expanded ? 12 : 1): string[] {
693
+ if (!children?.length || lineBudget <= 0) return [];
694
+ if (!expanded) {
695
+ const aggregate = formatNestedAggregate(children);
696
+ return aggregate ? [theme.fg("dim", `↳ ${aggregate}`)] : [];
697
+ }
698
+ const lines: string[] = [];
699
+ const maxDepth = 2;
700
+ const append = (items: NestedRunSummary[] | undefined, depth: number, prefix: string): void => {
701
+ if (!items?.length || lines.length >= lineBudget) return;
702
+ if (depth > maxDepth) {
703
+ const aggregate = formatNestedAggregate(items);
704
+ if (aggregate && lines.length < lineBudget) lines.push(theme.fg("dim", `${prefix}↳ ${aggregate}`));
705
+ return;
706
+ }
707
+ for (let index = 0; index < items.length; index++) {
708
+ const child = items[index]!;
709
+ if (lines.length >= lineBudget) {
710
+ const aggregate = formatNestedAggregate(items.slice(index));
711
+ if (aggregate) lines[lines.length - 1] = theme.fg("dim", `${prefix}↳ ${aggregate}`);
712
+ return;
713
+ }
714
+ const activity = nestedActivity(child, child.state, snapshotNow ?? child.lastUpdate);
715
+ const error = child.error ? ` · ${child.error}` : "";
716
+ lines.push(theme.fg("dim", `${prefix}↳ ${nestedStatusGlyph(child.state, theme, nestedRunSeed(child))} ${nestedRunName(child)} · ${child.state} · ${activity}${error}`));
717
+ if (depth === maxDepth) {
718
+ const aggregate = formatNestedAggregate([...(child.steps?.flatMap((step) => step.children ?? []) ?? []), ...(child.children ?? [])]);
719
+ if (aggregate && lines.length < lineBudget) lines.push(theme.fg("dim", `${prefix} ↳ ${aggregate}`));
720
+ continue;
721
+ }
722
+ for (const step of child.steps ?? []) {
723
+ if (lines.length >= lineBudget) return;
724
+ lines.push(theme.fg("dim", `${prefix} ↳ ${nestedStatusGlyph(step.status, theme)} ${step.agent} · ${step.status} · ${nestedActivity(step, step.status, snapshotNow ?? child.lastUpdate)}`));
725
+ append(step.children, depth + 1, `${prefix} `);
726
+ }
727
+ append(child.children, depth + 1, `${prefix} `);
728
+ }
729
+ };
730
+ append(children, 0, "");
731
+ return lines.map((line) => truncLine(line, width));
732
+ }
733
+
734
+ function foregroundStyleWidgetStepLines(
735
+ job: AsyncJobState,
736
+ theme: Theme,
737
+ step: NonNullable<AsyncJobState["steps"]>[number],
738
+ itemTitle: "Agent" | "Step",
739
+ index: number,
740
+ total: number,
741
+ expanded: boolean,
742
+ width: number,
743
+ ): string[] {
744
+ const status = widgetStepStatus(step.status, theme);
745
+ const stats = widgetStepStats(theme, step);
746
+ const modelDisplay = modelThinkingBadge(theme, step.model, step.thinking);
747
+ const lines = [` ${widgetStepGlyph(step.status, theme, widgetStepRunningSeed(step, index - 1))} ${itemTitle} ${index}/${total}: ${themeBold(theme, step.agent)} ${theme.fg("dim", "·")} ${status}${modelDisplay}${stats ? ` ${theme.fg("dim", "·")} ${stats}` : ""}`];
748
+ const activity = widgetStepActivityLine(step, width, expanded, job.updatedAt);
749
+ if (activity) lines.push(` ${theme.fg("dim", `⎿ ${activity}`)}`);
750
+ for (const nestedLine of formatNestedWidgetLines(step.children, theme, width, expanded, job.updatedAt)) {
751
+ lines.push(` ${nestedLine}`);
752
+ }
753
+ if (step.status === "running") {
754
+ if (!expanded) lines.push(` ${theme.fg("accent", "Press Ctrl+O for live detail")}`);
755
+ const output = widgetOutputPath(job, step);
756
+ if (output) lines.push(` ${theme.fg("dim", `output: ${shortenPath(output)}`)}`);
757
+ if (expanded) {
758
+ const liveStatus = buildLiveStatusLine(step, job.updatedAt);
759
+ if (liveStatus && liveStatus !== activity) lines.push(` ${theme.fg("accent", liveStatus)}`);
760
+ for (const tool of step.recentTools?.slice(-3) ?? []) {
761
+ const maxArgsLen = Math.max(40, width - 30);
762
+ const argsPreview = tool.args.length <= maxArgsLen ? tool.args : `${tool.args.slice(0, maxArgsLen)}...`;
763
+ lines.push(` ${theme.fg("dim", `${tool.tool}${argsPreview ? `: ${argsPreview}` : ""}`)}`);
764
+ }
765
+ for (const line of step.recentOutput?.slice(-5) ?? []) {
766
+ lines.push(` ${theme.fg("dim", line)}`);
767
+ }
768
+ }
769
+ }
770
+ return lines;
771
+ }
772
+
773
+ function foregroundStyleWidgetDetails(job: AsyncJobState, theme: Theme, expanded: boolean, width: number): string[] {
774
+ if (!job.steps?.length) return [
775
+ ` ${theme.fg("dim", `⎿ ${widgetActivity(job)}`)}`,
776
+ ...formatNestedWidgetLines(job.nestedChildren, theme, width, expanded, job.updatedAt).map((line) => ` ${line}`),
777
+ ];
778
+ if (job.mode === "chain" && !job.activeParallelGroup && job.parallelGroups?.length) return widgetChainDetails(job, theme, expanded, width);
779
+ const total = job.stepsTotal ?? job.steps.length;
780
+ const itemTitle = job.mode === "parallel" || job.activeParallelGroup ? "Agent" : "Step";
781
+ const lines: string[] = [];
782
+ for (const [index, step] of job.steps.entries()) {
783
+ lines.push(...foregroundStyleWidgetStepLines(job, theme, step, itemTitle, index + 1, total, expanded, width));
784
+ }
785
+ const attached = new Set(job.steps.flatMap((step) => step.children?.map((child) => child.id) ?? []));
786
+ const unattached = job.nestedChildren?.filter((child) => !attached.has(child.id)) ?? [];
787
+ for (const nestedLine of formatNestedWidgetLines(unattached, theme, width, expanded, job.updatedAt)) {
788
+ lines.push(` ${nestedLine}`);
789
+ }
790
+ return lines;
791
+ }
792
+
793
+ function buildSingleWidgetLines(job: AsyncJobState, theme: Theme, width: number, expanded: boolean): string[] {
794
+ const stats = widgetStats(job, theme);
795
+ const count = job.mode === "chain" ? job.chainStepCount : job.stepsTotal ?? job.agents?.length ?? job.steps?.length;
796
+ const mode = widgetJobName(job);
797
+ const title = `async subagent ${mode}${count && count > 1 ? ` (${count})` : ""}`;
798
+ return [
799
+ `${theme.fg("toolTitle", themeBold(theme, title))} ${theme.fg("dim", "· background")}`,
800
+ `${widgetStatusGlyph(job, theme)} ${themeBold(theme, mode)}${stats ? ` ${theme.fg("dim", "·")} ${stats}` : ""}`,
801
+ ...foregroundStyleWidgetDetails(job, theme, expanded, width),
802
+ ].map((line) => truncLine(line, width));
803
+ }
804
+
805
+ function compactSingleWidgetLines(job: AsyncJobState, theme: Theme, width: number): string[] {
806
+ const fullLines = buildSingleWidgetLines(job, theme, width, false);
807
+ if (fullLines.length <= 10 || !job.steps?.length || (job.mode !== "parallel" && !job.activeParallelGroup)) return fullLines;
808
+
809
+ const total = job.stepsTotal ?? job.steps.length;
810
+ const itemTitle = job.mode === "parallel" || job.activeParallelGroup ? "Agent" : "Step";
811
+ const lines = fullLines.slice(0, 2);
812
+ for (const [index, step] of job.steps.entries()) {
813
+ const status = widgetStepStatus(step.status, theme);
814
+ const activity = widgetStepActivityLine(step, width, false, job.updatedAt);
815
+ const stepStats = widgetStepStats(theme, step);
816
+ const activitySuffix = activity ? ` ${theme.fg("dim", "·")} ${theme.fg("dim", activity)}` : "";
817
+ const modelDisplay = modelThinkingBadge(theme, step.model, step.thinking);
818
+ lines.push(` ${widgetStepGlyph(step.status, theme, widgetStepRunningSeed(step, index))} ${itemTitle} ${index + 1}/${total}: ${themeBold(theme, step.agent)} ${theme.fg("dim", "·")} ${status}${modelDisplay}${activitySuffix}${stepStats ? ` ${theme.fg("dim", "·")} ${stepStats}` : ""}`);
819
+ for (const nestedLine of formatNestedWidgetLines(step.children, theme, width, false, job.updatedAt)) lines.push(` ${nestedLine}`);
820
+ }
821
+ if (job.steps.some((step) => step.status === "running")) lines.push(theme.fg("accent", " Press Ctrl+O for live detail"));
822
+ return lines.map((line) => truncLine(line, width));
823
+ }
824
+
825
+ function fitWidgetLineBudget(lines: string[], theme: Theme, width: number, expanded: boolean): string[] {
826
+ const rows = process.stdout.rows || 30;
827
+ const budget = expanded
828
+ ? Math.max(12, Math.min(24, Math.floor(rows * 0.55)))
829
+ : Math.max(10, Math.min(14, Math.floor(rows * 0.35)));
830
+ if (lines.length <= budget) return lines;
831
+ const visibleLines = Math.max(1, budget - 1);
832
+ const hiddenCount = lines.length - visibleLines;
833
+ const hint = expanded
834
+ ? `… ${hiddenCount} live-detail lines hidden`
835
+ : `… ${hiddenCount} lines hidden · Ctrl+O expands`;
836
+ return [...lines.slice(0, visibleLines), truncLine(theme.fg("dim", hint), width)];
837
+ }
838
+
839
+ function buildWidgetComponent(jobs: AsyncJobState[], expanded: boolean): (_tui: unknown, theme: Theme) => Component {
840
+ return (_tui, theme) => {
841
+ const width = getTermWidth();
842
+ const lines = expanded
843
+ ? buildWidgetLines(jobs, theme, width, true)
844
+ : jobs.length === 1
845
+ ? compactSingleWidgetLines(jobs[0]!, theme, width)
846
+ : buildWidgetLines(jobs, theme, width, false);
847
+ const container = new Container();
848
+ for (const line of fitWidgetLineBudget(lines, theme, width, expanded)) container.addChild(new Text(line, 1, 0));
849
+ return container;
850
+ };
851
+ }
852
+
853
+ export function buildWidgetLines(jobs: AsyncJobState[], theme: Theme, width = getTermWidth(), expanded = false): string[] {
854
+ if (jobs.length === 0) return [];
855
+ if (jobs.length === 1) return buildSingleWidgetLines(jobs[0]!, theme, width, expanded);
856
+ const running = jobs.filter((job) => job.status === "running");
857
+ const queued = jobs.filter((job) => job.status === "queued");
858
+ const finished = jobs.filter((job) => job.status !== "running" && job.status !== "queued");
859
+
860
+ const lines: string[] = [];
861
+ const hasActive = running.length > 0 || queued.length > 0;
862
+ const headerGlyph = running.length > 0 ? runningGlyph(widgetJobsRunningSeed(running)) : hasActive ? "●" : "○";
863
+ lines.push(truncLine(`${theme.fg(hasActive ? "accent" : "dim", headerGlyph)} ${theme.fg(hasActive ? "accent" : "dim", "Async agents")} ${theme.fg("dim", "· background")}`, width));
864
+
865
+ const items: string[][] = [];
866
+ let hiddenRunning = 0;
867
+ let hiddenFinished = 0;
868
+ let queuedSummaryShown = false;
869
+ let slots = MAX_WIDGET_JOBS;
870
+
871
+ for (const job of running) {
872
+ if (slots <= 0) { hiddenRunning++; continue; }
873
+ const stats = widgetStats(job, theme);
874
+ items.push([
875
+ `${widgetStatusGlyph(job, theme)} ${themeBold(theme, widgetJobName(job))}${stats ? ` ${theme.fg("dim", "·")} ${stats}` : ""}`,
876
+ ` ${theme.fg("dim", `⎿ ${widgetActivity(job)}`)}`,
877
+ ...widgetParallelAgentDetails(job, theme, expanded, width),
878
+ ]);
879
+ slots--;
880
+ }
881
+
882
+ if (queued.length > 0 && slots > 0) {
883
+ items.push([`${theme.fg("muted", "◦")} ${theme.fg("dim", `${queued.length} queued`)}`]);
884
+ queuedSummaryShown = true;
885
+ slots--;
886
+ }
887
+
888
+ for (const job of finished) {
889
+ if (slots <= 0) { hiddenFinished++; continue; }
890
+ const stats = widgetStats(job, theme);
891
+ items.push([
892
+ `${widgetStatusGlyph(job, theme)} ${themeBold(theme, widgetJobName(job))}${stats ? ` ${theme.fg("dim", "·")} ${stats}` : ""}`,
893
+ ` ${theme.fg("dim", `⎿ ${widgetActivity(job)}`)}`,
894
+ ...widgetParallelAgentDetails(job, theme, expanded, width),
895
+ ]);
896
+ slots--;
897
+ }
898
+
899
+ const hiddenQueued = queued.length > 0 && !queuedSummaryShown ? queued.length : 0;
900
+ const hiddenTotal = hiddenRunning + hiddenFinished + hiddenQueued;
901
+ if (hiddenTotal > 0) {
902
+ const parts: string[] = [];
903
+ if (hiddenRunning > 0) parts.push(`${hiddenRunning} running`);
904
+ if (hiddenQueued > 0) parts.push(`${hiddenQueued} queued`);
905
+ if (hiddenFinished > 0) parts.push(`${hiddenFinished} finished`);
906
+ items.push([theme.fg("dim", `+${hiddenTotal} more (${parts.join(", ")})`)]);
907
+ }
908
+
909
+ for (let i = 0; i < items.length; i++) {
910
+ const item = items[i]!;
911
+ const last = i === items.length - 1;
912
+ const branch = last ? "└─" : "├─";
913
+ const continuation = last ? " " : "│ ";
914
+ lines.push(truncLine(`${theme.fg("dim", branch)} ${item[0]}`, width));
915
+ for (const detail of item.slice(1)) {
916
+ lines.push(truncLine(`${theme.fg("dim", continuation)} ${detail}`, width));
917
+ }
918
+ }
919
+
920
+ return lines;
921
+ }
922
+
923
+ /**
924
+ * Render the async jobs widget
925
+ */
926
+ export function renderWidget(ctx: ExtensionContext, jobs: AsyncJobState[]): void {
927
+ if (jobs.length === 0) {
928
+ if (ctx.hasUI) ctx.ui.setWidget(WIDGET_KEY, undefined);
929
+ return;
930
+ }
931
+ if (!ctx.hasUI) return;
932
+ ctx.ui.setWidget(WIDGET_KEY, buildWidgetComponent(jobs, ctx.ui.getToolsExpanded?.() ?? false));
933
+ }
934
+
935
+ function renderSingleCompact(d: Details, r: Details["results"][number], theme: Theme): Component {
936
+ const output = r.truncation?.text || getSingleResultOutput(r);
937
+ const progress = r.progress || r.progressSummary;
938
+ const isRunning = r.progress?.status === "running";
939
+ const contextBadge = d.context === "fork" ? theme.fg("warning", " [fork]") : "";
940
+ const stats = statJoin(theme, [
941
+ r.usage?.turns ? `⟳ ${r.usage.turns}` : "",
942
+ formatProgressStats(theme, progress),
943
+ ]);
944
+ const c = new Container();
945
+ const width = getTermWidth() - 4;
946
+ const modelDisplay = modelThinkingBadge(theme, r.model);
947
+ c.addChild(new Text(truncLine(`${resultGlyph(r, output, theme, isRunning)} ${theme.fg("toolTitle", theme.bold(r.agent))}${modelDisplay}${contextBadge}${stats ? ` ${theme.fg("dim", "·")} ${stats}` : ""}`, width), 0, 0));
948
+
949
+ if (isRunning && r.progress) {
950
+ const progressSnapshotNow = snapshotNowForProgress(r.progress);
951
+ const activity = compactCurrentActivity(r.progress);
952
+ c.addChild(new Text(truncLine(theme.fg("dim", ` ⎿ ${activity}`), width), 0, 0));
953
+ const liveStatus = buildLiveStatusLine(r.progress, progressSnapshotNow);
954
+ if (liveStatus && liveStatus !== activity) c.addChild(new Text(truncLine(theme.fg("dim", ` ${liveStatus}`), width), 0, 0));
955
+ c.addChild(new Text(truncLine(theme.fg("accent", " Press Ctrl+O for live detail"), width), 0, 0));
956
+ if (r.artifactPaths) c.addChild(new Text(truncLine(theme.fg("dim", ` output: ${shortenPath(r.artifactPaths.outputPath)}`), width), 0, 0));
957
+ return c;
958
+ }
959
+
960
+ c.addChild(new Text(truncLine(theme.fg("dim", ` ⎿ ${resultStatusLine(r, output)}`), width), 0, 0));
961
+ const preview = firstOutputLine(output);
962
+ if (preview && r.exitCode === 0 && !hasEmptyTextOutputWithoutOutputTarget(r.task, output)) {
963
+ c.addChild(new Text(truncLine(theme.fg("dim", ` ${preview}`), width), 0, 0));
964
+ }
965
+ if (r.sessionFile) c.addChild(new Text(truncLine(theme.fg("dim", ` session: ${shortenPath(r.sessionFile)}`), width), 0, 0));
966
+ if (r.artifactPaths) c.addChild(new Text(truncLine(theme.fg("dim", ` output: ${shortenPath(r.artifactPaths.outputPath)}`), width), 0, 0));
967
+ if (r.truncation?.artifactPath) c.addChild(new Text(truncLine(theme.fg("dim", ` full output: ${shortenPath(r.truncation.artifactPath)}`), width), 0, 0));
968
+ return c;
969
+ }
970
+
971
+ function renderMultiCompact(d: Details, theme: Theme): Component {
972
+ const hasRunning = d.progress?.some((p) => p.status === "running")
973
+ || d.results.some((r) => r.progress?.status === "running");
974
+ const failed = d.results.some((r) => r.exitCode !== 0 && r.progress?.status !== "running");
975
+ const paused = d.results.some((r) => (r.interrupted || r.detached) && r.progress?.status !== "running");
976
+ let totalSummary = d.progressSummary;
977
+ if (!totalSummary) {
978
+ let sawProgress = false;
979
+ const summary = { toolCount: 0, tokens: 0, durationMs: 0 };
980
+ for (const r of d.results) {
981
+ const prog = r.progress || r.progressSummary;
982
+ if (!prog) continue;
983
+ sawProgress = true;
984
+ summary.toolCount += prog.toolCount;
985
+ summary.tokens += prog.tokens;
986
+ summary.durationMs = d.mode === "chain" ? summary.durationMs + prog.durationMs : Math.max(summary.durationMs, prog.durationMs);
987
+ }
988
+ if (sawProgress) totalSummary = summary;
989
+ }
990
+ const multiLabel = buildMultiProgressLabel(d, hasRunning);
991
+ const itemTitle = multiLabel.itemTitle;
992
+ const stats = statJoin(theme, [multiLabel.headerLabel, formatProgressStats(theme, totalSummary)]);
993
+ const glyph = hasRunning
994
+ ? theme.fg("accent", runningGlyph(runningSeed(progressRunningSeed(totalSummary), d.currentStepIndex)))
995
+ : failed
996
+ ? theme.fg("error", "✗")
997
+ : paused
998
+ ? theme.fg("warning", "■")
999
+ : theme.fg("success", "✓");
1000
+ const contextBadge = d.context === "fork" ? theme.fg("warning", " [fork]") : "";
1001
+ const c = new Container();
1002
+ const width = getTermWidth() - 4;
1003
+ c.addChild(new Text(truncLine(`${glyph} ${theme.fg("toolTitle", theme.bold(d.mode))}${contextBadge}${stats ? ` ${theme.fg("dim", "·")} ${stats}` : ""}`, width), 0, 0));
1004
+
1005
+ const useResultsDirectly = multiLabel.hasParallelInChain || !d.chainAgents?.length;
1006
+ const displayStart = multiLabel.showActiveGroupOnly ? multiLabel.groupStartIndex : 0;
1007
+ const displayEnd = multiLabel.showActiveGroupOnly ? multiLabel.groupEndIndex : (useResultsDirectly ? d.results.length : d.chainAgents!.length);
1008
+ for (let i = displayStart; i < displayEnd; i++) {
1009
+ const r = d.results[i];
1010
+ const fallbackLabel = itemTitle.toLowerCase();
1011
+ const rowNumber = multiLabel.showActiveGroupOnly ? (i - multiLabel.groupStartIndex + 1) : (i + 1);
1012
+ const agentName = useResultsDirectly ? (r?.agent || `${fallbackLabel}-${rowNumber}`) : (d.chainAgents![i] || r?.agent || `${fallbackLabel}-${rowNumber}`);
1013
+ if (!r) {
1014
+ c.addChild(new Text(truncLine(theme.fg("dim", ` ◦ ${itemTitle} ${rowNumber}: ${agentName} · pending`), width), 0, 0));
1015
+ continue;
1016
+ }
1017
+ const output = getSingleResultOutput(r);
1018
+ const progressFromArray = d.progress?.find((p) => p.index === i) || d.progress?.find((p) => p.agent === r.agent && p.status === "running");
1019
+ const rProg = r.progress || progressFromArray || r.progressSummary;
1020
+ const rRunning = rProg && "status" in rProg && rProg.status === "running";
1021
+ const rPending = rProg && "status" in rProg && rProg.status === "pending";
1022
+ const stepNumber = r.progress?.index !== undefined ? r.progress.index + 1 : progressFromArray?.index !== undefined ? progressFromArray.index + 1 : i + 1;
1023
+ const stepStats = formatProgressStats(theme, rProg);
1024
+ const glyph = rPending ? theme.fg("dim", "◦") : resultGlyph(r, output, theme, rRunning, progressRunningSeed(rProg));
1025
+ const pendingLabel = rPending ? ` ${theme.fg("dim", "· pending")}` : "";
1026
+ const stepLabel = resultRowLabel(d, multiLabel, i, stepNumber);
1027
+ const line = `${glyph} ${stepLabel}: ${themeBold(theme, agentName)}${stepStats ? ` ${theme.fg("dim", "·")} ${stepStats}` : ""}${pendingLabel}`;
1028
+ c.addChild(new Text(truncLine(` ${line}`, width), 0, 0));
1029
+ if (rRunning && rProg && "status" in rProg) {
1030
+ const activity = compactCurrentActivity(rProg);
1031
+ c.addChild(new Text(truncLine(theme.fg("dim", ` ⎿ ${activity}`), width), 0, 0));
1032
+ c.addChild(new Text(truncLine(theme.fg("accent", " Press Ctrl+O for live detail"), width), 0, 0));
1033
+ } else if (!rPending && (r.exitCode !== 0 || r.interrupted || r.detached || hasEmptyTextOutputWithoutOutputTarget(r.task, output))) {
1034
+ c.addChild(new Text(truncLine(theme.fg(r.exitCode !== 0 ? "error" : "dim", ` ⎿ ${resultStatusLine(r, output)}`), width), 0, 0));
1035
+ }
1036
+ const outputTarget = extractOutputTarget(r.task);
1037
+ if (outputTarget) c.addChild(new Text(truncLine(theme.fg("dim", ` output: ${outputTarget}`), width), 0, 0));
1038
+ if (r.artifactPaths) c.addChild(new Text(truncLine(theme.fg("dim", ` output: ${shortenPath(r.artifactPaths.outputPath)}`), width), 0, 0));
1039
+ }
1040
+ if (d.artifacts) c.addChild(new Text(truncLine(theme.fg("dim", ` artifacts: ${shortenPath(d.artifacts.dir)}`), width), 0, 0));
1041
+ return c;
1042
+ }
1043
+
1044
+ /**
1045
+ * Render a subagent result
1046
+ */
1047
+ export function renderSubagentResult(
1048
+ result: AgentToolResult<Details>,
1049
+ options: { expanded: boolean },
1050
+ theme: Theme,
1051
+ ): Component {
1052
+ const d = result.details;
1053
+ if (!d || !d.results.length) {
1054
+ const t = result.content[0];
1055
+ const text = t?.type === "text" ? t.text : "(no output)";
1056
+ const contextPrefix = d?.context === "fork" ? `${theme.fg("warning", "[fork]")} ` : "";
1057
+ return new Text(truncLine(`${contextPrefix}${text}`, getTermWidth() - 4), 0, 0);
1058
+ }
1059
+
1060
+ const expanded = options.expanded;
1061
+ const mdTheme = getMarkdownTheme();
1062
+
1063
+ if (d.mode === "single" && d.results.length === 1) {
1064
+ const r = d.results[0];
1065
+ if (!expanded) return renderSingleCompact(d, r, theme);
1066
+ const isRunning = r.progress?.status === "running";
1067
+ const icon = isRunning
1068
+ ? theme.fg("warning", "running")
1069
+ : r.detached
1070
+ ? theme.fg("warning", "detached")
1071
+ : r.exitCode === 0
1072
+ ? theme.fg("success", "ok")
1073
+ : theme.fg("error", "failed");
1074
+ const contextBadge = d.context === "fork" ? theme.fg("warning", " [fork]") : "";
1075
+ const output = r.truncation?.text || getSingleResultOutput(r);
1076
+
1077
+ const progressInfo = isRunning && r.progress
1078
+ ? ` | ${r.progress.toolCount} tools, ${formatTokens(r.progress.tokens)} tok, ${formatDuration(r.progress.durationMs)}`
1079
+ : r.progressSummary
1080
+ ? ` | ${r.progressSummary.toolCount} tools, ${formatTokens(r.progressSummary.tokens)} tok, ${formatDuration(r.progressSummary.durationMs)}`
1081
+ : "";
1082
+
1083
+ const w = getTermWidth() - 4;
1084
+ const fit = (text: string) => expanded ? text : truncLine(text, w);
1085
+ const toolCallLines = getToolCallLines(r, expanded);
1086
+ const c = new Container();
1087
+ c.addChild(new Text(fit(`${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${contextBadge}${progressInfo}`), 0, 0));
1088
+ c.addChild(new Spacer(1));
1089
+ const taskMaxLen = Math.max(20, w - 8);
1090
+ const taskPreview = expanded || r.task.length <= taskMaxLen
1091
+ ? r.task
1092
+ : `${r.task.slice(0, taskMaxLen)}...`;
1093
+ c.addChild(
1094
+ new Text(fit(theme.fg("dim", `Task: ${taskPreview}`)), 0, 0),
1095
+ );
1096
+ c.addChild(new Spacer(1));
1097
+
1098
+ if (isRunning && r.progress) {
1099
+ const progressSnapshotNow = snapshotNowForProgress(r.progress);
1100
+ const toolLine = formatCurrentToolLine(r.progress, w, expanded, progressSnapshotNow);
1101
+ if (toolLine) {
1102
+ c.addChild(new Text(fit(theme.fg("warning", `> ${toolLine}`)), 0, 0));
1103
+ }
1104
+ const liveStatusLine = buildLiveStatusLine(r.progress, progressSnapshotNow);
1105
+ if (liveStatusLine) {
1106
+ c.addChild(new Text(fit(theme.fg("accent", liveStatusLine)), 0, 0));
1107
+ }
1108
+ c.addChild(new Text(fit(theme.fg("accent", "Press Ctrl+O for live detail")), 0, 0));
1109
+ if (r.artifactPaths) {
1110
+ c.addChild(new Text(fit(theme.fg("dim", `Artifacts: ${shortenPath(r.artifactPaths.outputPath)}`)), 0, 0));
1111
+ }
1112
+ if (r.progress.recentTools?.length) {
1113
+ for (const t of r.progress.recentTools.slice(-3)) {
1114
+ const maxArgsLen = Math.max(40, w - 24);
1115
+ const argsPreview = expanded || t.args.length <= maxArgsLen
1116
+ ? t.args
1117
+ : `${t.args.slice(0, maxArgsLen)}...`;
1118
+ c.addChild(new Text(fit(theme.fg("dim", `${t.tool}: ${argsPreview}`)), 0, 0));
1119
+ }
1120
+ }
1121
+ for (const line of (r.progress.recentOutput ?? []).slice(-5)) {
1122
+ c.addChild(new Text(fit(theme.fg("dim", ` ${line}`)), 0, 0));
1123
+ }
1124
+ if (toolLine || liveStatusLine || r.progress.recentTools?.length || r.progress.recentOutput?.length || r.artifactPaths) {
1125
+ c.addChild(new Spacer(1));
1126
+ }
1127
+ }
1128
+
1129
+ if (expanded) {
1130
+ for (const line of toolCallLines) {
1131
+ c.addChild(new Text(fit(theme.fg("muted", line)), 0, 0));
1132
+ }
1133
+ if (toolCallLines.length) c.addChild(new Spacer(1));
1134
+ }
1135
+
1136
+ if (output) c.addChild(new Markdown(output, 0, 0, mdTheme));
1137
+ c.addChild(new Spacer(1));
1138
+ if (r.skills?.length) {
1139
+ c.addChild(new Text(fit(theme.fg("dim", `Skills: ${r.skills.join(", ")}`)), 0, 0));
1140
+ }
1141
+ if (r.skillsWarning) {
1142
+ c.addChild(new Text(fit(theme.fg("warning", `Warning: ${r.skillsWarning}`)), 0, 0));
1143
+ }
1144
+ if (r.attemptedModels && r.attemptedModels.length > 1) {
1145
+ c.addChild(new Text(fit(theme.fg("dim", `Fallbacks: ${r.attemptedModels.join(" → ")}`)), 0, 0));
1146
+ }
1147
+ c.addChild(new Text(fit(theme.fg("dim", formatUsage(r.usage, r.model))), 0, 0));
1148
+ if (r.sessionFile) {
1149
+ c.addChild(new Text(fit(theme.fg("dim", `Session: ${shortenPath(r.sessionFile)}`)), 0, 0));
1150
+ }
1151
+
1152
+ if (!isRunning && r.artifactPaths) {
1153
+ c.addChild(new Spacer(1));
1154
+ c.addChild(new Text(fit(theme.fg("dim", `Artifacts: ${shortenPath(r.artifactPaths.outputPath)}`)), 0, 0));
1155
+ }
1156
+ return c;
1157
+ }
1158
+
1159
+ if (!expanded) return renderMultiCompact(d, theme);
1160
+
1161
+ const hasRunning = d.progress?.some((p) => p.status === "running")
1162
+ || d.results.some((r) => r.progress?.status === "running");
1163
+ const ok = d.results.filter((r) => r.progress?.status === "completed" || (r.exitCode === 0 && r.progress?.status !== "running")).length;
1164
+ const hasEmptyWithoutTarget = d.results.some((r) =>
1165
+ r.exitCode === 0
1166
+ && r.progress?.status !== "running"
1167
+ && hasEmptyTextOutputWithoutOutputTarget(r.task, getSingleResultOutput(r)),
1168
+ );
1169
+ const icon = hasRunning
1170
+ ? theme.fg("warning", "running")
1171
+ : hasEmptyWithoutTarget
1172
+ ? theme.fg("warning", "warning")
1173
+ : ok === d.results.length
1174
+ ? theme.fg("success", "ok")
1175
+ : theme.fg("error", "failed");
1176
+
1177
+ const totalSummary =
1178
+ d.progressSummary ||
1179
+ d.results.reduce(
1180
+ (acc, r) => {
1181
+ const prog = r.progress || r.progressSummary;
1182
+ if (prog) {
1183
+ acc.toolCount += prog.toolCount;
1184
+ acc.tokens += prog.tokens;
1185
+ acc.durationMs =
1186
+ d.mode === "chain"
1187
+ ? acc.durationMs + prog.durationMs
1188
+ : Math.max(acc.durationMs, prog.durationMs);
1189
+ }
1190
+ return acc;
1191
+ },
1192
+ { toolCount: 0, tokens: 0, durationMs: 0 },
1193
+ );
1194
+
1195
+ const summaryStr =
1196
+ totalSummary.toolCount || totalSummary.tokens
1197
+ ? ` | ${totalSummary.toolCount} tools, ${formatTokens(totalSummary.tokens)} tok, ${formatDuration(totalSummary.durationMs)}`
1198
+ : "";
1199
+
1200
+ const modeLabel = d.mode;
1201
+ const contextBadge = d.context === "fork" ? theme.fg("warning", " [fork]") : "";
1202
+ const multiLabel = buildMultiProgressLabel(d, hasRunning);
1203
+ const itemTitle = multiLabel.itemTitle;
1204
+
1205
+ const chainVis = d.chainAgents?.length && !multiLabel.hasParallelInChain
1206
+ ? d.chainAgents
1207
+ .map((agent, i) => {
1208
+ const result = d.results[i];
1209
+ const isFailed = result && result.exitCode !== 0 && result.progress?.status !== "running";
1210
+ const isComplete = result && result.exitCode === 0 && result.progress?.status !== "running";
1211
+ const isEmptyWithoutTarget = Boolean(result)
1212
+ && Boolean(isComplete)
1213
+ && hasEmptyTextOutputWithoutOutputTarget(result.task, getSingleResultOutput(result));
1214
+ const isCurrent = i === (d.currentStepIndex ?? d.results.length);
1215
+ const stepIcon = isFailed
1216
+ ? theme.fg("error", "failed")
1217
+ : isEmptyWithoutTarget
1218
+ ? theme.fg("warning", "warning")
1219
+ : isComplete
1220
+ ? theme.fg("success", "done")
1221
+ : isCurrent && hasRunning
1222
+ ? theme.fg("warning", "running")
1223
+ : theme.fg("dim", "pending");
1224
+ return `${stepIcon} ${agent}`;
1225
+ })
1226
+ .join(theme.fg("dim", " → "))
1227
+ : null;
1228
+
1229
+ const w = getTermWidth() - 4;
1230
+ const fit = (text: string) => expanded ? text : truncLine(text, w);
1231
+ const c = new Container();
1232
+ c.addChild(
1233
+ new Text(
1234
+ fit(`${icon} ${theme.fg("toolTitle", theme.bold(modeLabel))}${contextBadge} · ${multiLabel.headerLabel}${summaryStr}`),
1235
+ 0,
1236
+ 0,
1237
+ ),
1238
+ );
1239
+ if (chainVis) {
1240
+ c.addChild(new Text(fit(` ${chainVis}`), 0, 0));
1241
+ }
1242
+
1243
+ const useResultsDirectly = multiLabel.hasParallelInChain || !d.chainAgents?.length;
1244
+ const displayStart = multiLabel.showActiveGroupOnly ? multiLabel.groupStartIndex : 0;
1245
+ const displayEnd = multiLabel.showActiveGroupOnly ? multiLabel.groupEndIndex : (useResultsDirectly ? d.results.length : d.chainAgents!.length);
1246
+
1247
+ c.addChild(new Spacer(1));
1248
+
1249
+ for (let i = displayStart; i < displayEnd; i++) {
1250
+ const r = d.results[i];
1251
+ const rowNumber = multiLabel.showActiveGroupOnly ? (i - multiLabel.groupStartIndex + 1) : (i + 1);
1252
+ const agentName = useResultsDirectly
1253
+ ? (r?.agent || `step-${rowNumber}`)
1254
+ : (d.chainAgents![i] || r?.agent || `step-${rowNumber}`);
1255
+
1256
+ if (!r) {
1257
+ c.addChild(new Text(fit(theme.fg("dim", ` ${itemTitle} ${rowNumber}: ${agentName}`)), 0, 0));
1258
+ c.addChild(new Text(theme.fg("dim", ` status: pending`), 0, 0));
1259
+ c.addChild(new Spacer(1));
1260
+ continue;
1261
+ }
1262
+
1263
+ const progressFromArray = d.progress?.find((p) => p.index === i)
1264
+ || d.progress?.find((p) => p.agent === r.agent && p.status === "running");
1265
+ const rProg = r.progress || progressFromArray || r.progressSummary;
1266
+ const rRunning = rProg?.status === "running";
1267
+ const stepNumber = typeof rProg?.index === "number" ? rProg.index + 1 : i + 1;
1268
+
1269
+ const resultOutput = getSingleResultOutput(r);
1270
+ const statusIcon = rRunning
1271
+ ? theme.fg("warning", "running")
1272
+ : r.exitCode !== 0
1273
+ ? theme.fg("error", "failed")
1274
+ : hasEmptyTextOutputWithoutOutputTarget(r.task, resultOutput)
1275
+ ? theme.fg("warning", "warning")
1276
+ : theme.fg("success", "done");
1277
+ const stats = rProg ? ` | ${rProg.toolCount} tools, ${formatDuration(rProg.durationMs)}` : "";
1278
+ const modelDisplay = modelThinkingBadge(theme, r.model);
1279
+ const stepLabel = resultRowLabel(d, multiLabel, i, stepNumber);
1280
+ const stepHeader = rRunning
1281
+ ? `${statusIcon} ${stepLabel}: ${theme.bold(theme.fg("warning", r.agent))}${modelDisplay}${stats}`
1282
+ : `${statusIcon} ${stepLabel}: ${theme.bold(r.agent)}${modelDisplay}${stats}`;
1283
+ const toolCallLines = getToolCallLines(r, expanded);
1284
+ c.addChild(new Text(fit(stepHeader), 0, 0));
1285
+
1286
+ const taskMaxLen = Math.max(20, w - 12);
1287
+ const taskPreview = expanded || r.task.length <= taskMaxLen
1288
+ ? r.task
1289
+ : `${r.task.slice(0, taskMaxLen)}...`;
1290
+ c.addChild(new Text(fit(theme.fg("dim", ` task: ${taskPreview}`)), 0, 0));
1291
+
1292
+ const outputTarget = extractOutputTarget(r.task);
1293
+ if (outputTarget) {
1294
+ c.addChild(new Text(fit(theme.fg("dim", ` output: ${outputTarget}`)), 0, 0));
1295
+ }
1296
+
1297
+ if (r.skills?.length) {
1298
+ c.addChild(new Text(fit(theme.fg("dim", ` skills: ${r.skills.join(", ")}`)), 0, 0));
1299
+ }
1300
+ if (r.skillsWarning) {
1301
+ c.addChild(new Text(fit(theme.fg("warning", ` Warning: ${r.skillsWarning}`)), 0, 0));
1302
+ }
1303
+ if (r.attemptedModels && r.attemptedModels.length > 1) {
1304
+ c.addChild(new Text(fit(theme.fg("dim", ` fallbacks: ${r.attemptedModels.join(" → ")}`)), 0, 0));
1305
+ }
1306
+
1307
+ if (rRunning && rProg) {
1308
+ if (rProg.skills?.length) {
1309
+ c.addChild(new Text(fit(theme.fg("accent", ` skills: ${rProg.skills.join(", ")}`)), 0, 0));
1310
+ }
1311
+ const progressSnapshotNow = snapshotNowForProgress(rProg);
1312
+ const toolLine = formatCurrentToolLine(rProg, w, expanded, progressSnapshotNow);
1313
+ if (toolLine) {
1314
+ c.addChild(new Text(fit(theme.fg("warning", ` > ${toolLine}`)), 0, 0));
1315
+ }
1316
+ const liveStatusLine = buildLiveStatusLine(rProg, progressSnapshotNow);
1317
+ if (liveStatusLine) {
1318
+ c.addChild(new Text(fit(theme.fg("accent", ` ${liveStatusLine}`)), 0, 0));
1319
+ }
1320
+ c.addChild(new Text(fit(theme.fg("accent", " Press Ctrl+O for live detail")), 0, 0));
1321
+ if (r.artifactPaths) {
1322
+ c.addChild(new Text(fit(theme.fg("dim", ` artifacts: ${shortenPath(r.artifactPaths.outputPath)}`)), 0, 0));
1323
+ }
1324
+ if (rProg.recentTools?.length) {
1325
+ for (const t of rProg.recentTools.slice(-3)) {
1326
+ const maxArgsLen = Math.max(40, w - 30);
1327
+ const argsPreview = expanded || t.args.length <= maxArgsLen
1328
+ ? t.args
1329
+ : `${t.args.slice(0, maxArgsLen)}...`;
1330
+ c.addChild(new Text(fit(theme.fg("dim", ` ${t.tool}: ${argsPreview}`)), 0, 0));
1331
+ }
1332
+ }
1333
+ const recentLines = (rProg.recentOutput ?? []).slice(-5);
1334
+ for (const line of recentLines) {
1335
+ c.addChild(new Text(fit(theme.fg("dim", ` ${line}`)), 0, 0));
1336
+ }
1337
+ }
1338
+
1339
+ if (!rRunning && r.artifactPaths) {
1340
+ c.addChild(new Text(fit(theme.fg("dim", ` artifacts: ${shortenPath(r.artifactPaths.outputPath)}`)), 0, 0));
1341
+ }
1342
+
1343
+ if (expanded && !rRunning) {
1344
+ for (const line of toolCallLines) {
1345
+ c.addChild(new Text(fit(theme.fg("muted", ` ${line}`)), 0, 0));
1346
+ }
1347
+ if (toolCallLines.length) c.addChild(new Spacer(1));
1348
+ }
1349
+
1350
+ c.addChild(new Spacer(1));
1351
+ }
1352
+
1353
+ if (d.artifacts) {
1354
+ c.addChild(new Spacer(1));
1355
+ c.addChild(new Text(fit(theme.fg("dim", `Artifacts dir: ${shortenPath(d.artifacts.dir)}`)), 0, 0));
1356
+ }
1357
+ return c;
1358
+ }