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.
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 humanRules are sorted by priority, highest first, and the first one whose condition matches decides. Nothing after it runs.
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.
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.
- Set both priorities to the same number and swap the two rules in the file.
- Give
block_big_refundsa 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.