15-04

Chapter 15 · Distrust fluent specs

False alarms

  • 40 min core
  • Core track
  • Skills: counterexamples, evidence

00Recall

One card from 03-02 (why an invariant's trace stops at the bad state), one from 00-03 (states and steps in a trace), and one from 15-03 (properties that kill nothing). Today the trace comes from a reviewer, not from TLC, and your job is to find out whether TLC agrees with it.

03-02-a from Your first counterexample

Why does a TLC trace for an invariant violation stop at the bad state instead of showing a whole infinite behavior?

Show answer

Once a state breaks an invariant, nothing later can repair it, so the path to that state is all the evidence needed.

00-03-a from Your first TLC run

A TLC trace lists states numbered 1 to k. How many steps does it contain?

Show answer

k - 1. The first state is where the run starts, before any step, like the first post of a fence.

15-03-a from Mutation as measurement

A property kills no mutant. Name three reasons it might not be worth keeping.

Show answer

It may restate TypeOK, it may mention no variable (an ASSUME, not an invariant), or it may be about a variable nothing writes. A property false of the real spec is a fourth kind: a different requirement.

Skip recall Recall is never required.

01Commit

A teammate's Counter.tla adds one to a shared counter under a lock: a worker acquires the lock, reads the counter, writes the value plus one, and releases. Its cfg passes NoLostUpdate. An AI reviewer disagrees. Its review, in ai/9-false-alarm/output/MESSAGE.md, says that Release frees the lock without checking who holds it, and gives a seven-record trace in which w2 releases the lock w1 holds, both workers read 0, and the counter ends at 1 after two writes. It suggests a fix.

You write the review's trace as data, one record per step, and make TLC follow it on the real Counter.tla, with deadlock checking on.

Commit a

TLC replays the review's trace on the real Counter, one record per step. What does it report?

  1. A violation of NoLostUpdate in the last state, as the review says.
  2. A deadlock: the replay reaches a record the spec does not allow, and cannot go on.
  3. A deadlock, which means Counter has a deadlock bug the review missed.

Answer A deadlock: the replay reaches a record the spec does not allow, and cannot go on.

Commit b

The item also replays the same trace on CounterAsClaimed, a copy of the counter whose Release has no guard, as the review describes it. Why?

  1. To check the script itself.
  2. It is not needed: a stuck replay already refutes the review.
  3. To confirm that the reviewer's suggested fix works.

Answer To check the script itself.

02Lab

This lab is 15-ai9. Open chapters/15-distrust-fluent-specs/ai/9-false-alarm/README.md and follow its first four numbered instructions: write the review's trace in ClaimData.tla, then run

shell
./tla check 15-ai9

and read all three blocks. Before your first run, the teammate's cfg passes, as the review said:

Expected output 15-distrust-fluent-specs/ai/9-false-alarm/Counter
15-ai9  Counter.cfg  (Workers={w1, w2}, NoOne=NoOne)
parse ok
model check: no violation found in 17 distinct states (19 generated, depth 9)
expected outcome met: pass
coverage: all 5 actions fired
witness AllCounted reached (trace of 9 states)
witness Contended reached (trace of 2 states)

Reveal for commit a

A deadlock. On CounterAsClaimed the script runs to the end, and the last state has two writes and a count of 1, exactly as the review says: that cfg checks it, with EndsAsReviewed. On the real counter TLC follows w1's Acquire and Read, then reaches the record where w2 releases, and stops: Release(w) requires pc[w] = "written", and w2 has not even acquired. The review read lock' = NoOne in Release and missed the guard on the line above it.

Reveal for commit b

To know what the deadlock means. Run on the counter as the review describes it, your script must replay to the end and finish in the review's last state; if it does, your script and the review describe the same behavior. Only then does a stuck replay on the real spec mean "the spec does not allow this behavior" rather than "I typed the trace wrong".

03Reason

If you expected the violation, you treated a detailed trace as a checked one. It had the shape of TLC's output: numbered steps, variables, a final state that breaks the invariant. It had not been near TLC. And if you thought the stuck replay was enough on its own, ask what else gets stuck: a script with one worker's name mistyped does too. The run on CounterAsClaimed is what rules that out.

A trace is the most testable claim a reviewer can make. It says: here is a behavior; every step of it is allowed by Next; its last state breaks the property. Each part can be checked by machine, and none of it needs you to trust the reviewer, or to out-argue them.

Write something before openinghow would you settle the review without TLC? What does that cost, and where could you go wrong?

By hand: start from Init, apply each record's action, check its guard in the current state, compute the next state, and repeat. It is seven steps here, and you would probably notice w2's missing "written". On a real spec with twenty variables and a forty-step trace, you could slip, and you would be reading the same lines the reviewer misread.

The replay wrapper turns the claim into a spec TLC can check. It adds one variable, step, the index of the next record, and allows exactly one kind of step: do what the current record says, and move on.

TLA+chapters/15-distrust-fluent-specs/ai/9-false-alarm/MCClaimReal.tla
ScriptStep is defined as
    and step less than or equal to Len(Claim)
    and Do(Claim[step])
    and stepprime = step + 1

There are three outcomes, and each one means something different:

  • The script runs out. ClaimRunsOut is violated in the last state: every record was a possible step, so this sequence of actions is possible. Compare its states with the claim's, and check the property in the last one.
  • A property fails on the way. The behavior exists and breaks the property. The review is right, perhaps earlier than it said.
  • The replay gets stuck. TLC reports a deadlock: from the state the records so far produced, the next record is not a step the spec allows. The behavior does not exist.

That reading holds here because each record fixes exactly one successor: given the action and the worker, Counter's actions leave nothing to choose. If an action can choose, say a value to write, one choice can get stuck while another runs on, and TLC still reports the deadlock. Then put the claimed values in the record and check them in Do. The same goes for the claim's numbers: the records here carry only a and w, so a script that runs out shows the actions are possible, not that the variables take the values the review wrote down. The cfg for CounterAsClaimed checks the review's last state for that reason.

Nothing in the wrapper knows what the reviewer feared or what the teammate intended. TLC answers from Init, Next and the script alone, and the run on CounterAsClaimed is what keeps the script honest.

Then calibrate. Not every alarm is false. In the Core exercise an AI reviewer says a car park's count can drift, gives a trace, and is right: the replay runs, and CountMatches fails on the way. The method does not take sides. It makes the claim and the spec meet, and reports which one gave way.

Write something before openingthe suggested fix, adding /\ lock = w to Release, is harmless here. Why not apply it anyway, to be safe?

Because "harmless" is a claim too, and a fix is a change someone must review, test and maintain. A fix for a bug that is not there also hides the real lesson: the reviewer misread the spec, so its next review deserves the same check. And in specs, as in code, a guard added "to be safe" can remove behaviors you needed, which an invariant check will never tell you.

This is the trick from chapter 14 with a review instead of a log: trace validation asks whether a recorded run is a behavior of the spec, and so does this. The difference is who wrote the trace.

04In your editor

The replay wrappers are MCClaimReal.tla and MCClaimAsClaimed.tla in chapters/15-distrust-fluent-specs/ai/9-false-alarm/. You can reuse their shape for any claimed trace: a record per step, a Do that maps a record to an action, and ClaimRunsOut. Check this lesson's exercises with:

shell
./tla check 15-04

The teammate's counter, with its witnesses:

Output 15-distrust-fluent-specs/ai/9-false-alarm/Counter
15-ai9  Counter.cfg  (Workers={w1, w2}, NoOne=NoOne)
parse ok
model check: no violation found in 17 distinct states (19 generated, depth 9)
expected outcome met: pass
coverage: all 5 actions fired
witness AllCounted reached (trace of 9 states)
witness Contended reached (trace of 2 states)

05Practice

Warm-up. A reviewer says a full buffer accepts one more item. Write the claim as a script and replay it.

Core. An AI reviewer says a garage's count can drift. Replay the claim. This time, be ready for the review to be right.

AI item. 15-ai9: the false alarm from this lesson, and a two-sentence reply to the reviewer.

15-04-ex1Warm-up5 min

15-04-ex1 · Replay the claim

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

A review comment on Buffer.tla, a bounded buffer with Capacity = 2:

When the buffer is full, Put still accepts one more item, so the producer can overrun the buffer.

The claim is concrete enough to test. Write it as a script of actions and let TLC try to follow it.

  1. Run ./tla check 15-04-ex1. Both replays stop at once, because the script is empty.
  2. Fill gap g1 in ClaimData.tla: the claimed behavior from the empty buffer, as a sequence of action names, one per step. Fill the buffer, then add one more.
  3. Run the check. MCClaimAsClaimed.cfg replays your script on BufferAsClaimed.tla, a buffer with no room check: there the script must run to the end, with one item more in the buffer than Capacity. MCClaimReal.cfg replays it on the real buffer.
  4. Read the real replay's trace. How many steps did TLC manage, and which guard refused the next one?

A deadlock is the goal here: the script asked for a step the spec does not allow. Leave the MC files and BufferAsClaimed.tla as they are.

Hints: HINTS.md, under "15-04-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/15-distrust-fluent-specs/exercises/04-ex1-replay-the-claim/; open it after you have an answer of your own.

Both cfgs must meet their goals: "goal met: violation EndsAsClaimed" for MCClaimAsClaimed.cfg, and "goal met: deadlock" for MCClaimReal.cfg.

Done when: ./tla check 15-04-ex1 prints "goal met: deadlock".

./tla check 15-04-ex1

Repo chapters/15-distrust-fluent-specs/exercises/04-ex1-replay-the-claim/

Hints

Hint 1

The claim starts from an empty buffer. How many Put steps fill a buffer with Capacity = 2?

Hint 2

The script is the steps that fill the buffer, followed by the one extra step the review says is accepted.

Hint 3 (nearly the answer)

Claim is a sequence of action names. How many Put steps fill the buffer, and how many does the review say it accepts?

15-04-ex2Core8 min

15-04-ex2 · When the AI is right

Tier: core. Time: about 8 minutes.

A teammate's Garage.tla drives a car park's "FULL" sign from a count. An AI reviewer left this comment, quoted here as part of the task:

Leave(c) only checks that c is a car, not that it is inside. If the exit barrier registers the same car twice, the count drifts: c1 enters (count 1), c1 leaves (count 0), c1 "leaves" again (count -1), then c2 enters (count 0, with one car inside). The sign will read wrong for the rest of the day. Suggested fix: guard Leave(c) with c \in inside.

In 15-04 the review was wrong. Test this one the same way, and be ready for a different answer.

  1. Run ./tla check 15-04-ex2. The replay on the real garage stops on a constant with no value.
  2. Fill gap g1 in ClaimData.tla: the review's trace, one record per step, such as [a |-> "Enter", c |-> c1].
  3. Fill gap g1 in MCClaimReal.cfg: replay the claim on the real garage (ScriptSpec), give the constants values (the cars are model values, and ClaimData names them too), and check both CountMatches and ClaimRunsOut. Keep deadlock checking on.
  4. Run the check and read all three blocks. MCClaimReal.ends.cfg replays your script on the real garage without CountMatches: it must run to the end and finish where the review does, with c2 inside and the count at zero, which shows your script is the review's. MCClaimFixed.cfg replays it on GarageFixed.tla, the garage with the review's guard: there it must get stuck. Leave MCClaimReal.ends.cfg, the MCClaimFixed files and GarageFixed.tla as they are.

Hints: HINTS.md, under "15-04-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/15-distrust-fluent-specs/exercises/04-ex2-when-the-ai-is-right/; open it after you have an answer of your own.

All three cfgs must meet their goals: "goal met: violation CountMatches" for MCClaimReal.cfg, "goal met: violation EndsAsReviewed" for MCClaimReal.ends.cfg, and "goal met: deadlock" for MCClaimFixed.cfg.

Done when: ./tla check 15-04-ex2 prints "goal met: violation CountMatches".

./tla check 15-04-ex2

Repo chapters/15-distrust-fluent-specs/exercises/04-ex2-when-the-ai-is-right/

Hints

Hint 1

Copy the review's four events in order. Each record has the action's name in a and the car in c.

Hint 2

The cfg replays the claim, so it uses ScriptSpec, not Spec. ClaimData declares c1 and c2 as constants, so they need values too; model values work.

Hint 3 (nearly the answer)

The cfg has SPECIFICATION ScriptSpec, a CONSTANTS line giving Cars a set of two model values and setting c1 and c2 to model values of the same names, and two INVARIANT lines, for CountMatches and ClaimRunsOut.

06Explain it

Open NOTES.md and answer the three prompts under 15-04, 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 "15-04 check your explanation" in HINTS.md.

After you writeCheck your explanation

Good: "I wrote the review's trace as records and replayed it on the real garage: it runs to the end and CountMatches fails when the car leaves a second time, so the review is right. The same script gets stuck on the garage with the guard, which shows the guard stops exactly this behavior. It assumes my script matches the review. TLC did not check other drifts, or that the guard is enough for every behavior; that needs a full run of the fixed garage."

Weak: "The AI was right this time, so we should trust its reviews."

The weak answer generalises from one case. The point is that the replay decided, not the reviewer.

Done?