@zyro-sec/session-ui 0.0.0-stage → 1.0.4

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 (261) hide show
  1. package/AGENTS.md +25 -0
  2. package/bunfig.toml +2 -0
  3. package/component-tests/basic-tool.fixture.tsx +46 -0
  4. package/component-tests/basic-tool.spec.ts +63 -0
  5. package/component-tests/file-media.fixture.tsx +39 -0
  6. package/component-tests/file-media.spec.ts +13 -0
  7. package/component-tests/file-tool.spec.ts +62 -0
  8. package/component-tests/file-viewer.fixture.tsx +109 -0
  9. package/component-tests/file-viewer.spec.ts +134 -0
  10. package/component-tests/file.spec.ts +33 -0
  11. package/component-tests/line-comment-v2.spec.ts +15 -0
  12. package/component-tests/markdown-code-wrap.spec.ts +94 -0
  13. package/component-tests/markdown-readiness.spec.ts +95 -0
  14. package/component-tests/markdown.fixture.tsx +104 -0
  15. package/component-tests/markdown.spec.ts +1564 -0
  16. package/component-tests/mermaid-preview.spec.ts +43 -0
  17. package/component-tests/patch-group.spec.ts +79 -0
  18. package/component-tests/read-group.spec.ts +72 -0
  19. package/component-tests/read-image.fixture.tsx +66 -0
  20. package/component-tests/read-image.spec.ts +154 -0
  21. package/component-tests/session-compaction.spec.ts +254 -0
  22. package/component-tests/session-lifecycle.spec.ts +190 -0
  23. package/component-tests/session-message-projection.spec.ts +639 -0
  24. package/component-tests/session-notice-projection.spec.ts +20 -0
  25. package/component-tests/session-review-comments.spec.ts +179 -0
  26. package/component-tests/session-shell.spec.ts +93 -0
  27. package/component-tests/session-timeline-mobile.spec.ts +201 -0
  28. package/component-tests/session-timeline-notices.spec.ts +38 -0
  29. package/component-tests/session-timeline-reasoning.spec.ts +107 -0
  30. package/component-tests/session-timeline.spec.ts +157 -0
  31. package/component-tests/session-tool-projection.spec.ts +323 -0
  32. package/component-tests/text-lifecycle.fixture.tsx +26 -0
  33. package/component-tests/text-lifecycle.spec.ts +42 -0
  34. package/component-tests/timeline-subagent-rows.spec.ts +33 -0
  35. package/component-tests/tool-disclosure.spec.ts +48 -0
  36. package/component-tests/tool-group-sticky.spec.ts +37 -0
  37. package/component-tests/tool-group.spec.ts +290 -0
  38. package/component-tests/tool-status-title.fixture.tsx +22 -0
  39. package/component-tests/tool-status-title.spec.ts +30 -0
  40. package/component-tests/user-message-quotes.spec.ts +111 -0
  41. package/package.json +91 -4
  42. package/playwright.components.config.ts +4 -0
  43. package/playwright.local.config.ts +2 -0
  44. package/src/actions.ts +54 -0
  45. package/src/components/apply-patch-file.ts +122 -0
  46. package/src/components/basic-tool.css +487 -0
  47. package/src/components/basic-tool.tsx +258 -0
  48. package/src/components/copy-machine.ts +78 -0
  49. package/src/components/copy.ts +85 -0
  50. package/src/components/diagram-preview.tsx +28 -0
  51. package/src/components/disclosure-machine.ts +188 -0
  52. package/src/components/disclosure.ts +129 -0
  53. package/src/components/dock-prompt.stories.tsx +73 -0
  54. package/src/components/dock-prompt.tsx +29 -0
  55. package/src/components/file-comment-selection.tsx +146 -0
  56. package/src/components/file-media-machine.ts +165 -0
  57. package/src/components/file-media.stories.tsx +112 -0
  58. package/src/components/file-media.tsx +323 -0
  59. package/src/components/file-search.tsx +73 -0
  60. package/src/components/file.css +59 -0
  61. package/src/components/file.tsx +1300 -0
  62. package/src/components/image-preview.tsx +35 -0
  63. package/src/components/line-comment-annotations.tsx +580 -0
  64. package/src/components/line-comment-machine.ts +262 -0
  65. package/src/components/line-comment-styles.ts +293 -0
  66. package/src/components/line-comment.stories.tsx +152 -0
  67. package/src/components/line-comment.tsx +448 -0
  68. package/src/components/link-segments.ts +20 -0
  69. package/src/components/linked-text.tsx +21 -0
  70. package/src/components/markdown-cache-machine.ts +257 -0
  71. package/src/components/markdown-cache.tsx +556 -0
  72. package/src/components/markdown-clipboard.ts +101 -0
  73. package/src/components/markdown-code-machine.ts +200 -0
  74. package/src/components/markdown-code-state.ts +22 -0
  75. package/src/components/markdown-fragment-links.ts +74 -0
  76. package/src/components/markdown-github.svg +3 -0
  77. package/src/components/markdown-globe.svg +5 -0
  78. package/src/components/markdown-image-machine.ts +170 -0
  79. package/src/components/markdown-image.ts +156 -0
  80. package/src/components/markdown-inline-code-kind.ts +39 -0
  81. package/src/components/markdown-mermaid.ts +18 -0
  82. package/src/components/markdown-phase-machine.ts +102 -0
  83. package/src/components/markdown-projection.ts +13 -0
  84. package/src/components/markdown-session-links.ts +183 -0
  85. package/src/components/markdown-solid.test.tsx +92 -0
  86. package/src/components/markdown-solid.tsx +113 -0
  87. package/src/components/markdown-stream.ts +95 -0
  88. package/src/components/markdown-worker-machine.ts +516 -0
  89. package/src/components/markdown-worker-protocol.ts +73 -0
  90. package/src/components/markdown-worker.ts +250 -0
  91. package/src/components/markdown.css +672 -0
  92. package/src/components/markdown.stories.tsx +199 -0
  93. package/src/components/markdown.tsx +1612 -0
  94. package/src/components/markdown.worker.ts +188 -0
  95. package/src/components/message-file.ts +102 -0
  96. package/src/components/message-nav.css +134 -0
  97. package/src/components/message-nav.tsx +159 -0
  98. package/src/components/message-part.css +2109 -0
  99. package/src/components/message-part.tsx +3 -0
  100. package/src/components/question-prompt-experimental.css +576 -0
  101. package/src/components/question-prompt.css +527 -0
  102. package/src/components/quote-highlight.css +59 -0
  103. package/src/components/review-focus-driver.ts +142 -0
  104. package/src/components/review-focus-machine.ts +95 -0
  105. package/src/components/session-compaction.css +270 -0
  106. package/src/components/session-diff.ts +358 -0
  107. package/src/components/session-error.tsx +41 -0
  108. package/src/components/session-retry.tsx +92 -0
  109. package/src/components/session-review-machine.ts +241 -0
  110. package/src/components/session-review.css +264 -0
  111. package/src/components/session-review.stories.tsx +232 -0
  112. package/src/components/session-review.tsx +733 -0
  113. package/src/components/session-turn.css +282 -0
  114. package/src/components/shell-submessage.css +24 -0
  115. package/src/components/timeline-separator.tsx +69 -0
  116. package/src/components/tool-count-label.css +57 -0
  117. package/src/components/tool-count-label.tsx +76 -0
  118. package/src/components/tool-count-summary.css +102 -0
  119. package/src/components/tool-count-summary.stories.tsx +99 -0
  120. package/src/components/tool-count-summary.tsx +44 -0
  121. package/src/components/tool-error-card.css +178 -0
  122. package/src/components/tool-error-card.stories.tsx +113 -0
  123. package/src/components/tool-error-card.tsx +202 -0
  124. package/src/components/tool-status-title-machine.ts +128 -0
  125. package/src/components/tool-status-title.css +89 -0
  126. package/src/components/tool-status-title.tsx +163 -0
  127. package/src/context/data.tsx +107 -0
  128. package/src/context/index.ts +1 -0
  129. package/src/context/markdown.tsx +129 -0
  130. package/src/context/sources.tsx +34 -0
  131. package/src/document.ts +8 -0
  132. package/src/file-presentation.ts +34 -0
  133. package/src/message/action-machine.ts +72 -0
  134. package/src/message/agent-label.ts +3 -0
  135. package/src/message/attachment-card.css +119 -0
  136. package/src/message/attachment-card.tsx +40 -0
  137. package/src/message/attachment-media-machine.ts +213 -0
  138. package/src/message/comment-card.tsx +155 -0
  139. package/src/message/compaction-entrance-machine.ts +93 -0
  140. package/src/message/current-message.tsx +157 -0
  141. package/src/message/current-tool-state.ts +161 -0
  142. package/src/message/message-content.tsx +1391 -0
  143. package/src/message/paced-text-machine.ts +73 -0
  144. package/src/message/prompt-message.stories.tsx +149 -0
  145. package/src/message/relative-time.ts +45 -0
  146. package/src/message/user-message-quote-parts.ts +16 -0
  147. package/src/message/user-message-quotes.stories.tsx +797 -0
  148. package/src/message/user-message-quotes.tsx +135 -0
  149. package/src/pierre/comment-hover-machine.ts +53 -0
  150. package/src/pierre/comment-hover.ts +131 -0
  151. package/src/pierre/comment-selection-machine.ts +257 -0
  152. package/src/pierre/commented-lines.ts +106 -0
  153. package/src/pierre/diff-selection.ts +91 -0
  154. package/src/pierre/file-find-machine.ts +323 -0
  155. package/src/pierre/file-find.ts +537 -0
  156. package/src/pierre/file-runtime.ts +42 -0
  157. package/src/pierre/file-selection.ts +140 -0
  158. package/src/pierre/index.ts +344 -0
  159. package/src/pierre/media.ts +140 -0
  160. package/src/pierre/render-machine.ts +381 -0
  161. package/src/pierre/selection-bridge.ts +82 -0
  162. package/src/pierre/selection-machine.ts +198 -0
  163. package/src/pierre/virtualizer.ts +114 -0
  164. package/src/pierre/worker.ts +46 -0
  165. package/src/sources/experiment.ts +59 -0
  166. package/src/sources/site-icon.tsx +53 -0
  167. package/src/sources/sources-chip.tsx +57 -0
  168. package/src/sources/sources-expand-machine.ts +101 -0
  169. package/src/sources/sources-panel.tsx +249 -0
  170. package/src/sources/sources-title.tsx +21 -0
  171. package/src/sources/sources.css +266 -0
  172. package/src/sources/sources.ts +141 -0
  173. package/src/storybook/current-session-fixtures.ts +1492 -0
  174. package/src/storybook/current-session-scenarios.ts +76 -0
  175. package/src/storybook/current-session-story.tsx +154 -0
  176. package/src/storybook/index.ts +43 -0
  177. package/src/styles/index.css +19 -0
  178. package/src/timeline/__snapshots__/group-labels.test.ts.snap +110 -0
  179. package/src/timeline/__snapshots__/placement.test.ts.snap +1041 -0
  180. package/src/timeline/code-blocks.stories.tsx +105 -0
  181. package/src/timeline/commentary.stories.tsx +70 -0
  182. package/src/timeline/commentary.ts +38 -0
  183. package/src/timeline/detail.ts +111 -0
  184. package/src/timeline/error-card.stories.tsx +134 -0
  185. package/src/timeline/error-spacing.stories.tsx +71 -0
  186. package/src/timeline/facts.ts +493 -0
  187. package/src/timeline/file-changes.stories.tsx +436 -0
  188. package/src/timeline/group-label-fixtures.ts +97 -0
  189. package/src/timeline/mermaid.stories.tsx +131 -0
  190. package/src/timeline/place.ts +280 -0
  191. package/src/timeline/placement-fixtures.ts +365 -0
  192. package/src/timeline/placement-reactivity.ts +133 -0
  193. package/src/timeline/projection.ts +1261 -0
  194. package/src/timeline/research-and-agents.stories.tsx +461 -0
  195. package/src/timeline/session-timeline-row.tsx +1675 -0
  196. package/src/timeline/session-timeline.tsx +68 -0
  197. package/src/timeline/steer-arrival-machine.ts +134 -0
  198. package/src/timeline/terminal-work.stories.tsx +464 -0
  199. package/src/timeline/timeline-row.stories.tsx +1069 -0
  200. package/src/timeline/timeline-row.ts +238 -0
  201. package/src/timeline/turn-phase-machine.ts +112 -0
  202. package/src/timeline/user-message-fold.stories.tsx +194 -0
  203. package/src/timeline/user-runs.ts +87 -0
  204. package/src/timeline-next/__snapshots__/placement.test.ts.snap +990 -0
  205. package/src/timeline-next/compaction.tsx +236 -0
  206. package/src/timeline-next/errors.ts +101 -0
  207. package/src/timeline-next/event-row.tsx +168 -0
  208. package/src/timeline-next/events.ts +103 -0
  209. package/src/timeline-next/index.ts +10 -0
  210. package/src/timeline-next/nested-rail.ts +125 -0
  211. package/src/timeline-next/projection.ts +1028 -0
  212. package/src/timeline-next/row.tsx +1728 -0
  213. package/src/timeline-next/rules.ts +89 -0
  214. package/src/timeline-next/separator.tsx +73 -0
  215. package/src/timeline-next/spacing.ts +127 -0
  216. package/src/timeline-next/status-line.tsx +41 -0
  217. package/src/timeline-next/status.ts +33 -0
  218. package/src/timeline-next/stories/compare-renderer.stories.tsx +190 -0
  219. package/src/timeline-next/stories/compare-sessions/.gitignore +3 -0
  220. package/src/timeline-next/stories/compare.stories.tsx +486 -0
  221. package/src/timeline-next/stories/playground.stories.tsx +297 -0
  222. package/src/timeline-next/stories/session-replay.ts +1156 -0
  223. package/src/timeline-next/stories/session-timeline.tsx +50 -0
  224. package/src/timeline-next/stories/subagent-status.stories.tsx +132 -0
  225. package/src/timeline-next/timeline-next.css +649 -0
  226. package/src/timeline-next/tools/__snapshots__/activity-label.test.ts.snap +39 -0
  227. package/src/timeline-next/tools/activity-label.ts +166 -0
  228. package/src/timeline-next/tools/assistant-content.tsx +84 -0
  229. package/src/timeline-next/tools/context-group.tsx +619 -0
  230. package/src/timeline-next/tools/error-card.tsx +147 -0
  231. package/src/timeline-next/tools/explore.tsx +119 -0
  232. package/src/timeline-next/tools/file-group.tsx +128 -0
  233. package/src/timeline-next/tools/patch.tsx +174 -0
  234. package/src/timeline-next/tools/shell.tsx +186 -0
  235. package/src/timeline-next/tools/thought.tsx +87 -0
  236. package/src/timeline-next/tools/tool-display.tsx +119 -0
  237. package/src/tools/ansi.ts +8 -0
  238. package/src/tools/context-group-label.ts +123 -0
  239. package/src/tools/file-runs.ts +35 -0
  240. package/src/tools/project-path.ts +18 -0
  241. package/src/tools/read-group.stories.tsx +159 -0
  242. package/src/tools/run-machine.ts +185 -0
  243. package/src/tools/shell-output-machine.ts +233 -0
  244. package/src/tools/shell-output.ts +91 -0
  245. package/src/tools/subagent-row-variants.stories.tsx +129 -0
  246. package/src/tools/tool-group.stories.tsx +345 -0
  247. package/src/tools/tool-renderer.tsx +2876 -0
  248. package/src/v2/components/line-comment-annotations-v2.tsx +215 -0
  249. package/src/v2/components/session-file-panel-v2.tsx +149 -0
  250. package/src/v2/components/session-progress-indicator-v2.css +5 -0
  251. package/src/v2/components/session-progress-indicator-v2.stories.tsx +86 -0
  252. package/src/v2/components/session-progress-indicator-v2.tsx +16 -0
  253. package/src/v2/components/session-review-empty-changes-v2.tsx +17 -0
  254. package/src/v2/components/session-review-empty-no-git-v2.tsx +28 -0
  255. package/src/v2/components/session-review-file-preview-v2-virtualize.ts +5 -0
  256. package/src/v2/components/session-review-file-preview-v2.tsx +266 -0
  257. package/src/v2/components/session-review-v2.css +653 -0
  258. package/src/v2/components/session-review-v2.tsx +422 -0
  259. package/src/v2/components/sidebar-presence-machine.ts +69 -0
  260. package/tsconfig.json +20 -0
  261. package/README.md +0 -3
@@ -0,0 +1,159 @@
1
+ import type { JsonValue, SessionMessageAssistant } from "@zyro-sec/client/promise"
2
+ import { createEffect, createSignal } from "solid-js"
3
+ import { CurrentSessionProviders } from "../storybook/current-session-story"
4
+ import { STORY_TIME } from "../storybook/current-session-fixtures"
5
+ import { storyDocument, storyTool } from "../storybook/current-session-scenarios"
6
+ import { SessionTimeline } from "../timeline/session-timeline"
7
+ import { timelinePresets } from "../timeline/detail"
8
+ import { type ContextGroupPart, CurrentContextToolGroup } from "./tool-renderer"
9
+
10
+ export default {
11
+ title: "OpenCode/Work/Read group",
12
+ id: "current-read-group",
13
+ component: CurrentContextToolGroup,
14
+ args: { width: 600 },
15
+ argTypes: { width: { control: { type: "range", min: 280, max: 960, step: 10 } } },
16
+ }
17
+
18
+ type Read = string | [string, Record<string, JsonValue>]
19
+
20
+ // Mirrors a real exploration turn: runs of reads split by thoughts, with a few shell calls.
21
+ const steps: (Read[] | { thought: number } | { shell: string })[] = [
22
+ ["src/index.tsx", "src/main.ts", "src/rpc.ts", "src/model.ts", "src/toolbar.tsx"],
23
+ { thought: 4 },
24
+ [["src/connection.ts", { limit: 120 }], "src/app/index.tsx", "src/environment.tsx", "src/env.d.ts"],
25
+ { thought: 3 },
26
+ ["src/components/index.tsx", "src/components/button.tsx", "src/components/toggle.ts", "src/hooks/index.tsx"],
27
+ { thought: 4 },
28
+ ["src/context.tsx"],
29
+ { thought: 4 },
30
+ [
31
+ "src/plugin/host.ts",
32
+ "src/plugin/surfaces.ts",
33
+ "src/plugin/builtins.ts",
34
+ "src/plugin/manager.ts",
35
+ "src/plugin/module.ts",
36
+ "src/plugin/registry.ts",
37
+ "src/plugin/loader.ts",
38
+ ],
39
+ { thought: 2 },
40
+ { shell: "bun typecheck" },
41
+ [
42
+ "src/session/index.ts",
43
+ "src/session/prompt.ts",
44
+ ["src/session/runner.ts", { offset: 200, limit: 80 }],
45
+ "src/session/store.ts",
46
+ "src/session/inbox.ts",
47
+ "src/session/compaction.ts",
48
+ "src/session/execution.ts",
49
+ "src/session/history.ts",
50
+ "src/session/events.ts",
51
+ "src/session/projection.ts",
52
+ ],
53
+ { thought: 5 },
54
+ [
55
+ "src/tui/app.tsx",
56
+ "src/tui/theme.ts",
57
+ "src/tui/keymap.ts",
58
+ "src/tui/layout.tsx",
59
+ "src/tui/prompt.tsx",
60
+ "src/tui/timeline.tsx",
61
+ "src/tui/sidebar.tsx",
62
+ "src/tui/dialog.tsx",
63
+ "src/tui/toast.tsx",
64
+ "src/tui/status.tsx",
65
+ "src/tui/scroll.ts",
66
+ "src/tui/input.ts",
67
+ ],
68
+ { shell: "git status --short" },
69
+ { thought: 3 },
70
+ [
71
+ "src/server/index.ts",
72
+ "src/server/routes.ts",
73
+ "src/server/auth.ts",
74
+ "src/server/cors.ts",
75
+ "src/server/events.ts",
76
+ "src/server/session.ts",
77
+ "src/server/project.ts",
78
+ "src/server/file.ts",
79
+ "src/server/config.ts",
80
+ ],
81
+ { thought: 4 },
82
+ [
83
+ "src/config/index.ts",
84
+ "src/config/agent.ts",
85
+ "src/config/model.ts",
86
+ "src/config/provider.ts",
87
+ "src/config/permission.ts",
88
+ "src/config/theme.ts",
89
+ "src/config/keybind.ts",
90
+ "src/config/mcp.ts",
91
+ "src/config/lsp.ts",
92
+ "src/config/formatter.ts",
93
+ "src/config/plugin.ts",
94
+ ],
95
+ ]
96
+
97
+ const parts = steps.flatMap<ContextGroupPart>((step, index) => {
98
+ if ("thought" in step)
99
+ return [
100
+ {
101
+ type: "reasoning",
102
+ id: `read_group_thought_${index}`,
103
+ text: "Keep tracing where the renderer groups these tool rows before changing anything.",
104
+ time: { created: STORY_TIME, completed: STORY_TIME + step.thought * 1000 },
105
+ },
106
+ ]
107
+
108
+ if ("shell" in step)
109
+ return [storyTool(`read_group_shell_${index}`, "shell", "completed", { command: step.shell }, { output: "ok" })]
110
+
111
+ return step.map((read, item) => {
112
+ const [path, args] = Array.isArray(read) ? read : [read, {}]
113
+
114
+ return storyTool(`read_group_read_${index}_${item}`, "read", "completed", { path, ...args })
115
+ })
116
+ })
117
+
118
+ const content = parts.flatMap<SessionMessageAssistant["content"][number]>((part) => {
119
+ if (part.type === "tool") return [part]
120
+
121
+ if (part.type === "reasoning") return [{ type: "reasoning", text: part.text, time: part.time }]
122
+
123
+ return []
124
+ })
125
+
126
+ export const UsedGroup = {
127
+ args: { open: true },
128
+ render: (args: { width: number; open: boolean }) => {
129
+ const [open, setOpen] = createSignal(args.open)
130
+ createEffect(() => setOpen(args.open))
131
+
132
+ return (
133
+ <section style={{ width: "100%", "max-width": `${args.width}px`, padding: "16px" }}>
134
+ <CurrentSessionProviders document={storyDocument(content)}>
135
+ <CurrentContextToolGroup parts={parts} busy={false} open={open()} onOpenChange={setOpen} />
136
+ </CurrentSessionProviders>
137
+ </section>
138
+ )
139
+ },
140
+ }
141
+
142
+ export const WithoutUsedGroup = {
143
+ args: { preset: timelinePresets[0].id },
144
+ argTypes: { preset: { options: timelinePresets.map((preset) => preset.id) } },
145
+ render: (args: { width: number; preset: string }) => {
146
+ const document = storyDocument(content)
147
+
148
+ return (
149
+ <section style={{ width: "100%", "max-width": `${args.width}px`, padding: "16px" }}>
150
+ <CurrentSessionProviders document={document}>
151
+ <SessionTimeline
152
+ document={document}
153
+ timelineDetail={timelinePresets.find((preset) => preset.id === args.preset)!.value}
154
+ />
155
+ </CurrentSessionProviders>
156
+ </section>
157
+ )
158
+ },
159
+ }
@@ -0,0 +1,185 @@
1
+ import { stripAnsi } from "./ansi"
2
+
3
+ /** A shell tool call as the renderer decoded it: tool state, input and metadata. */
4
+ type ShellTool = {
5
+ readonly status: string | undefined
6
+ readonly input: { readonly command?: string }
7
+ readonly metadata: { readonly command?: string; readonly shellID?: string; readonly status?: string }
8
+ readonly output: string | undefined
9
+ }
10
+
11
+ export type ShellRunProps = {
12
+ readonly status: string | undefined
13
+ readonly command: string
14
+ readonly output: string
15
+ readonly shellID: string | undefined
16
+ /** The result carries the shell's final output, so the shell is never read again. */
17
+ readonly saved: boolean
18
+ /** The running-shell inventory lists the shell. It can arrive after the timeline renders. */
19
+ readonly shellRunning: boolean
20
+ }
21
+
22
+ // `completed` is an agent shell that finished in the foreground; the others are direct user shells. A backgrounded agent
23
+ // shell reports `running` for good, so its output keeps coming from the shell.
24
+ const SAVED_STATUSES = new Set(["completed", "exited", "timeout", "killed"])
25
+
26
+ export function shellRunProps(tool: ShellTool, shellRunning: (id: string) => boolean): ShellRunProps {
27
+ const shellID = tool.metadata.shellID
28
+
29
+ return {
30
+ status: tool.status,
31
+ command: tool.input.command ?? tool.metadata.command ?? "",
32
+ output: tool.output ?? "",
33
+ shellID,
34
+ saved: tool.metadata.status !== undefined && SAVED_STATUSES.has(tool.metadata.status),
35
+ shellRunning: shellID !== undefined && shellRunning(shellID),
36
+ }
37
+ }
38
+
39
+ type ShellRunShared = { readonly command: string; readonly output: string }
40
+
41
+ /** `streamed` is the latest output read from shell `shellID`; it is shown in place of the result's output. */
42
+ type ShellRunFollowed = ShellRunShared & { readonly shellID: string | undefined; readonly streamed: string }
43
+
44
+ /** A shell row: the model is writing the command, the shell runs (in the foreground or background), or it ended. */
45
+ export type ShellRunState =
46
+ | (ShellRunShared & { readonly status: "writing" })
47
+ | (ShellRunFollowed & { readonly status: "running" })
48
+ | (ShellRunFollowed & { readonly status: "exited"; readonly saved: boolean })
49
+ | (ShellRunFollowed & { readonly status: "error" })
50
+
51
+ export type ShellRunEvent =
52
+ | { readonly type: "sync"; readonly props: ShellRunProps }
53
+ | { readonly type: "output.received"; readonly shellID: string; readonly output: string }
54
+
55
+ export function createShellRunState(props: ShellRunProps): ShellRunState {
56
+ return fromProps(props, "")
57
+ }
58
+
59
+ export function transitionShellRun(state: ShellRunState, event: ShellRunEvent): ShellRunState {
60
+ if (event.type === "output.received") {
61
+ if (state.status === "writing" || (state.status === "exited" && state.saved)) return state
62
+
63
+ if (state.shellID !== event.shellID || state.streamed === event.output) return state
64
+
65
+ return { ...state, streamed: event.output }
66
+ }
67
+
68
+ // Output read from one shell never shows for another.
69
+ const streamed = state.status !== "writing" && state.shellID === event.props.shellID ? state.streamed : ""
70
+ const next = fromProps(event.props, streamed)
71
+
72
+ return sameShellRun(state, next) ? state : next
73
+ }
74
+
75
+ // The tool status `BasicTool` reads, so a background shell counts as pending there too.
76
+ const SHELL_BASIC_STATUS = {
77
+ writing: "streaming",
78
+ running: "running",
79
+ exited: "completed",
80
+ error: "error",
81
+ } as const satisfies Record<ShellRunState["status"], string>
82
+
83
+ export function viewShellRun(state: ShellRunState) {
84
+ const followed = state.status === "writing" || (state.status === "exited" && state.saved) ? undefined : state
85
+ const id = followed?.shellID
86
+
87
+ return {
88
+ pending: state.status === "writing" || state.status === "running",
89
+ basicStatus: SHELL_BASIC_STATUS[state.status],
90
+ command: state.command,
91
+ writingPlaceholder: state.status === "writing" && !state.command,
92
+ /** The shell to read output from, and whether to keep polling it. */
93
+ follow: id === undefined ? undefined : { id, running: state.status === "running" },
94
+ output: stripAnsi((id !== undefined && followed?.streamed) || state.output).replace(/\r\n?/g, "\n"),
95
+ }
96
+ }
97
+
98
+ function fromProps(props: ShellRunProps, streamed: string): ShellRunState {
99
+ if (props.status === "streaming") return { status: "writing", command: props.command, output: props.output }
100
+ const shared = { command: props.command, output: props.output, shellID: props.shellID, streamed }
101
+
102
+ if (props.status === "error") return { ...shared, status: "error" }
103
+
104
+ if (!props.saved && (props.status === "running" || props.shellRunning)) return { ...shared, status: "running" }
105
+
106
+ return { ...shared, status: "exited", saved: props.saved }
107
+ }
108
+
109
+ function sameShellRun(a: ShellRunState, b: ShellRunState) {
110
+ if (a.status !== b.status || a.command !== b.command || a.output !== b.output) return false
111
+
112
+ if (a.status === "writing" || b.status === "writing") return true
113
+
114
+ if (a.status === "exited" && b.status === "exited" && a.saved !== b.saved) return false
115
+
116
+ return a.shellID === b.shellID && a.streamed === b.streamed
117
+ }
118
+
119
+ /**
120
+ * A shell call the user stopped fails with its output followed by a notice, which its metadata names. Its output shows
121
+ * like a finished call's and only the notice as the error. A plugin that rewrote the error gets it shown as is.
122
+ */
123
+ export function stoppedShell(tool: {
124
+ readonly tool: string
125
+ readonly status?: string
126
+ readonly metadata: { readonly interrupted?: boolean; readonly notice?: string }
127
+ readonly error?: string
128
+ }) {
129
+ if (tool.tool !== "shell" || tool.status !== "error" || tool.metadata.interrupted !== true) return undefined
130
+
131
+ const notice = tool.metadata.notice
132
+
133
+ if (!notice || !tool.error?.endsWith(notice)) return undefined
134
+
135
+ return { output: tool.error.slice(0, -notice.length).replace(/\n+$/, ""), notice }
136
+ }
137
+
138
+ /** A subagent call as the renderer decoded it. */
139
+ type SubagentTool = {
140
+ readonly status: string | undefined
141
+ readonly metadata: { readonly status?: string }
142
+ }
143
+
144
+ export type SubagentRunProps = {
145
+ readonly status: string | undefined
146
+ /** The child session is busy, which outlives the call because every subagent runs in the background. */
147
+ readonly childBusy: boolean
148
+ }
149
+
150
+ export function subagentRunProps(tool: SubagentTool, childBusy: boolean): SubagentRunProps {
151
+ return {
152
+ status: tool.status,
153
+ childBusy,
154
+ }
155
+ }
156
+
157
+ /** The model is writing the delegation, or the subagent runs, finished or failed. */
158
+ export type SubagentRunState =
159
+ | { readonly status: "delegating" }
160
+ | { readonly status: "running" | "completed" | "error" }
161
+
162
+ export type SubagentRunEvent = { readonly type: "sync"; readonly props: SubagentRunProps }
163
+
164
+ export const INITIAL_SUBAGENT_RUN_STATE: SubagentRunState = Object.freeze({ status: "delegating" })
165
+
166
+ export function transitionSubagentRun(state: SubagentRunState, event: SubagentRunEvent): SubagentRunState {
167
+ const props = event.props
168
+
169
+ if (props.status === "streaming") return INITIAL_SUBAGENT_RUN_STATE
170
+
171
+ const status =
172
+ props.status === "running" || props.childBusy ? "running" : props.status === "error" ? "error" : "completed"
173
+
174
+ if (state.status === status) return state
175
+
176
+ return { status }
177
+ }
178
+
179
+ export function viewSubagentRun(state: SubagentRunState) {
180
+ return {
181
+ delegating: state.status === "delegating",
182
+ running: state.status === "delegating" || state.status === "running",
183
+ basicStatus: state.status === "delegating" ? "streaming" : state.status,
184
+ }
185
+ }
@@ -0,0 +1,233 @@
1
+ import { TextBoundary } from "@zyro-sec/util/text-boundary"
2
+
3
+ // Same page size as the TUI shell output viewer; only this much recent output is retained and rendered.
4
+ export const SHELL_OUTPUT_TAIL_BYTES = 64 * 1024
5
+
6
+ export const SHELL_POLL_MS = 1_000
7
+
8
+ export const SHELL_MAX_BACKOFF_MS = 16_000
9
+
10
+ /** A read that has not settled by then counts as failed, so a hung request cannot stop the follower. */
11
+ export const SHELL_READ_TIMEOUT_MS = 15_000
12
+
13
+ /** Retries for a shell that is no longer running before its output is left as it is. */
14
+ export const SHELL_EXITED_MAX_RETRIES = 3
15
+
16
+ export type ShellCachedOutput = { readonly cursor: number; readonly output: string; readonly complete: boolean }
17
+
18
+ export type ShellOutputPage = { readonly output: string; readonly cursor: number; readonly size: number }
19
+
20
+ type Progress = { readonly attempt: number; readonly cursor: number; readonly output: string }
21
+
22
+ /**
23
+ * Reads one shell's output. `reading` has one request in flight; `final` means it started after the shell stopped
24
+ * running, so reaching the end completes the output. `waiting` is the poll interval while the shell runs, or a backoff
25
+ * after `failures` consecutive failed reads. `complete` and `failed` read again if the shell turns out to be running;
26
+ * a `missing` shell is gone from the server for good.
27
+ */
28
+ export type ShellFollowerState =
29
+ | (Progress & {
30
+ readonly status: "reading"
31
+ readonly running: boolean
32
+ readonly final: boolean
33
+ readonly failures: number
34
+ })
35
+ | (Progress & {
36
+ readonly status: "waiting"
37
+ readonly running: boolean
38
+ readonly failures: number
39
+ readonly delayMs: number
40
+ })
41
+ | (Progress & { readonly status: "complete" })
42
+ | (Progress & { readonly status: "failed" })
43
+ | (Progress & { readonly status: "missing" })
44
+ | { readonly status: "disposed" }
45
+
46
+ export type ShellFollowerEvent =
47
+ | { readonly type: "running.changed"; readonly running: boolean }
48
+ | { readonly type: "read.succeeded"; readonly attempt: number; readonly page: ShellOutputPage }
49
+ | { readonly type: "read.missing"; readonly attempt: number }
50
+ | { readonly type: "read.failed"; readonly attempt: number }
51
+ | { readonly type: "wait.elapsed"; readonly attempt: number }
52
+ | { readonly type: "disposed" }
53
+
54
+ /** `read` and `wait` replace the follower's single pending timer; `cancel` clears it. */
55
+ export type ShellFollowerEffect =
56
+ | { readonly type: "read"; readonly attempt: number; readonly cursor: number }
57
+ | { readonly type: "wait"; readonly attempt: number; readonly delayMs: number }
58
+ | { readonly type: "cancel" }
59
+ | { readonly type: "cache"; readonly cursor: number; readonly output: string; readonly complete: boolean }
60
+ | { readonly type: "cache-missing" }
61
+ | { readonly type: "emit"; readonly output: string }
62
+
63
+ export type ShellFollowerTransition = {
64
+ readonly state: ShellFollowerState
65
+ readonly effects: readonly ShellFollowerEffect[]
66
+ }
67
+
68
+ const NO_EFFECTS: readonly ShellFollowerEffect[] = Object.freeze([])
69
+
70
+ const CANCEL: ShellFollowerEffect = Object.freeze({ type: "cancel" })
71
+
72
+ const DISPOSED: ShellFollowerState = Object.freeze({ status: "disposed" })
73
+
74
+ export function createShellFollowerState(input: {
75
+ cached: ShellCachedOutput | undefined
76
+ missing: boolean
77
+ running: boolean
78
+ }): ShellFollowerTransition {
79
+ const cursor = input.cached?.cursor ?? 0
80
+ const output = input.cached?.output ?? ""
81
+ const emit: ShellFollowerEffect[] = input.cached ? [{ type: "emit", output }] : []
82
+
83
+ if (input.missing) return { state: { status: "missing", attempt: 0, cursor, output }, effects: emit }
84
+
85
+ if (input.cached?.complete && !input.running)
86
+ return { state: { status: "complete", attempt: 0, cursor, output }, effects: emit }
87
+
88
+ return {
89
+ state: {
90
+ status: "reading",
91
+ attempt: 1,
92
+ cursor,
93
+ output,
94
+ running: input.running,
95
+ final: !input.running,
96
+ failures: 0,
97
+ },
98
+ effects: [
99
+ // A shell believed complete is running after all, so its cached output is no longer final.
100
+ ...(input.cached?.complete ? [{ type: "cache", cursor, output, complete: false } as const] : []),
101
+ { type: "read", attempt: 1, cursor },
102
+ ...emit,
103
+ ],
104
+ }
105
+ }
106
+
107
+ export function transitionShellFollower(state: ShellFollowerState, event: ShellFollowerEvent): ShellFollowerTransition {
108
+ if (state.status === "disposed") return { state, effects: NO_EFFECTS }
109
+
110
+ if (event.type === "disposed")
111
+ return {
112
+ state: DISPOSED,
113
+ effects: state.status === "reading" || state.status === "waiting" ? [CANCEL] : NO_EFFECTS,
114
+ }
115
+
116
+ if (state.status === "missing") return { state, effects: NO_EFFECTS }
117
+
118
+ if (event.type === "running.changed") return runningChanged(state, event.running)
119
+
120
+ if (event.type === "wait.elapsed") {
121
+ if (state.status !== "waiting" || event.attempt !== state.attempt) return { state, effects: NO_EFFECTS }
122
+
123
+ return read(state, state.running, state.failures)
124
+ }
125
+
126
+ if (state.status !== "reading" || event.attempt !== state.attempt) return { state, effects: NO_EFFECTS }
127
+
128
+ if (event.type === "read.missing")
129
+ return {
130
+ state: { status: "missing", attempt: state.attempt, cursor: state.cursor, output: state.output },
131
+ effects: [CANCEL, { type: "cache-missing" }],
132
+ }
133
+
134
+ if (event.type === "read.failed") {
135
+ const failures = state.failures + 1
136
+
137
+ if (!state.running && failures > SHELL_EXITED_MAX_RETRIES)
138
+ return {
139
+ state: { status: "failed", attempt: state.attempt, cursor: state.cursor, output: state.output },
140
+ effects: [CANCEL],
141
+ }
142
+
143
+ return backoff(state, state.running, failures)
144
+ }
145
+
146
+ const page = event.page
147
+ const advanced = page.cursor > state.cursor
148
+ const cursor = Math.max(state.cursor, page.cursor)
149
+ const output = TextBoundary.tail(state.output + page.output, SHELL_OUTPUT_TAIL_BYTES)
150
+ const emit = { type: "emit", output } as const
151
+ const attempt = state.attempt + 1
152
+
153
+ if (state.running)
154
+ return {
155
+ state: { status: "waiting", attempt, cursor, output, running: true, failures: 0, delayMs: SHELL_POLL_MS },
156
+ effects: [
157
+ { type: "cache", cursor, output, complete: false },
158
+ { type: "wait", attempt, delayMs: SHELL_POLL_MS },
159
+ emit,
160
+ ],
161
+ }
162
+
163
+ // A read that started while the shell ran may have missed its last output, so the shell is read once more.
164
+ if (!state.final || (cursor < page.size && advanced))
165
+ return {
166
+ state: { status: "reading", attempt, cursor, output, running: false, final: true, failures: 0 },
167
+ effects: [{ type: "cache", cursor, output, complete: false }, { type: "read", attempt, cursor }, emit],
168
+ }
169
+
170
+ return {
171
+ state: { status: "complete", attempt: state.attempt, cursor, output },
172
+ effects: [CANCEL, { type: "cache", cursor, output, complete: cursor >= page.size }, emit],
173
+ }
174
+ }
175
+
176
+ function runningChanged(
177
+ state: Exclude<ShellFollowerState, { status: "disposed" | "missing" }>,
178
+ running: boolean,
179
+ ): ShellFollowerTransition {
180
+ if (state.status === "complete" || state.status === "failed") {
181
+ if (!running) return { state, effects: NO_EFFECTS }
182
+ const next = read(state, true, 0)
183
+
184
+ if (state.status === "failed") return next
185
+
186
+ return {
187
+ state: next.state,
188
+ effects: [{ type: "cache", cursor: state.cursor, output: state.output, complete: false }, ...next.effects],
189
+ }
190
+ }
191
+
192
+ if (running === state.running) return { state, effects: NO_EFFECTS }
193
+
194
+ // A shell that stops gets the full retry budget of an exited shell, however often its live polls failed.
195
+ // The read in flight still counts; the next one decides whether the output is final.
196
+ if (state.status === "reading")
197
+ return {
198
+ state: { ...state, running, final: state.final && !running, failures: running ? state.failures : 0 },
199
+ effects: NO_EFFECTS,
200
+ }
201
+
202
+ if (running) return { state: { ...state, running }, effects: NO_EFFECTS }
203
+
204
+ // The shell stopped between polls or during a backoff: read its final output now.
205
+ return read(state, false, 0)
206
+ }
207
+
208
+ function read(state: Progress, running: boolean, failures: number): ShellFollowerTransition {
209
+ const attempt = state.attempt + 1
210
+
211
+ return {
212
+ state: {
213
+ status: "reading",
214
+ attempt,
215
+ cursor: state.cursor,
216
+ output: state.output,
217
+ running,
218
+ final: !running,
219
+ failures,
220
+ },
221
+ effects: [{ type: "read", attempt, cursor: state.cursor }],
222
+ }
223
+ }
224
+
225
+ function backoff(state: Progress, running: boolean, failures: number): ShellFollowerTransition {
226
+ const attempt = state.attempt + 1
227
+ const delayMs = Math.min(SHELL_MAX_BACKOFF_MS, SHELL_POLL_MS * 2 ** (failures - 1))
228
+
229
+ return {
230
+ state: { status: "waiting", attempt, cursor: state.cursor, output: state.output, running, failures, delayMs },
231
+ effects: [{ type: "wait", attempt, delayMs }],
232
+ }
233
+ }
@@ -0,0 +1,91 @@
1
+ import { isShellNotFoundError, type ShellOutputInput, type ShellOutputOutput } from "@zyro-sec/client/promise"
2
+ import {
3
+ createShellFollowerState,
4
+ SHELL_READ_TIMEOUT_MS,
5
+ transitionShellFollower,
6
+ type ShellCachedOutput,
7
+ type ShellFollowerEffect,
8
+ type ShellFollowerEvent,
9
+ } from "./shell-output-machine"
10
+
11
+ // Virtualized timelines remount shell tools frequently. Output is remembered per shell so a remount resumes from its
12
+ // cursor instead of re-reading from zero. Missing shells are remembered apart from the output, which holds up to 64 KiB
13
+ // per shell, so viewing many other shells cannot evict them and a shell the server dropped is never requested again.
14
+ const outputs = new Map<string, ShellCachedOutput>()
15
+
16
+ const missing = new Map<string, true>()
17
+
18
+ /**
19
+ * Follows one shell's output, polling while it runs and reading its final output once it stops. `setRunning` keeps the
20
+ * read in flight, so a late running-shell inventory does not drop it.
21
+ */
22
+ export function createShellFollower(input: {
23
+ id: string
24
+ directory: string
25
+ running: boolean
26
+ load: (input: ShellOutputInput) => Promise<ShellOutputOutput>
27
+ onOutput: (output: string) => void
28
+ }) {
29
+ const cached = outputs.get(input.id)
30
+
31
+ if (cached) remember(outputs, input.id, cached, 32)
32
+ const initial = createShellFollowerState({ cached, missing: missing.has(input.id), running: input.running })
33
+ let state = initial.state
34
+ let timer: ReturnType<typeof setTimeout> | undefined
35
+
36
+ const dispatch = (event: ShellFollowerEvent) => {
37
+ const next = transitionShellFollower(state, event)
38
+ state = next.state
39
+ next.effects.forEach(run)
40
+ }
41
+
42
+ const run = (effect: ShellFollowerEffect) => {
43
+ if (effect.type === "emit") return input.onOutput(effect.output)
44
+
45
+ if (effect.type === "cache") {
46
+ remember(outputs, input.id, { cursor: effect.cursor, output: effect.output, complete: effect.complete }, 32)
47
+
48
+ return
49
+ }
50
+
51
+ if (effect.type === "cache-missing") return remember(missing, input.id, true, 512)
52
+ clearTimeout(timer)
53
+ timer = undefined
54
+
55
+ if (effect.type === "cancel") return
56
+
57
+ if (effect.type === "wait") {
58
+ timer = setTimeout(() => dispatch({ type: "wait.elapsed", attempt: effect.attempt }), effect.delayMs)
59
+
60
+ return
61
+ }
62
+
63
+ const attempt = effect.attempt
64
+ timer = setTimeout(() => dispatch({ type: "read.failed", attempt }), SHELL_READ_TIMEOUT_MS)
65
+ void input.load({ id: input.id, location: { directory: input.directory }, cursor: effect.cursor }).then(
66
+ (response) => dispatch({ type: "read.succeeded", attempt, page: response.data }),
67
+ (cause: unknown) => dispatch({ type: isShellNotFoundError(cause) ? "read.missing" : "read.failed", attempt }),
68
+ )
69
+ }
70
+
71
+ initial.effects.forEach(run)
72
+
73
+ return {
74
+ setRunning: (running: boolean) => dispatch({ type: "running.changed", running }),
75
+ dispose: () => dispatch({ type: "disposed" }),
76
+ }
77
+ }
78
+
79
+ export function followShellOutput(input: Parameters<typeof createShellFollower>[0]) {
80
+ return createShellFollower(input).dispose
81
+ }
82
+
83
+ function remember<T>(cache: Map<string, T>, id: string, entry: T, limit: number) {
84
+ cache.delete(id)
85
+ cache.set(id, entry)
86
+
87
+ if (cache.size <= limit) return
88
+ const oldest = cache.keys().next().value
89
+
90
+ if (oldest !== undefined) cache.delete(oldest)
91
+ }