Skip to content

rtk discover reports false positives - doesn't account for hook-rewritten commands #929

Closed as duplicate

Description

@MoulinLouis

Summary

rtk discover scans Claude Code JSONL session logs to identify commands that could benefit from RTK filtering. However, these logs record commands before the PreToolUse hook rewrites them - so discover never sees the rtk prefix and reports them all as missed.

This makes discover metrics unreliable for anyone using the hook-based setup from rtk init -g.

Repro

  1. Install RTK with rtk init -g (hook-based setup)
  2. Run a Claude Code session with several Bash commands
  3. Verify commands are processed: rtk gain shows them correctly
  4. Run rtk discover --since 1 - the same commands show up as missed

Example

$ rtk gain
Total commands:    420

$ rtk discover --since 2
Scanned: 133 sessions (last 2 days), 1134 Bash commands
Already using RTK: 7 commands (0%)

MISSED SAVINGS -- Commands RTK already handles
  git diff    242    ~100.2K tokens
  find        209    ~63.5K tokens
  grep -n     161    ~33.4K tokens
  ...
Total: 719 commands -> ~209.5K tokens saveable

420 commands went through RTK according to gain, but discover only finds 7 because the JSONL stores the pre-rewrite command.

Root cause

Claude Code logs the original tool_input.command to the session JSONL, not the rewritten version from the hook output. discover pattern-matches against these raw entries and never finds an rtk prefix.

Suggestion

Cross-reference discover results with RTK's own execution history, or flag in the output that hook-rewritten commands can't be detected from JSONL alone.

Related

Activity

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

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟡 Priority medium
    🎯 Confidence 87%

    Summary

    rtk discover miscounts RTK adoption because JSONL logs store pre-hook commands; fix requires cross-referencing RTK's own execution history


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

  3. Necmttn commented on Jun 21, 2026

    @Necmttn

    I think the important distinction here is covered by a hook vs missed opportunity. A one-line fix that treats every Classification::Supported raw command as already using RTK would match the session adoption view, but it would also make rtk discover stop finding real misses from sessions where the hook was not installed, excluded, disabled, or bypassed.

    Repo-grounded read of current develop:

    • src/discover/mod.rs only increments already_rtk in the Classification::Ignored branch when the raw part starts with rtk . Any Classification::Supported command is aggregated into the MISSED SAVINGS table.
    • src/analytics/session_cmd.rs already encodes the opposite semantics for an adoption lens: part.starts_with("rtk ") || matches!(classify_command(part), Classification::Supported { .. }). Its tests explicitly cover the hook-rewrite case where Claude JSONL logs git status even though the hook ran rtk git status.
    • src/core/tracking.rs has the execution-side evidence: commands.timestamp, original_cmd, rtk_cmd, and project_path. Existing public recent-history helpers only expose timestamp, rtk_cmd, savings fields, so discover probably needs a narrow read-only lookup rather than reusing get_recent_filtered as-is.
    • src/discover/provider.rs currently keeps command, session_id, output, error state, and sequence_index, but drops the Bash tool_use id and any JSONL timestamp if present. Keeping those would give discover better join keys and diagnostics.
    • src/hooks/hook_audit_cmd.rs can also prove rewrite activity when RTK_HOOK_AUDIT=1 is enabled, but the tracking DB is the better default because it is always part of RTK execution accounting.

    A durable fix could split the report into explicit buckets instead of forcing a binary already_rtk/missed result:

    Already using RTK (explicit prefix)
    Covered by RTK tracking (hook/proxy execution observed)
    Potential missed savings
    Ambiguous hook-rewrite candidates
    RTK_DISABLED bypass
    

    Concrete implementation shape:

    1. Extend ExtractedCommand with optional provenance: tool_use_id, observed_at from the JSONL line when present, session_path or derived project slug, and perhaps the raw session file mtime as a fallback. Keep sequence_index; it is useful when timestamps are missing.

    2. Add a tracker query shaped for this use case, for example Tracker::find_execution_match(original_cmd, project_path, observed_at, window). It should match on original_cmd = raw part or rtk_cmd = expected equivalent, scoped by project path and a small time window when possible. Exact command-only matching will over-count common commands like git status and grep.

    3. In discover, classify supported commands roughly like this:

    if raw part starts with "rtk ": explicit already_rtk
    else if RTK_DISABLED prefix: bypass bucket
    else if tracker match exists: covered_by_tracking
    else if Claude hook appears installed/current: ambiguous_hook_rewrite
    else: missed savings
    

    hook_check::status() can probably supply the Claude hook status; AgentIntegrationStatus currently covers Cursor/Hermes/Copilot notes but not the Claude rewrite hook state that this issue hinges on.

    1. Do not make session_cmd::count_rtk_commands the discover rule directly. I would factor a shared lower-level helper that returns a semantic enum like ExplicitRtk, SupportedRaw, Ignored, Unsupported, DisabledByEnv, then let session_cmd and discover interpret SupportedRaw differently. That prevents the two views from drifting while preserving their different jobs.

    2. Tests I would add:

    • Claude JSONL raw git status plus tracking row { original_cmd: "git status", rtk_cmd: "rtk git status", project_path: same project } => covered by tracking, not missed.
    • Claude JSONL raw git status with no tracking row and no hook installed => potential missed savings.
    • Claude JSONL raw git status with hook installed but no tracking row => ambiguous hook-rewrite candidate, not confident missed savings.
    • Explicit rtk git status in JSONL => explicit already using RTK.
    • RTK_DISABLED=1 git status => bypass bucket remains separate.
    • Chained commands like cd app && git status && echo done preserve per-part accounting.
    • Subagent JSONL stays intentional: either include it consistently in both discover and tracking comparison, or mark it separately. discover currently walks subagents, while session_cmd filters them out.

    The same lesson shows up in ax: transcript logs are pre-execution intent, while hook/tracking telemetry is execution outcome. Adoption metrics should not infer execution outcome from the transcript alone when a rewrite hook sits between the two.


    Generated with ax.

  4. Yohjisakamoto commented on Jul 22, 2026

    @Yohjisakamoto

    Your false-positive missed-savings report shows why hook-rewritten commands can distort RTK analytics. Tura’s DeepSWE benchmark measures completed tasks and verifier outcomes, finding fewer agent rounds cut about 80% of overhead. Would you evaluate it? https://turaai.net/docs#benchmark-current-test-set-record

  5. KuSh commented on Jul 26, 2026

    @KuSh
    Collaborator

    Hi, should be handled by #3206

  6. KuSh commented on Sep 5, 2026

    @KuSh
    Collaborator

    Closing as a duplicate of #1441, which tracks the same problem with the more complete discussion. Please follow up there; if your case differs in a way that issue does not cover, comment here and it can be reopened.

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:clibugeffort-mediumhelp wantedpriority:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions