1795921395075

A US-based startup I do freelance work for sent me an urgent message: "Reports aren't going out. Please fix ASAP." They had 270 subscribed clients. Only one of them was getting a report.

No crash. No stack trace. No red lines in the logs. The job ran, finished "successfully", and quietly did 1/270th of its work.

The cause turned out to be a single indentation change made by an AI coding tool. One line moved four spaces to the left.

The Code That Worked

The reporting job looked roughly like this. The real loop had about 100 lines of business logic in the middle.

j = None
for i in range(len(subscribed_clients)):
    ..
    # (complex logic of 100 lines)
    ..
    j = current_client


    send_report(j)

send_report(j) sits inside the loop. Every client goes through the logic, and every client gets a report. 270 clients, 270 reports.

The Code After the AI Tool Touched It

During an unrelated change, an AI tool reformatted this block. When it was done, the last line looked like this:

j = None
for i in range(len(subscribed_clients)):
    ..
    # (complex logic of 100 lines)
    ..
    j = current_client


send_report(j)

send_report(j) is now outside the loop. The loop runs 270 times and keeps overwriting j. When the loop ends, j holds only the last client. Then send_report(j) runs once.

Result: the last client got their report. The other 269 got nothing.

Why Nobody Noticed

This bug is quiet. Several things worked together to hide it.

Python is fine with it In Python, indentation is the syntax. Moving a line out of a block is not an error; it is a different, perfectly valid program.

j = None hid the problem Because j was set before the loop, it always exists after the loop. Without that line, linters like Pylint can warn about a loop variable used outside its loop. With it, there's nothing to flag.

The blank lines made it easy to miss Two empty lines sat between j = current_client and send_report(j). After 100 lines of logic, a reviewer's eye doesn't measure four spaces of whitespace after a gap.

The job still "succeeded" One report was sent. No exception, exit code 0, the scheduler showed green. From the outside, everything looked healthy.

The diff looked like formatting The AI change touched many lines for formatting. One whitespace change in a sea of whitespace changes looks like noise, not a logic change.

How I Found It

I didn't start with the code. I started with the numbers.

Step 1: Count what went out I compared the number of reports sent with the number of subscribed clients. 1 vs 270. That one number ruled out a lot. This wasn't a mail server outage or a bad email template. If delivery were broken, zero reports would go out. Exactly one meant the logic was running, just not enough times.

Step 2: Check which client got it The one client who received a report was the last one in the subscriber list. That's a strong signal. "Only the last item" almost always means something that should happen inside a loop is happening after it.

Step 3: Add a log line inside the loop I added logging for each client the loop processed and another one at the send_report call. The loop logged 270 times. send_report logged once. So the loop was fine; the send call wasn't in it.

Step 4: Look at the history I checked the git history for the file. The last commit came from an AI-assisted change. I looked at the diff with whitespace changes visible (no -w flag) and there it was: send_report(j) had lost its indentation.

Step 5: Confirm before fixing I wrote a quick test with three fake clients and a mocked send_report. It was called once. That proved the bug exactly.

The Fix

The immediate fix was one line: indent send_report(j) back into the loop. I tested it with the same three-client mock, and send_report was called three times. Then I ran it on staging against the real subscriber list and confirmed 270 reports.

But I didn't want this to happen again, so I also restructured the code to make this mistake harder to make:

def build_client_report(client):
    ..
    # (complex logic, now in its own function)
    ..
    return report


for client in subscribed_clients:
    report = build_client_report(client)
    send_report(client, report)

Pulled the 100 lines into a function The loop is now three lines long. When the loop is short, it's obvious what is inside it and what isn't.

Removed j = None There's no reason for a variable to live outside the loop anymore. If someone moves the send call out by mistake, linters can catch it again.

Looped over clients directly for client in subscribed_clients reads better than for i in range(...) and removes the extra variable juggling.

Added a test that checks the count The test asserts that send_report is called once per subscribed client. If this ever regresses, CI fails before the client notices.

What I Learned About AI Coding Tools

I use AI tools every day. This isn't a story about avoiding them. It's about how to review them.

AI changes need real review, not a skim An AI tool can produce a diff that looks like harmless formatting and still changes behaviour. In Python, a whitespace change can be a logic change.

Look at diffs with whitespace on Many of us hide whitespace changes to make diffs easier to read. For Python, that hides exactly this kind of bug. Keep whitespace visible for AI-generated changes.

Ask the AI to keep changes small If you ask for one fix, don't accept a diff that also reformats 200 lines. Small diffs are easier to check.

Long loops are a risk A 100-line loop body is hard for people and AI tools to reason about. Short functions protect you from both.

Test counts, not just success "The job ran" isn't the same as "the job did its work." A test or alert that checks how many reports went out would have caught this in minutes.

Final Thoughts

The fix took one line. Finding it took careful work: compare the numbers, notice the pattern (only the last client), confirm with logs, check the history, prove it with a test.

The AI tool didn't break the code in a loud way. It broke it quietly, and quiet bugs are the expensive ones. For a startup with 270 clients waiting on reports, quiet bugs cost trust.

Use AI tools. Just remember they can move a line four spaces to the left, and only you will notice.