@kabmo-labs/kabmo 0.24.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 +579 -510
- package/dist/client/static/js/index-bc9aa4mx.css +1 -0
- package/dist/client/static/js/{index-jcr5twtp.js → index-ga8cbpt4.js} +1072 -1174
- package/package.json +2 -4
- package/src/cli/lib/args.js +24 -1
- package/src/cli/lib/args.test.js +16 -0
- package/src/cli/lib/commands/session.js +233 -0
- package/src/cli/lib/commands/session.test.js +103 -0
- package/src/cli/main.js +1 -1
- package/src/client/components/common/ComposerPicker.tsx +3 -0
- package/src/client/components/common/code-surface/shiki.test.ts +82 -0
- package/src/client/components/common/code-surface/shiki.ts +28 -7
- package/src/client/components/common/session-actions-menu/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/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.tsx +36 -5
- 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.tsx +28 -35
- 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/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.tsx +4 -0
- package/src/client/components/workspace/chat-timeline/TimelineBody.tsx +3 -0
- package/src/client/components/workspace/chat-timeline/composer-dock-frame.test.tsx +155 -0
- package/src/client/components/workspace/chat-timeline/index.tsx +3 -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 +1 -1
- package/src/client/data/theme/terminal.test.ts +25 -0
- package/src/client/data/theme/terminal.ts +96 -79
- package/src/client/hooks/chat/composer/items.test.ts +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/prompt-text.test.ts +34 -1
- package/src/client/hooks/chat/timeline/prompt-text.ts +47 -20
- package/src/client/hooks/chat/timeline/send-execute.test.ts +39 -0
- package/src/client/hooks/chat/timeline/send-helpers.ts +30 -1
- package/src/client/hooks/chat/timeline/send-steer.test.ts +3 -1
- package/src/client/hooks/chat/timeline/send.test-util.ts +7 -1
- package/src/client/hooks/chat/timeline/send.test.ts +3 -1
- package/src/client/hooks/chat/timeline/send.ts +51 -50
- package/src/client/hooks/chat/timeline/use-fork-session.ts +49 -0
- 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/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 +17 -11
- package/src/server/index.ts +2 -0
- package/src/server/lib/auth/guard.ts +10 -0
- package/src/server/lib/btw/session-copy.server.ts +6 -44
- package/src/server/lib/btw/session-copy.test.ts +2 -1
- package/src/server/lib/db/schema.ts +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/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/gateway-env.test.ts +64 -0
- package/src/server/lib/harness/acp/gateway-env.ts +70 -0
- package/src/server/lib/harness/acp/prompt.test.ts +20 -0
- package/src/server/lib/harness/acp/prompt.ts +19 -3
- package/src/server/lib/harness/acp/session-background.test.ts +129 -0
- 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 +27 -1
- package/src/server/lib/harness/acp/session-listeners.ts +31 -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 +23 -1
- package/src/server/lib/harness/acp/session.ts +59 -59
- package/src/server/lib/harness/acp/turn.test.ts +81 -0
- package/src/server/lib/harness/acp/turn.ts +26 -9
- package/src/server/lib/harness/acp/verbs.ts +12 -2
- package/src/server/lib/harness/launch.ts +3 -1
- 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-worker.test.ts +153 -0
- package/src/server/lib/harness/update-worker.ts +197 -0
- package/src/server/lib/lifecycle/paths.ts +23 -0
- package/src/server/lib/omp/rpc/manager.ts +5 -5
- package/src/server/lib/omp/rpc/prewarm-pool.ts +12 -3
- package/src/server/lib/omp/rpc/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 +47 -0
- package/src/server/lib/omp/session/delete.server.ts +5 -0
- package/src/server/lib/omp/session/rewind-file.ts +39 -21
- package/src/server/lib/omp/session/sidebar-data.server.ts +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/harness/add-provider.test.ts +9 -1
- package/src/server/routes/harness/import-action.ts +4 -0
- package/src/server/routes/harness/import.test.ts +1 -1
- package/src/server/routes/harness/index.ts +5 -1
- package/src/server/routes/harness/update-status.ts +26 -0
- package/src/server/routes/harness/update.test.ts +119 -0
- package/src/server/routes/harness/update.ts +59 -147
- package/src/server/routes/sessions/answer.ts +24 -0
- package/src/server/routes/sessions/batch-delete.ts +11 -5
- package/src/server/routes/sessions/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/terminal/ws.ts +3 -3
- package/src/shared/lib/chat/composer/client.ts +31 -2
- package/src/shared/lib/chat/composer/delegation.test.ts +93 -0
- package/src/shared/lib/chat/composer/delegation.ts +206 -0
- package/src/shared/lib/chat/notice-text.ts +1 -1
- package/src/shared/lib/chat/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/provider-facts.test.ts +27 -0
- package/src/shared/lib/harness/providers.ts +35 -1
- package/src/shared/lib/harness/update-job.ts +33 -0
- package/src/shared/lib/harness/update-plan.test.ts +21 -6
- package/src/shared/lib/harness/update-plan.ts +12 -4
- package/src/shared/lib/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/theme.test.ts +1 -1
- package/src/shared/lib/workspace/terminal/input.test.ts +27 -0
- package/src/shared/lib/workspace/terminal/input.ts +33 -0
- package/src/shared/lib/workspace/terminal/keys.test.ts +1 -1
- package/src/shared/lib/workspace/terminal/keys.ts +7 -7
- package/src/shared/types/chat.ts +1 -1
- package/src/shared/types/harness-update.ts +65 -0
- 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/harness-table.test-util.ts +15 -1
- package/dist/client/static/js/index-z3w7n85q.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) {
|
|
@@ -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
|
+
}
|
|
@@ -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)) {
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A turn that spawns a subagent is not over when `session/prompt` resolves:
|
|
8
|
+
* antigravity holds the subagent's completion until the next request, so the
|
|
9
|
+
* session probes `session/load` and delivers the delayed answer as a follow-up
|
|
10
|
+
* run. This suite drives that wiring through the real probe and the session's
|
|
11
|
+
* own notification seam.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
15
|
+
import { isolateDb, releaseDb } from '@/test-support/isolated-db';
|
|
16
|
+
import { mountSession, notify } from '@/server/lib/harness/acp/session.test-util';
|
|
17
|
+
import { AcpBackgroundProbe, deliverBackgroundNotice } from '@/server/lib/harness/acp/background-work';
|
|
18
|
+
import { extractTextFromContent } from '@/shared/lib/omp/session/mapper';
|
|
19
|
+
|
|
20
|
+
interface FakeTimer {
|
|
21
|
+
fn: () => void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function settle(): Promise<void> {
|
|
25
|
+
for (let i = 0; i < 8; i += 1) await Promise.resolve();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('ACP background subagent notice', () => {
|
|
29
|
+
beforeEach(async () => {
|
|
30
|
+
await isolateDb();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
afterEach(async () => {
|
|
34
|
+
await releaseDb();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('a session/load replay delivers the answer past the holding message', async () => {
|
|
38
|
+
const { session, events, turn, setBackground } = mountSession();
|
|
39
|
+
const scheduled: FakeTimer[] = [];
|
|
40
|
+
const delivered: string[] = [];
|
|
41
|
+
const probe = new AcpBackgroundProbe({
|
|
42
|
+
load: async () => {
|
|
43
|
+
notify(session, { sessionUpdate: 'user_message_chunk', content: { type: 'text', text: 'buat gambar' } });
|
|
44
|
+
notify(session, {
|
|
45
|
+
sessionUpdate: 'agent_message_chunk',
|
|
46
|
+
content: { type: 'text', text: 'Sedang membuat gambar…' },
|
|
47
|
+
});
|
|
48
|
+
notify(session, {
|
|
49
|
+
sessionUpdate: 'agent_message_chunk',
|
|
50
|
+
content: { type: 'text', text: 'Berikut adalah gambar gunung.' },
|
|
51
|
+
});
|
|
52
|
+
return {};
|
|
53
|
+
},
|
|
54
|
+
resetIdle: () => {},
|
|
55
|
+
emit: (text) => {
|
|
56
|
+
delivered.push(text);
|
|
57
|
+
deliverBackgroundNotice(session, text);
|
|
58
|
+
},
|
|
59
|
+
firstDelayMs: 10,
|
|
60
|
+
schedule: (fn) => {
|
|
61
|
+
const timer: FakeTimer = { fn };
|
|
62
|
+
scheduled.push(timer);
|
|
63
|
+
return timer;
|
|
64
|
+
},
|
|
65
|
+
cancel: () => {},
|
|
66
|
+
});
|
|
67
|
+
setBackground(probe);
|
|
68
|
+
|
|
69
|
+
await session.send({ type: 'prompt', message: 'buat gambar' });
|
|
70
|
+
await turn.started;
|
|
71
|
+
notify(session, {
|
|
72
|
+
sessionUpdate: 'tool_call',
|
|
73
|
+
toolCallId: 'sub-1',
|
|
74
|
+
name: 'invoke_subagent',
|
|
75
|
+
title: 'Run invoke_subagent',
|
|
76
|
+
status: 'in_progress',
|
|
77
|
+
rawInput: { Prompt: 'gambar gunung' },
|
|
78
|
+
});
|
|
79
|
+
notify(session, {
|
|
80
|
+
sessionUpdate: 'agent_message_chunk',
|
|
81
|
+
content: { type: 'text', text: 'Sedang membuat gambar…' },
|
|
82
|
+
});
|
|
83
|
+
turn.finish({ stopReason: 'end_turn' });
|
|
84
|
+
await settle();
|
|
85
|
+
|
|
86
|
+
expect(scheduled).toHaveLength(1);
|
|
87
|
+
scheduled[0]?.fn();
|
|
88
|
+
await settle();
|
|
89
|
+
// A first sighting is only a prefix; a repeated replay completes it.
|
|
90
|
+
expect(delivered).toEqual([]);
|
|
91
|
+
expect(scheduled).toHaveLength(2);
|
|
92
|
+
scheduled[1]?.fn();
|
|
93
|
+
await settle();
|
|
94
|
+
|
|
95
|
+
// The replay's holding message is swallowed; only its continuation lands.
|
|
96
|
+
expect(delivered).toEqual(['Berikut adalah gambar gunung.']);
|
|
97
|
+
const ends = events.filter((event) => event.type === 'message_end');
|
|
98
|
+
const texts = ends.map((event) => {
|
|
99
|
+
const message = event.message;
|
|
100
|
+
if (!message || typeof message !== 'object' || !('content' in message)) return '';
|
|
101
|
+
return extractTextFromContent(message.content);
|
|
102
|
+
});
|
|
103
|
+
expect(texts.filter((text) => text === 'Sedang membuat gambar…')).toHaveLength(1);
|
|
104
|
+
expect(texts).toContain('Berikut adalah gambar gunung.');
|
|
105
|
+
// The delayed answer arrives as a complete run of its own.
|
|
106
|
+
expect(events.filter((event) => event.type === 'agent_end')).toHaveLength(2);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('a new prompt stops the probe before the agent flushes the notice itself', async () => {
|
|
110
|
+
const { session, turn, setBackground } = mountSession();
|
|
111
|
+
let stopped = 0;
|
|
112
|
+
setBackground({
|
|
113
|
+
start: () => {},
|
|
114
|
+
stop: () => {
|
|
115
|
+
stopped += 1;
|
|
116
|
+
},
|
|
117
|
+
consume: () => false,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
await session.send({ type: 'prompt', message: 'satu' });
|
|
121
|
+
await turn.started;
|
|
122
|
+
turn.finish({ stopReason: 'end_turn' });
|
|
123
|
+
await settle();
|
|
124
|
+
|
|
125
|
+
const stopsBefore = stopped;
|
|
126
|
+
await session.send({ type: 'prompt', message: 'dua' });
|
|
127
|
+
expect(stopped).toBe(stopsBefore + 1);
|
|
128
|
+
});
|
|
129
|
+
});
|