Step 1: Pin the behavior
You have been handed total(items, member, coupon) in harness.LEGACY: an order-total function with a
member discount, two coupons, and years of accumulated mess. Your job is to make it readable without
changing what it does. Before you touch a line, pin down exactly what it does now, quirks and all, because
that behaviour is the contract your customers already depend on.
This is a characterization test: you record what the code does today, whatever it was meant to do.
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 in Step 3.characterize(src, fn, inputs): runsrcon every input withharness.run_callsand return a list of{"input": args, "result": record}. This golden set is the pinned behaviour.
Run it: watch that SAVE20 only takes money off once the subtotal is over 100. That is a quirk, and it is
now part of the contract.
agent.py, the file you edit43 lines
"""Your refactoring agent. It pins the legacy behaviour with a characterization suite, asks a model to
clean the code up, and rejects any refactor that changes a single output. The tests are the contract:
a refactor that changes behaviour, even to something 'more correct', is not a refactor, it is a bug."""
import harness
def extract_code(reply):
"""Pull the 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 characterize(src, fn, inputs):
"""Pin the current behaviour: run src on every input and record what it returns. This golden set is the
contract the refactor must preserve, quirks included."""
# TODO (Step 1): run src on each input via harness.run_calls; return [{input, result}] - the pinned golden behaviour.
raise NotImplementedError("Step 1: write characterize()")
def find_regressions(candidate_src, fn, golden):
"""Run a candidate on the golden inputs and return every input whose result differs from the pinned one.
An empty list means behaviour is preserved."""
raise NotImplementedError("find_regressions() arrives in Step 2")
def refactor(src, ask=harness.ask, feedback=""):
"""Ask the model to refactor src for readability without changing behaviour. If a previous attempt
changed behaviour, pass the regressions back so it can correct them. Return the extracted code."""
raise NotImplementedError("refactor() arrives in Step 3")
def safe_refactor(src, fn, ask=harness.ask, inputs=None, max_iters=3):
"""Characterize the legacy code once, then refactor and check against the golden set, feeding any
regressions back and retrying until the refactor is behaviour-preserving or the budget runs out.
Returns {"code", "preserved", "iters", "regressions", "history"}."""
raise NotImplementedError("safe_refactor() arrives in Step 4")
def accept_refactor(original_src, candidate_src, fn, inputs=None):
"""The gate: accept a candidate only if it reproduces the original's behaviour on every input.
Returns {"accepted", "regressions"}."""
raise NotImplementedError("accept_refactor() arrives in Step 5")harness.pytry_it.py