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

102 lines
3.3 KiB
Go

package provider
import (
"context"
"log"
"strings"
"sync"
"sync/atomic"
"time"
)
// The Codex app greys its Send for every model, magpie's included, while
// the ChatGPT account it is on is out (MasterYi on Discord), till magpie is
// its provider and its app-server restarted (codex_app_server.go). What
// follows tells the account is out as soon as a reply says so, not at the
// next look five minutes on.
// codexLiveChecks counts the running loops that keep Codex on an account
// with room (KeepOnAnAccountWithRoom) past their first reading: while one
// does, a reply that says Codex's account is out has it read again at
// once (CodexMayBeUsedUp). A test's gateway, gone within the minute
// before that first reading, never turns it on, so tests never ask the
// vendor.
var codexLiveChecks atomic.Int32
// codexRecheckEvery is the least time between two such readings.
var codexRecheckEvery = 20 * time.Second
var codexRecheck struct {
sync.Mutex
at time.Time
}
// codexReadNow reads whether Codex's account is out, and signs Codex in
// to the next account with room when it is, as KeepOnAnAccountWithRoom's
// look does: the Codex app greys its Send once its account is out, so no
// request comes to tell magpie again, and that look could be five minutes
// off (danzi002 on Discord restarted the app each time instead). A var
// for tests.
var codexReadNow = func(user string) {
StaleAllowance("codex", user)
// what the sync last went by, before this read changes it
known := CodexUsedUpKnown()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
if _, err := SwitchWhenSpent(ctx, "codex"); err != nil {
log.Printf("codex: switching to an account with room: %v", err)
}
noteCodexUsedUp(known, CodexUsedUp(ctx))
}
// CodexMayBeUsedUp is a reply's word that user's ChatGPT account may be
// out (usage_limit_reached, or a window heard at 100%): for the account
// Codex is signed in to, its allowance is read again at once, at most
// every codexRecheckEvery, and a change told as the five-minute look tells
// it (noteCodexUsedUp).
func CodexMayBeUsedUp(user string) {
if codexLiveChecks.Load() <= 0 || user == "" {
return
}
active := ""
for _, l := range Logins("codex") {
if l.Active && strings.EqualFold(l.User, user) {
active = l.User
}
}
if active == "" {
return
}
c := &codexRecheck
c.Lock()
if time.Since(c.at) < codexRecheckEvery {
c.Unlock()
return
}
c.at = time.Now()
c.Unlock()
go codexReadNow(active)
}
// CodexServesWhileOut says magpie has something to serve Codex's own
// model on while the ChatGPT account Codex is signed in to is out: the
// Codex subscription's fallbacks, set in magpie, or another of its
// ChatGPT accounts not known out or lapsed. With neither, every turn
// magpie took over would be refused on that same account, so taking over
// would only hide the account and its reset from the app and restart the
// app's app-server twice (xiaoq6285 on Discord, one Plus account).
func CodexServesWhileOut(model string) bool {
if model == "" {
return false
}
if p, _, ok := Resolve("codex/" + model); ok && p.Account != nil && p.Account.Agent == "codex" && len(FallbackModels(p)) > 0 {
return true
}
for _, l := range Logins("codex") {
if !l.Active && l.Lapsed == "" && !codexHeldKnown(l.User) {
return true
}
}
return false
}