gateway: spawn_agent names every model for a Codex an app-server host runs under its own name
@m0eaki on X (magpie 0.1.4159, Codex 0.169.0 through LobeHub's
app-server, /v1/responses): the lead saw only Codex's first five of a
local catalog of 13. `codex app-server` makes the host's
clientInfo.name its originator and the head of its User-Agent (codex-rs
initialize_processor.rs, set_default_originator), so LobeHub's turns say
"lobehub-desktop/0.169.0 (…) (lobehub-desktop; …)", and the gate before
spawnAgentModels took only Codex's own names (fromCodex) or the codex
token.
The class: any host may name Codex's turns after itself (Zed, LobeHub,
other IDEs). Every Responses turn Codex sends, to any provider, carries
x-codex-window-id (and x-codex-turn-metadata): core/src/
responses_metadata.rs compatibility_headers. codexRuns takes those as
Codex's too, beside fromCodex and the codex token, for spawnAgentModels
alone. spawnAgentModels still widens only a body with Codex's own
spawn_agent signature or <model_catalog>, and its added text is the
same neutral line (nothing naming magpie or Codex). spawnChoices at /v1
reads the codex catalog and the key's list, not agentOf, so a host's
lead gets the catalog magpie wrote for Codex. codexForClaudeCode reads
the same headers through codexTurnHeaders now.
codexTiered stays on the old gate (split out of the same if).
Gates that send a request upstream as Codex's own, to a relay that
checks it (codexAsSent, codexNamed's header forwarding, keepTurnAsks),
stay on Codex's own names. The other gates on fromCodex (subagent pick,
resume and follow, hookKind, codexOwnOffMagpie, codexTiered, the events
only Codex reads) are left as they are, filed as #6653.
Test: TestSpawnAgentNamesEveryModelForAnAppServerHost posts at
/v1/responses with the headers Codex 0.169.0 sends under LobeHub
(User-Agent lobehub-desktop/0.169.0 (Mac OS 26.5.0; arm64) unknown
(lobehub-desktop; 2.1.40), originator lobehub-desktop, x-codex-window-id),
a lead on magpie's model, in four shapes (tools, V2 additional_tools,
model_catalog_in_context, tool search; the real 0.162.0 and 0.169.0
fixtures), and checks that the same app name without Codex's turn
headers is left as sent.
Run (on mica, mote's load was 100):
- Broken by hand: the old gate back in place, the four LobeHub
subtests go red, the four without-headers ones pass. Restored, green.
- gofmt -l clean; go vet ./internal/gateway, GOOS=windows and GOOS=linux
go vet ./internal/gateway: clean.
- go test ./internal/gateway -run SpawnAgent -count=1: ok.
- go test ./... -count=1 -timeout 45m at base bc9ea912a: every package
ok (101), internal/gateway 937s, exit 0.
This commit is contained in:
@@ -142,3 +142,76 @@ func TestSpawnAgentNamesEveryModelInCodex0169(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// An app that runs `codex app-server` names Codex's requests after itself:
|
||||
// Codex makes the host's clientInfo.name its originator and the head of
|
||||
// its User-Agent (codex-rs app-server initialize_processor.rs,
|
||||
// set_default_originator). LobeHub's desktop app runs Codex 0.169.0 so,
|
||||
// with magpie as its provider at /v1, and its lead saw only Codex's first
|
||||
// five models of a catalog of 13 (@m0eaki on X): the gate named only
|
||||
// Codex's own names. Any host's Codex turn carries the headers Codex puts
|
||||
// on every turn (x-codex-window-id, x-codex-turn-metadata), and is
|
||||
// widened by them; a request with neither and an app's name is left as
|
||||
// it came.
|
||||
func TestSpawnAgentNamesEveryModelForAnAppServerHost(t *testing.T) {
|
||||
var models []string
|
||||
for i := 1; i <= 12; i++ {
|
||||
models = append(models, fmt.Sprintf("m%d", i))
|
||||
}
|
||||
f := &fake{t: t, reply: sse(`data: {"type":"response.created","response":{"id":"r1"}}`,
|
||||
`data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":9,"output_tokens":2}}}`)}
|
||||
up := setup(t, provider.Responses, f)
|
||||
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Responses: up.URL + "/v1", Models: models}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
post := func(t *testing.T, body []byte, h map[string]string) {
|
||||
t.Helper()
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(string(body)))
|
||||
for k, v := range h {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
New().Handler().ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("%d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
// what Codex 0.169.0 sends under LobeHub's app-server
|
||||
lobehub := map[string]string{
|
||||
"User-Agent": "lobehub-desktop/0.169.0 (Mac OS 26.5.0; arm64) unknown (lobehub-desktop; 2.1.40)",
|
||||
"originator": "lobehub-desktop",
|
||||
"session_id": "019e1a2b-3c4d-7e5f-8a9b-0c1d2e3f4a5b",
|
||||
"x-codex-window-id": "019e1a2b-3c4d-7e5f-8a9b-0c1d2e3f4a5b:0",
|
||||
}
|
||||
shapes := []struct {
|
||||
name, file string
|
||||
listing func(t *testing.T, body []byte) string
|
||||
}{
|
||||
{"spawn_agent in tools", "codex-0.162.0.json", spawnDescription},
|
||||
{"V2, spawn_agent in additional_tools", "codex-0.169.0-v2-lite.json", searchedSpawnDescription},
|
||||
{"model_catalog_in_context", "codex-0.169.0-model-catalog.json", modelCatalogText},
|
||||
{"spawn_agent searched", "codex-0.169.0-tool-search.json", searchedSpawnDescription},
|
||||
}
|
||||
for _, sh := range shapes {
|
||||
b, err := os.ReadFile("testdata/workdir/" + sh.file)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b = withModel(b, "fake/m1")
|
||||
t.Run(sh.name+", LobeHub's Codex", func(t *testing.T) {
|
||||
post(t, b, lobehub)
|
||||
l := sh.listing(t, f.got)
|
||||
for _, m := range models {
|
||||
if !strings.Contains(l, "- `fake/"+m+"`") {
|
||||
t.Fatalf("doesn't name fake/%s:\n%s", m, l)
|
||||
}
|
||||
}
|
||||
})
|
||||
t.Run(sh.name+", an app's request without Codex's turn headers", func(t *testing.T) {
|
||||
post(t, b, map[string]string{"User-Agent": lobehub["User-Agent"], "originator": "lobehub-desktop"})
|
||||
if l := sh.listing(t, f.got); strings.Contains(l, spawnMore) {
|
||||
t.Fatalf("widened:\n%s", l)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ func agentOf(r *http.Request) string {
|
||||
// runs Codex as its own engine and names it after itself (Lody) is
|
||||
// counted as that app.
|
||||
func codexForClaudeCode(h http.Header) bool {
|
||||
if h.Get("X-Codex-Window-Id") == "" && h.Get("X-Codex-Turn-Metadata") == "" {
|
||||
if !codexTurnHeaders(h) {
|
||||
return false
|
||||
}
|
||||
for _, v := range []string{h.Get("User-Agent"), h.Get("Originator")} {
|
||||
@@ -112,6 +112,27 @@ func codexForClaudeCode(h http.Header) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// codexTurnHeaders is whether h carries the headers Codex puts on every
|
||||
// Responses turn it sends, to any provider and whoever runs it
|
||||
// (x-codex-window-id, x-codex-turn-metadata: codex-rs core/src/
|
||||
// responses_metadata.rs compatibility_headers). Its User-Agent and
|
||||
// originator are the name of whatever runs it: `codex app-server` takes
|
||||
// its host's clientInfo.name for both (initialize_processor.rs,
|
||||
// set_default_originator), so LobeHub's is "lobehub-desktop/0.169.0 (…)
|
||||
// (lobehub-desktop; …)" and names no Codex at all.
|
||||
func codexTurnHeaders(h http.Header) bool {
|
||||
return h.Get("X-Codex-Window-Id") != "" || h.Get("X-Codex-Turn-Metadata") != ""
|
||||
}
|
||||
|
||||
// codexRuns is whether r is a turn the Codex binary sent: by its token or
|
||||
// its own names (fromCodex), or, run by an app under the app's name, by
|
||||
// the headers Codex puts on every turn. Only for what Codex alone reads
|
||||
// (its spawn_agent's list): what is sent upstream as Codex's own, to a
|
||||
// relay that checks it (codexAsSent), stays with fromCodex.
|
||||
func codexRuns(r *http.Request) bool {
|
||||
return fromCodex(r.Header) || agentOf(r) == "codex" || codexTurnHeaders(r.Header)
|
||||
}
|
||||
|
||||
// startedByAgent says whether a request is from no agent magpie knows but
|
||||
// from something an agent started with its environment (a hook's script): it
|
||||
// came with the plain gateway token, not one magpie gave an agent, and
|
||||
@@ -2245,11 +2266,14 @@ func (s *Server) handle(from provider.Protocol) http.HandlerFunc {
|
||||
}
|
||||
// Codex with magpie as its provider: spawn_agent names every
|
||||
// model a subagent may be put on, not only Codex's first five
|
||||
// (Gearclickist on Discord)
|
||||
if from == provider.Responses && (fromCodex(r.Header) || agentOf(r) == "codex") {
|
||||
// (Gearclickist on Discord), Codex run by an app under the app's
|
||||
// own name too (LobeHub's app-server, @m0eaki on X)
|
||||
if from == provider.Responses && codexRuns(r) {
|
||||
body = spawnAgentModels(r, body, model, false)
|
||||
// the service tier set for Codex, in place of the one it
|
||||
// asked (codexTiered)
|
||||
}
|
||||
// the service tier set for Codex, in place of the one it asked
|
||||
// (codexTiered)
|
||||
if from == provider.Responses && (fromCodex(r.Header) || agentOf(r) == "codex") {
|
||||
body = codexTiered(r, body, model)
|
||||
}
|
||||
// the service tier the gateway key is set to, over the client's
|
||||
|
||||
Reference in New Issue
Block a user