Software engineers are currently trapped in a cycle of blind trust and frantic debugging. As large language models generate increasingly complex blocks of code, the industry has relied on a fragile safety net of unit tests and black-box validation. This approach works for simple scripts, but in the realm of computational geometry, a single floating-point error or a missed edge case can collapse an entire 3D model. The community is searching for a way to move beyond hoping the AI is correct to knowing it is mathematically impossible for the code to be wrong.

The Architecture of Mathematical Certainty

A new implementation of 3D Constructive Solid Geometry (CSG) mesh intersection has achieved a milestone by becoming the first of its kind to be formally verified using Lean 4. The project focuses on the high-stakes task of calculating the intersection of two 3D meshes, a process that requires absolute precision to ensure the resulting surface is structurally sound. To achieve this, the developers utilized a formal specification consisting of only 93 lines of code. This lean specification serves as the mathematical ground truth that the final implementation must satisfy.

The scale of the AI's contribution is massive compared to the human-verified specification. An AI agent autonomously authored over 1,000 lines of implementation code and a staggering 60,000 lines of Lean proofs. Lean 4, an interactive theorem prover, was used to bridge the gap between the high-level specification and the executable code. The system was tested using the Stanford bunnies dataset, where the intersection of two models—each consisting of 70,000 triangles—was calculated in 24 seconds. While this processing speed is significantly slower than the fastest modern mesh intersection libraries, the delay is a deliberate engineering trade-off. The developers prioritized human-verifiability and mathematical integrity over raw runtime performance, ensuring that the logic could be audited without requiring a PhD in geometry for every line of code.

The Specification Shift

The true breakthrough here is not that AI wrote the code, but how the human interacts with that code. In a traditional workflow, a senior engineer would be tasked with reviewing the 1,000 lines of implementation and the 60,000 lines of proof to ensure no hallucinations occurred. This is a cognitive impossibility. Instead, this project flips the trust model: the human reviewer ignores the 61,000 lines of AI-generated output entirely and focuses exclusively on the 93-line formal specification. Because the Lean checker mechanically verifies that the implementation and the proofs satisfy the specification at compile time, the massive volume of AI code becomes a black box that is mathematically guaranteed to be correct.

This shift in verification revealed the dangers of traditional testing. When the formally verified Lean kernel was compared against a C++ implementation of the same specification, the Lean-verified version identified three unique bugs that black-box testing had completely missed. These bugs were caught because the AI agent was tasked with incrementally refining the specification, starting from a simple, provable version and expanding it toward the full requirements. The verification focused on well-formedness conditions, ensuring the output mesh is a watertight surface with no self-intersections, a single solid boundary with consistent outward orientation, and no degenerate triangles. While the system allows surfaces to touch along edges or vertices, it forbids any intersection that would violate the integrity of the solid. By moving the point of human intervention from the implementation review to the specification review, the team reduced the cost of verification while increasing the level of certainty.

This methodology proves that the path to reliable AI software is not through better prompts, but through the adoption of formal specifications that turn AI-generated code into a provable mathematical certainty.