ai icon indicating copy to clipboard operation
ai copied to clipboard

cloneModelMessages corrupts cross-realm Uint8Arrays and crashes on circular structures with RangeError

Open nikolas-sapa opened this issue 1 month ago • 3 comments

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:

  1. Cross-realm binary view corruption: It checks value instanceof Uint8Array. In isolated execution environments (such as Node vm, sandboxes, and durable workflow VMs), instances of Uint8Array from another realm fail instanceof Uint8Array. As a result, the buffer falls through to typeof value === 'object' and Object.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.

  2. Uncaught RangeError on circular references: When messages contain objects with circular references (e.g. nested payloads or tool outputs), cloneValue recursively traverses Object.entries() without tracking visited objects, causing RangeError: 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

  1. Use ArrayBuffer.isView(value) to safely identify and clone typed arrays across all realms.
  2. Track visited objects with seen: Map<unknown, unknown> to preserve circular references and object identity without stack overflow.

nikolas-sapa avatar Oct 25 '26 13:10 nikolas-sapa

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

  • cloneModelMessages converts cross-realm Uint8Array values 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.

ai-sdk-factory[bot] avatar Oct 25 '26 13:10 ai-sdk-factory[bot]

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 Uint8Array and recursive Object.entries() without visited-object tracking.
  • The reproductions deliberately construct edge cases with Node vm and a synthetic circular object.
  • The proposed fixes directly target the inspected implementation by using ArrayBuffer.isView and a seen map.

ai-sdk-factory[bot] avatar Oct 25 '26 13:10 ai-sdk-factory[bot]

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.

gr2m avatar Oct 27 '26 18:10 gr2m