Policy Engine
The in-process Go evaluator. Its contract, inputs, guarantees, performance, and how it is loaded and called by the gateway.
runtime/policy-engine is a Go package, not a service. The gateway links it directly and calls one function per authorization:
func Evaluate(organization *Source, agent *Source, action string, resource string) (Decision, error)Contract
| Input | Source |
|---|---|
organization | The active organization policy's current version, or nil |
agent | The active agent policy's current version for the authenticated agent, or nil |
action | The request's operation |
resource | The request's resource |
| Output | Meaning |
|---|---|
Decision{Outcome, Explanation} | allow, deny, or require_approval, with policy id, version, matched rule, and reason |
error | A document failed validation. The gateway records deny with policy evaluation failed |
The package defines exactly three outcomes. ParseOutcome accepts only those names, case sensitive, so an unexpected value from storage cannot be interpreted as an allow. Deny(reason) builds a fail-closed decision for callers.
Guarantees
- Pure. No clock, network, filesystem, or database access. The same inputs return the same decision.
- Total. Every input returns a decision or an error. There is no implicit allow.
- Order-independent. List order and document order never change the result.
- Self-validating. Both documents are validated on every call, even though the control plane validated them on write.
- No storage clients. Loading policy is the gateway's job. The package cannot grow a dependency on a store.
The precedence rules and resource-rule semantics are documented in Policy evaluation, with an interactive simulator.
Loading
For each authorize request, the gateway opens a transaction, sets app.organization_id, and loads at most two rows:
-- organization policy
SELECT p.id, v.id, v.version, v.document, v.document_hash
FROM security_policies p
JOIN policy_versions v ON v.id = p.current_version_id AND v.organization_id = p.organization_id
WHERE p.organization_id = $1 AND p.status = 'active' AND p.scope = 'organization';
-- agent policy: same join, plus p.agent_id = $2 AND p.scope = 'agent'policy_versions is the only authorization source. The agent_permissions table is a display projection written alongside a policy activation; the evaluator never reads it.
Documents are immutable
Every edit appends a new policy_versions row. A database trigger rejects updates and deletes of versions. Each decision records the policy id and version it used, so any historical decision can be re-derived from the exact document that produced it.
Performance
Recorded medians from benchmarks/README.md:
| Benchmark | Median | Allocations |
|---|---|---|
Evaluate/small (5 entries per list) | 13.8 µs | 4 |
Evaluate/large (200 entries per list) | 565 µs | 47 |
Most of the cost is re-validation. If policies grow to hundreds of entries, the documented optimization is to validate once per loaded version.
Extending the evaluator
The Decision type is the extension point. A future evaluator, for example one that compiles these documents to Rego or calls OPA, must:
- Return only the three outcomes.
- Preserve deny-over-approval-over-allow precedence across both documents.
- Default to deny for unmatched actions and missing policy.
- Return an error, never an allow, when it cannot decide.
Implementation references
Was this page helpful?