xai-org/grok-build · error · io::Error

invalid segment id

Error message

invalid segment id

What it means

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'.

Source

Thrown at crates/codegen/xai-grok-workspace/src/file_system/index.rs:977

        let mut entries = Vec::with_capacity(n_entries);
        let mut path_to_idx =
            FxHashMap::with_capacity_and_hasher(n_entries, FxBuildHasher::default());

        for idx in 0..n_entries {
            let mut flags_buf = [0u8; 1];
            r.read_exact(&mut flags_buf)?;
            let flags = flags_buf[0];

            let mut depth_buf = [0u8; 1];
            r.read_exact(&mut depth_buf)?;
            let depth = depth_buf[0] as usize;

            let mut segments = smallvec::SmallVec::with_capacity(depth);
            for _ in 0..depth {
                r.read_exact(&mut buf4)?;
                let seg_idx = u32::from_le_bytes(buf4) as usize;
                if seg_idx >= seg_id_map.len() {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "invalid segment id",
                    ));
                }
                segments.push(seg_id_map[seg_idx]);
            }

            let key = PathKey::from(segments.as_slice());
            path_to_idx.insert(key, idx);

            entries.push(FileEntry { segments, flags });
        }

        Ok(Self {
            interner,
            entries,
            path_to_idx,
            removed_count: 0,

View on GitHub (pinned to bc7f02eddd)

Solutions

  1. Regenerate the index: delete the corrupt file and rebuild it from the workspace contents.
  2. Verify file integrity (the stream may be truncated — check reads against expected lengths) and restore from backup.
  3. Lock or serialize writes to the index so partial writes can't produce inconsistent segment maps.
  4. Confirm the reading library version matches the one that wrote the file.

Example fix

// before
let idx = Index::read_from(&mut file)?;
// after
match Index::read_from(&mut file) {
    Ok(i) => i,
    Err(e) if e.to_string() == "invalid segment id" => {
        // corrupt index — rebuild from source of truth
        Index::rebuild(&workspace_root)?
    }
    Err(e) => return Err(e.into()),
}
Defensive patterns

Strategy: try-catch

Try / catch

match Index::read_from(&mut file) {
    Ok(i) => i,
    Err(e) if e.kind() == std::io::ErrorKind::InvalidData && e.to_string() == "invalid segment id" => {
        eprintln!("corrupt index, rebuilding");
        Index::rebuild(&workspace_root)?
    }
    Err(e) => return Err(e.into()),
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of xai-org/grok-build@bc7f02eddd (2026-08-31). Data as JSON: /api/errors/4c104c7ac09cbd64. Report an issue: GitHub.