From b5cc78266f6dfc7e8b9764226a810b74b9f20b9c Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 31 Oct 2026 07:11:51 +0800 Subject: [PATCH] workbuddy 0.2.5: the filter's line is taken out with the newlines around it (#540) itspig (#540, 2026-10-30) ran 0.2.4 against copilot.tencent.com and the refusal still reached the agent. The real chunk's content is "\nSorry, I can't respond to this question.\n" with finish_reason content_filter. filterSaid trimmed only the start, so the trailing "\n" made the text longer than FILTER_SAYS, the prefix test failed, and the chunk went out as an ordinary answer. filterSaid now trims both sides, and text that is only white space so far may still be the line (so a "\n" sent in a chunk of its own is held, not passed on). The reporter's suggested `t.startsWith(s)` is not taken: it would treat the line followed by real words as the filter's and drop those words at content_filter. `without` already removes the whole content field, so the newlines go with it. Run (on mote, mote-run): - HOME=$(mktemp -d) bun test packages/workbuddy: 286 pass, 0 fail. - bun scripts/check.mjs: ok. - filtered.test.mjs has itspig's literal last three SSE events (the " node" reasoning chunk, the refusal chunk, [DONE]), cut every 1, 7 and 1MiB bytes, plus newline-in-its-own-chunk, line-then-more-words and newline-then-answer cases. Broken by hand: - 0.2.4's trimStart filterSaid: the 3 literal-bytes tests and the own-chunk newline test go red (4 of 15). - The reporter's trim() + t.startsWith(s) with t !== "": the own-chunk newline and line-then-more-words tests go red (2 of 15). Not tried: a live WorkBuddy account; the trigger prompt was not run. --- packages/workbuddy/README.md | 3 +++ packages/workbuddy/filtered.test.mjs | 38 ++++++++++++++++++++++++++++ packages/workbuddy/index.mjs | 10 +++++--- packages/workbuddy/package.json | 2 +- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index 0c610c6..f018ef5 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -249,6 +249,9 @@ instead of passing it to the agent as an answer. Only text that is still the filter's line is held, so any other reply's words go as they come; the same words ending `stop`, or the filter cutting off a reply after other words, go through as they came. +copilot.tencent.com sends the line with a newline on each side +(`"\nSorry, I can't respond to this question.\n"`); since 0.2.5 white +space around it no longer lets it through. ## Models diff --git a/packages/workbuddy/filtered.test.mjs b/packages/workbuddy/filtered.test.mjs index 7cbd750..98212d9 100644 --- a/packages/workbuddy/filtered.test.mjs +++ b/packages/workbuddy/filtered.test.mjs @@ -121,3 +121,41 @@ test("an answer's words are not held", async () => { globalThis.fetch = realFetch } }) + +// The last chunks copilot.tencent.com sent itspig, byte for byte (plugins +// #540, 2026-10-30): the filter's line comes with a newline on each side, in +// the chunk that ends content_filter. +const LITERAL = + 'data: {"choices":[{"delta":{"content":"","extra_fields":null,"function_call":null,"reasoning_content":" node","refusal":""},"finish_reason":null,"index":0,"logprobs":null}],"created":1793401068,"id":"c56c77084382622efa17c799396db5cf","model":"deepseek-v4.1-flash","object":"chat.completion.chunk","usage":null}\n\n' + + 'data: {"choices":[{"delta":{"content":"\\nSorry, I can\'t respond to this question.\\n","extra_fields":null,"function_call":null,"refusal":"","role":"assistant"},"finish_reason":"content_filter","index":0,"logprobs":null}],"created":0,"id":"c56c77084382622efa17c799396db5cf","model":"deepseek-v4.1-flash","object":"chat.completion.chunk","usage":null}\n\n' + + "data: [DONE]\n\n" + +for (const cuts of [1, 7, 1 << 20]) { + test(`WorkBuddy's own refusal chunks, newlines and all, say nothing (cut every ${cuts} bytes)`, async () => { + const out = await through(LITERAL, cuts) + expect(out).not.toContain("Sorry") + const cs = contents(out) + expect(cs.map((c) => c.delta.reasoning_content ?? "").join("")).toBe(" node") + expect(cs.some((c) => typeof c.delta.content === "string" && c.delta.content)).toBe(false) + expect(cs.at(-1).finish_reason).toBe("content_filter") + expect(out.endsWith("data: [DONE]\n\n")).toBe(true) + }) +} + +test("the filter's line with its newlines in chunks of their own is taken out", async () => { + const upstream = chunk({ reasoning_content: "hm" }) + chunk({ content: "\n" }) + chunk({ content: said }) + chunk({ content: "\n" }) + chunk({}, "content_filter", zero) + "data: [DONE]\n\n" + const out = await through(upstream) + expect(out).not.toContain("Sorry") + expect(contents(out).some((c) => typeof c.delta.content === "string" && c.delta.content)).toBe(false) + expect(contents(out).at(-1).finish_reason).toBe("content_filter") +}) + +test("the filter's line followed by more words goes through whole", async () => { + const upstream = chunk({ content: `\n${said}\n` }) + chunk({ content: "But here is the answer." }) + chunk({}, "content_filter") + "data: [DONE]\n\n" + expect(await through(upstream)).toBe(upstream) +}) + +test("a reply that opens with a newline and then answers goes through whole", async () => { + const upstream = chunk({ content: "\n" }) + chunk({ content: "Hello" }) + chunk({}, "stop") + "data: [DONE]\n\n" + expect(await through(upstream)).toBe(upstream) +}) diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 65e8564..67a3be5 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -1605,10 +1605,14 @@ function renamed(res, names) { // no output tokens counted (itspig, plugins #540). const FILTER_SAYS = ["Sorry, I can't respond to this question."] -// filterSaid tells whether text said so far may still be FILTER_SAYS. +// filterSaid tells whether text said so far may still be FILTER_SAYS, with +// white space on either side: copilot.tencent.com sends it as +// "\nSorry, I can't respond to this question.\n" (itspig, plugins #540). +// Text that is only white space so far may still be it; text that goes on +// past it is not. const filterSaid = (text) => { - const t = text.replace(/’/g, "'").trimStart() - return t !== "" && FILTER_SAYS.some((s) => s.startsWith(t)) + const t = text.replace(/’/g, "'").trim() + return FILTER_SAYS.some((s) => s.startsWith(t)) } // unsaid is a streamed chat whose reply WorkBuddy's filter refused with its diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index a1424d6..8599bb4 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.2.4", + "version": "0.2.5", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./index.mjs",