Skip to content

feat(guardrails): add a log mode that records matching calls without blocking them - #80

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

zereight merged 1 commit into
mainfrom
feat/guardrail-log-mode

Conversation

@zereight

@zereight zereight commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Stacked on #79 (feat/guardrail-examples): the base of this PR is that branch, so the diff shows only the log mode. Merge #79 first, then retarget this PR to main.

Summary

A tool guardrail rule can now carry mode: "enforce" (default) or "log". A log-mode rule never blocks. A call it matches goes through, and one row is appended to guardrail-log/<YYYY-MM>.ndjson in the data folder:

{"at":"2026-12-01T17:57:19.326Z","botId":"evidence-bot","ruleId":"no-direct-git-push","tool":"native:Bash","blockedBy":null,"excerpt":"git log --grep push -1 --oneline\nShow the most recent commit whose message conta"}
  • excerpt is the text the rule read, 80 characters either side of its first match, with secrets masked. blockedBy is the enforced rule that blocked the same call, or null: it tells a call that is already blocked apart from a call that would be newly blocked once the log rule is switched to enforce.
  • Omitting mode is the same as "enforce", is not stored back, and changes nothing for existing rules.
  • One call is one row. Native tools are written by the Claude/Codex hook (server/hooks/omb-tool-guard.ts) and by Pi's tool_call. MCP tools are written by the MCP gate (server/mcp-gate.ts) and by Pi's execute. The OpenAI-compatible runtime (chat-mcp-tools.ts) writes both, skipping a rule it already wrote for the same call on a host computer tool.
  • Files are 0600 in a 0700 folder, one per UTC month, and are pruned on the decision log's timer and retention (decisions.retentionDays).

Security boundary

toolGuardrailsWidens now compares enforced rules only:

Change Widens? Who
enforce → log (also: replacing an enforced rule with a log copy under a new id) yes person only
log → enforce no the bot may do it
adding, removing, or disabling a log rule no (it never blocked) the bot may do it

app_settings bots.guardrails refuses enforce → log with the existing owner-only message. PATCH /api/bots/:id refuses it with 409 while another bot is working, and takes it with a browser Origin, as with every other loosening. Both are covered by tests.

Decisions

  • The deny is printed before any row is written, and a row that cannot be written is dropped. The hook ends in main().then(exit 0, exit 0), and an exit 0 with no output is read as allow (inferred from the hook contract; not run). A write error ahead of the deny would have switched an enforced rule off. Test: an unwritable log folder still denies.
  • Every rule is evaluated; the first enforced match blocks. Returning at the first match would hide a log rule listed after an enforced one from the log. Test: both orders.
  • The whole text is masked before the window is cut. Cutting a window first can split OPENAI_API_KEY= from its value and leave a fragment unmasked. Measured on the same input: cut-then-mask kept d4a1b2c3d4a1b2c3d4 of the key; mask-then-cut kept none. A mark follows the first match through the masking so the window stays centred.
  • The folder is not added to protectedPaths. Measured with the real matcher: with a rule active, tail -n 5 <log file> is blocked and cd <data> && rm -r guardrail-log is not, and a bot with no rules is not protected at all. So it would get in the way of reading and not stop tampering. The log is for watching a rule before enforcing it. It is not an audit record.
  • Muse Code and ACP are unchanged. They refuse a turn when any active native rule exists, a log rule included, because they cannot run the check. Writing no rows and reading as "0 matches" would be worse than failing loudly.
  • Policy files carry the log target (bot id, folder) so the hook process, which has no access to the store, can write.

Before / After

A real Claude turn (claude -p, haiku) in a throwaway repo whose only commit message contains "push". The turn is told to run git log --grep push -1 --oneline once. The hook is omb-tool-guard.ts, wired as in toolGuardHookSettings. The rule is no-direct-git-push (the pattern from the live rule).

Before (main 5ac219492 hook, rule without mode):

--- policy rule: {"id":"no-direct-git-push","mode":"(none)"}
BLOCKED
> PreToolUse:Bash hook error: Blocked by bot guardrail (no-direct-git-push): direct git push is blocked: use omb-push.
--- guardrail-log:
(no log folder)

A read-only git log is blocked and nothing records that it happened.

After (this branch, same rule with "mode": "log"):

--- policy rule: {"id":"no-direct-git-push","mode":"log"}
결과: `00dc041 push notes to the wiki`
명령은 차단되지 않음, 1회만 실행함.
--- guardrail-log:
{"at":"2026-12-01T17:57:19.326Z","botId":"evidence-bot","ruleId":"no-direct-git-push","tool":"native:Bash","blockedBy":null,"excerpt":"git log --grep push -1 --oneline\nShow the most recent commit whose message conta"}

The command ran and exactly one row was written. The excerpt starts with the command and continues with the Bash tool's description: a rule without match.fields reads every string in the input.

Verification

  • vitest run over the guardrail files and everything that imports them (25 files: shared/tool-guardrails, server/tool-guardrail-log, server/hooks, mcp-gate, routes/bot-patch, app-settings, store, team-backup, saved-steps/dispatch, request-auth, tool-permission-audit.e2e, and the pi, pi-mcp-extension, chat-mcp-tools, openai-chat, claude and codex drivers): 1688 passed, 1 skipped. Run with FORCE_COLOR and NO_COLOR unset: with the shell's FORCE_COLOR=3 NO_COLOR=1, Node prints a warning on stderr and server/hooks/omb-hook.test.ts fails on expect(stderr).toBe(""). That file is not touched here and passes without them.
  • New cases:
    • mode is stored only as log, and an invalid value is refused.
    • A log rule lets the call through and lists where it hit.
    • Default and explicit enforce produce the same verdict.
    • An enforced rule blocks beside a log rule in either order.
    • Widening: enforce → log is true; log → enforce, removing, disabling, and adding a log rule are false.
    • bot-patch and app_settings: log → enforce is accepted, enforce → log is refused.
    • Hook: no output and one row; the masked secret straddling the window; blockedBy; an unwritable folder still denies; MCP calls are left to the gate; no log target is quiet.
    • MCP gate: a log rule's call reaches upstream and is recorded once, and an enforced rule beside it blocks with blockedBy set.
  • The hook and the Pi extension both still bundle with esbuild the way scripts/bundle-server.mjs does it.
  • pnpm typecheck: exit 0. oxlint --deny-warnings .: exit 0.

Not covered

  • Codex's hook path was not run in a real turn. It runs the same omb-tool-guard.ts, which the Claude turn above exercised, and the Codex driver tests pass.
  • Pi and the OpenAI-compatible runtime are wired and type-checked, but there is no test that drives a log rule through them (none of their guardrail paths had one).
  • OMB_OWN_MCP_HTTP=1 Claude MCP calls may not pass the gate, so they would not be recorded. Not checked.
  • A month file has no size cap. A tools: ["*"] log rule on a busy bot grows it until retention prunes it.
  • Changing a rule's mode is not written to the log, and there is no settings UI for guardrails yet.
  • Windows was not run.

…blocking them

A rule can carry mode "log". A call it matches goes through, and one row (time, bot, rule, tool, the rule that blocked the same call if any, and the masked text around the match) is appended to guardrail-log/<YYYY-MM>.ndjson in the data folder. Omitting mode keeps today's behavior: the rule blocks.

The log is written by the Claude and Codex hook for native tools, by the MCP gate for MCP tools, and by Pi and the OpenAI-compatible runtime for both, so one call is one row. The deny is printed before any row is written, and a row that cannot be written is dropped, so a log rule never turns into a block.

Switching an enforced rule to log is a loosening, so only the person can do it; log to enforce is a tightening and stays open to the bot. Rows follow the decision log's retention.
@zereight
zereight changed the base branch from feat/guardrail-examples to main December 2, 2026 01:25
@zereight
zereight merged commit b5b7b4b into main Dec 2, 2026
12 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