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:
danny 2026-10-16 15:54:52 +02:00
parent 6d976ed03e
commit 0b40eb90f8
27 changed files with 2222 additions and 27 deletions

View 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()
);
}

View file

@ -4,7 +4,7 @@ use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use lutra_text::{Buffer, LineEnding};
use serde::Serialize;
use serde::{Deserialize, Serialize};
/// Identifier of an open buffer, unique for the engine's lifetime.
pub type BufferId = u64;
@ -103,7 +103,7 @@ impl Buffers {
}
/// A line ending as sent to clients: `"lf"`, `"crlf"` or `"cr"`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LineEndingName {
/// `\n`.
@ -123,3 +123,13 @@ impl From<LineEnding> for LineEndingName {
}
}
}
impl From<LineEndingName> for LineEnding {
fn from(name: LineEndingName) -> Self {
match name {
LineEndingName::Lf => Self::Lf,
LineEndingName::Crlf => Self::CrLf,
LineEndingName::Cr => Self::Cr,
}
}
}

View file

@ -12,6 +12,7 @@ use serde_json::{Value, json};
use super::params::parse;
use crate::buffers::{BufferId, LineEndingName};
use crate::journal;
use crate::workspace::path_string;
use crate::{Command, CommandSpec, EngineError, Event, Outcome, State};
@ -163,6 +164,7 @@ impl Command for Edit {
.map_err(|e| EngineError::invalid_params(command, e.to_string()))?;
open.version = open.version.saturating_add(1);
let (version, dirty) = (open.version, open.dirty());
journal::mark_edited(state, p.id, std::time::Instant::now());
state.emit(Event::BufferChanged { id: p.id, version });
if dirty != was_dirty {
state.emit(Event::DirtyChanged { id: p.id, dirty });
@ -212,6 +214,7 @@ impl Command for Close {
if open.dirty() && !p.force {
return Err(EngineError::UnsavedChanges(path_string(&open.path)));
}
journal::forget(state, p.id);
state.buffers.remove(p.id);
state.emit(Event::BufferClosed { id: p.id });
Ok(Outcome::value(Value::Null))

View file

@ -6,6 +6,7 @@ use serde_json::{Value, json};
use super::buffer::describe;
use super::params::{absolute, parse, project_path};
use crate::buffers::{BufferId, OpenBuffer};
use crate::journal;
use crate::workspace::{path_string, vfs_error};
use crate::{Command, CommandSpec, EngineError, Event, Outcome, State};
@ -80,6 +81,7 @@ fn save(state: &mut State, command: &str, id: BufferId) -> Result<u64, EngineErr
let was_dirty = open.dirty();
open.saved_version = open.version;
let version = open.version;
journal::forget(state, id);
if was_dirty {
state.emit(Event::DirtyChanged { id, dirty: false });
}

View file

@ -5,6 +5,7 @@ mod buffer;
mod file;
mod params;
mod project;
mod recovery;
mod settings;
pub mod theme;
@ -27,6 +28,9 @@ pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> {
registry.register(Box::new(buffer::Get))?;
registry.register(Box::new(buffer::Edit))?;
registry.register(Box::new(buffer::Close))?;
registry.register(Box::new(recovery::List))?;
registry.register(Box::new(recovery::Restore))?;
registry.register(Box::new(recovery::Discard))?;
registry.register(Box::new(settings::List))?;
registry.register(Box::new(settings::Get))?;
registry.register(Box::new(settings::Set))?;

View file

@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use super::params::{parse, project_path};
use crate::journal::{self, Journal};
use crate::workspace::{Workspace, path_string, vfs_error};
use crate::{Command, CommandSpec, Effect, EngineError, Event, Outcome, State};
@ -51,9 +52,11 @@ pub fn close_project(state: &mut State, force: bool) -> Result<(), EngineError>
return Err(EngineError::UnsavedChanges(what));
}
for id in state.buffers.ids() {
journal::forget(state, id);
state.buffers.remove(id);
state.emit(Event::BufferClosed { id });
}
state.journal = None;
state.project = None;
state.emit(Event::ProjectClosed);
Ok(())
@ -70,7 +73,9 @@ struct OpenParams {
force: bool,
}
/// `project.open`: opens a folder as the project, closing the current one.
/// `project.open`: opens a folder as the project, closing the current one,
/// and offers the unsaved texts its recovery journal holds with
/// [`Event::RecoveryAvailable`] (only when there are any).
pub struct Open;
impl Command for Open {
@ -80,7 +85,7 @@ impl Command for Open {
label: "Open Folder",
menu_path: None,
default_shortcut: None,
params: "{ path: string (absolute folder), force?: bool (discard unsaved changes of the open project) }; returns { root, name }",
params: "{ path: string (absolute folder), force?: bool (discard unsaved changes of the open project) }; returns { root, name }; emits recoveryAvailable when the recovery journal holds unsaved texts",
}
}
@ -89,6 +94,10 @@ impl Command for Open {
let (workspace, events) = Workspace::open(std::path::Path::new(&p.path))?;
close_project(state, p.force)?;
let root = workspace.project.root().to_path_buf();
state.journal = state
.config
.as_ref()
.map(|c| Journal::new(&c.data_dir, &root));
state.project = Some(workspace);
let mut name = String::new();
for event in events {
@ -97,6 +106,10 @@ impl Command for Open {
}
state.emit(event);
}
let entries = super::recovery::infos(state);
if !entries.is_empty() {
state.emit(Event::RecoveryAvailable { entries });
}
Ok(Outcome::value(json!({
"root": root.to_string_lossy(),
"name": name,

View file

@ -0,0 +1,191 @@
//! Recovery commands: `recovery.list`, `recovery.restore`,
//! `recovery.discard`. See [`crate::journal`] for the journal itself.
use std::path::PathBuf;
use serde::Deserialize;
use serde_json::{Value, json};
use super::params::parse;
use crate::buffers::OpenBuffer;
use crate::journal::{self, Entry, EntryInfo};
use crate::{Command, CommandSpec, EngineError, Event, Outcome, State};
/// Params naming journal entries; all entries when `paths` is missing.
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct PathsParams {
/// Project-relative paths with `/`.
#[serde(default)]
paths: Option<Vec<String>>,
}
/// The journal entries `paths` names, or all of them, plus the requested
/// paths that have no entry.
fn select(state: &mut State, paths: Option<Vec<String>>) -> (Vec<Entry>, Vec<String>) {
let entries = journal::scan(state);
match paths {
None => (entries, Vec::new()),
Some(paths) => {
let missing = paths
.iter()
.filter(|p| !entries.iter().any(|e| &e.path == *p))
.cloned()
.collect();
let chosen = entries
.into_iter()
.filter(|e| paths.contains(&e.path))
.collect();
(chosen, missing)
}
}
}
/// The entries of the open project's journal as clients see them.
pub fn infos(state: &mut State) -> Vec<EntryInfo> {
let entries = journal::scan(state);
entries.iter().map(|e| journal::info(state, e)).collect()
}
/// `recovery.list`: the unsaved texts the journal holds.
pub struct List;
impl Command for List {
fn spec(&self) -> &CommandSpec {
&CommandSpec {
id: "recovery.list",
label: "List Recovered Files",
menu_path: None,
default_shortcut: None,
params: "none; returns { entries: [{ path, savedAt (ms since the Unix epoch), diskChanged }] }",
}
}
fn enabled(&self, state: &State) -> bool {
state.has_project()
}
fn run(&self, state: &mut State, _params: Value) -> Result<Outcome, EngineError> {
Ok(Outcome::value(json!({ "entries": infos(state) })))
}
}
/// Opens `entry` as a dirty buffer, or puts its text into the clean buffer
/// that already holds the file. Returns the buffer id.
///
/// # Errors
/// [`EngineError::UnsavedChanges`] if the file is open with unsaved changes,
/// which are newer than the journal.
fn restore(state: &mut State, entry: Entry) -> Result<u64, EngineError> {
let rel: PathBuf = PathBuf::from(&entry.path).components().collect();
let mut buffer = lutra_text::Buffer::new(&entry.text);
buffer.set_line_ending(entry.line_ending.into());
if let Some(id) = state.buffers.find(&rel) {
let open = state
.buffers
.get_mut(id)
.ok_or_else(|| EngineError::Failed(format!("no buffer {id}")))?;
if open.dirty() {
return Err(EngineError::UnsavedChanges(entry.path));
}
open.buffer = buffer;
open.bom = entry.bom;
open.version = open.version.saturating_add(1);
let version = open.version;
state.emit(Event::BufferChanged { id, version });
state.emit(Event::DirtyChanged { id, dirty: true });
journal::adopt(state, &entry.path);
return Ok(id);
}
let id = state.buffers.insert(OpenBuffer {
buffer,
path: rel,
version: 1,
saved_version: 0,
bom: entry.bom,
});
state.emit(Event::BufferOpened {
id,
path: entry.path.clone(),
});
state.emit(Event::DirtyChanged { id, dirty: true });
journal::adopt(state, &entry.path);
Ok(id)
}
/// `recovery.restore`: opens journal entries as dirty buffers.
pub struct Restore;
impl Command for Restore {
fn spec(&self) -> &CommandSpec {
&CommandSpec {
id: "recovery.restore",
label: "Restore Unsaved Changes",
menu_path: None,
default_shortcut: None,
params: "{ paths?: [string] (all entries if missing) }; opens each file as a dirty buffer with the journal text, line ending and BOM; returns { restored: [{ path, id }], failed: [{ path, message }] }",
}
}
fn enabled(&self, state: &State) -> bool {
state.has_project()
}
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
let p: PathsParams = parse(self.spec().id, params)?;
let (entries, missing) = select(state, p.paths);
let mut restored = Vec::new();
let mut failed: Vec<Value> = missing
.into_iter()
.map(|path| json!({ "path": path, "message": "no recovery entry" }))
.collect();
for entry in entries {
let path = entry.path.clone();
match restore(state, entry) {
Ok(id) => restored.push(json!({ "path": path, "id": id })),
Err(e) => failed.push(json!({ "path": path, "message": e.to_string() })),
}
}
Ok(Outcome::value(
json!({ "restored": restored, "failed": failed }),
))
}
}
/// `recovery.discard`: deletes journal entries.
pub struct Discard;
impl Command for Discard {
fn spec(&self) -> &CommandSpec {
&CommandSpec {
id: "recovery.discard",
label: "Discard Unsaved Changes",
menu_path: None,
default_shortcut: None,
params: "{ paths?: [string] (all entries if missing) }; deletes the journal entries; an open buffer with unsaved changes is journaled again; returns { discarded: [path] }",
}
}
fn enabled(&self, state: &State) -> bool {
state.has_project()
}
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
let p: PathsParams = parse(self.spec().id, params)?;
let (entries, _) = select(state, p.paths);
let mut discarded = Vec::new();
let now = std::time::Instant::now();
for entry in entries {
if journal::remove(state, &entry.path) {
let rel: PathBuf = PathBuf::from(&entry.path).components().collect();
if let Some(id) = state.buffers.find(&rel)
&& state.buffers.get(id).is_some_and(OpenBuffer::dirty)
{
journal::mark_edited(state, id, now);
}
discarded.push(entry.path);
}
}
Ok(Outcome::value(json!({ "discarded": discarded })))
}
}

View file

@ -114,7 +114,8 @@ impl Engine {
}
/// Applies pending changes on disk (reloads clean buffers, reports
/// changed directories and conflicts) and returns every queued event.
/// changed directories and conflicts), writes due recovery-journal
/// entries (see [`crate::journal`]) and returns every queued event.
/// Hosts call it regularly, e.g. every 100 ms from a background thread.
///
/// # Errors
@ -124,6 +125,23 @@ impl Engine {
let state = &mut self.state;
guard::run(|| {
crate::workspace::pump(state);
crate::journal::tick(state, std::time::Instant::now());
Ok(())
})?;
Ok(state.take_events())
}
/// Writes every unsaved edit to the recovery journal now instead of
/// after the debounce, and returns every queued event. For tests and
/// for hosts that are about to stop.
///
/// # Errors
/// [`EngineError::Internal`] if writing panicked; the queued events stay
/// queued.
pub fn flush_journal(&mut self) -> Result<Vec<crate::Event>, EngineError> {
let state = &mut self.state;
guard::run(|| {
crate::journal::flush(state);
Ok(())
})?;
Ok(state.take_events())
@ -283,6 +301,9 @@ mod tests {
"project.close",
"project.open",
"project.openDialog",
"recovery.discard",
"recovery.list",
"recovery.restore",
"settings.get",
"settings.list",
"settings.reset",

View file

@ -15,6 +15,8 @@ use std::collections::BTreeMap;
use serde::Serialize;
use serde_json::{Map, Value};
use crate::journal::EntryInfo;
/// A state change.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(
@ -96,6 +98,13 @@ pub enum Event {
/// Every token as `--lt-<name>` → CSS value.
tokens: BTreeMap<String, String>,
},
/// The recovery journal of the project just opened holds unsaved texts
/// from an earlier session; offer `recovery.restore` /
/// `recovery.discard`.
RecoveryAvailable {
/// The entries, sorted by path.
entries: Vec<EntryInfo>,
},
/// A message for the user, shown as a toast.
Notification {
/// Unique id of this notification, for dismissing it.

View file

@ -0,0 +1,460 @@
//! Crash-recovery journal: unsaved buffers written to disk so a crash never
//! loses the user's text.
//!
//! # Layout
//! `<data_dir>/recovery/<project-id>/<entry-id>.json`, where the project id
//! is the FNV-1a 64-bit hash of the canonical project root and the entry id
//! the same hash of the project-relative path, both as 16 hex digits. FNV is
//! used instead of `std`'s hasher because its output must stay the same
//! across Rust versions. One file per dirty buffer holds an [`Entry`]:
//! `{path, text, lineEnding, bom, savedAt}`; `text` is the buffer's text as
//! held in memory, `savedAt` milliseconds since the Unix epoch.
//!
//! # When entries are written and removed
//! - `buffer.edit` only records the time ([`mark_edited`]); nothing is
//! written per keystroke. [`tick`], called from every
//! [`crate::Engine::pump_vfs`], writes a buffer once it has been idle for
//! [`IDLE`], or [`MAX_WAIT`] after its first unwritten edit during
//! continuous typing. With the desktop's 100 ms pump an entry is on disk at
//! most about 1.1 s after the last edit.
//! - Writes go through [`lutra_vfs::write_atomic`], so an entry is either the
//! old or the new version, never half written.
//! - Saving, closing (also with `force`), `project.close`/`project.open`
//! with `force` and `recovery.discard` remove entries ([`forget`]). Only
//! entries this session wrote or restored are removed by save and close;
//! entries left by a crashed session stay until they are restored or
//! discarded. Editing such a file without restoring it overwrites its
//! entry with the new text.
//!
//! # Untrusted input
//! Entries are read with a size cap ([`MAX_ENTRY_BYTES`]), at most
//! [`MAX_ENTRIES`] per project. An entry that cannot be read or parsed, or
//! whose path escapes the project, is renamed to `<entry>.bad`, skipped and
//! reported as a warning notification; it never stops the project from
//! opening.
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::buffers::{BufferId, LineEndingName};
use crate::workspace::path_string;
use crate::{NotificationLevel, State};
/// A buffer is written after this long without an edit.
pub const IDLE: Duration = Duration::from_millis(1000);
/// A buffer edited continuously is written at least this often.
pub const MAX_WAIT: Duration = Duration::from_millis(2000);
/// Largest entry file read; larger ones count as corrupt.
pub const MAX_ENTRY_BYTES: u64 = 512 * 1024 * 1024;
/// Most entry files read per project.
pub const MAX_ENTRIES: usize = 1000;
/// Extension of entry files.
const ENTRY_EXT: &str = "json";
/// Suffix appended to an entry that could not be read.
const BAD_SUFFIX: &str = ".bad";
/// Catalog key of the warning for an unreadable entry.
pub const KEY_CORRUPT: &str = "notification.recovery.corrupt";
/// Catalog key of the warning for a failed journal write.
pub const KEY_WRITE_FAILED: &str = "notification.recovery.writeFailed";
/// Catalog key of the warning for an unreadable journal directory.
pub const KEY_UNAVAILABLE: &str = "notification.recovery.unavailable";
/// One journal file: the unsaved text of one buffer.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Entry {
/// Project-relative path with `/` separators.
pub path: String,
/// The buffer's text as held in memory.
pub text: String,
/// The line ending the buffer saves with.
pub line_ending: LineEndingName,
/// The file has a UTF-8 byte order mark.
pub bom: bool,
/// When the entry was written, in milliseconds since the Unix epoch.
pub saved_at: u64,
}
/// What `recoveryAvailable` and `recovery.list` report about an entry.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EntryInfo {
/// Project-relative path.
pub path: String,
/// When the entry was written, in milliseconds since the Unix epoch.
pub saved_at: u64,
/// The file was modified after the entry was written, or is gone.
pub disk_changed: bool,
}
/// Edit times of a buffer whose latest text is not in the journal yet.
#[derive(Debug, Clone, Copy)]
struct Pending {
/// The first edit since the last write.
first: Instant,
/// The latest edit.
last: Instant,
}
impl Pending {
/// Whether the buffer is due for writing at `now`: idle for [`IDLE`] or
/// waiting for [`MAX_WAIT`].
fn due(&self, now: Instant) -> bool {
now.saturating_duration_since(self.last) >= IDLE
|| now.saturating_duration_since(self.first) >= MAX_WAIT
}
}
/// The journal of the open project.
#[derive(Debug)]
pub struct Journal {
/// `<data_dir>/recovery/<project-id>`; created on the first write.
dir: PathBuf,
/// Buffers with edits not written yet.
pending: BTreeMap<BufferId, Pending>,
/// Paths whose entry this session wrote or restored.
owned: BTreeSet<String>,
/// A write failure was reported and no write has succeeded since.
write_failed: bool,
}
impl Journal {
/// The journal of the project at the canonical `root`, stored below
/// `data_dir`. Touches nothing on disk.
pub fn new(data_dir: &Path, root: &Path) -> Self {
Self {
dir: data_dir
.join("recovery")
.join(hex_hash(root.to_string_lossy().as_bytes())),
pending: BTreeMap::new(),
owned: BTreeSet::new(),
write_failed: false,
}
}
/// The directory holding the entries.
pub fn dir(&self) -> &Path {
&self.dir
}
/// The file of the entry for `path`.
fn file(&self, path: &str) -> PathBuf {
self.dir
.join(format!("{}.{ENTRY_EXT}", hex_hash(path.as_bytes())))
}
}
/// FNV-1a 64-bit hash of `bytes` as 16 lowercase hex digits.
pub fn hex_hash(bytes: &[u8]) -> String {
let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
for byte in bytes {
hash ^= u64::from(*byte);
hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
}
format!("{hash:016x}")
}
/// Milliseconds since the Unix epoch, 0 for a clock before it.
fn millis(time: SystemTime) -> u64 {
time.duration_since(UNIX_EPOCH)
.map(|d| u64::try_from(d.as_millis()).unwrap_or(u64::MAX))
.unwrap_or(0)
}
/// Records an edit of buffer `id` at `now`; [`tick`] writes it later.
pub fn mark_edited(state: &mut State, id: BufferId, now: Instant) {
if let Some(journal) = state.journal.as_mut() {
journal
.pending
.entry(id)
.and_modify(|p| p.last = now)
.or_insert(Pending {
first: now,
last: now,
});
}
}
/// Writes every buffer that has been idle for [`IDLE`] or waiting for
/// [`MAX_WAIT`] at `now`.
pub fn tick(state: &mut State, now: Instant) {
let Some(journal) = state.journal.as_ref() else {
return;
};
let due: Vec<BufferId> = journal
.pending
.iter()
.filter(|(_, p)| p.due(now))
.map(|(id, _)| *id)
.collect();
for id in due {
write(state, id);
}
}
/// Writes every buffer with unwritten edits now.
pub fn flush(state: &mut State) {
let ids: Vec<BufferId> = state
.journal
.as_ref()
.map(|j| j.pending.keys().copied().collect())
.unwrap_or_default();
for id in ids {
write(state, id);
}
}
/// Writes the entry of buffer `id` if it is dirty. A failed write keeps the
/// buffer pending, so the next tick tries again, and is reported once until
/// a write succeeds.
fn write(state: &mut State, id: BufferId) {
let Some(open) = state.buffers.get(id) else {
if let Some(journal) = state.journal.as_mut() {
journal.pending.remove(&id);
}
return;
};
if !open.dirty() {
if let Some(journal) = state.journal.as_mut() {
journal.pending.remove(&id);
}
return;
}
let entry = Entry {
path: path_string(&open.path),
text: open.buffer.text(),
line_ending: LineEndingName::from(open.buffer.line_ending()),
bom: open.bom,
saved_at: millis(SystemTime::now()),
};
let Some(journal) = state.journal.as_mut() else {
return;
};
let file = journal.file(&entry.path);
let result = serde_json::to_vec(&entry)
.map_err(|e| e.to_string())
.and_then(|bytes| {
std::fs::create_dir_all(&journal.dir).map_err(|e| e.to_string())?;
lutra_vfs::write_atomic(&file, &bytes).map_err(|e| e.to_string())
});
match result {
Ok(()) => {
journal.pending.remove(&id);
journal.owned.insert(entry.path);
journal.write_failed = false;
}
Err(reason) => {
if journal.write_failed {
return;
}
journal.write_failed = true;
let mut params = Map::new();
params.insert("path".into(), Value::String(entry.path.clone()));
params.insert("reason".into(), Value::String(reason.clone()));
state.notify(
NotificationLevel::Warning,
Some(KEY_WRITE_FAILED),
Some(format!(
"Could not write the recovery copy of {}: {reason}",
entry.path
)),
Some(params),
);
}
}
}
/// Drops the journal state of buffer `id` and removes its entry if this
/// session owns it. Called when the buffer is saved or closed, before it is
/// removed. A file that cannot be removed is left behind: it is offered
/// again on the next open, which is safer than failing the save or close.
pub fn forget(state: &mut State, id: BufferId) {
let path = state.buffers.get(id).map(|b| path_string(&b.path));
let Some(journal) = state.journal.as_mut() else {
return;
};
journal.pending.remove(&id);
let Some(path) = path else {
return;
};
if journal.owned.remove(&path) {
let _ = std::fs::remove_file(journal.file(&path));
}
}
/// Reads every valid entry of the open project's journal, sorted by path.
/// Unreadable entries are renamed to `<entry>.bad` and reported as warning
/// notifications. A missing journal directory means no entries.
pub fn scan(state: &mut State) -> Vec<Entry> {
let Some(journal) = state.journal.as_ref() else {
return Vec::new();
};
let dir = journal.dir.clone();
let listing = match std::fs::read_dir(&dir) {
Ok(listing) => listing,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Vec::new(),
Err(e) => {
let mut params = Map::new();
params.insert("reason".into(), Value::String(e.to_string()));
state.notify(
NotificationLevel::Warning,
Some(KEY_UNAVAILABLE),
Some(format!("Could not read the recovery journal: {e}")),
Some(params),
);
return Vec::new();
}
};
let mut files: Vec<PathBuf> = listing
.filter_map(Result::ok)
.map(|e| e.path())
.filter(|p| p.extension().is_some_and(|ext| ext == ENTRY_EXT) && p.is_file())
.collect();
files.sort();
files.truncate(MAX_ENTRIES);
let mut entries = Vec::new();
for file in files {
match read_entry(state, &file) {
Ok(entry) => entries.push(entry),
Err(reason) => quarantine(state, &file, &reason),
}
}
entries.sort_by(|a, b| a.path.cmp(&b.path));
entries.dedup_by(|a, b| a.path == b.path);
entries
}
/// Reads and checks one entry file.
///
/// # Errors
/// A description of why the entry is unusable.
fn read_entry(state: &State, file: &Path) -> Result<Entry, String> {
let bytes = lutra_vfs::read_bytes(file, MAX_ENTRY_BYTES).map_err(|e| e.to_string())?;
let entry: Entry = serde_json::from_slice(&bytes).map_err(|e| e.to_string())?;
if entry.path.is_empty() {
return Err("empty path".into());
}
let journal = state.journal.as_ref().ok_or("no journal")?;
if journal.file(&entry.path).file_name() != file.file_name() {
return Err("the file name does not match the path".into());
}
let workspace = state.project.as_ref().ok_or("no project")?;
workspace
.project
.resolve(Path::new(&entry.path))
.map_err(|e| e.to_string())?;
Ok(entry)
}
/// Moves an unusable entry aside and reports it. The text inside may still
/// be worth something to the user, so it is renamed, not deleted.
fn quarantine(state: &mut State, file: &Path, reason: &str) {
let mut bad = file.as_os_str().to_os_string();
bad.push(BAD_SUFFIX);
let _ = std::fs::rename(file, PathBuf::from(&bad));
let name = file
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
let mut params = Map::new();
params.insert("file".into(), Value::String(name.clone()));
params.insert("reason".into(), Value::String(reason.to_string()));
state.notify(
NotificationLevel::Warning,
Some(KEY_CORRUPT),
Some(format!(
"Skipped an unreadable recovery entry {name}: {reason}"
)),
Some(params),
);
}
/// What clients see about `entry`: whether the file changed on disk after
/// the entry was written (or is gone) is judged by its modification time.
pub fn info(state: &State, entry: &Entry) -> EntryInfo {
let disk_changed = state
.project
.as_ref()
.and_then(|w| w.project.resolve(Path::new(&entry.path)).ok())
.and_then(|abs| std::fs::metadata(abs).ok())
.and_then(|meta| meta.modified().ok())
.is_none_or(|modified| millis(modified) > entry.saved_at);
EntryInfo {
path: entry.path.clone(),
saved_at: entry.saved_at,
disk_changed,
}
}
/// Marks `path` as written by this session, so save and close remove it.
pub fn adopt(state: &mut State, path: &str) {
if let Some(journal) = state.journal.as_mut() {
journal.owned.insert(path.to_string());
}
}
/// Removes the entry for `path`. Returns whether a file was removed.
pub fn remove(state: &mut State, path: &str) -> bool {
let Some(journal) = state.journal.as_mut() else {
return false;
};
journal.owned.remove(path);
std::fs::remove_file(journal.file(path)).is_ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hashes_are_stable() {
assert_eq!(hex_hash(b""), "cbf29ce484222325");
assert_eq!(hex_hash(b"a"), "af63dc4c8601ec8c");
assert_eq!(hex_hash(b"src/a.php").len(), 16);
}
#[test]
fn entries_serialise_in_camel_case() {
let entry = Entry {
path: "a.php".into(),
text: "x\r\n".into(),
line_ending: LineEndingName::Crlf,
bom: true,
saved_at: 5,
};
assert_eq!(
serde_json::to_value(&entry).unwrap(),
serde_json::json!({"path": "a.php", "text": "x\r\n", "lineEnding": "crlf", "bom": true, "savedAt": 5})
);
}
#[test]
fn due_after_idle_or_max_wait() {
let start = Instant::now();
let typing = Pending {
first: start,
last: start + Duration::from_millis(1500),
};
assert!(!typing.due(start + Duration::from_millis(1900)));
assert!(typing.due(start + MAX_WAIT));
let once = Pending {
first: start,
last: start,
};
assert!(!once.due(start + Duration::from_millis(999)));
assert!(once.due(start + IDLE));
assert!(!once.due(start));
}
}

View file

@ -12,6 +12,7 @@ mod engine;
mod error;
mod event;
pub mod guard;
pub mod journal;
mod registry;
pub mod settings;
mod state;

View file

@ -3,6 +3,7 @@
use serde_json::{Map, Value};
use crate::buffers::Buffers;
use crate::journal::Journal;
use crate::settings::Settings;
use crate::workspace::Workspace;
use crate::{EngineConfig, Event, NotificationLevel};
@ -24,6 +25,9 @@ pub struct State {
/// Where the engine stores its files; `None` for an engine without
/// persistence ([`crate::Engine::new`]).
pub config: Option<EngineConfig>,
/// The crash-recovery journal of the open project; `None` without a
/// project or without persistence.
pub journal: Option<Journal>,
/// Number of notifications emitted, for their ids.
notifications: u64,
/// Events queued for the host, oldest first.

View file

@ -0,0 +1,497 @@
//! Crash-recovery journal: unsaved text survives dropping the engine and is
//! restored exactly by a new engine on the same directories.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::indexing_slicing,
clippy::panic
)]
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use lutra_engine::{Engine, EngineConfig, Event, journal};
use proptest::prelude::*;
use serde_json::{Value, json};
use tempfile::TempDir;
/// A project folder and engine directories that outlive the engines.
struct Setup {
/// Holds everything.
_tmp: TempDir,
/// The project root.
project: PathBuf,
/// The engine directories.
config: EngineConfig,
}
impl Setup {
fn new(files: &[(&str, &[u8])]) -> Self {
let tmp = tempfile::tempdir().unwrap();
let project = tmp.path().join("project");
std::fs::create_dir(&project).unwrap();
for (name, bytes) in files {
let path = project.join(name);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, bytes).unwrap();
}
let config = EngineConfig {
config_dir: tmp.path().join("config"),
data_dir: tmp.path().join("data"),
};
Self {
_tmp: tmp,
project,
config,
}
}
/// A fresh engine with the project open, and the events of opening it.
fn engine(&self) -> (Engine, Vec<Event>) {
let mut engine = Engine::with_config(self.config.clone()).unwrap();
engine.take_events();
let out = engine
.execute("project.open", json!({"path": self.project}))
.unwrap();
(engine, out.events)
}
/// The journal directory of the project.
fn journal_dir(&self) -> PathBuf {
let root = std::fs::canonicalize(&self.project).unwrap();
let (engine, _) = self.engine();
let dir = engine.state().journal.as_ref().unwrap().dir().to_path_buf();
assert!(dir.starts_with(self.config.data_dir.join("recovery")));
assert!(!root.as_os_str().is_empty());
dir
}
/// The entry files in the journal directory.
fn entry_files(&self) -> Vec<PathBuf> {
let Ok(listing) = std::fs::read_dir(self.journal_dir()) else {
return Vec::new();
};
let mut files: Vec<PathBuf> = listing
.map(|e| e.unwrap().path())
.filter(|p| p.extension().is_some_and(|e| e == "json"))
.collect();
files.sort();
files
}
}
fn run(engine: &mut Engine, command: &str, params: Value) -> Value {
engine
.execute(command, params)
.unwrap_or_else(|e| panic!("{command}: {e}"))
.value
}
fn open(engine: &mut Engine, path: &str) -> u64 {
run(engine, "file.open", json!({"path": path}))["id"]
.as_u64()
.unwrap()
}
fn edit(engine: &mut Engine, id: u64, from: usize, to: usize, insert: &str) {
let version = run(engine, "buffer.get", json!({"id": id}))["version"].clone();
run(
engine,
"buffer.edit",
json!({"id": id, "baseVersion": version, "edits": [{"from": from, "to": to, "insert": insert}]}),
);
}
fn recovery_entries(events: &[Event]) -> Option<Vec<Value>> {
events.iter().find_map(|e| match e {
Event::RecoveryAvailable { entries } => {
Some(serde_json::to_value(entries).unwrap().as_array()?.clone())
}
_ => None,
})
}
#[test]
fn a_dropped_engine_leaves_text_that_a_new_engine_restores_exactly() {
let original = b"\xEF\xBB\xBF<?php\r\necho 1;\r\n";
let setup = Setup::new(&[("src/a.php", original)]);
let (mut engine, events) = setup.engine();
assert!(
recovery_entries(&events).is_none(),
"nothing to recover yet"
);
let id = open(&mut engine, "src/a.php");
edit(&mut engine, id, 7, 7, "$x = 'ü';\r\n");
let expected = run(&mut engine, "buffer.get", json!({"id": id}))["text"].clone();
assert_eq!(expected, "<?php\r\n$x = 'ü';\r\necho 1;\r\n");
engine.pump_vfs().unwrap();
assert!(setup.entry_files().is_empty(), "not written per keystroke");
let deadline = Instant::now() + Duration::from_secs(5);
while setup.entry_files().is_empty() {
assert!(Instant::now() < deadline, "journal not written within 5 s");
std::thread::sleep(Duration::from_millis(100));
engine.pump_vfs().unwrap();
}
drop(engine);
assert_eq!(
std::fs::read(setup.project.join("src/a.php")).unwrap(),
original,
"the file itself is untouched"
);
let (mut engine, events) = setup.engine();
let entries = recovery_entries(&events).expect("recoveryAvailable after project.open");
assert_eq!(entries.len(), 1);
assert_eq!(entries[0]["path"], "src/a.php");
assert_eq!(entries[0]["diskChanged"], false);
assert!(entries[0]["savedAt"].as_u64().unwrap() > 0);
assert_eq!(
run(&mut engine, "recovery.list", Value::Null)["entries"],
Value::Array(entries)
);
let out = engine
.execute("recovery.restore", json!({"paths": ["src/a.php"]}))
.unwrap();
assert_eq!(out.value["failed"], json!([]));
let id = out.value["restored"][0]["id"].as_u64().unwrap();
assert!(
out.events
.contains(&Event::DirtyChanged { id, dirty: true })
);
let got = run(&mut engine, "buffer.get", json!({"id": id}));
assert_eq!(got["text"], expected);
assert_eq!(got["lineEnding"], "crlf");
assert_eq!(got["dirty"], true);
assert!(engine.state().buffers.get(id).unwrap().bom);
run(&mut engine, "file.save", json!({"id": id}));
assert_eq!(
std::fs::read(setup.project.join("src/a.php")).unwrap(),
"\u{feff}<?php\r\n$x = 'ü';\r\necho 1;\r\n".as_bytes()
);
assert!(setup.entry_files().is_empty(), "saving removes the entry");
}
#[test]
fn close_force_discard_and_project_close_remove_entries() {
let setup = Setup::new(&[("a.php", b"a"), ("b.php", b"b"), ("c.php", b"c")]);
let (mut engine, _) = setup.engine();
let a = open(&mut engine, "a.php");
let b = open(&mut engine, "b.php");
let c = open(&mut engine, "c.php");
for id in [a, b, c] {
edit(&mut engine, id, 0, 0, "x");
}
engine.flush_journal().unwrap();
assert_eq!(setup.entry_files().len(), 3);
run(&mut engine, "buffer.close", json!({"id": a, "force": true}));
assert_eq!(setup.entry_files().len(), 2);
let out = run(&mut engine, "recovery.discard", json!({"paths": ["b.php"]}));
assert_eq!(out["discarded"], json!(["b.php"]));
assert_eq!(setup.entry_files().len(), 1);
engine.flush_journal().unwrap();
assert_eq!(
setup.entry_files().len(),
2,
"a discarded entry of a dirty open buffer is journaled again"
);
run(&mut engine, "project.close", json!({"force": true}));
assert!(setup.entry_files().is_empty());
assert!(engine.state().journal.is_none());
}
#[test]
fn entries_of_a_crashed_session_survive_until_restored_or_discarded() {
let setup = Setup::new(&[("a.php", b"a"), ("b.php", b"b")]);
let (mut engine, _) = setup.engine();
for path in ["a.php", "b.php"] {
let id = open(&mut engine, path);
edit(&mut engine, id, 1, 1, "!");
}
engine.flush_journal().unwrap();
drop(engine);
let (mut engine, events) = setup.engine();
assert_eq!(recovery_entries(&events).unwrap().len(), 2);
let id = open(&mut engine, "a.php");
run(&mut engine, "buffer.close", json!({"id": id}));
assert_eq!(
setup.entry_files().len(),
2,
"closing a clean buffer keeps the old entry"
);
let out = run(&mut engine, "recovery.discard", Value::Null);
assert_eq!(out["discarded"], json!(["a.php", "b.php"]));
assert!(setup.entry_files().is_empty());
assert_eq!(
run(&mut engine, "recovery.list", Value::Null)["entries"],
json!([])
);
}
#[test]
fn restore_reports_missing_entries_and_newer_unsaved_buffers() {
let setup = Setup::new(&[("a.php", b"a"), ("b.php", b"b")]);
let (mut engine, _) = setup.engine();
for path in ["a.php", "b.php"] {
let id = open(&mut engine, path);
edit(&mut engine, id, 1, 1, "1");
}
engine.flush_journal().unwrap();
drop(engine);
let (mut engine, _) = setup.engine();
let a = open(&mut engine, "a.php");
let b = open(&mut engine, "b.php");
edit(&mut engine, b, 0, 0, "newer");
let out = run(
&mut engine,
"recovery.restore",
json!({"paths": ["a.php", "b.php", "nope.php"]}),
);
assert_eq!(out["restored"], json!([{"path": "a.php", "id": a}]));
let failed: Vec<&str> = out["failed"]
.as_array()
.unwrap()
.iter()
.map(|f| f["path"].as_str().unwrap())
.collect();
assert_eq!(failed, vec!["nope.php", "b.php"]);
assert_eq!(
run(&mut engine, "buffer.get", json!({"id": a}))["text"],
"a1"
);
assert_eq!(
run(&mut engine, "buffer.get", json!({"id": b}))["text"],
"newerb"
);
}
#[test]
fn disk_changes_after_the_entry_are_flagged() {
let setup = Setup::new(&[("a.php", b"a"), ("gone.php", b"g")]);
let (mut engine, _) = setup.engine();
for path in ["a.php", "gone.php"] {
let id = open(&mut engine, path);
edit(&mut engine, id, 0, 0, "x");
}
engine.flush_journal().unwrap();
drop(engine);
std::thread::sleep(Duration::from_millis(50));
std::fs::write(setup.project.join("a.php"), "changed").unwrap();
std::fs::remove_file(setup.project.join("gone.php")).unwrap();
let (mut engine, events) = setup.engine();
let entries = recovery_entries(&events).unwrap();
assert_eq!(entries[0]["path"], "a.php");
assert_eq!(entries[0]["diskChanged"], true);
assert_eq!(entries[1]["path"], "gone.php");
assert_eq!(entries[1]["diskChanged"], true);
let out = run(&mut engine, "recovery.restore", Value::Null);
assert_eq!(out["restored"].as_array().unwrap().len(), 2);
let gone = out["restored"][1]["id"].as_u64().unwrap();
assert_eq!(
run(&mut engine, "buffer.get", json!({"id": gone}))["text"],
"xg"
);
run(&mut engine, "file.save", json!({"id": gone}));
assert_eq!(
std::fs::read_to_string(setup.project.join("gone.php")).unwrap(),
"xg"
);
}
#[test]
fn corrupt_entries_are_skipped_with_a_warning() {
let setup = Setup::new(&[("a.php", b"a")]);
let (mut engine, _) = setup.engine();
let id = open(&mut engine, "a.php");
edit(&mut engine, id, 0, 0, "ok ");
engine.flush_journal().unwrap();
drop(engine);
let dir = setup.journal_dir();
let bad_cases: [(&str, &[u8]); 4] = [
("0000000000000001.json", b"{not json"),
("0000000000000002.json", b"\xff\xfe\x00"),
(
"0000000000000003.json",
br#"{"path":"../evil.php","text":"x","lineEnding":"lf","bom":false,"savedAt":1}"#,
),
(
"0000000000000004.json",
br#"{"path":"a.php","text":"x","lineEnding":"lf","bom":false,"savedAt":1}"#,
),
];
for (name, bytes) in bad_cases {
std::fs::write(dir.join(name), bytes).unwrap();
}
let (mut engine, events) = setup.engine();
let entries = recovery_entries(&events).unwrap();
assert_eq!(entries.len(), 1, "only the good entry: {entries:?}");
assert_eq!(entries[0]["path"], "a.php");
let warnings: Vec<&Event> = events
.iter()
.filter(|e| matches!(e, Event::Notification { message_key: Some(k), .. } if k == journal::KEY_CORRUPT))
.collect();
assert_eq!(warnings.len(), 4);
for (name, _) in bad_cases {
assert!(
dir.join(format!("{name}.bad")).is_file(),
"{name} kept aside"
);
assert!(!dir.join(name).exists());
}
let out = run(&mut engine, "recovery.restore", Value::Null);
let id = out["restored"][0]["id"].as_u64().unwrap();
assert_eq!(
run(&mut engine, "buffer.get", json!({"id": id}))["text"],
"ok a"
);
assert!(
engine.take_events().iter().all(
|e| !matches!(e, Event::Notification { message_key: Some(k), .. } if k == journal::KEY_CORRUPT)
),
"reported once"
);
}
#[test]
fn an_engine_without_persistence_has_no_journal() {
let setup = Setup::new(&[("a.php", b"a")]);
let mut engine = Engine::new().unwrap();
run(&mut engine, "project.open", json!({"path": setup.project}));
let id = open(&mut engine, "a.php");
edit(&mut engine, id, 0, 0, "x");
engine.flush_journal().unwrap();
assert!(engine.state().journal.is_none());
assert_eq!(
run(&mut engine, "recovery.list", Value::Null)["entries"],
json!([])
);
assert!(!setup.config.data_dir.exists());
}
#[test]
fn an_unwritable_journal_warns_once_and_keeps_editing() {
let setup = Setup::new(&[("a.php", b"a")]);
std::fs::create_dir_all(&setup.config.data_dir).unwrap();
std::fs::write(setup.config.data_dir.join("recovery"), "a file").unwrap();
let (mut engine, _) = setup.engine();
let id = open(&mut engine, "a.php");
edit(&mut engine, id, 0, 0, "x");
let events = engine.flush_journal().unwrap();
assert!(events.iter().any(|e| matches!(
e,
Event::Notification { message_key: Some(k), .. } if k == journal::KEY_WRITE_FAILED
)));
edit(&mut engine, id, 0, 0, "y");
assert!(engine.flush_journal().unwrap().is_empty(), "warned once");
assert_eq!(
run(&mut engine, "buffer.get", json!({"id": id}))["text"],
"yxa"
);
}
#[test]
fn journal_write_cost_for_a_one_megabyte_buffer() {
let line = "$value = str_repeat('lutra', 10); // äöü\n";
let text = line.repeat(1024 * 1024 / line.len() + 1);
let setup = Setup::new(&[("big.php", text.as_bytes())]);
let (mut engine, _) = setup.engine();
let id = open(&mut engine, "big.php");
let mut samples = Vec::new();
for i in 0..10 {
edit(&mut engine, id, 0, 0, &i.to_string());
let start = Instant::now();
engine.flush_journal().unwrap();
samples.push(start.elapsed());
}
samples.sort();
let median = samples[samples.len() / 2];
eprintln!(
"journal write of {} bytes: median {:?}, min {:?}, max {:?}",
text.len(),
median,
samples[0],
samples[samples.len() - 1]
);
assert!(median < Duration::from_millis(500), "median {median:?}");
let size = std::fs::metadata(&setup.entry_files()[0]).unwrap().len();
assert!(size > 1024 * 1024);
}
/// One random edit: where (as a fraction of the text), how much to delete,
/// and what to insert.
fn edit_strategy() -> impl Strategy<Value = (f64, usize, String)> {
(
0.0..=1.0f64,
0usize..6,
prop::string::string_regex("(a|ü|😀|\r\n|\n|\r| |x){0,6}").unwrap(),
)
}
/// Applies random edits through `buffer.edit`; edits whose offsets fall
/// inside a character or a CRLF are rejected by the engine and skipped.
fn apply_random(engine: &mut Engine, id: u64, edits: &[(f64, usize, String)]) {
for (at, del, insert) in edits {
let got = run(engine, "buffer.get", json!({"id": id}));
let len = got["text"].as_str().unwrap().encode_utf16().count();
let from = ((len as f64) * at) as usize;
let to = (from + del).min(len);
let _ = engine.execute(
"buffer.edit",
json!({"id": id, "baseVersion": got["version"], "edits": [{"from": from, "to": to, "insert": insert}]}),
);
}
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(24))]
#[test]
fn random_edits_then_crash_restore_the_buffer(
crlf in any::<bool>(),
bom in any::<bool>(),
edits in prop::collection::vec(edit_strategy(), 1..20),
more in prop::collection::vec(edit_strategy(), 0..5),
) {
let ending = if crlf { "\r\n" } else { "\n" };
let mut bytes = Vec::new();
if bom {
bytes.extend_from_slice(b"\xEF\xBB\xBF");
}
bytes.extend_from_slice(format!("<?php{ending}echo 1;{ending}").as_bytes());
let setup = Setup::new(&[("a.php", &bytes)]);
let (mut engine, _) = setup.engine();
let id = open(&mut engine, "a.php");
apply_random(&mut engine, id, &edits);
engine.flush_journal().unwrap();
let before = engine.state().buffers.get(id).unwrap().clone();
apply_random(&mut engine, id, &more);
drop(engine);
let (mut engine, events) = setup.engine();
let offered = recovery_entries(&events).is_some();
prop_assert_eq!(offered, before.dirty());
if !offered {
return Ok(());
}
let out = run(&mut engine, "recovery.restore", Value::Null);
let rid = out["restored"][0]["id"].as_u64().unwrap();
let after = engine.state().buffers.get(rid).unwrap();
prop_assert_eq!(after.buffer.text(), before.buffer.text());
prop_assert_eq!(after.buffer.line_ending(), before.buffer.line_ending());
prop_assert_eq!(after.buffer.save_text(), before.buffer.save_text());
prop_assert_eq!(after.bom, before.bom);
prop_assert!(after.dirty());
prop_assert!(Path::new(&setup.project).join("a.php").is_file());
}
}