Skip to content

fix(gain): Recent Commands displays hardcoded negative sign on savings percentage #2815

Open

Description

@MorningStar0709

Bug Description

rtk gain -H Recent Commands list displays all savings percentages with a hardcoded negative sign prefix (e.g., -94%, -31%, -0%), even though the actual savings_pct in the database is always >= 0.

Steps to Reproduce

  1. Run any RTK-tracked commands to populate history.db
  2. Run rtk gain -H
  3. Observe the Recent Commands section — all percentages show with a - prefix

Expected Behavior

Percentages should display as positive values (e.g., 94%, 0%), consistent with the By Command table.

Actual Behavior

All percentages display with a hardcoded - prefix (e.g., -94%, -0%).

Root Cause

In src/analytics/gain.rs L263, the format string for Recent Commands has a hardcoded - prefix:

println!(
    "{} {} {:<25} -{:.0}% ({})",  // "-" is hardcoded
    time, sign, cmd_short,
    rec.savings_pct,                // this value is always >= 0
    format_tokens(rec.saved_tokens)
);

The savings_pct value is calculated in tracking.rs using input_tokens.saturating_sub(output_tokens), which guarantees the value is always >= 0. The By Command table displays the same data correctly (e.g., 93.9%) because it does not have this hardcoded prefix.

Verification

  • Database query: SELECT COUNT(*) FROM commands WHERE savings_pct < 0 returns 0
  • rtk gain (By Command table) displays the same data correctly with positive percentages
  • rtk gain -H (Recent Commands) incorrectly shows -0% for commands with 0% savings

Impact

  • Display only — does not affect actual token tracking or savings functionality
  • All rtk gain -H users see this issue
  • By Command, Daily/Weekly/Monthly statistics are unaffected

Suggested Fix

Remove the hardcoded - prefix from the format string in gain.rs L263:

// Before
"{} {} {:<25} -{:.0}% ({})"

// After
"{} {} {:<25} {:.0}% ({})"

Activity

  1. added
    bug
    good first issue
    on Jul 4, 2026
  2. pszymkowiak commented on Jul 4, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟢 Priority low
    🎯 Confidence 97%

    Summary

    Recent Commands table in rtk gain -H hardcodes a '-' prefix on savings_pct, which is always non-negative; remove the hardcoded sign in the format string at src/analytics/gain.rs:263.

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

    📁 Relevant files
    • src/analytics/gain.rs

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

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:clibuggood first issuepriority:low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions