Guide: Investigating and Fixing a Bug

Related issue: #47 (Stores within 1 Mile logic)

Context

Issue #47 reports that after running a simulation step, all households appear to have the same number of stores within 1 mile. This guide walks through how to investigate and fix a bug using a test-first approach.

Beyond this project: This guide walks you through forming a hypothesis, testing it, and understanding the root cause before fixing. This is the scientific method applied to engineering. You’ll use this approach every time a production bug gets escalated: evidence first, hypothesis second, fix third.

Before You Start

Steps

1. Understand before you fix

Read stores_with_1_miles():

def stores_with_1_miles(self) -> int:
    total = 0
    for store in self.model.stores_list:
        distance = self.distances_map[store.unique_id]
        if distance <= 1.0:
            total += 1
    self.rating_evaluation(total)
    return total

It iterates all stores, checks distances_map for each, counts those within 1.0 mile.

2. Form a hypothesis

When is distances_map populated? Look at calculate_distances() (line 246) and where it’s called in step() (line 263):

def step(self) -> None:
    if self.distances_map is None:
        self.calculate_distances()

distances_map is only calculated if it’s None. After step 0, it’s never None again, so distances are never recalculated even if stores change between steps.

Is this the bug? Or is there something else going on? Think about:

3. Write a test that reproduces the bug BEFORE fixing it

def test_stores_within_1_mile_returns_different_counts_for_different_households():
    """Two households at different locations should see different store counts."""
    # Set up two households with different distances_map values
    # Assert they return different counts from stores_with_1_miles()
    ...

This test should FAIL with the current code if the bug is real. If it passes, your hypothesis is wrong and you need to dig deeper.

4. Investigate further if needed

Ask yourself:

5. Write the fix

Once you understand the root cause, fix it. Your reproducing test should now pass.

6. Write additional tests

LLM Usage

Definition of Done

While You’re In There