This question evaluates a candidate's ability to debug, test, and refactor Python code with attention to state management, mutable default arguments, deterministic unit testing, API design, and type-safety.

You're given a small Python repo. After creating and activating a clean virtual environment, installing requirements, and running "pytest -q", two tests intermittently fail with state leakage. Consider this simplified core function:
STATE = {} def add_item(cart=None, item_id=None, qty=1, price=0.0, discounts=[]): if cart is None: cart = STATE if item_id in cart: cart[item_id]['qty'] += qty else: cart[item_id] = {'qty': qty, 'price': price, 'discounts': discounts} total = sum(v['qty']*v['price'] for v in cart.values()) total -= sum(d for v in cart.values() for d in v['discounts']) return total
Tasks: