Skip to content

Code Lab

pstack Explained: Find a One-Step State Machine Bug with TLA+

By Miles Park·
GitHub preview of cursor/plugins

CODE LAB · PROJECT SPOTLIGHT

pstack TypeScript · MIT

Check a state model and explain the shortest path to a broken rule.

Preview: GitHub · cursor/plugins

Lauren Tan’s All Code Is Slop (Except Mine) talks about turning recurring review comments into checks the coding agent can run. One of the tools in her pstack plugin is statechart. Give it a TLA+ specification and it can ask TLC to check its behavioral rules, then explain any counterexample as a sequence of recognizable actions. Original AI Engineer talk.

Model a rule before writing the fix

In the upstream MediaPlayer example, playback and volume are separate state variables. The player begins stopped with normal volume. The LoudWhenStopped invariant forbids the combination of stopped playback and loud volume. In the original YouTurnItUp action, normal volume is enough to enable the action, so a user can turn the volume up while the player remains stopped. Pinned original model.

Side-by-side results from local pstack runs showing a one-step counterexample and its fixed model passing
We ran the two upstream fixtures with Node.js on Windows and TLC 1.7.4. The graphic summarizes the observed reports. Original fixture report · Fixed fixture report.

See how pstack reconstructs the shortest path

After TLC explores the model, pstack walks the resulting transition graph. The loop below starts with the initial states, visits their neighbors in breadth-first order, and stores the first incoming edge for each reached state. A later pass can follow those incoming edges backward from a state that breaks an invariant.

cursor / pluginsTypeScript · source excerpt
TS  statechart/scripts/passes/reach.ts
  for (const s of starts) {    depth[s] = 0;    reached[size++] = s;  }  for (let q = 0; q < size; q++) {    const s = reached[q];    for (let e = first[s]; e < first[s + 1]; e++) {      const t = to[next[e]];      if (depth[t] >= 0) continue;      depth[t] = depth[s] + 1;      via[t] = next[e];      reached[size++] = t;    }  }
Lines 71–84 · commit eb55343View original on GitHub ↗

Code copyright 2026 Lauren Tan. MIT license.

Run the original and fixed fixtures

Use the pinned pstack/skills/statechart directory with a supported Node version and TLC. We supplied portable Temurin Java 25 and the checksum-verified TLC 1.7.4 jar through the documented JAVA and TLA2TOOLS variables. From the scripts directory, run each fixture into its own new output folder.

node statechart.ts test/corpus/MediaPlayer/MediaPlayer.tla --view test/corpus/MediaPlayer/view.mjs --out out-original
node statechart.ts test/corpus/MediaPlayerFixed/MediaPlayerFixed.tla --view test/corpus/MediaPlayerFixed/view.mjs --out out-fixed

The first run exited 1 and reported one step to the broken state: turn the volume up while stopped. The fixed fixture prevents that transition with a guard on YouTurnItUp. It exited 0 with LoudWhenStopped holding. Both generated reports and Mermaid charts matched the checked-in fixture outputs. The fixed configuration omits the original EventuallyStops liveness check. Our comparison covers the shared volume invariant and the declared example models. Pinned fixed model · Runtime and invocation guide.

Choose what to verify in your own project

Start with a rule you can explain to a user, such as a save indicator appearing only after persistence. Write its model, inspect the shortest failing sequence, and use it to guide a test of the running application. A checked model establishes a result within its modeled states and actions. The application test checks whether the implementation and the model agree.

Illustration from pstack’s verification guide showing an engineer collecting evidence during a test
Illustration from Lauren Tan’s pstack verification guide. The guide focuses on collecting evidence from the feature the user actually exercises. Creator’s verification guide.

Read our Cursor plugin manifest walkthrough for the repository’s plugin format. This story came from MINDUNG’s breakdown of Lauren Tan’s talk.

Join the discussion

Your email address will not be published. Required fields are marked *