diff --git a/packages/e2e/src/agent/message-shape.ts b/packages/e2e/src/agent/message-shape.ts index 5ffa2df6aa..7d9faf39b0 100644 --- a/packages/e2e/src/agent/message-shape.ts +++ b/packages/e2e/src/agent/message-shape.ts @@ -1,63 +1,55 @@ /** - * What a message holds, as the model reads it, for telling a rewritten - * message from one carried forward. Anthropic binds a thinking block to the - * messages before it and rejects a replay after any of them changed, so the - * loop compares each turn's history with what the last request sent: by - * value, since an executor may edit a message through the object it was - * handed or hand back a clone of a history it did not change. + * Model-message content fingerprint for detecting edits to a signed thinking + * prefix without retaining another copy of the history, including screenshots. + * Provider metadata is excluded: moved cache breakpoints and signatures are + * not changes to the model-visible content. */ +import { createHash } from 'node:crypto'; import type { ModelMessage } from 'ai'; -/** A message's content serialized without provider options, and its binary payloads copied. */ -export interface MessageShape { - readonly text: string; - readonly binaries: readonly Uint8Array[]; -} +/** SHA-256 digest of one model-visible message. */ +export type MessageShape = string; -/** Keys a message carries for the provider, not for the model: a moved cache breakpoint or a signature is no edit. */ const PROVIDER_KEYS = new Set(['providerOptions', 'providerMetadata']); -/** The shape of one message. Bytes are copied, so an edit to the live array after the snapshot still shows. */ +/** + * Hashes a message's content as read by the model. Binary payload bytes enter + * the digest synchronously, so a subsequent in-place edit changes the next + * shape even if the same Uint8Array object is reused. + */ export function shapeOf(message: ModelMessage): MessageShape { - const binaries: Uint8Array[] = []; - const chunks: string[] = []; + const hash = createHash('sha256'); + const write = (value: string | undefined): void => { hash.update(value ?? ''); }; const walk = (value: unknown): void => { if (value instanceof Uint8Array) { - binaries.push(Uint8Array.from(value)); - chunks.push(``); + write(``); + hash.update(value); } else if (value instanceof ArrayBuffer) { walk(new Uint8Array(value)); } else if (Array.isArray(value)) { - chunks.push('['); + write('['); for (const item of value) walk(item); - chunks.push(']'); + write(']'); } else if (typeof value === 'object' && value !== null && !(value instanceof URL)) { - chunks.push('{'); + write('{'); for (const [key, item] of Object.entries(value)) { if (PROVIDER_KEYS.has(key) || item === undefined) continue; - chunks.push(JSON.stringify(key), ':'); + write(JSON.stringify(key)); + write(':'); walk(item); } - chunks.push('}'); + write('}'); } else { - chunks.push(JSON.stringify(value instanceof URL ? value.href : value), ','); + write(JSON.stringify(value instanceof URL ? value.href : value)); + write(','); } }; walk(message); - return { text: chunks.join(''), binaries }; + return hash.digest('hex'); } -/** Whether two shapes send the model the same message. */ +/** Whether two snapshots carry the same model-visible message. */ export function sameShape(a: MessageShape, b: MessageShape): boolean { - return a.text === b.text && a.binaries.length === b.binaries.length && a.binaries.every((bytes, index) => sameBytes(bytes, b.binaries[index]!)); -} - -/** Whether two payloads hold the same bytes. */ -function sameBytes(a: Uint8Array, b: Uint8Array): boolean { - if (a.byteLength !== b.byteLength) return false; - for (let index = 0; index < a.byteLength; index += 1) { - if (a[index] !== b[index]) return false; - } - return true; + return a === b; } diff --git a/packages/e2e/src/agent/tool-loop.ts b/packages/e2e/src/agent/tool-loop.ts index 20d19dace8..f779c16851 100644 --- a/packages/e2e/src/agent/tool-loop.ts +++ b/packages/e2e/src/agent/tool-loop.ts @@ -793,47 +793,34 @@ const OVERFLOW_TEXT_CLIP_CHARS = 16_384; * screen is elided regardless of the cache-friendly budget, then any text * still longer than the clip, the text beside a screenshot included, is cut * to its head with a notice, so the model knows to observe again for what it - * no longer sees. A message nothing shortened is returned as it is, so the - * next turn can tell which messages were rewritten. + * no longer sees. Prefix changes are detected by content, not object identity. */ function shrinkForOverflow(messages: ModelMessage[]): ModelMessage[] { return compactScreenHistory(messages, { keepStaleBytes: 0 }).map((message) => { if (message.role === 'user') { - if (typeof message.content === 'string') { - const content = clip(message.content); - return content === message.content ? message : { ...message, content }; - } - const content = clipped(message.content, (part) => (part.type === 'text' ? clippedText(part) : part)); - return content === undefined ? message : { ...message, content }; + if (typeof message.content === 'string') return { ...message, content: clip(message.content) }; + return { + ...message, + content: message.content.map((part) => (part.type === 'text' ? { ...part, text: clip(part.text) } : part)), + }; } if (message.role !== 'tool') return message; - const content = clipped(message.content, (part) => { - if (part.type !== 'tool-result') return part; - const { output } = part; - if (output.type === 'text') { - const value = clip(output.value); - return value === output.value ? part : { ...part, output: { ...output, value } }; - } - if (output.type !== 'content') return part; - const value = clipped(output.value, (item) => (item.type === 'text' ? clippedText(item) : item)); - return value === undefined ? part : { ...part, output: { ...output, value } }; - }); - return content === undefined ? message : { ...message, content }; + return { + ...message, + content: message.content.map((part) => { + if (part.type !== 'tool-result') return part; + const { output } = part; + if (output.type === 'text') return { ...part, output: { ...output, value: clip(output.value) } }; + if (output.type !== 'content') return part; + return { + ...part, + output: { ...output, value: output.value.map((item) => (item.type === 'text' ? { ...item, text: clip(item.text) } : item)) }, + }; + }), + }; }); } -/** The parts with `one` applied, or undefined when it returned every part as it was. */ -function clipped(parts: readonly Part[], one: (part: Part) => Part): Part[] | undefined { - const next = parts.map(one); - return next.some((part, index) => part !== parts[index]) ? next : undefined; -} - -/** A text part with its text clipped, or the part itself when it is short enough. */ -function clippedText(part: Part): Part { - const text = clip(part.text); - return text === part.text ? part : { ...part, text }; -} - function clip(text: string): string { if (text.length <= OVERFLOW_TEXT_CLIP_CHARS) return text; const cut = text.length - OVERFLOW_TEXT_CLIP_CHARS; diff --git a/packages/e2e/tests/unit/message-shape.test.ts b/packages/e2e/tests/unit/message-shape.test.ts index a6a0884f85..5911ef671e 100644 --- a/packages/e2e/tests/unit/message-shape.test.ts +++ b/packages/e2e/tests/unit/message-shape.test.ts @@ -45,15 +45,26 @@ describe('message shapes', () => { expect(sameShape(shapeOf({ role: 'user', content: 'x' }), shapeOf({ role: 'assistant', content: 'x' }))).toBe(false); }); - it('keep a copy of the bytes, so an edit to the live array after the snapshot shows', () => { + it('keeps a bounded digest that detects an in-place binary edit', () => { const message = screenshot(); const before = shapeOf(message); + expect(before).toMatch(/^[a-f0-9]{64}$/u); const data = (message.content as { data: Uint8Array }[])[1]!.data; data[0] = 0; expect(sameShape(before, shapeOf(message))).toBe(false); expect(sameShape(before, shapeOf(screenshot()))).toBe(true); }); + it('keeps a fixed-size fingerprint for a large screenshot without copying it into the snapshot', () => { + const content = new Uint8Array(2 * 1024 * 1024).fill(42); + const message: ModelMessage = { role: 'user', content: [{ type: 'file', data: content, mediaType: 'image/png' }] }; + const before = shapeOf(message); + expect(before).toHaveLength(64); + expect(sameShape(before, shapeOf(structuredClone(message)))).toBe(true); + content[content.length - 1] ^= 1; + expect(sameShape(before, shapeOf(message))).toBe(false); + }); + it('differ for payloads of another length or content, and for a file named by URL against one sent as bytes', () => { const shorter: ModelMessage = { role: 'user', content: [{ type: 'file', data: new Uint8Array([137, 80, 78]), mediaType: 'image/png' }] }; const byUrl = (): ModelMessage => ({ role: 'user', content: [{ type: 'file', data: new URL('https://example.test/a.png'), mediaType: 'image/png' }] });