SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Save: a design changed while it is written stays unsaved, and scripts wait for it (#6003) (#7619)
sascha2101e2d409e6edd7
diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rsindex 17d383a008..dcd91a6f55 100644--- a/crates/engine/src/cmd/file.rs+++ b/crates/engine/src/cmd/file.rs@@ -470,9 +470,11 @@ impl Session {}/// Take up a save that ran on `done`, a [`Session::scratch`] copy of this session that- /// `file.save` or `file.save_as` ran on (on another thread, so the window keeps drawing)- /// while this one did not change: the name the save gave the design, its- /// file, and its saved state, as running the command here would have left them.+ /// `file.save` or `file.save_as` ran on (on another thread, so the window keeps drawing):+ /// the name the save gave the design, its file, and its saved state, as running the command+ /// here would have left them. What is saved is the design the file holds, `done`'s, not this+ /// one's: a change made here while the save was written is not in the file, so it stays+ /// unsaved (#6003).pub fn adopt_save(&mut self, done: &Session) {if done.doc.name != self.doc.name {let (from, to) = (self.doc.name.clone(), done.doc.name.clone());@@ -481,7 +483,8 @@ impl Session {self.path.clone_from(&done.path);self.disk = done.disk;self.read_only = done.read_only;- self.mark_saved();+ self.saved = done.saved.clone();+ self.saved_visibility = done.saved_visibility.clone();}/// Another program or computer wrote the design's file since it was opened or last saved:@@ -923,6 +926,33 @@ mod tests {let _ = std::fs::remove_dir_all(&dir);}+ /// #6003: a session changed while its save ran on a copy takes up the save's file and name,+ /// but only what the file holds counts as saved: the change stays unsaved, and undoing it+ /// leaves nothing unsaved again. A hidden body is a change too.+ #[test]+ fn a_change_made_while_a_copy_saves_stays_unsaved() {+ let dir = std::env::temp_dir().join(format!("solidcraft-adopt-changed-{}", std::process::id()));+ std::fs::create_dir_all(&dir).unwrap();+ let path = dir.join("Bracket.solidcraft");+ for hide in [false, true] {+ let mut s = Session::default();+ s.execute("solid.box", &json!({"length": 10, "width": 10, "height": 10, "body_name": "Block"})).unwrap();+ let mut copy = s.scratch();+ copy.execute("file.save_as", &json!({"path": path.to_string_lossy()})).unwrap();+ if hide {+ s.execute("browser.visibility", &json!({"items": ["Block"], "visible": false})).unwrap();+ } else {+ s.execute("solid.sphere", &json!({"diameter": 4})).unwrap();+ }+ s.adopt_save(©);+ assert_eq!((s.path.as_deref(), s.doc.name.as_str()), (path.to_str(), "Bracket"));+ assert!(s.is_dirty(), "hide {hide}: the change is not in the file");+ s.execute("edit.undo", &json!({})).unwrap();+ assert!(!s.is_dirty(), "hide {hide}: back to what the file holds");+ }+ let _ = std::fs::remove_dir_all(&dir);+ }+/// #5467: a script, the CLI or the control channel never gets a prompt. A Save over a file/// another program or computer changed since it was opened is refused with an error naming/// `overwrite`, which replaces it; a design opened read-only only saves to another file.diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 02c794effc..7cb2387136 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -2320,10 +2320,13 @@ impl SolveApp {}let rx = self.control_rx.take();for _ in 0..PER_FRAME {- // A file loading, an agent's design change computing, or the agent cursor acting one- // out: commands that change the design wait their turn (the window, inspection and- // screenshots still answer, #1989).- let waits = self.file_load.busy() || self.file_picker.is_some() || self.control_job.is_some() || self.agent.busy();+ // A file loading, a save being written, an agent's design change computing, or the+ // agent cursor acting one out: commands that change the design wait their turn (the+ // window, inspection and screenshots still answer, #1989). `engine.script` runs+ // directly, past `SolveApp::run`'s guard, so without the save here it changed the+ // design the save was taken up for (#6003).+ let waits =+ self.file_load.busy() || self.file_picker.is_some() || self.saving.is_some() || self.control_job.is_some() || self.agent.busy();// A screenshot waiting for an opened design to be meshed holds what came after it.let shot_waits = open_meshing::active(self) && self.control_held.front().is_some_and(|r| r.method == "ui.screenshot");// Held requests go first, in order, once nothing makes them wait.diff --git a/crates/ui-egui/src/save_job.rs b/crates/ui-egui/src/save_job.rsindex 5b2095a7c1..0c5ce06f75 100644--- a/crates/ui-egui/src/save_job.rs+++ b/crates/ui-egui/src/save_job.rs@@ -107,6 +107,58 @@ mod tests {assert!(h.app.dialog.as_ref().unwrap().error.as_ref().unwrap().contains("file operation"));}+ /// #6003: an agent's `engine.script` that arrives while the window's Save is being written+ /// waits for the save, as the other design-changing requests do, and runs after it: the+ /// file has the design as it was when Save was pressed, and the script's change is left+ /// unsaved. Before, the script ran at once, and taking up the save marked its change saved.+ #[test]+ fn a_script_waits_for_a_save_being_written() {+ let mut h = crate::scenario::Harness::new();+ h.app.home.open = false;+ h.app.run("solid.box", json!({"length": 10, "width": 10, "height": 10})).unwrap();+ let path = h.dir.join("Plate.solidcraft");+ // The save, written on a worker, finishes when the test says.+ let mut copy = h.app.session.scratch();+ copy.execute("file.save_as", &json!({"path": path.to_string_lossy()})).unwrap();+ let (finish, done) = std::sync::mpsc::channel();+ h.app.saving = Some(super::Saving { command: "file.save_as", done });+ let (tx, rx) = std::sync::mpsc::channel();+ h.app.control_rx = Some(rx);+ let (req, reply) = crate::control::ControlRequest::new("engine.script", json!([{"command": "solid.sphere", "params": {"diameter": 4}}]));+ tx.send(req).unwrap();+ h.app.drain_control(&h.ctx);+ assert!(reply.try_recv().is_err(), "held while the save is written");+ assert_eq!(h.app.session.doc.features.len(), 1);+ finish.send((copy, Ok(json!({})))).unwrap();+ super::tick(&mut h.app, false);+ assert!(h.app.saving.is_none() && !h.app.session.is_dirty());+ h.app.drain_control(&h.ctx);+ assert!(reply.try_recv().is_ok(), "run once the save is taken up");+ assert_eq!(h.app.session.doc.features.len(), 2);+ assert!(h.app.session.is_dirty(), "the script's change is not in the file");+ }++ /// #6003: a save is taken up as saved only for the design it wrote. A design changed while+ /// the save was being written keeps its file and name from the save, but stays unsaved.+ #[test]+ fn a_design_changed_during_its_save_stays_unsaved() {+ let mut h = crate::scenario::Harness::new();+ h.app.home.open = false;+ h.app.run("solid.box", json!({"length": 10, "width": 10, "height": 10})).unwrap();+ let path = h.dir.join("Plate.solidcraft");+ let mut copy = h.app.session.scratch();+ copy.execute("file.save_as", &json!({"path": path.to_string_lossy()})).unwrap();+ let (finish, done) = std::sync::mpsc::channel();+ h.app.saving = Some(super::Saving { command: "file.save_as", done });+ // A change that reaches the session past the window's guards.+ h.app.session.execute("solid.sphere", &json!({"diameter": 4})).unwrap();+ finish.send((copy, Ok(json!({})))).unwrap();+ super::tick(&mut h.app, false);+ assert_eq!(h.app.session.path.as_deref(), path.to_str());+ assert_eq!(h.app.session.doc.name, "Plate");+ assert!(h.app.session.is_dirty(), "the sphere is not in the file");+ }+/// #3044: Save writes on a worker while the window keeps drawing; the design waits (commands/// are refused) until the save is taken up, which leaves it saved under its file, in the/// recent files, with the command echoed, as a save run here does.