{"record":{"id":"5a7fa12c53e9903a","repo":"nautechsystems/nautilus_trader","slug":"registered-order-context-for-venue-order-id-cont","errorCode":null,"errorMessage":"registered order context for {venue_order_id} contradicts cached order {}","messagePattern":"registered order context for (.+?) contradicts cached order (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/polymarket/src/execution/reports.rs","lineNumber":176,"sourceCode":"                anyhow::ensure!(\n                    cached_order.instrument_id() == requested_instrument_id,\n                    \"cached order instrument {} does not match requested instrument {requested_instrument_id}\",\n                    cached_order.instrument_id(),\n                );\n            }\n        }\n\n        if let Some(context) = context {\n            if let Some(requested_instrument_id) = requested_instrument_id {\n                anyhow::ensure!(\n                    context.identity.instrument_id == requested_instrument_id,\n                    \"registered order instrument {} does not match requested instrument {requested_instrument_id}\",\n                    context.identity.instrument_id,\n                );\n            }\n\n            if let Some(cached_order) = cached_order.as_ref() {\n                anyhow::ensure!(\n                    context.identity.client_order_id == cached_order.client_order_id()\n                        && context.identity.instrument_id == cached_order.instrument_id()\n                        && context.identity.order_side == cached_order.order_side()\n                        && context.identity.order_type == cached_order.order_type()\n                        && context.time_in_force == cached_order.time_in_force(),\n                    \"registered order context for {venue_order_id} contradicts cached order {}\",\n                    cached_order.client_order_id(),\n                );\n            }\n        }\n\n        Ok(TargetOrderAuthority {\n            client_order_id,\n            instrument_id: context\n                .map(|value| value.identity.instrument_id)\n                .or_else(|| cached_order.as_ref().map(Order::instrument_id)),\n            order_side: context\n                .map(|value| value.identity.order_side)","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/polymarket/src/execution/reports.rs#L158-L194","documentation":"After individual checks pass, the adapter cross-validates the registered order context against the cached order: client order id, instrument id, side, order type, and time in force must all agree. This ensure! fires when the registered context contradicts the cached order, indicating the registry and cache are out of sync — a serious internal consistency problem.","triggerScenarios":"generate_order_status_report_impl or generate_fill_reports_impl where order_contexts.get(venue_order_id) returns a context whose identity fields (or time_in_force) differ from cache.order_owned(client_order_id) — e.g. after a replace where the context was updated but the cache wasn't, or vice versa.","commonSituations":"A modify/replace updated only one of registry/cache; concurrent updates raced; a bug in registration wrote wrong side/type/TIF; stale process state after reconnect.","solutions":["Re-register the order context from the cached order so both views agree","Investigate which update path (submit vs modify) skipped syncing cache or registry","Restart/reconnect the adapter to rebuild consistent state","File a bug if the inconsistency arises from normal submit/modify flow"],"exampleFix":"// before\n// context updated with new TIF, cache order still old TIF -> ensure! fires\n// after\nlet cached = core.cache().order_owned(&coid).context(\"order not cached\")?;\norder_contexts.register(void_id, OrderContext::from(&*cached)); // keep registry derived from cache","handlingStrategy":"validation","validationCode":"let cached = core.cache().order_owned(&ctx.identity.client_order_id).context(\"missing cached order\")?;\nanyhow::ensure!(\n    ctx.identity.instrument_id == cached.instrument_id()\n        && ctx.identity.order_side == cached.order_side()\n        && ctx.identity.order_type == cached.order_type()\n        && ctx.time_in_force == cached.time_in_force(),\n    \"context/cache diverged\"\n);","typeGuard":"fn consistent(ctx: &OrderContext, cached: &OrderAny) -> bool {\n    ctx.identity.client_order_id == *cached.client_order_id()\n        && ctx.identity.instrument_id == *cached.instrument_id()\n        && ctx.identity.order_side == cached.order_side()\n        && ctx.identity.order_type == cached.order_type()\n}","tryCatchPattern":"match client.generate_order_status_report_impl(...).await {\n    Err(e) if e.to_string().contains(\"contradicts cached order\") => {\n        error!(\"registry/cache divergence — rebuild state before trading\");\n    }\n    other => other?,\n}","preventionTips":["Update the registry and cache in the same code path (single submit/modify handler)","Write tests covering submit and modify flows asserting context/cache consistency","Treat this error as a stop-trading condition until state is reconciled"],"tags":["rust","state-inconsistency","cache","internal"],"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"}