Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(transcripts): exclude torn non-utf8 tails in layout scan and test lossy decode
Totopo2783a258b439a3
diff --git a/crates/host-core/src/transcripts.rs b/crates/host-core/src/transcripts.rsindex e28304c21..629dd3a6f 100644--- a/crates/host-core/src/transcripts.rs+++ b/crates/host-core/src/transcripts.rs@@ -239,6 +239,11 @@ pub fn read_tool_call(Ok(None)}+/// Read one line from the reader into `line_buf`, tolerating mid-append torn UTF-8 bytes.+/// If `raw_buf` contains valid UTF-8, it is preserved verbatim. If torn/corrupt+/// bytes are encountered, they are decoded lossily via `String::from_utf8_lossy`+/// so that the downstream JSON / sniffing parser can safely discard the corrupted record+/// rather than aborting whole transcript reads with `InvalidData`.pub(crate) fn read_line_tolerant(reader: &mut BufReader<File>,raw_buf: &mut Vec<u8>,@@ -749,7 +754,7 @@ fn append_line(path: &Path, header: Option<String>, line: String) -> Result<()>buf.push('\n');}} else if !ends_with_newline(path)? {- // A crash mid-append left a torn tail. `scan_layout` tolerates it on+ // A crash mid-append left a torn tail. `read_transcript_window` tolerates it on// read, but appending straight after it would fuse the torn bytes and// this record into one invalid line and lose both. Terminate the torn// line first so this record starts on its own line.@@ -1840,6 +1845,22 @@ mod tests {assert_eq!(loaded_final[2].id, "m3");assert!(transcript_contains_id(dir.path(), "s1", "m3").unwrap());++ // A line with mid-string invalid UTF-8 bytes is lossily decoded with U+FFFD+ // so corrupt records do not abort reading the surrounding intact history.+ raw = fs::read(&path).unwrap();+ raw.extend_from_slice(b"{\"type\":\"message\",\"id\":\"m4\",\"role\":\"user\",\"blocks\":[{\"type\":\"text\",\"text\":\"hello");+ raw.extend_from_slice(&[0xE6, 0x92]);+ raw.extend_from_slice(b"world\"}],\"createdAt\":\"2026-07-26T00:00:00Z\"}\n");+ fs::write(&path, &raw).unwrap();++ let loaded_lossy = read_transcript(dir.path(), "s1").unwrap();+ assert_eq!(loaded_lossy.len(), 4);+ assert_eq!(loaded_lossy[3].id, "m4");+ assert!(loaded_lossy[3].blocks[0]["text"]+ .as_str()+ .unwrap()+ .contains('\u{FFFD}'));}#[test]diff --git a/crates/host-core/src/transcripts/layout.rs b/crates/host-core/src/transcripts/layout.rsindex b58286920..2f05a4e5d 100644--- a/crates/host-core/src/transcripts/layout.rs+++ b/crates/host-core/src/transcripts/layout.rs@@ -292,25 +292,31 @@ fn scan_layout(path: &Path, mut layout: TranscriptLayout) -> Result<TranscriptLa.with_context(|| format!("seek {}", path.display()))?;}let mut reader = BufReader::new(file);- let mut line = String::new();+ let mut raw = Vec::new();loop {- line.clear();- let read = reader.read_line(&mut line)?;+ raw.clear();+ let read = reader.read_until(b'\n', &mut raw)?;if read == 0 {break;}// A torn append is excluded until its newline arrives.- if !line.ends_with('\n') {+ if !raw.ends_with(b"\n") {break;}+ let line = std::str::from_utf8(&raw).map_err(|_| {+ std::io::Error::new(+ std::io::ErrorKind::InvalidData,+ "stream did not contain valid UTF-8",+ )+ })?;match sniff_line_kind(line.trim_end()) {Some("message") => layout.message_offsets.push(offset),Some("compaction") => layout.compaction_offsets.push(offset),_ => {}}offset += read as u64;- if line.capacity() > 1024 * 1024 {- line = String::new();+ if raw.capacity() > 1024 * 1024 {+ raw = Vec::new();}}layout.file_len = offset;diff --git a/crates/host-core/src/transcripts_layout_tests.rs b/crates/host-core/src/transcripts_layout_tests.rsindex 173f8ff2f..2007841b8 100644--- a/crates/host-core/src/transcripts_layout_tests.rs+++ b/crates/host-core/src/transcripts_layout_tests.rs@@ -307,6 +307,28 @@ fn real_invalid_utf8_is_reported_without_changing_transcript_bytes() {assert_eq!(fs::read(path).unwrap(), bytes);}+#[test]+fn torn_invalid_utf8_tail_is_excluded_from_layout_without_error() {+ let dir = tempdir().unwrap();+ let path = transcript_path(dir.path(), "layout-regression").unwrap();+ fs::create_dir_all(path.parent().unwrap()).unwrap();+ let mut bytes =+ b"{\"type\":\"session\"}\n{\"type\":\"message\",\"id\":\"m1\",\"role\":\"user\",\"blocks\":[],\"createdAt\":\"2026-07-26T00:00:00Z\"}\n"+ .to_vec();+ // Torn incomplete UTF-8 bytes at EOF (without trailing newline)+ bytes.extend_from_slice(&[0xE6, 0x92]);+ fs::write(&path, &bytes).unwrap();++ let layout =+ refresh_layout(dir.path(), "layout-regression", TranscriptLayout::default()).unwrap();+ assert_eq!(layout.message_count(), 1);+ let read =+ read_transcript_window_with_layout(dir.path(), "layout-regression", &layout, 0, None)+ .unwrap();+ assert_eq!(read.messages.len(), 1);+ assert_eq!(read.messages[0].id, "m1");+}+#[test]fn a_reliable_identity_persists_a_layout() {let dir = tempdir().unwrap();