{"record":{"id":"50d6c50178de72bb","repo":"nautechsystems/nautilus_trader","slug":"provider-venue-leg-quantity-does-not-match-expe","errorCode":null,"errorMessage":"provider venue-leg quantity {} does not match expected quantity {leg_quantity}","messagePattern":"provider venue-leg quantity (.+?) does not match expected quantity (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/polymarket/src/execution/reports.rs","lineNumber":873,"sourceCode":"                .client_order_id\n                .and_then(|id| self.core.cache().order_owned(&id))\n                .or_else(|| {\n                    self.core\n                        .cache()\n                        .client_order_id(&report.venue_order_id)\n                        .and_then(|id| self.core.cache().order_owned(id))\n                });\n            let filled_before_leg = if let Some(filled_before) =\n                modify_fill_offsets.get(&report.venue_order_id).copied()\n            {\n                filled_before\n            } else if let Some(cached_order) = cached_order.as_ref() {\n                let (filled_before, leg_quantity) = venue_leg_filled_before_and_quantity(\n                    cached_order,\n                    report.venue_order_id,\n                    report.quantity.precision,\n                )?;\n                anyhow::ensure!(\n                    report.quantity == leg_quantity,\n                    \"provider venue-leg quantity {} does not match expected quantity {leg_quantity}\",\n                    report.quantity,\n                );\n                report.quantity = cached_order.quantity();\n                report.filled_qty = filled_before\n                    .checked_add(report.filled_qty)\n                    .context(\"logical filled quantity overflow\")?;\n                filled_before\n            } else {\n                Quantity::zero(report.quantity.precision)\n            };\n\n            let cached_filled = cached_order.as_ref().map_or_else(\n                || Quantity::zero(report.quantity.precision),\n                Order::filled_qty,\n            );\n            let tracked_leg_filled = self","sourceCodeStart":855,"sourceCodeEnd":891,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/polymarket/src/execution/reports.rs#L855-L891","documentation":"For provider-sourced status reports on multi-leg (neg-risk) orders, the adapter computes the expected per-venue-leg quantity from the cached order via venue_leg_filled_before_and_quantity and requires the venue-reported leg quantity to match. This ensure! fires when the venue leg quantity differs from the quantity derived from the cached order, indicating divergence between local order state and venue state.","triggerScenarios":"generate_order_status_reports_impl processes a report for a venue_order_id leg whose cached order yields leg_quantity != report.quantity — typically after partial fills or a replace changed the quantity but the cache leg bookkeeping is stale.","commonSituations":"Missed fill events so cached filled quantities drifted; replace changed size without cache update; precision mismatch in leg quantity calculation; venue-side manual modification.","solutions":["Reconcile the cache with a full order status refresh from the venue","Verify fills were applied to the cached order (check for dropped fill events)","Check precision passed to venue_leg_filled_before_and_quantity matches the venue","Restart the adapter to rebuild cache from venue state if divergence persists"],"exampleFix":"// before\nlet (filled, leg_qty) = venue_leg_filled_before_and_quantity(cached_order, void_id, wrong_precision)?;\n// after\nlet (filled, leg_qty) = venue_leg_filled_before_and_quantity(\n    cached_order, void_id, report.quantity.precision)?; // use venue's own precision","handlingStrategy":"validation","validationCode":"let (_, leg_qty) = venue_leg_filled_before_and_quantity(cached_order, void_id, report.quantity.precision)?;\nanyhow::ensure!(leg_qty == report.quantity, \"cached leg state diverged from venue\");","typeGuard":"fn leg_quantity_consistent(cached: &OrderAny, report: &OrderStatusReport) -> bool {\n    venue_leg_filled_before_and_quantity(cached, report.venue_order_id, report.quantity.precision)\n        .map(|(_, q)| q == report.quantity)\n        .unwrap_or(false)\n}","tryCatchPattern":"match client.generate_order_status_reports(cmd).await {\n    Err(e) if e.to_string().contains(\"does not match expected quantity\") => {\n        warn!(\"cache/venue quantity drift for {} — requesting reconciliation\", cmd.venue_order_id);\n    }\n    other => other?,\n}","preventionTips":["Ensure all fill events are consumed so cached filled quantities stay current","Pass the venue's precision into leg quantity calculations, not a hardcoded value","Run periodic order-status reconciliation to detect and repair cache drift early"],"tags":["rust","quantity","cache-drift","venue-mismatch"],"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-14T05:17:10.506Z"}