One function
record_projection_snapshot in leeguooooo/claude-code-usage-bar
The author described this change as “fix(predict): 旧高频快照一次性重抽稀——不然 300KB 文件要拖 8 小时才瘦身”. It counts as a record because the check below fails on the code as it stood at 06a604980 and passes on a704f1d28, with nothing else changed between the two runs.
Fix saved2026-07-10
Sharing licenceMIT · LICENSE
Change size+20 −0
What the code was meant to do, written into the code itself as a docstring
Append a (used, projected) snapshot for offline backtesting. Throttled to one per window per SNAPSHOT_MIN_GAP_S: unthrottled, every compute appended one (observed live: 0.4s average gap), so the 1000-entry cap covered 8.5 MINUTES of history — useless for backtesting — while the ~150KB of snapshots were re-parsed and re-written by the daemon every second, its single largest CPU line.
The change
| 28 | 28 | break | |
| 29 | 29 | snaps.append(snap) | |
| 30 | 30 | del snaps[:-MAX_PROJECTION_SNAPSHOTS] | |
| 31 | + | # One-shot migration for stores written before throttling existed: a | |
| 32 | + | # legacy list holds ~1000 entries at 0.4s spacing, and at one append per | |
| 33 | + | # minute they'd take ~8 hours to age out — keeping the file fat the whole | |
| 34 | + | # time. Re-decimate in place whenever the grid is violated. | |
| 35 | + | last_by_window: Dict[str, float] = {} | |
| 36 | + | kept = [] | |
| 37 | + | for entry in snaps: | |
| 38 | + | if not isinstance(entry, dict): | |
| 39 | + | continue | |
| 40 | + | w = str(entry.get("window") or "") | |
| 41 | + | at = _coerce(entry.get("observed_at")) | |
| 42 | + | if at is None: | |
| 43 | + | continue | |
| 44 | + | prev_at = last_by_window.get(w) | |
| 45 | + | if prev_at is not None and at - prev_at < SNAPSHOT_MIN_GAP_S: | |
| 46 | + | continue | |
| 47 | + | last_by_window[w] = at | |
| 48 | + | kept.append(entry) | |
| 49 | + | if len(kept) != len(snaps): | |
| 50 | + | snaps[:] = kept | |
| 31 | 51 | return store |
The check that tells the two apart
fail→pass·tests/test_projection.py::test_legacy_dense_snapshots_are_redecimated_on_append
Check file tests/test_projection.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before it06a604980fab78a26e599b8564d8bf2c2887578d
Broken version dated2026-07-10
Moduleclaude_statusbar.predict
Units changedrecord_projection_snapshot
Fingerprinta884558cd831290b
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.
Other bugs found in leeguooooo/claude-code-usage-bar
- 2026-08-02_relay_host
- 2026-07-09run_forever
- 2026-07-03verdict
- 2026-07-02line_text
- 2026-06-10reconcile_account