From b044cc5ef4568fcb7ab32d7544ef17c786637119 Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 20:04:32 +0800 Subject: [PATCH 1/2] kernel: pass unknown anthropic top-level blocks through verbatim (#3410) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit anthropicToCore's block switch had no default case: unknown top-level block types (in the wild today: document/PDF) fell out silently and their payload vanished on every rebuild with no warning. Unknown blocks now ride rawAnthropicBlock exactly like image/redacted_thinking — coreToAnthropic re-emits them byte-exact (cache_control included) and never renders the label text; protected.ts classifies them as media AND unrecoverable so a fold can never destroy a payload the host archives nowhere. Absorbs the kernel-side hunks of PR #3547 (adapted to master's #3642 canonical identity); #3547 keeps its host-side notice/billing surface. kernel-change: wire保真(#3938 / #3939⑥, 吸收#3547) --- kernel/package-lock.json | 4 +- kernel/package.json | 2 +- kernel/src/protected.ts | 21 ++- kernel/src/wire/anthropic.ts | 20 +++ .../tests/issue3410-wire-passthrough.test.ts | 143 ++++++++++++++++++ .../issue3939-wire-unknown-parts-pins.test.ts | 21 ++- package-lock.json | 2 +- 7 files changed, 200 insertions(+), 13 deletions(-) create mode 100644 kernel/tests/issue3410-wire-passthrough.test.ts diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 514bb51cc7..f6759e557a 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.144", + "version": "0.0.145", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.144", + "version": "0.0.145", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index 476f41bf03..864a02133b 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.144", + "version": "0.0.145", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/protected.ts b/kernel/src/protected.ts index c814ff0c44..1e60eda8b3 100644 --- a/kernel/src/protected.ts +++ b/kernel/src/protected.ts @@ -402,6 +402,16 @@ export function hasMediaPayload(msg: CoreMessage): boolean { if (Array.isArray(content)) return content.some((p) => !isObjWith(p, "type", "text")); } + // #3410: unknown top-level block types (in the wild: document/PDF) ride the + // sidecar with their full payload — media like image, not text. + const abType = asObj(ab)?.type; + if ( + typeof abType === "string" && + abType !== "image" && + abType !== "tool_result" && + abType !== "redacted_thinking" + ) + return true; const item = m.rawResponsesItem; if (isObjWith(item, "type", "input_image")) return true; if (item && typeof item === "object") { @@ -462,7 +472,16 @@ export function hasUnrecoverableMediaPayload(msg: CoreMessage): boolean { } } } - // redacted_thinking etc. share this field — ignored, parity with hasMediaPayload + // #3410: unknown top-level block types (document/PDF ...) — the host + // archives no such payload to disk, so folding destroys it permanently. + if ( + typeof ab.type === "string" && + ab.type !== "image" && + ab.type !== "tool_result" && + ab.type !== "redacted_thinking" + ) + return true; + // redacted_thinking shares this field — ignored, parity with hasMediaPayload } const item = asObj(m.rawResponsesItem); diff --git a/kernel/src/wire/anthropic.ts b/kernel/src/wire/anthropic.ts index 8871425579..62eea6adf1 100644 --- a/kernel/src/wire/anthropic.ts +++ b/kernel/src/wire/anthropic.ts @@ -251,6 +251,26 @@ export function anthropicToCore(body: AnthropicRequestBody): Flat { }); break; } + default: { + // #3410: unknown top-level block types (in the wild today: + // document/PDF) used to fall out of this switch silently — the + // payload vanished on every rebuild with no warning. Ride the + // sidecar like image/redacted_thinking: coreToAnthropic re-emits + // rawAnthropicBlock verbatim (cache_control included) and never + // renders the label text, so the round trip is byte-exact. + const unknownType = (b as { type?: unknown }).type; + const label = + typeof unknownType === "string" ? `[${unknownType}]` : "[unknown]"; + const base = deriveCanonicalMessageId(m.role, "text", label); + msgs.push({ + id: clusters.next(base), + role: m.role, + contentType: "text", + text: label, + rawAnthropicBlock: b, + }); + break; + } } } for (let k = before; k < msgs.length; k++) { diff --git a/kernel/tests/issue3410-wire-passthrough.test.ts b/kernel/tests/issue3410-wire-passthrough.test.ts new file mode 100644 index 0000000000..19fd8aebfa --- /dev/null +++ b/kernel/tests/issue3410-wire-passthrough.test.ts @@ -0,0 +1,143 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { anthropicToCore, coreToAnthropic } from "../src/wire/anthropic.js"; +import { + hasMediaPayload, + hasUnrecoverableMediaPayload, +} from "../src/protected.js"; + +// #3410: unknown top-level block types (in the wild today: document/PDF) +// fell out of anthropicToCore's switch silently — the payload vanished on +// every rebuild with no warning. The fix rides them on rawAnthropicBlock +// like image/redacted_thinking; these tests pin the round-trip fidelity and +// the fold protection that keeps the payload from being destroyed by a +// later compression. + +const DOCUMENT = { + type: "document", + source: { + type: "base64", + media_type: "application/pdf", + data: "JVBERi0xLjQKMSAwIG9iago8PC9UeXBlL0NhdGFsb2c+PgplbmRvYmoK", + }, + cache_control: { type: "ephemeral" }, +}; + +function userBody(content: object[]) { + return { messages: [{ role: "user", content }] }; +} + +function coreFields(m: { [k: string]: unknown }) { + return { + id: m.id, + role: m.role, + contentType: m.contentType, + text: m.text, + rawAnthropicBlock: m.rawAnthropicBlock, + }; +} + +test("round-trips an unknown document block byte-exact (#3410)", () => { + const body = userBody([{ type: "text", text: "summarize this pdf" }, DOCUMENT]); + const parsed = anthropicToCore(body as never); + assert.equal(parsed.msgs.length, 2); + const doc = parsed.msgs[1] as { [k: string]: unknown }; + assert.equal(doc.contentType, "text"); + assert.equal(doc.text, "[document]"); + assert.deepEqual(doc.rawAnthropicBlock, DOCUMENT); + + const rebuilt = coreToAnthropic(parsed.msgs as never); + assert.equal(rebuilt.length, 1); + // The label text never reaches the wire; the original block goes out + // verbatim, cache_control included. + assert.deepEqual(rebuilt[0], { + role: "user", + content: [{ type: "text", text: "summarize this pdf" }, DOCUMENT], + }); +}); + +test("preserves block order when the unknown block leads", () => { + const body = userBody([DOCUMENT, { type: "text", text: "and this" }]); + const rebuilt = coreToAnthropic(anthropicToCore(body as never).msgs as never); + assert.deepEqual(rebuilt[0]!.content, [DOCUMENT, { type: "text", text: "and this" }]); +}); + +test("keeps multiple unknown block types verbatim", () => { + const future = { type: "future_block", foo: 1 }; + const body = userBody([ + { type: "text", text: "t" }, + DOCUMENT, + future, + ]); + const rebuilt = coreToAnthropic(anthropicToCore(body as never).msgs as never); + assert.deepEqual(rebuilt[0]!.content, [ + { type: "text", text: "t" }, + DOCUMENT, + future, + ]); +}); + +test("preserves unknown blocks on assistant messages", () => { + const serverTool = { type: "server_tool_use", id: "st_1", name: "web_search" }; + const body = { messages: [{ role: "assistant", content: [serverTool] }] }; + const parsed = anthropicToCore(body as never); + assert.equal((parsed.msgs[0] as { [k: string]: unknown }).role, "assistant"); + const rebuilt = coreToAnthropic(parsed.msgs as never); + assert.deepEqual(rebuilt[0], { role: "assistant", content: [serverTool] }); +}); + +test("unknown-block cores are media AND unrecoverable — folding would destroy them", () => { + const parsed = anthropicToCore(userBody([DOCUMENT]) as never); + assert.equal(hasMediaPayload(parsed.msgs[0] as never), true); + assert.equal(hasUnrecoverableMediaPayload(parsed.msgs[0] as never), true); +}); + +test("known sidecar classifications stay unchanged (parity pins)", () => { + const base64Img = { + type: "image", + source: { type: "base64", media_type: "image/png", data: "QQ==" }, + }; + const urlImg = { + type: "image", + source: { type: "url", url: "https://example.com/x.png" }, + }; + const redacted = { type: "redacted_thinking", data: "REDACTED==" }; + const cores = anthropicToCore( + userBody([base64Img, urlImg, redacted, { type: "text", text: "plain" }]) as never, + ).msgs as { [k: string]: unknown }[]; + // archivable base64 image: media yes, unrecoverable no (host spills to disk) + assert.equal(hasMediaPayload(cores[0] as never), true); + assert.equal(hasUnrecoverableMediaPayload(cores[0] as never), false); + // URL image: unrecoverable (no local archive) + assert.equal(hasMediaPayload(cores[1] as never), true); + assert.equal(hasUnrecoverableMediaPayload(cores[1] as never), true); + // redacted_thinking: existing parity — not media-classified here + assert.equal(hasMediaPayload(cores[2] as never), false); + assert.equal(hasUnrecoverableMediaPayload(cores[2] as never), false); + // plain text: neither + assert.equal(hasMediaPayload(cores[3] as never), false); + assert.equal(hasUnrecoverableMediaPayload(cores[3] as never), false); +}); + +test("ids are deterministic across repeated parses; duplicates stay distinct and stable", () => { + const body = userBody([DOCUMENT, { type: "text", text: "t" }, DOCUMENT]); + const once = anthropicToCore(body as never).msgs.map(coreFields); + const twice = anthropicToCore(body as never).msgs.map(coreFields); + assert.deepEqual(twice, once); + const docIds = [once[0]!.id, once[2]!.id] as string[]; + assert.notEqual(docIds[0], docIds[1]); +}); + +test("parse is a structural fixed point over rebuild", () => { + const body = userBody([ + { type: "text", text: "before" }, + DOCUMENT, + { type: "text", text: "after" }, + ]); + const first = anthropicToCore(body as never).msgs.map(coreFields); + const rebuilt = coreToAnthropic( + anthropicToCore(body as never).msgs as never, + ); + const second = anthropicToCore({ messages: rebuilt } as never).msgs.map(coreFields); + assert.deepEqual(second, first); +}); diff --git a/kernel/tests/issue3939-wire-unknown-parts-pins.test.ts b/kernel/tests/issue3939-wire-unknown-parts-pins.test.ts index 7bcf075f8f..b27e5688aa 100644 --- a/kernel/tests/issue3939-wire-unknown-parts-pins.test.ts +++ b/kernel/tests/issue3939-wire-unknown-parts-pins.test.ts @@ -11,13 +11,13 @@ import { } from "../src/wire/responses.js"; // Characterization pins for issue #3939 (打桩): each test freezes how ONE -// wire codec treats a part/item type it does not know. #3410/#3938 PR-C -// changes the anthropic line (preserve + warn instead of silent drop); when -// that lands, update the anthropic pins IN THE SAME PR. The openai line is +// wire codec treats a part/item type it does not know. The openai line is // already pinned in tests/media-payload.test.ts (#1205) and is not -// duplicated here. +// duplicated here. The anthropic pin below was intentionally MOVED by PR-C +// (#3961, #3410): silent drop → verbatim passthrough, per the plan this file +// recorded ("update the anthropic pins IN THE SAME PR"). -test("pin: anthropic silently drops unknown top-level content blocks (document block vanishes, no error)", () => { +test("pin: anthropic passes unknown top-level content blocks through verbatim (document survives round trip; moved by #3961 from silent drop)", () => { const body = { model: "claude-x", system: "s", @@ -41,12 +41,17 @@ test("pin: anthropic silently drops unknown top-level content blocks (document b const flat = anthropicToCore(body as never); - assert.equal(flat.msgs.length, 1); + assert.equal(flat.msgs.length, 2); assert.equal(flat.msgs[0]!.text, "hi"); - assert.ok(!JSON.stringify(flat.msgs).includes("document")); + assert.deepEqual( + (flat.msgs[1] as { rawAnthropicBlock?: unknown }).rawAnthropicBlock, + body.messages[0]!.content[1], + ); const rebuilt = coreToAnthropic(flat.msgs); - assert.ok(!JSON.stringify(rebuilt).includes("document")); + // one message, inbound order preserved, unknown block byte-exact + assert.equal(rebuilt.length, 1); + assert.deepEqual(rebuilt[0]!.content, body.messages[0]!.content); }); test("pin: google preserves unknown parts verbatim via the rawGoogleParts sidecar (fileData round-trips)", () => { diff --git a/package-lock.json b/package-lock.json index b833be77ee..198d33464d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.144", + "version": "0.0.145", "dev": true, "license": "MIT", "devDependencies": { From 477f3d78d3c0dc61348889452f9f238b6b01a7a4 Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 20:04:32 +0800 Subject: [PATCH 2/2] =?UTF-8?q?kernel:=20pin=20wire=20byte-stability=20con?= =?UTF-8?q?tract=20for=20openai/google=20normalization=20(#3938=20/=20#393?= =?UTF-8?q?9=E2=91=A5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The openai leading-opaque reorder and the google part-order normalization are the "convention-stable" rows of #3938's root-cause table: they rewrite bytes on every request by convention rather than by state. This commit makes that stability an EXPLICIT contract and pins it: - coreToOpenai JSDoc: emission is a pure function of the core array; the canonical user-run shape is fixed (lone plain text -> string, else parts in CORE order, text first then rawOpenaiContentParts); the leading-opaque PREPEND exists so [opaque,text,opaque] emits [text,opq1,opq2] deterministically. - coreToGoogle JSDoc: for alternating-role histories emission is pure in the core array; the canonical orders ARE the merge rules (model reasoning->text->tool-call, user functionResponse-then-content); same-role merging is required normalization and its output order is canonical. - issue3939-wire-byte-stability.test.ts: golden rebuild shapes per line plus cross-request prefix byte-identity (same shape re-sent on consecutive requests must produce identical request bytes) and id determinism on both lines. Same-role google merging is pinned as behavior, not chased as a bug (reachable only with client-invalid non-alternating input). No runtime change: comments + tests only. The goldens fail if a future edit turns either normalization into a shape-dependent or order-dependent rewrite. --- kernel/package-lock.json | 4 +- kernel/package.json | 2 +- kernel/src/wire/google.ts | 14 +- kernel/src/wire/openai.ts | 14 ++ .../issue3939-wire-byte-stability.test.ts | 214 ++++++++++++++++++ package-lock.json | 2 +- 6 files changed, 245 insertions(+), 5 deletions(-) create mode 100644 kernel/tests/issue3939-wire-byte-stability.test.ts diff --git a/kernel/package-lock.json b/kernel/package-lock.json index f6759e557a..5940127f74 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.145", + "version": "0.0.146", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.145", + "version": "0.0.146", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index 864a02133b..763683a634 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.145", + "version": "0.0.146", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/wire/google.ts b/kernel/src/wire/google.ts index 13595e0f27..298d3948ea 100644 --- a/kernel/src/wire/google.ts +++ b/kernel/src/wire/google.ts @@ -346,7 +346,19 @@ export function normalizeGoogleUserPartOrder(parts: GooglePart[]): GooglePart[] * becomes a single `{"role":"user"}` content normalized by * normalizeGoogleUserPartOrder (#3159: plain text leads, functionResponse * parts trail). Mid-conversation system cores (host-synthetic; Gemini has no - * system role inside contents) ride on the user side as a text part. */ + * system role inside contents) ride on the user side as a text part. + * + * Byte-stability contract (#3938 / #3939⑥): for alternating-role histories + * (the only shape Gemini accepts), this function is PURE in the core array + * — same shape, same bytes, every request; any drift breaks the upstream + * prefix cache mid-session. The canonical part orders ARE the merge rules + * above: model runs emit reasoning → text → tool-call; user runs emit + * functionResponse parts then content parts, re-normalized by + * normalizeGoogleUserPartOrder (#3159). Merging same-role runs into one + * content is the required normalization (Gemini rejects non-alternating + * roles), and its output order is the canonical one. Do not reorder this + * emission without moving the pins in + * kernel/tests/issue3939-wire-byte-stability.test.ts. */ export function coreToGoogle(messages: BiliMessage[]): GoogleContent[] { const out: GoogleContent[] = []; let model: { diff --git a/kernel/src/wire/openai.ts b/kernel/src/wire/openai.ts index ff20584f31..d19f2e86f9 100644 --- a/kernel/src/wire/openai.ts +++ b/kernel/src/wire/openai.ts @@ -336,6 +336,20 @@ export function openaiToCore(body: OpenAIRequestBody): Flat { return { msgs, systemText: systemParts.join("\n\n") }; } +/** Rebuild OpenAI Chat messages from kernel messages. + * + * Byte-stability contract (#3938 / #3939⑥): this function is PURE in the + * core array — the same logical shape must serialize to identical bytes on + * every request, because any drift breaks the upstream prefix cache + * mid-session. The canonical emission for a user run is FIXED: a lone + * plain-text run rebuilds as string content; any run carrying sidecars + * rebuilds as a parts array in CORE order where each sidecar core emits its + * text first, then its rawOpenaiContentParts in array order. The parse side + * (openaiToCore's leading-opaque PREPEND) makes that order stable for + * [opaque, text, opaque] shapes: every opaque part lands on one text core, + * so the rebuild always emits [text, opq1, opq2] regardless of the input + * interleaving. Do not reorder this emission without moving the pins in + * kernel/tests/issue3939-wire-byte-stability.test.ts. */ export function coreToOpenai(messages: BiliMessage[]): OpenAIMessage[] { const out: OpenAIMessage[] = []; let userRun: BiliMessage[] = []; diff --git a/kernel/tests/issue3939-wire-byte-stability.test.ts b/kernel/tests/issue3939-wire-byte-stability.test.ts new file mode 100644 index 0000000000..99d467008c --- /dev/null +++ b/kernel/tests/issue3939-wire-byte-stability.test.ts @@ -0,0 +1,214 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { coreToOpenai, openaiToCore } from "../src/wire/openai.js"; +import { coreToGoogle, googleToCore } from "../src/wire/google.js"; + +// #3938 / #3939⑥ — wire byte fidelity. The upstream prefix cache only holds +// when the SAME logical shape serializes to IDENTICAL bytes on every +// request. openaiToCore/coreToOpenai and googleToCore/coreToGoogle are pure +// functions of the request body; these pins make that stability explicit +// (the "explicit canonical" half of the #3939⑥ contract) so any future +// reorder — however well-intentioned — fails here before it breaks a live +// session's cache. Goldens below were characterized against the current +// emission (probe-verified 2026-10-20), not invented. + +const FILE = { + type: "file", + file: { filename: "a.pdf", file_data: "data:application/pdf;base64,AAA=" }, +}; +const CUSTOM = { type: "custom_op", x: 1 }; +const IMG = { type: "image_url", image_url: { url: "data:image/png;base64,QQ==" } }; + +function oaRebuild(content: object[]) { + return coreToOpenai( + openaiToCore({ messages: [{ role: "user", content }] } as never).msgs, + ); +} + +test("openai: [opaque, text] and [text, opaque] both emit the canonical [text, opaque]", () => { + const expected = [ + { role: "user", content: [{ type: "text", text: "T1" }, FILE] }, + ]; + assert.deepEqual(oaRebuild([FILE, { type: "text", text: "T1" }]), expected); + assert.deepEqual(oaRebuild([{ type: "text", text: "T1" }, FILE]), expected); +}); + +test("openai: [opaque, text, opaque] — leading PREPEND lands both opaquers after the text", () => { + assert.deepEqual(oaRebuild([FILE, { type: "text", text: "T1" }, CUSTOM]), [ + { role: "user", content: [{ type: "text", text: "T1" }, FILE, CUSTOM] }, + ]); +}); + +test("openai: [opaque, image, text] — image keeps its own core, leading opaque joins the text core", () => { + assert.deepEqual(oaRebuild([FILE, IMG, { type: "text", text: "T1" }]), [ + { role: "user", content: [IMG, { type: "text", text: "T1" }, FILE] }, + ]); +}); + +test("openai: [opaque, opaque, text] — both opaquers trail the text, input order kept", () => { + assert.deepEqual(oaRebuild([FILE, CUSTOM, { type: "text", text: "T1" }]), [ + { role: "user", content: [{ type: "text", text: "T1" }, FILE, CUSTOM] }, + ]); +}); + +test("openai: opaque-only message keeps the legacy single-part array shape", () => { + assert.deepEqual(oaRebuild([FILE]), [{ role: "user", content: [FILE] }]); +}); + +const OA_SHAPES: [string, object[]][] = [ + ["opaque-text", [FILE, { type: "text", text: "T1" }]], + ["text-opaque", [{ type: "text", text: "T1" }, FILE]], + ["opaque-text-opaque", [FILE, { type: "text", text: "T1" }, CUSTOM]], + ["opaque-image-text", [FILE, IMG, { type: "text", text: "T1" }]], + ["opaque-only", [FILE]], +]; +for (const [name, content] of OA_SHAPES) { + test(`openai: ${name} — appended turns leave the rebuilt prefix byte-identical`, () => { + const req1 = oaRebuild(content); + const req2 = coreToOpenai( + openaiToCore({ + messages: [ + { role: "user", content }, + { role: "assistant", content: "A1" }, + { role: "user", content: "next" }, + ], + } as never).msgs, + ); + assert.equal(req2.length, 3); + // The prefix-cache property: everything the previous request sent goes + // out byte-for-byte again. + assert.equal(JSON.stringify(req2.slice(0, req1.length)), JSON.stringify(req1)); + }); +} + +test("openai: parse is a structural fixed point over rebuild", () => { + const pick = (m: { [k: string]: unknown }) => ({ + id: m.id, + ct: m.contentType, + text: m.text, + raw: m.rawOpenaiContentParts ?? m.rawOpenaiContent ?? null, + }); + const body = { messages: [{ role: "user", content: [FILE, { type: "text", text: "T1" }, CUSTOM] }] }; + const once = openaiToCore(body as never).msgs.map(pick); + const twice = openaiToCore({ messages: coreToOpenai(openaiToCore(body as never).msgs) } as never).msgs.map(pick); + assert.deepEqual(twice, once); +}); + +test("openai: repeated parses of one body mint identical ids", () => { + const body = { messages: [{ role: "user", content: [FILE, { type: "text", text: "T1" }] }] }; + const a = openaiToCore(body as never).msgs.map((m) => m.id); + const b = openaiToCore(body as never).msgs.map((m) => m.id); + assert.deepEqual(b, a); +}); + +const FC = { functionCall: { name: "f", args: { b: 1, a: 2 } } }; +const THOUGHT = { thought: true, text: "hmm", thoughtSignature: "SIG" }; +const SIGNED_TEXT = { text: "MT", thoughtSignature: "SIGT" }; +const FD = { fileData: { fileUri: "https://x/y.bin" } }; + +function gRebuild(contents: object[]) { + return coreToGoogle(googleToCore({ contents } as never).msgs); +} + +test("google: model run emits the canonical reasoning → text → tool-call order regardless of input order", () => { + assert.deepEqual(gRebuild([{ role: "model", parts: [FC, THOUGHT, SIGNED_TEXT] }]), [ + { role: "model", parts: [THOUGHT, SIGNED_TEXT, FC] }, + ]); +}); + +test("google: movable plain text after a functionResponse leads (#3159)", () => { + assert.deepEqual( + gRebuild([ + { + role: "user", + parts: [ + { functionResponse: { name: "f", response: { output: "ok" }, id: "c1" } }, + { text: "plain after fr" }, + ], + }, + ]), + [ + { + role: "user", + parts: [ + { text: "plain after fr" }, + { functionResponse: { name: "f", response: { output: "ok" }, id: "c1" } }, + ], + }, + ], + ); +}); + +test("google: signed text after a functionResponse stays put (not movable)", () => { + assert.deepEqual( + gRebuild([ + { + role: "user", + parts: [ + { functionResponse: { name: "f", response: {}, id: "c1" } }, + { text: "signed", thoughtSignature: "S2" }, + ], + }, + ]), + [ + { + role: "user", + parts: [ + { functionResponse: { name: "f", response: {}, id: "c1" } }, + { text: "signed", thoughtSignature: "S2" }, + ], + }, + ], + ); +}); + +test("google: fileData part rides verbatim in place, both input orders emit [text, fileData]", () => { + const expected = [ + { role: "user", parts: [{ text: "after fd" }, FD] }, + ]; + assert.deepEqual(gRebuild([{ role: "user", parts: [{ text: "after fd" }, FD] }]), expected); + assert.deepEqual(gRebuild([{ role: "user", parts: [FD, { text: "after fd" }] }]), expected); +}); + +const G_SHAPES: [string, { role: string; parts: object[] }][] = [ + ["user-filedata-text", { role: "user", parts: [{ text: "look at" }, FD] }], + ["model-mixed", { role: "model", parts: [THOUGHT, SIGNED_TEXT, FC] }], +]; +for (const [name, first] of G_SHAPES) { + test(`google: ${name} — appended alternating turn leaves the rebuilt prefix byte-identical`, () => { + const req1 = gRebuild([first]); + const req2 = gRebuild([ + first, + first.role === "model" + ? { role: "user", parts: [{ text: "next" }] } + : { role: "model", parts: [{ text: "reply" }] }, + ]); + assert.equal(req2.length, 2); + assert.equal(JSON.stringify(req2.slice(0, req1.length)), JSON.stringify(req1)); + }); +} + +test("google: consecutive same-role contents merge into ONE canonical content (required normalization)", () => { + // Gemini rejects non-alternating roles, so the merge is mandatory; its + // output order (reasoning → text → tool-call) is the canonical one. No + // healthy client ever sends these — this pins the normalization itself. + assert.deepEqual( + gRebuild([ + { role: "model", parts: [{ text: "A1" }, FC] }, + { role: "model", parts: [{ text: "B1" }] }, + ]), + [ + { + role: "model", + parts: [{ text: "A1" }, { text: "B1" }, { functionCall: { name: "f", args: { b: 1, a: 2 } } }], + }, + ], + ); +}); + +test("google: repeated parses of one body mint identical ids", () => { + const body = { contents: [{ role: "user", parts: [{ text: "after fd" }, FD] }] }; + const a = googleToCore(body as never).msgs.map((m) => m.id); + const b = googleToCore(body as never).msgs.map((m) => m.id); + assert.deepEqual(b, a); +}); diff --git a/package-lock.json b/package-lock.json index 198d33464d..ece84ee990 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.145", + "version": "0.0.146", "dev": true, "license": "MIT", "devDependencies": {