GuardrailCustom rules with CEL

CEL engine

Look up how DefenseClaw runs CEL rules: the evaluation pipeline, what a rule pack must pass to load, the supported CEL subset, resource limits and results.

DefenseClaw uses CEL as a bounded matcher, not a general policy language. The gateway turns a tool call into typed ActionFacts and runs one Boolean expression per rule. How to write a rule: Custom rules with CEL.

Evaluation pipeline

Each rule is checked on its own. CEL runs only on calls the parser understood in full. A match is checked a second time against only the commands that will really run.

checks
yes
no
error, over budget
false
true
yes
no
raw match
Parses the argumentsinto ActionFacts
Fully understood?nothing unsupportedor ambiguous
CEL matches?on the full facts
Regex pattern runsinstead of CEL
No findingregex fallback skipped
Matches what really runs?execute-only view
Detection onlyrecorded, action unchanged
Enforcement candidateto the final proof check

List view for small screens. Use the expand button to open the drawing.

  1. Parses the argumentsinto ActionFacts
    • checksFully understood?
  2. Fully understood?nothing unsupportedor ambiguous
    • yesCEL matches?
    • noRegex pattern runs
  3. CEL matches?on the full facts
    • error, over budgetRegex pattern runs
    • falseNo finding
    • trueMatches what really runs?
  4. Regex pattern runsinstead of CEL
    • raw matchDetection only
  5. No findingregex fallback skipped
  6. Matches what really runs?execute-only view
    • yesEnforcement candidate
    • noDetection only
  7. Detection onlyrecorded, action unchanged
  8. Enforcement candidateto the final proof check
GatewayPolicy layerDecision
How one rule is checked. The execute-only view drops previews such as --help or --dry-run. A match also stays detection-only when the connector cannot block or the rule is detection-only. A regex match stays detection-only unless a bounded exact proof backs it.
Text version of this diagram
  • Parses the arguments: into ActionFacts (gateway)
    • checks to Fully understood?
  • Fully understood?: nothing unsupported, or ambiguous (decision)
    • yes to CEL matches?
    • no to Regex pattern runs
  • CEL matches?: on the full facts (decision)
    • error, over budget to Regex pattern runs
    • false to No finding
    • true to Matches what really runs?
  • Regex pattern runs: instead of CEL (policy layer)
    • raw match to Detection only
  • No finding: regex fallback skipped (component)
  • Matches what really runs?: execute-only view (decision)
    • yes to Enforcement candidate
    • no to Detection only
  • Detection only: recorded, action unchanged (component)
  • Enforcement candidate: to the final proof check (component)

Every finding, from CEL or a fallback, then passes one final check: it can change the action only with a complete proof pinned to the same rule ID, on a connector that can block. The check never removes a finding. A built-in rule marked alert-only can raise at most alert.

ActionFacts and raw tool arguments stay inside the gateway. They are not exposed through an endpoint, logged as CEL input or written to the state ledger.

Loading a rule pack

The loader compiles every expression, disabled rules included. Each must be Boolean, have no surrounding whitespace and use only the subset below, and its rule must have a valid pattern fallback.

  • A custom category with a new name adds rules.
  • A category with the name of a shipped category replaces that whole category.
  • Any schema, regex, type, enum or cost failure rejects the whole pack. A gateway that already runs a pack keeps it.

Supported CEL subset

SupportedExamples and constraints
Boolean logic&&, ||, !
Scalar comparison==, !=, and in for Boolean, integer and string values
Collection predicatesexists and all comprehensions; nesting is limited to two levels. A comprehension must give a Boolean, so validate-pack refuses map, filter and exists_one (semantic_surface)
String predicatesstartsWith, endsWith, contains, and member-form matches
LiteralsBoolean, integer, string and scalar lists
Field selectionTyped fields reachable from f, in snake case

Refused: arithmetic, ordering comparisons, indexing, ternaries, maps, has, null, float, unsigned and byte literals, other functions, and message or list equality. There are no custom helper functions. matches takes a literal RE2 pattern of at most 512 bytes; dynamic patterns and matches(value, pattern) are refused.

Resource limits

LimitMaximum
Expression size16 KiB
Checked syntax tree4,096 nodes; depth 64
Comprehension nesting2
CEL regex literal512 bytes
Fallback regex2,048 bytes
Static cost per rule6,000,000
Runtime cost per rule1,250,000
CEL rules per pack256
Static cost of all enabled rules32,000,000
Total cost per tool call24,000,000 (a cost bound, not a time limit)

A pack over a load limit is refused. A rule over a runtime limit falls back to its regex; other rules keep running.

The 512-byte CEL regex limit caps its size; the static cost limit can reject a shorter pattern. Check the whole rule with defenseclaw guardrail validate-pack. Its refusal messages:

ProblemMessage
Nesting too deepit nests exists() and all() too deeply; flatten the check or split it into separate rules
Over the static costestimated worst-case evaluation cost is above the per-rule limit. Test one list, prefer ==, in or startsWith over contains or matches in a nested comprehension, or split the rule.

Runtime results

ResultWhat happens
The pack fails to compile or loadIt is not used.
The call was not parsed in fullThe rule's regex fallback runs; a match is detection-only.
CEL returns falseNo finding from this rule.
CEL matches all facts but not the execute-only viewDetection only: a preview cannot block.
CEL matches both on a connector that can blockEnforcement candidate, subject to the final proof check.
CEL matches on a connector that cannot blockDetection only.
CEL errors, times out or runs over costThe regex fallback runs; other rules continue.

Some partly parsed commands are still read in full for CEL:

  • A redirect target expanded at run time (> "$HOME/out.txt"): the command is analyzed with a stand-in target. A match counts when the expression does not depend on the redirect or path.
  • && and || lists (cd dir && cmd): a block stops the whole call, so each list is judged as if every command runs. A rule that blocks a; b also blocks a && b.
  • A PowerShell list that is partial because of one statement: statements are read in order up to the first that cannot be proved.

A fallback match and a CEL match never produce two findings for the same rule. Custom rules own their own rule ID.

Single calls and chains

A CEL rule matches one call. The 26 ordered chains are a fixed, built-in list; a rule pack cannot add one. Only authenticated connector hook events with a session and invocation ID can advance a chain. Inspect, proxy and telemetry traffic never do. The chain list is on Deterministic detection, and which connector events count is on Tool-call state.

Test engine behavior

defenseclaw guardrail validate-pack checks schema, regex, CEL typing and cost. It does not inject facts or prove that a connector supplies the fields an expression reads, so test a match and a near miss on each connector in observe mode.