Agent Governance Toolkitagent-governance-toolkit 4.1.0 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
23 small wins to finish your pathNext lesson

Which rule wins

Lesson 11 had one rule at a time. Real policy has a general rule and an exception to it, and the order they are checked in decides which one the reader gets.

Two rules that disagree: refunds are capped at a hundred, except for one order the shop has already agreed to refund in full.

yaml
rules:
  - name: allow_goodwill_refund
    condition: {field: order_id, operator: eq, value: "A17"}
    action: allow
    priority: 10
  - name: block_big_refunds
    condition: {field: amount, operator: gt, value: 100}
    action: deny
    priority: 0
    message: Refunds over 100 need a human

Rules are sorted by priority, highest first, and the first one whose condition matches decides. Nothing after it runs.

Example
from agent_os.policies.schema import PolicyDocument
from agent_os.policies.evaluator import PolicyEvaluator

evaluator = PolicyEvaluator(policies=[PolicyDocument.from_yaml("refunds.yaml")])
for order_id in ["A17", "B22"]:
    decision = evaluator.evaluate({"order_id": order_id, "amount": 4000})
    print(order_id, decision.allowed, decision.matched_rule)

Same amount, different answers, because the exception was checked first and stopped the search.

What happens without priorities

Leave every priority at its default of zero and the sort becomes stable rather than meaningful: the rules keep the order they were loaded in. That works until somebody reorders the file for tidiness and quietly changes the policy.

Give every rule an explicit priority once a document has more than one. A policy whose behaviour depends on the order lines happen to sit in a file is a policy that changes when nobody meant to change it.

Priorities are compared across every loaded document, not just within one file. Two documents in the same evaluator are flattened into a single sorted list, so a rule in one file can pre-empt a rule in another.

Try it yourself
  • Set both priorities to the same number and swap the two rules in the file.
  • Give block_big_refunds a priority of 20 and watch the goodwill exception stop working.
  • Add a third rule with priority 5 and predict where it lands before running it.

You understood something today that you didn't yesterday.