Skip to content

fix(read): keep true file line numbers under --tail-lines -n - #12

Merged
ryanmurf merged 1 commit into
masterfrom
fix/read-tail-line-numbers
May 30, 2026
Merged

ryanmurf merged 1 commit into
masterfrom
fix/read-tail-line-numbers

Conversation

@ryanmurf

Copy link
Copy Markdown

Problem

rtk read <file> --tail-lines N -n renumbered the surviving tail window starting from 1. A model reading the output was told the last N lines of the file were lines 1..N, when they are actually the final N lines. Example (5-line file):

$ rtk read foo.txt --tail-lines 2 -n
1 │ l4      <- WRONG: this is line 4
2 │ l5      <- WRONG: this is line 5

This is the output-mangling class (same family as the grep #6 / wc #8 / tree #9 / find #10 fixes): a filter corrupts the information the model reasons over — here, the line references it would cite.

Fix

  • apply_line_window now returns (windowed_text, start_offset) where start_offset is the 1-based original position of the first emitted line (total - tail + 1 for a tail window, 1 otherwise).
  • format_with_line_numbers renders the gutter from that offset; gutter width is sized for the largest number actually printed so a high offset (e.g. lines 9–10) stays aligned.
  • --max-lines/smart_truncate keeps a non-contiguous selection starting at line 1, so it correctly stays 1-based (already signals omissions via the [N more lines] marker).

After:

$ rtk read foo.txt --tail-lines 2 -n
4 │ l4
5 │ l5

Tests

Adds a RED-on-revert regression (test_format_line_numbers_with_tail_offset — fails with 1 │ l4 if the offset wiring is reverted) plus edge cases: width across a digit boundary, empty content (no panic), tail=0, tail>len, and the no-window / max-lines offset invariants.

Verification

  • cargo fmt --check clean
  • cargo clippy --all-targets -- -D unsafe_code clean
  • cargo test --all green (1752 + 12 pass)
  • cargo build --release + manual E2E confirm the corrected gutter

`rtk read <file> --tail-lines N -n` renumbered the surviving tail window
from 1, so a model reading the output was told the last N lines were lines
1..N when they are really the final N lines of the file. That mislabels the
line references the model reasons over (the output-mangling class).

apply_line_window now returns the 1-based original position of the first
emitted line (total - tail + 1 for a tail window, 1 otherwise), and
format_with_line_numbers renders the gutter from that offset. Gutter width is
sized for the largest number actually printed so high offsets stay aligned.
max-lines/smart_truncate keeps a non-contiguous top selection, so it stays
1-based. Adds regression + edge-case tests (tail offset, width, empty, tail=0,
tail>len).
@github-actions

Copy link
Copy Markdown

👋 Thanks for the PR! It looks like this targets master, but all PRs should target the develop branch.

Please update the base branch:

  1. Click Edit at the top right of this PR
  2. Change the base branch from master to develop

See CONTRIBUTING.md for details.

@ryanmurf
ryanmurf enabled auto-merge (squash) May 30, 2026 04:10
@ryanmurf
ryanmurf merged commit 95d20f1 into master May 30, 2026
17 checks passed
@ryanmurf
ryanmurf deleted the fix/read-tail-line-numbers branch May 30, 2026 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant