@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,75 @@
|
|
|
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 {
|
|
12
|
+
archiveDependents,
|
|
13
|
+
collectDependents,
|
|
14
|
+
dependentsDeepestFirst,
|
|
15
|
+
} from '@/server/lib/sessions/lifecycle.server';
|
|
16
|
+
|
|
17
|
+
beforeEach(async () => {
|
|
18
|
+
await isolateDb();
|
|
19
|
+
await ensureSessionRelationsSchema(await getDb());
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(async () => {
|
|
23
|
+
await releaseDb();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('session lifecycle cascade', () => {
|
|
27
|
+
test('follows both the parent and the owner edge, once each', async () => {
|
|
28
|
+
await createSessionRelation({ sessionId: 'root' });
|
|
29
|
+
await createSessionRelation({ sessionId: 'child', parentSessionId: 'root' });
|
|
30
|
+
await createSessionRelation({
|
|
31
|
+
sessionId: 'grandchild',
|
|
32
|
+
parentSessionId: 'child',
|
|
33
|
+
lifecycleOwnerSessionId: 'root',
|
|
34
|
+
});
|
|
35
|
+
await createSessionRelation({
|
|
36
|
+
sessionId: 'worker',
|
|
37
|
+
lifecycleOwnerSessionId: 'root',
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const dependents = await collectDependents('root');
|
|
41
|
+
expect(new Set(dependents)).toEqual(new Set(['child', 'grandchild', 'worker']));
|
|
42
|
+
expect(dependents).toHaveLength(3);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('a fork is not a dependent of its source', async () => {
|
|
46
|
+
await createSessionRelation({ sessionId: 'root' });
|
|
47
|
+
await createSessionRelation({
|
|
48
|
+
sessionId: 'fork-1',
|
|
49
|
+
sourceSessionId: 'root',
|
|
50
|
+
originKind: 'fork',
|
|
51
|
+
});
|
|
52
|
+
expect(await collectDependents('root')).toEqual([]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('archiveDependents writes only the rows that were not archived', async () => {
|
|
56
|
+
await createSessionRelation({ sessionId: 'root' });
|
|
57
|
+
await createSessionRelation({ sessionId: 'child', parentSessionId: 'root' });
|
|
58
|
+
await createSessionRelation({ sessionId: 'grandchild', parentSessionId: 'child' });
|
|
59
|
+
const db = await getDb();
|
|
60
|
+
await db.run('INSERT INTO archived_sessions (session_id) VALUES (?)', ['child']);
|
|
61
|
+
|
|
62
|
+
expect(await archiveDependents('root')).toEqual(['grandchild']);
|
|
63
|
+
const rows = (await db.all('SELECT session_id FROM archived_sessions ORDER BY session_id')) as {
|
|
64
|
+
session_id: string;
|
|
65
|
+
}[];
|
|
66
|
+
expect(rows.map((row) => row.session_id)).toEqual(['child', 'grandchild']);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('deepest-first is the reverse of the walk', async () => {
|
|
70
|
+
await createSessionRelation({ sessionId: 'root' });
|
|
71
|
+
await createSessionRelation({ sessionId: 'child', parentSessionId: 'root' });
|
|
72
|
+
await createSessionRelation({ sessionId: 'grandchild', parentSessionId: 'child' });
|
|
73
|
+
expect(await dependentsDeepestFirst('root')).toEqual(['grandchild', 'child']);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The sessions that follow an owner into the archive, or out of existence.
|
|
8
|
+
*
|
|
9
|
+
* Two edges cascade and they are independent: the sidebar parent
|
|
10
|
+
* (`parent_session_id`) and cleanup ownership (`lifecycle_owner_session_id`).
|
|
11
|
+
* A session can be one without the other, and a fork is neither — a source edge
|
|
12
|
+
* never makes a session a dependent, so deleting or archiving a source leaves
|
|
13
|
+
* its forks alone.
|
|
14
|
+
*
|
|
15
|
+
* Collection is breadth-first with a visited set: the two edges may point at
|
|
16
|
+
* the same session, and a corrupted cycle must not hang a delete.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { getDb } from '@/server/db.server';
|
|
20
|
+
import { listChildRelations, listOwnedRelations } from '@/server/lib/sessions/relations.server';
|
|
21
|
+
|
|
22
|
+
/** Every dependent descendant, parents first, deduplicated, excluding the root. */
|
|
23
|
+
export async function collectDependents(sessionId: string): Promise<string[]> {
|
|
24
|
+
const seen = new Set<string>([sessionId]);
|
|
25
|
+
const ordered: string[] = [];
|
|
26
|
+
const queue = [sessionId];
|
|
27
|
+
while (queue.length > 0) {
|
|
28
|
+
const current = queue.shift();
|
|
29
|
+
if (!current) continue;
|
|
30
|
+
const [children, owned] = await Promise.all([
|
|
31
|
+
listChildRelations(current),
|
|
32
|
+
listOwnedRelations(current),
|
|
33
|
+
]);
|
|
34
|
+
for (const relation of [...children, ...owned]) {
|
|
35
|
+
const id = relation.sessionId;
|
|
36
|
+
if (seen.has(id)) continue;
|
|
37
|
+
seen.add(id);
|
|
38
|
+
ordered.push(id);
|
|
39
|
+
queue.push(id);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return ordered;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Archive every dependent of a session. Returns the ids this call archived.
|
|
47
|
+
*
|
|
48
|
+
* Unarchiving is deliberately NOT the reverse: the reference product restores
|
|
49
|
+
* explicitly, owner first, because auto-restoring a fan-out would resurrect
|
|
50
|
+
* work the user archived one session at a time.
|
|
51
|
+
*/
|
|
52
|
+
export async function archiveDependents(sessionId: string): Promise<string[]> {
|
|
53
|
+
const dependents = await collectDependents(sessionId);
|
|
54
|
+
if (dependents.length === 0) return [];
|
|
55
|
+
const db = await getDb();
|
|
56
|
+
const archived: string[] = [];
|
|
57
|
+
for (const id of dependents) {
|
|
58
|
+
const result = await db.run(
|
|
59
|
+
'INSERT OR IGNORE INTO archived_sessions (session_id) VALUES (?)',
|
|
60
|
+
[id],
|
|
61
|
+
);
|
|
62
|
+
if (result.changes > 0) archived.push(id);
|
|
63
|
+
}
|
|
64
|
+
return archived;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Deepest-first, so a parent is removed after the sessions that name it. */
|
|
68
|
+
export async function dependentsDeepestFirst(sessionId: string): Promise<string[]> {
|
|
69
|
+
const dependents = await collectDependents(sessionId);
|
|
70
|
+
return dependents.reverse();
|
|
71
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Child outcomes: filtering, coalescing and the one queue row they become.
|
|
8
|
+
*
|
|
9
|
+
* The window is exercised through `materializeNotice` rather than by waiting
|
|
10
|
+
* two seconds; the timer itself is cancelled in `afterEach` so a suite does not
|
|
11
|
+
* leak a flush into the next case.
|
|
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
|
+
createSessionRelation,
|
|
20
|
+
setParentNotifications,
|
|
21
|
+
} from '@/server/lib/sessions/relations.server';
|
|
22
|
+
import {
|
|
23
|
+
cancelNoticeTimer,
|
|
24
|
+
materializeNotice,
|
|
25
|
+
noteChildOutcome,
|
|
26
|
+
} from '@/server/lib/sessions/notify.server';
|
|
27
|
+
import { listQueue } from '@/server/lib/queue/store.server';
|
|
28
|
+
|
|
29
|
+
const parents = new Set<string>();
|
|
30
|
+
|
|
31
|
+
beforeEach(async () => {
|
|
32
|
+
parents.clear();
|
|
33
|
+
await isolateDb();
|
|
34
|
+
await ensureSessionRelationsSchema(await getDb());
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
afterEach(async () => {
|
|
38
|
+
for (const parent of parents) cancelNoticeTimer(parent);
|
|
39
|
+
await releaseDb();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
async function childOf(
|
|
43
|
+
sessionId: string,
|
|
44
|
+
parentSessionId: string,
|
|
45
|
+
preference?: 'auto' | 'stuck' | 'off',
|
|
46
|
+
): Promise<void> {
|
|
47
|
+
await createSessionRelation({ sessionId, parentSessionId });
|
|
48
|
+
if (preference) await setParentNotifications(sessionId, preference);
|
|
49
|
+
parents.add(parentSessionId);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
describe('child outcome notices', () => {
|
|
53
|
+
test('two children collapse into one row, not two turns', async () => {
|
|
54
|
+
await childOf('c1', 'p');
|
|
55
|
+
await childOf('c2', 'p');
|
|
56
|
+
await noteChildOutcome({ sessionId: 'c1', status: 'completed', output: 'first done' });
|
|
57
|
+
await noteChildOutcome({ sessionId: 'c2', status: 'completed', output: 'second done' });
|
|
58
|
+
|
|
59
|
+
const text = await materializeNotice('p');
|
|
60
|
+
expect(text).toContain('Child session updates');
|
|
61
|
+
expect(text).toContain('c1');
|
|
62
|
+
expect(text).toContain('c2');
|
|
63
|
+
expect(await listQueue('p')).toHaveLength(1);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('a repeated outcome for one child replaces the earlier line', async () => {
|
|
67
|
+
await childOf('c1', 'p');
|
|
68
|
+
await noteChildOutcome({ sessionId: 'c1', status: 'interrupted' });
|
|
69
|
+
await noteChildOutcome({ sessionId: 'c1', status: 'completed', output: 'finished later' });
|
|
70
|
+
|
|
71
|
+
const text = await materializeNotice('p');
|
|
72
|
+
expect(text).not.toBeNull();
|
|
73
|
+
if (text === null) throw new Error('expected a notice');
|
|
74
|
+
expect(text).toContain('completed');
|
|
75
|
+
expect(text).not.toContain('interrupted');
|
|
76
|
+
expect(text.match(/c1/g)).toHaveLength(1);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('off writes nothing; stuck skips a completed turn', async () => {
|
|
80
|
+
await childOf('quiet', 'p', 'off');
|
|
81
|
+
await noteChildOutcome({ sessionId: 'quiet', status: 'completed', output: 'x' });
|
|
82
|
+
expect(await materializeNotice('p')).toBeNull();
|
|
83
|
+
|
|
84
|
+
await childOf('stuck-child', 'p', 'stuck');
|
|
85
|
+
await noteChildOutcome({ sessionId: 'stuck-child', status: 'completed', output: 'x' });
|
|
86
|
+
expect(await materializeNotice('p')).toBeNull();
|
|
87
|
+
|
|
88
|
+
await noteChildOutcome({ sessionId: 'stuck-child', status: 'failed', failure: 'boom' });
|
|
89
|
+
expect(await materializeNotice('p')).toContain('boom');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('a fork and a root session are both silent', async () => {
|
|
93
|
+
await createSessionRelation({
|
|
94
|
+
sessionId: 'fork-1',
|
|
95
|
+
sourceSessionId: 'source',
|
|
96
|
+
originKind: 'fork',
|
|
97
|
+
});
|
|
98
|
+
await createSessionRelation({ sessionId: 'root' });
|
|
99
|
+
await noteChildOutcome({ sessionId: 'fork-1', status: 'completed', output: 'x' });
|
|
100
|
+
await noteChildOutcome({ sessionId: 'root', status: 'completed', output: 'x' });
|
|
101
|
+
expect(await materializeNotice('source')).toBeNull();
|
|
102
|
+
expect(await materializeNotice('root')).toBeNull();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('a second materialize cannot duplicate the row', async () => {
|
|
106
|
+
await childOf('c1', 'p');
|
|
107
|
+
await noteChildOutcome({ sessionId: 'c1', status: 'completed', output: 'done' });
|
|
108
|
+
expect(await materializeNotice('p')).not.toBeNull();
|
|
109
|
+
expect(await materializeNotice('p')).toBeNull();
|
|
110
|
+
expect(await listQueue('p')).toHaveLength(1);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Child outcomes → one message in the parent's queue.
|
|
8
|
+
*
|
|
9
|
+
* A turn end is the event a parent asked about, but waking it once per child
|
|
10
|
+
* turns a fan-out into a notification storm: ten children finishing within a
|
|
11
|
+
* second would open ten turns, each obliged to read the other nine. So outcomes
|
|
12
|
+
* land in `session_notices` first, coalesce for a short window, and become ONE
|
|
13
|
+
* `queued_messages` row — the queue already owns durability, retry and busy
|
|
14
|
+
* handling, and a second delivery path would drift from it.
|
|
15
|
+
*
|
|
16
|
+
* Only a delegated child notifies. A fork has no parent edge; a child whose
|
|
17
|
+
* `parentNotifications` is `off` or whose outcome is filtered by `stuck` writes
|
|
18
|
+
* nothing.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { getDb } from '@/server/db.server';
|
|
22
|
+
import { appendQueueItem } from '@/server/lib/queue/store.server';
|
|
23
|
+
import { deliverQueueNow } from '@/server/lib/queue/delivery.server';
|
|
24
|
+
import { getSessionRelation } from '@/server/lib/sessions/relations.server';
|
|
25
|
+
import { isSessionArchived } from '@/server/lib/sessions/resolve.server';
|
|
26
|
+
import { ensureSessionLive } from '@/server/lib/sessions/dispatch.server';
|
|
27
|
+
|
|
28
|
+
export const NOTICE_WINDOW_MS = 2_000;
|
|
29
|
+
const OUTPUT_EXCERPT_LIMIT = 4_000;
|
|
30
|
+
const TRUNCATION_MARKER = '\n\n[... output truncated ...]';
|
|
31
|
+
const COALESCE_KEY = 'child-outcomes';
|
|
32
|
+
|
|
33
|
+
export interface ChildOutcomeNotice {
|
|
34
|
+
sessionId: string;
|
|
35
|
+
title?: string | null;
|
|
36
|
+
status: 'completed' | 'failed' | 'interrupted';
|
|
37
|
+
/** The child's final assistant text, when the transport could read it. */
|
|
38
|
+
output?: string | null;
|
|
39
|
+
/** Why it failed or was interrupted, when known. */
|
|
40
|
+
failure?: string | null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface NoticeRow {
|
|
44
|
+
id: string;
|
|
45
|
+
parent_session_id: string;
|
|
46
|
+
coalesce_key: string;
|
|
47
|
+
state: string;
|
|
48
|
+
send_at: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
declare global {
|
|
52
|
+
// eslint-disable-next-line no-var
|
|
53
|
+
var __kabmoNoticeTimers: Map<string, ReturnType<typeof setTimeout>> | undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function timers(): Map<string, ReturnType<typeof setTimeout>> {
|
|
57
|
+
globalThis.__kabmoNoticeTimers ??= new Map();
|
|
58
|
+
return globalThis.__kabmoNoticeTimers;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function excerpt(text: string | null | undefined): string {
|
|
62
|
+
const trimmed = text?.trim();
|
|
63
|
+
if (!trimmed) return 'No final output was recorded.';
|
|
64
|
+
if (trimmed.length <= OUTPUT_EXCERPT_LIMIT) return trimmed;
|
|
65
|
+
const keep = Math.max(0, OUTPUT_EXCERPT_LIMIT - TRUNCATION_MARKER.length);
|
|
66
|
+
return `${trimmed.slice(0, keep).trimEnd()}${TRUNCATION_MARKER}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function describe(item: ChildOutcomeNotice): string {
|
|
70
|
+
const title = item.title?.trim();
|
|
71
|
+
return title ? `"${title}" (${item.sessionId})` : item.sessionId;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function oneLine(text: string): string {
|
|
75
|
+
return text.replace(/\s+/g, ' ').trim();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function renderOutcome(item: ChildOutcomeNotice): string {
|
|
79
|
+
const label = describe(item);
|
|
80
|
+
if (item.status === 'completed') {
|
|
81
|
+
return `Child session ${label} completed:\n\n${excerpt(item.output)}`;
|
|
82
|
+
}
|
|
83
|
+
const reason =
|
|
84
|
+
item.status === 'interrupted'
|
|
85
|
+
? item.failure || 'it was interrupted'
|
|
86
|
+
: item.failure || 'it failed';
|
|
87
|
+
return `Child session ${label} ${reason}.\n\nReview the session before deciding the next step.`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** One notice body, for one child or a coalesced batch. */
|
|
91
|
+
export function renderChildOutcomeNotice(items: readonly ChildOutcomeNotice[]): string {
|
|
92
|
+
if (items.length === 1 && items[0]) return renderOutcome(items[0]);
|
|
93
|
+
const lines = items.map((item) => {
|
|
94
|
+
const label = describe(item);
|
|
95
|
+
if (item.status === 'completed') {
|
|
96
|
+
return `- ${label} completed: ${oneLine(excerpt(item.output))}`;
|
|
97
|
+
}
|
|
98
|
+
return `- ${label} ${item.failure || item.status}.`;
|
|
99
|
+
});
|
|
100
|
+
return `Child session updates:\n\n${lines.join('\n')}`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function mergeOutcome(
|
|
104
|
+
previous: readonly ChildOutcomeNotice[],
|
|
105
|
+
next: ChildOutcomeNotice,
|
|
106
|
+
): ChildOutcomeNotice[] {
|
|
107
|
+
const index = previous.findIndex((item) => item.sessionId === next.sessionId);
|
|
108
|
+
if (index === -1) return [...previous, next];
|
|
109
|
+
return previous.map((item, position) => (position === index ? next : item));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function shouldNotify(
|
|
113
|
+
status: ChildOutcomeNotice['status'],
|
|
114
|
+
preference: 'auto' | 'stuck' | 'off',
|
|
115
|
+
): boolean {
|
|
116
|
+
if (preference === 'off') return false;
|
|
117
|
+
if (preference === 'stuck') return status !== 'completed';
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Record one outcome, coalescing it into the parent's open window.
|
|
123
|
+
*
|
|
124
|
+
* Best-effort by contract: this runs from a turn-settle hook, and a database
|
|
125
|
+
* failure must not turn a finished turn into a failed one. The caller wraps it.
|
|
126
|
+
*/
|
|
127
|
+
export async function noteChildOutcome(outcome: ChildOutcomeNotice): Promise<void> {
|
|
128
|
+
const relation = await getSessionRelation(outcome.sessionId);
|
|
129
|
+
if (!relation?.parentSessionId || relation.originKind !== null) return;
|
|
130
|
+
if (!shouldNotify(outcome.status, relation.parentNotifications)) return;
|
|
131
|
+
|
|
132
|
+
const parentSessionId = relation.parentSessionId;
|
|
133
|
+
if (await isSessionArchived(parentSessionId)) return;
|
|
134
|
+
|
|
135
|
+
const db = await getDb();
|
|
136
|
+
const now = Date.now();
|
|
137
|
+
const sendAt = now + NOTICE_WINDOW_MS;
|
|
138
|
+
const existing = await db.get<NoticeRow>(
|
|
139
|
+
'SELECT * FROM session_notices WHERE parent_session_id = ? AND coalesce_key = ?',
|
|
140
|
+
[parentSessionId, COALESCE_KEY],
|
|
141
|
+
);
|
|
142
|
+
const previous = existing
|
|
143
|
+
? (JSON.parse(existing.state) as ChildOutcomeNotice[])
|
|
144
|
+
: [];
|
|
145
|
+
const state = JSON.stringify(mergeOutcome(previous, outcome));
|
|
146
|
+
await db.run(
|
|
147
|
+
`INSERT INTO session_notices
|
|
148
|
+
(id, parent_session_id, coalesce_key, state, send_at, created_at, updated_at)
|
|
149
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
150
|
+
ON CONFLICT(parent_session_id, coalesce_key) DO UPDATE SET
|
|
151
|
+
state = excluded.state,
|
|
152
|
+
send_at = excluded.send_at,
|
|
153
|
+
updated_at = excluded.updated_at`,
|
|
154
|
+
[crypto.randomUUID(), parentSessionId, COALESCE_KEY, state, sendAt, now, now],
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
clearTimeout(timers().get(parentSessionId));
|
|
158
|
+
timers().set(
|
|
159
|
+
parentSessionId,
|
|
160
|
+
setTimeout(() => {
|
|
161
|
+
timers().delete(parentSessionId);
|
|
162
|
+
void flushNotice(parentSessionId).catch((error) => {
|
|
163
|
+
console.error(
|
|
164
|
+
`[sessions] could not deliver child outcomes to ${parentSessionId}:`,
|
|
165
|
+
error instanceof Error ? error.message : error,
|
|
166
|
+
);
|
|
167
|
+
});
|
|
168
|
+
}, NOTICE_WINDOW_MS),
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Drop a pending window without materialising it (tests and session purge). */
|
|
173
|
+
export function cancelNoticeTimer(parentSessionId: string): void {
|
|
174
|
+
clearTimeout(timers().get(parentSessionId));
|
|
175
|
+
timers().delete(parentSessionId);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Turn an open window into one queue row, without waking the parent.
|
|
180
|
+
*
|
|
181
|
+
* Split from `flushNotice` so a test can prove the merge and the text without
|
|
182
|
+
* spawning a process. Returns null when another caller already claimed it.
|
|
183
|
+
*/
|
|
184
|
+
export async function materializeNotice(parentSessionId: string): Promise<string | null> {
|
|
185
|
+
const db = await getDb();
|
|
186
|
+
const row = await db.get<NoticeRow>(
|
|
187
|
+
'SELECT * FROM session_notices WHERE parent_session_id = ? AND coalesce_key = ?',
|
|
188
|
+
[parentSessionId, COALESCE_KEY],
|
|
189
|
+
);
|
|
190
|
+
if (!row) return null;
|
|
191
|
+
// The DELETE is the claim: two timers (a hot reload, a second instance)
|
|
192
|
+
// cannot both turn one window into two queue rows.
|
|
193
|
+
const claimed = await db.run(
|
|
194
|
+
'DELETE FROM session_notices WHERE parent_session_id = ? AND coalesce_key = ?',
|
|
195
|
+
[parentSessionId, COALESCE_KEY],
|
|
196
|
+
);
|
|
197
|
+
if (claimed.changes === 0) return null;
|
|
198
|
+
|
|
199
|
+
const items = JSON.parse(row.state) as ChildOutcomeNotice[];
|
|
200
|
+
if (items.length === 0) return null;
|
|
201
|
+
const text = renderChildOutcomeNotice(items);
|
|
202
|
+
await appendQueueItem(parentSessionId, { text });
|
|
203
|
+
return text;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Materialize the window, then make sure the parent is awake to read it. */
|
|
207
|
+
export async function flushNotice(parentSessionId: string): Promise<void> {
|
|
208
|
+
const text = await materializeNotice(parentSessionId);
|
|
209
|
+
if (text === null) return;
|
|
210
|
+
const session = await ensureSessionLive(parentSessionId);
|
|
211
|
+
if (session) await deliverQueueNow(session);
|
|
212
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The vocabulary of a session tree: its shape, its error codes, and the row
|
|
8
|
+
* decoder. Split from the service so a route can name a refusal's code without
|
|
9
|
+
* importing the write path.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export const MAX_SESSION_DEPTH = 4;
|
|
13
|
+
|
|
14
|
+
export type SessionVisibility = 'visible' | 'hidden';
|
|
15
|
+
export type ParentNotifications = 'auto' | 'stuck' | 'off';
|
|
16
|
+
export type SessionOriginKind = 'fork';
|
|
17
|
+
|
|
18
|
+
export interface SessionRelation {
|
|
19
|
+
sessionId: string;
|
|
20
|
+
parentSessionId: string | null;
|
|
21
|
+
lifecycleOwnerSessionId: string | null;
|
|
22
|
+
sourceSessionId: string | null;
|
|
23
|
+
originKind: SessionOriginKind | null;
|
|
24
|
+
visibility: SessionVisibility;
|
|
25
|
+
parentNotifications: ParentNotifications;
|
|
26
|
+
depth: number;
|
|
27
|
+
createdAt: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type SessionRelationErrorCode =
|
|
31
|
+
| 'invalid_argument'
|
|
32
|
+
| 'self_parent'
|
|
33
|
+
| 'cycle'
|
|
34
|
+
| 'depth_exceeded'
|
|
35
|
+
| 'fork_requires_source'
|
|
36
|
+
| 'fork_is_not_a_child'
|
|
37
|
+
| 'not_fork';
|
|
38
|
+
|
|
39
|
+
export class SessionRelationError extends Error {
|
|
40
|
+
constructor(
|
|
41
|
+
readonly code: SessionRelationErrorCode,
|
|
42
|
+
message: string,
|
|
43
|
+
) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = 'SessionRelationError';
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface RelationRow {
|
|
50
|
+
session_id: string;
|
|
51
|
+
parent_session_id: string | null;
|
|
52
|
+
lifecycle_owner_session_id: string | null;
|
|
53
|
+
source_session_id: string | null;
|
|
54
|
+
origin_kind: string | null;
|
|
55
|
+
visibility: string;
|
|
56
|
+
parent_notifications: string;
|
|
57
|
+
depth: number;
|
|
58
|
+
created_at: number;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function fromRow(row: RelationRow): SessionRelation {
|
|
62
|
+
return {
|
|
63
|
+
sessionId: row.session_id,
|
|
64
|
+
parentSessionId: row.parent_session_id,
|
|
65
|
+
lifecycleOwnerSessionId: row.lifecycle_owner_session_id,
|
|
66
|
+
sourceSessionId: row.source_session_id,
|
|
67
|
+
originKind: row.origin_kind === 'fork' ? 'fork' : null,
|
|
68
|
+
visibility: row.visibility === 'hidden' ? 'hidden' : 'visible',
|
|
69
|
+
parentNotifications:
|
|
70
|
+
row.parent_notifications === 'stuck' || row.parent_notifications === 'off'
|
|
71
|
+
? row.parent_notifications
|
|
72
|
+
: 'auto',
|
|
73
|
+
depth: row.depth,
|
|
74
|
+
createdAt: row.created_at,
|
|
75
|
+
};
|
|
76
|
+
}
|