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
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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 });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))?;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
191
crates/engine/src/cmd/recovery.rs
Normal file
191
crates/engine/src/cmd/recovery.rs
Normal 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 })))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue