Repository navigation
Description
smart_truncate keeps structurally interesting lines and discards the rest, but it joins the survivors with no marker where the gap was, and appends a single [N more lines] at the end — which implies the omission is at the end. The result is code an agent reads as complete and correct while it says something different.
Provenance: AI-assisted source audit, human-directed, at v0.44.2 (700bdde). Reproduced with the shipped binary; input and output below are verbatim.
Reproduction
auth.rs, 15 lines:
1 fn can_admin(user: &User) -> bool {
2 if !user.is_admin {
3 return false;
4 }
5 if user.is_banned {
6 return false;
7 }
8 if user.mfa_ok {
9 return true;
10 }
11 false
12 }
13 fn wipe_database() {
14 drop_all();
15 }rtk read auth.rs --max-lines 10:
fn can_admin(user: &User) -> bool {
if !user.is_admin {
return false;
}
if user.is_banned {
}
}
}
fn wipe_database() {
[6 more lines]Two things happened:
- The ban check was inverted.
if user.is_banned {survived; itsreturn false;did not. What the agent reads is a guard that does nothing — the opposite of what the file says. Nothing marks the removal. fn wipe_database()was spliced in directly after, with no indication that lines were skipped between them. The only marker,[6 more lines], sits at the bottom and reads as "the rest of the file follows", not "content was removed from the middle".
An agent asked to review this file would conclude that banned users pass the check.
Why this is on a default path
This is not an exotic flag. rtk rewrite head -50 file emits rtk read file --max-lines 50, so the hook routes ordinary head invocations through the same code (src/core/filter.rs:342, reached from src/cmds/system/read.rs). The default filter level is none, and this happens at that level.
Related, same function
src/cmds/system/read.rs:157— with-n, the line numbers count the kept lines 1..N, not the file's real line numbers. In the run above, the row labelled 7 is raw line 9, and the[N more lines]marker itself gets numbered as though it were content. An agent told to "fix line 8" edits a different line than the one it was shown.src/core/filter.rs:349—kept_lines >= max_lines - 1underflows whenmax_lines == 0.Cargo.toml:45setspanic = "abort"but notoverflow-checks, so a release build wraps tousize::MAXand the loop never breaks (a debug build panics withattempt to subtract with overflow). Reachable ashead -0 file→--max-lines 0.
Suggested directions (untested — no programmer has reviewed these)
- Emit a marker where the gap is, not only at the end — e.g.
… 4 lines omitted …inline at each discontinuity. That single change makes the output honest even if the selection heuristic stays as is. - Consider keeping a line's closing brace only when its opening block was kept intact; dropping a body while keeping both braces is what makes the output read as valid.
- Guard
max_lines == 0explicitly rather than relying on- 1.
I would rate the inline gap marker as the important one: an agent can reason correctly about "content was removed here", but it cannot detect a guard clause that was silently emptied.
One thing I could not reproduce
An earlier note in my own review suggested rtk read - (stdin) could return zero bytes for a small error log. I tried it — a 147-byte tsc-style log came back as 146 bytes, exit 0. So that half does not reproduce and I am not claiming it.
Activity
pszymkowiak commentedon Aug 4, 2026
🔍 Automated Triage
bughighSummary
rtk read --max-lines(and theheadrewrite it powers) silently drops mid-file lines with no gap marker and mislabels renumbered lines, producing code that reads as complete/correct but inverts logic — fix by emitting inline gap markers and using real line numbers in filter.rs/read.rs.📁 Relevant files
src/core/filter.rssrc/cmds/system/read.rsTriaged automatically by wshm · This is an automated analysis, not a human review.
HetCreep commentedon Aug 4, 2026
Correcting myself: the stdin case at the bottom of this issue does reproduce. I tested it with the wrong input and wrote it off — apologies.
My earlier attempt used a log whose lines all survive the filter, so of course nothing was dropped. The condition is different: it needs input that the filter empties completely. Here is the same content down two paths:
The file path prints a warning and falls back to the raw content. The stdin path returns nothing, exit 0.
The asymmetry is in the source.
src/cmds/system/read.rs:43-48:run_stdin(read.rs:117-119) applies the filter with no equivalent guard.Other inputs that reach zero bytes through stdin, all exit 0: lines that are entirely
//comments (stdin is treated asLanguage::Unknown, which carries C-style comment patterns), input that is only blank lines, any all-comment input at-l aggressive, and--tail-lines 0.Why it matters here: a caller piping a build or test log into
rtk read -can receive an empty result with a success exit code, which reads as "there was no output" rather than "the filter removed everything". The file path already decided that is the wrong answer —run_stdinjust did not get the same guard.Suggested fix: apply the same fallback in
run_stdin. Since there is no filename to name in the warning, something likefilter produced empty output for stdin (N bytes), showing raw contentwould match the existing message.k4its1t commentedon Aug 24, 2026
I’d like to work on this. I reproduced the permission-guard example against the latest
develop. I’ll add regression coverage first, then make truncation emit omission markers at each real discontinuity, preserve original line numbers, handle zero-line windows without underflow, and give stdin the same non-empty fallback as file reads. I’ll keep the change scoped toread/smart_truncateand run the repository’s full pre-commit gate.albatrossflyon-coder commentedon Sep 13, 2026
Our fix for this exact bug has been open as #3166 since 2026-07-22 with no review. Linking here since this issue is now the canonical tracker and #3046 (where #3166 was filed) just got closed as a duplicate of this one.
HetCreep commentedon Sep 16, 2026
Flagging since two PRs are open for this and they're not just two patches of the same fix — they take different design directions, and both are unmerged and now
dirtyagainst currentdevelop.Provenance: read both PR descriptions and diffs via the API; re-ran the verification for #3689 myself back on 2026-08-24 (built the branch,
cargo test --bin rtkandcargo test --allboth green). Did not re-run #3166 — noting what it says of itself.#3166 (
albatrossflyon-coder, open since 2026-07-22): make truncation literally whatheadpromisesReplaces the "important line" scan with a plain contiguous slice of the first N lines — no line past the window is ever pulled forward, matching
head's actual contract (and the project's own stated principle inCONTRIBUTING.md§ Transparency, which the PR cites).#3689 (
k4its1t, open since 2026-08-24): keep smart selection, make every gap visibleKeeps the existing behavior of surfacing important lines beyond the window, but represents every discontinuity as an explicit omission marker with real source line numbers, so a spliced-together excerpt can no longer be mistaken for a contiguous one. Also separately fixes
--max-lines 0underflowing. I built and ran this one: all three findings this issue's thread raised are genuinely fixed, full suite green (2632 passed, 8 ignored).These aren't redundant — they're different products
#3166treats "look likehead" as the contract to honor.#3689treats "never silently hide a discontinuity" as the contract, and keeps the smart selection because it's often more useful than a raw prefix. Picking one isn't just "which patch is more correct" — it's a call on which behaviorrtk read --max-linesshould have going forward. Not mine to make; just didn't want it decided by whichever PR happens to get looked at first without the other being visible.Both are marked
dirtyright now, so either way needs a rebase before merge regardless of which direction gets picked.HetCreep commentedon Sep 16, 2026
Correction to my comment above — I synced to current
developright after posting it and found the ground had already shifted.Provenance: AI-assisted source audit, human-directed, at
develop@5e0f92c. Both halves below were executed against a binary built from this commit, with the same repro shape (an important line 46+ lines past the requested window).The
headrewrite half is already fixed — landed before my comment above, not by either open PRed8480f("fix(read): make head/tail rewrites faithful to the native commands", 2026-09-08) added a new--head-lines Nwindow that slices on a literal byte offset, and repointed the hook rewrite at it:So the specific shape both open PRs describe —
head -Non a real file producing a spliced, non-contiguous excerpt — no longer happens. My apologies for not catching this before commenting; I synced right after this time and should have synced right before instead.What's actually still open is narrower: direct
--max-lines NThe same commit deliberately left
--max-lines's own behavior alone ("leaving--max-linespreview semantics unchanged for existing callers"). Confirmed that's still exactly the original defect:18 lines total, and the two lines
fn important_but_far() { }are pulled in from 46+ lines past the shown window with no indication anything was skipped in between.So the PR comparison above still applies, just to a smaller surface
Both #3166 and #3689 patch
smart_truncateitself, which is still what--max-linescalls — so neither is moot. But theheadrewrite is no longer the reachable trigger for either: an agent has to type--max-linesexplicitly (or something else has to route through it) to hit what's left. Worth the maintainer knowing the blast radius shrank before deciding between the two directions.HetCreep commentedon Oct 14, 2026
Re-checked on v1.0.0 (stable, 2026-10-13): the official release binary plus the v1.0.0 source. Provenance: AI-assisted check, human-directed. "ran" = executed against
rtk 1.0.0with a control; "read" = verified from source only.Fixed (ran): the
headrewrite.rtk rewrite 'head -10 auth.rs'now givesrtk read auth.rs --head-lines 10, which prints the real contiguous lines.--max-lines 0now returns empty output. Thanks.Still live (ran, on the report's
auth.rs): directrtk read auth.rs --max-lines 10still printsif user.is_banned {,},},},fn wipe_database() {and[6 more lines]with no inline gap marker (same for--max-lines 5and1), so the guard reads as inverted.-nstill numbers the kept lines 1..N, including the[N more lines]marker (src/cmds/system/read.rs:226format_with_line_numbers). Control:--head-lines 10 -nnumbers correctly.