Sub topic 3
Verify the Fix with a Test
This chapter assumes you can run Python code and tests, read a traceback, and use basic Git commands. The goal is to verify a proposed fix instead of trusting an assistant’s explanation.
Verify the fix with a test
Suppose the assistant changes average so an empty list produces a clear error:
def average(values):
if not values:
raise ValueError("average() requires at least one value")
return sum(values) / len(values)Run the original failing case first. Then test both the failing input and a normal input:
import pytest
from stats import average
def test_average_of_numbers():
assert average([2, 4, 6]) == 4
def test_average_of_empty_list_raises():
with pytest.raises(ValueError, match="at least one value"):
average([])A passing test shows that the observed behavior matches the test’s expectation. It does not prove that every input is correct. Check the diff and run the wider test suite as well.
Iterate when the first fix does not work
If the test still fails, give the assistant the new output, not a restatement of the original request. Include:
- The exact command you ran.
- The complete failure output.
- The current code or relevant diff.
- What result you expected.
Ask it to update its diagnosis from the new evidence. Do not apply several speculative fixes at once. Each change should have one reason and one verification step.
Record the root cause
After the test passes, write down why the bug occurred. For this example, the root cause is that the function divided by len(values) without defining behavior for an empty input. Add the regression test to version control with the fix. The test prevents a future edit from silently removing the empty-input check.
Let the assistant run commands safely
An agentic assistant can run tests and inspect files, but limit its permissions. Start with read-only commands and a test command. Review proposed edits before allowing writes. Do not expose credentials, private customer data, or production access. Never approve an unexplained command that deletes files, changes permissions, deploys code, or sends data.
Know when to debug by hand
Stop prompting when the assistant repeats a disproved theory, invents APIs, or suggests broad rewrites without evidence. Read the code, trace the values, add temporary logging, or use a debugger. Human inspection is often faster when the failure depends on local state, timing, undocumented behavior, or a small algorithmic mistake.
Exercise
What should you provide after a proposed fix still fails its test?
Show answer
Provide the exact command, complete new output, current code or diff, and expected behavior, then ask for a diagnosis based on that evidence.