16-05
Chapter 16 · Specifying agentic systems
A multi-agent task board
00Recall
One card from 16-03 on what an at-most-once pass covers, one from 13-04 on leases without clocks, and one from 11-03 on refinement as substitution plus implication. The TaskBoard from chapters 06, 08, 10 and 12 (and 11's Stretch) is back for the last time, with agents for workers.
16-03-b from At-most-once side effects
TLC finds no violation of AtMostOnce in ToolEffects.cfg. An AI summary says "the agent charges exactly once". Is it accurate?
Show answer
No. The run shows at most one charge, in a model with one intent, two attempts and a service that deduplicates. It says nothing about exactly once, and the result depends on the service's deduplication, which is an assumption.
13-04-a from Leases without wall clocks
A lease client starts its count Margin ticks short, and its clock can lose up to MaxDrift ticks per lease. What must hold for two clients never to act at once, and why?
Show answer
Margin >= MaxDrift. When the server's count reaches zero, a client that lost d ticks still has d - Margin left on its own count, so it has stopped exactly when Margin covers the worst drift.
11-03-a from Refinement mappings
The implementation has no variable that matches an abstract variable. Is refinement still possible?
Show answer
Yes. A refinement mapping gives each abstract variable an expression over the implementation's variables, such as "the current message while the receiver has not delivered it, else empty". It is substitution, not renaming.
Skip recall Recall is never required.
01Commit
specs/TaskBoard.tla has worker agents (Workers = {w1, w2}) taking tasks (Tasks = {t1, t2}) from a shared board under leases. A lease can expire while its holder is still working: the agent paused for a long model call, or lost the network, and did not notice. Unlike chapter 13's countdowns, time is not modelled at all: Expire(t) may happen at any step while t is leased. That covers every timing, drift included. The store keeps one output per task, and the owner's requirement is that no worker's output replaces another's:
OneWriter is defined as for all t in Tasks : writes[t] less than or equal to 1A teammate's copy, specs/ZombieBoard.tla, has leases and nothing else: the store accepts every write it receives; a later write replaces the earlier output.
Commit a
ZombieBoard.cfg checks OneWriter. What does TLC report?
- A violation, with only two workers and one takeover.
- No violation: a worker writes only while it holds a lease, and a lease is held by one worker.
- No violation in this model, but there would be one with three workers.
Answer A violation, with only two workers and one takeover.
- If you picked B
- A lease is held by one worker as far as the board knows. The worker whose lease expired still believes it holds it, and nothing stops it from writing.
- If you picked C
- Two workers are enough: one whose lease expires, and one that takes the task over. Ask what each of them does next.
The fixed board numbers every lease. The number, a fencing token, grows with each new lease on a task, and the store keeps a write only if its token is the task's latest. The workers are interchangeable, so TaskBoard.symmetry.cfg adds SYMMETRY WorkerSymmetry: TLC then counts states that differ only by swapping the names w1 and w2 once. Without it, the board has 197 distinct states.
Commit b
With SYMMETRY over two workers, how many distinct states will TLC report?
Your answer is a whole number.
Answer 99
- Exactly right
- Right. Two workers have two orderings, and almost every state has a twin under a swap of their names.
- Within 10 %
- Within 10 %. Every state but one has exactly one twin under the swap; only the initial state, with both workers idle and nothing written, is its own twin.
- Under by more than half
- Under by more than half. Two workers have only two orderings, so symmetry can at best halve the count.
- More than double
- More than double. Symmetry never adds states: it only merges states that differ by a swap of worker names.
- Anything else
- Close to the full count? Then you ignored the symmetry. Think in pairs: a state where
w1holdst1andw2is idle has a twin wherew2holdst1andw1is idle, and TLC keeps one of each pair. States that look the same after a swap have no twin to drop, so the answer is a little over half.
02Lab
./tla check chapters/16-specifying-agentic-systems/specs/ZombieBoard.cfg
./tla check chapters/16-specifying-agentic-systems/specs/TaskBoard.cfg
./tla check chapters/16-specifying-agentic-systems/specs/TaskBoard.symmetry.cfg16-specifying-agentic-systems/specs/ZombieBoard16-specifying-agentic-systems/specs/ZombieBoard ZombieBoard.cfg (Workers={w1, w2}, Tasks={t1, t2}, MaxLeases=2)
parse ok
model check: invariant OneWriter violated; trace has 6 states (5 steps); 61 distinct states
expected outcome met: violation OneWriter
trace (changed variables only; ./tla trace --full shows all):
1 Init status = (t1 :> "open" @@ t2 :> "open"), token = (t1 :> 0 @@ t2 :> 0), lease = (w1 :> <<>> @@ w2 :> <<>>), output = {}, writes = (t1 :> 0 @@ t2 :> 0)
2 Claim status: (t1 :> "open" @@ t2 :> "open") -> (t1 :> "leased" @@ t2 :> "open"), token: (t1 :> 0 @@ t2 :> 0) -> (t1 :> 1 @@ t2 :> 0), lease: (w1 :> <<>> @@ w2 :> <<>>) -> (w1 :> <<t1, 1>> @@ w2 :> <<>>)
3 Expire status: (t1 :> "leased" @@ t2 :> "open") -> (t1 :> "open" @@ t2 :> "open")
4 Claim status: (t1 :> "open" @@ t2 :> "open") -> (t1 :> "leased" @@ t2 :> "open"), token: (t1 :> 1 @@ t2 :> 0) -> (t1 :> 2 @@ t2 :> 0), lease: (w1 :> <<t1, 1>> @@ w2 :> <<>>) -> (w1 :> <<t1, 1>> @@ w2 :> <<t1, 2>>)
5 Complete status: (t1 :> "leased" @@ t2 :> "open") -> (t1 :> "done" @@ t2 :> "open"), lease: (w1 :> <<t1, 1>> @@ w2 :> <<t1, 2>>) -> (w1 :> <<>> @@ w2 :> <<t1, 2>>), output: {} -> {<<t1, w1>>}, writes: (t1 :> 0 @@ t2 :> 0) -> (t1 :> 1 @@ t2 :> 0)
6 Complete lease: (w1 :> <<>> @@ w2 :> <<t1, 2>>) -> (w1 :> <<>> @@ w2 :> <<>>), output: {<<t1, w1>>} -> {<<t1, w2>>}, writes: (t1 :> 1 @@ t2 :> 0) -> (t1 :> 2 @@ t2 :> 0)Reveal for commit a
A violation, with a trace of 6 states. One worker's lease expires, another worker takes the task over, and the store accepts a write from each: writes for that task reaches 2.
Reveal for commit b
99 distinct states, against 197 without symmetry. The same witnesses are reached and the same invariants hold.
03Reason
If you picked "a lease is held by one worker", you took the board's point of view. The board knows exactly one holder at a time. The expired worker does not know that it is no longer the holder, and it is the one with the pen. A worker that keeps acting on a lease it has lost is called a zombie. Nothing about it is malicious. It woke up from a long pause and carried on.
Write something before openingthe zombie could check its lease before writing. Why does that not fix it?
Because the check and the write are two steps, and the lease can expire between them. That gap is the same one as in chapter 06's lost update: a decision made on a value that may be stale by the time it is acted on. Any fix that lives only in the worker has the gap. The fix has to live where the write lands.
That is what the fencing token does. The worker does not need to know that it is a zombie; the store knows, because once the task has been leased again, the token on the write is smaller than the task's latest. In the spec the whole fix is one comparison, in the operator Accepts(t, k) that decides whether the store keeps a write for task t carrying token k; the Warm-up asks you to write it. A zombie whose task was never leased again still holds the latest token, and its write is kept. That is harmless: nobody else wrote.
The design principle is worth more than the comparison: the resource enforces the lease, not the lease holder. A worker's belief about its lease is a cache, and caches go stale.
In real systems the token goes by other names: a version number on a conditional write, a generation number on an object, an entity tag sent back with "only if it still matches". The pattern is always the same. The write carries the number the writer believed in, and the store refuses it if the world has moved on. A store that cannot make that comparison as part of the write cannot fence, and no amount of care in the workers makes up for it.
Now step back from the details. A user of the board does not care about leases or tokens. To them it is a queue: a fixed set of tasks; each finishes at most once, keeping one worker's output. specs/Queue.tla is that queue, with two variables, pending and done. The board refines the queue when every behavior of the board, seen through a mapping, is a behavior of the queue:
Q is defined as INSTANCE Queue WITH pending <- {t in Tasks : status[t] not equal to "done"},
done <- output
Refines is defined as Q!SpecTaskBoard.cfg checks PROPERTY Refines along with OneWriter, and TLC finds no violation. Claims, expiries and refused writes change nothing the queue can see, so they are stuttering steps for it; a write that finishes a task is the queue's Finish.
Write something before openingExpire reopens a task on the board. Why is that a stuttering step for the queue, and not a step that puts the task back?
The mapping sends pending to every task whose status is not "done". A leased task and an open task are both not done, so the task is pending before the expiry and pending after it, and output does not change. The queue sees nothing happen. It never knew the task had been taken, so it cannot see it given back. Try the other mapping, pending as the open tasks only: then Claim removes a task from pending without adding anything to done, and no step of Queue does that.
04In your editor
The board, the zombie board and the queue are in chapters/16-specifying-agentic-systems/specs/. Check this lesson's specs and exercises with:
./tla check 16-05The fenced board, with the refinement checked, every action firing and both witnesses reached:
16-specifying-agentic-systems/specs/TaskBoard16-specifying-agentic-systems/specs/TaskBoard TaskBoard.cfg (Workers={w1, w2}, Tasks={t1, t2}, MaxLeases=2)
parse ok
model check: no violation found in 197 distinct states (409 generated, depth 11)
expected outcome met: pass
coverage: all 4 actions fired
witness FinishedPastZombie reached (trace of 5 states)
witness AllDone reached (trace of 5 states)When several agents share anything, a file, a branch, a ticket or a budget, ask what each agent believes it holds, and what checks that belief at the moment of the write. If the answer is "the agent checks before writing", you have found a gap like the zombie's. A shared budget is the same problem in a different coat: two agents read what is left, both decide it is enough, and both spend. The Stretch exercise asks you to reserve a unit before you spend it.
05Practice
Warm-up. The zombie board, with the store's check left open. Fence the write.
Core. One step past the lesson: a board on which a requester can cancel a task nobody has started, and a queue that can withdraw one. Write the mapping, and the cfg that checks it.
Stretch. Two agents sharing one budget of model calls, from a blank skeleton, including the cfg.
16-05-ex1Warm-up5 min
16-05-ex1 · Fence the write
Tier: warm-up. Time: about 5 minutes.
The lesson's task board, with the store's check left open. The starter's store accepts every write, and a later write replaces the earlier output. Two invariants check it: OneWriter (the store accepts one write per task) and KeptIsLatest (the write it kept carries the task's latest token). Do this one before you open 16-05-ex2, whose board is already fenced.
- Run
./tla check 16-05-ex1and read the trace. Which write did the store keep for the task, and how many leases had the task had by then? - Fill gap g1 in
TaskBoard.tla: when the store keeps a write for tasktthat carries tokenk. - Run the check again. It must pass, with every action firing and every witness reached, including a task finished by its newer lease while the old holder still thinks it holds one, and a task finished on a lease that was never replaced.
Hints: HINTS.md, under "16-05-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/16-specifying-agentic-systems/exercises/05-ex1-fence-the-write/; open it after you have a passing answer of your own.
If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.
Done when: ./tla check 16-05-ex1 prints "goal met: pass".
./tla check 16-05-ex1
Repo chapters/16-specifying-agentic-systems/exercises/05-ex1-fence-the-write/
Hints
Hint 1
In the trace, the task has had two leases when the store keeps a write. Compare the token on the kept write with token for that task at the moment it lands.
Hint 2
The store knows the task's latest token. A write whose token is older came from a lease the board has already replaced.
Hint 3 (nearly the answer)
Accepts compares k with a board variable, as it is when the write lands. A comparison with a constant fails a course check: no constant can tell a first lease that was replaced from one that was not.
16-05-ex2Core8 min
16-05-ex2 · Map the board
Tier: core. Time: about 8 minutes.
One step past the lesson. The fenced board in TaskBoard.tla has one new action: a requester may Cancel a task that nobody has ever leased. Its users now see a WorkQueue (WorkQueue.tla), with three variables: tasks still to do, the pairs << task, worker >> for finished tasks, and the tasks withdrawn unfinished. The mapping is blank, and so is most of the cfg.
- Run
./tla check 16-05-ex2. SANY stops: the instance ofWorkQueueneedsQTodo,QFinishedandQWithdrawn, and they do not exist yet. - Before you write anything, try the lesson's mapping in your head: the queue's tasks to do are the tasks whose status is not
"done". Which board step would that mapping turn into a stepWorkQueuecannot take? - Fill gap g1 in
TaskBoard.tla:QTodo,QFinishedandQWithdrawn, each in terms of the board's variables. - Fill gap g1 in
TaskBoard.cfg: the spec,Workers = {w1, w2}andTasks = {t1, t2}as model values,MaxLeases = 2,TypeOK,OneWriter, and the refinement. - The check also runs
TaskBoard.grader.cfg, the course's check of your mapping. It checksOneWriterand the refinement whatever your cfg says, and its witness needs the mapped queue to settle with one task withdrawn and one finished. Leave that file as it is.
A refinement violation prints property ? instead of property Refines: TLC names the place in WorkQueue.tla that failed, not the operator. Read the trace; it is the same check.
Hints: HINTS.md, under "16-05-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/16-specifying-agentic-systems/exercises/05-ex2-map-the-board/; open it after you have a passing answer of your own.
Both cfgs must meet their goals: "goal met: pass" for TaskBoard.cfg and for TaskBoard.grader.cfg.
Done when: ./tla check 16-05-ex2 prints "goal met: pass".
./tla check 16-05-ex2
Repo chapters/16-specifying-agentic-systems/exercises/05-ex2-map-the-board/
Hints
Hint 1
The queue has tasks to do, pairs << task, worker >>, and withdrawn tasks. Which board statuses belong in each? Try the lesson's "not done" for QTodo and follow a Cancel step through it.
Hint 2
A task is still to do in the queue's eyes whether it is open or leased. A cancelled task is neither to do nor finished. The store's output already has the shape of the finished pairs.
Hint 3 (nearly the answer)
QTodo covers two of the board's statuses, QFinished needs no new expression, and QWithdrawn covers the status the lesson's board did not have. The cfg names Spec, the three constants, TypeOK, OneWriter and PROPERTY Refines.
16-05-ex3Stretch40 min · mastery path
16-05-ex3 · Shared budget
Tier: stretch. Time: about 40 minutes.
Worker agents share one budget of model calls: Budget = 3 for the whole team. The obvious design checks that some budget is left, then makes the call in a later step. With two workers, both can see one unit left and both can spend it. Your design reserves before it spends.
The requirement:
- An idle worker may reserve one unit, but only if a unit is neither spent nor already reserved by someone.
- A worker with a reservation makes its model call, which spends the reservation.
- A worker whose task is cancelled hands its reservation back unspent.
- When the whole budget is spent and nobody holds a unit, nothing else happens.
NeverOverspent: the team never spends more than its budget, and never promises more than is left.
SharedBudget.tla names the variables, the actions and two witnesses; every body is a placeholder. You write the rest, including the cfg.
- Run
./tla check 16-05-ex3. Read the starter error. - Fill gaps g1 to g7 in
SharedBudget.tla, then gap g1 inSharedBudget.cfg: the spec,Workers = {w1, w2}as model values,Budget = 3, and every invariant worth checking. - Run the check until it passes with full coverage and both witnesses. The check also runs two course cfgs:
MCSharedBudget.cfgchecks your design against the course's own statement ofNeverOverspent, andMCSharedBudget.overpromise.cfgruns yourNeverOverspenton a design that never overspends but promises the last unit twice, where it must be violated. Leave theMCSharedBudgetfiles as they are. - Break it on purpose: drop the reservation from the guard of
Reserveand confirm TLC catches it. Then addSYMMETRYover the workers in a second cfg, and say inNOTES.mdwhy that is sound here and what it would hide ifw1had priority.
Hints: HINTS.md, under "16-05-ex3 hint 1" to "hint 3". The solution is in solutions/chapters/16-specifying-agentic-systems/exercises/05-ex3-shared-budget/; open it after you have a passing answer of your own.
If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.
All three cfgs must meet their goals: "goal met: pass" for SharedBudget.cfg and MCSharedBudget.cfg, and "goal met: violation NeverOverspent" for MCSharedBudget.overpromise.cfg.
Done when: ./tla check 16-05-ex3 prints "goal met: pass".
./tla check 16-05-ex3
Repo chapters/16-specifying-agentic-systems/exercises/05-ex3-shared-budget/
Hints
Hint 1
Write TypeOK and Init first, then Halt, and run: the starter error tells you whether Init is right before any action matters.
Hint 2
Held counts reserved units. Reserve must look at spent and reserved units together, in the same step that takes a unit. Spend moves one unit from reserved to spent, and records it for the worker.
Hint 3 (nearly the answer)
Reserve(w) has two guards: one about w, and one comparison that adds spent and Held. NeverOverspent has two conjuncts: one about spent, one about spent + Held. The cfg needs INVARIANT lines for TypeOK and NeverOverspent.
06Explain it
Open NOTES.md and answer the three prompts under 16-05, about the Core exercise.
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:Compare your answers with "16-05 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "QTodo is the open and leased tasks, QFinished the store's output, and QWithdrawn the cancelled tasks. Claims, expiries and refused writes change none of them, so they are stuttering steps for the queue; an accepted write is Finish and a cancel is Withdraw. The lesson's \"not done\" mapping fails here, because a cancel would withdraw a task that stays to do. The refinement depends on fencing and on cancelling only tasks nobody has leased. TLC checked two workers and two tasks; WorkQueue!Spec has no fairness, so this says nothing about tasks getting finished."
A weak answer: "I mapped the variables and Refines passed." A mapping that never moves passes too; the answer does not say why this mapping is the right one.
Done?