Repository navigation
test(policy): verify ::response events for tool grammar actions - #333
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
## Problem A tool grammar action records a `::response` with the exit status when the program that the Shell started ends. No test shows that a `when temporal` rule reads this response through a real box. No test shows that the response stays in history when the box starts again on the same box directory. The docs do not say which response a grammar action records, so an operator cannot write "refuse `git push` until `cargo test` passes" from the docs. ## Solution - No production code changes. The Shell adapter already records the response of an allowed grammar action. It records the same `output.status` that a `shell:spawn` response records, and it records no new attribute. - A new end-to-end test runs a box with `git` and `cargo` grammars and a `forbid` on `git push` unless an earlier `cargo test` exited 0. - A new policy unit test opens a second engine on the history of a first engine, and reads the grammar response that the first engine recorded. - `docs/user/policy/actions.md` adds a grammar action row to the response table. `docs/design/policy.md` says what the response carries. - A stdio MCP server start still records no response, for a `shell:spawn` and for a grammar action. This change does not change that. ## Tests - `adapters::shell::tests::a_new_engine_on_the_same_history_reads_an_earlier_grammar_response` pins durability across a new engine. It failed when I recorded exit status 1 in place of 0. I restored the test. - `tool_grammars.rs::a_push_is_denied_until_a_test_exits_0_and_stays_allowed_after_a_restart` pins three steps. In the first run, a push is denied before any test, and also after a test that exits 1. In the second run, a test exits 0 and then the push runs. In the third run, which runs no test, the push runs from history. - I ran the policy tests with and without `--all-features`, and they pass. Clippy shows no owned warning in `policy` or `box`, and fmt is clean. - Not run here: the end-to-end test. This host refuses to start a box at the mount table check. CI runs it on Linux arm64 and macOS.
… disclose each program a grammar decides, per review
…-enforce # Conflicts: # docs/user/mcp.md # docs/user/policy/actions.md # docs/user/shell.md
…an agent spawn under that name stays refused, per review
…-enforce # Conflicts: # crates/box/src/run/broker/host.rs
…-temporal # Conflicts: # crates/box/tests/tool_grammars.rs # crates/policy/src/adapters/shell.rs # docs/design/policy.md
|
Assessment: Comment The tests and the new DetailsReview details
The push-before-test, after-failure and after-restart sequence in the e2e test covers the main user flow end to end. |
|
Thanks. On the title: I'm keeping it as is. I fixed the cwd point from the inline comment. |
|
Assessment: Approve (re-review at The latest commit fixes the one finding from the previous review: the design doc now states what a grammar response pins, and the unit tests pin DetailsWhat I checked for the new commit
|
Description
With tool grammars, Box already writes down when a grammar-bound program finishes and what exit status it had. So a rule like "no
git pushuntilcargo testpassed" should work. But nothing checked it in a real box, nothing checked that it still holds after the box restarts, and the docs didn't mention it, so you couldn't learn from the docs that this rule is possible.This adds an end-to-end test for exactly that rule. The push is denied before any test runs and after a failing test. It works after a passing test, and it still works in a fresh run of the same box. There's also a smaller test that the recorded result survives reopening the history. The docs now list what a grammar action records. No production code changes. MCP server starts still record nothing when they finish, same as a regular
shell:spawn, and I left that alone.Related Issues
#38
Type of Change
New feature
Testing
The policy tests pass locally with and without features. I broke the new durability test on purpose (a failing test exit status) and it caught it. The end-to-end test needs a real box, so only CI on Linux arm and macOS runs it.
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.