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:

ts

12 extended presets are also re-exported from the main package for input/output scanning, PII handling, and resource ceilings:

ts

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.

ts

Preset Reference

blockTools

Block specific tools from being called. The most common policy.

ts

allowOnlyTools

Inverse of blockTools — only listed tools are permitted. Everything else is blocked.

ts

requireApproval

Flag specific action types for human review. Returns a "requires_approval" outcome instead of blocking.

ts

tokenBudget

Limit token usage per session. Blocks actions when budget is exceeded.

ts

rateLimit

Declarative threshold check. The SDK checks a caller-supplied count against the threshold — it does not track counts itself.

ts

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.

ts

requireSequence

Require prerequisite tools to run before a target tool. Useful for CI/CD-style pipelines.

ts

timeWindow

Restrict actions to specific time windows. Block deployments outside business hours.

ts

Boolean Combinators

Compose complex policies by combining conditions with any_of (OR), all_of (AND), and not (NEGATE). Nest infinitely.

ts

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.

PriorityRuleNote
999Kill switch (id prefix __kill_switch__)Reserved — only internal __-prefixed rules may use this
998Maximum user priorityAnything higher passed by a user rule is clamped to this
130mlInjectionGuard({ threshold: 0.7 })Preprocess stage
100blockTools(['shell_exec'])Tool allowlist/blocklist tier
95requireLevel(2)Agent-level gate
80requireApproval(['payment'])Approval-queue gate

Policy Composition

Merge policy sets from different teams with conflict resolution. Import from governance-sdk/policy-compose.

ts

Note: When teams disagree, strict picks the stricter rule. Use priority to let higher-priority rules win regardless.