@fitchmultz/pi-subagents 0.0.0-stage → 0.44.5

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