From 3968cdd75f121e221338626c2450b2085fec3a1b Mon Sep 17 00:00:00 2001 From: MYCF711 <1451895195@qq.com> Date: Fri, 30 Oct 2026 18:15:12 +0800 Subject: [PATCH] workbuddy 0.2.3: a refused opening is said with a comma first, and remembered (#513) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A chat WorkBuddy refuses as an unapproved channel (11128) for an opening FLAGGED doesn't know is sent once more with one more comma in the opening ("You are an AI agent, powered by X.") before the system prompt is moved into the first user message. The comma is set in system and assistant messages only; user messages and tool results go out as written. A system prompt whose comma passed is remembered (bounded at 256, like refusedSystems) and its next chats go with the comma before the first send, so a tool loop is not refused on every step. A refusal that stands names REFUSED.md (new) before the hint; that address is only in the error returned to the client, nothing sent upstream names magpie. Version 0.2.3. Run at the reviewed head cb8a026 on mote (macOS), temp HOME, outbound network denied by sandbox-exec (localhost allowed): - packages/workbuddy `bun test`: 271 pass, 0 fail. - repo root `bun test`: 3774 pass, 1 fail (layerfs import-limit, fixed on main by 3dd0f44). cb8a026 merged with origin/main 4d8d031: 3777 pass, 0 fail. - `bun scripts/check.mjs`: only cloudflare-workers-ai fails, with the network refused (#519); workbuddy ✓. Broken by hand: no commaSystems.add -> remembered-comma test red; no pre-send commaPausedOn -> same test red; role filter removed -> 5 tests red. Not covered: dropping the roles.has("system") guard leaves every test green (an assistant-only comma would be remembered). Not tried against copilot.tencent.com in this review. Co-authored-by: MYCF711 --- packages/workbuddy/README.md | 42 ++++--- packages/workbuddy/REFUSED.md | 141 +++++++++++++++++++++++ packages/workbuddy/comma.test.mjs | 132 +++++++++++++++++++++ packages/workbuddy/index.mjs | 121 ++++++++++++++++++- packages/workbuddy/package.json | 20 +++- packages/workbuddy/refused.test.mjs | 18 ++- packages/workbuddy/systemasuser.test.mjs | 68 ++++++++++- 7 files changed, 515 insertions(+), 27 deletions(-) create mode 100644 packages/workbuddy/REFUSED.md create mode 100644 packages/workbuddy/comma.test.mjs diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index 56b147e..44d3100 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -174,21 +174,33 @@ default one, as the app does. WorkBuddy refuses some agents' identity lines as an "unapproved channel" (11128), so those lines are reworded before a chat goes out. Since 0.1.27 -(#279) that includes DeepSeek Harness's opening line. A refusal of words -not reworded yet is sent once more, since 0.1.29, with the system prompt -moved into the first user message (inside `` tags) and -WorkBuddy's default system prompt in its place: WorkBuddy checks only the -system and assistant messages, and the line it matched is then in neither. -A system message later in the chat becomes a user message where it stands. -The plugin remembers each refused system prompt while it is loaded, so that -agent's next chats go moved at once. The move can't reach an assistant -message, so words the model quotes back into one keep that chat refused -until they are in the list reworded in every message (since 0.1.30, the "Web GUI whose URL the runtime -context names" line, #291); otherwise only a new chat clears it. A refusal -that stands after the move ends in a hint that names no one client -(since 0.1.28): WorkBuddy refused words in this client's prompt; use it from -Hermes, OpenCode or Pi, or add another provider to the group. Neither -refusal rests the account. +(#279) that includes DeepSeek Harness's opening line. Since 0.2.3 a refusal +of words not reworded yet is answered first by saying the same line with one +more comma — a vendor matches such a line whole and exactly, so the comma +passes it while the line still names the harness (measured 2026-10-30; a word +added after the product name did not pass, the comma did). The comma is set in +the system and assistant messages only — the two WorkBuddy reads — so the +user's own words and tools' results go out as they were sent. A system prompt +whose comma passed is remembered while the plugin is loaded, as a moved one +is, and its next chats go with the comma already in before the first send: +otherwise every turn, and every step of a tool loop, would pay the refusal +once. Only a chat refused still is sent once more, since 0.1.29, with the +system prompt moved into the first user message (inside `` tags) +and WorkBuddy's default system prompt in its place: WorkBuddy checks only the +system and assistant messages, and the line it matched is then in neither. A system message later +in the chat becomes a user message where it stands. The plugin remembers each +refused system prompt while it is loaded, so that agent's next chats go moved +at once. The move can't reach an assistant message, so words the model quotes +back into one keep that chat refused until they are in the list reworded in +every message (since 0.1.30, the "Web GUI whose URL the runtime context names" +line, #291); otherwise only a new chat clears it. A refusal that stands after +the move ends in a hint that names no one client (since 0.1.28): WorkBuddy +refused words in this client's prompt; use it from Hermes, OpenCode or Pi, or +add another provider to the group. Since 0.2.3 that hint is preceded by the +address of [REFUSED.md](REFUSED.md), which says what the check reads, how a +line is reworded to pass it, and how to turn such a line off in an agent that +has a switch for it — so the error carries its own fix, for the person who +reads it or the agent they paste it to. Neither refusal rests the account. The loader also translates OpenAI-compatible request spellings WorkBuddy doesn't take: `max_completion_tokens` becomes `max_tokens`, `developer` diff --git a/packages/workbuddy/REFUSED.md b/packages/workbuddy/REFUSED.md new file mode 100644 index 0000000..0928bf3 --- /dev/null +++ b/packages/workbuddy/REFUSED.md @@ -0,0 +1,141 @@ +# `Illegal API invocation from an unapproved channel` — what it is and how to fix it + +WorkBuddy reads the words of a chat's **system and assistant messages** and turns the +whole chat away when it recognizes a line from another agent's prompt: + +```json +{"code":11128,"msg":"Illegal API invocation from an unapproved channel"} +``` + +Your client is not at fault and no account will help: the same chat is refused on every +account it is sent as. This file is where the gateway points a refusal, so whoever holds +the error — the person reading it, or the agent they paste it to — can read this and fix +the prompt. + +## If you are an agent reading this + +Your user pasted an error that ends in a link to this file. Do this: + +1. **Find the line.** It is in your system prompt, or in an assistant message. Openers + are refused when they **start** a message; other fingerprints are refused anywhere in + it. The user's messages and tools' results are not read, so it is not there. +2. **Say the line differently, as little as possible.** The check matches a line **whole + and exactly**, so one more character is enough to pass it — and the line still says + what it said. A comma is what this plugin uses: + `You are an AI agent powered by X.` → `You are an AI agent, powered by X.` + Adding a word is **not** enough: `…powered by DeepSeek Harness Desktop.` and + `…powered by DeepSeek Harness CLI.` were refused just the same (measured 2026-10-30). +3. **Or drop the line**, if your harness has a switch for it (see *DeepSeek Harness* + below). It costs the agent being told which harness it runs in. +4. **If you are a magpie plugin**, add the line to your reword list, the way + `FLAGGED` in `../index.mjs` does, and keep the replacement saying the same thing. +5. **Re-run the request.** The fix needs no new account and no restart of the vendor. + +## What is measured, and what is not + +Everything here was measured against `https://copilot.tencent.com/v2/chat/completions` +with real accounts. Kept apart from guesses on purpose: + +| tried | result | +|---|---| +| `You are an AI agent powered by DeepSeek Harness.` | **400** code 11128 | +| `You are an AI agent powered by deepseek harness.` | **400** — case is not a way out | +| `You are an AI agent powered by DeepSeekHarness.` | **400** — the space is not either | +| `You are an AI agent, powered by DeepSeek Harness.` | **200** | +| `You are an AI agent powered by DeepSeek Harness Desktop.` | **400** | +| `You are an AI agent powered by DeepSeek Harness CLI.` | **400** | +| `You are an AI agent powered by Claude Code.` | **200** | +| `You are an AI agent powered by FooBar.` | **200** | +| the line anywhere but the start of the message | **200** | + +So the trigger is **a shape plus a name**: the shape `You are an AI agent powered by …` +together with the product name it knows. Break either one and the chat passes — +`You are a DeepSeek Harness agent …` passes, and a comma in the shape passes. +**A different product in the same shape passes**, which is what tells the two apart. + +## Two things that look like this error and are not + +- **`11101 Non-stream chat request is currently not supported`** — WorkBuddy serves + chat only streamed. Send `"stream": true`, or a probe reads 11101 and concludes the + wrong thing. +- **`11128` with `first message is not system prompt`** — the *shape* of the chat, not + its words. A chat must open with a system message. That is why this plugin puts + `You are a helpful assistant.` in the system slot when it has to move a system prompt + out of it. + +## DeepSeek Harness (DSH) + +DSH opens its system prompt with: + +``` +You are an AI agent powered by DeepSeek Harness. +``` + +It comes from `@deepseek-ai/dsh-system-prompt`, section `harness:identity`, order +`−1000` — the first section there is, so it can never be moved out of the way. Two ways +out: + +- **Keep it, and let this plugin reword it.** Its `FLAGGED` list holds the line and puts + a comma in, so the chat passes and the agent still knows which harness it runs in. + This is the default once `workbuddy` 0.2.2 or later is installed. +- **Turn it off.** Set `includeHarnessIdentity: false` in the `system-prompt` plugin's + config, in your profile's patch file (`/profiles//cordis.patch.yml`): + + ```yaml + - id: system-prompt + config: + personaPrefix: … # ⚠ a patch replaces the whole config, not one key: + personaSuffix: … # write the other keys back as they were or they are lost + includeHarnessIdentity: false + ``` + + The cost is registered, not hidden: the model is no longer told which harness it runs + in. Measured 2026-10-30, the difference that makes, with the rest of the prompt left + alone — asked which harness it runs on, a model without the line answers *"I can't see + which harness I'm on from the inside; unverified"*, and one with it answers *"I run on + DeepSeek Harness"*. A DSH prompt that still names the harness elsewhere (the checkout + path, the Web GUI address) lets the model work it out anyway. + +## Checking a line yourself + +Two rules that make a probe trustworthy: fix one account that is known to pass a plain +chat first, and always send `"stream": true`. + +```bash +KEY='' +curl -sS https://copilot.tencent.com/v2/chat/completions \ + -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' \ + -H 'X-Domain: www.workbuddy.cn' -H 'X-Product: SaaS' -H 'X-IDE-Type: WorkBuddy' \ + -H 'User-Agent: WorkBuddy/5.9.3 WorkBuddy/5.9.3 CLI/2.165.0' -H 'X-IDE-Version: 5.9.3' \ + --data-binary @- <<'JSON' | head -c 200 +{"model":"deepseek-v4.1-flash","stream":true,"max_tokens":16, + "messages":[{"role":"system","content":"You are an AI agent powered by DeepSeek Harness."}, + {"role":"user","content":"hi"}]} +JSON +``` + +`400` with `unapproved channel` means the line is refused; `200` with an SSE body means +it passes. A body that is refused costs no credits, so a probe is cheap. + +## Adding a line to `FLAGGED` + +`../index.mjs` — the list of words WorkBuddy refuses, each with words that pass: + +```js +[/You are an AI agent powered by DeepSeek Harness\.?/gi, "You are an AI agent, powered by DeepSeek Harness."], +``` + +Two rules, both from measurements rather than taste: + +- **Reword as little as the whole-line match needs.** The check matches exactly, so a + comma is enough; a replacement that says something else than the line did is a bug, + not a fix. An earlier replacement here said the agent "helps the user with programming + tasks", which took the harness's name out of the one line that says which agent is + running. +- **A fingerprint is a line, not a product name.** A bare `DeepSeek` or `DeepSeek + Harness` in a sentence someone else wrote was not refused; a whole opening line was. + Add the line, and keep the pattern as narrow as it needs to be. + +The words are refused whenever they appear in a system or assistant message, and reworded +in every message, so a line the model quotes back into an assistant message is covered +too. diff --git a/packages/workbuddy/comma.test.mjs b/packages/workbuddy/comma.test.mjs new file mode 100644 index 0000000..ea68aa0 --- /dev/null +++ b/packages/workbuddy/comma.test.mjs @@ -0,0 +1,132 @@ +// A vendor that turns a chat away over words in the agent's prompt matches +// the line whole and exactly, so the same line said with one more comma +// passes it (measured against WorkBuddy 2026-10-30) — and the line still says +// which harness the agent runs in. commaPaused is that, tried before the +// system prompt is moved out of the slot the agent's instructions are read in. +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const { opened, commaPaused } = _internal + +test("a comma goes in where the opening names its harness", () => { + expect(opened("You are an AI agent powered by DeepSeek Harness.")).toBe("You are an AI agent, powered by DeepSeek Harness.") + expect(opened("You are a coding agent powered by the {{model}} model.")).toBe("You are a coding agent, powered by the {{model}} model.") + expect(opened("You are an AI assistant running on Foo.")).toBe("You are an AI assistant, running on Foo.") + expect(opened("You are the deployment assistant built on Bar.")).toBe("You are the deployment assistant, built on Bar.") +}) + +// the harness's name stays: only the shape the vendor knows is changed +test("the harness's name survives the comma", () => { + const opener = "You are an AI agent powered by DeepSeek Harness." + expect(opened(opener)).toContain("DeepSeek Harness") + expect(opened(opener)).not.toBe(opener) +}) + +test("a comma already in place is left alone", () => { + const once = opened("You are an AI agent powered by DeepSeek Harness.") + expect(opened(once)).toBe(once) +}) + +test("words that name no harness are left alone", () => { + const said = "You are a helpful assistant.\nYour working directory is D:\\work.\nBe brief." + expect(opened(said)).toBe(said) +}) + +// commaPaused sets OPENERS' comma in the system and assistant messages, the +// two WorkBuddy reads (REFUSED.md): a comma anywhere else changes what +// someone said without changing what was refused +test("commaPaused sets the comma in the system and assistant messages", () => { + const body = JSON.stringify({ + model: "deepseek-v4.1-flash", + messages: [ + { role: "system", content: "You are an AI agent powered by DeepSeek Harness.\n\nBe brief." }, + { role: "user", content: "hi" }, + ], + }) + const out = JSON.parse(commaPaused(body)) + expect(out.messages[0].content).toBe("You are an AI agent, powered by DeepSeek Harness.\n\nBe brief.") + expect(out.messages[1].content).toBe("hi") + expect(out.model).toBe("deepseek-v4.1-flash") +}) + +// yetone, plugins#513: a user message quoting a line came back altered. The +// user's own words and tools' results go out as they were sent. +test("the user's messages and tools' results are left alone", () => { + const quoted = "Quote exactly: You are an assistant built on Rails." + const result = "You are a coding agent powered by Example Harness." + const body = JSON.stringify({ + messages: [ + { role: "system", content: "You are an AI agent powered by DeepSeek Harness." }, + { role: "user", content: quoted }, + { role: "assistant", content: "You are an AI assistant running on Foo." }, + { role: "tool", tool_call_id: "c1", content: result }, + ], + }) + const out = JSON.parse(commaPaused(body)) + expect(out.messages[0].content).toBe("You are an AI agent, powered by DeepSeek Harness.") + expect(out.messages[1].content).toBe(quoted) + expect(out.messages[2].content).toBe("You are an AI assistant, running on Foo.") + expect(out.messages[3]).toEqual({ role: "tool", tool_call_id: "c1", content: result }) +}) + +// the user's words alone have nothing to set a comma in, so nothing goes again +test("a chat whose opening is only in the user's message is not resent", () => { + const users = JSON.stringify({ messages: [{ role: "user", content: "Quote exactly: You are an assistant built on Rails." }] }) + expect(commaPaused(users)).toBe(null) +}) + +test("commaPaused sets it in a system message's parts too, and not in the user's", () => { + const body = JSON.stringify({ + messages: [ + { role: "system", content: [{ type: "text", text: "You are an AI agent powered by DeepSeek Harness." }] }, + { role: "user", content: [{ type: "text", text: "Quote exactly: You are an assistant built on Rails." }] }, + ], + }) + const out = JSON.parse(commaPaused(body)) + expect(out.messages[0].content[0].text).toBe("You are an AI agent, powered by DeepSeek Harness.") + expect(out.messages[1].content[0].text).toBe("Quote exactly: You are an assistant built on Rails.") +}) + +test("a chat with nothing of the kind is not resent", () => { + expect(commaPaused(JSON.stringify({ messages: [{ role: "system", content: "be brief" }, { role: "user", content: "hi" }] }))).toBe(null) + expect(commaPaused(JSON.stringify({ messages: [] }))).toBe(null) + expect(commaPaused("not json")).toBe(null) + expect(commaPaused(JSON.stringify({ messages: "not a list" }))).toBe(null) +}) + +// the comma a system prompt's next chats go with is the one set in that system +// prompt; one that only set an assistant message is not the system prompt's +// (commaSystems), so the roles are read per message, and a message whose parts +// held no opening of their own is not reported +test("the roles the comma went into are the messages it changed", () => { + const brief = [{ type: "text", text: "Be brief." }] + const body = JSON.stringify({ + messages: [ + { role: "system", content: brief }, + { role: "assistant", content: "You are an AI agent powered by Foo." }, + { role: "user", content: "Quote exactly: You are an assistant built on Rails." }, + ], + }) + const roles = new Set() + const out = JSON.parse(commaPaused(body, roles)) + expect([...roles]).toEqual(["assistant"]) + expect(out.messages[0].content).toEqual(brief) + + const system = JSON.stringify({ + messages: [ + { role: "system", content: "You are an AI agent powered by Foo." }, + { role: "user", content: "hi" }, + ], + }) + const only = new Set() + expect(JSON.parse(commaPaused(system, only)).messages[0].content).toBe("You are an AI agent, powered by Foo.") + expect([...only]).toEqual(["system"]) +}) + +// the shape WorkBuddy knows is the line, not the product alone: a suffix in +// its place (…Harness Desktop., …Harness CLI.) was refused just the same, and +// the comma was what passed, so this must change the shape and nothing else +test("adding a word to the opening is not what passes; the comma is", () => { + const suffixed = "You are an AI agent powered by DeepSeek Harness Desktop." + expect(opened(suffixed)).toBe("You are an AI agent, powered by DeepSeek Harness Desktop.") +}) \ No newline at end of file diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 344333b..b8b1664 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -1008,6 +1008,79 @@ function systemAsUserOn(b) { return true } +// OPENERS are the shapes in which an agent's opening says which harness it +// runs in, as a vendor may have fingerprinted one. A vendor matches a refused +// line whole and exactly — one extra space passes it, as FLAGGED's note above +// has it — so the same line with one more comma passes while still saying +// what it said. Measured 2026-10-30 against copilot.tencent.com, one account, +// streamed, deepseek-v4.1-flash at max: DeepSeek Harness's opener answered +// 400 code 11128 as written, and 200 with the comma, the harness's name still +// in it; a suffix in its place (…Harness Desktop., …Harness CLI.) changed +// nothing, so it is the line's shape the vendor knows, not the product alone. +const OPENERS = [ + [/(\bYou are (?:an?|the)\s[^\n.,]{0,60}?\b(?:agent|assistant))\s+(powered by|running on|running in|built on|based on)\b/gi, "$1, $2"], +] + +function opened(text) { + for (const [re, to] of OPENERS) text = text.replace(re, to) + return text +} + +// commaPaused is body with OPENERS' comma set in its system and assistant +// messages — the two WorkBuddy reads (REFUSED.md). The user's messages and +// tools' results are not among them: a comma in a message the vendor never +// matches changes what someone said without changing what was refused, and +// the user's own words come back altered. It is tried before systemAsUser, +// which takes the system prompt out of the slot an agent's instructions are +// read in. null when there was nothing to set a comma in. +// +// The two roles are read off the message's own role: a message with no role, +// or one of another kind, is left as it is. roles, when given, collects the +// roles the comma went into — the caller uses it to tell a comma that made a +// chat pass in its system prompt from one that only set an assistant +// message's (commaSystems). +const PAUSED_ROLES = new Set(["system", "assistant"]) + +function commaPaused(body, roles) { + const b = chatOf(body) + return commaPausedOn(b, roles) ? JSON.stringify(b) : null +} + +// commaPausedOn makes commaPaused's change on a chat's object, in place; false +// when there was nothing to set a comma in. commaPaused is this on a body, and +// the first send of a system prompt whose comma passed is this too +// (commaSystems), so the two can never drift apart. +function commaPausedOn(b, roles) { + const msgs = b?.messages + if (!Array.isArray(msgs)) return false + let changed = false + for (const m of msgs) { + if (!PAUSED_ROLES.has(m?.role)) continue + let mine = false + if (typeof m.content === "string") { + const t = opened(m.content) + if (t !== m.content) { + m.content = t + mine = true + } + } else if (Array.isArray(m.content)) { + for (const p of m.content) { + if (typeof p?.text !== "string") continue + const t = opened(p.text) + if (t === p.text) continue + p.text = t + mine = true + } + } + // a message whose parts held no opening of their own is not changed, and + // is not reported as one that was + if (!mine) continue + changed = true + roles?.add(m.role) + } + return changed +} + // normalizeToolChoice converts the object form emitted by OpenAI clients to // WorkBuddy's string form. It runs after fitted(), so a named choice follows a // tool name that was made safe for WorkBuddy. @@ -3040,6 +3113,13 @@ function makePlugin(site) { // the system prompts this run WorkBuddy refused as an unapproved channel: // their next chats go with systemAsUser at once, not refused first const refusedSystems = new Set() + // the system prompts this run WorkBuddy refused as written and took with + // OPENERS' comma: their next chats go with the comma set before the first + // send, as a moved one goes moved. Without it every turn of that agent — + // and every step of its tool loop — pays a refused round trip first, which + // is latency on each request and a pattern of channel refusals on the + // account. Bounded as refusedSystems is. + const commaSystems = new Set() return async ({ client, magpie }) => { // each account's list as last read, for FREE_ONLY: {at, list} const lists = new Map() @@ -3290,6 +3370,10 @@ function makePlugin(site) { // words at once (see systemAsUser) const sysKey = native ? "" : systemKeyOf(chat) if (sysKey && refusedSystems.has(sysKey) && systemAsUserOn(chat)) changed = true + // a system prompt whose comma passed once goes with the comma + // set before the first send (see commaSystems), the way one + // refused before goes moved above + if (sysKey && commaSystems.has(sysKey) && commaPausedOn(chat)) changed = true // shaped is a body as such a model takes it. The first try's // is made on the chat already parsed, after the move above, // as shaped would make it of the body written; a resend with @@ -3317,7 +3401,27 @@ function makePlugin(site) { else res = new Response(text, { status: res.status, statusText: res.statusText, headers: res.headers }) } if (res.status === 400 && !native) { let text = await res.text() - const moved = REFUSED.test(text) && sysKey && !refusedSystems.has(sysKey) ? systemAsUser(body) : null + // the vendor may know a shape of the agent's opening that FLAGGED + // doesn't: the same words said with one more comma pass it, and + // that is tried before the system prompt is moved out of its slot. + // The roles the comma went into are kept: one set in the system + // prompt is what its next chats go with, one set in an assistant + // message alone says nothing about the next chat's. + const roles = new Set() + const paused = REFUSED.test(text) ? commaPaused(body, roles) : null + if (paused) { + body = paused + res = await send(shaped(body)) + if (res.status === 400) text = await res.text() + // not refused this time: the comma is what passed the + // vendor's check, so this system prompt's next chats go + // with it set before the first send + else if (sysKey && roles.has("system")) { + commaSystems.add(sysKey) + if (commaSystems.size > 256) commaSystems.delete(commaSystems.values().next().value) + } + } + const moved = res.status === 400 && REFUSED.test(text) && sysKey && !refusedSystems.has(sysKey) ? systemAsUser(body) : null if (moved) { refusedSystems.add(sysKey) if (refusedSystems.size > 256) refusedSystems.delete(refusedSystems.values().next().value) @@ -3446,6 +3550,13 @@ function makePlugin(site) { // apart from WorkBuddy's own words. const REFUSED_HINT = "WorkBuddy refused words in this client's prompt (most often its system prompt); use it from Hermes, OpenCode or Pi, or add another provider to this group" + +// REFUSED_DOC is where a refusal's fix is written down, for whoever holds the +// error next: the person reading it, or the agent they paste it to. It goes +// before the hint, never after, because magpie's Routing page tells the hint +// from WorkBuddy's own words by the error ending in it. +const REFUSED_DOC = "https://github.com/magpie-community/plugins/blob/main/packages/workbuddy/REFUSED.md" + const REFUSED = /unapproved channel|illegal api invocation/i // BAD_SIGNATURE is WorkBuddy's refusal of a thinking block whose signature it @@ -3524,7 +3635,11 @@ async function explained(res) { const hint = isRefused ? REFUSED_HINT : isRestricted ? RESTRICTED_HINT : EXHAUSTED_HINT const type = isRefused || isRestricted ? "permission_error" : "insufficient_quota" const errCode = isRefused ? null : isRestricted ? RESTRICTED_CODE : 14018 - msg = `${msg} — ${hint}` + // a refusal says where its fix is written down, so whoever holds the error + // — the person, or the agent they paste it to — can go and read it. It goes + // before the hint, never after: the hint is the message's end. + const where = isRefused ? `${REFUSED_DOC} — ` : "" + msg = `${msg} — ${where}${hint}` const headers = new Headers(res.headers) headers.set("content-type", "application/json") headers.delete("content-length") @@ -3537,4 +3652,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy) export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"]) // for tests -export const _internal = { idFor, replyAfterToolsOn, withDefaultEffort, defaultEffortOf, gzipped, FREE_ONLY, PRICE_TTL, keyAccount, remoteConfig, cycleOf, typeUnions, systemAsUser, systemKey, DEFAULT_SYSTEM, EDGE_READS, fitShape, replyAfterTools, AFTER_TOOLS_REFUSED, enterpriseUsageOf, accountName, notDesktop, textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, asReasoning, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, priceOf, fresh, SITES, renewing, renewed, SESSION, attend, scopeOf, turnKey, conversationKey, signatureOf, isMessages, withoutUnsigned, withoutSignedThinking, prepared, answeredBefore, nativeOf, modelOf, configModels, freeWindowOf, tokensShort, freeWindowRows, freeWindowLedger, readLedgerTail, pinned, rowTime, rowAccount, ofAccount, windowOffset, windowReset, FREE_WINDOW_MODELS, FREE_WINDOW_SPAN, FREE_WINDOW_GAP, FREE_WINDOW_LEAD, checkinOf, API_MESSAGES: NPM_MESSAGES, API_CHAT: NPM } +export const _internal = { idFor, replyAfterToolsOn, withDefaultEffort, defaultEffortOf, gzipped, FREE_ONLY, PRICE_TTL, keyAccount, remoteConfig, cycleOf, typeUnions, systemAsUser, systemKey, DEFAULT_SYSTEM, EDGE_READS, fitShape, replyAfterTools, AFTER_TOOLS_REFUSED, enterpriseUsageOf, accountName, notDesktop, textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, asReasoning, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, REFUSED_DOC, commaPaused, commaPausedOn, opened, freeCredits, creditsOf, priceOf, fresh, SITES, renewing, renewed, SESSION, attend, scopeOf, turnKey, conversationKey, signatureOf, isMessages, withoutUnsigned, withoutSignedThinking, prepared, answeredBefore, nativeOf, modelOf, configModels, freeWindowOf, tokensShort, freeWindowRows, freeWindowLedger, readLedgerTail, pinned, rowTime, rowAccount, ofAccount, windowOffset, windowReset, FREE_WINDOW_MODELS, FREE_WINDOW_SPAN, FREE_WINDOW_GAP, FREE_WINDOW_LEAD, checkinOf, API_MESSAGES: NPM_MESSAGES, API_CHAT: NPM } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index c137e5f..87e3793 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,12 +1,24 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.2.2", + "version": "0.2.3", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./index.mjs", "exports": "./index.mjs", - "files": ["index.mjs", "README.md"], - "keywords": ["opencode", "opencode-plugin", "workbuddy", "codebuddy"], + "files": [ + "index.mjs", + "README.md" + ], + "keywords": [ + "opencode", + "opencode-plugin", + "workbuddy", + "codebuddy" + ], "license": "MIT", - "repository": { "type": "git", "url": "git+https://github.com/magpie-community/plugins.git", "directory": "packages/workbuddy" } + "repository": { + "type": "git", + "url": "git+https://github.com/magpie-community/plugins.git", + "directory": "packages/workbuddy" + } } diff --git a/packages/workbuddy/refused.test.mjs b/packages/workbuddy/refused.test.mjs index 4045f17..a895b5e 100644 --- a/packages/workbuddy/refused.test.mjs +++ b/packages/workbuddy/refused.test.mjs @@ -4,12 +4,24 @@ import { expect, test } from "bun:test" import { _internal } from "./index.mjs" -const { explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT } = _internal +const { explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, REFUSED_DOC } = _internal test("the refusal ends in the hint", async () => { const res = await explained(new Response(JSON.stringify({ error: { message: "Illegal API invocation from an unapproved channel", code: 11004 } }), { status: 403 })) expect(res.status).toBe(403) - expect((await res.json()).error.message).toBe("Illegal API invocation from an unapproved channel — " + REFUSED_HINT) + expect((await res.json()).error.message).toBe("Illegal API invocation from an unapproved channel — " + REFUSED_DOC + " — " + REFUSED_HINT) +}) + +// The hint stays the message's end, where magpie's Routing page matches it +// (Routing client.js: HINTS.find((h) => error.endsWith(" — " + h))), so a +// refusal points at where its fix is written down and still ends as it did. +test("the refusal names where its fix is written, and still ends in the hint", async () => { + const res = await explained(new Response(JSON.stringify({ error: { message: "Illegal API invocation from an unapproved channel" } }), { status: 400 })) + const message = (await res.json()).error.message + expect(message.endsWith(" — " + REFUSED_HINT)).toBe(true) + expect(message).toContain(REFUSED_DOC) + expect(message.indexOf(REFUSED_DOC)).toBeLessThan(message.indexOf(REFUSED_HINT)) + expect(REFUSED_DOC).toMatch(/^https:\/\/github\.com\/magpie-community\/plugins\/blob\/main\/packages\/workbuddy\/REFUSED\.md$/) }) test("the hint is magpie's word for word, and blames no one client", () => { @@ -104,7 +116,7 @@ test("a model of no credits is free, as magpie's built-in read them", () => { test("a refusal as {code, msg} says its msg, then the hint", async () => { const res = await explained(new Response(JSON.stringify({ code: 11004, msg: "Illegal API invocation from an unapproved channel" }), { status: 403 })) expect(res.status).toBe(403) - expect((await res.json()).error.message).toBe("Illegal API invocation from an unapproved channel — " + REFUSED_HINT) + expect((await res.json()).error.message).toBe("Illegal API invocation from an unapproved channel — " + REFUSED_DOC + " — " + REFUSED_HINT) }) // a body handed over as bytes still gets WorkBuddy's system message diff --git a/packages/workbuddy/systemasuser.test.mjs b/packages/workbuddy/systemasuser.test.mjs index e428f70..2c2a84a 100644 --- a/packages/workbuddy/systemasuser.test.mjs +++ b/packages/workbuddy/systemasuser.test.mjs @@ -9,7 +9,7 @@ import { expect, test } from "bun:test" import { WorkBuddyAuthPlugin, WorkBuddyAIAuthPlugin, _internal } from "./index.mjs" import { unz } from "./testdata/unz.mjs" -const { systemAsUser, REFUSED_HINT, DEFAULT_SYSTEM } = _internal +const { systemAsUser, REFUSED_HINT, REFUSED_DOC, DEFAULT_SYSTEM } = _internal const REFUSAL = JSON.stringify({ code: 11128, @@ -106,6 +106,70 @@ test("a line refused anywhere in the system prompt is moved too", async () => { }) }) +// An opening the plugin doesn't know is said with a comma before the system +// prompt is moved: the move costs the agent the slot its instructions are +// read in, and the comma keeps both the place and the harness's name. +test("a refused opening FLAGGED doesn't know is said with a comma, not moved", async () => { + const opener = "You are an AI agent powered by Example Harness." + const uncommaed = (b) => + b.messages.some((m) => (m.role === "system" || m.role === "assistant") && text(m.content).includes("You are an AI agent powered by Example Harness")) + await withService( + WorkBuddyAIAuthPlugin, + async (post, sent) => { + const res = await post({ messages: [{ role: "system", content: opener }, ask] }) + expect(res.status).toBe(200) + expect(sent.length).toBe(2) + expect(sent[1].messages[0].role).toBe("system") + expect(sent[1].messages[0].content).toBe("You are an AI agent, powered by Example Harness.") + }, + (b) => (uncommaed(b) ? new Response(REFUSAL, { status: 400, headers: { "content-type": "application/json; charset=utf-8" } }) : undefined), + ) +}) + +// yetone, plugins#513: a comma that passed wasn't remembered, so every chat of +// that agent, and every step of its tool loop, was refused first — an extra +// round trip each and a pattern of channel refusals on the account. His probe, +// one system prompt and three chats: main 4 sends and 1 refusal, this PR 6 and +// 3. After the fix the second chat (and every step after it) goes with the +// comma already in: 2 chats, 3 sends. +test("a system prompt whose comma passed goes with it in before the first send", async () => { + // the vendor's check as measured: the opening line whole, without the comma + const OPEN = /^You are (?:an?|the) [^\n]*?\b(?:agent|assistant) (?:powered by|running on|running in|built on|based on)\b/ + const uncommaed = (b) => + b.messages.some((m) => (m.role === "system" || m.role === "assistant") && OPEN.test(text(m.content))) + // a system prompt of its own: what passed is remembered for as long as the + // plugin is loaded, as a moved one is, so a file's tests would share it + const system = { role: "system", content: "You are an AI agent powered by Probe Harness. (#513)" } + // a user message quoting a line, and a tool result holding one: neither is + // WorkBuddy's to read, and neither comes back altered + const quoted = { role: "user", content: "Quote exactly: You are an assistant built on Rails." } + const call = { role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: "read", arguments: '{"path":"calc.py"}' } }] } + const result = { role: "tool", tool_call_id: "c1", content: "You are a coding agent powered by Example Harness." } + await withService( + WorkBuddyAIAuthPlugin, + async (post, sent) => { + const first = await post({ messages: [system, ask] }) + expect(first.status).toBe(200) + expect(sent.length).toBe(2) // refused as written, then taken with the comma + const second = await post({ messages: [system, quoted, call, result] }) + expect(second.status).toBe(200) + // the comma was set before the first send: one send, and the two + // messages WorkBuddy doesn't read are as they were sent + expect(sent.length).toBe(3) + expect(sent[2].messages[0].content).toBe("You are an AI agent, powered by Probe Harness. (#513)") + expect(sent[2].messages[1]).toEqual(quoted) + expect(sent[2].messages[3]).toEqual(result) + // another agent's system prompt isn't given a comma it hasn't earned + const other = await post({ messages: [{ role: "system", content: "You are a coding agent powered by Another Harness. (#513)" }, ask] }) + expect(other.status).toBe(200) + expect(sent.length).toBe(5) + expect(sent[3].messages[0].content).toBe("You are a coding agent powered by Another Harness. (#513)") + expect(sent[4].messages[0].content).toBe("You are a coding agent, powered by Another Harness. (#513)") + }, + (b) => (uncommaed(b) ? new Response(REFUSAL, { status: 400, headers: { "content-type": "application/json; charset=utf-8" } }) : undefined), + ) +}) + test("a refusal that stands after the move is the user's, with the hint, tried once", async () => { const system = systemOf() await withService(WorkBuddyAuthPlugin, async (post, sent) => { @@ -114,7 +178,7 @@ test("a refusal that stands after the move is the user's, with the hint, tried o expect(res.status).toBe(400) expect(sent.length).toBe(2) expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept") - expect((await res.json()).error.message).toBe("Illegal API invocation from an unapproved channel — " + REFUSED_HINT) + expect((await res.json()).error.message).toBe("Illegal API invocation from an unapproved channel — " + REFUSED_DOC + " — " + REFUSED_HINT) }) })