Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

compaction-wire.spec.ts

150 lines5.4 KB

1import { expect, test } from './fixtures'
2​
3/**
4 * Wire-format verification for `withCompaction`. Drives `/api/compaction-wire`,
5 * which sends a long history through `chat()` with a small `maxTokens` and
6 * captures the outgoing SDK request. The captured body must show the oldest
7 * message evicted, the compaction note injected, and the recent tail preserved.
8 */
9test.describe('withCompaction — wire format', () => {
10 test('evicts the old head, keeps the recent tail, injects a note', async ({
11 request,
12 }) => {
13 const response = await request.post('/api/compaction-wire')
14 expect(response.ok()).toBe(true)
15 const result = (await response.json()) as {
16 ok: boolean
17 error?: string
18 firstRequestBody: unknown
19 secondRequestBody: unknown
20 canonicalMessages: unknown
21 compactionCount: number
22 }
23 if (!result.ok) throw new Error(`Route failed: ${result.error}`)
24​
25 const wire = JSON.stringify(result.firstRequestBody)
26 // Recent tail is preserved verbatim.
27 expect(wire).toContain('KEEP_ME_LAST')
28 // The dropped head was replaced by the eviction note.
29 expect(wire).toContain('omitted to save context')
30 // The oldest message is gone.
31 expect(wire).not.toContain('SECRET_ALPHA_ONE')
32​
33 // Persistence keeps the canonical transcript, while a later request reuses
34 // the compacted checkpoint without compacting the same prefix again.
35 expect(JSON.stringify(result.canonicalMessages)).toContain(
36 'SECRET_ALPHA_ONE',
37 )
38 expect(JSON.stringify(result.secondRequestBody)).not.toContain(
39 'SECRET_ALPHA_ONE',
40 )
41 expect(result.compactionCount).toBe(1)
42 })
43​
44 test('clearToolResults stubs old tool output and keeps the recent one', async ({
45 request,
46 }) => {
47 const response = await request.post('/api/compaction-wire?strategy=clear')
48 expect(response.ok()).toBe(true)
49 const result = (await response.json()) as {
50 ok: boolean
51 error?: string
52 firstRequestBody: unknown
53 }
54 if (!result.ok) throw new Error(`Route failed: ${result.error}`)
55​
56 const wire = JSON.stringify(result.firstRequestBody)
57 // The most recent tool result is preserved verbatim.
58 expect(wire).toContain('KEEP_TOOL_BETA')
59 // The old tool result content is replaced by the stub.
60 expect(wire).toContain('tool output cleared')
61 // The old tool result content is gone.
62 expect(wire).not.toContain('SECRET_TOOL_ALPHA')
63 })
64​
65 test('an empty summary fails the compaction and sends no request', async ({
66 request,
67 }) => {
68 const response = await request.post(
69 '/api/compaction-wire?strategy=empty-summary',
70 )
71 expect(response.ok()).toBe(true)
72 const result = (await response.json()) as {
73 ok: boolean
74 error?: string
75 requestCount?: number
76 }
77​
78 // The history was not replaced with an empty summary and sent on.
79 expect(result.ok).toBe(false)
80 expect(result.error).toBe('The summarizer returned an empty summary.')
81 expect(result.requestCount).toBe(0)
82 })
83​
84 test('native compaction posts to /responses/compact and replays its item', async ({
85 request,
86 }) => {
87 const response = await request.post('/api/compaction-wire?strategy=native')
88 expect(response.ok()).toBe(true)
89 const result = (await response.json()) as {
90 ok: boolean
91 error?: string
92 firstRequestBody: unknown
93 secondRequestBody: unknown
94 compactRequestBodies: Array<unknown>
95 compactionCount: number
96 }
97 if (!result.ok) throw new Error(`Route failed: ${result.error}`)
98​
99 // One compaction call got the whole history.
100 expect(result.compactRequestBodies).toHaveLength(1)
101 expect(JSON.stringify(result.compactRequestBodies[0])).toContain(
102 'SECRET_ALPHA_ONE',
103 )
104 // The next model call sends the compacted history, with the encrypted
105 // item as it came back.
106 expect(result.firstRequestBody).toMatchObject({
107 input: [
108 { type: 'message', role: 'user' },
109 { type: 'compaction', id: 'cmp_e2e', encrypted_content: 'SEALED_E2E' },
110 ],
111 })
112 expect(JSON.stringify(result.firstRequestBody)).not.toContain(
113 'SECRET_ALPHA_ONE',
114 )
115 // The second run reuses the checkpoint and does not compact again.
116 expect(JSON.stringify(result.secondRequestBody)).toContain('SEALED_E2E')
117 expect(result.compactionCount).toBe(1)
118 })
119​
120 test('native compaction with no compaction item falls back to the strategy', async ({
121 request,
122 }) => {
123 const response = await request.post(
124 '/api/compaction-wire?strategy=native-empty',
125 )
126 expect(response.ok()).toBe(true)
127 const result = (await response.json()) as {
128 ok: boolean
129 error?: string
130 firstRequestBody: unknown
131 compactRequestBodies: Array<unknown>
132 compactionErrors: Array<string>
133 }
134 if (!result.ok) throw new Error(`Route failed: ${result.error}`)
135​
136 // Each run tries compact. A result with no item is not stored, so the
137 // next run tries again. evictOldest runs in its place.
138 expect(result.compactRequestBodies).toHaveLength(2)
139 expect(result.compactionErrors).toEqual([
140 'The compact result has no compaction item.',
141 'The compact result has no compaction item.',
142 ])
143 // evictOldest ran in its place.
144 const wire = JSON.stringify(result.firstRequestBody)
145 expect(wire).toContain('omitted to save context')
146 expect(wire).toContain('KEEP_ME_LAST')
147 expect(wire).not.toContain('SECRET_ALPHA_ONE')
148 expect(wire).not.toContain('SEALED_E2E')
149 })
150})