12-03

Chapter 12 · Taming the state space, choosing your evidence

Symmetry and its traps

  • 45 min core
  • Core track
  • Skills: evidence, abstraction

00Recall

One card from 12-02 on what a constraint and a view cost, one from chapter 10 on the definition of weak fairness, and one from chapter 09 on where a property must hold on a lasso. All three meet in the second half of this lesson, where a reduction that is perfectly sound for invariants quietly breaks a liveness check.

12-02-a from Predict the growth curve

What is the difference between CONSTRAINT and VIEW in what they cost you?

Show answer

A constraint cuts behaviors: TLC stops exploring from states that break it, so violations further on are never found. A view merges states that agree on an expression; it loses nothing if the dropped variables cannot affect any action or property.

10-02-a from Weak fairness from a starvation trace

Write WF_v(A) in terms of ENABLED, <<A>>_v, [] and <>, and say it in words.

Show answer

<>[](ENABLED <<A>>_v) => []<><<A>>_v. If an A step that changes v is possible at every moment from some point on, such steps happen infinitely often.

09-02-a from Operators on lassos

On a lasso (a stem, then a loop), where must P hold for []<>P, and where for <>[]P?

Show answer

[]<>P: in at least one state of the loop. <>[]P: in every state of the loop. The stem does not matter to either.

Skip recall Recall is never required.

01Commit

specs/TaskBoard.tla is the team's board, chapter 12's copy: three workers, three tasks, each worker holding at most one task at a time. No action, constant or property names a particular worker. Checked with no reduction, TaskBoard.cfg finds no violation in 86 distinct states.

specs/MCTaskBoard.cfg checks the same model with SYMMETRY WorkerSymmetry, where WorkerSymmetry == Permutations(Workers): any reordering of the three workers.

Commit a

How many distinct states will TLC report for MCTaskBoard.cfg, the same board with the workers treated as interchangeable?

Your answer is a whole number.

Answer 27

specs/Relay.tla has two runners and a baton. The holder may finish the job; the other may grab the baton instead, and each runner is weakly fair. Relay.cfg checks EventuallyFinished and reports the endless relay as a lasso. MCRelay.cfg runs the same check with SYMMETRY RunnerSymmetry. The runners really are interchangeable.

Commit b

Relay.cfg reports EventuallyFinished violated by an endless relay of two weakly fair runners. MCRelay.cfg runs the same check with a symmetry set over the runners. What does ./tla report for MCRelay.cfg?

  1. The same violation: the runners are interchangeable, so the reduction is sound.
  2. No violation, with a warning from TLC.
  3. A deadlock: once the two unfinished states are one state, a grab leads nowhere new.

Answer No violation, with a warning from TLC.

02Lab

shell
./tla check chapters/12-state-space-and-evidence/specs/MCTaskBoard.cfg
./tla check chapters/12-state-space-and-evidence/specs/Relay.cfg
./tla check chapters/12-state-space-and-evidence/specs/MCRelay.cfg
Expected output 12-state-space-and-evidence/specs/MCTaskBoard
12-state-space-and-evidence/specs/MCTaskBoard  MCTaskBoard.cfg  (Workers={w1, w2, w3}, Tasks={t1, t2, t3}, Nobody=Nobody)
parse ok
model check: no violation found in 27 distinct states (145 generated, depth 7)
expected outcome met: pass
coverage: all 4 actions fired
witness AllBusy reached (trace of 4 states)
Expected output 12-state-space-and-evidence/specs/MCRelay
12-state-space-and-evidence/specs/MCRelay  MCRelay.cfg  (Runners={r1, r2})
parse ok
model check: no violation found in 2 distinct states (5 generated, depth 2)
TLC warning: Declaring symmetry during liveness checking is dangerous. It might cause TLC to miss violations of the stated liveness properties. Please check liveness without symmetry defined.
expected outcome met: pass
coverage: all 3 actions fired
witness Finished reached (trace of 2 states)

Reveal for commit a

TLC reports 27 distinct states, down from 86. With a fourth worker the payoff grows: 152 distinct states without symmetry, 27 with it.

Reveal for commit b

No violation, after TLC's warning that declaring symmetry during liveness checking is dangerous. Without symmetry, Relay.cfg finds a lasso of 2 states: the baton passes back and forth for ever. With symmetry, the same behavior is gone from what TLC searched.

03Reason

If you divided by six, you had the right picture of what symmetry does and the wrong picture of how much. Let us count.

A board state says, for each task, whether it is to do, in progress or done, and who holds each task in progress. Without symmetry, TLC counts every assignment of holders. Pick the tasks in progress, then hand them to distinct workers.

With k tasks in progress there are 3-choose-k ways to pick them, 2 to the power 3 − k ways to set the others to "to do" or "done", and 3 × 2 × … (k factors) ways to give them owners. For k = 0, 1, 2, 3 that is 8, 36, 36 and 6: total 86. With symmetry, the owners' names stop mattering. Since each worker holds at most one task and there are as many workers as tasks, every choice of statuses is one state: 3 × 3 × 3 = 27. Of those 27 orbits, only 7 have six members: the ones with two or three tasks in progress. A board with nothing in progress has no names to swap at all.

That definition is also the warning label. SYMMETRY is a claim you make about the spec. TLC does not check it; it believes you, and explores one state per orbit.

specs/TaskBoardRoles.tla adds roles: the task Release must never be held by a junior, and Juniors = {w3}. The copy has a bug: Claim forgets the rule. TaskBoardRoles.cfg finds it in a trace of 2 states. A teammate copies Permutations(Workers) from MCTaskBoard into MCTaskBoardRoles.

Write something before openingwhy did TLC miss a bug one step from the start?

You told it that swapping w3 with w1 changes nothing. So "the junior holds Release" and "a senior holds Release" are one orbit, and TLC keeps and checks whichever of them it meets first; the pass tells you it met a senior's claim first. Go back to the order of 12-01's loop: TLC checks the invariants only in a state whose fingerprint is new. With a symmetry set, the fingerprint belongs to the whole orbit, so when the junior's claim arrives, its fingerprint is already known and TLC drops it without checking anything. The symmetry broke the definition above: ReleaseBySenior names Juniors, so swapping a junior with a senior changes its value. Permuting only the seniors, Permutations(Workers \ Juniors), is sound here, and the bug comes back.

Write something before openingyou are handed a spec and a SYMMETRY line. Where do you look to decide whether the line is sound?

Everywhere a single element of the set can be named. In the cfg: a constant assigned one particular model value, or a subset such as Juniors = {w3}. In the spec: an action whose guard or effect treats one element differently, an ordering between elements, an initial state that is not the same for all of them. In the properties and witnesses: any that mention one element by name. If every mention is "for all workers" or "some worker", each permutation maps the spec to itself. One named element is enough to break that, and TLC will not tell you.

The second trap is subtler, because it catches a symmetry that is perfectly sound for invariants.

Here is how it vanished in Relay. Without symmetry, the lasso is: one runner holds the baton, the other grabs it, the first grabs it back, for ever. Each runner takes a step again and again, so weak fairness is satisfied, and the job is never done.

Write something before openingwith symmetry, the two unfinished states are one state. Draw the reduced graph. Which loop is left, and is it fair?

Call the kept state A: runner A holds the baton. From A, the only relay step is the other runner grabbing it, and that leads to a state in A's orbit, so to A again. The reduced loop is "the other runner grabs, for ever". Runner A could finish in every state of that loop and never moves, so weak fairness on runner A rules the loop out. No fair loop avoids finishing, and TLC reports no violation. The real loop needed both runners to take turns, and the reduced graph has no path that does.

So the rule from 12-01 earns its keep: symmetry in the safety cfg, never in the liveness cfg. By the course's convention, a cfg with both SYMMETRY and PROPERTY carries a \* @lint-allow: symmetry-liveness reason=… header line that says why. Nothing enforces that convention yet, so check your liveness cfgs yourself. MCRelay.cfg carries the line because it is wrong on purpose.

Symmetry is a one-line change that makes a check faster, which makes it an easy suggestion for anyone, assistant or teammate, to make. This chapter's AI item is exactly that.

04In your editor

Check this lesson's exercises with:

shell
./tla check 12-03

For each symmetry set you write, say in a comment why every constant, action and property treats the permuted elements alike.

05Practice

Warm-up. The board with roles. Write a symmetry set that keeps the known bug visible, on two teams with different juniors.

Core. A failover spec's liveness cfg was copied from its safety cfg, SYMMETRY line and all. Decide whether the line belongs, write the client's requirement, and read the lasso.

12-03-ex1Warm-up5 min

12-03-ex1 · Not every worker is alike

Tier: warm-up. Time: about 5 minutes.

TaskBoardRoles.tla is the board with roles: the task Release must never be held by a junior. The copy has a known bug, marked in Claim, and the team wants the bug visible in every check until it is fixed. MCTaskBoardRoles.tla holds the symmetry set the cfgs use, copied from the plain board.

  1. Run ./tla check 12-03-ex1. Two cfgs run. The course's grader, with a different junior and a different Release, finds the bug. Your cfg does not. Why not?
  2. Fill gap g1 in MCTaskBoardRoles.tla: a symmetry set that permutes only workers that no constant, action or property tells apart.
  3. Run again. The bug must show in both cfgs.

Do not fix the bug in Claim. The exercise is about the check, not the board.

A symmetry set of one-element permutations would also keep the bug visible, and buy nothing. Aim for the largest set that is still sound.

Hints: HINTS.md, under "12-03-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/12-state-space-and-evidence/exercises/03-ex1-not-every-worker-is-alike/.

Both cfgs must meet their goals: "goal met: violation ReleaseBySenior" for MCTaskBoardRoles.cfg and for MCTaskBoardRoles.grader.cfg.

Done when: ./tla check 12-03-ex1 prints "goal met: violation ReleaseBySenior".

./tla check 12-03-ex1

Repo chapters/12-state-space-and-evidence/exercises/03-ex1-not-every-worker-is-alike/

Hints

Hint 1

Read the constants in the cfg. Which of them name a particular worker or task? Swapping that worker with another would change what the invariant says.

Hint 2

Juniors singles out some workers. The workers that no constant singles out can still be swapped among themselves.

Hint 3 (nearly the answer)

Permutations of the set of workers with the juniors removed. Set difference is \.

12-03-ex2Core8 min

12-03-ex2 · The loop that folds

Tier: core. Time: about 8 minutes.

Failover.tla models a client's write waiting for a leader among three replicas. The leader may apply it; any other replica may take over the lead first. Every replica is weakly fair. A teammate wrote the liveness cfg by copying the safety cfg, SYMMETRY line and all.

  1. Run ./tla check 12-03-ex2. Three cfgs run, and all pass, because WriteLands is <>TRUE for now.
  2. Fill gap g1 in Failover.tla: the client's requirement, "the write is applied, sooner or later".
  3. Run again, and read TLC's warning on MCFailover.live.cfg. Then decide about gap g1 in MCFailover.live.cfg: does the SYMMETRY line belong in a liveness check?
  4. Run again and read the lasso. Which replicas take part in the loop?

The course's Failover.grader.cfg checks your WriteLands on a repaired design in which no takeover happens, where it must pass. A property that cannot be satisfied fails there. Leave the grader and MCFailover.cfg as they are.

Hints: HINTS.md, under "12-03-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/12-state-space-and-evidence/exercises/03-ex2-the-loop-that-folds/.

All three cfgs must meet their goals: "goal met: property WriteLands" for MCFailover.live.cfg, and "goal met: pass" for the other two.

Done when: ./tla check 12-03-ex2 prints "goal met: property WriteLands".

./tla check 12-03-ex2

Repo chapters/12-state-space-and-evidence/exercises/03-ex2-the-loop-that-folds/

Hints

Hint 1

The requirement is a liveness property: something good happens eventually. It mentions only written.

Hint 2

Read TLC's warning about symmetry and liveness. Then remember 12-03: the reduced graph may not contain the loop the real graph has.

Hint 3 (nearly the answer)

WriteLands is "eventually" applied to one variable. The SYMMETRY line goes from the liveness cfg; replace it with a comment saying why.

12-mxMixed15 min · mastery path

12-mx · Charged twice

Tier: mixed. Time: about 15 minutes.

Off the core clock: this is on the mastery path.

A payment provider calls the shop's webhook about each payment, and calls again whenever it is unsure the first call arrived. Two handler workers take calls. A worker checks that the payment is not done yet, then, in a separate step, charges the order and marks the payment done. The shop's rule, ChargedOnce: no order is charged twice.

  1. Run ./tla check 12-mx and read the trace. Which step happens between the first check and the first charge?
  2. Fill gap g1 in Webhook.tla: one guard in Check that makes the rule hold, without merging Check and Charge into one step.
  3. Run again. The check must pass with every action firing and both witnesses reached: AllCharged, so a guard that stops all checking does not count, and BothBusy, so a guard that lets only one worker work at a time does not count either.

Hints: HINTS.md, under "12-mx hint 1" to "hint 3". The solution is in solutions/chapters/12-state-space-and-evidence/exercises/mx-charged-twice/.

Done when: ./tla check 12-mx prints "goal met: pass".

./tla check 12-mx

Repo chapters/12-state-space-and-evidence/exercises/mx-charged-twice/

Hints

Hint 1

Read the trace: two workers check the same payment. What lets the second one check it, when the first is already about to charge it?

Hint 2

The check and the charge are separate steps, and a repeated call keeps the payment available. The guard must look at what the other workers hold, not only at done.

Hint 3 (nearly the answer)

Add a guard that quantifies over Workers: no worker already holds p.

06Explain it

Open NOTES.md and answer the three prompts under 12-03, about the Core exercise.

output
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:

Compare your answers with "12-03 check your explanation" in HINTS.md.

After you writeCheck your explanation

A good answer: "WriteLands == <>written, checked without symmetry, fails with a lasso in which replicas take over from one another for ever, each acting again and again, so weak fairness is satisfied. It relies on takeovers being always possible while the write waits; a design that forbids them while the leader is alive passes, as the grader shows. The safety cfg keeps its symmetry, but no symmetric check here says anything about liveness, and the model has no terms or timers."

A weak answer: "TLC said symmetry was dangerous, so I removed it." It obeys the warning without saying why the reduced graph lost the loop.

Done?