Repository navigation
feat(base64): add base64 with -d, -i, and -w - #281
Conversation
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. The output matches GNU base64 9.1 byte for byte across ~6,500 differential cases. There's one 🟡 below, and it doesn't block.
🟡 If stdin isn't connected (for example, an agent runs base64 with no file and nothing piped in), the command exits 1 and prints nothing, so the agent has no error to act on. There's an inline suggestion; I built it and checked that it fixes this.
@ferdingler, this is the review you asked for.
What I verified
- Head
81e4f69. CI is green on all jobs.cargo test --test differentialpasses, andcargo test --test shell_integration base64passes 5 of 5. - I re-recorded the base64 differential cases against GNU 9.1 and none of the
.expectedfiles changed. - Random decode cases (3,000) and encode cases (1,500, across 10 wrap widths, with a round-trip) give stdout and exit status identical to GNU 9.1.
- An adversarial harness ran 2,021 cases, including chunk boundaries at 3 KiB and 8 KiB, inputs of about 1 MiB, every padding and garbage shape, and
-di. The only 18 differences are in option parsing (listed in the appendix). Decoding a 7 MB file and encoding it again with a redirect to a file reproduces the input exactly, in 0.2 s. - Registration is complete: native inventory, the WASM
lookuplist, the WASM/binlinks, and the README count of 34. - Evidence:
strands-agents/shell/pr/281/{repro-no-stdin.txt, adversarial-harness.py, adversarial-harness-output.txt, perf-output.txt}
Appendix: non-blocking (5)
- ⚪
base64.rs:78: theErrarm also catches write failures, so they're reported as read errors. For example,base64 big > outpast the file size limit printsbase64: read error: file size limit exceeded. The exit status is right; only the wording is misleading. - ⚪ Option parsing differs from GNU in a few places, but sibling commands behave the same way (lexopt): abbreviated long options such as
--decand--wr=5are rejected,-his accepted as help,--versionis rejected, and so is-w ' 5'. - ⚪ Very large
-wvalues are rejected once they overflowusize. Values of 2^63 and above but still withinusizeadd a final newline that GNU doesn't print. Unlikely anyone hits this. - ⚪ Encoding 50 MB through a pipe takes about 13 s, against about 5 s for
cat. That gap comes from the shell's pipe throughput, not this command; writing to a file is fast. - Not part of this PR:
curlandsleepare also missing from the WASM/binlist on main.
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. The fix for my earlier finding works and the merge with main is clean. Nothing new to fix.
- Previous 🟡 (silent exit with no stdin): fixed.
base64 <&-now printsstrands-shell: base64: bad fd 0and exits 1. The new regression test fails when I undo the fix locally and passes at head. - Previous ⚪ (
read errorwording): fixed. Writing past the file size limit now printsbase64: file size limit exceeded. - Merge with main: the README count (35), the WASM lookup list and the WASM
/binlist all include bothbase64andid.base64.rsand the differential cases are unchanged by the merge.
CI on 1d8770f was still running when I posted: the macOS jobs were queued, and every job that had finished was green.
What I verified
Head 1d8770f (merge of main 9851f51 into 5d62b5d), Linux aarch64, GNU coreutils 9.1.
cargo test --test shell_integration base64: 6 passed. With the stderr ordering reverted locally,base64_reports_a_closed_stdinfails with "expected an error on stderr" and the other 5 pass.cargo test --test differential: passes, including the four base64 cases.- A fresh differential harness against GNU 9.1: 3,565 cases comparing stdout bytes, exit status and whether stderr is empty. It covers encode sizes around the 3 KiB and 8 KiB buffer boundaries with 12 wrap widths, decode of wrapped input with
-dand-di, 59 hand-written padding and garbage shapes, 1,200 random decode inputs, and option and operand errors. Three differences, all expected: the--helptext (twice), andbase64 -with no stdin attached, which now reportsbad fd 0. - Release build, by hand:
base64 <&-,base64 -d <&-,base64 nope,echo hi | base64,base64 .andbase64 small >&-all report an error or produce output as expected. - 35
#[command]registrations match 35 WASMlookuparms.
The evidence files from my first review are no longer retrievable, so the harness was rewritten for this pass.
Appendix: non-blocking (1)
- ⚪
src/commands/base64.rs:67: with stderr closed,echo hi | base64 2>&-prints nothing and exits 1, where GNU printsaGkKand exits 0. The fix didn't cause this (it's the same on81e4f69),grepbehaves the same way, and2>/dev/nullworks. Taking stderr only when there's an error to print would avoid it.
Description
There was no
base64in the shell, so an agent couldn't encode a file to paste somewhere or decode a blob it got back from an API. It just got "command not found".This adds
base64with encode,-d/--decode,-w/--wrap(76 by default, 0 for no wrapping) and-i/--ignore-garbage. It reads one file, or stdin when there's no file or the file is-, and works on raw bytes, so binary input and output come through intact. Encoding uses thebase64crate, which was already in the dependency tree, so nothing new is pulled in. Decoding is a small hand-written loop because GNU's decoder has quirks the crate doesn't: it accepts several padded encodings back to back, ignores unused trailing bits, and prints whatever it decoded before it hits bad input. I wanted the output to match byte for byte. I also bumped the command count in the README.I compared about 50 encode/decode cases against GNU coreutils 8.32 on Linux, including binary data, wrap widths, padding edge cases, garbage with and without
-i, and truncated input. Stdout, exit status and whether anything went to stderr all matched. I added differential cases recorded from bash, plus integration tests that round-trip every byte value through files and stdin. The full Rust check passes locally. I couldn't build the WASM target here (no wasi-sdk), but the crate already builds for WASM through jaq.sha256sumandmd5sumcome in separate PRs. All three touch the command registration lists and the README count, so whichever merges second will need a quick rebase.Related Issues
Part of #207
Documentation PR
None
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.