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>