everystate 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -2
- package/native/darwin-arm64/everystate +0 -0
- package/native/darwin-x64/everystate +0 -0
- package/native/linux-arm64/everystate +0 -0
- package/native/linux-x64/everystate +0 -0
- package/native/win32-x64/everystate.exe +0 -0
- package/package.json +8 -2
- package/skills/formal-verification/SKILL.md +115 -0
- package/skills/formal-verification/example/Charge.cfg +7 -0
- package/skills/formal-verification/example/Charge.tla +61 -0
- package/skills/formal-verification/example/ChargeBroken.cfg +7 -0
- package/wasm/everystate.wasm +0 -0
package/README.md
CHANGED
|
@@ -100,6 +100,14 @@ operators.
|
|
|
100
100
|
shell out to TLC can point at it instead of a JVM. The pstack `statechart` script runs unmodified
|
|
101
101
|
this way.
|
|
102
102
|
|
|
103
|
+
## Agent skill
|
|
104
|
+
|
|
105
|
+
[`skills/formal-verification`](skills/formal-verification/SKILL.md) teaches a coding agent to
|
|
106
|
+
model-check a design with everystate: choose the promise, model the steps, run the check, see it
|
|
107
|
+
fail, report the counterexample in the code's words. It comes with a worked example
|
|
108
|
+
(`example/Charge.tla`). Copy the folder into your agent's skills directory, for example
|
|
109
|
+
`.claude/skills/`.
|
|
110
|
+
|
|
103
111
|
## Options
|
|
104
112
|
|
|
105
113
|
```
|
|
@@ -140,8 +148,10 @@ npm run build # native binary and wasm/everystate.wasm
|
|
|
140
148
|
npm test # WASM API, pretty-printer and community-module tests
|
|
141
149
|
```
|
|
142
150
|
|
|
143
|
-
Releases:
|
|
144
|
-
|
|
151
|
+
Releases use [changesets](.changeset/README.md): add one with `npx changeset` in your pull request,
|
|
152
|
+
then merge the "Version Packages" pull request that appears. That bumps `package.json` and
|
|
153
|
+
`Cargo.toml`, and `.github/workflows/release.yml` builds the binaries and publishes to npm and
|
|
154
|
+
crates.io (trusted publishing, no tokens).
|
|
145
155
|
|
|
146
156
|
An earlier pure-TypeScript engine is in the git history at commit `e8c67ce` (npm `everystate@0.1.0`).
|
|
147
157
|
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "everystate",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "A TLA+ model checker with no Java dependency (TLC-compatible). Prebuilt native binaries, with a WebAssembly build for use from JavaScript.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"wasm/everystate.d.ts",
|
|
24
24
|
"wasm/everystate.wasm",
|
|
25
25
|
"wasm/cli.mjs",
|
|
26
|
+
"skills",
|
|
26
27
|
"README.md",
|
|
27
28
|
"LICENSE"
|
|
28
29
|
],
|
|
@@ -32,7 +33,9 @@
|
|
|
32
33
|
"conformance:examples": "node scripts/conformance.mjs",
|
|
33
34
|
"conformance:tlc": "node scripts/diff-tlc.mjs",
|
|
34
35
|
"conformance:statechart": "node scripts/statechart-corpus.mjs",
|
|
35
|
-
"bench": "node scripts/bench.mjs"
|
|
36
|
+
"bench": "node scripts/bench.mjs",
|
|
37
|
+
"changeset": "changeset",
|
|
38
|
+
"version": "changeset version && node scripts/sync-version.mjs"
|
|
36
39
|
},
|
|
37
40
|
"engines": {
|
|
38
41
|
"node": ">=20"
|
|
@@ -48,5 +51,8 @@
|
|
|
48
51
|
"repository": {
|
|
49
52
|
"type": "git",
|
|
50
53
|
"url": "git+https://github.com/mattzcarey/everystate.git"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@changesets/cli": "^3.0.3"
|
|
51
57
|
}
|
|
52
58
|
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: formal-verification
|
|
3
|
+
description: "Model-check a design with TLA+ and everystate (no Java needed): write a small model of the code, check every reachable state, and report any counterexample in the code's own words. Use for races, retries, timeouts, locks, leases, queues, state machines, distributed protocols, or when asked to \"model-check this\", \"prove this invariant\", or \"can these steps happen in a bad order?\"."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Formal verification with everystate
|
|
7
|
+
|
|
8
|
+
A test checks the cases someone thought to write. A model checker checks every order of steps in a
|
|
9
|
+
small model of the design. Use it when the question is "can these events happen in an order that
|
|
10
|
+
breaks a promise?" and the answer depends on interleavings that tests rarely reach.
|
|
11
|
+
|
|
12
|
+
It verifies the model, not the code. It does not answer "is this function correct for every input";
|
|
13
|
+
use property-based tests or a verifier that runs on the code for that.
|
|
14
|
+
|
|
15
|
+
## The tool
|
|
16
|
+
|
|
17
|
+
`everystate` is a TLA+ model checker with TLC's command line, output and exit codes, as one binary.
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npx everystate Spec.tla # reads Spec.cfg next to it
|
|
21
|
+
npx everystate -config Other.cfg Spec.tla # another configuration of the same spec
|
|
22
|
+
npx everystate -json Spec.tla # one JSON object on the last line of stdout
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
| Exit | Meaning |
|
|
26
|
+
|---|---|
|
|
27
|
+
| 0 | every reachable state checked, nothing found |
|
|
28
|
+
| 10 | an `ASSUME` is false |
|
|
29
|
+
| 11 | deadlock: a state with no next step |
|
|
30
|
+
| 12 | an invariant is violated |
|
|
31
|
+
| 13 | a liveness property is violated |
|
|
32
|
+
| 150 / 151 | the spec / the `.cfg` does not parse |
|
|
33
|
+
|
|
34
|
+
The JSON object has `status` (`ok`, `invariant`, `liveness`, `deadlock`, ...), the state counts, and
|
|
35
|
+
on a failure `name` and `trace`: the shortest list of `{action, state}` steps that reaches it.
|
|
36
|
+
|
|
37
|
+
Not supported: simulation mode, multiple workers, the PlusCal translator (write TLA+ directly, or
|
|
38
|
+
translate with the TLA+ tools first), TLAPS proofs. A model must be finite and small enough to
|
|
39
|
+
enumerate; a few million states is fine.
|
|
40
|
+
|
|
41
|
+
## How to do it
|
|
42
|
+
|
|
43
|
+
1. **Pick the promise.** One sentence the code is supposed to keep, where a break harms users: "a
|
|
44
|
+
card is charged at most once", "two workers never hold the same lease". Read the code first and
|
|
45
|
+
take the promise from it; ask the user only what you cannot see.
|
|
46
|
+
2. **Model the steps, not the code.** One variable per piece of state that matters, one action per
|
|
47
|
+
step that can interleave with others. What the model treats as one step must be one indivisible
|
|
48
|
+
step in the code: a read followed by a write is two actions unless a lock or transaction makes
|
|
49
|
+
it one. Include the failures: lost messages, timeouts, crashes, retries. Use the code's names,
|
|
50
|
+
and put a comment on each action saying which function it stands for.
|
|
51
|
+
3. **Bound it.** Constants in the `.cfg` keep the model finite (2 or 3 workers, 3 retries). Most
|
|
52
|
+
design bugs show up at these sizes.
|
|
53
|
+
4. **State the properties.** A type invariant first, since it catches modelling mistakes. Then the
|
|
54
|
+
promise as an `INVARIANT` ("never bad") or a `PROPERTY` ("eventually good", which needs fairness
|
|
55
|
+
in `Spec`).
|
|
56
|
+
5. **Run it and read the result.** On a violation, retell the trace as a short story in the code's
|
|
57
|
+
words. Then decide: is this a real bug, or did the model allow a step the code cannot take?
|
|
58
|
+
Check against the code before changing either.
|
|
59
|
+
6. **See it fail.** A check that has never failed proves nothing. Break the model the way the code
|
|
60
|
+
could be broken (remove the guard, reorder two steps) and confirm the checker reports it. Keep
|
|
61
|
+
that as a second `.cfg` or note the mutation you tried.
|
|
62
|
+
7. **Report honestly.** Say what was checked, at which bounds, under which assumptions, and what
|
|
63
|
+
the model leaves out. A pass covers the model at those bounds, not the whole system.
|
|
64
|
+
|
|
65
|
+
Do not weaken a property or the model to get a pass, and do not change product code unless asked:
|
|
66
|
+
a counterexample on the current code is a finding to report.
|
|
67
|
+
|
|
68
|
+
## Worked example
|
|
69
|
+
|
|
70
|
+
[`example/Charge.tla`](example/Charge.tla) models a client that retries a card charge when the
|
|
71
|
+
reply is lost. The promise is `ChargedAtMostOnce`. The constant `Idempotent` says whether the
|
|
72
|
+
server remembers the request key.
|
|
73
|
+
|
|
74
|
+
```sh
|
|
75
|
+
cd example
|
|
76
|
+
npx everystate Charge.tla # Idempotent = TRUE: exit 0, 29 distinct states
|
|
77
|
+
npx everystate -config ChargeBroken.cfg Charge.tla # Idempotent = FALSE: exit 12
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The broken configuration fails with this trace, which reads: the client sends, times out and sends
|
|
81
|
+
again; the server handles both requests and charges twice.
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
Error: Invariant ChargedAtMostOnce is violated.
|
|
85
|
+
State 1: <Initial predicate> charges = 0, sent = 0
|
|
86
|
+
State 2: <Send ...> sent = 1, inFlight = 1
|
|
87
|
+
State 3: <Send ...> sent = 2, inFlight = 2
|
|
88
|
+
State 4: <Handle ...> charges = 1
|
|
89
|
+
State 5: <Handle ...> charges = 2
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
(Shortened: the real output prints every variable in every state.)
|
|
93
|
+
|
|
94
|
+
The same spec checks `EventuallyConfirmed`, a liveness property: under weak fairness on sending,
|
|
95
|
+
handling and receiving, the client always learns the charge went through.
|
|
96
|
+
|
|
97
|
+
## A `.cfg` in brief
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
SPECIFICATION Spec \* or INIT Init and NEXT Next
|
|
101
|
+
CONSTANTS
|
|
102
|
+
MaxSends = 3
|
|
103
|
+
Workers = {w1, w2} \* model values
|
|
104
|
+
INVARIANTS TypeOK ChargedAtMostOnce
|
|
105
|
+
PROPERTY EventuallyConfirmed
|
|
106
|
+
CHECK_DEADLOCK FALSE \* when stopping is a legitimate end
|
|
107
|
+
SYMMETRY Perms \* Perms == Permutations(Workers); safety only
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Leaving it for the next change
|
|
111
|
+
|
|
112
|
+
If the check should outlive this task, commit the spec and `.cfg` next to the code and record with
|
|
113
|
+
them: the one command that runs it, which files and functions each action stands for, the bounds,
|
|
114
|
+
and the commit at which the model was last confirmed against the code. Whoever changes that code
|
|
115
|
+
next can then rerun the check, and can see when the model needs another look.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
---- MODULE Charge ----
|
|
2
|
+
\* A client charges a card through a server and retries when the reply is lost.
|
|
3
|
+
\* Promise: the card is charged at most once, and the client eventually learns the charge went through.
|
|
4
|
+
EXTENDS Naturals
|
|
5
|
+
|
|
6
|
+
CONSTANTS
|
|
7
|
+
Idempotent, \* TRUE: the server remembers the request key and does not charge again
|
|
8
|
+
MaxSends \* bound on how many times the client sends the request
|
|
9
|
+
|
|
10
|
+
VARIABLES
|
|
11
|
+
sent, \* how many times the client has sent the request
|
|
12
|
+
inFlight, \* requests on the wire, not yet handled by the server
|
|
13
|
+
charges, \* how many times the card was charged
|
|
14
|
+
seen, \* the server has recorded the request key
|
|
15
|
+
replies, \* replies on the wire, not yet read by the client
|
|
16
|
+
confirmed \* the client knows the charge went through
|
|
17
|
+
|
|
18
|
+
vars == <<sent, inFlight, charges, seen, replies, confirmed>>
|
|
19
|
+
|
|
20
|
+
Init ==
|
|
21
|
+
/\ sent = 0 /\ inFlight = 0 /\ charges = 0
|
|
22
|
+
/\ seen = FALSE /\ replies = 0 /\ confirmed = FALSE
|
|
23
|
+
|
|
24
|
+
\* The client sends the request: the first time, or again after a timeout.
|
|
25
|
+
Send ==
|
|
26
|
+
/\ ~confirmed /\ sent < MaxSends
|
|
27
|
+
/\ sent' = sent + 1 /\ inFlight' = inFlight + 1
|
|
28
|
+
/\ UNCHANGED <<charges, seen, replies, confirmed>>
|
|
29
|
+
|
|
30
|
+
\* The server handles one request and replies.
|
|
31
|
+
Handle ==
|
|
32
|
+
/\ inFlight > 0
|
|
33
|
+
/\ inFlight' = inFlight - 1
|
|
34
|
+
/\ charges' = IF Idempotent /\ seen THEN charges ELSE charges + 1
|
|
35
|
+
/\ seen' = TRUE
|
|
36
|
+
/\ replies' = replies + 1
|
|
37
|
+
/\ UNCHANGED <<sent, confirmed>>
|
|
38
|
+
|
|
39
|
+
\* The network loses a reply. It never loses the last one the client can still get.
|
|
40
|
+
LoseReply ==
|
|
41
|
+
/\ replies > 0 /\ (sent < MaxSends \/ inFlight + replies > 1)
|
|
42
|
+
/\ replies' = replies - 1
|
|
43
|
+
/\ UNCHANGED <<sent, inFlight, charges, seen, confirmed>>
|
|
44
|
+
|
|
45
|
+
\* The client reads a reply.
|
|
46
|
+
Receive ==
|
|
47
|
+
/\ replies > 0
|
|
48
|
+
/\ replies' = replies - 1 /\ confirmed' = TRUE
|
|
49
|
+
/\ UNCHANGED <<sent, inFlight, charges, seen>>
|
|
50
|
+
|
|
51
|
+
Next == Send \/ Handle \/ LoseReply \/ Receive
|
|
52
|
+
|
|
53
|
+
Spec == Init /\ [][Next]_vars /\ WF_vars(Send) /\ WF_vars(Handle) /\ WF_vars(Receive)
|
|
54
|
+
|
|
55
|
+
TypeOK ==
|
|
56
|
+
/\ sent \in 0..MaxSends /\ inFlight \in 0..MaxSends /\ replies \in 0..MaxSends
|
|
57
|
+
/\ charges \in 0..MaxSends /\ seen \in BOOLEAN /\ confirmed \in BOOLEAN
|
|
58
|
+
|
|
59
|
+
ChargedAtMostOnce == charges <= 1
|
|
60
|
+
EventuallyConfirmed == <>confirmed
|
|
61
|
+
====
|
package/wasm/everystate.wasm
CHANGED
|
Binary file
|