Chapter

14

Specs meet code: trace validation and contracts

Part V · Real systems

  • 6 lessons
  • 4 h 16 min of core lessons
  • Skills: acceptance, evidence

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.md first. Those two lessons use a TraceSpec, its matchers, "first divergence" and ./tla validate, which 14-01 to 14-04 teach.
  • A working ./tla doctor (see START.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 scheduler sim.py, committed logs in impl/fixtures/, and four lease servers that the course wrote in the style of agent output (provenance curated). 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:

shell
./tla check 14-01
./tla validate --log chapters/14-specs-meet-code/impl/fixtures/kv-seed3.ndjson
./tla check 14-review
./tla check --gate 14

Skills

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 14

    Hints

    Hint 1

    Start from KVTrace: Init is both bins at 0 and l = 0, and every matcher checks l < Len(Trace) and the ev field before it reads anything else, and ends with the step on l. Get the north log to pass with Accepted reached 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 left field is an output; compare it with the spec's state after the step. To see where the other log stops, check a second cfg with CONSTANT Trace <- TraceSouth and read the depth.

    Hint 3 (nearly the answer; opening it marks a pass as "passed with help")

    Pick and Move each need a guard comparing Entry.qty with the stock of the bin the units leave, an EXCEPT that updates stock (two bins for a move), and an equation between the new stock of that bin and Entry.left. Safety is a \A over Bins. Divergence is the log whose replay finds no violation and stops short, with its depth.

  • 14-gate-twin Gate twin, another domain · ./tla check --gate 14 --twin

    Hints

    Hint 1

    Start from KVTrace: Init is both levels at 0 and l = 0, and every matcher checks l < Len(Trace) and the kind field before it reads anything else, and ends with the step on l. Get the east log to pass with Accepted reached 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. free is an output; compare it with Cap minus the cars on the level after the step. To see where the other log stops, check a second cfg with CONSTANT Trace <- TraceWest.

    Hint 3 (nearly the answer; opening it marks a pass as "passed with help")

    Arrive needs a guard comparing the level's count with Cap, and Leave a guard on the level's count; Shift needs both, on src and dst respectively. Each compares Entry.free with Cap minus the new count. Safety is a \A over Levels. Divergence is 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.