# 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. Live long and prosper.
- **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](https://github.com/mattzcarey/everystate) where TLC takes hours.
- **Fast.** Specs compile to machine code and use every core. In
[our benchmarks](docs/rfds/RFD0004-compiled-engine.md), 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.** `spock test specs/` checks every model in parallel against its `expect`
setting (including bug models that must fail), exit codes say what was found, and `--json`
gives the full report.
## Install
You need Rust 1.90 or newer.
```console
$ 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`:
```rust
type Proc;
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 { ..self, reg: self.reg.update(p, self.x), pc: self.pc.update(p, Write) }
}
action write(self, p: Proc) -> State {
require self.pc[p] == Write;
Self { ..self, x: self.reg[p] + 1, pc: self.pc.update(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:
```console
$ spock check counter.spock
error: invariant State::no_lost_update is violated
--> counter.spock:36:8
|
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)
🖖 "Once you have eliminated the impossible, whatever remains, however improbable, must be the truth." — Spock
```
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`:
```rust
fn finishes() -> Temporal {
eventually(|s: State| all p: Proc { s.pc[p] == Done })
}
```
and a model that checks it:
```rust
model finishes {
type Proc = {a, b};
init = State::init;
next = State::next;
properties = [State::finishes];
deadlock = false;
}
```
```console
$ spock check counter.spock --model finishes
error: temporal property State::finishes is violated
--> counter.spock:57:19
|
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)
🖖 "Live long and prosper — though this behavior does neither." — Spock
```
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:
```rust
fairness = [weakly_fair(State::next)];
```
```console
$ 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:
```json
{"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}}
```
```console
$ 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
|
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)
🖖 "Highly illogical. The trace departs from the specification." — Spock
```
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+
| `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 { ..self, x: self.x + 1, pc: self.pc.update(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/`](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](docs/rfds/RFD0001-spock-language.md#guide-level-explanation) covers
types, functions, actions, properties, fairness and models.
- `spock check --help`, `spock test --help` and `spock trace --help` list the options:
`--model`, `--filter`, `--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, 12 or 13 when it violates an invariant or step property, and 14
when none matches. `spock test` exits 0 when every model matches its `expect`, and 1
otherwise.
- [Trace validation](docs/rfds/RFD0008-trace-validation.md) 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`](LICENSE). The specs in [`specs/`](specs/) are ports of
[tlaplus/Examples](https://github.com/tlaplus/Examples), MIT-licensed by the TLA+ project and
its contributors.