Skip to content

rtk discover reports false-positive missed savings for hook-rewritten commands #1441

Closed

Description

@FlorianBruniaux

Summary

rtk discover reads the pre-hook command text from Claude Code JSONL transcripts and classifies every non-rtk prefix as "missed". The ~/.claude/hooks/rtk-rewrite.sh hook rewrites most of these at runtime (e.g. grep -n … → rtk grep -n …), but Claude Code never writes the post-hook command back to the transcript. Result: commands that actually ran through RTK get counted as missed, the adoption percentage is artificially deflated, and the user-facing doc actively misdiagnoses the cause.

The sibling command rtk session already solves this correctly in src/analytics/session_cmd.rs:28-51. rtk discover is the outlier.

Reproduction

On a machine with rtk ≥ 0.23.0 and the official hook installed, pick any recent Claude Code session JSONL and locate a bash tool_use whose command starts with grep -n (or any other hook-rewritten command).

CMD (from tool_use.input.command):
  grep -n "^version" crates/ccboard-core/Cargo.toml ...

tool_result.content (93 chars):
  1 matches in 1F:

  [file] crates/ccboard-core/Cargo.toml (1):
       4: version.workspace = true

The result is in the rtk grep filter format ([file] header, "X matches in YF:" summary). Raw grep would have produced path:line:content. The hook rewrote the command; the transcript still logs the original.

Now run:

rtk discover --all

The grep -n command appears in the MISSED SAVINGS column, even though the adjacent tool_result proves it ran through RTK.

Expected

Commands that classify_command returns as Classification::Supported should bucket as already-covered, matching rtk session's behavior. Only RTK_DISABLED=... prefixes and unsupported base commands (pnpm build, node, python3, …) should appear as missed.

Actual

src/discover/mod.rs:169-175 counts only the literal rtk prefix:

Classification::Ignored => {
    if part.trim().starts_with("rtk ") {
        already_rtk += 1;
    }
}

Every Classification::Supported goes to the missed bucket regardless of hook behavior.

Root Cause

Claude Code's JSONL logs the assistant's tool_use.input.command — what the model authored before any PreToolUse hook runs. The post-hook command is never persisted.

  • src/discover/provider.rs:175 extracts block.pointer("/input/command") from assistant tool_use blocks. No alternative field carries the rewritten command.
  • src/discover/mod.rs:115-175 makes the already-RTK decision on this pre-hook text.
  • src/analytics/session_cmd.rs:28-51 (reference implementation) already handles this, with an explicit doc comment:
/// A command is "covered" if it either:
/// - starts with "rtk " (explicit rtk invocation), or
/// - would be rewritten by the hook (classify_command returns Supported)
fn count_rtk_commands(...) -> (usize, usize, usize) {
    ...
    if part.starts_with("rtk ")
        || matches!(classify_command(part), Classification::Supported { .. })
    {
        rtk += 1;
    }
    ...
}

The two commands disagree on the same question.

Downstream impact

  1. Inflated "Est. Savings" in the MISSED column — the user sees a phantom ~5M token opportunity that is already captured.

  2. Deflated "Already using RTK" percentage — in my data, 8% reported vs a real adoption probably above 80%.

  3. Misleading docs — docs/guide/analytics/discover.md:35:

    "If commands appear in the missed list after installing RTK, it usually means the hook isn't active for that agent."

    This is the inverse of reality. The hook is active; discover just can't see past the transcript. That line sends users on a non-productive troubleshooting path (line 58 in the same doc already describes rtk session correctly).

Proposed fix

Mirror rtk session's logic in discover:

  • src/discover/mod.rs:115-175 — treat Classification::Supported as already-covered. The RTK_DISABLED= branch at mod.rs:98-112 stays unchanged (genuine opt-out, should still surface).
  • src/discover/report.rs:52, 83-90 — optionally split into already_rtk_explicit vs rewritten_by_hook so no signal is lost, or keep one counter and rely on the existing "TOP UNHANDLED" section for the gap analysis.
  • docs/guide/analytics/discover.md:35 — replace the misleading troubleshooting hint with accurate behavior description.

Existing tests in src/discover/report.rs:218-269 only exercise percentage formatting and will not regress. A new test mirroring src/analytics/session_cmd.rs:233-261 would be the right regression net.

Environment

  • rtk: 0.37.2 (also reproduced on 0.36.0)
  • OS: macOS 15.6 (Darwin 24.6.0)
  • Shell: zsh
  • Hook: ~/.claude/hooks/rtk-rewrite.sh (rtk-hook-version: 3), confirmed active via rtk rewrite "grep -n foo bar" → exit 0 → rtk grep -n foo bar

Activity

  1. self-assigned this
    on Apr 22, 2026
  2. added
    bug
    documentation
    effort-small
    good first issue
    on Apr 22, 2026
  3. pszymkowiak commented on Apr 22, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 96%

    Summary

    rtk discover falsely counts hook-rewritten commands as missed savings because it reads pre-hook transcript text; fix should mirror rtk session's classify_command logic in src/discover/mod.rs:169-175

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

    📁 Relevant files
    • src/discover/mod.rs
    • src/discover/provider.rs
    • src/discover/report.rs
    • src/analytics/session_cmd.rs
    • docs/guide/analytics/discover.md

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

  4. CapnRossDullblade commented on Apr 30, 2026

    @CapnRossDullblade

    Additional reproduction on rtk 0.38.0 with Claude Code 2.1.123 on macOS.

    I hit the same user-facing symptom:

    $ rtk discover --since 1 --format json
    sessions_scanned: 3
    total_commands: 263
    already_rtk: 0
    supported/missed includes: find, gh pr, grep, head, git log, diff, ls, wc
    
    $ rtk session
    recent sessions show RTK adoption: 31%, 45%, 83%, 82%, 100%, 64%, 47%
    

    The hook is installed and active in ~/.claude/settings.json:

    "PreToolUse": [{ "matcher": "Bash", "hooks": [{ "type": "command", "command": "rtk hook claude" }] }]

    Recent main-session JSONL contains PreToolUse:Bash hook records with permissionDecisionReason: "RTK auto-rewrite", for example:

    original tool_use.input.command:
      gh pr checks 42 --repo ... 2>&1
    
    hook updatedInput.command:
      rtk gh pr checks 42 --repo ... 2>&1
    

    So the main false positive is still present in 0.38.0: discover reports already_rtk: 0 because it reads the pre-hook assistant command text, while session sees/adopts RTK coverage.

    One extra edge case I found while debugging: rtk discover -v --since 1 included a Claude subagent JSONL file in the scan. That subagent transcript had:

    • 85 Bash tool_use commands
    • 0 explicit rtk ... commands
    • 0 PreToolUse:Bash hook_success records in the subagent JSONL
    • 68 commands where rtk rewrite "$cmd" exits 0

    Examples from that subagent transcript:

    find <project> -not -path ./ .git/* ... | head -100
      => raw find-style output in tool_result
    
    git log --oneline --all -20
      => raw git log output in tool_result
    

    I searched the surrounding project transcripts for the subagent toolu_* IDs and found only the subagent JSONL entries, not corresponding parent-session hook records. That suggests there may be two separate effects in the same discover output:

    1. main-session false positives from pre-hook transcript text, which this issue already covers;
    2. subagent transcripts that appear to have no recorded hook rewrite path at all, which may be genuine missed coverage or at least needs separate accounting.

    It would help if discover split these cases instead of a single "MISSED SAVINGS" bucket, e.g. explicit rtk, hook-rewritten/covered, and no-hook-or-unobserved subagent commands. That would answer the practical question users are asking: "is this retrospective noise, a discover accounting bug, or a real agent path where RTK is not being invoked?"

  5. KuSh commented on Sep 5, 2026

    @KuSh
    Collaborator

    This looks resolved on the current code (PR #3206, shipped in v0.48.0), but it could not be fully reproduced here. Marking it resolved-pending-close: it will be closed in a while unless someone reports it still happening on the latest version.

  6. added
    resolved-pending-close
    on Sep 5, 2026
  7. KuSh commented on Sep 13, 2026

    @KuSh
    Collaborator

    Fixed by #3206, shipped in v0.48.0: rtk discover no longer decides coverage from the literal rtk prefix. It now reconciles each command against the recorded hook decisions and, when no record exists, estimates hook coverage the same way rtk session does, so hook-rewritten commands land in the already-covered bucket (reported separately as estimated) instead of missed savings, and RTK_DISABLED= bypasses get their own bucket. On a transcript with grep -n ..., gh pr checks ... and node script.js, discover now reports two already-RTK commands and only node as missed. If you still see supported commands counted as missed on the latest version, comment here or reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:cliarea:docsbugdocumentationeffort-smallgood first issuehelp wantedpriority:highresolved-pending-close

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions