Files
magpie/internal/gateway/deferred_route_test.go
T
yetone fcbc1a2d02 gateway: Claude Code's deferred tools are declared in full where a load would lose the cache
Seapawn1 on X: GPT in Claude Code loses its prompt cache with tool search
on. A tool ToolSearch loads joined the top-level tools on the next request
on every route but Anthropic's and a Responses upstream that loads in
place (LoadedInPlace, #2660). An OpenAI-style cache is a prefix with the
tools before the messages, so each load (2.1.327 defers WebFetch,
WebSearch, Monitor and the other built-ins too) read the whole
conversation again uncached. A sandboxed Claude Code 2.1.327 through a
fake upstream showed it: on Chat and on a Responses relay the tools
changed at req1 and req3; in place they held.

On such a route a Claude Code request carrying DeferredToolPlaceholder is
answered 400 invalid_request_error naming tools.defer_loading, locally.
The real Claude Code 2.1.327 answers that (after its beta-strip retries,
3 refused requests) by declaring every tool in full for the rest of that
conversation, --continue included; a new conversation, on a Claude model,
tries tool search again. The message doesn't name the beta header, which
Claude Code would remember everywhere. Only while the reminder names 50
deferred tools or fewer: an MCP router's hundreds keep tool search, and a
request whose reminder can't be read keeps it too. Anthropic routes and
in-place Responses upstreams are unchanged. The refusal is not a member's
fault (retryable/shapeRefused false), so a group doesn't fall over or
rest.

Not handled: a user who set ENABLE_TOOL_SEARCH themselves can't be told
apart on the wire; additional-tools=on keeps search on a relay that takes
the item.

Run:
- mica: go test -run 'TestDeferred|TestLoadedTools|TestKimiIsSent|TestToolSearch|TestDeferredToolBlocks' ./internal/gateway ok.
- Broken by hand (the check in translate made false): TestDeferredToolsTurnedAwayWhereTheyLoseTheCache and TestDeferredToolsKeptWhereLoadedInPlace red (status 200, upstream called).
- mica: GOOS=linux and GOOS=windows go vet ./internal/gateway ./internal/agent ok; gofmt -l clean.
- wsl-run (magpie-desktop): go test -timeout 45m ./internal/gateway ./internal/agent started before push; result with the full suite.
- Real Claude Code 2.1.327 in a sandbox HOME against a fake 400 naming defer_loading: retried with every tool declared, kept for the conversation and --continue, new conversation searched again.

Full suite: run after push.
2026-11-19 01:13:33 +08:00

138 lines
6.1 KiB
Go

package gateway
import (
"fmt"
"net/http/httptest"
"strings"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// ccDeferred is Claude Code 2.1.327's reminder naming its deferred tools,
// as a sandboxed run with three MCP tools sent it.
func ccDeferred(names ...string) string {
return "The following deferred tools are now available via ToolSearch. Their schemas are NOT loaded — calling them directly will fail with InputValidationError. Use ToolSearch with query \"select:<name>[,<name>...]\" to load tool schemas before calling them:\n" +
strings.Join(names, "\n") + "\n\nAvailable agent types for the Agent tool:\n- claude: Catch-all"
}
var ccDeferredNames = []string{"CronCreate", "CronDelete", "CronList", "EnterWorktree", "ExitWorktree", "Monitor", "NotebookEdit", "SendMessage", "TaskStop", "WebFetch", "WebSearch", "mcp__fk__alpha", "mcp__fk__beta", "mcp__fk__gamma"}
func ccSearchRequest(names []string, tools ...string) string {
reminder := fmt.Sprintf(`{"role":"user","content":[{"type":"text","text":%q},{"type":"text","text":"hi"}]}`, ccDeferred(names...))
return tsRequest(append([]string{tsTools0}, tools...), reminder)
}
// Seapawn1 on X: GPT in Claude Code loses its prompt cache with tool
// search on. A tool ToolSearch loads joins the request's tools on a Chat
// route, before every message, so the start of the prompt changed at each
// load. Such a route turns the deferred tools away with a 400 naming
// defer_loading, which Claude Code answers with every tool declared in
// full; a route that loads in place, an Anthropic one, a list too long to
// declare and a request without Claude Code's placeholder go as before.
func TestDeferredToolsTurnedAwayWhereTheyLoseTheCache(t *testing.T) {
ok := sse(
`data: {"id":"c1","model":"m1","choices":[{"index":0,"delta":{"role":"assistant","content":"ok"}}]}`,
`data: {"id":"c1","model":"m1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"completion_tokens":1}}`,
`data: [DONE]`)
f := &fake{t: t, reply: ok}
setup(t, provider.Chat, f)
body := ccSearchRequest(ccDeferredNames, tsPlaceholder)
code, out := post(t, "/v1/messages", body)
if code != 400 || !strings.Contains(out, "defer_loading") || f.calls != 0 {
t.Fatalf("Chat route: status %d, %d upstream calls: %s", code, f.calls, out)
}
if gjson.Get(out, "type").String() != "error" || gjson.Get(out, "error.type").String() != "invalid_request_error" {
t.Fatalf("not an Anthropic invalid_request_error: %s", out)
}
// the beta header named would make Claude Code drop tool search for
// every conversation, a Claude model's too
if strings.Contains(out, "anthropic-beta") || strings.Contains(out, "tool-search") || strings.Contains(out, "advanced-tool-use") {
t.Fatalf("the refusal names the beta: %s", out)
}
// Claude Code's retry: every tool declared, no placeholder
full := ccSearchRequest(ccDeferredNames, tsReadA, tsReadB)
if code, out := post(t, "/v1/messages", full); code != 200 {
t.Fatalf("the retry with every tool declared: status %d: %s", code, out)
}
if n := len(gjson.GetBytes(f.got, "tools").Array()); n != 4 {
t.Fatalf("the retry reached the upstream with %d tools: %s", n, f.got)
}
// hundreds of deferred tools, an MCP router's, keep tool search
many := make([]string, 300)
for i := range many {
many[i] = fmt.Sprintf("mcp__router__t%d", i)
}
f.calls = 0
if code, out := post(t, "/v1/messages", ccSearchRequest(many, tsPlaceholder)); code != 200 || f.calls != 1 {
t.Fatalf("300 deferred tools: status %d, %d calls: %s", code, f.calls, out)
}
// no reminder to count from: unknown, so tool search stays
f.calls = 0
if code, out := post(t, "/v1/messages", tsRequests[1]); code != 200 || f.calls != 1 {
t.Fatalf("no reminder: status %d, %d calls: %s", code, f.calls, out)
}
}
// A Responses upstream that takes additional_tools keeps tool search: the
// tools it loads go in place, and the prefix holds (#2660).
func TestDeferredToolsKeptWhereLoadedInPlace(t *testing.T) {
f := &fake{t: t, reply: sse(
`data: {"type":"response.created","response":{"id":"r1","model":"m1"}}`,
`data: {"type":"response.completed","response":{"id":"r1","output":[],"usage":{"input_tokens":5,"output_tokens":2}}}`)}
setup(t, provider.Responses, f)
body := ccSearchRequest(ccDeferredNames, tsPlaceholder)
if code, out := post(t, "/v1/messages", body); code != 400 || !strings.Contains(out, "defer_loading") {
t.Fatalf("a Responses relay without additional_tools: status %d: %s", code, out)
}
p, err := provider.Find("fake")
if err != nil {
t.Fatal(err)
}
p.AdditionalTools = true
if err := provider.Save(*p); err != nil {
t.Fatal(err)
}
if code, out := post(t, "/v1/messages", body); code != 200 {
t.Fatalf("a relay that takes additional_tools: status %d: %s", code, out)
}
}
// A Claude model on Anthropic's API keeps tool search.
func TestDeferredToolsKeptOnAnthropic(t *testing.T) {
f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg_1","type":"message","role":"assistant","model":"m1","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}`}
setup(t, provider.Anthropic, f)
if code, out := post(t, "/v1/messages", ccSearchRequest(ccDeferredNames, tsPlaceholder)); code != 200 || !strings.Contains(string(f.got), "DeferredToolPlaceholder") {
t.Fatalf("status %d: %s\nsent %s", code, out, f.got)
}
}
func TestDeferredCount(t *testing.T) {
r, err := parseAnthropic([]byte(ccSearchRequest(ccDeferredNames, tsPlaceholder)))
if err != nil {
t.Fatal(err)
}
if n, ok := deferredCount(r); !ok || n != len(ccDeferredNames) {
t.Fatalf("count = %d %v, want %d", n, ok, len(ccDeferredNames))
}
if !r.DeferredNamed {
t.Fatal("the placeholder was not seen")
}
// the refusal is about the request: a group doesn't fall to its next
// member for it, nor rest the account
if body := []byte(writeErrorBody(deferredRefusal)); retryable(400, body) || shapeRefused(400, body) {
t.Fatal("the refusal reads as a member's fault")
}
}
func writeErrorBody(msg string) string {
rec := httptest.NewRecorder()
writeError(rec, provider.Anthropic, 400, msg)
return rec.Body.String()
}