Open History to see the last commit
1import { test, expect } from './fixtures'
2
3/**
4 * Issue #604 regression test — `webFetchTool()` mixed with a user client
5 * tool.
6 *
7 * Pre-fix, when Claude returned a client `tool_use` followed by a
8 * `server_tool_use` (web_fetch) in the same streaming response, the server
9 * tool's `input_json_delta`s appended onto the client tool's input buffer.
10 * The agent loop's downstream `JSON.parse` then threw on the concatenated
11 * JSON, so any user calling `chat({ tools: [myTool, webFetchTool()] })`
12 * could hit "Failed to parse tool arguments as JSON" the moment Claude
13 * called both in one turn.
14 *
15 * The bug shape can't be reproduced through aimock's built-in handlers —
16 * aimock has no concept of `server_tool_use` blocks. The
17 * `/anthropic-bug-test` mount in `global-setup.ts` hand-crafts the exact
18 * SSE Claude emits, and `/api/anthropic-bug-test` runs the Anthropic
19 * adapter against it with a real `webFetchTool()` in the tools array.
20 */
21test.describe('anthropic — webFetchTool() streaming (#604)', () => {
22 test('user tool + webFetchTool() in the same turn completes cleanly', async ({
23 request,
24 }) => {
25 const res = await request.post('/api/anthropic-bug-test')
26 expect(res.ok()).toBe(true)
27 const { chunks, error } = (await res.json()) as {
28 chunks: Array<Record<string, unknown>>
29 error: string | null
30 }
31
32 // The bug threw `Failed to parse tool arguments as JSON: {...}{"url":...}`
33 // in the agent loop. The fix means no error reaches the consumer.
34 expect(error).toBeNull()
35
36 const toolCallStarts = chunks.filter((c) => c.type === 'TOOL_CALL_START')
37 // The adapter's `TOOL_CALL_END` carries the parsed `input` — where the
38 // pre-fix `JSON.parse(concatenatedArgs)` blew up.
39 const toolCallArgEnds = chunks.filter(
40 (c) =>
41 c.type === 'TOOL_CALL_END' &&
42 (c as { input?: unknown }).input !== undefined,
43 )
44 const byId = (
45 list: Array<Record<string, unknown>>,
46 id: string,
47 ): Record<string, unknown> | undefined =>
48 list.find((c) => (c as { toolCallId?: string }).toolCallId === id)
49
50 // Two tool calls surface: the client `lookup_weather` and the
51 // Anthropic-executed `web_fetch`. The latter now streams as a
52 // provider-executed tool call (#839) so its result can be replayed into
53 // later turns — it is NOT routed to client-side execution.
54 expect(toolCallStarts).toHaveLength(2)
55 expect(byId(toolCallStarts, 'toolu_client_weather')).toMatchObject({
56 toolCallId: 'toolu_client_weather',
57 toolCallName: 'lookup_weather',
58 })
59
60 // The web_fetch start is flagged provider-executed (in metadata) and
61 // carries the raw server tool result the adapter replays verbatim.
62 expect(byId(toolCallStarts, 'srvtoolu_web_fetch')).toMatchObject({
63 toolCallId: 'srvtoolu_web_fetch',
64 toolCallName: 'web_fetch',
65 metadata: {
66 providerExecuted: true,
67 anthropic: { resultBlockType: 'web_fetch_tool_result' },
68 },
69 })
70
71 // Both tools surface a clean parsed `input` on TOOL_CALL_END. The client
72 // tool's args must still be the Berlin payload — not the pre-fix
73 // concatenated `{"location":"Berlin"}{"url":"..."}` — which is the
74 // regression this suite guards.
75 expect(toolCallArgEnds).toHaveLength(2)
76 expect(byId(toolCallArgEnds, 'toolu_client_weather')).toMatchObject({
77 toolCallId: 'toolu_client_weather',
78 input: { location: 'Berlin' },
79 })
80 expect(byId(toolCallArgEnds, 'srvtoolu_web_fetch')).toMatchObject({
81 toolCallId: 'srvtoolu_web_fetch',
82 input: { url: 'https://example.com' },
83 })
84
85 // Run completes cleanly through the agent loop's follow-up turn.
86 expect(chunks.some((c) => c.type === 'RUN_FINISHED')).toBe(true)
87 })
88})
89
90test.describe('anthropic — signed thinking replay (#910)', () => {
91 test('keeps thinking blocks on the correct side of server tool calls', async ({
92 request,
93 }) => {
94 const res = await request.post(
95 '/api/anthropic-bug-test?case=thinking-order',
96 )
97 expect(res.ok()).toBe(true)
98 const { chunks, error } = (await res.json()) as {
99 chunks: Array<Record<string, unknown>>
100 error: string | null
101 }
102
103 // The mounted Anthropic endpoint rejects the request unless the replayed
104 // content blocks are thinking(A) → server tool/result → thinking(B) →
105 // local tool. This mirrors Anthropic's signed-thinking validation.
106 expect(error).toBeNull()
107 expect(chunks.some((chunk) => chunk.type === 'RUN_FINISHED')).toBe(true)
108 })
109})