Dafny Verification for AI-Written Code: What Held Up

Developer reviewing proof output, showing Dafny verification of AI-written code

A developer who built an entire multi-platform app with AI coding agents says Dafny verification gave them confidence that unit tests could not. The catch: it only worked on a few small pieces of the codebase.

The app is NotiBuddy, which runs on iPhone, iPad and Apple Watch and talks to a Cloudflare Workers relay. A local CLI and MCP client complete the stack. Only Apple platforms are supported so far. Product and test code together total about 440,000 lines in more than a thousand commits, and AI agents produced every line over a few weeks.

The bottleneck moved to trust

With agents, writing code gets cheap almost at once. Trusting it doesn’t. An agent can produce a clean state machine and passing tests in minutes while hiding a subtle bug, such as a late reply that reopens an expired request or a retry that collides with a cancellation.

In December 2025, Martin Kleppmann forecast that AI would make formal verification a mainstream practice. The NotiBuddy developer tested that idea on real policy logic. Where formal models fit, they gave a level of assurance that unit tests alone couldn’t. Across most of the codebase, they didn’t apply or got in the way.

Where Dafny verification gets cheaper

Hand-written proofs are slow, which is why Dafny verification rarely suits a small team. Hunting for the one assertion that convinces an SMT solver is rarely worth a small team’s afternoon. An agent can grind through that trial and error, revising a proof and rerunning dafny verify until every obligation passes, while the developer checks that the specification itself is untouched.

When verification fails, Dafny exits with code 4 and points at the obligation it couldn’t establish. That line isn’t always where the bug lives, and counterexample extraction is opt-in and may be invalid. Still, knowing which obligation failed usually keeps an agent moving.

The bigger gain was less review. The developer proved small, side-effect-free models of the relay’s admission, replay and billing rules, and of how Custom Native Screens are admitted, a case where the relay checks stripped structural templates without reading plaintext. The Workers run TypeScript generated directly from those verified models, and CI checks that both the proofs pass and the generated output stays in sync.

Where agents cut corners

Agents want a green exit code by the shortest route, and Dafny has escape hatches. An assume statement with the axiom attribute lets a lemma pass by treating a condition as true without proof. A contract that merely repeats the function body pins the code but not the intent. Agents may also strengthen a requires clause to rule out the awkward case.

Catching these takes more than searching for assume or the verify-false attribute. CI should inspect the exact Dafny command line so a flag such as –no-verify can’t creep in, and confirm which declarations are in scope. One quirk in Dafny 4.11: dafny audit can report findings and still exit cleanly, so scripts must read its output.

The developer reviews Dafny diffs backward. Datatypes and requires and ensures changes come first, proof bodies last. A strengthened precondition or weakened postcondition made without being asked means the diff is rejected.

A mutation test that surprised

The example model covers an approval gate with a deadline, signed replies and optional biometric checks. Two lemmas hold across every finite sequence of events: a finished request can never flip its outcome, and approval requires verified evidence that satisfies the policy.

Breaking the logic on purpose tests the spec. Replacing the authorization check with a user-accepted flag alone makes Dafny 4.11.0 report 3 verified and 2 errors. But changing the deadline comparison from greater-than to greater-or-equal still verifies cleanly, at 4 verified and 0 errors. A reply landing on the exact millisecond of the deadline would then expire the request, and neither lemma requires approval. A passing proof can leave a product decision open, and Dafny verification can’t tell you which decision that is.

What the proofs never saw

Models only see the values they’re given. If a client sends Unix seconds where milliseconds are expected, or an edge router lets an unauthenticated request write a Durable Object before policy runs, verification still reports zero errors. Ordinary engineering covered that gap: strict runtime decoders, storage guards and golden tests pinning routing decisions.

Several of the hardest release bugs sat outside the models. During TestFlight, iOS froze the app partway through a transaction while shared SQLite locks were still held. A sluggish inbox turned out to be a foreground poll waiting more than half a minute on notificationSettings. Moving to push-invalidated stores and a WebSocket delta stream let the developer delete the polling loop and reduce idle CPU use and the delay between a tap and the queue updating by over 90%. Cloudflare bundle size, cold starts and billing needed local latency benchmarks plus a CI suite that flags rising costs.

What stays on the human’s desk

Dafny verification works best as one layer among several.

The developer still reviews every specification change, tests the runtime code outside the models, and requires a person to sign off on operational changes that can’t be undone. Daily use of NotiBuddy itself, for build alerts and for approving lengthy agent jobs, exposed rough edges early. Dafny cut the amount of sensitive policy code to trace manually, so releasing agent-written software felt less risky.

Source: NotiBuddy

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.