zen-free 0.1.1: an OpenCode tool name is given the agent's own spelling, and a refused call names its tool (#76, tkhs101)
Zen's models open multi-step work with OpenCode's own todowrite, while DeepSeek Harness declares todo_write. The guard folded names only for the five core tools the plugin injects, so with an agent that declares all five nothing was injected, nothing folded, and the whole turn died with "Upstream called a tool not declared by the client" - without saying which tool. Now any call is given the agent's tool whose name differs only in case, or else only in case and separators (todowrite -> todo_write, webfetch -> web_fetch, BASH -> bash), when exactly one declared tool folds to it; a name two declared tools fold to is still refused rather than guessed. A refused call's error ends with the tool's name. Run: bun test and node --test in packages/zen-free (141 pass; under a load average of ~140 bun's 32 MiB buffer tests sometimes hit their 5s timeout, the same on the old code, one run fully green); the new #76 tests fail on the old tool-response.mjs (6 red). bun scripts/check.mjs zen-free. Live against Zen (step-5-preview-free, chat, stream): calls pass with the folded names; the model didn't emit todowrite in 6 tries, so the mapping itself is shown by the tests.
This commit is contained in:
@@ -31,6 +31,9 @@ Requests go to `https://opencode.ai/zen/v1` using each model's native
|
||||
Chat Completions, Responses or Anthropic Messages API. Streaming and
|
||||
ordinary JSON replies are supported.
|
||||
|
||||
The agent's tools are preserved. If Zen calls an added tool, its name is
|
||||
mapped to the agent's unique case-insensitive match (`bash` to `Bash`).
|
||||
A call with no matching tool returns an error. Tools run in the agent.
|
||||
The agent's tools are preserved. Zen's models often call OpenCode's own
|
||||
tool names; such a call is given the agent's tool whose name differs only
|
||||
in case (`bash` to `Bash`), or else only in case and `_`/`-` (`todowrite`
|
||||
to `todo_write`, `webfetch` to `web_fetch`), when exactly one does. A
|
||||
call with no such tool returns an error that names it. Tools run in the
|
||||
agent.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-zen-free-auth",
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.1",
|
||||
"description": "OpenCode Zen free models through the public credential, with native streaming protocols",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
@@ -30,20 +30,35 @@ function list(value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
function nameGuard(policy) {
|
||||
const declared = new Set(policy.declared);
|
||||
const injected = new Set(policy.injected);
|
||||
// OpenCode's own models call OpenCode's tool names (bash, todowrite,
|
||||
// webfetch) whatever the agent declared. A call is given the agent's tool
|
||||
// whose name differs only in case, or else only in case and separators
|
||||
// (todowrite → todo_write, webfetch → web_fetch), when exactly one does; a
|
||||
// name two of the agent's tools fold to is never guessed between.
|
||||
const separators = /[\s_.-]/g;
|
||||
function foldsTo(declared, key) {
|
||||
const folded = new Map();
|
||||
for (const name of declared) {
|
||||
const key = name.toLowerCase();
|
||||
folded.set(key, folded.has(key) ? null : name);
|
||||
const k = key(name);
|
||||
folded.set(k, folded.has(k) ? null : name);
|
||||
}
|
||||
return folded;
|
||||
}
|
||||
|
||||
function nameGuard(policy) {
|
||||
const declared = new Set(policy.declared);
|
||||
const lower = (name) => name.toLowerCase();
|
||||
const bare = (name) => name.toLowerCase().replace(separators, "");
|
||||
const byCase = foldsTo(declared, lower);
|
||||
const byShape = foldsTo(declared, bare);
|
||||
return (name) => {
|
||||
if (typeof name !== "string" || !name) throw new GuardError("Missing upstream tool name");
|
||||
if (declared.has(name)) return name;
|
||||
const match = injected.has(name) && folded.get(name.toLowerCase());
|
||||
const match = byCase.get(lower(name)) ?? (byCase.has(lower(name)) ? null : byShape.get(bare(name)));
|
||||
if (match) return match;
|
||||
throw new GuardError("Upstream called a tool not declared by the client");
|
||||
throw new GuardError(
|
||||
`Upstream called a tool not declared by the client: ${JSON.stringify(name.slice(0, 128))}`,
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -195,8 +195,8 @@ for (const protocol of protocols) {
|
||||
for (const [name, rules] of [
|
||||
["unknown", policy],
|
||||
["shell", policy],
|
||||
["BASH", policy],
|
||||
["bash", { declared: new Set(["Bash", "BASH"]), injected: new Set(["bash"]) }],
|
||||
["todowrite", { declared: new Set(["todo_write", "todo-write"]), injected: new Set() }],
|
||||
["bash", { declared: new Set(), injected: new Set(["bash"]) }],
|
||||
]) {
|
||||
const response = await guardToolResponse(
|
||||
@@ -211,6 +211,55 @@ for (const protocol of protocols) {
|
||||
}
|
||||
});
|
||||
|
||||
// #76: DSH declares all five core tools (so nothing is injected) and
|
||||
// todo_write; step-5-preview-free opens with OpenCode's todowrite.
|
||||
test(`${protocol}: an OpenCode tool name is given the agent's own spelling of it (#76)`, async () => {
|
||||
const rules = {
|
||||
declared: new Set(["bash", "read", "edit", "glob", "grep", "write", "todo_write", "web_fetch", "TodoRead"]),
|
||||
injected: new Set(),
|
||||
};
|
||||
for (const [asked, given] of [
|
||||
["todowrite", "todo_write"],
|
||||
["webfetch", "web_fetch"],
|
||||
["todoread", "TodoRead"],
|
||||
["BASH", "bash"],
|
||||
]) {
|
||||
const json = await guardToolResponse(
|
||||
Response.json(snapshot(protocol, [asked])),
|
||||
protocol,
|
||||
rules,
|
||||
);
|
||||
assert.equal(json.status, 200);
|
||||
assert.deepEqual(namesIn(protocol, await json.json()), [given]);
|
||||
const streamed = await guardToolResponse(
|
||||
sse(event(streamSnapshot(protocol, [asked])) + terminal(protocol)),
|
||||
protocol,
|
||||
rules,
|
||||
);
|
||||
const frames = parse(await streamed.text()).filter(
|
||||
(frame) => frame.data && frame.data !== "[DONE]",
|
||||
);
|
||||
assert.deepEqual(namesIn(protocol, frames[0].data), [given]);
|
||||
}
|
||||
});
|
||||
|
||||
test(`${protocol}: a refused call names the tool (#76)`, async () => {
|
||||
const json = await guardToolResponse(
|
||||
Response.json(snapshot(protocol, ["subagent"])),
|
||||
protocol,
|
||||
policy,
|
||||
);
|
||||
const body = await json.json();
|
||||
assert.match(body.message ?? body.error?.message, /: "subagent"$/);
|
||||
const streamed = await guardToolResponse(
|
||||
sse(event(streamSnapshot(protocol, ["subagent"])) + terminal(protocol)),
|
||||
protocol,
|
||||
policy,
|
||||
);
|
||||
const frame = parse(await streamed.text())[0].data;
|
||||
assert.match(frame.message ?? frame.error?.message, /: "subagent"$/);
|
||||
});
|
||||
|
||||
test(`${protocol}: exact matches win over ambiguous casing`, async () => {
|
||||
const rules = { declared: new Set(["bash", "Bash"]), injected: new Set(["bash"]) };
|
||||
const result = await guardToolResponse(
|
||||
@@ -245,7 +294,7 @@ for (const protocol of protocols) {
|
||||
});
|
||||
|
||||
test(`${protocol}: bad mixed SSE snapshot never leaks a tool and cancels immediately`, async () => {
|
||||
for (const name of ["unknown", "shell", "BASH"]) {
|
||||
for (const name of ["unknown", "shell", "todowrite"]) {
|
||||
let cancelled = false;
|
||||
const source = openSource(
|
||||
event(streamSnapshot(protocol, ["Read", name])) + event({ sentinel: "must not escape" }),
|
||||
|
||||
Reference in New Issue
Block a user