gateway: a conversation that searched its tools compacts and goes on after the search tool is gone, on a Responses vendor that doesn't know tool_search_call (#1171, tcxp001; #1755, FivePink)

What changed: searchAsFunction turned Codex's tool_search_call and
tool_search_output into a function's call and result only while the
request offered the tool_search tool. Codex leaves the tool out once
nothing deferred is left to find, and its compaction of a magpie model
sends no tools at all, but the conversation keeps the earlier searches.
Those went as they came, and a vendor that doesn't know the items
refused the whole request: Anyrouter's "invalid codex request" on the
compaction, Xiaomi MiMo's "input item type 'tool_search_call' is not
supported by this gateway phase" on a later turn. They now go as the
function's call and result whether or not the search is offered, which
is how the same upstream saw them in the turns that offered it. With no
search offered, the tools a search found aren't added to the request's
tools and the reply isn't read as a search (searchTidy stays off). The
ChatGPT backend (a Codex account) still gets the items as Codex sent
them.

The call-id tests for OpenAI and Responses relays (Lullaby) expected a
history-only search sent as tool_search_call with a tsc_ id; it now goes
as a function_call, which has no item id to get wrong. Their ctc_ checks
are unchanged, and the ChatGPT-backend test keeps the tsc_ expectation
(sentToChatGPT).

Run: TestSearchHistoryWithoutTheSearch (compaction through CodexPath and
a later /v1 turn, each upstream refusing with the reporter's own words)
and TestSearchAsFunctionHistoryOnly fail on the old toolsearch.go, the
gateway answering 400 with each reporter's error; they pass with the
fix, with TestToolSearch*, *CallIDs, TestBareReasoning*,
TestAdditionalTools*, TestCodexAsSent*, TestSearchedConversation* and
TestSpawnAgentNamesEveryModelAfterToolSearch. Full go test
./internal/gateway/ below in the push record. gofmt -l clean; go vet
-tags nogui on darwin, linux and windows. Not tried: Anyrouter or MiMo
themselves.
This commit is contained in:
yetone
2026-10-13 14:14:59 +08:00
committed by yetone
parent b68377dae7
commit 9492e5db8a
3 changed files with 145 additions and 11 deletions
+10 -2
View File
@@ -82,7 +82,15 @@ const otherVendorsCalls = `[{"type":"message","role":"user","content":[{"type":"
// sentAsOpenAI is what the upstream must have been given: the calls' ids
// of their own kinds, the call_ids their outputs name them by as they came.
var sentAsOpenAI = []string{`"tsc_18da5b9a10256a3e00000011"`, `"ctc_18da5b9a10256a3e00000012"`, `"ctc_openai"`,
// The search goes as a function's call and result, as it went in the turns
// that offered it, and so with no id of its own (#1171).
var sentAsOpenAI = []string{`"ctc_18da5b9a10256a3e00000012"`, `"ctc_openai"`,
`"call_id":"s1","name":"tool_search","type":"function_call"`, `"call_id":"p1"`, `"call_id":"p2"`, "fix it"}
// sentToChatGPT is what the ChatGPT backend, which runs Codex's search as
// Codex sends it, must have been given: the search's call too with an id of
// its kind.
var sentToChatGPT = []string{`"tsc_18da5b9a10256a3e00000011"`, `"ctc_18da5b9a10256a3e00000012"`, `"ctc_openai"`,
`"call_id":"s1"`, `"call_id":"p1"`, `"call_id":"p2"`, "fix it"}
// The same conversation going on through a magpie model or a routing group
@@ -202,7 +210,7 @@ func TestCodexOwnModelCustomCallIDs(t *testing.T) {
t.Fatalf("%s: %d %s", path, rec.Code, rec.Body)
}
last := got[len(got)-1]
for _, want := range sentAsOpenAI {
for _, want := range sentToChatGPT {
if !strings.Contains(last, want) {
t.Errorf("%s: upstream lacks %s: %s", path, want, last)
}
+26 -9
View File
@@ -14,9 +14,15 @@ import (
// makes to it comes back to Codex as the tool_search_call Codex runs.
// searchAsFunction is a Responses request with Codex's tool search as a
// plain function, and whether there was one.
// plain function, and whether there was one. A request that offers no
// search still has the earlier searches' calls and results in its
// conversation: Codex leaves the tool out once nothing deferred is left
// to find, and its compaction sends no tools at all. Those go as a
// function's too, the tools they found left out, or a vendor that knows
// no tool_search_call turns the whole request away (#1171 tcxp001, the
// compaction on Anyrouter; #1755 FivePink, Xiaomi MiMo).
func searchAsFunction(body []byte) ([]byte, bool) {
if !bytes.Contains(body, []byte(`"tool_search"`)) {
if !bytes.Contains(body, []byte(`"tool_search`)) {
return body, false
}
dec := json.NewDecoder(bytes.NewReader(body))
@@ -33,21 +39,27 @@ func searchAsFunction(body []byte) ([]byte, bool) {
search = true
}
}
if !search {
return body, false
}
// a tool found is added once; a namespace found again (another search
// in the same MCP server) adds the tools it didn't have
// in the same MCP server) adds the tools it didn't have. With no search
// offered, nothing is added: the request's tools are what Codex offers
// now, and a compaction's are none.
have := toolsByName(tools)
add := func(tm map[string]any) { tools = addTool(tools, have, tm) }
add := func(tm map[string]any) {
if search {
tools = addTool(tools, have, tm)
}
}
history := false
if items, ok := m["input"].([]any); ok {
for i, it := range items {
im, _ := it.(map[string]any)
switch im["type"] {
case "tool_search_call":
history = true
args, _ := json.Marshal(im["arguments"])
items[i] = map[string]any{"type": "function_call", "call_id": im["call_id"], "name": toolSearch, "arguments": string(args)}
case "tool_search_output":
history = true
found, _ := im["tools"].([]any)
var named []rTool
for _, t := range found {
@@ -66,12 +78,17 @@ func searchAsFunction(body []byte) ([]byte, bool) {
}
}
}
m["tools"] = tools
if !search && !history {
return body, false
}
if search {
m["tools"] = tools
}
out, err := marshalPlain(m)
if err != nil {
return body, false
}
return out, true
return out, search
}
// undefer drops defer_loading from a tool a search found, and from a
+109
View File
@@ -0,0 +1,109 @@
package gateway
import (
"bytes"
"encoding/json"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A conversation that searched keeps the search's call and result after
// the search tool is gone: Codex offers tool_search only while a deferred
// tool is left to find, and its compaction of a magpie model goes with no
// tools at all. A Responses vendor that doesn't know the items refused the
// whole request. Each refusal is the reporter's, word for word: Anyrouter
// on the compaction (#1171, tcxp001), Xiaomi MiMo on a later turn (#1755,
// FivePink). The items go as a function's call and result, the tools the
// search found aren't offered again, and the reply isn't read as a search.
func TestSearchHistoryWithoutTheSearch(t *testing.T) {
refusals := map[string]string{
"compaction": `{"error":{"code":null,"message":"invalid codex request","param":null,"type":"invalid_request_error"}}`,
"later turn": `{"error":{"message":"input item type 'tool_search_call' is not supported by this gateway phase","type":"invalid_request_error","param":null,"code":null}}`,
}
withoutSearch := `"tools":[{"type":"function","name":"exec_command","parameters":{"type":"object"}}]`
for _, tc := range []struct{ name, body string }{
{"compaction", `{"model":"fake/m1","stream":true,` + withoutSearch + `,` + strings.TrimSuffix(searchInput, "]") + `,{"type":"compaction_trigger"}]}`},
{"later turn", `{"model":"fake/m1","stream":true,` + withoutSearch + `,` + searchInput + `}`},
} {
t.Run(tc.name, func(t *testing.T) {
f := &fake{t: t, reply: sse(
`data: {"type":"response.created","response":{"id":"r1"}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","content":[{"type":"output_text","text":"SUMMARY"}]}}`,
`data: {"type":"response.output_item.done","output_index":1,"item":{"type":"function_call","id":"fc_1","call_id":"c9","name":"tool_search","arguments":"{}","status":"completed"}}`,
`data: {"type":"response.completed","response":{"id":"r1","output":[],"usage":{"input_tokens":5,"output_tokens":2}}}`)}
f.refuse = func(b []byte) (int, string) {
if bytes.Contains(b, []byte(`"tool_search_call"`)) || bytes.Contains(b, []byte(`"tool_search_output"`)) {
return 400, refusals[tc.name]
}
return 0, ""
}
setup(t, provider.Responses, f)
var code int
var body string
if tc.name == "compaction" {
rec := codexPostTo(New(), tc.body)
code, body = rec.Code, rec.Body.String()
} else {
code, body = post(t, "/v1/responses", tc.body)
}
if code != 200 || f.calls == 0 {
t.Fatalf("%d %s", code, body)
}
var q struct {
Tools []rTool `json:"tools"`
Input []struct {
Type string `json:"type"`
CallID string `json:"call_id"`
Name string `json:"name"`
Arguments string `json:"arguments"`
Output string `json:"output"`
} `json:"input"`
}
if err := json.Unmarshal(f.got, &q); err != nil {
t.Fatal(err)
}
for _, rt := range q.Tools {
if rt.Name == "tool_search" || rt.Name == "mcp__cal" {
t.Errorf("offered %s, which Codex didn't: %s", rt.Name, f.got)
}
}
if in := q.Input[1]; in.Type != "function_call" || in.CallID != "s1" || in.Name != "tool_search" || !strings.Contains(in.Arguments, "calendar create") {
t.Fatalf("search call = %+v", in)
}
if in := q.Input[2]; in.Type != "function_call_output" || in.CallID != "s1" || !strings.Contains(in.Output, "mcp__cal__create_event") {
t.Fatalf("search output = %+v", in)
}
if in := q.Input[3]; in.Type != "function_call" || in.CallID != "c1" || in.Name != "create_event" {
t.Fatalf("the found tool's call changed: %+v", in)
}
if strings.Contains(body, `"tool_search_call"`) {
t.Errorf("a function the model called came back as a search: %s", body)
}
})
}
}
// Unit: history alone is changed, the tools are left as they came, and it
// doesn't say the search was offered.
func TestSearchAsFunctionHistoryOnly(t *testing.T) {
for _, tools := range []string{``, `"tools":[],`, `"tools":null,`, `"tools":[{"type":"function","name":"exec_command"}],`} {
in := `{"model":"m",` + tools + searchInput + `}`
out, ok := searchAsFunction([]byte(in))
if ok {
t.Errorf("%s: says the search was offered", tools)
}
if bytes.Contains(out, []byte(`"tool_search_`)) {
t.Errorf("%s: history left: %s", tools, out)
}
var a, b map[string]any
json.Unmarshal([]byte(in), &a)
json.Unmarshal(out, &b)
ta, _ := json.Marshal(a["tools"])
tb, _ := json.Marshal(b["tools"])
if string(ta) != string(tb) {
t.Errorf("tools %s became %s", ta, tb)
}
}
}