Skip to content

feat(which): add which, resolved like command -v - #272

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:feat/which
Nov 17, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:feat/which

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Agents reach for which all the time to check whether a tool is installed, and the shell didn't have it, so those checks always said the tool was missing even when it would run fine.

This adds which. It finds names the same way command -v does (built-in commands, builtins, names a command fallback serves, and scripts on PATH), so the two never disagree about what exists. Like the real one, it doesn't report shell functions. It takes several names and -a for every match on PATH, exits 0 only when all names are found, and prints nothing for a missing name. For a built-in command like ls it prints ls, the same as command -v, rather than a path. just-bash prints /usr/bin/ls there, but our /bin/ls is only a link back into the shell, and -a still shows it after the bare name.

I added tests for the cases above, including one that checks which and command -v give the same answer and a few with a command fallback. I also ran it by hand against scripts on PATH and a function. The full Rust check passes locally. I didn't run the Python or Node tests since this doesn't touch the bindings.

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:10
@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.

@ferdingler, here's the review you asked for. TL;DR: this is good to merge after one small fix. The command -v/-V refactor behaves exactly as it did before. The one issue is that which gives wrong answers for names that contain / (which /bin/ls and which ./s both exit 1), and the fix is three lines (inline).

✅ What I verified
  • Head 6c0d043, compared against a build of origin/main at 5f80040.
  • cargo test --test shell_integration --test command_fallback gives 1972 + 28 passed. With the inline suggestion applied, I re-ran it and still got 1972 + 28 passed.
  • I checked the refactor's equivalence by reading the code. lookup_command(.., true) keeps the old order: special, builtin, registered command, fallback (only when no function exists), function, hash, PATH. The -V labels are unchanged.
  • I ran more than 20 which probes by hand: -a with dedup, functions skipped, -aa, --, missing names, an empty PATH, an unset PATH, a directory on PATH, and a stale hash entry (matches command -v). All behave as described in the PR.
Questions (non-blocking)
  • Bare name for built-ins. Agents often use the output as a path: x=$(which ls); [ -x "$x" ] is false, and so is ls -l "$(which ls)". When a /bin/<name> link exists and is first on PATH, printing that link would match GNU/just-bash. I understand the choice to match command -v; I'm just flagging the tradeoff.
Appendix: non-blocking (3)
  • ⚪ src/builtins/which.rs:32: which --all ls prints which: --: invalid option, because the error names -- instead of --all. Accepting --all or naming the whole argument would read better.
  • ⚪ Pre-existing, not filed: command -v /bin/ls and command -v ./s already exit 1 on main. The inline fix covers this too, so a separate issue isn't needed.
  • ⚪ Pre-existing: README says "25 builtins" (README.md:118, :252), but main already has more. Optional to fix here.

Routing: I ran a single correctness/tests reviewer pass and my own probes. I skipped the API pass because there's no SDK/binding surface change, and the adversarial pass because there's no parsing, concurrency or security path.

Comment thread src/exec.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Pushed 2b86cd9:

  • Slash names: fixed as suggested (see the inline thread).
  • which --all: the error now names --all. I left --all itself unsupported since debianutils which only takes -a.
  • Bare name for built-ins: keeping it so which and command -v always agree, and because /bin/ls here is only a link back into the shell rather than a separate program. which -a ls still lists /bin/ls after the bare name. Agree it's a tradeoff, happy to revisit if agents trip over it.
  • README builtin count: pre-existing and outside this change, leaving it for a separate fix.

@ferdingler
ferdingler merged commit 39daa83 into strands-agents:main Nov 17, 2026
59 of 61 checks passed
@ferdingler
ferdingler deleted the feat/which branch November 17, 2026 19:32
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.

3 participants