16-01

Chapter 16 · Specifying agentic systems

The model is an oracle

  • 45 min core
  • Core track
  • Skills: abstraction, intent, hostile-env

00Recall

One card from 10-02 on weak fairness, one from 10-03 on strong fairness, and one from 09-01 on safety and liveness. All three come back within the first ten minutes: this lesson is fairness meeting a component that has no obligations at all.

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.

10-03-a from When WF is not enough

What is the one difference between the definitions of WF_v(A) and SF_v(A)?

Show answer

The hypothesis. WF needs ENABLED <<A>>_v continuously (<>[]); SF needs it only infinitely often ([]<>). Both then demand <<A>>_v steps infinitely often.

09-01-a from The prefix judge

What makes a property a safety property, and what makes it a liveness property?

Show answer

Safety: every violating behavior has a finite prefix that already violates it, whatever comes next. Liveness: every finite prefix can still be continued into a behavior that satisfies it.

Skip recall Recall is never required.

01Commit

You cannot put a language model in a spec. Its weights do not fit, and a spec could not use them if they did. What you can put in is the set of things it might say. Here is the whole model, as specs/OracleLoop.tla sees it:

TLA+
CallModel is defined as
    and phase = "model"
    and there exists r in Replies :
          and replyprime = r
          and phaseprime = IF r = "answer" THEN "done" ELSE "tool"

\E r \in Replies is read "there exists an r in Replies". The cfg sets Replies = {"tool", "answer"}. After a "tool" reply, RunTool runs the tool and hands the loop back to the model. There is no budget yet. The spec the cfg checks is

TLA+
FairSpec is defined as Spec and WF_vars(Next)

and the requirement is EventuallyDone == <>(phase = "done"), read "eventually, the phase is done".

Commit a

OracleLoop.live.cfg checks EventuallyDone under FairSpec, weak fairness on Next. What does TLC report?

  1. No violation: weak fairness keeps the loop moving, so it reaches "done".
  2. No violation: "answer" is one of the replies, so sooner or later TLC picks it.
  3. A violation: a lasso in which the model asks for the tool forever, and every step is fair.

Answer A violation: a lasso in which the model asks for the tool forever, and every step is fair.

A teammate suggests a sharper assumption: make the answer itself fair.

TLA+
ModelAnswers is defined as
    and CallModel
    and replyprime = "answer"

WeakOracleSpec is defined as FairSpec and WF_vars(ModelAnswers)

Commit b

OracleLoop.weak.cfg checks EventuallyDone under WeakOracleSpec, which adds weak fairness on ModelAnswers. What does TLC report?

  1. The same violation as before: the lasso still satisfies every fairness condition.
  2. No violation: the answer step is fair now, so it must happen.
  3. An error: fairness can only be put on Next.

Answer The same violation as before: the lasso still satisfies every fairness condition.

02Lab

Run the two cfgs from the commits, then a third that uses strong fairness:

shell
./tla check chapters/16-specifying-agentic-systems/specs/OracleLoop.live.cfg
./tla check chapters/16-specifying-agentic-systems/specs/OracleLoop.weak.cfg
./tla check chapters/16-specifying-agentic-systems/specs/OracleLoop.strong.cfg
Expected output 16-specifying-agentic-systems/specs/OracleLoop.live
16-specifying-agentic-systems/specs/OracleLoop.live  OracleLoop.live.cfg  (Replies={"tool", "answer"})
parse ok
model check: temporal property EventuallyDone violated; lasso of 3 states, looping back to state 2; 4 distinct states
expected outcome met: property EventuallyDone
trace (changed variables only; ./tla trace --full shows all):
  1  Init       phase = "model", reply = "none"
  2  CallModel  phase: "model" -> "tool", reply: "none" -> "tool"
  3  RunTool    phase: "tool" -> "model"
     CallModel  back to state 2

Reveal for commit a

EventuallyDone is violated. The trace is a lasso of 3 states: the model asks for the tool, the tool runs, and the run returns to the state where the tool is about to run, forever. Every step in it is a real step, so weak fairness on Next is satisfied the whole time.

Reveal for commit b

The same lasso, again a violation. ModelAnswers is enabled only while the phase is "model". Every time the loop runs the tool, it is disabled again, so it is never enabled continuously, and weak fairness asks for nothing.

03Reason

If you picked "weak fairness keeps the loop moving", you read fair as eventually does the right thing. Fairness never says that. It says what must happen if something stays possible.

Write something before openingin the lasso, which step was possible forever and never taken?

None was possible forever. In the state where the model is called, ModelAnswers is possible; one step later, in the tool phase, it is not. Weak fairness on an action needs that action to be enabled at every step from some point on. This one blinks: enabled, disabled, enabled. The lasso is fair.

Here is the idea underneath. \E r \in Replies is not a coin flip. TLC has no dice. A nondeterministic choice says "any of these may happen", and TLC follows every branch, so the spec allows the behavior in which the model gives the worst reply every single time. That is not pessimism for its own sake. A model you call through an API gives you no promise about its next reply, and an oracle is the honest way to write "no promise". Treat it like a hostile environment, the way chapter 08 treated the network.

Write something before openingyour model can also refuse. Where does a refusal go in this spec, and what must the loop do with it?

Into Replies, as one more value, and then into CallModel, which must say which phase a refusal leads to. That second part is the design decision. Send it to "done" and a refusal ends the run like an answer does; send it back to the model and the loop asks again, which is a retry and needs a bound. Writing the oracle forces the question, because every reply needs somewhere to go. Look at the IF in CallModel: a refusal added to Replies and to nothing else would be treated as a request for the tool, silently.

So how does an agent loop ever finish? There are two honest ways, and one dishonest one.

The first is a budget. specs/BudgetLoop.tla counts model calls and adds one action that the harness, not the model, takes:

TLA+
OutOfBudget is defined as
    and phase = "model"
    and calls = Budget
    and phaseprime = "done"
    and UNCHANGED tuple of reply, calls end tuple

With Budget = 3 and nothing but weak fairness on Next, BudgetLoop.live.cfg passes: termination now follows from the counter plus weak fairness on Next. Nothing is assumed about what the model says. The model may ask for tools all it likes; the counter does not care. Weak fairness does assume that each call to the model or the tool returns, because each is one step here: true of a harness whose calls time out, false of one that waits forever.

Write something before openingthe budget made EventuallyDone pass. Name one thing the user wanted that it does not give them.

An answer. OutOfBudget sets the phase to "done" without one, and EventuallyDone cannot tell the two endings apart: it says the loop stops, not how. That is why BudgetLoop.live.cfg names two witnesses. Answered shows that some run ends with the model's answer; BudgetSpent shows that some run ends with the counter. A loop that gave up on every run would satisfy EventuallyDone equally well. If you care which ending happened, and your user does, record it in a variable and write a property about it.

The second is an assumption about the model, written as strong fairness:

TLA+
OracleSpec is defined as FairSpec and SF_vars(ModelAnswers)

OracleLoop.strong.cfg passes, with both witnesses reached. Do not stop there. Strong fairness on ModelAnswers is a claim about the world: "a model that is asked again and again cannot avoid answering forever". Is that true of yours? If the harness sends the same context every time and the model is configured to be deterministic, the same question gets the same reply, and the claim is false. It becomes believable only when something changes between calls, such as a tool result appended to the context, and even then it is an assumption, not a fact. Write it down next to the spec, with the reason you believe it. The budget needs a smaller defence: only that every call returns.

Write something before openinga teammate writes OracleSpec == FairSpec /\ <>(phase = "done"). What will TLC say?

It passes. OracleLoop.assumed.cfg runs exactly that, under the name AssumedSpec, and TLC finds no violation in 4 distinct states, with both witnesses reached. You have assumed the requirement and then checked it. Fairness conditions on actions say how the world behaves; <>(phase = "done") says how the run must end, which is the thing you were supposed to show.

04In your editor

The specs are in chapters/16-specifying-agentic-systems/specs/. Check this lesson's exercises with:

shell
./tla check 16-01

The budgeted loop, with weak fairness and nothing assumed about what the model says:

Output 16-specifying-agentic-systems/specs/BudgetLoop.live
16-specifying-agentic-systems/specs/BudgetLoop.live  BudgetLoop.live.cfg  (Replies={"tool", "answer"}, Budget=3)
parse ok
model check: no violation found in 11 distinct states (15 generated, depth 8)
expected outcome met: pass
coverage: all 4 actions fired
witness Answered reached (trace of 2 states)
witness BudgetSpent reached (trace of 8 states)

When you specify a loop of your own, write the oracle first and the fairness last. List every reply the real component can give you, including the unhelpful ones. Then ask what makes the loop stop. If the answer is "the model will eventually cooperate", look for a counter you can add instead.

05Practice

Warm-up. A research assistant's loop has a model that always answers at once. Make it an oracle.

Core. Another loop: a coding agent that writes patches until the tests pass, with no limit on attempts, and a blank cfg. Find the step the model's choice lives in, state the one assumption about it that makes the loop stop, and check it.

16-01-ex1Warm-up5 min

16-01-ex1 · Let it say anything

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

A research assistant's loop: ask the model, run a web search when it asks for one, stop on an answer or when Budget = 3 model calls are used. In the starter, the model always answers at once. That is not an oracle; it is a wish.

  1. Run ./tla check 16-01-ex1. The starter passes, but ./tla says RunSearch never fires. Before reading on, say why.
  2. Fill gap g1 in ResearchLoop.tla: the model may return any reply in Replies. "answer" ends the loop; any other reply runs the search.
  3. Run the check again. It must pass with every action firing and both witnesses reached: an answer after a search, and a run that spends its budget without answering.

Hints: HINTS.md, under "16-01-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/16-specifying-agentic-systems/exercises/01-ex1-let-it-say-anything/; 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-01-ex1 prints "goal met: pass".

./tla check 16-01-ex1

Repo chapters/16-specifying-agentic-systems/exercises/01-ex1-let-it-say-anything/

Hints

Hint 1

RunSearch never fires because nothing ever sets the phase to "search". Read the two placeholder lines: which reply does the model always give?

Hint 2

The lesson's OracleLoop has the shape you need: one \E over the replies, and inside it, the new reply and a new phase that depends on which reply it was.

Hint 3 (nearly the answer)

Replace the two placeholder lines with one \E r \in Replies : … conjunct. Inside it, reply' takes the chosen reply, and phase' is an IF on which reply it was. Keep the guard and the calls' line above the gap.

16-01-ex2Core8 min

16-01-ex2 · State the assumption

Tier: core. Time: about 8 minutes.

A different loop from the lesson's. A coding agent fixes a failing test: the model writes a patch, the test suite runs it, a passing patch is merged, and a failing one goes back to the model with the test output. There is no limit on attempts. FixLoop.tla has the loop; the assumption that makes it stop, and the cfg that checks it, are yours.

  1. Run ./tla check 16-01-ex2. All three cfgs report a lasso: under FairSpec, the model can write failing patches forever.
  2. Fill gap g1 in FixLoop.tla: FixSpec is FairSpec plus one fairness condition, WF or SF, on an action. Next may not have the action you need; you can define it inside the gap. Write in a comment why you believe a real model satisfies this assumption.
  3. Fill gap g1 in FixLoop.cfg: check EventuallyMerged under your FixSpec, with Patches = {"passing", "failing"} and TypeOK.
  4. The check also runs two course cfgs. Leave both as they are. FixLoop.grader.cfg checks your FixSpec in the same world as yours. FixLoop.nofix.cfg checks it in a world where every patch fails: there, an assumption about the model asks for nothing, so EventuallyMerged must still be violated. A FixSpec that demands the merge outright, such as FairSpec /\ <>(phase = "merged"), leaves that world with no behaviors at all and passes, so it misses the goal.

Ask first which step the model's choice lives in. It is not the step that merges.

Hints: HINTS.md, under "16-01-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/16-specifying-agentic-systems/exercises/01-ex2-state-the-assumption/; open it after you have a passing answer of your own.

All three cfgs must meet their goals: "goal met: pass" for FixLoop.cfg and FixLoop.grader.cfg, and "goal met: property EventuallyMerged" for FixLoop.nofix.cfg.

Done when: ./tla check 16-01-ex2 prints "goal met: pass".

./tla check 16-01-ex2

Repo chapters/16-specifying-agentic-systems/exercises/01-ex2-state-the-assumption/

Hints

Hint 1

Read the lasso: the model writes a failing patch, the tests run, and the loop goes back to the model. Which choice, made by whom, would end it? Is there an action in Next that is exactly that choice?

Hint 2

RunTests merges, but in the lasso it never sees a passing patch, so fairness on it asks for nothing. The choice is made in WritePatch. Define the part of it in which the patch passes; it is enabled whenever the phase is "write", and disabled while the tests run.

Hint 3 (nearly the answer)

Inside the gap, define the part of WritePatch in which the patch passes, the way the lesson defined ModelAnswers. Then ask which kind of fairness still forces an action that blinks. In the cfg, SPECIFICATION names FixSpec, and the property line names the requirement.

06Explain it

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

After you writeCheck your explanation

A good answer: "I defined WritesPassingPatch, the model writing a passing patch, and assumed strong fairness on it: a model asked infinitely often eventually writes one. Fairness on RunTests would ask for nothing, because the lasso never tests a passing patch; weak fairness fails because the action is disabled while the tests run. The assumption breaks for a model that sees the same prompt every time; it is plausible only because test output changes the context, and a limit on attempts would remove the need for it. TLC checked one model, with passing and failing patches; the nofix world, where every patch fails, shows that my fairness is an assumption about the model and does not force the merge by itself, but nothing shows a real model satisfies it."

A weak answer: "I added SF and now it terminates." It does not say which action the assumption is about, what it claims about the model, when it would be false, or why WF was not enough.

Done?