Skip to content

feat(head): print all but the last N lines or bytes with -n -N and -c -N - #218

Merged
ferdingler merged 1 commit into
strands-agents:mainfrom
ferdingler:feat/head-negative-count
Nov 12, 2026
Merged

ferdingler merged 1 commit into
strands-agents:mainfrom
ferdingler:feat/head-negative-count

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

head -n -2 is supposed to print everything except the last 2 lines, but we couldn't parse a negative count, so it just failed:

$ printf 'a\nb\nc\nd\n' | head -n -2
strands-shell: head: cannot parse argument "-2": invalid digit found in string

Now -n -N prints all but the last N lines, and -c -N does the same for bytes, including the attached and long forms (-n-2, --lines=-2, --bytes=-3). The legacy head -5 form and positive counts work exactly as before.

I compared each case with GNU head on Linux, including -0 (prints everything), a count bigger than the input (prints nothing), a last line without a newline, and mixing -n and -c. The output matched in every case. I left out multiple files with headers, because our head only takes one file today and that's a separate feature.

Related Issues

Part of #207

Documentation PR

N/A

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • 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.

@ferdingler
ferdingler force-pushed the feat/head-negative-count branch from ea04c53 to db19b13 Compare November 12, 2026 20:13
@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 20:13
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 20:13
@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. -n -N / -c -N match GNU head 9.1 in every case we threw at it. No blocking or should-fix findings.

One optional follow-up: the GNU-parity claim was checked by hand. A tests/differential/cases/head_all_but_last.sh (recorded against bash + coreutils, next to head_tail_bytes.sh) would lock it in. For example: -n -2, -n -0, -n -1 on a file with no trailing newline, and -c -2.

While testing I found a pre-existing bug in the sibling command: tail -n -2 silently prints 10 lines (tail.rs:25, unwrap_or(10)). Filed as #253. It's out of scope here.

✅ What I verified
  • Head db19b13 on base d4c57a7, Rust 1.100.0 aarch64.
  • cargo test --test shell_integration head: 27 passed, including all 13 new tests. cargo test --lib --bins: 70 passed. cargo clippy --all-targets: no warnings in head.rs or the new test lines. The existing warnings come from the newer toolchain. I didn't run cargo fmt because rustfmt wasn't installed. Log: strands-agents/shell/pr/218/cargo-test.log in the artifacts portal.
  • Output bytes and exit status match /usr/bin/head (GNU 9.1) for:
    • the separate, attached and long forms of -n/-c, plus --lines -2;
    • -0, a count larger than the input, empty input, no trailing newline, and CRLF;
    • non-UTF-8 input on the new line path;
    • mixing options in both orders, and legacy head -5 -n -1;
    • -n -+1, and the error values --1, -, -x, ' -1';
    • -- -file, a missing file, and -n -0/-c -0 on a file;
    • -c -N around the 8 KiB chunk boundary (N = 1, 8191, 8192, 8193, 16384, 19999, 20000, 20001 on 20000 random bytes, read as a file and via cat, compared by md5);
    • 200 to 300 KB single lines;
    • tail -5 and tail -n +2, which share legacy_line_count, still work.
  • Performance: on a 9.4 MB file, -c -4000000 and -n -10 run as fast as the positive forms.

Routing: I ran a correctness/tests pass and an adversarial pass. I skipped the API pass because there's no public API, binding or model-facing change, only the head help text. The first adversarial run timed out, so I retried it with a narrower scope against a prebuilt binary, and it finished.

Appendix: non-blocking (5)
  • ⚪ head -c -18446744073709551615: GNU errors with "Value too large", while this prints nothing. Nobody realistically passes that count.
  • ⚪ Pre-existing, positive counts do the same:
    • size suffixes (-c -1K) aren't parsed;
    • - isn't read as stdin;
    • a closed downstream pipe prints head: pipe closed to stderr.
  • ⚪ Pre-existing: the positive -n path still uses read_line into a String (head.rs:58-61), so it fails on invalid UTF-8. The new negative path uses read_until, which avoids this.
  • Pre-existing, filed #253: tail -n -2 and tail -n abc silently fall back to defaults (tail.rs:23-25).

@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks for checking the raw bytes and the chunk boundaries. The new integration tests already cover those four suggested cases, including negative zero and a final line without a newline. A recorded GNU fixture would make the reference behavior easier to maintain, so that makes sense as a follow-up.

Thanks for filing #253 for tail. The existing suffix/stdin/positive-count UTF-8 gaps and the extreme-count validation can stay separate from this negative-count change.

@ferdingler
ferdingler merged commit ecd73d5 into strands-agents:main Nov 12, 2026
31 of 60 checks passed
@ferdingler
ferdingler deleted the feat/head-negative-count branch November 12, 2026 23:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants