CarolineDocs

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:

A refusal says what is wrong and on which line.

Do not trust what the request says about itself

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

Caroline DocsAuthZEN 1.0 · OPA