{"record":{"id":"4d33acfcca4a5be1","repo":"nautechsystems/nautilus_trader","slug":"canonical-backtest-result-violates-the-version-1-e","errorCode":null,"errorMessage":"canonical backtest result violates the version 1 encoding rules","messagePattern":"canonical backtest result violates the version 1 encoding rules","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/backtest/src/result.rs","lineNumber":193,"sourceCode":"}\n\nimpl CanonicalBacktestResult {\n    /// Decodes canonical result bytes and verifies their envelope, normalization, and exact\n    /// encoding.\n    ///\n    /// Inner records retain their NautilusTrader model serialization and are compared as semantic\n    /// content. Producers must use the version 1 writer rather than construct records independently.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the bytes are not a canonical version 1 result.\n    pub fn from_slice(bytes: &[u8]) -> anyhow::Result<Self> {\n        let document: Value = serde_json::from_slice(bytes)\n            .map_err(|e| anyhow::anyhow!(\"invalid canonical backtest result JSON: {e}\"))?;\n        validate_document(&document)?;\n        let mut normalized = document.clone();\n        canonicalize_document(&mut normalized)?;\n        anyhow::ensure!(\n            normalized == document,\n            \"canonical backtest result violates the version 1 encoding rules\"\n        );\n        let canonical = serde_json::to_vec(&normalized)?;\n        anyhow::ensure!(\n            canonical == bytes,\n            \"canonical backtest result bytes do not use the canonical encoding\"\n        );\n        Ok(Self { document })\n    }\n\n    /// Returns the canonical compact UTF-8 JSON bytes without trailing data.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the in-memory document cannot be serialized.\n    pub fn to_bytes(&self) -> anyhow::Result<Vec<u8>> {\n        Ok(serde_json::to_vec(&self.document)?)","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/backtest/src/result.rs#L175-L211","documentation":"`from_slice` validates that the parsed document is already in canonical form: it clones, canonicalizes, and requires the canonicalized value to equal the original document. If re-canonicalization changes anything (key order is fine since JSON objects compare by content, but e.g. number formatting/duplicates/extra or missing normalized fields differ), this error is thrown. The input must follow the version-1 encoding rules exactly.","triggerScenarios":"Feeding a semantically valid but non-canonical result document: fields added/removed/reordered in a way canonicalization would alter, numbers serialized differently than the canonical encoder would (e.g. `1.0` vs `1`, exponent forms), or a hand-edited result file.","commonSituations":"Manually editing a saved backtest result; re-serializing a result with a different JSON library that formats numbers differently; results produced by an older or newer version of the canonicalizer.","solutions":["Regenerate the result bytes with the library's canonical serializer instead of hand-editing or re-serializing with another tool","Round-trip: build a `BacktestResult` from state (`from_state`) and re-emit it canonically","Compare your document against `canonicalize_document` output to find the diverging field and fix its encoding","Ensure the result was produced by the same library version that defines CANONICAL_SCHEMA v1 rules"],"exampleFix":"// before\nlet bytes = serde_json::to_vec(&value)?; // non-canonical number formatting\nBacktestResult::from_slice(&bytes)?;\n// after\nlet result = BacktestResult::from_state(state)?; // canonical by construction\nlet bytes = result.to_bytes()?; // then from_slice succeeds","handlingStrategy":"validation","validationCode":"// Verify document survives canonicalization unchanged\nlet mut normalized = document.clone();\ncanonicalize_document(&mut normalized)?;\nassert_eq!(normalized, document, \"document is not canonically encoded\");","typeGuard":null,"tryCatchPattern":"match BacktestResult::from_slice(&bytes) {\n    Err(e) if e.to_string().contains(\"violates the version 1 encoding rules\") => {\n        eprintln!(\"non-canonical content, regenerate via canonical writer\");\n    }\n    Err(e) => return Err(e),\n    Ok(r) => r,\n}","preventionTips":["Only load bytes produced by the library's canonical writer","Never hand-edit result JSON; edit inputs and re-run the backtest","Use from_state when you hold in-memory data instead of reconstructing JSON"],"tags":["json","canonicalization","backtest","validation"],"backgroundTag":"schema-validation-failed","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"}