Decisions and combining
How several matching rules combine, what a Decision carries, and how grants turn an ask into an allow.
Code blocks on this page are excerpts of tested programs; each block's title names its file in examples/, where the complete program runs.
Most restrictive wins
Every rule is evaluated. The effect is the most restrictive among the rules that match: deny > ask > allow. If
no rule matches, the policy's default applies (deny unless set). This gives three properties first-match-wins
lacks:
- Order does not matter, so policy files merge safely and reordering cannot open a hole.
- A broad
allowcan never mask a narrowdeny. Exceptions are explicit, withunless. - When several
askrules match, the approval needs every matched rule's approvers.
The idiom it rewards: a broad allow, then narrow ask and deny rules.
gate = hitl.Gate(
{
"approvers": {"sales": {"channel": "sales-inbox", "members": ["sales:ana"]}},
"rules": {
"discounts": {"allow": "apply_discount"},
"big-discounts": {
"ask": "apply_discount",
"when": {"args": {"percent": {"gt": 10}}},
"approver": "sales",
"edits": True, # approvers may change the args; policy re-checks them
"reason": "Discounts over 10% need sales",
},
"no-deep-discounts": {
"deny": "apply_discount",
"when": {"args": {"percent": {"gt": 50}}},
"reason": "Never more than 50% off",
},
},
},
channels=[inbox],
clock=FakeClock("2026-11-25T10:00:00Z"),
)const gate = new Gate(
{
approvers: { sales: { channel: "sales-inbox", members: ["sales:ana"] } },
rules: {
discounts: { allow: "apply_discount" },
"big-discounts": {
ask: "apply_discount",
when: { args: { percent: { gt: 10 } } },
approver: "sales",
edits: true, // approvers may change the args; policy re-checks them
reason: "Discounts over 10% need sales",
},
"no-deep-discounts": {
deny: "apply_discount",
when: { args: { percent: { gt: 50 } } },
reason: "Never more than 50% off",
},
},
},
{ channels: [salesInbox], clock: new FakeClock("2026-11-25T10:00:00Z") },
);A 5% discount is allowed, 25% asks sales, and 60% matches all three rules: deny wins.
The Decision
gate.evaluate(action) runs the policy only (it loads limit usage and grants from the store, then calls the pure
engine) and returns a Decision value. It never raises for a denial.
| Field | Meaning |
|---|---|
effect | allow, ask or deny: the one field callers act on |
rules | Every rule that produced the winning effect (grant:<id> and invariant:<name> included) |
reason | The rule's reason, shown to approvers and returned to the model on refusal |
risk | The computed risk score |
requirements | For ask: one entry per approver that must answer, with its quorum |
digest | The digest of the policy that decided |
explain() | A multi-line trace: each rule, matched, not matched or indeterminate, and why |
gate.authorize(action) runs the full lifecycle and returns an Authorization: granted, status (allowed,
approved, denied, expired, cancelled, pending), args (what to execute with), decision, approval,
message (what to tell the model on refusal) and code. Gated functions and tools call it for you and raise
ActionDenied when not granted.
Grants: "always allow"
An approver can remember an approval as a grant. Later actions in the same scope (session, thread or actor)
that would ask become allow without asking anyone. match: "tool" covers any arguments for the tool;
match: "exact" only the same arguments.
second = await gate.authorize(discount(15, "call_2"), wait=False) # "pending" again
assert second.approval is not None
await gate.respond(
second.approval.id,
hitl.approve(by="sales:ana", remember={"scope": "thread", "match": "tool"}), # always allow in this thread
)
ran = await gate.authorize(discount(15, "call_2"), wait=False) # granted: runsconst second = await gate.authorize(discount(15, "call_2"), { wait: false }); // "pending" again
await gate.respond(
second.approval?.id as string,
approve({ by: "sales:ana", remember: { scope: "thread", match: "tool" } }), // always allow in this thread
);
const ran = await gate.authorize(discount(15, "call_2"), { wait: false }); // granted: runsGrants change ask into allow and nothing else. A deny rule still denies, and the grant does not reach other
threads:
nxt = await gate.authorize(discount(30, "call_3"), wait=False)
print(f"30% in thread t-42: {nxt.status} by {nxt.decision.rules[0][:6]}...")
# 30% in thread t-42: allowed by grant:...
deep = await gate.authorize(discount(60, "call_4"), wait=False)
print(f"60%: {deep.status}, {deep.message}") # 60%: denied, Never more than 50% off
elsewhere = await gate.authorize(discount(30, "call_5", "t-99"), wait=False)
print(f"30% in thread t-99: {elsewhere.status}") # 30% in thread t-99: pendingconst next = await gate.authorize(discount(30, "call_3"), { wait: false });
console.log(`30% in thread t-42: ${next.status} by ${next.decision.rules[0]?.slice(0, 6)}...`); // 30% in thread t-42: allowed by grant:...
const deep = await gate.authorize(discount(60, "call_4"), { wait: false });
console.log(`60%: ${deep.status}, ${deep.message}`); // 60%: denied, Never more than 50% off
const elsewhere = await gate.authorize(discount(30, "call_5", "t-99"), { wait: false });
console.log(`30% in thread t-99: ${elsewhere.status}`); // 30% in thread t-99: pendingGrants expire (8 hours by default, ttl in the grant spec) and can be listed and revoked: gate.grants.list(action),
gate.grants.revoke(grant_id).