Files
magpie/internal/provider/codex_hold_reset_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

103 lines
3.8 KiB
Go

package provider
import (
"context"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
)
// keepCodexReading puts q in the cache as the reading of the account Codex
// is signed in to (signIn's me@example.com), made at at, as a look hours
// ago left it.
func keepCodexReading(t *testing.T, q SubscriptionQuota, at time.Time) {
t.Helper()
loginUsageCache.Lock()
loginUsageCache.m = map[string]loginUsageEntry{"codex/me@example.com": {at: at, q: q, read: q}}
loginUsageCache.pending = nil
loginUsageCache.Unlock()
t.Cleanup(func() {
loginUsageCache.Lock()
loginUsageCache.m, loginUsageCache.pending = nil, nil
loginUsageCache.Unlock()
})
}
// heldAt is a held reading whose 5h window was used up and starts again
// at reset.
func heldAt(reset time.Time) SubscriptionQuota {
week := reset.Add(5 * 24 * time.Hour)
return SubscriptionQuota{Provider: "codex", Held: true, Windows: []QuotaWindow{
{Name: "5 hours", Used: 100, ResetsAt: &reset},
{Name: "7 days", Used: 63, ResetsAt: &week},
}}
}
// davezfr on X: Codex signed in to account 1 showed "magpie" for its
// account in the Codex app, account 2 its own. Account 1 had run out
// earlier, and the reading magpie kept of it in memory said held, with no
// end: the catalog sync (CodexUsedUpKnown) took the account as still out
// once Codex was on it again, past its window's reset, and made magpie
// Codex's provider for it (codex.out), where the app reads no account. A
// hold that came with a window used up ends when that window starts
// again, as the reading kept on disk already has it (lastQuotasT.reading).
func TestCodexHoldEndsAtItsWindowsReset(t *testing.T) {
signIn(t)
rememberLogins(true)
now := time.Now()
keepCodexReading(t, heldAt(now.Add(-time.Minute)), now.Add(-3*time.Hour))
if CodexUsedUpKnown() {
t.Fatal("CodexUsedUpKnown = true on a hold whose used-up window started again a minute ago")
}
if codexHeldKnown("me@example.com") {
t.Fatal("codexHeldKnown = true on a hold whose used-up window started again")
}
// allowances read only when asked: the background look gets the kept
// reading, as old as it is, and must not take it as held either
setQuotaReads(t, "asked")
t.Cleanup(func() { setQuotaReads(t, "") })
if CodexUsedUp(context.Background()) {
t.Fatal("CodexUsedUp = true, reads held back, on a hold whose window started again")
}
setQuotaReads(t, "")
// before the reset it is held
keepCodexReading(t, heldAt(now.Add(time.Hour)), now.Add(-3*time.Hour))
if !CodexUsedUpKnown() {
t.Fatal("CodexUsedUpKnown = false on a hold whose window starts again in an hour")
}
// a hold with no window used up has no reset to end it: it stands
// till a fresh read (TestCodexHeldWithNoWindowUpKeptTillAFreshRead)
q := heldAt(now.Add(-time.Minute))
q.Windows[0].Used = 40
keepCodexReading(t, q, now.Add(-3*time.Hour))
if !CodexUsedUpKnown() {
t.Fatal("CodexUsedUpKnown = false on a hold with no window used up")
}
}
// The catalog sync goes by what is known (CodexUsedUpKnown). When the
// loop's fresh read changes that, the sync is told, though the loop's own
// last answer was the same: Codex was on another account with room at the
// last look, then signed in to one whose kept reading said held, and the
// sync made magpie its provider for it. The fresh read saying room left
// magpie its provider till something else ran the sync.
func TestCodexUsedUpToldWhenKnownChanges(t *testing.T) {
told := 0
was := catalog.Changed
catalog.Changed = func() { told++ }
t.Cleanup(func() { catalog.Changed = was; codexWasUsedUp.Store(false) })
codexWasUsedUp.Store(false)
// the sync knew held, the loop last said not, the read now says not
noteCodexUsedUp(true, false)
if told != 1 {
t.Fatalf("told %d times, want 1: the sync took the account as out", told)
}
noteCodexUsedUp(false, false)
if told != 1 {
t.Fatalf("told %d times, want still 1", told)
}
}