Step 1: Reproduce the bug
A bug report lands: median() returns the wrong answer for lists with an even number of items. The code and
its test suite are in harness.BUGGY and harness.TESTS. The first rule of debugging is the one everyone
skips under pressure: reproduce the failure before you touch anything. A bug you cannot reproduce is a bug
you cannot fix, and cannot prove you fixed.
Write two functions in agent.py:
extract_code(reply): pull code out of a model reply, taking the first ``` fenced block (dropping an optionalpythontag) or the whole reply when there is no fence. You will need it from Step 2 on.reproduce(buggy_src, tests_src): run the suite withharness.runand return the failure record. If nothing fails, raiseValueError: there is no bug to reproduce, so there is nothing to debug.
Run it: two even-length tests fail, exactly the reported symptom.
agent.py, the file you edit48 lines
"""Your debugging agent. It reproduces a failure, asks a model for a fix, verifies the fix against the whole
suite, loops until green, and then insists on a regression test that fails on the old code, the discipline
that stops the same bug coming back."""
import harness
def extract_code(reply):
"""Pull code out of a model reply: the first ``` fenced block (dropping an optional python tag), or the
whole reply stripped when there is no fence."""
# TODO (Step 1): first ``` fenced block (drop a leading 'python'), else the whole reply stripped.
raise NotImplementedError("Step 1: write extract_code()")
def reproduce(buggy_src, tests_src):
"""Run the suite against the buggy code and return the failure. Raise if nothing fails: you cannot fix a
bug you cannot reproduce."""
# TODO (Step 1): run the suite on the buggy code; return the failure, raise ValueError if nothing fails.
raise NotImplementedError("Step 1: write reproduce()")
def propose_fix(buggy_src, failure, ask=harness.ask, feedback=""):
"""Ask the model to fix the code given the failing tests. Fold in feedback from a previous attempt.
Return the extracted code."""
raise NotImplementedError("propose_fix() arrives in Step 2")
def verify(fixed_src, tests_src):
"""Run the whole suite against a candidate fix. Returns {"passed", "failed", "total", "error", "ok"}
where ok is True only when every test passes."""
raise NotImplementedError("verify() arrives in Step 3")
def debug(buggy_src, tests_src, ask=harness.ask, max_iters=3):
"""Reproduce, then propose a fix and verify it, feeding the still-failing tests back until the suite is
green or the budget runs out. Returns {"code", "fixed", "iters", "history"}."""
raise NotImplementedError("debug() arrives in Step 4")
def guards_bug(test_src, buggy_src, fixed_src):
"""Does this test actually guard the bug? A real regression test fails on the buggy code and passes on
the fixed code. Returns True only when both hold."""
raise NotImplementedError("guards_bug() arrives in Step 5")
def add_regression_test(buggy_src, fixed_src, ask=harness.ask):
"""Ask the model for a new test_* that captures the bug, then keep it only if it actually guards the bug
(fails on the old code, passes on the new). Returns {"test", "guards"}."""
raise NotImplementedError("add_regression_test() arrives in Step 5")harness.pytry_it.py