provider: Codex's account is read every 30 minutes while Codex sits idle

dludream on Discord: with Codex signed in, magpie asked chatgpt.com every
five minutes, 288 times a day, used or not, and they worry about ban
risk. KeepOnAnAccountWithRoom read the account Codex is signed in to
(/wham/usage, through SwitchWhenSpent, CodexUsedUp and the app-left
restart) on every full look, every loginSwitchEvery.

Now a full look reads Codex's accounts only when codexDue says so:
- while no Codex session file was written in 30 minutes
  (codexSessionsQuiet over CODEX_HOME/sessions) and the account isn't
  near out, the next read is codexIdleLookEvery (30m) later;
- a session written again makes the next full look (5 minutes at most)
  read it;
- near out or held, it is codexNearOutEvery (1m) as before;
- a sessions folder that can't be read counts as written, so the
  cadence stays 5 minutes.
Claude's looks, the proxy sync and noteCodexGoneNow keep their cadence.
Requests magpie sends on the account keep its windows current from the
reply headers (NoteCodexLimits), so routing doesn't lean on the poll.
docs/reference.md says the new cadence.

Run at this tree (origin/main 5c47d59b5):
- mote-run go test -count=1 -timeout 45m ./internal/provider: ok 135.7s.
- gofmt -l internal/: nothing. mote-run GOOS=windows go vet and
  GOOS=linux go vet -tags nogui of ./internal/provider: ok.

Broken by hand: codexEveryAt never idle and codexDue without the
"written again" clause: TestCodexIdleLooksLess red ("idle, not near out:
5m0s false, want 30m0s true"). Restored, green.

Not tried: the loop itself against a real Codex account over hours (the
decision functions are tested, the loop's wiring is read); other readers
of the account (the Usage page, catalog syncs' CodexUsedUp, warm-up)
keep their own cadence; the full go test ./... suite.

Reported-by: dludream (Discord)
This commit is contained in:
yetone
2026-11-10 08:13:08 +08:00
parent 079da3a785
commit 40d39e1176
3 changed files with 113 additions and 6 deletions
+3 -1
View File
@@ -2196,7 +2196,9 @@ which they don't ask again for. Any other 400 is never asked again.
for requests sent through magpie. Claude Code or Codex used on its own
talks to the vendor with the account it is signed in to, and routing
never sees it. For that, magpie looks at the account the agent is signed
in to a minute after it starts and every 5 minutes after: once it has used
in to a minute after it starts and every 5 minutes after (Codex's every
30 minutes while it has written no session for half an hour, read at the
next look once it writes one again; every minute near its cap): once it has used
98% of a window (100% In order), or reached its usage cap, magpie signs
the agent in to the next ticked account, in their order, whose allowance
is known and under that share. A saved Claude account magpie knows
+55
View File
@@ -0,0 +1,55 @@
package provider
import (
"testing"
"time"
)
// TestCodexIdleLooksLess: while Codex has written no session in
// codexIdleAfter, an account not near out is read every
// codexIdleLookEvery, not loginSwitchEvery; near out it is still read
// every codexNearOutEvery; and a session written again makes the next
// full look read it at once.
func TestCodexIdleLooksLess(t *testing.T) {
old := codexIdle
t.Cleanup(func() { codexIdle = old })
idle := true
codexIdle = func(time.Time) bool { return idle }
now := time.Date(2026, 11, 10, 9, 0, 0, 0, time.UTC)
if got, was := codexEveryAt(loginSwitchEvery, now); got != codexIdleLookEvery || !was {
t.Fatalf("idle, not near out: %v %v, want %v true", got, was, codexIdleLookEvery)
}
if got, was := codexEveryAt(codexNearOutEvery, now); got != codexNearOutEvery || was {
t.Fatalf("idle, near out: %v %v, want %v false", got, was, codexNearOutEvery)
}
next := now.Add(codexIdleLookEvery)
if codexDue(now.Add(loginSwitchEvery), next, true) {
t.Fatal("idle still: read again before codexIdleLookEvery")
}
if !codexDue(next, next, true) {
t.Fatal("not read at codexIdleLookEvery")
}
idle = false
if got, was := codexEveryAt(loginSwitchEvery, now); got != loginSwitchEvery || was {
t.Fatalf("in use: %v %v, want %v false", got, was, loginSwitchEvery)
}
if !codexDue(now.Add(loginSwitchEvery), next, true) {
t.Fatal("a session written again doesn't have the next look read the account")
}
if codexDue(now.Add(time.Minute), now.Add(loginSwitchEvery), false) {
t.Fatal("in use: read before its loginSwitchEvery")
}
}
// TestCodexIdleUnreadableIsInUse: a sessions folder that can't be read
// counts as written, so the account is read every loginSwitchEvery.
func TestCodexIdleUnreadableIsInUse(t *testing.T) {
old := codexSessionsQuiet
t.Cleanup(func() { codexSessionsQuiet = old })
codexSessionsQuiet = func(string, time.Time, time.Duration) bool { return false }
if got, was := codexEveryAt(loginSwitchEvery, time.Now()); got != loginSwitchEvery || was {
t.Fatalf("%v %v, want %v false", got, was, loginSwitchEvery)
}
}
+55 -5
View File
@@ -93,6 +93,42 @@ func codexLookEvery(u map[string]SubscriptionQuota) time.Duration {
return loginSwitchEvery
}
// codexIdleAfter is how long no Codex session file has been written for
// Codex to count idle, and codexIdleLookEvery how often the account Codex
// is signed in to is read then, in place of loginSwitchEvery: nothing
// runs on the account, so a reading every five minutes told nothing new
// and asked chatgpt.com 288 times a day (dludream on Discord). Requests
// magpie sends on the account keep its windows current from what each
// reply says (NoteCodexLimits), and a session written to again has the
// next look read it at once (codexDue).
const (
codexIdleAfter = 30 * time.Minute
codexIdleLookEvery = 30 * time.Minute
)
// codexIdle says whether Codex has written no session in codexIdleAfter.
// A var so tests can say.
var codexIdle = func(now time.Time) bool {
return codexSessionsQuiet(codexHome(), now, codexIdleAfter)
}
// codexDue says whether a full look of KeepOnAnAccountWithRoom reads
// Codex's accounts at now: next, when the last look set it, has come, or
// that look found Codex idle (idle) and it no longer is.
func codexDue(now, next time.Time, idle bool) bool {
return !now.Before(next) || idle && !codexIdle(now)
}
// codexEveryAt is how long after a look at now the next reads Codex's
// accounts, given every (codexLookEvery), and whether that is for Codex
// being idle: only an account not near out waits the longer while idle.
func codexEveryAt(every time.Duration, now time.Time) (time.Duration, bool) {
if every >= loginSwitchEvery && codexIdle(now) {
return codexIdleLookEvery, true
}
return every, false
}
// CodexOutUntil is when the ChatGPT account Codex is signed in to has room
// again, as last read: the latest reset of the windows used up. Zero when
// it isn't out or the reading doesn't say.
@@ -619,6 +655,10 @@ func KeepOnAnAccountWithRoom(ctx context.Context) {
}
}()
var full time.Time // when the next full look is due; zero: now
// when a full look next reads Codex's accounts (zero: the first), and
// whether the last that did found Codex idle
var codexNext time.Time
codexWasIdle := false
for {
select {
case <-ctx.Done():
@@ -641,19 +681,29 @@ func KeepOnAnAccountWithRoom(ctx context.Context) {
t.Reset(min(time.Until(full), claudeLookEvery))
continue
}
now := time.Now()
readCodex := codexDue(now, codexNext, codexWasIdle)
for _, agent := range switchedAgents {
if agent == "codex" && !readCodex {
continue
}
lookAtSignIn(ctx, agent)
}
// a sign-in made in Claude Code itself, or a proxy picked since,
// takes its account's proxy too (#1807)
syncClaudeProxyEnvNow()
c, cancel := context.WithTimeout(ctx, time.Minute)
noteCodexUsedUp(CodexUsedUp(c))
restartCodexAppLeftOnSpent(c)
cancel()
if readCodex {
c, cancel := context.WithTimeout(ctx, time.Minute)
noteCodexUsedUp(CodexUsedUp(c))
restartCodexAppLeftOnSpent(c)
cancel()
var codexEvery time.Duration
codexEvery, codexWasIdle = codexEveryAt(codexLookEvery(knownLoginUsage("codex")), time.Now())
codexNext = time.Now().Add(codexEvery)
}
// Codex's own sign-in refused for good, or signed in again since
noteCodexGoneNow()
every := codexLookEvery(knownLoginUsage("codex"))
every := min(loginSwitchEvery, max(time.Until(codexNext), 0))
full = time.Now().Add(every)
signInsLooked(true)
t.Reset(min(every, claudeLookEvery))