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

@ -278,6 +278,7 @@ pub fn pump(state: &mut State) {
}
for path in gone {
if let Some(id) = state.buffers.find(&path) {
state.buffers.mark_conflict(id);
state.emit(Event::FileChangedOnDisk {
path: path_string(&path),
id,
@ -297,24 +298,22 @@ pub fn pump(state: &mut State) {
/// Brings buffer `id` in line with its file after the file changed: a clean
/// buffer takes the new text (unless it already matches, as after our own
/// save), a dirty one keeps its text and the client is told. A file that
/// cannot be read is reported like a removed one.
/// save), a dirty one keeps its text and the client is told, and the buffer
/// is marked as in conflict ([`crate::buffers::Buffers::mark_conflict`]).
/// A dirty buffer whose file holds exactly what Lutra last wrote is left
/// alone: that is our own save reported after the user typed again (an
/// autosave followed by a keystroke). A file that cannot be read is reported
/// like a removed one.
fn reload(state: &mut State, id: BufferId, abs: &Path) {
let Some(open) = state.buffers.get(id) else {
return;
};
let path = path_string(&open.path);
if open.dirty() {
state.emit(Event::FileChangedOnDisk {
path,
id,
removed: false,
});
return;
}
let dirty = open.dirty();
let file = match lutra_vfs::read_text(abs) {
Ok(file) => file,
Err(_) => {
state.buffers.mark_conflict(id);
state.emit(Event::FileChangedOnDisk {
path,
id,
@ -323,17 +322,40 @@ fn reload(state: &mut State, id: BufferId, abs: &Path) {
return;
}
};
if dirty {
if state.buffers.is_own_write(
id,
&crate::journal::hex_hash(file.text.as_bytes()),
file.bom,
) {
return;
}
state.buffers.mark_conflict(id);
state.emit(Event::FileChangedOnDisk {
path,
id,
removed: false,
});
return;
}
let Some(open) = state.buffers.get(id) else {
return;
};
if file.text == open.buffer.save_text() && file.bom == open.bom {
state.buffers.clear_conflict(id);
return;
}
let Some(open) = state.buffers.get_mut(id) else {
return;
};
let hash = crate::journal::hex_hash(file.text.as_bytes());
open.buffer = Buffer::new(&file.text);
open.bom = file.bom;
open.version = open.version.saturating_add(1);
open.saved_version = open.version;
let version = open.version;
state.buffers.clear_conflict(id);
state.buffers.note_written(id, hash, file.bom);
state.emit(Event::BufferChanged { id, version });
}