Skip to content

Policies

Policies are Rego files under policies/, evaluated by the Open Policy Agent embedded in Sefra. They decide whether a step may run, and who must approve it first. They never call a system: what they need to know comes in as facts.

One package per domain, package policies.<domain>, in Rego v1 syntax (if, contains). Each package may define:

Rule Type Meaning
allow boolean Must be true for anything to run. No rule that allows means a denial
deny set of violations Any member denies, whatever allow says
require_approval set of requirements Each member must be satisfied by a human’s approval before the step runs

Sefra adds its own package sefra, whose decision merges every domain package: allow if any package allows, the union of every deny and every require_approval. A bundle may not declare a package named sefra.

A violation has rule (a stable id such as procurement.budget_exceeded), message and, where they help the agent correct itself, path (a JSON pointer into the input), actual, limit and hint. The agent gets them in the envelope’s error.violations. Write messages and hints for the agent: say what is wrong and what would be accepted.

A requirement has rule, message, approver: { role }, distinct_from_requester (the approver may be neither the agent nor the user it acts for) and optionally min_evidence (token, oidc). Requirements are merged by role, so one approver with the role satisfies every requirement naming it; two requirements need two people only if they name two roles.

{
  "phase": "preflight",
  "principal": { "id": "agent:procurement-bot", "type": "agent", "roles": ["procurement_agent"],
                 "on_behalf_of": { "id": "user:u_jonas", "roles": ["buyer"] } },
  "intent": { "type": "procurement.create_purchase_order", "version": 1, "effect": "write", "irreversible": false },
  "input": { "vendor_id": "v_1001", "quantity": 10, "unit_price_minor": 120000, "currency": "EUR", "department": "it-ops" },
  "facts": { "budget": { "remaining_minor": 5000000, "currency": "EUR" }, "vendor": { "status": "active" },
             "sefra": { "duplicates_24h": 0, "denials_10m": 0 } },
  "unresolved": { "input": [], "facts": [] },
  "plan": { "size": 1, "index": 0, "intents": ["procurement.create_purchase_order"] },
  "env": { "time": "2026-11-17T10:15:00Z", "bundle_digest": "sha256:…" }
}

Sensitive fields arrive as { "$redacted": "<hash>" }: a rule can compare two of them for equality and cannot read the value. facts.sefra holds the built-in facts: duplicates_24h (the same principal’s accepted executions in the last 24 hours with a step of the same intent and identical input) and denials_10m. Exactly this document is stored with the decision, which is what makes replay exact.

Policies are compiled with restricted capabilities: every built-in OPA marks non-deterministic (http.send, time.now_ns, rand.intn, …) is unavailable, so a decision depends on its input document alone. Use input.env.time for the time.

Each step is evaluated at pre-flight, during submission, and again at step time, just before it runs. In a plan, a step’s input may refer to an earlier step’s output; at pre-flight that field is absent from input and named in unresolved.input, and facts that read it are absent too. Hence:

  • Write allow on what is known at submission: the principal and the intent type.
  • Write data-dependent constraints as deny. A deny that reads an absent value does not fire at pre-flight, and fires at step time.
  • Make approval rules depend on the agent’s input, not on facts or unresolved values. An approval requirement first discovered at step time fails the step with APPROVAL_SCOPE_EXCEEDED, because the approval already given did not cover it.
  • Fail closed where absence is not “unknown yet”: the starter’s procurement.currency_not_configured denies an order in a currency without thresholds, instead of skipping the approval rules, and binds the value first (currency := input.input.currency) so that it does not fire while the currency is unresolved.

Facts can change between the step-time decision and the call; the system of record remains the final enforcer of its own invariants.

Every rule has tests in a _test.rego file, run by sefra bundle test with OPA’s test runner and coverage. The starter bundle shows the pattern:

package policies.procurement_test

import data.policies.procurement

denied(x) := {v.rule | some v in procurement.deny with input as x}

# procurement.budget_exceeded
test_budget_exceeded_above_remaining if {
	"procurement.budget_exceeded" in denied(object.union(order, {"facts": {"budget": {"remaining_minor": 1199999}}}))
}

test_budget_exceeded_not_at_exactly_remaining if {
	not "procurement.budget_exceeded" in denied(object.union(order, {"facts": {"budget": {"remaining_minor": 1200000}}}))
}
  • One clean input that passes every rule, and one change of it per test (object.union, json.remove).
  • Test each rule on both sides of its boundary, and test that it does not fire while its value is unresolved.
  • Test the whole decision at least once through data.sefra.decision, as Sefra queries it.
  • Put settings in named constants and test a changed setting with with procurement.<name> as ….
  • Name each test after the rule it covers; a reviewer can then see which rules are untested. Aim for 100 % coverage; sefra bundle test prints it.

Tests prove the rules do what their author meant. Whether that is the right rule is the review’s question, and replay shows what a change would have done to real decisions (testing and activation).