{"record":{"id":"de8a34565b39e8d2","repo":"nautechsystems/nautilus_trader","slug":"command-venue-order-id-command-venue-order-id-do","errorCode":null,"errorMessage":"command venue order ID {command_venue_order_id} does not match cached order {target_venue_order_id}","messagePattern":"command venue order ID (.+?) does not match cached order (.+?)","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/interactive_brokers/src/execution/core.rs","lineNumber":2123,"sourceCode":"    ) -> anyhow::Result<()> {\n        anyhow::ensure!(\n            cmd.client_order_id == target_order.client_order_id(),\n            \"command client order ID {} does not match cached order {}\",\n            cmd.client_order_id,\n            target_order.client_order_id()\n        );\n        anyhow::ensure!(\n            cmd.instrument_id == target_order.instrument_id(),\n            \"command instrument ID {} does not match cached order {}\",\n            cmd.instrument_id,\n            target_order.instrument_id()\n        );\n\n        // Command actor IDs identify the requester and are not ownership evidence\n        if let (Some(command_venue_order_id), Some(target_venue_order_id)) =\n            (cmd.venue_order_id.as_ref(), target_order.venue_order_id())\n        {\n            anyhow::ensure!(\n                command_venue_order_id == &target_venue_order_id,\n                \"command venue order ID {command_venue_order_id} does not match cached order {target_venue_order_id}\"\n            );\n        }\n\n        Ok(())\n    }\n\n    /// Handles cancel order asynchronously.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if broker order resolution or identity caching fails.\n    async fn handle_cancel_order_async(\n        cmd: &CancelOrder,\n        target_order: &OrderAny,\n        client: &Arc<Client>,\n        order_id_map: &Arc<Mutex<AHashMap<ClientOrderId, i32>>>,","sourceCodeStart":2105,"sourceCodeEnd":2141,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/interactive_brokers/src/execution/core.rs#L2105-L2141","documentation":"Third check in `validate_cancel_order_target`: when BOTH the command and the cached order carry a venue order ID, they must match. Only command actor IDs are ignored (requester identity is not ownership evidence). A mismatch of venue order IDs indicates the command was resolved against the wrong venue order, so the cancel is refused to avoid cancelling the wrong IB order.","triggerScenarios":"`cmd.venue_order_id` is Some but differs from `target_order.venue_order_id()` — e.g. an order was replaced (modify producing a new venue ID) and a stale cancel referencing the old venue ID resolves to the new cached order.","commonSituations":"Cancel racing an order modify where IB assigned a new venue order ID; reconciliation updated the cached venue ID while an external component still held the old one; reconnection reassigning venue IDs.","solutions":["Refresh the venue order ID from reconciliation/latest execution reports before issuing the cancel.","Cancel by client order ID (omit venue_order_id) when the venue ID may be stale, letting the adapter resolve the current one.","After a modify/replace, wait for the OrderUpdated event confirming the new venue order ID before sending cancels."],"exampleFix":"// before\nlet cmd = CancelOrderBuilder::default()\n    .venue_order_id(old_venue_order_id)\n    ...\n// after (cancel by client order id, let adapter resolve venue id)\nlet cmd = CancelOrderBuilder::default()\n    .client_order_id(cmd_client_order_id)\n    ...","handlingStrategy":"validation","validationCode":"// Refresh venue order ID before issuing a cancel\nif let (Some(cmd_vo), Some(order_vo)) = (cmd.venue_order_id.as_ref(), order.venue_order_id()) {\n    anyhow::ensure!(cmd_vo == &order_vo, \"stale venue_order_id: cmd={} order={}\", cmd_vo, order_vo);\n}","typeGuard":"fn venue_order_id_current(cmd: &CancelOrder, order: &OrderAny) -> bool {\n    match (cmd.venue_order_id.as_ref(), order.venue_order_id()) {\n        (Some(a), Some(b)) => a == &b,\n        _ => true, // absent IDs are not compared by the adapter\n    }\n}","tryCatchPattern":"match cancel_order(&cmd).await {\n    Err(e) if e.to_string().contains(\"does not match cached order\") => {\n        // refresh from latest execution report, then retry once\n        refresh_venue_order_id(&cmd.client_order_id).await?;\n        cancel_order(&cmd).await?;\n    }\n    other => other?,\n}","preventionTips":["Wait for the OrderUpdated event after modify/replace before sending cancels with a venue order ID.","Prefer cancelling by client order ID when venue IDs may be stale.","Run reconciliation after every reconnect so cached venue IDs are current."],"tags":["rust","validation","order-management","venue-order-id"],"backgroundTag":"invalid-argument-value","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"}