Satchel Hamilton
← Work
Solo design & build · 2026

Congruent: checks that an AI rewrite still does what the original did

Given an original function and an AI-rewritten one, Congruent returns one of three answers: equivalent within a stated bound, the concrete input where they disagree, or unknown. It never reports a guess as a proof.

Source ↗PyPI ↗

Coding agents refactor, migrate and "optimize" code all day. When someone asks whether a rewrite kept the original behavior, the usual answer is that the tests still pass. Congruent gives a stronger answer for a narrow kind of code: EQUIVALENT up to bound N, COUNTEREXAMPLE with the input that breaks it, or UNKNOWN.

It is equivalence checking, the kind chip designers use to compare two versions of a circuit, applied to AI-written code. This essay covers the wider question: which verification methods carry over to AI systems and which don't.

The problem

Tests only sample the inputs. A refactor can pass every test and still differ on an input nobody wrote a test for. The claim worth having is stronger: within a stated bound, no input exists on which the two functions disagree. Formal methods can make that claim, and when it is false they can produce the input.

How it works

Cheap checks run first. The expensive proof runs only when they find nothing.

  1. Differential testing. Random and boundary inputs go through both functions. Obvious differences show up in milliseconds, and a counterexample here ends the run.
  2. Symbolic execution. Both functions are translated into Z3 bit-vector constraints, and the solver is asked whether any input makes the outputs differ. UNSAT means equivalent within the bound. SAT decodes back into a concrete input.
  3. Bounded model checking. Loops are unrolled to depth k, and the verdict reports that depth.

The example it was built around

An LLM "simplifies" a midpoint calculation from lo + (hi - lo) // 2 to (lo + hi) // 2. With 32-bit integers the second form overflows. Congruent returns the inputs that show it, lo = 1, hi = 2147483647, along with both outputs.

The same check runs as a loop. A model proposes a rewrite, Congruent checks it, and any counterexample goes back to the model to fix. The loop never accepts a rewrite that hasn't been proven. In a live run, a real model made exactly the midpoint mistake, was handed the overflowing input, and came back with the safe form, which Congruent then proved equivalent.

Why you can trust a verdict

  • UNKNOWN is a real answer. When the symbolic stage can't model something, the result is UNKNOWN. It is never upgraded to EQUIVALENT.
  • Every verdict states its bound and assumptions: integer width, unroll depth, preconditions. It claims nothing beyond them.
  • CI fails on a wrong verdict. The recall benchmark exits non-zero on any false EQUIVALENT or false COUNTEREXAMPLE. Today it is 16 for 16 with none wrong.
  • A fuzzer checks the checker. It generates random pairs of functions, asks Congruent, and re-validates each verdict against a plain interpreter: 3,000 pairs with none wrong at its default settings.
  • Two oracles share no code with Congruent. One turns generated programs back into real Python and compares behavior. It caught a negative-indexing bug that both stages had in common. The other checks integer overflow against numpy's fixed-width integers, including every pair of 8-bit operands.

Where it stands

The core is complete. It handles a subset of Python (integers, booleans, branches, bounded loops, fixed-length lists, bounded strings, and assume(...) preconditions) and a subset of C through a second front end.

Not supported: floating point, side effects and I/O, recursion and function calls, unbounded loops, and heap aliasing. Solver time climbs steeply as the bound grows, which is why the default bound is 8.