Skip to content

fix(read): head -N truncation must be a literal contiguous prefix - #3166

Closed
albatrossflyon-coder wants to merge 1 commit into
rtk-ai:developfrom
albatrossflyon-coder:fix/head-truncate-window
Closed

albatrossflyon-coder wants to merge 1 commit into
rtk-ai:developfrom
albatrossflyon-coder:fix/head-truncate-window

Conversation

@albatrossflyon-coder

Copy link
Copy Markdown
Contributor

Summary

Fixes #3046.

rtk read --max-lines N — what head -N (and cat/tail via the same rule) rewrites to — is supposed to carry the exact same contract as head itself: the literal first N lines, unchanged. Instead, smart_truncate scanned the entire file and pulled in any line matching an "important" pattern (function signatures, imports, pub/export prefixes, or a bare }/{) regardless of how far past the requested window it actually was, capping total shown lines at N with no marker indicating where lines were skipped from the middle.

Concretely, for a 154-line file with head -30: a closing } from line 63 got pulled in (it matched the bare-brace "important" rule) while its own function header on line 61 — which didn't match any of the importance patterns — got silently dropped. The output showed that stray } sitting directly after an early, unrelated line with nothing indicating a gap existed. The agent reading it concluded the file had a genuine orphaned brace and nearly edited the user's real shell config to "fix" a defect that was actually just missing context from head's own output.

This is the same category of bug the project's own design principle calls out directly (CONTRIBUTING.md § Transparency): RTK's output must be "a valid, useful subset of the original tool's output, not a different format the LLM wouldn't expect." A head-shaped request answered with a non-contiguous "greatest hits" sample is exactly that kind of invented format.

Fix: replace the importance-based scan with a plain contiguous slice of the first max_lines - 1 lines plus the existing single trailing marker — matching the tail-lines path in the same function, which was already implemented correctly as a genuine contiguous slice.

Test plan

rtk read --max-lines N (what head -N/cat rewrite to) is supposed to
have the exact same contract as head itself: the literal first N
lines, unchanged. smart_truncate instead scanned the *entire* file and
pulled in any line matching an "important" pattern (function
signatures, imports, pub/export prefixes, or a bare `}`/`{`) regardless
of how far past the requested window it was, then capped total shown
lines at N with no marker showing where lines were skipped from the
middle.

For a 154-line file with `head -30`, this pulled a closing `}` in from
line 63 while its own function header (which didn't match any
"important" pattern) got silently dropped — producing output where an
unrelated brace sat directly after an early line with nothing
indicating a gap. An agent reading that output concluded the file had
a genuine orphaned brace and nearly edited the user's real config to
"fix" it.

Replaces the importance-based scan with a plain contiguous slice of
the first (max_lines - 1) lines plus the existing single trailing
marker, matching the tail-lines path (which was already correct) and
satisfying the actual contract of a bounded read: exactly what's
there, nothing invented, nothing silently reordered.

Fixes rtk-ai#3046
@KuSh

KuSh commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks for tracking this down with such a clear reproduction. #3941, merged on 2026-09-14, fixed the reported symptom a different way: head -N is no longer rewritten to rtk read --max-lines N at all, it now goes through a new --head-lines N that slices a byte-exact first-N-lines window, and --max-lines was deliberately kept as the structural preview. Its help text on develop now reads "keeps signatures and imports; not the first N lines, use --head-lines for that", so making smart_truncate a contiguous slice would remove behaviour develop now documents on purpose, which is why this cannot land as written. What develop still misses is the other half of open issue #3421: the structural preview drops lines from the middle with no gap marker, only a trailing [N more lines] at src/core/filter.rs:320-363, and a narrow PR that adds gap markers there would be very welcome. If a case your patch covers has been missed here, please comment and this can be reopened.

@KuSh KuSh closed this Sep 19, 2026
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.

head -N rewritten to whole-file compressed view folds structure without inline markers — false-positive "orphaned }" nearly triggered a wrong edit

2 participants