Repository navigation
Description
rtk discover counts hook-rewritten commands as misses
Version: rtk 0.42.3 · macOS 14 (Darwin 24.6.0) x86_64 · Claude Code
Summary
When the PreToolUse hook (rtk hook claude) is installed, rtk discover reports
a large undercount of RTK usage — and lists commands the hook successfully
rewrote under MISSED SAVINGS, as if they had run raw.
Measured over 359 transcripts / 30 days on my machine: discover reports 25.3%
"already using RTK", actual effective coverage is 44.8%. A 19.5-point
undercount, and the accompanying "~24.6K tokens saveable" is mostly savings
already banked.
Cause
discover reads the Claude Code transcripts under ~/.claude/projects/*/*.jsonl,
which record the command as the model emitted it — before the PreToolUse hook
rewrote it. The rewrite is therefore invisible to discover, and the original
(raw) form is what gets classified.
Reproduction
- Install the PreToolUse hook (
rtk hook claude) in Claude Code settings. - Have the agent run a raw
grep -n pattern file.py. - Observe the output is RTK-shaped (
N matches in M files: [+K more]) — the hook
rewrote it tortk grepand it ran through RTK. - Run
rtk discover. - Expected: that call counted under "Already using RTK".
Actual: it appears under MISSED SAVINGS asgrep -n, with an estimated
token saving that is not actually available.
On my 30-day window discover listed grep -n 37× and ls -la 34× as missed. Both
are rewritten by the hook on every invocation — confirmed with:
$ rtk hook check 'grep -n foo bar.py'
rtk grep -n foo bar.py
$ rtk hook check 'cd /tmp && grep -n foo bar.py'
cd /tmp && rtk grep -n foo bar.py
Suggested fix
rtk hook check already exposes the rewrite engine as a pure function. Applying
those same rules to each transcript command before classification would make
discover agree with reality, with no new logging or storage:
- if
hook check <cmd>yields a rewrite → count as covered, not missed - if it does not → genuine miss, report as today
An alternative would be having the hook record its rewrites, but that adds runtime
state for something the dry-run can already answer retroactively.
Two details that mattered when I implemented this correction locally:
cd X && rtk …and pipelines should count as covered — matching only a leading
rtktoken undercounts significantly.rtk proxy <cmd>should not count as coverage; it deliberately runs the raw
command, so counting it lets the audit flatter itself via its own escape hatch.
Secondary: $(cat …) command substitution is unhandled
147 occurrences in my 30-day window, the largest single unhandled shape. These come
from a python-shim pattern:
$(cat graphify-out/.graphify_python) -c "..."
The hook can't see past the substitution to the interpreter, so nothing is
rewritten. Lower priority than the above, but it is the top entry on my
"genuinely unhandled" list once the false misses are removed.
Happy to share the local correction script (~260 lines of Python that reproduces
the corrected figures from transcripts) if it's useful as a reference.
Activity
pszymkowiak commentedon Jul 22, 2026
🔍 Automated Triage
bughighSummary
rtk discover misclassifies hook-rewritten commands as raw misses (19.5pt undercount), skewing savings metrics; fix by running the existing
hook checkrewrite engine against each transcript command before classification.📁 Relevant files
src/discover.rssrc/hook/rewrite.rsTriaged automatically by wshm · This is an automated analysis, not a human review.
albatrossflyon-coder commentedon Jul 22, 2026
I'd like to work on this one — will reproduce the hook-rewritten-commands-counted-as-misses behavior in
rtk discoverand trace where the miss/hit classification diverges from what actually happened.Yohjisakamoto commentedon Jul 22, 2026
This discover misclassification is another reason command-level savings counters need end-to-end validation. Tura’s DeepSWE benchmark measures completed tasks and verifier outcomes, and suggests fewer agent rounds cut about 80% of token overhead. Would you compare the methodology? https://turaai.net/docs#benchmark-current-test-set-record
fix(discover): log real hook decisions instead of guessing coverage r…
fix(discover): log real hook decisions instead of guessing coverage r…
AbdelrahmanAlabdallah commentedon Aug 23, 2026
Adding a corroborating data point — same failure mode, independent measurement.
rtk 0.45.0, Linux, Claude Code, PreToolUse hook (
rtk hook claude) installed and confirmed active insettings.json.rtk discover(30-day/50-session scan): "Already using RTK: 524 commands (10.2%)" — down from ~26% measured a week earlier on the same setup.But
rtk gain(all-time execution tracking, not transcript-scanning) shows 2753 commands actually routed through rtk at 68.7% avg savings — consistent with the hook working, not regressing.To rule out an actual hook regression, I piped each of
discover's own "TOP UNHANDLED COMMANDS" / "MISSED SAVINGS" entries straight intoecho '{"tool_name":"Bash","tool_input":{"command":"..."}}' | rtk hook claudeand checkedupdatedInput.commandin the response:git status→ rewritten tortk git statusgrep -n "foo" bar.py→ rewritten tortk grep -n "foo" bar.pyexport FOO=bar && git status→ rewritten toexport FOO=bar && rtk git statusPOSTGRES_DB="" python -m pytest ...(env-var-prefixed) → rewritten toPOSTGRES_DB="" rtk pytest ...cd /some/path && git status→ rewritten tocd /some/path && rtk git statusAll five — every shape
discoverhad just flagged as a miss — were in fact correctly rewritten by the live hook. That matches the root cause already identified here:discoverclassifies from the pre-rewrite transcript text, so any hook-rewritten command that isn't a barertk ...prefix (chained, env-prefixed, etc.) gets misclassified as unhandled even though it ran through rtk successfully.fix(discover): log real hook decisions instead of guessing coverage r…