# I had my coding agents write formal proofs, and they actually found bugs

> Source: <https://ryanmccue.ca/writing/coding-agents-formal-proofs/>
> Published: 2026-12-02 00:00:00+00:00

**Recommendation:** Run TLA+ model checking alongside unit tests on concurrency and retry paths, and deliberately break the model (e.g. downgrade SERIALIZABLE to REPEATABLE READ) to confirm it catches races your tests miss.
More in the playbook "Evals and testing for LLM apps": <https://wpnews.pro/playbooks/evals-testing>

# I had my coding agents write formal proofs, and they actually found bugs

After council reviews and live QA, I tried having coding agents write TLA+ and Lean 4 proofs. They caught real bugs tests missed, along with one big maintenance catch.

Between [multi-model code reviews](https://ryanmccue.ca/writing/council-reviews-it-first/) and [making agents run the app for hands-on QA](https://ryanmccue.ca/writing/dont-just-review-make-agents-run-it/), I already feel way more comfortable letting agents land changes on their own. Both catch a lot, but reviews only spot what a model thinks to look for, and tests only cover cases someone thought to write.

After watching [Lauren Tan's talk](https://www.youtube.com/watch?v=O_zcc6Xl12g) and digging through her [pstack](https://github.com/cursor/plugins/tree/main/pstack) skills, I wanted to try pushing that one step further with formal proofs. I adapted her formal verification and statechart skills into my [fleet repo](https://ryanmccue.ca/writing/manage-my-coding-agent-fleet-from-one-project/) and ran them across multiple codebases. Going in, I wasn't even sure they would turn up real issues.

Coding agents can now write formal proofs that catch real concurrency and edge-case bugs unit tests miss. This was always possible but before agents it would never be worth it to actually write these out by hand. But.. The catch is that separate models drift like code comments, so their long-term value comes down to upkeep.

## Why I tried proofs with my agents

The setup uses two tools depending on the job. TLA+ (with the TLC model checker) checks state machines, retries, and concurrency, while Lean 4 proves math properties that have to hold for any input size.

What made this actually usable for me is the `statechart` skill from `pstack`, which turns raw TLA+ traces and Lean proof trees into plain-English diagrams. I don't know TLA+ or Lean well enough to audit hundreds of lines of formal spec by hand, so seeing plain states and the shortest bug path lets me check whether the behavior makes sense.

Unfortunately the model checking and Lean builds chew through CPU and take quite a while to run. I was able to handle that though because every run goes to a [Boat](https://ryanmccue.ca/writing/moving-my-coding-agents-around/) cloud sandbox instead of my agent VMs. I fired off seven proofs in parallel across the two codebases: three on the personal web app and four on a backend service.

## They actually found real bugs

What surprised me most honestly was that the proofs actually caught real bugs on the first pass, plus blind spots in tests I already trusted:

- **Double-counted records after an account merge.** On the personal app, TLA+ found a three-step bug: upload an import, merge two accounts, then re-upload an older file for the merged-away account. Because nothing linked the old account to the surviving one, the app created a third account and counted every record twice. We reproduced it on real code, an agent shipped a fix to route old uploads to the surviving account, and rerunning the model proved the bug was gone.
- **An isolation downgrade tests missed.** On the same app's import workflow, the current code came back clean. To prove the model could fail, the agent ran a scratch break downgrading write transactions from`SERIALIZABLE` to`REPEATABLE READ` . The existing tests still passed, while TLA+ caught the stale-write and duplicate-row races right away.
- **Backend concurrency and auth races.** On a backend service, TLA+ caught a queue partition-handoff bug where hitting a timeout cancelled the old worker's write without waiting for it to finish, letting two workers write the same partition at once. A refresh-token model found a race where two concurrent CLI refreshes returned a`403` to the loser and signed an honest client out, while web sessions returned a retryable`409` .
- **Hidden assumptions in production and fuzz tests.** Another backend model showed an async pipeline was safe today only because of an undocumented rule matching the datastore's replica count to its write quorum. Adding a third replica without raising quorum would let a lagging replica permanently drop updates. And a Lean 4 cost-split proof came back clean, while pointing out that the existing fuzz test's "order never matters" check only passed because its generator never emitted duplicate IDs.

This stuff is crazy!!

## The catch

Even with real bugs caught in one day, I'm still not sure how great of a long-term strategy formal proofs are yet, even though running them surfaced three real problems right away.

**The runtimes really surprised me.** Some checks took a couple of hours, and an early import model blew past 160 million states before the agent stopped it and split the configs. On one hand, that shows how many interleavings TLC explores that unit tests never touch. On the other hand, slow runs mean you need cloud compute and tight bounds if you expect agents to rerun them on normal PRs.

**Models go stale just like code comments.** This is the biggest issue for me. TLA+ and Lean 4 check a hand-written model or translated copy, not your production code. We hit drift twice on the very same day, before the PRs even merged. Fixing the account-merge bug immediately made the personal app's sibling import model out of date, because one upload could now route two files to the same surviving account. On the backend service, an open PR flipped a quorum setting that invalidated the pipeline model's "production is safe" result hours after it ran. A passing stale proof is worse than an outdated comment because it looks like math.

**Vacuous checks and repo clutter.** A check you have never seen fail proves nothing. Two early models were bounded too tightly to catch deliberate scratch breaks, and agent-written docs tended to overclaim what was proved until review pushed back. Four backend proofs also totaled ~5,500 lines across nearly a hundred files. Leaving that buried in `.claude/skills/` hid real engineering code from teammates and search, so we moved the proofs into `tests/formal/` and kept the skills as short pointers with a sync commit.

## Where I think formal proofs actually fit

After seeing what they caught, I think formal proofs can be really useful and make releases safer, assuming you keep up with them. Recording a sync commit per model and having agents diff covered files when those paths change helps, though I haven't lived with that upkeep over months yet.

I could also see models getting good enough in the future that we need formal proofs less for everyday code. Even if that happens, I think there will always be a place for them on gnarly state machines, concurrency, auth flows, and money math. Giving agents a way to actually prove their changes is a really interesting step toward letting them work more autonomously.
