Repository navigation
fix(read): head -N truncation must be a literal contiguous prefix - #3166
albatrossflyon-coder wants to merge 1 commit into
Conversation
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
|
Thanks for tracking this down with such a clear reproduction. #3941, merged on 2026-09-14, fixed the reported symptom a different way: |
Summary
Fixes #3046.
rtk read --max-lines N— whathead -N(andcat/tailvia the same rule) rewrites to — is supposed to carry the exact same contract asheaditself: the literal first N lines, unchanged. Instead,smart_truncatescanned the entire file and pulled in any line matching an "important" pattern (function signatures, imports,pub/exportprefixes, 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 fromhead'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." Ahead-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 - 1lines plus the existing single trailing marker — matching thetail-linespath in the same function, which was already implemented correctly as a genuine contiguous slice.Test plan
test_smart_truncate_no_annotationsfor the corrected line count (was asserting the buggymax_lines/2behavior)test_smart_truncate_does_not_pull_in_later_important_lines— directly reproduces the head -N rewritten to whole-file compressed view folds structure without inline markers — false-positive "orphaned }" nearly triggered a wrong edit #3046 shape (a bare}past the window) and asserts it's no longer pulled intest_smart_truncate_overflow_count_exact(kept-count + reported-more == total invariant) needed no changes — still holds under the new contiguous logiccargo test— 2472 passed, 0 failedcargo fmt --check— cleancargo clippy --all-targets— no issues