{"record":{"id":"e444dc83474bcc48","repo":"xai-org/grok-build","slug":"invalid-magic","errorCode":null,"errorMessage":"invalid magic","messagePattern":"invalid magic","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-workspace/src/file_system/index.rs","lineNumber":902,"sourceCode":"                w.write_all(&seg_id.as_u32().to_le_bytes())?;\n            }\n        }\n\n        Ok(())\n    }\n\n    /// Deserialize from binary format.\n    pub fn from_bytes(data: &[u8]) -> io::Result<Self> {\n        Self::read_from(&mut io::Cursor::new(data))\n    }\n\n    /// Read from a reader in binary format.\n    pub fn read_from<R: Read>(r: &mut R) -> io::Result<Self> {\n        // Header\n        let mut magic = [0u8; 4];\n        r.read_exact(&mut magic)?;\n        if &magic != MAGIC_INDEX {\n            return Err(io::Error::new(io::ErrorKind::InvalidData, \"invalid magic\"));\n        }\n\n        let mut buf2 = [0u8; 2];\n        let mut buf4 = [0u8; 4];\n\n        r.read_exact(&mut buf2)?;\n        let version = u16::from_le_bytes(buf2);\n        if version != VERSION {\n            return Err(io::Error::new(\n                io::ErrorKind::InvalidData,\n                format!(\"unsupported version: {}\", version),\n            ));\n        }\n\n        r.read_exact(&mut buf2)?;\n        let flags = u16::from_le_bytes(buf2);\n\n        // Handle compression","sourceCodeStart":884,"sourceCodeEnd":920,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-workspace/src/file_system/index.rs#L884-L920","documentation":"read_from deserializes a binary-persisted workspace file-system index. The first 4 bytes of the stream must equal MAGIC_INDEX; if they don't, the data is not a valid index file and read_from fails with io::ErrorKind::InvalidData, 'invalid magic'.","triggerScenarios":"Calling Index::read_from on a file/stream whose first 4 bytes are not the index magic: an empty file, a file written by a different tool, a corrupted/truncated header, or pointing read_from at a wrong (non-index) file.","commonSituations":"Opening a stale or corrupted workspace index file after a crash; passing the wrong file path to a loader; reading a file that was replaced or zero-length.","solutions":["Verify you are opening the correct index file at the expected path.","Check the file is at least 4 bytes and inspect its header bytes against the expected magic before loading.","Regenerate the index (delete the corrupt file and let the workspace rebuild it) instead of loading a corrupt one.","Restore the file from a backup if it was corrupted on disk."],"exampleFix":"// before\nlet idx = Index::read_from(&mut file)?;\n// after\nlet mut magic = [0u8; 4];\nfile.read_exact(&mut magic)?;\nif &magic != MAGIC_INDEX {\n    // rebuild instead of failing\n    return Index::rebuild(&workspace_root);\n}\nlet idx = Index::read_from(&mut file)?;","handlingStrategy":"validation","validationCode":"fn looks_like_index(path: &std::path::Path) -> std::io::Result<bool> {\n    use std::io::Read;\n    let mut f = std::fs::File::open(path)?;\n    let mut magic = [0u8; 4];\n    match f.read_exact(&mut magic) {\n        Ok(()) => Ok(&magic == MAGIC_INDEX),\n        Err(_) => Ok(false), // too short\n    }\n}","typeGuard":null,"tryCatchPattern":"match Index::read_from(&mut file) {\n    Ok(i) => i,\n    Err(e) if e.kind() == std::io::ErrorKind::InvalidData && e.to_string() == \"invalid magic\" => {\n        Index::rebuild(&workspace_root)?\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Check file size and header before deserializing","Never point read_from at files written by other tools","Keep index files out of reach of manual edits; rebuild on doubt"],"tags":["serialization","binary-format","corruption","filesystem"],"backgroundTag":"invalid-magic","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}