Write the rules / Rules in Rego
Rules in Rego
When the rule model cannot say it, write the rule in Rego. The rule keeps its governance in YAML and its logic in one module that says only whether it matches. Everything else (deny overrides, shadow, the agent checks, bindings, the ledger) works exactly as for any rule.
When to use it
Prefer the model: it builds with blocks, drafts with AI, reads back as a sentence and is checked by the schema. Reach for Rego when a rule needs what the model has no word for: arithmetic across attributes, string patterns, logic over a nested structure.
Create one
Write it from your editor with the caroline command: a rule file with language: rego and its governance, and the module beside it under the same id.
# policies/rules/high-value-export.yaml
id: high-value-export
language: rego
effect: deny
owner: data-protection
review_by: '2027-06-30'
description: No export of a record set worth more than its owner's limit
# policies/rules/high-value-export.rego (value and export_limit stand for attributes of your own)
package caroline.rules.high_value_export
import rego.v1
default match := false
match if {
input.action.name == "export"
r := data.resources[input.resource.id]
u := data.users[input.subject.id]
r.value > u.export_limit
}
The package is caroline.rules. and the id with hyphens as underscores. A new rule starts from a template that matches nothing.
What a module may do
A module is code inside the engine, so Caroline fences it at save, and before any preview runs it:
- Its own package, and nothing else;
matchdefined. - Data read only by name:
data.usersanddata.resources, never as a whole or by a computed name, never the evaluator's or another rule's. - None of
http.send,net.lookup_ip_addr,opa.runtime,rand.intn,uuid.rfc4122. - Compiles strictly against the engine's own OPA version, and beside the evaluator, as the engine will load it.
- At most 64 KB.
A refusal says what is wrong and on which line.
input.subject.properties and input.resource.properties are the caller's own words. Read data.users and data.resources, which are what Caroline holds.
If the engine does not hold the module
It fails closed. An allow written in Rego matches nothing; a deny matches. The answer names the rule under rego_missing. A save publishes in an order that keeps this rare: a new or changed module reaches the engine before the rule document names it, and a removed rule leaves the document before its module goes.
Editing and reviewing
- On Policies its card reads Written in Rego; View as YAML shows the rule with its module. It is changed from your editor, never with the card's parts.
- Draft with AI writes the model only, never Rego.
- The review, impact and tests load each side's modules, so they decide as the engine would.
- From your editor,
caroline pullgives you both files; the editor's schema checks the YAML, and Caroline checks the module atcaroline preview.