Exercise: a developer, a tester and a gate
- Track
- Advanced Agents
- By
- Sourav Banerjee
- Last updated
- 30 Nov 2026
01 Context
Problem statement
Larkspur Cloud wants a small function that works out the SLA credit a customer is owed after an outage. You give the job to a deep agent with two sub-agents. The tester writes the pytest tests from the spec, and the developer writes the function. After each iteration you run the tests yourself, outside the agent, and send the result back. A cap in code stops the loop. When the loop ends, you check the work against the spec by hand, and nothing leaves the workspace until it passes an approval gate.
The rule you apply comes from lessons 1 and 2 of T8.2: the tests are the reward signal, but a test result is only evidence about the tests. Before you blame the developer or the tester, check both against a reference you wrote yourself.
02 Outcomes
What you will learn
- Build a deep agent with a developer and a tester sub-agent, and route between them with a counter and a cap in code.
- Run the tests outside the agent and log one row per iteration: who was called, whether the code changed and what pytest said.
- Decide which side is wrong when the tests fail, with the spec's own examples and a hand-written reference.
- Put a fail-closed approval gate, bound to a fingerprint, between the agent's workspace and the rest of your system.
- Explain what a small model does inside a harness built for a large one.
03 Setup
Before you start
Lessons
- T8.2 Coding, Computer-Use & Deep Agents: lesson 3 of T8.2 Deep agents: orchestrator, sub-agents, skills and long-term memory for
create_deep_agent, thetasktool and whatFilesystemBackend(virtual_mode=True)does and does not do, lesson 1 of T8.2 Applied ReAct for tests as the reward signal, and the optional lesson 2 of T8.2 Developer vs tester agents for the pure router, the cap and checking against a reference. - T8.1 Planning, Reflection & Reasoning Patterns: lesson 3 of T8.1 Reflection and Reflexion agents, and the optional lesson 5 of T8.1 Self-Discover and reflective code generation for execution as the critic.
- T8.3 Human Approval & Applied Single-Agent Builds: lesson 1 of T8.3 Dry-run harness and human approval for the fail-closed decision and the fingerprint check.
Dataset
The spec for one function, sla_credit(availability, server_charges), with three rules from the handbook, two input checks, six worked examples and a request for tests, in data/. The Larkspur handbook is copied beside it, and the agents see its SLA page.
Code
create_deep_agent and FilesystemBackend from deepagents, ChatOllama from langchain_ollama, and subprocess to run pytest in the workspace. Nothing from src/ is needed.
Environment
The course's default environment (uv run jupyter lab) and a local Ollama server with qwen2.5:3b pulled (ollama pull qwen2.5:3b). The extension also uses llama3.2:3b. Everything runs offline: no API keys and no hosted service. The notebook runs model-written code with your user's permissions, so run it in a throwaway environment.
04 Architecture
Experiment workflow
The spec, the prompts, the model, the cap and the test command stay fixed. Only the loop's round number changes, and with it the message the router sends.
Extension: the same loop with llama3.2:3b, in a new workspace.
05 Build
Implementation tasks
Task 1: Make a workspace
Copy
../data/spec.mdand the handbook's SLA page into a temporary folder. The agents see only that folder, and the files indata/are never touched.Task 2: Build the deep agent
Create an orchestrator with
create_deep_agentand aFilesystemBackendrooted at the workspace in virtual mode. Give it two sub-agents: a developer that owns/credit.pyand a tester that owns/test_credit.py. Tell each one the exact file path to write and what must not go in it.ChatOllama(model="qwen2.5:3b", temperature=0, num_ctx=8192, num_predict=1500) FilesystemBackend(root_dir=workdir, virtual_mode=True)New here: a sub-agent only sees the
descriptionthe orchestrator writes, and a 3B orchestrator writes thin ones. Pass each sub-agent as a compiled sub-agent ({"name", "description", "runnable"}) whose runnable adds the spec, its previous file and the latest evidence to that description before it calls a small deep agent of its own. Becausewrite_filerefuses to overwrite, the wrapper deletes the old file after putting it in the brief, and puts it back if the sub-agent writes nothing.Task 3: Run the tests outside the agent
Run pytest in a fresh process in the workspace, with a timeout, no keyboard and no keys in its environment. The exit code is the result. The agent's own claims are not.
[sys.executable, "-m", "pytest", "-q", "-p", "no:cacheprovider", "--tb=line", "test_credit.py"]Task 4: Loop with a cap and log every iteration
Round 1 asks the orchestrator to call the tester and then the developer. Every later round asks it to call the developer, whose brief now holds the pytest report. Stop on a zero exit code or after 5 iterations. Log the sub-agents the orchestrator really called, whether
credit.pychanged and the pytest summary.New here: give each round a fresh
thread_id. The router in code already holds the state, and a fresh thread stops one malformed model reply from failing every later round.Task 5: Check both sides against the spec
Run the spec's six worked examples and three out-of-range inputs against the agent's
credit.py, typed in from the spec by you. Then run the tester's tests against a five-line referencecredit.pyyou wrote yourself, in a separate folder. A test that fails against the reference contradicts the spec, and no developer round can fix it.Task 6: Route the disagreement
If the reference fails some of the tester's tests, send the tester one or two more rounds with the reference report. If the reference passes them all, the code is at fault, and the developer has already had its rounds.
Task 7: Gate the hand-off
Build a preview of
credit.py: its SHA-256 fingerprint, whether the tests pass and the spec-check score. A scripted reviewer answersapproveonly when both checks are clean. Accept only the exact wordapproveafter stripping spaces and lowercasing. Before copying the file out, recompute its fingerprint and refuse if it changed. Plant both failures: a list of loosely typed answers, and one line appended to the file after a planted approval.Task 8: Extension: a second model
Repeat tasks 01 to 05 with
llama3.2:3bin a new workspace and compare where each model breaks.
06 Measure
Record results
What you enter stays in this browser only.
| Round | Sub-agents called | credit.py | Pytest summary |
|---|---|---|---|
| Iteration 1 | |||
| Iteration 2 | |||
| Iteration 3 | |||
| Iteration 4 | |||
| Iteration 5 (cap) | |||
| Spec check, 9 cases | none | final version | |
| Tester's tests against the reference | none | reference |
Sub-agents called lists the task calls the orchestrator made in that round. The credit.py column says whether the file is a new version or unchanged. The last two rows hold the spec-check score (right out of 9) and the pytest summary against your reference.
07 Verify
Done when
0 of 4 done
What you enter stays in this browser only.
08 Debug
Common mistakes
Trusting the agent's last message
The sub-agents often say a file was written when it was not, or that it is correct when it is not. Read the workspace and the pytest exit code instead.
A cap in the prompt
"Stop after five tries" in a prompt is a request. The cap belongs in the loop that calls the agent (lesson 2 of T8.2).
Blaming the developer for a bad test
A red run says the code and the tests disagree. Only a check you wrote yourself says which one is wrong.
A lenient gate
If "yes" or "approved" counts as approval, the gate approves what a reviewer half-meant (lesson 1 of T8.3).
Calling the backend a sandbox
Virtual mode blocks .. and nothing else. The tests here run with your user's permissions (lesson 3 of T8.2).
09 Transfer
Interview angle
“Your coding agent says the tests pass. What do you check before you merge its change?”
Show a model answer
Run the tests myself, outside the agent, and read the exit code. Check the tests against the spec, because the agent may have written them too. Diff the files for edits no test asked for. Then put the change behind an approval that is bound to the exact bytes the reviewer saw.
10 Review
Worked solution
The worked solution is solution/p07_exercise_solution.ipynb (open in Colab). Locally, open it from exercise/solution/ so ../data/ resolves, with Ollama running and both models pulled. Colab has no Ollama server, so there the notebook runs only if you install and start Ollama in the runtime first.
Show our run and the reasoning
Our run: qwen2.5:3b and llama3.2:3b through Ollama, deepagents 0.7.32, langgraph 1.2.18, langchain-ollama 1.1.0 and pytest 9.2.0, offline.
| Round | Sub-agents called | credit.py | Pytest summary |
|---|---|---|---|
| Iteration 1 | tester, developer | new version | 5 failed, 2 passed |
| Iteration 2 | developer | new version | 5 failed, 2 passed |
| Iteration 3 | developer | new version | 5 failed, 2 passed |
| Iteration 4 | developer | unchanged | 5 failed, 2 passed |
| Iteration 5 (cap) | developer | unchanged | 5 failed, 2 passed |
| Spec check, 9 cases | none | final version | 3 of 9 right |
| Tester's tests against the reference | none | reference | 7 passed |
The tests never passed, and the cap stopped the loop after 5 iterations of 3 to 9 seconds each. The orchestrator called the right sub-agent in every round, so the routing in code worked. The failure is in the developer.
The tester's 7 tests were right: all 7 pass against the reference. The developer's code was wrong from the first round. It read availability as a fraction, with the check if availability < 0 or availability > 1: raise ValueError(...), so every real input such as 99.94 raised ValueError. The 2 passing tests are the two out-of-range cases, which pass only because everything raises. Each pytest report showed the error, and none of the three new versions the developer wrote fixed it. By iteration 5 its file held only that check and no return path. The spec check scores it 3 of 9, the three inputs that should raise.
Because the tests agreed with the reference, step 06 sent no tester rounds. The gate rejected the file, since neither check was clean, and nothing was promoted. The planted checks behaved as lesson 1 of T8.3 says: approve and Approve approved, approved, yes and reject did not, and the planted approval refused the file once one line was appended, then promoted the unedited copy (the notebook deletes that copy, because a planted approval is not a real one).
In the extension, llama3.2:3b failed earlier. Its tester never wrote /test_credit.py at the workspace root in 5 rounds, so pytest found no file each time. Its developer's credit.py returned 10% for every availability below 99.95, never 25%, and had no input checks, which scores 4 of 9.
Runs at temperature 0 still vary slightly between tries. In our trial runs before this one, a developer round once wrote a correct credit.py, and once the tester wrote ValueError tests for 0 and 100, which the spec allows. Read your own log before you compare it with ours.