Pillar: policy
Policy on live facts
Rego on embedded OPA decides each step, on facts read from the system of record at decision time and again just before execution.
Read the policy guideWhat goes wrong without it
- A limit checked against an old copy of the budget passes an order that today's budget cannot cover.
- An agent stuck in a loop repeats a valid write. Each call looks fine on its own, and the duplicates reach the system.
How it works
A rule can allow, deny, or require approval by a role. Allowing must be positive: if no rule allows, the request is denied. Evaluation is pure, with no network access and an injected clock, so the same input, facts and policy version always give the same decision.
# procurement.budget_exceeded: the order would overspend the department.deny contains { "rule": "procurement.budget_exceeded", "message": "Order total exceeds remaining department budget.", "path": "/quantity", "actual": order_total, "limit": input.facts.budget.remaining_minor, "hint": "Reduce the quantity or request a budget increase.",} if { is_order order_total > input.facts.budget.remaining_minor}Scroll sideways for long lines.
Pre-flight
During submission, for every step. It refuses early and computes the one approval the whole plan needs.
Step time
Once per step, immediately before its first call, with references resolved and facts read again. This is the authoritative gate.
A threshold, and its test
Every bundle carries its tests, and applying a bundle whose tests fail is refused. The previous bundle stays active.
# procurement.large_order: a finance approver, not the requester, approves# orders from the first threshold.require_approval contains { "rule": "procurement.large_order", "message": sprintf("Order total %d minor units is at least %d.", [order_total, approval_threshold_minor[input.input.currency]]), "approver": {"role": approver_role}, "distinct_from_requester": true,} if { is_order order_total >= approval_threshold_minor[input.input.currency]}Scroll sideways for long lines.
test_large_order_at_the_threshold if { # 17 x 120000 = 2,040,000; the threshold is 2,000,000. approvals(order_of(17)) == {"procurement.large_order"}}…test_large_order_not_below_the_threshold if { # 16 x 120000 = 1,920,000. count(approvals(order_of(16))) == 0}Scroll sideways for long lines.
Replay before you activate
A candidate bundle is evaluated against the recorded decisions, with their recorded inputs and times, and every decision whose effect would change is listed. Decisions whose output changes but whose effect does not are counted. The example below lowers the approval threshold from 20,000 to 10,000 over the traced example. Its sequence numbers and digest are illustrative, and its format is the real output's.
candidate ./candidate-bundle sha256:5be1…seq 4107 exe_…A3 step 0 preflight procurement.create_purchase_order: allow -> require_approval; rules [] -> [procurement.large_order]seq 4109 exe_…A3 step 0 step procurement.create_purchase_order: allow -> require_approval; rules [] -> [procurement.large_order]seq 4122 exe_…B step 1 preflight procurement.create_purchase_order: allow -> require_approval; rules [] -> [procurement.large_order]seq 4131 exe_…B step 1 step procurement.create_purchase_order: allow -> require_approval; rules [] -> [procurement.large_order]replay: 8 decisions from seq 4106; 4 change effect, 0 change output only, 0 not replayedLimits on a looping agent
- Idempotency keys
- A key is stored with a fingerprint of the payload. A repeat returns the same execution, and the same key on different content is refused.
- Rate limits
- Token buckets per principal, and per intent where configured. An empty bucket returns RATE_LIMITED with retry_after_s.
- Denial breaker
- A principal with 20 or more validation or policy rejections during submission within 10 minutes is suspended until an operator lifts it. FACT_UNAVAILABLE does not count.
- Kill switches
- Suspend a principal, disable an intent or pause everything. They act on the next check and never stop a compensation.
What it does not do
- A wrong request that policy allows will execute. Sefra bounds the damage to what the rules permit.
- A policy that is wrong but valid cannot be detected by Sefra. Tests, review in git and replay before activation reduce the risk.
- Between the step's check and its call, including any retries, the record can change. Sefra does not lock external state, and the system of record stays the final enforcer of its own rules.
- Rate limits count requests. A spending cap is a policy rule over facts.
How it behaves
Automated tests cover each of these behaviours.
- An order over budget is refused by a named rule, and your system receives reads only.
- The same request with the same key returns the same execution, marked as replayed, and creates nothing new.
- The same key with a different quantity is refused with IDEMPOTENCY_KEY_REUSED.
- Where a duplicate rule is in place, the same input with a new key is denied.
- A burst above the rate limit is refused with RATE_LIMITED, and the agent is told when to try again.
- Repeated invalid calls suspend the agent until an operator lifts the suspension.
- If a fact cannot be read, the request is refused with FACT_UNAVAILABLE and nothing is written.
- An operator can disable an intent, and the agent is refused until it is enabled again.
- A rule that fails to evaluate gives POLICY_ERROR, which is a denial.
- Replaying a lower approval threshold lists exactly the decisions whose effect would change, and no others.