Files
opencode2api/internal/gateway/toolshape_test.go
T
CheerC 4a4db10387 fix(free-tier): hide client tool names from shaped requests (#161)
The free tier refuses a shaped body whose tool list still reads as another
client's session. A Claude Code request that declares both ReportFindings and
ScheduleWakeup is answered with FreeUsageLimitError — the same 429 a genuinely
exhausted IP receives — and the refusal is deterministic: rename either name
and the same request succeeds, and two empty placeholder tools carrying just
those two names reproduce it. Schema, size and tool count do not matter.

The gateway forwarded client tools as clint_<name>, so every Claude Code
session behind CLIProxyAPI was refused (14 tools, both markers present) while
the official CLI kept working and small requests through the gateway were
fine.

Client tools now get an opaque wire name: clint_ plus 12 hex characters of the
name's SHA-256, collision-suffixed like before. Descriptions and schemas are
untouched, so the model still knows what a tool does, and the response path
still restores the names the client declared, streaming included.

Verified against the captured Claude Code request (95 KB, 14 tools):
3/3 429 before the change, 3/3 200 after, and the same request replayed
through CLIProxyAPI (Anthropic /v1/messages -> chat -> /inference) returns
200. Session ids are unaffected: they are normalized by CanonicalSessionID,
and an implausible session timestamp is a separate 429 trigger documented
there.
2026-10-29 21:39:19 +08:00

97 lines
3.5 KiB
Go

package gateway
import (
"encoding/json"
"strings"
"testing"
wire "opencode2api/internal/protocol"
)
// TestPrepareAnonymousBodyHidesClientToolNames pins the fix for the free tier's
// client detection: a Claude Code request that declares both of these tools is
// answered with FreeUsageLimitError, and renaming either name lifts the
// refusal. The shaped body must therefore never spell a client tool name.
func TestPrepareAnonymousBodyHidesClientToolNames(t *testing.T) {
body := []byte(`{
"model": "deepseek-v4.1-flash-free",
"messages": [{"role": "user", "content": "hi"}],
"stream": false,
"tools": [
{"type": "function", "function": {"name": "Read", "description": "Read a file.",
"parameters": {"type": "object", "properties": {"file_path": {"type": "string"}}}}},
{"type": "function", "function": {"name": "ReportFindings", "description": "Report findings.",
"parameters": {"type": "object", "properties": {}}}},
{"type": "function", "function": {"name": "ScheduleWakeup", "description": "Schedule a wakeup.",
"parameters": {"type": "object", "properties": {}}}}
]
}`)
shaped, renames := prepareAnonymousBody(body, wire.Chat, false)
if len(shaped) == 0 {
t.Fatal("no shaped body")
}
text := string(shaped)
// The two Claude Code markers are distinctive enough to assert over the
// whole body; a generic name like Read also appears in the official prompt
// prose, so the tool list is what must stay free of it.
for _, marker := range []string{"ReportFindings", "ScheduleWakeup"} {
if strings.Contains(text, marker) {
t.Errorf("shaped body still spells the client tool %q", marker)
}
}
for _, client := range []string{"Read", "ReportFindings", "ScheduleWakeup"} {
if _, ok := renames.Wire(client); !ok {
t.Errorf("no wire name recorded for %q", client)
}
}
var payload struct {
Stream bool `json:"stream"`
Tools []struct {
Function struct {
Name string `json:"name"`
} `json:"function"`
} `json:"tools"`
}
if err := json.Unmarshal(shaped, &payload); err != nil {
t.Fatalf("shaped body is not JSON: %v", err)
}
if !payload.Stream {
t.Error("shaped body is not streaming")
}
names := make(map[string]bool, len(payload.Tools))
for _, tool := range payload.Tools {
names[tool.Function.Name] = true
}
// The official set is what makes the request read as an OpenCode session;
// dropping it is refused just like keeping a foreign tool name.
for _, official := range []string{"read", "edit", "shell"} {
if !names[official] {
t.Errorf("shaped body is missing the official tool %q", official)
}
}
for client := range map[string]bool{"Read": true, "ReportFindings": true, "ScheduleWakeup": true} {
wireName, _ := renames.Wire(client)
if !names[wireName] {
t.Errorf("client tool %q is not declared under its wire name %q", client, wireName)
}
}
}
// TestPrepareAnonymousBodyRestoresToolCalls keeps the response side honest: a
// call the model makes to a renamed client tool comes back under the name the
// client declared.
func TestPrepareAnonymousBodyRestoresToolCalls(t *testing.T) {
body := []byte(`{"model":"deepseek-v4.1-flash-free","messages":[{"role":"user","content":"hi"}],
"tools":[{"type":"function","function":{"name":"ScheduleWakeup","parameters":{"type":"object","properties":{}}}}]}`)
_, renames := prepareAnonymousBody(body, wire.Chat, false)
wireName, ok := renames.Wire("ScheduleWakeup")
if !ok {
t.Fatal("no wire name recorded")
}
if got := renames.Restore(wireName); got != "ScheduleWakeup" {
t.Errorf("Restore(%q) = %q, want ScheduleWakeup", wireName, got)
}
}