Chapter
14
Specs meet code: trace validation and contracts
Part V · Real systems
Lessons
About
Suppose TLC found no violation in your spec's model, at the sizes you chose. Does your code do only what the spec allows? Now picture an agent handing you three implementations of the same lease server. Which one is right, and how would you know without reading all three?
This chapter checks real code against a real spec, one run at a time: the code writes a log, a TraceSpec pins the spec's behaviors to it, and TLC either explains every record or names the first one it cannot. Then the spec becomes a contract that an implementer, human or agent, cannot quietly weaken, as long as the owner protects the spec, the properties and the TraceSpec.
Before you start
- Chapter 03 (witnesses and coverage), chapter 11 (stuttering steps of an abstract spec), chapter 12 (what a check covers) and chapter 13 (leases without wall clocks).
- On the essentials track, which takes only 14-05 and 14-06: read the recaps of 14-01 to 14-04 in
CONCEPTS.mdfirst. Those two lessons use a TraceSpec, its matchers, "first divergence" and./tla validate, which 14-01 to 14-04 teach. - A working
./tla doctor(seeSTART.md). Python is optional: every log the lessons use is committed. - Time: the chapter page shows the estimate, generated from the lessons.
What else is here
specs/:KVStore,KVTrace,LeaseKV,LeaseKVProps,LeaseKVTrace,CONTRACT.md, and the variants the lessons run.impl/: the store, the seeded schedulersim.py, committed logs inimpl/fixtures/, and four lease servers that the course wrote in the style of agent output (provenancecurated). Treat those as untrusted, and run them only inside the dev container.review/(14-review): a teammate's checkout replay that never reads the last line of a log.ai/1-overclaimed-validation/(14-ai1): an AI summary that turns three accepted replays of a fourth lease server into "conforms to the contract".gate/(14-gate): a TraceSpec for a warehouse's bin log, and which of two shifts diverges;gate/twin/(14-gate-twin): a garage controller's log.
Run it
From the repository root:
./tla check 14-01
./tla validate --log chapters/14-specs-meet-code/impl/fixtures/kv-seed3.ndjson
./tla check 14-review
./tla check --gate 14Skills
acceptance (criteria an implementer cannot game), evidence (what a piece of evidence covers).
Elsewhere
- Web version: https://indicogni.com/learn/tla-plus/14-specs-meet-code/
- Recaps:
CONCEPTS.md. Hints and explanation checks:HINTS.md. Predictions without answers:PREDICT.md. - Solutions:
solutions/chapters/14-specs-meet-code/, including the gate references and their mutants. - Further reading: Cirstea, Kuppe, Loillier and Merz, Validating Traces of Distributed Programs Against TLA+ Specifications (2024). The specs, implementations and logs here are the course's own.
Beyond the lessons
- 14-review Review a teammate’s spec ·
chapters/14-specs-meet-code/review/·./tla check 14-review - 14-ai1 AI item ·
chapters/14-specs-meet-code/ai/1-overclaimed-validation/·./tla check 14-ai1 - 14-gate Gate ·
./tla check --gate 14Hints
Hint 1
Start from
KVTrace:Initis both bins at 0 andl = 0, and every matcher checksl < Len(Trace)and theevfield before it reads anything else, and ends with the step onl. Get the north log to pass withAcceptedreached before you think about the south log.Hint 2
The contract is in the README's middle paragraph: a pick or a move may take only what the bin holds. That is a guard on the state before the step. The
leftfield is an output; compare it with the spec's state after the step. To see where the other log stops, check a second cfg withCONSTANT Trace <- TraceSouthand read the depth.Hint 3 (nearly the answer; opening it marks a pass as "passed with help")
PickandMoveeach need a guard comparingEntry.qtywith the stock of the bin the units leave, anEXCEPTthat updatesstock(two bins for a move), and an equation between the new stock of that bin andEntry.left.Safetyis a\AoverBins.Divergenceis the log whose replay finds no violation and stops short, with its depth. - 14-gate-twin Gate twin, another domain ·
./tla check --gate 14 --twinHints
Hint 1
Start from
KVTrace:Initis both levels at 0 andl = 0, and every matcher checksl < Len(Trace)and thekindfield before it reads anything else, and ends with the step onl. Get the east log to pass withAcceptedreached first.Hint 2
The contract: a car may be parked only where a space is free, and may leave only where a car is parked. A shift does both, on two different levels.
freeis an output; compare it withCapminus the cars on the level after the step. To see where the other log stops, check a second cfg withCONSTANT Trace <- TraceWest.Hint 3 (nearly the answer; opening it marks a pass as "passed with help")
Arriveneeds a guard comparing the level's count withCap, andLeavea guard on the level's count;Shiftneeds both, onsrcanddstrespectively. Each comparesEntry.freewithCapminus the new count.Safetyis a\AoverLevels.Divergenceis the log whose replay stops short, with its depth. - CP4 Checkpoint CP4 follows this chapter: mixed items from every chapter so far, with no hints.