From 6134736febd7ce120530ea82d47948e50ed0ad86 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sun, 15 Nov 2026 22:46:31 +0800 Subject: [PATCH] fix: preserve signed Google text through continuations Fixes ranxianglei/billion-context#6842 --- src/loop/adapter-google.ts | 7 +++-- src/loop/core.ts | 14 ++++++--- src/plugin.ts | 19 +++++++----- src/stream-google.ts | 14 ++++++--- tests/google-complete-frame.test.ts | 47 ++++++++++++++++++++++++++--- 5 files changed, 77 insertions(+), 24 deletions(-) diff --git a/src/loop/adapter-google.ts b/src/loop/adapter-google.ts index 4d17d5d374..b45aceb7fe 100644 --- a/src/loop/adapter-google.ts +++ b/src/loop/adapter-google.ts @@ -634,13 +634,16 @@ export function createGoogleAdapter( }); continue; } - const clean = tagFilter.push(part.text); + // A signature may belong to ordinary text, without thought:true. + // Preserve its bytes and part boundary on both replay paths. + const signed = typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0; + const clean = signed ? part.text : tagFilter.push(part.text); if (clean !== part.text) { editedParts ??= [...parts]; editedParts[i] = { ...part, text: clean }; } if (clean.length === 0) continue; - partEvents.push({ kind: "text", delta: clean, rawHandled: true }); + partEvents.push({ kind: "text", delta: clean, rawHandled: true, ...(signed ? { rawGooglePart: part } : {}) }); } // Decide wire output only after every prose sibling has been // cleaned. Each complete frame belongs to one event, regardless diff --git a/src/loop/core.ts b/src/loop/core.ts index fa301f487c..5d19d8487a 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -15,7 +15,7 @@ import { maybeJudgeContinuity } from "../invariant-judge.js"; import type { SeparatorSlot } from "../server/responses-ordering.js"; import { countImagesInParsedBody, countImagesInRawBody } from "../image-tokens.js"; import { diagnoseSuccessWithoutUsage, lastCompressSuffix, releaseNudgeHold, withSessionLock, type Session } from "../session.js"; -import type { BiliMessage } from "acp-kernel/wire"; +import type { BiliMessage, GooglePart } from "acp-kernel/wire"; import { coreToAnthropic } from "acp-kernel/wire"; import { parseCompressInput, @@ -322,7 +322,7 @@ export interface RequestOptions { // arrive instead of being re-laid out as [reasoning…, text, tool…]. type RoundPart = | { kind: "reasoning"; text: string; signature: string } - | { kind: "text"; text: string } + | { kind: "text"; text: string; rawGooglePart?: GooglePart } | { kind: "redacted"; data: string } // #3158: a Gemini part whose only payload is a thoughtSignature (no text, // no functionCall). Recorded in stream order so a proxy-tool rebuild puts @@ -333,7 +333,7 @@ export type ParsedStreamEvent = // rawHandled: the adapter carries this delta in another event's complete // frame (or buffers that frame until tool settlement). Keep accounting it, // but do not synthesize a second client frame in continuation rounds. - | { kind: "text"; delta: string; raw?: Buffer; rawHandled?: boolean } + | { kind: "text"; delta: string; raw?: Buffer; rawHandled?: boolean; rawGooglePart?: GooglePart } // round2-hold: withheld marks a reasoning PAYLOAD the adapter kept off the // client wire (bili-owned round ≥ 2 on a signing upstream — see // ParseStreamOpts.upstreamSignsThinking). It still feeds the loop's @@ -963,8 +963,8 @@ export async function* runCompressLoop( if (ev.kind === "text") { assistantText += ev.delta; let tp = roundParts[roundParts.length - 1]; - if (!tp || tp.kind !== "text") { - tp = { kind: "text", text: "" }; + if (!tp || tp.kind !== "text" || tp.rawGooglePart !== undefined || ev.rawGooglePart !== undefined) { + tp = { kind: "text", text: "", ...(ev.rawGooglePart ? { rawGooglePart: ev.rawGooglePart } : {}) }; roundParts.push(tp); } tp.text += ev.delta; @@ -1531,6 +1531,10 @@ export async function* runCompressLoop( role: "assistant", contentType: "text", text: part.text, + ...(part.rawGooglePart ? { + googleThoughtSignature: part.rawGooglePart.thoughtSignature, + rawGoogleParts: [part.rawGooglePart], + } : {}), }); } else if (part.kind === "standalone_signature") { // #3158: put the standalone thoughtSignature back at its diff --git a/src/plugin.ts b/src/plugin.ts index 45cf4e2e38..2ae04fa446 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3737,10 +3737,11 @@ export async function pipePluginChatWithStrip( // field, so an interleaved thought/text pair in one frame never // shares held-back state. const field = p["thought"] === true ? "thinking" : "text"; + const signed = typeof p["thoughtSignature"] === "string" && p["thoughtSignature"].length > 0; // Whole-field tool-call emission on an absorb-instructed // request (m00885) — see processOpenai. Degenerate-close // residue (#2190) rides the same routing. - if (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !mayStartDegenerateRenderTag(raw) && !(absorbInstructed === true && mayStartToolCallEmission(raw)) && !anyPending()) { + if (signed || (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !mayStartDegenerateRenderTag(raw) && !(absorbInstructed === true && mayStartToolCallEmission(raw)) && !anyPending())) { if (raw.length > 0) { keptText = true; appendReply(field, index, raw); @@ -5178,12 +5179,16 @@ function stripGoogleChunk(obj: T, drop: boolean = false, requestText?: string ...cand, content: { ...cont, - parts: (cont["parts"] as unknown[]).map((p) => - // #1960/KDD#10: thought parts carry a thoughtSignature — leave them byte-for-byte. - p && typeof p === "object" && (p as Record)["thought"] !== true && typeof (p as Record)["text"] === "string" - ? { ...(p as Record), text: stripAcpTags((p as Record)["text"] as string, drop, requestText) } - : p, - ), + parts: (cont["parts"] as unknown[]).map((p) => { + if (!p || typeof p !== "object") return p; + const part = p as Record; + // #1960/KDD#10: ordinary text can carry a signature too. + return part["thought"] !== true + && !(typeof part["thoughtSignature"] === "string" && part["thoughtSignature"].length > 0) + && typeof part["text"] === "string" + ? { ...part, text: stripAcpTags(part["text"], drop, requestText) } + : p; + }), }, }; }); diff --git a/src/stream-google.ts b/src/stream-google.ts index fbe585404c..a722e2119a 100644 --- a/src/stream-google.ts +++ b/src/stream-google.ts @@ -31,7 +31,9 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator containsRenderTagText(text) || containsMarkerLineText(text) || containsBiliInternalText(text) || (absorbArmed && containsToolCallEmissionText(text)); // Capture request evidence before a yielded proxy tool can change the context. const requestText = absorbArmed && parts.some(part => part && typeof part === "object" - && (part as { thought?: boolean }).thought !== true && typeof part.text === "string" && containsEcho(part.text)) + && (part as { thought?: boolean }).thought !== true + && !(typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0) + && typeof part.text === "string" && containsEcho(part.text)) ? JSON.stringify(ctx.messages) : undefined; for (const part of parts) { if (!part || typeof part !== "object") continue; @@ -46,10 +48,12 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator 0) + && typeof part.text === "string" && containsEcho(part.text)) { ctx.log(`[tag-echo] stripped: non-stream google output ACP echo (render tags/markers/internal artifacts): ${part.text.slice(0, 120).replace(/\n/g, " ")}`); part.text = stripAcpTags(part.text, absorbArmed, requestText); } diff --git a/tests/google-complete-frame.test.ts b/tests/google-complete-frame.test.ts index 18bb8a8d94..76d8a66bc5 100644 --- a/tests/google-complete-frame.test.ts +++ b/tests/google-complete-frame.test.ts @@ -143,14 +143,15 @@ async function close(server: http.Server): Promise { await new Promise(resolve => server.close(() => resolve())); } -async function throughProxy(rounds: Array>>): Promise<{ calls: Array>; wire: string }> { +async function throughProxy(rounds: Array>>, options: { plugin?: boolean; stream?: boolean } = {}): Promise<{ calls: Array>; wire: string }> { const calls: Array> = []; const upstream = http.createServer(async (req, res) => { const buffers: Buffer[] = []; for await (const chunk of req) buffers.push(Buffer.from(chunk)); calls.push(JSON.parse(Buffer.concat(buffers).toString()) as Record); - res.writeHead(200, { "content-type": "text/event-stream" }); - res.end((rounds[calls.length - 1] ?? [candidate([{ text: "Unexpected retry." }], "STOP")]).map(sse).join("")); + const reply = rounds[calls.length - 1] ?? [candidate([{ text: "Unexpected retry." }], "STOP")]; + res.writeHead(200, { "content-type": options.stream === false ? "application/json" : "text/event-stream" }); + res.end(options.stream === false ? JSON.stringify(reply[0]) : reply.map(sse).join("")); }); upstream.listen(0, "127.0.0.1"); await once(upstream, "listening"); @@ -169,8 +170,9 @@ async function throughProxy(rounds: Array>>): Prom try { const upstreamPort = (upstream.address() as import("node:net").AddressInfo).port; const proxyPort = (proxy.address() as import("node:net").AddressInfo).port; - const response = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/gemini-2.5-pro:streamGenerateContent?alt=sse`, { - method: "POST", headers: { "content-type": "application/json", "x-acp-session": `complete-frame-${proxyPort}` }, + const method = options.stream === false ? "generateContent" : "streamGenerateContent?alt=sse"; + const response = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/gemini-2.5-pro:${method}`, { + method: "POST", headers: { "content-type": "application/json", "x-acp-session": `complete-frame-${proxyPort}`, ...(options.plugin ? { "x-bili-plugin": "pi", "x-bili-plugin-conversation": `complete-frame-${proxyPort}` } : {}) }, body: JSON.stringify({ contents: [{ role: "user", parts: [{ text: "Report status then answer." }] }] }), }); const wire = await response.text(); @@ -225,3 +227,38 @@ test("Google HTTP control: single-part replies remain single-part", async () => assert.equal(calls.length, 1); assert.deepEqual(partsOf(framesOf(wire)), [{ text: "Control answer." }]); }); + +for (const mode of ["sse", "array"] as const) { + test(`Google ${mode}: signed ordinary text bypasses prose filtering`, async () => { + const signed = { text: thought, thoughtSignature: signature }; + const events = await collect([candidate([signed]), candidate([], "STOP")], mode); + assert.deepEqual(partsOf(framesOf(forwarded(events))), [signed]); + assert.deepEqual(events.filter(event => event.kind === "text").map(event => event.delta), [thought]); + }); +} + +for (const plugin of [false, true]) { + for (const stream of [false, true]) { + test(`Google HTTP ${plugin ? "plugin" : "proxy"} ${stream ? "SSE" : "JSON"}: signed text stays byte-exact`, async () => { + const signed = { text: thought, thoughtSignature: signature }; + const { calls, wire } = await throughProxy([[candidate([signed], "STOP")]], { plugin, stream }); + assert.equal(calls.length, 1); + assert.deepEqual(partsOf(stream ? framesOf(wire) : [JSON.parse(wire) as Record]), [signed]); + }); + } +} + +test("Google HTTP proxy: signed text boundaries and signatures survive a tool continuation", async () => { + const signedA = { text: " Signed ordinary answer A. ", thoughtSignature: "c2lnLWE=" }; + const signedB = { text: "Signed ordinary answer B.\n", thoughtSignature: "c2lnLWI=" }; + const parts = [{ text: "Unsigned prefix." }, signedA, signedB, { text: "Unsigned suffix." }]; + const { calls, wire } = await throughProxy([ + [candidate(parts), candidate([{ functionCall: { id: "status", name: "acp_status", args: {} }, thoughtSignature: "c3RhdHVzLXNpZw==" }], "STOP")], + [candidate([{ text: "Final answer." }], "STOP")], + ]); + assert.equal(calls.length, 2); + const visible = partsOf(framesOf(wire)); + assert.deepEqual(visible.filter(part => part.thoughtSignature === signedA.thoughtSignature || part.thoughtSignature === signedB.thoughtSignature), [signedA, signedB]); + const replay = (calls[1]!.contents as Array<{ parts: Part[] }>).flatMap(content => content.parts); + assert.deepEqual(replay.filter(part => part.thoughtSignature === signedA.thoughtSignature || part.thoughtSignature === signedB.thoughtSignature), [signedA, signedB]); +});