Proof in production
Formal verification in the agentic era
A customer upgrades their plan. Stripe sends your server a webhook saying the payment succeeded, and your handler starts adding the extra seats. The database is slow for a moment, so the request takes eleven seconds. Stripe gives up after ten seconds and sends the same webhook again, so now two workers are handling the same payment.
Both workers check whether the event was already processed, and both see that it was not. Both add the seats, so the customer who paid for five seats gets ten. In the version of this bug that makes the news, someone pays twice.
The handler is twenty lines long and has a duplicate check, and its tests pass. A test sends one request at a time, while this bug only appears when two requests overlap in one particular order. You could write a test for that order if you thought of it, but the number of possible orders grows faster than anyone can list.
Formal verification checks all of those orders. Below I explain what it is and why agents make it practical for small teams, then walk through two flows from a real web app: accepting an invite and handling a payment webhook.
What formal verification is
A test checks one example. You choose an input and check what the code returns, for instance by inviting someone who is already a member and expecting an error. A good suite has a few hundred of these examples, and it tells you the code works for those cases. For every case in between, you assume.
Formal verification checks a rule instead of examples. You write down what must always be true in a precise language, and a program called a verifier checks that the code keeps it true for every possible input. For a protocol, it checks every order in which events can happen. When the verifier agrees, there is no input left to try. When it disagrees, it shows you the exact input or sequence of events that breaks the rule.
Each check has three parts. The specification states the rule. The implementation is the code meant to follow it. The checker, such as the Dafny verifier or the TLC model checker for TLA+, confirms that the code meets the specification or produces a counterexample.
Specifications look more ordinary than most people expect. A test might say that inviting alice@x.com twice fails. The specification says that inviting any address that already belongs to the team fails, for every team and every address. If you can write that sentence, you can have it checked.
Where it already runs
Some of the most heavily used software in the world has had its hardest parts checked this way for over a decade.
The best-known account comes from Amazon. In a 2014 paper, AWS engineers described using TLA+ on ten large systems, including S3 and DynamoDB, and reported that it found subtle bugs in every one. The most striking was in DynamoDB's replication: a bug that could lose data and only appeared after 35 steps of failures and recoveries. It had already passed review and testing. The same paper reports that engineers at every level learned TLA+ from scratch and got useful results within two to three weeks.
| Where | What is checked | Tool | What it found or proved |
|---|---|---|---|
| AWS: S3 and DynamoDB | Replication and fault tolerance | TLA+ | Data-loss bugs found before customers hit them, one of them 35 steps deep |
| AWS Cedar | The authorization engine behind Amazon Verified Permissions | Dafny, plus random tests against the Rust code | A forbid always overrides a permit; design flaws fixed before launch |
| Firefox | Curve25519 key exchange from the HACL* library | F*, compiled to C | Proven-correct cryptography that also ran faster than the code it replaced |
| seL4 | An operating-system microkernel | Isabelle/HOL | The implementation matches its specification |
Cedar is the closest to what most teams build. It decides whether a user may perform an action on a resource, which is the access check every SaaS app writes by hand. Amazon's team modeled it in Dafny and proved properties such as "a forbid always wins". They then compared the model with the production Rust code across roughly a hundred million random tests, and the proofs exposed flaws in the design itself.
None of these teams verified everything. Each picked the few pieces where a mistake would be serious and hard to notice, and checked those exhaustively. We copy that approach.
Why agents change the cost
Formal verification stayed rare because of the people it required. Writing a specification and steering a proof through a verifier took scarce specialists weeks, which few product teams could justify. Two changes have lowered that cost.
Review cannot keep up
Agents now write most of the code, faster than anyone can read it. A few agents can produce more in an afternoon than a team can review carefully in a week, so the old habit of having a colleague read every line no longer holds. The common response is to add reviewer agents. Lauren Tan (@poteto) argued against this this month, calling adversarial reviews mostly a waste of tokens that would be better spent on empirical and formal verification. A reviewer agent gives one model's opinion of another model's code.
Agents are good at proof work
Most proof work is a long loop of editing the code and rerunning the verifier until it stops complaining, and agents handle that loop well. The verifier also makes the author irrelevant. It checks the proof mechanically, so an agent cannot talk its way past it, and if the agent cut a corner, the proof fails and points to where.
This changes what the person does. They read the claim, a few sentences in plain language, and decide whether it describes what the product needs. The agent does the weeks of work in between, and the verifier checks it.
On the left, the person reviews every line and becomes the bottleneck. On the right, the person approves the claim and the verifier does the checking.
First flow: accepting an invite
Invites make a good first target because every team app has them. Someone invites a teammate by email, and the teammate clicks a link to join. The rule sounds simple: an invite can be accepted once, and not after it expires.
The rule breaks in small ways. Two open tabs or a link opened after expiry can add a stranger to a workspace or count a seat twice.
The diagram shows the path we follow. A person reads the claim closely and approves it, and the agent does the rest while the verifier checks each step.
1. Start from the statechart
The invite already has a statechart in our map, with four states (pending, accepted, expired and revoked) and the events that move between them. This step pays off even without a proof. Three booleans such as isAccepted, isExpired and isRevoked allow eight combinations, and four of them make no sense. One status field allows only the four real states.
2. Write the claim and approve it
Next, the claim is written in plain language: "An invite is accepted at most once, and never at or after its expiry time. Accepting a revoked or expired invite changes nothing." A person approves this sentence before any proof work starts. It is the one part of the process that needs careful human reading.
3. Turn the claim into a specification
In Dafny, the claim becomes an invariant on the invite type and a contract on the accept function. A simplified version looks like this:
datatype Status = Pending | Accepted | Expired | Revoked
datatype Invite = Invite(status: Status, expiresAt: int, acceptedCount: nat)
// Accepted exactly when it was accepted once; never more than once.
predicate Valid(i: Invite) {
i.acceptedCount <= 1 && (i.status == Accepted <==> i.acceptedCount == 1)
}
method Accept(i: Invite, now: int) returns (r: Invite, ok: bool)
requires Valid(i)
ensures Valid(r)
ensures ok ==> i.status == Pending && now < i.expiresAt && r.status == Accepted
ensures !ok ==> r == i
{
if i.status == Pending && now < i.expiresAt {
r, ok := i.(status := Accepted, acceptedCount := 1), true;
} else {
r, ok := i, false;
}
}
Each ensures line is a promise the verifier must confirm for every invite and every time. A successful accept means the invite was pending and unexpired. A failed accept changes nothing. Either way the invite stays valid, so a second acceptance cannot happen.
4. Check that the specification means what it says
A specification can go wrong in two ways: it can be so strict that no code could satisfy it, or so loose that any code would. Before the real implementation, the agent proves a few sanity lemmas. For example, a pending invite can be accepted, while an accepted one cannot be accepted again. If the specification forbade the normal case by mistake, this step would catch it.
5. Let the agent prove it
The agent writes the implementation and iterates with the verifier until it passes, with no escape hatches such as assume or axioms. When the verifier refuses, it shows a counterexample. Usually that counterexample is a real bug in the code or a gap in the claim, and only occasionally a proof that is hard to write.
6. Wire it in
Dafny compiles the verified code into the app's own language, and the generated module becomes the invite component's only contract. A thin wrapper converts the app's types and checks at runtime anything the proof assumed, since compiled code does not recheck its preconditions. The map marks the wrapper as the component's entry point, so a handler that updates the invite status directly fails the build.
7. Keep it true
CI reruns the verifier on every push and rejects escape hatches. It also fails if anyone edits the generated code by hand. Six months later, when an agent adds a "resend invite" feature, its push only goes through if the proof still passes.
Second flow: the webhook that arrives twice
The invite proof covers one decision: whether a given invite can be accepted at a given time. It says nothing about two requests making that decision at the same moment against the same database row. That second problem is about order, and it is where the double-charge story from the opening comes from.
Order problems need a different tool. TLA+ describes a system as steps that can happen in any order, and its model checker, TLC, tries every possible interleaving of those steps. You write the rules, and the checker finds the failing scenario for you.
The webhook handler checks whether the event was processed, applies it, and marks it as processed. The core of the TLA+ model is short:
Check(w) == pc[w] = "check"
/\ pc' = [pc EXCEPT ![w] = IF evt \in seen THEN "done" ELSE "apply"]
/\ UNCHANGED <<seen, applied>>
Apply(w) == pc[w] = "apply"
/\ applied' = applied + 1
/\ seen' = seen \cup {evt}
/\ pc' = [pc EXCEPT ![w] = "done"]
AppliedAtMostOnce == applied <= 1
With two workers and one redelivered event, TLC fails within a second. It prints the sequence: worker one checks and finds nothing, worker two checks and finds nothing, and then both apply, leaving applied at 2. Nobody had to imagine the bug.
A test runs one lane after the other and passes. The bug needs the lanes to overlap in this order, and the model checker tries every order.
The fix
The fix makes the check and the claim one step. The handler inserts the event id into a processed_events table with a unique constraint, in the same transaction as the effect, and applies the effect only if the insert succeeded. In the model, Check and the first half of Apply become one action.
TLC then rechecks every interleaving with two workers, three deliveries and a crash at any point, and the claim holds in all of them. Those bounds are written next to the claim, because a model check only covers the sizes it was given. The model also has one action per step of the flow in our map, so a later change to the handler shows up in the map and triggers a recheck.
What to prove
Proof costs enough that it belongs only where three conditions hold:
- Failure is unacceptable: money, access, deleting someone's data, or a protocol that must never apply an action twice.
- The piece can be isolated, such as a decision or a state machine with clear inputs and outputs.
- The rules are stable. A claim that will change next week can wait.
A page layout or the glue around a third-party API changes often and fails visibly, so tests cover those well.
A costly piece that is still tangled, like a checkout handler doing six things, needs untangling first. Pull the decision into its own module, then prove the decision.
In a typical web app this leaves a handful of components, a few percent of the code. Usually that means the access gate and the billing and webhook logic. Everything else stays tested.
Proofs also push the design in a good direction. To prove the access decision, you have to isolate it in one small module with one entry point, which is the design most teams want anyway.
What a proof cannot tell you
A good proof report states three limits next to every claim.
The specification can be wrong
A proof shows that the code does what the specification says. It cannot show that the specification matches what the product needs. If the claim forgot that admins may reopen a revoked invite, the proof confirms the wrong rule. A person approves the plain-language claim first for this reason, and the sanity lemmas exist to catch the same problem early.
Every proof rests on assumptions
The invite proof assumes the clock gives the real time and the database returns the invite that was requested. The webhook model assumes the database enforces the unique constraint. These assumptions are trusted, and the report lists them as trusted.
Model checks have bounds
TLC checked every interleaving of two workers and three deliveries. Most ordering bugs appear with two or three actors, even when they take many steps, so small bounds catch a lot. The bound is still written next to the claim. Where it is not enough, the claim is proved without bounds in a proof assistant, which costs more.
Test suites have the same kinds of limits, but they rarely write them down.
Where this leaves us
Verifiers have worked for decades. What kept them out of ordinary products was the cost of the specialists who could use them. Agents lower that cost, because they can run the loop of editing and rechecking for as long as it takes, and the verifier checks their output whoever wrote it.
People keep the part they are best at, which is deciding what must always be true. For the few percent of an app where a mistake costs money or someone's data, a proven rule that CI rechecks on every push is worth more than another round of review.
TL;DR
- Tests check examples. A verifier checks a rule for every input or every order of events, and shows a counterexample when the rule fails.
- Large systems already rely on it: AWS has used TLA+ since 2011, and Amazon proved its Cedar authorization engine in Dafny.
- Dafny proves decisions and state machines. TLA+ checks orderings and races.
- A person approves the claim in plain language. The agent writes the proof, and the verifier checks it.
- A statechart turns into a specification almost directly, so it is the natural starting point.
- Prove the few percent of code where failure is unacceptable and the logic is isolated and stable. Test the rest.
- The compiled proof is the component's only contract, and CI rechecks it on every push.
- Every claim lists its assumptions and the bounds it was checked within.
Sources
- Chris Newcombe et al., Use of Formal Methods at Amazon Web Services, September 2014
- Mike Hicks, How we built Cedar with automated reasoning and differential testing, Amazon Science, May 2023
- Mozilla Security Blog, Verified cryptography for Firefox 57, September 2017
- About seL4, seL4 Foundation
- Lauren Tan, post on adversarial reviews and verification, X, November 3, 2026