agent: a used-up ChatGPT account magpie can't serve past leaves the Codex app on its sign-in (xiaoq6285)

xiaoq6285 on Discord (magpie 0.1.2798, the Windows Codex app, one
ChatGPT Plus account, Codex on its own model joined beside the sign-in):
at the five-hour limit the app showed an error and had to be restarted,
then its account menu showed only "magpie" with no quota; at the reset
it restarted by itself and the account came back.

Cause. When the active account was held (used up, no credits), Sync's
own-model takeover made magpie Codex's provider on codex/<model>
whenever magpie's catalog listed that model on any Codex account -- the
same used-up one included. With one account and no fallback every turn
it took over is refused on that account anyway, while the takeover:
- has account/read answer no account, so the menu drops the user, Plus
  and the usage meter until the reset;
- changes the route twice (into it, back at the reset), and
  KeepCodexAppOnItsProvider ends the app's app-server each time, which
  ChatGPT desktop 26.1027.x shows as "ChatGPT 遇到了问题" and relaunches
  (#4550, still open, the app's own reaction to an ended app-server).
Separately, magpie's [model_providers.magpie] requires_openai_auth
follows the account out and back (openaiAuth), and codexAppRoute
counted it even when that table was not Codex's model_provider, so
staying beside the sign-in would still have ended the app-server at
the limit and at the reset.

Change.
- provider.CodexServesWhileOut(model): the Codex subscription has
  fallbacks for codex/<model> (FallbackModels), or another ChatGPT
  account is not known held and not lapsed.
- Sync's own-model takeover runs only when that holds. Otherwise Codex
  stays on its sign-in: the app keeps the account, its quota and reset,
  and shows its own limit message. With a fallback set it moves as
  before (affa979d5 serves the fallback through the gateway too).
- codexAppRoute counts magpie.requires_openai_auth only while
  model_provider is magpie; the table is still written as before.
- docs/subsystems/agent-wiring.md says both.

Tests.
- TestCodexOwnModelOneSpentAccountStaysOnItsSignIn (new): one account,
  no fallback, held: config stays on the sign-in, codexAppRoute and
  AppOut unchanged across two Syncs and the reset.
- TestCodexServesWhileOut (new): one account none; another not known
  out yes; every account out none; a codex fallback yes.
- TestCodexOwnModelGoesThroughMagpieWhileUsedUp and
  TestCodexOwnModelOutThenKeptOnChatGPT now set a codex fallback, the
  case where the takeover still serves something.

Broken by hand: the CodexServesWhileOut gate taken out -> the new agent
test red (Codex moved to model_provider = "magpie", codex/gpt-a);
codexAppRoute counting requires_openai_auth always -> red (route
changed with model_provider empty). Each put back, green.

Run on mote at this head (rebased on e41ef6288), -tags nogui -timeout 45m:
- internal/agent ok (381s), internal/provider ok (254s);
- internal/cli FAIL in TestEveryRouteHasACommandOrIsNamed alone: GET
  /api/context/reply has no command, from 0872bc2a0, filed as #5026,
  nothing of this change;
- gofmt -l clean; go vet of both packages, GOOS=linux go vet -tags
  nogui ./... and GOOS=windows go vet ./... ok.
- internal/gateway ok (1152s) at the earlier base 3088872e6, not run
  again after the rebase (it imports provider; this change adds a
  function there and changes nothing it calls).

Not tried: the real Windows Codex app signed in to a used-up Plus
account (the Windows box's app is not signed in; #4550's crash was not
reproduced there); a real codex app-server against a fake 429 upstream.
Users with a fallback or a second account still get the takeover and
its two app-server restarts, which #4550 tracks.
This commit is contained in:
yetone
2026-11-03 15:46:42 +08:00
parent e41ef6288c
commit f37742e14d
6 changed files with 168 additions and 7 deletions
File diff suppressed because one or more lines are too long
+17 -4
View File
@@ -97,16 +97,29 @@ func codexAppRoute() (route string, out, known bool) {
}
out = a.AppOut != nil && a.AppOut()
var b strings.Builder
var mp string
for _, k := range []string{"model_provider", "openai_base_url"} {
v, _ := edit.GetTOMLTop(a.Path, k)
if k == "model_provider" {
mp = v
}
b.WriteString(k + "=" + v + "\n")
}
t, _ := edit.GetTOMLTable(a.Path, "model_providers."+magpieID)
b.WriteString("magpie.base_url=" + t["base_url"] + "\n")
// and whether it requires OpenAI auth, which decides whether the app
// has its ChatGPT account at all (openaiAuth in codex.go): read as the
// app-server starts, like the rest
b.WriteString("magpie.requires_openai_auth=" + t["requires_openai_auth"] + "\n")
// and, while magpie's table is Codex's provider, whether it requires
// OpenAI auth, which decides whether the app has its ChatGPT account
// at all (openaiAuth in codex.go): read as the app-server starts, like
// the rest. The app-server reads the account of the provider Codex is
// on (codex-rs account_state), so beside the sign-in, on Codex's
// built-in provider, the key follows the account running out and
// coming back (openaiAuth) with nothing for the app to read again:
// counted there, it ended the app's app-server at the limit and again
// at the reset, and the ChatGPT app showed an error and relaunched
// each time (xiaoq6285 on Discord, #4550)
if mp == magpieID {
b.WriteString("magpie.requires_openai_auth=" + t["requires_openai_auth"] + "\n")
}
if out {
b.WriteString("out\n")
}
+7 -2
View File
@@ -1670,8 +1670,13 @@ func codexIn(at place) *Agent {
// below: magpie becomes Codex's provider on that model as
// magpie serves it, the one picked kept (codex.out_own) to go
// back to once the account has room. One magpie serves on no
// account of its own is left as it is.
if m := get("model"); !isMagpie(m) && !api() && chatGPT() && joined() && out() {
// account of its own is left as it is, and so is one magpie
// could serve on that same account alone, with no fallback
// and no other account (provider.CodexServesWhileOut): every
// turn would be refused there too, and the takeover only hid
// the account and its reset and restarted the app twice
// (xiaoq6285 on Discord, one Plus account)
if m := get("model"); !isMagpie(m) && !api() && chatGPT() && joined() && out() && provider.CodexServesWhileOut(m) {
if id := codexOwnViaMagpie(m); id != "" {
stash(map[string]string{at.key("codex.out_own"): m})
return set(id)
+63
View File
@@ -5,6 +5,8 @@ import (
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A ChatGPT account that runs out of its allowance after a magpie model was
@@ -119,6 +121,11 @@ func TestCodexKeptOnChatGPT(t *testing.T) {
// its sign-in.
func TestCodexOwnModelGoesThroughMagpieWhileUsedUp(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "model = \"gpt-a\"\n")
// the Codex subscription falls back to another provider while the
// account is out: magpie has something to serve the turn on
if err := provider.Save(provider.Provider{ID: "codex", Fallback: []string{"fake/m1"}}); err != nil {
t.Fatal(err)
}
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"),
[]byte(`{"models":[{"slug":"gpt-a","display_name":"A","priority":1}]}`), 0o644)
cx := codex(home)
@@ -176,6 +183,58 @@ func TestCodexOwnModelGoesThroughMagpieWhileUsedUp(t *testing.T) {
}
}
// One ChatGPT account, out, on one of Codex's own models joined beside the
// sign-in, and nothing else in magpie to serve that model on: no fallback
// on the Codex subscription and no other account (xiaoq6285 on Discord:
// a Plus account in the Windows Codex app, the menu down to "magpie" with
// no quota after the five-hour limit, the app erroring and restarting at
// the limit and again at the reset). magpie leaves Codex on its sign-in:
// every turn it took over would be refused on that same account, so the
// app keeps the account, its usage and its reset, and nothing restarts.
func TestCodexOwnModelOneSpentAccountStaysOnItsSignIn(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "model = \"gpt-a\"\n")
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"),
[]byte(`{"models":[{"slug":"gpt-a","display_name":"A","priority":1}]}`), 0o644)
cx := codex(home)
if ok, err := cx.Join(); err != nil || !ok {
t.Fatalf("join: %v %v", ok, err)
}
if codexOwnViaMagpie("gpt-a") == "" {
t.Fatal("magpie lists no codex/gpt-a: the test proves nothing")
}
route, _, known := codexAppRoute()
if !known {
t.Fatal("route not known")
}
// the route is what the app's app-server is ended over
// (KeepCodexAppOnItsProvider): unchanged, the app keeps running
same := func(when string) {
t.Helper()
cfg := read()
if !strings.Contains(cfg, `model = "gpt-a"`) || strings.Contains(cfg, "model_provider =") || !strings.Contains(cfg, "openai_base_url") {
t.Fatalf("%s: Codex moved off its sign-in:\n%s", when, cfg)
}
if r, out, _ := codexAppRoute(); r != route || out || cx.AppOut() {
t.Fatalf("%s: the app's app-server would be ended (out %v):\n%s\nwas:\n%s", when, out, r, route)
}
}
codexUsedUp = func() bool { return true }
for _, when := range []string{"used up", "used up, synced again"} {
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
same(when)
}
codexUsedUp = func() bool { return false }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
same("reset")
if !cx.Joined() {
t.Fatal("reset: not joined")
}
}
// Kept on ChatGPT, a used-up account on one of Codex's own models leaves
// Codex as it is: the user chose the sign-in.
func TestCodexOwnModelKeptOnChatGPT(t *testing.T) {
@@ -214,6 +273,10 @@ func TestCodexOwnModelOutThenKeptOnChatGPT(t *testing.T) {
if ok, err := cx.Join(); err != nil || !ok {
t.Fatalf("join: %v %v", ok, err)
}
// a fallback for magpie to serve the turn on (TestCodexOwnModelOneSpentAccountStaysOnItsSignIn)
if err := provider.Save(provider.Provider{ID: "codex", Fallback: []string{"fake/m1"}}); err != nil {
t.Fatal(err)
}
codexUsedUp = func() bool { return true }
t.Cleanup(func() { codexUsedUp = func() bool { return false } })
if err := cx.Sync(); err != nil {
+22
View File
@@ -75,3 +75,25 @@ func CodexMayBeUsedUp(user string) {
c.Unlock()
go codexReadNow(active)
}
// CodexServesWhileOut says magpie has something to serve Codex's own
// model on while the ChatGPT account Codex is signed in to is out: the
// Codex subscription's fallbacks, set in magpie, or another of its
// ChatGPT accounts not known out or lapsed. With neither, every turn
// magpie took over would be refused on that same account, so taking over
// would only hide the account and its reset from the app and restart the
// app's app-server twice (xiaoq6285 on Discord, one Plus account).
func CodexServesWhileOut(model string) bool {
if model == "" {
return false
}
if p, _, ok := Resolve("codex/" + model); ok && p.Account != nil && p.Account.Agent == "codex" && len(FallbackModels(p)) > 0 {
return true
}
for _, l := range Logins("codex") {
if !l.Active && l.Lapsed == "" && !codexHeldKnown(l.User) {
return true
}
}
return false
}
+58
View File
@@ -98,3 +98,61 @@ func TestCodexReadNowSwitchesAtOnce(t *testing.T) {
}
}
}
// Whether magpie has anything to serve Codex's own model on while the
// account Codex is signed in to is out (xiaoq6285 on Discord, one Plus
// account): with that account alone and no fallback, nothing; another
// ChatGPT account not known out, or a fallback on the Codex
// subscription, is something. Another account known out is not.
func TestCodexServesWhileOut(t *testing.T) {
home := signIn(t) // me@example.com
rememberLogins(true)
if CodexServesWhileOut("gpt-5.5") {
t.Fatal("one account, no fallback: serves")
}
if CodexServesWhileOut("") {
t.Fatal("no model: serves")
}
codexSignIn(t, home, "spare@example.com", "r-spare")
rememberLogins(true)
var active, other string
for _, l := range Logins("codex") {
if l.Active {
active = l.User
} else {
other = l.User
}
}
if other == "" {
t.Fatalf("no second account: %+v", Logins("codex"))
}
loginUsageCache.Lock()
oldCache := loginUsageCache.m
loginUsageCache.m = map[string]loginUsageEntry{
"codex/" + active: {at: time.Now(), q: SubscriptionQuota{Provider: "codex", User: active, Held: true}},
}
loginUsageCache.Unlock()
t.Cleanup(func() {
loginUsageCache.Lock()
loginUsageCache.m = oldCache
loginUsageCache.Unlock()
})
if !CodexServesWhileOut("gpt-5.5") {
t.Fatalf("another account (%s), not known out: doesn't serve", other)
}
loginUsageCache.Lock()
loginUsageCache.m["codex/"+other] = loginUsageEntry{at: time.Now(), q: SubscriptionQuota{Provider: "codex", User: other, Held: true}}
loginUsageCache.Unlock()
if CodexServesWhileOut("gpt-5.5") {
t.Fatal("every account out, no fallback: serves")
}
if err := Save(Provider{ID: "fb", Name: "FB", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m"}}); err != nil {
t.Fatal(err)
}
if err := Save(Provider{ID: "codex", Fallback: []string{"fb/m"}}); err != nil {
t.Fatal(err)
}
if !CodexServesWhileOut("gpt-5.5") {
t.Fatal("a fallback on the Codex subscription: doesn't serve")
}
}