agentleFS
christofferbergjchristofferbergj/agent-skills/skills/engineering/everystate/SKILL.md

everystate

Skill1 starsChanged 41 days agoMITView on GitHub

Use to run everystate, a TLA+ model checker that needs no Java: model-check a contract or design where several devices, tabs, or writers interleave or a rule says what must eventually happen, also before the code exists; or check a recorded run of the real system against a spec with `everystate conform`. For a chart of the states, use statechart.

The file

Files
SKILL.md

Everystate

A model checker tries every order of steps in a small model of a design. It checks the model, not the code, so it adds the most where a check of the real code is weakest: several devices, tabs, or writers that interleave, a rule about what must eventually happen, and a contract whose code does not exist yet. It replaces no check of the real code. Whether one function is right for every input is a question for a property test or a verifier that runs on the code.

everystate is a TLA+ model checker in one binary, without Java. It reads ordinary .tla specs and TLC .cfg files, and it keeps TLC's output and exit codes.

Run

Work in a scratch folder outside the repo. Pin the version in every command, so that a rerun uses the same checker:

npx -y everystate@0.4.2 Spec.tla                    # reads Spec.cfg beside it
npx -y everystate@0.4.2 -config Rung.cfg Spec.tla   # another configuration of the same spec
npx -y everystate@0.4.2 -json Spec.tla              # the result as one JSON object, the last line of stdout
ExitMeaning
0Every reachable state was checked, and no rule broke.
10An ASSUME is false.
11Deadlock: a state has no next step. Set CHECK_DEADLOCK FALSE when stopping there is a correct end.
12An INVARIANT broke, or a part of a PROPERTY that the checker checks as one, such as []P.
13Another PROPERTY broke, such as an "eventually" rule or a rule about steps.
150, 151The spec, or the .cfg, does not parse.
1The command line is wrong, such as an option the checker does not know.
75, 76, 255The checker could not evaluate the spec. The message names what failed, and the code depends on where it failed and on -json.

The JSON object has status and the state counts. On a broken rule it also has trace, the {action, state} steps that reach the break. name gives the broken rule, but a broken "eventually" rule can come without it, so check one PROPERTY in each .cfg when you must know which one broke.

Three common mistakes are behind those last three codes:

  • An action leaves a variable without a next value. Name the variable in the action, or in its UNCHANGED.
  • A .cfg boolean is TRUE or FALSE. The checker reads true as a model value, and the run stops where the spec uses it as a boolean.
  • Comparing values of different types, such as 1 = "done", is an error, as in TLC. Give each variable one type in every state.

The checker holds every state in memory, so a few million distinct states is the practical limit. -workers auto searches on every core and gives the same result. For a larger model, -simulate num=10000 checks random behaviours, and a pass then covers only the behaviours it generated. npx -y everystate@0.4.2 pcal Spec.tla translates a PlusCal algorithm in the module.

Build a model you can trust

  1. Write the model from the contract. Take the steps from whatever states the promise: an API contract, a protocol, or the code. Write one variable for each piece of state that a rule is about, and one action for each step that can interleave with another. On each action, write a comment that names the endpoint, message, or function it stands for. One step in the model must be one indivisible step in the system: a read and then a write are two actions, unless a lock or a transaction makes them one. Include the failures: a lost message, a timeout, a retry, a crash. Done when every action names what it stands for.
  2. Give each assumption its own constant. An assumption is anything the contract leaves open or the system may not do, such as "the server rejects a stale write" or "a rejected client retries". Write one .cfg for each combination worth a question. These configurations are the ladder, and each one is a rung. Constants also bound the model: two or three devices and three retries show most design bugs. Done when no action has an assumption fixed inside it.
  3. State the rules. Write a type invariant first, because it catches mistakes in the model. Then write each promise as an INVARIANT for "never", or as a PROPERTY for "eventually", which needs fairness in Spec. Keep each rule as the contract states it: a rule that breaks on a rung is a finding about that rung's assumptions. Done when the type invariant is in every .cfg, every promise is an INVARIANT or a PROPERTY, and Spec has fairness for each PROPERTY.
  4. Reproduce a known finding first. When the system has a known bug, or another check has a finding, name the rung on which the model must show it, and run that rung before any other. A model that cannot show what is known to be true is wrong. Done when the named rung gives the known finding's exit code and trace, or the report says that nothing was known.
  5. Keep a control for each promise. A control is a rung on which that rule must break, with exit 12 or 13 as the table says. A rule that has never broken proves nothing. Done when every promise has a control and you have seen that rule break on it.
  6. Run every rung. Retell each trace as a short story in the system's words, and check each of its steps against the contract or the code: either the system can take the step, or the model allows a step that the system cannot take. Done when every rung has an exit code and every trace has that verdict.

Replay on the real system

A finding is model-level until the real system has replayed it. Two replays close the gap, one from each side.

A trace from the model. Drive the real system through the trace, one step for each action, as a test or a live repro. If the system does not end where the trace ends, the model is wrong until you show that it is not.

A run from the system. Record a real run, and ask whether a rung allows it:

npx -y everystate@0.4.2 conform -trace run.jsonl -config Rung.cfg Spec.tla
ExitMeaning
0The run is a behaviour of this rung.
20It is not. The report names the first line that the spec cannot match, the closest step that the spec allows, and what differs.
12The rung allows the run, and the run breaks one of the rung's invariants.
21, 22The trace is malformed, or the checker tracked too many candidate states to give a verdict.

Exit 20 says that the system and the model differ at that line. Decide against the system which of them is wrong. When a stand-in for the system made the run, such as a mock or a local fake, the line is where the stand-in and the contract differ. Exit 12 says that the real system took a path that breaks the rule on this rung. On a control, that is the finding replayed, and the report's advice to fix the model does not apply. Exit 0 covers the runs you recorded, on the variables you logged. It is a test, not a proof.

To record a run, write a trace, or pick a conform option such as -hidden, read the trace format and options.

Worked example

references/example/Sync.tla models two devices that save one shared note. CheckVersion and Retry are its assumptions, and its three .cfg files are the ladder. Run these in a copy of the folder to see each result.

Arguments after npx -y everystate@0.4.2ExitWhat it shows
Sync.tla0Both promises hold when the server checks versions and a rejected device retries.
-config NoCheck.cfg Sync.tla12The control for NoLostEdit: the second save overwrites the first.
-config NoRetry.cfg Sync.tla13The control for EventuallyAllSaved: a rejected device never saves.
conform -trace run-ok.jsonl Sync.tla0A recorded run with one rejected save and a retry is allowed.
conform -trace run-stale-accepted.jsonl Sync.tla20Line 5: the system accepted a stale save that the contract rejects.
conform -trace run-stale-accepted.jsonl -config NoCheck.cfg Sync.tla12The same run is the NoCheck rung's lost edit, replayed on the real system.

Cross-check with TLC

everystate is young: release 0.4.2 fixed missed and false invariant violations. Before a result carries a decision, whether a pass that someone will rely on or a finding that you report, check the same spec and .cfg with TLC through /statechart, and compare the verdict and the state counts with TLC's output. /statechart also draws the model as a chart for the person who decides. When the two checkers differ, trust TLC, and report the difference as a finding about the tool.

Report

Give the person who decides:

  • what the model stands for, and where its steps came from;
  • each rung with its constants and its exit code, and which rungs are controls;
  • each finding as steps in the system's words, marked model-level or replayed, with how you replayed it;
  • the bounds, the assumptions, and what the model leaves out, because a pass covers the model at those bounds and nothing more;
  • whether TLC agreed, or that you ran no cross-check.

A model that must guard the code after this task is a lasting check. /create-formal-verification-skill builds one, and only the user can start it, so name that command for them.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.