Hmbown/CodeWhale · info
event recovery chunk length fits usize
Error message
event recovery chunk length fits usize
What it means
Panic from `usize::try_from(search_end.min(buffer_len))`. `search_end` is a u64 file length and `buffer_len` is the u64 form of an 8 KiB buffer, so the minimum is at most 8192 and always fits usize on any real target; the expect asserts that invariant.
Solutions
- No change needed for the current fixed 8 KiB chunking
- If chunk size ever derives from the file length, clamp to usize::MAX first
Defensive patterns
Strategy: validation
Validate before calling
let chunk_len_usize = usize::try_from(search_end.min(buffer_len)).expect("chunk fits usize"); Prevention
- Bound chunk size by the fixed buffer length, never by raw file length
- Re-check bounds if chunk derivation changes
When it happens
Trigger: Unreachable in practice: would require `min(search_end, buffer_len)` to exceed `usize::MAX`, impossible since buffer_len is 8192 and usize >= u32 on supported platforms.
Common situations: Only a change shrinking usize below u64 range for chunk sizes (no real target) or replacing the fixed chunk bound with file-length-derived values without a re-check.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- event recovery chunk length fits u64
- event recovery buffer fits u64
- event recovery newline index fits u64
- hardcoded project MCP state path is valid
- hardcoded project notes state path is valid
AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22).
Data as JSON: /api/errors/70945efbe4870ccf.
Report an issue: GitHub.
Appendix: source
Thrown at crates/tui/src/runtime_threads.rs:14023
.len();
if len == 0 {
return Ok(());
}
file.seek(SeekFrom::End(-1))?;
let mut last = [0_u8; 1];
file.read_exact(&mut last)?;
if last[0] == b'\n' {
return Ok(());
}
let mut search_end = len;
let mut truncate_at = 0_u64;
let mut buffer = [0_u8; 8 * 1024];
let buffer_len = u64::try_from(buffer.len()).expect("event recovery buffer fits u64");
while search_end > 0 {
let chunk_len = usize::try_from(search_end.min(buffer_len))
.expect("event recovery chunk length fits usize");
let chunk_len_u64 = u64::try_from(chunk_len).expect("event recovery chunk length fits u64");
let chunk_start = search_end - chunk_len_u64;
file.seek(SeekFrom::Start(chunk_start))?;
file.read_exact(&mut buffer[..chunk_len])?;
if let Some(index) = buffer[..chunk_len].iter().rposition(|byte| *byte == b'\n') {
truncate_at = chunk_start
+ u64::try_from(index).expect("event recovery newline index fits u64")
+ 1;
break;
}
search_end = chunk_start;
}
file.set_len(truncate_at)
.with_context(|| format!("Failed to truncate torn tail in {}", path.display()))?;
file.sync_all()
.with_context(|| format!("Failed to sync repaired {}", path.display()))?;
tracing::warn!(View on GitHub (pinned to 73e0f67d83)