@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,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The three tables that make a session part of a tree.
|
|
8
|
+
*
|
|
9
|
+
* A session has no canonical row: an omp chat is a JSONL file and an ACP chat
|
|
10
|
+
* is owned by the agent (`harness_sessions` records only who ran it). So the
|
|
11
|
+
* relationships live here, keyed by the id the sidebar and the URL use, and
|
|
12
|
+
* they survive a restart, a closed tab and a second Kabmo instance.
|
|
13
|
+
*
|
|
14
|
+
* `session_relations` carries three independent edges and no more:
|
|
15
|
+
*
|
|
16
|
+
* parent_session_id the sidebar tree and the outcome notification
|
|
17
|
+
* lifecycle_owner_session_id archive/delete ownership (may cross projects)
|
|
18
|
+
* source_session_id a fork's origin, with `origin_kind = 'fork'`
|
|
19
|
+
*
|
|
20
|
+
* `session_notices` is the coalescing buffer between a finishing child and the
|
|
21
|
+
* parent's queue; a row exists only inside the window and becomes one
|
|
22
|
+
* `queued_messages` row when it closes. It is deliberately NOT a row in the
|
|
23
|
+
* queue: the queue is the user's editable follow-up list and a system notice
|
|
24
|
+
* has no business being reordered or edited.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import type { DbClient } from '@/server/lib/db/client';
|
|
28
|
+
|
|
29
|
+
const SESSION_RELATIONS_SQL = `
|
|
30
|
+
CREATE TABLE IF NOT EXISTS session_relations (
|
|
31
|
+
session_id TEXT PRIMARY KEY,
|
|
32
|
+
parent_session_id TEXT,
|
|
33
|
+
lifecycle_owner_session_id TEXT,
|
|
34
|
+
source_session_id TEXT,
|
|
35
|
+
origin_kind TEXT,
|
|
36
|
+
visibility TEXT NOT NULL DEFAULT 'visible',
|
|
37
|
+
parent_notifications TEXT NOT NULL DEFAULT 'auto',
|
|
38
|
+
depth INTEGER NOT NULL DEFAULT 0,
|
|
39
|
+
created_at INTEGER NOT NULL
|
|
40
|
+
);
|
|
41
|
+
CREATE INDEX IF NOT EXISTS idx_session_relations_parent
|
|
42
|
+
ON session_relations (parent_session_id);
|
|
43
|
+
CREATE INDEX IF NOT EXISTS idx_session_relations_owner
|
|
44
|
+
ON session_relations (lifecycle_owner_session_id);
|
|
45
|
+
CREATE INDEX IF NOT EXISTS idx_session_relations_source
|
|
46
|
+
ON session_relations (source_session_id, origin_kind);
|
|
47
|
+
|
|
48
|
+
CREATE TABLE IF NOT EXISTS session_notices (
|
|
49
|
+
id TEXT PRIMARY KEY,
|
|
50
|
+
parent_session_id TEXT NOT NULL,
|
|
51
|
+
coalesce_key TEXT NOT NULL,
|
|
52
|
+
state TEXT NOT NULL,
|
|
53
|
+
send_at INTEGER NOT NULL,
|
|
54
|
+
created_at INTEGER NOT NULL,
|
|
55
|
+
updated_at INTEGER NOT NULL
|
|
56
|
+
);
|
|
57
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_session_notices_window
|
|
58
|
+
ON session_notices (parent_session_id, coalesce_key);
|
|
59
|
+
|
|
60
|
+
CREATE TABLE IF NOT EXISTS session_agent_tokens (
|
|
61
|
+
session_id TEXT PRIMARY KEY,
|
|
62
|
+
token_hash TEXT NOT NULL,
|
|
63
|
+
created_at INTEGER NOT NULL,
|
|
64
|
+
revoked_at INTEGER
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
-- The last assistant text an ACP turn closed. ACP history is agent-owned:
|
|
68
|
+
-- there is no transcript for /api/chat/:id to read after the process dies,
|
|
69
|
+
-- so the parent notice and 'kabmo session output' would otherwise lose the
|
|
70
|
+
-- answer. omp does not need this row (its JSONL is the record), but writing
|
|
71
|
+
-- both keeps the read path one rule.
|
|
72
|
+
CREATE TABLE IF NOT EXISTS session_answers (
|
|
73
|
+
session_id TEXT PRIMARY KEY,
|
|
74
|
+
answer TEXT NOT NULL,
|
|
75
|
+
updated_at INTEGER NOT NULL
|
|
76
|
+
);
|
|
77
|
+
`;
|
|
78
|
+
|
|
79
|
+
/** Idempotent: created on every database open, like every other feature table. */
|
|
80
|
+
export async function ensureSessionRelationsSchema(db: DbClient): Promise<void> {
|
|
81
|
+
await db.exec(SESSION_RELATIONS_SQL);
|
|
82
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
7
|
+
import { isolateDb, releaseDb } from '@/test-support/isolated-db';
|
|
8
|
+
import { getDb } from '@/server/db.server';
|
|
9
|
+
import { ensureSessionRelationsSchema } from '@/server/lib/sessions/schema.server';
|
|
10
|
+
import { createSessionRelation } from '@/server/lib/sessions/relations.server';
|
|
11
|
+
import { decorateSidebarSessions } from '@/server/lib/sessions/sidebar-relations.server';
|
|
12
|
+
import type { SessionItemData } from '@/shared/types';
|
|
13
|
+
|
|
14
|
+
function row(id: string): SessionItemData {
|
|
15
|
+
return { id, folder_id: 1, title: id };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
beforeEach(async () => {
|
|
19
|
+
await isolateDb();
|
|
20
|
+
await ensureSessionRelationsSchema(await getDb());
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(async () => {
|
|
24
|
+
await releaseDb();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('decorateSidebarSessions', () => {
|
|
28
|
+
test('adds the parent edge and direct child count', async () => {
|
|
29
|
+
await createSessionRelation({ sessionId: 'parent' });
|
|
30
|
+
await createSessionRelation({ sessionId: 'c1', parentSessionId: 'parent' });
|
|
31
|
+
await createSessionRelation({ sessionId: 'c2', parentSessionId: 'parent' });
|
|
32
|
+
await createSessionRelation({
|
|
33
|
+
sessionId: 'fork-1',
|
|
34
|
+
sourceSessionId: 'parent',
|
|
35
|
+
originKind: 'fork',
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const [parent, c1, c2, fork] = await decorateSidebarSessions([
|
|
39
|
+
row('parent'),
|
|
40
|
+
row('c1'),
|
|
41
|
+
row('c2'),
|
|
42
|
+
row('fork-1'),
|
|
43
|
+
]);
|
|
44
|
+
expect(parent).toMatchObject({ childSessionCount: 2 });
|
|
45
|
+
expect(parent?.parentSessionId).toBeUndefined();
|
|
46
|
+
expect(c1).toMatchObject({ parentSessionId: 'parent', childSessionCount: 0 });
|
|
47
|
+
expect(c2).toMatchObject({ parentSessionId: 'parent', childSessionCount: 0 });
|
|
48
|
+
expect(fork).toMatchObject({ childSessionCount: 0 });
|
|
49
|
+
expect(fork?.parentSessionId).toBeUndefined();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('a hidden session is not drawn, but a visible one with no relation is', async () => {
|
|
53
|
+
await createSessionRelation({
|
|
54
|
+
sessionId: 'worker',
|
|
55
|
+
parentSessionId: 'parent',
|
|
56
|
+
visibility: 'hidden',
|
|
57
|
+
});
|
|
58
|
+
const rows = await decorateSidebarSessions([row('worker'), row('plain')]);
|
|
59
|
+
expect(rows.map((entry) => entry.id)).toEqual(['plain']);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Sidebar rows, decorated with their tree fields.
|
|
8
|
+
*
|
|
9
|
+
* One relation read for the whole folder: `parentSessionId` lets the client
|
|
10
|
+
* build the tree, `childSessionCount` decides whether a row draws a chevron,
|
|
11
|
+
* and `visibility: hidden` keeps a background worker out of the list while it
|
|
12
|
+
* stays addressable by id (and still notifies its parent).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { SessionItemData } from '@/shared/types';
|
|
16
|
+
import { listSessionRelations } from '@/server/lib/sessions/relations.server';
|
|
17
|
+
|
|
18
|
+
export async function decorateSidebarSessions(
|
|
19
|
+
sessions: readonly SessionItemData[],
|
|
20
|
+
): Promise<SessionItemData[]> {
|
|
21
|
+
let relations: Awaited<ReturnType<typeof listSessionRelations>> = [];
|
|
22
|
+
try {
|
|
23
|
+
relations = await listSessionRelations();
|
|
24
|
+
} catch {
|
|
25
|
+
// A database opened before this feature's tables existed: no tree, and
|
|
26
|
+
// never an error page over the whole sidebar.
|
|
27
|
+
}
|
|
28
|
+
const relationBySession = new Map(relations.map((relation) => [relation.sessionId, relation]));
|
|
29
|
+
const childCountByParent = new Map<string, number>();
|
|
30
|
+
for (const relation of relations) {
|
|
31
|
+
if (!relation.parentSessionId) continue;
|
|
32
|
+
childCountByParent.set(
|
|
33
|
+
relation.parentSessionId,
|
|
34
|
+
(childCountByParent.get(relation.parentSessionId) ?? 0) + 1,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
return sessions
|
|
38
|
+
.filter((session) => relationBySession.get(String(session.id))?.visibility !== 'hidden')
|
|
39
|
+
.map((session) => {
|
|
40
|
+
const relation = relationBySession.get(String(session.id));
|
|
41
|
+
return {
|
|
42
|
+
...session,
|
|
43
|
+
...(relation?.parentSessionId ? { parentSessionId: relation.parentSessionId } : {}),
|
|
44
|
+
childSessionCount: childCountByParent.get(String(session.id)) ?? 0,
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Spawning a child session: the relation is written before the first prompt,
|
|
8
|
+
* the depth cap is checked before anything is spawned, and a failure after the
|
|
9
|
+
* spawn tears the child down instead of leaving a session nobody owns.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
13
|
+
import { mkdtempSync } from 'node:fs';
|
|
14
|
+
import { tmpdir } from 'node:os';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
import { isolateDb, releaseDb } from '@/test-support/isolated-db';
|
|
17
|
+
import { getDb } from '@/server/db.server';
|
|
18
|
+
import { ensureSessionRelationsSchema } from '@/server/lib/sessions/schema.server';
|
|
19
|
+
import { saveHarnessSession } from '@/server/lib/harness/session-store.server';
|
|
20
|
+
import { createSessionRelation, getSessionRelation } from '@/server/lib/sessions/relations.server';
|
|
21
|
+
import {
|
|
22
|
+
SessionSpawnError,
|
|
23
|
+
spawnChildSession,
|
|
24
|
+
type SpawnSessionDeps,
|
|
25
|
+
type SpawnedSession,
|
|
26
|
+
} from '@/server/lib/sessions/spawn.server';
|
|
27
|
+
|
|
28
|
+
let root = '';
|
|
29
|
+
|
|
30
|
+
beforeEach(async () => {
|
|
31
|
+
await isolateDb();
|
|
32
|
+
await ensureSessionRelationsSchema(await getDb());
|
|
33
|
+
root = mkdtempSync(join(tmpdir(), 'kabmo-spawn-'));
|
|
34
|
+
const db = await getDb();
|
|
35
|
+
await db.run(
|
|
36
|
+
'INSERT INTO workspace_folders (id, name, project_path) VALUES (?, ?, ?)',
|
|
37
|
+
[1, 'one', root],
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterEach(async () => {
|
|
42
|
+
await releaseDb();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
function fakeSession(sessionId: string, options: { failPrompt?: boolean } = {}): SpawnedSession & {
|
|
46
|
+
sent: Record<string, unknown>[];
|
|
47
|
+
destroyed: boolean;
|
|
48
|
+
} {
|
|
49
|
+
const sent: Record<string, unknown>[] = [];
|
|
50
|
+
const state = {
|
|
51
|
+
sessionId,
|
|
52
|
+
sent,
|
|
53
|
+
destroyed: false,
|
|
54
|
+
async send(command: Record<string, unknown>) {
|
|
55
|
+
if (options.failPrompt) throw new Error('provider refused');
|
|
56
|
+
sent.push(command);
|
|
57
|
+
return { agentInvoked: true };
|
|
58
|
+
},
|
|
59
|
+
async destroyAndWait() {
|
|
60
|
+
state.destroyed = true;
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
return state;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function depsFor(session: SpawnedSession, onStart?: (cwd: string) => void): SpawnSessionDeps {
|
|
67
|
+
return {
|
|
68
|
+
async startRpc(cwd) {
|
|
69
|
+
onStart?.(cwd);
|
|
70
|
+
return { session, realSessionId: session.sessionId };
|
|
71
|
+
},
|
|
72
|
+
async startAcp() {
|
|
73
|
+
throw new Error('not used');
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
describe('spawnChildSession', () => {
|
|
79
|
+
test('writes the relation before sending, and reports the depth', async () => {
|
|
80
|
+
await saveHarnessSession({
|
|
81
|
+
sessionId: 'parent',
|
|
82
|
+
harness: 'omp-rpc',
|
|
83
|
+
agentSessionId: '',
|
|
84
|
+
cwd: root,
|
|
85
|
+
});
|
|
86
|
+
const child = fakeSession('child');
|
|
87
|
+
const result = await spawnChildSession(
|
|
88
|
+
{ parentSessionId: 'parent', prompt: 'review this' },
|
|
89
|
+
depsFor(child),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
expect(result).toEqual({
|
|
93
|
+
sessionId: 'child',
|
|
94
|
+
harness: 'omp-rpc',
|
|
95
|
+
parentSessionId: 'parent',
|
|
96
|
+
depth: 1,
|
|
97
|
+
});
|
|
98
|
+
expect(child.sent).toHaveLength(1);
|
|
99
|
+
expect(child.sent[0]).toMatchObject({ type: 'prompt' });
|
|
100
|
+
expect(String(child.sent[0].message)).toStartWith('[kabmo dispatched worker]');
|
|
101
|
+
expect(String(child.sent[0].message)).toContain('your coordinator is session parent');
|
|
102
|
+
expect(String(child.sent[0].message)).toEndWith('TASK\nreview this');
|
|
103
|
+
expect(await getSessionRelation('child')).toMatchObject({
|
|
104
|
+
parentSessionId: 'parent',
|
|
105
|
+
lifecycleOwnerSessionId: 'parent',
|
|
106
|
+
depth: 1,
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test('refuses a fifth level before spawning anything', async () => {
|
|
111
|
+
await createSessionRelation({ sessionId: 'l1' });
|
|
112
|
+
await createSessionRelation({ sessionId: 'l2', parentSessionId: 'l1' });
|
|
113
|
+
await createSessionRelation({ sessionId: 'l3', parentSessionId: 'l2' });
|
|
114
|
+
await createSessionRelation({ sessionId: 'l4', parentSessionId: 'l3' });
|
|
115
|
+
await createSessionRelation({ sessionId: 'l5', parentSessionId: 'l4' });
|
|
116
|
+
await saveHarnessSession({
|
|
117
|
+
sessionId: 'l5',
|
|
118
|
+
harness: 'omp-rpc',
|
|
119
|
+
agentSessionId: '',
|
|
120
|
+
cwd: root,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
let started = false;
|
|
124
|
+
await expect(
|
|
125
|
+
spawnChildSession(
|
|
126
|
+
{ parentSessionId: 'l5', prompt: 'too deep' },
|
|
127
|
+
depsFor(fakeSession('child'), () => {
|
|
128
|
+
started = true;
|
|
129
|
+
}),
|
|
130
|
+
),
|
|
131
|
+
).rejects.toMatchObject({ code: 'spawn_failed' });
|
|
132
|
+
expect(started).toBe(false);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('refuses a child outside the parent workspace folder', async () => {
|
|
136
|
+
const other = mkdtempSync(join(tmpdir(), 'kabmo-other-'));
|
|
137
|
+
const db = await getDb();
|
|
138
|
+
await db.run('INSERT INTO workspace_folders (id, name, project_path) VALUES (?, ?, ?)', [
|
|
139
|
+
2,
|
|
140
|
+
'two',
|
|
141
|
+
other,
|
|
142
|
+
]);
|
|
143
|
+
await saveHarnessSession({
|
|
144
|
+
sessionId: 'parent',
|
|
145
|
+
harness: 'omp-rpc',
|
|
146
|
+
agentSessionId: '',
|
|
147
|
+
cwd: root,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
await expect(
|
|
151
|
+
spawnChildSession(
|
|
152
|
+
{ parentSessionId: 'parent', cwd: other, prompt: 'elsewhere' },
|
|
153
|
+
depsFor(fakeSession('child')),
|
|
154
|
+
),
|
|
155
|
+
).rejects.toMatchObject({ code: 'parent_folder_mismatch' });
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test('an ACP worker with no explicit model leaves the harness default alone', async () => {
|
|
159
|
+
await saveHarnessSession({
|
|
160
|
+
sessionId: 'parent',
|
|
161
|
+
harness: 'claude',
|
|
162
|
+
agentSessionId: 'agent-parent',
|
|
163
|
+
cwd: root,
|
|
164
|
+
});
|
|
165
|
+
let captured: { provider: string; modelId: string } | null | undefined;
|
|
166
|
+
const child = fakeSession('child');
|
|
167
|
+
const deps: SpawnSessionDeps = {
|
|
168
|
+
async startRpc() {
|
|
169
|
+
throw new Error('not used');
|
|
170
|
+
},
|
|
171
|
+
async startAcp(request) {
|
|
172
|
+
captured = request.model;
|
|
173
|
+
return { session: child, realSessionId: child.sessionId };
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
await spawnChildSession({ parentSessionId: 'parent', prompt: 'review this' }, deps);
|
|
178
|
+
expect(captured).toBeNull();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test('a refused first prompt destroys the child and records nothing', async () => {
|
|
182
|
+
await saveHarnessSession({
|
|
183
|
+
sessionId: 'parent',
|
|
184
|
+
harness: 'omp-rpc',
|
|
185
|
+
agentSessionId: '',
|
|
186
|
+
cwd: root,
|
|
187
|
+
});
|
|
188
|
+
const child = fakeSession('child', { failPrompt: true });
|
|
189
|
+
|
|
190
|
+
await expect(
|
|
191
|
+
spawnChildSession({ parentSessionId: 'parent', prompt: 'hello' }, depsFor(child)),
|
|
192
|
+
).rejects.toBeInstanceOf(SessionSpawnError);
|
|
193
|
+
expect(child.destroyed).toBe(true);
|
|
194
|
+
expect(await getSessionRelation('child')).toMatchObject({ parentSessionId: 'parent' });
|
|
195
|
+
});
|
|
196
|
+
});
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Spawn a child session — the delegation half of the tree.
|
|
8
|
+
*
|
|
9
|
+
* The child is a normal session: same spawn paths, same transport, same
|
|
10
|
+
* permissions. What makes it a child is one relation row plus the notification
|
|
11
|
+
* edge; nothing about the agent process knows it was delegated. That is
|
|
12
|
+
* deliberate: a child that outlives its parent is an ordinary session, and a
|
|
13
|
+
* parent that dies must not take the child's transcript with it.
|
|
14
|
+
*
|
|
15
|
+
* Validation happens BEFORE the spawn, and the relation is written before the
|
|
16
|
+
* first prompt: a fast provider can finish a turn in under a second, and a
|
|
17
|
+
* completion with no relation row would be a notification nobody receives.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { loadPersistedAccessMode } from '@/shared/lib/omp/config/access-mode.server';
|
|
21
|
+
import {
|
|
22
|
+
agentEnvironment,
|
|
23
|
+
createAgentToken,
|
|
24
|
+
rememberAgentToken,
|
|
25
|
+
} from '@/server/lib/sessions/agent-token.server';
|
|
26
|
+
import type { ApprovalMode } from '@/shared/lib/omp/config/access-mode';
|
|
27
|
+
import { projectModelForCwd } from '@/server/lib/workspace/project-model.server';
|
|
28
|
+
import { startAcpSession } from '@/server/lib/harness/spawn.server';
|
|
29
|
+
import { getLiveSession, harnessOfSession, startNewRpcSession } from '@/server/lib/omp/rpc/session-registry';
|
|
30
|
+
import { harnessProbeModels } from '@/server/lib/harness/probe-cache.server';
|
|
31
|
+
import { getDb } from '@/server/db.server';
|
|
32
|
+
import { HARNESS_IDS, harnessDescriptor, type HarnessId } from '@/shared/lib/harness/catalog';
|
|
33
|
+
import type { SelectedModel } from '@/shared/types/models';
|
|
34
|
+
import { MAX_SESSION_DEPTH } from '@/server/lib/sessions/relation-types';
|
|
35
|
+
import {
|
|
36
|
+
createSessionRelation,
|
|
37
|
+
getSessionRelation,
|
|
38
|
+
} from '@/server/lib/sessions/relations.server';
|
|
39
|
+
import {
|
|
40
|
+
folderIdForCwd,
|
|
41
|
+
isSessionArchived,
|
|
42
|
+
resolveSessionRef,
|
|
43
|
+
sessionExists,
|
|
44
|
+
} from '@/server/lib/sessions/resolve.server';
|
|
45
|
+
|
|
46
|
+
export type SessionSpawnErrorCode =
|
|
47
|
+
| 'cwd_required'
|
|
48
|
+
| 'harness_unknown'
|
|
49
|
+
| 'parent_not_found'
|
|
50
|
+
| 'parent_archived'
|
|
51
|
+
| 'parent_folder_mismatch'
|
|
52
|
+
| 'model_unknown'
|
|
53
|
+
| 'spawn_failed';
|
|
54
|
+
|
|
55
|
+
export class SessionSpawnError extends Error {
|
|
56
|
+
constructor(
|
|
57
|
+
readonly code: SessionSpawnErrorCode,
|
|
58
|
+
message: string,
|
|
59
|
+
) {
|
|
60
|
+
super(message);
|
|
61
|
+
this.name = 'SessionSpawnError';
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The slice of a spawned session this service uses. */
|
|
66
|
+
export interface SpawnedSession {
|
|
67
|
+
readonly sessionId: string;
|
|
68
|
+
send(command: Record<string, unknown>): Promise<unknown>;
|
|
69
|
+
destroyAndWait(): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface SpawnSessionDeps {
|
|
73
|
+
startRpc(
|
|
74
|
+
cwd: string,
|
|
75
|
+
accessMode: ApprovalMode,
|
|
76
|
+
env: Record<string, string>,
|
|
77
|
+
): Promise<{ session: SpawnedSession; realSessionId: string }>;
|
|
78
|
+
startAcp(request: {
|
|
79
|
+
harness: HarnessId;
|
|
80
|
+
sessionId: string;
|
|
81
|
+
cwd: string;
|
|
82
|
+
model: SelectedModel | null;
|
|
83
|
+
thinkingLevel: string | null;
|
|
84
|
+
accessMode: ApprovalMode;
|
|
85
|
+
env: Record<string, string>;
|
|
86
|
+
}): Promise<{ session: SpawnedSession; realSessionId: string }>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const defaultDeps: SpawnSessionDeps = {
|
|
90
|
+
startRpc: (cwd, accessMode, env) => startNewRpcSession(cwd, accessMode, env),
|
|
91
|
+
startAcp: (request) => startAcpSession(request),
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export interface SpawnChildSessionInput {
|
|
95
|
+
/** Omit for a root session created through this path. */
|
|
96
|
+
parentSessionId?: string | null;
|
|
97
|
+
harness?: string | null;
|
|
98
|
+
cwd?: string | null;
|
|
99
|
+
prompt: string;
|
|
100
|
+
model?: SelectedModel | null;
|
|
101
|
+
thinkingLevel?: string | null;
|
|
102
|
+
lifecycleOwnerSessionId?: string | null;
|
|
103
|
+
visibility?: 'visible' | 'hidden';
|
|
104
|
+
parentNotifications?: 'auto' | 'stuck' | 'off';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface SpawnChildSessionResult {
|
|
108
|
+
sessionId: string;
|
|
109
|
+
harness: HarnessId;
|
|
110
|
+
parentSessionId: string | null;
|
|
111
|
+
depth: number;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function harnessOf(value: string | null | undefined): HarnessId | null {
|
|
115
|
+
if (!value) return null;
|
|
116
|
+
return HARNESS_IDS.includes(value as HarnessId) ? (value as HarnessId) : null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Resolve the parent's execution context and the child's cwd.
|
|
121
|
+
*
|
|
122
|
+
* The folder check is refused only when BOTH folders resolve: a session whose
|
|
123
|
+
* cwd sits outside every registered workspace has no folder to compare, and
|
|
124
|
+
* refusing on that would block work the sidebar would happily show.
|
|
125
|
+
*/
|
|
126
|
+
async function resolveParent(
|
|
127
|
+
parentSessionId: string,
|
|
128
|
+
requestedCwd: string | null,
|
|
129
|
+
): Promise<{ cwd: string; harness: HarnessId | null }> {
|
|
130
|
+
if (!(await sessionExists(parentSessionId))) {
|
|
131
|
+
throw new SessionSpawnError('parent_not_found', `Parent session ${parentSessionId} not found`);
|
|
132
|
+
}
|
|
133
|
+
if (await isSessionArchived(parentSessionId)) {
|
|
134
|
+
throw new SessionSpawnError('parent_archived', 'Cannot spawn under an archived session');
|
|
135
|
+
}
|
|
136
|
+
const parent = await resolveSessionRef(parentSessionId);
|
|
137
|
+
const cwd = requestedCwd || parent.cwd;
|
|
138
|
+
if (!cwd) {
|
|
139
|
+
throw new SessionSpawnError(
|
|
140
|
+
'cwd_required',
|
|
141
|
+
'The parent has no working directory; pass one explicitly',
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
if (requestedCwd && parent.cwd) {
|
|
145
|
+
const [parentFolder, childFolder] = await Promise.all([
|
|
146
|
+
folderIdForCwd(parent.cwd),
|
|
147
|
+
folderIdForCwd(requestedCwd),
|
|
148
|
+
]);
|
|
149
|
+
if (parentFolder !== null && childFolder !== null && parentFolder !== childFolder) {
|
|
150
|
+
throw new SessionSpawnError(
|
|
151
|
+
'parent_folder_mismatch',
|
|
152
|
+
'A child session must stay in its parent workspace folder',
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return { cwd, harness: parent.harness };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The model a worker runs on, by the one order that cannot invent a value.
|
|
161
|
+
*
|
|
162
|
+
* Explicit wins. Next is the SAME harness's live parent — a Claude reviewer
|
|
163
|
+
* inherits Claude's model, never omp's model id. Then the harness's own
|
|
164
|
+
* configured default is resolved by `startAcpSession`, so ACP gets null here.
|
|
165
|
+
* `omp-rpc` keeps its project default because that is a real omp model id.
|
|
166
|
+
*/
|
|
167
|
+
async function resolveSpawnModel(
|
|
168
|
+
requested: SelectedModel | null,
|
|
169
|
+
harness: HarnessId,
|
|
170
|
+
parentSessionId: string | null,
|
|
171
|
+
cwd: string,
|
|
172
|
+
): Promise<SelectedModel | null> {
|
|
173
|
+
if (requested) {
|
|
174
|
+
if (harness !== 'omp-rpc') {
|
|
175
|
+
const models = await harnessProbeModels(harness);
|
|
176
|
+
const exact = `${requested.provider}/${requested.modelId}`;
|
|
177
|
+
const listed = models.length === 0
|
|
178
|
+
|| models.some((model) => model.value === exact || model.value === requested.modelId);
|
|
179
|
+
if (!listed) {
|
|
180
|
+
throw new SessionSpawnError(
|
|
181
|
+
'model_unknown',
|
|
182
|
+
`${harnessDescriptor(harness)?.label ?? harness} does not list model ${exact}`,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return requested;
|
|
187
|
+
}
|
|
188
|
+
const parent = parentSessionId ? getLiveSession(parentSessionId) : undefined;
|
|
189
|
+
if (parent && harnessOfSession(parent) === harness && parent.runModel) {
|
|
190
|
+
return { provider: parent.runModel.provider, modelId: parent.runModel.modelId };
|
|
191
|
+
}
|
|
192
|
+
if (harness === 'omp-rpc') {
|
|
193
|
+
return projectModelForCwd(await getDb(), cwd).catch(() => null);
|
|
194
|
+
}
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** What a child receives before its task: identity, the one control verb, and
|
|
199
|
+
* the completion rule. Kabmo, not the orchestrator, writes this. */
|
|
200
|
+
function dispatchedWorkerPrompt(parentSessionId: string, sessionId: string, task: string): string {
|
|
201
|
+
return [
|
|
202
|
+
'[kabmo dispatched worker]',
|
|
203
|
+
`You are session ${sessionId}; your coordinator is session ${parentSessionId}.`,
|
|
204
|
+
'The coordinator cannot see this chat.',
|
|
205
|
+
'',
|
|
206
|
+
' # Read another session:',
|
|
207
|
+
' kabmo session output <id>',
|
|
208
|
+
' # Ask the coordinator or report a blocker, then end your turn:',
|
|
209
|
+
` kabmo session tell ${parentSessionId} --message-file - --mode auto`,
|
|
210
|
+
'',
|
|
211
|
+
'Do only this task. When you finish, your final message is your report: what you did,',
|
|
212
|
+
'what you found, what is left. Do not send a separate acknowledgement.',
|
|
213
|
+
'[/kabmo dispatched worker]',
|
|
214
|
+
'',
|
|
215
|
+
'TASK',
|
|
216
|
+
task,
|
|
217
|
+
].join('\n');
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Spawn a session, record its relation, then dispatch the first prompt. */
|
|
221
|
+
export async function spawnChildSession(
|
|
222
|
+
input: SpawnChildSessionInput,
|
|
223
|
+
deps: SpawnSessionDeps = defaultDeps,
|
|
224
|
+
): Promise<SpawnChildSessionResult> {
|
|
225
|
+
const prompt = input.prompt.trim();
|
|
226
|
+
if (!prompt) {
|
|
227
|
+
throw new SessionSpawnError('spawn_failed', 'A first prompt is required');
|
|
228
|
+
}
|
|
229
|
+
const parentSessionId = input.parentSessionId ?? null;
|
|
230
|
+
const requestedHarness = input.harness ?? null;
|
|
231
|
+
const parent = parentSessionId
|
|
232
|
+
? await resolveParent(parentSessionId, input.cwd ?? null)
|
|
233
|
+
: null;
|
|
234
|
+
const cwd = parent?.cwd ?? input.cwd ?? null;
|
|
235
|
+
if (!cwd) {
|
|
236
|
+
throw new SessionSpawnError('cwd_required', 'A working directory is required');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const requested = harnessOf(requestedHarness);
|
|
240
|
+
if (requestedHarness && requested === null) {
|
|
241
|
+
throw new SessionSpawnError('harness_unknown', `Unknown harness: ${requestedHarness}`);
|
|
242
|
+
}
|
|
243
|
+
const harness = requested ?? parent?.harness ?? 'omp-rpc';
|
|
244
|
+
const parentRelation = parentSessionId ? await getSessionRelation(parentSessionId) : null;
|
|
245
|
+
const depth = (parentRelation?.depth ?? 0) + 1;
|
|
246
|
+
if (depth > MAX_SESSION_DEPTH) {
|
|
247
|
+
throw new SessionSpawnError(
|
|
248
|
+
'spawn_failed',
|
|
249
|
+
`A session tree is limited to ${MAX_SESSION_DEPTH} levels`,
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const accessMode = await loadPersistedAccessMode();
|
|
254
|
+
const token = createAgentToken();
|
|
255
|
+
const env = agentEnvironment(token);
|
|
256
|
+
const model = await resolveSpawnModel(input.model ?? null, harness, parentSessionId, cwd);
|
|
257
|
+
const started =
|
|
258
|
+
harness === 'omp-rpc'
|
|
259
|
+
? await deps.startRpc(cwd, accessMode, env)
|
|
260
|
+
: await deps.startAcp({
|
|
261
|
+
harness,
|
|
262
|
+
sessionId: `__new__${Bun.randomUUIDv7()}`,
|
|
263
|
+
cwd,
|
|
264
|
+
model,
|
|
265
|
+
thinkingLevel: input.thinkingLevel ?? null,
|
|
266
|
+
accessMode,
|
|
267
|
+
env,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
try {
|
|
271
|
+
await rememberAgentToken(started.realSessionId, token);
|
|
272
|
+
await createSessionRelation({
|
|
273
|
+
sessionId: started.realSessionId,
|
|
274
|
+
parentSessionId,
|
|
275
|
+
lifecycleOwnerSessionId: input.lifecycleOwnerSessionId ?? parentSessionId,
|
|
276
|
+
visibility: input.visibility ?? 'visible',
|
|
277
|
+
parentNotifications: input.parentNotifications ?? 'auto',
|
|
278
|
+
});
|
|
279
|
+
} catch (error) {
|
|
280
|
+
// The child exists but cannot be placed in the tree; leaving it running
|
|
281
|
+
// would be a session nobody owns.
|
|
282
|
+
await started.session.destroyAndWait().catch(() => {});
|
|
283
|
+
throw new SessionSpawnError(
|
|
284
|
+
'spawn_failed',
|
|
285
|
+
error instanceof Error ? error.message : 'Could not record the session relation',
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
try {
|
|
290
|
+
const message = parentSessionId
|
|
291
|
+
? dispatchedWorkerPrompt(parentSessionId, started.realSessionId, prompt)
|
|
292
|
+
: prompt;
|
|
293
|
+
await started.session.send({ type: 'prompt', message });
|
|
294
|
+
} catch (error) {
|
|
295
|
+
await started.session.destroyAndWait().catch(() => {});
|
|
296
|
+
throw new SessionSpawnError(
|
|
297
|
+
'spawn_failed',
|
|
298
|
+
`${harnessDescriptor(harness)?.label ?? harness} refused the first prompt: ${
|
|
299
|
+
error instanceof Error ? error.message : String(error)
|
|
300
|
+
}`,
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return {
|
|
305
|
+
sessionId: started.realSessionId,
|
|
306
|
+
harness,
|
|
307
|
+
parentSessionId,
|
|
308
|
+
depth,
|
|
309
|
+
};
|
|
310
|
+
}
|