All courses

Workflow: fixing a bug from an error message

Fix a bug end to end. Give the exact error and how to reproduce it, have it reproduce the failure as a test first, review the root-cause explanation, apply the minimal fix, and confirm the test passes and nothing else broke.

Builds on: Giving the right context: files, errors, examples, Tests as the definition of done

Read it card by card Start the whole course

A crash, and the urge to say 'fix it'

You run `python src/report.py data/march.csv` and get a traceback ending in `ZeroDivisionError: division by zero`. You open the agent and are tempted to type: 'the report is broken, please fix it.' The agent will probably produce a change that makes the crash go away. How would you know it fixed the actual bug, rather than hiding it or breaking something else?

Make the bug visible, then make it go away

The agent can only fix what it can see happen. If you give it the exact error and the command that triggers it, it can rerun the failure instead of guessing. Capturing that failure as a test that expects the *right* result turns 'fixed' into something it can check. The test fails now, for the reason you saw, and must pass afterwards. Knowing the **root cause** (why the code reaches that line in a bad state) lets you judge whether the fix targets the cause or only hides the symptom.

The five steps of a bug fix

1. **Evidence:** paste the exact error output, the command that triggers it, and what should happen instead. 2. **Reproduce:** ask for a test that expects the correct result and fails now. Check that it fails with *your* error, not a typo or an import problem. 3. **Root cause:** have the agent explain it before changing code. Check it against the stack trace. 4. **Smallest fix:** leave the test untouched. 5. **Confirm:** run the new test and the whole suite, read the diff, and rerun your command.

The first message you send

One message carries the evidence, the expected result and the order of work. Pasting the key traceback lines verbatim lets the agent find the line and rerun the failure.
▸ You: This command crashes:
Command: python src/report.py data/march.csv
  File "src/report.py", line 14, in average
    return total / len(values)
ZeroDivisionError: division by zero
It happens when a category has no rows.
Expected: show "n/a" for that category.
First add a test to tests/test_report.py
that expects "n/a" for an empty category.
Run it and show me it fails with the same
error. Then explain the root cause before
changing code. Then make the smallest fix,
don't edit the test, and run the whole
test suite.

What the agent does, and what you check

⚙ Edit: `tests/test_report.py` adds `test_empty_category`, which expects "n/a". ⚙ Run: `pytest tests/test_report.py` fails with `ZeroDivisionError` at line 14. That is your failure, so the bug is captured. ◂ Agent: 'Root cause: `average` divides by `len(values)` and gets an empty list for categories with no rows.' This matches the trace. The fix makes `average` return "n/a" for an empty list. ⚙ Run: `pytest` shows all tests pass. The diff touches only `average` and the new test. Finally you run `python src/report.py data/march.csv` yourself, and the empty category shows `n/a`.

Remember

Give the exact error and how to trigger it. Get a test that expects the right result and fails for *that* reason. Check the root cause against the trace. Make the smallest fix. Confirm that the new test and all old tests pass and that your command now works. A test you never saw fail proves nothing about this bug.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.