M1.7: crash-recovery journal and restore dialog
Dirty buffers are written atomically to <data_dir>/recovery/<project-id>/ about 1 s after the last edit (at most 2 s while typing), from pump_vfs, never per keystroke. Entries are removed on save, close (also force), forced project close/open and recovery.discard. project.open emits recoveryAvailable; recovery.list/restore/discard; restored buffers are dirty and keep line ending and BOM. Corrupt entries are moved aside and reported as warning notifications. UI: restore dialog in ui/src/components/recovery (Restore all, Restore selected, Discard, time, changed-on-disk hint), catalog keys and runtime allow-list. Tests: crash/restore with CRLF + BOM, corrupt entries, removal paths, property test of random edits, headless control-channel test, Vitest for the dialog. Docs: architecture, parity, gaps, ROADMAP. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
6d976ed03e
commit
0b40eb90f8
27 changed files with 2222 additions and 27 deletions
143
crates/control/tests/recovery.rs
Normal file
143
crates/control/tests/recovery.rs
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
//! Crash recovery over the control channel, headless: type into a file,
|
||||
//! drop the backend without saving, start a new one on the same directories,
|
||||
//! get `recoveryAvailable` after `project.open`, restore and check the text.
|
||||
|
||||
#![allow(
|
||||
clippy::unwrap_used,
|
||||
clippy::expect_used,
|
||||
clippy::indexing_slicing,
|
||||
clippy::panic
|
||||
)]
|
||||
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::net::TcpStream;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use lutra_testkit::{EngineBackend, engine_config};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
/// A control client that keeps event lines apart from responses.
|
||||
struct Client {
|
||||
reader: BufReader<TcpStream>,
|
||||
writer: TcpStream,
|
||||
events: Vec<Value>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
fn connect(addr: std::net::SocketAddr) -> Self {
|
||||
let stream = TcpStream::connect(addr).unwrap();
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_secs(5)))
|
||||
.unwrap();
|
||||
Self {
|
||||
writer: stream.try_clone().unwrap(),
|
||||
reader: BufReader::new(stream),
|
||||
events: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn call(&mut self, request: Value) -> Value {
|
||||
self.writer
|
||||
.write_all(format!("{request}\n").as_bytes())
|
||||
.unwrap();
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
self.reader.read_line(&mut line).expect("line within 5 s");
|
||||
let line: Value = serde_json::from_str(&line).unwrap();
|
||||
if line.get("event").is_some() {
|
||||
self.events.push(line["event"].clone());
|
||||
} else {
|
||||
return line;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Waits until an event of `kind` has arrived and returns it.
|
||||
fn event(&mut self, kind: &str) -> Value {
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
loop {
|
||||
if let Some(at) = self.events.iter().position(|e| e["kind"] == kind) {
|
||||
return self.events.remove(at);
|
||||
}
|
||||
assert!(
|
||||
Instant::now() < deadline,
|
||||
"no {kind} event; got {:?}",
|
||||
self.events
|
||||
);
|
||||
let mut line = String::new();
|
||||
self.reader.read_line(&mut line).expect("line within 5 s");
|
||||
let line: Value = serde_json::from_str(&line).unwrap();
|
||||
if line.get("event").is_some() {
|
||||
self.events.push(line["event"].clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn execute(&mut self, command: &str, params: Value) -> Value {
|
||||
let r = self.call(json!({"id": command, "method": "engine.execute", "params": {"command": command, "params": params}}));
|
||||
assert!(r.get("error").is_none(), "{command}: {r}");
|
||||
r["result"].clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_text_survives_a_crash_and_is_restored() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let project = dir.path().join("project");
|
||||
std::fs::create_dir(&project).unwrap();
|
||||
std::fs::write(project.join("a.php"), b"\xEF\xBB\xBF<?php\r\n").unwrap();
|
||||
let config = engine_config(dir.path());
|
||||
|
||||
let backend = Arc::new(EngineBackend::with_config(config.clone()));
|
||||
let server = lutra_control::start(0, backend.clone()).unwrap();
|
||||
let mut client = Client::connect(server.local_addr());
|
||||
client.call(json!({"id": 0, "method": "events.subscribe"}));
|
||||
client.execute("project.open", json!({"path": project}));
|
||||
let id = client.execute("file.open", json!({"path": "a.php"}))["id"].clone();
|
||||
for (version, ch) in "echo 1;".chars().enumerate() {
|
||||
client.execute(
|
||||
"buffer.edit",
|
||||
json!({"id": id, "baseVersion": version + 1, "edits": [{"from": 7 + version, "to": 7 + version, "insert": ch.to_string()}]}),
|
||||
);
|
||||
}
|
||||
let typed = client.execute("buffer.get", json!({"id": id}))["text"].clone();
|
||||
assert_eq!(typed, "<?php\r\necho 1;");
|
||||
|
||||
let entries = config.data_dir.join("recovery");
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
while std::fs::read_dir(&entries).map_or(0, |d| d.flatten().count()) == 0 {
|
||||
assert!(Instant::now() < deadline, "journal not written");
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
backend.pump();
|
||||
}
|
||||
drop(client);
|
||||
drop(backend);
|
||||
|
||||
let backend = Arc::new(EngineBackend::with_config(config));
|
||||
let server = lutra_control::start(0, backend.clone()).unwrap();
|
||||
let mut client = Client::connect(server.local_addr());
|
||||
client.call(json!({"id": 0, "method": "events.subscribe"}));
|
||||
client.execute("project.open", json!({"path": project}));
|
||||
let offered = client.event("recoveryAvailable");
|
||||
assert_eq!(offered["entries"][0]["path"], "a.php");
|
||||
assert_eq!(offered["entries"][0]["diskChanged"], false);
|
||||
|
||||
let out = client.execute("recovery.restore", json!({}));
|
||||
let id = out["restored"][0]["id"].clone();
|
||||
let got = client.execute("buffer.get", json!({"id": id}));
|
||||
assert_eq!(got["text"], typed);
|
||||
assert_eq!(got["lineEnding"], "crlf");
|
||||
assert_eq!(got["dirty"], true);
|
||||
client.execute("file.save", json!({"id": id}));
|
||||
assert_eq!(
|
||||
std::fs::read(project.join("a.php")).unwrap(),
|
||||
b"\xEF\xBB\xBF<?php\r\necho 1;"
|
||||
);
|
||||
assert!(
|
||||
client.execute("recovery.list", json!({}))["entries"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue