diff --git a/crates/engine/src/cmd/delete.rs b/crates/engine/src/cmd/delete.rs index d330dd79b..36e3a0a26 100644 --- a/crates/engine/src/cmd/delete.rs +++ b/crates/engine/src/cmd/delete.rs @@ -36,6 +36,9 @@ fn keys(p: &Value, k: &str) -> Vec { fn delete(s: &mut Session, p: &Value) -> Result { if bool_(p, "dry_run").unwrap_or(false) { let mut t = s.scratch(); + // What the check computes is kept where this session keeps it (a check run ahead, off a + // front end's window thread), so the delete itself finds it there (#9917). + t.model.keep(s.model.kept()); return apply(&mut t, p); } let r = apply(s, p)?; @@ -366,6 +369,39 @@ mod tests { assert_eq!(*s.doc, *doc, "the checks change nothing"); } + /// #9917: a delete checked ahead (off a front end's window thread) keeps what its dry run + /// builds of the history after the marker, and the delete run with that finds all of it: + /// nothing is built again on the window thread, where it took half a minute. + #[test] + fn a_delete_checked_ahead_keeps_the_history_it_builds_for_the_delete() { + let mut s = Session::default(); + let sk = run(&mut s, "sketch.create", json!({"plane": "XY"}))["sketch"].as_u64().unwrap(); + run(&mut s, "sketch.line", json!({"points": [[0, 0], [40, 0], [30, 20], [10, 20]], "closed": true})); + let hole = run(&mut s, "sketch.circle.center", json!({"center": [30, 15], "radius": 2}))["curves"][0].as_str().unwrap().to_string(); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "solid.extrude", json!({"distance": 5, "name": "Plate"})); + let later = 6; + for i in 0..later { + run(&mut s, "solid.cylinder", json!({"base": [12 + 3 * i, 8, -1], "radius": 1, "height": 10, "operation": "cut"})); + } + run(&mut s, "timeline.roll_to", json!({"position": 1})); + run(&mut s, "sketch.edit", json!({"sketch": sk})); + // A cache too small to keep a state, as on a large design: only what is kept carries over. + *s.model.cache.lock().unwrap() = solidcraft_doc::EvalCache::with_budget(1); + let items = json!([{"type": "sketch_curve", "id": hole}]); + let ahead = s.ahead(); + let kept = ahead.kept(); + let replies = ahead.replies(&[("selection.delete".to_string(), json!({"items": items, "dry_run": true}))]); + assert!(replies.iter().all(Result::is_ok), "{replies:?}"); + let built = kept.len(); + assert!(built > later, "the check kept the history after the marker: {built} results"); + s.model.keep(Some(kept.clone())); + run(&mut s, "selection.delete", json!({"items": items})); + s.model.keep(None); + assert_eq!(kept.len(), built, "the delete found everything it builds among what the check kept"); + assert!(s.model.state().sketch(sk).unwrap().sketch.curves.iter().all(|c| c.id != hole)); + } + #[test] fn mixed_selection_is_one_undo_step() { let (mut s, _) = part(); diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index c998019df..13cd94b1a 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -1349,9 +1349,12 @@ pub(super) fn newly_broken_in_history( let whole = |doc: &solidcraft_doc::Document, model: &solidcraft_doc::Model| { let mut doc = doc.clone(); doc.marker = None; - let mut model = model.clone(); - model.evaluate(&doc); - model + // A copy keeps nothing on its own: it keeps what the model keeps, so a delete checked + // ahead finds this build there when it runs (#9917). + let mut copy = model.clone(); + copy.keep(model.kept()); + copy.evaluate(&doc); + copy }; // Only a feature that fails after the change can be newly broken: when none does, the // history before it needn't be built again (#9917: each build ran every boolean after the diff --git a/crates/ui-egui/src/deferred.rs b/crates/ui-egui/src/deferred.rs index d9e945a0b..ab75509c7 100644 --- a/crates/ui-egui/src/deferred.rs +++ b/crates/ui-egui/src/deferred.rs @@ -66,6 +66,9 @@ pub struct DeleteCheck { /// When it started (ms). pub(crate) started: f64, then: AfterCheck, + /// What the check computes, for the delete itself to find instead of building it again on + /// the window thread (#9917). + kept: std::sync::Arc, /// The design's revision it was computed for: a design changed meanwhile checks again. revision: u64, } @@ -97,8 +100,8 @@ impl AfterCheck { } } - /// Carry on with the check's replies. - fn finish(self, app: &mut SolveApp, replies: Vec>) { + /// Carry on with the check's replies, and what it `kept` (when it ran ahead) for the delete. + fn finish(self, app: &mut SolveApp, replies: Vec>, kept: Option>) { match self { AfterCheck::Timeline { features, groups } => { let reports: Vec = replies.into_iter().map(Result::unwrap_or_default).collect(); @@ -106,7 +109,7 @@ impl AfterCheck { } AfterCheck::Selection(params) => { let report = replies.into_iter().next().unwrap_or_else(|| Err("the delete was not checked".into())); - crate::delete::checked(app, params, report); + crate::delete::checked(app, params, report, kept); } } } @@ -186,6 +189,7 @@ impl SolveApp { && self.file_picker.is_none() { let ahead = self.session.ahead(); + let kept = ahead.kept(); let (tx, rx) = std::sync::mpsc::channel(); let checks = commands.clone(); let spawned = std::thread::Builder::new().name("delete check".into()).spawn(move || { @@ -193,17 +197,17 @@ impl SolveApp { let _ = tx.send(replies); }); if spawned.is_ok() { - let check = DeleteCheck { done: rx, started: crate::now_ms(), then, revision: self.session.revision }; + let check = DeleteCheck { done: rx, started: crate::now_ms(), then, revision: self.session.revision, kept }; // As with the history's commands, zero always leaves it for a later frame. match (wait.is_zero(), check.done.recv_timeout(wait)) { - (false, Ok(replies)) => check.then.finish(self, replies), + (false, Ok(replies)) => check.then.finish(self, replies, Some(check.kept.clone())), _ => self.delete_check = Some(check), } return; } } let replies = commands.iter().map(|(id, params)| self.session.execute(id, params).map_err(|e| e.to_string())).collect(); - then.finish(self, replies); + then.finish(self, replies, None); } /// Each frame: a delete whose check is in carries on (checked again when the design changed @@ -222,7 +226,7 @@ impl SolveApp { match replies { None => self.set_status("the delete could not be checked", true), Some(_) if check.revision != self.session.revision => check.then.restart(self), - Some(replies) => check.then.finish(self, replies), + Some(replies) => check.then.finish(self, replies, Some(check.kept)), } } diff --git a/crates/ui-egui/src/delete.rs b/crates/ui-egui/src/delete.rs index 6b626fc9b..cee70ad27 100644 --- a/crates/ui-egui/src/delete.rs +++ b/crates/ui-egui/src/delete.rs @@ -2,7 +2,10 @@ //! the viewport or the browser goes in one undoable step through `selection.delete`. When it //! takes other features with it or makes some fail, a confirmation lists them first. +use std::sync::Arc; + use serde_json::{Value, json}; +use solidcraft_engine::doc::KeptResults; use crate::SolveApp; use crate::ui_kit; @@ -15,6 +18,8 @@ const MAX_LISTED: usize = 20; pub struct Pending { pub params: Value, pub report: Value, + /// What the dry run computed ahead, for the delete to find (#9917). + pub kept: Option>, } /// What Delete acts on now: the selection (bodies not locked), plus the components picked in @@ -127,7 +132,7 @@ pub fn request(app: &mut SolveApp, params: Value) { } /// [`request`] once its dry run's report is in. -pub(crate) fn checked(app: &mut SolveApp, params: Value, report: Result) { +pub(crate) fn checked(app: &mut SolveApp, params: Value, report: Result, kept: Option>) { let report = match report { Ok(r) => r, Err(e) => { @@ -141,14 +146,16 @@ pub(crate) fn checked(app: &mut SolveApp, params: Value, report: Result>) { + match app.run_with_kept(kept, |app| app.run("selection.delete", params.clone())) { Ok(r) => { app.tree.picked_components.clear(); app.tree.picked_canvases.clear(); @@ -165,7 +172,7 @@ fn run(app: &mut SolveApp, params: &Value) { pub fn confirm(app: &mut SolveApp, accept: bool) -> bool { let Some(p) = app.menu.confirm.take() else { return false }; if accept { - run(app, &p.params); + run(app, &p.params, p.kept); } true } diff --git a/crates/ui-egui/src/history_off_thread_tests.rs b/crates/ui-egui/src/history_off_thread_tests.rs index 14391fdb9..36cd0263a 100644 --- a/crates/ui-egui/src/history_off_thread_tests.rs +++ b/crates/ui-egui/src/history_off_thread_tests.rs @@ -922,6 +922,40 @@ fn delete_on_the_selection_checks_off_the_window_thread() { assert!((volume(&mut h) - block_with_hole()).abs() < 1.0); } +/// #9917: with the history rolled back, the check builds the features after the marker off the +/// window thread and keeps them; the confirmation carries them to the delete, which runs with +/// them instead of building that history again on the window thread. +#[test] +fn a_confirmed_delete_runs_with_what_its_check_kept() { + let mut h = Harness::new(); + h.app.home.open = false; + let sk = h.app.run("sketch.create", json!({"plane": "XY"})).unwrap()["sketch"].as_u64().unwrap(); + let side = h.app.run("sketch.line", json!({"points": [[0, 0], [40, 0], [30, 20], [10, 20]], "closed": true})).unwrap()["curves"][0] + .as_str() + .unwrap() + .to_string(); + h.app.run("sketch.finish", json!({})).unwrap(); + h.app.run("solid.extrude", json!({"distance": 5, "name": "Plate"})).unwrap(); + for i in 0..3 { + h.app.run("solid.cylinder", json!({"base": [12 + 6 * i, 8, -1], "radius": 1, "height": 10, "operation": "cut"})).unwrap(); + } + h.app.run("timeline.roll_to", json!({"position": 1})).unwrap(); + h.app.run("sketch.edit", json!({"sketch": sk})).unwrap(); + h.app.apply_off_thread = Some(Duration::ZERO); + crate::delete::request(&mut h.app, json!({"items": [{"type": "sketch_curve", "id": side}]})); + assert!(h.app.computing_since().is_some(), "checking"); + until_settled(&mut h); + let pending = h.app.menu.confirm.clone().expect("what the delete breaks after the marker is confirmed first"); + assert!(pending.report["would_fail"].as_array().is_some_and(|a| !a.is_empty()), "{}", pending.report); + let kept = pending.kept.expect("the check's results go with the confirmation"); + let built = kept.len(); + assert!(built > 0, "the check kept the features after the marker it built"); + assert!(crate::delete::confirm(&mut h.app, true)); + let curves = |h: &Harness| h.app.session.model.state().sketch(sk).map(|s| s.sketch.curves.len()).unwrap_or(0); + assert_eq!(curves(&h), 3, "deleted"); + assert_eq!(kept.len(), built, "the delete built nothing the check had not kept"); +} + /// A design changed while its delete is checked is checked again before anything is deleted, and /// a second Delete meanwhile is left out. #[test]