Bridging the Gap with TLA+ and Formal Verification
The search for implicit states quickly bore fruit, identifying several areas where the application’s behavior was ambiguous. However, identifying a potential bug in a text-based terminal is only the first step; understanding the "why" and "how" of a failure requires a more rigorous framework. This is where TLA+ enters the development workflow.

TLA+, a formal specification language developed by Turing Award winner Leslie Lamport, is designed for modeling and verifying concurrent and distributed systems. Unlike standard programming languages that describe how a system should perform an action, TLA+ describes what the system is allowed to do. It treats a program as a mathematical object, allowing for the exhaustive verification of every possible state the system can enter. Historically, TLA+ has been the domain of specialized engineers at firms like Amazon and Microsoft, used to verify the core logic of cloud databases and consensus algorithms.
The breakthrough in the "coffeeshop-jev" project was the use of the coding agent to bridge the gap between practical C# code and formal TLA+ specifications. By utilizing specialized skills such as /create-formal-verification-skill, the agent was able to model the application’s subsystems into formal specifications. This process resulted in the creation of three distinct verification models: verify-ask-loop, verify-answer-channel, and verify-fulfilment.

Through this formal modeling, the AI identified a specific logical failure in the "Ask Loop"—the mechanism that governs how the agent clarifies an order with a customer. The verification process revealed that in the UI path, certain "Extract" failures were being counted toward the maximum attempt cap improperly. A comment in the code even suggested that previous attempts to fix this had introduced a new bug, where "Gate-only counting" was causing the system to miscalculate when to terminate a request.
Visualizing Logic Failures Through Statecharts
While TLA+ provides the mathematical proof of a bug, it can be difficult for developers to grasp the specific sequence of events leading to a failure. To solve this, the workflow incorporated statecharts to visualize the most important user flows. Following a methodology popularized by Lauren Tan, the developer integrated "pstack" and statechart visualization commands to generate Mermaid diagrams directly from the verified models.

The resulting visualization for the ask-loop subsystem was revealing. In the statechart, the AI highlighted specific transitions in red and yellow, marking the exact points where the logic could fail. The diagram showed that the system could enter a state where it reached its MaxAsks threshold prematurely or failed to reset counters correctly after a specific type of user error.
