Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(transcripts): tolerate terminated torn encoded rows in storage verification

koeria6de6df6f3935
diff --git a/crates/host-core/src/transcripts.rs b/crates/host-core/src/transcripts.rs
index bb28fdde0..46221a659 100644
--- a/crates/host-core/src/transcripts.rs
+++ b/crates/host-core/src/transcripts.rs
@@ -1859,6 +1859,39 @@ mod tests {
assert_eq!(sequential_ids, ["m1", "m2"]);
}
+ /// The write side must match the read side: a terminated torn encoded
+ /// line must not make `append_message` reject the whole file, or the
+ /// session survives reads but can never persist another message (#2298).
+ #[test]
+ fn append_message_succeeds_after_terminated_torn_encoded_prefix() {
+ let dir = tempdir().unwrap();
+ append_message(
+ dir.path(),
+ "s1",
+ "2026-07-26T00:00:00Z",
+ &record("m1", "one"),
+ )
+ .unwrap();
+ let path = dir.path().join("sessions").join("s1.jsonl");
+ let mut body = fs::read_to_string(&path).unwrap();
+ body.push_str(r#"{"type":"message","id":"img","binaryStorage":{"image":"#);
+ body.push('\n');
+ fs::write(&path, body).unwrap();
+
+ append_message(
+ dir.path(),
+ "s1",
+ "2026-07-26T00:00:00Z",
+ &record("m2", "two"),
+ )
+ .expect("a terminated torn encoded prefix must not block writes");
+
+ let layout = refresh_layout(dir.path(), "s1", TranscriptLayout::default()).unwrap();
+ let read = read_transcript_window_with_layout(dir.path(), "s1", &layout, 0, None).unwrap();
+ let ids: Vec<&str> = read.messages.iter().map(|m| m.id.as_str()).collect();
+ assert_eq!(ids, ["m1", "m2"], "the appended message must be readable");
+ }
+
/// Corruption that merely contains "EOF while parsing" as a *value* must
/// stay a hard error: a complete, newline-terminated message whose
/// binaryStorage version field is that string decodes as InvalidStorage on
diff --git a/crates/host-core/src/transcripts/binary_integrity.rs b/crates/host-core/src/transcripts/binary_integrity.rs
index a4cc93e6b..bd5c2fa41 100644
--- a/crates/host-core/src/transcripts/binary_integrity.rs
+++ b/crates/host-core/src/transcripts/binary_integrity.rs
@@ -127,7 +127,11 @@ pub(super) fn verify(path: &Path, reader: &mut BufReader<File>) -> Result<()> {
break;
}
if let Err(error) = binary_storage::validate_line(line.trim()) {
- if binary_storage::is_error(&error) {
+ // Same semantics as the read paths: a terminated torn prefix
+ // (`InvalidStorageEof`) is tolerated, so a crash mid-append never
+ // blocks every future write to the session. Complete encoded lines
+ // that are otherwise corrupt stay hard errors.
+ if binary_storage::is_error(&error) && !binary_storage::is_torn_eof(&error) {
return Err(error).with_context(|| format!("validate storage {}", path.display()));
}
}
diff --git a/crates/host-core/src/transcripts/binary_integrity_tests.rs b/crates/host-core/src/transcripts/binary_integrity_tests.rs
index 5aa05f85c..9e00b74c0 100644
--- a/crates/host-core/src/transcripts/binary_integrity_tests.rs
+++ b/crates/host-core/src/transcripts/binary_integrity_tests.rs
@@ -45,21 +45,45 @@ fn replace_message_line(path: &Path, fault: &str) {
}
#[test]
-fn malformed_and_structurally_invalid_encoded_rows_block_mutations() {
- for fault in ["malformed", "typed"] {
- for update in [false, true] {
- let dir = tempfile::tempdir().unwrap();
- let path = seed_transcript(dir.path());
- replace_message_line(&path, fault);
- let before = fs::read(&path).unwrap();
- let result = if update {
- update_message(dir.path(), "s", &image_record()).map(|_| ())
- } else {
- append_message(dir.path(), "s", "old", &image_record())
- };
- let error = result.expect_err(&format!("fault={fault}, update={update}"));
- assert!(binary_storage::is_error(&error), "{error:#}");
- assert_eq!(fs::read(&path).unwrap(), before);
+fn structurally_invalid_encoded_rows_block_mutations() {
+ // "typed": a complete, well-formed JSON line whose business payload fails
+ // validation. Real corruption; every mutation must keep rejecting it.
+ for update in [false, true] {
+ let dir = tempfile::tempdir().unwrap();
+ let path = seed_transcript(dir.path());
+ replace_message_line(&path, "typed");
+ let before = fs::read(&path).unwrap();
+ let result = if update {
+ update_message(dir.path(), "s", &image_record()).map(|_| ())
+ } else {
+ append_message(dir.path(), "s", "old", &image_record())
+ };
+ let error = result.expect_err(&format!("update={update}"));
+ assert!(binary_storage::is_error(&error), "{error:#}");
+ assert_eq!(fs::read(&path).unwrap(), before);
+ }
+}
+
+/// The "malformed" fixture (`{"type":"message","binaryStorage":{"version":1`
+/// cut off mid-envelope, newline-terminated) is exactly a terminated torn
+/// prefix: the write side now tolerates it like every read path does (#2298).
+#[test]
+fn terminated_torn_encoded_rows_no_longer_block_mutations() {
+ for update in [false, true] {
+ let dir = tempfile::tempdir().unwrap();
+ let path = seed_transcript(dir.path());
+ replace_message_line(&path, "malformed");
+ let result = if update {
+ update_message(dir.path(), "s", &image_record()).map(|_| ())
+ } else {
+ append_message(dir.path(), "s", "old", &image_record())
+ };
+ result.expect("a terminated torn encoded prefix must not block mutations");
+ // The append variant must leave the new record readable; the torn row
+ // itself stays unreadable, and update_message only replaces records it
+ // can still find.
+ if !update {
+ assert_eq!(read_transcript(dir.path(), "s").unwrap().len(), 1);
}
}
}