Hmbown/CodeWhale · error · io::Error

session file missing parseable `metadata` block

Error message

session file missing parseable `metadata` block

What it means

The session lister reads only the head of each session file and extracts the top-level `metadata` object; if extraction fails it reads the whole file and retries. If the full buffer still yields no parseable metadata block, it throws this InvalidData io::Error. This means the file exists but its metadata cannot be located or parsed at all.

Solutions

  1. Inspect the session file: confirm it is valid JSON containing a top-level `metadata` object; repair or remove the broken file.
  2. Restore the file from backup, or delete it so the corrupted session stops failing the listing flow.
  3. If the file is from a different format version, load it with the matching app version or migrate it to the current format.

Example fix

// before
let meta = manager.read_session_metadata(path)?;
// after
let meta = manager.read_session_metadata(path).map_err(|e| {
    eprintln!("skipping unreadable session file {}: {e}", path.display());
    e
})?; // or filter such files out before listing
Defensive patterns

Strategy: validation

Validate before calling

fn file_has_metadata(path: &Path) -> bool {
    std::fs::read_to_string(path).map(|s| {
        serde_json::from_str::<serde_json::Value>(&s)
            .ok()
            .map(|v| v.get("metadata").is_some())
            .unwrap_or(false)
    }).unwrap_or(false)
}

Try / catch

match read_session_metadata(path) {
    Err(e) if e.kind() == std::io::ErrorKind::InvalidData => {
        log::warn!("skipping session file without parseable metadata: {}", path.display());
        continue; // in a listing loop
    }
    other => other,
}

Prevention

When it happens

Trigger: Listing session metadata for a file that is empty, truncated before the metadata object, not JSON, or whose top-level structure no longer contains a `metadata` key the extractor recognizes (e.g. written by a very old or very new format).

Common situations: Crash-corrupted session files, hand-edited files, foreign or placeholder files dropped into the checkpoints directory, or format drift across versions.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/d1b583c50617d8c8. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/session_manager.rs:2653

        let mut file = fs::File::open(path)?;
        let mut buf = Vec::with_capacity(PREFIX_BYTES);
        file.by_ref()
            .take(PREFIX_BYTES as u64)
            .read_to_end(&mut buf)?;

        if let Some(mut metadata) = extract_top_level_metadata(&buf) {
            apply_legacy_title_recovery(&mut metadata, &buf);
            return Ok(metadata);
        }

        // Metadata wasn't extractable from the prefix (truncated mid-block,
        // unusual key ordering, etc.). Read the rest and try again with the
        // full buffer before giving up.
        let mut rest = Vec::new();
        file.read_to_end(&mut rest)?;
        buf.extend_from_slice(&rest);
        let mut metadata = extract_top_level_metadata(&buf).ok_or_else(|| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "session file missing parseable `metadata` block",
            )
        })?;
        apply_legacy_title_recovery(&mut metadata, &buf);
        Ok(metadata)
    }

    /// Delete a session and its recovery checkpoints, retiring its origin.
    pub fn delete_session(&self, id: &str) -> std::io::Result<()> {
        self.remove_session(id, SessionRemoval::Explicit)
    }

    fn remove_session(&self, id: &str, removal: SessionRemoval) -> std::io::Result<()> {
        let path = self.validated_session_path(id)?;
        // Older ordinary snapshots may use a name reserved by the checkpoint
        // directory. Such a name must never address its shared legacy files.
        let checkpoint = self.validated_checkpoint_path(id).ok();

View on GitHub (pinned to 73e0f67d83)