@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.
- package/dist/client/index.html +1 -1
- package/dist/client/index.js +805 -736
- package/dist/client/static/js/index-bc9aa4mx.css +1 -0
- package/dist/client/static/js/{index-ph0q59rs.js → index-ga8cbpt4.js} +1071 -1171
- package/package.json +2 -4
- package/src/cli/lib/args.js +24 -1
- package/src/cli/lib/args.test.js +16 -0
- package/src/cli/lib/commands/session.js +233 -0
- package/src/cli/lib/commands/session.test.js +103 -0
- package/src/cli/main.js +1 -1
- package/src/client/components/common/ComposerPicker.tsx +3 -0
- package/src/client/components/common/Modal.tsx +2 -6
- package/src/client/components/common/code-surface/shiki.test.ts +82 -0
- package/src/client/components/common/code-surface/shiki.ts +28 -7
- package/src/client/components/common/session-actions-menu/NewChildSessionDialog.tsx +113 -0
- package/src/client/components/common/session-actions-menu/index.tsx +33 -2
- package/src/client/components/common/session-row/StatusSlot.tsx +4 -4
- package/src/client/components/common/session-row/index.tsx +27 -1
- package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +7 -1
- package/src/client/components/layout/desktop-layout/resizer/separator-drag.ts +3 -3
- package/src/client/components/layout/session-sidebar/CategoryItem.tsx +25 -12
- package/src/client/components/layout/session-sidebar/ChildSessionList.tsx +66 -0
- package/src/client/components/layout/session-sidebar/SessionItem.tsx +28 -3
- package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.test.tsx +20 -0
- package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.tsx +59 -12
- package/src/client/components/layout/session-sidebar/about-modal/HarnessTableLog.test.tsx +94 -0
- package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.test.tsx +130 -0
- package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.tsx +120 -0
- package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.tsx +47 -0
- package/src/client/components/layout/session-sidebar/about-modal/index.test.tsx +122 -0
- package/src/client/components/layout/session-sidebar/about-modal/index.tsx +55 -46
- package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +18 -5
- package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +21 -4
- package/src/client/components/mobile/mobile-session-sidebar/SessionRow.tsx +27 -5
- package/src/client/components/settings/categories/harness-settings/EnvProviderForm.tsx +31 -0
- package/src/client/components/settings/categories/harness-settings/provider-panel.test.tsx +49 -0
- package/src/client/components/workspace/chat-timeline/ChatOverlays.tsx +5 -2
- package/src/client/components/workspace/chat-timeline/ComposerDock.tsx +64 -70
- package/src/client/components/workspace/chat-timeline/ComposerDockFrame.tsx +35 -0
- package/src/client/components/workspace/chat-timeline/EmptyWorkspacePrompt.tsx +135 -118
- package/src/client/components/workspace/chat-timeline/MessageItem.tsx +22 -1
- package/src/client/components/workspace/chat-timeline/MessageList.test.ts +14 -0
- package/src/client/components/workspace/chat-timeline/MessageList.tsx +4 -0
- package/src/client/components/workspace/chat-timeline/TimelineBody.tsx +3 -0
- package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +4 -3
- package/src/client/components/workspace/chat-timeline/composer-dock-frame.test.tsx +155 -0
- package/src/client/components/workspace/chat-timeline/index.tsx +5 -2
- package/src/client/components/workspace/chat-timeline/pending-harness.test.tsx +1 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/FrameControl.tsx +11 -20
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/GroupedAsk.tsx +1 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/QuestionBlock.tsx +1 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/drafts.ts +1 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/index.tsx +2 -1
- package/src/client/components/workspace/file-explorer/index.test.ts +25 -0
- package/src/client/components/workspace/file-explorer/index.tsx +9 -3
- package/src/client/components/workspace/model-dropdown/harness-models.test.ts +17 -0
- package/src/client/components/workspace/model-dropdown/harness-models.ts +33 -18
- package/src/client/components/workspace/model-dropdown/index.tsx +3 -3
- package/src/client/components/workspace/model-dropdown/popover-metrics.ts +4 -1
- package/src/client/components/workspace/terminal-panel/KeyBar.tsx +10 -8
- package/src/client/components/workspace/terminal-panel/{RealtimeXtermView.tsx → RealtimeTerminalView.tsx} +51 -58
- package/src/client/components/workspace/terminal-panel/fit.test.ts +37 -0
- package/src/client/components/workspace/terminal-panel/fit.ts +64 -0
- package/src/client/components/workspace/terminal-panel/fonts.test.ts +161 -0
- package/src/client/components/workspace/terminal-panel/fonts.ts +164 -0
- package/src/client/components/workspace/terminal-panel/grid.test.ts +52 -0
- package/src/client/components/workspace/terminal-panel/grid.ts +118 -0
- package/src/client/components/workspace/terminal-panel/index.tsx +25 -21
- package/src/client/components/workspace/terminal-panel/mount.ts +247 -223
- package/src/client/components/workspace/terminal-panel/navigation.test.ts +54 -0
- package/src/client/components/workspace/terminal-panel/navigation.ts +47 -0
- package/src/client/data/settings/omp-schema.json +5 -5
- package/src/client/data/theme/terminal.test.ts +25 -0
- package/src/client/data/theme/terminal.ts +96 -79
- package/src/client/hooks/chat/composer/items.test.ts +12 -26
- package/src/client/hooks/chat/composer/pick-mapping.test.ts +42 -0
- package/src/client/hooks/chat/omp/prompt-send.test.ts +7 -7
- package/src/client/hooks/chat/omp/prompt-send.ts +26 -14
- package/src/client/hooks/chat/timeline/actions.ts +2 -2
- package/src/client/hooks/chat/timeline/ask-frames.ts +5 -4
- package/src/client/hooks/chat/timeline/new-chat-open.test.ts +86 -0
- package/src/client/hooks/chat/timeline/new-chat-open.ts +23 -5
- package/src/client/hooks/chat/timeline/new-chat.test.ts +32 -4
- package/src/client/hooks/chat/timeline/new-chat.ts +18 -6
- package/src/client/hooks/chat/timeline/prompt-text.test.ts +34 -1
- package/src/client/hooks/chat/timeline/prompt-text.ts +47 -20
- package/src/client/hooks/chat/timeline/rewind-actions.test.ts +24 -1
- package/src/client/hooks/chat/timeline/rewind-actions.ts +15 -11
- package/src/client/hooks/chat/timeline/run-identity.test.ts +52 -0
- package/src/client/hooks/chat/timeline/run-identity.ts +23 -11
- package/src/client/hooks/chat/timeline/send-execute.test.ts +56 -0
- package/src/client/hooks/chat/timeline/send-helpers.ts +30 -1
- package/src/client/hooks/chat/timeline/send-props.ts +5 -3
- package/src/client/hooks/chat/timeline/send-steer.test.ts +4 -4
- package/src/client/hooks/chat/timeline/send.test-util.ts +8 -2
- package/src/client/hooks/chat/timeline/send.test.ts +4 -4
- package/src/client/hooks/chat/timeline/send.ts +59 -53
- package/src/client/hooks/chat/timeline/session-load.test-util.ts +6 -2
- package/src/client/hooks/chat/timeline/session-load.test.ts +31 -0
- package/src/client/hooks/chat/timeline/session-load.ts +6 -14
- package/src/client/hooks/chat/timeline/session-shapes.ts +32 -0
- package/src/client/hooks/chat/timeline/use-fork-session.ts +49 -0
- package/src/client/hooks/models/harness-catalog.ts +114 -0
- package/src/client/hooks/models/provider-writes.test.ts +81 -0
- package/src/client/hooks/models/provider-writes.ts +10 -3
- package/src/client/hooks/models/use-harness-update.test.ts +193 -0
- package/src/client/hooks/models/use-harness-update.ts +202 -0
- package/src/client/hooks/models/use-harnesses-controller.ts +57 -0
- package/src/client/hooks/models/use-harnesses-signal.test.ts +128 -0
- package/src/client/hooks/models/use-harnesses.test.ts +2 -1
- package/src/client/hooks/models/use-harnesses.ts +48 -114
- package/src/client/hooks/workspace/child-expansion.ts +33 -0
- package/src/client/hooks/workspace/terminal/index.ts +17 -3
- package/src/client/hooks/workspace/terminal/socket.ts +9 -4
- package/src/client/lib/session-tree.test.ts +50 -0
- package/src/client/lib/session-tree.ts +59 -0
- package/src/client/tailwind.css +26 -12
- package/src/server/index.ts +2 -0
- package/src/server/lib/auth/guard.ts +10 -0
- package/src/server/lib/btw/session-copy.server.ts +6 -44
- package/src/server/lib/btw/session-copy.test.ts +2 -1
- package/src/server/lib/db/schema.ts +4 -0
- package/src/server/lib/db/seed-terminal.test.ts +1 -1
- package/src/server/lib/db/seed.test.ts +1 -1
- package/src/server/lib/harness/acp/activation.test.ts +81 -0
- package/src/server/lib/harness/acp/activation.ts +72 -0
- package/src/server/lib/harness/acp/background-work.test.ts +262 -0
- package/src/server/lib/harness/acp/background-work.ts +320 -0
- package/src/server/lib/harness/acp/client.ts +2 -2
- package/src/server/lib/harness/acp/frames.test.ts +50 -1
- package/src/server/lib/harness/acp/frames.ts +17 -5
- package/src/server/lib/harness/acp/gateway-env.test.ts +64 -0
- package/src/server/lib/harness/acp/gateway-env.ts +70 -0
- package/src/server/lib/harness/acp/mapper.ts +6 -4
- package/src/server/lib/harness/acp/prompt.test.ts +20 -0
- package/src/server/lib/harness/acp/prompt.ts +19 -3
- package/src/server/lib/harness/acp/session-background.test.ts +129 -0
- package/src/server/lib/harness/acp/session-config.test.ts +73 -0
- package/src/server/lib/harness/acp/session-config.ts +87 -6
- package/src/server/lib/harness/acp/session-exit.ts +31 -0
- package/src/server/lib/harness/acp/session-handshake.test.ts +108 -0
- package/src/server/lib/harness/acp/session-handshake.ts +36 -3
- package/src/server/lib/harness/acp/session-idle.ts +51 -0
- package/src/server/lib/harness/acp/session-listeners.ts +31 -0
- package/src/server/lib/harness/acp/session-notifications.ts +19 -3
- package/src/server/lib/harness/acp/session-observers.test.ts +9 -0
- package/src/server/lib/harness/acp/session-options.ts +33 -0
- package/src/server/lib/harness/acp/session-reaper.test.ts +96 -0
- package/src/server/lib/harness/acp/session-requests.ts +47 -0
- package/src/server/lib/harness/acp/session-turns.ts +20 -5
- package/src/server/lib/harness/acp/session.test-util.ts +54 -3
- package/src/server/lib/harness/acp/session.test.ts +65 -3
- package/src/server/lib/harness/acp/session.ts +79 -79
- package/src/server/lib/harness/acp/turn.test.ts +81 -0
- package/src/server/lib/harness/acp/turn.ts +54 -13
- package/src/server/lib/harness/acp/verbs.ts +12 -2
- package/src/server/lib/harness/auto-probe.server.test.ts +121 -0
- package/src/server/lib/harness/auto-probe.server.ts +178 -0
- package/src/server/lib/harness/config.server.test.ts +1 -1
- package/src/server/lib/harness/config.server.ts +1 -1
- package/src/server/lib/harness/launch-table.ts +195 -0
- package/src/server/lib/harness/launch.test.ts +37 -0
- package/src/server/lib/harness/launch.ts +16 -116
- package/src/server/lib/harness/provider-entry.ts +32 -0
- package/src/server/lib/harness/provider-file.server.ts +3 -0
- package/src/server/lib/harness/provider-sources.server.test.ts +45 -0
- package/src/server/lib/harness/provider-sources.server.ts +26 -2
- package/src/server/lib/harness/provider-write.server.ts +21 -32
- package/src/server/lib/harness/provider-zcode.server.test.ts +270 -0
- package/src/server/lib/harness/provider-zcode.server.ts +324 -0
- package/src/server/lib/harness/spawn.server.ts +15 -2
- package/src/server/lib/harness/update-job-store.test.ts +126 -0
- package/src/server/lib/harness/update-job-store.ts +109 -0
- package/src/server/lib/harness/update-job.server.ts +203 -0
- package/src/server/lib/harness/update-state.server.ts +15 -20
- package/src/server/lib/harness/update-worker.test.ts +153 -0
- package/src/server/lib/harness/update-worker.ts +197 -0
- package/src/server/lib/lifecycle/after-listen.ts +7 -0
- package/src/server/lib/lifecycle/paths.ts +23 -0
- package/src/server/lib/models/registry-refresh.server.test.ts +87 -0
- package/src/server/lib/models/registry-refresh.server.ts +98 -0
- package/src/server/lib/omp/rpc/child-config.ts +5 -4
- package/src/server/lib/omp/rpc/constants.test.ts +2 -1
- package/src/server/lib/omp/rpc/forked-resume.test.ts +14 -3
- package/src/server/lib/omp/rpc/manager.ts +5 -5
- package/src/server/lib/omp/rpc/prewarm-pool.ts +12 -3
- package/src/server/lib/omp/rpc/session-registry.ts +14 -2
- package/src/server/lib/omp/rpc/terminal-settle.ts +53 -5
- package/src/server/lib/omp/session/delete-flow.server.ts +62 -8
- package/src/server/lib/omp/session/delete.server.ts +17 -0
- package/src/server/lib/omp/session/rewind-file.ts +39 -21
- package/src/server/lib/omp/session/sidebar-data.server.ts +6 -6
- package/src/server/lib/omp/session/transcript-copy.ts +123 -0
- package/src/server/lib/sessions/agent-token.server.test.ts +64 -0
- package/src/server/lib/sessions/agent-token.server.ts +134 -0
- package/src/server/lib/sessions/answers.server.test.ts +33 -0
- package/src/server/lib/sessions/answers.server.ts +37 -0
- package/src/server/lib/sessions/dispatch.server.test.ts +143 -0
- package/src/server/lib/sessions/dispatch.server.ts +128 -0
- package/src/server/lib/sessions/fork.server.test.ts +209 -0
- package/src/server/lib/sessions/fork.server.ts +304 -0
- package/src/server/lib/sessions/lifecycle.server.test.ts +75 -0
- package/src/server/lib/sessions/lifecycle.server.ts +71 -0
- package/src/server/lib/sessions/notify.server.test.ts +112 -0
- package/src/server/lib/sessions/notify.server.ts +212 -0
- package/src/server/lib/sessions/relation-types.ts +76 -0
- package/src/server/lib/sessions/relations.server.test.ts +207 -0
- package/src/server/lib/sessions/relations.server.ts +337 -0
- package/src/server/lib/sessions/resolve.server.ts +106 -0
- package/src/server/lib/sessions/schema.server.ts +82 -0
- package/src/server/lib/sessions/sidebar-relations.server.test.ts +61 -0
- package/src/server/lib/sessions/sidebar-relations.server.ts +47 -0
- package/src/server/lib/sessions/spawn.server.test.ts +196 -0
- package/src/server/lib/sessions/spawn.server.ts +310 -0
- package/src/server/lib/terminal/attach-replay.test.ts +2 -2
- package/src/server/lib/terminal/output.test.ts +2 -2
- package/src/server/lib/terminal/output.ts +1 -1
- package/src/server/lib/terminal/resize.test.ts +99 -0
- package/src/server/lib/terminal/runtime.server.ts +26 -6
- package/src/server/lib/terminal/send-result.test.ts +18 -1
- package/src/server/lib/terminal/send-result.ts +26 -2
- package/src/server/lib/terminal/session.server.ts +6 -6
- package/src/server/lib/usage/new-api.test.ts +92 -0
- package/src/server/lib/usage/new-api.ts +131 -0
- package/src/server/lib/usage/provider-endpoint.test.ts +106 -0
- package/src/server/lib/usage/provider-key.ts +33 -0
- package/src/server/lib/usage/providers.server.ts +10 -3
- package/src/server/lib/usage/vendor-balance.server.ts +4 -53
- package/src/server/lib/usage/vendor-http.ts +41 -0
- package/src/server/lib/usage/window.ts +55 -0
- package/src/server/routes/agent/command.ts +14 -1
- package/src/server/routes/agent/harness-command.ts +6 -0
- package/src/server/routes/agent/new.ts +15 -0
- package/src/server/routes/chat/rewind.test.ts +147 -0
- package/src/server/routes/chat/rewind.ts +29 -7
- package/src/server/routes/chat/session.test.ts +165 -10
- package/src/server/routes/chat/session.ts +60 -7
- package/src/server/routes/harness/add-provider.test.ts +9 -1
- package/src/server/routes/harness/import-action.ts +22 -4
- package/src/server/routes/harness/import.test.ts +1 -1
- package/src/server/routes/harness/index.ts +5 -1
- package/src/server/routes/harness/root.ts +8 -49
- package/src/server/routes/harness/toggle.test.ts +20 -0
- package/src/server/routes/harness/update-status.ts +26 -0
- package/src/server/routes/harness/update.test.ts +119 -0
- package/src/server/routes/harness/update.ts +67 -113
- package/src/server/routes/harness/versions.ts +5 -1
- package/src/server/routes/sessions/answer.ts +24 -0
- package/src/server/routes/sessions/batch-delete.ts +11 -5
- package/src/server/routes/sessions/delete-acp.test.ts +227 -0
- package/src/server/routes/sessions/delete.ts +4 -2
- package/src/server/routes/sessions/fork.ts +74 -0
- package/src/server/routes/sessions/index.ts +14 -0
- package/src/server/routes/sessions/relations.ts +85 -0
- package/src/server/routes/sessions/session.ts +4 -0
- package/src/server/routes/sessions/spawn.ts +134 -0
- package/src/server/routes/settings/model-override.ts +5 -2
- package/src/server/routes/settings/provider-entry.ts +2 -2
- package/src/server/routes/settings/provider-model.ts +2 -2
- package/src/server/routes/settings/provider-models.ts +2 -2
- package/src/server/routes/settings/provider-refresh.ts +6 -4
- package/src/server/routes/settings/providers.ts +3 -1
- package/src/server/routes/settings/settings-config-routes.test.ts +10 -2
- package/src/server/routes/terminal/ws.ts +3 -3
- package/src/shared/lib/chat/ask-form/fields.ts +76 -0
- package/src/shared/lib/chat/ask-form/index.ts +323 -0
- package/src/shared/lib/chat/ask-form/tokens.ts +199 -0
- package/src/shared/lib/chat/ask-form/types.ts +43 -0
- package/src/shared/lib/chat/ask-form.test.ts +137 -0
- package/src/shared/lib/chat/ask-questions.test.ts +65 -1
- package/src/shared/lib/chat/ask-questions.ts +51 -145
- package/src/shared/lib/chat/ask-results.ts +198 -0
- package/src/shared/lib/chat/composer/client.ts +31 -2
- package/src/shared/lib/chat/composer/delegation.test.ts +93 -0
- package/src/shared/lib/chat/composer/delegation.ts +206 -0
- package/src/shared/lib/chat/notice-text.ts +1 -1
- package/src/shared/lib/chat/timeline/omp-callbacks.ts +13 -1
- package/src/shared/lib/chat/timeline/prompt-error.test.ts +146 -0
- package/src/shared/lib/chat/timeline/prompt-error.ts +88 -0
- package/src/shared/lib/chat/tool/summary-wait.test.ts +11 -0
- package/src/shared/lib/code/ansi.ts +1 -1
- package/src/shared/lib/fonts/nerd-symbols.css +4 -3
- package/src/shared/lib/fonts/nerd-symbols.test.ts +9 -9
- package/src/shared/lib/harness/catalog.test.ts +1 -1
- package/src/shared/lib/harness/config-fields.test.ts +4 -0
- package/src/shared/lib/harness/config-fields.ts +19 -4
- package/src/shared/lib/harness/dialects.test.ts +8 -0
- package/src/shared/lib/harness/dialects.ts +12 -0
- package/src/shared/lib/harness/provider-facts.test.ts +59 -8
- package/src/shared/lib/harness/provider-facts.ts +46 -11
- package/src/shared/lib/harness/providers.ts +44 -1
- package/src/shared/lib/harness/rewind.ts +24 -0
- package/src/shared/lib/harness/update-job.ts +33 -0
- package/src/shared/lib/harness/update-plan.test.ts +69 -4
- package/src/shared/lib/harness/update-plan.ts +57 -9
- package/src/shared/lib/harness/updates.test.ts +22 -0
- package/src/shared/lib/harness/updates.ts +24 -1
- package/src/shared/lib/models/client.ts +10 -1
- package/src/shared/lib/omp/dialog-methods.ts +4 -3
- package/src/shared/lib/omp/session/wait-idle.test.ts +11 -0
- package/src/shared/lib/omp/session/wait-idle.ts +12 -2
- package/src/shared/lib/theme/catalog.test.ts +13 -9
- package/src/shared/lib/theme/color.test.ts +2 -2
- package/src/shared/lib/theme/color.ts +4 -4
- package/src/shared/lib/theme/css.ts +2 -0
- package/src/shared/lib/theme/ink-scale.ts +21 -1
- package/src/shared/lib/theme/theme.test.ts +26 -10
- package/src/shared/lib/ui/safe-area.test.ts +28 -0
- package/src/shared/lib/ui/safe-area.ts +42 -0
- package/src/shared/lib/workspace/terminal/input.test.ts +27 -0
- package/src/shared/lib/workspace/terminal/input.ts +33 -0
- package/src/shared/lib/workspace/terminal/keys.test.ts +1 -1
- package/src/shared/lib/workspace/terminal/keys.ts +7 -7
- package/src/shared/types/chat.ts +1 -1
- package/src/shared/types/harness-update.ts +65 -0
- package/src/shared/types/harness.ts +30 -4
- package/src/shared/types/omp/agent.ts +19 -2
- package/src/shared/types/workspace.ts +4 -0
- package/src/test-support/fake-omp-fork.ts +16 -2
- package/src/test-support/file-explorer-render.test-util.ts +37 -0
- package/src/test-support/file-explorer.test-util.ts +20 -18
- package/src/test-support/harness-table.test-util.ts +21 -4
- package/dist/client/static/js/index-jhmg4w91.css +0 -1
- package/src/client/components/workspace/terminal-panel/webgl.test.ts +0 -75
- 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 {
|
|
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 `
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
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
|
-
|
|
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
|
|
177
|
-
//
|
|
178
|
-
//
|
|
179
|
-
|
|
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
|
+
}
|