M3.10: autosave and flushDirty

Autosave saves dirty buffers when the window loses focus (DOM blur and
visibilitychange), when the active tab changes and after
editor.autosave.idleSeconds (default 15) without an edit; three settings,
all on by default. flushDirty() in ui/src/stores/autosave.ts is the one
save-all-dirty-and-wait entry point for later deployment, run and
terminal features: it waits for pending buffer sync, then runs
file.saveAll {skipConflicts: true}; one toast per failed file.

Engine: file.saveAll takes skipConflicts and returns skipped; Buffers
tracks buffers in conflict with their file and the hash of the last
read/written content, so our own save reported late is not a conflict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-20 20:59:44 +02:00
parent 228669036c
commit e1dc24dc25
18 changed files with 996 additions and 37 deletions

View file

@ -1,4 +1,12 @@
//! File commands: `file.open`, `file.save`, `file.saveAll`.
//!
//! `file.saveAll {skipConflicts: true}` is the engine half of "save every
//! dirty buffer and wait" (M3.10): autosave and anything that must see the
//! files on disk first (deployment, run configurations, terminal commands)
//! call it through the frontend's `flushDirty()`. It extends `file.saveAll`
//! instead of adding a command, so there is one save-all with one result
//! shape; the flag only adds the rule that a buffer in conflict with its file
//! on disk is skipped, because only the user can settle that.
use serde::Deserialize;
use serde_json::{Value, json};
@ -57,6 +65,9 @@ impl Command for Open {
bom: file.bom,
};
let new_id = state.buffers.insert(buffer);
state
.buffers
.note_written(new_id, journal::hex_hash(file.text.as_bytes()), file.bom);
state.emit(Event::BufferOpened {
id: new_id,
path: path_string(&rel),
@ -66,7 +77,9 @@ impl Command for Open {
}
/// Writes buffer `id` to its file, keeping its line ending and byte order
/// mark, and marks it clean.
/// mark, marks it clean, clears a disk conflict (saving is the user's answer
/// to it) and remembers what was written, so the watcher's report of this
/// write is not taken for an outside change.
///
/// # Errors
/// [`EngineError::Failed`] for an unknown buffer or a failed write.
@ -76,7 +89,13 @@ fn save(state: &mut State, command: &str, id: BufferId) -> Result<u64, EngineErr
.get(id)
.ok_or_else(|| EngineError::Failed(format!("no buffer {id}")))?;
let abs = absolute(state, command, &open.path)?;
lutra_vfs::write_text(&abs, &open.buffer.save_text(), open.bom).map_err(vfs_error)?;
let text = open.buffer.save_text();
let bom = open.bom;
lutra_vfs::write_text(&abs, &text, bom).map_err(vfs_error)?;
state
.buffers
.note_written(id, journal::hex_hash(text.as_bytes()), bom);
state.buffers.clear_conflict(id);
let open = state
.buffers
.get_mut(id)
@ -124,8 +143,20 @@ impl Command for Save {
}
}
/// Params of `file.saveAll`.
#[derive(Deserialize, Default)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
struct SaveAllParams {
/// Leave buffers in conflict with their file on disk unsaved and list
/// them under `skipped`.
#[serde(default)]
skip_conflicts: bool,
}
/// `file.saveAll`: writes every dirty buffer. A failure doesn't stop the
/// others; failures are listed in the result.
/// others; failures are listed in the result. With `skipConflicts` a dirty
/// buffer whose file changed or disappeared on disk is not written and is
/// listed under `skipped`.
pub struct SaveAll;
impl Command for SaveAll {
@ -135,7 +166,7 @@ impl Command for SaveAll {
label: "Save All",
menu_path: Some("File/Save All"),
default_shortcut: Some("Mod+Alt+S"),
params: "none; returns { saved: [id], failed: [{ id, message }] }",
params: "{ skipConflicts?: boolean (leave buffers whose file changed on disk unsaved) }; returns { saved: [id], failed: [{ id, path, message }], skipped: [id] }",
}
}
@ -143,15 +174,30 @@ impl Command for SaveAll {
state.has_project()
}
fn run(&self, state: &mut State, _params: Value) -> Result<Outcome, EngineError> {
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
let p: SaveAllParams = parse(self.spec().id, params)?;
let mut saved = Vec::new();
let mut failed = Vec::new();
let mut skipped = Vec::new();
for id in state.buffers.dirty_ids() {
if p.skip_conflicts && state.buffers.has_conflict(id) {
skipped.push(id);
continue;
}
match save(state, self.spec().id, id) {
Ok(_) => saved.push(id),
Err(e) => failed.push(json!({ "id": id, "message": e.to_string() })),
Err(e) => {
let path = state
.buffers
.get(id)
.map(|b| path_string(&b.path))
.unwrap_or_default();
failed.push(json!({ "id": id, "path": path, "message": e.to_string() }));
}
}
}
Ok(Outcome::value(json!({ "saved": saved, "failed": failed })))
Ok(Outcome::value(
json!({ "saved": saved, "failed": failed, "skipped": skipped }),
))
}
}