diff --git a/crates/engine/src/cmd/catalogs.rs b/crates/engine/src/cmd/catalogs.rs index 18b4cc1104..2ea856d91c 100644 --- a/crates/engine/src/cmd/catalogs.rs +++ b/crates/engine/src/cmd/catalogs.rs @@ -11,7 +11,7 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ C::new("overture.add", "Add Overture Maps Data", "Home › Add › Web Data", overture_add).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( - r#"{"theme": "buildings"|"places"|"transportation"|"divisions"|"base"|"addresses", "type"?: "the theme's type (building, building_part; place; segment, connector; division_area, division, division_boundary; land_use, land, land_cover, water, infrastructure, bathymetry; address)", "release"?: "such as 2026-09-23.1 (default: the latest)", "bbox"?: [west, south, east, north] | "view" (the map's extent, the default), "tiles"?: bool (the whole world as map tiles, from the theme's PMTiles archive, instead of the features in view), "files"?: [the files' GeoParquet addresses, when already found and opened (the app's background requests)]}"#, + r#"{"theme": "buildings"|"places"|"transportation"|"divisions"|"base"|"addresses", "type"?: "the theme's type (building, building_part; place; segment, connector; division_area, division, division_boundary; land_use, land, land_cover, water, infrastructure, bathymetry; address)", "release"?: "such as 2026-09-23.1 (default: the latest)", "bbox"?: [west, south, east, north] | "view" (the map's extent, the default), "tiles"?: bool (the whole world as map tiles, from the theme's PMTiles archive, instead of the features in view), "files"?: [the files' GeoParquet addresses, when already found and opened (the app's background requests)]} (in a plugin's command in the app the files are left to download: it answers pending, with the layers already in, and the command runs again once they are added; see docs/plugins.md)"#, ), C::new("sourcecoop.products", "List Source Cooperative Products", "Home › Add › Web Data", sourcecoop_products).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)) .view() @@ -110,23 +110,52 @@ fn overture_add(s: &mut Session, p: &Value) -> CmdResult { /// Adds a theme type's files (read in place, the map's view kept), all or none: when one can't be /// added, the ones added before it are taken out again, so trying again doesn't add them twice. +/// Files left to the app (a plugin's command in the app) make the answer `"pending": true`; the +/// ones that landed before a later file failed stay (each its own undo step), and the error says so. pub(crate) fn add_files(s: &mut Session, ty: &str, urls: &[String]) -> CmdResult { // What a failure puts back: the map's layers in their order and its layer ids (not a copy of // their data), and the active layer. let (n, active, before) = (urls.len(), s.active_layer, s.map().layer_mark()); let mut layers: Vec = Vec::new(); + let mut pending = false; for (i, url) in urls.iter().enumerate() { match super::layer::add_url(s, &json!({"url": url, "name": overture::layer_name(ty, i, n), "zoom": false})) { - Ok(r) => layers - .extend(r["layers"].as_array().into_iter().flatten().filter_map(Value::as_u64).filter_map(|id| u32::try_from(id).ok())), + Ok(r) => { + // A plugin's command in the app leaves the file to the app (`layer.addUrl`'s pending + // answer): the layer comes when it lands, and the command runs again (#2677). + pending |= r.get("pending").and_then(Value::as_bool) == Some(true); + layers + .extend(r["layers"].as_array().into_iter().flatten().filter_map(Value::as_u64).filter_map(|id| u32::try_from(id).ok())); + } Err(e) => { s.map_mut().restore_layers(&before); s.active_layer = active; - return Err(if n > 1 { format!("{e} (none of the {n} files was added)") } else { e }); + // Layers a plugin's earlier run left to the app have landed before this run: they + // stay, each its own undo step, so say so rather than that none was added. + // Counted from the downloads that landed, whichever of the files they were: a file + // after the failed one may have landed too, and this loop never reached it. + let kept = s + .rerun_landed + .iter() + .filter(|(asked, _, added)| { + added.added_any() + && !added.gone(s.map()) + && asked.get("url").and_then(Value::as_str).is_some_and(|u| urls.iter().any(|x| x == u)) + }) + .count(); + return Err(match (n > 1, kept) { + (false, _) => e, + (true, 0) => format!("{e} (none of the {n} files was added)"), + (true, k) => format!("{e} ({k} of the {n} files landed earlier and stay, each its own undo step)"), + }); } } } - Ok(json!({"layers": layers, "files": n})) + let mut out = json!({"layers": layers, "files": n}); + if pending { + out["pending"] = json!(true); + } + Ok(out) } fn sourcecoop_products(_s: &mut Session, p: &Value) -> CmdResult { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index d1ffa103c7..0939df8603 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -37495,6 +37495,119 @@ fn overture_files_are_added_all_or_none() { assert_eq!(r["files"], 2); let names: Vec<&str> = s.map().layers.iter().map(|l| l.name.as_str()).collect(); assert!(names.contains(&"Overture building (1/2)") && names.contains(&"Overture building (2/2)"), "{names:?}"); + assert!(r.get("pending").is_none(), "files added here are not pending: {r}"); +} + +/// A plugin's command in the app leaves each Overture file to the app (`layer.addUrl`'s pending +/// answer): `overture.add` says so, instead of answering an empty list of layers (#2677). +#[test] +fn overture_files_a_plugin_command_leaves_to_the_app_answer_pending() { + let home = "https://overturemaps-us-west-2.s3.amazonaws.com/release/2026-11-18.0/theme=buildings/type=building"; + let urls = [format!("{home}/part-0.parquet"), format!("{home}/part-1.parquet")]; + let mut s = Session::new(Project::default()); + let r = s.run_leaving_downloads("overture.add", &json!({"theme": "buildings", "files": urls})).unwrap(); + assert_eq!(r["pending"], true, "{r}"); + assert_eq!((&r["files"], &r["layers"]), (&json!(2), &json!([])), "{r}"); + assert!(s.map().layers.is_empty()); + // Both files are asked for, named as the synchronous add names them; nothing was fetched. + let asked = s.take_url_requests(); + let named: Vec<(&str, &str)> = + asked.iter().map(|q| (q.params()["url"].as_str().unwrap_or_default(), q.params()["name"].as_str().unwrap_or_default())).collect(); + assert_eq!(named, [(urls[0].as_str(), "Overture building (1/2)"), (urls[1].as_str(), "Overture building (2/2)")]); +} + +/// A plugin command that adds an area's Overture files in the app waits for them, and runs again +/// once the app has added them: its `overture.add` then answers both layers, added once (#2677). +#[test] +fn a_plugin_command_adding_overture_files_runs_again_when_they_are_added() { + use rustgis_data::{Dataset, Feature, Field, FieldType, Geometry, GeometryType, Value}; + let mut ds = Dataset::new("b", GeometryType::Point, vec![Field::new("n", FieldType::Integer)]); + for i in 0..5u32 { + let c = rustgis_geom::Coord::new(7.0 + 0.01 * f64::from(i), 46.0); + ds.features.push(Feature { id: u64::from(i) + 1, geometry: Some(Geometry::Point(c)), attrs: vec![Value::Int(i64::from(i))] }); + } + let bytes = rustgis_data::source::parquet::write(&ds).unwrap(); + let mut s = world(); + plugin("eng-overture", &["edit", "network"]); + let layers = s.map().layers.len(); + let home = "https://overturemaps-us-west-2.s3.amazonaws.com/release/2026-11-18.0/theme=buildings/type=building"; + let urls = [format!("{home}/part-0.parquet"), format!("{home}/part-1.parquet")]; + let p = json!({"op": "overture.add", "params": {"theme": "buildings", "files": urls}}); + let first = s.run_leaving_downloads("plugin:eng-overture/f", &p).unwrap(); + assert_eq!(first["pending"], true, "{first}"); + assert_eq!(s.map().layers.len(), layers, "the call's own run is undone"); + let asked = s.take_url_requests(); + assert_eq!(asked.len(), 2); + assert!(s.poll_plugin_retries().is_empty(), "nothing landed yet"); + // The app downloads and adds both, each with its request's number. + for q in &asked { + let id = s.download_started_for(q).unwrap(); + assert!(s.download_landed(id, bytes.clone())); + let mut add = q.params().clone(); + add["id"] = json!(id); + s.complete_download(id, &add).unwrap(); + } + assert_eq!(s.map().layers.len(), layers + 2); + let undo = s.undo.len(); + let ran = s.poll_plugin_retries(); + assert_eq!(ran.len(), 1, "{ran:?}"); + let answer = ran[0].1.as_ref().unwrap(); + assert_eq!(answer["layers"].as_array().map(Vec::len), Some(2), "{answer}"); + assert!(answer.get("pending").is_none(), "{answer}"); + let names: Vec<&str> = s.map().layers.iter().map(|l| l.name.as_str()).collect(); + assert!(names.contains(&"Overture building (1/2)") && names.contains(&"Overture building (2/2)"), "{names:?}"); + assert_eq!((s.map().layers.len(), s.undo.len()), (layers + 2, undo), "added once"); + assert!(s.take_url_requests().is_empty() && !s.plugin_retries_waiting()); + rustgis_plugins::unload("eng-overture"); +} + +/// When one of a plugin command's Overture files fails and the other landed (before or after it), +/// the command's error says that one stays (its own undo step), not that none was added. +#[test] +fn a_failed_pending_overture_add_says_which_files_stay() { + // The failed file comes second (the loop has added the first) or first (it never reaches the + // one that landed after it). + for bad_first in [false, true] { + a_failed_pending_overture_add_says_which_files_stay_in(bad_first); + } +} + +fn a_failed_pending_overture_add_says_which_files_stay_in(bad_first: bool) { + use rustgis_data::{Dataset, Feature, Field, FieldType, Geometry, GeometryType, Value}; + let mut ds = Dataset::new("b", GeometryType::Point, vec![Field::new("n", FieldType::Integer)]); + for i in 0..5u32 { + let c = rustgis_geom::Coord::new(7.0 + 0.01 * f64::from(i), 46.0); + ds.features.push(Feature { id: u64::from(i) + 1, geometry: Some(Geometry::Point(c)), attrs: vec![Value::Int(i64::from(i))] }); + } + let good = rustgis_data::source::parquet::write(&ds).unwrap(); + fn bad_bytes() -> Vec { + b"not a parquet file".to_vec() + } + let mut s = world(); + plugin("eng-overture-fail", &["edit", "network"]); + let layers = s.map().layers.len(); + let home = "https://overturemaps-us-west-2.s3.amazonaws.com/release/2026-11-18.0/theme=buildings/type=building"; + let urls = [format!("{home}/part-0.parquet"), format!("{home}/part-1.parquet")]; + let p = json!({"op": "overture.add", "params": {"theme": "buildings", "files": urls}}); + s.run_leaving_downloads("plugin:eng-overture-fail/f", &p).unwrap(); + let asked = s.take_url_requests(); + assert_eq!(asked.len(), 2); + assert_eq!(asked[0].params()["url"], json!(urls[0]), "asked in the files' order"); + // One lands and is added; the other's download fails. + for (q, bytes) in asked.iter().zip(if bad_first { [bad_bytes(), good.clone()] } else { [good.clone(), bad_bytes()] }) { + let id = s.download_started_for(q).unwrap(); + assert!(s.download_landed(id, bytes)); + let mut add = q.params().clone(); + add["id"] = json!(id); + let _ = s.complete_download(id, &add); + } + let ran = s.poll_plugin_retries(); + let e = ran.first().and_then(|r| r.1.as_ref().err()).cloned().unwrap_or_default(); + assert_eq!(s.map().layers.len(), layers + 1, "the file that landed stays: {ran:?}"); + assert!(e.contains("1 of the 2 files landed earlier and stay"), "{e}"); + assert!(e.contains(if bad_first { "part-0.parquet" } else { "part-1.parquet" }), "the failed file's own error: {e}"); + assert!(!e.contains("none of the"), "{e}"); + rustgis_plugins::unload("eng-overture-fail"); } /// A multi-file add that fails puts the map back exactly: the same layers in the same order, and diff --git a/docs/plugins.md b/docs/plugins.md index 7f196bfc78..5b72cdb4c4 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -699,6 +699,15 @@ the way, once for each document at most, and a map layer is added by the run tha 16 documents are read at a time, and asking for more fails. In the web app `service.add` still refuses to download. +`overture.add` does the same on the desktop when it adds an area's files: each GeoParquet file is +left to download as `layer.addUrl` does, and while any is under way it answers `{"pending": true, +"files": n, "layers": [...]}` (the layers already in; none on the first run), so the command can +return and runs again once they are added, when `overture.add` answers the ids of the layers it +added. Where it adds the files in the same run (the command line, the assistant) it adds all of an +area's files or none. When they are left to download, a file that fails stops the command with an +error that says how many landed before it. Those stay, each its own undo step. +It also fetches the catalog on each run, which waits with the window held. + While a command waits the window answers, so the user can work and the project can change. The status bar says the command is running, and asking for it again with the same parameters does nothing. A run again sees the view the command first ran with, and the window's size then, unless