rtk icon indicating copy to clipboard operation
rtk copied to clipboard

`gain`: by-command table puts `SUM(saved_tokens)` next to `AVG(savings_pct)`, producing rows that can't be read together

Open juxtapo9090 opened this issue 3 months ago • 4 comments

What happens

rtk gain's By Command table prints a Saved column and an Avg% column side by side, but they come from different aggregations, so the two numbers in a row don't describe the same quantity. On my install the top row reads:

 #  Command      Count   Saved    Avg%
 1. rtk grep      1536  478.8M   24.0%

478.8M is a SUM. 24.0% is an AVG of per-command percentages. A reader naturally combines them — "24% off 478.8M" — and gets nonsense. Against the reported total input of 609.1M, a 478.8M saving is ~78%, not 24%.

Where

src/tracking.rs, get_by_command:

"SELECT rtk_cmd, COUNT(*), SUM(saved_tokens), AVG(savings_pct), AVG(exec_time_ms)
 FROM commands
 GROUP BY rtk_cmd
 ORDER BY SUM(saved_tokens) DESC
 LIMIT 10",

AVG(savings_pct) weights a command that saved 12 tokens exactly as heavily as one that saved 12 million. With a long-tailed distribution — which command output always is — the average percentage is dominated by the many tiny commands while the sum is dominated by the few huge ones. The columns end up describing disjoint populations.

Suggested fix

Make the percentage consistent with the sum in the same row:

"SELECT rtk_cmd, COUNT(*), SUM(saved_tokens),
        100.0 * SUM(saved_tokens) / NULLIF(SUM(input_tokens), 0),
        AVG(exec_time_ms)
 FROM commands GROUP BY rtk_cmd ORDER BY SUM(saved_tokens) DESC LIMIT 10",

If the mean-of-percentages is wanted too, it's genuinely useful — but as its own column with a distinct header (Avg% vs Total%), because they answer different questions.

Environment

rtk 0.45.0, Linux x86-64, 10,321 commands over the 90-day window.

juxtapo9090 avatar Aug 10 '26 01:08 juxtapo9090

wshm · Automated triage by AI

🔍 Automated Triage

🐛 Category bug
🟡 Priority medium
🎯 Confidence 90%

Summary

rtk gain's by-command table mixes SUM(saved_tokens) with AVG(savings_pct) in the same row, producing misleading combined figures; fix by computing weighted %-saved from SUM(saved_tokens)/SUM(input_tokens) or clearly separating the two metrics with distinct headers.

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

📁 Relevant files
  • src/tracking.rs

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

pszymkowiak avatar Aug 10 '26 01:08 pszymkowiak

I'm working on this. I plan to make the by-command percentage consistent with the displayed saved-token total by deriving it from the aggregate token sums, and to add regression coverage for weighted command totals and the zero-input edge case.

Written by an agent (Codex, GPT-5).

cakeni avatar Aug 10 '26 01:08 cakeni

This is fixed on develop by PR #891, but that change is not in a stable release yet. Labeling it resolved-pending-close; it will be closed once a release containing the fix ships. If it still happens on that release, please comment here.

KuSh avatar Sep 19 '26 02:09 KuSh

Fixed by #891, shipped in v0.50.0: the by-command table now shows a Total% computed from the same token sums as the Saved column, so the two numbers in a row describe the same quantity. A skewed history (one large 95% call plus several tiny 0% calls) now reads 95.0% instead of the diluted average. If you still see mismatched figures on v0.50.0 or later, please comment or reopen.

KuSh avatar Sep 26 '26 22:09 KuSh