Commit Graph
3 Commits
Author SHA1 Message Date
yetone 00afcf97a1 provider, gui, cli: a Codex account's auto-use of its resets is two switches, its week used up and one about to expire (#2078, alanwalk)
alanwalk asked for "自动使用重置卡" to be split: they want a reset that is
about to expire used rather than lost, but a used-up week waited out rather
than spending a reset on it (and the other way round for others).

What changed:
- settings: codexAutoResetOnly narrows an account in codexAutoReset to
  "week" or "expiring". An account with no entry keeps both, so every
  existing config means what it meant: nothing to migrate, and no user
  value is rewritten. Load drops entries for accounts not in the list or
  with an unknown value.
- provider: CodexAutoResetWhen(user) reads both times. AutoUseCodexReset
  (the week used up, called from the gateway's autoReset and
  autoResetSignedIn through AutoResetsWeek) only acts when the week is on;
  SpendExpiringCodexResets and routing's resetRunsOut only count on the
  expiring reset when that is on. SetCodexAutoResetCase(user, when, on)
  sets one time and keeps the other; SetCodexAutoReset still sets both.
  Both off removes the account.
- gateway: POST /api/settings/codex-auto-reset takes an optional "when";
  POST /api/settings keeps codexAutoResetOnly, which the page doesn't send.
- GUI: the account's card (Usage page and the menu bar panel) says
  "Auto-use resets:" with each time on its own line and its own switch;
  the brief account's one switch still sets both, its title naming each
  time; Settings › Usage gives each account an Off/On per time. The status
  line says which times are on now. "· auto-used before it expires" on
  the resets held follows the expiring switch alone. Strings in zh, zh-TW,
  ja and de.
- CLI/TUI: `magpie quota auto-reset <account> week|expiring on|off`; the
  quota and accounts tables and the TUI say "auto", "auto when the week
  runs out" or "auto before one expires".

What was run (worktree on origin/main 2fbbd1694):
- gofmt -l on the changed Go files: clean. go vet ./... (darwin),
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test -tags nogui ./internal/provider ./internal/gateway ./internal/cli
  ./internal/tui ./internal/gui ./internal/settings: ok. The gateway
  package hit go test's 10m default once while ~15 sibling sessions were
  building on this machine (no test failed; TestRemoteMagpieModels was
  merely running); with -timeout 30m it passed in 967s.
- New Go tests: TestCodexAutoResetCases (provider), a week-only step in
  TestSpendExpiringCodexResets, an "only before one expires" case in
  TestCodexAutoResetNot (gateway), CodexAutoResetOnly in
  TestSettingsSaveKeepsWhatItDoesNotSend. Broken by hand: making
  CodexAutoResetWhen ignore the narrowing turned all three reset tests
  red (the gateway case answered 200, a week-only account read 2 and
  spent 1); dropping the keep line in api.go turned the settings test red.
- GUI (Playwright, chromium and webkit): codex-auto-reset.test.cjs, now in
  en, zh, zh-TW, ja and de at 1100 and 420 wide, flips each time's switch
  on the card and the panel and checks posts carry "when", the other time
  is kept, the status, the resets-held line, the brief switch, fit at 420,
  no left border, no select; codex-auto-reset-settings.test.cjs does the
  same for Settings › Usage. Both, with codex-resets-used, routing-auto-reset,
  quota-curve (its class list now holds ar-split) and i18n-keys: 56 + 18
  pass. Broken by hand: posting without "when" turned 20 of 21 red.
  quota-curve failed 2 of 18 once while a go test build ran beside it; it
  passed 15/15 serially and 18/18 again at concurrency 4 just after.

Not tried: a real ChatGPT account using or keeping a reset; the times are
driven with the faked usage reads the existing tests use.
2026-10-14 16:37:59 +08:00
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