Skip to content

fix(box): bind a tool grammar to how box cli binds tools - #358

Merged
ferdingler merged 6 commits into
mainfrom
feature/grammar-binds-program
Nov 9, 2026
Merged

ferdingler merged 6 commits into
mainfrom
feature/grammar-binds-program

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

Description

This PR is the corresponding change in Box core to integrate Tool Grammars with Tool Packs from Box CLI.

Box CLI writes a tool's command as the real file a tool points to, for example npm resolve into .../npm/bin/npm-cli.js. Tool grammars took the program name from the end of that path. So, in that case, the tool grammar would have expected the tool to be npm-cli.js which would be incorrect, because an agent will run the npm command, not the npm-cli.js command. This PR fixes this problem.

When that name isn't the end of the command path, Box looks it up at startup on the same PATH the shell uses at runtime. It refuses the grammar if the name leads to a different file than the tool's command, or to no file at all. I updated the config guide, the tool grammars guide from #351, and the binding decision to match.

Related Issues

#38

Type of Change

Bug fix

Testing

I added unit and end-to-end tests: one with an npm link to npm-cli.js that gets decided as the grammar action, and two that check the startup refusals. I broke the new check on purpose and the tests caught it. On my Linux host, the end-to-end tests that start a full box fail the same way on main, because of a mount table issue on the host, so I'm relying on CI for those and for macOS.

  • I ran the relevant suites (just check, or cargo test --workspace --all-features)
  • If I touched Rust, I ran cargo fmt --all and cargo clippy --workspace --all-targets --all-features -- -D warnings

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
  • My changes generate no new warnings

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

## Problem

A tool grammar binds on the file identity of `command[0]` and on a name. Box took the name from the
last path component of `command[0]`. Box CLI writes `command[0]` as the file that each link
resolves to, because Box opens it without following links. So `npm` becomes
`.../npm/bin/npm-cli.js`. The load then refused `program = "npm"`, because the last component is
`npm-cli.js`. If the load had accepted it, the binding would be filed under `npm-cli.js`, and the
agent types `npm`. The grammar never decided a spawn.

## Solution

- Box binds a grammar on the file identity of `command[0]` and on the grammar's `program`. A tool
  with no grammar keeps the last component of its command.
- The load no longer compares `program` with the last component of `command[0]`.
- At startup, Box finds `program` on the `PATH` of the `run` process. The hosted Shell searches the
  same `PATH` at runtime. Box refuses the grammar when that file is not the file that `command`
  opened, or when `PATH` finds no file. The refusal names the tool, the program, and the file found.
  This still refuses a `git` grammar on `[tool.gh]`.
- The startup disclosure names the grammar's `program`.
- `docs/user/config.md` states the new refusal. The decision entry
  `a-tool-grammar-binds-on-the-program-file-and-the-called-name` now states that the name is the
  grammar's `program`, and keeps its anchor.

## Tests

- `hosted.rs::a_grammar_binds_under_its_program_when_the_command_names_the_linked_file`: the
  command names `npm-cli.js`, `bin/npm` links to it, and `npm install` is decided as
  `Tool::npm::Action::"install"`.
- `hosted.rs::a_grammar_whose_program_finds_another_file_is_refused` and
  `a_grammar_whose_program_finds_nothing_is_refused` pin the two refusals and their text.
- `hosted.rs::binding_uses_the_file_the_run_opened_and_not_what_its_path_holds_later` keeps a name
  for the opened file on its search path, so it still pins the replaced-file refusal.
- `run.rs::startup_discloses_each_tool_that_a_grammar_decides_under_its_bound_name` pins the
  disclosure.
- `grammar.rs::a_grammar_whose_program_is_not_the_last_component_of_its_command_loads` replaces
  the old load refusal.
- e2e `tool_grammars.rs`: `a_grammar_decides_the_name_the_agent_types_when_the_command_names_the_linked_file`,
  `a_grammar_whose_program_finds_another_file_on_path_is_refused_at_startup`, and
  `a_grammar_whose_program_finds_nothing_on_path_is_refused_at_startup`. The `attempt` fixture now
  sets `PATH` to the test's tools directory first.
- `cargo test -p strands-box-policy --all-features` passes. `cargo test -p strands-box
  --all-features --lib` has 728 passed and 69 failed. origin/main has the same 69 failures on this
  host (725 passed): the box cannot inventory mounted executable aliases because of the docker netns
  mounts in `/proc/self/mountinfo`. For the same reason, the e2e tests that start a full box fail on
  this host, on this branch and on origin/main. The two startup refusal e2e tests pass. The full
  startup e2e tests and macOS were not run locally. CI must prove them.
- clippy shows 0 owned warnings, and `cargo fmt --check` is clean.
Comment thread docs/design/decisions.md Resolved
Comment thread crates/box/src/run/hosted.rs Resolved
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

Assessment: Request Changes

Binding a grammar under the name the agent types fixes the npm → npm-cli.js case. But the new PATH check is stricter than the problem needs: it refuses grammars that load on main whenever another file with the same name comes first on PATH. And program is applied only to the tool that owns the grammar, while the decision text now says more than that.

Review Categories
  • Correctness / scope: The check should add the linked-file case on top of main's called-name rule, not replace it. I proved the Homebrew-first PATH regression with a temporary unit test.
  • Docs vs. code: The decision text says MCP servers and a second tool bind when they run the same file. The code still binds them by the last component of command.
  • Tests: Nothing covers a grammar on a tool that isn't first on PATH, or an MCP server whose command names the linked file.

Checks that held:

  • The startup PATH matches the runtime one. host_search_path copies the process PATH that strands-shell's host_path_lookup walks, and Box never changes its own PATH.
  • A git grammar on [tool.gh] is still refused.
  • If the file is replaced between ConfiguredCommands::load and the bind, startup is refused, so that race fails closed.
  • MCP start authorization still follows the premise in AGENTS.md: shell:spawn decides unless a grammar binds the server.

The unit and end-to-end tests for the npm case are well targeted.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

Both findings are addressed in 7dbfbf7. Box keeps the rule from main, and uses the PATH check only when the last component of command is not the program. A regression test now covers the Homebrew-first case. The decision text again says that a server binds when its command calls the program name.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

Assessment: Approve

7dbfbf7 fixes both findings from my earlier review. The PATH check now only adds the linked-file case on top of main's called-name rule, and the decision text matches how servers and grammar-less tools bind. I have no new findings.

Checks that held on this commit:

  • Previously valid configs still load. A command whose last component is the program skips the PATH check, including the Homebrew-first PATH case (new regression test, passes locally).
  • The linked-file case works. program = "npm" with a command naming npm-cli.js binds when PATH's npm reaches the same inode, and is refused when it reaches another file or nothing (gh/git and finds-nothing tests pass).
  • Startup and runtime use the same PATH. host_search_path and the Shell's host_path_lookup both read the process PATH.
  • Docs match the code. decisions.md and config.md describe bound_name for the grammar's own tool and called_name for servers and other tools.
  • Premise unchanged. MCP start authorization still follows the AGENTS.md premise.

Locally, run::hosted::tests has 10 failures. All of them come from the missing strands-box-contain-trampoline binary in my environment, not from this change.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

Assessment: Approve

dd365c4 changes only docs: a new guide, docs/user/tool-grammars.md, a field reference in policy/actions.md, and a rework of the grammar section in config.md. I checked the guide's examples against the code, and they behave as written. My one inline note is a sentence that still states the binding rule from before this PR.

Checks that held. I ran the guide's grammars and rules through the policy crate with temporary tests, which I did not commit:

  • The git and cargo grammars load and read commands as the guide says. push -f, --force-with-lease, origin +main, and -C ~/src/project push -f all have force_push. --dry-run removes it whichever order the options come in. push --delete …, frobnicate, and cargo test --manifest-path x all read as unmodeled.
  • The guide's rules pass strict validation. That covers the permit, the forbid on force_push, the push_after_project_tests temporal rule, and the deny-all over "*" plus unmodeled.
  • The startup refusals the guide promises happen:
    • input.args: ["test"] is refused because a temporal predicate can't hold an array constant.
    • A temporal predicate over the Tool::git::Action::"*" group is refused.
    • A rule that reads context.input.cwd across two grammars is refused.
  • The field table in actions.md matches Grammar::schema(). It agrees on the base fields, the optional credential_reads, the effects field only when the grammar declares an effect, unmodeled sitting outside "*", and the Tool::<name>::v<version> namespace for version 2 and later.
  • The binding text in config.md and the guide matches the code. That covers bound_name versus called_name, the PATH check only when program isn't the last part of command[0], and the format of the refusal message.
  • site-manifest.json has no duplicate nav order, and CI's site-manifest job covers it.

@ferdingler

ferdingler commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator Author

The inline note did not post. I think it is about docs/user/shell.md, which still said that a grammar binds each tool "whose command runs its program". In ee1e13f, that sentence now says that a grammar binds its own [tool] under its program, and binds each other [tool] and stdio [mcp] whose command ends in that name. If you meant a different sentence, please name it.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

Assessment: Approve

ee1e13f changes two lines in docs/user/shell.md, and they now match the code. The grammar's own [tool] binds under bound_name, which is its program. Every other [tool] and each stdio [mcp.<name>] binds under called_name, which is the last part of command[0].

@ferdingler, thanks for asking. The note that didn't post was about a different sentence. It's in docs/user/tool-grammars.md, line 221, the first sentence under "How a grammar decides a command":

A grammar binds a program: it decides each spawn of the file its [tool] runs, under the name in that [tool]'s command.

Suggestion (non-blocking): this sentence still states main's rule. With command = [".../npm/bin/npm-cli.js"] and program = "npm", it says the grammar binds under npm-cli.js. The code, and the "One grammar covers every tool that runs its program" section further down the same page, bind it under npm. A possible wording: "…it decides each spawn of the file its [tool] runs, under the grammar's program, the name the agent types."

Checks that held on ee1e13f:

  • The new shell.md sentence matches bound_name for the grammar's own tool and called_name for the other tools and servers.
  • The commit changes nothing else. My earlier checks on dd365c4 still apply: the guide's grammars and rules behave as written, and the field table matches Grammar::schema().

@ferdingler ferdingler changed the title fix(box): bind a tool grammar under the name the agent types fix(box): bind a tool grammar to how box cli binds tools Nov 7, 2026
@ferdingler

ferdingler commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator Author

Fixed in 583e54a. The sentence under "How a grammar decides a command" now says that a grammar binds under its program, which is the name the agent types.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

Assessment: Approve

583e54a rewrites the sentence at docs/user/tool-grammars.md:221, which closes my last open note. The sentence now says a grammar binds under its program, the name the agent types. That matches bound_name in the code and the "One grammar covers every tool that runs its program" section later on the same page. I have no new findings.

Checks that held on 583e54a:

  • The commit changes only those two lines.
  • No doc still states the old rule. I searched docs/ for "under the name in" and "whose command runs its", and nothing in docs/ matches either phrase.
  • My earlier checks on dd365c4 and ee1e13f still apply: the code-level binding and PATH checks, the guide's examples and refusals, and the field table matching Grammar::schema().

@ferdingler
ferdingler merged commit de5e94a into main Nov 9, 2026
21 checks passed
@ferdingler
ferdingler deleted the feature/grammar-binds-program branch November 9, 2026 22:38
hanaduy pushed a commit that referenced this pull request Nov 10, 2026
## Problem

The hosted Shell finds a bare name, such as `git`, on the `PATH` of the `run` process. Box then
selects a `[tool]` by the identity of the file that it found. A `[tool]` whose command file is not
the first match on that `PATH` cannot run by its name.

On macOS with full Xcode, the `box/git` pack of Box CLI runs `<developer>/usr/bin/git`, because the
Apple stub `/usr/bin/git` exits 71 in the sandbox. The Shell finds the stub first, no `[tool]`
names it, and policy decides `shell:spawn` on `/usr/bin/git`. So `git` does not work in the box.
The same gap occurs for Homebrew git behind `/usr/bin`, and for a tool whose `command` resolves on
its own `env.PATH` to a different file than the operator's `PATH` finds. A workaround in Box CLI
changed the `PATH` of the trusted process for every bare name (strands-agents/staging-boxy#365).

## Solution

A bare name that a `[tool]` answers to runs the command file that the run opened and checked at
startup, before any `PATH` lookup.

- A `[tool]` answers to the last component of its `command[0]`, not to its table label. A `[tool]`
  with a grammar answers only when the grammar's `program` is that name. A grammar whose `program`
  differs keeps the startup `PATH` check from #358 and the `PATH` lookup.
- Box leaves out each name that the Shell runs itself (builtins, Shell commands, `sh`), because the
  Shell's own program keeps precedence.
- A name that tables with different files answer to stays on `PATH`, as before.
- When the declared file is gone, the name is not found. The Shell does not search `PATH` for
  another file, so the refusals of #352 still apply.
- `program` stays the name that the agent typed. `program_path` is the tool's file. The spawn still
  needs a `shell:spawn` permit or a grammar action, so the change adds no authority.
- `run` prints one startup line for each name and the file it runs, and one for each shared name.

Interfaces that moved:

- Behaviour: which file a bare name runs in the Shell, so `program_path` can name another file for
  the same configuration. With an `[agent.filesystem] exec` entry and a `[tool]` with fixed
  arguments on another file of that name, a bare name now selects the tool and can be refused.
- Startup disclosure: two new line forms.
- Vendored Shell: `Kernel::host_program`, `os::HostPrograms`, `VfsKernel::host_programs`,
  `ShellBuilder::host_programs`, and `exec::implements_program`. `crates/shell/UPSTREAM.md` records
  the divergence.

The issue proposed the table label as the name. The label was rejected because it makes a label
such as `brew_git` a callable name, and `[tool.python]` on `python3` would change what `python`
runs. A new `[tool]` field was rejected because it adds a key to a frozen table.

The entry `a-host-binary-resolves-on-the-operator-path` in `docs/design/decisions.md` is not
rewritten in this commit, because that needs the maintainer's decision first.

## Tests

- `declared_host_programs.rs` (vendored Shell): a declared name runs its file and keeps the typed
  name; an undeclared name uses `PATH`; a spelling with `/` ignores the declarations; builtins,
  Shell commands, and functions keep precedence; a gone file is not found and `PATH` is not used.
- `run::commands::tests`: a tool answers to its command's file name; two files under one name stay
  on `PATH`; a grammar on another program name declares nothing; a grammar `program` that another
  file answers to stays on `PATH`; Shell names are not declared; a linked command keeps its link.
- `command::run::tests::startup_names_the_file_each_bare_name_runs` pins the disclosure.
- `tool_grammars.rs`: `a_bare_name_runs_the_tool_that_declares_it_when_another_file_is_first_on_path`
  and `without_a_grammar_a_bare_name_runs_its_tool_under_shell_spawn` run a real box with a decoy
  `git` first on `PATH`.
- Each new pin fails with its defect restored.
- Linux aarch64: the `strands-shell` suite passes, and clippy and fmt are clean. The `strands-box`
  suite passes except `runtime_ignores_workspace_and_private_generated_schema_artifacts`, which
  times out in its binary on this host on `main` too.
- macOS: the targeted tests above, including both end-to-end tests, and the `strands-box` suite.
- macOS 27.0.1 (mac2.metal): with `[tool.git]` on Xcode's git and `/usr/bin` first on `PATH`,
  `main` denies `shell:spawn` on `/usr/bin/git`, and this change prints `git version 2.50.1
  (Apple Git-155)`. On macOS, two `strands-box` unit tests fail on `main` too:
  `an_incomplete_scan_reports_its_cause_without_claiming_to_find_a_command` and
  `cancelling_before_the_launch_gate_preserves_the_signal_exit`.
- The containment suite was not run to completion on Linux, because its harness writes into
  `/usr/bin`, which this host does not allow. CI runs it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants