From 48076d28bdb3faa9c4ee9159f328a2c83de11e04 Mon Sep 17 00:00:00 2001 From: tax-mux <156999048+tax-mux@users.noreply.github.com> Date: Sat, 7 Nov 2026 11:55:33 +0900 Subject: [PATCH] fix(plugin): don't drain held tags on inert mid-stream frames (#5848) MTPLX interleaves a text-less progress frame ({"choices":[{"delta":{},"finish_reason":null}],"mtplx_progress":{...}}) roughly every ~15 decoded tokens. processOpenai's no-text path drained the tag-echo filter on it, so a render tag split across the frame lost its held head (`m00039`) reached the host as prose -- the opener-less residue reported in #5192. Only drain on a text-less frame when it carries something a held tail must precede (finish_reason, tool_calls or another delta key); terminal and [DONE] drains (#1546) are unchanged. Co-authored-by: Cursor --- src/plugin.ts | 6 +- .../plugin-passthrough-progress-frame.test.ts | 107 ++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 tests/plugin-passthrough-progress-frame.test.ts diff --git a/src/plugin.ts b/src/plugin.ts index 3e2bd8b667..3c412ad353 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3250,7 +3250,11 @@ export async function pipePluginChatWithStrip( // managed fields — audit them (argument frames excluded: #1039). if (!openaiFrameHasToolCalls(choices)) auditRawForward(rawEvent); if (drain.length > 0) return drain + rawEvent + "\n\n"; - if (!hadText && anyPending()) return flushTails() + rawEvent + "\n\n"; + // A non-terminal frame with no text, no tool call and no other delta key + // (MTPLX interleaves `delta:{}` progress frames mid-stream) carries nothing + // a held tail must precede. Draining on it split a render tag at the frame + // and leaked the tail as prose (#5192 shape) — same disease as #2405. + if (!hadText && anyPending() && hadTextOtherThanTextFields(choices)) return flushTails() + rawEvent + "\n\n"; return rawEvent + "\n\n"; }; const processAnthropic = (ev: Record, rawEvent: string): string => { diff --git a/tests/plugin-passthrough-progress-frame.test.ts b/tests/plugin-passthrough-progress-frame.test.ts new file mode 100644 index 0000000000..24baafa89a --- /dev/null +++ b/tests/plugin-passthrough-progress-frame.test.ts @@ -0,0 +1,107 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { pipePluginChatWithStrip } from "../src/plugin.ts"; + +// MTPLX (local MLX server) interleaves a text-less progress frame into the +// stream roughly every ~15 decoded tokens: +// {"choices":[{"index":0,"delta":{},"finish_reason":null}],"mtplx_progress":{…}} +// The frame is not terminal and carries nothing for the client, yet the no-text +// flush path drained the tag-echo filter on it. A render tag split across the +// frame lost its held head (`m00039`) no longer started with `<`, so it reached the +// host as prose (the #5192 shape). Inert frames must not change what the +// client sees. + +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 chunk(delta: Record, finishReason: string | null = null): string { + return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta, finish_reason: finishReason }] })}\n\n`; +} + +const PROGRESS = `data: ${JSON.stringify({ + id: "chatcmpl-1", + object: "chat.completion.chunk", + created: 1, + model: "qwen", + choices: [{ index: 0, delta: {}, finish_reason: null }], + mtplx_progress: { completion_tokens: 14, decode_phase: "content", decode_tok_s: 60.9 }, +})}\n\n`; + +function contentOf(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; + const obj = JSON.parse(payload) as { choices?: Array<{ delta?: { content?: unknown } }> }; + const v = obj.choices?.[0]?.delta?.content; + if (typeof v === "string") out += v; + } + return out; +} + +async function visible(contents: string[], progressAfter: (i: number) => boolean): Promise { + const events = [chunk({ role: "assistant" })]; + contents.forEach((c, i) => { + events.push(chunk({ content: c })); + if (progressAfter(i)) events.push(PROGRESS); + }); + events.push(chunk({}, "stop"), DONE); + const chunks: string[] = []; + await pipePluginChatWithStrip(streamOf(events), makeRes(chunks), "openai"); + return contentOf(chunks.join("")); +} + +const HEAD = 'm00039'; + +test("a 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, "前の文章。後の文章"); +}); + +test("a 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("progress frames at every chunk boundary leave 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); +});