Skip to content

feat(base64): add base64 with -d, -i, and -w - #281

Merged
ferdingler merged 3 commits into
strands-agents:mainfrom
ferdingler:feat/base64
Nov 19, 2026
Merged

ferdingler merged 3 commits into
strands-agents:mainfrom
ferdingler:feat/base64

Conversation

@ferdingler

Copy link
Copy Markdown
Collaborator

Description

There was no base64 in 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 base64 with 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 the base64 crate, 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.

sha256sum and md5sum come 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.

  • 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 requested a review from a team as a code owner November 13, 2026 05:15
@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. 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 differential passes, and cargo test --test shell_integration base64 passes 5 of 5.
  • I re-recorded the base64 differential cases against GNU 9.1 and none of the .expected files 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 lookup list, the WASM /bin links, 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: the Err arm also catches write failures, so they're reported as read errors. For example, base64 big > out past the file size limit prints base64: 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 --dec and --wr=5 are rejected, -h is accepted as help, --version is rejected, and so is -w ' 5'.
  • ⚪ Very large -w values are rejected once they overflow usize. Values of 2^63 and above but still within usize add 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: curl and sleep are also missing from the WASM /bin list on main.

Comment thread src/commands/base64.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. 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 prints strands-shell: base64: bad fd 0 and exits 1. The new regression test fails when I undo the fix locally and passes at head.
  • Previous ⚪ (read error wording): fixed. Writing past the file size limit now prints base64: file size limit exceeded.
  • Merge with main: the README count (35), the WASM lookup list and the WASM /bin list all include both base64 and id. base64.rs and 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_stdin fails 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 -d and -di, 59 hand-written padding and garbage shapes, 1,200 random decode inputs, and option and operand errors. Three differences, all expected: the --help text (twice), and base64 - with no stdin attached, which now reports bad fd 0.
  • Release build, by hand: base64 <&-, base64 -d <&-, base64 nope, echo hi | base64, base64 . and base64 small >&- all report an error or produce output as expected.
  • 35 #[command] registrations match 35 WASM lookup arms.

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 prints aGkK and exits 0. The fix didn't cause this (it's the same on 81e4f69), grep behaves the same way, and 2>/dev/null works. Taking stderr only when there's an error to print would avoid it.

@ferdingler
ferdingler merged commit 4b0bdbc into strands-agents:main Nov 19, 2026
32 checks passed
@ferdingler
ferdingler deleted the feat/base64 branch November 19, 2026 02:20
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