codex: a turn stuck on a used-up account no longer holds the Codex app's Send grey after a switch by hand or magpie's takeover (xiaoq6285)

xiaoq6285 on Discord (v0.1.2700): Codex on an official ChatGPT account
ran out of quota, and after switching to another account or provider in
magpie, the Codex app's Send button stayed grey. magpie has no chat
composer of its own; the button is the Codex app's, which greys Send for
every model while the app-server it runs is on a ChatGPT account the app
holds. magpie lifts that by restarting the app's app-server
(codexAppStep), but never while a Codex turn is open in its sessions, for
up to codexTurnWindow (30m). When quota runs out mid-turn the turn is
left open, so the restart, and the grey, waited.

cc81ad4ea let a quiet stuck turn be restarted past (codexTurnStuckAfter,
2m) only after SwitchWhenSpent's own switch. Two siblings that leave the
app-server on the same used-up account still waited the 30 minutes:
- a switch by hand (Accounts, CLI) off an account last read held:
  noteCodexSwitch now marks it spent when the account's reading in hand
  says Held (codexHeldKnown: cache only, no I/O; a failed read or none
  is not held);
- magpie becoming Codex's provider for the used-up account (codex.out):
  codexAppStep marks the change into out spent. The way back at the
  reset still waits for any open turn, as a real one may run there.
A turn still writing its session or sending through magpie in the last
2 minutes holds the restart as before.

Tests, both red on the old code:
- TestCodexAppServerRestartedPastATurnStuckWhenMagpieTakesOver
- TestCodexHandSwitchOffAHeldAccountIsSpent
docs/subsystems/agent-wiring.md says so.

Run at this head on mote:
- go test -tags nogui ./internal/provider: ok (151s), at this head
  after the rebase on 52195b6b0 (it changed provider_test.go and two
  more provider files, so provider was run again).
- Before the rebase (on 07f4d46c5): provider ok, agent ok, gui ok;
  gateway red on TestCodexPoolSaysTheClientsVersion alone (#4807, red in
  a full package run, passes alone; -count=5 alone ok at this change);
  cli red on TestEveryRouteHasACommandOrIsNamed (#4826), red the same
  on origin/main 52195b6b0 just fetched.
- gofmt -l clean; GOOS=linux go vet -tags nogui, GOOS=windows go vet
  and go vet of ./internal/provider ./internal/agent ok at this head.
- Not run: the full go test ./... (agent, gateway, gui, cli and provider
  are what reach this code). No GUI change, so no GUI suite.

Broken by hand, each red, then restored:
- spent not set on the takeover (c.spent && into): the takeover test
  failed "a turn stuck on the used-up account as magpie took over:
  ended [], want [60]".
- noteCodexSwitch never marking spent: the hand-switch test failed
  "switched off out@x: switched true, spent false, want true, true".

Not tried: the real Codex app with an account actually out of quota
(no such account to hand), and Windows/Linux runs of the package (the
change is OS-independent logic; GOOS vet run). Not confirmed with the
reporter that their case is a turn left open; their screenshot wasn't
seen here.
This commit is contained in:
yetone
2026-11-02 18:31:11 +08:00
parent 52195b6b07
commit 9f641daeab
5 changed files with 124 additions and 8 deletions
File diff suppressed because one or more lines are too long
+16 -5
View File
@@ -255,6 +255,9 @@ var codexApp struct {
sync.Mutex
seen bool
route string
// out: whether the route last seen was magpie as Codex's provider for
// its used-up account (CodexAppRoute's out)
out bool
// stale: the app-servers started before Codex's provider last changed,
// each with how often it was ended
stale map[int]int
@@ -263,8 +266,10 @@ var codexApp struct {
// switched: Codex's ChatGPT account changed since the app-servers were
// last listed (codexAppAccountSwitched)
switched bool
// spent: the switch was off an account used up (SwitchWhenSpent), so a
// turn the app-servers left open can't be served by them any more
// spent: the app-servers are on an account used up, so a turn they
// left open can't be served by them any more: Codex was switched off
// it (SwitchWhenSpent, or by hand off one known held, noteCodexSwitch),
// or magpie became Codex's provider for it (out)
spent bool
// ended: the app-servers ended and no new one seen since, and by when
// one has to start (codexAppBackWait after the last end)
@@ -367,8 +372,13 @@ func codexAppStep(ctx context.Context, now time.Time, busy func() (int, time.Tim
// at the first look only a used-up account's app-servers are ended:
// what the others read before magpie started isn't known
changed := !c.seen && out || c.seen && c.route != route || c.switched
// magpie becoming Codex's provider for the used-up account: the
// app-servers are on that account, and a turn left open on it when
// quota ran out mid-turn would hold their restart, and the app's grey
// Send, for up to codexTurnWindow (xiaoq6285 on Discord)
into := changed && out && (!c.seen || !c.out)
if !changed {
c.seen, c.route = true, route
c.seen, c.route, c.out = true, route, out
}
c.Unlock()
if changed {
@@ -384,12 +394,13 @@ func codexAppStep(ctx context.Context, now time.Time, busy func() (int, time.Tim
switch {
case c.switched:
c.why = "Codex's ChatGPT account was switched"
case !c.seen:
case into:
c.why = "Codex's account is used up, so magpie serves it"
default:
c.why = "where Codex's config sends it changed"
}
c.seen, c.route, c.switched = true, route, false
c.spent = c.spent || into
c.seen, c.route, c.out, c.switched = true, route, out, false
c.stale, c.stuck = map[int]int{}, false
for _, p := range pids {
c.stale[p] = 0
@@ -956,6 +956,96 @@ func TestCodexAppServerRestartedPastATurnStuckOnTheSpentAccount(t *testing.T) {
}
}
// When magpie becomes Codex's provider for the used-up account, the Codex
// app's app-server is on that account as a switch off it leaves it: a
// turn left open when quota ran out mid-turn held the restart for up to
// codexTurnWindow, and the app kept Send grey though another provider's
// model was picked (xiaoq6285 on Discord). Past a quiet one it is
// restarted, as after SwitchWhenSpent; a turn still writing, or sending
// through magpie, holds it, and the way back at the reset waits for any
// open turn as before.
func TestCodexAppServerRestartedPastATurnStuckWhenMagpieTakesOver(t *testing.T) {
signIn(t)
s := stubCodexApp(t)
now := time.Now()
quiet := func() (int, time.Time) { return 0, now.Add(-codexTurnStuckAfter - time.Second) }
step := func(busy func() (int, time.Time)) []int { return codexAppStep(context.Background(), now, busy) }
s.running, s.turnOpen = []int{60}, true
if got := step(quiet); got != nil {
t.Fatalf("on its account with room: ended %v", got)
}
s.on = true // the account is out: magpie is Codex's provider
if got := step(func() (int, time.Time) { return 0, now.Add(-time.Minute) }); got != nil {
t.Fatalf("Codex's last request through magpie a minute ago: ended %v", got)
}
s.written = true
if got := step(quiet); got != nil {
t.Fatalf("the turn still writing to its session: ended %v", got)
}
s.written = false
if got := step(quiet); !slices.Equal(got, []int{60}) {
t.Fatalf("a turn stuck on the used-up account as magpie took over: ended %v, want [60]", got)
}
if got := step(quiet); got != nil {
t.Fatalf("the new app-server read magpie as it started: ended %v", got)
}
// the reset: back on the account, whose app-server may run a real turn
s.on = false
if got := step(quiet); got != nil {
t.Fatalf("back on the account with a turn open: ended %v", got)
}
s.turnOpen = false
if got := step(quiet); !slices.Equal(got, []int{100}) {
t.Fatalf("back on the account, idle: ended %v, want [100]", got)
}
}
// A switch by hand in magpie off an account the Codex app holds (used up)
// is one off a used-up account, as SwitchWhenSpent's is: the app-server is
// restarted past a turn left stuck on it (xiaoq6285 on Discord switched
// accounts after Codex said the quota ran out, and Send stayed grey). A
// switch off an account with room, or one never read, waits for an open
// turn as before.
func TestCodexHandSwitchOffAHeldAccountIsSpent(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", t.TempDir())
stubCodexApp(t)
old := listProcesses
listProcesses = func(context.Context) ([]proc.Process, error) { return nil, nil }
loginUsageCache.Lock()
oldCache := loginUsageCache.m
loginUsageCache.m = map[string]loginUsageEntry{
"codex/out@x": {at: time.Now(), q: SubscriptionQuota{Provider: "codex", User: "out@x", Held: true}},
"codex/room@x": {at: time.Now(), q: SubscriptionQuota{Provider: "codex", User: "room@x"}},
"codex/fail@x": {at: time.Now(), q: SubscriptionQuota{Provider: "codex", User: "fail@x", Held: true, Error: "fetch failed"}},
}
loginUsageCache.Unlock()
t.Cleanup(func() {
listProcesses = old
loginUsageCache.Lock()
loginUsageCache.m = oldCache
loginUsageCache.Unlock()
DismissCodexDaemon()
DismissCodexApp()
})
for _, c := range []struct {
from string
spent bool
}{{"out@x", true}, {"OUT@x", true}, {"room@x", false}, {"fail@x", false}, {"never@x", false}} {
codexApp.Lock()
codexApp.switched, codexApp.spent = false, false
codexApp.Unlock()
noteCodexSwitch(c.from, "b@x")
codexApp.Lock()
switched, spent := codexApp.switched, codexApp.spent
codexApp.Unlock()
if !switched || spent != c.spent {
t.Errorf("switched off %s: switched %v, spent %v, want true, %v", c.from, switched, spent, c.spent)
}
}
}
// codexSessionsQuietFor reads the sessions' write times: a file written
// within d is not quiet, an older one or none is, and a sessions folder
// that can't be read is not.
+9 -2
View File
@@ -168,8 +168,15 @@ func noteCodexSwitch(from, to string) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// the Codex app's app-servers are found and restarted on
// KeepCodexAppOnItsProvider's own look, whatever this listing finds
codexAppAccountSwitched()
// KeepCodexAppOnItsProvider's own look, whatever this listing finds.
// A switch by hand off an account known held is one off a used-up
// account, as SwitchWhenSpent's is: a turn left open on it when quota
// ran out mid-turn is restarted past once quiet (xiaoq6285 on Discord)
if codexHeldKnown(from) {
codexAppSpentSwitch()
} else {
codexAppAccountSwitched()
}
ps, err := listProcesses(ctx)
known := err == nil
codexDaemonMu.Lock()
+8
View File
@@ -558,6 +558,14 @@ func codexReadingHeld(u map[string]SubscriptionQuota) bool {
return false
}
// codexHeldKnown is whether user's ChatGPT account was held by the Codex
// app (codexReadingHeld's held) at its last reading, asking no one; false
// when it was never read or the read failed.
func codexHeldKnown(user string) bool {
e, ok := loginReadingKnown(Login{Agent: "codex", User: user})
return ok && e.q.Error == "" && e.q.Held
}
// knownLoginUsage is LoginUsage over the readings already in hand, with
// no reading asked for: an account not read yet is left out, which its
// callers read as "not known".