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

@ -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);
}