{"record":{"id":"9d2a4194a313eaa3","repo":"nautechsystems/nautilus_trader","slug":"venue-leg-quantity-calculation-validated-cumulativ","errorCode":null,"errorMessage":"venue-leg quantity calculation validated cumulative fills","messagePattern":"venue-leg quantity calculation validated cumulative fills","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/polymarket/src/execution/lifecycle.rs","lineNumber":536,"sourceCode":"                    match venue_leg_filled_before_and_quantity(\n                        order,\n                        venue_order_id,\n                        order.quantity().precision,\n                    ) {\n                        Ok(quantities) => quantities,\n                        Err(e) => {\n                            log::error!(\n                                \"Cannot restore Polymarket order {} venue-leg quantities: {e}\",\n                                order.client_order_id()\n                            );\n                            continue;\n                        }\n                    };\n\n                let current_leg_filled = order\n                    .filled_qty()\n                    .checked_sub(prior_filled)\n                    .expect(\"venue-leg quantity calculation validated cumulative fills\");\n                self.fill_tracker.restore_order(\n                    venue_order_id,\n                    current_leg_quantity,\n                    current_leg_filled,\n                    order.order_side(),\n                );\n            }\n\n            for event in order.events() {\n                match event {\n                    OrderEventAny::Filled(fill) => {\n                        if let Some(key) = polymarket_trade_key(fill.info.as_ref()) {\n                            matched_fills.entry(key).or_default().push(fill.clone());\n                        }\n                    }\n                    OrderEventAny::FillVoided(voided) => {\n                        if let Some(key) = polymarket_trade_key(voided.info.as_ref()) {\n                            voided_trades.insert(key);","sourceCodeStart":518,"sourceCodeEnd":554,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/polymarket/src/execution/lifecycle.rs#L518-L554","documentation":"During cache-restore of Polymarket orders, the current venue-leg filled quantity is computed as order.filled_qty() - prior_filled using checked_sub. The expect asserts the cumulative filled quantity never decreased between recorded legs; if the cached fill history is inconsistent (cumulative went backwards), the panic fires.","triggerScenarios":"Calling load_orders_from_cache (at connect) for an order whose cached cumulative filled_qty is less than the prior leg's filled quantity — corrupted or partially-written cache state, or fill events recorded out of order.","commonSituations":"Recovering after a crash with an inconsistent cache; cache files edited or mixed between adapter versions; duplicate/out-of-order persisted fills.","solutions":["Inspect and repair/clear the cached order/fill records for the affected venue_order_id, then reconnect.","Verify the cache is not shared or downgraded between adapter versions with different fill bookkeeping.","If reproducible, capture the cache state and file a bug; it indicates a persistence-ordering defect in fill tracking."],"exampleFix":"// before\nlet current_leg_filled = order.filled_qty()\n    .checked_sub(prior_filled)\n    .expect(\"venue-leg quantity calculation validated cumulative fills\");\n\n// after (defensive)\nlet current_leg_filled = order.filled_qty()\n    .checked_sub(prior_filled)\n    .unwrap_or_else(|| {\n        log::error!(\"cumulative filled_qty regressed for {venue_order_id}; clamping to 0\");\n        Decimal::ZERO\n    });","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":"// wrap reconnect/cache-restore in a supervised task\nif let Err(e) = client.connect().await {\n    log::error!(\"cache restore failed: {e}; clearing suspect cache and retrying\");\n}","preventionTips":["Never share or hand-edit the cache directory between adapter versions.","Back up the cache before crash recovery and clear it if fills look inconsistent.","Verify persisted fill quantities are monotonically non-decreasing after crashes."],"tags":["rust","panic","polymarket","cache","fills","recovery"],"backgroundTag":"internal-invariant-violation","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}