magpie icon indicating copy to clipboard operation
magpie copied to clipboard

provider: a Codex subscription switched off or removed still has its allowance read and a reset spent

Open JasonBuildAI opened this issue 1 month ago • 3 comments

Impact. A user who has auto-use of Codex resets on for an account and then switches the Codex subscription off in Providers (#163) — or removes the Codex account from magpie — still has magpie read that account's allowance from ChatGPT and, when a reset credit is about to run out unused, spend it. The spend is irreversible: the credit is gone from their ChatGPT account. It is also against the rule the provider subsystem documents, that a subscription switched off (Off) or whose account was removed (Hidden) is left alone in the background, with its allowance not read (docs/subsystems/providers-accounts.md, leftAlone in internal/provider/left_alone.go). 75cbc7e1 (2026-10-14) is what turned that into the rule at every other background site — the CLI identity asks, the allowance reads, RenewLogins, NextLogin, keepWarm, newDue, checkinNow — and did not reach this one.

What happens. internal/gateway/gateway.go starts provider.KeepResetsFromRunningOut, which calls provider.SpendExpiringCodexResets (internal/provider/codex_expiring.go) every five minutes. It walks settings.CodexAutoReset — the accounts the user lets spend their resets — and asks nothing about whether the Codex subscription is off or removed. For each account it reads the windows and the reset credits from ChatGPT (codexWindows), and when one is about to run out within resetExpiryLead (30 minutes) and the account's windows have been used, it POSTs to /backend-api/wham/rate-limit-reset-credits/consume and spends it (useCodexReset).

Minimal repro, main at 98491737 (after v0.1.1238). TestExpiringResetLeftAlone in internal/provider/codex_expiring_test.go: signs a Codex account in against a fake ChatGPT, turns auto-use on for it, checks that the reset about to run out is spent, then writes providers.json with {"id":"codex","off":true} — and again with {"id":"codex","hidden":true} — and calls SpendExpiringCodexResets once more.

Expected: nothing is read and no reset is spent, as in the other background loops (e.g. checkinNow in checkin.go). Actual, on the unmodified main:

--- FAIL: TestExpiringResetLeftAlone (0.59s)
    codex_expiring_test.go:353: switched off: read its allowance 2 more times, spent 1 more of its resets
    codex_expiring_test.go:353: removed from magpie: read its allowance 2 more times, spent 1 more of its resets

Who is affected. Anyone with auto-use of a Codex reset on who then switches Codex off — the state Off is documented for, a key out of quota — or removes the account from magpie while Codex itself stays signed in. Their reset is spent within half an hour of running out, the account's windows start again, and what they had left is lost, whether they still want magpie to act on that account or not. The opt-in to auto-use is the earlier statement of intent; switching the subscription off or removing it from magpie is the later one, and the documented reading of that is that magpie stops acting on it.

Fix. SpendExpiringCodexResets returns at once when leftAlone("codex") — the predicate every other background loop already uses — and docs/subsystems/providers-accounts.md gains this site in its list. A PR with the test and the evidence follows.

JasonBuildAI avatar Oct 14 '26 16:10 JasonBuildAI

Thanks for the precise report and the repro. Confirmed and fixed in 62fc1b4b5: SpendExpiringCodexResets now returns at once when leftAlone("codex") — a Codex switched off or removed from magpie has no allowance read and no reset about to run out spent by the background loop. docs/subsystems/providers-accounts.md lists the site.

Run: TestExpiringResetLeftAlone fails on the old code ("switched off: read its allowance 2 times, spent 1 of its resets", same for removed) and passes with the fix, and the same account switched back on still spends the expiring reset; go test ./internal/provider/ ok; gofmt and go vet for darwin/linux(nogui)/windows clean. Not tried against a real ChatGPT account.

The week-used-up spend (AutoUseCodexReset) is unchanged: it only runs on a request that actually reached Codex, not in the background.

It ships in the next release (after v0.1.1231). Leaving this open until you confirm it.

yetone avatar Oct 14 '26 16:10 yetone

Confirmed against 62fc1b4b - the gate holds and the test guards it. Verified on a detached worktree at 62fc1b4b:

  • go test -count=1 -run TestExpiringResetLeftAlone ./internal/provider: ok github.com/yetone/magpie/internal/provider 4.251s.
  • Break check: with the three added lines in SpendExpiringCodexResets removed by hand, the same test is red with the reported symptom - codex_expiring_test.go:197: switched off: read its allowance 2 times, spent 1 of its resets and the same line for "removed from magpie" (FAIL ... 2.027s). With the gate back, ok. So it fails without the fix and passes with it, and the positive case at codex_expiring_test.go:200-205 ({"id":"codex"}, no off/hidden) still spends the reset running out.
  • Coverage: every loop started at gateway.go:479-502 is held back for a provider that is Off or Hidden - RenewLogins (keepalive.go:58), NextLogin (codex_switch.go:145), LoginUsage (login_usage.go:80), keepWarm (codex_warmup.go:568), the five check-ins (checkin.go:77), newDue (models.go:547), and now this one. KeepPluginsListed stays the one exception the doc names. I found no other site that reads an allowance or spends something for a switched-off or removed Codex.
  • docs/subsystems/providers-accounts.md:175 reads correctly, and both names in it resolve.

Two things that look right to me, for the record: by-hand spends are untouched, since UseCodexReset (internal/cli/quota_cli.go's magpie quota reset, the GUI's reset button) and AutoUseCodexReset reach Codex only on a request that was made, never from the background. And Codex's own-model traffic on a switched-off Codex is still relayed (codex_backend.go), which is the deliberate reading of Off on line 173, so "left alone" here is background activity only - a reader of the doc bullet could take it wider.

I had a PR ready with the same one-line gate plus a TestExpiringResetLeftAlone written against 98491737, where it goes red with switched off: read its allowance 2 more times, spent 1 more of its resets. Since 62fc1b4b is on main and covers it, I am not opening it - it would only duplicate a merged commit.

One small thing I saw while checking, not a request and not worth its own issue: settings.CodexAutoReset is never pruned when an account goes away, so codexUserToken fails once per pass and logs codex reset about to run out: <user>: ... forever. Nothing is read or spent, so it is log noise only, and it predates this fix.

AI disclosure: this report and the verification above were produced with AI assistance at my request, under my direction and review.

JasonBuildAI avatar Oct 14 '26 17:10 JasonBuildAI

Confirmed and closing: 62fc1b4b is an ancestor of v0.1.1242, so the gate is in the release I already run, and TestExpiringResetLeftAlone passes on main at 82f48f09 (Windows 11, Go 1.26.8):

> go test -tags nogui -count=1 -run TestExpiringResetLeftAlone ./internal/provider/
ok  github.com/yetone/magpie/internal/provider  0.453s

One leftover from my check, still true on main, offered rather than assumed: nothing prunes settings.CodexAutoReset when an account is forgotten (ForgetLogin, internal/provider/logins.go:1205), so SpendExpiringCodexResets still walks a user who is no longer signed in and logs codex reset about to run out: <user>: ... (internal/provider/codex_expiring.go:218) once per pass. Log noise only - nothing is read or spent - but if you want it tracked I will write it up on its own.

My branch fix/codex-expiring-reset-left-alone-a13aec is a duplicate of your commit and stays unmerged.

AI disclosure: this verification was produced with AI assistance at my request, under my direction and review.

JasonBuildAI avatar Oct 14 '26 21:10 JasonBuildAI