spock-cli 0.2.1

A model checker for specs that read like Rust: TLA's logic of actions, checked on every core in machine code
spock-cli-0.2.1 is not a library.

spock

Find the bugs in a design before they are in the code. spock tries every way your system can run, and when one goes wrong it shows you the shortest way there. You write the spec in a language that reads like a little Rust.

  • Specs read like Rust. The state is a struct, an action is a function that returns the next state, and a property is a function that returns Bool.
  • The shortest path to the bug. When an invariant fails, spock prints the fewest steps that reach it, and shows only what each step changed.
  • Checks what TLA+ checks: invariants, deadlocks, properties of steps, temporal properties under weak and strong fairness, refinement between specs, and symmetry. On 180 models from the TLA+ examples, spock gives the same answers as TLC, or as the TLC-compatible everystate where TLC takes hours.
  • Fast. Specs compile to machine code and use every core. In our benchmarks, spock is about 10 times faster than TLC on the same cores, and it checks a model with 59 million states in 36 seconds on a 12-core laptop.
  • Never checks the same thing twice. If nothing changed, the answer comes from the cache.
  • Checks your code against the spec. spock trace reads a log of a real run and finds a behavior of the spec that matches it, or the first step the spec doesn't allow.
  • Ready for CI. The exit code says what was found, and --json gives the full report.

Install

You need Rust 1.90 or newer.

$ cargo install --locked spock-cli

spock is tested on macOS with Apple Silicon.

Your first spec

Two processes each add one to a shared counter. Each one reads the counter into a register, then writes the register plus one. Save this as counter.spock:

type Proc;

type Pc = | Read | Write | Done

struct State {
    x: Int,
    pc: Map<Proc, Pc>,
    reg: Map<Proc, Int>,
}

impl State {
    action init() -> State {
        State {
            x: 0,
            pc: { p -> Read for p: Proc },
            reg: { p -> 0 for p: Proc },
        }
    }

    action read(self, p: Proc) -> State {
        require self.pc[p] == Read;
        self with { reg[p] = self.x, pc[p] = Write }
    }

    action write(self, p: Proc) -> State {
        require self.pc[p] == Write;
        self with { x = self.reg[p] + 1, pc[p] = Done }
    }

    action next(self) -> State {
        any p: Proc {
            any { self.read(p), self.write(p) }
        }
    }

    fn no_lost_update(self) -> Bool {
        all p: Proc { self.pc[p] == Done } ==> self.x == self.pc.len()
    }
}

model two_procs {
    type Proc = {a, b};
    init = State::init;
    next = State::next;
    invariants = [State::no_lost_update];
    deadlock = false;
}

An action takes the current state and returns the next one. require says when it can run, and any lets spock pick any process. A model says which values to check with and what must hold. Check it:

$ spock check counter.spock
error: invariant State::no_lost_update is violated
  --> counter.spock:36:8
   |
36 |     fn no_lost_update(self) -> Bool {
   |        ^^^^^^^^^^^^^^
a shortest behavior to a state that violates it, 5 states:
  1  State::init
       x = 0
       pc = {a -> Read, b -> Read}
       reg = {a -> 0, b -> 0}
  2  State::read(a)
       pc[a] = Write
  3  State::read(b)
       pc[b] = Write
  4  State::write(a)
       x = 1
       pc[a] = Done
  5  State::write(b)
       pc[b] = Done
two_procs: 12 distinct states, depth 5 (13 generated in 0.00 s)

Both processes read 0 before either writes, so one update is lost and the counter ends at 1.

Check that something eventually happens

Invariants say nothing bad happens. Temporal properties say something good eventually does. Add this to impl State:

    fn finishes() -> Temporal {
        eventually(|s: State| all p: Proc { s.pc[p] == Done })
    }

and a model that checks it:

model finishes {
    type Proc = {a, b};
    init = State::init;
    next = State::next;
    properties = [State::finishes];
    deadlock = false;
}
$ spock check counter.spock --model finishes
error: temporal property State::finishes is violated
  --> counter.spock:57:19
   |
57 |     properties = [State::finishes];
   |                   ^^^^^^^^^^^^^^^
a behavior that violates it, 1 state, then stutters forever:
  1  State::init
       x = 0
       pc = {a -> Read, b -> Read}
       reg = {a -> 0, b -> 0}
finishes: 13 distinct states, depth 5 (15 generated in 0.00 s)

Nothing forces a process to take a step, so the system may just stop. Say that next keeps running whenever it can, and the property holds:

    fairness = [weakly_fair(State::next)];
$ spock check counter.spock --model finishes
finishes: ok, 13 distinct states, depth 5 (15 generated in 0.01 s)

Check a run of your code

Once the spec is right, check that the code follows it. Have the code log each step as a line of JSON: the action, its arguments, and what it can see of the state after it. It may leave out any part of the state. Here b read after a wrote, but still wrote 1:

{"state":{"x":0}}
{"action":"read","args":["a"]}
{"action":"write","args":["a"],"state":{"x":1}}
{"action":"read","args":["b"]}
{"action":"write","args":["b"],"state":{"x":1}}
$ spock trace counter.spock run.jsonl --model two_procs
error: no behavior of two_procs matches line 5 of run.jsonl
 --> run.jsonl:5:1
  |
5 | {"action":"write","args":["b"],"state":{"x":1}}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
the spec's State::write(b) gives x = 2, and the line says x = 1
a behavior that matches lines 1 to 4, 4 states:
  1  State::init  // line 1
       x = 0
       pc = {a -> Read, b -> Read}
       reg = {a -> 0, b -> 0}
  2  State::read(a)  // line 2
       pc[a] = Write
  3  State::write(a)  // line 3
       x = 1
       pc[a] = Done
  4  State::read(b)  // line 4
       pc[b] = Write
       reg[b] = 1
two_procs: a behavior matches 4 of 5 states of run.jsonl (at most 1 candidate state, 7 generated in 0.00 s)

A line without an action is any step, or none. A behavior spock check prints is a trace too. When the code takes steps it can't log, such as a lost message, list their actions in the model, hidden = [State::lose];, and any number of them may come between two lines.

Coming from TLA+

TLA+ spock
VARIABLES x, pc struct State { x: Int, pc: Map<Proc, Pc> }
CONSTANT Proc, and Proc <- {a, b} in the .cfg type Proc;, and type Proc = {a, b}; in a model
Init, Next action init() -> State, action next(self) -> State
\E p \in Proc : A(p) any p: Proc { self.a(p) }
\A p \in Proc : pc[p] = "done" all p: Proc { self.pc[p] == Done }
an enabling condition require cond;
x' = x + 1 /\ pc' = [pc EXCEPT ![p] = "done"] /\ UNCHANGED y self with { x = self.x + 1, pc[p] = Done }
[]P, <>P, P ~> Q always(..), eventually(..), leads_to(..)
WF_vars(Next), SF_vars(A) weakly_fair(State::next), strongly_fair(State::a)
INVARIANT, PROPERTY, SYMMETRY in the .cfg invariants = [..], properties = [..], symmetry = [..] in a model

Learn more

  • specs/ has 125 specs ported from the TLA+ examples, among them Paxos, two-phase commit, the bakery algorithm, dining philosophers and Dijkstra's termination detection.
  • The language guide covers types, functions, actions, properties, fairness and models.
  • spock check --help lists the options: --model, --workers, --json, --no-cache and more.
  • The exit code is 0 when everything holds, 10 for a false assumption, 11 for a deadlock, 12 for an invariant, 13 for a property, and 150 for an error in the spec. spock trace exits 0 when a behavior matches, and 14 when none does.
  • Trace validation says how spock trace matches a run against the spec.
  • spock skill prints a skill that teaches coding agents to write, check and trace specs. Install it with mkdir -p ~/.agents/skills/spock && spock skill > ~/.agents/skills/spock/SKILL.md.

License

MIT, in LICENSE. The specs in specs/ are ports of tlaplus/Examples, MIT-licensed by the TLA+ project and its contributors. crates/everystate is Matt Carey's everystate, MIT, with its own LICENSE.