@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,337 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Session trees: the parent edge (sidebar + outcome notifications), the
|
|
8
|
+
* cleanup owner edge, and the fork origin edge.
|
|
9
|
+
*
|
|
10
|
+
* Sessions are not rows — an omp chat is a file, an ACP chat belongs to the
|
|
11
|
+
* agent — so this module stores only relationships and validates only their
|
|
12
|
+
* shape. Whether a parent is still alive, and whether it belongs to the same
|
|
13
|
+
* workspace folder, is the spawn service's question (it can resolve a session's
|
|
14
|
+
* cwd); a relation module that guessed either would be wrong for one transport.
|
|
15
|
+
*
|
|
16
|
+
* The depth cap is enforced HERE because it is a property of the tree, not of
|
|
17
|
+
* the UI: four levels, matching the reference, and a reparent computes the
|
|
18
|
+
* subtree height so a deep branch cannot be lifted under a deep parent.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { getDb } from '@/server/db.server';
|
|
22
|
+
import { forgetAgentTokenCache } from '@/server/lib/sessions/agent-token.server';
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
MAX_SESSION_DEPTH,
|
|
26
|
+
SessionRelationError,
|
|
27
|
+
fromRow,
|
|
28
|
+
type ParentNotifications,
|
|
29
|
+
type RelationRow,
|
|
30
|
+
type SessionOriginKind,
|
|
31
|
+
type SessionRelation,
|
|
32
|
+
type SessionVisibility,
|
|
33
|
+
} from '@/server/lib/sessions/relation-types';
|
|
34
|
+
|
|
35
|
+
function requireId(value: string, field: string): string {
|
|
36
|
+
const trimmed = value.trim();
|
|
37
|
+
if (!trimmed) {
|
|
38
|
+
throw new SessionRelationError('invalid_argument', `${field} is required`);
|
|
39
|
+
}
|
|
40
|
+
return trimmed;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** One session's row, or null when it never had a relationship. */
|
|
44
|
+
export async function getSessionRelation(sessionId: string): Promise<SessionRelation | null> {
|
|
45
|
+
const db = await getDb();
|
|
46
|
+
const row = await db.get<RelationRow>(
|
|
47
|
+
'SELECT * FROM session_relations WHERE session_id = ?',
|
|
48
|
+
[sessionId],
|
|
49
|
+
);
|
|
50
|
+
return row ? fromRow(row) : null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Every relation — the sidebar attaches these in one read. */
|
|
54
|
+
export async function listSessionRelations(): Promise<SessionRelation[]> {
|
|
55
|
+
const db = await getDb();
|
|
56
|
+
const rows = await db.all<RelationRow>('SELECT * FROM session_relations');
|
|
57
|
+
return rows.map(fromRow);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Direct children, ordered by creation (the order the tree draws them). */
|
|
61
|
+
export async function listChildRelations(parentSessionId: string): Promise<SessionRelation[]> {
|
|
62
|
+
const db = await getDb();
|
|
63
|
+
const rows = await db.all<RelationRow>(
|
|
64
|
+
'SELECT * FROM session_relations WHERE parent_session_id = ? ORDER BY created_at ASC, session_id ASC',
|
|
65
|
+
[parentSessionId],
|
|
66
|
+
);
|
|
67
|
+
return rows.map(fromRow);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Forks of one source, in creation order. */
|
|
71
|
+
export async function listForkRelations(sourceSessionId: string): Promise<SessionRelation[]> {
|
|
72
|
+
const db = await getDb();
|
|
73
|
+
const rows = await db.all<RelationRow>(
|
|
74
|
+
"SELECT * FROM session_relations WHERE source_session_id = ? AND origin_kind = 'fork' ORDER BY created_at ASC, session_id ASC",
|
|
75
|
+
[sourceSessionId],
|
|
76
|
+
);
|
|
77
|
+
return rows.map(fromRow);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Sessions this one owns for archive/delete cleanup. */
|
|
81
|
+
export async function listOwnedRelations(ownerSessionId: string): Promise<SessionRelation[]> {
|
|
82
|
+
const db = await getDb();
|
|
83
|
+
const rows = await db.all<RelationRow>(
|
|
84
|
+
'SELECT * FROM session_relations WHERE lifecycle_owner_session_id = ? ORDER BY created_at ASC, session_id ASC',
|
|
85
|
+
[ownerSessionId],
|
|
86
|
+
);
|
|
87
|
+
return rows.map(fromRow);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Depth of a session in the parent tree; a session with no row is a root. */
|
|
91
|
+
export async function sessionDepth(sessionId: string): Promise<number> {
|
|
92
|
+
const relation = await getSessionRelation(sessionId);
|
|
93
|
+
return relation?.depth ?? 0;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The deepest descendant under a session, counted inclusively (itself = 1). */
|
|
97
|
+
async function subtreeHeight(sessionId: string, seen = new Set<string>()): Promise<number> {
|
|
98
|
+
if (seen.has(sessionId)) {
|
|
99
|
+
throw new SessionRelationError('cycle', 'Session tree contains a cycle');
|
|
100
|
+
}
|
|
101
|
+
seen.add(sessionId);
|
|
102
|
+
const children = await listChildRelations(sessionId);
|
|
103
|
+
let height = 1;
|
|
104
|
+
for (const child of children) {
|
|
105
|
+
height = Math.max(height, 1 + (await subtreeHeight(child.sessionId, seen)));
|
|
106
|
+
}
|
|
107
|
+
seen.delete(sessionId);
|
|
108
|
+
return height;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Walk up from a candidate parent; a hit on the child is a cycle. */
|
|
112
|
+
async function assertNoCycle(childSessionId: string, parentSessionId: string): Promise<void> {
|
|
113
|
+
const seen = new Set<string>();
|
|
114
|
+
let current: string | null = parentSessionId;
|
|
115
|
+
while (current !== null) {
|
|
116
|
+
if (current === childSessionId) {
|
|
117
|
+
throw new SessionRelationError('cycle', 'A session cannot be its own ancestor');
|
|
118
|
+
}
|
|
119
|
+
if (seen.has(current)) {
|
|
120
|
+
throw new SessionRelationError('cycle', 'Session tree contains a cycle');
|
|
121
|
+
}
|
|
122
|
+
seen.add(current);
|
|
123
|
+
const relation = await getSessionRelation(current);
|
|
124
|
+
current = relation?.parentSessionId ?? null;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface CreateSessionRelationInput {
|
|
129
|
+
sessionId: string;
|
|
130
|
+
parentSessionId?: string | null;
|
|
131
|
+
lifecycleOwnerSessionId?: string | null;
|
|
132
|
+
sourceSessionId?: string | null;
|
|
133
|
+
originKind?: SessionOriginKind | null;
|
|
134
|
+
visibility?: SessionVisibility;
|
|
135
|
+
parentNotifications?: ParentNotifications;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Insert the relation for a new session.
|
|
140
|
+
*
|
|
141
|
+
* A fork is a SOURCE edge, not a child: `origin_kind = 'fork'` requires a
|
|
142
|
+
* source and forbids a parent, because a source-derived session stays silent
|
|
143
|
+
* and is listed under "Forks", not under the parent's tree. A user can reparent
|
|
144
|
+
* it afterwards; the origin stays.
|
|
145
|
+
*/
|
|
146
|
+
export async function createSessionRelation(
|
|
147
|
+
input: CreateSessionRelationInput,
|
|
148
|
+
): Promise<SessionRelation> {
|
|
149
|
+
const sessionId = requireId(input.sessionId, 'sessionId');
|
|
150
|
+
const parentSessionId = input.parentSessionId
|
|
151
|
+
? requireId(input.parentSessionId, 'parentSessionId')
|
|
152
|
+
: null;
|
|
153
|
+
const lifecycleOwnerSessionId = input.lifecycleOwnerSessionId
|
|
154
|
+
? requireId(input.lifecycleOwnerSessionId, 'lifecycleOwnerSessionId')
|
|
155
|
+
: null;
|
|
156
|
+
const sourceSessionId = input.sourceSessionId
|
|
157
|
+
? requireId(input.sourceSessionId, 'sourceSessionId')
|
|
158
|
+
: null;
|
|
159
|
+
const originKind = input.originKind ?? null;
|
|
160
|
+
|
|
161
|
+
if (parentSessionId === sessionId) {
|
|
162
|
+
throw new SessionRelationError('self_parent', 'A session cannot parent itself');
|
|
163
|
+
}
|
|
164
|
+
if (lifecycleOwnerSessionId === sessionId) {
|
|
165
|
+
throw new SessionRelationError('invalid_argument', 'A session cannot own itself');
|
|
166
|
+
}
|
|
167
|
+
if (originKind === 'fork') {
|
|
168
|
+
if (!sourceSessionId) {
|
|
169
|
+
throw new SessionRelationError('fork_requires_source', 'A fork needs a source session');
|
|
170
|
+
}
|
|
171
|
+
if (parentSessionId !== null) {
|
|
172
|
+
throw new SessionRelationError(
|
|
173
|
+
'fork_is_not_a_child',
|
|
174
|
+
'A fork is recorded as a source edge, not a parent edge',
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
if (sourceSessionId === sessionId) {
|
|
178
|
+
throw new SessionRelationError('invalid_argument', 'A fork cannot source itself');
|
|
179
|
+
}
|
|
180
|
+
} else if (sourceSessionId !== null) {
|
|
181
|
+
throw new SessionRelationError('not_fork', 'A source session requires originKind "fork"');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const parentDepth = parentSessionId === null ? 0 : await sessionDepth(parentSessionId);
|
|
185
|
+
const depth =
|
|
186
|
+
originKind === 'fork' || parentSessionId === null ? 0 : parentDepth + 1;
|
|
187
|
+
if (depth > MAX_SESSION_DEPTH) {
|
|
188
|
+
throw new SessionRelationError(
|
|
189
|
+
'depth_exceeded',
|
|
190
|
+
`A session tree is limited to ${MAX_SESSION_DEPTH} levels`,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
if (parentSessionId !== null) await assertNoCycle(sessionId, parentSessionId);
|
|
194
|
+
|
|
195
|
+
const visibility = input.visibility ?? 'visible';
|
|
196
|
+
const parentNotifications = input.parentNotifications ?? 'auto';
|
|
197
|
+
const createdAt = Date.now();
|
|
198
|
+
const db = await getDb();
|
|
199
|
+
await db.run(
|
|
200
|
+
`INSERT INTO session_relations
|
|
201
|
+
(session_id, parent_session_id, lifecycle_owner_session_id, source_session_id,
|
|
202
|
+
origin_kind, visibility, parent_notifications, depth, created_at)
|
|
203
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
204
|
+
[
|
|
205
|
+
sessionId,
|
|
206
|
+
parentSessionId,
|
|
207
|
+
lifecycleOwnerSessionId,
|
|
208
|
+
sourceSessionId,
|
|
209
|
+
originKind,
|
|
210
|
+
visibility,
|
|
211
|
+
parentNotifications,
|
|
212
|
+
depth,
|
|
213
|
+
createdAt,
|
|
214
|
+
],
|
|
215
|
+
);
|
|
216
|
+
return {
|
|
217
|
+
sessionId,
|
|
218
|
+
parentSessionId,
|
|
219
|
+
lifecycleOwnerSessionId,
|
|
220
|
+
sourceSessionId,
|
|
221
|
+
originKind,
|
|
222
|
+
visibility,
|
|
223
|
+
parentNotifications,
|
|
224
|
+
depth,
|
|
225
|
+
createdAt,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Reparent a session, revalidating both directions: the new depth must fit,
|
|
231
|
+
* the new parent must not be a descendant, and a fork keeps its origin while
|
|
232
|
+
* gaining a tree position.
|
|
233
|
+
*/
|
|
234
|
+
export async function setSessionParent(
|
|
235
|
+
sessionId: string,
|
|
236
|
+
parentSessionId: string | null,
|
|
237
|
+
): Promise<SessionRelation> {
|
|
238
|
+
const child = await getSessionRelation(sessionId);
|
|
239
|
+
const newParent = parentSessionId
|
|
240
|
+
? requireId(parentSessionId, 'parentSessionId')
|
|
241
|
+
: null;
|
|
242
|
+
if (newParent === sessionId) {
|
|
243
|
+
throw new SessionRelationError('self_parent', 'A session cannot parent itself');
|
|
244
|
+
}
|
|
245
|
+
if (newParent !== null) {
|
|
246
|
+
await assertNoCycle(sessionId, newParent);
|
|
247
|
+
}
|
|
248
|
+
const newDepth = newParent === null ? 0 : (await sessionDepth(newParent)) + 1;
|
|
249
|
+
const height = await subtreeHeight(sessionId);
|
|
250
|
+
if (newDepth + height - 1 > MAX_SESSION_DEPTH) {
|
|
251
|
+
throw new SessionRelationError(
|
|
252
|
+
'depth_exceeded',
|
|
253
|
+
`A session tree is limited to ${MAX_SESSION_DEPTH} levels`,
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const db = await getDb();
|
|
258
|
+
if (child === null) {
|
|
259
|
+
return createSessionRelation({ sessionId, parentSessionId: newParent });
|
|
260
|
+
}
|
|
261
|
+
await db.run(
|
|
262
|
+
'UPDATE session_relations SET parent_session_id = ?, depth = ? WHERE session_id = ?',
|
|
263
|
+
[newParent, newDepth, sessionId],
|
|
264
|
+
);
|
|
265
|
+
// Every descendant moves with the branch; recompute rather than trusting a
|
|
266
|
+
// relative offset, so a corrupted depth cannot survive a reparent.
|
|
267
|
+
await refreshSubtreeDepth(sessionId, newDepth);
|
|
268
|
+
return {
|
|
269
|
+
...child,
|
|
270
|
+
parentSessionId: newParent,
|
|
271
|
+
depth: newDepth,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async function refreshSubtreeDepth(sessionId: string, depth: number): Promise<void> {
|
|
276
|
+
const db = await getDb();
|
|
277
|
+
for (const child of await listChildRelations(sessionId)) {
|
|
278
|
+
const childDepth = depth + 1;
|
|
279
|
+
await db.run('UPDATE session_relations SET depth = ? WHERE session_id = ?', [
|
|
280
|
+
childDepth,
|
|
281
|
+
child.sessionId,
|
|
282
|
+
]);
|
|
283
|
+
await refreshSubtreeDepth(child.sessionId, childDepth);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Set the presentation flag; delivery is controlled separately. */
|
|
288
|
+
export async function setSessionVisibility(
|
|
289
|
+
sessionId: string,
|
|
290
|
+
visibility: SessionVisibility,
|
|
291
|
+
): Promise<void> {
|
|
292
|
+
const db = await getDb();
|
|
293
|
+
await db.run('UPDATE session_relations SET visibility = ? WHERE session_id = ?', [
|
|
294
|
+
visibility,
|
|
295
|
+
sessionId,
|
|
296
|
+
]);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Which child outcomes wake the parent. */
|
|
300
|
+
export async function setParentNotifications(
|
|
301
|
+
sessionId: string,
|
|
302
|
+
value: ParentNotifications,
|
|
303
|
+
): Promise<void> {
|
|
304
|
+
const db = await getDb();
|
|
305
|
+
await db.run('UPDATE session_relations SET parent_notifications = ? WHERE session_id = ?', [
|
|
306
|
+
value,
|
|
307
|
+
sessionId,
|
|
308
|
+
]);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Drop a session's own relation and every incoming edge.
|
|
313
|
+
*
|
|
314
|
+
* A source edge is cleared rather than deleted: deleting a fork's source must
|
|
315
|
+
* not delete the fork's history relationship row, and deleting a fork must not
|
|
316
|
+
* touch its source. Parent and owner edges are nulled because the session they
|
|
317
|
+
* named is gone; the cascade that deletes dependents runs before this.
|
|
318
|
+
*/
|
|
319
|
+
export async function deleteSessionRelations(sessionId: string): Promise<void> {
|
|
320
|
+
const db = await getDb();
|
|
321
|
+
db.raw.transaction(() => {
|
|
322
|
+
db.raw
|
|
323
|
+
.query('UPDATE session_relations SET source_session_id = NULL, origin_kind = NULL WHERE source_session_id = ?')
|
|
324
|
+
.run(sessionId);
|
|
325
|
+
db.raw
|
|
326
|
+
.query('UPDATE session_relations SET parent_session_id = NULL, depth = 0 WHERE parent_session_id = ?')
|
|
327
|
+
.run(sessionId);
|
|
328
|
+
db.raw
|
|
329
|
+
.query('UPDATE session_relations SET lifecycle_owner_session_id = NULL WHERE lifecycle_owner_session_id = ?')
|
|
330
|
+
.run(sessionId);
|
|
331
|
+
db.raw.query('DELETE FROM session_relations WHERE session_id = ?').run(sessionId);
|
|
332
|
+
db.raw.query('DELETE FROM session_notices WHERE parent_session_id = ?').run(sessionId);
|
|
333
|
+
db.raw.query('DELETE FROM session_agent_tokens WHERE session_id = ?').run(sessionId);
|
|
334
|
+
db.raw.query('DELETE FROM session_answers WHERE session_id = ?').run(sessionId);
|
|
335
|
+
})();
|
|
336
|
+
forgetAgentTokenCache(sessionId);
|
|
337
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* What a session id means, independent of transport.
|
|
8
|
+
*
|
|
9
|
+
* A session may be live in this process, recorded in `harness_sessions` (a chat
|
|
10
|
+
* this process has closed), or an omp transcript on disk (a chat this process
|
|
11
|
+
* never saw). Spawning a child, notifying a parent and forking all need the same
|
|
12
|
+
* three answers — harness, cwd, agent id — and resolving them in three modules
|
|
13
|
+
* is how the sidebar and the routes drift apart.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { getLiveSession, harnessOfSession } from '@/server/lib/omp/rpc/session-registry';
|
|
17
|
+
import { findSessionFileById } from '@/server/lib/omp/session/locator';
|
|
18
|
+
import { readRawHeaderLine } from '@/server/lib/omp/session/files';
|
|
19
|
+
import { knownHarness, loadHarnessSession } from '@/server/lib/harness/session-store.server';
|
|
20
|
+
import { canonicalPath } from '@/server/lib/fs/canonical';
|
|
21
|
+
import { resolveProjectRoot } from '@/server/lib/omp/core/worktree';
|
|
22
|
+
import { getDb } from '@/server/db.server';
|
|
23
|
+
import { HARNESS_IDS, type HarnessId } from '@/shared/lib/harness/catalog';
|
|
24
|
+
|
|
25
|
+
export interface SessionRef {
|
|
26
|
+
sessionId: string;
|
|
27
|
+
/** Null when nothing in this build can name the transport. */
|
|
28
|
+
harness: HarnessId | null;
|
|
29
|
+
cwd: string | null;
|
|
30
|
+
/** The agent's own id, for `session/load`; empty for omp. */
|
|
31
|
+
agentSessionId: string;
|
|
32
|
+
live: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Resolve harness, cwd and agent id for one session id. */
|
|
36
|
+
export async function resolveSessionRef(sessionId: string): Promise<SessionRef> {
|
|
37
|
+
const live = getLiveSession(sessionId);
|
|
38
|
+
if (live?.isAlive()) {
|
|
39
|
+
const harness = harnessOfSession(live);
|
|
40
|
+
return {
|
|
41
|
+
sessionId,
|
|
42
|
+
harness,
|
|
43
|
+
cwd: live.cwd || null,
|
|
44
|
+
agentSessionId: harness === 'omp-rpc' ? '' : live.sessionId,
|
|
45
|
+
live: true,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const record = await loadHarnessSession(sessionId);
|
|
50
|
+
const filePath = await findSessionFileById(sessionId);
|
|
51
|
+
const header = filePath ? await readRawHeaderLine(filePath) : null;
|
|
52
|
+
const headerCwd = typeof header?.cwd === 'string' ? header.cwd : null;
|
|
53
|
+
const recordedHarness = knownHarness(record?.harness, HARNESS_IDS);
|
|
54
|
+
return {
|
|
55
|
+
sessionId,
|
|
56
|
+
harness: recordedHarness ?? (filePath ? 'omp-rpc' : null),
|
|
57
|
+
cwd: record?.cwd || headerCwd || null,
|
|
58
|
+
agentSessionId: record?.agentSessionId || '',
|
|
59
|
+
live: false,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** True when a session has a transcript or a harness record. */
|
|
64
|
+
export async function sessionExists(sessionId: string): Promise<boolean> {
|
|
65
|
+
if (getLiveSession(sessionId)?.isAlive()) return true;
|
|
66
|
+
if (await loadHarnessSession(sessionId)) return true;
|
|
67
|
+
return (await findSessionFileById(sessionId)) !== undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** True when the sidebar has archived the session. */
|
|
71
|
+
export async function isSessionArchived(sessionId: string): Promise<boolean> {
|
|
72
|
+
const db = await getDb();
|
|
73
|
+
const row = await db.get('SELECT 1 AS present FROM archived_sessions WHERE session_id = ?', [
|
|
74
|
+
sessionId,
|
|
75
|
+
]);
|
|
76
|
+
return row != null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The workspace folder a cwd belongs to, or null.
|
|
81
|
+
*
|
|
82
|
+
* Same resolution the sidebar uses: the cwd's project root, canonicalised,
|
|
83
|
+
* compared against each folder's registered path. A cwd outside every folder
|
|
84
|
+
* answers null rather than being filed under the first one.
|
|
85
|
+
*/
|
|
86
|
+
export async function folderIdForCwd(cwd: string | null): Promise<number | null> {
|
|
87
|
+
if (!cwd) return null;
|
|
88
|
+
const root = await resolveProjectRoot(cwd).catch(() => '');
|
|
89
|
+
if (!root) return null;
|
|
90
|
+
const rootKey = await canonicalPath(root);
|
|
91
|
+
const db = await getDb();
|
|
92
|
+
const folders = await db.all<{ id: number; project_path: string | null }>(
|
|
93
|
+
'SELECT id, project_path FROM workspace_folders',
|
|
94
|
+
);
|
|
95
|
+
for (const folder of folders) {
|
|
96
|
+
if (!folder.project_path) continue;
|
|
97
|
+
if ((await canonicalPath(folder.project_path)) === rootKey) return folder.id;
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The folder a session is filed under, or null when it has no cwd yet. */
|
|
103
|
+
export async function folderIdForSession(sessionId: string): Promise<number | null> {
|
|
104
|
+
const ref = await resolveSessionRef(sessionId);
|
|
105
|
+
return folderIdForCwd(ref.cwd);
|
|
106
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The three tables that make a session part of a tree.
|
|
8
|
+
*
|
|
9
|
+
* A session has no canonical row: an omp chat is a JSONL file and an ACP chat
|
|
10
|
+
* is owned by the agent (`harness_sessions` records only who ran it). So the
|
|
11
|
+
* relationships live here, keyed by the id the sidebar and the URL use, and
|
|
12
|
+
* they survive a restart, a closed tab and a second Kabmo instance.
|
|
13
|
+
*
|
|
14
|
+
* `session_relations` carries three independent edges and no more:
|
|
15
|
+
*
|
|
16
|
+
* parent_session_id the sidebar tree and the outcome notification
|
|
17
|
+
* lifecycle_owner_session_id archive/delete ownership (may cross projects)
|
|
18
|
+
* source_session_id a fork's origin, with `origin_kind = 'fork'`
|
|
19
|
+
*
|
|
20
|
+
* `session_notices` is the coalescing buffer between a finishing child and the
|
|
21
|
+
* parent's queue; a row exists only inside the window and becomes one
|
|
22
|
+
* `queued_messages` row when it closes. It is deliberately NOT a row in the
|
|
23
|
+
* queue: the queue is the user's editable follow-up list and a system notice
|
|
24
|
+
* has no business being reordered or edited.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import type { DbClient } from '@/server/lib/db/client';
|
|
28
|
+
|
|
29
|
+
const SESSION_RELATIONS_SQL = `
|
|
30
|
+
CREATE TABLE IF NOT EXISTS session_relations (
|
|
31
|
+
session_id TEXT PRIMARY KEY,
|
|
32
|
+
parent_session_id TEXT,
|
|
33
|
+
lifecycle_owner_session_id TEXT,
|
|
34
|
+
source_session_id TEXT,
|
|
35
|
+
origin_kind TEXT,
|
|
36
|
+
visibility TEXT NOT NULL DEFAULT 'visible',
|
|
37
|
+
parent_notifications TEXT NOT NULL DEFAULT 'auto',
|
|
38
|
+
depth INTEGER NOT NULL DEFAULT 0,
|
|
39
|
+
created_at INTEGER NOT NULL
|
|
40
|
+
);
|
|
41
|
+
CREATE INDEX IF NOT EXISTS idx_session_relations_parent
|
|
42
|
+
ON session_relations (parent_session_id);
|
|
43
|
+
CREATE INDEX IF NOT EXISTS idx_session_relations_owner
|
|
44
|
+
ON session_relations (lifecycle_owner_session_id);
|
|
45
|
+
CREATE INDEX IF NOT EXISTS idx_session_relations_source
|
|
46
|
+
ON session_relations (source_session_id, origin_kind);
|
|
47
|
+
|
|
48
|
+
CREATE TABLE IF NOT EXISTS session_notices (
|
|
49
|
+
id TEXT PRIMARY KEY,
|
|
50
|
+
parent_session_id TEXT NOT NULL,
|
|
51
|
+
coalesce_key TEXT NOT NULL,
|
|
52
|
+
state TEXT NOT NULL,
|
|
53
|
+
send_at INTEGER NOT NULL,
|
|
54
|
+
created_at INTEGER NOT NULL,
|
|
55
|
+
updated_at INTEGER NOT NULL
|
|
56
|
+
);
|
|
57
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_session_notices_window
|
|
58
|
+
ON session_notices (parent_session_id, coalesce_key);
|
|
59
|
+
|
|
60
|
+
CREATE TABLE IF NOT EXISTS session_agent_tokens (
|
|
61
|
+
session_id TEXT PRIMARY KEY,
|
|
62
|
+
token_hash TEXT NOT NULL,
|
|
63
|
+
created_at INTEGER NOT NULL,
|
|
64
|
+
revoked_at INTEGER
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
-- The last assistant text an ACP turn closed. ACP history is agent-owned:
|
|
68
|
+
-- there is no transcript for /api/chat/:id to read after the process dies,
|
|
69
|
+
-- so the parent notice and 'kabmo session output' would otherwise lose the
|
|
70
|
+
-- answer. omp does not need this row (its JSONL is the record), but writing
|
|
71
|
+
-- both keeps the read path one rule.
|
|
72
|
+
CREATE TABLE IF NOT EXISTS session_answers (
|
|
73
|
+
session_id TEXT PRIMARY KEY,
|
|
74
|
+
answer TEXT NOT NULL,
|
|
75
|
+
updated_at INTEGER NOT NULL
|
|
76
|
+
);
|
|
77
|
+
`;
|
|
78
|
+
|
|
79
|
+
/** Idempotent: created on every database open, like every other feature table. */
|
|
80
|
+
export async function ensureSessionRelationsSchema(db: DbClient): Promise<void> {
|
|
81
|
+
await db.exec(SESSION_RELATIONS_SQL);
|
|
82
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
7
|
+
import { isolateDb, releaseDb } from '@/test-support/isolated-db';
|
|
8
|
+
import { getDb } from '@/server/db.server';
|
|
9
|
+
import { ensureSessionRelationsSchema } from '@/server/lib/sessions/schema.server';
|
|
10
|
+
import { createSessionRelation } from '@/server/lib/sessions/relations.server';
|
|
11
|
+
import { decorateSidebarSessions } from '@/server/lib/sessions/sidebar-relations.server';
|
|
12
|
+
import type { SessionItemData } from '@/shared/types';
|
|
13
|
+
|
|
14
|
+
function row(id: string): SessionItemData {
|
|
15
|
+
return { id, folder_id: 1, title: id };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
beforeEach(async () => {
|
|
19
|
+
await isolateDb();
|
|
20
|
+
await ensureSessionRelationsSchema(await getDb());
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(async () => {
|
|
24
|
+
await releaseDb();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('decorateSidebarSessions', () => {
|
|
28
|
+
test('adds the parent edge and direct child count', async () => {
|
|
29
|
+
await createSessionRelation({ sessionId: 'parent' });
|
|
30
|
+
await createSessionRelation({ sessionId: 'c1', parentSessionId: 'parent' });
|
|
31
|
+
await createSessionRelation({ sessionId: 'c2', parentSessionId: 'parent' });
|
|
32
|
+
await createSessionRelation({
|
|
33
|
+
sessionId: 'fork-1',
|
|
34
|
+
sourceSessionId: 'parent',
|
|
35
|
+
originKind: 'fork',
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const [parent, c1, c2, fork] = await decorateSidebarSessions([
|
|
39
|
+
row('parent'),
|
|
40
|
+
row('c1'),
|
|
41
|
+
row('c2'),
|
|
42
|
+
row('fork-1'),
|
|
43
|
+
]);
|
|
44
|
+
expect(parent).toMatchObject({ childSessionCount: 2 });
|
|
45
|
+
expect(parent?.parentSessionId).toBeUndefined();
|
|
46
|
+
expect(c1).toMatchObject({ parentSessionId: 'parent', childSessionCount: 0 });
|
|
47
|
+
expect(c2).toMatchObject({ parentSessionId: 'parent', childSessionCount: 0 });
|
|
48
|
+
expect(fork).toMatchObject({ childSessionCount: 0 });
|
|
49
|
+
expect(fork?.parentSessionId).toBeUndefined();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('a hidden session is not drawn, but a visible one with no relation is', async () => {
|
|
53
|
+
await createSessionRelation({
|
|
54
|
+
sessionId: 'worker',
|
|
55
|
+
parentSessionId: 'parent',
|
|
56
|
+
visibility: 'hidden',
|
|
57
|
+
});
|
|
58
|
+
const rows = await decorateSidebarSessions([row('worker'), row('plain')]);
|
|
59
|
+
expect(rows.map((entry) => entry.id)).toEqual(['plain']);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Sidebar rows, decorated with their tree fields.
|
|
8
|
+
*
|
|
9
|
+
* One relation read for the whole folder: `parentSessionId` lets the client
|
|
10
|
+
* build the tree, `childSessionCount` decides whether a row draws a chevron,
|
|
11
|
+
* and `visibility: hidden` keeps a background worker out of the list while it
|
|
12
|
+
* stays addressable by id (and still notifies its parent).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { SessionItemData } from '@/shared/types';
|
|
16
|
+
import { listSessionRelations } from '@/server/lib/sessions/relations.server';
|
|
17
|
+
|
|
18
|
+
export async function decorateSidebarSessions(
|
|
19
|
+
sessions: readonly SessionItemData[],
|
|
20
|
+
): Promise<SessionItemData[]> {
|
|
21
|
+
let relations: Awaited<ReturnType<typeof listSessionRelations>> = [];
|
|
22
|
+
try {
|
|
23
|
+
relations = await listSessionRelations();
|
|
24
|
+
} catch {
|
|
25
|
+
// A database opened before this feature's tables existed: no tree, and
|
|
26
|
+
// never an error page over the whole sidebar.
|
|
27
|
+
}
|
|
28
|
+
const relationBySession = new Map(relations.map((relation) => [relation.sessionId, relation]));
|
|
29
|
+
const childCountByParent = new Map<string, number>();
|
|
30
|
+
for (const relation of relations) {
|
|
31
|
+
if (!relation.parentSessionId) continue;
|
|
32
|
+
childCountByParent.set(
|
|
33
|
+
relation.parentSessionId,
|
|
34
|
+
(childCountByParent.get(relation.parentSessionId) ?? 0) + 1,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
return sessions
|
|
38
|
+
.filter((session) => relationBySession.get(String(session.id))?.visibility !== 'hidden')
|
|
39
|
+
.map((session) => {
|
|
40
|
+
const relation = relationBySession.get(String(session.id));
|
|
41
|
+
return {
|
|
42
|
+
...session,
|
|
43
|
+
...(relation?.parentSessionId ? { parentSessionId: relation.parentSessionId } : {}),
|
|
44
|
+
childSessionCount: childCountByParent.get(String(session.id)) ?? 0,
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
}
|