Repository navigation
fix(usage): never overwrite a corrupt usage rollup file - #2512
Conversation
Saving rollups re-reads the month's file under the lock in strict mode, but strict parsing only threw for another version. Malformed JSON or JSON that is not an object parsed to null, fell back to an empty file and was written over the existing rollup, dropping archived days whose event files had already been pruned. parseRollupFile now throws for every file it cannot use, and the reader catches that to report the month as unreadable, so the strict flag goes away. The save fails instead, and cleanup keeps the event files because their totals were never saved. Co-Authored-By: OpenClaude <openclaude@gitlawb.com>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Summary
saveUsageDayRollups(src/services/usage/rollups.ts). Saving rollups re-reads the month's file under the lock with "strict" parsing, but strict mode only threw for a file of another version. Malformed JSON, or JSON that is not an object, parsed tonulland fell back to an empty file. That empty file was written over the existing rollup, which dropped archived days whose raw event files had already been pruned. This contradicted the comment above the read ("if the file cannot be read, rewriting it would drop days whose event files are already gone, so the save fails instead").parseRollupFilenow throws for every file it cannot use (malformed JSON, not an object, or another version), and thestrictflag is removed:readRollupFileCheckedcatches the error and reports the month as unreadable, as before.saveUsageDayRollupsfails instead of overwriting the file.getUsageDayRollupsalready catches save errors, so the totals it returns are unchanged.appendToPrunedUsageDaystill fails on an unreadable or missing file, with the same outcome as before; only the error message differs for a corrupt file.Impact
~/.openclaude/usage/rollups/<YYYY-MM>.json) is no longer replaced, so archived usage for that month is kept and can still be recovered./usagekeeps showing "Could not read saved usage for " until the file is fixed or removed, instead of showing it once and then silently dropping those days. While the file is unreadable, rollups for that month are not saved and its event files are not pruned. This matches the existing behavior for rollup files from another version or without read permission.parseRollupFileloses itsstrictparameter and always returns aUsageRollupFileor throws. Its three callers now handle errors throughtry/catchinstead ofnullchecks.Testing
src/services/usage/), so the core checks, the compile-cache-disabled launcher check, focused regression tests for the usage service and its callers, and the PR intent scan.bun install --frozen-lockfile):bun run check:static: passedbun run typecheck:fast: no type errors in 3,359 filesbun run typecheck:type-tests: passed (11 files)node bin/openclaude --versionandNODE_DISABLE_COMPILE_CACHE=1 node bin/openclaude --version:0.36.0 (OpenClaude)git diff --check: cleanbun run security:pr-scan -- --base FETCH_HEAD --head HEAD: no suspicious additions foundbun test src/services/usage/ src/components/Usage/: 104 pass, 0 failbun run test ./src/services/usage/rollups.test.ts ./src/services/usage/ledger.test.ts ./src/services/usage/query.test.ts: 65 pass, 0 failrollups.ts, the corrupt-JSON and non-object cases fail; with the fix all three pass.Notes
/usagenote names the unreadable month but not the file path. Adding the path would help users fix it and could be a small follow-up.Summary by CodeRabbit