Skip to content

rtk discover counts hook-rewritten commands as misses #3148

Closed

Description

@marcomorettim

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

  1. Install the PreToolUse hook (rtk hook claude) in Claude Code settings.
  2. Have the agent run a raw grep -n pattern file.py.
  3. Observe the output is RTK-shaped (N matches in M files: [+K more]) — the hook
    rewrote it to rtk grep and it ran through RTK.
  4. Run rtk discover.
  5. Expected: that call counted under "Already using RTK".
    Actual: it appears under MISSED SAVINGS as grep -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
    rtk token 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

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

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 88%

    Summary

    rtk discover misclassifies hook-rewritten commands as raw misses (19.5pt undercount), skewing savings metrics; fix by running the existing hook check rewrite engine against each transcript command before classification.

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

    📁 Relevant files
    • src/discover.rs
    • src/hook/rewrite.rs

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

  3. albatrossflyon-coder commented on Jul 22, 2026

    @albatrossflyon-coder
    Contributor

    I'd like to work on this one — will reproduce the hook-rewritten-commands-counted-as-misses behavior in rtk discover and trace where the miss/hit classification diverges from what actually happened.

  4. Yohjisakamoto commented on Jul 22, 2026

    @Yohjisakamoto

    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

  5. added a commit that references this issue on Aug 2, 2026
    298d998
  6. added a commit that references this issue on Aug 10, 2026
    873b95c
  7. AbdelrahmanAlabdallah commented on Aug 23, 2026

    @AbdelrahmanAlabdallah

    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 in settings.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 into echo '{"tool_name":"Bash","tool_input":{"command":"..."}}' | rtk hook claude and checked updatedInput.command in the response:

    • git status → rewritten to rtk git status
    • grep -n "foo" bar.py → rewritten to rtk grep -n "foo" bar.py
    • export FOO=bar && git status → rewritten to export FOO=bar && rtk git status
    • POSTGRES_DB="" python -m pytest ... (env-var-prefixed) → rewritten to POSTGRES_DB="" rtk pytest ...
    • cd /some/path && git status → rewritten to cd /some/path && rtk git status

    All five — every shape discover had just flagged as a miss — were in fact correctly rewritten by the live hook. That matches the root cause already identified here: discover classifies from the pre-rewrite transcript text, so any hook-rewritten command that isn't a bare rtk ... prefix (chained, env-prefixed, etc.) gets misclassified as unhandled even though it ran through rtk successfully.

  8. added a commit that references this issue on Sep 1, 2026
    0c952eb
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:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions