Commit Graph
2 Commits
Author SHA1 Message Date
yetoneandggbdpq 96f99b7609 provider: a Codex reset's lost use record is named, not silent (#5556)
noteResetUsed dropped the errors of keeping codex-resets-used.json: a
full disk or a denied path left the spend a success with no record and
no word of it. It says so now, and spendCodexReset names the loss in
the log. The spend stays a success either way: the reset is spent on
the server, so a lost record must never read as one to spend again.

Run (wsl-run, magpie-desktop, Go 1.26.8), with #5555 on top:
- go vet ./internal/provider; gofmt -l internal/provider: clean
- go test -count=1 -run 'Reset|Expiring|CapNeverSpends' ./internal/provider: ok
- go test -race -count=5 -run 'TestResetUse|TestExpiringSpend|TestSpendExpiringCodexResets': ok
Broken by hand: the write's error dropped again (`_ = steady.WriteFile`):
TestResetUseWriteFailureSeen red, "the record's loss went unnamed".

Not changed (pre-existing, its own issue): when readResetsUsed fails
for a reason other than a parse (a denied read), no copy is put aside
and the history is written over, and the `.bad-` copy's own write
error is dropped.
Not tried: a real full disk or denied path on a real account.

Reported-by: cgsme
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-11-07 21:59:07 +08:00
yetone 4795f56469 provider, gui, cli: a Codex account's card says the last reset magpie used, when and why (foobra12 on Discord)
foobra12: "magpie会自动使用codex重置卡吗 我发现刚才好像不小心自动给我使用了
一张". Auto-use is off by default and spends only for accounts switched on
(the week used up with no other account to answer, or a reset ~30 min
before it expires), but a reset magpie spent was said only in the gateway's
log. A reset gone from the card read the same whether magpie used it, the
user did, or it ran out.

Each reset magpie spends is now kept in codex-resets-used.json (by account,
when, why: week / expiring / hand, windows started again; 90 days, 200 at
most, under a cross-process filelock, written with steady.WriteFile; a file
that doesn't parse is copied aside to .bad-<ts>, never read as empty). Only
a consume whose outcome is "reset" is kept. useCodexReset carries why:
AutoUseCodexReset passes week, SpendExpiringCodexResets expiring, and
UseCodexReset (Usage page, panel, CLI) hand.

loginReading, the choke point for LoginUsage and the Usage page, attaches
an account's last 5 uses to its reading (cached readings too, so a reading
kept from before a use still says it), and Quota/SubscriptionQuota carry
resetsUsed. The auto-use row on the Usage page's and the panel's card adds
a line "Last reset used: {when} · {why}" (zh 上次用卡:… · 自动,重置卡到期前),
every kept use in its title; an account in brief has it in its switch's
title. `magpie quota auto-reset <account>` lists them. en, zh, zh-TW, ja, de.

The default (off) and the expiry rule are unchanged: both are documented in
the switch's title and only reach accounts the user turned on.

Run:
- go test -tags nogui -count=10 -run TestResetUse ./internal/provider/
- break by hand: noteResetUsed call removed -> TestResetUsesKept red
  ("kept [], want [week hand expiring]"); loginReading attach removed ->
  red ("the card's reading has []")
- node --test codex-resets-used.test.cjs: chromium+webkit, 1100 and 420,
  en zh zh-TW ja de, the Usage page card, brief title, the panel card; 3
  runs green; with the say.append removed, 5/5 red
- codex-auto-reset, routing-auto-reset, quota-curve, click-scroll,
  tray-usages, i18n-keys, gui-ja, gui-de, gui-zh-tw: green.
  old-webkit is red on "Safari 15.0 styles" at app.css .prefs
  .val:has(> .side-effort), from dc2f0da82, the same on origin/main
  692a779d3; not this change's.
- gofmt -l, go vet (darwin, linux -tags nogui, windows), full tests of
  internal/provider, internal/cli, internal/gui, internal/gateway

Not tried: no real Codex account and no real reset spent (the ChatGPT
endpoints are faked); no notification when magpie spends one.
2026-10-13 17:32:39 +08:00