Skip to content

feat(guardrails): check a rule's deny and allow examples when it is saved - #79

Merged
zereight merged 1 commit into
mainfrom
feat/guardrail-examples
Dec 2, 2026
Merged

zereight merged 1 commit into
mainfrom
feat/guardrail-examples

Conversation

@zereight

@zereight zereight commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Summary

A tool guardrail rule can now carry examples: calls it must block (deny) and calls it must let through (allow). When the rule list is saved, every example runs through that rule's own matcher, and a save whose examples disagree is refused with 400 that names each failing example.

{
  "id": "no-direct-git-push",
  "tools": ["native:shell"],
  "match": { "kind": "regex", "patterns": ["(^|[;&|])\\s*[^;&|'\"]*\\bgit\\b[^;&|'\"]*\\spush\\b"] },
  "examples": {
    "deny": ["git push origin feat/x"],
    "allow": ["git stash list", { "tool": "Bash", "input": { "command": "git log --oneline", "description": "Check commits before a push" } }]
  },
  "message": "Push only through omb-push."
}
  • An example is a command string, or a { "tool", "input" } call. A string goes through the rule's argument matcher. A call goes through the rule's tool selectors and its matcher with the exact input, the way a real call is judged.
  • A rule that reads named match.fields takes only { tool, input } examples. A string would have to be copied into every field, and that cannot catch a field name the engine does not send, such as fields: ["command"] against Codex exec_command, which sends { "cmd": ... }.
  • The check runs at the save boundary: PATCH /api/bots/:id (server/routes/bot-patch.ts) and the bots.guardrails value check in app_settings (server/app-settings/registry-bots.ts). app_settings writes through the same PATCH route, so both paths refuse the same way. The app_settings help text and allowed describe the new field.

Why

While trying out no-direct-git-push on a bot, git stash push -m wip and git log --grep push were blocked. Nothing caught them before the rule was saved. They only showed up when the bot ran them. Examples put the intended allow and deny cases next to the rule, and saving proves them.

Decisions

  • Not checked in parseToolGuardrails. parse also runs on every tool call: the hook process and the MCP gate parse the policy file per call, and a parse failure becomes invalid-policy, which blocks every call of that bot. If a later matcher change made a saved example disagree, checking examples in parse would lock the bot out. parse checks only the shape of examples. The agreement check lives in toolGuardrailExampleFailures and runs where rules are saved.
  • Not part of the widening check. Examples do not change what a rule blocks, so canonicalRuleSignature leaves them out. A bot may add, change, or remove examples on its own, and a person is not needed.
  • Team backup restore writes through store.patchBot and does not run the example check. That path does not change what a rule blocks either.

Before / After

Same script, run against a real verification server (launchVerificationServer, temporary data dir), with the rule pattern copied from the live no-direct-git-push rule.

Before (main 5ac219492):

{"step":"save rule without examples","status":200}
{"step":"save rule with allow example git stash push -m wip","status":400,"error":"Unknown tool guardrail field: examples."}
{"step":"save rule with matching examples only","status":400,"error":"Unknown tool guardrail field: examples."}
{"step":"call time","command":"git stash push -m wip","blocked":true}
{"step":"call time","command":"git log --grep push","blocked":true}

The rule saves silently, there is no way to state the intended allow cases, and the false positives appear only at call time.

After (this branch):

{"step":"save rule without examples","status":200}
{"step":"save rule with allow example git stash push -m wip","status":400,"error":"Guardrail examples disagree with their rules: \"no-direct-git-push\" blocks allow example \"git stash push -m wip\". Fix the match or the example."}
{"step":"save rule with matching examples only","status":200}
{"step":"call time","command":"git stash push -m wip","blocked":true}
{"step":"call time","command":"git log --grep push","blocked":true}

The save that claims git stash push -m wip is allowed is refused and names the example. Examples that agree with the rule save. What the rule blocks at call time does not change.

Verification

  • npx vitest run shared/tool-guardrails.test.ts server/routes/bot-patch.test.ts server/app-settings/host.test.ts server/store.test.ts server/team-backup.test.ts server/drivers/claude.test.ts server/drivers/codex.test.ts server/saved-steps/dispatch.test.ts server/tool-permission-audit.e2e.test.ts server/request-auth.test.ts: 10 files, 1081 passed, 1 skipped.
  • New cases: parse refusals for examples, toolGuardrailExampleFailures, including a description that makes a field-less rule block git log --oneline and a Codex cmd input missed by fields: ["command"], examples-only changes not counting as widening, a 400 from the PATCH route while another bot is busy, and a 400 from app_settings set.
  • pnpm typecheck: exit 0. oxlint --deny-warnings .: exit 0.

Not covered

  • There is no settings UI for guardrails yet, so examples are set through the API or app_settings only.
  • The record-only mode ("log") is a separate PR stacked on this branch.

…aved

A rule can list calls it must block (examples.deny) and calls it must let through (examples.allow): a command string, or a {tool, input} call when the rule reads named fields or the whole input matters. PATCH /api/bots/:id and app_settings bots.guardrails run each example through the rule's own matcher and refuse the save with 400, naming every example that disagrees. Examples do not change what a rule blocks, so they stay out of the widening check, and enforcement never re-checks them.
@zereight
zereight merged commit 20c0d9a into main Dec 2, 2026
13 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant