Files
magpie/internal/gateway/loaded_in_place_test.go
T
yetone c688211d13 gateway: a ChatGPT (Codex) account gets loaded tools where they were loaded, keeping its cache (#4738)
infinitex233 (#4738, Discord): Claude Code -> magpie -> a Codex account,
and every ToolSearch load dropped cache_read to 0. loadsInPlace sent the
additional_tools item (#2660) only to OpenAI's own API, so for an
account the loaded tool went into the top-level tools and the cached
prefix broke at the tools.

Tried on the real ChatGPT backend first (owner's account, sandbox HOME
on mote with a copied auth.json whose refresh_token was replaced, so the
live token was never rotated; ~/.codex untouched). A probe client sent
Claude Code's shape: ToolSearch with two deferred mcp tools, then the
tool_reference result, then the calls, 4 requests per run.
- additional_tools item (on, and this commit's default): request 2..4
  read 7680-8320 tokens from cache on gpt-5.6-terra, gpt-6-luna and
  gpt-6-sol; the model called read_a and read_b from the item; no
  "turned away additional_tools" in the log.
- tools top-level (off): request 2 and 3 read 0 from cache (8044 and
  8163 tokens in), as the reporter saw.
So the backend takes it, and this makes it the default for a ChatGPT
account (p.Account.Agent == "codex"). Copilot and other sign-ins stay
off: not tried.

- loadsInPlace: AdditionalToolsOff wins, then AdditionalTools, then an
  account is in only if it's Codex's, then OpenAI's API as before.
- `magpie provider set <id> additional-tools=on|off|default`: off is new
  (AdditionalToolsOff, kept through an account's Save by accountSettings
  and through the GUI editor's save like AdditionalTools). The old
  "off"/"no" meant default; now they turn it off where the default is on.
- The 400/422 fallback that names the item still applies, so a backend
  that stops taking it costs one retry and then sends tools top-level.
- No prompt, instructions or identity line changed.

Run (mote, at base 586b59fe7, this commit and the previous together):
- go test -count=1 -timeout 45m ./internal/provider ./internal/gateway
  ./internal/cli ./internal/gui: provider ok 248s, gateway ok 830s,
  cli ok 105s; gui FAIL on two filed reds that this doesn't touch:
  TestSettingsSaveKeepsWhatItDoesNotSend (BusyLoad, #4739) and
  TestMainWindowCorners/macOS_26_SDK on macOS 27 (#3949).
- Rebased on origin/main 9feb1024c, then again at this head: gofmt
  clean, vet darwin/linux/windows ok, provider ok 179s, cli ok 88s,
  gateway -run 'LoadsInPlace|LoadedTools|Codex|AdditionalTools|InPlace|
  Account' ok 99s, gui -run 'Provider|Settings|Account' red only on
  #4739. The whole gateway and gui packages ran at the base only.
- gofmt -l clean; go vet -tags nogui ./... for darwin, linux, windows ok.
- New TestCodexAccountLoadsInPlace (a fake ChatGPT backend through the
  gateway: the item with read_a, then tools top-level after
  additionalToolsOff in providers.json); TestLoadsInPlaceScope extended
  (Codex on, Codex off, OpenAI off, Copilot off by default and on when
  set); CLI test for off, on after off, default.
- Broken by hand: with the Codex default taken out of loadsInPlace,
  TestCodexAccountLoadsInPlace and TestLoadsInPlaceScope go red.
- GUI node suite not run: no JS changed; the Go editor save only keeps
  one more field, covered by the Go tests above.

Not tried: the reporter's own setup (Windows, gpt-6.1-terra), a real
Claude Code client (the probe sent its request shape), Copilot or any
other signed-in vendor with the item.
2026-11-01 14:59:44 +08:00

270 lines
13 KiB
Go

package gateway
import (
"fmt"
"net/http"
"reflect"
"strings"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// Claude Code's tool search as maximia's probe recorded it (#2660, 2.1.302):
// the first request offers ToolSearch and the placeholder that keeps
// deferred loading on; once ToolSearch answers with a tool_reference, the
// next request offers the loaded MCP tool too, defer_loading true, and the
// one after that loads a second tool the same way.
const tsTools0 = `{"name":"Bash","description":"Run a command","input_schema":{"type":"object","properties":{"command":{"type":"string"}},"required":["command"]}},
{"name":"ToolSearch","description":"Fetches full schema definitions for deferred tools","input_schema":{"type":"object","properties":{"query":{"type":"string"},"max_results":{"type":"number"}},"required":["query","max_results"]}}`
const tsPlaceholder = `{"name":"DeferredToolPlaceholder","description":"","input_schema":{"type":"object"},"defer_loading":true}`
const tsReadA = `{"name":"mcp__probe__read_a","description":"Read A","input_schema":{"type":"object","properties":{"path":{"type":"string"}}},"defer_loading":true}`
const tsReadB = `{"name":"mcp__probe__read_b","description":"Read B","input_schema":{"type":"object","properties":{"path":{"type":"string"}}},"defer_loading":true}`
const tsSystem = `[{"type":"text","text":"You are Claude Code."}]`
const tsTurn1 = `{"role":"user","content":[{"type":"text","text":"load read_a with ToolSearch, call it, then load read_b and call it"}]}`
const tsSearchA = `{"role":"assistant","content":[{"type":"tool_use","id":"toolu_01","name":"ToolSearch","input":{"query":"select:mcp__probe__read_a","max_results":1}}]},
{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_01","content":[{"type":"tool_reference","tool_name":"mcp__probe__read_a"}]}]}`
const tsCallA = `{"role":"assistant","content":[{"type":"tool_use","id":"toolu_02","name":"mcp__probe__read_a","input":{"path":"a"}}]},
{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_02","content":"A"}]}`
const tsSearchB = `{"role":"assistant","content":[{"type":"tool_use","id":"toolu_03","name":"ToolSearch","input":{"query":"select:mcp__probe__read_b","max_results":1}}]},
{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_03","content":[{"type":"tool_reference","tool_name":"mcp__probe__read_b"}]}]}`
func tsRequest(tools []string, msgs ...string) string {
return fmt.Sprintf(`{"model":"m1","max_tokens":1000,"stream":true,"system":%s,"tools":[%s],"messages":[%s]}`,
tsSystem, strings.Join(tools, ","), strings.Join(msgs, ","))
}
// the three requests of maximia's probe
var tsRequests = []string{
tsRequest([]string{tsTools0, tsPlaceholder}, tsTurn1),
tsRequest([]string{tsTools0, tsReadA, tsPlaceholder}, tsTurn1, tsSearchA),
tsRequest([]string{tsTools0, tsReadA, tsReadB, tsPlaceholder}, tsTurn1, tsSearchA, tsCallA, tsSearchB),
}
func tsBuild(t *testing.T, body string, inPlace bool) gjson.Result {
t.Helper()
r, err := parseAnthropic([]byte(body))
if err != nil {
t.Fatal(err)
}
r.LoadedInPlace = inPlace
return gjson.ParseBytes(buildResponses(r, "m1", "api.openai.com", false))
}
func tsNames(v gjson.Result) []string {
var out []string
v.ForEach(func(_, t gjson.Result) bool {
out = append(out, t.Get("name").String())
return true
})
return out
}
// TestLoadedToolsKeepThePrefix: a tool Claude Code's tool search loads goes
// to a Responses upstream that takes additional_tools in such an item after
// the result that loaded it, so the top-level tools, the instructions and
// every earlier input item stay as they were sent (#2660).
func TestLoadedToolsKeepThePrefix(t *testing.T) {
var outs []gjson.Result
for _, body := range tsRequests {
outs = append(outs, tsBuild(t, body, true))
}
first := outs[0]
for i, o := range outs {
if got, want := o.Get("tools").Raw, first.Get("tools").Raw; got != want {
t.Fatalf("request %d's tools changed:\n%s\nwant\n%s", i+1, got, want)
}
if o.Get("instructions").Raw != first.Get("instructions").Raw {
t.Fatalf("request %d's instructions changed", i+1)
}
if i == 0 {
continue
}
prev, cur := outs[i-1].Get("input").Array(), o.Get("input").Array()
if len(cur) <= len(prev) {
t.Fatalf("request %d's input is no longer than the one before", i+1)
}
for j := range prev {
if prev[j].Raw != cur[j].Raw {
t.Fatalf("request %d's input item %d changed:\n%s\nwas\n%s", i+1, j, cur[j].Raw, prev[j].Raw)
}
}
}
if got := tsNames(first.Get("tools")); strings.Join(got, ",") != "Bash,ToolSearch" {
t.Fatalf("tools = %v", got)
}
// read_a right after the result that loaded it, read_b after its own
in := outs[2].Get("input").Array()
var kinds []string
for _, it := range in {
k := it.Get("type").String()
if k == "additional_tools" {
k += ":" + strings.Join(tsNames(it.Get("tools")), "+")
if it.Get("role").String() != "developer" {
t.Fatalf("item without the developer role: %s", it.Raw)
}
}
kinds = append(kinds, k)
}
want := "message,function_call,function_call_output,additional_tools:mcp__probe__read_a,function_call,function_call_output,function_call,function_call_output,additional_tools:mcp__probe__read_b"
if strings.Join(kinds, ",") != want {
t.Fatalf("input = %s\nwant %s", strings.Join(kinds, ","), want)
}
loaded := in[3].Get("tools.0")
if loaded.Get("type").String() != "function" || loaded.Get("parameters.properties.path.type").String() != "string" || loaded.Get("description").String() != "Read A" {
t.Fatalf("loaded tool = %s", loaded.Raw)
}
// what the client is told of the load is as before
if got := in[2].Get("output").String(); !strings.Contains(got, "mcp__probe__read_a is loaded") {
t.Fatalf("output = %q", got)
}
}
// TestLoadedToolsInOneItem: tools loaded by results answered together, by
// one result naming several, or named again by a later search go in one
// item after the last of those results, once each.
func TestLoadedToolsInOneItem(t *testing.T) {
body := tsRequest([]string{tsTools0, tsReadA, tsReadB, tsPlaceholder}, tsTurn1,
`{"role":"assistant","content":[
{"type":"tool_use","id":"toolu_01","name":"ToolSearch","input":{"query":"select:mcp__probe__read_a,mcp__probe__read_b","max_results":5}},
{"type":"tool_use","id":"toolu_02","name":"ToolSearch","input":{"query":"read b","max_results":5}}]}`,
`{"role":"user","content":[
{"type":"tool_result","tool_use_id":"toolu_01","content":[{"type":"tool_reference","tool_name":"mcp__probe__read_a"},{"type":"tool_reference","tool_name":"mcp__probe__read_b"}]},
{"type":"tool_result","tool_use_id":"toolu_02","content":[{"type":"tool_reference","tool_name":"mcp__probe__read_b"}]}]}`,
`{"role":"assistant","content":[{"type":"tool_use","id":"toolu_03","name":"ToolSearch","input":{"query":"select:mcp__probe__read_a","max_results":1}}]}`,
`{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_03","content":[{"type":"tool_reference","tool_name":"mcp__probe__read_a"}]},{"type":"text","text":"go on"}]}`)
o := tsBuild(t, body, true)
var kinds []string
for _, it := range o.Get("input").Array() {
k := it.Get("type").String()
if k == "additional_tools" {
k += ":" + strings.Join(tsNames(it.Get("tools")), "+")
}
kinds = append(kinds, k)
}
want := "message,function_call,function_call,function_call_output,function_call_output,additional_tools:mcp__probe__read_a+mcp__probe__read_b,function_call,function_call_output,message"
if strings.Join(kinds, ",") != want {
t.Fatalf("input = %s\nwant %s", strings.Join(kinds, ","), want)
}
if got := tsNames(o.Get("tools")); strings.Join(got, ",") != "Bash,ToolSearch" {
t.Fatalf("tools = %v", got)
}
}
// TestLoadedToolsWhereNotTaken: where the item isn't taken the request is
// built as before #2660: every tool top-level, no additional_tools. So is a
// deferred tool no result in the history loaded (a compacted history), and
// a tool a reference names that wasn't deferred.
func TestLoadedToolsWhereNotTaken(t *testing.T) {
for i, body := range tsRequests {
o := tsBuild(t, body, false)
if strings.Contains(o.Raw, "additional_tools") {
t.Fatalf("request %d: %s", i+1, o.Raw)
}
want := []string{"Bash", "ToolSearch", "mcp__probe__read_a", "mcp__probe__read_b"}[:2+i]
if got := tsNames(o.Get("tools")); !reflect.DeepEqual(got, want) {
t.Fatalf("request %d's tools = %v, want %v", i+1, got, want)
}
}
compacted := tsRequest([]string{tsTools0, tsReadA, tsPlaceholder}, `{"role":"user","content":"summary of what was done"}`)
o := tsBuild(t, compacted, true)
if strings.Contains(o.Raw, "additional_tools") || strings.Join(tsNames(o.Get("tools")), ",") != "Bash,ToolSearch,mcp__probe__read_a" {
t.Fatalf("compacted: %s", o.Raw)
}
notDeferred := tsRequest([]string{tsTools0, `{"name":"mcp__probe__read_a","input_schema":{"type":"object"}}`}, tsTurn1, tsSearchA)
o = tsBuild(t, notDeferred, true)
if strings.Contains(o.Raw, "additional_tools") || len(o.Get("tools").Array()) != 3 {
t.Fatalf("not deferred: %s", o.Raw)
}
}
// TestLoadsInPlaceScope: OpenAI's own API and a ChatGPT (Codex) account
// are sent the item (#4738), and a provider the user said takes it; xAI, a
// relay, another vendor's sign-in, Azure and a plugin aren't, nor one the
// user turned it off for.
func TestLoadsInPlaceScope(t *testing.T) {
for _, c := range []struct {
p provider.Provider
want bool
}{
{provider.Provider{ID: "openai", Responses: "https://api.openai.com/v1"}, true},
{provider.Provider{ID: "openai-eu", Responses: "https://eu.api.openai.com/v1"}, true},
{provider.Provider{ID: "lookalike", Responses: "https://api.openai.com.example.net/v1"}, false},
{provider.Provider{ID: "xai", Responses: "https://api.x.ai/v1"}, false},
{provider.Provider{ID: "relay", Responses: "https://relay.example.com/v1"}, false},
{provider.Provider{ID: "relay", Responses: "https://relay.example.com/v1", AdditionalTools: true}, true},
{provider.Provider{ID: "codex", Responses: "https://chatgpt.com/backend-api/codex", Account: &provider.Account{Agent: "codex"}}, true},
{provider.Provider{ID: "codex", Responses: "https://chatgpt.com/backend-api/codex", Account: &provider.Account{Agent: "codex"}, AdditionalToolsOff: true}, false},
{provider.Provider{ID: "openai", Responses: "https://api.openai.com/v1", AdditionalToolsOff: true}, false},
{provider.Provider{ID: "copilot", Responses: "https://api.githubcopilot.com", Account: &provider.Account{Agent: "copilot"}}, false},
{provider.Provider{ID: "copilot", Responses: "https://api.githubcopilot.com", Account: &provider.Account{Agent: "copilot"}, AdditionalTools: true}, true},
{provider.Provider{ID: "az", Responses: "https://x.openai.azure.com/openai/v1"}, false},
} {
if got := loadsInPlace(c.p); got != c.want {
t.Errorf("%s %s: %v, want %v", c.p.ID, c.p.Responses, got, c.want)
}
}
}
// TestLoadedToolsThroughTheGateway: a provider set to take the item gets
// it from /v1/messages; one that isn't gets the request as before; one
// that turns the item away is asked again with every tool top-level and
// isn't sent it again.
func TestLoadedToolsThroughTheGateway(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)
ask := func() gjson.Result {
t.Helper()
if code, out := post(t, "/v1/messages", tsRequests[1]); code != 200 {
t.Fatalf("status %d: %s", code, out)
}
return gjson.ParseBytes(f.got)
}
if got := ask(); strings.Contains(got.Raw, "additional_tools") || len(got.Get("tools").Array()) != 3 {
t.Fatalf("not set, the request is as before: %s", got.Raw)
}
p, err := provider.Find("fake")
if err != nil {
t.Fatal(err)
}
p.AdditionalTools = true
if err := provider.Save(*p); err != nil {
t.Fatal(err)
}
got := ask()
if !strings.Contains(got.Raw, `"additional_tools"`) || strings.Join(tsNames(got.Get("tools")), ",") != "Bash,ToolSearch" {
t.Fatalf("set, the loaded tool goes where it was loaded: %s", got.Raw)
}
f.calls = 0
f.refuse = func(b []byte) (int, string) {
if strings.Contains(string(b), `"additional_tools"`) {
return http.StatusBadRequest, `{"error":{"message":"Invalid value: 'additional_tools'. Supported values are: 'message', 'function_call'.","type":"invalid_request_error","param":"input[3].type"}}`
}
return 0, ""
}
srv := New()
for i := range 2 {
if code, out := postTo(t, srv, "/v1/messages", tsRequests[1]); code != 200 {
t.Fatalf("try %d: status %d: %s", i, code, out)
}
got := gjson.ParseBytes(f.got)
if strings.Contains(got.Raw, "additional_tools") || len(got.Get("tools").Array()) != 3 {
t.Fatalf("refused, asked again as before: %s", got.Raw)
}
}
if f.calls != 3 {
t.Fatalf("calls = %d, want the refused one, its retry, and the next without the item", f.calls)
}
}