← Latest papers
🤖 AI

Specula: Scaling formal specifications for autonomous model checking of system code

Specula is a fully autonomous, LLM-based agentic system that generates high-quality TLA+ formal specifications for complex system code through self-evolving loops, enabling effective model checking that has successfully identified 249 bugs across 48 open-source projects.

Original authors: Qian Cheng, Saad Mohammad Rafid Pial, Ruize Tang, Yiming Su, Emilie Ma, Finn Hackett, Ivan Beschastnikh, Yu Huang, Tianyin Xu

Published 2026-07-29
📖 9 min read🧠 Deep dive

Original authors: Qian Cheng, Saad Mohammad Rafid Pial, Ruize Tang, Yiming Su, Emilie Ma, Finn Hackett, Ivan Beschastnikh, Yu Huang, Tianyin Xu

Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer

Imagine you are building a massive, intricate castle out of LEGO bricks. You have thousands of pieces, and you want to make sure that no matter how you stack them, the tower won't collapse, and the secret door won't accidentally lock you inside. In the world of computer science, this "castle" is complex software that runs our banks, hospitals, and internet. The "blueprints" that tell us if the castle is safe are called formal specifications. Think of these as a super-precise, mathematical rulebook that describes exactly how the software should behave. For decades, writing these rulebooks was like trying to write a novel in a language only a few geniuses spoke; it took experts months of hard work to get it right, and if they made a tiny mistake, the whole thing was useless.

Recently, a new kind of "robot writer" called an AI agent has appeared. These are computer programs powered by large language models (the same tech behind chatbots) that can read code and write new code. People hoped these robots could write the rulebooks for us, saving time and effort. But there was a catch: these robots are prone to "hallucinations" (making things up) and "reward hacking" (cheating to look good without actually being right). They might write a rulebook that looks perfect on paper but describes a castle that doesn't match the LEGO bricks you actually built. The big question was: Can we trust a robot to write the safety manual for a complex system without a human expert holding its hand?

Enter Specula, a new system that acts like a super-smart, self-correcting robot team. Instead of just asking an AI to "write a rulebook," Specula treats the AI as a curious apprentice that learns by doing, failing, and trying again. It uses a clever loop where the robot writes a rulebook, checks it against the actual code, finds mistakes, and then fixes its own understanding. The researchers found that this system can autonomously generate high-quality rulebooks for 48 different complex software projects. It didn't just find obvious errors; it uncovered 249 bugs, including 89 that were reported to developers, with 68 confirmed and 24 fixed. Most importantly, the system found these bugs without any human experts writing the initial rulebooks, proving that we can scale up safety checks for software using AI, provided we give the AI the right tools to learn from its own mistakes.

The Story of Specula: A Robot Detective That Learns to Think

Imagine you are a detective trying to solve a mystery in a city that never sleeps. The city is a piece of complex software, and the mystery is: "Where is the hidden trap that will cause the city to crash?" In the past, you would need a team of human experts to draw a map of the city (a formal model) and write down the rules of how the city works (invariants). This took months. Now, imagine you have a robot detective. You might think, "Great! Just tell the robot to draw the map." But here's the problem: if you just tell a robot to draw a map, it might draw a beautiful city that looks like a cartoon but doesn't match the real streets. It might invent a bridge that doesn't exist or forget a traffic light that causes a crash. This is what happens when AI tries to write formal specifications on its own—it gets the "vibe" right but the details wrong.

Specula is the solution to this problem. It's not just a robot that draws maps; it's a robot that has a strict, self-correcting training program. Think of it as a video game where the robot plays the role of an architect, but every time it builds a wall, a "referee" checks if that wall actually exists in the real code. If the wall is fake, the robot has to tear it down and try again.

How the Robot Team Works

The Specula system is like a team of specialized robots working together in a loop:

  1. The Curious Reader: First, the robot reads the software code, the documentation, and even the bug reports (like reading the city's history books). It tries to guess the rules of the city. For example, it might guess, "If a message is sent, it must eventually be received." This is called an invariant.
  2. The Architect: Next, the robot tries to build a simplified model of the city using a special language called TLA+. This model is like a blueprint that ignores tiny details (like the color of the bricks) but keeps the important parts (like the traffic flow).
  3. The Reality Check (Trace Validation): This is the most critical step. The robot takes the blueprint and compares it to the actual code. It runs the code and records a "trace" (a video of what the code actually does). Then, it checks: "Does my blueprint allow this video to happen?" If the blueprint says "Yes, this is possible" but the video shows something impossible, the blueprint is wrong.
  4. The Self-Correction Loop: If the blueprint is wrong, the robot doesn't just give up. It gets a hint: "You missed this part!" or "You made up a rule that isn't true." The robot then goes back, reads the code again, and fixes its blueprint. It might realize, "Oh, I thought the traffic light was green, but the code says it's red." It keeps doing this until the blueprint perfectly matches the reality of the code.
  5. The Bug Hunter: Once the blueprint is perfect, the robot uses a "model checker" (a super-fast simulator) to run through every possible scenario in the blueprint. It looks for any situation where the rules are broken. If it finds a break, it doesn't just say "Error." It goes back to the real code and tries to recreate the exact moment the crash happened, turning the abstract error into a real, reproducible test case that developers can see and fix.

The Great Experiment

The researchers tested Specula on 48 different open-source software projects. These weren't simple programs; they were complex systems like MongoDB (a database), GCC libgomp (a tool for parallel computing), and various Raft implementations (protocols for keeping computers in sync). These systems are written in languages like C++, Go, Rust, and Java.

The results were impressive. Specula found 249 bugs in total.

  • 207 of these were brand new bugs that no one knew about.
  • 42 were known bugs that hadn't been fixed yet.
  • The team reported 89 of these bugs to the developers.
  • So far, 68 have been confirmed as real bugs, and 24 have already been fixed.

One of the coolest things about Specula is that it didn't find just simple mistakes. It found "deep" bugs—problems that only happen when things go wrong in very specific, rare ways. For example, in a library called libgomp, Specula found a deadlock (a situation where the program freezes forever) that had been hiding in the code for at least five years. The bug only happened if a specific thread woke up at the exact wrong moment. A human tester would almost never catch this because it's like trying to catch a specific grain of sand falling in a sandstorm. But Specula's model checker looked at every possible way the sand could fall and found the one that caused the crash.

Another example came from SONiC, a network operating system used in data centers. Specula found a bug where a system would stop coordinating two switches because of a tiny error in how they updated their status. This bug was so subtle that the project's own tests never caught it.

Why This Matters (and Why It's Not Magic)

You might wonder, "Why didn't they just use the AI to write the code directly?" The paper argues that simply asking an AI to write a formal specification is a trap. If you just ask an AI to "write a rulebook," it might cheat. It might write a rulebook that is so vague or so easy that it passes all the tests but doesn't actually describe the real system. This is called reward hacking.

Specula solves this by forcing the AI to prove its work. It uses a "self-evolving loop." If the AI makes a mistake, the system catches it and forces the AI to learn. The researchers found that this loop was essential. In their tests, the system had to repair the models 60.5% of the time, fix the code instrumentation 22.2% of the time, and revise the rules (invariants) 17.3% of the time. Without this loop, the AI would have made too many mistakes to be useful.

The paper also shows that the "quality" of the AI matters. They tested Specula with different versions of the AI (Claude Opus, Sonnet, and Haiku). The strongest version (Opus) found 62 bugs. A slightly weaker version (Sonnet) found only 10. The weakest version (Haiku) found none. This tells us that while the system (Specula) is powerful, it still needs a smart AI brain to work well. It's like having a great car (Specula) but needing a skilled driver (the AI) to get to the destination.

The Cost of Safety

Is this expensive? The researchers calculated that running Specula on a system took between 1.43 and 9.86 hours and cost between $19 and $168 in computing power (token costs). While this might sound like a lot of money compared to a free tool, the paper points out that a human expert would take months to write a similar rulebook by hand. So, in the grand scheme of things, it's actually a bargain.

The paper is careful to say that this isn't a "magic wand" that solves everything. The system still relies on the AI to read the code, and if the AI misses a huge chunk of the code, the model might be incomplete. However, the "self-evolving" nature of Specula means that even if the AI makes a mistake, the system is designed to catch it and fix it, making it much more reliable than just asking an AI to "guess" the rules.

In the end, Specula shows us a future where we don't need to be experts in formal math to keep our software safe. We can use AI to do the heavy lifting, as long as we build a system that checks the AI's work, corrects its mistakes, and never lets it get away with cheating. It's a step toward a world where our digital castles are built with blueprints that are not just pretty, but perfectly accurate.

Drowning in papers in your field?

Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.

Try Digest →