Plant a bug before the reviewer sees the diff
My dev loop uses a different model for each step. Sonnet 5.5 writes the plan, DeepSeek v4.1 Flash writes the code, and GPT-6.2 Astra reviews the diff and either passes it or sends it back. Grok 4.7 takes screenshots or a video as proof, and Claude opens the PR. Claude Code runs the whole thing, and pi is the only other tool in it.
A loop like that produces PRs every day, and the PRs mostly look fine. What it doesn’t tell me is which step is doing the work. If the reviewer approved everything for a month, the only sign would be a few more bugs in production. So I wanted the simplest eval that scores each step on its own, using DSPy, and that I can rerun after swapping one model.
This post is that eval, plus a small test run of it where local models stand in for the real ones. The code is short, and the toy run already turned up one useful number.
The loop writes a trace, the eval reads it
The eval never calls the loop’s models and doesn’t care how the loop is built. It needs one JSON file per run. Claude Code already sees every step, so the last thing it does in a run is write that file:
{
"task_id": "intervals",
"plan": "1. Sort by start ...",
"rounds": [
{"round": 1, "diff": "--- a/intervals.py ...", "planted_bug": null,
"verdict": "SEND_BACK", "review": "Line 11 compares against merged[-2] ..."},
{"round": 2, "diff": "...", "verdict": "PASS", "review": "..."}
],
"final_code": "...",
"proof": {"notes": "...", "visible_test_output": "..."},
"usage": {"prompt_tokens": 2114, "completion_tokens": 750},
"seconds": 82.6
}
Every task in the eval set has a short prompt, a starter file, one visible test the loop can see, and a hidden pytest file the loop never sees. The hidden tests are the hard signal. Everything else in the score is softer.
Planting a bug before review
Hidden tests tell you whether the final code works. They don’t tell you whether the reviewer did anything, because a good implementer and a reviewer that approves everything give the same green result.
So for a share of runs, a wrapper edits the implementer’s code before the reviewer sees it. It parses the file with ast, picks one small mutation and applies it to that line. The mutations are small, one-token changes: < becomes <=, a not goes missing, items[0] becomes items[1], a + becomes a -. The wrapper keeps a mutation only if it makes a hidden test fail that passed before, and it prefers mutations that still pass the visible test. The reviewer then gets a diff with exactly one bug I know about.
def plant_bug(code, task_dir, mod, rng):
hidden = os.path.join(task_dir, "hidden", "test_hidden.py")
base_failed = count_failed(code, mod, hidden)
order = list(range(len(candidate_mutations(code))))
rng.shuffle(order)
fallback = None
for k in order:
node, kind, fn = candidate_mutations(code)[k]
mutated, before, after, *_ = apply_mutation(code, node, fn)
if count_failed(mutated, mod, hidden) <= base_failed:
continue # the mutation has to break something the tests check
visible_ok, _ = run_pytest(mutated, mod, os.path.join(task_dir, "starter", "test_visible.py"))
bug = {"id": f"{kind}@L{node.lineno}", "before": before, "after": after,
"visible_test_still_passes": visible_ok}
if visible_ok:
return mutated, bug
fallback = fallback or (mutated, bug)
return fallback or (None, None)
If the first review sends that diff back, the reviewer caught it. The count of caught bugs over planted bugs is the reviewer’s recall, as a plain number. Running the same tasks with no planted bug gives the other half: how often the reviewer sends back code that was already correct.
In my loop the wrapper would sit between the DeepSeek step and the GPT-6.2 Astra call. It has to record the planted bug in the trace, so the eval knows what to look for.
The DSPy part
The softer scores come from one DSPy signature, used once per stage through dspy.ChainOfThought. It gets the task, what the stage was given and what it produced, and returns typed fields:
import dspy
class JudgeStage(dspy.Signature):
"""Judge one stage of a coding loop against the task and the previous stage's output."""
task: str = dspy.InputField()
stage: str = dspy.InputField(desc="plan | implement | proof")
upstream: str = dspy.InputField(desc="what this stage was given")
output: str = dspy.InputField(desc="what this stage produced")
followed_upstream: bool = dspy.OutputField()
problems: list[str] = dspy.OutputField()
score: float = dspy.OutputField(desc="0 to 1")
judge = dspy.ChainOfThought(JudgeStage)
The metric combines the hidden tests, the planted-bug check and the three judge scores. The weights are my guess and easy to change:
def loop_metric(example, pred, trace=None):
t = pred.trace
tests_ok = hidden_tests_pass(t) # runs the hidden pytest file on t["final_code"]
caught = None
if t.get("planted_bug"):
caught = t["rounds"][0]["verdict"] == "SEND_BACK"
stages = [
judge(task=example.task, stage="plan", upstream=example.task, output=t["plan"]),
judge(task=example.task, stage="implement", upstream=t["plan"], output=t["final_diff"]),
judge(task=example.task, stage="proof", upstream=t["final_diff"],
output=t["proof"]["notes"] + "\n\nTest output:\n" + t["proof"]["visible_test_output"]),
]
soft = sum(min(max(float(s.score), 0.0), 1.0) for s in stages) / len(stages)
return 0.5 * tests_ok + 0.3 * (1.0 if caught is None else float(caught)) + 0.2 * soft
dspy.Evaluate wants a program to call, and here the program under test is the loop, which already ran. So the “program” is a module that loads the trace file:
class ReadTrace(dspy.Module):
def forward(self, trace_path):
return dspy.Prediction(trace=json.load(open(trace_path)))
devset = [dspy.Example(trace_path=p, task=task_for(p)).with_inputs("trace_path") for p in trace_paths]
evaluate = dspy.Evaluate(devset=devset, metric=loop_metric, num_threads=2, display_progress=True)
result = evaluate(ReadTrace())
print(result.score)
The workflow is to run the loop over the task set, run this, change one thing in the loop and run both again. These calls are checked against DSPy 3.5.0.
What I ran
The test run did not use the models from my loop. It used a stand-in loop with local Qwen2.5-Coder models served by Ollama on an 8-core CPU box with 15 GB of RAM. The planner was Qwen2.5-Coder 3B, the implementer was the 1.5B, and the proof step ran the visible test and had the 1.5B write notes about the output. The reviewer was the 3B in the baseline, nothing in one ablation, and the 7B in the other. The DSPy judge was the 3B at temperature 0. The loop allows three review rounds.
The tasks are six small Python functions: a slug function, a duration parser, list chunking, interval merging, an LRU cache and a word counter. Each hidden test file has five to seven tests. I got 54 traces in total, which is small, so read the numbers below as a demo of the method and not as a benchmark of these models.
| Reviewer | Planted bugs sent back | Already-broken diffs sent back | Correct diffs sent back | Clean runs passing hidden tests |
|---|---|---|---|---|
| Qwen2.5-Coder 3B | 0 of 14 | 0 of 10 | 0 of 6 | 6 of 12 |
| Qwen2.5-Coder 7B | 3 of 3 | 2 of 6 | 0 of 3 | 3 of 6 |
| No reviewer | n/a | n/a | n/a | 2 of 6 |
The 3B reviewer returned VERDICT: PASS on all 30 first-round reviews, and in every one of them that line was the whole review. It passed a <= flipped to < in the interval merge, which stops touching intervals from merging. It also passed the word counter’s tie-break sort key changed from item[0] to item[1]. On top of the planted bugs, it passed ten diffs where the implementer’s own code already failed the hidden tests.

Without the planted bugs this reviewer would have looked fine. Clean runs passed the hidden tests 6 of 12 times with it and 2 of 6 times without it, which says little at this sample size. Nothing in the trace was red either. The planted bugs turned the problem into a number, 0 of 14, after about 15 minutes of runs.
The 7B reviewer sent back all three planted bugs it saw, including the LRU one that the visible test didn’t catch. It also sent back two of six diffs that were broken on their own, and none of the correct ones. It cost more: 1.67 review rounds per run against 1.0, and a median of 83 seconds per run against 38. The 1.5B implementer then fixed only one of the three caught planted bugs within three rounds. A reviewer can only send code back, so the next number to watch is what the implementer does with the feedback.
Planted runs with no reviewer behaved as expected: 6 of 6 shipped the bug, and none passed the hidden tests.
What the judge saw
I ran the DSPy judge over 36 of the traces: 24 from the 3B-reviewer baseline and all 12 from the 7B-reviewer run. I stopped before the no-reviewer traces to save time. The judge was the same 3B model as the weak reviewer, and it behaved the same way.
The hidden tests failed in 27 of those 36 runs. The judge said the implementation followed the plan in all 27, with a mean implement score of 0.86. The nine runs that passed the hidden tests got a mean of 0.80, so the score ran slightly backwards. On the interval merge with the planted <, the judge gave 1.0 and listed no problems.
The full metric still separated the two reviewers. dspy.Evaluate gave the 7B-reviewer run 68.4 and the judged baseline traces averaged 42.1. All of that gap came from the hidden tests and the planted-bug check. The judge scores added roughly 0.18 to every run, whatever the code did.
A judge needs the same check as the reviewer. Run it on traces where you already know the answer, like the planted-bug runs, and confirm its score drops before you give it any weight. Moving the judge to a stronger model is one line in dspy.configure, and the rest of the eval stays the same.
The proof step and its limit
In my loop the proof is screenshots or a video from Grok 4.7. JudgeStage takes text, so the judge can read Grok’s notes and the list of files, but it can’t watch the video. A cheap check that still works is to have the proof step write down which route or component it opened, and compare that with the files in the diff. A judge that looks at the frames needs a vision model, plus a few known-bad recordings to test it on, the same way the planted bugs test the reviewer.
Things to keep in mind
The tasks are toys and the models are small. The sample is 54 runs on one machine. With 3 planted bugs in the 7B row, “3 of 3” says the reviewer works at all, and not much more. The timings include Ollama loading and unloading models on a 15 GB box, so treat them as rough. Some planted runs got no bug, because the implementer’s code had nothing the mutator could break that the tests checked; those count as broken or correct diffs in the table.
For a real loop I would start with 15 to 20 tasks taken from my own merged PRs, with the hidden tests written from the merged code. I’d plant bugs in about a third of the runs and run the baseline twice to see how much the numbers move by chance. After that, I’d change one model at a time.
The code is under 300 lines across loop.py (the stand-in loop and the bug planter) and eval_loop.py (the DSPy part). Only the second file matters for a real loop. The first exists only to produce traces for testing the second.