Skip to content

Commit bd24f54

Browse files
committed
fix(state): treat invalid UTF-8 in the journal tail as a torn line
A crash can tear the last journal line inside a multi-byte character (a path with non-ASCII text). read_to_string then failed with InvalidData before per-line recovery ran, so ShellState::new failed and the workspace could not be reopened. The journal is now read as bytes and each line parsed with serde_json::from_slice, so an invalid tail ends replay like any other torn line. Codex review on #229. Regression test fails without the fix. cargo test 838 passed; stress suite 11 passed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J4QvDjNR1ZoYKEPxSKE2nq
1 parent 2273913 commit bd24f54

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

‎impl/rust-cli/src/state.rs‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -843,12 +843,16 @@ impl ShellState {
843843
}
844844

845845
if self.journal_file.exists() {
846-
let journal = crate::fs_pure::read_to_string(&self.journal_file)?;
846+
// Read bytes, not a string: a crash can tear the last line inside
847+
// a multi-byte character, and that must count as a torn line
848+
// rather than make the whole file unreadable.
849+
let journal = crate::fs_pure::read_to_end(&self.journal_file)?;
850+
let body = journal.strip_suffix(b"\n").unwrap_or(&journal);
847851
let mut replayed_all = true;
848-
for line in journal.lines() {
852+
for line in body.split(|&b| b == b'\n').filter(|_| !body.is_empty()) {
849853
// A torn line (crash mid-append) ends the replay, and so does
850854
// a sequence gap: replay keeps the longest consistent prefix.
851-
let Ok(JournalLine { seq, entry }) = serde_json::from_str::<JournalLine>(line)
855+
let Ok(JournalLine { seq, entry }) = serde_json::from_slice::<JournalLine>(line)
852856
else {
853857
replayed_all = false;
854858
break;
@@ -1804,6 +1808,30 @@ mod tests {
18041808
assert_eq!(summary(&reloaded), summary(&restarted));
18051809
}
18061810

1811+
#[test]
1812+
fn test_journal_torn_inside_multibyte_char_reopens() {
1813+
let root = persisted_root();
1814+
let mut state = reopen(&root);
1815+
record(&mut state, "a");
1816+
{
1817+
use std::io::Write;
1818+
let mut journal = fs::OpenOptions::new()
1819+
.append(true)
1820+
.open(&state.journal_file)
1821+
.unwrap();
1822+
// "é" is 0xC3 0xA9; stop after the first byte.
1823+
journal
1824+
.write_all(b"{\"seq\":2,\"entry\":{\"Record\":{\"path\":\"caf\xC3")
1825+
.unwrap();
1826+
}
1827+
1828+
let mut reopened = reopen(&root);
1829+
assert_eq!(summary(&reopened), summary(&state));
1830+
record(&mut reopened, "b");
1831+
let reloaded = reopen(&root);
1832+
assert_eq!(reloaded.history.len(), 2);
1833+
}
1834+
18071835
#[test]
18081836
fn test_previous_dir_survives_reload_without_snapshot() {
18091837
let root = persisted_root();

0 commit comments

Comments
 (0)