Skip to content

rtk discover counts hook-rewritten commands as missed savings #2299

@Sin213

Description

@Sin213

Summary

rtk discover reports commands as "missed savings" even when they are actively being rewritten by a PreToolUse hook. The transcript logs the pre-hook command (what Claude requested), but the hook rewrites it before execution. discover scans the pre-hook text and doesn't account for the rewrite, producing inflated numbers.

Reproduction

  1. Configure a PreToolUse hook in Claude Code settings.json that rewrites commands via rtk hook claude:

    {
      "hooks": {
        "PreToolUse": [{
          "matcher": "Bash",
          "hooks": [{ "type": "command", "command": "/path/to/rtk-unified.sh" }]
        }]
      }
    }
  2. The hook correctly rewrites commands — verified by piping test inputs through rtk hook claude:

    $ echo '{"tool_input":{"command":"git diff"}}' | rtk hook claude
    {"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecisionReason":"RTK auto-rewrite","updatedInput":{"command":"rtk git diff"}}}
    
    $ echo '{"tool_input":{"command":"rg -n \"pattern\" src/"}}' | rtk hook claude
    {"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecisionReason":"RTK auto-rewrite","updatedInput":{"command":"rtk grep -n \"pattern\" src/"}}}
    
  3. Run rtk discover --all --since 7:

    Already using RTK: 1036 commands (14.5%)
    
    MISSED SAVINGS
    git diff    922    ~148.3K tokens
    rg -n       973    ~81.4K tokens
    ls -la      424    ~16.8K tokens
    find        249    ~47.1K tokens
    ...
    Total: 3431 commands -> ~410.6K tokens saveable
    
  4. But rtk gain confirms those commands ARE being processed through RTK:

    Total commands: 1167
    rtk grep: 201, rtk read: 216, rtk git diff, rtk git commit: 38, ...
    

Root cause

Claude Code session transcripts (.jsonl) log tool_input.command — the pre-hook command. The hook emits updatedInput to rewrite it, but the transcript preserves the original. rtk discover scans the original and doesn't check whether a hook rewrite occurred.

Suggested fix

One or more of:

  • Detect the presence of an RTK PreToolUse hook and exclude commands that would match rtk hook claude from the "missed" count
  • Cross-reference against rtk gain data to filter already-processed commands
  • Note in the output when a hook is detected: "Note: PreToolUse hook active — these commands may already be rewritten at execution time"

Environment

  • rtk 0.42.3
  • Claude Code with PreToolUse hook via rtk hook claude
  • Linux

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions