@kabmo-labs/kabmo 0.23.0 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/index.html +1 -1
- package/dist/client/index.js +805 -736
- package/dist/client/static/js/index-bc9aa4mx.css +1 -0
- package/dist/client/static/js/{index-ph0q59rs.js → index-ga8cbpt4.js} +1071 -1171
- package/package.json +2 -4
- package/src/cli/lib/args.js +24 -1
- package/src/cli/lib/args.test.js +16 -0
- package/src/cli/lib/commands/session.js +233 -0
- package/src/cli/lib/commands/session.test.js +103 -0
- package/src/cli/main.js +1 -1
- package/src/client/components/common/ComposerPicker.tsx +3 -0
- package/src/client/components/common/Modal.tsx +2 -6
- package/src/client/components/common/code-surface/shiki.test.ts +82 -0
- package/src/client/components/common/code-surface/shiki.ts +28 -7
- package/src/client/components/common/session-actions-menu/NewChildSessionDialog.tsx +113 -0
- package/src/client/components/common/session-actions-menu/index.tsx +33 -2
- package/src/client/components/common/session-row/StatusSlot.tsx +4 -4
- package/src/client/components/common/session-row/index.tsx +27 -1
- package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +7 -1
- package/src/client/components/layout/desktop-layout/resizer/separator-drag.ts +3 -3
- package/src/client/components/layout/session-sidebar/CategoryItem.tsx +25 -12
- package/src/client/components/layout/session-sidebar/ChildSessionList.tsx +66 -0
- package/src/client/components/layout/session-sidebar/SessionItem.tsx +28 -3
- package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.test.tsx +20 -0
- package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.tsx +59 -12
- package/src/client/components/layout/session-sidebar/about-modal/HarnessTableLog.test.tsx +94 -0
- package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.test.tsx +130 -0
- package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.tsx +120 -0
- package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.tsx +47 -0
- package/src/client/components/layout/session-sidebar/about-modal/index.test.tsx +122 -0
- package/src/client/components/layout/session-sidebar/about-modal/index.tsx +55 -46
- package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +18 -5
- package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +21 -4
- package/src/client/components/mobile/mobile-session-sidebar/SessionRow.tsx +27 -5
- package/src/client/components/settings/categories/harness-settings/EnvProviderForm.tsx +31 -0
- package/src/client/components/settings/categories/harness-settings/provider-panel.test.tsx +49 -0
- package/src/client/components/workspace/chat-timeline/ChatOverlays.tsx +5 -2
- package/src/client/components/workspace/chat-timeline/ComposerDock.tsx +64 -70
- package/src/client/components/workspace/chat-timeline/ComposerDockFrame.tsx +35 -0
- package/src/client/components/workspace/chat-timeline/EmptyWorkspacePrompt.tsx +135 -118
- package/src/client/components/workspace/chat-timeline/MessageItem.tsx +22 -1
- package/src/client/components/workspace/chat-timeline/MessageList.test.ts +14 -0
- package/src/client/components/workspace/chat-timeline/MessageList.tsx +4 -0
- package/src/client/components/workspace/chat-timeline/TimelineBody.tsx +3 -0
- package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +4 -3
- package/src/client/components/workspace/chat-timeline/composer-dock-frame.test.tsx +155 -0
- package/src/client/components/workspace/chat-timeline/index.tsx +5 -2
- package/src/client/components/workspace/chat-timeline/pending-harness.test.tsx +1 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/FrameControl.tsx +11 -20
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/GroupedAsk.tsx +1 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/QuestionBlock.tsx +1 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/drafts.ts +1 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/index.tsx +2 -1
- package/src/client/components/workspace/file-explorer/index.test.ts +25 -0
- package/src/client/components/workspace/file-explorer/index.tsx +9 -3
- package/src/client/components/workspace/model-dropdown/harness-models.test.ts +17 -0
- package/src/client/components/workspace/model-dropdown/harness-models.ts +33 -18
- package/src/client/components/workspace/model-dropdown/index.tsx +3 -3
- package/src/client/components/workspace/model-dropdown/popover-metrics.ts +4 -1
- package/src/client/components/workspace/terminal-panel/KeyBar.tsx +10 -8
- package/src/client/components/workspace/terminal-panel/{RealtimeXtermView.tsx → RealtimeTerminalView.tsx} +51 -58
- package/src/client/components/workspace/terminal-panel/fit.test.ts +37 -0
- package/src/client/components/workspace/terminal-panel/fit.ts +64 -0
- package/src/client/components/workspace/terminal-panel/fonts.test.ts +161 -0
- package/src/client/components/workspace/terminal-panel/fonts.ts +164 -0
- package/src/client/components/workspace/terminal-panel/grid.test.ts +52 -0
- package/src/client/components/workspace/terminal-panel/grid.ts +118 -0
- package/src/client/components/workspace/terminal-panel/index.tsx +25 -21
- package/src/client/components/workspace/terminal-panel/mount.ts +247 -223
- package/src/client/components/workspace/terminal-panel/navigation.test.ts +54 -0
- package/src/client/components/workspace/terminal-panel/navigation.ts +47 -0
- package/src/client/data/settings/omp-schema.json +5 -5
- package/src/client/data/theme/terminal.test.ts +25 -0
- package/src/client/data/theme/terminal.ts +96 -79
- package/src/client/hooks/chat/composer/items.test.ts +12 -26
- package/src/client/hooks/chat/composer/pick-mapping.test.ts +42 -0
- package/src/client/hooks/chat/omp/prompt-send.test.ts +7 -7
- package/src/client/hooks/chat/omp/prompt-send.ts +26 -14
- package/src/client/hooks/chat/timeline/actions.ts +2 -2
- package/src/client/hooks/chat/timeline/ask-frames.ts +5 -4
- package/src/client/hooks/chat/timeline/new-chat-open.test.ts +86 -0
- package/src/client/hooks/chat/timeline/new-chat-open.ts +23 -5
- package/src/client/hooks/chat/timeline/new-chat.test.ts +32 -4
- package/src/client/hooks/chat/timeline/new-chat.ts +18 -6
- package/src/client/hooks/chat/timeline/prompt-text.test.ts +34 -1
- package/src/client/hooks/chat/timeline/prompt-text.ts +47 -20
- package/src/client/hooks/chat/timeline/rewind-actions.test.ts +24 -1
- package/src/client/hooks/chat/timeline/rewind-actions.ts +15 -11
- package/src/client/hooks/chat/timeline/run-identity.test.ts +52 -0
- package/src/client/hooks/chat/timeline/run-identity.ts +23 -11
- package/src/client/hooks/chat/timeline/send-execute.test.ts +56 -0
- package/src/client/hooks/chat/timeline/send-helpers.ts +30 -1
- package/src/client/hooks/chat/timeline/send-props.ts +5 -3
- package/src/client/hooks/chat/timeline/send-steer.test.ts +4 -4
- package/src/client/hooks/chat/timeline/send.test-util.ts +8 -2
- package/src/client/hooks/chat/timeline/send.test.ts +4 -4
- package/src/client/hooks/chat/timeline/send.ts +59 -53
- package/src/client/hooks/chat/timeline/session-load.test-util.ts +6 -2
- package/src/client/hooks/chat/timeline/session-load.test.ts +31 -0
- package/src/client/hooks/chat/timeline/session-load.ts +6 -14
- package/src/client/hooks/chat/timeline/session-shapes.ts +32 -0
- package/src/client/hooks/chat/timeline/use-fork-session.ts +49 -0
- package/src/client/hooks/models/harness-catalog.ts +114 -0
- package/src/client/hooks/models/provider-writes.test.ts +81 -0
- package/src/client/hooks/models/provider-writes.ts +10 -3
- package/src/client/hooks/models/use-harness-update.test.ts +193 -0
- package/src/client/hooks/models/use-harness-update.ts +202 -0
- package/src/client/hooks/models/use-harnesses-controller.ts +57 -0
- package/src/client/hooks/models/use-harnesses-signal.test.ts +128 -0
- package/src/client/hooks/models/use-harnesses.test.ts +2 -1
- package/src/client/hooks/models/use-harnesses.ts +48 -114
- package/src/client/hooks/workspace/child-expansion.ts +33 -0
- package/src/client/hooks/workspace/terminal/index.ts +17 -3
- package/src/client/hooks/workspace/terminal/socket.ts +9 -4
- package/src/client/lib/session-tree.test.ts +50 -0
- package/src/client/lib/session-tree.ts +59 -0
- package/src/client/tailwind.css +26 -12
- package/src/server/index.ts +2 -0
- package/src/server/lib/auth/guard.ts +10 -0
- package/src/server/lib/btw/session-copy.server.ts +6 -44
- package/src/server/lib/btw/session-copy.test.ts +2 -1
- package/src/server/lib/db/schema.ts +4 -0
- package/src/server/lib/db/seed-terminal.test.ts +1 -1
- package/src/server/lib/db/seed.test.ts +1 -1
- package/src/server/lib/harness/acp/activation.test.ts +81 -0
- package/src/server/lib/harness/acp/activation.ts +72 -0
- package/src/server/lib/harness/acp/background-work.test.ts +262 -0
- package/src/server/lib/harness/acp/background-work.ts +320 -0
- package/src/server/lib/harness/acp/client.ts +2 -2
- package/src/server/lib/harness/acp/frames.test.ts +50 -1
- package/src/server/lib/harness/acp/frames.ts +17 -5
- package/src/server/lib/harness/acp/gateway-env.test.ts +64 -0
- package/src/server/lib/harness/acp/gateway-env.ts +70 -0
- package/src/server/lib/harness/acp/mapper.ts +6 -4
- package/src/server/lib/harness/acp/prompt.test.ts +20 -0
- package/src/server/lib/harness/acp/prompt.ts +19 -3
- package/src/server/lib/harness/acp/session-background.test.ts +129 -0
- package/src/server/lib/harness/acp/session-config.test.ts +73 -0
- package/src/server/lib/harness/acp/session-config.ts +87 -6
- package/src/server/lib/harness/acp/session-exit.ts +31 -0
- package/src/server/lib/harness/acp/session-handshake.test.ts +108 -0
- package/src/server/lib/harness/acp/session-handshake.ts +36 -3
- package/src/server/lib/harness/acp/session-idle.ts +51 -0
- package/src/server/lib/harness/acp/session-listeners.ts +31 -0
- package/src/server/lib/harness/acp/session-notifications.ts +19 -3
- package/src/server/lib/harness/acp/session-observers.test.ts +9 -0
- package/src/server/lib/harness/acp/session-options.ts +33 -0
- package/src/server/lib/harness/acp/session-reaper.test.ts +96 -0
- package/src/server/lib/harness/acp/session-requests.ts +47 -0
- package/src/server/lib/harness/acp/session-turns.ts +20 -5
- package/src/server/lib/harness/acp/session.test-util.ts +54 -3
- package/src/server/lib/harness/acp/session.test.ts +65 -3
- package/src/server/lib/harness/acp/session.ts +79 -79
- package/src/server/lib/harness/acp/turn.test.ts +81 -0
- package/src/server/lib/harness/acp/turn.ts +54 -13
- package/src/server/lib/harness/acp/verbs.ts +12 -2
- package/src/server/lib/harness/auto-probe.server.test.ts +121 -0
- package/src/server/lib/harness/auto-probe.server.ts +178 -0
- package/src/server/lib/harness/config.server.test.ts +1 -1
- package/src/server/lib/harness/config.server.ts +1 -1
- package/src/server/lib/harness/launch-table.ts +195 -0
- package/src/server/lib/harness/launch.test.ts +37 -0
- package/src/server/lib/harness/launch.ts +16 -116
- package/src/server/lib/harness/provider-entry.ts +32 -0
- package/src/server/lib/harness/provider-file.server.ts +3 -0
- package/src/server/lib/harness/provider-sources.server.test.ts +45 -0
- package/src/server/lib/harness/provider-sources.server.ts +26 -2
- package/src/server/lib/harness/provider-write.server.ts +21 -32
- package/src/server/lib/harness/provider-zcode.server.test.ts +270 -0
- package/src/server/lib/harness/provider-zcode.server.ts +324 -0
- package/src/server/lib/harness/spawn.server.ts +15 -2
- package/src/server/lib/harness/update-job-store.test.ts +126 -0
- package/src/server/lib/harness/update-job-store.ts +109 -0
- package/src/server/lib/harness/update-job.server.ts +203 -0
- package/src/server/lib/harness/update-state.server.ts +15 -20
- package/src/server/lib/harness/update-worker.test.ts +153 -0
- package/src/server/lib/harness/update-worker.ts +197 -0
- package/src/server/lib/lifecycle/after-listen.ts +7 -0
- package/src/server/lib/lifecycle/paths.ts +23 -0
- package/src/server/lib/models/registry-refresh.server.test.ts +87 -0
- package/src/server/lib/models/registry-refresh.server.ts +98 -0
- package/src/server/lib/omp/rpc/child-config.ts +5 -4
- package/src/server/lib/omp/rpc/constants.test.ts +2 -1
- package/src/server/lib/omp/rpc/forked-resume.test.ts +14 -3
- package/src/server/lib/omp/rpc/manager.ts +5 -5
- package/src/server/lib/omp/rpc/prewarm-pool.ts +12 -3
- package/src/server/lib/omp/rpc/session-registry.ts +14 -2
- package/src/server/lib/omp/rpc/terminal-settle.ts +53 -5
- package/src/server/lib/omp/session/delete-flow.server.ts +62 -8
- package/src/server/lib/omp/session/delete.server.ts +17 -0
- package/src/server/lib/omp/session/rewind-file.ts +39 -21
- package/src/server/lib/omp/session/sidebar-data.server.ts +6 -6
- package/src/server/lib/omp/session/transcript-copy.ts +123 -0
- package/src/server/lib/sessions/agent-token.server.test.ts +64 -0
- package/src/server/lib/sessions/agent-token.server.ts +134 -0
- package/src/server/lib/sessions/answers.server.test.ts +33 -0
- package/src/server/lib/sessions/answers.server.ts +37 -0
- package/src/server/lib/sessions/dispatch.server.test.ts +143 -0
- package/src/server/lib/sessions/dispatch.server.ts +128 -0
- package/src/server/lib/sessions/fork.server.test.ts +209 -0
- package/src/server/lib/sessions/fork.server.ts +304 -0
- package/src/server/lib/sessions/lifecycle.server.test.ts +75 -0
- package/src/server/lib/sessions/lifecycle.server.ts +71 -0
- package/src/server/lib/sessions/notify.server.test.ts +112 -0
- package/src/server/lib/sessions/notify.server.ts +212 -0
- package/src/server/lib/sessions/relation-types.ts +76 -0
- package/src/server/lib/sessions/relations.server.test.ts +207 -0
- package/src/server/lib/sessions/relations.server.ts +337 -0
- package/src/server/lib/sessions/resolve.server.ts +106 -0
- package/src/server/lib/sessions/schema.server.ts +82 -0
- package/src/server/lib/sessions/sidebar-relations.server.test.ts +61 -0
- package/src/server/lib/sessions/sidebar-relations.server.ts +47 -0
- package/src/server/lib/sessions/spawn.server.test.ts +196 -0
- package/src/server/lib/sessions/spawn.server.ts +310 -0
- package/src/server/lib/terminal/attach-replay.test.ts +2 -2
- package/src/server/lib/terminal/output.test.ts +2 -2
- package/src/server/lib/terminal/output.ts +1 -1
- package/src/server/lib/terminal/resize.test.ts +99 -0
- package/src/server/lib/terminal/runtime.server.ts +26 -6
- package/src/server/lib/terminal/send-result.test.ts +18 -1
- package/src/server/lib/terminal/send-result.ts +26 -2
- package/src/server/lib/terminal/session.server.ts +6 -6
- package/src/server/lib/usage/new-api.test.ts +92 -0
- package/src/server/lib/usage/new-api.ts +131 -0
- package/src/server/lib/usage/provider-endpoint.test.ts +106 -0
- package/src/server/lib/usage/provider-key.ts +33 -0
- package/src/server/lib/usage/providers.server.ts +10 -3
- package/src/server/lib/usage/vendor-balance.server.ts +4 -53
- package/src/server/lib/usage/vendor-http.ts +41 -0
- package/src/server/lib/usage/window.ts +55 -0
- package/src/server/routes/agent/command.ts +14 -1
- package/src/server/routes/agent/harness-command.ts +6 -0
- package/src/server/routes/agent/new.ts +15 -0
- package/src/server/routes/chat/rewind.test.ts +147 -0
- package/src/server/routes/chat/rewind.ts +29 -7
- package/src/server/routes/chat/session.test.ts +165 -10
- package/src/server/routes/chat/session.ts +60 -7
- package/src/server/routes/harness/add-provider.test.ts +9 -1
- package/src/server/routes/harness/import-action.ts +22 -4
- package/src/server/routes/harness/import.test.ts +1 -1
- package/src/server/routes/harness/index.ts +5 -1
- package/src/server/routes/harness/root.ts +8 -49
- package/src/server/routes/harness/toggle.test.ts +20 -0
- package/src/server/routes/harness/update-status.ts +26 -0
- package/src/server/routes/harness/update.test.ts +119 -0
- package/src/server/routes/harness/update.ts +67 -113
- package/src/server/routes/harness/versions.ts +5 -1
- package/src/server/routes/sessions/answer.ts +24 -0
- package/src/server/routes/sessions/batch-delete.ts +11 -5
- package/src/server/routes/sessions/delete-acp.test.ts +227 -0
- package/src/server/routes/sessions/delete.ts +4 -2
- package/src/server/routes/sessions/fork.ts +74 -0
- package/src/server/routes/sessions/index.ts +14 -0
- package/src/server/routes/sessions/relations.ts +85 -0
- package/src/server/routes/sessions/session.ts +4 -0
- package/src/server/routes/sessions/spawn.ts +134 -0
- package/src/server/routes/settings/model-override.ts +5 -2
- package/src/server/routes/settings/provider-entry.ts +2 -2
- package/src/server/routes/settings/provider-model.ts +2 -2
- package/src/server/routes/settings/provider-models.ts +2 -2
- package/src/server/routes/settings/provider-refresh.ts +6 -4
- package/src/server/routes/settings/providers.ts +3 -1
- package/src/server/routes/settings/settings-config-routes.test.ts +10 -2
- package/src/server/routes/terminal/ws.ts +3 -3
- package/src/shared/lib/chat/ask-form/fields.ts +76 -0
- package/src/shared/lib/chat/ask-form/index.ts +323 -0
- package/src/shared/lib/chat/ask-form/tokens.ts +199 -0
- package/src/shared/lib/chat/ask-form/types.ts +43 -0
- package/src/shared/lib/chat/ask-form.test.ts +137 -0
- package/src/shared/lib/chat/ask-questions.test.ts +65 -1
- package/src/shared/lib/chat/ask-questions.ts +51 -145
- package/src/shared/lib/chat/ask-results.ts +198 -0
- package/src/shared/lib/chat/composer/client.ts +31 -2
- package/src/shared/lib/chat/composer/delegation.test.ts +93 -0
- package/src/shared/lib/chat/composer/delegation.ts +206 -0
- package/src/shared/lib/chat/notice-text.ts +1 -1
- package/src/shared/lib/chat/timeline/omp-callbacks.ts +13 -1
- package/src/shared/lib/chat/timeline/prompt-error.test.ts +146 -0
- package/src/shared/lib/chat/timeline/prompt-error.ts +88 -0
- package/src/shared/lib/chat/tool/summary-wait.test.ts +11 -0
- package/src/shared/lib/code/ansi.ts +1 -1
- package/src/shared/lib/fonts/nerd-symbols.css +4 -3
- package/src/shared/lib/fonts/nerd-symbols.test.ts +9 -9
- package/src/shared/lib/harness/catalog.test.ts +1 -1
- package/src/shared/lib/harness/config-fields.test.ts +4 -0
- package/src/shared/lib/harness/config-fields.ts +19 -4
- package/src/shared/lib/harness/dialects.test.ts +8 -0
- package/src/shared/lib/harness/dialects.ts +12 -0
- package/src/shared/lib/harness/provider-facts.test.ts +59 -8
- package/src/shared/lib/harness/provider-facts.ts +46 -11
- package/src/shared/lib/harness/providers.ts +44 -1
- package/src/shared/lib/harness/rewind.ts +24 -0
- package/src/shared/lib/harness/update-job.ts +33 -0
- package/src/shared/lib/harness/update-plan.test.ts +69 -4
- package/src/shared/lib/harness/update-plan.ts +57 -9
- package/src/shared/lib/harness/updates.test.ts +22 -0
- package/src/shared/lib/harness/updates.ts +24 -1
- package/src/shared/lib/models/client.ts +10 -1
- package/src/shared/lib/omp/dialog-methods.ts +4 -3
- package/src/shared/lib/omp/session/wait-idle.test.ts +11 -0
- package/src/shared/lib/omp/session/wait-idle.ts +12 -2
- package/src/shared/lib/theme/catalog.test.ts +13 -9
- package/src/shared/lib/theme/color.test.ts +2 -2
- package/src/shared/lib/theme/color.ts +4 -4
- package/src/shared/lib/theme/css.ts +2 -0
- package/src/shared/lib/theme/ink-scale.ts +21 -1
- package/src/shared/lib/theme/theme.test.ts +26 -10
- package/src/shared/lib/ui/safe-area.test.ts +28 -0
- package/src/shared/lib/ui/safe-area.ts +42 -0
- package/src/shared/lib/workspace/terminal/input.test.ts +27 -0
- package/src/shared/lib/workspace/terminal/input.ts +33 -0
- package/src/shared/lib/workspace/terminal/keys.test.ts +1 -1
- package/src/shared/lib/workspace/terminal/keys.ts +7 -7
- package/src/shared/types/chat.ts +1 -1
- package/src/shared/types/harness-update.ts +65 -0
- package/src/shared/types/harness.ts +30 -4
- package/src/shared/types/omp/agent.ts +19 -2
- package/src/shared/types/workspace.ts +4 -0
- package/src/test-support/fake-omp-fork.ts +16 -2
- package/src/test-support/file-explorer-render.test-util.ts +37 -0
- package/src/test-support/file-explorer.test-util.ts +20 -18
- package/src/test-support/harness-table.test-util.ts +21 -4
- package/dist/client/static/js/index-jhmg4w91.css +0 -1
- package/src/client/components/workspace/terminal-panel/webgl.test.ts +0 -75
- package/src/client/components/workspace/terminal-panel/webgl.ts +0 -108
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Session trees: depth, cycles, the fork edge, and cleanup edges.
|
|
8
|
+
*
|
|
9
|
+
* A session has no row of its own, so these cases use bare ids — which is the
|
|
10
|
+
* point of the module: the tree is a relationship store, and every rule here
|
|
11
|
+
* must hold for an omp file and an ACP session alike.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
15
|
+
import { isolateDb, releaseDb } from '@/test-support/isolated-db';
|
|
16
|
+
import { getDb } from '@/server/db.server';
|
|
17
|
+
import { ensureSessionRelationsSchema } from '@/server/lib/sessions/schema.server';
|
|
18
|
+
import {
|
|
19
|
+
MAX_SESSION_DEPTH,
|
|
20
|
+
SessionRelationError,
|
|
21
|
+
} from '@/server/lib/sessions/relation-types';
|
|
22
|
+
import {
|
|
23
|
+
createSessionRelation,
|
|
24
|
+
deleteSessionRelations,
|
|
25
|
+
getSessionRelation,
|
|
26
|
+
listChildRelations,
|
|
27
|
+
listForkRelations,
|
|
28
|
+
listOwnedRelations,
|
|
29
|
+
setParentNotifications,
|
|
30
|
+
setSessionParent,
|
|
31
|
+
setSessionVisibility,
|
|
32
|
+
} from '@/server/lib/sessions/relations.server';
|
|
33
|
+
|
|
34
|
+
beforeEach(async () => {
|
|
35
|
+
await isolateDb();
|
|
36
|
+
// The relations tables are created by `initSchema`; the explicit call keeps
|
|
37
|
+
// this suite runnable when it is the first to open the temp database.
|
|
38
|
+
await ensureSessionRelationsSchema(await getDb());
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterEach(async () => {
|
|
42
|
+
await releaseDb();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
/** The error code a rejected call reported, or null when it resolved. */
|
|
46
|
+
async function rejectionCode(run: () => Promise<unknown>): Promise<string | null> {
|
|
47
|
+
try {
|
|
48
|
+
await run();
|
|
49
|
+
return null;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
return error instanceof SessionRelationError ? error.code : `unexpected:${String(error)}`;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
describe('session relations', () => {
|
|
56
|
+
test('a child of a root starts at depth 1 and carries the parent edge', async () => {
|
|
57
|
+
const relation = await createSessionRelation({
|
|
58
|
+
sessionId: 'child',
|
|
59
|
+
parentSessionId: 'root',
|
|
60
|
+
lifecycleOwnerSessionId: 'root',
|
|
61
|
+
});
|
|
62
|
+
expect(relation).toMatchObject({
|
|
63
|
+
sessionId: 'child',
|
|
64
|
+
parentSessionId: 'root',
|
|
65
|
+
lifecycleOwnerSessionId: 'root',
|
|
66
|
+
depth: 1,
|
|
67
|
+
visibility: 'visible',
|
|
68
|
+
parentNotifications: 'auto',
|
|
69
|
+
originKind: null,
|
|
70
|
+
});
|
|
71
|
+
expect(await listChildRelations('root')).toHaveLength(1);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test(`the tree stops at ${MAX_SESSION_DEPTH} levels`, async () => {
|
|
75
|
+
await createSessionRelation({ sessionId: 'l1', parentSessionId: 'root' });
|
|
76
|
+
await createSessionRelation({ sessionId: 'l2', parentSessionId: 'l1' });
|
|
77
|
+
await createSessionRelation({ sessionId: 'l3', parentSessionId: 'l2' });
|
|
78
|
+
// l4 is the fourth level and is allowed.
|
|
79
|
+
const fourth = await createSessionRelation({ sessionId: 'l4', parentSessionId: 'l3' });
|
|
80
|
+
expect(fourth.depth).toBe(4);
|
|
81
|
+
|
|
82
|
+
expect(
|
|
83
|
+
await rejectionCode(() =>
|
|
84
|
+
createSessionRelation({ sessionId: 'l5', parentSessionId: 'l4' }),
|
|
85
|
+
),
|
|
86
|
+
).toBe('depth_exceeded');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('a session cannot be reparented under its own descendant', async () => {
|
|
90
|
+
await createSessionRelation({ sessionId: 'a' });
|
|
91
|
+
await createSessionRelation({ sessionId: 'b', parentSessionId: 'a' });
|
|
92
|
+
await createSessionRelation({ sessionId: 'c', parentSessionId: 'b' });
|
|
93
|
+
|
|
94
|
+
expect(await rejectionCode(() => setSessionParent('a', 'c'))).toBe('cycle');
|
|
95
|
+
expect(await rejectionCode(() => setSessionParent('a', 'a'))).toBe('self_parent');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('reparenting recomputes every descendant depth', async () => {
|
|
99
|
+
await createSessionRelation({ sessionId: 'a' });
|
|
100
|
+
await createSessionRelation({ sessionId: 'b', parentSessionId: 'a' });
|
|
101
|
+
await createSessionRelation({ sessionId: 'c', parentSessionId: 'b' });
|
|
102
|
+
|
|
103
|
+
await setSessionParent('b', null);
|
|
104
|
+
expect((await getSessionRelation('b'))?.depth).toBe(0);
|
|
105
|
+
expect((await getSessionRelation('c'))?.depth).toBe(1);
|
|
106
|
+
expect((await getSessionRelation('c'))?.parentSessionId).toBe('b');
|
|
107
|
+
|
|
108
|
+
await setSessionParent('b', 'a');
|
|
109
|
+
expect((await getSessionRelation('b'))?.depth).toBe(1);
|
|
110
|
+
expect((await getSessionRelation('c'))?.depth).toBe(2);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('reparenting a branch refuses when the moved subtree would exceed the cap', async () => {
|
|
114
|
+
await createSessionRelation({ sessionId: 'a' });
|
|
115
|
+
await createSessionRelation({ sessionId: 'b', parentSessionId: 'a' });
|
|
116
|
+
await createSessionRelation({ sessionId: 'c', parentSessionId: 'b' });
|
|
117
|
+
await createSessionRelation({ sessionId: 'd', parentSessionId: 'c' });
|
|
118
|
+
await createSessionRelation({ sessionId: 'deep1', parentSessionId: 'root1' });
|
|
119
|
+
await createSessionRelation({ sessionId: 'deep2', parentSessionId: 'deep1' });
|
|
120
|
+
await createSessionRelation({ sessionId: 'deep3', parentSessionId: 'deep2' });
|
|
121
|
+
|
|
122
|
+
// `a` is 4 tall; `deep3` is level 3, so a + 3 would be 6.
|
|
123
|
+
expect(await rejectionCode(() => setSessionParent('a', 'deep3'))).toBe('depth_exceeded');
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('a fork is a source edge, never a parent edge', async () => {
|
|
127
|
+
const fork = await createSessionRelation({
|
|
128
|
+
sessionId: 'fork-1',
|
|
129
|
+
sourceSessionId: 'source',
|
|
130
|
+
originKind: 'fork',
|
|
131
|
+
});
|
|
132
|
+
expect(fork).toMatchObject({
|
|
133
|
+
parentSessionId: null,
|
|
134
|
+
sourceSessionId: 'source',
|
|
135
|
+
originKind: 'fork',
|
|
136
|
+
depth: 0,
|
|
137
|
+
});
|
|
138
|
+
expect((await listForkRelations('source')).map((row) => row.sessionId)).toEqual(['fork-1']);
|
|
139
|
+
|
|
140
|
+
expect(
|
|
141
|
+
await rejectionCode(() =>
|
|
142
|
+
createSessionRelation({
|
|
143
|
+
sessionId: 'fork-2',
|
|
144
|
+
sourceSessionId: 'source',
|
|
145
|
+
parentSessionId: 'parent',
|
|
146
|
+
originKind: 'fork',
|
|
147
|
+
}),
|
|
148
|
+
),
|
|
149
|
+
).toBe('fork_is_not_a_child');
|
|
150
|
+
expect(
|
|
151
|
+
await rejectionCode(() =>
|
|
152
|
+
createSessionRelation({ sessionId: 'fork-3', originKind: 'fork' }),
|
|
153
|
+
),
|
|
154
|
+
).toBe('fork_requires_source');
|
|
155
|
+
expect(
|
|
156
|
+
await rejectionCode(() =>
|
|
157
|
+
createSessionRelation({ sessionId: 'plain', sourceSessionId: 'source' }),
|
|
158
|
+
),
|
|
159
|
+
).toBe('not_fork');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('visibility and notifications are presentation and delivery, independently', async () => {
|
|
163
|
+
await createSessionRelation({ sessionId: 'child', parentSessionId: 'root' });
|
|
164
|
+
await setSessionVisibility('child', 'hidden');
|
|
165
|
+
await setParentNotifications('child', 'stuck');
|
|
166
|
+
expect(await getSessionRelation('child')).toMatchObject({
|
|
167
|
+
visibility: 'hidden',
|
|
168
|
+
parentNotifications: 'stuck',
|
|
169
|
+
parentSessionId: 'root',
|
|
170
|
+
});
|
|
171
|
+
expect(await listChildRelations('root')).toHaveLength(1);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test('a cleanup owner sees every dependent, across parents', async () => {
|
|
175
|
+
await createSessionRelation({ sessionId: 'a', lifecycleOwnerSessionId: 'boss' });
|
|
176
|
+
await createSessionRelation({
|
|
177
|
+
sessionId: 'b',
|
|
178
|
+
parentSessionId: 'a',
|
|
179
|
+
lifecycleOwnerSessionId: 'boss',
|
|
180
|
+
});
|
|
181
|
+
await createSessionRelation({ sessionId: 'c', lifecycleOwnerSessionId: 'other' });
|
|
182
|
+
expect((await listOwnedRelations('boss')).map((row) => row.sessionId)).toEqual(['a', 'b']);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test('deleting a session clears incoming edges but keeps a fork source row', async () => {
|
|
186
|
+
await createSessionRelation({ sessionId: 'parent', lifecycleOwnerSessionId: 'owner' });
|
|
187
|
+
await createSessionRelation({ sessionId: 'child', parentSessionId: 'parent' });
|
|
188
|
+
await createSessionRelation({
|
|
189
|
+
sessionId: 'fork-1',
|
|
190
|
+
sourceSessionId: 'parent',
|
|
191
|
+
originKind: 'fork',
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
await deleteSessionRelations('parent');
|
|
195
|
+
expect(await getSessionRelation('parent')).toBeNull();
|
|
196
|
+
// The fork's own row survives; only its source edge is cleared.
|
|
197
|
+
expect(await getSessionRelation('fork-1')).toMatchObject({
|
|
198
|
+
sourceSessionId: null,
|
|
199
|
+
originKind: null,
|
|
200
|
+
});
|
|
201
|
+
// A child is released to the root rather than left pointing at a dead id.
|
|
202
|
+
expect(await getSessionRelation('child')).toMatchObject({
|
|
203
|
+
parentSessionId: null,
|
|
204
|
+
depth: 0,
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
});
|
|
@@ -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
|
+
}
|