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 early return like this can slip into an agent’s rewrite. The first available item returns True, skipping every later item, including anything out of stock. This deliberately broken checkout comes from Perch’s example order service.
Find it with a scan
perch scan --filter type=defect,security
The September 20 demo returned these checkout findings among its results:
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 contains all 19 findings. Scores and labels can vary on a fresh run. Use the issue ID from your own scan with perch issues <id> to inspect it.
Reproduce the bug
Run this from the example project with Python 3:
$ 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 contains an unavailable item, but the function accepts it.
Fix the stock check
Require every item to be available:
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 example now returns False. An empty cart returns True; reject it separately if that is your checkout policy.
Check the edited function before committing:
perch check checkout.py::can_fulfil --rules defect
Add a regression test for the mixed-stock cart, then commit and rescan.
Run the example
Follow the quick start, then copy the example into a separate repository:
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. You can scan its source without running a service or installing Python dependencies.