@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,237 @@
|
|
|
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
|
+
model: null,
|
|
98
|
+
});
|
|
99
|
+
expect(child.sent).toHaveLength(1);
|
|
100
|
+
expect(child.sent[0]).toMatchObject({ type: 'prompt' });
|
|
101
|
+
expect(String(child.sent[0].message)).toStartWith('[kabmo dispatched worker]');
|
|
102
|
+
expect(String(child.sent[0].message)).toContain('your coordinator is session parent');
|
|
103
|
+
expect(String(child.sent[0].message)).toEndWith('TASK\nreview this');
|
|
104
|
+
expect(await getSessionRelation('child')).toMatchObject({
|
|
105
|
+
parentSessionId: 'parent',
|
|
106
|
+
lifecycleOwnerSessionId: 'parent',
|
|
107
|
+
depth: 1,
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('refuses a fifth level before spawning anything', async () => {
|
|
112
|
+
await createSessionRelation({ sessionId: 'l1' });
|
|
113
|
+
await createSessionRelation({ sessionId: 'l2', parentSessionId: 'l1' });
|
|
114
|
+
await createSessionRelation({ sessionId: 'l3', parentSessionId: 'l2' });
|
|
115
|
+
await createSessionRelation({ sessionId: 'l4', parentSessionId: 'l3' });
|
|
116
|
+
await createSessionRelation({ sessionId: 'l5', parentSessionId: 'l4' });
|
|
117
|
+
await saveHarnessSession({
|
|
118
|
+
sessionId: 'l5',
|
|
119
|
+
harness: 'omp-rpc',
|
|
120
|
+
agentSessionId: '',
|
|
121
|
+
cwd: root,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
let started = false;
|
|
125
|
+
await expect(
|
|
126
|
+
spawnChildSession(
|
|
127
|
+
{ parentSessionId: 'l5', prompt: 'too deep' },
|
|
128
|
+
depsFor(fakeSession('child'), () => {
|
|
129
|
+
started = true;
|
|
130
|
+
}),
|
|
131
|
+
),
|
|
132
|
+
).rejects.toMatchObject({ code: 'spawn_failed' });
|
|
133
|
+
expect(started).toBe(false);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('refuses a child outside the parent workspace folder', async () => {
|
|
137
|
+
const other = mkdtempSync(join(tmpdir(), 'kabmo-other-'));
|
|
138
|
+
const db = await getDb();
|
|
139
|
+
await db.run('INSERT INTO workspace_folders (id, name, project_path) VALUES (?, ?, ?)', [
|
|
140
|
+
2,
|
|
141
|
+
'two',
|
|
142
|
+
other,
|
|
143
|
+
]);
|
|
144
|
+
await saveHarnessSession({
|
|
145
|
+
sessionId: 'parent',
|
|
146
|
+
harness: 'omp-rpc',
|
|
147
|
+
agentSessionId: '',
|
|
148
|
+
cwd: root,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
await expect(
|
|
152
|
+
spawnChildSession(
|
|
153
|
+
{ parentSessionId: 'parent', cwd: other, prompt: 'elsewhere' },
|
|
154
|
+
depsFor(fakeSession('child')),
|
|
155
|
+
),
|
|
156
|
+
).rejects.toMatchObject({ code: 'parent_folder_mismatch' });
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test('applies the composer model, thinking level and access mode to an RPC child', async () => {
|
|
160
|
+
await saveHarnessSession({
|
|
161
|
+
sessionId: 'parent',
|
|
162
|
+
harness: 'omp-rpc',
|
|
163
|
+
agentSessionId: '',
|
|
164
|
+
cwd: root,
|
|
165
|
+
});
|
|
166
|
+
const child = fakeSession('child');
|
|
167
|
+
let mode: string | undefined;
|
|
168
|
+
const deps: SpawnSessionDeps = {
|
|
169
|
+
async startRpc(_cwd, accessMode) {
|
|
170
|
+
mode = accessMode;
|
|
171
|
+
return { session: child, realSessionId: child.sessionId };
|
|
172
|
+
},
|
|
173
|
+
async startAcp() {
|
|
174
|
+
throw new Error('not used');
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
await spawnChildSession(
|
|
179
|
+
{
|
|
180
|
+
parentSessionId: 'parent',
|
|
181
|
+
prompt: 'review this',
|
|
182
|
+
model: { provider: 'acme', modelId: 'big' },
|
|
183
|
+
thinkingLevel: 'high',
|
|
184
|
+
accessMode: 'yolo',
|
|
185
|
+
images: [{ type: 'image', data: 'aGk=', mimeType: 'image/png' }],
|
|
186
|
+
},
|
|
187
|
+
deps,
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
expect(mode).toBe('yolo');
|
|
191
|
+
expect(child.sent[0]).toEqual({ type: 'set_model', provider: 'acme', modelId: 'big' });
|
|
192
|
+
expect(child.sent[1]).toEqual({ type: 'set_thinking_level', level: 'high' });
|
|
193
|
+
expect(child.sent[2]).toMatchObject({
|
|
194
|
+
type: 'prompt',
|
|
195
|
+
images: [{ type: 'image', data: 'aGk=', mimeType: 'image/png' }],
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test('an ACP worker with no explicit model leaves the harness default alone', async () => {
|
|
200
|
+
await saveHarnessSession({
|
|
201
|
+
sessionId: 'parent',
|
|
202
|
+
harness: 'claude',
|
|
203
|
+
agentSessionId: 'agent-parent',
|
|
204
|
+
cwd: root,
|
|
205
|
+
});
|
|
206
|
+
let captured: { provider: string; modelId: string } | null | undefined;
|
|
207
|
+
const child = fakeSession('child');
|
|
208
|
+
const deps: SpawnSessionDeps = {
|
|
209
|
+
async startRpc() {
|
|
210
|
+
throw new Error('not used');
|
|
211
|
+
},
|
|
212
|
+
async startAcp(request) {
|
|
213
|
+
captured = request.model;
|
|
214
|
+
return { session: child, realSessionId: child.sessionId };
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
await spawnChildSession({ parentSessionId: 'parent', prompt: 'review this' }, deps);
|
|
219
|
+
expect(captured).toBeNull();
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test('a refused first prompt destroys the child and records nothing', async () => {
|
|
223
|
+
await saveHarnessSession({
|
|
224
|
+
sessionId: 'parent',
|
|
225
|
+
harness: 'omp-rpc',
|
|
226
|
+
agentSessionId: '',
|
|
227
|
+
cwd: root,
|
|
228
|
+
});
|
|
229
|
+
const child = fakeSession('child', { failPrompt: true });
|
|
230
|
+
|
|
231
|
+
await expect(
|
|
232
|
+
spawnChildSession({ parentSessionId: 'parent', prompt: 'hello' }, depsFor(child)),
|
|
233
|
+
).rejects.toBeInstanceOf(SessionSpawnError);
|
|
234
|
+
expect(child.destroyed).toBe(true);
|
|
235
|
+
expect(await getSessionRelation('child')).toMatchObject({ parentSessionId: 'parent' });
|
|
236
|
+
});
|
|
237
|
+
});
|
|
@@ -0,0 +1,335 @@
|
|
|
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 type { AgentImage } from '@/shared/types/omp/agent';
|
|
35
|
+
import { MAX_SESSION_DEPTH } from '@/server/lib/sessions/relation-types';
|
|
36
|
+
import {
|
|
37
|
+
createSessionRelation,
|
|
38
|
+
getSessionRelation,
|
|
39
|
+
} from '@/server/lib/sessions/relations.server';
|
|
40
|
+
import {
|
|
41
|
+
folderIdForCwd,
|
|
42
|
+
isSessionArchived,
|
|
43
|
+
resolveSessionRef,
|
|
44
|
+
sessionExists,
|
|
45
|
+
} from '@/server/lib/sessions/resolve.server';
|
|
46
|
+
|
|
47
|
+
export type SessionSpawnErrorCode =
|
|
48
|
+
| 'cwd_required'
|
|
49
|
+
| 'harness_unknown'
|
|
50
|
+
| 'parent_not_found'
|
|
51
|
+
| 'parent_archived'
|
|
52
|
+
| 'parent_folder_mismatch'
|
|
53
|
+
| 'model_unknown'
|
|
54
|
+
| 'spawn_failed';
|
|
55
|
+
|
|
56
|
+
export class SessionSpawnError extends Error {
|
|
57
|
+
constructor(
|
|
58
|
+
readonly code: SessionSpawnErrorCode,
|
|
59
|
+
message: string,
|
|
60
|
+
) {
|
|
61
|
+
super(message);
|
|
62
|
+
this.name = 'SessionSpawnError';
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The slice of a spawned session this service uses. */
|
|
67
|
+
export interface SpawnedSession {
|
|
68
|
+
readonly sessionId: string;
|
|
69
|
+
send(command: Record<string, unknown>): Promise<unknown>;
|
|
70
|
+
destroyAndWait(): Promise<void>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface SpawnSessionDeps {
|
|
74
|
+
startRpc(
|
|
75
|
+
cwd: string,
|
|
76
|
+
accessMode: ApprovalMode,
|
|
77
|
+
env: Record<string, string>,
|
|
78
|
+
): Promise<{ session: SpawnedSession; realSessionId: string }>;
|
|
79
|
+
startAcp(request: {
|
|
80
|
+
harness: HarnessId;
|
|
81
|
+
sessionId: string;
|
|
82
|
+
cwd: string;
|
|
83
|
+
model: SelectedModel | null;
|
|
84
|
+
thinkingLevel: string | null;
|
|
85
|
+
accessMode: ApprovalMode;
|
|
86
|
+
env: Record<string, string>;
|
|
87
|
+
}): Promise<{ session: SpawnedSession; realSessionId: string }>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const defaultDeps: SpawnSessionDeps = {
|
|
91
|
+
startRpc: (cwd, accessMode, env) => startNewRpcSession(cwd, accessMode, env),
|
|
92
|
+
startAcp: (request) => startAcpSession(request),
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export interface SpawnChildSessionInput {
|
|
96
|
+
/** Omit for a root session created through this path. */
|
|
97
|
+
parentSessionId?: string | null;
|
|
98
|
+
harness?: string | null;
|
|
99
|
+
cwd?: string | null;
|
|
100
|
+
prompt: string;
|
|
101
|
+
/** Images the composer attached to the first prompt. */
|
|
102
|
+
images?: AgentImage[] | null;
|
|
103
|
+
model?: SelectedModel | null;
|
|
104
|
+
thinkingLevel?: string | null;
|
|
105
|
+
/** The composer's approval pick. Omitted by the CLI, which keeps the
|
|
106
|
+
* persisted setting. */
|
|
107
|
+
accessMode?: ApprovalMode | null;
|
|
108
|
+
lifecycleOwnerSessionId?: string | null;
|
|
109
|
+
visibility?: 'visible' | 'hidden';
|
|
110
|
+
parentNotifications?: 'auto' | 'stuck' | 'off';
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface SpawnChildSessionResult {
|
|
114
|
+
sessionId: string;
|
|
115
|
+
harness: HarnessId;
|
|
116
|
+
parentSessionId: string | null;
|
|
117
|
+
depth: number;
|
|
118
|
+
/** The model the child resolved to, so the client can seed its composer
|
|
119
|
+
* before the transcript carries a model_change entry. */
|
|
120
|
+
model: SelectedModel | null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function harnessOf(value: string | null | undefined): HarnessId | null {
|
|
124
|
+
if (!value) return null;
|
|
125
|
+
return HARNESS_IDS.includes(value as HarnessId) ? (value as HarnessId) : null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Resolve the parent's execution context and the child's cwd.
|
|
130
|
+
*
|
|
131
|
+
* The folder check is refused only when BOTH folders resolve: a session whose
|
|
132
|
+
* cwd sits outside every registered workspace has no folder to compare, and
|
|
133
|
+
* refusing on that would block work the sidebar would happily show.
|
|
134
|
+
*/
|
|
135
|
+
async function resolveParent(
|
|
136
|
+
parentSessionId: string,
|
|
137
|
+
requestedCwd: string | null,
|
|
138
|
+
): Promise<{ cwd: string; harness: HarnessId | null }> {
|
|
139
|
+
if (!(await sessionExists(parentSessionId))) {
|
|
140
|
+
throw new SessionSpawnError('parent_not_found', `Parent session ${parentSessionId} not found`);
|
|
141
|
+
}
|
|
142
|
+
if (await isSessionArchived(parentSessionId)) {
|
|
143
|
+
throw new SessionSpawnError('parent_archived', 'Cannot spawn under an archived session');
|
|
144
|
+
}
|
|
145
|
+
const parent = await resolveSessionRef(parentSessionId);
|
|
146
|
+
const cwd = requestedCwd || parent.cwd;
|
|
147
|
+
if (!cwd) {
|
|
148
|
+
throw new SessionSpawnError(
|
|
149
|
+
'cwd_required',
|
|
150
|
+
'The parent has no working directory; pass one explicitly',
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
if (requestedCwd && parent.cwd) {
|
|
154
|
+
const [parentFolder, childFolder] = await Promise.all([
|
|
155
|
+
folderIdForCwd(parent.cwd),
|
|
156
|
+
folderIdForCwd(requestedCwd),
|
|
157
|
+
]);
|
|
158
|
+
if (parentFolder !== null && childFolder !== null && parentFolder !== childFolder) {
|
|
159
|
+
throw new SessionSpawnError(
|
|
160
|
+
'parent_folder_mismatch',
|
|
161
|
+
'A child session must stay in its parent workspace folder',
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return { cwd, harness: parent.harness };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The model a worker runs on, by the one order that cannot invent a value.
|
|
170
|
+
*
|
|
171
|
+
* Explicit wins. Next is the SAME harness's live parent — a Claude reviewer
|
|
172
|
+
* inherits Claude's model, never omp's model id. Then the harness's own
|
|
173
|
+
* configured default is resolved by `startAcpSession`, so ACP gets null here.
|
|
174
|
+
* `omp-rpc` keeps its project default because that is a real omp model id.
|
|
175
|
+
*/
|
|
176
|
+
async function resolveSpawnModel(
|
|
177
|
+
requested: SelectedModel | null,
|
|
178
|
+
harness: HarnessId,
|
|
179
|
+
parentSessionId: string | null,
|
|
180
|
+
cwd: string,
|
|
181
|
+
): Promise<SelectedModel | null> {
|
|
182
|
+
if (requested) {
|
|
183
|
+
if (harness !== 'omp-rpc') {
|
|
184
|
+
const models = await harnessProbeModels(harness);
|
|
185
|
+
const exact = `${requested.provider}/${requested.modelId}`;
|
|
186
|
+
const listed = models.length === 0
|
|
187
|
+
|| models.some((model) => model.value === exact || model.value === requested.modelId);
|
|
188
|
+
if (!listed) {
|
|
189
|
+
throw new SessionSpawnError(
|
|
190
|
+
'model_unknown',
|
|
191
|
+
`${harnessDescriptor(harness)?.label ?? harness} does not list model ${exact}`,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return requested;
|
|
196
|
+
}
|
|
197
|
+
const parent = parentSessionId ? getLiveSession(parentSessionId) : undefined;
|
|
198
|
+
if (parent && harnessOfSession(parent) === harness && parent.runModel) {
|
|
199
|
+
return { provider: parent.runModel.provider, modelId: parent.runModel.modelId };
|
|
200
|
+
}
|
|
201
|
+
if (harness === 'omp-rpc') {
|
|
202
|
+
return projectModelForCwd(await getDb(), cwd).catch(() => null);
|
|
203
|
+
}
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** What a child receives before its task: identity, the one control verb, and
|
|
208
|
+
* the completion rule. Kabmo, not the orchestrator, writes this. */
|
|
209
|
+
function dispatchedWorkerPrompt(parentSessionId: string, sessionId: string, task: string): string {
|
|
210
|
+
return [
|
|
211
|
+
'[kabmo dispatched worker]',
|
|
212
|
+
`You are session ${sessionId}; your coordinator is session ${parentSessionId}.`,
|
|
213
|
+
'The coordinator cannot see this chat.',
|
|
214
|
+
'',
|
|
215
|
+
' # Read another session:',
|
|
216
|
+
' kabmo session output <id>',
|
|
217
|
+
' # Ask the coordinator or report a blocker, then end your turn:',
|
|
218
|
+
` kabmo session tell ${parentSessionId} --message-file - --mode auto`,
|
|
219
|
+
'',
|
|
220
|
+
'Do only this task. When you finish, your final message is your report: what you did,',
|
|
221
|
+
'what you found, what is left. Do not send a separate acknowledgement.',
|
|
222
|
+
'[/kabmo dispatched worker]',
|
|
223
|
+
'',
|
|
224
|
+
'TASK',
|
|
225
|
+
task,
|
|
226
|
+
].join('\n');
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Spawn a session, record its relation, then dispatch the first prompt. */
|
|
230
|
+
export async function spawnChildSession(
|
|
231
|
+
input: SpawnChildSessionInput,
|
|
232
|
+
deps: SpawnSessionDeps = defaultDeps,
|
|
233
|
+
): Promise<SpawnChildSessionResult> {
|
|
234
|
+
const prompt = input.prompt.trim();
|
|
235
|
+
if (!prompt) {
|
|
236
|
+
throw new SessionSpawnError('spawn_failed', 'A first prompt is required');
|
|
237
|
+
}
|
|
238
|
+
const parentSessionId = input.parentSessionId ?? null;
|
|
239
|
+
const requestedHarness = input.harness ?? null;
|
|
240
|
+
const parent = parentSessionId
|
|
241
|
+
? await resolveParent(parentSessionId, input.cwd ?? null)
|
|
242
|
+
: null;
|
|
243
|
+
const cwd = parent?.cwd ?? input.cwd ?? null;
|
|
244
|
+
if (!cwd) {
|
|
245
|
+
throw new SessionSpawnError('cwd_required', 'A working directory is required');
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const requested = harnessOf(requestedHarness);
|
|
249
|
+
if (requestedHarness && requested === null) {
|
|
250
|
+
throw new SessionSpawnError('harness_unknown', `Unknown harness: ${requestedHarness}`);
|
|
251
|
+
}
|
|
252
|
+
const harness = requested ?? parent?.harness ?? 'omp-rpc';
|
|
253
|
+
const parentRelation = parentSessionId ? await getSessionRelation(parentSessionId) : null;
|
|
254
|
+
const depth = (parentRelation?.depth ?? 0) + 1;
|
|
255
|
+
if (depth > MAX_SESSION_DEPTH) {
|
|
256
|
+
throw new SessionSpawnError(
|
|
257
|
+
'spawn_failed',
|
|
258
|
+
`A session tree is limited to ${MAX_SESSION_DEPTH} levels`,
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const accessMode = input.accessMode ?? await loadPersistedAccessMode();
|
|
263
|
+
const token = createAgentToken();
|
|
264
|
+
const env = agentEnvironment(token);
|
|
265
|
+
const model = await resolveSpawnModel(input.model ?? null, harness, parentSessionId, cwd);
|
|
266
|
+
const started =
|
|
267
|
+
harness === 'omp-rpc'
|
|
268
|
+
? await deps.startRpc(cwd, accessMode, env)
|
|
269
|
+
: await deps.startAcp({
|
|
270
|
+
harness,
|
|
271
|
+
sessionId: `__new__${Bun.randomUUIDv7()}`,
|
|
272
|
+
cwd,
|
|
273
|
+
model,
|
|
274
|
+
thinkingLevel: input.thinkingLevel ?? null,
|
|
275
|
+
accessMode,
|
|
276
|
+
env,
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// `startRpc` takes a cwd only, so an RPC child would ignore the composer's
|
|
280
|
+
// model pick and run the project default. The pick is applied to the child
|
|
281
|
+
// BEFORE its first prompt, the same order `POST /api/agent/new` uses. ACP
|
|
282
|
+
// already received both at spawn time above.
|
|
283
|
+
if (harness === 'omp-rpc') {
|
|
284
|
+
if (model) await started.session.send({ type: 'set_model', ...model }).catch(() => {});
|
|
285
|
+
if (input.thinkingLevel) {
|
|
286
|
+
await started.session.send({ type: 'set_thinking_level', level: input.thinkingLevel }).catch(() => {});
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
try {
|
|
291
|
+
await rememberAgentToken(started.realSessionId, token);
|
|
292
|
+
await createSessionRelation({
|
|
293
|
+
sessionId: started.realSessionId,
|
|
294
|
+
parentSessionId,
|
|
295
|
+
lifecycleOwnerSessionId: input.lifecycleOwnerSessionId ?? parentSessionId,
|
|
296
|
+
visibility: input.visibility ?? 'visible',
|
|
297
|
+
parentNotifications: input.parentNotifications ?? 'auto',
|
|
298
|
+
});
|
|
299
|
+
} catch (error) {
|
|
300
|
+
// The child exists but cannot be placed in the tree; leaving it running
|
|
301
|
+
// would be a session nobody owns.
|
|
302
|
+
await started.session.destroyAndWait().catch(() => {});
|
|
303
|
+
throw new SessionSpawnError(
|
|
304
|
+
'spawn_failed',
|
|
305
|
+
error instanceof Error ? error.message : 'Could not record the session relation',
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
try {
|
|
310
|
+
const message = parentSessionId
|
|
311
|
+
? dispatchedWorkerPrompt(parentSessionId, started.realSessionId, prompt)
|
|
312
|
+
: prompt;
|
|
313
|
+
await started.session.send({
|
|
314
|
+
type: 'prompt',
|
|
315
|
+
message,
|
|
316
|
+
...(input.images?.length ? { images: input.images } : {}),
|
|
317
|
+
});
|
|
318
|
+
} catch (error) {
|
|
319
|
+
await started.session.destroyAndWait().catch(() => {});
|
|
320
|
+
throw new SessionSpawnError(
|
|
321
|
+
'spawn_failed',
|
|
322
|
+
`${harnessDescriptor(harness)?.label ?? harness} refused the first prompt: ${
|
|
323
|
+
error instanceof Error ? error.message : String(error)
|
|
324
|
+
}`,
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return {
|
|
329
|
+
sessionId: started.realSessionId,
|
|
330
|
+
harness,
|
|
331
|
+
parentSessionId,
|
|
332
|
+
depth,
|
|
333
|
+
model,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
* exactly once: the replay's last line followed immediately by live output.
|
|
9
9
|
*
|
|
10
10
|
* The two ways this breaks are both silent, and both are duplicate/garbled text
|
|
11
|
-
* in
|
|
12
|
-
* delivery, so a replay captured BEFORE the queue is flushed carries the pending
|
|
11
|
+
* in the terminal rather than an error. Output is appended to `history` and
|
|
12
|
+
* queued for delivery, so a replay captured BEFORE the queue is flushed carries the pending
|
|
13
13
|
* bytes and the queued frame carries them again (each line printed twice); a
|
|
14
14
|
* frame delivered before the replay arrives ahead of it in the wrong order.
|
|
15
15
|
* `attachTerminal` flushes first for exactly this reason, and this is the test
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
* receives must equal the concatenation of the reads, in the same order, and the
|
|
10
10
|
* framing must be bounded.
|
|
11
11
|
*
|
|
12
|
-
* Two failures it must not have, both of which
|
|
13
|
-
* than as an error: dropping a trailing partial frame (a prompt that never
|
|
12
|
+
* Two failures it must not have, both of which the renderer draws as garbage
|
|
13
|
+
* rather than as an error: dropping a trailing partial frame (a prompt that never
|
|
14
14
|
* appears until the next keypress) and reordering (an escape sequence split
|
|
15
15
|
* across two reads, reassembled after a later one).
|
|
16
16
|
*
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
* concatenated after exit are byte-identical), and `history.append` already
|
|
22
22
|
* retains those views for its 512 KiB scrollback, so a defensive copy per
|
|
23
23
|
* read would cost exactly what the coalescing is there to save.
|
|
24
|
-
* - **Split sequences.**
|
|
24
|
+
* - **Split sequences.** the parser reads a byte stream across `write` calls, so a
|
|
25
25
|
* frame boundary mid-escape is harmless — dropping bytes is not, which is why
|
|
26
26
|
* a flush never discards anything it accepted.
|
|
27
27
|
* - **Buffer without a bound.** The flush is size- or time-triggered, so a
|