From e31384b1f90224c62ef2bae724a3139f7e8b5c93 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 30 Sep 2026 16:55:49 +0800 Subject: [PATCH] =?UTF-8?q?gateway:=20Codex=20on=20magpie=20as=20its=20pro?= =?UTF-8?q?vider=20has=20its=20approvals=20auto-reviewed=20again=20(shihao?= =?UTF-8?q?=20H=20on=20X:=20=E8=87=AA=E5=8A=A8=E5=AE=A1=E6=89=B9=E6=9C=AA?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=20=E2=80=A6=20404:=20magpie=20knows=20no=20m?= =?UTF-8?q?odel=20"codex-auto-review")=20=E2=80=94=20Codex=20asks=20for=20?= =?UTF-8?q?its=20guardian=20review=20by=20OpenAI's=20reviewer=20model,=20c?= =?UTF-8?q?odex-auto-review,=20whatever=20the=20provider=20("independent?= =?UTF-8?q?=20of=20current=20login"),=20and=20magpie,=20knowing=20no=20suc?= =?UTF-8?q?h=20model,=20answered=20404,=20which=20Codex=20reports=20as=20t?= =?UTF-8?q?he=20review=20service=20failing.=20A=20model=20Codex=20names=20?= =?UTF-8?q?that=20magpie=20does=20not=20serve=20now=20goes=20to=20the=20mo?= =?UTF-8?q?del=20Codex=20is=20set=20to=20(config.toml=20model),=20when=20m?= =?UTF-8?q?agpie=20is=20its=20provider,=20as=20Codex=20itself=20reviews=20?= =?UTF-8?q?with=20the=20turn's=20model=20when=20its=20catalog=20lacks=20th?= =?UTF-8?q?e=20reviewer;=20the=20Routing=20view=20shows=20it=20as=20the=20?= =?UTF-8?q?Approval=20check.=20Signed=20in,=20Codex's=20own=20names=20go?= =?UTF-8?q?=20on=20to=20OpenAI=20as=20before.=20TestCodexStandIn=20fails?= =?UTF-8?q?=20without=20the=20change;=20TestCodexAutoReviewStandIn=20has?= =?UTF-8?q?=20the=20Codex=20request=20go=20to=20the=20stand-in.=20go=20vet?= =?UTF-8?q?,=20go=20build=20-tags=20nogui=20(darwin,=20GOOS=3Dlinux),=20GO?= =?UTF-8?q?OS=3Dwindows=20go=20build,=20go=20test=20-tags=20nogui=20./...?= =?UTF-8?q?=20pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/agent/claude.go | 9 ++++--- internal/agent/codex.go | 16 +++++++++++++ internal/agent/standin_test.go | 40 +++++++++++++++++++++++++++++++- internal/gateway/standin_test.go | 24 +++++++++++++++++++ 4 files changed, 85 insertions(+), 4 deletions(-) diff --git a/internal/agent/claude.go b/internal/agent/claude.go index 9fb423cdd..c1d322d81 100644 --- a/internal/agent/claude.go +++ b/internal/agent/claude.go @@ -294,16 +294,19 @@ var claudeManaged = func() string { // StandIn is the model Claude Code is set to use in place of one it named // that magpie doesn't serve: claude-haiku-4-5-… for a title or a small // task goes to its haiku tier's model, and a name of no tier to its main -// model. "" when Claude Code isn't routed through magpie or asked for -// isn't Claude Code. For gateway.StandIn. +// model. For Codex, the model it is set to (codexStandIn). "" when the +// agent isn't routed through magpie or is neither. For gateway.StandIn. func StandIn(agent, model string) string { - if agent != "claude" { + if agent != "claude" && agent != "codex" { return "" } home, err := os.UserHomeDir() if err != nil { return "" } + if agent == "codex" { + return codexStandIn(filepath.Join(home, ".codex", "config.toml")) + } if m := claudeStandIn(filepath.Join(home, ".claude", "settings.json"), model); m != "" || runtime.GOOS != "windows" { return m } diff --git a/internal/agent/codex.go b/internal/agent/codex.go index e0479186e..e048cc048 100644 --- a/internal/agent/codex.go +++ b/internal/agent/codex.go @@ -38,6 +38,22 @@ import ( // again without the table ("Model provider `magpie` not found"), whichever // way Codex is routed now. +// codexStandIn is the model Codex is set to use, when magpie is its +// provider, for a model it names that magpie doesn't serve. Codex asks for +// some by a name of OpenAI's whatever its provider: auto-review of an +// approval goes out as "codex-auto-review" (the guardian's reviewer model, +// "independent of current login"), which magpie answered 404 "magpie knows +// no model", and Codex reported the review as failed. Codex itself reviews +// with the turn's model when its catalog lacks the reviewer; so does magpie. +// Signed in, Codex's own names go on to OpenAI (codexUpstream) instead. +func codexStandIn(path string) string { + if p, _ := edit.GetTOMLTop(path, "model_provider"); p != magpieID { + return "" + } + m, _ := edit.GetTOMLTop(path, "model") + return m +} + func codex(home string) *Agent { return codexIn(here(home)) } // codexIn is Codex as it lives at a place: this machine's home, or a WSL diff --git a/internal/agent/standin_test.go b/internal/agent/standin_test.go index d35b87a55..60d5fdc16 100644 --- a/internal/agent/standin_test.go +++ b/internal/agent/standin_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/yetone/magpie/internal/gateway" + "github.com/yetone/magpie/internal/usage" ) func TestClaudeStandIn(t *testing.T) { @@ -31,7 +32,44 @@ func TestClaudeStandIn(t *testing.T) { if got := claudeStandIn(path, "claude-haiku-4-5"); got != "" { t.Errorf("not routed: %q", got) } - if got := StandIn("codex", "claude-haiku-4-5"); got != "" { + if got := StandIn("gemini", "claude-haiku-4-5"); got != "" { t.Errorf("another agent: %q", got) } } + +// Codex on magpie as its provider asks for its auto-review by OpenAI's +// name, codex-auto-review (shihao H on X: 404 magpie knows no model +// "codex-auto-review"); it goes to the model Codex is set to. Signed in +// (routed by openai_base_url) or not routed, nothing stands in. +func TestCodexStandIn(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + dir := filepath.Join(home, ".codex") + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + write := func(s string) { + if err := os.WriteFile(filepath.Join(dir, "config.toml"), []byte(s), 0o644); err != nil { + t.Fatal(err) + } + } + write("model = \"deepseek/deepseek-v4\"\nmodel_provider = \"magpie\"\n\n[model_providers.magpie]\nname = \"magpie\"\n") + if got := StandIn("codex", "codex-auto-review"); got != "deepseek/deepseek-v4" { + t.Errorf("magpie as provider: %q", got) + } + // the gateway knows the request as Codex's by its User-Agent + for _, ua := range []string{"codex_cli_rs/0.160.0 (Mac OS 26.6.0; arm64) Apple_Terminal/455", "Codex Desktop/0.160.0"} { + if got := usage.AgentOf(ua); got != "codex" { + t.Errorf("%s: agent %q", ua, got) + } + } + write("model = \"gpt-5.5\"\nopenai_base_url = \"" + gateway.URL() + gateway.CodexPath + "\"\n") + if got := StandIn("codex", "codex-auto-review"); got != "" { + t.Errorf("signed in: %q", got) + } + write("model = \"gpt-5.5\"\n") + if got := StandIn("codex", "codex-auto-review"); got != "" { + t.Errorf("not routed: %q", got) + } +} diff --git a/internal/gateway/standin_test.go b/internal/gateway/standin_test.go index 393abefbe..ddfa4c961 100644 --- a/internal/gateway/standin_test.go +++ b/internal/gateway/standin_test.go @@ -1,6 +1,7 @@ package gateway import ( + "net/http/httptest" "strings" "testing" @@ -33,3 +34,26 @@ func TestStandIn(t *testing.T) { t.Fatalf("without a stand-in: %d", code) } } + +// Codex's auto-review asks magpie, its provider, for "codex-auto-review": +// the review goes to the model that stands in (Codex's, TestCodexStandIn). +func TestCodexAutoReviewStandIn(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + f := &fake{t: t, ctype: "application/json", reply: `{"id":"c1","choices":[{"message":{"role":"assistant","content":"OK"},"finish_reason":"stop"}]}`} + setup(t, provider.Chat, f) + StandIn = func(agent, model string) string { + if model == "codex-auto-review" { + return "fake/m1" + } + return "" + } + t.Cleanup(func() { StandIn = nil }) + rec := httptest.NewRecorder() + req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(`{"model":"codex-auto-review","input":"review this","stream":false}`)) + req.Header.Set("User-Agent", "codex_cli_rs/0.160.0 (Mac OS 26.6.0; arm64) Apple_Terminal/455") + req.Header.Set("x-openai-subagent", "guardian") + New().Handler().ServeHTTP(rec, req) + if rec.Code == 404 || modelOf(f.got) != "m1" { + t.Fatalf("auto-review: %d %s, upstream %s", rec.Code, rec.Body.String(), f.got) + } +}