Files
magpie/internal/provider/codex_idle_look_test.go
T
yetone 40d39e1176 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)
2026-11-10 08:13:08 +08:00

56 lines
2.1 KiB
Go

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)
}
}