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) + } +}