lean-verify Turns Claude Code Into a Bug Hunter That Checks Every Input
A small MIT skill points the Lean 4 theorem prover at one function at a time and asks a simple question: is there any input where this breaks?
Every test suite has the same blind spot. It checks the inputs somebody thought to try. The bugs that survive for years live in the inputs nobody thought of: the value that overflows, the shift by exactly the word size, the list that is one element too long.
Here is a real one. In Clojure, a programming language whose core data structures people have used since its 1.0 release, a blog post published November 24 shows that calling reduce on a primitive vector after dropping its first element never returns once the vector holds 65,505 or more items. One element fewer and it works. The cause is a bit mask that has sat in the code since 2010.
Nobody wrote a test with 65,505 elements. A theorem prover did not need one. It checked the chunk arithmetic for every possible index at once and handed back one that fails.
That is the idea behind lean-verify, a Claude Code skill released as v1.0.0 on November 23. It does not ask you to prove your program correct, which is slow, expensive and mostly impractical. It asks a much smaller question about one function, and it is built for your coding agent to run.
What a theorem prover is doing here
Lean 4 is a programming language that can also check mathematical proofs. You state a claim, and Lean either confirms it holds or tells you it doesn't. Researchers use it to check proofs of big theorems. lean-verify uses it for something humbler.
The trick is a Lean tool called bv_decide. "BV" stands for bit vector, which is just a fixed-width number like a 32-bit or 64-bit integer, the kind your CPU actually uses. bv_decide turns the question into a SAT problem, a yes-or-no puzzle about bits that a solver program can settle for every input without trying them one by one. If your claim fails for any input, it reports one that breaks it.
So instead of "here are 50 test cases that pass," you get "this property holds for all four billion 32-bit inputs" or "it fails when a = 4294967295." Either answer is more than a test suite can give you.
How the skill works
The skill's SKILL.md lays out a seven-step loop. In plain terms:
- Check the setup. A script looks for Lean and its tools. If they are missing, the skill asks before installing anything.
- Pick one target. One function or one rewrite rule. Anything bigger gets split.
- Model it. The agent rewrites the function in Lean, matching how the original language actually behaves.
- State the property, meaning the rule the function must always obey: either "this should always equal the simple version" or a condition like "the result stays in range."
- Hunt for a counterexample first. Before trying to prove anything, ask Lean to find an input that breaks it.
- Prove it if no counterexample turns up. Using a short ladder of Lean tactics, simplest first.
- Translate back. Turn any counterexample into a real test in the original language and confirm it fails there.
Step 5 is the insight that makes this practical. A proof is expensive and tells you something you hoped was true. A counterexample is cheap and tells you exactly where to look. The skill's bug-hunting guide says it directly: a counterexample "saves the proof effort and is directly usable as a bug report."
The classic example
The skill ships a runnable example of the oldest bug in the book: averaging two numbers with (a + b) / 2. If a + b is bigger than the integer can hold, the sum wraps around and the average comes out wrong.
import Std.Tactic.BVDecide
def avgBad (a b : BitVec 32) : BitVec 32 := (a + b) >>> 1
def avgSpec (a b : BitVec 32) : BitVec 32 :=
((a.zeroExtend 33 + b.zeroExtend 33) >>> 1).truncate 32
theorem bad_ng (a b : BitVec 32) : avgBad a b = avgSpec a b := by
unfold avgBad avgSpec; bv_decide
The spec computes the same average in 33 bits, where it cannot overflow. The skill's docs say bv_decide fails this theorem with the counterexample a = b = 0xFFFFFFFF, the largest 32-bit number. The same file proves a safer version, (a & b) + ((a ^ b) >> 1), equal to the spec for every input. Running that whole file ends in an error by design, because bad_ng is meant to fail.
Notice the pattern. The spec is written to be obviously correct, even if slow. The bug-hunting guide warns that writing the spec the same way as the implementation "just duplicates the same bug."
The same mode works for performance work. Two lines in that file prove that (x << 3) + (x << 1) equals x * 10 and that x % 8 equals x & 7 for every 32-bit value. If you are about to swap a readable expression for a faster bit trick, this is how you know the swap is safe.
What happened when someone pointed it at Clojure
The Clojure post is the best evidence so far that this approach finds real bugs. Its author applied the lean-verify procedure to Clojure 1.12.7, released November 18, alongside differential testing, and reports 17 bugs that they could not find in Clojure's JIRA tracker or on ask.clojure.org.
The vector-of bug shows the method cleanly. The code rounds an index down to a multiple of 32 with the mask 0xffe0. That mask also clears every bit above bit 15, so when the next chunk should start at 65,536, the code computes 0 and starts reading the vector again from the beginning, forever. In Lean, the author wrote the property "from a position that is a multiple of 32, the next chunk starts 32 later." bv_decide rejected it and returned a counterexample. A corrected mask passed.
Two details from that post matter for anyone trying this.
First, Lean only handled the small pieces. For the big persistent data structures, the author used differential testing instead, running the same random operations against Clojure's maps and Java's HashMap and comparing results, 1,500 operations for each of 2,000 seeds. Lean is a scalpel, not a scanner.
Second, the author reports that the bugs sat outside the main tree structures, "in places where two mechanisms meet, such as = and hashing." That is a useful map for where to aim your own first check.
Put this into practice
Install the skill one of three ways, per the README on the main branch (the APM option was added after the v1.0.0 tag):
# GitHub CLI skill support (preview)
gh skill install hirosassa/lean-verify lean-verify
# or Microsoft's Agent Package Manager
apm install hirosassa/lean-verify
# or by hand
git clone https://github.com/hirosassa/lean-verify /tmp/lean-verify
cp -r /tmp/lean-verify/skills/lean-verify ~/.claude/skills/
Then pick a good first target. The skill's own fit table rates these as excellent: integer arithmetic, bit operations, shifts, wrapping math, and "is the optimized version equal to the naive one?" Good candidates in most codebases include a hash function, a midpoint or average, a bounds check, a chunking or pagination calculation, or any line with a hex mask in it.
A prompt as plain as this works, because the skill's description tells Claude Code when to load it:
Use lean-verify on
next_chunk_startinsrc/vec.rs. Look for a counterexample first, then write a failing test if you find one.
What to expect:
- The first run is slow. If Lean is not installed, the skill asks, then installs
elan(Lean's version manager, the equivalent ofrustup) and downloads a toolchain. The setup notes warn this "takes minutes." - A counterexample shows up as an error. That is the good outcome. The next step is a test in your real language that reproduces it.
- A proof shows up as silence. No error means the property holds, for the model.
- "Undecided" is a real answer. If Lean can neither prove nor refute the claim, the docs say to suspect the spec or a missing precondition, such as
x ≠ 0.
Keep the reproducing test. It becomes your regression test, and it is the part of this whole exercise that protects the real code.
The modeling trap
Almost every false alarm comes from the model not matching the language. The SKILL.md lists the traps, and they are worth reading before you trust any result:
- Lean's fixed-width numbers wrap around on overflow. Rust's
+panics in debug builds instead, so the matching Rust operation iswrapping_add. - Shifting a 32-bit number by 32 or more gives 0 in Lean, panics in Rust debug builds (the amount is masked in release), and is undefined behavior in C.
- Division and remainder round differently across languages for negative numbers.
- Floating point is out of scope for
bv_decideentirely.
An agent writing the model will get some of these wrong. That is why step 7 exists, and why you should never skip it.
Honest limitations
The biggest one is written into the skill itself. A proof shows the model is correct, not your code. The report format instructs the agent: "Do not rephrase 'proved' as 'the original code is correct'." Whether the model matches the real function is, in the skill's words, "a human guarantee."
It is built for small, pure functions. The skill rates I/O, concurrency and networking as a poor fit and says to use ordinary tests or fuzzing there.
A counterexample is not automatically a bug you care about. The bug-hunting guide notes that whether a failing input is reachable in real calls "is a separate question." Check that before you file anything.
The project is young. As of November 25 it has a few dozen stars (counts disagreed between sources that day), one release, and no pinned Lean version in the repo; the optional property-testing add-on pins v4.36.0 and must match whatever Lean you install. The Clojure results are one author's report on their own run. And I read the skill, its references and its example files for this piece but did not run Lean myself, so the outputs quoted here come from the project's docs and the Clojure post.
Where this leaves you
You do not need to become a formal methods person to get value from a theorem prover anymore. You need one function you are not quite sure about, an agent that can write the model, and the discipline to turn whatever comes back into a test. Start with the line in your codebase that has a hex mask in it. If it is fine, you will know for every input. If it is not, you will know which input.
Sources: hirosassa/lean-verify v1.0.0 (README, SKILL.md, bug-hunting.md, setup.md, example), MIT, reviewed at tag v1.0.0; Conao3, "I checked Clojure's core with Lean and differential testing" (Nov 24, 2026). All read November 25, 2026.