rtk icon indicating copy to clipboard operation
rtk copied to clipboard

rtk discover double-counts commands that the hook actually rewrote

Open shaswatshah opened this issue 7 months ago • 2 comments

Running rtk discover -a --since 10 tells me I have 2166 bare gh api calls and 3404 bare git diff calls "missed" over the last 10 days, and "Already using RTK: 0%". Took this at face value at first and started hunting for a broken hook.

But rtk gain over the same window shows ~3800 actual rtk executions, including plenty of rtk gh pr diff, rtk git log, rtk git diff with real token savings. Both can't be right.

What I think is happening: rtk discover reads tool_input.command from Claude Code session transcripts. That field captures what the model emitted — before the PreToolUse hook mutates it. So even when the hook successfully rewrites gh api foo → rtk gh api foo and rtk runs, the transcript still records gh api foo as the input. Discover then counts it as a miss.

A couple things I noticed that back this up:

  1. Across ~340 post-install transcripts, 100% of git/gh calls are bare in tool_input.command. If the hook were intermittently failing I'd expect a mix. Uniform bare means the transcript simply never records the rewritten form.
  2. Spot-checked the tool_result for a bare gh api .../comments --paginate — it contained [rerun: b48], an rtk post-processing marker. rtk definitely ran, discover just had no way to know.

So rtk gain (real executions) and rtk discover (transcript scan) give opposite pictures for the same commands, and discover is the one that's off.

Possible fixes:

  • Cross-reference discover's miss list against rtk gain's execution log and subtract matches
  • Detect rtk markers in the adjacent tool_result and treat the input as already-rewritten
  • If Claude Code exposes the post-hook updatedInput.command anywhere in the transcript, prefer that over tool_input.command

On rtk 0.35.0, Claude Code on macOS, hook installed at ~/.claude/hooks/rtk-rewrite.sh per the README.

shaswatshah avatar Apr 17 '26 01:04 shaswatshah

Raw output from both commands for reference. Lightly anonymized (user paths, internal project slugs).

rtk gain

RTK Token Savings (Global Scope)
════════════════════════════════════════════════════════════

Total commands:    3836
Input tokens:      4.8M
Output tokens:     1.1M
Tokens saved:      3.7M (76.8%)
Total exec time:   32m12s (avg 503ms)
Efficiency meter: ██████████████████░░░░░░ 76.8%

By Command
────────────────────────────────────────────────────────────────────────
  #  Command                   Count   Saved    Avg%    Time  Impact
────────────────────────────────────────────────────────────────────────
 1.  rtk read                    364    1.9M   47.4%     0ms  ██████████
 2.  rtk gh pr diff               29  709.7K   64.4%   539ms  ████░░░░░░
 3.  rtk grep                    199  276.1K   19.3%    12ms  █░░░░░░░░░
 4.  rtk find                     50  257.6K   64.8%     9ms  █░░░░░░░░░
 5.  rtk git log --all --p...      1  114.0K   99.6%    20ms  █░░░░░░░░░
 6.  rtk ls -la /var/folde...      4   41.8K   63.0%    26ms  ░░░░░░░░░░
 7.  rtk ls -la /tmp/              3   29.7K   94.9%     7ms  ░░░░░░░░░░
 8.  rtk git diff origin/m...     20   29.2K   19.6%    26ms  ░░░░░░░░░░
 9.  rtk git diff 0f9ec9df...      1   28.3K   82.3%    36ms  ░░░░░░░░░░
10.  rtk git diff origin/m...      1   23.4K   96.8%    36ms  ░░░░░░░░░░
────────────────────────────────────────────────────────────────────────

rtk discover -a --since 10

RTK Discover -- Savings Opportunities
====================================================
Scanned: 255 sessions (last 10 days), 6802 Bash commands
Already using RTK: 71 commands (1%)

MISSED SAVINGS -- Commands RTK already handles
------------------------------------------------------------------------
Command                  Count    RTK Equivalent     Status        Est. Savings
gh api                    1421    rtk gh             existing      ~538.5K tokens
git log                   1632    rtk git            existing      ~235.9K tokens
grep -n                    311    rtk grep           existing      ~57.5K tokens
tail -10                   278    rtk read           existing      ~42.4K tokens
find ~/.claude/projects    158    rtk find           existing      ~31.2K tokens
ls -la                     297    rtk ls             existing      ~17.5K tokens
diff <(sed                  18    rtk diff           existing      ~6.9K tokens
wc -l                      156    rtk wc             existing      ~3.7K tokens
gcloud logging              55    rtk gcloud         existing      ~3.1K tokens
./.venv/bin/pytest te..      9    rtk pytest         existing      ~2.6K tokens
npm run                      8    rtk npm            existing      ~1.7K tokens
cargo test                  12    rtk cargo          existing      ~910 tokens
terraform plan               2    rtk terraform      existing      ~513 tokens
./node_modules/.bin/t..      4    rtk tsc            existing      ~78 tokens
npx --prefix                 3    rtk npx            existing      ~77 tokens
------------------------------------------------------------------------
Total: 4372 commands -> ~942.9K tokens saveable

TOP UNHANDLED COMMANDS -- open an issue?
----------------------------------------------------
Command                  Count    Example
poe                        326    poe -- lint-fix skills/<redacted>/ 2>&1
uv run                     249    uv run python -m pytest tests/<redacted>/..
git checkout                64    git checkout origin/main -- .github/wo..
jq                          64    jq -rn --argjson issues "$payload" '$i..
git rebase                  51    git rebase origin/main 2>&1
python3                     40    python3 /tmp/extract_<redacted>.py inputs..
git rev-parse               31    git rev-parse HEAD
poe test                    20    poe test 2>&1
body="$(cat <<'<RE..        17    body="$(cat <<'<REDACTED>_EOF'
## Review Tri..
bq query                    16    bq query --project_id=<redacted>..
git checkpoint              15    git checkpoint "<redacted>: ..
poe lint-fix                14    poe lint-fix 2>&1
cat                         13    cat > /var/folders/zz/..
echo "===                   12    TRANSCRIPT1="/Users/<user>/.claude/pr..
poe check                   12    poe check 2>&1
----------------------------------------------------
-> github.com/rtk-ai/rtk/issues

RTK_DISABLED BYPASS -- 2 commands ran without filtering
------------------------------------------------------------------------
These commands used RTK_DISABLED=1 unnecessarily:
  curl --proto (1x), gh repo (1x)
-> Remove RTK_DISABLED=1 to recover token savings

The contradiction is clearest on the top two rows of each: discover says 1421 bare gh api + 1632 bare git log are "misses" this window, but gain shows rtk gh pr diff and rtk git log .../rtk git diff ... among the top actual executions with real token savings banked. Both views are counting the same invocations — one sees the pre-hook string, the other sees the post-hook execution.

Also — poe at 326 is the top unhandled command in my setup if that's useful signal for what to prioritize next in the registry.

shaswatshah avatar Apr 17 '26 01:04 shaswatshah

Closing as duplicate of #2036. See the original issue for updates.

pszymkowiak avatar Apr 17 '26 15:04 pszymkowiak