@kabmo-labs/kabmo 0.24.0 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/index.html +1 -1
- package/dist/client/index.js +585 -512
- package/dist/client/static/js/{index-jcr5twtp.js → index-95b3n20k.js} +1105 -1207
- package/dist/client/static/js/index-p2yw9ywf.css +1 -0
- 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/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/index.tsx +59 -2
- package/src/client/components/common/session-branch/BranchGuides.tsx +93 -0
- package/src/client/components/common/session-branch/SubagentRow.tsx +77 -0
- package/src/client/components/common/session-branch/index.tsx +169 -0
- package/src/client/components/common/session-branch/session-branch.test.tsx +240 -0
- package/src/client/components/common/session-branch/style.ts +29 -0
- package/src/client/components/common/session-row/StatusSlot.tsx +17 -5
- package/src/client/components/common/session-row/index.tsx +28 -3
- package/src/client/components/common/session-row/session-row.test.tsx +11 -0
- package/src/client/components/common/session-state-provider/index.tsx +1 -1
- package/src/client/components/layout/desktop-layout/resizer/separator-drag.ts +3 -3
- package/src/client/components/layout/session-sidebar/CategoryItem.tsx +70 -61
- package/src/client/components/layout/session-sidebar/SessionItem.batch.test.ts +3 -3
- package/src/client/components/layout/session-sidebar/SessionItem.test.ts +25 -2
- package/src/client/components/layout/session-sidebar/SessionItem.tsx +35 -7
- package/src/client/components/layout/session-sidebar/about-modal/HarnessRow.tsx +261 -0
- package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.tsx +39 -198
- package/src/client/components/layout/session-sidebar/about-modal/HarnessTableLog.test.tsx +114 -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 +128 -0
- package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.tsx +56 -0
- package/src/client/components/layout/session-sidebar/about-modal/index.tsx +44 -36
- package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +62 -47
- package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +63 -50
- package/src/client/components/mobile/mobile-session-sidebar/SessionRow.test.ts +5 -0
- package/src/client/components/mobile/mobile-session-sidebar/SessionRow.tsx +37 -12
- package/src/client/components/settings/Sidebar.tsx +1 -1
- package/src/client/components/settings/categories/NotificationSettings.test.tsx +92 -0
- package/src/client/components/settings/categories/NotificationSettings.tsx +112 -2
- package/src/client/components/settings/categories/harness-settings/HarnessCard.tsx +1 -1
- package/src/client/components/workspace/chat-timeline/AiMessageFooter.fork.test.ts +94 -0
- package/src/client/components/workspace/chat-timeline/AiMessageFooter.tsx +31 -5
- 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 +128 -122
- package/src/client/components/workspace/chat-timeline/MessageItem.tsx +22 -1
- package/src/client/components/workspace/chat-timeline/MessageList.tsx +11 -5
- package/src/client/components/workspace/chat-timeline/PendingChatView.tsx +10 -0
- package/src/client/components/workspace/chat-timeline/RunFooter.tsx +12 -3
- package/src/client/components/workspace/chat-timeline/TimelineBody.tsx +3 -0
- package/src/client/components/workspace/chat-timeline/chat-input/AttachmentToolbar.tsx +2 -0
- package/src/client/components/workspace/chat-timeline/chat-input/ModeToggles.tsx +15 -1
- package/src/client/components/workspace/chat-timeline/chat-input/SessionModeDropdown.test.tsx +85 -0
- package/src/client/components/workspace/chat-timeline/chat-input/SessionModeDropdown.tsx +84 -0
- package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.tsx +2 -0
- package/src/client/components/workspace/chat-timeline/chat-input/modes-props.ts +5 -0
- package/src/client/components/workspace/chat-timeline/composer-dock-frame.test.tsx +155 -0
- package/src/client/components/workspace/chat-timeline/index.tsx +11 -20
- package/src/client/components/workspace/chat-timeline/target-picker/DraftTargetRow.tsx +115 -0
- package/src/client/components/workspace/chat-timeline/target-picker/ParentSessionPicker.tsx +173 -0
- package/src/client/components/workspace/chat-timeline/target-picker/parent-session-options.test.ts +49 -0
- 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 +1 -1
- 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 +31 -26
- package/src/client/hooks/chat/composer/pick-mapping.test.ts +42 -0
- package/src/client/hooks/chat/omp/folder-auto-expand.ts +1 -1
- package/src/client/hooks/chat/omp/pending-adoption.ts +51 -0
- package/src/client/hooks/chat/omp/pending-child.test.ts +73 -0
- package/src/client/hooks/chat/omp/pending-entry.ts +59 -0
- package/src/client/hooks/chat/omp/pending-rows.ts +6 -2
- package/src/client/hooks/chat/omp/pending-sessions.test.ts +1 -2
- package/src/client/hooks/chat/omp/pending-sessions.ts +45 -69
- package/src/client/hooks/chat/omp/pending-waiting.ts +1 -1
- 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/omp/run-switch.ts +1 -1
- package/src/client/hooks/chat/omp/session-list.tsx +4 -0
- package/src/client/hooks/chat/subagents/roster.ts +122 -0
- package/src/client/hooks/chat/timeline/child-spawn.test.ts +55 -0
- package/src/client/hooks/chat/timeline/child-spawn.ts +45 -0
- package/src/client/hooks/chat/timeline/fork-request.test.ts +54 -0
- package/src/client/hooks/chat/timeline/fork-request.ts +43 -0
- package/src/client/hooks/chat/timeline/mode-availability.test.ts +1 -1
- package/src/client/hooks/chat/timeline/mode-availability.ts +33 -0
- package/src/client/hooks/chat/timeline/mode-markers.ts +7 -1
- package/src/client/hooks/chat/timeline/mode-post.ts +15 -3
- package/src/client/hooks/chat/timeline/modes-contract.ts +9 -4
- package/src/client/hooks/chat/timeline/modes.test.ts +56 -18
- package/src/client/hooks/chat/timeline/modes.ts +42 -29
- package/src/client/hooks/chat/timeline/pending-branch.ts +205 -0
- package/src/client/hooks/chat/timeline/pending-dispatch.ts +253 -0
- 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/send-execute.test.ts +39 -0
- package/src/client/hooks/chat/timeline/send-helpers.ts +30 -1
- package/src/client/hooks/chat/timeline/send-steer.test.ts +3 -1
- package/src/client/hooks/chat/timeline/send.test-util.ts +7 -1
- package/src/client/hooks/chat/timeline/send.test.ts +3 -1
- package/src/client/hooks/chat/timeline/send.ts +48 -133
- package/src/client/hooks/chat/timeline/session-harness.ts +6 -0
- package/src/client/hooks/chat/timeline/stop-with-queue.ts +42 -0
- package/src/client/hooks/chat/timeline/use-fork-session.ts +58 -0
- package/src/client/hooks/models/use-harness-update.test.ts +286 -0
- package/src/client/hooks/models/use-harness-update.ts +250 -0
- package/src/client/hooks/ui/input-required-alert.ts +12 -5
- package/src/client/hooks/ui/notifications.ts +45 -0
- package/src/client/hooks/ui/run-outcome-alert.ts +48 -0
- package/src/client/hooks/workspace/expanded-sessions.ts +36 -7
- package/src/client/hooks/workspace/terminal/index.ts +17 -3
- package/src/client/hooks/workspace/terminal/socket.ts +9 -4
- package/src/client/lib/native/open-session.ts +33 -0
- package/src/client/lib/notifications/os.test.ts +125 -0
- package/src/client/lib/notifications/os.ts +130 -0
- package/src/client/lib/plugins/sidebar-bridge.ts +2 -20
- package/src/client/lib/router/open-session.test.ts +75 -0
- package/src/client/lib/router/open-session.ts +34 -0
- package/src/client/lib/session-tree.test.ts +50 -0
- package/src/client/lib/session-tree.ts +59 -0
- package/src/client/lib/signals.ts +5 -1
- package/src/client/main.tsx +6 -0
- package/src/client/tailwind.css +17 -11
- 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 +5 -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/background-work.test.ts +262 -0
- package/src/server/lib/harness/acp/background-work.ts +320 -0
- package/src/server/lib/harness/acp/calls.ts +15 -1
- package/src/server/lib/harness/acp/client-capabilities.test.ts +55 -0
- package/src/server/lib/harness/acp/client-capabilities.ts +61 -0
- package/src/server/lib/harness/acp/client-fs.test.ts +135 -0
- package/src/server/lib/harness/acp/client-fs.ts +131 -0
- package/src/server/lib/harness/acp/client-initialize.test.ts +70 -0
- package/src/server/lib/harness/acp/client-initialize.ts +84 -0
- package/src/server/lib/harness/acp/client.ts +33 -23
- package/src/server/lib/harness/acp/commands.test.ts +5 -5
- package/src/server/lib/harness/acp/commands.ts +14 -2
- package/src/server/lib/harness/acp/frames-timing.test.ts +68 -0
- package/src/server/lib/harness/acp/frames-v2.test.ts +85 -0
- package/src/server/lib/harness/acp/frames.ts +71 -158
- 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.test.ts +40 -0
- package/src/server/lib/harness/acp/mapper.ts +12 -0
- package/src/server/lib/harness/acp/permissions.ts +6 -0
- 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/protocol.ts +41 -2
- package/src/server/lib/harness/acp/session-background.test.ts +129 -0
- package/src/server/lib/harness/acp/session-exit.ts +31 -0
- package/src/server/lib/harness/acp/session-facts.ts +1 -1
- package/src/server/lib/harness/acp/session-handshake.test.ts +197 -0
- package/src/server/lib/harness/acp/session-handshake.ts +110 -7
- package/src/server/lib/harness/acp/session-idle.ts +7 -6
- package/src/server/lib/harness/acp/session-listeners.ts +31 -0
- package/src/server/lib/harness/acp/session-notifications.ts +41 -2
- package/src/server/lib/harness/acp/session-observers.test.ts +63 -0
- package/src/server/lib/harness/acp/session-permissions.test.ts +29 -0
- package/src/server/lib/harness/acp/session-permissions.ts +36 -0
- package/src/server/lib/harness/acp/session-requests.test.ts +92 -0
- package/src/server/lib/harness/acp/session-requests.ts +89 -0
- package/src/server/lib/harness/acp/session-turns.ts +28 -8
- package/src/server/lib/harness/acp/session-usage.ts +4 -0
- package/src/server/lib/harness/acp/session-web-state.ts +30 -2
- package/src/server/lib/harness/acp/session.test-util.ts +42 -2
- package/src/server/lib/harness/acp/session.ts +115 -115
- package/src/server/lib/harness/acp/tool-calls.ts +223 -0
- package/src/server/lib/harness/acp/turn.test.ts +183 -0
- package/src/server/lib/harness/acp/turn.ts +102 -10
- package/src/server/lib/harness/acp/v2.test.ts +144 -0
- package/src/server/lib/harness/acp/v2.ts +220 -0
- package/src/server/lib/harness/acp/verbs.ts +58 -8
- package/src/server/lib/harness/launch.test.ts +25 -0
- package/src/server/lib/harness/launch.ts +24 -10
- package/src/server/lib/harness/session.ts +3 -0
- package/src/server/lib/harness/spawn.server.ts +15 -2
- package/src/server/lib/harness/update-job-store.test.ts +151 -0
- package/src/server/lib/harness/update-job-store.ts +115 -0
- package/src/server/lib/harness/update-job.server.ts +94 -0
- package/src/server/lib/harness/update-queue.server.ts +292 -0
- package/src/server/lib/harness/update-queue.test.ts +265 -0
- package/src/server/lib/harness/update-spawn.server.ts +96 -0
- package/src/server/lib/harness/update-worker.test.ts +153 -0
- package/src/server/lib/harness/update-worker.ts +202 -0
- package/src/server/lib/lifecycle/after-listen.ts +9 -0
- package/src/server/lib/lifecycle/paths.ts +28 -0
- 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/run-settle.server.ts +10 -1
- package/src/server/lib/omp/rpc/session-registry.ts +14 -2
- package/src/server/lib/omp/rpc/subagent-liveness.ts +5 -5
- package/src/server/lib/omp/rpc/terminal-settle.ts +58 -6
- package/src/server/lib/omp/session/delete-flow.server.ts +48 -0
- package/src/server/lib/omp/session/delete.server.ts +5 -0
- package/src/server/lib/omp/session/rewind-file.ts +39 -21
- package/src/server/lib/omp/session/sidebar-data.server.ts +16 -18
- 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 +277 -0
- package/src/server/lib/sessions/fork.server.ts +329 -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 +237 -0
- package/src/server/lib/sessions/spawn.server.ts +335 -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 +12 -5
- package/src/server/routes/agent/new.ts +15 -0
- package/src/server/routes/chat/stream.ts +7 -2
- package/src/server/routes/harness/add-provider.test.ts +9 -1
- package/src/server/routes/harness/import-action.ts +4 -0
- package/src/server/routes/harness/import.test.ts +1 -1
- package/src/server/routes/harness/index.ts +5 -1
- package/src/server/routes/harness/update-status.ts +32 -0
- package/src/server/routes/harness/update.test.ts +156 -0
- package/src/server/routes/harness/update.ts +64 -147
- package/src/server/routes/sessions/answer.ts +24 -0
- package/src/server/routes/sessions/batch-delete.ts +11 -5
- package/src/server/routes/sessions/body-readers.ts +43 -0
- package/src/server/routes/sessions/delete.ts +4 -2
- package/src/server/routes/sessions/fork.ts +89 -0
- package/src/server/routes/sessions/index.ts +14 -0
- package/src/server/routes/sessions/relations.ts +85 -0
- package/src/server/routes/sessions/routes.test.ts +1 -2
- package/src/server/routes/sessions/session.ts +23 -0
- package/src/server/routes/sessions/spawn.ts +137 -0
- package/src/server/routes/terminal/ws.ts +3 -3
- 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/omp/agent-events.ts +11 -0
- package/src/shared/lib/chat/omp/session-modes-signal.test.ts +41 -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/mode-signal.ts +20 -0
- package/src/shared/lib/harness/provider-facts.test.ts +27 -0
- package/src/shared/lib/harness/providers.ts +35 -1
- package/src/shared/lib/harness/session-modes.ts +24 -0
- package/src/shared/lib/harness/update-job.ts +80 -0
- package/src/shared/lib/harness/update-plan.test.ts +21 -6
- package/src/shared/lib/harness/update-plan.ts +12 -4
- package/src/shared/lib/icons/tint-hues.generated.ts +34 -34
- package/src/shared/lib/notifications/outcomes.test.ts +96 -0
- package/src/shared/lib/notifications/outcomes.ts +64 -0
- package/src/shared/lib/notifications/settings.test.ts +49 -0
- package/src/shared/lib/notifications/settings.ts +34 -0
- package/src/shared/lib/omp/session/default-title.ts +5 -4
- package/src/shared/lib/omp/session/prewarm-misc.test.ts +3 -2
- package/src/shared/lib/omp/session/schema-migrations.ts +4 -0
- package/src/shared/lib/omp/session/stream-state.server.test.ts +19 -0
- package/src/shared/lib/omp/session/stream-state.server.ts +24 -4
- 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/settings/defaults.ts +3 -0
- 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/theme.test.ts +1 -1
- 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 +89 -0
- package/src/shared/types/omp/agent.ts +19 -2
- package/src/shared/types/realtime.ts +2 -0
- package/src/shared/types/settings/state.ts +10 -0
- package/src/shared/types/workspace.ts +8 -0
- package/src/test-support/fake-omp-fork.ts +16 -2
- package/src/test-support/harness-table.test-util.ts +31 -1
- package/dist/client/static/js/index-z3w7n85q.css +0 -1
- package/src/client/components/common/subagent-list/index.tsx +0 -165
- 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,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The harness update record on disk: read it, write it atomically, and tell a
|
|
8
|
+
* live run from a queued one and from a dead one.
|
|
9
|
+
*
|
|
10
|
+
* Kept apart from `update-job.server.ts` because the DETACHED WORKER writes the
|
|
11
|
+
* same record (`update-worker.ts`) and must not import the server's database and
|
|
12
|
+
* config modules to do it. One writer, one file shape, two processes.
|
|
13
|
+
*
|
|
14
|
+
* **A run outlives the request that started it.** The POST answers in
|
|
15
|
+
* milliseconds while the install keeps going, so the state cannot live in a
|
|
16
|
+
* closure or a `Map`: a reloaded page, a second tab and a `bun --hot` reload all
|
|
17
|
+
* have to read the same answer. It is one JSON file PER HARNESS, replaced
|
|
18
|
+
* atomically (`tmp` + `rename`), which is why a reader never sees half a record.
|
|
19
|
+
*
|
|
20
|
+
* The path is always passed in: the server derives it from the harness id
|
|
21
|
+
* (`getHarnessUpdateStatusPath`), and the worker uses the one its spec names.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import fs from 'node:fs';
|
|
25
|
+
import { isHarnessUpdateJob, jobQueued } from '@/shared/lib/harness/update-job';
|
|
26
|
+
import type { HarnessUpdateJob } from '@/shared/types/harness-update';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* How long a just-started job is trusted without a live pid.
|
|
30
|
+
*
|
|
31
|
+
* The server writes the record, then spawns the worker; for those first moments
|
|
32
|
+
* there is a `finishedAt: null` record and no worker pid yet. Reading that as "it
|
|
33
|
+
* died" would report a failure over a run that is about to start. Fifteen
|
|
34
|
+
* seconds is far longer than that handshake and far shorter than a human's
|
|
35
|
+
* patience for a stale row.
|
|
36
|
+
*/
|
|
37
|
+
export const UPDATE_START_GRACE_MS = 15_000;
|
|
38
|
+
|
|
39
|
+
/** The record at `path`, or null when none was ever written (or it is
|
|
40
|
+
* unreadable). `queuedAt` is normalized, because a record written before the
|
|
41
|
+
* queue existed has no such field. */
|
|
42
|
+
export async function readUpdateJob(path: string): Promise<HarnessUpdateJob | null> {
|
|
43
|
+
try {
|
|
44
|
+
const parsed = (await Bun.file(path).json()) as unknown;
|
|
45
|
+
if (!isHarnessUpdateJob(parsed)) return null;
|
|
46
|
+
return { ...parsed, queuedAt: typeof parsed.queuedAt === 'number' ? parsed.queuedAt : null };
|
|
47
|
+
} catch {
|
|
48
|
+
// Absent (never run) and corrupt (a half-written record from an old build)
|
|
49
|
+
// are the same answer: there is nothing to report.
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Replace the record atomically. A reader sees the previous one or this one. */
|
|
55
|
+
export async function writeUpdateJob(job: HarnessUpdateJob, path: string): Promise<void> {
|
|
56
|
+
const target = path;
|
|
57
|
+
const temp = `${target}.${process.pid}.tmp`;
|
|
58
|
+
await Bun.write(temp, JSON.stringify(job, null, 2));
|
|
59
|
+
await fs.promises.rename(temp, target);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Is this pid a live process? `EPERM` means it exists and is not ours. */
|
|
63
|
+
export function pidAlive(pid: number | null): boolean {
|
|
64
|
+
if (!pid || !Number.isInteger(pid) || pid <= 0) return false;
|
|
65
|
+
try {
|
|
66
|
+
process.kill(pid, 0);
|
|
67
|
+
return true;
|
|
68
|
+
} catch (cause) {
|
|
69
|
+
return (cause as NodeJS.ErrnoException).code === 'EPERM';
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Is this run still going?
|
|
75
|
+
*
|
|
76
|
+
* A known-live pid settles it. Without one the record is trusted only inside the
|
|
77
|
+
* start grace window; after that an absent worker means the run died without
|
|
78
|
+
* writing a result, and the caller reports that instead of polling forever.
|
|
79
|
+
* A QUEUED record is never in flight: it has no worker yet, by design.
|
|
80
|
+
*/
|
|
81
|
+
export function jobInFlight(job: HarnessUpdateJob | null, now = Date.now()): boolean {
|
|
82
|
+
if (!job || job.finishedAt !== null || jobQueued(job)) return false;
|
|
83
|
+
if (pidAlive(job.pid)) return true;
|
|
84
|
+
return now - job.startedAt < UPDATE_START_GRACE_MS;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The record with a run that died without reporting turned into a failure.
|
|
89
|
+
*
|
|
90
|
+
* `finishedAt` is what every reader keys on, so settling here is what stops the
|
|
91
|
+
* dialog from spinning forever; the message says the install itself may have
|
|
92
|
+
* completed, because a killed reporter is not proof the commands failed. A
|
|
93
|
+
* queued record is left alone — nothing was ever spawned for it.
|
|
94
|
+
*/
|
|
95
|
+
export async function settleDeadJob(
|
|
96
|
+
job: HarnessUpdateJob | null,
|
|
97
|
+
path: string,
|
|
98
|
+
): Promise<HarnessUpdateJob | null> {
|
|
99
|
+
// A record that already has a result is history, not a candidate: the worker
|
|
100
|
+
// wrote it, and rewriting it would turn a success into a "stopped without
|
|
101
|
+
// reporting" failure. A queued record is a candidate for the QUEUE, not for
|
|
102
|
+
// this.
|
|
103
|
+
if (!job || job.finishedAt !== null || jobQueued(job) || jobInFlight(job)) return job;
|
|
104
|
+
const settled: HarnessUpdateJob = {
|
|
105
|
+
...job,
|
|
106
|
+
finishedAt: Date.now(),
|
|
107
|
+
pid: null,
|
|
108
|
+
ok: false,
|
|
109
|
+
applied: true,
|
|
110
|
+
message: `${job.label} update stopped without reporting a result — check its status before trying again`,
|
|
111
|
+
steps: job.steps.map((step) => (step.state === 'running' ? { ...step, state: 'failed' } : step)),
|
|
112
|
+
};
|
|
113
|
+
await writeUpdateJob(settled, path);
|
|
114
|
+
return settled;
|
|
115
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Accepting a harness update: start it now, or queue it behind the install that
|
|
8
|
+
* is already running.
|
|
9
|
+
*
|
|
10
|
+
* The POST that asks for an update answers as soon as the record exists; the
|
|
11
|
+
* worker (`update-worker.ts`) owns the install and writes its progress to disk.
|
|
12
|
+
* This module is the server half of that handshake:
|
|
13
|
+
*
|
|
14
|
+
* - it refuses a SECOND press for the SAME harness, with the run that caused it;
|
|
15
|
+
* - it accepts a press for a DIFFERENT harness as a WAITING record, because two
|
|
16
|
+
* presses is now a queue rather than an error (`update-queue.server.ts` starts
|
|
17
|
+
* it when the one ahead of it settles);
|
|
18
|
+
* - and it never runs two workers at once, because both would install into the
|
|
19
|
+
* same global npm prefix.
|
|
20
|
+
*
|
|
21
|
+
* **The decision is a fact on disk, not a flag in memory.** The old route had no
|
|
22
|
+
* single-flight at all: a page reload mid-install offered the button again, and a
|
|
23
|
+
* second `npm install -g` over the same prefix is how an npm tree gets corrupted.
|
|
24
|
+
* The check reads the same records the worker writes, so a second tab and a
|
|
25
|
+
* second server instance see the same answer.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { getHarnessUpdateStatusPath } from '@/server/lib/lifecycle/paths';
|
|
29
|
+
import { readUpdateJob, settleDeadJob, writeUpdateJob } from '@/server/lib/harness/update-job-store';
|
|
30
|
+
import { pumpUpdateQueue, readAllUpdateJobs, serializeUpdateWork } from '@/server/lib/harness/update-queue.server';
|
|
31
|
+
import { spawnUpdateWorker } from '@/server/lib/harness/update-spawn.server';
|
|
32
|
+
import { jobRunning, jobUnfinished } from '@/shared/lib/harness/update-job';
|
|
33
|
+
import type { HarnessUpdateStep } from '@/shared/lib/harness/update-plan';
|
|
34
|
+
import type { HarnessId } from '@/shared/lib/harness/catalog';
|
|
35
|
+
import type { HarnessUpdateJob } from '@/shared/types/harness-update';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* This harness's unfinished record — installing, or waiting its turn — or null.
|
|
39
|
+
* Used to refuse a second press with a reason.
|
|
40
|
+
*/
|
|
41
|
+
export async function unfinishedUpdateJob(harness: HarnessId): Promise<HarnessUpdateJob | null> {
|
|
42
|
+
const path = getHarnessUpdateStatusPath(harness);
|
|
43
|
+
const job = await settleDeadJob(await readUpdateJob(path), path);
|
|
44
|
+
return jobUnfinished(job) ? job : null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Start `steps`, or write the record that waits for the install in front of it.
|
|
49
|
+
*
|
|
50
|
+
* Serialized with the queue's own passes: two presses arriving together are one
|
|
51
|
+
* decision, not two "nothing is running" reads that both spawn.
|
|
52
|
+
*/
|
|
53
|
+
export function startHarnessUpdateJob(
|
|
54
|
+
harness: HarnessId,
|
|
55
|
+
label: string,
|
|
56
|
+
steps: HarnessUpdateStep[],
|
|
57
|
+
): Promise<HarnessUpdateJob> {
|
|
58
|
+
return serializeUpdateWork(async () => {
|
|
59
|
+
const busy = (await readAllUpdateJobs()).some(jobRunning);
|
|
60
|
+
const job: HarnessUpdateJob = {
|
|
61
|
+
harness,
|
|
62
|
+
label,
|
|
63
|
+
startedAt: Date.now(),
|
|
64
|
+
finishedAt: null,
|
|
65
|
+
// Set only while this record waits; the spawn clears it.
|
|
66
|
+
queuedAt: busy ? Date.now() : null,
|
|
67
|
+
pid: null,
|
|
68
|
+
steps: steps.map((step) => ({ ...step, state: 'pending', code: null })),
|
|
69
|
+
output: '',
|
|
70
|
+
ok: null,
|
|
71
|
+
message: null,
|
|
72
|
+
applied: false,
|
|
73
|
+
repointed: null,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
if (busy) {
|
|
77
|
+
await writeUpdateJob(job, getHarnessUpdateStatusPath(harness));
|
|
78
|
+
// The ticker starts the queued run when the install ahead of it settles —
|
|
79
|
+
// including with the About card closed. It must not be awaited here.
|
|
80
|
+
void pumpUpdateQueue().catch((cause) => {
|
|
81
|
+
console.error('[harness-update] queue pump failed:', cause);
|
|
82
|
+
});
|
|
83
|
+
return job;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const started = await spawnUpdateWorker(job);
|
|
87
|
+
// Ticker for the FINALIZE half as well: the side effects a detached worker
|
|
88
|
+
// cannot apply happen without a browser polling this time.
|
|
89
|
+
void pumpUpdateQueue().catch((cause) => {
|
|
90
|
+
console.error('[harness-update] queue pump failed:', cause);
|
|
91
|
+
});
|
|
92
|
+
return started;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The queue behind "update two harnesses": read every record, apply what a
|
|
8
|
+
* finished run left behind, and start the next WAITING one.
|
|
9
|
+
*
|
|
10
|
+
* **Why a queue and not two installs at once.** The press used to be refused
|
|
11
|
+
* while any run was in flight, which made "update these two" impossible: the
|
|
12
|
+
* second press got a `409` and the user had to come back later. Two installs
|
|
13
|
+
* really cannot overlap — they share the global npm prefix, and two
|
|
14
|
+
* `npm install -g` over it is the way an npm tree is corrupted — so the second
|
|
15
|
+
* press is now ACCEPTED and its record carries `queuedAt`. This module starts it
|
|
16
|
+
* when the install ahead of it settles, and the row says it is waiting.
|
|
17
|
+
*
|
|
18
|
+
* **The queue is driven by a clock, not by the dialog.** A queued run must not
|
|
19
|
+
* depend on a browser tab staying open: the About card can be closed, the page
|
|
20
|
+
* reloaded, and the worker still has to start. The ticker reads the same records
|
|
21
|
+
* the worker writes, so it also settles a run that died without reporting, and
|
|
22
|
+
* applies the post-install side effects (`forgetProbe`, the launch-line repoint)
|
|
23
|
+
* that a detached process cannot — it does not own the database.
|
|
24
|
+
*
|
|
25
|
+
* **Every write goes through one chain.** Two presses, a queue tick and a status
|
|
26
|
+
* poll can each read-decide-write the records; serialized, the second press sees
|
|
27
|
+
* the first's record and waits instead of racing it into a second worker.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { probeWithConfig } from '@/server/lib/harness/auto-probe.server';
|
|
31
|
+
import { forgetProbe, rememberProbe } from '@/server/lib/harness/probe-cache.server';
|
|
32
|
+
import { forgetCliVersion } from '@/server/lib/harness/update-state.server';
|
|
33
|
+
import { loadHarnessConfig, saveHarnessConfig } from '@/server/lib/harness/config.server';
|
|
34
|
+
import { invalidateOmpCliCache } from '@/server/lib/omp/core/cli';
|
|
35
|
+
import { getHarnessUpdateStatusPath } from '@/server/lib/lifecycle/paths';
|
|
36
|
+
import { readUpdateJob, settleDeadJob, writeUpdateJob } from '@/server/lib/harness/update-job-store';
|
|
37
|
+
import { spawnUpdateWorker } from '@/server/lib/harness/update-spawn.server';
|
|
38
|
+
import { HARNESS_IDS, type HarnessId } from '@/shared/lib/harness/catalog';
|
|
39
|
+
import { harnessUpdateFacts, type HarnessUpdateFacts } from '@/shared/lib/harness/updates';
|
|
40
|
+
import { repointAdapterLaunch, type HarnessUpdateStep } from '@/shared/lib/harness/update-plan';
|
|
41
|
+
import { jobMoving, jobQueued, jobRunning } from '@/shared/lib/harness/update-job';
|
|
42
|
+
import type { HarnessProbe } from '@/server/lib/harness/capabilities';
|
|
43
|
+
import type { HarnessUpdateJob } from '@/shared/types/harness-update';
|
|
44
|
+
|
|
45
|
+
declare global {
|
|
46
|
+
// eslint-disable-next-line no-var
|
|
47
|
+
var __kabmoHarnessUpdateQueueTimer: ReturnType<typeof setInterval> | undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** How often a waiting run is looked at while anything is unfinished. 2 s is
|
|
51
|
+
* below the dialog's own poll, and the tick is a handful of small file reads. */
|
|
52
|
+
export const HARNESS_UPDATE_QUEUE_TICK_MS = 2_000;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Run `work` after every earlier read-decide-write over the update records.
|
|
56
|
+
*
|
|
57
|
+
* The records are files, so no lock is held between the read and the write; this
|
|
58
|
+
* chain is what makes "is anything running?" and "start this one" one decision
|
|
59
|
+
* inside a single process. Across processes the worker's own `flock` is still
|
|
60
|
+
* the guard (see `update-worker.ts`).
|
|
61
|
+
*/
|
|
62
|
+
let chain: Promise<unknown> = Promise.resolve();
|
|
63
|
+
|
|
64
|
+
export function serializeUpdateWork<T>(work: () => Promise<T>): Promise<T> {
|
|
65
|
+
const next = chain.then(work, work);
|
|
66
|
+
chain = next.then(() => undefined, () => undefined);
|
|
67
|
+
return next;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Every harness's record, with a run that died without reporting settled. */
|
|
71
|
+
export async function readAllUpdateJobs(): Promise<HarnessUpdateJob[]> {
|
|
72
|
+
const jobs = await Promise.all(HARNESS_IDS.map(async (id) => {
|
|
73
|
+
const path = getHarnessUpdateStatusPath(id);
|
|
74
|
+
return settleDeadJob(await readUpdateJob(path), path);
|
|
75
|
+
}));
|
|
76
|
+
return jobs.filter((job): job is HarnessUpdateJob => job !== null);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Make an adapter install the one the next spawn runs.
|
|
81
|
+
*
|
|
82
|
+
* The launch table starts an npm adapter through a PINNED `npx -y pkg@x.y.z`,
|
|
83
|
+
* while the update installs the package GLOBALLY. Those are two different
|
|
84
|
+
* resolvers: `npm i -g` writes a binary onto PATH that `npx` never consults, so
|
|
85
|
+
* the press reported success while every new session still started the old
|
|
86
|
+
* pinned build. A user's own configured command is never replaced.
|
|
87
|
+
*/
|
|
88
|
+
async function pointAtInstalledAdapter(
|
|
89
|
+
harness: HarnessId,
|
|
90
|
+
facts: HarnessUpdateFacts,
|
|
91
|
+
steps: HarnessUpdateStep[],
|
|
92
|
+
): Promise<string | null> {
|
|
93
|
+
const config = await loadHarnessConfig(harness);
|
|
94
|
+
const bin = repointAdapterLaunch(
|
|
95
|
+
facts,
|
|
96
|
+
steps,
|
|
97
|
+
config.command,
|
|
98
|
+
facts.agentBin ? Boolean(Bun.which(facts.agentBin)) : false,
|
|
99
|
+
);
|
|
100
|
+
if (!bin) return null;
|
|
101
|
+
await saveHarnessConfig(harness, { ...config, command: bin });
|
|
102
|
+
return bin;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* What a queue pass may run on the side, for the suite that cannot spawn an
|
|
107
|
+
* agent: the real probe by default, a stub in the tests (the seam
|
|
108
|
+
* `runAutoProbe` already offers for the boot pass).
|
|
109
|
+
*/
|
|
110
|
+
export interface HarnessQueueOptions {
|
|
111
|
+
probe?: (id: HarnessId, cwd: string) => Promise<HarnessProbe>;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The probes this process is running, keyed by run.
|
|
116
|
+
*
|
|
117
|
+
* A probe spawns an agent and is not awaited by the queue, so the ticker would
|
|
118
|
+
* otherwise start a second one on every pass (and a re-kicked record after a
|
|
119
|
+
* restart would stack on the first).
|
|
120
|
+
*/
|
|
121
|
+
const probing = new Set<string>();
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Probe the freshly installed agent and write the answer's arrival onto the
|
|
125
|
+
* record.
|
|
126
|
+
*
|
|
127
|
+
* **The update is not finished with the user until this answers.** `forgetProbe`
|
|
128
|
+
* only drops the stale number; without a new probe the row keeps quoting the
|
|
129
|
+
* version the install just replaced, and the reader has to open Settings and
|
|
130
|
+
* press Check status themselves. The probe is NOT awaited by the install's own
|
|
131
|
+
* report — it spawns an agent process that may take a minute — so the record
|
|
132
|
+
* carries `probe: 'pending'` and a polling dialog re-reads the rows when it
|
|
133
|
+
* turns `done`.
|
|
134
|
+
*/
|
|
135
|
+
async function probeAfterUpdate(
|
|
136
|
+
harness: HarnessId,
|
|
137
|
+
startedAt: number,
|
|
138
|
+
probe: (id: HarnessId, cwd: string) => Promise<HarnessProbe>,
|
|
139
|
+
): Promise<void> {
|
|
140
|
+
let version: string | null = null;
|
|
141
|
+
try {
|
|
142
|
+
const recorded = await probe(harness, process.cwd());
|
|
143
|
+
version = recorded.capabilities.agentVersion ?? null;
|
|
144
|
+
await rememberProbe(harness, recorded);
|
|
145
|
+
} catch (cause) {
|
|
146
|
+
// A probe that fails is a fact the row can live with (it reads "not probed"
|
|
147
|
+
// with the reason'); leaving the record `pending` forever is not.
|
|
148
|
+
console.error(`[harness-update] probe after updating ${harness} failed:`, cause);
|
|
149
|
+
}
|
|
150
|
+
const path = getHarnessUpdateStatusPath(harness);
|
|
151
|
+
const current = await readUpdateJob(path);
|
|
152
|
+
// A NEW run for the same harness may have replaced the record while this
|
|
153
|
+
// probe ran; writing here would revert that run to the previous one.
|
|
154
|
+
if (!current || current.startedAt !== startedAt) return;
|
|
155
|
+
// The version is stated only when the agent itself answered it: a claim about
|
|
156
|
+
// a number nobody read is the fabrication this whole record exists to avoid.
|
|
157
|
+
const message = version && current.message && !current.message.includes(version)
|
|
158
|
+
? `${current.message} — it now reports ${version}`
|
|
159
|
+
: current.message;
|
|
160
|
+
await writeUpdateJob({ ...current, probe: 'done', message }, path);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Run the probe for a settled record, once per run, in the background. */
|
|
164
|
+
function startProbe(job: HarnessUpdateJob, options: HarnessQueueOptions): void {
|
|
165
|
+
if (!HARNESS_IDS.includes(job.harness as HarnessId)) return;
|
|
166
|
+
const key = `${job.harness}:${job.startedAt}`;
|
|
167
|
+
if (probing.has(key)) return;
|
|
168
|
+
probing.add(key);
|
|
169
|
+
void probeAfterUpdate(job.harness as HarnessId, job.startedAt, options.probe ?? probeWithConfig)
|
|
170
|
+
.finally(() => probing.delete(key));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Apply a finished run's post-install side effects exactly once. */
|
|
174
|
+
async function applyFinishedJob(
|
|
175
|
+
job: HarnessUpdateJob,
|
|
176
|
+
options: HarnessQueueOptions,
|
|
177
|
+
): Promise<HarnessUpdateJob> {
|
|
178
|
+
// The record is a file, so its id is not proof of a catalog entry; an unknown
|
|
179
|
+
// one has no facts and nothing to repoint — it is still marked applied, so it
|
|
180
|
+
// is not re-applied on every tick.
|
|
181
|
+
const harness = HARNESS_IDS.includes(job.harness as HarnessId) ? job.harness as HarnessId : null;
|
|
182
|
+
const facts = harness ? harnessUpdateFacts(harness) : null;
|
|
183
|
+
let repointed: string | null = null;
|
|
184
|
+
|
|
185
|
+
if (job.ok && harness) {
|
|
186
|
+
// The cached probe describes the binary that WAS installed, and the CLI's
|
|
187
|
+
// `--version` read is cached for ten minutes: keeping either would report the
|
|
188
|
+
// pre-update number until its TTL ran out.
|
|
189
|
+
await forgetProbe(harness);
|
|
190
|
+
if (facts?.cli) forgetCliVersion(facts.cli.bin);
|
|
191
|
+
// `omp update` can replace its own launcher, so a cached path may be gone.
|
|
192
|
+
if (harness === 'omp-acp') invalidateOmpCliCache();
|
|
193
|
+
if (facts) repointed = await pointAtInstalledAdapter(harness, facts, job.steps);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// The probe starts only for a run that actually moved something: a failed
|
|
197
|
+
// install has no new binary to ask, and an `own` harness is never updated here.
|
|
198
|
+
const willProbe = Boolean(job.ok && harness);
|
|
199
|
+
const settled: HarnessUpdateJob = {
|
|
200
|
+
...job,
|
|
201
|
+
applied: true,
|
|
202
|
+
probe: willProbe ? 'pending' : job.probe ?? null,
|
|
203
|
+
repointed,
|
|
204
|
+
message: job.ok && job.message
|
|
205
|
+
? `${job.message}${repointed ? ` — launches now run \`${repointed}\`` : ''}`
|
|
206
|
+
: job.message,
|
|
207
|
+
};
|
|
208
|
+
await writeUpdateJob(settled, getHarnessUpdateStatusPath(job.harness));
|
|
209
|
+
if (willProbe) startProbe(settled, options);
|
|
210
|
+
return settled;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* The oldest WAITING record, or null.
|
|
215
|
+
*
|
|
216
|
+
* One install at a time, so the records form a queue and the OLDEST press is the
|
|
217
|
+
* next one — not the newest, which would let a later press jump ahead of the one
|
|
218
|
+
* the user made first.
|
|
219
|
+
*/
|
|
220
|
+
export function nextQueuedJob(jobs: HarnessUpdateJob[]): HarnessUpdateJob | null {
|
|
221
|
+
const waiting = jobs.filter(jobQueued);
|
|
222
|
+
if (waiting.length === 0) return null;
|
|
223
|
+
return waiting.sort((a, b) => (a.queuedAt ?? 0) - (b.queuedAt ?? 0))[0];
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Keep the ticker alive while anything is moving, and stop it when nothing is. */
|
|
227
|
+
function syncTicker(jobs: HarnessUpdateJob[]): void {
|
|
228
|
+
if (jobs.some(jobMoving)) {
|
|
229
|
+
globalThis.__kabmoHarnessUpdateQueueTimer ??= setInterval(() => {
|
|
230
|
+
void pumpUpdateQueue().catch((cause) => {
|
|
231
|
+
console.error('[harness-update] queue tick failed:', cause);
|
|
232
|
+
});
|
|
233
|
+
}, HARNESS_UPDATE_QUEUE_TICK_MS);
|
|
234
|
+
// A background queue must not be the reason the process will not exit.
|
|
235
|
+
globalThis.__kabmoHarnessUpdateQueueTimer.unref?.();
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (globalThis.__kabmoHarnessUpdateQueueTimer) {
|
|
239
|
+
clearInterval(globalThis.__kabmoHarnessUpdateQueueTimer);
|
|
240
|
+
globalThis.__kabmoHarnessUpdateQueueTimer = undefined;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* One pass: settle dead runs, apply finished ones, start the next waiting run.
|
|
246
|
+
*
|
|
247
|
+
* Returns the records as this pass left them, so the status route answers from
|
|
248
|
+
* the same read it acted on. Called by the status route, by the ticker while
|
|
249
|
+
* anything is unfinished, and once after the listener is up (a queued run
|
|
250
|
+
* accepted before a restart must not wait for a browser).
|
|
251
|
+
*/
|
|
252
|
+
export function pumpUpdateQueue(options: HarnessQueueOptions = {}): Promise<HarnessUpdateJob[]> {
|
|
253
|
+
return serializeUpdateWork(async () => {
|
|
254
|
+
let jobs = await readAllUpdateJobs();
|
|
255
|
+
|
|
256
|
+
const finished = await Promise.all(
|
|
257
|
+
jobs.map((job) => job.finishedAt !== null && !job.applied ? applyFinishedJob(job, options) : job),
|
|
258
|
+
);
|
|
259
|
+
jobs = finished;
|
|
260
|
+
|
|
261
|
+
// A probe whose server went away mid-flight leaves `pending` behind; the
|
|
262
|
+
// next pass (a poll, or the boot pump) runs it again rather than leaving the
|
|
263
|
+
// row on the old number forever.
|
|
264
|
+
for (const job of jobs) {
|
|
265
|
+
if (job.finishedAt !== null && job.probe === 'pending') startProbe(job, options);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const waiting = nextQueuedJob(jobs);
|
|
269
|
+
if (waiting && !jobs.some(jobRunning)) {
|
|
270
|
+
try {
|
|
271
|
+
const started = await spawnUpdateWorker(waiting);
|
|
272
|
+
jobs = jobs.map((job) => job.harness === started.harness ? started : job);
|
|
273
|
+
} catch (cause) {
|
|
274
|
+
// Starting it failed (the worker is missing, the spec is unwritable).
|
|
275
|
+
// The record says so instead of sitting in the queue forever.
|
|
276
|
+
const failed: HarnessUpdateJob = {
|
|
277
|
+
...waiting,
|
|
278
|
+
finishedAt: Date.now(),
|
|
279
|
+
pid: null,
|
|
280
|
+
ok: false,
|
|
281
|
+
applied: true,
|
|
282
|
+
message: `${waiting.label} update could not start: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
283
|
+
};
|
|
284
|
+
await writeUpdateJob(failed, getHarnessUpdateStatusPath(waiting.harness));
|
|
285
|
+
jobs = jobs.map((job) => job.harness === failed.harness ? failed : job);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
syncTicker(jobs);
|
|
290
|
+
return jobs;
|
|
291
|
+
});
|
|
292
|
+
}
|