# Catch the bugs your coding agent introduces

Scan generated code for broken behavior, then check the fix before committing.

Source: https://perchscan.com/examples/defect-scans/
Published: 2026-09-20

```python
def can_fulfil(items, stock):
    """True when every line in the cart is in stock."""
    for item in items:
        if stock.get(item.sku, 0) >= item.quantity:
            return True
    return False
```

An agent's rewrite can introduce an early return like this one. As soon as the first item is in stock, the function returns `True` without looking at the rest of the cart, including anything out of stock. This deliberately broken checkout comes from Perch's [example order service](https://github.com/lakeday-org/perch/blob/50860211516249122087f9da706b99452225c030/test/fixtures/order-service/checkout.py).

## Find it with a scan

```sh
perch scan --filter type=defect,security
```

These are the checkout findings from the September 20 demo scan:

```text
checkout.py
  ID        Line  Severity  Type    Confidence  Problem                     Method
  bdc67421    15  P1 (0.8)  defect         82%  wrong_order                 place_order
  ddc5c917    25  P1 (0.9)  defect         94%  does_not_do_what_it_claims  can_fulfil
  ddc5c917    25  P1 (0.9)  defect         92%  wrong_return_value          can_fulfil
  4f8bf5dc    31  P1 (0.7)  defect         70%  bad_state_change            refund
```

The [full scan output](/demos/scan-capture.json) lists all 19 findings. If you scan the example yourself, scores and labels can differ. Pass an issue ID from your own scan to `perch issues <id>` to inspect it.

## Reproduce the bug

Run this with Python 3 from the example project:

```console
$ python3 - <<'PYTHON'
from types import SimpleNamespace as Item
from checkout import can_fulfil
items = [Item(sku="available", quantity=1), Item(sku="missing", quantity=1)]
print(can_fulfil(items, {"available": 1}))
PYTHON
True
```

The cart has an item that is out of stock, and the function still accepts it.

## Fix the stock check

Require every item to be available:

```python
def can_fulfil(items, stock):
    """True when every line in the cart is in stock."""
    return all(stock.get(item.sku, 0) >= item.quantity for item in items)
```

The two-item cart now returns `False`. An empty cart still returns `True`; reject it separately if your checkout requires at least one item.

Check the edited function before you commit:

```sh
perch check checkout.py::can_fulfil --rules defect
```

Add a regression test for the cart with one missing item, then commit and scan again.

## Run the example

To try this yourself, follow the [quick start](https://docs.perchscan.com/install/), then copy the example into a new repository:

```sh
git clone https://github.com/lakeday-org/perch.git perch-source
git -C perch-source checkout 50860211516249122087f9da706b99452225c030
mkdir perch-demo
cp perch-source/test/fixtures/order-service/*.py perch-demo/
cd perch-demo
git init
git add '*.py'
git commit -m "Add example order service"
perch scan --filter type=defect,security
```

The example contains deliberate bugs for testing Perch. Scanning it does not require running the service or installing its Python dependencies.
