Skip to content

feat(sort): add -h/--human-numeric-sort#204

Merged
julesmcrt merged 4 commits into
jules.macret/host-remediation/sort-hfrom
jules.macret/host-remediation/sort-human-numeric
Apr 30, 2026
Merged

feat(sort): add -h/--human-numeric-sort#204
julesmcrt merged 4 commits into
jules.macret/host-remediation/sort-hfrom
jules.macret/host-remediation/sort-human-numeric

Conversation

@julesmcrt

Copy link
Copy Markdown
Collaborator

Summary

  • Implements GNU sort's -h/--human-numeric-sort ordering: by sign (negative, zero, positive), then SI suffix magnitude (none < K/k < M < G < T < P < E < Z < Y), then by numeric value within the same suffix.
  • Multi-letter suffixes (`Ki`, `Mi`) are NOT recognised — only the single byte directly following the digits is consumed, matching GNU sort.
  • Rejects `-hn` and `-dh` (GNU compat).

This is part of a stack enabling the host-remediation demo. The canonical pipeline du -sh /var/* | sort -rh now matches the standard incantation. Stacks on top of #203 (which freed the -h slot from --help).

Test plan

  • make fmt clean (`gofmt -l .` shows no output).
  • `go test ./builtins/sort/... ./tests/...` passes — including 9 new scenarios under `tests/scenarios/cmd/sort/flags/human_numeric_*.yaml`:
    • basic mixed suffixes (1.2G, 500M, 20K, plus no-suffix and equal-suffix tie-break)
    • long-form flag `--human-numeric-sort`
    • reverse `-rh`
    • tie-break by numeric value within same suffix
    • no-suffix vs suffixed ordering
    • negatives (sign category — neg < zero < pos; magnitude reversed within negatives)
    • combined with `-u`
    • `du -sh`-style golden path with size + path columns
    • `-hn` rejection
  • New unit tests in `builtins/sort/human_numeric_test.go` covering the parser (`parseHumanParts`) and the comparator (`compareHuman`) for: SI suffix order, fractions, negatives, no-suffix, leading blanks, leading-zero stripping, unparseable inputs, '+' as non-sign, and lowercase non-`k` suffixes (not recognised).
  • All existing sort tests still pass.
  • `RSHELL_BASH_TEST=1 go test ./tests/ -run TestShellScenariosAgainstBash -timeout 120s`: blocked locally on a Docker file-sharing issue (`bash: /work/runner.sh: No such file or directory`) that is independent of this change. CI should still validate.

Notes

  • Updated `SHELL_FEATURES.md` to advertise the new flag and SI suffix list.
  • Removed the now-stale `tests/scenarios/cmd/sort/help/short_help_flag.yaml` scenario (it asserted `-h` was rejected; that contract is replaced by the human-numeric scenarios above).

🤖 Generated with Claude Code

Implements GNU sort's human-numeric ordering: sorts first by sign
(negative, zero, positive), then by SI suffix magnitude (none < K/k <
M < G < T < P < E < Z < Y), then by numeric value within the same
suffix. Multi-letter suffixes ("Ki", "Mi") are not recognised — only
the single byte directly following the digits is consumed, matching
GNU sort.

This unlocks the canonical disk-space pipeline `du -sh ... | sort -rh`
for the host-remediation demo. Combines normally with -r, -u, -k, -t,
-b, and -f. Rejects the GNU-incompatible combinations -hn and -dh.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@julesmcrt
julesmcrt force-pushed the jules.macret/host-remediation/sort-human-numeric branch from f5a0087 to 8ff4ff7 Compare April 30, 2026 09:20
@julesmcrt

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8ff4ff7d8b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread builtins/sort/sort.go
Comment thread builtins/sort/sort.go Outdated
Two GNU-compat fixes from codex review on #204:

1. sort -fh now folds case before the human-numeric parser runs, so
   lowercase SI suffixes (1m, 1g, ...) are recognised as their
   uppercase counterparts. Previously they fell through as no-suffix
   values and sorted incorrectly.

2. sort -h -n is accepted when every -k spec carries its own ordering
   option (matching GNU's deferred -dn-style validation). The
   unconditional -hn check ran before key parsing and rejected
   otherwise valid command lines like `sort -h -n -k1,1h`.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@julesmcrt

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b3027e6656

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/scenarios/cmd/sort/flags/human_numeric_basic.yaml Outdated
AGENTS.md requires |+ block scalars for input.script, expect.stdout,
and expect.stderr — even when the value is single-line or empty.
Convert the empty stdout/stderr expectations in the new human_numeric
scenarios to |+. Caught by codex on #204.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@julesmcrt

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 98b4365cb5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread builtins/sort/sort.go
GNU sort's full SI suffix table is "kKMGTPEZYRQ". My initial
implementation stopped at Y, leaving R and Q at order 0 — so values
like 1R/1Q would sort BEFORE 1K instead of above 1Y. Add the missing
two suffixes, update the docs, and add unit + scenario coverage.

The new ronna_quetta scenario is marked skip_assert_against_bash
because debian:bookworm-slim ships coreutils 9.1 (predating R/Q,
which landed in 9.4). Caught by codex on #204.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@julesmcrt

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@julesmcrt
julesmcrt marked this pull request as ready for review April 30, 2026 10:25
@julesmcrt
julesmcrt merged commit 83db863 into jules.macret/host-remediation/sort-h Apr 30, 2026
1 check passed
@julesmcrt
julesmcrt deleted the jules.macret/host-remediation/sort-human-numeric branch April 30, 2026 10:49
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