diff --git a/kernel/package.json b/kernel/package.json index 47d27452be..badd1e6140 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.175", + "version": "0.0.176", "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 298d3948ea..19972b1390 100644 --- a/kernel/src/wire/google.ts +++ b/kernel/src/wire/google.ts @@ -309,6 +309,16 @@ export function googleToCore(body: GoogleRequestBody): GoogleFlat { return { msgs, systemText: googleSystemText(body) }; } +/** #5392 — Claude-family backends routed through Google's v1internal / Cloud + * Code Assist wire translate role:"user" parts into Anthropic content blocks + * IN ORDER, and Anthropic requires every tool_result block to LEAD the user + * message that follows an assistant tool_use turn — the OPPOSITE of the Gemini + * text-first rule below. Reordering such a body to text-first 400s it ("Each + * 'tool_use' block must have a corresponding 'tool_result' block in the next + * message"). Matched against the TARGET model name (case-insensitive), the same + * signal the host threads through every google-wire boundary. */ +const CLAUDE_WIRE_MODEL = /claude/i; + /** #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 @@ -317,8 +327,16 @@ export function googleToCore(body: GoogleRequestBody): GoogleFlat { * 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[] { + * nothing is dropped, renamed or rewritten. + * #5392 — when `opts.model` names a Claude-family target the input order is + * returned untouched: its natural responses-first shape is exactly what an + * Anthropic-behind-Google backend requires, so the text-first partition below + * applies only to non-Claude targets. */ +export function normalizeGoogleUserPartOrder( + parts: GooglePart[], + opts?: { model?: string }, +): GooglePart[] { + if (opts && typeof opts.model === "string" && CLAUDE_WIRE_MODEL.test(opts.model)) return parts; const firstFr = parts.findIndex((p) => p.functionResponse !== undefined); if (firstFr === -1) return parts; const movable = (p: GooglePart) => @@ -359,7 +377,7 @@ export function normalizeGoogleUserPartOrder(parts: GooglePart[]): GooglePart[] * 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[] { +export function coreToGoogle(messages: BiliMessage[], opts?: { model?: string }): GoogleContent[] { const out: GoogleContent[] = []; let model: { reasoning: GooglePart[]; @@ -375,10 +393,10 @@ export function coreToGoogle(messages: BiliMessage[]): GoogleContent[] { }; const flushUser = () => { if (!user) return; - const parts = normalizeGoogleUserPartOrder([ - ...user.responses, - ...user.content, - ]); + const parts = normalizeGoogleUserPartOrder( + [...user.responses, ...user.content], + { model: opts?.model }, + ); user = null; if (parts.length > 0) out.push({ role: "user", parts }); }; diff --git a/kernel/tests/wire-google.test.ts b/kernel/tests/wire-google.test.ts index f35d0069b2..b7c67a9cc4 100644 --- a/kernel/tests/wire-google.test.ts +++ b/kernel/tests/wire-google.test.ts @@ -478,3 +478,43 @@ test("an already-safe user part order round-trips byte-identical through coreToG }; assert.deepEqual(coreToGoogle(googleToCore(body).msgs), body.contents); }); + +test("normalizeGoogleUserPartOrder: Claude-family target keeps natural responses-first order (#5392)", () => { + const fr: GooglePart = { + functionResponse: { name: "add", response: { result: 66 }, id: "c1" }, + }; + const parts: GooglePart[] = [fr, { text: "nudge" }]; + assert.deepEqual(normalizeGoogleUserPartOrder(parts), [ + { text: "nudge" }, + fr, + ]); + // Anthropic-behind-Google needs tool_result(functionResponse) to LEAD the user + // message — the input order is returned byte-for-byte (same reference). + for (const m of ["claude-opus-4-8", "Claude-Sonnet-4-6"]) { + assert.equal(normalizeGoogleUserPartOrder(parts, { model: m }), parts); + } +}); + +test("coreToGoogle: Claude-family target emits functionResponse before text (#5392)", () => { + 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, { model: "claude-opus-4-8" }); + assert.deepEqual(contents[2]!.parts, [ + { functionResponse: { name: "add", response: { result: 66 }, id: "c1" } }, + { text: "nudge" }, + ]); +}); diff --git a/src/chain-checkpoint.ts b/src/chain-checkpoint.ts index cffdb20a3a..9c97795055 100644 --- a/src/chain-checkpoint.ts +++ b/src/chain-checkpoint.ts @@ -469,7 +469,7 @@ const ZERO_DIGEST = "sha256:" + "0".repeat(64); * see module header). Restamping an already-stamped body REPLACES the * trailing carrier instead of stacking. Returns the new body or null when * the shape admits no carrier slot. */ -export function insertCheckpointCarrier(parsed: unknown, wire: WireProtocol, tag: string): unknown | null { +export function insertCheckpointCarrier(parsed: unknown, wire: WireProtocol, tag: string, model?: string): unknown | null { if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return null; const body = parsed as Record; if (wire === "openai") { @@ -550,10 +550,10 @@ export function insertCheckpointCarrier(parsed: unknown, wire: WireProtocol, tag if (pp && typeof pp === "object" && !Array.isArray(pp)) { const ppo = pp as Record; if (Object.keys(ppo).length === 1 && typeof ppo.text === "string" && parseChainCheckpoint(ppo.text)) { - 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: normalizeGoogleUserPartOrder([...parts.slice(0, pi), { text: tag }] as GooglePart[], { model }) }] }; } } - return { ...body, contents: [...contents.slice(0, li), { ...c, parts: normalizeGoogleUserPartOrder([...parts, { text: tag }] as GooglePart[]) }] }; + return { ...body, contents: [...contents.slice(0, li), { ...c, parts: normalizeGoogleUserPartOrder([...parts, { text: tag }] as GooglePart[], { model }) }] }; } } return { ...body, contents: [...contents, { role: "user", parts: [{ text: tag }] }] }; @@ -565,14 +565,14 @@ export function insertCheckpointCarrier(parsed: unknown, wire: WireProtocol, tag * so any downstream bili verifying the same bytes recomputes the same value. * requestId derives from the digest — deterministic, constant-size, and it * correlates the stamp with the exact body it covers. */ -export function stampOutbound(parsed: unknown, wire: WireProtocol, processor: string, nowMs: number = Date.now()): unknown | null { +export function stampOutbound(parsed: unknown, wire: WireProtocol, processor: string, nowMs: number = Date.now(), model?: string): unknown | null { if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return null; const placeholder: Pick = { v: SUPPORTED_CHECKPOINT_VERSION, processor, issuedAt: nowMs, requestId: "-" }; const digest = computeCheckpointDigest(parsed, wire, placeholder); if (digest === null) return null; const requestId = digest.slice("sha256:".length, "sha256:".length + 8); const tag = renderChainCheckpoint({ v: SUPPORTED_CHECKPOINT_VERSION, processor, issuedAt: nowMs, requestId, digest }); - return insertCheckpointCarrier(parsed, wire, tag); + return insertCheckpointCarrier(parsed, wire, tag, model); } // Generation-side digest (step 3 stamps with this): insert a zero-digest diff --git a/src/degenerate-retry.ts b/src/degenerate-retry.ts index a21b28bd73..6d20098e39 100644 --- a/src/degenerate-retry.ts +++ b/src/degenerate-retry.ts @@ -26,12 +26,14 @@ export const DEGENERATE_RETRY_NUDGE = /** The retry body: the forwarded body with the continuation nudge appended as a * trailing user turn. Null when the body cannot carry one. */ -export function injectContinuationNudge(protocol: WireProtocol, body: string | Buffer): string | null { - return appendTrailingUserText(protocol, body, DEGENERATE_RETRY_NUDGE); +export function injectContinuationNudge(protocol: WireProtocol, body: string | Buffer, model?: string): string | null { + return appendTrailingUserText(protocol, body, DEGENERATE_RETRY_NUDGE, model); } interface ContinuationRetryOpts { protocol: WireProtocol; + /** #5392: target model — threaded so the appended nudge keeps tool_result-first order on Claude-through-Google targets. */ + model?: string; /** The body that was forwarded upstream (the agent's own, in plugin mode). */ body: string | Buffer; upstreamUrl: string; @@ -75,7 +77,7 @@ export function makeContinuationRefetch(opts: ContinuationRetryOpts): () => Prom /* unparseable body: injectContinuationNudge below reports and skips */ } } - const retryBody = injectContinuationNudge(opts.protocol, opts.body); + const retryBody = injectContinuationNudge(opts.protocol, opts.body, opts.model); if (retryBody === null) { opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry skipped: request body carries no turn array`); return null; diff --git a/src/fake-completion.ts b/src/fake-completion.ts index 21175efc5a..d74856b69d 100644 --- a/src/fake-completion.ts +++ b/src/fake-completion.ts @@ -104,6 +104,6 @@ export function isFakeCompletion(protocol: WireProtocol, rawText: string): boole /** Append the fake-completion hint to the request body as a trailing user turn * (per-wire shapes live in `appendTrailingUserText`). Returns the hinted JSON, * or null if the body is unparseable, in which case the retry is skipped. */ -export function injectFakeCompletionHint(protocol: WireProtocol, body: string | Buffer): string | null { - return appendTrailingUserText(protocol, body, FAKE_COMPLETION_HINT); +export function injectFakeCompletionHint(protocol: WireProtocol, body: string | Buffer, model?: string): string | null { + return appendTrailingUserText(protocol, body, FAKE_COMPLETION_HINT, model); } diff --git a/src/loop/adapter-google.ts b/src/loop/adapter-google.ts index ab5e3f1c7b..fe9e7216d4 100644 --- a/src/loop/adapter-google.ts +++ b/src/loop/adapter-google.ts @@ -273,7 +273,7 @@ export function createGoogleAdapter( return { buildRequest(coreMessages, systemPrompt, body) { - const contents = coreToGoogle(coreMessages); + const contents = coreToGoogle(coreMessages, { model: modelVersion }); if (notes && notes.length > 0) { for (const text of notes) contents.push({ role: "user", parts: [{ text }] }); } diff --git a/src/server/handle.ts b/src/server/handle.ts index 9c212fb2ff..ce0273d4a7 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -2557,7 +2557,7 @@ async function handleRequest( } if (countTokens) { return protocol === "google" - ? prepareGoogleCountTokens(work as GoogleRequestBody, core, reqConfig, log, session) + ? prepareGoogleCountTokens(work as GoogleRequestBody, core, reqConfig, log, session, googleModel ?? v1Model) : prepareCountTokens(work as AnthropicRequestBody, core, reqConfig, log, session); } if (protocol === "google") { diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 391443dc17..08afc879fb 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -36,15 +36,16 @@ import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { injectGoogleTool, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js"; -function appendGoogleNudge(contents: GoogleContent[], text: string): GoogleContent[] { +function appendGoogleNudge(contents: GoogleContent[], text: string, model?: string): GoogleContent[] { const last = contents[contents.length - 1]; if (last && (last.role ?? "user") !== "model") { const parts = Array.isArray(last.parts) ? last.parts : []; // #3159: a plain-text tail after a functionResponse part 400s on - // Vertex — normalize so the new text leads. + // Vertex — normalize so the new text leads (#5392: skipped for Claude + // targets, whose Anthropic-behind-Google backend wants tool_result first). return [ ...contents.slice(0, -1), - { ...last, parts: normalizeGoogleUserPartOrder([...parts, { text }]) }, + { ...last, parts: normalizeGoogleUserPartOrder([...parts, { text }], { model }) }, ]; } return [...contents, { role: "user", parts: [{ text }] }]; @@ -283,7 +284,7 @@ export async function prepareGoogle( // #3400: egress image rescale — outbound copies only; the originals // stay in session state/persistence untouched. const egressMessages = await applyImageRescalePass(processedMessages as BiliMessage[], { session, sessionId, log, maxDim: imageRescaleMaxDim(), toolArmed: imageRescaleToolArmed(session) }); - rebuiltContents = coreToGoogle(egressMessages); + rebuiltContents = coreToGoogle(egressMessages, { model }); // ONLY the static compress prompt joins the client's system text — the // system instruction is the prefix-cache anchor and must stay @@ -317,7 +318,7 @@ export async function prepareGoogle( toolsOut = injectGoogleTool(parsed.tools, [...(absorbActive ? [absorbTools.google] : []), ...(rulesActive ? [RULE_TOOL_GOOGLE] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).google] : []), ...(imageRescaleToolArmed(session) ? [READ_ORIGINAL_IMAGE_TOOL_GOOGLE] : [])], surface?.toolPrompts, ccrEnabled(session), externalSummaryEnabled(config)); } if (sysNotes.length > 0) { - rebuiltContents = appendGoogleNudge(rebuiltContents, sysNotes.join("\n\n---\n\n")); + rebuiltContents = appendGoogleNudge(rebuiltContents, sysNotes.join("\n\n---\n\n"), model); } if (willInjectNudge && turn.nudge) { // #2228: model-decided timing (see prepareAnthropic for the policy): @@ -330,7 +331,7 @@ export async function prepareGoogle( const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections, "google"); const renderedWithPayload = rendered.text; if (rendered.text) { - rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withFirstSightDrain(renderedWithPayload, turn.nudge.reason, externalSummaryEnabled(config)), visibilityMarkers)); + rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withFirstSightDrain(renderedWithPayload, turn.nudge.reason, externalSummaryEnabled(config)), visibilityMarkers), model); nudgeOutcome = "injected"; } else { nudgeOutcome = "skipped"; @@ -346,7 +347,7 @@ export async function prepareGoogle( if (outcome.kind === "yes") { const span = resolveDecisionRange(outcome, ranges); if (span) { - rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withFirstSightDrain(buildDirectiveText(span.startRef, span.endRef, outcome.topic), turn.nudge.reason, externalSummaryEnabled(config)), visibilityMarkers)); + rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withFirstSightDrain(buildDirectiveText(span.startRef, span.endRef, outcome.topic), turn.nudge.reason, externalSummaryEnabled(config)), visibilityMarkers), model); nudgeOutcome = "injected"; } else { nudgeOutcome = "skipped"; @@ -362,7 +363,7 @@ export async function prepareGoogle( const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections, "google"); const renderedWithPayload = rendered.text; if (rendered.text) { - rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withFirstSightDrain(renderedWithPayload, turn.nudge.reason, externalSummaryEnabled(config)), visibilityMarkers)); + rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withFirstSightDrain(renderedWithPayload, turn.nudge.reason, externalSummaryEnabled(config)), visibilityMarkers), model); nudgeOutcome = "injected"; } else { nudgeOutcome = "skipped"; @@ -418,6 +419,7 @@ export function prepareGoogleCountTokens( config: Config, log: (level: string, msg: string) => void, session: Session, + model?: string, ): Prepared { const sessionId = session.id; try { @@ -426,7 +428,7 @@ export function prepareGoogleCountTokens( // counted, but nothing is adopted (state is discarded here too). const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: usageGradeInputBaseline(session), renderTags: knobRenderNone() ? "none" : "text-only", contentStore: contentStoreOf(session) }); const stripped = stripKernelSummaries(turn.messages, turn.state); - const rebuilt: GoogleRequestBody = { ...parsed, contents: coreToGoogle(stripped as BiliMessage[]) }; + const rebuilt: GoogleRequestBody = { ...parsed, contents: coreToGoogle(stripped as BiliMessage[], { model }) }; log("info", `[${sessionId}] countTokens pruned: ${msgs.length} → ${stripped.length} msgs`); return { body: JSON.stringify(rebuilt), diff --git a/src/server/relay.ts b/src/server/relay.ts index 1b7642d5f3..07a067e4f0 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -432,7 +432,7 @@ async function forwardCaptured( // recognition + hop passthrough are unaffected by this switch. if (prepared && !prepared.sidePassthrough && prepared.processedMessages.length > 0 && typeof wireBody === "string" && opts.chainEgressStamp === true) { try { - const stamped = stampOutbound(JSON.parse(wireBody), prepared.protocol, instanceId); + const stamped = stampOutbound(JSON.parse(wireBody), prepared.protocol, instanceId, Date.now(), prepared.model ?? prepared.google?.model); if (stamped !== null) { wireBody = JSON.stringify(stamped); log("debug", `[${prepared.session.id}] [stamp] outbound ${prepared.protocol} request carries checkpoint carrier`); @@ -1750,6 +1750,7 @@ async function forwardCaptured( signal: clientAbort.signal, session: prepared.session, log, + model: prepared.model ?? prepared.google?.model, }); pluginBody = bufferToStream(resolvedBuf); } @@ -1794,6 +1795,7 @@ async function forwardCaptured( signal: clientAbort.signal, log, label: prepared.session.id, + model: prepared.model ?? prepared.google?.model, }), targetOrigin, absorbInstructed, @@ -1822,6 +1824,7 @@ async function forwardCaptured( signal: clientAbort.signal, log, label: prepared.session.id, + model: prepared.model ?? prepared.google?.model, }), targetOrigin, absorbInstructed, @@ -1894,6 +1897,7 @@ async function forwardCaptured( signal: clientAbort.signal, session: prepared.session, log, + model: prepared.model ?? prepared.google?.model, }); responseBody = bufferToStream(resolvedBuf); } @@ -1955,6 +1959,7 @@ async function forwardCaptured( signal: clientAbort.signal, log, label: prepared.session.id, + model: prepared.model ?? prepared.google?.model, }), undefined, undefined, @@ -2000,6 +2005,7 @@ async function forwardCaptured( signal: clientAbort.signal, log, label: p.session.id, + model: p.model ?? p.google?.model, }), undefined, undefined, @@ -2025,6 +2031,7 @@ async function forwardCaptured( signal: clientAbort.signal, log, label: p.session.id, + model: p.model ?? p.google?.model, }), undefined, undefined, @@ -2348,6 +2355,9 @@ async function resolveFakeCompletion( signal: AbortSignal; session: Session; log: (level: string, msg: string) => void; + /** #5392: target model name — lets the google-wire part-order normalize + * skip the WC-018 text-first reorder for Claude models. */ + model?: string; /** #1884: re-sign the retry body before it hits the wire (armed * re-sign lane only; undefined on unsigned traffic). */ resign?: (headers: Record, body: string | Buffer) => void; @@ -2363,7 +2373,7 @@ async function resolveFakeCompletion( const priorStreak = (opts.session.metadata.fakeCompletionStreak as number | undefined) ?? 0; if (max > 0 && priorStreak < max && isFakeCompletion(opts.protocol, buffer.toString("utf8"))) { for (let attempt = 1; attempt <= max && !opts.signal.aborted; attempt++) { - const hinted = injectFakeCompletionHint(opts.protocol, opts.wireBody); + const hinted = injectFakeCompletionHint(opts.protocol, opts.wireBody, opts.model); if (hinted === null) break; const wireHinted = opts.wrapBody ? opts.wrapBody(hinted) : hinted; opts.log("warn", `[${sid}] fake completion (tool-call XML, no tool block); retry ${attempt}/${max} with corrective hint`); diff --git a/src/wire-body.ts b/src/wire-body.ts index 9470ac67b8..3fc261d82e 100644 --- a/src/wire-body.ts +++ b/src/wire-body.ts @@ -12,7 +12,7 @@ import { normalizeGoogleUserPartOrder, type GooglePart } from "acp-kernel/wire"; * * Returns null when the body is unparseable or carries no turn array — the * caller skips the mutation rather than sending a body it has mangled. */ -export function appendTrailingUserText(protocol: WireProtocol, body: string | Buffer, text: string): string | null { +export function appendTrailingUserText(protocol: WireProtocol, body: string | Buffer, text: string, model?: string): string | null { const raw = typeof body === "string" ? body : body.toString("utf8"); let obj: Record; try { @@ -47,7 +47,7 @@ export function appendTrailingUserText(protocol: WireProtocol, body: string | Bu if (last && typeof last === "object" && last.role === "user" && Array.isArray(last.parts)) { // #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[]); + last.parts = normalizeGoogleUserPartOrder([...(last.parts as unknown[]), { text }] as GooglePart[], { model }); } else { arr.push({ role: "user", parts: [{ text }] }); } diff --git a/tests/issue5392-claude-google-part-order.test.ts b/tests/issue5392-claude-google-part-order.test.ts new file mode 100644 index 0000000000..5686989f3d --- /dev/null +++ b/tests/issue5392-claude-google-part-order.test.ts @@ -0,0 +1,190 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { once } from "node:events"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { startFakeUpstream } from "./wire-contract-fakes.ts"; + +// Regression for #5392: Claude-family models routed through Google's v1internal / +// Cloud Code Assist wire translate role:"user" parts into Anthropic content blocks +// IN ORDER, and Anthropic requires every tool_result block to LEAD the user message +// that follows an assistant tool_use turn — the OPPOSITE of the WC-018 text-first +// rule (#3159) that fixes Gemini/Vertex. Bili's unconditional part-order normalize +// reordered such a body to [text, functionResponse] and upstream 400'd ("Each +// 'tool_use' block must have a corresponding 'tool_result' block in the next +// message"). coreToGoogle (and the host boundaries) now skip the reorder when the +// target model matches /claude/i, keeping the natural responses-first order; the +// validation-parity fake skips WC-018 for the same Claude targets. + +const CLAUDE_MODEL = "claude-opus-4-8"; + +interface Harness { + proxyUrl: string; + close(): Promise; +} + +async function startHarness(fakeUrl: string): Promise { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: fakeUrl, + routes: { [fakeUrl]: { models: { [CLAUDE_MODEL]: { context: 1_000_000 } } } }, + modelContextLimit: 1_000_000, + kernelConfig: defaultConfig(1_000_000), + compress: { injectTool: true, injectNudge: true }, + stableSystemAnchor: true, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + await once(proxy, "listening"); + const port = (proxy.address() as { port: number }).port; + return { + proxyUrl: `http://127.0.0.1:${port}/bili/${fakeUrl}`, + close: () => new Promise((resolve, reject) => proxy.close((e) => (e ? reject(e) : resolve()))), + }; +} + +const TURN_1_CONTENTS = [{ role: "user", parts: [{ text: "What is 37 plus 29?" }] }]; +const MODEL_TURN = { role: "model", parts: [{ functionCall: { name: "add", args: { a: 37, b: 29 }, id: "call_1" } }] }; +const TOOL_RESPONSE_PART = { functionResponse: { name: "add", response: { result: 66 }, id: "call_1" } }; + +async function sendTurn(h: Harness, sessionId: string, contents: unknown[]): Promise { + const res = await fetch(`${h.proxyUrl}/v1beta/models/${CLAUDE_MODEL}:streamGenerateContent?alt=sse`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": sessionId }, + body: JSON.stringify({ contents, generationConfig: { maxOutputTokens: 4096 } }), + }); + await res.text(); + return res.status; +} + +test("#5392 claude-via-google: a user turn carrying a tool response forwards tool_result(functionResponse) FIRST", async () => { + const fake = await startFakeUpstream("google"); + let h: Harness | undefined; + try { + h = await startHarness(fake.url); + assert.equal(await sendTurn(h, "issue5392-claude", TURN_1_CONTENTS), 200, "turn 1 baseline completes"); + // No system instruction ⇒ no anchor notes; tiny session ⇒ nudge idle. + // Nothing appends — the forwarded shape is the pure rebuild of the + // client's own [functionResponse, text] order, which a Claude-behind- + // Google backend REQUIRES (tool_result-first). Pre-fix bili reordered + // this to [text, functionResponse], the exact bytes that 400'd upstream. + assert.equal( + await sendTurn( + h, + "issue5392-claude", + [...TURN_1_CONTENTS, MODEL_TURN, { role: "user", parts: [TOOL_RESPONSE_PART, { text: "carry on" }] }], + ), + 200, + "turn 2 completes", + ); + assert.equal(fake.violations.length, 0, `fake upstream violations:\n${fake.violations.join("\n")}`); + + const capturedBody = fake.requests[1]?.body as + | { contents?: Array<{ role?: string; parts?: Array> }> } + | undefined; + const last = capturedBody?.contents?.[capturedBody.contents.length - 1]; + assert.ok(last, "forwarded body has contents"); + assert.equal(last.role, "user"); + const parts = last.parts ?? []; + assert.ok(parts.length >= 2, `expected functionResponse + text parts, got ${JSON.stringify(parts)}`); + // THE regression assertion: tool_result(functionResponse) leads the + // user message that follows the assistant tool_use turn. + assert.equal((parts[0]?.functionResponse as { id?: string } | undefined)?.id, "call_1", "forwarded user content LEADS with the functionResponse (tool_result) part"); + const t1 = typeof parts[1]?.text === "string" ? parts[1].text : ""; + assert.ok(t1.endsWith("carry on"), "the client text trails the functionResponse part"); + } finally { + if (h) await h.close(); + await fake.close(); + } +}); + +test("#5392 claude-via-google: the SAME turn on a non-Claude target still normalizes text-first (WC-018 preserved)", async () => { + // Guard against the carve-out overreaching: a Gemini-style target must keep + // getting the WC-018 text-first reorder, or #3159 regresses. + const fake = await startFakeUpstream("google"); + let h: Harness | undefined; + try { + // Re-point the harness at a non-claude model name on the same fake. + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: fake.url, + routes: { [fake.url]: { models: { "gemini-test": { context: 1_000_000 } } } }, + modelContextLimit: 1_000_000, + kernelConfig: defaultConfig(1_000_000), + compress: { injectTool: true, injectNudge: true }, + stableSystemAnchor: true, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + await once(proxy, "listening"); + const port = (proxy.address() as { port: number }).port; + h = { + proxyUrl: `http://127.0.0.1:${port}/bili/${fake.url}`, + close: () => new Promise((resolve, reject) => proxy.close((e) => (e ? reject(e) : resolve()))), + }; + const post = async (sessionId: string, contents: unknown[]) => { + const res = await fetch(`${h!.proxyUrl}/v1beta/models/gemini-test:streamGenerateContent?alt=sse`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": sessionId }, + body: JSON.stringify({ contents, generationConfig: { maxOutputTokens: 4096 } }), + }); + await res.text(); + return res.status; + }; + assert.equal(await post("issue5392-gemini", TURN_1_CONTENTS), 200, "turn 1 baseline completes"); + assert.equal( + await post( + "issue5392-gemini", + [...TURN_1_CONTENTS, MODEL_TURN, { role: "user", parts: [TOOL_RESPONSE_PART, { text: "carry on" }] }], + ), + 200, + "turn 2 completes — WC-018 still enforced for the gemini target", + ); + assert.equal(fake.violations.length, 0, `fake upstream violations:\n${fake.violations.join("\n")}`); + const capturedBody = fake.requests[1]?.body as + | { contents?: Array<{ role?: string; parts?: Array> }> } + | undefined; + const last = capturedBody?.contents?.[capturedBody.contents.length - 1]; + const parts = last?.parts ?? []; + assert.equal(typeof parts[0]?.text, "string", "non-claude target still leads with the TEXT part (WC-018 intact)"); + assert.equal((parts[1]?.functionResponse as { id?: string } | undefined)?.id, "call_1", "the functionResponse trails the text"); + } finally { + if (h) await h.close(); + await fake.close(); + } +}); diff --git a/tests/wire-contract-fakes.ts b/tests/wire-contract-fakes.ts index c2aba2eda3..92120c209a 100644 --- a/tests/wire-contract-fakes.ts +++ b/tests/wire-contract-fakes.ts @@ -44,7 +44,7 @@ export const WIRE_RULES: readonly WireRule[] = [ id: "WC-018", wire: "google", summary: - "Vertex rejects a request whose user content carries plain (unsigned, non-thought) text AFTER a functionResponse part — 400 'Requests ending with a model turn are not supported'; text-before-functionResponse is accepted. bili normalizes user-side part order at both boundaries: coreToGoogle's flushUser and appendGoogleNudge (stable partition of movable plain text ahead of non-text parts; signed/thought parts never move)", + "Vertex rejects a request whose user content carries plain (unsigned, non-thought) text AFTER a functionResponse part — 400 'Requests ending with a model turn are not supported'; text-before-functionResponse is accepted. bili normalizes user-side part order at both boundaries: coreToGoogle's flushUser and appendGoogleNudge (stable partition of movable plain text ahead of non-text parts; signed/thought parts never move) — EXCEPT for Claude-family targets routed through Google's v1internal/Cloud Code wire, whose Anthropic backend requires the OPPOSITE tool_result-first order, so the normalize is skipped when the target model matches /claude/i (#5392)", provenance: "bili #3159 production 400 (dsh-vertex-argon 1.1.4 / DSH 0.2.0-rc.2 over billion-context 0.1.196, official Vertex endpoint, gemini-4-argon generateContent): after a successful tool call the next request 400'd because Bili appended a plain-text nudge AFTER the trailing functionResponse part. Controlled A/B matrix on the official endpoint (issue table, 2026-10-14): [functionResponse]→200, [functionResponse, text]→400, [text, functionResponse]→200. The Gemini Developer API accepts both orders — the strictness is Vertex-specific.", }, @@ -504,14 +504,18 @@ export function validateCodexResponsesWsCreate(body: unknown): string[] { } /** WC-006, WC-018 on a Gemini generateContent/streamGenerateContent body. */ -function validateGoogleBody(body: unknown): string[] { +function validateGoogleBody(body: unknown, model?: string): string[] { const out: string[] = []; if (!isPlainObject(body)) return out; // WC-018: Vertex rejects a request whose user content carries plain // (unsigned, non-thought) text AFTER a functionResponse part — 400 // "Requests ending with a model turn are not supported" (#3159). Runs // before the tools early-return like every other contents-level rule. - if (Array.isArray(body.contents)) { + // #5392: skipped for Claude-family targets — their Anthropic-behind-Google + // backend needs the OPPOSITE order (tool_result-first), which is exactly the + // natural responses-first shape coreToGoogle emits when told the model. + const claudeWire = typeof model === "string" && /claude/i.test(model); + if (!claudeWire && Array.isArray(body.contents)) { body.contents.forEach((content, i) => { if (!isPlainObject(content) || content.role === "model") return; if (!Array.isArray(content.parts)) return; @@ -776,7 +780,8 @@ export async function startFakeUpstream(wire: Wire, opts?: { replyText?: string; try { parsed = JSON.parse(Buffer.concat(chunks).toString("utf8") || "{}"); } catch { /* non-JSON body: nothing to validate */ } - const found = validate(parsed); + const gmodel = wire === "google" ? (req.url ?? "").match(/\/models\/([^/:]+):/)?.[1] : undefined; + const found = gmodel !== undefined ? validateGoogleBody(parsed, gmodel) : validate(parsed); const pathInfo = lane === "vertex-anthropic" ? vertexAnthropicPathInfo(req.url ?? "") : null; const streaming = pathInfo?.method === "streamRawPredict"; requests.push({ url: req.url ?? "", body: parsed, ...(lane === "vertex-anthropic" ? { model: pathInfo?.model ?? null } : {}) });