From b033987516968633de0f4656b227e178f30dad3b Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 12 Oct 2026 18:17:32 +0800 Subject: [PATCH] zen-free: a declared name of separators only matches only itself, and the unused injected set is gone (#76 follow-up, tkhs101) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit foldsTo kept a declared name whose fold is "" (an agent declaring "_"), so any call of separators only ("--", "-", "__") was handed that tool: an empty fold matching an empty fold, not a name matching a name. Such a name is now skipped in the fold maps, so it matches only itself and the other calls are left out with the note like any unknown name. normalizeTools still built and returned `injected`, which nothing has read since the guard folds over `declared` alone; it is gone. The "Never call this tool" stubs are sent as before. Run: packages/zen-free `bun test` 162 pass; the new test goes red with the foldsTo change taken out. Whole repo `bun test` (temp HOME and TMPDIR) and `node --test packages/zen-free` pass; `bun scripts/check.mjs zen-free` ✓. Rides in 0.1.4 with the #91 change. --- packages/zen-free/provider.mjs | 4 +--- packages/zen-free/tool-response.mjs | 3 +++ packages/zen-free/tool-response.test.mjs | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/zen-free/provider.mjs b/packages/zen-free/provider.mjs index 463137e..8ef048b 100644 --- a/packages/zen-free/provider.mjs +++ b/packages/zen-free/provider.mjs @@ -67,10 +67,8 @@ function normalizeTools(body, protocol) { if (protocol === "chat" && Array.isArray(body.functions)) { for (const tool of body.functions) if (typeof tool?.name === "string") declared.add(tool.name); } - const injected = new Set(); for (const name of CORE_TOOLS) { if (present.has(name)) continue; - injected.add(name); const parameters = { type: "object", properties: {}, additionalProperties: false }; const definition = { name, description: "Not available. Never call this tool." }; body.tools.push( @@ -81,7 +79,7 @@ function normalizeTools(body, protocol) { : { type: "function", ...definition, parameters }, ); } - return { declared, injected }; + return { declared }; } function sessionID(headers, body) { diff --git a/packages/zen-free/tool-response.mjs b/packages/zen-free/tool-response.mjs index cedef0f..bf13a2c 100644 --- a/packages/zen-free/tool-response.mjs +++ b/packages/zen-free/tool-response.mjs @@ -40,6 +40,9 @@ function foldsTo(declared, key) { const folded = new Map(); for (const name of declared) { const k = key(name); + // A name of separators only folds to nothing, and would be handed any + // other such call ("_" for "--"): it matches only itself. + if (!k) continue; folded.set(k, folded.has(k) ? null : name); } return folded; diff --git a/packages/zen-free/tool-response.test.mjs b/packages/zen-free/tool-response.test.mjs index c882676..a5697d3 100644 --- a/packages/zen-free/tool-response.test.mjs +++ b/packages/zen-free/tool-response.test.mjs @@ -1397,3 +1397,19 @@ test("anthropic and responses: a stream cut short still fails, naming the reply assert.match(message, protocol === "anthropic" ? /reply msg_1/ : /reply resp_1/); } }); + +test("a declared name of separators only is never handed another call (#76 follow-up)", async () => { + const rules = { declared: new Set(["todo_write", "_"]) }; + for (const name of ["--", "-", "__"]) { + const result = await guardToolResponse( + Response.json(snapshot("chat", [name])), + "chat", + rules, + ); + const data = await result.json(); + assert.deepEqual(callsIn("chat", [data]), []); + assert.match(textIn("chat", [data]), /a tool this agent doesn't have/); + } + const exact = await guardToolResponse(Response.json(snapshot("chat", ["_"])), "chat", rules); + assert.deepEqual(callsIn("chat", [await exact.json()]), ["_"]); +});