Repository navigation
fix(box): bind a tool grammar to how box cli binds tools - #358
Conversation
## 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.
|
Assessment: Request Changes Binding a grammar under the name the agent types fixes the Review Categories
Checks that held:
The unit and end-to-end tests for the |
|
Both findings are addressed in 7dbfbf7. Box keeps the rule from main, and uses the |
|
Assessment: Approve 7dbfbf7 fixes both findings from my earlier review. The Checks that held on this commit:
Locally, |
|
Assessment: Approve dd365c4 changes only docs: a new guide, Checks that held. I ran the guide's grammars and rules through the policy crate with temporary tests, which I did not commit:
|
|
The inline note did not post. I think it is about |
|
Assessment: Approve ee1e13f changes two lines in @ferdingler, thanks for asking. The note that didn't post was about a different sentence. It's in
Suggestion (non-blocking): this sentence still states Checks that held on ee1e13f:
|
|
Fixed in 583e54a. The sentence under "How a grammar decides a command" now says that a grammar binds under its |
|
Assessment: Approve 583e54a rewrites the sentence at Checks that held on 583e54a:
|
## 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.
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
npmresolve 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 benpm-cli.jswhich would be incorrect, because an agent will run thenpmcommand, not thenpm-cli.jscommand. 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
PATHthe 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
npmlink tonpm-cli.jsthat 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.just check, orcargo test --workspace --all-features)cargo fmt --allandcargo clippy --workspace --all-targets --all-features -- -D warningsChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.