Force agents into a test-first repair loop
Agents thrash when they “fix” by editing implementation until something looks plausible. Without a failing test, you cannot tell progress from churn.
Agents thrash when they “fix” by editing implementation until something looks plausible. Without a failing test, you cannot tell progress from churn. They rename symbols, tweak sleeps, and weaken assertions.
A test-first repair loop forces evidence: reproduce with a failing test, change code, re-run, and stop when the budget is gone.
Ready for a fix loop (or stop)

Working loop
Ready when:
- Observed vs expected behavior is written down.
- A failing automated test or minimal reproduction exists.
- The failure is for the right reason, not a broken harness.
- An attempt budget is set (for example three implementation tries).
- Deleting or skipping tests is forbidden as a “solution”.
Do not edit production code yet when there is no red signal, the bug is only guessed from logs, or the agent proposes a redesign before the failing test is green.
Workflow: fail → fix → re-run with a stop budget

Failure modes to catch early
- State the bug. Observed vs expected, env, inputs, logs.
- Require a red signal. Failing test or minimal reproduction before any production edit. The agent can write the test; a human confirms the assertion matches the real bug.
- Confirm the right fail. Broken harnesses do not count as progress.
- One focused implementation change. Re-run. If still failing, permit one more hypothesis-driven edit and log the hypothesis in the PR.
- Stop after the budget. Escalate with the failing test intact. Do not raise the attempt count without a new plan.
When the bug is only in production, build the best local reproduction you can (fixture, recorded traffic, feature flag) and still write a regression test once you understand the trigger. Environmental bugs still need a characterization script, even a shell repro in scripts/.
Stop gates
- Production edits before a failing test exists.
- Tests changed to match buggy behavior.
time.sleepor retries added to “stabilize” races.- Unrelated modules rewritten while the original test still fails.
Checklist before merge
- Failing test committed or shown in CI logs.
- Attempt budget defined and enforced.
- No test deletion without human approval.
- Hypotheses written when retries happen.
- Escalation path clear after budget exhaustion.
Three ways the loop collapses
- Edit before red. Production changes with no failing test. Fix: make “failing test shown” a checklist or CI label before implementation review.
- Weaken the test. Assertions softened or deleted. Fix: reject the PR; keep the red signal honest.
- Sleep as fix. Retries hide races. Fix: require a root-cause fix; flakes stay otherwise.
Bottom line

When this fits vs when it does not
The loop makes verification the spine of the session. Red first, focused fix, re-run, stop. Pair this with context budgeting so the agent does not thrash across unrelated packages, and with PR description quality so the verification section quotes the commands that turned red into green.
Share