Files
magpie/internal/agent/codex_failover_test.go
T
yetone 9f4e1120f3 agent: a Codex not connected is never pointed at magpie for account failover
TylerBai_loveai on X (tweet 2121532671180632290): with several ChatGPT
accounts added in magpie and Codex not connected, Codex's requests went
through magpie anyway; only adding Codex in Agents, then Disconnect,
put it back.

Cause: Sync's `failover` in codex.go wrote
`openai_base_url = "http://127.0.0.1:3425/backend-api/codex"` (stash
codex.failover), or pointed a CC Switch OpenAI mirror's base_url at the
gateway (codex.mirror_failover), for any Codex signed in to ChatGPT with
another of its accounts on (#1385), on every start and Sync, unless the
user had disconnected Codex or turned on "Only switch Codex's sign-in".
The row said not connected while every request went through magpie.

Fix: failover writes nothing any more. A Codex that isn't wired or
using magpie as its provider gets what failover wrote before taken out
by Sync: the marked base URL, or the mirror's base_url. A base URL the
user wrote by hand has no mark and stays (#856). Failover between Codex
accounts comes with connecting Codex. modelOptions no longer counts
failover as through magpie, so a pick of Codex's own model on the
Agents page doesn't go via magpie. codexProxyOK (the proxy probe only
failover waited for) is gone. The CLI's and GUI's switch-only lines,
reference.md and the subsystem pages say failover is for a connected
Codex.

Users already affected: the first Sync after updating (magpie's start)
removes the marked openai_base_url or mirror base_url, and the route
change restarts Codex's app-server; Codex then goes straight to OpenAI
on its own sign-in. A thread opened while it was on magpie keeps its
provider until reopened, as after Disconnect.

Tests:
- TestCodexNotConnectedStaysOffMagpie: two spare accounts on, Codex
  not connected; SyncCatalog, Sync twice and a model pick leave no
  base URL, no model_provider, not Wired/FailingOver/CodexOwnOnly.
- TestCodexFailoverFromBeforeTakenOut: a marked base URL and a marked
  mirror base_url from before are taken out, bytes restored exactly.
- TestCodexSwitchOnlyKeepsCodexOffTheGateway, the CC Switch mirror,
  disconnect-stays and proxy/EndFailover tests reworked to seed the
  old state by hand; TestCodexFailoverHeldWhileProxyDoesNotPassOn
  deleted with codexProxyOK.

Red on the old code (old codex.go + codex_proxy.go, new tests, mote):
TestCodexNotConnectedStaysOffMagpie, TestCodexFailoverFromBeforeTakenOut
and TestCodexSwitchOnlyKeepsCodexOffTheGateway fail with the reporter's
line `openai_base_url = "http://127.0.0.1:3425/backend-api/codex"`;
TestCodexCCSwitchOfficialOpenAIMirror fails with the mirror pointed at
magpie.

Run (mote; mica's disk is full, 100% with other sessions' runs, and
mote's load was ~17-25 of 18):
- At base b592c1fde: go vet ./... (darwin, GOOS=linux, GOOS=windows)
  ok; go test -timeout 45m ./... 98 ok, two reds:
  TestEveryRouteHasACommandOrIsNamed (fixed on main by 69946f43c) and
  TestCoreScriptBudget, mine (+24 bytes) - the GUI string was shortened
  to fit, no raise.
- Rebased on a63923e3d (what main changed since the base: agent, cli,
  gateway, gui, provider, sessions): gofmt -l clean; go test
  ./internal/agent ./internal/cli ./internal/gui ./internal/provider
  ./internal/sessions all ok. Then rebased on 943404477 (GUI tests
  only below run there).
- GUI (chromium + webkit, every language, both widths): switch-only,
  agent-failover, i18n-keys, i18n-en-no-cjk, claude-switch-only,
  panel-switch-accounts: 115 of 116; the red is claude-switch-only
  chromium 1100 en "the page doesn't move" (scrollTop 3), 4 of 12
  alone here, 0 of 9 on origin/main 943404477, on a path this change
  doesn't touch; filed as #6645 with its output. agent-switch-accounts is 20 of 24
  red the same on a fresh origin/main 69946f43c (#5925, Claude Code
  path, untouched here).

Not tried: a real Codex with real ChatGPT accounts across the update
(sandbox HOME tests only), Codex in WSL, Windows.
2026-11-14 18:40:09 +08:00

225 lines
8.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package agent
import (
"encoding/base64"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
// TylerBai_loveai on X: Codex (the app) signed in to ChatGPT and not
// connected, two more of the user's ChatGPT accounts added and on in
// magpie. magpie pointed Codex at its gateway by itself for account
// failover (#1385), so every Codex request went through magpie while the
// Agents row said not connected, until a Disconnect and cleanup. A Codex
// that isn't connected goes straight to OpenAI: no start, sync or pick of
// one of its own models writes magpie into its config, however many of its
// accounts are on.
func TestCodexNotConnectedStaysOffMagpie(t *testing.T) {
me, _ := json.Marshal(codexChatGPTAuth("me@example.com", "acct-1"))
const own = "model = \"gpt-5.5\"\n"
home, read := codexHome(t, string(me), own)
var saved []map[string]any
for i, u := range []string{"spare1@example.com", "spare2@example.com"} {
saved = append(saved, map[string]any{"agent": "codex", "user": u, "on": true,
"seen": time.Now(), "auth": codexChatGPTAuth(u, "acct-"+string(rune('2'+i)))})
}
b, _ := json.Marshal(saved)
os.MkdirAll(filepath.Dir(provider.Path()), 0o755)
os.WriteFile(filepath.Join(filepath.Dir(provider.Path()), "logins.json"), b, 0o600)
cx := codex(home)
check := func(when string) {
t.Helper()
cfg := read()
if strings.Contains(cfg, "openai_base_url") || strings.Contains(cfg, "model_provider") || strings.Contains(cfg, "127.0.0.1") {
t.Fatalf("%s: an unconnected Codex pointed at magpie:\n%s", when, cfg)
}
if cx.Wired() || cx.FailingOver() || cx.FailoverSaid() != "" || CodexOwnOnly() {
t.Fatalf("%s: wired %v, failing over %v", when, cx.Wired(), cx.FailingOver())
}
if s := stashLoad(); s["codex.failover"] != "" {
t.Fatalf("%s: failover marked: %v", when, s)
}
}
SyncCatalog()
check("magpie's start")
for i := 0; i < 2; i++ {
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
check("sync")
}
if err := cx.Pick("model", "gpt-5.4"); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model = "gpt-5.4"`) {
t.Fatalf("pick not written:\n%s", cfg)
}
check("own model picked")
}
// A Codex a magpie from before pointed at the gateway for failover, the
// reporter's config as they upgrade: the base URL it marked goes as Sync
// next runs, the Codex app's app-server restarted for it like any change of
// route, and it stays gone. So does CC Switch's OpenAI mirror that failover
// pointed at magpie (xuxing0727 on X), back to CC Switch's own bytes.
func TestCodexFailoverFromBeforeTakenOut(t *testing.T) {
me, _ := json.Marshal(codexChatGPTAuth("me@example.com", "acct-1"))
accountsOn := func() {
b, _ := json.Marshal([]map[string]any{{"agent": "codex", "user": "spare@example.com", "on": true,
"seen": time.Now(), "auth": codexChatGPTAuth("spare@example.com", "acct-2")}})
os.MkdirAll(filepath.Dir(provider.Path()), 0o755)
os.WriteFile(filepath.Join(filepath.Dir(provider.Path()), "logins.json"), b, 0o600)
}
home, read := codexHome(t, string(me), "model = \"gpt-5.5\"\n")
accountsOn()
if err := edit.SetTOMLTop(filepath.Join(home, ".codex", "config.toml"), edit.KV{Path: "openai_base_url", Value: here(home).codexURL()}); err != nil {
t.Fatal(err)
}
stash(map[string]string{"codex.failover": "1"})
cx := codex(home)
if !cx.FailingOver() {
t.Fatalf("not the reporter's state:\n%s", read())
}
for i := 0; i < 2; i++ {
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); cfg != "model = \"gpt-5.5\"\n" {
t.Fatalf("sync %d left failover's base URL:\n%s", i, cfg)
}
if cx.FailingOver() || stashLoad()["codex.failover"] != "" {
t.Fatalf("sync %d: failing over %v", i, cx.FailingOver())
}
}
const unified = "model_provider = \"custom\"\nmodel = \"gpt-5.5\"\n\n" +
"[model_providers.custom]\nname = \"OpenAI\"\nrequires_openai_auth = true\nsupports_websockets = true\nwire_api = \"responses\"\n"
home, read = codexHome(t, string(me), unified)
accountsOn()
if err := edit.SetTOMLKey(filepath.Join(home, ".codex", "config.toml"), "model_providers.custom", "base_url", here(home).codexURL()); err != nil {
t.Fatal(err)
}
stash(map[string]string{"codex.mirror_failover": "custom"})
cx = codex(home)
if !cx.FailingOver() {
t.Fatalf("not the mirror's state:\n%s", read())
}
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); cfg != unified || cx.FailingOver() {
t.Fatalf("CC Switch's mirror left on magpie (failing over %v):\n%s", cx.FailingOver(), cfg)
}
}
// A base URL the user wrote in config.toml by hand, to have Codex's own
// models go through magpie, stays when magpie starts (Sync): what failover
// wrote is taken away, and only that (#856: it was gone again after every
// magpie update).
func TestCodexHandWrittenBaseURLStays(t *testing.T) {
base := gateway.URL() + gateway.CodexPath
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`,
"model = \"gpt-5.5\"\nopenai_base_url = \""+base+"\"\n")
cx := codex(home)
for i := 0; i < 2; i++ {
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `openai_base_url = "`+base+`"`) || !strings.Contains(cfg, `model = "gpt-5.5"`) {
t.Fatalf("sync %d took the user's base URL away:\n%s", i, cfg)
}
}
}
// #1385 (shenghsi): Codex signed in to ChatGPT, not connected, with two
// more of the user's ChatGPT accounts on in magpie, pointed at the gateway
// by a magpie from before for failover. Till Sync takes that out, the
// Agents row says it goes through magpie for that, and Codex's model list
// is its own, not its own and 62 of magpie's. Connected, it is magpie's
// models' again.
func TestCodexFailoverOnlyKeepsOwnModels(t *testing.T) {
claims := func(m map[string]any) string {
b, _ := json.Marshal(m)
return "h." + base64.RawURLEncoding.EncodeToString(b) + ".s"
}
auth := func(email, acct string) map[string]any {
return map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{
"id_token": claims(map[string]any{"email": email}),
"access_token": claims(map[string]any{"exp": time.Now().Add(time.Hour).Unix()}),
"refresh_token": "r-" + acct, "account_id": acct}}
}
me, _ := json.Marshal(auth("me@example.com", "acct-1"))
home, read := codexHome(t, string(me), "model = \"gpt-5.5\"\n")
var saved []map[string]any
for i, u := range []string{"spare1@example.com", "spare2@example.com"} {
saved = append(saved, map[string]any{"agent": "codex", "user": u, "on": true,
"seen": time.Now(), "auth": auth(u, "acct-"+string(rune('2'+i)))})
}
b, _ := json.Marshal(saved)
os.MkdirAll(filepath.Dir(provider.Path()), 0o755)
os.WriteFile(filepath.Join(filepath.Dir(provider.Path()), "logins.json"), b, 0o600)
// what a magpie from before wrote for failover
if err := edit.SetTOMLTop(filepath.Join(home, ".codex", "config.toml"), edit.KV{Path: "openai_base_url", Value: here(home).codexURL()}); err != nil {
t.Fatal(err)
}
stash(map[string]string{"codex.failover": "1"})
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, `{"models":[{"slug":"gpt-5.5","priority":1}]}`)
}))
defer up.Close()
wasBase, wasHook := provider.CodexBase, gateway.CodexOwnOnly
provider.CodexBase = up.URL + "/backend-api/codex"
gateway.CodexOwnOnly = CodexOwnOnly
defer func() { provider.CodexBase, gateway.CodexOwnOnly = wasBase, wasHook }()
models := func() []string {
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", gateway.CodexPath+"/models", nil)
req.Header.Set("Authorization", "Bearer chatgpt-token")
gateway.New().Handler().ServeHTTP(rec, req)
var got struct {
Models []map[string]any `json:"models"`
}
json.Unmarshal(rec.Body.Bytes(), &got)
var slugs []string
for _, m := range got.Models {
slug, _ := m["slug"].(string)
slugs = append(slugs, slug)
}
return slugs
}
cx := codex(home)
if cfg := read(); !strings.Contains(cfg, "openai_base_url") {
t.Fatalf("not the reporter's state:\n%s", cfg)
}
if cx.Wired() || !cx.FailingOver() || !CodexOwnOnly() || !strings.Contains(cx.FailoverSaid(), "Providers › Codex") {
t.Errorf("wired %v, failing over %v, own only %v, said %q", cx.Wired(), cx.FailingOver(), CodexOwnOnly(), cx.FailoverSaid())
}
if got := models(); len(got) != 1 || got[0] != "gpt-5.5" {
t.Errorf("failover-only Codex lists %v, want just gpt-5.5", got)
}
// connected, magpie's models are in its list and the row says connected
if ok, err := cx.Join(); !ok || err != nil {
t.Fatalf("join %v %v", ok, err)
}
if !cx.Wired() || cx.FailingOver() || CodexOwnOnly() || cx.FailoverSaid() != "" {
t.Errorf("joined: wired %v, failing over %v, own only %v", cx.Wired(), cx.FailingOver(), CodexOwnOnly())
}
if got := strings.Join(models(), " "); !strings.Contains(got, "fake/m1") {
t.Errorf("joined Codex lists %v, want magpie's too", got)
}
}