Primary finding
`quote_sell` may produce negative reserves or NaN when shares > complete-set capacity
- backend/wonderwall/simulations/polymarket/amm.py:111-146
There’s no bound check that `x <= S` or that `usd_out >= 0`. For sells where shares S are very large relative to reserves, x may exceed S (since the quadratic root maximizes complete-set burn) — `usd_out` becomes negative, which gets written to portfolio.balance as `balance + usd_received` (decreasing the agent’s cash). Additionally, `new_reserve_b = k / new_reserve_a` can underflow but cannot go negative; still, an effective_price > 1 violates the AMM invariant comment ("each share can pay out at most $1"). The 2% cap exists only for buys, not sells.
Recommendation
Clamp x to [0, S]; if usd_out < 0 or > S, return an error from sell_shares ("trade size too large"). Also enforce a max-trade cap on sells, symmetric to buys.