cloneModelMessages corrupts cross-realm Uint8Arrays and crashes on circular structures with RangeError
Description
cloneModelMessages in packages/ai/src/prompt/clone-model-message.ts is called across every step in generateText and streamText to snapshot messages and responseMessages.
Two issues occur in its implementation:
-
Cross-realm binary view corruption: It checks
value instanceof Uint8Array. In isolated execution environments (such as Nodevm, sandboxes, and durable workflow VMs), instances ofUint8Arrayfrom another realm failinstanceof Uint8Array. As a result, the buffer falls through totypeof value === 'object'andObject.entries(value), turning the binary array into a plain dictionary of indexed properties ({ '0': 1, '1': 2, ... }), corrupting file and image content parts sent to providers. -
Uncaught
RangeErroron circular references: When messages contain objects with circular references (e.g. nested payloads or tool outputs),cloneValuerecursively traversesObject.entries()without tracking visited objects, causingRangeError: Maximum call stack size exceeded.
Reproduction
import vm from 'node:vm';
import { cloneModelMessages } from 'ai/internal';
// 1. Cross-realm Uint8Array
const ctx = vm.createContext({});
const crossRealmBuffer = vm.runInContext('new Uint8Array([1, 2, 3])', ctx);
const msg1 = [{ role: 'user', content: [{ type: 'file', data: crossRealmBuffer, mediaType: 'image/png' }] }];
console.log(cloneModelMessages(msg1)[0].content[0].data);
// Actual: { '0': 1, '1': 2, '2': 3 }
// Expected: Uint8Array([1, 2, 3])
// 2. Circular reference in tool payload
const circular = { name: 'output' };
circular.self = circular;
const msg2 = [{ role: 'tool', content: [{ type: 'tool-result', toolCallId: '1', toolName: 't', output: circular }] }];
cloneModelMessages(msg2);
// Throws: RangeError: Maximum call stack size exceeded
Proposed Fix
- Use
ArrayBuffer.isView(value)to safely identify and clone typed arrays across all realms. - Track visited objects with
seen: Map<unknown, unknown>to preserve circular references and object identity without stack overflow.
Identify issue type
Identified as Bug
Confidence: High
Rationale
This reports two concrete failures in existing message-cloning behavior: binary data is cloned incorrectly across realms, and circular structures crash the operation. The proposed changes are fixes for those failures rather than standalone feature requests.
Evidence
cloneModelMessagesconverts cross-realmUint8Arrayvalues into plain indexed objects, corrupting binary file and image data.- Circular objects in message payloads cause recursive cloning to throw
RangeError: Maximum call stack size exceeded. - The issue provides concrete reproductions and contrasts actual behavior with expected cloning behavior.
Identify bug discovery
Discovery source: discovery:audit (medium confidence).
The report is framed around inspection of the SDK’s cloning implementation and uses targeted synthetic reproductions. It does not establish that either failure was encountered in a production application.
- The description identifies the exact source file:
packages/ai/src/prompt/clone-model-message.ts. - It explains specific implementation flaws: realm-sensitive
instanceof Uint8Arrayand recursiveObject.entries()without visited-object tracking. - The reproductions deliberately construct edge cases with Node
vmand a synthetic circular object. - The proposed fixes directly target the inspected implementation by using
ArrayBuffer.isViewand aseenmap.
If you find bugs through an agent audit, please share the prompts you used instead. Please see our updated guidelines at https://github.com/vercel/ai/issues/17243. If you have any questions, comment on #24118.