Repository navigation
feat(shell): connect the shell to tool grammars - #327
Conversation
## Problem The policy crate has a tool grammar model, a schema generator, and a binding table, but `box.toml` cannot carry a grammar and no non-test engine opener installs one. So an operator cannot write a rule on a `Tool::<name>` action, and the Box CLI cannot hand Box Core one finished `box.toml` that holds its grammars. The test-only opener also let a caller pair a `GrammarBindings` with an engine that did not know the schemas of those grammars. A spawn bound to such a grammar is denied with no message. The staged opener also held a rule that names an undeclared `Tool::git` action as if an MCP server named `git` could declare it later. The box then started and the rule did not enforce. ## Solution - Add the switch `[experimental-features] tool-grammars` (boolean, default `false`). - Add a sixth field `grammar` to `[tool.<name>]`. It holds the grammar inline, with the same keys as a grammar file. `[agent]` refuses it, and `[mcp.<name>]` refuses it as an unknown key. A grammar is refused while the switch is off. The maintainer approved this frozen-surface addition. - A record that selects `tool-grammars` is record version 25, so an older build refuses the box and does not misread it. `RECORD_VERSION` stays 21, because a record with the switch off has the same shape. - Load each table through the existing grammar model. Each refusal now names a key path in `box.toml`, for example `tool.git.grammar.shell.push.usage`. Box no longer reads a grammar from a file. The test corpus loads its files inside the tests. - Startup refuses a grammar over 64 KiB, measured as the table written out as TOML. It refuses a `program` that is not the name that its own `[tool]` command runs. It refuses two grammars with one `program` or one `name`. - The engine owns the grammar set. The new opener `PolicyEngine::open_staged_file_with_grammars` installs the grammar schemas below every MCP schema that stages later, and through a reload. `PolicyEngine::grammar_bindings` gives out bindings for exactly those grammars, and `GrammarBindings` has no other public constructor. `GrammarBindings::declare_tool` declares a tool's program file and called name, and binds the grammar whose `program` is that name. So a grammar binds every `[tool]` that runs its program. - `PolicyEngine::validate_with_grammars` and `PolicyEngine::check_with_grammars` validate against the same schemas, so the config-time check and `check_policy` agree with the run. - The staged opener no longer holds a rule on a qualified namespace, such as `Tool::git`, for an MCP schema. No MCP schema can declare it, so the load refuses the rule. - At startup, `run` prints one warning for each grammar: the Shell still decides a spawn as `shell:spawn`, so a rule on a grammar action does not fire yet. - New public names in `strands-box-policy`: `ToolGrammar`, `ToolGrammars`, `ToolGrammarError`, `GrammarBindings`, `BindingRefusal`, and the methods above. The facade is 57 names with all features and 51 by default. ## Tests - `tool_grammars.rs`: a box whose policy forbids `Tool::git::Action::"push"` starts with the grammar and the switch, writes record version 25, and prints the warning (`a_policy_naming_a_grammar_action_starts_with_its_grammar_and_the_switch`). The same policy with no grammar is refused at startup (`the_same_policy_without_the_grammar_is_refused_at_startup`). A grammar with the switch off is refused (`a_grammar_with_the_switch_off_is_refused_at_startup`). A box with the switch off and no grammar keeps record version 21 (`a_box_with_the_switch_off_and_no_grammar_is_unchanged`). - `tool_grammar_engine.rs` goes through the non-test opener: open and reload (`a_policy_that_names_a_grammar_action_opens_with_its_grammar`), the refusal with no grammar, the refusal of a rule held for an MCP schema (`a_grammar_action_rule_is_refused_rather_than_held_for_an_mcp_schema`), an MCP schema that stages over the grammar schema, validation, and the example on the configuration page (`the_grammar_and_the_rule_the_configuration_page_shows_load`). - Refusals: `a_tool_grammar_needs_the_tool_grammars_switch`, `a_grammar_is_refused_on_the_agent`, `a_grammar_whose_program_is_not_its_tools_called_name_is_refused`, `two_grammars_for_one_program_are_refused`, `two_grammars_with_one_name_are_refused`, `a_grammar_over_the_size_limit_is_refused_at_its_table`, `a_refusal_names_the_key_path_under_the_tool`, and `a_record_that_selects_tool_grammars_carries_its_own_version`. - Binding: `a_grammar_binds_every_declared_tool_that_runs_its_program` and `an_unreadable_tool_program_is_refused_when_declared`. - `check_policy`: `a_check_reads_the_actions_of_each_tool_grammar`. - The grammar corpus test keeps every file and expects the new key paths in each refusal. - I removed each new guard, saw its test fail, and restored it. - Gates on Linux aarch64: fmt, clippy with 0 owned warnings, the policy suite with and without features, `cargo doc` with `-D warnings`, and the box suite with all features. `box_shell::a_lua_busy_loop_does_not_starve_the_control_socket` failed under full-suite load and passed alone. - Not run: macOS. No test pins the box-level call to `declare_tool`, because nothing reads the bindings until the Shell consumes them.
Resolve each crates/policy file to the main version. Port two box tests to the merged API: a test that called the removed ToolGrammars::is_empty now validates a policy against the grammar, and a new ProcessSpec literal in run/commands.rs gets the grammar field.
…e-program refusal The merged policy crate has no 64 KiB grammar limit, so the user guide no longer states one. The guide now shows the exact text of the refusal of two grammars for one program, and of two grammars with one name. The unit tests compare the full text, and a run test checks that startup refuses two grammars for one program and stores no record.
crates/box/AGENTS.md said that [agent] holds grammar. It does not, so the field list keeps five fields and names grammar as a [tool] field behind the switch. The README and the architecture page now state the startup warning for each grammar.
A record with native-filesystem-policy on and tool-grammars off wrote a tool-grammars key at version 22, 23, or 24. An older build refuses that key, so the documented downgrade broke. The key is now written only when the switch is on. A test pins that a version 22 record holds no such key, and a test pins that [mcp.<name>] refuses a grammar table. The policy pages now say that a tool grammar adds Tool:: actions.
…e guide for a follow-up
…cause macOS links /usr/bin/git
…efuses a grammar record A record version stops an older build from misreading a newer box. An older build already refuses a record that holds a grammar table or the tool-grammars key, because ProcessSpec and ExperimentalFeatures deny unknown fields. So a box with tool-grammars on now writes the version it would write without the switch. The test a_grammar_record_is_refused_where_its_keys_are_unknown pins the refusal. The docs no longer name version 25.
… action ## Problem A box with `tool-grammars` on loads a rule on a `Tool::` action, but the rule does not fire. The broker does not give the grammar bindings to the Shell, so the Shell decides every spawn as `shell:spawn`. A `forbid` on `Tool::git::Action::"push"` protects nothing, and `run` prints a warning that says so. The stdio MCP start also uses `shell:spawn` only. The bindings have more defects. They resolve each tool program a second time, after the run already opened and checked it. They accept bindings from any engine. And when a bound file goes away, a bare name falls through `PATH` to another file, and that spawn goes back to `shell:spawn`. Three more gaps stay from loading tool grammars. An `[mcp.Tool]` server can use the `Tool` namespace that every grammar action uses. `policy generate-schema` writes no grammar action, so an operator cannot see the names to write. And a rule that waits for an MCP schema and has a temporal predicate on a grammar action group is held, then silently drops when that schema stages, although no schema can make it fire. ## Solution - `run` binds the grammars to the program file that `ConfiguredCommands` opened for each `[tool]`, and installs the bindings on the engine before the engine is shared. `PolicyEngine::bound_by` installs them, and it refuses bindings that another engine issued (`BindingRefusal::AnotherEngine`). `GrammarBindings::declare_tool` now takes the opened file, its canonical path, and the spelling it is called through. - The Shell adapter reads the bindings from its engine. A bound spawn is decided only as its grammar action. The denial names that action, and the Shell telemetry records it. The telemetry category of a grammar action is `Tool::<name>`. - A spawn that the bindings refuse enters history as a `shell:spawn` request, with no verdict to the observer, and its telemetry is an enforcement denial. - A bare-name spawn of a bound name is refused when the file bound under that name is gone or replaced. - The stdio MCP start goes through the Shell adapter, so a bound program is decided as its grammar action there too. The start records no `::response`, as before. - The startup warning and its assertion are removed. ApprovedReach takes the commands that the run loaded. - While `tool-grammars` is on, the loader refuses `[mcp.Tool]`. - `policy generate-schema` composes the grammar actions of the config into `actions.cedarschema`, through the new `ToolGrammars::compose_action_schema`. `compose_action_schema` now calls it with no grammar. - MCP staging treats `PolicyError::UnsupportedClause` as final, as the other inert-rule refusals are. - New tests cover the validate paths that carry grammar schemas. - `docs/design/policy.md` lists the group-predicate refusal with the load refusals. ## Tests - `adapters::shell::tests::a_denied_grammar_action_names_the_action`, `a_refused_spawn_enters_history_as_a_shell_spawn_request`, and `bindings_from_another_engine_are_refused` pin the denial text, the history entry, and the engine check. - `grammar::bindings::tests::a_bare_name_whose_bound_file_is_gone_is_refused_and_does_not_fall_through_the_search_path` and `a_tool_is_bound_to_the_file_it_was_opened_as_and_not_to_what_its_path_holds_now` pin the `PATH` fix and the identity reuse. - `run::hosted::tests::binding_decides_a_declared_tool_as_its_grammar_action` and `binding_uses_the_file_the_run_opened_and_not_what_its_path_holds_later` pin the box binding step. - `run::broker::host::tests::an_mcp_start_of_a_bound_program_is_decided_as_its_grammar_action` pins the MCP start. - `tool_grammars.rs` adds end-to-end tests: a forbid denies `git push`, a permit runs `git status`, `unmodeled` is denied without a permit, `sh -c 'git push'` meets the same forbid, and with the switch off a spawn is `shell:spawn`. - I removed the history call and the `PATH` check one at a time, and the tests for each failed. I restored them. - I ran the policy tests with and without features, the telemetry tests, the box unit tests, and `mcp_schema_generation`, `policy_check`, `four_inputs`, `documented_inputs`, and `documented_commands`. Clippy shows no owned warning, and fmt is clean. - Not run here: the end-to-end tests in `tool_grammars.rs`, and the box unit tests that start a real box. This host refuses them at the mount table check, and the same 67 box unit tests fail on the base commit. CI runs them on Linux and macOS. - `record::config::tests::an_mcp_server_named_tool_is_refused_while_tool_grammars_is_on` pins the refusal and that the name is free with the switch off. - `record::config::tests::a_grammar_given_as_a_path_is_refused` pins that a grammar is a table inside `box.toml` and never a file it names. - `mcp_schema_generation.rs::generate_schema_writes_the_actions_of_each_tool_grammar` and `tool_grammar_engine.rs::the_composed_schema_holds_the_actions_of_each_grammar` pin the schema. - `tool_grammar_engine.rs::a_temporal_predicate_on_a_grammar_group_is_refused_when_its_mcp_schema_stages` pins the staging refusal. Without the fix it stages as `Accepted`. - I dropped the grammar schemas at each validate site, one at a time. Six sites had no test that failed. `each_mcp_schema_stages_over_the_grammar_schema_while_another_server_is_pending`, `a_rule_that_also_names_an_mcp_action_waits_for_its_schema_when_a_grammar_declares_its_tool_action`, and `a_staged_check_reads_the_grammar_schema_while_an_mcp_rule_waits` now fail at four of them. The other two are the complete checks at open and in the staged check. When either drops the schemas, its fallback path validates again with them and gets the same result, so no test can see the change.
…-enforce # Conflicts: # crates/box/README.md # crates/box/src/command/run.rs # crates/box/src/record/config/grammar.rs # crates/box/src/record/config/mod.rs # crates/box/src/run/hosted.rs # crates/box/tests/tool_grammars.rs # docs/design/architecture.md # docs/user/config.md
|
Assessment: Comment I reviewed head Assumptions and rules checked that held
Wiring one opened file through both reach judging and grammar binding is a solid way to keep the two from disagreeing. |
|
Assessment: Request Changes I reviewed head Review Categories
Assumptions and rules checked that held
Wiring the gate to the opened program file closes the TOCTOU window cleanly. |
… disclose each program a grammar decides, per review
…-enforce # Conflicts: # docs/user/mcp.md # docs/user/policy/actions.md # docs/user/shell.md
|
Replies to the two reviews:
|
|
Assessment: Request Changes I reviewed head Checked at this head that held
|
|
Assessment: Request Changes I reviewed head Checked at this head that held
|
…an agent spawn under that name stays refused, per review
|
Assessment: Approve At Checked and held
The fix is scoped well, and it tests both the agent side and the server side. |
…-enforce # Conflicts: # crates/box/src/run/broker/host.rs
|
Assessment: Comment
Checked and held
|
|
Assessment: Comment
Checked and held
|
|
Assessment: Approve
Checked and held
|
Description
This connects the shell to tool grammars, so a grammar rule actually fires. Until now a rule like "forbid
git push --force" loaded but did nothing, because the shell still saw every command as a plain spawn.Now when the agent runs a program that has a grammar, Box decides the grammar action, like
Tool::git::Action::"push", instead of a plain spawn. That coverssh -c 'git push'and MCP servers started through that program too. Telemetry records the action, and the startup warning is gone. It also includes a few small leftovers from the loading PR, likeToolnot being allowed as an MCP server name.Approvals
New public names in the policy crate (behind the
tool-grammarsflag) and the[mcp.Tool]refusal are approved by me. The denial message the agent sees stays the same; the grammar action shows up only in telemetry.Related Issues
Follows #281.
Type of Change
New feature
Testing
The policy, telemetry, and box tests that don't need a real box pass locally, and I broke each new check on purpose to make sure a test caught it. The end-to-end tests (push denied, status allowed, unmodeled denied, flag off unchanged) need a real box, so CI covers those and 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.