← All projects

Case study

Doctrine

Rules an AI coding agent can’t loosen

Role
Author and only approver
Stack
Python
Status
Open source, in daily use
Since
2026
Code
github.com/OmarAbdo/doctrine ↗

The problem

An AI coding agent can write a lot of code very fast, and that’s the problem. In an earlier project of mine, an agent shipped 95 commits in two days while the one number the project existed for, applications sent per day, fell from 18 to 0. The project had written rules, a CLAUDE.md, decision records and 394 tests. They were advice, and under pressure the agent treated them as advice.

So I built Doctrine around one idea. Rules are enforced by machinery the agent can’t change, and instructions only explain where things are.

How it works

Doctrine installs as an admin-owned program on the machine, outside any project the agent can edit. It hooks into every Claude Code session.

  • Before every command and every file edit, a hook judges the action. A write to a protected path, a push to main, an attempt to skip a hook or a change outside the current work item is refused, with the reason and what to do instead.
  • After every edit, the file is formatted and linted on the spot, and problems go straight back to the agent.
  • Every change is a work item. A feature or a fix starts with a failing test that is recorded as evidence, and the item can only close with a red run, a passing check and nothing left unchecked.
  • In CI, gates check readability (cyclomatic complexity 7, nesting 2), tests first, diff coverage, mutation testing on domain code, pull request size (400 lines, 15 files) and the eval sets of every AI prompt.
  • On GitHub, main is protected. A bot account does all of the agent’s work, so only my approval of a pull request can merge it or loosen a rule.

Decisions and what they cost

Approvals only through pull requests

Nothing in a chat loosens a rule. If the agent thinks a refusal is wrong, it stops and tells me, and the fix goes through a reviewed pull request to Doctrine itself. That’s slower on purpose, and it closes the path where an agent argues its way past a gate.

One measured outcome per project

Every project defines one number it exists to move, with a baseline and a stop value. When the number falls below the stop, only restore work may change code. Commits measure activity, and this measures the result. Defining the number takes a conversation before any code, and that’s the point.

Evals for every AI prompt

A prompt that ships needs an eval set. Runs are recorded per version, CI gates a changed prompt on them, and LLM judges are scored against labels I give by hand. It costs labelling time. It replaces “fix the last bad output with one more instruction”, which was exactly how the earlier project drifted.

Python first

The rule table is shared, and each language gets a profile that turns it into tool settings. Only the Python profile exists so far. A TypeScript profile comes later, so the Python version is finished properly first.

Proof

  • doctrine selftest runs every known bad sample and every bypass attempt against the rules. It caught 52 of 52 on 2026-10-22, and that share is Doctrine’s own outcome metric.
  • Doctrine has been built under its own rules since its fourth pull request. It refused its author’s shortcuts, and every refusal that turned out to be wrong was fixed through a reviewed item.
  • It updates itself from the protected main branch every 30 minutes.

What’s next

The TypeScript profile. This website uses the same limits through its own lint rules in the meantime.