@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,320 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Work an ACP agent keeps doing AFTER it has answered `session/prompt`.
|
|
8
|
+
*
|
|
9
|
+
* antigravity's `invoke_subagent` returns immediately: the tool call is marked
|
|
10
|
+
* `completed`, the agent says "sedang membuatkan…", and `session/prompt`
|
|
11
|
+
* resolves with `end_turn` while the subagent is still running. Measured on agy
|
|
12
|
+
* 1.5.3, the subagent's later completion and the agent's follow-up answer are
|
|
13
|
+
* written to its transcript but **no `session/update` frame is sent while the
|
|
14
|
+
* connection is idle**; the frames are held until the next request (upstream
|
|
15
|
+
* issue google-antigravity/antigravity-cli#1461). The operator then sees a
|
|
16
|
+
* stopped chat with no notice.
|
|
17
|
+
*
|
|
18
|
+
* `session/load` is the one request that replays that transcript, so the turn
|
|
19
|
+
* that spawned the subagent is followed by a load probe: replay frames are
|
|
20
|
+
* collected instead of folded (they repeat the whole chat), and the text past
|
|
21
|
+
* the answer the operator already has is emitted as the agent's continuation.
|
|
22
|
+
*
|
|
23
|
+
* The probe backs off (2s → 30s) and gives up after ten minutes; it never
|
|
24
|
+
* substitutes a failure for an answer it did not see.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { acpContentText } from '@/server/lib/harness/acp/mapper';
|
|
28
|
+
import { stampMessageModel } from '@/server/lib/harness/acp/session-config';
|
|
29
|
+
import { emitRealtimeSignal } from '@/server/lib/realtime/signals.server';
|
|
30
|
+
import { extractTextFromContent } from '@/shared/lib/omp/session/mapper';
|
|
31
|
+
import type { AgentEvent } from '@/server/lib/omp/rpc/constants';
|
|
32
|
+
|
|
33
|
+
/** Tool names that hand work to a process the turn does not await. */
|
|
34
|
+
const BACKGROUND_TOOLS: Record<string, true> = {
|
|
35
|
+
invoke_subagent: true,
|
|
36
|
+
spawn_subagent: true,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const BACKGROUND_FIRST_DELAY_MS = 2_000;
|
|
40
|
+
export const BACKGROUND_MAX_DELAY_MS = 30_000;
|
|
41
|
+
export const BACKGROUND_TIMEOUT_MS = 10 * 60_000;
|
|
42
|
+
export const BACKGROUND_LOAD_TIMEOUT_MS = 120_000;
|
|
43
|
+
|
|
44
|
+
/** Did this turn hand work to a background worker? Read from the closed
|
|
45
|
+
* assistant message, which is the only place the tool call survives. */
|
|
46
|
+
export function spawnsBackgroundWork(message: unknown): boolean {
|
|
47
|
+
if (!message || typeof message !== 'object' || !('content' in message)) return false;
|
|
48
|
+
const content = message.content;
|
|
49
|
+
if (!Array.isArray(content)) return false;
|
|
50
|
+
return content.some((block) => {
|
|
51
|
+
if (!block || typeof block !== 'object') return false;
|
|
52
|
+
if (!('type' in block) || block.type !== 'toolCall') return false;
|
|
53
|
+
return 'name' in block && typeof block.name === 'string' && BACKGROUND_TOOLS[block.name.toLowerCase()] === true;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The prose the operator already has, for the suffix comparison. */
|
|
58
|
+
export function assistantTextOf(message: unknown): string {
|
|
59
|
+
if (!message || typeof message !== 'object' || !('content' in message)) return '';
|
|
60
|
+
return extractTextFromContent(message.content).trim();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The NEW assistant text in a replay, or null when it carries nothing past
|
|
65
|
+
* what the operator has already seen.
|
|
66
|
+
*
|
|
67
|
+
* A replay repeats the whole conversation, and a long conversation's earlier
|
|
68
|
+
* turns are not this turn's notice. Two guards keep the notice unique: the
|
|
69
|
+
* replay segment must extend the known text, and a replay that diverges from it
|
|
70
|
+
* (a retried prompt, a different agent's wording) is left alone rather than
|
|
71
|
+
* guessed at.
|
|
72
|
+
*/
|
|
73
|
+
export function backgroundNotice(baseline: string, replay: string): string | null {
|
|
74
|
+
const current = replay.trim();
|
|
75
|
+
if (!current) return null;
|
|
76
|
+
const known = baseline.trim();
|
|
77
|
+
if (!known) return current;
|
|
78
|
+
if (!current.startsWith(known)) return null;
|
|
79
|
+
const extra = current.slice(known.length).trim();
|
|
80
|
+
return extra.length > 0 ? extra : null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The frames a delayed answer is delivered with. It is a complete run: the
|
|
84
|
+
* client's fold only clears its generating flag on a terminal `agent_end`. */
|
|
85
|
+
export function backgroundNoticeFrames(text: string): AgentEvent[] {
|
|
86
|
+
const message = {
|
|
87
|
+
role: 'assistant',
|
|
88
|
+
id: `acp-bg-${crypto.randomUUID()}`,
|
|
89
|
+
content: [{ type: 'text', text }],
|
|
90
|
+
};
|
|
91
|
+
return [
|
|
92
|
+
{ type: 'agent_start' },
|
|
93
|
+
{ type: 'turn_start' },
|
|
94
|
+
{
|
|
95
|
+
type: 'message_update',
|
|
96
|
+
message,
|
|
97
|
+
assistantMessageEvent: { type: 'text_delta', delta: text },
|
|
98
|
+
},
|
|
99
|
+
{ type: 'message_end', message },
|
|
100
|
+
{ type: 'agent_end', isTerminal: true, stopReason: 'end_turn' },
|
|
101
|
+
];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** What the session must move when the probe hands it the delayed answer. */
|
|
105
|
+
export interface BackgroundNoticeHost {
|
|
106
|
+
sessionId: string;
|
|
107
|
+
runModel: { provider: string; modelId: string } | null;
|
|
108
|
+
messageCount: number;
|
|
109
|
+
assistantCount: number;
|
|
110
|
+
emit: (event: AgentEvent) => void;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function deliverBackgroundNotice(host: BackgroundNoticeHost, text: string): void {
|
|
114
|
+
host.messageCount += 1;
|
|
115
|
+
host.assistantCount += 1;
|
|
116
|
+
for (const event of backgroundNoticeFrames(text)) {
|
|
117
|
+
host.emit(stampMessageModel(event, host.runModel));
|
|
118
|
+
}
|
|
119
|
+
emitRealtimeSignal('session-data-dirty', host.sessionId);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export interface AcpBackgroundProbeOptions {
|
|
123
|
+
/** Replay the session — `session/load` on the live process. */
|
|
124
|
+
load: () => Promise<unknown>;
|
|
125
|
+
/** A probe is a sign the session is still wanted. */
|
|
126
|
+
resetIdle: () => void;
|
|
127
|
+
/** The text past the answer already shown. */
|
|
128
|
+
emit: (text: string) => void;
|
|
129
|
+
timeoutMs?: number;
|
|
130
|
+
firstDelayMs?: number;
|
|
131
|
+
maxDelayMs?: number;
|
|
132
|
+
now?: () => number;
|
|
133
|
+
schedule?: (fn: () => void, ms: number) => unknown;
|
|
134
|
+
cancel?: (handle: unknown) => void;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Replay chunks collected since the last `user_message_chunk`. */
|
|
138
|
+
interface ReplaySegment {
|
|
139
|
+
text: string;
|
|
140
|
+
sawUser: boolean;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The load probe for one background turn.
|
|
145
|
+
*
|
|
146
|
+
* `consume` must be called for every notification while a probe is in flight:
|
|
147
|
+
* the replay repeats the whole transcript, and folding it into the live chat
|
|
148
|
+
* would duplicate history.
|
|
149
|
+
*/
|
|
150
|
+
export class AcpBackgroundProbe {
|
|
151
|
+
private readonly load: () => Promise<unknown>;
|
|
152
|
+
private readonly resetIdle: () => void;
|
|
153
|
+
private readonly emitNotice: (text: string) => void;
|
|
154
|
+
private readonly timeoutMs: number;
|
|
155
|
+
private readonly firstDelayMs: number;
|
|
156
|
+
private readonly maxDelayMs: number;
|
|
157
|
+
private readonly now: () => number;
|
|
158
|
+
private readonly schedule: (fn: () => void, ms: number) => unknown;
|
|
159
|
+
private readonly cancel: (handle: unknown) => void;
|
|
160
|
+
|
|
161
|
+
private baseline = '';
|
|
162
|
+
private collecting: ReplaySegment | null = null;
|
|
163
|
+
/** The notice seen in the last replay; a repeated value is COMPLETE. */
|
|
164
|
+
private pendingNotice: string | null = null;
|
|
165
|
+
private timer: unknown = null;
|
|
166
|
+
private deadline = 0;
|
|
167
|
+
private delay: number;
|
|
168
|
+
private stopped = true;
|
|
169
|
+
private inFlight = false;
|
|
170
|
+
/** Consecutive `session/load` failures; a lost agent is not retried forever. */
|
|
171
|
+
private failures = 0;
|
|
172
|
+
private queued: string | null = null;
|
|
173
|
+
|
|
174
|
+
constructor(options: AcpBackgroundProbeOptions) {
|
|
175
|
+
this.load = options.load;
|
|
176
|
+
this.resetIdle = options.resetIdle;
|
|
177
|
+
this.emitNotice = options.emit;
|
|
178
|
+
this.timeoutMs = options.timeoutMs ?? BACKGROUND_TIMEOUT_MS;
|
|
179
|
+
this.firstDelayMs = options.firstDelayMs ?? BACKGROUND_FIRST_DELAY_MS;
|
|
180
|
+
this.maxDelayMs = options.maxDelayMs ?? BACKGROUND_MAX_DELAY_MS;
|
|
181
|
+
this.now = options.now ?? Date.now;
|
|
182
|
+
this.schedule = options.schedule ?? defaultSchedule;
|
|
183
|
+
this.cancel = options.cancel ?? defaultCancel;
|
|
184
|
+
this.delay = this.firstDelayMs;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Watch for the delayed answer to a turn that spawned background work. */
|
|
188
|
+
start(baseline: string): void {
|
|
189
|
+
this.baseline = baseline.trim();
|
|
190
|
+
this.pendingNotice = null;
|
|
191
|
+
// A previous probe's load can still be running. Queue this turn instead of
|
|
192
|
+
// interleaving two replays; the queued baseline starts when that load ends.
|
|
193
|
+
if (this.inFlight) {
|
|
194
|
+
this.queued = this.baseline;
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
this.stopped = false;
|
|
198
|
+
this.deadline = this.now() + this.timeoutMs;
|
|
199
|
+
this.delay = this.firstDelayMs;
|
|
200
|
+
this.failures = 0;
|
|
201
|
+
this.arm();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
stop(): void {
|
|
205
|
+
this.stopped = true;
|
|
206
|
+
this.queued = null;
|
|
207
|
+
this.pendingNotice = null;
|
|
208
|
+
if (this.timer !== null) {
|
|
209
|
+
this.cancel(this.timer);
|
|
210
|
+
this.timer = null;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Swallow one replay notification. Returns true when the notification is part
|
|
216
|
+
* of a probe and must not reach the live session: a replay repeats the whole
|
|
217
|
+
* conversation, so folding it would open rows the operator already has.
|
|
218
|
+
*/
|
|
219
|
+
consume(method: string, params: Record<string, unknown>): boolean {
|
|
220
|
+
if (!this.collecting || method !== 'session/update') return false;
|
|
221
|
+
const update = params.update;
|
|
222
|
+
if (!update || typeof update !== 'object') return true;
|
|
223
|
+
const record = update as Record<string, unknown>;
|
|
224
|
+
const kind = typeof record.sessionUpdate === 'string' ? record.sessionUpdate : '';
|
|
225
|
+
if (kind === 'user_message_chunk') {
|
|
226
|
+
// A new user turn starts a new assistant segment. Everything before it is
|
|
227
|
+
// history this probe does not need.
|
|
228
|
+
this.collecting.text = '';
|
|
229
|
+
this.collecting.sawUser = true;
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
if (kind === 'agent_message_chunk') {
|
|
233
|
+
this.collecting.text += acpContentText(record.content);
|
|
234
|
+
}
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private arm(): void {
|
|
239
|
+
if (this.stopped) return;
|
|
240
|
+
this.timer = this.schedule(() => {
|
|
241
|
+
this.timer = null;
|
|
242
|
+
void this.probe();
|
|
243
|
+
}, this.delay);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private async probe(): Promise<void> {
|
|
247
|
+
if (this.stopped || this.inFlight) return;
|
|
248
|
+
this.inFlight = true;
|
|
249
|
+
this.collecting = { text: '', sawUser: false };
|
|
250
|
+
this.resetIdle();
|
|
251
|
+
let failed = false;
|
|
252
|
+
try {
|
|
253
|
+
await this.load();
|
|
254
|
+
} catch {
|
|
255
|
+
// The agent may be gone or refuse `session/load`; a probe never reports
|
|
256
|
+
// that as a turn failure. Stop rather than spinning against it.
|
|
257
|
+
failed = true;
|
|
258
|
+
} finally {
|
|
259
|
+
this.inFlight = false;
|
|
260
|
+
}
|
|
261
|
+
const segment = this.collecting;
|
|
262
|
+
this.collecting = null;
|
|
263
|
+
|
|
264
|
+
if (this.stopped) {
|
|
265
|
+
// A new prompt or a Stop already claimed the session; its own request is
|
|
266
|
+
// what flushes the agent's held update.
|
|
267
|
+
} else if (failed) {
|
|
268
|
+
// A parked permission can outlive the load's request budget. Retry a few
|
|
269
|
+
// times so approving the dialog still lets the notice through; a child
|
|
270
|
+
// that really exited is stopped by `stop()` from the session's exit hook.
|
|
271
|
+
this.failures += 1;
|
|
272
|
+
if (this.failures >= 3 || this.now() >= this.deadline) {
|
|
273
|
+
this.stopped = true;
|
|
274
|
+
} else {
|
|
275
|
+
this.delay = Math.min(this.delay * 2, this.maxDelayMs);
|
|
276
|
+
this.arm();
|
|
277
|
+
}
|
|
278
|
+
} else {
|
|
279
|
+
this.failures = 0;
|
|
280
|
+
// A replay that never reached a user chunk cannot be placed in the
|
|
281
|
+
// conversation, so it is not a notice.
|
|
282
|
+
const notice = segment?.sawUser ? backgroundNotice(this.baseline, segment.text) : null;
|
|
283
|
+
if (notice !== null) {
|
|
284
|
+
// The agent may still be writing: the first replay of a growing answer
|
|
285
|
+
// is a PREFIX. Emit only once a replay repeats the same text, which is
|
|
286
|
+
// the only completion signal ACP gives a replay.
|
|
287
|
+
const stable = notice === this.pendingNotice;
|
|
288
|
+
this.pendingNotice = notice;
|
|
289
|
+
if (stable || this.now() >= this.deadline) {
|
|
290
|
+
this.emitNotice(notice);
|
|
291
|
+
this.stopped = true;
|
|
292
|
+
} else {
|
|
293
|
+
this.delay = this.firstDelayMs;
|
|
294
|
+
this.arm();
|
|
295
|
+
}
|
|
296
|
+
} else if (this.now() >= this.deadline) {
|
|
297
|
+
this.stopped = true;
|
|
298
|
+
} else {
|
|
299
|
+
this.delay = Math.min(this.delay * 2, this.maxDelayMs);
|
|
300
|
+
this.arm();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (this.queued !== null) {
|
|
305
|
+
const baseline = this.queued;
|
|
306
|
+
this.queued = null;
|
|
307
|
+
this.start(baseline);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function defaultSchedule(fn: () => void, ms: number): unknown {
|
|
313
|
+
const handle = setTimeout(fn, ms);
|
|
314
|
+
handle.unref?.();
|
|
315
|
+
return handle;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function defaultCancel(handle: unknown): void {
|
|
319
|
+
clearTimeout(handle as NodeJS.Timeout);
|
|
320
|
+
}
|
|
@@ -169,8 +169,8 @@ export class AcpProcess {
|
|
|
169
169
|
return verbs.resumeSession(this.call, sessionId, cwd, timeoutMs);
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
forkSession(sessionId: string, cwd: string, timeoutMs?: number) {
|
|
173
|
-
return verbs.forkSession(this.call, sessionId, cwd, timeoutMs);
|
|
172
|
+
forkSession(sessionId: string, cwd: string, timeoutMs?: number, target?: Record<string, unknown>) {
|
|
173
|
+
return verbs.forkSession(this.call, sessionId, cwd, timeoutMs, target);
|
|
174
174
|
}
|
|
175
175
|
|
|
176
176
|
listSessions(cwd?: string, timeoutMs?: number) {
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
* - `tool_call_update` arrives as a PATCH, so a `completed` update carrying
|
|
12
12
|
* only `status` must not blank the card it completes;
|
|
13
13
|
* - `messageId` is the ONLY message delimiter (ACP has no `message_end`), so a
|
|
14
|
-
* changed id must close the previous row instead of appending into it;
|
|
14
|
+
* changed id must close the previous row instead of appending into it; an
|
|
15
|
+
* ABSENT id (antigravity 1.5.3) must still yield one stable row per turn;
|
|
15
16
|
* - `kind` must reach `ToolCallData.type`, because omp's `ToolType` is what the
|
|
16
17
|
* renderers switch on;
|
|
17
18
|
* - `usage_update` exists on some agents only, so its absence must emit
|
|
@@ -33,6 +34,19 @@ const textChunk = (text: string, messageId = 'm1') => ({
|
|
|
33
34
|
messageId,
|
|
34
35
|
});
|
|
35
36
|
|
|
37
|
+
/** ACP v1 allows `messageId` to be absent; antigravity 1.5.3 sends none. */
|
|
38
|
+
const anonymousChunk = (text: string) => ({
|
|
39
|
+
sessionUpdate: 'agent_message_chunk',
|
|
40
|
+
content: { type: 'text', text },
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const messageIdOf = (frame: unknown): string | undefined => {
|
|
44
|
+
if (!frame || typeof frame !== 'object' || !('message' in frame)) return undefined;
|
|
45
|
+
const message = frame.message;
|
|
46
|
+
if (!message || typeof message !== 'object' || !('id' in message)) return undefined;
|
|
47
|
+
return typeof message.id === 'string' ? message.id : undefined;
|
|
48
|
+
};
|
|
49
|
+
|
|
36
50
|
describe('acp frame mapper — assistant text', () => {
|
|
37
51
|
test('accumulates chunks into ONE message rather than one row per delta', () => {
|
|
38
52
|
const mapper = new AcpFrameMapper();
|
|
@@ -43,6 +57,41 @@ describe('acp frame mapper — assistant text', () => {
|
|
|
43
57
|
expect(message.content[0]).toEqual({ type: 'text', text: 'hello world' });
|
|
44
58
|
});
|
|
45
59
|
|
|
60
|
+
test('chunks without a messageId still update ONE row', () => {
|
|
61
|
+
const mapper = new AcpFrameMapper();
|
|
62
|
+
const first = mapper.mapUpdate(anonymousChunk('hello '));
|
|
63
|
+
const second = mapper.mapUpdate(anonymousChunk('world'));
|
|
64
|
+
const firstId = messageIdOf(first[0]);
|
|
65
|
+
expect(typeof firstId).toBe('string');
|
|
66
|
+
expect(messageIdOf(second[0])).toBe(firstId);
|
|
67
|
+
// The final frame must carry that SAME id, or the client appends the
|
|
68
|
+
// completed answer beside the streamed one.
|
|
69
|
+
const ended = mapper.closeMessage();
|
|
70
|
+
expect(messageIdOf(ended[0])).toBe(firstId);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('a tool call before the first chunk keeps that identity too', () => {
|
|
74
|
+
const mapper = new AcpFrameMapper();
|
|
75
|
+
const opened = mapper.mapUpdate({
|
|
76
|
+
sessionUpdate: 'tool_call',
|
|
77
|
+
toolCallId: 't1',
|
|
78
|
+
title: 'Read',
|
|
79
|
+
status: 'in_progress',
|
|
80
|
+
});
|
|
81
|
+
const chunk = mapper.mapUpdate(anonymousChunk('done'));
|
|
82
|
+
const openedId = messageIdOf(opened[0]);
|
|
83
|
+
expect(typeof openedId).toBe('string');
|
|
84
|
+
expect(messageIdOf(chunk[0])).toBe(openedId);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('a new turn gets a fresh identity when the agent sends no id', () => {
|
|
88
|
+
const mapper = new AcpFrameMapper();
|
|
89
|
+
const first = mapper.mapUpdate(anonymousChunk('one'));
|
|
90
|
+
mapper.reset();
|
|
91
|
+
const second = mapper.mapUpdate(anonymousChunk('two'));
|
|
92
|
+
expect(messageIdOf(second[0])).not.toBe(messageIdOf(first[0]));
|
|
93
|
+
});
|
|
94
|
+
|
|
46
95
|
test('a changed messageId closes the previous row first', () => {
|
|
47
96
|
const mapper = new AcpFrameMapper();
|
|
48
97
|
mapper.mapUpdate(textChunk('first', 'm1'));
|
|
@@ -16,9 +16,11 @@
|
|
|
16
16
|
*
|
|
17
17
|
* Three ACP facts drive the state kept here:
|
|
18
18
|
*
|
|
19
|
-
* - **`messageId` delimits messages
|
|
20
|
-
*
|
|
21
|
-
*
|
|
19
|
+
* - **`messageId` delimits messages, but is OPTIONAL.** One answer is many
|
|
20
|
+
* chunks sharing an id (measured 6 chunks for a five-word answer, 168 for a
|
|
21
|
+
* long one); a changed id closes the row and opens the next. An agent that
|
|
22
|
+
* sends no id at all (measured on antigravity 1.5.3) gets one synthesized
|
|
23
|
+
* identity per turn, so its chunks still update ONE row.
|
|
22
24
|
* - **`tool_call_update` is a PATCH.** A `status:completed` update carries no
|
|
23
25
|
* `content`, so the card's body lives in the accumulated block; replacing it
|
|
24
26
|
* blanks the card at the exact moment it finishes.
|
|
@@ -144,11 +146,21 @@ export class AcpFrameMapper {
|
|
|
144
146
|
return [{ type: 'message_end', message }];
|
|
145
147
|
}
|
|
146
148
|
|
|
147
|
-
/**
|
|
149
|
+
/**
|
|
150
|
+
* The accumulated message in omp's wire shape.
|
|
151
|
+
*
|
|
152
|
+
* `messageId` is OPTIONAL in ACP v1, and an agent that omits it (measured on
|
|
153
|
+
* antigravity 1.5.3: every chunk carried only `sessionUpdate` + `content`)
|
|
154
|
+
* still needs one identity for the whole turn. Without it, `toChatMessage`
|
|
155
|
+
* minted `msg-<Date.now()>-ai` per frame, so every chunk after the first
|
|
156
|
+
* APPENDED a new row instead of updating the streaming one — the turn showed
|
|
157
|
+
* as duplicate bubbles and the final answer again at `message_end`.
|
|
158
|
+
*/
|
|
148
159
|
private snapshot(): Record<string, unknown> {
|
|
160
|
+
const id = this.messageId ??= `acp-${crypto.randomUUID()}`;
|
|
149
161
|
return {
|
|
150
162
|
role: 'assistant',
|
|
151
|
-
|
|
163
|
+
id,
|
|
152
164
|
content: this.blocks.map((block) => ({ ...block })),
|
|
153
165
|
};
|
|
154
166
|
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The one body field that broke every Claude turn against a strict gateway.
|
|
8
|
+
*
|
|
9
|
+
* Measured on kenari.id: any `/v1/messages` carrying `metadata.user_id` — the
|
|
10
|
+
* attribution object Claude Code adds — answered `400 the model's provider
|
|
11
|
+
* rejected the request. check the model id and request fields`, while the same
|
|
12
|
+
* body without it, or with `metadata: {}`, completed. `CLAUDE_CODE_EXTRA_BODY`
|
|
13
|
+
* is the documented top-level merge, so it is the one place that can empty the
|
|
14
|
+
* field without touching thinking, context management or tools.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { describe, expect, test } from 'bun:test';
|
|
18
|
+
import { withGatewayCompatibility } from '@/server/lib/harness/acp/gateway-env';
|
|
19
|
+
|
|
20
|
+
describe('withGatewayCompatibility', () => {
|
|
21
|
+
test('a custom Claude base URL gets an empty metadata object', () => {
|
|
22
|
+
const env = withGatewayCompatibility('claude', {
|
|
23
|
+
ANTHROPIC_BASE_URL: 'https://kenari.id',
|
|
24
|
+
ANTHROPIC_AUTH_TOKEN: 'kn',
|
|
25
|
+
});
|
|
26
|
+
expect(JSON.parse(env?.CLAUDE_CODE_EXTRA_BODY ?? '{}')).toEqual({ metadata: {} });
|
|
27
|
+
expect(env?.ANTHROPIC_BASE_URL).toBe('https://kenari.id');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('a direct Anthropic connection keeps its attribution', () => {
|
|
31
|
+
const env = { ANTHROPIC_BASE_URL: 'https://api.anthropic.com' };
|
|
32
|
+
expect(withGatewayCompatibility('claude', env)).toBe(env);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('another harness is untouched', () => {
|
|
36
|
+
const env = { ANTHROPIC_BASE_URL: 'https://kenari.id' };
|
|
37
|
+
expect(withGatewayCompatibility('omp-acp', env)).toBe(env);
|
|
38
|
+
expect(withGatewayCompatibility('claude', undefined)).toBeUndefined();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('an existing extra body is preserved, and its own metadata wins', () => {
|
|
42
|
+
const kept = withGatewayCompatibility('claude', {
|
|
43
|
+
ANTHROPIC_BASE_URL: 'https://gw',
|
|
44
|
+
CLAUDE_CODE_EXTRA_BODY: JSON.stringify({ temperature: 0.2 }),
|
|
45
|
+
});
|
|
46
|
+
expect(JSON.parse(kept?.CLAUDE_CODE_EXTRA_BODY ?? '{}')).toEqual({ temperature: 0.2, metadata: {} });
|
|
47
|
+
|
|
48
|
+
const own = withGatewayCompatibility('claude', {
|
|
49
|
+
ANTHROPIC_BASE_URL: 'https://gw',
|
|
50
|
+
CLAUDE_CODE_EXTRA_BODY: JSON.stringify({ metadata: { user_id: 'tenant' } }),
|
|
51
|
+
});
|
|
52
|
+
expect(JSON.parse(own?.CLAUDE_CODE_EXTRA_BODY ?? '{}')).toEqual({ metadata: { user_id: 'tenant' } });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('a malformed extra body is left for the user to fix', () => {
|
|
56
|
+
const env = { ANTHROPIC_BASE_URL: 'https://gw', CLAUDE_CODE_EXTRA_BODY: '{oops' };
|
|
57
|
+
expect(withGatewayCompatibility('claude', env)).toBe(env);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('a malformed base URL is not guessed at', () => {
|
|
61
|
+
const env = { ANTHROPIC_BASE_URL: 'not a url' };
|
|
62
|
+
expect(withGatewayCompatibility('claude', env)).toBe(env);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Env a Claude Code gateway needs to accept its requests.
|
|
8
|
+
*
|
|
9
|
+
* Claude Code puts an attribution string in the request body's
|
|
10
|
+
* `metadata.user_id` (`{"device_id":…,"account_uuid":…,"session_id":…}`). The
|
|
11
|
+
* Anthropic API accepts it, but a strict gateway rejects the whole turn:
|
|
12
|
+
* measured against kenari.id, every `/v1/messages` body carrying ANY
|
|
13
|
+
* `metadata.user_id` answered
|
|
14
|
+
*
|
|
15
|
+
* ```
|
|
16
|
+
* 400 the model's provider rejected the request. check the model id and request fields
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* while the same body without the field — and the same body with
|
|
20
|
+
* `metadata: {}` — completed. `CLAUDE_CODE_EXTRA_BODY` is merged into the top
|
|
21
|
+
* level of the request, so an empty metadata object replaces the attribution
|
|
22
|
+
* one without touching anything else (thinking, context management, tools).
|
|
23
|
+
*
|
|
24
|
+
* Only a CUSTOM base URL gets this: a direct Anthropic connection is welcome
|
|
25
|
+
* to the attribution. A body the user already controls is respected — an
|
|
26
|
+
* existing `metadata` or a non-object `CLAUDE_CODE_EXTRA_BODY` is left alone
|
|
27
|
+
* rather than overwritten.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import type { HarnessId } from '@/shared/lib/harness/catalog';
|
|
31
|
+
|
|
32
|
+
/** Anthropic's own host. Everything else under `ANTHROPIC_BASE_URL` is a gateway. */
|
|
33
|
+
const ANTHROPIC_HOST = 'api.anthropic.com';
|
|
34
|
+
|
|
35
|
+
/** The env a spawn should use, with the gateway compatibility applied. */
|
|
36
|
+
export function withGatewayCompatibility(
|
|
37
|
+
harness: HarnessId,
|
|
38
|
+
env: Record<string, string> | undefined,
|
|
39
|
+
): Record<string, string> | undefined {
|
|
40
|
+
if (harness !== 'claude' || !env) return env;
|
|
41
|
+
const baseUrl = env.ANTHROPIC_BASE_URL?.trim();
|
|
42
|
+
if (!baseUrl) return env;
|
|
43
|
+
let host: string;
|
|
44
|
+
try {
|
|
45
|
+
host = new URL(baseUrl).host;
|
|
46
|
+
} catch {
|
|
47
|
+
// A malformed URL is already a spawn failure the agent reports; guessing
|
|
48
|
+
// at its meaning here would only hide that message.
|
|
49
|
+
return env;
|
|
50
|
+
}
|
|
51
|
+
if (host === ANTHROPIC_HOST) return env;
|
|
52
|
+
|
|
53
|
+
let extra: Record<string, unknown> = {};
|
|
54
|
+
const existing = env.CLAUDE_CODE_EXTRA_BODY?.trim();
|
|
55
|
+
if (existing) {
|
|
56
|
+
try {
|
|
57
|
+
const parsed: unknown = JSON.parse(existing);
|
|
58
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return env;
|
|
59
|
+
// The user's own metadata is deliberate; an empty object is the only
|
|
60
|
+
// value this module may substitute.
|
|
61
|
+
if ('metadata' in parsed) return env;
|
|
62
|
+
extra = parsed as Record<string, unknown>;
|
|
63
|
+
} catch {
|
|
64
|
+
// Not JSON: the agent would ignore it, and replacing it would discard a
|
|
65
|
+
// value the user is still editing.
|
|
66
|
+
return env;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return { ...env, CLAUDE_CODE_EXTRA_BODY: JSON.stringify({ ...extra, metadata: {} }) };
|
|
70
|
+
}
|
|
@@ -21,10 +21,12 @@
|
|
|
21
21
|
* 2. **`tool_call_update` is a PATCH.** A `status:completed` update carries no
|
|
22
22
|
* `content`, so replacing the card blanks it at the exact moment it
|
|
23
23
|
* finishes. The mapper merges into the accumulated call.
|
|
24
|
-
* 3. **`messageId` delimits messages.** ACP has no
|
|
25
|
-
* many `agent_message_chunk` frames sharing an
|
|
26
|
-
* five-word answer, 168 for a long one). Without
|
|
27
|
-
* 168 rows.
|
|
24
|
+
* 3. **`messageId` delimits messages, when present.** ACP has no
|
|
25
|
+
* `message_end`: one answer is many `agent_message_chunk` frames sharing an
|
|
26
|
+
* id (measured 6 chunks for a five-word answer, 168 for a long one). Without
|
|
27
|
+
* the id, one answer becomes 168 rows. The id is optional in ACP v1, so an
|
|
28
|
+
* agent that omits it (antigravity 1.5.3) gets one synthesized identity for
|
|
29
|
+
* the turn rather than a fresh one per frame.
|
|
28
30
|
* 4. **`usage_update` is optional.** Only omp emits it; the mapper forwards it
|
|
29
31
|
* when present and emits nothing when absent, so the panels can say
|
|
30
32
|
* "not reported" instead of showing a fabricated number.
|
|
@@ -127,4 +127,24 @@ describe('applySelections', () => {
|
|
|
127
127
|
);
|
|
128
128
|
expect(writes).toEqual([]);
|
|
129
129
|
});
|
|
130
|
+
|
|
131
|
+
test('a matched model the agent refuses fails the spawn with its reason', async () => {
|
|
132
|
+
// dsh answered `unknown model option` when its configured provider was no
|
|
133
|
+
// longer in the live list. Swallowing it ran the turn on the agent's own
|
|
134
|
+
// default provider, and the first prompt's error named neither the model nor
|
|
135
|
+
// the refused write.
|
|
136
|
+
await expect(applySelections(
|
|
137
|
+
{
|
|
138
|
+
harness: 'dsh',
|
|
139
|
+
configOptions: [option('sel-model', 'model', ['["kenari","qwen3-8-27b:free"]'])],
|
|
140
|
+
runModel: null,
|
|
141
|
+
setConfig: async () => {
|
|
142
|
+
throw new Error('Invalid params: unknown model option');
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
// dsh stores its selector as a stringified pair, so the configured
|
|
146
|
+
// default is a provider-less id. That is the spelling this write uses.
|
|
147
|
+
{ model: { provider: '', modelId: '["kenari","qwen3-8-27b:free"]' }, thinking: null, mode: null },
|
|
148
|
+
)).rejects.toThrow('dsh refused model "["kenari","qwen3-8-27b:free"]"');
|
|
149
|
+
});
|
|
130
150
|
});
|
|
@@ -103,6 +103,8 @@ export interface ConfigSelectionHost {
|
|
|
103
103
|
configOptions: AcpConfigOption[];
|
|
104
104
|
runModel: { provider: string; modelId: string } | null;
|
|
105
105
|
setConfig(configId: string, value: string | boolean): Promise<unknown>;
|
|
106
|
+
/** The harness id, so a refused write names the agent instead of "it". */
|
|
107
|
+
readonly harness?: string;
|
|
106
108
|
}
|
|
107
109
|
|
|
108
110
|
/**
|
|
@@ -110,8 +112,13 @@ export interface ConfigSelectionHost {
|
|
|
110
112
|
*
|
|
111
113
|
* A selector the agent does not publish is SKIPPED, never guessed: an agent with
|
|
112
114
|
* no thinking ladder must not have a level invented for it, and a model the
|
|
113
|
-
* agent does not list is not written.
|
|
114
|
-
*
|
|
115
|
+
* agent does not list is not written. That skip is about an OPTIONAL knob.
|
|
116
|
+
*
|
|
117
|
+
* The MODEL write is not optional: a chat whose model could not be applied runs
|
|
118
|
+
* on the agent's default provider. On dsh that was a silent switch to
|
|
119
|
+
* `deepseek-official` with no key, so the first prompt failed with a message
|
|
120
|
+
* that named neither the model nor the refused write. A matched model write
|
|
121
|
+
* that the agent rejects now fails the spawn with its own words.
|
|
115
122
|
*/
|
|
116
123
|
export async function applySelections(host: ConfigSelectionHost, wanted: ConfigSelections): Promise<void> {
|
|
117
124
|
const model = configOption(host.configOptions, 'model');
|
|
@@ -121,7 +128,16 @@ export async function applySelections(host: ConfigSelectionHost, wanted: ConfigS
|
|
|
121
128
|
const match = values.find((value) => value === exact) ?? values.find((value) => value === wanted.model?.modelId);
|
|
122
129
|
// `session/set_config_option` keys on the selector's ID, which an agent may
|
|
123
130
|
// name anything; the category only says what the selector MEANS.
|
|
124
|
-
if (match)
|
|
131
|
+
if (match) {
|
|
132
|
+
try {
|
|
133
|
+
await host.setConfig(model.id, match);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
throw new Error(
|
|
136
|
+
`${host.harness ?? 'The agent'} refused model "${match}": `
|
|
137
|
+
+ `${error instanceof Error ? error.message : String(error)}`,
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
125
141
|
}
|
|
126
142
|
const thinking = configOption(host.configOptions, 'thought_level');
|
|
127
143
|
if (thinking && wanted.thinking && acpOptionValues(thinking).includes(wanted.thinking)) {
|