gateway: codex=as-sent holds when Codex offers web_search
A provider set codex=as-sent (#1758) is meant to get Codex's turn as
Codex sent it, its model name alone swapped. But Codex offers web_search
on every turn, and route() translated any request offering it to a model
that doesn't search by itself (searchedForIt) before passthrough's
as-sent branch was ever reached: the developer messages merged into
instructions, apply_patch sent as a function, web_search left out,
max_output_tokens 500000 added. So as-sent never applied to a real
Codex turn, and maralica's relay, which checks requests are Codex's own,
turned them away.
route() now leaves a Responses request relayed when it goes to an
as-sent provider from Codex (codexAsSent, the same test passthrough's
branch uses, now one helper): its web_search goes to the relay as sent.
A provider at the default is translated and searched for as before.
Not changed: spawnAgentModels still widens spawn_agent's description on
an as-sent turn; filed as #3793 (whether as-sent should skip it too).
Test: TestCodexAsSentWebSearch posts the captured Codex 0.162.0 turn
(testdata/workdir/codex-0.162.0.json: developer messages, web_search;
its spawn_agent tool dropped, #3793) to a fake Responses relay: at the
default the relay gets the translation (no web_search, max_output_tokens);
set as-sent, the body is byte-identical to Codex's but for the model id.
Docs: gateway-routing.md's as-sent paragraph.
Broken by hand: the codexAsSent guard in route() off: the test red
("differs from Codex's at byte 2 of 29291": client_metadata first, the
translation). Restored, green.
Run at this commit (worktree, go1.26.8): go test internal/gateway
(whole package, -timeout 45m) ok (464s, rebased on origin/main 6afbe3f30). gofmt -l clean; go vet
gateway+provider darwin/linux/windows clean; builds as before.
Not run: the full ./... suite (machine load). Not tried: maralica's real
relay, or a real Codex against an as-sent relay.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1,9 +1,12 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -72,3 +75,73 @@ func TestCodexAsSent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Codex offers web_search on every turn, and a request offering it to a
|
||||
// model that doesn't search by itself was translated for magpie's search
|
||||
// before anything else: the developer messages merged into instructions,
|
||||
// web_search left out, max_output_tokens added. A relay set codex=as-sent
|
||||
// got that shape all the same (maralica on Discord). It now gets Codex's
|
||||
// turn byte for byte, web_search in it, its model name alone swapped; a
|
||||
// relay left at the default is still translated and searched for.
|
||||
func TestCodexAsSentWebSearch(t *testing.T) {
|
||||
codex, err := os.ReadFile("testdata/workdir/codex-0.162.0.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Contains(codex, []byte(`{"type":"web_search"`)) || !bytes.Contains(codex, []byte(`"role":"developer"`)) {
|
||||
t.Fatal("the fixture is no longer a Codex turn offering web_search")
|
||||
}
|
||||
// spawn_agent's description is widened with the catalog's models on
|
||||
// every Codex turn, as sent or not (spawnAgentModels), so the turn
|
||||
// goes without the multi-agent tools: each other tool is Codex's bytes
|
||||
tools, _, _ := topMember(codex, "tools")
|
||||
var each []json.RawMessage
|
||||
if err := json.Unmarshal(tools, &each); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
each = slices.DeleteFunc(each, func(m json.RawMessage) bool { return bytes.Contains(m, []byte(`"spawn_agent"`)) })
|
||||
kept, err := json.Marshal(each)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
codex = withMembers(codex, map[string][]byte{"tools": kept})
|
||||
asked := bytes.Replace(codex, []byte(`"model":"gpt-5"`), []byte(`"model":"fake/m1"`), 1)
|
||||
want := bytes.Replace(codex, []byte(`"model":"gpt-5"`), []byte(`"model":"m1"`), 1)
|
||||
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() {
|
||||
t.Helper()
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("POST", "/v1/responses", bytes.NewReader(asked))
|
||||
req.Header.Set("User-Agent", "codex_cli_rs/0.162.0 (Mac OS 26.2.0; arm64)")
|
||||
req.Header.Set("originator", "codex_cli_rs")
|
||||
New().Handler().ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("status %d: %s", rec.Code, rec.Body)
|
||||
}
|
||||
}
|
||||
|
||||
ask()
|
||||
if bytes.Contains(f.got, []byte(`{"type":"web_search"`)) || !bytes.Contains(f.got, []byte(`"max_output_tokens"`)) {
|
||||
t.Fatalf("by default the turn is translated for magpie's search: %.400s", f.got)
|
||||
}
|
||||
|
||||
p, err := provider.Find("fake")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p.CodexAsSent = true
|
||||
if err := provider.Save(*p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ask()
|
||||
if !bytes.Equal(f.got, want) {
|
||||
i := 0
|
||||
for i < len(f.got) && i < len(want) && f.got[i] == want[i] {
|
||||
i++
|
||||
}
|
||||
t.Fatalf("as sent, the relay's body differs from Codex's at byte %d of %d (want %d):\ngot …%.200s\nwant …%.200s", i, len(f.got), len(want), f.got[max(0, i-40):], want[max(0, i-40):])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3896,8 +3896,13 @@ func (s *Server) attempt(w http.ResponseWriter, r *http.Request, from provider.P
|
||||
// Chat (#997, Xiaomi MiMo)
|
||||
ownAPI := false
|
||||
// except a subagent's task its lead sealed (#1109): only the server
|
||||
// that sealed it reads it, and a translation would drop it
|
||||
if relay && searchedForIt(p, from, model, body) && !(from == provider.Responses && hasSealedAgentMessage(body)) {
|
||||
// that sealed it reads it, and a translation would drop it; and Codex's
|
||||
// turn to a relay that takes it as Codex sent it (codexAsSent): the
|
||||
// translation merged the developer message into instructions, sent
|
||||
// apply_patch as a function, left web_search out and added
|
||||
// max_output_tokens, the shape such a relay turns away (maralica on
|
||||
// Discord). Its web_search goes to the relay as sent.
|
||||
if relay && searchedForIt(p, from, model, body) && !(from == provider.Responses && (hasSealedAgentMessage(body) || codexAsSent(p, r.Header))) {
|
||||
relay, ownAPI = false, true
|
||||
}
|
||||
// Copilot seals an argument the tools mark "encrypted" (spawn_agent's
|
||||
@@ -4370,6 +4375,13 @@ func fromCodex(in http.Header) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// codexAsSent says a Responses request goes to p as Codex sent it, its
|
||||
// model name alone swapped: Codex's own, to a relay set codex=as-sent,
|
||||
// which checks that a request is Codex's own (#1758).
|
||||
func codexAsSent(p provider.Provider, in http.Header) bool {
|
||||
return p.CodexAsSent && p.Account == nil && fromCodex(in)
|
||||
}
|
||||
|
||||
// codexClientHeader is one of the headers Codex tells itself by to an API
|
||||
// it talks to with a key: its User-Agent and originator, the session and
|
||||
// thread (session-id, thread-id), and what codexHeader lets through. Never
|
||||
@@ -4403,7 +4415,7 @@ func (s *Server) passthrough(w http.ResponseWriter, r *http.Request, p provider.
|
||||
replay := proto == provider.Responses && (replaysReasoning(model, p.Host()) || replaysReasoning(upstream, p.Host()))
|
||||
switch proto {
|
||||
case provider.Responses:
|
||||
if p.CodexAsSent && p.Account == nil && fromCodex(r.Header) {
|
||||
if codexAsSent(p, r.Header) {
|
||||
// as Codex sent it, its model name alone swapped: a relay
|
||||
// that checks it is Codex's own turns away the shape fitted
|
||||
// for other backends (#1758)
|
||||
|
||||
Reference in New Issue
Block a user