← All posts

Post

Reviewable AI: Rules Diffs With Scores Beat Opaque Weights

For small decisions, keep the logic in a rules file where every change is a diff with a before and after score. A runnable demo with a review card and a gate.

19 min read

Copy this article

Clipboard access is unavailable. Select the Markdown and copy it, then paste it into your chat.

Quick answer

For a small, closed decision, such as routing a support message to one of five queues, keep the logic in a plain rules file and treat every change as a reviewable diff that arrives with a score. The reviewer sees the lines that changed, the accuracy before and after, which examples flipped and which classes got worse. A pre-commit gate can then refuse a change that regresses a class you’ve marked as protected, even when overall accuracy goes up.

Weights can’t give you that. You can still score a model, but you can’t read a diff of its weights, and you can’t point at the line responsible for one wrong answer. Prompts are readable, but their score is noisy and a flipped example doesn’t name a cause. Rules are exact: the same input gives the same answer, and each answer names the rule that produced it.

This post comes with a small runnable demo (Python, no packages, no network). Everything in it is synthetic: the messages are generated from a seed and the rules are written by hand. It shows what the workflow feels like, not how rules perform on real data. Rules also stop being the right tool once the task is open language, and we say where that line sits below.

Original diagram of three columns tracing one wrong answer back to its cause. For model weights, the wrong answer goes into a sealed box with a question mark, so the cause is unknown. For a prompt, the diff is readable but the score is a range from run to run, so the cause is unclear. For a rules file, the example message about a password change email that the user did not request points at the rule access-login that fired, the score is exact at 332 of 400 or 0.830 before and 358 of 400 or 0.895 after, and the cause is named.

What a rules file gives you that weights and prompts don’t

Reviewability comes down to three questions you want to ask about any change to a decision system.

  1. What exactly changed?
  2. Did it get better or worse, and by how much?
  3. If one answer is now wrong, which part of the system is responsible?

Here is how the three kinds of decision logic answer them.

Model weights. You can answer the second question with an eval, and you should. The first and third are the problem. A fine-tuned model’s change is millions of numbers, and no reviewer reads those. When one example flips, nothing names a line to blame.

A prompt. The diff is readable, which is a real advantage. The score is harder: a model’s output varies from run to run, so a before and after number has a range around it. We covered how to handle that in the 30-case eval set post. Attribution is also loose. If you reword three sentences and two examples flip, the prompt can’t tell you which sentence did it.

A rules file. The diff is readable and small. The score is exact, because the same message always gets the same label, so there’s no range to argue about. And every prediction carries the name of the rule that fired, so a flipped example points at a line.

None of this makes rules better at everything. It makes them better at being reviewed. If the decision is small enough that rules can express it, you get a review process that’s hard to match: a diff, a score and a cause, all in one pull request.

One more point worth keeping straight. The score card is not exclusive to rules. You can build the same gate for a prompt, and the prompt regression gate post does exactly that. What rules add is the exact score and the named cause.

The demo: routing support messages

The demo routes a short support message to one of five labels: billing, bug, access, security and other. It is the kind of decision where a wrong answer has a visible cost and the categories don’t change often.

Four pieces make it work:

  • rules.py holds the decision logic.
  • data.py generates 400 fictional labelled messages from a fixed seed. The class counts are 125 billing, 110 bug, 79 access, 40 security and 46 other. Security is rare on purpose, because the dangerous class is often the rare one.
  • review.py builds the review card for any pair of rules files.
  • gate.py is the pre-commit style gate. It decides whether a change may be committed.

We ran all of it on Python 3.13.16 and git 2.43.0 (checked 2026-11-13). The README says Python 3.10 or newer, which we did not test on older versions.

The labels come from the generator, so they are right by construction, which real work never gives you. The post on building a benchmark from your own logs covers where a real labelled set comes from.

The rules file

Here is the baseline rules.py. It’s an ordered list, and the first rule that matches wins. We ran it as written.

DEFAULT_LABEL = "other"

RULES = [
    ("sec-hacked", "security", r"\b(hacked|phishing|unauthori[sz]ed)\b"),
    ("access-login", "access", r"\b(password|log ?in|sign in|locked|two-factor|2fa|workspace|admin)\b"),
    ("billing-words", "billing", r"\b(invoice|refund|charge[ds]?|card|subscription|price)\b"),
    ("bug-words", "bug", r"\b(crash\w*|error|blank screen|freez\w*)\b"),
]

Each rule has a name, a label and a pattern. The name matters more than it looks. When the scorer classifies a message it records which rule fired, and the review card prints that name next to every flipped example.

On the 400 review messages this baseline gets 332 right, or 0.830. Look at the per-class numbers and the weakness is obvious: security recall is 14 of 40 (0.350) and bug recall is 71 of 110 (0.645). A message like “I got a password change email that I didn’t request” hits the password word in the access rule and gets routed to access. A message like “The refund button does nothing, the page just spins” hits refund in the billing rule and lands in the billing queue.

Those two failures have different weights. A bug filed under billing is an annoyance. A security report filed under access might sit unseen. That difference is why the gate has a notion of a protected class.

The review card

review.py takes the old rules file and the proposed one and prints one card. Our first proposal widens the security wording: it adds phrases like didn't request, don't recognize, logged into, unknown device and never visited to the security rule. This is the real card, from the run we did in a fresh unzip of the download:

SCORE on 400 synthetic review messages
  accuracy   332/400 = 0.830  ->  358/400 = 0.895   (+0.065)

  recall by class (right label / messages of that class)
  billing   122/125 = 0.976  ->  122/125 = 0.976  (+0.000)
  bug        71/110 = 0.645  ->   71/110 = 0.645  (+0.000)
  access      79/79 = 1.000  ->    79/79 = 1.000  (+0.000)
  security    14/40 = 0.350  ->    40/40 = 1.000  (+0.650)   improved
  other       46/46 = 1.000  ->    46/46 = 1.000  (+0.000)

FLIPPED  26 fixed, 0 broken, 0 wrong either way
  fixed:
    [m011] true=security  other ((default)) -> security (sec-hacked)
           "hi, unknown device signed in from another country"
    [m038] true=security  access (access-login) -> security (sec-hacked)
           "hi, I got a password change email that I didn't request"

CLASSES THAT REGRESSED  none

(We trimmed the list of flipped examples to two here. The full card prints six and says how many more repeat.) The card is organised the way a reviewer reads. The diff comes first, then the score, then the flipped examples with the rule that handled each one before and after, then the regressions.

Three details are worth copying.

Flips are listed in both directions. A change that fixes 26 examples and breaks 0 looks very different from one that fixes 35 and breaks 16, even if both raise accuracy. The card shows fixed, broken and changed but still wrong as separate counts.

Regressions are per class, by count. Overall accuracy hides the damage when the broken class is small. Recall by class, with raw counts next to the ratio, makes a drop of 16 messages in a 40-message class impossible to miss.

Duplicates are collapsed. The card shows each phrasing once and says how many more repeat, so the reviewer reads six examples instead of fifty.

This change is a clean one: security recall goes from 14 of 40 to 40 of 40 and nothing else moves. We should be honest about why it looks that tidy. The generator and the rules were written by the same hand, so the new wording happens to match every security template the generator has. Real messages would not be this obliging, and the real number would be lower.

The gate that refuses a protected regression

A card is only useful if somebody reads it. A gate makes the most important rule automatic: a protected class must never get worse.

The policy lives in a small file, gate.json:

{
  "rules_file": "rules.py",
  "protected": ["security"],
  "max_accuracy_drop": 0.0
}

The decision itself is short. This is the core of decide() in gate.py, which we ran as part of the demo:

for lab in cfg["protected"]:
    bh, bn = before["recall"][lab]
    ah, an = after["recall"][lab]
    if ah < bh:
        reasons.append(f"protected class '{lab}' got worse: {bh}/{bn} -> {ah}/{an}")
drop = before["correct"] / before["n"] - after["correct"] / after["n"]
if drop > cfg["max_accuracy_drop"] + 1e-12:
    reasons.append(f"overall accuracy dropped by {drop:.3f}")

The protected check compares counts, not percentages, and it allows no slack: one more wrong security message than before is a refusal. Overall accuracy gets its own limit. Here the limit is zero, so any drop is refused too. Loosen it if you like, but pick the number before you see the change.

The gate is installed as a git pre-commit hook. The hook file is two lines, and a hook that exits with a non-zero status aborts the commit before it’s created (see the githooks documentation, checked 2026-11-13).

#!/bin/sh
exec python3 gate.py --staged

Now the proposal that should be refused. Someone looks at the 39 bug messages landing in the wrong queue and makes two edits. They widen the bug wording and move it to the top, and they move the security rule to the bottom “because security is rare”. It is a plausible-looking change. Here is what the hook printed when we tried to commit it:

SCORE on 400 synthetic review messages
  accuracy   358/400 = 0.895  ->  377/400 = 0.943   (+0.047)

  recall by class (right label / messages of that class)
  billing   122/125 = 0.976  ->  122/125 = 0.976  (+0.000)
  bug        71/110 = 0.645  ->  106/110 = 0.964  (+0.318)   improved
  access      79/79 = 1.000  ->    79/79 = 1.000  (+0.000)
  security    40/40 = 1.000  ->    24/40 = 0.600  (-0.400)   REGRESSED
  other       46/46 = 1.000  ->    46/46 = 1.000  (+0.000)

FLIPPED  35 fixed, 16 broken, 0 wrong either way
  broken:
    [m038] true=security  security (sec-hacked) -> access (access-login)
           "hi, I got a password change email that I didn't request"
    [m097] true=security  security (sec-hacked) -> billing (billing-words)
           "hey, There is a charge I don't recognize and I think my account was hacked thanks"

CLASSES THAT REGRESSED
  security: 40/40 -> 24/40  (-16 messages)

GATE: REFUSED
  - protected class 'security' got worse: 40/40 -> 24/40
  Fix the rule, or change gate.json in its own reviewed commit.
>>> commit blocked by the hook; restoring rules.py

A plain accuracy check would have approved it, since accuracy rose by 0.047, from 0.895 to 0.943. The review card exposes the trade: the bug class gained 35 messages while 16 security messages slid into the access and billing queues. A reviewer might still choose to accept that trade, but the gate makes sure someone chooses it on purpose, in a visible commit that changes gate.json, rather than by accident.

The card also shows the cause. The broken lines name access-login and billing-words as the rules that stole the messages, so the fix is obvious: keep the security rule above them.

The third proposal does that. It moves only the widened bug rule above billing and leaves security on top:

GATE: ok  score 393/400 (was 358/400); security recall 40/40 (was 40/40)
>>> committed: Bug rule above billing

The gate also writes that same score line into the commit message, which is where the next section picks up.

One rule’s history, with a revert

A reviewable change should stay reviewable after it merges. The demo’s run_demo.sh puts the score into each commit message, so git log carries the history. This is the real output:

1104780 baseline rules
532d380 Widen security wording | score 358/400 (was 332/400); security recall 40/40 (was 14/40)
985ef19 Bug rule above billing | score 393/400 (was 358/400); security recall 40/40 (was 40/40)

Commit messages can drift from the truth, though, so history.py doesn’t read them. It re-scores rules.py as it stood at every commit that touched it. We then ran git revert on the last change and ran history.py again:

commit    accuracy    bug  security  subject
1104780      0.830  0.645     0.350  baseline rules
532d380      0.895  0.645     1.000  Widen security wording
985ef19      0.983  0.964     1.000  Bug rule above billing
58078db      0.895  0.645     1.000  Revert "Bug rule above billing"

The revert commit scores 0.895, exactly what the commit before the change scored. That is the kind of statement you can’t make with weights or a prompt without a fresh set of runs: undoing the diff put the behaviour back, and the table proves it.

When rules beat a model, and when they don’t

Here’s a checklist for deciding. It’s general guidance, not a result from the demo.

Rules are a good fit when:

  • The decision is small and closed: a handful of labels or outcomes, not free text in and free text out.
  • Someone will ask “why did it do that?” and expects a specific answer.
  • A wrong answer in one class costs far more than in another, so you want a per-class gate.
  • The logic changes in small steps that a person can read, such as adding a keyword, reordering two rules or changing a threshold.
  • You want a deterministic score, so that a before and after number needs no error bar.

A model with a good eval is the better call when:

  • The input is open language and the rules would need hundreds of patterns that interact. Our demo has four rules. At four hundred, nobody can review the diff, and the order dependence becomes a trap.
  • Meaning matters more than keywords. “I never asked for this charge” and “I’d like to dispute a payment” share no keywords but may share a label.
  • The categories shift faster than people can edit rules.

If you do use a model, the git-and-score workflow is still worth keeping. The git loop classifier post takes the same idea further: a loop proposes one rule change at a time, scores it, and commits only when the score improves, while a sealed test set shows how much of the gain was overfitting.

Run it yourself

Download the reviewable rules demo source package and unzip it into a new, empty folder. You need Python and git. There are no packages to install, no network calls and no API keys.

On macOS, Linux or WSL:

bash run_demo.sh
python3 tests.py
python3 review.py rules.py proposals/p1_security_wording.py

The first command builds a throwaway git repo in a demo_repo folder, installs the hook, and walks the three proposals through it. The second runs five tests. The third prints a single review card without involving git. We ran all three from a fresh unzip on Linux, and the tests printed Ran 5 tests ... OK. We have not tested native Windows, and the hook calls python3, which may be named python there.

To try the workflow on your own decision, change three things:

  • Replace data.py with your own labelled examples, loaded from a file.
  • Replace rules.py with your own rules, keeping the RULES list shape and the DEFAULT_LABEL.
  • Set protected in gate.json to the classes where a miss is expensive.

The scorer executes the rules file as Python, so only run it on files you own.

Limits of this demo

Read the numbers with these in mind.

  • The data is synthetic. The messages come from templates we wrote, and the rules were written by the same hand that wrote the templates. The scores are therefore higher and tidier than any real queue would give you. Treat the workflow as the lesson and the numbers as decoration.
  • The review set is also the tuning set. Every change here is judged on the same 400 messages, which invites overfitting. A sealed test set that’s opened once is the fix, and the git loop post shows what the gap looks like.
  • The hook is local. A developer can skip it with git commit --no-verify. Run the same gate.py --worktree in CI so the gate can’t be skipped. The regression gate post shows the CI side of the same idea.
  • The protected class is a human choice. The gate only protects what you name. Someone has to decide that security matters more than bug routing, and write it down in gate.json.
  • Rules are for small decisions. Five labels and four rules is about where review stays comfortable. The approach does not stretch to open language tasks, and we didn’t test where exactly it stops working.
  • No model was involved. Nothing here measures a model, a prompt or a vendor.

FAQ

Why are rules easier to review than model weights?

A rules change is a short text diff, and the same input always gives the same output, so the score before and after is exact. Each prediction also records which rule fired, so a flipped example points to a specific line. Weights offer none of those three things.

Can I use a score card with a prompt instead of rules?

Yes. The prompt regression gate post builds the same kind of check for a prompt. The difference is that a prompt’s output varies from run to run, so its score has a range, and a flipped example doesn’t name a single cause.

What is a protected class in a regression gate?

It is a class you named in advance as one that must not get worse. In the demo it’s security, and the gate refuses any change that leaves even one more security message misrouted, even when overall accuracy rises. In the demo run, accuracy rose from 0.895 to 0.943 while security recall fell from 40 of 40 to 24 of 40, and the gate refused it.

Can someone get around a pre-commit gate?

Yes. A pre-commit hook can be bypassed with git commit --no-verify, per the githooks documentation (checked 2026-11-13). Run the same gate in CI as well so a skipped hook is still caught.

When should I use a model instead of rules?

Use a model with a good eval when the input is open language, when meaning matters more than keywords, or when the categories change faster than people can edit rules. Rules fit small, closed decisions where someone will want a specific answer to “why did it do that?”.

Are the demo’s scores realistic?

No. The messages are synthetic and the rules were written alongside the generator, so the scores are tidier than real data would give. The demo shows the workflow, and real results on your own labelled set will differ.

Where to go next

  • #rules
  • #evaluation
  • #git
  • #python