2025-10-15 · Authensor

First-Match-Wins

First-match-wins is a policy evaluation model in which rules are processed sequentially and the first rule whose conditions match the action being evaluated determines the verdict, with all subsequent rules ignored.

In Detail

When a policy engine receives an action to evaluate, it must determine which rule applies. In a first-match-wins model, the engine begins at the top of the rule list and checks each rule in order. The moment it finds a rule whose conditions match the action, it returns that rule's effect (allow, deny, or require_approval) and stops processing. No further rules are examined.

This model has several important properties:

Determinism

For any given action and any given rule set, the outcome is always the same. There is no ambiguity about which rule applies — it is the first one in the list that matches. This makes policy behavior predictable and debuggable.

Rule Order Significance

The order of rules is semantically meaningful. Reordering rules can change the outcome for the same action. This is both a strength and a responsibility. The administrator must arrange rules so that more specific rules appear before more general rules.

Consider this example:

Rule 1: file_write to ./src/generated/** → deny
Rule 2: file_write to ./src/**          → allow
Rule 3: deny all

An action writing to ./src/generated/output.ts matches both Rule 1 and Rule 2. Under first-match-wins, Rule 1 is checked first, matches, and returns deny. Rule 2 is never reached.

If the rules were reordered:

Rule 1: file_write to ./src/**          → allow
Rule 2: file_write to ./src/generated/** → deny
Rule 3: deny all

The same action now matches Rule 1 first and is allowed. Rule 2, which was intended to deny writes to generated files, is never reached. The policy has a logical error caused by incorrect ordering.

Performance

First-match-wins is efficient. In the best case (the first rule matches), evaluation requires checking a single rule. In the worst case (no rule matches), all rules are checked. For typical policy sizes (tens to hundreds of rules), evaluation is extremely fast.

Comparison with Other Models

First-match-wins is used by iptables (Linux firewall), AWS Security Groups, Nginx location blocks, and many other systems where predictable, sequential rule evaluation is valued.

Examples

Related Concepts

In SafeClaw

SafeClaw, by Authensor, uses first-match-wins as its policy evaluation model. Policy rules are defined as an ordered list, and SafeClaw evaluates each action against the list sequentially. The first rule whose conditions match the action determines the verdict.

If no rule matches, SafeClaw's deny-by-default architecture applies: the action is denied. This means the rule list does not need an explicit "deny everything" rule at the end — the absence of a matching rule is equivalent to a denial.

SafeClaw's setup wizard and browser dashboard help administrators construct and order their rules correctly. Simulation mode is available for testing rule ordering before switching to enforcement, allowing administrators to observe how first-match-wins processes their specific agent workflows. SafeClaw evaluates policies locally with sub-millisecond latency across all four action types (file_write, file_read, shell_exec, network).

SafeClaw is 100% open source (MIT license), written in TypeScript strict mode with 446 tests and zero third-party dependencies. It works with Claude, OpenAI, and LangChain agents. Install with npx @authensor/safeclaw — free tier available with 7-day renewable keys and no credit card required.

Try SafeClaw

Action-level gating for AI agents. Set it up in your browser in 60 seconds.

$ npx @authensor/safeclaw