From d71ec4719c123e20a2bcbc5d14f56b44fad937e4 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 5 Nov 2026 21:44:54 +0100 Subject: [PATCH 1/5] feat(models): run a model's tools off the frame on the desktop (#1252) ModelBuilder's Run and Run in the Workflows pane's Models list now work out the model's tools on a thread of their own, with the status bar's progress and Cancel. The thread runs the model on a copy of the session and records what each tool read and made; `model.run` then runs here as it always has and takes each recorded result whose inputs are still the same, so what it adds, says, logs and puts in Undo is unchanged. From the first tool whose inputs changed, or a tool that writes files or is a plugin's or GeoLibre's, the tools run here. readjob gains a callback for the caller once the command lands, or never runs, so ModelBuilder marks its nodes when the run lands. --- crates/analysis/src/control.rs | 2 +- crates/engine/src/cmd/gp.rs | 168 ++++++++++++++++++++++++++--- crates/engine/src/cmd/reads.rs | 5 +- crates/engine/src/cmd/workflows.rs | 39 ++++++- crates/engine/src/lib.rs | 11 ++ crates/engine/src/tests.rs | 119 ++++++++++++++++++++ crates/ui-egui/src/analysispane.rs | 17 ++- crates/ui-egui/src/lib.rs | 8 ++ crates/ui-egui/src/modelbuilder.rs | 69 +++++++++++- crates/ui-egui/src/readjob.rs | 144 +++++++++++++++++++------ docs/user-guide/analysis.md | 10 ++ 11 files changed, 538 insertions(+), 54 deletions(-) diff --git a/crates/analysis/src/control.rs b/crates/analysis/src/control.rs index 5ab3ced986..cb7da5649b 100644 --- a/crates/analysis/src/control.rs +++ b/crates/analysis/src/control.rs @@ -91,7 +91,7 @@ thread_local! { } /// The control of the run on this thread, if any. -pub(crate) fn current() -> Option> { +pub fn current() -> Option> { CURRENT.with(|c| c.borrow().clone()) } diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 67e7059654..cde586eaca 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -510,6 +510,117 @@ pub fn tool_complete(s: &mut Session, id: u64, done: ToolDone) -> Result<(), Str complete(s, id, done.result, Some(done.full_ids)) } +#[cfg(not(target_arch = "wasm32"))] +/// A tool's run made ahead on a copy of the session, for a model the app runs on a thread of its +/// own ([`super::workflows::model_ahead`]): what it read and what it made, for the model's run +/// here to take instead of running the tool again. +pub(crate) struct RanAhead { + tool: String, + params: Value, + read: Snapshot<'static>, + /// The view's extent, when the tool asked for it. + view: Option>, + result: Result, + full_ids: FullIds, + millis: u64, +} + +#[cfg(not(target_arch = "wasm32"))] +/// The tools of a model run ahead: recorded on the copy, then taken in order by the run here. +pub(crate) enum Ahead { + Recording(Vec), + Taking(std::collections::VecDeque), +} + +#[cfg(not(target_arch = "wasm32"))] +/// What a model's run on a copy of the session stops at, as its run here does them: a tool that +/// writes files (which would be written twice), and plugin and GeoLibre tools, which run as they +/// always have. +const NOT_AHEAD: &str = "runs where the model runs"; + +#[cfg(not(target_arch = "wasm32"))] +/// Runs `run` taking the tools `ran` ahead: each one whose inputs are still the same as the run +/// ahead read them is taken; from the first that isn't, the tools run here. Restored on every +/// path, a panic included (raised again), so no later run takes them. +pub(crate) fn taking(s: &mut Session, ran: Vec, run: impl FnOnce(&mut Session) -> T) -> T { + s.tools_ahead = Some(Ahead::Taking(ran.into())); + let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(s))); + s.tools_ahead = None; + r.unwrap_or_else(|p| std::panic::resume_unwind(p)) +} + +#[cfg(not(target_arch = "wasm32"))] +/// The tools a copy of the session recorded ([`Ahead::Recording`]), taken. +pub(crate) fn ran_ahead(s: &mut Session) -> Vec { + match s.tools_ahead.take() { + Some(Ahead::Recording(ran)) => ran, + _ => Vec::new(), + } +} + +#[cfg(not(target_arch = "wasm32"))] +/// The next tool run ahead, if it is this one on the same inputs: its result, the streamed rows +/// it read whole and how long it took. Any other drops the rest, which came after it. +fn take_ahead(s: &mut Session, tool: &str, params: &Value) -> Option<(Result, FullIds, u64)> { + let Some(Ahead::Taking(ran)) = &mut s.tools_ahead else { return None }; + let next = ran.pop_front()?; + let now = Snapshot::of(s); + let view = next.view.is_none_or(|v| v == view_extent(&now)); + if next.tool == tool && next.params == *params && view && same_read(&next.read, &now) { + #[cfg(test)] + { + s.tools_taken += 1; + } + return Some((next.result, next.full_ids, next.millis)); + } + s.tools_ahead = None; + None +} + +#[cfg(not(target_arch = "wasm32"))] +/// Whether a tool reading `now` reads what it read in `was`: the same layers with the same rows +/// (for a streamed layer read whole, the same source), selections, rasters and projection, the +/// same tables and environment. +fn same_read(was: &Snapshot, now: &Snapshot) -> bool { + let layer = |a: &rustgis_map::Layer, b: &rustgis_map::Layer| { + let rows = match (&a.source, &b.source) { + // Read whole from its source, unless it has a selection (rows of the window). + (Some(x), Some(y)) if a.selection.is_empty() => x.same_opening(y) && a.dataset.fields == b.dataset.fields, + (None, None) => Read::unchanged(&a.dataset, &b.dataset), + _ => false, + } && a.selection == b.selection; + let raster = match (&a.raster, &b.raster) { + (Some(x), Some(y)) => { + x.path == y.path + && match (&x.data, &y.data) { + (Some(p), Some(q)) => std::sync::Arc::ptr_eq(p, q), + (None, None) => true, + _ => false, + } + } + (None, None) => true, + _ => false, + }; + a.id == b.id && a.name == b.name && a.definition_query == b.definition_query && rows && raster + }; + let (m, n) = (&was.map, &now.map); + m.layers.len() == n.layers.len() + && m.layers.iter().zip(&n.layers).all(|(a, b)| layer(a, b)) + && m.projection == n.projection + && was.tables.len() == now.tables.len() + && was.tables.iter().zip(now.tables.iter()).all(|(a, b)| a.id == b.id && Read::unchanged(&a.dataset, &b.dataset)) + && was.env == now.env +} + +/// The view's longitude/latitude extent, as a tool asks for it ([`Inputs::view_extent`]): the +/// globe shows the whole Earth (the camera's flat extent means nothing there). +fn view_extent(snap: &Snapshot) -> Option { + if snap.map.projection.is_globe() { + return Some(rustgis_geom::Extent::new(-180.0, -90.0, 180.0, 90.0)); + } + Some(super::workspace::view_extent_of(&snap.map, snap.view_size)).filter(|e| !e.is_empty()) +} + /// Fits a background run's result to edits made while it ran: its layer is the one it read, /// whatever its name now; a selection of a layer that changed, or of a streamed layer whose /// window moved, selects the same features (by id) where they now are; and an update of a layer @@ -925,13 +1036,7 @@ impl Inputs for MapInputs<'_> { flat.forward(c).map(|m| rustgis_geom::Coord::new(m.x * to_meter, m.y * to_meter)) } fn view_extent(&self) -> Option { - *self.view.get_or_init(|| { - // The globe shows the whole Earth (the camera's flat extent means nothing there). - if self.snap.map.projection.is_globe() { - return Some(rustgis_geom::Extent::new(-180.0, -90.0, 180.0, 90.0)); - } - Some(super::workspace::view_extent_of(&self.snap.map, self.snap.view_size)).filter(|e| !e.is_empty()) - }) + *self.view.get_or_init(|| view_extent(&self.snap)) } fn raster(&self, layer: &str) -> Option> { let rl = self.snap.map.layer_by_name(layer)?.raster.as_ref()?; @@ -1033,15 +1138,48 @@ pub fn run_tool_with( let e = format!("{name} was cancelled before it started"); return apply(s, Ran { name, id: tool, command: None, started: t0 }, recorded(spec, params), Err(e), 0); } - let inputs = map_inputs(s, rustgis_analysis::tool(&tool), ¶ms)?; - let mut res = rustgis_analysis::run_with(&tool, &inputs, ¶ms, control); - if let Ok(result) = &mut res { - result.messages.extend(inputs.messages.iter().cloned()); - } - let ids = full_ids(&inputs.loaded); - drop(inputs); + #[cfg(not(target_arch = "wasm32"))] + let ran = take_ahead(s, &tool, ¶ms); + #[cfg(target_arch = "wasm32")] + let ran = None; + let (mut res, ids, millis) = match ran { + Some(ran) => ran, + None => { + #[cfg(not(target_arch = "wasm32"))] + let recording = matches!(s.tools_ahead, Some(Ahead::Recording(_))); + #[cfg(not(target_arch = "wasm32"))] + if recording + && !rustgis_analysis::tool(&tool) + .is_some_and(|t| !t.id.starts_with(rustgis_plugins::PREFIX) && !is_geolibre_tool(t) && !writes_files(t)) + { + return Err(NOT_AHEAD.into()); + } + // A copy of the session runs under the control of the thread it runs on, which Cancel stops. + #[cfg(not(target_arch = "wasm32"))] + let current = rustgis_analysis::control::current().filter(|_| recording); + #[cfg(not(target_arch = "wasm32"))] + let control = current.as_ref().unwrap_or(control); + #[cfg(not(target_arch = "wasm32"))] + let read = recording.then(|| Snapshot::owned(s)); + let inputs = map_inputs(s, rustgis_analysis::tool(&tool), ¶ms)?; + let mut res = rustgis_analysis::run_with(&tool, &inputs, ¶ms, control); + if let Ok(result) = &mut res { + result.messages.extend(inputs.messages.iter().cloned()); + } + let ids = full_ids(&inputs.loaded); + #[cfg(not(target_arch = "wasm32"))] + let view = inputs.view.get().copied(); + drop(inputs); + let millis = now_ms().saturating_sub(t0); + #[cfg(not(target_arch = "wasm32"))] + if let (Some(read), Some(Ahead::Recording(ran))) = (read, &mut s.tools_ahead) { + let (result, full_ids) = (res.clone(), ids.clone()); + ran.push(RanAhead { tool: tool.clone(), params: params.clone(), read, view, result, full_ids, millis }); + } + (res, ids, millis) + } + }; fit_to_windows(s, &mut res, &ids); - let millis = now_ms().saturating_sub(t0); let spec = rustgis_analysis::tool(&tool); let name = spec.map(|t| t.name).unwrap_or("Tool").to_string(); apply(s, Ran { name, id: tool, command: None, started: t0 }, recorded(spec, params), res, millis) diff --git a/crates/engine/src/cmd/reads.rs b/crates/engine/src/cmd/reads.rs index 8a603db008..3e2099bb46 100644 --- a/crates/engine/src/cmd/reads.rs +++ b/crates/engine/src/cmd/reads.rs @@ -1,6 +1,7 @@ //! Reads the desktop app makes on a thread of its own before a command adds what they read //! (#1291 step 3): Add Data's whole-file reads (a raster, a raster GDAL converts, a GeoJSON, -//! delimited text or spreadsheet read whole) and Load All Features. [`read_ahead`] takes what +//! delimited text or spreadsheet read whole), Load All Features, and a model's tools, run on a +//! copy of the session. [`read_ahead`] takes what //! the read needs of the session, [`ReadAhead::run`] reads on the thread, and the app then runs //! the command as it would have with the read [`Read::placed`] in the session: the command takes //! it instead of reading again, so what it adds, says, logs and puts in Undo is the same as when @@ -62,6 +63,8 @@ pub fn read_ahead(s: &Session, command: &str, p: &Value) -> Option { match command { "layer.addData" => super::layer::add_data_ahead(p), "layer.loadAll" => super::layer::load_all_ahead(s, p), + #[cfg(not(target_arch = "wasm32"))] + "model.run" => Some(super::workflows::model_ahead(s, p)), _ => None, } } diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index 0dabd83a86..6e50ce2367 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -497,8 +497,45 @@ fn model_graph(s: &Session, p: &Value) -> Result<(String, crate::project::ModelG Ok((model.name.clone(), g, s.trusted_models.contains(&model.name.to_ascii_lowercase()))) } -/// Runs a model's graph, or the graph passed. +/// Runs a model's graph, or the graph passed: taking the tools the app ran ahead +/// ([`model_ahead`]) whose inputs are still the same. fn run_model(s: &mut Session, p: &Value) -> CmdResult { + #[cfg(not(target_arch = "wasm32"))] + if let Some(ran) = super::reads::take::>(s, MODEL_AHEAD) { + return super::gp::taking(s, ran, |s| run_model_here(s, p)); + } + run_model_here(s, p) +} + +#[cfg(not(target_arch = "wasm32"))] +/// The key of a model's tools run ahead ([`model_ahead`]). +const MODEL_AHEAD: &str = "model.run"; + +#[cfg(not(target_arch = "wasm32"))] +/// A model's run on a copy of the session, made on a thread of its own: it records what each of +/// its tools read and made, for `model.run` to take instead of running them again, so what the +/// run adds, says, logs and puts in Undo is the same as when it runs here. It stops at a tool +/// that writes files, a plugin's or GeoLibre's: from there the run here runs them as it always +/// has. What it says and how it ends are the run here's to say. +pub(crate) fn model_ahead(s: &Session, p: &Value) -> super::reads::ReadAhead { + let (project, trusted) = (s.project.clone(), s.trusted_models.clone()); + let (view_size, active_layer, p) = (s.view_size, s.active_layer, p.clone()); + // What its tools ask of servers and files goes in this session's Diagnostics, as when they + // run here: the run here takes their results and asks nothing. + let diagnostics = s.diagnostics.clone(); + super::reads::ahead("Run Model", MODEL_AHEAD.into(), move || { + let mut copy = Session::new(project); + copy.diagnostics = diagnostics; + copy.trusted_models = trusted; + copy.view_size = view_size; + copy.active_layer = active_layer; + copy.tools_ahead = Some(super::gp::Ahead::Recording(Vec::new())); + let _ = copy.run("model.run", &p); + super::gp::ran_ahead(&mut copy) + }) +} + +fn run_model_here(s: &mut Session, p: &Value) -> CmdResult { // A model saved as steps runs as steps: `%n` may stand anywhere in them, and inputs may be files. if p.get("graph").is_none_or(Value::is_null) { let model = match ps(p, "name") { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index afd26537d8..1de76a8c36 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -753,6 +753,13 @@ pub struct Session { /// A read the app made on a thread of its own for the command it runs next, which takes it /// instead of reading again ([`cmd::reads`]). pub(crate) read_ahead: Option, + /// A model's tools run ahead on a copy of the session, which a model's run here takes + /// ([`cmd::gp::Ahead::Taking`]), or which that copy records ([`cmd::gp::Ahead::Recording`]). + #[cfg(not(target_arch = "wasm32"))] + pub(crate) tools_ahead: Option, + /// The tools run ahead that a model's run took, for the tests. + #[cfg(test)] + pub(crate) tools_taken: usize, /// Set where a person at a terminal waits on a command (`rustgis-cli run` with stdin and /// stderr a terminal), who can stop it with Ctrl-C: tools and rasters take no work cap /// ([`rustgis_analysis::control::Control::stoppable`]). A program that calls it (an agent, a @@ -1015,6 +1022,10 @@ impl Session { zarr_off_frame: false, zarr_seen: 0, read_ahead: None, + #[cfg(not(target_arch = "wasm32"))] + tools_ahead: None, + #[cfg(test)] + tools_taken: 0, stoppable: false, }; for text in label_notices { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 73ea97f26e..bf5b83dff5 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -28631,3 +28631,122 @@ fn reads_made_ahead_are_taken_by_the_command() { assert!(ahead.run(&control).err().unwrap().contains("cancelled")); let _ = std::fs::remove_dir_all(&dir); } + +#[test] +fn a_models_tools_run_ahead_are_taken_by_its_run() { + use rustgis_analysis::control::Control; + let session = || { + let mut s = Session::new(Project::default()); + let mut points = rustgis_data::Dataset::new("Points", rustgis_data::GeometryType::Point, Vec::new()); + for (x, y) in [(8.0, 47.0), (9.0, 46.0), (10.0, 45.0)] { + points.push_next(Some(rustgis_data::Geometry::Point(rustgis_geom::Coord::new(x, y))), Vec::new()); + } + s.map_mut().add_layer(rustgis_map::Layer::new(0, points)); + s + }; + let graph = json!({"nodes": [ + {"id": "points", "kind": "input", "layer": "Points"}, + {"id": "near", "kind": "tool", "tool": "buffer", "params": {"distance": 1000}}, + {"id": "far", "kind": "tool", "tool": "buffer", "params": {"distance": 2000}}, + {"id": "output", "kind": "output", "name": "Rings"} + ], "edges": [ + {"from": "points", "fromPort": "out", "to": "near", "toPort": "input"}, + {"from": "near", "fromPort": "out", "to": "far", "toPort": "input"}, + {"from": "far", "fromPort": "out", "to": "output", "toPort": "in"} + ]}); + let p = json!({"graph": graph.clone()}); + let rings = |s: &Session| { + s.map().layer_by_name("Rings").map(|l| format!("{:?}", l.dataset.features.iter().map(|f| &f.geometry).collect::>())) + }; + let mut here = session(); + here.run("model.run", &p).unwrap(); + + // Both tools are taken: what the run adds, keeps in history and puts in Undo is the same. + let mut s = session(); + let depth = s.undo_depth(); + crate::cmd::reads::read_ahead(&s, "model.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); + let r = s.run("model.run", &p).unwrap(); + assert_eq!(s.tools_taken, 2, "{r}"); + assert!(s.read_ahead.is_none() && s.tools_ahead.is_none()); + assert_eq!(rings(&s), rings(&here)); + assert_eq!(s.map().layers.len(), here.map().layers.len()); + assert_eq!(s.project.gp_history.len(), 2); + assert!(s.model_run.as_ref().unwrap().ok); + assert_eq!(s.undo_depth(), depth + 1); + assert!(s.undo()); + assert_eq!(s.map().layers.len(), 1); + + // A point added after the run ahead: the tools run here, on it too. + let add = |s: &mut Session| { + let p = Some(rustgis_data::Geometry::Point(rustgis_geom::Coord::new(11.0, 44.0))); + s.map_mut().layers[0].dataset.push_next(p, Vec::new()); + }; + let mut s = session(); + crate::cmd::reads::read_ahead(&s, "model.run", &json!({"graph": graph})).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); + add(&mut s); + let mut here = session(); + add(&mut here); + here.run("model.run", &p).unwrap(); + s.run("model.run", &p).unwrap(); + assert_eq!(s.tools_taken, 0); + assert!(s.tools_ahead.is_none()); + assert_eq!(rings(&s), rings(&here)); + + // A run ahead that was cancelled is an error, and runs nothing. + let control = Control::stoppable(); + control.cancel(); + assert!(crate::cmd::reads::read_ahead(&session(), "model.run", &p).unwrap().run(&control).is_err()); + + // A model saved as steps takes them too, and so does one with a tool that fails. + let mut s = session(); + s.run( + "model.new", + &json!({"name": "Steps", "steps": [ + {"tool": "buffer", "params": {"input": "Points", "distance": 1000}}, + {"tool": "buffer", "params": {"input": "%1", "distance": 2000}} + ]}), + ) + .unwrap(); + let p = json!({"name": "Steps"}); + crate::cmd::reads::read_ahead(&s, "model.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); + s.run("model.run", &p).unwrap(); + assert_eq!(s.tools_taken, 2); + s.run("model.new", &json!({"name": "Fails", "steps": [{"tool": "buffer", "params": {"input": "Points", "distance": "x"}}]})).unwrap(); + let p = json!({"name": "Fails"}); + crate::cmd::reads::read_ahead(&s, "model.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); + let e = s.run("model.run", &p).unwrap_err(); + assert!(e.contains("step 1"), "{e}"); + assert_eq!(s.tools_taken, 3, "the failure is taken too"); + + // A tool that writes a file stops the copy: it writes once, as the run here runs it and + // those after it. + let dir = std::env::temp_dir().join(format!("rustgis-model-ahead-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("points.pmtiles"); + let mut s = session(); + s.run( + "model.new", + &json!({"name": "Writes", "steps": [ + {"tool": "buffer", "params": {"input": "Points", "distance": 1000}}, + {"tool": "vectorToPmtiles", "params": {"input": "Points", "minZoom": 0, "maxZoom": 0, "output": file.display().to_string()}}, + {"tool": "buffer", "params": {"input": "Points", "distance": 2000}} + ]}), + ) + .unwrap(); + let p = json!({"name": "Writes"}); + crate::cmd::reads::read_ahead(&s, "model.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); + assert!(!file.exists(), "the copy wrote nothing"); + s.run("model.run", &p).unwrap(); + assert_eq!(s.tools_taken, 1); + assert!(file.exists()); + assert_eq!(s.project.gp_history.len(), 3); + + // A selection made meanwhile is an input changed: the tools run here, on it. + let mut s = session(); + crate::cmd::reads::read_ahead(&s, "model.run", &json!({"graph": graph})).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); + s.map_mut().layers[0].selection = vec![0]; + s.run("model.run", &json!({"graph": graph})).unwrap(); + assert_eq!(s.tools_taken, 0); + assert_eq!(s.map().layer_by_name("Rings").unwrap().dataset.features.len(), 1); + std::fs::remove_dir_all(&dir).ok(); +} diff --git a/crates/ui-egui/src/analysispane.rs b/crates/ui-egui/src/analysispane.rs index 7dffd5a21f..fc2443b38f 100644 --- a/crates/ui-egui/src/analysispane.rs +++ b/crates/ui-egui/src/analysispane.rs @@ -176,6 +176,15 @@ fn run_now(app: &mut RustGisApp, id: &str, p: Value) { show(app, r); } +/// Shows how a model's run from the Models section went, or why it didn't run. +#[cfg(not(target_arch = "wasm32"))] +fn model_ran(app: &mut RustGisApp, landed: crate::Landed) { + match landed { + crate::Landed::Ran(r) => show(app, r), + crate::Landed::NotRun(why) => show(app, Err(why)), + } +} + /// Shows how a workflow's command went, and redraws the map it may have changed. fn show(app: &mut RustGisApp, r: Result) { match r { @@ -1105,8 +1114,14 @@ pub fn workflows(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { if ui.small_button(tl!("Open")).on_hover_text(tl!("Open in ModelBuilder")).clicked() { let _ = app.run("ui.modelBuilder", json!({"name": n})); } + // On the desktop its tools run on a thread of their own, the status bar + // showing it and Cancel; its result shows here once it lands. if ui.small_button(tl!("Run")).clicked() { - run(app, "model.run", json!({"name": n})); + (app.workflow.result, app.workflow.error) = (None, None); + #[cfg(not(target_arch = "wasm32"))] + let _ = crate::readjob::run_then(app, "model.run", json!({"name": n}), Some(model_ran)); + #[cfg(target_arch = "wasm32")] + run_now(app, "model.run", json!({"name": n})); } if ui.small_button("×").clicked() { run(app, "model.remove", json!({"name": n})); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index a34fba37c0..59d44c4340 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -67,6 +67,14 @@ mod pluginsui; mod portalsui; #[cfg(not(target_arch = "wasm32"))] mod readjob; + +/// How a command asked for ended ([`readjob::run_then`] on the desktop; the browser runs it where +/// it's asked): what it answered when it ran, or why it never ran (cancelled, or left), which is +/// said already. +pub enum Landed { + Ran(Result), + NotRun(String), +} #[cfg(not(target_arch = "wasm32"))] mod relayshot; mod repairsource; diff --git a/crates/ui-egui/src/modelbuilder.rs b/crates/ui-egui/src/modelbuilder.rs index 013fa79e6d..c721fb3e27 100644 --- a/crates/ui-egui/src/modelbuilder.rs +++ b/crates/ui-egui/src/modelbuilder.rs @@ -85,6 +85,9 @@ pub struct ModelBuilder { pub search: String, pub log: Vec<(String, LogKind)>, pub status: HashMap, + /// The graph of the run under way, until it lands: another one on the canvas by then (edited, + /// or another model opened) isn't marked with what it did. + running: Option, /// What to do once the user agrees to drop unsaved changes. confirm: Option, /// The problems of the graph, kept until it or the project changes (the project's @@ -386,7 +389,8 @@ fn save(app: &mut RustGisApp) -> bool { } } -/// Runs the model on the canvas, node statuses and messages from the engine's report. +/// Runs the model on the canvas: on the desktop its tools on a thread of their own ([`readjob`]), +/// the status bar showing it and Cancel; [`landed`] puts what it did in the canvas once it lands. fn run_model(app: &mut RustGisApp) { app.mb.status.clear(); app.mb.log.clear(); @@ -400,8 +404,37 @@ fn run_model(app: &mut RustGisApp) { } p = json!({"name": saved}); } - let res = app.run("model.run", p); - let report = app.session.model_run.clone().unwrap_or_default(); + app.mb.running = Some(app.mb.graph.clone()); + #[cfg(not(target_arch = "wasm32"))] + { + let _ = crate::readjob::run_then(app, "model.run", p, Some(landed)); + // Not landed yet: it says so until it does. + if app.mb.log.is_empty() { + app.mb.log(tl!("Running…").to_string(), LogKind::Info); + } + } + #[cfg(target_arch = "wasm32")] + { + let r = app.run("model.run", p); + landed(app, crate::Landed::Ran(r)); + } +} + +/// A model's run in the canvas: node statuses and messages from the engine's report, when it ran +/// on the graph still there; why it didn't run, when it didn't. +fn landed(app: &mut RustGisApp, landed: crate::Landed) { + app.mb.status.clear(); + app.mb.log.clear(); + let same = app.mb.running.take().is_some_and(|g| g == app.mb.graph); + let res = match landed { + crate::Landed::Ran(res) => res, + crate::Landed::NotRun(why) => { + app.mb.log(crate::i18n::fmt("Run failed: {error}", &[("error", &why)]), LogKind::Error); + return; + } + }; + // (Another graph on the canvas: only how the run ended.) + let report = if same { app.session.model_run.clone().unwrap_or_default() } else { Default::default() }; let titles: HashMap = app.mb.graph.nodes.iter().map(|n| (n.id.clone(), model::title(n))).collect(); for n in &report.nodes { let title = titles.get(&n.node).cloned().unwrap_or_default(); @@ -1806,6 +1839,36 @@ mod tests { assert_eq!(app.session.map().layers.len(), layers + 1); assert!(app.mb.graph.nodes.iter().all(|n| app.mb.status.get(&n.id) == Some(&Status::Done))); + // On the desktop its tools run on a thread of their own: the canvas says so until it lands. + app.reads_off_frame = true; + run_model(&mut app); + assert_eq!(app.mb.log, vec![("Running…".to_string(), LogKind::Info)]); + assert!(app.mb.status.is_empty()); + assert_eq!(app.session.map().layers.len(), layers + 1, "nothing added before it lands"); + let end = std::time::Instant::now() + std::time::Duration::from_secs(60); + while crate::readjob::land(&mut app) { + assert!(std::time::Instant::now() < end, "the model never landed"); + std::thread::sleep(std::time::Duration::from_millis(5)); + } + assert_eq!(app.session.map().layers.len(), layers + 2); + assert!(app.mb.graph.nodes.iter().all(|n| app.mb.status.get(&n.id) == Some(&Status::Done))); + assert!( + app.mb.log.last().is_some_and(|(line, kind)| *kind == LogKind::Ok && line.starts_with("Run finished.")), + "{:?}", + app.mb.log + ); + // Cancelled: nothing added, and the canvas says so without the last run's marks. + run_model(&mut app); + app.reads.cancel_for_tests(); + while crate::readjob::land(&mut app) { + assert!(std::time::Instant::now() < end, "the model never landed"); + std::thread::sleep(std::time::Duration::from_millis(5)); + } + assert_eq!(app.session.map().layers.len(), layers + 2); + assert!(app.mb.status.is_empty()); + assert_eq!(app.mb.log, vec![("Run failed: Run Model of Model was cancelled".to_string(), LogKind::Error)]); + app.reads_off_frame = false; + app.mb.name = "Dissolved cities".into(); assert!(app.mb.dirty()); assert!(save(&mut app)); diff --git a/crates/ui-egui/src/readjob.rs b/crates/ui-egui/src/readjob.rs index 25cd6ecf26..fd86c9a60e 100644 --- a/crates/ui-egui/src/readjob.rs +++ b/crates/ui-egui/src/readjob.rs @@ -1,10 +1,10 @@ -//! Add Data's whole-file reads and Load All Features on the desktop read on a thread of their own -//! (#1291 step 3), so a large raster, GeoJSON or table, or a streamed layer loaded whole, doesn't -//! hold the window: the status bar shows the read under way, how far it has come and Cancel, and -//! [`land`] then runs the command, which takes what was read ([`reads`]) and adds it as it does -//! when it reads here. Reads go one at a time, in the order asked for: files dropped together -//! wait for the one before, and so does a file added while one is read, so the layers come in -//! that order. +//! Add Data's whole-file reads, Load All Features and a model's tools on the desktop run on a +//! thread of their own (#1291 step 3), so a large raster, GeoJSON or table, a streamed layer +//! loaded whole, or a long model doesn't hold the window: the status bar shows the run under way, +//! how far it has come and Cancel, and [`land`] then runs the command, which takes what was made +//! ([`reads`]) and adds it as it does when it runs here. They go one at a time, in the order +//! asked for: files dropped together wait for the one before, and so does a file added while one +//! is read, so the layers come in that order. use std::collections::VecDeque; use std::sync::Arc; @@ -35,8 +35,15 @@ struct Asked { opened: u64, map: u64, layer: Option, + /// Given what the command answered when it runs, or why it didn't (cancelled, or left). + then: Option, } +/// What a caller does once the command lands (ModelBuilder shows its run). +pub type Then = fn(&mut RustGisApp, Landed); + +pub use crate::Landed; + /// A read under way: what it is for, its Cancel and progress, and what it read once done. struct ReadJob { asked: Asked, @@ -48,13 +55,20 @@ struct ReadJob { slot: crate::Slot>, } -/// Runs `command` (`layer.addData` or `layer.loadAll`) with `p`: its read on a thread of its own -/// when the desktop app makes it so ([`reads::read_ahead`]), after any read under way; here and -/// now otherwise, as the command does. While one is under way or waiting it answers that, not -/// what the command adds, which comes as it lands. +/// Runs `command` (`layer.addData`, `layer.loadAll` or `model.run`) with `p`: its read on a +/// thread of its own when the desktop app makes it so ([`reads::read_ahead`]), after any under +/// way; here and now otherwise, as the command does. While one is under way or waiting it answers +/// that, not what the command adds, which comes as it lands. pub fn run(app: &mut RustGisApp, command: &'static str, p: Value) -> Result { + run_then(app, command, p, None) +} + +/// [`run`], then `then` with what the command answered once it ran, here or as it lands, or why +/// it never ran. +pub fn run_then(app: &mut RustGisApp, command: &'static str, p: Value, then: Option) -> Result { if !app.reads_off_frame { - return app.run(command, p); + let r = app.run(command, p); + return answered(app, then, r); } let layer = (command == "layer.loadAll") .then(|| app.session.layer_id(&p).ok().and_then(|id| app.session.map().layer(id)).map(|l| (l.instance_id(), l.name.clone()))) @@ -68,6 +82,8 @@ pub fn run(app: &mut RustGisApp, command: &'static str, p: Value) -> Result name.clone(), + // A model by its name (one drawn but not yet named has none: see `called`). + None if command == "model.run" => p.get("name").and_then(Value::as_str).map(str::trim).unwrap_or_default().to_string(), None => p.get("path").and_then(Value::as_str).map(|path| file_name(path).to_string()).unwrap_or_default(), }; let asked = Asked { @@ -77,6 +93,7 @@ pub fn run(app: &mut RustGisApp, command: &'static str, p: Value) -> Result Result start(app, asked, p), - Err(why) => Err(why.to_string()), + Err(why) => { + let why = why.to_string(); + not_run(app, then, why.clone()); + Err(why) + } + } +} + +/// What the command answered, `r`, given to `then` first. +fn answered(app: &mut RustGisApp, then: Option, r: Result) -> Result { + if let Some(then) = then { + then(app, Landed::Ran(r.clone())); + } + r +} + +/// Why the command never ran, given to `then`. +fn not_run(app: &mut RustGisApp, then: Option, why: String) { + if let Some(then) = then { + then(app, Landed::NotRun(why)); } } +/// What a command asked for is called in a notice: an unnamed model by the command's name. +fn called(what: &str) -> &str { + if what.is_empty() { tl_raw!("Run Model") } else { what } +} + /// A path's last part, as a notice names the file. fn file_name(path: &str) -> &str { std::path::Path::new(path).file_name().and_then(|n| n.to_str()).unwrap_or(path) @@ -138,7 +179,10 @@ fn placed(app: &RustGisApp, asked: &Asked) -> Result { fn start(app: &mut RustGisApp, asked: Asked, p: Value) -> Result { // Picked on this thread, so a bug in the picking must not take the app down: it reads here. let picked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| reads::read_ahead(&app.session, asked.command, &p))); - let Ok(Some(ahead)) = picked else { return app.run(asked.command, p) }; + let Ok(Some(ahead)) = picked else { + let r = app.run(asked.command, p); + return answered(app, asked.then, r); + }; let name = ahead.name; let control = Control::stoppable(); let (ctl, slot) = (control.clone(), crate::Slot::default()); @@ -153,7 +197,8 @@ fn start(app: &mut RustGisApp, asked: Asked, p: Value) -> Result }); if let Err(e) = spawned { log::warn!("{name} reads here: its thread didn't start: {e}"); - return app.run(asked.command, p); + let r = app.run(asked.command, p); + return answered(app, asked.then, r); } app.reads.job = Some(ReadJob { asked, gone: None, name, control, slot }); Ok(json!({"started": name})) @@ -165,7 +210,7 @@ pub fn land(app: &mut RustGisApp) -> bool { if !busy(app) { return false; } - let mut left: Vec<(String, Gone)> = Vec::new(); + let mut left: Vec<(String, Gone, Option)> = Vec::new(); // What it was for is gone: it is cancelled, and said with the rest below once it stops. let gone = app.reads.job.as_ref().filter(|j| j.gone.is_none()).and_then(|j| placed(app, &j.asked).err()); if let (Some(why), Some(job)) = (gone, app.reads.job.as_mut()) { @@ -177,31 +222,53 @@ pub fn land(app: &mut RustGisApp) -> bool { let Some(done) = done else { return true }; let Some(job) = app.reads.job.take() else { return false }; match (job.gone, placed(app, &job.asked)) { - (Some(why), _) | (None, Err(why)) => left.push((job.asked.what, why)), + (Some(why), _) | (None, Err(why)) => left.push((job.asked.what, why, job.asked.then)), (None, Ok(p)) => { if job.control.is_cancelled() { // Cancelled (once it had read too): it adds nothing, and nor does what waits. - app.session.notify( - crate::i18n::fmt("{tool} of {what} was cancelled", &[("tool", tl_raw!(job.name)), ("what", &job.asked.what)]), - false, - ); - let n = std::mem::take(&mut app.reads.waiting).len(); - if n == 1 { - app.session.notify(tl!("The one waiting after it was left too").to_string(), false); - } else if n > 1 { - app.session.notify(crate::i18n::fmt("The {n} waiting after it were left too", &[("n", &n.to_string())]), false); + let said = if job.asked.what.is_empty() { + crate::i18n::fmt("{tool} was cancelled", &[("tool", tl_raw!(job.name))]) + } else { + crate::i18n::fmt("{tool} of {what} was cancelled", &[("tool", tl_raw!(job.name)), ("what", &job.asked.what)]) + }; + app.session.notify(said.clone(), false); + not_run(app, job.asked.then, said); + let waiting = std::mem::take(&mut app.reads.waiting); + let n = waiting.len(); + let left_too = match n { + 1 => tl!("The one waiting after it was left too").to_string(), + _ => crate::i18n::fmt("The {n} waiting after it were left too", &[("n", &n.to_string())]), + }; + if n > 0 { + app.session.notify(left_too.clone(), false); + } + for asked in waiting { + not_run(app, asked.then, left_too.clone()); } } else { match done { Ok(read) => { read.placed(&mut app.session); // A failure is said by the command. - let _ = app.run(job.asked.command, p); + let r = app.run(job.asked.command, p); + let _ = answered(app, job.asked.then, r); app.session.drop_read(); app.map.invalidate(); } + // A bug in a model's run on a copy of the session: it runs here, as it would + // have. + Err(e) if job.asked.command == "model.run" => { + log::warn!("{}: {e}; it runs here", job.name); + let r = app.run(job.asked.command, p); + let _ = answered(app, job.asked.then, r); + app.map.invalidate(); + } // A bug in a reader: said, and the next goes on. - Err(e) => app.session.notify(crate::i18n::fmt("{what}: {e}", &[("what", &job.asked.what), ("e", &e)]), true), + Err(e) => { + let said = crate::i18n::fmt("{what}: {e}", &[("what", &job.asked.what), ("e", &e)]); + app.session.notify(said.clone(), true); + not_run(app, job.asked.then, said); + } } } } @@ -216,20 +283,23 @@ pub fn land(app: &mut RustGisApp) -> bool { let _ = start(app, asked, p); app.map.invalidate(); } - Err(why) => left.push((asked.what, why)), + Err(why) => left.push((asked.what, why, asked.then)), } } // Those left, one notice for each reason, with a count, naming the first. + for (what, why, then) in &left { + not_run(app, *then, crate::i18n::fmt("{what} was left: {why}", &[("what", called(what)), ("why", &why.to_string())])); + } for why in [Gone::Project, Gone::Map, Gone::Layer] { - let of: Vec<&String> = left.iter().filter(|(_, w)| *w == why).map(|(what, _)| what).collect(); + let of: Vec<&String> = left.iter().filter(|(_, w, _)| *w == why).map(|(what, ..)| what).collect(); let reason = why.to_string(); match of.as_slice() { [] => {} - [what] => app.session.notify(crate::i18n::fmt("{what} was left: {why}", &[("what", what), ("why", &reason)]), false), + [what] => app.session.notify(crate::i18n::fmt("{what} was left: {why}", &[("what", called(what)), ("why", &reason)]), false), [first, ..] => app.session.notify( crate::i18n::fmt( "{n} files or layers were left, the first {what}: {why}", - &[("n", &of.len().to_string()), ("what", first), ("why", &reason)], + &[("n", &of.len().to_string()), ("what", called(first)), ("why", &reason)], ), false, ), @@ -249,6 +319,16 @@ pub fn stop(app: &mut RustGisApp, wait: std::time::Duration) { } } +#[cfg(test)] +impl Reads { + /// Presses Cancel on the run under way. + pub fn cancel_for_tests(&self) { + if let Some(job) = &self.job { + job.control.cancel(); + } + } +} + /// Whether the status bar shows a read. pub fn showing(app: &RustGisApp) -> bool { app.reads.job.is_some() diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 7d727b597b..27efe5f9ee 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1172,6 +1172,16 @@ their name. are none. The tools then run in flow order, and each node shows how it fared. A result that only feeds other tools is removed once the model has run; the outputs, and tools at the end of the flow, stay on the map. A run that fails is undone as a whole. +- On the desktop, **Run** (here or in the Workflows pane's Models section) works out the tools + in the background, so the map and the rest of the window stay in use: the status bar shows + **Run Model**, how far its current tool has come, and **Cancel**, and the canvas says + **Running…** until the results are added, as one step to undo. What a tool reads that changes + meanwhile (an edit, a selection, a layer added or reordered, the environment, the projection) + has that tool and those after it run again as the results are added, and so does a tool that + writes a file, a plugin tool or a GeoLibre tool, with those after it. Those run as a model + always has, holding the window until they finish, with the work limits of a run that can't + be cancelled. A model run from a script, the command line or the assistant runs where the + command runs. - **Save** keeps the model in the project; **Export** writes it as a `*.model.json` file, which **Import** (or dropping the file on the window) opens again. A model from a file may not read or write files. **Copy Command** copies a `model.run` command that runs the model from the From 70e6779589abf1397878adc852e81bda18345add Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 5 Nov 2026 23:01:47 +0100 Subject: [PATCH 2/5] fix(models): one canvas mark per run, the copy's notices kept out of Diagnostics, review notes - ModelBuilder keeps the graphs of the runs asked in order, so Run pressed twice marks the canvas as each lands, and uses the engine's report only when the run made it (not one refused before it ran). - The copy of the session sends only its requests to the session's Diagnostics; its notices (and its stop at a file-writing tool) stay its own, as the run here says them. - A failed copy says so before the model runs here. - A model landing while another Workflows section is open leaves that section's result alone. - The engine test is gated to the desktop; the docs say files read by path aren't compared. --- crates/engine/src/cmd/workflows.rs | 4 ++-- crates/engine/src/lib.rs | 10 ++++++++++ crates/engine/src/tests.rs | 1 + crates/ui-egui/src/analysispane.rs | 8 +++++++- crates/ui-egui/src/modelbuilder.rs | 30 ++++++++++++++++++++++-------- crates/ui-egui/src/readjob.rs | 8 +++++++- docs/user-guide/analysis.md | 5 +++-- 7 files changed, 52 insertions(+), 14 deletions(-) diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index 6e50ce2367..695c6ca62f 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -522,10 +522,10 @@ pub(crate) fn model_ahead(s: &Session, p: &Value) -> super::reads::ReadAhead { let (view_size, active_layer, p) = (s.view_size, s.active_layer, p.clone()); // What its tools ask of servers and files goes in this session's Diagnostics, as when they // run here: the run here takes their results and asks nothing. - let diagnostics = s.diagnostics.clone(); + let requests = s.diagnostics.capture(); super::reads::ahead("Run Model", MODEL_AHEAD.into(), move || { let mut copy = Session::new(project); - copy.diagnostics = diagnostics; + copy.requests_into = Some(requests); copy.trusted_models = trusted; copy.view_size = view_size; copy.active_layer = active_layer; diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 1de76a8c36..2179a15b61 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -757,6 +757,11 @@ pub struct Session { /// ([`cmd::gp::Ahead::Taking`]), or which that copy records ([`cmd::gp::Ahead::Recording`]). #[cfg(not(target_arch = "wasm32"))] pub(crate) tools_ahead: Option, + /// Where the requests a copy of the session makes for a model's run ahead go + /// ([`cmd::workflows::model_ahead`]): the session's own Diagnostics, as when they're made + /// there. Its notices stay its own: the run there says them. + #[cfg(not(target_arch = "wasm32"))] + pub(crate) requests_into: Option, /// The tools run ahead that a model's run took, for the tests. #[cfg(test)] pub(crate) tools_taken: usize, @@ -1024,6 +1029,8 @@ impl Session { read_ahead: None, #[cfg(not(target_arch = "wasm32"))] tools_ahead: None, + #[cfg(not(target_arch = "wasm32"))] + requests_into: None, #[cfg(test)] tools_taken: 0, stoppable: false, @@ -1205,6 +1212,9 @@ impl Session { /// panic restores it. A command run by itself (not as a part of another) that changes the /// project, or reaches a file or a service, goes into the session's log (#797). pub fn run(&mut self, id: &str, params: &Value) -> CmdResult { + #[cfg(not(target_arch = "wasm32"))] + let _diagnostic_scope = self.requests_into.as_ref().map_or_else(|| self.diagnostics.enter(), |c| c.enter()); + #[cfg(target_arch = "wasm32")] let _diagnostic_scope = self.diagnostics.enter(); let source = self.registry.get(id).map(|spec| spec.id).or_else(|| id.starts_with(rustgis_plugins::PREFIX).then_some("Plugin")); // Restored on every path, including panics outside `guarded`, so a later notice is not diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index bf5b83dff5..e23bfb1ab3 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -28632,6 +28632,7 @@ fn reads_made_ahead_are_taken_by_the_command() { let _ = std::fs::remove_dir_all(&dir); } +#[cfg(not(target_arch = "wasm32"))] #[test] fn a_models_tools_run_ahead_are_taken_by_its_run() { use rustgis_analysis::control::Control; diff --git a/crates/ui-egui/src/analysispane.rs b/crates/ui-egui/src/analysispane.rs index fc2443b38f..160bf5178c 100644 --- a/crates/ui-egui/src/analysispane.rs +++ b/crates/ui-egui/src/analysispane.rs @@ -176,9 +176,15 @@ fn run_now(app: &mut RustGisApp, id: &str, p: Value) { show(app, r); } -/// Shows how a model's run from the Models section went, or why it didn't run. +/// Shows how a model's run from the Models section went, or why it didn't run, while that +/// section is open: in another, its result isn't that section's (a failure is said in the +/// status bar either way). #[cfg(not(target_arch = "wasm32"))] fn model_ran(app: &mut RustGisApp, landed: crate::Landed) { + if app.workflow.section != "models" { + app.map.invalidate(); + return; + } match landed { crate::Landed::Ran(r) => show(app, r), crate::Landed::NotRun(why) => show(app, Err(why)), diff --git a/crates/ui-egui/src/modelbuilder.rs b/crates/ui-egui/src/modelbuilder.rs index c721fb3e27..6b446999d9 100644 --- a/crates/ui-egui/src/modelbuilder.rs +++ b/crates/ui-egui/src/modelbuilder.rs @@ -85,9 +85,10 @@ pub struct ModelBuilder { pub search: String, pub log: Vec<(String, LogKind)>, pub status: HashMap, - /// The graph of the run under way, until it lands: another one on the canvas by then (edited, - /// or another model opened) isn't marked with what it did. - running: Option, + /// The graphs of the runs under way and waiting, in the order asked, until each lands: + /// another one on the canvas by then (edited, or another model opened) isn't marked with + /// what it did. + running: std::collections::VecDeque, /// What to do once the user agrees to drop unsaved changes. confirm: Option, /// The problems of the graph, kept until it or the project changes (the project's @@ -404,7 +405,7 @@ fn run_model(app: &mut RustGisApp) { } p = json!({"name": saved}); } - app.mb.running = Some(app.mb.graph.clone()); + app.mb.running.push_back(app.mb.graph.clone()); #[cfg(not(target_arch = "wasm32"))] { let _ = crate::readjob::run_then(app, "model.run", p, Some(landed)); @@ -425,7 +426,7 @@ fn run_model(app: &mut RustGisApp) { fn landed(app: &mut RustGisApp, landed: crate::Landed) { app.mb.status.clear(); app.mb.log.clear(); - let same = app.mb.running.take().is_some_and(|g| g == app.mb.graph); + let same = app.mb.running.pop_front().is_some_and(|g| g == app.mb.graph); let res = match landed { crate::Landed::Ran(res) => res, crate::Landed::NotRun(why) => { @@ -433,8 +434,10 @@ fn landed(app: &mut RustGisApp, landed: crate::Landed) { return; } }; - // (Another graph on the canvas: only how the run ended.) - let report = if same { app.session.model_run.clone().unwrap_or_default() } else { Default::default() }; + // Its report, when the run made one (one refused before it ran, a model removed meanwhile, + // leaves the last run's); and only on the graph it ran: for another, only how it ended. + let report = app.session.model_run.clone().filter(|r| same && (res.is_ok() || r.error.as_ref() == res.as_ref().err())); + let report = report.unwrap_or_default(); let titles: HashMap = app.mb.graph.nodes.iter().map(|n| (n.id.clone(), model::title(n))).collect(); for n in &report.nodes { let title = titles.get(&n.node).cloned().unwrap_or_default(); @@ -1857,6 +1860,17 @@ mod tests { "{:?}", app.mb.log ); + // Run pressed twice: the second waits for the first, and each marks the canvas as it lands. + run_model(&mut app); + run_model(&mut app); + while crate::readjob::land(&mut app) { + assert!(std::time::Instant::now() < end, "the models never landed"); + std::thread::sleep(std::time::Duration::from_millis(5)); + } + assert_eq!(app.session.map().layers.len(), layers + 4); + assert!(app.mb.graph.nodes.iter().all(|n| app.mb.status.get(&n.id) == Some(&Status::Done))); + assert!(app.mb.log.iter().any(|(line, _)| line.starts_with("Buffer made")), "{:?}", app.mb.log); + assert!(app.mb.running.is_empty()); // Cancelled: nothing added, and the canvas says so without the last run's marks. run_model(&mut app); app.reads.cancel_for_tests(); @@ -1864,7 +1878,7 @@ mod tests { assert!(std::time::Instant::now() < end, "the model never landed"); std::thread::sleep(std::time::Duration::from_millis(5)); } - assert_eq!(app.session.map().layers.len(), layers + 2); + assert_eq!(app.session.map().layers.len(), layers + 4); assert!(app.mb.status.is_empty()); assert_eq!(app.mb.log, vec![("Run failed: Run Model of Model was cancelled".to_string(), LogKind::Error)]); app.reads_off_frame = false; diff --git a/crates/ui-egui/src/readjob.rs b/crates/ui-egui/src/readjob.rs index fd86c9a60e..95e47bed4c 100644 --- a/crates/ui-egui/src/readjob.rs +++ b/crates/ui-egui/src/readjob.rs @@ -258,7 +258,13 @@ pub fn land(app: &mut RustGisApp) -> bool { // A bug in a model's run on a copy of the session: it runs here, as it would // have. Err(e) if job.asked.command == "model.run" => { - log::warn!("{}: {e}; it runs here", job.name); + app.session.notify( + crate::i18n::fmt( + "{tool} couldn't work in the background ({e}): it ran here", + &[("tool", tl_raw!(job.name)), ("e", &e)], + ), + false, + ); let r = app.run(job.asked.command, p); let _ = answered(app, job.asked.then, r); app.map.invalidate(); diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 27efe5f9ee..ef9bc979ec 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1180,8 +1180,9 @@ their name. has that tool and those after it run again as the results are added, and so does a tool that writes a file, a plugin tool or a GeoLibre tool, with those after it. Those run as a model always has, holding the window until they finish, with the work limits of a run that can't - be cancelled. A model run from a script, the command line or the assistant runs where the - command runs. + be cancelled. A file a tool reads by its path isn't compared: one changed during the run + gives the result from what was read. A model run from a script, the command line or the + assistant runs where the command runs. - **Save** keeps the model in the project; **Export** writes it as a `*.model.json` file, which **Import** (or dropping the file on the window) opens again. A model from a file may not read or write files. **Copy Command** copies a `model.run` command that runs the model from the From a2cd5b3fcebc2e51542d22f9dc01b2e9a5e9edbb Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Fri, 6 Nov 2026 06:45:18 +0100 Subject: [PATCH 3/5] fix(models): a run from a closed project lands in its own place; the copy stops at every external tool --- crates/engine/src/cmd/gp.rs | 5 +---- crates/ui-egui/src/modelbuilder.rs | 34 ++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index cde586eaca..8699f638a8 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -1148,10 +1148,7 @@ pub fn run_tool_with( #[cfg(not(target_arch = "wasm32"))] let recording = matches!(s.tools_ahead, Some(Ahead::Recording(_))); #[cfg(not(target_arch = "wasm32"))] - if recording - && !rustgis_analysis::tool(&tool) - .is_some_and(|t| !t.id.starts_with(rustgis_plugins::PREFIX) && !is_geolibre_tool(t) && !writes_files(t)) - { + if recording && !rustgis_analysis::tool(&tool).is_some_and(|t| !rustgis_analysis::is_external(t) && !writes_files(t)) { return Err(NOT_AHEAD.into()); } // A copy of the session runs under the control of the thread it runs on, which Cancel stops. diff --git a/crates/ui-egui/src/modelbuilder.rs b/crates/ui-egui/src/modelbuilder.rs index 6b446999d9..65cdfe8d45 100644 --- a/crates/ui-egui/src/modelbuilder.rs +++ b/crates/ui-egui/src/modelbuilder.rs @@ -85,10 +85,10 @@ pub struct ModelBuilder { pub search: String, pub log: Vec<(String, LogKind)>, pub status: HashMap, - /// The graphs of the runs under way and waiting, in the order asked, until each lands: - /// another one on the canvas by then (edited, or another model opened) isn't marked with - /// what it did. - running: std::collections::VecDeque, + /// The graphs of the runs under way and waiting, in the order asked, with the project each + /// was asked in, until each lands: another one on the canvas by then (edited, or another + /// model opened) isn't marked with what it did, and one from another project leaves it be. + running: std::collections::VecDeque<(u64, ModelGraph)>, /// What to do once the user agrees to drop unsaved changes. confirm: Option, /// The problems of the graph, kept until it or the project changes (the project's @@ -405,7 +405,7 @@ fn run_model(app: &mut RustGisApp) { } p = json!({"name": saved}); } - app.mb.running.push_back(app.mb.graph.clone()); + app.mb.running.push_back((app.session.project_starts, app.mb.graph.clone())); #[cfg(not(target_arch = "wasm32"))] { let _ = crate::readjob::run_then(app, "model.run", p, Some(landed)); @@ -424,9 +424,14 @@ fn run_model(app: &mut RustGisApp) { /// A model's run in the canvas: node statuses and messages from the engine's report, when it ran /// on the graph still there; why it didn't run, when it didn't. fn landed(app: &mut RustGisApp, landed: crate::Landed) { + let Some((project, graph)) = app.mb.running.pop_front() else { return }; + // Asked in a project closed since: this canvas isn't the one it ran from. + if project != app.session.project_starts { + return; + } app.mb.status.clear(); app.mb.log.clear(); - let same = app.mb.running.pop_front().is_some_and(|g| g == app.mb.graph); + let same = graph == app.mb.graph; let res = match landed { crate::Landed::Ran(res) => res, crate::Landed::NotRun(why) => { @@ -1780,7 +1785,13 @@ fn confirm(app: &mut RustGisApp, ctx: &egui::Context) { /// Drops the canvas of a project no longer open (its model would be saved into this one). fn forget_old_project(app: &mut RustGisApp) { if app.mb.project != app.session.project_starts { - app.mb = ModelBuilder { project: app.session.project_starts, search: std::mem::take(&mut app.mb.search), ..Default::default() }; + // The runs still to land stay, so each one's landing is still matched with its own. + app.mb = ModelBuilder { + project: app.session.project_starts, + search: std::mem::take(&mut app.mb.search), + running: std::mem::take(&mut app.mb.running), + ..Default::default() + }; } } @@ -1954,9 +1965,18 @@ mod tests { app.run("ui.modelBuilder", Value::Null).unwrap(); app.mb.add(&Add::Tool("buffer"), pos2(0.0, 0.0)); app.mb.saved_as = Some("Old".into()); + // A run under way when the project changes still lands after the reset. + app.mb.running.push_back((app.session.project_starts, app.mb.graph.clone())); app.run("project.new", Value::Null).unwrap(); follow_view(&mut app); assert!(!app.mb.open && app.mb.graph.nodes.is_empty() && app.mb.saved_as.is_none()); + // Its landing takes its own place, not that of a run asked in this project, and leaves + // this canvas as it is. + app.mb.running.push_back((app.session.project_starts, app.mb.graph.clone())); + app.mb.log("Running…", LogKind::Info); + landed(&mut app, crate::Landed::NotRun("another project was opened".into())); + assert_eq!(app.mb.log, vec![("Running…".to_string(), LogKind::Info)]); + assert_eq!(app.mb.running.len(), 1); } /// Old models (steps with `%n`) open on the canvas as graphs. From 2f5a0bcb40e16844f72bc4ef35b88dcd7c32f262 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Fri, 6 Nov 2026 07:21:56 +0100 Subject: [PATCH 4/5] =?UTF-8?q?fix(models):=20another=20graph's=20run=20la?= =?UTF-8?q?nding=20leaves=20a=20pending=20canvas=20run's=20Running?= =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/ui-egui/src/modelbuilder.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/modelbuilder.rs b/crates/ui-egui/src/modelbuilder.rs index 65cdfe8d45..8302411842 100644 --- a/crates/ui-egui/src/modelbuilder.rs +++ b/crates/ui-egui/src/modelbuilder.rs @@ -429,9 +429,13 @@ fn landed(app: &mut RustGisApp, landed: crate::Landed) { if project != app.session.project_starts { return; } + let same = graph == app.mb.graph; + // Another graph's run, with a run of the canvas still to land: that one's Running… stays. + if !same && !app.mb.running.is_empty() { + return; + } app.mb.status.clear(); app.mb.log.clear(); - let same = graph == app.mb.graph; let res = match landed { crate::Landed::Ran(res) => res, crate::Landed::NotRun(why) => { @@ -1977,6 +1981,13 @@ mod tests { landed(&mut app, crate::Landed::NotRun("another project was opened".into())); assert_eq!(app.mb.log, vec![("Running…".to_string(), LogKind::Info)]); assert_eq!(app.mb.running.len(), 1); + // Another model's run landing first, in this project, leaves it too. + let mut other = app.mb.graph.clone(); + other.nodes.push(Default::default()); + app.mb.running.push_front((app.session.project_starts, other)); + landed(&mut app, crate::Landed::NotRun("cancelled".into())); + assert_eq!(app.mb.log, vec![("Running…".to_string(), LogKind::Info)]); + assert_eq!(app.mb.running.len(), 1); } /// Old models (steps with `%n`) open on the canvas as graphs. From e4c899320e7e10c5f3e7e39d84c5d1d2291527ff Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Fri, 6 Nov 2026 07:50:10 +0100 Subject: [PATCH 5/5] fix(models): a moved node keeps its run's marks, a shown ending stays, the copy stops at any file made --- crates/engine/src/cmd/gp.rs | 5 ++++ crates/ui-egui/src/modelbuilder.rs | 37 ++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 8699f638a8..0d3cacc991 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -1167,6 +1167,11 @@ pub fn run_tool_with( #[cfg(not(target_arch = "wasm32"))] let view = inputs.view.get().copied(); drop(inputs); + // A file made, with a SaveFile parameter or not, is written where the model runs. + #[cfg(not(target_arch = "wasm32"))] + if recording && matches!(&res, Ok(r) if matches!(r.output, Output::File { .. })) { + return Err(NOT_AHEAD.into()); + } let millis = now_ms().saturating_sub(t0); #[cfg(not(target_arch = "wasm32"))] if let (Some(read), Some(Ahead::Recording(ran))) = (read, &mut s.tools_ahead) { diff --git a/crates/ui-egui/src/modelbuilder.rs b/crates/ui-egui/src/modelbuilder.rs index 8302411842..b9979e2a3b 100644 --- a/crates/ui-egui/src/modelbuilder.rs +++ b/crates/ui-egui/src/modelbuilder.rs @@ -89,6 +89,9 @@ pub struct ModelBuilder { /// was asked in, until each lands: another one on the canvas by then (edited, or another /// model opened) isn't marked with what it did, and one from another project leaves it be. running: std::collections::VecDeque<(u64, ModelGraph)>, + /// The log shows Running… for a run still to land: another graph's landing may say how it + /// ended in its place, but once a run's ending is shown it stays. + waiting: bool, /// What to do once the user agrees to drop unsaved changes. confirm: Option, /// The problems of the graph, kept until it or the project changes (the project's @@ -411,6 +414,7 @@ fn run_model(app: &mut RustGisApp) { let _ = crate::readjob::run_then(app, "model.run", p, Some(landed)); // Not landed yet: it says so until it does. if app.mb.log.is_empty() { + app.mb.waiting = true; app.mb.log(tl!("Running…").to_string(), LogKind::Info); } } @@ -421,6 +425,19 @@ fn run_model(app: &mut RustGisApp) { } } +/// Whether `a` and `b` run alike: the same nodes and edges, wherever the nodes sit on the canvas +/// (a node moved while its run is under way still gets its marks). +fn same_run(a: &ModelGraph, b: &ModelGraph) -> bool { + let placed = |g: &ModelGraph| { + let mut g = g.clone(); + for n in &mut g.nodes { + (n.x, n.y) = (0.0, 0.0); + } + g + }; + placed(a) == placed(b) +} + /// A model's run in the canvas: node statuses and messages from the engine's report, when it ran /// on the graph still there; why it didn't run, when it didn't. fn landed(app: &mut RustGisApp, landed: crate::Landed) { @@ -429,11 +446,13 @@ fn landed(app: &mut RustGisApp, landed: crate::Landed) { if project != app.session.project_starts { return; } - let same = graph == app.mb.graph; - // Another graph's run, with a run of the canvas still to land: that one's Running… stays. - if !same && !app.mb.running.is_empty() { + let same = same_run(&graph, &app.mb.graph); + // Another graph's run leaves the canvas as it is while a run of it is still to land (its + // Running… stays), or once a run's ending is shown there. + if !same && (!app.mb.running.is_empty() || !app.mb.waiting) { return; } + app.mb.waiting = false; app.mb.status.clear(); app.mb.log.clear(); let res = match landed { @@ -1863,6 +1882,8 @@ mod tests { assert_eq!(app.mb.log, vec![("Running…".to_string(), LogKind::Info)]); assert!(app.mb.status.is_empty()); assert_eq!(app.session.map().layers.len(), layers + 1, "nothing added before it lands"); + // A node moved meanwhile still gets its mark: the graph runs the same. + app.mb.graph.nodes[0].x += 40.0; let end = std::time::Instant::now() + std::time::Duration::from_secs(60); while crate::readjob::land(&mut app) { assert!(std::time::Instant::now() < end, "the model never landed"); @@ -1984,10 +2005,18 @@ mod tests { // Another model's run landing first, in this project, leaves it too. let mut other = app.mb.graph.clone(); other.nodes.push(Default::default()); - app.mb.running.push_front((app.session.project_starts, other)); + app.mb.running.push_front((app.session.project_starts, other.clone())); landed(&mut app, crate::Landed::NotRun("cancelled".into())); assert_eq!(app.mb.log, vec![("Running…".to_string(), LogKind::Info)]); assert_eq!(app.mb.running.len(), 1); + // The canvas's own run lands; another graph's landing after it leaves its ending shown. + app.mb.waiting = true; + landed(&mut app, crate::Landed::NotRun("cancelled".into())); + let shown = app.mb.log.clone(); + assert_eq!(shown, vec![("Run failed: cancelled".to_string(), LogKind::Error)]); + app.mb.running.push_back((app.session.project_starts, other)); + landed(&mut app, crate::Landed::NotRun("cancelled".into())); + assert_eq!(app.mb.log, shown); } /// Old models (steps with `%n`) open on the canvas as graphs.