Skip to content

feat(guardrails): enforce per-bot tool guardrails with delegation messages across engines - #67

Merged
zereight merged 1 commit into
mainfrom
feat/bot-tool-guardrails
Nov 26, 2026
Merged

zereight merged 1 commit into
mainfrom
feat/bot-tool-guardrails

Conversation

@zereight

@zereight zereight commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

What changed

Adds bot.toolGuardrails: per-bot rules the harness checks on every tool call, before the tool runs. A blocked call does not run; the model gets back Blocked by bot guardrail (<id>): <message>, so the rule can tell it where the work belongs instead.

Replaces the closed #66 (browser-only host policy). The browser itself is already switchable per bot (browser: false); the actual problem is bots ignoring SOUL/MEMORY rules with any tool.

PATCH /api/bots/:id { toolGuardrails: [...] }      admin-only; null clears
  store / bot-patch    validate, audit, widening waits for a durable save,
                       loopback widening refused while any bot (or room turn) is busy (409)
turn start
  turnGuardrailFiles   DATA_DIR/runtime/guardrails/<hash(bot,thread)>.json (0600)
  Claude   PreToolUse hook omb-tool-guard  (merged with the browser guard, not replacing it)
  Codex    -c hooks.PreToolUse (session flag + trust hash), hooks/list must report it trusted or no prompt is sent
  Pi       pi.on("tool_call") + MCP execute
  OpenAI-compatible chat  mountChatTools validate
  ACP / Muse Code  MCP gate only; native rules refuse the turn
  every MCP server  mcp-gate re-reads the policy file per tools/call, answers isError (server stays alive)
  saved-steps replay  a recipe whose step is blocked is not replayed without the model

Rule shape:

{
  "id": "delegate-pr",
  "enabled": true,
  "tools": ["native:shell", "native:web", "mcp:browser:*"],
  "match": { "patterns": ["bitbucket.org", "pull-requests"], "excludePatterns": ["--dry-run"] },
  "message": "PR work goes to @PRCreator (reviews to @PR Reviewer) through coordinate_bots."
}
  • tools: native:<alias|name glob> (aliases shell, edit, write, read, web), mcp:<server|*>:<tool glob>, or *.
  • match (optional; no match = every call of those tools): patterns (any), allPatterns (all), excludePatterns (none), fields (dot-path or key name), kind contains (default, case-insensitive) or regex.
  • Always on while any rule is active: shell/edit/write calls that touch the policy folder or bots.json are refused (guardrail-self-protection).
  • Restricted MCP: like toolScope, guardrails with mcp: selectors force the strict MCP configuration (no inherited user/project MCP servers).
  • Malformed directory/bridged calls (call_tool, use_tool without a valid name) fail closed.
  • Not injected into the system prompt; the refusal text is the correction.
  • Kept by team backup/restore; changes are logged in the admin activity audit.

Why

BankXDev opened a Bitbucket PR in the built-in browser even though its instructions say PR work goes to PRCreator / PR Reviewer. Instructions are soft; this makes the rule a hard stop that also returns the delegation hint.

How it was verified

Before / After with the real CodexDriver and codex app-server 0.172.0 (isolated HOME/CODEX_HOME, a local fake Responses API issuing fixed tool calls, a fake MCP server), same script on origin/main and on this branch:

step origin/main this branch
shell echo bitbucket.org/... > forbidden-shell.txt runs, file written blocked, no file
apply_patch adding a bitbucket line runs, file written blocked, no file
MCP open_page(bitbucket.org/...) reaches the MCP server blocked, never reaches it
MCP open_page(example.com/docs) runs runs
shell writing the policy file runs blocked (self-protection)
ordinary shell runs runs

After, MCP server log: ["https://example.com/docs"] only. Before: [".../pull-requests/7", "https://example.com/docs"]. Codex hooks/list reports the guard sessionFlags, trusted, and keeps the user's own hooks.json / config.toml PreToolUse hooks alongside it.

  • pnpm typecheck, pnpm lint pass.
  • 82 related vitest files: 2548 passed, 2 skipped, 0 failed (rules, mcp-gate, claude/codex/pi/chat/acp drivers, store, bot-patch, request-auth, team-backup, saved-steps, audit e2e). New tests that fail on the old code: the store does not lift guardrails before the save, Claude keeps the browser guard next to the tool guard, Codex refuses the turn when hooks/list does not show the guard trusted.
  • pnpm build and node scripts/bundle-server.mjs pass; bundled dist-server/hooks/omb-tool-guard.js run directly: Bitbucket shell and MCP payloads denied, ls allowed, missing policy file denied.
  • Preflight: the two failures (server-lines German server.notify.stillWaiting*, store-read-ratchet WorkflowsPage.tsx) also fail on main 29c0e6aef.

Limits

  • Behavior guardrail, not a sandbox: string matching does not see obfuscated shell, script files, or a link click / redirect whose arguments carry no URL.
  • Codex: not confirmed whether write_stdin, web_search, view_image pass through PreToolUse; Windows trust key unverified. Codex appends . Command: after the reason, so the message shows a double period.
  • ACP and Muse Code: MCP rules only; a native: rule refuses the turn with an explanation.
  • Pi's temporary mcp.json is outside self-protection.
  • Set via PATCH /api/bots/:id only (not on create); the settings UI is a follow-up PR.
  • Behavior change: the loopback 409 for widening toolScope now also counts running room turns.

Merge Danger

Door: two-way. Bots without toolGuardrails take the old paths (strictMcp/restricted are false unless a rule is active).

Blast Radius: guarded bots. Turns on a guarded bot fail closed when the engine cannot prove the guard is loaded (old Codex CLI, inherited Claude MCP config).

Checklist

  • pnpm typecheck and related tests pass locally
  • Server behavior changes come with tests
  • No dist-server/ edits
  • No shell: true / cmd.exe string-building: Claude passes args on Windows, but Codex only takes a hook command string, so on Windows it is a double-quoted argv line (untested on Windows)
  • No secrets in logs, responses, events, or argv

@zereight
zereight merged commit 41eaf24 into main Nov 26, 2026
19 of 21 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