Skip to content

rtk discover reports false negatives when hook-based rewriting is active #1055

Closed

Description

@Gchapolin

Description

rtk discover reports commands as "missed savings" even when the PreToolUse hook (rtk-rewrite.sh) is actively rewriting them. This makes the discover output misleading -- it suggests massive token waste that isn't actually happening.

Steps to reproduce

  1. Install RTK with the Claude Code hook via rtk init
  2. Use Claude Code normally (commands like git diff, ls -la, etc.)
  3. Run rtk gain -- shows 2683 commands processed, 6M tokens saved (86.4%)
  4. Run rtk discover -- shows "Already using RTK: 27 commands (0%)" and reports ~1.3M tokens as "saveable"

Root cause

Claude Code's conversation history (JSONL files) records the command as the assistant requested it (e.g., git diff), not what was actually executed after the hook rewrote it (e.g., rtk git diff).

rtk discover scans these JSONL files, sees git diff, and concludes it wasn't processed by RTK. But the hook intercepted and rewrote it before execution.

Evidence

  • rtk gain shows 2683 commands with 86.4% savings -- these came from the hook
  • rtk discover says only 27 commands (0%) used RTK -- it only counts explicit rtk prefixed commands in the history
  • Output format in Claude Code sessions confirms RTK is active (compact ls output, filtered git diff, etc.)
  • The hook works correctly when tested manually: echo '{"tool_input":{"command":"git diff"}}' | rtk-rewrite.sh produces the correct rewrite

Suggested fix

Options:

  1. Check if hook is configured: if rtk-rewrite.sh is present in Claude Code's settings.json, assume supported commands are already being rewritten and exclude them from "missed savings"
  2. Hook-side logging: have the hook log rewritten commands to a file that rtk discover can cross-reference against the conversation history
  3. Label the output: at minimum, add a note to discover output like "Note: if using hook-based rewriting, these commands may already be handled"

Environment

  • rtk 0.30.1 (also checked: not fixed in 0.35.0 release notes)
  • macOS Darwin 25.4.0
  • Claude Code CLI

Activity

  1. added
    bug
    effort-medium
    on Apr 12, 2026
  2. pszymkowiak commented on Apr 12, 2026

    @pszymkowiak
    Collaborator

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

  3. PoloBLS commented on Apr 30, 2026

    @PoloBLS

    Hitting this on Claude Code, both macOS (Homebrew rtk 0.31.0) and Debian (deb 0.31.0). Concrete numbers from a 30-day window on the Mac:

    • rtk discover : 240 / 9771 commands "already using RTK" → reports 2% adoption
    • rtk gain : 3380 commands processed, 5.4M tokens saved → 51.7% efficiency

    The hook is functioning correctly (verified with a live test: ls -la /tmp → rewritten to rtk ls -la /tmp and the rtk DB increments). The discrepancy comes from CC's session JSONL storing the original tool_input.command, not the post-hook value, so any session-log-based scanner under-reports rewrites by ~25×. Almost led us to "fix" a working hook config based on the discover output.

    Suggested resolution direction (FYI for whoever picks this up): could discover cross-reference the local rtk telemetry DB and discount commands whose timestamp+cwd matches a recorded rewrite? Or simpler — surface a clear note in the discover output when rtk gain shows significant activity, warning that hook-rewritten commands aren't visible in session logs.

  4. added
    duplicate
    on May 27, 2026
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:clibugduplicateeffort-mediumpriority:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions