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
-
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" }]
}]
}
}
-
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/"}}}
-
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
-
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
Summary
rtk discoverreports 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.discoverscans the pre-hook text and doesn't account for the rewrite, producing inflated numbers.Reproduction
Configure a PreToolUse hook in Claude Code
settings.jsonthat rewrites commands viartk hook claude:{ "hooks": { "PreToolUse": [{ "matcher": "Bash", "hooks": [{ "type": "command", "command": "/path/to/rtk-unified.sh" }] }] } }The hook correctly rewrites commands — verified by piping test inputs through
rtk hook claude:Run
rtk discover --all --since 7:But
rtk gainconfirms those commands ARE being processed through RTK:Root cause
Claude Code session transcripts (
.jsonl) logtool_input.command— the pre-hook command. The hook emitsupdatedInputto rewrite it, but the transcript preserves the original.rtk discoverscans the original and doesn't check whether a hook rewrite occurred.Suggested fix
One or more of:
rtk hook claudefrom the "missed" countrtk gaindata to filter already-processed commandsEnvironment
rtk hook claude