Commit Graph
2 Commits
Author SHA1 Message Date
yetoneandggbdpq e297a8ab27 provider: a codex-autoreset.json that can't be read no longer lets a Codex account's auto-use spend a second reset the same week (#2017, ggbdpq)
What changed:
- autoResets.load read a codex-autoreset.json that didn't parse (a crash
  leaving it zeroed, as #1505's files were) as "none spent this week",
  so the next week-used-up look spent another reset, and the write after
  it took the unreadable bytes with it. Now a file that can't be read or
  parsed is unknown: the bytes are copied aside as .bad-<ts> (as
  noteResetUsed keeps codex-resets-used.json's), and what was spent is
  rebuilt from codex-resets-used.json, which records every reset used
  for a week used up. When that file can't be read either, no account
  spends one by itself for a week (the longest a lost spend could still
  count), kept past restarts under a "*" entry.
- autoResets.save writes through steady.WriteFile, as every write of
  magpie's own state does since #1505, and logs a failed write.

ggbdpq's patch kept the file aside and made the write durable, but still
read the unreadable file as none spent, so its own scenario (a crash,
then a restart) still spent a second reset. This takes their patch and
fixes the read at the cause.

Run:
- TestAutoResetUnreadableKeptAside (ggbdpq's) fails on main: "the
  unreadable file was written over with nothing kept aside".
- TestAutoResetUnreadableReadsResetsUsed and
  TestAutoResetNothingKnownSpendsNone fail on main and on ggbdpq's patch
  alone: "spent 1 resets, want 0" / "spent 1 resets with nothing known
  of the week". Cases: an auto-use an hour ago holds off; one 8 days
  ago, one by hand, or another account's doesn't.
- go test -tags nogui ./internal/provider under a temp HOME passes;
  -race -count=10 -run TestAutoReset passes; gofmt -l clean; go vet,
  GOOS=linux and GOOS=windows go vet -tags nogui ./internal/provider pass.
- Not tried: a real corrupted file from a user (none reported); a real
  Codex account (checked against the fake ChatGPT server only).

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-10-13 23:30:32 +08:00
yetone d1ec1cdf62 usage: a Codex account can spend one of its rate-limit resets by itself once its week is used up (StringKe on Discord: 账号里有 Bank Reset 的重置次数,可以自动使用 — 七天全部用完的时候重置) — off by default, turned on per account with Auto-use beside Use a reset (Usage page, panel) or magpie quota auto-reset <account> on. It fires only when every other account a request could go to is resting, fresh usage shows the weekly window used up (the 5 hours' being full never counts: a reset starts both, and five hours pass on their own), the account holds one, and none was spent by itself this week (kept in codex-autoreset.json across restarts); it spends the one expiring first, lifts the rest and asks once more, and on nothing_to_reset/no_credit/error ChatGPT's own 429 goes to the agent. Both the pooled accounts and Codex's own sign-in are covered; each use shows on the Routing page and in the request log. TestCodexAutoResetOneAccount, TestCodexAutoResetAfterEveryAccount, TestCodexAutoResetNot, TestWeekUsedUp, TestAutoResetKeptPastRestart, codex-auto-reset.test.cjs and routing-auto-reset.test.cjs are new; the gateway and GUI ones fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; click-scroll, old-webkit, tray-usages pass. 2026-09-30 21:35:40 +08:00