From 2a355a62bb8c26f002f15248713b3d949ff97aac Mon Sep 17 00:00:00 2001 From: tinysunspot <345811623+tinysunspot@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:56:13 +0800 Subject: [PATCH] Guard the google lane against inert-frame tail drains #5849 added hadTextOtherThanTextFields to the chat lane after a text-less frame drained a held render-tag head, letting the rest of the tag reach the host as prose. The google lane kept the unconditional drain, on the assumption that it never sees such frames -- an absence of observation, not a property of the lane. A Gemini progress frame whose only part is {text:""} reproduces the same split here, so gate that exit on googleFrameHasNonText, the predicate the rebuilt branch three lines above already uses. --- src/plugin.ts | 5 +- ...-passthrough-progress-frame-google.test.ts | 229 ++++++++++++++++++ 2 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 tests/plugin-passthrough-progress-frame-google.test.ts diff --git a/src/plugin.ts b/src/plugin.ts index a4824a39d0..dd72be102e 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3745,7 +3745,10 @@ export async function pipePluginChatWithStrip( // structured or user data (#1039) and are excluded from the audit. if (!googleFrameHasNonText(ev)) auditRawForward(rawEvent); if (drain.length > 0) return drain + rawEvent + "\n\n"; - if (!hadText && anyPending()) return flushTails() + rawEvent + "\n\n"; + // #5848 twin on the google lane: a frame carrying only empty parts has + // nothing a held tail must precede, and draining on it split a render tag + // so the remainder reached the host as prose (the #5192 shape). + if (!hadText && anyPending() && googleFrameHasNonText(ev)) return flushTails() + rawEvent + "\n\n"; return rawEvent + "\n\n"; }; try { diff --git a/tests/plugin-passthrough-progress-frame-google.test.ts b/tests/plugin-passthrough-progress-frame-google.test.ts new file mode 100644 index 0000000000..c6984c9d9d --- /dev/null +++ b/tests/plugin-passthrough-progress-frame-google.test.ts @@ -0,0 +1,229 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { pipePluginChatWithStrip } from "../src/plugin.ts"; +import type { Session } from "../src/session.ts"; + +// #5848 shipped the inert-frame guard on the chat (OpenAI) lane only. The +// Google lane still drained the tag-echo filter on ANY text-less frame, so the +// same split-tag leak survived there: a render tag split at the frame boundary +// lost its held head and the remainder (` type="text" …>mNNNNN`) no longer +// started with `<`, so it reached the host as prose — the #5192 shape. Google +// frames that carry nothing but empty parts must not drain; a frame with a +// finishReason / functionCall / functionResponse / usageMetadata still must. + +function makeSession(): Session { + return { + id: "testsess", + protocol: "google", + upstreamOrigin: "http://127.0.0.1:9/v1", + label: "test", + createdAt: 0, + lastUsedAt: 0, + requests: 0, + lastInputTokens: 0, + stats: {}, + dirty: false, + } as unknown as Session; +} + +function makeRes(chunks: string[]) { + return { + writes: chunks, + write(b: Buffer | string) { + chunks.push(typeof b === "string" ? b : b.toString("utf8")); + return true; + }, + end(b?: Buffer | string) { + if (b !== undefined) + chunks.push(typeof b === "string" ? b : b.toString("utf8")); + }, + once() {}, + destroyed: false, + writableEnded: false, + } as unknown as import("node:http").ServerResponse; +} + +function streamOf(events: string[]): ReadableStream { + const enc = new TextEncoder(); + let i = 0; + return new ReadableStream({ + pull(controller) { + if (i < events.length) { + controller.enqueue(enc.encode(events[i])); + i += 1; + } else { + controller.close(); + } + }, + }); +} + +const DONE = "data: [DONE]\n\n"; + +function gframe( + parts: Array>, + finishReason?: string, +): string { + const candidate: Record = { + index: 0, + content: { role: "model", parts }, + }; + if (finishReason) candidate["finishReason"] = finishReason; + return `data: ${JSON.stringify({ modelVersion: "gemini-test", candidates: [candidate] })}\n\n`; +} + +/** A progress frame in Gemini's own dialect: no usable text parts at all. */ +function gprogress(): string { + return `data: ${JSON.stringify({ modelVersion: "gemini-test", candidates: [{ index: 0, content: { role: "model", parts: [{ text: "" }] } }] })}\n\n`; +} + +function gTexts(f: Record): string[] { + const out: string[] = []; + const c = f["candidates"]; + if (!Array.isArray(c)) return out; + for (const cand of c) { + if (!cand || typeof cand !== "object") continue; + const content = (cand as Record)["content"]; + if (!content || typeof content !== "object") continue; + const parts = (content as Record)["parts"]; + if (!Array.isArray(parts)) continue; + for (const p of parts) { + if ( + p && + typeof p === "object" && + typeof (p as Record)["text"] === "string" + ) { + out.push((p as Record)["text"] as string); + } + } + } + return out; +} + +/** Concatenate every text part the client would render. */ +function visibleText(text: string): string { + let out = ""; + for (const block of text.split("\n\n")) { + const payload = block + .split("\n") + .filter((l) => l.startsWith("data:")) + .map((l) => l.slice(5).trim()) + .join(""); + if (!payload || payload === "[DONE]") continue; + let obj: Record; + try { + obj = JSON.parse(payload) as Record; + } catch { + continue; + } + for (const t of gTexts(obj)) out += t; + } + return out; +} + +async function visible( + contents: string[], + inertAfter: (i: number) => boolean, +): Promise { + const events: string[] = [gframe([{ text: "" }])]; + contents.forEach((c, i) => { + events.push(gframe([{ text: c }])); + if (inertAfter(i)) events.push(gprogress()); + }); + events.push(gframe([], "STOP"), DONE); + const chunks: string[] = []; + await pipePluginChatWithStrip( + streamOf(events), + makeRes(chunks), + "google", + makeSession(), + ); + return visibleText(chunks.join("")); +} + +const HEAD = 'm00039'; + +test("google: a text-less progress frame inside a paired render tag does not leak the tag's tail", async () => { + const out = await visible( + ["前の文章。", HEAD, REST + "", "後の文章"], + (i) => i === 1, + ); + assert.equal(out, "前の文章。後の文章"); + assert.doesNotMatch(out, /type="text"|m00039/); +}); + +test("google: a text-less progress frame inside an unclosed render tag yields the same text as without it", async () => { + const contents = [HEAD, REST + "\n", "子作成完了"]; + const without = await visible(contents, () => false); + const withProgress = await visible(contents, (i) => i === 0); + assert.equal(withProgress, without); + assert.doesNotMatch(withProgress, /type="text"|m00039/); +}); + +test("google: a text-less progress frame at every chunk boundary leaves the visible text unchanged", async () => { + const contents = [ + ..."前の文章。", + ...`m00039`, + ..."後の文章", + ]; + const without = await visible(contents, () => false); + const withProgress = await visible(contents, () => true); + assert.equal(without, "前の文章。後の文章"); + assert.equal(withProgress, without); +}); + +test("google: a frame carrying usageMetadata still drains the held tail (guard is not a blanket hold)", async () => { + const out: string[] = []; + const usage = `data: ${JSON.stringify({ + modelVersion: "gemini-test", + candidates: [{ index: 0, content: { role: "model", parts: [] } }], + usageMetadata: { + promptTokenCount: 10, + candidatesTokenCount: 3, + totalTokenCount: 13, + }, + })}\n\n`; + await pipePluginChatWithStrip( + streamOf([ + gframe([{ text: "hello world" }], "STOP"), + DONE, + ]), + makeRes(out), + "google", + makeSession(), + ); + const text = visibleText(out.join("")); + // The held ` world" }], "STOP"), + DONE, + ]), + makeRes(control), + "google", + makeSession(), + ); + assert.equal(text, visibleText(control.join(""))); +}); + +test("google: ordinary streams with no held tail are unchanged by the guard", async () => { + const out: string[] = []; + await pipePluginChatWithStrip( + streamOf([ + gframe([{ text: "plain " }]), + gprogress(), + gframe([{ text: "answer" }], "STOP"), + DONE, + ]), + makeRes(out), + "google", + makeSession(), + ); + assert.equal(visibleText(out.join("")), "plain answer"); +});