Start learning free

LESSON 96 OF 129 · AI ENGINEERING: BUILD YOUR OWN CODING AGENT

Fix a Failing Test in Python

Practice

Your agent can list files, read them, write them and run the tests. That is enough for a real task: "The tests are failing. Find the bug and fix it." The loop stays as it is. The model picks the next tool, your code runs it, and this repeats until the model stops asking for tools.

Keep a trace: to see afterwards what the agent did, keep a list and add a name each time something happens. The length of the list is the number of the latest step, and join prints the whole path on one line.

pages = []
pages.append("home")
pages.append("cart")
print("Page " + str(len(pages)) + ":", "cart")   # Page 2: cart
print("Path:", " > ".join(pages))               # Path: home > cart

A claim is not proof: when the loop ends, the model's answer says the bug is fixed. A model can say done while a test still fails. So your code runs the tests itself after the loop and reads the report. A report whose tests all pass ends with OK, and endswith answers with True or False: "invoice.pdf".endswith(".pdf") is True.

The finished program prints every step, the trace and your own verdict:

Model: I'll look at the project first.
Step 1: list_files {}
Model: I'll read the README and cart.py.
Step 2: read_file {'path': 'README.md'}
Step 3: read_file {'path': 'cart.py'}
Model: Before I change anything, I'll run the tests.
Step 4: run_tests {}
Tests: FAILED (failures=1)
Model: test_exactly_fifty_dollars_ships_free fails: … I'll change > to >= in cart.py.
Step 5: write_file {'path': 'cart.py', 'text': 'FREE_SHIPPING_FROM = 5000\nSHIP...
Wrote: cart.py
Model: Now I'll run the tests again.
Step 6: run_tests {}
Tests: OK
Answer: Fixed. … I changed > to >= in cart.py, and all 6 tests pass now.
Trace: list_files > read_file > read_file > run_tests > write_file > run_tests
Tests: OK
Tests pass after the run: True

Your task: the tools and the loop are already written. Create the list trace. In run_tools, add each tool's name to it and print a numbered Step line. After the answer, print the trace on one line. Then call run_tests yourself and print whether its report ends with OK.


Assignment
Follow the Coding Tutorial to add the trace to the agent's loop and to check the model's answer with your own run of the tests.


Hint
Append to trace before you print the step, so that len(trace) is already the number of this step. run_tests expects one argument, its input: give it an empty dictionary, {}.


STEP 1 OF 6
    Ctrl + Enter to check

    Ai Agent Fix Loop in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free