Repository navigation
Description
Problem
A shaped free-tier body appends the lane's official tool set (shell, grep, glob, read, task, …) and injects the official system prompt, whose text names those tools. Models follow the prompt and call shell / grep / glob, but the downstream client never declared those names, so Claude Code answers Error: No such tool available: shell and the turn is wasted.
The same fate meets two other classes of name:
- mangled wire names such as
clit_…,clnt_…,clich_…(the client or the model drops a character from a digest name), and - leftover ghost calls such as an earlier truncated tool id replayed as a tool name.
ToolRenames.Resolve only restores an exact clint_<digest>, so all of the above are forwarded to the client unchanged; the model then imitates them on later turns, which makes the failure self-reinforcing.
This is the follow-up to the fix that introduced digest names (#163, PR #161). That change is correct and must stay — it removed a real 429 FreeUsageLimitError — but it left the response and history paths unaware of the official names the gateway itself puts on the wire.
Evidence
Measured in a scratch copy of 2bf547c against a captured Claude Code request (Anthropic /v1/messages → Chat, replayed through CLIProxyAPI), shaped with PrepareRequest + prepareAnonymousBody:
| lookup | before | after |
|---|---|---|
Resolve("shell") |
shell |
Bash |
Resolve("bash") / "read" / "edit" / "write" |
folded only if unique | Bash / Read / Edit / Write |
Resolve("webfetch") |
WebFetch (case fold) |
WebFetch (alias) |
Resolve("clint_9b9a8d05a7ec") |
Read |
Read (unchanged) |
Resolve("clit_9b9a8d05a7ec") |
unchanged | unchanged (deliberate: the gateway must not guess a dropped character) |
client declares no Grep |
grep |
grep (no invention) |
History sanitisation on a synthetic turn that carried clit_9b9a8d05a7ec and shell:
before: assistant tool_calls = [clit_9b9a8d05a7ec, shell] msg role=tool "x" (orphan)
after: assistant tool_calls = [clint_d1e9567da9a8] msg role=tool "y"
The mangled call and its paired result are gone, the official-equivalent call is rewritten to the declared tool's wire name, and no orphan tool message remains. On the full captured request the same pass leaves zero orphan tool results across 586 tool blocks.
Chat stream rewriting, split name with interleaved arguments:
frames: name "c" → arguments '{"x":' → name "lint_9b9a8d05a7ec" → arguments '1}'
output: "name":"Read" with arguments {"x":1}
Before the change the first fragment was flushed as "c" as soon as an arguments field appeared in the same delta, which locks in a partial name because Anthropic tool_use names cannot be changed after the block opens.
Scale of the symptom in practice (Claude Code session logs, ~/.claude/projects): 57 No such tool available: shell, 49 grep, 8 glob spread over two weeks, plus mangled clit_…/clnt_…/claude_… names from the day digest names shipped. None of these produced a 429; they were silent tool-call failures.
Scope
internal/protocol/tools.go— official-name alias table consulted between the exact wire name and theclint_prefix fallback.internal/gateway/toolshape.go— binds aliases only to tools the client declared, and sanitises request history for Anthropic, Chat and Responses.internal/protocol/stream.go— holds a Chat name fragment while it is still a prefix of a known wire name and buffers the accompanying arguments until the name resolves.README.md,README.zh-CN.md— the Tool shaping section.
Deliberately unchanged: the digest naming, the official system prompt, the appended official tool set, and 429/403 classification. Removing the official tool set would be the cleaner fix for the shell class but risks reintroducing the 429 that #161 fixed; that needs an upstream replay to decide and is out of scope here.
Expected Outcome
- An official name reaches the client under the name the client declared when an equivalent exists, and is left untouched when none does.
- Tool calls in history whose name is neither declared nor equivalent are removed together with their paired result, and a named
tool_choicepointing at one is removed too. - A Chat name split across chunks never leaks a partial or wire name, and its arguments still arrive intact.
- A single-frame tool call keeps its current output.
gofmt -l .,go vet ./...andgo build ./cmd/opencode2apiare clean; no_test.gois added, matching the repo's test-free released tree.
Related
- Follows Shaped free-tier requests still carry client tool names, so proxied Claude Code sessions 429 as FreeUsageLimitError #163 / PR fix(free-tier): hide client tool names from shaped requests #161 (digest wire names).
- Related: Zen free-tier 429: confirmed quota model, fixes applied, and open questions needing verification #158 (free-tier 429 umbrella), Theory: anonymous-lane 429s are conversation-title requests, not quota #164 (a different 429 theory).
- Independent of Zen free-tier 429: confirmed quota model, fixes applied, and open questions needing verification #158's exit rotation and proxy work.
Activity
merge: upstream/main — adopt jasonxu114514#166 tool-name fix, keep th…