Files
magpie/internal/provider/codex_usedup_test.go
T
yetone 369ec0418c provider: a Codex account's hold ends at its window's reset, so Codex back on it shows the account, not "magpie" (davezfr on X)
davezfr on X: Codex signed in to their account 1 showed "magpie" as the
account in the Codex app's settings menu; signed in to account 2 it
showed account 2. It used to show account 1.

Cause. When the account Codex is signed in to is held (out of its
allowance, no credits: the app greys Send), magpie becomes Codex's
provider (codex.out, #540) without requires_openai_auth, and the app
reads no account and shows the provider's name. The sync decides that
from CodexUsedUpKnown, the readings kept in memory (loginUsageCache),
and those were taken as they were made, however old. Account 1 had run
out earlier; its reading said held. Codex signed back in to it after its
window's reset still read as held, so magpie took over and the app
showed "magpie". The reading kept on disk already ended such a hold at
the reset (lastQuotasT.reading); the in-memory one, and the kept reading
the background look gets while allowances are read only when asked,
never did. And the loop's fresh read ("room") told the sync nothing:
noteCodexUsedUp compared it with the loop's last answer, given for
account 2 (also room), not with what the sync had gone by.

Fix.
- holdEnded (quota_last.go) is the one rule: a hold that came with a
  window used up when read ends once that window has started again; a
  hold with no window used up stands till a fresh read. The disk reading
  uses it as before.
- loginUsageEntry.asOf applies it to readings from memory, as of when
  each was made: loginReading (LoginUsage, someLoginUsage, CodexUsedUp,
  the switch), loginReadingKnown (CodexUsedUpKnown, codexHeldKnown) and
  the Usage page's cards (withAccountReadings). A window whose reset had
  passed when it was read doesn't overrule the vendor's allowed false.
- noteCodexUsedUp(known, now) also tells the sync when the fresh read
  differs from what CodexUsedUpKnown said before it, in the loop and in
  codexReadNow.

Users already affected: on the first look after updating, the account's
reading has its hold ended at its reset, and the sync puts Codex back
beside its sign-in; the app shows the account again (after its app-server
restart, as the takeover's end already does). An account that really is
held still has magpie as Codex's provider till its reset: that is the
takeover itself, which the app shows as "magpie".

Tests (red on the old code, at origin/main 142994b21):
- TestCodexHoldEndsAtItsWindowsReset: a held reading from 3h ago whose
  5h window reset a minute ago. Old code: "CodexUsedUpKnown = true on a
  hold whose used-up window started again a minute ago". Also checks
  codexHeldKnown, CodexUsedUp with reads held back, a hold before its
  reset (held) and one with no window used up (held).
- TestCodexUsedUpToldWhenKnownChanges: with `|| known != now` taken out
  by hand it is red ("told 0 times, want 1").
- TestNoteCodexUsedUp takes the new signature, unchanged otherwise.

Run: full suite (go test -timeout 45m ./...) on mica at this change on
142994b21: every package ok but internal/gateway, whose reds were all
TempDir "no space left on device" (mica's disk filled mid-run; the
logged doneHold panic is #6690, recovered by net/http). After rebasing
on b754cb095, on mote: internal/provider ok, internal/agent ok,
internal/gateway ok but TestRefusalAfterShownThinkingGoesToTheNext/
chat_1301_to_responses (#5761, open; -count 10 alone ok). gofmt -l
clean, GOOS=linux go vet -tags nogui ./... ok, GOOS=windows go vet
./... ok. Old code: TestCodexHoldEndsAtItsWindowsReset red on
origin/main; TestCodexUsedUpToldWhenKnownChanges red with
'|| known != now' removed.

Not tried: a real ChatGPT account through a real reset with the Codex
app open; the reporter's account 1 may also simply be out now, which
the app shows as "magpie" till its reset, as before.
2026-11-15 06:06:25 +08:00

28 lines
772 B
Go

package provider
import (
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// The ChatGPT account running out of its allowance, or getting it back, is
// told to the agents' sync (#540), which makes magpie Codex's provider while
// the account is out; the same state again is not.
func TestNoteCodexUsedUp(t *testing.T) {
told := 0
was := catalog.Changed
catalog.Changed = func() { told++ }
t.Cleanup(func() { catalog.Changed = was; codexWasUsedUp.Store(false) })
codexWasUsedUp.Store(false)
for i, c := range []struct {
now bool
told int
}{{false, 0}, {true, 1}, {true, 1}, {false, 2}, {false, 2}} {
noteCodexUsedUp(c.now, c.now)
if told != c.told {
t.Fatalf("step %d (used up %v): told %d times, want %d", i, c.now, told, c.told)
}
}
}