From 692dc3cc5f6e83ec404dc9b11476fb46cf563930 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Mon, 2 Nov 2026 14:16:30 +0100 Subject: [PATCH 1/7] feat(gp): the Tools pane runs a built-in tool in the background, with progress and Cancel (#1291) Step 3c and 3d of #1291, on the desktop. - A tool's inputs no longer borrow the session: `MapInputs` owns a `Snapshot` of it (the active map and the standalone tables, which share their features and pixels, the geoprocessing environment and the view). `gp.run` reads through it as before. - `gp::tool_start`, `ToolRun::run` and `gp::tool_complete` (desktop only) start a built-in tool's run, run it on another thread under an analysis `Control`, reading its files and streamed layers there, and leave its result for `gp.finish`, as the plugin tools' runs do: undo, history and messages are unchanged, edits made while it ran are handled as for those, and opening another project drops it. - The Tools pane's Run starts a built-in tool so on the desktop: the window stays in use, and the pane shows the tool running, a progress bar for the tools that count their work, and Cancel. One background run at a time. The browser, scripts, models, the AI Assistant and the CLI run tools as before. - The user guide says so. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/gp.rs | 204 +++++++++++++++++++---- crates/engine/src/tests.rs | 62 +++++++ crates/ui-egui/src/lib.rs | 2 + crates/ui-egui/src/panes.rs | 135 ++++++++++++++- crates/ui-egui/tests/tool_field_lists.rs | 31 ++-- docs/user-guide/analysis.md | 15 +- 6 files changed, 399 insertions(+), 50 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 9bba22869d..839751d26f 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -275,7 +275,8 @@ impl PluginRun { } } -/// A plugin tool's run started and not yet complete. +/// A tool's run on another thread, started and not yet complete: a plugin tool's +/// ([`plugin_start`]) or a built-in one's ([`tool_start`]). pub(crate) struct StartedRun { id: u64, name: String, @@ -284,7 +285,8 @@ pub(crate) struct StartedRun { /// The parameters, as the history records them. params: Value, t0: u64, - /// The feature ids of the streamed layers it reads whole, by lowercase name. + /// The feature ids of the streamed layers it reads whole, by lowercase name (a built-in tool + /// reads them on its thread, and hands them back with its result). full_ids: FullIds, /// The other layers it reads, as it read them, by lowercase name. read: std::collections::HashMap, @@ -341,6 +343,18 @@ pub fn plugin_abandon(s: &mut Session, id: u64) { /// Finishes a run from [`plugin_start`] with what [`PluginRun::run`] returned: `gp.finish /// {"id"}` then applies it like `gp.run` would, with undo and history. pub fn plugin_complete(s: &mut Session, id: u64, result: Result) -> Result<(), String> { + complete(s, id, result, None) +} + +/// Completes a started run: fits its result to the edits made while it ran and to the windows of +/// the streamed layers it read whole (`full_ids` when it read them on its thread), and leaves it +/// for `gp.finish {"id"}`. +fn complete( + s: &mut Session, + id: u64, + result: Result, + full_ids: Option, +) -> Result<(), String> { let i = s .plugin_runs .iter() @@ -349,7 +363,7 @@ pub fn plugin_complete(s: &mut Session, id: u64, result: Result, + full_ids: FullIds, +} + +#[cfg(not(target_arch = "wasm32"))] +impl ToolRun { + /// Reads the tool's inputs and runs it, blocking until it ends; `control` stops it and counts + /// its progress (see [`rustgis_analysis::control`]). + pub fn run(self, control: &std::sync::Arc) -> ToolDone { + let ToolRun { tool, snap, params, .. } = self; + let name = rustgis_analysis::tool(tool).map_or(tool, |t| t.name); + if control.is_cancelled() { + return ToolDone { result: Err(format!("{name} was cancelled before it started")), full_ids: FullIds::new() }; + } + // A bug in reading the inputs, like one in a tool, must not take the app down. + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let inputs = match inputs_of(snap, rustgis_analysis::tool(tool), ¶ms) { + Ok(inputs) => inputs, + Err(e) => return ToolDone { result: Err(e), full_ids: FullIds::new() }, + }; + let mut result = rustgis_analysis::run_with(tool, &inputs, ¶ms, control); + if let Ok(r) = &mut result { + r.messages.extend(inputs.messages.iter().cloned()); + } + ToolDone { result, full_ids: full_ids(&inputs.loaded) } + })) + .unwrap_or_else(|_| ToolDone { result: Err(format!("{name} failed unexpectedly")), full_ids: FullIds::new() }) + } +} + +#[cfg(not(target_arch = "wasm32"))] +/// Starts a run of a built-in tool on the active map, as `gp.run` would run it, to go on another +/// thread: its parameters checked and what it reads of the session taken now (cheaply: the +/// features and pixels are shared), its files and streamed layers read on that thread. The run's +/// result goes to [`tool_complete`], then `gp.finish {"id"}` applies it with undo and history; a +/// layer edited meanwhile is handled as for a plugin tool. Plugin tools start with +/// [`plugin_start`]. +pub fn tool_start(s: &mut Session, tool: &str, params: &Value) -> Result { + let spec = rustgis_analysis::tool(tool_id(tool).unwrap_or(tool)) + .filter(|t| !t.id.starts_with(rustgis_plugins::PREFIX)) + .ok_or_else(|| format!("no tool '{tool}'"))?; + let resolved = rustgis_analysis::resolve_params(spec, params)?; + let read = layer_names(Some(spec), &resolved) + .into_iter() + .filter_map(|n| s.map().layer_by_name(n)) + .filter(|l| !l.is_streamed()) + .map(|l| (l.name.to_ascii_lowercase(), Read::of(l))) + .collect(); + s.next_pending = s.next_pending.wrapping_add(1); + let id = s.next_pending; + let recorded_params = recorded(Some(spec), params.clone()); + let full_ids = FullIds::new(); + s.plugin_runs.push(StartedRun { + id, + name: spec.name.to_string(), + tool: spec.id.to_string(), + params: recorded_params, + t0: now_ms(), + full_ids, + read, + }); + // A run that never completes is dropped eventually. + if s.plugin_runs.len() > MAX_ASYNC_RUNS { + s.plugin_runs.remove(0); + } + Ok(ToolRun { id, tool: spec.id, snap: Snapshot::of(s), params: params.clone() }) +} + +#[cfg(not(target_arch = "wasm32"))] +/// Whether a run from [`tool_start`] still waits for its result: opening or creating a project +/// drops the runs of the previous one. +pub fn tool_waiting(s: &Session, id: u64) -> bool { + plugin_waiting(s, id) +} + +#[cfg(not(target_arch = "wasm32"))] +/// Forgets a run from [`tool_start`] that won't complete (its thread couldn't start). +pub fn tool_abandon(s: &mut Session, id: u64) { + plugin_abandon(s, id); +} + +#[cfg(not(target_arch = "wasm32"))] +/// Finishes a run from [`tool_start`] with what [`ToolRun::run`] made: `gp.finish {"id"}` then +/// applies it like `gp.run` would, with undo and history. +pub fn tool_complete(s: &mut Session, id: u64, done: ToolDone) -> Result<(), String> { + complete(s, id, done.result, Some(done.full_ids)) +} + /// 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 selects the same features (by id) /// where they now are; and an update of a layer that changed, which would discard those edits, @@ -460,10 +577,33 @@ fn finish(s: &mut Session, p: &Value) -> CmdResult { apply(s, Ran { name: r.tool, id: r.tool_id, command: None, started: r.started }, r.params, r.result, r.millis) } +/// What a tool reads of the session, taken when it starts: the active map (a copy, which shares +/// the features and pixels), the standalone tables likewise, the geoprocessing environment and the +/// view. It owns them, so a tool can run on another thread while the session goes on. +pub struct Snapshot { + map: rustgis_map::Map, + tables: Vec, + env: crate::project::GpEnvironment, + /// The view's longitude/latitude extent; the whole Earth on the globe. + view: Option, +} + +impl Snapshot { + pub fn of(s: &Session) -> Snapshot { + let view = if s.map().projection.is_globe() { + // The globe shows the whole Earth (the camera's flat extent means nothing there). + Some(rustgis_geom::Extent::new(-180.0, -90.0, 180.0, 90.0)) + } else { + Some(super::workspace::view_extent(s)).filter(|e| !e.is_empty()) + }; + Snapshot { map: s.map().clone(), tables: s.project.tables.clone(), env: s.project.gp_env.clone(), view } + } +} + /// A tool's inputs on the active map. -struct MapInputs<'a> { - /// The session: its active map and its standalone tables. - session: &'a Session, +struct MapInputs { + /// The session as the tool started: its active map and its standalone tables. + snap: Snapshot, /// The rows of each layer (by lowercase name) the tool runs on: the selection or the /// environment's extent. rows: std::collections::HashMap>, @@ -475,15 +615,12 @@ struct MapInputs<'a> { /// first time a tool asks for it (Tissot's indicatrix does), once for the run; `None` on the /// globe. flat: std::cell::OnceCell>, - /// The view's longitude/latitude extent, worked out the first time a tool asks for it (it - /// inverse-projects the view, which most tools never need). - view: std::cell::OnceCell>, } -impl MapInputs<'_> { +impl MapInputs { /// The flat map's projection, built on first use. fn flat(&self) -> Option<&(String, rustgis_map::projection::Flat, f64)> { - let map = self.session.map(); + let map = &self.snap.map; self.flat .get_or_init(|| { (map.projection != rustgis_map::Projection::Globe).then(|| { @@ -499,8 +636,15 @@ impl MapInputs<'_> { } } -fn map_inputs<'a>(s: &'a Session, spec: Option<&rustgis_analysis::ToolSpec>, params: &Value) -> Result, String> { - let full = streamed_inputs(s.map(), spec, params)?; +fn map_inputs(s: &Session, spec: Option<&rustgis_analysis::ToolSpec>, params: &Value) -> Result { + inputs_of(Snapshot::of(s), spec, params) +} + +/// A tool's inputs from what it read of the session: the streamed layers and files it names read +/// whole, and the rows of each layer it runs on. It reads, so a background run does this on its +/// thread. +fn inputs_of(snap: Snapshot, spec: Option<&rustgis_analysis::ToolSpec>, params: &Value) -> Result { + let full = streamed_inputs(&snap.map, spec, params)?; let messages = Vec::new(); #[cfg(not(target_arch = "wasm32"))] let mut messages = messages; @@ -510,7 +654,7 @@ fn map_inputs<'a>(s: &'a Session, spec: Option<&rustgis_analysis::ToolSpec>, par if let Some(spec) = spec.filter(|t| rustgis_analysis::external_source(t).is_none()) { for parameter in spec.params.iter().filter(|p| p.kind == rustgis_analysis::ParamKind::VectorLayers) { for name in rustgis_analysis::layer_list(params.get(parameter.name).unwrap_or(&Value::Null))? { - if s.map().layer_by_name(name).is_none() && !full.contains_key(name) { + if snap.map.layer_by_name(name).is_none() && !full.contains_key(name) { let (dataset, report) = rustgis_data::io::read_path(std::path::Path::new(name)).map_err(|e| format!("{name}: {e}"))?; messages.extend(report.messages.into_iter().map(|message| format!("{name}: {message}"))); full.insert(name.to_string(), dataset); @@ -518,9 +662,9 @@ fn map_inputs<'a>(s: &'a Session, spec: Option<&rustgis_analysis::ToolSpec>, par } } } - let mut rows = env_inputs(s.map(), &full, &s.project.gp_env, params)?; + let mut rows = env_inputs(&snap.map, &full, &snap.env, params)?; for (name, dataset) in &full { - let Some(layer) = s.map().layer_by_name(name).filter(|l| !l.selection.is_empty()) else { continue }; + let Some(layer) = snap.map.layer_by_name(name).filter(|l| !l.selection.is_empty()) else { continue }; let missing = || format!("{}: a selected feature is no longer in the source", layer.name); let mut by_id = layer .selection @@ -539,7 +683,7 @@ fn map_inputs<'a>(s: &'a Session, spec: Option<&rustgis_analysis::ToolSpec>, par .collect::, _>>()?; rows.insert(name.clone(), selected); } - Ok(MapInputs { session: s, rows, loaded: full, messages, flat: std::cell::OnceCell::new(), view: std::cell::OnceCell::new() }) + Ok(MapInputs { snap, rows, loaded: full, messages, flat: std::cell::OnceCell::new() }) } /// Complete streamed inputs without selections, plus selected PMTiles inputs: that exporter @@ -643,7 +787,7 @@ fn env_rows( .collect() } -impl Inputs for MapInputs<'_> { +impl Inputs for MapInputs { fn vector_to_pmtiles( &self, dataset: &Dataset, @@ -656,7 +800,7 @@ impl Inputs for MapInputs<'_> { Ok((out.bytes, out.messages)) } fn raster_to_pmtiles(&self, layer: &str, min_zoom: u8, max_zoom: u8) -> Result, String> { - let map = self.session.map(); + let map = &self.snap.map; let layer = map.layer_by_name(layer).ok_or_else(|| format!("no raster layer named '{layer}'"))?; let raster = layer.raster.as_ref().ok_or_else(|| format!("no raster layer named '{}'", layer.name))?; let opacity = map.layer_opacity(layer); @@ -666,19 +810,15 @@ impl Inputs for MapInputs<'_> { self.loaded .get(layer) .or_else(|| self.loaded.get(&layer.to_ascii_lowercase())) - .or_else(|| self.session.map().layer_by_name(layer).map(|l| &l.dataset)) + .or_else(|| self.snap.map.layer_by_name(layer).map(|l| &l.dataset)) // A table as commands find one: by its name, else (names can repeat) by its id. - .or_else(|| { - crate::project::find_table(&self.session.project.tables, layer) - .and_then(|i| self.session.project.tables.get(i)) - .map(|t| &t.dataset) - }) + .or_else(|| crate::project::find_table(&self.snap.tables, layer).and_then(|i| self.snap.tables.get(i)).map(|t| &t.dataset)) } fn table(&self, id: u32) -> Option<&Dataset> { - self.session.project.tables.iter().find(|t| t.id == id).map(|t| &t.dataset) + self.snap.tables.iter().find(|t| t.id == id).map(|t| &t.dataset) } fn selection(&self, layer: &str) -> &[usize] { - let Some(l) = self.session.map().layer_by_name(layer) else { return &[] }; + let Some(l) = self.snap.map.layer_by_name(layer) else { return &[] }; // Loaded selections and environment extents are already rows of the dataset returned // above. An extent containing none is refused before the run by `env_inputs`. self.rows.get(&l.name.to_ascii_lowercase()).map(Vec::as_slice).unwrap_or(&l.selection) @@ -691,16 +831,10 @@ 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.session.map().projection.is_globe() { - return Some(rustgis_geom::Extent::new(-180.0, -90.0, 180.0, 90.0)); - } - Some(super::workspace::view_extent(self.session)).filter(|e| !e.is_empty()) - }) + self.snap.view } fn raster(&self, layer: &str) -> Option> { - let rl = self.session.map().layer_by_name(layer)?.raster.as_ref()?; + let rl = self.snap.map.layer_by_name(layer)?.raster.as_ref()?; Some(RasterInput { raster: rl.data.as_deref()?, path: rl.path.as_deref(), diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 06dacf0ba6..922769013b 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -23730,3 +23730,65 @@ fn a_cancelled_tool_leaves_the_map_as_it_was_and_says_so_in_the_history() { crate::cmd::gp::run_tool(&mut s, "idwInterpolation", ¶ms).unwrap_or_else(|e| panic!("{e}")); assert_eq!(s.map().layers.len(), layers + 1); } + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn built_in_tools_run_off_the_session() { + use crate::cmd::gp::{tool_abandon, tool_complete, tool_start, tool_waiting}; + use rustgis_analysis::control::Control; + fn sends(_: &T) {} + let mut s = world(); + // Started: what it reads is taken now; it runs elsewhere while the project goes on. + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + assert_eq!(run.tool, "buffer"); + sends(&run); + let id = run.id; + assert!(tool_waiting(&s, id)); + let made = std::thread::spawn(move || run.run(&Control::new())); + s.run("layer.remove", &json!({"layer": "Cities"})).unwrap(); + let done = made.join().unwrap(); + tool_complete(&mut s, id, done).unwrap(); + // Applied as gp.run applies a run: its layer, its history, undo. + let before = s.map().layers.len(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + assert_eq!(s.map().layers.len(), before + 1); + let h = s.project.gp_history.last().unwrap(); + assert!(h.ok && h.tool == "Buffer" && h.params["input"] == "Cities", "{h:?}"); + s.run("edit.undo", &Value::Null).unwrap(); + assert_eq!(s.map().layers.len(), before); + // Cancelled while it runs, or before: it fails, saying so, and adds nothing (a failed command + // leaves the project as it was, as a failed gp.run does). + let mut s = world(); + let layers = s.map().layers.len(); + let run = tool_start(&mut s, "idwInterpolation", &json!({"input": "Cities", "field": "pop_max", "pixelSize": 5.0})).unwrap(); + let control = Control::new(); + control.cancel(); + let id = run.id; + let done = run.run(&control); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert_eq!(e, "IDW Interpolation was cancelled before it started"); + assert_eq!(s.map().layers.len(), layers); + // Inputs it can't read fail its run, in the history, rather than its start. + let missing = std::env::temp_dir().join(format!("rustgis-no-such-bg-{}.geojson", std::process::id())); + let run = tool_start(&mut s, "merge", &json!({"inputs": [missing.display().to_string(), "Cities"]})).unwrap(); + let id = run.id; + let done = run.run(&Control::new()); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert!(e.starts_with(&missing.display().to_string()), "{e}"); + // Parameters are checked when it starts; plugin tools start their own way; a run completes once. + assert!(tool_start(&mut s, "buffer", &json!({})).err().unwrap().contains("required")); + assert!(tool_start(&mut s, "plugin:none/x", &json!({})).err().unwrap().contains("no tool")); + let again = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + let done = again.run(&Control::new()); + assert!(tool_complete(&mut s, id, done).unwrap_err().contains("dropped"), "completed once"); + // Opening another project drops its runs; one whose thread couldn't start is forgotten. + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + s.run("project.new", &Value::Null).unwrap(); + assert!(!tool_waiting(&s, run.id)); + let mut s = world(); + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + tool_abandon(&mut s, run.id); + assert!(!tool_waiting(&s, run.id)); +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index d1801985a9..f3f23c69b3 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -3764,6 +3764,8 @@ impl RustGisApp { rustgis_engine::plugins::set_language(self.lang().code()); panes::poll_geolibre(self, ctx); panes::poll_plugin_run(self, ctx); + #[cfg(not(target_arch = "wasm32"))] + panes::poll_tool_run(self, ctx); self.poll_downloads(); let _ = self.deliver_gp_files(Ok(Value::Null)); // A value picked under Dynamic Text lasts while the text tool does, however the tool diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 0ce6bdb04b..5efdd02f2f 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -2648,6 +2648,9 @@ pub struct GpForm { pub web_run: Option<(u64, &'static str, crate::Slot)>, /// A plugin tool running on a thread of its own, so the window stays responsive. pub plugin_run: Option, + /// A built-in tool running on a thread of its own, likewise (the browser runs them at once). + #[cfg(not(target_arch = "wasm32"))] + pub tool_run: Option, /// Files picked in the browser for file parameters, which have no paths there: (name, /// contents), sent with the run while the parameter still shows that name. pub files: HashMap<&'static str, (String, Vec)>, @@ -2671,6 +2674,68 @@ pub struct PluginJob { pub control: std::sync::Arc, } +/// A built-in tool running on a thread of its own. +#[cfg(not(target_arch = "wasm32"))] +pub struct ToolJob { + pub id: u64, + pub tool: &'static str, + /// What the run made, once it has. + pub slot: crate::Slot, + /// Stops the run, and counts its progress. + pub control: std::sync::Arc, +} + +/// Starts a built-in tool on a thread of its own; [`poll_tool_run`] applies what it made. +#[cfg(not(target_arch = "wasm32"))] +fn start_tool_run(app: &mut RustGisApp, tool: &'static str, params: &Value, ctx: &egui::Context) { + match rustgis_engine::cmd::gp::tool_start(&mut app.session, tool, params) { + Ok(run) => { + let slot = crate::Slot::default(); + let control = rustgis_engine::analysis::control::Control::new(); + let (out, ctl, ctx) = (slot.clone(), control.clone(), ctx.clone()); + let id = run.id; + let spawned = std::thread::Builder::new().name(tool.into()).spawn(move || { + let done = run.run(&ctl); + // Filled even when poisoned, or the pane would wait for it forever. + *out.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(done); + ctx.request_repaint(); + }); + match spawned { + Ok(_) => { + app.gp.tool_run = Some(ToolJob { id, tool, slot, control }); + app.gp.last = None; + } + Err(e) => { + rustgis_engine::cmd::gp::tool_abandon(&mut app.session, id); + app.gp.last = Some((false, vec![e.to_string()])); + } + } + } + Err(e) => app.gp.last = Some((false, vec![e])), + } +} + +/// Applies a built-in tool run that finished on its thread. +#[cfg(not(target_arch = "wasm32"))] +pub fn poll_tool_run(app: &mut RustGisApp, ctx: &egui::Context) { + let Some(job) = &app.gp.tool_run else { return }; + // Its project was closed: stop it, as its result has nowhere to go. + if !rustgis_engine::cmd::gp::tool_waiting(&app.session, job.id) { + job.control.cancel(); + app.gp.tool_run = None; + return; + } + let Some(done) = take(&job.slot) else { + // Its progress moves as it works. + ctx.request_repaint_after(std::time::Duration::from_millis(100)); + return; + }; + let (id, tool) = (job.id, job.tool); + app.gp.tool_run = None; + let r = rustgis_engine::cmd::gp::tool_complete(&mut app.session, id, done).and_then(|_| app.run("gp.finish", json!({"id": id}))); + finished(app, tool, r); +} + /// Starts a plugin tool on a thread of its own; [`poll_plugin_run`] applies what it returns. #[cfg(not(target_arch = "wasm32"))] fn start_plugin_run(app: &mut RustGisApp, tool: &'static str, params: &Value, ctx: &egui::Context) { @@ -3673,8 +3738,10 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } ui.add_space(8.0); ui.with_layout(egui::Layout::right_to_left(egui::Align::Min), |ui| { - // One browser run at a time, and one plugin run. + // One browser run at a time, and one background run. let idle = app.gp.web_run.is_none() && app.gp.plugin_run.is_none(); + #[cfg(not(target_arch = "wasm32"))] + let idle = idle && app.gp.tool_run.is_none(); if crate::widgets::primary_button(ui, tl!("Run"), idle, vec2(96.0, 30.0)).clicked() { // A blank output opens Save As before any work starts. Cancelling writes nothing. #[cfg(not(target_arch = "wasm32"))] @@ -3737,6 +3804,10 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { // On a thread of its own: a plugin tool may run for minutes. #[cfg(not(target_arch = "wasm32"))] start_plugin_run(app, spec.id, ¶ms, ui.ctx()); + } else if !cfg!(target_arch = "wasm32") { + // A built-in tool too, with its progress and Cancel; the browser has no threads. + #[cfg(not(target_arch = "wasm32"))] + start_tool_run(app, spec.id, ¶ms, ui.ctx()); } else { let r = app.run("gp.run", json!({"tool": spec.id, "params": params})); app.gp.last = Some(match r { @@ -3775,6 +3846,28 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { job.control.cancel(); } } + #[cfg(not(target_arch = "wasm32"))] + if let Some(job) = &app.gp.tool_run { + ui.add_space(8.0); + let mut cancel = false; + ui.horizontal(|ui| { + ui.spinner(); + let name = rustgis_engine::analysis::tool(job.tool).map(|t| t.name).unwrap_or(job.tool); + // Two labels, not one sentence put together from translated pieces. + ui.label(egui::RichText::new(tl!("Running…")).size(12.0)); + ui.label(egui::RichText::new(tl!(name)).size(12.0).strong()); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + cancel = ui.add_enabled(!job.control.is_cancelled(), egui::Button::new(tl!("Cancel"))).clicked(); + }); + }); + // How far its current step has come, when it counts its work. + if let Some((done, total)) = job.control.progress() { + ui.add(egui::ProgressBar::new(done as f32 / total.max(1) as f32).show_percentage().desired_height(14.0)); + } + if cancel { + job.control.cancel(); + } + } if let Some((ok, msgs)) = &app.gp.last { ui.add_space(8.0); let (fill, stroke, head) = @@ -6333,6 +6426,46 @@ pub(crate) fn layout_menu(app: &mut RustGisApp, ui: &mut Ui, n: &str) { mod tests { use super::*; + /// Run starts a built-in tool on a thread of its own: the pane shows it running, with Cancel, + /// and its result lands as gp.run's would, with its layer and history. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn the_geoprocessing_pane_runs_a_tool_in_the_background() { + use egui_kittest::kittest::Queryable; + let mut app = RustGisApp::with_sample(); + let _ = app.run("ui.gp.open", json!({"tool": "buffer"})); + app.gp.values.insert("input", "Cities".into()); + app.gp.values.insert("distance", "50".into()); + let layers = app.session.map().layers.len(); + let mut fonts = false; + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(420.0, 1000.0)).build_ui_state( + move |ui, app: &mut RustGisApp| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + return; + } + poll_tool_run(app, ui.ctx()); + geoprocessing(app, ui, &Tokens::light()); + }, + app, + ); + h.run(); + h.get_by_label("Run").click(); + h.step(); + assert!(h.state().gp.tool_run.is_some(), "it runs in the background"); + let end = std::time::Instant::now() + std::time::Duration::from_secs(60); + while h.state().gp.tool_run.is_some() { + assert!(std::time::Instant::now() < end, "the run never landed"); + std::thread::sleep(std::time::Duration::from_millis(10)); + h.step(); + } + let app = h.state(); + assert_eq!(app.gp.last.as_ref().map(|l| l.0), Some(true), "{:?}", app.gp.last); + assert_eq!(app.session.map().layers.len(), layers + 1); + assert_eq!(app.session.project.gp_history.last().map(|r| (r.ok, r.tool.as_str())), Some((true, "Buffer"))); + } + #[test] fn field_source_uses_the_longest_input_prefix() { use rustgis_engine::analysis::{ParamSpec, ToolSpec}; diff --git a/crates/ui-egui/tests/tool_field_lists.rs b/crates/ui-egui/tests/tool_field_lists.rs index 086f236513..a4a30fde2f 100644 --- a/crates/ui-egui/tests/tool_field_lists.rs +++ b/crates/ui-egui/tests/tool_field_lists.rs @@ -33,6 +33,19 @@ fn harness(tool: &str, values: serde_json::Value) -> Harness<'static, RustGisApp h } +/// Clicks Run and waits for the tool to land: on the desktop it runs on a thread of its own. +fn click_run(h: &mut Harness<'_, RustGisApp>) { + h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); + h.run_steps(1); + let end = std::time::Instant::now() + std::time::Duration::from_secs(60); + while h.state().gp.tool_run.is_some() { + assert!(std::time::Instant::now() < end, "the run never landed"); + std::thread::sleep(std::time::Duration::from_millis(5)); + h.run_steps(1); + } + h.run_steps(2); +} + /// AccessKit labels distinguish the list's field and statistic selectors from other tool inputs. fn choose(h: &mut Harness<'_, RustGisApp>, label: &str, value: &str) { h.get_by_role_and_label(egui::accesskit::Role::ComboBox, label).click(); @@ -52,8 +65,7 @@ fn summary_list_runs_with_ordered_case_fields_and_reopens_from_history() { h.get_by_label("amount").click(); h.run_steps(2); choose(&mut h, "Statistic", "SUM"); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); - h.run_steps(3); + click_run(&mut h); let history = h.state().session.project.gp_history.last().unwrap(); assert_eq!(history.params["statistics"], json!([{"field":"amount","statistic":"SUM"}])); assert_eq!(history.params["case"], json!(["region", "category"])); @@ -67,8 +79,7 @@ fn summary_list_runs_with_ordered_case_fields_and_reopens_from_history() { reversed["case"] = json!(["category", "region"]); h.state_mut().run("ui.gp.open", json!({"tool":"summaryStatistics", "values":reversed})).unwrap(); h.run_steps(3); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); - h.run_steps(3); + click_run(&mut h); let output = &h.state().session.project.tables.last().unwrap().dataset; assert_eq!(output.fields.iter().map(|f| f.name.as_str()).collect::>(), ["category", "region", "FREQUENCY", "SUM_amount"]); assert_eq!(output.value(0, 3).as_f64(), Some(16.3)); @@ -92,8 +103,7 @@ fn merge_rules_offer_join_fields_and_text_rules_and_preserve_delimiter() { h.run_steps(2); let rules: serde_json::Value = serde_json::from_str(&h.state().gp.values["merge"]).unwrap(); assert_eq!(rules, json!([{"field":"category","rule":"join","delimiter":"; "}])); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); - h.run_steps(3); + click_run(&mut h); let history = h.state().session.project.gp_history.last().unwrap(); assert_eq!(history.params["merge"], json!([{"field":"category","rule":"join","delimiter":"; "}])); } @@ -223,8 +233,7 @@ fn model_merge_fields_follow_the_wired_join_input() { fn invalid_statistics_text_is_not_changed_into_an_empty_list() { let mut h = harness("summaryStatistics", json!({"input":"Records", "field":"amount", "statistics":"null"})); assert!(h.query_by_label("JSON value").is_some()); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); - h.run_steps(3); + click_run(&mut h); let result = h.state().gp.last.as_ref().unwrap(); assert!(!result.0); assert!(result.1.iter().any(|message| message.contains("Statistics must be a list"))); @@ -421,8 +430,7 @@ fn summary_picker_excludes_case_variant_output_collisions() { assert!(h.query_by_role_and_label(egui::accesskit::Role::Button, "COUNT").is_none()); h.get_by_role_and_label(egui::accesskit::Role::Button, "SUM").click_accesskit(); h.run_steps(2); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); - h.run_steps(3); + click_run(&mut h); assert!(h.state().gp.last.as_ref().unwrap().0); let output = &h.state().session.project.tables.last().unwrap().dataset; assert_eq!(output.fields.iter().map(|f| f.name.as_str()).collect::>(), ["FREQUENCY", "COUNT_region", "SUM_REGION"]); @@ -448,8 +456,7 @@ fn saved_duplicate_statistics_explain_how_to_repair_them() { h.get_all_by_role_and_label(egui::accesskit::Role::Button, "Remove").nth(1).unwrap().click(); h.run_steps(3); assert!(h.query_by_label("Statistic is already listed for this field.").is_none()); - h.get_by_role_and_label(egui::accesskit::Role::Button, "Run").click(); - h.run_steps(3); + click_run(&mut h); assert!(h.state().gp.last.as_ref().unwrap().0); let output = &h.state().session.project.tables.last().unwrap().dataset; assert_eq!(output.value(0, 1).as_f64(), Some(16.3)); diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 71c822e301..4da8f4a4a8 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -57,6 +57,16 @@ Open a tool from **Analyze › Portal › Feature Analysis** or **Raster Analysi which searches every tool) or Command Search. Fill in its parameters and click **Run**: new layers or tables are added to the project; selection tools select existing rows and Calculate Geometry Attributes updates the input layer. Each run is listed under **Analyze › History**. + +In the desktop app a tool runs in the background, so the map and the rest of the window stay in +use. While it runs, the Tools pane shows its name, a **Cancel** button and, for the tools that +count their work (Viewshed, IDW Interpolation, Kriging, Kernel Density, Buffer, Select Layer By +Location and others), a progress bar. One tool runs at a time. A cancelled run adds nothing and +says how far it came. Edits made to its input layers while it runs are kept: a selection it makes +selects the same features where they now are, and a result that would replace an edited layer is +refused. Opening another project drops the run. Scripts, models, the AI Assistant, the command +line and the web app wait for each tool to finish. + From the command line or a script (`gp.run`), a built-in tool's parameter with a list of choices, such as Buffer's units, takes one of them in any letter case (`"meters"` for `Meters`); any other value is an error that names the choices (Buffer's units no longer take the abbreviations `m`, `mi` or `metres`). @@ -766,8 +776,9 @@ next visit. The runner's code comes from jsDelivr, pinned to one geolibre-wasm r same trust as installing that package. Tools that read files by path (LiDAR files, for instance) need the desktop app, since a web page has no file system. -In the desktop app and on the command line, RustGIS waits while a tool runs, so a long run on -a large raster or point cloud keeps the window busy until it finishes. In the web app the run +In the desktop app the Tools pane runs a GeoLibre tool in the background like any other; its +**Cancel** takes effect when the GeoLibre program ends, and the result is then dropped. On the +command line RustGIS waits while a tool runs. In the web app the run is started in the background and the result is added when it ends, but the tool itself runs in the page, so the page is busy while it computes. From 603f1814ae6f352683fe5ce9b092c02068083b64 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Mon, 2 Nov 2026 14:25:29 +0100 Subject: [PATCH 2/7] perf(gp): a background run takes its layers' state without reading their rows From the review before opening: starting a background run fingerprinted each input layer on the UI thread by serialising its whole dataset, and did so again when the run completed, so a large layer froze the window at Run. A run now keeps a copy of each dataset that shares its rows. A layer is unchanged when it still shares those rows (any edit copies them) and has the same fields. That check costs nothing however large the layer, and plugin runs benefit too. A layer renamed while a tool that rewrites it runs is updated under its new name: the applied dataset takes the layer's name, as GeoLibre's update does. Tests show that a field edited meanwhile gets the result refused, and that a rename doesn't. The user guide says only the tools that count their work say how far a cancelled run came. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/gp.rs | 35 +++++++++++------------------------ crates/engine/src/tests.rs | 23 +++++++++++++++++++++++ docs/user-guide/analysis.md | 4 ++-- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 839751d26f..b574b4a9c7 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -501,12 +501,12 @@ fn fit_to_changes( return; }; layer.clone_from(&now.name); - if was.fingerprint.is_some() && fingerprint(&now.dataset) == was.fingerprint { + if was.unchanged(&now.dataset) { return; } match &mut r.output { Output::Selection { rows, .. } => { - let ids: std::collections::HashSet = rows.iter().filter_map(|r| was.ids.get(*r)).copied().collect(); + let ids: std::collections::HashSet = rows.iter().filter_map(|r| was.dataset.features.get(*r)).map(|f| f.id).collect(); *rows = now.dataset.features.iter().enumerate().filter(|(_, f)| ids.contains(&f.id)).map(|(i, _)| i).collect(); } _ => *res = Err(format!("{} changed while {tool} ran; run it again", now.name)), @@ -516,35 +516,20 @@ fn fit_to_changes( /// A layer a background run read, to tell whether it changed while the run went on. pub(crate) struct Read { id: u32, - /// Its features' ids, in order: what its rows were. - ids: Vec, - /// None when it couldn't be taken: the layer then counts as changed. - fingerprint: Option, + /// Its dataset as the run read it: a copy that shares the rows, so taking it costs nothing + /// however large the layer, and any edit since (which copies the rows held here too) shows. + dataset: Dataset, } impl Read { fn of(l: &rustgis_map::Layer) -> Self { - Read { id: l.id, ids: l.dataset.features.iter().map(|f| f.id).collect(), fingerprint: fingerprint(&l.dataset) } + Read { id: l.id, dataset: l.dataset.clone() } } -} -/// A hash of a dataset's contents, without keeping a copy of them. -fn fingerprint(d: &Dataset) -> Option { - use std::hash::Hasher; - struct W(std::hash::DefaultHasher); - impl std::io::Write for W { - fn write(&mut self, b: &[u8]) -> std::io::Result { - self.0.write(b); - Ok(b.len()) - } - fn flush(&mut self) -> std::io::Result<()> { - Ok(()) - } + /// Whether `now` is the dataset as it was read: the same rows in memory, and the same fields. + fn unchanged(&self, now: &Dataset) -> bool { + now.features.shares(&self.dataset.features) && now.fields == self.dataset.fields } - let mut w = W(std::hash::DefaultHasher::new()); - // A hash of part of it would say too little. - serde_json::to_writer(&mut w, d).ok()?; - Some(w.0.finish()) } /// Fits a tool's result to the map: a selection made on the complete streamed source becomes @@ -1161,6 +1146,8 @@ fn apply(s: &mut Session, ran: Ran, params: Value, res: Result { let l = s.map_mut().layers.iter_mut().find(|l| l.name.eq_ignore_ascii_case(&layer)).ok_or("the input layer disappeared")?; l.dataset = dataset; + // Under the layer's name now: a background run's layer may have been renamed meanwhile. + l.dataset.name.clone_from(&l.name); // A streamed layer updated by a tool holds the whole result in memory from now on. l.dataset.windowed = false; l.source = None; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 922769013b..5333d5980d 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -23783,6 +23783,29 @@ fn built_in_tools_run_off_the_session() { let again = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); let done = again.run(&Control::new()); assert!(tool_complete(&mut s, id, done).unwrap_err().contains("dropped"), "completed once"); + // A tool that rewrites its layer applies when the layer is as it read it, and is refused when + // it was edited meanwhile, even only in its fields (the rows, however many, aren't compared). + let mut s = world(); + let run = tool_start(&mut s, "calculateGeometry", &json!({"input": "Countries"})).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + // Renamed meanwhile, the layer it read is updated, under its new name. + let run = tool_start(&mut s, "calculateGeometry", &json!({"input": "Countries"})).unwrap(); + s.run("layer.rename", &json!({"layer": "Countries", "name": "Nations"})).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + let nations = s.map().layer_by_name("Nations").unwrap(); + assert_eq!(nations.dataset.name, "Nations"); + s.run("layer.rename", &json!({"layer": "Nations", "name": "Countries"})).unwrap(); + let run = tool_start(&mut s, "calculateGeometry", &json!({"input": "Countries"})).unwrap(); + let countries = s.map_mut().layers.iter_mut().find(|l| l.name == "Countries").unwrap(); + countries.dataset.fields[0].name = "renamed".into(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert!(e.contains("changed while"), "{e}"); // Opening another project drops its runs; one whose thread couldn't start is forgotten. let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); s.run("project.new", &Value::Null).unwrap(); diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 4da8f4a4a8..51632104af 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -61,8 +61,8 @@ Geometry Attributes updates the input layer. Each run is listed under **Analyze In the desktop app a tool runs in the background, so the map and the rest of the window stay in use. While it runs, the Tools pane shows its name, a **Cancel** button and, for the tools that count their work (Viewshed, IDW Interpolation, Kriging, Kernel Density, Buffer, Select Layer By -Location and others), a progress bar. One tool runs at a time. A cancelled run adds nothing and -says how far it came. Edits made to its input layers while it runs are kept: a selection it makes +Location and others), a progress bar. One tool runs at a time. A cancelled run adds nothing, and +a tool that counts its work says how far it came. Edits made to its input layers while it runs are kept: a selection it makes selects the same features where they now are, and a result that would replace an edited layer is refused. Opening another project drops the run. Scripts, models, the AI Assistant, the command line and the web app wait for each tool to finish. From 53ba509dec8a054ebd301c1627975dbad020f06c Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Mon, 2 Nov 2026 15:02:06 +0100 Subject: [PATCH 3/7] fix(gp): background runs borrow nothing on the UI thread's runs, keep to their map, and record as gp.run From the first review round of #1317: - `gp.run` borrows the session again (map, tables, environment), as before this PR: only a background run takes a copy. The view extent is worked out again only when a tool asks for it. - A background run remembers the map it ran on. A result that would add to or change the map is refused, with a message, when another map is active by then, instead of landing on that map. Tables and files are unaffected. - Streamed input layers are tracked by id too, so a result never reaches another layer given the same name after the one it read was removed. - A layer counts as unchanged only with the same rows, fields, geometry type, coordinate system and source. - Record Task records a tool finished in the background as `gp.run` with its tool and parameters, which a replay can run, not as `gp.finish` with a run id that means nothing later. Plugin runs benefit as well. - A background run dropped because its project closed says so. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/gp.rs | 145 +++++++++++++++++++---------- crates/engine/src/cmd/workspace.rs | 7 +- crates/engine/src/tests.rs | 17 +++- crates/ui-egui/src/lib.rs | 37 ++++++++ crates/ui-egui/src/panes.rs | 12 ++- 5 files changed, 164 insertions(+), 54 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index b574b4a9c7..ef29b3767b 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -288,8 +288,10 @@ pub(crate) struct StartedRun { /// The feature ids of the streamed layers it reads whole, by lowercase name (a built-in tool /// reads them on its thread, and hands them back with its result). full_ids: FullIds, - /// The other layers it reads, as it read them, by lowercase name. + /// The layers it reads, as it read them, by lowercase name. read: std::collections::HashMap, + /// The map it ran on: its place among the project's maps, and its name. + map: (usize, String), } /// The feature ids of the streamed layers a tool read whole, by lowercase name: what its @@ -312,16 +314,21 @@ pub fn plugin_start(s: &mut Session, tool: &str, params: &Value) -> Result MAX_ASYNC_RUNS { s.plugin_runs.remove(0); @@ -362,6 +369,12 @@ fn complete( .ok_or("the tool's run was dropped (its project was closed, or too many runs were waiting)")?; let run = s.plugin_runs.remove(i); let mut result = result; + // What it adds to or changes on the map belongs on the map it ran on. + let on_map = matches!(&result, Ok(r) if matches!(r.output, Output::Layer(_) | Output::Selection { .. } | Output::Update { .. } | Output::Layers(_))); + if on_map && (s.project.active_map, s.map().name.as_str()) != (run.map.0, run.map.1.as_str()) { + result = + Err(format!("{} ran on the map '{}', which is no longer the active one: open it and run the tool again", run.name, run.map.1)); + } fit_to_changes(s, &mut result, &run.name, &run.read); fit_to_windows(s, &mut result, full_ids.as_ref().unwrap_or(&run.full_ids)); s.pending.push(crate::PendingRun { @@ -387,7 +400,7 @@ pub struct ToolRun { pub id: u64, /// The tool's id. pub tool: &'static str, - snap: Snapshot, + snap: Snapshot<'static>, params: Value, } @@ -436,12 +449,7 @@ pub fn tool_start(s: &mut Session, tool: &str, params: &Value) -> Result Result MAX_ASYNC_RUNS { s.plugin_runs.remove(0); } - Ok(ToolRun { id, tool: spec.id, snap: Snapshot::of(s), params: params.clone() }) + Ok(ToolRun { id, tool: spec.id, snap: Snapshot::owned(s), params: params.clone() }) } #[cfg(not(target_arch = "wasm32"))] @@ -501,12 +510,14 @@ fn fit_to_changes( return; }; layer.clone_from(&now.name); - if was.unchanged(&now.dataset) { + // A streamed layer's rows follow the view: only that it is still there is checked. + let Some(dataset) = &was.dataset else { return }; + if Read::unchanged(dataset, &now.dataset) { return; } match &mut r.output { Output::Selection { rows, .. } => { - let ids: std::collections::HashSet = rows.iter().filter_map(|r| was.dataset.features.get(*r)).map(|f| f.id).collect(); + let ids: std::collections::HashSet = rows.iter().filter_map(|r| dataset.features.get(*r)).map(|f| f.id).collect(); *rows = now.dataset.features.iter().enumerate().filter(|(_, f)| ids.contains(&f.id)).map(|(i, _)| i).collect(); } _ => *res = Err(format!("{} changed while {tool} ran; run it again", now.name)), @@ -515,23 +526,41 @@ fn fit_to_changes( /// A layer a background run read, to tell whether it changed while the run went on. pub(crate) struct Read { + /// The layer, by id: renamed perhaps, but not another of the same name. id: u32, /// Its dataset as the run read it: a copy that shares the rows, so taking it costs nothing /// however large the layer, and any edit since (which copies the rows held here too) shows. - dataset: Dataset, + /// `None` for a streamed layer, whose rows follow the view. + dataset: Option, } impl Read { fn of(l: &rustgis_map::Layer) -> Self { - Read { id: l.id, dataset: l.dataset.clone() } + Read { id: l.id, dataset: (!l.is_streamed()).then(|| l.dataset.clone()) } } - /// Whether `now` is the dataset as it was read: the same rows in memory, and the same fields. - fn unchanged(&self, now: &Dataset) -> bool { - now.features.shares(&self.dataset.features) && now.fields == self.dataset.fields + /// Whether `now` is the dataset `was` as it was read: the same rows in memory, and the same + /// fields, geometry type, coordinate system and source. + fn unchanged(was: &Dataset, now: &Dataset) -> bool { + now.features.shares(&was.features) + && now.fields == was.fields + && now.geometry_type == was.geometry_type + && now.crs == was.crs + && now.crs_incomplete == was.crs_incomplete + && now.zm_dropped == was.zm_dropped + && now.source == was.source } } +/// The layers a tool names, as it reads them now, by lowercase name. +fn read_layers(s: &Session, spec: &rustgis_analysis::ToolSpec, resolved: &Value) -> std::collections::HashMap { + layer_names(Some(spec), resolved) + .into_iter() + .filter_map(|n| s.map().layer_by_name(n)) + .map(|l| (l.name.to_ascii_lowercase(), Read::of(l))) + .collect() +} + /// Fits a tool's result to the map: a selection made on the complete streamed source becomes /// rows of the current window. An update from a selection is still refused: it would replace /// the complete source with just those selected features. @@ -562,33 +591,42 @@ fn finish(s: &mut Session, p: &Value) -> CmdResult { apply(s, Ran { name: r.tool, id: r.tool_id, command: None, started: r.started }, r.params, r.result, r.millis) } -/// What a tool reads of the session, taken when it starts: the active map (a copy, which shares -/// the features and pixels), the standalone tables likewise, the geoprocessing environment and the -/// view. It owns them, so a tool can run on another thread while the session goes on. -pub struct Snapshot { - map: rustgis_map::Map, - tables: Vec, - env: crate::project::GpEnvironment, - /// The view's longitude/latitude extent; the whole Earth on the globe. - view: Option, +/// What a tool reads of the session: the active map, the standalone tables, the geoprocessing +/// environment and the view's size. A run on the session's own thread borrows them; one on +/// another thread owns a copy taken when it starts, which shares the features and pixels. +struct Snapshot<'a> { + map: std::borrow::Cow<'a, rustgis_map::Map>, + tables: std::borrow::Cow<'a, [crate::project::Table]>, + env: std::borrow::Cow<'a, crate::project::GpEnvironment>, + /// The map view's size, for its extent. + view_size: (f64, f64), } -impl Snapshot { - pub fn of(s: &Session) -> Snapshot { - let view = if s.map().projection.is_globe() { - // The globe shows the whole Earth (the camera's flat extent means nothing there). - Some(rustgis_geom::Extent::new(-180.0, -90.0, 180.0, 90.0)) - } else { - Some(super::workspace::view_extent(s)).filter(|e| !e.is_empty()) - }; - Snapshot { map: s.map().clone(), tables: s.project.tables.clone(), env: s.project.gp_env.clone(), view } +impl<'a> Snapshot<'a> { + /// The session itself, for a run on its thread. + fn of(s: &'a Session) -> Snapshot<'a> { + use std::borrow::Cow::Borrowed; + Snapshot { map: Borrowed(s.map()), tables: Borrowed(&s.project.tables), env: Borrowed(&s.project.gp_env), view_size: s.view_size } + } + + /// A copy of the session for a run on another thread: cheap, as the features and pixels are + /// shared. + #[cfg(not(target_arch = "wasm32"))] + fn owned(s: &Session) -> Snapshot<'static> { + use std::borrow::Cow::Owned; + Snapshot { + map: Owned(s.map().clone()), + tables: Owned(s.project.tables.clone()), + env: Owned(s.project.gp_env.clone()), + view_size: s.view_size, + } } } /// A tool's inputs on the active map. -struct MapInputs { +struct MapInputs<'a> { /// The session as the tool started: its active map and its standalone tables. - snap: Snapshot, + snap: Snapshot<'a>, /// The rows of each layer (by lowercase name) the tool runs on: the selection or the /// environment's extent. rows: std::collections::HashMap>, @@ -600,9 +638,12 @@ struct MapInputs { /// first time a tool asks for it (Tissot's indicatrix does), once for the run; `None` on the /// globe. flat: std::cell::OnceCell>, + /// The view's longitude/latitude extent, worked out the first time a tool asks for it (it + /// inverse-projects the view, which most tools never need). + view: std::cell::OnceCell>, } -impl MapInputs { +impl MapInputs<'_> { /// The flat map's projection, built on first use. fn flat(&self) -> Option<&(String, rustgis_map::projection::Flat, f64)> { let map = &self.snap.map; @@ -621,14 +662,14 @@ impl MapInputs { } } -fn map_inputs(s: &Session, spec: Option<&rustgis_analysis::ToolSpec>, params: &Value) -> Result { +fn map_inputs<'a>(s: &'a Session, spec: Option<&rustgis_analysis::ToolSpec>, params: &Value) -> Result, String> { inputs_of(Snapshot::of(s), spec, params) } /// A tool's inputs from what it read of the session: the streamed layers and files it names read /// whole, and the rows of each layer it runs on. It reads, so a background run does this on its /// thread. -fn inputs_of(snap: Snapshot, spec: Option<&rustgis_analysis::ToolSpec>, params: &Value) -> Result { +fn inputs_of<'a>(snap: Snapshot<'a>, spec: Option<&rustgis_analysis::ToolSpec>, params: &Value) -> Result, String> { let full = streamed_inputs(&snap.map, spec, params)?; let messages = Vec::new(); #[cfg(not(target_arch = "wasm32"))] @@ -668,7 +709,7 @@ fn inputs_of(snap: Snapshot, spec: Option<&rustgis_analysis::ToolSpec>, params: .collect::, _>>()?; rows.insert(name.clone(), selected); } - Ok(MapInputs { snap, rows, loaded: full, messages, flat: std::cell::OnceCell::new() }) + Ok(MapInputs { snap, rows, loaded: full, messages, flat: std::cell::OnceCell::new(), view: std::cell::OnceCell::new() }) } /// Complete streamed inputs without selections, plus selected PMTiles inputs: that exporter @@ -772,7 +813,7 @@ fn env_rows( .collect() } -impl Inputs for MapInputs { +impl Inputs for MapInputs<'_> { fn vector_to_pmtiles( &self, dataset: &Dataset, @@ -816,7 +857,13 @@ 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.snap.view + *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()) + }) } fn raster(&self, layer: &str) -> Option> { let rl = self.snap.map.layer_by_name(layer)?.raster.as_ref()?; diff --git a/crates/engine/src/cmd/workspace.rs b/crates/engine/src/cmd/workspace.rs index 7ac15a104a..e7d2ad31b7 100644 --- a/crates/engine/src/cmd/workspace.rs +++ b/crates/engine/src/cmd/workspace.rs @@ -340,8 +340,11 @@ pub(crate) fn fill_cache(s: &mut Session) -> usize { /// The longitude/latitude extent the flat map's view shows. pub fn view_extent(s: &Session) -> Extent { - let m = s.map(); - let (w, h) = s.view_size; + view_extent_of(s.map(), s.view_size) +} + +/// The longitude/latitude extent a `size` view of map `m` shows. +pub fn view_extent_of(m: &rustgis_map::Map, (w, h): (f64, f64)) -> Extent { let e = m.camera.extent(w, h); let mut ll = Extent::EMPTY; for i in 0..=8 { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 5333d5980d..a8ac2acfd0 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -23781,8 +23781,23 @@ fn built_in_tools_run_off_the_session() { assert!(tool_start(&mut s, "buffer", &json!({})).err().unwrap().contains("required")); assert!(tool_start(&mut s, "plugin:none/x", &json!({})).err().unwrap().contains("no tool")); let again = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); - let done = again.run(&Control::new()); + let id = again.id; + tool_complete(&mut s, id, again.run(&Control::new())).unwrap(); + let late = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + let done = late.run(&Control::new()); assert!(tool_complete(&mut s, id, done).unwrap_err().contains("dropped"), "completed once"); + s.run("gp.finish", &json!({"id": id})).unwrap(); + // A run whose map is no longer the active one adds nothing to the one that is, and says so. + s.run("map.new", &json!({"name": "Second"})).unwrap(); + s.project.active_map = 0; + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + s.project.active_map = 1; + let layers = s.map().layers.len(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert!(e.contains("no longer the active one"), "{e}"); + assert_eq!(s.map().layers.len(), layers); // A tool that rewrites its layer applies when the layer is as it read it, and is refused when // it was edited meanwhile, even only in its fields (the rows, however many, aren't compared). let mut s = world(); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index f3f23c69b3..06b6fb0b68 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1883,6 +1883,20 @@ impl RustGisApp { /// made. /// `active` is the active layer's name from [`Self::layer_acted_on`], read before it ran. fn record_step(&mut self, id: &str, params: &Value, active: Option<(String, bool)>) { + if self.ws.recording.is_none() { + return; + } + // A tool run in the background is applied by `gp.finish`, whose run id means nothing to a + // replay: the step is the run itself, `gp.run` with its tool and parameters, from the + // history entry it has just added. + let finished; + let (id, params) = if id == "gp.finish" { + let Some(h) = self.session.project.gp_history.last().filter(|h| !h.id.is_empty()) else { return }; + finished = json!({"tool": h.id, "params": h.params}); + ("gp.run", &finished) + } else { + (id, params) + }; let Some(steps) = self.ws.recording.as_mut() else { return }; // Undo doesn't take back a recorded step (the undo history and the recording don't line // up step for step): say how to. @@ -4276,6 +4290,29 @@ mod record_tests { } /// A step keeping a plain `key` is recorded, and that is said once a recording. + #[test] + fn a_tool_finished_in_the_background_is_recorded_as_its_run() { + let mut app = RustGisApp::with_sample(); + app.ws.recording = Some(Vec::new()); + app.session.project.gp_history.push(rustgis_engine::GpRecord { + tool: "Buffer".into(), + id: "buffer".into(), + params: json!({"input": "Cities", "distance": 50}), + command: None, + at: 0, + ok: true, + messages: Vec::new(), + millis: 0, + outputs: Vec::new(), + }); + app.record_step("gp.finish", &json!({"id": 7}), None); + let steps = app.ws.recording.as_deref().unwrap_or_default(); + assert_eq!( + steps.iter().map(|s| (s.command.as_str(), s.params.clone())).collect::>(), + [("gp.run", json!({"tool": "buffer", "params": {"input": "Cities", "distance": 50}}))] + ); + } + #[test] fn a_kept_key_is_said_once() { let mut app = RustGisApp::with_sample(); diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 5efdd02f2f..d94dd6ee97 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -2719,9 +2719,10 @@ fn start_tool_run(app: &mut RustGisApp, tool: &'static str, params: &Value, ctx: #[cfg(not(target_arch = "wasm32"))] pub fn poll_tool_run(app: &mut RustGisApp, ctx: &egui::Context) { let Some(job) = &app.gp.tool_run else { return }; - // Its project was closed: stop it, as its result has nowhere to go. + // Its project was closed: stop it, as its result has nowhere to go, and say so. if !rustgis_engine::cmd::gp::tool_waiting(&app.session, job.id) { job.control.cancel(); + dropped(app, job.tool); app.gp.tool_run = None; return; } @@ -2769,9 +2770,10 @@ fn start_plugin_run(app: &mut RustGisApp, tool: &'static str, params: &Value, ct /// Applies a plugin tool run that finished on its thread. pub fn poll_plugin_run(app: &mut RustGisApp, ctx: &egui::Context) { let Some(job) = &app.gp.plugin_run else { return }; - // Its project was closed: stop it, as its result has nowhere to go. + // Its project was closed: stop it, as its result has nowhere to go, and say so. if !rustgis_engine::cmd::gp::plugin_waiting(&app.session, job.id) { job.control.cancel(); + dropped(app, job.tool); app.gp.plugin_run = None; return; } @@ -2786,6 +2788,12 @@ pub fn poll_plugin_run(app: &mut RustGisApp, ctx: &egui::Context) { finished(app, tool, r); } +/// Says that a tool's run was dropped: its project closed while it ran. +fn dropped(app: &mut RustGisApp, tool: &'static str) { + let name = rustgis_engine::analysis::tool(tool).map(|t| t.name).unwrap_or(tool); + app.session.notify(crate::i18n::fmt("{tool} was stopped: its project was closed while it ran.", &[("tool", tl_raw!(name))]), false); +} + /// Shows how an asynchronous tool run went: under its form when it's open, else as a notice. fn finished(app: &mut RustGisApp, tool: &'static str, r: Result) { let last = match r { From c4313b53d5b3e2b9e04cbbe8e3cf86547d95beb5 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Mon, 2 Nov 2026 15:11:03 +0100 Subject: [PATCH 4/7] fix(gp): a renamed streamed layer's selection maps to its window; a run finds its map after one before it is deleted From the reviews of the previous commit: - A background selection of a streamed layer renamed while the tool ran selected the wrong features without a word: its rows of the whole source were looked up under the layer's new name and never mapped to its window. They are now found under the name it was read by. A test shows it, and fails without the fix. - A run finds the map it ran on where it was, if that map still has its name there, else as the one map of that name, so deleting a map before it no longer refuses a valid result. When the map can't be told (gone, or its name shared), the result is refused as before. - A run also records where each layer it read comes from: a layer with the same id from another source (on a new map given the old one's name) isn't the one it read, and its result is refused rather than land there. - A result of tables alone is never refused for the map. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/gp.rs | 47 ++++++++++++++++++++++++--- crates/engine/src/tests.rs | 63 +++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index ef29b3767b..3d133f96cb 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -369,14 +369,36 @@ fn complete( .ok_or("the tool's run was dropped (its project was closed, or too many runs were waiting)")?; let run = s.plugin_runs.remove(i); let mut result = result; - // What it adds to or changes on the map belongs on the map it ran on. - let on_map = matches!(&result, Ok(r) if matches!(r.output, Output::Layer(_) | Output::Selection { .. } | Output::Update { .. } | Output::Layers(_))); - if on_map && (s.project.active_map, s.map().name.as_str()) != (run.map.0, run.map.1.as_str()) { + // What it adds to or changes on the map belongs on the map it ran on: where it was, if it is + // still there, else the one map of its name (one before it may have been removed). + let on_map = matches!(&result, Ok(r) if match &r.output { + Output::Layer(_) | Output::Selection { .. } | Output::Update { .. } => true, + Output::Layers(out) => out.iter().any(|o| !matches!(o, rustgis_analysis::OutputLayer::Table(_))), + _ => false, + }); + let named = |i: usize| s.project.maps.get(i).is_some_and(|m| m.name == run.map.1); + let ran_on = if named(run.map.0) { + Some(run.map.0) + } else { + let mut same = (0..s.project.maps.len()).filter(|&i| named(i)); + same.next().filter(|_| same.next().is_none()) + }; + if on_map && ran_on != Some(s.project.active_map) { result = Err(format!("{} ran on the map '{}', which is no longer the active one: open it and run the tool again", run.name, run.map.1)); } + // A selection's rows of a streamed layer read whole are found by the name it was read under, + // whatever the layer is called now. + let read_as = output_layer(&result); fit_to_changes(s, &mut result, &run.name, &run.read); - fit_to_windows(s, &mut result, full_ids.as_ref().unwrap_or(&run.full_ids)); + let mut full_ids = full_ids.unwrap_or(run.full_ids); + if let (Some(was), Some(now)) = (read_as, output_layer(&result)) + && was != now + && let Some(ids) = full_ids.remove(&was) + { + full_ids.insert(now, ids); + } + fit_to_windows(s, &mut result, &full_ids); s.pending.push(crate::PendingRun { id, tool: run.name, @@ -509,6 +531,11 @@ fn fit_to_changes( *res = Err(format!("{layer} was removed while {tool} ran")); return; }; + // Another layer with its id (on a map that took the name of the one it ran on) isn't it. + if now.dataset.source != was.source { + *res = Err(format!("{layer} was removed while {tool} ran")); + return; + } layer.clone_from(&now.name); // A streamed layer's rows follow the view: only that it is still there is checked. let Some(dataset) = &was.dataset else { return }; @@ -524,6 +551,14 @@ fn fit_to_changes( } } +/// The layer a result selects in or updates, by lowercase name. +fn output_layer(res: &Result) -> Option { + match &res.as_ref().ok()?.output { + Output::Selection { layer, .. } | Output::Update { layer, .. } => Some(layer.to_ascii_lowercase()), + _ => None, + } +} + /// A layer a background run read, to tell whether it changed while the run went on. pub(crate) struct Read { /// The layer, by id: renamed perhaps, but not another of the same name. @@ -532,11 +567,13 @@ pub(crate) struct Read { /// however large the layer, and any edit since (which copies the rows held here too) shows. /// `None` for a streamed layer, whose rows follow the view. dataset: Option, + /// Where its data comes from: a layer with the same id and another source is another layer. + source: Option, } impl Read { fn of(l: &rustgis_map::Layer) -> Self { - Read { id: l.id, dataset: (!l.is_streamed()).then(|| l.dataset.clone()) } + Read { id: l.id, dataset: (!l.is_streamed()).then(|| l.dataset.clone()), source: l.dataset.source.clone() } } /// Whether `now` is the dataset `was` as it was read: the same rows in memory, and the same diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index a8ac2acfd0..f4f3ac0acb 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -23830,3 +23830,66 @@ fn built_in_tools_run_off_the_session() { tool_abandon(&mut s, run.id); assert!(!tool_waiting(&s, run.id)); } + +/// A background selection on a streamed layer renamed while it ran selects the features it chose +/// where they are in the layer's window; a run whose map moved (one before it was deleted) still +/// lands on it. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn background_runs_follow_renamed_streamed_layers_and_moved_maps() { + use crate::cmd::gp::{tool_complete, tool_start}; + use rustgis_analysis::control::Control; + let path = grid_fgb("bgrenamed", 50); + let mut s = Session::new(Project::default()); + s.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap(); + // Only a corner is in view: the window holds part of the source, in its own order. + let v = view_of(&mut s, rustgis_geom::Extent::new(0.0, 0.0, 20.0, 20.0)); + crate::render::render_png(s.map_mut(), &v).unwrap(); + assert!(s.map().layers[0].dataset.features.len() < 2500, "only part of the source is in the window"); + let run = tool_start(&mut s, "selectByAttributes", &json!({"input": "bgrenamed", "where": "x > 0 AND x < 20"})).unwrap(); + s.run("layer.rename", &json!({"layer": "bgrenamed", "name": "Renamed"})).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + let l = s.map().layer_by_name("Renamed").unwrap(); + let x = |f: &rustgis_data::Feature| f.attrs.get(1).and_then(|v| v.as_f64()).unwrap_or(f64::NAN); + let mut selected: Vec = l.selection.iter().map(|&r| l.dataset.features[r].id).collect(); + let mut expected: Vec = l.dataset.features.iter().filter(|f| x(f) > 0.0 && x(f) < 20.0).map(|f| f.id).collect(); + selected.sort_unstable(); + expected.sort_unstable(); + assert!(!expected.is_empty() && selected == expected, "{selected:?} vs {expected:?}"); + let _ = std::fs::remove_file(path); + // A map deleted before the run's own map moves it up: the result still lands there. + let mut s = world(); + s.project.maps.insert(0, rustgis_map::Map::new("Before")); + s.project.active_map = 1; + let layers = s.map().layers.len(); + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + s.run("map.remove", &json!({"name": "Before"})).unwrap(); + assert_eq!(s.project.active_map, 0); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + assert_eq!(s.map().layers.len(), layers + 1); + // Its map deleted and another given its name, holding a layer of the same id from another + // source: that isn't the layer it read, and its selection doesn't land there. + let path = grid_fgb("bgother", 5); + let mut s = world(); + s.run("map.new", &json!({"name": "Ran"})).unwrap(); + s.project.active_map = s.project.maps.iter().position(|m| m.name == "Ran").unwrap(); + s.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true, "name": "Points"})).unwrap(); + let read_id = s.map().layer_by_name("Points").unwrap().id; + let run = tool_start(&mut s, "selectByAttributes", &json!({"input": "Points", "where": "i >= 0"})).unwrap(); + s.run("map.remove", &json!({"name": "Ran"})).unwrap(); + s.run("map.new", &json!({"name": "Ran"})).unwrap(); + s.project.active_map = s.project.maps.iter().position(|m| m.name == "Ran").unwrap(); + let other = grid_fgb("bgother2", 5); + s.run("layer.addData", &json!({"path": other.display().to_string(), "stream": true, "name": "Points"})).unwrap(); + assert_eq!(s.map().layer_by_name("Points").unwrap().id, read_id, "the same id, on a map of the same name"); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert!(e.contains("was removed"), "{e}"); + assert!(s.map().layer_by_name("Points").unwrap().selection.is_empty()); + let _ = (std::fs::remove_file(path), std::fs::remove_file(other)); +} From 4c4ff3f8decbc365febee13dcf1a05fa77ca1bce Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Mon, 2 Nov 2026 15:53:33 +0100 Subject: [PATCH 5/7] fix(gp): a background run's selection follows a re-read window, its map survives a rename - A streamed layer's window as the run read it is kept, so a selection of it is remapped by feature id when the window is read again meanwhile, and a layer loaded whole and edited during the run refuses the update that would discard the edit. A selection of a layer read whole is still fitted by fit_to_windows, loaded since or not. - A layer relinked to another table of its file counts as removed. - Features a remapped selection can no longer find are reported. - map.rename updates the map name recorded by running tools. - Doc comment and test comment fixes; the pane test checks Record Task. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/gp.rs | 61 +++++++++++++++++------ crates/engine/src/cmd/navigate.rs | 1 + crates/engine/src/tests.rs | 83 ++++++++++++++++++++++++++++++- crates/ui-egui/src/lib.rs | 3 +- crates/ui-egui/src/panes.rs | 4 ++ docs/user-guide/analysis.md | 8 +-- 6 files changed, 140 insertions(+), 20 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 3d133f96cb..d1c430e2cb 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -390,8 +390,8 @@ fn complete( // A selection's rows of a streamed layer read whole are found by the name it was read under, // whatever the layer is called now. let read_as = output_layer(&result); - fit_to_changes(s, &mut result, &run.name, &run.read); let mut full_ids = full_ids.unwrap_or(run.full_ids); + fit_to_changes(s, &mut result, &run.name, &run.read, &full_ids); if let (Some(was), Some(now)) = (read_as, output_layer(&result)) && was != now && let Some(ids) = full_ids.remove(&was) @@ -514,39 +514,55 @@ pub fn tool_complete(s: &mut Session, id: u64, done: ToolDone) -> Result<(), Str } /// 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 selects the same features (by id) -/// where they now are; and an update of a layer that changed, which would discard those edits, -/// or of one removed, is refused. +/// 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 +/// that changed, which would discard those edits, or of one removed, is refused. A streamed +/// layer it read whole is fitted by [`fit_to_windows`]. fn fit_to_changes( s: &Session, res: &mut Result, tool: &str, read: &std::collections::HashMap, + full_ids: &FullIds, ) { let Ok(r) = res else { return }; let (Output::Selection { layer, .. } | Output::Update { layer, .. }) = &mut r.output else { return }; - let Some(was) = read.get(&layer.to_ascii_lowercase()) else { return }; + let key = layer.to_ascii_lowercase(); + let Some(was) = read.get(&key) else { return }; // The layer it read, by id: renamed perhaps, but not another of the same name. let Some(now) = s.map().layer(was.id) else { *res = Err(format!("{layer} was removed while {tool} ran")); return; }; - // Another layer with its id (on a map that took the name of the one it ran on) isn't it. - if now.dataset.source != was.source { + // Another layer with its id (on a map that took the name of the one it ran on), or the layer + // relinked to another table of its file, isn't it. + if now.dataset.source != was.source || now.linked_table() != was.table.as_deref() { *res = Err(format!("{layer} was removed while {tool} ran")); return; } layer.clone_from(&now.name); - // A streamed layer's rows follow the view: only that it is still there is checked. - let Some(dataset) = &was.dataset else { return }; - if Read::unchanged(dataset, &now.dataset) { + if Read::unchanged(&was.dataset, &now.dataset) { return; } + // A selection of what it read whole (rows of the whole source) is fitted to the layer later, + // loaded since or not. A layer still streamed has rows that follow the view: an update of it + // is checked there too. + let streamed = was.streamed && now.is_streamed(); match &mut r.output { + Output::Selection { .. } if was.streamed && full_ids.contains_key(&key) => {} Output::Selection { rows, .. } => { - let ids: std::collections::HashSet = rows.iter().filter_map(|r| dataset.features.get(*r)).map(|f| f.id).collect(); + let ids: std::collections::HashSet = rows.iter().filter_map(|r| was.dataset.features.get(*r)).map(|f| f.id).collect(); *rows = now.dataset.features.iter().enumerate().filter(|(_, f)| ids.contains(&f.id)).map(|(i, _)| i).collect(); + let gone = ids.len().saturating_sub(rows.len()); + if gone > 0 { + let place = if now.is_streamed() { "outside the part of it read for the view" } else { "deleted" }; + r.messages.push(format!( + "{gone} of the features {tool} selected in {} are no longer there ({place} while it ran) and aren't selected", + now.name + )); + } } + _ if streamed => {} _ => *res = Err(format!("{} changed while {tool} ran; run it again", now.name)), } } @@ -565,15 +581,25 @@ pub(crate) struct Read { id: u32, /// Its dataset as the run read it: a copy that shares the rows, so taking it costs nothing /// however large the layer, and any edit since (which copies the rows held here too) shows. - /// `None` for a streamed layer, whose rows follow the view. - dataset: Option, + /// For a streamed layer, the window in view then. + dataset: Dataset, + /// Whether it was read in place from its file, its rows following the view. + streamed: bool, /// Where its data comes from: a layer with the same id and another source is another layer. source: Option, + /// The table of that file it reads: a repair can relink it to another. + table: Option, } impl Read { fn of(l: &rustgis_map::Layer) -> Self { - Read { id: l.id, dataset: (!l.is_streamed()).then(|| l.dataset.clone()), source: l.dataset.source.clone() } + Read { + id: l.id, + dataset: l.dataset.clone(), + streamed: l.is_streamed(), + source: l.dataset.source.clone(), + table: l.linked_table().map(String::from), + } } /// Whether `now` is the dataset `was` as it was read: the same rows in memory, and the same @@ -620,6 +646,13 @@ fn fit_to_windows(s: &Session, res: &mut Result CmdResult { let id = p.get("id").and_then(Value::as_u64).ok_or("id is required")?; diff --git a/crates/engine/src/cmd/navigate.rs b/crates/engine/src/cmd/navigate.rs index 79de67e9e5..a559aaf444 100644 --- a/crates/engine/src/cmd/navigate.rs +++ b/crates/engine/src/cmd/navigate.rs @@ -288,6 +288,7 @@ fn duplicate_map(s: &mut Session, p: &Value) -> CmdResult { /// Points layout map frames, presentation map pages and offline areas at a renamed map (#287), /// so they keep finding it. fn follow_renamed_map(s: &mut Session, old: &str, new: &str) { + super::gp::follow_renamed_map(s, old, new); let renamed = |m: &mut String| { if m.eq_ignore_ascii_case(old) { *m = new.to_string(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index f4f3ac0acb..fd6002bb8b 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -23756,8 +23756,9 @@ fn built_in_tools_run_off_the_session() { assert!(h.ok && h.tool == "Buffer" && h.params["input"] == "Cities", "{h:?}"); s.run("edit.undo", &Value::Null).unwrap(); assert_eq!(s.map().layers.len(), before); - // Cancelled while it runs, or before: it fails, saying so, and adds nothing (a failed command - // leaves the project as it was, as a failed gp.run does). + // Cancelled before it starts (a cancel while it runs is the analysis crate's test): it fails, + // saying so, and adds nothing (a failed command leaves the project as it was, as a failed + // gp.run does). let mut s = world(); let layers = s.map().layers.len(); let run = tool_start(&mut s, "idwInterpolation", &json!({"input": "Cities", "field": "pop_max", "pixelSize": 5.0})).unwrap(); @@ -23893,3 +23894,81 @@ fn background_runs_follow_renamed_streamed_layers_and_moved_maps() { assert!(s.map().layer_by_name("Points").unwrap().selection.is_empty()); let _ = (std::fs::remove_file(path), std::fs::remove_file(other)); } + +/// A background run's result lands on its map renamed while it ran; its selection of a streamed +/// layer whose window was read again meanwhile selects the same features, saying how many left +/// it; and its update of a streamed layer loaded whole and edited meanwhile is refused. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn background_runs_follow_a_renamed_map_a_moved_window_and_a_loaded_layer() { + use crate::cmd::gp::{tool_complete, tool_start}; + use rustgis_analysis::control::Control; + let mut s = world(); + let layers = s.map().layers.len(); + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + s.run("map.rename", &json!({"name": "Renamed while it ran"})).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + assert_eq!(s.map().layers.len(), layers + 1); + // A selection in the window (so the tool reads the window), then the view moves. + let path = grid_fgb("bgwindow", 50); + let mut s = Session::new(Project::default()); + s.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap(); + let v = view_of(&mut s, rustgis_geom::Extent::new(0.0, 0.0, 20.0, 20.0)); + crate::render::render_png(s.map_mut(), &v).unwrap(); + let ids = |s: &Session| -> Vec { s.map().layers[0].dataset.features.iter().map(|f| f.id).collect() }; + let x = |f: &rustgis_data::Feature| f.attrs.get(1).and_then(|v| v.as_f64()).unwrap_or(f64::NAN); + let picked: std::collections::HashSet = + s.map().layers[0].dataset.features.iter().filter(|f| x(f) > 0.0 && x(f) < 20.0).map(|f| f.id).collect(); + let before = ids(&s); + let n = before.len(); + s.map_mut().layers[0].selection = (0..n).collect(); + let run = tool_start(&mut s, "selectByAttributes", &json!({"input": "bgwindow", "where": "x > 0 AND x < 20"})).unwrap(); + // The view moved: the window, read again, holds part of what it did, in another order. + let l = &mut s.map_mut().layers[0]; + l.dataset.features = l.dataset.features.iter().rev().filter(|f| f.id % 3 != 0).cloned().collect(); + l.selection.clear(); + let after = ids(&s); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + let l = &s.map().layers[0]; + let mut selected: Vec = l.selection.iter().map(|&r| l.dataset.features[r].id).collect(); + let mut expected: Vec = after.iter().copied().filter(|i| picked.contains(i)).collect(); + selected.sort_unstable(); + expected.sort_unstable(); + assert!(!expected.is_empty() && expected.len() < picked.len(), "{} of {}", expected.len(), picked.len()); + assert_eq!(selected, expected); + let said = &s.project.gp_history.last().unwrap().messages; + assert!(said.iter().any(|m| m.contains("no longer there")), "{said:?}"); + // Streamed and read whole, then loaded whole while the tool ran: its selection, rows of the + // whole source, lands on the same features. + s.map_mut().layers[0].selection.clear(); + let mut t = Session::new(Project::default()); + t.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap(); + let v = view_of(&mut t, rustgis_geom::Extent::new(0.0, 0.0, 20.0, 20.0)); + crate::render::render_png(t.map_mut(), &v).unwrap(); + let run = tool_start(&mut t, "selectByAttributes", &json!({"input": "bgwindow", "where": "i < 100"})).unwrap(); + t.run("layer.loadAll", &json!({"layer": t.map().layers[0].id})).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut t, id, done).unwrap(); + t.run("gp.finish", &json!({"id": id})).unwrap(); + let l = &t.map().layers[0]; + assert!(!l.is_streamed()); + let i = |f: &rustgis_data::Feature| f.attrs.first().and_then(|v| v.as_f64()).unwrap_or(f64::NAN); + assert_eq!(l.selection.len(), 100); + assert!(l.selection.iter().all(|&r| i(&l.dataset.features[r]) < 100.0)); + // Streamed and read whole, then loaded whole and edited while the tool ran: its update + // would discard the edit. + let run = tool_start(&mut s, "calculateGeometry", &json!({"input": "bgwindow"})).unwrap(); + s.run("layer.loadAll", &json!({"layer": s.map().layers[0].id})).unwrap(); + s.run("edit.enable", &json!({"on": true})).unwrap(); + s.run("edit.setValue", &json!({"layer": "bgwindow", "row": 0, "field": "i", "value": -1})).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert!(e.contains("changed while"), "{e}"); + assert!(s.map().layers[0].dataset.features.iter().any(|f| f.attrs.first().and_then(|v| v.as_f64()) == Some(-1.0))); + let _ = std::fs::remove_file(path); +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 06b6fb0b68..e223f2129a 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -4289,7 +4289,7 @@ mod record_tests { assert_eq!(app.ws.recording.as_ref().map(Vec::len), Some(1)); } - /// A step keeping a plain `key` is recorded, and that is said once a recording. + /// A background run's `gp.finish` is recorded as the `gp.run` it finished. #[test] fn a_tool_finished_in_the_background_is_recorded_as_its_run() { let mut app = RustGisApp::with_sample(); @@ -4313,6 +4313,7 @@ mod record_tests { ); } + /// A step keeping a plain `key` is recorded, and that is said once a recording. #[test] fn a_kept_key_is_said_once() { let mut app = RustGisApp::with_sample(); diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index d94dd6ee97..0c8383e3c9 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -6444,6 +6444,7 @@ mod tests { let _ = app.run("ui.gp.open", json!({"tool": "buffer"})); app.gp.values.insert("input", "Cities".into()); app.gp.values.insert("distance", "50".into()); + app.ws.recording = Some(Vec::new()); let layers = app.session.map().layers.len(); let mut fonts = false; let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(420.0, 1000.0)).build_ui_state( @@ -6472,6 +6473,9 @@ mod tests { assert_eq!(app.gp.last.as_ref().map(|l| l.0), Some(true), "{:?}", app.gp.last); assert_eq!(app.session.map().layers.len(), layers + 1); assert_eq!(app.session.project.gp_history.last().map(|r| (r.ok, r.tool.as_str())), Some((true, "Buffer"))); + // Recorded once, as its run. + let steps = app.ws.recording.as_deref().unwrap_or_default(); + assert_eq!(steps.iter().map(|s| s.command.as_str()).collect::>(), ["gp.run"]); } #[test] diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 51632104af..4b0890f3f9 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -62,9 +62,11 @@ In the desktop app a tool runs in the background, so the map and the rest of the use. While it runs, the Tools pane shows its name, a **Cancel** button and, for the tools that count their work (Viewshed, IDW Interpolation, Kriging, Kernel Density, Buffer, Select Layer By Location and others), a progress bar. One tool runs at a time. A cancelled run adds nothing, and -a tool that counts its work says how far it came. Edits made to its input layers while it runs are kept: a selection it makes -selects the same features where they now are, and a result that would replace an edited layer is -refused. Opening another project drops the run. Scripts, models, the AI Assistant, the command +its message says how far through its work the tool was. Edits made to its input layers while it +runs are kept: a selection it makes selects the same features where they now are (and says how +many are gone), and a result that would replace an edited layer is refused. Renaming the map +while it runs is fine; switching to another map refuses the result. Opening another project +drops the run. Scripts, models, the AI Assistant, the command line and the web app wait for each tool to finish. From the command line or a script (`gp.run`), a built-in tool's parameter with a list of choices, such as Buffer's From d304a9c38c73ce14a83e434ebad1212a1f747698 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Mon, 2 Nov 2026 16:21:04 +0100 Subject: [PATCH 6/7] fix(gp): a background run finds its map by identity; the running tool stays in the tool list - Map gets a process-local instance_id, like Layer: never saved, kept by undo snapshots, renewed for a duplicated, opened, imported or synced copy. A run records it, so a map renamed (and the rename undone) keeps its result, and a map made with a deleted one's name doesn't get it. Replaces the map.rename hook. - offline.sync gives the synced offline map and its layers ids of their own. - fit_to_windows says how many selected features are outside the window. - The run status (spinner, progress, Cancel) is drawn in the tool list too, so a run can be cancelled after Back; plugin runs as well. - An Update of a layer loaded whole or replaced during the run says so. - Docs: what a map switch refuses; the pane test checks the recorded step. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/gp.rs | 44 ++++++++-------- crates/engine/src/cmd/items.rs | 1 + crates/engine/src/cmd/navigate.rs | 2 +- crates/engine/src/cmd/workspace.rs | 6 +++ crates/engine/src/tests.rs | 38 +++++++++++++- crates/map/src/lib.rs | 14 ++++++ crates/ui-egui/src/panes.rs | 80 +++++++++++++++++++++++------- docs/user-guide/analysis.md | 7 +-- 8 files changed, 145 insertions(+), 47 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index d1c430e2cb..b6f766bb63 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -290,8 +290,8 @@ pub(crate) struct StartedRun { full_ids: FullIds, /// The layers it reads, as it read them, by lowercase name. read: std::collections::HashMap, - /// The map it ran on: its place among the project's maps, and its name. - map: (usize, String), + /// The map it ran on: its identity ([`rustgis_map::Map::instance_id`]), and its name. + map: (u64, String), } /// The feature ids of the streamed layers a tool read whole, by lowercase name: what its @@ -318,7 +318,7 @@ pub fn plugin_start(s: &mut Session, tool: &str, params: &Value) -> Result true, Output::Layers(out) => out.iter().any(|o| !matches!(o, rustgis_analysis::OutputLayer::Table(_))), _ => false, }); - let named = |i: usize| s.project.maps.get(i).is_some_and(|m| m.name == run.map.1); - let ran_on = if named(run.map.0) { - Some(run.map.0) - } else { - let mut same = (0..s.project.maps.len()).filter(|&i| named(i)); - same.next().filter(|_| same.next().is_none()) - }; + let mut same = (0..s.project.maps.len()).filter(|&i| s.project.maps.get(i).is_some_and(|m| m.instance_id() == run.map.0)); + let ran_on = same.next().filter(|_| same.next().is_none()); if on_map && ran_on != Some(s.project.active_map) { - result = - Err(format!("{} ran on the map '{}', which is no longer the active one: open it and run the tool again", run.name, run.map.1)); + result = Err(format!( + "{} ran on the map '{}', which was deleted or is no longer the active one: open it and run the tool again", + run.name, run.map.1 + )); } // A selection's rows of a streamed layer read whole are found by the name it was read under, // whatever the layer is called now. @@ -484,7 +481,7 @@ pub fn tool_start(s: &mut Session, tool: &str, params: &Value) -> Result MAX_ASYNC_RUNS { @@ -537,7 +534,7 @@ fn fit_to_changes( // Another layer with its id (on a map that took the name of the one it ran on), or the layer // relinked to another table of its file, isn't it. if now.dataset.source != was.source || now.linked_table() != was.table.as_deref() { - *res = Err(format!("{layer} was removed while {tool} ran")); + *res = Err(format!("{layer} was removed or replaced while {tool} ran")); return; } layer.clone_from(&now.name); @@ -563,6 +560,8 @@ fn fit_to_changes( } } _ if streamed => {} + // Its rows since then can't be told from edits: they are another copy of the source. + _ if was.streamed => *res = Err(format!("{} was loaded whole or replaced while {tool} ran; run it again", now.name)), _ => *res = Err(format!("{} changed while {tool} ran; run it again", now.name)), } } @@ -635,6 +634,12 @@ fn fit_to_windows(s: &Session, res: &mut Result = rows.iter().filter_map(|r| full.get(*r)).copied().collect(); *rows = l.dataset.features.iter().enumerate().filter(|(_, f)| ids.contains(&f.id)).map(|(i, _)| i).collect(); + let outside = ids.len().saturating_sub(rows.len()); + if outside > 0 { + r.messages.push(format!( + "{outside} of the features selected in {layer} are outside the part of it read for the view, and aren't selected" + )); + } } if let Ok(r) = res && let Output::Update { layer, .. } = &r.output @@ -646,13 +651,6 @@ fn fit_to_windows(s: &Session, res: &mut Result CmdResult { let id = p.get("id").and_then(Value::as_u64).ok_or("id is required")?; diff --git a/crates/engine/src/cmd/items.rs b/crates/engine/src/cmd/items.rs index ad7d6794e1..aeda61248f 100644 --- a/crates/engine/src/cmd/items.rs +++ b/crates/engine/src/cmd/items.rs @@ -460,6 +460,7 @@ fn import_maps(s: &mut Session, p: &Value) -> CmdResult { m.name = format!("{base} ({i})"); } names.push(m.name.clone()); + m.renew_instance_id(); s.project.maps.push(m); } // Repair what was read (rasters reopen, cameras clamp); the sources that read S3 wait for diff --git a/crates/engine/src/cmd/navigate.rs b/crates/engine/src/cmd/navigate.rs index a559aaf444..8d6041c1e0 100644 --- a/crates/engine/src/cmd/navigate.rs +++ b/crates/engine/src/cmd/navigate.rs @@ -269,6 +269,7 @@ fn duplicate_map(s: &mut Session, p: &Value) -> CmdResult { let mut m = s.project.maps.get(i).ok_or("no map")?.clone(); m.name = super::copy_name(ps(p, "name"), &m.name, |n| s.project.maps.iter().any(|x| x.name.eq_ignore_ascii_case(n))); m.label_lock = None; + m.renew_instance_id(); for l in &mut m.layers { l.renew_instance_id(); l.selection.clear(); @@ -288,7 +289,6 @@ fn duplicate_map(s: &mut Session, p: &Value) -> CmdResult { /// Points layout map frames, presentation map pages and offline areas at a renamed map (#287), /// so they keep finding it. fn follow_renamed_map(s: &mut Session, old: &str, new: &str) { - super::gp::follow_renamed_map(s, old, new); let renamed = |m: &mut String| { if m.eq_ignore_ascii_case(old) { *m = new.to_string(); diff --git a/crates/engine/src/cmd/workspace.rs b/crates/engine/src/cmd/workspace.rs index e7d2ad31b7..5b707550d2 100644 --- a/crates/engine/src/cmd/workspace.rs +++ b/crates/engine/src/cmd/workspace.rs @@ -268,6 +268,7 @@ pub fn commands() -> Vec { } else { let mut m = a.data.clone(); m.name = map_name; + m.renew_instance_id(); // Reading a project checks only maps' layers, not offline snapshots', so their // selections are put right here. for l in &mut m.layers { @@ -730,6 +731,11 @@ fn offline_sync(s: &mut Session, p: &Value) -> CmdResult { let name = m.name.clone(); *m = fresh.clone(); m.name = name; + // A copy of the source map: a map, and layers, of its own. + m.renew_instance_id(); + for l in &mut m.layers { + l.renew_instance_id(); + } } if let Some(a) = s.project.offline_areas.get_mut(ai) { a.data = fresh; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index fd6002bb8b..5ecfc04567 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -6917,6 +6917,15 @@ fn offline_sync_rejects_incompatible_fields_and_preserves_safe_refreshes() { let offline = &s.project.maps.iter().find(|m| m.name == "Fieldwork (offline)").unwrap().layer_by_name("Assets").unwrap().dataset; assert_eq!(offline.fields, source.fields, "{mode}"); assert_eq!(offline.features, source.features, "{mode}"); + // The synced copy is a map, with layers, of its own (a background run finds its map by it). + let ids: Vec = s.project.maps.iter().map(|m| m.instance_id()).collect(); + assert!(ids.iter().enumerate().all(|(i, a)| ids[..i].iter().all(|b| a != b)), "{mode}: {ids:?}"); + let offline_map = s.project.maps.iter().find(|m| m.name == "Fieldwork (offline)").unwrap(); + assert_ne!( + offline_map.layer_by_name("Assets").unwrap().instance_id(), + s.map().layer_by_name("Assets").unwrap().instance_id(), + "{mode}" + ); } for mode in ["source_deleted", "geometry_only", "offline_added", "source_reordered", "source_type_changed", "empty_offline_schema"] { @@ -23859,6 +23868,9 @@ fn background_runs_follow_renamed_streamed_layers_and_moved_maps() { selected.sort_unstable(); expected.sort_unstable(); assert!(!expected.is_empty() && selected == expected, "{selected:?} vs {expected:?}"); + // What it selected outside the window isn't selected, and that is said. + let said = &s.project.gp_history.last().unwrap().messages; + assert!(said.iter().any(|m| m.contains("outside the part of it read for the view")), "{said:?}"); let _ = std::fs::remove_file(path); // A map deleted before the run's own map moves it up: the result still lands there. let mut s = world(); @@ -23890,7 +23902,7 @@ fn background_runs_follow_renamed_streamed_layers_and_moved_maps() { let (id, done) = (run.id, run.run(&Control::new())); tool_complete(&mut s, id, done).unwrap(); let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); - assert!(e.contains("was removed"), "{e}"); + assert!(e.contains("was deleted or is no longer the active one"), "{e}"); assert!(s.map().layer_by_name("Points").unwrap().selection.is_empty()); let _ = (std::fs::remove_file(path), std::fs::remove_file(other)); } @@ -23911,6 +23923,28 @@ fn background_runs_follow_a_renamed_map_a_moved_window_and_a_loaded_layer() { tool_complete(&mut s, id, done).unwrap(); s.run("gp.finish", &json!({"id": id})).unwrap(); assert_eq!(s.map().layers.len(), layers + 1); + // Renamed and the rename undone: the same map. + let layers = s.map().layers.len(); + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + s.run("map.rename", &json!({"name": "Renamed again"})).unwrap(); + s.run("edit.undo", &Value::Null).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + s.run("gp.finish", &json!({"id": id})).unwrap(); + assert_eq!(s.map().layers.len(), layers + 1); + // Deleted, and another map made with its name: the new layer doesn't land there. + s.run("map.new", &json!({"name": "Other"})).unwrap(); + let ran = s.project.maps[0].name.clone(); + s.project.active_map = 0; + let run = tool_start(&mut s, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + s.run("map.remove", &json!({"name": ran})).unwrap(); + s.run("map.new", &json!({"name": ran})).unwrap(); + s.project.active_map = s.project.maps.iter().position(|m| m.name == ran).unwrap(); + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); + assert!(e.contains("was deleted or is no longer the active one"), "{e}"); + assert!(s.map().layers.is_empty()); // A selection in the window (so the tool reads the window), then the view moves. let path = grid_fgb("bgwindow", 50); let mut s = Session::new(Project::default()); @@ -23968,7 +24002,7 @@ fn background_runs_follow_a_renamed_map_a_moved_window_and_a_loaded_layer() { let (id, done) = (run.id, run.run(&Control::new())); tool_complete(&mut s, id, done).unwrap(); let e = s.run("gp.finish", &json!({"id": id})).unwrap_err(); - assert!(e.contains("changed while"), "{e}"); + assert!(e.contains("loaded whole or replaced while"), "{e}"); assert!(s.map().layers[0].dataset.features.iter().any(|f| f.attrs.first().and_then(|v| v.as_f64()) == Some(-1.0))); let _ = std::fs::remove_file(path); } diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index 44454f706b..4ff1a98475 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -1244,6 +1244,9 @@ pub struct Map { pub body: String, #[serde(default)] next_layer_id: u32, + /// Process-local identity, kept by undo snapshots but never stored in a project. + #[serde(skip, default = "next_layer_instance")] + instance_id: u64, } fn earth() -> String { @@ -1350,9 +1353,20 @@ impl Map { time: None, body: earth(), next_layer_id: 1, + instance_id: next_layer_instance(), } } + /// Identifies this live map, whatever its name or place among the project's maps. + pub fn instance_id(&self) -> u64 { + self.instance_id + } + + /// Starts a distinct live map after copying one. Undo snapshots keep the original identity. + pub fn renew_instance_id(&mut self) { + self.instance_id = next_layer_instance(); + } + /// Every address the map keeps to read its data again (service groups and their layers' /// sources, tile layers and their dated addresses, terrain tiles, layers' dataset, streamed and /// raster sources, time-following Zarr stores). Local paths come too: callers judge each one. diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 0c8383e3c9..3f24ee2a2f 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -3322,7 +3322,14 @@ pub(crate) fn input_table<'a>(session: &'a Session, name: &str) -> Option<&'a ru fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { let Some(spec) = app.gp.tool.and_then(rustgis_engine::analysis::tool) else { - // Tool finder. + // Tool finder, under the tool running, if one is. + let running = app.gp.web_run.is_some() || app.gp.plugin_run.is_some(); + #[cfg(not(target_arch = "wasm32"))] + let running = running || app.gp.tool_run.is_some(); + if running { + run_status(app, ui, t); + ui.add_space(8.0); + } crate::widgets::search_field(ui, &mut app.gp.search, tl!("Find Tools")); ui.separator(); ui.add_space(4.0); @@ -3826,6 +3833,29 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } } }); + run_status(app, ui, t); + if let Some((ok, msgs)) = &app.gp.last { + ui.add_space(8.0); + let (fill, stroke, head) = + if *ok { (t.green.gamma_multiply(0.12), t.green, "Completed") } else { (t.red.gamma_multiply(0.12), t.red, "Failed") }; + egui::Frame::NONE + .fill(fill) + .stroke(Stroke::new(1.0, stroke)) + .corner_radius(CornerRadius::same(6)) + .inner_margin(egui::Margin::same(8)) + .show(ui, |ui| { + ui.set_width(ui.available_width()); + ui.label(egui::RichText::new(tl!(head)).font(semibold(12.5)).color(stroke)); + for m in msgs { + ui.label(egui::RichText::new(m).size(11.5)); + } + }); + } +} + +/// The tool running, if one is: in the tool's form and in the finder, so that it can be cancelled +/// from either. +fn run_status(app: &RustGisApp, ui: &mut Ui, t: &Tokens) { if app.gp.web_run.is_some() { ui.add_space(8.0); ui.horizontal(|ui| { @@ -3876,23 +3906,6 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { job.control.cancel(); } } - if let Some((ok, msgs)) = &app.gp.last { - ui.add_space(8.0); - let (fill, stroke, head) = - if *ok { (t.green.gamma_multiply(0.12), t.green, "Completed") } else { (t.red.gamma_multiply(0.12), t.red, "Failed") }; - egui::Frame::NONE - .fill(fill) - .stroke(Stroke::new(1.0, stroke)) - .corner_radius(CornerRadius::same(6)) - .inner_margin(egui::Margin::same(8)) - .show(ui, |ui| { - ui.set_width(ui.available_width()); - ui.label(egui::RichText::new(tl!(head)).font(semibold(12.5)).color(stroke)); - for m in msgs { - ui.label(egui::RichText::new(m).size(11.5)); - } - }); - } } /// Picks a file for a tool's file parameter: its path on the desktop, and in the browser, which @@ -6476,6 +6489,37 @@ mod tests { // Recorded once, as its run. let steps = app.ws.recording.as_deref().unwrap_or_default(); assert_eq!(steps.iter().map(|s| s.command.as_str()).collect::>(), ["gp.run"]); + assert_eq!((&steps[0].params["tool"], &steps[0].params["params"]["input"]), (&json!("buffer"), &json!("Cities"))); + } + + /// The tool running stays in view, with its Cancel button, when the pane goes back to the + /// list of tools. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_tool_running_in_the_background_can_be_cancelled_from_the_tool_list() { + use egui_kittest::kittest::Queryable; + let mut app = RustGisApp::with_sample(); + let run = rustgis_engine::cmd::gp::tool_start(&mut app.session, "buffer", &json!({"input": "Cities", "distance": 50})).unwrap(); + let control = rustgis_engine::analysis::control::Control::new(); + app.gp.tool_run = Some(ToolJob { id: run.id, tool: run.tool, slot: crate::Slot::default(), control: control.clone() }); + app.gp.tool = None; + let mut fonts = false; + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(420.0, 1000.0)).build_ui_state( + move |ui, app: &mut RustGisApp| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + return; + } + geoprocessing(app, ui, &Tokens::light()); + }, + app, + ); + // Its spinner keeps repainting: steps, not run. + h.run_steps(3); + h.get_by_label("Cancel").click(); + h.run_steps(2); + assert!(control.is_cancelled()); } #[test] diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 4b0890f3f9..3d3c44379b 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -64,9 +64,10 @@ count their work (Viewshed, IDW Interpolation, Kriging, Kernel Density, Buffer, Location and others), a progress bar. One tool runs at a time. A cancelled run adds nothing, and its message says how far through its work the tool was. Edits made to its input layers while it runs are kept: a selection it makes selects the same features where they now are (and says how -many are gone), and a result that would replace an edited layer is refused. Renaming the map -while it runs is fine; switching to another map refuses the result. Opening another project -drops the run. Scripts, models, the AI Assistant, the command +many are gone), and a result that would replace an edited layer, or one loaded whole meanwhile, +is refused. Renaming the map while it runs is fine; after switching to another map, a result +that adds to or changes the map is refused (one made only of tables or a file is still made). Opening another +project drops the run. Scripts, models, the AI Assistant, the command line and the web app wait for each tool to finish. From the command line or a script (`gp.run`), a built-in tool's parameter with a list of choices, such as Buffer's From 568b8122786e1b18177950907a47b51e8ce724f2 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Mon, 2 Nov 2026 16:35:49 +0100 Subject: [PATCH 7/7] fix(gp): say whether a selection's missing features left the view or were deleted - fit_to_windows words the count by the layer's state: still streamed, outside the window; loaded whole since, deleted while the tool ran. - Docs: only a tool that counts its work says how far it got when cancelled. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/gp.rs | 10 +++++----- crates/engine/src/tests.rs | 17 ++++++++++++----- docs/user-guide/analysis.md | 14 +++++++------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index b6f766bb63..c18cf994ed 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -634,11 +634,11 @@ fn fit_to_windows(s: &Session, res: &mut Result = rows.iter().filter_map(|r| full.get(*r)).copied().collect(); *rows = l.dataset.features.iter().enumerate().filter(|(_, f)| ids.contains(&f.id)).map(|(i, _)| i).collect(); - let outside = ids.len().saturating_sub(rows.len()); - if outside > 0 { - r.messages.push(format!( - "{outside} of the features selected in {layer} are outside the part of it read for the view, and aren't selected" - )); + let gone = ids.len().saturating_sub(rows.len()); + if gone > 0 { + // Still streamed: outside the window. Loaded whole since: deleted meanwhile. + let place = if l.is_streamed() { "are outside the part of it read for the view" } else { "were deleted while it ran" }; + r.messages.push(format!("{gone} of the features selected in {layer} {place}, and aren't selected")); } } if let Ok(r) = res diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 5ecfc04567..c7a057fb2b 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -23976,8 +23976,8 @@ fn background_runs_follow_a_renamed_map_a_moved_window_and_a_loaded_layer() { assert_eq!(selected, expected); let said = &s.project.gp_history.last().unwrap().messages; assert!(said.iter().any(|m| m.contains("no longer there")), "{said:?}"); - // Streamed and read whole, then loaded whole while the tool ran: its selection, rows of the - // whole source, lands on the same features. + // Streamed and read whole, then loaded whole while the tool ran, and one of the features it + // selects deleted: its selection, rows of the whole source, lands on the others, and says so. s.map_mut().layers[0].selection.clear(); let mut t = Session::new(Project::default()); t.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap(); @@ -23985,14 +23985,21 @@ fn background_runs_follow_a_renamed_map_a_moved_window_and_a_loaded_layer() { crate::render::render_png(t.map_mut(), &v).unwrap(); let run = tool_start(&mut t, "selectByAttributes", &json!({"input": "bgwindow", "where": "i < 100"})).unwrap(); t.run("layer.loadAll", &json!({"layer": t.map().layers[0].id})).unwrap(); + // One of the features it selects is deleted meanwhile. + let i = |f: &rustgis_data::Feature| f.attrs.first().and_then(|v| v.as_f64()).unwrap_or(f64::NAN); + let first = t.map().layers[0].dataset.features.iter().position(|f| i(f) == 0.0).unwrap(); + t.map_mut().layers[0].selection = vec![first]; + t.run("edit.enable", &json!({"on": true})).unwrap(); + t.run("edit.delete", &Value::Null).unwrap(); let (id, done) = (run.id, run.run(&Control::new())); tool_complete(&mut t, id, done).unwrap(); t.run("gp.finish", &json!({"id": id})).unwrap(); let l = &t.map().layers[0]; assert!(!l.is_streamed()); - let i = |f: &rustgis_data::Feature| f.attrs.first().and_then(|v| v.as_f64()).unwrap_or(f64::NAN); - assert_eq!(l.selection.len(), 100); - assert!(l.selection.iter().all(|&r| i(&l.dataset.features[r]) < 100.0)); + assert_eq!(l.selection.len(), 99); + assert!(l.selection.iter().all(|&r| i(&l.dataset.features[r]) < 100.0 && i(&l.dataset.features[r]) != 0.0)); + let said = &t.project.gp_history.last().unwrap().messages; + assert!(said.iter().any(|m| m.contains("1 of the features selected in bgwindow were deleted while it ran")), "{said:?}"); // Streamed and read whole, then loaded whole and edited while the tool ran: its update // would discard the edit. let run = tool_start(&mut s, "calculateGeometry", &json!({"input": "bgwindow"})).unwrap(); diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 3d3c44379b..3501110ba0 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -62,13 +62,13 @@ In the desktop app a tool runs in the background, so the map and the rest of the use. While it runs, the Tools pane shows its name, a **Cancel** button and, for the tools that count their work (Viewshed, IDW Interpolation, Kriging, Kernel Density, Buffer, Select Layer By Location and others), a progress bar. One tool runs at a time. A cancelled run adds nothing, and -its message says how far through its work the tool was. Edits made to its input layers while it -runs are kept: a selection it makes selects the same features where they now are (and says how -many are gone), and a result that would replace an edited layer, or one loaded whole meanwhile, -is refused. Renaming the map while it runs is fine; after switching to another map, a result -that adds to or changes the map is refused (one made only of tables or a file is still made). Opening another -project drops the run. Scripts, models, the AI Assistant, the command -line and the web app wait for each tool to finish. +for a tool that counts its work, its message says how far through its current step it got. Edits +made to its input layers while it runs are kept: a selection it makes selects the same features +where they now are (and says how many are gone), and a result that would replace an edited +layer, or one loaded whole meanwhile, is refused. Renaming the map while it runs is fine; after +switching to another map, a result that adds to or changes the map is refused (one made only of +tables or a file is still made). Opening another project drops the run. Scripts, models, the AI +Assistant, the command line and the web app wait for each tool to finish. From the command line or a script (`gp.run`), a built-in tool's parameter with a list of choices, such as Buffer's units, takes one of them in any letter case (`"meters"` for `Meters`); any other value is an error