Skip to content

rtk gain cannot represent a regression: saturating_sub floors negative savings to zero #3702

Open

Description

@randomparity

Summary

Tracker::record computes savings with saturating_sub on usize, so when RTK's output
is larger than the baseline the result is floored to 0. The schema has no
representation for "RTK made this worse": saved_tokens can never be negative, and no
aggregate — rtk gain, --daily, --weekly, --graph, --quota, --format json —
can surface a regression.

RTK does inflate some outputs, so these cases exist and are currently invisible.

Location

src/core/tracking.rs:425 (present on master @ b34be37 and in v0.45.0):

let saved = input_tokens.saturating_sub(output_tokens);
let pct = if input_tokens > 0 {
    (saved as f64 / input_tokens as f64) * 100.0
} else {
    0.0
};

Both operands come from estimate_tokens at src/core/tracking.rs:1394.

Evidence that regressions exist

Measured on a pinned Rust repository (457 .rs files), comparing raw output bytes against
rtk <cmd> output bytes:

command raw rtk change
ls src 251 341 +35%
ls -R src 6,926 8,956 +29%
cargo check --workspace --all-targets 72 100 +38%

Each of these records saved_tokens = 0, savings_pct = 0.0 — indistinguishable from a
command where RTK correctly declined to act.

Verified against the tracking DB directly:

$ sqlite3 ~/Library/Application\ Support/rtk/history.db \
    "SELECT COUNT(*) FROM commands WHERE saved_tokens < 0;"
0
$ sqlite3 ~/Library/Application\ Support/rtk/history.db \
    "SELECT COUNT(*) FROM commands WHERE output_tokens > input_tokens;"
0

Zero by construction, not by observation.

Why it matters

The aggregate savings number is the project's headline metric and the basis of the
--quota estimate. Flooring regressions to zero biases it upward: a deployment that is
net-negative for a given user cannot show that anywhere in the tooling. It also hides the
signal maintainers would most want — which handlers regress, and on what inputs.

Proposed fix

1. Store savings signed. Change saved_tokens to i64 and let savings_pct go
negative:

let saved = input_tokens as i64 - output_tokens as i64;
let pct = if input_tokens > 0 {
    (saved as f64 / input_tokens as f64) * 100.0
} else {
    0.0
};

Displays that assume non-negative values need a max(saved, 0) guard, or better, an
explicit branch so a loss renders as a loss. Existing rows remain valid — they are simply
non-negative.

2. Add a Regressions section to rtk gain, listing commands where output grew,
ordered by total tokens added. This turns an invisible failure into a work queue.

3. Optional, and arguably the real fix — a do-no-harm guard. run_filtered already
holds both the raw and filtered output at the point of decision. When the filtered form is
larger, emit the raw output and record the row as a no-op. That converts every regression
into a neutral outcome at negligible cost, and makes the guarantee "RTK never makes it
worse" true by construction rather than by handler-by-handler tuning.

I'd suggest (1) and (2) as one small change, with (3) as a follow-up since it changes
runtime behaviour rather than only accounting.

Environment

  • rtk 0.45.0 (Homebrew), macOS arm64
  • Confirmed still present on master @ b34be37

Related

Activity

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

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 92%

    Summary

    saturating_sub in Tracker::record floors output-larger-than-input cases to 0 savings, making RTK regressions structurally invisible in gain/quota metrics; store saved_tokens signed (i64) and surface regressions in rtk gain.

    📁 Relevant files
    • src/core/tracking.rs

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

  2. KuSh commented on Sep 22, 2026

    @KuSh
    Collaborator

    Another producer of the rows this issue is about, found while reviewing #4125, with a base-vs-head measurement in case it is useful for scoping the fix.

    rtk err|test|summary <missing-program> renders a [FAIL] block over a one-line command not found, so the rendered output is reliably larger than the raw it was made from. The tracked row therefore goes negative, and saturating_sub shows Saved 0 beside it:

    # isolated HOME/XDG/RTK_DB_PATH, LC_ALL=C
      #  Command                   Count  Saved  Total%
     1.  rtk summary                   1      0    0.0%     <- guards with never_worse
     2.  rtk test rtk-nope-xyz         1      0  -60.0%
     3.  rtk err rtk-nope-xyz          1      0  -100.0%
     4.  rtk err ./crlf.sh             1      0  -157.1%
    

    It is pre-existing rather than new — the same command on the merge-base already books -125.0% for rtk err <missing> — but #4125 adds a second surface that does it, so the shape is worth pinning here rather than in that PR.

    Two things that may save someone time:

    • summary.rs is the one path that already guards, via never_worse, which is why it reads 0.0%.
    • Applying the same guard to the err/test render is not the fix. never_worse returns the raw when the rendered form is larger, so it deletes the [FAIL] Command failed (exit code: 127) line — I applied it and tests/run_shell_test.rs::err_reports_an_unresolvable_program_as_exit_127 fails on exactly that assertion. Where a render is deliberately more legible than its input, the choice is between tracking it as a regression and not tracking it as a filter at all; the guard cannot decide it.
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:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions