zen-free: a declared name of separators only matches only itself, and the unused injected set is gone (#76 follow-up, tkhs101)

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.
This commit is contained in:
yetone
2026-10-12 18:18:36 +08:00
parent 6a5dd01d5d
commit b033987516
3 changed files with 20 additions and 3 deletions
+1 -3
View File
@@ -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) {
+3
View File
@@ -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;
+16
View File
@@ -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()]), ["_"]);
});