@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
|
@@ -22,15 +22,17 @@ import { DEFAULT_SESSION_SORT_OPTION, isValidSessionSortOption, sortFolders } fr
|
|
|
22
22
|
import { groupSessionsByRoot, sessionTitleFor } from '@/shared/lib/omp/session/sidebar';
|
|
23
23
|
import { loadOmpSidebarData } from '@/server/lib/omp/session/reader';
|
|
24
24
|
import { sessionHasSubagents } from '@/server/lib/omp/session/subagent-presence';
|
|
25
|
-
import { loadStreamStates
|
|
25
|
+
import { loadStreamStates } from '@/shared/lib/omp/session/stream-state.server';
|
|
26
26
|
import { healStaleStreamStatuses } from '@/shared/lib/omp/session/stream-heal.server';
|
|
27
27
|
import { getAwaitingInputSessionIds, getLiveRunSessionIds, listRpcSessions } from '@/server/lib/omp/rpc/session-registry';
|
|
28
28
|
import type { SessionItemData, SessionSortOption, WorkspaceFolderData } from '@/shared/types';
|
|
29
|
+
import type { SidebarStatusEntry } from '@/shared/types/realtime';
|
|
29
30
|
import { sessionWorktreeMap } from '@/server/lib/fs/worktree/topology';
|
|
30
31
|
import { DEFAULT_HARNESS_ID, HARNESS_IDS } from '@/shared/lib/harness/catalog';
|
|
31
32
|
import { knownHarness, loadAcpSessions, loadHarnessBySession } from '@/server/lib/harness/session-store.server';
|
|
32
33
|
import { canonicalPath } from '@/server/lib/fs/canonical';
|
|
33
34
|
import { acpRowsFor } from '@/server/lib/omp/session/acp-rows.server';
|
|
35
|
+
import { decorateSidebarSessions } from '@/server/lib/sessions/sidebar-relations.server';
|
|
34
36
|
import type { SessionWorktreeInfo } from '@/shared/lib/workspace/worktree/types';
|
|
35
37
|
import type { OmpSession } from '@/shared/types/omp/session';
|
|
36
38
|
|
|
@@ -152,13 +154,10 @@ export async function loadSidebarStructure(): Promise<SessionListPayload> {
|
|
|
152
154
|
* belongs to the subscribe-time snapshot, which is the moment the old sidebar
|
|
153
155
|
* poll used to provide it.
|
|
154
156
|
*/
|
|
157
|
+
/** One session's volatile sidebar fields, as the status topic carries them. */
|
|
155
158
|
export async function loadSidebarStatus(
|
|
156
159
|
options: { repair?: boolean } = {},
|
|
157
|
-
): Promise<Record<string, {
|
|
158
|
-
streamStatus?: 'stream' | 'finish' | 'abort';
|
|
159
|
-
awaitingInput?: boolean;
|
|
160
|
-
runModel?: SessionRunModel;
|
|
161
|
-
}>> {
|
|
160
|
+
): Promise<Record<string, SidebarStatusEntry>> {
|
|
162
161
|
// No MOCK short-circuit: the simulated stream writes the same `stream` row a
|
|
163
162
|
// real run does (`routes/chat/stream.ts`), so the sidebar spinner and the
|
|
164
163
|
// chat's generating indicator have one source in every mode. Returning `{}`
|
|
@@ -181,13 +180,13 @@ export async function loadSidebarStatus(
|
|
|
181
180
|
for (const session of listRpcSessions()) void session.runSettle.reconcile('request');
|
|
182
181
|
}
|
|
183
182
|
|
|
184
|
-
const status: Record<string, {
|
|
185
|
-
streamStatus?: 'stream' | 'finish' | 'abort';
|
|
186
|
-
awaitingInput?: boolean;
|
|
187
|
-
runModel?: SessionRunModel;
|
|
188
|
-
}> = {};
|
|
183
|
+
const status: Record<string, SidebarStatusEntry> = {};
|
|
189
184
|
for (const [id, state] of Object.entries(await loadStreamStates())) {
|
|
190
|
-
status[id] = {
|
|
185
|
+
status[id] = {
|
|
186
|
+
streamStatus: state.status,
|
|
187
|
+
...(state.model ? { runModel: state.model } : {}),
|
|
188
|
+
...(state.failure ? { runFailure: state.failure } : {}),
|
|
189
|
+
};
|
|
191
190
|
}
|
|
192
191
|
// Sessions blocked on a dialog nobody has answered yet. Read live from the
|
|
193
192
|
// process registry (never persisted): the child that owns the question is the
|
|
@@ -329,9 +328,8 @@ async function buildRealFolders(folderRows: FolderRow[], archivedIds: Set<string
|
|
|
329
328
|
// The scan's own rule is `b.modified.localeCompare(a.modified)`
|
|
330
329
|
// (`groupSessionsByRoot`), so this uses the same key rather than a second
|
|
331
330
|
// definition of "newest".
|
|
332
|
-
const allSessions = [...folderSessions, ...uniqueAcp].sort((a, b) =>
|
|
333
|
-
|
|
334
|
-
);
|
|
331
|
+
const allSessions = [...folderSessions, ...uniqueAcp].sort((a, b) => (b.updated_at ?? b.created_at ?? '').localeCompare(a.updated_at ?? a.created_at ?? ''));
|
|
332
|
+
const visibleSessions = await decorateSidebarSessions(allSessions);
|
|
335
333
|
return {
|
|
336
334
|
id: folder.id,
|
|
337
335
|
name: folder.name,
|
|
@@ -342,9 +340,9 @@ async function buildRealFolders(folderRows: FolderRow[], archivedIds: Set<string
|
|
|
342
340
|
accentColor: folder.accent_color || '',
|
|
343
341
|
icon: folder.icon || 'default',
|
|
344
342
|
customIconUrl: folder.custom_icon_url || undefined,
|
|
345
|
-
sessions:
|
|
346
|
-
hasMore:
|
|
347
|
-
totalSessions:
|
|
343
|
+
sessions: visibleSessions,
|
|
344
|
+
hasMore: visibleSessions.length > 7,
|
|
345
|
+
totalSessions: visibleSessions.length,
|
|
348
346
|
};
|
|
349
347
|
}));
|
|
350
348
|
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The small pieces of omp transcript surgery shared by two features.
|
|
8
|
+
*
|
|
9
|
+
* BTW promotion turns a side-question transcript into a chat; a fork turns a
|
|
10
|
+
* chat into a branch. Both need the same three primitives — read whole lines
|
|
11
|
+
* without a torn tail, find the leaf, and rewrite the title/header pair under a
|
|
12
|
+
* new id — and a second copy of them is how the two drift.
|
|
13
|
+
*
|
|
14
|
+
* The file layout they rely on (verified against omp files): line 1 is a
|
|
15
|
+
* fixed-width title slot, line 2 is the `session` header carrying `id`, `cwd`
|
|
16
|
+
* and `parentSession`, and the rest are entries.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { serializeTitleSlot } from '@/server/lib/omp/session/title-slot';
|
|
20
|
+
|
|
21
|
+
export function parseJsonObject(line: string): Record<string, unknown> | null {
|
|
22
|
+
try {
|
|
23
|
+
const value: unknown = JSON.parse(line);
|
|
24
|
+
return value && typeof value === 'object' && !Array.isArray(value)
|
|
25
|
+
? (value as Record<string, unknown>)
|
|
26
|
+
: null;
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The file's lines, minus a torn tail.
|
|
34
|
+
*
|
|
35
|
+
* A snapshot taken while the writer is mid-append can end in a partial line;
|
|
36
|
+
* keeping it would make a resumed child fail to load the transcript.
|
|
37
|
+
*/
|
|
38
|
+
export function completeLines(text: string): string[] {
|
|
39
|
+
const lines = text.split('\n');
|
|
40
|
+
if (lines[lines.length - 1] === '') lines.pop();
|
|
41
|
+
const last = lines[lines.length - 1];
|
|
42
|
+
if (last !== undefined && parseJsonObject(last) === null) lines.pop();
|
|
43
|
+
return lines;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The transcript's leaf: the id of its last entry. */
|
|
47
|
+
export function lastEntryId(lines: string[]): string | null {
|
|
48
|
+
for (let index = lines.length - 1; index >= 0; index -= 1) {
|
|
49
|
+
const record = parseJsonObject(lines[index]);
|
|
50
|
+
if (record && typeof record.id === 'string') return record.id;
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The title slot line, padded to the fixed slot omp rewrites in place, with
|
|
56
|
+
* the trailing newline removed (callers join the lines themselves). */
|
|
57
|
+
export function buildTitleLine(title: string, source: string, updatedAt: string): string {
|
|
58
|
+
const line = serializeTitleSlot({
|
|
59
|
+
title,
|
|
60
|
+
source: source === 'auto' ? 'auto' : 'user',
|
|
61
|
+
updatedAt,
|
|
62
|
+
});
|
|
63
|
+
return line.endsWith('\n') ? line.slice(0, -1) : line;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The chat's own title, from the title slot or the header. */
|
|
67
|
+
export function transcriptTitle(lines: string[]): string {
|
|
68
|
+
for (const line of lines) {
|
|
69
|
+
const record = parseJsonObject(line);
|
|
70
|
+
if (!record) continue;
|
|
71
|
+
if (record.type === 'title' && typeof record.title === 'string') return record.title;
|
|
72
|
+
if (record.type === 'session' && typeof record.title === 'string') return record.title;
|
|
73
|
+
if (record.type === 'session') return '';
|
|
74
|
+
}
|
|
75
|
+
return '';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface BranchTranscriptInput {
|
|
79
|
+
lines: string[];
|
|
80
|
+
newId: string;
|
|
81
|
+
timestamp: string;
|
|
82
|
+
title: string;
|
|
83
|
+
/** Absolute path of the file the branch came from, as omp records it. */
|
|
84
|
+
parentSessionFile: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Rewrite a source transcript as a branch under a new identity.
|
|
89
|
+
*
|
|
90
|
+
* The title slot moves to line 1 (dropping the source's slot), and the header
|
|
91
|
+
* gets the new id, the new timestamp, the branch title and the source path.
|
|
92
|
+
* Every other record is copied verbatim — including model/thinking changes,
|
|
93
|
+
* which are what make the resumed child continue with the same selections.
|
|
94
|
+
*/
|
|
95
|
+
export function branchTranscript(input: BranchTranscriptInput): string {
|
|
96
|
+
const body: string[] = [];
|
|
97
|
+
let titleDropped = false;
|
|
98
|
+
let headerWritten = false;
|
|
99
|
+
for (const line of input.lines) {
|
|
100
|
+
const record = parseJsonObject(line);
|
|
101
|
+
if (record?.type === 'title' && !titleDropped) {
|
|
102
|
+
titleDropped = true;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (record?.type === 'session' && !headerWritten) {
|
|
106
|
+
headerWritten = true;
|
|
107
|
+
body.push(
|
|
108
|
+
JSON.stringify({
|
|
109
|
+
...record,
|
|
110
|
+
id: input.newId,
|
|
111
|
+
timestamp: input.timestamp,
|
|
112
|
+
title: input.title,
|
|
113
|
+
titleSource: 'user',
|
|
114
|
+
parentSession: input.parentSessionFile,
|
|
115
|
+
}),
|
|
116
|
+
);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
body.push(line);
|
|
120
|
+
}
|
|
121
|
+
if (!headerWritten) throw new Error('Transcript has no session header');
|
|
122
|
+
return `${[buildTitleLine(input.title, 'user', input.timestamp), ...body].join('\n')}\n`;
|
|
123
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
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 {
|
|
11
|
+
agentTokenScopeForPath,
|
|
12
|
+
createAgentToken,
|
|
13
|
+
hashAgentToken,
|
|
14
|
+
initAgentTokens,
|
|
15
|
+
rememberAgentToken,
|
|
16
|
+
revokeAgentToken,
|
|
17
|
+
verifyAgentToken,
|
|
18
|
+
} from '@/server/lib/sessions/agent-token.server';
|
|
19
|
+
|
|
20
|
+
beforeEach(async () => {
|
|
21
|
+
await isolateDb();
|
|
22
|
+
await ensureSessionRelationsSchema(await getDb());
|
|
23
|
+
globalThis.__kabmoAgentTokens = undefined;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterEach(async () => {
|
|
27
|
+
globalThis.__kabmoAgentTokens = undefined;
|
|
28
|
+
await releaseDb();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('agent tokens', () => {
|
|
32
|
+
test('a remembered token verifies and can be revoked', async () => {
|
|
33
|
+
const token = createAgentToken();
|
|
34
|
+
await rememberAgentToken('s1', token);
|
|
35
|
+
expect(verifyAgentToken(token)).toBe('s1');
|
|
36
|
+
expect(verifyAgentToken('other')).toBeNull();
|
|
37
|
+
|
|
38
|
+
await revokeAgentToken('s1');
|
|
39
|
+
expect(verifyAgentToken(token)).toBeNull();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('the cache is rebuilt from the table after a restart', async () => {
|
|
43
|
+
const token = createAgentToken();
|
|
44
|
+
await rememberAgentToken('s1', token);
|
|
45
|
+
globalThis.__kabmoAgentTokens = undefined;
|
|
46
|
+
await initAgentTokens();
|
|
47
|
+
expect(verifyAgentToken(token)).toBe('s1');
|
|
48
|
+
// Only the hash is stored.
|
|
49
|
+
const rows = (await (await getDb()).all('SELECT token_hash FROM session_agent_tokens')) as {
|
|
50
|
+
token_hash: string;
|
|
51
|
+
}[];
|
|
52
|
+
expect(rows[0]?.token_hash).toBe(hashAgentToken(token));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('the path scope names the session it may touch', () => {
|
|
56
|
+
expect(agentTokenScopeForPath('/api/sessions/spawn')).toBe('any');
|
|
57
|
+
expect(agentTokenScopeForPath('/api/agent/s1')).toBe('s1');
|
|
58
|
+
expect(agentTokenScopeForPath('/api/chat/s1')).toBe('s1');
|
|
59
|
+
expect(agentTokenScopeForPath('/api/sessions/s1/fork')).toBe('s1');
|
|
60
|
+
expect(agentTokenScopeForPath('/api/sessions/s1/relations')).toBe('s1');
|
|
61
|
+
expect(agentTokenScopeForPath('/api/sessions/s1/archive')).toBeNull();
|
|
62
|
+
expect(agentTokenScopeForPath('/api/settings')).toBeNull();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The credential an agent uses to call back into Kabmo.
|
|
8
|
+
*
|
|
9
|
+
* A browser has a cookie; a subprocess does not. Every spawned session gets one
|
|
10
|
+
* random token in its environment, and the CLI presents it as a bearer token.
|
|
11
|
+
* Only the SHA-256 hash is stored, so the database cannot be replayed as a
|
|
12
|
+
* credential.
|
|
13
|
+
*
|
|
14
|
+
* The gate is synchronous and runs on every request, so verified hashes are
|
|
15
|
+
* mirrored in memory: `initAgentTokens` loads them once at boot, mint/revoke
|
|
16
|
+
* update both sides. A restart re-reads the table, which is what makes a token
|
|
17
|
+
* outlive the process that minted it.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { getDb } from '@/server/db.server';
|
|
21
|
+
|
|
22
|
+
interface CacheEntry {
|
|
23
|
+
sessionId: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
declare global {
|
|
27
|
+
// eslint-disable-next-line no-var
|
|
28
|
+
var __kabmoAgentTokens: Map<string, CacheEntry> | undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function cache(): Map<string, CacheEntry> {
|
|
32
|
+
globalThis.__kabmoAgentTokens ??= new Map();
|
|
33
|
+
return globalThis.__kabmoAgentTokens;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function hashAgentToken(token: string): string {
|
|
37
|
+
return new Bun.CryptoHasher('sha256').update(token).digest('hex');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Load every non-revoked token hash into the gate's cache. Call once at boot. */
|
|
41
|
+
export async function initAgentTokens(): Promise<void> {
|
|
42
|
+
try {
|
|
43
|
+
const db = await getDb();
|
|
44
|
+
const rows = (await db.all(
|
|
45
|
+
'SELECT session_id, token_hash FROM session_agent_tokens WHERE revoked_at IS NULL',
|
|
46
|
+
)) as { session_id: string; token_hash: string }[];
|
|
47
|
+
const next = new Map<string, CacheEntry>();
|
|
48
|
+
for (const row of rows) next.set(row.token_hash, { sessionId: row.session_id });
|
|
49
|
+
globalThis.__kabmoAgentTokens = next;
|
|
50
|
+
} catch {
|
|
51
|
+
// A database opened before this table existed has no agent credentials;
|
|
52
|
+
// the browser path is unaffected.
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A fresh token VALUE, before the session id exists. */
|
|
57
|
+
export function createAgentToken(): string {
|
|
58
|
+
return `kabmo_agent_${crypto.randomUUID().replace(/-/g, '')}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Record the token against the session the spawn actually produced. */
|
|
62
|
+
export async function rememberAgentToken(sessionId: string, token: string): Promise<void> {
|
|
63
|
+
const hash = hashAgentToken(token);
|
|
64
|
+
cache().set(hash, { sessionId });
|
|
65
|
+
const db = await getDb();
|
|
66
|
+
await db.run(
|
|
67
|
+
`INSERT INTO session_agent_tokens (session_id, token_hash, created_at, revoked_at)
|
|
68
|
+
VALUES (?, ?, ?, NULL)
|
|
69
|
+
ON CONFLICT(session_id) DO UPDATE SET token_hash = excluded.token_hash, revoked_at = NULL`,
|
|
70
|
+
[sessionId, hash, Date.now()],
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The session a bearer token identifies, or null. Synchronous by contract. */
|
|
75
|
+
export function verifyAgentToken(token: string | null): string | null {
|
|
76
|
+
if (!token) return null;
|
|
77
|
+
return cache().get(hashAgentToken(token))?.sessionId ?? null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A fresh token and the environment that carries it, for a session that has no
|
|
81
|
+
* id yet. Every spawn path uses this so even a top-level chat can delegate. */
|
|
82
|
+
export function mintAgentEnvironment(): { token: string; env: Record<string, string> } {
|
|
83
|
+
const token = createAgentToken();
|
|
84
|
+
return { token, env: agentEnvironment(token) };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The value from an `Authorization: Bearer …` header, or null. */
|
|
88
|
+
export function bearerToken(request: Request): string | null {
|
|
89
|
+
const header = request.headers.get('authorization') ?? '';
|
|
90
|
+
const match = /^Bearer\s+(.+)$/i.exec(header.trim());
|
|
91
|
+
return match?.[1]?.trim() || null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The environment a spawned child needs to reach this server. */
|
|
95
|
+
export function agentEnvironment(token: string): Record<string, string> {
|
|
96
|
+
const port = Number(Bun.env.PORT) || 3000;
|
|
97
|
+
return {
|
|
98
|
+
KABMO_AGENT_TOKEN: token,
|
|
99
|
+
KABMO_SERVER_URL: Bun.env.KABMO_SERVER_URL || `http://localhost:${port}`,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* What a token may touch, by path.
|
|
105
|
+
*
|
|
106
|
+
* `'any'` is only spawn: a child must be allowed to create its own children.
|
|
107
|
+
* Every other path carries the session id it names, and the token must be that
|
|
108
|
+
* session's. A token is not an admin credential.
|
|
109
|
+
*/
|
|
110
|
+
export function agentTokenScopeForPath(pathname: string): 'any' | string | null {
|
|
111
|
+
if (pathname === '/api/sessions/spawn') return 'any';
|
|
112
|
+
const direct = /^\/api\/(?:agent|chat)\/([^/]+)$/.exec(pathname);
|
|
113
|
+
if (direct) return direct[1] ?? null;
|
|
114
|
+
const sub = /^\/api\/sessions\/([^/]+)\/(?:fork|relations|answer)$/.exec(pathname);
|
|
115
|
+
return sub?.[1] ?? null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Drop the in-memory entry for a session whose rows are being purged. */
|
|
119
|
+
export function forgetAgentTokenCache(sessionId: string): void {
|
|
120
|
+
for (const [hash, entry] of cache()) {
|
|
121
|
+
if (entry.sessionId === sessionId) cache().delete(hash);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export async function revokeAgentToken(sessionId: string): Promise<void> {
|
|
126
|
+
const db = await getDb();
|
|
127
|
+
await db.run('UPDATE session_agent_tokens SET revoked_at = ? WHERE session_id = ?', [
|
|
128
|
+
Date.now(),
|
|
129
|
+
sessionId,
|
|
130
|
+
]);
|
|
131
|
+
for (const [hash, entry] of cache()) {
|
|
132
|
+
if (entry.sessionId === sessionId) cache().delete(hash);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
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 { loadSessionAnswer, saveSessionAnswer } from '@/server/lib/sessions/answers.server';
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
await isolateDb();
|
|
14
|
+
await ensureSessionRelationsSchema(await getDb());
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
afterEach(async () => {
|
|
18
|
+
await releaseDb();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('session answers', () => {
|
|
22
|
+
test('the last answer wins and whitespace-only text is ignored', async () => {
|
|
23
|
+
await saveSessionAnswer('s1', 'first');
|
|
24
|
+
await saveSessionAnswer('s1', ' ');
|
|
25
|
+
expect(await loadSessionAnswer('s1')).toBe('first');
|
|
26
|
+
await saveSessionAnswer('s1', 'second');
|
|
27
|
+
expect(await loadSessionAnswer('s1')).toBe('second');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('an unknown session answers null', async () => {
|
|
31
|
+
expect(await loadSessionAnswer('missing')).toBeNull();
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The last assistant text a turn closed.
|
|
8
|
+
*
|
|
9
|
+
* ACP has no transcript Kabmo can read after the process dies, so the answer a
|
|
10
|
+
* parent or a CLI asks for lived only in the closed message. Storing it is what
|
|
11
|
+
* makes `kabmo session output` and the child-outcome notice agree after a
|
|
12
|
+
* restart. omp rows are written too: the JSONL is authoritative, but one read
|
|
13
|
+
* path is simpler than two.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { getDb } from '@/server/db.server';
|
|
17
|
+
|
|
18
|
+
export async function saveSessionAnswer(sessionId: string, answer: string): Promise<void> {
|
|
19
|
+
const text = answer.trim();
|
|
20
|
+
if (!text) return;
|
|
21
|
+
const db = await getDb();
|
|
22
|
+
await db.run(
|
|
23
|
+
`INSERT INTO session_answers (session_id, answer, updated_at)
|
|
24
|
+
VALUES (?, ?, ?)
|
|
25
|
+
ON CONFLICT(session_id) DO UPDATE SET answer = excluded.answer, updated_at = excluded.updated_at`,
|
|
26
|
+
[sessionId, text, Date.now()],
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function loadSessionAnswer(sessionId: string): Promise<string | null> {
|
|
31
|
+
const db = await getDb();
|
|
32
|
+
const row = await db.get<{ answer: string }>(
|
|
33
|
+
'SELECT answer FROM session_answers WHERE session_id = ?',
|
|
34
|
+
[sessionId],
|
|
35
|
+
);
|
|
36
|
+
return row?.answer ?? null;
|
|
37
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The cold path's branch logic: file → RPC, recorded ACP → session/load,
|
|
8
|
+
* nothing resolvable → null. The deps are fakes; a real spawn is the live
|
|
9
|
+
* smoke's job, not a unit test's.
|
|
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 { ensureSessionRelationsSchema } from '@/server/lib/sessions/schema.server';
|
|
18
|
+
import { getDb } from '@/server/db.server';
|
|
19
|
+
import { ensureSessionLive, type DispatchDeps } from '@/server/lib/sessions/dispatch.server';
|
|
20
|
+
import { clearSessionFileCaches } from '@/server/lib/omp/session/files';
|
|
21
|
+
import { saveHarnessSession } from '@/server/lib/harness/session-store.server';
|
|
22
|
+
import type { QueueDeliveryHost } from '@/server/lib/queue/delivery.server';
|
|
23
|
+
|
|
24
|
+
let agentRoot = '';
|
|
25
|
+
let savedAgentDir: string | undefined;
|
|
26
|
+
|
|
27
|
+
beforeEach(async () => {
|
|
28
|
+
await isolateDb();
|
|
29
|
+
await ensureSessionRelationsSchema(await getDb());
|
|
30
|
+
savedAgentDir = Bun.env.PI_CODING_AGENT_DIR;
|
|
31
|
+
agentRoot = mkdtempSync(join(tmpdir(), 'kabmo-dispatch-'));
|
|
32
|
+
Bun.env.PI_CODING_AGENT_DIR = agentRoot;
|
|
33
|
+
clearSessionFileCaches();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
afterEach(async () => {
|
|
37
|
+
if (savedAgentDir === undefined) delete Bun.env.PI_CODING_AGENT_DIR;
|
|
38
|
+
else Bun.env.PI_CODING_AGENT_DIR = savedAgentDir;
|
|
39
|
+
clearSessionFileCaches();
|
|
40
|
+
await releaseDb();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
function host(sessionId: string): QueueDeliveryHost {
|
|
44
|
+
return {
|
|
45
|
+
sessionId,
|
|
46
|
+
isAlive: () => true,
|
|
47
|
+
isBusy: () => false,
|
|
48
|
+
async send() {
|
|
49
|
+
return null;
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function deps(overrides: Partial<DispatchDeps>): DispatchDeps {
|
|
55
|
+
return {
|
|
56
|
+
async findFile() {
|
|
57
|
+
return undefined;
|
|
58
|
+
},
|
|
59
|
+
async startOmp() {
|
|
60
|
+
throw new Error('not used');
|
|
61
|
+
},
|
|
62
|
+
async startAcp() {
|
|
63
|
+
throw new Error('not used');
|
|
64
|
+
},
|
|
65
|
+
...overrides,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
describe('ensureSessionLive', () => {
|
|
70
|
+
test('an id with no transcript, record or cwd answers null', async () => {
|
|
71
|
+
expect(await ensureSessionLive('missing-session', deps({}))).toBeNull();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('a recorded ACP session resumes through session/load', async () => {
|
|
75
|
+
await saveHarnessSession({
|
|
76
|
+
sessionId: 'acp-1',
|
|
77
|
+
harness: 'claude',
|
|
78
|
+
agentSessionId: 'agent-1',
|
|
79
|
+
cwd: '/tmp/project',
|
|
80
|
+
});
|
|
81
|
+
const calls: unknown[] = [];
|
|
82
|
+
const result = await ensureSessionLive(
|
|
83
|
+
'acp-1',
|
|
84
|
+
deps({
|
|
85
|
+
async startAcp(sessionId, harness, cwd, agentSessionId) {
|
|
86
|
+
calls.push({ sessionId, harness, cwd, agentSessionId });
|
|
87
|
+
return host(sessionId);
|
|
88
|
+
},
|
|
89
|
+
}),
|
|
90
|
+
);
|
|
91
|
+
expect(result?.sessionId).toBe('acp-1');
|
|
92
|
+
expect(calls).toEqual([
|
|
93
|
+
{ sessionId: 'acp-1', harness: 'claude', cwd: '/tmp/project', agentSessionId: 'agent-1' },
|
|
94
|
+
]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('a file-backed session resumes over RPC even when its record says ACP', async () => {
|
|
98
|
+
await saveHarnessSession({
|
|
99
|
+
sessionId: 'omp-acp-1',
|
|
100
|
+
harness: 'omp-acp',
|
|
101
|
+
agentSessionId: '',
|
|
102
|
+
cwd: '/tmp/project',
|
|
103
|
+
});
|
|
104
|
+
const calls: unknown[] = [];
|
|
105
|
+
const result = await ensureSessionLive(
|
|
106
|
+
'omp-acp-1',
|
|
107
|
+
deps({
|
|
108
|
+
async findFile() {
|
|
109
|
+
return '/tmp/sessions/project/session.jsonl';
|
|
110
|
+
},
|
|
111
|
+
async startOmp(sessionId, file, cwd) {
|
|
112
|
+
calls.push({ sessionId, file, cwd });
|
|
113
|
+
return host(sessionId);
|
|
114
|
+
},
|
|
115
|
+
async startAcp() {
|
|
116
|
+
throw new Error('must not use session/load for an omp file');
|
|
117
|
+
},
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
expect(result?.sessionId).toBe('omp-acp-1');
|
|
121
|
+
expect(calls).toEqual([
|
|
122
|
+
{ sessionId: 'omp-acp-1', file: '/tmp/sessions/project/session.jsonl', cwd: '/tmp/project' },
|
|
123
|
+
]);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('a failed resume answers null rather than throwing into the caller', async () => {
|
|
127
|
+
await saveHarnessSession({
|
|
128
|
+
sessionId: 'acp-2',
|
|
129
|
+
harness: 'claude',
|
|
130
|
+
agentSessionId: 'agent-2',
|
|
131
|
+
cwd: '/tmp/project',
|
|
132
|
+
});
|
|
133
|
+
const result = await ensureSessionLive(
|
|
134
|
+
'acp-2',
|
|
135
|
+
deps({
|
|
136
|
+
async startAcp() {
|
|
137
|
+
throw new Error('agent refused');
|
|
138
|
+
},
|
|
139
|
+
}),
|
|
140
|
+
);
|
|
141
|
+
expect(result).toBeNull();
|
|
142
|
+
});
|
|
143
|
+
});
|