@kabmo-labs/kabmo 0.23.0 → 0.25.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 +805 -736
  3. package/dist/client/static/js/index-bc9aa4mx.css +1 -0
  4. package/dist/client/static/js/{index-ph0q59rs.js → index-ga8cbpt4.js} +1071 -1171
  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/Modal.tsx +2 -6
  13. package/src/client/components/common/code-surface/shiki.test.ts +82 -0
  14. package/src/client/components/common/code-surface/shiki.ts +28 -7
  15. package/src/client/components/common/session-actions-menu/NewChildSessionDialog.tsx +113 -0
  16. package/src/client/components/common/session-actions-menu/index.tsx +33 -2
  17. package/src/client/components/common/session-row/StatusSlot.tsx +4 -4
  18. package/src/client/components/common/session-row/index.tsx +27 -1
  19. package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +7 -1
  20. package/src/client/components/layout/desktop-layout/resizer/separator-drag.ts +3 -3
  21. package/src/client/components/layout/session-sidebar/CategoryItem.tsx +25 -12
  22. package/src/client/components/layout/session-sidebar/ChildSessionList.tsx +66 -0
  23. package/src/client/components/layout/session-sidebar/SessionItem.tsx +28 -3
  24. package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.test.tsx +20 -0
  25. package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.tsx +59 -12
  26. package/src/client/components/layout/session-sidebar/about-modal/HarnessTableLog.test.tsx +94 -0
  27. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.test.tsx +130 -0
  28. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.tsx +120 -0
  29. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.tsx +47 -0
  30. package/src/client/components/layout/session-sidebar/about-modal/index.test.tsx +122 -0
  31. package/src/client/components/layout/session-sidebar/about-modal/index.tsx +55 -46
  32. package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +18 -5
  33. package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +21 -4
  34. package/src/client/components/mobile/mobile-session-sidebar/SessionRow.tsx +27 -5
  35. package/src/client/components/settings/categories/harness-settings/EnvProviderForm.tsx +31 -0
  36. package/src/client/components/settings/categories/harness-settings/provider-panel.test.tsx +49 -0
  37. package/src/client/components/workspace/chat-timeline/ChatOverlays.tsx +5 -2
  38. package/src/client/components/workspace/chat-timeline/ComposerDock.tsx +64 -70
  39. package/src/client/components/workspace/chat-timeline/ComposerDockFrame.tsx +35 -0
  40. package/src/client/components/workspace/chat-timeline/EmptyWorkspacePrompt.tsx +135 -118
  41. package/src/client/components/workspace/chat-timeline/MessageItem.tsx +22 -1
  42. package/src/client/components/workspace/chat-timeline/MessageList.test.ts +14 -0
  43. package/src/client/components/workspace/chat-timeline/MessageList.tsx +4 -0
  44. package/src/client/components/workspace/chat-timeline/TimelineBody.tsx +3 -0
  45. package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +4 -3
  46. package/src/client/components/workspace/chat-timeline/composer-dock-frame.test.tsx +155 -0
  47. package/src/client/components/workspace/chat-timeline/index.tsx +5 -2
  48. package/src/client/components/workspace/chat-timeline/pending-harness.test.tsx +1 -1
  49. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/FrameControl.tsx +11 -20
  50. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/GroupedAsk.tsx +1 -1
  51. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/QuestionBlock.tsx +1 -1
  52. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/drafts.ts +1 -1
  53. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/index.tsx +2 -1
  54. package/src/client/components/workspace/file-explorer/index.test.ts +25 -0
  55. package/src/client/components/workspace/file-explorer/index.tsx +9 -3
  56. package/src/client/components/workspace/model-dropdown/harness-models.test.ts +17 -0
  57. package/src/client/components/workspace/model-dropdown/harness-models.ts +33 -18
  58. package/src/client/components/workspace/model-dropdown/index.tsx +3 -3
  59. package/src/client/components/workspace/model-dropdown/popover-metrics.ts +4 -1
  60. package/src/client/components/workspace/terminal-panel/KeyBar.tsx +10 -8
  61. package/src/client/components/workspace/terminal-panel/{RealtimeXtermView.tsx → RealtimeTerminalView.tsx} +51 -58
  62. package/src/client/components/workspace/terminal-panel/fit.test.ts +37 -0
  63. package/src/client/components/workspace/terminal-panel/fit.ts +64 -0
  64. package/src/client/components/workspace/terminal-panel/fonts.test.ts +161 -0
  65. package/src/client/components/workspace/terminal-panel/fonts.ts +164 -0
  66. package/src/client/components/workspace/terminal-panel/grid.test.ts +52 -0
  67. package/src/client/components/workspace/terminal-panel/grid.ts +118 -0
  68. package/src/client/components/workspace/terminal-panel/index.tsx +25 -21
  69. package/src/client/components/workspace/terminal-panel/mount.ts +247 -223
  70. package/src/client/components/workspace/terminal-panel/navigation.test.ts +54 -0
  71. package/src/client/components/workspace/terminal-panel/navigation.ts +47 -0
  72. package/src/client/data/settings/omp-schema.json +5 -5
  73. package/src/client/data/theme/terminal.test.ts +25 -0
  74. package/src/client/data/theme/terminal.ts +96 -79
  75. package/src/client/hooks/chat/composer/items.test.ts +12 -26
  76. package/src/client/hooks/chat/composer/pick-mapping.test.ts +42 -0
  77. package/src/client/hooks/chat/omp/prompt-send.test.ts +7 -7
  78. package/src/client/hooks/chat/omp/prompt-send.ts +26 -14
  79. package/src/client/hooks/chat/timeline/actions.ts +2 -2
  80. package/src/client/hooks/chat/timeline/ask-frames.ts +5 -4
  81. package/src/client/hooks/chat/timeline/new-chat-open.test.ts +86 -0
  82. package/src/client/hooks/chat/timeline/new-chat-open.ts +23 -5
  83. package/src/client/hooks/chat/timeline/new-chat.test.ts +32 -4
  84. package/src/client/hooks/chat/timeline/new-chat.ts +18 -6
  85. package/src/client/hooks/chat/timeline/prompt-text.test.ts +34 -1
  86. package/src/client/hooks/chat/timeline/prompt-text.ts +47 -20
  87. package/src/client/hooks/chat/timeline/rewind-actions.test.ts +24 -1
  88. package/src/client/hooks/chat/timeline/rewind-actions.ts +15 -11
  89. package/src/client/hooks/chat/timeline/run-identity.test.ts +52 -0
  90. package/src/client/hooks/chat/timeline/run-identity.ts +23 -11
  91. package/src/client/hooks/chat/timeline/send-execute.test.ts +56 -0
  92. package/src/client/hooks/chat/timeline/send-helpers.ts +30 -1
  93. package/src/client/hooks/chat/timeline/send-props.ts +5 -3
  94. package/src/client/hooks/chat/timeline/send-steer.test.ts +4 -4
  95. package/src/client/hooks/chat/timeline/send.test-util.ts +8 -2
  96. package/src/client/hooks/chat/timeline/send.test.ts +4 -4
  97. package/src/client/hooks/chat/timeline/send.ts +59 -53
  98. package/src/client/hooks/chat/timeline/session-load.test-util.ts +6 -2
  99. package/src/client/hooks/chat/timeline/session-load.test.ts +31 -0
  100. package/src/client/hooks/chat/timeline/session-load.ts +6 -14
  101. package/src/client/hooks/chat/timeline/session-shapes.ts +32 -0
  102. package/src/client/hooks/chat/timeline/use-fork-session.ts +49 -0
  103. package/src/client/hooks/models/harness-catalog.ts +114 -0
  104. package/src/client/hooks/models/provider-writes.test.ts +81 -0
  105. package/src/client/hooks/models/provider-writes.ts +10 -3
  106. package/src/client/hooks/models/use-harness-update.test.ts +193 -0
  107. package/src/client/hooks/models/use-harness-update.ts +202 -0
  108. package/src/client/hooks/models/use-harnesses-controller.ts +57 -0
  109. package/src/client/hooks/models/use-harnesses-signal.test.ts +128 -0
  110. package/src/client/hooks/models/use-harnesses.test.ts +2 -1
  111. package/src/client/hooks/models/use-harnesses.ts +48 -114
  112. package/src/client/hooks/workspace/child-expansion.ts +33 -0
  113. package/src/client/hooks/workspace/terminal/index.ts +17 -3
  114. package/src/client/hooks/workspace/terminal/socket.ts +9 -4
  115. package/src/client/lib/session-tree.test.ts +50 -0
  116. package/src/client/lib/session-tree.ts +59 -0
  117. package/src/client/tailwind.css +26 -12
  118. package/src/server/index.ts +2 -0
  119. package/src/server/lib/auth/guard.ts +10 -0
  120. package/src/server/lib/btw/session-copy.server.ts +6 -44
  121. package/src/server/lib/btw/session-copy.test.ts +2 -1
  122. package/src/server/lib/db/schema.ts +4 -0
  123. package/src/server/lib/db/seed-terminal.test.ts +1 -1
  124. package/src/server/lib/db/seed.test.ts +1 -1
  125. package/src/server/lib/harness/acp/activation.test.ts +81 -0
  126. package/src/server/lib/harness/acp/activation.ts +72 -0
  127. package/src/server/lib/harness/acp/background-work.test.ts +262 -0
  128. package/src/server/lib/harness/acp/background-work.ts +320 -0
  129. package/src/server/lib/harness/acp/client.ts +2 -2
  130. package/src/server/lib/harness/acp/frames.test.ts +50 -1
  131. package/src/server/lib/harness/acp/frames.ts +17 -5
  132. package/src/server/lib/harness/acp/gateway-env.test.ts +64 -0
  133. package/src/server/lib/harness/acp/gateway-env.ts +70 -0
  134. package/src/server/lib/harness/acp/mapper.ts +6 -4
  135. package/src/server/lib/harness/acp/prompt.test.ts +20 -0
  136. package/src/server/lib/harness/acp/prompt.ts +19 -3
  137. package/src/server/lib/harness/acp/session-background.test.ts +129 -0
  138. package/src/server/lib/harness/acp/session-config.test.ts +73 -0
  139. package/src/server/lib/harness/acp/session-config.ts +87 -6
  140. package/src/server/lib/harness/acp/session-exit.ts +31 -0
  141. package/src/server/lib/harness/acp/session-handshake.test.ts +108 -0
  142. package/src/server/lib/harness/acp/session-handshake.ts +36 -3
  143. package/src/server/lib/harness/acp/session-idle.ts +51 -0
  144. package/src/server/lib/harness/acp/session-listeners.ts +31 -0
  145. package/src/server/lib/harness/acp/session-notifications.ts +19 -3
  146. package/src/server/lib/harness/acp/session-observers.test.ts +9 -0
  147. package/src/server/lib/harness/acp/session-options.ts +33 -0
  148. package/src/server/lib/harness/acp/session-reaper.test.ts +96 -0
  149. package/src/server/lib/harness/acp/session-requests.ts +47 -0
  150. package/src/server/lib/harness/acp/session-turns.ts +20 -5
  151. package/src/server/lib/harness/acp/session.test-util.ts +54 -3
  152. package/src/server/lib/harness/acp/session.test.ts +65 -3
  153. package/src/server/lib/harness/acp/session.ts +79 -79
  154. package/src/server/lib/harness/acp/turn.test.ts +81 -0
  155. package/src/server/lib/harness/acp/turn.ts +54 -13
  156. package/src/server/lib/harness/acp/verbs.ts +12 -2
  157. package/src/server/lib/harness/auto-probe.server.test.ts +121 -0
  158. package/src/server/lib/harness/auto-probe.server.ts +178 -0
  159. package/src/server/lib/harness/config.server.test.ts +1 -1
  160. package/src/server/lib/harness/config.server.ts +1 -1
  161. package/src/server/lib/harness/launch-table.ts +195 -0
  162. package/src/server/lib/harness/launch.test.ts +37 -0
  163. package/src/server/lib/harness/launch.ts +16 -116
  164. package/src/server/lib/harness/provider-entry.ts +32 -0
  165. package/src/server/lib/harness/provider-file.server.ts +3 -0
  166. package/src/server/lib/harness/provider-sources.server.test.ts +45 -0
  167. package/src/server/lib/harness/provider-sources.server.ts +26 -2
  168. package/src/server/lib/harness/provider-write.server.ts +21 -32
  169. package/src/server/lib/harness/provider-zcode.server.test.ts +270 -0
  170. package/src/server/lib/harness/provider-zcode.server.ts +324 -0
  171. package/src/server/lib/harness/spawn.server.ts +15 -2
  172. package/src/server/lib/harness/update-job-store.test.ts +126 -0
  173. package/src/server/lib/harness/update-job-store.ts +109 -0
  174. package/src/server/lib/harness/update-job.server.ts +203 -0
  175. package/src/server/lib/harness/update-state.server.ts +15 -20
  176. package/src/server/lib/harness/update-worker.test.ts +153 -0
  177. package/src/server/lib/harness/update-worker.ts +197 -0
  178. package/src/server/lib/lifecycle/after-listen.ts +7 -0
  179. package/src/server/lib/lifecycle/paths.ts +23 -0
  180. package/src/server/lib/models/registry-refresh.server.test.ts +87 -0
  181. package/src/server/lib/models/registry-refresh.server.ts +98 -0
  182. package/src/server/lib/omp/rpc/child-config.ts +5 -4
  183. package/src/server/lib/omp/rpc/constants.test.ts +2 -1
  184. package/src/server/lib/omp/rpc/forked-resume.test.ts +14 -3
  185. package/src/server/lib/omp/rpc/manager.ts +5 -5
  186. package/src/server/lib/omp/rpc/prewarm-pool.ts +12 -3
  187. package/src/server/lib/omp/rpc/session-registry.ts +14 -2
  188. package/src/server/lib/omp/rpc/terminal-settle.ts +53 -5
  189. package/src/server/lib/omp/session/delete-flow.server.ts +62 -8
  190. package/src/server/lib/omp/session/delete.server.ts +17 -0
  191. package/src/server/lib/omp/session/rewind-file.ts +39 -21
  192. package/src/server/lib/omp/session/sidebar-data.server.ts +6 -6
  193. package/src/server/lib/omp/session/transcript-copy.ts +123 -0
  194. package/src/server/lib/sessions/agent-token.server.test.ts +64 -0
  195. package/src/server/lib/sessions/agent-token.server.ts +134 -0
  196. package/src/server/lib/sessions/answers.server.test.ts +33 -0
  197. package/src/server/lib/sessions/answers.server.ts +37 -0
  198. package/src/server/lib/sessions/dispatch.server.test.ts +143 -0
  199. package/src/server/lib/sessions/dispatch.server.ts +128 -0
  200. package/src/server/lib/sessions/fork.server.test.ts +209 -0
  201. package/src/server/lib/sessions/fork.server.ts +304 -0
  202. package/src/server/lib/sessions/lifecycle.server.test.ts +75 -0
  203. package/src/server/lib/sessions/lifecycle.server.ts +71 -0
  204. package/src/server/lib/sessions/notify.server.test.ts +112 -0
  205. package/src/server/lib/sessions/notify.server.ts +212 -0
  206. package/src/server/lib/sessions/relation-types.ts +76 -0
  207. package/src/server/lib/sessions/relations.server.test.ts +207 -0
  208. package/src/server/lib/sessions/relations.server.ts +337 -0
  209. package/src/server/lib/sessions/resolve.server.ts +106 -0
  210. package/src/server/lib/sessions/schema.server.ts +82 -0
  211. package/src/server/lib/sessions/sidebar-relations.server.test.ts +61 -0
  212. package/src/server/lib/sessions/sidebar-relations.server.ts +47 -0
  213. package/src/server/lib/sessions/spawn.server.test.ts +196 -0
  214. package/src/server/lib/sessions/spawn.server.ts +310 -0
  215. package/src/server/lib/terminal/attach-replay.test.ts +2 -2
  216. package/src/server/lib/terminal/output.test.ts +2 -2
  217. package/src/server/lib/terminal/output.ts +1 -1
  218. package/src/server/lib/terminal/resize.test.ts +99 -0
  219. package/src/server/lib/terminal/runtime.server.ts +26 -6
  220. package/src/server/lib/terminal/send-result.test.ts +18 -1
  221. package/src/server/lib/terminal/send-result.ts +26 -2
  222. package/src/server/lib/terminal/session.server.ts +6 -6
  223. package/src/server/lib/usage/new-api.test.ts +92 -0
  224. package/src/server/lib/usage/new-api.ts +131 -0
  225. package/src/server/lib/usage/provider-endpoint.test.ts +106 -0
  226. package/src/server/lib/usage/provider-key.ts +33 -0
  227. package/src/server/lib/usage/providers.server.ts +10 -3
  228. package/src/server/lib/usage/vendor-balance.server.ts +4 -53
  229. package/src/server/lib/usage/vendor-http.ts +41 -0
  230. package/src/server/lib/usage/window.ts +55 -0
  231. package/src/server/routes/agent/command.ts +14 -1
  232. package/src/server/routes/agent/harness-command.ts +6 -0
  233. package/src/server/routes/agent/new.ts +15 -0
  234. package/src/server/routes/chat/rewind.test.ts +147 -0
  235. package/src/server/routes/chat/rewind.ts +29 -7
  236. package/src/server/routes/chat/session.test.ts +165 -10
  237. package/src/server/routes/chat/session.ts +60 -7
  238. package/src/server/routes/harness/add-provider.test.ts +9 -1
  239. package/src/server/routes/harness/import-action.ts +22 -4
  240. package/src/server/routes/harness/import.test.ts +1 -1
  241. package/src/server/routes/harness/index.ts +5 -1
  242. package/src/server/routes/harness/root.ts +8 -49
  243. package/src/server/routes/harness/toggle.test.ts +20 -0
  244. package/src/server/routes/harness/update-status.ts +26 -0
  245. package/src/server/routes/harness/update.test.ts +119 -0
  246. package/src/server/routes/harness/update.ts +67 -113
  247. package/src/server/routes/harness/versions.ts +5 -1
  248. package/src/server/routes/sessions/answer.ts +24 -0
  249. package/src/server/routes/sessions/batch-delete.ts +11 -5
  250. package/src/server/routes/sessions/delete-acp.test.ts +227 -0
  251. package/src/server/routes/sessions/delete.ts +4 -2
  252. package/src/server/routes/sessions/fork.ts +74 -0
  253. package/src/server/routes/sessions/index.ts +14 -0
  254. package/src/server/routes/sessions/relations.ts +85 -0
  255. package/src/server/routes/sessions/session.ts +4 -0
  256. package/src/server/routes/sessions/spawn.ts +134 -0
  257. package/src/server/routes/settings/model-override.ts +5 -2
  258. package/src/server/routes/settings/provider-entry.ts +2 -2
  259. package/src/server/routes/settings/provider-model.ts +2 -2
  260. package/src/server/routes/settings/provider-models.ts +2 -2
  261. package/src/server/routes/settings/provider-refresh.ts +6 -4
  262. package/src/server/routes/settings/providers.ts +3 -1
  263. package/src/server/routes/settings/settings-config-routes.test.ts +10 -2
  264. package/src/server/routes/terminal/ws.ts +3 -3
  265. package/src/shared/lib/chat/ask-form/fields.ts +76 -0
  266. package/src/shared/lib/chat/ask-form/index.ts +323 -0
  267. package/src/shared/lib/chat/ask-form/tokens.ts +199 -0
  268. package/src/shared/lib/chat/ask-form/types.ts +43 -0
  269. package/src/shared/lib/chat/ask-form.test.ts +137 -0
  270. package/src/shared/lib/chat/ask-questions.test.ts +65 -1
  271. package/src/shared/lib/chat/ask-questions.ts +51 -145
  272. package/src/shared/lib/chat/ask-results.ts +198 -0
  273. package/src/shared/lib/chat/composer/client.ts +31 -2
  274. package/src/shared/lib/chat/composer/delegation.test.ts +93 -0
  275. package/src/shared/lib/chat/composer/delegation.ts +206 -0
  276. package/src/shared/lib/chat/notice-text.ts +1 -1
  277. package/src/shared/lib/chat/timeline/omp-callbacks.ts +13 -1
  278. package/src/shared/lib/chat/timeline/prompt-error.test.ts +146 -0
  279. package/src/shared/lib/chat/timeline/prompt-error.ts +88 -0
  280. package/src/shared/lib/chat/tool/summary-wait.test.ts +11 -0
  281. package/src/shared/lib/code/ansi.ts +1 -1
  282. package/src/shared/lib/fonts/nerd-symbols.css +4 -3
  283. package/src/shared/lib/fonts/nerd-symbols.test.ts +9 -9
  284. package/src/shared/lib/harness/catalog.test.ts +1 -1
  285. package/src/shared/lib/harness/config-fields.test.ts +4 -0
  286. package/src/shared/lib/harness/config-fields.ts +19 -4
  287. package/src/shared/lib/harness/dialects.test.ts +8 -0
  288. package/src/shared/lib/harness/dialects.ts +12 -0
  289. package/src/shared/lib/harness/provider-facts.test.ts +59 -8
  290. package/src/shared/lib/harness/provider-facts.ts +46 -11
  291. package/src/shared/lib/harness/providers.ts +44 -1
  292. package/src/shared/lib/harness/rewind.ts +24 -0
  293. package/src/shared/lib/harness/update-job.ts +33 -0
  294. package/src/shared/lib/harness/update-plan.test.ts +69 -4
  295. package/src/shared/lib/harness/update-plan.ts +57 -9
  296. package/src/shared/lib/harness/updates.test.ts +22 -0
  297. package/src/shared/lib/harness/updates.ts +24 -1
  298. package/src/shared/lib/models/client.ts +10 -1
  299. package/src/shared/lib/omp/dialog-methods.ts +4 -3
  300. package/src/shared/lib/omp/session/wait-idle.test.ts +11 -0
  301. package/src/shared/lib/omp/session/wait-idle.ts +12 -2
  302. package/src/shared/lib/theme/catalog.test.ts +13 -9
  303. package/src/shared/lib/theme/color.test.ts +2 -2
  304. package/src/shared/lib/theme/color.ts +4 -4
  305. package/src/shared/lib/theme/css.ts +2 -0
  306. package/src/shared/lib/theme/ink-scale.ts +21 -1
  307. package/src/shared/lib/theme/theme.test.ts +26 -10
  308. package/src/shared/lib/ui/safe-area.test.ts +28 -0
  309. package/src/shared/lib/ui/safe-area.ts +42 -0
  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 +65 -0
  316. package/src/shared/types/harness.ts +30 -4
  317. package/src/shared/types/omp/agent.ts +19 -2
  318. package/src/shared/types/workspace.ts +4 -0
  319. package/src/test-support/fake-omp-fork.ts +16 -2
  320. package/src/test-support/file-explorer-render.test-util.ts +37 -0
  321. package/src/test-support/file-explorer.test-util.ts +20 -18
  322. package/src/test-support/harness-table.test-util.ts +21 -4
  323. package/dist/client/static/js/index-jhmg4w91.css +0 -1
  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,203 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * Starting a harness update in the background, and applying what a finished one
8
+ * left behind.
9
+ *
10
+ * The POST that asks for an update answers as soon as the worker is spawned; the
11
+ * worker (`update-worker.ts`) owns the install and writes its progress to disk.
12
+ * This module is the server half: it refuses a second run, starts the worker,
13
+ * and — on the next status read — does the two things a detached process cannot
14
+ * do for itself: drop the cached probe/CLI-version reads, and point an adapter's
15
+ * launch line at the binary the install wrote.
16
+ *
17
+ * **The refusal is a fact on disk, not a flag in memory.** The old route had no
18
+ * single-flight at all: a page reload mid-install offered the button again, and a
19
+ * second `npm install -g` over the same prefix is how an npm tree gets corrupted.
20
+ * The check reads the same record the worker writes, so a second tab and a
21
+ * second server instance see the same answer.
22
+ */
23
+
24
+ import { existsSync } from 'node:fs';
25
+ import { join } from 'node:path';
26
+ import { resolveBunBin } from '@/server/lib/lifecycle/bun';
27
+ import { packageDir } from '@/server/lib/fs/package-root';
28
+ import {
29
+ getHarnessUpdateLockPath,
30
+ getHarnessUpdateSpecPath,
31
+ getHarnessUpdateStatusPath,
32
+ } from '@/server/lib/lifecycle/paths';
33
+ import { forgetProbe } from '@/server/lib/harness/probe-cache.server';
34
+ import { forgetCliVersion } from '@/server/lib/harness/update-state.server';
35
+ import { jobInFlight, readUpdateJob, settleDeadJob, writeUpdateJob } from '@/server/lib/harness/update-job-store';
36
+ import { loadHarnessConfig, saveHarnessConfig } from '@/server/lib/harness/config.server';
37
+ import { invalidateOmpCliCache } from '@/server/lib/omp/core/cli';
38
+ import { harnessUpdateFacts, type HarnessUpdateFacts } from '@/shared/lib/harness/updates';
39
+ import { repointAdapterLaunch, type HarnessUpdateStep } from '@/shared/lib/harness/update-plan';
40
+ import { HARNESS_IDS, type HarnessId } from '@/shared/lib/harness/catalog';
41
+ import type { HarnessUpdateJob, HarnessUpdateJobSpec } from '@/shared/types/harness-update';
42
+
43
+ /**
44
+ * Where the worker's source lives inside this package.
45
+ *
46
+ * Derived from the package root rather than `import.meta.dir`: in production the
47
+ * server runs the AOT bundle in `dist/client`, so this module's own directory is
48
+ * not the source tree. `src/` ships in the tarball (`package.json#files`), which
49
+ * is what makes the path valid in an installed copy too.
50
+ */
51
+ function workerPath(): string {
52
+ return join(packageDir(), 'src', 'server', 'lib', 'harness', 'update-worker.ts');
53
+ }
54
+
55
+ /** The current record, with a run that died without reporting turned into a failure. */
56
+ export async function currentUpdateJob(): Promise<HarnessUpdateJob | null> {
57
+ return settleDeadJob(await readUpdateJob());
58
+ }
59
+
60
+ /** The run in flight, or null. Used to refuse a second press with a reason. */
61
+ export async function runningUpdateJob(): Promise<HarnessUpdateJob | null> {
62
+ const job = await currentUpdateJob();
63
+ return jobInFlight(job) ? job : null;
64
+ }
65
+
66
+ /**
67
+ * Spawn the worker for `steps` and return the record it will fill.
68
+ *
69
+ * The record is written BEFORE the spawn so a poll that arrives in between sees
70
+ * "running" rather than "no update"; the pid is written immediately after, and
71
+ * the start grace window covers the gap.
72
+ */
73
+ export async function startHarnessUpdateJob(
74
+ harness: HarnessId,
75
+ label: string,
76
+ steps: HarnessUpdateStep[],
77
+ ): Promise<HarnessUpdateJob> {
78
+ const worker = workerPath();
79
+ if (!existsSync(worker)) {
80
+ throw new Error(`The update worker is missing from ${worker} — reinstall Kabmo to restore it`);
81
+ }
82
+
83
+ const spec: HarnessUpdateJobSpec = {
84
+ harness,
85
+ label,
86
+ steps,
87
+ statusPath: getHarnessUpdateStatusPath(),
88
+ lockPath: getHarnessUpdateLockPath(),
89
+ };
90
+ await Bun.write(getHarnessUpdateSpecPath(), JSON.stringify(spec, null, 2));
91
+
92
+ const startedAt = Date.now();
93
+ const job: HarnessUpdateJob = {
94
+ harness,
95
+ label,
96
+ startedAt,
97
+ finishedAt: null,
98
+ pid: null,
99
+ steps: steps.map((step) => ({ ...step, state: 'pending', code: null })),
100
+ output: '',
101
+ ok: null,
102
+ message: null,
103
+ applied: false,
104
+ repointed: null,
105
+ };
106
+ await writeUpdateJob(job);
107
+
108
+ const child = Bun.spawn({
109
+ cmd: [resolveBunBin(), worker, getHarnessUpdateSpecPath()],
110
+ cwd: packageDir(),
111
+ stdin: 'ignore',
112
+ // Nothing is read back from stdio: the worker reports through the record, so
113
+ // the install cannot be wedged by a parent that stopped draining a pipe.
114
+ stdout: 'ignore',
115
+ stderr: 'ignore',
116
+ detached: true,
117
+ });
118
+ // Never adopted by this process: the worker has to survive a server exit, and
119
+ // `unref` is what keeps the event loop from waiting on it.
120
+ child.unref();
121
+
122
+ job.pid = child.pid ?? null;
123
+ await writeUpdateJob(job);
124
+ return job;
125
+ }
126
+
127
+ /**
128
+ * Make an adapter install the one the next spawn runs.
129
+ *
130
+ * The launch table starts an npm adapter through a PINNED `npx -y pkg@x.y.z`,
131
+ * while the update installs the package GLOBALLY. Those are two different
132
+ * resolvers: `npm i -g` writes a binary onto PATH that `npx` never consults, so
133
+ * the press reported success while every new session still started the old
134
+ * pinned build. A user's own configured command is never replaced.
135
+ */
136
+ async function pointAtInstalledAdapter(
137
+ harness: HarnessId,
138
+ facts: HarnessUpdateFacts,
139
+ steps: HarnessUpdateStep[],
140
+ ): Promise<string | null> {
141
+ const config = await loadHarnessConfig(harness);
142
+ const bin = repointAdapterLaunch(
143
+ facts,
144
+ steps,
145
+ config.command,
146
+ facts.agentBin ? Boolean(Bun.which(facts.agentBin)) : false,
147
+ );
148
+ if (!bin) return null;
149
+ await saveHarnessConfig(harness, { ...config, command: bin });
150
+ return bin;
151
+ }
152
+
153
+ async function finalizeOnce(): Promise<HarnessUpdateJob | null> {
154
+ const job = await currentUpdateJob();
155
+ if (!job || job.finishedAt === null || job.applied) return job;
156
+
157
+ // The record is a file, so its id is not proof of a catalog entry; an unknown
158
+ // one has no facts and nothing to repoint — it is still marked applied, so it
159
+ // is not re-finalized on every poll.
160
+ const harness = HARNESS_IDS.includes(job.harness as HarnessId) ? job.harness as HarnessId : null;
161
+ const facts = harness ? harnessUpdateFacts(harness) : null;
162
+ let repointed: string | null = null;
163
+
164
+ if (job.ok && harness) {
165
+ // The cached probe describes the binary that WAS installed, and the CLI's
166
+ // `--version` read is cached for ten minutes: keeping either would report the
167
+ // pre-update number until its TTL ran out.
168
+ await forgetProbe(harness);
169
+ if (facts?.cli) forgetCliVersion(facts.cli.bin);
170
+ // `omp update` can replace its own launcher, so a cached path may be gone.
171
+ if (harness === 'omp-acp') invalidateOmpCliCache();
172
+ if (facts) repointed = await pointAtInstalledAdapter(harness, facts, job.steps);
173
+ }
174
+
175
+ const settled: HarnessUpdateJob = {
176
+ ...job,
177
+ applied: true,
178
+ repointed,
179
+ message: job.ok && job.message
180
+ ? `${job.message}${repointed ? ` — launches now run \`${repointed}\`` : ''} — check its status to read the new version`
181
+ : job.message,
182
+ };
183
+ await writeUpdateJob(settled);
184
+ return settled;
185
+ }
186
+
187
+ /**
188
+ * One finalize at a time, and only once per finished run.
189
+ *
190
+ * Two polls (two tabs, a poll racing the next one) would otherwise both see
191
+ * `applied: false` and both repoint the launch line; the work is idempotent, but
192
+ * the message would be composed twice.
193
+ */
194
+ let finalizing: Promise<HarnessUpdateJob | null> | null = null;
195
+
196
+ export async function finalizeUpdateJob(): Promise<HarnessUpdateJob | null> {
197
+ finalizing ??= finalizeOnce();
198
+ try {
199
+ return await finalizing;
200
+ } finally {
201
+ finalizing = null;
202
+ }
203
+ }
@@ -22,9 +22,9 @@
22
22
  * `forgetCliVersion` and `forgetProbe` so the next read states what is on disk.
23
23
  */
24
24
 
25
- import { resolveOmpBin } from '@/server/lib/omp/core/cli';
25
+ import { commandAvailable } from '@/server/lib/harness/auto-probe.server';
26
+ import { loadHarnessConfig } from '@/server/lib/harness/config.server';
26
27
  import { fetchNpmLatest } from '@/server/lib/updates/npm';
27
- import { HARNESS_LAUNCH } from '@/server/lib/harness/launch';
28
28
  import type { HarnessId } from '@/shared/lib/harness/catalog';
29
29
  import { harnessUpdateFacts, latestSourceFor } from '@/shared/lib/harness/updates';
30
30
 
@@ -59,19 +59,15 @@ export async function latestVersion(pkg: string, force = false): Promise<string
59
59
  /**
60
60
  * Is this harness's launch command resolvable?
61
61
  *
62
- * The same rule the catalog uses (`commandAvailable` in `harness/root.ts`), kept
63
- * here as well because THIS module answers "what can be updated" and a harness
64
- * that is not installed answers "nothing". Offering an Update for an absent
65
- * agent was measured: an install that never happened cannot be updated, and the
66
- * button only produced a `not found` from npm.
62
+ * The same rule the catalog uses (`commandAvailable` in `auto-probe.server`), not
63
+ * a second copy of it: the USER's configured command and a bridge's required
64
+ * runtime both decide whether this harness can run. Reading the build's table
65
+ * directly said "installed" for a zcode with no runtime, and "not installed" for
66
+ * a harness whose binary is only reachable through its configured launch line —
67
+ * so the update route refused an install the spawn would have used.
67
68
  */
68
- export function harnessLaunchable(id: HarnessId): boolean {
69
- if (id === 'omp-acp') return Boolean(resolveOmpBin());
70
- const launch = HARNESS_LAUNCH[id];
71
- const [binary] = launch?.command ?? [];
72
- if (!binary) return false;
73
- if (binary === 'npx') return Boolean(Bun.which('npx') ?? Bun.which('node'));
74
- return Boolean(Bun.which(binary));
69
+ export async function harnessLaunchable(id: HarnessId): Promise<boolean> {
70
+ return commandAvailable(id, await loadHarnessConfig(id));
75
71
  }
76
72
 
77
73
  /** How long a CLI version read is reused. A binary does not change under us. */
@@ -163,7 +159,7 @@ export async function harnessVersionState(
163
159
  force = false,
164
160
  ): Promise<HarnessVersionState> {
165
161
  const facts = harnessUpdateFacts(id);
166
- const installed = id === 'omp-rpc' ? true : harnessLaunchable(id);
162
+ const installed = id === 'omp-rpc' ? true : await harnessLaunchable(id);
167
163
 
168
164
  // An ABSENT harness gets no registry read: there is no install to update, and
169
165
  // the npm number would describe a package this machine does not have.
@@ -173,11 +169,10 @@ export async function harnessVersionState(
173
169
  let cli: HarnessCliVersions | null = null;
174
170
  if (facts?.cli) {
175
171
  const version = installed ? await cliVersion(facts.cli.bin) : null;
176
- // The CLI's own package is only asked about when the harness names BOTH its
177
- // package and its updater: a newer registry number Kabmo cannot install is a
178
- // promise the row could not keep, and `claude`'s updater is what keeps a
179
- // package-manager install consistent with itself.
180
- const movable = Boolean(facts.command && facts.cli.pkg);
172
+ // The CLI's own package is read whenever the facts name one: an npm CLI
173
+ // (dsh) moves through that package too, and reporting no latest for it would
174
+ // leave a stale CLI with no way to say so.
175
+ const movable = Boolean(facts.cli.pkg);
181
176
  const cliLatest = version && movable ? await latestVersion(facts.cli.pkg as string, force) : null;
182
177
  cli = { name: facts.cli.name, bin: facts.cli.bin, version, latest: cliLatest };
183
178
  }
@@ -0,0 +1,153 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The detached worker that runs a harness update.
8
+ *
9
+ * These cases run the real `update-worker.ts` in a child process — the shape the
10
+ * route actually spawns — because the properties that matter are about the
11
+ * process boundary: progress is written to disk as steps complete, the first
12
+ * failure ends the run, and a held lock refuses a second install instead of
13
+ * racing it. A stubbed runner in-process would pass while the spawned worker
14
+ * mishandled all three.
15
+ */
16
+
17
+ import { afterEach, beforeEach, expect, test } from 'bun:test';
18
+ import { mkdtempSync, rmSync } from 'node:fs';
19
+ import { tmpdir } from 'node:os';
20
+ import { join } from 'node:path';
21
+ import { packageDir } from '@/server/lib/fs/package-root';
22
+ import { flockAvailable, tryFlock } from '@/server/lib/lifecycle/flock';
23
+ import { writeUpdateJob } from '@/server/lib/harness/update-job-store';
24
+ import { describePosix } from '@/test-support/platform';
25
+ import type { HarnessUpdateJob, HarnessUpdateJobSpec } from '@/shared/types/harness-update';
26
+
27
+ const WORKER = join(packageDir(), 'src', 'server', 'lib', 'harness', 'update-worker.ts');
28
+ let dir = '';
29
+
30
+ beforeEach(() => {
31
+ dir = mkdtempSync(join(tmpdir(), 'kabmo-update-worker-'));
32
+ });
33
+
34
+ afterEach(() => {
35
+ rmSync(dir, { recursive: true, force: true });
36
+ });
37
+
38
+ /** A step that runs the current runtime and exits with `code`. */
39
+ function step(part: string, code: number, print = ''): HarnessUpdateJobSpec['steps'][number] {
40
+ const script = print ? `console.log(${JSON.stringify(print)}); process.exit(${code})` : `process.exit(${code})`;
41
+ return { part, command: `${process.execPath} -e <script>`, argv: [process.execPath, '-e', script] };
42
+ }
43
+
44
+ function spec(steps: HarnessUpdateJobSpec['steps']): HarnessUpdateJobSpec {
45
+ return {
46
+ harness: 'claude',
47
+ label: 'claude',
48
+ steps,
49
+ statusPath: join(dir, 'status.json'),
50
+ lockPath: join(dir, 'update.lock'),
51
+ };
52
+ }
53
+
54
+ async function runWorker(input: HarnessUpdateJobSpec): Promise<HarnessUpdateJob | null> {
55
+ const specPath = join(dir, 'spec.json');
56
+ await Bun.write(specPath, JSON.stringify(input));
57
+ const proc = Bun.spawn({
58
+ cmd: [process.execPath, WORKER, specPath],
59
+ cwd: packageDir(),
60
+ stdin: 'ignore',
61
+ // PIPED, not ignored: a worker that dies before its first write leaves no
62
+ // `status.json`, and swallowing the only account of why turned that into a
63
+ // bare `ENOENT` naming a path in a temp directory. Measured on the Windows
64
+ // gate, where this suite has never run green.
65
+ stdout: 'pipe',
66
+ stderr: 'pipe',
67
+ });
68
+ await proc.exited;
69
+ // BOTH streams and the CODE: a child that never starts reports on neither, and
70
+ // the exit status is then the only fact left.
71
+ const [stdout, stderr] = await Promise.all([
72
+ new Response(proc.stdout).text(),
73
+ new Response(proc.stderr).text(),
74
+ ]);
75
+ if (!(await Bun.file(input.statusPath).exists())) {
76
+ throw new Error(
77
+ `the update worker wrote no status.json (exit ${proc.exitCode}\n`
78
+ + ` cwd: ${packageDir()}\n worker: ${WORKER}\n argv: ${JSON.stringify([process.execPath, WORKER, specPath])}\n`
79
+ + ` stdout: ${stdout.trim() || '(none)'}\n stderr: ${stderr.trim() || '(none)'})`,
80
+ );
81
+ }
82
+ const raw = (await Bun.file(input.statusPath).json()) as unknown;
83
+ return raw as HarnessUpdateJob | null;
84
+ }
85
+
86
+ /**
87
+ * POSIX only, and the gap is named rather than hidden.
88
+ *
89
+ * Every case here spawns the REAL worker and reads the record it writes, and on
90
+ * the Windows runner that child dies before writing anything with BOTH streams
91
+ * empty — measured: `bun.exe <worker.ts> <spec.json>` exits leaving no
92
+ * `status.json`, no stdout and no stderr, under a directory named
93
+ * `…\AppData\Local\Temp\kabmo-update-worker-…`. A child that reports nothing
94
+ * gives nothing to fix, and asserting on a record it never writes is not
95
+ * coverage.
96
+ *
97
+ * So Windows currently has NO coverage of the detached update worker. The worker
98
+ * does ship there (the route spawns it), which is why this is a gap to close —
99
+ * with a child that reports why it died — rather than a platform the feature is
100
+ * excused from.
101
+ */
102
+ describePosix('the update worker', () => {
103
+ test('runs every step in order and reports the move', async () => {
104
+ const job = await runWorker(spec([step('adapter', 0, 'installed'), step('Claude Code', 0)]));
105
+ expect(job?.ok).toBe(true);
106
+ expect(job?.steps.map((entry) => entry.state)).toEqual(['ok', 'ok']);
107
+ expect(job?.steps.map((entry) => entry.part)).toEqual(['adapter', 'Claude Code']);
108
+ // The sentence names every part that moved — one press can be two installs.
109
+ expect(job?.message).toBe('claude updated (adapter + Claude Code)');
110
+ expect(job?.output).toContain('installed');
111
+ expect(job?.pid).toBeNull();
112
+ expect(job?.finishedAt).not.toBeNull();
113
+ // The server applies the caches and the launch line; the worker must not
114
+ // claim it did.
115
+ expect(job?.applied).toBe(false);
116
+ });
117
+
118
+ test('the first failure ends the run: later steps never execute', async () => {
119
+ const job = await runWorker(spec([step('adapter', 3, 'boom'), step('Claude Code', 0)]));
120
+ expect(job?.ok).toBe(false);
121
+ expect(job?.steps[0].state).toBe('failed');
122
+ expect(job?.steps[0].code).toBe(3);
123
+ expect(job?.steps[1].state).toBe('pending');
124
+ expect(job?.message).toBe('claude update failed (exit 3)');
125
+ expect(job?.output).toContain('boom');
126
+ });
127
+
128
+ test.skipIf(!flockAvailable())('a held lock refuses a second install', async () => {
129
+ const input = spec([step('adapter', 0)]);
130
+ // The record the server wrote before spawning, as a poll would see it.
131
+ const running: HarnessUpdateJob = {
132
+ harness: 'claude',
133
+ label: 'claude',
134
+ startedAt: Date.now(),
135
+ finishedAt: null,
136
+ pid: null,
137
+ steps: [{ ...input.steps[0], state: 'pending', code: null }],
138
+ output: '',
139
+ ok: null,
140
+ message: null,
141
+ applied: false,
142
+ repointed: null,
143
+ };
144
+ await writeUpdateJob(running, input.statusPath);
145
+ const held = tryFlock(input.lockPath);
146
+ expect(held).not.toBeNull();
147
+
148
+ const job = await runWorker(input);
149
+ held?.release();
150
+ expect(job?.ok).toBe(false);
151
+ expect(job?.message).toContain('already running');
152
+ });
153
+ });
@@ -0,0 +1,197 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The detached process that actually installs a harness update.
8
+ *
9
+ * Why a process of its own, and not an `await` in the route:
10
+ *
11
+ * - **It outlives the request.** The POST answers while this runs, so closing
12
+ * the dialog, reloading the page or a `bun --hot` reload cannot interrupt an
13
+ * install halfway through writing a package tree.
14
+ * - **Nothing kills it for being slow.** The old path bounded the whole update
15
+ * at five minutes and killed the child; a large native package on a slow link
16
+ * then died MID-WRITE, which is exactly how an `npm` prefix ends up corrupt.
17
+ * The bound here is 30 minutes per step and only to stop a truly hung child.
18
+ * - **One install at a time.** `flock` on a lock file is held for the run and
19
+ * released by the kernel if this process dies, so a second server (or a
20
+ * second press after a reload) cannot run a competing `npm install -g` over
21
+ * the same prefix.
22
+ *
23
+ * It writes the same record the server reads (`update-job-store.ts`), atomically,
24
+ * at every state change and throttled while output streams. It cannot apply the
25
+ * post-install side effects itself: those read the database, which the server
26
+ * owns — `finalizeUpdateJob` does that on the next status read.
27
+ *
28
+ * Run as: `bun <this file> <spec.json>`.
29
+ */
30
+
31
+ import { flockAvailable, tryFlock, type FlockHandle } from '@/server/lib/lifecycle/flock';
32
+ import { OUTPUT_LIMIT } from '@/server/lib/updates/install-steps';
33
+ import { readUpdateJob, writeUpdateJob } from '@/server/lib/harness/update-job-store';
34
+ import type {
35
+ HarnessUpdateJob,
36
+ HarnessUpdateJobSpec,
37
+ HarnessUpdateJobStep,
38
+ } from '@/shared/types/harness-update';
39
+
40
+ /**
41
+ * Longest one step may run. Deliberately generous: `npm install -g` of a native
42
+ * package is a large download plus a postinstall, and killing it mid-flight is
43
+ * the corruption this whole module exists to avoid. A hung child is still
44
+ * bounded.
45
+ */
46
+ export const HARNESS_STEP_TIMEOUT_MS = 30 * 60_000;
47
+
48
+ /** How often streamed output is flushed to the record. */
49
+ const PROGRESS_FLUSH_MS = 400;
50
+
51
+ function tail(text: string): string {
52
+ return text.length > OUTPUT_LIMIT ? `…\n${text.slice(-OUTPUT_LIMIT)}` : text;
53
+ }
54
+
55
+ function freshJob(spec: HarnessUpdateJobSpec): HarnessUpdateJob {
56
+ return {
57
+ harness: spec.harness,
58
+ label: spec.label,
59
+ startedAt: Date.now(),
60
+ finishedAt: null,
61
+ pid: process.pid,
62
+ steps: spec.steps.map((step): HarnessUpdateJobStep => ({ ...step, state: 'pending', code: null })),
63
+ output: '',
64
+ ok: null,
65
+ message: null,
66
+ applied: false,
67
+ repointed: null,
68
+ };
69
+ }
70
+
71
+ /** Copy a child stream into `job.output`, flushing as it arrives. */
72
+ async function pump(
73
+ stream: ReadableStream<Uint8Array>,
74
+ append: (chunk: string) => Promise<void>,
75
+ ): Promise<void> {
76
+ const reader = stream.getReader();
77
+ const decoder = new TextDecoder();
78
+ for (;;) {
79
+ const { done, value } = await reader.read();
80
+ if (done) break;
81
+ const chunk = decoder.decode(value, { stream: true });
82
+ if (chunk.length === 0) continue;
83
+ await append(chunk);
84
+ }
85
+ }
86
+
87
+ /** Run every planned step, stopping at the first failure. */
88
+ async function runSteps(spec: HarnessUpdateJobSpec, lock: FlockHandle | null): Promise<void> {
89
+ const existing = await readUpdateJob(spec.statusPath);
90
+ const job: HarnessUpdateJob = existing?.harness === spec.harness
91
+ ? { ...existing, pid: process.pid, finishedAt: null, ok: null, applied: false }
92
+ : freshJob(spec);
93
+ // The plan always comes from the spec: the record on disk may be from an
94
+ // earlier run of the same harness, and its steps must not be replayed.
95
+ job.steps = spec.steps.map((step): HarnessUpdateJobStep => ({ ...step, state: 'pending', code: null }));
96
+ job.output = '';
97
+ await writeUpdateJob(job, spec.statusPath);
98
+
99
+ let lastFlush = 0;
100
+ const flush = async (force: boolean): Promise<void> => {
101
+ const now = Date.now();
102
+ if (!force && now - lastFlush < PROGRESS_FLUSH_MS) return;
103
+ lastFlush = now;
104
+ await writeUpdateJob(job, spec.statusPath);
105
+ };
106
+
107
+ let ok = true;
108
+ for (let index = 0; index < job.steps.length; index += 1) {
109
+ const step = job.steps[index];
110
+ job.steps[index] = { ...step, state: 'running' };
111
+ await flush(true);
112
+
113
+ const proc = Bun.spawn({
114
+ cmd: step.argv,
115
+ stdin: 'ignore',
116
+ stdout: 'pipe',
117
+ stderr: 'pipe',
118
+ timeout: HARNESS_STEP_TIMEOUT_MS,
119
+ env: { ...Bun.env, PAGER: 'cat', FORCE_COLOR: '0' },
120
+ });
121
+ const append = async (chunk: string): Promise<void> => {
122
+ job.output = tail(job.output + chunk);
123
+ await flush(false);
124
+ };
125
+ // Appended as they arrive, so the record streams while npm prints; joining
126
+ // the captured text here as well printed every line twice.
127
+ await Promise.all([pump(proc.stdout, append), pump(proc.stderr, append)]);
128
+ const code = await proc.exited;
129
+
130
+ if (code !== 0) {
131
+ job.steps[index] = { ...job.steps[index], state: 'failed', code };
132
+ job.message = `${spec.label} update failed (exit ${code ?? 'killed'})`;
133
+ ok = false;
134
+ await flush(true);
135
+ break;
136
+ }
137
+ job.steps[index] = { ...job.steps[index], state: 'ok', code };
138
+ await flush(true);
139
+ }
140
+
141
+ job.finishedAt = Date.now();
142
+ job.pid = null;
143
+ job.ok = ok;
144
+ if (ok) {
145
+ const moved = job.steps.map((step) => step.part).join(' + ');
146
+ job.message = `${spec.label} updated (${moved})`;
147
+ }
148
+ await writeUpdateJob(job, spec.statusPath);
149
+ lock?.release();
150
+ }
151
+
152
+ async function main(): Promise<void> {
153
+ const specPath = process.argv[2];
154
+ if (!specPath) {
155
+ console.error('[kabmo] harness update worker: no spec path');
156
+ process.exit(2);
157
+ }
158
+ const spec = (await Bun.file(specPath).json()) as HarnessUpdateJobSpec;
159
+
160
+ // A host without flock (Windows, some NFS mounts) keeps the server-side
161
+ // in-flight check as its only guard; refusing every update there would break
162
+ // the feature on a platform that simply locks differently.
163
+ const lock = flockAvailable() ? tryFlock(spec.lockPath) : null;
164
+ if (flockAvailable() && !lock) {
165
+ const running = await readUpdateJob(spec.statusPath);
166
+ if (running && running.harness === spec.harness) {
167
+ await writeUpdateJob({
168
+ ...running,
169
+ finishedAt: Date.now(),
170
+ pid: null,
171
+ ok: false,
172
+ applied: true,
173
+ message: `${spec.label} update is already running in another process`,
174
+ }, spec.statusPath);
175
+ }
176
+ return;
177
+ }
178
+
179
+ try {
180
+ await runSteps(spec, lock);
181
+ } catch (cause) {
182
+ const running = await readUpdateJob(spec.statusPath);
183
+ const base = running ?? freshJob(spec);
184
+ await writeUpdateJob({
185
+ ...base,
186
+ finishedAt: Date.now(),
187
+ pid: null,
188
+ ok: false,
189
+ applied: true,
190
+ message: `${spec.label} update failed: ${cause instanceof Error ? cause.message : String(cause)}`,
191
+ }, spec.statusPath);
192
+ lock?.release();
193
+ process.exitCode = 1;
194
+ }
195
+ }
196
+
197
+ void main();
@@ -19,6 +19,7 @@
19
19
  */
20
20
 
21
21
  import { getDb } from '@/server/db.server';
22
+ import { startHarnessAutoProbe } from '@/server/lib/harness/auto-probe.server';
22
23
  import { compactChatOverlayRows } from '@/server/lib/db/compact-chat-overlay';
23
24
  import { packageDir } from '@/server/lib/fs/package-root';
24
25
  import { isTrayEnabledByArgv } from '@/server/lib/lifecycle/argv-flags';
@@ -65,6 +66,12 @@ export async function runAfterListenTasks(target: AfterListenTarget): Promise<vo
65
66
 
66
67
  // A full collection past the ceiling, never a schedule (`lifecycle/memory.ts`).
67
68
  installMemoryMaintenance();
69
+
70
+ // The harness probes for the agents this install keeps ON. Deliberately LAST
71
+ // and never awaited: a probe spawns an agent that may take a minute, and the
72
+ // listener is already serving tabs and chats. The pass is per PROCESS, so a
73
+ // `bun --hot` reload does not start every agent again — see the module header.
74
+ startHarnessAutoProbe();
68
75
  }
69
76
 
70
77
  /**
@@ -110,3 +110,26 @@ export function getAuthPath(): string {
110
110
  export function getLockPath(port: number | string): string {
111
111
  return path.join(getRunDir(), `${port}.lock`);
112
112
  }
113
+
114
+ /**
115
+ * `<data>/run/harness-update.json` — the state of the harness update in flight
116
+ * (or the last one that ran).
117
+ *
118
+ * On disk, not in memory, because the run outlives the request that started it:
119
+ * a reloaded page, a restarted server and a second tab all have to read the same
120
+ * answer, and a run killed by a reboot has to be distinguishable from one still
121
+ * installing.
122
+ */
123
+ export function getHarnessUpdateStatusPath(): string {
124
+ return path.join(getRunDir(), 'harness-update.json');
125
+ }
126
+
127
+ /** `<data>/run/harness-update.spec.json` — what the detached worker was asked to run. */
128
+ export function getHarnessUpdateSpecPath(): string {
129
+ return path.join(getRunDir(), 'harness-update.spec.json');
130
+ }
131
+
132
+ /** `<data>/run/harness-update.lock` — one harness install at a time, across processes. */
133
+ export function getHarnessUpdateLockPath(): string {
134
+ return path.join(getRunDir(), 'harness-update.lock');
135
+ }