@kabmo-labs/kabmo 0.24.0 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (325) hide show
  1. package/dist/client/index.html +1 -1
  2. package/dist/client/index.js +585 -512
  3. package/dist/client/static/js/{index-jcr5twtp.js → index-95b3n20k.js} +1105 -1207
  4. package/dist/client/static/js/index-p2yw9ywf.css +1 -0
  5. package/package.json +2 -4
  6. package/src/cli/lib/args.js +24 -1
  7. package/src/cli/lib/args.test.js +16 -0
  8. package/src/cli/lib/commands/session.js +233 -0
  9. package/src/cli/lib/commands/session.test.js +103 -0
  10. package/src/cli/main.js +1 -1
  11. package/src/client/components/common/ComposerPicker.tsx +3 -0
  12. package/src/client/components/common/code-surface/shiki.test.ts +82 -0
  13. package/src/client/components/common/code-surface/shiki.ts +28 -7
  14. package/src/client/components/common/session-actions-menu/index.tsx +59 -2
  15. package/src/client/components/common/session-branch/BranchGuides.tsx +93 -0
  16. package/src/client/components/common/session-branch/SubagentRow.tsx +77 -0
  17. package/src/client/components/common/session-branch/index.tsx +169 -0
  18. package/src/client/components/common/session-branch/session-branch.test.tsx +240 -0
  19. package/src/client/components/common/session-branch/style.ts +29 -0
  20. package/src/client/components/common/session-row/StatusSlot.tsx +17 -5
  21. package/src/client/components/common/session-row/index.tsx +28 -3
  22. package/src/client/components/common/session-row/session-row.test.tsx +11 -0
  23. package/src/client/components/common/session-state-provider/index.tsx +1 -1
  24. package/src/client/components/layout/desktop-layout/resizer/separator-drag.ts +3 -3
  25. package/src/client/components/layout/session-sidebar/CategoryItem.tsx +70 -61
  26. package/src/client/components/layout/session-sidebar/SessionItem.batch.test.ts +3 -3
  27. package/src/client/components/layout/session-sidebar/SessionItem.test.ts +25 -2
  28. package/src/client/components/layout/session-sidebar/SessionItem.tsx +35 -7
  29. package/src/client/components/layout/session-sidebar/about-modal/HarnessRow.tsx +261 -0
  30. package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.tsx +39 -198
  31. package/src/client/components/layout/session-sidebar/about-modal/HarnessTableLog.test.tsx +114 -0
  32. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.test.tsx +130 -0
  33. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.tsx +128 -0
  34. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.tsx +56 -0
  35. package/src/client/components/layout/session-sidebar/about-modal/index.tsx +44 -36
  36. package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +62 -47
  37. package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +63 -50
  38. package/src/client/components/mobile/mobile-session-sidebar/SessionRow.test.ts +5 -0
  39. package/src/client/components/mobile/mobile-session-sidebar/SessionRow.tsx +37 -12
  40. package/src/client/components/settings/Sidebar.tsx +1 -1
  41. package/src/client/components/settings/categories/NotificationSettings.test.tsx +92 -0
  42. package/src/client/components/settings/categories/NotificationSettings.tsx +112 -2
  43. package/src/client/components/settings/categories/harness-settings/HarnessCard.tsx +1 -1
  44. package/src/client/components/workspace/chat-timeline/AiMessageFooter.fork.test.ts +94 -0
  45. package/src/client/components/workspace/chat-timeline/AiMessageFooter.tsx +31 -5
  46. package/src/client/components/workspace/chat-timeline/ComposerDock.tsx +64 -70
  47. package/src/client/components/workspace/chat-timeline/ComposerDockFrame.tsx +35 -0
  48. package/src/client/components/workspace/chat-timeline/EmptyWorkspacePrompt.tsx +128 -122
  49. package/src/client/components/workspace/chat-timeline/MessageItem.tsx +22 -1
  50. package/src/client/components/workspace/chat-timeline/MessageList.tsx +11 -5
  51. package/src/client/components/workspace/chat-timeline/PendingChatView.tsx +10 -0
  52. package/src/client/components/workspace/chat-timeline/RunFooter.tsx +12 -3
  53. package/src/client/components/workspace/chat-timeline/TimelineBody.tsx +3 -0
  54. package/src/client/components/workspace/chat-timeline/chat-input/AttachmentToolbar.tsx +2 -0
  55. package/src/client/components/workspace/chat-timeline/chat-input/ModeToggles.tsx +15 -1
  56. package/src/client/components/workspace/chat-timeline/chat-input/SessionModeDropdown.test.tsx +85 -0
  57. package/src/client/components/workspace/chat-timeline/chat-input/SessionModeDropdown.tsx +84 -0
  58. package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.tsx +2 -0
  59. package/src/client/components/workspace/chat-timeline/chat-input/modes-props.ts +5 -0
  60. package/src/client/components/workspace/chat-timeline/composer-dock-frame.test.tsx +155 -0
  61. package/src/client/components/workspace/chat-timeline/index.tsx +11 -20
  62. package/src/client/components/workspace/chat-timeline/target-picker/DraftTargetRow.tsx +115 -0
  63. package/src/client/components/workspace/chat-timeline/target-picker/ParentSessionPicker.tsx +173 -0
  64. package/src/client/components/workspace/chat-timeline/target-picker/parent-session-options.test.ts +49 -0
  65. package/src/client/components/workspace/terminal-panel/KeyBar.tsx +10 -8
  66. package/src/client/components/workspace/terminal-panel/{RealtimeXtermView.tsx → RealtimeTerminalView.tsx} +51 -58
  67. package/src/client/components/workspace/terminal-panel/fit.test.ts +37 -0
  68. package/src/client/components/workspace/terminal-panel/fit.ts +64 -0
  69. package/src/client/components/workspace/terminal-panel/fonts.test.ts +161 -0
  70. package/src/client/components/workspace/terminal-panel/fonts.ts +164 -0
  71. package/src/client/components/workspace/terminal-panel/grid.test.ts +52 -0
  72. package/src/client/components/workspace/terminal-panel/grid.ts +118 -0
  73. package/src/client/components/workspace/terminal-panel/index.tsx +25 -21
  74. package/src/client/components/workspace/terminal-panel/mount.ts +247 -223
  75. package/src/client/components/workspace/terminal-panel/navigation.test.ts +54 -0
  76. package/src/client/components/workspace/terminal-panel/navigation.ts +47 -0
  77. package/src/client/data/settings/omp-schema.json +1 -1
  78. package/src/client/data/theme/terminal.test.ts +25 -0
  79. package/src/client/data/theme/terminal.ts +96 -79
  80. package/src/client/hooks/chat/composer/items.test.ts +31 -26
  81. package/src/client/hooks/chat/composer/pick-mapping.test.ts +42 -0
  82. package/src/client/hooks/chat/omp/folder-auto-expand.ts +1 -1
  83. package/src/client/hooks/chat/omp/pending-adoption.ts +51 -0
  84. package/src/client/hooks/chat/omp/pending-child.test.ts +73 -0
  85. package/src/client/hooks/chat/omp/pending-entry.ts +59 -0
  86. package/src/client/hooks/chat/omp/pending-rows.ts +6 -2
  87. package/src/client/hooks/chat/omp/pending-sessions.test.ts +1 -2
  88. package/src/client/hooks/chat/omp/pending-sessions.ts +45 -69
  89. package/src/client/hooks/chat/omp/pending-waiting.ts +1 -1
  90. package/src/client/hooks/chat/omp/prompt-send.test.ts +7 -7
  91. package/src/client/hooks/chat/omp/prompt-send.ts +26 -14
  92. package/src/client/hooks/chat/omp/run-switch.ts +1 -1
  93. package/src/client/hooks/chat/omp/session-list.tsx +4 -0
  94. package/src/client/hooks/chat/subagents/roster.ts +122 -0
  95. package/src/client/hooks/chat/timeline/child-spawn.test.ts +55 -0
  96. package/src/client/hooks/chat/timeline/child-spawn.ts +45 -0
  97. package/src/client/hooks/chat/timeline/fork-request.test.ts +54 -0
  98. package/src/client/hooks/chat/timeline/fork-request.ts +43 -0
  99. package/src/client/hooks/chat/timeline/mode-availability.test.ts +1 -1
  100. package/src/client/hooks/chat/timeline/mode-availability.ts +33 -0
  101. package/src/client/hooks/chat/timeline/mode-markers.ts +7 -1
  102. package/src/client/hooks/chat/timeline/mode-post.ts +15 -3
  103. package/src/client/hooks/chat/timeline/modes-contract.ts +9 -4
  104. package/src/client/hooks/chat/timeline/modes.test.ts +56 -18
  105. package/src/client/hooks/chat/timeline/modes.ts +42 -29
  106. package/src/client/hooks/chat/timeline/pending-branch.ts +205 -0
  107. package/src/client/hooks/chat/timeline/pending-dispatch.ts +253 -0
  108. package/src/client/hooks/chat/timeline/prompt-text.test.ts +34 -1
  109. package/src/client/hooks/chat/timeline/prompt-text.ts +47 -20
  110. package/src/client/hooks/chat/timeline/send-execute.test.ts +39 -0
  111. package/src/client/hooks/chat/timeline/send-helpers.ts +30 -1
  112. package/src/client/hooks/chat/timeline/send-steer.test.ts +3 -1
  113. package/src/client/hooks/chat/timeline/send.test-util.ts +7 -1
  114. package/src/client/hooks/chat/timeline/send.test.ts +3 -1
  115. package/src/client/hooks/chat/timeline/send.ts +48 -133
  116. package/src/client/hooks/chat/timeline/session-harness.ts +6 -0
  117. package/src/client/hooks/chat/timeline/stop-with-queue.ts +42 -0
  118. package/src/client/hooks/chat/timeline/use-fork-session.ts +58 -0
  119. package/src/client/hooks/models/use-harness-update.test.ts +286 -0
  120. package/src/client/hooks/models/use-harness-update.ts +250 -0
  121. package/src/client/hooks/ui/input-required-alert.ts +12 -5
  122. package/src/client/hooks/ui/notifications.ts +45 -0
  123. package/src/client/hooks/ui/run-outcome-alert.ts +48 -0
  124. package/src/client/hooks/workspace/expanded-sessions.ts +36 -7
  125. package/src/client/hooks/workspace/terminal/index.ts +17 -3
  126. package/src/client/hooks/workspace/terminal/socket.ts +9 -4
  127. package/src/client/lib/native/open-session.ts +33 -0
  128. package/src/client/lib/notifications/os.test.ts +125 -0
  129. package/src/client/lib/notifications/os.ts +130 -0
  130. package/src/client/lib/plugins/sidebar-bridge.ts +2 -20
  131. package/src/client/lib/router/open-session.test.ts +75 -0
  132. package/src/client/lib/router/open-session.ts +34 -0
  133. package/src/client/lib/session-tree.test.ts +50 -0
  134. package/src/client/lib/session-tree.ts +59 -0
  135. package/src/client/lib/signals.ts +5 -1
  136. package/src/client/main.tsx +6 -0
  137. package/src/client/tailwind.css +17 -11
  138. package/src/server/index.ts +2 -0
  139. package/src/server/lib/auth/guard.ts +10 -0
  140. package/src/server/lib/btw/session-copy.server.ts +6 -44
  141. package/src/server/lib/btw/session-copy.test.ts +2 -1
  142. package/src/server/lib/db/schema.ts +5 -0
  143. package/src/server/lib/db/seed-terminal.test.ts +1 -1
  144. package/src/server/lib/db/seed.test.ts +1 -1
  145. package/src/server/lib/harness/acp/background-work.test.ts +262 -0
  146. package/src/server/lib/harness/acp/background-work.ts +320 -0
  147. package/src/server/lib/harness/acp/calls.ts +15 -1
  148. package/src/server/lib/harness/acp/client-capabilities.test.ts +55 -0
  149. package/src/server/lib/harness/acp/client-capabilities.ts +61 -0
  150. package/src/server/lib/harness/acp/client-fs.test.ts +135 -0
  151. package/src/server/lib/harness/acp/client-fs.ts +131 -0
  152. package/src/server/lib/harness/acp/client-initialize.test.ts +70 -0
  153. package/src/server/lib/harness/acp/client-initialize.ts +84 -0
  154. package/src/server/lib/harness/acp/client.ts +33 -23
  155. package/src/server/lib/harness/acp/commands.test.ts +5 -5
  156. package/src/server/lib/harness/acp/commands.ts +14 -2
  157. package/src/server/lib/harness/acp/frames-timing.test.ts +68 -0
  158. package/src/server/lib/harness/acp/frames-v2.test.ts +85 -0
  159. package/src/server/lib/harness/acp/frames.ts +71 -158
  160. package/src/server/lib/harness/acp/gateway-env.test.ts +64 -0
  161. package/src/server/lib/harness/acp/gateway-env.ts +70 -0
  162. package/src/server/lib/harness/acp/mapper.test.ts +40 -0
  163. package/src/server/lib/harness/acp/mapper.ts +12 -0
  164. package/src/server/lib/harness/acp/permissions.ts +6 -0
  165. package/src/server/lib/harness/acp/prompt.test.ts +20 -0
  166. package/src/server/lib/harness/acp/prompt.ts +19 -3
  167. package/src/server/lib/harness/acp/protocol.ts +41 -2
  168. package/src/server/lib/harness/acp/session-background.test.ts +129 -0
  169. package/src/server/lib/harness/acp/session-exit.ts +31 -0
  170. package/src/server/lib/harness/acp/session-facts.ts +1 -1
  171. package/src/server/lib/harness/acp/session-handshake.test.ts +197 -0
  172. package/src/server/lib/harness/acp/session-handshake.ts +110 -7
  173. package/src/server/lib/harness/acp/session-idle.ts +7 -6
  174. package/src/server/lib/harness/acp/session-listeners.ts +31 -0
  175. package/src/server/lib/harness/acp/session-notifications.ts +41 -2
  176. package/src/server/lib/harness/acp/session-observers.test.ts +63 -0
  177. package/src/server/lib/harness/acp/session-permissions.test.ts +29 -0
  178. package/src/server/lib/harness/acp/session-permissions.ts +36 -0
  179. package/src/server/lib/harness/acp/session-requests.test.ts +92 -0
  180. package/src/server/lib/harness/acp/session-requests.ts +89 -0
  181. package/src/server/lib/harness/acp/session-turns.ts +28 -8
  182. package/src/server/lib/harness/acp/session-usage.ts +4 -0
  183. package/src/server/lib/harness/acp/session-web-state.ts +30 -2
  184. package/src/server/lib/harness/acp/session.test-util.ts +42 -2
  185. package/src/server/lib/harness/acp/session.ts +115 -115
  186. package/src/server/lib/harness/acp/tool-calls.ts +223 -0
  187. package/src/server/lib/harness/acp/turn.test.ts +183 -0
  188. package/src/server/lib/harness/acp/turn.ts +102 -10
  189. package/src/server/lib/harness/acp/v2.test.ts +144 -0
  190. package/src/server/lib/harness/acp/v2.ts +220 -0
  191. package/src/server/lib/harness/acp/verbs.ts +58 -8
  192. package/src/server/lib/harness/launch.test.ts +25 -0
  193. package/src/server/lib/harness/launch.ts +24 -10
  194. package/src/server/lib/harness/session.ts +3 -0
  195. package/src/server/lib/harness/spawn.server.ts +15 -2
  196. package/src/server/lib/harness/update-job-store.test.ts +151 -0
  197. package/src/server/lib/harness/update-job-store.ts +115 -0
  198. package/src/server/lib/harness/update-job.server.ts +94 -0
  199. package/src/server/lib/harness/update-queue.server.ts +292 -0
  200. package/src/server/lib/harness/update-queue.test.ts +265 -0
  201. package/src/server/lib/harness/update-spawn.server.ts +96 -0
  202. package/src/server/lib/harness/update-worker.test.ts +153 -0
  203. package/src/server/lib/harness/update-worker.ts +202 -0
  204. package/src/server/lib/lifecycle/after-listen.ts +9 -0
  205. package/src/server/lib/lifecycle/paths.ts +28 -0
  206. package/src/server/lib/omp/rpc/manager.ts +5 -5
  207. package/src/server/lib/omp/rpc/prewarm-pool.ts +12 -3
  208. package/src/server/lib/omp/rpc/run-settle.server.ts +10 -1
  209. package/src/server/lib/omp/rpc/session-registry.ts +14 -2
  210. package/src/server/lib/omp/rpc/subagent-liveness.ts +5 -5
  211. package/src/server/lib/omp/rpc/terminal-settle.ts +58 -6
  212. package/src/server/lib/omp/session/delete-flow.server.ts +48 -0
  213. package/src/server/lib/omp/session/delete.server.ts +5 -0
  214. package/src/server/lib/omp/session/rewind-file.ts +39 -21
  215. package/src/server/lib/omp/session/sidebar-data.server.ts +16 -18
  216. package/src/server/lib/omp/session/transcript-copy.ts +123 -0
  217. package/src/server/lib/sessions/agent-token.server.test.ts +64 -0
  218. package/src/server/lib/sessions/agent-token.server.ts +134 -0
  219. package/src/server/lib/sessions/answers.server.test.ts +33 -0
  220. package/src/server/lib/sessions/answers.server.ts +37 -0
  221. package/src/server/lib/sessions/dispatch.server.test.ts +143 -0
  222. package/src/server/lib/sessions/dispatch.server.ts +128 -0
  223. package/src/server/lib/sessions/fork.server.test.ts +277 -0
  224. package/src/server/lib/sessions/fork.server.ts +329 -0
  225. package/src/server/lib/sessions/lifecycle.server.test.ts +75 -0
  226. package/src/server/lib/sessions/lifecycle.server.ts +71 -0
  227. package/src/server/lib/sessions/notify.server.test.ts +112 -0
  228. package/src/server/lib/sessions/notify.server.ts +212 -0
  229. package/src/server/lib/sessions/relation-types.ts +76 -0
  230. package/src/server/lib/sessions/relations.server.test.ts +207 -0
  231. package/src/server/lib/sessions/relations.server.ts +337 -0
  232. package/src/server/lib/sessions/resolve.server.ts +106 -0
  233. package/src/server/lib/sessions/schema.server.ts +82 -0
  234. package/src/server/lib/sessions/sidebar-relations.server.test.ts +61 -0
  235. package/src/server/lib/sessions/sidebar-relations.server.ts +47 -0
  236. package/src/server/lib/sessions/spawn.server.test.ts +237 -0
  237. package/src/server/lib/sessions/spawn.server.ts +335 -0
  238. package/src/server/lib/terminal/attach-replay.test.ts +2 -2
  239. package/src/server/lib/terminal/output.test.ts +2 -2
  240. package/src/server/lib/terminal/output.ts +1 -1
  241. package/src/server/lib/terminal/resize.test.ts +99 -0
  242. package/src/server/lib/terminal/runtime.server.ts +26 -6
  243. package/src/server/lib/terminal/send-result.test.ts +18 -1
  244. package/src/server/lib/terminal/send-result.ts +26 -2
  245. package/src/server/lib/terminal/session.server.ts +6 -6
  246. package/src/server/lib/usage/new-api.test.ts +92 -0
  247. package/src/server/lib/usage/new-api.ts +131 -0
  248. package/src/server/lib/usage/provider-endpoint.test.ts +106 -0
  249. package/src/server/lib/usage/provider-key.ts +33 -0
  250. package/src/server/lib/usage/providers.server.ts +10 -3
  251. package/src/server/lib/usage/vendor-balance.server.ts +4 -53
  252. package/src/server/lib/usage/vendor-http.ts +41 -0
  253. package/src/server/lib/usage/window.ts +55 -0
  254. package/src/server/routes/agent/command.ts +14 -1
  255. package/src/server/routes/agent/harness-command.ts +12 -5
  256. package/src/server/routes/agent/new.ts +15 -0
  257. package/src/server/routes/chat/stream.ts +7 -2
  258. package/src/server/routes/harness/add-provider.test.ts +9 -1
  259. package/src/server/routes/harness/import-action.ts +4 -0
  260. package/src/server/routes/harness/import.test.ts +1 -1
  261. package/src/server/routes/harness/index.ts +5 -1
  262. package/src/server/routes/harness/update-status.ts +32 -0
  263. package/src/server/routes/harness/update.test.ts +156 -0
  264. package/src/server/routes/harness/update.ts +64 -147
  265. package/src/server/routes/sessions/answer.ts +24 -0
  266. package/src/server/routes/sessions/batch-delete.ts +11 -5
  267. package/src/server/routes/sessions/body-readers.ts +43 -0
  268. package/src/server/routes/sessions/delete.ts +4 -2
  269. package/src/server/routes/sessions/fork.ts +89 -0
  270. package/src/server/routes/sessions/index.ts +14 -0
  271. package/src/server/routes/sessions/relations.ts +85 -0
  272. package/src/server/routes/sessions/routes.test.ts +1 -2
  273. package/src/server/routes/sessions/session.ts +23 -0
  274. package/src/server/routes/sessions/spawn.ts +137 -0
  275. package/src/server/routes/terminal/ws.ts +3 -3
  276. package/src/shared/lib/chat/composer/client.ts +31 -2
  277. package/src/shared/lib/chat/composer/delegation.test.ts +93 -0
  278. package/src/shared/lib/chat/composer/delegation.ts +206 -0
  279. package/src/shared/lib/chat/notice-text.ts +1 -1
  280. package/src/shared/lib/chat/omp/agent-events.ts +11 -0
  281. package/src/shared/lib/chat/omp/session-modes-signal.test.ts +41 -0
  282. package/src/shared/lib/chat/tool/summary-wait.test.ts +11 -0
  283. package/src/shared/lib/code/ansi.ts +1 -1
  284. package/src/shared/lib/fonts/nerd-symbols.css +4 -3
  285. package/src/shared/lib/fonts/nerd-symbols.test.ts +9 -9
  286. package/src/shared/lib/harness/mode-signal.ts +20 -0
  287. package/src/shared/lib/harness/provider-facts.test.ts +27 -0
  288. package/src/shared/lib/harness/providers.ts +35 -1
  289. package/src/shared/lib/harness/session-modes.ts +24 -0
  290. package/src/shared/lib/harness/update-job.ts +80 -0
  291. package/src/shared/lib/harness/update-plan.test.ts +21 -6
  292. package/src/shared/lib/harness/update-plan.ts +12 -4
  293. package/src/shared/lib/icons/tint-hues.generated.ts +34 -34
  294. package/src/shared/lib/notifications/outcomes.test.ts +96 -0
  295. package/src/shared/lib/notifications/outcomes.ts +64 -0
  296. package/src/shared/lib/notifications/settings.test.ts +49 -0
  297. package/src/shared/lib/notifications/settings.ts +34 -0
  298. package/src/shared/lib/omp/session/default-title.ts +5 -4
  299. package/src/shared/lib/omp/session/prewarm-misc.test.ts +3 -2
  300. package/src/shared/lib/omp/session/schema-migrations.ts +4 -0
  301. package/src/shared/lib/omp/session/stream-state.server.test.ts +19 -0
  302. package/src/shared/lib/omp/session/stream-state.server.ts +24 -4
  303. package/src/shared/lib/omp/session/wait-idle.test.ts +11 -0
  304. package/src/shared/lib/omp/session/wait-idle.ts +12 -2
  305. package/src/shared/lib/settings/defaults.ts +3 -0
  306. package/src/shared/lib/theme/catalog.test.ts +13 -9
  307. package/src/shared/lib/theme/color.test.ts +2 -2
  308. package/src/shared/lib/theme/color.ts +4 -4
  309. package/src/shared/lib/theme/theme.test.ts +1 -1
  310. package/src/shared/lib/workspace/terminal/input.test.ts +27 -0
  311. package/src/shared/lib/workspace/terminal/input.ts +33 -0
  312. package/src/shared/lib/workspace/terminal/keys.test.ts +1 -1
  313. package/src/shared/lib/workspace/terminal/keys.ts +7 -7
  314. package/src/shared/types/chat.ts +1 -1
  315. package/src/shared/types/harness-update.ts +89 -0
  316. package/src/shared/types/omp/agent.ts +19 -2
  317. package/src/shared/types/realtime.ts +2 -0
  318. package/src/shared/types/settings/state.ts +10 -0
  319. package/src/shared/types/workspace.ts +8 -0
  320. package/src/test-support/fake-omp-fork.ts +16 -2
  321. package/src/test-support/harness-table.test-util.ts +31 -1
  322. package/dist/client/static/js/index-z3w7n85q.css +0 -1
  323. package/src/client/components/common/subagent-list/index.tsx +0 -165
  324. package/src/client/components/workspace/terminal-panel/webgl.test.ts +0 -75
  325. package/src/client/components/workspace/terminal-panel/webgl.ts +0 -108
@@ -0,0 +1,220 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * ACP v2 normalization.
8
+ *
9
+ * Kabmo's internal model is v1-shaped: the frame mapper, the turn machine, the
10
+ * permission queue and every panel read v1 field names. That is deliberate — a
11
+ * second mapper would double every rule — so v2 support is an ADAPTER at the
12
+ * process boundary: `initialize` results, session setup results, config option
13
+ * lists, permission requests and the few update variants that changed shape are
14
+ * translated here, and the rest of the app stays unaware of the version.
15
+ *
16
+ * The prompt lifecycle is the one thing an adapter cannot hide: v2's
17
+ * `session/prompt` response acknowledges insertion, not completion. That is
18
+ * handled by `AcpTurns.completion = 'state'`, not by pretending the response
19
+ * ended the turn.
20
+ */
21
+
22
+ import type {
23
+ AcpAgentCommand,
24
+ AcpConfigOption,
25
+ AcpInitializeResult,
26
+ AcpSessionNewResult,
27
+ } from '@/server/lib/harness/acp/protocol';
28
+
29
+ /** A parsed JSON object, or undefined when the value is not one. */
30
+ export function asRecord(value: unknown): Record<string, unknown> | undefined {
31
+ return value && typeof value === 'object' && !Array.isArray(value)
32
+ ? Object.fromEntries(Object.entries(value))
33
+ : undefined;
34
+ }
35
+
36
+ export function asString(value: unknown): string | undefined {
37
+ return typeof value === 'string' ? value : undefined;
38
+ }
39
+
40
+ /** A support marker is an object in v2; `{}` means supported. */
41
+ function advertised(value: unknown): boolean {
42
+ return value !== undefined && value !== null;
43
+ }
44
+
45
+ /** A v2 initialize result, in the v1 shape the rest of Kabmo reads. */
46
+ export function normalizeInitializeResult(raw: Record<string, unknown>): AcpInitializeResult {
47
+ const version = typeof raw.protocolVersion === 'number' ? raw.protocolVersion : 1;
48
+ if (version !== 2) return raw as unknown as AcpInitializeResult;
49
+
50
+ const capabilities = asRecord(raw.capabilities) ?? {};
51
+ const session = asRecord(capabilities.session);
52
+ const prompt = asRecord(session?.prompt);
53
+ const mcp = asRecord(session?.mcp);
54
+ const info = asRecord(raw.info);
55
+ const meta = {
56
+ ...(asRecord(capabilities._meta) ?? {}),
57
+ ...(asRecord(raw._meta) ?? {}),
58
+ };
59
+
60
+ // `capabilities.session` commits the agent to the whole baseline: list,
61
+ // resume and close have no individual markers in v2.
62
+ const sessionCapabilities: Record<string, unknown> = {};
63
+ if (session !== undefined) {
64
+ sessionCapabilities.list = {};
65
+ sessionCapabilities.resume = {};
66
+ sessionCapabilities.close = {};
67
+ if (advertised(session.delete)) sessionCapabilities.delete = {};
68
+ if (advertised(session.additionalDirectories)) sessionCapabilities.additionalDirectories = {};
69
+ if (advertised(session.fork)) sessionCapabilities.fork = {};
70
+ }
71
+
72
+ return {
73
+ protocolVersion: 2,
74
+ ...(info?.name !== undefined
75
+ ? {
76
+ agentInfo: {
77
+ name: asString(info.name),
78
+ ...(asString(info.title) ? { title: asString(info.title) } : {}),
79
+ ...(asString(info.version) ? { version: asString(info.version) } : {}),
80
+ },
81
+ }
82
+ : {}),
83
+ agentCapabilities: {
84
+ loadSession: false,
85
+ promptCapabilities: {
86
+ image: advertised(prompt?.image),
87
+ audio: advertised(prompt?.audio),
88
+ embeddedContext: advertised(prompt?.embeddedContext),
89
+ },
90
+ mcpCapabilities: { http: advertised(mcp?.http), sse: false },
91
+ sessionCapabilities,
92
+ },
93
+ authMethods: normalizeAuthMethods(raw.authMethods),
94
+ ...(Object.keys(meta).length > 0 ? { _meta: meta } : {}),
95
+ };
96
+ }
97
+
98
+ /** v2 auth descriptors use `methodId`; internal consumers use `id`. */
99
+ export function normalizeAuthMethods(value: unknown): AcpInitializeResult['authMethods'] {
100
+ if (!Array.isArray(value)) return [];
101
+ return value.flatMap((entry) => {
102
+ const method = asRecord(entry);
103
+ const id = asString(method?.methodId) ?? asString(method?.id);
104
+ if (!id) return [];
105
+ return [{
106
+ id,
107
+ name: asString(method?.name) ?? id,
108
+ ...(asString(method?.description) ? { description: asString(method?.description) } : {}),
109
+ ...(asString(method?.type) ? { type: asString(method?.type) } : {}),
110
+ }];
111
+ });
112
+ }
113
+
114
+ /** A v2 config option list, with `configId`/`groupId` renamed to v1 keys. */
115
+ export function normalizeConfigOptions(value: unknown): AcpConfigOption[] {
116
+ if (!Array.isArray(value)) return [];
117
+ return value.flatMap((entry) => {
118
+ const option = asRecord(entry);
119
+ const id = asString(option?.configId) ?? asString(option?.id);
120
+ if (!option || !id) return [];
121
+ return [{
122
+ ...option,
123
+ id,
124
+ ...(Array.isArray(option.options) ? { options: normalizeSelectEntries(option.options) } : {}),
125
+ } as AcpConfigOption];
126
+ });
127
+ }
128
+
129
+ function normalizeSelectEntries(value: readonly unknown[]): NonNullable<AcpConfigOption['options']> {
130
+ const out: NonNullable<AcpConfigOption['options']> = [];
131
+ for (const entry of value) {
132
+ const item = asRecord(entry);
133
+ if (!item) continue;
134
+ const groupId = asString(item.groupId) ?? asString(item.group);
135
+ if (groupId && Array.isArray(item.options)) {
136
+ out.push({
137
+ group: groupId,
138
+ ...(asString(item.name) ? { name: asString(item.name) } : {}),
139
+ options: item.options.flatMap((nested) => {
140
+ const select = asRecord(nested);
141
+ const choice = asString(select?.value);
142
+ return choice ? [{ value: choice, ...(asString(select?.name) ? { name: asString(select?.name) } : {}) }] : [];
143
+ }),
144
+ });
145
+ continue;
146
+ }
147
+ const choice = asString(item.value);
148
+ if (choice) {
149
+ out.push({
150
+ value: choice,
151
+ ...(asString(item.name) ? { name: asString(item.name) } : {}),
152
+ ...(asString(item.description) ? { description: asString(item.description) } : {}),
153
+ });
154
+ }
155
+ }
156
+ return out;
157
+ }
158
+
159
+ /** A v2 slash-command list, in the shape `parseAgentCommands` produces. */
160
+ export function normalizeCommands(value: unknown): AcpAgentCommand[] {
161
+ if (!Array.isArray(value)) return [];
162
+ return value.flatMap((entry) => {
163
+ const command = asRecord(entry);
164
+ const name = asString(command?.name);
165
+ if (!name) return [];
166
+ const input = asRecord(command?.input);
167
+ return [{
168
+ name,
169
+ ...(asString(command?.description) ? { description: asString(command?.description) } : {}),
170
+ ...(asString(input?.hint) ? { inputHint: asString(input?.hint) } : {}),
171
+ }];
172
+ });
173
+ }
174
+
175
+ /** A v2 `session/new|resume` result, normalized for the handshake. */
176
+ export function normalizeSessionResult(raw: Record<string, unknown>): AcpSessionNewResult {
177
+ return {
178
+ sessionId: asString(raw.sessionId) ?? '',
179
+ ...(Array.isArray(raw.configOptions) ? { configOptions: normalizeConfigOptions(raw.configOptions) } : {}),
180
+ ...(Array.isArray(raw.availableCommands) ? { availableCommands: normalizeCommands(raw.availableCommands) } : {}),
181
+ };
182
+ }
183
+
184
+ /**
185
+ * A v2 `session/request_permission`, in the shape the approval queue expects.
186
+ *
187
+ * v2 carries `title`, an optional `subject` (a tool call or a command) and the
188
+ * options. The queue keys on the tool call, so a command subject becomes an
189
+ * exec tool call and an absent subject becomes a titled generic one.
190
+ */
191
+ export function normalizePermissionParams(params: Record<string, unknown>): Record<string, unknown> {
192
+ const subject = asRecord(params.subject);
193
+ let toolCall: Record<string, unknown> | undefined;
194
+ if (subject?.type === 'tool_call') {
195
+ toolCall = asRecord(subject.toolCall);
196
+ } else if (subject?.type === 'command') {
197
+ toolCall = {
198
+ kind: 'execute',
199
+ rawInput: {
200
+ ...(asString(subject.command) ? { command: asString(subject.command) } : {}),
201
+ ...(asString(subject.cwd) ? { cwd: asString(subject.cwd) } : {}),
202
+ },
203
+ };
204
+ }
205
+ const title = asString(params.title) ?? 'Approve this action';
206
+ return {
207
+ ...params,
208
+ toolCall: {
209
+ ...(toolCall ?? {}),
210
+ title: asString(toolCall?.title) ?? title,
211
+ },
212
+ };
213
+ }
214
+
215
+ /** The entries a v2 `plan_update` replaces for its plan, or [] for an unknown type. */
216
+ export function planEntries(update: Record<string, unknown>): unknown[] {
217
+ const plan = asRecord(update.plan);
218
+ if (!plan || plan.type !== 'items' || !Array.isArray(plan.entries)) return [];
219
+ return plan.entries;
220
+ }
@@ -17,6 +17,7 @@
17
17
  */
18
18
 
19
19
  import type { AcpSessionInfo, AcpSessionNewResult } from '@/server/lib/harness/acp/protocol';
20
+ import { normalizeConfigOptions, normalizeSessionResult } from '@/server/lib/harness/acp/v2';
20
21
 
21
22
  /** One request/response round trip against an ACP agent. Implemented by
22
23
  * `AcpProcess.call`; a test supplies its own. */
@@ -24,20 +25,46 @@ export type AcpInvoke = <T>(method: string, params: unknown, timeoutMs: number)
24
25
  /** Fire-and-forget frame (notifications only). */
25
26
  export type AcpNotify = (method: string, params: unknown) => void;
26
27
 
27
- export async function newSession(invoke: AcpInvoke, cwd: string, timeoutMs = 120_000): Promise<AcpSessionNewResult> {
28
- return invoke<AcpSessionNewResult>('session/new', { cwd, mcpServers: [] }, timeoutMs);
28
+ /** The negotiated major, so a verb can shape its params and normalize its answer. */
29
+ export type AcpDialect = 1 | 2;
30
+
31
+ export async function newSession(
32
+ invoke: AcpInvoke,
33
+ cwd: string,
34
+ timeoutMs = 120_000,
35
+ dialect: AcpDialect = 1,
36
+ ): Promise<AcpSessionNewResult> {
37
+ const result = await invoke<Record<string, unknown>>('session/new', { cwd, mcpServers: [] }, timeoutMs);
38
+ return dialect === 2 ? normalizeSessionResult(result) : (result as unknown as AcpSessionNewResult);
29
39
  }
30
40
 
31
41
  export async function loadSession(invoke: AcpInvoke, sessionId: string, cwd: string, timeoutMs = 120_000): Promise<unknown> {
32
42
  return invoke('session/load', { sessionId, cwd, mcpServers: [] }, timeoutMs);
33
43
  }
34
44
 
35
- export async function resumeSession(invoke: AcpInvoke, sessionId: string, cwd: string, timeoutMs = 120_000): Promise<unknown> {
36
- return invoke('session/resume', { sessionId, cwd, mcpServers: [] }, timeoutMs);
45
+ export async function resumeSession(
46
+ invoke: AcpInvoke,
47
+ sessionId: string,
48
+ cwd: string,
49
+ timeoutMs = 120_000,
50
+ dialect: AcpDialect = 1,
51
+ ): Promise<unknown> {
52
+ const result = await invoke<Record<string, unknown>>('session/resume', { sessionId, cwd, mcpServers: [] }, timeoutMs);
53
+ return dialect === 2 ? normalizeSessionResult(result) : result;
37
54
  }
38
55
 
39
- export async function forkSession(invoke: AcpInvoke, sessionId: string, cwd: string, timeoutMs = 120_000): Promise<AcpSessionNewResult> {
40
- return invoke<AcpSessionNewResult>('session/fork', { sessionId, cwd, mcpServers: [] }, timeoutMs);
56
+ export async function forkSession(
57
+ invoke: AcpInvoke,
58
+ sessionId: string,
59
+ cwd: string,
60
+ timeoutMs = 120_000,
61
+ target?: Record<string, unknown>,
62
+ ): Promise<AcpSessionNewResult> {
63
+ return invoke<AcpSessionNewResult>(
64
+ 'session/fork',
65
+ { sessionId, cwd, mcpServers: [], ...(target ? { target } : {}) },
66
+ timeoutMs,
67
+ );
41
68
  }
42
69
 
43
70
  export async function listSessions(invoke: AcpInvoke, cwd?: string, timeoutMs = 60_000): Promise<AcpSessionInfo[]> {
@@ -53,7 +80,7 @@ export async function prompt(
53
80
  sessionId: string,
54
81
  blocks: unknown[],
55
82
  timeoutMs = 0,
56
- ): Promise<{ stopReason?: string; usage?: unknown }> {
83
+ ): Promise<{ stopReason?: string; messageId?: string; usage?: unknown }> {
57
84
  return invoke('session/prompt', { sessionId, prompt: blocks }, timeoutMs);
58
85
  }
59
86
 
@@ -67,8 +94,15 @@ export async function setConfigOption(
67
94
  configId: string,
68
95
  value: string | boolean,
69
96
  timeoutMs = 30_000,
97
+ dialect: AcpDialect = 1,
70
98
  ): Promise<unknown> {
71
- return invoke('session/set_config_option', { sessionId, configId, value }, timeoutMs);
99
+ // v2 requires the value's shape: `id` for select options, `boolean` otherwise.
100
+ const params = dialect === 2
101
+ ? { sessionId, configId, type: typeof value === 'boolean' ? 'boolean' : 'id', value }
102
+ : { sessionId, configId, value };
103
+ const result = await invoke<Record<string, unknown>>('session/set_config_option', params, timeoutMs);
104
+ if (dialect !== 2) return result;
105
+ return { ...result, ...(Array.isArray(result.configOptions) ? { configOptions: normalizeConfigOptions(result.configOptions) } : {}) };
72
106
  }
73
107
 
74
108
  /** omp's steering extension (advertised as `_meta.steering.supported`). */
@@ -85,3 +119,19 @@ export async function steer(
85
119
  export async function closeSession(invoke: AcpInvoke, sessionId: string, timeoutMs = 30_000): Promise<unknown> {
86
120
  return invoke('session/close', { sessionId }, timeoutMs);
87
121
  }
122
+
123
+ /** The older Session Modes API; `configOptions` supersedes it but agents still
124
+ * publish one or the other. */
125
+ export async function setMode(
126
+ invoke: AcpInvoke,
127
+ sessionId: string,
128
+ modeId: string,
129
+ timeoutMs = 30_000,
130
+ ): Promise<unknown> {
131
+ return invoke('session/set_mode', { sessionId, modeId }, timeoutMs);
132
+ }
133
+
134
+ /** Remove one session from the agent's own history (`sessionCapabilities.delete`). */
135
+ export async function deleteSession(invoke: AcpInvoke, sessionId: string, timeoutMs = 30_000): Promise<unknown> {
136
+ return invoke('session/delete', { sessionId }, timeoutMs);
137
+ }
@@ -83,6 +83,31 @@ describe('capabilitiesFrom', () => {
83
83
  });
84
84
  });
85
85
 
86
+ test('a null session capability is ABSENT, not support', () => {
87
+ const caps = capabilitiesFrom(
88
+ { protocolVersion: 1, agentCapabilities: { sessionCapabilities: { resume: null, list: {}, close: null } } },
89
+ undefined,
90
+ { usage: false, plan: false },
91
+ );
92
+ expect(caps.resumeSession).toBe(false);
93
+ expect(caps.closeSession).toBe(false);
94
+ expect(caps.listSessions).toBe(true);
95
+ });
96
+
97
+ test('an agent with only the legacy modes API still publishes a mode ladder', () => {
98
+ const caps = capabilitiesFrom(
99
+ { protocolVersion: 1 },
100
+ undefined,
101
+ { usage: false, plan: false },
102
+ {
103
+ currentModeId: 'code',
104
+ availableModes: [{ id: 'code', name: 'Code' }, { id: 'plan', name: 'Plan' }],
105
+ },
106
+ );
107
+ expect(caps.configMode).toBe(2);
108
+ expect(caps.modeValues).toEqual(['code', 'plan']);
109
+ });
110
+
86
111
  test('usage and plan are OBSERVED, never assumed', () => {
87
112
  const caps = capabilitiesFrom(ompInit, ompConfig, { usage: false, plan: true });
88
113
  expect(caps.hasUsage).toBe(false);
@@ -22,8 +22,9 @@
22
22
  */
23
23
 
24
24
  import { AcpProcess } from '@/server/lib/harness/acp/client';
25
+ import { withGatewayCompatibility } from '@/server/lib/harness/acp/gateway-env';
25
26
  import { newSessionAfterActivation } from '@/server/lib/harness/acp/activation';
26
- import type { AcpConfigOption, AcpInitializeResult } from '@/server/lib/harness/acp/protocol';
27
+ import type { AcpConfigOption, AcpInitializeResult, AcpSessionModeState } from '@/server/lib/harness/acp/protocol';
27
28
  import { acpOptionValues } from '@/server/lib/harness/acp/protocol';
28
29
  import { UNPROBED_CAPABILITIES, type HarnessCapabilities, type HarnessProbe } from '@/server/lib/harness/capabilities';
29
30
  import { HARNESS_LAUNCH, type HarnessLaunch } from '@/server/lib/harness/launch-table';
@@ -58,12 +59,19 @@ export function capabilitiesFrom(
58
59
  init: AcpInitializeResult,
59
60
  configOptions: AcpConfigOption[] | undefined,
60
61
  observed: { usage: boolean; plan: boolean },
62
+ modes?: AcpSessionModeState | null,
61
63
  ): HarnessCapabilities {
62
64
  const caps = init.agentCapabilities ?? {};
63
65
  const sessionCaps = caps.sessionCapabilities ?? {};
64
66
  const model = configByCategory(configOptions ?? [], 'model');
65
67
  const thinking = configByCategory(configOptions ?? [], 'thought_level');
66
68
  const mode = configByCategory(configOptions ?? [], 'mode');
69
+ // Config options supersede the older Session Modes API, but an agent may
70
+ // publish only `modes`. A client that understands `configOptions` should
71
+ // ignore `modes` when a selector exists; otherwise the ladder is this list.
72
+ const modeValues = mode
73
+ ? acpOptionValues(mode)
74
+ : (modes?.availableModes ?? []).map((entry) => entry.id);
67
75
  // omp advertises steering at the RESULT root (`_meta.steering`), a sibling of
68
76
  // `agentCapabilities` — measured on 18.16.0. Reading it from inside
69
77
  // `agentCapabilities` found nothing, so the capability record said "no
@@ -72,18 +80,23 @@ export function capabilitiesFrom(
72
80
  const steeringSupported = Boolean(
73
81
  steering && typeof steering === 'object' && 'supported' in steering && steering.supported === true,
74
82
  );
83
+ // A session capability is advertised by PRESENCE, and `null` is the spec's
84
+ // other way of saying absent — `'key' in caps` would read `{resume: null}` as
85
+ // support and then call a method the agent does not implement.
86
+ const advertises = (key: string): boolean =>
87
+ sessionCaps[key] !== undefined && sessionCaps[key] !== null;
75
88
  return {
76
89
  ...UNPROBED_CAPABILITIES,
77
90
  configModel: model ? acpOptionValues(model).length : 0,
78
91
  configThinking: thinking ? acpOptionValues(thinking).length : 0,
79
- configMode: mode ? acpOptionValues(mode).length : 0,
80
- modeValues: mode ? acpOptionValues(mode) : [],
92
+ configMode: modeValues.length,
93
+ modeValues,
81
94
  loadSession: caps.loadSession === true,
82
- listSessions: 'list' in sessionCaps,
83
- forkSession: 'fork' in sessionCaps,
84
- deleteSession: 'delete' in sessionCaps,
85
- resumeSession: 'resume' in sessionCaps,
86
- closeSession: 'close' in sessionCaps,
95
+ listSessions: advertises('list'),
96
+ forkSession: advertises('fork'),
97
+ deleteSession: advertises('delete'),
98
+ resumeSession: advertises('resume'),
99
+ closeSession: advertises('close'),
87
100
  hasUsage: observed.usage,
88
101
  hasPlan: observed.plan,
89
102
  steerMode: steeringSupported ? 'steer' : null,
@@ -141,12 +154,13 @@ export async function probeHarness(
141
154
  let proc: AcpProcess | null = null;
142
155
  /** `agentInfo` from a handshake that succeeded before a later step failed. */
143
156
  let agent: AcpInitializeResult['agentInfo'] | undefined;
157
+ const env = withGatewayCompatibility(id, launch.env);
144
158
 
145
159
  try {
146
160
  const agentProcess = new AcpProcess({
147
161
  command: launch.command,
148
162
  cwd,
149
- ...(launch.env ? { env: launch.env } : {}),
163
+ ...(env ? { env } : {}),
150
164
  onStderr: (line) => {
151
165
  stderrTail = `${stderrTail}${line}\n`.slice(-2_000);
152
166
  },
@@ -158,7 +172,7 @@ export async function probeHarness(
158
172
  if (!update || typeof update !== 'object') return;
159
173
  if (!('sessionUpdate' in update)) return;
160
174
  if (update.sessionUpdate === 'usage_update') observed.usage = true;
161
- if (update.sessionUpdate === 'plan') observed.plan = true;
175
+ if (update.sessionUpdate === 'plan' || update.sessionUpdate === 'plan_update') observed.plan = true;
162
176
  },
163
177
  });
164
178
  proc = agentProcess;
@@ -52,6 +52,9 @@ export interface HarnessSession extends HarnessEventSource, HarnessSessionState
52
52
  resolvePendingUiDialog(id: string): void;
53
53
  destroy(): void;
54
54
  destroyAndWait(): Promise<void>;
55
+ /** Delete the harness's own session record, when the agent advertises one
56
+ * (`sessionCapabilities.delete`). Best-effort: the local rows are the caller's. */
57
+ deleteAgentSession?(): Promise<void>;
55
58
  onDestroy(callback: () => void): void;
56
59
  }
57
60
 
@@ -37,6 +37,9 @@ export interface AcpSpawnRequest {
37
37
  /** On-disk transcript to resume; '' for a new chat. For ACP that is the
38
38
  * agent's own session id, not a path — Kabmo stores it in `harness_sessions`. */
39
39
  resumeAgentSessionId?: string;
40
+ /** Fork this agent session instead of starting a new one. Mutually exclusive
41
+ * with `resumeAgentSessionId`; the returned id is the child's own. */
42
+ forkAgentSessionId?: string;
40
43
  cwd: string;
41
44
  model?: SelectedModel | null;
42
45
  thinkingLevel?: string | null;
@@ -44,6 +47,8 @@ export interface AcpSpawnRequest {
44
47
  /** The composer's access pick, applied to the ACP session's own approval
45
48
  * gate (this transport has no `--approval-mode` spawn flag). */
46
49
  accessMode?: ApprovalMode | null;
50
+ /** Extra environment for this child only (the agent token and server URL). */
51
+ env?: Record<string, string>;
47
52
  }
48
53
 
49
54
  export class HarnessNotLaunchableError extends Error {
@@ -82,11 +87,12 @@ export async function startAcpSession(
82
87
  // Built here rather than behind a factory in the session module: this is the
83
88
  // only place a session is constructed, and the handshake it runs (load vs
84
89
  // new) is decided by the request this function already holds.
90
+ const childEnv = { ...(launch.env ?? {}), ...(request.env ?? {}) };
85
91
  const session = new AcpSession(
86
92
  {
87
93
  harness: request.harness,
88
94
  command: sessionLaunchCommand(request.harness, launch.command),
89
- ...(launch.env ? { env: launch.env } : {}),
95
+ ...(Object.keys(childEnv).length > 0 ? { env: childEnv } : {}),
90
96
  model: startingModel,
91
97
  thinkingLevel: request.thinkingLevel ?? configured.thinking ?? null,
92
98
  mode: request.mode ?? configured.mode ?? null,
@@ -96,7 +102,10 @@ export async function startAcpSession(
96
102
  );
97
103
  let realSessionId: string;
98
104
  try {
99
- ({ sessionId: realSessionId } = await session.start(request.resumeAgentSessionId || undefined));
105
+ ({ sessionId: realSessionId } = await session.start(
106
+ request.resumeAgentSessionId || undefined,
107
+ request.forkAgentSessionId,
108
+ ));
100
109
  } catch (error) {
101
110
  // A refused handshake (an agent that will not `session/load`, a launch line
102
111
  // that cannot run) must not leave the child it just spawned running with
@@ -105,6 +114,10 @@ export async function startAcpSession(
105
114
  await session.destroyAndWait().catch(() => {});
106
115
  throw error;
107
116
  }
117
+ if (!realSessionId) {
118
+ await session.destroyAndWait().catch(() => {});
119
+ throw new Error(`${request.harness} started a session without an id`);
120
+ }
108
121
 
109
122
  claimHarness(session, request.harness);
110
123
  registerAcpSession(session, request.sessionId);
@@ -0,0 +1,151 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The record a background harness update leaves on disk.
8
+ *
9
+ * The two questions these cases pin are the ones the dialog cannot answer for
10
+ * itself: is this run still going, and is a record that stopped moving a failure
11
+ * rather than something to poll forever.
12
+ */
13
+
14
+ import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
15
+ import { mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
16
+ import { tmpdir } from 'node:os';
17
+ import { join } from 'node:path';
18
+ import {
19
+ jobInFlight,
20
+ pidAlive,
21
+ readUpdateJob,
22
+ settleDeadJob,
23
+ writeUpdateJob,
24
+ UPDATE_START_GRACE_MS,
25
+ } from '@/server/lib/harness/update-job-store';
26
+ import { jobQueued, jobRunning } from '@/shared/lib/harness/update-job';
27
+ import { getHarnessUpdateStatusPath } from '@/server/lib/lifecycle/paths';
28
+ import type { HarnessUpdateJob } from '@/shared/types/harness-update';
29
+
30
+ let dir = '';
31
+ let originalDataDir: string | undefined;
32
+
33
+ beforeEach(() => {
34
+ originalDataDir = Bun.env.KABMO_DATA_DIR;
35
+ dir = mkdtempSync(join(tmpdir(), 'kabmo-update-job-'));
36
+ Bun.env.KABMO_DATA_DIR = dir;
37
+ });
38
+
39
+ afterEach(() => {
40
+ if (originalDataDir === undefined) delete Bun.env.KABMO_DATA_DIR;
41
+ else Bun.env.KABMO_DATA_DIR = originalDataDir;
42
+ rmSync(dir, { recursive: true, force: true });
43
+ });
44
+
45
+ /** The one record this suite owns: claude's own file. */
46
+ const statusPath = (): string => getHarnessUpdateStatusPath('claude');
47
+
48
+ function job(overrides: Partial<HarnessUpdateJob> = {}): HarnessUpdateJob {
49
+ return {
50
+ harness: 'claude',
51
+ label: 'claude',
52
+ startedAt: Date.now(),
53
+ finishedAt: null,
54
+ queuedAt: null,
55
+ pid: process.pid,
56
+ steps: [{ part: 'adapter', command: 'npm install -g x', argv: ['npm', 'install', '-g', 'x'], state: 'running', code: null }],
57
+ output: '',
58
+ ok: null,
59
+ message: null,
60
+ applied: false,
61
+ repointed: null,
62
+ ...overrides,
63
+ };
64
+ }
65
+
66
+ describe('the status record', () => {
67
+ test('round-trips, and leaves no temp file behind', async () => {
68
+ await writeUpdateJob(job({ output: 'installing' }), statusPath());
69
+ const read = await readUpdateJob(statusPath());
70
+ expect(read?.harness).toBe('claude');
71
+ expect(read?.output).toBe('installing');
72
+ // `tmp` + `rename`, so a reader never sees half a record and the temp name
73
+ // does not pile up.
74
+ const leftovers = readdirSync(join(dir, 'run')).filter((name) => name.endsWith('.tmp'));
75
+ expect(leftovers).toEqual([]);
76
+ });
77
+
78
+ test('an absent record is null, not a throw', async () => {
79
+ expect(await readUpdateJob(statusPath())).toBeNull();
80
+ });
81
+
82
+ test('a foreign JSON file is not a job', async () => {
83
+ // A hand-written or half-written file must read as "nothing ran", not as a
84
+ // record whose missing fields crash the dialog.
85
+ writeFileSync(statusPath(), JSON.stringify({ hello: 'world' }));
86
+ expect(await readUpdateJob(statusPath())).toBeNull();
87
+ });
88
+ });
89
+
90
+ describe('telling a live run from a dead one', () => {
91
+ test('a run with a live pid is in flight', () => {
92
+ expect(jobInFlight(job())).toBe(true);
93
+ });
94
+
95
+ test('a finished run is not in flight', () => {
96
+ expect(jobInFlight(job({ finishedAt: Date.now(), ok: true }))).toBe(false);
97
+ });
98
+
99
+ test('a run whose worker is gone is settled by the next read', async () => {
100
+ const dead = job({ pid: 99_999_999, startedAt: Date.now() - UPDATE_START_GRACE_MS * 2 });
101
+ await writeUpdateJob(dead, statusPath());
102
+ const settled = await settleDeadJob(await readUpdateJob(statusPath()), statusPath());
103
+ expect(settled?.finishedAt).not.toBeNull();
104
+ expect(settled?.ok).toBe(false);
105
+ expect(settled?.message).toContain('without reporting a result');
106
+ expect(settled?.steps[0].state).toBe('failed');
107
+ // Settled once, and the settle itself is on disk for the next reader.
108
+ expect((await readUpdateJob(statusPath()))?.finishedAt).not.toBeNull();
109
+ });
110
+
111
+ test('the start grace covers the pid handshake', async () => {
112
+ // The record is written before the worker has a pid; reading that as dead
113
+ // would report failure over a run that is about to start.
114
+ const starting = job({ pid: null, startedAt: Date.now() });
115
+ await writeUpdateJob(starting, statusPath());
116
+ expect(await settleDeadJob(await readUpdateJob(statusPath()), statusPath())).toEqual(starting);
117
+ expect(jobInFlight(starting)).toBe(true);
118
+ });
119
+
120
+ test('a finished run is left exactly as the worker wrote it', async () => {
121
+ const done = job({ pid: null, finishedAt: Date.now(), ok: true, message: 'claude updated (adapter)' });
122
+ await writeUpdateJob(done, statusPath());
123
+ expect(await settleDeadJob(await readUpdateJob(statusPath()), statusPath())).toEqual(done);
124
+ });
125
+
126
+ test('a QUEUED record is neither in flight nor dead', async () => {
127
+ // Two presses are a queue: the second record exists with no pid on purpose,
128
+ // and reading that as a worker that died would fail a run that never started.
129
+ const waiting = job({ pid: null, queuedAt: Date.now() - UPDATE_START_GRACE_MS * 2 });
130
+ await writeUpdateJob(waiting, statusPath());
131
+ expect(jobQueued(waiting)).toBe(true);
132
+ expect(jobRunning(waiting)).toBe(false);
133
+ expect(jobInFlight(waiting)).toBe(false);
134
+ expect(await settleDeadJob(await readUpdateJob(statusPath()), statusPath())).toEqual(waiting);
135
+ });
136
+
137
+ test('a queued record starts reading as a RUN when its worker takes over', async () => {
138
+ const started = job({ pid: process.pid, queuedAt: Date.now() });
139
+ await writeUpdateJob(started, statusPath());
140
+ // The pid, not the stamp, is what makes it a run: the worker writes both.
141
+ expect(jobQueued(started)).toBe(false);
142
+ expect(jobRunning(started)).toBe(true);
143
+ expect(jobInFlight(started)).toBe(true);
144
+ });
145
+
146
+ test('an impossible pid is not alive', () => {
147
+ expect(pidAlive(null)).toBe(false);
148
+ expect(pidAlive(0)).toBe(false);
149
+ expect(pidAlive(process.pid)).toBe(true);
150
+ });
151
+ });