Step 1: Read the model's code
Read the model's code
A coding agent runs a loop a developer knows well: write tests, write code, run the tests, read the failures, try again. You will build that loop. It starts with the dullest but most necessary piece, getting runnable code out of a model reply, because everything downstream runs what the model returns.
harness.py gives you ask(prompt) (the model, cached), SPEC (the task: a leap-year function),
HOLDOUT_TESTS, and run(solution_src, test_src) which executes tests against a solution in a separate
process and reports {"passed", "failed", "total", "error"}. You write the agent in agent.py.
Write extract_code(reply): if the reply contains a ``` fence, return the first fenced block (dropping a
leading python tag); otherwise return the whole reply, stripped. Run it on a fenced and a bare reply.
agent.py, the file you edit46 lines
"""Your coding agent. Given a spec, it writes tests, writes a solution, runs the tests, and feeds the
failures back to itself until the tests pass, the loop a developer runs by hand. Then it checks the result
against held-out tests to be sure the solution solves the problem beyond the visible examples."""
import harness
# ---------- Step 1: read the model's code ----------
def extract_code(reply):
"""Pull the code out of a model reply. If it is fenced in ``` (optionally ```python), return the first
fenced block; otherwise return the whole reply, stripped."""
# TODO (Step 1): if the reply has a ``` fence, return the first fenced block (drop a
# leading 'python' tag); otherwise the stripped reply.
raise NotImplementedError("Step 1: write extract_code()")
# ---------- Step 2: write tests from the spec ----------
def write_tests(spec, ask=harness.ask):
"""Ask the model for test functions (named test_*) that encode the spec, and return the code. The tests
call the function directly, so they do not import it."""
raise NotImplementedError("write_tests() arrives in Step 2")
# ---------- Step 3: write a solution ----------
def write_solution(spec, ask=harness.ask, feedback=""):
"""Ask the model to implement the function. If feedback from a failed run is given, include it so the
model can fix its previous attempt. Return the code."""
raise NotImplementedError("write_solution() arrives in Step 3")
# ---------- Step 4: the agent loop ----------
def solve(spec, ask=harness.ask, max_iters=4):
"""Write tests once, then write a solution and run it, feeding the failures back and trying again until
the tests pass or max_iters is reached. Return {"code", "tests", "passed", "iters", "history"}."""
raise NotImplementedError("solve() arrives in Step 4")
# ---------- Step 5: guard against overfitting ----------
def holdout_check(solution_src):
"""Run the held-out tests the agent never saw against a solution, and return the names of any that
fail. An empty list means the solution generalises beyond the visible examples."""
raise NotImplementedError("holdout_check() arrives in Step 5")harness.pytry_it.py