Skip to content
Merged
47 changes: 41 additions & 6 deletions kernel/src/wire/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ export function googleToCore(body: GoogleRequestBody): GoogleFlat {
const parts = Array.isArray(content.parts) ? content.parts : [];
const isModel = content.role === "model";

// 1. Tool results first: the rebuilt user content must lead with its
// functionResponse parts.
// 1. Tool results first: their cores precede the content's own user core
// (pairing order); the rebuild's PART order is normalized separately
// (coreToGoogle / #3159).
for (const part of parts) {
const fr = part.functionResponse;
if (!fr || typeof fr.name !== "string") continue;
Expand Down Expand Up @@ -211,13 +212,44 @@ export function googleToCore(body: GoogleRequestBody): GoogleFlat {
return { msgs, systemText: googleSystemText(body) };
}

/** #3159 — Vertex rejects a request whose user content carries plain text
* AFTER a functionResponse part ("Requests ending with a model turn are not
* supported"); text-before-functionResponse is accepted (controlled A/B on
* the official endpoint, gemini-4-argon). When a movable plain-text part
* trails a functionResponse, stable-partition the unsigned non-thought plain
* text ahead of every non-text part; otherwise return the input untouched so
* already-safe shapes stay byte-identical (prefix-cache anchor). Signed text
* parts, thought parts and all non-text parts keep their relative order —
* nothing is dropped, renamed or rewritten. */
export function normalizeGoogleUserPartOrder(parts: GooglePart[]): GooglePart[] {
const firstFr = parts.findIndex((p) => p.functionResponse !== undefined);
if (firstFr === -1) return parts;
const movable = (p: GooglePart) =>
typeof p.text === "string" &&
p.thought !== true &&
typeof p.thoughtSignature !== "string";
let reorder = false;
for (let i = firstFr; i < parts.length; i++) {
if (movable(parts[i]!)) {
reorder = true;
break;
}
}
if (!reorder) return parts;
const moved: GooglePart[] = [];
const rest: GooglePart[] = [];
for (const p of parts) (movable(p) ? moved : rest).push(p);
return [...moved, ...rest];
}

/** Rebuild Gemini `contents` from kernel messages. Runs of same-side cores
* are merged into ONE content (Gemini rejects non-alternating roles): an
* assistant run becomes a single `{"role":"model"}` content whose parts are
* ordered reasoning, text, tool-call; a user-side run (user + tool-result)
* becomes a single `{"role":"user"}` content whose functionResponse parts
* lead. Mid-conversation system cores (host-synthetic; Gemini has no system
* role inside contents) ride on the user side as a text part. */
* 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. */
export function coreToGoogle(messages: BiliMessage[]): GoogleContent[] {
const out: GoogleContent[] = [];
let model: {
Expand All @@ -234,7 +266,10 @@ export function coreToGoogle(messages: BiliMessage[]): GoogleContent[] {
};
const flushUser = () => {
if (!user) return;
const parts = [...user.responses, ...user.content];
const parts = normalizeGoogleUserPartOrder([
...user.responses,
...user.content,
]);
user = null;
if (parts.length > 0) out.push({ role: "user", parts });
};
Expand Down
77 changes: 77 additions & 0 deletions kernel/tests/wire-google.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
googleSystemText,
googleToCore,
injectGoogleSystem,
normalizeGoogleUserPartOrder,
type GooglePart,
type GoogleRequestBody,
} from "../src/wire/google.js";
import {
Expand Down Expand Up @@ -389,3 +391,78 @@ test("google: edited core text re-renders plain while sibling raw parts and sign
{ videoMetadata: { startOffset: 0 } },
]);
});

test("normalizeGoogleUserPartOrder: stable partition; signed/thought/non-text parts never move", () => {
const fr: GooglePart = {
functionResponse: { name: "add", response: { result: 66 }, id: "c1" },
};
const pure: GooglePart[] = [
{ text: "hello" },
{ inlineData: { mimeType: "image/png", data: "x" } },
];
assert.equal(normalizeGoogleUserPartOrder(pure), pure);
assert.deepEqual(
normalizeGoogleUserPartOrder([
fr,
{ text: "nudge" },
{ inlineData: { mimeType: "image/png", data: "x" } },
{ text: "more" },
]),
[
{ text: "nudge" },
{ text: "more" },
fr,
{ inlineData: { mimeType: "image/png", data: "x" } },
],
);
const signed: GooglePart = { text: "signed", thoughtSignature: "sig" };
const thought: GooglePart = { text: "thinking", thought: true };
assert.deepEqual(
normalizeGoogleUserPartOrder([fr, signed, thought, { text: "plain" }]),
[{ text: "plain" }, fr, signed, thought],
);
});

test("coreToGoogle rebuilds a mixed user run with plain text before functionResponse parts (#3159)", () => {
const body: GoogleRequestBody = {
contents: [
{ role: "user", parts: [{ text: "go" }] },
{
role: "model",
parts: [{ functionCall: { name: "add", args: { a: 37, b: 29 }, id: "c1" } }],
},
{
role: "user",
parts: [
{ functionResponse: { name: "add", response: { result: 66 }, id: "c1" } },
{ text: "nudge" },
],
},
],
};
const contents = coreToGoogle(googleToCore(body).msgs);
assert.deepEqual(contents[2]!.parts, [
{ text: "nudge" },
{ functionResponse: { name: "add", response: { result: 66 }, id: "c1" } },
]);
});

test("an already-safe user part order round-trips byte-identical through coreToGoogle (#3159)", () => {
const body: GoogleRequestBody = {
contents: [
{ role: "user", parts: [{ text: "go" }] },
{
role: "model",
parts: [{ functionCall: { name: "add", args: { a: 37, b: 29 }, id: "c1" } }],
},
{
role: "user",
parts: [
{ text: "nudge" },
{ functionResponse: { name: "add", response: { result: 66 }, id: "c1" } },
],
},
],
};
assert.deepEqual(coreToGoogle(googleToCore(body).msgs), body.contents);
});
7 changes: 5 additions & 2 deletions src/chain-checkpoint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createHash } from "node:crypto";
import type { WireProtocol } from "./util.js";
import { normalizeGoogleUserPartOrder, type GooglePart } from "acp-kernel/wire";

// #1357/#1395/#1421 — Chain Checkpoint (first-processor-wins idempotent interop).
// A request-level checkpoint marks a request that already passed through a
Expand Down Expand Up @@ -542,15 +543,17 @@ export function insertCheckpointCarrier(parsed: unknown, wire: WireProtocol, tag
const role = typeof c.role === "string" ? c.role : "user";
if (role !== "model") {
const parts = Array.isArray(c.parts) ? (c.parts as unknown[]) : [];
// #3159: plain text after a functionResponse part 400s on Vertex —
// normalize whichever way the carrier lands (replace-in-place or append).
const pi = parts.length - 1;
const pp = parts[pi];
if (pp && typeof pp === "object" && !Array.isArray(pp)) {
const ppo = pp as Record<string, unknown>;
if (Object.keys(ppo).length === 1 && typeof ppo.text === "string" && parseChainCheckpoint(ppo.text)) {
return { ...body, contents: [...contents.slice(0, li), { ...c, parts: [...parts.slice(0, pi), { text: tag }] }] };
return { ...body, contents: [...contents.slice(0, li), { ...c, parts: normalizeGoogleUserPartOrder([...parts.slice(0, pi), { text: tag }] as GooglePart[]) }] };
}
}
return { ...body, contents: [...contents.slice(0, li), { ...c, parts: [...parts, { text: tag }] }] };
return { ...body, contents: [...contents.slice(0, li), { ...c, parts: normalizeGoogleUserPartOrder([...parts, { text: tag }] as GooglePart[]) }] };
}
}
return { ...body, contents: [...contents, { role: "user", parts: [{ text: tag }] }] };
Expand Down
9 changes: 7 additions & 2 deletions src/server/prepare-google.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import http from "node:http";
import type { CompressionCore, Config, CoreMessage, NudgeDecision, PackSurface, Prompts } from "acp-kernel";
import { renderNudgeText, viableRanges } from "acp-kernel";
import { coreToGoogle, googleToCore, type BiliMessage, type GoogleContent, type GoogleRequestBody, type GoogleSystemInstruction, type GoogleTool } from "acp-kernel/wire";
import { coreToGoogle, googleToCore, normalizeGoogleUserPartOrder, type BiliMessage, type GoogleContent, type GoogleRequestBody, type GoogleSystemInstruction, type GoogleTool } from "acp-kernel/wire";
import type { ProxyOptions } from "../config.js";
import { currentCalibrationFactor } from "../util.js";
import { diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, reapOrphansLogged, runNudgeDecision, stripKernelSummaries, usageGradeInputBaseline, type Prepared } from "../server.js";
Expand Down Expand Up @@ -31,7 +31,12 @@ function appendGoogleNudge(contents: GoogleContent[], text: string): GoogleConte
const last = contents[contents.length - 1];
if (last && (last.role ?? "user") !== "model") {
const parts = Array.isArray(last.parts) ? last.parts : [];
return [...contents.slice(0, -1), { ...last, parts: [...parts, { text }] }];
// #3159: a plain-text tail after a functionResponse part 400s on
// Vertex — normalize so the new text leads.
return [
...contents.slice(0, -1),
{ ...last, parts: normalizeGoogleUserPartOrder([...parts, { text }]) },
];
}
return [...contents, { role: "user", parts: [{ text }] }];
}
Expand Down
5 changes: 4 additions & 1 deletion src/wire-body.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { WireProtocol } from "./util.js";
import { normalizeGoogleUserPartOrder, type GooglePart } from "acp-kernel/wire";

/** Append `text` as a trailing user turn on the wire's own request body.
*
Expand Down Expand Up @@ -44,7 +45,9 @@ export function appendTrailingUserText(protocol: WireProtocol, body: string | Bu
const arr = contents as Record<string, unknown>[];
const last = arr[arr.length - 1];
if (last && typeof last === "object" && last.role === "user" && Array.isArray(last.parts)) {
last.parts = [...(last.parts as unknown[]), { text }];
// #3159: plain text after a functionResponse part 400s on Vertex —
// keep the canonical order when merging into a tool-result tail.
last.parts = normalizeGoogleUserPartOrder([...(last.parts as unknown[]), { text }] as GooglePart[]);
} else {
arr.push({ role: "user", parts: [{ text }] });
}
Expand Down
34 changes: 28 additions & 6 deletions tests/cache-friendly-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
// an EXACT byte-prefix of the next (mid-history never mutates); the
// volatile tail slots (chain checkpoint / nudge / imgNote / retrieval
// note, ≤ 4) may only appear as a contiguous tail run;
// #3159 (google lane only): Vertex rejects plain text after a
// functionResponse part inside one user content, so coreToGoogle
// forwards such a content with the plain-text parts LEADING — when a
// user content ending on tool results gains new text parts between
// turns, the previous body's trailing tool units shift right past the
// inserted text run (assertPrefix's allowCanonShift sanctions exactly
// that move); every other byte stays an exact prefix;
// P2 fold transitions (client re-sends its compress pair after a
// /__bili/plugin/tool fold):
// P2a the pre-fold span is replaced by exactly the expected carrier
Expand Down Expand Up @@ -290,11 +297,26 @@ function canonEq(a: Unit[], b: Unit[], label: string): void {
}
}

function assertPrefix(prev: Unit[], next: Unit[], label: string, expectNew: string[]): void {
for (let i = 0; i < prev.length; i++) {
assert.ok(eqUnit(prev[i]!, next[i]!), `${label}: mid-history unit[${i}] mutated\n previous=${JSON.stringify(prev[i])}\n next =${JSON.stringify(next[i])}`);
// #3159 (google lane only): a user content ending on functionResponse parts
// is forwarded with new plain-text parts LEADING, so between turns the
// previous body's trailing tool units shift right past an inserted run of
// text units from that same content. allowCanonShift sanctions exactly that
// move — text-unit insertions immediately before a surviving byte-equal tool
// unit — and nothing else; any other mutation still fails.
function assertPrefix(prev: Unit[], next: Unit[], label: string, expectNew: string[], allowCanonShift = false): void {
let pi = 0;
let ni = 0;
while (pi < prev.length) {
if (ni < next.length && eqUnit(prev[pi]!, next[ni]!)) { pi++; ni++; continue; }
const p = prev[pi]!;
const n = ni < next.length ? next[ni]! : null;
assert.ok(allowCanonShift && p.k !== "text" && n !== null && n.k === "text", `${label}: mid-history unit[${pi}] mutated\n previous=${JSON.stringify(p)}\n next =${JSON.stringify(n)}`);
let k = 0;
while (ni + k < next.length && next[ni + k]!.k === "text") k++;
assert.ok(k > 0 && ni + k < next.length && eqUnit(p, next[ni + k]!), `${label}: unit[${pi}] not realigned after ${k} inserted text unit(s)\n previous=${JSON.stringify(p)}\n next =${JSON.stringify(next.slice(ni, ni + k + 1))}`);
ni += k;
}
const added = next.slice(prev.length);
const added = next.slice(ni);
assert.deepEqual(added.map(keyOf), expectNew, `${label}: appended units ${JSON.stringify(added.map(keyOf))} != expected ${JSON.stringify(expectNew)}`);
}

Expand Down Expand Up @@ -592,9 +614,9 @@ for (const wire of ["anthropic", "openai", "responses", "google"] as Wire[]) {
assertPrefix(cans[0]!.core, cans[1]!.core, L(1), ["q3", "a3"]);
assertPrefix(cans[1]!.core, cans[2]!.core, L(2), ["q4", "a4"]);
assertFoldTransition(cans[2]!.core, cans[3]!.core, ["q1", "a1"], ["q1"], L(3)); // head boundary survives; anchor stripped (#1567)
assertPrefix(cans[3]!.core, cans[4]!.core, L(4), ["q5", "a5"]);
assertPrefix(cans[3]!.core, cans[4]!.core, L(4), wire === "google" ? ["a5"] : ["q5", "a5"], wire === "google"); // google: #3159 — q5 leads the fold-pair result
assertFoldTransition(cans[4]!.core, cans[5]!.core, ["q2", "a2", "q3"], [], L(5)); // anchor stripped (#1567)
assertPrefix(cans[5]!.core, cans[6]!.core, L(6), ["q7", "a7"]);
assertPrefix(cans[5]!.core, cans[6]!.core, L(6), wire === "google" ? ["a7"] : ["q7", "a7"], wire === "google"); // google: #3159 — q7 leads the fold-pair result
assertPrefix(cans[6]!.core, cans[7]!.core, L(7), ["q8", "a8"]);

for (let i = 3; i < cans.length; i++) {
Expand Down
32 changes: 16 additions & 16 deletions tests/golden/storage-render/google.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,84 +8,84 @@
},
{
"i": 1,
"sha": "f2304ead1cb1ba6a45d9a9fe3ee1adf428bd2da6c9b6c0868b057f5de88e5448",
"sha": "e91922e4e9c8bd804a5d70851c6eb36c35313fee08f3f8ed03c3370edc1973f1",
"bytes": 28994
},
{
"i": 2,
"sha": "f3caead28fe9f5463298037010de4a6da7e193893c394350ee856f88604fe10e",
"sha": "7ad79bc2a5cb75357b00a31f01a07a65ea543af87a2a84be52020c1f906512a7",
"bytes": 34820
},
{
"i": 3,
"sha": "8f1bb1090a0f451375e0842dd2cf6c098e27981b395d06d508095b49a076da5b",
"sha": "7d107e28634d26ee51033febb7fabc9ad3e8861180bb41a8ceaf5b393e9772c1",
"bytes": 43375
},
{
"i": 4,
"sha": "994526737d0e88ac920d46083bec343fcd6df1d3dab9389db578c5d05dbff902",
"sha": "6e8f516b2992ecd7b1cb8575bb272cd0a83afa0e1c48ed9172c34786f61bd11a",
"bytes": 49201
},
{
"i": 5,
"sha": "63da90071ec92f7ba2d3311a213ceb89d2b71fedb4c6bf36e46e72c672ea3bfd",
"sha": "448ad969da6c56cba686e0a978c93a388973d6cca1138eeb2b0e71d3e3f4e096",
"bytes": 57756
},
{
"i": 6,
"sha": "7973c51f87afe5c86f1db405fb35bc08adf0bb12260ebe55573d0aa47df4f385",
"sha": "c2dc7f94e11d9bf97804d38f1f2f75126c7e2d63aa252b9335f322f8ad2df6f0",
"bytes": 63582
},
{
"i": 7,
"sha": "a5052cf869a79bc59ff45e54b9cd54128792815c0e6e8252fcb587ffaf2d3b92",
"sha": "49a33d09117cd0bf72e6e7bd6743eefe2a2b7c0e163030f1a25f7d8a5f7b8a92",
"bytes": 72137
},
{
"i": 8,
"sha": "6f0f9a6e59865a099e40deae587d3d33955561a5f8e8e3a2218f8117b7c75fd9",
"sha": "20908c180801039f546635431c7c28f71b2de10f9672aff1f88bd2d3e549b9a6",
"bytes": 77963
},
{
"i": 9,
"sha": "48796aeee1714ad38248edbf25b78cb61a4ca3eb82fb8afeca79fb42e53260dc",
"sha": "6289a41bdefd707971fe7786e0eb427280439fdea15456a6c2820f511ab05529",
"bytes": 50549
},
{
"i": 10,
"sha": "884a2721898db188264dc89f82e8396cdb01d0f15c7e7cbe1f15a6ec7f15d9f7",
"sha": "99589eac7dc278aab04407c4ccf50163873c49b295c785fbe5b273d37ba2fec8",
"bytes": 57952
},
{
"i": 11,
"sha": "fca8f37555a5cc691f1ababdde584af0a132d29ff2bdca435c231e763751a50a",
"sha": "c7d18ef1a3f77a5650adbb7c4d186a8c68976f839b557fa5605200b38576b09d",
"bytes": 63701
},
{
"i": 12,
"sha": "540646448d404876a083e4073aab59e1e14ede8aea31d0a563fb4c38cfc848fe",
"sha": "1676577abc874430e62c3fcc195a8bb1a020e95aaab177503e21e833a5f4f814",
"bytes": 77706
},
{
"i": 13,
"sha": "447cdfbb1e875a06d8f20bdcedb79434095614e207a014ed113196673b6a7705",
"sha": "976bc5a9fc08922ec38ac5200692bc1963b7ccbf7768cbcd7329dee78c192d38",
"bytes": 83455
},
{
"i": 14,
"sha": "f418b52a98a55c5dd7c328fcbc256928887667e2dc46f51b14d9eaf16e358edd",
"sha": "3e44a6cc5e31953a4c6226092c1a8c8074af5140cdd9949db4956d1e72f9677a",
"bytes": 91985
}
],
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "a0ad11b7ba567ea6d182847f2a9a17c9700b1793f241c6e08adfe73c5172a112",
"stateDigest": "e2d98c51df1b704d6167b72081afa72599e1db51bc55170357feb4c22fbf28c3",
"sectionDigests": {
"state": "6d2f312d7bc924d7",
"blockContents": "473264d9df8ba338",
"stats": "5702a6493cdfe768",
"metadata": "fe714004e9ae3104"
"metadata": "4c966b20f33b4d9b"
},
"stateKeyDigests": {
"blocks": "8e14f0879bf7ebd7",
Expand Down
Loading
Loading