16-02
Chapter 16 · Specifying agentic systems
AgentToolLoop
00Recall
One card from 16-01 on why fairness does not make a model answer, one from 03-03 on what a pass over zero states shows, and one from 12-04 on what the small-scope hypothesis does and does not claim. The third one decides today's first prediction.
16-01-a from The model is an oracle
An agent loop has weak fairness on Next and no budget. Why can TLC still find a run that never stops?
Show answer
Weak fairness only forces steps to keep happening. Calling the model and running a tool are steps, so a model that asks for a tool forever gives a fair run that never stops.
03-03-a from Break it on purpose
TLC reports no violation and zero distinct states. What has it shown about your invariants?
Show answer
Nothing. With no reachable states, every invariant holds vacuously.
12-04-b from Simulation and honest claims
What does the small-scope hypothesis claim, and what does it not?
Show answer
That many bugs show up in small instances, so small models are the right place to start. It is an empirical observation, not a proof: a bug that needs four of something cannot appear in a model with three.
Skip recall Recall is never required.
01Commit
specs/AgentToolLoop.tla is one agent's whole loop. The model may ask for any tool in Tools = {"search", "pay"}, or answer. pay is in Risky, so a request for it waits for a human, who approves or denies that one request. Every tool run is appended to audit, a record of which tool ran, after which model call, and under which call's approval. The requirement:
ApprovedEffects is defined as
for all i in 1..Len(audit) :
audit[i].tool in Risky implies audit[i].grant = audit[i].call=> is read "implies". In words: every run of a risky tool was approved, and approved for the very call that asked for it.
A teammate's copy, specs/StickyApproval.tla, saves the human some clicks. After the first approval it sends risky requests straight to the tool, and it keeps the approval for the rest of the session. Their cfg, with Budget = 1, passes: no violation, every action fires, the witness is reached.
Commit a
StickyApproval.budget2.cfg is the teammate's model with Budget = 2 and nothing else changed. What does TLC report?
- No violation: the budget changes how long a run is, not whether approvals are checked.
- A violation of ApprovedEffects, once one run has room to ask to pay twice.
- An error: a second entry in audit breaks TypeOK's bound on its length.
Answer A violation of ApprovedEffects, once one run has room to ask to pay twice.
- If you picked A
- With one model call there is room for one risky request, so "approved once per session" and "approved per request" cannot differ. Ask what the smallest run is in which they could.
- If you picked C
- TypeOK allows up to Budget entries, so two entries are fine with Budget = 2. And a TypeOK violation would be a violation, not an error: TLC evaluates it like any invariant.
Commit b
If TLC finds a violation, how many states does its trace have?
Your answer is a whole number.
Answer 6
- Exactly right
- Right. Count them in the trace: the start, then one state per step.
- Within 10 %
- Within 10 %. Recount the steps the shortest bad run needs, then add the starting state.
- Under by more than half
- Under by more than half. A bad run needs two risky requests, one approval and two tool runs before the audit can hold a stale approval.
- More than double
- More than double. TLC searches breadth-first, so it prints a shortest bad run, not a tour of the model.
- Anything else
- If you were one short, you counted steps, not states: the first state is the start, before any step. Otherwise list the steps of the shortest bad run: a model call asking to pay, an approval, the payment, a second request to pay, the second payment.
02Lab
The teammate's cfg, then the same spec with room for a second call:
./tla check chapters/16-specifying-agentic-systems/specs/StickyApproval.cfg
./tla check chapters/16-specifying-agentic-systems/specs/StickyApproval.budget2.cfg16-specifying-agentic-systems/specs/StickyApproval.budget216-specifying-agentic-systems/specs/StickyApproval.budget2 StickyApproval.budget2.cfg (Tools={"search", "pay"}, Risky={"pay"}, Budget=2)
parse ok
model check: invariant ApprovedEffects violated; trace has 6 states (5 steps); 27 distinct states
expected outcome met: violation ApprovedEffects
trace (changed variables only; ./tla trace --full shows all):
1 Init phase = "model", pending = "none", calls = 0, grant = 0, audit = <<>>
2 CallModel phase: "model" -> "approval", pending: "none" -> "pay", calls: 0 -> 1
3 Approve phase: "approval" -> "tool", grant: 0 -> 1
4 RunTool phase: "tool" -> "model", pending: "pay" -> "none", audit: <<>> -> <<[grant |-> 1, tool |-> "pay", call |-> 1]>>
5 CallModel phase: "model" -> "tool", pending: "none" -> "pay", calls: 1 -> 2
6 RunTool phase: "tool" -> "model", pending: "pay" -> "none", audit: <<[grant |-> 1, tool |-> "pay", call |-> 1]>> -> << [grant |-> 1, tool |-> "pay", call |-> 1], [grant |-> 1, tool |-> "pay", call |-> 2] >>Reveal for commit a
With Budget = 1, no violation in 10 distinct states. With Budget = 2, ApprovedEffects is violated: the second payment runs under the approval granted for the first call.
Reveal for commit b
The trace has 6 states (5 steps). Its last entry in audit shows call and grant disagreeing.
03Reason
If you picked "no violation", you treated the budget as a performance knob. In this model it is the size of the world. With one model call, a session holds at most one risky request, and "approve once per session" means exactly the same as "approve every request". The bug has nowhere to live.
Write something before openingbefore running anything, how could you have chosen Budget = 2?
Ask what the smallest scenario is in which the two designs differ. Approval per session and approval per request disagree only when one session holds two risky requests. Two risky requests need two model calls. So Budget = 2 is the smallest model that can tell them apart, and anything smaller is a model in which the teammate is right by construction.
That question, asked before every run, is how you choose constants. Chapter 12's small-scope hypothesis says to start small. It cannot tell you how small is too small. This question can: name the bug you fear, find the smallest run that contains it, and make the constants at least that big.
Now the loop itself. It follows the loop of the course Build an AI Agent, plus the budget check from that course's lesson on knowing when to stop, written as five steps and their actions:
| Step in the loop | Action in the spec |
|---|---|
| Call the model with the conversation so far | CallModel, with the oracle \E r \in Replies |
| Run the tool it asked for | RunTool, after Approve when the tool is risky |
| Feed the tool's result back | RunTool returns the phase to "model" |
| Check the budget | CallModel needs calls < Budget; OutOfBudget stops |
| Stop on an answer | CallModel with reply "answer", then Halt |
The spec says nothing about what the tool returns or what the model makes of it. Those details are where the agent's usefulness lives, and for this requirement, approval per request, none of them change which runs are safe. Lesson 16-04 is where tool output matters.
Write something before openinga human denies the payment. What may the model do next, and what stops it from asking forever?
Deny sends the refusal back to the model, and the model is an oracle: it may ask for pay again at once. Nothing in the spec makes it accept a no. What bounds the asking is the budget, because every request costs a model call, so a human sees at most Budget approval prompts in one run. Lesson 16-04 says why a limit on prompts matters to the person answering them.
AgentToolLoop.live.cfg checks EventuallyDone under FairSpec and passes, in 35 distinct states. Read the fairness before you trust that. Weak fairness on Next covers every action, including Approve and Deny, so in the approval phase it says that the human eventually answers, one way or the other. It does not say which. That is a fair description of a harness with an approval timeout that counts as a denial. It is not a fair description of a harness that waits on a person who has gone home, and that harness can hang in the approval phase. If yours has no timeout, the honest spec has no fairness on Approve or Deny, and then nothing in it makes a waiting request end.
Write something before openingwhy does ApprovedEffects look at a log of the past instead of at the current state?
Because the question is about the past. "Was this payment approved for this request?" cannot be read off the state after the payment: by then the request is gone. audit is a history variable: it records what happened, and nothing in the spec reads it to decide what happens next. A requirement about history needs history in the state.
The approval is the interesting design decision. grant records which call a human approved, not only that someone approved. With only a Boolean approved, you could not even state the requirement: in the sticky design, the second payment really does run with an approval on record. It is the wrong approval. A requirement is only as sharp as the variables it is written over.
The same goes for what an approval covers. grant ties an approval to a call, not to the arguments of that call, as the box below admits. If your approval screen shows an amount, the spec that checks it needs the amount in pending and in audit, and a requirement that the amount that ran is the amount that was approved.
04In your editor
The loop and its teammate's copy are in chapters/16-specifying-agentic-systems/specs/. Check this lesson's exercises with:
./tla check 16-02The correct loop, with Budget = 2, every action firing and both witnesses reached:
16-specifying-agentic-systems/specs/AgentToolLoop16-specifying-agentic-systems/specs/AgentToolLoop AgentToolLoop.cfg (Tools={"search", "pay"}, Risky={"pay"}, Budget=2)
parse ok
model check: no violation found in 35 distinct states (48 generated, depth 8)
expected outcome met: pass
coverage: all 6 actions fired
witness PaidWithApproval reached (trace of 4 states)
witness BudgetSpent reached (trace of 6 states)When you review an agent harness, find the step that stops the loop and the step that gates side effects, and ask of each: what exactly does it remember, and for how long? The sticky design is a memory bug of exactly that kind: an approval that lives longer, and covers more, than the request it was given for.
If you model your own harness, do it in my-system/ (a private repository). Don't paste your harness, prompts or tool list into third-party AI tools without your organisation's approval (see 00-04).
05Practice
Warm-up. The loop in the starter cannot stop when the budget runs out. Write the budget check.
Core. One step past the lesson: a teammate lets the model correct a payment's amount after the human approved it. Write the requirement that an approval covers the amount, and a cfg big enough to catch the design.
16-02-ex1Warm-up5 min
16-02-ex1 · Stop at the budget
Tier: warm-up. Time: about 5 minutes.
The lesson's AgentToolLoop, with the budget check missing.
- Run
./tla check 16-02-ex1and read the trace. What is the agent doing in its last state, and why can nothing happen next? - Fill gap g1 in
AgentToolLoop.tla: when no model call is left, the harness stops the loop, and nothing else changes. - Run the check again: no violation, every action firing, both witnesses reached.
- The check also runs
MCAgentToolLoop.cfg, the course's check that the harness stops the loop only from the model phase, only with the budget spent, and without changing anything but the phase. Leave the twoMCAgentToolLoopfiles as they are.
Hints: HINTS.md, under "16-02-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/16-specifying-agentic-systems/exercises/02-ex1-stop-at-the-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. Both cfgs must meet the goal "goal met: pass".
Done when: ./tla check 16-02-ex1 prints "goal met: pass".
./tla check 16-02-ex1
Repo chapters/16-specifying-agentic-systems/exercises/02-ex1-stop-at-the-budget/
Hints
Hint 1
In the last state of the trace, the phase is "model" and calls equals Budget. Which action could run there? Read the guard of CallModel.
Hint 2
OutOfBudget is taken by the harness, not the model. It needs the phase to be "model" and the budget to be spent, and then it ends the loop.
Hint 3 (nearly the answer)
Two guards, one new value for phase, and every other variable unchanged. MCAgentToolLoop.cfg fails if any of those parts is missing.
16-02-ex2Core8 min
16-02-ex2 · Approve this call
Tier: core. Time: about 8 minutes.
One step past the lesson. In PayApproval.tla, a request to pay carries an amount, and the human approves it with the amount on screen. The approval is tied to the call, as the lesson asked. But the model may correct a pending call's amount before it runs, and the teammate keeps the approval when it does: same call, they reason. Their cfg passes.
- Run
./tla check 16-02-ex2.PayApproval.cfgpasses,./tlasaysAmendnever fires, and TLC warns that your invariant is a constant. Before reading on, say whyAmendcannot fire in that model. - Fill gap g1 in
PayApproval.tla:PaidAsApproved, every risky tool run inauditwas approved for the call that asked for it, and ran with the amount the human approved. - Fill gap g1 in
PayApproval.cfg: a model big enough to show whatAmendallows, withTypeOK,WithinBudgetandPaidAsApproved. Name the bug first, then find the smallest constants that contain it; it is not the constant the lesson raised. - The check also runs two course cfgs. Leave the
MCPayApprovalfiles as they are.MCPayApproval.cfgis a correct design that sends a corrected payment back to the human and pays two amounts in one run: your invariant must pass there.MCPayApproval.sticky.cfgis the lesson's sticky approval with a single amount: your invariant must catch it.
Hints: HINTS.md, under "16-02-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/16-specifying-agentic-systems/exercises/02-ex2-approve-this-call/; open it after you have a passing answer of your own.
All three cfgs must meet their goals: "goal met: violation PaidAsApproved" for PayApproval.cfg and MCPayApproval.sticky.cfg, and "goal met: pass" for MCPayApproval.cfg.
Done when: ./tla check 16-02-ex2 prints "goal met: violation PaidAsApproved".
./tla check 16-02-ex2
Repo chapters/16-specifying-agentic-systems/exercises/02-ex2-approve-this-call/
Hints
Hint 1
Amend cannot fire with one amount: its guard needs a different one. Then read an entry of audit: it has the tool, the amount it ran with, the call that asked for it, and grant, a record of the call and the amount the human approved. Write in words what must be true of an entry whose tool is risky.
Hint 2
Two things must match, not one: the approval's call and the entry's call, and the approval's amount and the entry's amount. The course checks catch either half alone. For the cfg, the bug needs one call and two amounts.
Hint 3 (nearly the answer)
For every index i of audit whose tool is in Risky, compare audit[i].grant with one record built from the entry itself. Which two fields does that record need? In the cfg, give Amounts two values; Budget = 1 is enough.
06Explain it
Open NOTES.md and answer the three prompts under 16-02, 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-02 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "PaidAsApproved requires each risky run in audit to carry the approval for its own call and for the amount it ran with. The teammate's Amend violates it with one call and two amounts: the human approves one amount and another runs. With one amount Amend cannot fire, so the model was too small in the amounts, not the budget. Comparing calls alone passes the teammate's design, and comparing amounts alone misses the sticky approval. The invariant assumes audit records what really ran and that the screen showed pending.amount. TLC did not check what the human read, other arguments, or larger models."
A weak answer: "TLC found the bug once I added an amount." It does not explain why one amount hid it, or why both halves of the comparison are needed.
Done?