forge
Explore
Log in

Don't you dare go Hollow

Autosave only arms for a project this window opened or saved

Browse files at this commit

a80334d6f47f4f16fc917eef51b31c63ea810bbdAhmet Kaan GümüşParent: 5134b1d
  • fix
    • autosave only arms for a project this window opened or saved
Show full message
🐛 fix(app): autosave only arms for a project this window opened or saved

A plain media open derived project_path (a guess at where a save would
land) without ever arming autosave for it -- so a dirty, never-saved
scratch session could write a sidecar beside a foreign .edith it never
touched, and the next real open of that file offered the scratch
timeline as "recovery" over the user's own saved work.

autosave_armed now gates every tick: set on install_project (opened
from the .edith itself) and on save_project's Ok, never on a media
open's derived guess. Declining the recovery notice now deletes the
stale sidecar instead of leaving it to reappear on every later open.
crates/app/src/main.rsmodified+12 -0
@@ -409,6 +409,17 @@ struct Player {
409409 /// `project_path` is never touched by either path, only a manual save
410410 /// writes the real file.
411411 recovery_sidecar: Option<PathBuf>,
412+ /// Whether `project_path` names a file this window itself put there --
413+ /// loaded from a `.edith` ([`Player::install_project`]) or written by a
414+ /// manual save ([`Player::save_project`]) -- rather than one merely
415+ /// derived beside an imported media file. [`Player::autosave_tick`]
416+ /// requires it: a path derived from `media.mp4` names `media.edith`
417+ /// whether or not that file is the user's own saved project, and writing
418+ /// a sidecar beside a *foreign* `.edith` the user never opened is what
419+ /// let a scratch timeline's autosave outdate and "recover" over a real
420+ /// one. `mark_dirty` does not touch this -- an edit earns nothing an open
421+ /// or save has not already granted.
422+ autosave_armed: bool,
412423 /// Which stroke means what, and what every shortcut on screen is called.
413424 /// The one place either question is answered.
414425 keymap: Keymap,
@@ -842,6 +853,7 @@ fn main() {
842853 autosave_last_edit: None,
843854 autosave_last_run: None,
844855 recovery_sidecar: None,
856+ autosave_armed: false,
845857 keymap: keymap.clone(),
846858 keys_open: false,
847859 keys_search: String::new(),
crates/app/src/player/library.rsmodified+79 -1
@@ -982,6 +982,10 @@ impl Player {
982982 // opening another clip is not a change of mind about that.
983983 self.export_path = retarget(&export_path(path), self.format);
984984 self.project_path = project_path(path);
985+ // Derived, not opened: `project_path` here is a guess at where
986+ // a save would land, not a file this window has any business
987+ // writing an autosave beside until a manual save earns it.
988+ self.autosave_armed = false;
985989 self.name = file_name(path).into();
986990 self.reset_after_reseek();
987991 let name = file_name(path);
@@ -1260,6 +1264,9 @@ impl Player {
12601264 self.auto_proxies_on = session.auto_proxies();
12611265 }
12621266 self.project_path = path.to_path_buf();
1267+ // Opened from the `.edith` itself: the file this window will
1268+ // autosave beside is the one the user actually chose.
1269+ self.autosave_armed = true;
12631270 self.name = file_name(path).into();
12641271 // A copied clip names its source by index, which means a
12651272 // different file -- or none -- in another project.
@@ -1328,6 +1335,10 @@ impl Player {
13281335 }
13291336 self.autosave_dirty = false;
13301337 self.autosave_last_edit = None;
1338+ // A manual save is exactly the event this flag exists to
1339+ // require: whatever `project_path` was derived from, it now
1340+ // names a file the user just chose to write.
1341+ self.autosave_armed = true;
13311342 format!("SAVED {}", file_name(&self.project_path))
13321343 }
13331344 Some(Err(e)) => format!("SAVE FAILED: {e}"),
@@ -1381,6 +1392,17 @@ impl Player {
13811392 self.recovery_sidecar = None;
13821393 }
13831394
1395+ /// Answers the recovery notice with anything but `enter`: the sidecar is
1396+ /// discarded, on disk as well as in memory. Left behind, an unread
1397+ /// autosave from a scratch session would keep outdating the real project
1398+ /// it sits beside and reappear as "recovery" on every open of that file
1399+ /// (reference-editor behaviour: a declined backup is discarded, not kept
1400+ /// around to ask again). Pure at the path level so it needs no `Player`
1401+ /// to test -- a missing sidecar is not an error, only a no-op.
1402+ pub(crate) fn discard_sidecar(sidecar: &Path) {
1403+ let _ = std::fs::remove_file(sidecar);
1404+ }
1405+
13841406 /// Whether `project_path` has a sidecar beside it that outdates the file
13851407 /// itself: the sidecar exists, the project exists, and the sidecar's mtime
13861408 /// is strictly the younger of the two. Pure and file-system-only -- no
@@ -1416,6 +1438,15 @@ impl Player {
14161438 .detach();
14171439 }
14181440
1441+ /// Whether a tick is allowed to write at all: armed (`project_path` names
1442+ /// a file this window opened or saved -- see `autosave_armed`), dirty,
1443+ /// and not mid-export. Pure and split out from [`autosave_tick`](Self::autosave_tick)
1444+ /// for the same reason [`recovery_offer`](Self::recovery_offer) is: a test
1445+ /// can ask it with three bools, with no `Player` to build.
1446+ fn autosave_gate(armed: bool, dirty: bool, exporting: bool) -> bool {
1447+ armed && dirty && !exporting
1448+ }
1449+
14191450 /// One tick of the loop above. Writes the sidecar when an edit has been
14201451 /// idle for [`AUTOSAVE_DEBOUNCE`] (the debounce) or dirty edits have gone
14211452 /// unwritten for [`AUTOSAVE_PERIOD`] (the periodic safety net for a run
@@ -1425,7 +1456,7 @@ impl Player {
14251456 /// write is not something a person can do anything about right now, and a
14261457 /// modal over it would be worse than the risk it is guarding against.
14271458 fn autosave_tick(&mut self, cx: &mut Context<Self>) {
1428- if !self.autosave_dirty || self.exporting().is_some() {
1459+ if !Self::autosave_gate(self.autosave_armed, self.autosave_dirty, self.exporting().is_some()) {
14291460 return;
14301461 }
14311462 let now = Instant::now();
@@ -1683,4 +1714,51 @@ mod autosave_tests {
16831714
16841715 std::fs::remove_dir_all(&dir).unwrap();
16851716 }
1717+
1718+ /// The bug this module exists to close: a plain media open derives
1719+ /// `project_path` (a guess at where a save would land) but never arms
1720+ /// autosave for it -- so a dirty, never-saved scratch session writes no
1721+ /// sidecar beside whatever `.edith` that guess happens to name, even if
1722+ /// one already exists as somebody's real saved project.
1723+ #[test]
1724+ fn unarmed_session_never_writes_even_when_dirty() {
1725+ assert!(!Player::autosave_gate(false, true, false));
1726+ }
1727+
1728+ /// Both doors that earn the write: opening the `.edith` itself
1729+ /// (`install_project`) and a manual save (`save_project`), each setting
1730+ /// `autosave_armed = true` before this gate is ever asked again.
1731+ #[test]
1732+ fn armed_and_dirty_session_writes() {
1733+ assert!(Player::autosave_gate(true, true, false));
1734+ // Still withheld while an export is running, armed or not.
1735+ assert!(!Player::autosave_gate(true, true, true));
1736+ // Still withheld while clean, armed or not.
1737+ assert!(!Player::autosave_gate(true, false, false));
1738+ }
1739+
1740+ /// Declining the recovery notice (any key but `enter`, in
1741+ /// [`render::key handler`](crate::render)) discards the sidecar on disk,
1742+ /// not just the in-memory offer -- otherwise the same stale sidecar
1743+ /// outdates the project again on the very next open.
1744+ #[test]
1745+ fn declining_recovery_deletes_the_sidecar() {
1746+ let dir = std::env::temp_dir().join(format!(
1747+ "edith-autosave-decline-test-{}-{:?}",
1748+ std::process::id(),
1749+ std::thread::current().id()
1750+ ));
1751+ std::fs::create_dir_all(&dir).unwrap();
1752+ let sidecar = dir.join("p.edith.autosave");
1753+ std::fs::write(&sidecar, b"stale").unwrap();
1754+
1755+ Player::discard_sidecar(&sidecar);
1756+ assert!(!sidecar.exists());
1757+
1758+ // A missing sidecar (already recovered, or declined twice) is a
1759+ // no-op, not an error.
1760+ Player::discard_sidecar(&sidecar);
1761+
1762+ std::fs::remove_dir_all(&dir).unwrap();
1763+ }
16861764 }
crates/app/src/render.rsmodified+2 -2
@@ -216,8 +216,8 @@ impl Render for Player {
216216 if this.recovery_sidecar.is_some() {
217217 if key == "enter" {
218218 this.recover_from_sidecar(cx);
219- } else {
220- this.recovery_sidecar = None;
219+ } else if let Some(sidecar) = this.recovery_sidecar.take() {
220+ Player::discard_sidecar(&sidecar);
221221 }
222222 }
223223 // Any key answers the message on the bar and brings the next of