Skip to content

fix(read): preserve context across truncated source - #3689

Open
k4its1t wants to merge 4 commits into
rtk-ai:developfrom
k4its1t:fix/read-honest-truncation
Open

k4its1t wants to merge 4 commits into
rtk-ai:developfrom
k4its1t:fix/read-honest-truncation

Conversation

@k4its1t

@k4its1t k4its1t commented Aug 24, 2026 •

Copy link
Copy Markdown

Summary

rtk read --max-lines N can remove the body of a guard and display its braces together, making the shortened code appear to mean something different. Mark every omitted span at its actual position, and preserve source line numbers for numbered truncation and tail windows. Also retain nonempty stdin when a source filter removes all of it.

The head rewrite, byte-faithful head/tail windows, zero-budget handling, chunked head reading, and quoted tracking labels remain as upstream implements them.

Closes #3421

Upstream sync

Normally merged develop 5eb0509. The CLI's gap-aware selection remains in src/core/filter.rs, using the shared rtk_text import/signature predicates and the new language-independent function signatures. The shared text and engine window behavior is unchanged. The diff remains limited to the four read/filter/test files.

The original binary regressions now use upstream's common::rtk_command() so their children have isolated data directories. The existing numbered-tail expectation remains source line 4 rather than renumbered line 1.

Verification

On Rust 1.99.0:

  • Format, workspace Clippy, and diff checks passed.
  • Read unit tests: 32 passed, 3 ignored. Filter unit tests: 28 passed.
  • Both read integration suites: 9 tests passed.
  • The upstream test-isolation guard passed after adapting the original test constructor.
  • Final cargo test --locked --quiet: 4,664 tests passed, 8 ignored, no failures. No tests were skipped beyond the upstream ignored tests, and no timeout settings changed.

OpenAI Codex assisted with implementation, conflict resolution, and verification.

Emit omission markers at each discontinuity and preserve source line numbers through max and tail windows. Handle zero-line windows safely and give stdin the same non-empty filter fallback as file reads.
@CLAassistant

CLAassistant commented Aug 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@HetCreep HetCreep left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built fix/read-honest-truncation at b93dd68 and ran the full verification independently (release build, cargo test --bin rtk, cargo test --all, plus the original #3421 reproducer by hand).

All three core problems from #3421 are genuinely fixed:

  • Gap markers now appear at every real discontinuity, not just at the end. Re-running the auth.rs / --max-lines 10 example from the issue: the is_banned guard's dropped return false; now shows [1 line omitted] right where it was removed, instead of silently vanishing into if user.is_banned { }.
  • -n now prints the file's real line numbers for kept lines (omission markers left unnumbered), not a sequential kept-line count.
  • --max-lines 0 returns cleanly (empty output, exit 0) — no underflow, no hang.
  • cargo test --bin rtk: 2623 passed, 0 failed, 8 ignored. cargo test --all: 17 passed, 0 failed, including the new read_integrity_test.rs (truncation_marks_removed_guard_body_at_the_gap, numbered_truncation_uses_source_line_numbers, zero_line_window_is_empty_without_panicking, stdin_falls_back_when_filter_removes_nonempty_input).

Nice work — the inline-marker approach is exactly what the issue asked for, and the regression coverage is real (I ran it, not just read it).

One thing worth flagging for @rtk-ai maintainers before merge, not a correctness objection: this PR's diff is wider than its own description states ("keep the change scoped to read/smart_truncate"). Alongside the read.rs/filter.rs changes, it also touches:

  • src/cmds/git/git.rs (+466/-…) — a git log -- <path> fix citing #1215 (a real, closed issue — the trailing_var_arg/-- bug apparently never got the same fix git log that git diff got). Comes with its own regression test (git_log_dash_p_pathspec_after_double_dash_is_not_patch_flag), which I also ran — passes.
  • src/core/tee.rs (+45/-…) — reworks slug truncation for tee recovery filenames (readable-prefix + hash instead of a hard 40-char cut, to avoid collisions on long embedded paths).
  • scripts/benchmark.sh (+8/-…) — fixes rm -rf "$BENCH_DIR" deleting tracked benchmark-harness files on every local run.

Each of those looks like a legitimate, independently-motivated fix with its own justification in the diff — I'm not saying any of them is wrong. But bundled together with #3421 in one PR, a maintainer reviewing this has to sign off on four unrelated concerns at once, and if any one of them needs discussion the whole PR (including the already-verified #3421 fix) sits blocked. Might be worth splitting git.rs/tee.rs/benchmark.sh into their own PRs so the #3421 fix — which is fully verified and ready — isn't held hostage to review of the other three.

(Disclosure, matching this PR's own AI-assistance note: this review was carried out with AI (Claude) assistance, human-directed — I built the branch and ran every command above myself rather than trusting the PR description.)

@k4its1t

k4its1t commented Aug 24, 2026

Copy link
Copy Markdown
Author

Thanks for independently running the reproducer and full test suites. I rechecked the scope concern against the actual PR head and base: commit b93dd68 has parent 29f9bb7, and both git show 29f9bb7..b93dd68 --stat and GitHub’s PR Files view contain exactly three paths: src/cmds/system/read.rs, src/core/filter.rs, and tests/read_integrity_test.rs. The PR currently reports 3 changed files (359 additions, 64 deletions). src/cmds/git/git.rs, src/core/tee.rs, and scripts/benchmark.sh are not part of this commit or PR diff; they are already present in the base history, so there is nothing from those concerns to split out here.

@HetCreep

Copy link
Copy Markdown

You're right, and I was wrong — sorry for the noise. Root cause on my end: my local develop branch was stale at ba7a9ce (from an earlier sync in this session) while origin/develop had moved to 29f9bb7. I diffed against the stale local branch instead of the real tip, so the diff pulled in everything that landed on develop between those two points — none of which is yours.

Re-ran it properly against origin/develop (29f9bb7, which is in fact b93dd68's actual parent): 3 files, 359 insertions, 64 deletions — src/cmds/system/read.rs, src/core/filter.rs, tests/read_integrity_test.rs. Matches what you posted exactly. The git.rs change I flagged came from PR #3575 (fix/git-log-grep-value-misdetected-as-patch-flag), already merged into develop before your branch forked — nothing to do with this PR. Same story for tee.rs/benchmark.sh.

Retracting the scope note entirely — this PR is scoped exactly as described. The functional verification (build, cargo test --bin rtk 2623/0 failed, cargo test --all 17/0 failed, and the three #3421 repros) stands unchanged; that part didn't depend on the bad diff.

@k4its1t

k4its1t commented Sep 23, 2026

Copy link
Copy Markdown
Author

Merged develop b748a5f normally in 9ab008f. Kept upstream chunked-head reading alongside the existing numbered truncation function. The only extra test adjustment is the numbered-tail expectation (source line 4, not renumbered line 1). Format, Clippy, 3,742 unit tests, and all 9 targeted read integration tests passed. The full run still hits the two previously documented, unrelated Git integration assertions; details are in the refreshed description. No new tests or refactors added. AI-assisted resolution and verification.

@k4its1t

k4its1t commented Oct 15, 2026

Copy link
Copy Markdown
Author

Merged develop 5eb0509 normally in 301ea56. Kept the gap/source-numbering fix in the CLI while reusing upstream's shared recognition predicates and preserving its byte windows, chunked head reading, tracking labels, and engine behavior. The original binary tests now use common::rtk_command() as required by the new isolation guard.

Format, workspace Clippy, focused read/filter tests, and both read integration suites passed. The final full test command passed 4,664 tests with 8 ignored and no failures. The PR remains limited to four files. AI-assisted resolution; no force-push.

@KuSh KuSh mentioned this pull request Oct 31, 2026
7 tasks done
@KuSh

KuSh commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

Heads-up for the rebase: #4647 changes the rtk read -n gutter from │ to a tab, so the two new gutter producers here (numbered lines and the omission marker) and their assertions need the same separator to keep the output uniform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants