zen-free 0.1.22: tool names Zen refuses go under other names (#624)

alonewolfsy-web bisected a real Claude Code desktop request that Zen's
free tier answered 429 FreeUsageLimitError while 0.1.21 had added the
environment block and core tools: a tool name with "terminal" in it (an
MCP server's terminal tools), and ReportFindings offered beside
ScheduleWakeup, as Claude Code desktop does, each got 429 at once; the
same request with those tools renamed was served (their patch, 40+ real
requests). Either of the pair alone was served.

Such tools now go to Zen under other names of the same words
(mcp__ide__terminal_run as mcp__ide__console_run, RunTerminal as
RunConsole, ReportFindings and ScheduleWakeup as findings_report and
wakeup_schedule, with _2 when the agent has that name), in the agent's
past calls and tool_choice too, on Chat, Responses and Messages. A call
of one comes back under the agent's own name (toolPolicy.renamed, read
first by nameGuard). A request without them is sent as before, and one
of the pair alone keeps its name.

Run at this head on mote (bun 1.4.2, HOME=$(mktemp -d)):
- packages/zen-free bun test: 208 pass, 0 fail.
- whole repo bun test: 4785 pass, 14 skip, 0 fail.
- bun scripts/check.mjs zen-free: ok.

Broken by hand:
- origin/main's provider.mjs: the three per-protocol #624 tests red.
- origin/main's tool-response.mjs (no renamed lookup): the same three red.
- the pair renamed even alone: "a request without a refused name" red.

Not tried: a request to the real Zen. Every probe from this network was
answered 429 FreeUsageLimitError today, a plain control without tools
included (the address's free allowance is spent until 00:00 UTC), so the
gate itself rests on the reporter's bisection. Not tried with Claude Code
desktop.
This commit is contained in:
yetone
2026-11-02 17:07:12 +08:00
parent 4c801e5756
commit 1080b81c7d
5 changed files with 178 additions and 2 deletions
+8
View File
@@ -91,6 +91,14 @@ itself (`edit`, `glob`, `grep`, `read`, `shell`), described as not
available. A call of one of them the agent doesn't have reaches the agent
like any other.
Zen's free tier also refuses some tool names outright, with `429
FreeUsageLimitError`: a name with `terminal` in it, and `ReportFindings`
offered beside `ScheduleWakeup`, as Claude Code desktop offers them
(#624). Those tools go to Zen under other names of the same words
(`mcp__ide__console_run`, `findings_report`, `wakeup_schedule`), in the
agent's past calls and `tool_choice` too, and a call of one reaches the
agent under its own name.
Zen's free tier also tells OpenCode's own requests by the environment
block every OpenCode prompt ends with (`<env>`, a `Working directory:`
line, `</env>`) in a system message. Another agent's request without one
+91
View File
@@ -937,3 +937,94 @@ test("the environment block names a real directory, never a fixed placeholder",
text = added("chat", { messages: [{ role: "system", content: "Working directory: unknown" }, { role: "user", content: "hi" }] });
assert.equal(line(text.split("<env>")[1]), home);
});
// Zen's free tier answers 429 FreeUsageLimitError at once to a tool name
// with "terminal" in it and to ReportFindings beside ScheduleWakeup, as
// Claude Code desktop offers them (alonewolfsy-web, plugins#624). They go
// under other names, in the agent's past calls and tool_choice too, and a
// call of one comes back under the agent's own name.
for (const { protocol, path, model } of protocols) {
test(`${protocol}: tools Zen refuses by name go under other names and come back as the agent's (#624)`, async (t) => {
const { plugin, state, origin } = await fixture(t);
await plugin.provider.models({ models: {} }, { auth: await publicAuth() });
const loader = await plugin.auth.loader(publicAuth);
const schema = { type: "object", properties: { command: { type: "string" } } };
const names = ["Bash", "ReportFindings", "ScheduleWakeup", "mcp__ide__terminal_run", "RunTerminal", "findings_report"];
const tools = names.map((name) => {
const tool = { name, description: `Client ${name}` };
if (protocol === "chat") return { type: "function", function: { ...tool, parameters: schema } };
if (protocol === "anthropic") return { ...tool, input_schema: schema };
return { type: "function", ...tool, parameters: schema };
});
const body = { model, stream: false, tools };
if (protocol === "chat") {
body.messages = [
{ role: "user", content: "hi" },
{ role: "assistant", content: null, tool_calls: [{ id: "c0", type: "function", function: { name: "ReportFindings", arguments: "{}" } }] },
{ role: "tool", tool_call_id: "c0", content: "ok" },
];
body.tool_choice = { type: "function", function: { name: "ScheduleWakeup" } };
} else if (protocol === "anthropic") {
body.messages = [
{ role: "user", content: "hi" },
{ role: "assistant", content: [{ type: "tool_use", id: "c0", name: "ReportFindings", input: {} }] },
{ role: "user", content: [{ type: "tool_result", tool_use_id: "c0", content: "ok" }] },
];
body.tool_choice = { type: "tool", name: "ScheduleWakeup" };
} else {
body.input = [
{ role: "user", content: "hi" },
{ type: "function_call", call_id: "c0", name: "ReportFindings", arguments: "{}" },
{ type: "function_call_output", call_id: "c0", output: "ok" },
];
body.tool_choice = { type: "function", name: "ScheduleWakeup" };
}
const send = () => loader.fetch(`${origin}/v1/${path}`, { method: "POST", body: JSON.stringify(body) });
for (const [upstream, expected] of [
["findings_report_2", "ReportFindings"],
["wakeup_schedule", "ScheduleWakeup"],
["mcp__ide__console_run", "mcp__ide__terminal_run"],
["RunConsole", "RunTerminal"],
["findings_report", "findings_report"],
["Bash", "Bash"],
]) {
state.reply = toolReply(protocol, upstream, true);
const response = await send();
assert.equal(response.status, 200);
const text = await response.text();
assert.match(text, new RegExp(`"name"\\s*:\\s*"${expected}"`), upstream);
}
const sent = state.calls.at(-1).body;
const sentNames = sent.tools.map((tool) => (protocol === "chat" ? tool.function.name : tool.name));
assert.deepEqual(sentNames.slice(0, names.length), [
"Bash",
"findings_report_2",
"wakeup_schedule",
"mcp__ide__console_run",
"RunConsole",
"findings_report",
]);
const past =
protocol === "chat"
? sent.messages.find((message) => message.tool_calls).tool_calls[0].function.name
: protocol === "anthropic"
? sent.messages[1].content[0].name
: sent.input[1].name;
assert.equal(past, "findings_report_2");
assert.equal(sent.tool_choice.function?.name ?? sent.tool_choice.name, "wakeup_schedule");
});
}
test("a request without a refused name goes with its tools as named (#624)", async (t) => {
const { plugin, state, origin } = await fixture(t);
await plugin.provider.models({ models: {} }, { auth: await publicAuth() });
const loader = await plugin.auth.loader(publicAuth);
// one of the pair alone is served
const tools = [...declaredTools("chat"), { type: "function", function: { name: "ReportFindings", parameters: { type: "object" } } }];
const response = await loader.fetch(`${origin}/v1/chat/completions`, {
method: "POST",
body: JSON.stringify({ model: "chat-free", stream: false, tools }),
});
await response.text();
assert.deepEqual(state.calls.at(-1).body.tools.slice(0, tools.length), tools);
});
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-zen-free-auth",
"version": "0.1.21",
"version": "0.1.22",
"description": "OpenCode Zen free models through the public credential, with native streaming protocols",
"type": "module",
"main": "./index.mjs",
+76 -1
View File
@@ -260,7 +260,82 @@ function normalizeTools(body, protocol) {
: { type: "function", ...definition, parameters },
);
}
return { declared };
return { declared, renamed: renameRefused(body, protocol) };
}
// Tool names Zen's free tier refuses to serve under, whatever else the
// request says, as alonewolfsy-web bisected a real Claude Code desktop
// request on 2026-11-01 (plugins#624): a name with "terminal" in it, in
// any case (an MCP server's terminal tools), and ReportFindings and
// ScheduleWakeup offered together, as Claude Code desktop's own tools are
// (either alone was served). Each got 429 FreeUsageLimitError at once, and
// the same request with those tools under other names was served. They go to Zen
// under a name of the same words (ReportFindings as findings_report,
// mcp__x__terminal_run as mcp__x__console_run), in the agent's past calls
// and tool_choice too, and a call of one comes back under the agent's own
// name (toolPolicy.renamed, read by tool-response.mjs's nameGuard).
const REFUSED_NAMES = { ReportFindings: "findings_report", ScheduleWakeup: "wakeup_schedule" };
const REFUSED_WORD = /terminal/i;
function servedName(name, taken) {
if (Object.hasOwn(REFUSED_NAMES, name))
return Object.keys(REFUSED_NAMES).every((pair) => taken.has(pair)) ? REFUSED_NAMES[name] : undefined;
if (!REFUSED_WORD.test(name)) return undefined;
return name.replace(new RegExp(REFUSED_WORD.source, "gi"), (word) =>
word === word.toUpperCase() ? "CONSOLE" : word[0] === "T" ? "Console" : "console",
);
}
// renameRefused gives each refused tool its served name and answers the
// served names, each to the agent's own.
function renameRefused(body, protocol) {
const get = (tool) => (protocol === "chat" ? tool?.function?.name : tool?.name);
const functions = protocol === "chat" && Array.isArray(body.functions) ? body.functions : [];
const taken = new Set(
[...body.tools.map(get), ...functions.map((fn) => fn?.name)].filter((name) => typeof name === "string"),
);
const served = new Map(); // agent's name -> served name
const renamed = new Map(); // served name -> agent's name
for (const name of taken) {
const wanted = servedName(name, taken);
if (!wanted) continue;
let alias = wanted;
for (let i = 2; taken.has(alias) || renamed.has(alias); i++) alias = `${wanted}_${i}`;
served.set(name, alias);
renamed.set(alias, name);
}
if (!served.size) return renamed;
const rename = (name) => (typeof name === "string" && served.has(name) ? served.get(name) : name);
for (const tool of body.tools) {
if (!served.has(get(tool))) continue;
if (protocol === "chat") tool.function.name = served.get(tool.function.name);
else tool.name = served.get(tool.name);
}
for (const fn of functions) if (served.has(fn?.name)) fn.name = served.get(fn.name);
// the agent's past calls of them
const messages = protocol === "responses" ? body.input : body.messages;
if (Array.isArray(messages)) {
for (const item of messages) {
if (protocol === "responses") {
if (item?.type === "function_call") item.name = rename(item.name);
} else if (protocol === "anthropic") {
if (Array.isArray(item?.content))
for (const part of item.content) if (part?.type === "tool_use") part.name = rename(part.name);
} else {
if (Array.isArray(item?.tool_calls))
for (const call of item.tool_calls) if (call?.function) call.function.name = rename(call.function.name);
if (item?.function_call) item.function_call.name = rename(item.function_call.name);
}
}
}
const choice = body.tool_choice;
if (choice && typeof choice === "object") {
if (choice.function && typeof choice.function === "object") choice.function.name = rename(choice.function.name);
else if ("name" in choice) choice.name = rename(choice.name);
}
if (body.function_call && typeof body.function_call === "object")
body.function_call.name = rename(body.function_call.name);
return renamed;
}
// Zen's free tier serves a request as OpenCode's own only when one of its
+2
View File
@@ -61,8 +61,10 @@ function nameGuard(policy) {
const bare = (name) => name.toLowerCase().replace(separators, "");
const byCase = foldsTo(declared, lower);
const byShape = foldsTo(declared, bare);
const renamed = policy.renamed ?? new Map();
return (name) => {
if (typeof name !== "string" || !name) return null;
if (renamed.has(name)) return renamed.get(name);
if (declared.has(name)) return name;
const match =
byCase.get(lower(name)) ?? (byCase.has(lower(name)) ? null : byShape.get(bare(name)));