Skip to main content

codetether_agent/tui/chat/sync/
archive_reader.rs

1//! Archive file reader: batched reads from the chat events JSONL.
2
3use std::io::{Read, Seek, SeekFrom};
4use std::path::Path;
5
6use anyhow::Result;
7
8/// Read up to ~1 MB from the archive starting at `offset`.
9/// Returns `(payload, next_offset, record_count)`.
10pub fn read_chat_archive_batch(archive_path: &Path, offset: u64) -> Result<(Vec<u8>, u64, usize)> {
11    let mut file = std::fs::File::open(archive_path)?;
12    let file_len = file.metadata()?.len();
13    if offset >= file_len {
14        return Ok((Vec::new(), offset, 0));
15    }
16
17    const MAX_BATCH_BYTES: usize = 1024 * 1024;
18    let to_read = ((file_len - offset) as usize).min(MAX_BATCH_BYTES);
19    file.seek(SeekFrom::Start(offset))?;
20
21    let mut buf = vec![0u8; to_read];
22    let bytes_read = file.read(&mut buf)?;
23    buf.truncate(bytes_read);
24
25    let records = buf.windows(1).filter(|w| w[0] == b'\n').count();
26    Ok((buf, offset + bytes_read as u64, records))
27}