Open History to see the last commit
1import { test, expect } from './fixtures'
2
3/**
4 * Claude signs each thinking block against the blocks before it. When
5 * web_search runs inside one response, a follow-up request that replays the
6 * turn as "thinking, thinking, text, server_tool_use, result" fails with
7 * "thinking blocks in the latest assistant message cannot be modified".
8 * `/api/anthropic-thinking-order-wire` scripts that response and returns what
9 * the wire and the client saw.
10 */
11test.describe('anthropic — signed thinking order around web_search', () => {
12 test('replay, client tools, and the interrupt view keep the order', async ({
13 request,
14 }) => {
15 const response = await request.post('/api/anthropic-thinking-order-wire')
16 expect(response.ok()).toBe(true)
17 const result = (await response.json()) as {
18 ok: boolean
19 error?: string
20 replayedBlocks: Array<string>
21 clientToolRequests: Array<string>
22 viewAfterInterrupt: Array<Array<string>>
23 }
24 if (!result.ok) throw new Error(`Route failed: ${result.error}`)
25
26 // The second request replays the first turn in the order Claude signed it.
27 expect(result.replayedBlocks).toEqual([
28 'thinking',
29 'server_tool_use',
30 'web_search_tool_result',
31 'thinking',
32 'text',
33 ])
34
35 // Only the real client tool waits on the client. web_search already ran.
36 expect(result.clientToolRequests).toEqual(['toolu_pick'])
37
38 // After the interrupt snapshot the client still shows one message.
39 expect(result.viewAfterInterrupt).toEqual([
40 ['thinking', 'tool-call', 'thinking', 'text', 'tool-call'],
41 ])
42 })
43
44 test('a client tool answer keeps its order through the client and the wire', async ({
45 request,
46 }) => {
47 const response = await request.post('/api/anthropic-thinking-order-wire')
48 expect(response.ok()).toBe(true)
49 const result = (await response.json()) as {
50 ok: boolean
51 error?: string
52 clientToolView: Array<Array<string>>
53 clientToolReplay: Array<string>
54 }
55 if (!result.ok) throw new Error(`Route failed: ${result.error}`)
56
57 // The client shows the answer in the order Claude sent it.
58 expect(result.clientToolView).toEqual([
59 ['thinking', 'tool-call', 'thinking', 'tool-call'],
60 ])
61
62 // After the client sends both tool results, the next request replays the
63 // answer in the order Claude signed it.
64 expect(result.clientToolReplay).toEqual([
65 'thinking:sig-c',
66 'tool_use:toolu_stockholm',
67 'thinking:sig-d',
68 'tool_use:toolu_oslo',
69 ])
70 })
71})