In Part 2 I built loop_graph.py. It runs workers in parallel. A shell check loops each worker until the work is done, and one human gate sits before the final command. Later I added Cursor as a second runner.
Then a “build your agent team” prompt went around. It is written for Gemini and Antigravity, but most of its ideas do not depend on the platform:
- Each agent has one job, a “done well” checklist, a NEVER list and an ASK ME FIRST list.
- A Checker scores every result.
- The edges of the graph carry labelled handoffs.
- A Lead routes the work with if/then rules.
- The quality loop is capped, and then the work comes back to you with what is still wrong.
- A short team memory is read before every run.
- Each week, the Lead finds the weakest agent and proposes one prompt change. Nothing changes until you say yes.
- You compare v1, v2 and v3 side by side.
This post shows how I added these ideas to loop_graph.py. I kept the one rule that made Part 2 work: code decides if the work is done, not a model.
1. What I took and what I did not take
| From the prompt | In loop_graph.py |
|---|---|
| One job, input, output, NEVER, ASK ME FIRST per agent | Task fields prompt, input, output, never, ask_first. They are put together into one worker prompt. |
| A “done well” checklist of 3 to 5 checks | done_well: a list of named shell commands. |
| A Checker scores each result | Score = checks passed / total. The exit codes decide. No LLM grades anything. |
| A failed check goes back with the exact fixes | Only the failing checks, with their output, go back into the same session. |
| Max 2 quality rounds, then it comes to me | max_iters: 3 = the work + 2 fix rounds. Then the node is RED, and ESCALATION.md lists what is still wrong. |
| Edges labelled with what they carry | "needs": {"coder": "what was changed"}. The upstream answer goes into the downstream prompt with that label. |
| Parallel steps, then a join | Nodes with no unmet needs run at the same time. A node starts when all its needs are done. |
| The Lead routes with if/then rules | "when": "red": a node that runs only if its needs fail (triage, diagnosis). |
| A Mermaid flowchart plus a plain arrow version | --graph |
| A human pause before anything leaves | The Part 2 gate: --approve checks everything again, then runs final_cmd. |
| A short team memory, read before every run | <spec>.memory.md. It is capped at 40 lines and is put into every worker prompt. |
| Log every run, one success number | <spec>.runs.jsonl. The success number is the first-pass rate. |
| A weekly improvement: weakest agent, ONE prompt change, my yes | --improve proposes. --accept or --reject decides. |
| v1 → v2 → v3 side by side | Every --accept keeps the old spec as spec.v<N>.json. --report shows one column per version. |
What I did not take:
– The intake questions, the “launch to GitHub” step and the 7-day plan. These are for building a team from zero in a chat. My setup already exists.
– Antigravity and ADK specifics: /swarm, Orchestrator mode and ADK graph agents. The runner stays a small Python script that calls the claude or Cursor agent CLI.
– A model as the Checker. In the prompt, “scores every result” could mean an LLM judge. Here it means shell commands. An LLM judge that says “looks good” is the babysitting problem again, with an extra step.
– Installing the weekly automation. The prompt itself says “ask me before creating automations”. The timer is in section 6. You install it yourself.
2. An agent is a contract
Part 2 had a bare prompt and one check_cmd. A team member now looks like this:
{
"name": "coder",
"cwd": "/work/code",
"prompt": "Fix the bug in calc.py so the test passes.",
"output": "One sentence that says what you changed.",
"never": ["edit test_calc.py"],
"ask_first": ["installing packages"],
"done_well": [
{"name": "tests", "cmd": "python3 -c \"from calc import add; assert add(2, 3) == 5\""},
{"name": "test untouched", "cmd": "grep -q 'add(2, 3) == 5' test_calc.py"}
]
}
The runner puts these fields together into one prompt with fixed sections: JOB, INPUT, the handoffs, OUTPUT, DONE WELL (with the exact commands, so the worker knows how it will be judged), NEVER (git is always on this list), ASK ME FIRST and TEAM MEMORY.
Look at the second check. “Never edit the test” is in the NEVER list, but the NEVER list is only a request. The check test untouched enforces it. When a rule matters, make it a check.
ASK ME FIRST is a stop, not a request
A headless worker cannot ask you anything. The runner gives it a protocol instead: answer with one line, ASK ME FIRST: <question>, and stop. The runner finds that line with a regex. The node stops with status ASK, the gate stays closed, and the question goes into the escalation report.
This happened in my first live run, without a plan. My check used python3 -m pytest, and pytest was not installed for that interpreter. The worker fixed the bug. Then it saw that the only way to make the check pass was pip install --break-system-packages pytest into a shared Python. It did not install it. It stopped and wrote:
ASK coder (score 1/2, 2 iter, $0.305) - pytest isn't installed for the Homebrew
python3.13 that runs the check, and the only fix is installing it into that shared
interpreter (`pip install --break-system-packages pytest`). May I do that?
SKIPPED writer (score -, 0 iter, $0.000) - runs only when coder GREEN
The bad check was my fault, and the system caught it correctly. The downstream writer was skipped because its need did not end GREEN.
3. The checker and the quality loop
Each done_well check is a shell command. The exit code decides. The score is passed/total:
- Score first. If all checks already pass, no agent starts. The node is reported as “already green before work: are the checks meaningful?”. This comes from Part 2.
- Run the worker. Score again.
- If any check fails, the same session (
--resume) gets only the failing checks:
The checker scored your work 1/2. Fix exactly these failing checks:
- second: `test -f a2` fails. Its output (tail):
...
Fix the cause and stop; do not run git commands.
The output budget (4000 characters) is shared between the failing checks. One noisy check cannot push out the others.
- After
max_itersturns the node is RED. The default is 3: the first work, then the prompt’s “max 2 rounds” of fixes.
When the gate stays closed, the run writes ESCALATION.md in its log folder. It has one section for each RED or ASK node: the score, the failing checks, the question, and a link to the log. It also has the answer of any red-route node. “It comes to me with what is still wrong” is now a file, not something you find in the scrollback.
4. The graph: edges, joins and if/then routes
A task can name its upstream tasks:
{"name": "writer", "cwd": "/work/docs",
"needs": {"coder": "what was changed"},
"prompt": "Write CHANGELOG.md with one bullet that describes the change the coder made.",
"done_well": [{"name": "changelog", "cmd": "test -s CHANGELOG.md"},
{"name": "mentions add", "cmd": "grep -qi 'add' CHANGELOG.md"}]}
- Fan-out and join: the scheduler starts every node whose needs are done, runs them in a thread pool, and waits for the first one to finish (
FIRST_COMPLETED). Then it checks again. Nodes with no needs all start at once, as in Part 2. A node with several needs is a join. - Handoff: the writer’s prompt gets
HANDOFF from coder (what was changed):and the last 2000 characters of the coder’s final answer. The edge label tells the receiver what the text is. - If/then routing:
"when": "red"reverses the condition. Such a node runs only when its needs end RED. A typical use is a triage agent that reads the failure and explains it. Its answer goes intoESCALATION.md. A red route never opens the gate: a failure that was explained is still a failure. - Validation: unknown needs and cycles are refused before any agent starts.
--graph prints the shape. The node names are short, and quotes and brackets are removed from the labels so that Mermaid does not fail on them:
flowchart LR
coder -->|what was changed| writer
writer -->|all checks green| Gate
coder -.->|if red: failing checks| triage
Gate -->|you approve| Final
Paste this code at mermaid.live to see the picture.
coder --(what was changed)--> writer
writer --(all checks green)--> Gate
...
A dotted edge is a red route. Every node that no green-route node consumes points at the Gate, because its checks must be green before --approve lets anything out.
5. Team memory
<spec>.memory.md sits next to the spec. After each run, the Lead (the script) adds one line for each node that ran:
- 2026-11-25 v1 coder: ASK in 2 turn(s), score 1/2, failing: tests
- 2026-11-25 v1 coder: GREEN in 1 turn(s), score 2/2
- 2026-11-25 v1 writer: GREEN in 1 turn(s), score 2/2
A worker can also add a fact. Any line REMEMBER: <fact> in its answer is copied as <agent> learned: <fact>. Before the next run, the last 1500 characters are put into every worker prompt as TEAM MEMORY (read first). The file keeps only its last 40 lines. This memory is short on purpose: it is a sticky note for the team, not a knowledge base.
6. The improvement loop, with a human yes
Every run adds one record to <spec>.runs.jsonl: the spec version, the log folder, and the status, turns, score and cost of each node. Three commands work on it.
--report shows the success numbers side by side for each spec version: the first-pass rate (GREEN with at most one agent turn), the runs, the mean turns and the mean cost:
agent v1 v2
coder 50% (2, 1.5, $0.23) 100% (3, 1.0, $0.15)
writer 100% (1, 1.0, $0.13) 100% (3, 1.0, $0.12)
(The v2 column is an example of how the table looks. My live data has only v1.)
--improve finds the weakest agent of the current version. It ranks by the lowest first-pass rate, then the lowest green rate, then the most turns. Then it makes one headless call with no tools (--tools "", hooks off, and output that must match a JSON schema). The model gets the agent’s spec, its numbers and the end of its last log. It may change only prompt, input, output, or add one item to never or ask_first. It cannot touch the checks. The checks are your definition of done, and an optimiser that can change the definition of done will find the easy way out. The proposal is written to <spec>.proposal.json, and nothing else changes.
--accept copies the current spec to spec.v<N>.json, applies the one change, increases version, and writes a line to the team memory. A proposal made for an older version is refused as stale. --reject deletes it.
The live proposal from my run shows why this step needs a human:
weakest agent: coder (first pass 50% over 2 run(s), 1.5 turns)
proposed change to prompt:
Fix the bug in calc.py so the test passes. Editing calc.py is undoable and safe: do it
without asking. Then run the python3 check from DONE WELL to confirm it passes. Do not
ask first for either step.
why: Both failing runs ended with the agent asking or leaving the check unrun ...
(cost $0.066) Nothing changed. Apply: --accept Drop: --reject
It looks reasonable, but it is wrong. The ASK was the correct behaviour: the alternative was to install packages into a shared interpreter. The real fix was my check. I rejected the proposal. An improvement loop that applies its own changes would have trained the agent to stop asking.
To run the proposal step weekly, install a user timer yourself. It only proposes; it never accepts:
# ~/.config/systemd/user/loop-graph-improve.service
[Service]
Type=oneshot
ExecStart=/usr/bin/python3 %h/scripts/loop_graph.py %h/teams/my-team.json --improve
# ~/.config/systemd/user/loop-graph-improve.timer
[Timer]
OnCalendar=Mon 08:00
Persistent=true
[Install]
WantedBy=timers.target
systemctl --user enable --now loop-graph-improve.timer
On Monday you read my-team.proposal.json, then you --accept or --reject it.
7. What did not change
- The gate. A normal run never executes
final_cmd.--approveruns no agents. It runs every green-route check again and runsfinal_cmdonly if all of them pass. - Two runners.
runner: claude(claude -p --resume) orrunner: cursor(agent -p --resume). - Model guard. You choose the model explicitly. Model names on a deny list are refused for both the workers and the improvement call, and the improvement call also checks the
modelUsageof the result. - Old specs still work.
check_cmdis a one-itemdone_well. A spec with noneedsis the flat parallel graph from Part 2.
The whole script is about 500 lines of standard-library Python.
8. What I ran
First, with a stub in place of claude (no model calls), I ran one graph with five nodes:
– a node that needed two rounds to go from 1/2 to 2/2;
– a downstream node that got its handoff;
– a node whose check can never pass (RED after the cap);
– a red-route triage node that ran because of that failure;
– a node that answered ASK ME FIRST.
That run wrote the escalation report, the memory and the history correctly. The tests also covered --graph, --improve and --accept with a stubbed proposal, refusal of a stale proposal, refusal of a cycle and of a denied model, and --approve both refusing and running final_cmd.
The tests found two bugs:
– --improve ranked a node that turned GREEN on turn 2 as weaker than a node that was always RED. Ties now break on the green rate.
– --graph drew no gate edge for a node that only a red route consumed. Red routes no longer count as consumers.
Then I did live runs with claude (Sonnet) on throwaway files: a two-node graph, coder → writer.
– Run 1: ASK. My pytest check was broken, and the worker would not install packages (section 2).
– Run 2: both nodes GREEN in one turn. The coder fixed a - b. The writer used only the handoff and wrote Fix add in calc.py: it now returns a + b instead of a - b. Cost: $0.277. --approve then ran the final command.
– --improve: $0.066. The proposal was wrong, as described in section 6.
The sample is small, so read it as “the mechanism works”, not as a benchmark.
9. Rules of thumb
- Every “never” that matters must be a check. The prompt asks. The check enforces.
- 3 to 5 checks per agent. With one check, a worker can pass while it breaks something nearby. With ten checks, the fix prompt is mostly noise.
- Label every edge. “what was changed” tells the receiver how to read the text it gets.
- Use a red route for diagnosis, not for repair. A red route that tries to fix the work hides the failure from you.
- Read the proposal against the log before you accept it. The improvement model sees numbers. You know which “failures” were correct behaviour.
10. A usage prompt
You do not have to write the spec yourself. Give Claude Code (or Cursor) a prompt like the one below. It designs the team, writes the spec and shows you the graph. It does not run anything until you say yes.
Build an agent team for loop_graph.py (~/scripts/loop_graph.py; read its docstring for the spec format).
GOAL In ~/work/billing-api, add pagination to GET /invoices, test it, and document it.
TEAM 3 to 5 agents, each with one job. For each agent give: prompt (the JOB), input, output, never,
ask_first, and 3 to 5 done_well checks. Every check is a shell command that exits 0 only when the
work is right. No check may pass before the work is done. Every "never" that matters must also
be a check (for example, "do not edit existing tests" -> a git diff check on tests/).
GRAPH Independent agents have no needs, so they run in parallel. Label every edge with what it
carries. Add one "when": "red" triage agent after the coder that explains a failure but fixes
nothing. Give agents that edit files separate working directories or separate files.
LIMITS runner claude, model sonnet, max_iters 3, final_cmd "git -C ~/work/billing-api push".
STEPS 1. Write the spec to ~/teams/billing-pagination.json.
2. Run every done_well check once now, and show me that each check FAILS before any work.
3. Run `loop_graph.py <spec> --graph` and show me the Mermaid and arrow versions.
4. Stop and wait. Do not run the graph, --approve or --accept until I say so.
The spec it writes looks like this (shortened):
{
"runner": "claude", "model": "sonnet", "max_iters": 3,
"final_cmd": "git -C ~/work/billing-api push", "final_cwd": "/home/me/work/billing-api",
"tasks": [
{"name": "coder", "cwd": "/home/me/work/billing-api",
"prompt": "Add page and size query parameters to GET /invoices in api/invoices.py.",
"output": "The new parameters and their defaults, in two lines.",
"never": ["edit existing tests", "change the response schema of other endpoints"],
"ask_first": ["adding a dependency", "changing the database schema"],
"done_well": [
{"name": "unit tests", "cmd": "pytest -q tests/test_invoices.py"},
{"name": "has paging", "cmd": "grep -q 'page' api/invoices.py"},
{"name": "old tests untouched", "cmd": "git diff --quiet HEAD -- tests/test_invoices.py"}]},
{"name": "tester", "cwd": "/home/me/work/billing-api", "needs": {"coder": "the new parameters"},
"prompt": "Add tests for the page and size parameters in tests/test_invoices_paging.py.",
"done_well": [
{"name": "new tests pass", "cmd": "pytest -q tests/test_invoices_paging.py"},
{"name": "at least 3 tests", "cmd": "test $(grep -c '^def test_' tests/test_invoices_paging.py) -ge 3"}]},
{"name": "docs", "cwd": "/home/me/work/billing-api", "needs": {"coder": "the new parameters"},
"prompt": "Document the page and size parameters in docs/api.md.",
"done_well": [{"name": "documented", "cmd": "grep -q 'page' docs/api.md && grep -q 'size' docs/api.md"}]},
{"name": "triage", "cwd": "/home/me/work/billing-api", "needs": {"coder": "the failing checks"}, "when": "red",
"prompt": "Explain why the coder's checks fail. Write the cause to TRIAGE.md. Change no other file.",
"done_well": [{"name": "wrote triage", "cmd": "test -s TRIAGE.md"}]}
]
}
Step 3 of the prompt (--graph) shows this:
flowchart LR
coder -->|the new parameters| tester
tester -->|all checks green| Gate
coder -->|the new parameters| docs
docs -->|all checks green| Gate
coder -.->|if red: the failing checks| triage
Gate -->|you approve| Final
cwd must be an absolute path: the script does not expand ~. tester and docs both need only coder, so they run in parallel after it. They edit different files in one tree, which is safe here. Two agents that edit the same file need separate working directories.
Then you run it yourself:
python3 ~/scripts/loop_graph.py ~/teams/billing-pagination.json # run, stop at the gate
python3 ~/scripts/loop_graph.py ~/teams/billing-pagination.json --approve # re-check, then push
python3 ~/scripts/loop_graph.py ~/teams/billing-pagination.json --improve # after a few runs
For the weekly review, a short prompt is enough:
Run loop_graph.py ~/teams/billing-pagination.json --report and --improve. Read the proposal
against the weakest agent's last log. Tell me if the "failures" were real failures or correct
behaviour (for example, a correct ASK ME FIRST). Recommend accept or reject. Do not run --accept.
Series: Loops and Graphs in Antigravity (Part 1) · Part 2: deterministic checks and a final approval gate.