`rtk read --max-lines N` outputs only N/2 lines — `head -N` hook rewrite silently under-delivers half the requested lines
Environment
- rtk 0.38.0 and 0.44.2 (both reproduce)
- macOS (Darwin 25.5.0, arm64), Homebrew install
- Claude Code, hook:
PreToolUse→matcher: "Bash"→rtk hook claude
Summary
--max-lines N emits exactly N/2 content lines (plus a [K more lines] marker), not N. Because the Claude Code hook rewrites head -N <file> to rtk read <file> --max-lines N, every agent head -N receives half the lines it asked for.
Repro
$ python3 -c "open('long.txt','w').write(''.join(f'line {i}: some content here\n' for i in range(3000)))"
$ for n in 5 10 20 50 100 200; do
echo "max-lines $n -> $(rtk read long.txt --max-lines $n | grep -c '^line ')"
done
max-lines 5 -> 2
max-lines 10 -> 5
max-lines 20 -> 10
max-lines 50 -> 25
max-lines 100 -> 50
max-lines 200 -> 100
Expected (matching head -50): 50 lines. Actual: 25 lines + [2975 more lines].
Hook path that makes this user-visible:
$ rtk hook check 'head -50 big.log'
rtk read big.log --max-lines 50 # → agent gets 25 lines
--tail-lines N is exact (verified against tail -N, byte-identical); only --max-lines halves.
Impact
- Any agent running
head -N filesilently getsN/2lines. The truncation marker is visible in standalone use, but the count contradicts what was requested. - On 0.38.0 this also poisoned pipelines (
head -200 f | grep X→ grep searched only 100 lines and the marker was consumed by grep → silent false negative). 0.44.2 no longer rewritesheadinside pipelines, which contains that failure mode — but the standalone halving remains.
Additional observation (savings accounting)
rtk read ... --max-lines/--tail-lines invocations are logged to history.db with original_cmd synthesized as plain cat <file> (flags erased), input_tokens = the full file, and the difference booked as savings. Example measured entry: a --max-lines 20 read of a 1.27 MB file logged as cat <file>, input=318,445 tokens, output=119, savings 99.96%. A raw head -20 would have produced the same small output without rtk, so the full-file baseline overstates real savings. In our 3-month ledger, 205 such entries account for 71.5M of 79.4M total rtk read "saved" tokens (~90%). Related in spirit to #3339 (different mechanism). Happy to split this into its own issue if you prefer.
wshm · Automated triage by AI
🔍 Automated Triage
| 🐛 Category | bug |
| 🟠 Priority | high |
| 🎯 Confidence | 95% |
Summary
rtk read --max-lines N truncates to N/2 lines (not N), so the head -N hook rewrite silently delivers half the requested content to agents; --tail-lines is unaffected.
This looks like a simple fix that could be auto-resolved. Repo maintainers can use
/wshm fixto attempt it.
📁 Relevant files
src/read.rs
Triaged automatically by wshm · This is an automated analysis, not a human review.
I'd like to work on this. Investigating the --max-lines halving bug in \src/read.rs\ and will open a PR with a fix + regression test.
Still reproduces on rtk 0.48.0 (Homebrew bottle, macOS arm64), and on a tiny file it is worse than N/2: --max-lines 3 on a 5-line file returns a single line, and the [K more lines] marker over-counts.
$ printf 'line1\nline2\nline3\nline4\nline5\n' > five.txt
$ rtk --version
rtk 0.48.0
$ head -3 five.txt
line1
line2
line3
$ rtk read five.txt --max-lines 3
line1
[4 more lines]
$ rtk read five.txt --max-lines 1
line1
[5 more lines]
--max-lines 1 prints 1 line and claims 5 remain (only 4 do). --tail-lines 2 is correct (line4 line5).
Larger case: rtk read src/profile.rs --max-lines 3 on a 354-line Rust file prints 2 lines + [352 more lines].
Until fixed, an agent that runs head -N through the hook silently loses content, so I've excluded head/tail/cat/wc in exclude_commands on my side.
Fixed by #3941, shipped in v0.50.0: head -N file now rewrites to rtk read file --head-lines N, which prints exactly the first N lines. --max-lines remains a structural preview, as rtk read --help describes, and the savings accounting point is tracked in #3339. Please reopen if head -N still returns fewer lines on the latest version.