Guard DSL

Write rules in plain English. They compile to safe JSON AST automatically. No eval(), no code injection.

Syntax

when <field> <operator> <value> then <action>

Rules are deterministic. The LLM never sees them. They run BEFORE every action.

Examples

when action contains 'delete' and env eq 'production' then block
when spend gt 100 then require_approval
when cpu_pct gt 90 then require_approval
when message contains 'urgent' then ask
when day_of_week eq 'Friday' and risk gt 0.7 then block_deploy

All 17 Operators

OperatorExampleDescription
eqstatus eq 'active'Exact equality
neqrole neq 'admin'Not equal
gtspend gt 100Greater than (numeric)
gtescore gte 650Greater than or equal
ltage lt 18Less than
lterisk lte 0.5Less than or equal
containsmsg contains 'urgent'Case-insensitive substring
not_containsemail not_contains 'spam'Negated substring
starts_withtool starts_with 'send_'Prefix match
ends_withfile ends_with '.sql'Suffix match
matchesphone matches '\d+'Regex match
inenv in ['production','staging']Membership in list
not_inrole not_in ['admin','owner']Not in list
is_trueapproved is_trueBoolean truthy
is_falseblocked is_falseBoolean falsy
is_emptynotes is_emptyNull or empty string in both SDKs. Whitespace-only and 0 are also empty in TypeScript, but not in Python.
is_not_emptynotes is_not_emptyThe inverse. Note 0: not empty in Python, empty in TypeScript — so a spend or count rule diverges on zero.

Logical Combinators

AND — all conditions must be true

when role eq 'admin' and mfa is_true and ip_allowed is_true then allow

OR — any condition can be true

when env eq 'staging' or env eq 'development' then allow

NOT — negate a condition

when not tool starts_with 'delete' then allow

Important details

Actions

Nine actions are valid. Any other word is rejected: the Python SDK raises ValueError rather than build a rule that can never fire.

ActionBlocks?allowedUse for
blockyesfalseStop it outright
denyyesfalseSynonym for block
block_deployyesfalseStop a deploy specifically
require_approvalyesfalseGate on a human. This stops execution — it is not a soft warning
suggestnofalseOffer a better command; caller decides
rewritenofalseSwap in a replacement command
asknofalsePrompt the user for a decision
allownotrueExplicitly permit, short-circuiting later rules
permitnotrueSynonym for allow

The three soft actions leave both blocked and allowed false. They are denials with a message, so if (result.allowed) is the safe test and if (!result.blocked) silently permits all three.

Cross-rule evaluation order

The first rule that matches wins, in the order you listed them. Evaluation stops there — severity is not considered, so a later block never runs if an earlier rule already matched.

when x eq 'hit' then suggest
when x eq 'hit' then block      # never reached -> action is "suggest"

when x eq 'hit' then block
when x eq 'hit' then suggest    # never reached -> action is "block"

when x eq 'hit' then allow
when x eq 'hit' then block      # never reached -> allow short-circuits

So order your rules most-restrictive first, and be careful with allow: a broad allow placed above a block disables it. Identical in the Python and TypeScript SDKs.

Operator precedence

and and or evaluate left-to-right with no precedence. a and b or c evaluates as (a and b) or c. Use separate rules instead of complex expressions for clarity.

Field names

Fields can be any key from the context object you pass to guard.check(). Common fields: action, env, tool, spend, amount, role, day_of_week. You define the context — the DSL just reads from it.

Case sensitivity

The two SDKs currently differ here. contains and not_contains are case-insensitive in both. But starts_with and ends_with are case-insensitive in the TypeScript SDK and case-sensitive in Python — tool starts_with 'SEND_' matches send_mail in TypeScript and does not in Python. Write rules that do not depend on case until this is reconciled.

Type coercion

Numeric operators (gt, lt, gte, lte) convert both sides to numbers in both SDKs, and eq coerces too: 100 eq '100' is true in TypeScript and in Python. Verified against both SDKs on 2026-11-23.

Using in code

import { createGuard } from '@savants.dev/guard';

const guard = await createGuard([
  "when action contains 'delete' and env eq 'production' then block",
  "when spend gt 100 then require_approval",
]);

// Check any context
const result = guard.check({ action: 'delete_db', env: 'production' });
// result.blocked === true
// result.rule === "when action contains 'delete'..."
// result.action === "block"

Python

The same DSL, the same nine actions, the same first-match-wins order. Install with pip install savants-guard (use a virtualenv — a bare pip install fails under PEP 668 on a managed Python).

from savants_guard import create_guard

guard = create_guard([
    "when action contains 'delete' and env eq 'production' then block",
    "when spend gt 100 then require_approval",
])

result = guard.check({"action": "delete_db", "env": "production"})
# result.blocked == True
# result.action  == "block"

Python presets — don't hand-write what already ships

Five presets cover the common cases, so you rarely start from an empty rule list:

from savants_guard import (
    production_safety,   # 8 rules: destructive ops against production
    spend_limit,         # gate above a cost ceiling
    deploy_safety,       # risky-window and deploy rules
    business_hours,      # time-of-day gating
    risk_aware_safety,   # scales with a computed risk score
)

guard = production_safety()
guard.check({"action": "delete_database", "env": "production"}).blocked
# True  (8 rules active)

spend_limit(100).check({"spend": 500}).action
# "require_approval"

Add your own rules at runtime with guard.add_rule(...) (addRule in TypeScript). It validates the same way the constructor does: a malformed rule raises rather than leaving you a guard with fewer rules than you wrote, and a comment or blank line is skipped rather than refused.

One difference between the SDKs today: an unknown action raises in Python and only warns in TypeScript, where the rule is kept and treated as non-blocking. Python's behaviour is the safer one — an unrecognised action builds a rule that can never fire — so do not rely on the warning.

Try it live: Paste a schema into the Playground and test guard rules interactively.