{"record":{"id":"4c104c7ac09cbd64","repo":"xai-org/grok-build","slug":"invalid-segment-id","errorCode":null,"errorMessage":"invalid segment id","messagePattern":"invalid segment id","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-workspace/src/file_system/index.rs","lineNumber":977,"sourceCode":"        let mut entries = Vec::with_capacity(n_entries);\n        let mut path_to_idx =\n            FxHashMap::with_capacity_and_hasher(n_entries, FxBuildHasher::default());\n\n        for idx in 0..n_entries {\n            let mut flags_buf = [0u8; 1];\n            r.read_exact(&mut flags_buf)?;\n            let flags = flags_buf[0];\n\n            let mut depth_buf = [0u8; 1];\n            r.read_exact(&mut depth_buf)?;\n            let depth = depth_buf[0] as usize;\n\n            let mut segments = smallvec::SmallVec::with_capacity(depth);\n            for _ in 0..depth {\n                r.read_exact(&mut buf4)?;\n                let seg_idx = u32::from_le_bytes(buf4) as usize;\n                if seg_idx >= seg_id_map.len() {\n                    return Err(io::Error::new(\n                        io::ErrorKind::InvalidData,\n                        \"invalid segment id\",\n                    ));\n                }\n                segments.push(seg_id_map[seg_idx]);\n            }\n\n            let key = PathKey::from(segments.as_slice());\n            path_to_idx.insert(key, idx);\n\n            entries.push(FileEntry { segments, flags });\n        }\n\n        Ok(Self {\n            interner,\n            entries,\n            path_to_idx,\n            removed_count: 0,","sourceCodeStart":959,"sourceCodeEnd":995,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-workspace/src/file_system/index.rs#L959-L995","documentation":"The index stores segment ids as indices into a seg_id_map written earlier in the file. While decoding per-depth segment entries, read_from checks each u32 index against seg_id_map.len(); an index beyond the map means the record references a segment id that does not exist, so it fails with io::ErrorKind::InvalidData, 'invalid segment id'.","triggerScenarios":"Calling Index::read_from on a file whose entry records contain segment indices out of range — from a corrupted file, a partially written index, or data written by a mismatched format version where the seg_id_map was serialized differently.","commonSituations":"Disk corruption or truncation of the index file mid-write; concurrent writes without locking producing inconsistent seg_id_map vs entries; mixing files across incompatible library versions.","solutions":["Regenerate the index: delete the corrupt file and rebuild it from the workspace contents.","Verify file integrity (the stream may be truncated — check reads against expected lengths) and restore from backup.","Lock or serialize writes to the index so partial writes can't produce inconsistent segment maps.","Confirm the reading library version matches the one that wrote the file."],"exampleFix":"// before\nlet idx = Index::read_from(&mut file)?;\n// after\nmatch Index::read_from(&mut file) {\n    Ok(i) => i,\n    Err(e) if e.to_string() == \"invalid segment id\" => {\n        // corrupt index — rebuild from source of truth\n        Index::rebuild(&workspace_root)?\n    }\n    Err(e) => return Err(e.into()),\n}","handlingStrategy":"try-catch","validationCode":null,"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 segment id\" => {\n        eprintln!(\"corrupt index, rebuilding\");\n        Index::rebuild(&workspace_root)?\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Serialize index writes with a file lock to avoid partial/inconsistent files","Keep the reading and writing library versions in sync","Periodically verify index files and rebuild proactively; keep backups"],"tags":["serialization","corruption","binary-format","filesystem"],"backgroundTag":"corrupt-index-file","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}