@draht/coding-agent 2026.10.4-3 → 2026.10.8-1

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 (246) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +32 -6
  3. package/dist/bundle/chunks/anthropic.js +1 -1
  4. package/dist/bundle/chunks/{chunk-JTLUBTZT.js → chunk-6QIFWMDF.js} +1 -1
  5. package/dist/bundle/chunks/{chunk-AFL4IDSI.js → chunk-AHGUOQPH.js} +2 -2
  6. package/dist/bundle/chunks/{chunk-VPYAVQLP.js → chunk-BOGY4QVF.js} +4 -4
  7. package/dist/bundle/chunks/chunk-GD2WVXWT.js +65 -0
  8. package/dist/bundle/chunks/{chunk-3HF756LN.js → chunk-KJJJSTND.js} +292 -153
  9. package/dist/bundle/chunks/chunk-PGJAHC3R.js +107 -0
  10. package/dist/bundle/chunks/chunk-QVFELZW2.js +312 -0
  11. package/dist/bundle/chunks/{chunk-V5HG6S2T.js → chunk-QVTLBSTH.js} +1 -1
  12. package/dist/bundle/chunks/{chunk-A4OX5CPR.js → chunk-U6LDZZGM.js} +1 -1
  13. package/dist/bundle/chunks/{chunk-XO3IECI5.js → chunk-VQOV5VFH.js} +1 -1
  14. package/dist/bundle/chunks/{cli-L74YA5GF.js → cli-O6OWCY7N.js} +1 -1
  15. package/dist/bundle/chunks/execute-4JU5WQXP.js +18 -0
  16. package/dist/bundle/chunks/google-antigravity.js +1 -1
  17. package/dist/bundle/chunks/google-gemini-cli.js +1 -1
  18. package/dist/bundle/chunks/openai-chatgpt.js +1 -1
  19. package/dist/bundle/chunks/openai-codex.js +1 -1
  20. package/dist/bundle/chunks/openrouter.js +1 -1
  21. package/dist/bundle/chunks/radius.js +1 -1
  22. package/dist/bundle/chunks/{runtime-RYUR6LRB.js → runtime-FIGQHW75.js} +1 -1
  23. package/dist/bundle/chunks/runtime-TJ3EAMJK.js +3 -0
  24. package/dist/bundle/chunks/{settings-manager-CHEMMAGQ.js → settings-manager-HC5RR36T.js} +1 -1
  25. package/dist/bundle/chunks/virtual-modules-UIGTRGHQ.js +2 -0
  26. package/dist/bundle/chunks/xai.js +1 -1
  27. package/dist/bundle/cli-runtime.js +1 -1
  28. package/dist/bundle/index.js +1 -1
  29. package/dist/bundle/rpc-entry.js +1 -1
  30. package/dist/cli/args.js +3 -3
  31. package/dist/cli/args.js.map +1 -1
  32. package/dist/core/builtins/duet.d.ts +5 -2
  33. package/dist/core/builtins/duet.d.ts.map +1 -1
  34. package/dist/core/builtins/duet.js +65 -27
  35. package/dist/core/builtins/duet.js.map +1 -1
  36. package/dist/core/builtins/index.d.ts.map +1 -1
  37. package/dist/core/builtins/index.js +2 -0
  38. package/dist/core/builtins/index.js.map +1 -1
  39. package/dist/core/builtins/polyphase.d.ts +15 -0
  40. package/dist/core/builtins/polyphase.d.ts.map +1 -0
  41. package/dist/core/builtins/polyphase.js +196 -0
  42. package/dist/core/builtins/polyphase.js.map +1 -0
  43. package/dist/core/builtins/subagent.d.ts +44 -34
  44. package/dist/core/builtins/subagent.d.ts.map +1 -1
  45. package/dist/core/builtins/subagent.js +827 -560
  46. package/dist/core/builtins/subagent.js.map +1 -1
  47. package/dist/core/extensions/types.d.ts +17 -2
  48. package/dist/core/extensions/types.d.ts.map +1 -1
  49. package/dist/core/extensions/types.js.map +1 -1
  50. package/dist/core/keybindings.d.ts +30 -0
  51. package/dist/core/keybindings.d.ts.map +1 -1
  52. package/dist/core/keybindings.js +24 -0
  53. package/dist/core/keybindings.js.map +1 -1
  54. package/dist/core/model-resolver.js +4 -4
  55. package/dist/core/model-resolver.js.map +1 -1
  56. package/dist/core/multi-agent/permission-gate.d.ts.map +1 -1
  57. package/dist/core/multi-agent/permission-gate.js +4 -1
  58. package/dist/core/multi-agent/permission-gate.js.map +1 -1
  59. package/dist/core/polyphase/approval.d.ts +35 -0
  60. package/dist/core/polyphase/approval.d.ts.map +1 -0
  61. package/dist/core/polyphase/approval.js +184 -0
  62. package/dist/core/polyphase/approval.js.map +1 -0
  63. package/dist/core/polyphase/child-events.d.ts +107 -0
  64. package/dist/core/polyphase/child-events.d.ts.map +1 -0
  65. package/dist/core/polyphase/child-events.js +862 -0
  66. package/dist/core/polyphase/child-events.js.map +1 -0
  67. package/dist/core/polyphase/child-process.d.ts +65 -0
  68. package/dist/core/polyphase/child-process.d.ts.map +1 -0
  69. package/dist/core/polyphase/child-process.js +361 -0
  70. package/dist/core/polyphase/child-process.js.map +1 -0
  71. package/dist/core/polyphase/commands.d.ts +44 -0
  72. package/dist/core/polyphase/commands.d.ts.map +1 -0
  73. package/dist/core/polyphase/commands.js +244 -0
  74. package/dist/core/polyphase/commands.js.map +1 -0
  75. package/dist/core/polyphase/details.d.ts +40 -0
  76. package/dist/core/polyphase/details.d.ts.map +1 -0
  77. package/dist/core/polyphase/details.js +538 -0
  78. package/dist/core/polyphase/details.js.map +1 -0
  79. package/dist/core/polyphase/emitter.d.ts +20 -0
  80. package/dist/core/polyphase/emitter.d.ts.map +1 -0
  81. package/dist/core/polyphase/emitter.js +90 -0
  82. package/dist/core/polyphase/emitter.js.map +1 -0
  83. package/dist/core/polyphase/jsonl-reader.d.ts +31 -0
  84. package/dist/core/polyphase/jsonl-reader.d.ts.map +1 -0
  85. package/dist/core/polyphase/jsonl-reader.js +161 -0
  86. package/dist/core/polyphase/jsonl-reader.js.map +1 -0
  87. package/dist/core/polyphase/keyword.d.ts +27 -0
  88. package/dist/core/polyphase/keyword.d.ts.map +1 -0
  89. package/dist/core/polyphase/keyword.js +97 -0
  90. package/dist/core/polyphase/keyword.js.map +1 -0
  91. package/dist/core/polyphase/limiter.d.ts +38 -0
  92. package/dist/core/polyphase/limiter.d.ts.map +1 -0
  93. package/dist/core/polyphase/limiter.js +129 -0
  94. package/dist/core/polyphase/limiter.js.map +1 -0
  95. package/dist/core/polyphase/model-selection.d.ts +35 -0
  96. package/dist/core/polyphase/model-selection.d.ts.map +1 -0
  97. package/dist/core/polyphase/model-selection.js +72 -0
  98. package/dist/core/polyphase/model-selection.js.map +1 -0
  99. package/dist/core/polyphase/render/format.d.ts +52 -0
  100. package/dist/core/polyphase/render/format.d.ts.map +1 -0
  101. package/dist/core/polyphase/render/format.js +169 -0
  102. package/dist/core/polyphase/render/format.js.map +1 -0
  103. package/dist/core/polyphase/render/renderers.d.ts +63 -0
  104. package/dist/core/polyphase/render/renderers.d.ts.map +1 -0
  105. package/dist/core/polyphase/render/renderers.js +277 -0
  106. package/dist/core/polyphase/render/renderers.js.map +1 -0
  107. package/dist/core/polyphase/render/run-block.d.ts +45 -0
  108. package/dist/core/polyphase/render/run-block.d.ts.map +1 -0
  109. package/dist/core/polyphase/render/run-block.js +843 -0
  110. package/dist/core/polyphase/render/run-block.js.map +1 -0
  111. package/dist/core/polyphase/render/snapshot.d.ts +106 -0
  112. package/dist/core/polyphase/render/snapshot.d.ts.map +1 -0
  113. package/dist/core/polyphase/render/snapshot.js +292 -0
  114. package/dist/core/polyphase/render/snapshot.js.map +1 -0
  115. package/dist/core/polyphase/result-text.d.ts +41 -0
  116. package/dist/core/polyphase/result-text.d.ts.map +1 -0
  117. package/dist/core/polyphase/result-text.js +326 -0
  118. package/dist/core/polyphase/result-text.js.map +1 -0
  119. package/dist/core/polyphase/session.d.ts +33 -0
  120. package/dist/core/polyphase/session.d.ts.map +1 -0
  121. package/dist/core/polyphase/session.js +106 -0
  122. package/dist/core/polyphase/session.js.map +1 -0
  123. package/dist/core/polyphase/settings.d.ts +18 -0
  124. package/dist/core/polyphase/settings.d.ts.map +1 -0
  125. package/dist/core/polyphase/settings.js +93 -0
  126. package/dist/core/polyphase/settings.js.map +1 -0
  127. package/dist/core/polyphase/store.d.ts +197 -0
  128. package/dist/core/polyphase/store.d.ts.map +1 -0
  129. package/dist/core/polyphase/store.js +558 -0
  130. package/dist/core/polyphase/store.js.map +1 -0
  131. package/dist/core/polyphase/types.d.ts +371 -0
  132. package/dist/core/polyphase/types.d.ts.map +1 -0
  133. package/dist/core/polyphase/types.js +36 -0
  134. package/dist/core/polyphase/types.js.map +1 -0
  135. package/dist/core/polyphase/ui/controller.d.ts +28 -0
  136. package/dist/core/polyphase/ui/controller.d.ts.map +1 -0
  137. package/dist/core/polyphase/ui/controller.js +150 -0
  138. package/dist/core/polyphase/ui/controller.js.map +1 -0
  139. package/dist/core/polyphase/ui/dock.d.ts +27 -0
  140. package/dist/core/polyphase/ui/dock.d.ts.map +1 -0
  141. package/dist/core/polyphase/ui/dock.js +121 -0
  142. package/dist/core/polyphase/ui/dock.js.map +1 -0
  143. package/dist/core/polyphase/ui/inspector.d.ts +117 -0
  144. package/dist/core/polyphase/ui/inspector.d.ts.map +1 -0
  145. package/dist/core/polyphase/ui/inspector.js +1016 -0
  146. package/dist/core/polyphase/ui/inspector.js.map +1 -0
  147. package/dist/core/polyphase/ui/transcript-layout.d.ts +55 -0
  148. package/dist/core/polyphase/ui/transcript-layout.d.ts.map +1 -0
  149. package/dist/core/polyphase/ui/transcript-layout.js +210 -0
  150. package/dist/core/polyphase/ui/transcript-layout.js.map +1 -0
  151. package/dist/core/polyphase/update-pump.d.ts +18 -0
  152. package/dist/core/polyphase/update-pump.d.ts.map +1 -0
  153. package/dist/core/polyphase/update-pump.js +132 -0
  154. package/dist/core/polyphase/update-pump.js.map +1 -0
  155. package/dist/core/polyphase/workflow/agent-host.d.ts +26 -0
  156. package/dist/core/polyphase/workflow/agent-host.d.ts.map +1 -0
  157. package/dist/core/polyphase/workflow/agent-host.js +187 -0
  158. package/dist/core/polyphase/workflow/agent-host.js.map +1 -0
  159. package/dist/core/polyphase/workflow/guidance.d.ts +19 -0
  160. package/dist/core/polyphase/workflow/guidance.d.ts.map +1 -0
  161. package/dist/core/polyphase/workflow/guidance.js +119 -0
  162. package/dist/core/polyphase/workflow/guidance.js.map +1 -0
  163. package/dist/core/polyphase/workflow/launch.d.ts +41 -0
  164. package/dist/core/polyphase/workflow/launch.d.ts.map +1 -0
  165. package/dist/core/polyphase/workflow/launch.js +173 -0
  166. package/dist/core/polyphase/workflow/launch.js.map +1 -0
  167. package/dist/core/polyphase/workflow/meta.d.ts +43 -0
  168. package/dist/core/polyphase/workflow/meta.d.ts.map +1 -0
  169. package/dist/core/polyphase/workflow/meta.js +529 -0
  170. package/dist/core/polyphase/workflow/meta.js.map +1 -0
  171. package/dist/core/polyphase/workflow/prelude.d.ts +24 -0
  172. package/dist/core/polyphase/workflow/prelude.d.ts.map +1 -0
  173. package/dist/core/polyphase/workflow/prelude.js +93 -0
  174. package/dist/core/polyphase/workflow/prelude.js.map +1 -0
  175. package/dist/core/polyphase/workflow/runtime.d.ts +79 -0
  176. package/dist/core/polyphase/workflow/runtime.d.ts.map +1 -0
  177. package/dist/core/polyphase/workflow/runtime.js +217 -0
  178. package/dist/core/polyphase/workflow/runtime.js.map +1 -0
  179. package/dist/core/polyphase/workflow/runtime.lazy.d.ts +3 -0
  180. package/dist/core/polyphase/workflow/runtime.lazy.d.ts.map +1 -0
  181. package/dist/core/polyphase/workflow/runtime.lazy.js +3 -0
  182. package/dist/core/polyphase/workflow/runtime.lazy.js.map +1 -0
  183. package/dist/core/polyphase/workflow/saved.d.ts +55 -0
  184. package/dist/core/polyphase/workflow/saved.d.ts.map +1 -0
  185. package/dist/core/polyphase/workflow/saved.js +299 -0
  186. package/dist/core/polyphase/workflow/saved.js.map +1 -0
  187. package/dist/core/polyphase/workflow/structured-output.d.ts +22 -0
  188. package/dist/core/polyphase/workflow/structured-output.d.ts.map +1 -0
  189. package/dist/core/polyphase/workflow/structured-output.js +66 -0
  190. package/dist/core/polyphase/workflow/structured-output.js.map +1 -0
  191. package/dist/core/polyphase/workflow/tool.d.ts +24 -0
  192. package/dist/core/polyphase/workflow/tool.d.ts.map +1 -0
  193. package/dist/core/polyphase/workflow/tool.js +139 -0
  194. package/dist/core/polyphase/workflow/tool.js.map +1 -0
  195. package/dist/core/sdk.d.ts +1 -1
  196. package/dist/core/sdk.js +1 -1
  197. package/dist/core/sdk.js.map +1 -1
  198. package/dist/core/settings-manager.d.ts +27 -0
  199. package/dist/core/settings-manager.d.ts.map +1 -1
  200. package/dist/core/settings-manager.js.map +1 -1
  201. package/dist/core/socket-server/permission-relay.js +4 -0
  202. package/dist/core/socket-server/permission-relay.js.map +1 -1
  203. package/dist/core/trust-manager.d.ts +3 -2
  204. package/dist/core/trust-manager.d.ts.map +1 -1
  205. package/dist/core/trust-manager.js +5 -2
  206. package/dist/core/trust-manager.js.map +1 -1
  207. package/dist/experimental/micro/runtime.js +1 -1
  208. package/dist/experimental/micro/runtime.js.map +1 -1
  209. package/dist/modes/interactive/components/keybinding-hints.d.ts +7 -0
  210. package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -1
  211. package/dist/modes/interactive/components/keybinding-hints.js +18 -0
  212. package/dist/modes/interactive/components/keybinding-hints.js.map +1 -1
  213. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  214. package/dist/modes/interactive/interactive-mode.js +11 -2
  215. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  216. package/dist/modes/rpc/rpc-types.d.ts +9 -0
  217. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  218. package/dist/modes/rpc/rpc-types.js.map +1 -1
  219. package/dist/prompts/commands/orchestrate.md +1 -1
  220. package/dist/utils/shell.d.ts.map +1 -1
  221. package/dist/utils/shell.js +7 -1
  222. package/dist/utils/shell.js.map +1 -1
  223. package/docs/docs.json +4 -0
  224. package/docs/duet.md +3 -3
  225. package/docs/environment-variables.md +11 -0
  226. package/docs/json.md +7 -0
  227. package/docs/keybindings.md +20 -0
  228. package/docs/polyphase.md +391 -0
  229. package/docs/rpc-commands.md +9 -9
  230. package/docs/settings.md +12 -0
  231. package/docs/virtual-models.md +3 -3
  232. package/examples/extensions/preset.ts +3 -3
  233. package/examples/extensions/subagent/README.md +1 -1
  234. package/examples/extensions/subagent/agents/planner.md +1 -1
  235. package/examples/extensions/subagent/agents/reviewer.md +1 -1
  236. package/examples/extensions/subagent/agents/scout.md +1 -1
  237. package/examples/extensions/subagent/agents/worker.md +1 -1
  238. package/examples/extensions/summarize.ts +3 -3
  239. package/examples/sdk/02-custom-model.ts +1 -1
  240. package/examples/sdk/12-full-control.ts +1 -1
  241. package/examples/sdk/README.md +1 -1
  242. package/package.json +10 -10
  243. package/prompts/commands/orchestrate.md +1 -1
  244. package/dist/bundle/chunks/chunk-SPZUYYP5.js +0 -170
  245. package/dist/bundle/chunks/execute-EMOVNTD6.js +0 -328
  246. package/dist/bundle/chunks/virtual-modules-OC56AAJ7.js +0 -2
@@ -0,0 +1,538 @@
1
+ import { combineUsage } from "../usage-totals.js";
2
+ import { billableTokens, extractStatusLine } from "./child-events.js";
3
+ import { capHeadTail } from "./result-text.js";
4
+ import { FINAL_DETAILS_MAX_BYTES, PARTIAL_DETAILS_MAX_BYTES, SUBAGENT_TOOL_NAME, WORKFLOW_TOOL_NAME } from "./types.js";
5
+ /**
6
+ * Builds the bounded, JSON-compatible `PolyphaseDetails` persisted as a tool result (D2), and
7
+ * reads them back out of the session log for the inspector's archived-run view.
8
+ */
9
+ /** Reserve for capHeadTail's "[... N characters omitted ...]" marker, so the final `output` text
10
+ * still fits `outputMax` even though the marker is appended after the head/tail split. */
11
+ const OUTPUT_MARKER_RESERVE = 48;
12
+ const LEVEL_PLANS = [
13
+ {
14
+ level: 0,
15
+ outputMax: 1200,
16
+ outputHeadRatio: 1 / 3,
17
+ taskMax: 200,
18
+ errorMax: 400,
19
+ nowMax: 160,
20
+ logLines: 20,
21
+ logLineMax: 200,
22
+ resultPreviewMax: 1200,
23
+ includeTools: true,
24
+ phaseDetailMax: 200,
25
+ titleMax: 200,
26
+ workflowDescMax: 2000,
27
+ workflowArgsMax: 2000,
28
+ workflowPathMax: 300,
29
+ maxPhases: 100,
30
+ phaseTitleMax: 120,
31
+ phaseModelMax: 60,
32
+ runErrorMax: 2000,
33
+ agentLabelMax: 200,
34
+ agentTypeMax: 100,
35
+ agentModelMax: 200,
36
+ },
37
+ {
38
+ level: 1,
39
+ outputMax: 300,
40
+ outputHeadRatio: 1 / 3,
41
+ taskMax: 200,
42
+ errorMax: 400,
43
+ nowMax: 160,
44
+ logLines: 5,
45
+ logLineMax: 200,
46
+ resultPreviewMax: 300,
47
+ includeTools: false,
48
+ phaseDetailMax: 160,
49
+ titleMax: 200,
50
+ workflowDescMax: 1000,
51
+ workflowArgsMax: 1000,
52
+ workflowPathMax: 300,
53
+ maxPhases: 60,
54
+ phaseTitleMax: 100,
55
+ phaseModelMax: 50,
56
+ runErrorMax: 1000,
57
+ agentLabelMax: 200,
58
+ agentTypeMax: 100,
59
+ agentModelMax: 150,
60
+ },
61
+ {
62
+ level: 2,
63
+ outputMax: 0,
64
+ outputHeadRatio: 1 / 3,
65
+ taskMax: 80,
66
+ errorMax: 160,
67
+ nowMax: 0,
68
+ logLines: 0,
69
+ logLineMax: 200,
70
+ resultPreviewMax: 120,
71
+ includeTools: false,
72
+ phaseDetailMax: 120,
73
+ titleMax: 160,
74
+ workflowDescMax: 400,
75
+ workflowArgsMax: 400,
76
+ workflowPathMax: 200,
77
+ maxPhases: 40,
78
+ phaseTitleMax: 80,
79
+ phaseModelMax: 40,
80
+ runErrorMax: 400,
81
+ agentLabelMax: 160,
82
+ agentTypeMax: 80,
83
+ agentModelMax: 100,
84
+ },
85
+ {
86
+ level: 3,
87
+ outputMax: 0,
88
+ outputHeadRatio: 1 / 3,
89
+ taskMax: 80,
90
+ errorMax: 160,
91
+ nowMax: 0,
92
+ logLines: 0,
93
+ logLineMax: 200,
94
+ resultPreviewMax: 120,
95
+ includeTools: false,
96
+ phaseDetailMax: 80,
97
+ titleMax: 120,
98
+ workflowDescMax: 200,
99
+ workflowArgsMax: 200,
100
+ workflowPathMax: 160,
101
+ maxPhases: 30,
102
+ phaseTitleMax: 60,
103
+ phaseModelMax: 30,
104
+ runErrorMax: 200,
105
+ agentLabelMax: 120,
106
+ agentTypeMax: 60,
107
+ agentModelMax: 80,
108
+ },
109
+ {
110
+ level: 4,
111
+ outputMax: 0,
112
+ outputHeadRatio: 1 / 3,
113
+ taskMax: 40,
114
+ errorMax: 160,
115
+ nowMax: 0,
116
+ logLines: 0,
117
+ logLineMax: 200,
118
+ resultPreviewMax: 120,
119
+ includeTools: false,
120
+ titleMax: 80,
121
+ workflowDescMax: 80,
122
+ workflowArgsMax: 80,
123
+ workflowPathMax: 80,
124
+ maxPhases: 20,
125
+ phaseTitleMax: 40,
126
+ phaseModelMax: 20,
127
+ runErrorMax: 120,
128
+ agentLabelMax: 40,
129
+ agentTypeMax: 40,
130
+ agentModelMax: 40,
131
+ },
132
+ ];
133
+ function truncateChars(text, maxChars) {
134
+ if (maxChars <= 0)
135
+ return "";
136
+ return text.length <= maxChars ? text : `${text.slice(0, Math.max(0, maxChars - 1))}…`;
137
+ }
138
+ function firstLine(text) {
139
+ const idx = text.indexOf("\n");
140
+ return idx === -1 ? text : text.slice(0, idx);
141
+ }
142
+ function byteLength(details) {
143
+ return Buffer.byteLength(JSON.stringify(details), "utf8");
144
+ }
145
+ const IMPORTANT_STATUSES = ["failed", "running", "queued", "starting"];
146
+ /** Every important-status agent, plus the first/last 20 by index, capped at `cap` total. */
147
+ function selectAgentsBounded(allAgents, cap) {
148
+ const important = allAgents.filter((a) => IMPORTANT_STATUSES.includes(a.status));
149
+ const firstN = allAgents.slice(0, 20);
150
+ const lastN = allAgents.slice(Math.max(0, allAgents.length - 20));
151
+ const seen = new Set();
152
+ const selected = [];
153
+ for (const agent of [...important, ...firstN, ...lastN]) {
154
+ if (seen.has(agent.index))
155
+ continue;
156
+ seen.add(agent.index);
157
+ selected.push(agent);
158
+ if (selected.length >= cap)
159
+ break;
160
+ }
161
+ return selected.sort((a, b) => a.index - b.index);
162
+ }
163
+ /** Failed agents first, then running, then the rest in index order, capped at `cap`. */
164
+ function selectAgentsMinimal(allAgents, cap) {
165
+ const failed = allAgents.filter((a) => a.status === "failed");
166
+ const running = allAgents.filter((a) => a.status === "running");
167
+ const rest = allAgents.filter((a) => a.status !== "failed" && a.status !== "running");
168
+ return [...failed, ...running, ...rest].slice(0, cap).sort((a, b) => a.index - b.index);
169
+ }
170
+ /** Same priority order as {@link selectAgentsMinimal} (failed, then running, then the rest),
171
+ * for `forceFit`, which must drop already-selected `AgentDetails` in that same order. */
172
+ function rankAgentDetailsByPriority(agents) {
173
+ const failed = agents.filter((a) => a.status === "failed");
174
+ const running = agents.filter((a) => a.status === "running");
175
+ const rest = agents.filter((a) => a.status !== "failed" && a.status !== "running");
176
+ return [...failed, ...running, ...rest];
177
+ }
178
+ function selectAgents(allAgents, plan) {
179
+ if (plan.level < 3)
180
+ return { agents: allAgents };
181
+ const selected = plan.level === 4 ? selectAgentsMinimal(allAgents, 20) : selectAgentsBounded(allAgents, 60);
182
+ if (selected.length === allAgents.length)
183
+ return { agents: selected };
184
+ const selectedIndices = new Set(selected.map((a) => a.index));
185
+ const omitted = allAgents.filter((a) => !selectedIndices.has(a.index));
186
+ const byStatus = {};
187
+ for (const agent of omitted)
188
+ byStatus[agent.status] = (byStatus[agent.status] ?? 0) + 1;
189
+ return { agents: selected, agentsOmitted: { count: omitted.length, byStatus } };
190
+ }
191
+ function buildTotals(allAgents) {
192
+ const byStatus = {};
193
+ let tokens = 0;
194
+ let cost = 0;
195
+ let blockedToolCalls = 0;
196
+ for (const agent of allAgents) {
197
+ byStatus[agent.status] = (byStatus[agent.status] ?? 0) + 1;
198
+ tokens += billableTokens(agent.state.usage) + billableTokens(agent.state.liveUsage);
199
+ cost += (agent.state.usage?.cost.total ?? 0) + (agent.state.liveUsage?.cost.total ?? 0);
200
+ blockedToolCalls += agent.state.blockedCount;
201
+ }
202
+ return { agents: allAgents.length, tokens, cost, byStatus, blockedToolCalls };
203
+ }
204
+ function modelText(agent) {
205
+ const model = agent.state.model;
206
+ if (model.provider && model.id)
207
+ return `${model.provider}/${model.id}`;
208
+ return model.requested;
209
+ }
210
+ /** `extractStatusLine` scans the whole output with a global regex; computing it once per agent
211
+ * (instead of once per agent per degradation level) keeps buildDetails O(agents), not O(agents * levels). */
212
+ function computeStatusLines(agents) {
213
+ const lines = new Map();
214
+ for (const agent of agents) {
215
+ const outputText = agent.result?.output ?? agent.state.finalText;
216
+ lines.set(agent.index, outputText ? extractStatusLine(outputText) : undefined);
217
+ }
218
+ return lines;
219
+ }
220
+ function buildAgentDetails(agent, plan, ctx, statusLines) {
221
+ const details = {
222
+ i: agent.index,
223
+ label: truncateChars(agent.label, plan.agentLabelMax),
224
+ agent: truncateChars(agent.agentType, plan.agentTypeMax),
225
+ status: agent.status,
226
+ task: truncateChars(agent.task, plan.taskMax),
227
+ };
228
+ const model = modelText(agent);
229
+ if (model !== undefined)
230
+ details.model = truncateChars(model, plan.agentModelMax);
231
+ // L4 keeps only i, label, agent, status, model, task (§9.2); everything below is dropped.
232
+ if (plan.level === 4)
233
+ return details;
234
+ if (agent.phase !== undefined)
235
+ details.phase = agent.phase;
236
+ if (agent.step !== undefined)
237
+ details.step = agent.step;
238
+ if (agent.cancelReason !== undefined)
239
+ details.cancelReason = agent.cancelReason;
240
+ if (agent.state.model.thinkingLevel !== undefined)
241
+ details.thinking = agent.state.model.thinkingLevel;
242
+ details.modelSource = agent.state.model.source;
243
+ details.modelConfirmed = agent.state.model.confirmed;
244
+ if (agent.state.startedAt !== undefined)
245
+ details.startedAt = agent.state.startedAt;
246
+ if (agent.state.endedAt !== undefined)
247
+ details.endedAt = agent.state.endedAt;
248
+ if (agent.state.turns > 0)
249
+ details.turns = agent.state.turns;
250
+ if (agent.state.toolCalls > 0)
251
+ details.toolCalls = agent.state.toolCalls;
252
+ if (plan.includeTools && Object.keys(agent.state.toolCounts).length > 0) {
253
+ details.tools = { ...agent.state.toolCounts };
254
+ }
255
+ const tokens = billableTokens(agent.state.usage) + billableTokens(agent.state.liveUsage);
256
+ if (tokens > 0)
257
+ details.tokens = tokens;
258
+ const cost = (agent.state.usage?.cost.total ?? 0) + (agent.state.liveUsage?.cost.total ?? 0);
259
+ if (cost > 0)
260
+ details.cost = cost;
261
+ if (agent.state.blockedCount > 0) {
262
+ details.blocked = agent.state.blockedCount;
263
+ const first = agent.state.blocked[0];
264
+ if (first)
265
+ details.blockedSample = `${first.toolName}: ${first.summary}`;
266
+ }
267
+ const outputText = agent.result?.output ?? agent.state.finalText;
268
+ if (outputText) {
269
+ const statusLine = statusLines.get(agent.index);
270
+ if (statusLine !== undefined)
271
+ details.statusLine = truncateChars(statusLine, 120);
272
+ if (plan.outputMax > 0) {
273
+ const budget = Math.max(1, plan.outputMax - OUTPUT_MARKER_RESERVE);
274
+ const capped = capHeadTail(outputText, budget, { headRatio: plan.outputHeadRatio, keepStatusLine: false });
275
+ details.output = truncateChars(capped.text, plan.outputMax);
276
+ }
277
+ }
278
+ if (!ctx.final && plan.nowMax > 0 && agent.state.nowLine) {
279
+ details.now = truncateChars(agent.state.nowLine, plan.nowMax);
280
+ }
281
+ const errorSource = agent.result?.stderr || agent.state.errorMessage;
282
+ if (errorSource)
283
+ details.error = truncateChars(firstLine(errorSource), plan.errorMax);
284
+ if (agent.result?.merge !== undefined)
285
+ details.merge = agent.result.merge.success ? "ok" : "failed";
286
+ return details;
287
+ }
288
+ function buildPhaseDetails(phase, plan) {
289
+ const details = { title: truncateChars(phase.title, plan.phaseTitleMax) };
290
+ if (plan.phaseDetailMax !== undefined && phase.detail !== undefined) {
291
+ details.detail = truncateChars(phase.detail, plan.phaseDetailMax);
292
+ }
293
+ if (phase.model !== undefined)
294
+ details.model = truncateChars(phase.model, plan.phaseModelMax);
295
+ if (phase.dynamic)
296
+ details.dynamic = phase.dynamic;
297
+ return details;
298
+ }
299
+ function buildLog(run, plan) {
300
+ if (plan.logLines <= 0)
301
+ return { omitted: run.logDropped + run.log.length };
302
+ const kept = run.log.slice(-plan.logLines);
303
+ return {
304
+ lines: kept.map((line) => truncateChars(line.text, plan.logLineMax)),
305
+ omitted: run.logDropped + (run.log.length - kept.length),
306
+ };
307
+ }
308
+ function buildAtLevel(run, plan, ctx, statusLines) {
309
+ const { agents, agentsOmitted } = selectAgents(run.agents, plan);
310
+ const log = buildLog(run, plan);
311
+ const details = {
312
+ v: 1,
313
+ runId: run.id,
314
+ kind: run.kind,
315
+ origin: run.origin,
316
+ title: truncateChars(run.title, plan.titleMax),
317
+ status: run.status,
318
+ startedAt: run.startedAt,
319
+ agents: agents.map((agent) => buildAgentDetails(agent, plan, ctx, statusLines)),
320
+ totals: buildTotals(run.agents),
321
+ degraded: plan.level,
322
+ };
323
+ if (run.mode !== undefined)
324
+ details.mode = run.mode;
325
+ if (run.endedAt !== undefined)
326
+ details.endedAt = run.endedAt;
327
+ if (run.workflow !== undefined) {
328
+ details.workflow = {
329
+ name: run.workflow.name,
330
+ description: truncateChars(run.workflow.description, plan.workflowDescMax),
331
+ source: run.workflow.source,
332
+ ...(run.workflow.path !== undefined ? { path: truncateChars(run.workflow.path, plan.workflowPathMax) } : {}),
333
+ args: truncateChars(run.workflow.args, plan.workflowArgsMax),
334
+ };
335
+ }
336
+ if (run.phases.length > 0) {
337
+ details.phases = run.phases.slice(0, plan.maxPhases).map((phase) => buildPhaseDetails(phase, plan));
338
+ }
339
+ if (run.currentPhase !== undefined)
340
+ details.currentPhase = run.currentPhase;
341
+ if (agentsOmitted !== undefined)
342
+ details.agentsOmitted = agentsOmitted;
343
+ if (log.lines !== undefined)
344
+ details.log = log.lines;
345
+ if (log.omitted > 0)
346
+ details.logOmitted = log.omitted;
347
+ if (run.budget.totalTokens !== null) {
348
+ details.budget = {
349
+ totalTokens: run.budget.totalTokens,
350
+ spentTokens: run.budget.spentTokens,
351
+ exhausted: run.budget.exhausted,
352
+ };
353
+ }
354
+ if (run.resultPreview !== undefined) {
355
+ details.resultPreview = truncateChars(capHeadTail(run.resultPreview, plan.resultPreviewMax).text, plan.resultPreviewMax);
356
+ }
357
+ if (run.error !== undefined)
358
+ details.error = truncateChars(run.error, plan.runErrorMax);
359
+ return details;
360
+ }
361
+ /**
362
+ * Last-resort fit for L4 results that still exceed `maxBytes` (e.g. many agents each carrying a
363
+ * close-to-the-cap label/agent/model string). Strips optional fields, then drops agents in
364
+ * reverse priority order (the rest, then running, then failed last; §9.2 finding: dropping by
365
+ * index order instead could discard the one failed agent a user most needs to see), until the
366
+ * result fits. Always terminates: with zero agents and no optional fields, the remaining scalar
367
+ * fields and `totals` are far below either byte budget.
368
+ */
369
+ function forceFit(details, maxBytes) {
370
+ let d = details;
371
+ if (byteLength(d) <= maxBytes)
372
+ return d;
373
+ if (d.workflow && (d.workflow.description.length > 0 || (d.workflow.args?.length ?? 0) > 0)) {
374
+ d = { ...d, workflow: { ...d.workflow, description: "", args: "" } };
375
+ if (byteLength(d) <= maxBytes)
376
+ return d;
377
+ }
378
+ if (d.phases !== undefined) {
379
+ d = { ...d, phases: undefined };
380
+ if (byteLength(d) <= maxBytes)
381
+ return d;
382
+ }
383
+ if (d.error !== undefined) {
384
+ d = { ...d, error: undefined };
385
+ if (byteLength(d) <= maxBytes)
386
+ return d;
387
+ }
388
+ const originalAgents = d.agents;
389
+ const priorOmitted = d.agentsOmitted;
390
+ // Rank by the same priority selectAgentsMinimal uses (failed, then running, then the rest),
391
+ // so dropping from the low-priority end here never sacrifices a failed or running agent
392
+ // before every less important one is already gone.
393
+ const ranked = rankAgentDetailsByPriority(originalAgents);
394
+ for (let cap = originalAgents.length - 1; cap >= 0; cap--) {
395
+ const keptIds = new Set(ranked.slice(0, cap).map((a) => a.i));
396
+ const kept = originalAgents.filter((a) => keptIds.has(a.i));
397
+ const dropped = originalAgents.filter((a) => !keptIds.has(a.i));
398
+ const byStatus = { ...priorOmitted?.byStatus };
399
+ for (const agent of dropped)
400
+ byStatus[agent.status] = (byStatus[agent.status] ?? 0) + 1;
401
+ const candidate = {
402
+ ...d,
403
+ agents: kept,
404
+ agentsOmitted: { count: (priorOmitted?.count ?? 0) + dropped.length, byStatus },
405
+ };
406
+ if (byteLength(candidate) <= maxBytes || cap === 0)
407
+ return candidate;
408
+ }
409
+ return d;
410
+ }
411
+ /**
412
+ * Degrades `run` through L0-L4 (§9.2) until the JSON-stringified result fits `maxBytes`
413
+ * (`FINAL_DETAILS_MAX_BYTES` or `PARTIAL_DETAILS_MAX_BYTES` by default). L4 always fits: per-level
414
+ * caps keep run-level strings and phases bounded at every level, and a final {@link forceFit} pass
415
+ * strips fields and agents if an L4 result (e.g. hundreds of near-cap agents) still does not.
416
+ *
417
+ * `options.now` is accepted for signature symmetry with the rest of the polyphase API (store.ts's
418
+ * injectable clock) but is not currently read: every persisted field is an absolute timestamp, and
419
+ * no derived field here depends on "the current time".
420
+ */
421
+ export function buildDetails(run, options) {
422
+ const maxBytes = options.maxBytes ?? (options.final ? FINAL_DETAILS_MAX_BYTES : PARTIAL_DETAILS_MAX_BYTES);
423
+ const statusLines = computeStatusLines(run.agents);
424
+ let result;
425
+ for (const plan of LEVEL_PLANS) {
426
+ result = buildAtLevel(run, plan, options, statusLines);
427
+ if (plan.level === 4 || byteLength(result) <= maxBytes)
428
+ break;
429
+ }
430
+ const built = result;
431
+ return byteLength(built) <= maxBytes ? built : forceFit(built, maxBytes);
432
+ }
433
+ function isAgentDetailsLike(value) {
434
+ if (typeof value !== "object" || value === null)
435
+ return false;
436
+ const a = value;
437
+ return (typeof a.i === "number" &&
438
+ typeof a.label === "string" &&
439
+ typeof a.agent === "string" &&
440
+ typeof a.status === "string" &&
441
+ typeof a.task === "string");
442
+ }
443
+ function isTotalsLike(value) {
444
+ if (typeof value !== "object" || value === null)
445
+ return false;
446
+ const t = value;
447
+ return typeof t.agents === "number" && typeof t.tokens === "number" && typeof t.cost === "number";
448
+ }
449
+ /** Structural guard: `v === 1`, the required top-level fields, every agent's required fields, and
450
+ * the shape of the optional array/object fields (`phases`, `log`, `totals`). Persisted details can
451
+ * be hand-edited or come from an older schema version, so this is read defensively, not just at the
452
+ * top level: {@link collectArchivedRuns} hands these straight to renderers as `PolyphaseDetails`. */
453
+ export function isPolyphaseDetails(value) {
454
+ if (typeof value !== "object" || value === null)
455
+ return false;
456
+ const d = value;
457
+ if (d.v !== 1)
458
+ return false;
459
+ if (typeof d.runId !== "string")
460
+ return false;
461
+ if (typeof d.kind !== "string")
462
+ return false;
463
+ if (typeof d.origin !== "string")
464
+ return false;
465
+ if (typeof d.title !== "string")
466
+ return false;
467
+ if (typeof d.status !== "string")
468
+ return false;
469
+ if (typeof d.startedAt !== "number")
470
+ return false;
471
+ if (!Array.isArray(d.agents))
472
+ return false;
473
+ if (d.phases !== undefined && !Array.isArray(d.phases))
474
+ return false;
475
+ if (d.log !== undefined && !Array.isArray(d.log))
476
+ return false;
477
+ if (!isTotalsLike(d.totals))
478
+ return false;
479
+ return d.agents.every(isAgentDetailsLike);
480
+ }
481
+ function extractArchivedRun(entry) {
482
+ if (entry.type === "message" && entry.message.role === "toolResult") {
483
+ const { toolName, details } = entry.message;
484
+ if (toolName !== SUBAGENT_TOOL_NAME && toolName !== WORKFLOW_TOOL_NAME)
485
+ return undefined;
486
+ if (!isPolyphaseDetails(details))
487
+ return undefined;
488
+ return {
489
+ runId: details.runId,
490
+ entryId: entry.id,
491
+ timestamp: Date.parse(entry.timestamp),
492
+ source: "toolResult",
493
+ toolName,
494
+ details,
495
+ };
496
+ }
497
+ if (entry.type === "custom_message" && entry.customType === "polyphase-workflow") {
498
+ if (!isPolyphaseDetails(entry.details))
499
+ return undefined;
500
+ return {
501
+ runId: entry.details.runId,
502
+ entryId: entry.id,
503
+ timestamp: Date.parse(entry.timestamp),
504
+ source: "message",
505
+ details: entry.details,
506
+ };
507
+ }
508
+ return undefined;
509
+ }
510
+ /**
511
+ * `toolResult` entries for `subagent`/`workflow` and `custom_message` "polyphase-workflow"
512
+ * entries whose `details` pass {@link isPolyphaseDetails}, excluding `liveRunIds`, deduped by
513
+ * `runId` (newest kept), newest first, capped at `limit`.
514
+ */
515
+ export function collectArchivedRuns(entries, liveRunIds, limit = 50) {
516
+ const byRunId = new Map();
517
+ for (const entry of entries) {
518
+ const candidate = extractArchivedRun(entry);
519
+ if (!candidate || liveRunIds.has(candidate.runId))
520
+ continue;
521
+ const existing = byRunId.get(candidate.runId);
522
+ if (!existing || candidate.timestamp >= existing.timestamp)
523
+ byRunId.set(candidate.runId, candidate);
524
+ }
525
+ return [...byRunId.values()].sort((a, b) => b.timestamp - a.timestamp).slice(0, limit);
526
+ }
527
+ /** `combineUsage` over every agent's `state.usage`. */
528
+ export function sumRunUsage(run) {
529
+ let total;
530
+ for (const agent of run.agents) {
531
+ const usage = agent.state.usage;
532
+ if (!usage)
533
+ continue;
534
+ total = total ? combineUsage(total, usage) : usage;
535
+ }
536
+ return total;
537
+ }
538
+ //# sourceMappingURL=details.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"details.js","sourceRoot":"","sources":["../../../src/core/polyphase/details.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAU/C,OAAO,EAAE,uBAAuB,EAAE,yBAAyB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAExH;;;GAGG;AAEH;0FAC0F;AAC1F,MAAM,qBAAqB,GAAG,EAAE,CAAC;AA6BjC,MAAM,WAAW,GAAyB;IACzC;QACC,KAAK,EAAE,CAAC;QACR,SAAS,EAAE,IAAI;QACf,eAAe,EAAE,CAAC,GAAG,CAAC;QACtB,OAAO,EAAE,GAAG;QACZ,QAAQ,EAAE,GAAG;QACb,MAAM,EAAE,GAAG;QACX,QAAQ,EAAE,EAAE;QACZ,UAAU,EAAE,GAAG;QACf,gBAAgB,EAAE,IAAI;QACtB,YAAY,EAAE,IAAI;QAClB,cAAc,EAAE,GAAG;QACnB,QAAQ,EAAE,GAAG;QACb,eAAe,EAAE,IAAI;QACrB,eAAe,EAAE,IAAI;QACrB,eAAe,EAAE,GAAG;QACpB,SAAS,EAAE,GAAG;QACd,aAAa,EAAE,GAAG;QAClB,aA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IACD,OAAO,KAAK,CAAC;AACd,CAAC","sourcesContent":["import type { Usage } from \"@draht/ai\";\nimport type { SessionEntry } from \"../session-manager.ts\";\nimport { combineUsage } from \"../usage-totals.ts\";\nimport { billableTokens, extractStatusLine } from \"./child-events.ts\";\nimport { capHeadTail } from \"./result-text.ts\";\nimport type {\n\tAgentDetails,\n\tAgentStatus,\n\tAgentView,\n\tPhaseDetails,\n\tPhaseInfo,\n\tPolyphaseDetails,\n\tRunView,\n} from \"./types.ts\";\nimport { FINAL_DETAILS_MAX_BYTES, PARTIAL_DETAILS_MAX_BYTES, SUBAGENT_TOOL_NAME, WORKFLOW_TOOL_NAME } from \"./types.ts\";\n\n/**\n * Builds the bounded, JSON-compatible `PolyphaseDetails` persisted as a tool result (D2), and\n * reads them back out of the session log for the inspector's archived-run view.\n */\n\n/** Reserve for capHeadTail's \"[... N characters omitted ...]\" marker, so the final `output` text\n * still fits `outputMax` even though the marker is appended after the head/tail split. */\nconst OUTPUT_MARKER_RESERVE = 48;\n\ninterface LevelPlan {\n\tlevel: 0 | 1 | 2 | 3 | 4;\n\toutputMax: number;\n\toutputHeadRatio: number;\n\ttaskMax: number;\n\terrorMax: number;\n\tnowMax: number;\n\tlogLines: number;\n\tlogLineMax: number;\n\tresultPreviewMax: number;\n\tincludeTools: boolean;\n\t/** `undefined` at L4, where `phase.detail` is dropped outright. */\n\tphaseDetailMax?: number;\n\t/** Caps for fields that are not per-agent (§9.2 finding: these were unbounded at every level). */\n\ttitleMax: number;\n\tworkflowDescMax: number;\n\tworkflowArgsMax: number;\n\tworkflowPathMax: number;\n\tmaxPhases: number;\n\tphaseTitleMax: number;\n\tphaseModelMax: number;\n\trunErrorMax: number;\n\tagentLabelMax: number;\n\tagentTypeMax: number;\n\tagentModelMax: number;\n}\n\nconst LEVEL_PLANS: readonly LevelPlan[] = [\n\t{\n\t\tlevel: 0,\n\t\toutputMax: 1200,\n\t\toutputHeadRatio: 1 / 3,\n\t\ttaskMax: 200,\n\t\terrorMax: 400,\n\t\tnowMax: 160,\n\t\tlogLines: 20,\n\t\tlogLineMax: 200,\n\t\tresultPreviewMax: 1200,\n\t\tincludeTools: true,\n\t\tphaseDetailMax: 200,\n\t\ttitleMax: 200,\n\t\tworkflowDescMax: 2000,\n\t\tworkflowArgsMax: 2000,\n\t\tworkflowPathMax: 300,\n\t\tmaxPhases: 100,\n\t\tphaseTitleMax: 120,\n\t\tphaseModelMax: 60,\n\t\trunErrorMax: 2000,\n\t\tagentLabelMax: 200,\n\t\tagentTypeMax: 100,\n\t\tagentModelMax: 200,\n\t},\n\t{\n\t\tlevel: 1,\n\t\toutputMax: 300,\n\t\toutputHeadRatio: 1 / 3,\n\t\ttaskMax: 200,\n\t\terrorMax: 400,\n\t\tnowMax: 160,\n\t\tlogLines: 5,\n\t\tlogLineMax: 200,\n\t\tresultPreviewMax: 300,\n\t\tincludeTools: false,\n\t\tphaseDetailMax: 160,\n\t\ttitleMax: 200,\n\t\tworkflowDescMax: 1000,\n\t\tworkflowArgsMax: 1000,\n\t\tworkflowPathMax: 300,\n\t\tmaxPhases: 60,\n\t\tphaseTitleMax: 100,\n\t\tphaseModelMax: 50,\n\t\trunErrorMax: 1000,\n\t\tagentLabelMax: 200,\n\t\tagentTypeMax: 100,\n\t\tagentModelMax: 150,\n\t},\n\t{\n\t\tlevel: 2,\n\t\toutputMax: 0,\n\t\toutputHeadRatio: 1 / 3,\n\t\ttaskMax: 80,\n\t\terrorMax: 160,\n\t\tnowMax: 0,\n\t\tlogLines: 0,\n\t\tlogLineMax: 200,\n\t\tresultPreviewMax: 120,\n\t\tincludeTools: false,\n\t\tphaseDetailMax: 120,\n\t\ttitleMax: 160,\n\t\tworkflowDescMax: 400,\n\t\tworkflowArgsMax: 400,\n\t\tworkflowPathMax: 200,\n\t\tmaxPhases: 40,\n\t\tphaseTitleMax: 80,\n\t\tphaseModelMax: 40,\n\t\trunErrorMax: 400,\n\t\tagentLabelMax: 160,\n\t\tagentTypeMax: 80,\n\t\tagentModelMax: 100,\n\t},\n\t{\n\t\tlevel: 3,\n\t\toutputMax: 0,\n\t\toutputHeadRatio: 1 / 3,\n\t\ttaskMax: 80,\n\t\terrorMax: 160,\n\t\tnowMax: 0,\n\t\tlogLines: 0,\n\t\tlogLineMax: 200,\n\t\tresultPreviewMax: 120,\n\t\tincludeTools: false,\n\t\tphaseDetailMax: 80,\n\t\ttitleMax: 120,\n\t\tworkflowDescMax: 200,\n\t\tworkflowArgsMax: 200,\n\t\tworkflowPathMax: 160,\n\t\tmaxPhases: 30,\n\t\tphaseTitleMax: 60,\n\t\tphaseModelMax: 30,\n\t\trunErrorMax: 200,\n\t\tagentLabelMax: 120,\n\t\tagentTypeMax: 60,\n\t\tagentModelMax: 80,\n\t},\n\t{\n\t\tlevel: 4,\n\t\toutputMax: 0,\n\t\toutputHeadRatio: 1 / 3,\n\t\ttaskMax: 40,\n\t\terrorMax: 160,\n\t\tnowMax: 0,\n\t\tlogLines: 0,\n\t\tlogLineMax: 200,\n\t\tresultPreviewMax: 120,\n\t\tincludeTools: false,\n\t\ttitleMax: 80,\n\t\tworkflowDescMax: 80,\n\t\tworkflowArgsMax: 80,\n\t\tworkflowPathMax: 80,\n\t\tmaxPhases: 20,\n\t\tphaseTitleMax: 40,\n\t\tphaseModelMax: 20,\n\t\trunErrorMax: 120,\n\t\tagentLabelMax: 40,\n\t\tagentTypeMax: 40,\n\t\tagentModelMax: 40,\n\t},\n] as const;\n\nfunction truncateChars(text: string, maxChars: number): string {\n\tif (maxChars <= 0) return \"\";\n\treturn text.length <= maxChars ? text : `${text.slice(0, Math.max(0, maxChars - 1))}…`;\n}\n\nfunction firstLine(text: string): string {\n\tconst idx = text.indexOf(\"\\n\");\n\treturn idx === -1 ? text : text.slice(0, idx);\n}\n\nfunction byteLength(details: PolyphaseDetails): number {\n\treturn Buffer.byteLength(JSON.stringify(details), \"utf8\");\n}\n\nconst IMPORTANT_STATUSES: readonly AgentStatus[] = [\"failed\", \"running\", \"queued\", \"starting\"];\n\n/** Every important-status agent, plus the first/last 20 by index, capped at `cap` total. */\nfunction selectAgentsBounded(allAgents: readonly AgentView[], cap: number): AgentView[] {\n\tconst important = allAgents.filter((a) => IMPORTANT_STATUSES.includes(a.status));\n\tconst firstN = allAgents.slice(0, 20);\n\tconst lastN = allAgents.slice(Math.max(0, allAgents.length - 20));\n\n\tconst seen = new Set<number>();\n\tconst selected: AgentView[] = [];\n\tfor (const agent of [...important, ...firstN, ...lastN]) {\n\t\tif (seen.has(agent.index)) continue;\n\t\tseen.add(agent.index);\n\t\tselected.push(agent);\n\t\tif (selected.length >= cap) break;\n\t}\n\treturn selected.sort((a, b) => a.index - b.index);\n}\n\n/** Failed agents first, then running, then the rest in index order, capped at `cap`. */\nfunction selectAgentsMinimal(allAgents: readonly AgentView[], cap: number): AgentView[] {\n\tconst failed = allAgents.filter((a) => a.status === \"failed\");\n\tconst running = allAgents.filter((a) => a.status === \"running\");\n\tconst rest = allAgents.filter((a) => a.status !== \"failed\" && a.status !== \"running\");\n\treturn [...failed, ...running, ...rest].slice(0, cap).sort((a, b) => a.index - b.index);\n}\n\n/** Same priority order as {@link selectAgentsMinimal} (failed, then running, then the rest),\n * for `forceFit`, which must drop already-selected `AgentDetails` in that same order. */\nfunction rankAgentDetailsByPriority(agents: readonly AgentDetails[]): AgentDetails[] {\n\tconst failed = agents.filter((a) => a.status === \"failed\");\n\tconst running = agents.filter((a) => a.status === \"running\");\n\tconst rest = agents.filter((a) => a.status !== \"failed\" && a.status !== \"running\");\n\treturn [...failed, ...running, ...rest];\n}\n\nfunction selectAgents(\n\tallAgents: readonly AgentView[],\n\tplan: LevelPlan,\n): { agents: readonly AgentView[]; agentsOmitted?: { count: number; byStatus: Partial<Record<AgentStatus, number>> } } {\n\tif (plan.level < 3) return { agents: allAgents };\n\n\tconst selected = plan.level === 4 ? selectAgentsMinimal(allAgents, 20) : selectAgentsBounded(allAgents, 60);\n\tif (selected.length === allAgents.length) return { agents: selected };\n\n\tconst selectedIndices = new Set(selected.map((a) => a.index));\n\tconst omitted = allAgents.filter((a) => !selectedIndices.has(a.index));\n\tconst byStatus: Partial<Record<AgentStatus, number>> = {};\n\tfor (const agent of omitted) byStatus[agent.status] = (byStatus[agent.status] ?? 0) + 1;\n\n\treturn { agents: selected, agentsOmitted: { count: omitted.length, byStatus } };\n}\n\nfunction buildTotals(allAgents: readonly AgentView[]): PolyphaseDetails[\"totals\"] {\n\tconst byStatus: Partial<Record<AgentStatus, number>> = {};\n\tlet tokens = 0;\n\tlet cost = 0;\n\tlet blockedToolCalls = 0;\n\tfor (const agent of allAgents) {\n\t\tbyStatus[agent.status] = (byStatus[agent.status] ?? 0) + 1;\n\t\ttokens += billableTokens(agent.state.usage) + billableTokens(agent.state.liveUsage);\n\t\tcost += (agent.state.usage?.cost.total ?? 0) + (agent.state.liveUsage?.cost.total ?? 0);\n\t\tblockedToolCalls += agent.state.blockedCount;\n\t}\n\treturn { agents: allAgents.length, tokens, cost, byStatus, blockedToolCalls };\n}\n\nfunction modelText(agent: AgentView): string | undefined {\n\tconst model = agent.state.model;\n\tif (model.provider && model.id) return `${model.provider}/${model.id}`;\n\treturn model.requested;\n}\n\n/** `extractStatusLine` scans the whole output with a global regex; computing it once per agent\n * (instead of once per agent per degradation level) keeps buildDetails O(agents), not O(agents * levels). */\nfunction computeStatusLines(agents: readonly AgentView[]): Map<number, string | undefined> {\n\tconst lines = new Map<number, string | undefined>();\n\tfor (const agent of agents) {\n\t\tconst outputText = agent.result?.output ?? agent.state.finalText;\n\t\tlines.set(agent.index, outputText ? extractStatusLine(outputText) : undefined);\n\t}\n\treturn lines;\n}\n\nfunction buildAgentDetails(\n\tagent: AgentView,\n\tplan: LevelPlan,\n\tctx: { final: boolean },\n\tstatusLines: ReadonlyMap<number, string | undefined>,\n): AgentDetails {\n\tconst details: AgentDetails = {\n\t\ti: agent.index,\n\t\tlabel: truncateChars(agent.label, plan.agentLabelMax),\n\t\tagent: truncateChars(agent.agentType, plan.agentTypeMax),\n\t\tstatus: agent.status,\n\t\ttask: truncateChars(agent.task, plan.taskMax),\n\t};\n\n\tconst model = modelText(agent);\n\tif (model !== undefined) details.model = truncateChars(model, plan.agentModelMax);\n\n\t// L4 keeps only i, label, agent, status, model, task (§9.2); everything below is dropped.\n\tif (plan.level === 4) return details;\n\n\tif (agent.phase !== undefined) details.phase = agent.phase;\n\tif (agent.step !== undefined) details.step = agent.step;\n\tif (agent.cancelReason !== undefined) details.cancelReason = agent.cancelReason;\n\tif (agent.state.model.thinkingLevel !== undefined) details.thinking = agent.state.model.thinkingLevel;\n\n\tdetails.modelSource = agent.state.model.source;\n\tdetails.modelConfirmed = agent.state.model.confirmed;\n\n\tif (agent.state.startedAt !== undefined) details.startedAt = agent.state.startedAt;\n\tif (agent.state.endedAt !== undefined) details.endedAt = agent.state.endedAt;\n\tif (agent.state.turns > 0) details.turns = agent.state.turns;\n\tif (agent.state.toolCalls > 0) details.toolCalls = agent.state.toolCalls;\n\tif (plan.includeTools && Object.keys(agent.state.toolCounts).length > 0) {\n\t\tdetails.tools = { ...agent.state.toolCounts };\n\t}\n\n\tconst tokens = billableTokens(agent.state.usage) + billableTokens(agent.state.liveUsage);\n\tif (tokens > 0) details.tokens = tokens;\n\tconst cost = (agent.state.usage?.cost.total ?? 0) + (agent.state.liveUsage?.cost.total ?? 0);\n\tif (cost > 0) details.cost = cost;\n\n\tif (agent.state.blockedCount > 0) {\n\t\tdetails.blocked = agent.state.blockedCount;\n\t\tconst first = agent.state.blocked[0];\n\t\tif (first) details.blockedSample = `${first.toolName}: ${first.summary}`;\n\t}\n\n\tconst outputText = agent.result?.output ?? agent.state.finalText;\n\tif (outputText) {\n\t\tconst statusLine = statusLines.get(agent.index);\n\t\tif (statusLine !== undefined) details.statusLine = truncateChars(statusLine, 120);\n\t\tif (plan.outputMax > 0) {\n\t\t\tconst budget = Math.max(1, plan.outputMax - OUTPUT_MARKER_RESERVE);\n\t\t\tconst capped = capHeadTail(outputText, budget, { headRatio: plan.outputHeadRatio, keepStatusLine: false });\n\t\t\tdetails.output = truncateChars(capped.text, plan.outputMax);\n\t\t}\n\t}\n\n\tif (!ctx.final && plan.nowMax > 0 && agent.state.nowLine) {\n\t\tdetails.now = truncateChars(agent.state.nowLine, plan.nowMax);\n\t}\n\n\tconst errorSource = agent.result?.stderr || agent.state.errorMessage;\n\tif (errorSource) details.error = truncateChars(firstLine(errorSource), plan.errorMax);\n\n\tif (agent.result?.merge !== undefined) details.merge = agent.result.merge.success ? \"ok\" : \"failed\";\n\n\treturn details;\n}\n\nfunction buildPhaseDetails(phase: PhaseInfo, plan: LevelPlan): PhaseDetails {\n\tconst details: PhaseDetails = { title: truncateChars(phase.title, plan.phaseTitleMax) };\n\tif (plan.phaseDetailMax !== undefined && phase.detail !== undefined) {\n\t\tdetails.detail = truncateChars(phase.detail, plan.phaseDetailMax);\n\t}\n\tif (phase.model !== undefined) details.model = truncateChars(phase.model, plan.phaseModelMax);\n\tif (phase.dynamic) details.dynamic = phase.dynamic;\n\treturn details;\n}\n\nfunction buildLog(run: RunView, plan: LevelPlan): { lines?: string[]; omitted: number } {\n\tif (plan.logLines <= 0) return { omitted: run.logDropped + run.log.length };\n\tconst kept = run.log.slice(-plan.logLines);\n\treturn {\n\t\tlines: kept.map((line) => truncateChars(line.text, plan.logLineMax)),\n\t\tomitted: run.logDropped + (run.log.length - kept.length),\n\t};\n}\n\nfunction buildAtLevel(\n\trun: RunView,\n\tplan: LevelPlan,\n\tctx: { final: boolean },\n\tstatusLines: ReadonlyMap<number, string | undefined>,\n): PolyphaseDetails {\n\tconst { agents, agentsOmitted } = selectAgents(run.agents, plan);\n\tconst log = buildLog(run, plan);\n\n\tconst details: PolyphaseDetails = {\n\t\tv: 1,\n\t\trunId: run.id,\n\t\tkind: run.kind,\n\t\torigin: run.origin,\n\t\ttitle: truncateChars(run.title, plan.titleMax),\n\t\tstatus: run.status,\n\t\tstartedAt: run.startedAt,\n\t\tagents: agents.map((agent) => buildAgentDetails(agent, plan, ctx, statusLines)),\n\t\ttotals: buildTotals(run.agents),\n\t\tdegraded: plan.level,\n\t};\n\n\tif (run.mode !== undefined) details.mode = run.mode;\n\tif (run.endedAt !== undefined) details.endedAt = run.endedAt;\n\tif (run.workflow !== undefined) {\n\t\tdetails.workflow = {\n\t\t\tname: run.workflow.name,\n\t\t\tdescription: truncateChars(run.workflow.description, plan.workflowDescMax),\n\t\t\tsource: run.workflow.source,\n\t\t\t...(run.workflow.path !== undefined ? { path: truncateChars(run.workflow.path, plan.workflowPathMax) } : {}),\n\t\t\targs: truncateChars(run.workflow.args, plan.workflowArgsMax),\n\t\t};\n\t}\n\tif (run.phases.length > 0) {\n\t\tdetails.phases = run.phases.slice(0, plan.maxPhases).map((phase) => buildPhaseDetails(phase, plan));\n\t}\n\tif (run.currentPhase !== undefined) details.currentPhase = run.currentPhase;\n\tif (agentsOmitted !== undefined) details.agentsOmitted = agentsOmitted;\n\tif (log.lines !== undefined) details.log = log.lines;\n\tif (log.omitted > 0) details.logOmitted = log.omitted;\n\tif (run.budget.totalTokens !== null) {\n\t\tdetails.budget = {\n\t\t\ttotalTokens: run.budget.totalTokens,\n\t\t\tspentTokens: run.budget.spentTokens,\n\t\t\texhausted: run.budget.exhausted,\n\t\t};\n\t}\n\tif (run.resultPreview !== undefined) {\n\t\tdetails.resultPreview = truncateChars(\n\t\t\tcapHeadTail(run.resultPreview, plan.resultPreviewMax).text,\n\t\t\tplan.resultPreviewMax,\n\t\t);\n\t}\n\tif (run.error !== undefined) details.error = truncateChars(run.error, plan.runErrorMax);\n\n\treturn details;\n}\n\n/**\n * Last-resort fit for L4 results that still exceed `maxBytes` (e.g. many agents each carrying a\n * close-to-the-cap label/agent/model string). Strips optional fields, then drops agents in\n * reverse priority order (the rest, then running, then failed last; §9.2 finding: dropping by\n * index order instead could discard the one failed agent a user most needs to see), until the\n * result fits. Always terminates: with zero agents and no optional fields, the remaining scalar\n * fields and `totals` are far below either byte budget.\n */\nfunction forceFit(details: PolyphaseDetails, maxBytes: number): PolyphaseDetails {\n\tlet d = details;\n\tif (byteLength(d) <= maxBytes) return d;\n\n\tif (d.workflow && (d.workflow.description.length > 0 || (d.workflow.args?.length ?? 0) > 0)) {\n\t\td = { ...d, workflow: { ...d.workflow, description: \"\", args: \"\" } };\n\t\tif (byteLength(d) <= maxBytes) return d;\n\t}\n\tif (d.phases !== undefined) {\n\t\td = { ...d, phases: undefined };\n\t\tif (byteLength(d) <= maxBytes) return d;\n\t}\n\tif (d.error !== undefined) {\n\t\td = { ...d, error: undefined };\n\t\tif (byteLength(d) <= maxBytes) return d;\n\t}\n\n\tconst originalAgents = d.agents;\n\tconst priorOmitted = d.agentsOmitted;\n\t// Rank by the same priority selectAgentsMinimal uses (failed, then running, then the rest),\n\t// so dropping from the low-priority end here never sacrifices a failed or running agent\n\t// before every less important one is already gone.\n\tconst ranked = rankAgentDetailsByPriority(originalAgents);\n\tfor (let cap = originalAgents.length - 1; cap >= 0; cap--) {\n\t\tconst keptIds = new Set(ranked.slice(0, cap).map((a) => a.i));\n\t\tconst kept = originalAgents.filter((a) => keptIds.has(a.i));\n\t\tconst dropped = originalAgents.filter((a) => !keptIds.has(a.i));\n\t\tconst byStatus: Partial<Record<AgentStatus, number>> = { ...priorOmitted?.byStatus };\n\t\tfor (const agent of dropped) byStatus[agent.status] = (byStatus[agent.status] ?? 0) + 1;\n\t\tconst candidate: PolyphaseDetails = {\n\t\t\t...d,\n\t\t\tagents: kept,\n\t\t\tagentsOmitted: { count: (priorOmitted?.count ?? 0) + dropped.length, byStatus },\n\t\t};\n\t\tif (byteLength(candidate) <= maxBytes || cap === 0) return candidate;\n\t}\n\treturn d;\n}\n\n/**\n * Degrades `run` through L0-L4 (§9.2) until the JSON-stringified result fits `maxBytes`\n * (`FINAL_DETAILS_MAX_BYTES` or `PARTIAL_DETAILS_MAX_BYTES` by default). L4 always fits: per-level\n * caps keep run-level strings and phases bounded at every level, and a final {@link forceFit} pass\n * strips fields and agents if an L4 result (e.g. hundreds of near-cap agents) still does not.\n *\n * `options.now` is accepted for signature symmetry with the rest of the polyphase API (store.ts's\n * injectable clock) but is not currently read: every persisted field is an absolute timestamp, and\n * no derived field here depends on \"the current time\".\n */\nexport function buildDetails(\n\trun: RunView,\n\toptions: { final: boolean; maxBytes?: number; now?: number },\n): PolyphaseDetails {\n\tconst maxBytes = options.maxBytes ?? (options.final ? FINAL_DETAILS_MAX_BYTES : PARTIAL_DETAILS_MAX_BYTES);\n\tconst statusLines = computeStatusLines(run.agents);\n\n\tlet result: PolyphaseDetails | undefined;\n\tfor (const plan of LEVEL_PLANS) {\n\t\tresult = buildAtLevel(run, plan, options, statusLines);\n\t\tif (plan.level === 4 || byteLength(result) <= maxBytes) break;\n\t}\n\tconst built = result as PolyphaseDetails;\n\treturn byteLength(built) <= maxBytes ? built : forceFit(built, maxBytes);\n}\n\nfunction isAgentDetailsLike(value: unknown): boolean {\n\tif (typeof value !== \"object\" || value === null) return false;\n\tconst a = value as Record<string, unknown>;\n\treturn (\n\t\ttypeof a.i === \"number\" &&\n\t\ttypeof a.label === \"string\" &&\n\t\ttypeof a.agent === \"string\" &&\n\t\ttypeof a.status === \"string\" &&\n\t\ttypeof a.task === \"string\"\n\t);\n}\n\nfunction isTotalsLike(value: unknown): boolean {\n\tif (typeof value !== \"object\" || value === null) return false;\n\tconst t = value as Record<string, unknown>;\n\treturn typeof t.agents === \"number\" && typeof t.tokens === \"number\" && typeof t.cost === \"number\";\n}\n\n/** Structural guard: `v === 1`, the required top-level fields, every agent's required fields, and\n * the shape of the optional array/object fields (`phases`, `log`, `totals`). Persisted details can\n * be hand-edited or come from an older schema version, so this is read defensively, not just at the\n * top level: {@link collectArchivedRuns} hands these straight to renderers as `PolyphaseDetails`. */\nexport function isPolyphaseDetails(value: unknown): value is PolyphaseDetails {\n\tif (typeof value !== \"object\" || value === null) return false;\n\tconst d = value as Record<string, unknown>;\n\tif (d.v !== 1) return false;\n\tif (typeof d.runId !== \"string\") return false;\n\tif (typeof d.kind !== \"string\") return false;\n\tif (typeof d.origin !== \"string\") return false;\n\tif (typeof d.title !== \"string\") return false;\n\tif (typeof d.status !== \"string\") return false;\n\tif (typeof d.startedAt !== \"number\") return false;\n\tif (!Array.isArray(d.agents)) return false;\n\tif (d.phases !== undefined && !Array.isArray(d.phases)) return false;\n\tif (d.log !== undefined && !Array.isArray(d.log)) return false;\n\tif (!isTotalsLike(d.totals)) return false;\n\treturn d.agents.every(isAgentDetailsLike);\n}\n\nexport interface ArchivedRun {\n\trunId: string;\n\tentryId: string;\n\ttimestamp: number;\n\tsource: \"toolResult\" | \"message\";\n\ttoolName?: string;\n\tdetails: PolyphaseDetails;\n}\n\nfunction extractArchivedRun(entry: SessionEntry): ArchivedRun | undefined {\n\tif (entry.type === \"message\" && entry.message.role === \"toolResult\") {\n\t\tconst { toolName, details } = entry.message;\n\t\tif (toolName !== SUBAGENT_TOOL_NAME && toolName !== WORKFLOW_TOOL_NAME) return undefined;\n\t\tif (!isPolyphaseDetails(details)) return undefined;\n\t\treturn {\n\t\t\trunId: details.runId,\n\t\t\tentryId: entry.id,\n\t\t\ttimestamp: Date.parse(entry.timestamp),\n\t\t\tsource: \"toolResult\",\n\t\t\ttoolName,\n\t\t\tdetails,\n\t\t};\n\t}\n\tif (entry.type === \"custom_message\" && entry.customType === \"polyphase-workflow\") {\n\t\tif (!isPolyphaseDetails(entry.details)) return undefined;\n\t\treturn {\n\t\t\trunId: entry.details.runId,\n\t\t\tentryId: entry.id,\n\t\t\ttimestamp: Date.parse(entry.timestamp),\n\t\t\tsource: \"message\",\n\t\t\tdetails: entry.details,\n\t\t};\n\t}\n\treturn undefined;\n}\n\n/**\n * `toolResult` entries for `subagent`/`workflow` and `custom_message` \"polyphase-workflow\"\n * entries whose `details` pass {@link isPolyphaseDetails}, excluding `liveRunIds`, deduped by\n * `runId` (newest kept), newest first, capped at `limit`.\n */\nexport function collectArchivedRuns(\n\tentries: readonly SessionEntry[],\n\tliveRunIds: ReadonlySet<string>,\n\tlimit = 50,\n): ArchivedRun[] {\n\tconst byRunId = new Map<string, ArchivedRun>();\n\tfor (const entry of entries) {\n\t\tconst candidate = extractArchivedRun(entry);\n\t\tif (!candidate || liveRunIds.has(candidate.runId)) continue;\n\t\tconst existing = byRunId.get(candidate.runId);\n\t\tif (!existing || candidate.timestamp >= existing.timestamp) byRunId.set(candidate.runId, candidate);\n\t}\n\treturn [...byRunId.values()].sort((a, b) => b.timestamp - a.timestamp).slice(0, limit);\n}\n\n/** `combineUsage` over every agent's `state.usage`. */\nexport function sumRunUsage(run: RunView): Usage | undefined {\n\tlet total: Usage | undefined;\n\tfor (const agent of run.agents) {\n\t\tconst usage = agent.state.usage;\n\t\tif (!usage) continue;\n\t\ttotal = total ? combineUsage(total, usage) : usage;\n\t}\n\treturn total;\n}\n"]}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Throttled partial-update emitter (§10.7): turns a run's `onFlush` notifications into
3
+ * `AgentToolUpdateCallback` invocations, coalesced to at most once per `liveUpdateMs` (TUI) or
4
+ * 1000 ms (every other mode), with a fast path for coarse changes so the row shows a blocked or
5
+ * finished agent without waiting out the full throttle.
6
+ */
7
+ import type { AgentToolUpdateCallback, ExtensionMode } from "../extensions/types.ts";
8
+ import type { PolyphaseRun } from "./store.ts";
9
+ import type { PolyphaseDetails } from "./types.ts";
10
+ export interface PartialUpdateEmitter {
11
+ /** Emits immediately, bypassing the throttle. */
12
+ flush(): void;
13
+ dispose(): void;
14
+ }
15
+ export interface PartialUpdateEmitterOptions {
16
+ mode: ExtensionMode;
17
+ liveUpdateMs: number;
18
+ }
19
+ export declare function createPartialUpdateEmitter(run: PolyphaseRun, onUpdate: AgentToolUpdateCallback<PolyphaseDetails> | undefined, options: PartialUpdateEmitterOptions): PartialUpdateEmitter;
20
+ //# sourceMappingURL=emitter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"emitter.d.ts","sourceRoot":"","sources":["../../../src/core/polyphase/emitter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,uBAAuB,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAGrF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD,MAAM,WAAW,oBAAoB;IACpC,iDAAiD;IACjD,KAAK,IAAI,IAAI,CAAC;IACd,OAAO,IAAI,IAAI,CAAC;CAChB;AAED,MAAM,WAAW,2BAA2B;IAC3C,IAAI,EAAE,aAAa,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;CACrB;AAOD,wBAAgB,0BAA0B,CACzC,GAAG,EAAE,YAAY,EACjB,QAAQ,EAAE,uBAAuB,CAAC,gBAAgB,CAAC,GAAG,SAAS,EAC/D,OAAO,EAAE,2BAA2B,GAClC,oBAAoB,CA4EtB"}