Skip to content

docs: add a user guide for tool grammars - #351

Merged
ferdingler merged 8 commits into
mainfrom
feature/tool-grammars-guide
Nov 7, 2026
Merged

ferdingler merged 8 commits into
mainfrom
feature/tool-grammars-guide

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

This is the PR that documents how the tool grammars feature works. AppSec review also asked us to be clear about what a grammar does not cover, like git hooks and other programs that git starts. This adds docs/user/tool-grammars.md, a walk-through with a git grammar, a force-push forbid, and the push-after-tests rule.

Related Issues

#38

Type of Change

Documentation update

Testing

I traced every behaviour claim on the page to a test on main. I put all the page's config and policy examples into one box and ran it on Linux, and both grammars and all the rules loaded. I couldn't run the end-to-end grammar tests on my machine, because it can't start a box. A quick probe showed that a temporal rule can't pin the arguments as a list, so the page pins them through the grammar instead. The site manifest test passes. The docs reviewer gave "Ship it" after three rounds. No diagrams, so there was nothing to compile.

  • 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

Tool grammars are on `main` behind `[experimental-features] tool-grammars`, but no page tells an
operator how to write one or how to write rules against it. `docs/user/config.md` said that a guide
was "coming later". The reference pages also did not list the fields that a grammar action carries.
An operator who adds a grammar can lose a `shell:spawn` forbid, permit `unmodeled` by mistake, or
write a temporal rule that a test in another directory satisfies.

The `config.md` entry told the operator to "move" the `shell:spawn` rules. That is not sufficient:
a copy of the program at another path is still a `shell:spawn`, so a broad `shell:spawn` permit
lets the copy past a grammar forbid.

## Solution

Add `docs/user/tool-grammars.md`. It is a how-to: turn on the switch, add a git grammar, forbid a
force push through its effect, start the box and read the startup line, and allow a push only after
`cargo test` exited 0 in the project. It states that a grammar action replaces `shell:spawn`, that
`unmodeled` is outside `"*"` and denied by default, that a rule covers one grammar, and that a
temporal predicate cannot name the group. It states which files a grammar binds, what a grammar
governs (a spawn that reaches the Shell, and not a child of a spawned program), and what it cannot
see (environment, configuration files, child processes, another name or a copy).

The page describes the behaviour of the multi-name change that is in progress: another name of a
bound file is decided as `shell:spawn`. It does not document the startup refusal for a file with
several names, because that change removes it.

Add a "Tool grammar actions" section to `docs/user/policy/actions.md` with each field and its
type. Change the `config.md` entry to link the guide and to say "keep `shell:spawn` permits
narrow". Add the page to `docs/user/README.md` and to `docs/site-manifest.json`.

## Tests

- Each behaviour claim is traced to a test on `main`: `tool_grammars.rs` (switch, startup line,
  forbid and permit, `unmodeled`, `sh -c`, push after a test, restart), the schema cases in
  `grammar/schema.rs` (effect of another grammar, two families, the group in a temporal predicate,
  `v2` namespace), the shell adapter tests (no `shell:spawn` for a bound program, `input.cwd` pin,
  durable response), `bindings.rs` (links, copies, replaced file), `box_shell.rs` (`xargs`), and the
  git corpus (`--dry-run` removes `force_push`, `-C` with a push).
- I extracted every `toml` and `cedar` block on the page into one `box.toml` and `policy.dw` and ran
  `strands-box run` on Linux. Both grammars and every rule loaded, and `run` printed both startup
  lines. The run then stopped at a mount-table check of this host, after validation.
- A local probe showed that a temporal predicate with `input.args: ["test"]` is refused, so the page
  pins the arguments through the grammar's `usage`.
- `cargo test -p strands-box --test site_manifest --all-features` passes.
- The `docs-reviewer` skill gave "Ship it" after three rounds. `check-prose.sh` is clean.
- Not run: the end-to-end grammar suite, because this host cannot start a box. The behaviour of
  another name of a bound file comes from the pending multi-name change and not from `main`.
Comment thread docs/user/tool-grammars.md Resolved
Comment thread docs/user/tool-grammars.md Resolved
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Assessment: Request Changes

Head 5751b92. The page is written against behaviour from an unmerged branch (feature/tool-grammars-multiname, which has no open PR). On main, the most common setups (Apple /usr/bin/git, and a rustup-managed cargo) give a startup refusal or a [tool-grammar] refusal where the page promises a shell:spawn. The page also contradicts the config.md refusal list in this same PR. Details are inline.

Checked and held
  • removes takes precedence over adds in any order (parse.rs adds.difference(&removes)), so git push --dry-run -f and git push -f --dry-run both lose force_push.
  • An undeclared option or an extra operand reads as unmodeled. This covers git push --delete, cargo test --manifest-path …, and cargo test -- x.
  • unmodeled is outside the "*" group (schema.rs, every_action_of_a_grammar_is_in_its_group_and_unmodeled_is_not).
  • A copy of the program at an unbound path is decided as shell:spawn, as the page and the updated config.md say.
  • A grammar action records ::response with output.status (actions.md table, test(policy): verify ::response events for tool grammar actions #333).
  • The startup line text matches command/run.rs:106, and the 126 deny format matches shell.md.
  • Frozen surfaces: the only one this PR changes is site-manifest.json, and that is an addition (new id tool-grammars, a new route, no retirement), which the manifest contract allows.

The walk-through itself is clear and well structured. Once the multi-name behaviour question is settled, it should be ready.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks. The blocking point is answered inline: this PR now states that it depends on the multi-name PR and must merge after it. The cargo test caveat is fixed in eb7b351.

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Assessment: Comment

Re-reviewed at eb7b351 against 5751b92. The cargo test caveat is fixed as suggested. The program-files section now explicitly describes the multi-name semantics. The only open point is how the merge-order dependency is enforced (reply inline). I found no new correctness issues in the changed text.

Checked on this head and held
  • The new caveat covers both ways an agent can turn a test run into a pass: a runner in the project's .cargo/config.toml, and #[ignore] on every test. An inline CARGO_TARGET_*_RUNNER=… can't do it, because the Shell's variables don't reach a tool spawn (hosted.rs builds the tool's own environment and never reads spawn.env).
  • The bare-name refusal when a bound file is gone or replaced matches bindings.rs (gone_under, and a_bare_name_whose_bound_file_is_gone_is_refused_and_does_not_fall_through_the_search_path).
  • The advice to match program as well as program_path holds. A bare name resolves on the host PATH, not the agent's proc.env (exec.rs), so the agent can't redirect a bare make to a link of its own.
  • Everything else is unchanged since the last review, and the earlier "checked and held" items still apply.

Apart from the dependency, this is ready.

@ferdingler
ferdingler marked this pull request as draft November 6, 2026 19:23
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Assessment: Comment

I re-reviewed at 33fd725 against eb7b351. The only change is the "Environment variables" bullet in "What a grammar can't see". I found nothing new to flag. The PR is now a draft, so the dependency on the multi-name change is enforced and my open thread is addressed. I'll check the page against the multi-name code once that branch is pushed.

Checked on this head and held
  • New env example: tool_spawner (hosted.rs) builds the spawn from the selected [tool.<name>] spec and never reads the Shell's spawn.env. So a GIT_SSH_COMMAND in the tool's own env changes what git runs while the grammar reads the same action. An inline X=y git push from the agent doesn't reach the tool. That makes the new example accurate where the old GIT_DIR=… git push one wasn't.
  • HOME defaults to the operator's, so ~/.gitconfig applies, and the "Configuration files" bullet covers that route.
  • Merge order: the PR is a draft, and feature/tool-grammars-multiname isn't on origin yet.

Once the multi-name PR merges and this is retargeted, I have no further findings.

Comment thread docs/user/tool-grammars.md Resolved
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Assessment: Request Changes

I reviewed head c4f2e74. This commit adds the bare-name rule for missing or replaced files, and that rule holds at this head. But the "other names" behaviour still comes from the unmerged multi-name branch. At this head, Apple /usr/bin/git and rustup's cargo get a startup refusal or a [tool-grammar] refusal where the page promises shell:spawn. The page also contradicts this PR's own config.md. Details are inline.

Checked and held
  • New in this commit: a bare name whose bound file is gone or replaced is refused, and the search path doesn't take over (gone_under, a_bare_name_whose_bound_file_is_gone_is_refused_and_does_not_fall_through_the_search_path).
  • A link called by the bound name is decided by the grammar, and a copy at an unbound path goes to shell:spawn.
  • removes takes precedence over adds in any order, so --dry-run drops force_push whatever the flag order.
  • An undeclared option or an extra operand reads as unmodeled, including --manifest-path and cargo test -- x. unmodeled is outside "*".
  • A grammar action records ::response with output.status, and the 126 deny format matches shell.md.
  • The site-manifest.json entry is a normal page addition, which docs/dev/site-manifest.md allows without a schema bump.

The walk-through itself is well organized. Once it describes the code that's actually in this tree, it should be close to ready.

Comment thread docs/user/tool-grammars.md Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

That review covered an older head (c4f2e74). Both points are handled: the cargo test caveat is in, and this draft now depends on #352.

@ferdingler
ferdingler marked this pull request as ready for review November 6, 2026 20:59
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Assessment: Request Changes

I re-reviewed at 81270b2 against 33fd725. The new commit brings the bound-file cases in line with #352, and every changed claim holds against #352's code (f9cf38b). The one blocker is merge order (see my reply in the existing thread): the PR is now out of draft and targets main while #352 is still open. Ignore the 19:43 "Request Changes" summary that names c4f2e74. It was stale, and this review replaces it.

Checked against #352 and held
  • Replaced file: only its bound names stay refused. replaced() requires (old identity, name) in bound, and a_replaced_file_at_a_bound_path_is_refused asserts git-lfs and gitk stay spawns while git is refused. That matches the new wording. (fix(policy): decide an undeclared name of a grammar-bound file as shell:spawn #352's description says the refusal covers "every name". Its code and test don't, and the page follows the code.)
  • Server-only name. An agent spawn is refused only when servers has (file, name), declared doesn't, and some grammar binds the file (binds_file). That matches "only a stdio server declares" and "a server on a file that no grammar binds isn't affected". A server's own start returns None, so shell:spawn decides it.
  • Pinning on program and program_path. program_path resolves links but keeps a hard link's own path, so /usr/bin/make stays /usr/bin/make. A workload symlink ./make gives program == "./make", and its program_path resolves to the target. A bare make resolves on the host PATH. So the example pin admits only the real make. It also matches fix(policy): decide an undeclared name of a grammar-bound file as shell:spawn #352's shell.md text.
  • Earlier "held" items (the env example, the cargo test caveat, removes beating adds, unmodeled, the manifest addition) are unchanged at this head.

akujane
akujane previously approved these changes Nov 6, 2026
@ferdingler

Copy link
Copy Markdown
Collaborator Author

The merge-order blocker is now closed. #352 is merged to main, and 0b403d6 merges main into this branch. The one conflict was in docs/user/config.md. The resolution keeps the link to this guide and the sentence from #352 that a shell:spawn rule still decides each other name of a bound file. docs/user/tool-grammars.md already agrees with main: an undeclared name of a bound file is decided as shell:spawn, and the refused cases are a replaced file, a bare name whose bound file is gone or replaced, and an agent spawn under a name that only a stdio [mcp.<name>] declares.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

Assessment: Approve

I re-reviewed at 0b403d6 against 81270b2. The merge-order blocker is closed: #352 is merged, and this head's merge base is current main (2aa281d). Against main, the PR now changes only docs (5 files). tool-grammars.md hasn't changed since my last review, and the merge commit only resolves the config.md conflict.

Checked on this head and held

Thanks for handling the merge order and the conflict resolution carefully.

@ferdingler
ferdingler merged commit 473e543 into main Nov 7, 2026
18 of 19 checks passed
@ferdingler
ferdingler deleted the feature/tool-grammars-guide branch November 7, 2026 04:08
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