@librechat/agents 3.3.3 → 3.3.4

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.
Files changed (156) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +2 -1
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/graphs/Graph.cjs +342 -109
  4. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  5. package/dist/cjs/graphs/MultiAgentGraph.cjs +3 -1
  6. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  7. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +79 -6
  8. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
  9. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs +37 -10
  10. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs.map +1 -1
  11. package/dist/cjs/llm/google/utils/common.cjs +19 -7
  12. package/dist/cjs/llm/google/utils/common.cjs.map +1 -1
  13. package/dist/cjs/llm/invoke.cjs +64 -3
  14. package/dist/cjs/llm/invoke.cjs.map +1 -1
  15. package/dist/cjs/llm/openai/index.cjs +41 -5
  16. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  17. package/dist/cjs/llm/openai/utils/index.cjs +25 -4
  18. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
  19. package/dist/cjs/llm/openrouter/index.cjs +4 -1
  20. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  21. package/dist/cjs/main.cjs +19 -2
  22. package/dist/cjs/messages/cache.cjs +54 -33
  23. package/dist/cjs/messages/cache.cjs.map +1 -1
  24. package/dist/cjs/messages/contextPruning.cjs +17 -43
  25. package/dist/cjs/messages/contextPruning.cjs.map +1 -1
  26. package/dist/cjs/messages/core.cjs +315 -23
  27. package/dist/cjs/messages/core.cjs.map +1 -1
  28. package/dist/cjs/messages/format.cjs +218 -60
  29. package/dist/cjs/messages/format.cjs.map +1 -1
  30. package/dist/cjs/messages/index.cjs +1 -1
  31. package/dist/cjs/messages/prune.cjs +503 -127
  32. package/dist/cjs/messages/prune.cjs.map +1 -1
  33. package/dist/cjs/stream.cjs +7 -3
  34. package/dist/cjs/stream.cjs.map +1 -1
  35. package/dist/cjs/summarization/node.cjs +28 -14
  36. package/dist/cjs/summarization/node.cjs.map +1 -1
  37. package/dist/cjs/tools/ToolNode.cjs +54 -41
  38. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  39. package/dist/cjs/tools/toolOutputReferences.cjs +6 -0
  40. package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -1
  41. package/dist/cjs/utils/index.cjs +1 -1
  42. package/dist/cjs/utils/llm.cjs +1 -1
  43. package/dist/cjs/utils/llm.cjs.map +1 -1
  44. package/dist/cjs/utils/tokens.cjs +307 -20
  45. package/dist/cjs/utils/tokens.cjs.map +1 -1
  46. package/dist/cjs/utils/toolContent.cjs +1514 -0
  47. package/dist/cjs/utils/toolContent.cjs.map +1 -0
  48. package/dist/esm/agents/AgentContext.mjs +2 -1
  49. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  50. package/dist/esm/graphs/Graph.mjs +349 -116
  51. package/dist/esm/graphs/Graph.mjs.map +1 -1
  52. package/dist/esm/graphs/MultiAgentGraph.mjs +3 -1
  53. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  54. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +79 -6
  55. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
  56. package/dist/esm/llm/bedrock/utils/message_inputs.mjs +37 -10
  57. package/dist/esm/llm/bedrock/utils/message_inputs.mjs.map +1 -1
  58. package/dist/esm/llm/google/utils/common.mjs +19 -7
  59. package/dist/esm/llm/google/utils/common.mjs.map +1 -1
  60. package/dist/esm/llm/invoke.mjs +65 -5
  61. package/dist/esm/llm/invoke.mjs.map +1 -1
  62. package/dist/esm/llm/openai/index.mjs +41 -5
  63. package/dist/esm/llm/openai/index.mjs.map +1 -1
  64. package/dist/esm/llm/openai/utils/index.mjs +25 -4
  65. package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
  66. package/dist/esm/llm/openrouter/index.mjs +4 -1
  67. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  68. package/dist/esm/main.mjs +7 -7
  69. package/dist/esm/messages/cache.mjs +55 -34
  70. package/dist/esm/messages/cache.mjs.map +1 -1
  71. package/dist/esm/messages/contextPruning.mjs +17 -43
  72. package/dist/esm/messages/contextPruning.mjs.map +1 -1
  73. package/dist/esm/messages/core.mjs +305 -24
  74. package/dist/esm/messages/core.mjs.map +1 -1
  75. package/dist/esm/messages/format.mjs +218 -61
  76. package/dist/esm/messages/format.mjs.map +1 -1
  77. package/dist/esm/messages/index.mjs +1 -1
  78. package/dist/esm/messages/prune.mjs +500 -128
  79. package/dist/esm/messages/prune.mjs.map +1 -1
  80. package/dist/esm/stream.mjs +7 -3
  81. package/dist/esm/stream.mjs.map +1 -1
  82. package/dist/esm/summarization/node.mjs +28 -14
  83. package/dist/esm/summarization/node.mjs.map +1 -1
  84. package/dist/esm/tools/ToolNode.mjs +54 -41
  85. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  86. package/dist/esm/tools/toolOutputReferences.mjs +6 -0
  87. package/dist/esm/tools/toolOutputReferences.mjs.map +1 -1
  88. package/dist/esm/utils/index.mjs +1 -1
  89. package/dist/esm/utils/llm.mjs +1 -1
  90. package/dist/esm/utils/llm.mjs.map +1 -1
  91. package/dist/esm/utils/tokens.mjs +307 -21
  92. package/dist/esm/utils/tokens.mjs.map +1 -1
  93. package/dist/esm/utils/toolContent.mjs +1503 -0
  94. package/dist/esm/utils/toolContent.mjs.map +1 -0
  95. package/dist/types/graphs/Graph.d.ts +1 -1
  96. package/dist/types/llm/invoke.d.ts +27 -1
  97. package/dist/types/llm/openai/index.d.ts +4 -0
  98. package/dist/types/llm/openai/utils/index.d.ts +2 -0
  99. package/dist/types/llm/openrouter/index.d.ts +5 -0
  100. package/dist/types/messages/contextPruning.d.ts +2 -1
  101. package/dist/types/messages/core.d.ts +36 -0
  102. package/dist/types/messages/format.d.ts +6 -0
  103. package/dist/types/messages/prune.d.ts +16 -12
  104. package/dist/types/types/stream.d.ts +1 -1
  105. package/dist/types/utils/tokens.d.ts +7 -0
  106. package/dist/types/utils/toolContent.d.ts +107 -0
  107. package/package.json +1 -1
  108. package/src/__tests__/stream.eagerEventExecution.test.ts +74 -0
  109. package/src/agents/AgentContext.ts +1 -0
  110. package/src/graphs/Graph.ts +698 -200
  111. package/src/graphs/MultiAgentGraph.ts +6 -1
  112. package/src/graphs/__tests__/Graph.contextOverflow.test.ts +1128 -4
  113. package/src/llm/anthropic/utils/message_inputs.ts +158 -12
  114. package/src/llm/anthropic/utils/tool-id-normalization.test.ts +109 -0
  115. package/src/llm/bedrock/utils/message_inputs.test.ts +49 -1
  116. package/src/llm/bedrock/utils/message_inputs.ts +86 -13
  117. package/src/llm/google/utils/common.test.ts +75 -9
  118. package/src/llm/google/utils/common.ts +34 -3
  119. package/src/llm/invoke.test.ts +488 -0
  120. package/src/llm/invoke.ts +196 -2
  121. package/src/llm/openai/index.ts +106 -6
  122. package/src/llm/openai/utils/index.ts +109 -53
  123. package/src/llm/openai/utils/messages.test.ts +330 -1
  124. package/src/llm/openrouter/index.ts +12 -2
  125. package/src/llm/openrouter/reasoning.test.ts +314 -0
  126. package/src/messages/__tests__/observationMasking.test.ts +93 -2
  127. package/src/messages/cache.tail.test.ts +193 -0
  128. package/src/messages/cache.test.ts +113 -0
  129. package/src/messages/cache.ts +92 -60
  130. package/src/messages/contextPruning.test.ts +184 -0
  131. package/src/messages/contextPruning.ts +49 -42
  132. package/src/messages/core.ts +653 -40
  133. package/src/messages/ensureThinkingBlock.test.ts +49 -1
  134. package/src/messages/foldToollessToolBlocks.test.ts +163 -5
  135. package/src/messages/format.ts +363 -91
  136. package/src/messages/formatAgentMessages.test.ts +604 -14
  137. package/src/messages/formatAgentMessages.tools.test.ts +103 -0
  138. package/src/messages/prune.ts +996 -183
  139. package/src/specs/prune.test.ts +1083 -6
  140. package/src/specs/summarization-unit.test.ts +105 -0
  141. package/src/specs/tokens.test.ts +609 -32
  142. package/src/stream.ts +23 -15
  143. package/src/summarization/__tests__/node.test.ts +77 -0
  144. package/src/summarization/node.ts +46 -18
  145. package/src/tools/ToolNode.ts +147 -68
  146. package/src/tools/__tests__/ToolNode.onResultCompletion.test.ts +148 -1
  147. package/src/tools/__tests__/ToolNode.outputReferences.test.ts +112 -3
  148. package/src/tools/__tests__/annotateMessagesForLLM.test.ts +32 -0
  149. package/src/tools/__tests__/directToolHooks.test.ts +255 -7
  150. package/src/tools/toolOutputReferences.ts +6 -0
  151. package/src/types/stream.ts +1 -1
  152. package/src/utils/llm.test.ts +18 -0
  153. package/src/utils/llm.ts +4 -1
  154. package/src/utils/tokens.ts +586 -31
  155. package/src/utils/toolContent.test.ts +1432 -0
  156. package/src/utils/toolContent.ts +2707 -0
@@ -0,0 +1,1432 @@
1
+ import { spawnSync } from 'child_process';
2
+ import type { BaseMessage } from '@langchain/core/messages';
3
+ import {
4
+ compactToolContent,
5
+ getBoundedCacheControlledTextToolContent,
6
+ getBoundedSingleTextToolContent,
7
+ getComputerCallOutputScreenshot,
8
+ getToolContentCharLength,
9
+ isAtomicToolContentBlock,
10
+ serializeStructuredValue,
11
+ serializeStructuredValueBounded,
12
+ serializeToolContent,
13
+ serializeToolContentBounded,
14
+ } from './toolContent';
15
+
16
+ type ToolContent = BaseMessage['content'];
17
+
18
+ describe('toolContent', () => {
19
+ it('normalizes a small opaque array to provider-neutral text', () => {
20
+ const content = [{ type: 'json', rows: [{ id: 1 }] }] as ToolContent;
21
+
22
+ const result = compactToolContent(content, 10_000);
23
+
24
+ expect(result.changed).toBe(true);
25
+ expect(result.content).toBe(JSON.stringify(content));
26
+ });
27
+
28
+ it('does not trust a media-looking type without a valid media payload', () => {
29
+ const invalidBlocks = [
30
+ {
31
+ type: 'image_url',
32
+ rows: [{ id: 1, value: 'not an image' }],
33
+ },
34
+ { type: 'image', text: 'ordinary row' },
35
+ { type: 'file', content: [{ id: 1 }] },
36
+ { type: 'media', uri: 'not-a-supported-media-payload' },
37
+ { type: 'image_url', image_url: [{ id: 1 }] },
38
+ {
39
+ type: 'media',
40
+ mimeType: 'application/octet-stream',
41
+ data: [{ id: 1 }],
42
+ },
43
+ ];
44
+
45
+ for (const block of invalidBlocks) {
46
+ const content = [block] as ToolContent;
47
+ const result = compactToolContent(content, 10_000);
48
+
49
+ expect(result.changed).toBe(true);
50
+ expect(result.content).toBe(JSON.stringify(content));
51
+ }
52
+ });
53
+
54
+ it('recognizes native byte and resource payload shapes as atomic', () => {
55
+ const bytes = new Uint8Array([1, 2, 3]);
56
+
57
+ expect(
58
+ isAtomicToolContentBlock({
59
+ type: 'video',
60
+ video: { source: { bytes } },
61
+ })
62
+ ).toBe(true);
63
+ expect(
64
+ isAtomicToolContentBlock({
65
+ type: 'document',
66
+ document: {
67
+ source: { content: [{ type: 'text', text: 'page' }] },
68
+ },
69
+ })
70
+ ).toBe(true);
71
+ expect(
72
+ isAtomicToolContentBlock({
73
+ type: 'resource',
74
+ resource: { uri: 'file:///result.bin', blob: bytes },
75
+ })
76
+ ).toBe(true);
77
+ expect(
78
+ isAtomicToolContentBlock({
79
+ type: 'media',
80
+ mimeType: 'application/octet-stream',
81
+ data: new ArrayBuffer(8),
82
+ })
83
+ ).toBe(true);
84
+ expect(
85
+ isAtomicToolContentBlock({
86
+ type: 'file',
87
+ source_type: 'id',
88
+ id: 'file_123',
89
+ })
90
+ ).toBe(true);
91
+ expect(
92
+ isAtomicToolContentBlock({
93
+ type: 'computer_screenshot',
94
+ image_url: 'data:image/png;base64,AAAA',
95
+ })
96
+ ).toBe(true);
97
+ expect(
98
+ isAtomicToolContentBlock({
99
+ type: 'application/pdf',
100
+ data: 'JVBERi0xLjQ=',
101
+ })
102
+ ).toBe(true);
103
+ });
104
+
105
+ it('normalizes a direct object result to provider-neutral text', () => {
106
+ const content = { rows: [{ id: 1 }], count: 1 };
107
+
108
+ const result = compactToolContent(content, 10_000);
109
+
110
+ expect(result.changed).toBe(true);
111
+ expect(result.content).toBe(JSON.stringify(content));
112
+ });
113
+
114
+ it('keeps valid text and small media blocks intact', () => {
115
+ const image = {
116
+ type: 'image_url',
117
+ image_url: { url: 'https://example.com/chart.png' },
118
+ };
119
+ const content = [
120
+ { type: 'text', text: 'Query result chart' },
121
+ image,
122
+ ] as ToolContent;
123
+
124
+ const result = compactToolContent(content, 10_000);
125
+
126
+ expect(result.changed).toBe(false);
127
+ expect(result.content).toBe(content);
128
+ });
129
+
130
+ it('classifies each mixed content block once during compaction', () => {
131
+ const blocks: Array<Record<string, unknown>> = [];
132
+ const tracked = new Set<object>();
133
+ for (let i = 0; i < 100; i++) {
134
+ const text = { type: 'text', text: `row-${i}-${'x'.repeat(20)}` };
135
+ const image = {
136
+ type: 'image_url',
137
+ image_url: { url: `https://example.com/chart-${i}.png` },
138
+ };
139
+ const opaque = { type: 'json', row: { id: i } };
140
+ blocks.push(text, image, opaque);
141
+ tracked.add(text);
142
+ tracked.add(image);
143
+ tracked.add(opaque);
144
+ }
145
+ const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
146
+ let topLevelSafetyWalks = 0;
147
+ const descriptorSpy = jest
148
+ .spyOn(Object, 'getOwnPropertyDescriptor')
149
+ .mockImplementation((target, property) => {
150
+ if (property === 'toJSON' && tracked.has(target as object)) {
151
+ topLevelSafetyWalks++;
152
+ }
153
+ return getOwnPropertyDescriptor(target, property);
154
+ });
155
+
156
+ const compacted = (() => {
157
+ try {
158
+ return compactToolContent(blocks as ToolContent, 1_000);
159
+ } finally {
160
+ descriptorSpy.mockRestore();
161
+ }
162
+ })();
163
+
164
+ expect(topLevelSafetyWalks).toBe(blocks.length);
165
+ expect(compacted.changed).toBe(true);
166
+ expect(serializeToolContent(compacted.content).length).toBeLessThanOrEqual(
167
+ 1_000
168
+ );
169
+ });
170
+
171
+ it('does not reclassify oversized text blocks while serializing', () => {
172
+ const blocks: Array<Record<string, unknown>> = [];
173
+ const tracked = new Set<object>();
174
+ for (let i = 0; i < 300; i++) {
175
+ const block = {
176
+ type: 'text',
177
+ text: `row-${i}-${'x'.repeat(20)}`,
178
+ };
179
+ blocks.push(block);
180
+ tracked.add(block);
181
+ }
182
+ const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
183
+ let topLevelSafetyWalks = 0;
184
+ const descriptorSpy = jest
185
+ .spyOn(Object, 'getOwnPropertyDescriptor')
186
+ .mockImplementation((target, property) => {
187
+ if (property === 'toJSON' && tracked.has(target as object)) {
188
+ topLevelSafetyWalks++;
189
+ }
190
+ return getOwnPropertyDescriptor(target, property);
191
+ });
192
+
193
+ const compacted = (() => {
194
+ try {
195
+ return compactToolContent(blocks as ToolContent, 1_000);
196
+ } finally {
197
+ descriptorSpy.mockRestore();
198
+ }
199
+ })();
200
+
201
+ expect(topLevelSafetyWalks).toBe(blocks.length);
202
+ expect(compacted.changed).toBe(true);
203
+ expect(typeof compacted.content).toBe('string');
204
+ expect((compacted.content as string).length).toBeLessThanOrEqual(1_000);
205
+ });
206
+
207
+ it('includes forwarded text-block metadata in the compaction bound', () => {
208
+ const citations = 'A'.repeat(1_000_000);
209
+ const content = [
210
+ {
211
+ type: 'text',
212
+ text: 'x',
213
+ citations,
214
+ },
215
+ ] as ToolContent;
216
+
217
+ const compacted = compactToolContent(content, 2_000);
218
+ const serialized = serializeToolContent(compacted.content);
219
+
220
+ expect(compacted.changed).toBe(true);
221
+ expect(compacted.content).not.toBe(content);
222
+ expect(serialized.length).toBeLessThanOrEqual(2_000);
223
+ expect(serialized).not.toContain(citations);
224
+ expect(compacted.originalChars).toBeGreaterThan(1_000_000);
225
+ });
226
+
227
+ it('does not trust callable serialization hooks on the content array', () => {
228
+ const toJSON = jest.fn(() => ({
229
+ expanded: 'x'.repeat(100_000),
230
+ }));
231
+ const content = [{ type: 'text', text: 'safe' }] as ToolContent;
232
+ Object.defineProperty(content, 'toJSON', {
233
+ enumerable: true,
234
+ value: toJSON,
235
+ });
236
+
237
+ const compacted = compactToolContent(content, 100);
238
+
239
+ expect(toJSON).not.toHaveBeenCalled();
240
+ expect(compacted.changed).toBe(true);
241
+ expect(typeof compacted.content).toBe('string');
242
+ expect((compacted.content as string).length).toBeLessThanOrEqual(100);
243
+ });
244
+
245
+ it('uses one bounded fallback when nested opaque validation is exhausted', () => {
246
+ const opaque = Array.from({ length: 10_001 }, (_, index) => index);
247
+ const image = {
248
+ type: 'image_url',
249
+ image_url: { url: 'https://example.com/chart.png' },
250
+ };
251
+ const content: unknown[] = [];
252
+ for (let i = 0; i < 25; i++) {
253
+ content.push(image, opaque);
254
+ }
255
+ const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
256
+ let descriptorReads = 0;
257
+ const descriptorSpy = jest
258
+ .spyOn(Object, 'getOwnPropertyDescriptor')
259
+ .mockImplementation((target, property) => {
260
+ if (target === opaque && property !== 'toJSON') {
261
+ descriptorReads++;
262
+ }
263
+ return getOwnPropertyDescriptor(target, property);
264
+ });
265
+
266
+ const compacted = (() => {
267
+ try {
268
+ return compactToolContent(content as ToolContent, 1_000);
269
+ } finally {
270
+ descriptorSpy.mockRestore();
271
+ }
272
+ })();
273
+
274
+ expect(descriptorReads).toBeLessThan(400_000);
275
+ expect(compacted.changed).toBe(true);
276
+ expect(typeof compacted.content).toBe('string');
277
+ expect((compacted.content as string).length).toBeLessThanOrEqual(1_000);
278
+ });
279
+
280
+ it('preserves only the atomic occurrence that fits the budget', () => {
281
+ const image = {
282
+ type: 'image_url',
283
+ image_url: { url: 'https://example.com/chart.png' },
284
+ };
285
+ const imageChars = JSON.stringify(image).length;
286
+ const content = [
287
+ image,
288
+ image,
289
+ { type: 'text', text: 'x'.repeat(1_000) },
290
+ ] as ToolContent;
291
+
292
+ const compacted = compactToolContent(content, imageChars * 2 + 20);
293
+
294
+ expect(Array.isArray(compacted.content)).toBe(true);
295
+ expect(
296
+ (compacted.content as Exclude<ToolContent, string>).filter(
297
+ (block) => block === image
298
+ )
299
+ ).toHaveLength(1);
300
+ expect(serializeToolContent(compacted.content).length).toBeLessThanOrEqual(
301
+ imageChars * 2 + 20
302
+ );
303
+ });
304
+
305
+ it('accounts for non-finite numbers using their JSON null length', () => {
306
+ const content = [
307
+ {
308
+ type: 'image_url',
309
+ image_url: { url: 'https://example.com/chart.png' },
310
+ metadata: [NaN, NaN, NaN, NaN, NaN],
311
+ },
312
+ ] as ToolContent;
313
+ const maxChars = JSON.stringify(content).length - 1;
314
+
315
+ const compacted = compactToolContent(content, maxChars);
316
+
317
+ expect(compacted.changed).toBe(true);
318
+ expect(serializeToolContent(compacted.content).length).toBeLessThanOrEqual(
319
+ maxChars
320
+ );
321
+ });
322
+
323
+ it('fails closed without invoking traps on an array proxy prototype', () => {
324
+ let trapCalls = 0;
325
+ const proxyPrototype = new Proxy(Array.prototype, {
326
+ getOwnPropertyDescriptor() {
327
+ trapCalls++;
328
+ return undefined;
329
+ },
330
+ getPrototypeOf(target) {
331
+ trapCalls++;
332
+ return Reflect.getPrototypeOf(target);
333
+ },
334
+ });
335
+ const nested = ['safe'];
336
+ Object.setPrototypeOf(nested, proxyPrototype);
337
+
338
+ const compacted = compactToolContent(
339
+ [nested] as unknown as ToolContent,
340
+ 100
341
+ );
342
+
343
+ expect(trapCalls).toBe(0);
344
+ expect(compacted.changed).toBe(true);
345
+ expect(typeof compacted.content).toBe('string');
346
+ expect((compacted.content as string).length).toBeLessThanOrEqual(100);
347
+ });
348
+
349
+ it('does not invoke overridden byteLength accessors on typed views', () => {
350
+ let byteLengthCalls = 0;
351
+ const bytes = new Uint8Array([1]);
352
+ Object.defineProperty(bytes, 'byteLength', {
353
+ get() {
354
+ byteLengthCalls++;
355
+ return 10_000_000;
356
+ },
357
+ });
358
+
359
+ const compacted = compactToolContent(
360
+ [{ type: 'video', data: bytes }] as ToolContent,
361
+ 100
362
+ );
363
+
364
+ expect(byteLengthCalls).toBe(0);
365
+ expect(compacted.changed).toBe(true);
366
+ expect(typeof compacted.content).toBe('string');
367
+ expect((compacted.content as string).length).toBeLessThanOrEqual(100);
368
+ });
369
+
370
+ it('uses fixed native view names for exact cap accounting', () => {
371
+ const bytes = new Uint8Array([1]);
372
+ Object.defineProperty(bytes, 'constructor', {
373
+ value: { name: 'X'.repeat(1_000) },
374
+ });
375
+ const content = [{ type: 'video', data: bytes }] as ToolContent;
376
+
377
+ const compacted = compactToolContent(content, 60);
378
+ const serialized = serializeToolContent(compacted.content);
379
+
380
+ expect(compacted.changed).toBe(true);
381
+ expect(serialized.length).toBeLessThanOrEqual(60);
382
+ expect(serialized).not.toContain('X'.repeat(100));
383
+ });
384
+
385
+ it('caps aggregate character traversal across repeated opaque runs', () => {
386
+ const payload = 'x'.repeat(100_000);
387
+ const opaque = { type: 'json', payload };
388
+ const image = {
389
+ type: 'image_url',
390
+ image_url: { url: 'https://example.com/chart.png' },
391
+ };
392
+ const content = Array.from({ length: 20 }, (_, index) =>
393
+ index % 2 === 0 ? opaque : image
394
+ ) as ToolContent;
395
+ const charCodeAt = String.prototype.charCodeAt;
396
+ let characterReads = 0;
397
+ const charSpy = jest
398
+ .spyOn(String.prototype, 'charCodeAt')
399
+ .mockImplementation(function (this: string, index: number) {
400
+ characterReads++;
401
+ return charCodeAt.call(this, index);
402
+ });
403
+
404
+ const compacted = (() => {
405
+ try {
406
+ return compactToolContent(content, 200);
407
+ } finally {
408
+ charSpy.mockRestore();
409
+ }
410
+ })();
411
+
412
+ expect(characterReads).toBeLessThanOrEqual(1_010_000);
413
+ expect(compacted.changed).toBe(true);
414
+ expect(serializeToolContent(compacted.content).length).toBeLessThanOrEqual(
415
+ 200
416
+ );
417
+ expect(compacted.originalChars).toBe(Number.MAX_SAFE_INTEGER);
418
+ });
419
+
420
+ it('bounds structured traversal across repeated block references', () => {
421
+ const metadata = Array.from({ length: 1_000 }, (_, index) => index);
422
+ const block = { type: 'text', text: 'safe', metadata };
423
+ const content = new Array(100).fill(block) as ToolContent;
424
+ const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
425
+ let descriptorReads = 0;
426
+ const descriptorSpy = jest
427
+ .spyOn(Object, 'getOwnPropertyDescriptor')
428
+ .mockImplementation((target, property) => {
429
+ if (target === metadata && property !== 'toJSON') {
430
+ descriptorReads++;
431
+ }
432
+ return getOwnPropertyDescriptor(target, property);
433
+ });
434
+
435
+ try {
436
+ expect(serializeToolContent(content)).toContain('safe');
437
+ expect(getToolContentCharLength(content)).toBeGreaterThan(0);
438
+ expect(
439
+ serializeToolContentBounded(content, 100).length
440
+ ).toBeLessThanOrEqual(100);
441
+ } finally {
442
+ descriptorSpy.mockRestore();
443
+ }
444
+
445
+ expect(descriptorReads).toBeLessThan(10_000);
446
+ });
447
+
448
+ it('rejects oversized MIME-like types before scanning each distinct block', () => {
449
+ const hugeType = `${'a'.repeat(100_000)}/b`;
450
+ const content = Array.from({ length: 50 }, () => ({
451
+ type: hugeType,
452
+ data: 'x',
453
+ })) as ToolContent;
454
+ const indexOf = String.prototype.indexOf;
455
+ let hugeTypeScans = 0;
456
+ const indexOfSpy = jest
457
+ .spyOn(String.prototype, 'indexOf')
458
+ .mockImplementation(function (
459
+ this: string,
460
+ searchString: string,
461
+ position?: number
462
+ ) {
463
+ if (String(this) === hugeType) {
464
+ hugeTypeScans++;
465
+ }
466
+ return indexOf.call(this, searchString, position);
467
+ });
468
+
469
+ const compacted = (() => {
470
+ try {
471
+ return compactToolContent(content, 200);
472
+ } finally {
473
+ indexOfSpy.mockRestore();
474
+ }
475
+ })();
476
+ const serialized = serializeToolContent(compacted.content);
477
+
478
+ expect(hugeTypeScans).toBe(0);
479
+ expect(compacted.changed).toBe(true);
480
+ expect(serialized.length).toBeLessThanOrEqual(200);
481
+ expect(serialized).not.toContain('a'.repeat(1_000));
482
+ });
483
+
484
+ it('bounds omitted atomic type labels before building the notice', () => {
485
+ const boundedType = `${'a'.repeat(250)}/b`;
486
+ const content = Array.from({ length: 50 }, () => ({
487
+ type: boundedType,
488
+ data: 'x',
489
+ })) as ToolContent;
490
+
491
+ const compacted = compactToolContent(content, 200);
492
+ const serialized = serializeToolContent(compacted.content);
493
+
494
+ expect(compacted.changed).toBe(true);
495
+ expect(serialized.length).toBeLessThanOrEqual(200);
496
+ expect(serialized).not.toContain(boundedType);
497
+ });
498
+
499
+ it('preserves safe provider-native tool-result blocks atomically', () => {
500
+ const webSearchResult = {
501
+ type: 'web_search_result',
502
+ url: 'https://example.com/result',
503
+ title: 'Example result',
504
+ encrypted_content: 'encrypted',
505
+ };
506
+ const content = [
507
+ {
508
+ type: 'search_result',
509
+ title: 'Knowledge base result',
510
+ source: 'https://example.com/source',
511
+ citations: { enabled: true },
512
+ content: [{ type: 'text', text: 'Citation-preserving content' }],
513
+ },
514
+ {
515
+ type: 'web_search_tool_result',
516
+ tool_use_id: 'srvtoolu_search',
517
+ content: [webSearchResult],
518
+ },
519
+ webSearchResult,
520
+ {
521
+ type: 'tool_result',
522
+ tool_use_id: 'toolu_custom',
523
+ content: 'done',
524
+ },
525
+ {
526
+ type: 'server_tool_call_result',
527
+ toolCallId: 'server_call',
528
+ status: 'success',
529
+ output: { stdout: 'done' },
530
+ },
531
+ {
532
+ type: 'toolResponse',
533
+ toolResponse: {
534
+ id: 'google_search',
535
+ name: 'google_search',
536
+ response: { results: [] },
537
+ },
538
+ },
539
+ ] as ToolContent;
540
+
541
+ expect(
542
+ (content as Exclude<ToolContent, string>).every(isAtomicToolContentBlock)
543
+ ).toBe(true);
544
+ const result = compactToolContent(content, 10_000);
545
+
546
+ expect(result.changed).toBe(false);
547
+ expect(result.content).toBe(content);
548
+ });
549
+
550
+ it('omits an oversized provider-native result as one atomic block', () => {
551
+ const sentinel = 'OVERSIZED_NATIVE_RESULT';
552
+ const content = [
553
+ {
554
+ type: 'search_result',
555
+ title: 'Large result',
556
+ source: 'https://example.com/large',
557
+ citations: { enabled: true },
558
+ content: [
559
+ {
560
+ type: 'text',
561
+ text: `${sentinel}:${'x'.repeat(20_000)}`,
562
+ },
563
+ ],
564
+ },
565
+ ] as ToolContent;
566
+
567
+ const result = compactToolContent(content, 200);
568
+ const serialized = serializeToolContent(result.content);
569
+
570
+ expect(result.changed).toBe(true);
571
+ expect(serialized.length).toBeLessThanOrEqual(200);
572
+ expect(serialized).toContain('omitted');
573
+ expect(serialized).toContain('search_result');
574
+ expect(serialized).not.toContain(sentinel);
575
+ });
576
+
577
+ it('omits an oversized inline media block instead of leaking past the cap', () => {
578
+ const base64 = 'A'.repeat(10_000);
579
+ const content = [
580
+ {
581
+ type: 'image_url',
582
+ image_url: { url: `data:image/png;base64,${base64}` },
583
+ },
584
+ ] as ToolContent;
585
+
586
+ const result = compactToolContent(content, 200);
587
+
588
+ expect(result.changed).toBe(true);
589
+ expect(serializeToolContent(result.content).length).toBeLessThanOrEqual(
590
+ 200
591
+ );
592
+ expect(serializeToolContent(result.content)).toContain('omitted');
593
+ expect(serializeToolContent(result.content)).not.toContain(base64);
594
+ });
595
+
596
+ it('sizes large native byte payloads without JSON-expanding them', () => {
597
+ const bytes = new Uint8Array(1_000_000);
598
+ const content = [
599
+ {
600
+ type: 'video',
601
+ video: { source: { bytes } },
602
+ },
603
+ ] as ToolContent;
604
+
605
+ const uncapped = compactToolContent(content, Number.MAX_SAFE_INTEGER);
606
+ const compacted = compactToolContent(content, 200);
607
+
608
+ expect(uncapped.content).toBe(content);
609
+ expect(compacted.changed).toBe(true);
610
+ expect(compacted.originalChars).toBeGreaterThan(1_000_000);
611
+ expect(serializeToolContent(compacted.content).length).toBeLessThanOrEqual(
612
+ 200
613
+ );
614
+ expect(serializeToolContent(compacted.content)).toContain('omitted');
615
+ });
616
+
617
+ it('preserves small document blocks while compacting adjacent text', () => {
618
+ const document = {
619
+ type: 'document',
620
+ source: { type: 'url', url: 'https://example.com/report.pdf' },
621
+ };
622
+ const content = [
623
+ { type: 'text', text: 'x'.repeat(2_000) },
624
+ document,
625
+ ] as ToolContent;
626
+
627
+ const result = compactToolContent(content, 400);
628
+
629
+ expect(result.changed).toBe(true);
630
+ expect(Array.isArray(result.content)).toBe(true);
631
+ expect(result.content).toContain(document);
632
+ expect(serializeToolContent(result.content).length).toBeLessThanOrEqual(
633
+ 400
634
+ );
635
+ });
636
+
637
+ it('repeats shared non-cyclic values and marks only true cycles', () => {
638
+ const shared = { value: 'repeated' };
639
+ expect(serializeStructuredValue([shared, shared])).toBe(
640
+ '[{"value":"repeated"},{"value":"repeated"}]'
641
+ );
642
+
643
+ const cyclic: { self?: unknown } = {};
644
+ cyclic.self = cyclic;
645
+ expect(serializeStructuredValue(cyclic)).toBe('{"self":"[Circular]"}');
646
+ });
647
+
648
+ it('normalizes cyclic atomic and text blocks instead of returning them unchanged', () => {
649
+ const cyclicPayload: {
650
+ url: string;
651
+ self?: unknown;
652
+ } = {
653
+ url: 'https://example.com/chart.png',
654
+ };
655
+ cyclicPayload.self = cyclicPayload;
656
+ const atomicBlock = {
657
+ type: 'image_url',
658
+ image_url: cyclicPayload,
659
+ };
660
+ const textBlock: {
661
+ type: 'text';
662
+ text: string;
663
+ metadata?: unknown;
664
+ } = {
665
+ type: 'text',
666
+ text: 'done',
667
+ };
668
+ textBlock.metadata = textBlock;
669
+
670
+ const atomicResult = compactToolContent(
671
+ [atomicBlock] as ToolContent,
672
+ 1_000
673
+ );
674
+ const textResult = compactToolContent([textBlock] as ToolContent, 1_000);
675
+
676
+ expect(isAtomicToolContentBlock(atomicBlock)).toBe(false);
677
+ for (const result of [atomicResult, textResult]) {
678
+ expect(result.changed).toBe(true);
679
+ expect(typeof result.content).toBe('string');
680
+ expect((result.content as string).length).toBeLessThanOrEqual(1_000);
681
+ expect(result.content).toContain('[Circular]');
682
+ expect(() => JSON.parse(result.content as string)).not.toThrow();
683
+ }
684
+ });
685
+
686
+ it('still preserves atomic blocks with shared non-cyclic values', () => {
687
+ const shared = { source: 'tool' };
688
+ const block = {
689
+ type: 'image_url',
690
+ image_url: { url: 'https://example.com/chart.png' },
691
+ first: shared,
692
+ second: shared,
693
+ };
694
+ const content = [block] as ToolContent;
695
+
696
+ expect(isAtomicToolContentBlock(block)).toBe(true);
697
+ const compacted = compactToolContent(content, 1_000);
698
+
699
+ expect(compacted.changed).toBe(false);
700
+ expect(compacted.content).toBe(content);
701
+ });
702
+
703
+ it('bounds structured values without invoking custom toJSON', () => {
704
+ let toJSONCalls = 0;
705
+ const value = {
706
+ safe: 'ok',
707
+ toJSON() {
708
+ toJSONCalls++;
709
+ return 'x'.repeat(10_000);
710
+ },
711
+ };
712
+
713
+ const serialized = serializeStructuredValueBounded(value, 10);
714
+ const compacted = compactToolContent(value, 10);
715
+
716
+ expect(toJSONCalls).toBe(0);
717
+ expect(serialized.content.length).toBeLessThanOrEqual(10);
718
+ expect(String(compacted.content).length).toBeLessThanOrEqual(10);
719
+ expect(serialized.originalChars).toBe(
720
+ JSON.stringify({ safe: 'ok' }).length
721
+ );
722
+ });
723
+
724
+ it('does not preserve atomic blocks with callable toJSON methods', () => {
725
+ let toJSONCalls = 0;
726
+ const block = {
727
+ type: 'image_url',
728
+ image_url: { url: 'https://example.com/chart.png' },
729
+ toJSON() {
730
+ toJSONCalls++;
731
+ return {
732
+ type: 'image_url',
733
+ image_url: { url: 'x'.repeat(10_000) },
734
+ };
735
+ },
736
+ };
737
+
738
+ const compacted = compactToolContent([block] as ToolContent, 200);
739
+
740
+ expect(toJSONCalls).toBe(0);
741
+ expect(typeof compacted.content).toBe('string');
742
+ expect((compacted.content as string).length).toBeLessThanOrEqual(200);
743
+ expect(compacted.changed).toBe(true);
744
+ });
745
+
746
+ it('does not invoke toJSON accessors while validating atomic blocks', () => {
747
+ let getterCalls = 0;
748
+ const source = { url: 'https://example.com/chart.png' };
749
+ Object.defineProperty(source, 'toJSON', {
750
+ enumerable: false,
751
+ get() {
752
+ getterCalls++;
753
+ return (): string => 'unsafe';
754
+ },
755
+ });
756
+ const block = { type: 'image_url', image_url: source };
757
+
758
+ const compacted = compactToolContent([block] as ToolContent, 200);
759
+
760
+ expect(getterCalls).toBe(0);
761
+ expect(typeof compacted.content).toBe('string');
762
+ expect((compacted.content as string).length).toBeLessThanOrEqual(200);
763
+ });
764
+
765
+ it('does not preserve ArrayBuffer payloads with custom toJSON methods', () => {
766
+ let toJSONCalls = 0;
767
+ const data = new ArrayBuffer(8) as ArrayBuffer & {
768
+ toJSON: () => string;
769
+ };
770
+ data.toJSON = () => {
771
+ toJSONCalls++;
772
+ return 'x'.repeat(10_000);
773
+ };
774
+ const block = {
775
+ type: 'media',
776
+ mimeType: 'application/octet-stream',
777
+ data,
778
+ };
779
+
780
+ const compacted = compactToolContent([block] as ToolContent, 200);
781
+
782
+ expect(isAtomicToolContentBlock(block)).toBe(false);
783
+ expect(toJSONCalls).toBe(0);
784
+ expect(typeof compacted.content).toBe('string');
785
+ expect((compacted.content as string).length).toBeLessThanOrEqual(200);
786
+ });
787
+
788
+ it('preserves only native Dates inside atomic blocks', () => {
789
+ let toJSONCalls = 0;
790
+ let toISOStringCalls = 0;
791
+ const nativeBlock = {
792
+ type: 'image_url',
793
+ image_url: { url: 'https://example.com/chart.png' },
794
+ capturedAt: new Date('2026-07-27T12:00:00.000Z'),
795
+ };
796
+ const dateWithToJSON = new Date('2026-07-27T12:00:00.000Z');
797
+ Object.defineProperty(dateWithToJSON, 'toJSON', {
798
+ value() {
799
+ toJSONCalls++;
800
+ return 'x'.repeat(10_000);
801
+ },
802
+ });
803
+ const dateWithToISOString = new Date('2026-07-27T12:00:00.000Z');
804
+ Object.defineProperty(dateWithToISOString, 'toISOString', {
805
+ value() {
806
+ toISOStringCalls++;
807
+ return 'x'.repeat(10_000);
808
+ },
809
+ });
810
+ class ExpandingDate extends Date {
811
+ override toJSON(): string {
812
+ toJSONCalls++;
813
+ return 'x'.repeat(10_000);
814
+ }
815
+ }
816
+
817
+ expect(isAtomicToolContentBlock(nativeBlock)).toBe(true);
818
+ expect(
819
+ isAtomicToolContentBlock({
820
+ ...nativeBlock,
821
+ capturedAt: dateWithToJSON,
822
+ })
823
+ ).toBe(false);
824
+ expect(
825
+ isAtomicToolContentBlock({
826
+ ...nativeBlock,
827
+ capturedAt: dateWithToISOString,
828
+ })
829
+ ).toBe(false);
830
+ expect(
831
+ isAtomicToolContentBlock({
832
+ ...nativeBlock,
833
+ capturedAt: new ExpandingDate('2026-07-27T12:00:00.000Z'),
834
+ })
835
+ ).toBe(false);
836
+ expect(toJSONCalls).toBe(0);
837
+ expect(toISOStringCalls).toBe(0);
838
+ });
839
+
840
+ it('rejects atomic blocks with enumerable accessors without invoking them', () => {
841
+ let getterCalls = 0;
842
+ const imageUrl = {};
843
+ Object.defineProperty(imageUrl, 'url', {
844
+ enumerable: true,
845
+ get() {
846
+ getterCalls++;
847
+ return getterCalls === 1
848
+ ? 'https://example.com/chart.png'
849
+ : 'x'.repeat(10_000);
850
+ },
851
+ });
852
+
853
+ expect(
854
+ isAtomicToolContentBlock({
855
+ type: 'image_url',
856
+ image_url: imageUrl,
857
+ })
858
+ ).toBe(false);
859
+ expect(getterCalls).toBe(0);
860
+ });
861
+
862
+ it('rejects inherited discriminators without invoking accessors', () => {
863
+ for (const enumerable of [true, false]) {
864
+ let getterCalls = 0;
865
+ const prototype = {};
866
+ Object.defineProperty(prototype, 'type', {
867
+ enumerable,
868
+ get() {
869
+ getterCalls++;
870
+ return 'image_url';
871
+ },
872
+ });
873
+ const block = Object.create(prototype) as Record<string, unknown>;
874
+ block.image_url = { url: 'https://example.com/chart.png' };
875
+
876
+ expect(isAtomicToolContentBlock(block)).toBe(false);
877
+ const compacted = compactToolContent([block] as ToolContent, 200);
878
+ expect(getterCalls).toBe(0);
879
+ expect(typeof compacted.content).toBe('string');
880
+ }
881
+
882
+ const inheritedDataBlock = Object.create({
883
+ type: 'image_url',
884
+ image_url: { url: 'https://example.com/chart.png' },
885
+ }) as Record<string, unknown>;
886
+ expect(isAtomicToolContentBlock(inheritedDataBlock)).toBe(false);
887
+ });
888
+
889
+ it('rejects inherited payloads without invoking accessors', () => {
890
+ let getterCalls = 0;
891
+ const imageUrlPrototype = {};
892
+ Object.defineProperty(imageUrlPrototype, 'url', {
893
+ enumerable: false,
894
+ get() {
895
+ getterCalls++;
896
+ return `data:image/png;base64,${'A'.repeat(100_000)}`;
897
+ },
898
+ });
899
+ const block = {
900
+ type: 'image_url',
901
+ image_url: Object.create(imageUrlPrototype) as Record<string, unknown>,
902
+ };
903
+
904
+ expect(isAtomicToolContentBlock(block)).toBe(false);
905
+ const compacted = compactToolContent([block] as ToolContent, 200);
906
+
907
+ expect(getterCalls).toBe(0);
908
+ expect(compacted.changed).toBe(true);
909
+ expect(typeof compacted.content).toBe('string');
910
+ expect((compacted.content as string).length).toBeLessThanOrEqual(200);
911
+ });
912
+
913
+ it('compacts accessor-backed atomic shapes without invoking accessors', () => {
914
+ let getterCalls = 0;
915
+ const imageUrl = {};
916
+ Object.defineProperty(imageUrl, 'url', {
917
+ enumerable: true,
918
+ get() {
919
+ getterCalls++;
920
+ return getterCalls === 1
921
+ ? 'https://example.com/chart.png'
922
+ : 'x'.repeat(10_000);
923
+ },
924
+ });
925
+
926
+ const compacted = compactToolContent(
927
+ [{ type: 'image_url', image_url: imageUrl }] as ToolContent,
928
+ 200
929
+ );
930
+
931
+ expect(getterCalls).toBe(0);
932
+ expect(typeof compacted.content).toBe('string');
933
+ expect((compacted.content as string).length).toBeLessThanOrEqual(200);
934
+ expect(compacted.content).toContain('[Property accessor omitted]');
935
+ });
936
+
937
+ it('bounds generic accessor properties without invoking them', () => {
938
+ let getterCalls = 0;
939
+ const value = {};
940
+ Object.defineProperty(value, 'payload', {
941
+ enumerable: true,
942
+ get() {
943
+ getterCalls++;
944
+ return 'x'.repeat(10_000);
945
+ },
946
+ });
947
+
948
+ const serialized = serializeStructuredValueBounded(value, 200);
949
+
950
+ expect(getterCalls).toBe(0);
951
+ expect(serialized.content).toBe(
952
+ '{"payload":"[Property accessor omitted]"}'
953
+ );
954
+ });
955
+
956
+ it('serializes array holes without invoking inherited accessors', () => {
957
+ let getterCalls = 0;
958
+ const prototype = Object.create(Array.prototype) as unknown[];
959
+ Object.defineProperty(prototype, '0', {
960
+ get() {
961
+ getterCalls++;
962
+ return 'x'.repeat(10_000);
963
+ },
964
+ });
965
+ const value: unknown[] = [];
966
+ value.length = 1;
967
+ Object.setPrototypeOf(value, prototype);
968
+
969
+ const serialized = serializeStructuredValueBounded(value, 200);
970
+
971
+ expect(getterCalls).toBe(0);
972
+ expect(serialized.content).toBe('"[Unsafe array omitted]"');
973
+ });
974
+
975
+ it('fails closed when a proxy blocks own property descriptors', () => {
976
+ let propertyReads = 0;
977
+ const value = new Proxy<unknown[]>(['safe'], {
978
+ get(target, property, receiver) {
979
+ propertyReads++;
980
+ return Reflect.get(target, property, receiver);
981
+ },
982
+ getOwnPropertyDescriptor(target, property) {
983
+ if (property === '0') {
984
+ throw new Error('blocked descriptor');
985
+ }
986
+ return Reflect.getOwnPropertyDescriptor(target, property);
987
+ },
988
+ });
989
+
990
+ const serialized = serializeStructuredValueBounded(value, 200);
991
+
992
+ expect(propertyReads).toBe(0);
993
+ expect(serialized.content).toBe('"[Proxy value omitted]"');
994
+ expect(serialized.originalChars).toBe(Number.MAX_SAFE_INTEGER);
995
+ expect(serialized.truncated).toBe(true);
996
+ });
997
+
998
+ it('never returns stateful proxy blocks through structured fast paths', () => {
999
+ const hugeValue = 'x'.repeat(1_000_000);
1000
+ let descriptorReads = 0;
1001
+ let propertyReads = 0;
1002
+ function statefulProxy<T extends object>(
1003
+ target: T,
1004
+ unstableKey: PropertyKey,
1005
+ expandedValue: unknown
1006
+ ): T {
1007
+ return new Proxy(target, {
1008
+ get(innerTarget, property, receiver) {
1009
+ propertyReads++;
1010
+ if (property === unstableKey && propertyReads > 2) {
1011
+ return expandedValue;
1012
+ }
1013
+ return Reflect.get(innerTarget, property, receiver);
1014
+ },
1015
+ getOwnPropertyDescriptor(innerTarget, property) {
1016
+ descriptorReads++;
1017
+ const descriptor = Reflect.getOwnPropertyDescriptor(
1018
+ innerTarget,
1019
+ property
1020
+ );
1021
+ if (
1022
+ descriptor != null &&
1023
+ property === unstableKey &&
1024
+ descriptorReads > 2
1025
+ ) {
1026
+ return { ...descriptor, value: expandedValue };
1027
+ }
1028
+ return descriptor;
1029
+ },
1030
+ });
1031
+ }
1032
+ const textBlock = statefulProxy(
1033
+ { type: 'text', text: 'ok' },
1034
+ 'text',
1035
+ hugeValue
1036
+ );
1037
+ const atomicBlock = statefulProxy(
1038
+ {
1039
+ type: 'image_url',
1040
+ image_url: { url: 'https://example.com/chart.png' },
1041
+ },
1042
+ 'image_url',
1043
+ { url: `data:image/png;base64,${hugeValue}` }
1044
+ );
1045
+ const content = [textBlock, atomicBlock] as ToolContent;
1046
+
1047
+ const compacted = compactToolContent(content, 1_000);
1048
+
1049
+ expect(descriptorReads).toBe(0);
1050
+ expect(propertyReads).toBe(0);
1051
+ expect(compacted.changed).toBe(true);
1052
+ expect(compacted.content).not.toBe(content);
1053
+ expect(typeof compacted.content).toBe('string');
1054
+ expect((compacted.content as string).length).toBeLessThanOrEqual(1_000);
1055
+ expect(compacted.content).toContain('[Proxy value omitted]');
1056
+ expect(() => JSON.parse(compacted.content as string)).not.toThrow();
1057
+ });
1058
+
1059
+ it('does not traverse proxy arrays or proxy payloads while compacting', () => {
1060
+ const hugeValue = 'x'.repeat(1_000_000);
1061
+ let proxyTrapCalls = 0;
1062
+ let accessorCalls = 0;
1063
+ const payloadTarget = {};
1064
+ Object.defineProperty(payloadTarget, 'url', {
1065
+ enumerable: true,
1066
+ get() {
1067
+ accessorCalls++;
1068
+ return `data:image/png;base64,${hugeValue}`;
1069
+ },
1070
+ });
1071
+ const payload = new Proxy(payloadTarget, {
1072
+ get() {
1073
+ proxyTrapCalls++;
1074
+ return hugeValue;
1075
+ },
1076
+ getOwnPropertyDescriptor(target, property) {
1077
+ proxyTrapCalls++;
1078
+ return Reflect.getOwnPropertyDescriptor(target, property);
1079
+ },
1080
+ });
1081
+ const proxiedArray = new Proxy(
1082
+ [
1083
+ {
1084
+ type: 'image_url',
1085
+ image_url: payload,
1086
+ },
1087
+ ],
1088
+ {
1089
+ get(target, property, receiver) {
1090
+ proxyTrapCalls++;
1091
+ return Reflect.get(target, property, receiver);
1092
+ },
1093
+ getOwnPropertyDescriptor(target, property) {
1094
+ proxyTrapCalls++;
1095
+ return Reflect.getOwnPropertyDescriptor(target, property);
1096
+ },
1097
+ }
1098
+ ) as ToolContent;
1099
+ const callableProxy = new Proxy(() => 'safe', {
1100
+ get() {
1101
+ proxyTrapCalls++;
1102
+ return hugeValue;
1103
+ },
1104
+ });
1105
+
1106
+ const arrayResult = compactToolContent(proxiedArray, 1_000);
1107
+ const callableResult = compactToolContent(callableProxy, 1_000);
1108
+ const nestedResult = compactToolContent(
1109
+ [
1110
+ {
1111
+ type: 'image_url',
1112
+ image_url: payload,
1113
+ },
1114
+ ] as ToolContent,
1115
+ 1_000
1116
+ );
1117
+
1118
+ expect(proxyTrapCalls).toBe(0);
1119
+ expect(accessorCalls).toBe(0);
1120
+ expect(arrayResult.changed).toBe(true);
1121
+ expect(callableResult.changed).toBe(true);
1122
+ expect(nestedResult.changed).toBe(true);
1123
+ expect(arrayResult.content).toBe('"[Proxy value omitted]"');
1124
+ expect(callableResult.content).toBe('"[Proxy value omitted]"');
1125
+ expect(typeof nestedResult.content).toBe('string');
1126
+ expect((nestedResult.content as string).length).toBeLessThanOrEqual(1_000);
1127
+ expect(nestedResult.content).toContain('[Proxy value omitted]');
1128
+ expect(() => JSON.parse(nestedResult.content as string)).not.toThrow();
1129
+ });
1130
+
1131
+ it('counts lone surrogates before preserving atomic content', () => {
1132
+ const content = [
1133
+ {
1134
+ type: 'image_url',
1135
+ image_url: { url: '\ud800'.repeat(30) },
1136
+ },
1137
+ ] as ToolContent;
1138
+
1139
+ const compacted = compactToolContent(content, 200);
1140
+
1141
+ expect(compacted.changed).toBe(true);
1142
+ expect(serializeToolContent(compacted.content).length).toBeLessThanOrEqual(
1143
+ 200
1144
+ );
1145
+ });
1146
+
1147
+ it('serializes Buffer values as bounded byte placeholders', () => {
1148
+ const value = Buffer.alloc(10_000, 1);
1149
+
1150
+ const serialized = serializeStructuredValueBounded(value, 15_000);
1151
+ const compacted = compactToolContent(value, 15_000);
1152
+
1153
+ expect(serialized.content).toBe('"[Buffer: 10000 bytes]"');
1154
+ expect(serialized.content.length).toBeLessThanOrEqual(15_000);
1155
+ expect(compacted.content).toBe(serialized.content);
1156
+ });
1157
+
1158
+ it('does not preserve Buffer toJSON expansion inside atomic blocks', () => {
1159
+ const content = [
1160
+ {
1161
+ type: 'image',
1162
+ data: Buffer.alloc(100, 255),
1163
+ },
1164
+ ] as ToolContent;
1165
+
1166
+ const compacted = compactToolContent(content, 200);
1167
+
1168
+ expect(compacted.changed).toBe(true);
1169
+ expect(typeof compacted.content).toBe('string');
1170
+ expect((compacted.content as string).length).toBeLessThanOrEqual(200);
1171
+ expect(compacted.content).toContain('[Buffer: 100 bytes]');
1172
+ });
1173
+
1174
+ it('retains an exact registry prefix independently of the provider preview', () => {
1175
+ const value = { payload: 'x'.repeat(1_000), tail: 'done' };
1176
+ const exact = JSON.stringify(value);
1177
+
1178
+ const serialized = serializeStructuredValueBounded(
1179
+ value,
1180
+ 100,
1181
+ exact.length
1182
+ );
1183
+
1184
+ expect(serialized.content.length).toBeLessThanOrEqual(100);
1185
+ expect(serialized.content).toContain('truncated');
1186
+ expect(serialized.prefix).toBe(exact);
1187
+ });
1188
+
1189
+ it('emits JSON-exact strings across escape and surrogate chunk boundaries', () => {
1190
+ const value = {
1191
+ payload:
1192
+ `${'x'.repeat(4_095)}😀` +
1193
+ `\ud800${'y'.repeat(4_095)}\udc00` +
1194
+ '\b\t\n\f\r"\\',
1195
+ };
1196
+ const exact = JSON.stringify(value);
1197
+
1198
+ const serialized = serializeStructuredValueBounded(value, exact.length);
1199
+
1200
+ expect(serialized.content).toBe(exact);
1201
+ expect(serialized.originalChars).toBe(exact.length);
1202
+ expect(serialized.truncated).toBe(false);
1203
+ });
1204
+
1205
+ it('bounds a 32 MiB primitive string within a 128 MiB heap', () => {
1206
+ const script = `
1207
+ const { serializeStructuredValueBounded } =
1208
+ await import('./src/utils/toolContent.ts');
1209
+ const payload = 'A'.repeat(32 * 1024 * 1024);
1210
+ const result = serializeStructuredValueBounded({ payload }, 4096);
1211
+ if (
1212
+ result.content.length !== 4096 ||
1213
+ result.originalChars !== Number.MAX_SAFE_INTEGER ||
1214
+ result.truncated !== true
1215
+ ) {
1216
+ throw new Error(JSON.stringify({
1217
+ contentLength: result.content.length,
1218
+ originalChars: result.originalChars,
1219
+ truncated: result.truncated,
1220
+ }));
1221
+ }
1222
+ process.stdout.write('ok');
1223
+ `;
1224
+ const benchmark = spawnSync(
1225
+ process.execPath,
1226
+ [
1227
+ '--max-old-space-size=128',
1228
+ '--loader',
1229
+ 'ts-node/esm/transpile-only',
1230
+ '--experimental-specifier-resolution=node',
1231
+ '--input-type=module',
1232
+ '--eval',
1233
+ script,
1234
+ ],
1235
+ {
1236
+ cwd: process.cwd(),
1237
+ encoding: 'utf8',
1238
+ env: { ...process.env, NODE_NO_WARNINGS: '1' },
1239
+ timeout: 30_000,
1240
+ }
1241
+ );
1242
+
1243
+ if (benchmark.status !== 0) {
1244
+ throw new Error(
1245
+ `Constrained-heap serializer benchmark failed: ${benchmark.stderr}`
1246
+ );
1247
+ }
1248
+ expect(benchmark.stdout).toBe('ok');
1249
+ });
1250
+
1251
+ it('bounds arrays with many small values using an exact segmented prefix', () => {
1252
+ const value = Array.from({ length: 25_000 }, (_, index) => index);
1253
+ const exact = JSON.stringify(value);
1254
+
1255
+ const serialized = serializeStructuredValueBounded(value, 1_000, 2_000);
1256
+
1257
+ expect(serialized.content.length).toBeLessThanOrEqual(1_000);
1258
+ expect(serialized.content).toContain('truncated');
1259
+ expect(serialized.prefix).toBe(exact.slice(0, 2_000));
1260
+ expect(serialized.originalChars).toBe(exact.length);
1261
+ });
1262
+
1263
+ it('routes sparse text-looking arrays through bounded serialization', () => {
1264
+ const sparse: unknown[] = [];
1265
+ sparse.length = 100;
1266
+
1267
+ const compacted = compactToolContent(sparse, 20);
1268
+
1269
+ expect(typeof compacted.content).toBe('string');
1270
+ expect((compacted.content as string).length).toBeLessThanOrEqual(20);
1271
+ expect(compacted.changed).toBe(true);
1272
+ });
1273
+
1274
+ it('fails closed without reading a hostile array proxy length', () => {
1275
+ const hostile = new Proxy<unknown[]>([], {
1276
+ get(target, property, receiver) {
1277
+ if (property === 'length') {
1278
+ throw new Error('blocked length');
1279
+ }
1280
+ return Reflect.get(target, property, receiver);
1281
+ },
1282
+ });
1283
+
1284
+ const compacted = compactToolContent(hostile, 20);
1285
+
1286
+ expect(typeof compacted.content).toBe('string');
1287
+ expect((compacted.content as string).length).toBeLessThanOrEqual(20);
1288
+ expect(compacted.originalChars).toBe(Number.MAX_SAFE_INTEGER);
1289
+ });
1290
+
1291
+ it('caps traversal work for enormous sparse arrays', () => {
1292
+ const sparse: unknown[] = [];
1293
+ sparse.length = 0xffffffff;
1294
+
1295
+ const serialized = serializeStructuredValueBounded(sparse, 200);
1296
+ const compacted = compactToolContent(sparse, 200);
1297
+
1298
+ expect(serialized.content.length).toBeLessThanOrEqual(200);
1299
+ expect(serialized.originalChars).toBe(Number.MAX_SAFE_INTEGER);
1300
+ expect(serialized.truncated).toBe(true);
1301
+ expect(typeof compacted.content).toBe('string');
1302
+ expect((compacted.content as string).length).toBeLessThanOrEqual(200);
1303
+ expect(compacted.originalChars).toBe(Number.MAX_SAFE_INTEGER);
1304
+ });
1305
+
1306
+ it('bounds canonical cache-controlled text without enumerating malformed arrays', () => {
1307
+ const canonical = getBoundedCacheControlledTextToolContent(
1308
+ [
1309
+ {
1310
+ type: 'text',
1311
+ text: 'result body',
1312
+ cache_control: { type: 'ephemeral' },
1313
+ },
1314
+ ],
1315
+ 6
1316
+ );
1317
+ const ownKeys = jest.spyOn(Reflect, 'ownKeys');
1318
+ const malformed: unknown[] = [];
1319
+ malformed.length = 100_000;
1320
+
1321
+ expect(canonical).toEqual([
1322
+ {
1323
+ type: 'text',
1324
+ text: 'result',
1325
+ cache_control: { type: 'ephemeral' },
1326
+ },
1327
+ ]);
1328
+ expect(
1329
+ getBoundedCacheControlledTextToolContent(malformed, 100)
1330
+ ).toBeUndefined();
1331
+ expect(ownKeys).not.toHaveBeenCalled();
1332
+ ownKeys.mockRestore();
1333
+ });
1334
+
1335
+ it('rejects an accessor-backed cache ttl without invoking it', () => {
1336
+ let ttlReads = 0;
1337
+ const cacheControl = { type: 'ephemeral' };
1338
+ Object.defineProperty(cacheControl, 'ttl', {
1339
+ enumerable: true,
1340
+ get() {
1341
+ ttlReads++;
1342
+ return '1h';
1343
+ },
1344
+ });
1345
+
1346
+ expect(
1347
+ getBoundedCacheControlledTextToolContent(
1348
+ [{ type: 'text', text: 'result', cache_control: cacheControl }],
1349
+ 100
1350
+ )
1351
+ ).toBeUndefined();
1352
+ expect(ttlReads).toBe(0);
1353
+
1354
+ const nonEnumerableTtl = { type: 'ephemeral' };
1355
+ Object.defineProperty(nonEnumerableTtl, 'ttl', { value: '1h' });
1356
+ expect(
1357
+ getBoundedCacheControlledTextToolContent(
1358
+ [{ type: 'text', text: 'result', cache_control: nonEnumerableTtl }],
1359
+ 100
1360
+ )
1361
+ ).toBeUndefined();
1362
+ });
1363
+
1364
+ it('only unwraps canonical text cache wrappers', () => {
1365
+ expect(
1366
+ getBoundedSingleTextToolContent(
1367
+ [{ type: 'text', text: 'result', extra: true }],
1368
+ 100
1369
+ )
1370
+ ).toBeUndefined();
1371
+ expect(
1372
+ getBoundedCacheControlledTextToolContent(
1373
+ [
1374
+ {
1375
+ type: 'text',
1376
+ text: 'result',
1377
+ cache_control: { type: 'ephemeral', extra: true },
1378
+ },
1379
+ ],
1380
+ 100
1381
+ )
1382
+ ).toBeUndefined();
1383
+ });
1384
+
1385
+ it('rejects oversized screenshot URLs and file ids before scanning them', () => {
1386
+ const oversizedUrl = `https://example.com/${'a'.repeat(20_000)}`;
1387
+ const charCodeAt = jest.spyOn(String.prototype, 'charCodeAt');
1388
+
1389
+ const urlResult = getComputerCallOutputScreenshot(oversizedUrl);
1390
+ const urlCharacterReads = charCodeAt.mock.calls.length;
1391
+ charCodeAt.mockRestore();
1392
+
1393
+ expect(urlResult).toBeUndefined();
1394
+ expect(urlCharacterReads).toBe(0);
1395
+ expect(
1396
+ getComputerCallOutputScreenshot([
1397
+ {
1398
+ type: 'input_image',
1399
+ file_id: `file_${'a'.repeat(5_000)}`,
1400
+ },
1401
+ ])
1402
+ ).toBeUndefined();
1403
+ });
1404
+
1405
+ it('preserves bounded native computer screenshots', () => {
1406
+ const screenshot = `data:image/png;base64,${'A'.repeat(100_000)}`;
1407
+
1408
+ expect(getComputerCallOutputScreenshot(screenshot)).toEqual({
1409
+ type: 'input_image',
1410
+ image_url: screenshot,
1411
+ });
1412
+ });
1413
+
1414
+ it('keeps large bounded head-tail collection linear', () => {
1415
+ const values = new Array<number>(500_000).fill(0);
1416
+ const serialized = serializeStructuredValueBounded(values, 400_000);
1417
+
1418
+ expect(serialized.content).toHaveLength(400_000);
1419
+ expect(serialized.originalChars).toBe(1_000_001);
1420
+ expect(serialized.truncated).toBe(true);
1421
+ });
1422
+
1423
+ it('bounds provider-facing tool content directly', () => {
1424
+ const serialized = serializeToolContentBounded(
1425
+ { payload: 'x'.repeat(1_000) },
1426
+ 80
1427
+ );
1428
+
1429
+ expect(serialized.length).toBeLessThanOrEqual(80);
1430
+ expect(serialized).toContain('truncated');
1431
+ });
1432
+ });