Skip to content

fix(read): don't count --max-lines truncation as token savings (#1045)#1076

Open
ousamabenyounes wants to merge 3 commits intortk-ai:developfrom
ousamabenyounes:fix/issue-1045-max-lines-false-savings
Open

fix(read): don't count --max-lines truncation as token savings (#1045)#1076
ousamabenyounes wants to merge 3 commits intortk-ai:developfrom
ousamabenyounes:fix/issue-1045-max-lines-false-savings

Conversation

@ousamabenyounes
Copy link
Copy Markdown
Contributor

Summary

  • rtk read file --max-lines N was comparing the full raw file against the truncated output in timer.track(), inflating reported savings by the entire skipped portion — 250M+ tokens reported for a single command on large files
  • Fix: capture the post-filter, pre-truncation content and use it as the tracking baseline when --max-lines or --tail-lines is active
  • Same fix applied to run_stdin()

Root cause

// BEFORE — bug
timer.track("cat file", "rtk read", &content, &rtk_output);
//                                   ^^^^^^^^   ^^^^^^^^^^
//                                   11MB file  5 lines → fake 2.8M token "savings"

// AFTER — fix
let tracking_output = filtered_for_tracking.as_deref().unwrap_or(&rtk_output);
timer.track("cat file", "rtk read", &content, tracking_output);
//                                             ^^^^^^^^^^^^^^
//                                             pre-truncation filtered content → real savings only

Test plan

4 new regression tests in src/cmds/system/read.rs:

  • test_max_lines_tracking_uses_pretruncation_content — tracking baseline equals full filtered content, not truncated slice
  • test_tail_lines_tracking_uses_pretruncation_content — same invariant for --tail-lines
  • test_no_truncation_tracking_unchanged — without truncation flags, behaviour is identical to before
  • test_savings_inflation_is_zero_with_fix — numerical proof: buggy code reports >1k fake tokens, fix reports 0
cargo test: 1347 passed, 0 failed

Fixes #1045

🤖 Generated with Claude Code

ousamabenyounes and others added 3 commits April 3, 2026 21:14
Ready-to-use Claude Code PreToolUse hook that intercepts destructive
CLI operations before execution:

- rm -rf on root/home/parent → block
- rm -rf on non-safe dirs → ask confirmation
- git push --force (but allows --force-with-lease) → block
- git reset --hard, clean -f, checkout -- ., branch -D → ask
- Secrets exposure (cat .env, .pem, API keys in commands) → block
- Destructive DB ops (DROP, TRUNCATE, DELETE ALL) → block
- Docker mass removal / system prune -a → ask

Container-aware: skips host-path checks inside docker/kubectl exec
since paths resolve inside the container, not the host.

See rtk-ai#1007 for design discussion.

Co-Authored-By: Claude <noreply@anthropic.com>
39 test cases covering all categories:
- File ops: rm -rf block/ask, safe dirs allowed, container exec bypass
- Git: force-push block, force-with-lease allowed, destructive ops ask
- Secrets: .env/.pem/.key blocked, API keys in commands blocked
- Database: DROP/TRUNCATE blocked, SELECT allowed
- Docker: prune -a ask, normal commands allowed
- Edge cases: empty input, safe commands pass through

Co-Authored-By: Claude <noreply@anthropic.com>
…i#1045)

When --max-lines or --tail-lines is active, timer.track() was comparing
the full raw file against the truncated output, inflating savings by the
entire skipped portion (250M+ tokens reported for large files).

Fix: capture post-filter, pre-truncation content and use it as the
tracking baseline when a line window is active. Savings now reflect only
RTK's actual compression, not user-requested truncation.

Same fix applied to run_stdin().

Adds 4 regression tests covering max-lines, tail-lines, no-truncation
(unchanged behaviour), and a numerical proof that the bug inflated
savings >1k tokens while the fix reports 0 for uncompressed content.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@ousamabenyounes ousamabenyounes changed the base branch from master to develop April 7, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rtk read: truncation via --max-lines incorrectly counted as token savings, producing 250M+ saved tokens in one command

1 participant