Skip to content

fix(tail): reject an unparseable -n count and accept a leading - - #349

Merged
ferdingler merged 4 commits into
strands-agents:mainfrom
ferdingler:fix/tail-n-count-253
Nov 26, 2026
Merged

ferdingler merged 4 commits into
strands-agents:mainfrom
ferdingler:fix/tail-n-count-253

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

When tail -n got a count it couldn't parse, it quietly swapped in a default. So tail -n -2 printed the last 10 lines instead of the last 2, and tail -n abc printed the whole input and exited 0. An agent got a wrong answer with no hint anything was off.

Now tail reads the count the same way head already does: a leading - means the same as no sign, +N still starts at line N, and anything else is an error with status 1, like GNU tail. As in GNU, once any -n count has a +, later counts are start lines too.

Related Issues

Fixes #253

Documentation PR

None

Type of Change

Bug fix

Testing

I added differential cases recorded from bash and GNU coreutils for signed counts, bad counts, and -n given more than once. The first two failed on main and pass with the fix. The third caught a regression in my first version, and it passes now. The full test suite passes.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

tail replaced a count it could not parse with a default, so tail -n -2
printed 10 lines and tail -n abc printed everything with status 0. Parse
the count with head's signed_count, which accepts the optional sign and
propagates the error.

Fixes strands-agents#253
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@github-actions github-actions Bot added the bug Something isn't working label Nov 21, 2026

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Changes requested: the #253 fix itself is correct, but a later -n now cancels an earlier +N, which GNU tail and main don't do. It needs -n twice to hit, and the fix is one character (inline).

Pass Result
Correctness and tests 🔴 1 inline. Both new cases fail on main and pass here.
Adversarial (GNU 8.32 and 9.1 vs main vs PR) Same regression. head is unchanged, no panics or hangs.
Local gates at c1774a4 cargo test --workspace --all-targets: 2733 passed, 0 failed. cargo fmt --check and cargo clippy exit 0.

One non-blocking question: should a count with leading whitespace be accepted? GNU accepts it, and this shell's own wc -l < f produces one (details below).

What I verified, the question, and appendix

Verified

  • Head c1774a4fc2be88c2b8af7b4a590f2015a4323849 against base dbb6189b6c6e4d124732fd228a89f2cef9309d9f.
  • The issue's two repros: tail -n -2 prints 11 12, and tail -n abc exits 1 with a message on stderr. On main they print 10 lines and the whole input with status 0.
  • Copying the two new cases onto main makes cargo test --test differential fail; it passes on the PR.
  • Both .expected files match bash with GNU tail 8.32 and 9.1.
  • head -n and head -c behave the same on main and the PR across about 3,800 fuzzed commands, so making signed_count pub(super) changed nothing there.
  • About 7,600 single -n values compared with GNU: the PR never accepts a value GNU rejects. The only values it rejects that GNU accepts are leading whitespace and multiplier suffixes.
  • Error path: ||, &&, set -e, pipefail, $(...) and subshells behave like GNU after tail -n abc.

Question (non-blocking): leading whitespace in the count

n=$(wc -l < f); tail -n "$n" f now fails with cannot parse argument " 3", because this shell's wc pads a count read from stdin to 7 columns (the recorded known-difference in tests/differential/cases/wc_counts.sh). GNU tail skips leading whitespace and prints the lines. On main this printed the last 10 lines, which was only right for short files, so the error is still an improvement. head already rejects the same input on main. Skipping leading whitespace in signed_count would fix both commands; a follow-up is fine.

Appendix, non-blocking (2)

  • GNU multiplier suffixes (tail -n 1k) are now an error instead of a silent 10 lines. GNU accepts them; head already rejects them.
  • The error text is cannot parse argument "abc": invalid digit found in string rather than GNU's invalid number of lines. It matches head, and the differential rules only compare whether stderr is empty.

Not covered: the Python and Node bindings and the wasm build were not run.

Comment thread src/commands/tail.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approve. My one finding is fixed at 1dca4a7: a later -n keeps an earlier +N, as GNU tail does, and the PR description now says so.

  • Previous finding (repeated -n): fixed. The delta is the |= change, a comment line, and the tail_n_repeated case.
  • Attacking the fix: 8,600 repeated--n commands compared with GNU tail 9.1 found no regression against main or the previous head. The only 4 that differ are the legacy tail -5 -n N form, which behaves the same on main.
  • Gates at 1dca4a7: cargo test --workspace --all-targets 2733 passed, 0 failed; cargo fmt --check and cargo clippy exit 0.

I don't have permission to resolve the inline thread, so it is still open on your side. The leading-whitespace and suffix follow-up you mentioned has no issue tracking it yet.

How the fix was checked
  • Compared bash with GNU coreutils 9.1, main (dbb6189), the previous head (c1774a4) and this head, each run as -c '<script>' in an empty directory against a 12-line file. GNU 8.32 was not rerun this round.
  • Inputs: every ordered pair of 30 count values (plain, +N, -N, zero, out-of-range line numbers, invalid, empty, 2^64-1 and 2^64) across -n V, -nV and --lines=V, with the file before or after the second option, from a file and from a pipe; plus triples of six values and a few legacy and -- forms.
  • Result: 7,616 already matched GNU, 980 match GNU only since this delta, 4 differ the same way on main (tail -5 -n +2 f and tail -5 -n 3 f are accepted; GNU exits 1).
  • Whitespace-padded counts and multiplier suffixes were left out, since those are deferred.
  • Not run: Python and Node bindings, wasm build.

@ferdingler
ferdingler merged commit e7dde62 into strands-agents:main Nov 26, 2026
38 of 39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] tail -n with an unparseable count silently prints 10 lines (e.g. tail -n -2)

4 participants