Coding Agents Write Formal Proofs, Find Real Bugs

Coding agents writing formal proofs to find software bugs

Coding agents can now write formal proofs, and a developer who ran them across two codebases says they caught real bugs that unit tests and code reviews missed. The proofs found a double-counting bug, a database isolation gap and several concurrency races within a day. They also exposed a maintenance problem: the proofs go stale as fast as code comments.

Writing formal proofs by hand was rarely worth the effort. Agents change that math. The developer took the formal verification and statechart skills from Lauren Tan’s pstack, adapted them, and fired off seven proofs at once, three against a personal web app and four against a backend service.

The setup: TLA+, Lean 4 and plain-English diagrams

Two tools split the work. TLA+ with the TLC model checker examines state machines, retries and concurrency. Lean 4 proves mathematical properties that must hold for any input size.

The statechart skill made the output usable. It converts raw TLA+ traces and Lean proof trees into diagrams in plain English. Someone who doesn’t read either language can still follow the states and see the briefest route to a failure. Model checking and Lean builds eat CPU and run slowly, so every run went to a cloud sandbox instead of the agents’ own virtual machines.

Bugs the proofs found

  • Double-counted records. TLA+ turned up a three-step failure in the personal app. A user imports a file, merges two accounts, and then uploads an older file that belonged to the account that was merged away. The app had no link from the old account to the survivor, so it spawned a third account and every record got counted twice. The team reproduced it on real code, an agent routed old uploads to the surviving account, and a rerun of the model confirmed the fix.
  • An isolation downgrade tests missed. The import workflow’s current code came back clean. To check the model could fail, the agent downgraded write transactions from SERIALIZABLE to REPEATABLE READ. Existing tests still passed. TLA+ flagged the stale-write and duplicate-row races at once.
  • Backend races. On the backend service, a queue handoff bug meant a timeout cancelled the previous worker’s write without waiting for it to finish, leaving two workers free to write to one partition. A refresh-token model showed two concurrent CLI refreshes could return a 403 to the loser and sign out an honest client, while web sessions got a retryable 409.
  • Hidden assumptions. One async pipeline was safe today only through an unwritten convention: the datastore’s replica count happened to line up with its write quorum. A third replica added without raising the quorum would allow a laggard to lose updates permanently. A Lean 4 proof of a cost-split calculation passed, but revealed that an existing fuzz test’s “order never matters” check held only because its generator never produced duplicate IDs.

The developer says the proofs found real problems on the first pass, within a single day.

The catch: runtime, drift and clutter

Some checks took a couple of hours. One early import model ran past 160 million states before the agent halted it and divided the configurations. The count shows how many event orderings TLC walks through that unit tests never reach. It also means ordinary pull requests can only rerun proofs if teams have cloud compute and tight bounds.

The bigger worry is drift. TLA+ and Lean check a hand-written or translated model, not the production code. Drift hit twice on the same day, before the pull requests even merged. The account-merge fix immediately outdated the neighboring import model, since a single upload could now send two files to one surviving account. On the backend, a pending pull request changed a quorum setting and voided the pipeline model’s “production is safe” verdict a few hours after it came back. A stale proof that still passes is worse than an old comment, since it looks like mathematics.

Two early models had bounds so tight that they missed deliberately planted breaks. A check nobody has watched fail proves nothing. Agent-written docs also overclaimed what was proved until review pushed back. Four backend proofs added about 5,500 lines across nearly a hundred files. Buried in a skills folder, that code was hidden from teammates and search, so the team moved it to tests/formal/ and left short pointers behind.

Where formal proofs fit next

The developer’s plan is to record a sync commit for each model and have agents compare the covered files whenever those paths change. That hasn’t been tested over months yet. Stronger models may cut the need for proofs on everyday code. Gnarly state machines, concurrency, auth flows and money math still look like places where they will earn their keep. For teams letting agents land changes alone, proofs are one more way for the agent to show its work.

Source: Ryan McCue

Written by

Saksham Bhargava

Saksham Bhargava runs Alteroid and has been writing about consumer technology for over five years, covering iPhone, Android, Windows, AI tools and gaming. With an MCA and six years of hands-on web development experience, he tests the features he writes about on his own devices and focuses on clear, practical guides that solve real problems. Away from the screen, he enjoys travelling to places most tourists skip.