{"record":{"id":"afcbd13cd1441ce4","repo":"nautechsystems/nautilus_trader","slug":"order-invariant-violated-first-event-must-be-orde","errorCode":null,"errorMessage":"Order invariant violated: first event must be OrderInitialized","messagePattern":"Order invariant violated: first event must be OrderInitialized","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/orders/any.rs","lineNumber":116,"sourceCode":"        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,\n            Self::StopLimit(order) => order,\n            Self::StopMarket(order) => order,\n            Self::TrailingStopLimit(order) => order,","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/orders/any.rs#L98-L134","documentation":"`OrderAny::init_event` retrieves the first event of the order and asserts it is an `OrderEventAny::Initialized`. A panic here means the internal order invariant — every order's event log starts with `OrderInitialized` — was violated: either the order was constructed without an init event or events were reordered/removed. This is a protected internal invariant, not a user-recoverable error.","triggerScenarios":"Calling `order.init_event()` on an `OrderAny` whose `events` list is non-empty but does not start with `Initialized` (e.g. manually pushed a Submitted/Filled event first, or deserialized a corrupted event log).","commonSituations":"Custom event-log reconstruction or replay code appending events out of order; test helpers building orders by pushing events manually; serde deserialization of truncated event histories.","solutions":["Ensure every OrderAny is created via its constructor (`OrderAny::new`/initialized from an OrderInitialized event) before any other events are pushed.","When rebuilding event logs, always insert/replay the OrderInitialized event first.","Check serialization/deserialization code so event order is preserved.","Use the `set_order_list_id`-style public mutators instead of mutating `events` directly."],"exampleFix":"// before\nlet mut order = OrderAny::test_default();\norder.events.clear();\nlet init = order.init_event(); // panics\n// after\nlet order = OrderAny::new(init_event, trader_id, strategy_id, account_id); // init first\nlet init = order.init_event();","handlingStrategy":"type-guard","validationCode":"fn has_valid_init_event(order: &OrderAny) -> bool {\n    matches!(order.events().first(), Some(OrderEventAny::Initialized(_)))\n}","typeGuard":"fn first_event_is_initialized(order: &OrderAny) -> Option<&OrderInitialized> {\n    match order.events().first() {\n        Some(OrderEventAny::Initialized(init)) => Some(init),\n        _ => None,\n    }\n}","tryCatchPattern":null,"preventionTips":["Construct orders only via their constructors so OrderInitialized is always the first event","Never push events into the events vec manually; use the event-application APIs","Preserve event order in serialization/replay pipelines"],"tags":["rust","invariant","order-events","panic"],"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"}