00-04
Chapter 00 · The bug your tests won't find
How this course works
00Recall
Three cards: why sequential tests miss the race, how many steps a trace of k states has, and what separates a TLC error from a violation.
00-01-b from Predict the balance
Why can a suite of sequential tests never see this race?
Show answer
Each test runs one call to completion before starting the next, so two calls are never in flight at once, and the bad orders of steps never happen.
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.
00-03-b from Your first TLC run
What is the difference between a TLC error and a violation?
Show answer
A violation means TLC found a reachable state where your invariant is false, and it shows a trace to it (so does a deadlock, a reachable state where no step is possible). An error means TLC could not evaluate, parse or configure something, so there is no verdict.
Skip recall Recall is never required.
01Commit
You ask a coding agent to write transfer() for a wallet service. It returns the code, nine tests, and a message. The message says all nine tests pass. It says the function is safe to call from concurrent request handlers, because each read or write of a dictionary entry is atomic in CPython. One test is called test_concurrent_transfers. The files are in this chapter's ai/1-agent-transfer/output/ folder. They are canned: the course wrote them by hand in the style of agent output, and nothing in this course calls a live model.
Commit a
An agent's transfer() (in ai/1-agent-transfer/output/) comes with nine passing tests and a message saying it is safe under concurrency. Is it safe when two requests draw on the same account at the same time?
- Yes. It has a test for concurrent transfers, and that test passes.
- Yes. Each dictionary read and write is atomic, so no two requests can interfere.
- No. It has the same race as the transfer() from lesson 00-01.
Answer No. It has the same race as the transfer() from lesson 00-01.
- If you picked A
- Read the body of test_concurrent_transfers. It calls transfer() in a loop, one call after another. The name says "concurrent"; the code is sequential. A test's name is a claim about the test, and it can be wrong.
- If you picked B
- The fact is true and the conclusion does not follow. Each single read is atomic and each single write is atomic, but the read and the write are two separate operations. Another request can run between them, which is the window from lesson 00-02.
- If you picked C
- Right. The agent's function reads the balance into a local variable, checks it, and writes back a value computed from that copy: the same three steps, with the same gap.
Commit b
Another agent is told to make ./tla check 00-ai1 pass. It deletes the two invariants about money, NoFreeMoney and BothApplied, from the cfg, turns off the deadlock check, and reports "goal met", all actions fired, witness reached. What do you conclude?
- The agent failed the task: it made the check pass by removing what the check was for.
- Still fine: the cfg still checks
TypeOK, so the model is still being checked. - Acceptable for now: coverage is full and the witness was reached, so the model still runs both transfers to the end.
Answer The agent failed the task: it made the check pass by removing what the check was for.
- If you picked A
- Right. This lesson's AI checklist says it: an agent that makes checks pass by editing the checks has failed the task. Read the diff of every cfg, not only of the code.
- If you picked B
TypeOKsays each variable holds a value of the right kind. A balance of 70 after 80 was paid out is still a number in range, soTypeOKcannot fail on this race.- If you picked C
- Coverage and the witness show that the model still runs to the end. They say nothing about the money. Green only means something if it could have been red: with both gone, nothing in the run looks at the balance, so the race is still there and nothing can see it.
02Lab
The course wrote a model of the agent's code: ai/1-agent-transfer/Transfer.tla. It is a copy of the chapter's specs/Transfer.tla, because every folder in the course is self-contained, with comments that point each step at a line of the agent's code. Its last two definitions are new, and its cfg checks them as invariants too: BothApplied says that once both transfers are done, the balance is down by both amounts and both payees were paid, and NotStuck says that until both are done, some step can still change something. Run its check from the repository root:
./tla check 00-ai1It prints the same kind of block as lesson 00-03. TLC's model check: line reports a violation of NoFreeMoney. The course's line below it starts with starter state: instead of expected outcome met, because the run ended the way the AI item ends as shipped, and judging it is your job. The line means only that: an edited model that still breaks NoFreeMoney prints it too. The trace:
- State 1 Initial state balance = 100 paid = 0 pc = <<"read", "read">> seen = <<0, 0>>
- State 2 Read pc <<"read", "read">> becomes <<"debit", "read">> seen <<0, 0>> becomes <<100, 0>>
- State 3 Read pc <<"debit", "read">> becomes <<"debit", "debit">> seen <<100, 0>> becomes <<100, 100>>
- State 4 Debit balance 100 becomes 50 pc <<"debit", "debit">> becomes <<"debit", "credit">>
- State 5 Debit balance 50 becomes 70 pc <<"debit", "credit">> becomes <<"credit", "credit">>
- State 6 Credit paid 0 becomes 50 pc <<"credit", "credit">> becomes <<"credit", "done">>
Reveal for commit a
TLC finds the same violation of NoFreeMoney, with the same 6-state trace. "Starter state" is the expected result here: this item asks you to diagnose the bug, not to fix it, and the solution shows a fix that TLC checks. The agent's message was fluent, specific, and it cited a real fact about Python. It was still wrong, and only a check that could fail showed it.
Reveal for commit b
The check went green because nothing in it could go red on this bug any more. On this item's cfg, deleting those two lines and turning deadlock checking off is enough for ./tla check 00-ai1 to print "goal met", with full coverage and the witness reached. The race is untouched. The solutions folder keeps that gamed cfg as TransferGamed.cfg, and the course's CI checks that it still passes, so this paragraph is checked, not remembered. When an agent's change touches a .cfg, read that diff first.
03Reason
If you picked "yes, it has a test", look at what persuaded you: a name. If you picked "yes, operations are atomic", you were persuaded by a true fact attached to a false conclusion. Both are the moves a confident teammate makes, human or not. The habit this course trains is to ask one question of any evidence: could this check have failed? test_concurrent_transfers could not fail on this bug, because it never runs two transfers at once. TLC could fail, and did.
The loop. Every lesson has the same beats. You recall up to three old cards. You commit to a prediction, with a confidence of 60, 75 or 90 %. You settle it with a lab or a ./tla check. Then you read the reasoning, practise, and explain what you built. The order matters. A prediction written down before the run is what makes a surprise visible.
Write something before openingwhy does the course ask for a confidence along with each prediction?
So that you can find out whether your confidence means anything. If you say 90 % twenty times and are right twelve times, your 90 % is really closer to 60 %. For the predictions in the final third of the course, the progress page shows, for each confidence level, how many you got right and a likely range. It never grades you.
Hints, notes and solutions. Every exercise has three hints in the chapter's HINTS.md, from a nudge to nearly the answer. Use them. After each lesson you answer three prompts in NOTES.md, and you grade yourself against a good answer and a weak answer in HINTS.md. Solutions are in solutions/, which mirrors the course folders. They are there so that you can check your work, not so that you can skip it.
Gates. From chapter 03 on, each chapter ends with a gate: a short spec you write from a prose description, graded by ./tla check --gate NN. The grader checks hidden properties, makes sure every action can happen, and tries deliberately broken copies of a correct spec (mutants) against your properties. Your properties must catch every hand-written mutant, and every systematic mutant that the reference properties catch.
Tracks. Each lesson is marked essentials or core. The essentials track (every lesson of this chapter is in it) is the shortest path to using TLC at work, and it ends in a small project: spec one thing from your own work. The full course goes on to fairness, refinement, distributed systems and specs for AI-built code. The course map shows both tracks with their generated time estimates.
AI in this course. You may use AI tools for explanations, and for Warm-up and Core practice. You may not use them for gate specs, gate properties, checkpoints or mastery drills. If an LLM writes your invariants, the only thing you've learned is reading TLC output. In chapters 14 to 16, AI becomes part of the workflow, inside a process where you own the spec. No exercise needs an API key, an account or a network call. Every piece of "AI output" in the course is a committed file, like the one in this lesson.
When you do use an AI tool, follow this checklist:
- Read every diff before you run AI-generated code or
.cfgfiles. - If you run AI-written code, including this chapter's
ai/1-agent-transfer/output/files, read it first and run it inside the course's dev container or a Codespace, not directly on your own machine. That keeps it away from the rest of your machine, but it is not a sandbox: the container has the network, it can write anywhere in the repository (.git/too, where a planted hook runs at your nextgitcommand), and by default VS Code passes it your git credentials and a Codespace holds a GitHub token. Run code you do not trust in a throwaway clone, and look atgit statusand.git/hooks/afterwards. - An agent that makes checks pass by editing the checks has failed the task.
- An AI summary of TLC output is a claim. Check it against the raw log.
- Strip identifiers (names of systems, customers, hosts) before you paste a spec anywhere.
- Do not paste proprietary designs, customer data, credentials or unreleased specs into third-party AI tools without your organisation's approval.
- Prefer local models for sensitive work: a model running on hardware you control does not hand your spec to a third party.
Your own system, privately. From chapter 01 on, milestones ask you to model a system you work with. Keep that work in a private repository created with GitHub's "Use this template" button, never in a public fork, because a fork of a public repository is public. START.md has the steps.
04In your editor
The AI item is in chapters/00-the-bug-your-tests-wont-find/ai/1-agent-transfer/. Its canned output is under output/. Read output/MESSAGE.md first, then the code. From the repository root:
./tla check 00-ai1Besides the verdict and the trace shown in the LAB, it prints a hints: line with the address of this item's hints, and the raw log: line from lesson 00-03.
05Practice
Warm-up: a worked example, then your turn. Under the course policy, which of these uses of an AI tool are allowed?
- Item 1: asking it to explain what "successor state is not completely specified" means.
Worked example for item 1: allowed. It asks for an explanation, which the policy allows, and nothing it produces is graded as your work. Check the explanation against the lesson it points to, the way you would check a teammate's.
Your turn:
- Item 2: asking it to write the
Safetyproperty for the chapter 06 gate. - Item 3: asking it to check the syntax of a Warm-up exercise you wrote.
- Item 4: pasting your employer's unreleased design document into a third-party chatbot to get a spec started.
- Item 5: asking it to summarise a long TLC log, then reading the log to check the summary.
Write something before openingallowed or not, and why.
Items 3 and 5 are allowed: help with Warm-up practice, and a summary you check against the raw log. Item 2 is not: gate properties are yours to write, because a gate measures what you can do. Item 4 is not, unless your organisation has approved it: unreleased designs stay out of third-party tools.
Core: the agent's transfer. Work through 00-ai1. The task is in ai/1-agent-transfer/README.md, and its hints are in HINTS.md under "00-ai1".
06Explain it
Open NOTES.md and answer the three prompts under 00-04. For the first, explain why the agent's concurrency test could not have caught the race. Then compare with "00-04 check your explanation" in HINTS.md.
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:After you writeCheck your explanation
A good answer: "The agent's concurrency test calls transfer() two hundred times in a loop, one call after another, so two calls never overlap and the race cannot show. Its other evidence, that dictionary operations are atomic, is true of each operation but not of a read followed by a write. TLC, checking a model of the same three steps, found the violation. That model is the course's reading of the agent's code; the Python itself was never run under TLC, and same-target credits were abstracted away."
A weak answer: "AI code can be wrong, so you should always test it." The agent did test it. The answer misses why these particular tests could not fail on this bug.
Done?