Skip to content

rtk gain panics with 'attempt to add with overflow' when any command has negative saved_tokens #3958

Closed

Description

@KuSh

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 overflow

rtk 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 here

then src/core/tracking.rs:851:

total_saved += saved;              // overflow in debug, silently absurd in release

In a release build there is no panic — the totals and the savings percentage are simply nonsense, which is arguably worse.

Notes

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

  1. rtk-wshm-sync-bot commented on Sep 9, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 97%

    Summary

    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

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • src/core/tracking.rs

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  2. KuSh commented on Sep 19, 2026

    @KuSh
    CollaboratorAuthor

    This is fixed on develop by commit 9da3270, but that change is not in a stable release yet: rtk gain no 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.

  3. added
    resolved-pending-close
    on Sep 19, 2026
  4. KuSh commented on Sep 26, 2026

    @KuSh
    CollaboratorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:clibugpriority:highresolved-pending-close

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions