codex: a searched conversation under Codex's web.run isn't sent the hosted web_search beside it (Savin on Discord)

Savin (#help, magpie 0.1.1296, Codex 0.163.0-alpha.10.2, signed in with
ChatGPT): the turn failed with the ChatGPT backend's "a single request
can't contain both a client-declared function web.run and the hosted tool
web_search".

Who adds which:
- web.run is Codex's own. codex-rs/ext/web-search (tool.rs) declares it
  as a function "run" in the "web" namespace, and spec_plan.rs's
  hosted_model_tool_specs leaves the hosted web_search out whenever
  web.run is registered: on a Responses Lite model (whose requests take
  no hosted tools at all), or with the standalone_web_search feature on.
  Codex never sends both itself.
- The hosted web_search was magpie's. provider.DeclareSearch (#1270)
  adds the cached-only {"type":"web_search","external_web_access":false}
  to any ChatGPT-bound request whose input replays a web_search_call
  without declaring search. declaresSearch looked only for a web_search*
  type, so a conversation that had searched with the hosted tool and
  went on under web.run (a Lite model, a newer Codex, or magpie's own
  searcher's web_search_call items) got web_search added beside web.run,
  in the additional_tools item for Lite or in the tools. The backend then
  refused the turn.

What changed: declaresSearch counts web.run as the conversation's search.
It matches the namespace "web" holding a function "run", and a function
named "web.run". DeclareSearch then leaves the body as Codex sent it.
This covers both routes that call it: codexBody (a ChatGPT account in
magpie) and codexUpstreamOn (Codex's own sign-in relayed). Nothing the
client declared is dropped. web.run is the tool the backend serves this
client (through /alpha/search), so magpie no longer adds the tool that
conflicts with it. A namespace other than "web" with a "run" function
still gets web_search declared.

Not verified: whether the ChatGPT backend accepts a replayed
web_search_call under web.run with no hosted tool. That is the request
the official Codex client sends to it directly. It was not probed on a
real account, and the test fake assumes it is accepted.

Tests:
- TestDeclareSearch: new cases cover web.run in Lite's additional_tools,
  web.run among the tools, the flat "web.run" function, and another
  namespace's run.
- TestSearchedConversationUnderWebRun: protectedSearch now refuses a
  request that carries both tools, with Savin's error. The test covers
  Lite on Codex's sign-in (relayed byte for byte) and two turns on a
  ChatGPT account (Lite and tools).
- On ba319a758 without the codex_request.go change, both tests fail.
  The gateway test gets the 400 with Savin's error. With the change,
  both pass, along with TestSearchedConversation* and
  TestUnsearchedConversationUnchanged.

docs/subsystems/gateway-routing.md says it next to #1270.

Run:
- gofmt -l: clean. go vet ./... passes on darwin, GOOS=linux -tags nogui
  and GOOS=windows -tags nogui.
- `set -o pipefail; go test -timeout 45m ./...` at base ba319a758: every
  package ok except internal/agent and internal/gateway. Both hit the 45m
  package timeout with no test failing. The tests in flight at the panic
  had run 5s and 0s, and four suites from other sessions were running on
  the machine.
- Rerun of those two at -timeout 90m: gateway ok (2057s), agent ok
  (2418s).
- Rebased onto c79400556, whose 4 new commits touch agent, library,
  redact and provider. Rerun there: gofmt, vet on all three OSes,
  ./internal/provider ok, ./internal/redact ok, and
  ./internal/gateway -run 'Search|Codex|WebRun|Protect' ok.
- Rebased again onto e68a0b1db (4 commits in internal/sessions only).
  go vet on sessions, provider and gateway passes there.
This commit is contained in:
yetone
2026-10-21 18:40:51 +08:00
parent e68a0b1db5
commit 049040283e
4 changed files with 101 additions and 8 deletions
File diff suppressed because one or more lines are too long
+65 -4
View File
@@ -25,8 +25,11 @@ const searchedHistory = `{"type":"message","role":"user","content":[{"type":"inp
// history on 2026-10-09: replayed without the web_search tool declared,
// HTTP 200 and only this error event; declared (in the tools, or in an
// additional_tools item), a reply. A Lite request with web_search among
// its top-level tools is refused 400, as the backend refused it. It keeps
// the bodies it was sent.
// its top-level tools is refused 400, as the backend refused it. A request
// declaring both Codex's web.run and the hosted web_search is refused as
// Savin was (Discord, Codex 0.163.0-alpha); web.run alone is taken as the
// conversation's search, as Codex sends it so to the backend itself. It
// keeps the bodies it was sent.
func protectedSearch(t *testing.T) *[][]byte {
t.Helper()
var mu sync.Mutex
@@ -44,14 +47,21 @@ func protectedSearch(t *testing.T) *[][]byte {
Tools []map[string]any `json:"tools"`
}
json.Unmarshal(b, &q)
searched, declared, compact := false, false, false
searched, declared, compact, webRun := false, false, false, false
isWebRun := func(tl map[string]any) bool {
return tl["type"] == "namespace" && tl["name"] == "web"
}
for _, it := range q.Input {
searched = searched || it.Type == "web_search_call"
compact = compact || it.Type == "compaction_trigger"
for _, tl := range it.Tools {
declared = declared || tl["type"] == "web_search"
webRun = webRun || isWebRun(tl)
}
}
for _, tl := range q.Tools {
webRun = webRun || isWebRun(tl)
}
for _, tl := range q.Tools {
if tl["type"] == "web_search" && strings.EqualFold(r.Header.Get("X-OpenAI-Internal-Codex-Responses-Lite"), "true") {
w.WriteHeader(400)
@@ -60,7 +70,12 @@ func protectedSearch(t *testing.T) *[][]byte {
}
declared = declared || tl["type"] == "web_search"
}
if searched && !declared {
if webRun && declared {
w.WriteHeader(400)
io.WriteString(w, `{"error":{"message":"The native ChatGPT backend does not allow a single request to contain both the client-declared function web.run and the hosted tool web_search.","type":"invalid_request_error","param":"tools","code":null}}`)
return
}
if searched && !declared && !webRun {
io.WriteString(w, "event: error\ndata: {\"type\":\"error\",\"error\":{\"message\":\"response protection is unavailable\",\"type\":\"internal_error\",\"param\":null,\"code\":null}}\n\n")
return
}
@@ -195,6 +210,52 @@ func TestSearchedConversationOnCodexSignIn(t *testing.T) {
}
}
// webRunTool is Codex's web.run as it declares it (codex-rs/ext/web-search
// tool.rs): a function in the "web" namespace, which Codex offers in place
// of the hosted web_search on a Responses Lite model or with its standalone
// web search on, and runs through /alpha/search.
const webRunTool = `{"type":"namespace","name":"web","description":"Tools in the web namespace.","tools":[{"type":"function","name":"run","description":"Search the web.","strict":false,"parameters":{"type":"object","properties":{"search_query":{"type":"array"}}}}]}`
// Savin on Discord (Codex 0.163.0-alpha.10.2, signed in with ChatGPT):
// a conversation that had searched with the hosted tool went on under
// Codex's web.run, and magpie declared the hosted web_search for the
// replayed search (#1270) beside it, which the backend refuses. web.run is
// the conversation's search: the turn goes as Codex sent it, on Codex's
// own sign-in and on a ChatGPT account in magpie, Lite or not.
func TestSearchedConversationUnderWebRun(t *testing.T) {
codexSignedIn(t)
got := protectedSearch(t)
lite := `{"model":"gpt-6.1-sol","stream":true,"store":false,"parallel_tool_calls":false,"reasoning":{"effort":"medium","context":"all_turns"},"input":[{"type":"additional_tools","role":"developer","tools":[` + webRunTool + `]},` + searchedHistory + `]}`
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(lite))
req.Header.Set("Authorization", "Bearer chatgpt-token")
req.Header.Set("chatgpt-account-id", "acct-1")
req.Header.Set("X-OpenAI-Internal-Codex-Responses-Lite", "true")
New().Handler().ServeHTTP(rec, req)
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "response.completed") {
t.Fatalf("Lite on Codex's sign-in: status %d: %s", rec.Code, rec.Body.String())
}
if sent := (*got)[len(*got)-1]; string(sent) != lite {
t.Errorf("Lite on Codex's sign-in relayed changed:\n%s\n%s", lite, sent)
}
for _, in := range []string{
`{"model":"codex/gpt-6.1-sol","stream":true,"reasoning":{"effort":"medium","context":"all_turns"},"input":[{"type":"additional_tools","role":"developer","tools":[` + webRunTool + `]},` + searchedHistory + `]}`,
`{"model":"codex/gpt-5.5","stream":true,"tools":[{"type":"function","name":"shell","parameters":{"type":"object","properties":{}}},` + webRunTool + `],"tool_choice":"auto","input":[` + searchedHistory + `]}`,
} {
code, body := post(t, "/v1/responses", in)
if code != 200 || !strings.Contains(body, "response.completed") {
t.Fatalf("on an account: status %d: %s", code, body)
}
tools, additional, _, _ := sentTools(t, (*got)[len(*got)-1])
for _, typ := range append(tools, additional...) {
if typ == "web_search" {
t.Errorf("web_search declared beside web.run: tools %v, additional %v", tools, additional)
}
}
}
}
// Nothing changes for a conversation that didn't search, or one whose
// request declares search already.
func TestUnsearchedConversationUnchanged(t *testing.T) {
+27 -3
View File
@@ -571,13 +571,37 @@ func declareSearch(m map[string]any, lite bool) bool {
return true
}
// declaresSearch says whether tools has a web search among them, by any of
// its names (web_search, web_search_preview, a dated one).
// declaresSearch says whether tools has a web search among them: the
// hosted one by any of its names (web_search, web_search_preview, a dated
// one), or Codex's own web.run, a function in the "web" namespace.
//
// Codex declares web.run in place of the hosted tool, never beside it: on a
// model its catalog puts on Responses Lite, or with its standalone web
// search on, it offers the function and leaves web_search out. The ChatGPT
// backend refuses a request that carries both ("a single request can't
// contain both a client-declared function web.run and the hosted tool
// web_search", Savin on Discord, Codex 0.163.0-alpha), so a conversation
// that replays an earlier web_search_call under web.run is sent as Codex
// sent it, with no hosted tool added: web.run is the search the backend
// serves it, through /alpha/search.
func declaresSearch(tools []any) bool {
for _, raw := range tools {
t, _ := raw.(map[string]any)
if typ, _ := t["type"].(string); strings.HasPrefix(typ, "web_search") {
typ, _ := t["type"].(string)
name, _ := t["name"].(string)
switch {
case strings.HasPrefix(typ, "web_search"):
return true
case typ == "function" && name == "web.run":
return true
case typ == "namespace" && name == "web":
inner, _ := t["tools"].([]any)
for _, raw := range inner {
f, _ := raw.(map[string]any)
if f["type"] == "function" && f["name"] == "run" {
return true
}
}
}
}
return false
+8
View File
@@ -294,6 +294,8 @@ func TestDeclareSearch(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", t.TempDir())
const search = `{"type":"web_search_call","status":"completed","action":{"type":"search","query":"go release"}}`
const user = `{"type":"message","role":"user","content":[{"type":"input_text","text":"go on"}]}`
// web.run as Codex declares it (codex-rs/ext/web-search, tool.rs)
const webRun = `{"type":"namespace","name":"web","description":"Tools in the web namespace.","tools":[{"type":"function","name":"run","description":"Search the web.","strict":false,"parameters":{"type":"object","properties":{"search_query":{"type":"array"}}}}]}`
type sent struct {
Input []struct {
Type string `json:"type"`
@@ -319,6 +321,12 @@ func TestDeclareSearch(t *testing.T) {
{"Lite with no tools item: one first", `{"model":"gpt-6.1-sol","parallel_tool_calls":false,"reasoning":{"effort":"low","context":"all_turns"},"input":[` + search + `,{"type":"compaction_trigger"}]}`, false, "", "web_search", "additional_tools", "none"},
{"declared already", `{"model":"gpt-5.5","tools":[{"type":"web_search_preview"}],"input":[` + search + `]}`, false, "web_search_preview", "", "web_search_call", "auto"},
{"declared in additional_tools", `{"model":"gpt-5.5","input":[{"type":"additional_tools","tools":[{"type":"web_search"}]},` + search + `]}`, false, "", "web_search", "additional_tools", "auto"},
// Codex's web.run is its search: the backend refuses it beside the
// hosted tool (Savin on Discord, Codex 0.163.0-alpha)
{"web.run in Lite's additional_tools", `{"model":"gpt-6.1-sol","reasoning":{"effort":"low","context":"all_turns"},"input":[{"type":"additional_tools","role":"developer","tools":[` + webRun + `]},` + search + `,` + user + `]}`, false, "", "namespace", "additional_tools", "auto"},
{"web.run among the tools", `{"model":"gpt-5.5","tools":[{"type":"function","name":"shell","parameters":{"type":"object"}},` + webRun + `],"input":[` + search + `,` + user + `]}`, false, "function,namespace", "", "web_search_call", "auto"},
{"web.run flat", `{"model":"gpt-5.5","tools":[{"type":"function","name":"web.run","parameters":{"type":"object"}}],"input":[` + search + `]}`, false, "function", "", "web_search_call", "auto"},
{"another namespace's run", `{"model":"gpt-5.5","tools":[{"type":"namespace","name":"functions","tools":[{"type":"function","name":"run","parameters":{"type":"object"}}]}],"input":[` + search + `]}`, false, "namespace,web_search", "", "web_search_call", "auto"},
} {
var got sent
if err := json.Unmarshal(codexBody([]byte(c.body)), &got); err != nil {