Skip to content

feat(config): enable the tool packs feature behind a feature flag - #281

Merged
abhisheksp merged 9 commits into
mainfrom
feature/tool-grammars-loading
Nov 5, 2026
Merged

abhisheksp merged 9 commits into
mainfrom
feature/tool-grammars-loading

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Description

The policy half of tool grammars is now on main, but Box still can't load a grammar. There's nowhere to put one in box.toml, and the box never opens the policy engine with any grammars. So nobody can write a rule on a grammar action like git push.

This PR is the box half. A tool can now carry its grammar inline in box.toml. It's inline because the Box CLI compiles one finished box.toml plus a policy, and that output has to run on its own with no other file. All of this sits behind a new experimental flag. With the flag off, a grammar is refused and the box behaves as before. Box checks each grammar at startup, opens the engine with the grammars, and checks the policy against their actions. An older build already refuses a box that uses a grammar, so I didn't add a new record version. Its message is less helpful than a version mismatch would be, and I accept that while the feature is experimental. I'm reworking how record versions mark features in a separate change. The new field on a tool and the flag were approved ahead of time as changes to a frozen surface. One more thing is deliberate: a rule on a grammar action loads, but it can't take effect until a follow-up PR I have planned teaches the shell to use grammars. The box warns at startup for each grammar. I chose to allow this while the feature is experimental. The docs here are only the reference entries: the new field, the flag, going back to an older build, and the startup refusals. The user guide for tool grammars comes in a follow-up PR I have planned.

Related Issues

None.

Type of Change

New feature

Testing

I ran the box and policy test suites, formatting, and lint. Each startup refusal has a test: a grammar on the agent, a grammar with the flag off, a grammar for a different program than its tool runs, and two grammars for one program. I broke each one on purpose and its test caught it. Most end-to-end box tests can't run on my Linux machine because of a host mount setup that has nothing to do with this change, so CI covers those, along with 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

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.
@ferdingler ferdingler changed the title feat(config): load inline tool grammars behind the tool-grammars flag feat(config): load inline tool grammars from box.toml behind the tool-grammars flag Nov 4, 2026
@ferdingler
ferdingler marked this pull request as ready for review November 4, 2026 21:17
@ferdingler ferdingler changed the title feat(config): load inline tool grammars from box.toml behind the tool-grammars flag feat(config): enable the tool packs feature behind a feature flag Nov 4, 2026
Comment thread crates/box/src/command/run.rs Resolved
Comment thread crates/box/src/record/config/mod.rs Resolved
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Assessment: Request Changes

Reviewed head 54d4ded. The loading path is careful and well covered by tests: the switch, refusal on [agent] and [mcp], the program and duplicate checks, and key-path errors. One thing blocks the merge: this PR loads policy rules that can never fire, which the repo's recorded decision forbids. The other two comments are about keeping the frozen-surface bookkeeping and the namespace docs accurate.

Assumptions and rules checked that held
  • record_version: (off, off) → 21; native on with grammars off → 22 and writes no tool-grammars key (the fix in 54d4ded); tool-grammars on → 25, including with a shared MCP program; an existing v22 record still parses.
  • The called-name check is right for /usr/bin/git, for git found on PATH, and for a gh tool carrying a git grammar (refused). A grammar-less brew_git being covered matches the documented binding decision.
  • grammar is refused on [agent] (validate), refused on [mcp] (unknown field), and refused on a tool while the switch is off, in both ConfigFile and Record validation.
  • bind_tool_grammars resolving every tool adds no new startup refusal, because ConfiguredCommands::load already resolves every tool.
  • Policy reload keeps the grammar schemas (authority.rs), and box policy check validates with grammars both with and without MCP servers.
  • Doc-update table: config.md, crates/box/README.md, and native-filesystem-policy.md (record version) are updated. documented_inputs, four_inputs, policy_check, and 156 record::config unit tests pass locally. tool_grammars.rs skipped locally (no namespace launcher).

Comment thread crates/box/src/command/run.rs Resolved
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Assessment: Request Changes

Reviewed head 6d3c695. This head trims the docs to the reference entries, and the loading code, tests, and startup path are the same as in the version I checked before. One thing blocks the merge: this PR loads policy rules on grammar actions that can never fire, which the repo's recorded decision forbids. The other comment is about keeping the frozen-surface list in step with record version 25.

Assumptions and rules checked that held
  • record_version: (off, off) → 21; native on with grammars off → 22 and writes no tool-grammars key; tool-grammars on → 25, including with a shared MCP program; an existing v22 record still parses.
  • The called-name check is right for /usr/bin/git, for git found on PATH, and for a gh tool carrying a git grammar (refused). A grammar-less brew_git being covered matches the documented binding decision.
  • grammar is refused on [agent] (validate), refused on [mcp] (unknown field), and refused on a tool while the switch is off, in both ConfigFile and Record validation.
  • bind_tool_grammars resolving every tool adds no new startup refusal, because ConfiguredCommands::load already resolves every tool.
  • Policy reload keeps the grammar schemas (authority.rs), and box policy check validates with grammars both with and without MCP servers.
  • Doc-update table: config.md, crates/box/README.md, shell.md, and native-filesystem-policy.md (record version) are updated. The earlier "name is the policy namespace" sentence, which was wrong for version > 1, is gone at this head.
  • documented_inputs, four_inputs, policy_check, and 156 record::config unit tests pass locally. tool_grammars.rs skipped itself here (no namespace launcher), so CI covers it.

Comment thread crates/box/src/record/config/mod.rs Resolved
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Assessment: Request Changes

Reviewed head 67e7fa3. The only change since 6d3c695 is in tests/tool_grammars.rs: the end-to-end tests now point [tool.git] at a copy of true(1) that the test owns, instead of the host's /usr/bin/git. The two open threads on run.rs:164 (a rule on a grammar action loads but never takes effect, which decisions.md forbids) and mod.rs:593 (the freeze list in crates/box/AGENTS.md stops at version 24) still apply, because neither line changed.

Assumptions checked that held for the new commit
  • The copied program keeps its execute bit (fs::copy preserves permissions). It is named git, so called_name matches program = "git" whatever the host's /usr/bin/git is.
  • The TempDir holding the tool lives until attempt returns, after the run and the record read.
  • All 5 tool_grammars tests compile and pass locally. They skip themselves here (no namespace launcher), so CI on macOS and Linux runs them for real.

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Assessment: Comment

Reviewed head 2f6f38f. This commit only changes crates/box/AGENTS.md, so that the frozen-record list now runs through GRAMMAR_RECORD_VERSION. That resolves the freeze-list thread. The author says a maintainer waived the inert-rule thread, so I'm leaving that decision to the maintainer and no longer requesting changes on it.

Checked at this head
  • GRAMMAR_RECORD_VERSION is the newest record version (25), so the "through" bound in AGENTS.md covers every stored shape this PR adds.
  • The PR description now lists the grammar field, the tool-grammars switch, and record version 25 as approved frozen-surface changes.
  • No code changed since 67e7fa3, so the earlier checks still hold: record-version selection, refusals on [agent], [mcp], and switch-off, the called-name match, and policy reload with grammars.

abhisheksp
abhisheksp previously approved these changes Nov 5, 2026
Comment thread docs/user/native-filesystem-policy.md Resolved
…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.
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

Assessment: Comment

Reviewed head 11d511e. The new commit drops record version 25. A box with tool-grammars on now writes version 21–24, as it would without the switch. The crates/box/AGENTS.md freeze list and the docs go back to version 24. The inline comment covers the rule about bumping the record version. The inert-rule thread is still waiting on the maintainer, as before.

Assumptions checked that held
  • "An older build refuses a grammar record": holds. On main, Record, ExperimentalFeatures, and ProcessSpec all set deny_unknown_fields, and every reader of the stored record goes through Record::parse (layout.rs). Every grammar record carries tool-grammars = true (the switch is required), so main refuses it. I proved this by parsing this build's record with main.
  • A record with the switch on but no grammar is also refused by main. That is conservative, and correct.
  • This build reads its own v21 grammar record back unchanged. With the switch removed it refuses the stored grammar (a_record_that_selects_tool_grammars_keeps_its_ordinary_version).
  • The config.md rollback steps (turn the switch off, remove every grammar table, start once with this build) still match what an older build needs.
  • 157 record::config unit tests pass locally.

Comment thread crates/box/src/record/config/mod.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@abhisheksp CI is green again. Since your approval, I dropped record version 25, because an older build already refuses a grammar record, and I put the line you asked about back. Could you take another look?

@abhisheksp
abhisheksp merged commit 3f69359 into main Nov 5, 2026
35 of 38 checks passed
@abhisheksp
abhisheksp deleted the feature/tool-grammars-loading branch November 5, 2026 05:24
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