{"record":{"id":"cae951a99999b581","repo":"nautechsystems/nautilus_trader","slug":"provider-order-quantity-does-not-match-cached-o","errorCode":null,"errorMessage":"provider order quantity {} does not match cached order quantity {}","messagePattern":"provider order quantity (.+?) does not match cached order quantity (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/polymarket/src/execution/reconciliation.rs","lineNumber":472,"sourceCode":"            },\n        )\n    })?;\n\n    if let Some(requested_instrument_id) = requested_instrument_id {\n        anyhow::ensure!(\n            instrument.id() == requested_instrument_id,\n            \"{evidence} resolves to instrument {}, not requested instrument {requested_instrument_id}\",\n            instrument.id(),\n        );\n    }\n    Ok(instrument)\n}\n\npub(super) fn validate_client_bound_order_quantity(\n    provider_order: &PolymarketOpenOrder,\n    expected_quantity: Quantity,\n) -> anyhow::Result<()> {\n    anyhow::ensure!(\n        expected_quantity.as_decimal() == provider_order.original_size,\n        \"provider order quantity {} does not match cached order quantity {}\",\n        provider_order.original_size,\n        expected_quantity,\n    );\n    Ok(())\n}\n\nfn validate_client_bound_order_row(\n    provider_order: &PolymarketOpenOrder,\n    cached_order: &OrderAny,\n    expected_quantity: Quantity,\n    provider_expire_time: Option<UnixNanos>,\n) -> anyhow::Result<()> {\n    anyhow::ensure!(\n        cached_order.order_side() == provider_order.side.into(),\n        \"provider order side {} does not match cached order side {}\",\n        provider_order.side,","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/polymarket/src/execution/reconciliation.rs#L454-L490","documentation":"validate_client_bound_order_quantity cross-checks a provider PolymarketOpenOrder's original_size against the expected Quantity from the client's cached order. Any exact-decimal inequality raises this anyhow error, naming both values, because the reconciliation cannot safely trust a provider order whose size disagrees with the local cache.","triggerScenarios":"validate_client_bound_order_row during build_order_report_from_order, or handle_fok_rest_status when handling a FOK order resting status, pass an expected_quantity that differs from provider_order.original_size — e.g. partial fills recorded locally but not reflected in original_size, or size/quantity unit mismatch (shares vs contracts).","commonSituations":"Provider mutates original_size on amend/partial-fill while the local cache keeps the original; local cache updated with remaining size instead of original size; decimal scale differences (1.0 vs 1.00 are equal as Decimal, but 1 vs 1.000001 are not); restoring an old cache snapshot after a provider-side change.","solutions":["Re-fetch the current provider order and update the local cache's original_size, then retry reconciliation.","Ensure the cached order stores original (not remaining) size and that both use the same unit and Decimal scale.","If the mismatch is a legit amend, process the provider modify event first so the cache is in sync before validating.","Clear/rebuild the order cache if the snapshot is stale relative to provider state."],"exampleFix":"// before\nlet cached_qty = Quantity::from(remaining_size); // wrong: remaining, not original\nvalidate_client_bound_order_quantity(&provider_order, cached_qty)?;\n// after\nlet cached_qty = Quantity::new(provider_order.original_size, instrument.size_precision);\nvalidate_client_bound_order_quantity(&provider_order, cached_qty)?;","handlingStrategy":"validation","validationCode":"fn sizes_match(provider: &PolymarketOpenOrder, expected: Quantity) -> bool {\n    expected.as_decimal() == provider.original_size\n}\n// refresh cache before reconcile:\nlet provider_order = client.get_order(&venue_order_id).await?;","typeGuard":"fn is_consistent_with_cache(o: &PolymarketOpenOrder, cached: &OrderAny) -> bool {\n    cached.quantity().as_decimal() == o.original_size\n}","tryCatchPattern":"match validate_client_bound_order_quantity(&provider_order, expected_qty) {\n    Err(e) if e.to_string().contains(\"does not match cached order quantity\") => {\n        rebuild_cache_from_provider(&provider_order.id).await?; // resync then retry\n    }\n    other => other?,\n}","preventionTips":["Store original size (not remaining) in the cached order, matching provider semantics","Process provider modify/partial-fill events before reconciliation so the cache is current","Keep size units and Decimal precision consistent between cache and provider conversions"],"tags":["polymarket","reconciliation","order-state","quantity-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"}