A2: engine half of files changed on disk and Local History v1

- Disk conflicts per buffer (changed / removed), reported in buffer.get
  as diskConflict; diskConflictResolved when settled.
- Every watcher event only means "look again": delete + create (git
  checkout, atomic saves) reloads instead of losing the buffer; a file that
  disappears keeps its buffer as unsaved.
- file.save checks mtime + hash and fails with disk_changed instead of
  overwriting a newer file (overwrite: true after asking); saveAll lists
  such buffers under diskChanged. New file.reload and file.readDisk.
- Local History store in the data dir (content-addressed SHA-256 blobs,
  index.jsonl, 14 days / 200 MB, pruned on open); snapshots on save,
  before reloads, on deletion, before lsp.rename and file-changing git
  commands. Commands history.list / history.read / history.revert.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-11-08 12:09:16 +01:00
parent 7f309dbc4e
commit dfe2f19ca5
17 changed files with 1953 additions and 116 deletions

View file

@ -0,0 +1,260 @@
//! Local History (C10 A2) through the engine: snapshots on save, reload,
//! deletion and revert, stored in the data directory, never in the project.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use std::fs;
use std::time::{Duration, Instant};
use lutra_engine::{Engine, EngineConfig, Event, Outcome};
use serde_json::{Value, json};
use tempfile::TempDir;
/// A project folder, a data directory and an engine with that project open.
struct Fixture {
project: TempDir,
data: TempDir,
_config: TempDir,
engine: Engine,
}
fn fixture(files: &[(&str, &str)]) -> Fixture {
let project = tempfile::tempdir().unwrap();
for (path, text) in files {
fs::write(project.path().join(path), text).unwrap();
}
let data = tempfile::tempdir().unwrap();
let config = tempfile::tempdir().unwrap();
let mut engine = Engine::with_config(EngineConfig {
config_dir: config.path().to_path_buf(),
data_dir: data.path().to_path_buf(),
})
.unwrap();
engine
.execute("project.open", json!({"path": project.path()}))
.unwrap();
Fixture {
project,
data,
_config: config,
engine,
}
}
fn run(engine: &mut Engine, command: &str, params: Value) -> Outcome {
engine
.execute(command, params)
.unwrap_or_else(|e| panic!("{command} failed: {e}"))
}
fn open(engine: &mut Engine, path: &str) -> u64 {
run(engine, "file.open", json!({"path": path})).value["id"]
.as_u64()
.unwrap()
}
fn replace(engine: &mut Engine, id: u64, text: &str) {
let now = run(engine, "buffer.get", json!({"id": id})).value;
let len = now["text"].as_str().unwrap().encode_utf16().count();
run(
engine,
"buffer.edit",
json!({"id": id, "baseVersion": now["version"], "edits": [{"from": 0, "to": len, "insert": text}]}),
);
}
/// `(reason, text)` of every entry of `path`, newest first.
fn entries(engine: &mut Engine, path: &str) -> Vec<(String, String)> {
let list = run(engine, "history.list", json!({"path": path})).value;
list["entries"]
.as_array()
.unwrap()
.iter()
.map(|e| {
let text =
run(engine, "history.read", json!({"path": path, "id": e["id"]})).value["text"]
.as_str()
.unwrap()
.to_string();
(e["reason"].as_str().unwrap().to_string(), text)
})
.collect()
}
fn pair(reason: &str, text: &str) -> (String, String) {
(reason.to_string(), text.to_string())
}
fn wait_for(engine: &mut Engine, pred: impl Fn(&Event) -> bool) {
let deadline = Instant::now() + Duration::from_secs(5);
while Instant::now() < deadline {
if engine.pump_vfs().unwrap().iter().any(&pred) {
return;
}
std::thread::sleep(Duration::from_millis(25));
}
panic!("event not seen within 5 s");
}
#[test]
fn saves_keep_the_original_and_every_saved_text() {
let mut f = fixture(&[("a.php", "original")]);
let id = open(&mut f.engine, "a.php");
replace(&mut f.engine, id, "first");
run(&mut f.engine, "file.save", json!({"id": id}));
run(&mut f.engine, "file.save", json!({"id": id}));
replace(&mut f.engine, id, "second");
run(&mut f.engine, "file.saveAll", json!({}));
assert_eq!(
entries(&mut f.engine, "a.php"),
vec![
pair("save", "second"),
pair("save", "first"),
pair("beforeSave", "original"),
],
"a save of an unchanged text adds nothing"
);
let list = run(&mut f.engine, "history.list", json!({"path": "a.php"})).value;
let entry = &list["entries"][0];
assert!(entry["time"].as_u64().unwrap() > 0);
assert_eq!(entry["size"], 6);
}
#[test]
fn the_history_lives_in_the_data_dir_and_survives_a_reopen() {
let mut f = fixture(&[("a.php", "one")]);
let id = open(&mut f.engine, "a.php");
replace(&mut f.engine, id, "two");
run(&mut f.engine, "file.save", json!({"id": id}));
let names: Vec<String> = fs::read_dir(f.project.path())
.unwrap()
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
.collect();
assert_eq!(
names,
vec!["a.php".to_string()],
"nothing inside the project"
);
assert!(f.data.path().join("history").is_dir());
run(
&mut f.engine,
"project.open",
json!({"path": f.project.path()}),
);
assert_eq!(entries(&mut f.engine, "a.php").len(), 2);
}
#[test]
fn reloads_and_deletions_keep_the_buffers_text() {
let mut f = fixture(&[("a.php", "mine")]);
let id = open(&mut f.engine, "a.php");
replace(&mut f.engine, id, "unsaved");
fs::write(f.project.path().join("a.php"), "theirs").unwrap();
wait_for(&mut f.engine, |e| {
matches!(e, Event::FileChangedOnDisk { removed: false, .. })
});
run(&mut f.engine, "file.reload", json!({"id": id}));
assert_eq!(
entries(&mut f.engine, "a.php"),
vec![
pair("beforeReload", "unsaved"),
pair("externalChange", "theirs")
]
);
fs::remove_file(f.project.path().join("a.php")).unwrap();
wait_for(&mut f.engine, |e| {
matches!(e, Event::FileChangedOnDisk { removed: true, .. })
});
assert_eq!(
entries(&mut f.engine, "a.php")[0],
pair("deleted", "theirs")
);
}
#[test]
fn revert_edits_the_buffer_and_keeps_what_it_replaced() {
let mut f = fixture(&[("a.php", "one")]);
let id = open(&mut f.engine, "a.php");
replace(&mut f.engine, id, "two");
run(&mut f.engine, "file.save", json!({"id": id}));
replace(&mut f.engine, id, "three, unsaved");
let list = run(&mut f.engine, "history.list", json!({"path": "a.php"})).value;
let original = list["entries"][1]["id"].clone();
let out = run(
&mut f.engine,
"history.revert",
json!({"path": "a.php", "id": original}),
);
assert_eq!(out.value["id"], id);
assert_eq!(out.value["dirty"], true, "an ordinary, unsaved edit");
assert!(
out.events
.iter()
.any(|e| matches!(e, Event::BufferChanged { id: got, .. } if *got == id))
);
let now = run(&mut f.engine, "buffer.get", json!({"id": id})).value;
assert_eq!(now["text"], "one");
assert_eq!(
fs::read_to_string(f.project.path().join("a.php")).unwrap(),
"two"
);
assert_eq!(
entries(&mut f.engine, "a.php")[0],
pair("beforeRevert", "three, unsaved")
);
}
#[test]
fn revert_opens_a_closed_file() {
let mut f = fixture(&[("a.php", "one")]);
let id = open(&mut f.engine, "a.php");
replace(&mut f.engine, id, "two");
run(&mut f.engine, "file.save", json!({"id": id}));
run(&mut f.engine, "buffer.close", json!({"id": id}));
let list = run(&mut f.engine, "history.list", json!({"path": "a.php"})).value;
let out = run(
&mut f.engine,
"history.revert",
json!({"path": "a.php", "id": list["entries"][1]["id"]}),
);
let new_id = out.value["id"].as_u64().unwrap();
assert!(
out.events
.iter()
.any(|e| matches!(e, Event::BufferOpened { id, .. } if *id == new_id))
);
let now = run(&mut f.engine, "buffer.get", json!({"id": new_id})).value;
assert_eq!(now["text"], "one");
}
#[test]
fn unknown_entries_and_projects_without_history_fail_clearly() {
let mut f = fixture(&[("a.php", "one")]);
let err = f
.engine
.execute("history.read", json!({"path": "a.php", "id": 99}))
.unwrap_err();
assert_eq!(err.code(), "failed");
assert!(
err.to_string().contains("no Local History entry 99"),
"{err}"
);
assert_eq!(
run(&mut f.engine, "history.list", json!({"path": "a.php"})).value["entries"],
json!([])
);
let dir = tempfile::tempdir().unwrap();
let mut bare = Engine::new().unwrap();
bare.execute("project.open", json!({"path": dir.path()}))
.unwrap();
let err = bare
.execute("history.list", json!({"path": "a.php"}))
.unwrap_err();
assert!(err.to_string().contains("not available"), "{err}");
}

View file

@ -267,7 +267,7 @@ fn save_all_saves_every_dirty_buffer() {
let out = run(&mut engine, "file.saveAll", Value::Null);
assert_eq!(
out.value,
json!({"saved": [a, b], "failed": [], "skipped": []})
json!({"saved": [a, b], "failed": [], "skipped": [], "diskChanged": []})
);
assert_eq!(fs::read_to_string(dir.path().join("b.php")).unwrap(), ">b");
}
@ -603,7 +603,7 @@ fn save_all_can_skip_buffers_in_conflict_with_the_disk() {
let out = run(&mut engine, "file.saveAll", json!({"skipConflicts": true}));
assert_eq!(
out.value,
json!({"saved": [mine], "failed": [], "skipped": [other]})
json!({"saved": [mine], "failed": [], "skipped": [other], "diskChanged": []})
);
assert_eq!(
fs::read_to_string(dir.path().join("other.php")).unwrap(),
@ -614,7 +614,19 @@ fn save_all_can_skip_buffers_in_conflict_with_the_disk() {
"xone"
);
run(&mut engine, "file.save", json!({"id": other}));
let err = engine
.execute("file.save", json!({"id": other}))
.unwrap_err();
assert_eq!(
err.code(),
"disk_changed",
"a newer file is never overwritten silently"
);
run(
&mut engine,
"file.save",
json!({"id": other, "overwrite": true}),
);
assert_eq!(
fs::read_to_string(dir.path().join("other.php")).unwrap(),
"xone"
@ -682,3 +694,198 @@ fn typing_right_after_a_save_is_not_a_conflict() {
let out = run(&mut engine, "file.saveAll", json!({"skipConflicts": true}));
assert_eq!(out.value["saved"], json!([id]));
}
fn edit(engine: &mut Engine, id: u64, base: u64, at: u64, insert: &str) {
run(
engine,
"buffer.edit",
json!({"id": id, "baseVersion": base, "edits": [{"from": at, "to": at, "insert": insert}]}),
);
}
#[test]
fn reload_discards_mine_and_settles_the_conflict() {
let (dir, mut engine, _) = open_project(&[("a.php", b"one")]);
let (id, _) = open_file(&mut engine, "a.php");
edit(&mut engine, id, 1, 0, "mine ");
fs::write(dir.path().join("a.php"), "theirs").unwrap();
wait_for(
&mut engine,
|e| matches!(e, Event::FileChangedOnDisk { id: got, removed: false, .. } if *got == id),
);
assert_eq!(
run(&mut engine, "buffer.get", json!({"id": id})).value["diskConflict"],
"changed"
);
let out = run(&mut engine, "file.reload", json!({"id": id}));
assert_eq!(out.value["dirty"], false);
assert!(
out.events
.iter()
.any(|e| matches!(e, Event::DiskConflictResolved { id: got } if *got == id))
);
assert!(
out.events
.iter()
.any(|e| matches!(e, Event::DirtyChanged { id: got, dirty: false } if *got == id))
);
let now = run(&mut engine, "buffer.get", json!({"id": id})).value;
assert_eq!(now["text"], "theirs");
assert_eq!(now["diskConflict"], Value::Null);
}
#[test]
fn a_save_over_a_newer_file_asks_even_before_the_watcher_saw_it() {
let (dir, mut engine, _) = open_project(&[("a.php", b"one"), ("b.php", b"one")]);
let (a, _) = open_file(&mut engine, "a.php");
let (b, _) = open_file(&mut engine, "b.php");
edit(&mut engine, a, 1, 0, "x");
edit(&mut engine, b, 1, 0, "x");
std::thread::sleep(Duration::from_millis(30));
fs::write(dir.path().join("a.php"), "theirs, longer").unwrap();
let err = engine.execute("file.save", json!({"id": a})).unwrap_err();
assert_eq!(err.code(), "disk_changed");
assert_eq!(
fs::read_to_string(dir.path().join("a.php")).unwrap(),
"theirs, longer"
);
let out = run(&mut engine, "file.saveAll", json!({}));
assert_eq!(out.value["saved"], json!([b]));
assert_eq!(out.value["diskChanged"], json!([a]));
run(
&mut engine,
"file.save",
json!({"id": a, "overwrite": true}),
);
assert_eq!(
fs::read_to_string(dir.path().join("a.php")).unwrap(),
"xone"
);
}
#[test]
fn a_touched_file_with_the_same_text_saves_without_asking() {
let (dir, mut engine, _) = open_project(&[("a.php", b"one")]);
let (id, _) = open_file(&mut engine, "a.php");
edit(&mut engine, id, 1, 0, "x");
std::thread::sleep(Duration::from_millis(30));
fs::write(dir.path().join("a.php"), "one").unwrap();
run(&mut engine, "file.save", json!({"id": id}));
assert_eq!(
fs::read_to_string(dir.path().join("a.php")).unwrap(),
"xone"
);
}
#[test]
fn a_deleted_file_keeps_its_buffer_as_unsaved_until_it_comes_back() {
let (dir, mut engine, _) = open_project(&[("a.php", b"one")]);
let (id, _) = open_file(&mut engine, "a.php");
fs::remove_file(dir.path().join("a.php")).unwrap();
let events = wait_for(
&mut engine,
|e| matches!(e, Event::FileChangedOnDisk { id: got, removed: true, .. } if *got == id),
);
assert!(
events
.iter()
.any(|e| matches!(e, Event::DirtyChanged { id: got, dirty: true } if *got == id))
);
let now = run(&mut engine, "buffer.get", json!({"id": id})).value;
assert_eq!(now["text"], "one");
assert_eq!(now["dirty"], true);
assert_eq!(now["diskConflict"], "removed");
assert_eq!(
engine
.execute("buffer.close", json!({"id": id}))
.unwrap_err()
.code(),
"unsaved_changes",
"closing asks"
);
fs::write(dir.path().join("a.php"), "back, but different").unwrap();
wait_for(
&mut engine,
|e| matches!(e, Event::DiskConflictResolved { id: got } if *got == id),
);
let now = run(&mut engine, "buffer.get", json!({"id": id})).value;
assert_eq!(
now["text"], "back, but different",
"a buffer dirty only because its file was gone reloads like a clean one"
);
assert_eq!(now["dirty"], false);
}
#[test]
fn keep_as_unsaved_then_save_recreates_the_file() {
let (dir, mut engine, _) = open_project(&[("a.php", b"one")]);
let (id, _) = open_file(&mut engine, "a.php");
edit(&mut engine, id, 1, 0, "x");
fs::remove_file(dir.path().join("a.php")).unwrap();
wait_for(
&mut engine,
|e| matches!(e, Event::FileChangedOnDisk { id: got, removed: true, .. } if *got == id),
);
let out = run(&mut engine, "file.save", json!({"id": id}));
assert!(
out.events
.iter()
.any(|e| matches!(e, Event::DiskConflictResolved { id: got } if *got == id))
);
assert_eq!(
fs::read_to_string(dir.path().join("a.php")).unwrap(),
"xone"
);
}
#[test]
fn a_file_replaced_by_delete_and_create_reloads_instead_of_being_lost() {
let (dir, mut engine, _) = open_project(&[("a.php", b"one"), ("b.php", b"one")]);
let (clean, _) = open_file(&mut engine, "a.php");
let (dirty, _) = open_file(&mut engine, "b.php");
edit(&mut engine, dirty, 1, 0, "mine ");
for name in ["a.php", "b.php"] {
fs::remove_file(dir.path().join(name)).unwrap();
fs::write(dir.path().join(name), "checked out").unwrap();
}
let mut seen = wait_for(
&mut engine,
|e| matches!(e, Event::BufferChanged { id, .. } if *id == clean),
);
if !seen
.iter()
.any(|e| matches!(e, Event::FileChangedOnDisk { id, removed: false, .. } if *id == dirty))
{
seen.extend(wait_for(
&mut engine,
|e| matches!(e, Event::FileChangedOnDisk { id, removed: false, .. } if *id == dirty),
));
}
let a = run(&mut engine, "buffer.get", json!({"id": clean})).value;
assert_eq!(a["text"], "checked out");
assert_eq!(a["dirty"], false);
let b = run(&mut engine, "buffer.get", json!({"id": dirty})).value;
assert_eq!(
b["text"], "mine one",
"the dirty buffer keeps the user's text"
);
assert_eq!(b["diskConflict"], "changed");
}
#[test]
fn read_disk_answers_like_git_show() {
let (_dir, mut engine, _) =
open_project(&[("a.php", b"on disk"), ("bin.dat", b"\xff\xfe\x00x")]);
let (id, _) = open_file(&mut engine, "a.php");
edit(&mut engine, id, 1, 0, "mine ");
let out = run(&mut engine, "file.readDisk", json!({"path": "a.php"}));
assert_eq!(
out.value,
json!({"text": "on disk", "binary": false, "tooLarge": false})
);
let out = run(&mut engine, "file.readDisk", json!({"path": "gone.php"}));
assert_eq!(out.value["text"], Value::Null);
let out = run(&mut engine, "file.readDisk", json!({"path": "bin.dat"}));
assert_eq!(out.value["binary"], true);
}