Lesson 11 / 25

Debugging with an Assistant

Combine stack traces, hypotheses and experiments to find and fix bugs faster.

Hypothesis, experiment, result

Give the assistant the stack trace, the code around it and what you expected versus what happened. Ask for several possible causes ranked by likelihood, then test them one by one with a print, a log or a failing test. Feed each result back. Avoid accepting a "fix" you do not understand: a patch that makes the symptom vanish may hide the real bug.

A bug an assistant often misses

A mutable default argument is shared between calls. I ran this: the buggy version returns the same list twice, the fixed version gives independent lists.

def add_item(x, bucket=[]):          # buggy: one shared list
    bucket.append(x); return bucket
print(add_item(1), add_item(2))

def add_item_ok(x, bucket=None):
    bucket = [] if bucket is None else bucket
    bucket.append(x); return bucket
print(add_item_ok(1), add_item_ok(2))

Output:

[1, 2] [1, 2]
[1] [2]

Write a failing test first

Reproduce the bug as a test before fixing it. The test proves the fix worked and stops the bug coming back, and an agent can use it to confirm its own patch.

Quick check: Why avoid accepting a fix you do not understand?

  • It makes the editor slower
  • Fixes are always wrong
  • Git forbids it
  • It may hide the real bug while removing the symptom
Answer

It may hide the real bug while removing the symptom — Understanding the cause is how you know the problem is truly solved.