Policies & Rules
9 core presets + 12 extended presets + ML classifier hook. Condition types, boolean combinators, priority clamp.
Policies are the core of governance-sdk. Every enforce() call evaluates your policies against the proposed action and returns allow, block, warn, require_approval, or mask.
Policy Presets
9 core presets cover most governance needs. Import them directly from the main package:
12 extended presets are also re-exported from the main package for input/output scanning, PII handling, and resource ceilings:
mlInjectionGuard bridges the synchronous policy engine with an async ML classifier. Your host runs the classifier before enforce() and populates ctx.mlInjectionScore; the preset reads that pre-computed score and blocks when it crosses the threshold.
Preset Reference
blockTools
Block specific tools from being called. The most common policy.
allowOnlyTools
Inverse of blockTools — only listed tools are permitted. Everything else is blocked.
requireApproval
Flag specific action types for human review. Returns a "requires_approval" outcome instead of blocking.
tokenBudget
Limit token usage per session. Blocks actions when budget is exceeded.
rateLimit
Declarative threshold check. The SDK checks a caller-supplied count against the threshold — it does not track counts itself.
Warning: This is a declarative check, not server-side rate limiting. For production rate limiting, use the governance API with Upstash/Redis.
requireLevel
Require agents to reach a minimum governance score level before acting.
requireSequence
Require prerequisite tools to run before a target tool. Useful for CI/CD-style pipelines.
timeWindow
Restrict actions to specific time windows. Block deployments outside business hours.
Boolean Combinators
Compose complex policies by combining conditions with any_of (OR), all_of (AND), and not (NEGATE). Nest infinitely.
Priority Ordering
Rules are evaluated in priority order — higher numbers win. User priorities are clamped at 998 so the kill switch (priority 999, reserved __ id prefix) remains the unconditional top rule. If you pass a user rule with priority: 1000, the engine silently rewrites it to 998 at registration time so no one can beat the kill switch.
| Priority | Rule | Note |
|---|---|---|
| 999 | Kill switch (id prefix __kill_switch__) | Reserved — only internal __-prefixed rules may use this |
| 998 | Maximum user priority | Anything higher passed by a user rule is clamped to this |
| 130 | mlInjectionGuard({ threshold: 0.7 }) | Preprocess stage |
| 100 | blockTools(['shell_exec']) | Tool allowlist/blocklist tier |
| 95 | requireLevel(2) | Agent-level gate |
| 80 | requireApproval(['payment']) | Approval-queue gate |
Policy Composition
Merge policy sets from different teams with conflict resolution. Import from governance-sdk/policy-compose.
Note: When teams disagree,
strictpicks the stricter rule. Usepriorityto let higher-priority rules win regardless.