diff --git a/docs/reference.md b/docs/reference.md index 6abb2db84..c8d341820 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -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 diff --git a/internal/provider/codex_idle_look_test.go b/internal/provider/codex_idle_look_test.go new file mode 100644 index 000000000..2ca8d7b9a --- /dev/null +++ b/internal/provider/codex_idle_look_test.go @@ -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) + } +} diff --git a/internal/provider/codex_switch.go b/internal/provider/codex_switch.go index 92ec332b2..25080a946 100644 --- a/internal/provider/codex_switch.go +++ b/internal/provider/codex_switch.go @@ -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))