Skip to content

fix(shell): reject invalid head and tail line counts (OPE-100) - #243

Open
aakarim wants to merge 1 commit into
mainfrom
fix/ope-100-line-count-validation
Open

aakarim wants to merge 1 commit into
mainfrom
fix/ope-100-line-count-validation

Conversation

@aakarim

@aakarim aakarim commented Nov 8, 2026

Copy link
Copy Markdown
Owner

Issue

Fixes OPE-100.

Implementation thread: https://ampcode.com/threads/T-3199f5c9-6253-45b6-9ec8-ac369ffab8b6

Diagnosis

The -n branches used unchecked fmt.Sscanf. Missing arguments fell through to shorthand parsing; invalid values left the default count of 10 untouched; numeric prefixes such as 3abc were accepted. Both commands could therefore emit plausible but incorrect slices with exit status 0.

The new regression test failed on unchanged production code: seq 1 15 | head -n returned lines 1–10 with no error, and tail -n + returned lines 10–15. Malformed counts also reproduced the silent fallback for file input.

Fix

  • Require a nonempty argument after -n in head and tail.
  • Parse the entire count with strconv.Atoi, rejecting malformed, overflowing, or negative counts with a diagnostic, exit status 1, and no stdout.
  • Preserve valid positive counts, tail +N selection, byte-mode overrides, and existing shorthand behavior. Negative-count GNU extensions are not implemented, consistent with the existing byte-count validation.
  • Add regression coverage for missing/empty arguments and expansions, partial numbers, bare +, overflow, file input, and exact successful slices.

Pipeline status semantics are intentionally unchanged: a trailing wc still supplies the pipeline status, so check the failing head/tail command itself rather than expecting pipefail.

Verification

Passed in the Nix development environment:

  • go test ./pkg/shell/... -count=1
  • go test -race ./pkg/shell/... -count=1
  • go test ./pkg/openlore -run 'Test(MCPHTTPAPI_Shell|MCPHTTPSessionShellCommandFailureIsResult|MCPShellErrorClassification|ExecShellTranscriptPreservesStreams)' -count=1
  • go vet ./pkg/shell/...
  • go build ./...
  • gofmt check on changed Go files and git diff --check

No UI changes. This PR is left open and unmerged.

Validate the full -n argument before producing output instead of silently falling back to ten lines or accepting a numeric prefix.

Cover missing and empty expansions, malformed and overflowing counts, file input, and valid head/tail slices.

Amp-Thread-ID: https://ampcode.com/threads/T-3199f5c9-6253-45b6-9ec8-ac369ffab8b6
Co-authored-by: Amp <amp@ampcode.com>
@linear-code

linear-code Bot commented Nov 8, 2026

Copy link
Copy Markdown

OPE-100

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.

1 participant