Repository navigation
Description
Summary
A single tracked command whose RTK output is larger than its input makes rtk gain panic — permanently, for every later invocation, until the row is deleted by hand.
saved_tokens is stored as a signed integer and can legitimately be negative (RTK produced more output than the raw command). gain.rs reads it back as usize, so -51 becomes 18446744073709551565, and the running total overflows on the next add.
Reproduction
Any row with saved_tokens < 0 triggers it. On my machine four such rows arrived from a normal test run:
$ sqlite3 ~/.local/share/rtk/history.db \
"select original_cmd, input_tokens, output_tokens, saved_tokens
from commands where saved_tokens < 0;"
git worktree list|0|51|-51
find . -name '*.txt'|3|25|-22
find . -name 'cargo.toml'|3|25|-22
find . -name secret.txt|0|22|-22
$ rtk gain
thread 'main' (221433) panicked at src/core/tracking.rs:851:13:
attempt to add with overflowrtk gain --history fails the same way. Deleting the negative rows restores both.
Cause
src/core/tracking.rs:841 casts the column straight to usize:
row.get::<_, i64>(3)? as u64, // exec_time_ms
row.get::<_, i64>(1)? as usize, // input
row.get::<_, i64>(2)? as usize, // output
row.get::<_, i64>(3)? as usize, // saved <- negative wraps herethen src/core/tracking.rs:851:
total_saved += saved; // overflow in debug, silently absurd in releaseIn a release build there is no panic — the totals and the savings percentage are simply nonsense, which is arguably worse.
Notes
git worktree listshowsinput_tokens = 0, output_tokens = 51, i.e. the raw command produced nothing and RTK's version produced 51 tokens. Whatever the merits of that, the analytics must survive it.- The "never worse" guard does not prevent these rows from being recorded.
- Distinct from the UTF-8 char-boundary panics (rtk gain --failures (-F) panics on non-ASCII command text at gain.rs:731 (sibling of #2318) #3232, gain --history panics: 'end byte index is not a char boundary' with CJK/multi-byte command history #3733, rtk gain --history panics on non-ASCII (CJK) filenames in command history #2318, panic: rtk gain --history crashes on non-ASCII (Cyrillic) command strings — UTF-8 char boundary #2954, rtk gain --history panics on non-ASCII (Cyrillic) text — byte index not on char boundary #2787) — this one is an integer overflow and does not depend on the command text.
- Distinct from Rtk gain is not working #3853 ("No tracking data yet"), which is an empty database rather than a crash.
Suggested fix
Read the column as i64 and accumulate in i64, so negative savings subtract as they should, then clamp only at the presentation layer (a negative total is real information — it means RTK cost tokens over that window). A saturating cast would hide the signal.
Activity
rtk-wshm-sync-bot commentedon Sep 9, 2026
🔍 Automated Triage
bughighSummary
rtk gain panics on integer overflow when saved_tokens is negative because tracking.rs casts the signed i64 column to usize; fix by reading/accumulating as i64 and clamping only at display time
📁 Relevant files
src/core/tracking.rsTriaged automatically by wshm · This is an automated analysis, not a human review.
KuSh commentedon Sep 19, 2026
This is fixed on develop by commit 9da3270, but that change is not in a stable release yet:
rtk gainno longer overflows on a negative saved_tokens row, it reports those commands as 0 saved and exits normally. Labeling it resolved-pending-close; it will be closed once a release containing the fix ships. Note that the aggregate totals are still floored at zero rather than showing a net regression, which is the separate ask in #3702.KuSh commentedon Sep 26, 2026
Fixed by commit 9da3270, shipped in v0.50.0: rtk gain and rtk gain --history no longer overflow on a row with negative saved_tokens and report such commands as 0 saved. Showing a net-negative total instead of flooring it at zero is tracked separately in #3702. Please reopen or comment if rtk gain still fails for you on v0.50.0 or later.