{"record":{"id":"d4dc12fa83e1880b","repo":"nautechsystems/nautilus_trader","slug":"order-invariant-violated-no-events","errorCode":null,"errorMessage":"Order invariant violated: no events","messagePattern":"Order invariant violated: no events","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/orders/any.rs","lineNumber":113,"sourceCode":"                .map_err(|source| OrderReplayError::ApplyFailed { source })?;\n        }\n\n        Ok(order)\n    }\n\n    /// Returns a reference to the [`crate::events::OrderInitialized`] event.\n    ///\n    /// This is always the first event in the order's event list (invariant).\n    ///\n    /// # Panics\n    ///\n    /// Panics if the first event is not `OrderInitialized` (violates invariant).\n    #[must_use]\n    pub fn init_event(&self) -> &crate::events::OrderInitialized {\n        match self\n            .events()\n            .first()\n            .expect(\"Order invariant violated: no events\")\n        {\n            OrderEventAny::Initialized(init) => init,\n            _ => panic!(\"Order invariant violated: first event must be OrderInitialized\"),\n        }\n    }\n\n    /// Assigns the order to the list identified by `id`.\n    ///\n    /// # Panics\n    ///\n    /// Panics if the order has no events or its first event is not `OrderInitialized`.\n    pub fn set_order_list_id(&mut self, id: OrderListId) {\n        let order: &mut OrderCore = match self {\n            Self::Limit(order) => order,\n            Self::LimitIfTouched(order) => order,\n            Self::Market(order) => order,\n            Self::MarketIfTouched(order) => order,\n            Self::MarketToLimit(order) => order,","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/orders/any.rs#L95-L131","documentation":"OrderAny::init_event returns the first event, which by the order-model invariant is always OrderInitialized. It unwraps with expect(\"Order invariant violated: no events\") and panics when the order's event list is empty, and with the sibling message when the first event is a different type. Since from_events rejects empty input, an OrderAny with no events should never exist — hitting this means the object was constructed or mutated outside the documented paths.","triggerScenarios":"Calling init_event on an OrderAny that was built with zero events (e.g. via unsafe/Default-ish paths, manual struct construction, or code that cleared `events`), or on a partially deserialized order where the Initialized event was dropped.","commonSituations":"Custom deserialization that skips the init event; tests or tools constructing OrderAny fields directly; cache/restore code that applied events into a fresh order without seeding OrderInitialized; version-skewed persisted state.","solutions":["Always construct OrderAny via OrderAny::from_events with OrderInitialized as the first event — it returns a proper OrderReplayError::EmptyInput instead of an order that panics later.","Check the deserialization/restore path so the first persisted event (Initialized) is included and not filtered out.","Verify no code clears or truncates the `events` vec on a live order.","Where you cannot guarantee the invariant, call order.events().first() yourself and handle None before calling init_event.","Prefer OrderInitialized event data directly from your creation site if you already hold it, rather than re-reading from the order."],"exampleFix":"// before\nlet init = order.init_event(); // panics if events empty\n// after\nlet init = order.events().first().map(|e| match e {\n    OrderEventAny::Initialized(init) => init.clone(),\n    _ => panic!(\"first event must be Initialized\"),\n});\nmatch init {\n    Some(init) => { /* use init */ }\n    None => return Err(\"order has no events\".into()),\n}","handlingStrategy":"type-guard","validationCode":"if order.events().is_empty() {\n    return Err(\"order has no events; cannot read init_event\".into());\n}\nlet init = order.init_event();","typeGuard":"fn has_init_event(order: &OrderAny) -> bool {\n    matches!(order.events().first(), Some(OrderEventAny::Initialized(_)))\n}","tryCatchPattern":"// Prefer from_events, which returns Result instead of panicking:\nlet order = match OrderAny::from_events(events) {\n    Ok(o) => o,\n    Err(OrderReplayError::EmptyInput) => return Err(\"no order events\".into()),\n    Err(e) => return Err(e.into()),\n};\nlet init = order.init_event();","preventionTips":["Only build orders via OrderAny::from_events with OrderInitialized first.","Never truncate or clear the order's event list after creation.","Check deserialization paths so the Initialized event is never dropped.","Guard with events().first() when reconstructing orders from unknown sources."],"tags":["rust","orders","panic","internal-invariant-violation","events"],"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"}