diff --git a/AGENTS.md b/AGENTS.md index 4c391ad83e..800c932610 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -44,6 +44,11 @@ Before picking work, read [ROADMAP.md](ROADMAP.md) (check Underway) and - **Everything is a command**: a `CommandSpec` in `crates/engine/src/cmd/*.rs`. UI-only commands (`ui.*`) live in `crates/ui-egui/src/lib.rs`. The ribbon, shortcuts, command search and CLI dispatch by id. Commands never open dialogs. +- **Command ingress**: root `Session::run` is trusted native User dispatch. Assistant, MCP, + plugin, imported-item and continuation adapters carry their named opaque policy scope; + scopes inherit or narrow, and `with_scope` rejects nested ingress. Peer project text uses + `open_peer_text`. See [embedding-page limitations](SECURITY.md#embedding-page-command-boundary) + when changing web message handlers. - **Ribbon placement** follows the [ribbon guidelines](docs/contributing.md#ribbon-guidelines): the tab's purpose, one button per destination, labels that say what happens, and what a ribbon pull request states. diff --git a/SECURITY.md b/SECURITY.md index 1db8ac6b49..70f1c6432d 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -41,6 +41,13 @@ including output from another session. This changes displayed output, not the st requests or permission checks. RustGIS does not retain revoked secrets just to recognize them in later output; review that output before sharing it. +## Embedding-page command boundary + +The execution policy covers engine commands admitted through named assistant, MCP, plugin and +imported-item scopes. The authority of `ui.*` RUN messages from embedding pages remains a +separate, unresolved boundary; engine approval checks do not establish authorization for those +UI messages. + ## What counts as a security problem RustGIS makes promises about what untrusted plugins, files, agents and web pages can do. A way diff --git a/apps/rustgis-web/src/arcgis.rs b/apps/rustgis-web/src/arcgis.rs index 1996623934..c84e6d4cb1 100644 --- a/apps/rustgis-web/src/arcgis.rs +++ b/apps/rustgis-web/src/arcgis.rs @@ -60,6 +60,12 @@ async fn sleep(ms: i32) -> Result<(), String> { /// A request to the portal: a form POSTed, or a GET. Never redirected, never with the page's /// cookies. async fn request(url: &str, form: Option<&str>) -> Result, String> { + request_claimed(url, form, None).await +} + +/// A renewal caused by a claimed download checks its owned predicate on first poll and send. +async fn request_claimed(url: &str, form: Option<&str>, still: Option) -> Result, String> { + cache::check_still(&still)?; let window = web_sys::window().ok_or("no window")?; let init = web_sys::RequestInit::new(); init.set_redirect(web_sys::RequestRedirect::Error); @@ -81,7 +87,9 @@ async fn request(url: &str, form: Option<&str>) -> Result, String> { }; let timer = window.set_timeout_with_callback_and_timeout_and_arguments_0(stop.unchecked_ref(), REQUEST_MS).ok(); let read = async { - let resp: web_sys::Response = wasm_bindgen_futures::JsFuture::from(window.fetch_with_str_and_init(url, &init)) + let promise = + crate::request::prepare_and_send(&still, || async { Ok(()) }, |()| window.fetch_with_str_and_init(url, &init)).await?; + let resp: web_sys::Response = wasm_bindgen_futures::JsFuture::from(promise) .await .map_err(|_| "couldn't reach the portal".to_string())? .dyn_into() @@ -177,7 +185,10 @@ async fn finish(flow: &arcgis_auth::WebSignIn, popup: &web_sys::Window) -> Resul /// Renews the session covering `url` when its access token is about to expire (one renewal at a /// time; the fetches meanwhile use the token, still valid for a minute). -pub async fn renew_if_due(url: &str) { +pub async fn renew_if_due(url: &str, still: Option) { + if cache::check_still(&still).is_err() { + return; + } if RENEWING.get() { return; } @@ -191,6 +202,9 @@ pub async fn renew_if_due(url: &str) { } RENEWING.set(true); let _renewing = Renewing; - let answer = request(&r.endpoint, Some(&r.body)).await; + let answer = request_claimed(&r.endpoint, Some(&r.body), still.clone()).await; + if cache::check_still(&still).is_err() { + return; + } arcgis_auth::renewal_done(&r, answer.as_deref().map_err(String::clone)); } diff --git a/apps/rustgis-web/src/geolibre.rs b/apps/rustgis-web/src/geolibre.rs index 44e929608d..2a1574df0d 100644 --- a/apps/rustgis-web/src/geolibre.rs +++ b/apps/rustgis-web/src/geolibre.rs @@ -16,15 +16,22 @@ use wasm_bindgen::prelude::*; // safeguards: the same trust as installing the npm package. #[wasm_bindgen(inline_js = r#" const BASE = "https://cdn.jsdelivr.net/npm/geolibre-wasm@1.8.0"; +let loaded = null; let ready = null; -function tools() { +async function tools(still = () => true) { + if (!still()) throw new Error("GeoLibre run cancelled before loading"); + if (!loaded) { + loaded = import(`${BASE}/tools.mjs/+esm`); + loaded.catch(() => { loaded = null; }); + } + const mod = await loaded; + if (!still()) throw new Error("GeoLibre run cancelled before initialization"); if (!ready) { ready = (async () => { - const mod = await import(`${BASE}/tools.mjs/+esm`); await mod.initTools(`${BASE}/geolibre-cli.wasm`); return mod; })(); - // A failed download can be retried. + // A failed initialization can be retried without importing the module again. ready.catch(() => { ready = null; }); } return ready; @@ -66,8 +73,10 @@ export async function geolibre_manifests() { } return text; } -export async function geolibre_run(tool, argv, names, datas) { - const t = await tools(); +export async function geolibre_run(tool, argv, names, datas, still) { + if (!still()) throw new Error("GeoLibre run cancelled before preparation"); + const t = await tools(still); + if (!still()) throw new Error("GeoLibre run cancelled before starting"); const input = {}; for (let i = 0; i < names.length; i++) input[names[i]] = datas[i]; const r = await t.runTool(tool, { args: argv, input }); @@ -79,7 +88,13 @@ extern "C" { #[wasm_bindgen(catch)] async fn geolibre_manifests() -> Result; #[wasm_bindgen(catch)] - async fn geolibre_run(tool: &str, argv: js_sys::Array, names: js_sys::Array, datas: js_sys::Array) -> Result; + async fn geolibre_run( + tool: &str, + argv: js_sys::Array, + names: js_sys::Array, + datas: js_sys::Array, + still: &js_sys::Function, + ) -> Result; } fn js_error(e: &JsValue) -> String { @@ -114,10 +129,19 @@ pub fn manifests(slot: Slot>, ctx: egui::Context) { /// Runs one tool and puts its new files and printed lines (or its error) in `slot`. pub fn run(job: ExternalRun, slot: Slot, ctx: egui::Context) { wasm_bindgen_futures::spawn_local(async move { + if let Err(e) = job.can_start() { + if let Ok(mut out) = slot.lock() { + *out = Some(Err(e)); + } + ctx.request_repaint(); + return; + } let argv: js_sys::Array = job.argv.iter().map(|a| JsValue::from_str(a)).collect(); let names: js_sys::Array = job.files.iter().map(|(n, _)| JsValue::from_str(n)).collect(); let datas: js_sys::Array = job.files.iter().map(|(_, b)| JsValue::from(js_sys::Uint8Array::from(b.as_slice()))).collect(); - let r = match geolibre_run(&job.tool, argv, names, datas).await { + let still = job.still(); + let check = wasm_bindgen::closure::Closure:: bool>::new(move || still()); + let r = match geolibre_run(&job.tool, argv, names, datas, check.as_ref().unchecked_ref()).await { Err(e) => Err(js_error(&e)), Ok(out) => { let lines = strings(&get(&out, "stdout")); diff --git a/apps/rustgis-web/src/main.rs b/apps/rustgis-web/src/main.rs index 3934d44142..8c4d5a9c85 100644 --- a/apps/rustgis-web/src/main.rs +++ b/apps/rustgis-web/src/main.rs @@ -22,6 +22,8 @@ mod embed; mod geolibre; #[cfg(target_arch = "wasm32")] mod parts; +#[cfg(any(target_arch = "wasm32", test))] +mod request; #[cfg(target_arch = "wasm32")] mod web; diff --git a/apps/rustgis-web/src/request.rs b/apps/rustgis-web/src/request.rs new file mode 100644 index 0000000000..bb34998c7c --- /dev/null +++ b/apps/rustgis-web/src/request.rs @@ -0,0 +1,203 @@ +//! The browser adapters' owned handoff, preparation and synchronous final-send boundary. +//! No synchronous diagnostic or request context is held while a future is suspended. + +use rustgis_ui_egui::rustgis_engine::{data::diagnostics::Capture, render::rustgis_tiles::cache}; +use std::future::Future; + +/// Captures context at adapter invocation, before `spawn_local` can defer its first poll. +/// Completion re-enters it only synchronously, so callbacks can start follow-on requests. +pub(crate) fn handoff>>( + work: impl FnOnce(Capture, Option) -> F, + done: impl FnOnce(Result), +) -> impl Future { + let capture = Capture::current(); + let still = cache::current_still(); + async move { + let result = match cache::check_still(&still) { + Ok(()) => work(capture.clone(), still.clone()).await, + Err(e) => Err(e), + }; + let _diagnostics = capture.enter(); + cache::with_optional_still(still, || done(result)); + } +} + +/// The actual adapters prepare lazily, including renewal awaits, then invoke their real fetch +/// synchronously after the final check. The sender must initiate I/O without another handoff. +pub(crate) async fn prepare_and_send>>( + still: &Option, + prepare: impl FnOnce() -> F, + send: impl FnOnce(T) -> R, +) -> Result { + cache::check_still(still)?; + let prepared = prepare().await?; + cache::check_still(still)?; + Ok(send(prepared)) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::{ + cell::{Cell, RefCell}, + pin::Pin, + rc::Rc, + sync::{ + Arc, + atomic::{AtomicBool, Ordering::SeqCst}, + }, + task::{Context, Poll, Waker}, + }; + + fn poll(f: Pin<&mut F>) -> Poll { + f.poll(&mut Context::from_waker(Waker::noop())) + } + + struct Held(Rc>); + impl Future for Held { + type Output = Result<(), String>; + fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll { + if self.0.get() { Poll::Ready(Ok(())) } else { Poll::Pending } + } + } + + fn live() -> (Arc, cache::Still) { + let live = Arc::new(AtomicBool::new(true)); + let flag = live.clone(); + (live, Arc::new(move || flag.load(SeqCst))) + } + + /// The installed GET/HEAD/POST handoff captures before scheduling, not on first poll. + #[test] + fn permission_transport_handoff_refuses_before_first_poll() { + let (live, still) = live(); + let prepared = Cell::new(false); + let finished = Cell::new(false); + let mut f = Box::pin(cache::with_still(still, || { + handoff( + |_, _| async { + prepared.set(true); + Ok(()) + }, + |r| { + assert!(r.unwrap_err().contains("cancelled")); + finished.set(true); + }, + ) + })); + assert!(cache::current_still().is_none()); + live.store(false, SeqCst); + assert!(poll(f.as_mut()).is_ready()); + assert!(!prepared.get() && finished.get()); + assert!(cache::current_still().is_none()); + } + + /// The same production preparation/send function backs GET, HEAD, POST and renewal fetch. + /// Unchanged account identity sends; replacement behind the same public URL stops a held send. + #[test] + fn permission_transport_preparation_await_refuses_final_send() { + use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::arcgis_auth as auth; + let url = "https://browser-identity.example.org/server/rest/services/A/MapServer"; + auth::add_token(url, "synthetic-first").unwrap(); + let identity = auth::account_identity_for(url).unwrap(); + let still: cache::Still = Arc::new(move || identity.is_current()); + let released = Rc::new(Cell::new(false)); + let sends = Cell::new(0); + let finished = Cell::new(false); + let owned = Some(still.clone()); + let mut clean = Box::pin(prepare_and_send(&owned, || async { Ok(()) }, |()| sends.set(sends.get() + 1))); + assert!(matches!(poll(clean.as_mut()), Poll::Ready(Ok(())))); + drop(clean); + assert_eq!(sends.get(), 1); + let mut f = Box::pin(cache::with_still(still, || { + handoff( + |_, still| { + let (released, sends) = (&released, &sends); + async move { + prepare_and_send( + &still, + || async { + assert!(cache::current_still().is_none()); + Held(released.clone()).await + }, + |()| sends.set(sends.get() + 1), + ) + .await + } + }, + |r| { + assert!(r.unwrap_err().contains("cancelled")); + finished.set(true); + }, + ) + })); + assert!(poll(f.as_mut()).is_pending()); + assert!(cache::current_still().is_none(), "no context crosses the await"); + auth::add_token(url, "synthetic-replacement").unwrap(); + released.set(true); + assert!(poll(f.as_mut()).is_ready()); + assert_eq!(sends.get(), 1, "only the unchanged identity sent"); + assert!(finished.get()); + assert!(cache::current_still().is_none()); + auth::sign_out(Some("https://browser-identity.example.org/server")); + } + + /// A new service account binding cannot cross a browser preparation await under old consent. + #[test] + fn permission_transport_service_binding_refuses_after_preparation() { + use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::logins::{self, Method}; + let url = "https://browser-service-identity.example"; + let binding = logins::activate(url, "9150003/connection", Method::Header("X-Key".into()), "synthetic-browser", false).unwrap(); + let identity = logins::account_identity(9_150_003).unwrap(); + let still = Some(Arc::new(move || identity.is_current()) as cache::Still); + let released = Rc::new(Cell::new(false)); + let sends = Cell::new(0); + let mut held = Box::pin(prepare_and_send(&still, || Held(released.clone()), |()| sends.set(sends.get() + 1))); + assert!(poll(held.as_mut()).is_pending()); + let other = logins::activate(url, "91500030/connection", Method::Header("X-Key".into()), "synthetic-other", false).unwrap(); + assert!(cache::check_still(&still).is_ok(), "another session preserves consent"); + assert!(logins::deactivate(&binding)); + let replacement = logins::activate(url, "9150003/connection", Method::Header("X-Key".into()), "synthetic-browser", false).unwrap(); + released.set(true); + assert!(matches!(poll(held.as_mut()), Poll::Ready(Err(_)))); + assert_eq!(sends.get(), 0); + assert!(cache::current_still().is_none(), "no context crosses the await"); + assert!(logins::deactivate(&replacement)); + assert!(logins::deactivate(&other)); + } + + /// A callback's follow-on adapter owns the same predicate after the callback returns. + #[test] + fn permission_transport_callback_follow_on_keeps_owned_context() { + let (live, still) = live(); + let expected = still.clone(); + let sends = Rc::new(Cell::new(0)); + let finished = Rc::new(Cell::new(false)); + type Pending = Pin>>; + let follow_on: RefCell> = RefCell::new(None); + let mut f = Box::pin(cache::with_still(still, || { + handoff( + |_, still| async move { prepare_and_send(&still, || async { Ok(()) }, |()| {}).await }, + |r| { + r.unwrap(); + assert!(Arc::ptr_eq(&cache::current_still().unwrap(), &expected)); + let (sent, done) = (sends.clone(), finished.clone()); + *follow_on.borrow_mut() = Some(Box::pin(handoff( + move |_, still| async move { prepare_and_send(&still, || async { Ok(()) }, |()| sent.set(sent.get() + 1)).await }, + move |r| { + assert!(r.unwrap_err().contains("cancelled")); + done.set(true); + }, + ))); + }, + ) + })); + assert!(poll(f.as_mut()).is_ready()); + assert!(cache::current_still().is_none()); + live.store(false, SeqCst); + assert!(poll(follow_on.borrow_mut().as_mut().unwrap().as_mut()).is_ready()); + assert_eq!(sends.get(), 0); + assert!(finished.get()); + assert!(cache::current_still().is_none()); + } +} diff --git a/apps/rustgis-web/src/web.rs b/apps/rustgis-web/src/web.rs index 7d639e163f..ab41fdc11e 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -591,79 +591,98 @@ fn refused(resp: &web_sys::Response) -> String { /// map's edge): its failure isn't recorded, only noted once per server, as on the desktop. A /// request carrying a login is recorded by its plain address and scrubbed text /// ([`RequestDiagnostics::failed`]), never its binding or credential. -async fn fetch_bytes(url: &str, capture: Capture, omit: &'static [u16]) -> Result, String> { +async fn fetch_bytes( + url: &str, + capture: Capture, + still: Option, + omit: &'static [u16], +) -> Result, String> { use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache::RequestDiagnostics, failure::Failure}; let mut diagnostics = RequestDiagnostics::with_capture(url, "GET", capture); diagnostics.omit_statuses(omit); let answer = async { - let window = web_sys::window().ok_or("no window")?; use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache, logins}; - // A data download carries its caller's budget (at most MAX_DOWNLOAD); other fetches use 64 MB. - let (url, limit) = cache::split_download_limit(url); - let max = limit.map(|n| n as u64).unwrap_or(64 << 20); - let (url, tail) = cache::split_tail(url); - let (url, range) = cache::split_range(url); - // An `s3://` address becomes its web address, and a request to an S3 connection's endpoint - // is signed (the bucket's CORS rules must allow the Authorization and x-amz-* headers). - logins::check(url).map_err(str::to_string)?; - let bound = url; - let prepared = rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::s3::prepare(logins::inner(url))?; - let url = prepared.url.as_str(); - // An ArcGIS sign-in's token, for the portal and the servers it covers, renewed first when it - // is about to expire. It goes as the request's `token` parameter, not a header: ArcGIS's - // query endpoints answer the header's CORS preflight with an error. - if !prepared.signed { - crate::arcgis::renew_if_due(url).await; - } - let target = logins::inherit(bound, url); - let login = if prepared.signed { None } else { logins::apply(&target).map_err(str::to_string)? }; - let login_url = login.as_ref().map_or(url, |applied| applied.url.as_str()); - let arcgis = - if prepared.signed { None } else { rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::arcgis_auth::with_token(login_url) }; - // Only the request carries credentials: messages and cache keys keep the original address. - let request = arcgis.as_deref().unwrap_or(login_url); - let result = async { - let err = |e: wasm_bindgen::JsValue| { - if login.is_some() { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") } - }; - let login_header = login.as_ref().and_then(|applied| applied.header.as_ref()); - let asked = match (range, tail) { - (Some((a, b)), _) => Some(format!("bytes={a}-{b}")), - (None, Some(n)) => Some(format!("bytes=-{n}")), - (None, None) => None, - }; - let init = web_sys::RequestInit::new(); - // A tile or document that stalls is given up (see `STALL_MS`). - let abort = if limit.is_none() { web_sys::AbortController::new().ok() } else { None }; - if let Some(a) = &abort { - init.set_signal(Some(&a.signal())); - } - let stalled = || format!("nothing came in {} seconds", STALL_MS / 1000); - if asked.is_some() || !prepared.headers.is_empty() || login_header.is_some() { - let headers = web_sys::Headers::new().map_err(err)?; - if let Some(r) = &asked { - headers.set("Range", r).map_err(err)?; + let (promise, abort, range, tail, max, url, bound, authenticated) = crate::request::prepare_and_send( + &still, + || async { + let window = web_sys::window().ok_or("no window")?; + // A data download carries its caller's budget (at most MAX_DOWNLOAD); other fetches use 64 MB. + let (url, limit) = cache::split_download_limit(url); + let max = limit.map(|n| n as u64).unwrap_or(64 << 20); + let (url, tail) = cache::split_tail(url); + let (url, range) = cache::split_range(url); + // An `s3://` address becomes its web address, and a request to an S3 connection's endpoint + // is signed (the bucket's CORS rules must allow the Authorization and x-amz-* headers). + logins::check(url).map_err(str::to_string)?; + let bound = url; + let prepared = rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::s3::prepare(logins::inner(url))?; + let url = prepared.url.as_str(); + // An ArcGIS sign-in's token, for the portal and the servers it covers, renewed first when it + // is about to expire. It goes as the request's `token` parameter, not a header: ArcGIS's + // query endpoints answer the header's CORS preflight with an error. + if !prepared.signed { + crate::arcgis::renew_if_due(url, still.clone()).await; + cache::check_still(&still)?; } - for (k, v) in &prepared.headers { - if !login_header.is_some_and(|(name, _)| name.eq_ignore_ascii_case(k)) { - headers.set(k, v).map_err(err)?; + let target = logins::inherit(bound, url); + let login = if prepared.signed { None } else { logins::apply(&target).map_err(str::to_string)? }; + let login_url = login.as_ref().map_or(url, |applied| applied.url.as_str()); + let arcgis = + if prepared.signed { None } else { rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::arcgis_auth::with_token(login_url) }; + // Only the request carries credentials: messages and cache keys keep the original address. + let request = arcgis.as_deref().unwrap_or(login_url); + let authenticated = prepared.signed || arcgis.is_some() || login.is_some(); + let err = |e: wasm_bindgen::JsValue| { + if authenticated { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") } + }; + let login_header = login.as_ref().and_then(|applied| applied.header.as_ref()); + let asked = match (range, tail) { + (Some((a, b)), _) => Some(format!("bytes={a}-{b}")), + (None, Some(n)) => Some(format!("bytes=-{n}")), + (None, None) => None, + }; + let init = web_sys::RequestInit::new(); + // A tile or document that stalls is given up (see `STALL_MS`). + let abort = if limit.is_none() { web_sys::AbortController::new().ok() } else { None }; + if let Some(a) = &abort { + init.set_signal(Some(&a.signal())); + } + if asked.is_some() || !prepared.headers.is_empty() || login_header.is_some() { + let headers = web_sys::Headers::new().map_err(err)?; + if let Some(r) = &asked { + headers.set("Range", r).map_err(err)?; } + for (k, v) in &prepared.headers { + if !login_header.is_some_and(|(name, _)| name.eq_ignore_ascii_case(k)) { + headers.set(k, v).map_err(err)?; + } + } + if let Some((name, value)) = login_header { + headers.set(name, value).map_err(err)?; + } + init.set_headers(&headers); } - if let Some((name, value)) = login_header { - headers.set(name, value).map_err(err)?; + // A signed or authorized request goes only where its credentials are for. + if authenticated { + init.set_redirect(web_sys::RequestRedirect::Error); } - init.set_headers(&headers); - } - // A signed or authorized request goes only where its credentials are for. - if prepared.signed || arcgis.is_some() || login.is_some() { - init.set_redirect(web_sys::RequestRedirect::Error); - } - // A credential-bearing address goes nowhere else: no referrer from it, no cookies with it. - if arcgis.is_some() || login.is_some() { - init.set_referrer_policy(web_sys::ReferrerPolicy::NoReferrer); - init.set_credentials(web_sys::RequestCredentials::Omit); - } - let promise = window.fetch_with_str_and_init(request, &init); + // A credential-bearing address goes nowhere else: no referrer from it, no cookies with it. + if arcgis.is_some() || login.is_some() { + init.set_referrer_policy(web_sys::ReferrerPolicy::NoReferrer); + init.set_credentials(web_sys::RequestCredentials::Omit); + } + Ok((window, request.to_string(), init, abort, range, tail, max, prepared.url, bound.to_string(), authenticated)) + }, + |(window, request, init, abort, range, tail, max, url, bound, authenticated)| { + (window.fetch_with_str_and_init(&request, &init), abort, range, tail, max, url, bound, authenticated) + }, + ) + .await?; + let err = |e: wasm_bindgen::JsValue| { + if authenticated { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") } + }; + let stalled = || format!("nothing came in {} seconds", STALL_MS / 1000); + let result = async { // A refused fetch is an opaque TypeError ("Failed to fetch") by design. Keep its message // only on requests without a login: even a browser's error may repeat credentials. let resp: web_sys::Response = unless_stalled(promise, abort.as_ref()) @@ -673,7 +692,7 @@ async fn fetch_bytes(url: &str, capture: Capture, omit: &'static [u16]) -> Resul stalled() })? .map_err(|e| { - let why = if login.is_some() { + let why = if authenticated { "the authenticated request failed".to_string() } else { e.dyn_ref::().map(|e| String::from(e.message())).unwrap_or_else(|| err(e)) @@ -755,8 +774,9 @@ async fn fetch_bytes(url: &str, capture: Capture, omit: &'static [u16]) -> Resul Ok(bytes) } .await; - logins::check(bound).map_err(str::to_string)?; - result.map_err(|error| if login.is_some() { logins::scrub_error(&format!("{url}: {error}")) } else { error }) + cache::check_still(&still)?; + logins::check(&bound).map_err(str::to_string)?; + result.map_err(|error| if authenticated { logins::scrub_error(&format!("{url}: {error}")) } else { error }) } .await; diagnostics.record(answer.map_err(|e| Failure::new(url, e))).map_err(|failure| failure.message) @@ -764,45 +784,61 @@ async fn fetch_bytes(url: &str, capture: Capture, omit: &'static [u16]) -> Resul /// POSTs a JSON document and reads the answer (at most the tile cache's `MAX_BYTES`), without /// following redirects; an error status is `HTTP `. -async fn post_json(url: &str, body: &str, capture: Capture) -> Result, String> { +async fn post_json( + url: &str, + body: &str, + capture: Capture, + still: Option, +) -> Result, String> { use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache::RequestDiagnostics, failure::Failure}; let mut diagnostics = RequestDiagnostics::with_capture(url, "POST", capture); let answer = async { use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache, logins, s3}; - let window = web_sys::window().ok_or("no window")?; - logins::check(url).map_err(str::to_string)?; - // Preparation supplies the reached URL, never GET signing headers for a POST. - let prepared = s3::prepare(logins::inner(url))?; - if prepared.signed { - return Err("S3 signed JSON POST requests are not supported.".into()); - } - let target = logins::inherit(url, &prepared.url); - let login = logins::apply(&target).map_err(str::to_string)?; - let request = prepared.url.as_str(); - let result = async { - let err = |e: wasm_bindgen::JsValue| { - if login.is_some() { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") } - }; - let init = web_sys::RequestInit::new(); - init.set_method("POST"); - init.set_redirect(web_sys::RequestRedirect::Error); - let headers = web_sys::Headers::new().map_err(err)?; - let login_header = login.as_ref().and_then(|applied| applied.header.as_ref()); - for name in ["Content-Type", "Accept"] { - if !login_header.is_some_and(|(login_name, _)| login_name.eq_ignore_ascii_case(name)) { - headers.set(name, "application/json").map_err(err)?; + let (promise, authenticated) = crate::request::prepare_and_send( + &still, + || async { + let window = web_sys::window().ok_or("no window")?; + logins::check(url).map_err(str::to_string)?; + // Preparation supplies the reached URL, never GET signing headers for a POST. + let prepared = s3::prepare(logins::inner(url))?; + if prepared.signed { + return Err("S3 signed JSON POST requests are not supported.".into()); } - } - if let Some((name, value)) = login_header { - headers.set(name, value).map_err(err)?; - } - init.set_headers(&headers); - init.set_body(&wasm_bindgen::JsValue::from_str(body)); - if login.is_some() { - init.set_referrer_policy(web_sys::ReferrerPolicy::NoReferrer); - init.set_credentials(web_sys::RequestCredentials::Omit); - } - let resp: web_sys::Response = wasm_bindgen_futures::JsFuture::from(window.fetch_with_str_and_init(request, &init)) + let target = logins::inherit(url, &prepared.url); + let login = logins::apply(&target).map_err(str::to_string)?; + let request = prepared.url.as_str(); + let err = |e: wasm_bindgen::JsValue| { + if login.is_some() { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") } + }; + let init = web_sys::RequestInit::new(); + init.set_method("POST"); + init.set_redirect(web_sys::RequestRedirect::Error); + let headers = web_sys::Headers::new().map_err(err)?; + let login_header = login.as_ref().and_then(|applied| applied.header.as_ref()); + for name in ["Content-Type", "Accept"] { + if !login_header.is_some_and(|(login_name, _)| login_name.eq_ignore_ascii_case(name)) { + headers.set(name, "application/json").map_err(err)?; + } + } + if let Some((name, value)) = login_header { + headers.set(name, value).map_err(err)?; + } + init.set_headers(&headers); + init.set_body(&wasm_bindgen::JsValue::from_str(body)); + if login.is_some() { + init.set_referrer_policy(web_sys::ReferrerPolicy::NoReferrer); + init.set_credentials(web_sys::RequestCredentials::Omit); + } + Ok((window, request.to_string(), init, login.is_some())) + }, + |(window, request, init, authenticated)| (window.fetch_with_str_and_init(&request, &init), authenticated), + ) + .await?; + let err = |e: wasm_bindgen::JsValue| { + if authenticated { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") } + }; + let result = async { + let resp: web_sys::Response = wasm_bindgen_futures::JsFuture::from(promise) .await .map_err(|_| "the browser couldn't reach it (offline, or the server doesn't accept requests from web pages)".to_string())? .dyn_into() @@ -819,8 +855,9 @@ async fn post_json(url: &str, body: &str, capture: Capture) -> Result, S Ok(bytes.to_vec()) } .await; + cache::check_still(&still)?; logins::check(url).map_err(str::to_string)?; - result.map_err(|error| if login.is_some() { logins::scrub_error(&format!("{url}: {error}")) } else { error }) + result.map_err(|error| if authenticated { logins::scrub_error(&format!("{url}: {error}")) } else { error }) } .await; diagnostics.record(answer.map_err(|e| Failure::new(url, e))).map_err(|failure| failure.message) @@ -829,48 +866,68 @@ async fn post_json(url: &str, body: &str, capture: Capture) -> Result, S /// Asks for an address's headers (`HEAD`): a TIFF header's announced length and the type. The /// browser shows a page `X-TIFF-Header-Length` only when the server exposes it /// (`Access-Control-Expose-Headers`); without it, the header is read as any other file's. -async fn head(url: &str, capture: Capture) -> Result { +async fn head( + url: &str, + capture: Capture, + still: Option, +) -> Result { use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache::RequestDiagnostics, failure::Failure}; let mut diagnostics = RequestDiagnostics::with_capture(url, "HEAD", capture); let answer = async { use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache, logins, s3}; - let window = web_sys::window().ok_or("no window")?; - logins::check(url).map_err(str::to_string)?; - let bound = url; - let prepared = s3::prepare(logins::inner(url))?; - // A request signed for S3 is signed for a GET. - if prepared.signed { - return Ok(cache::Head::default()); - } - let url = prepared.url.as_str(); - let target = logins::inherit(bound, url); - let login = logins::apply(&target).map_err(str::to_string)?; - let request = login.as_ref().map_or(url, |applied| applied.url.as_str()); - let result = async { - let err = |e: wasm_bindgen::JsValue| { - if login.is_some() { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") } - }; - let init = web_sys::RequestInit::new(); - init.set_method("HEAD"); - let login_header = login.as_ref().and_then(|applied| applied.header.as_ref()); - if !prepared.headers.is_empty() || login_header.is_some() { - let headers = web_sys::Headers::new().map_err(err)?; - for (k, v) in &prepared.headers { - if !login_header.is_some_and(|(name, _)| name.eq_ignore_ascii_case(k)) { - headers.set(k, v).map_err(err)?; + let sent = crate::request::prepare_and_send( + &still, + || async { + let window = web_sys::window().ok_or("no window")?; + logins::check(url).map_err(str::to_string)?; + let bound = url; + let prepared = s3::prepare(logins::inner(url))?; + // A request signed for S3 is signed for a GET. + if prepared.signed { + return Ok(None); + } + let url = prepared.url.as_str(); + let target = logins::inherit(bound, url); + let login = logins::apply(&target).map_err(str::to_string)?; + let request = login.as_ref().map_or(url, |applied| applied.url.as_str()); + let err = |e: wasm_bindgen::JsValue| { + if login.is_some() { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") } + }; + let init = web_sys::RequestInit::new(); + init.set_method("HEAD"); + let login_header = login.as_ref().and_then(|applied| applied.header.as_ref()); + if !prepared.headers.is_empty() || login_header.is_some() { + let headers = web_sys::Headers::new().map_err(err)?; + for (k, v) in &prepared.headers { + if !login_header.is_some_and(|(name, _)| name.eq_ignore_ascii_case(k)) { + headers.set(k, v).map_err(err)?; + } + } + if let Some((name, value)) = login_header { + headers.set(name, value).map_err(err)?; } + init.set_headers(&headers); } - if let Some((name, value)) = login_header { - headers.set(name, value).map_err(err)?; + if login.is_some() { + init.set_redirect(web_sys::RequestRedirect::Error); + init.set_referrer_policy(web_sys::ReferrerPolicy::NoReferrer); + init.set_credentials(web_sys::RequestCredentials::Omit); } - init.set_headers(&headers); - } - if login.is_some() { - init.set_redirect(web_sys::RequestRedirect::Error); - init.set_referrer_policy(web_sys::ReferrerPolicy::NoReferrer); - init.set_credentials(web_sys::RequestCredentials::Omit); - } - let resp: web_sys::Response = wasm_bindgen_futures::JsFuture::from(window.fetch_with_str_and_init(request, &init)) + Ok(Some((window, request.to_string(), init, url.to_string(), login.is_some()))) + }, + |request| { + request.map(|(window, request, init, url, authenticated)| { + (window.fetch_with_str_and_init(&request, &init), url, authenticated) + }) + }, + ) + .await?; + let Some((promise, reached, authenticated)) = sent else { return Ok(cache::Head::default()) }; + let err = |e: wasm_bindgen::JsValue| { + if authenticated { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") } + }; + let result = async { + let resp: web_sys::Response = wasm_bindgen_futures::JsFuture::from(promise) .await .map_err(|_| "the browser couldn't reach it (offline, or the server doesn't accept requests from web pages)".to_string())? .dyn_into() @@ -888,8 +945,9 @@ async fn head(url: &str, capture: Capture) -> Result Option { fn services(inbox: Inbox, font_inbox: Inbox, notes: Notes, ctx: egui::Context, embedded: bool) -> Services { // Map tiles come through the browser's fetch (the app repaints when they arrive). rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache::set_fetcher_omitting(|url, done, omit| { - let capture = Capture::current(); - wasm_bindgen_futures::spawn_local(async move { - let result = fetch_bytes(&url, capture.clone(), omit).await; - let _scope = capture.enter(); - done(result); - }); + wasm_bindgen_futures::spawn_local(crate::request::handoff( + move |capture, still| async move { fetch_bytes(&url, capture, still, omit).await }, + done, + )); }); // Their pictures are decoded by the browser, off the page's thread. rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache::set_image_decoder(|bytes, done| { @@ -988,21 +1044,17 @@ fn services(inbox: Inbox, font_inbox: Inbox, notes: Notes, ctx: egui::Context, e }); // So are HEAD requests (a GeoTIFF made on request saying how long its header is). rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache::set_header(|url, done| { - let capture = Capture::current(); - wasm_bindgen_futures::spawn_local(async move { - let result = head(&url, capture.clone()).await; - let _scope = capture.enter(); - done(result); - }); + wasm_bindgen_futures::spawn_local(crate::request::handoff( + move |capture, still| async move { head(&url, capture, still).await }, + done, + )); }); // So are JSON documents POSTed (the NLDI's flowtrace). rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache::set_poster(|url, body, done| { - let capture = Capture::current(); - wasm_bindgen_futures::spawn_local(async move { - let result = post_json(&url, &body, capture.clone()).await; - let _scope = capture.enter(); - done(result); - }); + wasm_bindgen_futures::spawn_local(crate::request::handoff( + move |capture, still| async move { post_json(&url, &body, capture, still).await }, + done, + )); }); let open_inbox = inbox.clone(); let font_ctx = ctx.clone(); @@ -1059,7 +1111,7 @@ fn services(inbox: Inbox, font_inbox: Inbox, notes: Notes, ctx: egui::Context, e let (url, inbox, ctx) = (url.to_string(), font_sink.clone(), font_ctx.clone()); let capture = Capture::current(); wasm_bindgen_futures::spawn_local(async move { - match fetch_bytes(&url, capture, &[]).await { + match fetch_bytes(&url, capture, None, &[]).await { Ok(bytes) => { inbox.lock().unwrap_or_else(|e| e.into_inner()).push((url, bytes)); ctx.request_repaint(); @@ -1157,8 +1209,9 @@ fn services(inbox: Inbox, font_inbox: Inbox, notes: Notes, ctx: egui::Context, e fetch: Some(Box::new(move |url: &str, slot| { let (url, ctx) = (url.to_string(), fetch_ctx.clone()); let capture = Capture::current(); + let still = rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache::current_still(); wasm_bindgen_futures::spawn_local(async move { - let r = fetch_bytes(&url, capture, &[]).await.map_err(|e| format!("{url}: {e}")); + let r = fetch_bytes(&url, capture, still, &[]).await.map_err(|e| format!("{url}: {e}")); *slot.lock().unwrap_or_else(|e| e.into_inner()) = Some(r); ctx.request_repaint(); }); diff --git a/crates/analysis/src/control.rs b/crates/analysis/src/control.rs index cb7da5649b..b024cf8f16 100644 --- a/crates/analysis/src/control.rs +++ b/crates/analysis/src/control.rs @@ -66,6 +66,38 @@ impl rustgis_data::progress::Probe for Control { } } +/// Carries the initiating request predicate to the data writer's last commit check as well as +/// its loops. Progress still belongs to the original control; revocation never changes its flag. +struct Progress { + control: Option>, + still: Option, +} + +impl rustgis_data::progress::Probe for Progress { + fn cancelled(&self) -> bool { + self.control.as_ref().is_some_and(|c| c.is_cancelled()) || rustgis_data::source::remote::check_still(&self.still).is_err() + } + fn begin(&self, total: u64) { + if let Some(control) = &self.control { + rustgis_data::progress::Probe::begin(control.as_ref(), total); + } + } + fn advance(&self, n: u64) { + if let Some(control) = &self.control { + rustgis_data::progress::Probe::advance(control.as_ref(), n); + } + } +} + +/// Installs the existing progress carrier with the current owned request predicate. With no +/// explicit control, forwards the enclosing analysis run's, if any; authority is captured even +/// when no run control exists, for a synchronous command's file commit checks. +pub fn install_progress(control: Option>) -> rustgis_data::progress::Installed { + let control = control.or_else(current); + let still = rustgis_data::source::remote::current_still(); + rustgis_data::progress::install(Some(Arc::new(Progress { control, still }))) +} + /// A tool stopped because it was asked to. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Cancelled; @@ -114,6 +146,7 @@ impl Drop for Installed { /// Whether the run on this thread was asked to stop. pub(crate) fn cancelled() -> bool { CURRENT.with(|c| c.borrow().as_ref().is_some_and(|c| c.is_cancelled())) + || rustgis_data::source::remote::check_still(&rustgis_data::source::remote::current_still()).is_err() } /// Whether the run on this thread can be stopped while it goes ([`Control::stoppable`]), and so @@ -191,6 +224,50 @@ pub(crate) fn advance(n: usize) { mod tests { use super::*; + /// Revocation during real file construction must reach the data writer's final rename, + /// even though the person did not press Cancel. Its prior file and the next run survive. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn an_owned_write_rechecks_authority_before_installing_its_file() -> Result<(), Box> { + use std::io::Write; + let dir = std::env::temp_dir().join(format!("rustgis-owned-progress-{}", std::process::id())); + std::fs::create_dir_all(&dir)?; + let path = dir.join("output"); + std::fs::write(&path, b"before")?; + let live = Arc::new(AtomicBool::new(true)); + let current = live.clone(); + let control = Control::new(); + for controlled in [true, false] { + live.store(true, Ordering::SeqCst); + std::fs::write(&path, b"before")?; + let current = current.clone(); + let result = rustgis_data::source::remote::with_still(Arc::new(move || current.load(Ordering::SeqCst)), || { + let write = || { + let written: std::io::Result<()> = rustgis_data::io::write_atomic_with(&path, |out| { + out.write_all(b"replacement")?; + live.store(false, Ordering::SeqCst); + Ok(()) + }); + assert!(written.is_err(), "revocation must stop the actual file commit"); + written.map_err(|e| e.to_string()) + }; + if controlled { + crate::run_under("Export", &control, write) + } else { + let _progress = install_progress(None); + write() + } + }); + assert!(result.is_err()); + assert_eq!(std::fs::read(&path)?, b"before"); + assert!(!control.is_cancelled(), "revocation did not change the user's cancel flag"); + rustgis_data::io::write_atomic(&path, b"next run")?; + assert_eq!(std::fs::read(&path)?, b"next run"); + } + std::fs::remove_dir_all(dir)?; + Ok(()) + } + #[test] fn a_run_sees_its_own_control_and_the_one_before_comes_back() { assert!(current().is_none() && check().is_ok()); diff --git a/crates/analysis/src/lib.rs b/crates/analysis/src/lib.rs index 8867803436..f977953fdb 100644 --- a/crates/analysis/src/lib.rs +++ b/crates/analysis/src/lib.rs @@ -230,6 +230,7 @@ struct Source { provider: ExternalProvider, runner: ExternalRunner, tools: Option<&'static [ToolSpec]>, + generation: u64, } static SOURCES: std::sync::RwLock> = std::sync::RwLock::new(Vec::new()); @@ -241,43 +242,42 @@ pub fn register_external(key: &'static str, provider: ExternalProvider, runner: if let Ok(mut v) = SOURCES.write() && !v.iter().any(|s| s.key == key) { - v.push(Source { key, provider, runner, tools: None }); + v.push(Source { key, provider, runner, tools: None, generation: 0 }); } } /// The tools of the suite registered as `key` (empty when there is none). pub fn external_tools_of(key: &str) -> &'static [ToolSpec] { - let provider = match SOURCES.read() { + if let Ok(v) = SOURCES.read() + && let Some(tools) = v.iter().find(|s| s.key == key).and_then(|s| s.tools) + { + return tools; + } + // Discovery is an explicit loading API. A reload while the provider runs must not + // publish the superseded result as the current schema. + static LOADING: std::sync::Mutex<()> = std::sync::Mutex::new(()); + let Ok(_loading) = LOADING.lock() else { return &[] }; + let (provider, generation) = match SOURCES.read() { Ok(v) => match v.iter().find(|s| s.key == key) { Some(Source { tools: Some(t), .. }) => return t, - Some(s) => s.provider, + Some(s) => (s.provider, s.generation), None => return &[], }, Err(_) => return &[], }; - // One load at a time: a second caller waits for the first instead of loading again. The - // provider runs without the sources' lock held, so a slow one never blocks the others. - static LOADING: std::sync::Mutex<()> = std::sync::Mutex::new(()); - let _loading = LOADING.lock(); - if let Ok(v) = SOURCES.read() - && let Some(Source { tools: Some(t), .. }) = v.iter().find(|s| s.key == key) - { - return t; - } - let tools: &'static [ToolSpec] = Box::leak( - std::panic::catch_unwind(provider) - .unwrap_or_else(|_| { - log::error!("the external tools '{key}' failed to load"); - Vec::new() - }) - .into_boxed_slice(), - ); + let tools = std::panic::catch_unwind(provider).unwrap_or_else(|_| { + log::error!("the external tools '{key}' failed to load"); + Vec::new() + }); match SOURCES.write() { - Ok(mut v) => match v.iter_mut().find(|s| s.key == key) { - Some(s) => s.tools.get_or_insert(tools), - None => tools, + Ok(mut v) => match v.iter_mut().find(|s| s.key == key && s.generation == generation) { + Some(s) => s.tools.get_or_insert_with(|| Box::leak(tools.into_boxed_slice())), + None => { + log::info!("{} external tool schema(s) from '{key}' discarded after its registration changed during loading", tools.len()); + &[] + } }, - Err(_) => tools, + Err(_) => &[], } } @@ -292,6 +292,7 @@ pub fn reload_external() { if let Ok(mut v) = SOURCES.write() { for s in v.iter_mut() { s.tools = None; + s.generation = s.generation.saturating_add(1); } } } @@ -303,6 +304,7 @@ pub fn reload_external_of(key: &str) { && let Some(s) = v.iter_mut().find(|s| s.key == key) { s.tools = None; + s.generation = s.generation.saturating_add(1); } } @@ -340,6 +342,95 @@ pub fn tool(id: &str) -> Option<&'static ToolSpec> { tools().find(by_id).or_else(|| TOOLS.iter().find(by_name)).or_else(|| external_tools().into_iter().find(by_name)) } +/// A tool specification and its runner resolved together, before a later stage starts. +/// Reloading a suite does not replace either part of this value. Plugin callers must also +/// pin the plugin load: the plugins suite's runner alone does not identify that load. +#[derive(Clone, Copy)] +pub struct ResolvedTool { + spec: &'static ToolSpec, + source: Option<&'static str>, + runner: Option, + generation: u64, +} + +impl ResolvedTool { + /// An external spec belongs to the exact currently published load, without name lookup. + pub fn is_current(&self) -> bool { + self.source.is_none_or(|source| { + SOURCES.read().is_ok_and(|v| { + v.iter().any(|s| { + s.key == source + && s.generation == self.generation + && s.tools.is_some_and(|tools| tools.iter().any(|spec| std::ptr::eq(spec, self.spec))) + }) + }) + }) + } + /// The immutable schema captured when this tool was resolved. + pub fn spec(&self) -> &'static ToolSpec { + self.spec + } + + /// The captured external suite, or `None` for a built-in tool. + pub fn source(&self) -> Option<&'static str> { + self.source + } + + /// Resolves defaults using this tool's original schema and suite classification. + pub fn resolve_params(&self, params: &Value) -> Result { + params_for(self.spec, params, self.runner.is_none()) + } +} + +/// Resolves ids before names, with built-in names winning over external names, as [`tool`] +/// does. The owning suite and runner come from the same lookup as its specification. +pub fn resolve(name: &str) -> Option { + if TOOLS.iter().any(|t| t.id.eq_ignore_ascii_case(name)) { + return resolve_published(name); + } + let _ = external_tools(); + resolve_published(name) +} + +/// Published schemas only: this accessor never invokes a provider or performs discovery. +pub fn published_external_tools_of(key: &str) -> &'static [ToolSpec] { + SOURCES.read().ok().and_then(|v| v.iter().find(|s| s.key == key).and_then(|s| s.tools)).unwrap_or(&[]) +} + +/// Lists builtins and currently published external schemas without initializing any source. +pub fn published_tools() -> impl Iterator { + let external: Vec<_> = SOURCES.read().map(|v| v.iter().flat_map(|s| s.tools.unwrap_or(&[]).iter()).collect()).unwrap_or_default(); + TOOLS.iter().chain(external) +} + +/// Resolves only published schemas and captures the same publication's runner and generation. +/// IDs precede names; builtin display names precede external display names. Missing publication +/// remains missing until trusted native startup or an explicitly authorized loading action. +pub fn resolve_published(name: &str) -> Option { + let builtin = |spec| ResolvedTool { spec, source: None, runner: None, generation: 0 }; + if let Some(spec) = TOOLS.iter().find(|t| t.id.eq_ignore_ascii_case(name)) { + return Some(builtin(spec)); + } + let v = SOURCES.read().ok()?; + let external: Vec<_> = v + .iter() + .flat_map(|s| { + s.tools.unwrap_or(&[]).iter().map(move |spec| ResolvedTool { + spec, + source: Some(s.key), + runner: Some(s.runner), + generation: s.generation, + }) + }) + .collect(); + external + .iter() + .find(|r| r.spec.id.eq_ignore_ascii_case(name)) + .copied() + .or_else(|| TOOLS.iter().find(|t| t.name.eq_ignore_ascii_case(name)).map(builtin)) + .or_else(|| external.into_iter().find(|r| r.spec.name.eq_ignore_ascii_case(name))) +} + /// The runner for external tools; never called (external specs carry the runner's id). pub fn external_placeholder(_: &dyn Inputs, _: &Value) -> Result { Err("this tool runs through the external suite".into()) @@ -349,9 +440,12 @@ pub fn external_placeholder(_: &dyn Inputs, _: &Value) -> Result Result { + params_for(spec, params, source_of(spec).is_none()) +} + +/// Applies the captured schema without looking its suite up again after a reload. +fn params_for(spec: &ToolSpec, params: &Value, built_in: bool) -> Result { let mut p = params.as_object().cloned().unwrap_or_default(); - // Not an external suite's tool (a copy of a built-in spec counts as built-in). - let built_in = source_of(spec).is_none(); if built_in { tools::normalize_params(spec, &mut p); } @@ -477,20 +571,41 @@ pub fn run_with(id: &str, inputs: &dyn Inputs, params: &Value, control: &std::sy run_under(tool(id).map_or(id, |t| t.name), control, || run(id, inputs, params)) } +/// Runs the captured specification and suite runner under `control`, without resolving its +/// name again. Plugin execution instead needs the plugin load's own pinned preparation. +pub fn run_resolved( + tool: &ResolvedTool, + inputs: &dyn Inputs, + params: &Value, + control: &std::sync::Arc, +) -> Result { + run_under(tool.spec.name, control, || { + let params = tool.resolve_params(params)?; + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| match tool.runner { + Some(runner) => runner(tool.spec, inputs, ¶ms), + None => (tool.spec.run)(inputs, ¶ms), + })) + .unwrap_or_else(|_| Err(format!("{} failed unexpectedly", tool.spec.name))) + }) +} + /// `work` on this thread under `control`, as [`run_with`] runs a tool, for work that isn't one (an /// engine command's raster): setting `control`'s flag from another thread stops it at its next /// check, its count follows the work's progress, and a cancelled run's error says how far `name` /// came. pub fn run_under(name: &str, control: &std::sync::Arc, work: impl FnOnce() -> Result) -> Result { + let still = rustgis_data::source::remote::current_still(); + rustgis_data::source::remote::check_still(&still)?; if control.is_cancelled() { return Err(format!("{name} was cancelled before it started")); } let _current = control::install(Some(control.clone())); // A file the work writes or reads counts and stops with it too. - let _data = rustgis_data::progress::install(Some(control.clone())); + let _data = control::install_progress(Some(control.clone())); // No count until the work starts one: a control used before doesn't show that run's. control::begin(0); let res = work(); + rustgis_data::source::remote::check_still(&still)?; if !control.is_cancelled() { // Finished: its last step is all done, however it counted. A failed one stays where it // stopped. diff --git a/crates/analysis/src/parallel.rs b/crates/analysis/src/parallel.rs index 539e5ff790..548496c02b 100644 --- a/crates/analysis/src/parallel.rs +++ b/crates/analysis/src/parallel.rs @@ -105,28 +105,31 @@ pub(crate) fn for_each_row( return Ok(()); } let run = control::current(); + let still = rustgis_data::source::remote::current_still(); let rows = std::sync::Mutex::new(rows.enumerate()); let failed = AtomicBool::new(false); let work = || { - let _stop = StopOnPanic(&failed); - let _run = control::install(run.clone()); - let mut s = scratch(); - let mut taken = Vec::with_capacity(BATCH); - while !failed.load(Ordering::Relaxed) && !control::cancelled() { - // The lock is held only to take rows; a poisoned one means a thread panicked. - if let Ok(mut r) = rows.lock() { - let take = (r.len() / threads.saturating_mul(BATCH)).clamp(1, BATCH); - taken.extend(r.by_ref().take(take)); - } - if taken.is_empty() { - break; - } - let n = taken.len(); - for (i, row) in taken.drain(..) { - f(&mut s, i, row); + rustgis_data::source::remote::with_optional_still(still.clone(), || { + let _stop = StopOnPanic(&failed); + let _run = control::install(run.clone()); + let mut s = scratch(); + let mut taken = Vec::with_capacity(BATCH); + while !failed.load(Ordering::Relaxed) && !control::cancelled() { + // The lock is held only to take rows; a poisoned one means a thread panicked. + if let Ok(mut r) = rows.lock() { + let take = (r.len() / threads.saturating_mul(BATCH)).clamp(1, BATCH); + taken.extend(r.by_ref().take(take)); + } + if taken.is_empty() { + break; + } + let n = taken.len(); + for (i, row) in taken.drain(..) { + f(&mut s, i, row); + } + control::advance(n); } - control::advance(n); - } + }) }; std::thread::scope(|scope| { let others: Vec<_> = @@ -182,34 +185,37 @@ pub(crate) fn try_map_on>( .collect(); } let run = control::current(); + let still = rustgis_data::source::remote::current_still(); let next = AtomicUsize::new(0); let failed = AtomicBool::new(false); let work = || { - let _stop = StopOnPanic(&failed); - let _run = control::install(run.clone()); - let mut s = scratch(); - let mut made = Vec::new(); - while !failed.load(Ordering::Relaxed) { - let from = next.fetch_add(BATCH, Ordering::Relaxed); - let Some(batch) = items.get(from..items.len().min(from.saturating_add(BATCH))).filter(|b| !b.is_empty()) else { break }; - // Cancel is looked at, and progress counted, item by item, as on one thread: an item - // can take long (a large zone, say), so a batch of them would hold both back. - for (k, item) in batch.iter().enumerate() { - if control::cancelled() { - failed.store(true, Ordering::Relaxed); - return Err((from + k, Cancelled.into())); - } - match f(&mut s, from + k, item) { - Ok(r) => made.push((from + k, r)), - Err(e) => { + rustgis_data::source::remote::with_optional_still(still.clone(), || { + let _stop = StopOnPanic(&failed); + let _run = control::install(run.clone()); + let mut s = scratch(); + let mut made = Vec::new(); + while !failed.load(Ordering::Relaxed) { + let from = next.fetch_add(BATCH, Ordering::Relaxed); + let Some(batch) = items.get(from..items.len().min(from.saturating_add(BATCH))).filter(|b| !b.is_empty()) else { break }; + // Cancel is looked at, and progress counted, item by item, as on one thread: an item + // can take long (a large zone, say), so a batch of them would hold both back. + for (k, item) in batch.iter().enumerate() { + if control::cancelled() { failed.store(true, Ordering::Relaxed); - return Err((from + k, e)); + return Err((from + k, Cancelled.into())); } + match f(&mut s, from + k, item) { + Ok(r) => made.push((from + k, r)), + Err(e) => { + failed.store(true, Ordering::Relaxed); + return Err((from + k, e)); + } + } + control::advance(1); } - control::advance(1); } - } - Ok(made) + Ok(made) + }) }; let mut parts = Vec::with_capacity(threads); @@ -258,6 +264,38 @@ impl Drop for StopOnPanic<'_> { mod tests { use super::*; + /// The raster row workers inherit the initiating predicate, rather than creating a root + /// request context on a new thread. + #[test] + fn raster_rows_keep_their_request_context() { + let mut rows = vec![0u8; 256]; + rustgis_data::source::remote::with_still(std::sync::Arc::new(|| true), || { + assert_eq!( + for_each_row( + rows.chunks_mut(1), + 4, + || (), + |_, _, row| { + assert!(rustgis_data::source::remote::current_still().is_some()); + row[0] = 1; + } + ), + Ok(()) + ); + let items = try_map_on( + &rows, + 4, + || (), + |_, _, value| { + assert!(rustgis_data::source::remote::current_still().is_some()); + Ok::<_, Cancelled>(*value) + }, + ); + assert_eq!(items, Ok(vec![1; 256])); + }); + assert!(rustgis_data::source::remote::current_still().is_none()); + } + #[test] fn a_work_cap_grows_with_the_threads_a_run_takes() { assert_eq!(work_cap(400, 1), 400); diff --git a/crates/analysis/tests/resolved_tools.rs b/crates/analysis/tests/resolved_tools.rs new file mode 100644 index 0000000000..857ec913d4 --- /dev/null +++ b/crates/analysis/tests/resolved_tools.rs @@ -0,0 +1,119 @@ +//! A delayed run must keep its original tool schema and runner after a suite reload. + +use std::sync::atomic::{AtomicBool, Ordering}; + +use rustgis_analysis::{Inputs, Output, ParamKind, ParamSpec, ToolResult, ToolSpec}; +use serde_json::{Value, json}; + +// Both fixtures publish into the process-wide registry; discovery visits every suite. +static REGISTRY: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +static DISCOVERIES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); +static RELOADED: AtomicBool = AtomicBool::new(false); + +/// No layer input is needed by the synthetic suite. +struct Empty; + +impl Inputs for Empty { + /// The synthetic runner receives no borrowed project data. + fn dataset(&self, _: &str) -> Option<&rustgis_data::Dataset> { + None + } + + /// No selected rows can affect this fixture's output. + fn selection(&self, _: &str) -> &[usize] { + &[] + } +} + +const OLD: &[ParamSpec] = &[ParamSpec { name: "value", label: "Value", kind: ParamKind::Number, required: false, default: "1", help: "" }]; +const NEW: &[ParamSpec] = &[ParamSpec { name: "value", label: "Value", kind: ParamKind::Number, required: false, default: "2", help: "" }]; + +/// Reloading changes both the schema default and the runner-visible specification. +fn provider() -> Vec { + DISCOVERIES.fetch_add(1, Ordering::SeqCst); + let reloaded = RELOADED.load(Ordering::Relaxed); + vec![ToolSpec { + id: "resolved-test-tool", + name: rustgis_analysis::TOOLS[0].name, + toolbox: "Test", + summary: if reloaded { "new" } else { "old" }, + params: if reloaded { NEW } else { OLD }, + run: rustgis_analysis::external_placeholder, + }] +} + +/// Reports which captured schema and defaults actually reached the runner. +fn runner(spec: &ToolSpec, _: &dyn Inputs, params: &Value) -> Result { + Ok(ToolResult { output: Output::Layers(Vec::new()), messages: vec![format!("{}:{}", spec.summary, params["value"])] }) +} + +#[test] +fn resolved_tool_keeps_schema_after_reload_and_honors_cancellation() { + let _registry = REGISTRY.lock().unwrap(); + rustgis_analysis::register_external("resolved-test-suite", provider, runner); + assert!(rustgis_analysis::resolve_published("resolved-test-tool").is_none()); + assert!(rustgis_analysis::published_external_tools_of("resolved-test-suite").is_empty()); + assert!(!rustgis_analysis::published_tools().any(|t| t.id == "resolved-test-tool")); + assert_eq!(DISCOVERIES.load(Ordering::SeqCst), 0); + let original = rustgis_analysis::resolve("RESOLVED-TEST-TOOL").unwrap(); + assert_eq!(original.source(), Some("resolved-test-suite")); + assert_eq!(original.resolve_params(&json!({})).unwrap(), json!({"value": 1})); + // Built-in names still win over the external name; ids are considered first. + let builtin = &rustgis_analysis::TOOLS[0]; + assert_eq!(rustgis_analysis::resolve(builtin.name).unwrap().spec().id, builtin.id); + + RELOADED.store(true, Ordering::Relaxed); + rustgis_analysis::reload_external_of("resolved-test-suite"); + assert!(!original.is_current()); + assert!(rustgis_analysis::resolve_published("resolved-test-tool").is_none()); + assert_eq!(DISCOVERIES.load(Ordering::SeqCst), 1, "current/review must not rediscover"); + let current = rustgis_analysis::resolve("resolved-test-tool").unwrap(); + let control = rustgis_analysis::control::Control::new(); + assert_eq!(rustgis_analysis::run_resolved(&original, &Empty, &json!({}), &control).unwrap().messages, ["old:1"]); + assert_eq!(rustgis_analysis::run_resolved(¤t, &Empty, &json!({}), &control).unwrap().messages, ["new:2"]); + + control.cancel(); + assert!(rustgis_analysis::run_resolved(&original, &Empty, &json!({}), &control).unwrap_err().contains("cancelled before")); +} + +/// A provider finishing across reload cannot publish its superseded snapshot as current. +#[test] +fn permission_policy_provider_publication_rejects_in_flight_reload() { + let _registry = REGISTRY.lock().unwrap(); + use std::sync::{Condvar, Mutex}; + static STATE: (Mutex<(bool, bool)>, Condvar) = (Mutex::new((false, false)), Condvar::new()); + fn held() -> Vec { + let (state, ready) = &STATE; + let mut state = state.lock().unwrap(); + state.0 = true; + ready.notify_all(); + while !state.1 { + state = ready.wait(state).unwrap(); + } + vec![ToolSpec { + id: "stale-publication-tool", + name: "Stale publication", + toolbox: "Test", + summary: "held", + params: OLD, + run: rustgis_analysis::external_placeholder, + }] + } + rustgis_analysis::register_external("held-publication", held, runner); + let loading = std::thread::spawn(|| rustgis_analysis::external_tools_of("held-publication")); + let (state, ready) = &STATE; + let mut state = state.lock().unwrap(); + while !state.0 { + state = ready.wait(state).unwrap(); + } + rustgis_analysis::reload_external_of("held-publication"); + state.1 = true; + ready.notify_all(); + drop(state); + assert!(loading.join().unwrap().is_empty()); + assert!(rustgis_analysis::resolve_published("stale-publication-tool").is_none()); + assert!(rustgis_analysis::published_external_tools_of("held-publication").is_empty()); + assert_eq!(rustgis_analysis::external_tools_of("held-publication").len(), 1); + assert!(rustgis_analysis::resolve_published("stale-publication-tool").unwrap().is_current()); +} diff --git a/crates/assistant/Cargo.toml b/crates/assistant/Cargo.toml index 709b840a50..738c788b65 100644 --- a/crates/assistant/Cargo.toml +++ b/crates/assistant/Cargo.toml @@ -33,3 +33,7 @@ zip = { version = "9", default-features = false, features = ["deflate"] } # The relay's token folder and file must belong to the user running the process. [target.'cfg(unix)'.dependencies] rustix = { version = "1", features = ["process"] } + +[dev-dependencies] +# Exercise the existing real Wasm fixture through Agent and MCP entry points. +rustgis-engine = { workspace = true, features = ["testing"] } diff --git a/crates/assistant/src/acp.rs b/crates/assistant/src/acp.rs index 946438d841..5d0f27e555 100644 --- a/crates/assistant/src/acp.rs +++ b/crates/assistant/src/acp.rs @@ -135,12 +135,10 @@ enum Phase { Finished, } -/// The request ids of the three requests RustGIS makes. +/// The initial request ids; later prompts and mode changes share a monotonic allocator. const INIT: u64 = 0; const NEW: u64 = 1; const PROMPT: u64 = 2; -/// The id of a `session/set_mode` request (one at a time). -const SET_MODE: u64 = 1_000_000; /// The agents' modes that act without asking (Codex, Claude Code, others'). const FULL_MODES: &[&str] = &["agent-full-access", "bypassPermissions", "full-access", "yolo"]; @@ -149,6 +147,32 @@ const ASKING_MODES: &[&str] = &["agent", "default", "ask", "code", "workspace-wr /// Their modes that can't do the work (read only, planning). const IDLE_MODES: &[&str] = &["read-only", "plan"]; +/// What the advertised, confirmed mode lets the agent do. Missing or malformed mode data +/// never establishes that the agent asks before acting. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum ModeClass { + Asking, + Bypass, + Idle, + Unknown, + None, +} + +/// Readiness for the latest automatic-approval setting, rather than for an earlier request. +enum Ready { + Now, + Switch(String), + Refuse, +} + +/// Only a response with this identity can confirm this mode change. +#[derive(Debug)] +struct Negotiation { + id: u64, + mode: String, + started: web_time::Instant, +} + /// One task's ACP session with an agent. #[derive(Debug)] pub struct Session { @@ -160,6 +184,9 @@ pub struct Session { session_id: Option, /// The id of the task's `session/prompt`: each follow-up gets the next one. prompt_id: u64, + next_id: u64, + /// A cancelled turn still owns its prompt until the agent answers it. + stop_reason: Option, /// The message being streamed, and the last whole one. text: String, last: String, @@ -170,9 +197,9 @@ pub struct Session { /// The agent's modes, and the one it's in. modes: Vec, mode: Option, - /// The task, waiting for a mode change to be answered; and the mode asked for. + /// The task held until its current mode negotiation succeeds. queued: Option, - asked_mode: Option, + asked_mode: Option, } /// The longest message kept from an agent (bytes): the rest is cut. @@ -221,6 +248,8 @@ impl Session { phase: Phase::Initializing, session_id: None, prompt_id: PROMPT, + next_id: PROMPT + 1, + stop_reason: None, text: String::new(), last: String::new(), tools: Vec::new(), @@ -259,7 +288,7 @@ impl Session { out.push(json!({"jsonrpc": "2.0", "id": id, "error": {"code": -32601, "message": format!("RustGIS doesn't provide {method}")}}).to_string()); } } - (Some("session/update"), None) => events.extend(self.update(msg.pointer("/params/update").unwrap_or(&Value::Null))), + (Some("session/update"), None) => events.extend(self.update(msg.pointer("/params/update").unwrap_or(&Value::Null), &mut out)), // A response to one of RustGIS's requests. (None, Some(id)) => self.response(id.as_u64(), &msg, &mut out, &mut events), _ => {} @@ -267,14 +296,30 @@ impl Session { (out, events) } + /// Accept only the current negotiation or turn reply; a failed downgrade never releases work. fn response(&mut self, id: Option, msg: &Value, out: &mut Vec, events: &mut Vec) { - // A mode change answered (or refused: the task goes ahead in the mode it's in). - if id == Some(SET_MODE) && self.asked_mode.is_some() && self.phase == Phase::Working && self.queued.is_some() { - let asked = self.asked_mode.take(); - if msg.get("error").is_none() { - self.mode = asked; + // A stale response cannot change the confirmed mode or release a held prompt. + if self.asked_mode.as_ref().is_some_and(|asked| Some(asked.id) == id) { + let Some(asked) = self.asked_mode.take() else { return }; + if msg.get("error").is_none() && msg.get("result").is_some_and(Value::is_object) { + self.mode = Some(asked.mode); + out.extend(self.release_prompt(false, events)); + } else { + // An invalid response doesn't establish whether the requested change was + // applied. In particular, don't retain an old asking mode after an upgrade. + if !msg.get("error").is_some_and(Value::is_object) { + self.mode = None; + } + if self.queued.is_some() { + if self.auto { + // Upgrading is best effort: automatic approval still permits work. + // Downgrading with auto off is never best effort. + out.extend(self.release_prompt(false, events)); + } else { + events.extend(self.refuse_mode("the agent refused the asking mode")); + } + } } - out.extend(self.queued.take()); return; } // Ignore stale or unsolicited responses before interpreting success or failure. @@ -333,24 +378,32 @@ impl Session { }; self.session_id = Some(sid.to_string()); self.phase = Phase::Working; - let modes = result.pointer("/modes/availableModes").and_then(Value::as_array).into_iter().flatten(); - self.modes = modes.filter_map(|m| m.get("id").and_then(Value::as_str).map(str::to_string)).collect(); + self.modes = result + .pointer("/modes/availableModes") + .and_then(Value::as_array) + .and_then(|modes| modes.iter().map(|m| m.get("id")?.as_str().map(str::to_string)).collect::>>()) + .unwrap_or_default(); self.mode = result.pointer("/modes/currentModeId").and_then(Value::as_str).map(str::to_string); let prompt = rpc( PROMPT, "session/prompt", json!({"sessionId": sid, "prompt": [{"type": "text", "text": format!("{BRIEF}{}", self.task)}]}), ); - out.extend(self.with_mode(prompt)); + self.queued = Some(prompt); + out.extend(self.release_prompt(true, events)); } (Some(id), Phase::Working) if id == self.prompt_id => { let stop = result.get("stopReason").and_then(Value::as_str).unwrap_or("end_turn"); events.extend(self.flush()); - let (report, error) = match stop { - "end_turn" => (self.last.clone(), false), - "cancelled" => (format!("{} was stopped.", self.label), true), - "refusal" => (format!("{} refused the task. {}", self.label, self.last), true), - _ => (format!("{} stopped early ({stop}). {}", self.label, self.last), true), + let (report, error) = if let Some(why) = self.stop_reason.take() { + (why, true) + } else { + match stop { + "end_turn" => (self.last.clone(), false), + "cancelled" => (format!("{} was stopped.", self.label), true), + "refusal" => (format!("{} refused the task. {}", self.label, self.last), true), + _ => (format!("{} stopped early ({stop}). {}", self.label, self.last), true), + } }; events.extend(self.turn_done(report, error)); } @@ -358,41 +411,159 @@ impl Session { } } - /// The mode the agent should be in for the next task, if not the one it's in: its full-access - /// mode when the user lets it act without asking; otherwise a mode that works but asks. - fn wanted_mode(&self) -> Option { - let has = |m: &&str| self.modes.iter().any(|x| x == m); - let current = self.mode.as_deref().unwrap_or(""); - let want = if self.auto { - FULL_MODES.iter().copied().find(has) - } else if current.is_empty() || FULL_MODES.contains(¤t) || IDLE_MODES.contains(¤t) { - ASKING_MODES.iter().copied().find(has) + /// Classify only confirmed mode data from a valid advertised mode list. + fn mode_class(&self) -> ModeClass { + let Some(mode) = self.mode.as_deref().filter(|_| !self.modes.is_empty()) else { return ModeClass::None }; + if ASKING_MODES.contains(&mode) { + ModeClass::Asking + } else if FULL_MODES.contains(&mode) { + ModeClass::Bypass + } else if IDLE_MODES.contains(&mode) { + ModeClass::Idle } else { - None - }; - want.filter(|m| *m != current).map(|m| m.to_string()) + ModeClass::Unknown + } } - /// The lines that start a task: a mode change first when one is needed (the task waits for - /// its answer), then the task. - fn with_mode(&mut self, prompt: String) -> Vec { - let (Some(mode), Some(sid)) = (self.wanted_mode(), self.session_id.clone()) else { return vec![prompt] }; - self.queued = Some(prompt); - self.asked_mode = Some(mode.clone()); - vec![rpc(SET_MODE, "session/set_mode", json!({"sessionId": sid, "modeId": mode}))] + /// Recompute readiness under the current setting. Automatic upgrades are attempted only + /// before a task, so a refused optional upgrade doesn't prevent an auto-on task running. + fn ready(&self, upgrade: bool) -> Ready { + let offered = |modes: &[&str]| modes.iter().find(|m| self.modes.iter().any(|x| x == **m)).map(|m| (*m).to_string()); + if self.auto { + return match offered(FULL_MODES).filter(|m| upgrade && Some(m.as_str()) != self.mode.as_deref()) { + Some(mode) => Ready::Switch(mode), + None => Ready::Now, + }; + } + if self.mode_class() == ModeClass::Asking { + return Ready::Now; + } + match offered(ASKING_MODES) { + Some(mode) => Ready::Switch(mode), + None if self.mode_class() == ModeClass::Idle => Ready::Now, + None => Ready::Refuse, + } + } + + /// Allocate distinct JSON-RPC ids for prompts and mode changes; exhaustion fails closed. + fn request_id(&mut self) -> Option { + let id = self.next_id; + self.next_id = id.checked_add(1)?; + Some(id) } - /// Whether the user lets the agent act without asking, for its next task. - pub fn set_auto(&mut self, auto: bool) { + /// Ask for a mode under a new identity, with at most one change in flight. A sent + /// change can still be applied by the agent even if its response is ignored locally. + fn switch_mode(&mut self, mode: String) -> Option { + if self.asked_mode.is_some() { + return None; + } + let sid = self.session_id.clone()?; + let id = self.request_id()?; + self.asked_mode = Some(Negotiation { id, mode: mode.clone(), started: web_time::Instant::now() }); + Some(rpc(id, "session/set_mode", json!({"sessionId": sid, "modeId": mode}))) + } + + /// Only a fresh readiness check releases work. Even an already-safe current mode waits + /// while a switch is in flight: that switch could still put the agent in bypass mode. + fn release_prompt(&mut self, upgrade: bool, events: &mut Vec) -> Vec { + if self.queued.is_none() || self.asked_mode.is_some() { + return Vec::new(); + } + match self.ready(upgrade) { + Ready::Now => self.queued.take().into_iter().collect(), + Ready::Switch(mode) => match self.switch_mode(mode) { + Some(line) => vec![line], + None => { + events.extend(self.refuse_mode("a mode request could not be started")); + Vec::new() + } + }, + Ready::Refuse => { + let why = if self.modes.is_empty() { + "the agent advertised no modes, so RustGIS can't confirm that it asks before acting" + } else { + "RustGIS can't confirm that the agent asks before acting" + }; + events.extend(self.refuse_mode(why)); + Vec::new() + } + } + } + + /// Refuse this held task visibly, retaining the session for a safely negotiated follow-up. + fn refuse_mode(&mut self, why: &str) -> Vec { + self.queued = None; + let mode = self.mode.as_deref().unwrap_or("unreported"); + self.turn_done( + format!("{}: {why} (mode: {mode}). Change the agent's mode, or turn automatic approval on, then try again.", self.label), + true, + ) + } + + /// Cancel a running turn whose mode no longer meets the setting, and negotiate asking. + /// Cancellation can't recall actions already performed, so keep waiting for the turn's reply. + fn contain_running(&mut self, why: &str, events: &mut Vec) -> Vec { + if self.auto + || self.phase != Phase::Working + || self.queued.is_some() + || self.stop_reason.is_some() + || matches!(self.mode_class(), ModeClass::Asking | ModeClass::Idle) + { + return Vec::new(); + } + let mut out: Vec = self.cancel().into_iter().collect(); + let report = format!("{} was stopped: {why}.", self.label); + self.stop_reason = Some(report.clone()); + events.extend(self.flush()); + events.push(Event::Text(format!("Stopping {}: {why}.", self.label))); + // Do not overlap mode changes: an earlier change could otherwise apply after + // the downgrade. Held follow-ups wait for it and then check readiness again. + if self.asked_mode.is_none() + && let Ready::Switch(mode) = self.ready(false) + { + out.extend(self.switch_mode(mode)); + } + out + } + + /// Apply automatic approval immediately. Turning it off cancels an unsafe running turn; + /// held work is re-negotiated and never released by an earlier upgrade's response. + /// Callers send the lines in order and display the returned events. + pub fn set_auto(&mut self, auto: bool) -> (Vec, Vec) { + if self.auto == auto { + return (Vec::new(), Vec::new()); + } self.auto = auto; + let mut events = Vec::new(); + if self.queued.is_some() { + let out = self.release_prompt(true, &mut events); + return (out, events); + } + let out = self.contain_running("automatic approval was turned off", &mut events); + (out, events) + } + + /// Retire a session after an unanswered negotiation. A timed-out request can still + /// change the agent's mode later, so this session must never receive another task. + pub fn poll(&mut self) -> Vec { + if !self.asked_mode.as_ref().is_some_and(|a| a.started.elapsed() >= web_time::Duration::from_secs(60)) { + return Vec::new(); + } + self.mode = None; + self.finish_with(format!("{}: the mode request timed out. Start a new agent thread before trying again.", self.label), true) } - fn update(&mut self, u: &Value) -> Vec { + /// Track mode notifications as well as replies: an auto-off turn entering an unsafe mode + /// must be cancelled even when RustGIS didn't request that change. + fn update(&mut self, u: &Value, out: &mut Vec) -> Vec { let s = |k: &str| u.get(k).and_then(Value::as_str).unwrap_or("").to_string(); match u.get("sessionUpdate").and_then(Value::as_str) { Some("current_mode_update") => { self.mode = u.get("currentModeId").and_then(Value::as_str).map(str::to_string); - Vec::new() + let mut events = Vec::new(); + out.extend(self.contain_running("the agent entered a mode that doesn't confirm approval", &mut events)); + events } Some("agent_message_chunk") => { if let Some(t) = u.pointer("/content/text").and_then(Value::as_str) { @@ -495,19 +666,23 @@ impl Session { Some(json!({"jsonrpc": "2.0", "method": "session/cancel", "params": {"sessionId": sid}}).to_string()) } - /// A follow-up task for the thread, once it has finished the last one: the lines to send (a - /// mode change first, when the user's setting changed since). - pub fn follow_up(&mut self, task: &str) -> Option> { + /// A follow-up task for the thread after its last prompt finishes. It uses the same + /// readiness gate as the first prompt; a refusal is returned as an event with no prompt. + pub fn follow_up(&mut self, task: &str) -> Option<(Vec, Vec)> { let sid = self.session_id.clone().filter(|_| self.phase == Phase::Idle)?; + let id = self.request_id()?; self.phase = Phase::Working; - self.prompt_id += 1; + self.prompt_id = id; self.last.clear(); let prompt = rpc( self.prompt_id, "session/prompt", json!({"sessionId": sid, "prompt": [{"type": "text", "text": format!("{FOLLOW_UP}{task}")}]}), ); - Some(self.with_mode(prompt)) + self.queued = Some(prompt); + let mut events = Vec::new(); + let out = self.release_prompt(true, &mut events); + Some((out, events)) } /// The agent's process ended: its report, if it was working, or that the thread closed. @@ -543,12 +718,19 @@ impl Session { /// A task ended; the thread waits for a follow-up. fn turn_done(&mut self, report: String, error: bool) -> Vec { self.phase = Phase::Idle; + self.queued = None; + self.stop_reason = None; let report = if report.trim().is_empty() { format!("{} finished without a report.", self.label) } else { report }; vec![Event::Done { report, error }] } + /// End this session permanently, discarding held work and pending negotiations so + /// late replies cannot revive it or authorize a follow-up. fn finish_with(&mut self, report: String, error: bool) -> Vec { self.phase = Phase::Finished; + self.queued = None; + self.asked_mode = None; + self.stop_reason = None; let report = if report.trim().is_empty() { format!("{} finished without a report.", self.label) } else { report }; vec![Event::Done { report, error }] } @@ -626,6 +808,33 @@ pub fn installed() -> Installed { (ready, need_node) } +#[cfg(test)] +mod mode_tests { + use super::*; + + #[test] + fn a_timed_out_mode_request_retires_the_session_even_between_turns() { + for held in [true, false] { + let (mut s, _) = Session::new("Agent", "Map it", "/w", vec![], !held); + s.line(r#"{"id":0,"result":{"protocolVersion":1}}"#); + s.line(r#"{"id":1,"result":{"sessionId":"s","modes":{"currentModeId":"agent-full-access","availableModes":[{"id":"agent"},{"id":"agent-full-access"}]}}}"#); + if !held { + s.set_auto(false); + s.line(r#"{"id":2,"result":{"stopReason":"cancelled"}}"#); + assert!(s.idle()); + } + let asked = s.asked_mode.as_mut().unwrap(); + let id = asked.id; + asked.started = asked.started.checked_sub(web_time::Duration::from_secs(61)).unwrap(); + let events = s.poll(); + assert!(events.iter().any(|e| matches!(e, Event::Done { error: true, report } if report.contains("timed out")))); + assert!(s.finished() && s.follow_up("Try again").is_none()); + assert_eq!(s.line(&json!({"id": id, "result": {}}).to_string()), (vec![], vec![])); + assert!(s.poll().is_empty()); + } + } +} + #[cfg(all(test, not(target_arch = "wasm32")))] mod lookup_tests { #[test] diff --git a/crates/assistant/src/agent.rs b/crates/assistant/src/agent.rs index 2a337d25c9..ce455c9171 100644 --- a/crates/assistant/src/agent.rs +++ b/crates/assistant/src/agent.rs @@ -7,6 +7,12 @@ use std::collections::VecDeque; use crate::provider::{self, Config, HttpRequest, HttpResponse, Note, ToolCall, ToolResult, Turn}; use crate::{acp, compact, skills, tools}; use rustgis_engine::Session; +use rustgis_engine::policy::{Outcome, Pending, Permit, Remembered, Scope}; +use std::sync::{ + Arc, + atomic::{AtomicU64, Ordering}, +}; +static CONVERSATIONS: AtomicU64 = AtomicU64::new(1); /// Model requests per user message before the agent stops and reports back. pub const MAX_STEPS: u32 = 16; @@ -91,8 +97,11 @@ pub struct Agent { last_context: String, /// Goes up when a run is stopped or cleared: responses to older requests are dropped. generation: u64, - /// Commands approved for the rest of the conversation (see [`tools::approval_key`]). - always: Vec, + /// Exact operations approved for the rest of this conversation and project generation. + always: Vec, + pending_policy: Option, + conversation: u64, + live: Arc, system: String, /// Retries of the request out ([`MAX_RETRIES`]). retries: u32, @@ -171,6 +180,9 @@ impl Agent { last_context: String::new(), generation: 0, always: Vec::new(), + pending_policy: None, + conversation: CONVERSATIONS.fetch_add(1, Ordering::Relaxed), + live: Arc::new(tools::Live::default()), system: base_system.clone() + &skills::catalog(&skills), base_system, skills, @@ -291,6 +303,7 @@ impl Agent { child.depth = self.depth + 1; child.allowed = allowed; child.always = self.always.clone(); + child.conversation = self.conversation; child.no_reasoning = self.no_reasoning.clone(); // The budget is the main assistant's: what's left of it for this message. child.max_steps = self.max_steps.saturating_sub(self.steps).max(1); @@ -366,13 +379,22 @@ impl Agent { } return Some((what, "it starts or follows up a local agent with its own file, network and execution tools".into(), false)); } - let reason = tools::gate(s, call)?; - (!self.approved(s, call)).then(|| (tools::summary(call), reason, tools::approval_key(s, call).is_some())) + let _ = s; + let review = self.pending_policy.as_ref()?.review(); + let details = review.details.iter().flat_map(|d| d.targets.iter().chain(&d.places)).cloned().collect::>().join("\n"); + Some((format!("{}\n{}\n{}", review.what, review.steps.join("\n"), details), review.why.clone(), review.remember)) } - /// Whether the user allowed calls like this one for the rest of the conversation. - fn approved(&self, s: &Session, call: &ToolCall) -> bool { - tools::approval_key(s, call).is_some_and(|k| self.always.contains(&k)) + /// The exact displayed engine card, with structured details for translated UI presentation. + pub fn pending_review(&self) -> Option<&rustgis_engine::policy::Review> { + if let Some(child) = &self.child { + return child.pending_review(); + } + self.pending_policy.as_ref().map(|p| p.review()) + } + + fn scope(&self) -> Scope { + Scope::assistant(self.conversation, self.live.clone()) } /// Changes in the generation tell a caller its response is stale. @@ -390,6 +412,10 @@ impl Agent { self.steps = 0; self.last_context.clear(); self.always.clear(); + self.pending_policy = None; + self.live.0.store(false, Ordering::SeqCst); + self.live = Arc::new(tools::Live::default()); + self.conversation = CONVERSATIONS.fetch_add(1, Ordering::Relaxed); self.retries = 0; self.retrying_summary = None; self.preview = None; @@ -406,10 +432,14 @@ impl Agent { } fn ask_unguarded(&mut self, text: &str, s: &Session) -> Step { + self.always.retain(|r| r.belongs_to_generation(s.project_generation())); let text = text.trim(); if text.is_empty() || self.busy() { return Step::Done; } + if !self.live.0.load(Ordering::SeqCst) { + self.live = Arc::new(tools::Live::default()); + } self.transcript.push(Entry::User(text.to_string())); let text = compact::escape_user(text); let ctx = compact::escape_user(&tools::context(s)); @@ -486,7 +516,7 @@ impl Agent { } /// Runs a call: the agent's own tools, else the session's. - fn run_tool(&self, s: &mut Session, call: &ToolCall, tokens: usize) -> Result { + fn run_tool(&self, s: &mut Session, call: &ToolCall, tokens: usize, permit: Option) -> Result { if !self.allowed.is_empty() && call.name != "load_skill" && !self.allowed.contains(&call.name) { return Err(format!("'{}' isn't one of this subagent's tools", call.name)); } @@ -496,7 +526,7 @@ impl Agent { let markdown = skills::load(&self.skills, name)?; Ok(tools::bounded_json(&serde_json::json!({"name":name,"markdown":markdown}), tokens)) } - _ => tools::run_with_budget(s, call, tokens), + _ => tools::run_with_budget(s, call, tokens, self.scope(), permit), } } @@ -734,8 +764,26 @@ impl Agent { } return self.start_child(s, &call); } - if tools::gate(s, &call).is_some() && !self.approved(s, &call) { - return Step::Confirm; + match tools::policy_call(&call) { + Ok(Some(engine)) => match s.review(&self.scope(), &engine) { + Outcome::Allowed => {} + Outcome::NeedsApproval(pending) => { + let covered = self.always.iter().any(|r| pending.covered_by(r)); + self.pending_policy = Some(pending); + if !covered { + return Step::Confirm; + } + } + Outcome::Refused(why) => { + self.record_front(why.clone(), why, true); + continue; + } + }, + Err(why) => { + self.record_front(why.clone(), why, true); + continue; + } + Ok(None) => {} } self.run_front(s, true); } @@ -758,8 +806,9 @@ impl Agent { // Off the queue before it runs: a call that panics is interrupted, not left to run. let State::Calls(q, _) = &mut self.state else { return }; let Some(call) = q.pop_front() else { return }; + let permit = self.pending_policy.take().filter(|_| allow).map(Pending::allow); let (content, error) = if allow { - match self.run_tool(s, &call, tokens) { + match self.run_tool(s, &call, tokens, permit) { Ok(c) => (c, false), Err(e) => (tools::bounded_json(&serde_json::json!({"error": e}), tokens), true), } @@ -872,9 +921,10 @@ impl Agent { // Its steps count against this message's budget; its "always" approvals stay. self.steps = self.steps.saturating_add(child.steps); for key in &child.always { - if !self.always.contains(key) { - self.always.push(key.clone()); + if self.always.len() >= 64 { + self.always.remove(0); } + self.always.push(key.clone()); } let (report, error) = child.report(); self.record_front(report.clone(), report, error); @@ -933,10 +983,12 @@ impl Agent { } if allow && always - && let State::Calls(q, _) = &self.state - && let Some(key) = q.front().and_then(|c| tools::approval_key(s, c)) + && let Some(remembered) = self.pending_policy.as_ref().and_then(Pending::remember) { - self.always.push(key); + if self.always.len() >= 64 { + self.always.remove(0); + } + self.always.push(remembered); } self.run_front(s, allow); self.drain(s) @@ -959,6 +1011,8 @@ impl Agent { /// Stops the run: a request out is forgotten, calls not yet run are skipped. pub fn stop(&mut self) { + self.live.0.store(false, Ordering::SeqCst); + self.pending_policy = None; if !self.busy() { return; } @@ -1001,6 +1055,8 @@ impl Agent { /// queued get the result `queued`; a call that was running when the run ended (it has left the /// queue but has no result) gets `interrupted`. fn halt(&mut self, why: Entry, queued: &str, interrupted: &str) { + self.live.0.store(false, Ordering::SeqCst); + self.pending_policy = None; self.child = None; self.retrying_summary = None; self.retries = 0; @@ -1164,3 +1220,9 @@ pub fn run_blocking>( }; } } + +impl Drop for Agent { + fn drop(&mut self) { + self.live.0.store(false, Ordering::SeqCst); + } +} diff --git a/crates/assistant/src/filebridge.rs b/crates/assistant/src/filebridge.rs index c4e39ff51d..1c75e03635 100644 --- a/crates/assistant/src/filebridge.rs +++ b/crates/assistant/src/filebridge.rs @@ -281,7 +281,24 @@ impl FileBridge { } let entered = s.active_layer; s.actor = Some(BRIDGE_ACTOR.to_string()); - let text = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| server.handle(s, &call))).ok().flatten(); + let access = if server.full_access { + rustgis_engine::policy::AgentAccess::Full + } else if server.allow_access { + rustgis_engine::policy::AgentAccess::Allow + } else { + rustgis_engine::policy::AgentAccess::Refuse + }; + let scope = rustgis_engine::policy::Scope::agent( + access, + true, + BRIDGE_ACTOR, + Some(BRIDGE_ACTOR.into()), + Arc::new(BridgeLive(Arc::downgrade(&self.inner))), + ); + let text = + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| server.handle_scoped(s, &call, scope, Vec::new(), "bridge"))) + .ok() + .flatten(); s.actor = None; if s.active_layer != entered || (crate::relay::selects_layer(&call) && crate::relay::reply_ok(text.as_deref())) { *lock(&self.inner.active) = s.active_layer; @@ -372,6 +389,26 @@ fn write_atomic(dir: &Path, name: &str, text: &str) -> Result<(), String> { }) } +struct BridgeLive(std::sync::Weak); +impl rustgis_engine::policy::Revocation for BridgeLive { + fn allows(&self, e: rustgis_engine::policy::Effects) -> bool { + use rustgis_engine::policy::Effect; + let Some(inner) = self.0.upgrade() else { + return false; + }; + if inner.stopped.load(SeqCst) { + return false; + } + if e.contains(Effect::Reserved) || e.contains(Effect::Automation) || e.contains(Effect::Replaces) { + return inner.full_access.load(SeqCst); + } + if e.contains(Effect::Files) || e.contains(Effect::Network) || e.contains(Effect::Plugin) { + return inner.allow_access.load(SeqCst) || inner.full_access.load(SeqCst); + } + true + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/assistant/src/mcp.rs b/crates/assistant/src/mcp.rs index 31cb390a53..14473fc241 100644 --- a/crates/assistant/src/mcp.rs +++ b/crates/assistant/src/mcp.rs @@ -12,8 +12,9 @@ //! network use left is drawing web tile layers that the project the user opened already has. use rustgis_engine::Session; -use rustgis_engine::cmd::plugins::{Reach, reaches, user_only}; +use rustgis_engine::policy::{AgentAccess, Effects, Outcome, Permit, Scope}; use serde_json::{Value, json}; +use std::sync::Arc; use crate::provider::ToolCall; use crate::{docs, tools}; @@ -81,10 +82,11 @@ const INVALID_PARAMS: i64 = -32602; /// MCP's code for a resource that doesn't exist. const RESOURCE_NOT_FOUND: i64 = -32002; -/// Commands that replace the project. They are reserved to the user in the desktop app, to keep -/// their work, and refused on its relay ([`Server::users_project`]); in `rustgis-cli mcp` the -/// session is the client's own. -const PROJECT_COMMANDS: &[&str] = &["project.new", "project.open", "project.openText", "project.sample", "project.fromTemplate"]; +/// Binds an ingress request to its complete-message position, independently of wire ids. +/// Count batch entries before filtering; an unbatched message always uses position zero. +pub(crate) fn request_key(root: &str, position: usize) -> String { + format!("{root}/{position}") +} impl Server { /// A server with the built-in skills. `rustgis-cli mcp` and the desktop's relay replace them @@ -96,6 +98,17 @@ impl Server { /// Handles one message (a line of JSON): the reply to send, or `None` for a notification or /// a response. pub fn handle(&self, s: &mut Session, message: &str) -> Option { + self.handle_scoped(s, message, self.scope(s), Vec::new(), "") + } + /// Named relay/file-bridge ingress; permits are engine plans stored when approval was shown. + pub(crate) fn handle_scoped( + &self, + s: &mut Session, + message: &str, + scope: Scope, + mut permits: Vec<(String, Permit)>, + request: &str, + ) -> Option { let msg: Value = match serde_json::from_str(message) { Ok(v) => v, Err(e) => return Some(error(&Value::Null, PARSE_ERROR, &format!("not JSON: {e}"))), @@ -108,18 +121,30 @@ impl Server { error_value(&Value::Null, INVALID_REQUEST, &format!("a batch holds at most {MAX_BATCH} messages")) } Value::Array(batch) => { - let replies: Vec = batch.iter().filter_map(|m| self.handle_value(s, m)).collect(); + let replies: Vec = batch + .iter() + .enumerate() + .filter_map(|(position, m)| self.handle_value(s, m, &scope, &mut permits, &request_key(request, position))) + .collect(); if replies.is_empty() { return None; } Value::Array(replies) } - _ => self.handle_value(s, &msg)?, + _ => self.handle_value(s, &msg, &scope, &mut permits, &request_key(request, 0))?, }; Some(reply.to_string()) } - fn handle_value(&self, s: &mut Session, msg: &Value) -> Option { + /// Applies per-entry JSON-RPC identity/notification rules before tool dispatch. + fn handle_value( + &self, + s: &mut Session, + msg: &Value, + scope: &Scope, + permits: &mut Vec<(String, Permit)>, + request: &str, + ) -> Option { let Some(obj) = msg.as_object() else { return Some(error_value(&Value::Null, INVALID_REQUEST, "expected a JSON-RPC object")); }; @@ -136,28 +161,29 @@ impl Server { return Some(error_value(&Value::Null, INVALID_REQUEST, "id must be a string or a number")); } let params = obj.get("params").cloned().unwrap_or(Value::Null); - Some(match self.call(s, method, ¶ms) { + Some(match self.call(s, method, ¶ms, scope, permits, request) { Ok(result) => json!({"jsonrpc": "2.0", "id": id, "result": result}), Err((code, message)) => error_value(&id, code, &message), }) } - fn call(&self, s: &mut Session, method: &str, params: &Value) -> Result { + fn call( + &self, + s: &mut Session, + method: &str, + params: &Value, + scope: &Scope, + permits: &mut Vec<(String, Permit)>, + request: &str, + ) -> Result { match method { "initialize" => Ok(initialize(params, &self.instructions())), "ping" => Ok(json!({})), "tools/list" => Ok(json!({"tools": tool_list()})), "tools/call" => { - let name = params.get("name").and_then(Value::as_str).ok_or((INVALID_PARAMS, "name is required".to_string()))?; - let args = match params.get("arguments") { - None | Some(Value::Null) => json!({}), - Some(v @ Value::Object(_)) => v.clone(), - Some(_) => return Err((INVALID_PARAMS, "arguments must be an object".into())), - }; - if !TOOL_NAMES.contains(&name) && !tools::defs().iter().any(|d| d.name == name) { - return Err((INVALID_PARAMS, format!("unknown tool '{name}'"))); - } - Ok(self.call_tool(s, name, args)) + let (name, args) = tool_request(params)?; + let permit = permits.iter().position(|(id, _)| id == request).map(|i| permits.remove(i).1); + Ok(self.call_tool(s, name, args, scope.clone(), permit, request)) } "resources/list" => Ok(json!({"resources": resource_list()})), "resources/templates/list" => Ok(json!({"resourceTemplates": []})), @@ -183,24 +209,27 @@ impl Server { } /// Runs a tool; failures are results the model reads (`isError`), not protocol errors. - fn call_tool(&self, s: &mut Session, name: &str, args: Value) -> Value { + fn call_tool(&self, s: &mut Session, name: &str, args: Value, scope: Scope, permit: Option, request: &str) -> Value { let text = |t: String, is_error: bool| json!({"content": [{"type": "text", "text": t}], "isError": is_error}); + let call = ToolCall { id: request.into(), name: name.into(), args: args.clone() }; let result = match name { - "view_map" => return view_map(s, &args).unwrap_or_else(|e| text(e, true)), - "snapshot" => { - let call = ToolCall { id: String::new(), name: name.to_string(), args: args.clone() }; - return match self.refusal(s, &call) { - Some(why) => text(why, true), - None => self.snapshot(s, &args).unwrap_or_else(|e| text(e, true)), - }; + "view_map" => { + let result = (|| { + let engine = tools::policy_call(&call)?.ok_or("missing view adapter")?; + let owned = s.defer(scope, &engine, permit)?; + s.finish_deferred(owned, |s| view_map(s, &args))? + })(); + return result.unwrap_or_else(|e| text(e, true)); } - _ => { - let call = ToolCall { id: String::new(), name: name.to_string(), args }; - match self.refusal(s, &call) { - Some(why) => Err(why), - None => tools::run(s, &call), - } + "snapshot" => { + let admitted = (|| { + let engine = tools::policy_call(&call)?.ok_or("missing snapshot adapter")?; + let owned = s.defer(scope.clone(), &engine, permit)?; + s.finish_deferred(owned, |s| self.snapshot(s, &args, &scope))? + })(); + return admitted.unwrap_or_else(|e| text(e, true)); } + _ => tools::run(s, &call, scope, permit), }; match result { Ok(t) => text(t, false), @@ -211,7 +240,7 @@ impl Server { /// `snapshot` drawn offscreen: the server can't see the app's window, so a `screen` snapshot /// is drawn too, with a note. The desktop relay captures the window itself /// ([`crate::relay`]) and comes here only when the map isn't on screen. - fn snapshot(&self, s: &mut Session, a: &Value) -> Result { + fn snapshot(&self, s: &mut Session, a: &Value, scope: &Scope) -> Result { let note = match a.get("mode").and_then(Value::as_str) { Some("screen") if self.users_project => Some( "the map isn't on screen in RustGIS (a layout or the catalog is shown, or the window is hidden), so it was drawn offscreen", @@ -221,7 +250,7 @@ impl Server { } _ => None, }; - render_snapshot(s, a, note) + render_snapshot_checked(s, a, note, scope) } /// What the model is told about the server when it connects. @@ -234,75 +263,43 @@ impl Server { /// commands, files and the network included. A refusal added below is lifted by it too, so a /// rule that must hold even then belongs before this check. pub fn refusal(&self, s: &Session, call: &ToolCall) -> Option { - if self.full_access { - return None; - } - // A snapshot saved to a file writes it. - if call.name == "snapshot" { - let path = save_path(&call.args)?; - return (!self.allow_access).then(|| { - format!("snapshot was not saved to '{path}': it writes a file on this computer. The server allows that only when started with --yes (rustgis-cli mcp --yes).") - }); + match tools::policy_call(call) { + Err(e) => Some(e), + Ok(None) => None, + Ok(Some(call)) => match s.review(&self.scope(s), &call) { + Outcome::Allowed => None, + Outcome::NeedsApproval(p) => Some(p.review().why.clone()), + Outcome::Refused(e) => Some(e), + }, } - // A script is refused when any of its lines would be, before any of it runs. A script - // that doesn't parse runs to say so. - if call.name == "run" { - let script = call.args.get("script").and_then(Value::as_str).unwrap_or(""); - let lines = rustgis_engine::cmd::workflows::script_commands(script).ok()?; - // An agent's undo is checked against the step on top before the script runs, so a - // second undo in it could reach a step beneath, the user's: undo and redo run alone - // (a script of that one line, as the file bridge sends, is checked like a command). - if s.actor.is_some() - && lines.len() > 1 - && let Some((n, id, _)) = lines.iter().find(|(_, id, _)| matches!(id.as_str(), "edit.undo" | "edit.redo")) - { - return Some(format!( - "line {n}: '{id}' runs on its own (run_command, or a script of that one line), so it takes back only your own last step" - )); - } - return lines.into_iter().find_map(|(n, id, params)| { - let line = ToolCall { id: String::new(), name: "run_command".into(), args: json!({"command": id, "params": params}) }; - self.refusal(s, &line).map(|why| format!("line {n}: {why}")) - }); - } - let (id, params) = tools::target(call)?; - // An agent working on the user's project undoes and redoes only its own steps. - if let (Some(me), true) = (s.actor.as_deref(), matches!(id.as_str(), "edit.undo" | "edit.redo")) { - let top = if id == "edit.undo" { s.undo_actor() } else { s.redo_actor() }; - if let Some(by) = top.filter(|by| *by != Some(me)) { - let whose = by.map_or_else(|| "the user's".to_string(), |other| format!("{other}'s")); - return Some(format!( - "'{id}' was not run: the step it would change is {whose}, and an agent undoes and redoes only its own steps" - )); - } - } - let project = PROJECT_COMMANDS.contains(&id.as_str()); - if project && self.users_project { - return Some(format!( - "'{id}' is reserved to the user: it would replace the project they have open in RustGIS. Work on the open project instead, or ask the user to open another" - )); - } - if user_only(&id) && !project && !matches!(id.as_str(), "edit.undo" | "edit.redo") { - return Some(format!( - "'{id}' is reserved to the user (plugin management, scripts, tasks, models and the GeoLibre runner); run it in RustGIS itself" - )); - } - let why = if id.starts_with(rustgis_engine::plugins::PREFIX) { - "it runs a plugin's command" - } else if id == "project.openText" { - // A project can hold web tile layers, which drawing the map (view_map) fetches. - "a project can bring in web layers that connect to the internet" + } + /// Captures this server's named ingress; nested work inherits it through Session::run. + fn scope(&self, s: &Session) -> Scope { + let access = if self.full_access { + AgentAccess::Full + } else if self.allow_access { + AgentAccess::Allow } else { - match reaches(s.registry.get(&id)?, ¶ms)? { - Reach::Files => "it reads or writes files on this computer", - Reach::Network => "it connects to an address on the internet", - } + AgentAccess::Refuse }; - (!self.allow_access) - .then(|| format!("'{id}' was not run: {why}. The server allows that only when started with --yes (rustgis-cli mcp --yes).")) + Scope::agent(access, self.users_project, "stdio", s.actor.clone(), Arc::new(FixedLive)) } } +/// Validates the same tool envelope for execution and reviewed batch extraction. +pub(crate) fn tool_request(params: &Value) -> Result<(&str, Value), RpcError> { + let name = params.get("name").and_then(Value::as_str).ok_or((INVALID_PARAMS, "name is required".to_string()))?; + let args = match params.get("arguments") { + None | Some(Value::Null) => json!({}), + Some(v @ Value::Object(_)) => v.clone(), + Some(_) => return Err((INVALID_PARAMS, "arguments must be an object".into())), + }; + if !TOOL_NAMES.contains(&name) && !tools::defs().iter().any(|d| d.name == name) { + return Err((INVALID_PARAMS, format!("unknown tool '{name}'"))); + } + Ok((name, args)) +} + fn initialize(params: &Value, instructions: &str) -> Value { // Speak the client's version when we know it, else our newest. let asked = params.get("protocolVersion").and_then(Value::as_str).unwrap_or(""); @@ -436,6 +433,12 @@ pub fn snapshot_wait(a: &Value) -> f64 { /// A `snapshot` drawn offscreen at `max_width` in the view's shape, with `note` saying why when it /// was asked of the screen. pub fn render_snapshot(s: &mut Session, a: &Value, note: Option<&str>) -> Result { + render_snapshot_with(s, a, note, None) +} +pub(crate) fn render_snapshot_checked(s: &mut Session, a: &Value, note: Option<&str>, scope: &Scope) -> Result { + render_snapshot_with(s, a, note, Some(scope)) +} +fn render_snapshot_with(s: &mut Session, a: &Value, note: Option<&str>, scope: Option<&Scope>) -> Result { let asked = a.get("max_width").and_then(Value::as_f64).filter(|v| v.is_finite()).unwrap_or(1024.0).clamp(64.0, MAX_VIEW); // The view's shape, so the image shows what the window does: a very tall view is narrowed // to fit rather than squashed. @@ -446,16 +449,47 @@ pub fn render_snapshot(s: &mut Session, a: &Value, note: Option<&str>) -> Result let png = rustgis_engine::cmd::share::map_png(s, Some(width), Some(height), None)?; // Tiles still on their way are missing from it. let settled = rustgis_engine::render::rustgis_tiles::cache::in_flight() == 0; - snapshot_result(s, &png, "render", settled, a, note) + snapshot_result_with(s, &png, "render", settled, a, note, scope) } /// A `snapshot` result: the image, and the view it shows as JSON. `mode` is how it was made /// (`screen` or `render`) and `settled` whether drawing had finished. Saves the PNG to /// `save_path` when the arguments name one (the caller checked the server allows it). pub fn snapshot_result(s: &mut Session, png: &[u8], mode: &str, settled: bool, a: &Value, note: Option<&str>) -> Result { + snapshot_result_with(s, png, mode, settled, a, note, None) +} +/// Formats a delivered screenshot inside its already resumed initiating scope. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn snapshot_result_checked( + s: &mut Session, + png: &[u8], + mode: &str, + settled: bool, + a: &Value, + note: Option<&str>, + scope: &Scope, +) -> Result { + snapshot_result_with(s, png, mode, settled, a, note, Some(scope)) +} +/// Checks late save authority and acquires metadata through inherited dispatch. +fn snapshot_result_with( + s: &mut Session, + png: &[u8], + mode: &str, + settled: bool, + a: &Value, + note: Option<&str>, + scope: Option<&Scope>, +) -> Result { use base64::Engine; + if let Some(scope) = scope { + scope.check_live(Effects::NONE)?; + } let saved = match save_path(a) { Some(path) => { + if let Some(scope) = scope { + scope.check_live(Effects::NONE.with(rustgis_engine::policy::Effect::Files))?; + } std::fs::write(path, png).map_err(|e| format!("could not save the snapshot to '{path}': {e}"))?; Some(path) } @@ -463,7 +497,7 @@ pub fn snapshot_result(s: &mut Session, png: &[u8], mode: &str, settled: bool, a }; // The PNG's own size, from its header. let dim = |at: usize| png.get(at..at + 4).and_then(|b| <[u8; 4]>::try_from(b).ok()).map(u32::from_be_bytes); - let info = s.run("info.project", &json!({"includeFields": false}))?; + let info = s.run("info.project", &json!({"includeFields":false}))?; let meta = json!({ "mode": mode, "width": dim(16), @@ -512,3 +546,10 @@ fn error_value(id: &Value, code: i64, message: &str) -> Value { fn error(id: &Value, code: i64, message: &str) -> String { error_value(id, code, message).to_string() } + +struct FixedLive; +impl rustgis_engine::policy::Revocation for FixedLive { + fn allows(&self, _: Effects) -> bool { + true + } +} diff --git a/crates/assistant/src/relay.rs b/crates/assistant/src/relay.rs index 9a4befc809..7ca9e34757 100644 --- a/crates/assistant/src/relay.rs +++ b/crates/assistant/src/relay.rs @@ -26,6 +26,7 @@ use std::sync::{Arc, Mutex, MutexGuard}; use std::time::{Duration, Instant}; use rustgis_engine::Session; +use rustgis_engine::policy::{AgentAccess, Effect, Effects, Outcome, Pending, Revocation, Scope}; use serde_json::Value; use crate::framing::{self, MAX_LINE, Oversize, read_message_max}; @@ -109,6 +110,7 @@ const CANCELLED: u8 = 2; /// A queued message: its line, what its connection allows, where its reply goes, and its state. struct Request { + plans: Vec<(String, Pending)>, /// A positive preflight is cached while this exact message waits for a decision. approval_required: bool, /// Set while it waits for the user's approval, so its connection waits for the user @@ -131,6 +133,8 @@ struct Request { pub struct Approval { pub id: u64, pub summary: String, + /// Sanitized engine cards, including structured details for translated UI presentation. + pub reviews: Vec, token: String, state: Arc, decision: Arc, @@ -215,6 +219,8 @@ pub struct ScreenRequest { /// A `snapshot` request waiting for the app to capture its window. struct Shot { req: Request, + scope: Scope, + completion: rustgis_engine::policy::Deferred, /// The request's JSON-RPC id and the tool's arguments. id: Value, args: Value, @@ -692,18 +698,22 @@ impl Relay { } // A last resort, as for the messages `poll` runs: a panic answers with an error instead // of breaking the app. - let answer = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| match shot_png { - Ok(png) => crate::mcp::snapshot_result(s, &png, "screen", settled, &shot.args, None), - Err(why) => crate::mcp::render_snapshot(s, &shot.args, Some(&why)), + let req = shot.req; + let scope = shot.scope; + let answer = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + s.finish_deferred(shot.completion, |s| match shot_png { + Ok(png) => crate::mcp::snapshot_result_checked(s, &png, "screen", settled, &shot.args, None, &scope), + Err(why) => crate::mcp::render_snapshot_checked(s, &shot.args, Some(&why), &scope), + })? })) .unwrap_or_else(|_| Err("the snapshot failed unexpectedly".into())); let result = answer.unwrap_or_else(|e| serde_json::json!({"content": [{"type": "text", "text": e}], "isError": true})); let reply = serde_json::json!({"jsonrpc": "2.0", "id": shot.id, "result": result}).to_string(); - if let Some(a) = action(&shot.req.line, Some(&reply)) { + if let Some(a) = action(&req.line, Some(&reply)) { *lock(&self.owner.last) = Some(a); } - self.audit(&shot.req.line, Some(&reply), Duration::ZERO); - let _ = shot.req.reply.send(Some(reply)); + self.audit(&req.line, Some(&reply), Duration::ZERO); + let _ = req.reply.send(Some(reply)); self.owner.shared.notify(); } @@ -765,8 +775,8 @@ impl Relay { Ok(Grant { token, file, shared: Arc::downgrade(shared) }) } - /// A local agent still has file/network capability, but every gated MCP message waits for - /// RustGIS-side approval. Neither --yes nor the global relay checkbox bypasses this queue. + /// With automatic approval off, gated MCP messages attached with this grant wait for + /// RustGIS-side approval. Neither --yes nor the global relay checkbox widens the grant. pub fn grant_reviewed(&self, auto: bool) -> Result { let grant = self.grant(true)?; let grants = lock(&self.owner.shared.grants); @@ -817,13 +827,21 @@ impl Relay { let reviewed = policy.get(&req.token).copied() == Some(false); if reviewed && access { if !req.approval_required - && let Some(summary) = approval_summary(s, &req.line) + && let Some((summary, plans)) = { + let layers = self.enter_agent(s, &req.token); + let scope = self.request_scope(&req, AgentAccess::Review, s.actor.clone()); + let result = approval_plans(s, &req.line, &scope, &req.id.to_string()); + self.leave_agent(s, &req.token, layers, false); + result + } { + req.plans = plans; req.approval_required = true; req.held.store(true, SeqCst); lock(&self.owner.shared.approvals).push(Approval { id: req.id, summary, + reviews: req.plans.iter().map(|(_, p)| p.review().clone()).collect(), token: req.token.clone(), state: Arc::clone(&req.state), decision: Arc::clone(&req.decision), @@ -877,7 +895,38 @@ impl Relay { .is_none() { let wait = Duration::from_secs_f64(crate::mcp::snapshot_wait(&args)); - *lock(&self.owner.shared.shot) = Some(Shot { req, id, args, wait }); + let mode = if full_access { + AgentAccess::Full + } else if reviewed && allow_access { + AgentAccess::Review + } else if allow_access { + AgentAccess::Allow + } else { + AgentAccess::Refuse + }; + let scope = self.request_scope(&req, mode, None); + let call = crate::provider::ToolCall { + id: crate::mcp::request_key(&req.id.to_string(), 0), + name: "snapshot".into(), + args: args.clone(), + }; + let completion = (|| { + let engine = crate::tools::policy_call(&call)?.ok_or("missing snapshot adapter")?; + let permit = req.plans.iter().position(|(id, _)| id == &call.id).map(|i| req.plans.remove(i).1.allow()); + s.defer(scope.clone(), &engine, permit) + })(); + let completion = match completion { + Ok(c) => c, + Err(e) => { + let answer = + serde_json::json!({"jsonrpc":"2.0","id":id,"result":{"content":[{"type":"text","text":e}],"isError":true}}) + .to_string(); + let _ = req.reply.send(Some(answer)); + ran += 1; + continue; + } + }; + *lock(&self.owner.shared.shot) = Some(Shot { req, scope, completion, id, args, wait }); let mut queue = lock(&self.owner.shared.requests); for rest in pending.rev() { queue.push_front(rest); @@ -889,9 +938,23 @@ impl Relay { // The agent works with its own active layer and is named in the undo history; the // user's selection is theirs again afterwards. let layers = self.enter_agent(s, &req.token); + let mode = if full_access { + AgentAccess::Full + } else if reviewed && allow_access { + AgentAccess::Review + } else if allow_access { + AgentAccess::Allow + } else { + AgentAccess::Refuse + }; + let scope = self.request_scope(&req, mode, s.actor.clone()); + let permits = + if reviewed { std::mem::take(&mut req.plans).into_iter().map(|(id, p)| (id, p.allow())).collect() } else { Vec::new() }; let started = Instant::now(); - let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| server.handle(s, &req.line))) - .unwrap_or_else(|_| Some(internal_error(&req.line))); + let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + server.handle_scoped(s, &req.line, scope, permits, &req.id.to_string()) + })) + .unwrap_or_else(|_| Some(internal_error(&req.line))); // A layer.select that failed (an unknown layer, a refusal) chose nothing. let picked = selects_layer(&req.line) && reply_ok(out.as_deref()); self.leave_agent(s, &req.token, layers, picked); @@ -909,6 +972,18 @@ impl Relay { impl Relay { /// Before an agent's call: names it as the session's actor and makes its own active layer /// (if it still exists) the session's. Returns the user's active layer. + fn request_scope(&self, req: &Request, access: AgentAccess, actor: Option) -> Scope { + let actor = actor + .or_else(|| lock(&self.owner.shared.agents).get(&req.token).map(|a| a.name.clone())) + .unwrap_or_else(|| format!("relay {} request {}", self.owner.shared.run_id, req.id)); + Scope::agent( + access, + true, + &actor, + Some(actor.clone()), + Arc::new(GrantLive { shared: Arc::downgrade(&self.owner.shared), token: req.token.clone(), asked: req.asked, access }), + ) + } fn enter_agent(&self, s: &mut Session, token: &str) -> (Option, Option) { let mut agents = lock(&self.owner.shared.agents); let n = agents.len() + 1; @@ -1231,6 +1306,7 @@ fn connection(stream: TcpStream, slot: &mut Slot) { false } else { queue.push_back(Request { + plans: Vec::new(), approval_required: false, held: Arc::clone(&held), id: shared.next_id.fetch_add(1, SeqCst), @@ -1386,43 +1462,36 @@ fn request_id(msg: &Value) -> Value { /// Preflight the exact tool calls in a message, including batches. Reserved commands still go /// through Server::refusal and remain unavailable even after approval. -fn approval_summary(s: &Session, line: &str) -> Option { +fn approval_plans(s: &Session, line: &str, scope: &Scope, request: &str) -> Option<(String, Vec<(String, Pending)>)> { let msg: Value = serde_json::from_str(line).ok()?; let messages = msg.as_array().map(Vec::as_slice).unwrap_or_else(|| std::slice::from_ref(&msg)); - let server = Server { allow_access: true, skills: Vec::new(), users_project: true, full_access: false }; + if messages.len() > crate::mcp::MAX_BATCH { + return None; + } let mut summaries = Vec::new(); - for msg in messages { - if msg.get("method").and_then(Value::as_str) != Some("tools/call") || msg.get("id").is_none() { + let mut plans = Vec::new(); + for (position, msg) in messages.iter().enumerate() { + if msg.get("method").and_then(Value::as_str) != Some("tools/call") { continue; } - let Some(params) = msg.get("params") else { continue }; - let Some(name) = params.get("name").and_then(Value::as_str) else { continue }; - let call = crate::provider::ToolCall { - id: String::new(), - name: name.to_string(), - args: params.get("arguments").cloned().unwrap_or(Value::Null), + let Some(id) = msg.get("id") else { + continue; }; - if server.refusal(s, &call).is_none() - && let Some(reason) = crate::tools::gate(s, &call) + if !matches!(id, Value::String(_) | Value::Number(_)) { + continue; + } + let Some(params) = msg.get("params").filter(|p| p.is_object()) else { continue }; + let Ok((name, arguments)) = crate::mcp::tool_request(params) else { continue }; + let call = crate::provider::ToolCall { id: crate::mcp::request_key(request, position), name: name.into(), args: arguments }; + if let Ok(Some(engine)) = crate::tools::policy_call(&call) + && let Outcome::NeedsApproval(p) = s.review(scope, &engine) { - summaries.push(format!("{}: {reason}", approval_text(name, &call.args))); + let review = p.review(); + summaries.push(format!("{}: {}\n{}", review.what, review.why, review.steps.join("\n"))); + plans.push((call.id, p)); } } - (!summaries.is_empty()).then(|| summaries.join("\n")) -} - -/// The most of a request's arguments an approval shows. -const MAX_APPROVAL_TEXT: usize = 4096; - -/// A tool call as an approval shows it: its complete arguments, never cut (a cut text could hide -/// the file or address approved), or a request too long to show, to decline. -fn approval_text(name: &str, args: &Value) -> String { - let full = args.to_string(); - if full.len() > MAX_APPROVAL_TEXT { - format!("{name}: a request too long to show in full ({} bytes); decline it", full.len()) - } else { - format!("{name} {full}") - } + (!plans.is_empty()).then(|| (summaries.join("\n"), plans)) } fn declined_reply(line: &str) -> String { @@ -1453,6 +1522,41 @@ fn internal_error(message: &str) -> String { error_reply(&id, -32603, "internal error") } +/// The original grant and current user settings, checked on fetch workers as well as the UI. +struct GrantLive { + shared: std::sync::Weak, + token: String, + asked: bool, + access: AgentAccess, +} +impl Revocation for GrantLive { + fn allows(&self, effects: Effects) -> bool { + let Some(shared) = self.shared.upgrade() else { + return false; + }; + if shared.stopped.load(SeqCst) { + return false; + } + let Some(access) = shared.access(&self.token, self.asked) else { + return false; + }; + if (effects.contains(Effect::Reserved) || effects.contains(Effect::Automation) || effects.contains(Effect::Replaces)) + && (!shared.full_access.load(SeqCst) || !same_token(&self.token, &shared.token)) + { + return false; + } + if effects.contains(Effect::Files) || effects.contains(Effect::Network) || effects.contains(Effect::Plugin) { + if !access { + return false; + } + if self.access == AgentAccess::Allow && lock(&shared.reviewed).get(&self.token) == Some(&false) { + return false; + } + } + true + } +} + #[cfg(test)] #[allow(clippy::unwrap_used, clippy::panic)] mod tests { @@ -1491,6 +1595,7 @@ mod tests { let state = Arc::new(AtomicU8::new(PENDING)); let id = relay.owner.shared.next_id.fetch_add(1, SeqCst); lock(&relay.owner.shared.requests).push_back(Request { + plans: Vec::new(), approval_required: false, held: Arc::default(), id, @@ -1517,6 +1622,37 @@ mod tests { assert!(path.exists()); let reply: Value = serde_json::from_str(&rx.recv().unwrap().unwrap()).unwrap(); assert_ne!(reply["result"]["isError"], true, "{reply}"); + let changed = root.path().join("changed.png"); + let (id, rx, _) = queue(export(&changed)); + assert_eq!(relay.poll(&mut s), 0); + assert!(grant.pending().iter().any(|p| p.id == id)); // Freeze the actual first card. + s.run("layer.select", &json!({"layer":"Cities"})).unwrap(); + grant.decide(id, true); + assert_eq!(relay.poll(&mut s), 1); + assert!(rx.recv().unwrap().unwrap().contains("changed")); + assert!(!changed.exists()); + // A load replaced while the actual relay card is shown cannot inherit its consent. + let load = || { + rustgis_engine::plugins::load( + rustgis_engine::plugins::Package::from_zip(&rustgis_engine::plugins::testing::package("relay-policy-load", &["edit"])) + .unwrap(), + "test", + ) + .unwrap() + }; + load(); + let (id, rx, _) = queue( + json!({"jsonrpc":"2.0","id":9,"method":"tools/call","params":{"name":"run_command","arguments":{"command":"plugin:relay-policy-load/f","params":{"op":"layer.rename","params":{"layer":"Cities","name":"stale load"}}}}}), + ); + assert_eq!(relay.poll(&mut s), 0); + assert!(grant.pending().iter().any(|p| p.id == id)); + rustgis_engine::plugins::unload("relay-policy-load"); + load(); + grant.decide(id, true); + assert_eq!(relay.poll(&mut s), 1); + assert!(rx.recv().unwrap().unwrap().contains("changed")); + assert!(s.map().layer_by_name("stale load").is_none()); + rustgis_engine::plugins::unload("relay-policy-load"); let denied = root.path().join("denied.png"); let (id, rx, _) = queue(export(&denied)); assert_eq!(relay.poll(&mut s), 0); @@ -1529,10 +1665,26 @@ mod tests { let batch_path = root.path().join("batch.png"); let (id, rx, _) = queue(json!([{"jsonrpc":"2.0","id":0,"method":"tools/call"},export(&batch_path)])); assert_eq!(relay.poll(&mut s), 0, "malformed earlier entries cannot bypass batch approval"); + assert!(grant.pending().iter().any(|p| p.id == id && p.summary.contains("share.exportMap"))); grant.decide(id, false); assert_eq!(relay.poll(&mut s), 1); assert!(rx.recv().unwrap().unwrap().contains("declined")); assert!(!batch_path.exists()); + // Repeated wire ids must not let a read consume the export's approval. + let duplicate_path = root.path().join("duplicate-id.png"); + let (id, rx, _) = queue(json!([ + {"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"list_layers","arguments":{}}}, + export(&duplicate_path) + ])); + assert_eq!(relay.poll(&mut s), 0); + assert!(!duplicate_path.exists()); + grant.decide(id, true); + assert_eq!(relay.poll(&mut s), 1); + let reply: Value = serde_json::from_str(&rx.recv().unwrap().unwrap()).unwrap(); + let replies = reply.as_array().unwrap(); + assert_eq!(replies.len(), 2, "{reply}"); + assert!(replies.iter().all(|r| r["id"] == 1 && r["result"]["isError"] == false), "{reply}"); + assert!(duplicate_path.exists()); let expired = root.path().join("expired.png"); let (id, _, state) = queue(export(&expired)); assert_eq!(relay.poll(&mut s), 0); @@ -2104,6 +2256,42 @@ mod tests { relay.stop(); } + /// The actual queued screen completion keeps its original grant and project generation. + #[test] + fn permission_policy_delayed_screen_refuses_revocation_and_replacement() { + let relay = Relay::start(0).unwrap(); + let mut s = sample(); + relay.set_screen(true); + relay.set_allow_access(true); + let mut client = TcpStream::connect(("127.0.0.1", relay.port())).unwrap(); + attach(&mut client, &relay, false); + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("held.png"); + for (id, replace) in [(1, false), (2, true)] { + relay.set_allow_access(true); + writeln!(client, "{}", snapshot_call(id, json!({"save_path":path}))).unwrap(); + client.flush().unwrap(); + wait_until("the held screen request", || { + relay.poll(&mut s); + relay.screen_request().is_some() + }); + if replace { + s.run("project.new", &json!({"name":"replacement"})).unwrap(); + } else { + relay.set_allow_access(false); + } + // The fallback path is also late work; it must refuse before rendering/writing. + relay.finish_screen(&mut s, Err("screen unavailable".into()), false); + let r = reply(&client); + assert_eq!(r["result"]["isError"], true, "{r}"); + assert!(!path.exists()); + assert!(relay.screen_request().is_none()); + relay.finish_screen(&mut s, Ok(vec![1, 2, 3]), false); + assert!(!path.exists(), "a replay has no owner claim"); + } + relay.stop(); + } + /// Without the map on screen, or when the app can't capture it, a snapshot is drawn /// offscreen and says so; `mode: render` never waits for the app. #[test] @@ -2630,9 +2818,20 @@ mod tests { json!({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "run_command", "arguments": {"command": "share.exportMap", "params": params}}}) .to_string() }; - let padded = approval_summary(&s, &call(json!({"a": "a".repeat(400), "path": "/home/u/.profile"}))).unwrap(); + let scope = Scope::agent(AgentAccess::Review, true, "review-fixture", None, std::sync::Arc::new(crate::tools::Live::default())); + let summary = |line: &str| approval_plans(&s, line, &scope, "request").map(|(text, _)| text); + let padded = summary(&call(json!({"a": "a".repeat(400), "path": "/home/u/.profile"}))).unwrap(); assert!(padded.contains("/home/u/.profile"), "{padded}"); - let huge = approval_summary(&s, &call(json!({"a": "a".repeat(5000), "path": "/home/u/.profile"}))).unwrap(); - assert!(huge.contains("too long to show in full") && !huge.contains(".profile"), "{huge}"); + let huge = call(json!({"a": "a".repeat(5000), "path": "/home/u/.profile"})); + assert!(summary(&huge).is_none(), "an unshowable request is refused before approval"); + let raw: Value = serde_json::from_str(&huge).unwrap(); + let engine = crate::tools::policy_call(&crate::provider::ToolCall { + id: "request/1".into(), + name: "run_command".into(), + args: raw["params"]["arguments"].clone(), + }) + .unwrap() + .unwrap(); + assert!(matches!(s.review(&scope, &engine), Outcome::Refused(e) if e.contains("too long to show in full"))); } } diff --git a/crates/assistant/src/relay/http.rs b/crates/assistant/src/relay/http.rs index 0bff0ad534..56fbd9b9e1 100644 --- a/crates/assistant/src/relay/http.rs +++ b/crates/assistant/src/relay/http.rs @@ -436,6 +436,7 @@ fn submit(shared: &Arc, line: String) -> Submitted { return Submitted::Failed("503 Service Unavailable", "the app is busy; try again".into()); } queue.push_back(Request { + plans: Vec::new(), approval_required: false, held: Arc::new(AtomicBool::new(false)), id: shared.next_id.fetch_add(1, SeqCst), diff --git a/crates/assistant/src/tests.rs b/crates/assistant/src/tests.rs index 80cddbf730..4d4557a89e 100644 --- a/crates/assistant/src/tests.rs +++ b/crates/assistant/src/tests.rs @@ -8,6 +8,25 @@ use crate::chatgpt; use crate::provider::*; use crate::tools; +/// Direct behavior fixtures explicitly accept a displayed engine review; Agent tests below +/// still exercise their own pending/decision path. +fn run_tool_call(s: &mut Session, call: &ToolCall) -> Result { + run_tool_call_with_budget(s, call, tools::DEFAULT_OUTPUT_TOKENS) +} +fn run_tool_call_with_budget(s: &mut Session, call: &ToolCall, tokens: usize) -> Result { + use rustgis_engine::policy::{Outcome, Scope}; + let scope = Scope::assistant(42, std::sync::Arc::new(tools::Live::default())); + let permit = match tools::policy_call(call)? { + Some(call) => match s.review(&scope, &call) { + Outcome::Allowed => None, + Outcome::NeedsApproval(p) => Some(p.allow()), + Outcome::Refused(e) => return Err(e), + }, + None => None, + }; + tools::run_with_budget(s, call, tokens, scope, permit) +} + fn sample() -> Session { let mut s = Session::new(Project::default()); let _ = s.run("project.sample", &Value::Null); @@ -227,6 +246,24 @@ fn connection_management_requires_fresh_approval_but_can_run_in_app() { #[test] fn connection_commands_require_fresh_approval_before_login_or_scripts() { + // The S3 logins change a process-wide registry that parallel approval readers snapshot. + #[cfg(not(target_arch = "wasm32"))] + { + const SCENARIO: &str = "tests::connection_commands_require_fresh_approval_before_login_or_scripts"; + if std::env::var("RUSTGIS_ASSISTANT_CONNECTION_APPROVAL_CHILD").as_deref() != Ok(SCENARIO) { + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", SCENARIO]) + .env("RUSTGIS_ASSISTANT_CONNECTION_APPROVAL_CHILD", SCENARIO) + .output() + .unwrap(); + let stdout = String::from_utf8_lossy(&output.stdout); + let mut summaries = stdout.lines().filter(|line| line.starts_with("test result:")); + let ran_one = summaries.next().is_some_and(|line| line.starts_with("test result: ok. 1 passed; 0 failed; 0 ignored;")) + && summaries.next().is_none(); + assert!(output.status.success() && ran_one, "{stdout}\n{}", String::from_utf8_lossy(&output.stderr)); + return; + } + } use rustgis_engine::render::rustgis_tiles::s3; for command in ["connection.browse", "connection.test", "connection.login"] { @@ -317,6 +354,71 @@ fn remembered_database_forget_requires_fresh_direct_and_script_approval() { } } +/// Undeclared external runners fail closed at assistant/MCP ingress; the engine tests load +/// the real plugin fixture to exercise reviewed plugin approvals. +#[test] +fn undeclared_external_tools_refuse_by_id_and_display_name() { + use rustgis_engine::analysis::{self, Inputs, Output, ToolResult, ToolSpec}; + /// Register a unique tool name plus a colliding built-in name to exercise resolution. + fn specs() -> Vec { + [("plugin:assistant-containment/x", "Assistant containment selection"), ("plugin:assistant-containment/shadow", "Buffer")] + .into_iter() + .map(|(id, name)| ToolSpec { + id, + name, + toolbox: "Tests", + summary: "Selection fixture", + params: &[], + run: analysis::external_placeholder, + }) + .collect() + } + /// A deterministic tool result makes unauthorized execution observable without I/O. + fn run(_: &ToolSpec, _: &dyn Inputs, _: &Value) -> Result { + Ok(ToolResult { output: Output::Selection { layer: "Cities".into(), rows: vec![0, 2] }, messages: vec![] }) + } + analysis::register_external("assistant-containment", specs, run); + // Explicit native discovery publishes metadata; untrusted callers cannot discover it. + assert_eq!(analysis::external_tools_of("assistant-containment").len(), 2); + assert!(analysis::resolve_published("plugin:assistant-containment/x").is_some()); + let mut s = sample(); + let cities = s.map().layer_by_name("Cities").unwrap().id; + for tool in ["plugin:assistant-containment/x", "Assistant containment selection"] { + for (name, args) in [ + ("run_tool", json!({"tool": tool, "params": {}})), + ("run_tool", json!({"tool": tool, "params": "{}"})), + ("run_command", json!({"command": "gp.run", "params": {"tool": tool, "params": {}}})), + ("run_command", json!({"command": "gp.run", "params": json!({"tool": tool, "params": {}}).to_string()})), + ("run", json!({"script": format!("gp.run {}", json!({"tool": tool, "params": {}}))})), + ] { + let c = ToolCall { id: "envelope".into(), name: name.into(), args: args.clone() }; + if name == "run_command" { + assert_eq!(tools::target(&c), Some(("gp.run".into(), json!({"tool": tool, "params": {}})))); + } + s.run("select.all", &json!({"layer": "Cities"})).unwrap(); + s.run("select.clear", &json!({"layer": "Cities"})).unwrap(); + let (error, why) = call_tool(&mcp::Server::new(false), &mut s, name, args.clone()); + assert!(error && why.contains("plugin"), "{why}"); + assert!(s.map().layer(cities).unwrap().selection.is_empty()); + let mut a = Agent::new(cfg("openai"), &s); + a.ask("select", &s); + assert!(matches!(a.receive(a.generation(), ok(calls(name, args.clone())), &mut s), Step::Send(_))); + assert!(a.pending(&s).is_none(), "an unreviewed runner cannot be approved"); + assert!(s.map().layer(cities).unwrap().selection.is_empty()); + let (error, why) = call_tool(&mcp::Server::new(true), &mut s, name, args); + assert!(error, "{why}"); + assert!(s.map().layer(cities).unwrap().selection.is_empty()); + } + } + let built_in = ToolCall { + id: "b".into(), + name: "run_tool".into(), + args: json!({"tool": "Buffer", "params": {"input": "Cities", "distance": 10}}), + }; + assert!(tools::gate(&s, &built_in).is_none(), "the built-in name wins"); + assert!(tools::policy_call(&built_in).is_ok()); +} + #[test] fn gate_rules() { let s = sample(); @@ -336,7 +438,7 @@ fn gate_rules() { } // A QGIS project given as text names its files and addresses inside: asked, as with a path. let qgis = g("run_command", json!({"command": "project.importQgis", "params": {"text": ""}})); - assert!(qgis.as_deref().is_some_and(|w| w.contains("QGIS project")), "{qgis:?}"); + assert!(qgis.as_deref().is_some_and(|w| w.contains("before reading")), "{qgis:?}"); assert!(g("run_command", json!({"command": "script.run", "params": {"script": "map.zoomFull {}"}})).is_some()); assert!(g("run_command", json!({"command": "plugin.install", "params": {"path": "x.rgplugin"}})).is_some()); assert!(g("run_command", json!({"command": "map.addTiles", "params": {"url": "https://tile.example/{z}/{x}/{y}.png"}})).is_some()); @@ -347,23 +449,27 @@ fn gate_rules() { } assert!(g("run_command", json!({"command": "connection.login", "params": {"id": "c1", "remember": true}})).is_some()); let sql = g("run_command", json!({"command": "layer.addSql", "params": {"sql": "SELECT ST_Point(0, 0) AS g"}})); - assert!(sql.as_deref().is_some_and(|w| w.contains("SQL") && w.contains("internet")), "{sql:?}"); + assert!(sql.as_deref().is_some_and(|w| w.contains("files") && w.contains("internet")), "{sql:?}"); let sql = g("run_command", json!({"command": "layer.addSql", "params": {"sql": "FROM t", "database": "/data/x.duckdb"}})); assert!(sql.as_deref().is_some_and(|w| w.contains("views in the database /data/x.duckdb")), "{sql:?}"); + let script = "layer.addSql {\"sql\":\"FROM t\",\"database\":\"/data/x.duckdb\"}"; + let sql = g("run", json!({"script":script})); + assert!(sql.as_deref().is_some_and(|w| w.contains("views in the database /data/x.duckdb")), "{sql:?}"); + assert!(g("run_tool", json!({"tool": "buffer", "params": {"input": "Cities", "distance": 100}})).is_none()); assert!(g("list_layers", json!({})).is_none()); assert!(g("run_command", json!({"command": "info.fileLayers", "params": {"path": "/etc/passwd"}})).is_some()); assert!(g("run_command", json!({"command": "info.fileColumns", "params": {"path": "/etc/passwd"}})).is_some()); // Georeferencing and projecting are file commands only when they write a file. - let extent = json!({"layer": "r", "extent": [10.0, 45.0, 10.04, 45.04]}); + let extent = json!({"layer": "Cities", "extent": [10.0, 45.0, 10.04, 45.04]}); assert!(g("run_command", json!({"command": "raster.georeference", "params": extent})).is_none()); - let pts = json!({"layer": "r", "points": [[0, 0, 1, 1], [1, 0, 2, 1], [0, 1, 1, 2]], "transform": "affine", "crs": 32613}); + let pts = json!({"layer": "Cities", "points": [[0, 0, 1, 1], [1, 0, 2, 1], [0, 1, 1, 2]], "transform": "affine", "crs": 32613}); assert!(g("run_command", json!({"command": "raster.georeference", "params": pts})).is_none()); let mut file = pts.clone(); file["path"] = json!("/tmp/x.tif"); assert!(g("run_command", json!({"command": "raster.georeference", "params": file})).is_some()); // So is projecting a raster. - let project = json!({"layer": "r", "crs": 31983, "cellSize": 100, "extent": "Countries"}); + let project = json!({"layer": "Cities", "crs": 31983, "cellSize": 100, "extent": "Countries"}); assert!(g("run_command", json!({"command": "raster.project", "params": project})).is_none()); let mut file = project.clone(); file["path"] = json!("/tmp/x.tif"); @@ -390,13 +496,13 @@ fn tool_results_carry_notices() { name: "run_command".into(), args: json!({"command": "layer.addDataText", "params": {"fileName": "pts.csv", "text": "lon,lat\n1,2\nx,y\n"}}), }; - let out: Value = serde_json::from_str(&tools::run(&mut s, &call).unwrap()).unwrap(); + let out: Value = serde_json::from_str(&run_tool_call(&mut s, &call).unwrap()).unwrap(); assert!( out["notices"].as_array().is_some_and(|n| n.iter().any(|t| t.as_str().is_some_and(|t| t.contains("no usable coordinates")))), "{out}" ); let quiet: Value = serde_json::from_str( - &tools::run(&mut s, &ToolCall { id: "y".into(), name: "run_command".into(), args: json!({"command": "map.zoomFull"}) }).unwrap(), + &run_tool_call(&mut s, &ToolCall { id: "y".into(), name: "run_command".into(), args: json!({"command": "map.zoomFull"}) }).unwrap(), ) .unwrap(); assert!(quiet.get("notices").is_none(), "{quiet}"); @@ -412,7 +518,7 @@ fn a_failed_commands_redacted_error_is_not_repeated_as_a_notice() { name: "run_command".into(), args: json!({"command": "layer.addData", "params": {"path": "/no/such/dir/data.geojson?token=hidden"}}), }; - let err = tools::run(&mut s, &call).unwrap_err(); + let err = run_tool_call(&mut s, &call).unwrap_err(); assert_eq!(err.lines().count(), 1, "{err}"); } @@ -424,56 +530,57 @@ fn map_switch_and_remove_do_not_report_layers_added() { let world = s.map().name.clone(); assert!(!world.is_empty(), "sample project needs a named map"); let on_world: Value = serde_json::from_str( - &tools::run(&mut s, &t("run_command", json!({"command": "map.activate", "params": {"name": world}}))).unwrap(), + &run_tool_call(&mut s, &t("run_command", json!({"command": "map.activate", "params": {"name": world}}))).unwrap(), ) .unwrap(); assert!(on_world.get("layersAdded").is_none(), "activating the current map must not invent layersAdded: {on_world}"); - tools::run(&mut s, &t("run_command", json!({"command": "map.new", "params": {"name": "Repro"}}))).unwrap(); + run_tool_call(&mut s, &t("run_command", json!({"command": "map.new", "params": {"name": "Repro"}}))).unwrap(); assert_eq!(s.map().name, "Repro"); let activate: Value = serde_json::from_str( - &tools::run(&mut s, &t("run_command", json!({"command": "map.activate", "params": {"name": world}}))).unwrap(), + &run_tool_call(&mut s, &t("run_command", json!({"command": "map.activate", "params": {"name": world}}))).unwrap(), ) .unwrap(); assert!(activate.get("layersAdded").is_none(), "map.activate must not report the target map's layers as added: {activate}"); assert_eq!(s.map().name, world); let remove: Value = serde_json::from_str( - &tools::run(&mut s, &t("run_command", json!({"command": "map.remove", "params": {"name": "Repro"}}))).unwrap(), + &run_tool_call(&mut s, &t("run_command", json!({"command": "map.remove", "params": {"name": "Repro"}}))).unwrap(), ) .unwrap(); assert!(remove.get("layersAdded").is_none(), "map.remove must not report remaining layers as added: {remove}"); assert_eq!(s.project.maps.len(), 1); // Removing the active, populated map switches to another map's layers: still nothing added. - tools::run(&mut s, &t("run_command", json!({"command": "map.new", "params": {"name": "Gone"}}))).unwrap(); + run_tool_call(&mut s, &t("run_command", json!({"command": "map.new", "params": {"name": "Gone"}}))).unwrap(); let pts = json!({"fileName": "pts.csv", "text": "lon,lat\n1,2\n"}); - tools::run(&mut s, &t("run_command", json!({"command": "layer.addDataText", "params": pts.clone()}))).unwrap(); + run_tool_call(&mut s, &t("run_command", json!({"command": "layer.addDataText", "params": pts.clone()}))).unwrap(); assert_eq!(s.map().name, "Gone"); - let remove_active: Value = - serde_json::from_str(&tools::run(&mut s, &t("run_command", json!({"command": "map.remove", "params": {"name": "Gone"}}))).unwrap()) - .unwrap(); + let remove_active: Value = serde_json::from_str( + &run_tool_call(&mut s, &t("run_command", json!({"command": "map.remove", "params": {"name": "Gone"}}))).unwrap(), + ) + .unwrap(); assert!(remove_active.get("layersAdded").is_none(), "removing the active map must not report the next map's layers: {remove_active}"); assert_eq!(s.map().name, world); // Renaming the active map keeps its layers: nothing added. let renamed: Value = serde_json::from_str( - &tools::run(&mut s, &t("run_command", json!({"command": "map.rename", "params": {"name": "Renamed"}}))).unwrap(), + &run_tool_call(&mut s, &t("run_command", json!({"command": "map.rename", "params": {"name": "Renamed"}}))).unwrap(), ) .unwrap(); assert!(renamed.get("layersAdded").is_none(), "map.rename must not report the map's layers as added: {renamed}"); // A layer added to a map the command also created is still reported. - tools::run(&mut s, &t("run_command", json!({"command": "map.new", "params": {"name": "Fresh"}}))).unwrap(); + run_tool_call(&mut s, &t("run_command", json!({"command": "map.new", "params": {"name": "Fresh"}}))).unwrap(); let fresh: Value = - serde_json::from_str(&tools::run(&mut s, &t("run_command", json!({"command": "layer.addDataText", "params": pts}))).unwrap()) + serde_json::from_str(&run_tool_call(&mut s, &t("run_command", json!({"command": "layer.addDataText", "params": pts}))).unwrap()) .unwrap(); assert!(fresh.get("layersAdded").and_then(Value::as_array).is_some_and(|a| a.len() == 1), "{fresh}"); - tools::run(&mut s, &t("run_command", json!({"command": "map.activate", "params": {"name": "Renamed"}}))).unwrap(); + run_tool_call(&mut s, &t("run_command", json!({"command": "map.activate", "params": {"name": "Renamed"}}))).unwrap(); // Real adds still report layersAdded. let buffered: Value = serde_json::from_str( - &tools::run(&mut s, &t("run_tool", json!({"tool": "buffer", "params": {"input": "Cities", "distance": 100}}))).unwrap(), + &run_tool_call(&mut s, &t("run_tool", json!({"tool": "buffer", "params": {"input": "Cities", "distance": 100}}))).unwrap(), ) .unwrap(); let added = buffered.get("layersAdded").and_then(Value::as_array); @@ -488,10 +595,10 @@ fn read_features_reads_a_pivot_table() { let t = |name: &str, args: Value| ToolCall { id: "x".into(), name: name.into(), args }; let params = json!({"input": "Countries", "fields": "continent", "pivot": "income_grp", "value": "pop_est", "statistic": "SUM"}); let ran: Value = - serde_json::from_str(&tools::run(&mut s, &t("run_tool", json!({"tool": "pivotTable", "params": params}))).unwrap()).unwrap(); + serde_json::from_str(&run_tool_call(&mut s, &t("run_tool", json!({"tool": "pivotTable", "params": params}))).unwrap()).unwrap(); let name = ran["table"].as_str().map(str::to_string).unwrap_or_default(); assert_eq!(name, "Countries_Pivot", "{ran}"); - let read: Value = serde_json::from_str(&tools::run(&mut s, &t("read_features", json!({"table": name}))).unwrap()).unwrap(); + let read: Value = serde_json::from_str(&run_tool_call(&mut s, &t("read_features", json!({"table": name}))).unwrap()).unwrap(); let rows = read["rows"].as_array().cloned().unwrap_or_default(); assert!(!rows.is_empty() && read["total"].as_u64() == Some(rows.len() as u64), "{read}"); let mut cells = 0.0; @@ -518,7 +625,7 @@ fn read_features_reads_a_tools_output_table() { let mut s = sample(); let t = |name: &str, args: Value| ToolCall { id: "x".into(), name: name.into(), args }; let stats = json!({"tool": "summaryStatistics", "params": {"input": "Countries", "field": "pop_est", "case": "continent"}}); - let ran: Value = serde_json::from_str(&tools::run(&mut s, &t("run_tool", stats)).unwrap()).unwrap(); + let ran: Value = serde_json::from_str(&run_tool_call(&mut s, &t("run_tool", stats)).unwrap()).unwrap(); let name = ran["table"].as_str().map(str::to_string).unwrap_or_default(); assert!(!name.is_empty(), "run_tool must name the table it added: {ran}"); let groups = s.project.tables.last().map(|t| t.features.len()).unwrap_or_default(); @@ -530,7 +637,7 @@ fn read_features_reads_a_tools_output_table() { json!({"layer": name.to_uppercase()}), json!({"layer": name, "where": "COUNT_pop_est > 0", "order_by": "SUM_pop_est DESC"}), ] { - let read: Value = serde_json::from_str(&tools::run(&mut s, &t("read_features", args.clone())).unwrap()).unwrap(); + let read: Value = serde_json::from_str(&run_tool_call(&mut s, &t("read_features", args.clone())).unwrap()).unwrap(); assert_eq!(read["table"], json!(name), "{args}: {read}"); assert!(read.get("layer").is_none(), "{read}"); assert_eq!(read["total"].as_u64(), Some(groups as u64), "{read}"); @@ -538,20 +645,20 @@ fn read_features_reads_a_tools_output_table() { assert!(!rows.is_empty() && rows.iter().all(|r| r.get("_lon").is_none() && r["continent"].is_string()), "{read}"); } let sorted: Value = - serde_json::from_str(&tools::run(&mut s, &t("read_features", json!({"table": name, "order_by": "SUM_pop_est DESC"}))).unwrap()) + serde_json::from_str(&run_tool_call(&mut s, &t("read_features", json!({"table": name, "order_by": "SUM_pop_est DESC"}))).unwrap()) .unwrap(); let sums: Vec = sorted["rows"].as_array().unwrap().iter().filter_map(|r| r["SUM_pop_est"].as_f64()).collect(); assert!(sums.windows(2).all(|w| w[0] >= w[1]), "{sums:?}"); - let sel = tools::run(&mut s, &t("read_features", json!({"table": name, "selected": true}))); + let sel = run_tool_call(&mut s, &t("read_features", json!({"table": name, "selected": true}))); assert!(sel.as_ref().is_err_and(|e| e.contains("no selection")), "{sel:?}"); - let missing = tools::run(&mut s, &t("read_features", json!({"layer": "No such thing"}))); + let missing = run_tool_call(&mut s, &t("read_features", json!({"layer": "No such thing"}))); assert!(missing.as_ref().is_err_and(|e| e.contains("no layer or standalone table named")), "{missing:?}"); - assert!(tools::run(&mut s, &t("read_features", json!({"table": "Countries"}))).is_err(), "a layer is not a table"); - assert!(tools::run(&mut s, &t("read_features", json!({}))).is_err()); + assert!(run_tool_call(&mut s, &t("read_features", json!({"table": "Countries"}))).is_err(), "a layer is not a table"); + assert!(run_tool_call(&mut s, &t("read_features", json!({}))).is_err()); // An unused `table` sent as null or empty text leaves `layer` in charge. for unused in [json!(null), json!(""), json!(" ")] { let read: Value = serde_json::from_str( - &tools::run(&mut s, &t("read_features", json!({"layer": "Cities", "table": unused, "limit": 1}))).unwrap(), + &run_tool_call(&mut s, &t("read_features", json!({"layer": "Cities", "table": unused, "limit": 1}))).unwrap(), ) .unwrap(); assert_eq!(read["layer"], json!("Cities"), "{unused}: {read}"); @@ -561,8 +668,8 @@ fn read_features_reads_a_tools_output_table() { s.run("layer.addDataText", &json!({"fileName": file, "text": format!("k,v\na,{v}\n")})).unwrap(); } for name in ["stats", "Stats"] { - let by_layer = tools::run(&mut s, &t("read_features", json!({"layer": name}))).unwrap(); - assert_eq!(by_layer, tools::run(&mut s, &t("read_features", json!({"table": name}))).unwrap(), "{name}"); + let by_layer = run_tool_call(&mut s, &t("read_features", json!({"layer": name}))).unwrap(); + assert_eq!(by_layer, run_tool_call(&mut s, &t("read_features", json!({"table": name}))).unwrap(), "{name}"); assert!(by_layer.contains(&format!("\"table\":\"{name}\"")), "{by_layer}"); } // Names are matched as written, by `layer` as by `table`: `stats ` and ` Stats` are tables of @@ -576,31 +683,31 @@ fn read_features_reads_a_tools_output_table() { for (name, v) in [("stats", 1.0), ("Stats", 2.0), ("stats ", 3.0), (" Stats", 4.0)] { for arg in ["layer", "table"] { let args = Value::Object([(arg.to_string(), json!(name))].into_iter().collect()); - let read: Value = serde_json::from_str(&tools::run(&mut s, &t("read_features", args)).unwrap()).unwrap(); + let read: Value = serde_json::from_str(&run_tool_call(&mut s, &t("read_features", args)).unwrap()).unwrap(); assert_eq!((read["table"].as_str(), read["rows"][0]["v"].as_f64()), (Some(name), Some(v)), "{arg} {name:?}: {read}"); } } for name in ["stats ", " Stats"] { - let by_layer = tools::run(&mut s, &t("read_features", json!({"layer": name}))); + let by_layer = run_tool_call(&mut s, &t("read_features", json!({"layer": name}))); assert!(by_layer.as_ref().is_err_and(|e| e.contains("no layer or standalone table named")), "{name:?}: {by_layer:?}"); - let by_table = tools::run(&mut s, &t("read_features", json!({"table": name}))); + let by_table = run_tool_call(&mut s, &t("read_features", json!({"table": name}))); assert!(by_table.as_ref().is_err_and(|e| e.contains("no standalone table named")), "{name:?}: {by_table:?}"); } let id = s.project.tables.last().map(|t| t.id).unwrap_or_default(); // A layer with that id reads; otherwise it's an error, never the table. - let by_id = tools::run(&mut s, &t("read_features", json!({"layer": id.to_string()}))); + let by_id = run_tool_call(&mut s, &t("read_features", json!({"layer": id.to_string()}))); let read_a_layer = match &by_id { Ok(r) => r.contains("\"layer\"") && !r.contains("\"table\""), Err(e) => e.contains("no layer or standalone table named"), }; assert!(read_a_layer, "{by_id:?}"); - assert!(tools::run(&mut s, &t("read_features", json!({"table": id.to_string()}))).is_ok()); + assert!(run_tool_call(&mut s, &t("read_features", json!({"table": id.to_string()}))).is_ok()); // A table named like a layer: `layer` reads the layer. s.run("layer.addDataText", &json!({"fileName": "Cities.csv", "text": "k,v\na,1\n"})).unwrap(); let city: Value = - serde_json::from_str(&tools::run(&mut s, &t("read_features", json!({"layer": "Cities", "limit": 1}))).unwrap()).unwrap(); + serde_json::from_str(&run_tool_call(&mut s, &t("read_features", json!({"layer": "Cities", "limit": 1}))).unwrap()).unwrap(); assert_eq!(city["layer"], json!("Cities"), "{city}"); - let tab: Value = serde_json::from_str(&tools::run(&mut s, &t("read_features", json!({"table": "Cities"}))).unwrap()).unwrap(); + let tab: Value = serde_json::from_str(&run_tool_call(&mut s, &t("read_features", json!({"table": "Cities"}))).unwrap()).unwrap(); assert_eq!((tab["table"].as_str(), tab["total"].as_u64()), (Some("Cities"), Some(1)), "{tab}"); } @@ -608,15 +715,15 @@ fn read_features_reads_a_tools_output_table() { fn geoprocessing_and_reading_data() { let mut s = sample(); let t = |name: &str, args: Value| ToolCall { id: "x".into(), name: name.into(), args }; - let found = tools::run(&mut s, &t("search_tools", json!({"query": "buffer"}))).unwrap_or_default(); + let found = run_tool_call(&mut s, &t("search_tools", json!({"query": "buffer"}))).unwrap_or_default(); assert!(found.contains("\"buffer\""), "{found}"); - let desc = tools::run(&mut s, &t("describe_tool", json!({"tool": "buffer"}))).unwrap_or_default(); + let desc = run_tool_call(&mut s, &t("describe_tool", json!({"tool": "buffer"}))).unwrap_or_default(); assert!(desc.contains("distance"), "{desc}"); let before = s.map().layers.len(); - let ran = tools::run(&mut s, &t("run_tool", json!({"tool": "buffer", "params": {"input": "Cities", "distance": 100}}))); + let ran = run_tool_call(&mut s, &t("run_tool", json!({"tool": "buffer", "params": {"input": "Cities", "distance": 100}}))); assert!(ran.as_deref().is_ok_and(|r| r.contains("layersAdded")), "{ran:?}"); assert_eq!(s.map().layers.len(), before + 1); - let rows = tools::run( + let rows = run_tool_call( &mut s, &t("read_features", json!({"layer": "Cities", "where": "pop_max > 10000000", "fields": ["name", "pop_max"], "order_by": "pop_max", "descending": true, "limit": 3})), ) @@ -626,9 +733,9 @@ fn geoprocessing_and_reading_data() { let r = v["rows"].as_array().cloned().unwrap_or_default(); assert_eq!(r.len(), 3); assert!(r[0]["pop_max"].as_i64() >= r[1]["pop_max"].as_i64()); - assert!(tools::run(&mut s, &t("read_features", json!({"layer": "Cities", "fields": ["nope"]}))).is_err()); - assert!(tools::run(&mut s, &t("read_features", json!({"layer": "Cities", "fields": [1, null]}))).is_err()); - let cmds = tools::run(&mut s, &t("search_commands", json!({"query": "basemap"}))).unwrap_or_default(); + assert!(run_tool_call(&mut s, &t("read_features", json!({"layer": "Cities", "fields": ["nope"]}))).is_err()); + assert!(run_tool_call(&mut s, &t("read_features", json!({"layer": "Cities", "fields": [1, null]}))).is_err()); + let cmds = run_tool_call(&mut s, &t("search_commands", json!({"query": "basemap"}))).unwrap_or_default(); assert!(cmds.contains("map.basemap"), "{cmds}"); } @@ -660,13 +767,13 @@ fn hostile_arguments_never_panic() { let c = ToolCall { id: "x".into(), name: def.name.into(), args: args.clone() }; let _ = tools::gate(&s, &c); let _ = tools::summary(&c); - if let Ok(r) = tools::run(&mut s, &c) { + if let Ok(r) = run_tool_call(&mut s, &c) { assert!(r.len() <= tools::MAX_RESULT); assert!(serde_json::from_str::(&r).is_ok()); } } } - let _ = tools::run(&mut s, &ToolCall { id: "x".into(), name: "rm_rf".into(), args: Value::Null }); + let _ = run_tool_call(&mut s, &ToolCall { id: "x".into(), name: "rm_rf".into(), args: Value::Null }); } #[test] @@ -909,7 +1016,7 @@ fn ordering_mixed_columns_never_panics() { name: "read_features".into(), args: json!({"layer": "Cities", "order_by": "name DESC", "fields": ["name"], "limit": 30}), }; - let r = tools::run(&mut s, &t); + let r = run_tool_call(&mut s, &t); let v: Value = r.as_deref().ok().and_then(|r| serde_json::from_str(r).ok()).unwrap_or(Value::Null); let rows = v["rows"].as_array().cloned().unwrap_or_default(); assert_eq!(rows.len(), 30, "{r:?}"); @@ -966,13 +1073,22 @@ fn stopping_a_request_drops_its_message() { fn malformed_params_are_refused() { let mut s = sample(); let n = s.map().layers.len(); - for params in [json!("{\"layer\": "), json!("[1]"), json!(5)] { - let c = ToolCall { id: "x".into(), name: "run_command".into(), args: json!({"command": "layer.remove", "params": params}) }; - assert!(tools::run(&mut s, &c).is_err()); + for params in [json!("{\"layer\": "), json!("[1]"), json!([1]), json!(5), json!("false")] { + for (name, args) in [ + ("run_command", json!({"command": "layer.remove", "params": params})), + ("run_tool", json!({"tool": "buffer", "params": params})), + ] { + let c = ToolCall { id: "x".into(), name: name.into(), args: args.clone() }; + assert!(tools::target(&c).is_none(), "invalid params aren't classified as defaults"); + assert!(tools::gate(&s, &c).is_some_and(|e| e.contains("params")) && tools::policy_call(&c).is_err()); + assert!(run_tool_call(&mut s, &c).is_err()); + let (error, why) = call_tool(&mcp::Server::new(true), &mut s, name, args); + assert!(error && why.contains("params"), "{why}"); + assert_eq!(s.map().layers.len(), n, "a malformed envelope must not remove the active layer or run a tool"); + } } - assert_eq!(s.map().layers.len(), n, "no layer may be removed by a call whose parameters didn't parse"); let c = ToolCall { id: "x".into(), name: "run_command".into(), args: json!({"command": "map.zoomFull", "params": "{}"}) }; - assert!(tools::run(&mut s, &c).is_ok()); + assert!(run_tool_call(&mut s, &c).is_ok()); } #[test] @@ -1128,6 +1244,59 @@ fn mcp_snapshot_draws_offscreen_and_saves_with_access() { assert!(tools::gate(&s, &plain).is_none()); } +/// Image adapters share map-render source reach and exact private routes with command exports. +#[test] +fn permission_render_adapters_require_and_bind_remote_tile_sources() { + use rustgis_engine::policy::{AgentAccess, Effect, Effects, Outcome, Revocation, Scope}; + use std::sync::Arc; + struct LocalOnly; + impl Revocation for LocalOnly { + fn allows(&self, effects: Effects) -> bool { + !effects.contains(Effect::Network) + } + } + let local = Scope::agent(AgentAccess::Allow, false, "adapter-render-local", None, Arc::new(LocalOnly)); + let mut missed = Vec::new(); + for name in ["snapshot", "view_map"] { + let mut s = Session::new(Project::default()); + let call = ToolCall { id: name.into(), name: name.into(), args: json!({}) }; + let engine = tools::policy_call(&call).unwrap().unwrap(); + assert!(matches!(s.review(&local, &engine), Outcome::Allowed), "an in-memory map stays offline"); + s.map_mut().tiles.push(rustgis_engine::map::TileLayer::new( + "Remote image", + rustgis_engine::map::TileKind::Raster, + "https://adapter-render.invalid/{z}/{x}/{y}.png?token=RGSYNTH_adapter_first", + )); + if !matches!(s.review(&local, &engine), Outcome::Refused(_)) { + missed.push(name); + } + let scope = Scope::assistant(1521, Arc::new(tools::Live::default())); + if let Outcome::NeedsApproval(pending) = s.review(&scope, &engine) { + assert!(!format!("{:?}", pending.review()).contains("RGSYNTH_adapter_")); + let remembered = pending.remember().unwrap(); + let permit = pending.allow(); + s.map_mut().tiles[0].url = "https://adapter-render.invalid/{z}/{x}/{y}.png?token=RGSYNTH_adapter_changed".into(); + let Outcome::NeedsApproval(changed) = s.review(&scope, &engine) else { panic!("changed render still needs approval") }; + assert!(!changed.covered_by(&remembered), "redaction cannot hide a changed tile route"); + assert!(s.defer(scope.clone(), &engine, Some(permit)).is_err(), "the changed route is refused before rendering"); + let held = s.defer(scope, &engine, Some(changed.allow())).unwrap(); + s.map_mut().tiles[0].url = "https://adapter-render.invalid/{z}/{x}/{y}.png?token=RGSYNTH_adapter_after_defer".into(); + let mut called = false; + let error = s + .finish_deferred(held, |_| { + called = true; + }) + .unwrap_err(); + assert!(!called && error.contains("changed"), "the live render callback never sees a route changed after admission: {error}"); + } else { + missed.push(name); + } + s.map_mut().tiles[0].visible = false; + assert!(matches!(s.review(&local, &engine), Outcome::Allowed), "hidden remote tiles are not fetched"); + } + assert!(missed.is_empty(), "image adapters omitted remote tile admission: {missed:?}"); +} + /// Only the desktop relay, where the session is the user's open project, tells the agent the /// user is watching. #[test] @@ -1492,13 +1661,13 @@ fn docs_tools_are_the_assistants_too() { assert!(names.contains(&"search_docs") && names.contains(&"read_doc")); let mut s = sample(); let call = |name: &str, args: Value| ToolCall { id: "d".into(), name: name.into(), args }; - let found = tools::run(&mut s, &call("search_docs", json!({"query": "basemap", "limit": 3}))).unwrap(); + let found = run_tool_call(&mut s, &call("search_docs", json!({"query": "basemap", "limit": 3}))).unwrap(); assert!(found.contains("basemap") || found.contains("Basemap"), "{found}"); - assert!(tools::run(&mut s, &call("read_doc", json!({"page": "cli.md"}))).unwrap().contains("rustgis-cli")); - assert!(tools::run(&mut s, &call("read_doc", json!({"page": "nope.md"}))).is_err()); + assert!(run_tool_call(&mut s, &call("read_doc", json!({"page": "cli.md"}))).unwrap().contains("rustgis-cli")); + assert!(run_tool_call(&mut s, &call("read_doc", json!({"page": "nope.md"}))).is_err()); // Every page is complete JSON, including escaped Markdown, and fits the shared byte budget. for p in crate::docs::PAGES { - let page = tools::run(&mut s, &call("read_doc", json!({"page": p.path}))).unwrap(); + let page = run_tool_call(&mut s, &call("read_doc", json!({"page": p.path}))).unwrap(); assert!(page.len() <= tools::MAX_RESULT); assert!(serde_json::from_str::(&page).is_ok_and(|v| v["page"] == p.path), "{}", p.path); } @@ -2286,12 +2455,13 @@ fn tool_output_truncation_is_valid_json() { } let mut s = sample(); let search = - tools::run(&mut s, &ToolCall { id: "s".into(), name: "search_docs".into(), args: json!({"query": "map", "limit": 30}) }).unwrap(); + run_tool_call(&mut s, &ToolCall { id: "s".into(), name: "search_docs".into(), args: json!({"query": "map", "limit": 30}) }) + .unwrap(); let parsed: Value = serde_json::from_str(&search).unwrap(); assert_eq!(parsed["results"].as_array().unwrap().len(), 30); assert!(search.len() > 8000, "exercise the former mid-JSON clipping boundary"); for query in ["map", "layer", "data", "the"] { - let result = tools::run_with_budget( + let result = run_tool_call_with_budget( &mut s, &ToolCall { id: "s".into(), name: "search_docs".into(), args: json!({"query": query, "limit": 30}) }, 819, @@ -2309,7 +2479,7 @@ fn documentation_reads_resume_without_gaps() { let mut offset = 1u64; let mut text = String::new(); loop { - let result = tools::run_with_budget( + let result = run_tool_call_with_budget( &mut s, &ToolCall { id: "r".into(), @@ -2389,15 +2559,15 @@ fn documentation_offsets_reject_invalid_values() { for offset in [json!(null), json!(0), json!(-1), json!(1.5), json!("2"), json!([]), json!({}), json!(1e20)] { let args = json!({"page": "cli.md", "offset": offset}); let call = ToolCall { id: "r".into(), name: "read_doc".into(), args: args.clone() }; - assert!(tools::run(&mut s, &call).unwrap_err().contains("offset must be a positive integer")); + assert!(run_tool_call(&mut s, &call).unwrap_err().contains("offset must be a positive integer")); let (error, text) = call_tool(&server, &mut s, "read_doc", args); assert!(error && text.contains("offset must be a positive integer")); } assert!(crate::docs::read_with_budget("cli.md", None, 0, 2000, tools::MAX_RESULT).is_err()); let read = |args| ToolCall { id: "r".into(), name: "read_doc".into(), args }; - let default = tools::run(&mut s, &read(json!({"page": "cli.md"}))).unwrap(); - assert_eq!(default, tools::run(&mut s, &read(json!({"page": "cli.md", "offset": 1}))).unwrap()); - assert_ne!(default, tools::run(&mut s, &read(json!({"page": "cli.md", "offset": 2}))).unwrap()); + let default = run_tool_call(&mut s, &read(json!({"page": "cli.md"}))).unwrap(); + assert_eq!(default, run_tool_call(&mut s, &read(json!({"page": "cli.md", "offset": 1}))).unwrap()); + assert_ne!(default, run_tool_call(&mut s, &read(json!({"page": "cli.md", "offset": 2}))).unwrap()); } #[test] @@ -3218,7 +3388,9 @@ fn acp_agent_output_is_bounded() { use crate::acp::{Event, MAX_TEXT, Session}; let (mut s, _) = Session::new("Codex", "x", "/w", vec!["cli".into()], false); s.line(r#"{"jsonrpc":"2.0","id":0,"result":{"protocolVersion":1,"agentCapabilities":{},"authMethods":[]}}"#); - s.line(r#"{"jsonrpc":"2.0","id":1,"result":{"sessionId":"sess_1"}}"#); + s.line( + r#"{"jsonrpc":"2.0","id":1,"result":{"sessionId":"sess_1","modes":{"currentModeId":"agent","availableModes":[{"id":"agent"}]}}}"#, + ); // A message streamed in huge pieces (multi-byte, to cut on a character) stops growing at // the cap. let chunk = "é".repeat(300 * 1024); @@ -3262,7 +3434,9 @@ fn acp_sessions_run_a_task_ask_permission_and_report() { ); assert!(s.cancel().is_none(), "nothing to cancel before the session exists"); // session/new → session/prompt with the brief and the task. - let (out, _) = s.line(r#"{"jsonrpc":"2.0","id":1,"result":{"sessionId":"sess_1"}}"#); + let (out, _) = s.line( + r#"{"jsonrpc":"2.0","id":1,"result":{"sessionId":"sess_1","modes":{"currentModeId":"agent","availableModes":[{"id":"agent"}]}}}"#, + ); let prompt: Value = serde_json::from_str(&out[0]).unwrap(); assert_eq!((prompt["method"].as_str(), prompt["params"]["sessionId"].as_str()), (Some("session/prompt"), Some("sess_1"))); assert!(prompt["params"]["prompt"][0]["text"].as_str().unwrap().ends_with("Task: Map the rivers")); @@ -3301,7 +3475,7 @@ fn acp_sessions_run_a_task_ask_permission_and_report() { // The thread waits: a follow-up goes to the same session, with the next id, and its report // is its own. assert!(s.idle() && !s.finished() && s.cancel().is_none()); - let follow: Value = serde_json::from_str(&s.follow_up("Colour them by flow").unwrap()[0]).unwrap(); + let follow: Value = serde_json::from_str(&s.follow_up("Colour them by flow").unwrap().0[0]).unwrap(); assert_eq!( (follow["method"].as_str(), follow["id"].as_u64(), follow["params"]["sessionId"].as_str()), (Some("session/prompt"), Some(3), Some("sess_1")) @@ -3334,7 +3508,7 @@ fn acp_sessions_run_a_task_ask_permission_and_report() { assert!(matches!(&ev[0], Event::Done { error: true, report } if report.contains("sign in"))); let ev = fail(&[ r#"{"jsonrpc":"2.0","id":0,"result":{"protocolVersion":1}}"#, - r#"{"jsonrpc":"2.0","id":1,"result":{"sessionId":"s"}}"#, + r#"{"jsonrpc":"2.0","id":1,"result":{"sessionId":"s","modes":{"currentModeId":"agent","availableModes":[{"id":"agent"}]}}}"#, r#"{"jsonrpc":"2.0","id":2,"result":{"stopReason":"cancelled"}}"#, ]); assert!(matches!(&ev[0], Event::Done { error: true, report } if report.contains("stopped"))); @@ -3364,6 +3538,9 @@ fn acp_agents_are_put_in_a_mode_that_works_and_asks_or_acts_alone() { // The task goes once the agent answers. let (out, _) = s.line(&json!({"jsonrpc": "2.0", "id": set["id"], "result": {}}).to_string()); assert_eq!(method(&out[0])["method"], "session/prompt"); + // An unfamiliar current mode is never assumed to ask; negotiate a known asking mode. + let (_, out) = start(false, "surprise"); + assert_eq!(method(&out[0])["params"]["modeId"], "agent"); // Already in a mode that works: the task goes at once. let (_, out) = start(false, "agent"); assert_eq!(method(&out[0])["method"], "session/prompt"); @@ -3375,13 +3552,103 @@ fn acp_agents_are_put_in_a_mode_that_works_and_asks_or_acts_alone() { s.line(r#"{"jsonrpc":"2.0","id":2,"result":{"stopReason":"end_turn"}}"#); // The setting turned off before a follow-up: back to asking, then the follow-up. s.set_auto(false); - let lines = s.follow_up("Next").unwrap(); + let (lines, _) = s.follow_up("Next").unwrap(); assert_eq!(method(&lines[0])["params"]["modeId"], "agent"); - // A refused mode change: the task goes anyway, in the mode it's in. + // A refused auto-on upgrade keeps the task going in its confirmed asking mode. let (mut s, out) = start(true, "agent"); let (out, _) = s.line(&json!({"jsonrpc": "2.0", "id": method(&out[0])["id"], "error": {"code": -32602, "message": "no"}}).to_string()); assert_eq!(method(&out[0])["method"], "session/prompt"); - // An agent without modes just gets the task. + // A failed downgrade refuses both the initial task and a follow-up, with a report. + let (mut s, out) = start(false, "agent-full-access"); + let refused = json!({"jsonrpc": "2.0", "id": method(&out[0])["id"], "error": {"code": -32602, "message": "no"}}); + let (out, events) = s.line(&refused.to_string()); + assert!(out.is_empty() && s.idle()); + assert!( + events + .iter() + .any(|e| matches!(e, crate::acp::Event::Done { error: true, report } if report.contains("refused") && report.contains("mode"))) + ); + let (out, _) = s.follow_up("Still asks").unwrap(); + assert_eq!(method(&out[0])["method"], "session/set_mode"); + // Mode changes never overlap: the agent could otherwise apply the old upgrade after + // a successful downgrade. Wait for the upgrade, then negotiate the latest setting. + let (mut s, upgrade) = start(true, "agent"); + assert!(s.set_auto(false).0.is_empty()); + let (downgrade, _) = s.line(&json!({"id": method(&upgrade[0])["id"], "result": {}}).to_string()); + assert_eq!(method(&downgrade[0])["params"]["modeId"], "agent"); + assert_ne!(method(&upgrade[0])["id"], method(&downgrade[0])["id"]); + assert!(s.line(&json!({"id": method(&upgrade[0])["id"], "result": {}}).to_string()).0.is_empty()); + assert!(s.line(&json!({"id": method(&upgrade[0])["id"], "error": {"message": "stale"}}).to_string()).1.is_empty()); + let (out, _) = s.line(&json!({"id": method(&downgrade[0])["id"], "result": {}}).to_string()); + assert_eq!(method(&out[0])["method"], "session/prompt"); + // A malformed success cannot establish asking mode or release the held task. + for result in [Value::Null, json!(false), json!([]), json!("ok")] { + let (mut s, out) = start(false, "agent-full-access"); + let (out, events) = s.line(&json!({"id": method(&out[0])["id"], "result": result}).to_string()); + assert!(out.is_empty() && s.idle(), "{result}"); + assert!(events.iter().any(|e| matches!(e, crate::acp::Event::Done { error: true, .. }))); + } + let (mut s, out) = start(false, "agent-full-access"); + let (out, events) = s.line(&json!({"id": method(&out[0])["id"]}).to_string()); + assert!(out.is_empty() && s.idle()); + assert!(events.iter().any(|e| matches!(e, crate::acp::Event::Done { error: true, .. }))); + // Auto-on may continue after an invalid upgrade response, but turning auto off must + // contain the now-unconfirmed mode rather than trust the old asking-mode report. + let (mut s, out) = start(true, "agent"); + let (out, _) = s.line(&json!({"id": method(&out[0])["id"], "result": null}).to_string()); + assert_eq!(method(&out[0])["method"], "session/prompt"); + let (out, _) = s.set_auto(false); + assert_eq!(method(&out[0])["method"], "session/cancel"); + assert_eq!(method(&out[1])["params"]["modeId"], "agent"); + // An active bypass turn is cancelled as well as downgraded. A follow-up waits for + // cancellation to be acknowledged, then goes through readiness again. + let (mut s, out) = start(true, "agent-full-access"); + assert_eq!(method(&out[0])["method"], "session/prompt"); + let (out, events) = s.set_auto(false); + assert_eq!( + out.iter().map(|l| method(l)["method"].clone()).collect::>(), + json!(["session/cancel", "session/set_mode"]).as_array().unwrap().clone() + ); + assert!(events.iter().any(|e| matches!(e, crate::acp::Event::Text(t) if t.contains("automatic approval")))); + assert!(s.set_auto(false).0.is_empty() && s.follow_up("too early").is_none()); + let downgrade = method(&out[1]); + s.line(r#"{"id":2,"result":{"stopReason":"cancelled"}}"#); + let (out, _) = s.follow_up("Wait for asking").unwrap(); + assert!(out.is_empty()); + let (out, _) = s.line(&json!({"id": downgrade["id"], "result": {}}).to_string()); + assert_eq!(method(&out[0])["method"], "session/prompt"); + // The same containment applies to an unsolicited mode change during an auto-off turn. + let (mut s, _) = start(false, "agent"); + let (out, _) = s.line(&json!({"method": "session/update", "params": {"update": {"sessionUpdate": "current_mode_update", "currentModeId": "agent-full-access"}}}).to_string()); + assert_eq!(method(&out[0])["method"], "session/cancel"); + assert_eq!(method(&out[1])["params"]["modeId"], "agent"); + // No asking mode: a bypass or unverified agent gets no task. Read-only still works. + for modes in [ + json!({"currentModeId": "agent-full-access", "availableModes": [{"id": "agent-full-access"}]}), + json!({"currentModeId": "surprise", "availableModes": [{"id": "surprise"}]}), + json!({"currentModeId": "agent", "availableModes": ["agent"]}), + json!({"currentModeId": "agent", "availableModes": [{"id": "agent"}, {"id": 5}]}), + json!({"currentModeId": "agent", "availableModes": []}), + Value::Null, + ] { + let (mut s, _) = Session::new("Unverified", "x", "/w", vec![], false); + s.line(r#"{"id":0,"result":{"protocolVersion":1}}"#); + let (out, events) = s.line(&json!({"id":1,"result":{"sessionId":"s","modes":modes}}).to_string()); + assert!(out.is_empty() && s.idle(), "{modes}"); + assert!(events.iter().any(|e| matches!(e, crate::acp::Event::Done { error: true, .. })), "{events:?}"); + assert!(s.follow_up("no bypass").unwrap().0.is_empty()); + s.set_auto(true); + assert_eq!(method(&s.follow_up("explicit auto").unwrap().0[0])["method"], "session/prompt"); + let (out, _) = s.set_auto(false); + assert_eq!(method(&out[0])["method"], "session/cancel", "an unverified running mode is contained too"); + } + let (mut s, _) = Session::new("Read only", "x", "/w", vec![], false); + s.line(r#"{"id":0,"result":{"protocolVersion":1}}"#); + let (out, _) = + s.line(r#"{"id":1,"result":{"sessionId":"s","modes":{"currentModeId":"read-only","availableModes":[{"id":"read-only"}]}}}"#); + assert_eq!(method(&out[0])["method"], "session/prompt"); + // With automatic approval on, an agent without modes still gets the task. + let (mut s, _) = Session::new("goose", "x", "/w", vec![], true); s.line(r#"{"jsonrpc":"2.0","id":0,"result":{"protocolVersion":1}}"#); let (out, _) = s.line(r#"{"jsonrpc":"2.0","id":1,"result":{"sessionId":"g"}}"#); @@ -3840,7 +4107,7 @@ fn acp_permissions_never_upgrade_once_to_always_and_stale_errors_are_ignored() { } let (mut s, _) = Session::new("Agent", "task", "/work", vec![], false); s.line(r#"{"jsonrpc":"2.0","id":0,"result":{"protocolVersion":1,"authMethods":[]}}"#); - s.line(r#"{"jsonrpc":"2.0","id":1,"result":{"sessionId":"s"}}"#); + s.line(r#"{"jsonrpc":"2.0","id":1,"result":{"sessionId":"s","modes":{"currentModeId":"agent","availableModes":[{"id":"agent"}]}}}"#); s.line(r#"{"jsonrpc":"2.0","id":2,"result":{"stopReason":"end_turn"}}"#); assert!(s.idle()); assert!(s.line(r#"{"jsonrpc":"2.0","id":2,"error":{"code":-32000,"message":"Authentication required"}}"#).1.is_empty()); @@ -4303,7 +4570,7 @@ fn list_layers_includes_web_tile_layers() { s.run("map.addTiles", &json!({"url": wms, "kind": "raster", "name": "Rain radar", "overlay": true})).unwrap(); s.run("map.setTiles", &json!({"name": "Rain radar", "visible": false})).unwrap(); let call = ToolCall { id: "x".into(), name: "list_layers".into(), args: json!({}) }; - let out: Value = serde_json::from_str(&tools::run(&mut s, &call).unwrap()).unwrap(); + let out: Value = serde_json::from_str(&run_tool_call(&mut s, &call).unwrap()).unwrap(); let tiles = out["tileLayers"].as_array().cloned().unwrap_or_default(); let radar = tiles.iter().find(|t| t["name"] == "Rain radar"); assert!(radar.is_some_and(|t| t["visible"] == false && t["overlay"] == true && t["kind"] == "raster" && t["index"].is_u64()), "{out}"); @@ -4365,3 +4632,141 @@ fn peer_replacement_cancels_approvals_and_stale_replies() { assert!(agent.waiting() && agent.pending(&s).is_none()); assert_eq!(agent.transcript.len(), transcript); } + +/// The existing real Wasm fixture crosses Agent pending/decide and MCP, by id and display name. +#[test] +fn permission_policy_real_plugin_approval_and_changed_first_card() { + use rustgis_engine::plugins; + let mut s = sample(); + let mut package = plugins::Package::from_zip(&plugins::testing::package("assistant-policy-real", &["edit"])).unwrap(); + package.manifest.tools.iter_mut().find(|t| t.id == "x").unwrap().name = "Reviewed selection fixture".into(); + package.manifest.tools.iter_mut().find(|t| t.id == "m").unwrap().name = "Buffer".into(); + plugins::load(package, "test").unwrap(); + for tool in ["plugin:assistant-policy-real/x", "Reviewed selection fixture"] { + for (name, args) in [ + ("run_tool", json!({"tool":tool,"params":{"input":"Cities"}})), + ("run_tool", json!({"tool":tool,"params":"{\"input\":\"Cities\"}"})), + ("run_command", json!({"command":"gp.run","params":{"tool":tool,"params":{"input":"Cities"}}})), + ("run_command", json!({"command":"gp.run","params":json!({"tool":tool,"params":{"input":"Cities"}}).to_string()})), + ("run", json!({"script":format!("gp.run {}",json!({"tool":tool,"params":{"input":"Cities"}}))})), + ] { + if !s.map().layer_by_name("Cities").unwrap().selection.is_empty() { + s.run("select.clear", &json!({"layer":"Cities"})).unwrap(); + } + assert!(call_tool(&mcp::Server::new(false), &mut s, name, args.clone()).0); + assert!(s.map().layer_by_name("Cities").unwrap().selection.is_empty()); + let mut a = Agent::new(cfg("openai"), &s); + a.ask("select", &s); + assert_eq!(a.receive(a.generation(), ok(call("real", name, args.clone())), &mut s), Step::Confirm); + assert!(!a.pending(&s).unwrap().2, "every plugin run asks separately"); + a.decide(true, true, &mut s); + assert_eq!(s.map().layer_by_name("Cities").unwrap().selection, [0, 2]); + if !s.map().layer_by_name("Cities").unwrap().selection.is_empty() { + s.run("select.clear", &json!({"layer":"Cities"})).unwrap(); + } + let (error, why) = call_tool(&mcp::Server::new(true), &mut s, name, args); + assert!(!error, "{why}"); + assert_eq!(s.map().layer_by_name("Cities").unwrap().selection, [0, 2]); + } + } + assert_eq!(rustgis_engine::analysis::resolve_published("Buffer").unwrap().spec().id, "buffer"); + s.run("layer.select", &json!({"layer":"Cities"})).unwrap(); + let mut a = Agent::new(cfg("openai"), &s); + a.ask("rename the active layer", &s); + let args = json!({"command":"plugin:assistant-policy-real/f","params":{"op":"layer.rename","params":{"name":"No stale rename"}}}); + assert_eq!(a.receive(a.generation(), ok(call("changed", "run_command", args)), &mut s), Step::Confirm); + let first = a.pending(&s).unwrap(); + assert!(first.0.contains("plugin:assistant-policy-real/f")); + s.run("layer.select", &json!({"layer":"Countries"})).unwrap(); + a.decide(true, false, &mut s); + assert!(s.map().layer_by_name("No stale rename").is_none()); + assert!(a.transcript.iter().any(|e| matches!(e,Entry::Tool{error:true,result,..} if result.contains("changed")))); + // The actual first shown item card also retains its original executable content. + s.run("task.new", &json!({"name":"Shown item","steps":[{"title":"Visibility","command":"layer.visibility","params":{"layer":"Cities","visible":false}}]})) + .unwrap(); + let mut a = Agent::new(cfg("openai"), &s); + a.ask("run the task", &s); + assert_eq!( + a.receive(a.generation(), ok(call("item", "run_command", json!({"command":"task.run","params":{"task":"Shown item"}}))), &mut s), + Step::Confirm + ); + assert!(a.pending(&s).unwrap().0.contains("Shown item")); + let map = s.map().clone(); + s.run("task.addStep", &json!({"task":"Shown item","title":"Changed content","command":"map.zoomIn","params":{}})).unwrap(); + a.decide(true, false, &mut s); + assert_eq!(s.map().name, map.name); + assert_eq!(s.map().camera, map.camera); + assert_eq!(s.map().layer_by_name("Cities").unwrap().visible, map.layer_by_name("Cities").unwrap().visible); + assert!(a.transcript.iter().any(|e| matches!(e,Entry::Tool{error:true,result,..} if result.contains("changed")))); + plugins::unload("assistant-policy-real"); +} + +/// Non-Ask scripts have no approval-plan node/project budget, but deny the whole vehicle first. +#[test] +fn permission_policy_mcp_long_script_preflight_and_unrelated_project_size() { + let mut s = sample(); + let server = mcp::Server::new(true); + let script = "map.link {}\n".repeat(1001); + let (error, why) = call_tool(&server, &mut s, "run", json!({"script":script})); + assert!(!error, "{why}"); + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("never-export.png"); + assert!(!path.exists()); + let forbidden = format!( + "share.exportMap {}\n{}model.new {{\"name\":\"reserved later\"}}", + json!({"path":path,"width":64,"height":64}), + "map.link {}\n".repeat(1001) + ); + let (error, why) = call_tool(&server, &mut s, "run", json!({"script":forbidden})); + assert!(error && why.contains("reserved"), "{why}"); + assert!(!path.exists(), "whole-script refusal precedes the first irreversible line"); + let id = s.map().layer_by_name("Cities").unwrap().id; + s.map_mut().layer_mut(id).unwrap().dataset.source = Some("unrelated metadata".repeat(10_000)); + let (error, why) = call_tool(&server, &mut s, "run_command", json!({"command":"map.graticule","params":{"on":true}})); + assert!(!error, "{why}"); +} + +/// The actual pending card exposes effective saved targets and nested SQL's specialized reason. +#[test] +fn permission_policy_pending_effective_share_and_nested_sql() { + let mut s = sample(); + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("saved-cities.zip"); + s.run("share.webLayer", &json!({"layer":"Cities","path":path})).unwrap(); + s.run("layer.select", &json!({"layer":"Countries"})).unwrap(); + let mut a = Agent::new(cfg("openai"), &s); + a.ask("update the saved share", &s); + assert_eq!( + a.receive(a.generation(), ok(call("saved", "run_command", json!({"command":"share.replaceWebLayer","params":{}}))), &mut s), + Step::Confirm + ); + let shown = a.pending(&s).unwrap().0; + assert!(shown.contains("Cities") && shown.contains(&path.display().to_string()), "{shown}"); + assert!(a.pending_review().unwrap().defaults.is_none()); + a.decide(false, false, &mut s); + let params = json!({"sql":"FROM t","database":"/data/x.duckdb", "password":"RGSYNTH_direct_review_secret", + "metadata":{"script":format!("layer.addUrl {}", json!({"url":"https://example.invalid/data?token=RGSYNTH_url_review_secret","token":"RGSYNTH_nested_review_secret"}))}}); + let script = format!("layer.addSql {params}"); + s.run("task.new", &json!({"name":"SQL child","steps":[{"title":"Query","command":"layer.addSql","params":{"sql":"FROM t","database":"/data/x.duckdb"}}]})).unwrap(); + s.run("notebook.new", &json!({"name":"SQL notebook"})).unwrap(); + s.run("notebook.editCell", &json!({"notebook":"SQL notebook","cell":1,"source":script})).unwrap(); + for (name, args) in [ + ("run", json!({"script":script})), + ("run_command", json!({"command":"task.run","params":{"task":"SQL child"}})), + ("run_command", json!({"command":"notebook.run","params":{"notebook":"SQL notebook","cell":1}})), + ] { + let mut a = Agent::new(cfg("openai"), &s); + a.ask("query", &s); + assert_eq!(a.receive(a.generation(), ok(call("sql", name, args)), &mut s), Step::Confirm); + let (what, why, _) = a.pending(&s).unwrap(); + assert!(why.contains("views in the database /data/x.duckdb"), "{why}"); + if name == "run" { + assert_eq!(a.pending_review().unwrap().lines, [(1, "layer.addSql".into())]); + assert!(why.contains("line 1 (layer.addSql)"), "{why}"); + assert!(what.contains("FROM t") && what.contains("/data/x.duckdb"), "{what}"); + assert!(!what.contains("RGSYNTH_"), "secrets cannot become consent display data"); + assert!(!what.contains("structured content omitted"), "{what}"); + } + a.decide(false, false, &mut s); + } +} diff --git a/crates/assistant/src/tools.rs b/crates/assistant/src/tools.rs index 0dc2aa58a5..880a294d87 100644 --- a/crates/assistant/src/tools.rs +++ b/crates/assistant/src/tools.rs @@ -6,7 +6,7 @@ //! beyond the project (files, the network, the user's own automation) need the user's approval //! first ([`gate`]). -use rustgis_engine::cmd::plugins::{Reach, reaches, user_only}; +use rustgis_engine::policy::{Call, Outcome, Permit, Scope}; use rustgis_engine::{Session, analysis, project}; use serde_json::{Value, json}; @@ -142,99 +142,63 @@ pub fn defs() -> Vec { } /// The command a call runs and its parameters (`gp.run` for a tool), if it runs one. +/// Use execution's strict envelope parser for policy too: malformed input has no runnable target. pub fn target(call: &ToolCall) -> Option<(String, Value)> { match call.name.as_str() { - "run_command" => Some((str_arg(&call.args, "command").unwrap_or_default(), obj_arg(&call.args, "params"))), + "run_command" => Some((str_arg(&call.args, "command").unwrap_or_default(), params_arg(&call.args).ok()?)), "run_tool" => Some(( "gp.run".to_string(), - json!({"tool": str_arg(&call.args, "tool").unwrap_or_default(), "params": obj_arg(&call.args, "params")}), + json!({"tool": str_arg(&call.args, "tool").unwrap_or_default(), "params": params_arg(&call.args).ok()?}), )), _ => None, } } -/// Whether a call needs the user's approval, and why. +/// The executor's strict envelope is the only input to engine planning. +pub fn policy_call(call: &ToolCall) -> Result, String> { + let c = match call.name.as_str() { + "run_command" => Some(Call::command(&str_arg(&call.args, "command").ok_or("command is required")?, params_arg(&call.args)?)), + "run_tool" => Some(Call::tool(&str_arg(&call.args, "tool").ok_or("tool is required")?, params_arg(&call.args)?)), + "run" => Some(Call::script(call.args.get("script").and_then(Value::as_str).ok_or("script is required")?)), + "snapshot" | "view_map" => { + let files = call.name == "snapshot" && call.args.get("save_path").and_then(Value::as_str).is_some_and(|p| !p.trim().is_empty()); + let effects = if files { + rustgis_engine::policy::Effects::NONE.with(rustgis_engine::policy::Effect::Files) + } else { + rustgis_engine::policy::Effects::NONE + }; + Some(Call::adapter(if call.name == "snapshot" { "snapshot" } else { "view_map" }, effects, call.args.clone())) + } + _ => None, + }; + Ok(c.map(|c| c.with_request(&call.id))) +} +/// A mapping of the engine outcome for callers that display the reason only. pub fn gate(s: &Session, call: &ToolCall) -> Option { - // MCP's snapshot, saved to a file. - if call.name == "snapshot" { - return call - .args - .get("save_path") - .and_then(Value::as_str) - .filter(|p| !p.trim().is_empty()) - .map(|_| "it writes a file on this computer".into()); - } - // A script asks once, for every line that needs it, each named. - if call.name == "run" { - let script = call.args.get("script").and_then(Value::as_str).unwrap_or(""); - let reasons: Vec = rustgis_engine::cmd::workflows::script_commands(script) - .unwrap_or_default() - .into_iter() - .filter_map(|(n, id, params)| { - let line = ToolCall { id: String::new(), name: "run_command".into(), args: json!({"command": id, "params": params}) }; - gate(s, &line).map(|why| format!("line {n} ({id}): {why}")) - }) - .collect(); - return (!reasons.is_empty()).then(|| reasons.join("; ")); - } - let (id, params) = target(call)?; - if id.starts_with(rustgis_engine::plugins::PREFIX) { - return Some("it runs a plugin's command".into()); - } - if id == "ui.addUrl" && ["geolibreProject", "geolibreLayer", "geolibreZarr"].iter().any(|key| params.get(key).is_some()) { - return Some("it adds the files and web addresses a GeoLibre project names".into()); - } - if user_only(&id) && !matches!(id.as_str(), "edit.undo" | "edit.redo") { - return Some(match id.as_str() { - "project.new" | "project.open" | "project.openText" | "project.sample" | "project.fromTemplate" => { - "it replaces the open project".into() - } - // Whether given a path, text or data: the project names the files and web addresses - // its layers are added from. - "layer.addDownload" => "it completes a download that may import a project and its referenced sources".into(), - "project.importGeolibre" => "it adds the files and web addresses a GeoLibre project names".into(), - "project.importQgis" => "it adds the files and web addresses a QGIS project names".into(), - _ => "it is reserved to you (plugins, scripts, tasks, models and the GeoLibre runner)".into(), - }); - } - let spec = s.registry.get(&id)?; - if id == rustgis_engine::cmd::plugins::SQL || id == "sql.query" { - let why = "it runs an SQL query, which can read files on this computer and addresses on the internet"; - // Views in a database the query opens run too, though the query doesn't show them. - return Some(match params.get("database").and_then(Value::as_str).filter(|d| !d.trim().is_empty()) { - Some(d) => format!("{why} (as can views in the database {d})"), - None => why.into(), - }); + let call = match policy_call(call) { + Ok(Some(call)) => call, + Ok(None) => return None, + Err(error) => return Some(error), + }; + let scope = Scope::assistant(0, std::sync::Arc::new(Live::default())); + match s.review(&scope, &call) { + Outcome::Allowed => None, + Outcome::NeedsApproval(p) => Some(p.review().why.clone()), + Outcome::Refused(e) => Some(e), } - match reaches(spec, ¶ms) { - Some(Reach::Files) => Some("it reads or writes files on this computer".into()), - Some(Reach::Network) => Some("it connects to an address on the internet".into()), - None => None, +} +/// A revocable assistant lifetime. A stopped lifetime is never re-enabled. +pub(crate) struct Live(pub(crate) std::sync::atomic::AtomicBool); +impl Default for Live { + fn default() -> Self { + Self(std::sync::atomic::AtomicBool::new(true)) } } - -/// What "allow for this conversation" remembers for a call: its command (a tool's id for -/// `gp.run`) with its exact parameters, so allowing one file or address allows no other. Only file -/// and network access can be allowed for good: plugin commands, the user's own automation and -/// replacing the project are asked about every time. -pub fn approval_key(s: &Session, call: &ToolCall) -> Option { - let (command, key) = match call.name.as_str() { - "run_command" => { - let id = str_arg(&call.args, "command")?; - (id.clone(), id) - } - "run_tool" => ("gp.run".to_string(), format!("gp.run:{}", str_arg(&call.args, "tool")?)), - _ => return None, - }; - if command.starts_with(rustgis_engine::plugins::PREFIX) || user_only(&command) || s.registry.get(&command).is_none() { - return None; +impl rustgis_engine::policy::Revocation for Live { + fn allows(&self, _: rustgis_engine::policy::Effects) -> bool { + self.0.load(std::sync::atomic::Ordering::SeqCst) } - // Object keys serialise sorted, so the same parameters always make the same key. - // Parameters that won't run make no key. - let params = params_arg(&call.args).ok()?; - Some(format!("{key} {params}")) } - /// A one-line description of a call for the transcript and the approval prompt. pub fn summary(call: &ToolCall) -> String { let short = |v: Value| { @@ -253,21 +217,41 @@ pub fn summary(call: &ToolCall) -> String { } /// Runs a call; `Err` is a result the model is told failed. -pub fn run(s: &mut Session, call: &ToolCall) -> Result { - run_with_budget(s, call, DEFAULT_OUTPUT_TOKENS) +pub fn run(s: &mut Session, call: &ToolCall, scope: Scope, permit: Option) -> Result { + run_with_budget(s, call, DEFAULT_OUTPUT_TOKENS, scope, permit) } /// Runs a call with a per-result token estimate, capped by the absolute ceiling. -pub fn run_with_budget(s: &mut Session, call: &ToolCall, tokens: usize) -> Result { +pub fn run_with_budget(s: &mut Session, call: &ToolCall, tokens: usize, scope: Scope, permit: Option) -> Result { // Script failures are structured JSON: clean their error field before serialization. if call.name == "run" { - return run_script(s, &call.args, tokens); + return run_script(s, call, tokens, scope, permit); + } + run_with_budget_inner(s, call, tokens, scope, permit).map_err(|e| public_error(&e)) +} + +/// Retains the initiating scope and one-use permit while assembling ordinary tool results. +fn run_with_budget_inner(s: &mut Session, call: &ToolCall, tokens: usize, scope: Scope, permit: Option) -> Result { + if matches!(call.name.as_str(), "run_command" | "run_tool") { + if call.name == "run_command" && str_arg(&call.args, "command").is_some_and(|id| id.starts_with("ui.")) { + return Err("ui.* commands only open panes and dialogs; the assistant can't use them".into()); + } + let engine = policy_call(call)?.ok_or("missing executable call")?; + let (before, mark) = (layer_snap(s), s.notice_mark()); + let result = match permit { + Some(p) => s.run_permitted(scope, p, &engine), + None => s.run_scoped(scope, &engine), + } + .map_err(|e| error_with_notices(e, s, mark))?; + return Ok(command_result(s, result, &before, mark, tokens)); } - run_with_budget_inner(s, call, tokens).map_err(|e| public_error(&e)) + if permit.is_some() { + return Err("this approval is for an executable call".into()); + } + s.with_scope(scope, |s| run_read(s, call, tokens))? } -/// Assembles ordinary tool results; the public boundary cleans every early error too. -fn run_with_budget_inner(s: &mut Session, call: &ToolCall, tokens: usize) -> Result { +fn run_read(s: &mut Session, call: &ToolCall, tokens: usize) -> Result { let max_bytes = output_bytes(tokens); let a = &call.args; let v = match call.name.as_str() { @@ -278,26 +262,6 @@ fn run_with_budget_inner(s: &mut Session, call: &ToolCall, tokens: usize) -> Res "search_commands" => search_commands(s, a)?, "search_tools" => search_tools(a)?, "describe_tool" => describe_tool(a)?, - "run_command" => { - let id = str_arg(a, "command").ok_or("command is required")?; - if id.starts_with("ui.") { - return Err("ui.* commands only open panes and dialogs; the assistant can't use them".into()); - } - if s.registry.get(&id).is_none() && !id.starts_with(rustgis_engine::plugins::PREFIX) { - return Err(format!("unknown command '{id}'; find ids with search_commands")); - } - let (before, mark) = (layer_snap(s), s.notice_mark()); - let r = s.run(&id, ¶ms_arg(a)?).map_err(|e| error_with_notices(e, s, mark))?; - return Ok(command_result(s, r, &before, mark, tokens)); - } - "run_tool" => { - let tool = str_arg(a, "tool").ok_or("tool is required")?; - let (before, mark) = (layer_snap(s), s.notice_mark()); - let r = s - .run("gp.run", &json!({"tool": tool, "params": params_arg(a)?})) - .map_err(|e| error_with_notices(e, s, mark))?; - return Ok(command_result(s, r, &before, mark, tokens)); - } "read_features" => read_features(s, a)?, "search_docs" => crate::docs::search(&str_arg(a, "query").unwrap_or_default(), limit_arg(a, 8, 30))?, "read_doc" => { @@ -333,7 +297,8 @@ const MAX_DEPTH: usize = 16; /// `text` with every runtime login wrapper (`rustgis-login:…`, alone or after a prefix such as /// `pmtiles:…@`) replaced by its plain address. Commands return the bound addresses the app needs -/// to use a connection's login; the model and MCP clients get only the public ones. +/// to use a connection's login; the model and MCP clients get only the public ones. Active +/// raw secrets are scrubbed too, including an echo with no wrapper in the result. fn public_text(text: String) -> String { rustgis_engine::render::rustgis_tiles::logins::scrub(&text) } @@ -413,8 +378,8 @@ struct Withheld { keys: usize, } -/// Projects keys and values through active-login redaction; ambiguous objects and deep subtrees -/// are withheld and counted without changing the completed command's success. +/// `v` with string values and field names made public ([`public_text`]), to a depth of +/// [`MAX_DEPTH`]. An object whose public field names collide is withheld as a whole. fn public_addresses(v: Value, depth: usize, withheld: &mut Withheld) -> Value { match v { Value::String(s) => Value::String(public_text(s)), @@ -560,7 +525,8 @@ fn with_new_layers(r: Value, s: &Session, before: &[(String, Vec)]) -> Valu /// kept (files it wrote stay). The answer is `{success, result, lines, layersAdded, notices, /// error, duration_s}`, where `result` /// is the last command's; a failure is an `Err` holding the same JSON. -fn run_script(s: &mut Session, a: &Value, tokens: usize) -> Result { +fn run_script(s: &mut Session, call: &ToolCall, tokens: usize, scope: Scope, permit: Option) -> Result { + let a = &call.args; // Untrimmed, so errors name the same lines the caller wrote. let script = a .get("script") @@ -569,7 +535,11 @@ fn run_script(s: &mut Session, a: &Value, tokens: usize) -> Result s.run_permitted(scope, p, &engine), + None => s.run_scoped(scope, &engine), + }; let secs = (start.elapsed().as_secs_f64() * 1000.0).round() / 1000.0; let notices: Vec<&str> = s.notices_since(mark).iter().map(|n| n.text.as_str()).collect(); match r { @@ -673,7 +643,7 @@ fn search_commands(s: &Session, a: &Value) -> Result { fn search_tools(a: &Value) -> Result { let words = words(a)?; let limit = limit_arg(a, 12, 40); - let mut hits: Vec<(usize, Value)> = analysis::tools() + let mut hits: Vec<(usize, Value)> = analysis::published_tools() .filter_map(|t| { let (id, name, tb, summary) = (t.id.to_lowercase(), t.name.to_lowercase(), t.toolbox.to_lowercase(), t.summary.to_lowercase()); let sc = score(&words, &[&id, &name, &tb, &summary]); @@ -687,7 +657,9 @@ fn search_tools(a: &Value) -> Result { fn describe_tool(a: &Value) -> Result { let id = str_arg(a, "tool").ok_or("tool is required")?; - let t = analysis::tool(&id).ok_or_else(|| format!("no tool '{id}'; find ids with search_tools"))?; + let t = analysis::resolve_published(&id) + .map(|r| r.spec()) + .ok_or_else(|| format!("no published tool '{id}'; open the tools or load the suite first"))?; let params: Vec = t .params .iter() @@ -976,7 +948,7 @@ mod tests { ] { let call = ToolCall { id: "error".into(), name: name.into(), args }; let original = call.clone(); - let error = run_with_budget(&mut s, &call, DEFAULT_OUTPUT_TOKENS).unwrap_err(); + let error = run_with_budget(&mut s, &call, DEFAULT_OUTPUT_TOKENS, Scope::peer(), None).unwrap_err(); assert!(!error.contains("RGSYNTH_returned_error"), "{name}, active={active}: {error}"); assert_eq!(call, original); if name == "run" { @@ -993,7 +965,9 @@ mod tests { } } let clean = ToolCall { id: "clean".into(), name: "read_features".into(), args: json!({"layer":"ordinary missing layer"}) }; - assert!(run_with_budget(&mut s, &clean, DEFAULT_OUTPUT_TOKENS).unwrap_err().contains("ordinary missing layer")); + assert!( + run_with_budget(&mut s, &clean, DEFAULT_OUTPUT_TOKENS, Scope::peer(), None).unwrap_err().contains("ordinary missing layer") + ); assert_eq!(Failure::new("https://clean.invalid/a?layer=roads", "HTTP 403").message, "HTTP 403"); assert_eq!(logins::scrub_error("failed https://clean.invalid/a?layer=roads"), "failed https://clean.invalid/a?layer=roads"); } @@ -1016,8 +990,15 @@ mod tests { name: "run_command".into(), args: json!({"command": command, "params": params}), }; - let added = run_with_budget(&mut s, &call("map.addTiles", json!({"url": tile, "name": "T"})), DEFAULT_OUTPUT_TOKENS).unwrap(); - let listed = run_with_budget(&mut s, &call("map.tiles", json!({})), DEFAULT_OUTPUT_TOKENS).unwrap(); + let scope = Scope::assistant(757, std::sync::Arc::new(Live::default())); + let add = call("map.addTiles", json!({"url": tile, "name": "T"})); + let pending = match s.review(&scope, &policy_call(&add).unwrap().unwrap()) { + Outcome::NeedsApproval(pending) => Some(pending), + _ => None, + } + .unwrap(); + let added = run_with_budget(&mut s, &add, DEFAULT_OUTPUT_TOKENS, scope.clone(), Some(pending.allow())).unwrap(); + let listed = run_with_budget(&mut s, &call("map.tiles", json!({})), DEFAULT_OUTPUT_TOKENS, scope, None).unwrap(); assert!(s.map().tiles[0].url.starts_with("rustgis-login:")); for text in [&added, &listed] { assert!(!text.contains("rustgis-login:") && !text.contains("assistant-secret-757"), "{text}"); @@ -1049,9 +1030,10 @@ mod tests { .unwrap()["id"] .clone(); s.run("connection.login", &json!({"id":id, "password":"success"})).unwrap(); + let scope = Scope::assistant(152140, std::sync::Arc::new(Live::default())); for (script, success) in [("map.zoomIn {}", true), ("not.a.command {}", false)] { let call = ToolCall { id: "envelope".into(), name: "run".into(), args: json!({"script":script}) }; - let text = run(&mut s, &call).unwrap_or_else(|e| e); + let text = run(&mut s, &call, scope.clone(), None).unwrap_or_else(|e| e); let out: Value = serde_json::from_str(&text).unwrap(); assert_eq!(out.get("success"), Some(&json!(success)), "{text}"); assert!(out.get("result").is_some() && out.get("notices").is_some() && out.get("duration_s").is_some()); @@ -1063,7 +1045,7 @@ mod tests { [("search_commands", json!({"query":"zoom"}), "commands"), ("search_tools", json!({"query":"buffer"}), "tools")] { let call = ToolCall { id: "metadata".into(), name: name.into(), args }; - let text = run(&mut s, &call).unwrap(); + let text = run(&mut s, &call, scope.clone(), None).unwrap(); let out: Value = serde_json::from_str(&text).unwrap(); let entries = out.get(array).and_then(Value::as_array).unwrap(); assert!(!entries.is_empty(), "{text}"); @@ -1103,15 +1085,6 @@ mod tests { assert_eq!(s.notices_since(0).len(), 1, "the app also receives one aggregate loss notice"); } - #[test] - fn clean_nested_results_need_no_withholding_notice() { - let mut s = Session::new(rustgis_engine::Project::default()); - let result = json!({"result": {"nested": [{"value": "complete"}]}, "success": true}); - let out: Value = serde_json::from_str(&public_result(&mut s, result.clone(), DEFAULT_OUTPUT_TOKENS)).unwrap(); - assert_eq!(out, result); - assert!(s.notices_since(0).is_empty()); - } - #[test] fn successful_results_scrub_active_raw_service_keys_without_a_wrapper() { let mut s = Session::new(rustgis_engine::Project::default()); @@ -1140,8 +1113,9 @@ mod tests { s.run("layer.addDataText", &json!({"fileName":"echo-fields.geojson","text":source.to_string()})).unwrap(); let name = s.map().layers.last().unwrap().name.clone(); let call = ToolCall { id: "echo-fields".into(), name: "read_features".into(), args: json!({"layer":name,"limit":1}) }; + let scope = Scope::assistant(9035, std::sync::Arc::new(Live::default())); let mark = s.notice_mark(); - let text = run_with_budget(&mut s, &call, DEFAULT_OUTPUT_TOKENS).unwrap(); + let text = run_with_budget(&mut s, &call, DEFAULT_OUTPUT_TOKENS, scope, None).unwrap(); assert!(!text.contains("fake001"), "generated object keys must be scrubbed too: {text}"); let out: Value = serde_json::from_str(&text).unwrap(); assert_eq!(out["rows"][0]["[redacted]"], "retained value"); @@ -1159,4 +1133,13 @@ mod tests { assert_eq!(s.notices_since(mark).len(), 1); assert!(s.notices_since(mark)[0].text.contains("field names")); } + + #[test] + fn clean_nested_results_need_no_withholding_notice() { + let mut s = Session::new(rustgis_engine::Project::default()); + let result = json!({"result": {"nested": [{"value": "complete"}]}, "success": true}); + let out: Value = serde_json::from_str(&public_result(&mut s, result.clone(), DEFAULT_OUTPUT_TOKENS)).unwrap(); + assert_eq!(out, result); + assert!(s.notices_since(0).is_empty()); + } } diff --git a/crates/data/src/source/remote.rs b/crates/data/src/source/remote.rs index 09afd5c420..b003c89ef5 100644 --- a/crates/data/src/source/remote.rs +++ b/crates/data/src/source/remote.rs @@ -41,6 +41,40 @@ pub const NEED: &str = "waiting for the file's bytes"; /// covering, a server without ranges): it is downloaded whole instead. pub const READ_WHOLE: &str = "can't be read in place"; +/// An owned, principal-free predicate for a claimed deferred request. +pub type Still = std::sync::Arc bool + Send + Sync>; +thread_local! { static STILL: std::cell::RefCell> = const { std::cell::RefCell::new(None) }; } +/// Clones synchronous request context before a worker or browser future is scheduled. +pub fn current_still() -> Option { + STILL.with(|s| s.borrow().clone()) +} +/// The guard is synchronous. Never hold it across an await. +pub fn with_still(still: Still, f: impl FnOnce() -> T) -> T { + with_optional_still(Some(still), f) +} +/// Re-enters captured context synchronously, including around follow-on callbacks. +pub fn with_optional_still(still: Option, f: impl FnOnce() -> T) -> T { + struct Restore(Option); + impl Drop for Restore { + fn drop(&mut self) { + STILL.with(|s| *s.borrow_mut() = self.0.take()); + } + } + let previous = current_still(); + let combined = match (previous.clone(), still) { + (Some(a), Some(b)) => Some(std::sync::Arc::new(move || a() && b()) as Still), + (Some(a), None) => Some(a), + (None, b) => b, + }; + STILL.with(|s| *s.borrow_mut() = combined); + let _restore = Restore(previous); + f() +} +/// Checked immediately before a request-producing stage, without suspension before sending. +pub fn check_still(still: &Option) -> std::result::Result<(), String> { + if still.as_ref().is_some_and(|s| !s()) { Err("request cancelled before it was sent".into()) } else { Ok(()) } +} + /// A request for part of a file. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Want { @@ -86,6 +120,17 @@ pub fn can_fetch() -> bool { /// Fetches a whole file through the installed fetcher (a file that can't be read in place). pub fn fetch_whole(url: &str, done: Done) { + let still = current_still(); + if let Err(e) = check_still(&still) { + done(Err(Failed::Other(e))); + return; + } + let context = still.clone(); + let done: Done = Box::new(move |r| { + with_optional_still(context.clone(), || { + done(check_still(&context).map_err(Failed::Other).and(r)); + }) + }); if let Err(error) = crate::login_address::check(url) { done(Err(Failed::Other(error.into()))); return; @@ -438,16 +483,34 @@ pub fn drive( done: Box) + Send>, ) { static NEXT: AtomicU64 = AtomicU64::new(1); - step(remote, work, done, NEXT.fetch_add(1, Ordering::Relaxed), 0); + step(remote, work, done, NEXT.fetch_add(1, Ordering::Relaxed), 0, current_still()); } +/// Re-enters the owned synchronous carrier for each read round, including callbacks on another thread. fn step( remote: Arc, work: Arc Result + Send + Sync>, done: Box) + Send>, drive: u64, rounds: usize, + still: Option, +) { + with_optional_still(still.clone(), || step_in(remote, work, done, drive, rounds, still)); +} + +/// Checks before reading, sending each new range, accepting bytes and starting another round. +fn step_in( + remote: Arc, + work: Arc Result + Send + Sync>, + done: Box) + Send>, + drive: u64, + rounds: usize, + still: Option, ) { + if let Err(e) = check_still(&still) { + done(Err(DataError::Format(e))); + return; + } if let Err(error) = crate::login_address::check(remote.url()) { done(Err(DataError::Format(error.into()))); return; @@ -462,7 +525,7 @@ fn step( } let wants = remote.take_missing(drive); if wants.is_empty() { - done(r); + done(check_still(&still).map_err(DataError::Format).and(r)); return; } // Readers plan within READ_BUDGET; a round asking for far more is a hostile file's offsets. @@ -488,42 +551,49 @@ fn step( let round = Arc::new(Mutex::new(Round { left: wants.len(), failed: None, done: Some(done) })); for w in wants { let (remote2, work2, round2) = (remote.clone(), work.clone(), round.clone()); + let context = still.clone(); let landed: Done = Box::new(move |res| { - match (w, res) { - (Want::Range(a, b), Ok(bytes)) => remote2.insert(a, b - a, bytes), - (Want::Tail(n), Ok(bytes)) => remote2.insert_tail(n, bytes), - // (Reads ask for ranges and tails; a whole file is all of its bytes from the start.) - (Want::Whole, Ok(bytes)) => remote2.insert(0, bytes.len() as u64, bytes), - // A request dropped unfetched (the view moved on) is asked for again next round. - (_, Err(Failed::Dropped)) => {} - // A range past the end of the file. - (Want::Range(a, b), Err(Failed::PastEnd)) => remote2.insert(a, b - a, Vec::new()), - (_, Err(e)) => { - let e = match e { - Failed::NoRanges => format!("the server doesn't answer byte-range requests ({READ_WHOLE})"), - Failed::PastEnd => "the last bytes asked for past the end of the file".into(), - Failed::Dropped => "the request was dropped".into(), - Failed::Other(e) => e, - }; - let mut r = round2.lock().unwrap_or_else(|e| e.into_inner()); - r.failed.get_or_insert(e); + with_optional_still(context.clone(), || { + let res = check_still(&context).map_err(Failed::Other).and(res); + match (w, res) { + (Want::Range(a, b), Ok(bytes)) => remote2.insert(a, b - a, bytes), + (Want::Tail(n), Ok(bytes)) => remote2.insert_tail(n, bytes), + // (Reads ask for ranges and tails; a whole file is all of its bytes from the start.) + (Want::Whole, Ok(bytes)) => remote2.insert(0, bytes.len() as u64, bytes), + // A request dropped unfetched (the view moved on) is asked for again next round. + (_, Err(Failed::Dropped)) => {} + // A range past the end of the file. + (Want::Range(a, b), Err(Failed::PastEnd)) => remote2.insert(a, b - a, Vec::new()), + (_, Err(e)) => { + let e = match e { + Failed::NoRanges => format!("the server doesn't answer byte-range requests ({READ_WHOLE})"), + Failed::PastEnd => "the last bytes asked for past the end of the file".into(), + Failed::Dropped => "the request was dropped".into(), + Failed::Other(e) => e, + }; + let mut r = round2.lock().unwrap_or_else(|e| e.into_inner()); + r.failed.get_or_insert(e); + } } - } - let next = { - let mut r = round2.lock().unwrap_or_else(|e| e.into_inner()); - r.left = r.left.saturating_sub(1); - if r.left == 0 { Some((r.done.take(), r.failed.take())) } else { None } - }; - if let Some((Some(done), failed)) = next { - match failed { - Some(e) => { - done(Err(DataError::Format(if e.starts_with(remote2.url()) { e } else { format!("{}: {e}", remote2.url()) }))) + let next = { + let mut r = round2.lock().unwrap_or_else(|e| e.into_inner()); + r.left = r.left.saturating_sub(1); + if r.left == 0 { Some((r.done.take(), r.failed.take())) } else { None } + }; + if let Some((Some(done), failed)) = next { + match failed { + Some(e) => { + done(Err(DataError::Format(if e.starts_with(remote2.url()) { e } else { format!("{}: {e}", remote2.url()) }))) + } + None => step(remote2, work2, done, drive, rounds + 1, context), } - None => step(remote2, work2, done, drive, rounds + 1), } - } + }) }); - fetch(&remote.url, w, landed); + match check_still(&still) { + Ok(()) => fetch(&remote.url, w, landed), + Err(e) => landed(Err(Failed::Other(e))), + } } } diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index f635c9f2c9..7ccb414d78 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -29,6 +29,8 @@ rustgis-geolibre = { workspace = true, optional = true } [features] geolibre = ["dep:rustgis-geolibre"] +# Existing in-tree Wasm fixtures for adapter tests; no guest or production interface change. +testing = ["rustgis-plugins/testing"] [dev-dependencies] # Writing sample GeoPackages. diff --git a/crates/engine/src/cmd/arcgis.rs b/crates/engine/src/cmd/arcgis.rs index c9582dc2e8..4ebb321b25 100644 --- a/crates/engine/src/cmd/arcgis.rs +++ b/crates/engine/src/cmd/arcgis.rs @@ -8,24 +8,25 @@ use super::{f, ps}; use crate::render::rustgis_tiles::arcgis_auth::{self, Info}; use crate::{CmdResult, CommandSpec as C, Session}; +/// Declares sign-in changes as reserved account operations, independently of their network reach. pub fn commands() -> Vec { vec![ - C::new("arcgis.signIn", "Sign In to ArcGIS", "Home › Add › Service", sign_in).roles(&[]).view().params( + C::new("arcgis.signIn", "Sign In to ArcGIS", "Home › Add › Service", sign_in).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Reserved)).view().params( r#"{"portal"?: "the portal's address: an ArcGIS Online organization's (https://myorg.maps.arcgis.com) or an ArcGIS Enterprise portal's (https://gis.example.org/portal); ArcGIS Online when blank", "clientId"?: "the client id of an OAuth app registered on the portal (Developer credentials) whose redirect URLs list http://127.0.0.1:7290/arcgis/callback; RustGIS's own app when absent on ArcGIS Online (required for ArcGIS Enterprise)", "port"?: "the callback server's port (7290; the redirect URL registered must name it)"} → the sign-in page to open in the browser; the session is kept when the browser comes back (arcgis.status tells). Desktop and command line only"#, ), - C::new("arcgis.token", "Use ArcGIS Token", "Home › Add › Service", token).roles(&[]).view().params( + C::new("arcgis.token", "Use ArcGIS Token", "Home › Add › Service", token).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Reserved)).view().params( r#"{"url": "an ArcGIS REST address on https:// (a service's, …/rest/services, or a portal's …/sharing/rest)", "token": "an ArcGIS token for it"} — kept for the session only, never in the project; sent to that ArcGIS Server's (or portal's) requests only. A service address given to service.layers or service.add with ?token=… does the same"#, ), - C::new("arcgis.signOut", "Sign Out of ArcGIS", "Home › Add › Service", sign_out).roles(&[]) + C::new("arcgis.signOut", "Sign Out of ArcGIS", "Home › Add › Service", sign_out).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Reserved)) .view() .params(r#"{"portal"?: "a signed-in portal's address, or a server a token was given for (arcgis.status lists them); every one when absent"}"#), C::new("arcgis.cancelSignIn", "Cancel ArcGIS Sign-In", "Home › Add › Service", |_s, _p| { #[cfg(not(target_arch = "wasm32"))] arcgis_auth::cancel_sign_in(); Ok(status()) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)) .view(), - C::new("arcgis.status", "ArcGIS Sign-In Status", "Home › Add › Service", |_s, _p| Ok(status())).roles(&[]).view(), + C::new("arcgis.status", "ArcGIS Sign-In Status", "Home › Add › Service", |_s, _p| Ok(status())).roles(&[]).effects(crate::policy::Effects::NONE).view(), ] } diff --git a/crates/engine/src/cmd/catalogs.rs b/crates/engine/src/cmd/catalogs.rs index df92b51896..18b4cc1104 100644 --- a/crates/engine/src/cmd/catalogs.rs +++ b/crates/engine/src/cmd/catalogs.rs @@ -10,13 +10,13 @@ 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(&[]).params( + 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)]}"#, ), - C::new("sourcecoop.products", "List Source Cooperative Products", "Home › Add › Web Data", sourcecoop_products).roles(&[]) + 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() .params(r#"{"account"?: "an account's products (default: the featured ones)"}"#), - C::new("sourcecoop.list", "List Source Cooperative Files", "Home › Add › Web Data", sourcecoop_list).roles(&[]).view().params( + C::new("sourcecoop.list", "List Source Cooperative Files", "Home › Add › Web Data", sourcecoop_list).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).view().params( r#"{"account": text, "product": text, "path"?: "a folder inside the product, ending in /", "token"?: "the next page's, from a listing"} (a file's "url" is added with layer.addUrl)"#, ), ] diff --git a/crates/engine/src/cmd/clipboard.rs b/crates/engine/src/cmd/clipboard.rs index d9ce18c337..d9e4c1b08b 100644 --- a/crates/engine/src/cmd/clipboard.rs +++ b/crates/engine/src/cmd/clipboard.rs @@ -20,17 +20,33 @@ pub struct Clipboard { pub fn commands() -> Vec { vec![ - C::new("edit.copy", "Copy", "Edit › Clipboard", |s, _| copy(s)).roles(&[]).view().key("Mod+C").when(need_selection), - C::new("edit.cut", "Cut", "Edit › Clipboard", cut).roles(&[]).key("Mod+X").when(super::design::need_editable_selection), + C::new("edit.copy", "Copy", "Edit › Clipboard", |s, _| copy(s)) + .roles(&[]) + .effects(crate::policy::Effects::NONE) + .view() + .key("Mod+C") + .when(need_selection), + C::new("edit.cut", "Cut", "Edit › Clipboard", cut) + .roles(&[]) + .effects(crate::policy::Effects::NONE) + .key("Mod+X") + .when(super::design::need_editable_selection), C::new("edit.paste", "Paste", "Edit › Clipboard", paste) .roles(&[]) + .effects(crate::policy::Effects::NONE) .key("Mod+V") .when(super::design::need_editable) .params(r#"{"layer"?, "text"?: GeoJSON text (the copied features when omitted)}"#), C::new("edit.pasteSpecial", "Paste Special", "Edit › Clipboard", paste_special) .roles(&[]) + .effects(crate::policy::Effects::NONE) .params(r#"{"name"?: string, "text"?: GeoJSON text (the copied features when omitted)}"#), - C::new("layer.copyPath", "Copy Path", "Edit › Clipboard", copy_path).roles(&[]).view().when(need_layer).params(r#"{"layer"?}"#), + C::new("layer.copyPath", "Copy Path", "Edit › Clipboard", copy_path) + .roles(&[]) + .effects(crate::policy::Effects::NONE) + .view() + .when(need_layer) + .params(r#"{"layer"?}"#), ] } diff --git a/crates/engine/src/cmd/connections.rs b/crates/engine/src/cmd/connections.rs index ca00b4f04f..0ae1067629 100644 --- a/crates/engine/src/cmd/connections.rs +++ b/crates/engine/src/cmd/connections.rs @@ -25,34 +25,34 @@ mod connection_use; pub fn commands() -> Vec { vec![ - C::new("connection.list", "List Connections", "Home › Add › Data Sources", list).roles(&[]).view().params( + C::new("connection.list", "List Connections", "Home › Add › Data Sources", list).roles(&[]).effects(crate::policy::Effects::NONE).view().params( r#"{"kind"?: "postgres"|"mysql"|"sqlserver"|"service"|"wms"|"wmts"|"wfs"|"wcs"|"ogcapi"|"arcgis"|"csw"|"stac"|"s3", "group"?: text}"#, ), - C::new("connection.new", "New Connection", "Home › Add › Data Sources", new).roles(&[]).view().params( + C::new("connection.new", "New Connection", "Home › Add › Data Sources", new).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( r#"{"kind": "postgres"|"mysql"|"sqlserver"|"service"|"wms"|"wmts"|"wfs"|"wcs"|"ogcapi"|"arcgis"|"csw"|"stac"|"s3", "address"?: "as the kind's add command takes it, without its password or token (postgres:host=… dbname=… user=…, mysql://user@host/db, mssql://user@host/db, a service's or STAC API's https:// address, an S3 endpoint: AWS when absent)", "name"?: "unique (default from the address)", "group"?: text, "options"?: {"region"?: text, "buckets"?: "a,b", "credentials"?: "keys"|"profile"|"environment"|"none", "profile"?: text} (S3 only), "auth"?: "none"|"basic"|"header": how a web-service connection logs in (basic authentication or an API key in a header; the password or key is given to connection.login, never kept here), "user"?: "the basic login's user name", "keyName"?: "the header the API key goes in, e.g. X-API-Key"}"#, ), - C::new("connection.edit", "Edit Connection", "Home › Add › Data Sources", edit).roles(&[]).view().params( + C::new("connection.edit", "Edit Connection", "Home › Add › Data Sources", edit).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( r#"{"id": "a connection's id or name", "name"?: text, "group"?: text, "address"?: text, "options"?: "as connection.new (replaces them)", "auth"?: "as connection.new", "user"?: text, "keyName"?: text, "kind"?: "its kind (unchanged)"} (another origin, login method, user or key name ends the web connection's session login)"#, ), - C::new("connection.duplicate", "Duplicate Connection", "Home › Add › Data Sources", duplicate).roles(&[]).view().params(r#"{"id": "a connection's id or name", "name"?: text}"#), - C::new("connection.remove", "Remove Connection", "Home › Add › Data Sources", remove).roles(&[]).view().params(r#"{"id": "a connection's id or name"} (deletes its saved database password or S3 keys; ends its web-service session login)"#), - C::new("connection.login", "Log In to Connection", "Home › Add › Data Sources", login_command).roles(&[]).view().params( + C::new("connection.duplicate", "Duplicate Connection", "Home › Add › Data Sources", duplicate).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params(r#"{"id": "a connection's id or name", "name"?: text}"#), + C::new("connection.remove", "Remove Connection", "Home › Add › Data Sources", remove).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params(r#"{"id": "a connection's id or name"} (deletes its saved database password or S3 keys; ends its web-service session login)"#), + C::new("connection.login", "Log In to Connection", "Home › Add › Data Sources", login_command).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( r#"{"id": "a connection's id or name; blank credentials explicitly use this ID's session credentials (or saved credentials for database/S3 connections)", "password"?: "a database password, or a web service's basic-authentication password", "apiKey"?: "a web service's API key (auth header)", "allowInsecure"?: "boolean, default false: confirms sending a web service's login to its plain http:// address, unencrypted", "accessKeyId"?: text, "secretAccessKey"?: text, "sessionToken"?: "temporary S3 credentials, never persisted", "remember"?: "boolean, default false: opt into this computer's credential store for a newly typed database password or complete S3 key pair (saved keys mode only). Web-service credentials are session-only and reject remember:true. No credentials are stored in connections or projects; a failed database/S3 save preserves the session login"} (a web service's login is sent only with this session's requests bound to that connection and only to its address's origin: the same scheme, host and port; logging in to one of several connections chooses it for unassigned requests to that address)"#, ), - C::new("connection.logout", "Log Out of Connection", "Home › Add › Data Sources", logout).roles(&[]).view().params( + C::new("connection.logout", "Log Out of Connection", "Home › Add › Data Sources", logout).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( r#"{"id": "a web-service connection's id or name"} (clears this session's login and its password or key, which is no longer sent)"#, ), - C::new("connection.forgetSavedKeys", "Forget Saved Keys", "Home › Add › Data Sources", forget_saved_keys).roles(&[]).view().params( + C::new("connection.forgetSavedKeys", "Forget Saved Keys", "Home › Add › Data Sources", forget_saved_keys).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( r#"{"id": "a saved S3 connection's id or name"} (deletes stored keys; a login in this session keeps working)"#, ), - C::new("connection.resolveS3", "Choose S3 Connection", "Home › Add › Data Sources", resolve_s3).roles(&[]).view().params( + C::new("connection.resolveS3", "Choose S3 Connection", "Home › Add › Data Sources", resolve_s3).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Files)).view().params( r#"{"source": "a pending project S3 source address", "id": "a candidate saved connection id"} → {id, reopened, failed, reading}: explicitly grants this bucket and opens only its waiting sources; reading counts the rasters read in place whose overview is still being read (the desktop doesn't wait for it: it lands as the map draws, and one that fails is said then)"#, ), - C::new("connection.forgetPassword", "Forget Saved Password", "Home › Add › Data Sources", forget_password).roles(&[]).view().params(r#"{"id": "a database connection's id or name"}"#), - C::new("connection.test", "Test Connection", "Home › Add › Data Sources", test).roles(&[]).view().params( + C::new("connection.forgetPassword", "Forget Saved Password", "Home › Add › Data Sources", forget_password).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params(r#"{"id": "a database connection's id or name"}"#), + C::new("connection.test", "Test Connection", "Home › Add › Data Sources", test).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( r#"{"id"?: "a connection's id or name", "kind"?: "postgres"|"mysql"|"sqlserver"|"service"|"wms"|"wmts"|"wfs"|"wcs"|"ogcapi"|"arcgis"|"csw"|"stac"|"s3", "name"?: text, "group"?: text, "address"?: text, "options"?: "an unsaved connection, as connection.new (when no id)", "auth"?: text, "user"?: text, "keyName"?: "as connection.new (when no id)", "password"?: "for a named database, authentication refusal restores prior session credentials; a later listing failure keeps the new password; never persisted. A saved web service's: as connection.login (logs in first)", "apiKey"?: "a saved web service's, as connection.login (logs in first)", "allowInsecure"?: "as connection.login", "accessKeyId"?: text, "secretAccessKey"?: text, "sessionToken"?: "as connection.login (logs in first)", "probe"?: "true: only the address the test reads, {fetch} (null for databases, which their driver tests)", "answer"?: "non-database kinds only: what that address returned, when the app fetched it (no network request then); databases ignore answer and test through their driver"}"#, ), - C::new("connection.browse", "Browse Connection", "Home › Add › Data Sources", browse::browse).roles(&[]).view().params( + C::new("connection.browse", "Browse Connection", "Home › Add › Data Sources", browse::browse).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Files)).view().params( r#"{"id": "a connection's id or name", "node"?: "a child's node, as a listing gives it (the connection's top level when absent)", "probe"?: "true: only the address this level reads, {fetch} (null: read without a fetch, or by the database driver); an S3 connection logs in first, as connection.test does", "answer"?: "what that address returned, when the app fetched it", "documents"?: [{"url": "a further document a listing asked for (next)", "text": "its answer"}]} → {connection, node, children: [{key, name, what, detail?, node? (browse it), children? (listed already), add? {command, params}: the command that adds it}], truncated?, matched?, notes?, service? {url, type}, next? (fetch it, then ask again with answer and documents)}. Database roots list schemas; open a schema node to list its tables."#, ), ] diff --git a/crates/engine/src/cmd/cube.rs b/crates/engine/src/cmd/cube.rs index 4c80027c55..7e433c3683 100644 --- a/crates/engine/src/cmd/cube.rs +++ b/crates/engine/src/cmd/cube.rs @@ -25,10 +25,10 @@ use super::ps; use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { - vec![C::new("cube.trend", "Trend Analysis", "Analyze › Image Analysis", trend_cmd).roles(&[]).params( + vec![C::new("cube.trend", "Trend Analysis", "Analyze › Image Analysis", trend_cmd).inputs(cube_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)).params( r#"{"cube"?: an entry of cube.catalog (its STAC API, collection, band, cell size and cloud cap, or its Zarr store and variable) | "layers"?: [raster layers, a date each; one on another grid is resampled (nearest) onto the first's] | "layer"?: a Zarr layer (or "path" | "url", "variable", "index", "crs" as for zarr.reduce) or a raster of several bands, a date each (default: the active layer) | "collection": a STAC collection (desktop; read in place over an area, in pieces when large; with url, asset, bbox, maxCloud, cellSize and resampling below, and "start", "end"), "url"?: the collection's STAC API (as for stac.addCube), "asset"?: the collection's band, such as NDVI, "bbox"?: [w, s, e, n] | "view" (the collection's area), "maxCloud"?: a collection's cloud cover cap, "cellSize"?: a collection's cell size in the files' units (the overview level nearest it is read), "resampling"?: "nearest" (default: that overview's pixels as stored) | "average" (each cell the mean of the full images' pixels in it, which reads them), "band"?: 1-based (a stack's band), "dates"?: [dates] (a stack's or the bands' dates, in their order; default: read from the layers' names or files, or the bands' names), "start"?, "end"?: dates (a whole number from 1000 to 9999 is a year; an end given as a year or a month takes all of it), "months"?: [1–12] (one season across years), "test"?: "hamedRao" (default) | "yueWang" | "seasonal" | "original", "seasons"?: parts of a year for the seasonal test (default 12), "alpha"?: 0.05 (above 0, below 0.5), "minValid"?: dates a cell needs (default: half of them, at least 4; the seasonal test also needs 4 in seasons of two or more), "per"?: "year" (default) | "decade", "name"?: text, "ran"?: (internal: a trend the app has worked out)} → four raster layers: Sen's slope, the test's Z and p-value, and the slope where p < alpha"#, ), - C::new("cube.reduce", "Summarise a Data Cube over Time", "Analyze › Image Analysis", reduce_cmd).roles(&[]).params( + C::new("cube.reduce", "Summarise a Data Cube over Time", "Analyze › Image Analysis", reduce_cmd).inputs(cube_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)).params( r#"{"cube"?: an entry of cube.catalog (as for cube.trend) | "layers"?: [raster layers, a date each; one on another grid is resampled (nearest) onto the first's] | "layer"?: a Zarr layer or a raster of several bands, a date each (default: the active layer) | "path"?: a Zarr store's folder | "url"?: a Zarr store's address, or a STAC collection's API (as for stac.addCube) | "collection": a STAC collection (desktop; read in place over an area, in pieces when large; with url, asset, bbox, maxCloud, cellSize and resampling below, and "start", "end"), "variable"?, "index"?, "crs"?: a Zarr variable's (as for zarr.reduce), "asset"?: the collection's band, such as NDVI, "bbox"?: [w, s, e, n] | "view" (the collection's area), "maxCloud"?: a collection's cloud cover cap, "cellSize"?: a collection's cell size in the files' units (the overview level nearest it is read), "resampling"?: "nearest" (default) | "average" (as for cube.trend), "band"?: 1-based (a stack's band), "dates"?: [dates] (a stack's or the bands' dates, in their order; default: read from the layers' names or files, or the bands' names), "stat": "mean"|"min"|"max"|"sum"|"std" (the population standard deviation, n)|"count"|"countAbove"|"mode" or "majority" (a categorical cube's most frequent class; the least of those tied)|"modeCount" (how many dates had the mode)|"variety" (how many classes)|"changes" (how many times the class changed from one valued date to the next)|"longestRun" (the class of the longest run of dates in one class; the least of those tied)|"longestRunLength" (how many dates that run lasted)|"frequency" (a layer for each class met: its count of dates)|"countIn" (how many dates in the classes given), "above"?: number (countAbove's threshold), "classes"?: [whole numbers 0–255, at most 256] (countIn's classes), "by"?: "all" (default: one layer) | "year" (a layer for each calendar year) | "month" (a layer for each month of the year, across years), "start"?, "end"?: dates (as for cube.trend), "months"?: [1–12] (a season across years), "minValid"?: 0–1 (a cell with a smaller share of its group's dates is left empty; default 0), "name"?: text} → raster layers of the statistic, one for each group (frequency: one for each class of each group)"#, )] } @@ -272,7 +272,8 @@ pub const SUMMARY: Needs = Needs { what: "summary", min_dates: 2, ordered: false pub fn cube_from(s: &mut Session, p: &Value, needs: Needs) -> Result, String> { let dates = date_list(p)?; let what = needs.what; - if let Some(list) = p.get("layers").filter(|v| !v.is_null()) { + let choice = cube_choice(s, p)?; + if let CubeChoice::Layers(list) = choice { let list = list.as_array().ok_or("layers is a list of raster layers")?; if list.len() < needs.min_dates { return Err(format!("a {what}'s stack needs {} layers or more, a date each", needs.min_dates)); @@ -334,24 +335,14 @@ pub fn cube_from(s: &mut Session, p: &Value, needs: Needs) -> Result Result<(), String> { type Read = Result, usize)>, String>; let (tx, rx) = std::sync::mpsc::sync_channel::<(usize, Read)>(readers); for _ in 0..readers { let (tx, next) = (tx.clone(), &next); let capture = &capture; + let still = still.clone(); scope.spawn(move || { - let _diagnostic_scope = capture.enter(); - loop { - let k = next.fetch_add(1, Ordering::Relaxed); - let Some(&i) = steps.get(k) else { break }; - if cancel.load(Ordering::Relaxed) || tx.send((k, cube.slice_of(i, piece))).is_err() { - break; + crate::render::rustgis_tiles::cache::with_optional_still(still, || { + let _diagnostic_scope = capture.enter(); + loop { + let k = next.fetch_add(1, Ordering::Relaxed); + let Some(&i) = steps.get(k) else { break }; + if cancel.load(Ordering::Relaxed) || tx.send((k, cube.slice_of(i, piece))).is_err() { + break; + } } - } + }) }); } drop(tx); @@ -1055,12 +1050,15 @@ fn run_telling( let (cancel, done) = (AtomicBool::new(false), AtomicUsize::new(0)); let total = total.max(1); let capture = rustgis_data::diagnostics::Capture::current(); + let still = crate::render::rustgis_tiles::cache::current_still(); std::thread::scope(|scope| { let (sent, ended) = channel(); let (c, d) = (&cancel, &done); let worker = scope.spawn(move || { - let _diagnostic_scope = capture.enter(); - let _ = sent.send(work(c, d)); + crate::render::rustgis_tiles::cache::with_optional_still(still, || { + let _diagnostic_scope = capture.enter(); + let _ = sent.send(work(c, d)); + }) }); let mut told = usize::MAX; // Waited for, not polled, so a short run returns as soon as it ends. @@ -1837,6 +1835,54 @@ fn reduce_cmd(s: &mut Session, p: &Value) -> CmdResult { #[cfg(test)] mod tests { + /// Both I/O thread hops retain the initiating request context; numeric workers don't need it. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn permission_transport_cube_readers_inherit_run_telling_context() { + use crate::render::rustgis_tiles::cache; + use std::sync::{ + Arc, + atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst}, + }; + struct Cube(AtomicUsize); + impl super::TimeCube for Cube { + fn dates(&self) -> &[f64] { + &[0.0, 1.0] + } + fn label(&self) -> String { + "synthetic".into() + } + fn readers(&self) -> usize { + 2 + } + fn slice(&self, _: usize) -> Result, usize)>, String> { + self.0.fetch_add(1, SeqCst); + let still = cache::current_still(); + assert!(still.is_some(), "both run and slice threads must inherit context"); + cache::check_still(&still)?; + Ok(None) + } + } + let live = Arc::new(AtomicBool::new(true)); + let flag = live.clone(); + let still: cache::Still = Arc::new(move || flag.load(SeqCst)); + let cube = Cube(AtomicUsize::new(0)); + let run = || { + super::run_telling( + "synthetic", + 2, + |_, _| {}, + |cancel, _| { + assert!(cache::current_still().is_some(), "run_telling must carry the predicate"); + super::read_slices(&cube, &[0, 1], 0, cancel, false, &mut |_, _| Ok(())) + }, + ) + }; + assert!(cache::with_still(still.clone(), run).is_ok()); + live.store(false, SeqCst); + assert!(cache::with_still(still, run).unwrap_err().contains("cancelled")); + assert!(cube.0.load(SeqCst) >= 3); + } /// The outputs' names: the date taken out, a MODIS date's A with it, and no other final A. #[test] fn labels_lose_their_date() { @@ -2004,3 +2050,49 @@ mod tests { assert!(e.contains("50 cells × classes across the groups being read, more than 49"), "{e}"); } } + +/// The executor's pure precedence, shared before either planning or I/O-capable cube assembly. +#[derive(Clone, Copy)] +enum CubeChoice<'a> { + Layers(&'a Value), + Collection, + Zarr { layer: Option }, + Bands(u32), +} +/// Purely chooses layer-array, STAC, Zarr or primary-band precedence; it never opens a source. +fn cube_choice<'a>(input: &impl super::InputSource, p: &'a Value) -> Result, String> { + let context = input.input_context(); + let s = &context; + let given = |k| p.get(k).is_some_and(|v| !v.is_null()); + if let Some(layers) = p.get("layers").filter(|v| !v.is_null()) { + return Ok(CubeChoice::Layers(layers)); + } + if given("collection") { + return Ok(CubeChoice::Collection); + } + if given("path") || given("url") { + return Ok(CubeChoice::Zarr { layer: None }); + } + let id = s.layer_id(p)?; + let zarr = s + .map() + .layer(id) + .and_then(|l| l.raster.as_ref()) + .is_some_and(|r| r.time_zarr.is_some() || r.subdataset.as_deref().is_some_and(|d| d.contains('['))); + Ok(if zarr { CubeChoice::Zarr { layer: Some(id) } } else { CubeChoice::Bands(id) }) +} +/// Binds the chosen in-project cube inputs and leaves explicit external locations in frozen parameters. +fn cube_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let ids = match cube_choice(input, p)? { + CubeChoice::Layers(list) => list + .as_array() + .ok_or("layers is a list of raster layers")? + .iter() + .map(|v| s.layer_id(&json!({"layer":v}))) + .collect::, _>>()?, + CubeChoice::Zarr { layer: Some(id) } | CubeChoice::Bands(id) => vec![id], + CubeChoice::Collection | CubeChoice::Zarr { layer: None } => Vec::new(), + }; + Ok(super::Inputs::layers(ids.iter().filter_map(|id| s.map().layer(*id)).map(|l| (s.map(), l)))) +} diff --git a/crates/engine/src/cmd/cube_catalog.rs b/crates/engine/src/cmd/cube_catalog.rs index a32a20dfbd..1184d9f1ed 100644 --- a/crates/engine/src/cmd/cube_catalog.rs +++ b/crates/engine/src/cmd/cube_catalog.rs @@ -14,10 +14,10 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("cube.catalog", "Data Cube Catalog", "Home › Add › Web Data", catalog).roles(&[]).view().params( + C::new("cube.catalog", "Data Cube Catalog", "Home › Add › Web Data", catalog).roles(&[]).effects(crate::policy::Effects::NONE).view().params( r#"{"theme"?: "vegetation"|"land cover"|"water"|"climate"|"ocean"|"atmosphere"|"elevation", "bbox"?: [west, south, east, north] | "view" (entries with data there; default: anywhere), "query"?: "words, all of them in an entry's name, provider or id"} → the data cubes ready to open (cube.open), each with its provider, themes, area, period, step, resolution, source (a STAC collection and band, or a Zarr variable) and licence"#, ), - C::new("cube.open", "Open a Data Cube", "Home › Add › Web Data", open).roles(&[]).params( + C::new("cube.open", "Open a Data Cube", "Home › Add › Web Data", open).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"id": an entry of cube.catalog, "bbox"?: [west, south, east, north] | "view" (a STAC entry's area, default the map's extent; a Zarr entry's variable is read whole, but an area given must meet the entry's), "start"?, "end"?: dates, both or neither (default: the entry's last full calendar year), "maxCloud"?: percent (a STAC entry's cloud cover cap; default the entry's), "range"?: [low, high] (a STAC entry's values shown from black to white, in the band's units; default the entry's), "name"?: text} → the cube on the map, following the time slider, as stac.addCube or layer.addZarr adds it (desktop)"#, ), ] diff --git a/crates/engine/src/cmd/deeplearning.rs b/crates/engine/src/cmd/deeplearning.rs index 0c5470b25e..f28008829f 100644 --- a/crates/engine/src/cmd/deeplearning.rs +++ b/crates/engine/src/cmd/deeplearning.rs @@ -52,10 +52,10 @@ pub fn commands() -> Vec { let mut r = json!({"model": m.name, "accuracy": m.accuracy, "classes": m.model.classes, "epochs": m.epochs}); r["replaced"] = json!(store(s, m)); Ok(r) - }).roles(&[]) + }).inputs(training_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_raster) .params(r#"{"layer": raster, "training": polygon layer, "classField": field naming each polygon's class, "bands"?: [1-based], "hidden"?: [units per hidden layer] (default [32, 16]), "epochs"?: 1..500 (default 60), "model"?: name}"#), - C::new("dl.classify", "Deep Learning Tools", "Analyze › Image Classification", classify).roles(&[]).when(need_raster).params( + C::new("dl.classify", "Deep Learning Tools", "Analyze › Image Classification", classify).inputs(classify_inputs).roles(&[]).effects(crate::policy::Effects::NONE).when(need_raster).params( r#"{"layer": raster, "model"?: a trained model's name, "training"?: polygon layer, "classField"?: field, "bands"?: [1-based], "hidden"?: [units], "epochs"?: 1..500 (without a model: train one first, as dl.train), "name"?: output, "ran"?: (internal: classes the app has worked out)}"#, ), C::new("dl.package", "Deep Learning Package", "Publish › Package", |s, p| { @@ -63,7 +63,7 @@ pub fn commands() -> Vec { let bytes = package(s, p)?; rustgis_data::io::write_atomic(Path::new(path), &bytes).map_err(|e| format!("{path}: {e}"))?; Ok(json!({"path": path, "bytes": bytes.len()})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_model) .params(r#"{"model"?: name (default: the last trained), "path": "*.rgisdlpk"}"#), @@ -91,16 +91,16 @@ pub fn commands() -> Vec { let name = m.name.clone(); store(s, m); Ok(json!({"model": name})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"path"?: "*.rgisdlpk", "data"?: base64}"#), C::new("dl.remove", "Delete Model", "Analyze › Image Classification", |s, p| { let i = model_index(s, p)?; s.project.dl_models.remove(i); Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_model) .params(r#"{"model": name}"#), - C::new("imagery.hostedImagery", "Create Hosted Imagery", "Analyze › Imagery Sharing", hosted).roles(&[]).view().when(need_raster).params( + C::new("imagery.hostedImagery", "Create Hosted Imagery", "Analyze › Imagery Sharing", hosted).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).view().when(need_raster).params( r#"{"layer": raster, "path": "*.zip" (z/x/y.png tiles, tiles.json and a viewer page), "minZoom"?: 0..22, "maxZoom"?: 0..22 (default: the raster's resolution)}"#, ), ] @@ -134,7 +134,9 @@ fn store(s: &mut Session, m: DlModel) -> bool { } } -fn model_index(s: &Session, p: &Value) -> Result { +fn model_index(input: &impl super::InputSource, p: &Value) -> Result { + let context = input.input_context(); + let s = &context; let ms = &s.project.dl_models; match ps(p, "model") { Some(n) => ms.iter().position(|m| m.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no model '{n}'")), @@ -190,7 +192,7 @@ fn training(s: &Session, p: &Value) -> Result { let r = raster(s, p)?; r.require_coordinates()?; let bands = bands_param(p, &r)?; - let tl = ps(p, "training").ok_or("training (a polygon layer) is required")?; + let tl = super::imagery::training_name(p)?; let l = s .map() .layer_by_name(tl) @@ -478,3 +480,20 @@ onwheel=e=>{{const nz=Math.max(minZ,Math.min(maxZ,z+(e.deltaY<0?1:-1)));if(nz!== cy = c.y ) } + +/// Binds both raster and training polygons through their shared literal selectors. +fn training_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + super::Inputs::named(s.map(), [super::imagery::raster_name(p)?, super::imagery::training_name(p)?]) +} +/// Follows classification's training-or-saved-model branch without copying trained weights. +fn classify_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + if p.get("training").is_some() { + return training_inputs(input, p); + } + let s = input; + let mut inputs = super::Inputs::named(s.map(), [super::imagery::raster_name(p)?])?; + let i = model_index(input, p)?; + inputs.state = json!({"model":s.project.dl_models.get(i).map(|m|&m.name),"index":i}); + Ok(inputs) +} diff --git a/crates/engine/src/cmd/design.rs b/crates/engine/src/cmd/design.rs index 1fdd96cd7e..019cae3351 100644 --- a/crates/engine/src/cmd/design.rs +++ b/crates/engine/src/cmd/design.rs @@ -81,7 +81,7 @@ pub(crate) fn apply_with(s: &mut Session, id: u32, changes: Vec, new_def Elevation::Constant(z) => Some(*z), Elevation::Surface(name) => { let c = g.label_point()?; - let r = s.map().layer_by_name(name)?.raster.as_ref()?.data.as_ref()?; + let r = surface_layer(s, name)?.raster.as_ref()?.data.as_ref()?; let (col, row) = r.pixel_at(c)?; r.value(0, col, row).map(f64::from) } @@ -238,16 +238,16 @@ pub(crate) fn apply_with(s: &mut Session, id: u32, changes: Vec, new_def pub fn commands() -> Vec { vec![ // Data design. - C::new("data.domain", "Domains", "Data › Data Design", domain).roles(&[]).when(need_feature_layer).params( + C::new("data.domain", "Domains", "Data › Data Design", domain).roles(&[]).effects(crate::policy::Effects::NONE).when(need_feature_layer).params( r#"{"layer"?, "field": string, "coded"?: [[code, description], …], "min"?: number, "max"?: number, "clear"?: bool, "force"?: bool (set it even though existing values fall outside)}"#, ), - C::new("data.subtypes", "Subtypes", "Data › Data Design", subtypes).roles(&[]).when(need_feature_layer).params( + C::new("data.subtypes", "Subtypes", "Data › Data Design", subtypes).roles(&[]).effects(crate::policy::Effects::NONE).when(need_feature_layer).params( r#"{"layer"?, "field": integer field, "subtypes": [{"code": n, "name": string, "defaults"?: {field: value}}], "symbolize"?: bool, "clear"?: bool}"#, ), - C::new("data.attributeRule", "Attribute Rules", "Data › Data Design", attribute_rule).roles(&[]).when(need_feature_layer).params( + C::new("data.attributeRule", "Attribute Rules", "Data › Data Design", attribute_rule).roles(&[]).effects(crate::policy::Effects::NONE).when(need_feature_layer).params( r#"{"layer"?, "name": string, "kind": "calculation"|"constraint", "field"?: field set by a calculation, "expression": calculator expression or SQL where clause, "remove"?: bool}"#, ), - C::new("data.contingentValues", "Contingent Values", "Data › Data Design", contingent).roles(&[]).when(need_feature_layer).params( + C::new("data.contingentValues", "Contingent Values", "Data › Data Design", contingent).roles(&[]).effects(crate::policy::Effects::NONE).when(need_feature_layer).params( r#"{"layer"?, "name": string, "fields": [field, …], "combos": [[value, …], …], "remove"?: bool}"#, ), C::new("data.archive", "Add Archive", "Data › Archiving", |s, p| { @@ -256,7 +256,7 @@ pub fn commands() -> Vec { let l = s.map_mut().layer_mut(id).ok_or("no such layer")?; l.design.archive = if on { Some(l.design.archive.take().unwrap_or_default()) } else { None }; Ok(json!({"archiving": on})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "on"?: bool}"#), C::new("data.archiveHistory", "Archive History", "Data › Archiving", |s, p| { @@ -270,23 +270,23 @@ pub fn commands() -> Vec { .map(|e| json!({"time": e.time, "feature": e.id, "action": match (&e.before, &e.after) { (None, _) => "insert", (_, None) => "delete", _ => "update" }})) .collect(); Ok(json!({"edits": a.len(), "recent": rows})) - }).roles(&[]).params(r#"{"layer"?: name|id (default: the active layer)}"#) + }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?: name|id (default: the active layer)}"#) .view() .when(need_feature_layer), - C::new("data.archiveRestore", "Restore From Archive", "Data › Archiving", archive_restore).roles(&[]) + C::new("data.archiveRestore", "Restore From Archive", "Data › Archiving", archive_restore).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_editable) .params(r#"{"layer"?, "time": seconds since 1970 (the layer as it was then)}"#), - C::new("data.relate", "Related Data", "Data › Relationship", relate).roles(&[]).params( + C::new("data.relate", "Related Data", "Data › Relationship", relate).inputs(relationship_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"name": string, "origin": layer, "originKey": field, "destination": layer, "destinationKey": field, "remove"?: bool}"#, ), - C::new("data.related", "Select Related", "Data › Relationship", related).roles(&[]) + C::new("data.related", "Select Related", "Data › Relationship", related).inputs(related_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name"?: relationship (all when omitted), "select"?: bool (select the related rows; default true)}"#), // Manage edits. C::new("edit.enable", "Edit", "Edit › Manage Edits", |s, p| { let set = &mut s.map_mut().editing.settings; set.enabled = b(p, "on").unwrap_or(!set.enabled); Ok(json!({"enabled": set.enabled})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"on"?: bool}"#), C::new("edit.settings", "Editing Settings", "Edit › Manage Edits", |s, p| { let set = &mut s.map_mut().editing.settings; @@ -294,7 +294,7 @@ pub fn commands() -> Vec { set.show_vertices = v; } Ok(json!({"enabled": set.enabled, "showVertices": set.show_vertices})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"showVertices"?: bool}"#), C::new("edit.status", "Edit Status", "Edit › Manage Edits", |s, _| { let rows: Vec = s @@ -303,9 +303,9 @@ pub fn commands() -> Vec { .filter_map(|(id, c)| s.map().layer(*id).map(|l| json!({"layer": l.name, "added": c.added, "modified": c.modified, "deleted": c.deleted}))) .collect(); Ok(json!({"unsaved": s.dirty(), "layers": rows})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("edit.template", "Manage Templates", "Edit › Manage Edits", template).roles(&[]).when(need_feature_layer).params( + C::new("edit.template", "Manage Templates", "Edit › Manage Edits", template).roles(&[]).effects(crate::policy::Effects::NONE).when(need_feature_layer).params( r#"{"layer"?, "name": string, "defaults"?: {field: value}, "description"?: string, "remove"?: bool}"#, ), // Snapping. @@ -321,9 +321,9 @@ pub fn commands() -> Vec { } } Ok(json!({"enabled": sn.enabled, "tolerancePx": sn.tolerance_px, "vertex": sn.vertex, "edge": sn.edge, "end": sn.end})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"on"?: bool, "tolerancePx"?: number, "vertex"?: bool, "edge"?: bool, "end"?: bool}"#), - C::new("edit.snap", "Snap Point", "Edit › Snapping", snap).roles(&[]).view().params(r#"{"lon", "lat"}"#), + C::new("edit.snap", "Snap Point", "Edit › Snapping", snap).roles(&[]).effects(crate::policy::Effects::NONE).view().params(r#"{"lon", "lat"}"#), // Modify features. C::new("edit.move", "Move", "Edit › Features › Modify", |s, p| { let unit = distance_unit_metres(p)?; @@ -335,7 +335,7 @@ pub fn commands() -> Vec { return Err("dx and dy must be finite distances in the command's units (limit: 40,000 km per axis after ground-to-grid)".into()); } modify(s, |g| Some(g.map_coords(&|c: Coord| shift(c, dx, dy)))) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_editable_selection) .params(r#"{"dx": distance east, "dy": distance north, "units"?: "Meters" (default)|"Feet"|"Kilometers"|"Miles"}"#), C::new("edit.rotate", "Rotate", "Edit › Features › Modify", |s, p| { @@ -355,7 +355,7 @@ pub fn commands() -> Vec { merc_to_lonlat(Coord::new(c.x + x * cos - y * sin, c.y + x * sin + y * cos)) })) }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_editable_selection) .params(r#"{"angle": degrees clockwise}"#), C::new("edit.scale", "Scale", "Edit › Features › Modify", |s, p| { @@ -371,7 +371,7 @@ pub fn commands() -> Vec { merc_to_lonlat(Coord::new(c.x + (m.x - c.x) * k, c.y + (m.y - c.y) * k)) })) }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_editable_selection) .params(r#"{"factor": number}"#), C::new("edit.flip", "Flip Line", "Edit › Features › Modify", |s, _| { @@ -379,36 +379,36 @@ pub fn commands() -> Vec { Geometry::Line(parts) => Some(Geometry::Line(parts.iter().rev().map(|p| p.iter().rev().copied().collect()).collect())), _ => None, }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_editable_selection), - C::new("edit.setGeometry", "Set Geometry", "Edit › Features › Modify", set_geometry).roles(&[]) + C::new("edit.setGeometry", "Set Geometry", "Edit › Features › Modify", set_geometry).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_editable) .params(r#"{"layer"?, "row": number, "geometry": GeoJSON geometry}"#), - C::new("edit.vertex", "Edit Vertex", "Edit › Features › Modify", edit_vertex).roles(&[]).when(need_editable).params( + C::new("edit.vertex", "Edit Vertex", "Edit › Features › Modify", edit_vertex).roles(&[]).effects(crate::policy::Effects::NONE).when(need_editable).params( r#"{"layer"?, "row": number, "action": "move"|"insert"|"delete", "part"?: number, "ring"?: number, "index": number (insert: the vertex the new one follows), "lon"?, "lat"?}"#, ), - C::new("edit.merge", "Merge", "Edit › Features › Modify", merge).roles(&[]).when(need_editable_selection), - C::new("edit.moveSharedVertex", "Move Shared Vertex", "Edit › Features › Modify", shared_vertex::run).roles(&[]).when(need_editable).params( + C::new("edit.merge", "Merge", "Edit › Features › Modify", merge).roles(&[]).effects(crate::policy::Effects::NONE).when(need_editable_selection), + C::new("edit.moveSharedVertex", "Move Shared Vertex", "Edit › Features › Modify", shared_vertex::run).roles(&[]).effects(crate::policy::Effects::NONE).when(need_editable).params( r#"{"layer"?, "row": number, "part"?: number, "ring"?: number, "index": number, "lon": degrees, "lat": degrees}"#, ), - C::new("edit.explode", "Explode", "Edit › Features › Modify", explode).roles(&[]).when(need_editable_selection), - C::new("edit.split", "Split", "Edit › Features › Modify", split).roles(&[]).when(need_editable_selection).params(r#"{"lon", "lat"}"#), - C::new("edit.cutPolygon", "Cut Polygon", "Edit › Features › Modify", sketch::cut_polygon).roles(&[]) + C::new("edit.explode", "Explode", "Edit › Features › Modify", explode).roles(&[]).effects(crate::policy::Effects::NONE).when(need_editable_selection), + C::new("edit.split", "Split", "Edit › Features › Modify", split).roles(&[]).effects(crate::policy::Effects::NONE).when(need_editable_selection).params(r#"{"lon", "lat"}"#), + C::new("edit.cutPolygon", "Cut Polygon", "Edit › Features › Modify", sketch::cut_polygon).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_editable_selection) .params(r#"{"line": GeoJSON LineString (lon/lat) drawn across the selected polygons}"#), - C::new("edit.reshape", "Reshape", "Edit › Features › Modify", sketch::reshape).roles(&[]) + C::new("edit.reshape", "Reshape", "Edit › Features › Modify", sketch::reshape).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_editable_selection) .params(r#"{"line": GeoJSON LineString (lon/lat) drawn across the selected line or polygon's boundary}"#), - C::new("edit.trace", "Trace Edge Path", "Edit › Features", sketch::trace).roles(&[]).view().params( + C::new("edit.trace", "Trace Edge Path", "Edit › Features", sketch::trace).roles(&[]).effects(crate::policy::Effects::NONE).view().params( r#"{"from": [lon, lat], "to": [lon, lat], "layer"?, "row"?: number (else the nearest edge within the tolerance), "tolerancePx"?: number (default: the snapping tolerance)}"#, ), - C::new("edit.traverse", "Traverse", "Edit › Features › Modify", traverse).roles(&[]).when(need_editable).params( + C::new("edit.traverse", "Traverse", "Edit › Features › Modify", traverse).roles(&[]).effects(crate::policy::Effects::NONE).when(need_editable).params( r#"{"layer"?, "start": [lon, lat], "units"?: "Meters" (default)|"Feet"|"Kilometers"|"Miles", "legs": [{"distance": number, "bearing": degrees from north}]} (closed in a polygon layer, open in a line layer)"#, ), C::new("edit.tools", "Editor Tools", "Edit › Tools", |s, _| { let tools: Vec = s.registry.commands.iter().filter(|c| c.location.starts_with("Edit › Features")).map(|c| json!({"id": c.id, "label": c.label, "params": c.params})).collect(); Ok(Json::Array(tools)) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), // Elevation and corrections. C::new("edit.elevation", "Elevation", "Edit › Elevation", |s, p| { @@ -423,15 +423,15 @@ pub fn commands() -> Vec { Elevation::Constant(z.clamp(-12_000.0, 100_000.0)) }, "surface" => { - let n = ps(p, "surface").ok_or("surface (a raster layer) is required")?; - let l = s.map().layer_by_name(n).filter(|l| l.is_raster()).ok_or_else(|| format!("no raster layer '{n}'"))?; + let n = surface_name(p)?; + let l = surface_layer(s,n).ok_or_else(|| format!("no raster layer '{n}'"))?; Elevation::Surface(l.name.clone()) } m => return Err(format!("unknown elevation mode '{m}'")), }; s.map_mut().editing.settings.elevation = e; Ok(Json::Null) - }).roles(&[]) + }).inputs(elevation_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"mode": "none"|"constant"|"surface", "z"?: number, "units"?: "Meters" (default)|"Feet"|"Kilometers"|"Miles", "surface"?: raster layer}"#), C::new("edit.groundToGrid", "Ground To Grid", "Edit › Corrections", |s, p| { let on = b(p, "on").unwrap_or(true); @@ -442,13 +442,13 @@ pub fn commands() -> Vec { } s.map_mut().editing.settings.ground_to_grid = on.then_some((k, r.clamp(-360.0, 360.0))); Ok(json!({"on": on})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"on"?: bool, "scale"?: factor, "rotation"?: degrees}"#), // Topology. - C::new("topology.rules", "Topology Options", "Edit › Manage Edits", topology_rules).roles(&[]).params( + C::new("topology.rules", "Topology Options", "Edit › Manage Edits", topology_rules).inputs(topology_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"add"?: {"rule": "mustNotOverlap"|"mustNotHaveGaps"|"mustNotSelfIntersect"|"mustNotHaveDangles"|"mustBeCoveredBy", "layer": layer, "by"?: polygon layer}, "remove"?: index, "clear"?: bool}"#, ), - C::new("topology.validate", "Error Inspector", "Edit › Manage Edits", topology_validate).roles(&[]).view().params(r#"{"toleranceM"?: metres (dangles)}"#), + C::new("topology.validate", "Error Inspector", "Edit › Manage Edits", topology_validate).inputs(validation_inputs).roles(&[]).effects(crate::policy::Effects::NONE).view().params(r#"{"toleranceM"?: metres (dangles)}"#), ] } @@ -1043,9 +1043,9 @@ fn relate(s: &mut Session, p: &Json) -> CmdResult { let f = l.dataset.field_index(key).and_then(|i| l.dataset.fields.get(i)).ok_or_else(|| format!("{layer} has no field '{key}'"))?; Ok((l.name.clone(), f.name.clone())) }; - let (origin, origin_key) = field_of(ps(p, "origin").ok_or("origin is required")?, ps(p, "originKey").ok_or("originKey is required")?)?; - let (destination, destination_key) = - field_of(ps(p, "destination").ok_or("destination is required")?, ps(p, "destinationKey").ok_or("destinationKey is required")?)?; + let [origin_name, destination_name] = relationship_names(p)?; + let (origin, origin_key) = field_of(origin_name, ps(p, "originKey").ok_or("originKey is required")?)?; + let (destination, destination_key) = field_of(destination_name, ps(p, "destinationKey").ok_or("destinationKey is required")?)?; let rels = &mut s.map_mut().editing.relationships; rels.retain(|r| !r.name.eq_ignore_ascii_case(&name)); rels.push(Relationship { name: name.clone(), origin, origin_key, destination, destination_key }); @@ -1054,18 +1054,7 @@ fn relate(s: &mut Session, p: &Json) -> CmdResult { /// The rows related to the selection through relationships (both ways), selecting them. fn related(s: &mut Session, p: &Json) -> CmdResult { - let rels: Vec = match ps(p, "name") { - Some(n) => vec![ - s.map() - .editing - .relationships - .iter() - .find(|r| r.name.eq_ignore_ascii_case(n)) - .cloned() - .ok_or_else(|| format!("no relationship '{n}'"))?, - ], - None => s.map().editing.relationships.clone(), - }; + let rels = related_relationships(s, p)?; if rels.is_empty() { return Err("the map has no relationships".into()); } @@ -1258,8 +1247,9 @@ fn topology_rules(s: &mut Session, p: &Json) -> CmdResult { rules.remove(i as usize); } if let Some(a) = p.get("add") { + let names = topology_add_names(p)?; let layer_of = |k: &str| -> Result { - let n = a.get(k).and_then(Json::as_str).ok_or_else(|| format!("{k} is required"))?; + let n = names.get(if k == "by" { 1 } else { 0 }).copied().ok_or_else(|| format!("{k} is required"))?; s.map().layer_by_name(n).filter(|l| !l.is_raster()).map(|l| l.name.clone()).ok_or_else(|| format!("no feature layer '{n}'")) }; let layer = layer_of("layer")?; @@ -1342,3 +1332,96 @@ fn topology_validate(s: &mut Session, p: &Json) -> CmdResult { s.topology_errors = errors; Ok(json!({"errors": n, "first": out, "incomplete": incomplete})) } + +/// Resolves the first named relationship, or all relationships, for both-direction selection. +fn related_relationships(input: &impl super::InputSource, p: &Json) -> Result, String> { + let context = input.input_context(); + let s = &context; + Ok(match ps(p, "name") { + Some(n) => vec![ + s.map() + .editing + .relationships + .iter() + .find(|r| r.name.eq_ignore_ascii_case(n)) + .cloned() + .ok_or_else(|| format!("no relationship '{n}'"))?, + ], + None => s.map().editing.relationships.clone(), + }) +} + +/// Removal consumes no endpoints; creation binds both actual feature-layer names. +fn relationship_inputs(input: &super::InputContext<'_>, p: &Json) -> Result { + let s = input; + if b(p, "remove") == Some(true) { + return Ok(super::Inputs::layers(std::iter::empty())); + } + let names = relationship_names(p)?; + super::Inputs::named(s.map(), names) +} +/// Parses the required origin and destination names shared with relationship creation. +fn relationship_names(p: &Json) -> Result<[&str; 2], String> { + Ok([ps(p, "origin").ok_or("origin is required")?, ps(p, "destination").ok_or("destination is required")?]) +} +/// Binds the existing endpoints visited by the same stored relationship selection. +fn related_inputs(input: &super::InputContext<'_>, p: &Json) -> Result { + let s = input; + let relationships = related_relationships(input, p)?; + Ok(super::Inputs::layers( + relationships.iter().flat_map(|r| [&r.origin, &r.destination]).filter_map(|n| s.map().layer_by_name(n)).map(|l| (s.map(), l)), + )) +} +/// Binds only the endpoints of a rule being added. +fn topology_inputs(input: &super::InputContext<'_>, p: &Json) -> Result { + let s = input; + let names = topology_add_names(p)?; + super::Inputs::named(s.map(), names) +} +/// Only a covered-by rule consumes a second layer named by. +fn topology_add_names(p: &Json) -> Result, String> { + let Some(add) = p.get("add") else { return Ok(Vec::new()) }; + let mut names = vec![add.get("layer").and_then(Json::as_str).ok_or("layer is required")?]; + if add.get("rule").and_then(Json::as_str) == Some("mustBeCoveredBy") { + names.push(add.get("by").and_then(Json::as_str).ok_or("by is required")?); + } + Ok(names) +} +/// Binds the stored rule endpoints that validation can actually find. +fn validation_inputs(input: &super::InputContext<'_>, _: &Json) -> Result { + let s = input; + let names = s.map().editing.topology.iter().flat_map(|r| { + let mut names = vec![r.layer()]; + if let TopologyRule::MustBeCoveredBy { by, .. } = r { + names.push(by.as_str()); + } + names + }); + Ok(super::Inputs::layers(names.filter_map(|n| s.map().layer_by_name(n)).map(|l| (s.map(), l)))) +} + +/// Finds the literal raster surface used for elevation validation and sampling. +fn surface_layer<'a>(input: &'a impl super::InputSource, name: &str) -> Option<&'a rustgis_map::Layer> { + let context = input.input_context(); + let s = &context; + s.map().layer_by_name(name).filter(|l| l.is_raster()) +} +/// The literal surface selector shared by the executor and dependency planning. +fn surface_name(p: &Json) -> Result<&str, String> { + ps(p, "surface").ok_or("surface (a raster layer) is required".into()) +} +/// Only surface mode consumes a layer; constant and none are parameter-only. +fn elevation_inputs(input: &super::InputContext<'_>, p: &Json) -> Result { + let name = if ps(p, "mode") == Some("surface") { Some(surface_name(p)?) } else { None }; + super::Inputs::named(input.map(), name) +} +/// Only new rows sample the stored elevation surface; ordinary attribute edits do not. +pub(crate) fn new_feature_inputs(input: &super::InputContext<'_>, p: &Json) -> Result { + let s = input; + let mut inputs = super::Inputs::primary(s, p)?; + if let Elevation::Surface(name) = &s.map().editing.settings.elevation { + inputs.layers.extend(surface_layer(input, name).map(|l| (s.map().instance_id(), l.id))); + inputs.state = json!({"surface":name}); + } + Ok(inputs) +} diff --git a/crates/engine/src/cmd/diagram.rs b/crates/engine/src/cmd/diagram.rs index 5311b377f5..47e6b30e44 100644 --- a/crates/engine/src/cmd/diagram.rs +++ b/crates/engine/src/cmd/diagram.rs @@ -17,17 +17,27 @@ enum Asked { Scaled(f32, f32), } +/// Registers diagram styling and inspection over the selected layer's resident data. pub fn commands() -> Vec { vec![ - C::new("layer.diagram", "Set Diagram", "Style › Drawing", set).roles(&[]) + C::new("layer.diagram", "Set Diagram", "Style › Drawing", set) + .roles(&[]) + .effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) .when(need_feature_layer) .params( r##"{"layer"?, "kind": "pie"|"bar", "fields": [numeric field names, or {"field": name, "color"?: "#RRGGBB"|"#RRGGBBAA"}] (a slice or bar each, in order; colours default to the palette new layers take), "size"?: points (fixed: a pie's radius or a bar chart's height, the same for every feature) | {"from"?: points (default 6), "to"?: points (default 40)} (grown with the categories' sum, square-root scaled as Proportional symbols are; the default), "startAngle"?: degrees clockwise from north (a pie's first slice; default 0), "labels"?: "none"|"value"|"percent"|"both" (what each slice or bar says beside it: its value in the legend's number format, its share of the total in percent, or both; default "none"), "background"?: "#RRGGBB"|"#RRGGBBAA"|null (the disc or box behind the chart; default none), "outline"?: {"color"?: "#RRGGBB"|"#RRGGBBAA" (default #4D4D4D), "width"?: points (default 0.75)}} (drawn on each feature over its symbol)"##, ), - C::new("layer.clearDiagram", "Clear Diagram", "Style › Drawing", |s, p| with(s, p, |l| l.diagram = None)).roles(&[]) + C::new("layer.clearDiagram", "Clear Diagram", "Style › Drawing", |s, p| with(s, p, |l| l.diagram = None)) + .roles(&[]) + .effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) .when(need_feature_layer) .params(r#"{"layer"?: name|id (removes the layer's diagram; the renderer is untouched)}"#), - C::new("layer.diagramInfo", "Diagram Settings", "Style › Drawing", info).roles(&[]) + C::new("layer.diagramInfo", "Diagram Settings", "Style › Drawing", info) + .roles(&[]) + .effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) .view() .when(need_feature_layer) .params(r#"{"layer"?: name|id} → {"kind", "fields": [{"field", "color"}], "size": points (fixed) | {"from", "to", "min", "max"} (scaled; min and max are the range of sums it was measured over), "startAngle"?, "labels", "background"?, "outline"} or null without one (layer.diagram takes it back as it is)"#), diff --git a/crates/engine/src/cmd/directions.rs b/crates/engine/src/cmd/directions.rs index b7b2c3556b..8a7d3053cd 100644 --- a/crates/engine/src/cmd/directions.rs +++ b/crates/engine/src/cmd/directions.rs @@ -11,7 +11,7 @@ use crate::render::rustgis_tiles::routing::{self, Avoid, Mode, Route, Router}; use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { - vec![C::new("map.route", "Directions", "Home › Navigate", route).roles(&[]).params( + vec![C::new("map.route", "Directions", "Home › Navigate", route).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"from": [lon, lat] | "a place, an address or 'lat, lon'", "to": the same, "via"?: [places] (stops on the way), "mode"?: "drive" (default) | "bike" | "walk", "avoid"?: ["tolls", "highways", "ferries"] (driving), "imperial"?: bool (miles in the instructions), "language"?: "en-US" | "de" | …, "url"?: "a Valhalla server of your own", "addLayer"?: bool (adds the route and its steps as layers, default true), "zoom"?: bool (default true), "answer"?: "a routing answer already fetched (the app's; nothing is fetched)", "route"?: index of the route in answer}"#, )] } diff --git a/crates/engine/src/cmd/earthdata.rs b/crates/engine/src/cmd/earthdata.rs index bab3e474ea..bd60d26210 100644 --- a/crates/engine/src/cmd/earthdata.rs +++ b/crates/engine/src/cmd/earthdata.rs @@ -12,16 +12,16 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("earthdata.collections", "Search NASA Earthdata Collections", "Home › Add › Web Data › NASA Earthaccess", collections).roles(&[]) + C::new("earthdata.collections", "Search NASA Earthdata Collections", "Home › Add › Web Data › NASA Earthaccess", collections).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)) .view() .params(r#"{"keyword"?: text (free text: "sea surface temperature"), "shortName"?: "a collection's short name (HLSL30, ATL08, GEDI02_A…)", "cloudHosted"?: bool (only collections in NASA's cloud), "limit"?: 1–100 (20), "page"?: number (1), "answer"?: "CMR's JSON answer, when already fetched (the app's background request)"} — keyword or shortName is needed; searching needs no Earthdata Login"#), - C::new("earthdata.granules", "Search NASA Earthdata Granules", "Home › Add › Web Data › NASA Earthaccess", granules).roles(&[]).params( + C::new("earthdata.granules", "Search NASA Earthdata Granules", "Home › Add › Web Data › NASA Earthaccess", granules).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"collection"?: "a collection's concept id (C…-PROVIDER, as earthdata.collections lists it)", "shortName"?: "or a short name: its cloud-hosted, newest version", "bbox"?: [west, south, east, north] | "view" (the map's extent, the default), "view"?: bool (false: everywhere), "start"?: "YYYY-MM-DD", "end"?: "YYYY-MM-DD", "limit"?: 1–100 (25), "page"?: number (1), "footprints"?: bool (adds the granules' footprints as a layer, default true), "name"?: text (the footprints layer's), "answer"?: "CMR's JSON answer, when already fetched"} — each granule lists its data files; add a GeoTIFF with layer.addUrl (files need an Earthdata Login token: earthdata.login)"#, ), - C::new("earthdata.login", "Earthdata Login Token", "Home › Add › Web Data › NASA Earthaccess", login).roles(&[]).view().params( + C::new("earthdata.login", "Earthdata Login Token", "Home › Add › Web Data › NASA Earthaccess", login).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( r#"{"token": "an Earthdata Login token (Earthdata Login › Generate Token)", "remember"?: bool (false: kept for the session only; true: also remembered on this computer, in the system's credential store, and read back when next needed; without it a token remembered before is deleted)} — never in the project; sent only to NASA's Earthdata data hosts (https://*.earthdatacloud.nasa.gov, *.earthdata.nasa.gov) when their files are read; an expired token is forgotten"#, ), - C::new("earthdata.logout", "Forget Earthdata Login Token", "Home › Add › Web Data › NASA Earthaccess", logout).roles(&[]) + C::new("earthdata.logout", "Forget Earthdata Login Token", "Home › Add › Web Data › NASA Earthaccess", logout).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)) .view() .params("{} — forgets the token for the session and deletes the one remembered on this computer"), C::new("earthdata.status", "Earthdata Login Status", "Home › Add › Web Data › NASA Earthaccess", |_s, _p| { @@ -29,7 +29,7 @@ pub fn commands() -> Vec { earthdata::restore(); earthdata::forget_if_expired(); Ok(status()) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params("{} — whether a token is kept, its expiry, whether it is remembered on this computer, and whether an expired one was forgotten (give a new one with earthdata.login)"), ] diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 6554e1f44a..53fdedae7d 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -8,23 +8,23 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("edit.calculateGeometry", "Calculate Geometry", "Attribute Table", crate::geometry_calc::run).roles(&[]) + C::new("edit.calculateGeometry", "Calculate Geometry", "Attribute Table", crate::geometry_calc::run).roles(&[]).effects(crate::policy::Effects::NONE) .when(super::design::need_editable) .params(r#"{"layer"?, "field": string, "create": bool, "overwrite": bool, "property": string, "unit": string, "epsg"?: number, "scope": "all"|"selected"}"#), - C::new("edit.addFeature", "Create Feature", "Edit › Features", add_feature).roles(&[]) + C::new("edit.addFeature", "Create Feature", "Edit › Features", add_feature).inputs(super::design::new_feature_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(super::design::need_editable) .params(r#"{"layer"?, "geometry": GeoJSON geometry (lon/lat), "attributes"?: {field: value}, "template"?: feature template}"#), - C::new("edit.delete", "Delete", "Edit › Features", delete).roles(&[]).key("Delete").when(super::design::need_editable_selection), - C::new("edit.setValue", "Edit Attribute", "Attribute Table", set_value).roles(&[]) + C::new("edit.delete", "Delete", "Edit › Features", delete).roles(&[]).effects(crate::policy::Effects::NONE).key("Delete").when(super::design::need_editable_selection), + C::new("edit.setValue", "Edit Attribute", "Attribute Table", set_value).roles(&[]).effects(crate::policy::Effects::NONE) .when(super::design::need_editable) .params(r#"{"layer"?, "row": number, "field": string, "value": any}"#), - C::new("edit.addField", "Add Field", "Data › Data Design › Fields", add_field).roles(&[]) + C::new("edit.addField", "Add Field", "Data › Data Design › Fields", add_field).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_layer) .params(r#"{"layer"?, "name": string, "type": "long"|"double"|"text"|"boolean"}"#), - C::new("edit.deleteField", "Delete Field", "Data › Data Design › Fields", delete_field).roles(&[]) + C::new("edit.deleteField", "Delete Field", "Data › Data Design › Fields", delete_field).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_layer) .params(r#"{"layer"?, "field": string}"#), - C::new("edit.calculateField", "Calculate Field", "Attribute Table", calculate_field).roles(&[]) + C::new("edit.calculateField", "Calculate Field", "Attribute Table", calculate_field).roles(&[]).effects(crate::policy::Effects::NONE) .when(super::design::need_editable) .params(r#"{"layer"?, "field": string, "expression": "an expression: fields, 'text', numbers, + - * / % ^ ||, functions (see the Expressions page)"}"#), ] diff --git a/crates/engine/src/cmd/effects.rs b/crates/engine/src/cmd/effects.rs index 7820f1a12b..a364ef9c6b 100644 --- a/crates/engine/src/cmd/effects.rs +++ b/crates/engine/src/cmd/effects.rs @@ -34,29 +34,17 @@ pub fn commands() -> Vec { with(s, p, |l| l.blend = mode) }) .roles(&[]) + .effects(crate::policy::Effects::NONE) .when(need_layer) .params(r#"{"layer"?, "mode": "normal"|"multiply"|"screen"|"overlay"|"darken"|"lighten"|"difference"}"#), C::new("layer.mask", "Masking", "Style › Drawing", |s, p| { let id = s.layer_id(p)?; - let mask = match p.get("mask") { - None | Some(Value::Null) => None, - Some(Value::String(n)) if n.trim().is_empty() => None, - Some(Value::String(n)) => { - let m = s - .map() - .layer_by_name(n) - .filter(|l| l.geometry_type() == GeometryType::Polygon) - .ok_or_else(|| format!("no polygon layer '{n}'"))?; - if m.id == id { - return Err("a layer can't mask itself".into()); - } - Some(m.name.clone()) - } - Some(_) => return Err("mask is a polygon layer's name, or null".into()), - }; + let mask = mask_of(s, p, id)?; with(s, p, |l| l.mask = mask.clone()) }) + .inputs(mask_inputs) .roles(&[]) + .effects(crate::policy::Effects::NONE) .when(need_layer) .params(r#"{"layer"?, "mask": polygon layer|null}"#), C::new("layer.swipe", "Swipe", "Style › Compare", |s, p| { @@ -73,6 +61,7 @@ pub fn commands() -> Vec { Ok(json!({"swipe": s.map().swipe.map(|(l, x)| json!({"layer": l, "position": x}))})) }) .roles(&[]) + .effects(crate::policy::Effects::NONE) .when(need_layer) .params(r#"{"layer"?, "position"?: 0..1, "off"?: bool}"#), C::new("raster.rotation", "Rotation Type", "Style › Rotation", |s, p| { @@ -84,10 +73,12 @@ pub fn commands() -> Vec { Ok(json!({"angle": rl.style.rotation})) }) .roles(&[]) + .effects(crate::policy::Effects::NONE) .when(need_raster) .params(r#"{"layer"?, "angle": degrees clockwise}"#), C::new("layer.displayFilters", "Display Filters", "Style › Drawing", display_filters) .roles(&[]) + .effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "filters": [{"name", "minScale"?, "maxScale"?, "where"}] (empty to clear)}"#), C::new("layer.aggregation", "Aggregation", "Style › Drawing", |s, p| { @@ -105,11 +96,16 @@ pub fn commands() -> Vec { with(s, p, |l| l.aggregation = agg) }) .roles(&[]) + .effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "shape": "hexagon"|"square"|"none", "size"?: points}"#), - C::new("layer.extrusion", "Layer Extrusion", "Style › Extrusion", extrusion).roles(&[]).when(need_polygons).params( - r#"{"layer"?, "type": "none"|"height"|"field", "field"?: numeric field (metres), "height"?: metres, "factor"?: number}"#, - ), + C::new("layer.extrusion", "Layer Extrusion", "Style › Extrusion", extrusion) + .roles(&[]) + .effects(crate::policy::Effects::NONE) + .when(need_polygons) + .params( + r#"{"layer"?, "type": "none"|"height"|"field", "field"?: numeric field (metres), "height"?: metres, "factor"?: number}"#, + ), C::new("layer.faces", "Faces", "Style › Faces", |s, p| { let (cull, light) = (b(p, "culling"), b(p, "lighting")); change_extrusion(s, p, |x| { @@ -122,6 +118,7 @@ pub fn commands() -> Vec { }) }) .roles(&[]) + .effects(crate::policy::Effects::NONE) .when(need_polygons) .params(r#"{"layer"?, "culling"?: bool, "lighting"?: bool}"#), C::new("layer.lightShadow", "Light and Shadow", "Style › Light and Shadow", |s, p| { @@ -136,6 +133,7 @@ pub fn commands() -> Vec { }) }) .roles(&[]) + .effects(crate::policy::Effects::NONE) .when(need_polygons) .params(r#"{"layer"?, "lighting"?: bool, "shadows"?: bool}"#), ] @@ -209,3 +207,45 @@ fn change_extrusion(s: &mut Session, p: &Value, f: impl FnOnce(&mut Extrusion)) f(x); Ok(Value::Null) } + +/// Binds the primary target and literal mask, retaining names resolved by earlier planned steps. +fn mask_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let mut inputs = super::Inputs::single_layer(input, p)?; + if let Some(name) = mask_name(p)? { + // Preserve known live validation; future names are bound by the planner and checked + // against the actual polygon and target when the handler runs. + if input.map().layer_by_name(name).is_some() { + mask_layer(input.map(), name, input.layer_id(p).ok())?; + } + inputs.extend(super::Inputs::named(input.map(), [name])?); + } + Ok(inputs) +} + +/// Parses the optional literal name exactly as the mask handler consumes it. +fn mask_name(p: &Value) -> Result, String> { + match p.get("mask") { + None | Some(Value::Null) => Ok(None), + Some(Value::String(n)) if n.trim().is_empty() => Ok(None), + Some(Value::String(n)) => Ok(Some(n)), + Some(_) => Err("mask is a polygon layer's name, or null".into()), + } +} + +/// Validates a live polygon mask, including the existing self-mask refusal. +fn mask_layer<'a>(map: &'a rustgis_map::Map, name: &str, id: Option) -> Result<&'a rustgis_map::Layer, String> { + let mask = map + .layer_by_name(name) + .filter(|l| l.geometry_type() == GeometryType::Polygon) + .ok_or_else(|| format!("no polygon layer '{name}'"))?; + if Some(mask.id) == id { + return Err("a layer can't mask itself".into()); + } + Ok(mask) +} + +/// Resolves and validates the literal mask against the actual execution map. +fn mask_of(input: &impl super::InputSource, p: &Value, id: u32) -> Result, String> { + let context = input.input_context(); + mask_name(p)?.map(|name| mask_layer(context.map(), name, Some(id)).map(|mask| mask.name.clone())).transpose() +} diff --git a/crates/engine/src/cmd/embeddings.rs b/crates/engine/src/cmd/embeddings.rs index edc0784234..1114f4f2e8 100644 --- a/crates/engine/src/cmd/embeddings.rs +++ b/crates/engine/src/cmd/embeddings.rs @@ -18,13 +18,13 @@ pub fn commands() -> Vec { vec![ C::new("embeddings.datasets", "Satellite Embedding Datasets", "Home › Add › Web Data", |_, _| { Ok(json!({"datasets": emb::DATASETS.iter().map(|d| d.to_json()).collect::>()})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), // Not a view: it adds the footprints layer (by default). - C::new("embeddings.search", "Search Satellite Embeddings", "Home › Add › Web Data", search).roles(&[]).params( + C::new("embeddings.search", "Search Satellite Embeddings", "Home › Add › Web Data", search).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"dataset": "alphaearth" | "earth-index" | "tessera", "year"?: number (AlphaEarth and Tessera: 2017–2025, default 2024), "bbox"?: [west, south, east, north] | "view" (the map's extent, the default), "footprints"?: bool (adds the tiles' footprints as a layer, default true)}"#, ), - C::new("embeddings.add", "Add Satellite Embeddings", "Home › Add › Web Data", add).roles(&[]).params( + C::new("embeddings.add", "Add Satellite Embeddings", "Home › Add › Web Data", add).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"dataset": "alphaearth" | "earth-index" | "tessera", "url"?: "AlphaEarth's tile or Earth Index's file, as embeddings.search lists it (Earth Index: default the MGRS tile at the view's centre)", "year"?: number (Tessera, default 2024), "bands"?: [red, green, blue] (bands from 1; AlphaEarth default [2, 17, 10], Tessera [1, 2, 3]), "stretch"?: number (AlphaEarth: dequantised values shown over ±stretch, default 0.3), "dimension"?: number (Earth Index: a vector value kept as each point's `value`, from 1), "bbox"?: [west, south, east, north] | "view" (Earth Index and Tessera: the area read, default the map's extent), "name"?: text, "ran"?: (internal: what the app read)}"#, ), ] diff --git a/crates/engine/src/cmd/exports.rs b/crates/engine/src/cmd/exports.rs index 52f3e8365e..6122412017 100644 --- a/crates/engine/src/cmd/exports.rs +++ b/crates/engine/src/cmd/exports.rs @@ -2,9 +2,9 @@ //! pixels, a copy of the map or project to draw). A script or the command line writes it where //! the command is called; the app writes it on a thread of its own with progress and Cancel //! (#1291 step 3): [`export_start`] reads the session, [`ExportRun::run`] writes the file, and -//! [`export_land`] says what was written, as the command would have. It lands whatever layer is -//! active or project is open by then: the file is written either way, and an export changes -//! nothing in the project. +//! [`export_land`] says what was written, as the command would have. The admitted operation owns +//! preparation, file commit and delivery; a completed write remains written even if delivery +//! is subsequently refused. use serde_json::Value; @@ -15,9 +15,14 @@ use crate::{CmdResult, Session}; pub struct Wrote { pub(crate) answer: Value, pub(crate) notes: Vec, + /// A physical write completed, even if authority to deliver its result is later refused. + pub(crate) written_path: Option, /// What it does to the session once written, after its notes, and adds to its answer (a /// GeoPackage table replaced: the layers reading it read it again). pub(crate) then: Option, + /// Authority retained for delivery from the native export worker. + #[cfg(not(target_arch = "wasm32"))] + pub(crate) owner: Option, } /// What an export does to the session once its file is written, and adds to its answer; or the @@ -48,12 +53,28 @@ pub fn runs_in_background(command: &str) -> Option<(&'static str, &'static str)> /// `command`'s export written here: it says what it wrote. pub(crate) fn write_here(s: &mut Session, p: &Value, command: &str) -> CmdResult { let &(_, name, start) = IN_BACKGROUND.iter().find(|(c, ..)| *c == command).ok_or_else(|| format!("{command} isn't an export"))?; - let work = start(s, p)?; - // Where a person at a terminal can stop it with Ctrl-C, as on the app's thread. - let wrote = if s.stoppable { written_under(name, &rustgis_analysis::control::Control::stoppable(), work)? } else { work()? }; + let owner = s.continuation()?; + owner.check()?; + let current = owner.clone(); + let still = std::sync::Arc::new(move || current.check().is_ok()); + // The probe keeps authority checks at file commit even when the user cannot cancel this run. + let control = if s.stoppable { rustgis_analysis::control::Control::stoppable() } else { rustgis_analysis::control::Control::new() }; + let wrote = crate::render::rustgis_tiles::cache::with_still(still, || { + let work = start(s, p)?; + written_under(name, &control, work) + })?; + s.check_continuation(&owner).map_err(|why| completion_refused(wrote.written_path.as_deref(), why))?; said(s, wrote) } +/// Keeps a completed file visible when its result cannot be delivered to the session. +pub(crate) fn completion_refused(written_path: Option<&str>, why: String) -> String { + match written_path { + Some(path) => format!("Exported {path}; completion was refused: {why}"), + None => why, + } +} + /// `work` under `control`, as [`rustgis_analysis::run_under`] runs it, except that a file once /// placed is written: a Cancel that comes after the last step doesn't turn it into a failure. fn written_under(name: &str, control: &std::sync::Arc, work: Work) -> Result { @@ -67,7 +88,7 @@ fn written_under(name: &str, control: &std::sync::Arc CmdResult { - let Wrote { mut answer, notes, then } = wrote; + let Wrote { mut answer, notes, then, .. } = wrote; for note in notes { s.notify(note, false); } @@ -101,6 +122,7 @@ pub(crate) fn drawn( Some((l.instance_id(), l.name.clone(), src.error.clone()?, src.clone())) }) .collect(); + let written_path = written.is_ok().then(|| path.clone()); let answer = written.as_ref().map_or(Value::Null, Value::clone); let then: Then = Box::new(move |s: &mut Session, _: &mut Value| { use rustgis_data::diagnostics::Severity; @@ -135,7 +157,14 @@ pub(crate) fn drawn( s.notify(format!("Exported {path}"), false); Ok(()) }); - Ok(Wrote { answer, notes: Vec::new(), then: Some(then) }) + Ok(Wrote { + answer, + notes: Vec::new(), + written_path, + then: Some(then), + #[cfg(not(target_arch = "wasm32"))] + owner: None, + }) } #[cfg(not(target_arch = "wasm32"))] @@ -159,6 +188,8 @@ pub struct Started { pub(crate) params: Value, pub(crate) at: u64, pub(crate) start: web_time::Instant, + /// The admitted invocation; cloned metadata never selects replacement authority. + pub(crate) owner: crate::policy::Continuation, } #[cfg(not(target_arch = "wasm32"))] @@ -172,10 +203,17 @@ impl ExportRun { /// (see [`rustgis_analysis::control`]). A cancelled export leaves no file, or the one there /// before. pub fn run(self, control: &std::sync::Arc) -> Result { - let ExportRun { name, work, .. } = self; + let ExportRun { name, work, started } = self; + let owner = started.owner; + owner.check()?; + let current = owner.clone(); + let still = std::sync::Arc::new(move || current.check().is_ok()); // A bug in a writer must not take the app down. - std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| written_under(name, control, work))) - .unwrap_or_else(|_| Err(format!("{name} failed unexpectedly"))) + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + crate::render::rustgis_tiles::cache::with_still(still, || written_under(name, control, work)) + })) + .unwrap_or_else(|_| Err(format!("{name} failed unexpectedly"))) + .map(|w| Wrote { owner: Some(owner), ..w }) } } @@ -183,10 +221,23 @@ impl ExportRun { /// Starts `command` (one of [`IN_BACKGROUND`]'s) with `p`, to write its file on another thread: /// its parameters checked and its inputs taken now. pub fn export_start(s: &mut Session, command: &str, p: &Value) -> Result { - let &(command, name, start) = + s.start_export(command, p) +} + +#[cfg(not(target_arch = "wasm32"))] +/// Prepares only a registered, admitted export and seals its initiating authority. +pub(crate) fn export_start_raw(s: &mut Session, op: crate::policy::Authorized) -> Result { + let crate::policy::CommandOp::Registered { spec, params } = op.op() else { return Err("not a registered export".into()) }; + let (command, p) = (spec.id, params); + let &(_, name, start) = IN_BACKGROUND.iter().find(|(c, ..)| *c == command).ok_or_else(|| format!("{command} doesn't run in the background"))?; - let started = Started { command, params: p.clone(), at: crate::project_log::now_ms(), start: web_time::Instant::now() }; - Ok(ExportRun { name, started, work: start(s, p)? }) + let owner = s.continuation()?; + owner.check()?; + let current = owner.clone(); + let still = std::sync::Arc::new(move || current.check().is_ok()); + let started = Started { command, params: p.clone(), at: crate::project_log::now_ms(), start: web_time::Instant::now(), owner }; + let work = crate::render::rustgis_tiles::cache::with_still(still, || start(s, p))?; + Ok(ExportRun { name, started, work }) } #[cfg(not(target_arch = "wasm32"))] @@ -194,10 +245,20 @@ pub fn export_start(s: &mut Session, command: &str, p: &Value) -> Result) -> CmdResult { - let Started { command, params, at, start } = started; + let Started { command, params, at, start, owner } = started; let before = s.diagnostic_source.replace(command); let mark = s.notice_mark(); - let landed = done.and_then(|wrote| said(s, wrote)); + let landed: CmdResult = (|| { + if let Ok(wrote) = &done { + let completed = wrote.owner.as_ref().ok_or("the export has no initiating authority")?; + if !owner.same_run(completed) { + return Err("this export result belongs to another run".into()); + } + } + let written_path = done.as_ref().ok().and_then(|wrote| wrote.written_path.clone()); + s.finish_export(&owner, |s| done.and_then(|wrote| said(s, wrote))) + .map_err(|why| completion_refused(written_path.as_deref(), why))? + })(); // Its failure said once, under the command, as `Session::run` says one. if let Err(e) = &landed { let text = Session::redacted(e); @@ -226,7 +287,14 @@ mod tests { // The work places its file, then Cancel comes before the run returns. let work: Work = Box::new(move || { late.cancel(); - Ok(Wrote { answer: serde_json::json!({"path": "x"}), notes: vec!["Exported x".into()], then: None }) + Ok(Wrote { + answer: serde_json::json!({"path": "x"}), + notes: vec!["Exported x".into()], + written_path: Some("x".into()), + then: None, + #[cfg(not(target_arch = "wasm32"))] + owner: None, + }) }); let wrote = written_under("Export Raster", &control, work).unwrap(); assert_eq!(wrote.notes, ["Exported x"]); diff --git a/crates/engine/src/cmd/geocode.rs b/crates/engine/src/cmd/geocode.rs index 1efb6a15b1..466cf15a7c 100644 --- a/crates/engine/src/cmd/geocode.rs +++ b/crates/engine/src/cmd/geocode.rs @@ -10,10 +10,10 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("map.findPlace", "By Place or Address…", "Home › Navigate › Locate", find_place).roles(&[]).params( + C::new("map.findPlace", "By Place or Address…", "Home › Navigate › Locate", find_place).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"query": "a place or an address", "provider"?: "nominatim" (OpenStreetMap, default) | "photon" | "locatieserver" (PDOK, the Netherlands' addresses and places), "url"?: "a geocoding server of your own", "limit"?: 1–20, "go"?: bool (zooms to the first, default true)}"#, ), - C::new("layer.geocode", "Geocode Addresses", "Home › Add › Add Data", geocode).roles(&[]).params( + C::new("layer.geocode", "Geocode Addresses", "Home › Add › Add Data", geocode).inputs(source_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"table"?: name|id (a standalone table, such as a CSV of addresses), "layer"?: name|id (or the active layer), "address": field | [fields] (joined with commas, such as street, city, country), "provider"?: "nominatim" | "photon" | "locatieserver" (PDOK, Dutch addresses; up to 10,000 a run), "url"?: "a geocoding server of your own (more than 100 addresses with nominatim or photon)", "name"?: text, "places"?: "answers already looked up, {address: {lon, lat, name} | null} (the app's background run; nothing is fetched)"}"#, ), ] @@ -66,7 +66,9 @@ fn find_place(s: &mut Session, p: &Value) -> CmdResult { /// The table or layer whose addresses are geocoded: `table` names a standalone table; `layer` a /// layer, or a table when no layer has its name. -fn source<'a>(s: &'a Session, p: &Value) -> Result<&'a rustgis_data::Dataset, String> { +fn source<'a>(input: &'a impl super::InputSource, p: &Value) -> Result<&'a rustgis_data::Dataset, String> { + let context = input.input_context(); + let s = &context; let table = |n: &str| s.project.tables.iter().find(|t| t.name == n).map(|t| &t.dataset); if p.get("table").is_some_and(|t| !t.is_null()) { let i = s.table_index(p)?; @@ -299,3 +301,12 @@ fn lookup(g: &Geocoder, distinct: &[String]) -> Result { fn lookup(_: &Geocoder, _: &[String]) -> Result { Err("in the web app, geocode addresses with Home › Add › Add Data › Geocode Addresses".into()) } + +/// Reuses the executor's actual layer/table choice, including its exact-case table fallback. +fn source_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let dataset = source(input, p)?; + let mut inputs = super::Inputs::layers(s.map().layers.iter().filter(|l| std::ptr::eq(&l.dataset, dataset)).map(|l| (s.map(), l))); + inputs.tables.extend(s.project.tables.iter().filter(|t| std::ptr::eq(&t.dataset, dataset)).map(|t| t.id)); + Ok(inputs) +} diff --git a/crates/engine/src/cmd/geolibre.rs b/crates/engine/src/cmd/geolibre.rs index e307b6af24..20a680e2ef 100644 --- a/crates/engine/src/cmd/geolibre.rs +++ b/crates/engine/src/cmd/geolibre.rs @@ -6,7 +6,7 @@ use serde_json::{Value, json}; /// The importer reads files and URLs named inside the project, so callers require approval. pub fn commands() -> Vec { - vec![C::new("project.importGeolibre", "Import GeoLibre Project", "Project › Import", import_project).roles(&[]).params( + vec![C::new("project.importGeolibre", "Import GeoLibre Project", "Project › Import", import_project).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Files)).params( r#"{"path"?: "*.geolibre|*.geolibre.json", "url"?: "https://…", "text"?: "project JSON", "data"?: "base64 JSON", "dir"?: "folder for local data"} → {map, layers, notes}"#, )] } @@ -64,13 +64,14 @@ fn import_project(s: &mut Session, p: &Value) -> CmdResult { } q["url"] = json!(geolibre::download_url(url)); } - return s.request_url(&q); + return s.request_import_url(&q); } let text = project_text(p)?; let mut report = Report::default(); let project = geolibre::read_project(&text, &mut report)?; - #[cfg(target_arch = "wasm32")] - ensure_download_capacity(s, &project.layers)?; + if cfg!(target_arch = "wasm32") || s.leave_downloads { + ensure_download_capacity(s, &project.layers, cfg!(target_arch = "wasm32"))?; + } let dir = ps(p, "dir").filter(|d| !d.trim().is_empty()).map(str::to_string).or_else(|| { ps(p, "path") .and_then(|p| std::path::Path::new(p).parent()) @@ -208,29 +209,38 @@ fn source_address(r: &Value) -> &str { } } -/// Refuses a browser import before creating its map when its whole-file requests cannot all -/// be queued. Streaming tiles/COGs and inline features consume no whole-file request slots. -#[cfg(any(target_arch = "wasm32", test))] -fn ensure_download_capacity(s: &Session, layers: &[Value]) -> Result<(), String> { - let count = layers - .iter() - .filter(|r| { - let kind = string(r, "type"); - let address = source_address(r); - rustgis_map::tiles::is_web(address) - && !(kind == "geojson" && inline_data(r).is_some()) - && (matches!(kind, "geojson" | "flatgeobuf" | "geoparquet" | "zarr" | "lidar" | "mbtiles") - || (kind == "raster" && array(&r["source"], "tiles").is_empty() && !super::layer::is_tiff_url(address))) - }) - .count(); +/// The loader's whole-file queue choice; native MBTiles is synchronous and browser TIFFs stream. +fn needs_deferred_download(r: &Value, browser: bool) -> bool { + let kind = string(r, "type"); + if string(r.get("metadata").unwrap_or(&Value::Null), "sourceKind") == "sql-query" + || (kind == "geojson" && inline_data(r).is_some()) + || !rustgis_map::tiles::is_web(source_address(r)) + { + return false; + } + match kind { + "geojson" | "flatgeobuf" | "geoparquet" | "zarr" | "lidar" => true, + "mbtiles" => browser, + "raster" => { + array(r.get("source").unwrap_or(&Value::Null), "tiles").is_empty() + && (!browser || !super::layer::is_tiff_url(source_address(r))) + } + _ => false, + } +} + +/// Refuses a deferred import before creating its map when whole-file requests cannot all fit. +/// Streaming tiles/COGs and inline features consume no whole-file request slots. +fn ensure_download_capacity(s: &Session, layers: &[Value], browser: bool) -> Result<(), String> { + let count = layers.iter().filter(|r| needs_deferred_download(r, browser)).count(); // Once the UI drains url_requests, each Zarr reader (including a queued reader) // owns a session download id. Count all stages so a second import sees those slots. let occupied = - s.url_requests.len().saturating_add(s.downloading.len()).saturating_add(s.downloaded.len()).saturating_add(s.landed_reads.len()); + s.queued_url_count().saturating_add(s.downloading.len()).saturating_add(s.downloaded.len()).saturating_add(s.landed_reads.len()); let available = crate::MAX_URL_REQUESTS.saturating_sub(occupied); if count > available { return Err(format!( - "GeoLibre import needs {count} downloads, but only {available} of {} browser download slots are available; split the project or wait for pending downloads before importing", + "GeoLibre import needs {count} downloads, but only {available} of {} download slots are available; split the project or wait for pending downloads before importing", crate::MAX_URL_REQUESTS )); } @@ -295,7 +305,7 @@ fn add_layer(s: &mut Session, r: &Value, dir: Option<&str>, report: &mut Report, let mut saved = r.clone(); saved["visible"] = json!(boolean(r, "visible", true) && folder.0); saved["opacity"] = json!(number(r, "opacity", 1.0).clamp(0.0, 1.0) * folder.1); - s.request_url(&json!({"url":address,"whole":true,"name":name,"geolibreLayer":{"token":token,"record":saved}}))?; + s.request_import_url(&json!({"url":address,"whole":true,"name":name,"geolibreLayer":{"token":token,"record":saved}}))?; report.approximated(name, "MBTiles archive is downloading; wait before saving"); } report.approximated(name, "tile overlays draw above feature layers; tile vector styling uses RustGIS defaults"); @@ -344,8 +354,8 @@ fn add_layer(s: &mut Session, r: &Value, dir: Option<&str>, report: &mut Report, // The project's saved camera is restored after the import, so each added layer keeps the view. let mut q = json!({"name":name,"zoom":false}); q[if remote { "url" } else { "path" }] = json!(address); - #[cfg(target_arch = "wasm32")] - if remote && kind != "cog" && !(kind == "raster" && super::layer::is_tiff_url(&address)) { + let defer = cfg!(target_arch = "wasm32") || s.leave_downloads; + if defer && needs_deferred_download(r, cfg!(target_arch = "wasm32")) { // Reserve the final position and group membership before downloads can finish in // arbitrary order. The token follows the layer across map switches and reordering. let token = pending_token(); @@ -367,7 +377,7 @@ fn add_layer(s: &mut Session, r: &Value, dir: Option<&str>, report: &mut Report, } } q["geolibreLayer"] = json!({"token":token,"record":r}); - s.request_url(&q)?; + s.request_import_url(&q)?; report.approximated(name, "URL data is downloading; its saved style is applied when the download completes"); return Ok(Added::Feature(id)); } @@ -577,19 +587,61 @@ fn group_look(groups: &std::collections::HashMap<&str, &Value>, id: &str) -> (bo mod admission_tests { use super::*; + /// Taken opaque requests still occupy capacity; native deferred import must refuse atomically. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn geolibre_native_deferred_import_checks_taken_request_capacity() { + let mut s = Session::new(crate::project::Project::default()); + s.request_import_url(&json!({"url":"https://capacity.invalid/held.geojson"})).unwrap(); + let held = s.take_url_requests().pop().unwrap(); + let before = serde_json::to_value(&s.project).unwrap(); + let layers: Vec<_> = (0..crate::MAX_URL_REQUESTS) + .map(|i| json!({"id":i.to_string(),"name":format!("Layer {i}"),"type":"geojson","source":{"url":format!("https://capacity.invalid/{i}.geojson")}})) + .collect(); + let text = json!({"version":"0.2.0","name":"Capacity","mapView":{},"basemapStyleUrl":"","layers":layers}).to_string(); + let result = s.run_leaving_downloads("project.importGeolibre", &json!({"text":text})); + assert!(result.is_err(), "an oversized native deferred import returned a partial map"); + assert!(result.unwrap_err().contains("15"), "the held opaque request must occupy its slot"); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before); + assert!(s.take_url_requests().is_empty()); + let claim = s.download_started_for(&held).unwrap(); + s.download_failed(claim); + s.request_import_url(&json!({"url":"https://capacity.invalid/abandoned.geojson"})).unwrap(); + let abandoned = s.take_url_requests().pop().unwrap(); + assert!(ensure_download_capacity(&s, &layers, false).is_err()); + drop(abandoned); + assert!(ensure_download_capacity(&s, &layers, false).is_ok(), "a dropped opaque handle must release its slot"); + } + + /// A generic raster TIFF deferred by the native loader keeps its import reservation and style. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn geolibre_native_deferred_raster_tiff_keeps_its_reservation() { + let mut s = Session::new(crate::project::Project::default()); + let record = json!({"id":"r","name":"Raster","type":"raster","source":{"url":"https://capacity.invalid/image.tif"}}); + let text = json!({"version":"0.2.0","name":"Raster","mapView":{},"basemapStyleUrl":"","layers":[record]}).to_string(); + let out = s.run_leaving_downloads("project.importGeolibre", &json!({"text":text})).unwrap(); + assert_eq!(out["layers"], json!(["Raster"])); + let layer = s.map().layer_by_name("Raster").unwrap(); + assert!(layer.dataset.source.as_deref().is_some_and(|s| s.starts_with("geolibre-pending:"))); + let requests = s.take_url_requests(); + assert_eq!(requests.len(), 1); + assert!(requests[0].params().get("geolibreLayer").is_some()); + } + #[test] fn geolibre_browser_import_refuses_an_oversized_download_batch_before_mutation() { let mut s = Session::new(crate::project::Project::default()); let layers: Vec<_> = (0..17).map(|i| json!({"type":"geojson","source":{"url":format!("https://example.org/{i}.geojson")}})).collect(); let before = s.project.clone(); - assert!(ensure_download_capacity(&s, &layers).unwrap_err().contains("needs 17 downloads")); + assert!(ensure_download_capacity(&s, &layers, true).unwrap_err().contains("needs 17 downloads")); assert_eq!(s.project.maps.len(), before.maps.len()); assert!(s.url_requests.is_empty()); - assert!(ensure_download_capacity(&s, &layers[..16]).is_ok()); + assert!(ensure_download_capacity(&s, &layers[..16], true).is_ok()); s.request_url(&json!({"url":"https://example.org/other.geojson"})).unwrap(); - assert!(ensure_download_capacity(&s, &layers[..16]).is_err()); - assert!(ensure_download_capacity(&s, &layers[..15]).is_ok()); + assert!(ensure_download_capacity(&s, &layers[..16], true).is_err()); + assert!(ensure_download_capacity(&s, &layers[..15], true).is_ok()); } #[test] @@ -605,11 +657,20 @@ mod admission_tests { json!({"type":"raster","source":{"tiles":["https://example.org/{z}/{x}/{y}.png"]}}), json!({"type":"pmtiles","source":{"url":"https://example.org/archive.pmtiles"}}), json!({"type":"geojson","sourcePath":"local.geojson"}), + json!({"type":"geojson","metadata":{"sourceKind":"sql-query"},"source":{"url":"https://example.org/query"}}), ]; - assert!(ensure_download_capacity(&s, &layers).is_ok()); + assert!(ensure_download_capacity(&s, &layers, true).is_ok()); for kind in ["flatgeobuf", "geoparquet", "zarr", "lidar", "mbtiles", "raster"] { - assert!(ensure_download_capacity(&s, &[json!({"type":kind,"source":{"url":"https://example.org/data"}})]).is_err(), "{kind}"); + assert!( + ensure_download_capacity(&s, &[json!({"type":kind,"source":{"url":"https://example.org/data"}})], true).is_err(), + "{kind}" + ); } + assert!( + ensure_download_capacity(&s, &[json!({"type":"mbtiles","source":{"url":"https://example.org/archive.mbtiles"}})], false) + .is_ok() + ); + assert!(ensure_download_capacity(&s, &layers[2..3], false).is_err(), "native deferred TIFFs occupy a whole-file slot"); } /// A long ancestry and a cycle exercise indexed lookup without recursion or repeated scans. @@ -632,16 +693,16 @@ mod admission_tests { let before = s.project.maps.len(); let layers: Vec<_> = (0..10).map(|i| json!({"type":"zarr","source":{"url":format!("https://example.org/{i}.zarr")}})).collect(); for i in 0..10 { - s.request_url(&json!({"url":format!("https://example.org/{i}.zarr"),"geolibreZarr":true})).unwrap(); + s.request_import_url(&json!({"url":format!("https://example.org/{i}.zarr"),"geolibreZarr":true})).unwrap(); } let requests = s.take_url_requests(); - let ids: Vec<_> = requests.iter().map(|_| s.download_started()).collect(); - assert!(ensure_download_capacity(&s, &layers).is_err()); - assert!(ensure_download_capacity(&s, &layers[..6]).is_ok()); + let ids: Vec<_> = requests.iter().map(|request| s.download_started_for(request).unwrap()).collect(); + assert!(ensure_download_capacity(&s, &layers, true).is_err()); + assert!(ensure_download_capacity(&s, &layers[..6], true).is_ok()); // Landed bytes still await their completion callback and retain the slot. assert!(s.download_landed(ids[0], Vec::new())); - assert!(ensure_download_capacity(&s, &layers).is_err()); - assert!(ensure_download_capacity(&s, &layers[..6]).is_ok()); + assert!(ensure_download_capacity(&s, &layers, true).is_err()); + assert!(ensure_download_capacity(&s, &layers[..6], true).is_ok()); assert_eq!(s.project.maps.len(), before); assert!(s.url_requests.is_empty()); } diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 818ed2d26a..79302d1a08 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -10,51 +10,51 @@ use crate::{CmdResult, CommandSpec as C, GpRecord, Session}; pub fn commands() -> Vec { vec![ - C::new("gp.run", "Run Tool", "Analyze › Geoprocessing", run).roles(&[crate::ParamRole { key: "params", role: crate::Role::ToolParams }]).when(need_layers).params(r#"{"tool": tool id, "params": {...} (a layer by name; a standalone table by name or id)}"#), - C::new("gp.spatialJoin", "Spatial Join", "Analyze › Overlay", spatial_join).roles(&[]) + C::new("gp.run", "Run Tool", "Analyze › Geoprocessing", run).roles(&[crate::ParamRole { key: "params", role: crate::Role::ToolParams }]).effects(crate::policy::Effects::NONE).when(need_layers).params(r#"{"tool": tool id, "params": {...} (a layer by name; a standalone table by name or id)}"#), + C::new("gp.spatialJoin", "Spatial Join", "Analyze › Overlay", spatial_join).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_layers) .params( r#"{"target": layer, "join": layer, "match"?: "Intersects" (default) | "Contains" | "Within", "operation"?: "Join one to one" (default) | "Join one to many", "keepAll"?: "Keep all" (default) | "Keep only matching", "prefix"?: "join_", "output"?: text}"#, ), - C::new("gp.zonalStats", "Zonal Statistics", "Analyze › Raster", zonal_stats).roles(&[]) + C::new("gp.zonalStats", "Zonal Statistics", "Analyze › Raster", zonal_stats).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_layers) .params( r#"{"raster": raster, "polygons": polygon layer, "bands"?: [1-based band numbers] or single band (default 1), "stats"?: ["mean", "min", "max", "sum", "count", "stddev"] or "all" (default "all"), "prefix"?: text, "output"?: text}"#, ), - C::new("gp.bufferPoint", "Buffer A Point", "Analyze › Geoprocessing", buffer_point).roles(&[]).params( + C::new("gp.bufferPoint", "Buffer A Point", "Analyze › Geoprocessing", buffer_point).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"lon": number, "lat": number, "distance": number, "units"?: "Kilometers" (default) | "Meters" | "Miles" | "Feet", "name"?: output}"#, ), C::new("gp.tools", "Tools", "Analyze › Geoprocessing", |_, _| { Ok(Value::Array( - rustgis_analysis::tools() + rustgis_analysis::published_tools() .map(|t| { json!({"id": t.id, "name": t.name, "toolbox": t.toolbox, "summary": t.summary, "params": t.params.iter().map(|p| json!({"name": p.name, "label": p.label, "required": p.required, "kind": format!("{:?}", p.kind)})).collect::>()}) }) .collect(), )) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("gp.geolibre", "GeoLibre Tools", "Analyze › Geoprocessing", |_, _| Ok(geolibre_status())).roles(&[]).view(), + C::new("gp.geolibre", "GeoLibre Tools", "Analyze › Geoprocessing", |_, _| Ok(geolibre_status())).roles(&[]).effects(crate::policy::Effects::NONE).view(), C::new("gp.installGeolibre", "Install GeoLibre Tools", "Analyze › Geoprocessing", |_, _| { let path = install_geolibre(&|_, _| {})?; #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] rustgis_geolibre::set_runner(Some(std::path::PathBuf::from(&path)))?; Ok(json!({"path": path, "status": geolibre_status()})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)) .view(), - C::new("gp.uninstallGeolibre", "Uninstall GeoLibre Tools", "Analyze › Geoprocessing", |_, _| uninstall_geolibre()).roles(&[]).view(), - C::new("gp.finish", "Finish Tool Run", "Analyze › Geoprocessing", finish).roles(&[]) + C::new("gp.uninstallGeolibre", "Uninstall GeoLibre Tools", "Analyze › Geoprocessing", |_, _| uninstall_geolibre()).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view(), + C::new("gp.finish", "Finish Tool Run", "Analyze › Geoprocessing", finish).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"id": a finished run (the browser's GeoLibre tools run asynchronously)}"#), - C::new("gp.fileDelivered", "Finish File Export", "Analyze › Geoprocessing", file_delivered).roles(&[]) + C::new("gp.fileDelivered", "Finish File Export", "Analyze › Geoprocessing", file_delivered).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"id": pending file id, "error"?: download failure message}"#), - C::new("gp.setGeolibre", "Locate GeoLibre Runner", "Analyze › Geoprocessing", set_geolibre).roles(&[]) + C::new("gp.setGeolibre", "Locate GeoLibre Runner", "Analyze › Geoprocessing", set_geolibre).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Files)) .view() .params(r#"{"path"?: "the geolibre executable (empty: look in the usual places)"}"#), C::new("gp.clearHistory", "Clear History", "Analyze › Geoprocessing › History", |s, _| { s.project.gp_history.clear(); Ok(Value::Null) - }).roles(&[]), + }).roles(&[]).effects(crate::policy::Effects::NONE), ] } @@ -62,7 +62,7 @@ pub fn commands() -> Vec { pub fn geolibre_tools() -> &'static [rustgis_analysis::ToolSpec] { #[cfg(feature = "geolibre")] { - rustgis_analysis::external_tools_of(rustgis_geolibre::SOURCE) + rustgis_analysis::published_external_tools_of(rustgis_geolibre::SOURCE) } #[cfg(not(feature = "geolibre"))] { @@ -74,7 +74,7 @@ pub fn geolibre_tools() -> &'static [rustgis_analysis::ToolSpec] { pub fn is_geolibre_tool(spec: &rustgis_analysis::ToolSpec) -> bool { #[cfg(feature = "geolibre")] { - rustgis_analysis::external_source(spec) == Some(rustgis_geolibre::SOURCE) + rustgis_analysis::published_external_tools_of(rustgis_geolibre::SOURCE).iter().any(|t| std::ptr::eq(t, spec)) } #[cfg(not(feature = "geolibre"))] { @@ -88,7 +88,7 @@ pub fn is_geolibre_tool(spec: &rustgis_analysis::ToolSpec) -> bool { pub fn geolibre_status() -> Value { #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] { - let found = rustgis_geolibre::runner::find(); + let found = rustgis_geolibre::runner::selected(); json!({ "supported": true, "web": false, @@ -178,14 +178,26 @@ const MAX_ASYNC_RUNS: usize = 8; /// A GeoLibre run to carry out asynchronously (the browser's WASI runner): run `tool` with /// `argv` in a working folder `/work` holding `files`, then pass the new files and the printed /// lines to [`external_complete`] with `id`. -#[derive(Clone, Debug)] +#[derive(Clone)] pub struct ExternalRun { + continuation: crate::policy::Continuation, pub id: u64, pub tool: String, pub argv: Vec, pub files: Vec<(String, Vec)>, } +impl ExternalRun { + pub fn can_start(&self) -> Result<(), String> { + self.continuation.check() + } + /// An owned principal-free predicate for preparation and the actual browser tool start. + pub fn still(&self) -> crate::render::rustgis_tiles::cache::Still { + let c = self.continuation.clone(); + std::sync::Arc::new(move || c.check().is_ok()) + } +} + /// The new files and printed lines of a finished run, or the runner's error. pub type ExternalOutput = Result<(Vec<(String, Vec)>, Vec), String>; @@ -194,15 +206,25 @@ pub type ExternalOutput = Result<(Vec<(String, Vec)>, Vec), String>; pub fn external_start(s: &mut Session, tool: &str, params: &Value) -> Result { #[cfg(feature = "geolibre")] { - let spec = rustgis_analysis::tool(tool) - .filter(|t| rustgis_analysis::external_source(t) == Some(rustgis_geolibre::SOURCE)) - .ok_or_else(|| format!("no GeoLibre tool '{tool}'"))?; - let params = rustgis_analysis::resolve_params(spec, params)?; - let job = rustgis_geolibre::prepare(spec, &map_inputs(s, Some(spec), ¶ms)?, ¶ms)?; + let op = s.authorize_tool(tool, params)?; + let continuation = s.tool_continuation(&op)?; + let crate::policy::ToolOp { pin, params } = op.op(); + let spec = pin.spec(); + if pin.resolved().source() != Some(rustgis_geolibre::SOURCE) { + return Err(format!("no GeoLibre tool '{tool}'")); + } + let params = params.clone(); + let job = preparing(&continuation, || pin.prepare_suite(&map_inputs(s, Some(spec), ¶ms)?, ¶ms))?; s.next_pending = s.next_pending.wrapping_add(1); let id = s.next_pending; - let run = ExternalRun { id, tool: job.tool.clone(), argv: job.argv("/work"), files: job.files.clone() }; - s.started.push((id, job, params, now_ms())); + let run = ExternalRun { + id, + tool: job.tool.clone(), + argv: job.argv("/work"), + files: job.files.clone(), + continuation: continuation.clone(), + }; + s.started.push((id, job, params, now_ms(), continuation)); // A run whose runner never answers is dropped eventually (it holds its input files). if s.started.len() > MAX_ASYNC_RUNS { s.started.remove(0); @@ -226,7 +248,9 @@ pub fn external_complete(s: &mut Session, id: u64, ran: ExternalOutput) -> Resul .iter() .position(|(n, ..)| *n == id) .ok_or("the tool's run was dropped (its project was closed, or too many runs were waiting)")?; - let (_, job, params, t0) = s.started.remove(i); + s.check_delivery(&s.started.get(i).ok_or("no started tool with that id")?.4)?; + let (_, job, params, t0, continuation) = s.started.remove(i); + s.check_continuation(&continuation)?; // Reading the runner's files back parses outside `Session::run`'s guard: a bug in a // reader must not take the app down. let result = ran.map_err(|e| format!("{}: {e}", job.name)).and_then(|(files, lines)| { @@ -236,6 +260,7 @@ pub fn external_complete(s: &mut Session, id: u64, ran: ExternalOutput) -> Resul let tool_id = format!("{}{}", rustgis_geolibre::PREFIX, job.tool); s.pending.push(crate::PendingRun { id, + continuation, tool: job.name.clone(), tool_id, params, @@ -260,6 +285,7 @@ pub fn external_complete(s: &mut Session, id: u64, ran: ExternalOutput) -> Resul /// [`PluginRun::run`] there, hand what it returns to [`plugin_complete`], then apply it with /// `gp.finish {"id"}`. pub struct PluginRun { + continuation: crate::policy::Continuation, pub id: u64, /// The tool's id. pub tool: &'static str, @@ -269,6 +295,7 @@ pub struct PluginRun { impl PluginRun { /// Runs the tool, blocking until it ends; `control` stops it and shows its progress. pub fn run(&self, control: &rustgis_plugins::tools::Control) -> Result { + self.continuation.check()?; // A bug in a tool must not take the app down. std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| self.job.run(control))) .unwrap_or_else(|_| Err(format!("{} failed unexpectedly", self.job.name()))) @@ -278,6 +305,7 @@ impl PluginRun { /// 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 { + continuation: crate::policy::Continuation, id: u64, name: String, /// The tool's id, for the history. @@ -302,24 +330,42 @@ fn full_ids(full: &std::collections::HashMap) -> FullIds { full.iter().map(|(k, d)| (k.clone(), d.features.iter().map(|f| f.id).collect())).collect() } +/// Checks and carries the admitted owner through every synchronous input/preparation request. +fn preparing(continuation: &crate::policy::Continuation, f: impl FnOnce() -> Result) -> Result { + continuation.check()?; + let owner = continuation.clone(); + let still = std::sync::Arc::new(move || owner.check().is_ok()) as crate::render::rustgis_tiles::cache::Still; + crate::render::rustgis_tiles::cache::with_still(still, || { + let result = f()?; + continuation.check()?; + Ok(result) + }) +} + /// Prepares a run of a plugin tool on the active map, as `gp.run` would run it: its parameters /// checked and its layers read now, so that the map can change while it runs. pub fn plugin_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 plugin tool '{tool}'"))?; - let resolved = rustgis_analysis::resolve_params(spec, params)?; - let inputs = map_inputs(s, Some(spec), &resolved)?; - let job = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| rustgis_plugins::tools::prepare(spec, &inputs, &resolved))) + let op = s.authorize_tool(tool, params)?; + let continuation = s.tool_continuation(&op)?; + let crate::policy::ToolOp { pin, params: resolved } = op.op(); + let spec = pin.spec(); + let plugin = pin.plugin().ok_or_else(|| format!("no plugin tool '{tool}'"))?; + let (_, tid) = rustgis_plugins::split_id(spec.id).ok_or("invalid plugin tool id")?; + let (job, full_ids) = preparing(&continuation, || { + let inputs = map_inputs(s, Some(spec), resolved)?; + let job = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + rustgis_plugins::tools::prepare_pinned(plugin.plugin(), tid, &inputs, resolved) + })) .unwrap_or_else(|_| Err(format!("{} failed unexpectedly", spec.name)))?; - let full_ids = full_ids(&inputs.loaded); - drop(inputs); - let read = read_layers(s, spec, &resolved); + Ok((job, full_ids(&inputs.loaded))) + })?; + let read = read_layers(s, spec, resolved); s.next_pending = s.next_pending.wrapping_add(1); let id = s.next_pending; let params = recorded(Some(spec), params.clone()); let map = (s.map().instance_id(), s.map().name.clone()); s.plugin_runs.push(StartedRun { + continuation: continuation.clone(), id, name: spec.name.to_string(), tool: spec.id.to_string(), @@ -333,7 +379,7 @@ pub fn plugin_start(s: &mut Session, tool: &str, params: &Value) -> Result MAX_ASYNC_RUNS { s.plugin_runs.remove(0); } - Ok(PluginRun { id, tool: spec.id, job }) + Ok(PluginRun { id, tool: spec.id, job, continuation }) } /// Whether a run from [`plugin_start`] still waits for its result: opening or creating a @@ -353,6 +399,25 @@ pub fn plugin_complete(s: &mut Session, id: u64, result: Result) -> Landing { + match result { + Err(_) => Landing::Failure, + Ok(r) => match &r.output { + Output::Layer(_) | Output::Selection { .. } | Output::Update { .. } => Landing::Map, + Output::Layers(out) if out.iter().any(|o| !matches!(o, rustgis_analysis::OutputLayer::Table(_))) => Landing::Map, + _ => Landing::Project, + }, + } +} + /// 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"}`. @@ -367,15 +432,13 @@ fn complete( .iter() .position(|r| r.id == id) .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); + s.check_gp_delivery(&s.plugin_runs.get(i).ok_or("no started tool with that id")?.continuation)?; + let mut run = s.plugin_runs.remove(i); + s.refit_gp_sources(&mut run.continuation, &run.read)?; let mut result = result; // What it adds to or changes on the map belongs on the map it ran on, whatever its name or // place now: not another made since with its name. - 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 on_map = matches!(landing(&result), Landing::Map); 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) { @@ -398,6 +461,7 @@ fn complete( fit_to_windows(s, &mut result, &full_ids); s.pending.push(crate::PendingRun { id, + continuation: run.continuation, tool: run.name, tool_id: run.tool, params: run.params, @@ -416,6 +480,8 @@ fn complete( /// another thread ([`ToolRun::run`]) while the session goes on. Desktop only: the browser runs /// tools inline, and its GeoLibre tools through its own runner ([`external_start`]). pub struct ToolRun { + pin: crate::policy::ToolPin, + continuation: crate::policy::Continuation, pub id: u64, /// The tool's id. pub tool: &'static str, @@ -435,22 +501,30 @@ 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); + let ToolRun { pin, continuation, snap, params, .. } = self; + let spec = pin.spec(); + let name = spec.name; + if let Err(e) = continuation.check() { + return ToolDone { result: Err(e), full_ids: FullIds::new() }; + } if control.is_cancelled() { return ToolDone { result: Err(format!("{name} was cancelled before it started")), full_ids: FullIds::new() }; } + let current = continuation.clone(); + let still = std::sync::Arc::new(move || current.check().is_ok()) as crate::render::rustgis_tiles::cache::Still; // 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) } + crate::render::rustgis_tiles::cache::with_still(still, || { + let inputs = match inputs_of(snap, Some(spec), ¶ms) { + Ok(inputs) => inputs, + Err(e) => return ToolDone { result: Err(e), full_ids: FullIds::new() }, + }; + let mut result = pin.run(&inputs, ¶ms, control, &|| continuation.check()); + 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() }) } @@ -464,16 +538,22 @@ impl ToolRun { /// 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 op = s.authorize_tool(tool, params)?; + let continuation = s.tool_continuation(&op)?; + let crate::policy::ToolOp { pin, params: resolved } = op.op(); + let pin = pin.clone(); + let resolved = resolved.clone(); + let spec = pin.spec(); + if pin.plugin().is_some() { + return Err(format!("no built-in or suite tool '{tool}'")); + } let read = read_layers(s, spec, &resolved); 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 { + continuation: continuation.clone(), id, name: spec.name.to_string(), tool: spec.id.to_string(), @@ -487,7 +567,7 @@ pub fn tool_start(s: &mut Session, tool: &str, params: &Value) -> Result MAX_ASYNC_RUNS { s.plugin_runs.remove(0); } - Ok(ToolRun { id, tool: spec.id, snap: Snapshot::owned(s), params: params.clone() }) + Ok(ToolRun { id, tool: spec.id, snap: Snapshot::owned(s), params: resolved, pin, continuation }) } #[cfg(not(target_arch = "wasm32"))] @@ -515,7 +595,7 @@ pub fn tool_complete(s: &mut Session, id: u64, done: ToolDone) -> Result<(), Str /// thread of its own ([`super::workflows::ahead_on_copy`]): what it read and what it made, for /// the run here to take instead of running the tool again. pub(crate) struct RanAhead { - tool: String, + pin: crate::policy::ToolPin, params: Value, read: Snapshot<'static>, /// The view's extent, when the tool asked for it. @@ -562,12 +642,16 @@ pub(crate) fn ran_ahead(s: &mut Session) -> Vec { #[cfg(not(target_arch = "wasm32"))] /// The next tool run ahead, if it is this one on the same inputs: its result, the streamed rows /// it read whole and how long it took. Any other drops the rest, which came after it. -fn take_ahead(s: &mut Session, tool: &str, params: &Value) -> Option<(Result, FullIds, u64)> { +fn take_ahead( + s: &mut Session, + pin: &crate::policy::ToolPin, + params: &Value, +) -> Option<(Result, FullIds, u64)> { let Some(Ahead::Taking(ran)) = &mut s.tools_ahead else { return None }; let next = ran.pop_front()?; let now = Snapshot::of(s); let view = next.view.is_none_or(|v| v == view_extent(&now)); - if next.tool == tool && next.params == *params && view && same_read(&next.read, &now) { + if next.pin == *pin && next.params == *params && view && same_read(&next.read, &now) { #[cfg(test)] { s.tools_taken += 1; @@ -645,7 +729,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() { + if now.instance_id() != was.instance || now.dataset.source != was.source || now.linked_table() != was.table.as_deref() { *res = Err(format!("{layer} was removed or replaced while {tool} ran")); return; } @@ -690,6 +774,8 @@ fn output_layer(res: &Result) -> Option bool { + self.id == id + && self.instance == instance + && self.streamed + && !now.is_streamed() + && now.dataset.source == self.source + && now.linked_table() == self.table.as_deref() + } + /// Retains the same in-memory incarnation and source while names and streamed windows move. fn of(l: &rustgis_map::Layer) -> Self { Read { id: l.id, + instance: l.instance_id(), dataset: l.dataset.clone(), streamed: l.is_streamed(), source: l.dataset.source.clone(), @@ -767,8 +864,12 @@ 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")?; let i = s.pending.iter().position(|r| r.id == id).ok_or("no finished run with that id")?; + s.check_gp_finish(&s.pending[i].continuation, landing(&s.pending[i].result))?; let r = s.pending.remove(i); - apply(s, Ran { name: r.tool, id: r.tool_id, command: None, started: r.started }, r.params, r.result, r.millis) + let c = r.continuation.clone(); + s.resume_gp(&c, landing(&r.result), |s| { + 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: the active map, the standalone tables, the geoprocessing @@ -857,10 +958,10 @@ fn inputs_of<'a>(snap: Snapshot<'a>, spec: Option<&rustgis_analysis::ToolSpec>, #[cfg(not(target_arch = "wasm32"))] let mut full = full; #[cfg(not(target_arch = "wasm32"))] - if let Some(spec) = spec.filter(|t| rustgis_analysis::external_source(t).is_none()) { + if let Some(spec) = spec.filter(|t| rustgis_analysis::TOOLS.iter().any(|builtin| std::ptr::eq(builtin, *t))) { 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 snap.map.layer_by_name(name).is_none() && !full.contains_key(name) { + if vector_file_input(spec, &snap.map, name) && !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); @@ -905,18 +1006,22 @@ fn streamed_inputs( if full.contains_key(&key) { continue; } - if let Some(l) = - map.layer_by_name(n).filter(|l| l.is_streamed() && (l.selection.is_empty() || spec.is_some_and(|t| t.id == "vectorToPmtiles"))) - { + if let Some(l) = map.layer_by_name(n).filter(|l| reads_full_source(l, spec)) { full.insert(key, l.load_all().map_err(|e| format!("{}: {e}", l.name))?); } } Ok(full) } +/// The executor's source-read choice, shared with pure permission planning. +/// Selected in-memory windows need no whole read, except for the PMTiles exporter. +pub(crate) fn reads_full_source(layer: &rustgis_map::Layer, spec: Option<&rustgis_analysis::ToolSpec>) -> bool { + layer.is_streamed() && (layer.selection.is_empty() || spec.is_some_and(|t| t.id == "vectorToPmtiles")) +} + /// The layers a tool's parameters name: the values of its layer parameters (all of them, for a /// tool without a spec), so a field or text value that happens to match a layer's name isn't one. -fn layer_names<'v>(spec: Option<&rustgis_analysis::ToolSpec>, params: &'v Value) -> Vec<&'v str> { +pub(crate) fn layer_names<'v>(spec: Option<&rustgis_analysis::ToolSpec>, params: &'v Value) -> Vec<&'v str> { fn names<'v>(v: &'v Value, out: &mut Vec<&'v str>) { match v { Value::String(s) => out.push(s), @@ -1016,9 +1121,7 @@ impl Inputs for MapInputs<'_> { self.loaded .get(layer) .or_else(|| self.loaded.get(&layer.to_ascii_lowercase())) - .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.snap.tables, layer).and_then(|i| self.snap.tables.get(i)).map(|t| &t.dataset)) + .or_else(|| project_input(&self.snap.map, &self.snap.tables, layer, false).map(|i| i.dataset())) } fn table(&self, id: u32) -> Option<&Dataset> { self.snap.tables.iter().find(|t| t.id == id).map(|t| &t.dataset) @@ -1055,9 +1158,15 @@ fn run(s: &mut Session, p: &Value) -> CmdResult { run_tool(s, &tool, ¶ms) } +/// Resolves the same tool for execution and permission checks, keeping built-in name precedence. +/// History records may name a tool by its display name rather than its id. +pub fn resolve_tool(name: &str) -> Option<&'static rustgis_analysis::ToolSpec> { + rustgis_analysis::resolve_published(name).map(|r| r.spec()) +} + /// The id of a tool named by its id or its display name (history records keep the name). pub fn tool_id(name: &str) -> Option<&'static str> { - rustgis_analysis::tool(name).or_else(|| rustgis_analysis::tools().find(|t| t.name.eq_ignore_ascii_case(name))).map(|t| t.id) + resolve_tool(name).map(|t| t.id) } /// One point, for a tool run on a place clicked on the map. @@ -1096,7 +1205,12 @@ fn buffer_point(s: &mut Session, p: &Value) -> CmdResult { command["name"] = n.clone(); } let t0 = now_ms(); - let res = rustgis_analysis::run("buffer", &PointInput(ds), ¶ms); + let op = s.point_tool(¶ms)?; + let c = s.tool_continuation(&op)?; + s.check_continuation(&c)?; + let tool = op.op(); + let res = tool.pin.run(&PointInput(ds), &tool.params, &rustgis_analysis::control::Control::new(), &|| c.check()); + s.check_continuation(&c)?; let millis = now_ms().saturating_sub(t0); let ran = Ran { name: "Buffer".into(), id: "buffer".into(), command: Some(("gp.bufferPoint".into(), command)), started: t0 }; apply(s, ran, params, res, millis) @@ -1130,37 +1244,60 @@ pub fn run_tool_with( params: &Value, control: &std::sync::Arc, ) -> CmdResult { - let tool = tool_id(tool).unwrap_or(tool).to_string(); - let params = params.clone(); + let op = s.authorize_tool(tool, params)?; + s.invoke_tool(op, control) +} + +/// Runs only the immutable tool/schema/load admitted by policy. +pub(crate) fn execute( + s: &mut Session, + op: crate::policy::Authorized, + control: &std::sync::Arc, +) -> CmdResult { + let crate::policy::ToolOp { pin, params } = op.op(); + pin.current()?; + let spec = pin.spec(); + #[cfg(not(target_arch = "wasm32"))] + let recording = matches!(s.tools_ahead, Some(Ahead::Recording(_))); + #[cfg(not(target_arch = "wasm32"))] + let current = rustgis_analysis::control::current().filter(|_| recording); + #[cfg(not(target_arch = "wasm32"))] + let control = current.as_ref().unwrap_or(control); let t0 = now_ms(); if control.is_cancelled() { - let spec = rustgis_analysis::tool(&tool); - let name = spec.map(|t| t.name).unwrap_or("Tool").to_string(); - let e = format!("{name} was cancelled before it started"); - return apply(s, Ran { name, id: tool, command: None, started: t0 }, recorded(spec, params), Err(e), 0); + let e = format!("{} was cancelled before it started", spec.name); + return apply( + s, + Ran { name: spec.name.into(), id: spec.id.into(), command: None, started: t0 }, + recorded(Some(spec), params.clone()), + Err(e), + 0, + ); } + let continuation = s.tool_continuation(&op)?; #[cfg(not(target_arch = "wasm32"))] - let ran = take_ahead(s, &tool, ¶ms); + let ran = take_ahead(s, pin, params); #[cfg(target_arch = "wasm32")] let ran = None; let (mut res, ids, millis) = match ran { Some(ran) => ran, None => { #[cfg(not(target_arch = "wasm32"))] - let recording = matches!(s.tools_ahead, Some(Ahead::Recording(_))); - #[cfg(not(target_arch = "wasm32"))] - if recording && !rustgis_analysis::tool(&tool).is_some_and(|t| !rustgis_analysis::is_external(t) && !writes_files(t)) { + if recording && (pin.resolved().source().is_some() || writes_files(spec)) { return Err(NOT_AHEAD.into()); } - // A copy of the session runs under the control of the thread it runs on, which Cancel stops. - #[cfg(not(target_arch = "wasm32"))] - let current = rustgis_analysis::control::current().filter(|_| recording); - #[cfg(not(target_arch = "wasm32"))] - let control = current.as_ref().unwrap_or(control); #[cfg(not(target_arch = "wasm32"))] let read = recording.then(|| Snapshot::owned(s)); - let inputs = map_inputs(s, rustgis_analysis::tool(&tool), ¶ms)?; - let mut res = rustgis_analysis::run_with(&tool, &inputs, ¶ms, control); + let inputs = map_inputs(s, Some(spec), params)?; + let mut res = if let Some(plugin) = pin.plugin() { + let (_, tool) = rustgis_plugins::split_id(spec.id).ok_or("invalid plugin tool id")?; + rustgis_analysis::run_under(spec.name, control, || { + rustgis_plugins::tools::prepare_pinned(plugin.plugin(), tool, &inputs, params)? + .run(&rustgis_plugins::tools::Control::default()) + }) + } else { + pin.run(&inputs, params, control, &|| continuation.check()) + }; if let Ok(result) = &mut res { result.messages.extend(inputs.messages.iter().cloned()); } @@ -1176,16 +1313,29 @@ pub fn run_tool_with( let millis = now_ms().saturating_sub(t0); #[cfg(not(target_arch = "wasm32"))] if let (Some(read), Some(Ahead::Recording(ran))) = (read, &mut s.tools_ahead) { - let (result, full_ids) = (res.clone(), ids.clone()); - ran.push(RanAhead { tool: tool.clone(), params: params.clone(), read, view, result, full_ids, millis }); + ran.push(RanAhead { + pin: pin.clone(), + params: params.clone(), + read, + view, + result: res.clone(), + full_ids: ids.clone(), + millis, + }); } (res, ids, millis) } }; + pin.current()?; + continuation.check()?; fit_to_windows(s, &mut res, &ids); - let spec = rustgis_analysis::tool(&tool); - let name = spec.map(|t| t.name).unwrap_or("Tool").to_string(); - apply(s, Ran { name, id: tool, command: None, started: t0 }, recorded(spec, params), res, millis) + apply( + s, + Ran { name: spec.name.into(), id: spec.id.into(), command: None, started: t0 }, + recorded(Some(spec), params.clone()), + res, + millis, + ) } /// Whether a tool can read files named by a path or vector-layer-list parameter. @@ -1200,6 +1350,7 @@ pub fn writes_files(spec: &rustgis_analysis::ToolSpec) -> bool { /// A browser file export waiting for delivery, without a success record until acknowledged. pub(crate) struct PendingFile { + pub(crate) continuation: crate::policy::Continuation, pub(crate) id: u64, pub(crate) file_name: String, pub(crate) bytes: Option>, @@ -1220,7 +1371,7 @@ fn queue_file(s: &mut Session, file_name: String, bytes: Vec, record: GpReco } s.next_pending = s.next_pending.wrapping_add(1); let id = s.next_pending; - s.file_outputs.push(PendingFile { id, file_name: file_name.clone(), bytes: Some(bytes), record }); + s.file_outputs.push(PendingFile { id, file_name: file_name.clone(), bytes: Some(bytes), record, continuation: s.continuation()? }); s.request_only = true; Ok(json!({"pending": true, "download": id, "file": file_name})) } @@ -1229,6 +1380,7 @@ fn queue_file(s: &mut Session, file_name: String, bytes: Vec, record: GpReco fn file_delivered(s: &mut Session, p: &Value) -> CmdResult { let id = p.get("id").and_then(Value::as_u64).ok_or("id is required")?; let i = s.file_outputs.iter().position(|f| f.id == id).ok_or("no pending file with that id")?; + s.gp_file_owner(&s.file_outputs[i].continuation)?; if s.file_outputs[i].bytes.is_some() { return Err("the file has not been handed to the download service".into()); } @@ -1333,6 +1485,8 @@ fn names_file(source: &str, destination: &std::path::Path) -> bool { /// Adds a tool's result to the map and the history. fn apply(s: &mut Session, ran: Ran, params: Value, res: Result, millis: u64) -> CmdResult { + let continuation = s.continuation()?; + s.check_continuation(&continuation)?; let Ran { name, id, command, started } = ran; let at = started / 1000; let mut result = match res { @@ -1518,6 +1672,18 @@ mod file_tests { assert!(pending.contains("pixels are not loaded"), "{pending}"); } + /// Unit bytes still enter through Session::run so their late acknowledgment has an owner. + fn queue_file(s: &mut Session, name: String, bytes: Vec, record: GpRecord) -> CmdResult { + if s.registry.get("test.queueFile").is_none() { + s.registry.commands.push(C::new("test.queueFile", "File export", "Test", |s, p| { + let record = serde_json::from_value(p["record"].clone()).unwrap(); + let bytes = serde_json::from_value(p["bytes"].clone()).unwrap(); + super::queue_file(s, p["name"].as_str().unwrap().into(), bytes, record) + })); + } + s.run("test.queueFile", &json!({"name":name,"bytes":bytes,"record":record})) + } + fn record() -> GpRecord { GpRecord { tool: "File export".into(), @@ -1587,12 +1753,110 @@ mod file_tests { assert_eq!(s.undo_depth(), depth); } + /// Browser file bytes are an owned snapshot; native delivery does not target the displayed map. + #[test] + fn native_file_delivery_survives_a_map_switch() { + for take_before_switch in [false, true] { + let mut s = task_session(json!([])); + let pending = s.run("gp.run", &json!({"tool":"vectorToPmtiles", "params":{"input":"Cities","minZoom":0,"maxZoom":0}})).unwrap(); + let id = pending["download"].as_u64().unwrap(); + let early = take_before_switch.then(|| s.take_gp_files()); + s.run("map.new", &json!({"name":"Other"})).unwrap(); + let displayed = s.map().instance_id(); + let files = early.unwrap_or_else(|| s.take_gp_files()); + assert_eq!(files.len(), 1, "the native snapshot survives either delivery boundary"); + assert_eq!(files[0].0, id); + assert!(files[0].2.starts_with(b"PMTiles\x03"), "the real exporter produced the queued file"); + assert!(s.project.gp_history.is_empty(), "bytes alone do not record success"); + assert_eq!(s.acknowledge_gp_file(id, None).unwrap()["delivered"], true); + assert_eq!(s.map().instance_id(), displayed, "delivery does not activate its origin map"); + assert_eq!(s.project.gp_history.len(), 1); + assert!(s.project.gp_history[0].ok); + assert!(s.take_gp_files().is_empty(), "bytes are handed over once"); + assert!(s.acknowledge_gp_file(id, None).is_err(), "delivery is acknowledged once"); + } + } + + /// Browser-style bytes and final delivery remain one-use and owned by their initiating agent. + #[test] + fn permission_policy_file_delivery_owner_and_replay() { + struct Live(std::sync::atomic::AtomicBool); + impl crate::policy::Revocation for Live { + fn allows(&self, _: crate::policy::Effects) -> bool { + self.0.load(std::sync::atomic::Ordering::SeqCst) + } + } + let mut s = task_session(json!([])); + let live = std::sync::Arc::new(Live(std::sync::atomic::AtomicBool::new(true))); + let owner = + crate::policy::Scope::agent(crate::policy::AgentAccess::Allow, false, "file-owner", Some("file-owner".into()), live.clone()); + let foreign = crate::policy::Scope::agent( + crate::policy::AgentAccess::Allow, + false, + "file-foreign", + None, + std::sync::Arc::new(Live(std::sync::atomic::AtomicBool::new(true))), + ); + let call = crate::policy::Call::tool("vectorToPmtiles", json!({"input":"Cities","minZoom":0,"maxZoom":0})); + let pending = s.run_scoped(owner.clone(), &call).unwrap(); + let id = pending["download"].as_u64().unwrap(); + assert!(s.run_scoped(foreign.clone(), &crate::policy::Call::command("gp.fileDelivered", json!({"id":id}))).is_err()); + let mark = s.notice_mark(); + assert!(s.with_scope(foreign, |s| s.take_gp_files()).unwrap().is_empty()); + assert!(s.notices_since(mark).is_empty(), "a foreign drain leaves the owner's bytes and notices alone"); + assert_eq!(s.take_gp_files().len(), 1); + assert!(s.take_gp_files().is_empty()); + assert_eq!(s.acknowledge_gp_file(id, None).unwrap()["delivered"], true); + assert_eq!(s.undo_actor(), Some(Some("file-owner"))); + assert!(s.acknowledge_gp_file(id, None).is_err()); + s.run_scoped(owner.clone(), &crate::policy::Call::command("edit.undo", Value::Null)).unwrap(); + // Several exports can be revoked together before the UI takes their bytes. + let mut retired = Vec::new(); + for name in ["first.pmtiles", "second.pmtiles"] { + let call = crate::policy::Call::tool("vectorToPmtiles", json!({"input":"Cities","minZoom":0,"maxZoom":0,"output":name})); + let pending = s.run_scoped(owner.clone(), &call).unwrap(); + retired.push(pending["download"].as_u64().unwrap()); + } + live.0.store(false, std::sync::atomic::Ordering::SeqCst); + let mark = s.notice_mark(); + assert!(s.take_gp_files().is_empty()); + let errors: Vec<_> = s.notices_since(mark).iter().filter(|notice| notice.error).map(|notice| notice.text.as_str()).collect(); + assert_eq!( + errors, + [ + "2 pending file export(s) could not be completed (\"first.pmtiles\", \"second.pmtiles\"): this request's authority was revoked" + ] + ); + assert!(s.project.gp_history.is_empty(), "revoked queued files cannot record success"); + assert!(s.take_gp_files().is_empty()); + for id in retired { + assert!(s.acknowledge_gp_file(id, None).is_err(), "a retired file cannot replay its acknowledgment"); + } + assert_eq!(s.notices_since(mark).len(), 1, "retired file exports report their count once"); + live.0.store(true, std::sync::atomic::Ordering::SeqCst); + // A host callback may revoke authority after taking the bytes but before acknowledgment. + let pending = s.run_scoped(owner.clone(), &call).unwrap(); + let id = pending["download"].as_u64().unwrap(); + assert_eq!(s.take_gp_files().len(), 1); + live.0.store(false, std::sync::atomic::Ordering::SeqCst); + let notice_mark = s.notice_mark(); + assert!(s.acknowledge_gp_file(id, None).unwrap_err().contains("revoked")); + assert!(s.notices_since(notice_mark).is_empty(), "the UI must report failures before command dispatch"); + assert!(s.project.gp_history.is_empty(), "revoked delivery cannot record success"); + live.0.store(true, std::sync::atomic::Ordering::SeqCst); + let pending = s.run_scoped(owner, &call).unwrap(); + s.run("map.new", &json!({"name":"Other"})).unwrap(); + assert!(s.take_gp_files().is_empty(), "restricted delivery still requires its original active map"); + assert!(s.acknowledge_gp_file(pending["download"].as_u64().unwrap(), None).is_err()); + assert!(s.project.gp_history.is_empty(), "refused restricted delivery cannot record success"); + } + #[test] fn queueing_a_file_is_not_an_edit_before_delivery() { let mut s = Session::new(crate::Project::default()); s.mark_saved(); let depth = s.undo_depth(); - let pending = s.guarded("File export", true, |s| queue_file(s, "cities.pmtiles".into(), vec![1, 2, 3], record())).unwrap(); + let pending = queue_file(&mut s, "cities.pmtiles".into(), vec![1, 2, 3], record()).unwrap(); assert!(!s.dirty(), "generated bytes alone do not change the project"); assert_eq!(s.undo_depth(), depth, "queueing must not add an empty undo step"); assert!(s.project.gp_history.is_empty()); @@ -1657,6 +1921,34 @@ mod file_tests { assert_eq!(s.take_gp_files().len(), 8); } + /// Draining an old refused file cannot let a new export escape a failed handler's rollback. + #[test] + fn failed_commands_cannot_replace_retired_pending_files() { + let mut s = task_session(json!([])); + let login = s + .run("connection.new", &json!({"kind":"wms","address":"https://file-rollback.invalid/wms","auth":"header","keyName":"X-Key"})) + .unwrap()["id"] + .as_str() + .unwrap() + .to_string(); + s.run("connection.login", &json!({"id":login,"apiKey":"RGSYNTH_file_rollback"})).unwrap(); + let old = + s.run("gp.run", &json!({"tool":"vectorToPmtiles","params":{"input":"Cities","minZoom":0,"maxZoom":0}})).unwrap()["download"] + .as_u64() + .unwrap(); + s.run("connection.logout", &json!({"id":login})).unwrap(); + s.registry.commands.push(C::new("test.replaceFileThenFail", "Failed replacement export", "Test", |s, _| { + s.take_gp_files(); + s.run("gp.run", &json!({"tool":"vectorToPmtiles","params":{"input":"Cities","minZoom":0,"maxZoom":0}}))?; + Err("fail after replacing a refused file".into()) + })); + assert!(s.run("test.replaceFileThenFail", &Value::Null).is_err()); + assert!(s.take_gp_files().is_empty(), "the failed handler's replacement file cannot reach the UI"); + assert!(s.file_outputs.is_empty()); + assert!(s.acknowledge_gp_file(old, None).is_err(), "the old refused file must not be restored either"); + assert!(s.project.gp_history.is_empty()); + } + #[cfg(not(target_arch = "wasm32"))] #[test] fn native_export_preserves_the_saved_project_file() { @@ -2154,15 +2446,20 @@ mod file_tests { std::fs::create_dir(&root).unwrap(); let destination = root.join("cities.pmtiles"); std::fs::write(&destination, b"previous").unwrap(); - let file = || { - Ok(rustgis_analysis::ToolResult { - output: Output::File { file_name: "cities.pmtiles".into(), bytes: b"archive".to_vec() }, - messages: vec!["Exported data".into()], - }) - }; - let ran = || Ran { name: "File export".into(), id: "vectorToPmtiles".into(), command: None, started: 1000 }; + s.registry.commands.push(C::new("test.applyFile", "File export", "Test", |s, p| { + apply( + s, + Ran { name: "File export".into(), id: "vectorToPmtiles".into(), command: None, started: 1000 }, + p.clone(), + Ok(rustgis_analysis::ToolResult { + output: Output::File { file_name: "cities.pmtiles".into(), bytes: b"archive".to_vec() }, + messages: vec!["Exported data".into()], + }), + 3, + ) + })); let params = json!({"output": destination.display().to_string()}); - let out = apply(&mut s, ran(), params, file(), 3).unwrap(); + let out = s.run("test.applyFile", ¶ms).unwrap(); assert_eq!(out["bytes"], 7); assert_eq!(std::fs::read(&destination).unwrap(), b"archive"); assert!(s.map().layers.is_empty()); @@ -2172,7 +2469,7 @@ mod file_tests { std::fs::create_dir(&occupied).unwrap(); std::fs::write(occupied.join("keep"), b"unchanged").unwrap(); let params = json!({"output": occupied.display().to_string()}); - assert!(s.guarded("File export", true, |s| apply(s, ran(), params, file(), 3)).is_err()); + assert!(s.guarded("File export", true, |s| s.run("test.applyFile", ¶ms)).is_err()); assert_eq!(std::fs::read(occupied.join("keep")).unwrap(), b"unchanged"); assert_eq!(s.project.gp_history.len(), 2, "a failed save must be recorded"); let failed = &s.project.gp_history[1]; @@ -2188,7 +2485,7 @@ mod file_tests { s.project.gp_history.resize(500, previous); let params = json!({"output": occupied.display().to_string()}); assert!( - s.guarded("Failed export task", true, |s| { s.guarded("File export", true, |s| apply(s, ran(), params, file(), 3)) }).is_err() + s.guarded("Failed export task", true, |s| { s.guarded("File export", true, |s| s.run("test.applyFile", ¶ms)) }).is_err() ); assert_eq!(s.project.gp_history.len(), 500); assert!(s.project.gp_history.last().is_some_and(|record| !record.ok)); @@ -2205,7 +2502,7 @@ mod file_permission_tests { #[test] fn file_tools_are_file_access_even_without_an_output_path() { let s = Session::new(crate::Project::default()); - let tool = rustgis_analysis::tool("vectorToPmtiles").expect("registered file export tool"); + let tool = rustgis_analysis::resolve_published("vectorToPmtiles").expect("registered file export tool").spec(); assert!(writes_files(tool)); assert!(!reads_files(tool), "output files are not input paths"); for output in [None, Some("archive"), Some("cities.pmtiles")] { @@ -2214,7 +2511,7 @@ mod file_permission_tests { params["output"] = json!(output); } let p = json!({"tool": "vectorToPmtiles", "params": params}); - assert!(crate::cmd::items::touches_files("gp.run", &p)); + assert!(crate::cmd::items::touches_files(&s, "gp.run", &p)); let reaches = s.registry.get("gp.run").and_then(|spec| crate::cmd::plugins::reaches(spec, &p)); assert!(matches!(reaches, Some(crate::cmd::plugins::Reach::Files))); } @@ -2226,3 +2523,31 @@ mod file_permission_tests { assert!(crate::model::output_ports(&node).is_empty(), "files must not be wired as layers"); } } + +/// A project dataset chosen with the same precedence as the actual tool input provider. +pub(crate) enum ProjectInput<'a> { + Layer(&'a rustgis_map::Layer), + Table(&'a crate::project::Table), +} +impl<'a> ProjectInput<'a> { + fn dataset(self) -> &'a Dataset { + match self { + Self::Layer(l) => &l.dataset, + Self::Table(t) => &t.dataset, + } + } +} +pub(crate) fn project_input<'a>( + map: &'a rustgis_map::Map, + tables: &'a [crate::project::Table], + name: &str, + raster: bool, +) -> Option> { + map.layer_by_name(name).map(ProjectInput::Layer).or_else(|| { + (!raster).then(|| crate::project::find_table(tables, name).and_then(|i| tables.get(i)).map(ProjectInput::Table)).flatten() + }) +} +/// Builtin VectorLayers reads absent map names as files before MapInputs' table fallback. +pub(crate) fn vector_file_input(spec: &rustgis_analysis::ToolSpec, map: &rustgis_map::Map, name: &str) -> bool { + rustgis_analysis::TOOLS.iter().any(|t| std::ptr::eq(t, spec)) && map.layer_by_name(name).is_none() +} diff --git a/crates/engine/src/cmd/gpkg.rs b/crates/engine/src/cmd/gpkg.rs index 9bfd336961..106c024d67 100644 --- a/crates/engine/src/cmd/gpkg.rs +++ b/crates/engine/src/cmd/gpkg.rs @@ -11,14 +11,14 @@ use serde_json::{Value, json}; pub fn commands() -> Vec { vec![ - C::new("gpkg.addLayer", "Edit GeoPackage Table", "Home › Add › Add Data", add_layer).roles(&[]).params( + C::new("gpkg.addLayer", "Edit GeoPackage Table", "Home › Add › Add Data", add_layer).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( r#"{"path": "a GeoPackage file on this computer", "table"?: "its feature table (needed when it has several)", "limit"?: "the most rows (default and most: 100000)", "name"?: string}"#, ), - C::new("gpkg.pendingEdits", "GeoPackage Edits", "Edit › Manage Edits", pending_edits).roles(&[]).view() + C::new("gpkg.pendingEdits", "GeoPackage Edits", "Edit › Manage Edits", pending_edits).roles(&[]).effects(crate::policy::Effects::NONE).view() .params(r#"{"layer"?: "a layer added with gpkg.addLayer (default: the active one)"}"#), - C::new("gpkg.saveEdits", "Save to GeoPackage", "Edit › Manage Edits", save_edits).roles(&[]) + C::new("gpkg.saveEdits", "Save to GeoPackage", "Edit › Manage Edits", save_edits).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)) .params(r#"{"layer"?: "a layer added with gpkg.addLayer (default: the active one)"}"#), - C::new("gpkg.discardEdits", "Discard GeoPackage Edits", "Edit › Manage Edits", discard_edits).roles(&[]) + C::new("gpkg.discardEdits", "Discard GeoPackage Edits", "Edit › Manage Edits", discard_edits).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"layer"?: "a layer added with gpkg.addLayer (default: the active one)"}"#), ] } diff --git a/crates/engine/src/cmd/graphics.rs b/crates/engine/src/cmd/graphics.rs index 4d89de39ae..0ad7a25adf 100644 --- a/crates/engine/src/cmd/graphics.rs +++ b/crates/engine/src/cmd/graphics.rs @@ -16,27 +16,28 @@ const MAX_PICTURE: usize = 16 << 20; /// Most vertices in one graphic. const MAX_POINTS: usize = 100_000; +/// Keeps in-memory graphics available without file access; explicit picture paths add that reach. pub fn commands() -> Vec { vec![ C::new("graphics.newLayer", "Add Graphics Layer", "Home › Add", |s, p| { let name = ps(p, "name").map(str::trim).filter(|n| !n.is_empty()).unwrap_or("Graphics"); let id = new_layer(s, name); Ok(json!({"layer": id})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name"?: string}"#), - C::new("graphics.add", "Add Graphic", "Edit › Graphics", add).roles(&[]).params( + C::new("graphics.add", "Add Graphic", "Edit › Graphics", add).roles(&[]).effects(crate::policy::Effects::NONE).params( r##"{"shape": "point"|"text"|"line"|"curve"|"freehand"|"polygon"|"lasso"|"rectangle"|"circle"|"ellipse"|"picture", "points": [[lon, lat], …] (rectangle, ellipse, picture: two corners; circle: centre and a point on the edge), "layer"?: graphics layer, "text"?: string (dynamic tokens: {map} {scale} {date} {time} {crs} {layers} {center}), "fill"?: "#RRGGBBAA", "outline"?: "#RRGGBBAA", "width"?: number, "size"?: number, "fontSize"?: number, "bold"?: bool, "italic"?: bool, "align"?: "left"|"center"|"right" (a text's lines in their block, which stays centred on its point), "letterSpacing"?: points, -5 to 50, "image"?: base64 PNG/JPEG, "path"?: picture file}"##, ), - C::new("graphics.style", "Graphic Style", "Edit › Graphics", style).roles(&[]) + C::new("graphics.style", "Graphic Style", "Edit › Graphics", style).roles(&[]).effects(crate::policy::Effects::NONE) .when(super::need_selection) .params(r##"{"fill"?: "#RRGGBBAA", "outline"?: "#RRGGBBAA", "width"?: number, "size"?: number, "fontSize"?: number, "bold"?: bool, "italic"?: bool, "align"?: "left"|"center"|"right", "letterSpacing"?: points, -5 to 50, "text"?: string}"##), - C::new("graphics.edit", "Edit Graphic", "Edit › Graphics", edit).roles(&[]).params( + C::new("graphics.edit", "Edit Graphic", "Edit › Graphics", edit).roles(&[]).effects(crate::policy::Effects::NONE).params( r##"{"layer": graphics layer, "row": number, "points"?: [[lon, lat]] (a point or a text: where it sits), "text"?: string, "fontSize"?: number, "bold"?: bool, "italic"?: bool, "align"?: "left"|"center"|"right", "letterSpacing"?: points, -5 to 50, "fill"?: "#RRGGBBAA", "outline"?: "#RRGGBBAA", "width"?: number, "size"?: number}"##, ), C::new("graphics.keepTool", "Keep the last tool active", "Edit › Graphics", |s, p| { s.keep_tool = b(p, "on").unwrap_or(!s.keep_tool); Ok(json!({"keepTool": s.keep_tool})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"on"?: bool}"#), ] diff --git a/crates/engine/src/cmd/group.rs b/crates/engine/src/cmd/group.rs index d868f4f3ad..ef7ba1ce23 100644 --- a/crates/engine/src/cmd/group.rs +++ b/crates/engine/src/cmd/group.rs @@ -10,38 +10,38 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("group.add", "New Group", "Layers", add).roles(&[]).params( + C::new("group.add", "New Group", "Layers", add).inputs(add_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"name"?: text (default New Group), "parent"?: name|id (the group it goes in), "layers"?: [name|id] (moved into it, out of any other group; together in the drawing order at the topmost)}"#, ), - C::new("group.set", "Group Properties", "Layers", set).roles(&[]).params( + C::new("group.set", "Group Properties", "Layers", set).inputs(group_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"group": name|id, "name"?: text, "visible"?: bool (toggles when no other property is given), "transparency"?: 0-100 (on top of each layer's own)}"#, ), - C::new("group.expand", "Expand Group", "Layers", expand).roles(&[]) + C::new("group.expand", "Expand Group", "Layers", expand).inputs(group_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"group": name|id, "on"?: bool (toggles when omitted)}"#) .view(), - C::new("group.scaleRange", "Group Visibility Range", "Layers", scale_range).roles(&[]).params( + C::new("group.scaleRange", "Group Visibility Range", "Layers", scale_range).inputs(group_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"group": name|id, "min"?: scale (out beyond), "max"?: scale (in beyond); both set on each call, one left out is cleared; its layers draw only within it and their own}"#, ), - C::new("group.move", "Move Group", "Layers", move_group).roles(&[]).params( + C::new("group.move", "Move Group", "Layers", move_group).inputs(group_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"group": name|id, "to": position among the other layers, counted in layers (0 = top; past the end, the bottom; never inside another group: above it; a group in another stays in it, at most at its top or bottom)}"#, ), - C::new("group.setParent", "Move Group To Group", "Layers", set_parent).roles(&[]).params( + C::new("group.setParent", "Move Group To Group", "Layers", set_parent).inputs(parent_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"group": name|id, "parent": name|id|null (the group it goes in; null: the top level), "before"?: name|id|null (a layer or group: places the group just above it; null puts it at the end of the parent, or the bottom at the top level; omitted: next to the parent's layers, or where it is at the top level)}"#, ), - C::new("group.indent", "Indent Group", "Layers", indent_group).roles(&[]) + C::new("group.indent", "Indent Group", "Layers", indent_group).inputs(indent_group_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"group": name|id} (into the group just above it in the Layers pane, as its last item)"#), - C::new("group.outdent", "Outdent Group", "Layers", outdent_group).roles(&[]) + C::new("group.outdent", "Outdent Group", "Layers", outdent_group).inputs(outdent_group_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"group": name|id} (out of the group it is in, up one level, just below that group)"#), - C::new("group.ungroup", "Ungroup", "Layers", ungroup).roles(&[]) + C::new("group.ungroup", "Ungroup", "Layers", ungroup).inputs(group_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"group": name|id} (its layers and subgroups stay, one level up)"#), - C::new("group.remove", "Remove Group", "Layers", remove).roles(&[]).params(r#"{"group": name|id} (and its layers and subgroups)"#), - C::new("layer.group", "Move To Group", "Layers", move_to_group).roles(&[]) + C::new("group.remove", "Remove Group", "Layers", remove).inputs(group_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"group": name|id} (and its layers and subgroups)"#), + C::new("layer.group", "Move To Group", "Layers", move_to_group).inputs(layer_group_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(super::need_layer) .params(r#"{"layer"?: name|id | [name|id] (default: the layer selection), "group": name|id|null (null takes it out of its group), "before"?: name|id|null (places a single layer just above this layer, one of the group's or its subgroups'; null puts it at the group's end, or the bottom out of a group; omitted: next to the group's layers, or where it is when taken out)}"#), - C::new("layer.indent", "Indent Layer", "Layers", indent_layer).roles(&[]) + C::new("layer.indent", "Indent Layer", "Layers", indent_layer).inputs(indent_layer_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(super::need_layer) .params(r#"{"layer"?: name|id (default: the active layer)} (into the group just above it in the Layers pane, as its last item)"#), - C::new("layer.outdent", "Outdent Layer", "Layers", outdent_layer).roles(&[]) + C::new("layer.outdent", "Outdent Layer", "Layers", outdent_layer).inputs(outdent_layer_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(super::need_layer) .params(r#"{"layer"?: name|id (default: the active layer)} (out of its group, up one level, just below that group)"#), ] @@ -80,7 +80,9 @@ fn parent_of(m: &Map, p: &Value) -> Result, String> { } /// A layer that can join a group: not one of a service's collections (its service group keeps it). -fn groupable(s: &Session, id: u32) -> Result { +fn groupable(input: &impl super::InputSource, id: u32) -> Result { + let context = input.input_context(); + let s = &context; if in_service(s.map(), id) { let name = s.map().layer(id).map(|l| l.name.clone()).unwrap_or_default(); return Err(format!("'{name}' belongs to a service group")); @@ -114,16 +116,7 @@ fn add(s: &mut Session, p: &Value) -> CmdResult { if parent.is_some_and(|g| s.map().group_depth(g) >= MAX_GROUP_DEPTH) { return Err(format!("groups nest at most {MAX_GROUP_DEPTH} levels deep")); } - let mut children = Vec::new(); - if let Some(layers) = p.get("layers") { - let layers = layers.as_array().ok_or("layers must be a list of layer names or ids")?; - for v in layers { - let id = groupable(s, s.layer_id(&json!({"layer": v}))?)?; - if !children.contains(&id) { - children.push(id); - } - } - } + let mut children = added_layers(s, p)?; // In drawing order, so gathering keeps them as they were. let m = s.map_mut(); children.sort_by_key(|id| m.layer_index(*id)); @@ -201,7 +194,9 @@ fn move_group(s: &mut Session, p: &Value) -> CmdResult { } /// A `before` that names a layer or a group, as the layer at the top of it. -fn before_of(s: &Session, v: &Value) -> Result { +fn before_of(input: &impl super::InputSource, v: &Value) -> Result { + let context = input.input_context(); + let s = &context; let m = s.map(); if let Ok(id) = s.layer_id(&json!({"layer": v})) { return Ok(id); @@ -257,7 +252,7 @@ fn below(m: &Map, group: u32, parent: Option, moving: &[u32]) -> Option CmdResult { let id = groupable(s, s.layer_id(p)?)?; let m = s.map(); - let into = group_above(m, m.group_of(id).map(|g| g.id), Outline::Layer(id))?; + let into = indent_destination(m, Outline::Layer(id))?; s.map_mut().place_layer(id, Some(into), None)?; Ok(json!({"layer": id, "group": into})) } @@ -265,9 +260,7 @@ fn indent_layer(s: &mut Session, p: &Value) -> CmdResult { fn outdent_layer(s: &mut Session, p: &Value) -> CmdResult { let id = s.layer_id(p)?; let m = s.map(); - let g = m.group_of(id).ok_or("the layer isn't in a group")?; - let (group, parent) = (g.id, g.parent); - let before = below(m, group, parent, &[id]); + let (parent, before) = outdent_layer_destination(m, id)?; s.map_mut().place_layer(id, parent, before)?; Ok(json!({"layer": id, "group": parent})) } @@ -275,8 +268,7 @@ fn outdent_layer(s: &mut Session, p: &Value) -> CmdResult { fn indent_group(s: &mut Session, p: &Value) -> CmdResult { let id = group_id(s.map(), p)?; let m = s.map(); - let parent = m.group(id).and_then(|g| g.parent); - let into = group_above(m, parent, Outline::Group(id))?; + let into = indent_destination(m, Outline::Group(id))?; s.map_mut().place_group(id, Some(into), None)?; Ok(json!({"group": id, "parent": into})) } @@ -284,9 +276,7 @@ fn indent_group(s: &mut Session, p: &Value) -> CmdResult { fn outdent_group(s: &mut Session, p: &Value) -> CmdResult { let id = group_id(s.map(), p)?; let m = s.map(); - let parent = m.group(id).and_then(|g| g.parent).ok_or("the group isn't in another group")?; - let up = m.group(parent).and_then(|g| g.parent); - let before = below(m, parent, up, &m.group_layers(id)); + let (up, before) = outdent_group_destination(m, id)?; s.map_mut().place_group(id, up, before)?; Ok(json!({"group": id, "parent": up})) } @@ -311,11 +301,7 @@ fn remove(s: &mut Session, p: &Value) -> CmdResult { } fn move_to_group(s: &mut Session, p: &Value) -> CmdResult { - let group = match p.get("group") { - None => return Err("give the group's name or id (\"group\"), or null to take the layer out of its group".into()), - Some(Value::Null) => None, - Some(_) => Some(group_id(s.map(), p)?), - }; + let group = destination_group(s.map(), p)?; // Several (a list, or the layer selection): each in or out of the group, one undo step. let ids = super::layer::layer_ids(s, p)?; if ids.len() > 1 { @@ -349,3 +335,117 @@ fn move_to_group(s: &mut Session, p: &Value) -> CmdResult { } Ok(json!({"layer": layer, "group": group})) } + +/// Resolves and deduplicates groupable layer names/ids in the actual insertion order. +fn added_layers(input: &impl super::InputSource, p: &Value) -> Result, String> { + let context = input.input_context(); + let s = &context; + let mut children = Vec::new(); + if let Some(layers) = p.get("layers") { + let layers = layers.as_array().ok_or("layers must be a list of layer names or ids")?; + for v in layers { + let id = groupable(input, s.layer_id(&json!({"layer": v}))?)?; + if !children.contains(&id) { + children.push(id); + } + } + } + Ok(children) +} + +/// Binds the chosen group's expanded layer subtree, including an empty group's identity. +fn group_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let id = group_id(s.map(), p)?; + let mut inputs = super::Inputs::layers(s.map().group_layers(id).iter().filter_map(|id| s.map().layer(*id)).map(|l| (s.map(), l))); + inputs.state = json!({"group":id}); + Ok(inputs) +} +/// Uses the actual insertion selector and parent resolver without editing group membership. +fn add_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let ids = added_layers(input, p)?; + let mut inputs = super::Inputs::layers(ids.iter().filter_map(|id| s.map().layer(*id)).map(|l| (s.map(), l))); + inputs.state = json!({"parent":parent_of(s.map(),p)?}); + Ok(inputs) +} +/// Binds moved members, parent and the layer-first before selector. +fn parent_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let mut inputs = group_inputs(input, p)?; + let before = p.get("before").filter(|v| !v.is_null()).map(|v| before_of(input, v)).transpose()?; + if let Some(id) = before { + inputs.layers.push((s.map().instance_id(), id)); + } + inputs.state["parent"] = json!(parent_of(s.map(), p)?); + Ok(inputs) +} +/// Uses the actual selected-layer list and layer-only before semantics. +fn layer_group_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let ids = super::layer::layer_ids(input, p)?; + let mut inputs = super::Inputs::layers(ids.iter().filter_map(|id| s.map().layer(*id)).map(|l| (s.map(), l))); + let group = destination_group(s.map(), p)?; + let before = p.get("before").filter(|v| !v.is_null()).map(|v| s.layer_id(&json!({"layer":v}))).transpose()?; + if let Some(id) = before { + inputs.layers.push((s.map().instance_id(), id)); + } + inputs.state = json!({"group":group}); + Ok(inputs) +} + +/// Distinguishes missing group from explicit null, which removes a layer from its group. +fn destination_group(map: &Map, p: &Value) -> Result, String> { + match p.get("group") { + None => Err("give the group's name or id (\"group\"), or null to take the layer out of its group".into()), + Some(Value::Null) => Ok(None), + Some(_) => group_id(map, p).map(Some), + } +} +/// Finds the existing group immediately above an outline item at its current level. +fn indent_destination(map: &Map, item: Outline) -> Result { + let parent = match item { + Outline::Layer(id) => map.group_of(id).map(|g| g.id), + Outline::Group(id) => map.group(id).and_then(|g| g.parent), + }; + group_above(map, parent, item) +} +/// Computes the parent and following layer used by the real layer outdent. +fn outdent_layer_destination(map: &Map, id: u32) -> Result<(Option, Option), String> { + let group = map.group_of(id).ok_or("the layer isn't in a group")?; + Ok((group.parent, below(map, group.id, group.parent, &[id]))) +} +/// Computes the parent and following layer used by the real group outdent. +fn outdent_group_destination(map: &Map, id: u32) -> Result<(Option, Option), String> { + let parent = map.group(id).and_then(|g| g.parent).ok_or("the group isn't in another group")?; + let up = map.group(parent).and_then(|g| g.parent); + Ok((up, below(map, parent, up, &map.group_layers(id)))) +} +/// Binds the moving subtree and the actual group selected by indentation. +fn indent_group_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let mut inputs = group_inputs(input, p)?; + inputs.state["into"] = json!(indent_destination(s.map(), Outline::Group(group_id(s.map(), p)?))?); + Ok(inputs) +} +/// Binds the moving subtree and the actual outdent placement. +fn outdent_group_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let mut inputs = group_inputs(input, p)?; + inputs.state["destination"] = json!(outdent_group_destination(s.map(), group_id(s.map(), p)?)?); + Ok(inputs) +} +/// Binds the moving layer and its current outline destination. +fn indent_layer_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let mut inputs = super::Inputs::primary(s, p)?; + inputs.state = json!({"into":indent_destination(s.map(),Outline::Layer(s.layer_id(p)?))?}); + Ok(inputs) +} +/// Binds the moving layer and its current outdent placement. +fn outdent_layer_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let mut inputs = super::Inputs::primary(s, p)?; + inputs.state = json!({"destination":outdent_layer_destination(s.map(),s.layer_id(p)?)?}); + Ok(inputs) +} diff --git a/crates/engine/src/cmd/history.rs b/crates/engine/src/cmd/history.rs index f1eed6a751..d7dc3aa01e 100644 --- a/crates/engine/src/cmd/history.rs +++ b/crates/engine/src/cmd/history.rs @@ -15,9 +15,13 @@ pub fn commands() -> Vec { vec![ C::new("gp.historyCode", "Copy Run As Code", "Analyze › Geoprocessing", code) .roles(&[]) + .effects(crate::policy::Effects::NONE) .view() .params(r#"{"run": index in the history (0 = the first run), "as"?: "python" (default)|"command" (a Script pane line)|"cli"}"#), - C::new("gp.historyScript", "Save History As Python Script", "Analyze › Geoprocessing", script).roles(&[]).view(), + C::new("gp.historyScript", "Save History As Python Script", "Analyze › Geoprocessing", script) + .roles(&[]) + .effects(crate::policy::Effects::NONE) + .view(), ] } diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index 002b2920a2..b503664367 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -38,42 +38,42 @@ fn first() -> String { pub fn commands() -> Vec { vec![ - C::new("raster.georeference", "Georeference", "Analyze › Alignment", georeference).roles(&[]).params( + C::new("raster.georeference", "Georeference", "Analyze › Alignment", georeference).inputs(raster_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layer": raster, "points"?: [[column, row, lon, lat], …] (two or more; with "transform": [column, row, x, y, on?]), "extent"?: [minLon, minLat, maxLon, maxLat], "transform"?: "similarity"|"affine"|"projective"|"polynomial2"|"polynomial3"|"spline" (fits the points and warps the raster onto a north-up grid covering the warped image; without it the points and extent only shift and scale the grid), "pointsCrs"?: EPSG code or "EPSG:n" of the points' x and y (default 4326), "crs"?: the output's (default pointsCrs), "resampling"?: "nearest"|"bilinear"|"cubic" (default bilinear), "cellSize"?: output units (default about the source pixel's size on the ground), "name"?: new layer for the result (default replaces the layer's raster), "path"?: "*.tif" (also writes the warp as a cloud-optimised GeoTIFF)}"#, ), - C::new("raster.project", "Project Raster", "Analyze › Alignment", project_raster).roles(&[]).params( + C::new("raster.project", "Project Raster", "Analyze › Alignment", project_raster).inputs(project_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layer": raster, "crs"?: EPSG code or "EPSG:n" of the output (default the layer's), "cellSize"?: number or [x, y] in the output's units (default about the layer's pixel there), "origin"?: [x, y] a cell corner falls on (default [0, 0], so grids of the same cellSize line up), "extent"?: polygon layer (its selected or all features' bounds) | [west, south, east, north] in degrees | "view" (default the layer's footprint), "grid"?: raster layer whose coordinate system, cells and extent are taken exactly (instead of crs, cellSize, origin and extent), "resampling"?: "nearest" (default) | "bilinear" | "cubic" | "average" (the mean of the pixels whose centres fall in a cell) | "mode" (their most frequent value, the least of those tied), "name"?: new layer (default " projected"), "path"?: GeoTIFF to write it to}"#, ), - C::new("raster.rasterize", "Polygons to Raster", "Analyze › Alignment", rasterize).roles(&[]).params( + C::new("raster.rasterize", "Polygons to Raster", "Analyze › Alignment", rasterize).inputs(rasterize_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layer": polygon layer (its selected or all features; a later one over an earlier), "field"?: numeric field (its values) or text field (codes 1 to n in sorted order of its values, listed in a table named after the result with classes), "value"?: number every polygon gives when there is no field (default 1), "nodata"?: number of the cells under no polygon (default 0), "crs"?: EPSG code or "EPSG:n" of the output (default the layer's), "cellSize"?: number or [x, y] in the output's units (required without grid), "origin"?: [x, y] a cell corner falls on (default [0, 0], so grids of the same cellSize line up), "extent"?: polygon layer (its selected or all features' bounds) | [west, south, east, north] in degrees | "view" (default the polygons' bounds), "grid"?: raster layer whose coordinate system, cells and extent are taken exactly (instead of crs, cellSize, origin and extent), "name"?: new layer (default " raster"), "path"?: GeoTIFF to write it to}"#, ), // A fit is a measurement of the points, not a project change: no undo step. - C::new("raster.gcpFit", "Control Point Fit", "Analyze › Alignment", gcp_fit).roles(&[]).view().params( + C::new("raster.gcpFit", "Control Point Fit", "Analyze › Alignment", gcp_fit).roles(&[]).effects(crate::policy::Effects::NONE).view().params( r#"{"points": [[column, row, x, y, on?], …] (on: 1/0 or true/false, default on), "transform"?: "similarity"|"affine"|"projective"|"polynomial2"|"polynomial3"|"spline" (default: similarity under 3 points, else affine), "compare"?: bool (fit every transform and recommend one; transform and leaveOneOut are then ignored), "leaveOneOut"?: bool (default true)}"#, ), - C::new("raster.change", "Change Detection", "Analyze › Image Analysis", change).roles(&[]) + C::new("raster.change", "Change Detection", "Analyze › Image Analysis", change).inputs(change_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"before": raster, "after": raster, "band"?: 1-based, "method"?: "difference"|"ratio"|"percent", "name"?, "ran"?: (internal: a raster the app has worked out)}"#), - C::new("raster.spectral", "Spectral Analysis", "Analyze › Image Analysis", spectral).roles(&[]) + C::new("raster.spectral", "Spectral Analysis", "Analyze › Image Analysis", spectral).inputs(spectral_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"layer": raster, "lon"?, "lat"?, "zone"?: polygon layer (the selected or first polygon)}"#), C::new("raster.classify", "Classification Wizard", "Analyze › Image Classification", |s, p| { super::workflows::made_or_ran(s, p, "raster.classify") - }).roles(&[]) + }).inputs(raster_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"layer": raster, "classes"?: 2..50, "bands"?: [1-based], "name"?: output, "ran"?: (internal: classes the app has worked out)}"#), - C::new("raster.classifySupervised", "Classification Tools", "Analyze › Image Classification", supervised).roles(&[]) + C::new("raster.classifySupervised", "Classification Tools", "Analyze › Image Classification", supervised).inputs(supervised_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"layer": raster, "training": polygon layer, "classField": field, "bands"?: [1-based], "name"?: output, "ran"?: (internal: a raster the app has worked out)}"#), - C::new("raster.index", "Indices", "Analyze › Imagery Tools", index).roles(&[]).params( + C::new("raster.index", "Indices", "Analyze › Imagery Tools", index).inputs(raster_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layer": raster, "index": "ndvi"|"ndwi"|"savi"|"nbr"|"ndbi"|"evi", "red"?, "green"?, "blue"?, "nir"?, "swir"?: band numbers, "name"?: output, "ran"?: (internal: a raster the app has worked out)}"#, ), - C::new("raster.process", "Process", "Analyze › Imagery Tools", process).roles(&[]) + C::new("raster.process", "Process", "Analyze › Imagery Tools", process).inputs(process_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"layer": raster, "operation": "clip"|"resample", "extent"?: [minLon, minLat, maxLon, maxLat] (clip; the view when omitted), "polygon"?: polygon layer (clip to its selected or all polygons; cells outside become no-data), "selectedOnly"?: bool (clip to selected polygons when true, all when false), "factor"?: n (resample: n×n pixels become one), "name"?: output, "ran"?: (internal: a raster the app has worked out)}"#), - C::new("raster.mosaic", "Mosaic Rasters", "Analyze › Imagery Tools", raster_mosaic).roles(&[]).params( + C::new("raster.mosaic", "Mosaic Rasters", "Analyze › Imagery Tools", raster_mosaic).inputs(mosaic_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( r#"{"inputs": [raster layer or file path, …] (two or more, in one coordinate system and cell size; where they overlap the first wins), "name"?: output, "path"?: GeoTIFF to write the mosaic to}"#, ), - C::new("raster.editPixels", "Pixel Editor", "Analyze › Imagery Tools", edit_pixels).roles(&[]) + C::new("raster.editPixels", "Pixel Editor", "Analyze › Imagery Tools", edit_pixels).inputs(raster_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"layer": raster, "polygon": [[lon, lat], …], "value"?: number (no-data when omitted), "band"?: 1-based (all when omitted)}"#), // Measurements are session results, not project changes: no undo step. - C::new("raster.measure", "Mensuration Tools", "Analyze › Mensuration", measure).roles(&[]).view().params( + C::new("raster.measure", "Mensuration Tools", "Analyze › Mensuration", measure).inputs(measure_inputs).roles(&[]).effects(crate::policy::Effects::NONE).view().params( r#"{"layer"?: elevation raster, "kind": "distance"|"area"|"height"|"volume", "points": [[lon, lat], …], "base"?: metres (volume)}"#, ), C::new("raster.measurements", "Results", "Analyze › Mensuration", |s, p| { @@ -81,10 +81,10 @@ pub fn commands() -> Vec { s.measurements.clear(); } Ok(Value::Array(s.measurements.clone())) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"clear"?: bool}"#), - C::new("imagery.workspace", "New Workspace", "Analyze › Ortho Mapping", new_workspace).roles(&[]).params( + C::new("imagery.workspace", "New Workspace", "Analyze › Ortho Mapping", new_workspace).inputs(workspace_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"kind": "ortho"|"reality", "name"?: string, "images": [raster layers], "cellSize"?: degrees, "method"?: "first"|"mean"}"#, ), C::new("imagery.workspaceOptions", "Workspace Options", "Analyze › Ortho Mapping", |s, p| { @@ -97,21 +97,23 @@ pub fn commands() -> Vec { w.method = method(p, "first")?; } serde_json::to_value(&*w).map_err(|e| e.to_string()) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": workspace, "cellSize"?: degrees|null, "method"?: "first"|"mean"}"#), - C::new("imagery.mosaic", "Build Mosaic", "Analyze › Ortho Mapping", mosaic).roles(&[]).params(r#"{"name": workspace, "ran"?: (internal: a raster the app has worked out)}"#), - C::new("imagery.mesh", "Export Mesh", "Analyze › Reality Mapping", mesh).roles(&[]) + C::new("imagery.mosaic", "Build Mosaic", "Analyze › Ortho Mapping", mosaic).inputs(workspace_mosaic_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"name": workspace, "ran"?: (internal: a raster the app has worked out)}"#), + C::new("imagery.mesh", "Export Mesh", "Analyze › Reality Mapping", mesh).inputs(raster_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"layer": elevation raster, "path"?: "*.obj" (the text is returned without one), "step"?: pixels between vertices}"#), ] } -fn raster_named(s: &Session, n: &str) -> Result, String> { +fn raster_named(input: &impl super::InputSource, n: &str) -> Result, String> { + let context = input.input_context(); + let s = &context; s.map().layer_by_name(n).and_then(|l| l.raster.as_ref()).and_then(|r| r.data.clone()).ok_or_else(|| format!("no raster layer '{n}'")) } pub(crate) fn raster(s: &Session, p: &Value) -> Result, String> { - raster_named(s, ps(p, "layer").ok_or("layer is required")?) + raster_named(s, raster_name(p)?) } pub(crate) fn name(p: &Value, d: String) -> String { @@ -399,7 +401,7 @@ const EXACT_WHOLE: f64 = 16_777_216.0; /// text field's code or one `value` (see `rustgis_analysis::warp::rasterize`). A new layer, and /// for a text field a table of its codes. fn rasterize(s: &mut Session, p: &Value) -> CmdResult { - let ln = ps(p, "layer").ok_or("layer is required: a polygon layer")?; + let ln = rasterize_name(p)?; let l = s.map().layer_by_name(ln).filter(|l| l.raster.is_none()).ok_or_else(|| format!("no vector layer '{ln}'"))?; let ds = &l.dataset; if ds.geometry_type != GeometryType::Polygon { @@ -593,6 +595,9 @@ fn tif_path(p: &Value) -> Result, String> { /// written there first as a cloud-optimised GeoTIFF, which becomes the layer's source. `info`, /// with the extent, the file and the name the layer got, is the result. fn add_output(s: &mut Session, out: Raster, path: Option, classes: bool, mut info: Value) -> CmdResult { + let owner = s.continuation()?; + owner.check()?; + let _progress = rustgis_analysis::control::install_progress(None); let out_name = out.name.clone(); let e = out.extent(); info["extent"] = json!([e.min_x, e.min_y, e.max_x, e.max_y]); @@ -610,6 +615,7 @@ fn add_output(s: &mut Session, out: Raster, path: Option, classes: bool, if classes && let Some(d) = rl.data.clone() { rl.style.renderer = rustgis_map::raster::unique_values(&d, 0, rustgis_map::Ramp::Harbour); } + s.check_continuation(&owner)?; let v = super::raster::add_raster(s, &out_name, rl)?; if let (Some(o), Some(i)) = (info.as_object_mut(), v.as_object()) { for (k, val) in i { @@ -635,10 +641,7 @@ fn grid_param( natural: impl FnOnce(Crs) -> Result<([f64; 4], Option), String>, ) -> Result { // A grid named as anything but a raster layer's name is refused rather than ignored. - let template = match p.get("grid") { - None | Some(Value::Null) => None, - Some(v) => Some(v.as_str().ok_or("grid is a raster layer's name")?), - }; + let template = project_grid(p)?; Ok(match template { Some(template) => { if ["crs", "cellSize", "origin", "extent"].iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())) { @@ -681,7 +684,8 @@ fn grid_param( }; let bounds = match p.get("extent") { None | Some(Value::Null) => footprint, - Some(Value::String(l)) if l != "view" => { + Some(Value::String(_)) if project_extent_name(p).is_some() => { + let l = project_extent_name(p).ok_or("extent layer is required")?; let layer = s.map().layer_by_name(l).filter(|l| l.raster.is_none()).ok_or_else(|| format!("no vector layer '{l}'"))?; let rows: Vec = if layer.selection.is_empty() { (0..layer.dataset.features.len()).collect() } else { layer.selection.clone() }; @@ -801,8 +805,9 @@ fn change(s: &mut Session, p: &Value) -> CmdResult { /// What a change detection reads of the session, and the work that compares the rasters. pub(crate) fn change_work(s: &Session, p: &Value) -> Result { - let a = raster_named(s, ps(p, "before").ok_or("before is required")?)?; - let bfr = raster_named(s, ps(p, "after").ok_or("after is required")?)?; + let [before, after] = change_names(p)?; + let a = raster_named(s, before)?; + let bfr = raster_named(s, after)?; // The method is checked before any pixels are resampled. let method = ps(p, "method").unwrap_or("difference").to_string(); if !matches!(method.as_str(), "difference" | "ratio" | "percent") { @@ -843,7 +848,7 @@ fn spectral(s: &mut Session, p: &Value) -> CmdResult { let r = raster(s, p)?; let all: Vec = (0..r.band_count()).collect(); let names: Vec = r.bands.iter().map(|b| b.name.clone()).collect(); - if let Some(z) = ps(p, "zone") { + if let Some(z) = spectral_zone(p) { let l = s .map() .layer_by_name(z) @@ -878,7 +883,7 @@ pub(crate) fn supervised_work(s: &Session, p: &Value) -> Result Result { // Before the mask, which places the polygons on the raster's coordinates. r.require_coordinates()?; - let pl = ps(p, "polygon").ok_or("polygon is a polygon layer's name")?; + let pl = clip_polygon(p).ok_or("polygon is a polygon layer's name")?; let l = s .map() .layer_by_name(pl) @@ -1142,8 +1147,8 @@ fn mosaic_name(v: &Value) -> Result<&str, &'static str> { /// The raster layer a mosaic input names, with its cells in memory. An error means the input isn't /// one. -fn mosaic_layer(s: &Session, v: &Value) -> Result, String> { - raster_named(s, mosaic_name(v)?) +fn mosaic_layer(input: &impl super::InputSource, v: &Value) -> Result, String> { + raster_named(input, mosaic_name(v)?) } /// Whether [`raster_mosaic`] reads a mosaic input from a file: it isn't a [`mosaic_layer`] and it @@ -1591,16 +1596,7 @@ fn new_workspace(s: &mut Session, p: &Value) -> CmdResult { if !matches!(kind, "ortho" | "reality") { return Err("kind is ortho or reality".into()); } - let images: Vec = p - .get("images") - .and_then(Value::as_array) - .ok_or("images is required")? - .iter() - .map(|v| { - let n = v.as_str().ok_or("images are raster layer names")?; - s.map().layer_by_name(n).filter(|l| l.is_raster()).map(|l| l.name.clone()).ok_or_else(|| format!("no raster layer '{n}'")) - }) - .collect::>()?; + let images = workspace_images(s, p)?; if images.is_empty() { return Err("give at least one image".into()); } @@ -1629,8 +1625,7 @@ fn mosaic(s: &mut Session, p: &Value) -> CmdResult { /// What a workspace's mosaic reads of the session (its images and options), and the work that /// lays them on one grid. pub(crate) fn mosaic_work(s: &Session, p: &Value) -> Result { - let n = ps(p, "name").ok_or("name is required")?; - let w = s.project.workspaces.iter().find(|w| w.name.eq_ignore_ascii_case(n)).cloned().ok_or_else(|| format!("no workspace '{n}'"))?; + let w = workspace_of(s, p)?.clone(); let imgs: Vec> = w.images.iter().map(|i| raster_named(s, i)).collect::>()?; let mut e = Extent::EMPTY; for r in &imgs { @@ -1727,3 +1722,132 @@ fn mesh(s: &mut Session, p: &Value) -> CmdResult { None => Ok(json!({"text": obj, "vertices": v, "faces": faces})), } } + +/// The literal raster/secondary inputs used by this module, without reading any pixels/files. +fn raster_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + super::Inputs::named(s.map(), [raster_name(p)?]) +} +/// Measures distance and area without a raster, or binds the explicitly selected elevation raster. +fn measure_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + super::Inputs::named(input.map(), ps(p, "layer")) +} +/// Binds both literal before/after rasters used by change detection. +fn change_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + super::Inputs::named(s.map(), change_names(p)?) +} +/// Binds the primary raster and optional literal zone polygon. +fn spectral_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + super::Inputs::named(s.map(), std::iter::once(raster_name(p)?).chain(spectral_zone(p))) +} +/// Binds the primary raster and the actual training polygon selector. +fn supervised_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + super::Inputs::named(s.map(), [raster_name(p)?, training_name(p)?]) +} +/// Binds a clip polygon only when the selected processing operation consumes one. +fn process_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let clip = clip_polygon(p); + super::Inputs::named(s.map(), std::iter::once(raster_name(p)?).chain(clip)) +} +/// Binds the grid template when present, otherwise the optional extent layer. +fn project_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + super::Inputs::named(s.map(), std::iter::once(raster_name(p)?).chain(project_grid(p)?.or_else(|| project_extent_name(p)))) +} +/// The rasterizer's literal vector selector, shared with pure dependency planning. +fn rasterize_name(p: &Value) -> Result<&str, String> { + ps(p, "layer").ok_or("layer is required: a polygon layer".into()) +} +/// Binds the exact vector selector and only the grid or extent the rasterizer consumes. +fn rasterize_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let mut inputs = super::Inputs::named(input.map(), [rasterize_name(p)?])?; + if let Some(name) = project_grid(p)?.or_else(|| project_extent_name(p)) { + inputs.extend(super::Inputs::named(input.map(), [name])?); + } + Ok(inputs) +} +/// Retains in-project raster precedence over paths using the executor's pure lookup. +fn mosaic_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let inputs = p.get("inputs").and_then(Value::as_array).ok_or("inputs are raster names or files")?; + let mut names = Vec::new(); + for v in inputs { + let name = mosaic_name(v)?; + if mosaic_layer(input, v).is_ok() { + names.push(name); + } + } + super::Inputs::named(s.map(), names) +} +/// Binds raster images validated for a newly created imagery workspace. +fn workspace_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let names = workspace_images(input, p)?; + super::Inputs::named(s.map(), names.iter().map(String::as_str)) +} +/// Resolves the stored workspace and its image names without mosaicking pixels. +fn workspace_mosaic_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let workspace = workspace_of(input, p)?; + super::Inputs::named(s.map(), workspace.images.iter().map(String::as_str)) +} + +/// The literal primary raster selector shared by handlers and their pure input metadata. +pub(crate) fn raster_name(p: &Value) -> Result<&str, String> { + ps(p, "layer").ok_or("layer is required".into()) +} +/// The literal training polygons used by imagery and deep learning. +pub(crate) fn training_name(p: &Value) -> Result<&str, String> { + ps(p, "training").ok_or("training (a polygon layer) is required".into()) +} +/// Parses the same required literal names used by change detection. +fn change_names(p: &Value) -> Result<[&str; 2], String> { + Ok([ps(p, "before").ok_or("before is required")?, ps(p, "after").ok_or("after is required")?]) +} +/// Selects only a string zone, as the spectral executor does. +fn spectral_zone(p: &Value) -> Option<&str> { + ps(p, "zone") +} +/// Only the clip operation consumes the polygon selector. +fn clip_polygon(p: &Value) -> Option<&str> { + (ps(p, "operation") == Some("clip")).then(|| ps(p, "polygon")).flatten() +} +/// A string extent other than view names a literal project vector layer. +fn project_extent_name(p: &Value) -> Option<&str> { + ps(p, "extent").filter(|n| *n != "view") +} + +/// Validates literal raster names in the workspace's declared image order. +fn workspace_images(input: &impl super::InputSource, p: &Value) -> Result, String> { + let context = input.input_context(); + let s = &context; + p.get("images") + .and_then(Value::as_array) + .ok_or("images is required")? + .iter() + .map(|v| { + let n = v.as_str().ok_or("images are raster layer names")?; + s.map().layer_by_name(n).filter(|l| l.is_raster()).map(|l| l.name.clone()).ok_or_else(|| format!("no raster layer '{n}'")) + }) + .collect::>() +} + +/// Resolves the stored workspace by its case-insensitive name. +fn workspace_of<'a>(input: &'a impl super::InputSource, p: &Value) -> Result<&'a Workspace, String> { + let context = input.input_context(); + let s = &context; + let name = ps(p, "name").ok_or("name is required")?; + s.project.workspaces.iter().find(|w| w.name.eq_ignore_ascii_case(name)).ok_or_else(|| format!("no workspace '{name}'")) +} + +/// Distinguishes omitted/null grid from a required literal raster name. +fn project_grid(p: &Value) -> Result, String> { + match p.get("grid") { + None | Some(Value::Null) => Ok(None), + Some(v) => v.as_str().map(Some).ok_or_else(|| "grid is a raster layer's name".into()), + } +} diff --git a/crates/engine/src/cmd/items.rs b/crates/engine/src/cmd/items.rs index aad82aa83e..eed853ac66 100644 --- a/crates/engine/src/cmd/items.rs +++ b/crates/engine/src/cmd/items.rs @@ -30,14 +30,14 @@ pub fn commands() -> Vec { s.project.folders.push(path.to_string()); } Ok(json!({"folders": s.project.folders})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"path": folder}"#), C::new("project.removeFolder", "Remove Folder", "Browser", |s, p| { let path = ps(p, "path").ok_or("path is required")?; let before = s.project.folders.len(); s.project.folders.retain(|f| f != path); if s.project.folders.len() == before { Err(format!("{path} isn't connected")) } else { Ok(Value::Null) } - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"path": folder}"#), C::new("project.connections", "Connections", "Browser", |s, _| { let folders: Vec = s.project.folders.iter().map(|f| json!({"path": f, "available": Path::new(f).is_dir()})).collect(); @@ -50,21 +50,21 @@ pub fn commands() -> Vec { "ramps": s.project.ramps.iter().map(|r| r.name().to_string()).collect::>(), "tasks": s.project.tasks.iter().map(|t| t.name.clone()).collect::>(), })) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view(), // Map and layer files. - C::new("map.save", "Save Map File", "Publish › Save As", |s, p| save_item(s, "map", p)).roles(&[]) + C::new("map.save", "Save Map File", "Publish › Save As", |s, p| save_item(s, "map", p)).inputs(map_save_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"path": "*.rgismap", "map"?: name}"#), - C::new("map.import", "Import", "Command Search", import_maps).roles(&[crate::ParamRole { key: "text", role: crate::Role::Document(crate::Document::Map) }]).params(r#"{"path": "*.rgismap|*.rgis|*.rgispkg" | "text": map file JSON | "data": base64 file}"#), - C::new("layer.save", "Save Layer File", "Publish › Save As", |s, p| save_item(s, "layer", p)).roles(&[]) + C::new("map.import", "Import", "Command Search", import_maps).roles(&[crate::ParamRole { key: "text", role: crate::Role::Document(crate::Document::Map) }]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params(r#"{"path": "*.rgismap|*.rgis|*.rgispkg" | "text": map file JSON | "data": base64 file}"#), + C::new("layer.save", "Save Layer File", "Publish › Save As", |s, p| save_item(s, "layer", p)).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_layer) .params(r#"{"layer"?, "path": "*.rgislyr"}"#), C::new("layer.addLayerFile", "Add Layer File", "Home › Add", |s, p| { let text = read_text(p)?; super::layer::add_layer_text(s, &text) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"path"?: "*.rgislyr", "text"?: layer file JSON, "data"?: base64 file}"#), // Styles. C::new("style.new", "New Style", "Style › Styles", |s, p| { @@ -74,7 +74,7 @@ pub fn commands() -> Vec { let item = StyleItem { name: name.clone(), geometry: l.geometry_type(), renderer: l.renderer.clone(), labels: l.labels.clone(), transparency: l.transparency }; add_style(s, item); Ok(json!({"style": name})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "name"?: string}"#), C::new("style.add", "Add Style", "Style › Styles", |s, p| { @@ -87,9 +87,9 @@ pub fn commands() -> Vec { n += 1; } Ok(json!({"added": n})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"path"?: "*.rgisstyle", "text"?: style file JSON, "data"?: base64 file}"#), - C::new("style.import", "Import Styles", "Style › Styles", import_styles).roles(&[]).params(r#"{"path"?: "*.rgislyr|*.rgismap|*.rgis", "text"?: JSON, "data"?: base64 file}"#), + C::new("style.import", "Import Styles", "Style › Styles", import_styles).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params(r#"{"path"?: "*.rgislyr|*.rgismap|*.rgis", "text"?: JSON, "data"?: base64 file}"#), C::new("style.apply", "Apply Style", "Style › Styles", |s, p| { let name = ps(p, "name").ok_or("name is required")?; let st = s.project.styles.iter().find(|t| t.name.eq_ignore_ascii_case(name)).cloned().ok_or_else(|| format!("no style '{name}'"))?; @@ -124,7 +124,7 @@ pub fn commands() -> Vec { s.notify(format!("{lost} saved label move(s) dropped with label classes style {name} doesn't have"), false); } Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "name": style}"#), C::new("style.remove", "Delete Style", "Browser", |s, p| { @@ -132,9 +132,9 @@ pub fn commands() -> Vec { let before = s.project.styles.len(); s.project.styles.retain(|t| !t.name.eq_ignore_ascii_case(name)); if s.project.styles.len() == before { Err(format!("no style '{name}'")) } else { Ok(Value::Null) } - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": style}"#), - C::new("style.save", "Save Style Items", "Publish › Save As", |s, p| save_item(s, "styles", p)).roles(&[]) + C::new("style.save", "Save Style Items", "Publish › Save As", |s, p| save_item(s, "styles", p)).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"path": "*.rgisstyle"}"#), // Custom colour ramps. @@ -167,7 +167,7 @@ pub fn commands() -> Vec { s.project.ramps.push(r); } Ok(json!({"added": names})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params( r#"{"path"?: "*.rgisramp|*.xml|*.qml|*.txt|*.cpt", "text"?: file contents, "data"?: base64 file, "file"?: file name for "text" or "data", "format"?: "rgisramp"|"qgis"|"gdal"|"cpt", "name"?: name for a single ramp, "kind"?: "sequential"|"diverging"|"qualitative"}"#, ), @@ -176,16 +176,16 @@ pub fn commands() -> Vec { let before = s.project.ramps.len(); s.project.ramps.retain(|r| !r.name().eq_ignore_ascii_case(name.trim())); if s.project.ramps.len() == before { Err(format!("no colour ramp '{name}'")) } else { Ok(Value::Null) } - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": colour ramp}"#), - C::new("ramp.save", "Save Color Ramps", "Publish › Save As", |s, p| save_item(s, "ramps", p)).roles(&[]) + C::new("ramp.save", "Save Color Ramps", "Publish › Save As", |s, p| save_item(s, "ramps", p)).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"path": "*.rgisramp"}"#), // Project templates. - C::new("project.saveTemplate", "Save Project Template", "Publish › Save As", |s, p| save_item(s, "template", p)).roles(&[]) + C::new("project.saveTemplate", "Save Project Template", "Publish › Save As", |s, p| save_item(s, "template", p)).inputs(project_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"path": "*.rgistemplate", "name"?: template name}"#), - C::new("project.newTemplate", "New Custom Template", "Publish › Save As", |s, p| save_item(s, "template", p)).roles(&[]) + C::new("project.newTemplate", "New Custom Template", "Publish › Save As", |s, p| save_item(s, "template", p)).inputs(project_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"path": "*.rgistemplate", "name"?: template name}"#), C::new("project.fromTemplate", "Open Custom Template", "Command Search", |s, p| { @@ -204,21 +204,21 @@ pub fn commands() -> Vec { super::connections::resolve_project(s); super::connections::note_waiting(s); Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Replaces).with(crate::policy::Effect::Files)) .view() .params(r#"{"path"?: "*.rgistemplate", "text"?: template JSON, "data"?: base64 file, "name"?: new project name}"#), // Packages. - C::new("project.package", "Package Project", "Publish › Package", |s, p| save_item(s, "projectPackage", p)).roles(&[]) + C::new("project.package", "Package Project", "Publish › Package", |s, p| save_item(s, "projectPackage", p)).inputs(project_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"path": "*.rgispkg"}"#), - C::new("map.package", "Package Map", "Publish › Package", |s, p| save_item(s, if b(p, "mobile").unwrap_or(false) { "mobilePackage" } else { "mapPackage" }, p)).roles(&[]) + C::new("map.package", "Package Map", "Publish › Package", |s, p| save_item(s, if b(p, "mobile").unwrap_or(false) { "mobilePackage" } else { "mapPackage" }, p)).inputs(map_package_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"path": "*.rgismpk", "mobile"?: bool (simplify geometry and drop hidden layers, for phones and tablets), "tolerance"?: degrees}"#), - C::new("gp.package", "Package Results", "Publish › Package", |s, p| save_item(s, "gpPackage", p)).roles(&[]) + C::new("gp.package", "Package Results", "Publish › Package", |s, p| save_item(s, "gpPackage", p)).inputs(gp_package_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"path": "*.rgisgpk"}"#), // Layer templates. - C::new("layer.template", "Layer Template", "Data › Layer Templates", layer_template).roles(&[]) + C::new("layer.template", "Layer Template", "Data › Layer Templates", layer_template).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"template": "points"|"lines"|"polygons"|"places"|"routes"|"areas"|"observations", "name"?: string}"#), // Tasks. C::new("task.new", "New Task", "Analyze › Tasks", |s, p| { @@ -250,14 +250,9 @@ pub fn commands() -> Vec { i += 1; name = format!("{base} {i}"); } - // History that came with an opened project is from a file too. - let from_file_history = b(p, "fromHistory").unwrap_or(false) && p.get("steps").is_none() && s.history_from_file > 0; - if !b(p, "imported").unwrap_or(false) && !from_file_history { - s.trusted_tasks.insert(name.to_ascii_lowercase()); - } s.project.tasks.push(Task { name: name.clone(), description: ps(p, "description").unwrap_or("").chars().take(2000).collect(), steps: steps.into_iter().take(200).collect() }); Ok(json!({"task": name})) - }).roles(&[crate::ParamRole { key: "steps", role: crate::Role::TaskSteps }]) + }).roles(&[crate::ParamRole { key: "steps", role: crate::Role::TaskSteps }]).effects(crate::policy::Effects::NONE) .params(r#"{"name"?: string, "description"?: string, "steps"?: [{"title", "instructions"?, "command": id, "params"?: {}}], "fromHistory"?: bool (one step per geoprocessing run), "imported"?: bool (from a file: its steps may not touch files)}"#), C::new("task.addStep", "Add Task Step", "Tasks", |s, p| { let step: TaskStep = serde_json::from_value(p.clone()).map_err(|e| format!("a step needs a title and a command: {e}"))?; @@ -270,7 +265,7 @@ pub fn commands() -> Vec { } t.steps.push(step); Ok(json!({"steps": t.steps.len()})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"task"?: name, "title": string, "instructions"?: string, "command": id, "params"?: {}}"#), // Editing a step's words, its place or the step itself. Its command and parameters stay // as they were, so a task from a file stays as untrusted as it came. @@ -295,7 +290,7 @@ pub fn commands() -> Vec { st.title = title; st.instructions = instructions; Ok(json!({"step": step})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"task"?: name, "step": number (from 0), "title"?: string, "instructions"?: string}"#), C::new("task.moveStep", "Move Task Step", "Tasks", |s, p| { let to = match p.get("to") { @@ -313,7 +308,7 @@ pub fn commands() -> Vec { let st = t.steps.remove(step); t.steps.insert(to, st); Ok(json!({"step": to})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"task"?: name, "step": number (from 0), "to": number (from 0)}"#), C::new("task.removeStep", "Delete Task Step", "Tasks", |s, p| { let step = step_index(p)?; @@ -323,9 +318,9 @@ pub fn commands() -> Vec { } t.steps.remove(step); Ok(json!({"steps": t.steps.len()})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"task"?: name, "step": number (from 0)}"#), - C::new("task.run", "Run Task", "Tasks", run_task).roles(&[]).params(r#"{"task"?: name, "step"?: number (one step; all when omitted)}"#), + C::new("task.run", "Run Task", "Tasks", run_task).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Automation)).params(r#"{"task"?: name, "step"?: number (one step; all when omitted)}"#), C::new("task.remove", "Delete Task", "Tasks", |s, p| { let name = ps(p, "task").ok_or("task is required")?; let before = s.project.tasks.len(); @@ -333,24 +328,24 @@ pub fn commands() -> Vec { // A later task by this name isn't the user's just because of its name. s.trusted_tasks.remove(&name.to_ascii_lowercase()); if s.project.tasks.len() == before { Err(format!("no task '{name}'")) } else { Ok(Value::Null) } - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"task": name}"#), - C::new("task.save", "Save Task Item", "Publish › Save As", |s, p| save_item(s, "task", p)).roles(&[]) + C::new("task.save", "Save Task Item", "Publish › Save As", |s, p| save_item(s, "task", p)).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"task"?: name, "path": "*.rgistask"}"#), C::new("task.import", "Import Task", "Analyze › Tasks", |s, p| { let text = read_text(p)?; let t: Task = serde_json::from_str(&text).map_err(|e| format!("not a RustGIS task: {e}"))?; // A task from a file is someone else's: it may not read or write files, or run scripts. - if let Some((i, st)) = t.steps.iter().enumerate().find(|(_, st)| touches_files(&st.command, &st.params)) { - return Err(format!("step {} ({}) reads or writes files or runs a script, which imported tasks can't do", i + 1, st.command)); + if let Some((i, st)) = t.steps.iter().enumerate().find(|(_, st)| touches_files(s, &st.command, &st.params)) { + return Err(format!("step {} ({}) reads or writes files, uses the network or runs other software or reserved actions, which imported tasks can't do", i + 1, st.command)); } let steps = serde_json::to_value(&t.steps).map_err(|e| e.to_string())?; s.run("task.new", &json!({"name": t.name, "description": t.description, "steps": steps, "imported": true})) - }).roles(&[crate::ParamRole { key: "text", role: crate::Role::Document(crate::Document::Task) }]) + }).roles(&[crate::ParamRole { key: "text", role: crate::Role::Document(crate::Document::Task) }]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"path"?: "*.rgistask", "text"?: task JSON, "data"?: base64 file}"#), // 3D export. - C::new("share.export3d", "Export To 3D File", "Publish › Output", export_3d).roles(&[]) + C::new("share.export3d", "Export To 3D File", "Publish › Output", export_3d).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_feature_layer) .params(r#"{"layer"?, "path": "*.obj", "heightField"?: field (extrusion height in metres), "height"?: metres, "zScale"?: number}"#), @@ -369,16 +364,8 @@ pub fn can_be_step(s: &Session, id: &str) -> bool { /// The JSON of an item from `text`, or the file at `path`. pub(crate) fn read_text(p: &Value) -> Result { - if let Some(d) = ps(p, "data") { - use base64::Engine; - if d.len() as u64 > MAX_FILE / 3 * 4 { - return Err("the file is too large".into()); - } - let bytes = base64::engine::general_purpose::STANDARD.decode(d.trim()).map_err(|_| "data must be base64".to_string())?; - if bytes.starts_with(b"PK") { - return package_text(&bytes); - } - return String::from_utf8(bytes).map_err(|_| "not a RustGIS file".to_string()); + if let Some(text) = inline_text(p)? { + return Ok(text); } match (ps(p, "text"), ps(p, "path")) { (Some(t), _) if t.len() as u64 <= MAX_FILE => Ok(t.to_string()), @@ -574,8 +561,7 @@ fn carry_moves(old: &rustgis_map::symbology::Labeling, new: &mut rustgis_map::sy (foreign, lost) } -/// The project without its features (layer schemas, symbology, layouts and styles stay), and how -/// many saved label moves went with them. +/// Data removed from a feature-less project template. struct TemplateLosses { label_moves: usize, offline_areas: usize, @@ -583,6 +569,7 @@ struct TemplateLosses { source_references: usize, } +/// The project without features or sources that would reload them; schemas and styles stay. fn template(p: &Project) -> (Project, TemplateLosses) { let mut t = p.clone(); t.gp_history.clear(); @@ -723,25 +710,17 @@ fn package_gp(s: &mut Session) -> Result, String> { crate::durable::output(&mut s.project.gp_history, None, |h| serde_json::to_vec_pretty(h).map_err(|e| e.to_string()))?; crate::durable::notify(s, &report); let mut entries = vec![("history.json".to_string(), history)]; - let mut layers = std::collections::HashSet::new(); let mut files = std::collections::HashSet::new(); - for rec in s.project.gp_history.iter().filter(|r| r.ok) { - for name in &rec.outputs { - for l in s.project.maps.iter().flat_map(|m| &m.layers).filter(|l| !l.is_raster() && l.name == *name) { - if !layers.insert(l.name.clone()) { - continue; - } - // Names that clean up to the same file name get a number. - let base = safe(&l.name); - let mut file = format!("results/{base}.geojson"); - let mut k = 1; - while !files.insert(file.to_ascii_lowercase()) { - k += 1; - file = format!("results/{base}_{k}.geojson"); - } - entries.push((file, rustgis_data::io::to_geojson(&l.dataset).to_string().into_bytes())); - } + for (_, l) in gp_result_layers(s) { + // Names that clean up to the same file name get a number. + let base = safe(&l.name); + let mut file = format!("results/{base}.geojson"); + let mut k = 1; + while !files.insert(file.to_ascii_lowercase()) { + k += 1; + file = format!("results/{base}_{k}.geojson"); } + entries.push((file, rustgis_data::io::to_geojson(&l.dataset).to_string().into_bytes())); } rustgis_data::zip::write(&entries) } @@ -773,7 +752,7 @@ fn layer_template(s: &mut Session, p: &Value) -> CmdResult { Ok(json!({"layer": id})) } -fn task_ref<'a>(s: &'a Session, p: &Value) -> Result<&'a Task, String> { +pub(crate) fn task_ref<'a>(s: &'a Session, p: &Value) -> Result<&'a Task, String> { match ps(p, "task") { Some(n) => s.project.tasks.iter().find(|t| t.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no task '{n}'")), None => s.project.tasks.last().ok_or_else(|| "the project has no tasks".to_string()), @@ -803,8 +782,15 @@ fn run_task(s: &mut Session, p: &Value) -> CmdResult { return Err("a task is already running (a step can't run tasks, even through a script or notebook)".into()); } s.task_running = true; - let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run_task_steps(s, p))) - .unwrap_or_else(|_| Err("the task failed unexpectedly".into())); + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let name = task_ref(s, p)?.name.clone(); + if s.item_trusted("task", &name) { + run_task_steps(s, p) + } else { + s.narrowed(crate::policy::Narrow::Item { kind: "task", name }, |s| run_task_steps(s, p))? + } + })) + .unwrap_or_else(|_| Err("the task failed unexpectedly".into())); s.task_running = false; result } @@ -828,16 +814,16 @@ fn run_task_steps(s: &mut Session, p: &Value) -> CmdResult { if st.command.starts_with("task.") { return Err(format!("step {} would run another task", i + 1)); } - // Tasks that came in a file (imported, or inside an opened project) can't touch files. - if !s.trusted_tasks.contains(&t.name.to_ascii_lowercase()) && touches_files(&st.command, &st.params) { + // Tasks that came in a file (imported, or inside an opened project) can't reach outside the project. + if !s.item_trusted("task", &t.name) && touches_files(s, &st.command, &st.params) { return Err(format!( - "step {} ({}) reads or writes files or runs a script, which a task from a file can't do; make a new task with that step to run it", + "step {} ({}) reads or writes files, uses the network or runs other software or reserved actions, which a task from a file can't do; make a new task with that step to run it", i + 1, st.command )); } let spec = s.registry.get(&st.command).ok_or_else(|| format!("step {} runs an unknown command '{}'", i + 1, st.command))?; - let (run, enabled, mutates) = (spec.run, spec.enabled, spec.mutates); + let (enabled, mutates) = (spec.enabled, spec.mutates); // As when run directly, a step naming a layer is checked against that layer. if st.params.get("layer").is_some() && let Ok(id) = s.layer_id(&st.params) @@ -851,9 +837,12 @@ fn run_task_steps(s: &mut Session, p: &Value) -> CmdResult { // erase the undo step for earlier edits. s.request_only = false; // A step that panics fails the task instead of the app (the whole task is one undo step). - let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(s, &st.params))) - .unwrap_or_else(|_| Err("failed unexpectedly".into())) - .map_err(|e| format!("step {} ({}): {e}", i + 1, st.title))?; + let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let op = s.authorize(&st.command, &st.params)?; + s.invoke(op, crate::policy::Txn::Shared) + })) + .unwrap_or_else(|_| Err("failed unexpectedly".into())) + .map_err(|e| format!("step {} ({}): {e}", i + 1, st.title))?; requested |= s.request_only; edited |= mutates && !s.request_only; done.push(json!({"step": i, "title": st.title, "result": r})); @@ -862,22 +851,20 @@ fn run_task_steps(s: &mut Session, p: &Value) -> CmdResult { Ok(json!({"task": t.name, "ran": done})) } -/// Whether a step may reach outside the session: a command that reads or writes files, opens -/// items or runs other steps, or a parameter that names a file, folder or address (by its key or -/// by looking like a path or URL). -pub(crate) fn touches_files(command: &str, params: &Value) -> bool { - const KEYS: &[&str] = - &["path", "folder", "dir", "file", "url", "uri", "source", "output", "connection", "database", "data", "text", "poster", "locator"]; - let reaches = |v: &Value| names_place(&layer_name_left_out(command, v), KEYS, 0); - let id = command.to_ascii_lowercase(); - let verb = id.split('.').nth(1).unwrap_or(""); - ["script.", "notebook.", "model.", "project.", "task.", "share.", "offline.", "cache.", "locator."].iter().any(|p| id.starts_with(p)) - || ["import", "export", "open", "save", "add", "package", "upload", "download"].iter().any(|w| verb.starts_with(w)) - // Its inputs may be bare file names, which don't look like paths. - || id == "raster.mosaic" - || (id == "gp.run" && params.get("tool").and_then(Value::as_str).and_then(super::gp::tool_id) - .and_then(rustgis_analysis::tool).is_some_and(super::gp::writes_files)) - || reaches(params) +/// Whether an imported step needs authority beyond in-project work. Reuse the shared command +/// and tool classifier, including network, reserved and plugin actions, while retaining the +/// conservative legacy prefix, verb and parameter checks. Undo/redo keep their existing behavior. +pub(crate) fn touches_files(s: &Session, command: &str, params: &Value) -> bool { + use crate::policy::Effect; + let e = s.registry.get(command).map(|spec| crate::policy::command_effects(spec, params)).unwrap_or(crate::policy::Effects::UNREVIEWED); + e.contains(Effect::Files) + || e.contains(Effect::Network) + || e.contains(Effect::Plugin) + || e.contains(Effect::Automation) + || e.contains(Effect::Reserved) + || e.contains(Effect::Replaces) + || e.contains(Effect::Unreviewed) + || crate::policy::imported_heuristic(command, params) } /// Parameters without a built-in tool's output layer name, which stays in the session, so a task @@ -895,7 +882,7 @@ pub(crate) fn layer_name_left_out<'a>(command: &str, params: &'a Value) -> std:: params .get("tool") .and_then(Value::as_str) - .and_then(rustgis_analysis::tool) + .and_then(|id| rustgis_analysis::resolve_published(id).map(|r| r.spec())) .is_some_and(|spec| !super::gp::writes_files(spec) && rustgis_analysis::TOOLS.iter().any(|b| std::ptr::eq(b, spec))) }; let plain = |t: &str| !t.trim().is_empty() && !t.contains(['/', '\\', '.', ':']) && !looks_like_path(t); @@ -1255,10 +1242,80 @@ fn durable_bytes(s: &mut Session, Ok(bytes) } -/// Chooses the saved map with the executor's existing name/default rule. -fn saved_map<'a>(s: &'a Session, p: &Value) -> Result<&'a Map, String> { +/// Decodes the same data-before-text choice without opening a path. +pub(crate) fn inline_text(p: &Value) -> Result, String> { + if let Some(d) = ps(p, "data") { + use base64::Engine; + if d.len() as u64 > MAX_FILE / 3 * 4 { + return Err("the file is too large".into()); + } + let bytes = base64::engine::general_purpose::STANDARD.decode(d.trim()).map_err(|_| "data must be base64".to_string())?; + if bytes.starts_with(b"PK") { + return package_text(&bytes).map(Some); + } + return String::from_utf8(bytes).map(Some).map_err(|_| "not a RustGIS file".to_string()); + } + match ps(p, "text") { + Some(t) if t.len() as u64 <= MAX_FILE => Ok(Some(t.to_string())), + Some(_) => Err("the file is too large".into()), + None => Ok(None), + } +} + +/// Saved map files accept a string name, otherwise the active map. +fn saved_map<'a>(input: &'a impl super::InputSource, p: &Value) -> Result<&'a Map, String> { + let context = input.input_context(); + let s = &context; match ps(p, "map") { Some(n) => s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no map '{n}'")), None => Ok(s.map()), } } +/// Binds the saved map's layers before optional raster embedding. +fn map_save_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let m = saved_map(input, p)?; + { + let mut inputs = super::Inputs::layers(m.layers.iter().map(|l| (m, l))); + inputs.maps.push(m.instance_id()); + Ok(inputs) + } +} +/// Packages consume the active map and all raster paths before mobile filtering. +fn map_package_inputs(input: &super::InputContext<'_>, _: &Value) -> Result { + let s = input; + let m = s.map(); + { + let mut inputs = super::Inputs::layers(m.layers.iter().map(|l| (m, l))); + inputs.maps.push(m.instance_id()); + Ok(inputs) + } +} +/// Exact-case output names, with the first feature layer in project order for each name. +fn gp_result_layers(input: &impl super::InputSource) -> Vec<(&Map, &Layer)> { + let context = input.input_context(); + let s = &context; + let mut seen = std::collections::HashSet::new(); + s.project + .gp_history + .iter() + .filter(|r| r.ok) + .flat_map(|r| &r.outputs) + .flat_map(|name| { + s.project.maps.iter().flat_map(move |m| m.layers.iter().filter(move |l| !l.is_raster() && l.name == *name).map(move |l| (m, l))) + }) + .filter(|(_, l)| seen.insert(l.name.clone())) + .collect() +} +/// Binds only the exact-case, first project result layers emitted by the package iterator. +fn gp_package_inputs(input: &super::InputContext<'_>, _: &Value) -> Result { + Ok(super::Inputs::layers(gp_result_layers(input))) +} + +/// Whole-project exports own all exported map/layer/table identities, unlike unrelated commands. +pub(crate) fn project_inputs(input: &super::InputContext<'_>, _: &Value) -> Result { + let s = input; + let mut inputs = super::Inputs::layers(s.project.maps.iter().flat_map(|m| m.layers.iter().map(move |l| (m, l)))); + inputs.maps.extend(s.project.maps.iter().map(|m| m.instance_id())); + inputs.tables.extend(s.project.tables.iter().map(|t| t.id)); + Ok(inputs) +} diff --git a/crates/engine/src/cmd/labels.rs b/crates/engine/src/cmd/labels.rs index f46a1133df..f2298aabb0 100644 --- a/crates/engine/src/cmd/labels.rs +++ b/crates/engine/src/cmd/labels.rs @@ -93,21 +93,21 @@ pub fn commands() -> Vec { let d = &mut s.map_mut().label_display; d.locked = b(p, "on").unwrap_or(!d.locked); Ok(json!({"locked": d.locked})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"on"?: bool}"#), C::new("labels.pause", "Pause", "Labeling › Map Labeling", |s, p| { let d = &mut s.map_mut().label_display; d.paused = b(p, "on").unwrap_or(!d.paused); Ok(json!({"paused": d.paused})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"on"?: bool}"#), C::new("labels.viewUnplaced", "View Unplaced", "Labeling › Map Labeling", |s, p| { let d = &mut s.map_mut().label_display; d.show_unplaced = b(p, "on").unwrap_or(!d.show_unplaced); Ok(json!({"showUnplaced": d.show_unplaced})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"on"?: bool}"#), C::new("labels.fonts", "Label Fonts", "View › Windows › Labeling", |_, p| { @@ -119,17 +119,20 @@ pub fn commands() -> Vec { let fonts: Vec<&str> = all.iter().filter(|f| f.listed && f.name.to_lowercase().contains(&search)).map(|f| f.name.as_str()).collect(); Ok(json!({"fonts": fonts})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"search"?: text in the family name} → {"fonts": [the installed font families, by name], "indexing"?: true (still being indexed: none listed yet)}"#), - C::new("labels.convert", "Convert To Annotation", "Labeling › Map Labeling", convert).roles(&[]) - .when(need_feature_layer) + C::new("labels.convert", "Convert To Annotation", "Labeling › Map Labeling", convert).roles(&[]).effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) + .when(need_feature_layer) .params(r#"{"layer"?, "name"?: string}"#), - C::new("labels.scaleRange", "Label Visibility Range", "Labeling › Label Range", scale_range).roles(&[]) - .when(need_feature_layer) + C::new("labels.scaleRange", "Label Visibility Range", "Labeling › Label Range", scale_range).roles(&[]).effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) + .when(need_feature_layer) .params(r#"{"layer"?, "class"?: name (none: the Default class, by whatever name it has), "min"?: scale|null (out beyond), "max"?: scale|null (in beyond), "clear"?: bool (clears the scale and distance limits, as Clear Limits does)}"#), - C::new("labels.distanceRange", "Label Distance Range", "Labeling › Label Range", distance_range).roles(&[]) - .when(need_feature_layer) + C::new("labels.distanceRange", "Label Distance Range", "Labeling › Label Range", distance_range).roles(&[]).effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) + .when(need_feature_layer) .params(r#"{"layer"?, "class"?: name (none: the Default class, by whatever name it has), "closest"?: metres|null, "farthest"?: metres|null, "clear"?: bool}"#), C::new("labels.placement", "Label Placement", "Labeling › Label Placement", |s, p| { let pos = match ps(p, "position") { @@ -226,7 +229,8 @@ pub fn commands() -> Vec { s.notify(note, false); } Ok(done) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) .when(need_feature_layer) .params( r#"{"layer"?, "class"?: name (none: the Default class, by whatever name it has), "position"?: "auto"|"center"|"above"|"below"|"left"|"right"|"aboveRight"|"aboveLeft"|"belowRight"|"belowLeft"|"parallel" (along a line, straight)|"curved" (along a line, following its bends) (required without offset, repeat, otherPositions, showAll or callout), "offset"?: [right, down] points (-200 to 200 each) the label moves from where its position puts it ([0, 0]: none; a label along a line isn't moved), "repeat"?: points (10 to 10000) between a line's labels, spaced out from the one at its middle, at most 32 a line (0 or null: the one at its middle; parallel and curved only), "otherPositions"?: bool (a label whose place is taken tries others: a point's round the point, corners first; a line's along the line, nearest its middle first, placed parallel or curved without a repeat; true is refused for any other line label and on polygons), "showAll"?: bool (every label drawn, over the others where they crowd), "callout"?: true|false|points (5 to 500: a label crowded off its spot moves to a free one nearby, a line drawn back to its feature at most this long; true keeps its length, or 40; false, 0 or null: none; not labels along a line or with an icon)}"#, @@ -291,7 +295,8 @@ pub fn commands() -> Vec { // Locked, the labels are locked again where they are placed next (`with_labels`: the // moves changed), as the one moved may not have been placed when the lock was taken. Ok(r) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) .when(need_feature_layer) .params(r#"{"layer"?, "class"?: name (none: the Default class), "feature": the feature's id, "to": [x, y] (longitude, latitude: where the label's text is centred) | null (back where its class puts it)}"#), C::new("labels.resetMoved", "Reset Moved", "Labeling › Label Placement", |s, p| { @@ -311,13 +316,15 @@ pub fn commands() -> Vec { // As after a move, those put back are locked again where they are placed next; with // nothing moved, nothing changes and the lock holds (`with_labels`). Ok(r) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) .when(need_feature_layer) .params(r#"{"layer"?, "class"?: name (none: the Default class), "all"?: bool (every class of the layer)}"#), // Label classes: each labels every feature its clause takes, as in ArcGIS Pro; the classes // listed first, Default last, are placed first (`Labeling`, #732). - C::new("labels.class.add", "Add Label Class", "View › Windows › Labeling", add_class).roles(&[]) - .when(need_feature_layer) + C::new("labels.class.add", "Add Label Class", "View › Windows › Labeling", add_class).roles(&[]).effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) + .when(need_feature_layer) .params(r#"{"layer"?, "name": string, "where"?: sql (a definition query's language; none takes every feature), "from"?: class (whose field and text symbol it starts with; none: the Default class)}"#), C::new("labels.class.remove", "Remove Label Class", "View › Windows › Labeling", |s, p| { let name = s_req(p, "class")?.trim().to_string(); @@ -326,7 +333,8 @@ pub fn commands() -> Vec { labels.classes.remove(i); Ok(()) }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) .when(need_feature_layer) .params(r#"{"layer"?, "class": name (not Default)}"#), // Every class but Default; none to remove is no error, so a script can start with it. @@ -338,7 +346,8 @@ pub fn commands() -> Vec { Ok(()) })?; Ok(json!({"removed": removed})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) .when(need_feature_layer) .params(r#"{"layer"?}"#), C::new("labels.class.rename", "Rename Label Class", "View › Windows › Labeling", |s, p| { @@ -349,7 +358,8 @@ pub fn commands() -> Vec { labels.class_mut(&class).ok_or_else(|| format!("no label class named '{class}'"))?.name = name; Ok(()) }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) .when(need_feature_layer) .params(r#"{"layer"?, "class": name, "name": string}"#), C::new("labels.class.update", "Update Label Class", "View › Windows › Labeling", |s, p| { @@ -364,7 +374,8 @@ pub fn commands() -> Vec { c.visible = v; } }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) .when(need_feature_layer) .params(r#"{"layer"?, "class"?: name (none: the Default class, by whatever name it has), "where"?: sql|null, "visible"?: bool}"#), C::new("labels.class.move", "Move Label Class", "View › Windows › Labeling", |s, p| { @@ -377,11 +388,13 @@ pub fn commands() -> Vec { labels.classes.insert(to, c); Ok(()) }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) .when(need_feature_layer) .params(r#"{"layer"?, "class": name (not Default), "to": number (its place among the classes listed before Default, 0 first: placed first when labels compete for room)}"#), - C::new("labels.by", "Labels by Expression", "View › Windows › Labeling", labels_by).roles(&[]) - .when(need_feature_layer) + C::new("labels.by", "Labels by Expression", "View › Windows › Labeling", labels_by).roles(&[]).effects(crate::policy::Effects::NONE) + .inputs(super::Inputs::single_layer) + .when(need_feature_layer) .params(r#"{"layer"?, "class"?: name (none: the Default class, by whatever name it has), "text"?: expression|null (each label's text, in place of the class's field; a feature it gives null or blanks for isn't labelled), "size"?: expression|null (the text's size in points, 4 to 72), "color"?: expression|null (the text's colour: '#RRGGBB', a CSS colour name or 'r,g,b[,a]' as color_rgb gives it), "angle"?: expression|null (a point's or polygon's label turned by it, in degrees clockwise; labels along lines keep the line's)} (each expression in the language of queries, per feature, @map_scale the map's scale; a key left out keeps its expression, null or "" removes it; where a size, color or angle expression gives null the class's own setting is used)"#), ] } diff --git a/crates/engine/src/cmd/layer.rs b/crates/engine/src/cmd/layer.rs index 8a90c18c1e..c5b0b6e043 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -15,45 +15,45 @@ use crate::{CmdResult, CommandSpec as C, Session}; /// Registers layer commands shared by the UI, CLI and command search. pub fn commands() -> Vec { vec![ - C::new("layer.addData", "Add Data", "Home › Add", add_data).roles(&[]) + C::new("layer.addData", "Add Data", "Home › Add", add_data).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"path": string, "name"?: string, "layer"?: "GeoPackage, GDAL, DuckDB or DXF layer", "paperSpace"?: "bool: a DXF's layout entities too", "stream"?: bool, "as"?: "scan" (a TIFF, PNG or JPEG, even a photo or a GeoTIFF, as a picture with no coordinates), "crs"?: "EPSG:… (or WKT) for Shapefile coordinates (overrides .prj), a CSV, a CityJSON model, or a GDAL or DuckDB file that doesn't say", "x"?: "CSV x/longitude column", "y"?: "CSV y/latitude column", "wkt"?: "CSV WKT geometry column", "delimiter"?: ","|";"|"\\t"|"|"}"#), - C::new("layer.addPhotos", "Add Photos", "Home › Add", add_photos).roles(&[]).params( + C::new("layer.addPhotos", "Add Photos", "Home › Add", add_photos).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( r#"{"paths"?: [JPEG photo files], "folder"?: "a folder of photos (every JPEG in it)", "name"?: "the layer (default Photos; photos join it when it exists)"}"#, ), - C::new("info.fileLayers", "List Layers in File", "Home › Add › Add Data", file_layers).roles(&[]) + C::new("info.fileLayers", "List Layers in File", "Home › Add › Add Data", file_layers).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"path": string, "crs"?: "EPSG:… for a .city.json that doesn't say (as layer.addData)"}"#), - C::new("info.fileColumns", "List Columns in File", "Home › Add › Add Data › Delimited Text", file_columns).roles(&[]).view().params( + C::new("info.fileColumns", "List Columns in File", "Home › Add › Add Data › Delimited Text", file_columns).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).view().params( r#"{"path": "a delimited text file (CSV, TSV, TXT) or a spreadsheet", "delimiter"?: ","|";"|"\\t"|"|", "layer"?: "a spreadsheet's sheet (the first by default)"}"#, ), - C::new("layer.addSql", "Add Data From SQL", "Home › Add", add_sql).roles(&[]).params( + C::new("layer.addSql", "Add Data From SQL", "Home › Add", add_sql).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files).with(crate::policy::Effect::Network)).params( r#"{"sql": "one DuckDB SELECT; DuckDB Spatial's ST_ functions, read_parquet('…'), web addresses", "name"?: string, "database"?: "a .duckdb file or a PostgreSQL connection (postgres:host=… dbname=…) whose tables the query names", "password"?: "the connection's, kept for this session only", "crs"?: "EPSG:… of a geometry that doesn't say", "stream"?: bool, "ran"?: "a result the app ran (the SQL pane)"}"#, ), - C::new("layer.addDatabase", "Add Data From Database", "Home › Add › Add Data", add_database).roles(&[]).params( + C::new("layer.addDatabase", "Add Data From Database", "Home › Add › Add Data", add_database).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)).params( r#"{"database": "postgres:host=… dbname=… user=… (or a postgresql:// address; PostGIS tables are read live), a mysql://user@host:port/database address (MySQL or MariaDB, read live; mariadb:// too), an mssql://user@host:1433/database address (SQL Server or Azure SQL, read live; ?encrypt=strict|false, ?trust-server-certificate=true for a self-signed certificate, ?instance=NAME; on Windows DOMAIN\\user or no user signs in with Windows), or a .duckdb file", "password"?: "kept for this session only, never saved", "layer"?: "a table, as table or schema.table (table:column for one of several geometry columns; default: every table with a geometry column)", "crs"?: "EPSG:… of a geometry that doesn't say", "name"?: string, "stream"?: bool}"#, ), - C::new("info.databaseTables", "List Database Tables", "Home › Add › Add Data › Database", database_tables).roles(&[]) + C::new("info.databaseTables", "List Database Tables", "Home › Add › Add Data › Database", database_tables).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)) .view() .params(r#"{"database": "as layer.addDatabase", "password"?: string}"#), - C::new("layer.defineProjection", "Define Projection", "Layers", define_projection).roles(&[]) + C::new("layer.defineProjection", "Define Projection", "Layers", define_projection).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?: name|id, "crs": "EPSG:… or WKT; reinterpret native coordinates, without rewriting the source file"}"#), - C::new("layer.loadAll", "Load All Features", "Layers", load_all).roles(&[]).when(need_streamed).params(r#"{"layer"?: name|id}"#), - C::new("layer.addUrl", "Add Data From URL", "Home › Add › Add Data", add_url).roles(&[]).params( + C::new("layer.loadAll", "Load All Features", "Layers", load_all).roles(&[]).effects(crate::policy::Effects::NONE).when(need_streamed).params(r#"{"layer"?: name|id}"#), + C::new("layer.addUrl", "Add Data From URL", "Home › Add › Add Data", add_url).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"url": "https://…/data.geojson|.csv|.parquet|.fgb|.gpkg|.zip|.shp (its .dbf fetched beside it, and its .shx, .prj and .cpg when there)|.kml|.kmz|.gpx|.tif|.jp2 (JPEG 2000: desktop, through GDAL) (the format from the address, else the contents) | a .pmtiles archive", "name"?: text, "layer"?: "one layer of a zip, KML or GPX", "zoom"?: "bool: true frames everything added; false keeps the view, even on an empty map; omitted, the first content on an empty map is framed (a GeoParquet, FlatGeobuf or cloud-optimised GeoTIFF read in place likewise; a PMTiles or MBTiles archive ignores zoom and is framed on its bounds unless it covers the whole world)", "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "stream"?, "as"? (as layer.addData), "whole"?: "bool: download a cloud-optimised GeoTIFF whole (as a raster layer) instead of reading it in place (an address without a file name is read in place when its first bytes are a cloud-optimised GeoTIFF)", "cog"?: "bool: the address is a cloud-optimised GeoTIFF whatever its name says (the app's probe found it so)", "bbox"?: "[west, south, east, north] in degrees | "view": a cloud-optimised GeoTIFF's window as a raster layer (desktop and command line), read by range at full resolution, in the file's coordinate system, with its colour table and class names; at most 64 million values (all bands)", "cellSize"?: "with bbox: the coarsest overview whose cells are at most this size is read (the file's units; metres for longitude/latitude, a degree taken as 111.32 km)", "cube"?: "bool: a data cube's file: a tile layer that follows the time slider (stac.addCube adds them), kept one when the project opens again", "start"?, "end"?, "unit"?, "interval"?, "dates"?: "a GeoTIFF address with date placeholders ({date:YYYY}, {YYYY}{MM}{DD}), one file per date: the dates it spans and steps through (as time.set), turning the time slider on", "connection"?: "the id of the saved web-service connection it comes from (as service.add)", "anonymous"?: bool (internal: a plugin-origin download must never adopt a saved login), "request"?: "number (internal: the download a plugin command left to the app, #1202)"}"#, ), - C::new("layer.addDownload", "Add Downloaded Data", "Home › Add", add_download).roles(&[]) + C::new("layer.addDownload", "Add Downloaded Data", "Home › Add", add_download).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Files)) .params(r#"{"id": a download the app finished (Home › Add › Add Data › From URL), "url": its address, "name"?: text, "layer"?: text, "zoom"?: bool, "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "stream"?, "as"? (as layer.addData), "service"?: bool (a service's features: their axes (when they came as GeoJSON) and paging are checked, and the address is kept as the layer's source), "bounds"?: [west, south, east, north] (with service: the layer's longitude/latitude bounds from its capabilities, which its axes are checked against; without bounds they are left as they come; ignored without service), "coverage"?: bool (a WCS coverage: a GeoTIFF, else the service's error, and its request kept as the layer's source), "geolibreProject"?: bool (internal: downloaded project JSON), "geolibreLayer"?: {"token", "record"} (internal: reserved imported layer and its saved style), "geolibreZarr"?: bool (internal: an imported Zarr slice as GeoTIFF), "connection"?: "as layer.addUrl", "anonymous"?: bool (internal: preserves a plugin-origin download's anonymous reads), "request"?: number (internal: the download a plugin command left to the app, #1202)}"#), - C::new("layer.addEncodedPolyline", "Add Encoded Polyline", "Home › Add", add_encoded_polyline).roles(&[]).params( + C::new("layer.addEncodedPolyline", "Add Encoded Polyline", "Home › Add", add_encoded_polyline).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"polyline": "encoded polyline text" | [texts] (one line each), "precision"?: 5 (Google) | 6 (OSRM, Valhalla), "name"?: text}"#, ), - C::new("layer.addDataText", "Add Data (from text)", "Home › Add", add_data_text).roles(&[]).params(r#"{"fileName": "name.geojson|.csv|.tsv", "text": string, "name"?: text, "x"?, "y"?, "wkt"?, "crs"?, "delimiter"? (as layer.addData, for delimited text), "layer"?, "paperSpace"? (as layer.addData, for a file of several layers)}"#), - C::new("layer.addSample", "Add Sample Data", "Home › Add › Add Data", add_sample).roles(&[]).params(r#"{"name": "countries"|"cities"|"rivers"|"lakes"}"#), - C::new("layer.remove", "Remove", "Layers", remove).roles(&[]) + C::new("layer.addDataText", "Add Data (from text)", "Home › Add", add_data_text).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"fileName": "name.geojson|.csv|.tsv", "text": string, "name"?: text, "x"?, "y"?, "wkt"?, "crs"?, "delimiter"? (as layer.addData, for delimited text), "layer"?, "paperSpace"? (as layer.addData, for a file of several layers)}"#), + C::new("layer.addSample", "Add Sample Data", "Home › Add › Add Data", add_sample).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"name": "countries"|"cities"|"rivers"|"lakes"}"#), + C::new("layer.remove", "Remove", "Layers", remove).roles(&[]).effects(crate::policy::Effects::NONE) .key("Delete") .when(need_layer) .params(r#"{"layer"?: name|id | [name|id] (default: the layer selection)}"#), - C::new("layer.select", "Select Layer", "Layers", select).roles(&[]) + C::new("layer.select", "Select Layer", "Layers", select).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"layer"?: name|id | [name|id] (default: the active layer; the last of a list becomes the active layer), "mode"?: "replace" (only these; the default) | "toggle" (each in or out of the layer selection; the last one out leaves no active layer) | "extend" (added to it)}"#) .view(), C::new("layer.rename", "Rename", "Layers", |s, p| { @@ -102,7 +102,7 @@ pub fn commands() -> Vec { // So do legends that leave it out. legends_follow_layer(s, &old, Some(&n)); Ok(r) - }).roles(&[]).params(r#"{"layer"?, "name": string}"#) + }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?, "name": string}"#) .when(need_layer), C::new("layer.visibility", "Visibility", "Layers", |s, p| { let ids = layer_ids(s, p)?; @@ -118,11 +118,11 @@ pub fn commands() -> Vec { } } Ok(Value::Null) - }).roles(&[]).params(r#"{"layer"?: name|id | [name|id] (default: the layer selection), "visible"?: bool (toggles when omitted: all take the opposite of the active layer's state)}"#) + }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?: name|id | [name|id] (default: the layer selection), "visible"?: bool (toggles when omitted: all take the opposite of the active layer's state)}"#) .when(need_layer), - C::new("layer.moveUp", "Move Up", "Layers", |s, p| move_layer(s, p, -1)).roles(&[]).params(r#"{"layer"?: name|id (default: the active layer)}"#).when(need_layer), - C::new("layer.moveDown", "Move Down", "Layers", |s, p| move_layer(s, p, 1)).roles(&[]).params(r#"{"layer"?: name|id (default: the active layer)}"#).when(need_layer), - C::new("layer.move", "Move", "Layers", move_to).roles(&[]) + C::new("layer.moveUp", "Move Up", "Layers", |s, p| move_layer(s, p, -1)).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?: name|id (default: the active layer)}"#).when(need_layer), + C::new("layer.moveDown", "Move Down", "Layers", |s, p| move_layer(s, p, 1)).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?: name|id (default: the active layer)}"#).when(need_layer), + C::new("layer.move", "Move", "Layers", move_to).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_layer) .params(r#"{"layer"?: name|id | [name|id] (default: the layer selection), "to": position (0 = top; past the end, the bottom). Several keep their order, the topmost landing at "to" as one layer would, and go only to the top or the bottom (one in a group, to its group's): with 4 layers and 2 of them, 2 or more is the bottom, 1 is refused}"#), C::new("layer.zoomTo", "Zoom To Layer", "Layers", |s, p| { @@ -133,10 +133,10 @@ pub fn commands() -> Vec { } s.zoom_to(&e); Ok(Value::Null) - }).roles(&[]).params(r#"{"layer"?: name|id | [name|id] (default: the layer selection; zooms to all of them)}"#) + }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?: name|id | [name|id] (default: the layer selection; zooms to all of them)}"#) .view() .when(need_layer), - C::new("layer.symbology", "Symbology", "Style › Drawing", symbology).roles(&[]) + C::new("layer.symbology", "Symbology", "Style › Drawing", symbology).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "type": "single"|"unique"|"graduated"|"proportional", "field"?, "expression"?: text|null (unique: class by this expression's values instead of a field; blank or null: by a field; with neither key, Unique Values keep the layer's expression), "classes"?: 1..12, "method"?: "naturalBreaks"|"equalInterval"|"quantile"|"geometricInterval"|"standardDeviation"|"arithmeticProgression"|"headTailBreaks"|"manual", "breaks"?: [numbers] (manual: the classes' upper bounds, closed by the field's largest value), "ramp"?: string}"#), C::new("layer.ramp", "Color Scheme", "Style › Drawing", |s, p| { @@ -152,9 +152,9 @@ pub fn commands() -> Vec { } } }) - }).roles(&[]).params(r#"{"layer"?, "ramp": colour ramp name}"#) + }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?, "ramp": colour ramp name}"#) .when(need_layer), - C::new("layer.classStyle", "Class Style", "Style › Drawing", class_style).roles(&[]) + C::new("layer.classStyle", "Class Style", "Style › Drawing", class_style).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params( r##"{"layer"?, "value"?: text|number (a Unique Values class), "index"?: number (a Graduated Colors class, 0 = the lowest), "other"?: true (Unique Values' ), "color"?: "#RRGGBB" (preserves polygon translucency, minimum alpha 150)|"#RRGGBBAA" (sets alpha exactly), "outline"?: "#RRGGBB"|"#RRGGBBAA", "label"?: text (the legend label; "" goes back to the value or range)}"##, @@ -185,7 +185,7 @@ pub fn commands() -> Vec { // A fill colour after No Fill is drawn, unless it is transparent itself (#1301). take_no_fill(l); }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r##"{"layer"?, "color": "#RRGGBB"|"#RRGGBBAA" (the fill; a polygon's outline is left as it is, set it with layer.outline)}"##), C::new("layer.noFill", "No Fill", "Style › Drawing", |s, p| { @@ -209,7 +209,7 @@ pub fn commands() -> Vec { l.no_fill = Some(NoFill::Symbols); } }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_polygon_features) .params(r#"{"layer"?: name|id (default: the active layer containing polygons; hides polygon fills, keeping outlines and point/line symbols)}"#), C::new("layer.outline", "Outline", "Style › Drawing", |s, p| { @@ -225,15 +225,15 @@ pub fn commands() -> Vec { } }) }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r##"{"layer"?, "color"?: "#RRGGBB"|"#RRGGBBAA", "width"?: number}"##), - C::new("layer.lineStrokes", "Line Strokes", "Style › Drawing", line_strokes).roles(&[]) + C::new("layer.lineStrokes", "Line Strokes", "Style › Drawing", line_strokes).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_lines_or_points) .params( r##"{"layer"?, "strokes": [{"color"?: "#RRGGBB"|"#RRGGBBAA"|null (null: the renderer's line colour), "width"?: points|null (null: the renderer's width), "offset"?: points (positive: right of the line's direction, negative: left), "cap"?: "round"|"flat"|"square", "join"?: "round"|"miter"|"bevel", "level"?: 0..255|null (with layer.strokeLevels: the level it draws at; null: its place in the stack), "dash"?: [points, …]|null (dash, gap, … in turn, at most 8; null: solid), "markers"?: {"mark"?: "circle"|"square"|"triangle"|"diamond"|"star"|"character"|"hash" (a mark across the line), "char"?: text (a character of a font as the marker instead of a shape), "font"?: text (the installed family it is drawn from; none: the label font), "size"?: points (a marker's size, a hash mark's length; default 4), "at"?: "interval"|"vertices"|"innerVertices"|"first"|"last"|"middle"|"segmentMiddles" or a list of them (default "interval"), "interval"?: points (default 12), "offsetAlong"?: points (from the start, back from the end, on from the middle), "rotate"?: bool (turned with the line; default true), "averageAngle"?: points (the stretch of line a marker at an interval turns with; 0: its segment), "angle"?: degrees clockwise (a hash mark at 0 crosses the line square), "outline"?: "#RRGGBB"|"#RRGGBBAA" (the markers' outline, as wide as the stroke)}|null (markers or hash marks along the line in the stroke's colour, instead of a line; cap, join and dash don't apply)}] (drawn bottom first; [] goes back to the single stroke, or for a class to the layer's strokes; on a point layer they are its marks: each stroke's markers drawn at the point in place of the symbol's, "offsetAlong" shifting a mark right and "offset" down, both turned with its "angle", and "rotate" turning it by layer.markerRotation's field), "value"?: text|number (a Unique Values class's own strokes), "index"?: number (a Graduated Colors class's, 0 = the lowest), "other"?: true (Unique Values' )}"##, ), - C::new("layer.fillLayers", "Fill Layers", "Style › Drawing", fill_layers).roles(&[]) + C::new("layer.fillLayers", "Fill Layers", "Style › Drawing", fill_layers).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_polygon_features) .params( r##"{"layer"?, "value"?|"index"?|"other"?: a class of Unique Values (its value, or other: true) or Graduated Colors (its index) to set its own fills (none: the layer's), "fills": [{"color"?: "#RRGGBB"|"#RRGGBBAA"|null (null: the feature's fill colour, so the fills follow the classes), "pattern"?: "solid" (default)|"hatch" (parallel lines)|"dots" (markers on a grid)|"gradient" (from color to color2 across the feature)|"shapeburst" (from color at the edge to color2 inside), "angle"?: degrees anticlockwise from east (hatch), "spacing"?: points between lines (hatch, 1 to 500; default 8) or points across and down between markers, a number or [across, down] (dots), "width"?: a hatch line's width in points (0 to 50; default 1), "offset"?: points across the lines (hatch) or [across, down] (dots), "marker"?: "circle"|"square"|"triangle"|"diamond"|"star" (dots), "char"?: text (dots: a character of a font as the marker instead of a shape), "font"?: text (the installed family it is drawn from; none: the label font), "size"?: a marker's size in points (default 3), "outline"?: a marker's outline colour or null, "outlineWidth"?: points, "displacement"?: [across, down] points every other row and column are shifted by (dots), "color2"?: "#RRGGBB"|"#RRGGBBAA" (gradient, shapeburst: the second colour; default white), "stops"?: [[0..1, "#RRGGBB"|"#RRGGBBAA"], …] (gradient, shapeburst: colours between the two, at most 16), "discrete"?: bool (each colour held until the next stop), "shape"?: "linear" (default)|"radial"|"conical" (gradient), "from"?: [across, down]|"centroid" (gradient: where color is, 0..1 in the feature's box; default [0.5, 0], the top middle), "to"?: [across, down]|"centroid" (where color2 is; default [0.5, 1]), "spread"?: "pad" (default)|"reflect"|"repeat" (gradient: past its points), "angle"?: degrees anticlockwise (gradient: turned round its box's middle), "inView"?: bool (gradient: from and to in the map view's box, not the feature's), "distance"?: points|null (shapeburst: how deep it shades; null or none: to the point deepest inside), "blur"?: 0..17 (shapeburst: pixels), "ignoreRings"?: bool (shapeburst: holes' edges left out)}] (bottom first, at most 8; [] for the symbol's single fill; the outline is drawn over them)}"##, @@ -263,10 +263,10 @@ pub fn commands() -> Vec { } l.marker_rotation = field.map(|field| rustgis_map::MarkerRotation { field, counterclockwise }); Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_points) .params(r#"{"layer"?, "field": numeric field name|null (each point's marker turned by the angle in it, in degrees; null: not turned), "counterclockwise"?: bool (the angles run counterclockwise, as in mathematics; default clockwise, as on a compass)}"#), - C::new("layer.symbolBy", "Symbol by Expression", "Style › Drawing", symbol_by).roles(&[]) + C::new("layer.symbolBy", "Symbol by Expression", "Style › Drawing", symbol_by).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "size"?: expression|null (a point's marker size in points), "fill"?: expression|null (the fill colour: '#RRGGBB', a CSS colour name or 'r,g,b[,a]' as color_rgb gives it), "stroke"?: expression|null (the outline colour, a line's colour), "strokeWidth"?: expression|null (the outline's width in points, a line's width), "angle"?: expression|null (point layers: each marker turned by it, in degrees clockwise, in place of layer.markerRotation's field)} (each expression in the language of queries, per feature, @map_scale the map's scale; a key left out keeps its expression, null or "" removes it; where an expression gives null the symbol's own value is drawn)"#), C::new("layer.strokeLevels", "Stroke Levels", "Style › Drawing", |s, p| { @@ -278,7 +278,7 @@ pub fn commands() -> Vec { } l.stroke_levels = on; Ok(json!({"on": on})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_lines) .params(r#"{"layer"?, "on": bool (draw every feature's stacked strokes of one level before the next, so casings join under the cores)}"#), C::new("layer.marker", "Marker", "Style › Drawing", |s, p| { @@ -308,13 +308,13 @@ pub fn commands() -> Vec { } }) }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "shape"?: "circle"|"square"|"triangle"|"diamond"|"star", "char"?: text (a character of a font as the marker, its first; sized by the font's em), "font"?: text (the installed family the character is drawn from; "" or one not installed: the label font), "size"?: number}"#), C::new("layer.transparency", "Transparency", "Style › Effects", |s, p| { let t = f(p, "value").ok_or("value (0–100) is required")?.clamp(0.0, 100.0) as f32; with(s, p, |l| l.transparency = t) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_layer) .params(r#"{"layer"?, "value": 0..100}"#), C::new("layer.scaleRange", "Visibility Range", "Style › Visibility Range", |s, p| { @@ -324,7 +324,7 @@ pub fn commands() -> Vec { l.min_scale = min; l.max_scale = max; }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_layer) .params(r#"{"layer"?, "min"?: scale (out beyond), "max"?: scale (in beyond)}"#), C::new("layer.referenceScale", "Reference Scale", "Style › Drawing", |s, p| { @@ -347,7 +347,7 @@ pub fn commands() -> Vec { } l.reference_scale = scale; Ok(json!({"scale": scale})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "scale": 1:N the symbols' and labels' sizes are set for (drawn at another scale they grow and shrink with the map)|"current" (the map's scale)|null (sizes stay as set on screen)}"#), C::new("layer.selectable", "Selectable", "Layers", |s, p| { @@ -362,18 +362,18 @@ pub fn commands() -> Vec { } } Ok(Value::Null) - }).roles(&[]).params(r#"{"layer"?: name|id | [name|id] (default: the layer selection), "on"?: bool (toggles when omitted: all take the opposite of the active layer's state)}"#) + }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?: name|id | [name|id] (default: the layer selection), "on"?: bool (toggles when omitted: all take the opposite of the active layer's state)}"#) .when(need_feature_layer), C::new("layer.expand", "Expand Legend", "Layers", |s, p| { let on = b(p, "on"); with(s, p, |l| l.expanded = on.unwrap_or(!l.expanded)) - }).roles(&[]).params(r#"{"layer"?, "on"?: bool (toggles when omitted)}"#) + }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?, "on"?: bool (toggles when omitted)}"#) .view() .when(need_layer), C::new("labels.enable", "Label", "Labeling › Labels", |s, p| { let on = b(p, "on"); with(s, p, |l| l.labels.enabled = on.unwrap_or(!l.labels.enabled)) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "on"?: bool}"#), C::new("labels.field", "Label Field", "Labeling › Label Class", |s, p| { @@ -386,7 +386,7 @@ pub fn commands() -> Vec { labels.enabled = true; Ok(()) }) - }).roles(&[]).params(r#"{"layer"?, "class"?: name (none: the Default class, by whatever name it has), "field": name}"#) + }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?, "class"?: name (none: the Default class, by whatever name it has), "field": name}"#) .when(need_feature_layer), C::new("labels.style", "Text Symbol", "Labeling › Text Symbol", |s, p| { let size = f(p, "size").map(|v| v.clamp(*LABEL_SIZE.start(), *LABEL_SIZE.end()) as f32); @@ -488,7 +488,7 @@ pub fn commands() -> Vec { c.italic = it; } }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r##"{"layer"?, "class"?: name (none: the Default class, by whatever name it has), "size"?: number, "color"?: "#RRGGBB"|"#RRGGBBAA", "halo"?: bool, "haloColor"?: "#RRGGBB"|"#RRGGBBAA"|"" (""/null: white or dark, whichever stands out from the text), "haloWidth"?: points 0-10|null (null: the thin halo), "spacing"?: points between letters (-5 to 50), "bold"?: bool, "italic"?: bool, "font"?: installed family name (labels.fonts lists them; "" is the label font), "background"?: false|null (none) | true (white, a thin dark outline) | {"fill"?: "#RRGGBB"|"#RRGGBBAA"|"" ("": none), "stroke"?: colour|"" (the outline; "": none), "strokeWidth"?: points 0-10, "padding"?: [across, up and down] points 0-50, "radius"?: corner radius in points 0-50} (a rectangle behind the text; what isn't given is kept, or the default's), "wrapChar"?: text (a line break wherever it occurs, left out; a space puts each word on its own line; "" for none), "wrapLength"?: characters (a longest line, broken between words; 0 for none; at most 500), "lineAlign"?: "left"|"center"|"right" (how a label's lines line up)}"##), C::new("layer.definitionQuery", "Definition Query", "Data › Definition Query", |s, p| { @@ -519,20 +519,20 @@ pub fn commands() -> Vec { ); } with(s, p, |l| l.definition_query = if q.is_empty() { None } else { Some(q.clone()) }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "where": "SQL clause, empty to clear"}"#), C::new("table.open", "Attribute Table", "Data › Table", |s, p| { s.table_layer = Some(s.layer_id(p)?); Ok(Value::Null) - }).roles(&[]).params(r#"{"layer"?: name|id (default: the active layer)}"#) + }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?: name|id (default: the active layer)}"#) .view() .key("Ctrl+T") .when(need_feature_layer), C::new("table.close", "Close Table", "Data › Table", |s, _| { s.table_layer = None; Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), C::new("table.remove", "Remove Table", "Data › Table", |s, p| { // The position this command took before tables had ids: refused, rather than @@ -543,13 +543,13 @@ pub fn commands() -> Vec { let i = s.table_index(p)?; s.project.tables.remove(i); Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"table": name|id (a standalone table; of tables sharing a name, the name is the first)} (the old position, "at", is refused)"#), C::new("layer.fields", "Fields", "Data › Data Design", |s, p| { let id = s.layer_id(p)?; let l = s.map().layer(id).ok_or("no layer")?; Ok(json!(l.dataset.fields.iter().map(|f| json!({"name": f.name, "type": f.ty.label(), "alias": f.alias})).collect::>())) - }).roles(&[]).params(r#"{"layer"?: name|id (default: the active layer)}"#) + }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?: name|id (default: the active layer)}"#) .view() .when(need_feature_layer), ] @@ -1462,7 +1462,7 @@ fn read_whole(p: &Value, path: &str) -> WholeRead { /// with `"stream"` given, a `.json` with a coordinate system or layer given, a large GeoJSON that /// isn't a FeatureCollection). #[cfg(not(target_arch = "wasm32"))] -pub(crate) fn add_data_ahead(p: &Value) -> Option { +pub(crate) fn add_data_ahead(p: &Value) -> Option { use super::reads::ahead; const NAME: &str = "Add Data"; let path = gdb_path(ps(p, "path")?).into_owned(); @@ -1499,7 +1499,7 @@ pub(crate) fn add_data_ahead(p: &Value) -> Option { } #[cfg(target_arch = "wasm32")] -pub(crate) fn add_data_ahead(_: &Value) -> Option { +pub(crate) fn add_data_ahead(_: &Value) -> Option { None } @@ -2811,7 +2811,7 @@ fn load_all_key(l: &rustgis_map::Layer) -> String { /// Load All's read of the streamed layer `p` names, made before it runs ([`super::reads`]), on a /// copy of the layer. -pub(crate) fn load_all_ahead(s: &Session, p: &Value) -> Option { +pub(crate) fn load_all_ahead(s: &Session, p: &Value) -> Option { let l = s.map().layer(s.layer_id(p).ok()?)?; let mut copy = l.clone(); let source = copy.source.as_mut()?; @@ -3084,6 +3084,29 @@ pub fn url_file_name(url: &str, bytes: &[u8]) -> Result { } } +/// Identifies a bounded data download using the same filename and companion rules as its reader. +/// Discovery, content-sniffed imports and external drivers keep conservative account checks. +pub(crate) fn fixed_download_urls(p: &Value) -> Option> { + let url = p.get("url")?.as_str()?; + if url.is_empty() + || url.trim() != url + || ["service", "coverage"].iter().any(|key| p.get(key).is_some_and(|v| !v.is_null() && v.as_bool() != Some(false))) + || ["geolibreProject", "geolibreLayer", "geolibreZarr"].iter().any(|key| p.get(key).is_some()) + || rustgis_data::time::is_template(url) + { + return None; + } + let name = url_file_name(url, &[]).ok()?; + if rustgis_data::raster::is_jpeg2000_name(&name) { + return None; + } + let mut urls = vec![url.to_string()]; + if let Some(parts) = rustgis_data::io::shapefile_part_urls(url) { + urls.extend(parts.into_iter().map(|(_, url)| url)); + } + Some(urls) +} + /// Downloads data from a web address and adds it (the command line, MCP and the assistant; the /// app downloads off the frame and adds the bytes). In the browser, where nothing may wait, the /// request is kept for the app ([`Session::request_url`]), which downloads and adds it as its own @@ -3091,13 +3114,16 @@ pub fn url_file_name(url: &str, bytes: &[u8]) -> Result { /// Its errors quote addresses without their keys: the assistant's and MCP's tools return them as /// they are. pub(crate) fn add_url(s: &mut Session, p: &Value) -> CmdResult { - add_url_as_given(s, p).map_err(|e| crate::sources::redact_text(&e)) + let owner = s.continuation()?; + let out = add_url_as_given(s, p).map_err(|e| crate::sources::redact_text(&e))?; + s.check_continuation(&owner)?; + Ok(out) } fn add_url_as_given(s: &mut Session, p: &Value) -> CmdResult { let bound = crate::service_logins::request_params(s, p)?; let p = bound.as_ref(); - if ["geolibreProject", "geolibreLayer", "geolibreZarr"].iter().any(|key| p.get(key).is_some()) { + if ["geolibreProject", "geolibreLayer", "geolibreZarr"].iter().any(|key| p.get(key).is_some()) && !s.import_completion(p) { return Err("GeoLibre download state is internal; use project.importGeolibre".into()); } let url = ps(p, "url").map(str::trim).filter(|u| !u.is_empty()).ok_or("url is required")?; @@ -3449,6 +3475,7 @@ fn download_shapefile_blocking(url: &str) -> Result, String> { Err(e) => return Err(part_failed(&u, over_shared_limit(e))), } } + cache::check_still(&cache::current_still())?; zipped_within(&name, shp, got, left).map_err(|e| part_failed(url, e)) } @@ -4486,7 +4513,9 @@ const MAX_LAYER_LIST: usize = 10_000; /// The layers a command acts on: `layer` as a name or id, or a list of them; else the layer /// selection (the active layer alone when nothing else is picked). -pub(crate) fn layer_ids(s: &Session, p: &Value) -> Result, String> { +pub(crate) fn layer_ids(input: &impl super::InputSource, p: &Value) -> Result, String> { + let context = input.input_context(); + let s = &context; match p.get("layer") { Some(Value::Array(list)) => { if list.is_empty() { @@ -4858,3 +4887,12 @@ fn class_style(s: &mut Session, p: &Value) -> CmdResult { let e = l.renderer.set_class_style(&class, &style, g)?; Ok(json!({"label": e.label, "color": e.symbol.fill.hex(), "outline": e.symbol.stroke.hex()})) } + +/// These loaders interpret `layer` inside an external file/database, never as a map layer. +/// The selector remains frozen with its path/address, including the loader's empty default. +pub(crate) fn external_dataset_selector(command: &str) -> bool { + matches!( + command, + "layer.addData" | "layer.addDatabase" | "info.fileColumns" | "layer.addUrl" | "layer.addDownload" | "layer.addDataText" + ) +} diff --git a/crates/engine/src/cmd/layer_copy.rs b/crates/engine/src/cmd/layer_copy.rs index e46ce3cc11..7e9983a3e8 100644 --- a/crates/engine/src/cmd/layer_copy.rs +++ b/crates/engine/src/cmd/layer_copy.rs @@ -28,10 +28,10 @@ pub struct LayerClip { pub fn commands() -> Vec { vec![ - C::new("layer.copy", "Copy Layers", "Layers", copy).roles(&[]).view().params( + C::new("layer.copy", "Copy Layers", "Layers", copy).inputs(copy_inputs).roles(&[]).effects(crate::policy::Effects::NONE).view().params( r#"{"layer"?: name|id | [name|id, …] (default: the active layer, unless a group is given), "group"?: name|id | [name|id, …] (the group with all its layers and subgroups)}"#, ), - C::new("layer.paste", "Paste Layers", "Layers", paste).roles(&[]).when(need_copied).params( + C::new("layer.paste", "Paste Layers", "Layers", paste).inputs(paste_inputs).roles(&[]).effects(crate::policy::Effects::NONE).when(need_copied).params( r#"{"map"?: name (default: the map in view), "group"?: name|id (into that group, at its top; copied groups go in it too), "to"?: position (0 = top, the default; never inside a group: above it)}"#, ), ] @@ -51,39 +51,8 @@ fn list(v: &Value) -> Result, String> { } fn copy(s: &mut Session, p: &Value) -> CmdResult { - let mut ids: Vec = Vec::new(); - let mut groups: Vec = Vec::new(); - if let Some(v) = p.get("group").filter(|v| !v.is_null()) { - for g in list(v)? { - let id = super::group::group_id(s.map(), &json!({"group": g}))?; - let m = s.map(); - // The group and its subgroups, outermost first, each once. - let mut tree = vec![id]; - tree.extend(m.subgroups(id)); - tree.sort_by_key(|g| m.group_depth(*g)); - for gid in tree { - let Some(group) = m.group(gid) else { continue }; - if groups.iter().all(|x| x.id != gid) { - ids.extend(&group.children); - groups.push(group.clone()); - } - } - } - } - match p.get("layer") { - Some(v @ Value::Array(_)) => { - for v in list(v)? { - ids.push(s.layer_id(&json!({"layer": v}))?); - } - } - Some(v) if !v.is_null() => ids.push(s.layer_id(p)?), - // The active layer by default, unless only groups are copied. - _ if groups.is_empty() => ids.push(s.layer_id(p)?), - _ => {} - } + let (ids, groups) = copy_selection(s, p)?; let m = s.map(); - ids.sort_by_key(|id| m.layer_index(*id)); - ids.dedup(); let layers: Vec = ids .iter() .filter_map(|id| m.layer(*id)) @@ -189,14 +158,10 @@ fn paste(s: &mut Session, p: &Value) -> CmdResult { } // A mask follows its layer when that was pasted too; one the map lacks is cleared. Masks are // found as the renderer finds them: the first polygon layer of that name, case aside (ASCII). - let polygon = |m: &Map, id: u32| m.layer(id).is_some_and(|l| l.geometry_type() == GeometryType::Polygon); let mut unmasked: Vec = Vec::new(); for id in &pasted { let Some(mask) = m.layer(*id).and_then(|l| l.mask.clone()) else { continue }; - let own = copied.iter().find(|(was, c)| was.eq_ignore_ascii_case(&mask) && polygon(m, *c)).map(|(_, c)| *c); - let found = own.or_else(|| { - m.layers.iter().find(|l| l.name.eq_ignore_ascii_case(&mask) && l.geometry_type() == GeometryType::Polygon).map(|l| l.id) - }); + let found = mask_target(&mask, copied.iter().filter_map(|(name, id)| m.layer(*id).map(|l| (name.as_str(), l))), m).map(|l| l.id); let target = found.and_then(|c| m.layer(c)).map(|l| l.name.clone()); if let Some(l) = m.layer_mut(*id) { if target.is_none() { @@ -237,3 +202,76 @@ fn paste(s: &mut Session, p: &Value) -> CmdResult { } Ok(json!({"map": map, "layers": pasted, "groups": group_ids, "unmasked": unmasked})) } + +/// Resolves explicit layer lists and expanded groups with copy's precise active-layer fallback. +fn copy_selection(input: &impl super::InputSource, p: &Value) -> Result<(Vec, Vec), String> { + let context = input.input_context(); + let s = &context; + let mut ids: Vec = Vec::new(); + let mut groups: Vec = Vec::new(); + if let Some(v) = p.get("group").filter(|v| !v.is_null()) { + for g in list(v)? { + let id = super::group::group_id(s.map(), &json!({"group": g}))?; + let m = s.map(); + // The group and its subgroups, outermost first, each once. + let mut tree = vec![id]; + tree.extend(m.subgroups(id)); + tree.sort_by_key(|g| m.group_depth(*g)); + for gid in tree { + let Some(group) = m.group(gid) else { continue }; + if groups.iter().all(|x| x.id != gid) { + ids.extend(&group.children); + groups.push(group.clone()); + } + } + } + } + match p.get("layer") { + Some(v @ Value::Array(_)) => { + for v in list(v)? { + ids.push(s.layer_id(&json!({"layer": v}))?); + } + } + Some(v) if !v.is_null() => ids.push(s.layer_id(p)?), + // The active layer by default, unless only groups are copied. + _ if groups.is_empty() => ids.push(s.layer_id(p)?), + _ => {} + } + let m = s.map(); + ids.sort_by_key(|id| m.layer_index(*id)); + ids.dedup(); + Ok((ids, groups)) +} + +/// Binds the copied layer subtree and copied group membership without changing the clipboard. +fn copy_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let (ids, groups) = copy_selection(input, p)?; + let mut inputs = super::Inputs::layers(ids.iter().filter_map(|id| s.map().layer(*id)).map(|l| (s.map(), l))); + inputs.state = json!(groups.iter().map(|g| (g.id, g.parent, &g.children)).collect::>()); + Ok(inputs) +} +/// Binds detached clipboard incarnations, destination and existing mask dependencies without rebinding ids. +fn paste_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let clip = s.layer_clipboard.as_ref().ok_or("no layers have been copied")?; + let map = s.project.maps.get(super::navigate::map_at(input, p)?).ok_or("no map")?; + let group = p.get("group").filter(|v| !v.is_null()).map(|_| super::group::group_id(map, p)).transpose()?; + let masks = clip.layers.iter().filter_map(|l| l.mask.as_ref()).filter_map(|name| { + let found = mask_target(name, clip.layers.iter().map(|l| (l.name.as_str(), l)), map)?; + map.layers.iter().find(|l| std::ptr::eq(*l, found)) + }); + let mut inputs = super::Inputs::layers(masks.map(|l| (map, l))); + inputs.maps.push(map.instance_id()); + inputs.state = json!({"group":group,"clipboard":clip.layers.iter().map(|l|(l.instance_id(),&l.name,&l.mask,crate::policy::input_source(l))).collect::>(),"groups":clip.groups.iter().map(|g|(g.id,g.parent,&g.children)).collect::>()}); + Ok(inputs) +} + +/// Copied polygons win over the destination's first matching polygon, exactly as paste remaps masks. +fn mask_target<'a>(name: &str, copied: impl IntoIterator, destination: &'a Map) -> Option<&'a Layer> { + copied + .into_iter() + .find(|(was, l)| was.eq_ignore_ascii_case(name) && l.geometry_type() == GeometryType::Polygon) + .map(|(_, l)| l) + .or_else(|| destination.layers.iter().find(|l| l.name.eq_ignore_ascii_case(name) && l.geometry_type() == GeometryType::Polygon)) +} diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index fa134886bc..7a45b5c4fc 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -25,26 +25,26 @@ const MAX_LAYOUT: u64 = 64 << 20; pub fn commands() -> Vec { vec![ - C::new("layout.new", "New Layout", "Home › Project", new_layout).roles(&[]).params( + C::new("layout.new", "New Layout", "Home › Project", new_layout).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"name"?: string, "page"?: "letter"|"legal"|"tabloid"|"a5"|"a4"|"a3"|"a2"|"a1"|"a0", "orientation"?: "landscape"|"portrait", "width"?: mm, "height"?: mm, "template"?: "map" (frame, title, legend, north arrow, scale bar; the default)|"blank"}"#, ), - C::new("layout.publication", "New Publication Layout", "Publish › Layout", publication).roles(&[]).params( + C::new("layout.publication", "New Publication Layout", "Publish › Layout", publication).inputs(publication_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"profile"?: "a4" (the default) | "a3" | "a2" (a sheet turned to portrait or landscape, whichever the area's map comes out larger on: its extent east to west against north to south, on the map's projection) | "a4-portrait"|"a4-landscape"|"a3-portrait"|"a3-landscape"|"a2-portrait"|"a2-landscape" (a sheet: frame, title, subtitle, the map, a boxed footer)|"figure-1col" (90 mm)|"figure-1.5col" (140 mm)|"figure-2col" (190 mm)|"thesis-full" (160 × 225 mm)|"thesis-half" (160 × 110 mm) (a figure: the map edge to edge, its title and sources left to the caption), "extent"?: [west, south, east, north] in degrees that must fit (default: the map view), "name"?: string, "title"?: text, "subtitle"?: text, "sources"?: text | [texts], "reference"?: text | [texts] (default: the projection, datum and graticule), "note"?: text, "author"?: text (default: "Made with RustGIS · {date}"; "" or [] for none), "headings"?: [the sources' heading, the reference's heading, an editorial column's heading], "locator"?: "globe" (a globe with the area outlined; the default on sheets) | "inset" (the surrounding region, the area outlined) | "none" (the default on figures), "decimal"?: "." (the default) | "," (graticule, scale bar and colorbar numbers and {scale} with a decimal comma: 0,05 and 1:6.300.000), "style"?: "classic" (the default: the title over the map, the footer under it) | "banner" (the title and subtitle in a band of the accent colour across the top) | "panel" (the map at the left, a panel at the right with the title, locator, legend, sources and reference) | "editorial" (the title large in serif, a column at the right with the note, the locator and the legend) | "modular" (cards on a light page: the map, the locator and the legend each in one, the sources and reference in tinted ones) | "poster" (the page in the accent colour, the title in light text, the map in a light card, the footer in darker blocks) (a sheet's arrangement; figures leave it to the caption), "accent"?: a colour as #RRGGBB (the style's colour: its band, panel tint, headings and rules, a poster's page), "arrangement"?: "best" (the default: the favourite and each arrangement by name laid out, the one moving the least area off its corners kept, then the one leaving the least over the data, the favourite on a tie) | "favourite" (layout.arrangement's) | an arrangement by name or corner by corner, as layout.arrangement takes (the corners the locator, north arrow, legend and scale bar are at home in; one covering what the map draws there still moves)} → a new layout of the active map at a round scale, its texts wrapped to their boxes; {projection}, {datum} and {grid} in the texts are filled from the map"#, ), - C::new("layout.arrangement", "Favourite Arrangement", "Publish › Layout", arrangement).roles(&[]).view().params( + C::new("layout.arrangement", "Favourite Arrangement", "Publish › Layout", arrangement).roles(&[]).effects(crate::policy::Effects::NONE).view().params( r#"{"favourite"?: "classic" (the locator top left, the north arrow top right, the legend bottom left, the scale bar bottom right) | "mirrored" | "legendRight" | "legendTop" | {"locator", "northArrow", "legend", "scaleBar": "top-left"|"top-right"|"bottom-left"|"bottom-right", each a corner of its own}} → the favourite arrangement of publication layouts (kept with the app's preferences), its corners and the arrangements by name; with no favourite given, it is only shown"#, ), C::new("layout.activate", "Open Layout", "Browser", |s, p| { let i = index(s, &by_name(p))?; s.active_layout = Some(i); Ok(json!({"layout": i})) - }).roles(&[]) + }).inputs(named_object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"name": string | "layout": name|index}"#), C::new("layout.close", "Show Map", "Browser", |s, _| { s.active_layout = None; Ok(Value::Null) - }).roles(&[]) + }).inputs(close_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .view(), C::new("layout.remove", "Delete Layout", "Browser", |s, p| { let i = index(s, &by_name(p))?; @@ -56,7 +56,7 @@ pub fn commands() -> Vec { _ => None, }; Ok(Value::Null) - }).roles(&[]) + }).inputs(named_object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name"?: string, "layout"?: name|index (instead of name; default: the open layout)}"#), C::new("layout.rename", "Rename Layout", "Browser", |s, p| { let name = ps(p, "newName").map(str::trim).filter(|n| !n.is_empty()).ok_or("newName is required")?; @@ -67,7 +67,7 @@ pub fn commands() -> Vec { l.name = unique.clone(); } Ok(json!({"name": unique})) - }).roles(&[]) + }).inputs(named_object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name"?: string, "layout"?: name|index (instead of name; default: the open layout), "newName": string}"#), // A copy of a layout with all its elements (#288), right after it and open. Element ids // are per layout, so legends and scale bars stay on their frames, which show the same maps. @@ -79,27 +79,27 @@ pub fn commands() -> Vec { s.project.layouts.insert(i + 1, l); s.active_layout = Some(i + 1); Ok(json!({"name": name, "layout": i + 1})) - }).roles(&[]) + }).inputs(named_object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name"?: string, "layout"?: name|index (instead of name; default: the open layout), "newName"?: string (default: " copy")}"#), - C::new("layout.page", "Page Size", "Publish › Page Setup", page).roles(&[]).params( + C::new("layout.page", "Page Size", "Publish › Page Setup", page).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layout"?, "page"?: page id, "orientation"?: "landscape"|"portrait", "width"?: mm, "height"?: mm, "decimal"?: "."|"," (the decimal mark of the numbers its surrounds print)}"#, ), - C::new("layout.addFrame", "Map Frame", "Publish › Map Frames", add_frame).roles(&[]).params( + C::new("layout.addFrame", "Map Frame", "Publish › Map Frames", add_frame).inputs(add_frame_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layout"?, "shape"?: "rectangle"|"circle"|"ellipse"|"polygon"|"lasso", "points"?: [[u, v], …] (polygon/lasso, 0..1 across the frame), "map"?: name, "x"?, "y"?, "w"?, "h"?: mm}"#, ), - C::new("layout.addElement", "Add Element", "Publish › Map Surrounds", add_element).roles(&[]).params( + C::new("layout.addElement", "Add Element", "Publish › Map Surrounds", add_element).inputs(element_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( r##"{"layout"?, "kind": "northArrow"|"scaleBar"|"legend"|"colorbar"|"text"|"dynamicText"|"picture"|"table"|"chart"|"rectangle" (a neatline framing the page unless any of x, y, w, h is given)|"ellipse"|"circle"|"line"|"arrow"|"extentIndicator"|"grid", "frame"?: element id (default: the first map frame), "of"?: frame id (extentIndicator, which also takes "color" and "width" in points, default red and 2), "text"?, "size"?, "color"?: "#RRGGBB"|"#RRGGBBAA" (text, dynamicText, line, arrow, rectangle, ellipse, circle: its outline; extentIndicator; grid; scaleBar; refused for other kinds), "bold"?: bool (text), "italic"?: bool, "align"?: "left"|"center"|"right", "letterSpacing"?: points, -5 to 50 (text, dynamicText), "font"?: "sans"|"serif"|an installed font family's name, as labels.fonts lists them (text, dynamicText; a family not installed here is drawn in its fallback, and a serif where none is installed in the sans, with a warning; refused for other kinds), "fallback"?: "sans"|"serif" (a named font's, where it isn't installed; default its own class, else sans), "title"?: legend title (default "Legend"), "allClasses"?: bool (legend: every class, not only those the frame draws), "hideOther"?: bool (legend: leave out ), "exclude"?: [layer names] (legend: layers it leaves out though the map draws them), "orientation"?: "horizontal"|"vertical" (colorbar), "ticks"?: 2..21 (colorbar: labelled values, ends included), "tickSide"?: "below"|"above"|"right"|"left" (colorbar), "reverse"?: bool (colorbar: high values first), "fill"?: "#RRGGBBAA" (rectangle, ellipse, circle, northArrow; scaleBar: the alternating bar's light divisions, default white; refused for other kinds), "width"?: outline width in points (rectangle, ellipse, circle; 0 for none) or line width in points (line, grid), "radius"?: mm, 0 to 50 (rectangle: rounded corners), "dir"?: "horizontal"|"vertical"|"falling"|"rising" (line), "head"?: "none"|"end"|"start"|"both" (line; default "end" for an arrow), "angle"?: degrees anticlockwise from pointing right, centred on the page unless x or y is given (line, arrow; it sets the box and the direction, so w, h and dir are not used with it), "length"?: mm (line, arrow), "image"?: base64, "path"?: picture file, "layer"?: (table, chart; colorbar: a raster layer, default the top visible one), "field"?, "label"?, "rows"?, "step"?: degrees, or a measured grid's units (metres) (0 picks one), "type"?: "graticule" (latitudes and longitudes, default) | "measured" (eastings and northings in a projected system, as ArcGIS Pro's measured grid), "crs"?: a measured grid's coordinate system: EPSG:25832, WKT or a PROJ string ("" or none: the map's when projected, else the UTM zone at the frame's centre), "short"?: bool (a measured grid's labels as their kilometres' last two digits, 43 for 543000), "labels"?: bool, "style"?: "lines"|"ticks"|"linesAndTicks" (grid) or "classic"|"compass"|"triangle"|"star" (northArrow) or "alternating"|"stepped"|"line" (scaleBar), "tickLength"?: mm, "format"?: "decimal"|"dms", "hemisphere"?: bool (N/S/E/W rather than a sign), "edges"?: ["top","bottom","left","right"] (labelled edges), "labelOrientation"?: "horizontal"|"vertical", "off"?: bool (grid; options not given keep the grid's), "outline"?: "#RRGGBB" (northArrow, or a rectangle's, ellipse's or circle's outline instead of color; refused for other kinds), "units"?: "auto"|"kilometers"|"meters"|"miles"|"feet", "divisions"?: 1..10 (scaleBar, whose "color" is its lines, dark divisions and labels, default #222222), "x"?, "y"?, "w"?, "h"?: mm}"##, ), - C::new("layout.update", "Element Properties", "Publish › Element", update).roles(&[]).params( + C::new("layout.update", "Element Properties", "Publish › Element", update).inputs(update_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( r##"{"layout"?, "element": id, "x"?, "y"?, "w"?, "h"?: mm, "name"?, "visible"?, "locked"?: bool (a locked element takes no other change until unlocked; "locked": false unlocks it and may come with changes), "text"?, "size"?, "color"? (text, line, rectangle, ellipse: its outline; scaleBar: its lines, dark divisions and labels; refused for other elements), "bold"?, "italic"?, "align"?, "letterSpacing"?, "font"?: "sans"|"serif"|an installed font family's name, "fallback"?: "sans"|"serif" (text, as in layout.addElement; refused for other elements), "title"?, "allClasses"?: bool (legend), "hideOther"?: bool (legend), "exclude"?: [layer names] (legend; [] lists every layer again), "orientation"?, "ticks"?, "tickSide"?, "reverse"?: (colorbar), "style"?: (northArrow, scaleBar), "fill"?, "outline"? (northArrow, rectangle, ellipse, and a scaleBar's fill; refused for other elements; for a rectangle or ellipse, color sets it when outline is not given; a scaleBar's fill is the alternating bar's light divisions), "radius"?: mm (rectangle: rounded corners), "width"?: points (rectangle, ellipse, line), "dir"?, "head"?, "angle"?, "length"? (line: turned or sized about its centre, then moved by any x or y; dir does not come with angle or length), "units"?, "divisions"? (scaleBar), "map"?, "scale"?, "center"?: [lon, lat], "bearing"?: degrees clockwise from north, "pitch"?: degrees of tilt (0 to 85), "grid"?: {grid options as in layout.addElement} | false (map frame), "extentIndicator"?: {"color"?, "width"?: points} | false (map frame: its extent indicator's style, or removes it), "layer"?: (colorbar: "" for the top visible raster), "field"?, "rows"?}"##, ), - C::new("layout.guides", "Guides", "Publish › Page Setup", guides).roles(&[]).params( + C::new("layout.guides", "Guides", "Publish › Page Setup", guides).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layout"?, "x"?: [mm] (vertical guides, from the page's left edge), "y"?: [mm] (horizontal guides, from its top edge), "clear"?: bool (remove every guide first)}"#, ), - C::new("layout.duplicateElement", "Duplicate Element", "Publish › Element", duplicate_element).roles(&[]).params( + C::new("layout.duplicateElement", "Duplicate Element", "Publish › Element", duplicate_element).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layout"?, "element": id, "dx"?, "dy"?: mm (offset of the copy, default 5)}"#, ), - C::new("layout.arrange", "Arrange Element", "Publish › Element", arrange).roles(&[]).params( + C::new("layout.arrange", "Arrange Element", "Publish › Element", arrange).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layout"?, "element": id, "to": "front"|"forward"|"backward"|"back" (later elements are drawn over earlier ones: front puts it over every other, forward over the one above it, backward under the one below, back under every other)}"#, ), C::new("layout.removeElement", "Delete Element", "Publish › Element", |s, p| { @@ -110,18 +110,18 @@ pub fn commands() -> Vec { unlocked(l, locked)?; } if l.remove(id) { Ok(Value::Null) } else { Err(format!("no element {id}")) } - }).roles(&[]) + }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"layout"?, "element": id}"#), - C::new("layout.frameZoom", "Zoom Frame", "Publish › Map Frame", frame_zoom).roles(&[]).params( + C::new("layout.frameZoom", "Zoom Frame", "Publish › Map Frame", frame_zoom).inputs(frame_zoom_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layout"?, "element"?: frame id, "frame"?: (as element), "to"?: "view" (the map view's extent, bearing and pitch; the default)|"full"|"selection"|"layer", "layer"?}"#, ), - C::new("layout.series", "Map Series", "Publish › Map Frames", series).roles(&[]).params( + C::new("layout.series", "Map Series", "Publish › Map Frames", series).inputs(series_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layout"?, "layer": index layer, "nameField"?: string, "frame"?: id, "margin"?: 0..1, "off"?: bool}"#, ), - C::new("layout.seriesPage", "Map Series Page", "Publish › Map Frames", series_page).roles(&[]).params(r#"{"layout"?, "page": number|"next"|"previous"}"#), - C::new("layout.seriesRefresh", "Refresh Map Series", "Publish › Map Frames", |s, p| series_page(s, &json!({"layout": p.get("layout"), "page": "current"}))).roles(&[]) + C::new("layout.seriesPage", "Map Series Page", "Publish › Map Frames", series_page).inputs(series_page_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layout"?, "page": number|"next"|"previous"}"#), + C::new("layout.seriesRefresh", "Refresh Map Series", "Publish › Map Frames", |s, p| series_page(s, &json!({"layout": p.get("layout"), "page": "current"}))).inputs(series_page_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"layout"?}"#), - C::new("layout.export", "Export Layout", "Publish › Output", |s, p| super::exports::write_here(s, p, "layout.export")).roles(&[]) + C::new("layout.export", "Export Layout", "Publish › Output", |s, p| super::exports::write_here(s, p, "layout.export")).inputs(layout_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"layout"?, "path": "*.png|*.pdf|*.svg" (PNG/SVG: current page; SVG: each element a group of its own; text and shapes as SVG, map frames and surrounds as vectors, rasters and tiles in them as pictures at dpi), "dpi"?: 72..600, "allPages"?: bool (PDF only; default true: every map series page, up to 500; false: the current page)}"#), C::new("layout.save", "Save Layout File", "Publish › Save As", |s, p| { @@ -130,7 +130,7 @@ pub fn commands() -> Vec { let text = serde_json::to_string_pretty(&l).map_err(|e| e.to_string())?; rustgis_data::io::write_atomic(Path::new(path), text.as_bytes()).map_err(|e| format!("{path}: {e}"))?; Ok(json!({"path": path})) - }).roles(&[]) + }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"layout"?, "path": "*.rgislayout"}"#), C::new("layout.import", "Import Layout", "Home › Project", |s, p| { @@ -143,13 +143,15 @@ pub fn commands() -> Vec { (None, None) => return Err("path or text is required".into()), }; import(s, &text) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"path"?: "*.rgislayout", "text"?: layout JSON}"#), ] } /// The layout named or numbered by `layout`, else the active one, else the only one. -fn index(s: &Session, p: &Value) -> Result { +fn index(input: &impl super::InputSource, p: &Value) -> Result { + let context = input.input_context(); + let s = &context; let ls = &s.project.layouts; // A `layout` that is neither a name nor an index is an error, never the open layout. match p.get("layout") { @@ -856,21 +858,7 @@ fn landscape_suits(aspect: f64, short: f64, long: f64, style: Style) -> bool { /// subtitle and a footer of two boxed columns (sources, cartographic reference) with a note and /// authorship under them, each text wrapped to its box with the layout's own font. fn publication(s: &mut Session, p: &Value) -> CmdResult { - let id = match p.get("profile").filter(|v| !v.is_null()) { - None => "a4".to_string(), - Some(v) => v.as_str().map(|t| t.trim().to_ascii_lowercase()).ok_or("profile is a profile's name, such as \"a4-portrait\"")?, - }; - // A sheet's size alone ("a4", the default): its orientation chosen below, from the area's - // shape, portrait until then. - let auto = matches!(id.as_str(), "a4" | "a3" | "a2").then(|| id.clone()); - let id = match &auto { - Some(size) => format!("{size}-portrait"), - None => id, - }; - let profile = PUBLICATION_PROFILES.iter().find(|pr| pr.id == id).ok_or_else(|| { - let names: Vec<&str> = PUBLICATION_PROFILES.iter().map(|pr| pr.id).collect(); - format!("unknown profile '{id}': a4, a3 or a2 (turned to the area's shape), {}", names.join(", ")) - })?; + let (auto, profile) = publication_profile(p)?; let (sheet, page_w, page_h) = (profile.sheet, profile.width, profile.height); let extent = match p.get("extent").filter(|v| !v.is_null()) { // The view; on a globe the whole Earth, as geoprocessing takes it (the globe camera's flat @@ -906,21 +894,7 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { }; let headings = texts_of(p, "headings")?.unwrap_or_default(); let comma = decimal_mark(p)?.unwrap_or(false); - let locator = match p.get("locator").filter(|v| !v.is_null()) { - None => { - if sheet { - Locator::Globe - } else { - Locator::None - } - } - Some(v) => match v.as_str().map(str::trim) { - Some("globe") => Locator::Globe, - Some("inset") => Locator::Inset, - Some("none") => Locator::None, - _ => return Err("locator is \"globe\", \"inset\" or \"none\"".into()), - }, - }; + let locator = publication_locator(p, sheet)?; // A style or an accent that isn't text is refused, as an unknown one is, rather than left out. for k in ["style", "accent"] { if p.get(k).is_some_and(|v| !v.is_null() && !v.is_string()) { @@ -1605,27 +1579,7 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { // A locator an earlier sheet made that no layout shows any more (its layout deleted) // is replaced, so sheets made again and again don't leave a trail of them; one still // shown is kept, and the new one numbered beside it. - let shown = |n: &str| { - s.project - .layouts - .iter() - .flat_map(|l| &l.elements) - .any(|e| matches!(&e.kind, ElementKind::MapFrame { map, .. } if map.eq_ignore_ascii_case(n))) - }; - let ours = |n: &str| { - n.strip_prefix(base.as_str()) - .is_some_and(|rest| rest.is_empty() || rest.strip_prefix(' ').is_some_and(|k| k.parse::().is_ok())) - }; - // And made as this command makes them: no web tiles, only unlabelled polygon layers, - // so a globe map made by hand under that name is left alone. - let made_here = |x: &rustgis_map::Map| { - x.tiles.is_empty() && x.layers.iter().all(|l| l.geometry_type() == rustgis_data::GeometryType::Polygon && !l.labels.enabled) - }; - let orphan = s - .project - .maps - .iter() - .position(|x| x.projection.is_globe() && ours(&x.name) && !shown(&x.name) && x.name != src.name && made_here(x)); + let orphan = locator_orphan(s); let name = match orphan.and_then(|i| s.project.maps.get(i)) { Some(x) => x.name.clone(), None => numbered(&base, |n| s.project.maps.iter().any(|x| x.name.eq_ignore_ascii_case(n))), @@ -2168,12 +2122,7 @@ fn add_frame(s: &mut Session, p: &Value) -> CmdResult { } o => return Err(format!("unknown frame shape '{o}'")), }; - let map = match ps(p, "map") { - Some(n) => { - s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(n)).map(|m| m.name.clone()).ok_or_else(|| format!("no map '{n}'"))? - } - None => s.map().name.clone(), - }; + let map = added_frame_map(s, p)?.name.clone(); let i = index(s, p)?; let l = s.project.layouts.get(i).ok_or("no layout")?; let n = l.elements.len() as f64; @@ -2246,18 +2195,6 @@ fn add_element(s: &mut Session, p: &Value) -> CmdResult { } let i = index(s, p)?; let image = if kind == "picture" { Some(super::graphics::picture(p)?) } else { None }; - let layer_name = |s: &Session, l: &Layout, frame: u32| -> Result { - let map = match l.element(frame).map(|e| &e.kind) { - Some(ElementKind::MapFrame { map, .. }) => map.clone(), - _ => return Err("no such frame".into()), - }; - let m = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(&map)).ok_or_else(|| format!("no map '{map}'"))?; - let found = match ps(p, "layer") { - Some(n) => m.layer_by_name(n), - None => m.layers.iter().find(|l| !l.is_raster() && !l.graphics), - }; - found.map(|l| l.name.clone()).ok_or_else(|| "no such layer in the frame's map".to_string()) - }; let l = s.project.layouts.get(i).ok_or("no layout")?; let (pw, ph) = (l.width, l.height); let (name, rect, k) = match kind { @@ -2325,14 +2262,14 @@ fn add_element(s: &mut Session, p: &Value) -> CmdResult { } "table" => { let frame = frame_of(l, p)?; - let layer = layer_name(s, l, frame)?; + let layer = frame_layer_name(s, l, frame, p)?; let rows = p.get("rows").and_then(Value::as_u64).unwrap_or(12).clamp(1, 500) as usize; ("Table Frame", place(p, l, (15.0, ph * 0.6, pw * 0.5, ph * 0.35))?, ElementKind::Table { frame, layer, rows }) } "chart" => { let frame = frame_of(l, p)?; - let layer = layer_name(s, l, frame)?; - let m = s.project.maps.iter().find_map(|m| m.layer_by_name(&layer)).ok_or("no such layer")?; + let layer = frame_layer_name(s, l, frame, p)?; + let (_, m) = chart_field_layer(s, &layer).ok_or("no such layer")?; let field = match ps(p, "field") { Some(fl) => m .dataset @@ -2463,7 +2400,9 @@ fn add_element(s: &mut Session, p: &Value) -> CmdResult { /// The raster layer a colorbar on `frame` shows: `name` checked against the frame's map, or the /// map's top visible raster (`None` when the map has none yet). -fn colorbar_layer(s: &Session, l: &Layout, frame: u32, name: Option<&str>) -> Result, String> { +fn colorbar_layer(input: &impl super::InputSource, l: &Layout, frame: u32, name: Option<&str>) -> Result, String> { + let context = input.input_context(); + let s = &context; let map = match l.element(frame).map(|e| &e.kind) { Some(ElementKind::MapFrame { map, .. }) => map.clone(), _ => return Err("no such frame".into()), @@ -2763,7 +2702,9 @@ fn font_warning(font: FontFamily, name: Option<&str>, fallback_given: bool, styl /// A legend's `exclude` from `p`: the names of layers in its frame's map, as the map spells them; /// `None` when not given. Not a list of names, or a name the map hasn't, is an error. -fn legend_exclude(s: &Session, l: &Layout, frame: u32, p: &Value) -> Result>, String> { +fn legend_exclude(input: &impl super::InputSource, l: &Layout, frame: u32, p: &Value) -> Result>, String> { + let context = input.input_context(); + let s = &context; let Some(v) = p.get("exclude").filter(|v| !v.is_null()) else { return Ok(None) }; let names = v.as_array().ok_or("exclude is a list of layer names")?; if names.len() > rustgis_map::layout::MAX_LEGEND_EXCLUDED { @@ -2886,50 +2827,7 @@ fn update(s: &mut Session, p: &Value) -> CmdResult { unlocked(s.project.layouts.get(li).ok_or("no layout")?, id)?; } // A new layer (tables and charts) or field (charts) must exist in the frame's map. - let checked = { - let l = s.project.layouts.get(li).ok_or("no layout")?; - let e = l.element(id).ok_or_else(|| format!("no element {id}"))?; - match (&e.kind, ps(p, "layer"), ps(p, "field")) { - (ElementKind::Table { frame, layer, .. } | ElementKind::Chart { frame, layer, .. }, nl, nf) if nl.is_some() || nf.is_some() => { - let map = match l.element(*frame).map(|f| &f.kind) { - Some(ElementKind::MapFrame { map, .. }) => map.clone(), - _ => return Err("the element's map frame is gone".into()), - }; - let m = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(&map)).ok_or_else(|| format!("no map '{map}'"))?; - let target = m - .layer_by_name(nl.unwrap_or(layer)) - .filter(|l| !l.is_raster()) - .ok_or_else(|| format!("no feature layer '{}' in {map}", nl.unwrap_or(layer)))?; - // Only charts have a field. - let field = match nf.filter(|_| matches!(e.kind, ElementKind::Chart { .. })) { - Some(fl) => Some( - target - .dataset - .field_index(fl) - .and_then(|i| target.dataset.fields.get(i)) - .filter(|f| f.ty.is_numeric()) - .map(|f| f.name.clone()) - .ok_or_else(|| format!("no numeric field '{fl}' in {}", target.name))?, - ), - // A chart moved to another layer keeps its field only if that layer has it as a - // number; otherwise it charts the layer's first numeric field. - None => match &e.kind { - ElementKind::Chart { field, .. } if nl.is_some() => { - let numeric = |f: &&rustgis_data::Field| f.ty.is_numeric(); - let keep = target.dataset.field_index(field).and_then(|i| target.dataset.fields.get(i)).filter(numeric); - let f = keep - .or_else(|| target.dataset.fields.iter().find(numeric)) - .ok_or_else(|| format!("{} has no numeric field to chart", target.name))?; - Some(f.name.clone()) - } - _ => None, - }, - }; - Some((target.name.clone(), field)) - } - _ => None, - } - }; + let checked = checked_update(s, p, li, id)?; // A colorbar's new layer must be a raster in its frame's map; empty follows the top raster. let colorbar = { let l = s.project.layouts.get(li).ok_or("no layout")?; @@ -3240,16 +3138,9 @@ fn set_frame(s: &mut Session, li: usize, frame: u32, e: &Extent) -> Result<(), S fn frame_zoom(s: &mut Session, p: &Value) -> CmdResult { let li = index(s, p)?; let l = s.project.layouts.get(li).ok_or("no layout")?; - let frame = match p.get("element") { - Some(_) => frame_of(l, &json!({"frame": p.get("element")}))?, - None => frame_of(l, p)?, - }; + let frame = zoom_frame(l, p)?; unlocked(l, frame)?; - let map = match l.element(frame).map(|e| &e.kind) { - Some(ElementKind::MapFrame { map, .. }) => map.clone(), - _ => return Err("no such frame".into()), - }; - let m = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(&map)).ok_or_else(|| format!("no map '{map}'"))?; + let m = frame_map(s, l, frame)?; match ps(p, "to").unwrap_or("view") { "view" => { let w = l.element(frame).map(|e| e.w).unwrap_or(100.0); @@ -3300,7 +3191,7 @@ fn series(s: &mut Session, p: &Value) -> CmdResult { _ => return Err("no such frame".into()), }; let n = ps(p, "layer").ok_or("layer is required")?; - let m = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(&map)).ok_or_else(|| format!("no map '{map}'"))?; + let m = frame_map(s, l, frame)?; let layer = m.layer_by_name(n).filter(|l| !l.is_raster()).ok_or_else(|| format!("no feature layer '{n}' in {map}"))?; if layer.dataset.features.is_empty() { return Err("the index layer has no features".into()); @@ -3336,7 +3227,9 @@ pub fn series_info(s: &Session, li: usize, page: usize) -> Option<(usize, String } /// A map series' index layer and its name field. -fn series_layer(s: &Session, li: usize) -> Option<(&rustgis_map::Layer, Option)> { +fn series_layer(input: &impl super::InputSource, li: usize) -> Option<(&rustgis_map::Layer, Option)> { + let context = input.input_context(); + let s = &context; let l = s.project.layouts.get(li)?; let sr = l.series.as_ref()?; let map = match l.element(sr.frame).map(|e| &e.kind) { @@ -3614,3 +3507,362 @@ pub(crate) fn import(s: &mut Session, text: &str) -> CmdResult { s.active_layout = Some(i); Ok(json!({"layout": i, "name": name})) } + +/// Selects the explicit or first feature layer in the stored frame's map. +fn frame_layer_name(input: &impl super::InputSource, l: &Layout, frame: u32, p: &Value) -> Result { + let context = input.input_context(); + let s = &context; + let map = match l.element(frame).map(|e| &e.kind) { + Some(ElementKind::MapFrame { map, .. }) => map.clone(), + _ => return Err("no such frame".into()), + }; + let m = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(&map)).ok_or_else(|| format!("no map '{map}'"))?; + let found = match ps(p, "layer") { + Some(n) => m.layer_by_name(n), + None => m.layers.iter().find(|l| !l.is_raster() && !l.graphics), + }; + found.map(|l| l.name.clone()).ok_or_else(|| "no such layer in the frame's map".to_string()) +} + +/// Resolves a frame's stored map literally; missing frame maps are errors here. +fn frame_map<'a>(input: &'a impl super::InputSource, l: &Layout, frame: u32) -> Result<&'a rustgis_map::Map, String> { + let context = input.input_context(); + let s = &context; + let map = match l.element(frame).map(|e| &e.kind) { + Some(ElementKind::MapFrame { map, .. }) => map, + _ => return Err("no such frame".into()), + }; + s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(map)).ok_or_else(|| format!("no map '{map}'")) +} +/// Binds the chosen frame map and the layers whose extent this zoom consumes. +fn frame_zoom_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let layout = s.project.layouts.get(index(input, p)?).ok_or("no layout")?; + let frame = zoom_frame(layout, p)?; + let map = frame_map(input, layout, frame)?; + let mut inputs = match ps(p, "to").unwrap_or("view") { + "layer" => super::Inputs::named(map, [ps(p, "layer").ok_or("layer is required")?])?, + "full" | "selection" => super::Inputs::layers(map.layers.iter().map(|l| (map, l))), + _ => super::Inputs::layers(std::iter::empty()), + }; + inputs.maps.push(map.instance_id()); + inputs.state = json!({"layout":index(input,p)?,"frame":frame}); + Ok(inputs) +} +/// Binds a new series to its actual frame map, unless the call only turns it off. +fn series_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + if b(p, "off") == Some(true) { + return Ok(super::Inputs::layers(std::iter::empty())); + } + let layout = s.project.layouts.get(index(input, p)?).ok_or("no layout")?; + let frame = frame_of(layout, p)?; + let map = frame_map(input, layout, frame)?; + let mut inputs = super::Inputs::named(map, [ps(p, "layer").ok_or("layer is required")?])?; + inputs.maps.push(map.instance_id()); + inputs.state = json!({"layout":index(input,p)?,"frame":frame}); + Ok(inputs) +} +/// Binds the stored series frame/layer before changing its current page. +fn series_page_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let li = index(input, p)?; + let (layer, _) = series_layer(input, li).ok_or("the layout has no map series")?; + let mut inputs = + super::Inputs::layers(s.project.maps.iter().flat_map(|m| m.layers.iter().filter(|l| std::ptr::eq(*l, layer)).map(move |l| (m, l)))); + inputs.state = json!({"layout":li,"series":s.project.layouts.get(li).and_then(|l|l.series.as_ref())}); + Ok(inputs) +} +/// Collects only the selectors the requested surround validates, including chart field precedence. +fn element_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let kind = ps(p, "kind").ok_or("kind is required")?; + if !matches!(kind, "table" | "chart" | "colorbar" | "legend") { + let mut inputs = object_inputs(input, p)?; + if matches!(kind, "northArrow" | "scaleBar" | "extentIndicator" | "grid") { + let layout = s.project.layouts.get(index(input, p)?).ok_or("no layout")?; + inputs.state["frame"] = json!(frame_of(layout, p)?); + } + return Ok(inputs); + } + let layout = s.project.layouts.get(index(input, p)?).ok_or("no layout")?; + let frame = frame_of(layout, p)?; + if kind == "legend" && legend_exclude(input, layout, frame, p)?.is_none() { + let mut inputs = object_inputs(input, p)?; + inputs.state["frame"] = json!(frame); + return Ok(inputs); + } + let map = frame_map(input, layout, frame)?; + let mut inputs = super::Inputs::layers(std::iter::empty()); + if matches!(kind, "table" | "chart") { + let name = frame_layer_name(input, layout, frame, p)?; + inputs.extend(super::Inputs::named(map, [name.as_str()])?); + if kind == "chart" + && let Some((m, l)) = chart_field_layer(input, &name) + { + inputs.layers.push((m.instance_id(), l.id)); + } + } else if kind == "colorbar" { + if let Some(name) = colorbar_layer(input, layout, frame, ps(p, "layer"))? { + inputs.extend(super::Inputs::named(map, [name.as_str()])?); + } + } else if let Some(names) = legend_exclude(input, layout, frame, p)? { + inputs.layers.extend(names.iter().filter_map(|name| map.layer_by_name(name)).map(|l| (map.instance_id(), l.id))); + // Tile exclusions are stored selector text, not invented layer/table identities. + inputs.state = json!(names); + } + inputs.maps.push(map.instance_id()); + inputs.state = json!({"layout":index(input,p)?,"frame":frame,"selectors":inputs.state}); + Ok(inputs) +} +/// Resolves the same active/name/index layout used by export before its dependency walk. +fn layout_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + Ok(render_inputs_at(input, index(input, p)?)) +} +/// Collects only maps actually referenced by renderer frames; never renders or opens a source. +pub(crate) fn render_inputs_at(input: &impl super::InputSource, li: usize) -> super::Inputs { + let context = input.input_context(); + let s = &context; + let Some(layout) = s.project.layouts.get(li) else { return super::Inputs::unresolved() }; + let mut inputs = super::Inputs::layers(std::iter::empty()); + for element in &layout.elements { + // A missing stored map renders a placeholder, and consumes no map/layer source. + if let ElementKind::MapFrame { map, .. } = &element.kind + && let Some(map) = rustgis_render::layout::map_index(&s.project.maps, map).and_then(|i| s.project.maps.get(i)) + { + inputs.extend(super::tiles::render_inputs(map)); + } + } + inputs.state = json!({"layout":li,"name":layout.name,"selectors":layout.elements.iter().filter_map(|e| element_selectors(&e.kind).map(|v|(e.id,v))).collect::>(),"series":layout.series,"tiles":inputs.state}); + inputs +} + +/// Charts find fields in the first matching project layer, separately from the frame's layer. +fn chart_field_layer<'a>(input: &'a impl super::InputSource, name: &str) -> Option<(&'a rustgis_map::Map, &'a rustgis_map::Layer)> { + let context = input.input_context(); + let s = &context; + s.project.maps.iter().find_map(|map| map.layer_by_name(name).map(|layer| (map, layer))) +} + +/// Finds only the generated, unreferenced globe map that publication may replace. +fn locator_orphan(input: &impl super::InputSource) -> Option { + let context = input.input_context(); + let s = &context; + let src = s.map(); + let base = format!("{} Locator", src.name); + let shown = |n: &str| { + s.project + .layouts + .iter() + .flat_map(|l| &l.elements) + .any(|e| matches!(&e.kind, ElementKind::MapFrame { map, .. } if map.eq_ignore_ascii_case(n))) + }; + let ours = |n: &str| { + n.strip_prefix(base.as_str()) + .is_some_and(|rest| rest.is_empty() || rest.strip_prefix(' ').is_some_and(|k| k.parse::().is_ok())) + }; + // And made as this command makes them: no web tiles, only unlabelled polygon layers, + // so a globe map made by hand under that name is left alone. + let made_here = |x: &rustgis_map::Map| { + x.tiles.is_empty() && x.layers.iter().all(|l| l.geometry_type() == rustgis_data::GeometryType::Polygon && !l.labels.enabled) + }; + s.project.maps.iter().position(|x| x.projection.is_globe() && ours(&x.name) && !shown(&x.name) && x.name != src.name && made_here(x)) +} + +/// Parses the publication's locator choice with the sheet-specific default. +fn publication_locator(p: &Value, sheet: bool) -> Result { + Ok(match p.get("locator").filter(|v| !v.is_null()) { + None => { + if sheet { + Locator::Globe + } else { + Locator::None + } + } + Some(v) => match v.as_str().map(str::trim) { + Some("globe") => Locator::Globe, + Some("inset") => Locator::Inset, + Some("none") => Locator::None, + _ => return Err("locator is \"globe\", \"inset\" or \"none\"".into()), + }, + }) +} + +/// Binds the active shown inputs and the optional generated map replacement. +fn publication_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let (_, profile) = publication_profile(p)?; + let locator = publication_locator(p, profile.sheet)?; + let map = s.map(); + let mut inputs = super::Inputs::layers(map.layers.iter().filter(|l| map.shows(l)).map(|l| (map, l))); + inputs.maps.push(map.instance_id()); + if matches!(locator, Locator::Globe) { + if let Some(old) = locator_orphan(input).and_then(|i| s.project.maps.get(i)) { + inputs.maps.push(old.instance_id()); + inputs.layers.extend(old.layers.iter().map(|l| (old.instance_id(), l.id))); + } else { + inputs.state = json!({"locator":"new map"}); + } + } + Ok(inputs) +} + +/// Resolves the profile and automatic sheet orientation without laying out or rendering. +fn publication_profile(p: &Value) -> Result<(Option, &'static PublicationProfile), String> { + let id = match p.get("profile").filter(|v| !v.is_null()) { + None => "a4".to_string(), + Some(v) => v.as_str().map(|t| t.trim().to_ascii_lowercase()).ok_or("profile is a profile's name, such as \"a4-portrait\"")?, + }; + // A sheet's size alone ("a4", the default): its orientation chosen below, from the area's + // shape, portrait until then. + let auto = matches!(id.as_str(), "a4" | "a3" | "a2").then(|| id.clone()); + let id = match &auto { + Some(size) => format!("{size}-portrait"), + None => id, + }; + let profile = PUBLICATION_PROFILES.iter().find(|pr| pr.id == id).ok_or_else(|| { + let names: Vec<&str> = PUBLICATION_PROFILES.iter().map(|pr| pr.id).collect(); + format!("unknown profile '{id}': a4, a3 or a2 (turned to the area's shape), {}", names.join(", ")) + })?; + Ok((auto, profile)) +} + +/// Validates a table/chart's effective layer and field before mutation; input binding uses this same choice. +fn checked_update(input: &impl super::InputSource, p: &Value, li: usize, id: u32) -> Result)>, String> { + let context = input.input_context(); + let s = &context; + Ok({ + let l = s.project.layouts.get(li).ok_or("no layout")?; + let e = l.element(id).ok_or_else(|| format!("no element {id}"))?; + match (&e.kind, ps(p, "layer"), ps(p, "field")) { + (ElementKind::Table { frame, layer, .. } | ElementKind::Chart { frame, layer, .. }, nl, nf) if nl.is_some() || nf.is_some() => { + let map = match l.element(*frame).map(|f| &f.kind) { + Some(ElementKind::MapFrame { map, .. }) => map.clone(), + _ => return Err("the element's map frame is gone".into()), + }; + let m = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(&map)).ok_or_else(|| format!("no map '{map}'"))?; + let target = m + .layer_by_name(nl.unwrap_or(layer)) + .filter(|l| !l.is_raster()) + .ok_or_else(|| format!("no feature layer '{}' in {map}", nl.unwrap_or(layer)))?; + // Only charts have a field. + let field = match nf.filter(|_| matches!(e.kind, ElementKind::Chart { .. })) { + Some(fl) => Some( + target + .dataset + .field_index(fl) + .and_then(|i| target.dataset.fields.get(i)) + .filter(|f| f.ty.is_numeric()) + .map(|f| f.name.clone()) + .ok_or_else(|| format!("no numeric field '{fl}' in {}", target.name))?, + ), + // A chart moved to another layer keeps its field only if that layer has it as a + // number; otherwise it charts the layer's first numeric field. + None => match &e.kind { + ElementKind::Chart { field, .. } if nl.is_some() => { + let numeric = |f: &&rustgis_data::Field| f.ty.is_numeric(); + let keep = target.dataset.field_index(field).and_then(|i| target.dataset.fields.get(i)).filter(numeric); + let f = keep + .or_else(|| target.dataset.fields.iter().find(numeric)) + .ok_or_else(|| format!("{} has no numeric field to chart", target.name))?; + Some(f.name.clone()) + } + _ => None, + }, + }; + Some((target.name.clone(), field)) + } + _ => None, + } + }) +} + +/// An element key overrides frame even when null, using frame_of's existing fallback semantics. +fn zoom_frame(l: &Layout, p: &Value) -> Result { + match p.get("element") { + Some(_) => frame_of(l, &json!({"frame":p.get("element")})), + None => frame_of(l, p), + } +} +/// String names select another map; other values retain the active-map convention. +fn added_frame_map<'a>(input: &'a impl super::InputSource, p: &Value) -> Result<&'a rustgis_map::Map, String> { + let context = input.input_context(); + let s = &context; + match ps(p, "map") { + Some(n) => s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no map '{n}'")), + None => Ok(s.map()), + } +} +/// Records the map whose current view initializes the new frame. +fn add_frame_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let mut inputs = object_inputs(input, p)?; + inputs.maps.push(added_frame_map(input, p)?.instance_id()); + Ok(inputs) +} +/// Records only stored references actually validated by this element update. +fn update_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let li = index(input, p)?; + let id = element_id(p)?; + let l = s.project.layouts.get(li).ok_or("no layout")?; + let mut inputs = super::Inputs::layers(std::iter::empty()); + if let Some((name, _)) = checked_update(input, p, li, id)? { + let frame = match l.element(id).map(|e| &e.kind) { + Some(ElementKind::Table { frame, .. } | ElementKind::Chart { frame, .. }) => *frame, + _ => return Ok(inputs), + }; + inputs.extend(super::Inputs::named(frame_map(input, l, frame)?, [name.as_str()])?); + } + match l.element(id).map(|e| &e.kind) { + Some(ElementKind::MapFrame { .. }) if ps(p, "map").is_some() => inputs.maps.push(added_frame_map(input, p)?.instance_id()), + Some(ElementKind::Colorbar { frame, .. }) if ps(p, "layer").is_some_and(|n| !n.trim().is_empty()) => { + if let Some(name) = colorbar_layer(input, l, *frame, ps(p, "layer"))? { + inputs.extend(super::Inputs::named(frame_map(input, l, *frame)?, [name.as_str()])?); + } + } + Some(ElementKind::Legend { frame, .. }) => { + if let Some(names) = legend_exclude(input, l, *frame, p)? { + let map = frame_map(input, l, *frame)?; + inputs.layers.extend(names.iter().filter_map(|n| map.layer_by_name(n)).map(|l| (map.instance_id(), l.id))); + inputs.state = json!(names); + } + } + _ => {} + } + inputs.state = json!({"layout":li,"element":id,"kind":l.element(id).map(|e|e.kind.code()),"stored":l.element(id).and_then(|e|element_selectors(&e.kind)),"selectors":inputs.state}); + Ok(inputs) +} + +/// Reference-only operations bind the actual chosen layout, without reading its stored maps. +fn object_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let i = index(input, p)?; + let layout = s.project.layouts.get(i).ok_or("no layout")?; + let mut inputs = super::Inputs::layers(std::iter::empty()); + inputs.state = json!({"layout":i,"name":layout.name}); + Ok(inputs) +} +/// Browser name-based actions use their existing name-to-layout parameter projection. +fn named_object_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + object_inputs(input, &by_name(p)) +} +/// Closing the layout view succeeds even when none is open. +fn close_inputs(input: &super::InputContext<'_>, _: &Value) -> Result { + let s = input; + let mut inputs = super::Inputs::layers(std::iter::empty()); + inputs.state = json!({"activeLayout":s.active_layout}); + Ok(inputs) +} + +/// Stored surround references consumed by rendering or validation, kept as private selector text. +fn element_selectors(kind: &ElementKind) -> Option { + Some(match kind { + ElementKind::MapFrame { map, extent_of, .. } => json!({"map":map,"extentOf":extent_of}), + ElementKind::Table { frame, layer, .. } => json!({"frame":frame,"layer":layer}), + ElementKind::Chart { frame, layer, field, label } => json!({"frame":frame,"layer":layer,"field":field,"label":label}), + ElementKind::Legend { frame, exclude, .. } => json!({"frame":frame,"exclude":exclude}), + ElementKind::Colorbar { frame, layer, .. } => json!({"frame":frame,"layer":layer}), + ElementKind::NorthArrow { frame, .. } | ElementKind::ScaleBar { frame, .. } => json!({"frame":frame}), + _ => return None, + }) +} diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index 4849dc4d11..f4fb1a7500 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -214,3 +214,174 @@ pub(crate) fn map_is_empty(s: &Session) -> bool { pub(crate) fn need_selection(s: &Session) -> Option<&'static str> { if s.map().selection_count() == 0 { Some("nothing is selected") } else { None } } + +/// Borrowed executor selectors for a known ordered map activation. Project data is never cloned. +#[derive(Clone, Copy)] +pub(crate) struct InputContext<'a> { + session: &'a Session, + pub project: &'a crate::Project, + pub active_map: usize, + pub active_layer: Option, +} +/// Shared by live executors and their pure input resolvers, preserving the same selector rules. +pub(crate) trait InputSource { + fn input_context(&self) -> InputContext<'_>; +} +impl InputSource for Session { + fn input_context(&self) -> InputContext<'_> { + InputContext { session: self, project: &self.project, active_map: self.project.active_map, active_layer: self.active_layer } + } +} +impl InputSource for InputContext<'_> { + fn input_context(&self) -> InputContext<'_> { + *self + } +} +impl std::ops::Deref for InputContext<'_> { + type Target = Session; + fn deref(&self) -> &Session { + self.session + } +} +impl<'a> InputContext<'a> { + /// Views live data with only the approved map/default scalar transition applied. + pub fn at(session: &'a Session, selection: Selection) -> Self { + Self { session, project: &session.project, active_map: selection.map, active_layer: selection.layer } + } + /// The real map, selected by the same valid index convention as Session::map. + pub fn map(&self) -> &'a rustgis_map::Map { + self.project.maps.get(self.active_map).or_else(|| self.project.maps.first()).unwrap_or_else(|| self.session.map()) + } + /// Captures only map/default scalars; layer-selection bookkeeping is left exactly as stored. + pub fn selection(&self) -> Selection { + Selection { map: self.active_map, layer: self.active_layer, names_changed: false } + } + /// Resolves numeric, name-before-id and default layer choices for both planner and executor. + pub fn layer_id(&self, p: &Value) -> Result { + let map = self.map(); + match p.get("layer") { + Some(Value::Number(n)) => n + .as_u64() + .and_then(|n| u32::try_from(n).ok()) + .filter(|id| map.layer(*id).is_some()) + .ok_or_else(|| format!("no layer with id {n}")), + Some(Value::String(s)) => map + .layer_by_name(s) + .map(|l| l.id) + .or_else(|| s.parse::().ok().filter(|id| map.layer(*id).is_some())) + .ok_or_else(|| format!("no layer named '{s}'")), + _ => { + self.active_layer.filter(|id| map.layer(*id).is_some()).ok_or_else(|| "select a layer in the Layers pane first".to_string()) + } + } + } + /// Uses the original raw selection fields, including their behavior after map activation. + pub fn selected_layers(&self) -> Vec { + let picked: &[u32] = if self.session.layer_selection_of == self.active_layer { &self.session.layer_selection } else { &[] }; + self.map().layers.iter().map(|l| l.id).filter(|id| picked.contains(id) || self.active_layer == Some(*id)).collect() + } +} +/// Only scalar choices a pure owner transition can model without running a handler. +#[derive(Clone, Copy)] +pub(crate) struct Selection { + pub map: usize, + pub layer: Option, + /// A prior rename preserves defaults, but subsequent named/stored map selectors need a new review. + pub names_changed: bool, +} + +/// Pure references selected by an executor, before it loads data or changes the session. +/// Stored selector state is private equality data; output roles are not consulted here. +pub(crate) struct Inputs { + pub layers: Vec<(u64, u32)>, + pub maps: Vec, + pub tables: Vec, + pub future_layers: Vec, + pub state: serde_json::Value, + pub unresolved: bool, + /// Public destination data, separately sanitized for review; never private selector state. + pub review_places: Vec, + /// This owner actually selected the ordinary active/default layer. + pub uses_default: bool, +} +impl Inputs { + /// Captures existing layer identities, including their owning maps. + pub fn layers<'a>(layers: impl IntoIterator) -> Self { + Self { + layers: layers.into_iter().map(|(m, l)| (m.instance_id(), l.id)).collect(), + maps: Vec::new(), + tables: Vec::new(), + future_layers: Vec::new(), + state: serde_json::Value::Null, + unresolved: false, + review_places: Vec::new(), + uses_default: false, + } + } +} + +impl Inputs { + /// Keeps scalar names and evolving defaults on the planner's existing in-run target paths. + pub fn single_layer(s: &InputContext<'_>, p: &serde_json::Value) -> Result { + match p.get("layer") { + Some(serde_json::Value::String(name)) => Self::primary(s, p).or_else(|_| Self::named(s.map(), [name.as_str()])), + Some(serde_json::Value::Number(_)) => Self::primary(s, p), + _ => { + let mut inputs = Self::layers(std::iter::empty()); + inputs.uses_default = true; + Ok(inputs) + } + } + } + + /// Literal names, exactly as map.layer_by_name resolves them (no numeric/default fallback). + pub fn named<'a>(map: &rustgis_map::Map, names: impl IntoIterator) -> Result { + let mut inputs = Self::layers(std::iter::empty()); + for name in names { + match map.layer_by_name(name) { + Some(l) => inputs.layers.push((map.instance_id(), l.id)), + None => inputs.future_layers.push(name.to_string()), + } + } + Ok(inputs) + } + + /// The ordinary primary selector uses Session's exact numeric/name/default semantics. + pub fn primary(s: &impl InputSource, p: &serde_json::Value) -> Result { + let context = s.input_context(); + let s = &context; + let id = s.layer_id(p)?; + Ok(Self { + layers: vec![(s.map().instance_id(), id)], + maps: Vec::new(), + tables: Vec::new(), + future_layers: Vec::new(), + state: serde_json::Value::Null, + unresolved: false, + review_places: Vec::new(), + uses_default: !matches!(p.get("layer"), Some(serde_json::Value::String(_) | serde_json::Value::Number(_))), + }) + } +} + +impl Inputs { + /// A file-held or missing reference that an exact Ask cannot resolve without doing work. + pub fn unresolved() -> Self { + let mut inputs = Self::layers(std::iter::empty()); + inputs.unresolved = true; + inputs + } + /// Combines operation-owned dependency selections without widening to unrelated namespaces. + pub fn extend(&mut self, other: Self) { + self.layers.extend(other.layers); + self.maps.extend(other.maps); + self.tables.extend(other.tables); + self.future_layers.extend(other.future_layers); + self.unresolved |= other.unresolved; + self.review_places.extend(other.review_places); + self.uses_default |= other.uses_default; + if !other.state.is_null() { + self.state = if self.state.is_null() { other.state } else { serde_json::json!([self.state, other.state]) }; + } + } +} diff --git a/crates/engine/src/cmd/navigate.rs b/crates/engine/src/cmd/navigate.rs index 66406e66a9..ded818a6e8 100644 --- a/crates/engine/src/cmd/navigate.rs +++ b/crates/engine/src/cmd/navigate.rs @@ -11,23 +11,23 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("map.zoomFull", "Full Extent", "Home › Navigate", zoom_full).roles(&[]).view(), - C::new("map.zoomIn", "Fixed Zoom In", "Home › Navigate", |s, _| zoom(s, 2.0)).roles(&[]).view().key("Mod+="), - C::new("map.zoomOut", "Fixed Zoom Out", "Home › Navigate", |s, _| zoom(s, 0.5)).roles(&[]).view().key("Mod+-"), + C::new("map.zoomFull", "Full Extent", "Home › Navigate", zoom_full).roles(&[]).effects(crate::policy::Effects::NONE).view(), + C::new("map.zoomIn", "Fixed Zoom In", "Home › Navigate", |s, _| zoom(s, 2.0)).roles(&[]).effects(crate::policy::Effects::NONE).view().key("Mod+="), + C::new("map.zoomOut", "Fixed Zoom Out", "Home › Navigate", |s, _| zoom(s, 0.5)).roles(&[]).effects(crate::policy::Effects::NONE).view().key("Mod+-"), C::new("map.prevExtent", "Previous Extent", "Home › Navigate", |s, _| { if s.extent_back() { Ok(Value::Null) } else { Err("no previous extent".into()) } - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .when(|s| if s.can_extent_back() { None } else { Some("no previous extent") }), C::new("map.nextExtent", "Next Extent", "Home › Navigate", |s, _| { if s.extent_forward() { Ok(Value::Null) } else { Err("no next extent".into()) } - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .when(|s| if s.can_extent_forward() { None } else { Some("no next extent") }), C::new("map.zoomWorld", "Zoom To World", "Home › Navigate", |s, _| { s.zoom_to(&Extent::new(-170.0, -58.0, 180.0, 78.0)); Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), C::new("map.extent", "View Extent", "Home › Navigate", |s, _| { let e = if s.map().projection.is_globe() { Extent::new(-180.0, -90.0, 180.0, 90.0) } else { super::workspace::view_extent(s) }; @@ -35,26 +35,26 @@ pub fn commands() -> Vec { return Err("the view shows no part of the Earth".into()); } Ok(json!({"extent": [e.min_x, e.min_y, e.max_x, e.max_y]})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("map.zoomToExtent", "Zoom To Extent", "Home › Navigate", zoom_to_extent).roles(&[]) + C::new("map.zoomToExtent", "Zoom To Extent", "Home › Navigate", zoom_to_extent).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"extent": [minLon, minLat, maxLon, maxLat]}"#), - C::new("map.goToXY", "By Coordinates…", "Home › Navigate › Locate", go_to_xy).roles(&[]) + C::new("map.goToXY", "By Coordinates…", "Home › Navigate › Locate", go_to_xy).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"lon" | "x": number, "lat" | "y": number, "scale"?: number, "bearing"?: degrees clockwise from north, "pitch"?: degrees of tilt (0 to 85)}"#), - C::new("map.goToLink", "Go To Map Link", "Home › Navigate", go_to_link).roles(&[]) + C::new("map.goToLink", "Go To Map Link", "Home › Navigate", go_to_link).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"url": a map link ending in #zoom/lat/lon[/bearing[/pitch]] (as MapLibre writes them; also #map=…), or just zoom/lat/lon[/bearing[/pitch]]}"#), C::new("map.link", "Map Link", "Home › Navigate", |s, _| { let hash = link_of(s.map()).ok_or("the view shows no part of the Earth")?; Ok(json!({"hash": hash})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("map.setBearing", "Rotate Map", "Home › Navigate", set_bearing).roles(&[]) + C::new("map.setBearing", "Rotate Map", "Home › Navigate", set_bearing).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"bearing": degrees clockwise from north (the direction the top of the view faces) | "by": degrees to turn it further}"#), - C::new("map.setPitch", "Tilt Map", "Home › Navigate", set_pitch).roles(&[]) + C::new("map.setPitch", "Tilt Map", "Home › Navigate", set_pitch).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"pitch": degrees of tilt from looking straight down (0 to 85) | "by": degrees to tilt it further}"#), C::new("map.resetNorth", "Reset North", "Home › Navigate", |s, _| { @@ -63,29 +63,29 @@ pub fn commands() -> Vec { m.camera.set_pitch(0.0); s.record_extent(); Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), C::new("map.setScale", "Set Scale", "Status Bar", |s, p| { let sc = f(p, "scale").ok_or("scale is required")?; s.map_mut().set_scale(sc); s.record_extent(); Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"scale": number}"#), - C::new("map.basemap", "Basemap", "Home › Add", basemap).roles(&[]) + C::new("map.basemap", "Basemap", "Home › Add", basemap).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": "paper"|"midnight"|"ocean"|"atlas"|"blank", "keepTiles"?: bool (keeps the web tile basemaps over it)}"#), C::new("map.graticule", "Graticule", "View › Display", |s, p| { let m = s.map_mut(); m.graticule = super::b(p, "on").unwrap_or(!m.graticule); Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"on"?: bool (toggles when omitted)}"#), C::new("map.reloadWebLayers", "Reload Web Layers", "View › Display", |_, _| { // The map view draws again when the tile cache's generation moves. crate::render::rustgis_tiles::cache::clear(); Ok(json!({"generation": crate::render::rustgis_tiles::cache::generation()})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), C::new("map.projection", "Globe", "View › View", |s, p| { // A preset's parameters, over those of `type` or of the map's own preset. @@ -111,18 +111,18 @@ pub fn commands() -> Vec { super::workspace::fill_cache(s); } Ok(out) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"type"?: "mercator"|"globe"|preset id (with parameters: "robinson +lon_0=150")|"EPSG:"|"ESRI:" (toggles the globe when omitted), "lon_0"?: degrees (a preset's central meridian), "lat_1"?, "lat_2"?: degrees (a conic preset's standard parallels; these apply to the map's own preset when type is omitted)}"#), C::new("map.projections", "Projections", "View › View", |_, _| { let mut v = vec![json!({"id": "mercator", "label": "Web Mercator"}), json!({"id": "globe", "label": "Globe"})]; v.extend(rustgis_map::projection::PRESETS.iter().map(|(id, label, _)| json!({"id": id, "label": label}))); Ok(Value::Array(v)) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("bookmark.add", "New Bookmark", "Home › Navigate › Bookmarks", add_bookmark).roles(&[]).params( + C::new("bookmark.add", "New Bookmark", "Home › Navigate › Bookmarks", add_bookmark).inputs(map_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"name"?: string (a number is added when another bookmark has it), "description"?: string, "map"?: name (default: the active map; its current view)}"#, ), - C::new("bookmark.go", "Go To Bookmark", "Home › Navigate › Bookmarks", go_bookmark).roles(&[]) + C::new("bookmark.go", "Go To Bookmark", "Home › Navigate › Bookmarks", go_bookmark).inputs(bookmark_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"name": string, "map"?: name (the bookmark's map), "pan"?: bool (keep the current scale)}"#), C::new("bookmark.remove", "Remove Bookmark", "Home › Navigate › Bookmarks", |s, p| { @@ -131,7 +131,7 @@ pub fn commands() -> Vec { m.bookmarks.remove(bi); } Ok(Value::Null) - }).roles(&[]) + }).inputs(bookmark_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": string, "map"?: name (default: the active map)}"#), C::new("bookmark.rename", "Rename Bookmark", "Bookmarks", |s, p| { let new = ps(p, "newName").map(str::trim).filter(|n| !n.is_empty()).ok_or("newName is required")?; @@ -141,7 +141,7 @@ pub fn commands() -> Vec { b.name = name.clone(); } Ok(json!({"name": name})) - }).roles(&[]) + }).inputs(bookmark_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": string, "newName": string (a number is added when another bookmark has it), "map"?: name}"#), C::new("bookmark.update", "Update Bookmark", "Bookmarks", |s, p| { let (mi, bi) = bookmark_at(s, p)?; @@ -156,7 +156,7 @@ pub fn commands() -> Vec { } } Ok(Value::Null) - }).roles(&[]) + }).inputs(bookmark_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": string (it takes the current view; its map must be in view), "map"?: name}"#), C::new("bookmark.describe", "Bookmark Description", "Bookmarks", |s, p| { let text: String = ps(p, "description").ok_or("description is required")?.trim().chars().take(MAX_DESCRIPTION).collect(); @@ -165,7 +165,7 @@ pub fn commands() -> Vec { b.description = text; } Ok(Value::Null) - }).roles(&[]) + }).inputs(bookmark_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": string, "description": string (empty clears it), "map"?: name}"#), C::new("bookmark.move", "Move Bookmark", "Bookmarks", |s, p| { let to = super::uint(p, "to").ok_or("to is required (an index)")?; @@ -176,48 +176,41 @@ pub fn commands() -> Vec { m.bookmarks.insert(to, b); } Ok(Value::Null) - }).roles(&[]) + }).inputs(bookmark_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": string, "to": index (0: first; past the end: last), "map"?: name}"#), - C::new("bookmark.save", "Export Bookmarks", "Home › Navigate › Bookmarks", |s, p| super::items::save_item(s, "bookmarks", p)).roles(&[]) + C::new("bookmark.save", "Export Bookmarks", "Home › Navigate › Bookmarks", |s, p| super::items::save_item(s, "bookmarks", p)).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"path": "*.rgisbookmarks"}"#), - C::new("bookmark.import", "Import Bookmarks", "Home › Navigate › Bookmarks", import_bookmarks).roles(&[]) + C::new("bookmark.import", "Import Bookmarks", "Home › Navigate › Bookmarks", import_bookmarks).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"path"?: "*.rgisbookmarks", "text"?: bookmarks JSON, "data"?: base64 file}"#), - C::new("map.new", "New Map", "Home › Project", new_map).roles(&[]).params(r#"{"name"?: string}"#), + C::new("map.new", "New Map", "Home › Project", new_map).transition(unmodelled_transition).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"name"?: string}"#), C::new("map.activate", "Activate Map", "Browser", |s, p| { - let n = s_name(p)?; - let i = s.project.maps.iter().position(|m| m.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no map '{n}'"))?; - s.project.active_map = i; - s.active_layer = s.map().layers.first().map(|l| l.id); + let choice = activation(&super::InputSource::input_context(&*s), p)?; + let choice = choice.ok_or("no activation choice")?; + s.project.active_map = choice.map; + s.active_layer = choice.layer; s.table_layer = None; Ok(Value::Null) - }).roles(&[]) + }).inputs(required_map_inputs).transition(activation).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": string}"#) .view(), C::new("map.rename", "Rename Map", "Layers", |s, p| { - let n = s_name(p)?.trim(); - if n.is_empty() { - return Err("name is required".into()); - } - let i = map_at(s, p)?; + let (i, name) = rename_choice(&super::InputSource::input_context(&*s), p)?; let old = s.project.maps.get(i).map(|m| m.name.clone()).ok_or("no map")?; - // Unique among the other maps, so a name finds one map (tabs, Browser, map frames). - let name = super::numbered(n, |n| s.project.maps.iter().enumerate().any(|(j, m)| j != i && m.name.eq_ignore_ascii_case(n))); if let Some(m) = s.project.maps.get_mut(i) { m.name = name.clone(); } follow_renamed_map(s, &old, &name); Ok(json!({"name": name})) - }).roles(&[]) + }).transition(rename_transition).inputs(map_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": string (the new name; a number is added when another map has it), "map"?: name (default: the active map)}"#), - C::new("map.duplicate", "Duplicate Map", "Browser", duplicate_map).roles(&[]) + C::new("map.duplicate", "Duplicate Map", "Browser", duplicate_map).transition(unmodelled_transition).inputs(duplicate_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"map"?: name (default: the active map), "name"?: string (default: " copy"; a number is added when another map has it)}"#), C::new("map.remove", "Delete Map", "Browser", |s, p| { if s.project.maps.len() < 2 { return Err("a project keeps at least one map".into()); } - let n = s_name(p)?; - let i = s.project.maps.iter().position(|m| m.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no map '{n}'"))?; + let i = required_map_at(s,p)?; s.project.maps.remove(i); // Deleting another map keeps the one in view (a map tab's close button). let active = s.project.active_map; @@ -229,7 +222,7 @@ pub fn commands() -> Vec { s.table_layer = None; } Ok(Value::Null) - }).roles(&[]) + }).transition(unmodelled_transition).inputs(required_map_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": string}"#), ] } @@ -254,9 +247,11 @@ fn s_name(p: &Value) -> Result<&str, String> { /// The map `map` names (default: the active one). A `map` that isn't a name is an error, never /// the active map. -pub(crate) fn map_at(s: &Session, p: &Value) -> Result { +pub(crate) fn map_at(input: &impl super::InputSource, p: &Value) -> Result { + let context = input.input_context(); + let s = &context; match p.get("map") { - None | Some(Value::Null) => Ok(s.project.active_map), + None | Some(Value::Null) => Ok(s.active_map), Some(Value::String(m)) => s.project.maps.iter().position(|x| x.name.eq_ignore_ascii_case(m)).ok_or_else(|| format!("no map '{m}'")), Some(_) => Err("map must be a map's name".into()), } @@ -619,8 +614,10 @@ fn place_at(map: &Map, lonlat: Coord, ground: f64) -> Option Result<(usize, usize), String> { - let mi = map_at(s, p)?; +fn bookmark_at(input: &impl super::InputSource, p: &Value) -> Result<(usize, usize), String> { + let context = input.input_context(); + let s = &context; + let mi = map_at(input, p)?; let n = s_name(p)?; let bi = s.project.maps.get(mi).and_then(|m| m.bookmarks.iter().position(|b| b.name.eq_ignore_ascii_case(n))); Ok((mi, bi.ok_or_else(|| format!("no bookmark '{n}'"))?)) @@ -752,3 +749,74 @@ fn new_map(s: &mut Session, p: &Value) -> CmdResult { s.table_layer = None; Ok(json!({"name": name})) } + +/// Uses the strict map selector for operations that accept map names or null. +fn map_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let map = s.project.maps.get(map_at(input, p)?).ok_or("no map")?; + let mut inputs = super::Inputs::layers(std::iter::empty()); + inputs.maps.push(map.instance_id()); + Ok(inputs) +} + +/// Activation and removal require a string name, unlike optional map selectors. +fn required_map_at(input: &impl super::InputSource, p: &Value) -> Result { + let context = input.input_context(); + let s = &context; + let n = s_name(p)?; + s.project.maps.iter().position(|m| m.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no map '{n}'")) +} +/// Binds the required map and its existing layers before activation/removal. +fn required_map_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let map = s.project.maps.get(required_map_at(input, p)?).ok_or("no map")?; + let mut inputs = super::Inputs::layers(map.layers.iter().map(|l| (map, l))); + inputs.maps.push(map.instance_id()); + Ok(inputs) +} +/// Binds the map and layer incarnations that duplication will clone. +fn duplicate_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let map = s.project.maps.get(map_at(input, p)?).ok_or("no map")?; + let mut inputs = super::Inputs::layers(map.layers.iter().map(|l| (map, l))); + inputs.maps.push(map.instance_id()); + Ok(inputs) +} +/// Binds the bookmark's source map and the active destination map without freezing camera data. +fn bookmark_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let (mi, bi) = bookmark_at(input, p)?; + let map = s.project.maps.get(mi).ok_or("no map")?; + let mut inputs = super::Inputs::layers(std::iter::empty()); + inputs.maps.extend([map.instance_id(), s.map().instance_id()]); + inputs.state = json!({"bookmark":bi,"name":map.bookmarks.get(bi).map(|b|&b.name)}); + Ok(inputs) +} + +/// The actual activation choice, shared with ordered approval; raw selection bookkeeping survives. +fn activation(s: &super::InputContext<'_>, p: &Value) -> Result, String> { + let map = required_map_at(s, p)?; + let layer = s.project.maps.get(map).and_then(|m| m.layers.first()).map(|l| l.id); + Ok(Some(super::Selection { map, layer, names_changed: false })) +} +/// Validates the actual rename selector and uniqueness rule without changing project data. +fn rename_choice(s: &super::InputContext<'_>, p: &Value) -> Result<(usize, String), String> { + let n = s_name(p)?.trim(); + if n.is_empty() { + return Err("name is required".into()); + } + let i = map_at(s, p)?; + let name = super::numbered(n, |n| s.project.maps.iter().enumerate().any(|(j, m)| j != i && m.name.eq_ignore_ascii_case(n))); + Ok((i, name)) +} +/// Renaming preserves defaults on the same incarnation; it does not model a later map namespace. +fn rename_transition(s: &super::InputContext<'_>, p: &Value) -> Result, String> { + let _ = rename_choice(s, p)?; + let mut selection = s.selection(); + selection.names_changed = true; + Ok(Some(selection)) +} +/// These namespace changes cannot be predicted by a scalar borrowed view, so exact Ask refuses. +fn unmodelled_transition(_: &super::InputContext<'_>, _: &Value) -> Result, String> { + Ok(None) +} diff --git a/crates/engine/src/cmd/nldi.rs b/crates/engine/src/cmd/nldi.rs index 1dd51601de..4caa2fcc19 100644 --- a/crates/engine/src/cmd/nldi.rs +++ b/crates/engine/src/cmd/nldi.rs @@ -13,19 +13,19 @@ const WHERE: &str = "Home › Add › Web Data › United States › USGS NLDI"; pub fn commands() -> Vec { vec![ - C::new("nldi.flowline", "NLDI Flowline", WHERE, flowline).roles(&[]).params( + C::new("nldi.flowline", "NLDI Flowline", WHERE, flowline).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"lon": number, "lat": number (a point in the United States), "direction"?: "none" (the whole flowline, default) | "up" (its part upstream of the point) | "down", "name"?: text, "answer"?: "the flowtrace's GeoJSON, or a COMID's flowline, when already fetched", "snapped"?: [lon, lat] (with a COMID's flowline: where the point meets it)} — adds the flowline and the raindrop's path to it; returns its COMID"#, ), - C::new("nldi.comid", "NLDI COMID at a Point", WHERE, comid_cmd).roles(&[]) + C::new("nldi.comid", "NLDI COMID at a Point", WHERE, comid_cmd).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)) .view() .params(r#"{"lon": number, "lat": number} — the nearest NHDPlus flowline's COMID, and where the point snaps to it"#), - C::new("nldi.basin", "NLDI Basin", WHERE, basin).roles(&[]).params( + C::new("nldi.basin", "NLDI Basin", WHERE, basin).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"comid"?: number (an NHDPlus flowline's), "lon"?: number, "lat"?: number (or a point: its flowline's COMID), "name"?: text, "answer"?: "the basin's GeoJSON, when already fetched"} — adds the basin draining to the flowline"#, ), - C::new("nldi.sources", "NLDI Navigation Sources", WHERE, sources).roles(&[]).view().params( + C::new("nldi.sources", "NLDI Navigation Sources", WHERE, sources).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).view().params( r#"{"comid"?: number (an NHDPlus flowline's), "lon"?: number, "lat"?: number (or a point: its flowline's COMID), "mode"?: "UM" (upstreamMain, default) | "UT" (upstreamTributaries) | "DM" (downstreamMain) | "DD" (downstreamDiversions), "answer"?: text} — the linked data a navigation can list (flowlines, nwissite, wqp, huc12pp…)"#, ), - C::new("nldi.navigate", "NLDI Navigation", WHERE, navigate).roles(&[]).params( + C::new("nldi.navigate", "NLDI Navigation", WHERE, navigate).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"comid"?: number (an NHDPlus flowline's), "lon"?: number, "lat"?: number (or a point: its flowline's COMID), "mode"?: "UM" (upstreamMain, default) | "UT" | "DM" | "DD", "distance"?: km 1–9999 (500), "source"?: "flowlines" (default) or a source nldi.sources lists (nwissite, wqp…), "name"?: text, "answer"?: "the navigation's GeoJSON, when already fetched"} — adds the flowlines or sites along the way"#, ), ] diff --git a/crates/engine/src/cmd/notebook.rs b/crates/engine/src/cmd/notebook.rs index 0f3bbfa624..a5621fcb7e 100644 --- a/crates/engine/src/cmd/notebook.rs +++ b/crates/engine/src/cmd/notebook.rs @@ -128,10 +128,11 @@ pub fn commands() -> Vec { }; let r = push(s, nb)?; // Trusted only once it exists. - s.trusted_notebooks.insert(name.to_ascii_lowercase()); + Ok(r) }) .roles(&[]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"name"?}"#), C::new("notebook.open", "Add and Open Notebook", "Analyze › Notebook", |s, p| { let text = super::items::read_text(p)?; @@ -145,6 +146,7 @@ pub fn commands() -> Vec { push(s, nb) }) .roles(&[crate::ParamRole { key: "text", role: crate::Role::Document(crate::Document::Notebook) }]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"path"?: "*.rgisnb|*.ipynb", "text"?: notebook JSON, "data"?: base64 file}"#), C::new("notebook.activate", "Open Notebook", "Browser", |s, p| { let i = index(s, p)?; @@ -152,6 +154,7 @@ pub fn commands() -> Vec { Ok(json!({"notebook": i})) }) .roles(&[]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_notebook) .params(r#"{"notebook": name|index}"#), @@ -163,6 +166,7 @@ pub fn commands() -> Vec { Ok(Value::Null) }) .roles(&[]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .when(need_notebook) .params(r#"{"notebook"?}"#), C::new("notebook.addCell", "Add Cell", "Notebook", |s, p| { @@ -182,6 +186,7 @@ pub fn commands() -> Vec { Ok(json!({"cell": at, "cells": nb.cells.len()})) }) .roles(&[crate::ParamRole { key: "source", role: crate::Role::Opaque }]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .when(need_notebook) .params(r#"{"notebook"?, "kind"?: "code"|"markdown", "source"?, "at"?: index}"#), C::new("notebook.editCell", "Edit Cell", "Notebook", |s, p| { @@ -192,6 +197,7 @@ pub fn commands() -> Vec { Ok(json!({"cell": k})) }) .roles(&[crate::ParamRole { key: "source", role: crate::Role::Opaque }]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .when(need_notebook) .params(r#"{"notebook"?, "cell": index, "source": text}"#), C::new("notebook.removeCell", "Delete Cell", "Notebook", |s, p| { @@ -201,10 +207,12 @@ pub fn commands() -> Vec { Ok(json!({"cells": nb.cells.len()})) }) .roles(&[]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .when(need_notebook) .params(r#"{"notebook"?, "cell": index}"#), C::new("notebook.run", "Run Cells", "Notebook", run) .roles(&[]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Automation).with(crate::policy::Effect::Files)) .when(need_notebook) .params(r#"{"notebook"?, "cell"?: index (default: every command cell, stopping at an error)}"#), C::new("notebook.save", "Save Notebook", "Notebook", |s, p| { @@ -215,6 +223,7 @@ pub fn commands() -> Vec { Ok(json!({"path": path})) }) .roles(&[]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_notebook) .params(r#"{"notebook"?, "path": "*.rgisnb|*.ipynb"}"#), @@ -281,13 +290,20 @@ fn run(s: &mut Session, p: &Value) -> CmdResult { } s.notebook_running = true; // The flag is cleared on every path, a panic included. - let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run_cells(s, p))) - .unwrap_or_else(|_| Err("the notebook failed unexpectedly".into())); + let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let name = s.item_name("notebook", p)?; + if s.item_trusted("notebook", &name) { + run_cells(s, p) + } else { + s.narrowed(crate::policy::Narrow::Item { kind: "notebook", name }, |s| run_cells(s, p))? + } + })) + .unwrap_or_else(|_| Err("the notebook failed unexpectedly".into())); s.notebook_running = false; r } -/// Runs command cells and cleans generated output before applying its retained-size bound. +/// Runs cells through the shared command boundary and cleans retained generated output. fn run_cells(s: &mut Session, p: &Value) -> CmdResult { let i = index(s, p)?; let cells: Vec = match p.get("cell") { @@ -316,10 +332,10 @@ fn run_cells(s: &mut Session, p: &Value) -> CmdResult { let script = c.source.clone(); // A notebook from a file (opened, or in an opened project) is someone else's: its // cells may not read or write files or run other scripts. - let trusted = s.project.notebooks.get(i).is_some_and(|nb| s.trusted_notebooks.contains(&nb.name.to_ascii_lowercase())); - let r = match (trusted, untrusted_line(&script)) { + let trusted = s.project.notebooks.get(i).is_some_and(|nb| s.item_trusted("notebook", &nb.name)); + let r = match (trusted, untrusted_line(s, &script)) { (false, Some(n)) => Err(format!( - "line {n} reads or writes files or runs a script, which a notebook from a file can't do; copy it into a new notebook to run it" + "line {n} reads or writes files, uses the network or runs other software or reserved actions, which a notebook from a file can't do; copy it into a new notebook to run it" )), _ => super::workflows::run_script(s, &json!({"script": script})), }; @@ -338,9 +354,10 @@ fn run_cells(s: &mut Session, p: &Value) -> CmdResult { .collect(); if lines.is_empty() { "(nothing to run)".to_string() } else { lines.join("\n") } } - Err(e) => rustgis_data::diagnostics::Redacted::new(&e).into_string(), + Err(e) => e, }; - let text = rustgis_data::address::scrub_addresses(&text).0.into_owned(); + let text = crate::durable::generated_text(&text); + let text = if failed { rustgis_data::diagnostics::Redacted::new(&text).into_string() } else { text }; // Into the cell that ran, which a command removing or adding cells may have moved: the // cell with its source nearest to where it was. let ran_cell = find(s).and_then(|i| s.project.notebooks.get_mut(i)).and_then(|nb| { @@ -349,7 +366,6 @@ fn run_cells(s: &mut Session, p: &Value) -> CmdResult { }); if let Some(c) = ran_cell { // Scrubbed before the cut, so no part of a login binding a result quoted is kept. - let text = crate::render::rustgis_tiles::logins::scrub(&text); c.output = Some(text.chars().take(MAX_OUTPUT).collect()); c.error = failed; // The Jupyter outputs are from an earlier run; this one's output replaces them. @@ -367,7 +383,7 @@ fn run_cells(s: &mut Session, p: &Value) -> CmdResult { } /// The first line (1-based) of a cell whose command could reach outside the session. -fn untrusted_line(script: &str) -> Option { +fn untrusted_line(s: &Session, script: &str) -> Option { script.lines().enumerate().find_map(|(n, line)| { let line = line.trim(); if line.is_empty() || line.starts_with('#') { @@ -376,7 +392,7 @@ fn untrusted_line(script: &str) -> Option { let (id, rest) = line.split_once(char::is_whitespace).unwrap_or((line, "")); // Malformed params read as none here; `run_script` rejects that line before it runs. let params: Value = serde_json::from_str(rest.trim()).unwrap_or(Value::Null); - super::items::touches_files(id.trim_end_matches('='), ¶ms).then_some(n + 1) + super::items::touches_files(s, id.trim_end_matches('='), ¶ms).then_some(n + 1) }) } diff --git a/crates/engine/src/cmd/pg.rs b/crates/engine/src/cmd/pg.rs index bf3bc9b134..40c7eea031 100644 --- a/crates/engine/src/cmd/pg.rs +++ b/crates/engine/src/cmd/pg.rs @@ -30,20 +30,21 @@ const SSL_MODES: &[&str] = &["disable", "allow", "prefer", "require", "verify-ca #[cfg(not(target_arch = "wasm32"))] const MAX_LISTED: usize = 5_000; +/// Keeps database writes reserved and declares network and credential-file reach for live checks. pub fn commands() -> Vec { vec![ - C::new("pg.connect", "Connect PostGIS", "Home › Add › Add Data", connect).roles(&[]).view().params( + C::new("pg.connect", "Connect PostGIS", "Home › Add › Add Data", connect).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)).view().params( r#"{"connection"?: "host=… port=… dbname=… user=…, or a postgresql:// address (instead of the parts below)", "host"?: string, "port"?: number, "database"?: string, "user"?: string, "password"?: "kept for this session only, never saved", "sslmode"?: "disable"|"prefer"|"require"|"verify-ca"|"verify-full"}"#, ), - C::new("pg.disconnect", "Disconnect PostGIS", "Home › Add › Add Data", disconnect).roles(&[]).view().when(need_connection), - C::new("pg.status", "PostGIS Connection", "Home › Add › Add Data", status).roles(&[]).view(), - C::new("pg.tables", "List PostGIS Tables", "Home › Add › Add Data", tables).roles(&[]).view().when(need_connection).params(r#"{"schema"?: string}"#), - C::new("pg.pendingEdits", "PostGIS Edits", "Edit › Manage Edits", pending_edits).roles(&[]).view().params(r#"{"layer"?: "a layer added with pg.addLayer (default: the active one)"}"#), - C::new("pg.saveEdits", "Save to PostGIS", "Edit › Manage Edits", save_edits).roles(&[]) + C::new("pg.disconnect", "Disconnect PostGIS", "Home › Add › Add Data", disconnect).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().when(need_connection), + C::new("pg.status", "PostGIS Connection", "Home › Add › Add Data", status).roles(&[]).effects(crate::policy::Effects::NONE).view(), + C::new("pg.tables", "List PostGIS Tables", "Home › Add › Add Data", tables).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)).view().when(need_connection).params(r#"{"schema"?: string}"#), + C::new("pg.pendingEdits", "PostGIS Edits", "Edit › Manage Edits", pending_edits).roles(&[]).effects(crate::policy::Effects::NONE).view().params(r#"{"layer"?: "a layer added with pg.addLayer (default: the active one)"}"#), + C::new("pg.saveEdits", "Save to PostGIS", "Edit › Manage Edits", save_edits).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)) .params(r#"{"layer"?: "a layer added with pg.addLayer (default: the active one)"}"#), - C::new("pg.discardEdits", "Discard PostGIS Edits", "Edit › Manage Edits", discard_edits).roles(&[]) + C::new("pg.discardEdits", "Discard PostGIS Edits", "Edit › Manage Edits", discard_edits).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)) .params(r#"{"layer"?: "a layer added with pg.addLayer (default: the active one)"}"#), - C::new("pg.addLayer", "Add PostGIS Layer", "Home › Add › Add Data", add_layer).roles(&[]).when(need_connection).params( + C::new("pg.addLayer", "Add PostGIS Layer", "Home › Add › Add Data", add_layer).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)).when(need_connection).params( r#"{"table": "table, schema.table, or table:column for one of several geometry columns", "where"?: "an SQL condition on the table's columns", "limit"?: "the most rows of each layer (default and most: 100000)", "name"?: string, "crs"?: "EPSG:… of a geometry that doesn't say"}"#, ), ] diff --git a/crates/engine/src/cmd/plugin_fetch.rs b/crates/engine/src/cmd/plugin_fetch.rs index b089161a5c..168e10dac3 100644 --- a/crates/engine/src/cmd/plugin_fetch.rs +++ b/crates/engine/src/cmd/plugin_fetch.rs @@ -282,7 +282,9 @@ impl Calls { pub const REQUEST_WAIT: Duration = Duration::from_secs(10 * 60); /// A plugin command waiting for its fetches, or its downloads, before it runs again. +#[derive(Clone)] pub(crate) struct Retry { + continuation: crate::policy::Continuation, id: String, params: Value, urls: Vec, @@ -301,8 +303,9 @@ pub(crate) struct Retry { /// The downloads plugin commands wait for (#1202): those the app is making, with what was /// asked and since when, and those added, with what adding them answered, until the command /// waiting for them runs again. -#[derive(Default)] +#[derive(Clone, Default)] pub(crate) struct UrlWaits { + owners: std::collections::BTreeMap, in_flight: Vec<(u64, Value, Instant)>, landed: Vec<(u64, Value, CmdResult, Added)>, /// Requests given up (past [`REQUEST_WAIT`], or their command replaced or dropped), with their @@ -436,12 +439,16 @@ fn asked_by(asked: &Value, params: &Value) -> bool { } impl UrlWaits { + pub(crate) fn owner(&self, n: u64) -> Option<&crate::policy::Continuation> { + self.owners.get(&n) + } + /// The number of a download being made for `asked`, or made and added while its command /// waits to run again: a request made again then is that one, not another download. - pub(crate) fn in_flight(&self, asked: &Value) -> Option { + pub(crate) fn in_flight(&self, asked: &Value, owns: impl Fn(u64) -> bool) -> Option { // One that failed added nothing: asked again, it is downloaded again. - let made = self.landed.iter().find(|(_, q, answer, _)| answer.is_ok() && same_request(q, asked)).map(|(n, ..)| *n); - self.in_flight.iter().find(|(_, q, _)| same_request(q, asked)).map(|(n, ..)| *n).or(made) + let made = self.landed.iter().find(|(n, q, answer, _)| answer.is_ok() && owns(*n) && same_request(q, asked)).map(|(n, ..)| *n); + self.in_flight.iter().find(|(n, q, _)| owns(*n) && same_request(q, asked)).map(|(n, ..)| *n).or(made) } /// Whether what request `n` added once it landed is no longer in the project. @@ -517,6 +524,9 @@ impl UrlWaits { } } self.landed.retain(|(m, ..)| *m != n); + if !taken { + self.owners.remove(&n); + } } /// Kept until its download lands (and is refused) or fails: never forgotten while it can @@ -539,6 +549,9 @@ impl UrlWaits { /// A given-up request's late download was refused: nothing more to remember of it. pub(crate) fn refused(&mut self, n: u64) { self.expired.retain(|(m, _)| *m != n); + if !self.has(n) { + self.owners.remove(&n); + } } /// Only what `waiting` still waits for is kept. @@ -546,6 +559,14 @@ impl UrlWaits { let wanted = |n: &u64| waiting.iter().any(|r| r.requests.contains(n)); self.in_flight.retain(|(n, ..)| wanted(n)); self.landed.retain(|(n, ..)| wanted(n)); + let retained: Vec = self + .in_flight + .iter() + .map(|(n, ..)| *n) + .chain(self.landed.iter().map(|(n, ..)| *n)) + .chain(self.expired.iter().map(|(n, ..)| *n)) + .collect(); + self.owners.retain(|n, _| retained.contains(n)); } } @@ -598,6 +619,7 @@ impl Session { requests.iter().filter(|(n, _)| !self.url_waits.has(*n)).filter_map(|(_, q)| q["url"].as_str().map(String::from)).collect(); return Err(format!("{id}: gave up after fetching {MAX_RETRIES} times{}", not_started(&dropped))); } + let continuation = self.continuation()?; // Past the given-up downloads that can still land, no more are asked for: each must be // remembered until it lands, to be refused then. Those not yet taken by the front end // aren't downloaded. It is refused before any wait changes, so the command's earlier run @@ -610,10 +632,11 @@ impl Session { cancelled.extend( self.url_requests .iter() - .filter(|q| q["request"].as_u64() == Some(*n)) - .filter_map(|q| q["url"].as_str().map(String::from)), + .filter(|q| q.params()["request"].as_u64() == Some(*n)) + .filter_map(|q| q.params()["url"].as_str().map(String::from)), ); - self.url_requests.retain(|q| q["request"].as_u64() != Some(*n)); + self.url_requests.retain(|q| q.params()["request"].as_u64() != Some(*n)); + self.retire_abandoned_requests(); } } return Err(format!( @@ -625,7 +648,7 @@ impl Session { // downloads a replaced or dropped run alone waited for are given up, so they aren't added // with no command to go on with them. let keeps: Vec = requests.iter().map(|(n, _)| *n).collect(); - if let Some(i) = self.plugin_retries.iter().position(|r| r.id == id) { + if let Some(i) = self.plugin_retries.iter().position(|r| r.id == id && self.same_url_caller(&r.continuation, &continuation)) { let old = self.plugin_retries.remove(i); let cancelled = self.give_up_requests(&old, &keeps); self.notify( @@ -652,11 +675,13 @@ impl Session { let numbers = requests.iter().map(|(n, _)| *n).collect(); for (n, asked) in requests { self.url_waits.wait(n, asked); + self.url_waits.owners.entry(n).or_insert_with(|| continuation.clone()); } // What this run already had answered (it is a rerun waiting again): kept for the next. let answered = self.rerun_landed.clone(); let said = self.rerun_said.clone(); self.plugin_retries.push(Retry { + continuation, id: id.to_string(), params: params.clone(), urls, @@ -679,11 +704,12 @@ impl Session { let queued: Vec = self .url_requests .iter() - .filter(|q| q["request"].as_u64() == Some(*n)) - .filter_map(|q| q["url"].as_str().map(String::from)) + .filter(|q| q.params()["request"].as_u64() == Some(*n)) + .filter_map(|q| q.params()["url"].as_str().map(String::from)) .collect(); self.url_waits.give_up(*n, queued.is_empty()); - self.url_requests.retain(|q| q["request"].as_u64() != Some(*n)); + self.url_requests.retain(|q| q.params()["request"].as_u64() != Some(*n)); + self.retire_abandoned_requests(); cancelled.extend(queued); } } @@ -742,8 +768,7 @@ impl Session { self.rerun_landed = r.answered.iter().cloned().chain(mine).collect(); self.rerun_said = r.said; self.retry_tries = r.tries; - // Run as the app runs a plugin's command: what it asks for now is left to the app. - let res = self.run_leaving_downloads(&r.id, &r.params); + let res = self.with_deferred_downloads(|s| s.retry_continuation(&r.continuation, &r.params)); self.retry_tries = 0; self.rerun_landed.clear(); self.rerun_said.clear(); @@ -864,378 +889,272 @@ mod tests { assert!(!asked_by(&json!({"url": u}), &json!({"url": u, "connection": "c1"}))); } - /// Downloads given up after the wait are said once, in one notice with their addresses, - /// whatever the commands do with their errors; none while they still have time. + fn load_test_plugin(id: &str) -> String { + let package = rustgis_plugins::Package::from_zip(&rustgis_plugins::testing::package(id, &["edit", "network"])).unwrap(); + rustgis_plugins::load(package, "test").unwrap(); + format!("plugin:{id}/f") + } + + fn ask(s: &mut Session, command: &str, params: Value) -> CmdResult { + s.run_leaving_downloads(command, &json!({"op":"layer.addUrl", "params":params})) + } + + /// Timeouts are one counted, redacted notice, and a real pinned command receives the failure. #[test] fn a_download_given_up_is_said_with_its_address() { - // Only where the clock has run that long: a machine just started can't be that far back. let Some(long_ago) = Instant::now().checked_sub(REQUEST_WAIT + Duration::from_secs(1)) else { return }; let mut s = Session::new(crate::project::Project::default()); - let url = "https://x.org/slow.csv?token=hunter2"; - s.retry_plugin("plugin:eng-slow/f", &json!({}), Vec::new(), vec![(900, json!({"url": url}))]).unwrap(); + let first = load_test_plugin("eng-slow-real"); + let other = load_test_plugin("eng-slow-other"); + ask(&mut s, &first, json!({"url":"https://x.org/slow.csv?token=hunter2"})).unwrap(); + let taken = s.take_url_requests(); let mark = s.notice_mark(); s.poll_plugin_retries(); assert!(s.notices_since(mark).is_empty(), "it still has time"); s.url_waits.in_flight[0].2 = long_ago; + let ran = s.poll_plugin_retries(); + assert!(ran[0].1.is_err(), "{ran:?}"); s.poll_plugin_retries(); - s.poll_plugin_retries(); - let notices = s.notices_since(mark); - let said: Vec<&str> = notices.iter().filter(|n| n.error && n.text.contains("stopped waiting")).map(|n| n.text.as_str()).collect(); + let said: Vec<_> = s.notices_since(mark).iter().filter(|n| n.error && n.text.contains("stopped waiting")).collect(); assert_eq!(said.len(), 1, "{said:?}"); - assert!(said[0].starts_with("https://x.org/slow.csv") && !said[0].contains("hunter2"), "{said:?}"); - // Several at once: one notice, with how many and which. - let others = vec![(901, json!({"url": "https://x.org/a.csv?token=hunter2"})), (902, json!({"url": "https://x.org/b.csv"}))]; - s.retry_plugin("plugin:eng-slow/g", &json!({}), Vec::new(), others).unwrap(); + assert!(said[0].text.starts_with("https://x.org/slow.csv") && !said[0].text.contains("hunter2")); + // A real late claim is refused before its bytes can add anything. + let id = s.download_started_for(&taken[0]).unwrap(); + assert!(s.download_landed(id, b"x,y\n1,2\n".to_vec())); + let mut p = taken[0].params().clone(); + p["id"] = json!(id); + assert!(s.complete_download(id, &p).unwrap_err().contains("stopped waiting")); + assert!(s.downloaded.is_empty() && s.map().layers.is_empty()); + ask(&mut s, &first, json!({"url":"https://x.org/a.csv?token=hunter2"})).unwrap(); + ask(&mut s, &other, json!({"url":"https://x.org/b.csv"})).unwrap(); + let _taken = s.take_url_requests(); s.url_waits.in_flight.iter_mut().for_each(|w| w.2 = long_ago); let mark = s.notice_mark(); s.poll_plugin_retries(); - let notices = s.notices_since(mark); - let said: Vec<&str> = notices.iter().filter(|n| n.error && n.text.contains("stopped waiting")).map(|n| n.text.as_str()).collect(); + let said: Vec<_> = s.notices_since(mark).iter().filter(|n| n.error && n.text.contains("stopped waiting")).collect(); assert_eq!(said.len(), 1, "{said:?}"); assert!( - said[0].starts_with("2 downloads") && said[0].contains("https://x.org/a.csv") && said[0].contains("https://x.org/b.csv"), - "{said:?}" + said[0].text.starts_with("2 downloads") + && said[0].text.contains("https://x.org/a.csv") + && said[0].text.contains("https://x.org/b.csv") ); - assert!(!said[0].contains("hunter2"), "{said:?}"); + assert!(!said[0].text.contains("hunter2")); + rustgis_plugins::unload("eng-slow-real"); + rustgis_plugins::unload("eng-slow-other"); } - /// A waiting command's answer names what it waits for, without the addresses' keys. #[test] fn a_waiting_answer_hides_the_keys_of_its_addresses() { let mut s = Session::new(crate::project::Project::default()); + let command = load_test_plugin("eng-keys-real"); let doc = "https://x.org/doc.json?token=hunter2"; - let csv = "https://x.org/a.csv?token=hunter3"; - let r = s.retry_plugin("plugin:eng-keys/f", &json!({}), vec![format!("eng-keys {doc}")], vec![(900, json!({"url": csv}))]).unwrap(); - let (fetching, downloading) = (r["fetching"][0].as_str().unwrap(), r["downloading"][0].as_str().unwrap()); - assert!(fetching.starts_with("https://x.org/doc.json") && !fetching.contains("hunter2"), "{r}"); - assert!(downloading.starts_with("https://x.org/a.csv") && !downloading.contains("hunter3"), "{r}"); - assert!(s.url_waits.holds(900, &json!({"url": csv})), "it still waits for the address asked for"); + let cache_key = key("eng-keys-real", doc); + cache().in_flight.push((cache_key.clone(), Instant::now())); + TEST.with(|t| t.set((false, false))); + let r = s.run_leaving_downloads(&command, &json!({"op":"host.fetch","params":{"url":doc}})).unwrap(); + TEST.with(|t| t.set((false, true))); + assert!(r["fetching"][0].as_str().unwrap().starts_with("https://x.org/doc.json") && !r.to_string().contains("hunter2")); + let r = ask(&mut s, &command, json!({"url":"https://x.org/a.csv?token=hunter3"})).unwrap(); + assert!(r["downloading"][0].as_str().unwrap().starts_with("https://x.org/a.csv") && !r.to_string().contains("hunter3")); + let taken = s.take_url_requests(); + assert!(taken[0].params()["url"].as_str().unwrap().contains("hunter3"), "the actual request remains exact"); + cache().in_flight.retain(|(k, _)| k != &cache_key); + rustgis_plugins::unload("eng-keys-real"); } - /// A refused landing's error, which the assistant and MCP get as it is, keeps an address's - /// keys out of it. + /// Refusal requires a real claim and cannot consume a different caller's bytes by number. #[test] fn a_refused_landing_hides_the_keys_of_its_address() { let mut s = Session::new(crate::project::Project::default()); + let command = load_test_plugin("eng-refuse-real"); let url = "https://x.org/e.geojson?token=hunter2"; - s.url_waits.remember_given_up(800, json!({"url": url})); - let e = s.run("layer.addUrl", &json!({"url": url, "request": 800})).unwrap_err(); + ask(&mut s, &command, json!({"url":url})).unwrap(); + let taken = s.take_url_requests(); + let id = s.download_started_for(&taken[0]).unwrap(); + let n = taken[0].params()["request"].as_u64().unwrap(); + ask(&mut s, &command, json!({"url":"https://x.org/next.csv"})).unwrap(); + assert!(s.url_waits.given_up(n, taken[0].params())); + assert!(s.download_landed(id, br#"{"type":"Point","coordinates":[1,2]}"#.to_vec())); + let mut p = taken[0].params().clone(); + p["id"] = json!(id); + s.merge_undo("late download"); + s.run("map.rename", &json!({"name":"Before late completion"})).unwrap(); + s.merge_undo("late download"); + let e = s.complete_download(id, &p).unwrap_err(); assert!(e.starts_with("https://x.org/e.geojson") && e.contains("stopped waiting") && !e.contains("hunter2"), "{e}"); - s.url_waits.added.push_back((801, json!({"url": url}))); - let e = s.run("layer.addUrl", &json!({"url": url, "request": 801})).unwrap_err(); - assert!(e.starts_with("https://x.org/e.geojson") && e.contains("added already") && !e.contains("hunter2"), "{e}"); + let before = s.undo_depth(); + s.run("map.rename", &json!({"name":"After late completion"})).unwrap(); + assert_eq!(s.undo_depth(), before + 1, "a pre-guard completion refusal must consume its merge key"); + s.run("edit.undo", &Value::Null).unwrap(); + assert_eq!(s.map().name, "Before late completion"); + assert!(s.map().layers.is_empty() && s.downloaded.is_empty()); + rustgis_plugins::unload("eng-refuse-real"); } - /// A caller giving a waited download's number with its address lands it; the app's own - /// download of it landing after isn't added again, and says so (#1202). + /// Bookkeeping matches imports exactly and keeps bounded tombstones until a late delivery. #[test] - fn a_download_landed_twice_is_added_once() { + fn landed_downloads_answer_every_waiting_run() { + let mut waits = UrlWaits::default(); + let asked = json!({"url":"https://x.org/f.csv", "x":"lon", "anonymous":true}); + waits.wait(905, asked.clone()); + assert!(!waits.holds(905, &json!({"url":"https://x.org/f.csv", "x":"lng", "anonymous":true}))); + assert!(waits.holds(905, &json!({"url":"https://x.org/f.csv", "x":"lon", "anonymous":true,"id":3,"cog":true}))); + waits.land(905, Ok(json!({"layer":7})), Added::default()); + assert_eq!(waits.in_flight(&asked, |_| true), Some(905)); + assert_eq!(waits.in_flight(&asked, |_| false), None, "another owner cannot share it"); + assert!(waits.added_already(905, &asked)); + let passed = json!({"url":"https://x.org/g.csv","x":"lon","whole":true,"zoom":false,"dates":["2020"],"name":5,"bounds":"x"}); + waits.wait(906, passed); + for zoom in [None, Some(true), Some(false)] { + let mut p = json!({"url":"https://x.org/g.csv","x":"lon","id":4}); + if let Some(zoom) = zoom { + p["zoom"] = json!(zoom); + } + assert!(waits.holds(906, &p)); + } + assert!(!waits.holds(906, &json!({"url":"https://x.org/g.csv"}))); + waits.give_up(906, true); + assert!(waits.given_up(906, &json!({"url":"https://x.org/g.csv","x":"lon"}))); + waits.refused(906); + assert!(!waits.given_up(906, &json!({"url":"https://x.org/g.csv","x":"lon"}))); + let first = json!({"url":"https://x.org/out.csv"}); + waits.remember_given_up(800, first.clone()); + for n in 1..MAX_EXPIRED as u64 { + waits.remember_given_up(10_000 + n, json!({"url":format!("https://x.org/{n}.csv")})); + } + assert!(waits.given_up_full() && waits.given_up(800, &first)); + waits.refused(800); + assert!(!waits.given_up_full()); let mut s = Session::new(crate::project::Project::default()); - let url = "https://x.org/a.geojson"; - s.url_waits.wait(900, json!({"url": url})); - let point = br#"{"type": "Point", "coordinates": [1, 2]}"#; - let add = |s: &mut Session| { - let id = s.download_started(); - assert!(s.download_landed(id, point.to_vec())); - s.run("layer.addDownload", &json!({"id": id, "url": url, "request": 900})) - }; - add(&mut s).unwrap(); - assert_eq!(s.map().layers.len(), 1); - let mark = s.notice_mark(); - let e = add(&mut s).unwrap_err(); - assert!(e.starts_with(url) && e.contains("added already"), "{e}"); - assert_eq!(s.map().layers.len(), 1, "added once"); - // Said by the session, as any refused command is: once, for every front end. - let said: Vec<&str> = s.notices_since(mark).iter().filter(|n| n.error).map(|n| n.text.as_str()).collect(); - assert_eq!(said, [e.as_str()]); - assert!(s.downloaded.is_empty(), "its bytes go"); - // One that landed read already (a service's protocol buffer pages) goes the same way. - let id = s.download_started(); - let read = rustgis_data::Dataset::new("a", rustgis_data::GeometryType::Point, Vec::new()); - assert!(s.download_landed_read(id, read, rustgis_data::ReadReport::default())); - s.run("layer.addDownload", &json!({"id": id, "url": url, "request": 900})).unwrap_err(); - assert!(s.landed_reads.is_empty(), "its read goes"); + let command = load_test_plugin("eng-full-real"); + ask(&mut s, &command, json!({"url":"https://x.org/kept.csv"})).unwrap(); + for n in 0..MAX_EXPIRED as u64 { + s.url_waits.remember_given_up(10_000 + n, json!({"url":format!("https://x.org/{n}.csv")})); + } + let e = ask(&mut s, &command, json!({"url":"https://x.org/refused.csv"})).unwrap_err(); + assert!(e.contains("still under way") && e.contains("wasn't started"), "{e}"); + let kept = s.take_url_requests(); + assert_eq!(kept.len(), 1); + assert_eq!(kept[0].params()["url"], "https://x.org/kept.csv"); + assert!(s.plugin_retries_waiting() && s.poll_plugin_retries().is_empty()); + rustgis_plugins::unload("eng-full-real"); + for cancelled in + [vec!["https://x.org/a?token=hunter2".into()], vec!["https://x.org/a?token=hunter2".into(), "https://x.org/b".into()]] + { + assert!(!not_started(&cancelled).contains("hunter2")); + } } - /// A download made while a command still waits is that one when asked for again, until what it - /// added is taken away: then a request made since is a new download, not that error (#1202). + /// Sharing is bounded by the original plugin incarnation; duplicate clicks of that owner + /// share one download, while another plugin gets its own real request and answer. #[test] - fn a_download_taken_away_is_downloaded_again() { - let mut s = Session::new(crate::sample::world_project()); - let asked = json!({"url": "https://x.org/a.geojson"}); - let ask = |s: &mut Session| { - s.call_requests = Some(Vec::new()); - s.request_url(&asked).unwrap(); - s.call_requests.take().unwrap()[0].0 - }; - let id = s.map().layers[0].id; - s.url_waits.wait(900, asked.clone()); - let answer: CmdResult = Ok(json!({"layer": id})); - let added = Added::of(s.map(), &answer); - s.url_waits.land(900, answer, added); - assert_eq!(ask(&mut s), 900, "still there"); - assert!(s.take_url_requests().is_empty()); - s.map_mut().layers.retain(|l| l.id != id); - let again = ask(&mut s); - assert_ne!(again, 900, "taken away"); - assert_eq!(s.take_url_requests(), [json!({"url": "https://x.org/a.geojson", "request": again})]); - // A standalone table, which no map holds, the same. - let mut s = Session::new(crate::sample::world_project()); - let table = s.project.add_table(rustgis_data::Dataset::new("t", rustgis_data::GeometryType::None, Vec::new())); - s.url_waits.wait(901, asked.clone()); - let answer: CmdResult = Ok(json!({"table": "t", "id": table})); - let added = Added::of(s.map(), &answer); - s.url_waits.land(901, answer, added); - assert_eq!(ask(&mut s), 901, "still there"); - assert!(s.take_url_requests().is_empty()); - s.project.tables.retain(|t| t.id != table); - assert_ne!(ask(&mut s), 901, "taken away"); - assert_eq!(s.take_url_requests().len(), 1); + fn a_failed_download_asked_again_is_downloaded_again() { + let mut s = Session::new(crate::project::Project::default()); + let a = load_test_plugin("eng-share-real-a"); + let b = load_test_plugin("eng-share-real-b"); + let p = json!({"url":"https://x.org/a.csv"}); + ask(&mut s, &a, p.clone()).unwrap(); + let first = s.take_url_requests(); + ask(&mut s, &a, p.clone()).unwrap(); + assert!(s.take_url_requests().is_empty(), "same pinned owner shares its outstanding request"); + ask(&mut s, &b, p.clone()).unwrap(); + let other = s.take_url_requests(); + assert_ne!(first[0].params()["request"], other[0].params()["request"], "different plugin pins do not share authority"); + s.request_failed(first[0].params(), "404"); + let ran = s.poll_plugin_retries(); + assert_eq!(ran.len(), 1); + assert_eq!(ran[0].0, a); + assert!(ran[0].1.as_ref().unwrap_err().contains("404")); + assert!(s.plugin_retries_waiting(), "the other owner's wait is intact"); + ask(&mut s, &a, p).unwrap(); + let again = s.take_url_requests(); + assert_ne!(again[0].params()["request"], first[0].params()["request"]); + rustgis_plugins::unload("eng-share-real-a"); + rustgis_plugins::unload("eng-share-real-b"); } - /// A download that failed is asked for again as a new download, not answered as the one - /// that failed; one being made or added is the same request still for a plugin command's - /// call, and another download for any other; a request that isn't an object is refused - /// (#1202). + /// Payload numbers never select an existing wait or authority. Native requests still get + /// distinct engine receipts, and non-object requests are refused. #[test] - fn a_failed_download_asked_again_is_downloaded_again() { + fn a_number_given_for_another_address_is_set_aside() { let mut s = Session::new(crate::project::Project::default()); - let asked = json!({"url": "https://x.org/a.geojson"}); - s.url_waits.wait(900, asked.clone()); - s.url_waits.land(900, Err("https://x.org/a.geojson: 404 Not Found".into()), Added::default()); - assert_eq!(s.url_waits.in_flight(&asked), None); - s.request_url(&asked).unwrap(); - let taken = s.take_url_requests(); - assert_eq!(taken.len(), 1, "{taken:?}"); - assert_ne!(taken[0]["request"], json!(900)); - // Added: the same request for a plugin command's call, nothing downloaded again... - let other = json!({"url": "https://x.org/b.geojson"}); - s.url_waits.wait(901, other.clone()); - s.url_waits.land(901, Ok(json!({"layer": 1})), Added::default()); - assert_eq!(s.url_waits.in_flight(&other), Some(901)); - s.call_requests = Some(Vec::new()); - s.request_url(&other).unwrap(); - assert!(s.take_url_requests().is_empty()); - assert_eq!(s.call_requests.take().unwrap()[0].0, 901); - // ... and downloaded again when asked from anywhere else (the Add Data dialog). - s.request_url(&other).unwrap(); + s.request_url(&json!({"url":"https://x.org/a.geojson"})).unwrap(); + let first = s.take_url_requests(); + let n = first[0].params()["request"].as_u64().unwrap(); + s.request_url(&json!({"url":"https://x.org/a.geojson","request":n})).unwrap(); + s.request_url(&json!({"url":"https://x.org/b.geojson","request":n})).unwrap(); let taken = s.take_url_requests(); - assert_eq!(taken.len(), 1, "{taken:?}"); - assert_ne!(taken[0]["request"], json!(901)); - // Being made, the same: that one for a plugin command's call, another from anywhere else. - let third = json!({"url": "https://x.org/c.geojson"}); - s.url_waits.wait(902, third.clone()); - s.call_requests = Some(Vec::new()); - // However often one call asks for it, its command waits for it once. - for _ in 0..3 { - s.request_url(&third).unwrap(); - } - assert!(s.take_url_requests().is_empty()); - let asked: Vec = s.call_requests.take().unwrap().iter().map(|(n, _)| *n).collect(); - assert_eq!(asked, [902]); - s.request_url(&third).unwrap(); - assert_ne!(s.take_url_requests()[0]["request"], json!(902)); + assert_eq!(taken.len(), 2); + assert_ne!(taken[0].params()["request"], json!(n)); + assert_ne!(taken[1].params()["request"], taken[0].params()["request"]); assert!(s.request_url(&json!("https://x.org/d.geojson")).unwrap_err().contains("an object")); } - /// A number given with another address than the download it was given for isn't taken: the - /// request is a download of its own, with its own number; handed back with its own address, - /// it is queued again, whole (#1202). + /// In-place dispatch shares the same terminal result hook as downloaded-byte completion. #[test] - fn a_number_given_for_another_address_is_set_aside() { + fn an_owned_in_place_failure_settles_its_wait() { let mut s = Session::new(crate::project::Project::default()); - s.url_waits.wait(900, json!({"url": "https://x.org/a.geojson"})); - s.request_url(&json!({"url": "https://x.org/b.geojson", "request": 900})).unwrap(); + s.request_url(&json!({"url":""})).unwrap(); let taken = s.take_url_requests(); - assert_eq!(taken.len(), 1, "{taken:?}"); - assert_eq!(taken[0]["url"], "https://x.org/b.geojson"); - assert_ne!(taken[0]["request"], json!(900)); - s.request_url(&json!({"url": "https://x.org/a.geojson", "request": 900})).unwrap(); - let taken = s.take_url_requests(); - assert_eq!((taken[0]["request"].clone(), taken[0]["whole"].clone()), (json!(900), json!(true)), "{taken:?}"); - // The same address read another way (columns it wasn't asked with) isn't it either. - let other_import = json!({"url": "https://x.org/a.geojson", "x": "lon", "y": "lat", "request": 900}); - assert!(!s.url_waits.holds(900, &other_import) && s.url_waits.holds(900, &json!({"url": "https://x.org/a.geojson"}))); - // A number nothing asked under, taken by no one: set aside, so the counter can't reach it - // and queue two downloads under one number. - let next = s.next_request + 1; - s.request_url(&json!({"url": "https://x.org/c.geojson", "request": next + 1})).unwrap(); - s.request_url(&json!({"url": "https://x.org/d.geojson"})).unwrap(); - let taken = s.take_url_requests(); - let numbers: Vec<_> = taken.iter().map(|q| q["request"].as_u64()).collect(); - assert_eq!(numbers, [Some(next), Some(next + 1)], "{taken:?}"); - // Handed back with the same request queued under another number: queued as well, or nothing - // would land the number its command waits for. - let doubled = json!({"url": "https://x.org/f.geojson"}); - s.url_waits.wait(910, doubled.clone()); - s.request_url(&doubled).unwrap(); - s.request_url(&json!({"url": "https://x.org/f.geojson", "request": 910})).unwrap(); - let taken = s.take_url_requests(); - assert_eq!(taken.len(), 2, "{taken:?}"); - assert_eq!((taken[1]["request"].clone(), taken[1]["whole"].clone()), (json!(910), json!(true)), "{taken:?}"); - assert_ne!(taken[0]["request"], json!(910)); - // Given up, its number is set aside too: another download, and the first one's landing, - // under that number, is still refused. - let late = json!({"url": "https://x.org/e.geojson"}); - s.url_waits.remember_given_up(800, late.clone()); - s.request_url(&json!({"url": "https://x.org/e.geojson", "request": 800})).unwrap(); - assert_ne!(s.take_url_requests()[0]["request"], json!(800)); - assert!(s.url_waits.given_up(800, &late)); - let e = s.run("layer.addUrl", &json!({"url": "https://x.org/e.geojson", "request": 800})).unwrap_err(); - assert!(e.contains("stopped waiting"), "{e}"); + let p = taken[0].params(); + let n = p["request"].as_u64().unwrap(); + s.url_waits.wait(n, without_request(p)); + let id = s.download_started_for(&taken[0]).unwrap(); + let error = s.add_in_place_for(id, p).unwrap_err(); + assert!(error.contains("url is required"), "{error}"); + assert!(s.url_waits.landed.iter().any(|(m, _, answer, _)| *m == n && answer.is_err())); + assert!(!s.url_waits.holds(n, p)); } - /// Plugin commands waiting for the same download each get its answer, a rerun that waits - /// again keeps the answers it had, and a download that lands after its command gave up isn't - /// added (#1202). + /// The ordinary pending-download cap can retire a plugin-owned claim below the claim cap. #[test] - fn landed_downloads_answer_every_waiting_run() { - use crate::Session; - for id in ["eng-share-a", "eng-share-b"] { - let pkg = rustgis_plugins::Package::from_zip(&rustgis_plugins::testing::package(id, &["edit", "network"])).unwrap(); - rustgis_plugins::load(pkg, "test").unwrap(); - } - // Answers name layers that are on the map (the sample's), as a rerun checks. - let mut s = Session::new(crate::sample::world_project()); - let (l7, l8) = (u64::from(s.map().layers[0].id), u64::from(s.map().layers[1].id)); - // What a command calls, and what is kept as asked: read with no saved login. - let asked = json!({"url": "https://x.org/a.csv", "anonymous": true}); - let p = json!({"op": "layer.addUrl", "params": {"url": "https://x.org/a.csv"}}); - // Two commands wait for one download (request 900): both get what adding it answered. - for id in ["plugin:eng-share-a/f", "plugin:eng-share-b/f"] { - s.retry_plugin(id, &p, Vec::new(), vec![(900, asked.clone())]).unwrap(); + fn retiring_a_download_claim_settles_only_its_own_wait() { + let mut s = Session::new(crate::project::Project::default()); + let mut requests = Vec::new(); + for url in ["https://retired.invalid/first.geojson", "https://retired.invalid/second.geojson"] { + s.request_url(&json!({"url":url})).unwrap(); + let request = s.take_url_requests().pop().unwrap(); + let n = request.params()["request"].as_u64().unwrap(); + s.url_waits.wait(n, without_request(request.params())); + let id = s.download_started_for(&request).unwrap(); + requests.push((id, n, request)); } - s.url_waits.land(900, Ok(json!({"layer": l7})), Added::default()); - let ran = s.poll_plugin_retries(); - assert_eq!(ran.len(), 2, "{ran:?}"); - assert!(ran.iter().all(|(_, r)| r.as_ref().is_ok_and(|v| v["layer"] == l7)), "{ran:?}"); - assert!(s.take_url_requests().is_empty(), "nothing asked again"); - // A rerun that waits again for another download keeps what it had: a.csv isn't asked again. - s.rerun_landed = vec![(asked.clone(), Ok(json!({"layer": l7})), Default::default())]; - s.retry_plugin("plugin:eng-share-a/f", &p, Vec::new(), vec![(901, json!({"url": "https://x.org/b.csv"}))]).unwrap(); - s.rerun_landed.clear(); - s.url_waits.land(901, Ok(json!({"layer": l8})), Added::default()); - let ran = s.poll_plugin_retries(); - assert_eq!(ran.len(), 1, "{ran:?}"); - assert_eq!(ran[0].1.as_ref().unwrap()["layer"], l7, "{ran:?}"); - assert!(s.take_url_requests().is_empty(), "nothing asked again"); - // The download it waited for answers too, for a run that asks for it. - let b = json!({"url": "https://x.org/b.csv", "anonymous": true}); - let call = json!({"op": "layer.addUrl", "params": {"url": "https://x.org/b.csv"}}); - s.rerun_landed = vec![(asked.clone(), Ok(json!({"layer": l7})), Default::default())]; - s.retry_plugin("plugin:eng-share-a/f", &call, Vec::new(), vec![(904, b.clone())]).unwrap(); - s.rerun_landed.clear(); - s.url_waits.land(904, Ok(json!({"layer": l8})), Added::default()); - let ran = s.poll_plugin_retries(); - assert_eq!(ran[0].1.as_ref().unwrap()["layer"], l8, "{ran:?}"); - // The same address asked another way (other columns) doesn't land a wait. - s.retry_plugin("plugin:eng-share-a/f", &p, Vec::new(), vec![(905, json!({"url": "https://x.org/f.csv", "x": "lon"}))]).unwrap(); - assert!(!s.url_waits.holds(905, &json!({"url": "https://x.org/f.csv", "x": "lng", "id": 3}))); - assert!(s.url_waits.holds(905, &json!({"url": "https://x.org/f.csv", "x": "lon", "id": 3, "cog": true}))); - // What the front end doesn't pass on (`whole`, a `name` that isn't text, `bounds` that - // aren't a list), or may change (`zoom`), doesn't stop a landing. - let asked = - json!({"url": "https://x.org/g.csv", "x": "lon", "whole": true, "zoom": false, "dates": ["2020"], "name": 5, "bounds": "x"}); - s.retry_plugin("plugin:eng-share-a/f", &p, Vec::new(), vec![(906, asked)]).unwrap(); - assert!(s.url_waits.holds(906, &json!({"url": "https://x.org/g.csv", "x": "lon", "id": 4}))); - assert!(s.url_waits.holds(906, &json!({"url": "https://x.org/g.csv", "x": "lon", "zoom": true}))); - assert!(s.url_waits.holds(906, &json!({"url": "https://x.org/g.csv", "x": "lon", "zoom": false}))); - // A key the front end passes on, left out: another import, which lands nothing. - assert!(!s.url_waits.holds(906, &json!({"url": "https://x.org/g.csv"}))); - // Given up past the wait (`expire`): each late landing is refused, once. - // (Only where the clock has run that long: a machine just started can't be that far back.) - if let Some(long_ago) = Instant::now().checked_sub(REQUEST_WAIT + Duration::from_secs(1)) { - for (n, file) in [(902, "c.csv"), (903, "c2.csv")] { - s.url_waits.in_flight.push((n, json!({"url": format!("https://x.org/{file}")}), long_ago)); - } - s.url_waits.expire(); - for (n, file) in [(902, "c.csv"), (903, "c2.csv")] { - let late = json!({"url": format!(" https://x.org/{file} "), "request": n}); - assert!(s.url_waits.given_up(n, &late), "{n}"); - let e = s.run("layer.addUrl", &late).unwrap_err(); - assert!(e.contains("stopped waiting"), "{e}"); - assert!(!s.url_waits.given_up(n, &late), "{n}: forgotten once refused"); + let (first, number, request) = &requests[0]; + let still = s.download_still(*first).unwrap(); + struct Live; + impl crate::policy::Revocation for Live { + fn allows(&self, _: crate::policy::Effects) -> bool { + true } } - // One still out stays given up however many more are: past the bound, a plugin command - // asks for no new download (and its request isn't downloaded); one that fails is - // forgotten, which frees a place. - let first = json!({"url": "https://x.org/out.csv"}); - s.url_waits.expired.clear(); - s.retry_plugin("plugin:eng-share-c/f", &p, Vec::new(), vec![(959, json!({"url": "https://x.org/old.csv"}))]).unwrap(); - s.url_waits.remember_given_up(800, first.clone()); - for n in 1..MAX_EXPIRED as u64 { - s.url_waits.remember_given_up(10_000 + n, json!({"url": format!("https://x.org/{n}.csv")})); + let foreign = crate::policy::Scope::assistant(1521, std::sync::Arc::new(Live)); + s.with_scope(foreign, |s| { + s.download_failed(*first); + assert!(!s.download_done(*first)); + assert!(s.download_continues(*first).is_err()); + assert!(!s.download_landed(*first, Vec::new())); + }) + .unwrap(); + assert!(still() && s.url_waits.holds(*number, request.params()), "foreign callbacks cannot consume the owner's claim or wait"); + for _ in 0..62 { + s.download_started(); } + assert_eq!( + requests.iter().filter(|(id, ..)| s.download_still(*id).is_some()).count(), + 2, + "both original claims remain below the cap" + ); let mark = s.notice_mark(); - s.url_requests.push(json!({"url": "https://x.org/new.csv", "request": 960})); - let e = s.retry_plugin("plugin:eng-share-c/f", &p, Vec::new(), vec![(960, json!({"url": "https://x.org/new.csv"}))]).unwrap_err(); - assert!(e.contains("still under way") && e.contains("its download of https://x.org/new.csv wasn't started"), "{e}"); - for cancelled in [ - vec!["https://x.org/a?token=hunter2".to_string()], - vec!["https://x.org/a?token=hunter2".to_string(), "https://x.org/b".to_string()], - ] { - assert!(!not_started(&cancelled).contains("hunter2")); - } - assert!(s.take_url_requests().is_empty(), "not downloaded"); - // Refused, it changed nothing: the command's earlier run still waits. - assert!(s.plugin_retries.iter().any(|r| r.id == "plugin:eng-share-c/f" && r.requests == [959])); - assert!(s.url_waits.has(959) && !s.notices_since(mark).iter().any(|n| n.text.contains("replaced by this one"))); - // One another command already waits for starts no download: it is shared, not refused. - let shared = json!({"url": "https://x.org/shared.csv"}); - s.url_waits.wait(961, shared.clone()); - s.retry_plugin("plugin:eng-share-d/f", &p, Vec::new(), vec![(961, shared)]).unwrap(); - s.plugin_retries.clear(); - s.url_waits.keep_only(&[]); - assert!(s.url_waits.given_up(800, &first), "still refused when it lands"); - s.request_failed(&json!({"url": "https://x.org/out.csv", "request": 800}), "offline"); - assert!(!s.url_waits.given_up(800, &first) && !s.url_waits.given_up_full()); - s.url_waits.expired.clear(); - // A run replaced by another gives up the download only it waited for. - s.retry_plugin("plugin:eng-share-b/f", &p, Vec::new(), vec![(950, json!({"url": "https://x.org/d.csv"}))]).unwrap(); - s.retry_plugin("plugin:eng-share-b/f", &p, Vec::new(), vec![(951, json!({"url": "https://x.org/e.csv"}))]).unwrap(); - let e = s.run("layer.addUrl", &json!({"url": "https://x.org/d.csv", "request": 950})).unwrap_err(); - assert!(e.contains("stopped waiting"), "{e}"); - // One the front end hadn't taken yet is taken off the queue and forgotten: it can't land. - let queued = json!({"url": "https://x.org/q.csv"}); - s.url_requests.push(json!({"url": "https://x.org/q.csv", "request": 970})); - s.retry_plugin("plugin:eng-share-e/f", &p, Vec::new(), vec![(970, queued.clone())]).unwrap(); - s.retry_plugin("plugin:eng-share-e/f", &p, Vec::new(), Vec::new()).unwrap(); - assert!(!s.url_waits.given_up(970, &queued) && s.take_url_requests().is_empty()); - // A number given with another address lands nothing. - let _ = s.run("layer.addDownload", &json!({"url": "https://x.org/other.csv", "request": 951, "id": 424242})); - assert!(s.url_waits.holds(951, &json!({"url": "https://x.org/e.csv"})), "951 still waits for e.csv"); - // Asked again by a plugin command's call once made, while the command waits: that one, - // not another download. - s.url_waits.land(951, Ok(json!({"layer": l7})), Added::default()); - s.call_requests = Some(Vec::new()); - s.request_url(&json!({"url": "https://x.org/e.csv"})).unwrap(); - s.call_requests = None; - assert!(s.take_url_requests().is_empty(), "made already"); - // Handed back by the front end that took it (the browser found it must be downloaded - // whole): queued again, whole. Asked again by its address alone by a plugin command's - // call, it is that one. - let g = json!({"url": "https://x.org/g.csv"}); - s.retry_plugin("plugin:eng-share-f/f", &p, Vec::new(), vec![(952, g.clone())]).unwrap(); - s.call_requests = Some(Vec::new()); - s.request_url(&g).unwrap(); - s.call_requests = None; - assert!(s.take_url_requests().is_empty(), "under way"); - s.request_url(&json!({"url": "https://x.org/g.csv", "request": 952})).unwrap(); - assert_eq!(s.take_url_requests(), [json!({"url": "https://x.org/g.csv", "request": 952, "whole": true})]); - // A run replaced gives up the download only it waits for, unless a command running again - // in the same poll (out of the list until it has run) waits for it too. - for (n, ready) in [(953, false), (954, true)] { - let h = json!({"url": format!("https://x.org/{n}.csv")}); - s.retry_plugin("plugin:eng-share-g/f", &p, Vec::new(), vec![(n, h.clone())]).unwrap(); - s.rerun_requests = if ready { vec![n] } else { Vec::new() }; - s.retry_plugin("plugin:eng-share-g/f", &p, Vec::new(), Vec::new()).unwrap(); - s.rerun_requests.clear(); - assert_eq!(s.url_waits.given_up(n, &h), !ready, "{n}"); - } - for id in ["eng-share-a", "eng-share-b"] { - rustgis_plugins::unload(id); - } + s.download_started(); + assert!(!still(), "retirement revokes its transport predicate"); + assert!(!s.url_waits.holds(*number, request.params()), "the retired request no longer blocks its plugin"); + let (_, other, request) = &requests[1]; + assert!(s.url_waits.holds(*other, request.params()), "the other wait is untouched"); + assert!(s.url_waits.landed.iter().any(|(n, _, answer, _)| n == number && answer.is_err())); + assert_eq!(s.notices_since(mark).len(), 1, "retirement is reported once"); + s.download_failed(requests[1].0); + assert!(!s.url_waits.holds(*other, request.params()), "an explicit failure also settles its exact wait"); + assert!(s.url_waits.landed.iter().any(|(n, _, answer, _)| n == other && answer.is_err())); } #[test] diff --git a/crates/engine/src/cmd/plugins.rs b/crates/engine/src/cmd/plugins.rs index 591456d727..745ad28b12 100644 --- a/crates/engine/src/cmd/plugins.rs +++ b/crates/engine/src/cmd/plugins.rs @@ -19,22 +19,22 @@ pub mod fetch; pub fn commands() -> Vec { vec![ - C::new("plugin.list", "Plugins", "Plugins › Manage", list).roles(&[]).view(), - C::new("plugin.install", "Install Plugin", "Plugins › Manage", install).roles(&[]) + C::new("plugin.list", "Plugins", "Plugins › Manage", list).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view(), + C::new("plugin.install", "Install Plugin", "Plugins › Manage", install).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Files)) .view() .params(r#"{"path": "a .rgplugin package, or a plugin folder (for development)"}"#), - C::new("plugin.uninstall", "Uninstall Plugin", "Plugins › Manage", uninstall).roles(&[]).view().params(r#"{"id": plugin id}"#), - C::new("plugin.enable", "Enable Plugin", "Plugins › Manage", enable).roles(&[]).view().params(r#"{"id": plugin id, "enabled"?: bool}"#), - C::new("plugin.registry", "Browse Plugins", "Plugins › Manage", browse).roles(&[]) + C::new("plugin.uninstall", "Uninstall Plugin", "Plugins › Manage", uninstall).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params(r#"{"id": plugin id}"#), + C::new("plugin.enable", "Enable Plugin", "Plugins › Manage", enable).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params(r#"{"id": plugin id, "enabled"?: bool}"#), + C::new("plugin.registry", "Browse Plugins", "Plugins › Manage", browse).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Files)) .view() .params(r#"{"url"?: "registry.json (default: https://plugins.rustgis.app/registry.json)"}"#), - C::new("plugin.installFromRegistry", "Install Plugin From Registry", "Plugins › Manage", install_from_registry).roles(&[]) + C::new("plugin.installFromRegistry", "Install Plugin From Registry", "Plugins › Manage", install_from_registry).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Files)) .view() .params(r#"{"id": plugin id, "url"?: registry.json}"#), - C::new("plugin.panel", "Plugin Panel State", "Plugins", panel).roles(&[]).view().params( + C::new("plugin.panel", "Plugin Panel State", "Plugins", panel).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( r#"{"id": plugin id, "values"?: {widget id: true/false, a whole number, or a tree node's id (null for none)}, "refresh"?: bool (run the panel's state command)}"#, ), - C::new("plugin.prepare", "Prepare Plugin Environment", "Plugins › Manage", prepare).roles(&[]) + C::new("plugin.prepare", "Prepare Plugin Environment", "Plugins › Manage", prepare).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)) .view() .params(r#"{"id": plugin id (a process plugin that declares "python": downloads its Python and packages)}"#), ] @@ -56,7 +56,7 @@ fn panel(s: &mut Session, p: &Value) -> CmdResult { } if b(p, "refresh").unwrap_or(false) && !decl.state.is_empty() { // A failed refresh leaves the values as they were, as a failed command does. - if let Err(e) = s.run_plugin(&rustgis_plugins::command_id(id, &decl.state), &json!({})) { + if let Err(e) = s.run(&rustgis_plugins::command_id(id, &decl.state), &json!({})) { *s.panel_state(id, &decl) = before; return Err(e); } @@ -250,201 +250,36 @@ pub(crate) fn leak(s: &str) -> &'static str { new } -/// Commands reserved to the user (see [`user_only`]); plugin management is too. -const NEVER: &[&str] = &[ - "gp.installGeolibre", - "gp.uninstallGeolibre", - "gp.setGeolibre", - "script.run", - "task.run", - "model.run", - // A model saved by a plugin would be trusted to touch files when the user runs it. - "model.new", - "model.save", - "model.import", - "project.new", - "project.open", - "project.openText", - "project.sample", - "project.fromTemplate", - // A QGIS project names the files and web addresses its layers come from, and the import - // adds them without each one passing a plugin's permissions. - "project.importQgis", - "project.importGeolibre", - // Completion callbacks can finish project imports; plugins never supply their state. - "layer.addDownload", - "edit.undo", - "edit.redo", - // Credentials: a plugin never sets where the user's are sent. - "connection.new", - "connection.edit", - "connection.duplicate", - "connection.remove", - "connection.login", - "connection.forgetPassword", - "connection.forgetSavedKeys", - "connection.logout", - "connection.resolveS3", - "connection.test", - "connection.browse", - "s3.connect", - "s3.disconnect", - "earthdata.login", - "earthdata.logout", - // The session log records what was done (#797): a plugin never empties it or turns it off. - "project.clearLog", - "project.logCommands", - "pg.connect", - "pg.disconnect", - // Writes to a database: the user's to do, asked each time. - "pg.saveEdits", - "gpkg.saveEdits", -]; - -/// Commands that write files without naming one in their parameters, or read what the user's -/// commands named (the session log's paths, addresses and SQL). -const REACHES_FILES: &[&str] = &["project.save", "project.saveAs", "project.log"]; - -/// Commands that read a file a layer names, not their parameters. -const READS_FILES: &[&str] = &["gpkg.discardEdits"]; - -/// Parameter keys that name a file, folder or address. (`data` and `text` carry content, which -/// plugins may pass: `layer.addDataBytes`, `map.import` from text.) -pub(crate) const PLACE_KEYS: &[&str] = &["path", "folder", "dir", "file", "url", "uri", "source", "output", "connection", "database"]; - -/// The command running an SQL query through DuckDB, which can read any file and web address. +/// Parameter locations shared with the log's reach heuristic. +pub(crate) const PLACE_KEYS: &[&str] = crate::policy::PLACE_KEYS; pub const SQL: &str = "layer.addSql"; - -/// Commands that read a database, which can be a PostgreSQL server's as well as a file, and a -/// Zarr store, a folder or a web address. -const DATABASES: &[&str] = &[ - SQL, - "layer.addDatabase", - "info.databaseTables", - "sql.query", - "sql.schema", - "raster.zarrInfo", - "layer.addZarr", - "pg.tables", - "pg.addLayer", - "pg.discardEdits", - "zarr.series", - "zarr.reduce", - "cube.trend", - "cube.reduce", -]; - -/// What a command reaches beyond the project when run with some parameters. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Reach { - /// Reads or writes files, or runs a native program (the GeoLibre suite) that does. Files, - /// Fetches from an address (web tiles). Network, + Plugin(&'static str), } - -/// Whether `id` is reserved to the user: plugin management, the GeoLibre runner, the user's own -/// automation (scripts, tasks, models run with the user's trust), and replacing the project or -/// stepping through its undo history. Plugins never run these; the AI assistant asks first. +/// Compatibility projection of the engine's additive declarations. pub fn user_only(id: &str) -> bool { - id.starts_with("plugin.") || NEVER.contains(&id) + use crate::policy::Effect; + id.starts_with("plugin.") + || crate::cmd::all().iter().find(|c| c.id == id).is_some_and(|c| { + [Effect::Automation, Effect::Reserved, Effect::Replaces, Effect::History].into_iter().any(|e| c.effects.contains(e)) + }) } - -/// What running `spec` with `params` reaches beyond the project, if anything: by what the -/// parameters name, what the command's parameters can name, and what it is. Without the session, -/// it counts every `raster.mosaic` as reaching files; `refused` lets a plugin's mosaic of the -/// project's layers through. pub fn reaches(spec: &C, params: &Value) -> Option { - // External tools (the GeoLibre suite) run a native program that reads files a parameter - // names, including bare file names. - if spec.id == "gp.run" && ps(params, "tool").and_then(rustgis_analysis::tool).is_some_and(super::gp::is_geolibre_tool) { - return Some(Reach::Files); - } - // So do tools with a file parameter (a plugin's `file`): they're handed the file. - if spec.id == "gp.run" - && ps(params, "tool").and_then(super::gp::tool_id).and_then(rustgis_analysis::tool).is_some_and(super::gp::reads_files) - { - return Some(Reach::Files); - } - if spec.id == "gp.run" - && ps(params, "tool").and_then(super::gp::tool_id).and_then(rustgis_analysis::tool).is_some_and(super::gp::writes_files) - { - return Some(Reach::Files); - } - // A catalog's Zarr entry is opened as `layer.addZarr` is, a database's way. - if spec.id == "cube.open" && super::cube_catalog::opens_zarr(params) { - return Some(Reach::Files); - } - // Commands that fetch from an address read and write no files, so `files` doesn't come into - // it (their addresses would look like paths to the check below). - if super::tiles::NETWORK.contains(&spec.id) { - return Some(Reach::Network); - } - if DATABASES.contains(&spec.id) { - return Some(Reach::Files); - } - // Georeferencing, projecting and rasterizing write a file only when given a `path` (they read - // a null one as none); the rest of them stays in the project. - if ["raster.georeference", "raster.project", "raster.rasterize"].contains(&spec.id) { - let mut given = params.clone(); - if let Some(o) = given.as_object_mut() { - o.retain(|_, v| !v.is_null()); - } - return super::items::names_place(&given, PLACE_KEYS, 0).then_some(Reach::Files); - } - // A built-in tool's output layer name stays in the session, as it does in a task. - let params = super::items::layer_name_left_out(spec.id, params); - let files = super::items::names_place(¶ms, PLACE_KEYS, 0) - || PLACE_KEYS.iter().any(|k| spec.params.contains(&format!("\"{k}\""))) - || REACHES_FILES.contains(&spec.id) - || READS_FILES.contains(&spec.id) - || ["share.", "offline.", "cache.", "notebook."].iter().any(|p| spec.id.starts_with(p)); - files.then_some(Reach::Files) -} - -/// Why a plugin may not run `id` with `params` on the session `s`, if it may not. -fn refused(s: &Session, plugin: &Plugin, spec: &C, params: &Value) -> Option { - let m = &plugin.manifest; - if user_only(spec.id) { - return Some(format!("plugins can't run '{}'", spec.id)); + use crate::policy::Effect; + let e = crate::policy::command_effects(spec, params); + if e.contains(Effect::Plugin) { + return ps(params, "tool").and_then(super::gp::resolve_tool).map(|t| Reach::Plugin(t.id)); } - // A saved web-service connection's login goes where the user chose it: a plugin never picks - // which login a request carries, as it never logs in or out. - if let Some(id) = params.get("connection").and_then(Value::as_str).map(str::trim) - && s.connections.connections.iter().any(|c| c.id == id && crate::service_logins::is_service(c)) - { - return Some(format!("plugins can't use a saved web-service connection's login ('{}')", spec.id)); - } - // An SQL query reads files and web addresses alike, and a database is a file or a server. - let database = DATABASES.contains(&spec.id) || (spec.id == "cube.open" && super::cube_catalog::opens_zarr(params)); - if database && !(m.has(Permission::Files) && m.has(Permission::Network)) { - return Some(format!("{} needs the 'files' and 'network' permissions to run '{}'", m.name, spec.id)); - } - // A mosaic writes a file only when given a `path`, and reads one for an input that is a raster - // file name and isn't a raster layer with its cells in memory; a mosaic of the project's layers - // stays in it, as `raster.project` does. - let reach = reaches(spec, params).filter(|_| !(spec.id == "raster.mosaic" && super::imagery::mosaic_stays_in_project(s, params))); - match reach { + if e.contains(Effect::Files) { Some(Reach::Files) - if spec.id == "gp.run" - && !m.has(Permission::Files) - && ps(params, "tool").and_then(rustgis_analysis::tool).is_some_and(super::gp::is_geolibre_tool) => - { - return Some(format!("{} needs the 'files' permission to run GeoLibre tools", m.name)); - } - // The address could carry what the plugin read out of the project. - Some(Reach::Network) if !m.has(Permission::Network) => { - return Some(format!("{} needs the 'network' permission to run '{}'", m.name, spec.id)); - } - Some(Reach::Files) if !m.has(Permission::Files) => { - return Some(format!("{} needs the 'files' permission to run '{}'", m.name, spec.id)); - } - _ => {} + } else if e.contains(Effect::Network) { + Some(Reach::Network) + } else { + None } - if spec.mutates && !m.has(Permission::Edit) { - return Some(format!("{} needs the 'edit' permission to run '{}'", m.name, spec.id)); - } - None } /// Answers a running plugin's requests. @@ -461,6 +296,12 @@ struct Bridge<'a> { impl Bridge<'_> { fn need(&self, p: Permission, what: &str) -> Result<(), String> { + if p == Permission::Edit { + return self.s.host_effect(crate::policy::Effects::NONE.with(crate::policy::Effect::Edits), "host.addLayer"); + } + if p == Permission::Network { + return self.s.host_effect(crate::policy::Effects::NONE.with(crate::policy::Effect::Network), "host.fetch"); + } if self.plugin.manifest.has(p) { Ok(()) } else { @@ -534,9 +375,7 @@ impl Bridge<'_> { if id.starts_with(rustgis_plugins::PREFIX) { return Err("plugins can't run other plugins' commands".into()); } - // A download's number (`"request"`) is the app's own: a plugin's is taken out, so it can't - // land another command's wait, or share one it didn't ask for. (Other commands' `request` - // is theirs: `identify.featureInfo`'s asks for the request instead of the answer.) + // Routing receipts belong to engine-issued downloads, never guest JSON. let unnumbered; let params = if matches!(id, "layer.addUrl" | "layer.addDownload") && params.get("request").is_some() { unnumbered = fetch::without_request(params); @@ -544,11 +383,13 @@ impl Bridge<'_> { } else { params }; - let spec = self.s.registry.get(id).ok_or_else(|| format!("unknown command '{id}'"))?; - if let Some(why) = refused(self.s, &self.plugin, spec, params) { - return Err(why); + if let Some(connection) = params.get("connection").and_then(Value::as_str).map(str::trim) + && self.s.connections.connections.iter().any(|c| c.id == connection && crate::service_logins::is_service(c)) + { + return Err(format!("plugins can't use a saved web-service connection's login ('{id}')")); } - let (enabled, run, label, mutates) = (spec.enabled, spec.run, spec.label, spec.mutates); + let spec = self.s.registry.get(id).ok_or_else(|| format!("unknown command '{id}'"))?; + let (enabled, label, mutates) = (spec.enabled, spec.label, spec.mutates); let named = params.get("layer").and_then(|_| self.s.layer_id(params).ok()); let active = self.s.active_layer; if named.is_some() { @@ -572,21 +413,11 @@ impl Bridge<'_> { // command that only left a request for the front end ([`Session::request_url`]) changed // nothing; a later one must not erase the undo step for an earlier edit. self.s.request_only = false; - // No saved web-service login goes with what a plugin asks for: the user chooses where it is sent. - let anonymous = self.s.web_logins.set_anonymous(true); - let mut res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - let before = if mutates { crate::service_logins::before(self.s, id, params)? } else { None }; - let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| super::workflows::steps_of(self.s, |s| run(s, params)))) - .unwrap_or_else(|_| Err(format!("{label} failed unexpectedly"))); - // Mark additions even when a command reports an error after changing the project. - // Their addresses must remain anonymous at later logins and after saving/reopening. - if let Some(before) = before { - crate::service_logins::tag(self.s, params, before, out.as_ref().unwrap_or(&Value::Null))?; - } - out - })) + let mut res = crate::service_logins::anonymously(self.s, true, |s| { + let op = s.authorize(id, params)?; + super::workflows::steps_of(s, |s| s.invoke(op, crate::policy::Txn::Shared)) + }) .unwrap_or_else(|_| Err(format!("{label} failed unexpectedly"))); - self.s.web_logins.set_anonymous(anonymous); let requested = std::mem::take(&mut self.s.request_only); self.requested |= requested; self.changed |= mutates && !requested; @@ -628,7 +459,9 @@ impl Host for Bridge<'_> { "host.fetch" => { // The address, and what the plugin answers, can carry what it read from the project. self.need(Permission::Network, "fetch from the web")?; - self.fetches.fetch(params) + let c = self.s.continuation()?; + let still = Arc::new(move || c.check().is_ok()); + crate::render::rustgis_tiles::cache::with_still(still, || self.fetches.fetch(params)) } _ if op.starts_with("host.") => Err(format!("unknown request '{op}'")), _ => self.command(op, params), @@ -642,13 +475,16 @@ impl Host for Bridge<'_> { impl Session { /// Runs a plugin command (`plugin:/`) as one undoable step. - pub(crate) fn run_plugin(&mut self, id: &str, params: &Value) -> CmdResult { - let Some(cmd) = rustgis_plugins::command(id) else { - return Err(match rustgis_plugins::split_id(id).and_then(|(p, _)| rustgis_plugins::get(p)) { - Some(p) if !p.enabled => format!("the plugin {} is disabled", p.manifest.name), - _ => format!("unknown command '{id}'"), - }); - }; + pub(crate) fn run_plugin(&mut self, op: crate::policy::Authorized) -> CmdResult { + let crate::policy::CommandOp::Plugin { cmd, pin, params } = op.op() else { return Err("not a plugin command".into()) }; + let cmd = cmd.clone(); + if !rustgis_plugins::is_current(pin.plugin()) { + return Err("the plugin was disabled, updated or removed".into()); + } + let params = params.clone(); + let params = ¶ms; + let id = rustgis_plugins::command_id(&cmd.plugin.manifest.id, &cmd.decl.id); + let id = id.as_str(); let label = leak(&cmd.decl.label); let mutates = cmd.mutates(); let plugin = cmd.plugin.clone(); @@ -682,6 +518,8 @@ impl Session { let state_command = panel.as_ref().is_some_and(|d| !d.state.is_empty() && d.state == cmd.decl.id); let mut pending = Vec::new(); let mut requested = Vec::new(); + let mut suspended = Vec::new(); + let queued_before = self.queued_authority(); // The downloads this call leaves to the app: its own, apart from a calling command's. let outer = self.call_requests.replace(Vec::new()); let mark = self.notice_mark(); @@ -690,12 +528,15 @@ impl Session { let mut bridge = Bridge { s, plugin, fetches, changed: false, requested: false }; let res = rustgis_plugins::run_command_with_panel(&cmd, &mut bridge, params, request); let asked = bridge.s.call_requests.as_mut().map(std::mem::take).unwrap_or_default(); - if !bridge.fetches.pending.is_empty() || !asked.is_empty() { + if res.is_ok() && (!bridge.fetches.pending.is_empty() || !asked.is_empty()) { // Waiting for a fetch (the browser) or a download left to the app: whatever the // call did is undone, quietly, and the command runs again from the start once // the answers are in and the downloads added (#1202). pending = std::mem::take(&mut bridge.fetches.pending); requested = asked; + // Retain the exact engine-issued requests only for successful suspension. + // A genuine guest error/panic keeps guarded's ordinary retirement behavior. + suspended = bridge.s.suspend_plugin_requests(&queued_before, &requested); bridge.s.quiet_failure = true; return Err(String::new()); } @@ -723,17 +564,11 @@ impl Session { if pending.is_empty() && requested.is_empty() { return res; } - // The undone call took its download requests with it: they go to the app still, once, - // unless the command gives up now (it has run again too often): then nothing waits for them. - let giving_up = self.retry_tries >= fetch::MAX_RETRIES; - for (n, asked) in requested.iter().filter(|_| !giving_up) { - // Queued already: this number for this address (another caller may give the number). - let taken = self.url_requests.iter().any(|q| q["request"].as_u64() == Some(*n) && fetch::same_request(q, asked)); - if !taken && !self.url_waits.has(*n) { - let mut q = asked.clone(); - q["request"] = json!(n); - self.url_requests.push(q); - } + // Restore only the original opaque authorities removed by intentional suspension. + // No JSON request can create a fresh User admission after the rollback. + let suspended_keys: Vec = suspended.iter().map(|q| q.key).collect(); + if self.retry_tries < fetch::MAX_RETRIES { + self.restore_plugin_requests(suspended)?; } // The errors this run said stay: the next run, doing the same again, doesn't repeat them. let earlier = self.rerun_said.len(); @@ -743,6 +578,7 @@ impl Session { let res = self.retry_plugin(id, given, pending, requested); self.rerun_said.truncate(earlier); if let Err(e) = &res { + self.discard_plugin_requests(&suspended_keys); self.notify(e.clone(), true); } res @@ -801,14 +637,17 @@ mod tests { fn a_plugin_cant_choose_a_downloads_number() { let pkg = rustgis_plugins::Package::from_zip(&rustgis_plugins::testing::package("eng-number", &["edit", "network"])).unwrap(); rustgis_plugins::load(pkg, "test").unwrap(); - let plugin = rustgis_plugins::get("eng-number").unwrap(); let mut s = Session::new(crate::Project::default()); - s.leave_downloads = true; - let fetches = fetch::Calls::for_plugin("eng-number"); - let mut bridge = Bridge { s: &mut s, plugin, fetches, changed: false, requested: false }; - bridge.call("layer.addUrl", &json!({"url": "https://x.org/a.geojson", "request": 99})).unwrap(); + s.run_leaving_downloads( + "plugin:eng-number/f", + &json!({ + "op":"layer.addUrl", "params":{"url":"https://x.org/a.geojson", "request":99} + }), + ) + .unwrap(); let taken = s.take_url_requests(); - assert!(taken.len() == 1 && taken[0]["request"] != json!(99), "{taken:?}"); + assert_eq!(taken.len(), 1); + assert_ne!(taken[0].params()["request"], json!(99)); rustgis_plugins::unload("eng-number"); } diff --git a/crates/engine/src/cmd/portals.rs b/crates/engine/src/cmd/portals.rs index 6c8c2d8db2..9541bc670c 100644 --- a/crates/engine/src/cmd/portals.rs +++ b/crates/engine/src/cmd/portals.rs @@ -11,20 +11,20 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("osm.download", "Download OpenStreetMap Data", "Home › Add › Web Data", osm_download).roles(&[]).params( + C::new("osm.download", "Download OpenStreetMap Data", "Home › Add › Web Data", osm_download).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"bbox"?: [west, south, east, north] | "view" (the map's extent, the default), "view"?: bool (false: a bbox must be given), "preset"?: "buildings"|"roads"|"railways"|"water"|"landuse"|"parks"|"pois"|"boundaries", "tags"?: "key=value, key" (any of them), "url"?: "an Overpass server (default https://overpass-api.de/api/interpreter)", "name"?: text, "answer"?: "the server's JSON answer, when already fetched (the app's background request)"}"#, ), - C::new("boundaries.add", "Add Administrative Boundaries", "Home › Add › Web Data", boundaries_add).roles(&[]).params( + C::new("boundaries.add", "Add Administrative Boundaries", "Home › Add › Web Data", boundaries_add).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"country": "ISO 3166 alpha-3 code (CHE, KEN…)", "level"?: "ADM0" (the country, default) … "ADM5", "name"?: text, "text"?: "the boundaries' GeoJSON, when already fetched (the app's background request)"}"#, ), - C::new("boundaries.countries", "List geoBoundaries Countries", "Home › Add › Web Data", boundaries_countries).roles(&[]).view(), - C::new("portal.search", "Search Open Data", "Home › Add › Web Data", portal_search).roles(&[]).view().params( + C::new("boundaries.countries", "List geoBoundaries Countries", "Home › Add › Web Data", boundaries_countries).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).view(), + C::new("portal.search", "Search Open Data", "Home › Add › Web Data", portal_search).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).view().params( r#"{"query"?: text (needed by ckan, socrata, arcgis-hub and huggingface), "portal"?: "ckan" (data.gov's catalogue, the default, or any CKAN site) | "socrata" | "arcgis-hub" | "arcgis-portal" (ArcGIS Online, or the portal at url) | "earthdata-gis" (NASA's) | "openaerialmap" | "usgs-dem" (3DEP elevation models) | "ign-lidar" (IGN's LiDAR HD tiles, France) | "geolens" | "huggingface" (a dataset's files: portal.files) | "ocean-data" | "dynamical" (weather and climate Zarr stores), "url"?: "the portal: a CKAN site (https://open.canada.ca/data), a Socrata site (data.cityofnewyork.us), an ArcGIS Hub site, an ArcGIS portal, a GeoLens server", "org"?: "an ArcGIS organisation's id: its public items only", "type"?: "an ArcGIS portal's kind of item: all (the default), imagery, maps, features or webmaps", "dataset"?: "usgs-dem's elevation model: 1 meter, 1/9 arc-second (about 3 m), 1/3 arc-second (about 10 m, the default), 1 arc-second (about 30 m), Alaska 5 meter", "bbox"?: [west, south, east, north] | "view", "view"?: bool (true: where the map is; openaerialmap, usgs-dem and ign-lidar search where the map is unless given a bbox), "limit"?: 1–50 (20)}"#, ), - C::new("portal.files", "List a Dataset's Files", "Home › Add › Web Data", portal_files).roles(&[]).view().params( + C::new("portal.files", "List a Dataset's Files", "Home › Add › Web Data", portal_files).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).view().params( r#"{"dataset": "a Hugging Face dataset, owner/name, or an ArcGIS web map's item id (portal.search lists them)", "portal"?: "huggingface" (the default) | "arcgis-portal" | "earthdata-gis", "url"?: "the ArcGIS portal"}"#, ), - C::new("portal.add", "Add Open Data", "Home › Add › Web Data", portal_add).roles(&[]).params( + C::new("portal.add", "Add Open Data", "Home › Add › Web Data", portal_add).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"url": "a resource's address, as portal.search lists it", "use"?: "file" (read by address, the default) | "zarr" (a store: its variable, or the first, at "index"'s steps, as layer.addZarr) | "wms" | "wfs" | "arcgis" (a service), "variable"?: "a Zarr store's variable", "index"?: {"time": 0}, "layer"?: "a service's layer (default: its first)", "name"?: text, "portal"?: "the portal's address the resource came from (an address it gives may not reach into the local network)"}"#, ), ] diff --git a/crates/engine/src/cmd/presentation.rs b/crates/engine/src/cmd/presentation.rs index 98b17dfbc2..cfcbf73994 100644 --- a/crates/engine/src/cmd/presentation.rs +++ b/crates/engine/src/cmd/presentation.rs @@ -43,7 +43,7 @@ pub fn commands() -> Vec { let i = s.project.presentations.len() - 1; s.active_presentation = Some(i); Ok(json!({"presentation": i, "name": name})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name"?, "title"?: the title page's text}"#), C::new("presentation.import", "Import Presentation", "Publish › Presentation", |s, p| { let text = super::items::read_text(p)?; @@ -58,13 +58,13 @@ pub fn commands() -> Vec { let i = s.project.presentations.len() - 1; s.active_presentation = Some(i); Ok(json!({"presentation": i, "name": name})) - }).roles(&[crate::ParamRole { key: "text", role: crate::Role::Document(crate::Document::Presentation) }]) + }).roles(&[crate::ParamRole { key: "text", role: crate::Role::Document(crate::Document::Presentation) }]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"path"?: "*.rgispres", "text"?: presentation JSON, "data"?: base64 file}"#), C::new("presentation.activate", "Open Presentation", "Browser", |s, p| { let i = index(s, p)?; s.active_presentation = Some(i); Ok(json!({"presentation": i})) - }).roles(&[]) + }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .view() .when(need_presentation) .params(r#"{"presentation": name|index}"#), @@ -73,14 +73,11 @@ pub fn commands() -> Vec { s.project.presentations.remove(i); s.active_presentation = None; Ok(Value::Null) - }).roles(&[]) + }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_presentation) .params(r#"{"presentation"?}"#), C::new("presentation.mapPage", "Map Page", "Publish › Page", |s, p| { - let map = match ps(p, "map") { - Some(n) => s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no map '{n}'"))?, - None => s.map(), - }; + let map = map_page_source(s,p)?; let name = map.name.clone(); // The map view as it is now, unless a place is given. let c = map.center_lonlat(); @@ -88,14 +85,14 @@ pub fn commands() -> Vec { let width_m = f(p, "widthM").unwrap_or(s.view_size.0 * map.ground_res()).clamp(10.0, 4e7); let title = ps(p, "title").unwrap_or(&name).to_string(); add_page(s, p, title, PageKind::Map { map: name, center, width_m }) - }).roles(&[]) + }).inputs(map_page_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_presentation) .params(r#"{"presentation"?, "map"?: name (default: the active map, at its current view), "lon"?, "lat"?, "widthM"?: ground width shown (m), "title"?, "at"?: page index}"#), C::new("presentation.blankPage", "Blank Page", "Publish › Page", |s, p| { let background = super::layout::color(p, "background", Rgba::rgb(0xFF, 0xFF, 0xFF))?; let text: String = ps(p, "text").unwrap_or("").chars().take(MAX_TEXT).collect(); add_page(s, p, ps(p, "title").unwrap_or("").to_string(), PageKind::Blank { text, background }) - }).roles(&[]) + }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_presentation) .params(r##"{"presentation"?, "title"?, "text"?, "background"?: "#RRGGBB"|"#RRGGBBAA", "at"?}"##), C::new("presentation.imagePage", "Image Page", "Publish › Page", |s, p| { @@ -106,7 +103,7 @@ pub fn commands() -> Vec { }; let caption: String = ps(p, "caption").unwrap_or("").chars().take(MAX_CAPTION).collect(); add_page(s, p, ps(p, "title").unwrap_or("").to_string(), PageKind::Image { image, caption }) - }).roles(&[]) + }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .when(need_presentation) .params(r#"{"presentation"?, "path"?: PNG/JPEG file, "image"?: base64, "data"?: base64, "title"?, "caption"?, "at"?}"#), C::new("presentation.videoPage", "Video Page", "Publish › Page", |s, p| { @@ -119,10 +116,10 @@ pub fn commands() -> Vec { let caption: String = ps(p, "caption").unwrap_or("").chars().take(MAX_CAPTION).collect(); let title = ps(p, "title").map(str::to_string).unwrap_or_else(|| video_label(&source)); add_page(s, p, title, PageKind::Video { source, poster, caption }) - }).roles(&[]) + }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .when(need_presentation) .params(r#"{"presentation"?, "source": video file or URL, "poster"?: PNG/JPEG file, "posterImage"?: base64, "title"?, "caption"?, "at"?}"#), - C::new("presentation.updatePage", "Page Properties", "Presentation", update_page).roles(&[]).when(need_presentation).params( + C::new("presentation.updatePage", "Page Properties", "Presentation", update_page).inputs(page_inputs).roles(&[]).effects(crate::policy::Effects::NONE).when(need_presentation).params( r##"{"presentation"?, "page": index, "title"?, "text"?, "caption"?, "notes"?, "seconds"?, "background"?: "#RRGGBB"|"#RRGGBBAA", "moveTo"?: index}"##, ), C::new("presentation.removePage", "Delete Page", "Presentation", |s, p| { @@ -130,7 +127,7 @@ pub fn commands() -> Vec { let pres = s.project.presentations.get_mut(i).ok_or("no presentation")?; pres.pages.remove(k); Ok(json!({"pages": pres.pages.len()})) - }).roles(&[]) + }).inputs(page_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_presentation) .params(r#"{"presentation"?, "page": index}"#), C::new("presentation.save", "Presentation File", "Publish › Save As", |s, p| { @@ -139,7 +136,7 @@ pub fn commands() -> Vec { let bytes = presentation_file(s, i)?; rustgis_data::io::write_atomic(Path::new(path), &bytes).map_err(|e| format!("{path}: {e}"))?; Ok(json!({"path": path})) - }).roles(&[]) + }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_presentation) .params(r#"{"presentation"?, "path": "*.rgispres"}"#), @@ -160,7 +157,7 @@ pub fn commands() -> Vec { let (bytes, pages) = export_bytes(s, i, format, dpi)?; rustgis_data::io::write_atomic(Path::new(&path), &bytes).map_err(|e| format!("{path}: {e}"))?; Ok(json!({"path": path, "pages": pages, "bytes": bytes.len()})) - }).roles(&[]) + }).inputs(export_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_presentation) .params(r#"{"presentation"?, "path": "*.pdf|*.html (a self-contained slideshow, videos playable)|*.png (the first page)", "dpi"?: 48..300}"#), @@ -190,7 +187,9 @@ pub(crate) fn video_label(source: &str) -> String { } /// The presentation named or numbered by `presentation`, else the active one, else the last. -pub fn index(s: &Session, p: &Value) -> Result { +fn index_input(input: &impl super::InputSource, p: &Value) -> Result { + let context = input.input_context(); + let s = &context; let ps_ = &s.project.presentations; match p.get("presentation") { Some(Value::Number(n)) => { @@ -209,8 +208,10 @@ pub fn index(s: &Session, p: &Value) -> Result { } } -fn page_of(s: &Session, p: &Value) -> Result<(usize, usize), String> { - let i = index(s, p)?; +fn page_of(input: &impl super::InputSource, p: &Value) -> Result<(usize, usize), String> { + let context = input.input_context(); + let s = &context; + let i = index_input(input, p)?; let n = s.project.presentations.get(i).map_or(0, |x| x.pages.len()); let k = p.get("page").and_then(Value::as_u64).and_then(|k| usize::try_from(k).ok()).ok_or("page is required")?; if k >= n { @@ -328,8 +329,7 @@ pub fn page_layout(s: &Session, pres: &Presentation, page: &Page) -> Layout { l.add("Title", (m, m, w - 2.0 * m, h * 0.12), text(&page.title, 30.0, dark, true)); } let (fw, fh) = (w - 2.0 * m, h - top - m); - let map = - s.project.maps.iter().find(|x| x.name.eq_ignore_ascii_case(map)).map_or_else(|| s.map().name.clone(), |x| x.name.clone()); + let map = page_map(s, map).name.clone(); let scale = (width_m / (fw / 1000.0)).clamp(rustgis_map::layout::MIN_FRAME_SCALE, rustgis_map::layout::MAX_FRAME_SCALE); l.add( "Map", @@ -531,3 +531,62 @@ document.addEventListener('click',e=>{{if(e.target.tagName!=='VIDEO')go(i+1)}}); ); Ok((html.into_bytes(), n)) } + +/// Resolves a supplied string map or the active-map default used to initialize a page. +fn map_page_source<'a>(input: &'a impl super::InputSource, p: &Value) -> Result<&'a rustgis_map::Map, String> { + let context = input.input_context(); + let s = &context; + match ps(p, "map") { + Some(name) => s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(name)).ok_or_else(|| format!("no map '{name}'")), + None => Ok(s.map()), + } +} +/// Stored page map names fall back to the active map when absent, matching rendering. +fn page_map<'a>(input: &'a impl super::InputSource, name: &str) -> &'a rustgis_map::Map { + let context = input.input_context(); + let s = &context; + s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(name)).unwrap_or_else(|| s.map()) +} +/// Binds the map whose current view initializes the page. +fn map_page_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let map = map_page_source(input, p)?; + let mut inputs = object_inputs(input, p)?; + inputs.maps.push(map.instance_id()); + Ok(inputs) +} +/// PNG consumes page zero; PDF/HTML consume all pages, using the renderer's stored-map fallback. +fn export_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let i = index_input(input, p)?; + let pres = s.project.presentations.get(i).ok_or("no presentation")?; + let png = ps(p, "path").is_some_and(|p| p.to_ascii_lowercase().ends_with(".png")); + let mut inputs = super::Inputs::layers(std::iter::empty()); + for page in pres.pages.iter().take(if png { 1 } else { usize::MAX }) { + if let PageKind::Map { map, .. } = &page.kind { + let map = page_map(input, map); + inputs.extend(super::tiles::render_inputs(map)); + } + } + inputs.state = json!({"presentation":i,"name":pres.name,"maps":pres.pages.iter().take(if png {1}else {usize::MAX}).enumerate().filter_map(|(i,p)|match &p.kind {PageKind::Map{map,..}=>Some((i,map)),_=>None}).collect::>(),"tiles":inputs.state}); + Ok(inputs) +} + +/// Stored page references are not read by ordinary object edits or serialization. +fn object_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let i = index_input(input, p)?; + let mut inputs = super::Inputs::layers(std::iter::empty()); + inputs.state = json!({"presentation":i,"name":s.project.presentations.get(i).map(|p|&p.name)}); + Ok(inputs) +} +/// Page editing uses the executor's required numeric page selector. +fn page_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let mut inputs = object_inputs(input, p)?; + inputs.state["page"] = json!(page_of(input, p)?.1); + Ok(inputs) +} + +/// The presentation/report selected by the live session, using the same pure resolver as approval. +pub fn index(s: &Session, p: &Value) -> Result { + index_input(s, p) +} diff --git a/crates/engine/src/cmd/project.rs b/crates/engine/src/cmd/project.rs index bc60021751..84e4d989e6 100644 --- a/crates/engine/src/cmd/project.rs +++ b/crates/engine/src/cmd/project.rs @@ -11,36 +11,36 @@ pub fn commands() -> Vec { vec![ // Replacing or saving the project isn't an undoable edit: these are `.view()` commands // that manage `dirty` and the history themselves. - C::new("project.new", "New Project", "Project", new).roles(&[]).key("Mod+N").view().params(r#"{"name"?: string}"#), - C::new("project.sample", "World Atlas Sample", "Project", sample).roles(&[]).view(), - C::new("project.open", "Open Project", "Project", open).roles(&[]).key("Mod+O").view().params(r#"{"path": string}"#), - C::new("project.openText", "Open Project (from text)", "Project", open_text).roles(&[crate::ParamRole { key: "text", role: crate::Role::Document(crate::Document::Project) }]).view().params(r#"{"text": project JSON, "fileName"?: string, "package"?: "true for package contents: never restore saved layer secrets"}"#), - C::new("project.save", "Save Project", "Project", save).roles(&[]).key("Mod+S").view().params(r#"{"path"?: string}"#), - C::new("project.saveAs", "Save Project As", "Project", save_as).roles(&[]).key("Mod+Shift+S").view().params(r#"{"path": string}"#), - C::new("project.rename", "Rename Project", "Project", rename).roles(&[]).params(r#"{"name": string}"#), - C::new("edit.undo", "Undo", "Quick Access Toolbar", |s, _| if s.undo() { Ok(Value::Null) } else { Err("nothing to undo".into()) }).roles(&[]) + C::new("project.new", "New Project", "Project", new).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Replaces)).key("Mod+N").view().params(r#"{"name"?: string}"#), + C::new("project.sample", "World Atlas Sample", "Project", sample).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Replaces)).view(), + C::new("project.open", "Open Project", "Project", open).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Replaces).with(crate::policy::Effect::Files)).key("Mod+O").view().params(r#"{"path": string}"#), + C::new("project.openText", "Open Project (from text)", "Project", open_text).roles(&[crate::ParamRole { key: "text", role: crate::Role::Document(crate::Document::Project) }]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Replaces).with(crate::policy::Effect::Network)).view().params(r#"{"text": project JSON, "fileName"?: string, "package"?: "true for package contents: never restore saved layer secrets"}"#), + C::new("project.save", "Save Project", "Project", save).inputs(save_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).key("Mod+S").view().params(r#"{"path"?: string}"#), + C::new("project.saveAs", "Save Project As", "Project", save_as).inputs(save_as_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).key("Mod+Shift+S").view().params(r#"{"path": string}"#), + C::new("project.rename", "Rename Project", "Project", rename).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"name": string}"#), + C::new("edit.undo", "Undo", "Quick Access Toolbar", |s, _| if s.undo() { Ok(Value::Null) } else { Err("nothing to undo".into()) }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::History)) .key("Mod+Z") .view() .when(|s| if s.can_undo() { None } else { Some("nothing to undo") }), - C::new("edit.redo", "Redo", "Quick Access Toolbar", |s, _| if s.redo() { Ok(Value::Null) } else { Err("nothing to redo".into()) }).roles(&[]) + C::new("edit.redo", "Redo", "Quick Access Toolbar", |s, _| if s.redo() { Ok(Value::Null) } else { Err("nothing to redo".into()) }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::History)) .key("Mod+Y") .view() .when(|s| if s.can_redo() { None } else { Some("nothing to redo") }), - C::new("tool.set", "Set Map Tool", "Home › Navigate", set_tool).roles(&[]) + C::new("tool.set", "Set Map Tool", "Home › Navigate", set_tool).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"tool": "explore"|"select"|"selectPolygon"|"selectLasso"|"selectRadius"|"zoomIn"|"measure"|"measureArea"|"createPoint"|"createLine"|"createPolygon"|"editVertices"|"cutPolygon"|"reshape"|"graphicPoint"|"graphicText"|"graphicLine"|"graphicCurve"|"graphicFreehand"|"graphicPolygon"|"graphicLasso"|"graphicRectangle"|"graphicCircle"|"graphicEllipse"|"graphicPicture"}"#), - C::new("info.project", "Project Info", "Project", info).roles(&[]).view().params(r#"{"nameFilter"?: string, "includeFields"?: bool}"#), - C::new("info.commands", "List Commands", "Help", list).roles(&[]).view(), + C::new("info.project", "Project Info", "Project", info).inputs(super::tiles::listing_inputs).roles(&[]).effects(crate::policy::Effects::NONE).view().params(r#"{"nameFilter"?: string, "includeFields"?: bool}"#), + C::new("info.commands", "List Commands", "Help", list).roles(&[]).effects(crate::policy::Effects::NONE).view(), // The session log (#797): read, written out, emptied, turned on or off. It sits outside // the project, so none of these changes the project or takes an undo step. - C::new("project.log", "Session Log", "Project", log).roles(&[]).view().params( + C::new("project.log", "Session Log", "Project", log).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).view().params( r#"{"since"?: milliseconds since 1970 (UTC), "until"?: milliseconds, "command"?: "a command id, or the start of one (layer.)", "failed"?: bool (true: only those that failed; false: only those that succeeded), "limit"?: entries, a whole number (default 100, the newest; 1 to 1000)} → {"entries": [{"at", "command", "params", "ok", "error"?, "millis"}], "count": matching, "total": in the log, "on": bool}"#, ), - C::new("project.exportLog", "Export Session Log", "Project", export_log).roles(&[]) + C::new("project.exportLog", "Export Session Log", "Project", export_log).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"path": ".csv, .jsonl or .ndjson file", "format"?: "csv"|"jsonl" (for a path with no extension; one given must match the extension)}"#), - C::new("project.clearLog", "Clear Session Log", "Project", clear_log).roles(&[]).view(), - C::new("project.logCommands", "Log Commands", "Project", log_commands).roles(&[]).view().params(r#"{"on"?: bool (default: turn it the other way)}"#), + C::new("project.clearLog", "Clear Session Log", "Project", clear_log).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view(), + C::new("project.logCommands", "Log Commands", "Project", log_commands).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params(r#"{"on"?: bool (default: turn it the other way)}"#), ] } @@ -162,6 +162,7 @@ fn reset(s: &mut Session, p: Project) { } s.opened_package = None; s.opened = s.opened.wrapping_add(1); + s.sync_policy_generation(); s.active_layer = s.map().layers.first().map(|l| l.id); s.table_layer = None; s.table_standalone = None; @@ -327,10 +328,7 @@ fn name_from_file(s: &mut Session, file: &std::path::Path) { } fn save(s: &mut Session, p: &Value) -> CmdResult { - let path = match super::ps(p, "path") { - Some(x) => PathBuf::from(x), - None => s.path.clone().ok_or("this project hasn't been saved yet; use Save As")?, - }; + let path = save_path(s, p)?; write(s, path) } @@ -519,3 +517,25 @@ fn label_classes_set(labels: &rustgis_map::Labeling) -> bool { || d.name != rustgis_map::symbology::DEFAULT_LABEL_CLASS || d.where_.as_deref().is_some_and(|w| !w.trim().is_empty()) } + +/// Save uses the explicit string path, otherwise the current native project path. +fn save_path(input: &impl super::InputSource, p: &Value) -> Result { + let context = input.input_context(); + let s = &context; + match super::ps(p, "path") { + Some(x) => Ok(PathBuf::from(x)), + None => s.path.clone().ok_or_else(|| "this project hasn't been saved yet; use Save As".into()), + } +} +/// Binds the full exported project and the same implicit destination used by Save. +fn save_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let mut inputs = super::items::project_inputs(input, p)?; + inputs.state = json!({"path":save_path(input,p)?}); + Ok(inputs) +} +/// Save As requires its explicit path; there is no stored-path fallback. +fn save_as_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let mut inputs = super::items::project_inputs(input, p)?; + inputs.state = json!({"path":ps(p,"path").ok_or("path is required")?}); + Ok(inputs) +} diff --git a/crates/engine/src/cmd/qgis.rs b/crates/engine/src/cmd/qgis.rs index 0b3d9c2405..323177a230 100644 --- a/crates/engine/src/cmd/qgis.rs +++ b/crates/engine/src/cmd/qgis.rs @@ -12,10 +12,10 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("project.importQgis", "Import QGIS Project", "Project › Import", import_project).roles(&[]).params( + C::new("project.importQgis", "Import QGIS Project", "Project › Import", import_project).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Files)).params( r#"{"path"?: "*.qgs|*.qgz", "text"?: "a .qgs file's text", "data"?: "a .qgs or .qgz file, base64", "dir"?: "the folder its relative paths start from (default: the file's)"} → the layers added and notes on what wasn't carried over"#, ), - C::new("style.importQgis", "Import QGIS Style", "Style › Styles", import_style).roles(&[]).when(need_layer).params( + C::new("style.importQgis", "Import QGIS Style", "Style › Styles", import_style).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).when(need_layer).params( r#"{"layer"?, "path"?: "*.qml", "text"?: "a .qml file's text", "data"?: "a .qml file, base64"} → notes on what wasn't carried over"#, ), ] diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 8e1f34b808..e696b5cbe8 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -33,12 +33,12 @@ pub(crate) fn need_raster(s: &Session) -> Option<&'static str> { pub fn commands() -> Vec { vec![ - C::new("layer.addDataBytes", "Add Data (from file contents)", "Home › Add", add_bytes).roles(&[]) + C::new("layer.addDataBytes", "Add Data (from file contents)", "Home › Add", add_bytes).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"fileName": "name.tif|.tiff|.asc|.geojson|.csv|.zip|.kml|.kmz|.gpx|.parquet|.fgb|.gpkg|.mbtiles|.jp2|.j2k (JPEG 2000: desktop, through GDAL)", "data": base64, "zoom"?: bool, "layer"?: "one layer of a zip, KML or GPX by name", "name"?: text, "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "stream"?, "as"? (as layer.addData)}"#), - C::new("raster.symbology", "Symbology", "Style › Rendering", symbology).roles(&[]).when(need_raster).params( + C::new("raster.symbology", "Symbology", "Style › Rendering", symbology).when(need_raster).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layer"?, "type": "stretch"|"rgb"|"classify"|"unique"|"colormap"|"shadedRelief", "band"?: 1.., "ramp"?, "invert"?: bool, "classes"?: 1..32, "method"?, "azimuth"?, "altitude"?, "zFactor"?, "grey"?: bool (shadedRelief without its colour ramp)}"#, ), - C::new("raster.stretch", "Stretch Type", "Style › Rendering", stretch).roles(&[]) + C::new("raster.stretch", "Stretch Type", "Style › Rendering", stretch).inputs(stretch_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_raster) .params(r#"{"layer"?, "type": "none"|"minMax"|"custom"|"percentClip"|"standardDeviation"|"histogramEqualize", "low"?: %, "high"?: %, "n"?: deviations, "min"?, "max"?: custom bounds in the data's units, a number for every band or [red, green, blue] on an RGB layer, null for a band's own (a missing one is the range now drawn; the layer's own on one band), "like"?: raster layer (instead of type: that layer's stretch as fixed bounds, so tiles match at their joins)}"#), C::new("raster.dra", "DRA", "Style › Rendering", |s, p| { @@ -47,10 +47,10 @@ pub fn commands() -> Vec { rl.style.dra = on.unwrap_or(!rl.style.dra); Ok(()) }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_raster) .params(r#"{"layer"?, "on"?: bool}"#), - C::new("raster.mask", "Mask Values", "Style › Rendering", mask).roles(&[]).when(need_raster).params( + C::new("raster.mask", "Mask Values", "Style › Rendering", mask).roles(&[]).effects(crate::policy::Effects::NONE).when(need_raster).params( r#"{"layer"?, "values"?: [numbers] or "0, 254, 255" (drawn as no data; [] for none), "min"?, "max"?: hide values outside this range (null for no bound), "inside"?: bool (hide those inside it instead), "clear"?: bool}"#, ), C::new("raster.resampling", "Resampling Type", "Style › Rendering", |s, p| { @@ -60,10 +60,10 @@ pub fn commands() -> Vec { rl.style.resampling = r; Ok(()) }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_raster) .params(r#"{"layer"?, "type": "nearest"|"bilinear"}"#), - C::new("raster.bands", "Band Combination", "Style › Rendering", bands).roles(&[]) + C::new("raster.bands", "Band Combination", "Style › Rendering", bands).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_raster) .params(r#"{"layer"?, "red"?: 1.., "green"?: 1.., "blue"?: 1.., "preset"?: "natural"|"colorInfrared"}"#), C::new("raster.enhance", "Enhancement", "Style › Enhancement", |s, p| { @@ -80,14 +80,14 @@ pub fn commands() -> Vec { } Ok(()) }) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_raster) .params(r#"{"layer"?, "brightness"?: -100..100, "contrast"?: -100..100, "gamma"?: 0.1..10}"#), - C::new("raster.setDefaultStyle", "Save as Default Raster Style", "Style › Rendering", set_default_style).roles(&[]).view().params( + C::new("raster.setDefaultStyle", "Save as Default Raster Style", "Style › Rendering", set_default_style).roles(&[]).effects(crate::policy::Effects::NONE).view().params( r#"{"fromLayer"?: bool (the active or named layer's colour ramp stretch; the settings apply to every raster added later that would be a grey stretch, opened files and analysis results alike), "layer"?, "ramp"?, "invert"?: bool, "stretch"?: "none"|"minMax"|"percentClip"|"standardDeviation"|"histogramEqualize", "low"?, "high"?: %, "n"?: deviations, "resampling"?: "nearest"|"bilinear", "reset"?: bool (back to the black-to-white grey stretch)}"#, ), - C::new("raster.info", "Raster Information", "Data › Information", info).roles(&[]).view().when(need_raster).params(r#"{"layer"?}"#), - C::new("share.exportRaster", "Export Raster", "Data › Information", |s, p| super::exports::write_here(s, p, "share.exportRaster")).roles(&[]).view().when(need_raster).params( + C::new("raster.info", "Raster Information", "Data › Information", info).view().when(need_raster).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?}"#), + C::new("share.exportRaster", "Export Raster", "Data › Information", |s, p| super::exports::write_here(s, p, "share.exportRaster")).view().when(need_raster).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( r#"{"layer"?, "path": "*.tif", "band"?: 1.. (rasters that aren't colour ones; a colour raster writes its colour bands), "cog"?: bool (a cloud-optimised GeoTIFF: 512 px tiles, Deflate, overviews; default false, an untiled GeoTIFF)}"#, ), ] @@ -179,8 +179,7 @@ fn like_stretch(s: &Session, name: &str) -> Result { } fn stretch(s: &mut Session, p: &Value) -> CmdResult { - if let Some(like) = p.get("like").filter(|v| !v.is_null()) { - let like = like.as_str().ok_or("like is a raster layer's name")?; + if let Some(like) = like_name(p)? { let st = like_stretch(s, like)?; return with(s, p, |rl, _| { rl.style.stretch = st; @@ -1004,7 +1003,14 @@ fn write_geotiff(r: &Raster, bi: usize, cog: bool, path: &str) -> Result Option { }) } +/// Binds the primary raster and optional actual like raster without copying pixel data. +fn stretch_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let mut inputs = super::Inputs::single_layer(s, p)?; + if let Some(name) = like_name(p)? { + inputs.extend(super::Inputs::named(s.map(), [name])?); + } + Ok(inputs) +} + +/// Selects only the literal like raster consumed by stretch. +fn like_name(p: &Value) -> Result, String> { + p.get("like").filter(|v| !v.is_null()).map(|v| v.as_str().ok_or_else(|| "like is a raster layer's name".to_string())).transpose() +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/engine/src/cmd/reads.rs b/crates/engine/src/cmd/reads.rs index eaad7f1098..c15e688134 100644 --- a/crates/engine/src/cmd/reads.rs +++ b/crates/engine/src/cmd/reads.rs @@ -1,79 +1,125 @@ //! Reads the desktop app makes on a thread of its own before a command adds what they read //! (#1291 step 3): Add Data's whole-file reads (a raster, a raster GDAL converts, a GeoJSON, -//! delimited text or spreadsheet read whole), Load All Features, and a model's or a script's -//! tools, run on a copy of the session. [`read_ahead`] takes what the read needs of the session, -//! [`ReadAhead::run`] reads on the thread, and the app then runs the command as it would have -//! with the read [`Read::placed`] in the session: the command takes it instead of reading again, -//! so what it adds, says, logs and puts in Undo is the same as when it reads here. A script's own -//! Add Data lines, and the command line, read where the command runs. +//! delimited text or spreadsheet read whole), Load All Features and native models or scripts. [`read_ahead`] +//! takes what the read needs of the session, [`ReadAhead::run`] reads on the thread, and [`read_land`] +//! runs the admitted command under its initiating owner: the command takes +//! it instead of reading again, so what it adds, says, logs and puts in Undo is the same as when +//! it reads here. A script or the command line reads where the command runs. use std::any::Any; use serde_json::Value; -use crate::Session; +use crate::policy::{Authorized, CommandOp, Continuation}; +use crate::{CmdResult, Session}; -/// What a read gave, for the command it was made for: kept in the session ([`Read::placed`]) -/// while that command runs, and taken by the site that would have read it. -pub struct Read { +/// A prepared value accessible only while its original admitted command runs. +pub(crate) struct Ready { key: String, value: Box, } -impl Read { - /// Puts the read in the session for the next command to take, until [`Session::drop_read`]. - pub fn placed(self, s: &mut Session) { - s.read_ahead = Some(self); - } +/// What a read gave, together with the command and authority that alone may consume it. +pub struct Read { + pub(crate) op: Authorized, + pub(crate) owner: Continuation, + pub(crate) source: Option, + pub(crate) ready: Option, +} + +/// A reader chosen only after the command and its original input have been admitted. +pub(crate) struct ReadWork { + name: &'static str, + key: String, + work: Box Box + Send>, } /// A read started for a command, to run on a thread of its own. pub struct ReadAhead { /// What the read is called, for its progress and a cancelled run's error. pub name: &'static str, - key: String, - work: Box Box + Send>, + op: Authorized, + owner: Continuation, + source: Option, + work: Option, } impl ReadAhead { + /// Whether this command has a read to make before it lands; otherwise landing runs it inline. + pub fn reads(&self) -> bool { + self.work.is_some() + } + /// Reads, blocking until it's done; `control` stops it and counts its progress (see /// [`rustgis_analysis::control`]). A read that fails gives its failure to the command, which /// says it as it would have; only a cancelled run (or a bug in a reader) is an error here. pub fn run(self, control: &std::sync::Arc) -> Result { - let ReadAhead { name, key, work } = self; - let mut value = None; + let ReadAhead { name, op, owner, source, work } = self; + owner.check()?; + let current = owner.clone(); + let still = std::sync::Arc::new(move || current.check().is_ok()); + let mut ready = None; // A bug in a reader must not take the app down. let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - rustgis_analysis::run_under(name, control, || { - value = Some(work()); - Ok(()) + crate::render::rustgis_tiles::cache::with_still(still, || { + rustgis_analysis::run_under(name, control, || { + ready = work.map(|ReadWork { key, work, .. }| Ready { key, value: work() }); + Ok(()) + }) }) })) .unwrap_or_else(|_| Err(format!("{name} failed unexpectedly"))); res?; - let value = value.ok_or_else(|| format!("{name} read nothing"))?; - Ok(Read { key, value }) + Ok(Read { op, owner, source, ready }) } } /// The read `command` makes with `p`, when the app makes it on a thread of its own before the -/// command runs: `None` for one it runs as it is (a layer read in place, a web service, a file -/// the command reads differently). -pub fn read_ahead(s: &Session, command: &str, p: &Value) -> Option { - match command { - "layer.addData" => super::layer::add_data_ahead(p), - "layer.loadAll" => super::layer::load_all_ahead(s, p), - #[cfg(not(target_arch = "wasm32"))] - "model.run" => Some(super::workflows::ahead_on_copy(s, "model.run", p)), - #[cfg(not(target_arch = "wasm32"))] - "script.run" => super::workflows::script_ahead(s, p), - _ => None, - } +/// command runs. Admission precedes all preparation; even a command without an off-thread read +/// keeps its original operation for synchronous landing. +pub fn read_ahead(s: &mut Session, command: &str, p: &Value) -> Result { + s.start_read(command, p) +} + +/// Picks work only for an already admitted command, retaining its exact streamed-source opening. +pub(crate) fn prepare(s: &Session, op: Authorized, owner: Continuation) -> Result { + let CommandOp::Registered { spec, params } = op.op() else { return Err("not a registered read command".into()) }; + let (name, work, source) = match spec.id { + "layer.addData" => ("Add Data", super::layer::add_data_ahead(params), None), + "layer.loadAll" => { + let layer = s.map().layer(s.layer_id(params)?).ok_or("no such layer")?; + ("Load All Features", super::layer::load_all_ahead(s, params), layer.source.clone()) + } + "model.run" => { + // A copied session has native authority. Scoped callers keep their admitted + // command and execute on the original session instead of widening into that copy. + #[cfg(not(target_arch = "wasm32"))] + let work = owner.native_tool_owner().then(|| super::workflows::ahead_on_copy(s, "model.run", params)); + #[cfg(target_arch = "wasm32")] + let work = None; + ("Run Model", work, None) + } + "script.run" => { + #[cfg(not(target_arch = "wasm32"))] + let work = if owner.native_tool_owner() { super::workflows::script_ahead(s, params) } else { None }; + #[cfg(target_arch = "wasm32")] + let work = None; + ("Run Script", work, None) + } + _ => return Err("only Add Data, Load All, models and scripts can prepare background work".into()), + }; + let name = work.as_ref().map_or(name, |work| work.name); + Ok(ReadAhead { name, op, owner, source, work }) +} + +/// Consumes a completed read under its initiating owner, with ordinary command history and undo. +pub fn read_land(s: &mut Session, read: Read) -> CmdResult { + s.finish_read(read) } /// `work` as a read for the command to take under `key`. -pub(crate) fn ahead(name: &'static str, key: String, work: impl FnOnce() -> T + Send + 'static) -> ReadAhead { - ReadAhead { name, key, work: Box::new(move || Box::new(work()) as Box) } +pub(crate) fn ahead(name: &'static str, key: String, work: impl FnOnce() -> T + Send + 'static) -> ReadWork { + ReadWork { name, key, work: Box::new(move || Box::new(work()) as Box) } } /// The read placed for `key`, taken: `None` when none was, so the caller reads here. diff --git a/crates/engine/src/cmd/repair.rs b/crates/engine/src/cmd/repair.rs index 3905b2cbe6..4faa6d589d 100644 --- a/crates/engine/src/cmd/repair.rs +++ b/crates/engine/src/cmd/repair.rs @@ -7,9 +7,9 @@ use serde_json::json; pub fn commands() -> Vec { vec![ - C::new("layer.repairSource", "Repair Data Source", "Data › Data Source", repair_source).roles(&[]).params( + C::new("layer.repairSource", "Repair Data Source", "Data › Data Source", repair_source).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( r#"{"layer"?: name|id (default: the active layer), "path": "the file or folder (a Zarr store, a File Geodatabase) the layer's data moved to", "table"?: "the dataset in a GeoPackage, GDAL or DuckDB file, or a raster's subdataset (default: the one the layer reads now)"}"#), - C::new("map.repairTiles", "Repair Tile Archive", "Layers", repair_tiles).roles(&[]).params( + C::new("map.repairTiles", "Repair Tile Archive", "Layers", repair_tiles).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( r#"{"name"?: text | "index"?: number (0 = top) | "id"?: catalog id, "path": "the .pmtiles or .mbtiles file the archive moved to"}"#), ] } diff --git a/crates/engine/src/cmd/report.rs b/crates/engine/src/cmd/report.rs index 836ec8258f..2d5c36d9ca 100644 --- a/crates/engine/src/cmd/report.rs +++ b/crates/engine/src/cmd/report.rs @@ -34,17 +34,17 @@ fn need_report(s: &Session) -> Option<&'static str> { pub fn commands() -> Vec { vec![ - C::new("report.new", "New Report", "Publish › Report", new_report).roles(&[]).params( + C::new("report.new", "New Report", "Publish › Report", new_report).inputs(new_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layer"?: name (default: the selected layer), "name"?, "title"?, "fields"?: [names] (default: all), "where"?: SQL, "selectedOnly"?: bool, "groups"?: [fields], "sort"?: field, "descending"?: bool, "page"?: "letter"|"a4"|…, "orientation"?: "portrait"|"landscape"}"#, ), - C::new("report.update", "Report Properties", "Report", update).roles(&[]).when(need_report).params( + C::new("report.update", "Report Properties", "Report", update).inputs(update_inputs).roles(&[]).effects(crate::policy::Effects::NONE).when(need_report).params( r#"{"report"?: name|index, "name"?, "title"?, "layer"?, "where"?, "selectedOnly"?, "sort"?: field|null, "descending"?, "page"?, "orientation"?, "width"?, "height"?: mm}"#, ), C::new("report.activate", "Open Report", "Browser", |s, p| { let i = index(s, p)?; s.active_report = Some(i); Ok(json!({"report": i})) - }).roles(&[]) + }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .view() .when(need_report) .params(r#"{"report": name|index}"#), @@ -57,7 +57,7 @@ pub fn commands() -> Vec { } s.active_report = None; Ok(Value::Null) - }).roles(&[]) + }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?}"#), // Report sections. @@ -65,7 +65,7 @@ pub fn commands() -> Vec { let title: String = ps(p, "title").unwrap_or("About this report").chars().take(500).collect(); let text: String = ps(p, "text").unwrap_or("").chars().take(100_000).collect(); add_section(s, p, Section::Page { title, text, end: b(p, "end").unwrap_or(false) }) - }).roles(&[]) + }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "title"?, "text"?, "end"?: bool (after the records; default before)}"#), C::new("report.newTemplate", "New Template", "Publish › Report", |s, p| { @@ -79,13 +79,13 @@ pub fn commands() -> Vec { s.active_layout = before; let layout = r["name"].as_str().unwrap_or_default().to_string(); add_section(s, &json!({"report": i}), Section::Layout { layout, end: b(p, "end").unwrap_or(false) }) - }).roles(&[]) + }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "name"?, "page"?, "orientation"?, "template"?: "map"|"blank", "end"?: bool}"#), C::new("report.existingTemplate", "Existing Template", "Publish › Report", |s, p| { let i = index(s, p)?; let layout = if let Some(n) = ps(p, "layout") { - s.project.layouts.iter().find(|l| l.name.eq_ignore_ascii_case(n)).map(|l| l.name.clone()).ok_or_else(|| format!("no layout '{n}'"))? + s.project.layouts.get(template_at(s,n)?).ok_or("no layout")?.name.clone() } else { let text = super::items::read_text(p).map_err(|e| format!("{e} (pass a layout name, or a layout file's path or text)"))?; let before = s.active_layout; @@ -94,22 +94,22 @@ pub fn commands() -> Vec { r["name"].as_str().unwrap_or_default().to_string() }; add_section(s, &json!({"report": i}), Section::Layout { layout, end: b(p, "end").unwrap_or(false) }) - }).roles(&[]) + }).inputs(template_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .when(need_report) .params(r#"{"report"?, "layout"?: a layout's name, "path"?: "*.rgislayout", "text"?: layout JSON, "data"?: base64 file, "end"?: bool}"#), C::new("report.relatedReport", "Related Report", "Publish › Report", |s, p| { let i = index(s, p)?; let other = ps(p, "related").ok_or("related is required (another report's name)")?; - let j = s.project.reports.iter().position(|r| r.name.eq_ignore_ascii_case(other)).ok_or_else(|| format!("no report '{other}'"))?; + let j = related_report_at(s,other)?; if i == j { return Err("a report can't follow itself".into()); } let report = s.project.reports.get(j).map(|r| r.name.clone()).unwrap_or_default(); add_section(s, &json!({"report": i}), Section::Related { report }) - }).roles(&[]) + }).inputs(related_report_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "related": report name}"#), - C::new("report.subreport", "Subreport", "Publish › Report", subreport).roles(&[]) + C::new("report.subreport", "Subreport", "Publish › Report", subreport).inputs(subreport_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "relationship": name (Data Design › Relationships), "fields"?: [related fields]}"#), C::new("report.removeSection", "Remove Section", "Report", |s, p| { @@ -121,7 +121,7 @@ pub fn commands() -> Vec { } r.sections.remove(k); Ok(json!({"sections": r.sections.len()})) - }).roles(&[]) + }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "index": section number}"#), // Data. @@ -130,7 +130,7 @@ pub fn commands() -> Vec { let r = s.project.reports.get_mut(i).ok_or("no report")?; toggle(&mut r.fields, field, b(p, "remove").unwrap_or(false)); Ok(json!({"fields": r.fields})) - }).roles(&[]) + }).inputs(layer_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "field": name, "remove"?: bool}"#), C::new("report.autofill", "Autofill Fields", "Data › Data Design", |s, p| { @@ -139,7 +139,7 @@ pub fn commands() -> Vec { let r = s.project.reports.get_mut(i).ok_or("no report")?; r.fields = names; Ok(json!({"fields": r.fields})) - }).roles(&[]) + }).inputs(layer_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?}"#), C::new("report.statistics", "Statistics", "Data › Data Design", |s, p| { @@ -157,7 +157,7 @@ pub fn commands() -> Vec { _ => {} } Ok(json!({"stats": r.stats.len()})) - }).roles(&[]) + }).inputs(layer_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "field": name, "kind"?: "count"|"sum"|"mean"|"min"|"max", "remove"?: bool}"#), C::new("report.group", "Group", "Data › Data Design", |s, p| { @@ -169,7 +169,7 @@ pub fn commands() -> Vec { } toggle(&mut r.groups, field, remove); Ok(json!({"groups": r.groups})) - }).roles(&[]) + }).inputs(layer_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "field": name, "remove"?: bool}"#), C::new("report.attachments", "Attachments", "Data › Data Design", |s, p| { @@ -177,7 +177,7 @@ pub fn commands() -> Vec { let r = s.project.reports.get_mut(i).ok_or("no report")?; r.attachments_field = field; Ok(json!({"field": r.attachments_field})) - }).roles(&[]) + }).inputs(optional_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "field": name of a field listing each record's attachments (paths separated by ';')|null}"#), C::new("report.dynamicPicture", "Dynamic Picture", "Data › Data Design", |s, p| { @@ -185,7 +185,7 @@ pub fn commands() -> Vec { let r = s.project.reports.get_mut(i).ok_or("no report")?; r.picture_field = field; Ok(json!({"field": r.picture_field})) - }).roles(&[]) + }).inputs(optional_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "field": name of a field holding a picture path or base64 PNG/JPEG|null}"#), C::new("report.chart", "Chart Frame", "Data › Data Design", |s, p| { @@ -204,7 +204,7 @@ pub fn commands() -> Vec { let r = s.project.reports.get_mut(i).ok_or("no report")?; r.chart = chart; Ok(json!({"chart": r.chart.is_some()})) - }).roles(&[]) + }).inputs(chart_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "category": field|null (no chart), "value"?: field to sum (default: count records)}"#), // Output. @@ -220,26 +220,28 @@ pub fn commands() -> Vec { let (bytes, pages) = report_bytes(s, i, dpi, pdf)?; rustgis_data::io::write_atomic(Path::new(&path), &bytes).map_err(|e| format!("{path}: {e}"))?; Ok(json!({"path": path, "pages": pages, "bytes": bytes.len()})) - }).roles(&[]) + }).inputs(export_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_report) .params(r#"{"report"?, "path": "*.pdf|*.png" (PNG: the first page), "dpi"?: 72..600}"#), - C::new("report.save", "Report File", "Publish › Save As", |s, p| save(s, p, false)).roles(&[]) + C::new("report.save", "Report File", "Publish › Save As", |s, p| save(s, p, false)).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_report) .params(r#"{"report"?, "path": "*.rgisreport"}"#), - C::new("report.saveTemplate", "Report Template", "Publish › Save As", |s, p| save(s, p, true)).roles(&[]) + C::new("report.saveTemplate", "Report Template", "Publish › Save As", |s, p| save(s, p, true)).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_report) .params(r#"{"report"?, "path": "*.rgisrpttemplate"}"#), - C::new("report.open", "Import Report", "Publish › Report", open).roles(&[]).params( + C::new("report.open", "Import Report", "Publish › Report", open).inputs(open_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( r#"{"path"?: "*.rgisreport|*.rgisrpttemplate", "text"?: report JSON, "data"?: base64 file, "layer"?: the layer a template reports on (default: the selected layer)}"#, ), ] } /// The report named or numbered by `report`, else the active one, else the last one. -pub fn index(s: &Session, p: &Value) -> Result { +fn index_input(input: &impl super::InputSource, p: &Value) -> Result { + let context = input.input_context(); + let s = &context; let rs = &s.project.reports; match p.get("report") { Some(Value::Number(n)) => { @@ -259,31 +261,33 @@ pub fn index(s: &Session, p: &Value) -> Result { } /// A layer by name: the active map's, else any map's. -fn find_layer<'a>(s: &'a Session, name: &str) -> Option<&'a Layer> { - s.map().layer_by_name(name).or_else(|| s.project.maps.iter().find_map(|m| m.layer_by_name(name))) +fn find_layer<'a>(input: &'a impl super::InputSource, name: &str) -> Option<&'a Layer> { + find_layer_map(input, name).map(|(_, layer)| layer) } /// The layer report `i` lists (the active map's first, else any map's). -pub fn layer_of(s: &Session, i: usize) -> Result<&Layer, String> { +fn layer_of_input(input: &impl super::InputSource, i: usize) -> Result<&Layer, String> { + let context = input.input_context(); + let s = &context; let r = s.project.reports.get(i).ok_or("no report")?; - find_layer(s, &r.layer).ok_or_else(|| format!("the report's layer '{}' isn't in the project", r.layer)) + find_layer(input, &r.layer).ok_or_else(|| format!("the report's layer '{}' isn't in the project", r.layer)) } /// The report and a field of its layer named by `key` (spelled as the layer spells it). -fn field_of(s: &Session, p: &Value, key: &str) -> Result<(usize, String), String> { - let i = index(s, p)?; +fn field_of(input: &impl super::InputSource, p: &Value, key: &str) -> Result<(usize, String), String> { + let i = index_input(input, p)?; let name = ps(p, key).ok_or_else(|| format!("{key} is required"))?; - let l = layer_of(s, i)?; + let l = layer_of_input(input, i)?; let fi = l.dataset.field_index(name).ok_or_else(|| format!("'{}' has no field '{name}'", l.name))?; let field = l.dataset.fields.get(fi).map(|f| f.name.clone()).ok_or("no such field")?; Ok((i, field)) } /// `field` as a field of the report's layer, or `None` for null/absent. -fn optional_field(s: &Session, p: &Value) -> Result<(usize, Option), String> { +fn optional_field(input: &impl super::InputSource, p: &Value) -> Result<(usize, Option), String> { match p.get("field") { - None | Some(Value::Null) => Ok((index(s, p)?, None)), - Some(_) => field_of(s, p, "field").map(|(i, f)| (i, Some(f))), + None | Some(Value::Null) => Ok((index_input(input, p)?, None)), + Some(_) => field_of(input, p, "field").map(|(i, f)| (i, Some(f))), } } @@ -361,10 +365,7 @@ fn page(p: &Value, current: Option<(f64, f64)>) -> Result, St } fn new_report(s: &mut Session, p: &Value) -> CmdResult { - let layer = match ps(p, "layer") { - Some(n) => find_layer(s, n).ok_or_else(|| format!("no layer named '{n}'"))?, - None => s.active_layer.and_then(|id| s.map().layer(id)).ok_or("select a layer, or pass layer")?, - }; + let layer = new_layer(s, p)?; if layer.is_raster() { return Err("reports list a feature layer's records, not a raster's".into()); } @@ -432,17 +433,7 @@ fn update(s: &mut Session, p: &Value) -> CmdResult { let name = l.name.clone(); r.sections.retain_mut(|x| match x { Section::Subreport { relationship, fields } => { - let other = relationships(s).find_map(|rel| { - if !rel.name.eq_ignore_ascii_case(relationship) { - None - } else if rel.origin.eq_ignore_ascii_case(&name) { - Some(rel.destination.clone()) - } else if rel.destination.eq_ignore_ascii_case(&name) { - Some(rel.origin.clone()) - } else { - None - } - }); + let other = update_other(s, relationship, &name); let Some(other) = other else { return false }; match find_layer(s, &other) { Some(o) => fields.retain(|f| o.dataset.field_index(f).is_some()), @@ -510,13 +501,7 @@ fn subreport(s: &mut Session, p: &Value) -> CmdResult { let rel = ps(p, "relationship").ok_or("relationship is required")?; let layer = s.project.reports.get(i).map(|r| r.layer.clone()).unwrap_or_default(); let r = relationships(s).find(|r| r.name.eq_ignore_ascii_case(rel)).ok_or_else(|| format!("no relationship '{rel}'"))?; - let other = if r.origin.eq_ignore_ascii_case(&layer) { - r.destination.clone() - } else if r.destination.eq_ignore_ascii_case(&layer) { - r.origin.clone() - } else { - return Err(format!("the relationship '{}' doesn't involve '{layer}'", r.name)); - }; + let other = opposite(r, &layer).ok_or_else(|| format!("the relationship '{}' doesn't involve '{layer}'", r.name))?.to_string(); let name = r.name.clone(); let ds = &find_layer(s, &other).ok_or_else(|| format!("no layer named '{other}'"))?.dataset; let mut fields = Vec::new(); @@ -529,7 +514,9 @@ fn subreport(s: &mut Session, p: &Value) -> CmdResult { } /// Every map's relationships. -fn relationships(s: &Session) -> impl Iterator { +fn relationships(input: &impl super::InputSource) -> impl Iterator { + let context = input.input_context(); + let s = &context; s.project.maps.iter().flat_map(|m| m.editing.relationships.iter()) } @@ -552,16 +539,7 @@ fn open(s: &mut Session, p: &Value) -> CmdResult { let text = super::items::read_text(p)?; let mut r: Report = serde_json::from_str(&text).map_err(|e| format!("not a RustGIS report: {e}"))?; r.clamp(); - if let Some(n) = ps(p, "layer") { - r.layer = find_layer(s, n).ok_or_else(|| format!("no layer named '{n}'"))?.name.clone(); - } else if find_layer(s, &r.layer).is_none() { - // A template (or a report on a layer this project lacks) reports on the selected layer. - r.layer = s - .active_layer - .and_then(|id| s.map().layer(id)) - .map(|l| l.name.clone()) - .ok_or("select the layer to report on, or pass layer")?; - } + import_layer(s, p, &mut r)?; // Drop what the layer lacks, so the report still draws. if let Some(l) = find_layer(s, &r.layer) { fit_to_layer(&mut r, &l.dataset); @@ -735,13 +713,7 @@ fn blocks(s: &Session, i: usize, out: &mut Vec, seen: &mut Vec, de let Section::Subreport { relationship, fields } = sec else { return None }; let rel = relationships(s).find(|x| x.name.eq_ignore_ascii_case(relationship))?; // A relationship that doesn't involve this layer is skipped. - let (here_key, other, other_key) = if rel.origin.eq_ignore_ascii_case(&layer.name) { - (&rel.origin_key, &rel.destination, &rel.destination_key) - } else if rel.destination.eq_ignore_ascii_case(&layer.name) { - (&rel.destination_key, &rel.origin, &rel.origin_key) - } else { - return None; - }; + let (here_key, other, other_key) = relationship_columns(rel, &layer.name)?; let ods = &find_layer(s, other)?.dataset; let ok = ods.field_index(other_key)?; let mut by_key: HashMap> = HashMap::new(); @@ -939,3 +911,237 @@ pub fn report_bytes(s: &mut Session, i: usize, dpi: f64, pdf: bool) -> Result<(V super::layout::pictured_notices(s, &frames_pictured, &pages_pictured); Ok((rustgis_render::pdf::write(&out, &doc.title), pages.len())) } + +/// Resolves the report creator's string-name or active/default layer convention. +fn new_layer<'a>(input: &'a impl super::InputSource, p: &Value) -> Result<&'a Layer, String> { + let context = input.input_context(); + let s = &context; + Ok(match ps(p, "layer") { + Some(n) => find_layer(input, n).ok_or_else(|| format!("no layer named '{n}'"))?, + None => s.active_layer.and_then(|id| s.map().layer(id)).ok_or("select a layer, or pass layer")?, + }) +} + +/// Fits an imported report to its explicit or stored existing layer without opening data. +fn import_layer(input: &impl super::InputSource, p: &Value, r: &mut Report) -> Result<(), String> { + let context = input.input_context(); + let s = &context; + if let Some(n) = ps(p, "layer") { + r.layer = find_layer(input, n).ok_or_else(|| format!("no layer named '{n}'"))?.name.clone(); + } else if find_layer(input, &r.layer).is_none() { + // A template (or a report on a layer this project lacks) reports on the selected layer. + r.layer = s + .active_layer + .and_then(|id| s.map().layer(id)) + .map(|l| l.name.clone()) + .ok_or("select the layer to report on, or pass layer")?; + } + Ok(()) +} + +/// Finds a literal feature layer active-map first, then in project order. +fn find_layer_map<'a>(input: &'a impl super::InputSource, name: &str) -> Option<(&'a rustgis_map::Map, &'a Layer)> { + let context = input.input_context(); + let s = &context; + let in_map = |map: &'a rustgis_map::Map| map.layer_by_name(name).map(|l| (map, l)); + in_map(s.map()).or_else(|| s.project.maps.iter().find_map(in_map)) +} +/// Retains the owning map of the already resolved layer, including repeated ids in other maps. +fn owning_inputs(input: &impl super::InputSource, layer: &Layer) -> super::Inputs { + let context = input.input_context(); + let s = &context; + super::Inputs::layers( + s.project.maps.iter().flat_map(|map| map.layers.iter().filter(|l| std::ptr::eq(*l, layer)).map(move |l| (map, l))), + ) +} +/// Binds the creator's actual layer without constructing report rows. +fn new_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + Ok(owning_inputs(input, new_layer(input, p)?)) +} +/// Binds the selected report's stored layer through the report executor's lookup. +fn layer_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let mut inputs = owning_inputs(input, layer_of_input(input, index_input(input, p)?)?); + inputs.extend(object_inputs(input, p)?); + Ok(inputs) +} +/// Binds a field-dependent layer only when an optional field is actually supplied. +fn optional_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let (i, field) = optional_field(input, p)?; + let mut inputs = + if field.is_some() { owning_inputs(input, layer_of_input(input, i)?) } else { super::Inputs::layers(std::iter::empty()) }; + inputs.extend(object_inputs(input, p)?); + Ok(inputs) +} +/// Clearing a chart consumes no field; a new chart uses the existing field resolver. +fn chart_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + if p.get("category").is_none_or(Value::is_null) { + return object_inputs(input, p); + } + let (i, _) = field_of(input, p, "category")?; + { + let mut inputs = owning_inputs(input, layer_of_input(input, i)?); + inputs.extend(object_inputs(input, p)?); + Ok(inputs) + } +} +/// Returns the matching relationship direction and both keys for a report's current layer. +fn relationship_columns<'a>(rel: &'a rustgis_map::design::Relationship, here: &str) -> Option<(&'a str, &'a str, &'a str)> { + if rel.origin.eq_ignore_ascii_case(here) { + Some((&rel.origin_key, &rel.destination, &rel.destination_key)) + } else if rel.destination.eq_ignore_ascii_case(here) { + Some((&rel.destination_key, &rel.origin, &rel.origin_key)) + } else { + None + } +} +/// Selects the other relationship endpoint using the same direction test as row construction. +fn opposite<'a>(rel: &'a rustgis_map::design::Relationship, here: &str) -> Option<&'a str> { + relationship_columns(rel, here).map(|(_, other, _)| other) +} + +/// Selects the first named relationship and its active-first other endpoint. +fn subreport_input<'a>(input: &'a impl super::InputSource, here: &str, name: &str) -> Option<(&'a rustgis_map::Map, &'a Layer)> { + let rel = relationships(input).find(|rel| rel.name.eq_ignore_ascii_case(name))?; + find_layer_map(input, opposite(rel, here)?) +} +/// Collects the layer used to fit a new subreport's fields, without joining rows. +fn subreport_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let report = s.project.reports.get(index_input(input, p)?).ok_or("no report")?; + let name = ps(p, "relationship").ok_or("relationship is required")?; + let mut inputs = super::Inputs::layers(subreport_input(input, &report.layer, name)); + inputs.extend(object_inputs(input, p)?); + inputs.state["relationship"] = json!(relationships(input).find(|r| r.name.eq_ignore_ascii_case(name))); + Ok(inputs) +} +/// A report layer change retains the first named relationship that involves the new layer. +fn update_other(input: &impl super::InputSource, relationship: &str, layer: &str) -> Option { + relationships(input).filter(|rel| rel.name.eq_ignore_ascii_case(relationship)).find_map(|rel| opposite(rel, layer)).map(str::to_string) +} +/// Binds only layer/field and retained-subreport dependencies consumed by this update. +fn update_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let report = s.project.reports.get(index_input(input, p)?).ok_or("no report")?; + let mut inputs = object_inputs(input, p)?; + if let Some(name) = ps(p, "layer") { + let (map, layer) = find_layer_map(input, name).ok_or_else(|| format!("no layer named '{name}'"))?; + inputs.layers.push((map.instance_id(), layer.id)); + for sec in &report.sections { + if let Section::Subreport { relationship, .. } = sec + && let Some(other) = update_other(input, relationship, &layer.name) + && let Some((map, layer)) = find_layer_map(input, &other) + { + inputs.layers.push((map.instance_id(), layer.id)); + } + } + } else if ps(p, "where").is_some_and(|w| !w.trim().is_empty()) || ps(p, "sort").is_some() { + inputs.extend(layer_inputs(input, p)?); + } + Ok(inputs) +} +/// Uses inline data/text precedence for pure import planning; path-held selectors remain unresolved. +fn open_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + if let Some(name) = ps(p, "layer") { + return Ok(super::Inputs::layers([find_layer_map(input, name).ok_or_else(|| format!("no layer named '{name}'"))?])); + } + let Some(text) = super::items::inline_text(p)? else { + return Ok(super::Inputs::unresolved()); + }; + let mut report: Report = serde_json::from_str(&text).map_err(|e| format!("not a RustGIS report: {e}"))?; + report.clamp(); + import_layer(input, p, &mut report)?; + Ok(super::Inputs::layers(find_layer_map(input, &report.layer))) +} +/// Walks bounded report references before any row building, picture reading or output write. +fn export_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let root = index_input(input, p)?; + let mut inputs = layer_inputs(input, p)?; + let mut seen = vec![root]; + report_dependencies(input, root, &mut inputs, &mut seen, 0); + Ok(inputs) +} +/// The same bounded related-report graph as blocks, without building rows or reading pictures. +fn report_dependencies(input: &impl super::InputSource, i: usize, inputs: &mut super::Inputs, seen: &mut Vec, depth: usize) { + let context = input.input_context(); + let s = &context; + let Some(report) = s.project.reports.get(i) else { return }; + if let Some(found) = find_layer_map(input, &report.layer) { + inputs.layers.push((found.0.instance_id(), found.1.id)); + } else { + return; + } + let mut own = super::Inputs::layers(std::iter::empty()); + own.state = json!({"report":i,"name":report.name,"layer":report.layer,"picture":report.picture_field,"attachments":report.attachments_field, + "refs":report.sections.iter().filter_map(|section|match section {Section::Layout{layout,..}=>Some(json!({"layout":layout})),Section::Related{report}=>Some(json!({"report":report})),Section::Subreport{relationship,fields}=>Some(json!({"relationship":relationship,"fields":fields})),_=>None}).collect::>()}); + inputs.extend(own); + for sec in &report.sections { + match sec { + Section::Subreport { relationship, .. } => { + if let Some((map, layer)) = subreport_input(input, &report.layer, relationship) { + inputs.layers.push((map.instance_id(), layer.id)); + } + } + Section::Layout { layout, .. } => { + if let Some(li) = s.project.layouts.iter().position(|l| l.name.eq_ignore_ascii_case(layout)) { + inputs.extend(super::layout::render_inputs_at(input, li)); + } + } + Section::Related { report } if depth < MAX_DEPTH => { + if let Some(j) = s.project.reports.iter().position(|r| r.name.eq_ignore_ascii_case(report)) + && !seen.contains(&j) + { + seen.push(j); + report_dependencies(input, j, inputs, seen, depth + 1); + } + } + _ => {} + } + } +} + +/// Reference-only edits bind the actual report without requiring its stored layer to still exist. +fn object_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let i = index_input(input, p)?; + let mut inputs = super::Inputs::layers(std::iter::empty()); + inputs.state = json!({"report":i,"name":s.project.reports.get(i).map(|r|&r.name)}); + Ok(inputs) +} +/// A related report is the first literal name match used by section insertion. +fn related_report_at(input: &impl super::InputSource, name: &str) -> Result { + let context = input.input_context(); + let s = &context; + s.project.reports.iter().position(|r| r.name.eq_ignore_ascii_case(name)).ok_or_else(|| format!("no report '{name}'")) +} +/// Binds both existing report objects, without building either one's rows. +fn related_report_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let mut inputs = object_inputs(input, p)?; + let i = related_report_at(input, ps(p, "related").ok_or("related is required (another report's name)")?)?; + inputs.state["related"] = json!((i, &s.project.reports.get(i).map(|r| &r.name))); + Ok(inputs) +} +/// An existing layout is a stored reference here; an imported document stays frozen as raw params. +fn template_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let mut inputs = object_inputs(input, p)?; + if let Some(name) = ps(p, "layout") { + inputs.state["layout"] = json!(template_at(input, name)?); + } + Ok(inputs) +} +/// Shares the exact first-name layout lookup with the reference-only template insertion. +fn template_at(input: &impl super::InputSource, name: &str) -> Result { + let context = input.input_context(); + let s = &context; + s.project.layouts.iter().position(|l| l.name.eq_ignore_ascii_case(name)).ok_or_else(|| format!("no layout '{name}'")) +} + +/// The presentation/report selected by the live session, using the same pure resolver as approval. +pub fn index(s: &Session, p: &Value) -> Result { + index_input(s, p) +} + +/// The live report layer, using the same active-first resolver as approval. +pub fn layer_of(s: &Session, i: usize) -> Result<&Layer, String> { + layer_of_input(s, i) +} diff --git a/crates/engine/src/cmd/s3.rs b/crates/engine/src/cmd/s3.rs index 5886df63df..eb6c252682 100644 --- a/crates/engine/src/cmd/s3.rs +++ b/crates/engine/src/cmd/s3.rs @@ -9,16 +9,16 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("s3.connect", "Connect to S3", "Home › Add › S3", connect).roles(&[]).params( + C::new("s3.connect", "Connect to S3", "Home › Add › S3", connect).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).params( r#"{"name"?: text (default "default"), "endpoint"?: "https://… of an S3-compatible service (MinIO, Cloudflare R2, Wasabi…); AWS when absent", "region"?: text (default us-east-1), "buckets"?: [names] (the buckets it serves; others are read anonymously unless browsed through it with s3.list), "accessKeyId"?: text, "secretAccessKey"?: text, "sessionToken"?: text, "profile"?: "an AWS profile in ~/.aws/credentials and config (desktop)", "anonymous"?: bool (no credentials), "environment"?: bool (AWS_ACCESS_KEY_ID… from the environment, desktop)} — without keys, a profile or environment, the connection is anonymous; credentials are kept for the session only"#, ) .view(), - C::new("s3.disconnect", "Disconnect From S3", "Home › Add › S3", disconnect).roles(&[]).view().params(r#"{"name": text}"#), + C::new("s3.disconnect", "Disconnect From S3", "Home › Add › S3", disconnect).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params(r#"{"name": text}"#), C::new("s3.connections", "S3 Connections", "Home › Add › S3", |_s, _p| { Ok(json!({"connections": s3::connections().iter().map(Connection::public).collect::>()})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("s3.list", "List S3 Bucket", "Home › Add › S3", list).roles(&[]).view().params( + C::new("s3.list", "List S3 Bucket", "Home › Add › S3", list).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).view().params( r#"{"url"?: "s3://bucket/prefix/", "bucket"?: text, "prefix"?: text, "connection"?: "a connection's name: its buckets when no bucket is given; a bucket listed through it is served by it from then on", "limit"?: number (default 1000, at most 10000)}"#, ), ] diff --git a/crates/engine/src/cmd/search.rs b/crates/engine/src/cmd/search.rs index d0dee851f3..989edd41ef 100644 --- a/crates/engine/src/cmd/search.rs +++ b/crates/engine/src/cmd/search.rs @@ -15,7 +15,7 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ // Not a view command: going to a feature selects it. - C::new("map.search", "Search", "Map", search_cmd).roles(&[]).params( + C::new("map.search", "Search", "Map", search_cmd).inputs(search_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"query": "a latitude and longitude (40.7, -74.0; 40°42'N 74°0'W), a layer, a bookmark or a value in a layer's table", "limit"?: 1–50, "go"?: true (the first hit) | index}"#, ), ] @@ -134,6 +134,11 @@ pub struct Results { /// Searches the active map for `query`: a coordinate first, then layers, bookmarks and features. pub fn search(s: &Session, query: &str, limit: usize) -> Results { + search_map(s.map(), query, limit) +} + +/// Builds bounded hits without selecting one or opening tile metadata. +fn search_map(map: &rustgis_map::Map, query: &str, limit: usize) -> Results { let q: String = query.trim().chars().take(MAX_CHARS).collect(); let mut out = Results::default(); if q.is_empty() { @@ -143,7 +148,6 @@ pub fn search(s: &Session, query: &str, limit: usize) -> Results { out.hits.push(Hit::Coordinate { lon: c.x, lat: c.y }); } let needle = q.to_lowercase(); - let map = s.map(); for l in &map.layers { if contains_ci(&l.name, &needle) { out.hits.push(Hit::Layer { id: l.id, name: l.name.clone() }); @@ -317,10 +321,11 @@ fn around(c: Coord) -> Extent { Extent::new(c.x - 0.05, c.y - 0.04, c.x + 0.05, c.y + 0.04) } -fn search_cmd(s: &mut Session, p: &Value) -> CmdResult { +/// Resolves the command's actual bounded hit list and optional destination without going to it. +fn search_request(map: &rustgis_map::Map, p: &Value) -> Result<(Results, Option), String> { let query = ps(p, "query").map(str::trim).filter(|q| !q.is_empty()).ok_or("query is required")?; let limit = f(p, "limit").filter(|l| l.is_finite()).map(|l| l.clamp(1.0, 50.0) as usize).unwrap_or(LIMIT); - let r = search(s, query, limit); + let r = search_map(map, query, limit); let pick = match p.get("go") { None | Some(Value::Null | Value::Bool(false)) => None, Some(Value::Bool(true)) => Some(0), @@ -332,8 +337,33 @@ fn search_cmd(s: &mut Session, p: &Value) -> CmdResult { ), }; if let Some(i) = pick { - let hit = - r.hits.get(i).ok_or_else(|| if r.hits.is_empty() { format!("nothing matches '{query}'") } else { format!("no hit {i}") })?; + r.hits.get(i).ok_or_else(|| if r.hits.is_empty() { format!("nothing matches '{query}'") } else { format!("no hit {i}") })?; + } + Ok((r, pick)) +} + +/// Binds a chosen tile's raw route; other hits retain the ordinary layer context. +fn search_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let (hits, pick) = search_request(input.map(), p)?; + match pick.and_then(|i| hits.hits.get(i)) { + Some(Hit::Tile { index, .. }) => Ok(super::tiles::tile_inputs(input.map(), [*index])), + _ => super::Inputs::single_layer(input, p), + } +} + +/// Only going to a tile hit can ask its coverage reader to open the selected source. +pub(crate) fn metadata_reach(input: &super::InputContext<'_>, p: &Value) -> crate::policy::Effects { + let tile = search_request(input.map(), p).ok().and_then(|(hits, pick)| match pick.and_then(|i| hits.hits.get(i)) { + Some(Hit::Tile { index, .. }) => input.map().tiles.get(*index), + _ => None, + }); + tile.map_or(crate::policy::Effects::NONE, super::tiles::coverage_reach) +} + +/// Uses the same bounded hit choice as approval before changing the view or selection. +fn search_cmd(s: &mut Session, p: &Value) -> CmdResult { + let (r, pick) = search_request(s.map(), p)?; + if let Some(hit) = pick.and_then(|i| r.hits.get(i)) { go_to(s, hit)?; } Ok(json!({"hits": r.hits.iter().map(Hit::to_json).collect::>(), "partial": r.partial})) diff --git a/crates/engine/src/cmd/select.rs b/crates/engine/src/cmd/select.rs index d1c5eca21e..2f790bb30b 100644 --- a/crates/engine/src/cmd/select.rs +++ b/crates/engine/src/cmd/select.rs @@ -16,16 +16,16 @@ mod info; pub fn commands() -> Vec { let mut commands = vec![ - C::new("select.byAttributes", "Select By Attributes", "Home › Selection", by_attributes).roles(&[]) + C::new("select.byAttributes", "Select By Attributes", "Home › Selection", by_attributes).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_layers) .params(r#"{"layer"?, "where": "SQL clause", "mode"?: "new"|"add"|"remove"|"subset"}"#), - C::new("select.byRect", "Select (rectangle)", "Home › Selection", by_rect).roles(&[]) + C::new("select.byRect", "Select (rectangle)", "Home › Selection", by_rect).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"minLon", "minLat", "maxLon", "maxLat", "mode"?: "new"|"add"|"remove"}"#), - C::new("select.byPolygon", "Select by Polygon", "Home › Selection", by_polygon).roles(&[]) + C::new("select.byPolygon", "Select by Polygon", "Home › Selection", by_polygon).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"points": [[lon,lat],...], "mode"?: "new"|"add"|"remove"}"#), - C::new("select.byRadius", "Select by Radius", "Home › Selection", by_radius).roles(&[]) + C::new("select.byRadius", "Select by Radius", "Home › Selection", by_radius).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"lon", "lat", "radius": metres (0 < radius <= 5000000), "mode"?: "new"|"add"|"remove"}"#), - C::new("select.atPoint", "Select (click)", "Home › Selection", at_point).roles(&[]).params(r#"{"lon", "lat", "tolerancePx"?, "mode"?}"#), + C::new("select.atPoint", "Select (click)", "Home › Selection", at_point).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"lon", "lat", "tolerancePx"?, "mode"?}"#), C::new("select.rows", "Select Rows", "Attribute Table", |s, p| { let id = s.layer_id(p)?; let rows: Vec = p @@ -35,19 +35,19 @@ pub fn commands() -> Vec { .unwrap_or_default(); set(s, id, rows, mode(p)); Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"layer"?, "rows": [number], "mode"?}"#), C::new("select.clear", "Clear", "Home › Selection", |s, _| { s.map_mut().clear_selection(); Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_selection), C::new("select.all", "Select All", "Data › Selection", |s, p| { let id = s.layer_id(p)?; let n = s.map().layer(id).map(|l| l.dataset.features.len()).unwrap_or(0); set(s, id, (0..n).collect(), Mode::New); Ok(Value::Null) - }).roles(&[]).params(r#"{"layer"?: name|id (default: the active layer)}"#) + }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?: name|id (default: the active layer)}"#) .when(need_layer), C::new("select.switch", "Switch Selection", "Data › Selection", |s, p| { let id = s.layer_id(p)?; @@ -55,7 +55,7 @@ pub fn commands() -> Vec { let rows: Vec = (0..l.dataset.features.len()).filter(|r| !l.selection.contains(r)).collect(); set(s, id, rows, Mode::New); Ok(Value::Null) - }).roles(&[]).params(r#"{"layer"?: name|id (default: the active layer)}"#) + }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?: name|id (default: the active layer)}"#) .when(need_layer), C::new("select.zoomTo", "Zoom To Selection", "Home › Selection", |s, _| { let e = s.map().selection_extent(); @@ -64,7 +64,7 @@ pub fn commands() -> Vec { } s.zoom_to(&e); Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .when(need_selection), C::new("select.layerFromSelection", "Make Layer From Selected Features", "Data › Selection", |s, p| { @@ -81,12 +81,12 @@ pub fn commands() -> Vec { } let nid = super::layer::add_dataset(s, ds); Ok(json!({"layer": nid})) - }).roles(&[]).params(r#"{"layer"?: name|id (default: the active layer, which need not be the layer last selected in)}"#) + }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?: name|id (default: the active layer, which need not be the layer last selected in)}"#) .when(need_selection), - C::new("identify", "Identify", "Home › Inspect", identify).roles(&[]).view().params( + C::new("identify", "Identify", "Home › Inspect", identify).roles(&[]).effects(crate::policy::Effects::NONE).view().params( r#"{"lon", "lat", "tolerancePx"?, "mode"?: "topmost"|"visible"|"selectable"|"selectedInContents"|"none" (the map's pop-up mode when omitted; the mode picks the layers, and every feature under the point in them, up to 100, is in "results"; topmost and visible take vector tile layers' features too, with "tileLayer" and "sourceLayer", from the tiles drawn there: one not loaded yet is asked for, and its features come on a later identify; and they list in "featureInfo" the WMS layers drawn there to ask, each {tileLayer, url} (its GetFeatureInfo request) or {tileLayer, capabilities} (its service's, when the layer doesn't know its request): identify.featureInfo reads what they answer; selectedInContents does both for the tile layer picked in the Layers pane (map.selectTiles) alone, whichever it is, a basemap or MapLibre style too)}"#, ), - C::new("map.popupMode", "Pop-up Mode", "Home › Navigate", popup_mode).roles(&[]) + C::new("map.popupMode", "Pop-up Mode", "Home › Navigate", popup_mode).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"mode": "topmost"|"visible"|"selectable"|"selectedInContents"|"none"}"#), ]; commands.extend(info::commands()); diff --git a/crates/engine/src/cmd/select_info.rs b/crates/engine/src/cmd/select_info.rs index 739bdc5a2d..030a227f35 100644 --- a/crates/engine/src/cmd/select_info.rs +++ b/crates/engine/src/cmd/select_info.rs @@ -22,7 +22,7 @@ const MAX_TEXT: usize = 2 * ogc::MAX_INFO_BYTES; pub(super) fn commands() -> Vec { vec![ - C::new("identify.featureInfo", "Identify WMS Layer", "Home › Inspect", feature_info).roles(&[]).view().params( + C::new("identify.featureInfo", "Identify WMS Layer", "Home › Inspect", feature_info).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).view().params( r#"{"layer": "a WMS tile layer's name", "lon"?, "lat"?: the point (for a request), "text"?: "the GetFeatureInfo answer, when already fetched (identify lists the requests)", "url"?: "the request text answers: with lon and lat, the service's error in its format gives the request in the next format the layer offers ({url}), and features answered in a later format keep the layer asking in it", "capabilities"?: "the service's capabilities document, when the layer doesn't know its request (identify then asks for it)", "request"?: bool (only say what to fetch next: {url} or {capabilities})} → results, as identify's (with "tileLayer"), or with request or after an error, what to fetch"#, ), ] diff --git a/crates/engine/src/cmd/service.rs b/crates/engine/src/cmd/service.rs index eb131c1580..52e9f172b0 100644 --- a/crates/engine/src/cmd/service.rs +++ b/crates/engine/src/cmd/service.rs @@ -30,16 +30,16 @@ const DOCUMENT_ROLES: &[crate::ParamRole] = &[ pub fn commands() -> Vec { let mut commands = vec![ - C::new("service.layers", "Service Layers", "Home › Add", layers).roles(&[]).view().params( + C::new("service.layers", "Service Layers", "Home › Add", layers).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).view().params( r#"{"url": "a WMS, WMTS or WFS address (GetCapabilities is added), an OGC API collections address or landing page (its map tiles and linked collections), or an ArcGIS FeatureServer, MapServer, ImageServer or VectorTileServer, or an ArcGIS services directory or folder (its folders and services, each listed by its own url)", "type"?: "wms"|"wmts"|"wfs"|"ogcapi"|"arcgis" (guessed from the address), "text"?: "the capabilities document, when already fetched", "documents"?: [{"url": "a discovered document address", "text": "its fetched JSON"}], "connection"?: "the saved web-service connection id whose login the requests use"} → layers with source_url, and next when supplied documents need another fetch"#, ), - C::new("service.add", "Add Service Layer", "Home › Add", add).roles(&[]).params( + C::new("service.add", "Add Service Layer", "Home › Add", add).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"url": "as service.layers", "type"?, "layer": "the layer's name (service.layers lists them)", "name"?: text, "time"?: "a time-enabled WMS or WMTS layer: by default (or \"slider\", true) it follows the time slider, turned on with the layer's times when it is off; or one of its dates (2024-06-01), or \"default\" (false): its service's default", "text"?: "the capabilities document, when already fetched", "documents"?: "additional fetched documents, as service.layers", "source_url"?: "the listed layer's source_url", "describe"?: "a WCS coverage's DescribeCoverage document, when already fetched", "tiles"?: "an OGC API layer's tile template, when already read from its tileset (the app's)", "bbox"?: [west, south, east, north] | "view" | "full": a WCS coverage's area (the view by default); for a WFS, OGC API or ArcGIS feature layer, only the features that cross it (the whole layer, "full", by default), "connection"?: "the id of the saved web-service connection it comes from (connection.browse gives it): the layer records it, and is read with its login"}"#, ), - C::new("service.coverage", "WCS Coverage Request", "Home › Add", coverage).roles(&[]).view().params( + C::new("service.coverage", "WCS Coverage Request", "Home › Add", coverage).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).view().params( r#"{"url": "a WCS address", "type"?: "wcs", "layer": "the coverage's name", "text"?: "the capabilities document, when already fetched", "documents"?: "additional fetched documents, as service.layers", "source_url"?: "the listed coverage's source_url", "describe"?: "its DescribeCoverage answer, when already fetched", "bbox"?: [west, south, east, north] | "view" (the default) | "full", "connection"?: "the saved web-service connection id whose login the request uses", "anonymous"?: boolean (never use a saved login for the coverage or its description)} → the GetCoverage address of a GeoTIFF (layer.addUrl adds it)"#, ), - C::new("catalog.search", "Search Catalogue", "Home › Add", catalog_search).roles(&[]).view().params( + C::new("catalog.search", "Search Catalogue", "Home › Add", catalog_search).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).view().params( r#"{"url": "a catalogue's CSW address", "query"?: "words anywhere in the records", "bbox"?: [west, south, east, north] | "view" (none by default), "start"?: "the first record's position (a page's next)", "text"?: "the GetRecords answer, when already fetched", "connection"?: "as service.layers"} → records with their service and download links (service.add or layer.addUrl add them)"#, ), ]; @@ -359,6 +359,7 @@ pub(crate) fn download_layer(s: &mut Session, name: &str, url: &str, max: usize, until = std::time::Instant::now() + cache::BLOCKING_TIMEOUT; } }; + cache::check_still(&cache::current_still())?; if let Some(note) = note { s.notify(format!("{name}: {note}"), false); } diff --git a/crates/engine/src/cmd/service_group.rs b/crates/engine/src/cmd/service_group.rs index f1025a6bb1..bcb1e403ac 100644 --- a/crates/engine/src/cmd/service_group.rs +++ b/crates/engine/src/cmd/service_group.rs @@ -9,16 +9,16 @@ use super::{C, CmdResult, MAX_GROUP_BYTES, Session, capabilities, ps, service_js pub fn commands() -> Vec { vec![ - C::new("service.import", "Import Service", "Home › Add", import).roles(&[]).params( + C::new("service.import", "Import Service", "Home › Add", import).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"url": "an OGC API landing page or collections address", "type"?, "name"?: "group name (default My OGC Layer)", "text"?, "documents"?: "as service.layers", "responses"?: [{"url": "features URL", "text": "downloaded GeoJSON"}], "connection"?: "as service.add", "anonymous"?: bool (internal: preserve plugin-selected service provenance)} ("group" and "layer" are rejected: use service.refresh)"#, ), - C::new("service.refresh", "Refresh", "Layers", refresh).roles(&[]).params( + C::new("service.refresh", "Refresh", "Layers", refresh).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"group"?: id, "layer"?: "one child layer id or name", "text"?, "documents"?, "responses"?: "as service.import", "connection"?: "as service.add (default: the one its layers record)", "anonymous"?: bool (internal: do not borrow a saved login)}"#, ), - C::new("service.groupPlan", "Service Import Requests", "Layers", plan).roles(&[]).view().params( + C::new("service.groupPlan", "Service Import Requests", "Layers", plan).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).view().params( r#"{"url"?, "type"?, "name"?, "group"?: id, "layer"?: "one child layer", "sourceOnly"?: "return the connection without fetching", "text"?, "documents"?, "connection"?: "as service.add", "anonymous"?: bool (internal: preserve plugin-selected service provenance)} → request, layers, and next while discovery is incomplete"#, ), - C::new("service.group", "Service Group", "Layers", set_group).roles(&[]).params( + C::new("service.group", "Service Group", "Layers", set_group).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"group"?: id, "layer"?: "a child identifying its group", "name"?: text, "expanded"?: bool, "visible"?: "set all children", "remove"?: "remove the group and its children"}"#, ), ] diff --git a/crates/engine/src/cmd/share.rs b/crates/engine/src/cmd/share.rs index 344e5817e8..0ed7c7f843 100644 --- a/crates/engine/src/cmd/share.rs +++ b/crates/engine/src/cmd/share.rs @@ -12,6 +12,11 @@ use super::exports::{Work, Wrote}; use super::{f, need_layer, ps}; use crate::{CommandSpec as C, Session}; +/// The map export binds exactly the map and tile routes the worker will copy. +fn map_inputs(s: &super::InputContext<'_>, _: &Value) -> Result { + Ok(super::tiles::render_inputs(s.map())) +} + pub fn commands() -> Vec { vec![ C::new("share.exportMap", "Export Map", "Publish › Output", |s, p| { @@ -19,14 +24,17 @@ pub fn commands() -> Vec { // tiles. The app runs it here, not on a thread, while one loads. super::tiles::follow_style_view(s, web_time::Duration::from_secs(30)); super::exports::write_here(s, p, "share.exportMap") - }).roles(&[]) + }).inputs(map_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"path": "*.png", "width"?: px, "height"?: px, "dpi"?: 96..600}"#), - C::new("share.exportLayer", "Export Features", "Publish › Output", |s, p| super::exports::write_here(s, p, "share.exportLayer")).roles(&[]) + C::new("share.exportLayer", "Export Features", "Publish › Output", |s, p| super::exports::write_here(s, p, "share.exportLayer")).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_layer) .params(r#"{"layer"?, "path": "*.geojson|*.csv|*.fgb|*.gpkg|*.parquet|*.shp|*.zip|*.kml|*.kmz|*.gpx", "crs"?: "EPSG:… (or WKT) for a GeoPackage, Shapefile or FlatGeobuf (default: the system the layer was read in, else EPSG:4326)", "mode"?: "newFile (default: refuses an existing file) | addTable | replaceTable, for a GeoPackage", "tableName"?: "the GeoPackage table (default: the layer's name)"}"#), C::new("share.exportTable", "Export Table", "Publish › Output", |s, p| { + let owner = s.continuation()?; + owner.check()?; + let _progress = rustgis_analysis::control::install_progress(None); let path = ps(p, "path").ok_or("path is required")?; let ext = Path::new(path).extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase(); let gpkg = gpkg_table(p, &ext)?; @@ -40,6 +48,7 @@ pub fn commands() -> Vec { } let (left_out, table) = rustgis_data::io::write_path_to(&t.dataset, Path::new(path), Crs::LonLat, &gpkg).map_err(|e| e.to_string())?; + s.check_continuation(&owner).map_err(|why| super::exports::completion_refused(Some(path), why))?; if left_out > 0 { s.notify(format!("{path}: {left_out} features without drawable geometry were left out"), false); } @@ -49,7 +58,7 @@ pub fn commands() -> Vec { let mut out = json!({"path": path}); report_table(s, path, &gpkg, table, &mut out); Ok(out) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params( r#"{"table": name|id (a standalone table), "path": "*.geojson|*.csv|*.fgb|*.gpkg|*.parquet|*.shp|*.zip|*.kml|*.kmz|*.gpx", "mode"?: "newFile (default: refuses an existing file) | addTable | replaceTable, for a GeoPackage", "tableName"?: "the GeoPackage table (default: the table's name)"}"#, ) @@ -280,6 +289,7 @@ fn write_layer(l: &rustgis_map::Layer, path: &str, asked: Option, gpkg: Gpk let answer = json!({"path": path, "features": written, "leftOut": left_out, "crs": crs.label()}); // The table written, said and put in the answer once the export lands, where the session // (layers reading a replaced table) is at hand. + let written_path = Some(path.to_string()); let path = path.to_string(); let then: Option = table.map(|t| { Box::new(move |s: &mut Session, out: &mut Value| { @@ -287,7 +297,14 @@ fn write_layer(l: &rustgis_map::Layer, path: &str, asked: Option, gpkg: Gpk Ok(()) }) as super::exports::Then }); - Ok(Wrote { answer, notes: said, then }) + Ok(Wrote { + answer, + notes: said, + written_path, + then, + #[cfg(not(target_arch = "wasm32"))] + owner: None, + }) } /// Whether `a` and `b` are the same existing file. diff --git a/crates/engine/src/cmd/sharing.rs b/crates/engine/src/cmd/sharing.rs index ae63bbab7d..437ad59710 100644 --- a/crates/engine/src/cmd/sharing.rs +++ b/crates/engine/src/cmd/sharing.rs @@ -129,7 +129,20 @@ const MANAGE: &str = r#"{"item"?: id|name (default: the last of its kind), "map" /// Publish shared items and import models, checking file compatibility before creating a model. pub fn commands() -> Vec { let share_as = |id: &'static str, label: &'static str, run: fn(&mut Session, &Value) -> CmdResult, params: &'static str| { - C::new(id, label, "Publish › Share As", run).roles(&[]).params(params) + C::new(id, label, "Publish › Share As", run) + .inputs(match id { + "share.webMap" => web_map_inputs, + "share.webScene" => web_scene_inputs, + "share.geodataService" => geodata_inputs, + "share.webTool" => web_tool_inputs, + "share.linkChart" => link_inputs, + "share.webStyle" => style_inputs, + "share.locator" => locator_inputs, + _ => layer_inputs, + }) + .roles(&[]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) + .params(params) }; vec![ share_as("share.webMap", "Web Map", web_map, r#"{"map"?: name (default: the active map), "path"?: "*.html" (default: the first data store)}"#), @@ -143,19 +156,19 @@ pub fn commands() -> Vec { C::new("share.jobs", "Jobs", "Publish › Share As", |s, _| { let items: Vec = s.project.shared.iter().rev().map(|i| serde_json::to_value(i).unwrap_or(Value::Null)).collect(); Ok(json!({"items": items, "stores": s.project.data_stores})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("share.saveWebMap", "Save Web Map", "Publish › Manage", |s, p| update_item(s, p, "webMap", ps(p, "map"))).roles(&[]).when(need_items).params(MANAGE), - C::new("share.replaceWebLayer", "Replace Web Layer", "Publish › Manage", |s, p| update_item(s, p, "webLayer", ps(p, "layer"))).roles(&[]) + C::new("share.saveWebMap", "Save Web Map", "Publish › Manage", |s, p| update_item(s, p, "webMap", ps(p, "map"))).inputs(update_web_map_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).when(need_items).params(MANAGE), + C::new("share.replaceWebLayer", "Replace Web Layer", "Publish › Manage", |s, p| update_item(s, p, "webLayer", ps(p, "layer"))).inputs(update_layer_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .when(need_items) .params(r#"{"item"?: id|name (default: the last web layer), "layer"?: the layer replacing it (default: the one it was made from)}"#), - C::new("share.dataStore", "Data Stores", "Publish › Manage", data_store).roles(&[]).params(r#"{"name"?, "path"?: folder, "remove"?: name (no parameters: list them)}"#), - C::new("share.upload", "Upload File", "Publish › Manage", upload).roles(&[]).params(r#"{"path": file, "store"?: name (default: the first data store)}"#), - C::new("share.saveWebScene", "Save Web Scene", "Publish › Manage", |s, p| update_item(s, p, "webScene", ps(p, "map"))).roles(&[]).when(need_items).params(MANAGE), - C::new("share.saveWebLinkChart", "Save Web Link Chart", "Publish › Manage", |s, p| update_item(s, p, "linkChart", ps(p, "relationship"))).roles(&[]) + C::new("share.dataStore", "Data Stores", "Publish › Manage", data_store).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params(r#"{"name"?, "path"?: folder, "remove"?: name (no parameters: list them)}"#), + C::new("share.upload", "Upload File", "Publish › Manage", upload).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params(r#"{"path": file, "store"?: name (default: the first data store)}"#), + C::new("share.saveWebScene", "Save Web Scene", "Publish › Manage", |s, p| update_item(s, p, "webScene", ps(p, "map"))).inputs(update_web_scene_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).when(need_items).params(MANAGE), + C::new("share.saveWebLinkChart", "Save Web Link Chart", "Publish › Manage", |s, p| update_item(s, p, "linkChart", ps(p, "relationship"))).inputs(update_links_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .when(need_items) .params(r#"{"item"?: id|name (default: the last link chart), "relationship"?: re-share another relationship}"#), - C::new("locator.find", "Find Place", "Home › Inspect", find).roles(&[]).params(r#"{"locator"?: shared item name or *.rgisloc path (default: the last locator), "text": search text, "limit"?: 1..100, "zoom"?: bool (go to the best match)}"#), + C::new("locator.find", "Find Place", "Home › Inspect", find).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params(r#"{"locator"?: shared item name or *.rgisloc path (default: the last locator), "text": search text, "limit"?: 1..100, "zoom"?: bool (go to the best match)}"#), C::new("model.import", "Import Model", "Analyze › Geoprocessing", |s, p| { let text = super::items::read_text(p)?; let v: Value = serde_json::from_str(&text).map_err(|e| format!("not a RustGIS web tool: {e}"))?; @@ -172,7 +185,7 @@ pub fn commands() -> Vec { s.notify("1 model name was trimmed, shortened, replaced or made unique during import.", false); } Ok(imported) - }).roles(&[crate::ParamRole { key: "text", role: crate::Role::Document(crate::Document::Model) }]) + }).roles(&[crate::ParamRole { key: "text", role: crate::Role::Document(crate::Document::Model) }]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Files)) .params(r#"{"path"?: "*.model.json" | "*.rgistool", "text"?: file JSON, "data"?: base64 file}"#), ] } @@ -182,7 +195,7 @@ pub fn share_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Ve match kind { "webMap" => { let mut m = map_of(s, p)?; - m.layers.iter_mut().filter(|l| l.visible).try_for_each(load_whole)?; + m.layers.iter_mut().filter(|l| shared_layer("webMap", l)).try_for_each(load_whole)?; let html = web_map_html(&m, &m.name)?; Ok((m.name.clone(), html.into_bytes(), m.name)) } @@ -205,17 +218,14 @@ pub fn share_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Ve } "geodataService" => { let mut m = map_of(s, p)?; - m.layers.iter_mut().filter(|l| !l.is_raster() && !l.graphics).try_for_each(load_whole)?; - let mut layers: Vec<&Dataset> = m.layers.iter().filter(|l| !l.is_raster() && !l.graphics).map(|l| &l.dataset).collect(); + m.layers.iter_mut().filter(|l| shared_layer("geodataService", l)).try_for_each(load_whole)?; + let mut layers: Vec<&Dataset> = m.layers.iter().filter(|l| shared_layer("geodataService", l)).map(|l| &l.dataset).collect(); layers.extend(s.project.tables.iter().map(|t| &t.dataset)); Ok((m.name.clone(), ogc_zip(&m.name, &layers, &[])?, m.name.clone())) } "webTool" => { - let ms = &mut s.project.models; - let m = match ps(p, "model") { - Some(n) => ms.iter_mut().find(|m| m.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no model '{n}'"))?, - None => ms.last_mut().ok_or("make a model first (ModelBuilder)")?, - }; + let i = shared_model(s, p)?; + let m = s.project.models.get_mut(i).ok_or("make a model first (ModelBuilder)")?; let name = m.name.clone(); let (bytes, report) = crate::durable::output(m, None, |m| { let params: Vec = m @@ -238,7 +248,7 @@ pub fn share_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Ve } "webScene" => { let mut m = map_of(s, p)?; - m.layers.iter_mut().filter(|l| l.visible && !l.is_raster()).try_for_each(load_whole)?; + m.layers.iter_mut().filter(|l| shared_layer("webScene", l)).try_for_each(load_whole)?; Ok((m.name.clone(), gltf(&m, s.view_size)?, m.name.clone())) } "linkChart" => { @@ -255,10 +265,7 @@ pub fn share_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Ve /// A map to share, its layer groups folded into their layers (the shared formats have none). fn map_of(s: &Session, p: &Value) -> Result { - let mut m = match ps(p, "map") { - Some(n) => s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(n)).cloned().ok_or_else(|| format!("no map '{n}'"))?, - None => s.map().clone(), - }; + let mut m = shared_map(s, p)?.clone(); m.bake_groups(); Ok(m) } @@ -271,7 +278,9 @@ fn load_whole(l: &mut Layer) -> Result<(), String> { Ok(()) } -fn layer_of(s: &Session, p: &Value) -> Result { +fn layer_of(input: &impl super::InputSource, p: &Value) -> Result { + let context = input.input_context(); + let s = &context; let id = s.layer_id(p)?; let l = s.map().layer(id).cloned().ok_or("no such layer")?; if l.is_raster() { @@ -281,7 +290,9 @@ fn layer_of(s: &Session, p: &Value) -> Result { } /// Where a share goes: `path`, else the named or first data store. -fn target(s: &Session, p: &Value, name: &str, ext: &str) -> Result { +fn target(input: &impl super::InputSource, p: &Value, name: &str, ext: &str) -> Result { + let context = input.input_context(); + let s = &context; if let Some(path) = ps(p, "path") { return Ok(path.to_string()); } @@ -337,25 +348,7 @@ fn share(s: &mut Session, p: &Value, kind: &str) -> CmdResult { /// Re-shares an item in place, from its source or a new one. fn update_item(s: &mut Session, p: &Value, kind: &str, new_source: Option<&str>) -> CmdResult { - let item = match p.get("item") { - Some(Value::Number(n)) => s.project.shared.iter().find(|i| Some(u64::from(i.id)) == n.as_u64() && i.kind == kind), - Some(Value::String(n)) => s.project.shared.iter().rev().find(|i| i.kind == kind && i.name.eq_ignore_ascii_case(n)), - _ => s.project.shared.iter().rev().find(|i| i.kind == kind), - } - .cloned() - .ok_or_else(|| format!("no shared {} to update", KINDS.iter().find(|k| k.0 == kind).map_or(kind, |k| k.2).to_ascii_lowercase()))?; - if !item.this_session { - return Err(format!( - "'{}' came with the opened project; share it again with Share As (choosing where it goes) before updating it", - item.name - )); - } - let source = new_source.unwrap_or(&item.source).to_string(); - let q = match kind { - "webLayer" => json!({"layer": source, "path": item.path}), - "linkChart" => json!({"relationship": if source.is_empty() { Value::Null } else { json!(source) }, "path": item.path}), - _ => json!({"map": source, "path": item.path}), - }; + let q = update_params(s, p, kind, new_source)?; share(s, &q, kind) } @@ -888,23 +881,7 @@ fn locator(l: &Layer, fields: Option<&[String]>) -> Result { fn find(s: &mut Session, p: &Value) -> CmdResult { let text = ps(p, "text").map(str::trim).filter(|t| !t.is_empty()).ok_or("text is required")?.to_lowercase(); - let path = match ps(p, "locator") { - Some(n) => s - .project - .shared - .iter() - .rev() - .find(|i| i.kind == "locator" && i.name.eq_ignore_ascii_case(n)) - .map_or_else(|| n.to_string(), |i| i.path.clone()), - None => s - .project - .shared - .iter() - .rev() - .find(|i| i.kind == "locator") - .map(|i| i.path.clone()) - .ok_or("share a locator first (Share As › Locator)")?, - }; + let path = locator_path(s, p)?; let body = super::items::read_text(&json!({"path": path}))?; let loc: Locator = serde_json::from_str(&body).map_err(|e| format!("not a RustGIS locator: {e}"))?; let limit = p.get("limit").and_then(Value::as_u64).unwrap_or(10).clamp(1, 100) as usize; @@ -1179,25 +1156,8 @@ fn viewpoint(m: &Map, at: &rustgis_map::Camera, view: (f64, f64)) -> (Value, Val /// A link chart: the features joined by the map's relationships, as a node-link diagram (a /// self-contained page with a force layout; drag nodes, hover for names). fn link_chart(s: &Session, rel: Option<&str>) -> Result { - let rels: Vec<&rustgis_map::design::Relationship> = s - .project - .maps - .iter() - .flat_map(|m| m.editing.relationships.iter()) - .filter(|r| rel.is_none_or(|n| r.name.eq_ignore_ascii_case(n))) - .collect(); - if rels.is_empty() { - return Err(match rel { - Some(n) => format!("no relationship '{n}'"), - None => "the project has no relationships (Data Design › Relationships)".into(), - }); - } - // A layer by name (the active map first) and the map it's in. - let active = s.project.active_map; - let find = |n: &str| { - let in_map = |mi: usize| s.project.maps.get(mi).and_then(|m| m.layer_by_name(n)).map(|l| (mi, l)); - in_map(active).or_else(|| (0..s.project.maps.len()).find_map(in_map)) - }; + let rels = link_relationships(s, rel)?; + let find = |name: &str| link_layer(s, name).and_then(|(m, l)| s.project.maps.iter().position(|p| std::ptr::eq(p, m)).map(|i| (i, l))); let mut nodes: Vec = Vec::new(); // Nodes by map, layer id and row (same-named layers in different maps stay apart). let mut index: HashMap<(usize, u32, usize), usize> = HashMap::new(); @@ -1296,3 +1256,207 @@ const moving=N.reduce((m,a)=>Math.max(m,Math.abs(a.vx),Math.abs(a.vy)),0);if(mov layers = script_json(&json!(layers)), )) } + +/// Resolves saved/default and literal locator paths without reading their contents. +pub(crate) fn locator_path(s: &Session, p: &Value) -> Result { + match ps(p, "locator") { + Some(n) => Ok(s + .project + .shared + .iter() + .rev() + .find(|i| i.kind == "locator" && i.name.eq_ignore_ascii_case(n)) + .map_or_else(|| n.to_string(), |i| i.path.clone())), + None => s + .project + .shared + .iter() + .rev() + .find(|i| i.kind == "locator") + .map(|i| i.path.clone()) + .ok_or_else(|| "share a locator first (Share As › Locator)".to_string()), + } +} + +/// Sharing accepts a string map name; every other value selects the active map. +fn shared_map<'a>(input: &'a impl super::InputSource, p: &Value) -> Result<&'a Map, String> { + let context = input.input_context(); + let s = &context; + match ps(p, "map") { + Some(n) => s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no map '{n}'")), + None => Ok(s.map()), + } +} +/// Reconstructs a managed item's effective stored source and path exactly as re-sharing uses them. +fn update_params(input: &impl super::InputSource, p: &Value, kind: &str, new_source: Option<&str>) -> Result { + let context = input.input_context(); + let s = &context; + let item = match p.get("item") { + Some(Value::Number(n)) => s.project.shared.iter().find(|i| Some(u64::from(i.id)) == n.as_u64() && i.kind == kind), + Some(Value::String(n)) => s.project.shared.iter().rev().find(|i| i.kind == kind && i.name.eq_ignore_ascii_case(n)), + _ => s.project.shared.iter().rev().find(|i| i.kind == kind), + } + .cloned() + .ok_or_else(|| format!("no shared {} to update", KINDS.iter().find(|k| k.0 == kind).map_or(kind, |k| k.2).to_ascii_lowercase()))?; + if !item.this_session { + return Err(format!( + "'{}' came with the opened project; share it again with Share As (choosing where it goes) before updating it", + item.name + )); + } + let source = new_source.unwrap_or(&item.source).to_string(); + Ok(match kind { + "webLayer" => json!({"layer": source, "path": item.path}), + "linkChart" => json!({"relationship": if source.is_empty() { Value::Null } else { json!(source) }, "path": item.path}), + _ => json!({"map": source, "path": item.path}), + }) +} + +/// Binds the managed web map's effective source and saved output path. +fn update_web_map_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + update_source_inputs(input, p, "webMap", ps(p, "map")) +} +/// Binds the managed scene's effective source and saved output path. +fn update_web_scene_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + update_source_inputs(input, p, "webScene", ps(p, "map")) +} +/// Binds a managed feature layer even when the request omits its source. +fn update_layer_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + update_source_inputs(input, p, "webLayer", ps(p, "layer")) +} +/// Binds the managed chart's stored relationship selection and saved path. +fn update_links_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + update_source_inputs(input, p, "linkChart", ps(p, "relationship")) +} +/// Uses the same effective parameter reconstruction as the actual managed update. +fn update_source_inputs(input: &impl super::InputSource, p: &Value, kind: &str, new_source: Option<&str>) -> Result { + let q = update_params(input, p, kind, new_source)?; + let mut inputs = share_kind_inputs(input, &q, kind)?; + inputs.state = json!({"source":inputs.state,"effective":q}); + Ok(inputs) +} + +/// Keeps every matching relationship in project order, or all relationships when omitted. +fn link_relationships<'a>( + input: &'a impl super::InputSource, + rel: Option<&str>, +) -> Result, String> { + let context = input.input_context(); + let s = &context; + let rels = s + .project + .maps + .iter() + .flat_map(|m| &m.editing.relationships) + .filter(|r| rel.is_none_or(|n| r.name.eq_ignore_ascii_case(n))) + .collect::>(); + if rels.is_empty() { + return Err(match rel { + Some(n) => format!("no relationship '{n}'"), + None => "the project has no relationships (Data Design › Relationships)".into(), + }); + } + Ok(rels) +} +/// Relationship endpoints resolve in the active map first, then the first project match. +fn link_layer<'a>(input: &'a impl super::InputSource, name: &str) -> Option<(&'a Map, &'a Layer)> { + let context = input.input_context(); + let s = &context; + let in_map = |m: &'a Map| m.layer_by_name(name).map(|l| (m, l)); + in_map(s.map()).or_else(|| s.project.maps.iter().find_map(in_map)) +} +/// Collects the actual existing relationship endpoints without constructing the chart. +fn link_source_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let rels = link_relationships(input, ps(p, "relationship"))?; + let mut inputs = + super::Inputs::layers(rels.iter().flat_map(|r| [r.origin.as_str(), r.destination.as_str()]).filter_map(|n| link_layer(input, n))); + inputs.state = json!({"relationships":rels}); + Ok(inputs) +} + +/// Layer inclusion is shared by the serializer's whole-source preparation and its pure selector. +fn shared_layer(kind: &str, l: &Layer) -> bool { + match kind { + "geodataService" => !l.is_raster() && !l.graphics, + "webScene" => l.visible && !l.is_raster(), + _ => l.visible, + } +} +/// Selects a named model case-insensitively, otherwise the last model, for web-tool serialization. +fn shared_model(input: &impl super::InputSource, p: &Value) -> Result { + let context = input.input_context(); + let s = &context; + match ps(p, "model") { + Some(n) => s.project.models.iter().position(|m| m.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no model '{n}'")), + None => s.project.models.len().checked_sub(1).ok_or_else(|| "make a model first (ModelBuilder)".into()), + } +} +/// Binds effective included datasets and the actual target path without producing export bytes. +fn share_kind_inputs(input: &impl super::InputSource, p: &Value, kind: &str) -> Result { + let context = input.input_context(); + let s = &context; + let (mut inputs, name) = match kind { + "webMap" | "webScene" | "geodataService" => { + let map = shared_map(input, p)?; + let mut baked = map.clone(); + baked.bake_groups(); + let mut inputs = super::Inputs::layers( + baked.layers.iter().filter(|l| shared_layer(kind, l)).filter_map(|l| map.layer(l.id)).map(|l| (map, l)), + ); + inputs.maps.push(map.instance_id()); + if kind == "geodataService" { + inputs.tables.extend(s.project.tables.iter().map(|t| t.id)); + } + (inputs, map.name.clone()) + } + "webTool" => { + let i = shared_model(input, p)?; + let model = s.project.models.get(i).ok_or("no model")?; + let mut inputs = super::Inputs::layers(std::iter::empty()); + inputs.state = json!({"model":model}); + (inputs, model.name.clone()) + } + "linkChart" => (link_source_inputs(s, p)?, ps(p, "relationship").unwrap_or("Relationships").to_string()), + _ => { + let l = layer_of(input, p)?; + (super::Inputs::primary(s, p)?, l.name) + } + }; + let ext = KINDS.iter().find(|k| k.0 == kind).map_or("bin", |k| k.1); + let path = target(input, p, &name, ext)?; + inputs.review_places.push(path.clone()); + inputs.state = json!({"selection":inputs.state,"path":path}); + Ok(inputs) +} +/// Binds visible web-map sources after applying the same group visibility as export. +fn web_map_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + share_kind_inputs(input, p, "webMap") +} +/// Binds visible feature sources that the scene exporter materializes. +fn web_scene_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + share_kind_inputs(input, p, "webScene") +} +/// Binds the feature map and standalone tables consumed by geodata export. +fn geodata_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + share_kind_inputs(input, p, "geodataService") +} +/// Freezes the selected model's executable definition for this output operation. +fn web_tool_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + share_kind_inputs(input, p, "webTool") +} +/// Binds chart relationships together with the effective export destination. +fn link_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + share_kind_inputs(input, p, "linkChart") +} +/// Binds the shared feature layer and its effective destination. +fn layer_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + share_kind_inputs(input, p, "webLayer") +} +/// Binds the style's owning feature layer and effective JSON destination. +fn style_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + share_kind_inputs(input, p, "webStyle") +} +/// Binds the locator's feature layer and effective locator destination. +fn locator_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + share_kind_inputs(input, p, "locator") +} diff --git a/crates/engine/src/cmd/sources.rs b/crates/engine/src/cmd/sources.rs index 6848ac603b..7498992862 100644 --- a/crates/engine/src/cmd/sources.rs +++ b/crates/engine/src/cmd/sources.rs @@ -10,14 +10,14 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("sources.list", "List Data Sources", "Home › Add › Data Sources", list).roles(&[]).view(), - C::new("sources.save", "Save Data Source", "Home › Add › Data Sources", |s, p| keep(s, p, "saved")).roles(&[]).view().params( + C::new("sources.list", "List Data Sources", "Home › Add › Data Sources", list).roles(&[]).effects(crate::policy::Effects::NONE).view(), + C::new("sources.save", "Save Data Source", "Home › Add › Data Sources", |s, p| keep(s, p, "saved")).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).view().params( r#"{"kind": "service"|"database"|"stac"|"file"|"url"|"tiles"|"zarr"|"s3", "address": "its path, web address or connection (never with a password)", "name"?: text, "extra"?: {"type"?: service type, "layer"?: text}}"#, ), - C::new("sources.favourite", "Favourite Data Source", "Home › Add › Data Sources", favourite).roles(&[]) + C::new("sources.favourite", "Favourite Data Source", "Home › Add › Data Sources", favourite).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"kind": as sources.save, "address": text, "name"?: text, "extra"?: object, "on"?: bool (false: no longer a favourite)}"#), - C::new("sources.remove", "Remove Data Source", "Home › Add › Data Sources", remove).roles(&[]) + C::new("sources.remove", "Remove Data Source", "Home › Add › Data Sources", remove).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"list": "saved"|"favourites"|"recent", "kind": text, "address": text}"#), ] diff --git a/crates/engine/src/cmd/sql.rs b/crates/engine/src/cmd/sql.rs index b8b2f7507b..da5bf13a9c 100644 --- a/crates/engine/src/cmd/sql.rs +++ b/crates/engine/src/cmd/sql.rs @@ -12,10 +12,10 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("sql.query", "Run SQL Query", "Analyze › SQL", query).roles(&[crate::ParamRole { key: "sql", role: crate::Role::Sql }, crate::ParamRole { key: "query", role: crate::Role::Sql }]).view().params( + C::new("sql.query", "Run SQL Query", "Analyze › SQL", query).roles(&[crate::ParamRole { key: "sql", role: crate::Role::Sql }, crate::ParamRole { key: "query", role: crate::Role::Sql }]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)).view().params( r#"{"sql": "one DuckDB SELECT (DuckDB Spatial's ST_ functions, read_parquet('…'), web addresses)", "database"?: "a .duckdb file or a PostgreSQL connection (postgres:host=… dbname=…) whose tables the query names", "password"?: "the connection's, kept for this session only", "limit"?: "rows returned, up to 1000 (default 100)"}"#, ), - C::new("sql.schema", "List Database Columns", "Analyze › SQL", schema).roles(&[crate::ParamRole { key: "sql", role: crate::Role::Sql }, crate::ParamRole { key: "query", role: crate::Role::Sql }]) + C::new("sql.schema", "List Database Columns", "Analyze › SQL", schema).roles(&[crate::ParamRole { key: "sql", role: crate::Role::Sql }, crate::ParamRole { key: "query", role: crate::Role::Sql }]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)) .view() .params(r#"{"database": "a .duckdb file or a PostgreSQL connection", "password"?: string}"#), ] diff --git a/crates/engine/src/cmd/stac.rs b/crates/engine/src/cmd/stac.rs index e2bb89de1b..57de452fd4 100644 --- a/crates/engine/src/cmd/stac.rs +++ b/crates/engine/src/cmd/stac.rs @@ -11,18 +11,18 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("stac.collections", "List STAC Collections", "Home › Add › Web Data › STAC Search", collections).roles(&[]) + C::new("stac.collections", "List STAC Collections", "Home › Add › Web Data › STAC Search", collections).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)) .view() .params(r#"{"url"?: "a STAC API (default: Earth Search, https://earth-search.aws.element84.com/v1; Planetary Computer: https://planetarycomputer.microsoft.com/api/stac/v1), or a static catalog's catalog.json or collection.json (its child catalogs and collections are listed)", "connection"?: "as service.add: the saved web-service connection whose login the request uses"}"#), - C::new("stac.search", "STAC Search", "Home › Add › Web Data › STAC Search", search).roles(&[]).params( + C::new("stac.search", "STAC Search", "Home › Add › Web Data › STAC Search", search).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"url"?: "a STAC API (default: Earth Search), or a static catalog's catalog.json or collection.json (walked from there, at most 400 documents)", "collections"?: id | [ids] (APIs), "bbox"?: [west, south, east, north] | "view" (the map's extent), "datetime"?: "2024-06-01" | "2024-06-01/2024-06-30", "limit"?: 1–200 (default 10), "maxCloud"?: percent, "sortby"?: "eo:cloud_cover" | "-datetime" (APIs that sort), "phase"?: "pre" | "post" (static catalogs of events), "properties"?: bool (every property of each item, not only its date and cloud cover; default false), "footprints"?: bool (adds the items' footprints as a layer, default true), "connection"?: "as service.add: the saved web-service connection whose login the request uses"}"#, ), - C::new("stac.addFootprints", "Add STAC Footprints", "Home › Add › Web Data › STAC Search", add_footprints_cmd).roles(&[]) + C::new("stac.addFootprints", "Add STAC Footprints", "Home › Add › Web Data › STAC Search", add_footprints_cmd).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"footprints": GeoJSON FeatureCollection (items' footprints, as STAC Search finds them), "name"?: text}"#), - C::new("stac.addCube", "Add STAC Data Cube", "Home › Add › Web Data › STAC Search", add_cube).roles(&[]).params( + C::new("stac.addCube", "Add STAC Data Cube", "Home › Add › Web Data › STAC Search", add_cube).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"url"?: a STAC API (default: Earth Search), "collection": id, "asset": key (a band, such as NDVI), "bbox"?: [west, south, east, north] | "view" (default: the map's extent), "start", "end": dates, "maxCloud"?: percent, "range"?: [low, high] (the values shown from black to white, in the asset's own units after its raster:bands scale and offset, the same for every date and tile; default: each file's 2nd–98th percentile), "name"?: text, "connection"?: "as service.add: the saved web-service connection whose login the request uses"} → a tile layer per tile of the grid the items cover, each a file per date following the time slider, which turns on over their dates when it is off (desktop)"#, ), - C::new("stac.addAsset", "Add STAC Asset", "Home › Add › Web Data › STAC Search", add_asset).roles(&[]).params( + C::new("stac.addAsset", "Add STAC Asset", "Home › Add › Web Data › STAC Search", add_asset).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"item": a STAC item (as stac.search returns it) | "url": an item's address (Planetary Computer assets are signed), "asset"?: key (default: visual, else the first GeoTIFF, else the first data asset), "name"?: text, "connection"?: "as service.add"}"#, ), ] @@ -1122,8 +1122,9 @@ mod deferred_asset_tests { assert_eq!(pending["pending"], true); let mut requests = s.take_url_requests(); assert_eq!(requests.len(), 1); - let mut request = requests.remove(0); - let download = s.download_started(); + let request = requests.remove(0); + let mut params = request.params().clone(); + let download = s.download_started_for(&request).unwrap(); assert!( s.download_landed( download, @@ -1137,8 +1138,8 @@ mod deferred_asset_tests { .into_bytes() ) ); - request["id"] = json!(download); - let added = s.run("layer.addDownload", &request).unwrap(); + params["id"] = json!(download); + let added = s.complete_download(download, ¶ms).unwrap(); let layer = s.map().layer(added["layer"].as_u64().unwrap() as u32).unwrap(); assert_eq!(layer.connection, connection); assert_eq!(layer.name, "Downloaded asset"); diff --git a/crates/engine/src/cmd/tiles.rs b/crates/engine/src/cmd/tiles.rs index 8c41e9a588..10b0dc5899 100644 --- a/crates/engine/src/cmd/tiles.rs +++ b/crates/engine/src/cmd/tiles.rs @@ -62,17 +62,18 @@ pub const NETWORK: &[&str] = &[ "arcgis.token", ]; +/// Registers commands for tile-layer sources, drawing properties, coverage and celestial bodies. pub fn commands() -> Vec { vec![ - C::new("map.addTiles", "Add Tile Layer", "Home › Add", add).roles(&[]).params( + C::new("map.addTiles", "Add Tile Layer", "Home › Add", add).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"url": "https://…/{z}/{x}/{y}.png" | vector tiles ".pbf" or TileJSON | MapLibre style.json | a PMTiles archive (web address or file path) | an MBTiles file path, "kind"?: "raster"|"vector"|"style"|"pmtiles"|"mbtiles" (guessed from the url), "name"?: text, "bounds"?: [west, south, east, north] (longitude/latitude coverage, when known), "attribution"?: text, "minZoom"?: number, "maxZoom"?: number, "tileSize"?: 256|512 (1024 too for a WMS or map service asked for in Web Mercator by its tiles' box: their pictures are asked for that size), "singleImage"?: bool (a WMS or map service: one picture of the view instead of tiles, so symbols and labels aren't cut at tile edges; tiles on the globe, a tilted map or a projection the service doesn't offer; by default on for one, unless tileSize is given), "tms"?: bool, "subdomains"?: [text], "layers"?: [vector tile layer names, drawn in colours of their own], "styleLayers"?: [MapLibre style layers drawing vector tiles instead (their source is the tiles')], "opacity"?: 0–1, "blend"?: "normal"|"multiply"|"screen"|"overlay"|"darken"|"lighten"|"difference" (how it combines with what is beneath), "overlay"?: bool (on top of the map's layers, as every new layer goes; without it a basemap, under them; one with the same address and role is replaced when it has the same name or the same look), "top"?: bool (on top of the map's layers, as a layer added as data goes; by default an overlay's place), "replace"?: bool (removes the other tile basemaps, not overlays), "id"?: catalog id, "view"?: bool (a MapLibre style: go to the place, zoom, bearing and pitch its root properties give, once it loads), "start"?, "end"?: dates a url with date placeholders ({date:YYYY-MM-DD}, {YYYY}) spans, turning the time slider on (or, when it is on, setting its span), "unit"?, "interval"?, "dates"?: its steps (as in time.set), "legend"?: "the web address of its legend picture (PNG or JPEG)" | [address | {"url": address, "minScale"?: scale denominator, "maxScale"?: one (the scales it is for, from minScale up to below maxScale)}] (at most 16; the Layers pane shows the one for the map's scale under the layer; service.add gives a WMS or WMTS layer its service's), "connection"?: "the id of the saved web-service connection it comes from (as service.add): the tile layer records it, and its tiles are read with its login"}"#, ), - C::new("map.removeTiles", "Remove Tile Layer", "Home › Add", remove).roles(&[]) + C::new("map.removeTiles", "Remove Tile Layer", "Home › Add", remove).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name"?: text, "index"?: number, "id"?: catalog id} (removes every tile layer when none is given)"#), - C::new("map.setTiles", "Tile Layer Properties", "Home › Add", set).roles(&[]).params( + C::new("map.setTiles", "Tile Layer Properties", "Home › Add", set).inputs(set_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"name"?: text | "index"?: number (0 = top) | "id"?: catalog id, "visible"?: bool, "opacity"?: 0–1, "blend"?: "normal"|"multiply"|"screen"|"overlay"|"darken"|"lighten"|"difference", "move"?: "up"|"down"|"top"|"bottom" (past the next layer in the drawing order of all the map's layers, a feature or raster layer too), "to"?: number (its place in that order among the other layers, 0 = top; the Layers pane lists it), "rename"?: text, "bands"?: [red, green, blue] | [band] (a cloud-optimised GeoTIFF's bands shown, from 1; [] for its own), "range"?: [low, high] (the values shown from black to full colour; null: each band's 2nd–98th percentile), "dequantize"?: "aef" (AlphaEarth's quantised embeddings) | "none", "legend"?: as map.addTiles's, or null for none, "hiddenLayers"?: [source layer name] (vector tiles' source layers not drawn or identified, as map.tiles's sourceLayers names them; replaces the list, [] or null for none), "singleImage"?: bool (as map.addTiles's), "tileSize"?: 256|512|1024 (a WMS or map service asked for in Web Mercator: its tiles' size)}"#, ), - C::new("map.tiles", "Tile Layers", "Home › Add", |s, _| Ok(list(s, false))).roles(&[]).view(), + C::new("map.tiles", "Tile Layers", "Home › Add", |s, _| Ok(list(s, false))).inputs(listing_inputs).roles(&[]).effects(crate::policy::Effects::NONE).view(), // As `layer.expand` for a layer's classes: what the pane shows, not an edit to undo. C::new("map.expandTiles", "Expand Tile Legend", "Layers", |s, p| { let i = find(s, p)?.ok_or("name or index is required")?; @@ -80,20 +81,20 @@ pub fn commands() -> Vec { let t = s.map_mut().tiles.get_mut(i).ok_or("no tile layer at that index")?; t.expanded = on.unwrap_or(!t.expanded); Ok(json!({"index": i, "expanded": t.expanded})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name"?: text | "index"?: number (0 = top) | "id"?: catalog id, "on"?: bool (toggles when omitted)} — shows or hides a tile layer's legend and source layers under it in the Layers pane"#) .view(), // As `layer.select` for a map layer: what the pane shows, not an edit to undo. - C::new("map.selectTiles", "Select Tile Layer", "Layers", select_tiles).roles(&[]) + C::new("map.selectTiles", "Select Tile Layer", "Layers", select_tiles).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name"?: text | "index"?: number (0 = top) | "id"?: catalog id} — picks a tile layer in the Layers pane, leaving no map layer active (picking one ends it): the Selected in Layers pop-up mode identifies it"#) .view(), - C::new("map.zoomToTiles", "Zoom To Tile Layer", "Home › Add", zoom_to).roles(&[]) + C::new("map.zoomToTiles", "Zoom To Tile Layer", "Home › Add", zoom_to).roles(&[]).effects(crate::policy::Effects::NONE).inputs(zoom_inputs) .params(r#"{"name"?: text | "index"?: number (0 = top) | "id"?: catalog id} (advertised service bounds, or the bounds in a PMTiles archive or cloud-optimised GeoTIFF header)"#) .view(), - C::new("map.body", "Celestial Body", "Home › Add", body).roles(&[]).params( + C::new("map.body", "Celestial Body", "Home › Add", body).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"name"?: "earth"|"moon"|"mars"|"mercury"|"venus"|"io"|"europa"|"ganymede"|"callisto"|"titan"|"pluto"|"charon" (map.bodies lists them; default: the basemap's body), "basemap"?: a planetary basemap's id (default: the body's first; USGS Imagery on the Earth) | false (adds none, keeping the web basemaps but other bodies')}"#, ), - C::new("map.bodies", "Celestial Bodies", "Home › Add", |s, _| Ok(bodies(s))).roles(&[]).view(), + C::new("map.bodies", "Celestial Bodies", "Home › Add", |s, _| Ok(bodies(s))).roles(&[]).effects(crate::policy::Effects::NONE).view(), ] } @@ -204,15 +205,29 @@ pub(crate) fn add(s: &mut Session, p: &Value) -> CmdResult { add_with_metadata_wait(s, p, web_time::Duration::from_secs(1)) } +/// The executor and its source-reach selector accept the same explicit or guessed kind. +fn add_kind(p: &Value, url: &str) -> Result { + match ps(p, "kind") { + Some(k) => TileKind::from_code(k).ok_or_else(|| format!("unknown kind '{k}' (raster, vector, style, pmtiles or mbtiles)")), + None => Ok(TileKind::guess(url)), + } +} + +/// Local archive additions read files even though the command also declares network reach. +pub(crate) fn add_reach(p: &Value) -> crate::policy::Effects { + let Some(url) = ps(p, "url").map(str::trim) else { return crate::policy::Effects::NONE }; + match add_kind(p, url) { + Ok(kind @ (TileKind::PmTiles | TileKind::MbTiles)) => tile_source_reach(&TileLayer::new("", kind, url)), + _ => crate::policy::Effects::NONE, + } +} + /// Adds after a caller's header check; a zero wait avoids spending that caller's budget twice. pub(crate) fn add_with_metadata_wait(s: &mut Session, p: &Value, metadata_wait: web_time::Duration) -> CmdResult { let bound = crate::service_logins::request_params(s, p)?; let p = bound.as_ref(); let url = ps(p, "url").map(str::trim).filter(|u| !u.is_empty()).ok_or("url is required")?; - let kind = match ps(p, "kind") { - Some(k) => TileKind::from_code(k).ok_or_else(|| format!("unknown kind '{k}' (raster, vector, style, pmtiles or mbtiles)"))?, - None => TileKind::guess(url), - }; + let kind = add_kind(p, url)?; // A dated address (`{date:YYYY-MM-DD}`) follows the time slider, which the layer's span // (`start`, `end`) turns on when it is off. let dated = rustgis_data::time::is_template(url); @@ -516,8 +531,9 @@ fn follow_archive(s: &mut Session, t: &TileLayer, wait: web_time::Duration) -> b } /// The tile layer a command names by `index`, `name` or `id`. -pub(crate) fn find(s: &Session, p: &Value) -> Result, String> { - let tiles = &s.map().tiles; +pub(crate) fn find(input: &impl super::InputSource, p: &Value) -> Result, String> { + let context = input.input_context(); + let tiles = &context.map().tiles; if let Some(i) = f(p, "index") { let i = (i >= 0.0 && i.fract() == 0.0).then_some(i as usize).filter(|i| *i < tiles.len()); return i.map(Some).ok_or_else(|| "no tile layer at that index".to_string()); @@ -531,6 +547,143 @@ pub(crate) fn find(s: &Session, p: &Value) -> Result, String> { Ok(None) } +/// Exact source and routing state, kept private and charged to the planner's existing size limit. +/// Inline style layers change drawing, not the tile source, and are deliberately not copied here. +fn tile_source_state(t: &TileLayer) -> Value { + json!({ + "name":t.name,"id":t.id,"kind":t.kind.code(),"url":t.url,"bounds":t.bounds, + "connection":t.connection,"anonymous":t.anonymous,"subdomains":t.subdomains, + "minZoom":t.min_zoom,"maxZoom":t.max_zoom,"tileSize":t.tile_size,"tms":t.tms, + "singleImage":t.single_image,"layers":t.layers,"hiddenLayers":t.hidden_layers, + "timeUrl":t.time_url,"timeUrls":t.time_urls,"timeAt":t.time_at,"timeHidden":t.time_hidden, + "legends":t.legends,"visible":t.visible,"opacity":t.opacity,"error":t.error.is_some(), + "overlay":t.overlay,"over":t.over + }) +} + +/// Binds the executor's chosen tile before its coverage header can be read. +fn zoom_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let i = find(input, p)?.ok_or("give the tile layer's name or index")?; + Ok(tile_inputs(input.map(), [i])) +} + +/// Freezes only the selected tile routes, with their stack positions and owning map. +pub(crate) fn tile_inputs(map: &rustgis_map::Map, indices: impl IntoIterator) -> super::Inputs { + let mut inputs = super::Inputs::layers(std::iter::empty()); + inputs.maps.push(map.instance_id()); + let tiles: Vec<_> = indices + .into_iter() + .filter_map(|i| { + map.tiles.get(i).map(|tile| { + inputs.review_places.push(crate::sources::redact(&tile.url)); + json!({"index":i,"tile":tile_source_state(tile)}) + }) + }) + .collect(); + inputs.state = json!({"map":map.instance_id(),"tiles":tiles}); + inputs +} + +/// Archive names in the returned list need metadata even when their bounds are already known. +fn listing_indices(map: &rustgis_map::Map) -> impl Iterator + '_ { + map.tiles.iter().enumerate().filter(|(_, t)| matches!(t.kind, TileKind::PmTiles | TileKind::MbTiles)).map(|(i, _)| i) +} + +/// Listing source-layer names asks archive metadata for every archive, even with known bounds. +pub(crate) fn listing_inputs(input: &super::InputContext<'_>, _: &Value) -> Result { + Ok(tile_inputs(input.map(), listing_indices(input.map()))) +} + +/// Binds the property target and archive routes read for the returned listing. +fn set_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let i = find(input, p)?.ok_or("name or index is required")?; + let mut indices: Vec<_> = listing_indices(input.map()).collect(); + if !indices.contains(&i) { + indices.push(i); + } + Ok(tile_inputs(input.map(), indices)) +} + +/// An uploaded MBTiles archive is already in memory; only unwrapped local archives read files. +/// Anonymous S3 and saved-login wrappers retain network reach even if their payload is malformed. +fn tile_source_reach(t: &TileLayer) -> crate::policy::Effects { + use crate::policy::{Effect, Effects}; + use rustgis_render::rustgis_tiles::{mbtiles, s3}; + if t.kind == TileKind::MbTiles && mbtiles::is_upload(&t.url) { + Effects::NONE + } else if matches!(t.kind, TileKind::PmTiles | TileKind::MbTiles) + && !s3::split_anonymous(&t.url).1 + && rustgis_map::tiles::is_local_archive(&t.url) + { + Effects::NONE.with(Effect::Files) + } else { + Effects::NONE.with(Effect::Network) + } +} + +/// Mirrors `extent_of`: advertised bounds and kinds without a coverage reader do no I/O. +pub(crate) fn metadata_reach(input: &impl super::InputSource, p: &Value) -> crate::policy::Effects { + let context = input.input_context(); + let tile = find(&context, p).ok().flatten().and_then(|i| context.map().tiles.get(i)); + tile.map_or(crate::policy::Effects::NONE, coverage_reach) +} + +/// Coverage is read only when it was not supplied by the service or project. +pub(crate) fn coverage_reach(t: &TileLayer) -> crate::policy::Effects { + if t.bounds.is_none() && matches!(t.kind, TileKind::PmTiles | TileKind::MbTiles | TileKind::Cog) { + tile_source_reach(t) + } else { + crate::policy::Effects::NONE + } +} + +/// Archive source-layer names come from metadata rather than the stored geographic bounds. +pub(crate) fn listing_reach(map: &rustgis_map::Map) -> crate::policy::Effects { + listing_indices(map) + .filter_map(|i| map.tiles.get(i)) + .fold(crate::policy::Effects::NONE, |reach, tile| reach.union(tile_source_reach(tile))) +} + +/// Setting a COG look checks its header; the command's returned list also reads archive names. +pub(crate) fn set_reach(input: &super::InputContext<'_>, p: &Value) -> crate::policy::Effects { + let mut reach = listing_reach(input.map()); + if matches!(cog_look(p), Ok(Some(_))) + && let Some(tile) = find(input, p).ok().flatten().and_then(|i| input.map().tiles.get(i)) + && tile.kind == TileKind::Cog + { + reach = reach.union(tile_source_reach(tile)); + } + reach +} + +/// Retains raw tile routes alongside the map and ordinary layers consumed by an export. +/// Only separately redacted addresses enter its public review; private state is never a label. +pub(crate) fn render_inputs(map: &rustgis_map::Map) -> super::Inputs { + let mut inputs = super::Inputs::layers(map.layers.iter().map(|layer| (map, layer))); + inputs.maps.push(map.instance_id()); + inputs.state = json!({"map":map.instance_id(),"tiles":map.tiles.iter().map(tile_source_state).collect::>()}); + for tile in map.tiles.iter().filter(|t| t.draws() && t.opacity > 0.0) { + inputs.review_places.push(crate::sources::redact(&tile.url)); + inputs.review_places.extend(tile.legends.iter().map(|l| crate::sources::redact(&l.url))); + } + inputs +} + +/// Source reach of tiles a referenced map draws, including their web legend pictures. +/// The map's built-in basemap is a drawn palette and has no external source. +pub(crate) fn render_reach(map: &rustgis_map::Map) -> crate::policy::Effects { + map.tiles.iter().fold(crate::policy::Effects::NONE, |reach, tile| reach.union(render_tile_reach(tile))) +} + +/// A resolved frame's tile uses the same drawing and source rules as an ordinary map export. +pub(crate) fn render_tile_reach(tile: &TileLayer) -> crate::policy::Effects { + if !(tile.draws() && tile.opacity > 0.0) { + return crate::policy::Effects::NONE; + } + let reach = tile_source_reach(tile); + if tile.legends.is_empty() { reach } else { reach.with(crate::policy::Effect::Network) } +} + /// Picks a tile layer in the Layers pane: the only one picked, with no map layer active. fn select_tiles(s: &mut Session, p: &Value) -> CmdResult { let i = find(s, p)?.ok_or("name or index is required")?; diff --git a/crates/engine/src/cmd/time.rs b/crates/engine/src/cmd/time.rs index a2f471a13c..205c28a362 100644 --- a/crates/engine/src/cmd/time.rs +++ b/crates/engine/src/cmd/time.rs @@ -25,7 +25,7 @@ fn need_slider(s: &Session) -> Option<&'static str> { pub fn commands() -> Vec { vec![ - C::new("time.bind", "Bind to Time Slider", "View › Time", bind).roles(&[]) + C::new("time.bind", "Bind to Time Slider", "View › Time", bind).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "field"?: date field (default: the one most of whose values read as dates), "start"?, "end"?: dates (default: the field's span, when the slider is new), "unit"?: "hour"|"day"|"month"|"year", "interval"?: number, "show"?: "step"|"wide"|"wider"|"cumulative"}"#), C::new("time.unbind", "Unbind from Time Slider", "View › Time", |s, p| { @@ -34,7 +34,7 @@ pub fn commands() -> Vec { let was = l.time.take().map(|t| t.field); super::zarr::follow_time(s); Ok(json!({"unbound": was})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?}"#), C::new("time.fields", "Time Fields", "View › Time", |s, p| { @@ -42,11 +42,11 @@ pub fn commands() -> Vec { let l = s.map().layer(id).ok_or("no such layer")?; let fields: Vec = time_fields(&l.dataset).into_iter().map(|(n, share)| json!({"field": n, "share": share})).collect(); Ok(json!({"fields": fields, "bound": l.time.as_ref().map(|t| t.field.clone())})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .when(need_feature_layer) .params(r#"{"layer"?}"#), - C::new("time.set", "Time Slider Settings", "View › Time", set).roles(&[]).params( + C::new("time.set", "Time Slider Settings", "View › Time", set).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"start"?, "end"?: dates (text, a year or milliseconds since 1970), "date"?: the current step, "unit"?: "hour"|"day"|"month"|"year", "interval"?: steps of that many units, "dates"?: [dates] (irregular steps; [] for regular ones), "show"?: "step"|"wide"|"wider"|"cumulative", "before"?: steps before the current one the window takes in, "after"?: steps from the current one (at least 1), "cumulative"?: bool, "speed"?: milliseconds a step shows for, "loop"?: bool, "format"?: date format (YYYY, MM, MMM, DD, HH, mm), "off"?: bool (turns the slider off, every feature showing)}"#, ), C::new("time.goTo", "Go to Date", "View › Time", |s, p| { @@ -64,7 +64,7 @@ pub fn commands() -> Vec { m.apply_time(); super::zarr::follow_time(s); Ok(info(s)) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .when(need_slider) .params(r#"{"date": date | "step": number (0 = the first)}"#), @@ -76,12 +76,12 @@ pub fn commands() -> Vec { m.apply_time(); super::zarr::follow_time(s); Ok(info(s)) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .when(need_slider) .params(r#"{"by"?: steps (default 1; negative goes back), "wrap"?: bool (past the end starts over)}"#), - C::new("time.info", "Time Slider", "View › Time", |s, _| Ok(info(s))).roles(&[]).view(), - C::new("time.export", "Export Time Animation", "View › Time", export).roles(&[]) + C::new("time.info", "Time Slider", "View › Time", |s, _| Ok(info(s))).roles(&[]).effects(crate::policy::Effects::NONE).view(), + C::new("time.export", "Export Time Animation", "View › Time", export).inputs(export_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_slider) .params(r#"{"path": "*.gif" | "*.zip" (PNG frames), "width"?, "height"?: px, "speed"?: milliseconds a frame shows for (default: the slider's), "from"?, "to"?: dates (default: the whole span)}"#), @@ -253,6 +253,72 @@ pub(crate) fn set(s: &mut Session, p: &Value) -> CmdResult { Ok(info(s)) } +/// The executor's bounded frame selection, shared with its pure source selector. +struct ExportFrames { + saved: TimeSlider, + steps: Vec, + width: f64, + height: f64, +} + +/// Selects date-filtered steps and dimensions within the executor's frame and pixel budgets. +fn export_frames(map: &rustgis_map::Map, p: &Value) -> Result { + let (w, h) = (f(p, "width").unwrap_or(640.0).clamp(64.0, 1920.0), f(p, "height").unwrap_or(400.0).clamp(64.0, 1080.0)); + let (from, to) = (date(p, "from")?, date(p, "to")?); + let saved = map.time.clone().ok_or("the time slider is off")?; + let steps: Vec = saved.steps().into_iter().filter(|t| from.is_none_or(|a| *t >= a) && to.is_none_or(|z| *t <= z)).collect(); + if steps.is_empty() { + return Err("no step falls between from and to".into()); + } + // Rendering blocks while it runs: a bound on the work. + if steps.len() > MAX_FRAMES || steps.len() as f64 * w * h > MAX_PIXELS { + return Err(format!("{} frames at {w:.0} × {h:.0} is too many; narrow from and to, or lower the size", steps.len())); + } + Ok(ExportFrames { saved, steps, width: w, height: h }) +} + +/// Resolves only bounded tile/slider copies with the same date owner that each real frame uses. +fn frame_tiles(map: &rustgis_map::Map, frames: &ExportFrames, mut visit: impl FnMut(&rustgis_map::TileLayer)) { + for source in &map.tiles { + let mut tile = source.clone(); + let mut slider = frames.saved.clone(); + for step in &frames.steps { + slider.current = *step; + slider.normalize(); + tile.apply_time(Some(&slider)); + if tile.draws() && tile.opacity > 0.0 { + visit(&tile); + } + } + } +} + +/// Binds raw dated definitions and selected steps, showing only resolved redacted destinations. +fn export_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let frames = export_frames(input.map(), p)?; + let mut inputs = super::tiles::render_inputs(input.map()); + // Raw definitions plus the exact slider/steps bind every derived route without expanding + // private state into a second copy for every frame. Only redacted resolved URLs are shown. + inputs.state = json!({"render":inputs.state,"time":frames.saved,"steps":frames.steps}); + let mut places = std::collections::BTreeSet::new(); + frame_tiles(input.map(), &frames, |tile| { + places.insert(crate::sources::redact(&tile.url)); + places.extend(tile.legends.iter().map(|legend| crate::sources::redact(&legend.url))); + }); + inputs.review_places = places.into_iter().collect(); + Ok(inputs) +} + +/// A later selected frame can reveal another source even when the current frame draws none. +pub(crate) fn export_reach(input: &super::InputContext<'_>, p: &Value) -> crate::policy::Effects { + let Ok(frames) = export_frames(input.map(), p) else { return crate::policy::Effects::NONE }; + let mut reach = crate::policy::Effects::NONE; + frame_tiles(input.map(), &frames, |tile| { + reach = reach.union(super::tiles::render_tile_reach(tile)); + }); + reach +} + /// Renders every step (or those from `from` to `to`) and writes them as a GIF or a zip of PNGs. fn export(s: &mut Session, p: &Value) -> CmdResult { // Files are written by the desktop app; the browser's go through its services. @@ -265,18 +331,8 @@ fn export(s: &mut Session, p: &Value) -> CmdResult { if !gif && !lower.ends_with(".zip") { return Err("path must end in .gif (an animation) or .zip (PNG frames)".into()); } - let (w, h) = (f(p, "width").unwrap_or(640.0).clamp(64.0, 1920.0), f(p, "height").unwrap_or(400.0).clamp(64.0, 1080.0)); - let (from, to) = (date(p, "from")?, date(p, "to")?); - let saved = s.map().time.clone().ok_or("the time slider is off")?; + let ExportFrames { saved, steps, width: w, height: h } = export_frames(s.map(), p)?; let speed = f(p, "speed").map_or(saved.speed_ms, |x| x.clamp(16.0, 60_000.0) as u32); - let steps: Vec = saved.steps().into_iter().filter(|t| from.is_none_or(|a| *t >= a) && to.is_none_or(|z| *t <= z)).collect(); - if steps.is_empty() { - return Err("no step falls between from and to".into()); - } - // Rendering blocks while it runs: a bound on the work. - if steps.len() > MAX_FRAMES || steps.len() as f64 * w * h > MAX_PIXELS { - return Err(format!("{} frames at {w:.0} × {h:.0} is too many; narrow from and to, or lower the size", steps.len())); - } // Each frame shows the view's extent, as Export Map does. The slider comes back however the // frames end (an error, or a panic the session catches). let made = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| frames(s, &steps, w, h, gif.then_some(speed)))); @@ -336,3 +392,19 @@ fn frames(s: &mut Session, steps: &[f64], w: f64, h: f64, gif: Option) -> R } if gif.is_some() { Ok(out) } else { rustgis_data::zip::write(&pngs) } } + +/// `follow_time` visits these source-backed layers in every map, including deferred reads. +/// Capturing their reach is pure: it neither advances time nor opens a Zarr store. +pub(crate) fn following_sources(s: &Session) -> Vec<(u64, u32, &str)> { + s.project + .maps + .iter() + .flat_map(|m| { + m.layers.iter().filter_map(move |l| l.raster.as_ref()?.time_zarr.as_ref().map(|z| (m.instance_id(), l.id, z.store.as_str()))) + }) + .collect() +} +/// Commands that actually call the shared `follow_time` executor, including nested settings. +pub(crate) fn follows_sources(id: &str) -> bool { + matches!(id, "time.bind" | "time.unbind" | "time.set" | "time.goTo" | "time.step" | "time.export") +} diff --git a/crates/engine/src/cmd/view.rs b/crates/engine/src/cmd/view.rs index eb4306b565..4af97f16fb 100644 --- a/crates/engine/src/cmd/view.rs +++ b/crates/engine/src/cmd/view.rs @@ -11,7 +11,7 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("map.effects", "View Effects", "View › Effects", effects).roles(&[]).params( + C::new("map.effects", "View Effects", "View › Effects", effects).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"sunTime"?: seconds since 1970|"now"|null (illumination), "weather"?: "none"|"haze"|"fog"|"rain"|"snow", "vignette"?, "edges"?, "stars"?: bool (visual effects; stars around the globe), "tiltShift"?, "grain"?: bool (camera effects), "brightness"?, "contrast"?, "saturation"?: -100..100, "sepia"?: 0..100 (post-processing), "reset"?: bool}"#, ), C::new("map.colorVision", "Color Vision Simulator", "View › Accessibility", |s, p| { @@ -19,13 +19,13 @@ pub fn commands() -> Vec { let cv = ColorVision::from_code(m).ok_or_else(|| format!("unknown colour vision '{m}'"))?; s.map_mut().view.color_vision = cv; Ok(json!({"mode": cv.code()})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"mode": "normal"|"protanopia"|"deuteranopia"|"tritanopia"|"achromatopsia"}"#), C::new("layer.priority", "Depth Priority", "View › Scene", |s, p| { let v = p.get("value").and_then(Value::as_i64).ok_or("value is required (higher draws on top)")?.clamp(-1000, 1000) as i32; with(s, p, |l| l.priority = v) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_layer) .params(r#"{"layer"?, "value": integer (higher draws on top)}"#), C::new("map.drawingMode", "Drawing Mode", "View › Scene", |s, p| { @@ -36,7 +36,7 @@ pub fn commands() -> Vec { }; s.map_mut().view.wireframe = w; Ok(json!({"wireframe": w})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"mode": "standard"|"wireframe"}"#), C::new("map.clipping", "View Clipping", "View › View Clipping", |s, p| { let d = match ps(p, "mode").unwrap_or("distance") { @@ -46,9 +46,9 @@ pub fn commands() -> Vec { }; s.map_mut().view.clip_distance = d; Ok(json!({"distance": d})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"mode": "none"|"distance", "distance"?: metres from the view's centre}"#), - C::new("map.terrain", "Terrain", "View › Scene", terrain).roles(&[]).params( + C::new("map.terrain", "Terrain", "View › Scene", terrain).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"layer"?: a DEM raster layer (heights in metres; without layer or tiles, and no terrain yet, the active layer when it is a raster) | "tiles"?: elevation tile URL with {z}/{x}/{y}, "encoding"?: "terrarium"|"mapbox" (tiles; default terrarium), "exaggeration"?: 0 to 20 (default 1, or the terrain's), "off"?: true}"#, ), C::new("map.displayType", "Display Type", "View › Options", |s, p| { @@ -59,17 +59,17 @@ pub fn commands() -> Vec { }; s.map_mut().view.fast = fast; Ok(json!({"mode": if fast { "speed" } else { "quality" }})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"mode": "quality"|"speed"}"#), C::new("map.navigator", "Navigator", "View › Navigation", |s, p| { let v = &mut s.map_mut().view; v.navigator = b(p, "on").unwrap_or(!v.navigator); Ok(json!({"navigator": v.navigator})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"on"?: bool}"#), - C::new("map.camera", "Camera", "View › Navigation", camera).roles(&[]).view().params(r#"{"lon"?, "lat"?, "scale"? (the camera's position when omitted)}"#), + C::new("map.camera", "Camera", "View › Navigation", camera).roles(&[]).effects(crate::policy::Effects::NONE).view().params(r#"{"lon"?, "lat"?, "scale"? (the camera's position when omitted)}"#), C::new("map.navigation", "Navigation Mode", "Home › Navigate", |s, p| { let fp = match ps(p, "mode").ok_or("mode is required")? { "planar" => false, @@ -83,7 +83,7 @@ pub fn commands() -> Vec { m.set_projection(rustgis_map::Projection::WebMercator); } Ok(json!({"mode": if fp { "firstPerson" } else { "planar" }})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"mode": "planar"|"firstPerson"}"#), C::new("map.viewMode", "View", "View › View", |s, p| { let next = match ps(p, "mode").ok_or("mode is required")? { @@ -100,9 +100,9 @@ pub fn commands() -> Vec { let m = s.map_mut(); m.set_projection(next); Ok(json!({"projection": m.projection.code(), "label": m.projection.label()})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"mode": "global" (a globe)|"local" (a flat view centred here; not in the web app)}"#), - C::new("map.walk", "Walk", "Home › Navigate", walk).roles(&[]).view().params(r#"{"forward"?: steps, "right"?: steps, "up"?: steps (first-person navigation)}"#), + C::new("map.walk", "Walk", "Home › Navigate", walk).roles(&[]).effects(crate::policy::Effects::NONE).view().params(r#"{"forward"?: steps, "right"?: steps, "up"?: steps (first-person navigation)}"#), ] } diff --git a/crates/engine/src/cmd/webdata.rs b/crates/engine/src/cmd/webdata.rs index 0dd8a77cda..d65a153630 100644 --- a/crates/engine/src/cmd/webdata.rs +++ b/crates/engine/src/cmd/webdata.rs @@ -13,12 +13,12 @@ pub fn commands() -> Vec { vec![ C::new("nationalMap.services", "List The National Map Services", "Home › Add › Web Data › United States", |_, _| { Ok(national_map_list()) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("nationalMap.add", "Add The National Map Service", "Home › Add › Web Data › United States", national_map_add).roles(&[]).params( + C::new("nationalMap.add", "Add The National Map Service", "Home › Add › Web Data › United States", national_map_add).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"service": "a service's id or title (nationalMap.services lists them), e.g. \"basemap/USGSTopo\"", "name"?: text}"#, ), - C::new("fieldsOfTheWorld.add", "Add Fields of the World", "Home › Add › Web Data", fields_add).roles(&[]).params( + C::new("fieldsOfTheWorld.add", "Add Fields of the World", "Home › Add › Web Data", fields_add).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).params( r#"{"year"?: 2017–2025 (2025), "draw"?: "both"|"fields"|"density" (both: field density zoomed out, fields from zoom 9), "minScore"?: 0–100 (fields scoring below are hidden; 0)}"#, ), ] diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index bacc89b789..48fed6abc9 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -14,25 +14,25 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("gp.env", "Environments", "Analyze › Geoprocessing", environments).roles(&[]).params( + C::new("gp.env", "Environments", "Analyze › Geoprocessing", environments).inputs(environment_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"extent"?: "view"|"full"|layer name|[minLon, minLat, maxLon, maxLat]|null, "cellSize"?: degrees|null}"#, ), - C::new("gp.gallery", "Tool Gallery", "Analyze › Portal", gallery).roles(&[]).view().params(r#"{"kind": "ready"|"feature"|"raster"}"#), - C::new("model.new", "ModelBuilder", "Analyze › Geoprocessing", new_model).roles(&[crate::ParamRole { key: "steps", role: crate::Role::ModelSteps }, crate::ParamRole { key: "graph", role: crate::Role::ModelGraph }]).params( + C::new("gp.gallery", "Tool Gallery", "Analyze › Portal", gallery).roles(&[]).effects(crate::policy::Effects::NONE).view().params(r#"{"kind": "ready"|"feature"|"raster"}"#), + C::new("model.new", "ModelBuilder", "Analyze › Geoprocessing", new_model).roles(&[crate::ParamRole { key: "steps", role: crate::Role::ModelSteps }, crate::ParamRole { key: "graph", role: crate::Role::ModelGraph }]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Files)).params( r#"{"name"?: string, "graph"?: {"nodes": [{"id", "kind": "input"|"tool"|"output", "x", "y" (optional), an input's "layer", a tool's "tool" and "params", an output's "name"}], "edges": [{"from", "fromPort": "out", "to", "toPort": a tool's layer parameter | "in" (output)}]}, "steps"?: [{"tool": id, "params": {…, "%1" for step 1's output}}], "fromHistory"?: bool, "imported"?: bool (from a file: its tools may not touch files)}"#, ), - C::new("model.save", "Save Model", "Analyze › Geoprocessing", save_model).roles(&[crate::ParamRole { key: "graph", role: crate::Role::ModelGraph }]) + C::new("model.save", "Save Model", "Analyze › Geoprocessing", save_model).roles(&[crate::ParamRole { key: "graph", role: crate::Role::ModelGraph }]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)) .params(r#"{"name": model (made if new), "graph": graph (as model.new), "rename"?: new name, "imported"?: bool (from a file: its tools may not touch files; a model from a file stays one)}"#), - C::new("model.run", "Run Model", "Analyze › Geoprocessing", run_model).roles(&[crate::ParamRole { key: "graph", role: crate::Role::ModelGraph }]) + C::new("model.run", "Run Model", "Analyze › Geoprocessing", run_model).roles(&[crate::ParamRole { key: "graph", role: crate::Role::ModelGraph }]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Automation)) .params(r#"{"name"?: model (default: the last), "graph"?: run this graph instead (as model.new)}"#), C::new("model.validate", "Validate Model", "Analyze › Geoprocessing", |s, p| { let (_, g, _) = model_graph(s, p)?; let problems = crate::model::validate(s, &g); Ok(json!({"ok": problems.is_empty(), "problems": problems})) - }).roles(&[crate::ParamRole { key: "graph", role: crate::Role::ModelGraph }]) + }).roles(&[crate::ParamRole { key: "graph", role: crate::Role::ModelGraph }]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"name"?: model (default: the last), "graph"?: graph}"#), - C::new("model.export", "Export Model", "Analyze › Geoprocessing", export_model).roles(&[crate::ParamRole { key: "graph", role: crate::Role::ModelGraph }]) + C::new("model.export", "Export Model", "Analyze › Geoprocessing", export_model).roles(&[crate::ParamRole { key: "graph", role: crate::Role::ModelGraph }]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"name"?: model (default: the last), "graph"?: export this graph instead, "path"?: "*.model.json" (else the file's text is returned)}"#), C::new("model.remove", "Delete Model", "Browser", |s, p| { @@ -41,33 +41,33 @@ pub fn commands() -> Vec { s.project.models.retain(|m| !m.name.eq_ignore_ascii_case(n)); s.trusted_models.remove(&n.to_ascii_lowercase()); if s.project.models.len() == before { Err(format!("no model '{n}'")) } else { Ok(Value::Null) } - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": model}"#), - C::new("script.run", "Run Script", "Analyze › Geoprocessing", run_script).roles(&[crate::ParamRole { key: "script", role: crate::Role::Script }]) + C::new("script.run", "Run Script", "Analyze › Geoprocessing", run_script).roles(&[crate::ParamRole { key: "script", role: crate::Role::Script }]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Automation)) .params(r#"{"script": "one command per line: id {json}; # comments"}"#), - C::new("raster.function", "Raster Functions", "Analyze › Raster", raster_function).roles(&[]).params( + C::new("raster.function", "Raster Functions", "Analyze › Raster", raster_function).inputs(function_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layer": raster, "function": "slope"|"aspect"|"hillshade"|"ndvi"|"bandMath"|"remap"|"threshold"|"resample", "band"?: 1-based, "expression"?: "(b4-b3)/(b4+b3)", "ranges"?: [[lo, hi, value]], "value"?: number, "keep"?: bool (remap: keep unmatched cells), "nir"?, "red"?: band numbers (ndvi; default 4 and 3), "azimuth"?, "altitude"?, "zFactor"?, "target"?: raster (resample onto its grid), "name"?: output, "ran"?: (internal: a raster the app has worked out)}"#, ), - C::new("raster.functionChain", "Function Editor", "Analyze › Raster", function_chain).roles(&[]) + C::new("raster.functionChain", "Function Editor", "Analyze › Raster", function_chain).inputs(chain_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"layer": raster, "steps": [{"function", …}], "name"?: output, "ran"?: (internal: a raster the app has worked out)}"#), - C::new("analysis.suitability", "Suitability Modeler", "Analyze › Workflows", suitability).roles(&[]).params( + C::new("analysis.suitability", "Suitability Modeler", "Analyze › Workflows", suitability).inputs(suitability_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"criteria": [{"layer": raster, "band"?: 1-based, "ranges": [[lo, hi, score]], "weight": number}], "name"?: output, "ran"?: (internal: a raster the app has worked out)}"#, ), - C::new("analysis.viewshed", "Visibility Analysis", "Analyze › Workflows", viewshed).roles(&[]) + C::new("analysis.viewshed", "Visibility Analysis", "Analyze › Workflows", viewshed).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"layer": elevation raster, "lon", "lat", "band"?: 1-based, "height"?: observer metres, "radius"?: metres, "name"?: output, "ran"?: (internal: a viewshed the app has worked out)}"#), - C::new("analysis.lineOfSight", "Line Of Sight", "Analyze › Workflows", line_of_sight).roles(&[]).params( + C::new("analysis.lineOfSight", "Line Of Sight", "Analyze › Workflows", line_of_sight).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layer": elevation raster, "from": [lon, lat], "to": [lon, lat], "band"?: 1-based, "observerHeight"?: m, "targetHeight"?: m, "name"?: output}"#, ), - C::new("analysis.interpolate", "Geostatistical Wizard", "Analyze › Workflows", interpolate).roles(&[]).params( + C::new("analysis.interpolate", "Geostatistical Wizard", "Analyze › Workflows", interpolate).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layer": points, "field": numeric field, "cellSize"?: degrees, "power"?: 2, "neighbors"?: 12, "name"?: output, "ran"?: (internal: a surface the app has worked out)}"#, ), - C::new("analysis.route", "Network Analysis", "Analyze › Workflows", route).roles(&[]).params( + C::new("analysis.route", "Network Analysis", "Analyze › Workflows", route).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layer": line network, "from": [lon, lat], "to": [lon, lat], "costField"?: field, "snapDistance"?: metres, "name"?: output}"#, ), - C::new("analysis.neighborhood", "Neighborhood Explorer", "Analyze › Workflows", neighborhood).roles(&[]) + C::new("analysis.neighborhood", "Neighborhood Explorer", "Analyze › Workflows", neighborhood).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"lon", "lat", "radius": metres, "layers"?: [names], "select"?: bool}"#), - C::new("analysis.simulate", "Simulation", "Analyze › Workflows", simulate).roles(&[]).params( + C::new("analysis.simulate", "Simulation", "Analyze › Workflows", simulate).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layer": polygons, "count"?: points per polygon, "seed"?: number, "field"?: numeric field (count per polygon from it, scaled by "rate"), "rate"?: number, "name"?: output}"#, ), C::new("data.profile", "Data Engineering", "Analyze › Workflows", |s, p| { @@ -77,15 +77,15 @@ pub fn commands() -> Vec { .map(|f| json!({"field": f.name, "type": f.ty, "count": f.count, "nulls": f.nulls, "distinct": f.distinct, "truncated": f.truncated, "min": f.min, "max": f.max, "mean": f.mean, "top": f.top})) .collect(); Ok(json!({"layer": ds.name, "features": ds.features.len(), "fields": rows})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"layer"?}"#), - C::new("data.explore", "Exploratory Analysis", "Analyze › Workflows", explore).roles(&[]) + C::new("data.explore", "Exploratory Analysis", "Analyze › Workflows", explore).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"layer"?, "fields"?: [numeric fields], "bins"?: number}"#), - C::new("data.similar", "Embeddings", "Analyze › Workflows", similar).roles(&[]) + C::new("data.similar", "Embeddings", "Analyze › Workflows", similar).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"layer"?, "row"?: number (the selected feature when omitted), "fields"?: [numeric fields], "count"?: number, "select"?: bool}"#), - C::new("data.convert", "Data Interop", "Analyze › Workflows", convert).roles(&[]) + C::new("data.convert", "Data Interop", "Analyze › Workflows", convert).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"layer"?, "format": "geojson"|"csv"|"kml"|"gpx"|"wkt", "path"?: file (the text is returned without one), "nameField"?: field}"#), ] @@ -129,6 +129,7 @@ pub struct Made { then: Option, /// Made on another thread: the command's name and the map it was started on. ran: Option<(&'static str, u64)>, + pub(crate) continuation: Option, } /// A workflow's work once its inputs are read: it reads nothing more of the session, so it runs on @@ -149,7 +150,7 @@ pub(crate) fn made(raster: Raster, extra: Option) -> Result /// A layer made (styled, say), to be added as `name`. pub(crate) fn made_layer(name: String, layer: rustgis_map::RasterLayer, extra: Option, then: Option) -> Made { - Made { name, layer, extra, then, ran: None } + Made { name, layer, extra, then, ran: None, continuation: None } } /// Adds a made raster as a layer: the command's answer is the layer's (its id, size and bands) and @@ -205,13 +206,18 @@ pub(crate) fn made_or_ran(s: &mut Session, p: &Value, command: &str) -> CmdResul if s.raster_results[at].1.ran.is_some_and(|(made_by, _)| made_by != name) { return Err(format!("that run made another command's raster, not {name}'s")); } + let c = s.raster_results[at].1.continuation.clone().ok_or("the raster has no initiating authority")?; + s.continuation_owner(&c)?; let (_, m) = s.raster_results.remove(at); - return add_made(s, m); + return s.resume(&c, |s| add_made(s, m))?; } + let owner = s.continuation()?; + owner.check()?; let made = work(s, p)?; // Where a person at a terminal can stop it with Ctrl-C: no work cap, as on the app's thread. - let made = - if s.stoppable { rustgis_analysis::run_under(name, &rustgis_analysis::control::Control::stoppable(), made)? } else { made()? }; + let control = if s.stoppable { rustgis_analysis::control::Control::stoppable() } else { rustgis_analysis::control::Control::new() }; + let made = rustgis_analysis::run_under(name, &control, made)?; + s.check_continuation(&owner)?; add_made(s, made) } @@ -227,6 +233,7 @@ pub struct RasterRun { work: Work, /// The map it started on, the one its raster is added to. map: u64, + continuation: crate::policy::Continuation, } #[cfg(not(target_arch = "wasm32"))] @@ -234,11 +241,16 @@ impl RasterRun { /// Makes the raster, blocking until it's made; `control` stops it and counts its progress (see /// [`rustgis_analysis::control`]). pub fn run(self, control: &std::sync::Arc) -> Result { - let RasterRun { name, work, map, .. } = self; + let RasterRun { name, work, map, continuation, .. } = self; + continuation.check()?; + let current = continuation.clone(); + let still = std::sync::Arc::new(move || current.check().is_ok()) as crate::render::rustgis_tiles::cache::Still; // A bug in the work, as one in a tool, must not take the app down. - std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| rustgis_analysis::run_under(name, control, work))) - .unwrap_or_else(|_| Err(format!("{name} failed unexpectedly"))) - .map(|m| Made { ran: Some((name, map)), ..m }) + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + crate::render::rustgis_tiles::cache::with_still(still, || rustgis_analysis::run_under(name, control, work)) + })) + .unwrap_or_else(|_| Err(format!("{name} failed unexpectedly"))) + .map(|m| Made { ran: Some((name, map)), continuation: Some(continuation), ..m }) } } @@ -247,9 +259,21 @@ impl RasterRun { /// it would run, to make its raster on another thread: its parameters checked and its inputs read /// now. The run belongs to this project: [`raster_land`] refuses it once another project is open. pub fn raster_start(s: &mut Session, command: &str, p: &Value) -> Result { + s.start_raster(command, p) +} + +/// Raw preparation requires the engine's admission token and retains its initiating scope. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn raster_start_raw(s: &mut Session, op: crate::policy::Authorized) -> Result { + let crate::policy::RasterOp { spec, params } = op.op(); + let (command, p) = (spec.id, params); + let continuation = s.continuation()?; let &(_, name, start) = IN_BACKGROUND.iter().find(|(c, ..)| *c == command).ok_or_else(|| format!("{command} doesn't run in the background"))?; - let work = start(s, p)?; + continuation.check()?; + let current = continuation.clone(); + let still = std::sync::Arc::new(move || current.check().is_ok()); + let work = crate::render::rustgis_tiles::cache::with_still(still, || start(s, p))?; s.next_pending = s.next_pending.wrapping_add(1); let id = s.next_pending; // One runs at a time from the app; a few are kept, apart from the downloads' list, so no @@ -257,21 +281,21 @@ pub fn raster_start(s: &mut Session, command: &str, p: &Value) -> Result= 8 { s.raster_runs.remove(0); } - s.raster_runs.push(id); - Ok(RasterRun { id, name, work, map: s.map().instance_id() }) + s.raster_runs.push((id, continuation.clone())); + Ok(RasterRun { id, name, work, map: s.map().instance_id(), continuation }) } #[cfg(not(target_arch = "wasm32"))] /// Whether a run from [`raster_start`] still waits for its raster: opening or creating a project /// drops the runs of the previous one. pub fn raster_waiting(s: &Session, id: u64) -> bool { - s.raster_runs.contains(&id) + s.raster_runs.iter().any(|(n, c)| *n == id && s.check_continuation(c).is_ok()) } #[cfg(not(target_arch = "wasm32"))] /// Forgets a run from [`raster_start`] that won't land (it failed, or its thread couldn't start). pub fn raster_abandon(s: &mut Session, id: u64) { - s.raster_runs.retain(|r| *r != id); + s.raster_runs.retain(|(r, _)| *r != id); } #[cfg(not(target_arch = "wasm32"))] @@ -280,9 +304,17 @@ pub fn raster_abandon(s: &mut Session, id: u64) { /// is active than the one it started on, as for a tool run in the background. A run that failed /// is forgotten with [`raster_abandon`]. pub fn raster_land(s: &mut Session, id: u64, m: Made) -> Result { - if !raster_waiting(s, id) { + // A missing run was dropped; a present run with revoked authority or another map must + // reach its owner/current check and report the refusal, not masquerade as missing. + if !s.raster_runs.iter().any(|(n, _)| *n == id) { return Ok(false); } + let c = m.continuation.as_ref().ok_or("the raster has no initiating authority")?; + s.check_delivery(c)?; + let owner = s.raster_runs.iter().find(|(n, _)| *n == id).map(|(_, c)| c).ok_or("no pending raster with that id")?; + if !owner.same_run(c) { + return Err("this raster result belongs to another run".into()); + } raster_abandon(s, id); if let Some((name, map)) = m.ran && map != s.map().instance_id() @@ -389,7 +421,7 @@ pub fn gallery_list(kind: &str) -> CmdResult { }; let mut out: Vec = ids .iter() - .filter_map(|id| rustgis_analysis::tool(id)) + .filter_map(|id| rustgis_analysis::resolve_published(id).map(|r| r.spec())) .map(|t| json!({"id": t.id, "name": t.name, "summary": t.summary, "command": "gp.run"})) .collect(); if kind == "raster" { @@ -402,7 +434,7 @@ pub fn gallery_list(kind: &str) -> CmdResult { out.push(json!({"id": id, "name": name, "summary": summary, "command": id})); } out.extend( - rustgis_analysis::tools() + rustgis_analysis::published_tools() .filter(|t| t.toolbox.to_lowercase().contains("raster")) .take(40) .map(|t| json!({"id": t.id, "name": t.name, "summary": t.summary, "command": "gp.run"})), @@ -426,7 +458,7 @@ fn new_model(s: &mut Session, p: &Value) -> CmdResult { let Some(tool) = super::gp::tool_id(&r.tool) else { continue }; let mut params = r.params.clone(); // Only layer parameters can name an earlier output. - let spec = rustgis_analysis::tool(tool); + let spec = rustgis_analysis::resolve_published(tool).map(|r| r.spec()); let is_layer = |k: &str| { spec.and_then(|t| t.params.iter().find(|p| p.name == k)).is_some_and(|p| { matches!( @@ -472,11 +504,6 @@ fn new_model(s: &mut Session, p: &Value) -> CmdResult { i += 1; name = format!("{base} {i}"); } - // History that came with an opened project is from a file too. - let file_history = b(p, "fromHistory").unwrap_or(false) && p.get("steps").is_none() && s.history_from_file > 0; - if !b(p, "imported").unwrap_or(false) && !file_history { - s.trusted_models.insert(name.to_ascii_lowercase()); - } let n = steps.len(); s.project.models.push(Model { name: name.clone(), steps, graph }); Ok(json!({"model": name, "steps": n})) @@ -484,7 +511,7 @@ fn new_model(s: &mut Session, p: &Value) -> CmdResult { /// The model a command names (or the last one), or the graph it passes: its name, graph and /// whether it may touch files (a graph passed in is the caller's own). -fn model_graph(s: &Session, p: &Value) -> Result<(String, crate::project::ModelGraph, bool), String> { +pub(crate) fn model_graph(s: &Session, p: &Value) -> Result<(String, crate::project::ModelGraph, bool), String> { if let Some(g) = p.get("graph").filter(|g| !g.is_null()) { let name = ps(p, "name").unwrap_or("Model").chars().take(200).collect(); return Ok((name, crate::model::graph_from_json(g)?, true)); @@ -497,7 +524,7 @@ fn model_graph(s: &Session, p: &Value) -> Result<(String, crate::project::ModelG Some(graph) => graph.clone(), None => crate::model::checked_steps_to_graph(&model.steps)?, }; - Ok((model.name.clone(), g, s.trusted_models.contains(&model.name.to_ascii_lowercase()))) + Ok((model.name.clone(), g, s.item_trusted("model", &model.name))) } /// Runs a model's graph, or the graph passed: taking the tools the app ran ahead @@ -525,7 +552,7 @@ const SCRIPT_AHEAD: &str = "script.run"; /// runs here. It stops at a tool that writes files, a plugin's or GeoLibre's, and at a script's /// line that may reach outside the session ([`runs_on_a_copy`]): from there the run here runs /// them as it always has. What it says and how it ends are the run here's to say. -pub(crate) fn ahead_on_copy(s: &Session, command: &'static str, p: &Value) -> super::reads::ReadAhead { +pub(crate) fn ahead_on_copy(s: &Session, command: &'static str, p: &Value) -> super::reads::ReadWork { let name = if command == SCRIPT_AHEAD { "Run Script" } else { "Run Model" }; let (project, trusted) = (s.project.clone(), s.trusted_models.clone()); let (view_size, active_layer, p) = (s.view_size, s.active_layer, p.clone()); @@ -548,10 +575,10 @@ pub(crate) fn ahead_on_copy(s: &Session, command: &'static str, p: &Value) -> su /// A script's run on a copy of the session ([`ahead_on_copy`]), unless its first line would stop /// the copy at once, or the script has no lines or doesn't parse: then it runs here, with no copy /// made. -pub(crate) fn script_ahead(s: &Session, p: &Value) -> Option { +pub(crate) fn script_ahead(s: &Session, p: &Value) -> Option { let lines = script_commands(ps(p, "script")?).ok()?; let (_, id, params) = lines.first()?; - runs_on_a_copy(id, params).then(|| ahead_on_copy(s, SCRIPT_AHEAD, p)) + runs_on_a_copy(s, id, params).then(|| ahead_on_copy(s, SCRIPT_AHEAD, p)) } #[cfg(not(target_arch = "wasm32"))] @@ -587,9 +614,9 @@ pub(crate) const ON_A_COPY: &[&str] = &[ /// [`ON_A_COPY`], naming no file: run twice, none changes anything outside the session. Any /// other line stops the copy there, Add Data and Load All among them (they would read their /// files and servers twice). -fn runs_on_a_copy(id: &str, params: &Value) -> bool { +fn runs_on_a_copy(s: &Session, id: &str, params: &Value) -> bool { // A model's own tools are checked as they run; any other line by what its parameters name. - ON_A_COPY.contains(&id) && (id == "model.run" || !super::items::touches_files(id, params)) + ON_A_COPY.contains(&id) && (id == "model.run" || !super::items::touches_files(s, id, params)) } fn run_model_here(s: &mut Session, p: &Value) -> CmdResult { @@ -600,18 +627,34 @@ fn run_model_here(s: &mut Session, p: &Value) -> CmdResult { None => s.project.models.last(), }; if let Some(m) = model.filter(|m| m.graph.is_none()).cloned() { - return run_steps(s, m); + let trusted = s.item_trusted("model", &m.name); + if trusted { + return run_steps(s, m); + } + let name = m.name.clone(); + return s.narrowed(crate::policy::Narrow::Item { kind: "model", name }, |s| run_steps(s, m))?; } } - let (name, g, trusted) = model_graph(s, p)?; - crate::model::run(s, &name, &g, trusted) + let (name, _, trusted) = model_graph(s, p)?; + let op = s.authorize_model(p)?; + if trusted || p.get("graph").is_some_and(|g| !g.is_null()) { + crate::model::run(s, op) + } else { + s.narrowed(crate::policy::Narrow::Item { kind: "model", name }, |s| crate::model::run(s, op))? + } +} + +/// The stored source and destination names used by graph saving and its provenance check. +pub(crate) fn model_save_names(p: &Value) -> Result<(String, String), String> { + let name: String = ps(p, "name").map(str::trim).filter(|n| !n.is_empty()).ok_or("name is required")?.chars().take(200).collect(); + let new_name: String = ps(p, "rename").map(str::trim).filter(|n| !n.is_empty()).unwrap_or(&name).chars().take(200).collect(); + Ok((name, new_name)) } /// Saves a graph as a model, replacing the one of that name (or adding it). fn save_model(s: &mut Session, p: &Value) -> CmdResult { - let name: String = ps(p, "name").map(str::trim).filter(|n| !n.is_empty()).ok_or("name is required")?.chars().take(200).collect(); + let (name, new_name) = model_save_names(p)?; let g = crate::model::graph_from_json(p.get("graph").ok_or("graph is required")?)?; - let new_name: String = ps(p, "rename").map(str::trim).filter(|n| !n.is_empty()).unwrap_or(&name).chars().take(200).collect(); let i = s.project.models.iter().position(|m| m.name.eq_ignore_ascii_case(&name)); if !new_name.eq_ignore_ascii_case(&name) && s.project.models.iter().any(|m| m.name.eq_ignore_ascii_case(&new_name)) { return Err(format!("a model named '{new_name}' already exists")); @@ -619,15 +662,11 @@ fn save_model(s: &mut Session, p: &Value) -> CmdResult { let model = Model { name: new_name.clone(), steps: crate::model::graph_to_steps(&g), graph: Some(g) }; // A model that came in a file stays one when saved over (or renamed): its tools still may not // touch files. So does a graph saved as `imported`. - let was_untrusted = i.is_some() && !s.trusted_models.contains(&name.to_ascii_lowercase()); match i.and_then(|i| s.project.models.get_mut(i)) { Some(m) => *m = model, None => s.project.models.push(model), } s.trusted_models.remove(&name.to_ascii_lowercase()); - if !was_untrusted && !b(p, "imported").unwrap_or(false) { - s.trusted_models.insert(new_name.to_ascii_lowercase()); - } Ok(json!({"model": new_name})) } @@ -655,15 +694,15 @@ fn run_steps(s: &mut Session, model: Model) -> CmdResult { } // A model that came in a file (an opened project, or an imported tool) may not touch files. // Each step is checked as the `gp.run` it is, tool and all, as ModelBuilder's graphs are. - if !s.trusted_models.contains(&model.name.to_ascii_lowercase()) + if !s.item_trusted("model", &model.name) && let Some((i, st)) = model .steps .iter() .enumerate() - .find(|(_, st)| super::items::touches_files("gp.run", &json!({"tool": st.tool, "params": st.params}))) + .find(|(_, st)| super::items::touches_files(s, "gp.run", &json!({"tool": st.tool, "params": st.params}))) { return Err(format!( - "step {} ({}) reads or writes files, which a model from a file can't do; make a new model with that step to run it", + "step {} ({}) reads or writes files, uses the network or runs other software, which a model from a file can't do; make a new model with that step to run it", i + 1, st.tool )); @@ -718,11 +757,11 @@ fn run_script_here(s: &mut Session, p: &Value) -> CmdResult { let mut out = Vec::new(); for (n, id, params) in script_commands(script)? { let spec = s.registry.get(&id).ok_or_else(|| format!("line {n}: unknown command '{id}'"))?; - let (run, enabled, mutates, label) = (spec.run, spec.enabled, spec.mutates, spec.label); + let (enabled, mutates, label) = (spec.enabled, spec.mutates, spec.label); // On a copy of the session ([`ahead_on_copy`]): from a line that may reach outside it, // the run here runs the rest. #[cfg(not(target_arch = "wasm32"))] - if matches!(s.tools_ahead, Some(super::gp::Ahead::Recording(_))) && !runs_on_a_copy(&id, ¶ms) { + if matches!(s.tools_ahead, Some(super::gp::Ahead::Recording(_))) && !runs_on_a_copy(s, &id, ¶ms) { return Err(super::gp::NOT_AHEAD.into()); } // As when typed directly, a line naming a layer is checked against that layer. @@ -738,7 +777,8 @@ fn run_script_here(s: &mut Session, p: &Value) -> CmdResult { if let Some(e) = s.streamed_edit(&id, mutates, label, s.active_layer) { return Err(format!("line {n}: {e}")); } - let r = steps_of(s, |s| run(s, ¶ms)).map_err(|e| format!("line {n}: {id}: {e}"))?; + let op = s.authorize(&id, ¶ms).map_err(|e| format!("line {n} ({id}): {e}"))?; + let r = steps_of(s, |s| s.invoke(op, crate::policy::Txn::Shared)).map_err(|e| format!("line {n}: {id}: {e}"))?; out.push(json!({"line": n, "command": id, "result": r})); } Ok(json!({"ran": out})) @@ -753,7 +793,8 @@ pub(crate) fn steps_of(s: &mut Session, run: impl FnOnce(&mut Session) -> T) r.unwrap_or_else(|p| std::panic::resume_unwind(p)) } -/// The most lines a script is read to: Run reads no further, and the Script pane colours no further. +/// Maximum script lines: execution refuses longer input before any command runs. +/// The Script pane colors at most this many lines. pub const MAX_SCRIPT_LINES: usize = 10_000; /// Whether command `id` can't run in a script: scripts and notebooks (which would run scripts), and @@ -767,7 +808,10 @@ pub fn refused_in_script(id: &str) -> bool { /// a script (scripts, notebooks, `ui.*`), is an error naming its line. pub fn script_commands(script: &str) -> Result, String> { let mut out = Vec::new(); - for (n, line) in script.lines().enumerate().take(MAX_SCRIPT_LINES) { + for (n, line) in script.lines().enumerate().take(MAX_SCRIPT_LINES + 1) { + if n == MAX_SCRIPT_LINES { + return Err(format!("the script has more than {MAX_SCRIPT_LINES} lines; split it before running")); + } if let Some(parsed) = crate::script_line(line) { let (id, params) = parsed.map_err(|e| format!("line {}: {e}", n + 1))?; out.push((n + 1, id, params)); @@ -823,7 +867,7 @@ fn resample_target(s: &Session, p: &Value) -> Result rasterops::max_pixels() { return Err(format!("the target grid is larger than this computer's {} cells", rasterops::max_pixels())); } @@ -893,11 +937,10 @@ fn suitability_work(s: &Session, p: &Value) -> Result { if crit.is_empty() || crit.len() > 20 { return Err("give 1 to 20 criteria".into()); } - let first = - raster_of(s, crit.first().and_then(|c| c.get("layer")).and_then(Value::as_str).ok_or("each criterion names a raster layer")?)?; + let first = raster_of(s, criterion_layer(crit.first().ok_or("give 1 to 20 criteria")?)?)?; let mut criteria = Vec::new(); for c in crit { - let r = raster_of(s, c.get("layer").and_then(Value::as_str).ok_or("each criterion names a raster layer")?)?; + let r = raster_of(s, criterion_layer(c)?)?; let band = band_of(c, &r)?; let ranges: Vec<(f64, f64, f64)> = c .get("ranges") @@ -1238,3 +1281,51 @@ fn convert(s: &mut Session, p: &Value) -> CmdResult { } Ok(out) } + +/// Binds the extent layer, or all full-extent layers, without computing a view or loading pixels. +fn environment_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + match ps(p, "extent") { + Some("full") => Ok(super::Inputs::layers(s.map().layers.iter().map(|l| (s.map(), l)))), + Some(n) if n != "view" => super::Inputs::named(s.map(), [n]), + _ => Ok(super::Inputs::layers(std::iter::empty())), + } +} +/// Only resampling consumes a required target raster; all other functions return no target. +fn resample_target_name(p: &Value) -> Result, String> { + if ps(p, "function") == Some("resample") { + Ok(Some(ps(p, "target").ok_or("target (a raster to match) is required")?)) + } else { + Ok(None) + } +} +/// Binds the actual primary raster and conditional resampling template. +fn function_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + super::Inputs::named(s.map(), [ps(p, "layer").ok_or("layer is required")?].into_iter().chain(resample_target_name(p)?)) +} +/// Collects each function's actual target choice in step order. +fn chain_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let mut names = vec![ps(p, "layer").ok_or("layer is required")?]; + for step in p.get("steps").and_then(Value::as_array).ok_or("steps is required")? { + names.extend(resample_target_name(step)?); + } + super::Inputs::named(s.map(), names) +} +/// Parses the literal raster selector used by each suitability criterion. +fn criterion_layer(p: &Value) -> Result<&str, String> { + ps(p, "layer").ok_or_else(|| "each criterion names a raster layer".into()) +} +/// Binds the same ordered criterion raster names used by the suitability executor. +fn suitability_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let names = p + .get("criteria") + .and_then(Value::as_array) + .ok_or("criteria is required")? + .iter() + .map(criterion_layer) + .collect::, _>>()?; + super::Inputs::named(s.map(), names) +} diff --git a/crates/engine/src/cmd/workspace.rs b/crates/engine/src/cmd/workspace.rs index e237cf5760..7d9add4cf1 100644 --- a/crates/engine/src/cmd/workspace.rs +++ b/crates/engine/src/cmd/workspace.rs @@ -63,6 +63,7 @@ fn now() -> f64 { web_time::SystemTime::now().duration_since(web_time::UNIX_EPOCH).map(|d| d.as_secs_f64()).unwrap_or(0.0) } +/// Separates in-memory cache controls from commands that read or remove files on disk. pub fn commands() -> Vec { vec![ C::new("view.linkViews", "Link Views", "View › Link", |s, p| { @@ -71,19 +72,19 @@ pub fn commands() -> Vec { s.sync_linked_views(); } Ok(json!({"linked": s.project.link_views})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"on"?: bool}"#), C::new("project.thumbnail", "Create Thumbnail", "Publish › Thumbnail", |s, p| { let t = thumbnail(s, p)?; s.project.thumbnail = t; Ok(json!({"thumbnail": s.project.thumbnail.is_some()})) - }).roles(&[]) + }).inputs(thumbnail_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"create"?: bool (from the map view), "image"?: base64 PNG/JPEG, "path"?: image file, "clear"?: bool}"#), C::new("map.thumbnail", "Map Thumbnail", "Publish › Thumbnail", |s, p| { let t = thumbnail(s, p)?; s.map_mut().thumbnail = t; Ok(json!({"thumbnail": s.map().thumbnail.is_some()})) - }).roles(&[]) + }).inputs(thumbnail_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"create"?: bool, "image"?: base64 PNG/JPEG, "path"?: image file, "clear"?: bool}"#), // Workflow Manager. C::new("job.new", "Workflow Manager", "View › Windows", |s, p| { @@ -105,7 +106,7 @@ pub fn commands() -> Vec { updated: t, }); Ok(json!({"job": id})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": string, "description"?, "assigned"?, "viewExtent"?: bool (the job covers the map view)}"#), C::new("job.update", "Update Job", "View › Windows", |s, p| { let id = p.get("job").and_then(Value::as_u64).ok_or("job is required")?; @@ -125,14 +126,14 @@ pub fn commands() -> Vec { } j.updated = now(); Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"job": id, "status"?: "new"|"in progress"|"on hold"|"done", "assigned"?, "description"?}"#), C::new("job.remove", "Delete Job", "View › Windows", |s, p| { let id = p.get("job").and_then(Value::as_u64).ok_or("job is required")?; let before = s.project.jobs.len(); s.project.jobs.retain(|j| u64::from(j.id) != id); if s.project.jobs.len() == before { Err(format!("no job {id}")) } else { Ok(Value::Null) } - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"job": id}"#), // Animation. C::new("animation.add", "Add Keyframe", "View › Animation", |s, p| { @@ -144,7 +145,7 @@ pub fn commands() -> Vec { } m.animation.push(k); Ok(json!({"keyframes": m.animation.len()})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"seconds"?: time from the previous keyframe}"#), C::new("animation.remove", "Remove Keyframe", "View › Animation", |s, p| { let m = s.map_mut(); @@ -159,21 +160,21 @@ pub fn commands() -> Vec { } } Ok(json!({"keyframes": m.animation.len()})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"index"?: number (the last one when omitted), "all"?: bool}"#), C::new("animation.export", "Export Animation", "View › Animation", |s, p| { let path = ps(p, "path").ok_or("path is required (a .zip of PNG frames)")?.to_string(); let bytes = animation_zip(s, p)?; rustgis_data::io::write_atomic(std::path::Path::new(&path), &bytes).map_err(|e| format!("{path}: {e}"))?; Ok(json!({"path": path, "bytes": bytes.len()})) - }).roles(&[]) + }).inputs(map_render_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"path": "*.zip", "fps"?: 1..30, "width"?, "height"?: px}"#), // Profiles. C::new("profile.create", "Create Profile", "View › Profile Viewing", |s, p| { let pts = points(p)?; profile(s, p, &pts) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"layer": elevation raster, "points": [[lon, lat], …], "samples"?: number}"#), C::new("profile.autoComplete", "Auto-complete Profile", "View › Profile Viewing", |s, p| { @@ -191,7 +192,7 @@ pub fn commands() -> Vec { t *= 1.1; } profile(s, p, &[a, Coord::new(a.x + dx * t, a.y + dy * t)]) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"layer": elevation raster, "samples"?: number (default 200), "points": [[lon, lat], [lon, lat]] (a start and a direction)}"#), C::new("profile.crossSection", "Cross Section", "View › Profile Viewing", |s, p| { @@ -207,7 +208,7 @@ pub fn commands() -> Vec { let half_deg = f(p, "width").unwrap_or(2000.0).clamp(1.0, 1e6) / 2.0 / 111_320.0; let (nx, ny) = (-dy / len * half_deg / k, dx / len * half_deg); profile(s, p, &[Coord::new(mid.x - nx, mid.y - ny), Coord::new(mid.x + nx, mid.y + ny)]) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params( r#"{"layer": elevation raster, "samples"?: number (default 200), "points": [[lon, lat], [lon, lat]] (the line crossed), "at"?: 0..1 along it, "width"?: metres}"#, @@ -216,25 +217,25 @@ pub fn commands() -> Vec { let lines = selected_lines(s, p, false)?; let first = lines.into_iter().next().ok_or("no line")?; profile(s, p, &first) - }).roles(&[]) + }).inputs(profile_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"layer": elevation raster, "samples"?: number (default 200), "lines": line layer, "row"?: number (the first selected when omitted)}"#), C::new("profile.alongSelected", "Profile Along Selected Lines", "View › Profile Viewing", |s, p| { let lines = selected_lines(s, p, true)?; let out: Vec = lines.iter().take(50).map(|l| profile(s, p, l)).collect::>()?; Ok(Value::Array(out)) - }).roles(&[]) + }).inputs(profile_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"layer": elevation raster, "samples"?: number (default 200), "lines": line layer}"#), // Infographics and diagnostics. - C::new("info.infographic", "Infographics", "Home › Inspect", infographic).roles(&[]) + C::new("info.infographic", "Infographics", "Home › Inspect", infographic).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"layer"? (every visible feature layer when omitted)}"#), - C::new("info.diagnostics", "Diagnostic Monitor", "Help › Performance", |s, _| Ok(diagnostics(s))).roles(&[]).view(), + C::new("info.diagnostics", "Diagnostic Monitor", "Help › Performance", |s, _| Ok(diagnostics(s))).roles(&[]).effects(crate::policy::Effects::NONE).view(), // Offline areas. - C::new("offline.download", "Download Map", "Data › Offline", offline_download).roles(&[]) + C::new("offline.download", "Download Map", "Data › Offline", offline_download).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name"?: string, "extent"?: [minLon, minLat, maxLon, maxLat] (the map view when omitted)}"#), - C::new("offline.sync", "Sync", "Data › Offline", offline_sync).roles(&[]).params(r#"{"name": offline area}"#), + C::new("offline.sync", "Sync", "Data › Offline", offline_sync).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"name": offline area}"#), C::new("offline.remove", "Remove", "Data › Offline", |s, p| { let n = ps(p, "name").ok_or("name is required")?; let before = s.project.offline_areas.len(); @@ -255,7 +256,7 @@ pub fn commands() -> Vec { } } Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": offline area}"#), C::new("offline.open", "Open Offline Map", "Data › Offline", |s, p| { let n = ps(p, "name").ok_or("name is required")?; @@ -282,13 +283,13 @@ pub fn commands() -> Vec { let e = s.project.offline_areas.iter().find(|a| a.name.eq_ignore_ascii_case(n)).map(|a| a.extent).unwrap_or(Extent::EMPTY); s.zoom_to(&e); Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": offline area}"#), // Feature cache. C::new("cache.fill", "Fill Cache", "Data › Feature Cache", |s, _| { let n = fill_cache(s); Ok(json!({"cached": n})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), C::new("cache.empty", "Empty Cache", "Data › Feature Cache", |s, _| { let mut n = 0; @@ -299,17 +300,17 @@ pub fn commands() -> Vec { } } Ok(json!({"emptied": n})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), // The Shapefile index cache on disk, shared by every project (#1259). Command search, // scripts and the CLI run it; it has no ribbon button. - C::new("cache.emptyIndexes", "Empty Index Cache", "Data › Shapefile Index Cache", empty_indexes).roles(&[]).view(), + C::new("cache.emptyIndexes", "Empty Index Cache", "Data › Shapefile Index Cache", empty_indexes).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).view(), // A fill runs to the end in one go (it's quick); cancelling stops the automatic fills // that follow projection changes. C::new("cache.cancel", "Cancel Caching", "Data › Feature Cache", |s, _| { s.auto_cache = false; Ok(Value::Null) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), C::new("cache.auto", "Auto Cache", "Data › Feature Cache", |s, p| { s.auto_cache = b(p, "on").unwrap_or(!s.auto_cache); @@ -317,7 +318,7 @@ pub fn commands() -> Vec { fill_cache(s); } Ok(json!({"autoCache": s.auto_cache})) - }).roles(&[]) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"on"?: bool}"#), ] @@ -359,11 +360,27 @@ pub fn view_extent_of(m: &rustgis_map::Map, (w, h): (f64, f64)) -> Extent { ll } +/// Connects active-map rendering to the common layer and raw tile route bindings. +fn map_render_inputs(input: &super::InputContext<'_>, _: &Value) -> Result { + Ok(super::tiles::render_inputs(input.map())) +} + +/// Clearing wins; otherwise only an explicit create request asks the map renderer. +fn creates_thumbnail(p: &Value) -> bool { + !b(p, "clear").unwrap_or(false) && b(p, "create").unwrap_or(false) +} + +/// Adds renderer bindings only on the branch that creates a thumbnail from the map. +fn thumbnail_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + if creates_thumbnail(p) { map_render_inputs(input, p) } else { Ok(super::Inputs::layers(std::iter::empty())) } +} + +/// Clears, renders, or accepts a supplied picture in the same precedence as its selector. fn thumbnail(s: &mut Session, p: &Value) -> Result, String> { if b(p, "clear").unwrap_or(false) { return Ok(None); } - if b(p, "create").unwrap_or(false) { + if creates_thumbnail(p) { let png = super::share::map_png(s, Some(240.0), Some(160.0), Some(96.0))?; return Ok(Some(base64::engine::general_purpose::STANDARD.encode(png))); } @@ -410,7 +427,7 @@ fn profile(s: &Session, p: &Value, path: &[Coord]) -> CmdResult { } fn selected_lines(s: &Session, p: &Value, all: bool) -> Result>, String> { - let n = ps(p, "lines").ok_or("lines (a line layer) is required")?; + let n = profile_lines(p)?; let l = s.map().layer_by_name(n).filter(|l| l.geometry_type() == GeometryType::Polyline).ok_or_else(|| format!("no line layer '{n}'"))?; let rows: Vec = match p.get("row").and_then(Value::as_u64) { @@ -776,3 +793,14 @@ fn offline_sync(s: &mut Session, p: &Value) -> CmdResult { } Ok(json!({"updated": updated, "added": added, "deleted": deleted, "skipped": skipped, "conflicts": conflicts.len()})) } + +/// Binds the elevation raster and literal selected-line source. +fn profile_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + super::Inputs::named(s.map(), [super::imagery::raster_name(p)?, profile_lines(p)?]) +} + +/// Parses the same required line-layer name used by profile extraction. +fn profile_lines(p: &Value) -> Result<&str, String> { + ps(p, "lines").ok_or("lines (a line layer) is required".into()) +} diff --git a/crates/engine/src/cmd/zarr.rs b/crates/engine/src/cmd/zarr.rs index aa318964dc..ab5b82269f 100644 --- a/crates/engine/src/cmd/zarr.rs +++ b/crates/engine/src/cmd/zarr.rs @@ -18,16 +18,16 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("raster.zarrInfo", "Zarr Variables", "Home › Add › Add Data › Zarr", info).roles(&[]) + C::new("raster.zarrInfo", "Zarr Variables", "Home › Add › Add Data › Zarr", info).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)) .view() .params(r#"{"path": "a Zarr store's folder" | "url": "https://… a store with consolidated metadata"}"#), - C::new("zarr.series", "Time Series at a Point", "View › Time Series", series).roles(&[]).view().params( + C::new("zarr.series", "Time Series at a Point", "View › Time Series", series).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)).view().params( r#"{"lon", "lat", "layer"? (a Zarr layer: its store, variable and steps; default the active layer) | "path": folder | "url": "https://…", "variable"?, "index"?: {"level": 0} (the other dimensions' steps), "start"?, "end"?: dates (default: the layer's period, else the whole axis), "crs"?: for a projected store that doesn't say} → dates, values (null for no data) and labels along the variable's time dimension at the cell holding the point (desktop)"#, ), - C::new("zarr.reduce", "Summarise Zarr Over Time", "View › Time Series", reduce).roles(&[]).params( + C::new("zarr.reduce", "Summarise Zarr Over Time", "View › Time Series", reduce).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)).params( r#"{"layer"? (a Zarr layer: its store, variable and steps; default the active layer) | "path": folder | "url": "https://…", "variable"?, "index"?, "stat": "mean"|"min"|"max"|"sum"|"std" (the population standard deviation, n)|"count"|"countAbove", "above"?: number (countAbove's threshold), "start"?, "end"?: dates (default: the layer's period, else the whole axis), "months"?: [1–12] (a season across years: [12, 1, 2]), "minValid"?: 0–1 (a cell with a smaller share of values is left empty; default 0), "name"?: text, "crs"?} → a raster layer of the statistic (desktop)"#, ), - C::new("layer.addZarr", "Add Zarr Variable", "Home › Add › Add Data", add).roles(&[]).params( + C::new("layer.addZarr", "Add Zarr Variable", "Home › Add › Add Data", add).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)).params( r#"{"path": folder | "url": "https://…", "variable"?: "its path in the store (default: the first)", "index"?: {"time": 3, "level": 0} (other dimensions' steps, default 0), "crs"?: "EPSG:… for a store that doesn't say", "name"?: text, "time"?: bool (follow the time slider: the layer shows the step the slider is at, and turns the slider on over the variable's dates when it is off; desktop), "start"?, "end"?: dates (with time: the period to take, for an axis longer than the slider's 20,000 steps), "ran"?: (internal: a slice the app has read), "summary"?: bool (internal: with ran, a summary over time rather than a slice)}"#, ), ] @@ -93,46 +93,52 @@ fn fetch(root: &str, keys: &[String]) -> Result<(HashMap>, usize let failure: Mutex> = Mutex::new(None); let next = std::sync::atomic::AtomicUsize::new(0); let capture = rustgis_data::diagnostics::Capture::current(); + let still = crate::render::rustgis_tiles::cache::current_still(); std::thread::scope(|scope| { for _ in 0..8.min(keys.len().max(1)) { scope.spawn(|| { - let _diagnostic_scope = capture.enter(); - loop { - let i = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed); - let Some(k) = keys.get(i) else { break }; - if failure.lock().is_ok_and(|f| f.is_some()) { - break; - } - // A missing file is an expected answer here (see `is_absent`), not a failure: its 404 - // or 403 isn't recorded one by one (one note per server says the - // omission, and a store refusing everything still says "access denied"), while - // a real one (a 500, a timeout) does, with its address, status and duration. - let got = crate::render::rustgis_tiles::cache::download_blocking_omitting(&format!("{root}/{k}"), &[404, 403]); - match got { - Ok(b) => { - let len = b.len() as u64; - if total.fetch_add(len, std::sync::atomic::Ordering::Relaxed).saturating_add(len) > zarr::MAX_READ { - if let Ok(mut f) = failure.lock() { - f.get_or_insert(format!("{root}: more than {} GB to read: take a smaller part", zarr::MAX_READ >> 30)); + crate::render::rustgis_tiles::cache::with_optional_still(still.clone(), || { + let _diagnostic_scope = capture.enter(); + loop { + let i = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let Some(k) = keys.get(i) else { break }; + if failure.lock().is_ok_and(|f| f.is_some()) { + break; + } + // A missing file is an expected answer here (see `is_absent`), not a failure: its 404 + // or 403 isn't recorded one by one (one note per server says the + // omission, and a store refusing everything still says "access denied"), while + // a real one (a 500, a timeout) does, with its address, status and duration. + let got = crate::render::rustgis_tiles::cache::download_blocking_omitting(&format!("{root}/{k}"), &[404, 403]); + match got { + Ok(b) => { + let len = b.len() as u64; + if total.fetch_add(len, std::sync::atomic::Ordering::Relaxed).saturating_add(len) > zarr::MAX_READ { + if let Ok(mut f) = failure.lock() { + f.get_or_insert(format!( + "{root}: more than {} GB to read: take a smaller part", + zarr::MAX_READ >> 30 + )); + } + break; + } + if let Ok(mut o) = out.lock() { + o.insert(k.clone(), b); } - break; - } - if let Ok(mut o) = out.lock() { - o.insert(k.clone(), b); } - } - Err(e) if is_absent(&e) => { - if crate::render::rustgis_tiles::cache::http_status(&e) == Some(403) { - forbidden.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + Err(e) if is_absent(&e) => { + if crate::render::rustgis_tiles::cache::http_status(&e) == Some(403) { + forbidden.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } } - } - Err(e) => { - if let Ok(mut f) = failure.lock() { - f.get_or_insert(format!("{root}/{k}: {e}")); + Err(e) => { + if let Ok(mut f) = failure.lock() { + f.get_or_insert(format!("{root}/{k}: {e}")); + } } } } - } + }) }); } }); @@ -143,6 +149,58 @@ fn fetch(root: &str, keys: &[String]) -> Result<(HashMap>, usize Ok((out, forbidden.into_inner())) } +#[cfg(all(test, not(target_arch = "wasm32")))] +mod transport_tests { + /// Drive the real chunk-fetch scoped thread and its blocking native sender. The listening + /// socket spies on sends; no portal or remote endpoint is involved. + #[test] + fn permission_transport_zarr_chunk_thread_sends_nothing_after_revocation() { + use crate::render::rustgis_tiles::cache; + use std::{ + io::Write, + sync::{ + Arc, Mutex, + atomic::{AtomicBool, Ordering::SeqCst}, + mpsc::channel, + }, + time::Duration, + }; + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let root = format!("http://{}/synthetic.zarr", listener.local_addr().unwrap()); + let live = Arc::new(AtomicBool::new(true)); + let (entered, entry) = channel(); + let (release, released) = channel(); + let released = Mutex::new(released); + let first = AtomicBool::new(true); + let flag = live.clone(); + let still: cache::Still = Arc::new(move || { + if first.swap(false, SeqCst) { + entered.send(()).unwrap(); + released.lock().unwrap().recv().unwrap(); + } + flag.load(SeqCst) + }); + let worker = std::thread::spawn(move || cache::with_still(still, || super::fetch(&root, &["a/0.0".into()]))); + let held = entry.recv_timeout(Duration::from_secs(10)).is_ok(); + live.store(false, SeqCst); + let _ = release.send(()); + // On a broken propagation path, answer a mistakenly sent request before failing, so + // the regression doesn't leave a blocking HTTP worker behind in the test suite. + let sent = listener.accept(); + let mut sends = 0; + if let Ok((mut stream, _)) = sent { + sends += 1; + let _ = stream.write_all(b"HTTP/1.1 500 Synthetic\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"); + } + let result = worker.join().unwrap(); + assert!(held, "the real chunk worker must inherit the initiating predicate"); + assert!(result.unwrap_err().contains("cancelled")); + assert_eq!(sends, 0); + assert_eq!(listener.accept().unwrap_err().kind(), std::io::ErrorKind::WouldBlock); + } +} + /// A store's metadata from its web address. #[cfg(not(target_arch = "wasm32"))] fn store_at(url: &str) -> Result { @@ -399,26 +457,29 @@ fn read_ahead(zt: ZarrTime, i: usize) { } let ids: Vec = keys.iter().map(|(_, k)| k.clone()).collect(); let capture = rustgis_data::diagnostics::Capture::current(); + let still = crate::render::rustgis_tiles::cache::current_still(); let spawned = std::thread::Builder::new().name("zarr-read-ahead".into()).spawn(move || { - let _diagnostic_scope = capture.enter(); - let _done = Done(keys.iter().map(|(_, k)| k.clone()).collect()); - for (n, (j, k)) in keys.iter().enumerate() { - let ok = read_step(&zt, *j, k.clone()).is_ok(); - // Not read ahead again (each follow would): the slider reaching it reads it off the - // frame, and says why it fails. - if !ok && let Ok(mut f) = failed_ahead().lock() { - f.retain(|x| x != k); - f.push(k.clone()); - let excess = f.len().saturating_sub(FAILED_KEPT); - f.drain(..excess); - } - release(&keys[n..=n]); - // A layer waiting for this step off the frame takes it now. - landed(); - if !ok { - break; + crate::render::rustgis_tiles::cache::with_optional_still(still, || { + let _diagnostic_scope = capture.enter(); + let _done = Done(keys.iter().map(|(_, k)| k.clone()).collect()); + for (n, (j, k)) in keys.iter().enumerate() { + let ok = read_step(&zt, *j, k.clone()).is_ok(); + // Not read ahead again (each follow would): the slider reaching it reads it off the + // frame, and says why it fails. + if !ok && let Ok(mut f) = failed_ahead().lock() { + f.retain(|x| x != k); + f.push(k.clone()); + let excess = f.len().saturating_sub(FAILED_KEPT); + f.drain(..excess); + } + release(&keys[n..=n]); + // A layer waiting for this step off the frame takes it now. + landed(); + if !ok { + break; + } } - } + }) }); // No thread to be had: nothing is read ahead, and its keys and place are given back. if spawned.is_err() { @@ -461,19 +522,18 @@ fn read_waiting(zt: &ZarrTime, i: usize) -> Result Result, String> { + use crate::render::rustgis_tiles::cache; + let still = cache::current_still(); + cache::check_still(&still)?; if let Some(r) = kept_slice(&key) { + cache::check_still(&still)?; return Ok(r); } let r = std::sync::Arc::new(read_slice(zt, i)?.ok_or_else(|| format!("{}: step {i} has no data written", zt.store))?); - // Read now: a failure kept for it from before is stale. - #[cfg(not(target_arch = "wasm32"))] - { - if let Ok(mut f) = failed_reads().lock() { - f.retain(|(k, _)| *k != key); - } - if let Ok(mut f) = failed_ahead().lock() { - f.retain(|k| *k != key); - } + // Validate outside shared locks: a predicate may consult other runtime state. + cache::check_still(&still)?; + if let Ok(mut f) = failed_ahead().lock() { + f.retain(|k| *k != key); } if let Ok(mut c) = slices().lock() { c.retain(|(k, _)| *k != key); @@ -481,6 +541,7 @@ pub(crate) fn read_step(zt: &ZarrTime, i: usize, key: String) -> Result Res Ok(new) } -/// Steps read off the frame (see [`follow_time`]) whose read failed, by key, with why: said by -/// the next [`follow_time`], once. +/// Failed read-ahead keys retained to avoid repeatedly prefetching a missing slice. #[cfg(not(target_arch = "wasm32"))] -fn failed_reads() -> &'static std::sync::Mutex> { - static F: std::sync::OnceLock>> = std::sync::OnceLock::new(); - F.get_or_init(Default::default) +const FAILED_KEPT: usize = 16; + +/// Pending steps belong to the requesting session and live map/layer, outside shared caches. +#[cfg(not(target_arch = "wasm32"))] +#[derive(Default)] +pub(crate) struct ReadWaits { + layers: HashMap<(u64, u64), std::sync::Arc>, + shared: HashMap<(String, String, Option), std::sync::Weak>, } -/// Failed reads kept to be said (see [`failed_reads`]). #[cfg(not(target_arch = "wasm32"))] -const FAILED_KEPT: usize = 16; +impl ReadWaits { + /// Retains a move's owner while queued; layers on the same owned slice share its completion. + fn for_layer(&mut self, owner: (u64, u64), zt: &ZarrTime, i: usize) -> std::sync::Arc { + use std::sync::Arc; + if let Some(read) = self.layers.get(&owner) + && read.step == i + && read.zt.store == zt.store + && read.zt.crs == zt.crs + && read.zt.selection(i) == zt.selection(i) + { + return read.clone(); + } + let selection = (zt.store.clone(), zt.selection(i), zt.crs.clone()); + let still = crate::render::rustgis_tiles::cache::current_still(); + let read = self + .shared + .get(&selection) + .and_then(std::sync::Weak::upgrade) + .filter(|read| match (&read.still, &still) { + (Some(a), Some(b)) => Arc::ptr_eq(a, b), + (None, None) => true, + _ => false, + }) + .unwrap_or_else(|| Arc::new(PendingRead::new(zt, i, still))); + self.shared.insert(selection, Arc::downgrade(&read)); + self.layers.insert(owner, read.clone()); + read + } + + /// Drops only this session's obsolete requests; another session owns its results. + fn retain(&mut self, waiting: &std::collections::HashSet<(u64, u64)>) { + self.layers.retain(|owner, _| waiting.contains(owner)); + self.shared.retain(|_, read| read.strong_count() > 0); + } +} + +/// One requested slice, its initiating lifetime, and its terminal completion shared by its layers. +#[cfg(not(target_arch = "wasm32"))] +struct PendingRead { + zt: ZarrTime, + step: usize, + key: String, + still: Option, + capture: rustgis_data::diagnostics::Capture, + started: std::sync::atomic::AtomicBool, + result: std::sync::OnceLock, String>>, +} + +#[cfg(not(target_arch = "wasm32"))] +impl PendingRead { + /// Captures at the initial move, even when all worker slots are occupied. + fn new(zt: &ZarrTime, step: usize, still: Option) -> Self { + Self { + zt: zt.clone(), + step, + key: slice_key(zt, step), + still, + capture: rustgis_data::diagnostics::Capture::current(), + started: std::sync::atomic::AtomicBool::new(false), + result: std::sync::OnceLock::new(), + } + } + + /// Polls only this request; a frame never replaces its completion with a newly admitted read. + fn poll(self: &std::sync::Arc) -> Option, String>> { + use crate::render::rustgis_tiles::cache; + if let Err(e) = cache::check_still(&self.still) { + return Some(Err(e)); + } + if let Some(result) = self.result.get() { + return Some(cache::check_still(&self.still).and(result.clone())); + } + if self.started.load(std::sync::atomic::Ordering::SeqCst) { + return None; + } + if let Some(r) = kept_slice(&self.key) { + return Some(cache::check_still(&self.still).map(|()| r)); + } + self.start() + } + + /// Reads and catches hostile-store failures under the original lifetime and diagnostic owner. + fn read(&self) -> Result, String> { + crate::render::rustgis_tiles::cache::with_optional_still(self.still.clone(), || { + let _diagnostic_scope = self.capture.enter(); + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| read_step(&self.zt, self.step, self.key.clone()))) + .unwrap_or_else(|_| Err(format!("{}: reading step {} failed unexpectedly", self.zt.store, self.step))) + }) + } + + /// Uses the existing bounded worker pool and shared slice coalescing, retaining queued owners. + fn start(self: &std::sync::Arc) -> Option, String>> { + use std::sync::atomic::Ordering; + if OFF_FRAME_THREADS.fetch_add(1, Ordering::SeqCst) >= MAX_OFF_FRAME { + OFF_FRAME_THREADS.fetch_sub(1, Ordering::SeqCst); + return None; + } + let entered = match reading_ahead().lock() { + Ok(mut r) => r.insert(self.key.clone()), + Err(_) => { + OFF_FRAME_THREADS.fetch_sub(1, Ordering::SeqCst); + return Some(Err("Zarr read queue is unavailable".into())); + } + }; + if !entered { + OFF_FRAME_THREADS.fetch_sub(1, Ordering::SeqCst); + return None; + } + // Closure drop also returns the slot if spawning fails; every completion wakes the app. + struct Done(String); + impl Drop for Done { + fn drop(&mut self) { + if let Ok(mut r) = reading_ahead().lock() { + r.remove(&self.0); + } + OFF_FRAME_THREADS.fetch_sub(1, Ordering::SeqCst); + landed(); + } + } + self.started.store(true, Ordering::SeqCst); + let (read, done) = (self.clone(), Done(self.key.clone())); + let spawned = std::thread::Builder::new().name("zarr-step".into()).spawn(move || { + let _done = done; + let _ = read.result.set(read.read()); + }); + if spawned.is_err() { + // Native thread exhaustion retains the existing synchronous fallback, under its owner. + let result = self.read(); + let _ = self.result.set(result.clone()); + Some(result) + } else { + None + } + } +} /// The most threads reading steps off the frame at once: a step past them is started by the /// next follow, which a read landing wakes. @@ -596,54 +794,6 @@ pub(crate) fn landed_since(seen: &mut u64) -> bool { } } -/// Starts reading step `i` (kept as `key`) on a thread of its own, unless it is being read -/// already (ahead, or for another layer) or [`MAX_OFF_FRAME`] are under way: when one lands, -/// [`follow_time`] shows it or starts the next. A read that fails is kept to be said; no thread -/// to be had reads it here. -#[cfg(not(target_arch = "wasm32"))] -fn read_off_frame(zt: &ZarrTime, i: usize, key: String) -> Result<(), String> { - use std::sync::atomic::Ordering; - if OFF_FRAME_THREADS.fetch_add(1, Ordering::SeqCst) >= MAX_OFF_FRAME { - OFF_FRAME_THREADS.fetch_sub(1, Ordering::SeqCst); - return Ok(()); - } - if !reading_ahead().lock().is_ok_and(|mut r| r.insert(key.clone())) { - OFF_FRAME_THREADS.fetch_sub(1, Ordering::SeqCst); - return Ok(()); - } - // Whatever ends the read, a panic on a hostile store included, its key and its place among - // the threads are given back, and the app looks again. - struct Done(String); - impl Drop for Done { - fn drop(&mut self) { - if let Ok(mut r) = reading_ahead().lock() { - r.remove(&self.0); - } - OFF_FRAME_THREADS.fetch_sub(1, Ordering::SeqCst); - landed(); - } - } - let (zt, done) = (zt.clone(), Done(key.clone())); - let capture = rustgis_data::diagnostics::Capture::current(); - let spawned = std::thread::Builder::new().name("zarr-step".into()).spawn(move || { - let _diagnostic_scope = capture.enter(); - let _done = done; - let read = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| read_step(&zt, i, key.clone()))) - .unwrap_or_else(|_| Err(format!("{}: reading step {i} failed unexpectedly", zt.store))); - if let Err(e) = read - && let Ok(mut f) = failed_reads().lock() - { - f.retain(|(k, _)| *k != key); - f.push((key, e)); - // The last few, while a layer still waits for the step (`follow` drops the rest). - let excess = f.len().saturating_sub(FAILED_KEPT); - f.drain(..excess); - } - }); - // No thread: its key was given back with the closure; the step is read here instead. - spawned.map(|_| ()).map_err(|e| e.to_string()) -} - /// Layers that follow the time slider read the step it shows, in every map: the last of their /// steps in the slider's window, or none (the layer draws nothing) when no step falls in it. A /// web store's next steps are then read ahead in the background, so playing doesn't wait for @@ -656,7 +806,13 @@ pub(crate) fn follow_time(s: &mut Session) { // Only the user's own move, a command of its own: an agent, a model's or a task's step, a // notebook, a script's line and a plugin's call wait for the step, as their next command may // read it. - let wait = !s.zarr_off_frame || s.actor.is_some() || s.depth > 1 || s.task_running || s.notebook_running || s.steps_running > 0; + let wait = !s.zarr_off_frame + || !s.policy_user() + || s.actor.is_some() + || s.depth > 1 + || s.task_running + || s.notebook_running + || s.steps_running > 0; follow(s, wait); } @@ -671,17 +827,16 @@ fn follow(s: &mut Session, wait: bool) { #[cfg(not(target_arch = "wasm32"))] { let mut errors = Vec::new(); - // A worker may finish between layers of the same slice. Match every layer against - // one completion batch so the failure is reported once with all affected layers. - let failures = if wait { Vec::new() } else { failed_reads().lock().map(|f| f.clone()).unwrap_or_default() }; - let mut ahead: Vec<(ZarrTime, usize)> = Vec::new(); - let mut said_failed: Vec = Vec::new(); - // The steps layers wait for off the frame: a failure kept for any other is dropped, so - // the slider coming back to its step reads it again. - let mut waiting: Vec = Vec::new(); + let mut ahead = Vec::new(); + let mut waiting = std::collections::HashSet::new(); + // Observe each shared completion once, even when it is still pending. A worker finishing + // between two layers must not split one counted failure across successive frames. + let mut observed = HashMap::new(); for m in &mut s.project.maps { + let map = m.instance_id(); let window = m.time.as_ref().map(rustgis_map::time::TimeSlider::window); for l in &mut m.layers { + let owner = (map, l.instance_id()); let name = l.name.clone(); let Some(rl) = l.raster.as_mut() else { continue }; let Some(zt) = rl.time_zarr.as_mut() else { continue }; @@ -696,50 +851,29 @@ fn follow(s: &mut Session, wait: bool) { rl.data = None; zt.shown = None; } - // Off the frame: a step kept is shown now (looked up once), one that failed says - // why, and any other is read on a thread of its own while the layer shows the - // step before. + // Consume this layer's original completion, including its lifetime at landing. Some(i) if !wait => { - let key = slice_key(zt, i); - if let Some(r) = kept_slice(&key) { - rl.data = Some(r); - zt.shown = Some(i); - if is_web(&zt.store) { - ahead.push((zt.clone(), i)); + let read = s.zarr_reads.for_layer(owner, zt, i); + let result = observed.entry(std::sync::Arc::as_ptr(&read)).or_insert_with(|| read.poll()).clone(); + // The shared observation does not replace the lifetime check at delivery. + let result = result.map(|r| crate::render::rustgis_tiles::cache::check_still(&read.still).and(r)); + match result { + Some(Ok(r)) => { + rl.data = Some(r); + zt.shown = Some(i); + if is_web(&zt.store) { + ahead.push((zt.clone(), i, read.still.clone())); + } } - continue; - } - // Said for every layer on that step, then dropped (below). - let failed = failures.iter().find(|(k, _)| *k == key).map(|(_, e)| e.clone()); - if failed.is_some() { - said_failed.push(key.clone()); - } - match failed { - Some(e) => { + Some(Err(e)) => { rl.data = None; zt.shown = Some(i); errors.push((name, e)); } None => { - waiting.push(key.clone()); - // A web store's next steps too, as when the step is read here. + waiting.insert(owner); if is_web(&zt.store) { - ahead.push((zt.clone(), i)); - } - if let Err(e) = read_off_frame(zt, i, key) { - // No thread to read it on: read here, as a script does. - log::warn!("{name}: reading step {i} here: {e}"); - match read_waiting(zt, i) { - Ok(r) => { - rl.data = Some(r); - zt.shown = Some(i); - } - Err(e) => { - rl.data = None; - zt.shown = Some(i); - errors.push((name, e)); - } - } + ahead.push((zt.clone(), i, read.still.clone())); } } } @@ -749,7 +883,7 @@ fn follow(s: &mut Session, wait: bool) { rl.data = Some(r); zt.shown = Some(i); if is_web(&zt.store) { - ahead.push((zt.clone(), i)); + ahead.push((zt.clone(), i, crate::render::rustgis_tiles::cache::current_still())); } } Err(e) => { @@ -761,9 +895,8 @@ fn follow(s: &mut Session, wait: bool) { } } } - if !wait && let Ok(mut f) = failed_reads().lock() { - f.retain(|(k, _)| waiting.contains(k) && !said_failed.contains(k)); - } + // Only this session's obsolete requests are dropped; another session owns its results. + s.zarr_reads.retain(&waiting); // One notice for each cause, naming the layers it emptied (with a count past one). let mut causes: Vec<(String, Vec)> = Vec::new(); for (name, e) in errors { @@ -780,8 +913,8 @@ fn follow(s: &mut Session, wait: bool) { }; s.notify(text, true); } - for (zt, i) in ahead { - read_ahead(zt, i); + for (zt, i, still) in ahead { + crate::render::rustgis_tiles::cache::with_optional_still(still, || read_ahead(zt, i)); } } #[cfg(target_arch = "wasm32")] diff --git a/crates/engine/src/connections.rs b/crates/engine/src/connections.rs index 76d848e000..4c3639f50a 100644 --- a/crates/engine/src/connections.rs +++ b/crates/engine/src/connections.rs @@ -755,6 +755,11 @@ impl DatabaseLogins { self.selected.iter().find(|((k, d), _)| *k == kind && d == key).map(|(_, id)| id.as_str()) } + /// Current web-service choices, including choices whose login is presently absent. + pub(crate) fn web_selections(&self) -> Vec<(&str, &str)> { + self.selected.iter().filter(|((kind, _), _)| *kind == WEB).map(|((_, origin), id)| (origin.as_str(), id.as_str())).collect() + } + pub fn select(&mut self, kind: &'static str, key: &str, id: &str) { self.selected.insert((kind, key.to_string()), id.to_string()); } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 7941d0f85f..1e1dc255aa 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -11,6 +11,7 @@ pub mod durable; pub mod geometry_calc; pub mod model; pub mod peer; +pub mod policy; #[cfg(not(target_arch = "wasm32"))] pub mod prefs; pub mod project; @@ -44,6 +45,18 @@ use std::path::PathBuf; use rustgis_map::{Camera, Map}; use serde_json::Value; +/// Opaque deferred request. Reading its parameters does not claim or widen its authority. +pub struct UrlRequest { + key: u64, + params: Value, + authority: std::sync::Arc, +} +impl UrlRequest { + pub fn params(&self) -> &Value { + &self.params + } +} + pub type CmdResult = Result; /// A download that landed read already: its layer, and what reading it skipped or noticed. @@ -65,9 +78,14 @@ pub struct CommandSpec { pub mutates: bool, /// `None` = enabled; `Some(reason)` = disabled. pub enabled: fn(&Session) -> Option<&'static str>, - pub run: fn(&mut Session, &Value) -> CmdResult, + pub run: policy::Handler, + /// Reviewed reach, independently consumed by the engine policy. + pub effects: policy::Effects, /// Reviewed builtin durable-output roles. Extension parameters remain opaque. pub(crate) roles: Option<&'static [ParamRole]>, + /// Pure executor-owned resolution for commands whose inputs exceed the common layer selector. + pub(crate) transition: Option, &Value) -> Result, String>>, + pub(crate) inputs: Option, &Value) -> Result>, } fn always(_: &Session) -> Option<&'static str> { @@ -87,13 +105,44 @@ impl CommandSpec { } pub const fn new(id: &'static str, label: &'static str, location: &'static str, run: fn(&mut Session, &Value) -> CmdResult) -> Self { - CommandSpec { id, label, location, shortcut: "", params: "{}", mutates: true, enabled: always, run, roles: None } + CommandSpec { + id, + label, + location, + shortcut: "", + params: "{}", + mutates: true, + enabled: always, + run: policy::Handler::new(run), + effects: policy::Effects::UNREVIEWED, + roles: None, + inputs: None, + transition: None, + } + } + /// Declares the effects reviewed for this command; omitted declarations fail closed. + pub const fn effects(mut self, effects: policy::Effects) -> Self { + self.effects = effects; + self } /// Declares builtin durable-output roles; an empty list uses reviewed common parameter rules. pub(crate) const fn roles(mut self, roles: &'static [ParamRole]) -> Self { self.roles = Some(roles); self } + /// Shares a command owner's actual selectors with exact approval; never performs I/O. + pub(crate) const fn inputs(mut self, inputs: fn(&cmd::InputContext<'_>, &Value) -> Result) -> Self { + self.inputs = Some(inputs); + self + } + /// Declares a pure scalar selector transition; exact Ask refuses an unmodelled transition. + pub(crate) const fn transition( + mut self, + transition: fn(&cmd::InputContext<'_>, &Value) -> Result, String>, + ) -> Self { + self.transition = Some(transition); + self + } pub const fn key(mut self, s: &'static str) -> Self { self.shortcut = s; self @@ -505,7 +554,8 @@ struct ProjectFiles { /// An undo or redo step: the project before it, the layout open then, its label, who made it, /// and which state of the project it is ([`Session::dirty`]). File origins accompany changes /// made by the command only: undoing an ordinary edit must not undo a subsequent Save As. -type Step = (Project, Option, &'static str, Option, u64, Option); +type Step = + (Project, Option, &'static str, Option, u64, Option, (policy::Trust, policy::Trust, policy::Trust)); /// What a project holds in the session beside itself: what [`Session::clear_history`] forgets /// when another project starts, and what opening one sets (its file, layer, tool…). When another @@ -526,9 +576,9 @@ struct PutAside { history_projection: String, state: u64, saved: Option, - trusted_tasks: std::collections::HashSet, - trusted_models: std::collections::HashSet, - trusted_notebooks: std::collections::HashSet, + trusted_tasks: policy::Trust, + trusted_models: policy::Trust, + trusted_notebooks: policy::Trust, project_starts: u64, history_from_file: usize, active_layer: Option, @@ -550,12 +600,13 @@ struct PutAside { pending: Vec, file_outputs: Vec, #[cfg(feature = "geolibre")] - started: Vec<(u64, rustgis_geolibre::Job, serde_json::Value, u64)>, + started: Vec<(u64, rustgis_geolibre::Job, serde_json::Value, u64, policy::Continuation)>, plugin_runs: Vec, downloading: Vec, downloaded: Vec<(u64, Vec)>, + url_requests: Vec, + policy_work: policy::downloads::Work, landed_reads: Vec<(u64, LandedRead)>, - url_requests: Vec, plugin_retries: Vec, url_waits: cmd::plugins::fetch::UrlWaits, /// A style of the project still loading, which mustn't move the next one's view. @@ -564,11 +615,12 @@ struct PutAside { sql_results: Vec<(u64, String, Option, rustgis_data::source::duckdb::Output)>, trend_results: Vec<(u64, Vec, usize)>, raster_results: Vec<(u64, cmd::workflows::Made)>, - raster_runs: Vec, + raster_runs: Vec<(u64, policy::Continuation)>, edit_counts: std::collections::HashMap, } pub struct Session { + policy: policy::State, pub project: Project, pub path: Option, /// What was run in this session (#797), whatever project was open: kept in memory only, so @@ -676,12 +728,12 @@ pub struct Session { pub active_layout: Option, /// Tasks made in this session (lower-cased names). Others came from files, and their steps /// may not touch files when run. - pub trusted_tasks: std::collections::HashSet, + trusted_tasks: policy::Trust, /// A task is executing: its steps may not start another task, even through scripts. /// Kept across project replacement until the active call returns; never part of undo. pub(crate) task_running: bool, /// Models made in this session (lower-cased names); the steps of others may not touch files. - pub trusted_models: std::collections::HashSet, + trusted_models: policy::Trust, /// What the last model run did, node by node (`model::RunReport`), kept even when the run /// failed and the project was restored, so ModelBuilder can show which node stopped it. pub model_run: Option, @@ -689,7 +741,7 @@ pub struct Session { /// the project (ModelBuilder's canvas) drop it when this changes. pub project_starts: u64, /// Notebooks made in this session (lower-cased names); the cells of others may not touch files. - pub trusted_notebooks: std::collections::HashSet, + trusted_notebooks: policy::Trust, /// A notebook is running: it may not start another, even through a task or model (no /// unbounded recursion). pub(crate) notebook_running: bool, @@ -746,7 +798,7 @@ pub struct Session { queue_gp_files: bool, /// Asynchronous runs started and not yet complete: (id, job, resolved params, start time). #[cfg(feature = "geolibre")] - pub(crate) started: Vec<(u64, rustgis_geolibre::Job, serde_json::Value, u64)>, + pub(crate) started: Vec<(u64, rustgis_geolibre::Job, serde_json::Value, u64, policy::Continuation)>, /// Plugin tool runs started and not yet complete (`gp::plugin_start`). pub(crate) plugin_runs: Vec, /// Plugins' panels, by plugin id: the widgets' values and what the plugin last said about @@ -762,7 +814,7 @@ pub struct Session { pub(crate) landed_reads: Vec<(u64, LandedRead)>, /// Downloads a command asked for where nothing may wait (the browser): the parameters of /// `layer.addUrl` calls, for the front end to fetch and add (see [`Session::take_url_requests`]). - pub(crate) url_requests: Vec, + pub(crate) url_requests: Vec, /// Set by [`Session::request_url`] for [`Session::guarded`]: the command running changed /// nothing, it only left a request (a plugin's own answer can't claim that). request_only: bool, @@ -804,7 +856,7 @@ pub struct Session { pub(crate) raster_results: Vec<(u64, cmd::workflows::Made)>, /// The ids of the Analysis workflows running on a thread (see /// [`cmd::workflows::raster_start`]), apart from the downloads'. - pub(crate) raster_runs: Vec, + pub(crate) raster_runs: Vec<(u64, policy::Continuation)>, /// Set where nothing waits on a command but a shell (`rustgis-cli run`): long runs a command /// works out where it's called (a trend over a state from a data cube) take the app's larger /// bounds, as its panes do on a thread, and tell their progress (a share, 0 to 1, and 1 when @@ -818,9 +870,12 @@ pub struct Session { pub zarr_off_frame: bool, /// The Zarr steps landed that [`Session::follow_time_landed`] has seen. zarr_seen: u64, + /// Each off-frame Zarr step retains its initiating lifetime and private completion. + #[cfg(not(target_arch = "wasm32"))] + zarr_reads: cmd::zarr::ReadWaits, /// A read the app made on a thread of its own for the command it runs next, which takes it /// instead of reading again ([`cmd::reads`]). - pub(crate) read_ahead: Option, + pub(crate) read_ahead: Option, /// A model's tools run ahead on a copy of the session, which a model's run here takes /// ([`cmd::gp::Ahead::Taking`]), or which that copy records ([`cmd::gp::Ahead::Recording`]). #[cfg(not(target_arch = "wasm32"))] @@ -845,6 +900,7 @@ const MAX_URL_REQUESTS: usize = 16; /// A finished tool run, held until `gp.finish` applies it. pub(crate) struct PendingRun { + pub(crate) continuation: policy::Continuation, pub id: u64, pub tool: String, /// The tool's id, for the history. @@ -993,10 +1049,11 @@ impl Session { // The engine's tests never add Shapefile indexes to the user's cache (#1230). #[cfg(all(test, not(target_arch = "wasm32")))] rustgis_data::source::shp::use_temporary_index_dir(); - // Cheap: the suite's tools load the first time a tool list is needed. + // Trusted native startup publishes tool schemas before restricted metadata consumers run. #[cfg(feature = "geolibre")] rustgis_geolibre::register(); rustgis_plugins::tools::register(); + let _ = rustgis_analysis::external_tools(); // FlatGeobuf and GeoParquet by their web address are read through the tile fetchers, and // cloud-optimised GeoTIFFs' overviews for raster layers read in place. render::rustgis_tiles::remote::install(); @@ -1044,6 +1101,7 @@ impl Session { layer_clipboard: None, keep_tool: false, active_layout: None, + policy: Default::default(), trusted_tasks: Default::default(), task_running: false, trusted_models: Default::default(), @@ -1107,6 +1165,8 @@ impl Session { long_runs: None, zarr_off_frame: false, zarr_seen: 0, + #[cfg(not(target_arch = "wasm32"))] + zarr_reads: Default::default(), read_ahead: None, #[cfg(not(target_arch = "wasm32"))] tools_ahead: None, @@ -1143,7 +1203,9 @@ impl Session { self.next_pending = self.next_pending.wrapping_add(1); // A few at a time: past that many, the oldest are forgotten. if self.downloading.len() >= 64 { - self.downloading.remove(0); + let retired = self.downloading.remove(0); + self.retire_download_claim(retired, "this download was retired because 64 downloads were already pending"); + self.notify("1 pending download was retired because 64 downloads were already pending", true); } self.downloading.push(self.next_pending); self.next_pending @@ -1173,6 +1235,16 @@ impl Session { /// Takes a download off those under way: `false` when it isn't one of them (another project /// was opened meanwhile). fn landed(&mut self, id: u64) -> bool { + if self.download_owner(id).is_err() { + return false; + } + let pending = self.download_is_pending(id); + if let Err(e) = self.download_continues(id) { + if pending { + self.notify(e, true); + } + return false; + } let Some(i) = self.downloading.iter().position(|d| *d == id) else { return false }; self.downloading.remove(i); true @@ -1180,118 +1252,81 @@ impl Session { /// Forgets a download that failed. pub fn download_failed(&mut self, id: u64) { + if self.download_owner(id).is_err() { + return; + } + self.retire_download_claim(id, "this download failed before it completed"); self.downloading.retain(|d| *d != id); } /// Forgets a download the front end opens itself (a project by its address), saying whether /// it still belongs to this project: `false` when another project was opened meanwhile. pub fn download_done(&mut self, id: u64) -> bool { + if self.download_owner(id).is_err() { + return false; + } let current = self.downloading.contains(&id); - self.download_failed(id); + self.forget_download_claim(id); + self.downloading.retain(|d| *d != id); current } - /// Keeps a `layer.addUrl` request for the front end to download off the frame and add when - /// it lands (the browser, where a command may not wait): the command's answer, which says - /// so. A request made again while it waits is made once (the same address read another way, - /// as another layer or with other columns, is another request), and past a few waiting - /// requests the next is refused rather than silently dropping one already promised. - /// - /// Each request has a number (`"request"` in what the front end takes), which it hands on to - /// the command that adds the download; a plugin command waiting for it runs again with what - /// that command answered (#1202). A plugin command's call asking again for a download a - /// command waits for (clicked twice) is that one, not a second download, while what it added - /// is still there; from anywhere else, a request made again once taken is another download, as - /// Add Data From URL's is. - pub fn request_url(&mut self, p: &serde_json::Value) -> Result { - if !p.is_object() { - return Err("a download is asked for by an object: its \"url\" and how to read it".into()); - } - let url = p.get("url").and_then(serde_json::Value::as_str).unwrap_or("").to_string(); - let asked = self.asked_for(p); - let waiting = self.url_requests.iter().find(|q| cmd::plugins::fetch::same_request(q, &asked)).and_then(|q| q["request"].as_u64()); - // A number is taken only as what it was made for here: this request, waited for. Any other - // is set aside, so the request gets a download of its own, under a number no other request - // can get. (One given up comes here only from inside another command: a front end's is - // refused in `run`.) - let given = p.get("request").and_then(serde_json::Value::as_u64).filter(|n| self.url_waits.holds(*n, &asked)); - let made = self - .call_requests - .as_ref() - .and_then(|_| self.url_waits.in_flight(&asked)) - .filter(|n| !self.url_waits.taken_away(*n, &self.project)); - let number = match given.or(waiting).or(made) { - Some(n) => n, - None => { - self.next_request += 1; - self.next_request - } - }; - // A number the front end took and hands back (in the browser, a file it found must be - // downloaded whole after all) isn't being downloaded: queued again, whole, so it isn't - // probed again. - let handed_back = given.is_some_and(|n| self.url_waits.holds(n, &asked)); - // (Even with the same request queued under another number: nothing else lands this one.) - if (handed_back && waiting != Some(number)) || (waiting.is_none() && !self.url_waits.has(number)) { - if self.url_requests.len() >= MAX_URL_REQUESTS { - return Err(format!("{url}: {MAX_URL_REQUESTS} downloads are already waiting; try again when they have landed")); - } - let mut q = asked.clone(); - q["request"] = serde_json::json!(number); - if handed_back { - q["whole"] = serde_json::json!(true); - } - self.url_requests.push(q); - } - // Once per download, however many times a command asks for it in one call. - if let Some(asked_now) = self.call_requests.as_mut() - && !asked_now.iter().any(|(n, _)| *n == number) - { - asked_now.push((number, asked)); - } - self.request_only = true; - Ok(serde_json::json!({"pending": true, "url": url})) - } - - /// Says a download the front end was asked for ([`Session::request_url`]) failed before - /// anything could add it (it didn't download, or its project was closed): a plugin command - /// waiting for it runs again, and its `layer.addUrl` answers this error. `p` is the request - /// the front end took. - pub fn request_failed(&mut self, p: &serde_json::Value, error: &str) { - // Only the download its number names (the same address and import): a number given with - // another changes no wait or refusal. - if let Some(n) = p.get("request").and_then(serde_json::Value::as_u64) { - if self.url_waits.holds(n, p) { - // Its command gets this as its answer: without the address's keys. - self.url_waits.land(n, Err(sources::redact_text(error)), cmd::plugins::fetch::Added::default()); - } - // One given up won't land now: nothing left to refuse. - if self.url_waits.given_up(n, p) { - self.url_waits.refused(n); - } - } - } - /// Runs a command as [`Session::run`] does, except that the downloads `layer.addUrl` would /// wait for (on the desktop) are left to the front end, as in the browser: the app runs the /// commands of plugins this way, so a plugin adding data by address doesn't freeze it. The /// command line, scripts and the assistant run commands that wait, and see what was added. pub fn run_leaving_downloads(&mut self, id: &str, params: &serde_json::Value) -> CmdResult { + self.with_deferred_downloads(|s| s.run(id, params)) + } + + /// Restores the caller's download mode after a UI plugin invocation, including a failed one. + pub(crate) fn with_deferred_downloads(&mut self, f: impl FnOnce(&mut Self) -> T) -> T { let was = std::mem::replace(&mut self.leave_downloads, true); - let r = self.run(id, params); + let r = f(self); self.leave_downloads = was; r } - /// The `layer.addUrl` requests waiting for the front end ([`Session::request_url`]), taken. - pub fn take_url_requests(&mut self) -> Vec { - std::mem::take(&mut self.url_requests) - } - /// Takes tool files awaiting download. Metadata stays until `gp.fileDelivered` acknowledges - /// delivery, so producing bytes alone never records a successful run. + /// delivery, so producing bytes alone never records a successful run. Another caller's + /// queued files stay untouched until their owner or the native host takes them. pub fn take_gp_files(&mut self) -> Vec<(u64, String, Vec)> { - self.file_outputs.iter_mut().filter_map(|f| f.bytes.take().map(|bytes| (f.id, f.file_name.clone(), bytes))).collect() + let mut live = Vec::new(); + let mut refused: Vec<(String, Vec)> = Vec::new(); + for mut file in std::mem::take(&mut self.file_outputs) { + if self.check_delivery_owner(&file.continuation).is_err() { + self.file_outputs.push(file); + continue; + } + if let Err(e) = self.check_export_delivery(&file.continuation) { + if let Some(report) = &mut self.model_run { + report.file_delivered(file.id, Some(&e)); + } + // Redact before clipping: a partial credential must never become display text. + let clean = Self::redacted(&file.file_name); + let mut name: String = clean.as_str().chars().take(80).collect(); + if clean.as_str().chars().nth(80).is_some() { + name.push('…'); + } + let name = format!("{name:?}"); + if let Some((_, names)) = refused.iter_mut().find(|(reason, _)| reason == &e) { + names.push(name); + } else { + refused.push((e, vec![name])); + } + continue; + } + if let Some(bytes) = file.bytes.take() { + live.push((file.id, file.file_name.clone(), bytes)); + } + self.file_outputs.push(file); + } + for (reason, names) in refused { + let count = names.len(); + let names = names.join(", "); + self.notify(format!("{count} pending file export(s) could not be completed ({names}): {reason}"), true); + } + live } /// Whether background work still belongs to this project. @@ -1312,16 +1347,17 @@ impl Session { self.zarr_off_frame && cmd::zarr::waiting(self) } - /// Drops a read placed for a command ([`cmd::reads::Read::placed`]) that the command didn't - /// take: one refused before it read, or that read another way. + /// Drops the private prepared value left by a command that did not take it; owned reads + /// enter only through [`cmd::reads::read_land`]. pub fn drop_read(&mut self) { self.read_ahead = None; } /// Shows the Zarr steps read off the frame ([`Session::zarr_off_frame`]) once they land: the - /// app asks each frame. True when one landed, so the map draws again. + /// app asks each frame. Named scopes leave the completion for its native owner. True when + /// one landed, so the map draws again. pub fn follow_time_landed(&mut self) -> bool { - if !cmd::zarr::landed_since(&mut self.zarr_seen) { + if !self.policy_ingress_idle() || !cmd::zarr::landed_since(&mut self.zarr_seen) { return false; } cmd::zarr::follow_time(self); @@ -1351,6 +1387,21 @@ impl Session { /// panic restores it. A command run by itself (not as a part of another) that changes the /// project, or reaches a file or a service, goes into the session's log (#797). pub fn run(&mut self, id: &str, params: &Value) -> CmdResult { + self.command_epilogue(id, params, |s| { + let native_maintenance = s.depth == 0 && s.policy_ingress_idle(); + let result = s.run_command(id, params); + // Native sessions without a drawing loop recover their own failed overviews. + // Named callers and sealed completions do not initiate unrelated maintenance. + if native_maintenance && s.read_in_place_again() { + s.wait_in_place_reads(); + } + result + }) + } + + /// Shared command bookkeeping for ordinary dispatch and execution of an already pinned retry. + pub(crate) fn command_epilogue(&mut self, id: &str, params: &Value, run: impl FnOnce(&mut Self) -> CmdResult) -> CmdResult { + let outermost = self.depth == 0; #[cfg(not(target_arch = "wasm32"))] let _diagnostic_scope = self.requests_into.as_ref().map_or_else(|| self.diagnostics.enter(), |c| c.enter()); #[cfg(target_arch = "wasm32")] @@ -1362,7 +1413,7 @@ impl Session { let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { let notice_mark = self.notice_mark(); if self.depth > 0 { - return self.run_command(id, params); + return run(self); } // Undo and redo are logged with the step they take back or bring back. let step = match id { @@ -1374,52 +1425,7 @@ impl Session { // one entry too, as it is one step to undo. let joins = self.merge.is_some() && self.merge == self.merged && !self.undo.is_empty(); let (at, start) = (project_log::now_ms(), web_time::Instant::now()); - // A download that lands after its plugin command gave up waiting (it ran again with - // that error) isn't added: no command is left to go on with it. Nor is one landing - // again, added already (a caller gave its number itself, then the app's own download - // came) (#1202). - let refused = if matches!(id, "layer.addUrl" | "layer.addDownload") - && let Some(n) = params.get("request").and_then(Value::as_u64) - && (self.url_waits.given_up(n, params) || self.url_waits.added_already(n, params)) - { - // Its bytes or its read go too; the refusal is said as any refused command is, - // once, with its address. - if let Some(d) = params.get("id").and_then(Value::as_u64) { - self.downloaded.retain(|(k, _)| *k != d); - self.landed_reads.retain(|(k, _)| *k != d); - } - // Redacted here too: the assistant and MCP get the error as it is. - let url = sources::redact_text(params.get("url").and_then(Value::as_str).unwrap_or("download")); - Some(if self.url_waits.added_already(n, params) { - Err(format!("{url}: it was added already for its plugin command, so it wasn't added again")) - } else { - self.url_waits.refused(n); - Err(format!("{url}: it came after its plugin command stopped waiting, so it wasn't added")) - }) - } else { - None - }; - let res = match refused { - Some(refused) => refused, - None => self.run_command(id, params), - }; - // A download a plugin command left to the app, added: what it answered is kept for the - // command to run again with (#1202). - if matches!(id, "layer.addUrl" | "layer.addDownload") - && let Some(n) = params.get("request").and_then(Value::as_u64) - && self.url_waits.holds(n, params) - && !res.as_ref().is_ok_and(|v| v.get("pending").and_then(Value::as_bool) == Some(true)) - { - // What it added, to tell on the command's next run whether it is still there. - let added = cmd::plugins::fetch::Added::of(self.map(), &res); - self.url_waits.land(n, res.clone(), added); - } - // A failed overview whose wait is over is read again (the app does this each frame; - // a session that doesn't draw, the command line or Python, as its next command runs), - // waited for where the session waits for reads in place. - if self.read_in_place_again() { - self.wait_in_place_reads(); - } + let res = run(self); // Pixels the command read in place (a project, an import) say what they lost. self.notice_landed_rasters(); // A plugin's command waiting for its fetches answers `pending` and runs again when they @@ -1460,6 +1466,10 @@ impl Session { res })); self.diagnostic_source = previous; + // Every top-level invocation consumes its key, even a refusal or panic before guarded. + if outermost { + self.merge = None; + } match result { Ok(result) => result, Err(payload) => std::panic::resume_unwind(payload), @@ -1480,8 +1490,9 @@ impl Session { self.follow_style_view(); } if id.starts_with(rustgis_plugins::PREFIX) { + let op = self.authorize(id, params)?; self.merge = merge; - return self.run_plugin(id, params); + return self.invoke(op, policy::Txn::Own); } let Some(&spec) = self.registry.get(id) else { return Err(format!("unknown command '{}'", project_log::log_identifier(id))) }; // Keys the command does not read: said, so a script or an agent can correct itself. @@ -1515,29 +1526,15 @@ impl Session { if let Some(why) = blocked { return Err(format!("{}: {why}", spec.label)); } - let (run, mutates, label) = (spec.run, spec.mutates, spec.label); + let (mutates, label) = (spec.mutates, spec.label); if let Some(e) = self.streamed_edit(id, mutates, label, named.or(active)) { // Said, as a failure in `guarded` is: this returns before it (#468). self.notify(e.clone(), true); return Err(e); } + let op = self.authorize(id, params)?; self.merge = merge; - // A layer added from a saved web-service connection records it, inside the command's step. - let res = self.guarded(label, mutates, |s| { - let add = |s: &mut Session| { - let before = service_logins::before(s, id, params)?; - let out = run(s, params)?; - if let Some(before) = before { - service_logins::tag(s, params, before, &out)?; - } - Ok(out) - }; - if matches!(id, "layer.addUrl" | "layer.addDownload") && params.get("anonymous").and_then(Value::as_bool) == Some(true) { - service_logins::anonymously(s, true, add).unwrap_or_else(|_| Err(format!("{label} failed unexpectedly"))) - } else { - add(s) - } - }); + let res = self.invoke(op, policy::Txn::Own); if id == "layer.addDownload" && res.is_ok() && let Some(token) = params.get("geolibreLayer").and_then(|v| v.get("token")).and_then(serde_json::Value::as_str) @@ -1577,6 +1574,7 @@ impl Session { /// and restored if `f` fails or panics. pub(crate) fn guarded(&mut self, label: &'static str, mutates: bool, f: impl FnOnce(&mut Session) -> CmdResult) -> CmdResult { let snapshot = mutates.then(|| self.project.clone()); + let trust = (self.trusted_tasks.clone(), self.trusted_models.clone(), self.trusted_notebooks.clone()); let state = self.state; // The merge key asked for applies to this command only, whatever it does; and to a // command run by itself, not to those it runs. @@ -1597,8 +1595,9 @@ impl Session { self.request_only = false; self.quiet_failure = false; // Requests a failed command left for the front end go with its other changes. - let requests = self.url_requests.len(); - let file_outputs = self.file_outputs.len(); + let requests = self.url_request_checkpoint(); + let retry_state = (self.plugin_retries.clone(), self.url_waits.clone()); + let file_outputs: Vec<_> = self.file_outputs.iter().map(|file| file.id).collect(); let said = self.notices_given; let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(self))) .unwrap_or_else(|_| Err(format!("{label} failed unexpectedly; the project was restored"))); @@ -1649,7 +1648,7 @@ impl Session { files.identity != self.file_identity || files.path != self.path || files.opened_package != self.opened_package }); if key.is_none() || key != self.merged || self.undo.is_empty() { - self.undo.push((snap, open_layout, label, self.actor.clone(), state, files)); + self.undo.push((snap, open_layout, label, self.actor.clone(), state, files, trust.clone())); if self.undo.len() > UNDO_LIMIT { self.undo.remove(0); } @@ -1673,6 +1672,7 @@ impl Session { // The project as it was before the command, so the layout open then, whatever // the command opened on the way (a task's steps). self.project = snap; + (self.trusted_tasks, self.trusted_models, self.trusted_notebooks) = trust; self.active_layout = open_layout; // Replaced during the command: what it held beside itself (its undo history, its // trust, its unsaved state) comes back with it. @@ -1685,6 +1685,7 @@ impl Session { self.opened_package = files.opened_package; } self.opened = opened; + self.sync_policy_generation(); self.state = state; if let Some(c) = counts { self.edit_counts = c; @@ -1697,11 +1698,9 @@ impl Session { self.notices_given -= (new - errors.len()) as u64; self.notices.extend(errors); let mut recorded_failure = false; - for record in failed_history - .into_iter() - .skip(history_from) - .filter(|record| !record.ok && rustgis_analysis::tool(&record.id).is_some_and(cmd::gp::writes_files)) - { + for record in failed_history.into_iter().skip(history_from).filter(|record| { + !record.ok && rustgis_analysis::resolve_published(&record.id).is_some_and(|r| cmd::gp::writes_files(r.spec())) + }) { self.push_gp_record(record); recorded_failure = true; } @@ -1716,8 +1715,10 @@ impl Session { _ => {} } if res.is_err() { - self.url_requests.truncate(requests); - self.file_outputs.truncate(file_outputs); + self.restore_url_request_checkpoint(requests); + (self.plugin_retries, self.url_waits) = retry_state; + // Keep only still-present old identities: drained bytes and terminal refusals stay consumed. + self.file_outputs.retain(|file| file_outputs.contains(&file.id)); } let quiet = std::mem::take(&mut self.quiet_failure); if let Err(e) = &res @@ -1793,6 +1794,7 @@ impl Session { downloaded: take(&mut self.downloaded), landed_reads: take(&mut self.landed_reads), url_requests: take(&mut self.url_requests), + policy_work: self.take_policy_work(), plugin_retries: take(&mut self.plugin_retries), url_waits: take(&mut self.url_waits), style_view: self.style_view.take(), @@ -1845,6 +1847,7 @@ impl Session { downloaded, landed_reads, url_requests, + policy_work, plugin_retries, url_waits, style_view, @@ -1869,6 +1872,7 @@ impl Session { (self.pending, self.plugin_runs, self.plugin_retries, self.url_waits) = (pending, plugin_runs, plugin_retries, url_waits); self.file_outputs = file_outputs; (self.downloading, self.downloaded, self.url_requests) = (downloading, downloaded, url_requests); + self.restore_policy_work(policy_work); self.landed_reads = landed_reads; (self.trend_results, self.raster_results, self.raster_runs) = (trend_results, raster_results, raster_runs); #[cfg(feature = "geolibre")] @@ -1989,6 +1993,9 @@ impl Session { /// ([`Session::in_place_wait`]). Whether a read was started. pub fn read_in_place_again(&mut self) -> bool { use render::rustgis_tiles::cache; + if !self.policy_ingress_idle() || self.depth > 0 { + return false; + } let mut started = false; for m in &mut self.project.maps { for l in &mut m.layers { @@ -2195,9 +2202,14 @@ impl Session { pub(crate) fn undo(&mut self) -> bool { self.merged = None; - let Some((p, saved, label, actor, state, mut files)) = self.undo.pop() else { return false }; + let Some((p, saved, label, actor, state, mut files, trust)) = self.undo.pop() else { return false }; let (open, was) = (self.open_layout_name(), self.active_layout); let cur = std::mem::replace(&mut self.project, p); + let old_trust = ( + std::mem::replace(&mut self.trusted_tasks, trust.0), + std::mem::replace(&mut self.trusted_models, trust.1), + std::mem::replace(&mut self.trusted_notebooks, trust.2), + ); if let Some(files) = &mut files { std::mem::swap(&mut self.file_identity, &mut files.identity); std::mem::swap(&mut self.path, &mut files.path); @@ -2207,16 +2219,21 @@ impl Session { self.reopen_layout(open, saved); self.keep_time_position(&cur); self.keep_tile_pick(&cur); - self.redo.push((cur, was, label, actor, std::mem::replace(&mut self.state, state), files)); + self.redo.push((cur, was, label, actor, std::mem::replace(&mut self.state, state), files, old_trust)); self.touch_all_geometry(); true } pub(crate) fn redo(&mut self) -> bool { self.merged = None; - let Some((p, saved, label, actor, state, mut files)) = self.redo.pop() else { return false }; + let Some((p, saved, label, actor, state, mut files, trust)) = self.redo.pop() else { return false }; let (open, was) = (self.open_layout_name(), self.active_layout); let cur = std::mem::replace(&mut self.project, p); + let old_trust = ( + std::mem::replace(&mut self.trusted_tasks, trust.0), + std::mem::replace(&mut self.trusted_models, trust.1), + std::mem::replace(&mut self.trusted_notebooks, trust.2), + ); if let Some(files) = &mut files { std::mem::swap(&mut self.file_identity, &mut files.identity); std::mem::swap(&mut self.path, &mut files.path); @@ -2226,7 +2243,7 @@ impl Session { self.reopen_layout(open, saved); self.keep_time_position(&cur); self.keep_tile_pick(&cur); - self.undo.push((cur, was, label, actor, std::mem::replace(&mut self.state, state), files)); + self.undo.push((cur, was, label, actor, std::mem::replace(&mut self.state, state), files, old_trust)); self.touch_all_geometry(); true } @@ -2378,9 +2395,7 @@ impl Session { /// selection if it was made around the current active layer, else that layer alone (none /// when there is no active layer). Layers the map no longer has are left out. pub fn selected_layers(&self) -> Vec { - let map = self.map(); - let picked: &[u32] = if self.layer_selection_of == self.active_layer { &self.layer_selection } else { &[] }; - map.layers.iter().map(|l| l.id).filter(|id| picked.contains(id) || self.active_layer == Some(*id)).collect() + cmd::InputSource::input_context(self).selected_layers() } /// The tile layer picked in the Layers pane (`map.selectTiles`): the topmost one marked, while @@ -2460,22 +2475,7 @@ impl Session { /// Resolves a `layer` parameter (name or id), defaulting to the active layer. pub fn layer_id(&self, p: &Value) -> Result { - let map = self.map(); - match p.get("layer") { - Some(Value::Number(n)) => n - .as_u64() - .and_then(|n| u32::try_from(n).ok()) - .filter(|id| map.layer(*id).is_some()) - .ok_or_else(|| format!("no layer with id {n}")), - Some(Value::String(s)) => map - .layer_by_name(s) - .map(|l| l.id) - .or_else(|| s.parse::().ok().filter(|id| map.layer(*id).is_some())) - .ok_or_else(|| format!("no layer named '{s}'")), - _ => { - self.active_layer.filter(|id| map.layer(*id).is_some()).ok_or_else(|| "select a layer in the Layers pane first".to_string()) - } - } + cmd::InputSource::input_context(self).layer_id(p) } /// Resolves a `table` parameter to a standalone table's position in the project: by its id, diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index 74676d0268..5b207a79df 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -72,7 +72,7 @@ pub fn port_kind(k: ParamKind) -> Option<(PortKind, bool)> { /// A tool node's tool, if RustGIS has it. pub fn tool_of(node: &ModelNode) -> Option<&'static ToolSpec> { - node.tool.as_deref().and_then(crate::cmd::gp::tool_id).and_then(rustgis_analysis::tool) + node.tool.as_deref().and_then(|id| rustgis_analysis::resolve_published(id).map(|r| r.spec())) } /// The ports edges can come into. @@ -567,9 +567,10 @@ impl RunReport { /// in a file, whose tools may not read or write files. The report is kept in /// `Session::model_run`, whatever happens. A failed run discards its queued exports, so their /// nodes become errors rather than waiting for delivery. -pub fn run(s: &mut Session, name: &str, g: &ModelGraph, trusted: bool) -> Result { +pub(crate) fn run(s: &mut Session, op: crate::policy::Authorized) -> Result { + let crate::policy::ModelOp { name, graph: g } = op.op(); let mut report = RunReport { model: name.to_string(), ..Default::default() }; - let res = run_into(s, g, trusted, &mut report); + let res = run_into(s, g, &mut report); if res.is_err() { for node in report.nodes.iter_mut().filter(|node| node.status == "pending") { node.status = "error"; @@ -584,20 +585,12 @@ pub fn run(s: &mut Session, name: &str, g: &ModelGraph, trusted: bool) -> Result out } -fn run_into(s: &mut Session, g: &ModelGraph, trusted: bool, report: &mut RunReport) -> Result, String> { +/// Validate the graph and reject an imported graph's external tools before executing any node. +/// Trusted graphs retain the normal tool execution and output wiring. +fn run_into(s: &mut Session, g: &ModelGraph, report: &mut RunReport) -> Result, String> { if let Some(p) = validate(s, g).first() { return Err(p.message.clone()); } - if !trusted - && let Some(n) = g.nodes.iter().find(|n| { - n.kind == ModelNodeKind::Tool && crate::cmd::items::touches_files("gp.run", &json!({"tool": n.tool, "params": n.params})) - }) - { - return Err(format!( - "{} reads or writes files, which a model from a file can't do; add that tool again in ModelBuilder to run it", - title(n) - )); - } let order = topological_order(g).ok_or("The model contains a loop.")?; // The layer each node hands on, and the layers each tool node added (by id). let mut value: HashMap<&str, String> = HashMap::new(); @@ -933,6 +926,19 @@ mod tests { #[test] fn queued_file_does_not_hide_undo_for_prior_model_tools() { + // Other native connection tests rotate process-wide accounts. This local-byte/undo + // scenario must not acquire their account changes between producing and delivering bytes. + #[cfg(not(target_arch = "wasm32"))] + if std::env::var_os("RUSTGIS_MODEL_FILE_CHILD").is_none() { + let out = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "model::tests::queued_file_does_not_hide_undo_for_prior_model_tools", "--nocapture"]) + .env("RUSTGIS_MODEL_FILE_CHILD", "1") + .output() + .unwrap(); + assert!(out.status.success(), "{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr)); + assert!(String::from_utf8_lossy(&out.stdout).contains("1 passed")); + return; + } let mut s = session(); let depth = s.undo_depth(); let graph = json!({"nodes": [ @@ -951,7 +957,7 @@ mod tests { assert!(!s.model_run.as_ref().unwrap().ok); assert!(s.model_run.as_ref().unwrap().nodes.iter().any(|n| n.node == "file" && n.status == "pending")); let files = s.take_gp_files(); - assert_eq!(files.len(), 1); + assert_eq!(files.len(), 1, "{:?}", s.notices); assert!(files[0].2.starts_with(b"PMTiles\x03")); assert!(s.dirty()); assert_eq!(s.undo_depth(), depth + 1); @@ -1037,6 +1043,32 @@ mod tests { assert!(report.nodes.iter().all(|n| n.status != "pending")); } + /// Terminal authority refusal updates the matching model node without claiming delivery. + #[test] + fn retired_model_file_does_not_remain_pending() { + let mut s = session(); + let login = s + .run("connection.new", &json!({"kind":"wms","address":"https://model-retire.invalid/wms","auth":"header","keyName":"X-Key"})) + .unwrap()["id"] + .as_str() + .unwrap() + .to_string(); + s.run("connection.login", &json!({"id":login,"apiKey":"RGSYNTH_model_retire"})).unwrap(); + s.run("model.run", &json!({"graph":file_graph()})).unwrap(); + let id = s.file_outputs[0].id; + assert_eq!(s.model_run.as_ref().unwrap().nodes[0].status, "pending"); + s.run("connection.logout", &json!({"id":login})).unwrap(); + assert!(s.take_gp_files().is_empty()); + let report = s.model_run.as_ref().unwrap(); + assert_eq!(report.nodes[0].status, "error", "a retired export cannot wait for an impossible acknowledgement"); + assert!(!report.ok && report.error.is_some()); + assert!(report.pending_files.is_empty()); + assert!(s.project.gp_history.is_empty(), "refused bytes cannot record successful delivery"); + let before = report.clone(); + assert!(s.acknowledge_gp_file(id, None).is_err()); + assert_eq!(s.model_run.as_ref(), Some(&before)); + } + #[test] fn model_waits_for_all_downloads_and_ignores_older_run_acknowledgements() { let mut s = session(); diff --git a/crates/engine/src/peer.rs b/crates/engine/src/peer.rs index 6cb89e3502..c26f6199a1 100644 --- a/crates/engine/src/peer.rs +++ b/crates/engine/src/peer.rs @@ -415,9 +415,14 @@ impl Session { Ok((text, report)) } - /// Replaces a project with peer/import semantics, bypassing explicit-save restoration. - /// Reset clears active connections, pending work and item trust before incoming resolution. + /// Replaces a project synchronously with peer/import semantics. No command runs with User + /// authority. Reset advances generation, clears item trust, history, pending work and active + /// connections; old approvals and continuations must bind to that generation. pub fn open_peer_text(&mut self, peer: Peer, text: &str) -> Result { + self.with_scope(crate::policy::Scope::peer(), |s| s.open_peer_text_scoped(peer, text))? + } + /// The existing raw census/reset path runs synchronously under peer provenance. + fn open_peer_text_scoped(&mut self, peer: Peer, text: &str) -> Result { let mut p = Project::from_peer_raw(text)?; let exchange = self.exchange.as_ref().filter(|e| peer == Peer::Bridge && e.generation == self.opened); let raw = exchange.map(|e| e.incoming(&p)); diff --git a/crates/engine/src/policy/decide.rs b/crates/engine/src/policy/decide.rs new file mode 100644 index 0000000000..d6ec93234e --- /dev/null +++ b/crates/engine/src/policy/decide.rs @@ -0,0 +1,188 @@ +use super::{ + Effect, Effects, + plan::{Node, Op}, + scope::{AgentAccess, Kind, Scope}, +}; +use crate::Session; +use rustgis_plugins::manifest::Permission; +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub(super) enum Decision { + Allow, + Remember, + Ask, + Deny, +} +/// The one permission table. A new effect requires an explicit disposition here. +pub(super) fn decide(s: &Session, scope: &Scope, node: &Node) -> Result { + // Base access refusal supplies actionable guidance even for an attached read-only caller. + // An admitted operation still checks live settings and its captured load before any work. + let d = kind(s, &scope.kind, node); + if d == Decision::Deny { + return Err(refusal(s, &scope.kind, node)); + } + scope.check(node.effects)?; + node.current()?; + Ok(d) +} +/// Applies one table to the current principal and its narrowing intersections. +fn kind(s: &Session, k: &Kind, node: &Node) -> Decision { + use Decision::*; + use Effect::*; + match k { + Kind::User => Allow, + Kind::Peer => { + if node.effects.contains(Replaces) { + Allow + } else { + Deny + } + } + Kind::Assistant => node + .effects + .iter() + .map(|e| match e { + Edits | History => Allow, + Files | Network => Remember, + Plugin | Automation | Reserved | Replaces => Ask, + Unreviewed => Deny, + }) + .max() + .unwrap_or(Allow), + Kind::Agent { access: AgentAccess::Full, .. } => Allow, + Kind::Agent { access, users_project, actor } => node + .effects + .iter() + .map(|e| match e { + Edits => Allow, + Files | Network | Plugin => match access { + AgentAccess::Refuse => Deny, + AgentAccess::Review => Ask, + AgentAccess::Allow | AgentAccess::Full => Allow, + }, + Automation | Reserved | Unreviewed => Deny, + Replaces => { + if *users_project { + Deny + } else { + Allow + } + } + History => { + let top = match &node.op { + Op::Command(spec) if spec.id == "edit.undo" => s.undo_actor(), + _ => s.redo_actor(), + }; + if (*users_project && actor.is_none()) || actor.as_deref().is_some_and(|me| top.is_some_and(|by| by != Some(me))) { + Deny + } else { + Allow + } + } + }) + .max() + .unwrap_or(Allow), + Kind::Item { within, .. } => { + let own = node.effects.iter().any(|e| !matches!(e, Edits | History)) + || match &node.op { + Op::Tool(pin) => super::effects::imported("gp.run", &serde_json::json!({"tool":pin.spec().id,"params":node.params})), + _ => super::effects::imported(node.id(), &node.params), + }; + if own { Deny } else { kind(s, within, node) } + } + Kind::Plugin { pin, within } => { + let m = &pin.0.manifest; + let own = node.effects.iter().all(|e| match e { + Edits => m.has(Permission::Edit), + Files => { + m.has(Permission::Files) + || (node.id() == "raster.mosaic" && crate::cmd::imagery::mosaic_stays_in_project(s, &node.params)) + } + Network => m.has(Permission::Network), + Plugin => matches!(&node.op, Op::Tool(t) if t.plugin().is_some_and(|p| p.0.manifest.id == m.id)) && m.has(Permission::Edit), + Automation | Reserved | Replaces | History | Unreviewed => false, + }); + if !own { + return Deny; + } + // Approval of this plugin admits its manifest's host requests. Item restrictions + // remain an intersection; the outer caller cannot grant an imported item authority. + /// Retains imported-item restrictions inside an approved plugin manifest. + fn item(s: &Session, k: &Kind, n: &Node) -> Decision { + match k { + Kind::Item { .. } => kind(s, k, n), + Kind::Plugin { within, .. } => item(s, within, n), + _ => Allow, + } + } + item(s, within, node) + } + } +} +/// Names the strongest external effect for an approval/refusal. +pub(super) fn reason(effects: Effects) -> &'static str { + use Effect::*; + if effects.contains(Unreviewed) { + "it uses an unreviewed external runner or plugin" + } else if effects.contains(Plugin) { + "it runs a plugin's code; approve each run" + } else if effects.contains(Replaces) { + "it replaces the open project" + } else if effects.contains(Automation) || effects.contains(Reserved) { + "it is reserved to you (plugins, scripts, tasks, models and the GeoLibre runner)" + } else if effects.contains(Files) && effects.contains(Network) { + "it reads files and connects to addresses on the internet" + } else if effects.contains(Files) { + "it reads or writes files on this computer" + } else if effects.contains(Network) { + "it connects to an address on the internet" + } else { + "this operation needs your approval" + } +} +/// Explains denial without exposing private source or account equality data. +fn refusal(s: &Session, k: &Kind, n: &Node) -> String { + match k { + Kind::Item { kind, name, .. } => format!( + "{}: this {kind} '{name}' came from a file, or was made or changed by an assistant, agent or plugin, so its steps may only edit the project; recreate the step yourself to run it", + n.id() + ), + Kind::Plugin { pin, .. } => { + let m = &pin.0.manifest; + if let Op::Tool(tool) = &n.op + && tool.plugin().is_some_and(|p| p.0.manifest.id != m.id) + { + return format!("{} cannot run '{}' (another plugin's code)", m.name, n.id()); + } + let missing = if n.effects.contains(Effect::Edits) && !m.has(Permission::Edit) { + Some("'edit' permission") + } else if n.effects.contains(Effect::Files) + && n.effects.contains(Effect::Network) + && (!m.has(Permission::Files) || !m.has(Permission::Network)) + { + Some("'files' and 'network' permissions") + } else if n.effects.contains(Effect::Files) && !m.has(Permission::Files) { + Some("'files' permission") + } else if n.effects.contains(Effect::Network) && !m.has(Permission::Network) { + Some("'network' permission") + } else { + None + }; + match missing { + Some(permission) => format!("{} needs the {permission} to run '{}'", m.name, n.id()), + None => { + format!("{} cannot run '{}' (plugins can't run another plugin's code, automation or reserved actions)", m.name, n.id()) + } + } + } + _ if n.effects.contains(Effect::History) => { + let top = if n.id() == "edit.undo" { s.undo_actor() } else { s.redo_actor() }; + let whose = if top == Some(None) { "the user's" } else { "another agent's" }; + format!("an agent cannot undo or redo {whose} step; it undoes and redoes only its own top step") + } + _ => format!( + "'{}' was not run: {}. This caller needs approval or --yes; these actions are reserved to the user unless this caller has full access", + n.id(), + reason(n.effects) + ), + } +} diff --git a/crates/engine/src/policy/dispatch.rs b/crates/engine/src/policy/dispatch.rs new file mode 100644 index 0000000000..565e7c4c07 --- /dev/null +++ b/crates/engine/src/policy/dispatch.rs @@ -0,0 +1,1090 @@ +use super::plan::OutputRef; +use super::{ + Call, Node, Outcome, Pending, Permit, PluginPin, Remembered, Scope, ToolPin, + decide::{self, Decision}, + plan::{CallKind, LayerKey, Op, Plan, Target}, + scope::{Continuation, Kind, Narrow}, +}; +use crate::{CmdResult, CommandSpec, Session}; +use serde_json::{Value, json}; +use std::sync::Arc; + +pub(crate) enum Txn { + Own, + Shared, +} +/// The only key accepted by raw runners; construction is private to this module. +pub(crate) struct Authorized { + op: T, + node: Arc, +} +impl Authorized { + /// Borrows the immutable admitted operation; possession never permits replacing its node. + pub(crate) fn op(&self) -> &T { + &self.op + } +} +pub(crate) enum CommandOp { + Registered { spec: CommandSpec, params: Value }, + Plugin { cmd: rustgis_plugins::PluginCommand, pin: PluginPin, params: Value }, +} +/// A single plugin invocation held before handler dispatch. Its operation and authority are +/// engine-owned; it cannot be cloned, serialized or captured from a handler that may roll back. +pub struct PreparedCommand { + op: Authorized, + completion: super::Deferred, +} +impl PreparedCommand { + /// Checks the held invocation without consuming it or granting a replacement load authority. + pub fn is_current(&self) -> bool { + self.completion.0.check().is_ok() + } +} +pub(crate) struct ToolOp { + pub(crate) pin: ToolPin, + pub(crate) params: Value, +} +#[cfg(not(target_arch = "wasm32"))] +pub(crate) struct RasterOp { + pub(crate) spec: CommandSpec, + pub(crate) params: Value, +} +pub(crate) struct ModelOp { + pub(crate) name: String, + pub(crate) graph: crate::project::ModelGraph, +} +#[derive(Default)] +pub(crate) struct State { + scopes: Vec, + nodes: Vec>, + cursor: Option, + epoch: Arc, + pub(super) work: super::downloads::Work, +} +struct Cursor { + plan: Arc, + frames: Vec<(Vec, usize)>, + designated: Vec, + outputs: std::collections::BTreeMap, + made_layers: Vec, + made_tables: Vec, +} +impl Session { + /// Holds a plugin command at the pre-handler UI queue boundary, inheriting the current scope. + /// A handler or approved composite must run its children synchronously through normal dispatch. + pub fn prepare_command(&mut self, id: &str, params: &Value) -> Result { + if self.depth > 0 || !self.policy.nodes.is_empty() || self.policy.cursor.is_some() { + return Err("plugin commands can only be queued before handler dispatch".into()); + } + if !id.starts_with(rustgis_plugins::PREFIX) { + return Err("only plugin commands can wait in the UI command queue".into()); + } + let op = self.authorize(id, params)?; + let completion = super::Deferred(self.continuation_for(op.node.clone())?); + Ok(PreparedCommand { op, completion }) + } + /// Consumes the held invocation under its initiating owner, with ordinary command bookkeeping. + pub fn run_prepared(&mut self, prepared: PreparedCommand) -> CmdResult { + let PreparedCommand { op, completion } = prepared; + let (id, params) = match op.op() { + CommandOp::Plugin { cmd, params, .. } => (cmd.id.clone(), params.clone()), + _ => return Err("not a prepared plugin command".into()), + }; + self.with_deferred_downloads(|s| { + s.command_epilogue(&id, ¶ms, |s| { + s.check_delivery(&completion.0)?; + // Native clicks retain dynamic plugin host calls, including a second queued click + // that names the first one's output. Restricted callers keep their exact targets. + if !completion.0.native_tool_owner() && *op.node != s.command_node(&id, ¶ms)? { + return Err("the queued command changed since it was authorized".into()); + } + s.resume(&completion.0, |s| s.invoke(op, Txn::Own))? + }) + }) + } + /// Admits a file read before its picker, freezes Load All's selector, and seals its owner. + pub(crate) fn start_read(&mut self, command: &str, params: &Value) -> Result { + if self.depth > 0 || !self.policy.nodes.is_empty() || self.policy.cursor.is_some() { + return Err("background reads can only start before handler dispatch".into()); + } + if !matches!(command, "layer.addData" | "layer.loadAll" | "model.run" | "script.run") { + return Err("only Add Data, Load All, models and scripts can prepare background work".into()); + } + let mut params = params.as_object().cloned().ok_or("read parameters must be an object")?; + if command == "layer.loadAll" { + let id = self.layer_id(&Value::Object(params.clone()))?; + params.insert("layer".into(), json!(id)); + } + let mut op = self.authorize(command, &Value::Object(params))?; + self.check_read_command(&op)?; + let copy_command = matches!(command, "model.run" | "script.run") && self.policy_user(); + if copy_command { + // A model's or script's native copy may read through several tools. Bind the original + // session's runtime stores, including web logins, before that copy exists. + let mut node = (*op.node).clone(); + node.auth = Some(Arc::new(super::scope::RuntimeAccounts::capture(self)?)); + op.node = Arc::new(node); + } + let node = op.node.clone(); + let mut owner = self.continuation_for(node.clone())?; + if copy_command { + owner.accounts = self.policy_accounts(); + owner.s3 = Some(vec![crate::render::rustgis_tiles::s3::account_identity()?]); + } + owner.check()?; + let current = owner.clone(); + let still = Arc::new(move || current.check().is_ok()); + self.with_node(node, |s| crate::render::rustgis_tiles::cache::with_still(still, || crate::cmd::reads::prepare(s, op, owner))) + } + /// Lands only the original read invocation; no result can be claimed by another command. + pub(crate) fn finish_read(&mut self, read: crate::cmd::reads::Read) -> CmdResult { + let crate::cmd::reads::Read { op, owner, source, ready } = read; + let (command, params) = match op.op() { + CommandOp::Registered { spec, params } => (spec.id, params.clone()), + _ => return Err("not a registered read command".into()), + }; + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + self.with_deferred_downloads(|s| { + s.command_epilogue(command, ¶ms, |s| { + s.check_delivery(&owner)?; + if let Some(source) = &source { + let layer = s.map().layer(s.layer_id(¶ms)?).ok_or("no such layer")?; + if !layer.source.as_ref().is_some_and(|current| current.same_opening(source)) { + return Err("the live source changed since this read started".into()); + } + } + s.resume(&owner, |s| { + if !owner.native_tool_owner() && *op.node != s.command_node(command, ¶ms)? { + return Err("the read command changed since it was authorized".into()); + } + s.check_read_command(&op)?; + s.read_ahead = ready; + s.invoke(op, Txn::Own) + })? + }) + }) + })); + self.read_ahead = None; + match result { + Ok(result) => result, + Err(panic) => std::panic::resume_unwind(panic), + } + } + /// Preserves ordinary command conditions for the admitted read's explicit layer selector. + fn check_read_command(&mut self, op: &Authorized) -> Result<(), String> { + let CommandOp::Registered { spec, params } = op.op() else { return Err("not a registered read command".into()) }; + let named = self.layer_id(params).ok(); + let active = self.active_layer; + if named.is_some() { + self.active_layer = named; + } + let blocked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (spec.enabled)(self))).unwrap_or(Some("unavailable")); + self.active_layer = active; + if let Some(why) = blocked { + return Err(format!("{}: {why}", spec.label)); + } + if let Some(why) = self.streamed_edit(spec.id, spec.mutates, spec.label, named.or(active)) { + return Err(why); + } + Ok(()) + } + /// Admits an export before its preparation reads any input or creates off-thread work. + #[cfg(not(target_arch = "wasm32"))] + pub(crate) fn start_export(&mut self, command: &str, params: &Value) -> Result { + if self.depth > 0 || !self.policy.nodes.is_empty() || self.policy.cursor.is_some() { + return Err("background exports can only start before handler dispatch".into()); + } + let op = self.authorize(command, params)?; + let node = op.node.clone(); + self.with_node(node, |s| crate::cmd::exports::export_start_raw(s, op)) + } + /// Exports own their input snapshot. Native delivery may report it after a layer/map switch; + /// restricted delivery still binds the original live map and source identities. + #[cfg(not(target_arch = "wasm32"))] + pub(crate) fn finish_export(&mut self, c: &Continuation, f: impl FnOnce(&mut Session) -> T) -> Result { + self.check_export_delivery(c)?; + self.resume_checked(c, f) + } + /// Exported bytes own their snapshot; a native map switch cannot retarget their delivery. + /// Scoped delivery retains its original map, sources, accounts and live owner. + pub(crate) fn check_export_delivery(&self, c: &Continuation) -> Result<(), String> { + self.check_delivery_owner(c)?; + if c.native_tool_owner() { + self.check_continuation_accounts(c)?; + for (key, source) in &c.sources { + if key.get(self).is_some_and(|l| super::plan::private_source(l) != *source) { + return Err("the live source or account changed since this request started".into()); + } + } + Ok(()) + } else { + self.check_continuation(c) + } + } + /// A JSON file acknowledgment must already be executing as the captured owner. + pub(crate) fn gp_file_owner(&self, c: &Continuation) -> Result<(), String> { + if !self.scope().same_caller(&c.scope) { + return Err("this run belongs to another caller".into()); + } + self.check_export_delivery(c) + } + /// Distinguishes a direct native URL ingress from a handler already holding admission. + pub(super) fn policy_has_no_operation(&self) -> bool { + self.policy.nodes.is_empty() + } + /// Direct native URL requests use the same effect decision as their command ingress. + pub(super) fn request_url_authorized(&mut self, p: &Value) -> CmdResult { + let op = self.authorize("layer.addUrl", p)?; + self.with_node(op.node, |s| s.enqueue_url(p)) + } + /// The import executor's native entry admits the import before creating an internal claim. + pub(super) fn request_import_url_authorized(&mut self, p: &Value) -> CmdResult { + let op = self.authorize("project.importGeolibre", p)?; + self.with_node(op.node, |s| s.request_import_url(p)) + } + /// Native completion helpers may resume an owner only at a root ingress. + pub(crate) fn policy_ingress_idle(&self) -> bool { + self.policy.scopes.is_empty() + } + /// Only the native user scope may record executable-content provenance. + pub(crate) fn policy_user(&self) -> bool { + self.policy.scopes.last().is_none_or(|s| matches!(s.kind, Kind::User)) + } + /// Inherits the current named scope; absence is the documented trusted native convention. + fn scope(&self) -> Scope { + self.policy.scopes.last().cloned().unwrap_or_else(Scope::user) + } + /// Named untrusted ingress. Cannot be used to widen an existing scope, including on panic. + pub fn with_scope(&mut self, scope: Scope, f: impl FnOnce(&mut Session) -> T) -> Result { + if !self.policy.scopes.is_empty() { + return Err("an ingress scope cannot be nested or widened".into()); + } + scope.check(super::Effects::NONE)?; + self.in_scope(scope, f) + } + /// Restores scope depth after success, refusal or an unwinding handler panic. + fn in_scope(&mut self, scope: Scope, f: impl FnOnce(&mut Session) -> T) -> Result { + let n = self.policy.scopes.len(); + let actor = std::mem::replace(&mut self.actor, scope.actor()); + self.policy.scopes.push(scope); + let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(self))); + self.policy.scopes.truncate(n); + self.actor = actor; + match r { + Ok(r) => Ok(r), + Err(p) => std::panic::resume_unwind(p), + } + } + /// Intersects nested plugin/item authority with its initiating scope. + pub(crate) fn narrowed(&mut self, to: Narrow, f: impl FnOnce(&mut Session) -> T) -> Result { + let mut scope = self.scope(); + scope.kind = match to { + Narrow::Plugin(pin) => Kind::Plugin { pin, within: Box::new(scope.kind) }, + Narrow::Item { kind, name } => Kind::Item { kind, name, within: Box::new(scope.kind) }, + }; + self.in_scope(scope, f) + } + /// Read-only planning and the one engine decision for this call. + pub fn review(&self, scope: &Scope, call: &Call) -> Outcome { + let result = (|| { + let node = self.call_node(scope, call)?; + if node.decision(self, scope)? == Decision::Allow { + return Ok(Outcome::Allowed); + } + let plan = self.plan(scope, call)?; + let decision = plan.decision(self, scope)?; + /// Finds imports whose operations cannot be enumerated before external content is read. + fn generated(n: &Node) -> bool { + n.generated || n.children.iter().any(generated) + } + if generated(&plan.root) { + return Err(format!( + "{}; RustGIS can't show its operations before reading it; import it yourself", + if plan.root.id() == "project.importQgis" { + "it adds the files and web addresses a QGIS project names" + } else { + "it adds the files and web addresses a GeoLibre project names" + } + )); + } + let review = plan.review(decision == Decision::Remember)?; + Ok(Outcome::NeedsApproval(Pending { plan: Arc::new(plan), review: Box::new(review), request: call.request.clone() })) + })(); + result.unwrap_or_else(Outcome::Refused) + } + /// Consumes one displayed approval after checking its queued request identity. + pub fn run_permitted(&mut self, scope: Scope, permit: Permit, call: &Call) -> CmdResult { + if permit.request != call.request { + return Err("this approval belongs to another request".into()); + } + self.run_plan(scope, permit.plan, call) + } + /// Reuses consent only when the entire current operation equals the remembered plan. + pub fn run_remembered(&mut self, scope: Scope, remembered: &Remembered, call: &Call) -> CmdResult { + self.run_plan(scope, remembered.plan.clone(), call) + } + /// Replans before any effect and consumes ordered frozen nodes during execution. + fn run_plan(&mut self, scope: Scope, plan: Arc, call: &Call) -> CmdResult { + self.run_plan_with(scope, plan, call, |s| s.run_call(call)) + } + /// Shared exact-plan entry for command execution and one-use adapter capture. + fn run_plan_with( + &mut self, + scope: Scope, + plan: Arc, + call: &Call, + f: impl FnOnce(&mut Session) -> Result, + ) -> Result { + if !self.policy.scopes.is_empty() || self.policy.cursor.is_some() { + return Err("an approval cannot widen a nested scope".into()); + } + let current = self.plan(&scope, call)?; + if *plan != current { + return Err("changed since you approved it; ask again".into()); + } + // Recheck live denial at use, without turning the approval into another policy setting. + plan.decision(self, &scope)?; + let designated = plan.designated.clone(); + self.policy.cursor = Some(Cursor { + plan: plan.clone(), + frames: vec![(vec![plan.root.clone()], 0)], + designated, + outputs: Default::default(), + made_layers: Vec::new(), + made_tables: Vec::new(), + }); + let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| self.with_scope(scope, f))); + self.policy.cursor = None; + match r { + Ok(r) => r?, + Err(p) => std::panic::resume_unwind(p), + } + } + /// Captures one adapter's original authority and generation before handing it to a queue. + pub fn defer(&mut self, scope: Scope, call: &Call, permit: Option) -> Result { + if !matches!(call.kind, CallKind::Adapter { .. }) { + return Err("only an adapter can defer its completion".into()); + } + let start = |s: &mut Session| { + let node = s.call_node(&s.scope(), call)?; + let node = s.admit(node)?; + s.with_node(node, |s| s.continuation().map(super::Deferred)) + }; + match permit { + Some(p) => { + if p.request != call.request { + return Err("this approval belongs to another request".into()); + } + self.run_plan_with(scope, p.plan, call, start) + } + None => self.with_scope(scope, start)?, + } + } + /// Consumes a stored completion at its real landing; root UI delivery may resume its owner. + pub fn finish_deferred(&mut self, completion: super::Deferred, f: impl FnOnce(&mut Session) -> T) -> Result { + self.check_delivery(&completion.0)?; + if let Op::Adapter(what @ ("snapshot" | "view_map")) = &completion.0.node.op { + // These callbacks render the live map, rather than an owned export snapshot. + let call = Call::adapter(what, completion.0.node.effects, completion.0.node.params.clone()); + if *completion.0.node != self.call_node(&completion.0.scope, &call)? { + return Err("the map sources changed since this image request was approved".into()); + } + } + self.resume(&completion.0, f) + } + /// Executes a call already admitted by its ingress scope; Ask at dispatch still refuses. + pub fn run_scoped(&mut self, scope: Scope, call: &Call) -> CmdResult { + self.with_scope(scope, |s| s.run_call(call))? + } + /// Dispatches command, script or adapter vehicles without promoting their ingress. + fn run_call(&mut self, call: &Call) -> CmdResult { + match &call.kind { + CallKind::Command { id, params } => self.run(id, params), + CallKind::Script(script) => { + // The MCP vehicle is not the reserved script.run command; its lines use the seam. + let node = self.call_node(&self.scope(), call)?; + let decision = node.decision(self, &self.scope())?; + if self.policy.cursor.is_none() && decision != Decision::Allow { + return Err("this operation needs your approval".into()); + } + let node = self.admit(node)?; + self.with_node(node, |s| s.guarded("Run Script", true, |s| crate::cmd::workflows::run_script(s, &json!({"script":script})))) + } + CallKind::Adapter { .. } => { + let node = self.call_node(&self.scope(), call)?; + let decision = node.decision(self, &self.scope())?; + if self.policy.cursor.is_none() && decision != Decision::Allow { + return Err("this operation needs your approval".into()); + } + let node = self.admit(node)?; + self.with_node(node, |_| Ok(Value::Null)) + } + } + } + /// Checks live authority, ordered operation, effective params and bound targets at each stage. + fn admit(&mut self, mut node: Node) -> Result, String> { + let scope = self.scope(); + let decision = node.decision(self, &scope)?; + let current_sources = self + .project + .maps + .iter() + .flat_map(|m| m.layers.iter().map(move |l| (LayerKey::of(m, l), super::plan::private_source(l)))) + .collect::>(); + let selected = self + .selected_layers() + .into_iter() + .chain(self.active_layer) + .filter_map(|id| self.map().layer(id)) + .map(|l| LayerKey::of(self.map(), l)) + .collect::>(); + let layers = self.map().layers.iter().map(|l| (l.name.clone(), LayerKey::of(self.map(), l))).collect::>(); + let tables = self.project.tables.iter().map(|t| (t.dataset.name.clone(), t.id)).collect::>(); + let expected = self.policy.cursor.as_ref().and_then(|c| c.frames.last()).and_then(|(n, at)| n.get(*at)).cloned(); + // A composite may not replace a later item's code (or its own next cell) after review. + for n in self.policy.nodes.iter().map(Arc::as_ref).chain(expected.as_ref()) { + if let Some((kind, name, trusted, content)) = &n.item { + let graph = *kind == "model" && n.params.get("graph").is_some_and(|g| !g.is_null()); + let current = if graph { n.params.get("graph").cloned() } else { self.item_content(kind, name) }; + if current.as_ref() != Some(content) || (!graph && self.item_trusted(kind, name) != *trusted) { + return Err("the task, notebook or model changed since approval; ask again".into()); + } + } + } + let mut wanted = expected.as_ref().map(|n| n.params.clone()).unwrap_or(Value::Null); + if let Some(expected) = &expected { + for wire in &expected.wires { + let mut names = Vec::new(); + for from in &wire.from { + let from = match from { + OutputRef::Node(n) => self + .policy + .cursor + .as_ref() + .and_then(|c| c.outputs.get(n)) + .ok_or("the model's earlier node made no layer or table")?, + other => other, + }; + let name = match from { + OutputRef::ExistingLayer(key) => layers.iter().find(|(_, n)| n == key).map(|(name, _)| name), + OutputRef::ExistingTable(id) => tables.iter().find(|(_, n)| n == id).map(|(name, _)| name), + OutputRef::Node(_) => None, + } + .ok_or("the model's wired output no longer exists")?; + names.push(name.clone()); + } + wanted[&wire.key] = json!(names.join(";")); + // The wire is a data target, never an externally chosen place. + node.places.retain(|v| v.get(&wire.key).is_none()); + } + } + let opened = self.opened; + let map = self.map().instance_id(); + let accounts = self.policy_accounts(); + let s3 = crate::render::rustgis_tiles::s3::connections(); + if let Some(cursor) = &mut self.policy.cursor { + if opened != cursor.plan.opened { + return Err("the project or active map changed since approval".into()); + } + if cursor.plan.accounts != accounts || cursor.plan.s3 != s3 { + return Err("the live source or account changed since approval".into()); + } + let (nodes, at) = cursor.frames.last_mut().ok_or("the approved plan has finished")?; + let expected = nodes.get(*at).ok_or("an unplanned operation was requested; ask again")?; + if map != expected.context { + return Err("the active map does not match this approved stage".into()); + } + if expected.op != node.op || wanted != node.params || expected.effects != node.effects || expected.places != node.places { + return Err("changed since you approved it; ask again".into()); + } + for target in &expected.targets { + match target { + Target::Default { active, .. } => { + if expected + .effects + .iter() + .any(|e| matches!(e, super::Effect::Files | super::Effect::Network | super::Effect::Plugin)) + && selected.iter().any(|key| { + cursor.plan.sources.iter().find(|(k, _)| k == key).is_some_and(|old| !current_sources.contains(old)) + }) + { + return Err("the default layer's private source changed since approval".into()); + } + if selected.iter().any(|key| !cursor.designated.contains(key) && !cursor.made_layers.contains(key)) { + return Err("this step acts on whichever layer an earlier step left active; name the layer in the step".into()); + } + if self.active_layer.is_none() && active.is_some() { + return Err("the planned default layer no longer exists; name the layer in the step".into()); + } + } + Target::Layer { key, source } => { + if !node + .targets + .iter() + .any(|t| matches!(t,Target::Layer{key:now,source:now_source}if now==key && source==now_source)) + { + return Err("the step's layer changed since approval".into()); + } + cursor.designated.push(*key); + } + Target::Table { id, source } => { + if !node.targets.iter().any(|t| matches!(t,Target::Table{id:now,source:now_source}if now==id && source==now_source)) + { + return Err("the step's table changed since approval".into()); + } + } + Target::Map(_) | Target::TimeSources(_) | Target::LayerInMap { .. } => { + if !node.targets.contains(target) { + return Err("the step's map or time source changed since approval".into()); + } + if let Target::LayerInMap { map, id, instance, .. } = target { + cursor.designated.push(LayerKey { map: *map, id: *id, instance: *instance }); + } + } + Target::MadeLayer { map, name } => { + if !layers + .iter() + .any(|(n, key)| n.eq_ignore_ascii_case(name) && key.map == *map && cursor.made_layers.contains(key)) + { + return Err("the planned output layer was not made by this run".into()); + } + } + Target::ProducedDataset { producer, map, .. } => { + let matches = match cursor.outputs.get(producer) { + Some(OutputRef::ExistingLayer(key)) if key.map == *map => { + node.targets.iter().any(|t| matches!(t, Target::Layer { key: now, .. } if now == key)) + } + Some(OutputRef::ExistingTable(id)) => { + node.targets.iter().any(|t| matches!(t, Target::Table { id: now, .. } if now == id)) + } + _ => false, + }; + if !matches { + return Err("the planned dataset was not produced by its approved tool".into()); + } + } + Target::MadeTable(name) => { + if !crate::project::find_table(&self.project.tables, name) + .and_then(|i| self.project.tables.get(i)) + .is_some_and(|t| t.id > cursor.plan.last_table && cursor.made_tables.contains(&t.id)) + { + return Err("the planned output table was not made by this run".into()); + } + } + } + } + *at += 1; + node = expected.clone(); + } else if decision != Decision::Allow { + return Err("this operation needs your approval".into()); + } + Ok(Arc::new(node)) + } + /// Keeps the admitted node and synchronous request predicate through nested execution; restores both on panic. + fn with_node(&mut self, node: Arc, f: impl FnOnce(&mut Session) -> Result) -> Result { + let before_layers = self.project.maps.iter().flat_map(|m| m.layers.iter().map(move |l| LayerKey::of(m, l))).collect::>(); + let before_tables = self.project.tables.iter().map(|t| t.id).collect::>(); + let continuation = (!matches!(self.scope().kind, Kind::User)).then(|| self.continuation_for(node.clone())).transpose()?; + let depth = self.policy.nodes.len(); + self.policy.nodes.push(node.clone()); + if let Some(cursor) = &mut self.policy.cursor { + cursor.frames.push((node.children.clone(), 0)); + } + let still = continuation.map(|c| Arc::new(move || c.check().is_ok()) as crate::render::rustgis_tiles::cache::Still); + let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + crate::render::rustgis_tiles::cache::with_optional_still(still, || f(self)) + })); + let complete = + if let Some(cursor) = &mut self.policy.cursor { cursor.frames.pop().is_none_or(|(n, at)| n.len() == at) } else { true }; + if let Some(cursor) = &mut self.policy.cursor { + cursor.made_layers.extend( + self.project + .maps + .iter() + .flat_map(|m| m.layers.iter().map(move |l| LayerKey::of(m, l))) + .filter(|key| !before_layers.contains(key)), + ); + cursor.made_tables.extend(self.project.tables.iter().map(|t| t.id).filter(|id| !before_tables.contains(id))); + } + self.policy.nodes.truncate(depth); + match r { + Ok(Ok(_)) if !complete => Err("the approved plan did not run all its frozen steps".into()), + Ok(r) => r, + Err(p) => std::panic::resume_unwind(p), + } + } + /// Executes only the engine-held download owner and its original address. + pub(super) fn run_url_completion(&mut self, id: u64, command: &str, params: &Value) -> CmdResult { + let c = self.claim_for(id).ok_or("no claimed download with that id")?; + self.continuation_owner(&c)?; + let params = self.download_params(id, params, command == "layer.addUrl")?; + let spec = *self.registry.get(command).ok_or("no URL completion command")?; + let op = Authorized { op: CommandOp::Registered { spec, params: params.clone() }, node: c.node.clone() }; + self.invoke(op, Txn::Own) + } + /// Resolves the command once or retrieves a restricted owner-only completion token. + pub(crate) fn authorize(&mut self, id: &str, params: &Value) -> Result, String> { + if let Some(id_of_run) = params.get("ran").and_then(Value::as_u64) + && let Some(c) = self.raster_results.iter().find(|(n, _)| *n == id_of_run).and_then(|(_, m)| m.continuation.clone()) + { + self.continuation_owner(&c)?; + if id != c.node.id() { + return Err("that run made another command's raster".into()); + } + if params.as_object().is_none_or(|p| p.iter().any(|(key, value)| key != "ran" && c.node.params.get(key) != Some(value))) { + return Err("the raster completion's command or parameters changed".into()); + } + let mut original = c.node.params.clone(); + original["ran"] = json!(id_of_run); + let Op::Command(spec) = &c.node.op else { return Err("not a raster command continuation".into()) }; + return Ok(Authorized { op: CommandOp::Registered { spec: *spec, params: original }, node: c.node.clone() }); + } + + if id == "layer.addDownload" + && let Some(key) = params.get("id").and_then(Value::as_u64) + && let Some(c) = self.claim_for(key) + { + self.continuation_owner(&c)?; + let params = self.download_params(key, params, false)?; + let spec = *self.registry.get(id).ok_or("no download completion command")?; + return Ok(Authorized { op: CommandOp::Registered { spec, params: params.clone() }, node: c.node.clone() }); + } + if id == "gp.finish" + && let Some(run_id) = params.get("id").and_then(Value::as_u64) + && let Some(r) = self.pending.iter().find(|r| r.id == run_id) + { + self.check_gp_finish(&r.continuation, crate::cmd::gp::landing(&r.result))?; + let spec = *self.registry.get(id).ok_or("no tool completion command")?; + return Ok(Authorized { op: CommandOp::Registered { spec, params: params.clone() }, node: r.continuation.node.clone() }); + } + if id == "gp.fileDelivered" + && let Some(run_id) = params.get("id").and_then(Value::as_u64) + && let Some(r) = self.file_outputs.iter().find(|r| r.id == run_id) + { + self.gp_file_owner(&r.continuation)?; + let spec = *self.registry.get(id).ok_or("no file completion command")?; + return Ok(Authorized { op: CommandOp::Registered { spec, params: params.clone() }, node: r.continuation.node.clone() }); + } + let node = self.command_node(id, params)?; + let node = self.admit(node)?; + let op = match &node.op { + Op::Command(spec) => CommandOp::Registered { spec: *spec, params: params.clone() }, + Op::Plugin(cmd, pin) => CommandOp::Plugin { cmd: cmd.clone(), pin: pin.clone(), params: params.clone() }, + _ => return Err("not a command".into()), + }; + Ok(Authorized { op, node }) + } + /// Pins schema/runner/load and effective parameters before granting a raw tool token. + pub(crate) fn authorize_tool(&mut self, tool: &str, params: &Value) -> Result, String> { + let node = self.tool_node(tool, params)?; + let node = self.admit(node)?; + let Op::Tool(pin) = &node.op else { return Err("not a tool".into()) }; + Ok(Authorized { op: ToolOp { pin: pin.clone(), params: pin.resolved.resolve_params(params)? }, node }) + } + /// The synthetic clicked-point input uses only the builtin captured by its command plan. + pub(crate) fn point_tool(&self, params: &Value) -> Result, String> { + let node = self.policy.nodes.last().cloned().ok_or("a point buffer needs its admitted command")?; + if node.id() != "gp.bufferPoint" { + return Err("not a point buffer command".into()); + } + let pin = node.fixed_tool.as_ref().ok_or("the command has no captured buffer tool")?; + pin.current()?; + Ok(Authorized { op: ToolOp { pin: pin.clone(), params: pin.resolved.resolve_params(params)? }, node }) + } + /// Runs a pinned tool and records symbolic output identities for later frozen wires. + pub(crate) fn invoke_tool(&mut self, op: Authorized, control: &Arc) -> CmdResult { + let node = op.node.clone(); + let number = node.number; + let result = self.with_node(node, |s| crate::cmd::gp::execute(s, op, control)); + if let Ok(value) = &result { + // Engine apply's result identifies the actual output kind. An identically named + // layer must never substitute for a produced standalone table (or vice versa). + let layer = value + .get("layer") + .or_else(|| value.get("layers").and_then(|v| v.get(0))) + .and_then(Value::as_u64) + .and_then(|id| u32::try_from(id).ok()) + .and_then(|id| self.map().layer(id)) + .map(|l| OutputRef::ExistingLayer(LayerKey::of(self.map(), l))); + let table = value + .get("table") + .or_else(|| value.get("tables").and_then(|v| v.get(0))) + .and_then(Value::as_str) + .and_then(|name| self.project.tables.iter().find(|t| t.dataset.name == name)) + .map(|t| OutputRef::ExistingTable(t.id)); + if let (Some(cursor), Some(out)) = (&mut self.policy.cursor, layer.or(table)) { + cursor.outputs.insert(number, out); + } + } + result + } + /// Native raster starters admit the command before reading any inputs or preparing work. + #[cfg(not(target_arch = "wasm32"))] + pub(crate) fn start_raster(&mut self, command: &str, params: &Value) -> Result { + let op = self.authorize(command, params)?; + let CommandOp::Registered { spec, params } = op.op else { return Err("not a registered raster command".into()) }; + let named = self.layer_id(¶ms).ok(); + let active = self.active_layer; + if named.is_some() { + self.active_layer = named; + } + let enabled = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (spec.enabled)(self))).unwrap_or(Some("unavailable")); + self.active_layer = active; + if let Some(why) = enabled { + return Err(format!("{}: {why}", spec.label)); + } + if let Some(error) = self.streamed_edit(spec.id, spec.mutates, spec.label, named.or(active)) { + return Err(error); + } + let node = op.node; + let op = Authorized { op: RasterOp { spec, params }, node: node.clone() }; + self.with_node(node, |s| crate::cmd::workflows::raster_start_raw(s, op)) + } + /// A UI completion resumes the stored owner; scoped callers must already be that owner. + pub fn finish_raster(&mut self, command: &str, id: u64, params: &Value) -> CmdResult { + let c = self + .raster_results + .iter() + .find(|(n, _)| *n == id) + .and_then(|(_, m)| m.continuation.clone()) + .ok_or("no pending raster with that id")?; + if !self.policy.scopes.is_empty() { + self.continuation_owner(&c)?; + } + let mut params = params.clone(); + params.as_object_mut().ok_or("raster completion parameters must be an object")?.insert("ran".into(), json!(id)); + self.resume(&c, |s| s.run(command, ¶ms))? + } + /// Builds a raw model token only inside its admitted model.run node. + pub(crate) fn authorize_model(&mut self, p: &Value) -> Result, String> { + let (name, graph, _) = crate::cmd::workflows::model_graph(self, p)?; + let node = self.policy.nodes.last().cloned().ok_or("a model must run through model.run")?; + Ok(Authorized { op: ModelOp { name, graph }, node }) + } + /// The sole Handler call seam; preserves transaction ownership and provenance rollback. + pub(crate) fn invoke(&mut self, op: Authorized, txn: Txn) -> CmdResult { + let node = op.node.clone(); + match &op.op { + CommandOp::Plugin { pin, .. } => { + let pin = pin.clone(); + // Plugin host calls are checked against the pinned manifest, not an invented + // finite list of calls made by code that has yet to run. + let cursor = self.policy.cursor.take(); + let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + self.with_node(node, |s| s.narrowed(Narrow::Plugin(pin), |s| s.run_plugin(op))?) + })); + self.policy.cursor = cursor; + match r { + Ok(r) => r, + Err(p) => std::panic::resume_unwind(p), + } + } + CommandOp::Registered { spec, params } => { + let (spec, params) = (*spec, params.clone()); + if let Some(error) = self.url_completion_refusal(spec.id, ¶ms)? { + return Err(error); + } + let run = |s: &mut Session| { + s.with_node(node, |s| { + let editor = s.trust_editor(spec.id, ¶ms); + let add = |s: &mut Session| { + let before = crate::service_logins::before(s, spec.id, ¶ms)?; + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| spec.run.call(s, ¶ms))) + .unwrap_or_else(|_| Err(format!("{} failed unexpectedly", spec.label))); + // A nested plugin command may fail after making additions. Its outer + // transaction owns rollback; mark those additions anonymous meanwhile. + if let Some(before) = before { + crate::service_logins::tag(s, ¶ms, before, result.as_ref().unwrap_or(&Value::Null))?; + } + result + }; + let anonymous = s.web_logins.is_anonymous() + || (matches!(spec.id, "layer.addUrl" | "layer.addDownload") + && params.get("anonymous").and_then(Value::as_bool) == Some(true)); + let result = crate::service_logins::anonymously(s, anonymous, add) + .unwrap_or_else(|_| Err(format!("{} failed unexpectedly", spec.label))) + .map_err(|e| crate::durable::generated_text(&e)); + if let Ok(value) = &result { + s.trust_after(spec.id, ¶ms, value, editor); + } + result + }) + }; + let result = match txn { + Txn::Own => self.guarded(spec.label, spec.mutates, run), + Txn::Shared => run(self), + }; + self.url_completion_landed(spec.id, ¶ms, &result); + result + } + } + } + /// Invalidates off-thread predicates whenever the active project generation changes. + pub(crate) fn sync_policy_generation(&self) { + self.policy.epoch.store(self.opened, std::sync::atomic::Ordering::SeqCst); + } + /// Copies the admitted tool authority without consulting its public name again. + pub(crate) fn tool_continuation(&self, op: &Authorized) -> Result { + self.continuation_for(op.node.clone()) + } + /// Own private identity snapshots cross workers; display copies never become authority. + fn continuation_for(&self, node: Arc) -> Result { + let reads = node.effects.iter().any(|e| matches!(e, super::Effect::Files | super::Effect::Network | super::Effect::Plugin)); + let mut sources = Vec::new(); + let mut tables = Vec::new(); + if reads { + for target in &node.targets { + match target { + Target::Layer { key, .. } => { + if let Some(layer) = key.get(self) { + sources.push((*key, super::plan::private_source(layer))); + } + } + Target::Table { id, .. } => { + if let Some(t) = self.project.tables.iter().find(|t| t.id == *id) { + tables.push((*id, json!(t.dataset.source))); + } + } + Target::LayerInMap { map, id, .. } => { + if let Some(layer) = self.project.maps.iter().find(|m| m.instance_id() == *map).and_then(|m| m.layer(*id)) { + sources + .push((LayerKey { map: *map, id: *id, instance: layer.instance_id() }, super::plan::private_source(layer))); + } + } + Target::Default { .. } => { + for id in self.selected_layers().into_iter().chain(self.active_layer) { + if let Some(layer) = self.map().layer(id) { + sources.push((LayerKey::of(self.map(), layer), super::plan::private_source(layer))); + } + } + } + _ => {} + } + } + } + Ok(Continuation { + scope: self.scope(), + node: node.clone(), + opened: self.opened, + map: self.map().instance_id(), + epoch: self.policy.epoch.clone(), + accounts: if reads { self.policy_accounts() } else { Value::Null }, + sources, + time_sources: node_time_sources(&node), + tables, + s3: reads.then(|| crate::render::rustgis_tiles::s3::account_identity().map(|identity| vec![identity])).transpose()?, + }) + } + /// Native UI delivery re-enters the engine-held owner; a JSON completion cannot select it. + pub fn finish_gp(&mut self, id: u64) -> CmdResult { + let r = self.pending.iter().find(|r| r.id == id).ok_or("no finished run with that id")?; + let (c, landing) = (r.continuation.clone(), crate::cmd::gp::landing(&r.result)); + self.check_gp_delivery(&c)?; + if c.native_tool_owner() && self.policy.scopes.is_empty() { + // Root is already the same native User, including for failure-only delivery. + return self.resume_gp(&c, landing, |s| s.run("gp.finish", &json!({"id":id})))?; + } + self.resume(&c, |s| s.run("gp.finish", &json!({"id":id})))? + } + /// Applies delivery status under the stored owner, after a live/generation check. + pub fn acknowledge_gp_file(&mut self, id: u64, error: Option<&str>) -> CmdResult { + let c = self.file_outputs.iter().find(|r| r.id == id).ok_or("no pending file with that id")?.continuation.clone(); + self.check_export_delivery(&c)?; + self.resume_checked(&c, |s| s.run("gp.fileDelivered", &json!({"id":id,"error":error})))? + } + /// Retries the same pinned plugin command and exact parameters under its initiating scope. + pub(crate) fn retry_continuation(&mut self, c: &Continuation, params: &Value) -> CmdResult { + self.command_epilogue(c.node.id(), params, |s| { + s.check_continuation(c)?; + if &c.node.params != params { + return Err("the retry's parameters changed".into()); + } + let Op::Plugin(cmd, pin) = &c.node.op else { return Err("not a plugin continuation".into()) }; + let op = + Authorized { op: CommandOp::Plugin { cmd: cmd.clone(), pin: pin.clone(), params: params.clone() }, node: c.node.clone() }; + s.resume(c, |s| s.run_plugin(op))? + }) + } + /// Applies the engine decision to non-command plugin host capabilities. + pub(crate) fn host_effect(&self, effects: super::Effects, what: &'static str) -> Result<(), String> { + let node = Node { + context: self.map().instance_id(), + line: None, + op: Op::Adapter(what), + params: Value::Null, + effects, + targets: Vec::new(), + places: Vec::new(), + display: Default::default(), + children: Vec::new(), + item: None, + generated: false, + number: 0, + wires: Vec::new(), + graph_output: false, + fixed_tool: None, + auth: None, + }; + if decide::decide(self, &self.scope(), &node)? != Decision::Allow { + return Err("this host request needs approval".into()); + } + Ok(()) + } + /// Captures restricted authority only while an admitted operation is executing. + pub(crate) fn continuation(&self) -> Result { + let node = self.policy.nodes.last().cloned().ok_or("late work needs an authorized initiating operation")?; + self.continuation_for(node) + } + /// Checks generation, map, revocation, current load and private source/account identity. + pub(crate) fn check_continuation(&self, c: &Continuation) -> Result<(), String> { + self.check_continuation_origin(c)?; + if c.map != self.map().instance_id() { + return Err("this run belongs to a replaced project or another map".into()); + } + Ok(()) + } + /// Checks live authority and the original identities without routing into the currently displayed map. + pub(crate) fn check_continuation_origin(&self, c: &Continuation) -> Result<(), String> { + self.check_continuation_accounts(c)?; + if c.sources.iter().any(|(key, source)| key.get(self).is_none_or(|l| super::plan::private_source(l) != *source)) { + return Err("the live source or account changed since this request started".into()); + } + if c.time_sources.as_ref().is_some_and(|old| { + *old != crate::cmd::time::following_sources(self).into_iter().map(|(m, l, s)| (m, l, s.to_string())).collect::>() + }) { + return Err("the live map or time source changed since this request started".into()); + } + if c.tables + .iter() + .any(|(id, source)| self.project.tables.iter().find(|t| t.id == *id).is_none_or(|t| json!(t.dataset.source) != *source)) + { + return Err("the live table source changed since this request started".into()); + } + Ok(()) + } + /// Lifetime, load and exact active accounts apply even to failure-only owned tool delivery. + pub(crate) fn check_continuation_accounts(&self, c: &Continuation) -> Result<(), String> { + c.check()?; + if c.opened != self.opened { + return Err("this run belongs to a replaced project".into()); + } + if !c.accounts.is_null() && c.accounts != self.policy_accounts() { + return Err("the live source or account changed since this request started".into()); + } + Ok(()) + } + /// GP owns its input snapshot. Native delivery still checks lifetime, loads and accounts; + /// the GP owner fits its result to the original map and source before application. + pub(crate) fn check_gp_delivery(&self, c: &Continuation) -> Result<(), String> { + if !self.policy.scopes.is_empty() && !self.scope().same_caller(&c.scope) { + return Err("this run belongs to another caller".into()); + } + if c.native_tool_owner() { self.check_continuation_accounts(c) } else { self.check_continuation(c) } + } + /// Rebinds only native owned inputs after the GP owner has checked actual source identity. + /// Missing inputs are valid for a new output from the owned snapshot; updates are fitted by GP. + pub(crate) fn refit_gp_sources( + &self, + c: &mut Continuation, + read: &std::collections::HashMap, + ) -> Result<(), String> { + if !c.native_tool_owner() { + return self.check_continuation(c); + } + let mut sources = Vec::new(); + for (key, old) in &c.sources { + if let Some(layer) = key.get(self) { + let now = super::plan::private_source(layer); + if now != *old && !read.values().any(|r| r.materialized(key.id, key.instance, layer)) { + return Err("the live source or account changed since this request started".into()); + } + sources.push((*key, now)); + } + } + let mut tables = Vec::new(); + for (id, old) in &c.tables { + if let Some(table) = self.project.tables.iter().find(|t| t.id == *id) { + if json!(table.dataset.source) != *old { + return Err("the live table source changed since this request started".into()); + } + tables.push((*id, old.clone())); + } + } + c.sources = sources; + c.tables = tables; + Ok(()) + } + /// The native GP owner distinguishes map edits, project-wide outputs and failure-only delivery. + /// Its sealed result chooses the route; source/account/lifetime checks remain at the applicable boundary. + pub(crate) fn check_gp_finish(&self, c: &Continuation, landing: crate::cmd::gp::Landing) -> Result<(), String> { + if !self.scope().same_caller(&c.scope) { + return Err("this run belongs to another caller".into()); + } + if c.native_tool_owner() { + match landing { + crate::cmd::gp::Landing::Failure => self.check_continuation_accounts(c), + crate::cmd::gp::Landing::Project => self.check_continuation_origin(c), + crate::cmd::gp::Landing::Map => self.check_continuation(c), + } + } else { + self.check_continuation(c) + } + } + /// Re-enters an owned GP completion, retaining actor and panic-safe scope restoration. + pub(crate) fn resume_gp( + &mut self, + c: &Continuation, + landing: crate::cmd::gp::Landing, + f: impl FnOnce(&mut Session) -> T, + ) -> Result { + self.check_gp_finish(c, landing)?; + self.resume_checked(c, f) + } + /// Refuses another caller without consuming the rightful owner's result. + pub(crate) fn continuation_owner(&self, c: &Continuation) -> Result<(), String> { + if !self.scope().same_caller(&c.scope) { + return Err("this run belongs to another caller".into()); + } + self.check_continuation(c) + } + /// Refuses foreign ingress before a failed start may retire its owner's queued work. + pub(crate) fn check_delivery_owner(&self, c: &Continuation) -> Result<(), String> { + if !self.policy.scopes.is_empty() && !self.scope().same_caller(&c.scope) { + return Err("this run belongs to another caller".into()); + } + Ok(()) + } + /// Native workers may deliver at root; an existing ingress must own the stored continuation. + pub(crate) fn check_delivery(&self, c: &Continuation) -> Result<(), String> { + self.check_delivery_owner(c)?; + self.check_continuation(c) + } + /// Re-enters stored authority synchronously and restores stacks even on panic. + pub(crate) fn resume(&mut self, c: &Continuation, f: impl FnOnce(&mut Session) -> T) -> Result { + self.check_continuation(c)?; + self.resume_checked(c, f) + } + /// Restores the stored principal and actor after the owner-specific admission has succeeded. + fn resume_checked(&mut self, c: &Continuation, f: impl FnOnce(&mut Session) -> T) -> Result { + let n = self.policy.nodes.len(); + self.policy.nodes.push(c.node.clone()); + let owned = c.clone(); + let still = Arc::new(move || owned.check().is_ok()) as crate::render::rustgis_tiles::cache::Still; + let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + crate::render::rustgis_tiles::cache::with_still(still, || self.in_scope(c.scope.clone(), f)) + })); + self.policy.nodes.truncate(n); + match r { + Ok(r) => r, + Err(p) => std::panic::resume_unwind(p), + } + } +} + +/// Retains exactly the time-following set chosen at initiation, including secondary maps. +fn node_time_sources(n: &Node) -> Option> { + n.targets.iter().find_map(|t| match t { + Target::TimeSources(s) => Some(s.clone()), + _ => None, + }) +} diff --git a/crates/engine/src/policy/downloads.rs b/crates/engine/src/policy/downloads.rs new file mode 100644 index 0000000000..f3259f1db7 --- /dev/null +++ b/crates/engine/src/policy/downloads.rs @@ -0,0 +1,477 @@ +//! Engine-held claims for deferred URL stages. Payload JSON never creates authority. +use super::Continuation; +use crate::{CmdResult, Session, UrlRequest}; +use serde_json::{Value, json}; +use std::{ + collections::BTreeMap, + sync::{ + Arc, + atomic::{AtomicBool, Ordering}, + }, +}; + +pub(super) struct Claim { + pub c: Continuation, + pub params: Value, + pub live: Arc, + import: bool, +} +impl Drop for Claim { + /// Revokes queued browser/native predicates when an engine claim is retired. + fn drop(&mut self) { + self.live.store(false, Ordering::SeqCst); + } +} +#[derive(Clone)] +pub(super) struct Queued { + c: std::sync::Weak, + import: bool, + consumed: Arc, +} +#[derive(Default)] +pub(crate) struct Work { + pub(super) queued: BTreeMap, + pub(super) claims: BTreeMap, +} +/// A guarded command restores prior opaque requests and retires every newly issued key. +pub(crate) struct UrlRequestCheckpoint { + requests: Vec, + queued: BTreeMap, +} +impl Session { + /// The current project generation, used to prune adapter-owned remembered operations. + pub fn project_generation(&self) -> u64 { + self.opened + } + /// Marks queued authority present before a guarded operation, including taken requests. + pub(crate) fn queued_authority(&self) -> Vec { + self.policy.work.queued.keys().copied().collect() + } + /// Keeps sealed prior requests, including their original owning Arc, across rollback. + pub(crate) fn url_request_checkpoint(&self) -> UrlRequestCheckpoint { + UrlRequestCheckpoint { + requests: self + .url_requests + .iter() + .map(|q| UrlRequest { key: q.key, params: q.params.clone(), authority: q.authority.clone() }) + .collect(), + queued: self.policy.work.queued.clone(), + } + } + pub(crate) fn restore_url_request_checkpoint(&mut self, checkpoint: UrlRequestCheckpoint) { + // Claimed and terminally retired keys stay consumed even if their command later fails. + let mut queued = checkpoint.queued; + queued.retain(|_, q| !q.consumed.load(Ordering::SeqCst)); + self.url_requests = checkpoint.requests.into_iter().filter(|q| queued.contains_key(&q.key)).collect(); + self.policy.work.queued = queued; + self.retire_abandoned_requests(); + } + /// Only successful plugin suspension may retain its new non-import requests through rollback. + pub(crate) fn suspend_plugin_requests(&mut self, before: &[u64], asked: &[(u64, Value)]) -> Vec { + let mut retained = Vec::new(); + for q in std::mem::take(&mut self.url_requests) { + let number = q.params.get("request").and_then(Value::as_u64); + if !before.contains(&q.key) + && self.policy.work.queued.get(&q.key).is_some_and(|queued| !queued.import) + && asked.iter().any(|(n, _)| Some(*n) == number) + { + retained.push(q); + } else { + self.url_requests.push(q); + } + } + retained + } + pub(crate) fn restore_plugin_requests(&mut self, requests: Vec) -> Result<(), String> { + self.retire_abandoned_requests(); + if self.policy.work.queued.len().saturating_add(requests.len()) > crate::MAX_URL_REQUESTS { + return Err(format!("{} downloads are already waiting; try again when they have landed", crate::MAX_URL_REQUESTS)); + } + for q in &requests { + self.check_delivery(&q.authority)?; + } + for q in requests { + self.policy + .work + .queued + .insert(q.key, Queued { c: Arc::downgrade(&q.authority), import: false, consumed: Arc::new(AtomicBool::new(false)) }); + self.url_requests.push(q); + } + Ok(()) + } + pub(crate) fn discard_plugin_requests(&mut self, keys: &[u64]) { + self.url_requests.retain(|q| !keys.contains(&q.key)); + self.policy.work.queued.retain(|key, queued| { + let keep = !keys.contains(key); + if !keep { + queued.consumed.store(true, Ordering::SeqCst); + } + keep + }); + } + /// Plugin pins and caller lifetimes are part of request sharing, not just URL text. + pub(crate) fn same_url_caller(&self, a: &Continuation, b: &Continuation) -> bool { + a.scope.same_caller(&b.scope) && Arc::ptr_eq(&a.epoch, &b.epoch) && a.opened == b.opened && a.map == b.map + } + /// Failure notification settles routing only; named ingress must own the original wait. + /// Native adapters retain the documented root delivery convention. + pub fn request_failed(&mut self, p: &Value, error: &str) { + let Some(n) = p.get("request").and_then(Value::as_u64) else { return }; + if !self.policy_ingress_idle() && self.url_waits.owner(n).is_none_or(|c| self.check_delivery_owner(c).is_err()) { + return; + } + if self.url_waits.holds(n, p) { + self.url_waits.land(n, Err(crate::sources::redact_text(error)), crate::cmd::plugins::fetch::Added::default()); + } + if self.url_waits.given_up(n, p) { + self.url_waits.refused(n); + } + } + /// Only a validated frozen claim can consume retry routing or remove its landed bytes. + pub(super) fn url_completion_refusal(&mut self, command: &str, p: &Value) -> Result, String> { + if !matches!(command, "layer.addUrl" | "layer.addDownload") { + return Ok(None); + } + let Some(id) = p.get("id").and_then(Value::as_u64).filter(|id| self.claim_for(*id).is_some()) else { return Ok(None) }; + let c = self.claim_for(id).ok_or("no claimed download with that id")?; + self.continuation_owner(&c)?; + self.download_params(id, p, command == "layer.addUrl")?; + let Some(n) = p.get("request").and_then(Value::as_u64) else { return Ok(None) }; + let added = self.url_waits.added_already(n, p); + if !added && !self.url_waits.given_up(n, p) { + return Ok(None); + } + self.downloaded.retain(|(k, _)| *k != id); + self.landed_reads.retain(|(k, _)| *k != id); + let url = crate::sources::redact_text(p.get("url").and_then(Value::as_str).unwrap_or("download")); + if added { + Ok(Some(format!("{url}: it was added already for its plugin command, so it wasn't added again"))) + } else { + self.url_waits.refused(n); + Ok(Some(format!("{url}: it came after its plugin command stopped waiting, so it wasn't added"))) + } + } + /// Admitted owner-only completions share retry bookkeeping, including in-place probes. + pub(super) fn url_completion_landed(&mut self, command: &str, p: &Value, result: &CmdResult) { + if !matches!(command, "layer.addUrl" | "layer.addDownload") { + return; + } + let Some(id) = p.get("id").and_then(Value::as_u64) else { return }; + let Some(c) = self.claim_for(id) else { return }; + if self.check_delivery_owner(&c).is_err() || self.download_params(id, p, command == "layer.addUrl").is_err() { + return; + } + if let Some(n) = p.get("request").and_then(Value::as_u64) + && self.url_waits.holds(n, p) + && !result.as_ref().is_ok_and(|v| v.get("pending").and_then(Value::as_bool) == Some(true)) + { + let added = crate::cmd::plugins::fetch::Added::of(self.map(), result); + self.url_waits.land(n, result.clone(), added); + } + } + /// Captures a handler's authority, or checks the equivalent direct native ingress. + pub fn request_url(&mut self, p: &Value) -> CmdResult { + if ["geolibreProject", "geolibreLayer", "geolibreZarr"].iter().any(|k| p.get(k).is_some()) { + return Err("internal import targets require an engine import request".into()); + } + if self.policy_has_no_operation() { + return self.request_url_authorized(p); + } + self.enqueue_url(p) + } + /// Only the engine import executor supplies reservation flags; JSON never chooses purpose. + pub(crate) fn request_import_url(&mut self, p: &Value) -> CmdResult { + if self.policy_has_no_operation() { + return self.request_import_url_authorized(p); + } + self.enqueue_url_for(p, true) + } + /// Drops only expired weak queue entries; taken requests own their authority until claimed/dropped. + pub(crate) fn retire_abandoned_requests(&mut self) { + self.policy.work.queued.retain(|_, q| q.c.strong_count() > 0); + } + /// Counts live unclaimed requests, including opaque handles already taken by an adapter. + /// Dropped handles do not occupy a slot, matching abandoned-request retirement. + pub(crate) fn queued_url_count(&self) -> usize { + self.policy.work.queued.values().filter(|q| q.c.strong_count() > 0).count() + } + /// Deduplicates exact requests within one owner and retains their initiating node. + pub(super) fn enqueue_url(&mut self, p: &Value) -> CmdResult { + self.enqueue_url_for(p, false) + } + /// Owns the originating continuation in the opaque request and bounds all retained unclaimed requests. + fn enqueue_url_for(&mut self, p: &Value, import: bool) -> CmdResult { + let mut c = self.continuation()?; + self.check_continuation(&c)?; + // Only the built-in data reader proves this bounded set. Services, imports and + // plugin-initiated discovery retain the full active storage identity. + if !import + && matches!(&c.node.op, super::plan::Op::Command(spec) if spec.id == "layer.addUrl") + && let Some(urls) = crate::cmd::layer::fixed_download_urls(p) + { + let selected = urls + .iter() + .map(|url| crate::render::rustgis_tiles::s3::account_identity_for(crate::render::rustgis_tiles::logins::inner(url))) + .collect::, _>>()?; + // A concurrent account change cannot become fresh authority during narrowing. + self.check_continuation(&c)?; + c.s3 = Some(selected); + } + let c = Arc::new(c); + self.retire_abandoned_requests(); + if !p.is_object() { + return Err("a download is asked for by an object: its \"url\" and how to read it".into()); + } + let asked = self.asked_for(p); + let waiting = self + .url_requests + .iter() + .find(|q| { + crate::cmd::plugins::fetch::same_request(&q.params, &asked) + && self.same_url_caller(&q.authority, &c) + && self.check_continuation(&q.authority).is_ok() + }) + .and_then(|q| q.params.get("request").and_then(Value::as_u64)); + // A handed-back receipt is honored only through the original validated opaque claim. + let given = p + .get("id") + .and_then(Value::as_u64) + .filter(|id| self.download_params(*id, p, true).is_ok()) + .and_then(|id| self.policy.work.claims.get(&id)) + .filter(|claim| self.same_url_caller(&claim.c, &c)) + .and_then(|claim| claim.params.get("request").and_then(Value::as_u64)) + .filter(|n| self.url_waits.holds(*n, &asked)); + let made = self + .call_requests + .as_ref() + .and_then(|_| { + self.url_waits.in_flight(&asked, |n| { + self.url_waits.owner(n).is_some_and(|owner| self.same_url_caller(owner, &c) && self.check_continuation(owner).is_ok()) + }) + }) + .filter(|n| !self.url_waits.taken_away(*n, &self.project)); + let number = match given.or(waiting).or(made) { + Some(n) => n, + None => { + self.next_request = self.next_request.checked_add(1).ok_or("download request numbers were exhausted")?; + self.next_request + } + }; + if (given.is_some() && waiting != Some(number)) || (waiting.is_none() && !self.url_waits.has(number)) { + if self.policy.work.queued.len() >= crate::MAX_URL_REQUESTS { + return Err(format!("{} downloads are already waiting; try again when they have landed", crate::MAX_URL_REQUESTS)); + } + let mut params = asked.clone(); + params.as_object_mut().ok_or("download parameters must be an object")?.insert("request".into(), json!(number)); + if given.is_some() { + params.as_object_mut().ok_or("download parameters must be an object")?.insert("whole".into(), json!(true)); + } + self.next_pending = self.next_pending.wrapping_add(1); + let key = self.next_pending; + self.policy.work.queued.insert(key, Queued { c: Arc::downgrade(&c), import, consumed: Arc::new(AtomicBool::new(false)) }); + self.url_requests.push(UrlRequest { key, params, authority: c }); + } + if !import + && let Some(asked_now) = self.call_requests.as_mut() + && !asked_now.iter().any(|(n, _)| *n == number) + { + asked_now.push((number, asked)); + } + self.request_only = true; + Ok(json!({"pending":true,"url":p.get("url").and_then(Value::as_str).map(crate::sources::redact)})) + } + /// Taking an opaque request sends nothing and retains other callers' queued requests. + /// Authority is checked again when the owner or native host claims it. + pub fn take_url_requests(&mut self) -> Vec { + let mut live = Vec::new(); + let mut revoked = 0; + for request in std::mem::take(&mut self.url_requests) { + if self.check_delivery_owner(&request.authority).is_err() { + self.url_requests.push(request); + continue; + } + let ok = self + .policy + .work + .queued + .get(&request.key) + .and_then(|q| q.c.upgrade()) + .ok_or("the request was already claimed") + .and_then(|c| self.check_continuation_origin(&c).map_err(|_| "this URL request was revoked")); + if ok.is_ok() { + live.push(request); + } else { + self.request_failed(&request.params, "this URL request was revoked before it started"); + if let Some(queued) = self.policy.work.queued.remove(&request.key) { + queued.consumed.store(true, Ordering::SeqCst); + if queued.import { + crate::cmd::geolibre::discard_pending(self, &request.params); + } + } + revoked += 1; + } + } + match revoked { + 0 => {} + 1 => self.notify("1 deferred URL request was revoked before it started", true), + n => self.notify(format!("{n} deferred URL requests were revoked before they started"), true), + } + live + } + /// Claims one engine-issued request immediately before its first I/O. + pub fn download_started_for(&mut self, request: &UrlRequest) -> Result { + let q = self.policy.work.queued.get(&request.key).ok_or("this URL request was already claimed or dropped")?; + let c = q.c.upgrade().ok_or("this URL request was dropped")?; + if !Arc::ptr_eq(&request.authority, &c) { + return Err("this URL request belongs to another session".into()); + } + self.check_delivery_owner(&c)?; + let import = q.import; + let ready = if self.policy.work.claims.len() >= 64 { + Err("64 downloads are already awaiting completion".into()) + } else { + self.check_delivery(&c) + }; + if let Err(error) = ready { + self.request_failed(&request.params, &error); + if import { + // Only the verified owner may abandon this sealed reservation before a reader exists. + if let Some(queued) = self.policy.work.queued.remove(&request.key) { + queued.consumed.store(true, Ordering::SeqCst); + } + crate::cmd::geolibre::discard_pending(self, &request.params); + } + return Err(error); + } + if let Some(q) = self.policy.work.queued.remove(&request.key) { + q.consumed.store(true, Ordering::SeqCst); + } + let c = (*c).clone(); + let id = self.download_started(); + self.policy.work.claims.insert(id, Claim { c, params: request.params.clone(), live: Arc::new(AtomicBool::new(true)), import }); + Ok(id) + } + /// Rechecks every new stage, including a probe's fallback and later pages or keys. + pub fn download_continues(&mut self, id: u64) -> Result<(), String> { + self.download_owner(id)?; + if !self.download_is_pending(id) + && !self.downloaded.iter().any(|(n, _)| *n == id) + && !self.landed_reads.iter().any(|(n, _)| *n == id) + { + return Err("this download belongs to a replaced project or was dropped".into()); + } + if let Some(claim) = self.policy.work.claims.get(&id) + && let Err(e) = self.check_continuation(&claim.c) + { + if let Some(claim) = self.policy.work.claims.remove(&id) { + self.request_failed(&claim.params, &e); + if claim.import { + crate::cmd::geolibre::discard_pending(self, &claim.params); + } + } + self.downloading.retain(|n| *n != id); + self.downloaded.retain(|(n, _)| *n != id); + self.landed_reads.retain(|(n, _)| *n != id); + return Err(e); + } + Ok(()) + } + /// Owns a principal-free predicate that survives queues and browser awaits. + pub fn download_still(&self, id: u64) -> Option { + let claim = self.policy.work.claims.get(&id)?; + let (c, live) = (claim.c.clone(), claim.live.clone()); + Some(Arc::new(move || live.load(Ordering::SeqCst) && c.check().is_ok())) + } + /// Completes a read-in-place probe under its engine-held owner and exact original address. + pub fn add_in_place_for(&mut self, id: u64, p: &Value) -> CmdResult { + let c = self.policy.work.claims.get(&id).ok_or("no claimed URL request with that id")?.c.clone(); + self.check_delivery(&c)?; + self.download_continues(id)?; + self.download_params(id, p, true)?; + let r = self.resume(&c, |s| s.run_url_completion(id, "layer.addUrl", p)); + self.policy.work.claims.remove(&id); + self.downloading.retain(|n| *n != id); + r? + } + /// Native delivery resumes the claim. Ordinary user-owned downloads retain their ingress. + pub fn complete_download(&mut self, id: u64, p: &Value) -> CmdResult { + if let Some(claim) = self.policy.work.claims.get(&id) { + let c = claim.c.clone(); + if !self.policy_ingress_idle() { + self.continuation_owner(&c)?; + } + let result = self.resume(&c, |s| s.run("layer.addDownload", p)); + // Consume only the owner's claim. A rejected foreign call never reaches this path. + self.policy.work.claims.remove(&id); + result? + } else { + self.run("layer.addDownload", p) + } + } + /// Reconstructs the frozen effective request, allowing only documented completion refinements. + pub(super) fn download_params(&self, id: u64, p: &Value, in_place: bool) -> Result { + let claim = self.policy.work.claims.get(&id).ok_or("no claimed download with that id")?; + self.check_continuation(&claim.c)?; + let given = p.as_object().ok_or("download parameters must be an object")?; + let mut params = claim.params.as_object().cloned().ok_or("the stored download parameters are not an object")?; + for (key, value) in given { + if ["geolibreProject", "geolibreLayer", "geolibreZarr"].contains(&key.as_str()) && !claim.import { + return Err("a simple URL completion cannot import a project's internal targets".into()); + } + let refinement = (key == "id" && value.as_u64() == Some(id)) + || (key == "cog" && in_place && value.as_bool() == Some(true)) + || (key == "whole" && value.as_bool() == Some(true)) + || (key == "zoom" && value.as_bool() == Some(false) && claim.params.get(key).and_then(Value::as_bool) == Some(true)); + if !refinement && claim.params.get(key) != Some(value) { + return Err("the download's address or parameters changed since it was requested".into()); + } + params.insert(key.clone(), value.clone()); + } + params.insert("id".into(), json!(id)); + Ok(Value::Object(params)) + } + /// Internal import flags are accepted only while applying that engine-issued claim. + pub(crate) fn import_completion(&self, p: &Value) -> bool { + p.get("id") + .and_then(Value::as_u64) + .and_then(|id| self.policy.work.claims.get(&id)) + .is_some_and(|claim| claim.import && self.check_delivery(&claim.c).is_ok()) + } + /// Borrows stored completion authority; payload fields cannot manufacture one. + pub(super) fn claim_for(&self, id: u64) -> Option { + self.policy.work.claims.get(&id).map(|c| c.c.clone()) + } + /// Moves claims with the project state so transactional replacement can roll back. + pub(crate) fn take_policy_work(&mut self) -> Work { + std::mem::take(&mut self.policy.work) + } + /// Restores the original project's still-owned claims after rollback. + pub(crate) fn restore_policy_work(&mut self, work: Work) { + self.policy.work = work; + } + /// Named callbacks own their claim; the native root retains its documented delivery role. + pub(crate) fn download_owner(&self, id: u64) -> Result<(), String> { + if let Some(claim) = self.policy.work.claims.get(&id) { + self.check_delivery_owner(&claim.c)?; + } + Ok(()) + } + /// A native queue retirement settles only the exact wait held by its sealed claim. + /// No public request number or current caller can select another wait here. + pub(crate) fn retire_download_claim(&mut self, id: u64, error: &str) { + let Some(claim) = self.policy.work.claims.remove(&id) else { return }; + claim.live.store(false, Ordering::SeqCst); + if let Some(number) = claim.params.get("request").and_then(Value::as_u64) + && self.url_waits.holds(number, &claim.params) + { + self.url_waits.land(number, Err(crate::sources::redact_text(error)), crate::cmd::plugins::fetch::Added::default()); + } + if claim.import { + crate::cmd::geolibre::discard_pending(self, &claim.params); + } + } + /// Consumes a completed claim and invalidates every captured predicate. + pub(crate) fn forget_download_claim(&mut self, id: u64) { + self.policy.work.claims.remove(&id); + } +} diff --git a/crates/engine/src/policy/effects.rs b/crates/engine/src/policy/effects.rs new file mode 100644 index 0000000000..71592896a3 --- /dev/null +++ b/crates/engine/src/policy/effects.rs @@ -0,0 +1,151 @@ +use crate::CommandSpec; +use serde_json::Value; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Effect { + Edits, + Files, + Network, + Plugin, + Automation, + Reserved, + Replaces, + History, + Unreviewed, +} +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct Effects(u16); +impl Effects { + pub const NONE: Self = Self(0); + pub const UNREVIEWED: Self = Self::NONE.with(Effect::Unreviewed); + /// Adds an effect without removing any previously declared reach. + pub const fn with(self, effect: Effect) -> Self { + Self(self.0 | 1 << effect as u16) + } + /// Tests one additive effect without treating the set as a trust level. + pub const fn contains(self, effect: Effect) -> bool { + self.0 & (1 << effect as u16) != 0 + } + /// Combines reviewed effects from a command and its nested operations. + pub const fn union(self, other: Self) -> Self { + Self(self.0 | other.0) + } + /// Enumerates every declared effect so mixed Files/Network/Replaces remain additive. + pub(super) fn iter(self) -> impl Iterator { + [ + Effect::Edits, + Effect::Files, + Effect::Network, + Effect::Plugin, + Effect::Automation, + Effect::Reserved, + Effect::Replaces, + Effect::History, + Effect::Unreviewed, + ] + .into_iter() + .filter(move |e| self.contains(*e)) + } +} +pub(crate) const PLACE_KEYS: &[&str] = &["path", "folder", "dir", "file", "url", "uri", "source", "output", "connection", "database"]; +/// State-dependent additions to a reviewed declaration. Effects add; they never remove authority. +pub(super) fn declared(spec: &CommandSpec, params: &Value) -> Effects { + let mut effects = spec.effects; + if spec.mutates { + effects = effects.with(Effect::Edits); + } + if spec.id == "map.body" && params.get("basemap").and_then(Value::as_bool) != Some(false) { + effects = effects.with(Effect::Network); + } + if spec.id == "cube.open" && crate::cmd::cube_catalog::opens_zarr(params) { + effects = effects.with(Effect::Files).with(Effect::Network); + } + if spec.id == "map.addTiles" { + effects = effects.union(crate::cmd::tiles::add_reach(params)); + } + let params = crate::cmd::items::layer_name_left_out(spec.id, params); + // These owners read an explicit null path as no output file. Keep the existing narrow + // in-memory raster contract, including rasterization, without ignoring any other place. + let params = + if matches!(spec.id, "raster.georeference" | "raster.project" | "raster.rasterize") && params.get("path") == Some(&Value::Null) { + let mut given = params.into_owned(); + if let Some(o) = given.as_object_mut() { + o.remove("path"); + } + std::borrow::Cow::Owned(given) + } else { + params + }; + if !effects.contains(Effect::Network) && crate::cmd::items::names_place(¶ms, PLACE_KEYS, 0) { + effects = effects.with(Effect::Files); + } + effects +} +/// Adds only source reach of the actual current execution targets, without opening them. +pub(super) fn command(s: &crate::cmd::InputContext<'_>, spec: &CommandSpec, params: &Value, layer: Option<&rustgis_map::Layer>) -> Effects { + let mut effects = declared(spec, params); + effects = effects.union(match spec.id { + "map.zoomToTiles" => crate::cmd::tiles::metadata_reach(s, params), + "map.search" => crate::cmd::search::metadata_reach(s, params), + "map.tiles" | "info.project" => crate::cmd::tiles::listing_reach(s.map()), + "map.setTiles" => crate::cmd::tiles::set_reach(s, params), + "time.export" => crate::cmd::time::export_reach(s, params), + _ => Effects::NONE, + }); + if matches!( + spec.id, + "layout.export" | "presentation.export" | "share.exportMap" | "animation.export" | "project.thumbnail" | "map.thumbnail" + ) && let Some(select) = spec.inputs + && let Ok(inputs) = select(s, params) + { + // Reuse the export owner's actual frame/page selection, never every project map. + for id in inputs.maps { + if let Some(map) = s.project.maps.iter().find(|m| m.instance_id() == id) { + effects = effects.union(crate::cmd::tiles::render_reach(map)); + } + } + } + if spec.id == "layer.loadAll" + && let Some(source) = layer.and_then(|layer| layer.source.as_ref()) + { + effects = effects.union(source_reach(source)); + } + if crate::cmd::time::follows_sources(spec.id) { + for (_, _, source) in crate::cmd::time::following_sources(s) { + effects = effects.with(Effect::Files); + if source.starts_with("https://") || source.starts_with("http://") { + effects = effects.with(Effect::Network); + } + } + } + effects +} +/// Pure reach of a source the executor will read, without opening it. +pub(super) fn source_reach(source: &rustgis_map::source::SourceLayer) -> Effects { + let mut effects = Effects::NONE; + let remote = source.path.as_deref().is_some_and(rustgis_data::source::is_remote_name); + if (source.path.is_some() && !remote) || source.gdal.is_some() || source.duckdb.is_some() { + effects = effects.with(Effect::Files); + } + if remote + || source.postgis.is_some() + || source.mysql.is_some() + || source.sqlserver.is_some() + || source.gdal.is_some() + || source.duckdb.is_some() + { + effects = effects.with(Effect::Network); + } + effects +} +/// Legacy imported-item deny heuristic, retained in addition to declared effects. +pub(super) fn imported(command: &str, params: &Value) -> bool { + const KEYS: &[&str] = + &["path", "folder", "dir", "file", "url", "uri", "source", "output", "connection", "database", "data", "text", "poster", "locator"]; + let id = command.to_ascii_lowercase(); + let verb = id.split('.').nth(1).unwrap_or(""); + ["script.", "notebook.", "model.", "project.", "task.", "share.", "offline.", "cache.", "locator."].iter().any(|p| id.starts_with(p)) + || ["import", "export", "open", "save", "add", "package", "upload", "download"].iter().any(|w| verb.starts_with(w)) + || id == "raster.mosaic" + || crate::cmd::items::names_place(&crate::cmd::items::layer_name_left_out(command, params), KEYS, 0) +} diff --git a/crates/engine/src/policy/handler.rs b/crates/engine/src/policy/handler.rs new file mode 100644 index 0000000000..625fb02b02 --- /dev/null +++ b/crates/engine/src/policy/handler.rs @@ -0,0 +1,20 @@ +//! The sole callable command pointer. Outside policy it is metadata, not authority. +use crate::{CmdResult, Session}; +use serde_json::Value; + +#[derive(Clone, Copy)] +pub struct Handler(fn(&mut Session, &Value) -> CmdResult); +impl Handler { + /// Only engine registration may wrap a raw function pointer. + pub(crate) const fn new(f: fn(&mut Session, &Value) -> CmdResult) -> Self { + Self(f) + } + /// Only policy dispatch may execute the wrapped handler. + pub(in crate::policy) fn call(self, s: &mut Session, p: &Value) -> CmdResult { + (self.0)(s, p) + } + /// Consent binds the captured handler as well as its command id and display metadata. + pub(in crate::policy) fn same(self, other: Self) -> bool { + std::ptr::fn_addr_eq(self.0, other.0) + } +} diff --git a/crates/engine/src/policy/mod.rs b/crates/engine/src/policy/mod.rs new file mode 100644 index 0000000000..f3318c9ba1 --- /dev/null +++ b/crates/engine/src/policy/mod.rs @@ -0,0 +1,48 @@ +//! Permission decisions and execution authority for commands and their continuations. +//! +//! Native user entry points may call `Session::run`. Untrusted adapters select a named +//! `Scope`, show the engine's `Review`, and consume its permit; nested work only narrows it. +mod decide; +mod dispatch; +pub(crate) mod downloads; +mod effects; +pub mod handler; +mod plan; +mod provenance; +mod scope; + +pub use dispatch::PreparedCommand; +#[cfg(not(target_arch = "wasm32"))] +pub(crate) use dispatch::RasterOp; +pub(crate) use dispatch::{Authorized, CommandOp, ModelOp, State, ToolOp, Txn}; +pub(crate) use effects::PLACE_KEYS; +pub use effects::{Effect, Effects}; +pub use handler::Handler; +pub use plan::{Call, Review, ReviewDetail}; +pub(crate) use plan::{Node, PluginPin, ToolPin}; +pub(crate) use provenance::Trust; +pub use scope::{AgentAccess, Deferred, Outcome, Pending, Permit, Principal, Remembered, Revocation, Scope}; +pub(crate) use scope::{Continuation, Narrow}; + +/// Compatibility projection for native metadata consumers; policy itself keeps additive effects. +pub fn command_effects(spec: &crate::CommandSpec, params: &serde_json::Value) -> Effects { + let effects = effects::declared(spec, params); + if spec.id == "gp.run" + && let Some(tool) = params.get("tool").and_then(serde_json::Value::as_str) + && let Ok(node) = plan::tool_effect_node(tool, params.get("params").unwrap_or(&serde_json::Value::Null)) + { + effects.union(node) + } else { + effects + } +} + +/// Retains the reviewed conservative imported-item restriction separately from declared effects. +pub(crate) fn imported_heuristic(id: &str, p: &serde_json::Value) -> bool { + effects::imported(id, p) +} + +/// Raw in-memory source identity for an executor-owned clipboard snapshot; never a display copy. +pub(crate) fn input_source(layer: &rustgis_map::Layer) -> serde_json::Value { + plan::private_source(layer) +} diff --git a/crates/engine/src/policy/plan.rs b/crates/engine/src/policy/plan.rs new file mode 100644 index 0000000000..9f6c7ec6cf --- /dev/null +++ b/crates/engine/src/policy/plan.rs @@ -0,0 +1,1455 @@ +use super::{ + Effect, Effects, + decide::{self, Decision}, + scope::{Owner, Scope}, +}; +use crate::{CommandSpec, Session}; +use serde_json::{Value, json}; +use std::sync::Arc; + +pub struct Call { + pub(super) kind: CallKind, + pub(super) request: String, +} +pub(super) enum CallKind { + Command { id: String, params: Value }, + Script(String), + Adapter { what: &'static str, effects: Effects, params: Value }, +} +impl Call { + /// Owns the raw command request for later resolution; output redactions never become execution data. + pub fn command(id: &str, params: Value) -> Self { + Self { kind: CallKind::Command { id: id.into(), params }, request: String::new() } + } + /// Uses gp.run as the canonical vehicle for the raw tool request. + pub fn tool(tool: &str, params: Value) -> Self { + Self::command("gp.run", json!({"tool": tool, "params": params})) + } + /// Owns script text for later bounded expansion by the execution parser. + pub fn script(text: &str) -> Self { + Self { kind: CallKind::Script(text.into()), request: String::new() } + } + /// Captures the trusted adapter's exact effect and destination for review. + pub fn adapter(what: &'static str, effects: Effects, params: Value) -> Self { + Self { kind: CallKind::Adapter { what, effects, params }, request: String::new() } + } + /// Binds the trusted adapter's queued request id, independently of remembered operations. + pub fn with_request(mut self, request: &str) -> Self { + self.request = request.into(); + self + } +} +/// Sanitized effective selectors, for presentation only; raw targets remain private. +#[derive(Clone, Debug, Default, PartialEq, serde::Serialize)] +pub struct ReviewDetail { + pub operation: String, + pub targets: Vec, + pub places: Vec, +} +#[derive(Clone, Debug)] +pub struct Review { + pub what: String, + pub why: String, + pub steps: Vec, + pub remember: bool, + /// Sanitized actual targets and destinations, counted in the full-card bound. + pub details: Vec, + /// Project-only edit count; presentation layers supply their own translated wording. + pub edits: usize, + /// Sanitized initial active/selected names, separate from execution identity. + pub defaults: Option<(Option, Vec)>, + /// Stable explanation template and sanitized substitutions for translated cards. + pub explanation: (&'static str, Vec<(&'static str, String)>), + /// Script line numbers and command ids needing review, for translated reason prefixes. + pub lines: Vec<(usize, String)>, +} +pub(super) const MAX_NODES: usize = 1_000; +const MAX_DEPTH: usize = 4; +const MAX_REVIEW: usize = 4_096; +const MAX_PLAN_BYTES: usize = 64 * 1024; + +/// Measures serialized shape without allocating a second unbounded copy of hostile content. +fn bounded_size(value: &impl serde::Serialize, limit: usize) -> Result { + struct Size { + bytes: usize, + limit: usize, + } + impl std::io::Write for Size { + fn write(&mut self, bytes: &[u8]) -> std::io::Result { + let size = self.bytes.saturating_add(bytes.len()); + if size > self.limit { + return Err(std::io::Error::other("plan size bound")); + } + self.bytes = size; + Ok(bytes.len()) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + let mut size = Size { bytes: 0, limit }; + serde_json::to_writer(&mut size, value) + .map_err(|_| "this operation is too large to freeze for review; run it yourself or request fewer steps".to_string())?; + Ok(size.bytes) +} +#[derive(Clone, PartialEq)] +pub(super) struct Plan { + pub owner: Owner, + pub opened: u64, + pub map: u64, + pub designated: Vec, + pub last_table: u32, + pub picked: Option<(Option, Vec)>, + pub names: Vec<(String, u32)>, + pub sources: Vec<(LayerKey, Value)>, + pub accounts: Value, + pub s3: Vec, + pub root: Node, +} +#[derive(Clone)] +pub(crate) struct PluginPin(pub(super) Arc); +impl PluginPin { + /// Borrows the exact plugin load already held by the pin. + pub(crate) fn plugin(&self) -> &Arc { + &self.0 + } +} +impl PartialEq for PluginPin { + /// Compares raw operation identity, including exact pinned loads; no digest grants consent. + fn eq(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.0, &other.0) + } +} +#[derive(Clone)] +pub(crate) struct ToolPin { + pub(super) resolved: rustgis_analysis::ResolvedTool, + pub(super) plugin: Option, + pub(super) runner: Value, + #[cfg(feature = "geolibre")] + suite: Option, + #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] + native_runner: Option, +} +impl PartialEq for ToolPin { + /// Compares raw operation identity, including exact pinned loads; no digest grants consent. + fn eq(&self, other: &Self) -> bool { + std::ptr::eq(self.resolved.spec(), other.resolved.spec()) + && self.resolved.source() == other.resolved.source() + && self.plugin == other.plugin + && self.runner == other.runner + } +} +impl ToolPin { + /// Returns the immutable captured schema used for planning and execution. + pub(crate) fn spec(&self) -> &'static rustgis_analysis::ToolSpec { + self.resolved.spec() + } + /// Returns the immutable analysis schema/runner pair, without another name lookup. + pub(crate) fn resolved(&self) -> &rustgis_analysis::ResolvedTool { + &self.resolved + } + /// Borrows the exact plugin load already held by the pin. + pub(crate) fn plugin(&self) -> Option<&PluginPin> { + self.plugin.as_ref() + } + /// Rejects a withdrawn load or reloaded schema before a new stage begins. + pub(crate) fn current(&self) -> Result<(), String> { + if self.plugin.is_none() && !self.resolved.is_current() { + return Err("the tool schema was reloaded since this run was approved".into()); + } + #[cfg(feature = "geolibre")] + if let Some(suite) = &self.suite { + suite.check()?; + } + #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] + if let Some(runner) = &self.native_runner { + runner.check()?; + } + if self.plugin.as_ref().is_some_and(|p| !rustgis_plugins::is_current(&p.0)) { + return Err("the plugin was disabled, updated or removed since this run was approved".into()); + } + if let Some(pin) = &self.plugin { + rustgis_plugins::tools::matches_pinned(&pin.0, self.spec())?; + } + if self.resolved.source() == Some("geolibre") && self.runner != crate::cmd::gp::geolibre_status() { + return Err("the GeoLibre runner changed since this run was approved".into()); + } + Ok(()) + } + /// Executes the captured runner, including the pinned native suite path and manifest metadata. + pub(crate) fn run( + &self, + inputs: &dyn rustgis_analysis::Inputs, + params: &Value, + control: &Arc, + live: &dyn Fn() -> Result<(), String>, + ) -> Result { + self.current()?; + live()?; + #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] + if let Some(suite) = &self.suite { + let runner = self.native_runner.as_ref().ok_or("the GeoLibre runner isn't installed")?; + return rustgis_analysis::run_under(self.spec().name, control, || { + rustgis_geolibre::run_pinned(suite, inputs, params, runner, live) + }); + } + rustgis_analysis::run_resolved(self.resolved(), inputs, params, control) + } + /// Builds browser argv from captured suite metadata after checking its load. + #[cfg(feature = "geolibre")] + pub(crate) fn prepare_suite(&self, inputs: &dyn rustgis_analysis::Inputs, params: &Value) -> Result { + self.current()?; + self.suite.as_ref().ok_or("not a pinned GeoLibre tool")?.prepare(inputs, params) + } + /// Resolves once, pairing plugin schema with its exact manifest owner before inputs are read. + pub(super) fn resolve(name: &str) -> Result { + let resolved = rustgis_analysis::resolve_published(name) + .ok_or_else(|| format!("no tool '{name}' is published; locate or reload external tools from the native app first"))?; + let plugin = if resolved.source() == Some(rustgis_plugins::tools::SOURCE) { + let (id, _) = rustgis_plugins::split_id(resolved.spec().id).ok_or("invalid plugin tool id")?; + Some(PluginPin(rustgis_plugins::get(id).filter(|p| p.enabled).ok_or("the plugin is disabled or no longer loaded")?)) + } else { + None + }; + if let Some(pin) = &plugin { + rustgis_plugins::tools::matches_pinned(&pin.0, resolved.spec())?; + } + let runner = if resolved.source() == Some("geolibre") { crate::cmd::gp::geolibre_status() } else { Value::Null }; + #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] + let native_runner = + if resolved.source() == Some("geolibre") { rustgis_geolibre::runner::ResolvedRunner::capture().ok() } else { None }; + #[cfg(feature = "geolibre")] + let suite = + if resolved.source() == Some("geolibre") { Some(rustgis_geolibre::ResolvedTool::capture(resolved.spec())?) } else { None }; + Ok(Self { + resolved, + plugin, + runner, + #[cfg(feature = "geolibre")] + suite, + #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] + native_runner, + }) + } +} +#[derive(Clone)] +pub(super) enum Op { + Command(CommandSpec), + Plugin(rustgis_plugins::PluginCommand, PluginPin), + Tool(ToolPin), + Adapter(&'static str), +} +impl PartialEq for Op { + /// Compares raw operation identity, including exact pinned loads; no digest grants consent. + fn eq(&self, o: &Self) -> bool { + match (self, o) { + (Self::Command(a), Self::Command(b)) => { + a.id == b.id + && a.label == b.label + && a.location == b.location + && a.shortcut == b.shortcut + && a.params == b.params + && a.mutates == b.mutates + && a.effects == b.effects + && match (a.inputs, b.inputs) { + (Some(a), Some(b)) => std::ptr::fn_addr_eq(a, b), + (None, None) => true, + _ => false, + } + && match (a.transition, b.transition) { + (Some(a), Some(b)) => std::ptr::fn_addr_eq(a, b), + (None, None) => true, + _ => false, + } + && a.run.same(b.run) + && std::ptr::fn_addr_eq(a.enabled, b.enabled) + } + (Self::Plugin(a, p), Self::Plugin(b, q)) => a.decl.id == b.decl.id && p == q, + (Self::Tool(a), Self::Tool(b)) => a == b, + (Self::Adapter(a), Self::Adapter(b)) => a == b, + _ => false, + } + } +} +#[derive(Clone, PartialEq)] +pub(super) struct Wire { + pub key: String, + pub from: Vec, +} +#[derive(Clone, PartialEq)] +pub(super) enum OutputRef { + ExistingLayer(LayerKey), + ExistingTable(u32), + Node(u16), +} +#[derive(Clone, PartialEq)] +pub(crate) struct Node { + pub(super) context: u64, + pub(super) line: Option, + pub(super) op: Op, + pub(super) params: Value, + pub(super) effects: Effects, + pub(super) targets: Vec, + pub(super) places: Vec, + pub(super) display: ReviewDetail, + pub(super) children: Vec, + pub(super) item: Option<(&'static str, String, bool, Value)>, + pub(super) generated: bool, + pub(super) number: u16, + pub(super) wires: Vec, + pub(super) graph_output: bool, + pub(super) fixed_tool: Option, + pub(super) auth: Option>, +} +/// An in-memory layer incarnation qualified by its owning map; numeric ids repeat across maps. +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub(super) struct LayerKey { + pub map: u64, + pub id: u32, + pub instance: u64, +} +impl LayerKey { + /// Qualifies a layer with its owning map and in-memory incarnation. + pub fn of(map: &rustgis_map::Map, layer: &rustgis_map::Layer) -> Self { + Self { map: map.instance_id(), id: layer.id, instance: layer.instance_id() } + } + /// Finds that exact incarnation without consulting the active map or a name. + pub fn get(self, s: &Session) -> Option<&rustgis_map::Layer> { + s.project + .maps + .iter() + .find(|m| m.instance_id() == self.map) + .and_then(|m| m.layer(self.id)) + .filter(|l| l.instance_id() == self.instance) + } +} +#[derive(Clone, PartialEq)] +pub(super) enum Target { + Default { active: Option, selected: Vec }, + Layer { key: LayerKey, source: Option }, + Table { id: u32, source: Option }, + MadeLayer { map: u64, name: String }, + MadeTable(String), + ProducedDataset { producer: u16, map: u64, name: String }, + Map(u64), + LayerInMap { map: u64, id: u32, instance: u64, source: Option }, + TimeSources(Vec<(u64, u32, String)>), +} +impl Node { + /// The captured operation's stable id, used for dispatch checks and sanitized review. + pub(crate) fn id(&self) -> &str { + match &self.op { + Op::Command(spec) => spec.id, + Op::Plugin(c, _) => &c.id, + Op::Tool(pin) => pin.spec().id, + Op::Adapter(what) => what, + } + } + /// Rejects a withdrawn load or reloaded schema before a new stage begins. + pub(crate) fn current(&self) -> Result<(), String> { + if self.auth.as_ref().is_some_and(|a| !a.is_current()) { + return Err("the active account changed since this operation was approved".into()); + } + if let Some(pin) = &self.fixed_tool { + pin.current()?; + } + match &self.op { + Op::Tool(t) => t.current(), + Op::Plugin(_, p) if !rustgis_plugins::is_current(&p.0) => Err("the plugin changed since this run was approved".into()), + _ => Ok(()), + } + } +} +struct Builder<'a> { + s: &'a Session, + selection: crate::cmd::Selection, + unresolved_transition: bool, + designated: Vec, + sources: Vec<(LayerKey, Value)>, + nodes: usize, + bytes: usize, + stack: Vec<(&'static str, String)>, + strict: bool, + auth: Option>, + symbols: std::collections::BTreeMap<(u64, String), Target>, + outputs: std::collections::BTreeSet<(u64, String)>, + table_outputs: std::collections::BTreeSet, + dataset_outputs: std::collections::BTreeMap, +} +impl Session { + /// Expands a bounded ordered plan and binds project/defaults, namespaces and private identities. + pub(super) fn plan(&self, scope: &Scope, call: &Call) -> Result { + let mut b = Builder { + s: self, + selection: crate::cmd::InputSource::input_context(self).selection(), + unresolved_transition: false, + designated: Vec::new(), + sources: Vec::new(), + nodes: 0, + bytes: 0, + stack: Vec::new(), + strict: true, + auth: None, + symbols: Default::default(), + outputs: Default::default(), + table_outputs: Default::default(), + dataset_outputs: Default::default(), + }; + b.charge(&self.connections)?; + let accounts = self.policy_accounts(); + b.charge(&accounts)?; + let s3 = crate::render::rustgis_tiles::s3::connections(); + for c in &s3 { + b.charge(&( + &c.name, + &c.endpoint, + &c.region, + &c.buckets, + c.credentials.as_ref().map(|k| (&k.access_key_id, &k.secret_access_key, &k.session_token)), + ))?; + } + let root = b.call(scope, call)?; + /// Finds nodes whose execution depends on the initially active or selected layers. + fn defaults(n: &Node) -> bool { + n.targets.iter().any(|t| matches!(t, Target::Default { .. })) || n.children.iter().any(defaults) + } + Ok(Plan { + owner: scope.owner.clone(), + opened: self.opened, + map: self.map().instance_id(), + designated: b.designated, + last_table: self.project.tables.iter().map(|t| t.id).max().unwrap_or(0), + picked: defaults(&root).then(|| (self.active_layer, self.selected_layers())), + names: self + .selected_layers() + .into_iter() + .chain(self.active_layer) + .filter_map(|id| self.map().layer(id)) + .map(|l| (l.name.clone(), l.id)) + .collect(), + sources: b.sources, + accounts, + s3, + root, + }) + } + /// Per-stage resolution/preflight has no approval-card size or project-size limits. + pub(super) fn call_node(&self, scope: &Scope, call: &Call) -> Result { + Builder { + s: self, + selection: crate::cmd::InputSource::input_context(self).selection(), + unresolved_transition: false, + designated: Vec::new(), + sources: Vec::new(), + nodes: 0, + bytes: 0, + stack: Vec::new(), + strict: false, + auth: None, + symbols: Default::default(), + outputs: Default::default(), + table_outputs: Default::default(), + dataset_outputs: Default::default(), + } + .call(scope, call) + } + /// Resolves one dispatch stage; only explicit approval planning expands automation recursively. + pub(super) fn command_node(&self, id: &str, params: &Value) -> Result { + Builder { + s: self, + selection: crate::cmd::InputSource::input_context(self).selection(), + unresolved_transition: false, + designated: Vec::new(), + sources: Vec::new(), + nodes: 0, + bytes: 0, + stack: Vec::new(), + strict: false, + auth: None, + symbols: Default::default(), + outputs: Default::default(), + table_outputs: Default::default(), + dataset_outputs: Default::default(), + } + .command(id, params, 0) + } + /// Resolves a single checked tool stage from its immutable runtime schema. + pub(super) fn tool_node(&self, name: &str, params: &Value) -> Result { + Builder { + s: self, + selection: crate::cmd::InputSource::input_context(self).selection(), + unresolved_transition: false, + designated: Vec::new(), + sources: Vec::new(), + nodes: 0, + bytes: 0, + stack: Vec::new(), + strict: false, + auth: None, + symbols: Default::default(), + outputs: Default::default(), + table_outputs: Default::default(), + dataset_outputs: Default::default(), + } + .tool(name, params) + } +} +impl<'a> Builder<'a> { + /// Borrows live project data with only previously planned scalar map transitions applied. + fn context(&self) -> crate::cmd::InputContext<'a> { + crate::cmd::InputContext::at(self.s, self.selection) + } + /// Records only actual targets/defaults as possible later default sources, never a namespace. + fn capture_source(&mut self, key: LayerKey) -> Result<(), String> { + if !self.sources.iter().any(|(k, _)| *k == key) + && let Some(layer) = key.get(self.s) + { + let source = private_source(layer); + self.charge(&source)?; + self.sources.push((key, source)); + } + Ok(()) + } + /// The same raw layer selection as the executor, qualified by map and incarnation. + fn default_target(&mut self) -> Result { + let s = self.context(); + let active = s.active_layer.and_then(|id| s.map().layer(id)).map(|l| LayerKey::of(s.map(), l)); + let selected = + s.selected_layers().into_iter().filter_map(|id| s.map().layer(id)).map(|l| LayerKey::of(s.map(), l)).collect::>(); + for key in selected.iter().copied().chain(active) { + if !self.designated.contains(&key) { + self.designated.push(key); + } + self.capture_source(key)?; + } + Ok(Target::Default { active, selected }) + } + /// Shares execution parsing and whole-script refusal preflight with the approval planner. + fn call(&mut self, scope: &Scope, call: &Call) -> Result { + Ok(match &call.kind { + CallKind::Command { id, params } => self.command(id, params, 0)?, + CallKind::Script(script) => { + self.charge(script)?; + let mut node = self.adapter("script", Effects::NONE, json!({"script":script})); + node.children = self.script(script, 0)?; + if node.children.len() > 1 + && node.children.iter().any(|n| n.effects.contains(Effect::History)) + && matches!(scope.kind, super::scope::Kind::Agent { access, .. } if access != super::AgentAccess::Full) + { + return Err("undo and redo run alone, so an agent takes back only its own top step".into()); + } + node + } + CallKind::Adapter { what, effects, params } => { + self.charge(params)?; + let effects = if matches!(*what, "snapshot" | "view_map") { + effects.union(crate::cmd::tiles::render_reach(self.context().map())) + } else { + *effects + }; + let mut node = self.adapter(what, effects, params.clone()); + self.bind(&mut node, params, None)?; + node + } + }) + } + /// Charges borrowed content before freezing copies, including repeatedly expanded items. + fn charge(&mut self, value: &impl serde::Serialize) -> Result<(), String> { + if self.strict { + self.bytes += bounded_size(value, MAX_PLAN_BYTES.saturating_sub(self.bytes))?; + } + Ok(()) + } + /// Bounds expanded operations and nesting before traversing another composite. + fn count(&mut self, depth: usize) -> Result<(), String> { + if self.strict && self.unresolved_transition { + return Err("a later step follows a map change whose targets cannot be frozen; split the request or run it yourself".into()); + } + self.nodes += 1; + if self.strict && (self.nodes > MAX_NODES || depth > MAX_DEPTH) { + return Err("this request has too many steps or nested items; ask for a smaller request".into()); + } + Ok(()) + } + /// Constructs a node with no implicit targets or hidden children. + fn adapter(&self, what: &'static str, effects: Effects, params: Value) -> Node { + Node { + context: self.context().map().instance_id(), + line: None, + op: Op::Adapter(what), + params, + effects, + targets: Vec::new(), + places: Vec::new(), + display: ReviewDetail { operation: String::new(), targets: Vec::new(), places: Vec::new() }, + children: Vec::new(), + item: None, + generated: false, + number: self.nodes as u16, + wires: Vec::new(), + graph_output: false, + fixed_tool: None, + auth: None, + } + } + /// Parses each line through the execution parser and preserves its order in the plan. + fn script(&mut self, text: &str, depth: usize) -> Result, String> { + crate::cmd::workflows::script_commands(text)? + .into_iter() + .map(|(line, id, p)| { + let mut node = self.command(&id, &p, depth + 1).map_err(|e| format!("line {line} ({id}): {e}"))?; + node.line = Some(line); + Ok(node) + }) + .collect() + } + /// Captures a registration or plugin command, then expands only reviewed composite vehicles. + fn command(&mut self, id: &str, params: &Value, depth: usize) -> Result { + self.count(depth)?; + self.charge(params)?; + if id.starts_with(rustgis_plugins::PREFIX) { + let c = rustgis_plugins::command(id).ok_or_else(|| format!("unknown command or disabled plugin command '{id}'"))?; + let pin = PluginPin(c.plugin.clone()); + let effects = + if c.mutates() { Effects::NONE.with(Effect::Plugin).with(Effect::Edits) } else { Effects::NONE.with(Effect::Plugin) }; + let mut n = self.adapter("plugin", effects, params.clone()); + n.op = Op::Plugin(c, pin); + self.bind(&mut n, params, None)?; + return Ok(n); + } + let spec = *self.s.registry.get(id).ok_or_else(|| format!("unknown command '{id}'"))?; + if self.strict && self.selection.names_changed && (spec.inputs.is_some() || params.get("map").is_some()) { + return Err( + "a later map selector follows a rename whose namespace cannot be frozen; split the request or run it yourself".into() + ); + } + let effects = super::effects::command(&self.context(), &spec, params, self.primary_layer(params)); + let mut n = self.adapter("command", effects, params.clone()); + n.op = Op::Command(spec); + self.bind(&mut n, params, None)?; + match id { + "gp.bufferPoint" => n.fixed_tool = Some(ToolPin::resolve("buffer")?), + "gp.run" => { + let tool = params.get("tool").and_then(Value::as_str).ok_or("tool is required")?; + n.children.push(self.tool(tool, params.get("params").unwrap_or(&Value::Null))?); + } + "gp.spatialJoin" => n.children.push(self.tool("spatialJoin", params)?), + "gp.zonalStats" => n.children.push(self.tool("zonalStats", params)?), + "script.run" if self.strict => { + n.children = self.script(params.get("script").and_then(Value::as_str).ok_or("script is required")?, depth)? + } + "task.run" | "notebook.run" | "model.run" if self.strict => self.item(&mut n, id, params, depth)?, + "project.importQgis" | "project.importGeolibre" | "map.import" => n.generated = true, + _ => {} + } + // Rename changes a spelling, not the approved object's identity. Later named steps + // resolve the new spelling to the same id, without simulating the handler's edits. + if id == "layer.rename" + && let Some(name) = params.get("name").and_then(Value::as_str) + { + let target = n + .targets + .iter() + .find(|t| matches!(t, Target::Layer { .. } | Target::MadeLayer { .. })) + .cloned() + .or_else(|| self.context().active_layer.map(|id| self.layer_target(id, false))); + if let Some(target) = target { + let spelling = name.trim().chars().take(200).collect::(); + if self.strict + && (self.symbol(&spelling).is_some_and(|other| !Self::same_layer_target(&target, other)) + || self.context().map().layers.iter().any(|layer| { + layer.name.eq_ignore_ascii_case(&spelling) + && !Self::same_layer_target( + &target, + &Target::Layer { key: LayerKey::of(self.context().map(), layer), source: None }, + ) + })) + { + return Err("a layer rename would share another layer's name; choose a unique name before asking for approval".into()); + } + let target = match target { + Target::MadeLayer { .. } => Target::MadeLayer { map: self.context().map().instance_id(), name: spelling.clone() }, + other => other, + }; + self.symbols.insert((self.context().map().instance_id(), spelling.to_ascii_lowercase()), target); + } + } + if self.strict + && matches!(id, "layer.addData" | "layer.addDataText" | "layer.addDatabase" | "layer.addSql" | "layer.addEncodedPolyline") + && let Some(name) = params.get("name").and_then(Value::as_str).filter(|n| !n.trim().is_empty()) + { + self.outputs.insert((self.context().map().instance_id(), name.trim().to_ascii_lowercase())); + self.table_outputs.insert(name.trim().to_ascii_lowercase()); + } + if self.strict + && let Some(transition) = spec.transition + { + match transition(&self.context(), params)? { + Some(selection) => self.selection = selection, + None => self.unresolved_transition = true, + } + } + Ok(n) + } + /// Captures a tool without symbolic wiring. + fn tool(&mut self, name: &str, params: &Value) -> Result { + self.tool_wired(name, params, Vec::new()) + } + /// Freezes effective defaults and explicit in-run output references before binding inputs. + fn tool_wired(&mut self, name: &str, params: &Value, wires: Vec) -> Result { + self.count(0)?; + let pin = ToolPin::resolve(name)?; + let params = pin.resolved.resolve_params(params)?; + self.charge(¶ms)?; + let mut effects = Effects::NONE.with(Effect::Edits); + if pin.plugin.is_some() { + effects = effects.with(Effect::Plugin); + } + if matches!(pin.resolved.source(), Some("geolibre")) + || crate::cmd::gp::reads_files(pin.spec()) + || crate::cmd::gp::writes_files(pin.spec()) + { + effects = effects.with(Effect::Files); + } + if pin.resolved.source().is_some_and(|s| s != "geolibre" && s != rustgis_plugins::tools::SOURCE) { + effects = effects.with(Effect::Unreviewed); + } + // Source effects follow the same whole-read decision as the executor, resolving + // prior renames by their bound identity in fast preflight and strict review alike. + for name in crate::cmd::gp::layer_names(Some(pin.spec()), ¶ms) { + if let Some(layer) = self.named_layer(name).filter(|layer| crate::cmd::gp::reads_full_source(layer, Some(pin.spec()))) + && let Some(source) = &layer.source + { + effects = effects.union(super::effects::source_reach(source)); + } + } + let mut n = self.adapter("tool", effects, params.clone()); + n.wires = wires; + self.bind(&mut n, ¶ms, Some(pin.spec()))?; + if !crate::cmd::gp::writes_files(pin.spec()) + && let Some(name) = params.get("output").and_then(Value::as_str).filter(|n| !n.trim().is_empty()) + { + self.outputs.insert((self.context().map().instance_id(), name.trim().to_ascii_lowercase())); + self.table_outputs.insert(name.trim().to_ascii_lowercase()); + self.dataset_outputs.insert(name.trim().to_ascii_lowercase(), (n.number, self.context().map().instance_id())); + } + n.op = Op::Tool(pin); + n.display.operation = n.id().to_string(); + Ok(n) + } + /// Binds dataset identities through executor resolvers and keeps private places separate from review. + fn bind(&mut self, n: &mut Node, params: &Value, tool: Option<&rustgis_analysis::ToolSpec>) -> Result<(), String> { + let reads = n.effects.iter().any(|e| matches!(e, Effect::Files | Effect::Network | Effect::Plugin)); + let session = self.s; + if reads { + if self.auth.is_none() { + self.auth = Some(Arc::new(super::scope::RuntimeAccounts::capture(session)?)); + } + n.auth = self.auth.clone(); + self.charge(&session.connections)?; + } + if let Some(spec) = tool { + for p in spec.params { + if n.wires.iter().any(|w| w.key == p.name) { + continue; + } + use rustgis_analysis::ParamKind::*; + if matches!(p.kind, Layer | PolygonLayer | PointLayer | VectorLayer | RasterLayer | VectorLayers | RasterLayers) { + let names: Vec<&str> = match params.get(p.name) { + Some(Value::String(v)) => v.split(';').map(str::trim).filter(|s| !s.is_empty()).collect(), + Some(Value::Array(v)) => { + if self.strict && v.iter().any(|v| !v.is_string()) { + return Err("layer inputs must be names or file paths".into()); + } + v.iter().filter_map(Value::as_str).collect() + } + Some(Value::Null) | None => Vec::new(), + _ if self.strict => return Err("layer inputs must be names or file paths".into()), + _ => Vec::new(), + }; + for name in names { + if let Some(target) = self.symbol(name) { + n.targets.push(self.bound_symbol(target, reads)); + } else if matches!(p.kind, VectorLayers) && crate::cmd::gp::vector_file_input(spec, self.context().map(), name) { + n.places.push(json!({p.name:name})); + } else if let Some(input) = crate::cmd::gp::project_input( + self.context().map(), + &self.s.project.tables, + name, + matches!(p.kind, RasterLayer | RasterLayers), + ) { + match input { + crate::cmd::gp::ProjectInput::Layer(l) => n.targets.push(Target::Layer { + key: LayerKey::of(self.context().map(), l), + source: reads.then(|| private_source(l)), + }), + crate::cmd::gp::ProjectInput::Table(t) => { + n.targets.push(Target::Table { id: t.id, source: reads.then(|| json!(t.dataset.source)) }) + } + } + } else if matches!(p.kind, VectorLayers) || spec.id.starts_with("geolibre:") { + // These schemas explicitly accept files as well as project datasets. + // Keep the original path as private equality data, never a guessed layer. + n.places.push(json!({p.name:name})); + } else if let Some(&(producer, map)) = self.dataset_outputs.get(&name.to_ascii_lowercase()) { + n.targets.push(Target::ProducedDataset { producer, map, name: name.into() }); + } else if self.strict && !self.outputs.contains(&(self.context().map().instance_id(), name.to_ascii_lowercase())) { + return Err(format!("no layer or table named '{name}' and no earlier output")); + } else { + n.targets.push(Target::MadeLayer { map: self.context().map().instance_id(), name: name.into() }); + } + } + } + } + } else { + // Image adapters consume the same map and raw tile routes as registered exports. + // Their caller-supplied effects can add reach, but cannot remove these owner inputs. + let selected = match &n.op { + Op::Command(spec) => spec.inputs.map(|select| select(&self.context(), params)), + Op::Adapter("snapshot" | "view_map") => Some(Ok(crate::cmd::tiles::render_inputs(self.context().map()))), + _ => None, + }; + let owner_selected = selected.is_some(); + if let Some(inputs) = selected { + let inputs = match inputs { + Ok(inputs) => inputs, + Err(_) if !self.strict => crate::cmd::Inputs::unresolved(), + Err(e) => return Err(e), + }; + if self.strict && inputs.unresolved { + return Err("this operation's targets cannot be resolved before reading its file; run it yourself".into()); + } + self.charge(&(&inputs.layers, &inputs.maps, &inputs.tables, &inputs.future_layers))?; + self.charge(&inputs.state)?; + n.places.push(inputs.state); + n.display.places.extend(inputs.review_places); + let uses_default = inputs.uses_default; + for name in inputs.future_layers { + if let Some(target) = self.symbol(&name) { + // Owner callbacks keep the same map-qualified representation after an in-run rename. + n.targets.push(match self.bound_symbol(target, reads) { + Target::Layer { key, source } => { + Target::LayerInMap { map: key.map, id: key.id, instance: key.instance, source } + } + other => other, + }); + } else if self.outputs.contains(&(self.context().map().instance_id(), name.to_ascii_lowercase())) { + n.targets.push(Target::MadeLayer { map: self.context().map().instance_id(), name }); + } else if self.strict { + return Err(format!("no layer '{name}' and no earlier output")); + } + } + for id in inputs.tables { + let table = self.s.project.tables.iter().find(|t| t.id == id).ok_or("an input table disappeared")?; + n.targets.push(Target::Table { id, source: reads.then(|| json!(table.dataset.source)) }); + } + n.targets.extend(inputs.maps.into_iter().map(Target::Map)); + for (map, id) in inputs.layers { + let layer = self + .s + .project + .maps + .iter() + .find(|m| m.instance_id() == map) + .and_then(|m| m.layer(id)) + .ok_or("an input disappeared")?; + n.targets.push(Target::LayerInMap { + map, + id, + instance: layer.instance_id(), + source: reads.then(|| private_source(layer)), + }); + } + if uses_default { + n.targets.push(self.default_target()?); + } + } else { + if crate::cmd::layer::external_dataset_selector(n.id()) { + // This is a dataset/sheet inside the frozen external origin, even when empty. + n.places.push(json!({"dataset": params.get("layer")})); + n.targets.push(self.default_target()?); + } else { + match params.get("layer") { + Some(Value::Array(v)) => { + // Reuse the command owner's list resolver, including hostile/null semantics. + match crate::cmd::layer::layer_ids(&self.context(), params) { + Ok(ids) => { + for id in ids { + n.targets.push(self.layer_target(id, reads)); + } + } + Err(_) => { + for v in v { + n.targets.push(self.layer_target_value(v, reads)?); + } + } + } + } + Some(v @ (Value::String(_) | Value::Number(_))) => n.targets.push(self.layer_target_value(v, reads)?), + _ => n.targets.push(self.default_target()?), + } + } + } + if !owner_selected && matches!(params.get("table"), Some(Value::String(_) | Value::Number(_))) { + match self.s.table_index(params) { + Ok(i) => { + if let Some(t) = self.s.project.tables.get(i) { + n.targets.push(Target::Table { id: t.id, source: reads.then(|| json!(t.dataset.source)) }); + } + } + Err(e) + if self.strict + && !params + .get("table") + .and_then(Value::as_str) + .is_some_and(|n| self.table_outputs.contains(&n.to_ascii_lowercase())) => + { + return Err(e); + } + Err(_) => { + if let Some(name) = params.get("table").and_then(Value::as_str) { + n.targets.push(Target::MadeTable(name.into())); + } + } + } + } + } + for target in &n.targets { + match target { + Target::Layer { key, source } => self.charge(&(key.map, key.id, key.instance, source))?, + Target::LayerInMap { map, id, instance, source } => self.charge(&(map, id, instance, source))?, + Target::Table { id, source } => self.charge(&(id, source))?, + Target::Default { active, selected } => self.charge(&( + active.map(|k| (k.map, k.id, k.instance)), + selected.iter().map(|k| (k.map, k.id, k.instance)).collect::>(), + ))?, + Target::MadeLayer { map, name } => self.charge(&(map, name))?, + Target::MadeTable(name) => self.charge(name)?, + Target::ProducedDataset { producer, map, name } => self.charge(&(producer, map, name))?, + Target::Map(map) => self.charge(map)?, + Target::TimeSources(sources) => self.charge(sources)?, + } + let key = match target { + Target::Layer { key, .. } => Some(*key), + Target::LayerInMap { map, id, instance, .. } => Some(LayerKey { map: *map, id: *id, instance: *instance }), + _ => None, + }; + if let Some(key) = key { + self.capture_source(key)?; + } + } + if n.id() == "locator.find" { + let path = crate::cmd::sharing::locator_path(self.s, params)?; + n.display.places.push(path.clone()); + n.places.push(json!({"locator":path})); + } + if crate::cmd::time::follows_sources(n.id()) { + n.targets.push(Target::TimeSources( + crate::cmd::time::following_sources(self.s).into_iter().map(|(m, l, s)| (m, l, s.into())).collect(), + )); + } + if reads { + if let Some(o) = params.as_object() { + for (k, v) in o { + if super::effects::PLACE_KEYS.contains(&k.as_str()) && !n.wires.iter().any(|w| &w.key == k) { + n.places.push(json!({k:v})); + if let Some(place) = v.as_str() { + n.display.places.push(place.to_string()); + } + } + } + } + // Saved definitions and live bindings are private equality data, never Review data. + n.places.push(serde_json::to_value(&self.s.connections).map_err(|e| e.to_string())?); + } + n.display.operation = n.id().to_string(); + for target in &n.targets { + match target { + Target::Layer { .. } | Target::LayerInMap { .. } => { + let key = match target { + Target::Layer { key, .. } => *key, + Target::LayerInMap { map, id, instance, .. } => LayerKey { map: *map, id: *id, instance: *instance }, + _ => continue, + }; + if let Some(map) = self.s.project.maps.iter().find(|m| m.instance_id() == key.map) + && let Some(layer) = key.get(self.s) + { + n.display.targets.push(format!("{} / {}", map.name, layer.name)); + } + } + Target::Table { id, .. } => { + if let Some(table) = self.s.project.tables.iter().find(|t| t.id == *id) { + n.display.targets.push(table.dataset.name.clone()); + } + } + Target::Map(id) => { + if let Some(map) = self.s.project.maps.iter().find(|m| m.instance_id() == *id) { + n.display.targets.push(map.name.clone()); + } + } + Target::MadeLayer { name, .. } | Target::MadeTable(name) | Target::ProducedDataset { name, .. } => { + n.display.targets.push(name.clone()) + } + Target::TimeSources(sources) => { + for (map_id, layer_id, _) in sources { + if let Some(map) = self.s.project.maps.iter().find(|m| m.instance_id() == *map_id) + && let Some(layer) = map.layer(*layer_id) + { + n.display.targets.push(format!("{} / {}", map.name, layer.name)); + } + } + } + _ => {} + } + } + for value in n.display.targets.iter_mut().chain(n.display.places.iter_mut()) { + *value = rustgis_data::diagnostics::Redacted::new(value).into_string(); + } + n.display.targets.dedup(); + n.display.places.dedup(); + Ok(()) + } + /// Compares proven layer identity without treating source metadata as identity. + fn same_layer_target(a: &Target, b: &Target) -> bool { + let key = |target: &Target| match target { + Target::Layer { key, .. } => Some(*key), + Target::LayerInMap { map, id, instance, .. } => Some(LayerKey { map: *map, id: *id, instance: *instance }), + _ => None, + }; + match (key(a), key(b)) { + (Some(a), Some(b)) => a == b, + _ => matches!((a, b), (Target::MadeLayer { map: a, name: an }, Target::MadeLayer { map: b, name: bn }) + if a == b && an.eq_ignore_ascii_case(bn)), + } + } + /// Map-qualified planned spellings match the executor's ASCII case-insensitive lookup. + fn symbol(&self, name: &str) -> Option<&Target> { + self.symbols.get(&(self.context().map().instance_id(), name.to_ascii_lowercase())) + } + /// Literal names retain the executor's map-layer precedence; known future outputs never + /// fall back to a same-named old layer as if they already had a source. + fn named_layer(&self, name: &str) -> Option<&rustgis_map::Layer> { + match self.symbol(name) { + Some(Target::Layer { key, .. }) => key.get(self.s), + Some(Target::LayerInMap { map, id, instance, .. }) => LayerKey { map: *map, id: *id, instance: *instance }.get(self.s), + Some(_) => None, + None => self.context().map().layer_by_name(name), + } + } + /// Uses the execution owner's name-before-id/default selector, with prior bound renames. + fn primary_layer(&self, params: &Value) -> Option<&rustgis_map::Layer> { + if let Some(name) = params.get("layer").and_then(Value::as_str) + && self.symbol(name).is_some() + { + return self.named_layer(name); + } + self.context().layer_id(params).ok().and_then(|id| self.context().map().layer(id)) + } + /// Uses Session's real name-before-id and numeric/default selector semantics. + fn layer_target_value(&self, value: &Value, reads: bool) -> Result { + if let Some(t) = value.as_str().and_then(|name| self.symbol(name)) { + return Ok(self.bound_symbol(t, reads)); + } + match self.context().layer_id(&json!({"layer":value})) { + Ok(id) => Ok(self.layer_target(id, reads)), + Err(error) + if self.strict + && !value + .as_str() + .is_some_and(|n| self.outputs.contains(&(self.context().map().instance_id(), n.to_ascii_lowercase()))) => + { + Err(error) + } + Err(_) => value + .as_str() + .map(|n| Target::MadeLayer { map: self.context().map().instance_id(), name: n.into() }) + .ok_or("unresolved layer target".into()), + } + } + /// An approved rename retains the original incarnation and adds its source when later read. + fn bound_symbol(&self, target: &Target, reads: bool) -> Target { + match target { + Target::Layer { key, .. } => Target::Layer { key: *key, source: reads.then(|| key.get(self.s).map(private_source)).flatten() }, + other => other.clone(), + } + } + /// Edit-only targets still bind map/id/incarnation; source identity is additive for external reach. + fn layer_target(&self, id: u32, reads: bool) -> Target { + let map = self.context().map(); + Target::Layer { + key: LayerKey { map: map.instance_id(), id, instance: map.layer(id).map_or(0, |l| l.instance_id()) }, + source: reads.then(|| map.layer(id).map(private_source)).flatten(), + } + } + /// Freezes selected task/cell/model content and provenance with bounded cycle-aware expansion. + fn item(&mut self, n: &mut Node, id: &str, params: &Value, depth: usize) -> Result<(), String> { + let kind = id.split('.').next().unwrap_or(""); + let kind: &'static str = match kind { + "task" => "task", + "notebook" => "notebook", + _ => "model", + }; + let name = self.s.item_name(kind, params)?; + let session = self.s; + match kind { + "task" => self.charge(&crate::cmd::items::task_ref(session, params)?.steps)?, + "notebook" => { + let nb = session.project.notebooks.get(crate::cmd::notebook::index(session, params)?).ok_or("no such notebook")?; + for cell in &nb.cells { + self.charge(&(&cell.kind, &cell.source))?; + } + } + _ if params.get("graph").is_none_or(Value::is_null) => { + let model = session.project.models.iter().find(|m| m.name.eq_ignore_ascii_case(&name)).ok_or("no such model")?; + self.charge(model)?; + } + _ => {} + } + let ad_hoc = kind == "model" && params.get("graph").is_some_and(|g| !g.is_null()); + let content = if ad_hoc { + params.get("graph").cloned().unwrap_or(Value::Null) + } else { + self.s.item_content(kind, &name).ok_or("the item no longer exists")? + }; + let trusted = ad_hoc || self.s.item_trusted(kind, &name); + if self.stack.iter().any(|(k, n)| *k == kind && n.eq_ignore_ascii_case(&name)) { + return Err("this request contains an item cycle".into()); + } + self.stack.push((kind, name.clone())); + n.item = Some((kind, name.clone(), trusted, content)); + match kind { + "task" => { + let t = crate::cmd::items::task_ref(self.s, params)?; + for (i, st) in t + .steps + .iter() + .enumerate() + .filter(|(i, _)| params.get("step").filter(|v| !v.is_null()).is_none_or(|v| v.as_u64() == Some(*i as u64))) + { + if st.command.starts_with("task.") { + return Err(format!("step {} would run another task", i + 1)); + } + n.children.push(self.command(&st.command, &st.params, depth + 1)?); + } + } + "notebook" => { + let i = crate::cmd::notebook::index(self.s, params)?; + let nb = self.s.project.notebooks.get(i).ok_or("no notebook")?; + if nb.language().is_none() { + for (i, c) in + nb.cells.iter().enumerate().filter(|(i, _)| params.get("cell").is_none_or(|v| v.as_u64() == Some(*i as u64))) + { + let _ = i; + if c.kind == crate::cmd::notebook::CellKind::Code { + n.children.extend(self.script(&c.source, depth + 1)?); + } + } + } + } + _ => { + let stored = (!ad_hoc).then(|| self.s.project.models.iter().find(|m| m.name.eq_ignore_ascii_case(&name))).flatten(); + if let Some(model) = stored.filter(|m| m.graph.is_none()) { + let mut outputs = Vec::new(); + for step in &model.steps { + let pin = ToolPin::resolve(&step.tool)?; + let mut p = step.params.clone(); + let mut wires = Vec::new(); + if let Some(o) = p.as_object_mut() { + for (key, v) in o { + if let Some(index) = v.as_str().and_then(|t| t.strip_prefix('%')).and_then(|n| n.parse::().ok()) { + let layer = pin.spec().params.iter().any(|p| p.name == key && layer_kind(p.kind)); + if !layer { + return Err( + "a model's dynamic external target cannot be shown before it runs; run the model yourself" + .into(), + ); + } + let from = outputs + .get(index.wrapping_sub(1)) + .copied() + .ok_or("a model references an output that has not run")?; + wires.push(Wire { key: key.clone(), from: vec![OutputRef::Node(from)] }); + } else if dynamic(v, 0) { + return Err("a model's nested dynamic target cannot be resolved for approval".into()); + } + } + } + let child = self.tool_wired(&step.tool, &p, wires)?; + outputs.push(child.number); + n.children.push(child); + } + } else { + let (_, g, _) = crate::cmd::workflows::model_graph(self.s, params)?; + let order = crate::model::topological_order(&g).ok_or("the model contains a loop")?; + let mut outputs = std::collections::HashMap::::new(); + for i in order { + let node = g.nodes.get(i).ok_or("missing model node")?; + use crate::project::ModelNodeKind; + match node.kind { + ModelNodeKind::Input => { + let name = node.layer.as_deref().ok_or("model input has no layer")?; + let from = if let Some(l) = self.context().map().layer_by_name(name) { + OutputRef::ExistingLayer(LayerKey::of(self.context().map(), l)) + } else if let Some(t) = + crate::project::find_table(&self.s.project.tables, name).and_then(|i| self.s.project.tables.get(i)) + { + OutputRef::ExistingTable(t.id) + } else { + return Err(format!("no model input layer or table '{name}'")); + }; + outputs.insert(node.id.clone(), from); + } + ModelNodeKind::Output => { + let edge = g + .edges + .iter() + .find(|e| e.to == node.id && e.to_port == crate::model::IN) + .ok_or("model output has no input")?; + let from = outputs.get(&edge.from).cloned().ok_or("model output's source is unresolved")?; + outputs.insert(node.id.clone(), from); + } + ModelNodeKind::Tool => { + let tool = node.tool.as_deref().ok_or("model tool missing")?; + let pin = ToolPin::resolve(tool)?; + let mut p = match &node.params { + Value::Object(o) => Value::Object(o.clone()), + _ => json!({}), + }; + let mut wires = Vec::new(); + for param in pin.spec().params.iter().filter(|p| layer_kind(p.kind)) { + let edges = g.edges.iter().filter(|e| e.to == node.id && e.to_port == param.name).collect::>(); + if edges.is_empty() { + continue; + } + let from = edges + .iter() + .map(|e| { + outputs.get(&e.from).cloned().ok_or_else(|| "the model's wired input is unresolved".to_string()) + }) + .collect::, _>>()?; + p[param.name] = json!("[output of an earlier model node]"); + wires.push(Wire { key: param.name.into(), from }); + } + let mut child = self.tool_wired(tool, &p, wires)?; + child.graph_output = true; + outputs.insert(node.id.clone(), OutputRef::Node(child.number)); + n.children.push(child); + } + } + } + } + } + } + self.stack.pop(); + Ok(()) + } +} +impl Node { + /// Checks every known child before a script's first irreversible operation, using one engine table. + pub(super) fn decision(&self, s: &Session, scope: &Scope) -> Result { + /// Visits ordered children under their content-bound item restriction. + fn visit(s: &Session, scope: &Scope, n: &Node) -> Result { + let own = decide::decide(s, scope, n); + let mut d = Decision::Allow; + let scope = if let Some((kind, name, false, _)) = &n.item { + let mut scope = scope.clone(); + scope.kind = super::scope::Kind::Item { kind, name: name.clone(), within: Box::new(scope.kind) }; + scope + } else { + scope.clone() + }; + // Report the actual pinned child operation before its vehicle's broad effects. + // Both decisions still finish before the vehicle's first effect. + for c in &n.children { + let child = + visit(s, &scope, c).map_err(|e| c.line.map_or_else(|| e.clone(), |line| format!("line {line} ({}): {e}", c.id())))?; + d = d.max(child); + } + Ok(d.max(own?)) + } + visit(s, scope, self) + } +} +impl Plan { + /// Exact approval uses the same decision as unbounded allowed-path preflight. + pub(super) fn decision(&self, s: &Session, scope: &Scope) -> Result { + self.root.decision(s, scope) + } + /// Produces a bounded sanitized display copy; raw equality data remains private. + pub(super) fn review(&self, remember: bool) -> Result { + /// Refuses parameters too large to present before constructing an approval card. + fn raw_size(n: &Node) -> usize { + n.params.to_string().len().saturating_add(n.children.iter().map(raw_size).sum::()) + } + if raw_size(&self.root) > MAX_REVIEW { + return Err("too long to show in full; run it yourself or ask for a smaller request".into()); + } + /// Lists every asking node and counts project-only edits without exposing private bindings. + fn shown(n: &Node, steps: &mut Vec, edits: &mut usize, details: &mut Vec) -> Result<(), String> { + if !n.display.targets.is_empty() || !n.display.places.is_empty() { + details.push(n.display.clone()); + } + if n.effects.iter().any(|e| !matches!(e, Effect::Edits | Effect::History)) { + let mut line = format!("{} {}", n.id(), crate::project_log::review_params(&n.params)?); + if let Op::Plugin(_, p) = &n.op { + line.push_str(&format!( + " — {} {:?}, SHA-256 {}, permissions {:?}", + crate::project_log::review_params(&json!(p.0.manifest.name))?, + p.0.manifest.runtime, + p.0.sha256, + p.0.manifest.permissions + )); + } + if let Op::Tool(t) = &n.op + && let Some(p) = &t.plugin + { + line.push_str(&format!( + " — {} {:?}, SHA-256 {}, permissions {:?}", + crate::project_log::review_params(&json!(p.0.manifest.name))?, + p.0.manifest.runtime, + p.0.sha256, + p.0.manifest.permissions + )); + } + steps.push(line); + } else { + *edits += 1; + } + for c in &n.children { + shown(c, steps, edits, details)?; + } + Ok(()) + } + let mut steps = Vec::new(); + let mut edits = 0; + let mut details = Vec::new(); + shown(&self.root, &mut steps, &mut edits, &mut details)?; + let defaults = self.picked.as_ref().map(|(active, selection)| { + let name = |id: &u32| { + rustgis_data::diagnostics::Redacted::new( + self.names.iter().find(|(_, n)| n == id).map(|(name, _)| name.as_str()).unwrap_or("(missing layer)"), + ) + .into_string() + }; + (active.as_ref().map(name), selection.iter().map(name).collect()) + }); + let what = format!("{} {}", self.root.id(), crate::project_log::review_params(&self.root.params)?); + /// Finds the strongest reason across all frozen steps, including nested tools. + fn all_effects(n: &Node) -> Effects { + n.children.iter().fold(n.effects, |e, c| e.union(all_effects(c))) + } + let explanation = self.root.explanation(all_effects(&self.root)); + let mut why = explanation.0.to_string(); + for (key, value) in &explanation.1 { + why = why.replace(&format!("{{{key}}}"), value); + } + // Both reason-only callers and translated cards use the same frozen script locations. + let lines = self + .root + .children + .iter() + .filter_map(|n| { + n.line + .filter(|_| n.effects.iter().any(|e| !matches!(e, Effect::Edits | Effect::History))) + .map(|line| (line, n.id().to_string())) + }) + .collect::>(); + if !lines.is_empty() { + let locations = lines.iter().map(|(line, id)| format!("line {line} ({id})")).collect::>(); + why = format!("{}: {why}", locations.join("; ")); + } + if bounded_size(&(&what, &why, &steps, &details, &defaults, &explanation, &lines), MAX_REVIEW).is_err() { + return Err("too long to show in full; run it yourself or ask for a smaller request".into()); + } + Ok(Review { what, why, steps, remember, edits, defaults, explanation, details, lines }) + } +} + +/// Identifies schema roles that can carry model output wires. +fn layer_kind(kind: rustgis_analysis::ParamKind) -> bool { + use rustgis_analysis::ParamKind::*; + matches!(kind, Layer | PolygonLayer | PointLayer | VectorLayer | RasterLayer | VectorLayers | RasterLayers) +} +/// Rejects nested/external interpolation that cannot be resolved to a dataset wire. +fn dynamic(v: &Value, depth: usize) -> bool { + if depth > 8 { + return true; + } + match v { + Value::String(t) => t.strip_prefix('%').is_some_and(|n| n.parse::().is_ok()), + Value::Array(a) => a.iter().any(|v| dynamic(v, depth + 1)), + Value::Object(o) => o.values().any(|v| dynamic(v, depth + 1)), + _ => false, + } +} + +/// Captures live source identity, including streamed and raster locations, without redaction. +pub(super) fn private_source(l: &rustgis_map::Layer) -> Value { + json!({"data":l.dataset.source,"streamed":l.source.as_ref().map(|s|(&s.path,&s.table,&s.gdal,&s.duckdb,&s.postgis,&s.mysql,&s.sqlserver)),"raster":l.raster.as_ref().and_then(|r|r.path.as_ref()), + "zarr":l.raster.as_ref().and_then(|r|r.time_zarr.as_ref()).map(|z|(&z.store,&z.variable,&z.index,&z.dim,z.offset,&z.crs)), + "postgis":l.postgis.as_ref().map(|p|(&p.origin,&p.key,p.srid,p.writable,&p.condition,p.limit)), + "gpkg":l.gpkg.as_ref().map(|p|(&p.path,&p.table,&p.key,&p.geometry,p.srs_id,&p.geometry_type,p.z,p.m,&p.revision,p.writable,p.limit))}) +} +impl Session { + /// Captures only already available connection state; never reads the secret store. + pub(super) fn policy_accounts(&self) -> Value { + json!({"connections": + self.connections + .connections + .iter() + .map(|c| json!({"definition":c,"password":self.database_logins.known_password(&c.id).map(|p|p.expose())})) + .collect::>(), + "web":{"selected":self.database_logins.web_selections(),"active":self.web_logins.active_ids()} + }) + } +} + +/// Projects pinned tool effects for compatibility consumers; execution still uses its own token. +pub(super) fn tool_effect_node(name: &str, params: &Value) -> Result { + let pin = ToolPin::resolve(name)?; + let _ = pin.resolved.resolve_params(params)?; + let mut e = Effects::NONE.with(Effect::Edits); + if pin.plugin.is_some() { + e = e.with(Effect::Plugin); + } + if pin.resolved.source() == Some("geolibre") || crate::cmd::gp::reads_files(pin.spec()) || crate::cmd::gp::writes_files(pin.spec()) { + e = e.with(Effect::Files); + } + if pin.resolved.source().is_some_and(|s| s != "geolibre" && s != rustgis_plugins::tools::SOURCE) { + e = e.with(Effect::Unreviewed); + } + Ok(e) +} + +impl Node { + /// Preserves the established operation-specific explanation; values are display-only. + fn explanation(&self, effects: Effects) -> (&'static str, Vec<(&'static str, String)>) { + let clean = |v: &str| rustgis_data::diagnostics::Redacted::new(v).into_string(); + match self.id() { + "layer.addSql" | "sql.query" => match self.params.get("database").and_then(Value::as_str).filter(|d| !d.trim().is_empty()) { + Some(d) => ( + "it runs an SQL query, which can read files on this computer and addresses on the internet (as can views in the database {database})", + vec![("database", clean(d))], + ), + None => ("it runs an SQL query, which can read files on this computer and addresses on the internet", vec![]), + }, + "snapshot" if effects.contains(Effect::Files) => ("it writes a file on this computer", vec![]), + "project.importGeolibre" => ("it adds the files and web addresses a GeoLibre project names", vec![]), + "project.importQgis" => ("it adds the files and web addresses a QGIS project names", vec![]), + "layer.addDownload" => ("it completes a download that may import a project and its referenced sources", vec![]), + _ => match &self.op { + Op::Plugin(..) => ("it runs a plugin's command", vec![]), + Op::Tool(pin) if pin.plugin.is_some() => ("it runs a plugin's tool ({tool})", vec![("tool", clean(pin.spec().id))]), + _ => { + for child in &self.children { + let reason = child.explanation(child.effects); + if reason.0 != decide::reason(child.effects) { + return reason; + } + } + (decide::reason(effects), vec![]) + } + }, + } + } +} diff --git a/crates/engine/src/policy/provenance.rs b/crates/engine/src/policy/provenance.rs new file mode 100644 index 0000000000..5f87c97152 --- /dev/null +++ b/crates/engine/src/policy/provenance.rs @@ -0,0 +1,109 @@ +use crate::Session; +use serde_json::{Value, json}; +use std::{collections::HashMap, sync::Arc}; + +/// Exact executable content last authored by the user. In memory, never persisted or logged. +pub(crate) type Trust = HashMap>; +impl Session { + /// Whether this item's executable content still equals its user-authored record. + pub fn item_trusted(&self, kind: &str, name: &str) -> bool { + let records = match kind { + "task" => &self.trusted_tasks, + "notebook" => &self.trusted_notebooks, + _ => &self.trusted_models, + }; + records.get(&name.to_ascii_lowercase()).zip(self.item_content(kind, name)).is_some_and(|(a, b)| a.as_ref() == &b) + } + /// Snapshots only executable content; generated notebook output never confers trust. + pub(crate) fn item_content(&self, kind: &str, name: &str) -> Option { + match kind { + "task" => self.project.tasks.iter().find(|t| t.name.eq_ignore_ascii_case(name)).map(|t| json!(t.steps)), + "notebook" => self.project.notebooks.iter().find(|n| n.name.eq_ignore_ascii_case(name)).map(|n| { + json!({"language":n.language(),"cells":n.cells.iter().map(|c|json!({"kind":c.kind,"source":c.source})).collect::>()}) + }), + _ => self.project.models.iter().find(|m| m.name.eq_ignore_ascii_case(name)).map(|m| json!({"steps":m.steps,"graph":m.graph})), + } + } + /// Uses each item executor's own selector, including active/default items. + pub(crate) fn item_name(&self, kind: &str, p: &Value) -> Result { + if kind == "model" && p.get("graph").is_some_and(|g| !g.is_null()) { + return Ok(p.get("name").and_then(Value::as_str).unwrap_or("Model").into()); + } + let given = p.get("name").and_then(Value::as_str); + let name = match kind { + "task" => Some(crate::cmd::items::task_ref(self, p)?.name.clone()), + "notebook" => self.project.notebooks.get(crate::cmd::notebook::index(self, p)?).map(|n| n.name.clone()), + _ => given + .and_then(|name| self.project.models.iter().find(|m| m.name.eq_ignore_ascii_case(name))) + .or_else(|| given.is_none().then(|| self.project.models.last()).flatten()) + .map(|m| m.name.clone()), + }; + name.ok_or_else(|| format!("no {kind} with that name")) + } + /// Records exact user-authored executable content in memory only. + pub(super) fn trust_record(&mut self, kind: &str, name: &str) { + if let Some(content) = self.item_content(kind, name) { + let records = match kind { + "task" => &mut self.trusted_tasks, + "notebook" => &mut self.trusted_notebooks, + _ => &mut self.trusted_models, + }; + records.insert(name.to_ascii_lowercase(), Arc::new(content)); + } + } + /// An editor may refresh only a record that matched before the user edit. + pub(super) fn trust_editor(&self, id: &str, p: &Value) -> Option<(&'static str, String, bool)> { + let kind = match id { + "task.addStep" | "task.editStep" | "task.moveStep" | "task.removeStep" => "task", + "notebook.addCell" | "notebook.editCell" | "notebook.removeCell" => "notebook", + "model.save" => "model", + _ => return None, + }; + let name = if id == "model.save" { + crate::cmd::workflows::model_save_names(p).ok()?.0 + } else { + self.item_name(kind, p).ok().or_else(|| p.get("name").and_then(Value::as_str).map(str::to_string))? + }; + let trusted = self.item_trusted(kind, &name) + || (id == "model.save" && !self.project.models.iter().any(|m| m.name.eq_ignore_ascii_case(&name))); + Some((kind, name, trusted)) + } + /// Records successful User creation/edits; imports and non-user changes cannot promote trust. + pub(super) fn trust_after(&mut self, id: &str, p: &Value, result: &Value, editor: Option<(&'static str, String, bool)>) { + if !self.policy_user() || p.get("imported").and_then(Value::as_bool) == Some(true) { + return; + } + if let Some((kind, _, true)) = editor { + let name = if id == "model.save" { + result.get("model").and_then(Value::as_str).map(str::to_owned) + } else { + self.item_name(kind, p).ok() + }; + if let Some(name) = name { + self.trust_record(kind, &name); + } + } + let kind = match id { + "task.new" => "task", + "notebook.new" => "notebook", + "model.new" => "model", + _ => return, + }; + if p.get("fromHistory").and_then(Value::as_bool) == Some(true) && p.get("steps").is_none() && self.history_from_file > 0 { + return; + } + let name = if kind == "notebook" { + result + .get("notebook") + .and_then(Value::as_u64) + .and_then(|n| usize::try_from(n).ok()) + .and_then(|n| self.project.notebooks.get(n)) + .map(|n| n.name.clone()) + } else { + result.get(kind).and_then(Value::as_str).map(str::to_owned) + }; + if let Some(name) = name { + self.trust_record(kind, &name); + } + } +} diff --git a/crates/engine/src/policy/scope.rs b/crates/engine/src/policy/scope.rs new file mode 100644 index 0000000000..0c5aedcc1e --- /dev/null +++ b/crates/engine/src/policy/scope.rs @@ -0,0 +1,230 @@ +use super::{ + Effects, Review, + plan::{Node, Plan, PluginPin}, +}; +use std::sync::Arc; + +/// Exact in-memory authentication state. Never serialized, displayed or used as a public label. +#[derive(Clone, PartialEq, Eq)] +pub(super) struct RuntimeAccounts { + arcgis: crate::render::rustgis_tiles::arcgis_auth::AccountIdentity, + earthdata: crate::render::rustgis_tiles::earthdata::AccountIdentity, + web: crate::render::rustgis_tiles::logins::AccountIdentity, +} +impl RuntimeAccounts { + /// Dynamic commands may choose multiple URLs, so their entire active stores are bound. + pub(super) fn capture(s: &crate::Session) -> Result { + Ok(Self { + arcgis: crate::render::rustgis_tiles::arcgis_auth::account_identity()?, + earthdata: crate::render::rustgis_tiles::earthdata::account_identity()?, + web: crate::render::rustgis_tiles::logins::account_identity(s.web_logins.session_id())?, + }) + } + /// A rotation/replacement, including renewal, invalidates old consent and held sends. + pub(super) fn is_current(&self) -> bool { + self.arcgis.is_current() && self.earthdata.is_current() && self.web.is_current() + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Principal { + User, + Assistant, + Agent { access: AgentAccess, users_project: bool }, + Plugin { id: String }, + Item { kind: &'static str, name: String }, + Peer, +} +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum AgentAccess { + Refuse, + Review, + Allow, + Full, +} +/// Live authority, checked at dispatch and at the actual start and application of late work. +pub trait Revocation: Send + Sync { + /// Answers from live adapter state at each new start or application, including workers. + fn allows(&self, effects: Effects) -> bool; +} +#[derive(Clone)] +pub struct Scope { + pub(super) kind: Kind, + pub(super) owner: Owner, + pub(super) live: Option>, +} +#[derive(Clone)] +pub(super) enum Kind { + User, + Assistant, + Agent { access: AgentAccess, users_project: bool, actor: Option }, + Plugin { pin: PluginPin, within: Box }, + Item { kind: &'static str, name: String, within: Box }, + Peer, +} +#[derive(Clone, PartialEq, Eq)] +pub(super) struct Owner { + pub kind: &'static str, + pub id: String, +} +impl Scope { + /// Undo attribution follows the initiating agent through plugin/item narrowing. + pub(super) fn actor(&self) -> Option { + fn actor(kind: &Kind) -> Option<&str> { + match kind { + Kind::Agent { actor, .. } => actor.as_deref(), + Kind::Plugin { within, .. } | Kind::Item { within, .. } => actor(within), + _ => None, + } + } + actor(&self.kind).map(str::to_owned) + } + /// Names one assistant conversation and its revocable lifetime. + pub fn assistant(conversation: u64, live: Arc) -> Self { + Self { kind: Kind::Assistant, owner: Owner { kind: "assistant", id: conversation.to_string() }, live: Some(live) } + } + /// Names an explicit ingress grant, actor and access setting; payload JSON cannot create it. + pub fn agent(access: AgentAccess, users_project: bool, grant: &str, actor: Option, live: Arc) -> Self { + Self { kind: Kind::Agent { access, users_project, actor }, owner: Owner { kind: "agent", id: grant.into() }, live: Some(live) } + } + /// Names project-text exchange as untrusted peer provenance. + pub fn peer() -> Self { + Self { kind: Kind::Peer, owner: Owner { kind: "peer", id: String::new() }, live: None } + } + /// Returns a display/debug identity without exposing authority construction fields. + pub fn principal(&self) -> Principal { + match &self.kind { + Kind::User => Principal::User, + Kind::Assistant => Principal::Assistant, + Kind::Agent { access, users_project, .. } => Principal::Agent { access: *access, users_project: *users_project }, + Kind::Plugin { pin, .. } => Principal::Plugin { id: pin.0.manifest.id.clone() }, + Kind::Item { kind, name, .. } => Principal::Item { kind, name: name.clone() }, + Kind::Peer => Principal::Peer, + } + } + /// Compares owner and narrowing chain, including exact plugin load incarnation. + pub(super) fn same_caller(&self, other: &Self) -> bool { + /// Recurses through plugin/item intersections without allowing a less restricted completion. + fn same(a: &Kind, b: &Kind) -> bool { + match (a, b) { + (Kind::Plugin { pin: a, within: x }, Kind::Plugin { pin: b, within: y }) => a == b && same(x, y), + (Kind::Item { kind: a, name: n, within: x }, Kind::Item { kind: b, name: m, within: y }) => a == b && n == m && same(x, y), + (Kind::Plugin { .. } | Kind::Item { .. }, _) | (_, Kind::Plugin { .. } | Kind::Item { .. }) => false, + _ => true, + } + } + self.owner == other.owner && same(&self.kind, &other.kind) + } + /// The private conventional root used only when trusted native dispatch has no ingress scope. + pub(super) fn user() -> Self { + Self { kind: Kind::User, owner: Owner { kind: "user", id: String::new() }, live: None } + } + /// Lets adapters recheck live state immediately before their own late side effects. + pub fn check_live(&self, effects: Effects) -> Result<(), String> { + self.check(effects) + } + /// Checks adapter revocation and every plugin load in the narrowing chain. + pub(super) fn check(&self, effects: Effects) -> Result<(), String> { + if self.live.as_ref().is_some_and(|live| !live.allows(effects)) { + return Err("this request's authority was revoked".into()); + } + /// Checks current enabled plugin loads through nested item and plugin scopes. + fn current(k: &Kind) -> bool { + match k { + Kind::Plugin { pin, within } => rustgis_plugins::is_current(&pin.0) && current(within), + Kind::Item { within, .. } => current(within), + _ => true, + } + } + if !current(&self.kind) { + return Err("the plugin was disabled, updated or removed since this request started".into()); + } + Ok(()) + } +} +pub enum Outcome { + Allowed, + NeedsApproval(Pending), + Refused(String), +} +pub struct Pending { + pub(super) plan: Arc, + pub(super) review: Box, + pub(super) request: String, +} +pub struct Permit { + pub(super) plan: Arc, + pub(super) request: String, +} +#[derive(Clone)] +pub struct Remembered { + pub(super) plan: Arc, +} +impl Remembered { + /// Prunes remembered operations after project replacement. + pub fn belongs_to_generation(&self, generation: u64) -> bool { + self.plan.opened == generation + } +} +impl Pending { + /// Borrows the sanitized card frozen when approval was first requested. + pub fn review(&self) -> &Review { + &self.review + } + /// Consumes this pending request into a non-cloneable, one-use permit. + pub fn allow(self) -> Permit { + Permit { plan: self.plan, request: self.request } + } + /// Retains exact consent only for rememberable Files/Network operations. + pub fn remember(&self) -> Option { + self.review.remember.then(|| Remembered { plan: self.plan.clone() }) + } + /// Compares the complete raw plan, independently of the queued request id. + pub fn covered_by(&self, remembered: &Remembered) -> bool { + self.plan == remembered.plan + } +} +/// Engine-owned authority for an already authorized operation. Never reconstructed from JSON. +#[derive(Clone)] +pub(crate) struct Continuation { + pub(super) scope: Scope, + pub(super) node: Arc, + pub(super) opened: u64, + pub(super) map: u64, + pub(super) epoch: Arc, + pub(super) accounts: serde_json::Value, + pub(super) sources: Vec<(super::plan::LayerKey, serde_json::Value)>, + pub(super) tables: Vec<(u32, serde_json::Value)>, + pub(super) time_sources: Option>, + pub(super) s3: Option>, +} +impl Continuation { + /// Only an engine-created root native User may deliver owned tool snapshots this way. + pub(crate) fn native_tool_owner(&self) -> bool { + matches!(self.scope.kind, Kind::User) + } + /// Distinguishes two invocations of the same command without exposing their raw nodes. + #[cfg(not(target_arch = "wasm32"))] + pub(crate) fn same_run(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.node, &other.node) + } + /// Checks adapter revocation and every plugin load in the narrowing chain. + pub(crate) fn check(&self) -> Result<(), String> { + if self.epoch.load(std::sync::atomic::Ordering::SeqCst) != self.opened { + return Err("this run belongs to a replaced project".into()); + } + if self.s3.as_ref().is_some_and(|identities| identities.iter().any(|identity| !identity.is_current())) { + return Err("the live storage account changed since this request started".into()); + } + self.scope.check(self.node.effects)?; + self.node.current() + } +} +/// One-use completion for a native adapter's admitted asynchronous operation. +/// Fields and initiating authority are private; payloads cannot construct or widen it. +pub struct Deferred(pub(super) Continuation); + +pub(crate) enum Narrow { + Plugin(PluginPin), + Item { kind: &'static str, name: String }, +} diff --git a/crates/engine/src/project_log.rs b/crates/engine/src/project_log.rs index e1114e025e..7898e2ae87 100644 --- a/crates/engine/src/project_log.rs +++ b/crates/engine/src/project_log.rs @@ -138,6 +138,65 @@ fn kept_error(e: &str) -> String { /// `v` without secrets: as History's Copy as code takes them out (`history::redacted`), and, /// first, inside the JSON a text holds (a script's command lines: `s3.connect {"secret": …}`), /// whose objects are redacted in place. +/// A bounded review's display projection, without the log's clipping. Never used for +/// execution, consent equality or provenance; the policy retains its private original. +pub(crate) fn review_params(v: &Value) -> Result { + review_value(v, 0) +} + +/// Projects bounded review data recursively, including JSON held inside nested script strings. +/// A card that cannot show its structure safely is refused; log/diagnostic clipping is separate. +fn review_value(v: &Value, depth: usize) -> Result { + if depth > 32 { + return Err("too deeply nested to show in full; ask for a smaller request".into()); + } + Ok(match v { + Value::Object(o) => { + let mut out = serde_json::Map::new(); + for (key, value) in o { + let sensitive = + rustgis_data::address::normalized_name(key).as_deref() != Some("key") && rustgis_data::address::log_sensitive(key); + let Value::String(public_key) = review_value(&Value::String(key.clone()), depth + 1)? else { + return Err("a review field name must be text".into()); + }; + let value = if sensitive { Value::String(String::new()) } else { review_value(value, depth + 1)? }; + if out.insert(public_key, value).is_some() { + return Err("redacted field names cannot be distinguished; simplify the request".into()); + } + } + crate::cmd::history::redacted(&Value::Object(out)) + } + Value::Array(a) => Value::Array(a.iter().map(|v| review_value(v, depth + 1)).collect::>()?), + Value::String(text) => { + let mut out = String::new(); + let mut rest = text.as_str(); + while let Some(at) = rest.find(['{', '[']) { + let (before, from) = rest.split_at(at); + out.push_str(before); + let mut values = serde_json::Deserializer::from_str(from).into_iter::(); + match values.next() { + Some(Ok(value @ (Value::Object(_) | Value::Array(_)))) => { + let value = review_value(&value, depth + 1)?; + out.push_str(&value.to_string()); + rest = from.get(values.byte_offset()..).unwrap_or(""); + } + _ if from.get(1..).is_some_and(|r| r.trim_start().starts_with(['"', '\''])) || last_secret_name(from).is_some() => { + return Err("structured text cannot be safely shown in full; simplify the request".into()); + } + _ => { + let first = from.chars().next().ok_or("empty structured text")?; + out.push(first); + rest = from.get(first.len_utf8()..).unwrap_or(""); + } + } + } + out.push_str(rest); + Value::String(crate::cmd::history::redacted_text(&sql_secrets_removed(&out))) + } + other => other.clone(), + }) +} + fn redacted(v: &Value) -> Value { crate::cmd::history::redacted(&embedded_redacted(v, 0)) } diff --git a/crates/engine/src/service_logins.rs b/crates/engine/src/service_logins.rs index 01dbca38e7..6a86ede41f 100644 --- a/crates/engine/src/service_logins.rs +++ b/crates/engine/src/service_logins.rs @@ -136,6 +136,16 @@ impl SessionLogins { self.anonymous } + /// The immutable runtime owner used by the networking crate's private account snapshot. + pub(crate) fn session_id(&self) -> u64 { + self.session + } + + /// Sorted connection ids only; bearer bindings never enter the policy's JSON projection. + pub(crate) fn active_ids(&self) -> Vec<&str> { + self.active.keys().map(String::as_str).collect() + } + fn owner(&self, id: &str) -> String { format!("{}/{id}", self.session) } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 440066783b..47caf136d2 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -2,6 +2,12 @@ use serde_json::{Value, json}; use crate::{Project, Session, sample}; +#[path = "tests/permission_policy.rs"] +mod permission_policy; + +#[path = "tests/review_boundaries.rs"] +mod review_boundaries; + #[path = "tests/task_reentry.rs"] mod task_reentry; @@ -34,6 +40,29 @@ fn world() -> Session { s } +/// Runs a test that owns process-wide state in a fresh instance of the test executable. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn isolate_process_test(name: &str) -> bool { + if std::env::var("RUSTGIS_ISOLATED_SCENARIO").as_deref() == Ok(name) { + return false; + } + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", name]) + .env("RUSTGIS_ISOLATED_SCENARIO", name) + .output() + .unwrap(); + let stdout = String::from_utf8_lossy(&output.stdout); + let mut summaries = stdout.lines().filter(|line| line.starts_with("test result:")); + let ran_one = summaries.next().is_some_and(|line| line.starts_with("test result: ok. 1 passed; 0 failed; 0 ignored;")) + && summaries.next().is_none(); + assert!( + output.status.success() && ran_one, + "isolated process scenario failed or ran no test:\n{stdout}\n{}", + String::from_utf8_lossy(&output.stderr) + ); + true +} + #[test] fn worldview_swaps_only_untouched_sample_countries() { use sample::{Worldview, swap_countries}; @@ -2348,6 +2377,82 @@ fn geotiff_parts_open_and_survive_a_saved_project() { assert_eq!(value(&s), Some(244.0)); } +/// File-supplied items reject implicit network and reserved actions before their handlers run, +/// while ordinary in-project cells still run. This uses the existing Wasm plugin fixture. +#[test] +fn imported_tasks_and_notebooks_reject_network_and_reserved_actions() { + let mut s = world(); + plugin("eng-item-containment", &[]); + for (command, params) in [ + ("layer.geocode", json!({"layer": "Cities", "address": "name"})), + ("connection.test", json!({"id": "saved-connection"})), + ("plugin.uninstall", json!({"id": "eng-item-containment"})), + ] { + let steps = json!([{"title": "Outside", "command": command, "params": params}]); + let task = json!({"name": "From a file", "steps": steps}); + let why = s.run("task.import", &json!({"text": task.to_string()})).unwrap_err(); + assert!(why.contains("network") && why.contains("imported"), "{why}"); + // Opened projects and later edits reach the execution check without task.import. + s.run("task.new", &json!({"name": command, "steps": steps, "imported": true})).unwrap(); + let why = s.run("task.run", &json!({"task": command})).unwrap_err(); + assert!(why.contains("from a file"), "{why}"); + } + let text = json!({"name": "Shared", "cells": [ + {"kind": "code", "source": "select.all {\"layer\":\"Cities\"}"}, + {"kind": "code", "source": "plugin.uninstall {\"id\":\"eng-item-containment\"}"}, + {"kind": "code", "source": "layer.geocode {\"layer\":\"Cities\",\"address\":\"name\"}"} + ]}) + .to_string(); + s.run("notebook.open", &json!({"text": text})).unwrap(); + let result = s.run("notebook.run", &json!({"notebook": "Shared"})).unwrap(); + assert_eq!(result["error"], 1); + let notebook = s.project.notebooks.iter().find(|n| n.name == "Shared").unwrap(); + assert!(!notebook.cells[0].error); + assert!(notebook.cells[1].error && notebook.cells[1].output.as_deref().unwrap().contains("from a file")); + assert!(!s.map().layer_by_name("Cities").unwrap().selection.is_empty()); + let result = s.run("notebook.run", &json!({"notebook": "Shared", "cell": 2})).unwrap(); + assert_eq!(result["error"], 2); + assert!(rustgis_plugins::get("eng-item-containment").is_some(), "no uninstall handler ran"); + rustgis_plugins::unload("eng-item-containment"); +} + +/// Plugin tools are rejected through all imported item runners, including models' direct tool +/// paths. A locally authored model still runs the same real plugin tool. +#[test] +fn imported_items_reject_resolved_plugin_tools() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::imported_items_reject_resolved_plugin_tools") { + return; + } + let mut s = world(); + plugin("eng-tool-containment", &[]); + let tool = "plugin:eng-tool-containment/m"; + let params = json!({"input": "Cities"}); + let gp = json!({"tool": tool, "params": params}); + let before = s.map().layers.len(); + let task = json!({"name": "Plugin task", "steps": [{"title": "Run", "command": "gp.run", "params": gp}]}); + assert!(s.run("task.import", &json!({"text": task.to_string()})).unwrap_err().contains("imported")); + let notebook = json!({"name": "Plugin notebook", "cells": [{"kind": "code", "source": format!("gp.run {gp}")}]}); + s.run("notebook.open", &json!({"text": notebook.to_string()})).unwrap(); + assert_eq!(s.run("notebook.run", &json!({"notebook": "Plugin notebook"})).unwrap()["error"], 0); + let graph = json!({"nodes": [{"id": "x", "kind": "tool", "tool": tool, "params": params}], "edges": []}); + for (name, body) in [("Plugin steps", json!({"steps": [{"tool": tool, "params": params}]})), ("Plugin graph", json!({"graph": graph}))] + { + let mut model = body; + model["name"] = json!(name); + s.run("model.import", &json!({"text": model.to_string()})).unwrap(); + let why = s.run("model.run", &json!({"name": name})).unwrap_err(); + assert!(why.contains("from a file"), "{why}"); + } + assert_eq!(s.map().layers.len(), before, "no untrusted tool ran"); + // The same graph made here is authorized; no plugin interface or manifest policy changes. + s.run("model.new", &json!({"name": "Made here", "graph": graph})).unwrap(); + s.run("model.run", &json!({"name": "Made here"})).unwrap(); + assert_eq!(s.map().layers.len(), before + 1); + assert!(s.map().layer_by_name("Made").is_some()); + rustgis_plugins::unload("eng-tool-containment"); +} + #[test] fn imported_tasks_name_outputs_and_steps_are_edited() { let mut s = Session::new(sample::world_project()); @@ -2362,12 +2467,12 @@ fn imported_tasks_name_outputs_and_steps_are_edited() { assert!(s.map().layer_by_name("Near cities").is_some()); let to_a_file = json!({"name": "To a file", "steps": [{"title": "Buffer", "command": "gp.run", "params": {"tool": "buffer", "params": {"input": "Cities", "distance": 100, "output": "C:/temp/x.shp"}}}]}); assert!(s.run("task.import", &json!({"text": to_a_file.to_string()})).is_err()); - assert!(crate::cmd::items::touches_files("gp.run", &json!({"tool": "buffer", "params": {"output": "../x.shp"}}))); - assert!(!crate::cmd::items::touches_files("gp.run", &json!({"tool": "buffer", "params": {"output": "Near cities"}}))); + assert!(crate::cmd::items::touches_files(&s, "gp.run", &json!({"tool": "buffer", "params": {"output": "../x.shp"}}))); + assert!(!crate::cmd::items::touches_files(&s, "gp.run", &json!({"tool": "buffer", "params": {"output": "Near cities"}}))); // Spatial Join and Zonal Statistics run their built-in tool with the parameters flat: a // plain output name stays in the session there too. - assert!(!crate::cmd::items::touches_files("gp.spatialJoin", &json!({"target": "Cities", "join": "Countries", "output": "Joined"}))); - assert!(!crate::cmd::items::touches_files("gp.zonalStats", &json!({"raster": "dem", "polygons": "Countries", "output": "Zones"}))); + assert!(!crate::cmd::items::touches_files(&s, "gp.spatialJoin", &json!({"target": "Cities", "join": "Countries", "output": "Joined"}))); + assert!(!crate::cmd::items::touches_files(&s, "gp.zonalStats", &json!({"raster": "dem", "polygons": "Countries", "output": "Zones"}))); // Only a plain layer name, only gp.run's (and those two's), only for RustGIS's own tools. for (command, params) in [ ("gp.run", json!({"tool": "buffer", "params": {"output": "x.shp"}})), @@ -2383,7 +2488,7 @@ fn imported_tasks_name_outputs_and_steps_are_edited() { ("gp.spatialJoin", json!({"target": "Cities", "join": "Countries", "output": "C:/temp/x.shp"})), ("gp.zonalStats", json!({"raster": "dem", "polygons": "Countries", "output": "../zones"})), ] { - assert!(crate::cmd::items::touches_files(command, ¶ms), "{command} {params}"); + assert!(crate::cmd::items::touches_files(&s, command, ¶ms), "{command} {params}"); } // `"step": null` runs the whole task, as before; a step that isn't an index is refused. @@ -2428,7 +2533,7 @@ Line two." assert!(s.run("task.editStep", &json!({"task": "Lesson", "step": 1, "title": format!(" {same} ")})).is_err()); assert_eq!(s.undo_depth(), depth); // Still a task from a file: a step that writes a file stays refused after editing. - assert!(!s.trusted_tasks.contains("lesson")); + assert!(!s.item_trusted("task", "lesson")); } #[test] @@ -2891,6 +2996,10 @@ fn hostile_params_never_panic_7() { /// Runs the commands of one shard: those whose place in the registry is `shard` modulo /// [`HOSTILE_SHARDS`]. fn hostile_params_never_panic(shard: usize) { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test(&format!("tests::hostile_params_never_panic_{shard}")) { + return; + } let junk = [ Value::Null, json!({}), @@ -3107,7 +3216,7 @@ fn geolibre_runs_can_finish_asynchronously() { eprintln!("skipped: no GeoLibre runner (set RUSTGIS_GEOLIBRE)"); return; } - assert!(external_start(&mut s, "geolibre:slope", &json!({})).unwrap_err().contains("required")); + assert!(external_start(&mut s, "geolibre:slope", &json!({})).err().unwrap().contains("required")); assert!(external_start(&mut s, "buffer", &json!({})).is_err(), "only GeoLibre tools"); let run = external_start(&mut s, "geolibre:slope", &json!({"input": "scene"})).unwrap(); assert_eq!((run.tool.as_str(), run.files.len()), ("slope", 1), "the embedded raster is staged"); @@ -7463,11 +7572,11 @@ fn project_items_files_styles_packages_tasks_and_3d() { s.run("task.run", &json!({"task": "Explore", "step": 2})).unwrap(); assert_eq!(s.map().selection_count(), 0); s.run("task.new", &json!({"name": "Replay", "fromHistory": true})).unwrap(); - assert!(s.trusted_tasks.contains("replay"), "history run in this session"); + assert!(s.item_trusted("task", "replay"), "history run in this session"); // History that came with an opened project is someone else's. s.history_from_file = 1; s.run("task.new", &json!({"name": "Replay file", "fromHistory": true})).unwrap(); - assert!(!s.trusted_tasks.contains("replay file")); + assert!(!s.item_trusted("task", "replay file")); s.history_from_file = 0; assert_eq!(s.project.tasks.last().unwrap().steps.len(), 1); s.run("task.save", &json!({"task": "Explore", "path": path("explore.rgistask")})).unwrap(); @@ -8972,6 +9081,17 @@ fn plugin_tools_name_their_output_layer_with_edit_alone() { use crate::cmd::plugins::reaches; let mut s = world(); plugin("eng-out", &["edit"]); + #[cfg(feature = "geolibre")] + crate::cmd::gp::external_load_manifests( + &json!([{ + "id":"slope", "display_name":"Slope", "params":[ + {"name":"input","data_kind":"raster","io_role":"input","required":true}, + {"name":"output","data_kind":"raster","io_role":"output"} + ] + }]) + .to_string(), + ) + .unwrap(); let clip = json!({"tool": "clip", "params": {"input": "Cities", "clip": "Countries", "output": "Cities in countries"}}); let gp = s.registry.get("gp.run").copied().unwrap(); assert!(reaches(&gp, &clip).is_none(), "the assistant doesn't ask either"); @@ -8981,8 +9101,9 @@ fn plugin_tools_name_their_output_layer_with_edit_alone() { json!({"tool": "clip", "params": {"input": "Cities", "clip": "Countries", "output": "out/cities"}}), json!({"tool": "clip", "params": {"input": "Cities", "clip": "Countries", "output": "cities.shp"}}), json!({"tool": "clip", "params": {"input": "Cities", "clip": "Countries", "output": "C:\\cities"}}), + #[cfg(feature = "geolibre")] json!({"tool": "geolibre:slope", "params": {"input": "Cities", "output": "slope"}}), - json!({"tool": "vectorToPmtiles", "params": {"input": "Cities", "output": "cities"}}), + json!({"tool": "vectorToPmtiles", "params": {"input": "Cities", "minZoom": 0, "maxZoom": 0, "output": "cities"}}), ] { let e = forward(&mut s, "eng-out", "gp.run", params.clone()).unwrap_err(); assert!(e.contains("'files' permission"), "{params}: {e}"); @@ -8990,6 +9111,64 @@ fn plugin_tools_name_their_output_layer_with_edit_alone() { rustgis_plugins::unload("eng-out"); } +/// The real Wasm forwarding command may run only its own tool, resolved by id or display +/// name, with edit and file requirements preserved. Process tools share the same boundary. +#[test] +fn plugin_bridges_cannot_run_another_plugins_tools() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::plugin_bridges_cannot_run_another_plugins_tools") { + return; + } + let mut s = world(); + for id in ["eng-bridge-owner", "eng-bridge-other"] { + let mut pkg = rustgis_plugins::Package::from_zip(&rustgis_plugins::testing::package(id, &["edit"])).unwrap(); + pkg.manifest.tools[0].name = format!("Points for {id}"); + rustgis_plugins::load(pkg, "test").unwrap(); + } + let before = s.map().layers.len(); + for tool in ["plugin:eng-bridge-other/m", "Points for eng-bridge-other"] { + let why = forward(&mut s, "eng-bridge-owner", "gp.run", json!({"tool": tool, "params": {"input": "Cities"}})).unwrap_err(); + assert!(why.contains("another plugin") && why.contains("eng-bridge-other"), "{why}"); + assert_eq!(s.map().layers.len(), before); + } + for tool in ["plugin:eng-bridge-owner/m", "Points for eng-bridge-owner"] { + forward(&mut s, "eng-bridge-owner", "gp.run", json!({"tool": tool, "params": {"input": "Cities"}})).unwrap(); + } + assert_eq!(s.map().layers.len(), before + 2); + plugin("eng-bridge-readonly", &[]); + let why = + forward(&mut s, "eng-bridge-readonly", "gp.run", json!({"tool": "plugin:eng-bridge-readonly/m", "params": {"input": "Cities"}})) + .unwrap_err(); + assert!(why.contains("'edit' permission"), "{why}"); + // File-shaped parameters to an own tool still require files before the tool runs. + let why = forward( + &mut s, + "eng-bridge-owner", + "gp.run", + json!({"tool": "plugin:eng-bridge-owner/m", "params": {"input": "Cities", "path": "never-opened.nc"}}), + ) + .unwrap_err(); + assert!(why.contains("'files' permission"), "{why}"); + #[cfg(not(target_arch = "wasm32"))] + { + let run = rustgis_plugins::testing::child_run("tests::plugin_process_child"); + let manifest = + rustgis_plugins::Manifest::parse(&rustgis_plugins::testing::process_manifest("eng-bridge-process", &[], &run)).unwrap(); + let pkg = + rustgis_plugins::Package { manifest, wasm: vec![], sha256: "test".into(), dir: Some(std::env::temp_dir()), skills: vec![] }; + rustgis_plugins::load(pkg, "test").unwrap(); + let why = + forward(&mut s, "eng-bridge-owner", "gp.run", json!({"tool": "plugin:eng-bridge-process/m", "params": {"input": "Cities"}})) + .unwrap_err(); + assert!(why.contains("another plugin"), "{why}"); + assert_eq!(s.map().layers.len(), before + 2, "no process tool ran"); + rustgis_plugins::unload("eng-bridge-process"); + } + for id in ["eng-bridge-owner", "eng-bridge-other", "eng-bridge-readonly"] { + rustgis_plugins::unload(id); + } +} + /// A plugin's command the app runs leaves its downloads to the app, which fetches them off the /// frame (as the browser does): nothing waits, and nothing is an undo step until the data lands, /// with what the plugin asked (how a table's points are read) kept. Run another way (the command @@ -9007,9 +9186,12 @@ fn plugin_downloads_are_left_to_the_app() { assert_eq!((s.undo.len(), s.map().layers.len()), (undo, layers)); let asked = s.take_url_requests(); assert_eq!(asked.len(), 1); - assert_eq!((asked[0]["name"].as_str(), asked[0]["x"].as_str(), asked[0]["y"].as_str()), (Some("Places"), Some("lon"), Some("lat"))); - assert!(asked[0]["request"].is_u64(), "{}", asked[0]); - s.request_failed(&asked[0], "dropped"); + assert_eq!( + (asked[0].params()["name"].as_str(), asked[0].params()["x"].as_str(), asked[0].params()["y"].as_str()), + (Some("Places"), Some("lon"), Some("lat")) + ); + assert!(asked[0].params()["request"].is_u64()); + s.request_failed(asked[0].params(), "dropped"); s.poll_plugin_retries(); assert!(s.run("plugin:eng-url/f", &p).is_err(), "run otherwise, it waits"); assert!(s.take_url_requests().is_empty()); @@ -9036,8 +9218,8 @@ fn a_command_giving_up_names_the_downloads_it_leaves() { s.run_leaving_downloads("plugin:eng-giveup-other/f", &add("https://x.org/shared.csv")).unwrap(); s.retry_tries = crate::cmd::plugins::fetch::MAX_RETRIES; let e = s.run_leaving_downloads("plugin:eng-giveup/f", &add("https://x.org/shared.csv")).unwrap_err(); - assert!(e.contains("gave up") && !e.contains("wasn't started"), "{e}"); - assert_eq!(s.take_url_requests()[0]["url"], "https://x.org/shared.csv", "the other command's still comes"); + assert!(e.contains("gave up") && e.contains("wasn't started"), "{e}: another plugin gets its own request"); + assert_eq!(s.take_url_requests()[0].params()["url"], "https://x.org/shared.csv", "the other command's still comes"); rustgis_plugins::unload("eng-giveup"); rustgis_plugins::unload("eng-giveup-other"); } @@ -9056,13 +9238,13 @@ fn a_plugin_command_runs_again_when_its_download_is_added() { assert_eq!(s.run_leaving_downloads("plugin:eng-rerun/f", &p).unwrap()["downloading"], json!([url])); let asked = s.take_url_requests(); assert_eq!(asked.len(), 1); - assert_eq!(asked[0]["anonymous"], json!(true), "no saved login goes with a plugin's download"); + assert_eq!(asked[0].params()["anonymous"], json!(true), "no saved login goes with a plugin's download"); // Clicked again while it downloads: the same download, not a second one. s.run_leaving_downloads("plugin:eng-rerun/f", &p).unwrap(); assert!(s.take_url_requests().is_empty(), "one download"); assert!(s.poll_plugin_retries().is_empty(), "nothing landed yet"); // The app downloads it and adds it, with the request's number, as its own step. - let id = s.download_started(); + let id = s.download_started_for(&asked[0]).unwrap(); assert!( s.download_landed( id, @@ -9073,9 +9255,9 @@ B,11,51 .to_vec() ) ); - let mut add = asked[0].clone(); + let mut add = asked[0].params().clone(); add["id"] = json!(id); - s.run("layer.addDownload", &add).unwrap(); + s.complete_download(id, &add).unwrap(); assert_eq!(s.map().layers.len(), layers + 1); let added = s.map().layer_by_name("Places").unwrap().id; // The command runs again: its `layer.addUrl` answers the layer added, and adds nothing (nor @@ -9093,20 +9275,19 @@ B,11,51 let q = json!({"op": "layer.addUrl", "params": {"url": "https://x.org/gone.csv"}}); s.run_leaving_downloads("plugin:eng-rerun/f", &q).unwrap(); let asked = s.take_url_requests(); - s.request_failed(&asked[0], "https://x.org/gone.csv?token=hunter2: 404 Not Found"); + s.request_failed(asked[0].params(), "https://x.org/gone.csv?token=hunter2: 404 Not Found"); let ran = s.poll_plugin_retries(); let e = ran[0].1.as_ref().unwrap_err(); assert!(e.contains("404") && !e.contains("hunter2"), "{ran:?}"); assert!(!s.plugin_retries_waiting()); - // Another caller's download queued under the number the command's will get: the command's is - // still queued, for its own address. + // A native request and a plugin request get distinct engine routing numbers. let next = s.next_request + 1; - s.url_requests.push(json!({"url": "https://x.org/other.csv", "request": next})); + s.request_url(&json!({"url": "https://x.org/other.csv", "request": next})).unwrap(); let q = json!({"op": "layer.addUrl", "params": {"url": "https://x.org/mine.csv"}}); s.run_leaving_downloads("plugin:eng-rerun/f", &q).unwrap(); let asked = s.take_url_requests(); - let urls: Vec<_> = asked.iter().map(|q| (q["url"].as_str(), q["request"].as_u64())).collect(); - assert_eq!(urls, [(Some("https://x.org/other.csv"), Some(next)), (Some("https://x.org/mine.csv"), Some(next))]); + let urls: Vec<_> = asked.iter().map(|q| (q.params()["url"].as_str(), q.params()["request"].as_u64())).collect(); + assert_eq!(urls, [(Some("https://x.org/other.csv"), Some(next)), (Some("https://x.org/mine.csv"), Some(next + 1))]); // A run replaced by one asking for another address before its download was started: that // download is never started, and the notice of the replaced run says so. let first = json!({"op": "layer.addUrl", "params": {"url": "https://x.org/first.csv"}}); @@ -9120,10 +9301,112 @@ B,11,51 "{said:?}" ); let left = s.take_url_requests(); - assert!(left.iter().all(|q| q["url"] != "https://x.org/first.csv"), "{left:?}"); + assert!(left.iter().all(|q| q.params()["url"] != "https://x.org/first.csv"), "{} remaining requests", left.len()); rustgis_plugins::unload("eng-rerun"); } +/// Retry metadata and queued capabilities obey the same outer transaction as actual plugin edits. +#[test] +fn plugin_download_retries_roll_back_with_their_outer_command() { + let mut s = world(); + plugin("eng-outer-url", &["edit", "network"]); + let call = |url: &str| json!({"op":"layer.addUrl", "params":{"url":url}}); + s.run_leaving_downloads("plugin:eng-outer-url/f", &call("https://x.org/kept.csv")).unwrap(); + let before = serde_json::to_value(&s.project).unwrap(); + let mut pending = Value::Null; + let result = s.guarded("Failed plugin composite", true, |s| { + pending = s.run_leaving_downloads("plugin:eng-outer-url/f", &call("https://x.org/rolled-back.csv"))?; + s.run("layer.addDataText", &json!({"fileName":"broken.geojson", "text":"{"})) + }); + assert_eq!(pending["downloading"], json!(["https://x.org/rolled-back.csv"]), "the real plugin suspended before the outer failure"); + assert!(result.is_err(), "{result:?}"); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before); + let kept = s.take_url_requests(); + assert_eq!(kept.len(), 1); + assert_eq!(kept[0].params()["url"], "https://x.org/kept.csv"); + assert!(s.plugin_retries_waiting() && s.poll_plugin_retries().is_empty()); + s.request_failed(kept[0].params(), "cancelled"); + assert_eq!(s.poll_plugin_retries().len(), 1); + // An opaque request taken during a failed outer guard stays retired, not just hidden. + let mut escaped = None; + let result = s.guarded("Failed plugin wrapper", true, |s| { + s.run_leaving_downloads("plugin:eng-outer-url/f", &call("https://x.org/escaped.csv"))?; + escaped = s.take_url_requests().pop(); + Err("outer command failed".into()) + }); + assert!(result.is_err()); + let escaped = escaped.unwrap(); + assert!(s.download_started_for(&escaped).is_err()); + assert!(!s.plugin_retries_waiting() && s.poll_plugin_retries().is_empty() && s.take_url_requests().is_empty()); + // A prior queued key consumed inside a failed guard is never restored by its checkpoint. + s.request_url(&json!({"url":"https://x.org/consumed.csv"})).unwrap(); + let issued = s.take_url_requests(); + let result = s.guarded("Claim then fail", true, |s| { + let id = s.download_started_for(&issued[0])?; + s.download_failed(id); + Err("failed after claim".into()) + }); + assert!(result.is_err() && s.download_started_for(&issued[0]).is_err()); + rustgis_plugins::unload("eng-outer-url"); +} + +/// A routing number cannot consume another session's claim or wait; a revoked original owner +/// is retired before dequeue and never rerun as a fresh native User. +#[test] +fn plugin_download_retries_keep_their_original_owner() { + use crate::policy::{Call, Effects, Outcome, Revocation, Scope}; + use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, + }; + struct Live(AtomicBool); + impl Revocation for Live { + fn allows(&self, _: Effects) -> bool { + self.0.load(Ordering::SeqCst) + } + } + let mut s = world(); + plugin("eng-owned-url", &["edit", "network"]); + let params = json!({"op":"layer.addUrl","params":{"url":"https://x.org/owned.csv"}}); + let live = Arc::new(Live(AtomicBool::new(true))); + let scope = Scope::assistant(146101, live.clone()); + let call = Call::command("plugin:eng-owned-url/f", params.clone()); + let Outcome::NeedsApproval(prepared) = s.review(&scope, &call) else { panic!("plugin must ask") }; + let pending = s.with_deferred_downloads(|s| s.run_permitted(scope, prepared.allow(), &call)).unwrap(); + assert_eq!(pending["downloading"], json!(["https://x.org/owned.csv"])); + assert!(s.plugin_retries_waiting(), "approved work is pending before revocation"); + let before = serde_json::to_value(&s.project).unwrap(); + live.0.store(false, Ordering::SeqCst); + assert!(s.take_url_requests().is_empty()); + let ran = s.poll_plugin_retries(); + assert_eq!(ran.len(), 1); + assert!(ran[0].1.is_err()); + assert!(!s.plugin_retries_waiting() && s.poll_plugin_retries().is_empty()); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before); + let mut first = world(); + let mut second = world(); + first.run_leaving_downloads("plugin:eng-owned-url/f", ¶ms).unwrap(); + second.run_leaving_downloads("plugin:eng-owned-url/f", ¶ms).unwrap(); + let foreign = first.take_url_requests(); + let rightful = second.take_url_requests(); + assert_eq!(foreign[0].params()["request"], rightful[0].params()["request"], "routing numbers are session-local"); + assert!(second.download_started_for(&foreign[0]).is_err()); + let other = Scope::assistant(146102, Arc::new(Live(AtomicBool::new(true)))); + second.with_scope(other, |s| s.request_failed(rightful[0].params(), "foreign cancellation")).unwrap(); + assert!(second.poll_plugin_retries().is_empty() && second.plugin_retries_waiting()); + let id = second.download_started_for(&rightful[0]).unwrap(); + assert!(second.download_landed(id, b"name,lon,lat\nA,1,2\n".to_vec())); + let mut p = rightful[0].params().clone(); + p["id"] = json!(id); + second.complete_download(id, &p).unwrap(); + let ran = second.poll_plugin_retries(); + assert_eq!(ran.len(), 1); + assert!(ran[0].1.is_ok()); + assert!(second.download_started_for(&rightful[0]).is_err()); + assert!(first.poll_plugin_retries().is_empty() && first.plugin_retries_waiting()); + rustgis_plugins::unload("eng-owned-url"); +} + #[test] fn plugin_panels_keep_their_state_between_calls() { let mut s = world(); @@ -9420,6 +9703,7 @@ fn plugin_fetches_run_the_command_again_when_they_land() { let (base, _) = serve_documents(); fetch_mode(true, false); let url = format!("{base}/json?web=1"); + s.log.clear(); let notices = s.notice_mark(); let undo = s.undo.len(); let r = forward(&mut s, "eng-fetch-web", "host.fetch", json!({"url": url})).unwrap(); @@ -9439,6 +9723,11 @@ fn plugin_fetches_run_the_command_again_when_they_land() { assert_eq!(ran[0].0, "plugin:eng-fetch-web/f"); assert_eq!(ran[0].1.as_ref().unwrap()["json"]["valid"], true); assert!(s.poll_plugin_retries().is_empty(), "run once"); + assert_eq!( + s.log.entries().filter(|e| e.command == "plugin:eng-fetch-web/f").count(), + 1, + "one actual final log entry, no pending entry" + ); // A failed fetch is given to the run again as its error. let r = forward(&mut s, "eng-fetch-web", "host.fetch", json!({"url": format!("{base}/missing")})).unwrap(); assert_eq!(r["pending"], true); @@ -9451,6 +9740,35 @@ fn plugin_fetches_run_the_command_again_when_they_land() { std::thread::sleep(std::time::Duration::from_millis(20)); }; assert!(ran[0].1.as_ref().unwrap_err().contains("404"), "{:?}", ran[0].1); + // A held pinned retry publishes revocation instead of disappearing from the UI. + struct Live(std::sync::atomic::AtomicBool); + impl crate::policy::Revocation for Live { + fn allows(&self, _: crate::policy::Effects) -> bool { + self.0.load(std::sync::atomic::Ordering::SeqCst) + } + } + let live = std::sync::Arc::new(Live(true.into())); + let scope = crate::policy::Scope::assistant(901, live.clone()); + let call = crate::policy::Call::command( + "plugin:eng-fetch-web/f", + json!({"op":"host.fetch","params":{"url":format!("{base}/json?revoked=1")}}), + ); + let crate::policy::Outcome::NeedsApproval(p) = s.review(&scope, &call) else { panic!("plugin must ask") }; + assert_eq!(s.run_permitted(scope, p.allow(), &call).unwrap()["pending"], true); + live.0.store(false, std::sync::atomic::Ordering::SeqCst); + let mark = s.notice_mark(); + let started = std::time::Instant::now(); + let revoked = loop { + let r = s.poll_plugin_retries(); + if !r.is_empty() || started.elapsed() > std::time::Duration::from_secs(10) { + break r; + } + std::thread::yield_now(); + }; + assert_eq!(revoked.len(), 1); + assert!(revoked[0].1.is_err()); + assert!(s.notices_since(mark).iter().any(|n| n.error), "revoked retry is visible"); + assert!(s.poll_plugin_retries().is_empty()); // A command forever waiting gives up. s.retry_tries = crate::cmd::plugins::fetch::MAX_RETRIES; let e = forward(&mut s, "eng-fetch-web", "host.fetch", json!({"url": format!("{base}/json?web=2")})).unwrap_err(); @@ -9462,6 +9780,10 @@ fn plugin_fetches_run_the_command_again_when_they_land() { #[test] fn plugin_tools_run_in_the_geoprocessing_pane() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::plugin_tools_run_in_the_geoprocessing_pane") { + return; + } let mut s = world(); plugin("eng-tools", &[]); let tools = s.run("gp.tools", &Value::Null).unwrap(); @@ -9478,6 +9800,19 @@ fn plugin_tools_run_in_the_geoprocessing_pane() { #[test] fn plugin_tools_run_off_the_session() { + // This snapshot-fitting scenario needs stable accounts; connection tests rotate the + // process-wide store and correctly revoke unrelated held continuations. + #[cfg(not(target_arch = "wasm32"))] + if std::env::var_os("RUSTGIS_PLUGIN_BG_TEST_CHILD").is_none() { + let out = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "tests::plugin_tools_run_off_the_session", "--nocapture"]) + .env("RUSTGIS_PLUGIN_BG_TEST_CHILD", "1") + .output() + .unwrap(); + assert!(out.status.success(), "{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr)); + assert!(String::from_utf8_lossy(&out.stdout).contains("test result: ok. 1 passed; 0 failed;")); + return; + } use crate::cmd::gp::{plugin_complete, plugin_start}; use rustgis_plugins::tools::Control; let mut s = world(); @@ -9580,18 +9915,25 @@ fn plugin_tools_run_off_the_session() { #[test] fn plugin_file_parameters_reach_files_and_stay_out_of_the_history() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::plugin_file_parameters_reach_files_and_stay_out_of_the_history") { + return; + } use crate::cmd::plugins::{Reach, reaches}; let mut s = world(); plugin("eng-file", &["files"]); let run = json!({"tool": "plugin:eng-file/r", "params": {"model": "/data/pism.nc"}}); - // Running a tool that reads a file reaches files (the assistant asks first)... + // Plugin identity asks each time, with or without a file parameter. let gp = s.registry.get("gp.run").copied().unwrap(); - assert!(matches!(reaches(&gp, &run), Some(Reach::Files))); - assert!(reaches(&gp, &json!({"tool": "plugin:eng-file/m", "params": {"input": "Cities"}})).is_none()); - // ...so a plugin without `files` can't run it. + assert_eq!(reaches(&gp, &run), Some(Reach::Plugin("plugin:eng-file/r"))); + assert_eq!( + reaches(&gp, &json!({"tool": "plugin:eng-file/m", "params": {"input": "Cities"}})), + Some(Reach::Plugin("plugin:eng-file/m")) + ); + // A different plugin cannot run it, regardless of its file parameters. plugin("eng-file-edit", &["edit"]); let e = forward(&mut s, "eng-file-edit", "gp.run", run).unwrap_err(); - assert!(e.contains("'files' permission"), "{e}"); + assert!(e.contains("another plugin"), "{e}"); // A file picked in the browser travels with its contents, but the history keeps its name. let picked = json!({"tool": "plugin:eng-file/r", "params": {"model": {"name": "pism.nc", "data": "Q0RGAQ=="}}}); s.run("gp.run", &picked).unwrap(); @@ -9643,10 +9985,15 @@ fn a_command_run_again_does_not_repeat_its_errors() { assert_eq!(said(&s, mark, "said before the call"), 1, "said while it waits"); let asked = s.take_url_requests(); // Each download that fails runs it again, its `layer.addUrl` answering the error: nothing more. - s.request_failed(&asked[0], "404 Not Found"); + s.request_failed(asked[0].params(), "404 Not Found"); let ran = s.poll_plugin_retries(); assert!(ran[0].1.as_ref().is_err_and(|e| e.contains("404")), "{ran:?}"); assert_eq!(said(&s, mark, "said before the call"), 1, "not repeated"); + // A real guest error after the successful host call is a failure, never suspension. + let failed = json!({"op":"layer.addUrl", "params":{"url":"https://x.org/not-started.csv"}, "fail":true}); + assert!(s.run_leaving_downloads("plugin:eng-said/d", &failed).unwrap_err().contains("nope")); + assert!(s.take_url_requests().is_empty() && !s.plugin_retries_waiting()); + assert!(s.poll_plugin_retries().is_empty()); // The next time it is asked for, it is said again. let mark = s.notice_mark(); s.run_leaving_downloads("plugin:eng-said/d", &p).unwrap(); @@ -9659,6 +10006,10 @@ fn a_command_run_again_does_not_repeat_its_errors() { #[test] fn process_plugins_reach_the_project_like_modules() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::process_plugins_reach_the_project_like_modules") { + return; + } let mut s = world(); let dir = std::env::temp_dir().join(format!("rustgis-test-eng-process-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); @@ -12114,7 +12465,7 @@ fn shapefile_by_its_shp_address() { s.leave_downloads = true; assert_eq!(s.run("layer.addUrl", &json!({"url": url, "name": "Pela janela"})).unwrap()["pending"], true); s.leave_downloads = false; - assert_eq!(s.take_url_requests()[0]["url"], json!(url)); + assert_eq!(s.take_url_requests()[0].params()["url"], json!(url)); // The app's download, waited for here (failing rather than hanging past 30 s). let fetch = |url: &str, max: usize| { let slot = std::sync::Arc::new(std::sync::Mutex::new(None)); @@ -12308,10 +12659,11 @@ fn url_requests_wait_for_the_front_end() { assert!(s.request_url(&json!({"url": "https://x.org/a.geojson", "name": "A2"})).is_ok()); let taken = s.take_url_requests(); assert_eq!(taken.len(), 3, "the same request once"); - assert_eq!((taken[0]["name"].as_str(), taken[0]["zoom"].as_bool()), (Some("A"), Some(true))); - assert_eq!(taken[1]["url"], "https://x.org/b.geojson"); - assert_eq!((taken[2]["url"].as_str(), taken[2]["name"].as_str()), (Some("https://x.org/a.geojson"), Some("A2"))); + assert_eq!((taken[0].params()["name"].as_str(), taken[0].params()["zoom"].as_bool()), (Some("A"), Some(true))); + assert_eq!(taken[1].params()["url"], "https://x.org/b.geojson"); + assert_eq!((taken[2].params()["url"].as_str(), taken[2].params()["name"].as_str()), (Some("https://x.org/a.geojson"), Some("A2"))); assert!(s.take_url_requests().is_empty()); + drop(taken); // Abandoned opaque requests release their bounded unclaimed authority. for i in 0..16 { assert!(s.request_url(&json!({"url": format!("https://x.org/{i}")})).is_ok(), "{i}"); } @@ -12320,8 +12672,12 @@ fn url_requests_wait_for_the_front_end() { assert!(s.request_url(&json!({"url": "https://x.org/3"})).is_ok(), "one already waiting is fine"); let taken = s.take_url_requests(); assert_eq!(taken.len(), 16, "a few at a time"); - assert_eq!((taken[0]["url"].as_str(), taken[15]["url"].as_str()), (Some("https://x.org/0"), Some("https://x.org/15"))); - assert!(s.request_url(&json!({"url": "https://x.org/16"})).is_ok(), "room again once taken"); + assert_eq!( + (taken[0].params()["url"].as_str(), taken[15].params()["url"].as_str()), + (Some("https://x.org/0"), Some("https://x.org/15")) + ); + drop(taken); + assert!(s.request_url(&json!({"url": "https://x.org/16"})).is_ok(), "room again once abandoned"); } /// A mutating command that only left a request for the front end (the browser's `layer.addUrl`) @@ -12366,8 +12722,8 @@ fn a_failed_command_takes_its_request_back() { }); assert!(r.is_err()); let left = s.take_url_requests(); - assert_eq!(left.len(), 1, "{left:?}"); - assert_eq!(left[0]["url"], "https://x.org/kept.geojson"); + assert_eq!(left.len(), 1); + assert_eq!(left[0].params()["url"], "https://x.org/kept.geojson"); } /// A service's features keep the address they came from, and Copy Path gives it without its key. @@ -12397,10 +12753,14 @@ fn downloads_land_in_their_own_project() { assert!(s.run("layer.addDownload", &json!({"id": id, "url": "https://x.org/a.geojson"})).is_err(), "used once"); let late = s.download_started(); s.run("project.new", &json!({})).unwrap(); + let notice_mark = s.notice_mark(); assert!(!s.download_landed(late, b"{}".to_vec()), "another project is open now"); + assert!(s.notices_since(notice_mark).is_empty(), "a normal dropped download leaves its informational notice to the UI"); let failed = s.download_started(); s.download_failed(failed); + let notice_mark = s.notice_mark(); assert!(!s.download_landed(failed, b"{}".to_vec())); + assert!(s.notices_since(notice_mark).is_empty(), "an already failed request is not a new delivery error"); // A link's first file zooms the map to it, whatever the map held. let mut w = world(); let id = w.download_started(); @@ -12941,6 +13301,8 @@ fn service_times_and_directories() { assert_eq!(v["layers"][1]["offer"], json!({"type": "service", "url": "https://x.example.org/arcgis/rest/services/Topo/MapServer"})); let e = s.run("service.add", &json!({"url": root, "text": dir, "layer": "Topo/MapServer"})).unwrap_err(); assert!(e.contains("service.layers"), "{e}"); + let foreign = WMTS.replace("https://gibs.example.org/TC/default/", "https://fallback-b.invalid/TC/default/"); + service_logins::foreign_service_fallback_stays_anonymous("wmts", url, "TC", WMTS, &foreign); } /// Adding an OGC API collection the listing left out, as 3D Tiles (#1267), says why, not that the @@ -12961,6 +13323,10 @@ fn adding_a_collection_of_3d_tiles_says_why_it_is_left_out() { /// its layers' queries), and no address the session lists or keeps carries it. #[test] fn an_arcgis_token_in_a_service_address_reaches_its_layers() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::an_arcgis_token_in_a_service_address_reaches_its_layers") { + return; + } use crate::render::rustgis_tiles::arcgis_auth; let mut s = Session::new(Project::default()); let base = "https://secure914.invalid/server/rest/services/Roads/MapServer"; @@ -14422,7 +14788,8 @@ fn diagnostics_keep_failed_command_context_without_changing_notice_rollback() { id: "diagnostics.testSourceRestore", label: "Diagnostics source restore", mutates: false, - run: panic_run, + run: crate::policy::Handler::new(panic_run), + effects: crate::policy::Effects::NONE, ..*s.registry.get("map.zoomIn").unwrap() }; s.registry.commands.push(command); @@ -14481,7 +14848,8 @@ fn diagnostics_network_requests_belong_only_to_the_command_session() { id: "diagnostics.testRequest", label: "Diagnostics request", mutates: false, - run: request, + run: crate::policy::Handler::new(request), + effects: crate::policy::Effects::NONE, ..*a.registry.get("map.zoomIn").unwrap() }; a.registry.commands.push(command); @@ -16506,6 +16874,7 @@ fn a_look_naming_bands_the_file_hasnt_is_said() { #[test] fn an_overview_of_more_tiles_than_the_fetch_queue_holds_lands() { // This fixture fills the shared native queue; other tests must keep their own requests. + #[cfg(not(target_arch = "wasm32"))] if isolate_process_test("tests::an_overview_of_more_tiles_than_the_fetch_queue_holds_lands") { return; } @@ -16530,6 +16899,7 @@ fn an_overview_of_more_tiles_than_the_fetch_queue_holds_lands() { #[test] fn a_window_of_more_tiles_than_the_fetch_queue_holds_asks_for_them_in_batches() { // This fixture fills the shared native queue; other tests must keep their own requests. + #[cfg(not(target_arch = "wasm32"))] if isolate_process_test("tests::a_window_of_more_tiles_than_the_fetch_queue_holds_asks_for_them_in_batches") { return; } @@ -16747,6 +17117,53 @@ fn a_waiting_sessions_next_command_reads_a_failed_overview_again() { assert_eq!(said(&s), 1, "{:?}", s.notices); } +/// A retry belongs to the read which failed, not whichever command happens to run once +/// its server's wait is over. A native read still recovers when the native user asks again. +#[test] +fn permission_policy_raster_retry_keeps_the_original_owner() { + use crate::policy::{AgentAccess, Effects, Revocation, Scope}; + use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering::SeqCst}, + }; + use std::time::Duration; + struct Live; + impl Revocation for Live { + fn allows(&self, _: Effects) -> bool { + true + } + } + let mut failures = Vec::new(); + for (owned, restricted) in [(true, false), (false, true), (false, false)] { + let (base, sent) = serve_refusing_first(rustgis_data::raster::cog::example(512, 128), 1); + let mut s = Session::new(Project::default()); + let live = Arc::new(AtomicBool::new(true)); + let flag = live.clone(); + let still = owned.then(|| Arc::new(move || flag.load(SeqCst)) as crate::render::rustgis_tiles::cache::Still); + let mut raster = rustgis_map::raster::RasterLayer::unread_in_place(format!("{base}/scene.tif")); + crate::render::rustgis_tiles::cache::with_optional_still(still, || assert!(raster.reload().is_err())); + assert_eq!(raster.read_failure().map(|(_, _, n)| n), Some(1)); + s.map_mut().add_raster("Scene", raster); + live.store(false, SeqCst); + std::thread::sleep(Duration::from_millis(1100)); + if restricted { + let scope = Scope::agent(AgentAccess::Refuse, true, "raster-retry", None, Arc::new(Live)); + s.with_scope(scope, |s| s.run("info.project", &json!({}))).unwrap().unwrap(); + } else { + s.run("info.project", &json!({})).unwrap(); + } + let landed = s.map().layer_by_name("Scene").unwrap().raster.as_ref().unwrap().data.is_some(); + if owned || restricted { + if landed || sent.load(SeqCst) != 0 { + failures.push(format!("retry escaped its owner: owned={owned}, restricted={restricted}")); + } + } else { + assert!(landed && sent.load(SeqCst) > 0, "native maintenance must keep recovering"); + } + } + assert!(failures.is_empty(), "{failures:?}"); +} + /// Where the session doesn't wait for reads in place (the desktop app, whose commands run on the /// window's own thread), a cloud-optimised GeoTIFF is added at once, its header probed as the app /// does, and its overview lands as the map draws; one that can't be read is added too, and says so @@ -16819,6 +17236,10 @@ fn a_cog_added_without_waiting_lands_as_the_map_draws() { /// one at a time would never get their first answer. #[test] fn binding_a_connection_reads_its_in_place_rasters_together() { + // The fixture changes process-wide S3 accounts; keep parallel readers in their own store. + if isolate_process_test("tests::binding_a_connection_reads_its_in_place_rasters_together") { + return; + } use rustgis_map::raster::RasterLayer; use std::time::Duration; let base = serve_s3_together(vec![ @@ -16851,6 +17272,10 @@ fn binding_a_connection_reads_its_in_place_rasters_together() { /// draws. Waiting, the same choice reads them before it answers, and they are reopened. #[test] fn a_connection_chosen_without_waiting_counts_its_in_place_rasters_as_reading() { + // The fixture changes process-wide S3 accounts; keep parallel readers in their own store. + if isolate_process_test("tests::a_connection_chosen_without_waiting_counts_its_in_place_rasters_as_reading") { + return; + } use rustgis_map::raster::RasterLayer; use std::time::Duration; let base = serve_s3_together(vec![ @@ -18713,6 +19138,169 @@ fn a_zarr_step_is_read_off_the_frame_in_the_app() { let _ = std::fs::remove_dir_all(dir); } +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_review_zarr_actorless_scoped_moves_wait_for_the_requested_step() { + use crate::policy::{AgentAccess, Call, Effects, Outcome, Revocation, Scope}; + use std::sync::Arc; + struct Live; + impl Revocation for Live { + fn allows(&self, _: Effects) -> bool { + true + } + } + let _cache = zarr_cache_lock(); + for agent in [false, true] { + let (dir, path) = zarr_store_over_time(if agent { "review-agent-step" } else { "review-assistant-step" }); + let mut s = Session::new(Project::default()); + s.zarr_off_frame = true; + let id = s.run("layer.addZarr", &json!({"path":path,"variable":"t","time":true})).unwrap()["layer"].as_u64().unwrap() as u32; + let live = Arc::new(Live); + let scope = + if agent { Scope::agent(AgentAccess::Allow, false, "zarr-actorless", None, live) } else { Scope::assistant(1461, live) }; + let call = Call::command("time.goTo", json!({"step":1})); + if agent { + assert!(matches!(s.review(&scope, &call), Outcome::Allowed)); + s.run_scoped(scope, &call).unwrap(); + } else { + let Outcome::NeedsApproval(pending) = s.review(&scope, &call) else { panic!("the assistant's local read must ask") }; + s.run_permitted(scope, pending.allow(), &call).unwrap(); + } + // No frame has landed a worker: the actual command must have shown its own requested step. + let raster = s.map().layer(id).unwrap().raster.as_ref().unwrap(); + assert_eq!(raster.time_zarr.as_ref().unwrap().shown, Some(1)); + assert_eq!(raster.data.as_ref().unwrap().bands[0].data[0], 108.0); + assert!(s.actor.is_none() && !s.zarr_waiting()); + std::fs::remove_dir_all(dir).unwrap(); + } +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_review_zarr_worker_rechecks_captured_still_before_a_local_read() { + if isolate_process_test("tests::permission_review_zarr_worker_rechecks_captured_still_before_a_local_read") { + return; + } + use crate::policy::{AgentAccess, Effects, Revocation, Scope}; + use crate::render::rustgis_tiles::cache; + use std::sync::{ + Arc, Mutex, + atomic::{AtomicBool, Ordering}, + mpsc, + }; + let _cache = zarr_cache_lock(); + struct Live; + impl Revocation for Live { + fn allows(&self, _: Effects) -> bool { + true + } + } + let mut failures = Vec::new(); + for (tag, revoke_before, revoke_after, foreign_poll, scoped_poll) in [ + ("native", false, false, false, false), + ("revoked-before", true, false, false, false), + ("revoked-after", false, true, false, false), + ("other-session", true, false, true, false), + ("foreign-scope", true, false, false, true), + ] { + let (dir, path) = zarr_store_over_time(&format!("review-worker-{tag}")); + let mut s = Session::new(Project::default()); + s.zarr_off_frame = true; + let id = s.run("layer.addZarr", &json!({"path":path,"variable":"t","time":true})).unwrap()["layer"].as_u64().unwrap() as u32; + let zt = s.map().layer(id).unwrap().raster.as_ref().unwrap().time_zarr.as_ref().unwrap(); + let key = crate::cmd::zarr::slice_key(zt, 1); + let caller = std::thread::current().id(); + let live = Arc::new(AtomicBool::new(true)); + let current = live.clone(); + let held = AtomicBool::new(false); + let (started, entered) = mpsc::sync_channel(1); + let (release, released) = mpsc::sync_channel(1); + let released = Mutex::new(released); + let still = Arc::new(move || { + if std::thread::current().id() != caller && !held.swap(true, Ordering::SeqCst) { + let _ = started.send(()); + if released.lock().unwrap().recv_timeout(std::time::Duration::from_secs(10)).is_err() { + return false; + } + } + current.load(Ordering::SeqCst) + }); + let mut completed = 0; + crate::cmd::zarr::landed_since(&mut completed); + let mark = s.notice_mark(); + cache::with_still(still, || s.run("time.goTo", &json!({"step":1}))).unwrap(); + entered.recv_timeout(std::time::Duration::from_secs(10)).expect("the actual worker checks its initiating predicate"); + assert_eq!(s.map().layer(id).unwrap().raster.as_ref().unwrap().time_zarr.as_ref().unwrap().shown, Some(0)); + live.store(!revoke_before, Ordering::SeqCst); + release.send(()).unwrap(); + let end = std::time::Instant::now() + std::time::Duration::from_secs(10); + // This isolated scenario has one worker: observe completion before any frame can consume it. + while !crate::cmd::zarr::landed_since(&mut completed) { + assert!(std::time::Instant::now() < end, "the held worker never completed"); + std::thread::sleep(std::time::Duration::from_millis(2)); + } + if revoke_after { + live.store(false, Ordering::SeqCst); + } + if foreign_poll { + let mut other = Session::new(Project::default()); + other.zarr_off_frame = true; + other.follow_time_landed(); + } + if scoped_poll { + let scope = Scope::agent(AgentAccess::Refuse, false, "foreign-zarr", None, Arc::new(Live)); + assert!(!s.with_scope(scope, |s| s.follow_time_landed()).unwrap(), "foreign scope must not consume the native completion"); + assert_eq!(s.map().layer(id).unwrap().raster.as_ref().unwrap().time_zarr.as_ref().unwrap().shown, Some(0)); + assert!(s.zarr_waiting(), "the original completion remains for its owner"); + } + while s.zarr_waiting() { + assert!(std::time::Instant::now() < end, "the completed Zarr worker never landed"); + s.follow_time_landed(); + std::thread::sleep(std::time::Duration::from_millis(2)); + } + let revoked = revoke_before || revoke_after; + let raster = s.map().layer(id).unwrap().raster.as_ref().unwrap(); + let value = raster.data.as_ref().map(|data| data.bands[0].data[0]); + if raster.time_zarr.as_ref().unwrap().shown != Some(1) || value != if revoked { None } else { Some(108.0) } { + failures.push(format!("{tag}: completed read applied {value:?}")); + } + if crate::cmd::zarr::kept_slice(&key).is_some() != !revoke_before { + failures.push(format!("{tag}: invalid cache publication")); + } + let errors = s.notices_since(mark).iter().filter(|notice| notice.error).count(); + if errors != usize::from(revoked) { + failures.push(format!("{tag}: expected one failure notice when revoked, got {errors}")); + } + std::fs::remove_dir_all(dir).unwrap(); + } + assert!(failures.is_empty(), "{failures:?}"); +} + +/// A cached slice and a cold local read both require the current initiating carrier. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_review_zarr_slice_cache_never_bypasses_a_revoked_reader() { + use crate::render::rustgis_tiles::cache; + let _cache = zarr_cache_lock(); + let mut failures = Vec::new(); + for cached in [false, true] { + let (dir, path) = zarr_store_over_time(&format!("review-cache-owner-{cached}")); + let mut s = Session::new(Project::default()); + let id = s.run("layer.addZarr", &json!({"path":path,"variable":"t","time":true})).unwrap()["layer"].as_u64().unwrap() as u32; + let zt = s.map().layer(id).unwrap().raster.as_ref().unwrap().time_zarr.as_ref().unwrap(); + let key = crate::cmd::zarr::slice_key(zt, 1); + if cached { + crate::cmd::zarr::read_step(zt, 1, key.clone()).unwrap(); + } + let result = cache::with_still(std::sync::Arc::new(|| false), || crate::cmd::zarr::read_step(zt, 1, key.clone())); + if result.is_ok() || crate::cmd::zarr::kept_slice(&key).is_some() != cached { + failures.push(format!("cached={cached}: revoked reader obtained or published pixels")); + } + std::fs::remove_dir_all(dir).unwrap(); + } + assert!(failures.is_empty(), "{failures:?}"); +} + /// A Zarr store in a folder: its variables listed (with time labels), a slice added by command /// and by Add Data, saved by folder and selection and read again when the project opens; a slice /// the app read itself handed over by id. @@ -18838,6 +19426,10 @@ fn serve_s3() -> (String, std::sync::Arc>>) { /// redirected. #[test] fn s3_connections_listings_and_objects() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::s3_connections_listings_and_objects") { + return; + } let (base, seen) = serve_s3(); let mut s = Session::new(Project::default()); let r = s @@ -19157,6 +19749,7 @@ fn layer_address_redaction_preserves_templates_and_local_paths() { ("/tmp/data?key=not-a-web-secret", "/tmp/data?key=not-a-web-secret"), ("upload:map?token=local", "upload:map?token=local"), ("postgres:host=localhost dbname=gis", "postgres:host=localhost dbname=gis"), + ("postgresql://user@host.invalid/db%40tag", "postgresql://user@host.invalid/db%40tag"), ]; for (original, expected) in cases { let address = match crate::durable::public_address(original) { @@ -19166,6 +19759,14 @@ fn layer_address_redaction_preserves_templates_and_local_paths() { }; assert_eq!(address, expected); } + let original = "postgresql://user:p@ss/word@host.invalid/db"; + let mut s = world(); + s.map_mut().layers[0].dataset.source = Some(original.into()); + let (text, report) = s.project.saved_text(None).unwrap(); + let saved: Value = serde_json::from_str(&text).unwrap(); + assert_eq!(saved["maps"][0]["layers"][0]["dataset"]["source"], "postgresql:"); + assert_eq!(report.withheld, 1, "ambiguous connection output is counted instead of retargeted"); + assert_eq!(s.map().layers[0].dataset.source.as_deref(), Some(original), "the live connection is unchanged"); let prefix = crate::render::rustgis_tiles::s3::ANONYMOUS; let crate::durable::Kept::Changed(anonymous) = crate::durable::public_address(&format!("{prefix}https://bucket.example/a?token=secret")) @@ -19453,6 +20054,10 @@ fn saved_projects_keep_no_layer_tokens() { #[test] fn connections_commands() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::connections_commands") { + return; + } use crate::render::rustgis_tiles::s3; let mut s = Session::new(Project::default()); let a = s.run("connection.new", &json!({"kind":"wms","address":"https://a.example/wms?token=t","name":"A"})).unwrap(); @@ -19531,6 +20136,10 @@ fn connections_commands() { #[test] fn connections_loaded_names_route_login_and_remove_to_the_right_endpoint() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::connections_loaded_names_route_login_and_remove_to_the_right_endpoint") { + return; + } use crate::render::rustgis_tiles::s3; let mut s = Session::new(Project::default()); s.connections = crate::connections::Connections::from_json(&json!({"connections":[ @@ -19550,6 +20159,10 @@ fn connections_loaded_names_route_login_and_remove_to_the_right_endpoint() { #[test] fn connections_s3_rename_cannot_inherit_a_destination_session() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::connections_s3_rename_cannot_inherit_a_destination_session") { + return; + } use crate::render::rustgis_tiles::s3; let mut s = Session::new(Project::default()); let (old, destination, occupied) = ("rename-test-old", "rename-test-destination", "rename-test-occupied"); @@ -19589,6 +20202,10 @@ fn connections_s3_rename_cannot_inherit_a_destination_session() { #[test] fn connection_test_probes_and_answers() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::connection_test_probes_and_answers") { + return; + } use crate::render::rustgis_tiles::s3; let mut s = Session::new(Project::default()); let mut wms = json!({"kind":"wms","address":"https://a.example/wms","probe":true}); @@ -19638,6 +20255,10 @@ fn connection_test_probes_and_answers() { /// connection's is refused, and nothing browsed is saved or undoable. #[test] fn connection_browse_levels_and_adds() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::connection_browse_levels_and_adds") { + return; + } use crate::render::rustgis_tiles::s3; let mut s = Session::new(Project::default()); let new = |s: &mut Session, p: Value| s.run("connection.new", &p).unwrap(); @@ -19921,6 +20542,23 @@ fn catalog_commands_refuse_bad_parameters() { /// Overture's files are added all or none: one that can't be read takes the ones added before it /// out again, so trying again doesn't add them twice. +/// Runs the local catalog fixture through admission, as the real Overture handler does. +fn catalog_files(s: &mut Session, urls: &[String]) -> crate::CmdResult { + if s.registry.get("test.catalogFiles").is_none() { + s.registry.commands.push( + crate::CommandSpec::new("test.catalogFiles", "Local catalog files", "Test", |s, p| { + let urls = p["urls"].as_array().unwrap().iter().map(|url| url.as_str().unwrap().to_string()).collect::>(); + crate::cmd::catalogs::add_files(s, "building", &urls) + }) + .params(r#"{"urls": [text]}"#) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)) + .roles(&[]), + ); + } + s.run("test.catalogFiles", &json!({"urls":urls})) +} + +/// Overture's files are added all or none; the fixture retains their real command authority. #[test] fn overture_files_are_added_all_or_none() { use rustgis_data::{Dataset, Feature, Field, FieldType, Geometry, GeometryType, Value}; @@ -19934,8 +20572,9 @@ fn overture_files_are_added_all_or_none() { let mut s = Session::new(Project::default()); let before = s.map().layers.len(); let urls = vec![format!("{good}/part-0.fgb"), format!("{bad}/part-1.fgb")]; - let e = crate::cmd::catalogs::add_files(&mut s, "building", &urls).unwrap_err(); + let e = catalog_files(&mut s, &urls).unwrap_err(); assert!(e.contains("none of the 2 files was added"), "{e}"); + assert!(e.to_ascii_lowercase().contains("flatgeobuf"), "the actual bad file was reached: {e}"); assert_eq!(s.map().layers.len(), before, "the first file's layer was taken out again"); // The app's way (the files found and opened already) takes Overture's own files only: any // other address is refused before anything is read. @@ -19949,7 +20588,7 @@ fn overture_files_are_added_all_or_none() { assert_eq!(s.map().layers.len(), before); // Both readable: both added, numbered. let urls = vec![format!("{good}/part-0.fgb"), format!("{good}/part-1.fgb")]; - let r = crate::cmd::catalogs::add_files(&mut s, "building", &urls).unwrap(); + let r = catalog_files(&mut s, &urls).unwrap(); 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:?}"); @@ -19973,7 +20612,7 @@ fn a_failed_multi_file_add_leaves_the_map_as_it_was() { s.active_layer = Some(rivers); let project = serde_json::to_string(&s.project).unwrap(); let urls = vec![format!("{good}/part-0.fgb"), format!("{good}/part-1.fgb"), format!("{bad}/part-2.fgb")]; - assert!(crate::cmd::catalogs::add_files(&mut s, "building", &urls).is_err()); + assert!(catalog_files(&mut s, &urls).is_err()); assert_eq!(serde_json::to_string(&s.project).unwrap(), project, "the same layers, in the same order"); assert_eq!(s.active_layer, Some(rivers)); // A box across the antimeridian (west past east) is an area, not an error. @@ -21553,6 +22192,27 @@ fn merged_undo_steps_undo_as_one() { let before = s.undo_depth(); s.run("layout.update", &json!({"element": title.id, "x": 41.0})).unwrap(); assert_eq!(s.undo_depth(), before + 1); + + // Failed admission, including the plugin branch, also consumes the next-command merge key. + let mut refused_merges = Vec::new(); + for (index, id) in ["map.addTiles", "plugin:missing-merge-test"].into_iter().enumerate() { + let x = 50.0 + index as f64 * 10.0; + nudge(&mut s, "refused", x); + s.merge_undo("refused"); + let denied = s + .with_scope(crate::policy::Scope::peer(), |s| s.run(id, &json!({"url":"https://merge-refusal.invalid/{z}/{x}/{y}.png"}))) + .unwrap(); + assert!(denied.is_err(), "the command must be refused before its handler runs"); + let before = s.undo_depth(); + s.run("layout.update", &json!({"element":title.id,"x":x+1.0})).unwrap(); + let separate = s.undo_depth() == before + 1; + s.run("edit.undo", &Value::Null).unwrap(); + refused_merges.push((id, separate, at(&s).0 == x)); + } + assert!( + refused_merges.iter().all(|(_, separate, restored)| *separate && *restored), + "failed admission reused its merge key: {refused_merges:?}" + ); } #[test] @@ -22018,8 +22678,8 @@ fn a_failed_command_keeps_the_project_open_before_it() { let mut s = world(); // Opened from a file, edited, unsaved, with a task made here. s.path = Some("atlas.rgis".into()); + s.run("task.new", &json!({"name":"mine", "steps":[]})).unwrap(); s.run("map.rename", &json!({"name": "Edited"})).unwrap(); - s.trusted_tasks.insert("mine".into()); let (name, opened, undo) = (s.project.name.clone(), s.opened, s.undo_label()); assert!(undo.is_some() && s.dirty()); let r = s.guarded("Task", true, |s| { @@ -22031,7 +22691,7 @@ fn a_failed_command_keeps_the_project_open_before_it() { // It comes back with what it held: its undo history, its unsaved state, the trust it had, and // its file, so Save writes there. assert_eq!(s.undo_label(), undo); - assert!(s.dirty() && s.trusted_tasks.contains("mine")); + assert!(s.dirty() && s.item_trusted("task", "mine")); assert_eq!(s.path.as_deref(), Some(std::path::Path::new("atlas.rgis"))); s.run("edit.undo", &Value::Null).unwrap(); assert_ne!(s.map().name, "Edited", "the edit before still undoes"); @@ -22067,7 +22727,7 @@ fn a_failed_command_keeps_the_project_open_before_it() { // Opened for good, it counts, and the old project's history and file go with it. s.run("project.new", &json!({"name": "Other"})).unwrap(); assert_eq!((s.opened, s.undo_label(), s.path.as_deref()), (opened + 1, None, None)); - assert!(!s.trusted_tasks.contains("mine")); + assert!(!s.item_trusted("task", "mine")); } /// A run from the history as code (#539): a Python call, a Script pane line, a CLI command. A @@ -22085,11 +22745,16 @@ fn a_run_is_copied_from_the_history_as_code() { let py = code(&mut s, 0, "python").unwrap(); assert!(py.starts_with("s.run(\n \"gp.run\",\n tool=\"buffer\",\n params={\n"), "{py}"); assert!(py.contains("\"distance\": 100,") && py.contains("\"input\": \"Cities\",") && py.ends_with(")"), "{py}"); - assert_eq!(code(&mut s, 0, "command").unwrap(), r#"gp.run {"params":{"distance":100,"input":"Cities"},"tool":"buffer"}"#); + assert_eq!( + code(&mut s, 0, "command").unwrap(), + r#"gp.run {"params":{"dissolve":"None","distance":100,"input":"Cities","units":"Kilometers"},"tool":"buffer"}"# + ); let cli = code(&mut s, 0, "cli").unwrap(); assert!( cli.starts_with("rustgis-cli run --project '") - && cli.ends_with(r#"--cmd 'gp.run={"params":{"distance":100,"input":"Cities"},"tool":"buffer"}'"#), + && cli.ends_with( + r#"--cmd 'gp.run={"params":{"dissolve":"None","distance":100,"input":"Cities","units":"Kilometers"},"tool":"buffer"}'"# + ), "{cli}" ); // A point buffer: by its command, with the point. @@ -24215,7 +24880,7 @@ fn dem_tiles_mosaic_clip_by_polygon_share_a_stretch_and_report_overviews() { assert!(s.notices_since(mark).iter().any(|n| n.text == said), "{:?}", s.notices_since(mark)); std::fs::remove_dir_all(&dir).ok(); // Bare file names read files too: tasks and notebooks from elsewhere may not run it. - assert!(crate::cmd::items::touches_files("raster.mosaic", &json!({"inputs": ["a.tif", "b.tif"]}))); + assert!(crate::cmd::items::touches_files(&s, "raster.mosaic", &json!({"inputs": ["a.tif", "b.tif"]}))); for (bad, why) in [ (json!({"inputs": ["west"]}), "two or more"), (json!({"inputs": ["west", "coarse"]}), "cell size"), @@ -24371,9 +25036,9 @@ fn model_graphs(s: &mut Session) { assert_eq!(r["outputs"][0], "Buffers"); // Saving over a model from a file keeps it one. s.run("model.save", &json!({"name": "From disk", "graph": both, "imported": true})).unwrap(); - assert!(!s.trusted_models.contains("from disk")); + assert!(!s.item_trusted("model", "from disk")); s.run("model.save", &json!({"name": "From disk", "graph": both, "rename": "Renamed"})).unwrap(); - assert!(!s.trusted_models.contains("renamed") && !s.trusted_models.contains("from disk")); + assert!(!s.item_trusted("model", "renamed") && !s.item_trusted("model", "from disk")); s.run("model.remove", &json!({"name": "Renamed"})).unwrap(); // Problems: a missing layer, an unconnected required input, a loop. @@ -26345,7 +27010,29 @@ fn project_template_carries_no_data() { table.source = Some(path.display().to_string()); s.project.add_table(table); assert!(!s.project.offline_areas.is_empty()); + let offline = s.project.offline_areas.len(); + let archived: usize = s + .project + .maps + .iter() + .chain(s.project.offline_areas.iter().map(|area| &area.data)) + .flat_map(|map| &map.layers) + .filter_map(|layer| layer.design.archive.as_ref()) + .map(Vec::len) + .sum(); + assert!(archived > 0, "the fixture has archived edits to omit"); + let before = serde_json::to_value(&s.project).unwrap(); + let mark = s.notice_mark(); let (_, bytes) = crate::cmd::items::item_bytes(&mut s, "template", &Value::Null).unwrap(); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before, "template export preserves the live project"); + // The streamed layer has both its reload source and record-only provenance; the table + // has its own provenance. All three owning references are omitted, independently of rows. + for (kind, count) in [("offline area(s)", offline), ("archived edit record(s)", archived), ("data source reference(s)", 3)] { + let expected = format!("{count} {kind} left out of the template, which keeps no features"); + let notices: Vec<_> = + s.notices_since(mark).iter().filter(|notice| notice.text.contains(kind)).map(|notice| notice.text.as_str()).collect(); + assert_eq!(notices, [expected.as_str()], "one exact omission count for {kind}"); + } let text = String::from_utf8(bytes).unwrap(); assert!(!text.contains(path.to_str().unwrap()), "the streamed layer's file is in the template"); let t: Project = serde_json::from_str(&text).unwrap(); @@ -26359,6 +27046,10 @@ fn project_template_carries_no_data() { let mut opened = Session::new(Project::default()); opened.run("project.fromTemplate", &json!({"text": text, "name": "Setup"})).unwrap(); assert!(opened.project.maps.iter().flat_map(|m| &m.layers).all(|l| l.dataset.features.is_empty())); + let mut clean = Session::new(Project::default()); + let mark = clean.notice_mark(); + crate::cmd::items::item_bytes(&mut clean, "template", &Value::Null).unwrap(); + assert!(clean.notices_since(mark).is_empty(), "a clean template has no omissions: {:?}", clean.notices_since(mark)); } #[cfg(not(target_arch = "wasm32"))] @@ -26377,7 +27068,15 @@ fn geopackage_template_drops_edit_binding() { let before = rows(); let mut s = fixture.session(); let template = fixture.dir.join("roads.rgistemplate"); + let mark = s.notice_mark(); s.run("project.saveTemplate", &json!({"path": template})).unwrap(); + let notices: Vec<_> = s + .notices_since(mark) + .iter() + .filter(|notice| notice.text.contains("data source reference(s)")) + .map(|notice| notice.text.as_str()) + .collect(); + assert_eq!(notices, ["2 data source reference(s) left out of the template, which keeps no features"]); let mut restored = Session::new(Project::default()); restored.run("project.fromTemplate", &json!({"path": template, "name": "Road setup"})).unwrap(); let layer = restored.map().layer_by_name("R").unwrap(); @@ -27610,6 +28309,10 @@ fn calculate_geometry_updates_ellipsoidal_lengths_in_one_undo_step() { /// turned off, none of which changes the project. #[test] fn the_session_log_records_each_command_run_in_the_session() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::the_session_log_records_each_command_run_in_the_session") { + return; + } use crate::project_log::{LOG_LIMIT, LOG_PARAMS_BYTES, LogEntry}; let mut s = world(); // The sample project's making is its first entry: counted from after it. @@ -29184,6 +29887,16 @@ fn built_in_tools_run_off_the_session() { 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 standalone table belongs to the project, so its owned result can land while another map is in view. + s.project.active_map = 0; + let run = tool_start(&mut s, "summaryStatistics", &json!({"input":"Cities","field":"pop_max"})).unwrap(); + s.project.active_map = 1; + let (id, done) = (run.id, run.run(&Control::new())); + tool_complete(&mut s, id, done).unwrap(); + let made = s.run("gp.finish", &json!({"id":id})).unwrap(); + assert_eq!(made["table"], "Cities_Statistics"); + assert!(s.project.tables.iter().any(|t| t.dataset.name == "Cities_Statistics")); + 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(); @@ -30096,9 +30809,11 @@ fn exports_run_on_another_thread() { failed.error = Some("gone.fgb: not found".into()); copy.layers[li].source = Some(failed); let land = |s: &mut Session, copy: &rustgis_map::Map| { - let wrote = crate::cmd::exports::drawn(std::slice::from_ref(copy), Vec::new(), Ok(json!({})), at("drawn.png")); + let mut wrote = crate::cmd::exports::drawn(std::slice::from_ref(copy), Vec::new(), Ok(json!({})), at("drawn.png")).unwrap(); let started = export_start(s, "share.exportMap", &json!({"path": at("drawn.png")})).unwrap().started(); - export_land(s, started, wrote).unwrap(); + // This fixture supplies a drawn result directly, using the actual admitted run's owner. + wrote.owner = Some(started.owner.clone()); + export_land(s, started, Ok(wrote)).unwrap(); s.map().layers[li].source.as_ref().and_then(|src| src.error.clone()) }; assert_eq!(land(&mut s, ©).as_deref(), Some("gone.fgb: not found")); @@ -30217,6 +30932,9 @@ fn raster_classes_run_on_another_thread() { #[cfg(not(target_arch = "wasm32"))] #[test] fn reads_made_ahead_are_taken_by_the_command() { + if isolate_process_test("tests::reads_made_ahead_are_taken_by_the_command") { + return; + } use base64::Engine; use rustgis_analysis::control::Control; let dir = std::env::temp_dir().join(format!("rustgis-reads-ahead-{}", std::process::id())); @@ -30233,29 +30951,31 @@ fn reads_made_ahead_are_taken_by_the_command() { let path = dir.join(name); std::fs::write(&path, bytes).unwrap(); let p = json!({"path": path.display().to_string(), "zoom": false}); - let ahead = crate::cmd::reads::read_ahead(&s, "layer.addData", &p).unwrap_or_else(|| panic!("{name} is read ahead")); + let ahead = crate::cmd::reads::read_ahead(&mut s, "layer.addData", &p).unwrap(); + assert!(ahead.reads(), "{name} is read ahead"); let read = ahead.run(&Control::new()).unwrap(); // The file gone: what the command adds can only come from the read. std::fs::remove_file(&path).unwrap(); - read.placed(&mut s); - let r = s.run("layer.addData", &p).unwrap_or_else(|e| panic!("{name}: {e}")); + let r = crate::cmd::reads::read_land(&mut s, read).unwrap_or_else(|e| panic!("{name}: {e}")); assert!(s.read_ahead.is_none(), "{name}: the read was taken"); assert!(r.get("layer").is_some() || r.get("layers").is_some(), "{name}: {r}"); } assert_eq!(s.map().layers.len(), 4, "{:?}", s.map().layers.iter().map(|l| &l.name).collect::>()); // A file read in place (and any the command reads differently) reads as it runs. let gpkg = json!({"path": dir.join("table.gpkg").display().to_string()}); - assert!(crate::cmd::reads::read_ahead(&s, "layer.addData", &gpkg).is_none()); + assert!(!crate::cmd::reads::read_ahead(&mut s, "layer.addData", &gpkg).unwrap().reads()); // Load All's read, on a copy of the streamed layer. let path = dir.join("streamed.geojson"); std::fs::write(&path, points).unwrap(); - let id = s.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap()["layer"].as_u64().unwrap(); + let inline = + crate::cmd::reads::read_ahead(&mut s, "layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap(); + assert!(!inline.reads(), "an in-place opening still owns its original inline command"); + let id = crate::cmd::reads::read_land(&mut s, inline.run(&Control::new()).unwrap()).unwrap()["layer"].as_u64().unwrap(); assert!(s.map().layer(id as u32).unwrap().is_streamed()); let p = json!({"layer": id}); - let read = crate::cmd::reads::read_ahead(&s, "layer.loadAll", &p).unwrap().run(&Control::new()).unwrap(); - read.placed(&mut s); - assert_eq!(s.run("layer.loadAll", &p).unwrap()["features"], 2); + let read = crate::cmd::reads::read_ahead(&mut s, "layer.loadAll", &p).unwrap().run(&Control::new()).unwrap(); + assert_eq!(crate::cmd::reads::read_land(&mut s, read).unwrap()["features"], 2); assert!(s.read_ahead.is_none()); assert!(!s.map().layer(id as u32).unwrap().is_streamed()); let unread = |s: &Session| s.notices.iter().filter(|n| n.text.contains("couldn't be read and were left out")).count(); @@ -30269,11 +30989,14 @@ fn reads_made_ahead_are_taken_by_the_command() { let mut s = Session::new(Project::default()); let id = s.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap()["layer"].as_u64().unwrap(); let p = json!({"layer": id}); - if ahead { - crate::cmd::reads::read_ahead(&s, "layer.loadAll", &p).unwrap().run(&Control::new()).unwrap().placed(&mut s); - } let before = unread(&s); - assert_eq!(s.run("layer.loadAll", &p).unwrap()["features"], 1); + let result = if ahead { + let read = crate::cmd::reads::read_ahead(&mut s, "layer.loadAll", &p).unwrap().run(&Control::new()).unwrap(); + crate::cmd::reads::read_land(&mut s, read) + } else { + s.run("layer.loadAll", &p) + }; + assert_eq!(result.unwrap()["features"], 1); assert_eq!(unread(&s), before + 1, "{:?}", s.notices.iter().map(|n| &n.text).collect::>()); } @@ -30282,27 +31005,124 @@ fn reads_made_ahead_are_taken_by_the_command() { std::fs::write(&path, points).unwrap(); let id = s.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap()["layer"].as_u64().unwrap(); let p = json!({"layer": id}); - let read = crate::cmd::reads::read_ahead(&s, "layer.loadAll", &p).unwrap().run(&Control::new()).unwrap(); + let read = crate::cmd::reads::read_ahead(&mut s, "layer.loadAll", &p).unwrap().run(&Control::new()).unwrap(); let mut extra = s.map().layer(id as u32).unwrap().dataset.fields[0].clone(); extra.name = "added".into(); s.map_mut().layer_mut(id as u32).unwrap().dataset.fields.push(extra); - read.placed(&mut s); - s.run("layer.loadAll", &p).unwrap(); + s.run("layer.rename", &json!({"layer": id, "name": "Renamed while reading"})).unwrap(); + crate::cmd::reads::read_land(&mut s, read).unwrap(); + assert_eq!(s.map().layer(id as u32).unwrap().name, "Renamed while reading"); assert!(s.map().layer(id as u32).unwrap().dataset.fields.iter().any(|f| f.name == "added")); + // An opening replaced at the same path cannot trigger a new read under old authority. + let id = s.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap()["layer"].as_u64().unwrap(); + let read = crate::cmd::reads::read_ahead(&mut s, "layer.loadAll", &json!({"layer": id})).unwrap().run(&Control::new()).unwrap(); + s.map_mut().layer_mut(id as u32).unwrap().source.as_mut().unwrap().reopen().unwrap(); + assert!(crate::cmd::reads::read_land(&mut s, read).unwrap_err().contains("source changed")); + assert!(s.map().layer(id as u32).unwrap().is_streamed() && s.read_ahead.is_none()); + // A cancelled read gives nothing to add, and says so. let path = dir.join("again.csv"); std::fs::write(&path, "name,x,y\nA,8,47\n").unwrap(); - let ahead = crate::cmd::reads::read_ahead(&s, "layer.addData", &json!({"path": path.display().to_string()})).unwrap(); + let ahead = crate::cmd::reads::read_ahead(&mut s, "layer.addData", &json!({"path": path.display().to_string()})).unwrap(); let control = Control::new(); control.cancel(); assert!(ahead.run(&control).err().unwrap().contains("cancelled")); let _ = std::fs::remove_dir_all(&dir); } +/// Read admission, the first real source read and delivery retain one revocable owner. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn background_reads_keep_their_owner_before_read_and_landing() { + if isolate_process_test("tests::background_reads_keep_their_owner_before_read_and_landing") { + return; + } + use crate::cmd::reads::{read_ahead, read_land}; + use crate::policy::{AgentAccess, Effects, Revocation, Scope}; + use rustgis_analysis::control::Control; + use rustgis_data::source::{Batch, FeatureSource, Request, SourceInfo}; + use std::sync::{ + Arc, + atomic::{AtomicBool, AtomicUsize, Ordering}, + }; + + struct Live(AtomicBool); + impl Revocation for Live { + /// This fixture revokes only its own admitted caller. + fn allows(&self, _: Effects) -> bool { + self.0.load(Ordering::SeqCst) + } + } + struct OwnedSource { + inner: Box, + reads: Arc, + } + impl FeatureSource for OwnedSource { + /// The real FlatGeobuf source supplies the schema. + fn info(&self) -> &SourceInfo { + self.inner.info() + } + /// The actual worker's first read must carry its admitted live predicate. + fn read(&self, request: &Request) -> rustgis_data::Result { + assert!(rustgis_data::source::remote::current_still().is_some_and(|still| still())); + self.reads.fetch_add(1, Ordering::SeqCst); + self.inner.read(request) + } + } + let mut s = world(); + let id = s.map().layer_by_name("Cities").unwrap().id; + let bytes = Arc::new(rustgis_data::source::fgb::write(&s.map().layer(id).unwrap().dataset).unwrap()); + let inner = rustgis_data::source::open_shared("owned.fgb", bytes.clone(), None).unwrap(); + let reads = Arc::new(AtomicUsize::new(0)); + s.map_mut().layer_mut(id).unwrap().source = + Some(rustgis_map::source::SourceLayer::from_bytes("owned.fgb", bytes, None, Box::new(OwnedSource { inner, reads: reads.clone() }))); + let live = Arc::new(Live(AtomicBool::new(true))); + let owner = Scope::agent(AgentAccess::Allow, false, "owned-read", None, live.clone()); + let denied = Scope::agent(AgentAccess::Refuse, false, "denied-read", None, live.clone()); + // Refuse denies external reads; the owned FlatGeobuf source below is already in memory. + assert!(s.with_scope(denied, |s| read_ahead(s, "layer.addData", &json!({"path":"refused-read.geojson"}))).unwrap().is_err()); + s.guarded("Read fixture", false, |s| { + assert!(read_ahead(s, "layer.loadAll", &json!({"layer":id})).is_err(), "a handler cannot queue work beyond its transaction"); + Ok(Value::Null) + }) + .unwrap(); + let ahead = s.with_scope(owner.clone(), |s| read_ahead(s, "layer.loadAll", &json!({"layer":id}))).unwrap().unwrap(); + live.0.store(false, Ordering::SeqCst); + assert!(std::thread::spawn(move || ahead.run(&Control::new())).join().unwrap().is_err()); + assert_eq!(reads.load(Ordering::SeqCst), 0, "denied or revoked reads never enter their source"); + + live.0.store(true, Ordering::SeqCst); + let ahead = s.with_scope(owner.clone(), |s| read_ahead(s, "layer.loadAll", &json!({"layer":id}))).unwrap().unwrap(); + let read = std::thread::spawn(move || ahead.run(&Control::new())).join().unwrap().unwrap(); + assert!(reads.load(Ordering::SeqCst) > 0); + let foreign = Scope::agent(AgentAccess::Allow, false, "foreign-read", None, live.clone()); + assert!(s.with_scope(foreign, |s| read_land(s, read)).unwrap().unwrap_err().contains("another caller")); + assert!(s.read_ahead.is_none() && s.map().layer(id).unwrap().is_streamed()); + + let ahead = s.with_scope(owner.clone(), |s| read_ahead(s, "layer.loadAll", &json!({"layer":id}))).unwrap().unwrap(); + let read = ahead.run(&Control::new()).unwrap(); + live.0.store(false, Ordering::SeqCst); + assert!(read_land(&mut s, read).unwrap_err().contains("revoked"), "revocation also refuses delivery at native root"); + assert!(s.read_ahead.is_none() && s.map().layer(id).unwrap().is_streamed()); + + live.0.store(true, Ordering::SeqCst); + s.active_layer = Some(id); + let ahead = s.with_scope(owner.clone(), |s| read_ahead(s, "layer.loadAll", &json!({}))).unwrap().unwrap(); + s.active_layer = s.map().layer_by_name("Countries").map(|layer| layer.id); + let read = ahead.run(&Control::new()).unwrap(); + assert_eq!(s.with_scope(owner, |s| read_land(s, read)).unwrap().unwrap()["layer"], id); + assert!(!s.map().layer(id).unwrap().is_streamed()); + assert!(s.read_ahead.is_none() && rustgis_data::source::remote::current_still().is_none()); +} + #[cfg(not(target_arch = "wasm32"))] #[test] fn a_models_tools_run_ahead_are_taken_by_its_run() { + if isolate_process_test("tests::a_models_tools_run_ahead_are_taken_by_its_run") { + return; + } + use crate::cmd::reads::{read_ahead, read_land}; use rustgis_analysis::control::Control; let session = || { let mut s = Session::new(Project::default()); @@ -30333,8 +31153,8 @@ fn a_models_tools_run_ahead_are_taken_by_its_run() { // Both tools are taken: what the run adds, keeps in history and puts in Undo is the same. let mut s = session(); let depth = s.undo_depth(); - crate::cmd::reads::read_ahead(&s, "model.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); - let r = s.run("model.run", &p).unwrap(); + let read = read_ahead(&mut s, "model.run", &p).unwrap().run(&Control::stoppable()).unwrap(); + let r = read_land(&mut s, read).unwrap(); assert_eq!(s.tools_taken, 2, "{r}"); assert!(s.read_ahead.is_none() && s.tools_ahead.is_none()); assert_eq!(rings(&s), rings(&here)); @@ -30351,12 +31171,12 @@ fn a_models_tools_run_ahead_are_taken_by_its_run() { s.map_mut().layers[0].dataset.push_next(p, Vec::new()); }; let mut s = session(); - crate::cmd::reads::read_ahead(&s, "model.run", &json!({"graph": graph})).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); + let read = read_ahead(&mut s, "model.run", &json!({"graph": graph})).unwrap().run(&Control::stoppable()).unwrap(); add(&mut s); let mut here = session(); add(&mut here); here.run("model.run", &p).unwrap(); - s.run("model.run", &p).unwrap(); + read_land(&mut s, read).unwrap(); assert_eq!(s.tools_taken, 0); assert!(s.tools_ahead.is_none()); assert_eq!(rings(&s), rings(&here)); @@ -30364,7 +31184,7 @@ fn a_models_tools_run_ahead_are_taken_by_its_run() { // A run ahead that was cancelled is an error, and runs nothing. let control = Control::stoppable(); control.cancel(); - assert!(crate::cmd::reads::read_ahead(&session(), "model.run", &p).unwrap().run(&control).is_err()); + assert!(read_ahead(&mut session(), "model.run", &p).unwrap().run(&control).is_err()); // A model saved as steps takes them too, and so does one with a tool that fails. let mut s = session(); @@ -30377,13 +31197,13 @@ fn a_models_tools_run_ahead_are_taken_by_its_run() { ) .unwrap(); let p = json!({"name": "Steps"}); - crate::cmd::reads::read_ahead(&s, "model.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); - s.run("model.run", &p).unwrap(); + let read = read_ahead(&mut s, "model.run", &p).unwrap().run(&Control::stoppable()).unwrap(); + read_land(&mut s, read).unwrap(); assert_eq!(s.tools_taken, 2); s.run("model.new", &json!({"name": "Fails", "steps": [{"tool": "buffer", "params": {"input": "Points", "distance": "x"}}]})).unwrap(); let p = json!({"name": "Fails"}); - crate::cmd::reads::read_ahead(&s, "model.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); - let e = s.run("model.run", &p).unwrap_err(); + let read = read_ahead(&mut s, "model.run", &p).unwrap().run(&Control::stoppable()).unwrap(); + let e = read_land(&mut s, read).unwrap_err(); assert!(e.contains("step 1"), "{e}"); assert_eq!(s.tools_taken, 3, "the failure is taken too"); @@ -30403,26 +31223,94 @@ fn a_models_tools_run_ahead_are_taken_by_its_run() { ) .unwrap(); let p = json!({"name": "Writes"}); - crate::cmd::reads::read_ahead(&s, "model.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); + let read = read_ahead(&mut s, "model.run", &p).unwrap().run(&Control::stoppable()).unwrap(); assert!(!file.exists(), "the copy wrote nothing"); - s.run("model.run", &p).unwrap(); + read_land(&mut s, read).unwrap(); assert_eq!(s.tools_taken, 1); assert!(file.exists()); assert_eq!(s.project.gp_history.len(), 3); // A selection made meanwhile is an input changed: the tools run here, on it. let mut s = session(); - crate::cmd::reads::read_ahead(&s, "model.run", &json!({"graph": graph})).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); + let read = read_ahead(&mut s, "model.run", &json!({"graph": graph})).unwrap().run(&Control::stoppable()).unwrap(); s.map_mut().layers[0].selection = vec![0]; - s.run("model.run", &json!({"graph": graph})).unwrap(); + read_land(&mut s, read).unwrap(); assert_eq!(s.tools_taken, 0); assert_eq!(s.map().layer_by_name("Rings").unwrap().dataset.features.len(), 1); + // The copied session must not hide a change to the initiating session's login. + let p = json!({"graph":graph.clone()}); + let mut s = session(); + let connection = s + .run( + "connection.new", + &json!({ + "kind":"wms", "address":"https://model-owner.example.invalid", "auth":"header", "keyName":"X-Key" + }), + ) + .unwrap()["id"] + .as_str() + .unwrap() + .to_string(); + s.run("connection.login", &json!({"id":connection,"apiKey":"RGSYNTH_model_owner_before"})).unwrap(); + let ahead = read_ahead(&mut s, "model.run", &p).unwrap(); + assert!(ahead.reads(), "the native model keeps upstream off-frame execution"); + let read = ahead.run(&Control::stoppable()).unwrap(); + s.run("connection.login", &json!({"id":connection,"apiKey":"RGSYNTH_model_owner_after"})).unwrap(); + let before = serde_json::to_value(&s.project).unwrap(); + let error = read_land(&mut s, read).unwrap_err(); + assert!(error.contains("account"), "{error}"); + assert_eq!(s.tools_taken, 0); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before); + + // A scoped caller stays on its original session instead of becoming User in a copy. + use crate::policy::{AgentAccess, Effects, Revocation, Scope}; + use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, + }; + struct Live(AtomicBool); + impl Revocation for Live { + /// Revokes only this fixture's initiating caller. + fn allows(&self, _: Effects) -> bool { + self.0.load(Ordering::SeqCst) + } + } + for command in ["model.run", "script.run"] { + let params = if command == "model.run" { p.clone() } else { json!({"script":format!("model.run {p}")}) }; + let mut s = session(); + let live = Arc::new(Live(AtomicBool::new(true))); + let denied = Scope::agent(AgentAccess::Allow, false, "model-denied", None, live.clone()); + let error = s.with_scope(denied, |s| read_ahead(s, command, ¶ms)).unwrap().err().unwrap(); + assert!(error.contains("reserved"), "{error}"); + let scope = Scope::agent(AgentAccess::Full, false, "model-owner", None, live.clone()); + let ahead = s.with_scope(scope.clone(), |s| read_ahead(s, command, ¶ms)).unwrap().unwrap(); + assert!(!ahead.reads(), "scoped work is not run in a native User copy"); + live.0.store(false, Ordering::SeqCst); + assert!(ahead.run(&Control::stoppable()).err().unwrap().contains("revoked")); + live.0.store(true, Ordering::SeqCst); + let read = s.with_scope(scope.clone(), |s| read_ahead(s, command, ¶ms)).unwrap().unwrap().run(&Control::stoppable()).unwrap(); + live.0.store(false, Ordering::SeqCst); + assert!(read_land(&mut s, read).unwrap_err().contains("revoked")); + assert!(rings(&s).is_none()); + live.0.store(true, Ordering::SeqCst); + let read = s.with_scope(scope.clone(), |s| read_ahead(s, command, ¶ms)).unwrap().unwrap().run(&Control::stoppable()).unwrap(); + let foreign = Scope::agent(AgentAccess::Full, false, "another-model-owner", None, live.clone()); + assert!(s.with_scope(foreign, |s| read_land(s, read)).unwrap().unwrap_err().contains("another caller")); + let read = s.with_scope(scope.clone(), |s| read_ahead(s, command, ¶ms)).unwrap().unwrap().run(&Control::stoppable()).unwrap(); + s.with_scope(scope, |s| read_land(s, read)).unwrap().unwrap(); + assert!(rings(&s).is_some()); + assert_eq!(s.tools_taken, 0, "scoped execution ran its own admitted tools"); + } std::fs::remove_dir_all(&dir).ok(); } #[cfg(not(target_arch = "wasm32"))] #[test] fn a_scripts_tools_run_ahead_are_taken_by_its_run() { + if isolate_process_test("tests::a_scripts_tools_run_ahead_are_taken_by_its_run") { + return; + } + use crate::cmd::reads::{read_ahead, read_land}; use rustgis_analysis::control::Control; let session = || { let mut s = Session::new(Project::default()); @@ -30451,8 +31339,8 @@ fn a_scripts_tools_run_ahead_are_taken_by_its_run() { here.run("script.run", &p).unwrap(); let mut s = session(); let depth = s.undo_depth(); - crate::cmd::reads::read_ahead(&s, "script.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); - let r = s.run("script.run", &p).unwrap(); + let read = read_ahead(&mut s, "script.run", &p).unwrap().run(&Control::stoppable()).unwrap(); + let r = read_land(&mut s, read).unwrap(); assert_eq!(s.tools_taken, 3, "{r}"); assert!(s.read_ahead.is_none() && s.tools_ahead.is_none()); assert_eq!(layers(&s), layers(&here)); @@ -30466,9 +31354,9 @@ fn a_scripts_tools_run_ahead_are_taken_by_its_run() { let saved = dir.join("saved.rgis"); let p = json!({"script": format!("{}\nproject.save {}\n{}", tool(1000), json!({"path": saved.display().to_string()}), tool(2000))}); let mut s = session(); - crate::cmd::reads::read_ahead(&s, "script.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); + let read = read_ahead(&mut s, "script.run", &p).unwrap().run(&Control::stoppable()).unwrap(); assert!(!saved.exists(), "the copy saved nothing"); - s.run("script.run", &p).unwrap(); + read_land(&mut s, read).unwrap(); assert_eq!(s.tools_taken, 1); assert!(saved.exists()); assert_eq!(s.project.gp_history.len(), 2); @@ -30479,8 +31367,8 @@ fn a_scripts_tools_run_ahead_are_taken_by_its_run() { let join = r#"gp.spatialJoin {"target": "Points", "join": "Points", "output": "Joined"}"#; let p = json!({"script": format!("{join}\n{}", tool(1000))}); let mut s = session(); - crate::cmd::reads::read_ahead(&s, "script.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); - s.run("script.run", &p).unwrap(); + let read = read_ahead(&mut s, "script.run", &p).unwrap().run(&Control::stoppable()).unwrap(); + read_land(&mut s, read).unwrap(); assert_eq!(s.tools_taken, 2); assert!(s.map().layer_by_name("Joined").is_some()); @@ -30488,50 +31376,28 @@ fn a_scripts_tools_run_ahead_are_taken_by_its_run() { // before it is taken, the one after it runs here. let p = json!({"script": format!("{}\nedit.addField {{\"layer\": \"Points\", \"name\": \"n\", \"type\": \"long\"}}\n{}", tool(1000), tool(2000))}); let mut s = session(); - crate::cmd::reads::read_ahead(&s, "script.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); - s.run("script.run", &p).unwrap(); + let read = read_ahead(&mut s, "script.run", &p).unwrap().run(&Control::stoppable()).unwrap(); + read_land(&mut s, read).unwrap(); assert_eq!(s.tools_taken, 1); assert!(s.map().layer_by_name("Points").unwrap().dataset.fields.iter().any(|f| f.name == "n")); // A selection changed meanwhile: the tools after it run here, on it. let p = json!({"script": tool(1000)}); let mut s = session(); - crate::cmd::reads::read_ahead(&s, "script.run", &p).unwrap().run(&Control::stoppable()).unwrap().placed(&mut s); + let read = read_ahead(&mut s, "script.run", &p).unwrap().run(&Control::stoppable()).unwrap(); s.map_mut().layers[0].selection = vec![0]; - s.run("script.run", &p).unwrap(); + read_land(&mut s, read).unwrap(); assert_eq!(s.tools_taken, 0); // A script whose first line would stop the copy runs here, with no copy made. let p = json!({"script": format!("edit.addField {{\"layer\": \"Points\", \"name\": \"m\", \"type\": \"long\"}}\n{}", tool(1000))}); - assert!(crate::cmd::reads::read_ahead(&s, "script.run", &p).is_none()); - assert!(crate::cmd::reads::read_ahead(&s, "script.run", &json!({"script": "# nothing"})).is_none()); + assert!(!read_ahead(&mut s, "script.run", &p).unwrap().reads()); + assert!(!read_ahead(&mut s, "script.run", &json!({"script": "# nothing"})).unwrap().reads()); // Each command a line may run on the copy is one the session has. for id in crate::cmd::workflows::ON_A_COPY { assert!(s.registry.get(id).is_some(), "{id}"); } } -/// Runs a test that owns process-wide state in a fresh instance of the test executable. -pub(crate) fn isolate_process_test(name: &str) -> bool { - if std::env::var("RUSTGIS_ISOLATED_SCENARIO").as_deref() == Ok(name) { - return false; - } - let output = std::process::Command::new(std::env::current_exe().unwrap()) - .args(["--exact", name]) - .env("RUSTGIS_ISOLATED_SCENARIO", name) - .output() - .unwrap(); - let stdout = String::from_utf8_lossy(&output.stdout); - let mut summaries = stdout.lines().filter(|line| line.starts_with("test result:")); - let ran_one = summaries.next().is_some_and(|line| line.starts_with("test result: ok. 1 passed; 0 failed; 0 ignored;")) - && summaries.next().is_none(); - assert!( - output.status.success() && ran_one, - "isolated process scenario failed or ran no test:\n{stdout}\n{}", - String::from_utf8_lossy(&output.stderr) - ); - true -} - #[test] fn topology_validate_says_when_a_polar_check_is_incomplete() { // A line winding round the North Pole about 19,000 times: its dangle check measures it coarsely, diff --git a/crates/engine/src/tests/credential_output.rs b/crates/engine/src/tests/credential_output.rs index da66e7789e..023fee5b65 100644 --- a/crates/engine/src/tests/credential_output.rs +++ b/crates/engine/src/tests/credential_output.rs @@ -580,7 +580,7 @@ fn generated_notebook_errors_are_cleaned_before_the_output_is_retained() { let mut s = marked(); let source = format!("layer.addUrl {{\"url\":\"https://h.invalid/?token={SECRET}\",\"cellSize\":1}}"); s.project.notebooks[0].cells[0].source = source.clone(); - s.trusted_notebooks.insert("credentials".into()); + s.trusted_notebooks.insert("credentials".into(), std::sync::Arc::new(s.item_content("notebook", "credentials").unwrap())); let _ = s.run("notebook.run", &json!({"notebook":0})); assert_eq!(s.project.notebooks[0].cells[0].source, source); assert!(!s.project.notebooks[0].cells[0].output.as_ref().unwrap().contains(SECRET)); @@ -607,7 +607,7 @@ fn generated_notebook_errors_are_cleaned_before_the_output_is_retained() { fn peers_bypass_saved_restore_and_retention_requires_the_current_exact_slot() { let mut s = marked(); let saved = s.project.saved_text(None).unwrap().0; - s.trusted_tasks.insert("credentials".into()); + s.trusted_tasks.insert("credentials".into(), std::sync::Arc::new(s.item_content("task", "credentials").unwrap())); let generation = s.opened; s.open_peer_text(Peer::Embedder, &saved).unwrap(); assert!(s.opened != generation && s.trusted_tasks.is_empty()); @@ -712,12 +712,18 @@ fn short_basic_passwords_are_scrubbed_only_from_output_while_active() { } use crate::render::rustgis_tiles::logins; + let owner = Session::new(Project::default()); let mut other = Session::new(Project::default()); let value = logins::basic("synthetic-user", "a"); let origin = "https://short-basic.invalid"; - let binding = - logins::activate(origin, "synthetic-short-basic-output-owner", logins::Method::Header("Authorization".into()), &value, false) - .unwrap(); + let binding = logins::activate( + origin, + &format!("{}/short-basic", owner.web_logins.session_id()), + logins::Method::Header("Authorization".into()), + &value, + false, + ) + .unwrap(); let payload = value.strip_prefix("Basic ").unwrap(); assert_eq!(logins::scrub(&format!("echo {value} {payload} a")), "echo [redacted] [redacted] [redacted]"); other.notify("alpha", false); diff --git a/crates/engine/src/tests/permission_policy.rs b/crates/engine/src/tests/permission_policy.rs new file mode 100644 index 0000000000..d934f8f19c --- /dev/null +++ b/crates/engine/src/tests/permission_policy.rs @@ -0,0 +1,1782 @@ +//! Public dispatch, frozen consent and owned late work share the engine's permission outcome. +use super::*; +use crate::policy::{Call, Effects, Outcome, Pending, Revocation, Scope}; +use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, +}; + +struct Live(AtomicBool); +impl Revocation for Live { + fn allows(&self, _: Effects) -> bool { + self.0.load(Ordering::SeqCst) + } +} +fn assistant(id: u64) -> (Scope, Arc) { + let live = Arc::new(Live(AtomicBool::new(true))); + (Scope::assistant(id, live.clone()), live) +} +fn pending(s: &Session, scope: &Scope, call: &Call) -> Pending { + match s.review(scope, call) { + Outcome::NeedsApproval(p) => p, + Outcome::Refused(e) => panic!("refused instead of a review: {e}"), + Outcome::Allowed => panic!("the external operation needs approval"), + } +} + +/// Training chooses its raster and polygons before a model exists; approval must use that branch. +#[test] +fn permission_policy_training_classification_binds_its_actual_inputs() { + let mut s = world(); + s.run("layer.addDataBytes", &json!({"fileName":"scene.tif","data":two_class_b64()})).unwrap(); + assert!(s.project.dl_models.is_empty()); + let (scope, _) = assistant(14610); + let call = Call::script(&format!( + "dl.classify {}\nmap.addTiles {}", + json!({"layer":"scene","training":"Countries","classField":"name","model":"New classification","epochs":1}), + json!({"url":"https://classification-review.invalid/{z}/{x}/{y}.png"}) + )); + let approved = pending(&s, &scope, &call); + s.run("layer.remove", &json!({"layer":"Countries"})).unwrap(); + let error = s.run_permitted(scope, approved.allow(), &call).unwrap_err(); + assert!(!error.is_empty()); + assert!(s.project.dl_models.is_empty(), "a changed training input is refused before fitting"); +} + +/// The save executor's trimmed names cannot turn an imported model into user-authored content. +#[test] +fn permission_policy_model_save_uses_the_executors_normalized_names() { + let mut s = world(); + let graph = json!({"nodes":[ + {"id":"input","kind":"input","layer":"Cities"}, + {"id":"buffer","kind":"tool","tool":"buffer","params":{"distance":10}} + ],"edges":[{"from":"input","fromPort":"out","to":"buffer","toPort":"input"}]}); + s.run("model.new", &json!({"name":"Imported","graph":graph,"imported":true})).unwrap(); + assert!(!s.item_trusted("model", "Imported")); + let result = s.run("model.save", &json!({"name":" Imported ","graph":graph,"rename":"Renamed"})).unwrap(); + assert_eq!(result["model"], "Renamed"); + assert!(!s.item_trusted("model", "Renamed"), "normalizing an imported name cannot confer trust"); + let result = s.run("model.save", &json!({"name":" Fresh ","graph":graph,"rename":" Normalized "})).unwrap(); + assert_eq!(result["model"], "Normalized"); + assert!(s.item_trusted("model", "Normalized"), "fresh user content records the actual saved name"); +} + +/// Equal display redactions do not confer consent for different raw addresses or defaults. +#[test] +fn permission_policy_consent_binds_raw_params_and_default_targets() { + let mut s = world(); + #[cfg(not(target_arch = "wasm32"))] + { + s.leave_downloads = true; + } + let (scope, _) = assistant(1); + let a = Call::command("layer.addUrl", json!({"url":"https://example.invalid/data.geojson?token=raw-one"})).with_request("r"); + let b = Call::command("layer.addUrl", json!({"url":"https://example.invalid/data.geojson?token=raw-two"})).with_request("r"); + let p = pending(&s, &scope, &a); + assert!(!p.review().what.contains("raw-one")); + assert!(s.run_permitted(scope.clone(), p.allow(), &b).unwrap_err().contains("changed")); + assert!(s.take_url_requests().is_empty()); + let p = pending(&s, &scope, &a); + s.active_layer = s.map().layer_by_name("Cities").map(|l| l.id); + assert!(s.run_permitted(scope.clone(), p.allow(), &a).unwrap_err().contains("changed")); + let p = pending(&s, &scope, &a); + s.run_permitted(scope, p.allow(), &a).unwrap(); + assert_eq!(s.take_url_requests().len(), 1); + let (scope, _) = assistant(127); + let nested = Call::command( + "layer.addUrl", + json!({"url":"https://example.invalid/clean.csv", "token":"RGSYNTH_direct", + "note":format!("message {}", json!({"script":format!("layer.addUrl {}", json!({"password":"RGSYNTH_nested","url":"https://example.invalid/x?token=RGSYNTH_url"}))}))}), + ); + let shown = pending(&s, &scope, &nested); + assert!(shown.review().what.contains("clean.csv")); + assert!(!shown.review().what.contains("RGSYNTH_")); + let long = Call::command("layer.addUrl", json!({"url":"https://example.invalid/clean.csv","note":"safe".repeat(1500)})); + assert!(matches!(s.review(&scope, &long), Outcome::Refused(e) if e.contains("too long"))); + + // A single-layer owner falls back to the active layer for a non-scalar selector. + // Review diagram and manual-label operations against their actual effect in an asking script. + let mut s = world(); + for name in ["Diagram A", "Diagram B"] { + s.run( + "layer.addDataText", + &json!({"name":name,"fileName":"diagram.geojson","text":r#"{"type":"Feature","properties":{"n":1},"geometry":{"type":"Point","coordinates":[0,0]}}"#}), + ) + .unwrap(); + } + let (scope, _) = assistant(130); + for selector in [json!({"layer":["Diagram A"]}), json!({"layer":"Diagram A"}), json!({})] { + for name in ["Diagram A", "Diagram B"] { + s.run("layer.diagram", &json!({"layer":name,"kind":"pie","fields":["n"],"size":8})).unwrap(); + let feature = s.map().layer_by_name(name).unwrap().dataset.features.first().unwrap().id; + s.run("labels.move", &json!({"layer":name,"feature":feature,"to":[1,2]})).unwrap(); + } + s.run("layer.select", &json!({"layer":"Diagram B"})).unwrap(); + let expected = if selector["layer"].is_string() { "Diagram A" } else { "Diagram B" }; + let other = if expected == "Diagram A" { "Diagram B" } else { "Diagram A" }; + let mut set = selector.clone(); + set["kind"] = json!("bar"); + set["fields"] = json!(["n"]); + set["size"] = json!(12); + let mut moved = selector.clone(); + moved["feature"] = json!(s.map().layer_by_name(expected).unwrap().dataset.features.first().unwrap().id); + moved["to"] = json!([3, 4]); + let call = Call::script(&format!( + "layer.diagram {set}\nlayer.diagramInfo {selector}\nlayer.clearDiagram {selector}\nlabels.move {moved}\nlabels.resetMoved {selector}\nmap.addTiles {{\"url\":\"https://diagram.invalid/{{z}}/{{x}}/{{y}}.png\"}}" + )); + let p = pending(&s, &scope, &call); + let details = p.review().details.clone(); + let defaults = p.review().defaults.clone(); + s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + assert!(s.map().layer_by_name(expected).unwrap().diagram.is_none(), "the executor clears its actual target"); + assert!(s.map().layer_by_name(other).unwrap().diagram.is_some(), "the other layer retains its diagram"); + assert!(s.map().layer_by_name(expected).unwrap().labels.default.moved.is_empty(), "the label owner clears its actual target"); + assert_eq!(s.map().layer_by_name(other).unwrap().labels.default.moved.len(), 1, "the other layer keeps its saved label move"); + let commands = ["layer.diagram", "layer.diagramInfo", "layer.clearDiagram", "labels.move", "labels.resetMoved"]; + if selector["layer"].is_string() { + for id in commands { + let detail = details.iter().find(|d| d.operation == id).unwrap(); + assert_eq!( + detail.targets, + [format!("{} / {expected}", s.map().name)], + "single-layer review target differs from execution: {id}" + ); + } + } else { + // Defaults are named once in Starting selection, not repeated as explicit targets. + assert_eq!(defaults, Some((Some(expected.to_string()), vec![expected.to_string()]))); + for detail in details.iter().filter(|d| commands.contains(&d.operation.as_str())) { + assert_eq!(detail.targets, [format!("{} / {expected}", s.map().name)], "no contradictory target may appear for a default"); + } + } + } + // A scalar name created or renamed by an earlier step remains a bound in-run target. + for (first, params, name, named) in [ + ( + "layer.addDataText", + json!({"name":"Fresh created","fileName":"fresh.geojson","text":r#"{"type":"Feature","properties":{"n":1},"geometry":{"type":"Point","coordinates":[0,0]}}"#}), + "Fresh created", + true, + ), + ("layer.rename", json!({"layer":"Diagram A","name":"Fresh renamed"}), "Fresh renamed", true), + ( + "layer.addDataText", + json!({"name":"Fresh default","fileName":"fresh.geojson","text":r#"{"type":"Feature","properties":{"n":1},"geometry":{"type":"Point","coordinates":[0,0]}}"#}), + "Fresh default", + false, + ), + ] { + let selector = if named { json!({"layer":name}) } else { json!({}) }; + let mut diagram = selector.clone(); + diagram["kind"] = json!("pie"); + diagram["fields"] = json!(["n"]); + diagram["size"] = json!(8); + let mut moved = selector.clone(); + moved["feature"] = json!(0); + moved["to"] = Value::Null; + let call = Call::script(&format!( + "{first} {params}\nlayer.diagram {diagram}\nlayer.diagramInfo {selector}\nlayer.clearDiagram {selector}\nlabels.move {moved}\nlabels.resetMoved {selector}\nmap.addTiles {{\"url\":\"https://diagram.invalid/{{z}}/{{x}}/{{y}}.png\"}}" + )); + let p = pending(&s, &scope, &call); + s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + assert!(s.map().layer_by_name(name).unwrap().diagram.is_none(), "the in-run target is created, styled and cleared"); + } + let unknown = + Call::script("layer.diagramInfo {\"layer\":\"Never made\"}\nmap.addTiles {\"url\":\"https://diagram.invalid/{z}/{x}/{y}.png\"}"); + assert!(matches!(s.review(&scope, &unknown), Outcome::Refused(why) if why.contains("no earlier output"))); +} + +/// A displayed task is frozen by content, including its project-only edits; undo restores trust. +#[test] +fn permission_policy_task_content_and_trust_follow_undo() { + let mut s = world(); + #[cfg(not(target_arch = "wasm32"))] + { + s.leave_downloads = true; + } + s.run("task.new", &json!({"name":"Reviewed", "steps":[{"title":"data", "command":"layer.addUrl", "params":{"url":"https://example.invalid/data.geojson"}}]})).unwrap(); + assert!(s.item_trusted("task", "Reviewed")); + let (scope, _) = assistant(2); + let call = Call::command("task.run", json!({"task":"Reviewed"})); + let p = pending(&s, &scope, &call); + s.run_scoped(scope.clone(), &Call::command("task.editStep", json!({"task":"Reviewed", "step":0, "title":"agent changed"}))).unwrap(); + assert!(!s.item_trusted("task", "Reviewed")); + assert!(s.run_permitted(scope.clone(), p.allow(), &call).is_err()); + s.run("edit.undo", &Value::Null).unwrap(); + assert!(s.item_trusted("task", "Reviewed")); + let p = pending(&s, &scope, &call); + s.run_permitted(scope, p.allow(), &call).unwrap(); + assert_eq!(s.take_url_requests().len(), 1); + s.run("edit.undo", &Value::Null).unwrap(); + assert!(!s.item_trusted("task", "Reviewed"), "undo of creation removes its content record"); +} + +/// A drain counts revoked requests once while preserving a current owner's queued request. +#[test] +fn permission_policy_revoked_url_requests_report_one_counted_notice() { + let mut s = world(); + #[cfg(not(target_arch = "wasm32"))] + { + s.leave_downloads = true; + } + let (revoked, live) = assistant(3401); + let (current, _) = assistant(3402); + for (scope, file) in [(&revoked, "revoked-first"), (¤t, "current"), (&revoked, "revoked-second")] { + let call = Call::command("layer.addUrl", json!({"url":format!("https://example.invalid/{file}.geojson")})); + let p = pending(&s, scope, &call); + s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + } + live.0.store(false, Ordering::SeqCst); + let mark = s.notice_mark(); + let requests = s.take_url_requests(); + assert_eq!(requests.len(), 1, "only the current owner's request survives"); + assert_eq!(requests[0].params()["url"], "https://example.invalid/current.geojson"); + let notices = s.notices_since(mark); + assert_eq!(notices.len(), 1, "one notice for both revoked requests: {notices:?}"); + assert!(notices[0].error); + assert_eq!(notices[0].text, "2 deferred URL requests were revoked before they started"); + let id = s.with_scope(current.clone(), |s| s.download_started_for(&requests[0])).unwrap().unwrap(); + assert!(s.download_continues(id).is_ok(), "the survivor retains its original owner"); + assert!(s.queued_authority().is_empty(), "revoked keys were retired and the survivor was claimed"); + let mark = s.notice_mark(); + assert!(s.take_url_requests().is_empty()); + assert!(s.notices_since(mark).is_empty(), "a repeated empty drain is quiet"); + + let call = Call::command("layer.addUrl", json!({"url":"https://example.invalid/clean.geojson"})); + let p = pending(&s, ¤t, &call); + s.run_permitted(current, p.allow(), &call).unwrap(); + let mark = s.notice_mark(); + assert_eq!(s.take_url_requests().len(), 1); + assert!(s.notices_since(mark).is_empty(), "a clean drain is quiet"); +} + +/// A request has one opaque claim, refuses a foreign completion, and revokes before sending. +#[test] +fn permission_policy_deferred_url_claim_keeps_owner_and_generation() { + let mut s = world(); + #[cfg(not(target_arch = "wasm32"))] + { + s.leave_downloads = true; + } + let (scope, live) = assistant(3); + let (foreign, _) = assistant(4); + let url = "https://example.invalid/data.geojson?token=RGSYNTH_pending"; + let call = Call::command("layer.addUrl", json!({"url":url})); + let p = pending(&s, &scope, &call); + let reported = s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + let mark = s.notice_mark(); + assert!(s.with_scope(foreign.clone(), |s| s.take_url_requests()).unwrap().is_empty()); + assert!(s.notices_since(mark).is_empty(), "a foreign drain cannot retire the owner's queued request"); + let q = s.take_url_requests().pop().unwrap(); + assert_eq!(q.params()["url"], url, "opaque transport parameters retain the exact private address"); + assert!(!reported.to_string().contains("RGSYNTH_pending"), "the public pending response must omit credentials"); + let logged = s.log.entries().last().unwrap(); + assert_eq!(logged.command, "layer.addUrl"); + assert!(!serde_json::to_string(logged).unwrap().contains("RGSYNTH_pending"), "the session log must omit credentials"); + let id = s.download_started_for(&q).unwrap(); + assert!(s.download_started_for(&q).is_err()); + let still = s.download_still(id).unwrap(); + assert!(still()); + let error = s.run_scoped(foreign, &Call::command("layer.addDownload", json!({"id":id}))).unwrap_err(); + assert!(error.contains("another caller"), "{error}"); + assert!(s.download_continues(id).is_ok(), "foreign call does not consume the owner's result"); + live.0.store(false, Ordering::SeqCst); + assert!(!still()); + assert!(s.download_continues(id).is_err()); + assert!(!still()); + let (scope, _) = assistant(5); + let p = pending(&s, &scope, &call); + s.run_permitted(scope, p.allow(), &call).unwrap(); + let q = s.take_url_requests().pop().unwrap(); + let id = s.download_started_for(&q).unwrap(); + let still = s.download_still(id).unwrap(); + s.run("project.new", &json!({"name":"Replacement"})).unwrap(); + assert!(!still()); + assert!(s.download_continues(id).is_err()); +} + +/// Native tools check revocation on actual worker entry and again before applying a result. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_policy_native_tool_revocation_and_foreign_finish() { + let mut s = world(); + #[cfg(not(target_arch = "wasm32"))] + { + s.leave_downloads = true; + } + let (scope, live) = assistant(6); + let run = s + .with_scope(scope.clone(), |s| crate::cmd::gp::tool_start(s, "buffer", &json!({"input":"Cities", "distance":10}))) + .unwrap() + .unwrap(); + let id = run.id; + live.0.store(false, Ordering::SeqCst); + let before = s.map().layers.len(); + let done = run.run(&rustgis_analysis::control::Control::new()); + assert!(crate::cmd::gp::tool_complete(&mut s, id, done).is_err()); + assert_eq!(s.map().layers.len(), before); + let (scope, _) = assistant(7); + let run = s + .with_scope(scope.clone(), |s| crate::cmd::gp::tool_start(s, "buffer", &json!({"input":"Cities", "distance":10}))) + .unwrap() + .unwrap(); + let id = run.id; + let (foreign, _) = assistant(8); + assert!( + s.with_scope(foreign.clone(), |s| crate::cmd::gp::plugin_complete(s, id, Err("foreign result".into()))) + .unwrap() + .unwrap_err() + .contains("another caller") + ); + crate::cmd::gp::tool_complete(&mut s, id, run.run(&rustgis_analysis::control::Control::new())).unwrap(); + assert!(s.run_scoped(foreign, &Call::command("gp.finish", json!({"id":id}))).unwrap_err().contains("another caller")); + s.with_scope(scope, |s| s.run("gp.finish", &json!({"id":id}))).unwrap().unwrap(); + assert_eq!(s.map().layers.len(), before + 1); + assert!(s.finish_gp(id).is_err()); +} + +/// Presence is a registry invariant, not evidence that each classification is correct. +#[test] +fn permission_policy_metadata_presence_and_unreviewed_default() { + for spec in crate::cmd::all() { + assert!(!spec.effects.contains(crate::policy::Effect::Unreviewed), "{}", spec.id); + assert!(spec.roles.is_some(), "{}", spec.id); + } + let mut s = world(); + #[cfg(not(target_arch = "wasm32"))] + { + s.leave_downloads = true; + } + s.registry.commands.push(crate::CommandSpec::new("test.unreviewed", "Unreviewed", "Test", |s, _| { + s.map_mut().name = "Changed".into(); + Ok(Value::Null) + })); + let (scope, _) = assistant(9); + let call = Call::command("test.unreviewed", Value::Null); + assert!(matches!(s.review(&scope, &call), Outcome::Refused(_))); + assert!(s.run_scoped(scope, &call).is_err()); + assert_ne!(s.map().name, "Changed"); + s.run("test.unreviewed", &Value::Null).unwrap(); + let (scope, _) = assistant(13); + let before = s.map().name.clone(); + let huge = Call::script(&"layer.addData {\"path\":\"never read.gpkg\"}\n".repeat(10_000)); + assert!(matches!(s.review(&scope, &huge), Outcome::Refused(e) if e.contains("too large"))); + assert_eq!(s.map().name, before); +} + +/// Actual execution families retain additive reach and every plugin call asks separately. +#[test] +fn permission_policy_effects_and_real_plugin_approval() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::permission_policy::permission_policy_effects_and_real_plugin_approval") { + return; + } + use crate::policy::{Effect, command_effects}; + let mut s = world(); + for id in ["layer.addSql", "cube.reduce", "cube.trend"] { + let effects = command_effects(s.registry.get(id).unwrap(), &Value::Null); + assert!(effects.contains(Effect::Files) && effects.contains(Effect::Network), "{id}: {effects:?}"); + } + assert!(command_effects(s.registry.get("project.open").unwrap(), &Value::Null).contains(Effect::Replaces)); + assert!(command_effects(s.registry.get("notebook.run").unwrap(), &Value::Null).contains(Effect::Files)); + assert!(command_effects(s.registry.get("gp.run").unwrap(), &json!({"tool":"buffer"})).contains(Effect::Edits)); + let mut pkg = rustgis_plugins::Package::from_zip(&rustgis_plugins::testing::package("eng-policy-approval", &["edit"])).unwrap(); + pkg.manifest.tools.iter_mut().find(|t| t.id == "x").unwrap().name = "Permission selection".into(); + rustgis_plugins::load(pkg, "test").unwrap(); + let tool = "plugin:eng-policy-approval/x"; + let effects = command_effects(s.registry.get("gp.run").unwrap(), &json!({"tool":tool,"params":{"input":"Cities"}})); + assert!(effects.contains(Effect::Edits) && effects.contains(Effect::Plugin)); + let (scope, _) = assistant(14); + for name in [tool, "Permission selection"] { + let call = Call::tool(name, json!({"input":"Cities"})); + let p = pending(&s, &scope, &call); + assert!(!p.review().remember); + s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + assert_eq!(s.map().layer_by_name("Cities").unwrap().selection.len(), 2); + assert!(!pending(&s, &scope, &call).review().remember); + } + rustgis_plugins::unload("eng-policy-approval"); +} + +/// The newly upstreamed native classification route carries its owner through work and `ran`. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_policy_native_classification_keeps_scope_through_completion() { + use crate::cmd::workflows::{raster_land, raster_start}; + let mut s = world(); + s.run("layer.addDataBytes", &json!({"fileName":"scene.tif", "data":two_class_b64()})).unwrap(); + let params = json!({"layer":"scene", "classes":3}); + let (scope, live) = assistant(10); + let run = s.with_scope(scope, |s| raster_start(s, "raster.classify", ¶ms)).unwrap().unwrap(); + live.0.store(false, Ordering::SeqCst); + assert!(run.run(&rustgis_analysis::control::Control::new()).err().unwrap().contains("revoked")); + let (scope, _) = assistant(11); + let run = s.with_scope(scope.clone(), |s| raster_start(s, "raster.classify", ¶ms)).unwrap().unwrap(); + let id = run.id; + let made = run.run(&rustgis_analysis::control::Control::new()).unwrap(); + raster_land(&mut s, id, made).unwrap(); + let (foreign, _) = assistant(12); + let finish = Call::command("raster.classify", json!({"ran":id})); + assert!(s.run_scoped(foreign, &finish).unwrap_err().contains("another caller")); + s.run_scoped(scope.clone(), &finish).unwrap(); + assert!(s.run_scoped(scope, &finish).is_err()); +} + +/// Background preparers reject non-object params before they can create a raster completion. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_policy_native_raster_rejects_non_object_params_before_queuing() { + use crate::cmd::workflows::{raster_start, runs_in_background}; + let mut s = world(); + s.run("layer.addDataBytes", &json!({"fileName":"scene.tif", "data":two_class_b64()})).unwrap(); + let source = s.map().layer_by_name("scene").unwrap().id; + let commands = s.registry.commands.iter().filter(|c| runs_in_background(c.id).is_some()).map(|c| c.id).collect::>(); + assert!(!commands.is_empty()); + for command in commands { + s.run("layer.select", &json!({"layer":source})).unwrap(); + for params in [json!(7), json!([]), json!("defaults"), json!(false), Value::Null] { + assert!(raster_start(&mut s, command, ¶ms).is_err(), "{command}: {params}"); + assert!(s.raster_runs.is_empty(), "{command}: a rejected request queued no worker"); + } + } +} + +/// The new export worker cannot start after revocation or land in a replacement project; +/// native snapshots still report after the person switches maps. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_policy_export_worker_keeps_its_initiating_owner() { + use crate::cmd::exports::{export_land, export_start}; + use crate::policy::AgentAccess; + use rustgis_analysis::control::Control; + let mut s = world(); + let dir = std::env::temp_dir().join(format!("rustgis-export-owner-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = |name: &str| dir.join(name); + let params = |name: &str| json!({"layer":"Cities", "path":path(name)}); + let (assistant, _) = assistant(129); + assert!(s.with_scope(assistant, |s| export_start(s, "share.exportLayer", ¶ms("unapproved.geojson"))).unwrap().is_err()); + let live = Arc::new(Live(AtomicBool::new(true))); + let scope = Scope::agent(AgentAccess::Allow, false, "export-owner", None, live.clone()); + let run = s.with_scope(scope.clone(), |s| export_start(s, "share.exportLayer", ¶ms("revoked.geojson"))).unwrap().unwrap(); + live.0.store(false, Ordering::SeqCst); + assert!(std::thread::spawn(move || run.run(&Control::new())).join().unwrap().is_err()); + assert!(!path("revoked.geojson").exists()); + live.0.store(true, Ordering::SeqCst); + let run = s.with_scope(scope, |s| export_start(s, "share.exportLayer", ¶ms("completed.geojson"))).unwrap().unwrap(); + let started = run.started(); + let wrote = run.run(&Control::new()); + assert!(wrote.is_ok(), "the worker committed its file: {:?}", wrote.as_ref().err()); + s.run("project.new", &Value::Null).unwrap(); + let replacement = serde_json::to_value(&s.project).unwrap(); + let mark = s.notice_mark(); + let error = export_land(&mut s, started, wrote).unwrap_err(); + assert!(error.contains("replaced project")); + assert!(error.contains(&path("completed.geojson").display().to_string()), "the completed file is reported: {error}"); + let errors: Vec<_> = s.notices_since(mark).iter().filter(|n| n.error).map(|n| n.text.as_str()).collect(); + assert_eq!(errors, [error.as_str()], "the completed file and delivery refusal are reported once"); + assert_eq!(serde_json::to_value(&s.project).unwrap(), replacement, "delivery leaves the replacement project alone"); + assert!(path("completed.geojson").exists(), "a completed write is not undone"); + // Drawn exports can also carry a failed write as an owned result: only a committed file + // may be reported when delivery is refused, and neither case may run its finalizer. + for committed in [false, true] { + let output = if committed { path("completed.png") } else { path("missing").join("not-written.png") }; + let run = export_start(&mut s, "share.exportMap", &json!({"path":output,"width":64,"height":48})).unwrap(); + let started = run.started(); + let wrote = run.run(&Control::new()); + assert!(wrote.is_ok(), "drawing delivers its write outcome: {:?}", wrote.as_ref().err()); + assert_eq!(output.exists(), committed); + s.run("project.new", &Value::Null).unwrap(); + let replacement = serde_json::to_value(&s.project).unwrap(); + let mark = s.notice_mark(); + let error = export_land(&mut s, started, wrote).unwrap_err(); + assert!(error.contains("replaced project"), "{error}"); + assert_eq!(error.contains("Exported "), committed, "{error}"); + if committed { + assert!(error.contains(&output.display().to_string()), "the committed drawing is reported: {error}"); + } + let notices: Vec<_> = s.notices_since(mark).iter().map(|notice| (notice.error, notice.text.as_str())).collect(); + assert_eq!(notices, [(true, error.as_str())], "only the refusal is delivered; its finalizer stays blocked"); + assert_eq!(serde_json::to_value(&s.project).unwrap(), replacement); + } + s.run("project.sample", &Value::Null).unwrap(); + let run = export_start(&mut s, "share.exportLayer", ¶ms("native.geojson")).unwrap(); + let started = run.started(); + s.run("map.new", &json!({"name":"Other"})).unwrap(); + let wrote = run.run(&Control::new()); + let count = s.map().layers.len(); + assert_eq!(export_land(&mut s, started, wrote).unwrap()["path"], json!(path("native.geojson"))); + assert_eq!(s.map().layers.len(), count); + // A completion cannot borrow another invocation's command/log envelope. + s.run("map.activate", &json!({"name":s.project.maps[0].name.clone()})).unwrap(); + let a = export_start(&mut s, "share.exportLayer", ¶ms("owner-a.geojson")).unwrap(); + let b = export_start(&mut s, "share.exportLayer", ¶ms("owner-b.geojson")).unwrap(); + assert!(export_land(&mut s, b.started(), a.run(&Control::new())).unwrap_err().contains("another run")); + std::fs::remove_dir_all(dir).unwrap(); +} + +/// The delayed UI token inherits a scoped caller and cannot escape a handler's transaction. +#[test] +fn permission_policy_prepared_plugin_keeps_scope_and_pre_handler_boundary() { + use crate::policy::AgentAccess; + let mut s = world(); + plugin("eng-prepared-owner", &["edit"]); + let id = "plugin:eng-prepared-owner/f"; + let params = json!({"op":"map.rename", "params":{"name":"Queued"}}); + let before = s.map().name.clone(); + let live = Arc::new(Live(AtomicBool::new(true))); + let scope = Scope::agent(AgentAccess::Allow, false, "prepared-owner", None, live.clone()); + let prepared = s.with_scope(scope, |s| s.prepare_command(id, ¶ms)).unwrap().unwrap(); + live.0.store(false, Ordering::SeqCst); + assert!(s.run_prepared(prepared).unwrap_err().contains("revoked")); + assert!(!s.leave_downloads, "a refused prepared command restores the caller's download mode"); + assert_eq!(s.map().name, before); + let mut preparation_error = None; + let transaction = s.guarded("Transaction", true, |s| { + preparation_error = s.prepare_command(id, ¶ms).err(); + Err("a later transaction failure".into()) + }); + assert_eq!(transaction.unwrap_err(), "a later transaction failure"); + assert!(preparation_error.as_deref().is_some_and(|e| e.contains("before handler")), "{preparation_error:?}"); + assert_eq!(s.map().name, before); + rustgis_plugins::unload("eng-prepared-owner"); +} + +/// Public creation returns a numeric notebook handle; trust follows its exact content and undo. +#[test] +fn permission_policy_notebook_creator_edit_and_import_provenance() { + let mut s = world(); + let made = s.run("notebook.new", &json!({"name":"Local"})).unwrap(); + assert!(made["notebook"].is_number()); + assert!(s.item_trusted("notebook", "Local")); + s.run("notebook.editCell", &json!({"notebook":"Local","cell":1,"source":"map.rename {\"name\":\"Notebook map\"}"})).unwrap(); + assert!(s.item_trusted("notebook", "Local")); + let (scope, _) = assistant(101); + let call = Call::command("notebook.run", json!({"notebook":"Local","cell":1})); + let p = pending(&s, &scope, &call); + s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + assert_eq!(s.map().name, "Notebook map"); + plugin("policy-notebook", &["edit", "files"]); + forward( + &mut s, + "policy-notebook", + "notebook.editCell", + json!({"notebook":"Local","cell":1,"source":"map.rename {\"name\":\"plugin edit\"}"}), + ) + .unwrap(); + assert!(!s.item_trusted("notebook", "Local")); + s.run("edit.undo", &Value::Null).unwrap(); + assert!(s.item_trusted("notebook", "Local")); + let text = json!({"name":"Imported","cells":[{"kind":"code","source":"map.zoomFull {}"}]}).to_string(); + s.run("notebook.open", &json!({"text":text})).unwrap(); + s.run("notebook.editCell", &json!({"notebook":"Imported","cell":0,"source":"map.zoomIn {}"})).unwrap(); + assert!(!s.item_trusted("notebook", "Imported")); + rustgis_plugins::unload("policy-notebook"); +} + +/// Plans accept their own new outputs/renames while refusing unknown producers and stale cards. +#[test] +fn permission_policy_ordered_outputs_rename_and_default_cursor() { + let mut s = world(); + let (scope, _) = assistant(102); + s.run( + "task.new", + &json!({"name":"Make then use","steps":[ + {"title":"Step","command":"gp.run","params":{"tool":"buffer","params":{"input":"Cities","distance":10,"output":"Fresh"}}}, + {"title":"Step","command":"layer.rename","params":{"layer":"Fresh","name":"Renamed"}}, + {"title":"Step","command":"layer.visibility","params":{"visible":false}} + ]}), + ) + .unwrap(); + let call = Call::command("task.run", json!({"task":"Make then use"})); + let p = pending(&s, &scope, &call); + s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + assert!(!s.map().layer_by_name("Renamed").unwrap().visible); + s.run("edit.undo", &Value::Null).unwrap(); + assert!(s.map().layer_by_name("Renamed").is_none()); + s.run( + "layer.addDataText", + &json!({"name":"Numeric","fileName":"Numeric.geojson","text":json!({"type":"FeatureCollection","features":[ + {"type":"Feature","properties":{"v":2},"geometry":{"type":"Point","coordinates":[0,0]}}, + {"type":"Feature","properties":{"v":4},"geometry":{"type":"Point","coordinates":[1,1]}} + ]}).to_string()}), + ) + .unwrap(); + let origin = s.map().name.clone(); + // gp.run retains its existing nonempty-map enabled check; the input below is still + // the produced project-global table, with no same-named layer in this destination. + s.run("map.duplicate", &json!({"name":"Table destination"})).unwrap(); + s.run("map.activate", &json!({"name":origin})).unwrap(); + s.run("task.new", &json!({"name":"Produce table then use","steps":[ + {"title":"Statistics","command":"gp.run","params":{"tool":"summaryStatistics","params":{"input":"Numeric","field":"v","output":"Numeric_Statistics"}}}, + {"title":"Switch","command":"map.activate","params":{"name":"Table destination"}}, + {"title":"Use global table","command":"gp.run","params":{"tool":"summaryStatistics","params":{"input":"Numeric_Statistics","field":"FREQUENCY","output":"Numeric_Statistics_Statistics"}}} + ]})).unwrap(); + let table_call = Call::command("task.run", json!({"task":"Produce table then use"})); + let approved = pending(&s, &scope, &table_call); + s.run_permitted(scope.clone(), approved.allow(), &table_call).unwrap(); + assert!(s.project.tables.iter().any(|t| t.dataset.name == "Numeric_Statistics_Statistics")); + assert!(s.map().layer_by_name("Numeric_Statistics").is_none()); + s.run("edit.undo", &Value::Null).unwrap(); + assert_eq!(s.map().name, origin); + assert!(!s.project.tables.iter().any(|t| t.dataset.name == "Numeric_Statistics")); + let missing = Call::script("layer.rename {\"layer\":\"Absent\",\"name\":\"No\"}\nlayer.addData {\"path\":\"never.gpkg\"}"); + assert!(matches!(s.review(&scope, &missing), Outcome::Refused(_))); + s.run("layer.select", &json!({"layer":"Cities"})).unwrap(); + s.run( + "task.new", + &json!({"name":"Rename existing","steps":[ + {"title":"Step","command":"layer.rename","params":{"layer":"Cities","name":"Same id"}}, + {"title":"Step","command":"layer.visibility","params":{"visible":false}} + ]}), + ) + .unwrap(); + let call = Call::command("task.run", json!({"task":"Rename existing"})); + let p = pending(&s, &scope, &call); + s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + assert!(!s.map().layer_by_name("Same id").unwrap().visible); + s.run("edit.undo", &Value::Null).unwrap(); + s.run( + "task.new", + &json!({"name":"Remove default","steps":[ + {"title":"Step","command":"layer.remove","params":{"layer":"Cities"}}, + {"title":"Step","command":"layer.visibility","params":{"visible":false}} + ]}), + ) + .unwrap(); + let call = Call::command("task.run", json!({"task":"Remove default"})); + let p = pending(&s, &scope, &call); + assert!(s.run_permitted(scope, p.allow(), &call).is_err()); + assert!(s.map().layer_by_name("Cities").is_some(), "transaction rolls back the removal"); + // Layer ids are per-map: activation resolves later defaults on the destination, while a + // changed destination before approval use never inherits a same-numbered source layer. + let original = s.map().name.clone(); + let original_map = s.map().instance_id(); + let shared_id = s.map().layers.first().unwrap().id; + s.run("map.duplicate", &json!({"name":"Destination"})).unwrap(); + let destination = s.map().instance_id(); + assert_ne!(original_map, destination); + assert_eq!(s.map().layers.first().unwrap().id, shared_id); + s.run("map.activate", &json!({"name":original})).unwrap(); + s.run( + "task.new", + &json!({"name":"Activate then edit","steps":[ + {"title":"Step","command":"map.activate","params":{"name":"Destination"}}, + {"title":"Step","command":"layer.visibility","params":{"visible":false}} + ]}), + ) + .unwrap(); + let call = Call::command("task.run", json!({"task":"Activate then edit"})); + let (scope, _) = assistant(122); + let p = pending(&s, &scope, &call); + s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + assert_eq!(s.map().instance_id(), destination); + assert!(!s.map().layer(shared_id).unwrap().visible); + assert!(s.project.maps.iter().find(|m| m.instance_id() == original_map).unwrap().layer(shared_id).unwrap().visible); + s.run("edit.undo", &Value::Null).unwrap(); + assert_eq!(s.map().instance_id(), original_map); + let p = pending(&s, &scope, &call); + let dest = s.project.maps.iter_mut().find(|m| m.instance_id() == destination).unwrap(); + dest.layer_mut(shared_id).unwrap().renew_instance_id(); + assert!(s.run_permitted(scope, p.allow(), &call).unwrap_err().contains("changed")); + assert_eq!(s.map().instance_id(), original_map); + // Renaming keeps default identities, but later named-map choices cannot use the old namespace. + // The executor makes an existing destination name unique; planning must not bind that old map. + for renamed in ["Approved renamed", "Destination"] { + let names = s.project.maps.iter().map(|m| m.name.clone()).collect::>(); + let call = Call::script(&format!( + "map.rename {}\nmap.activate {}\nshare.exportMap {{}}", + json!({"name":renamed}), + json!({"name":original}) + )); + let (scope, _) = assistant(128); + assert!(matches!(s.review(&scope, &call), Outcome::Refused(why) if why.contains("namespace"))); + assert_eq!(s.project.maps.iter().map(|m| m.name.clone()).collect::>(), names); + } + // Planning streamed reach must use the borrowed destination, not the initial in-memory map. + let mut source = rustgis_map::source::SourceLayer::default(); + source.path = Some("https://example.invalid/destination.fgb".into()); + let dest = s.project.maps.iter_mut().find(|m| m.instance_id() == destination).unwrap(); + let city = dest.layer_by_name("Cities").unwrap().id; + dest.layer_mut(city).unwrap().source = Some(source); + s.run( + "task.new", + &json!({"name":"Destination stream","steps":[ + {"title":"Activate","command":"map.activate","params":{"name":"Destination"}}, + {"title":"Buffer","command":"gp.run","params":{"tool":"buffer","params":{"input":"Cities","distance":10}}} + ]}), + ) + .unwrap(); + let (scope, live) = assistant(126); + let streamed = Call::command("task.run", json!({"task":"Destination stream"})); + let approved = pending(&s, &scope, &streamed); + assert!(approved.review().steps.iter().any(|line| line.starts_with("buffer ")), "the destination's streamed tool is an external step"); + live.0.store(false, Ordering::SeqCst); + assert!(s.run_permitted(scope, approved.allow(), &streamed).is_err()); + assert_eq!(s.map().instance_id(), original_map); +} + +/// External dataset/sheet names are frozen with their origin without opening it for review. +#[test] +fn permission_policy_external_selectors_and_dataset_precedence() { + let mut s = world(); + let (scope, _) = assistant(103); + for (id, origin) in [("layer.addData", "path"), ("layer.addDatabase", "database"), ("info.fileColumns", "path")] { + for name in ["absent dataset", ""] { + let call = Call::command(id, json!({origin:"never-opened.gpkg","layer":name})); + assert!(pending(&s, &scope, &call).review().why.contains("files")); + } + } + let ds = s.map().layer_by_name("Cities").unwrap().dataset.clone(); + let table = s.project.add_table(ds); + s.run("task.new", &json!({"name":"Colliding table","steps":[{"title":"Step","command":"gp.run","params":{"tool":"buffer","params":{"input":"Cities","distance":10}}}]})).unwrap(); + let call = Call::command("task.run", json!({"task":"Colliding table"})); + let p = pending(&s, &scope, &call); + s.project.tables.iter_mut().find(|t| t.id == table).unwrap().dataset.source = Some("changed unrelated table".into()); + // Replanning conservatively notices project changes, but the tool's actual target is the layer. + let p2 = pending(&s, &scope, &call); + assert_eq!(p.review().what, p2.review().what); + s.run_permitted(scope, p2.allow(), &call).unwrap(); + assert!(s.map().layers.len() > 3); +} + +/// A same-public-URL account replacement invalidates a held card and its owned send predicate. +#[test] +fn permission_policy_exact_runtime_account_rotation() { + // Account stores are process-wide: isolate mutation from every parallel plan reader. + #[cfg(not(target_arch = "wasm32"))] + if std::env::var_os("RUSTGIS_POLICY_ACCOUNT_CHILD").is_none() { + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "tests::permission_policy::permission_policy_exact_runtime_account_rotation", "--nocapture"]) + .env("RUSTGIS_POLICY_ACCOUNT_CHILD", "1") + .output() + .unwrap(); + assert!(output.status.success(), "{}{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr)); + assert!(String::from_utf8_lossy(&output.stdout).contains("1 passed")); + return; + } + + use crate::render::rustgis_tiles::earthdata; + let mut s = world(); + #[cfg(not(target_arch = "wasm32"))] + { + s.leave_downloads = true; + } + let (scope, _) = assistant(104); + earthdata::set_token(Some("synthetic-policy-first")).unwrap(); + let call = Call::command("layer.addUrl", json!({"url":"https://example.invalid/unchanged.geojson"})); + let p = pending(&s, &scope, &call); + earthdata::set_token(Some("synthetic-policy-second")).unwrap(); + assert!(s.run_permitted(scope.clone(), p.allow(), &call).unwrap_err().contains("changed")); + let p = pending(&s, &scope, &call); + s.run_permitted(scope, p.allow(), &call).unwrap(); + let request = s.take_url_requests().pop().unwrap(); + let id = s.download_started_for(&request).unwrap(); + let still = s.download_still(id).unwrap(); + assert!(still()); + earthdata::set_token(Some("synthetic-policy-third")).unwrap(); + assert!(!still()); + assert!(s.download_continues(id).is_err()); + earthdata::set_token(None).unwrap(); + use crate::render::rustgis_tiles::arcgis_auth; + let url = "https://policy-identity.invalid/server/rest/services/Data/FeatureServer/0"; + let root = arcgis_auth::add_token(url, "synthetic-policy-server-first").unwrap(); + let (scope, _) = assistant(125); + let call = Call::command("layer.addUrl", json!({"url":url})); + let p = pending(&s, &scope, &call); + arcgis_auth::add_token(url, "synthetic-policy-server-second").unwrap(); + assert!(s.run_permitted(scope, p.allow(), &call).unwrap_err().contains("changed")); + arcgis_auth::sign_out(Some(root.trim_end_matches('/'))); +} + +/// Foreign in-place completions cannot borrow/consume ownership; omitted options retain defaults. +#[test] +fn permission_policy_download_refinements_rollback_and_abandonment() { + let mut s = world(); + #[cfg(not(target_arch = "wasm32"))] + { + s.leave_downloads = true; + } + let (scope, _) = assistant(105); + let (foreign, _) = assistant(106); + let call = Call::command( + "layer.addUrl", + json!({"url":"https://example.invalid/data.csv","name":"Frozen","x":"east","y":"north","zoom":false}), + ); + let p = pending(&s, &scope, &call); + s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + let q = s.take_url_requests().pop().unwrap(); + let id = s.download_started_for(&q).unwrap(); + let parameters = q.params().clone(); + assert!(s.with_scope(foreign, |s| s.add_in_place_for(id, ¶meters)).unwrap().unwrap_err().contains("another caller")); + assert!(s.download_still(id).unwrap()()); + assert!(s.download_landed(id, b"east,north\n1,2\n".to_vec())); + s.complete_download(id, &json!({"id":id})).unwrap(); + assert_eq!(s.map().layer_by_name("Frozen").unwrap().dataset.features.len(), 1); + assert!(s.complete_download(id, &json!({"id":id})).is_err()); + assert!(s.run("layer.addUrl", &json!({"url":"https://example.invalid/project.json","geolibreProject":true})).is_err()); + for i in 0..32 { + let failed = format!("layer.addUrl {{\"url\":\"https://example.invalid/rollback-{i}.csv\"}}\nmissing.command {{}}"); + assert!(s.run("script.run", &json!({"script":failed})).is_err()); + assert!(s.take_url_requests().is_empty()); + } + s.request_url(&json!({"url":"https://example.invalid/earlier.csv"})).unwrap(); + let earlier = s.take_url_requests().pop().unwrap(); + let mut taken = None; + assert!( + s.guarded("Retained failed request", false, |s| { + s.request_url(&json!({"url":"https://example.invalid/failed-retained.csv"}))?; + taken = s.take_url_requests().pop(); + Err("fail after the owner took the request".into()) + }) + .is_err() + ); + assert!(s.download_started_for(&taken.unwrap()).is_err()); + let id = s.download_started_for(&earlier).unwrap(); + assert!(s.download_continues(id).is_ok()); + s.download_failed(id); + let mut held = Vec::new(); + for i in 0..crate::MAX_URL_REQUESTS { + s.request_url(&json!({"url":format!("https://example.invalid/held-{i}.csv")})).unwrap(); + held.extend(s.take_url_requests()); + } + assert!(s.request_url(&json!({"url":"https://example.invalid/over.csv"})).is_err()); + drop(held); + s.request_url(&json!({"url":"https://example.invalid/fresh.csv"})).unwrap(); + assert_eq!(s.take_url_requests().len(), 1); +} + +/// Async apply retains its initiating actor so only that agent can undo the new step. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_policy_async_actor_and_streamed_preparation() { + use crate::policy::AgentAccess; + let mut s = world(); + let live = Arc::new(Live(AtomicBool::new(true))); + let scope = Scope::agent(AgentAccess::Allow, true, "actor-run", Some("rightful".into()), live); + let run = s + .with_scope(scope.clone(), |s| crate::cmd::gp::tool_start(s, "buffer", &json!({"input":"Cities","distance":10}))) + .unwrap() + .unwrap(); + let id = run.id; + crate::cmd::gp::tool_complete(&mut s, id, run.run(&rustgis_analysis::control::Control::new())).unwrap(); + s.finish_gp(id).unwrap(); + assert_eq!(s.undo_actor(), Some(Some("rightful"))); + assert_eq!(s.actor, None); + let foreign = Scope::agent(AgentAccess::Allow, true, "foreign", Some("foreign".into()), Arc::new(Live(AtomicBool::new(true)))); + assert!(s.run_scoped(foreign, &Call::command("edit.undo", Value::Null)).is_err()); + s.run_scoped(scope, &Call::command("edit.undo", Value::Null)).unwrap(); + assert_eq!(s.actor, None); +} + +/// Hold the real streamed-input worker before its range send, then revoke its initiating scope. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_policy_streamed_tool_worker_refuses_before_remote_input_send() { + use std::sync::{Mutex, mpsc}; + struct Held { + armed: AtomicBool, + live: AtomicBool, + polls: std::sync::atomic::AtomicUsize, + entered: mpsc::SyncSender<()>, + released: Mutex>, + } + impl Revocation for Held { + fn allows(&self, _: Effects) -> bool { + if self.armed.load(Ordering::SeqCst) && self.polls.fetch_add(1, Ordering::SeqCst) == 1 { + self.entered.send(()).unwrap(); + self.released.lock().unwrap().recv().unwrap(); + } + self.live.load(Ordering::SeqCst) + } + } + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let url = format!("http://{}/input.fgb", listener.local_addr().unwrap()); + let mut s = world(); + let cities = s.map().layer_by_name("Cities").unwrap().id; + let mut source = rustgis_map::source::SourceLayer::default(); + source.path = Some(url); + s.map_mut().layer_mut(cities).unwrap().source = Some(source); + let (entered, waiting) = mpsc::sync_channel(1); + let (release, released) = mpsc::sync_channel(1); + let live = Arc::new(Held { + armed: AtomicBool::new(false), + live: AtomicBool::new(true), + polls: 0.into(), + entered, + released: Mutex::new(released), + }); + let scope = Scope::agent(crate::policy::AgentAccess::Allow, false, "streamed-worker", None, live.clone()); + let run = s.with_scope(scope, |s| crate::cmd::gp::tool_start(s, "buffer", &json!({"input":"Cities","distance":10}))).unwrap().unwrap(); + let id = run.id; + live.armed.store(true, Ordering::SeqCst); + let worker = std::thread::spawn(move || run.run(&rustgis_analysis::control::Control::new())); + let entered = waiting.recv_timeout(std::time::Duration::from_secs(3)); + live.live.store(false, Ordering::SeqCst); + let _ = release.send(()); + assert!(entered.is_ok(), "the actual input preparation must inherit its worker's owned predicate"); + assert!(matches!(listener.accept(), Err(e) if e.kind() == std::io::ErrorKind::WouldBlock)); + assert!(crate::cmd::gp::tool_complete(&mut s, id, worker.join().unwrap()).is_err()); +} + +/// The real rustgis-source thread must carry the initiating context through a held source read. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_policy_source_worker_checks_follow_on_send_and_landing() { + use rustgis_data::source::{Batch, FeatureSource, Request, SourceInfo}; + use std::{ + io::Write, + sync::{Mutex, mpsc}, + time::Duration, + }; + struct HeldSource { + inner: Box, + first: AtomicBool, + entered: mpsc::SyncSender<()>, + release: Mutex>, + url: String, + } + impl FeatureSource for HeldSource { + fn info(&self) -> &SourceInfo { + self.inner.info() + } + fn read(&self, req: &Request) -> rustgis_data::Result { + if self.first.swap(false, Ordering::SeqCst) { + assert_eq!(std::thread::current().name(), Some("rustgis-source")); + self.entered.send(()).unwrap(); + self.release.lock().unwrap().recv().unwrap(); + crate::render::rustgis_tiles::cache::download_blocking_within(&self.url, 64, Duration::from_secs(2)) + .map_err(rustgis_data::DataError::Format)?; + } + self.inner.read(req) + } + } + for revoke in [false, true] { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let url = format!("http://{}/follow-on", listener.local_addr().unwrap()); + let mut s = world(); + let bytes = Arc::new(rustgis_data::source::fgb::write(&s.map().layer_by_name("Cities").unwrap().dataset).unwrap()); + let inner = rustgis_data::source::open_shared("held.fgb", bytes.clone(), None).unwrap(); + let (entered, waiting) = mpsc::sync_channel(1); + let (release, released) = mpsc::sync_channel(1); + let mut source = rustgis_map::source::SourceLayer::from_bytes( + "held.fgb", + bytes, + None, + Box::new(HeldSource { inner, first: AtomicBool::new(true), entered, release: Mutex::new(released), url }), + ); + let (scope, live) = assistant(120 + u64::from(revoke)); + let call = Call::adapter("source export", Effects::NONE.with(crate::policy::Effect::Files), Value::Null); + let p = pending(&s, &scope, &call); + let completion = s.defer(scope, &call, Some(p.allow())).unwrap(); + s.finish_deferred(completion, |_| source.start(rustgis_geom::Extent::new(-180.0, -85.0, 180.0, 85.0), false)).unwrap(); + waiting.recv_timeout(Duration::from_secs(3)).unwrap(); + live.0.store(!revoke, Ordering::SeqCst); + release.send(()).unwrap(); + if !revoke { + let started = std::time::Instant::now(); + let mut peer = loop { + match listener.accept() { + Ok((peer, _)) => break peer, + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock && started.elapsed() < Duration::from_secs(3) => { + std::thread::yield_now() + } + other => panic!("the allowed actual worker must send: {other:?}"), + } + }; + peer.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok").unwrap(); + assert!(matches!(source.poll(true), rustgis_map::source::Polled::Done(_, _))); + } else { + assert!(matches!(source.poll(true), rustgis_map::source::Polled::Failed(_))); + assert!(matches!(listener.accept(),Err(e) if e.kind()==std::io::ErrorKind::WouldBlock)); + assert_eq!(source.window().unwrap().kept, 0); + } + assert!(rustgis_data::source::remote::current_still().is_none(), "synchronous reentry restores the caller"); + } +} + +/// A notebook cannot change its frozen code while running; model wires and cycle refusal use real commands. +#[test] +fn permission_policy_notebook_self_edit_model_outputs_and_intersections() { + let mut s = world(); + s.run("notebook.new", &json!({"name":"Self"})).unwrap(); + let script = "layer.visibility {\"layer\":\"Cities\",\"visible\":false}\nnotebook.editCell {\"notebook\":\"Self\",\"cell\":1,\"source\":\"map.zoomIn {}\"}\nmap.zoomOut {}"; + s.run("notebook.editCell", &json!({"notebook":"Self","cell":1,"source":script})).unwrap(); + let old = s.map().layer_by_name("Cities").unwrap().visible; + let (scope, _) = assistant(122); + let call = Call::command("notebook.run", json!({"notebook":"Self","cell":1})); + let p = pending(&s, &scope, &call); + assert!(s.run_permitted(scope.clone(), p.allow(), &call).is_err()); + assert_eq!(s.map().layer_by_name("Cities").unwrap().visible, old); + assert!(s.project.notebooks.iter().find(|n| n.name == "Self").unwrap().cells[1].source.contains("notebook.editCell")); + let graph = json!({"nodes":[ + {"id":"i","kind":"input","layer":"Cities"}, + {"id":"b","kind":"tool","tool":"buffer","params":{"distance":10}}, + {"id":"d","kind":"tool","tool":"dissolve"}, + {"id":"o","kind":"output","name":"Policy zones"}], + "edges":[{"from":"i","fromPort":"out","to":"b","toPort":"input"},{"from":"b","fromPort":"out","to":"d","toPort":"input"},{"from":"d","fromPort":"out","to":"o","toPort":"in"}]}); + s.run("model.new", &json!({"name":"Wired","graph":graph})).unwrap(); + let call = Call::command("model.run", json!({"name":"Wired"})); + let p = pending(&s, &scope, &call); + s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + assert!(s.map().layer_by_name("Policy zones").is_some()); + let mut cycle = graph.clone(); + cycle["edges"].as_array_mut().unwrap().push(json!({"from":"d","fromPort":"out","to":"b","toPort":"input"})); + s.run("model.new", &json!({"name":"Cycle","graph":cycle})).unwrap(); + assert!(matches!( + s.review(&scope, &Call::command("model.run", json!({"name":"Cycle"}))), + Outcome::Refused(reason) if reason.contains("loop") + )); + s.run( + "task.new", + &json!({"name":"Imported external","imported":true,"steps":[{"title":"Step","command":"layer.addData","params":{"path":"never-read.gpkg"}}]}), + ) + .unwrap(); + let full = Scope::agent(crate::policy::AgentAccess::Full, false, "full", None, Arc::new(Live(AtomicBool::new(true)))); + assert!(s.run_scoped(full, &Call::command("task.run", json!({"task":"Imported external"}))).is_err()); + let nested = s.with_scope(scope.clone(), |s| s.run_scoped(Scope::peer(), &Call::command("map.zoomIn", Value::Null))).unwrap(); + assert!(nested.is_err(), "nested entry cannot widen or switch owner"); + let external = Call::command("layer.addData", json!({"path":"never-read.gpkg"})).with_request("first"); + let p = pending(&s, &scope, &external); + assert!(s.run_permitted(scope, p.allow(), &external.with_request("second")).is_err()); +} + +/// State-dependent declarations follow the source actually used, including saved/default locator paths. +#[test] +fn permission_policy_effects_follow_saved_locator_streamed_and_time_sources() { + let mut s = world(); + let (scope, _) = assistant(123); + for p in [json!({"locator":"bare.rgisloc","text":"Austin"}), json!({"text":"Austin"})] { + let outcome = s.review(&scope, &Call::command("locator.find", p)); + assert!(!matches!(outcome, Outcome::Allowed)); + } + let id = s.map().layer_by_name("Cities").unwrap().id; + let mut source = rustgis_map::source::SourceLayer::default(); + source.path = Some("https://example.invalid/data.fgb".into()); + s.map_mut().layer_mut(id).unwrap().source = Some(source); + assert!(matches!(s.review(&scope, &Call::command("layer.loadAll", json!({"layer":"Cities"}))), Outcome::NeedsApproval(_))); + s.run("layer.addDataBytes", &json!({"fileName":"scene.tif","data":two_class_b64()})).unwrap(); + s.map_mut().layers.iter_mut().find(|l| l.name == "scene").unwrap().raster.as_mut().unwrap().time_zarr = + Some(rustgis_map::raster::ZarrTime { + store: "https://example.invalid/cube.zarr".into(), + variable: "v".into(), + dim: "time".into(), + dates: vec![0., 1.], + ..Default::default() + }); + assert!(matches!(s.review(&scope, &Call::command("time.step", json!({"direction":"next"}))), Outcome::NeedsApproval(_))); +} + +/// Cross-map frames and detached clipboards bind their actual owners; managed shares bind saved paths. +#[test] +fn permission_policy_stored_cross_map_clipboard_and_managed_share_targets() { + let mut s = world(); + s.leave_downloads = true; + let original = s.map().name.clone(); + s.run("map.duplicate", &json!({"name":"Other"})).unwrap(); + s.run("map.activate", &json!({"name":original})).unwrap(); + s.run("layout.new", &json!({"name":"Framed","template":"blank"})).unwrap(); + let frame = s.run("layout.addFrame", &json!({"map":"Other"})).unwrap()["element"].clone(); + s.run( + "task.new", + &json!({"name":"Frame export","steps":[ + {"title":"Step","command":"layout.frameZoom","params":{"element":frame,"to":"layer","layer":"Cities"}}, + {"title":"Step","command":"layer.addUrl","params":{"url":"https://example.invalid/no-send.csv"}} + ]}), + ) + .unwrap(); + let (scope, _) = assistant(124); + let call = Call::command("task.run", json!({"task":"Frame export"})); + let p = pending(&s, &scope, &call); + s.run("map.activate", &json!({"name":"Other"})).unwrap(); + s.run("layer.remove", &json!({"layer":"Cities"})).unwrap(); + s.run("layer.addDataText", &json!({"name":"Cities","fileName":"Cities.geojson","text":"{\"type\":\"Point\",\"coordinates\":[0,0]}"})) + .unwrap(); + s.run("map.activate", &json!({"name":original})).unwrap(); + assert!(s.run_permitted(scope.clone(), p.allow(), &call).unwrap_err().contains("changed")); + assert!(s.take_url_requests().is_empty()); + s.run("layer.copy", &json!({"layer":"Cities"})).unwrap(); + s.run("task.new",&json!({"name":"Paste","steps":[{"title":"Step","command":"layer.paste","params":{"map":"Other"}},{"title":"Step","command":"layer.addUrl","params":{"url":"https://example.invalid/no-send.csv"}}]})).unwrap(); + let call = Call::command("task.run", json!({"task":"Paste"})); + let p = pending(&s, &scope, &call); + s.run("layer.copy", &json!({"layer":"Countries"})).unwrap(); + assert!(s.run_permitted(scope.clone(), p.allow(), &call).unwrap_err().contains("changed")); + let dir = std::env::temp_dir().join(format!("rustgis-policy-stored-{}-{}", std::process::id(), crate::project_log::now_ms())); + std::fs::create_dir(&dir).unwrap(); + let first = dir.join("first.zip"); + let second = dir.join("second.zip"); + s.run("share.webLayer", &json!({"layer":"Cities","path":first})).unwrap(); + let call = Call::command("share.replaceWebLayer", json!({})); + let p = pending(&s, &scope, &call); + assert!( + p.review() + .details + .iter() + .any(|d| d.targets.iter().any(|n| n.ends_with(" / Cities")) && d.places.iter().any(|n| n == &first.display().to_string())) + ); + assert!(p.review().defaults.is_none(), "a managed saved source does not act on the unrelated active layer"); + s.run("share.webLayer", &json!({"layer":"Countries","path":second})).unwrap(); + let before = std::fs::read(&second).unwrap(); + assert!(s.run_permitted(scope, p.allow(), &call).unwrap_err().contains("changed")); + assert_eq!(std::fs::read(&second).unwrap(), before); + std::fs::remove_dir_all(dir).unwrap(); +} + +/// Plugin preparation reads a held streamed source before returning its pinned job. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_policy_plugin_preparation_keeps_owner_before_remote_send() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::permission_policy::permission_policy_plugin_preparation_keeps_owner_before_remote_send") { + return; + } + use rustgis_data::source::{Batch, FeatureSource, Request, SourceInfo}; + use std::{ + io::{Read, Write}, + sync::{Mutex, mpsc}, + time::Duration, + }; + struct Held { + inner: Box, + entered: mpsc::SyncSender<()>, + release: Mutex>, + url: String, + first: AtomicBool, + } + impl FeatureSource for Held { + fn info(&self) -> &SourceInfo { + self.inner.info() + } + fn read(&self, request: &Request) -> rustgis_data::Result { + if self.first.swap(false, Ordering::SeqCst) { + assert!(rustgis_data::source::remote::current_still().is_some(), "actual plugin input preparation owns its carrier"); + self.entered.send(()).unwrap(); + self.release.lock().unwrap().recv().unwrap(); + crate::render::rustgis_tiles::cache::download_blocking_within(&self.url, 64, Duration::from_secs(2)) + .map_err(rustgis_data::DataError::Format)?; + } + self.inner.read(request) + } + } + let plugin = "policy-preparing-owner"; + rustgis_plugins::load(rustgis_plugins::Package::from_zip(&rustgis_plugins::testing::package(plugin, &["edit"])).unwrap(), "test") + .unwrap(); + for revoke in [false, true] { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let url = format!("http://{}/input-prepare", listener.local_addr().unwrap()); + let mut s = world(); + let id = s.map().layer_by_name("Cities").unwrap().id; + let bytes = Arc::new(rustgis_data::source::fgb::write(&s.map().layer(id).unwrap().dataset).unwrap()); + let inner = rustgis_data::source::open_shared("input.fgb", bytes.clone(), None).unwrap(); + let (entered, waiting) = mpsc::sync_channel(1); + let (release, released) = mpsc::sync_channel(1); + s.map_mut().layer_mut(id).unwrap().source = Some(rustgis_map::source::SourceLayer::from_bytes( + "input.fgb", + bytes, + None, + Box::new(Held { inner, entered, release: Mutex::new(released), url, first: AtomicBool::new(true) }), + )); + let live = Arc::new(Live(AtomicBool::new(true))); + let scope = Scope::agent(crate::policy::AgentAccess::Allow, false, "plugin-preparation", None, live.clone()); + let worker = std::thread::spawn(move || { + let result = s + .with_scope(scope, |s| crate::cmd::gp::plugin_start(s, "plugin:policy-preparing-owner/x", &json!({"input":"Cities"}))) + .unwrap(); + assert!(rustgis_data::source::remote::current_still().is_none()); + result + }); + waiting.recv_timeout(Duration::from_secs(3)).unwrap(); + live.0.store(!revoke, Ordering::SeqCst); + let released_at = std::time::Instant::now(); + release.send(()).unwrap(); + let peer = if !revoke { + let start = std::time::Instant::now(); + let mut peer = loop { + match listener.accept() { + Ok((peer, _)) => break peer, + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock && start.elapsed() < Duration::from_secs(3) => { + std::thread::yield_now() + } + other => panic!("live preparation must actually send: {other:?}"), + } + }; + peer.set_nonblocking(false).unwrap(); + peer.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); + let mut request = Vec::new(); + while !request.ends_with(b"\r\n\r\n") { + let mut chunk = [0; 1024]; + let n = peer.read(&mut chunk).unwrap(); + assert!(n > 0 && request.len() + n <= 32_768, "a bounded complete HTTP request"); + request.extend_from_slice(&chunk[..n]); + } + assert!(request.starts_with(b"GET /input-prepare HTTP/")); + peer.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok").unwrap(); + Some(peer) + } else { + None + }; + let result = worker.join().unwrap(); + drop(peer); + assert_eq!(result.is_err(), revoke, "revoke={revoke}, elapsed={:?}, error={:?}", released_at.elapsed(), result.as_ref().err()); + assert!(matches!(listener.accept(), Err(e) if e.kind() == std::io::ErrorKind::WouldBlock)); + } + rustgis_plugins::unload(plugin); +} + +/// Cancellation after a real plugin process starts stops both pinned engine and analysis +/// calls before its valid result is released. The fallback releases and joins a broken run. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_policy_plugin_tools_follow_the_callers_control_while_running() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::permission_policy::permission_policy_plugin_tools_follow_the_callers_control_while_running") { + return; + } + use std::{ + sync::mpsc, + time::{Duration, Instant}, + }; + struct Input(rustgis_data::Dataset); + impl rustgis_analysis::Inputs for Input { + fn dataset(&self, name: &str) -> Option<&rustgis_data::Dataset> { + (name == "Cities").then_some(&self.0) + } + fn selection(&self, _: &str) -> &[usize] { + &[] + } + } + let plugin = "policy-plugin-control"; + let tool = "plugin:policy-plugin-control/m"; + let dir = std::env::temp_dir().join(format!("rustgis-policy-plugin-control-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let run = rustgis_plugins::testing::child_run("tests::plugin_process_child"); + std::fs::write(dir.join("plugin.json"), rustgis_plugins::testing::process_manifest(plugin, &[], &run)).unwrap(); + rustgis_plugins::load(rustgis_plugins::Package::from_dir(&dir).unwrap(), "test").unwrap(); + let mut outcomes = Vec::new(); + for engine in [true, false] { + let mut s = world(); + let before = serde_json::to_value(&s.map().layers).unwrap(); + let ready = dir.join(format!("ready-{engine}")); + let release = dir.join(format!("release-{engine}")); + let params = json!({"input":"Cities", "control_ready":ready, "control_release":release}); + let control = rustgis_analysis::control::Control::new(); + let caller = control.clone(); + let (finished, done) = mpsc::sync_channel(1); + let worker = std::thread::spawn(move || { + let result = if engine { + crate::cmd::gp::run_tool_with(&mut s, tool, ¶ms, &caller) + } else { + let input = Input(s.map().layer_by_name("Cities").unwrap().dataset.clone()); + rustgis_analysis::run_with(tool, &input, ¶ms, &caller).map(|_| Value::Null) + }; + finished.send(()).unwrap(); + (s, result) + }); + let start = Instant::now(); + while !ready.is_file() && !worker.is_finished() && start.elapsed() < Duration::from_secs(10) { + std::thread::sleep(Duration::from_millis(5)); + } + let entered = ready.is_file(); + control.cancel(); + let stopped = done.recv_timeout(Duration::from_secs(5)).is_ok(); + std::fs::write(release, b"finish if cancellation was ignored").unwrap(); + let (s, result) = worker.join().unwrap(); + let unchanged = serde_json::to_value(&s.map().layers).unwrap() == before; + let failed_history = !engine || s.project.gp_history.last().is_some_and(|h| !h.ok && h.outputs.is_empty()); + outcomes.push((engine, entered, stopped, result.err(), unchanged, failed_history)); + } + let mut clean = world(); + crate::cmd::gp::run_tool_with(&mut clean, tool, &json!({"input":"Cities"}), &rustgis_analysis::control::Control::new()).unwrap(); + assert!(clean.map().layer_by_name("Made").is_some()); + rustgis_plugins::unload(plugin); + std::fs::remove_dir_all(dir).unwrap(); + for (engine, entered, stopped, error, unchanged, failed_history) in outcomes { + assert!( + entered && stopped && error.as_ref().is_some_and(|e| e.contains("cancelled")) && unchanged && failed_history, + "engine={engine}, entered={entered}, stopped={stopped}, error={error:?}, unchanged={unchanged}, failed_history={failed_history}" + ); + } +} + +/// Choosing another already-active login changes consent without rotating either bearer. +#[test] +fn permission_policy_web_login_reselection_refuses_old_consent_and_landing() { + use crate::{render::rustgis_tiles::logins, service_logins}; + let mut s = Session::new(Project::default()); + let origin = "https://policy-choice-r15.example.invalid"; + let mut ids = Vec::new(); + for name in ["A", "B"] { + let id = + s.run("connection.new", &json!({"kind":"wms","name":name,"address":origin,"auth":"header","keyName":"X-Key"})).unwrap()["id"] + .as_str() + .unwrap() + .to_string(); + s.run("connection.login", &json!({"id":id,"apiKey":format!("RGSYNTH_choice_r15_{name}")})).unwrap(); + ids.push(id); + } + s.run("connection.login", &json!({"id":ids[0]})).unwrap(); + let registry = logins::account_identity(s.web_logins.session_id()).unwrap(); + let url = format!("{origin}/roads.geojson"); + let selected_a = service_logins::request(&mut s, None, &url).unwrap(); + let call = Call::command("layer.addUrl", json!({"url":url,"zoom":true})).with_request("choice-r15"); + let (scope, _) = assistant(1501); + let old = pending(&s, &scope, &call); + let run = pending(&s, &scope, &call); + #[cfg(not(target_arch = "wasm32"))] + { + s.leave_downloads = true; + } + s.run_permitted(scope.clone(), run.allow(), &call).unwrap(); + let request = s.take_url_requests().pop().unwrap(); + assert_eq!(request.params()["url"], selected_a); + assert_eq!(request.params()["connection"], ids[0]); + let claim = s.download_started_for(&request).unwrap(); + s.run("connection.login", &json!({"id":ids[1]})).unwrap(); + assert!(registry.is_current(), "reselection keeps both active generations"); + let selected_b = service_logins::request(&mut s, None, &url).unwrap(); + assert_ne!(selected_a, selected_b); + assert_eq!(logins::apply(&selected_a).unwrap().unwrap().header.unwrap().1, "RGSYNTH_choice_r15_A"); + assert!(s.run_permitted(scope, old.allow(), &call).unwrap_err().contains("changed")); + assert!(s.take_url_requests().is_empty(), "the old approval started no fresh request"); + assert!(s.download_continues(claim).unwrap_err().contains("account changed")); + assert!(s.map().layers.is_empty()); +} + +/// Runtime snapshots include absence and this owner's logout, while another Session is separate. +#[test] +fn permission_policy_web_accounts_ignore_other_sessions_and_revoke_owned_sends() { + use crate::render::rustgis_tiles::logins; + let mut s = Session::new(Project::default()); + let origin = "https://policy-owner-r15.example.invalid"; + let definition = json!({"kind":"wms","address":origin,"auth":"header","keyName":"X-Key"}); + let id = s.run("connection.new", &definition).unwrap()["id"].as_str().unwrap().to_string(); + let absent = logins::account_identity(s.web_logins.session_id()).unwrap(); + s.run("connection.login", &json!({"id":id,"apiKey":"fake002"})).unwrap(); + assert!(!absent.is_current(), "a newly covering login is a different runtime identity"); + let registry = logins::account_identity(s.web_logins.session_id()).unwrap(); + let (scope, _) = assistant(1502); + let call = Call::command("layer.addUrl", json!({"url":format!("{origin}/roads.geojson")})).with_request("owner-r15"); + let approved = pending(&s, &scope, &call); + let mut other = Session::new(Project::default()); + let other_id = other.run("connection.new", &definition).unwrap()["id"].as_str().unwrap().to_string(); + other.run("connection.login", &json!({"id":other_id,"apiKey":"fake003"})).unwrap(); + assert!(registry.is_current()); + #[cfg(not(target_arch = "wasm32"))] + { + s.leave_downloads = true; + } + s.run_permitted(scope, approved.allow(), &call).unwrap(); + let request = s.take_url_requests().pop().unwrap(); + let claim = s.download_started_for(&request).unwrap(); + let still = s.download_still(claim).unwrap(); + assert!(still()); + s.run("connection.logout", &json!({"id":id})).unwrap(); + assert!(!registry.is_current()); + assert!(!still(), "the actual send predicate sees this owner's logout"); + assert!(!s.download_landed(claim, b"late".to_vec())); + assert!(s.map().layers.is_empty()); +} + +/// Both export owners refer to the original map, even when another map is active. +fn map_export_session() -> Session { + let mut s = Session::new(Project::default()); + let map = s.map().name.clone(); + s.run("layout.new", &json!({"name":"Tile consent","template":"blank"})).unwrap(); + s.run("layout.addFrame", &json!({"map":map})).unwrap(); + s.run("presentation.new", &json!({"name":"Tile consent"})).unwrap(); + s.run("presentation.mapPage", &json!({"map":map})).unwrap(); + s +} + +/// Rendering consent binds private tile addresses, including changes hidden by redaction. +#[test] +fn permission_policy_render_exports_bind_raw_tile_sources() { + use rustgis_map::{TileKind, TileLayer}; + for (i, command) in ["layout.export", "presentation.export", "share.exportMap"].into_iter().enumerate() { + let mut s = map_export_session(); + s.map_mut().tiles.push(TileLayer::new( + "Reviewed tiles", + TileKind::Raster, + "https://tiles-r16.invalid/{z}/{x}/{y}.png?token=RGSYNTH_render_one", + )); + // The unsupported suffix stops the old handler before rendering or writing a file. + let call = Call::command(command, json!({"path":"policy-render.unsupported"})); + let (scope, _) = assistant(1600 + i as u64); + let approved = pending(&s, &scope, &call); + assert!(!format!("{:?}", approved.review()).contains("RGSYNTH_render_")); + s.map_mut().tiles[0].url = "https://tiles-r16.invalid/{z}/{x}/{y}.png?token=RGSYNTH_render_two".into(); + let error = s.run_permitted(scope, approved.allow(), &call).unwrap_err(); + assert!(error.contains("changed"), "{command} admitted a changed tile source: {error}"); + } +} + +/// Only maps consumed by the exporter contribute network reach; unrelated maps stay local. +#[test] +fn permission_policy_render_exports_require_referenced_tile_reach() { + use crate::policy::{AgentAccess, Effect}; + use rustgis_map::{TileKind, TileLayer}; + struct LocalOnly; + impl Revocation for LocalOnly { + fn allows(&self, effects: Effects) -> bool { + !effects.contains(Effect::Network) + } + } + let scope = Scope::agent(AgentAccess::Allow, false, "render-local-r16", None, Arc::new(LocalOnly)); + let mut s = map_export_session(); + s.run("map.duplicate", &json!({"name":"Unused map"})).unwrap(); + let calls = ["layout.export", "presentation.export"].map(|command| Call::command(command, json!({"path":"policy-render.pdf"}))); + for call in &calls { + assert!(matches!(s.review(&scope, call), Outcome::Allowed), "in-memory maps need no network access"); + } + s.map_mut().tiles.push(TileLayer::new("Unused tiles", TileKind::Raster, "https://unused-r16.invalid/{z}/{x}/{y}.png")); + for call in &calls { + assert!(matches!(s.review(&scope, call), Outcome::Allowed), "an unreferenced map is not rendered"); + } + let tile = s.map_mut().tiles.pop().unwrap(); + s.project.maps[0].tiles.push(tile); + for call in &calls { + assert!(matches!(s.review(&scope, call), Outcome::Refused(_)), "rendering referenced web tiles requires network access"); + } +} + +/// Advertised bounds are an in-memory zoom; absent archive/COG bounds require source access. +#[test] +fn permission_policy_zoom_to_tiles_reads_only_unknown_metadata() { + use crate::policy::AgentAccess; + use rustgis_map::{TileKind, TileLayer}; + let refuse = Scope::agent(AgentAccess::Refuse, false, "tile-metadata-refuse-r16", None, Arc::new(Live(AtomicBool::new(true)))); + let review = Scope::agent(AgentAccess::Review, false, "tile-metadata-review-r16", None, Arc::new(Live(AtomicBool::new(true)))); + let local = std::env::temp_dir().join("rustgis-policy-r16.mbtiles").display().to_string(); + for (kind, url) in [ + (TileKind::PmTiles, "https://metadata-r16.invalid/map.pmtiles?token=RGSYNTH_metadata"), + (TileKind::Cog, "https://metadata-r16.invalid/map.tif?token=RGSYNTH_metadata"), + (TileKind::MbTiles, local.as_str()), + ] { + let mut s = Session::new(Project::default()); + s.map_mut().tiles.push(TileLayer::new("Coverage", kind, url)); + let call = Call::command("map.zoomToTiles", json!({"index":0})); + assert!(matches!(s.review(&refuse, &call), Outcome::Refused(_)), "{kind:?} metadata needs source access"); + let Outcome::NeedsApproval(approved) = s.review(&review, &call) else { panic!("{kind:?} metadata needs review") }; + assert!(!format!("{:?}", approved.review()).contains("RGSYNTH_metadata")); + s.map_mut().tiles[0].bounds = Some([-12.0, 40.0, 12.0, 60.0]); + assert!(matches!(s.review(&refuse, &call), Outcome::Allowed), "stored bounds need no source read"); + assert_eq!(s.run_scoped(refuse.clone(), &call).unwrap()["bounds"], json!([-12.0, 40.0, 12.0, 60.0])); + } + let mut s = Session::new(Project::default()); + s.map_mut().tiles.push(TileLayer::new("No metadata", TileKind::Raster, "https://metadata-r16.invalid/{z}/{x}/{y}.png")); + let call = Call::command("map.zoomToTiles", json!({"index":0})); + assert!(matches!(s.review(&refuse, &call), Outcome::Allowed), "ordinary XYZ tiles have no metadata read"); + assert!(s.run_scoped(refuse, &call).unwrap_err().contains("doesn't say what it covers")); +} + +/// Optional measurement rasters stay optional in both strict automation vehicles. +#[test] +fn permission_policy_layerless_measurement_matches_strict_execution() { + let mut s = world(); + let (scope, _) = assistant(3501); + for kind in ["distance", "area"] { + let params = json!({"kind":kind,"points":[[0,0],[1,0],[1,1]]}); + let direct = s.run("raster.measure", ¶ms).unwrap(); + let script = format!("raster.measure {params}"); + let call = Call::command("script.run", json!({"script":script})); + let p = pending(&s, &scope, &call); + let result = s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + assert_eq!(result["ran"][0]["result"], direct); + let name = format!("Measure {kind}"); + s.run("task.new", &json!({"name":name,"steps":[{"title":"Measure","command":"raster.measure","params":params}]})).unwrap(); + let call = Call::command("task.run", json!({"task":name})); + let p = pending(&s, &scope, &call); + s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + } + s.run("layer.addDataBytes", &json!({"fileName":"scene.tif","data":two_class_b64()})).unwrap(); + let params = json!({"kind":"distance","layer":"scene","points":[[0,0],[1,0]]}); + let call = Call::command("script.run", json!({"script":format!("raster.measure {params}")})); + let p = pending(&s, &scope, &call); + s.run("layer.remove", &json!({"layer":"scene"})).unwrap(); + let measurements = s.measurements.len(); + assert!(s.run_permitted(scope.clone(), p.allow(), &call).is_err()); + assert_eq!(s.measurements.len(), measurements, "an explicitly selected missing raster remains bound"); + assert!(s.run("raster.measure", &json!({"kind":"volume","points":[[0,0],[1,0],[1,1]]})).is_err()); +} + +/// In-run renamed inputs retain external reach in the fast preflight and the strict card. +#[test] +fn permission_policy_renamed_streamed_inputs_keep_their_reach() { + use crate::policy::AgentAccess; + let mut outcomes = Vec::new(); + for tool in [true, false] { + let mut s = world(); + let id = s.map().layer_by_name("Cities").unwrap().id; + let mut source = rustgis_map::source::SourceLayer::default(); + source.path = Some("https://example.invalid/renamed.fgb".into()); + let layer = s.map_mut().layer_mut(id).unwrap(); + layer.source = Some(source); + layer.selection.clear(); + let read = if tool { + "gp.run {\"tool\":\"buffer\",\"params\":{\"input\":\"Renamed source\",\"distance\":10}}" + } else { + "layer.loadAll {\"layer\":\"Renamed source\"}" + }; + let script = format!("layer.rename {{\"layer\":\"Cities\",\"name\":\"Renamed source\"}}\n{read}"); + let (scope, _) = assistant(3502 + u64::from(tool)); + let call = Call::script(&script); + let needs_approval = matches!(s.review(&scope, &call), Outcome::NeedsApproval(_)); + let strict = Call::command("script.run", json!({"script":script})); + let p = pending(&s, &scope, &strict); + let operation = if tool { "buffer " } else { "layer.loadAll " }; + let shown_as_external = p.review().steps.iter().any(|step| step.starts_with(operation)); + outcomes.push((operation, needs_approval, shown_as_external)); + // A late refusal must still restore the earlier rename; no source read is permitted. + let before = serde_json::to_value(&s.project).unwrap(); + let refuse = Scope::agent(AgentAccess::Refuse, false, "rename-no-io", None, Arc::new(Live(AtomicBool::new(true)))); + assert!(s.run_scoped(refuse, &call).is_err()); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before); + } + assert!(outcomes.iter().all(|(_, asks, shown)| *asks && *shown), "{outcomes:?}"); +} + +/// In-memory range reads need Network, while local paths and external drivers retain Files. +#[test] +fn permission_policy_remote_range_reads_need_only_network() { + let mut s = world(); + let id = s.map().layer_by_name("Cities").unwrap().id; + s.map_mut().layer_mut(id).unwrap().selection.clear(); + let (scope, _) = assistant(3601); + for (path, driver, files, network) in [ + ("https://example.invalid/roads.fgb", false, false, true), + ("http://example.invalid/roads.parquet?subset=all", false, false, true), + ("s3://example/roads.geoparquet", false, false, true), + ("/local/roads.fgb", false, true, false), + ("https://example.invalid/roads.fgb", true, true, true), + ] { + let mut source = rustgis_map::source::SourceLayer::default(); + source.path = Some(path.into()); + source.gdal = driver.then(|| rustgis_map::source::GdalOrigin { + path: "external-driver-source".into(), + layer: "roads".into(), + family: None, + crs: None, + }); + s.map_mut().layer_mut(id).unwrap().source = Some(source); + for call in [ + Call::command("layer.loadAll", json!({"layer":"Cities"})), + Call::tool("buffer", json!({"input":"Cities","distance":10})), + Call::command("gp.spatialJoin", json!({"target":"Cities","join":"Countries","output":"Joined"})), + ] { + let approved = pending(&s, &scope, &call); + let why = &approved.review().why; + assert_eq!(why.contains("files"), files, "{path}: {why}"); + assert_eq!(why.contains("internet"), network, "{path}: {why}"); + } + } +} + +/// Unicode folds do not alias distinct executor names, for prior renames or new outputs. +#[test] +fn permission_policy_planned_names_use_executor_ascii_case() { + let mut s = world(); + let cities = s.map().layer_by_name("Cities").unwrap().id; + let countries = s.map().layer_by_name("Countries").unwrap().id; + let (scope, _) = assistant(3602); + let call = Call::command( + "script.run", + json!({"script": + "layer.rename {\"layer\":\"Cities\",\"name\":\"K\"}\nlayer.rename {\"layer\":\"Countries\",\"name\":\"K\"}\nlayer.visibility {\"layer\":\"k\",\"visible\":false}\nlayer.visibility {\"layer\":\"K\",\"visible\":true}" + }), + ); + let approved = pending(&s, &scope, &call); + s.run_permitted(scope.clone(), approved.allow(), &call).unwrap(); + assert_eq!(s.map().layer_by_name("k").unwrap().id, cities); + assert!(!s.map().layer(cities).unwrap().visible); + assert_eq!(s.map().layer_by_name("K").unwrap().id, countries); + assert!(s.map().layer(countries).unwrap().visible); + for first in [ + format!( + "layer.addDataText {}", + json!({"name":"Kelvin","fileName":"point.geojson","text":"{\"type\":\"Point\",\"coordinates\":[0,0]}"}) + ), + "gp.run {\"tool\":\"buffer\",\"params\":{\"input\":\"K\",\"distance\":10,\"output\":\"Kelvin\"}}".into(), + ] { + for next in [ + "layer.visibility {\"layer\":\"Kelvin\",\"visible\":false}", + "gp.run {\"tool\":\"buffer\",\"params\":{\"input\":\"Kelvin\",\"distance\":10}}", + ] { + let call = Call::command("script.run", json!({"script":format!("{first}\n{next}")})); + assert!( + matches!(s.review(&scope, &call), Outcome::Refused(reason) + if reason.contains("Kelvin") && (reason.contains("no layer") || reason.contains("no earlier output"))), + "Unicode-distinct missing outputs must refuse before execution" + ); + } + } +} + +/// Secondary optional inputs must retain future names for the planner's existing alias resolver. +#[test] +fn permission_policy_stretch_like_keeps_its_renamed_raster_target() { + let mut s = world(); + for file in ["scene.tif", "template.tif"] { + s.run("layer.addDataBytes", &json!({"fileName":file,"data":two_class_b64()})).unwrap(); + } + let (scope, _) = assistant(3504); + let call = Call::command( + "script.run", + json!({"script": + "layer.rename {\"layer\":\"scene\",\"name\":\"Renamed scene\"}\nlayer.rename {\"layer\":\"template\",\"name\":\"Renamed template\"}\nraster.stretch {\"layer\":\"Renamed scene\",\"like\":\"Renamed template\"}" + }), + ); + let p = pending(&s, &scope, &call); + let detail = p.review().details.iter().find(|d| d.operation == "raster.stretch").unwrap(); + assert!(detail.targets.iter().any(|t| t.ends_with(" / scene")), "{detail:?}"); + assert!(detail.targets.iter().any(|t| t.ends_with(" / template")), "the actual secondary raster must appear: {detail:?}"); + s.run_permitted(scope, p.allow(), &call).unwrap(); + assert!(s.map().layer_by_name("Renamed scene").is_some()); + assert!(s.map().layer_by_name("Renamed template").is_some()); +} + +/// The save executor already retires the old trust key before the shared post-handler hook. +#[test] +fn permission_policy_model_rename_does_not_leave_old_name_trust() { + let mut s = world(); + let graph = json!({"nodes":[{"id":"i","kind":"input","layer":"Cities"},{"id":"b","kind":"tool","tool":"buffer","params":{"distance":10}}], + "edges":[{"from":"i","fromPort":"out","to":"b","toPort":"input"}]}); + s.run("model.new", &json!({"name":"Original","graph":graph})).unwrap(); + let original = s.item_content("model", "Original").unwrap(); + assert!(s.item_trusted("model", "Original")); + s.run("model.save", &json!({"name":" Original ","rename":" Renamed ","graph":graph})).unwrap(); + assert!(s.item_trusted("model", "Renamed")); + assert!(!s.trusted_models.contains_key("original")); + s.run("model.new", &json!({"name":"Original","graph":graph,"imported":true})).unwrap(); + assert_eq!(s.item_content("model", "Original").unwrap(), original, "the recreated executable content is identical"); + assert!(!s.item_trusted("model", "Original")); + s.run("edit.undo", &Value::Null).unwrap(); + assert!(s.item_trusted("model", "Renamed")); + s.run("edit.undo", &Value::Null).unwrap(); + assert!(s.item_trusted("model", "Original")); + assert!(s.project.models.iter().all(|m| m.name != "Renamed")); +} + +/// An ignored field on a trusted editor cannot confer trust on another item's unchanged code. +#[test] +fn permission_policy_ignored_rename_does_not_promote_another_task() { + let mut s = world(); + s.run("task.new", &json!({"name":"Authored","steps":[]})).unwrap(); + s.run( + "task.new", + &json!({"name":"Foreign","imported":true,"steps":[ + {"title":"Foreign source","command":"layer.addUrl","params":{"url":"https://example.invalid/foreign.csv"}} + ]}), + ) + .unwrap(); + let foreign = s.item_content("task", "Foreign").unwrap(); + assert!(s.item_trusted("task", "Authored")); + assert!(!s.item_trusted("task", "Foreign")); + s.run("task.addStep", &json!({"task":"Authored","title":"Zoom","command":"map.zoomIn","rename":"Foreign"})).unwrap(); + assert_eq!(s.item_content("task", "Foreign").unwrap(), foreign); + assert!(!s.item_trusted("task", "Foreign"), "an ignored field must not promote unchanged imported code"); + assert!(s.item_trusted("task", "Authored"), "the actual user-edited task retains its content record"); +} + +/// Strict consent refuses a name collision instead of guessing which layer execution will select. +#[test] +fn permission_policy_rename_collision_refuses_only_strict_review() { + let s = world(); + let params = json!({"layer":"Cities","name":" Countries "}); + let call = Call::command("script.run", json!({"script":format!("layer.rename {params}")})); + let (scope, _) = assistant(3505); + let before = serde_json::to_value(&s.project).unwrap(); + assert!(matches!(s.review(&scope, &call), Outcome::Refused(e) if e.contains("share another layer's name"))); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before, "refusal never executes the rename"); + + let call = Call::command( + "script.run", + json!({"script": + "layer.rename {\"layer\":\"Cities\",\"name\":\"Fresh\"}\nlayer.rename {\"layer\":\"Countries\",\"name\":\"Fresh\"}" + }), + ); + assert!(matches!(s.review(&scope, &call), Outcome::Refused(e) if e.contains("share another layer's name"))); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before, "planned collisions are refused before either rename"); + + let mut s = world(); + let id = s.map().layer_by_name("Cities").unwrap().id; + let call = Call::command( + "script.run", + json!({"script": + "layer.rename {\"layer\":\"Cities\",\"name\":\"Fresh\"}\nlayer.rename {\"layer\":\"Fresh\",\"name\":\"Fresh\"}" + }), + ); + let p = pending(&s, &scope, &call); + s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + assert_eq!(s.map().layer(id).unwrap().name, "Fresh", "a repeat rename of the same bound object remains allowed"); + + let mut s = world(); + let id = s.map().layer_by_name("Cities").unwrap().id; + s.run("layer.rename", ¶ms).unwrap(); + assert_eq!(s.map().layer(id).unwrap().name, "Countries", "ordinary User naming remains unchanged"); + let mut s = world(); + let id = s.map().layer_by_name("Cities").unwrap().id; + let allow = + Scope::agent(crate::policy::AgentAccess::Allow, false, "rename-collision-allow", None, Arc::new(Live(AtomicBool::new(true)))); + let call = Call::script(&format!("layer.rename {params}")); + assert!(matches!(s.review(&allow, &call), Outcome::Allowed)); + s.run_scoped(allow, &call).unwrap(); + assert_eq!(s.map().layer(id).unwrap().name, "Countries", "the Allow ingress retains its existing convention"); +} + +/// Mask and elevation metadata retain names that only exist after an earlier planned rename. +#[test] +fn permission_policy_renamed_mask_and_elevation_keep_their_targets() { + let mut s = world(); + s.run("layer.addDataBytes", &json!({"fileName":"scene.tif","data":two_class_b64()})).unwrap(); + let cities = s.map().layer_by_name("Cities").unwrap().id; + let (scope, _) = assistant(3506); + for params in [json!({"layer":"Cities","mask":"Cities"}), json!({"layer":"Countries","mask":"Countries"})] { + let call = Call::command("script.run", json!({"script":format!("layer.mask {params}")})); + assert!(matches!(s.review(&scope, &call), Outcome::Refused(_)), "known nonpolygon and self masks remain refused"); + } + assert!(s.run("edit.elevation", &json!({"mode":"surface","surface":"Cities"})).is_err()); + let call = Call::command( + "script.run", + json!({"script": + "layer.rename {\"layer\":\"Countries\",\"name\":\"Renamed mask\"}\nlayer.rename {\"layer\":\"Cities\",\"name\":\"Renamed cities\"}\nlayer.rename {\"layer\":\"scene\",\"name\":\"Renamed surface\"}\nlayer.mask {\"layer\":\"Renamed cities\",\"mask\":\"Renamed mask\"}\nedit.elevation {\"mode\":\"surface\",\"surface\":\"Renamed surface\"}" + }), + ); + let p = pending(&s, &scope, &call); + let mask = p.review().details.iter().find(|d| d.operation == "layer.mask").unwrap(); + assert!(mask.targets.iter().any(|t| t.ends_with(" / Countries")), "{mask:?}"); + assert!(mask.targets.iter().any(|t| t.ends_with(" / Cities")), "{mask:?}"); + let elevation = p.review().details.iter().find(|d| d.operation == "edit.elevation").unwrap(); + assert!(elevation.targets.iter().any(|t| t.ends_with(" / scene")), "{elevation:?}"); + s.run_permitted(scope, p.allow(), &call).unwrap(); + assert_eq!(s.map().layer(cities).unwrap().mask.as_deref(), Some("Renamed mask")); + assert!(matches!(&s.map().editing.settings.elevation, rustgis_map::design::Elevation::Surface(name) if name == "Renamed surface")); +} + +/// A failed drain can restore untouched queued work, but cannot revive terminally retired work. +#[test] +fn permission_policy_rollback_keeps_retired_url_requests_consumed() { + let mut s = world(); + s.leave_downloads = true; + let (owner, live) = assistant(245); + let call = Call::command("layer.addUrl", json!({"url":"https://rollback.invalid/revoked.csv"})); + let p = pending(&s, &owner, &call); + s.run_permitted(owner, p.allow(), &call).unwrap(); + s.request_url(&json!({"url":"https://rollback.invalid/untouched.csv"})).unwrap(); + live.0.store(false, Ordering::SeqCst); + let mark = s.notice_mark(); + assert!( + s.guarded("Failed request drain", true, |s| { + s.take_url_requests(); + Err("fail after retiring a revoked request".into()) + }) + .is_err() + ); + live.0.store(true, Ordering::SeqCst); + let remaining = s.take_url_requests(); + let addresses: Vec<_> = remaining.iter().map(|q| q.params()["url"].as_str().unwrap()).collect(); + assert_eq!(addresses, ["https://rollback.invalid/untouched.csv"], "rollback cannot revive retired authority"); + assert_eq!(s.notices_since(mark).iter().filter(|n| n.text.contains("revoked before it started")).count(), 1); +} diff --git a/crates/engine/src/tests/review_boundaries.rs b/crates/engine/src/tests/review_boundaries.rs new file mode 100644 index 0000000000..60c8e9bb37 --- /dev/null +++ b/crates/engine/src/tests/review_boundaries.rs @@ -0,0 +1,372 @@ +//! Review regressions exercise public admission and ownership without external traffic. +use super::*; +use crate::policy::{AgentAccess, Call, Effect, Effects, Outcome, Revocation, Scope}; +use std::sync::Arc; + +struct Live; +impl Revocation for Live { + fn allows(&self, _: Effects) -> bool { + true + } +} + +/// A live agent whose external access setting is enforced by the normal decision table. +fn agent(access: AgentAccess) -> Scope { + Scope::agent(access, true, "review-boundaries", None, Arc::new(Live)) +} + +#[test] +fn permission_review_authentication_changes_are_reserved() { + let s = world(); + for (id, params) in [ + ("arcgis.signIn", json!({})), + ("arcgis.token", json!({"url":"https://auth-review.example.invalid/arcgis/rest/services","token":"RGSYNTH_review_auth"})), + ("arcgis.signOut", json!({})), + ("arcgis.cancelSignIn", json!({})), + ] { + let call = Call::command(id, params); + assert!(matches!(s.review(&agent(AgentAccess::Allow), &call), Outcome::Refused(_)), "{id} is reserved even with external access"); + assert!( + matches!(s.review(&Scope::assistant(1461, Arc::new(Live)), &call), Outcome::NeedsApproval(_)), + "{id} needs explicit approval" + ); + assert!(matches!(s.review(&agent(AgentAccess::Full), &call), Outcome::Allowed), "full native-client access remains explicit"); + } +} + +#[test] +fn permission_review_connected_folder_availability_requires_files() { + let mut s = world(); + s.project.folders.push("not-opened-review-folder".into()); + let call = Call::command("project.connections", Value::Null); + assert!(matches!(s.review(&agent(AgentAccess::Refuse), &call), Outcome::Refused(_))); + assert!(matches!(s.review(&agent(AgentAccess::Review), &call), Outcome::NeedsApproval(_))); + assert!(matches!(s.review(&agent(AgentAccess::Allow), &call), Outcome::Allowed)); + assert!(matches!(s.review(&Scope::assistant(1461, Arc::new(Live)), &call), Outcome::NeedsApproval(_))); +} + +#[test] +fn permission_review_memory_only_commands_need_no_external_access() { + let mut s = world(); + let mut errors = Vec::new(); + for (id, params) in [ + ("graphics.add", json!({"shape":"text","points":[[0,0]],"text":"Review note"})), + ("cache.empty", json!({})), + ("cache.cancel", json!({})), + ("map.body", json!({"name":"moon","basemap":false})), + ] { + if let Err(error) = s.with_scope(agent(AgentAccess::Refuse), |s| s.run(id, ¶ms)).unwrap() { + errors.push((id, error)); + } + } + assert!(errors.is_empty(), "in-memory work was refused: {errors:?}"); + assert_eq!(s.map().body().id, "moon"); + for call in [ + Call::command("graphics.add", json!({"shape":"picture","points":[[0,0],[1,1]],"path":"missing-review.png"})), + Call::command("cache.emptyIndexes", json!({})), + Call::command("map.body", json!({"name":"earth"})), + ] { + assert!(matches!(s.review(&agent(AgentAccess::Refuse), &call), Outcome::Refused(_)), "real external reach stays gated"); + } +} + +#[test] +fn permission_review_postgis_write_declares_network_reach() { + struct Without(Effect); + impl Revocation for Without { + fn allows(&self, effects: Effects) -> bool { + !effects.contains(self.0) + } + } + let s = world(); + let call = Call::command("pg.saveEdits", json!({"layer":"Cities"})); + for effect in [Effect::Network, Effect::Files] { + let scope = Scope::assistant(1462, Arc::new(Without(effect))); + assert!(matches!(s.review(&scope, &call), Outcome::Refused(_)), "a database write must check connection authority before any I/O"); + assert!(matches!(s.review(&scope, &Call::command("pg.pendingEdits", json!({"layer":"Cities"}))), Outcome::Allowed)); + } +} + +#[test] +fn permission_review_url_claim_rejects_another_sessions_request() { + let mut a = world(); + let mut b = world(); + for (s, url) in [(&mut a, "https://request-a.invalid/a.geojson"), (&mut b, "https://request-b.invalid/b.geojson")] { + s.request_url(&json!({"url":url})).unwrap(); + } + let qa = a.take_url_requests().pop().unwrap(); + let qb = b.take_url_requests().pop().unwrap(); + assert_eq!(qa.key, qb.key, "independent sessions reuse their own queue sequence"); + assert!(b.download_started_for(&qa).is_err(), "a foreign opaque request must not consume the matching local queue key"); + let id = b.download_started_for(&qb).unwrap(); + assert!(b.download_continues(id).is_ok(), "the rejected claim preserves the rightful request"); + assert!(a.download_started_for(&qa).is_ok()); + assert!(b.download_started_for(&qb).is_err(), "a rightful claim still has one use"); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_review_non_s3_download_survives_an_unrelated_storage_login() { + if isolate_process_test("tests::review_boundaries::permission_review_non_s3_download_survives_an_unrelated_storage_login") { + return; + } + use crate::render::rustgis_tiles::s3; + let connection = |name: &str, bucket: &str, endpoint: &str| s3::Connection { + name: name.into(), + endpoint: Some(endpoint.into()), + region: s3::DEFAULT_REGION.into(), + buckets: vec![bucket.into()], + credentials: None, + }; + s3::connect(connection("review-owned", "review-owned", "https://owned-storage.invalid")).unwrap(); + let mut s = world(); + let mut ids = Vec::new(); + for url in ["https://plain-request.invalid/data.geojson", "s3://review-owned/data.geojson"] { + s.request_url(&json!({"url":url})).unwrap(); + let request = s.take_url_requests().pop().unwrap(); + ids.push(s.download_started_for(&request).unwrap()); + } + let plain_still = s.download_still(ids[0]).unwrap(); + let storage_still = s.download_still(ids[1]).unwrap(); + s3::connect(connection("review-unrelated", "review-unrelated", "https://other-storage.invalid")).unwrap(); + assert!(s.download_continues(ids[0]).is_ok(), "an unrelated S3 login cannot change this HTTPS request's account choice"); + assert!(plain_still(), "the actual sender keeps the same account choice"); + + // A service can follow discovered links even when its first URL looks like a data file. + s.request_url(&json!({"url":"https://plain-request.invalid/data.geojson","service":true})).unwrap(); + let discovery = s.take_url_requests().pop().unwrap(); + let discovery = s.download_started_for(&discovery).unwrap(); + s3::connect(connection("review-owned", "review-owned", "https://changed-storage.invalid")).unwrap(); + assert!(!storage_still(), "a changed account refuses the actual sender before another request"); + assert!(s.download_continues(ids[1]).is_err(), "a changed endpoint for the actual S3 source still revokes its continuation"); + assert!(s.download_continues(discovery).is_err(), "unresolved service discovery keeps conservative account checks"); + assert!(plain_still(), "a fixed HTTPS address still has no S3 signer"); + + let mut newly_covering = connection("review-covering", "review-covering", "https://plain-request.invalid"); + newly_covering.credentials = Some(s3::Credentials { + access_key_id: "RGSYNTH_review_covering".into(), + secret_access_key: "RGSYNTH_review_covering_secret".into(), + session_token: None, + }); + s3::connect(newly_covering).unwrap(); + assert!(!plain_still(), "absence is authority too: a newly covering signer requires a fresh request"); + assert!(s.download_continues(ids[0]).is_err()); + for name in ["review-owned", "review-unrelated", "review-covering"] { + s3::disconnect(name); + } +} + +/// Allows ordinary agent work while refusing one source capability before any I/O. +fn without_effect(effect: Effect) -> Scope { + struct Without(Effect); + impl Revocation for Without { + fn allows(&self, effects: Effects) -> bool { + !effects.contains(self.0) + } + } + Scope::agent(AgentAccess::Allow, true, "review-source-reach", None, Arc::new(Without(effect))) +} + +/// Two dates and two camera stops exercise the real time and animation render owners. +fn rendering_session() -> Session { + let mut s = world(); + s.run("time.set", &json!({"dates":["2020-01-01","2020-01-02"],"date":"2020-01-01"})).unwrap(); + for _ in 0..2 { + s.run("animation.add", &json!({"seconds":0.1})).unwrap(); + } + s +} + +#[test] +fn permission_review_render_owners_require_tile_network_access() { + let mut s = rendering_session(); + s.map_mut().tiles.push(rustgis_map::TileLayer::new( + "Review imagery", + rustgis_map::TileKind::Raster, + "https://render-review.invalid/{z}/{x}/{y}.png", + )); + let scope = without_effect(Effect::Network); + let mut admitted = Vec::new(); + for (id, params) in [ + ("time.export", json!({"path":"review-frames.zip"})), + ("animation.export", json!({"path":"review-camera.zip"})), + ("project.thumbnail", json!({"create":true})), + ("map.thumbnail", json!({"create":true})), + ] { + if !matches!(s.review(&scope, &Call::command(id, params)), Outcome::Refused(_)) { + admitted.push(id); + } + } + for id in ["project.thumbnail", "map.thumbnail"] { + for params in [json!({"image":"supplied-image"}), json!({"create":true,"clear":true})] { + assert!(matches!(s.review(&scope, &Call::command(id, params)), Outcome::Allowed), "{id} does not render this branch"); + } + s.run_scoped(scope.clone(), &Call::command(id, json!({"create":true,"clear":true}))).unwrap(); + } + assert!(admitted.is_empty(), "rendering web tiles needs network authority: {admitted:?}"); +} + +#[test] +fn permission_review_render_owners_bind_raw_tile_routes() { + let mut unchanged = Vec::new(); + for (id, params) in [ + ("time.export", json!({"path":"review-frames.zip"})), + ("animation.export", json!({"path":"review-camera.zip"})), + ("project.thumbnail", json!({"create":true})), + ("map.thumbnail", json!({"create":true})), + ] { + let mut s = rendering_session(); + s.map_mut().tiles.push(rustgis_map::TileLayer::new( + "Review imagery", + rustgis_map::TileKind::Raster, + "https://render-review.invalid/{z}/{x}/{y}.png?token=RGSYNTH_review_one", + )); + let call = Call::command(id, params); + let scope = Scope::assistant(1463, Arc::new(Live)); + let Outcome::NeedsApproval(approved) = s.review(&scope, &call) else { panic!("{id} needs approval") }; + assert!(!format!("{:?}", approved.review()).contains("RGSYNTH_review_")); + let remembered = approved.remember().unwrap(); + s.map_mut().tiles[0].url = "https://render-review.invalid/{z}/{x}/{y}.png?token=RGSYNTH_review_two".into(); + let Outcome::NeedsApproval(changed) = s.review(&scope, &call) else { panic!("{id} still needs approval") }; + if changed.covered_by(&remembered) { + unchanged.push(id); + } + } + assert!(unchanged.is_empty(), "remembered consent cannot cover changed raw tile routes: {unchanged:?}"); +} + +#[test] +fn permission_review_time_export_includes_only_selected_frame_routes() { + let mut s = rendering_session(); + let later = rustgis_data::time::parse_input(&json!("2020-01-02")).unwrap(); + let mut tile = rustgis_map::TileLayer::new( + "Future review imagery", + rustgis_map::TileKind::Raster, + "https://time-render-review.invalid/initial/{z}/{x}/{y}.png", + ); + tile.time_urls = vec![(later, "https://time-render-review.invalid/later/{z}/{x}/{y}.png?token=RGSYNTH_future_one".into())]; + s.map_mut().tiles.push(tile); + s.run("time.goTo", &json!({"step":0})).unwrap(); + assert!(!s.map().tiles[0].draws(), "the current frame has no imagery"); + let call = |date| Call::command("time.export", json!({"path":"review-time.zip","from":date,"to":date})); + assert!( + matches!(s.review(&without_effect(Effect::Network), &call("2020-01-01")), Outcome::Allowed), + "excluded imagery adds no network reach" + ); + let later_call = call("2020-01-02"); + let refused = matches!(s.review(&without_effect(Effect::Network), &later_call), Outcome::Refused(_)); + let scope = Scope::assistant(1464, Arc::new(Live)); + let Outcome::NeedsApproval(approved) = s.review(&scope, &later_call) else { panic!("time export needs approval") }; + assert!(!format!("{:?}", approved.review()).contains("RGSYNTH_future_")); + let remembered = approved.remember().unwrap(); + s.map_mut().tiles[0].time_urls[0].1 = "https://time-render-review.invalid/later/{z}/{x}/{y}.png?token=RGSYNTH_future_two".into(); + let Outcome::NeedsApproval(changed) = s.review(&scope, &later_call) else { panic!("changed time export needs approval") }; + assert!( + refused && !changed.covered_by(&remembered), + "a selected later frame needs network authority ({refused}) and exact route consent" + ); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn permission_review_local_tile_archives_require_files() { + let dir = std::env::temp_dir().join(format!("rustgis-review-tile-files-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("review.pmtiles"); + std::fs::write(&path, empty_pmtiles([-24.5, 63.3, -13.5, 66.6])).unwrap(); + let params = json!({"url":path.display().to_string()}); + let mut s = world(); + let mut admitted = Vec::new(); + for (kind, source) in [("pmtiles", path.clone()), ("mbtiles", dir.join("review.mbtiles"))] { + let call = Call::command("map.addTiles", json!({"url":source.display().to_string(),"kind":kind})); + if !matches!(s.review(&without_effect(Effect::Files), &call), Outcome::Refused(_)) { + admitted.push(kind); + } + } + for call in [ + Call::command("map.addTiles", json!({"url":"upload:review.mbtiles","kind":"mbtiles"})), + Call::command("sourcecoop.list", json!({"account":"review","product":"review","path":"folder/"})), + ] { + assert!( + matches!(s.review(&without_effect(Effect::Files), &call), Outcome::Allowed), + "memory archives and remote object prefixes do not read files" + ); + } + plugin("eng-review-local-tile", &["edit", "network"]); + let before = s.map().tiles.len(); + let denied = forward(&mut s, "eng-review-local-tile", "map.addTiles", params.clone()); + let unchanged = s.map().tiles.len() == before; + rustgis_plugins::unload("eng-review-local-tile"); + plugin("eng-review-file-tile", &["edit", "network", "files"]); + let allowed = forward(&mut world(), "eng-review-file-tile", "map.addTiles", params); + rustgis_plugins::unload("eng-review-file-tile"); + std::fs::remove_dir_all(dir).unwrap(); + assert!(allowed.is_ok(), "the same synthetic archive is available with files permission: {allowed:?}"); + assert!(admitted.is_empty(), "local archive metadata needs files authority: {admitted:?}"); + assert!( + unchanged && denied.as_ref().err().is_some_and(|e| e.contains("files")), + "a plugin without files cannot insert/read the archive: {denied:?}" + ); +} + +#[test] +fn permission_review_tile_metadata_helpers_require_source_access() { + let mut s = world(); + s.map_mut().tiles.push(rustgis_map::TileLayer::new( + "Review archive", + rustgis_map::TileKind::PmTiles, + std::env::temp_dir().join("rustgis-review-metadata.pmtiles").display().to_string(), + )); + let mut wrong = Vec::new(); + for bounds in [None, Some([-24.5, 63.3, -13.5, 66.6])] { + s.map_mut().tiles[0].bounds = bounds; + for (id, params) in [ + ("map.search", json!({"query":"Review archive","go":true})), + ("map.tiles", json!({})), + ("info.project", json!({})), + ("map.setTiles", json!({"index":0,"opacity":0.5})), + ] { + let needs_files = id != "map.search" || bounds.is_none(); + let refused = matches!(s.review(&without_effect(Effect::Files), &Call::command(id, params)), Outcome::Refused(_)); + if refused != needs_files { + wrong.push(format!("{id}: bounds={}, needs files={needs_files}", bounds.is_some())); + } + } + } + assert!(matches!( + s.review(&without_effect(Effect::Files), &Call::command("map.search", json!({"query":"Review archive"}))), + Outcome::Allowed + )); + s.map_mut().tiles[0] = rustgis_map::TileLayer::new("Review COG", rustgis_map::TileKind::Cog, "https://cog-review.invalid/image.tif"); + for bounds in [None, Some([-24.5, 63.3, -13.5, 66.6])] { + s.map_mut().tiles[0].bounds = bounds; + for (id, params, needs_network) in [ + ("map.search", json!({"query":"Review COG","go":true}), bounds.is_none()), + ("map.setTiles", json!({"index":0,"bands":[1]}), true), + ("map.setTiles", json!({"index":0,"opacity":0.5}), false), + ] { + let refused = matches!(s.review(&without_effect(Effect::Network), &Call::command(id, params)), Outcome::Refused(_)); + if refused != needs_network { + wrong.push(format!("{id}: bounds={}, needs network={needs_network}", bounds.is_some())); + } + } + } + assert!(wrong.is_empty(), "metadata readers must declare their actual source reach: {wrong:?}"); +} + +/// Rejects an over-limit script before its first edit instead of silently running a prefix. +#[test] +fn permission_review_script_line_limit_refuses_before_any_edit() { + let mut s = world(); + let before = serde_json::to_value(&s.project).unwrap(); + let prefix = format!( + "layer.rename {{\"layer\":\"Cities\",\"name\":\"Renamed\"}}\n{}", + "# kept\n".repeat(crate::cmd::workflows::MAX_SCRIPT_LINES - 1) + ); + let oversized = format!("{prefix}map.basemap {{\"name\":\"blank\"}}"); + let result = s.run("script.run", &json!({"script":oversized})); + assert!(result.is_err(), "a script over the line limit must be refused, not partially run"); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before); + s.run("script.run", &json!({"script":prefix})).unwrap(); + assert!(s.map().layer_by_name("Renamed").is_some(), "the exact boundary remains supported"); +} diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index 3d96572550..da9ac523a0 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -1143,13 +1143,13 @@ fn plugin_downloads_stay_anonymous_after_the_front_end_adds_them() { .unwrap(); let request = s.take_url_requests().pop().unwrap(); // The claimed UI path preserves the engine's bound parameters and anonymous provenance. - let mut pending = request.clone(); + let mut pending = request.params().clone(); assert_eq!(pending["anonymous"], true); assert!(header(pending["url"].as_str().unwrap()).is_none()); - let token = s.download_started(); + let token = s.download_started_for(&request).unwrap(); assert!(s.download_landed(token, br#"{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":{}}]}"#.to_vec())); pending["id"] = json!(token); - s.run("layer.addDownload", &pending).unwrap(); + s.complete_download(token, &pending).unwrap(); s.run("layer.addUrl", &json!({"url":format!("{origin}/deferred.pmtiles"),"anonymous":true,"name":"Archive"})).unwrap(); s.run("connection.login", &json!({"id":id,"apiKey":"deferred-sentinel-757"})).unwrap(); let layer = s.map().layers.iter().find(|l| l.name == "Deferred").unwrap(); diff --git a/crates/engine/src/tests/task_reentry.rs b/crates/engine/src/tests/task_reentry.rs index 97b5b05cbd..e9acde7212 100644 --- a/crates/engine/src/tests/task_reentry.rs +++ b/crates/engine/src/tests/task_reentry.rs @@ -1,5 +1,19 @@ //! Task re-entry stays bounded without changing finite scripts or outer transactions. use super::*; +use crate::policy::{AgentAccess, Call, Effect, Effects, Outcome, Revocation, Scope}; +use std::sync::Arc; + +struct Live; +impl Revocation for Live { + fn allows(&self, _: Effects) -> bool { + true + } +} + +/// A live caller whose access setting is checked by the ordinary permission table. +fn agent(access: AgentAccess) -> Scope { + Scope::agent(access, true, "metadata-review", Some("metadata-review".into()), Arc::new(Live)) +} /// Create a locally trusted task through the public API, keeping the trust policy in the test. fn task(s: &mut Session, name: &str, steps: Value) { @@ -158,3 +172,149 @@ fn task_reentry_guard_covers_notebook_paths() { assert!(s.project.notebooks[0].cells.iter().any(|c| c.error && c.output.as_deref().is_some_and(|o| o.contains("already running")))); assert!(!s.task_running && !s.notebook_running); } + +/// Running locally authored notebook cells requires a fresh approval, as other automation does. +#[test] +fn permission_review_notebook_automation_asks_each_time() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::task_reentry::permission_review_notebook_automation_asks_each_time") { + return; + } + let mut s = world(); + s.run("notebook.new", &json!({"name":"Local automation"})).unwrap(); + s.run("notebook.editCell", &json!({"notebook":"Local automation","cell":1,"source":"map.rename {\"name\":\"Notebook result\"}"})) + .unwrap(); + let scope = Scope::assistant(1481, Arc::new(Live)); + let call = Call::command("notebook.run", json!({"notebook":"Local automation","cell":1})); + let Outcome::NeedsApproval(pending) = s.review(&scope, &call) else { panic!("notebook automation needs approval") }; + assert!(!pending.review().remember, "a notebook run cannot be approved for the whole conversation"); + s.run_permitted(scope, pending.allow(), &call).unwrap(); + assert_eq!(s.map().name, "Notebook result"); +} + +/// A plugin's file/edit permissions do not grant access to a user's notebook automation. +#[test] +fn permission_review_notebook_automation_is_refused_to_plugins() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::task_reentry::permission_review_notebook_automation_is_refused_to_plugins") { + return; + } + let mut s = world(); + s.run("notebook.new", &json!({"name":"User notebook"})).unwrap(); + s.run("notebook.editCell", &json!({"notebook":"User notebook","cell":1,"source":"map.rename {\"name\":\"Notebook result\"}"})).unwrap(); + let plugin_id = "review-notebook-automation"; + plugin(plugin_id, &["files", "edit"]); + let before = serde_json::to_value(&s.project).unwrap(); + let result = forward(&mut s, plugin_id, "notebook.run", json!({"notebook":"User notebook","cell":1})); + rustgis_plugins::unload(plugin_id); + assert!(result.is_err(), "a plugin cannot start notebook automation: {result:?}"); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before); + s.run("notebook.run", &json!({"notebook":"User notebook","cell":1})).unwrap(); + assert_eq!(s.map().name, "Notebook result", "native notebook execution remains available"); +} + +/// A live network denial must refuse connection admission before any database is opened. +#[test] +fn permission_review_postgis_connect_checks_live_network_authority() { + struct NoNetwork; + impl Revocation for NoNetwork { + fn allows(&self, effects: Effects) -> bool { + !effects.contains(Effect::Network) + } + } + let s = world(); + let scope = Scope::assistant(1482, Arc::new(NoNetwork)); + let call = Call::command("pg.connect", json!({"host":"127.0.0.1","database":"not-opened"})); + assert!( + matches!(s.review(&scope, &call), Outcome::Refused(error) if error.contains("revoked")), + "connecting must check the caller's live network authority before I/O" + ); + assert!(matches!(s.review(&scope, &Call::command("pg.status", json!({}))), Outcome::Allowed)); + let live = Scope::assistant(1483, Arc::new(Live)); + assert!(matches!(s.review(&live, &call), Outcome::NeedsApproval(_)), "a live connection request still asks for explicit approval"); +} + +/// Lists, offline snapshots and projected caches stay available without external access or lost undo. +#[test] +fn permission_review_memory_views_and_offline_work_need_no_external_access() { + // Undo invalidates geometry caches; every other project field must be restored exactly. + let payload = |project: &Project| { + let mut project = project.clone(); + for map in &mut project.maps { + for layer in &mut map.layers { + layer.geometry_rev = 0; + } + } + serde_json::to_value(project).unwrap() + }; + let mut s = world(); + for name in ["Open area", "Sync area", "Remove area"] { + s.run("offline.download", &json!({"name":name,"extent":[1,48,4,50]})).unwrap(); + } + s.run("cache.empty", &Value::Null).unwrap(); + let scope = agent(AgentAccess::Refuse); + let mut errors = Vec::new(); + for (id, params) in [ + ("share.jobs", json!({})), + ("offline.download", json!({"name":"Created area","extent":[1,48,4,50]})), + ("offline.open", json!({"name":"Open area"})), + ("offline.sync", json!({"name":"Sync area"})), + ("offline.remove", json!({"name":"Remove area"})), + ("cache.fill", json!({})), + ("cache.auto", json!({"on":true})), + ] { + let (before, undo) = (payload(&s.project), s.undo_depth()); + match s.run_scoped(scope.clone(), &Call::command(id, params)) { + Err(error) => errors.push((id, error)), + Ok(result) => { + if id == "cache.fill" { + assert!(result["cached"].as_u64().is_some_and(|n| n > 0), "{result}"); + } + if id == "cache.auto" { + assert!(s.auto_cache); + } + if matches!(id, "offline.download" | "offline.open" | "offline.remove") { + assert_eq!(s.undo_depth(), undo + 1, "{id} keeps its ordinary edit transaction"); + s.run("edit.undo", &Value::Null).unwrap(); + assert_eq!(payload(&s.project), before, "{id} remains undoable"); + } + } + } + } + assert!(errors.is_empty(), "memory-only work was refused: {errors:?}"); +} + +/// Changes to saved/favourite preferences require file permission before their lists change. +#[test] +fn permission_review_source_preferences_require_file_permission() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_process_test("tests::task_reentry::permission_review_source_preferences_require_file_permission") { + return; + } + // A connection label carries no path-shaped text from which Files could be inferred. + let source = json!({"kind":"database","address":"Review connection"}); + let mut failures = Vec::new(); + for (id, params) in [ + ("sources.favourite", source.clone()), + ("sources.favourite", json!({"kind":"database","address":source["address"],"on":false})), + ("sources.remove", json!({"list":"saved","kind":"database","address":source["address"]})), + ] { + let mut s = world(); + s.run("sources.save", &source).unwrap(); + if params.get("on") == Some(&Value::Bool(false)) { + s.run("sources.favourite", &source).unwrap(); + } + let before = s.sources.to_json(); + let call = Call::command(id, params); + if s.run_scoped(agent(AgentAccess::Refuse), &call).is_ok() { + failures.push(format!("{id} changed preferences without file permission")); + continue; + } + assert_eq!(s.sources.to_json(), before, "a refused preference change leaves its lists intact"); + let scope = Scope::assistant(1484, Arc::new(Live)); + let Outcome::NeedsApproval(pending) = s.review(&scope, &call) else { panic!("{id} needs file approval") }; + s.run_permitted(scope, pending.allow(), &call).unwrap(); + assert_ne!(s.sources.to_json(), before, "explicit approval permits the preference change"); + } + assert!(failures.is_empty(), "{}", failures.join("; ")); +} diff --git a/crates/geolibre/src/lib.rs b/crates/geolibre/src/lib.rs index 81eb9f4737..79c197b84f 100644 --- a/crates/geolibre/src/lib.rs +++ b/crates/geolibre/src/lib.rs @@ -57,6 +57,7 @@ pub fn register() { pub fn set_runner(exe: Option) -> Result, String> { let found = runner::choose(exe)?; rustgis_analysis::reload_external_of(SOURCE); + let _ = rustgis_analysis::external_tools_of(SOURCE); Ok(found) } @@ -73,6 +74,7 @@ pub fn set_manifests(json: &str) -> Result { *m = Some(v); } rustgis_analysis::reload_external_of(SOURCE); + let _ = rustgis_analysis::external_tools_of(SOURCE); Ok(n) } @@ -129,6 +131,7 @@ struct MetaParam { /// What a run needs to know about a tool beyond its [`ToolSpec`]. #[derive(Clone, Debug)] struct Meta { + schema: &'static [ParamSpec], category: String, params: Vec, } @@ -301,7 +304,8 @@ fn spec_of(m: &Value) -> Option<(ToolSpec, Meta)> { params: Box::leak(params.into_boxed_slice()), run: rustgis_analysis::external_placeholder, }; - Some((spec, Meta { category, params: meta })) + let schema = spec.params; + Some((spec, Meta { schema, category, params: meta })) } /// `g` with every point converted by `f`, or `None` when a point can't be (rather than @@ -527,6 +531,40 @@ fn option_value(v: &Value, data: Data) -> Value { } } +/// Immutable manifest metadata paired with one published analysis schema. +#[derive(Clone)] +pub struct ResolvedTool { + spec: &'static ToolSpec, + meta: Meta, +} +impl ResolvedTool { + /// Pairs a published analysis schema with metadata from that exact manifest load. + pub fn capture(spec: &'static ToolSpec) -> Result { + let id = spec.id.strip_prefix(PREFIX).ok_or("not a GeoLibre tool")?; + let meta = + META.lock().ok().and_then(|m| m.as_ref().and_then(|m| m.get(id)).cloned()).ok_or("the GeoLibre metadata is unavailable")?; + if !std::ptr::eq(meta.schema, spec.params) { + return Err("the GeoLibre metadata and analysis schema belong to different loads".into()); + } + let pinned = Self { spec, meta }; + pinned.check()?; + Ok(pinned) + } + /// Rejects a withdrawn or replaced schema before inputs or runner arguments are prepared. + pub fn check(&self) -> Result<(), String> { + if !rustgis_analysis::published_external_tools_of(SOURCE).iter().any(|spec| std::ptr::eq(spec, self.spec)) { + return Err("the GeoLibre tool schema was reloaded".into()); + } + Ok(()) + } + /// Preparation uses captured metadata, never another lookup by tool name. + /// Uses captured manifest metadata to prepare arguments; does not resolve the tool by name. + pub fn prepare(&self, inputs: &dyn Inputs, params: &Value) -> Result { + self.check()?; + prepare_with(self.spec, &self.meta, inputs, params) + } +} + /// Prepares a run of `spec` (a GeoLibre tool) on the map's layers, with `params` already /// resolved (defaults filled, required ones present: see `rustgis_analysis::resolve_params`). pub fn prepare(spec: &ToolSpec, inputs: &dyn Inputs, params: &Value) -> Result { @@ -747,5 +785,21 @@ fn run(spec: &ToolSpec, inputs: &dyn Inputs, params: &Value) -> Result Result<(), String>, +) -> Result { + pin.check()?; + live()?; + let job = tool.prepare(inputs, params)?; + native::run_pinned(&job, pin, live) +} + #[cfg(test)] mod tests; diff --git a/crates/geolibre/src/native.rs b/crates/geolibre/src/native.rs index 1566888c2f..5a2e94b8e2 100644 --- a/crates/geolibre/src/native.rs +++ b/crates/geolibre/src/native.rs @@ -52,12 +52,19 @@ fn new_files(dir: &Path, job: &Job) -> Vec<(String, Vec)> { } pub(crate) fn run(job: &Job) -> Result { + run_pinned(job, &runner::ResolvedRunner::capture()?, &|| Ok(())) +} + +/// Stages only after the captured runner and live caller are still valid. +pub(crate) fn run_pinned(job: &Job, pin: &runner::ResolvedRunner, live: &dyn Fn() -> Result<(), String>) -> Result { + pin.check()?; + live()?; let scratch = Scratch::new()?; for (name, bytes) in &job.files { let path = scratch.0.join(name); std::fs::write(&path, bytes).map_err(|e| format!("can't write {}: {e}", path.display()))?; } - let lines = runner::run(&job.tool, &job.argv(&scratch.0.display().to_string()), &scratch.0)?; + let lines = runner::run_pinned(pin, &job.tool, &job.argv(&scratch.0.display().to_string()), &scratch.0, live)?; let files = new_files(&scratch.0, job); Ok(finish(job, &files, &lines, Some(&scratch.0))) } diff --git a/crates/geolibre/src/runner.rs b/crates/geolibre/src/runner.rs index ba3e9b3cd2..d20f5e8d98 100644 --- a/crates/geolibre/src/runner.rs +++ b/crates/geolibre/src/runner.rs @@ -25,7 +25,17 @@ pub(crate) fn exe_name() -> &'static str { static CHOSEN: Mutex> = Mutex::new(None); /// The runner found, with its version, cached until the choice changes. -static FOUND: Mutex>> = Mutex::new(None); +struct Found { + generation: u64, + value: Option>, +} +static FOUND: Mutex = Mutex::new(Found { generation: 0, value: None }); + +/// The already discovered choice, without probing programs or searching the filesystem. +/// None means unavailable or not yet explicitly discovered by trusted native code. +pub fn selected() -> Option<(PathBuf, String)> { + FOUND.lock().ok().and_then(|f| f.value.clone().flatten()) +} /// A command for the runner, without a console window on Windows. fn command(exe: &Path) -> Command { @@ -122,17 +132,19 @@ fn candidates() -> Vec { /// The answer is kept until the choice changes (Locate…). Probing runs without holding the /// cache's lock, so a slow candidate never blocks another caller. pub fn find() -> Option<(PathBuf, String)> { - if let Ok(found) = FOUND.lock() - && let Some(f) = found.as_ref() - { - return f.clone(); - } + let generation = { + let found = FOUND.lock().ok()?; + if let Some(value) = &found.value { + return value.clone(); + } + found.generation + }; let probed = candidates().into_iter().find_map(|p| version_of(&p).map(|v| (p, v))); - if let Ok(mut found) = FOUND.lock() { - // Another caller may have stored an answer meanwhile: the first one stays. - return found.get_or_insert(probed).clone(); + let mut found = FOUND.lock().ok()?; + if found.generation != generation { + return found.value.clone().flatten(); } - probed + found.value.get_or_insert(probed).clone() } /// Uses `exe` as the runner (`None`: look in the usual places again). Fails, keeping the @@ -147,7 +159,8 @@ pub fn choose(exe: Option) -> Result, String> *c = exe; } if let Ok(mut f) = FOUND.lock() { - *f = None; + f.generation = f.generation.saturating_add(1); + f.value = None; } Ok(find()) } @@ -167,12 +180,49 @@ pub fn manifests() -> Result, String> { } } +/// A chosen native runner. Captures path/version, not an attestation of executable bytes. +#[derive(Clone, PartialEq, Eq)] +pub struct ResolvedRunner { + exe: PathBuf, + version: String, +} +impl ResolvedRunner { + /// Captures the selected executable path and reported version without permitting mutation. + pub fn capture() -> Result { + let (exe, version) = selected().ok_or("the GeoLibre runner is not published; open the tools or locate the runner first")?; + Ok(Self { exe, version }) + } + /// Rejects a changed runner choice before preparing or starting another stage. + pub fn check(&self) -> Result<(), String> { + if selected().as_ref() != Some(&(self.exe.clone(), self.version.clone())) { + Err("the GeoLibre runner changed before this stage started".into()) + } else { + Ok(()) + } + } +} + /// Runs one tool in `dir` with `argv` (`--name=value` strings); returns the lines it printed. /// The error carries what the tool wrote to stderr. pub fn run(id: &str, argv: &[String], dir: &Path) -> Result, String> { - let (exe, _) = find().ok_or("the GeoLibre runner isn't installed")?; - let mut c = command(&exe); + let _ = find(); // Explicit legacy native execution may discover; pinned policy execution never does. + run_pinned(&ResolvedRunner::capture()?, id, argv, dir, &|| Ok(())) +} + +/// Checks the captured choice and caller's live predicate immediately before spawning it. +pub(crate) fn run_pinned( + pin: &ResolvedRunner, + id: &str, + argv: &[String], + dir: &Path, + live: &dyn Fn() -> Result<(), String>, +) -> Result, String> { + pin.check()?; + let exe = &pin.exe; + let mut c = command(exe); c.current_dir(dir).stdin(Stdio::null()).arg(id).args(argv); + live()?; + pin.check()?; let out = c.output().map_err(|e| format!("{}: {e}", exe.display()))?; if !out.status.success() { let err = String::from_utf8_lossy(&out.stderr); diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index 7ed3c1151e..56cd049421 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -1496,35 +1496,14 @@ impl Map { t.normalize(); } let window = self.time.as_ref().map(time::TimeSlider::window); - let current = self.time.as_ref().map(|t| t.current); for l in &mut self.layers { let Layer { time, dataset, .. } = l; if let Some(b) = time.as_mut() { b.apply(dataset, window); } } - let window = self.time.as_ref().map(time::TimeSlider::window); for t in &mut self.tiles { - // A file per date (a STAC data cube's tile): the last date in the window, or none; with - // the slider off, the last date of all, as at the end of the slider. - if !t.time_urls.is_empty() { - let at = match window { - Some((from, to)) => t.time_urls.iter().rposition(|(d, _)| *d >= from && *d < to), - None => t.time_urls.len().checked_sub(1), - }; - t.time_hidden = at.is_none(); - if let Some((d, u)) = at.and_then(|i| t.time_urls.get(i)) { - t.url.clone_from(u); - t.time_at = *d; - } - continue; - } - // Only web addresses take dates (a file's template is dropped when it is repaired). - if let Some(template) = t.time_url.as_ref().filter(|u| tiles::is_web(u)) { - // Off: the first date the slider showed, or the template's start of time. - t.url = rustgis_data::time::fill_template(template, current.unwrap_or(t.time_at)); - t.time_at = current.unwrap_or(t.time_at); - } + t.apply_time(self.time.as_ref()); } } @@ -2851,7 +2830,7 @@ impl Map { } impl Layer { - /// Classifies whole owning fields; a source serving as provenance and a path occurs once. + /// Classifies each whole owning field once; equal addresses in different fields stay distinct. pub fn output_slots(&self) -> Vec<(LayerRole, &str, bool, bool)> { let Layer { id: _, diff --git a/crates/map/src/raster.rs b/crates/map/src/raster.rs index ec5cbab55b..857d4b21a2 100644 --- a/crates/map/src/raster.rs +++ b/crates/map/src/raster.rs @@ -61,6 +61,9 @@ pub struct RasterLayer { /// A remote reload shared with render snapshots, never serialized. #[serde(skip)] opening: Option, + /// The initiating read's runtime lifetime, retained through queued application and retry. + #[serde(skip)] + read_still: Option, /// The style is the file's own look once its pixels land (a layer added before they have; /// saved with a project saved before they did, so it opens in that look still). #[serde(default, skip_serializing_if = "std::ops::Not::not")] @@ -217,6 +220,7 @@ impl RasterLayer { error: None, masked: MaskedStats::default(), opening: None, + read_still: None, restyle: false, look: None, pixels_said: false, @@ -246,6 +250,7 @@ impl RasterLayer { error: None, masked: MaskedStats::default(), opening: None, + read_still: None, restyle: false, look: None, pixels_said: false, @@ -275,6 +280,7 @@ impl RasterLayer { error: None, masked: MaskedStats::default(), opening: None, + read_still: None, restyle: false, look: None, pixels_said: false, @@ -309,6 +315,7 @@ impl RasterLayer { error: None, masked: MaskedStats::default(), opening: None, + read_still: None, restyle: true, look: None, pixels_said: false, @@ -358,6 +365,9 @@ impl RasterLayer { /// [`RasterLayer::reload`] natively). What it can't start (no address, an address that isn't a /// tile layer's, no reader here) is the caller's to say. fn begin_in_place_read(&mut self) -> Result<(), String> { + use rustgis_data::source::remote; + let still = remote::current_still(); + remote::check_still(&still)?; let Some(address) = self.path.clone() else { return Err("the raster has no source".to_string()) }; // Checked as a tile layer's address is (http(s) or s3, a host, no control characters): a // project edited by hand asks for nothing else. @@ -369,10 +379,11 @@ impl RasterLayer { let slot = Arc::new(Mutex::new(None)); let land = slot.clone(); self.opening = Some(slot); + self.read_still = still.clone(); reader( &address, Box::new(move |result| { - *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(result.map(Arc::new)); + *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(remote::check_still(&still).and(result.map(Arc::new))); crate::source::wake(); }), ); @@ -399,7 +410,13 @@ impl RasterLayer { if self.read_failure().is_none() { return; } - if let Err(e) = self.begin_in_place_read() { + use rustgis_data::source::remote; + let still = self.read_still.clone(); + if let Err(e) = remote::check_still(&still) { + self.fail(e); + return; + } + if let Err(e) = remote::with_optional_still(still, || self.begin_in_place_read()) { self.land(Err(e)); } } @@ -423,6 +440,10 @@ impl RasterLayer { if self.time_zarr.is_some() { return Ok(()); } + use rustgis_data::source::remote; + let still = remote::current_still(); + remote::check_still(&still)?; + self.read_still = still; self.opening = None; self.error = None; self.failed_at = None; @@ -458,16 +479,17 @@ impl RasterLayer { let placement = self.placement; let slot = Arc::new(Mutex::new(None)); let land = slot.clone(); + let still = self.read_still.clone(); self.data = None; self.opening = Some(slot); rustgis_data::source::remote::fetch_whole( path, Box::new(move |result| { - let result = result - .map_err(|e| match e { + let result = remote::check_still(&still) + .and(result.map_err(|e| match e { rustgis_data::source::remote::Failed::Other(message) => message, _ => "The raster could not be downloaded.".to_string(), - }) + })) .and_then(|bytes| { rustgis_data::raster::read_bytes(&name, &bytes) .and_then(|r| placed(r, placement)) @@ -510,6 +532,11 @@ impl RasterLayer { fn wait_for_read(&mut self, deadline: Instant) -> Result<(), String> { let Some(slot) = self.opening.clone() else { return Ok(()) }; loop { + if let Err(e) = rustgis_data::source::remote::check_still(&self.read_still) { + self.opening = None; + self.fail(e.clone()); + return Err(e); + } if let Some(result) = slot.lock().unwrap_or_else(|e| e.into_inner()).clone() { self.opening = None; let error = result.as_ref().err().cloned(); @@ -582,6 +609,12 @@ impl RasterLayer { /// Installs a completed remote read; each render copy can observe the same shared result. pub(crate) fn poll_reload(&mut self) -> bool { let Some(slot) = self.opening.as_ref() else { return false }; + if let Err(e) = rustgis_data::source::remote::check_still(&self.read_still) { + self.opening = None; + self.fail(e); + crate::source::another_frame(); + return false; + } let Some(result) = slot.lock().unwrap_or_else(|e| e.into_inner()).clone() else { return true }; self.opening = None; self.land(result); @@ -1567,6 +1600,7 @@ impl RasterLayer { error: _, masked: _, opening: _, + read_still: _, in_place: _, restyle: _, look: _, @@ -1600,6 +1634,39 @@ impl RasterLayer { #[cfg(test)] mod tests { + /// A completed read can wait for a render copy or the blocking consumer. Revocation + /// between callback and application must refuse both consumers of the shared slot. + #[test] + fn permission_policy_completed_raster_checks_lifetime_at_application() { + use rustgis_data::source::remote; + use std::sync::atomic::{AtomicBool, Ordering::SeqCst}; + let previous = in_place_reader().lock().unwrap().take(); + set_in_place_reader(|_, done| done(Ok(dem()))); + let mut failures = Vec::new(); + for revoke in [false, true] { + let live = Arc::new(AtomicBool::new(true)); + let flag = live.clone(); + let still: remote::Still = Arc::new(move || flag.load(SeqCst)); + let mut layer = remote::with_still(still, || RasterLayer::opening_in_place("https://raster-landing.invalid/scene.tif")); + assert!(layer.data.is_none() && layer.opening.as_ref().unwrap().lock().unwrap().is_some(), "callback completed before poll"); + let mut render_copy = layer.clone(); + live.store(!revoke, SeqCst); + assert!(!layer.poll_reload()); + if layer.data.is_some() == revoke { + failures.push(format!("render application ignored revoke={revoke}")); + } + #[cfg(not(target_arch = "wasm32"))] + { + let r = render_copy.wait_for_read(Instant::now() + web_time::Duration::from_secs(1)); + if r.is_ok() == revoke || render_copy.data.is_some() == revoke { + failures.push(format!("blocking application ignored revoke={revoke}")); + } + } + } + *in_place_reader().lock().unwrap() = previous; + assert!(failures.is_empty(), "{failures:?}"); + } + use rustgis_data::raster::{Crs, GeoTransform}; use super::*; diff --git a/crates/map/src/source.rs b/crates/map/src/source.rs index 0d4270e9ae..44a7cf9fea 100644 --- a/crates/map/src/source.rs +++ b/crates/map/src/source.rs @@ -531,10 +531,25 @@ pub struct Window { struct Pending { extent: Extent, result: Mutex>>, + still: Option, } /// A source being opened off the frame. -type Opening = Arc, String>>>>; +type Opening = Arc; +struct OpeningState { + result: Mutex, String>>>, + still: Option, +} +impl OpeningState { + /// Owns the initiating carrier before an asynchronous source open can leave this thread. + fn new() -> Opening { + Arc::new(Self { result: Mutex::new(None), still: rustgis_data::source::remote::current_still() }) + } + /// Retains the existing completion-slot locking behavior; authority stays outside its payload. + fn lock(&self) -> std::sync::LockResult, String>>>> { + self.result.lock() + } +} /// Asks the app for a frame (the waker) with nothing new to draw: what landed while the map drew /// is said a frame after (see [`wake`] for something new to draw). @@ -693,6 +708,7 @@ impl SourceLayer { /// Opens the source again (after a project loads). A source by its web address is opened off /// the frame: `Ok` while it is (see [`SourceLayer::is_pending`]), open once it has landed. pub fn open(&mut self) -> Result<(), String> { + rustgis_data::source::remote::check_still(&rustgis_data::source::remote::current_still())?; if self.reconnect_required { self.error = Some(RECONNECT_REQUIRED.into()); return Err(RECONNECT_REQUIRED.into()); @@ -780,6 +796,10 @@ impl SourceLayer { /// (or footer) off the frame, taken by a later call when it has landed. fn open_remote(&mut self, url: &str) -> Result<(), String> { if let Some(slot) = self.opening.clone() { + if let Err(e) = rustgis_data::source::remote::check_still(&slot.still) { + self.opening = None; + return self.opened(Err(e)); + } // A file that can't be read in place is downloaded whole into the same slot, by the // first copy to see it; the others wait on that download rather than start their own. let claimed = slot.lock().ok().is_some_and(|mut s| { @@ -804,9 +824,9 @@ impl SourceLayer { } match rustgis_data::source::open_url_now(url) { Ok(s) => self.opened(Ok(Arc::from(s))), - Err(e) if e.to_string().contains(rustgis_data::source::remote::READ_WHOLE) => self.open_whole(url, Arc::new(Mutex::new(None))), + Err(e) if e.to_string().contains(rustgis_data::source::remote::READ_WHOLE) => self.open_whole(url, OpeningState::new()), Err(e) if e.to_string().contains(rustgis_data::source::remote::NEED) => { - let slot: Opening = Arc::new(Mutex::new(None)); + let slot = OpeningState::new(); let land = slot.clone(); self.opening = Some(slot); rustgis_data::source::open_url( @@ -835,22 +855,25 @@ impl SourceLayer { let name = url.split(['?', '#']).next().unwrap_or(url).rsplit('/').next().unwrap_or("layer").to_string(); let table = self.table.clone(); let url = url.to_string(); - rustgis_data::source::remote::fetch_whole( - &url.clone(), - Box::new(move |r| { - let r = match r { - Ok(bytes) => rustgis_data::source::open_shared(&name, Arc::new(bytes), table.as_deref()) - .map(Arc::from) - .map_err(|e| format!("{url}: {e}")), - Err(rustgis_data::source::remote::Failed::Other(e)) => Err(format!("{url}: {e}")), - Err(e) => Err(format!("{url}: couldn't be downloaded ({e:?})")), - }; - if let Ok(mut s) = land.lock() { - *s = Some(r); - } - wake(); - }), - ); + let still = land.still.clone(); + rustgis_data::source::remote::with_optional_still(still, || { + rustgis_data::source::remote::fetch_whole( + &url.clone(), + Box::new(move |r| { + let r = match r { + Ok(bytes) => rustgis_data::source::open_shared(&name, Arc::new(bytes), table.as_deref()) + .map(Arc::from) + .map_err(|e| format!("{url}: {e}")), + Err(rustgis_data::source::remote::Failed::Other(e)) => Err(format!("{url}: {e}")), + Err(e) => Err(format!("{url}: couldn't be downloaded ({e:?})")), + }; + if let Ok(mut s) = land.lock() { + *s = Some(r); + } + wake(); + }), + ); + }); Ok(()) } @@ -1070,7 +1093,8 @@ impl SourceLayer { let full_read = self.full_read; let fields = self.window_fields; let query = self.query.clone(); - let pending = Arc::new(Pending { extent, result: Mutex::new(None) }); + let still = rustgis_data::source::remote::current_still(); + let pending = Arc::new(Pending { extent, result: Mutex::new(None), still: still.clone() }); let p = pending.clone(); // A source by its web address: the read runs as its bytes land (on the desktop and in // the browser alike), fetching what it needs in rounds. @@ -1097,14 +1121,20 @@ impl SourceLayer { return; } let job = move || { - let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - window::read_mode(h.as_ref(), &req, budget, &projection, estimate, full_read, fields, query.as_ref()) - })) - .unwrap_or_else(|_| Err(rustgis_data::DataError::Format("the read failed unexpectedly".into()))) - .map_err(|e| e.to_string()); - if let Ok(mut slot) = p.result.lock() { - *slot = Some(r); - } + rustgis_data::source::remote::with_optional_still(still.clone(), || { + let r = rustgis_data::source::remote::check_still(&still) + .and_then(|()| { + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + window::read_mode(h.as_ref(), &req, budget, &projection, estimate, full_read, fields, query.as_ref()) + })) + .unwrap_or_else(|_| Err(rustgis_data::DataError::Format("the read failed unexpectedly".into()))) + .map_err(|e| e.to_string()) + }) + .and_then(|r| rustgis_data::source::remote::check_still(&still).map(|()| r)); + if let Ok(mut slot) = p.result.lock() { + *slot = Some(r); + } + }) }; #[cfg(not(target_arch = "wasm32"))] if !block { @@ -1140,7 +1170,7 @@ impl SourceLayer { let Some(p) = self.pending.clone() else { return Polled::Idle }; let start = web_time::Instant::now(); loop { - let done = p.result.lock().ok().and_then(|mut r| r.take()); + let done = p.result.lock().ok().and_then(|mut r| r.take()).map(|r| rustgis_data::source::remote::check_still(&p.still).and(r)); match done { Some(Ok((b, cache))) => { self.pending = None; @@ -1324,6 +1354,61 @@ impl SourceLayer { #[cfg(test)] mod tests { + /// A later frame's whole-file fallback owns the opening's carrier through send and landing. + #[test] + fn permission_policy_opening_fallback_retains_owner_outside_ambient_context() { + use rustgis_data::source::remote; + use std::sync::{ + Arc, Mutex, + atomic::{AtomicBool, AtomicUsize, Ordering}, + }; + for (owned, revoke) in [(true, false), (true, true), (false, false)] { + let live = Arc::new(AtomicBool::new(true)); + let check = live.clone(); + let still = owned.then(|| Arc::new(move || check.load(Ordering::SeqCst)) as remote::Still); + let held = Arc::new(Mutex::new(None)); + let hold = held.clone(); + let sends = Arc::new(AtomicUsize::new(0)); + let count = sends.clone(); + let ranges = Arc::new(AtomicUsize::new(0)); + let range_count = ranges.clone(); + let url = format!("https://opening-fallback.invalid/{owned}-{revoke}.fgb"); + remote::forget_file(&url); + remote::set_fetcher(move |_, want, done| match want { + remote::Want::Whole => { + let context = remote::current_still(); + assert_eq!(context.is_some(), owned, "the actual later-frame fallback must re-enter its saved carrier"); + *hold.lock().unwrap() = Some((context, done)); + } + _ => { + range_count.fetch_add(1, Ordering::SeqCst); + done(Err(remote::Failed::NoRanges)); + } + }); + let mut source = super::SourceLayer { path: Some(url.clone()), ..Default::default() }; + remote::with_optional_still(still, || source.open()).unwrap(); + assert_eq!(ranges.load(Ordering::SeqCst), 1, "actual opening tried a range"); + assert!(source.opening.is_some()); + assert!(remote::current_still().is_none()); + // The next frame starts outside the original synchronous scope. + source.open().unwrap(); + let (context, done) = held.lock().unwrap().take().unwrap(); + live.store(!revoke, Ordering::SeqCst); + let mut ds = rustgis_data::Dataset::new("fallback", rustgis_data::GeometryType::Point, vec![]); + ds.push_next(Some(rustgis_data::Geometry::Point(rustgis_geom::Coord::new(0., 0.))), vec![]); + let result = remote::check_still(&context).map_err(remote::Failed::Other).map(|()| { + count.fetch_add(1, Ordering::SeqCst); + rustgis_data::source::fgb::write(&ds).unwrap() + }); + done(result); + assert_eq!(source.open().is_err(), revoke); + assert_eq!(source.is_open(), !revoke, "a revoked landing must never install the source"); + assert_eq!(sends.load(Ordering::SeqCst), usize::from(!revoke)); + assert!(remote::current_still().is_none()); + remote::forget_file(&url); + } + } + /// Reopening this same layer must invalidate populated decisions even without `reset`. #[test] fn reopening_a_populated_thinning_cache_uses_the_new_handle() { diff --git a/crates/map/src/tiles.rs b/crates/map/src/tiles.rs index 77e5069938..79f8f910aa 100644 --- a/crates/map/src/tiles.rs +++ b/crates/map/src/tiles.rs @@ -431,6 +431,30 @@ impl TileLayer { self.visible && !self.time_hidden && self.error.is_none() } + /// Selects this tile's date and address from a normalized slider, without fetching it. + /// With no slider, a dated list shows its last entry and a template keeps its saved date. + pub fn apply_time(&mut self, time: Option<&crate::time::TimeSlider>) { + let window = time.map(crate::time::TimeSlider::window); + let current = time.map(|t| t.current); + if !self.time_urls.is_empty() { + let at = match window { + Some((from, to)) => self.time_urls.iter().rposition(|(d, _)| *d >= from && *d < to), + None => self.time_urls.len().checked_sub(1), + }; + self.time_hidden = at.is_none(); + if let Some((d, u)) = at.and_then(|i| self.time_urls.get(i)) { + self.url.clone_from(u); + self.time_at = *d; + } + return; + } + // Only web addresses take dates (a file's template is dropped when it is repaired). + if let Some(template) = self.time_url.as_ref().filter(|u| is_web(u)) { + self.url = rustgis_data::time::fill_template(template, current.unwrap_or(self.time_at)); + self.time_at = current.unwrap_or(self.time_at); + } + } + /// Checks the address and brings the numbers into range, after reading one from a project /// file or a command. pub fn repair(&mut self) -> Result<(), String> { diff --git a/crates/plugins/src/lib.rs b/crates/plugins/src/lib.rs index 72d0c50016..24970a5389 100644 --- a/crates/plugins/src/lib.rs +++ b/crates/plugins/src/lib.rs @@ -178,6 +178,17 @@ pub fn get(id: &str) -> Option> { PLUGINS.read().ok()?.iter().find(|p| p.manifest.id == id).cloned() } +/// Whether this exact load is still enabled and not withdrawn. Reloading, toggling or +/// removing a plugin invalidates old handles even if its id and package bytes are unchanged. +/// An unavailable plugin or blocklist lock refuses the handle. +pub fn is_current(plugin: &Arc) -> bool { + plugin.enabled + && PLUGINS.read().is_ok_and(|v| v.iter().any(|p| p.enabled && Arc::ptr_eq(p, plugin))) + && [&BLOCKED, &BLOCKED_EXTRA] + .iter() + .all(|list| list.read().is_ok_and(|v| registry::blocked(&v, &plugin.manifest.id, &plugin.sha256).is_none())) +} + /// `plugin:/`. pub fn command_id(plugin: &str, item: &str) -> String { format!("{PREFIX}{plugin}/{item}") diff --git a/crates/plugins/src/testing.rs b/crates/plugins/src/testing.rs index e1554a1ef1..ed5e7602c7 100644 --- a/crates/plugins/src/testing.rs +++ b/crates/plugins/src/testing.rs @@ -305,7 +305,14 @@ pub fn serve_if_child() { let _ = out.flush(); answer.clear(); let _ = input.read_line(&mut answer); - serde_json::from_str(&answer).unwrap_or_else(|_| json!({"error": "no answer"})) + let reply: Value = serde_json::from_str(&answer).unwrap_or_else(|_| json!({"error": "no answer"})); + if params.get("fail").and_then(Value::as_bool) == Some(true) + && reply.get("ok").and_then(|v| v.get("pending")).and_then(Value::as_bool) == Some(true) + { + json!({"error":"nope"}) + } else { + reply + } } Some('n') => { let _ = writeln!(out, "a library printing"); @@ -349,6 +356,16 @@ pub fn serve_if_child() { } } Some('m') => { + // Hold an actual tool request for cancellation tests; ordinary calls do not wait. + if let (Some(ready), Some(release)) = + (params.get("control_ready").and_then(Value::as_str), params.get("control_release").and_then(Value::as_str)) + { + let _ = std::fs::write(ready, b"started"); + let start = std::time::Instant::now(); + while !std::path::Path::new(release).is_file() && start.elapsed() < std::time::Duration::from_secs(20) { + std::thread::sleep(std::time::Duration::from_millis(5)); + } + } let _ = writeln!(out, "{}", json!({"progress": "making"})); json!({"ok": {"layers": [{"name": "Made", "geojson": {"type": "FeatureCollection", "features": [ {"type": "Feature", "properties": {"n": 1}, "geometry": {"type": "Point", "coordinates": [1, 2]}}]}}], "messages": ["made"]}}) diff --git a/crates/plugins/src/tests.rs b/crates/plugins/src/tests.rs index 2c3db15a61..be87984cde 100644 --- a/crates/plugins/src/tests.rs +++ b/crates/plugins/src/tests.rs @@ -250,6 +250,92 @@ fn tools_join_the_geoprocessing_tools() { crate::unload("unit-tools"); } +/// Old prepared tool jobs cannot run after their plugin is toggled, replaced or removed. +#[test] +fn prepared_tools_keep_their_load_and_refuse_revoked_handles() { + struct Empty; + impl rustgis_analysis::Inputs for Empty { + /// The fixed-output plugin needs no project dataset. + fn dataset(&self, _: &str) -> Option<&rustgis_data::Dataset> { + None + } + /// The fixture gives the plugin no selected rows. + fn selection(&self, _: &str) -> &[usize] { + &[] + } + } + + let plugin = load("unit-pinned-tool", &[]); + let job = crate::tools::prepare_pinned(&plugin, "m", &Empty, &json!({})).unwrap(); + assert!(crate::is_current(&plugin)); + assert_eq!(job.run(&crate::tools::Control::default()).unwrap().messages, ["made"]); + + crate::set_enabled("unit-pinned-tool", false).unwrap(); + assert!(!crate::is_current(&plugin)); + assert!(job.run(&crate::tools::Control::default()).unwrap_err().contains("changed or was disabled")); + crate::set_enabled("unit-pinned-tool", true).unwrap(); + let enabled = crate::get("unit-pinned-tool").unwrap(); + assert!(crate::is_current(&enabled)); + assert!(!crate::is_current(&plugin), "enabling cannot revive an old load"); + let job = crate::tools::prepare_pinned(&enabled, "m", &Empty, &json!({})).unwrap(); + + let replacement = load("unit-pinned-tool", &[]); + assert!(crate::is_current(&replacement)); + assert!(!crate::is_current(&enabled), "identical package bytes still make a new load"); + assert!(job.run(&crate::tools::Control::default()).is_err()); + let err = crate::tools::prepare_pinned(&enabled, "m", &Empty, &json!({"input": "Missing"})).err().unwrap(); + assert!(err.contains("updated or removed"), "refuse before reading inputs: {err}"); + let job = crate::tools::prepare_pinned(&replacement, "m", &Empty, &json!({})).unwrap(); + crate::unload("unit-pinned-tool"); + assert!(!crate::is_current(&replacement)); + assert!(job.run(&crate::tools::Control::default()).is_err()); + + let withdrawn = load("unit-pinned-withdrawn", &[]); + let job = crate::tools::prepare_pinned(&withdrawn, "m", &Empty, &json!({})).unwrap(); + crate::apply_blocklist( + "https://pinned-test.invalid/registry.json", + vec![registry::Blocked { id: "unit-pinned-withdrawn".into(), sha256: None, reason: "test withdrawal".into() }], + ); + assert!(!crate::is_current(&withdrawn)); + assert!(job.run(&crate::tools::Control::default()).is_err()); +} + +/// A schema captured from one registry snapshot cannot be paired with another plugin load. +#[test] +fn pinned_tool_schema_refuses_changed_defaults_display_and_stale_load() { + // This schema-comparison scenario holds its own publication; other fixtures reload the + // process-wide provider, which may legitimately make a concurrent lookup unavailable. + #[cfg(not(target_arch = "wasm32"))] + if std::env::var_os("RUSTGIS_SCHEMA_PIN_TEST_CHILD").is_none() { + let out = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "tests::pinned_tool_schema_refuses_changed_defaults_display_and_stale_load", "--nocapture"]) + .env("RUSTGIS_SCHEMA_PIN_TEST_CHILD", "1") + .output() + .unwrap(); + assert!(out.status.success(), "{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr)); + assert!(String::from_utf8_lossy(&out.stdout).contains("test result: ok. 1 passed; 0 failed;")); + return; + } + crate::tools::register(); + let first = load("unit-schema-pin", &[]); + let spec = rustgis_analysis::resolve("plugin:unit-schema-pin/m").unwrap(); + crate::tools::matches_pinned(&first, spec.spec()).unwrap(); + let mut manifest: Value = serde_json::from_str(&testing::manifest("unit-schema-pin", &[])).unwrap(); + manifest["tools"][0]["name"] = json!("Different operation"); + manifest["tools"][0]["params"][1]["default"] = json!(42); + let text = manifest.to_string(); + let bytes = testing::zip_of(&[("plugin.json", text.as_bytes()), ("plugin.wasm", &testing::wasm())]); + let second = crate::load(Package::from_zip(&bytes).unwrap(), "test").unwrap(); + assert!(crate::tools::matches_pinned(&first, spec.spec()).unwrap_err().contains("updated or removed")); + assert!(crate::tools::matches_pinned(&second, spec.spec()).unwrap_err().contains("schema or display")); + let current = rustgis_analysis::resolve("plugin:unit-schema-pin/m").unwrap(); + crate::tools::matches_pinned(&second, current.spec()).unwrap(); + assert_eq!(current.resolve_params(&json!({"input":"Cities"})).unwrap()["size"], 42); + crate::set_enabled("unit-schema-pin", false).unwrap(); + assert!(crate::tools::matches_pinned(&second, current.spec()).is_err()); + crate::unload("unit-schema-pin"); +} + /// Registry and blocklist parsing validates entries and resolves package addresses safely. #[test] fn registries_and_blocklists_are_read_defensively() { @@ -1026,9 +1112,9 @@ fn process_plugins_that_hang_are_stopped() { #[test] fn process_tools_show_progress_and_can_be_cancelled() { let dir = process_folder("proc-cancel", &[]); - let p = crate::prepare(Package::from_dir(&dir).unwrap(), "test").unwrap(); + let p = crate::load(Package::from_dir(&dir).unwrap(), "test").unwrap(); let job = crate::tools::Job { - plugin: std::sync::Arc::new(p), + plugin: p.clone(), name: "Work Forever".into(), request: json!({"abi": 1, "kind": "tool", "id": "w", "params": {}, "layers": {}, "lang": "en"}), }; @@ -1051,13 +1137,14 @@ fn process_tools_show_progress_and_can_be_cancelled() { assert!(e == "Work Forever: cancelled" && t0.elapsed() < std::time::Duration::from_secs(10), "{e}"); // Progress shows while a tool runs, but isn't kept in its messages as log lines are. let job = crate::tools::Job { - plugin: std::sync::Arc::new(crate::prepare(Package::from_dir(&dir).unwrap(), "test").unwrap()), + plugin: p, name: "Make Points".into(), request: json!({"abi": 1, "kind": "tool", "id": "m", "params": {}, "layers": {}, "lang": "en"}), }; let control = crate::tools::Control::default(); assert_eq!(job.run(&control).unwrap().messages, vec!["made"]); assert_eq!(control.last_log().as_deref(), Some("making")); + crate::unload("proc-cancel"); let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/plugins/src/tools.rs b/crates/plugins/src/tools.rs index 4a48478c2a..b61dd40f4d 100644 --- a/crates/plugins/src/tools.rs +++ b/crates/plugins/src/tools.rs @@ -31,6 +31,7 @@ pub fn register() { /// Rebuilds the tool list the next time it's needed (a plugin was loaded, removed or toggled). pub(crate) fn reload() { rustgis_analysis::reload_external_of(SOURCE); + let _ = rustgis_analysis::external_tools_of(SOURCE); } /// A `'static` copy of `s`, shared, so reloading the list doesn't leak another copy of every @@ -96,6 +97,29 @@ fn params_of(sha256: &str, t: &crate::manifest::ToolDecl) -> &'static [ParamSpec made } +/// Verifies that a separately captured analysis specification belongs to this exact load's +/// manifest. The interned parameter owner is shared with registry publication and preparation; +/// no policy layer reconstructs defaults or accepts an id/hash as schema equality. +pub fn matches_pinned(plugin: &Arc, spec: &ToolSpec) -> Result<(), String> { + if !crate::is_current(plugin) { + return Err("the plugin was disabled, updated or removed before its schema was pinned".into()); + } + let (owner, id) = crate::split_id(spec.id).ok_or("not a plugin tool id")?; + if owner != plugin.manifest.id { + return Err("the tool belongs to another plugin".into()); + } + let decl = plugin.manifest.tools.iter().find(|t| t.id == id).ok_or("the pinned plugin has no such tool")?; + let toolbox = if decl.toolbox.trim().is_empty() { &plugin.manifest.name } else { &decl.toolbox }; + if spec.name != decl.name + || spec.summary != decl.summary + || spec.toolbox != format!("Plugins › {toolbox}") + || !std::ptr::eq(spec.params, params_of(&plugin.sha256, decl)) + { + return Err("the plugin tool's schema or display metadata changed before it was pinned".into()); + } + Ok(()) +} + fn specs() -> Vec { let mut out = Vec::new(); for p in crate::list().into_iter().filter(|p| p.enabled) { @@ -162,7 +186,8 @@ impl Host for Messages<'_> { } } fn cancelled(&self) -> bool { - self.control.is_cancelled() + // An analysis caller carries its cancellation through the existing data progress probe. + self.control.is_cancelled() || rustgis_data::progress::check().is_err() } } @@ -203,6 +228,9 @@ pub struct Job { impl Job { /// Runs the tool (blocking until it ends), and reads what it returns. pub fn run(&self, control: &Control) -> Result { + if !crate::is_current(&self.plugin) { + return Err(format!("{} changed or was disabled since this run was prepared", self.plugin.manifest.name)); + } let mut log = Messages { lines: Vec::new(), control }; let reply = crate::call(&self.plugin, &mut log, &self.request, true); // A WebAssembly tool isn't stopped by a cancel; what it returns then is dropped. @@ -227,6 +255,19 @@ impl Job { pub fn prepare(spec: &ToolSpec, inputs: &dyn Inputs, params: &Value) -> Result { let (pid, tid) = split_id(spec.id).ok_or("not a plugin tool")?; let plugin = crate::get(pid).filter(|p| p.enabled).ok_or_else(|| format!("the plugin '{pid}' isn't loaded"))?; + prepare_pinned(&plugin, tid, inputs, params) +} + +/// Prepares the tool from this exact plugin load's manifest, without substituting a newly +/// loaded plugin with the same id. Callers pass parameters resolved against the pinned +/// schema. A stale load is refused before layers or file parameters are read. +pub fn prepare_pinned(plugin: &Arc, tid: &str, inputs: &dyn Inputs, params: &Value) -> Result { + if !plugin.enabled { + return Err(format!("the plugin '{}' is disabled", plugin.manifest.id)); + } + if !crate::is_current(plugin) { + return Err(format!("{} was updated or removed since this run was approved", plugin.manifest.name)); + } let decl = plugin.manifest.tools.iter().find(|t| t.id == tid).ok_or_else(|| format!("{} has no tool '{tid}'", plugin.manifest.name))?; let mut layers = Map::new(); for p in decl.params.iter().filter(|p| is_layer_kind(&p.kind)) { @@ -237,7 +278,7 @@ pub fn prepare(spec: &ToolSpec, inputs: &dyn Inputs, params: &Value) -> Result { - let mi = maps.iter().position(|m| m.name.eq_ignore_ascii_case(map))?; + let mi = map_index(maps, map)?; let m = maps.get(mi)?; Some((e.id, mi, frame_camera(m, *center, *scale, *bearing, *pitch), e.w * PT_PER_MM, e.h * PT_PER_MM)) } @@ -2042,6 +2042,11 @@ fn chart(pm: &mut Canvas, r: Rect, ds: &rustgis_data::Dataset, field: &str, labe } } +/// The first case-insensitive frame-map name match; a missing reference renders no map. +pub fn map_index(maps: &[rustgis_map::Map], name: &str) -> Option { + maps.iter().position(|map| map.name.eq_ignore_ascii_case(name)) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/tiles/src/arcgis_auth.rs b/crates/tiles/src/arcgis_auth.rs index 24795839a7..f7bc2fb0b6 100644 --- a/crates/tiles/src/arcgis_auth.rs +++ b/crates/tiles/src/arcgis_auth.rs @@ -371,6 +371,76 @@ struct Session { retry_at: i64, } +/// An immutable, exact runtime account snapshot, never an output representation. It contains +/// credentials and deliberately has no formatting, serialization or raw-value accessors. +/// Renewal that rotates credentials invalidates it just like replacing a token does. +#[derive(Clone, PartialEq, Eq)] +pub struct AccountIdentity { + url: Option, + accounts: Vec, +} + +#[derive(Clone, PartialEq, Eq)] +struct Account { + portal: String, + client_id: String, + username: String, + access_token: String, + expires_at: i64, + refresh_token: Option, + scopes: Vec, +} + +impl From<&Session> for Account { + fn from(s: &Session) -> Self { + Self { + portal: s.portal.clone(), + client_id: s.client_id.clone(), + username: s.username.clone(), + access_token: s.access_token.clone(), + expires_at: s.expires_at, + refresh_token: s.refresh_token.clone(), + scopes: s.scopes.clone(), + } + } +} + +impl AccountIdentity { + /// Compares only active in-memory accounts. Never restores, renews or performs I/O; + /// an unavailable or poisoned store returns false. + pub fn is_current(&self) -> bool { + identity(self.url.as_deref()).is_ok_and(|current| *self == current) + } +} + +fn identity(url: Option<&str>) -> Result { + identity_in(store(), url) +} + +fn identity_in(store: &Mutex>, url: Option<&str>) -> Result { + let all = store.lock().map_err(|_| "active account store unavailable".to_string())?; + let accounts = match url { + Some(url) => covering(&all, url).and_then(|i| all.get(i)).map(Account::from).into_iter().collect(), + None => all.iter().map(Account::from).collect(), + }; + Ok(AccountIdentity { url: url.map(str::to_string), accounts }) +} + +/// Snapshots all active runtime accounts in selection order, without I/O. Use when the +/// request's eventual addresses cannot yet be scoped; unrelated account changes invalidate it. +/// An unavailable or poisoned store is an error, never an empty identity. +pub fn account_identity() -> Result { + identity(None) +} + +/// Snapshots the account selected for this address, including its absence. Changes to an +/// unrelated account do not invalidate it; a newly covering account or rotation does. +/// Expiry and retry scheduling are not executed by this pure read. +/// An unavailable or poisoned store is an error, never an absent account. +pub fn account_identity_for(url: &str) -> Result { + identity(Some(url)) +} + fn store() -> &'static Mutex> { static S: std::sync::OnceLock>> = std::sync::OnceLock::new(); S.get_or_init(Mutex::default) @@ -818,12 +888,17 @@ mod native { /// POSTs a form; a refusal's answer (an OAuth `error`, often with HTTP 400) is returned for /// the caller to read, while a server error (5xx) counts as the portal not being reached. fn post_form(url: &str, body: &str) -> Result, String> { - let resp = agent(true) - .post(url) - .header("Content-Type", "application/x-www-form-urlencoded") - .header("Accept", "application/json") - .send(body) - .map_err(|e| format!("couldn't reach the portal: {e}"))?; + post_form_claimed(url, body, &None) + } + + /// Checks a download's owned predicate after request preparation, directly at renewal send. + /// Interactive sign-in and sign-out use `post_form` without a download predicate. + fn post_form_claimed(url: &str, body: &str, still: &Option) -> Result, String> { + let resp = send_form( + still, + || agent(true).post(url).header("Content-Type", "application/x-www-form-urlencoded").header("Accept", "application/json"), + |req| req.send(body).map_err(|e| format!("couldn't reach the portal: {e}")), + )?; let status = resp.status().as_u16(); if status >= 500 { return Err(format!("the portal answered HTTP {status}")); @@ -831,6 +906,17 @@ mod native { resp.into_body().with_config().limit(1 << 20).read_to_vec().map_err(|e| e.to_string()) } + /// Request preparation (including TLS setup) can wait; the final send has its own check. + fn send_form( + still: &Option, + prepare: impl FnOnce() -> T, + send: impl FnOnce(T) -> Result, + ) -> Result { + let prepared = prepare(); + crate::cache::check_still(still)?; + send(prepared) + } + fn get_json(url: &str, token: &str) -> Result, String> { let resp = agent(false) .get(url) @@ -1008,12 +1094,29 @@ mod native { /// Like [`authorization`](super::authorization), renewing the access token first when it is /// about to expire. A refused renewal ends the session; an unreachable portal keeps it. pub fn authorization_fresh(url: &str) -> Option { + authorization_fresh_using(url, crate::cache::current_still(), post_form_claimed) + } + + fn renewing() -> &'static Mutex<()> { + static RENEWING: Mutex<()> = Mutex::new(()); + &RENEWING + } + + /// The same owned predicate crosses the renewal lock and reaches the actual sender. + fn authorization_fresh_using( + url: &str, + still: Option, + post: impl FnOnce(&str, &str, &Option) -> Result, String>, + ) -> Option { + crate::cache::check_still(&still).ok()?; if super::renewal_due(url).is_some() { // One renewal at a time: the others wait for it, then find the token fresh. - static RENEWING: Mutex<()> = Mutex::new(()); - let _one = RENEWING.lock().unwrap_or_else(|e| e.into_inner()); + let _one = renewing().lock().unwrap_or_else(|e| e.into_inner()); + crate::cache::check_still(&still).ok()?; if let Some(r) = super::renewal_due(url) { - let answer = post_form(&r.endpoint, &r.body); + crate::cache::check_still(&still).ok()?; + let answer = post(&r.endpoint, &r.body, &still); + crate::cache::check_still(&still).ok()?; super::renewal_done(&r, answer.as_deref().map_err(String::clone)); } } @@ -1080,6 +1183,149 @@ mod native { m = escape(text) ) } + + #[cfg(test)] + mod renewal_tests { + use super::*; + + /// Revocation during synchronous request preparation is checked at the actual sender. + #[test] + fn permission_transport_renewal_rechecks_immediately_before_post() { + use std::sync::{ + Arc, + atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst}, + mpsc::channel, + }; + let live = Arc::new(AtomicBool::new(true)); + let flag = live.clone(); + let still: crate::cache::Still = Arc::new(move || flag.load(SeqCst)); + let sends = Arc::new(AtomicUsize::new(0)); + let posted = sends.clone(); + let (entered, entry) = channel(); + let (release, released) = channel(); + let worker = std::thread::spawn(move || { + send_form( + &Some(still), + || { + entered.send(()).unwrap(); + released.recv().unwrap(); + }, + |()| { + posted.fetch_add(1, SeqCst); + Ok(()) + }, + ) + }); + entry.recv_timeout(Duration::from_secs(10)).unwrap(); + live.store(false, SeqCst); + release.send(()).unwrap(); + assert!(worker.join().unwrap().unwrap_err().contains("cancelled")); + assert_eq!(sends.load(SeqCst), 0); + } + + /// Renewal is permitted while current, but a rotated credential needs fresh consent. + #[test] + fn permission_transport_renewal_rotation_invalidates_old_identity_only() { + use crate::{arcgis_auth as auth, cache}; + let portal = "https://rotation.example.org/portal"; + let url = format!("{portal}/sharing/rest/content/items/a"); + let due = || { + auth::sign_in( + portal, + "client", + auth::Grant { + access_token: "synthetic-old".into(), + refresh_token: Some("synthetic-refresh".into()), + expires_at: auth::now_secs() + 20, + username: "test".into(), + }, + auth::scopes_for(portal, None, &[]), + ) + }; + let answer = br#"{"access_token":"synthetic-new","refresh_token":"synthetic-rotated","expires_in":3600}"#; + due(); + let identity = auth::account_identity_for(&url).unwrap(); + let still: cache::Still = std::sync::Arc::new(move || identity.is_current()); + let token = authorization_fresh_using(&url, Some(still.clone()), |_, _, owned| { + assert!(std::sync::Arc::ptr_eq(owned.as_ref().unwrap(), &still)); + cache::check_still(owned).unwrap(); + Ok(answer.to_vec()) + }); + assert_eq!(token.as_deref(), Some("Bearer synthetic-new")); + assert!(cache::check_still(&Some(still)).is_err(), "the final GET must refuse old identity"); + let fresh = auth::account_identity_for(&url).unwrap(); + let fresh: cache::Still = std::sync::Arc::new(move || fresh.is_current()); + assert!(cache::check_still(&Some(fresh.clone())).is_ok()); + assert!(authorization_fresh_using(&url, Some(fresh), |_, _, _| panic!("already renewed")).is_some()); + // Ordinary unclaimed User requests retain renewal behavior. + due(); + assert_eq!( + authorization_fresh_using(&url, None, |_, _, owned| { + assert!(owned.is_none()); + Ok(answer.to_vec()) + }) + .as_deref(), + Some("Bearer synthetic-new") + ); + auth::sign_out(Some(portal)); + } + + /// A real renewal waits behind preparation; revocation prevents its POST and the GET. + #[test] + fn permission_transport_renewal_rechecks_after_waiting_for_preparation() { + use std::sync::{ + Arc, + atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst}, + mpsc::channel, + }; + let portal = "https://held-renewal.example.org/portal"; + let url = format!("{portal}/sharing/rest/content/items/a"); + super::super::sign_in( + portal, + "client", + super::super::Grant { + access_token: "synthetic-old".into(), + refresh_token: Some("synthetic-refresh".into()), + expires_at: super::super::now_secs() + 20, + username: "test".into(), + }, + super::super::scopes_for(portal, None, &[]), + ); + let one = renewing().lock().unwrap(); + let live = Arc::new(AtomicBool::new(true)); + let sends = Arc::new(AtomicUsize::new(0)); + let (entered, entry) = channel(); + let flag = live.clone(); + let first = AtomicBool::new(true); + let still: crate::cache::Still = Arc::new(move || { + let allowed = flag.load(SeqCst); + if first.swap(false, SeqCst) { + entered.send(()).unwrap(); + } + allowed + }); + let posted = sends.clone(); + let worker = std::thread::spawn(move || { + crate::cache::with_still(still, || { + let auth = authorization_fresh_using(&url, crate::cache::current_still(), |_, _, _| { + posted.fetch_add(1, SeqCst); + Ok(br#"{"access_token":"synthetic-new","expires_in":3600}"#.to_vec()) + }); + assert!(auth.is_none()); + // The native GET's actual-send check still rejects the same initiating claim. + if crate::cache::check_still(&crate::cache::current_still()).is_ok() { + posted.fetch_add(1, SeqCst); + } + }) + }); + entry.recv_timeout(Duration::from_secs(10)).unwrap(); + live.store(false, SeqCst); + drop(one); + worker.join().unwrap(); + assert_eq!(sends.load(SeqCst), 0); + super::super::sign_out(Some(portal)); + } + } } #[cfg(test)] diff --git a/crates/tiles/src/arcgis_auth/tests.rs b/crates/tiles/src/arcgis_auth/tests.rs index 33c4d5280d..51eb23d95d 100644 --- a/crates/tiles/src/arcgis_auth/tests.rs +++ b/crates/tiles/src/arcgis_auth/tests.rs @@ -6,6 +6,55 @@ fn grant(token: &str, expires_at: i64) -> Grant { Grant { access_token: token.into(), refresh_token: Some("refresh".into()), expires_at, username: "ana".into() } } +/// Raw account identity stays in memory and follows the effective selection, including absence. +#[test] +fn permission_transport_account_identity_binds_exact_selected_credentials() { + fn send_sync() {} + send_sync::(); + let portal = "https://identity.example.org/portal"; + let url = "https://identity.example.org/server/rest/services/A/MapServer"; + let absent = account_identity_for(url).unwrap(); + assert!(absent.is_current()); + sign_in(portal, "client", grant("synthetic-a", now_secs() + 3600), scopes_for(portal, None, &[])); + assert!(!absent.is_current()); + let selected = account_identity_for(url).unwrap(); + assert!(selected.is_current()); + assert!(selected == account_identity_for(url).unwrap()); + // A different store entry doesn't affect this request's selected account. + add_token("https://unrelated-identity.example.org/rest/services/A/MapServer", "synthetic-other").unwrap(); + assert!(selected.is_current()); + // A more specific account replaces the effective credential behind the same public URL. + add_token(url, "synthetic-server").unwrap(); + assert!(!selected.is_current()); + let first = account_identity_for(url).unwrap(); + add_token(url, "synthetic-replacement").unwrap(); + assert!(!first.is_current()); + let replaced = account_identity_for(url).unwrap(); + assert!(replaced.is_current() && replaced != first); + // Removing a selected account also changes identity; credentials never come from a label. + sign_out(Some("https://identity.example.org/server")); + assert!(!replaced.is_current()); + sign_out(Some(portal)); + sign_out(Some("https://unrelated-identity.example.org")); +} + +/// A poisoned active store must never turn into a normal empty snapshot. +#[test] +fn permission_transport_account_capture_fails_closed() { + let store = std::sync::Arc::new(Mutex::new(Vec::new())); + let worker = store.clone(); + assert!( + std::thread::spawn(move || { + let _locked = worker.lock().unwrap(); + panic!("synthetic poison"); + }) + .join() + .is_err() + ); + assert!(identity_in(&store, None).is_err()); + assert!(identity_in(&store, Some("https://identity.example.org/rest/services/A")).is_err()); +} + #[test] fn portal_bases() { assert_eq!(portal_base("").unwrap(), ONLINE); diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index 1e404fbbf7..766269805c 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -54,6 +54,31 @@ pub enum State { pub type Done = Box, String>) + Send>; /// Called once with a download's bytes or what is known of its failure (#509). pub type DoneDetailed = Box, Failure>) + Send>; +// The carrier lives with source I/O so lower-layer workers inherit the same context. +pub use rustgis_data::source::remote::{Still, check_still, current_still, with_optional_still, with_still}; +fn check_current() -> Result<(), String> { + check_still(¤t_still()) +} + +/// Only a live request's failure describes the shared resource. A dropped or retired read +/// must let the next caller retry immediately under its own authority, without classifying cancellation by error text. +pub(crate) fn keep_failure(message: &str, still: &Option) -> bool { + message != DROPPED && check_still(still).is_ok() +} + +/// Shared cache callbacks retain their caller across ready hits and loading waiters. A loader +/// may narrow that lifetime, but cannot replace it when the callback starts another read. +pub(crate) fn caller_callback( + then: Box) + Send>, +) -> Box) + Send> { + let still = current_still(); + Box::new(move |result| { + with_optional_still(still, || { + then(check_still(¤t_still()).and(result)); + }); + }) +} + type Fetcher = Box; /// Hands back a decoded picture: [`RAW`] pixels, or the encoded bytes it was given when it /// couldn't decode them (they are decoded here then). @@ -556,8 +581,8 @@ pub fn wait_for_news_unless(since: u64, timeout: Duration, stop: impl Fn() -> bo let _ = (since, timeout, stop); } -/// A waiting fetch: what to decode it as, whose diagnostics it goes to, and its expected statuses. -type Job = (Want, Capture, &'static [u16]); +/// A waiting fetch: its decode kind, diagnostics owner, expected statuses and initiating context. +type Job = (Want, Capture, &'static [u16], Option); fn queue() -> &'static Mutex> { static Q: OnceLock>> = OnceLock::new(); @@ -567,13 +592,14 @@ fn queue() -> &'static Mutex> { /// Asks for a tile: fetched now when its server has room, else it waits its turn. fn start(url: String, want: Want, omit: &'static [u16]) { let capture = Capture::current(); + let still = current_still(); IN_FLIGHT.fetch_add(1, Ordering::Relaxed); let mut swept = Vec::new(); let now = { let mut q = queue().lock().unwrap_or_else(|e| e.into_inner()); let mut c = lock(); let latest = c.latest.unwrap_or_else(Instant::now); - let now = q.push(url, (want, capture, omit), |u| c.slots.get(u).map(|s| s.asked), latest, &mut swept); + let now = q.push(url, (want, capture, omit, still), |u| c.slots.get(u).map(|s| s.asked), latest, &mut swept); // Under the same lock as the choice: a tile asked for again meanwhile isn't forgotten. forget_dropped(&mut c, &swept); now @@ -632,9 +658,9 @@ fn launch(jobs: Vec>) { } let _lower = Lower; while let Some(w) = pop() { - let (want, capture, omit) = w.job; + let (want, capture, omit, still) = w.job; let _scope = capture.enter(); - fetch_tile(w.url, w.host, want, omit); + with_optional_still(still, || fetch_tile(w.url, w.host, want, omit)); } } // A tile queued after the last `pop` and before the flag went down is started here. @@ -677,9 +703,15 @@ impl Drop for Answer { } } +/// Retains the initiating lifetime through deferred decoding. Retired results are forgotten; +/// only live successes and resource failures are shared with later callers. fn fetch_tile(url: String, host: String, want: Want, omit: &'static [u16]) { + let owner = current_still(); let answer = Answer { key: url.clone(), host, entry: None }; let done: Done = Box::new(move |bytes| { + // Shared archive reads can answer under another loader's narrower lifetime. Retain + // that floor with this tile's owner before a browser decoder suspends completion. + let still = with_optional_still(owner, current_still); if matches!(&bytes, Err(e) if e == DROPPED) { return; } @@ -688,10 +720,13 @@ fn fetch_tile(url: String, host: String, want: Want, omit: &'static [u16]) { let settle = move |decoded: Result| { let mut answer = answer; answer.set(match decoded { + Ok(_) if check_still(&still).is_err() => return, Ok(p) => { let size = payload_bytes(&p); Entry::Ready(Arc::new(p), size) } + // The browser's decoder may answer after the initiating owner was retired. + Err(e) if !keep_failure(&e, &still) => return, Err(e) => { log::debug!("{}", crate::logins::scrub_error(&format!("{}: {e}", answer.key))); // Refused until a time its server gave: the map draws again then. @@ -978,6 +1013,12 @@ pub fn download_detailed(url: &str, max: usize, done: DoneDetailed) { /// Like [`download_detailed`], giving up after `timeout` natively; a status in `omit` isn't /// recorded as a failure. fn download_detailed_within(url: &str, max: usize, timeout: std::time::Duration, omit: &'static [u16], done: DoneDetailed) { + if let Err(e) = check_current() { + done(Err(Failure::new(url, e))); + return; + } + let still = current_still(); + let done: DoneDetailed = Box::new(move |r| with_optional_still(still, || done(r))); if let Err(e) = crate::address::check(url, None) { done(Err(Failure::new(url, e))); return; @@ -1156,6 +1197,12 @@ pub fn tiff_header_length(value: &str) -> Option { /// the web), counted as a fetch under way. A request signed for S3 isn't made (its signature is /// for a GET): it answers with nothing known. pub fn head(url: &str, done: HeadDone) { + if let Err(e) = check_current() { + done(Err(e)); + return; + } + let still = current_still(); + let done: HeadDone = Box::new(move |r| with_optional_still(still, || done(r))); if let Err(e) = crate::address::check(url, None) { done(Err(e)); return; @@ -1177,6 +1224,12 @@ pub fn head(url: &str, done: HeadDone) { /// A status in `omit` is an expected answer: its failure isn't recorded, only noted once per /// server (a tile past a map's edge). pub(crate) fn fetch_bytes(url: String, done: Done, omit: &'static [u16]) { + if let Err(e) = check_current() { + done(Err(e)); + return; + } + let still = current_still(); + let done: Done = Box::new(move |r| with_optional_still(still, || done(r))); if let Err(e) = crate::logins::check(resource_of(&url)) { done(Err(e.to_string())); return; @@ -1388,7 +1441,7 @@ mod native { struct Jobs { /// Waiting, oldest first: the address, its server, callback, initiating owner, and the statuses /// whose failures the caller expects. - waiting: Vec<(String, String, Done, Capture, &'static [u16])>, + waiting: Vec<(String, String, Done, Capture, &'static [u16], Option)>, /// Fetches under way, by server. active: HashMap, } @@ -1457,13 +1510,13 @@ mod native { let dropped = { let mut jobs = q.jobs.lock().unwrap_or_else(|e| e.into_inner()); let host = host(&url); - jobs.waiting.push((url, host, done, capture, omit)); + jobs.waiting.push((url, host, done, capture, omit, super::current_still())); (jobs.waiting.len() > QUEUE).then(|| jobs.waiting.remove(0)) }; // Every worker may be waiting for a server at its cap, and only some can take this one. q.ready.notify_all(); // Answered once the queue is unlocked: `done` takes the cache's lock and wakes the app. - if let Some((_, _, done, _, _)) = dropped { + if let Some((_, _, done, _, _, _)) = dropped { done(Err(DROPPED.into())); } } @@ -1551,11 +1604,14 @@ mod native { return; } let capture = Capture::current(); + let still = super::current_still(); std::thread::spawn(move || { - let _scope = capture.enter(); - let r = head_with(AGENT.get_or_init(agent), &url); - HEADS.fetch_sub(1, Relaxed); - done(r); + super::with_optional_still(still, || { + let _scope = capture.enter(); + let r = super::check_current().and_then(|_| head_with(AGENT.get_or_init(agent), &url)); + HEADS.fetch_sub(1, Relaxed); + done(r); + }) }); } @@ -1588,6 +1644,7 @@ mod native { } } diagnostics.sending(); + super::check_current()?; let resp = req.call().map_err(unanswered)?; diagnostics.answered(resp.status().as_u16()); let header = |k: &str| resp.headers().get(k).and_then(|v| v.to_str().ok()); @@ -1636,9 +1693,12 @@ mod native { pub(super) fn post(url: String, body: String, done: Done) { // Internet addresses only: a name that resolves into the local network gets no body. let capture = Capture::current(); + let still = super::current_still(); std::thread::spawn(move || { - let _scope = capture.enter(); - done(post_with(&public_agent(std::time::Duration::from_secs(60)), &url, &body)); + super::with_optional_still(still, || { + let _scope = capture.enter(); + done(super::check_current().and_then(|_| post_with(&public_agent(std::time::Duration::from_secs(60)), &url, &body))); + }); }); } @@ -1661,6 +1721,7 @@ mod native { if let Some((name, value)) = login.as_ref().and_then(|a| a.header.as_ref()) { req = req.header(name.as_str(), value.as_str()); } + super::check_current()?; let mut resp = req.send(body).map_err(unanswered)?; let status = resp.status().as_u16(); diagnostics.answered(status); @@ -1698,9 +1759,14 @@ mod native { pub(super) fn download_within(url: String, timeout: std::time::Duration, omit: &'static [u16], done: DoneDetailed) { let capture = Capture::current(); + let still = super::current_still(); std::thread::spawn(move || { - let _scope = capture.enter(); - done(get_detailed_omitting(&download_agent(timeout), &url, omit)); + super::with_optional_still(still, || { + let _scope = capture.enter(); + let r = + super::check_current().map_err(Failure::from).and_then(|_| get_detailed_omitting(&download_agent(timeout), &url, omit)); + done(r); + }) }); } @@ -1708,20 +1774,23 @@ mod native { let agent = shared_agent(); let q = queue(); loop { - let (url, done, host, capture, omit) = { + let (url, done, host, capture, omit, still) = { let mut jobs = q.jobs.lock().unwrap_or_else(|e| e.into_inner()); loop { - let hosts: Vec<&str> = jobs.waiting.iter().map(|(_, h, _, _, _)| h.as_str()).collect(); + let hosts: Vec<&str> = jobs.waiting.iter().map(|(_, h, _, _, _, _)| h.as_str()).collect(); if let Some(i) = pick(&hosts, &jobs.active) { - let (url, host, done, capture, omit) = jobs.waiting.remove(i); + let (url, host, done, capture, omit, still) = jobs.waiting.remove(i); *jobs.active.entry(host.clone()).or_insert(0) += 1; - break (url, done, host, capture, omit); + break (url, done, host, capture, omit, still); } jobs = q.ready.wait(jobs).unwrap_or_else(|e| e.into_inner()); } }; let _scope = capture.enter(); - let result = get(agent, &url, omit); + let result = super::with_optional_still(still.clone(), || { + super::check_current()?; + get(agent, &url, omit) + }); { let mut jobs = q.jobs.lock().unwrap_or_else(|e| e.into_inner()); if let Some(n) = jobs.active.get_mut(&host) { @@ -1733,7 +1802,7 @@ mod native { } // A job waiting for this server may go now. q.ready.notify_all(); - done(result); + super::with_optional_still(still, || done(result)); } } @@ -1815,6 +1884,7 @@ mod native { let mut diagnostics = super::RequestDiagnostics::new(url, "GET"); diagnostics.omit_statuses(omit); let answer = (|| -> Result, Failure> { + super::check_current()?; if crate::mbtiles::is_tile(url) { return crate::mbtiles::read_local(url).map_err(Failure::from); } @@ -1876,6 +1946,7 @@ mod native { req = req.header("X-Esri-Authorization", &auth); } diagnostics.sending(); + super::check_current()?; let mut resp = req.call().map_err(unanswered)?; let status = resp.status().as_u16(); diagnostics.answered(status); @@ -2046,8 +2117,67 @@ mod native { #[cfg(test)] mod tests { + /// Runs a fixture that needs stable process-wide cache entries without unrelated login + /// revocation clearing them. The exact named child must execute its existing assertions. + #[cfg(not(target_arch = "wasm32"))] + fn isolate_cache_fixture(name: &str) -> bool { + const MARK: &str = "RUSTGIS_TEST_CACHE_FIXTURE"; + if std::env::var(MARK).as_deref() == Ok(name) { + return false; + } + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", name, "--nocapture", "--test-threads=1"]) + .env(MARK, name) + .output() + .unwrap(); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!(output.status.success(), "{name}:\n{stdout}\n{}", String::from_utf8_lossy(&output.stderr)); + assert!(stdout.contains("1 passed; 0 failed"), "the child must run exactly {name}: {stdout}"); + true + } + use super::*; + /// Queued fetches and dedicated download threads recheck authority on actual start. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn revoked_workers_send_nothing_and_callbacks_inherit_context() { + use std::sync::atomic::{AtomicBool, Ordering::SeqCst}; + for kind in 0..2 { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let url = format!("http://{}/held", listener.local_addr().unwrap()); + let live = Arc::new(AtomicBool::new(true)); + let first = Arc::new(AtomicBool::new(true)); + let (entered, entry) = std::sync::mpsc::channel(); + let (release, released) = std::sync::mpsc::channel(); + let released = Mutex::new(released); + let flag = live.clone(); + let still: Still = Arc::new(move || { + if first.swap(false, SeqCst) { + entered.send(()).unwrap(); + released.lock().unwrap().recv().unwrap(); + } + flag.load(SeqCst) + }); + let (done, result) = std::sync::mpsc::channel(); + let callback = Box::new(move |r: Result, String>| { + assert!(check_still(¤t_still()).is_err(), "a follow-on request inherits revocation"); + done.send(r).unwrap(); + }); + with_still(still, || match kind { + 0 => native::fetch(url, callback, &[]), + _ => native::download(url, Box::new(move |r| callback(r.map_err(|e| e.message)))), + }); + entry.recv_timeout(std::time::Duration::from_secs(10)).unwrap(); + live.store(false, SeqCst); + release.send(()).unwrap(); + assert!(result.recv_timeout(std::time::Duration::from_secs(10)).unwrap().unwrap_err().contains("cancelled")); + assert_eq!(listener.accept().unwrap_err().kind(), std::io::ErrorKind::WouldBlock); + assert!(current_still().is_none()); + } + } + fn network_records(history: &rustgis_data::diagnostics::History, url: &str) -> Vec { history.records().into_iter().filter(|record| record.network.as_ref().is_some_and(|network| network.url == url)).collect() } @@ -2891,6 +3021,10 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn live_connection_bindings_separate_requests_and_cache_entries() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_cache_fixture("cache::tests::live_connection_bindings_separate_requests_and_cache_entries") { + return; + } let _turn = crate::cache::turn(); use crate::logins::{self, Method}; let answer = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}".to_vec(); @@ -2963,6 +3097,10 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn derived_style_tilejson_sprite_and_feature_requests_carry_the_binding() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_cache_fixture("cache::tests::derived_style_tilejson_sprite_and_feature_requests_carry_the_binding") { + return; + } let _turn = crate::cache::turn(); use crate::{ logins::{self, Method}, @@ -2993,8 +3131,7 @@ mod tests { logins::activate(&base, "derived-style", Method::Header("X-Service-Key".into()), "derived-style-secret", true).unwrap(); let url = logins::bind(&binding, &format!("{base}/style.json")).unwrap(); let tilejson_url = template::join(&url, "./tiles.json"); - // The cache is shared with tests running at once, and any of them may clear it: store the - // fetched documents again on each try until the style resolves from them. + // Seed asynchronous style resolution with the two documents already fetched. let mut documents = Vec::new(); for address in [&url, &tilejson_url] { let bytes = native::get(&native::agent(), address, &[]).unwrap(); @@ -3234,8 +3371,116 @@ mod tests { assert!(most <= native::cap(&addr.to_string()), "{most} at a time"); } + /// Successful bytes and browser decodes cannot populate a shared cache after their owner + /// retires, including a decode held across reload; active owners still populate it. + #[test] + fn retired_successes_do_not_populate_the_tile_cache() { + let _turn = turn(); + let held = Arc::new(Mutex::new(Vec::::new())); + let hold = held.clone(); + set_fetcher(move |_, done| hold.lock().unwrap().push(done)); + let decoded = Arc::new(Mutex::new(Vec::<(Vec, Decoded)>::new())); + let hold = decoded.clone(); + let old = image_decoder().lock().unwrap().clone(); + struct RestoreDecoder(Option>); + impl Drop for RestoreDecoder { + fn drop(&mut self) { + *image_decoder().lock().unwrap() = self.0.take(); + } + } + let _restore = RestoreDecoder(old); + set_image_decoder(move |bytes, done| hold.lock().unwrap().push((bytes, done))); + let png = Pixmap::new(1, 1).unwrap().encode_png().unwrap(); + let mut failures = Vec::new(); + for want in [Want::Image, Want::Json, Want::Vector] { + for revoke in [false, true] { + let url = format!("https://retired-success.invalid/{want:?}-{revoke}"); + let live = Arc::new(std::sync::atomic::AtomicBool::new(true)); + let flag = live.clone(); + assert!(matches!(with_still(Arc::new(move || flag.load(Ordering::SeqCst)), || get(&url, want)), State::Pending)); + let done = held.lock().unwrap().pop().unwrap(); + if want == Want::Image { + done(Ok(png.clone())); + assert_eq!(decoded.lock().unwrap().len(), 1, "real cache entry waits for browser decoding"); + clear(); + live.store(!revoke, Ordering::SeqCst); + let (bytes, done) = decoded.lock().unwrap().pop().unwrap(); + done(Err(bytes)); + } else { + live.store(!revoke, Ordering::SeqCst); + done(Ok(if want == Want::Json { b"1".to_vec() } else { Vec::new() })); + } + if peek(&url).is_some() == revoke { + failures.push(format!("{want:?}: cached success with revoke={revoke}")); + if revoke { + forget(&url); + } + } + if revoke { + assert!(matches!(get(&url, want), State::Pending)); + // A fresh live caller can finish immediately, with no inherited failure. + let done = held.lock().unwrap().pop().unwrap(); + done(Ok(Vec::new())); + assert!(matches!(get(&url, want), State::Ready(_))); + } + } + } + assert!(failures.is_empty(), "{failures:?}"); + } + + /// Revocation belongs to the caller, while a genuine server failure belongs to the resource. + /// Header caches must let a later caller retry immediately under its own lifetime. + #[test] + fn revoked_archive_headers_do_not_delay_live_callers() { + let _turn = crate::cache::turn(); + let _alarm = crate::cache::alarm_turn(); + let held = Arc::new(Mutex::new(Vec::::new())); + let hold = held.clone(); + set_fetcher(move |_, done| hold.lock().unwrap().push(done)); + let mut failures = Vec::new(); + for cog in [false, true] { + let url = if cog { "https://revoked-header.invalid/retry.tif" } else { "https://revoked-header.invalid/retry.pmtiles" }; + let ask = || if cog { crate::cog::info(url).map(|i| i.is_some()) } else { crate::pmtiles::info(url).map(|i| i.is_some()) }; + let live = Arc::new(std::sync::atomic::AtomicBool::new(true)); + let flag = live.clone(); + let still: Still = Arc::new(move || flag.load(Ordering::SeqCst)); + assert_eq!(with_still(still, ask), Ok(false)); + // A distinct live tile joins the scoped header read. Its resource must not keep + // the loader's cancellation, even though its own initiating lifetime is current. + let tile = format!("{}:0/0/0@{url}", if cog { "cog" } else { "pmtiles" }); + assert!(matches!(get(&tile, Want::Image), State::Pending)); + live.store(false, Ordering::SeqCst); + held.lock().unwrap().pop().unwrap()(Err("HTTP 503".into())); + if lock().slots.contains_key(&tile) { + failures.push(format!("{tile}: a joining live tile cached the retired loader's failure")); + } + let retry = ask(); + let tile_retry = get(&tile, Want::Image); + if !matches!(tile_retry, State::Pending) { + failures.push(format!("{tile}: the next live draw could not retry immediately")); + } + let next = held.lock().unwrap().pop(); + if retry != Ok(false) || next.is_none() { + failures.push(format!("{url}: a revoked owner's error delayed a live caller: {retry:?}")); + } + if let Some(done) = next { + done(Err("HTTP 503".into())); + assert!(ask().is_err(), "a live owner's server failure keeps its retry delay"); + assert!(matches!(get(&tile, Want::Image), State::Failed(_)), "live tile failures still keep the server's retry delay"); + assert!(held.lock().unwrap().is_empty()); + } + } + assert!(failures.is_empty(), "{failures:?}"); + } + + /// A shared tile slot and its server queue retain their owners, release retired reads, + /// and let live callers retry without inheriting another request's cancellation. #[test] fn fetches_through_the_fetcher_once() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_cache_fixture("cache::tests::fetches_through_the_fetcher_once") { + return; + } let _turn = crate::cache::turn(); static CALLS: AtomicUsize = AtomicUsize::new(0); set_fetcher(|url, done| { @@ -3288,6 +3533,79 @@ mod tests { assert!(matches!(get(&format!("test://held/{i}.json"), Want::Json), State::Ready(_)), "{i}"); } + // Waiting at the server cap must retain the initiating request's revocation and + // diagnostics owner, even when another owner's completion starts the fetch. + let seen = Arc::new(Mutex::new(Vec::new())); + let fetched = seen.clone(); + set_fetcher(move |url, done| { + if url.starts_with("test://held/") { + fetched.lock().unwrap().push(url.clone()); + if url == "test://held/context/diagnostics.json" { + let mut diagnostics = RequestDiagnostics::new(&url, "GET"); + diagnostics.answered(503); + diagnostics.failed(&Failure::new(&url, "http status: 503")); + done(Err("http status: 503".into())); + } else { + HELD.lock().unwrap().push((url, done)); + } + } else { + done(Ok(b"1".to_vec())); + } + }); + let drain_held = || loop { + let Some((_, done)) = HELD.lock().unwrap().pop() else { break }; + done(Ok(b"3".to_vec())); + }; + let owner = rustgis_data::diagnostics::History::default(); + let launcher = rustgis_data::diagnostics::History::default(); + let live = Arc::new(std::sync::atomic::AtomicBool::new(true)); + let flag = live.clone(); + let still: Still = Arc::new(move || flag.load(Ordering::SeqCst)); + for i in 0..crate::schedule::PER_HOST { + let _ = get(&format!("test://held/context/filler-{i}.json"), Want::Json); + } + let revoked = "test://held/context/revoked.json"; + let diagnostic = "test://held/context/diagnostics.json"; + let healthy = "test://held/context/healthy.json"; + let admitted = { + let _scope = owner.enter(); + [with_still(still, || get(revoked, Want::Json)), get(diagnostic, Want::Json)] + }; + let control = get(healthy, Want::Json); + let held_before = HELD.lock().unwrap().len(); + live.store(false, Ordering::SeqCst); + { + let _scope = launcher.enter(); + drain_held(); + } + // Drain before asserting: the RED result must not strand the other queued requests. + assert_eq!(held_before, crate::schedule::PER_HOST); + assert!(admitted.iter().all(|state| matches!(state, State::Pending)) && matches!(control, State::Pending)); + assert!(!seen.lock().unwrap().iter().any(|url| url == revoked), "a revoked waiting request reached the fetcher"); + assert!(!lock().slots.contains_key(revoked), "a revoked owner must not leave a shared tile failure"); + assert!(matches!(get(revoked, Want::Json), State::Pending), "a live caller starts its own fetch immediately"); + drain_held(); + assert!(matches!(get(revoked, Want::Json), State::Ready(_))); + assert!(peek(healthy).is_some(), "the unscoped waiting control still completes"); + assert_eq!(network_records(&owner, diagnostic).len(), 1); + assert!(launcher.records().is_empty(), "a queued request keeps its original diagnostics owner"); + assert!(current_still().is_none()); + + // Restoring an initiator must also preserve the launcher's existing revocation floor. + for i in 0..crate::schedule::PER_HOST { + let _ = get(&format!("test://held/context-floor/filler-{i}.json"), Want::Json); + } + let floor = "test://held/context-floor/queued.json"; + let admitted = with_still(Arc::new(|| true), || get(floor, Want::Json)); + with_still(Arc::new(|| false), drain_held); + assert!(matches!(admitted, State::Pending)); + assert!(!seen.lock().unwrap().iter().any(|url| url == floor)); + assert!(!lock().slots.contains_key(floor), "the launcher's revocation must not leave a shared tile failure"); + assert!(matches!(get(floor, Want::Json), State::Pending)); + drain_held(); + assert!(matches!(get(floor, Want::Json), State::Ready(_))); + assert!(current_still().is_none()); + // A fetcher that drops a request unanswered (or panics) gives its server's place back: // more requests than the cap still all reach it, and each is forgotten, not stuck. static SEEN: AtomicUsize = AtomicUsize::new(0); diff --git a/crates/tiles/src/cog.rs b/crates/tiles/src/cog.rs index 09c867cf95..02ca65d8da 100644 --- a/crates/tiles/src/cog.rs +++ b/crates/tiles/src/cog.rs @@ -22,7 +22,7 @@ use rustgis_data::raster::crs::Transformer; use rustgis_geom::Coord; use web_time::{Duration, Instant}; -use crate::cache::{Done, MAX_BYTES, RANGE}; +use crate::cache::{Done, MAX_BYTES, RANGE, caller_callback}; pub mod raster; @@ -345,6 +345,7 @@ fn forget_file_in(t: &mut Tiles, url: &str) { /// A file's info: `Ok(None)` while it is being read (asking for it starts the read). pub fn info(url: &str) -> Result>, String> { + crate::cache::check_still(&crate::cache::current_still())?; crate::logins::check(url).map_err(str::to_string)?; let url = split(url).0; { @@ -367,7 +368,13 @@ pub fn info(url: &str) -> Result>, String> { Ok(None) } +/// Shares parsed headers and loading waiters, retaining retry delays only for resource failures. fn with_info(url: &str, then: Box, String>) + Send>) { + let then = caller_callback(then); + if let Err(e) = crate::cache::check_still(&crate::cache::current_still()) { + then(Err(e)); + return; + } if let Err(e) = crate::logins::check(url) { then(Err(e.to_string())); return; @@ -401,22 +408,25 @@ fn with_info(url: &str, then: Box, String>) + Send>) load( url.to_string(), Box::new(move |r| { + let r = crate::cache::check_still(&crate::cache::current_still()).and(r); let r = crate::logins::check(&key) .map_err(str::to_string) .and(r) .map_err(|e| if rustgis_data::login_address::is_wrapper(&key) { crate::logins::scrub_error(&e) } else { e }); + let transient = r.as_ref().err().is_some_and(|e| !crate::cache::keep_failure(e, &crate::cache::current_still())); let waiting = { let mut i = infos().lock().unwrap_or_else(|e| e.into_inner()); - let next = match &r { - Ok(info) => State::Ready(info.clone()), + let old = match &r { + Ok(info) => i.insert(key, State::Ready(info.clone())), + Err(_) if transient => i.remove(&key), Err(e) => { // Read again once its wait is over: the map draws again then (a 429 // or 503 says when). crate::cache::notify_at(Instant::now() + crate::cache::retry_wait(e, RETRY)); - State::Failed(e.clone(), Instant::now()) + i.insert(key, State::Failed(e.clone(), Instant::now())) } }; - match i.insert(key, next) { + match old { Some(State::Loading(w)) => w, _ => Vec::new(), } @@ -703,7 +713,13 @@ fn percentiles(mut v: Vec) -> (f32, f32) { /// tiles of the bands shown) and kept. type StretchThen = Box, String>) + Send>; +/// Shares computed stretches; a retired read cannot leave fallback values or a retry delay. fn with_stretch(info: Arc, file: String, look: Look, then: StretchThen) { + let then = caller_callback(then); + if let Err(e) = crate::cache::check_still(&crate::cache::current_still()) { + then(Err(e)); + return; + } if let Some(s) = info.stretch_for(&look) { then(Ok(s)); return; @@ -744,25 +760,27 @@ fn with_stretch(info: Arc, file: String, look: Look, then: StretchThen) { let shown_bands = colour.len(); let key_aef = look.aef; let done = move |r: Result, String>| { + let r = crate::cache::check_still(&crate::cache::current_still()).and(r); let waiting = i.stretching.lock().unwrap_or_else(|e| e.into_inner()).remove(&key).unwrap_or_default(); - // An overview that can't be read: the type's full range for now (not kept: the failure may - // pass), and no read again until RETRY has passed. + // A server error uses the type's full range until RETRY passes. Dropped or retired + // reads propagate their refusal without leaving a shared stretch or retry delay. let s = match r { Ok(s) => { keep_stretch(&i, key, s.clone()); - s + Ok(s) } + Err(e) if !crate::cache::keep_failure(&e, &crate::cache::current_still()) => Err(e), Err(_) => { let mut failed = i.stretch_failed.lock().unwrap_or_else(|e| e.into_inner()); if failed.len() >= MAX_LOOKS { failed.clear(); } failed.insert(key, Instant::now()); - vec![type_range(&i.layout, key_aef); shown_bands] + Ok(vec![type_range(&i.layout, key_aef); shown_bands]) } }; for w in waiting { - w(Ok(s.clone())); + w(s.clone()); } }; fetch_bands( @@ -994,6 +1012,11 @@ fn fetch_all_with(get: GetTile, info: Arc, url: String, level: usize, want /// One tile of a level, decoded: from the tiles kept, or read. fn get_tile(info: Arc, url: String, level: usize, idx: u32, then: Box) + Send>) { + let then = caller_callback(then); + if let Err(e) = crate::cache::check_still(&crate::cache::current_still()) { + then(Err(e)); + return; + } if let Err(e) = crate::logins::check(&url) { then(Err(e.to_string())); return; @@ -1058,6 +1081,7 @@ fn get_tile(info: Arc, url: String, level: usize, idx: u32, then: Box::new())); + let hold = held.clone(); + crate::cache::set_fetcher(move |url, done| hold.lock().unwrap().push((url, done))); + let drain = || { + let mut sent = 0; + loop { + let pending = std::mem::take(&mut *held.lock().unwrap()); + if pending.is_empty() { + return sent; + } + for (url, done) in pending { + sent += 1; + assert!(sent < 100, "bounded overview fixture"); + let (_, range) = crate::cache::split_range(&url); + let (a, b) = range.unwrap(); + done(Ok(file[a as usize..((b + 1) as usize).min(file.len())].to_vec())); + } + } + }; + let mut failures = Vec::new(); + for revoke in [false, true] { + let url = format!("https://shared-cog.invalid/{revoke}.tif"); + let (loaded, info_result) = std::sync::mpsc::channel(); + with_info(&url, Box::new(move |r| loaded.send(r).unwrap())); + assert_eq!(held.lock().unwrap().len(), 1, "first caller holds the header"); + let live = Arc::new(AtomicBool::new(true)); + let flag = live.clone(); + let still: crate::cache::Still = Arc::new(move || flag.load(SeqCst)); + let (done, result) = std::sync::mpsc::channel(); + crate::cache::with_still(still.clone(), || raster::overview(&url, Box::new(move |r| done.send(r).unwrap()))); + live.store(!revoke, SeqCst); + let sent = drain(); + let info = info_result.recv().unwrap().unwrap(); + let r = result.recv().unwrap(); + if revoke { + if r.is_ok() || sent != 1 { + failures.push(format!("revoked metadata waiter succeeded or fetched {sent} ranges")); + } + } else { + assert!(r.is_ok() && sent > 1, "live overview needs real tile ranges: {sent}"); + } + // A different tile starts under User; the bounded joiner's successful callback + // would ask for a second tile. It must receive refusal before doing so. + let (first, first_result) = std::sync::mpsc::channel(); + get_tile(info.clone(), url.clone(), 0, 0, Box::new(move |r| first.send(r).unwrap())); + live.store(true, SeqCst); + let (done, result) = std::sync::mpsc::channel(); + let (i, u) = (info.clone(), url.clone()); + crate::cache::with_still(still.clone(), || { + get_tile( + i.clone(), + u.clone(), + 0, + 0, + Box::new(move |r| match r { + Ok(_) => get_tile(i, u, 0, 1, Box::new(move |r| done.send(r).unwrap())), + Err(e) => done.send(Err(e)).unwrap(), + }), + ); + }); + live.store(!revoke, SeqCst); + drain(); + assert!(first_result.recv().unwrap().is_ok()); + if result.recv().unwrap().is_ok() == revoke { + failures.push(format!("tile joiner did not respect revoke={revoke}")); + } + // Ready hits are an entry point too, even without another network request. + live.store(false, SeqCst); + let (done, result) = std::sync::mpsc::channel(); + crate::cache::with_still(still.clone(), || get_tile(info.clone(), url.clone(), 0, 0, Box::new(move |r| done.send(r).unwrap()))); + if result.recv().unwrap().is_ok() { + failures.push("revoked ready tile was delivered".into()); + } + // A retired statistics read must not delay a later caller's stretch for a minute. + forget_file(&url); + let look = Look { aef: true, ..Default::default() }; + let (done, result) = std::sync::mpsc::channel(); + live.store(true, SeqCst); + crate::cache::with_still(still, || { + with_stretch(info.clone(), url.clone(), look.clone(), Box::new(move |r| done.send(r).unwrap())) + }); + assert!(!held.lock().unwrap().is_empty(), "the stretch starts a real range read"); + live.store(false, SeqCst); + drain(); + assert!(result.recv().unwrap().is_err()); + if !info.stretch_failed.lock().unwrap().is_empty() { + failures.push("a revoked stretch left a shared retry delay".into()); + } + let (done, result) = std::sync::mpsc::channel(); + let key = (info.colour(&look), look.aef); + with_stretch(info.clone(), url, look, Box::new(move |r| done.send(r).unwrap())); + drain(); + assert!(result.recv().unwrap().is_ok()); + // Valid raw samples may already be shared; a computed stretch is kept, whereas + // a recent failure's placeholder would return without storing this look's key. + if !info.stretch.lock().unwrap().contains_key(&key) { + failures.push("a live stretch caller received the cancelled read's fallback".into()); + } + } + let url = "https://shared-cog.invalid/retired-loader.tif"; + let live = Arc::new(AtomicBool::new(true)); + let flag = live.clone(); + let still: crate::cache::Still = Arc::new(move || flag.load(SeqCst)); + let (done, result) = std::sync::mpsc::channel(); + crate::cache::with_still(still.clone(), || with_info(url, Box::new(move |r| done.send(r).unwrap()))); + live.store(false, SeqCst); + drain(); + assert!(result.recv().unwrap().is_err()); + if infos().lock().unwrap().contains_key(url) { + failures.push("a retired loader cached a successful header".into()); + forget_file(url); + } + let (done, result) = std::sync::mpsc::channel(); + with_info(url, Box::new(move |r| done.send(r).unwrap())); + drain(); + let info = result.recv().unwrap().unwrap(); + live.store(true, SeqCst); + let (done, result) = std::sync::mpsc::channel(); + crate::cache::with_still(still, || get_tile(info.clone(), url.into(), 0, 0, Box::new(move |r| done.send(r).unwrap()))); + live.store(false, SeqCst); + drain(); + assert!(result.recv().unwrap().is_err()); + if tiles().lock().unwrap().map.contains_key(&(url.into(), 0, 0)) { + failures.push("a retired loader cached successful raw samples".into()); + forget_file(url); + } + let (done, result) = std::sync::mpsc::channel(); + get_tile(info, url.into(), 0, 0, Box::new(move |r| done.send(r).unwrap())); + assert!(drain() > 0, "the live caller reads fresh raw samples"); + assert!(result.recv().unwrap().is_ok()); + assert!(failures.is_empty(), "{failures:?}"); + } + /// A palette image's own look colours its values by the table the header keeps apart from its /// colour hint (`Layout::palette`), with no stretch read or kept; a look of its bands doesn't. #[test] diff --git a/crates/tiles/src/cog/raster.rs b/crates/tiles/src/cog/raster.rs index 3193b350a5..320b22c4d1 100644 --- a/crates/tiles/src/cog/raster.rs +++ b/crates/tiles/src/cog/raster.rs @@ -198,12 +198,12 @@ const MAX_FAILED: usize = 4096; /// What a tile's arrival leaves: the map draws again. One that failed is remembered until its /// wait is over (a minute, or what its server's `Retry-After` said), the map asked to draw again -/// then; one the fetch queue dropped (the view moved on, see [`crate::cache::DROPPED`]) is no -/// failure: not remembered, it is asked for again when it is drawn next, and the tiles that +/// then; a dropped or retired read is no resource failure (see [`crate::cache::keep_failure`]): +/// not remembered, it is asked for again when it is drawn next, and the tiles that /// pushed it out draw the map as they land. fn tile_landed(key: (String, usize, u32), r: &Result) { match r { - Err(e) if e == crate::cache::DROPPED => return, + Err(e) if !crate::cache::keep_failure(e, &crate::cache::current_still()) => return, Err(e) => { let until = Instant::now() + crate::cache::retry_wait(e, RETRY); remember_failed(&mut failed().lock().unwrap_or_else(|e| e.into_inner()), key, until); @@ -591,6 +591,10 @@ mod tests { assert!(failed().lock().unwrap().contains_key(&key(2))); tile_landed(key(3), &Ok(Arc::new(vec![vec![0.0f32]].into()))); assert!(!failed().lock().unwrap().contains_key(&key(3))); + crate::cache::with_still(Arc::new(|| false), || tile_landed(key(4), &Err("HTTP 503".into()))); + assert!(!failed().lock().unwrap().contains_key(&key(4)), "a retired window read must not delay a live caller"); + tile_landed(key(4), &Err("HTTP 503".into())); + assert!(failed().lock().unwrap().contains_key(&key(4)), "a live server failure still waits before retrying"); } /// Failed tiles are forgotten with their file (a login or a connection changed: asked for at diff --git a/crates/tiles/src/earthdata.rs b/crates/tiles/src/earthdata.rs index 0d558e2b3a..a210ec808e 100644 --- a/crates/tiles/src/earthdata.rs +++ b/crates/tiles/src/earthdata.rs @@ -61,6 +61,40 @@ fn lock(m: &Mutex) -> std::sync::MutexGuard<'_, T> { m.lock().unwrap_or_else(|e| e.into_inner()) } +/// An immutable, exact active-runtime token snapshot. It never reads the credential store +/// or expires/restores a token, and has no formatting, serialization or raw-value accessors. +#[derive(Clone, PartialEq, Eq)] +pub struct AccountIdentity { + relevant: bool, + token: Option, +} + +impl AccountIdentity { + /// Checks exact active in-memory identity, without restoration or any other I/O. + /// An unavailable or poisoned relevant store returns false. + pub fn is_current(&self) -> bool { + identity_in(token_store(), self.relevant).is_ok_and(|current| *self == current) + } +} + +/// Snapshots the active token only; an unread remembered token remains unread. +/// An unavailable or poisoned store is an error, never an absent token. +pub fn account_identity() -> Result { + identity_in(token_store(), true) +} + +fn identity_in(store: &Mutex>, relevant: bool) -> Result { + let token = if relevant { store.lock().map_err(|_| "active account store unavailable".to_string())?.clone() } else { None }; + Ok(AccountIdentity { relevant, token }) +} + +/// Snapshots the token for a data host, including its absence. Public/search/login and +/// unrelated addresses do not acquire a dependency on the active token. +/// An unavailable or poisoned relevant store is an error, never an absent token. +pub fn account_identity_for(url: &str) -> Result { + identity_in(token_store(), is_data_host(url)) +} + /// A token as given (`None` when blank): one word of JWT characters, without `Bearer `; anything /// else is refused, so it can't break the header it goes in. fn checked(token: &str) -> Result, String> { @@ -796,9 +830,20 @@ mod tests { const DATA: &str = "https://data.lpdaac.earthdatacloud.nasa.gov/a.tif"; fn session_tokens() { + fn send_sync() {} + send_sync::(); + let irrelevant = account_identity_for(CMR).unwrap(); + let absent = account_identity_for(DATA).unwrap(); assert!(set_token(Some("bad token with spaces")).is_err()); assert!(set_token(Some("a\r\nX-Evil: 1")).is_err()); set_token(Some(&format!("Bearer {LASTING}"))).unwrap(); + assert!(!absent.is_current()); + let identity = account_identity_for(DATA).unwrap(); + assert!(identity.is_current() && identity == account_identity().unwrap()); + set_token(Some("synthetic-replacement")).unwrap(); + assert!(!identity.is_current() && irrelevant.is_current()); + assert!(account_identity_for(DATA).unwrap().is_current()); + set_token(Some(LASTING)).unwrap(); assert!(has_token()); assert_eq!(token_expiry(), Some(4_000_000_000)); assert_eq!(authorization(DATA).as_deref(), Some(format!("Bearer {LASTING}").as_str())); @@ -816,6 +861,22 @@ mod tests { assert!(!has_token()); } + #[test] + fn permission_transport_earthdata_capture_fails_closed() { + let store = Arc::new(Mutex::new(None)); + let worker = store.clone(); + assert!( + std::thread::spawn(move || { + let _locked = worker.lock().unwrap(); + panic!("synthetic poison"); + }) + .join() + .is_err() + ); + assert!(identity_in(&store, true).is_err()); + assert!(identity_in(&store, false).is_ok(), "unrelated URLs don't depend on this store"); + } + /// A new run of the app: nothing typed yet, the store not read. fn relaunch(store: &Arc) { set_token(None).unwrap(); @@ -837,9 +898,12 @@ mod tests { assert!(is_remembered()); relaunch(&store); assert!(!has_token() && !is_remembered()); + let unread = account_identity_for(DATA).unwrap(); + assert!(unread.is_current() && !has_token(), "snapshotting never restores the remembered token"); assert_eq!(authorization("https://cmr.earthdata.nasa.gov/search"), None); assert!(!has_token(), "a public host doesn't read the store"); assert_eq!(authorization(DATA).as_deref(), Some(format!("Bearer {LASTING}").as_str())); + assert!(!unread.is_current(), "restoration changes active identity just like replacement"); assert!(is_remembered()); assert_eq!(restore(), Restored::Nothing, "read once"); diff --git a/crates/tiles/src/logins.rs b/crates/tiles/src/logins.rs index 7c92a34980..64ffbe67d8 100644 --- a/crates/tiles/src/logins.rs +++ b/crates/tiles/src/logins.rs @@ -90,9 +90,40 @@ struct Redaction { encoded: bool, } -fn entries() -> MutexGuard<'static, BTreeMap> { +fn registry() -> &'static Mutex> { static ENTRIES: OnceLock>> = OnceLock::new(); - ENTRIES.get_or_init(Mutex::default).lock().unwrap_or_else(|e| e.into_inner()) + ENTRIES.get_or_init(Mutex::default) +} + +fn entries() -> MutexGuard<'static, BTreeMap> { + registry().lock().unwrap_or_else(|e| e.into_inner()) +} + +/// An exact runtime snapshot of one session's login generations, including their absence. +/// It contains no credential and is never a display or persistence representation. +#[derive(Clone, PartialEq, Eq)] +pub struct AccountIdentity { + session: u64, + entries: Vec<(String, String, Method)>, +} + +/// Captures only the initiating session's runtime bindings. Other sessions cannot invalidate it. +/// Approval capture must fail closed when the runtime registry cannot be read consistently. +pub fn account_identity(session: u64) -> Result { + let entries = registry().lock().map_err(|_| "The service login identity is unavailable.")?; + let entries = entries + .iter() + .filter(|(_, entry)| entry.owner.split_once('/').is_some_and(|(s, _)| s.parse::() == Ok(session))) + .map(|(binding, entry)| (binding.clone(), entry.origin.clone(), entry.method.clone())) + .collect(); + Ok(AccountIdentity { session, entries }) +} + +impl AccountIdentity { + /// Poison or any change to this session's active binding generations refuses later work. + pub fn is_current(&self) -> bool { + account_identity(self.session).is_ok_and(|current| current == *self) + } } /// Installs a fresh session/connection login generation. Only addresses bound to the returned @@ -275,6 +306,8 @@ fn append_percent_encoded(result: &mut String, value: &str) { /// Redacts active raw credentials, percent-encoded credentials (with either hex case), and /// the bare Base64 payload of Basic credentials. Matching the original input in one pass /// prevents overlapping values or a credential resembling `[redacted]` from leaking secrets. +/// Matching covers every active login in this process; short passwords can replace ordinary +/// text in another session's output. Revoked credentials are not retained for redaction. pub fn scrub(text: &str) -> String { let entries = entries(); if entries.is_empty() && !text.contains(rustgis_data::login_address::PREFIX) { @@ -550,6 +583,44 @@ mod tests { } } + /// Identity includes an empty session and ignores a different session with a shared prefix. + #[test] + fn runtime_identity_binds_session_generations_and_absence() { + let _turn = crate::cache::turn(); + let session = 9_150_001; + let empty = account_identity(session).unwrap(); + assert!(empty.is_current()); + let unrelated = + activate("https://identity-login.example", "91500010/other", Method::Header("X-Key".into()), "synthetic-identity-other", false) + .unwrap(); + assert!(empty.is_current()); + let binding = activate( + "https://identity-login.example", + "9150001/connection", + Method::Header("X-Key".into()), + "synthetic-identity-first", + false, + ) + .unwrap(); + assert!(!empty.is_current()); + let active = account_identity(session).unwrap(); + assert!(active.is_current() && active == account_identity(session).unwrap()); + assert!(deactivate(&unrelated)); + assert!(active.is_current()); + assert!(deactivate(&binding)); + assert!(!active.is_current()); + let replacement = activate( + "https://identity-login.example", + "9150001/connection", + Method::Header("X-Key".into()), + "synthetic-identity-first", + false, + ) + .unwrap(); + assert!(active != account_identity(session).unwrap(), "same credential is a new generation"); + assert!(deactivate(&replacement)); + } + #[test] fn scrubs_header_and_basic_payload() { let _turn = crate::cache::turn(); diff --git a/crates/tiles/src/mbtiles.rs b/crates/tiles/src/mbtiles.rs index 9ceb885c98..e6a77bfa3c 100644 --- a/crates/tiles/src/mbtiles.rs +++ b/crates/tiles/src/mbtiles.rs @@ -109,6 +109,7 @@ pub(crate) fn forget() { /// An archive's metadata. pub fn info(archive: &str) -> Result, String> { + crate::cache::check_still(&crate::cache::current_still())?; check(archive)?; if is_upload(archive) { return upload(archive) @@ -120,6 +121,7 @@ pub fn info(archive: &str) -> Result, String> { } let epoch = EPOCH.load(Ordering::SeqCst); let r = read_info(archive); + crate::cache::check_still(&crate::cache::current_still())?; remember_info(archive, &r, epoch); r } @@ -127,6 +129,9 @@ pub fn info(archive: &str) -> Result, String> { /// Keeps a completion only if no clear happened since its read started; check and insert share /// the same lock as the clear, so an old completion cannot repopulate the new cache. fn remember_info(archive: &str, result: &Result, String>, epoch: u64) { + if crate::cache::check_still(&crate::cache::current_still()).is_err() { + return; + } let mut all = infos().lock().unwrap_or_else(|e| e.into_inner()); if EPOCH.load(Ordering::SeqCst) != epoch { return; @@ -162,12 +167,20 @@ fn cached_info(archive: &str) -> Option, String>> { }) } +/// The metadata workers' shared pending reads and completion signal. +#[cfg(not(target_arch = "wasm32"))] +fn metadata_reads() -> &'static (Mutex>, std::sync::Condvar) { + static LOADING: OnceLock<(Mutex>, std::sync::Condvar)> = OnceLock::new(); + LOADING.get_or_init(|| (Mutex::new(HashMap::new()), std::sync::Condvar::new())) +} + /// Metadata without file I/O on the caller: local reads run in at most eight workers. A caller /// may wait up to `timeout`; otherwise `None` means pending. Completion wakes the map. Uploads /// are already in memory. At capacity a read remains pending until a later poll can start it. /// A read still pending after 30 seconds reports an error. A stalled OS read keeps its worker /// slot until it answers; those slots are never recycled into more potentially blocked threads. pub fn info_ready(archive: &str, timeout: Duration) -> Result>, String> { + crate::cache::check_still(&crate::cache::current_still())?; check(archive)?; if is_upload(archive) { return info(archive).map(Some); @@ -179,9 +192,7 @@ pub fn info_ready(archive: &str, timeout: Duration) -> Result>, } #[cfg(not(target_arch = "wasm32"))] { - use std::sync::Condvar; - static LOADING: OnceLock<(Mutex>, Condvar)> = OnceLock::new(); - let (pending, ready) = LOADING.get_or_init(|| (Mutex::new(HashMap::new()), Condvar::new())); + let (pending, ready) = metadata_reads(); let mut all = pending.lock().unwrap_or_else(|e| e.into_inner()); if let Some(result) = cached_info(archive) { return result.map(Some); @@ -197,8 +208,11 @@ pub fn info_ready(archive: &str, timeout: Duration) -> Result>, } all.insert(archive.to_string(), Instant::now()); let address = archive.to_string(); + let still = crate::cache::current_still(); let spawned = std::thread::Builder::new().name("rustgis-mbtiles-info".into()).spawn(move || { - let _ = info(&address); + crate::cache::with_optional_still(still, || { + let _ = info(&address); + }); pending.lock().unwrap_or_else(|e| e.into_inner()).remove(&address); ready.notify_all(); crate::cache::notify(); @@ -213,6 +227,9 @@ pub fn info_ready(archive: &str, timeout: Duration) -> Result>, let (reads, _) = ready.wait_timeout_while(all, wait, |all| all.contains_key(archive)).unwrap_or_else(|e| e.into_inner()); all = reads; } + drop(all); + crate::cache::check_still(&crate::cache::current_still())?; + let all = pending.lock().unwrap_or_else(|e| e.into_inner()); if let Some(result) = cached_info(archive) { return result.map(Some); } @@ -279,8 +296,74 @@ pub(crate) fn read_local(url: &str) -> Result, String> { mod tests { use super::*; + /// A reentrant authority check may clear metadata; the old epoch must still reject its result. + #[test] + fn metadata_completion_checks_still_outside_the_cache_lock() { + let _turn = crate::cache::turn(); + let available = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let seen = available.clone(); + let still: crate::cache::Still = Arc::new(move || { + let free = infos().try_lock().is_ok(); + seen.store(free, Ordering::SeqCst); + if free { + forget(); + } + true + }); + let epoch = EPOCH.load(Ordering::SeqCst); + let address = "/reentrant-cache-completion.mbtiles"; + crate::cache::with_still(still, || remember_info(address, &Err("old completion".into()), epoch)); + assert!(available.load(Ordering::SeqCst), "Still ran with the metadata cache locked"); + assert!(cached_info(address).is_none(), "a clear during the predicate invalidates the earlier completion"); + } + + /// Both preflight and post-wait authority checks permit metadata reentry without a lock cycle. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn metadata_ready_checks_still_outside_the_pending_lock() { + let _turn = crate::cache::turn(); + let origin = std::thread::current().id(); + let available = Arc::new(std::sync::atomic::AtomicBool::new(true)); + let checks = Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let (free, count) = (available.clone(), checks.clone()); + let still: crate::cache::Still = Arc::new(move || { + if std::thread::current().id() != origin { + return false; + } + count.fetch_add(1, Ordering::SeqCst); + free.fetch_and(metadata_reads().0.try_lock().is_ok(), Ordering::SeqCst); + true + }); + let _ = crate::cache::with_still(still, || info_ready("/reentrant-pending-read.mbtiles", Duration::ZERO)); + assert!(checks.load(Ordering::SeqCst) >= 2, "the post-wait check must execute"); + assert!(available.load(Ordering::SeqCst), "Still ran with pending metadata reads locked"); + } + + /// The metadata worker inherits its initiating transport context instead of reading at root. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn metadata_worker_keeps_still_and_drops_revoked_completion() { + let _turn = crate::cache::turn(); + let checks = Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let worker = checks.clone(); + let still: crate::cache::Still = Arc::new(move || { + if std::thread::current().name() == Some("rustgis-mbtiles-info") { + worker.fetch_add(1, Ordering::SeqCst); + return false; + } + true + }); + let address = "/revoked-metadata-worker.mbtiles"; + let got = crate::cache::with_still(still, || info_ready(address, Duration::from_secs(10))); + assert!(matches!(got, Ok(None)), "revoked work must not populate metadata"); + assert!(checks.load(Ordering::SeqCst) > 0, "the real worker checked the inherited context"); + assert!(cached_info(address).is_none()); + assert!(crate::cache::current_still().is_none()); + } + #[test] fn cleared_metadata_rejects_an_older_completion() { + let _turn = crate::cache::turn(); let epoch = EPOCH.load(Ordering::SeqCst); forget(); remember_info("/old-completion.mbtiles", &Err("obsolete read".into()), epoch); diff --git a/crates/tiles/src/picture.rs b/crates/tiles/src/picture.rs index d646e4c8d8..3e0deb9902 100644 --- a/crates/tiles/src/picture.rs +++ b/crates/tiles/src/picture.rs @@ -619,6 +619,7 @@ mod tests { #[test] fn pictures_draw_what_the_style_draws() { + let _turn = crate::cache::turn(); let (spec, dir) = spec("same"); let r = resolved(&spec); // Zooms whose pictures are a whole number of pixels across, so they are at the view's @@ -654,6 +655,7 @@ mod tests { #[test] fn pictures_drawn_stand_in_while_the_map_moves() { + let _turn = crate::cache::turn(); let (spec, dir) = spec("moving"); let r = resolved(&spec); let f = frame(2.2, 1.0); @@ -703,6 +705,7 @@ mod tests { #[test] fn pictures_are_drawn_a_round_at_least_in_a_frame() { + let _turn = crate::cache::turn(); let (spec, dir) = spec("round"); let r = resolved(&spec); let squares: Vec = template::covering(&frame(3.0, 1.0).extent(300, 200), 4); @@ -720,6 +723,7 @@ mod tests { #[test] fn pictures_are_drawn_once() { + let _turn = crate::cache::turn(); let (spec, dir) = spec("once"); resolved(&spec); let f = frame(2.2, 1.0); diff --git a/crates/tiles/src/pmtiles.rs b/crates/tiles/src/pmtiles.rs index 8a5cb2dc5c..0de9570d29 100644 --- a/crates/tiles/src/pmtiles.rs +++ b/crates/tiles/src/pmtiles.rs @@ -16,7 +16,7 @@ use std::sync::{Arc, Mutex, OnceLock}; use serde_json::Value; use web_time::{Duration, Instant}; -use crate::cache::{Done, MAX_BYTES}; +use crate::cache::{Done, MAX_BYTES, caller_callback}; /// The scheme of a tile address read from a PMTiles archive. pub const SCHEME: &str = "pmtiles:"; @@ -445,6 +445,7 @@ pub fn info_blocking(archive: &str, timeout: std::time::Duration) -> Result, String>) + Send>) { + let f = caller_callback(f); if let Err(e) = crate::logins::check(archive) { f(Err(e.to_string())); return; @@ -493,6 +494,12 @@ fn load(archive: String) { meta_at, meta_len, Box::new(move |m| { + if let Err(e) = &m + && !crate::cache::keep_failure(e, &crate::cache::current_still()) + { + finish(&a2, Err(e.clone())); + return; + } // Unreadable metadata only loses the layer names and credits. if let Ok(m) = m { let m = info.internal.decode(m).unwrap_or_default(); @@ -507,19 +514,22 @@ fn load(archive: String) { ); } +/// Settles archive waiters with live results, forgetting dropped or retired reads. fn finish(archive: &str, r: Result) { + let r = crate::cache::check_still(&crate::cache::current_still()).and(r); let r = crate::logins::check(archive) .map_err(str::to_string) .and(r) .map(Arc::new) .map_err(|e| if rustgis_data::login_address::is_wrapper(archive) { crate::logins::scrub_error(&e) } else { e }); + let transient = r.as_ref().err().is_some_and(|e| !crate::cache::keep_failure(e, &crate::cache::current_still())); let waiting = { let mut all = archives().lock().unwrap_or_else(|e| e.into_inner()); let old = match &r { Ok(i) => all.insert(archive.to_string(), State::Ready(i.clone())), - // A read the busy queue dropped isn't the archive's fault: forgotten, so the next - // draw reads it again, rather than kept as a failure. - Err(e) if e == crate::cache::DROPPED => all.remove(archive), + // A dropped or retired owner's read isn't the archive's fault: the next caller + // starts its own read instead of inheriting a shared retry delay. + Err(_) if transient => all.remove(archive), Err(e) => { // Read again once its wait is over: the map draws again then (a 429 or 503 says // when). @@ -601,6 +611,7 @@ fn find(archive: String, info: Arc, id: u64, dir: Arc>, depth: Err(e) => done(Err(e)), }) }; + let next = caller_callback(next); // One read per leaf: tiles needing a leaf already on its way wait for it. { let mut l = leaves().lock().unwrap_or_else(|e| e.into_inner()); @@ -632,6 +643,7 @@ fn find(archive: String, info: Arc, id: u64, dir: Arc>, depth: e.length, Box::new(move |b| { let leaf = b.and_then(|b| info.internal.decode(b)).and_then(|b| parse_dir(&b)).map(Arc::new); + let leaf = crate::cache::check_still(&crate::cache::current_still()).and(leaf); let waiting = { let mut l = leaves().lock().unwrap_or_else(|e| e.into_inner()); let w = match &leaf { @@ -833,6 +845,150 @@ fn gunzip(b: &[u8]) -> Result, String> { pub(crate) mod tests { use super::*; + /// Revoked metadata/directory reads and dropped metadata cannot become cached successes. + /// Ordinary metadata failures retain the fallback; a fresh read restores the real style. + #[test] + fn retired_metadata_does_not_become_ready_archive_info() { + use std::sync::atomic::{AtomicBool, Ordering::SeqCst}; + let _turn = crate::cache::turn(); + let held = Arc::new(Mutex::new(Vec::<(String, Done)>::new())); + let hold = held.clone(); + crate::cache::set_fetcher(move |url, done| hold.lock().unwrap().push((url, done))); + let bytes = archive(&[(0, 0, 0, Vec::new())], 1, false, r#"{"vector_layers":[{"id":"roads"}],"attribution":"Test"}"#); + let answer = |url: String, done: Done| { + let (_, range) = crate::cache::split_range(&url); + let (a, b) = range.unwrap(); + done(Ok(bytes[a as usize..((b + 1) as usize).min(bytes.len())].to_vec())); + }; + let mut failures = Vec::new(); + for revoke in [false, true] { + for (case, error) in [None, Some("HTTP 503"), Some(crate::cache::DROPPED)].into_iter().enumerate() { + let url = format!("https://retired-metadata.invalid/{revoke}-{case}.pmtiles"); + let live = Arc::new(AtomicBool::new(true)); + let flag = live.clone(); + assert!(crate::cache::with_still(Arc::new(move || flag.load(SeqCst)), || info(&url)).unwrap().is_none()); + let (asked, done) = held.lock().unwrap().pop().unwrap(); + answer(asked, done); + assert_eq!(held.lock().unwrap().len(), 1, "metadata is a separate real range read"); + live.store(!revoke, SeqCst); + let (asked, done) = held.lock().unwrap().pop().unwrap(); + if let Some(error) = error { + done(Err(error.into())) + } else { + answer(asked, done) + } + let retry = revoke || error == Some(crate::cache::DROPPED); + if retry { + if archives().lock().unwrap().contains_key(&url) { + failures.push(format!("retired/dropped metadata cached with revoke={revoke}, error={error:?}")); + archives().lock().unwrap().remove(&url); + } + assert!(info(&url).unwrap().is_none()); + for _ in 0..2 { + let (asked, done) = held.lock().unwrap().pop().unwrap(); + answer(asked, done); + } + } + let got = info(&url).unwrap().unwrap(); + if retry || error.is_none() { + assert_eq!(got.vector_layers(), vec!["roads"]); + assert_eq!(got.attribution(), "Test"); + } else { + assert!(got.vector_layers().is_empty(), "a live metadata error keeps the existing fallback"); + } + assert!(held.lock().unwrap().is_empty()); + } + } + // A ready header can start a separate directory read. Revocation during that read + // must not leave old directory bytes for another caller, even though its tile fails. + let bytes = archive(&[(0, 0, 0, b"tile".to_vec())], 1, true, "{}"); + let answer = |url: String, done: Done| { + let (_, range) = crate::cache::split_range(&url); + let (a, b) = range.unwrap(); + done(Ok(bytes[a as usize..((b + 1) as usize).min(bytes.len())].to_vec())); + }; + let url = "https://retired-metadata.invalid/directory.pmtiles"; + assert!(info(url).unwrap().is_none()); + for _ in 0..2 { + let (asked, done) = held.lock().unwrap().pop().unwrap(); + answer(asked, done); + } + let live = Arc::new(AtomicBool::new(true)); + let flag = live.clone(); + let (done, result) = std::sync::mpsc::channel(); + let tile = format!("pmtiles:0/0/0@{url}"); + crate::cache::with_still(Arc::new(move || flag.load(SeqCst)), || fetch(&tile, Box::new(move |r| done.send(r).unwrap()))); + assert_eq!(held.lock().unwrap().len(), 1); + live.store(false, SeqCst); + let (asked, done) = held.lock().unwrap().pop().unwrap(); + answer(asked, done); + assert!(result.recv().unwrap().is_err()); + if leaves().lock().unwrap().keys().any(|(a, _)| a == url) { + failures.push("a retired reader cached a successful leaf directory".into()); + forget_archive(url); + } + let (done, result) = std::sync::mpsc::channel(); + fetch(&tile, Box::new(move |r| done.send(r).unwrap())); + for _ in 0..4 { + let next = held.lock().unwrap().pop(); + let Some((asked, done)) = next else { break }; + answer(asked, done); + } + assert_eq!(result.recv().unwrap().unwrap(), b"tile"); + assert!(failures.is_empty(), "{failures:?}"); + } + + /// A metadata or directory joiner retains its own lifetime before starting another range; + /// revoking it does not stop the independent live loader or its tile. + #[test] + fn shared_archive_waiters_keep_their_own_lifetime() { + use std::sync::atomic::{AtomicBool, Ordering::SeqCst}; + let _turn = crate::cache::turn(); + let bytes = archive(&[(0, 0, 0, b"tile".to_vec())], 1, true, "{}"); + let held = Arc::new(Mutex::new(Vec::<(String, Done)>::new())); + let hold = held.clone(); + crate::cache::set_fetcher(move |url, done| hold.lock().unwrap().push((url, done))); + let drain = || { + let mut sent = 0; + loop { + let pending = std::mem::take(&mut *held.lock().unwrap()); + if pending.is_empty() { + return sent; + } + for (url, done) in pending { + sent += 1; + assert!(sent <= 8, "bounded directory fixture"); + let (_, range) = crate::cache::split_range(&url); + let (a, b) = range.unwrap(); + done(Ok(bytes[a as usize..((b + 1) as usize).min(bytes.len())].to_vec())); + } + } + }; + let mut failures = Vec::new(); + for join_leaf in [false, true] { + let url = format!("https://shared-pmtiles.invalid/{join_leaf}.pmtiles"); + if join_leaf { + assert!(info(&url).unwrap().is_none()); + assert_eq!(drain(), 2); + } + let tile = format!("pmtiles:0/0/0@{url}"); + let (first, first_result) = std::sync::mpsc::channel(); + fetch(&tile, Box::new(move |r| first.send(r).unwrap())); + let live = Arc::new(AtomicBool::new(true)); + let flag = live.clone(); + let (done, result) = std::sync::mpsc::channel(); + crate::cache::with_still(Arc::new(move || flag.load(SeqCst)), || fetch(&tile, Box::new(move |r| done.send(r).unwrap()))); + assert_eq!(held.lock().unwrap().len(), 1, "the joiner shares the pending read"); + live.store(false, SeqCst); + let sent = drain(); + assert_eq!(first_result.recv().unwrap().unwrap(), b"tile"); + if result.recv().unwrap().is_ok() || sent != if join_leaf { 2 } else { 4 } { + failures.push(format!("join_leaf={join_leaf}: revoked joiner succeeded or sent extra ranges ({sent})")); + } + } + assert!(failures.is_empty(), "{failures:?}"); + } + #[test] fn tile_ids_follow_the_spec() { assert_eq!(tile_id(0, 0, 0), 0); @@ -966,6 +1122,7 @@ pub(crate) mod tests { #[test] fn written_archives_read_back() { + let _turn = crate::cache::turn(); let dir = std::env::temp_dir().join(format!("rustgis-pmtiles-write-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); let new = |tile_type| NewArchive { @@ -1014,6 +1171,7 @@ pub(crate) mod tests { #[test] fn reads_tiles_from_root_and_leaf_directories() { + let _turn = crate::cache::turn(); let dir = std::env::temp_dir().join(format!("rustgis-pmtiles-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); let tiles = vec![(0u8, 0u32, 0u32, b"zero".to_vec()), (1, 1, 0, b"one-one-zero".to_vec()), (2, 3, 3, b"two".to_vec())]; diff --git a/crates/tiles/src/s3.rs b/crates/tiles/src/s3.rs index 9c566821db..bcbc1b00cc 100644 --- a/crates/tiles/src/s3.rs +++ b/crates/tiles/src/s3.rs @@ -103,6 +103,11 @@ fn lock() -> std::sync::MutexGuard<'static, Registry> { REGISTRY.lock().unwrap_or_else(|e| e.into_inner()) } +/// Reads selection state without recovering a poisoned store as fresh authority. +fn read_registry() -> Result, String> { + REGISTRY.lock().map_err(|_| "active S3 connection store unavailable".to_string()) +} + /// A connection as the registry keeps it: name, endpoint, region, buckets and credentials checked. fn checked(mut c: Connection) -> Result { c.name = c.name.trim().to_string(); @@ -251,9 +256,19 @@ pub fn disconnect(name: &str) -> bool { reg.connections.len() != before } -fn granted() -> std::sync::MutexGuard<'static, Vec<(String, String)>> { +/// Shares the existing bucket grants between requests and pure identity snapshots. +fn grant_store() -> &'static Mutex> { static G: std::sync::OnceLock>> = std::sync::OnceLock::new(); - G.get_or_init(Default::default).lock().unwrap_or_else(|e| e.into_inner()) + G.get_or_init(Default::default) +} + +fn granted() -> std::sync::MutexGuard<'static, Vec<(String, String)>> { + grant_store().lock().unwrap_or_else(|e| e.into_inner()) +} + +/// Captures grants with the same fail-closed lock behavior as account selection. +fn read_grants() -> Result>, String> { + grant_store().lock().map_err(|_| "active S3 bucket grants unavailable".to_string()) } /// Lets a connection serve a bucket for the rest of the session: the user browsed or added it @@ -270,8 +285,13 @@ pub fn grant(name: &str, bucket: &str) { } /// Whether a connection serves a bucket: it was given the bucket, or the bucket was granted to it. -fn serves(c: &Connection, bucket: &str) -> bool { - c.buckets.iter().any(|b| b == bucket) || granted().iter().any(|(n, b)| *n == c.name && b == bucket) +fn serves(c: &Connection, bucket: &str, grants: &[(String, String)]) -> bool { + c.buckets.iter().any(|b| b == bucket) || grants.iter().any(|(n, b)| *n == c.name && b == bucket) +} + +/// Selects the first implicit account serving this bucket under the captured grants. +fn bucket_connection(reg: &Registry, grants: &[(String, String)], bucket: &str) -> Option { + reg.implicit().find(|c| serves(c, bucket, grants)).cloned() } /// The session's connections. @@ -382,7 +402,8 @@ pub fn parse(url: &str) -> Result<(String, String), String> { /// connection's login serves no bucket this way: its addresses name it ([`connect_saved`]). pub fn for_bucket(bucket: &str) -> Option { let reg = lock(); - reg.implicit().find(|c| serves(c, bucket)).cloned() + let grants = granted(); + bucket_connection(®, &grants, bucket) } /// Starts an address read anonymously whatever the connections: nothing signed, and an `s3://` @@ -525,7 +546,7 @@ fn bound_target(address: &str) -> Result<(String, Connection), String> { // Snapshot only the selected login and this request's explicit grant. No URL parsing, // origin validation or signing runs under the registry lock, and the grant list isn't copied. let snapshot = |bucket: Option<&str>| -> Result<(Connection, bool), String> { - let reg = lock(); + let reg = read_registry()?; let saved = reg.saved.iter().find(|s| s.binding == b.binding).ok_or(UNRESOLVED)?; let c = reg.connections.iter().find(|c| c.name == saved.name).ok_or(UNRESOLVED)?; let granted = bucket.is_some_and(|bucket| saved.grants.iter().any(|g| g == bucket)); @@ -582,23 +603,59 @@ pub struct Prepared { pub signed: bool, } -/// Makes a GET of `url` ready: an `s3://` address becomes its web address, and a request to a -/// connection's endpoint (or to AWS S3 for an AWS connection) is signed with its credentials. -/// An address read through a saved connection is signed by that connection alone -/// ([`bound_target`]), and never read without it. Other addresses pass unchanged. -pub fn prepare(url: &str) -> Result { +/// An exact in-memory S3 selection, never a display or persistence representation. +/// Known addresses retain only their selected account or its absence; unknown routes retain all state. +#[derive(Clone, PartialEq, Eq)] +pub struct AccountIdentity(Identity); + +#[derive(Clone, PartialEq, Eq)] +enum Identity { + All { connections: Vec, saved: Vec, grants: Vec<(String, String)> }, + Address { url: String, choice: Choice }, +} + +/// Captures all active selection state for a route whose eventual addresses are unknown. +/// No I/O or signing occurs; a poisoned registry or grant store is an error. +pub fn account_identity() -> Result { + let reg = read_registry()?; + let grants = read_grants()?; + Ok(AccountIdentity(Identity::All { connections: reg.connections.clone(), saved: reg.saved.clone(), grants: grants.clone() })) +} + +/// Captures the account selected by `prepare`, including absence and explicit anonymous reads. +/// Pass the same address as `prepare`; service-login wrappers are removed by its caller. +/// No I/O or signing occurs; unavailable relevant state is an error, never an absent account. +pub fn account_identity_for(url: &str) -> Result { + Ok(AccountIdentity(Identity::Address { url: url.to_string(), choice: choice(url)? })) +} + +impl AccountIdentity { + /// Re-selects without I/O; poison, rotation, replacement or newly covering accounts refuse later work. + pub fn is_current(&self) -> bool { + let current = match &self.0 { + Identity::All { .. } => account_identity(), + Identity::Address { url, .. } => account_identity_for(url), + }; + current.is_ok_and(|current| current == *self) + } +} + +/// The sender and identity capture share this unsigned destination/account selection. +#[derive(Clone, PartialEq, Eq)] +struct Choice { + url: String, + signer: Option<(Connection, String)>, +} + +/// Resolves the destination and signer once, without clock-dependent signing or I/O. +fn choice(url: &str) -> Result { if rustgis_data::s3_address::is_wrapper(url) { let (target, c) = bound_target(url)?; - let Some(cr) = c.credentials.as_ref() else { - return Ok(Prepared { url: target, headers: Vec::new(), signed: false }); - }; let region = match c.endpoint { Some(_) => c.region.clone(), None => aws_region(&target.to_ascii_lowercase(), &c.region), }; - let url = canonical_url(&target)?; - let headers = sign_get(&url, ®ion, cr, &amz_date(now_secs()))?; - return Ok(Prepared { url, headers, signed: true }); + return Ok(Choice { url: target, signer: Some((c, region)) }); } let (url, anonymous) = split_anonymous(url); if anonymous && rustgis_data::s3_address::is_wrapper(url) { @@ -606,12 +663,27 @@ pub fn prepare(url: &str) -> Result { } let (url, signer) = if is_s3(url) { let (bucket, key) = parse(url)?; - let conn = if anonymous { None } else { for_bucket(&bucket) }; + let conn = if anonymous { + None + } else { + let reg = read_registry()?; + let grants = read_grants()?; + bucket_connection(®, &grants, &bucket) + }; let region = conn.as_ref().map(|c| c.region.clone()).unwrap_or_default(); (object_url(&bucket, &key, conn.as_ref()), conn.map(|c| (c, region))) } else { - (url.to_string(), if anonymous { None } else { signer_for(url) }) + (url.to_string(), if anonymous { None } else { signer_for(url)? }) }; + Ok(Choice { url, signer }) +} + +/// Makes a GET of `url` ready: an `s3://` address becomes its web address, and a request to a +/// connection's endpoint (or to AWS S3 for an AWS connection) is signed with its credentials. +/// An address read through a saved connection is signed by that connection alone +/// ([`bound_target`]), and never read without it. Other addresses pass unchanged. +pub fn prepare(url: &str) -> Result { + let Choice { url, signer } = choice(url)?; let Some((c, region)) = signer else { return Ok(Prepared { url, headers: Vec::new(), signed: false }); }; @@ -681,37 +753,45 @@ fn aws_region(url: &str, default: &str) -> String { /// one whose endpoint is the address's scheme and authority (default ports aside), or (an AWS /// connection) an address at AWS S3 of a bucket the connection serves ([`for_bucket`]): never /// another bucket at amazonaws.com. A saved connection's login never signs by address. -fn signer_for(url: &str) -> Option<(Connection, String)> { +fn signer_for(url: &str) -> Result, String> { let lower = url.to_ascii_lowercase(); - let (scheme, rest) = lower.split_once("://")?; - let authority = rest.split(['/', '?', '#']).next()?; + let Some((scheme, rest)) = lower.split_once("://") else { return Ok(None) }; + let authority = rest.split(['/', '?', '#']).next().unwrap_or(""); if authority.contains('@') { - return None; + return Ok(None); } let here = origin(scheme, authority); - let reg = lock(); - if let Some(c) = reg.implicit().find(|c| c.credentials.is_some() && c.endpoint.as_deref() == Some(here.as_str())) { - return Some((c.clone(), c.region.clone())); - } let host = authority.split(':').next().unwrap_or(authority); - if !aws_host(scheme, &here, host) { - return None; - } - let c = match aws_bucket(host, rest) { - Some(bucket) => reg.implicit().find(|c| c.credentials.is_some() && c.endpoint.is_none() && serves(c, &bucket))?, - // The service itself (`https://s3..amazonaws.com/`, no bucket): the account's - // bucket list (`buckets_url`), signed by an AWS connection of that region. It names no - // bucket, so no one else's server sees the request. - None => { - let path = rest.split_once('/').map(|(_, p)| p).unwrap_or(""); - let region = host.strip_prefix("s3.").and_then(|r| r.strip_suffix(".amazonaws.com"))?; - if !path.split(['?', '#']).next().unwrap_or("").is_empty() { - return None; + let aws = aws_host(scheme, &here, host); + let bucket = aws_bucket(host, rest); + let list_region = host.strip_prefix("s3.").and_then(|r| r.strip_suffix(".amazonaws.com")); + let path = rest.split_once('/').map(|(_, p)| p).unwrap_or(""); + let list_root = path.split(['?', '#']).next().unwrap_or("").is_empty(); + // Parse before locking; only selected connections are cloned while the registry is held. + let c = { + let reg = read_registry()?; + if let Some(c) = reg.implicit().find(|c| c.credentials.is_some() && c.endpoint.as_deref() == Some(here.as_str())) { + return Ok(Some((c.clone(), c.region.clone()))); + } + if !aws { + return Ok(None); + } + match bucket { + Some(bucket) => { + let grants = read_grants()?; + reg.implicit().find(|c| c.credentials.is_some() && c.endpoint.is_none() && serves(c, &bucket, &grants)).cloned() + } + // The service itself names no bucket: only an AWS connection of its region signs its root. + None => { + let Some(region) = list_region.filter(|_| list_root) else { return Ok(None) }; + reg.implicit().find(|c| c.credentials.is_some() && c.endpoint.is_none() && c.region == region).cloned() } - reg.implicit().find(|c| c.credentials.is_some() && c.endpoint.is_none() && c.region == region)? } }; - Some((c.clone(), aws_region(&lower, &c.region))) + Ok(c.map(|c| { + let region = aws_region(&lower, &c.region); + (c, region) + })) } /// The bucket an AWS S3 address names: its host's first label (virtual-hosted) or its path's diff --git a/crates/tiles/src/shapes.rs b/crates/tiles/src/shapes.rs index 3214359339..a105200388 100644 --- a/crates/tiles/src/shapes.rs +++ b/crates/tiles/src/shapes.rs @@ -506,6 +506,7 @@ mod tests { #[test] fn shapes_are_what_the_style_draws() { + let _turn = crate::cache::turn(); let (spec, dir) = archive("draws", STYLE); ready(&spec); let v = tile(&spec, 2.5); @@ -533,6 +534,7 @@ mod tests { #[test] fn patterns_without_a_sprite_and_line_patterns_are_left_out() { + let _turn = crate::cache::turn(); let style = STYLE.replacen( '[', r#"[{"id": "p", "type": "fill", "source": "x", "source-layer": "parks", "paint": {"fill-pattern": "dots"}}, @@ -551,6 +553,7 @@ mod tests { #[test] fn a_pattern_chosen_with_image_asks_the_loaded_sprite() { + let _turn = crate::cache::turn(); let pattern = |name: &str, value: &str| { let style = STYLE.replacen( '[', @@ -588,6 +591,7 @@ mod tests { #[test] fn a_pattern_changing_with_the_zoom_is_drawn_zoom_by_zoom() { + let _turn = crate::cache::turn(); let source = |name: &str, value: &str| { let style = STYLE.replacen( '[', @@ -609,6 +613,7 @@ mod tests { #[test] fn a_step_at_the_next_level_holds_over_this_one() { + let _turn = crate::cache::turn(); let style = STYLE.replace(r#""line-width": 2,"#, r#""line-width": ["step", ["zoom"], 2, 3, 6],"#); let (spec, dir) = archive("step", &style); ready(&spec); @@ -623,6 +628,7 @@ mod tests { #[test] fn styles_are_judged_over_the_whole_level() { + let _turn = crate::cache::turn(); // A raster layer from the middle of the level: its meshes would serve where it shows. let style = STYLE.replacen('[', r#"[{"id": "r", "type": "raster", "source": "x", "minzoom": 2.8},"#, 1); let (spec, dir) = archive("level", &style); @@ -661,6 +667,7 @@ mod tests { #[test] fn styles_with_long_dash_patterns_are_declined() { + let _turn = crate::cache::turn(); // Three lengths repeat to six: more than the meshes hold. let style = STYLE.replace(r#""line-dasharray": [3, 2]"#, r#""line-dasharray": [3, 2, 1]"#); let (spec, dir) = archive("dashes", &style); @@ -682,6 +689,7 @@ mod tests { #[test] fn styles_drawn_otherwise_are_declined() { + let _turn = crate::cache::turn(); // A raster layer is a picture of its own. let style = STYLE.replacen('[', r#"[{"id": "r", "type": "raster", "source": "x"},"#, 1); let (spec, dir) = archive("raster", &style); diff --git a/crates/tiles/src/source.rs b/crates/tiles/src/source.rs index 8b4671a04f..ac6f5387c7 100644 --- a/crates/tiles/src/source.rs +++ b/crates/tiles/src/source.rs @@ -671,6 +671,7 @@ mod tests { #[test] fn resolves_raster_and_vector_templates_at_once() { + let _turn = crate::cache::turn(); let Resolve::Ready(r) = resolve(&spec(Kind::Raster, "https://{s}.tile.example.com/{z}/{x}/{y}.png")) else { panic!("not ready") }; assert_eq!(r.attribution, vec!["© OSM".to_string()]); match r.style.sources.first().map(|s| &s.1) { @@ -702,6 +703,7 @@ mod tests { #[test] fn own_style_layers_draw_vector_tiles() { + let _turn = crate::cache::turn(); let mut own = spec(Kind::Vector, "https://tiles.example.com/fields/{z}/{x}/{y}.pbf"); own.style_layers = r##"[{"id": "f", "type": "fill", "source": "elsewhere", "source-layer": "fields", "paint": {"fill-color": ["step", ["get", "score"], "#d73027", 50, "#1a9850"]}}]"## @@ -755,6 +757,7 @@ mod tests { #[test] fn refuses_forbidden_addresses() { + let _turn = crate::cache::turn(); assert!(matches!(resolve(&spec(Kind::Raster, "http://169.254.169.254/{z}/{x}/{y}.png")), Resolve::Failed(_))); // A self-hosted server the user names is fine. assert!(matches!(resolve(&spec(Kind::Raster, "http://localhost:8080/{z}/{x}/{y}.png")), Resolve::Ready(_))); diff --git a/crates/tiles/src/sprite.rs b/crates/tiles/src/sprite.rs index 474b0c2884..6d2605411d 100644 --- a/crates/tiles/src/sprite.rs +++ b/crates/tiles/src/sprite.rs @@ -351,6 +351,7 @@ mod tests { #[test] fn forgetting_drops_the_decoded_sheets() { + let _turn = crate::cache::turn(); let key = "forget-test|".to_string(); let sprite = Sheet { sheet: sheet(), index: Default::default(), cut: Default::default() }; sprites().lock().unwrap().insert(key.clone(), Arc::new(sprite)); diff --git a/crates/ui-egui/src/agentthreads.rs b/crates/ui-egui/src/agentthreads.rs index 66633ddc1d..eb05d10d5a 100644 --- a/crates/ui-egui/src/agentthreads.rs +++ b/crates/ui-egui/src/agentthreads.rs @@ -62,6 +62,31 @@ pub struct Threads { relay_ours: bool, } +impl Threads { + /// Publish the shared approval setting synchronously to every live ACP session. + pub(crate) fn set_auto(&self, auto: bool) { + for t in &self.runs { + t.run.set_auto(auto); + } + } +} + +/// Put the real process fixture into the same live-thread collection the checkbox updates. +#[cfg(test)] +pub(crate) fn insert_test_run(app: &mut RustGisApp, run: Run) { + app.assistant.threads.runs.push(ThreadRun { + id: "goose-1".into(), + label: "goose", + run, + feed: Vec::new(), + requests: Vec::new(), + working: true, + since: web_time::Instant::now(), + window: false, + map_seen: None, + }); +} + impl Drop for Threads { fn drop(&mut self) { // Ordinary Close/New conversation clears runs without blocking. Actual application @@ -80,15 +105,15 @@ fn cli() -> Option { /// Starts the tasks the assistant handed out, passes on what threads did, asked and reported, /// and gives the assistant the reports once it's free. pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { + // Apply restored/programmatic setting changes before queued tasks or follow-ups start. + let auto = app.assistant.agents_auto; + app.assistant.threads.set_auto(auto); let delegations = app.assistant.agent.as_mut().map(|a| a.take_delegations()).unwrap_or_default(); for d in delegations { start(app, d, ctx); } - let auto = app.assistant.agents_auto; let (mut reports, mut closed) = (Vec::new(), Vec::new()); for t in &mut app.assistant.threads.runs { - // The user's setting reaches every thread: its next task's mode, and requests waiting. - t.run.set_auto(auto); let newest = t.run.map_approvals().iter().map(|a| a.id).max(); if newest > t.map_seen { t.window = true; @@ -151,8 +176,9 @@ fn deliver(app: &mut RustGisApp, ctx: &egui::Context) { /// Starts a thread on a task, or sends a follow-up to one. fn start(app: &mut RustGisApp, d: rustgis_assistant::agent::Delegation, ctx: &egui::Context) { if d.follow_up { + let auto = app.assistant.agents_auto; let sent = app.assistant.threads.runs.iter_mut().find(|t| t.id == d.thread).is_some_and(|t| { - let sent = !t.working && t.run.follow_up(&d.task); + let sent = !t.working && t.run.follow_up(&d.task, auto); if sent { t.working = true; t.window = true; @@ -316,9 +342,27 @@ pub fn windows(app: &mut RustGisApp, ctx: &egui::Context) { if let Some(p) = th.run.map_approvals().first() { egui::Frame::NONE.fill(t.amber.gamma_multiply(0.15)).corner_radius(6).inner_margin(8).show(ui, |ui| { ui.label(RichText::new(tl!("Allow this agent's RustGIS action?")).strong()); - // The whole request (up to 4 KB), scrolled rather than cut. + if th.run.map_approvals().len() > 1 { + ui.label(crate::i18n::fmt("{count} requests waiting", &[("count", &th.run.map_approvals().len().to_string())])); + } + // Overview and exact Details share one scroll area; controls stay outside. egui::ScrollArea::vertical().max_height(160.0).id_salt(("acp_approval", p.id)).show(ui, |ui| { - ui.label(RichText::new(&p.summary).monospace().small()); + if p.reviews.is_empty() { + ui.label(RichText::new(&p.summary).monospace().small()); + } else { + for (index, review) in p.reviews.iter().enumerate() { + ui.push_id(index, |ui| { + if p.reviews.len() > 1 { + ui.separator(); + ui.label(crate::i18n::fmt( + "Request {current} of {count}", + &[("current", &(index + 1).to_string()), ("count", &p.reviews.len().to_string())], + )); + } + crate::approvalcard::show(ui, &app.session.registry, review); + }); + } + } }); ui.horizontal(|ui| { if ui.button(tl!("Allow")).clicked() { diff --git a/crates/ui-egui/src/analysispane.rs b/crates/ui-egui/src/analysispane.rs index 463f564187..209a6a89bc 100644 --- a/crates/ui-egui/src/analysispane.rs +++ b/crates/ui-egui/src/analysispane.rs @@ -329,15 +329,15 @@ pub fn land_job(app: &mut RustGisApp) -> bool { match job.kind { JobKind::Tool { id, slot } => { let Some(done) = take(&slot) else { return false }; - let r = gp::tool_complete(&mut app.session, id, done).and_then(|_| app.run("gp.finish", json!({"id": id}))); + let r = gp::tool_complete(&mut app.session, id, done).and_then(|_| app.finish_gp(id)); show(app, r); } - JobKind::Raster { id, command, mut params, slot } => match take(&slot) { + JobKind::Raster { id, command, params, slot } => match take(&slot) { // Added by its command, as if it had run here. Some(Ok(made)) => match workflows::raster_land(&mut app.session, id, made) { Ok(true) => { - params["ran"] = json!(id); - run_now(app, command, params); + let result = app.finish_raster(command, id, ¶ms); + show(app, result); } Ok(false) => dropped(app, job.name), Err(e) => show(app, Err(e)), diff --git a/crates/ui-egui/src/approvalcard.rs b/crates/ui-egui/src/approvalcard.rs new file mode 100644 index 0000000000..25c01254fb --- /dev/null +++ b/crates/ui-egui/src/approvalcard.rs @@ -0,0 +1,93 @@ +//! Shared presentation of frozen engine reviews; display data never grants approval. + +use egui::{RichText, Ui}; +use rustgis_engine::{Registry, policy::Review}; + +/// Registry labels name operations without interpreting the request's script or parameters. +fn operation(registry: &Registry, id: &str) -> String { + registry.get(id).map_or_else(|| id.to_string(), |spec| tl_raw!(spec.label).to_string()) +} + +/// Shows consequences first and keeps the complete sanitized vehicle under Details. +/// Callers bound the scroll area and keep their existing approval controls outside it. +pub(crate) fn show(ui: &mut Ui, registry: &Registry, review: &Review) { + ui.push_id((&review.what, &review.steps, &review.defaults, &review.lines), |ui| { + let mut operations = Vec::new(); + for id in review.details.iter().map(|d| &d.operation).chain(review.lines.iter().map(|(_, id)| id)) { + if !id.is_empty() && !operations.contains(id) { + operations.push(id.clone()); + } + } + let actions = if operations.is_empty() { + tl_raw!("Review this request").to_string() + } else { + operations.iter().map(|id| operation(registry, id)).collect::>().join(", ") + }; + let title = match review.edits { + 0 => crate::i18n::fmt("{actions}", &[("actions", &actions)]), + 1 => crate::i18n::fmt("{actions} · 1 project edit", &[("actions", &actions)]), + count => crate::i18n::fmt("{actions} · {count} project edits", &[("actions", &actions), ("count", &count.to_string())]), + }; + ui.add(egui::Label::new(RichText::new(title).strong()).wrap()); + // These are starting defaults, not an exhaustive target list for a composite request. + if let Some((active, selected)) = &review.defaults { + ui.add( + egui::Label::new(crate::i18n::fmt( + "Starting active layer: {active}; selected layers: [{selected}]", + &[("active", &active.clone().unwrap_or_else(|| tl_raw!("(none)").to_string())), ("selected", &selected.join(", "))], + )) + .wrap(), + ); + } + ui.add(egui::Label::new(RichText::new(reason(review)).small()).wrap()); + for detail in &review.details { + let name = operation(registry, &detail.operation); + if !detail.targets.is_empty() { + ui.add( + egui::Label::new(crate::i18n::fmt( + "Target details ({operation}): {targets}", + &[("operation", &name), ("targets", &detail.targets.join(", "))], + )) + .wrap(), + ); + } + if !detail.places.is_empty() { + ui.add( + egui::Label::new(crate::i18n::fmt( + "Destinations ({operation}): {places}", + &[("operation", &name), ("places", &detail.places.join(", "))], + )) + .wrap(), + ); + } + } + egui::CollapsingHeader::new(tl!("Details")).default_open(false).show(ui, |ui| { + ui.label(tl!("Exact request")); + ui.add(egui::Label::new(RichText::new(&review.what).monospace().small()).wrap().selectable(true)); + // Direct commands repeat their vehicle as an asking step; show it only once. + let steps = review.steps.iter().filter(|step| *step != &review.what).collect::>(); + if !steps.is_empty() { + ui.label(tl!("Steps needing access")); + for step in steps { + ui.add(egui::Label::new(RichText::new(step).monospace().small()).wrap().selectable(true)); + } + } + }); + }); +} + +/// Translates the engine's reason and script locations without parsing the displayed vehicle. +fn reason(review: &Review) -> String { + let fields = review.explanation.1.iter().map(|(k, v)| (*k, v.as_str())).collect::>(); + let reason = crate::i18n::fmt(review.explanation.0, &fields); + if review.lines.is_empty() { + return reason; + } + let steps = review + .lines + .iter() + .map(|(line, command)| crate::i18n::fmt("line {line} ({command})", &[("line", &line.to_string()), ("command", command)])) + .collect::>() + .join("; "); + crate::i18n::fmt("{steps}: {reason}", &[("steps", &steps), ("reason", &reason)]) +} diff --git a/crates/ui-egui/src/assistantpane.rs b/crates/ui-egui/src/assistantpane.rs index 8de0d33b24..c0f0f483e7 100644 --- a/crates/ui-egui/src/assistantpane.rs +++ b/crates/ui-egui/src/assistantpane.rs @@ -1319,6 +1319,9 @@ fn local_agents_setting(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { ); } if app.assistant.local_agents && ui.checkbox(&mut app.assistant.agents_auto, tl!("Approve agents' actions without asking")).changed() { + // Publish the change before returning from the widget: a held ACP reply can arrive + // before the next frame polls threads. + app.assistant.threads.set_auto(app.assistant.agents_auto); save_pref(app, "assistant_agents_auto", if app.assistant.agents_auto { "on" } else { "off" }); if let Some(a) = app.assistant.agent.as_mut() { a.set_agents_auto(app.assistant.agents_auto); @@ -1481,14 +1484,28 @@ fn composer(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, busy: bool, ctx: &egu #[cfg(not(target_arch = "wasm32"))] crate::agentthreads::list(app, ui, t); // An action waiting for approval comes first. - let pending = app.assistant.agent.as_ref().and_then(|a| a.pending(&app.session)); - if let Some((what, why, can_always)) = pending { + let pending = app + .assistant + .agent + .as_ref() + .and_then(|a| a.pending(&app.session).map(|(what, why, always)| (what, why, always, a.pending_review().cloned()))); + if let Some((what, why, can_always, review)) = pending { egui::Frame::NONE.fill(t.amber.gamma_multiply(0.15)).corner_radius(6).inner_margin(8).stroke(egui::Stroke::new(1.0, t.amber)).show( ui, |ui| { ui.label(RichText::new(tl!("Allow this action?")).font(semibold(12.5))); - ui.label(RichText::new(crate::i18n::fmt("The assistant wants to run this, and {why}:", &[("why", tl_raw!(&why))])).small()); - ui.label(RichText::new(what).monospace().small()); + egui::ScrollArea::vertical().max_height(280.0).id_salt(("assistant_approval", &what, &why)).show(ui, |ui| { + if let Some(review) = &review { + crate::approvalcard::show(ui, &app.session.registry, review); + } else { + // Legacy requests have no structured review: keep all their context visible. + ui.label( + RichText::new(crate::i18n::fmt("The assistant wants to run this, and {why}:", &[("why", tl_raw!(&why))])) + .small(), + ); + ui.add(egui::Label::new(RichText::new(&what).monospace().small()).wrap()); + } + }); // Plugin commands, scripts and replacing the project are asked about every time. if can_always { ui.checkbox(&mut app.assistant.always, tl!("Allow such actions for this conversation")); @@ -1802,7 +1819,7 @@ pub(crate) fn peer_replaced(app: &mut RustGisApp) { } #[cfg(test)] -mod tests { +pub(crate) mod tests { use super::*; #[cfg(not(target_arch = "wasm32"))] @@ -2069,6 +2086,28 @@ mod tests { ) } + /// Render the real local-agent settings without polling threads, preserving the interval + /// between a checkbox callback and the next poll while the fake ACP agent replies. + #[cfg(not(target_arch = "wasm32"))] + pub(crate) fn local_agents_harness(mut app: RustGisApp) -> egui_kittest::Harness<'static, RustGisApp> { + app.assistant.local_agents = true; + app.assistant.agents_auto = true; + app.assistant.agents_found = Some((Vec::new(), Vec::new())); + let mut fonts = false; + egui_kittest::Harness::builder().with_size(vec2(700.0, 350.0)).build_ui_state( + move |ui, app| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + ui.ctx().request_repaint(); + return; + } + local_agents_setting(app, ui, &Tokens::light()); + }, + app, + ) + } + /// The actual Remember checkbox goes through the deferred save path and deletes the stored key. #[cfg(not(target_arch = "wasm32"))] #[test] @@ -2247,6 +2286,92 @@ mod tests { assert!(!h.state().assistant.agent.as_ref().unwrap().busy()); } + /// Real pending reviews keep their reach visible while exact requests wait under Details. + #[test] + fn compact_approval_details_preserve_frozen_request_and_defaults() { + use egui_kittest::kittest::Queryable; + use serde_json::json; + + for (tool, args, expected) in [ + ( + "run", + json!({"script": concat!( + "layer.visibility {\"layer\":\"Countries\",\"visible\":false}\n", + "layer.visibility {\"visible\":false}\n", + "map.addTiles {\"name\":\"Synthetic tiles\",\"url\":\"https://tiles.invalid/{z}/{x}/{y}.png\"}" + )}), + vec!["3 project edits", "line 3 (map.addTiles)", "World / Countries", "https://tiles.invalid/{z}/{x}/{y}.png"], + ), + ( + "run_command", + json!({"command":"layer.addSql","params":{"sql":"FROM t","database":"/synthetic/views.duckdb"}}), + vec!["views in the database /synthetic/views.duckdb", "Destinations"], + ), + ] { + let mut app = RustGisApp::with_sample(); + app.session.run("map.basemap", &json!({"name":"ocean"})).unwrap(); + app.session.run("layer.select", &json!({"layer":"Cities"})).unwrap(); + let config = Config { + provider: "custom".into(), + base_url: "https://provider.invalid/v1".into(), + model: "synthetic".into(), + key: String::new(), + account: String::new(), + client_id: String::new(), + session: String::new(), + reasoning: None, + tool_output_token_limit: rustgis_assistant::tools::DEFAULT_OUTPUT_TOKENS, + }; + let mut agent = Agent::new(config, &app.session); + assert!(matches!(agent.ask("Review only", &app.session), Step::Send(_))); + let body = json!({"choices":[{"message":{"role":"assistant","content":null,"tool_calls":[{ + "id":"compact-card","type":"function","function":{"name":tool,"arguments":args.to_string()} + }]},"finish_reason":"tool_calls"}]}) + .to_string() + .into_bytes(); + assert_eq!( + agent.receive(agent.generation(), Ok(HttpResponse { status: 200, body, headers: vec![] }), &mut app.session), + Step::Confirm + ); + let exact = agent.pending_review().unwrap().what.clone(); + let can_always = agent.pending(&app.session).unwrap().2; + let project = serde_json::to_value(&app.session.project).unwrap(); + app.assistant.agent = Some(agent); + let mut fonts = false; + let mut h = egui_kittest::Harness::builder().with_size(vec2(680.0, 600.0)).build_ui_state( + move |ui, app: &mut RustGisApp| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + ui.ctx().request_repaint(); + return; + } + let ctx = ui.ctx().clone(); + composer(app, ui, &Tokens::light(), true, &ctx); + }, + app, + ); + h.run(); + assert!(h.query_by_label(&exact).is_none(), "raw vehicle starts collapsed"); + for text in expected { + assert!(h.query_by_label_contains(text).is_some(), "missing overview context: {text}"); + } + assert!(h.query_by_label_contains("Starting active layer: Cities; selected layers: [Cities]").is_some()); + assert_eq!(h.query_by_label("Allow such actions for this conversation").is_some(), can_always); + h.get_by_label("Details").click(); + h.run(); + h.get_by_label(&exact); + h.get_by_label("Allow"); + h.get_by_label("Decline"); + h.get_by_label("Details").click(); + h.run(); + assert!(h.query_by_label(&exact).is_none()); + assert_eq!(h.state().assistant.agent.as_ref().unwrap().pending_review().unwrap().what, exact); + assert_eq!(serde_json::to_value(&h.state().session.project).unwrap(), project); + assert!(!h.state().assistant.always); + } + } + #[test] fn plan_settings_order_and_signed_out_model() { use egui_kittest::kittest::{NodeT, Queryable}; diff --git a/crates/ui-egui/src/connectiontree.rs b/crates/ui-egui/src/connectiontree.rs index 55805112df..790cf284c5 100644 --- a/crates/ui-egui/src/connectiontree.rs +++ b/crates/ui-egui/src/connectiontree.rs @@ -37,6 +37,8 @@ pub struct State { /// A STAC asset waiting on its signing token: the token's address, the asset, the layer's name, /// the connection it was added from (named in Recent once it is added) and the token on its way. struct Signing { + /// The project that asked for the asset, checked before caching its token or adding data. + opened: u64, token_url: String, href: String, name: String, @@ -209,6 +211,7 @@ fn advance(app: &mut RustGisApp, id: &str, mut f: Fetch, bytes: Vec) -> Entr } } +/// Advances connection listings and only the asset signings owned by the current project. pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { let tree = &mut app.ui.connection_tree; let mut landed = Vec::new(); @@ -229,6 +232,9 @@ pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { } // STAC assets that needed a token: signed and added when it lands. for signing in std::mem::take(&mut app.ui.connection_tree.tokens) { + if !app.service_read_current(signing.opened, &signing.href, None) { + continue; + } let Some(r) = signing.slot.lock().unwrap_or_else(|e| e.into_inner()).take() else { app.ui.connection_tree.tokens.push(signing); continue; @@ -315,7 +321,7 @@ fn add_asset(app: &mut RustGisApp, href: &str, name: &str, origin: Option<&str>, let slot = crate::fetch_into(&token_url); let (href, name, origin) = (href.to_string(), name.to_string(), origin.map(str::to_string)); let connection = connection.map(str::to_string); - app.ui.connection_tree.tokens.push(Signing { token_url, href, name, origin, connection, slot }); + app.ui.connection_tree.tokens.push(Signing { opened: app.session.opened, token_url, href, name, origin, connection, slot }); } Ok(()) } @@ -1035,6 +1041,7 @@ mod tests { #[test] fn a_tileset_step_names_its_layer_and_a_refused_one_is_not_recorded() { + let _turn = crate::fetching::turn(); let mut h = harness(); let api = "https://x.org/collections"; let tiled = r#"{"collections": [{"id": "relief", "links": [{"rel": "tilesets-map", "href": "https://x.org/relief/map/tiles"}]}]}"#; @@ -1130,6 +1137,79 @@ mod tests { assert_eq!(h.state().session.map().layer_by_name("Soil carbon").unwrap().connection, id); } + /// A signing response cannot turn an old click into an addition to a replacement project. + #[test] + fn deferred_asset_signing_cannot_add_to_a_replacement_project() { + use rustgis_engine::render::rustgis_tiles::cache; + use std::sync::Mutex; + let _turn = crate::fetching::turn(); + for (case, (replace, ready)) in + [(None, true), (Some(false), false), (Some(false), true), (Some(true), false), (Some(true), true)].into_iter().enumerate() + { + let href = format!("https://review29{case}.blob.core.windows.net/old-assets/points.csv"); + let token_url = stac::sas_token_url(&href).unwrap(); + let (asset, token) = (href.clone(), token_url.clone()); + let sent = Arc::new(Mutex::new(Vec::new())); + let held = Arc::new(Mutex::new(Vec::::new())); + let (seen, callbacks) = (sent.clone(), held.clone()); + cache::set_fetcher(move |url, done| { + let plain = cache::split_download_limit(&url).0; + // Earlier frames' workers can still reach the process-wide fetcher. + if plain != token && plain.split('?').next() != Some(asset.as_str()) { + done(Err("outside the signing fixture".into())); + return; + } + seen.lock().unwrap().push(plain.to_string()); + callbacks.lock().unwrap().push(done); + }); + let mut app = RustGisApp::with_sample(); + let (text, _) = app.session.project.saved_text(None).unwrap(); + add_asset(&mut app, &href, "Old asset", None, None).unwrap(); + assert_eq!(app.ui.connection_tree.tokens.len(), 1); + assert_eq!(sent.lock().unwrap().as_slice(), std::slice::from_ref(&token_url)); + let finish = || { + for done in std::mem::take(&mut *held.lock().unwrap()) { + done(Ok(br#"{"token":"sig=review29-synthetic"}"#.to_vec())); + } + }; + if ready { + finish(); + } + match replace { + Some(true) => app.open_peer_text(&text).unwrap(), + Some(false) => { + app.session.run("project.openText", &json!({"text":text})).unwrap(); + } + None => {} + } + let mark = app.session.notice_mark(); + poll(&mut app, &egui::Context::default()); + let drained = app.ui.connection_tree.tokens.is_empty(); + if !ready { + finish(); + } + poll(&mut app, &egui::Context::default()); + for done in std::mem::take(&mut *held.lock().unwrap()) { + done(Err("test download finished".into())); + } + assert!(drained && app.ui.connection_tree.tokens.is_empty(), "old signing must retire before inspecting its slot"); + if replace.is_some() { + assert_eq!(sent.lock().unwrap().as_slice(), std::slice::from_ref(&token_url), "the old asset must not be requested"); + assert!(app.ui.downloads.is_empty() && app.ui.cog_probes.is_empty()); + assert!(stac::cached_token(&token_url).is_none(), "an old response must not update the token cache"); + assert_eq!(app.session.notices_since(mark).iter().filter(|n| n.text.contains("project was closed")).count(), 1); + } else { + assert_eq!( + *sent.lock().unwrap(), + [token_url.clone(), stac::sign(&href, "sig=review29-synthetic")], + "a current signing response starts exactly its asset download" + ); + assert_eq!(app.ui.downloads.len(), 1); + assert!(stac::cached_token(&token_url).is_some()); + } + } + } + /// The whole path on screen: open a connection in the Data Sources pane and double-click a /// layer. Recent then lists it under the connection's name with the source as added, and /// clicking that entry opens the service browser on the same address. diff --git a/crates/ui-egui/src/geolibreui.rs b/crates/ui-egui/src/geolibreui.rs index 001ebe605d..0b77da5e0b 100644 --- a/crates/ui-egui/src/geolibreui.rs +++ b/crates/ui-egui/src/geolibreui.rs @@ -29,12 +29,12 @@ pub struct Read { } /// Starts an import read; the engine has already reserved its layer and drawing position. -pub fn start(app: &mut RustGisApp, p: &Value) -> Result<(), String> { +pub fn start(app: &mut RustGisApp, p: &Value, claim: Option) -> Result<(), String> { if app.ui.geolibre_zarr.len() >= 16 { return Err(tl!("16 imported Zarr stores are already queued").into()); } let root = p.get("url").and_then(Value::as_str).ok_or(tl!("Zarr URL is required"))?.trim_end_matches('/').to_string(); - let id = app.session.download_started(); + let id = claim.unwrap_or_else(|| app.session.download_started()); app.ui.geolibre_zarr.push(Read { id, params: p.clone(), @@ -143,11 +143,20 @@ pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { let mut done = Vec::new(); // Serial reads bound aggregate buffers and assembly to one store. Queued jobs hold // parameters only; they cannot start downloads until earlier stores complete. - if let Some(job) = app.ui.geolibre_zarr.first_mut() - && let Some(result) = job.poll() - { - done.push((job.id, job.root.clone(), job.params.clone(), result)); - app.ui.geolibre_zarr.remove(0); + if let Some(id) = app.ui.geolibre_zarr.first().map(|job| job.id) { + let allowed = app.session.download_continues(id); + let still = app.session.download_still(id); + let result = match allowed { + Err(e) => Some(Err(e)), + Ok(()) => cache::with_optional_still(still, || app.ui.geolibre_zarr.first_mut().and_then(Read::poll)), + }; + if let Some(result) = result { + let Some(job) = app.ui.geolibre_zarr.first() else { + return; + }; + done.push((job.id, job.root.clone(), job.params.clone(), result)); + app.ui.geolibre_zarr.remove(0); + } } for (id, url, mut params, result) in done { match result { @@ -306,54 +315,260 @@ mod tests { assert!(app.session.map().layers.is_empty()); } + /// Uses the real importer to reserve a grouped URL layer without starting its read. + fn deferred_zarr(app: &mut RustGisApp) -> (usize, u32) { + app.session + .run_leaving_downloads( + "project.importGeolibre", + &json!({"text":json!({ + "name":"Deferred Zarr", "version":"0.2.0", "mapView":{}, + "layers":[{"id":"overflow","name":"Overflow","type":"zarr","groupId":"folder", + "source":{"url":"https://example.invalid/overflow.zarr"}}], + "layerGroups":[{"id":"folder","name":"Folder"}] + }).to_string()}), + ) + .unwrap(); + (app.session.project.active_map, app.session.map().layer_by_name("Overflow").unwrap().id) + } + #[test] fn geolibre_zarr_start_failure_removes_reservation_through_app_frame() { - let mut app = RustGisApp::with_sample(); - // Keep sixteen reads pending without issuing network requests. - for i in 0..16 { - app.ui.geolibre_zarr.push(Read { - id: i, - params: json!({}), - root: "https://example.org/grid.zarr".into(), - queue: Vec::new(), - waiting: vec![(".zmetadata".into(), Slot::default())], - got: HashMap::new(), - phase: None, - bytes: 0, - forbidden: 0, - error: None, - }); + for late_failure in [false, true] { + let mut app = RustGisApp::with_sample(); + let (target, id) = deferred_zarr(&mut app); + // Fill capacity after admission so the queued import reaches the reader's start check. + // Existing native reader tickets keep sixteen held slots valid without any sends. + if !late_failure { + for _ in 0..16 { + let id = app.session.download_started(); + app.ui.geolibre_zarr.push(Read { + id, + params: json!({}), + root: "https://example.org/grid.zarr".into(), + queue: Vec::new(), + waiting: vec![(".zmetadata".into(), Slot::default())], + got: HashMap::new(), + phase: None, + bytes: 0, + forbidden: 0, + error: None, + }); + } + let unrelated = app.session.download_started(); + let error = app.add_url(&json!({"url":"https://example.org/unclaimed.zarr","geolibreZarr":true}), None, None).unwrap_err(); + assert!(error.contains("16 imported Zarr stores"), "{error}"); + assert!(app.session.download_is_pending(unrelated), "an unclaimed failure must leave other downloads alone"); + app.session.download_failed(unrelated); + } + assert!(app.session.map().groups.iter().any(|g| g.children.contains(&id))); + if late_failure { + // Claim/start in the origin map, then let the actual admitted reader fail on a + // later frame after a map switch. Its cleanup owns the original reservation. + let request = app.session.take_url_requests().pop().unwrap(); + app.add_url(request.params(), Some(&request), None).unwrap(); + let claim = app.ui.geolibre_zarr[0].id; + assert!(app.session.download_is_pending(claim)); + assert!(app.session.download_still(claim).unwrap()(), "a successful start retains its live claim"); + app.ui.geolibre_zarr[0].queue.clear(); + app.ui.geolibre_zarr[0].error = Some("synthetic imported read failure".into()); + app.run("map.new", json!({"name":"Other"})).unwrap(); + } + let active = app.session.project.active_map; + let ctx = egui::Context::default(); + let mut output = ctx.run_ui(egui::RawInput::default(), |ui| app.logic(ui.ctx())); + output.textures_delta.clear(); + assert_eq!(app.session.project.active_map, active); + assert!(app.session.project.maps[target].layer(id).is_none()); + assert!(app.session.project.maps[target].groups.iter().all(|g| !g.children.contains(&id))); + assert_eq!(app.ui.geolibre_zarr.len(), if late_failure { 0 } else { 16 }); + assert!(app.session.take_url_requests().is_empty()); + // Queue/claim only: no reads start. A failed import must leave all 64 claim slots reusable. + let mut claims = Vec::new(); + for n in 0..64 { + app.session.request_url(&json!({"url":format!("https://example.org/capacity-{n}.csv")})).unwrap(); + let request = app.session.take_url_requests().pop().unwrap(); + let claim = app.session.download_started_for(&request); + assert!(claim.is_ok(), "claim {n} after failed imported read: {claim:?}"); + claims.push(claim.unwrap()); + } + for claim in claims { + app.session.download_failed(claim); + } } - let target = app.session.project.active_map; - let token = "geolibre-pending:overflow-test"; - let mut ds = rustgis_engine::data::Dataset::new("Overflow", rustgis_engine::data::GeometryType::Point, Vec::new()); - ds.source = Some(token.into()); - let id = app.session.map_mut().add_dataset(ds); - let mut group = rustgis_engine::map::LayerGroup::new("Folder"); - group.children.push(id); - app.session.map_mut().add_group(group); - app.session - .request_url(&json!({"url":"https://example.org/overflow.zarr","geolibreZarr":true,"geolibreLayer":{"token":token}})) - .unwrap(); - app.run("map.new", json!({"name":"Other"})).unwrap(); - let active = app.session.project.active_map; - let ctx = egui::Context::default(); - let mut output = ctx.run_ui(egui::RawInput::default(), |ui| app.logic(ui.ctx())); - // Logic-only frames do not paint; explicitly discard their font texture updates. - output.textures_delta.clear(); - assert_eq!(app.session.project.active_map, active); - assert!(app.session.project.maps[target].layer(id).is_none()); - assert!(app.session.project.maps[target].groups.iter().all(|g| !g.children.contains(&id))); - assert_eq!(app.ui.geolibre_zarr.len(), 16); - assert!(app.session.take_url_requests().is_empty()); + } + + /// Rejections before reader creation must retire both live and undo/redo reservations. + #[test] + fn geolibre_zarr_pre_start_rejection_retires_reservation_through_app_frame() { + use std::sync::{ + Arc, + atomic::{AtomicUsize, Ordering}, + }; + let _turn = crate::fetching::turn(); + let sent = Arc::new(AtomicUsize::new(0)); + let count = sent.clone(); + cache::set_fetcher(move |_, done| { + count.fetch_add(1, Ordering::SeqCst); + done(Err("unexpected imported read".into())); + }); + for case in ["revoked", "map", "capacity"] { + let mut app = RustGisApp::with_sample(); + let unrelated = serde_json::to_value(&app.session.project.maps[0]).unwrap(); + // These logins belong only to this Session, not a process-wide Earthdata account. + let login = if case == "revoked" { + let id = app + .session + .run( + "connection.new", + &json!({ + "kind":"wms", "address":"https://retirement.invalid", "auth":"header", "keyName":"X-Key" + }), + ) + .unwrap()["id"] + .as_str() + .unwrap() + .to_string(); + app.session.run("connection.login", &json!({"id":id,"apiKey":"RGSYNTH_retirement_session"})).unwrap(); + Some(id) + } else { + None + }; + let (target, id) = deferred_zarr(&mut app); + let mut claims = Vec::new(); + if case == "capacity" { + use rustgis_engine::policy; + struct Live; + impl policy::Revocation for Live { + fn allows(&self, _: policy::Effects) -> bool { + true + } + } + // Grow unrelated work after admission. Its owner drains only its own requests, + // leaving the import queued for the actual app frame's start and cleanup path. + app.session.active_layer = None; + assert!(app.session.selected_layers().is_empty()); + let scope = policy::Scope::agent(policy::AgentAccess::Allow, true, "held-capacity", None, Arc::new(Live)); + claims = app + .session + .with_scope(scope, |s| -> Result, String> { + let mut claims = Vec::new(); + for n in 0..64 { + s.request_url(&json!({"url":format!("https://example.invalid/held-{n}.csv")}))?; + let request = s.take_url_requests().pop().ok_or("held request was not queued")?; + claims.push(s.download_started_for(&request)?); + } + Ok(claims) + }) + .unwrap() + .unwrap(); + } + let token = app.session.map().layer(id).unwrap().dataset.source.clone().unwrap(); + assert!(app.session.map().groups.iter().any(|g| g.children.contains(&id))); + // This ordinary edit leaves a real undo snapshot containing the reservation. + app.session.run("map.rename", &json!({"name":"Queued import"})).unwrap(); + if let Some(login) = login { + app.session.run("connection.logout", &json!({"id":login})).unwrap(); + } + if case == "map" { + let name = app.session.project.maps[0].name.clone(); + app.session.run("map.activate", &json!({"name":name})).unwrap(); + } + let active = app.session.project.active_map; + let mark = app.session.notice_mark(); + let ctx = egui::Context::default(); + let mut output = ctx.run_ui(egui::RawInput::default(), |ui| app.logic(ui.ctx())); + output.textures_delta.clear(); + assert_eq!(sent.load(Ordering::SeqCst), 0, "{case}: rejected before any send"); + assert_eq!(app.session.project.active_map, active, "{case}"); + assert_eq!(serde_json::to_value(&app.session.project.maps[0]).unwrap(), unrelated, "{case}: unrelated map"); + assert!(app.session.project.maps[target].layer(id).is_none(), "{case}: reservation removed"); + assert!(app.session.project.maps[target].groups.iter().all(|g| !g.children.contains(&id)), "{case}: group repaired"); + assert!(app.ui.geolibre_zarr.is_empty(), "{case}: no reader started"); + assert!(app.session.take_url_requests().is_empty()); + let errors = app.session.notices_since(mark).iter().filter(|n| n.error).count(); + assert_eq!(errors, 1, "{case}: one refusal notice"); + assert!(app.session.project.saved_text(None).is_ok(), "{case}: saving is no longer blocked"); + for claim in &claims { + assert!(app.session.download_is_pending(*claim), "capacity refusal preserves other downloads"); + assert!(app.session.download_still(*claim).unwrap()()); + } + for command in ["edit.undo", "edit.redo"] { + app.session.run(command, &Value::Null).unwrap(); + let saved = app.session.project.saved_text(None).unwrap().0; + assert!(!saved.contains(&token), "{case}: {command} cannot resurrect the reservation"); + } + for claim in claims { + app.session.download_failed(claim); + } + } + } + + /// Failed foreign and duplicate claims must leave the rightful import untouched. + #[test] + fn geolibre_zarr_rejected_claim_preserves_the_original_request() { + let mut a = RustGisApp::with_sample(); + let mut b = RustGisApp::with_sample(); + let (map_a, layer_a) = deferred_zarr(&mut a); + let (map_b, layer_b) = deferred_zarr(&mut b); + let qa = a.session.take_url_requests().pop().unwrap(); + let qb = b.session.take_url_requests().pop().unwrap(); + assert!(b.session.download_started_for(&qa).unwrap_err().contains("another session")); + assert!(a.session.project.maps[map_a].layer(layer_a).is_some()); + assert!(b.session.project.maps[map_b].layer(layer_b).is_some()); + let foreign = b.session.with_scope(rustgis_engine::policy::Scope::peer(), |s| s.download_started_for(&qb)).unwrap(); + assert!(foreign.unwrap_err().contains("another caller")); + assert!(b.session.project.maps[map_b].layer(layer_b).is_some()); + let id = b.session.download_started_for(&qb).unwrap(); + assert!(b.session.download_started_for(&qb).is_err(), "a duplicate does not retire the already claimed import"); + assert!(b.session.project.maps[map_b].layer(layer_b).is_some()); + assert!(b.session.download_continues(id).is_ok()); + assert!(a.session.download_started_for(&qa).is_ok(), "foreign rejection preserves the other session too"); + } + + /// A deterministic stage boundary covers invalidation after claim but before reader creation. + #[test] + fn geolibre_zarr_claimed_invalidation_retires_only_its_reservation() { + let mut app = RustGisApp::with_sample(); + let connection = app + .session + .run( + "connection.new", + &json!({ + "kind":"wms", "address":"https://claimed-retirement.invalid", "auth":"header", "keyName":"X-Key" + }), + ) + .unwrap()["id"] + .as_str() + .unwrap() + .to_string(); + app.session.run("connection.login", &json!({"id":connection,"apiKey":"RGSYNTH_claimed_retirement"})).unwrap(); + let (target, layer) = deferred_zarr(&mut app); + let request = app.session.take_url_requests().pop().unwrap(); + let claimed = app.session.download_started_for(&request).unwrap(); + app.session.run("connection.logout", &json!({"id":connection})).unwrap(); + // This unrelated current request has no default dependency on the reserved layer. + app.session.active_layer = None; + assert!(app.session.selected_layers().is_empty()); + app.session.request_url(&json!({"url":"https://example.invalid/unrelated.csv"})).unwrap(); + let unrelated = app.session.take_url_requests().pop().unwrap(); + let unrelated = app.session.download_started_for(&unrelated).unwrap(); + let error = app.session.download_continues(claimed).unwrap_err(); + assert!(error.contains("account"), "{error}"); + assert!(!app.session.download_is_pending(claimed)); + assert!(app.session.project.maps[target].layer(layer).is_none()); + assert!(app.session.project.maps[target].groups.iter().all(|g| !g.children.contains(&layer))); + assert!(app.session.project.saved_text(None).is_ok()); + assert!(app.session.download_continues(unrelated).is_ok()); + app.session.download_failed(unrelated); } /// A full active reader must not let queued stores issue downloads or retain chunk buffers. #[test] fn geolibre_zarr_waiting_store_does_not_multiply_the_active_byte_budget() { let mut app = RustGisApp::with_sample(); + let id = app.session.download_started(); app.ui.geolibre_zarr.push(Read { - id: 0, + id, params: json!({}), root: "https://example.org/active.zarr".into(), queue: vec!["next".into()], @@ -364,7 +579,7 @@ mod tests { forbidden: 0, error: None, }); - start(&mut app, &json!({"url":"https://example.org/queued.zarr"})).unwrap(); + start(&mut app, &json!({"url":"https://example.org/queued.zarr"}), None).unwrap(); let ctx = egui::Context::default(); let mut output = ctx.run_ui(egui::RawInput::default(), |ui| app.logic(ui.ctx())); output.textures_delta.clear(); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 678298303b..7a51eb24f7 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -11,6 +11,7 @@ pub mod i18n; #[cfg(not(target_arch = "wasm32"))] mod agentthreads; mod analysispane; +mod approvalcard; mod arcgisui; #[cfg(not(target_arch = "wasm32"))] mod assistantkeys; @@ -445,6 +446,7 @@ pub struct TilesResolve { /// A service layer waiting on a document before it is added. `dialog` tells whether the add came /// from the add-service dialog (its result shows there) or from the Connections tree (a notice); /// `origin` is the connection a tree add is for, named in Recent once the layer is added. +/// `opened` binds every follow-up and final addition to the project that started discovery. pub enum ServiceStep { /// An OGC API layer whose advertised tile template is being read. Tiles { resolve: TilesResolve, dialog: bool, origin: Option, opened: u64 }, @@ -453,6 +455,21 @@ pub enum ServiceStep { Coverage { params: Value, slot: Slot, String>>, dialog: bool, origin: Option, opened: u64 }, } +impl ServiceStep { + /// The dialog's progress text is shared with its writer, so a retired step clears only + /// its own message even when another project is reading a service with the same name. + fn pending_message(&self) -> Option { + match self { + Self::Tiles { resolve, dialog, .. } => { + dialog.then(|| serviceui::reading_tileset(resolve.params.get("name").and_then(Value::as_str).unwrap_or(""))) + } + Self::Coverage { params, dialog, .. } => { + dialog.then(|| serviceui::describing(params.get("name").and_then(Value::as_str).unwrap_or(""))) + } + } + } +} + /// Fetches an address into a slot, off the frame. pub(crate) fn fetch_into(url: &str) -> Slot, String>> { fetch_into_limited(url, rustgis_engine::render::rustgis_tiles::cache::MAX_DOWNLOAD) @@ -698,7 +715,7 @@ pub struct UiState { /// GeoTIFFs, FlatGeobufs and GeoParquets by address whose headers are being read: those /// that can be read in place are added so, others downloaded (the `ui.addUrl` parameters, /// and the answer). - pub cog_probes: Vec<(Value, Slot>)>, + pub cog_probes: Vec<(Value, Option, Slot>)>, /// Service layers whose features are being read page by page (an ArcGIS layer's description /// first). pub service_pages: Vec, @@ -938,6 +955,20 @@ impl Default for UiState { } } +/// Native UI dispatch choices share one pre/post lifecycle; command JSON cannot select them. +enum AppDispatch { + Direct, + Prepared(Box), + InPlace(u64), + Download(u64), + #[cfg(not(target_arch = "wasm32"))] + Read(Box), + ResolvedTiles { + template: String, + crossed_origin: bool, + }, +} + /// The resolution of a layout copied to the clipboard. const CAPTURE_DPI: f64 = 192.0; /// The most pixels a layout copied to the clipboard has (a page above it is copied at a lower dpi). @@ -1690,6 +1721,16 @@ impl RustGisApp { } } + /// Reports acknowledgment failures even when the engine refuses before command dispatch. + fn acknowledge_gp_file(&mut self, id: u64, error: Option<&str>) -> Result { + let mark = self.session.notice_mark(); + let completed = self.session.acknowledge_gp_file(id, error); + if let Err(error) = &completed { + self.command_failed("gp.fileDelivered", error, mark); + } + completed + } + /// Delivers exports from all command entry points, including scripts and the assistant. /// The engine records success only after the service submits the file to the host. fn deliver_gp_files(&mut self, mut requested: Result) -> Result { @@ -1698,18 +1739,14 @@ impl RustGisApp { Some(download) => download(&name, bytes), None => Err(tl!("this build has no download service").into()), }; - let mut params = json!({"id": id}); - if let Err(error) = &delivered { - params["error"] = json!(error); - } - let completed = self.session.run("gp.fileDelivered", ¶ms); + // gp.fileDelivered reports delivery failures from its history record. The app also + // reports acknowledgment failures before dispatch, without repeating engine notices. + let completed = self.acknowledge_gp_file(id, delivered.as_ref().err().map(String::as_str)); let result = delivered.and(completed); self.gp.last = Some(match &result { Ok(_) => (true, self.session.project.gp_history.last().map(|h| h.messages.clone()).unwrap_or_default()), Err(error) => (false, vec![error.clone()]), }); - // gp.fileDelivered reports delivery failures from the history record and its - // own acknowledgment errors through Session::run; neither needs a second notice. requested = complete_gp_file(requested, id, &result); } requested @@ -2074,7 +2111,8 @@ impl RustGisApp { } // A plugin's may wait for a server: the window says so first (#1319). if id.starts_with(rustgis_engine::plugins::PREFIX) { - pluginstart::ask(self, id, params); + let notice_mark = self.session.notice_mark(); + pluginstart::ask(self, id, params).inspect_err(|e| self.command_failed(id, e, notice_mark))?; return Ok(Value::Null); } self.run(id, params) @@ -2105,7 +2143,7 @@ impl RustGisApp { /// Reports pending header reads retired by either ordinary or peer project replacement. fn retire_cog_probes(&mut self) { - for (p, _) in std::mem::take(&mut self.ui.cog_probes) { + for (p, _, _) in std::mem::take(&mut self.ui.cog_probes) { let url = p.get("url").and_then(Value::as_str).unwrap_or(""); let url = serviceui::address_in_message(rustgis_engine::data::s3_address::inner(url)); let why = i18n::fmt("{url}: its project was closed while it was read, so it wasn't added", &[("url", &url)]); @@ -2119,7 +2157,7 @@ impl RustGisApp { self.run_with_prepared(id, params, AppDispatch::Direct) } - /// Reports a command failure once through the normal diagnostics and notice path. + /// Reports failures before engine dispatch as well as execution, without repeating its notice. fn command_failed(&mut self, id: &str, error: &str, notice_mark: u64) { let text = rustgis_engine::Session::redacted(error); log::info!("{id}: {}", text.as_str()); @@ -2128,7 +2166,18 @@ impl RustGisApp { } } - /// Runs direct and resolved-service commands with the same app lifecycle. + /// Runs an engine-admitted delayed command with the app's usual delivery and recording. + fn run_prepared(&mut self, id: &str, params: Value, prepared: rustgis_engine::policy::PreparedCommand) -> Result { + self.run_with_prepared(id, params, AppDispatch::Prepared(Box::new(prepared))) + } + + /// Lands an admitted read through the same notices, recording and preferences as a command. + #[cfg(not(target_arch = "wasm32"))] + fn run_read(&mut self, id: &str, params: Value, read: rustgis_engine::cmd::reads::Read) -> Result { + self.run_with_prepared(id, params, AppDispatch::Read(Box::new(read))) + } + + /// Keeps direct, prepared and resolved-service dispatch within the same UI lifecycle. fn run_with_prepared(&mut self, id: &str, params: Value, dispatch: AppDispatch) -> Result { let _diagnostics = self.session.diagnostics.enter(); if !matches!(id, "ui.menu" | "ui.ribbon" | "ui.language" | "ui.worldview") { @@ -2153,11 +2202,17 @@ impl RustGisApp { let notice_mark = self.session.notice_mark(); // A plugin's command that adds data by address doesn't freeze the app: the download is // left for the frame loop (`ui.addUrl`), as in the browser. + let in_place = matches!(&dispatch, AppDispatch::InPlace(_)); let resolved_lineage = match &dispatch { AppDispatch::ResolvedTiles { crossed_origin, .. } => Some(*crossed_origin), _ => None, }; let mut r = match dispatch { + AppDispatch::Prepared(prepared) => self.session.run_prepared(*prepared), + AppDispatch::InPlace(id) => self.session.add_in_place_for(id, ¶ms), + AppDispatch::Download(id) => self.session.complete_download(id, ¶ms), + #[cfg(not(target_arch = "wasm32"))] + AppDispatch::Read(read) => rustgis_engine::cmd::reads::read_land(&mut self.session, *read), AppDispatch::ResolvedTiles { template, crossed_origin } => { rustgis_engine::cmd::service::add_resolved_tiles(&mut self.session, ¶ms, &template, crossed_origin) } @@ -2262,6 +2317,13 @@ impl RustGisApp { } } self.record_step(id, &replay, active); + } else if in_place { + // The request number correlates this delivery; replay starts a fresh request. + let mut replay = params.clone(); + if let Some(object) = replay.as_object_mut() { + object.remove("request"); + } + self.record_step(id, &replay, active); } else { self.record_step(id, ¶ms, active); } @@ -2313,6 +2375,27 @@ impl RustGisApp { Some((name, shared)) } + /// Applies the engine-held background tool owner, then records the completed operation. + pub(crate) fn finish_gp(&mut self, id: u64) -> rustgis_engine::CmdResult { + let result = self.session.finish_gp(id); + if result.is_ok() { + self.record_step("gp.finish", &json!({"id":id}), None); + } + result + } + + /// Applies a workflow's admitted raster completion and records its replayable command. + #[cfg(not(target_arch = "wasm32"))] + pub(crate) fn finish_raster(&mut self, command: &str, id: u64, params: &Value) -> rustgis_engine::CmdResult { + let result = self.session.finish_raster(command, id, params); + if result.is_ok() { + let mut recorded = params.clone(); + recorded["ran"] = json!(id); + self.record_step(command, &recorded, None); + } + result + } + /// Record Task: adds a command that ran from the app and changed the project as a step. Not /// panes and dialogs (`ui.*`), tasks themselves, or what only moves the view. A colour, outline /// or transparency set again on the same layer (dragged in its picker) updates the step it just @@ -2996,154 +3079,7 @@ impl RustGisApp { } // Data by its web address: the dialog, then (with an address) the download, off the // frame; it's added when it lands. A PMTiles archive is a tile layer, never downloaded. - // Optional `connection`: the saved web-service id forwarded to layer.addUrl or - // layer.addDownload, including probes, whole-file fallbacks and paged features. - "ui.addUrl" => { - if p.get("geolibreZarr").and_then(Value::as_bool) == Some(true) { - if let Err(e) = geolibreui::start(self, p) { - rustgis_engine::cmd::geolibre::discard_pending(&mut self.session, p); - return Err(e); - } - return Ok(Value::Null); - } - // The connection a Connections tree add is for (named in Recent once it is added). - let origin = connectiontree::take_origin(self); - let Some(raw_url) = p.get("url").and_then(Value::as_str).filter(|u| !u.trim().is_empty()) else { - return self.run("ui.dialog", json!({"name": "addUrl"})); - }; - let url = raw_url.trim(); - // Bind the same normalized address that every deferred consumer receives. - let mut normalized = std::borrow::Cow::Borrowed(p); - if url.len() != raw_url.len() { - normalized.to_mut()["url"] = json!(url); - } - let bound = rustgis_engine::service_logins::request_params(&mut self.session, normalized.as_ref()) - .map_err(|e| connectionui::translated(&e))?; - let p = bound.as_ref(); - let url = rustgis_engine::data::login_address::inner(p.get("url").and_then(Value::as_str).unwrap_or(url)); - if url.to_ascii_lowercase().split(['?', '#']).next().is_some_and(|u| u.ends_with(".pmtiles")) { - return connectiontree::run_named(self, "layer.addUrl", p.clone(), origin.as_deref()); - } - let address = p.get("url").and_then(Value::as_str).unwrap_or(url).to_string(); - // A GeoTIFF, FlatGeobuf or GeoParquet: its header (or footer) first, off the - // frame; one that can be read in place is, another is downloaded (see - // `poll_downloads`). An address without a file name (an API answering with a - // file, such as Vantor's `…/geotiff?…`) is probed as a GeoTIFF too, and downloaded - // as before when it doesn't answer as one. - let remote = rustgis_engine::data::source::is_remote_name(url); - let tiff = rustgis_engine::cmd::layer::is_tiff_url(url); - // A scan (`"as"`, a plugin's or a script's) keeps only the pixels: downloaded - // whole. - let scan = p.get("as").and_then(Value::as_str) == Some("scan"); - let unnamed = !remote - && !tiff - && rustgis_engine::cmd::layer::is_unnamed_url(url) - && ["service", "coverage"].iter().all(|k| p.get(*k).and_then(Value::as_bool) != Some(true)); - if (remote || tiff || unnamed) && !scan && p.get("whole").and_then(Value::as_bool) != Some(true) { - let slot: Slot> = Slot::default(); - let land = slot.clone(); - if remote { - rustgis_engine::data::source::open_url( - &address, - Box::new(move |r| { - let r = match r { - Ok(_) => Ok(true), - Err(e) if e.to_string().contains(rustgis_engine::data::source::remote::READ_WHOLE) => Ok(false), - Err(e) => Err(e.to_string()), - }; - *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r); - }), - ); - } else { - rustgis_engine::render::rustgis_tiles::cog::probe( - &address, - Box::new(move |r| { - let r = if unnamed { Ok(r.unwrap_or(false)) } else { r }; - *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r); - }), - ); - } - // An address read through a saved S3 connection is said as the address it wraps. - let shown = rustgis_engine::data::s3_address::inner(url); - self.session.notify(i18n::fmt("Reading {url}…", &[("url", shown)]), false); - let mut probe = p.clone(); - probe["url"] = json!(address); - if let Some(o) = probe.as_object_mut() { - o.remove(connectiontree::ORIGIN); - if let Some(origin) = &origin { - o.insert(connectiontree::ORIGIN.into(), json!(origin)); - } - } - self.ui.cog_probes.push((probe, slot)); - return Ok(Value::Null); - } - let url = address; - // With its address, as the request it may answer has: what fails or lands later is - // matched to the plugin command waiting for it by this (#1202). - let mut q = json!({"url": url}); - for k in ["name", "layer", "crs", "connection"] { - if let Some(v) = p.get(k).filter(|v| v.as_str().is_some_and(|s| !s.trim().is_empty())) { - q[k] = v.clone(); - } - } - // How the bytes are read, as `layer.addData` takes it (a command asking by - // address, such as a plugin's: the dialog gives none of these). - for k in ["x", "y", "wkt", "delimiter", "paperSpace", "stream", "as"] { - if let Some(v) = p.get(k).filter(|v| !v.is_null()) { - q[k] = v.clone(); - } - } - if let Some(zoom) = - p.get("zoom").and_then(|v| v.as_bool().or_else(|| v.as_str().map(|s| matches!(s, "true" | "yes" | "1" | "on")))) - { - q["zoom"] = json!(zoom); - } - // A plugin command's request: its number goes to the command that adds it, for - // the plugin command to run again with what it answered (#1202). - if let Some(n) = p.get("request").filter(|v| v.is_u64()) { - q["request"] = n.clone(); - } - for k in ["service", "coverage", "anonymous"] { - if p.get(k).and_then(Value::as_bool) == Some(true) { - q[k] = json!(true); - } - } - for key in ["geolibreProject", "geolibreLayer"] { - if let Some(value) = p.get(key) { - q[key] = value.clone(); - } - } - // A service layer's bounds, which its features' axes are checked against. - if let Some(b) = p.get("bounds").filter(|b| b.is_array()) { - q["bounds"] = b.clone(); - } - if let Some(origin) = &origin { - q[connectiontree::ORIGIN] = json!(origin); - } - let shown = rustgis_engine::data::s3_address::inner(&url); - self.session.notify(i18n::fmt("Downloading {url}…", &[("url", shown)]), false); - let id = self.session.download_started(); - // A service's features: read page by page from the start, off the frame (an ArcGIS - // layer's description first). - if q.get("service").and_then(Value::as_bool) == Some(true) { - let origin = connectiontree::take_origin_key(&mut q); - serviceui::read_pages(self, serviceui::Landed { id, url, params: q, origin }); - return Ok(Value::Null); - } - let slot: Slot, String>> = Slot::default(); - let land = slot.clone(); - // A Shapefile's `.shp` address brings its `.dbf`, `.shx`, `.prj` and `.cpg` too. - rustgis_engine::cmd::layer::download_data( - &url, - if q.get("geolibreProject").and_then(Value::as_bool) == Some(true) { - rustgis_engine::map::geolibre::MAX_TEXT - } else { - rustgis_engine::render::rustgis_tiles::cache::MAX_DOWNLOAD - }, - Box::new(move |r| *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r)), - ); - self.ui.downloads.push((id, url, q, slot)); - } + "ui.addUrl" => return self.add_url(p, None, None), "ui.connection" => { if let Some(id) = p.get("id").and_then(Value::as_str) { connectionui::open_edit(self, id)?; @@ -4206,18 +4142,210 @@ impl RustGisApp { /// Consumes completed probes and downloads on the app frame, finalizing imports in their /// reserved map or discarding failed reservations even when another map is active. + /// A native dialog or an engine-issued deferred request. The claim rides outside JSON. + fn add_url(&mut self, p: &Value, request: Option<&rustgis_engine::UrlRequest>, existing: Option) -> Result { + let claim = match request { + Some(request) => Some(self.session.download_started_for(request)?), + None => existing, + }; + if let Some(id) = claim { + self.session.download_continues(id)?; + } + let still = claim.and_then(|id| self.session.download_still(id)); + rustgis_engine::render::rustgis_tiles::cache::with_optional_still(still, || { + if p.get("geolibreZarr").and_then(Value::as_bool) == Some(true) { + if let Err(e) = geolibreui::start(self, p, claim) { + rustgis_engine::cmd::geolibre::discard_pending(&mut self.session, p); + if let Some(id) = claim { + self.session.download_failed(id); + } + return Err(e); + } + return Ok(Value::Null); + } + // The connection a Connections tree add is for (named in Recent once it is added). + let origin = connectiontree::take_origin(self); + let Some(raw_url) = p.get("url").and_then(Value::as_str).filter(|u| !u.trim().is_empty()) else { + return self.run("ui.dialog", json!({"name": "addUrl"})); + }; + let url = raw_url.trim(); + // Claimed work keeps its exact admitted address and connection. Only direct native + // UI ingress chooses a login here; a continuation never acquires a new default. + let bound = if claim.is_some() { + std::borrow::Cow::Borrowed(p) + } else { + let mut normalized = std::borrow::Cow::Borrowed(p); + if url.len() != raw_url.len() { + normalized.to_mut()["url"] = json!(url); + } + std::borrow::Cow::Owned( + rustgis_engine::service_logins::request_params(&mut self.session, normalized.as_ref()) + .map_err(|e| connectionui::translated(&e))? + .into_owned(), + ) + }; + let p = bound.as_ref(); + let url = rustgis_engine::data::login_address::inner(p.get("url").and_then(Value::as_str).unwrap_or(url)); + if url.to_ascii_lowercase().split(['?', '#']).next().is_some_and(|u| u.ends_with(".pmtiles")) { + let before = self.session.sources.generation; + let result = if let Some(id) = claim { + self.run_with_prepared("layer.addUrl", p.clone(), AppDispatch::InPlace(id)) + } else { + self.run("layer.addUrl", p.clone()) + }; + if result.is_ok() { + connectiontree::name_recent(self, before, origin.as_deref()); + } else if let Err(error) = &result { + self.session.request_failed(p, error); + } + return result; + } + let address = p.get("url").and_then(Value::as_str).unwrap_or(url).to_string(); + // A GeoTIFF, FlatGeobuf or GeoParquet: its header (or footer) first, off the + // frame; one that can be read in place is, another is downloaded (see + // `poll_downloads`). An address without a file name (an API answering with a + // file, such as Vantor's `…/geotiff?…`) is probed as a GeoTIFF too, and downloaded + // as before when it doesn't answer as one. + let remote = rustgis_engine::data::source::is_remote_name(url); + let tiff = rustgis_engine::cmd::layer::is_tiff_url(url); + // A scan (`"as"`, a plugin's or a script's) keeps only the pixels: downloaded + // whole. + let scan = p.get("as").and_then(Value::as_str) == Some("scan"); + let unnamed = !remote + && !tiff + && rustgis_engine::cmd::layer::is_unnamed_url(url) + && ["service", "coverage"].iter().all(|k| p.get(*k).and_then(Value::as_bool) != Some(true)); + if (remote || tiff || unnamed) && !scan && p.get("whole").and_then(Value::as_bool) != Some(true) { + let slot: Slot> = Slot::default(); + let land = slot.clone(); + if remote { + rustgis_engine::data::source::open_url( + &address, + Box::new(move |r| { + let r = match r { + Ok(_) => Ok(true), + Err(e) if e.to_string().contains(rustgis_engine::data::source::remote::READ_WHOLE) => Ok(false), + Err(e) => Err(e.to_string()), + }; + *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r); + }), + ); + } else { + rustgis_engine::render::rustgis_tiles::cog::probe( + &address, + Box::new(move |r| { + let r = if unnamed { Ok(r.unwrap_or(false)) } else { r }; + *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r); + }), + ); + } + // An address read through a saved S3 connection is said as the address it wraps. + let shown = rustgis_engine::data::s3_address::inner(url); + self.session.notify(i18n::fmt("Reading {url}…", &[("url", shown)]), false); + let mut probe = p.clone(); + probe["url"] = json!(address); + if let Some(o) = probe.as_object_mut() { + o.remove(connectiontree::ORIGIN); + if let Some(origin) = &origin { + o.insert(connectiontree::ORIGIN.into(), json!(origin)); + } + } + self.ui.cog_probes.push((probe, claim, slot)); + return Ok(Value::Null); + } + let url = address; + // The request receipt is correlation only; its admitted claim stays outside JSON. + let mut q = json!({"url": url}); + for k in ["name", "layer", "crs", "connection"] { + if let Some(v) = p.get(k).filter(|v| v.as_str().is_some_and(|s| !s.trim().is_empty())) { + q[k] = v.clone(); + } + } + // How the bytes are read, as `layer.addData` takes it (a command asking by + // address, such as a plugin's: the dialog gives none of these). + for k in ["x", "y", "wkt", "delimiter", "paperSpace", "stream", "as"] { + if let Some(v) = p.get(k).filter(|v| !v.is_null()) { + q[k] = v.clone(); + } + } + if claim.is_some() { + // A claimed request keeps its original JSON value through completion. + if let Some(zoom) = p.get("zoom") { + q["zoom"] = zoom.clone(); + } + } else if let Some(zoom) = + p.get("zoom").and_then(|v| v.as_bool().or_else(|| v.as_str().map(|s| matches!(s, "true" | "yes" | "1" | "on")))) + { + q["zoom"] = json!(zoom); + } + if let Some(number) = p.get("request").filter(|v| v.is_u64()) { + q["request"] = number.clone(); + } + for k in ["service", "coverage", "anonymous"] { + if p.get(k).and_then(Value::as_bool) == Some(true) { + q[k] = json!(true); + } + } + for key in ["geolibreProject", "geolibreLayer"] { + if let Some(value) = p.get(key) { + q[key] = value.clone(); + } + } + // A service layer's bounds, which its features' axes are checked against. + if let Some(b) = p.get("bounds").filter(|b| b.is_array()) { + q["bounds"] = b.clone(); + } + if let Some(origin) = &origin { + q[connectiontree::ORIGIN] = json!(origin); + } + let shown = rustgis_engine::data::s3_address::inner(&url); + self.session.notify(i18n::fmt("Downloading {url}…", &[("url", shown)]), false); + let id = claim.unwrap_or_else(|| self.session.download_started()); + // A service's features: read page by page from the start, off the frame (an ArcGIS + // layer's description first). + if q.get("service").and_then(Value::as_bool) == Some(true) { + let origin = connectiontree::take_origin_key(&mut q); + serviceui::read_pages(self, serviceui::Landed { id, url, params: q, origin }); + return Ok(Value::Null); + } + let slot: Slot, String>> = Slot::default(); + let land = slot.clone(); + // A Shapefile address brings its companion parts under the same download owner. + rustgis_engine::cmd::layer::download_data( + &url, + if q.get("geolibreProject").and_then(Value::as_bool) == Some(true) { + rustgis_engine::map::geolibre::MAX_TEXT + } else { + rustgis_engine::render::rustgis_tiles::cache::MAX_DOWNLOAD + }, + Box::new(move |r| *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r)), + ); + self.ui.downloads.push((id, url, q, slot)); + + Ok(Value::Null) + }) + } + fn poll_data_downloads(&mut self) { // GeoTIFFs, FlatGeobufs and GeoParquets whose headers are in: read in place, or // downloaded whole. let mut probed = Vec::new(); - self.ui.cog_probes.retain(|(p, slot)| match slot.lock().unwrap_or_else(|e| e.into_inner()).take() { + self.ui.cog_probes.retain(|(p, claim, slot)| match slot.lock().unwrap_or_else(|e| e.into_inner()).take() { Some(r) => { - probed.push((p.clone(), r)); + probed.push((p.clone(), *claim, r)); false } None => true, }); - for (mut p, r) in probed { + for (mut p, claim, r) in probed { + if let Some(id) = claim + && let Err(e) = self.session.download_continues(id) + { + self.session.request_failed(&p, &e); + self.landed_in_dialog(&p, Err(&e)); + self.session.notify(e, true); + continue; + } let origin = connectiontree::take_origin_key(&mut p); let url = p.get("url").and_then(Value::as_str).unwrap_or("").to_string(); match r { @@ -4228,26 +4356,44 @@ impl RustGisApp { } let before = self.session.sources.generation; let pending = p.clone(); - match self.run("layer.addUrl", p) { + let added = if let Some(id) = claim { + self.run_with_prepared("layer.addUrl", p, AppDispatch::InPlace(id)) + } else { + self.run("layer.addUrl", p) + }; + match added { Ok(_) => { connectiontree::name_recent(self, before, origin.as_deref()); self.map.invalidate(); self.landed_in_dialog(&pending, Ok(())); } - Err(e) => self.landed_in_dialog(&pending, Err(&e)), + Err(e) => { + self.session.request_failed(&pending, &e); + self.landed_in_dialog(&pending, Err(&e)); + } } } Ok(false) => { p["whole"] = json!(true); let pending = p.clone(); // Downloaded whole: the line waits for the download (`add_download`). - if let Err(e) = connectiontree::run_add_url(self, p, origin.as_deref()) { + let notice_mark = self.session.notice_mark(); + let started = if claim.is_some() { + self.add_url(&p, None, claim) + } else { + connectiontree::run_add_url(self, p, origin.as_deref()) + }; + if let Err(e) = started { self.session.request_failed(&pending, &e); self.landed_in_dialog(&pending, Err(&e)); + self.command_failed("layer.addUrl", &e, notice_mark); } } Err(e) => { self.session.request_failed(&p, &e); + if let Some(id) = claim { + self.session.download_failed(id); + } let e = if e.starts_with(&url) { e } else { format!("{url}: {e}") }; self.landed_in_dialog(&p, Err(&e)); self.session.notify(e, true); @@ -4267,9 +4413,9 @@ impl RustGisApp { match r { Ok(bytes) => self.add_landed(serviceui::Landed { id, url, params: p, origin }, bytes), Err(e) => { + self.session.request_failed(&p, &e); self.session.download_failed(id); rustgis_engine::cmd::geolibre::discard_pending(&mut self.session, &p); - self.session.request_failed(&p, &e); // Named without the address's keys, token or signature. let e = e.strip_prefix(url.as_str()).map(|r| r.trim_start_matches(": ").to_string()).unwrap_or(e); let why = rustgis_engine::sources::redact_text(&e); @@ -4323,7 +4469,7 @@ impl RustGisApp { } let before = self.session.sources.generation; let pending = p.clone(); - match self.run("layer.addDownload", p) { + match self.run_with_prepared("layer.addDownload", p, AppDispatch::Download(id)) { Ok(r) => { connectiontree::name_recent(self, before, origin.as_deref()); if r.get("zoomed").and_then(Value::as_bool) == Some(true) { @@ -4333,15 +4479,13 @@ impl RustGisApp { self.landed_in_dialog(&pending, Ok(())); } Err(e) => { + self.session.request_failed(&pending, &e); rustgis_engine::cmd::geolibre::discard_pending(&mut self.session, &pending); // Running it said the error already: again only to name the address it lacks. An // error may name it redacted. let named = e.starts_with(&url) || e.starts_with(&shown); let e = if named { e } else { format!("{shown}: {e}") }; self.landed_in_dialog(&pending, Err(&e)); - if !named { - self.session.notify(e, true); - } } } } @@ -4414,26 +4558,15 @@ impl RustGisApp { // progress belonging to that live generation, even before its answer has landed. let current: std::collections::HashSet = steps .iter() - .filter_map(|step| { - let (params, dialog, opened, message) = match step { - ServiceStep::Tiles { resolve, dialog, opened, .. } => { - (&resolve.params, *dialog, *opened, "Reading the tileset of {name}") - } - ServiceStep::Coverage { params, dialog, opened, .. } => (params, *dialog, *opened, "Describing {name}"), - }; - (dialog && opened == self.session.opened) - .then(|| i18n::fmt(message, &[("name", params.get("name").and_then(Value::as_str).unwrap_or(""))])) + .filter(|step| match step { + ServiceStep::Tiles { opened, .. } | ServiceStep::Coverage { opened, .. } => *opened == self.session.opened, }) + .filter_map(ServiceStep::pending_message) .collect(); for step in steps { + let pending = step.pending_message(); match step { ServiceStep::Tiles { mut resolve, dialog, origin, opened } => { - let pending = dialog.then(|| { - i18n::fmt( - "Reading the tileset of {name}", - &[("name", resolve.params.get("name").and_then(Value::as_str).unwrap_or(""))], - ) - }); if !self.service_read_current(opened, &resolve.doc, pending.as_deref().filter(|message| !current.contains(*message))) { continue; } @@ -4493,8 +4626,6 @@ impl RustGisApp { self.map.invalidate(); } ServiceStep::Coverage { mut params, slot, dialog, origin, opened } => { - let pending = dialog - .then(|| i18n::fmt("Describing {name}", &[("name", params.get("name").and_then(Value::as_str).unwrap_or(""))])); if !self.service_read_current( opened, params.get("url").and_then(Value::as_str).unwrap_or(""), @@ -4647,9 +4778,10 @@ impl RustGisApp { pluginstart::run_next(self, ctx); // Addresses commands asked for where they couldn't download themselves (the browser): // fetched off the frame and added when they land, as Home › Add › Add Data › From URL does. - for p in self.session.take_url_requests() { - if let Err(e) = self.run("ui.addUrl", p.clone()) { - self.session.request_failed(&p, &e); + for request in self.session.take_url_requests() { + let notice_mark = self.session.notice_mark(); + if let Err(e) = self.add_url(request.params(), Some(&request), None) { + self.command_failed("layer.addUrl", &e, notice_mark); } } // Plugin commands that waited for `host.fetch` answers (the browser) run again once @@ -5003,6 +5135,29 @@ pub(crate) fn click_in_one_frame(h: &mut egui_kittest::Harness<'_, RustGisApp>, h.input_mut().time = None; } +/// Runs a test that owns process-wide accounts/registrations in its own executable process. +#[cfg(all(test, not(target_arch = "wasm32")))] +pub(crate) fn isolate_account_test(name: &str) -> bool { + if std::env::var("RUSTGIS_ACCOUNT_SCENARIO").as_deref() == Ok(name) { + return false; + } + let output = std::env::current_exe().and_then(|executable| { + std::process::Command::new(executable).args(["--exact", name]).env("RUSTGIS_ACCOUNT_SCENARIO", name).output() + }); + assert!(output.is_ok(), "could not start isolated account scenario: {:?}", output.as_ref().err()); + let Ok(output) = output else { return true }; + let stdout = String::from_utf8_lossy(&output.stdout); + let mut summaries = stdout.lines().filter(|line| line.starts_with("test result:")); + let ran_one = summaries.next().is_some_and(|line| line.starts_with("test result: ok. 1 passed; 0 failed; 0 ignored;")) + && summaries.next().is_none(); + assert!( + output.status.success() && ran_one, + "isolated account scenario failed or ran no test:\n{stdout}\n{}", + String::from_utf8_lossy(&output.stderr) + ); + true +} + /// Tests that fetch through the tile cache take turns: it has one fetcher for the whole process, /// and a test that replaces it (`set_fetcher`) would answer another's downloads. So do tests that /// log in to a connection, as a login replaced or ended empties the cache (`cache::clear`), and @@ -5028,6 +5183,29 @@ pub(crate) mod fetching { pub(crate) fn turn() -> Turn { Turn { _held: TURN.lock().unwrap_or_else(|e| e.into_inner()) } } + + /// Waits at most ten seconds for a fixture's request, then restores blocking reads so + /// its existing stream timeout remains effective. Missing sends fail instead of hanging CI. + #[cfg(not(target_arch = "wasm32"))] + pub(crate) fn accept(server: &std::net::TcpListener) -> std::io::Result { + server.set_nonblocking(true)?; + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); + loop { + match server.accept() { + Ok((stream, _)) => { + stream.set_nonblocking(false)?; + return Ok(stream); + } + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { + if std::time::Instant::now() >= deadline { + return Err(std::io::Error::new(std::io::ErrorKind::TimedOut, "no request reached the test server")); + } + std::thread::sleep(std::time::Duration::from_millis(10)); + } + Err(e) => return Err(e), + } + } + } } #[cfg(test)] @@ -5131,25 +5309,6 @@ mod record_tests { use super::{DEFAULTS_TO_ACTIVE_LAYER, RustGisApp}; use serde_json::json; - /// The first request `server` gets, waited for up to 10 s: a request that never comes fails the - /// test instead of hanging it. - fn first_request(server: &std::net::TcpListener) -> std::net::TcpStream { - server.set_nonblocking(true).unwrap(); - let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); - loop { - match server.accept() { - Ok((stream, _)) => { - stream.set_nonblocking(false).unwrap(); - return stream; - } - Err(e) if e.kind() == std::io::ErrorKind::WouldBlock && std::time::Instant::now() < deadline => { - std::thread::sleep(std::time::Duration::from_millis(10)); - } - Err(e) => panic!("no request reached the test server: {e}"), - } - } - } - /// Terrain run with nothing named (from command search) records the DEM it took, so the /// step replays on it; a change of exaggeration on a terrain already on records none. #[test] @@ -5324,29 +5483,71 @@ mod record_tests { assert_eq!(said(&app), 1); } - /// Data added from a download isn't a step: its download is used up, so a task couldn't - /// add it again. + /// The actual URL completion receiver says when a successful download cannot be recorded. + /// Failed and revoked deliveries neither add a layer nor claim a recording omission. #[test] fn downloaded_data_isnt_recorded() { - let mut app = RustGisApp::with_sample(); - app.ws.recording = Some(Vec::new()); - app.record_step("layer.addDownload", &json!({"id": 1, "url": "https://example.com/rivers.geojson"}), None); - assert_eq!(app.ws.recording.as_ref().map(Vec::len), Some(0)); - assert!(app.session.notices.iter().any(|n| n.text.contains("session only"))); - app.record_step("layer.color", &json!({"layer": "Lakes", "color": "#123456"}), None); - assert_eq!(app.ws.recording.as_ref().map(Vec::len), Some(1)); + use rustgis_engine::{policy, render::rustgis_tiles::cache}; + use std::sync::{ + Arc, Mutex, + atomic::{AtomicBool, Ordering}, + }; + struct Live(AtomicBool); + impl policy::Revocation for Live { + fn allows(&self, _: policy::Effects) -> bool { + self.0.load(Ordering::SeqCst) + } + } + let _turn = crate::fetching::turn(); + for outcome in ["added", "transport failed", "decode failed", "revoked"] { + let held = Arc::new(Mutex::new(Vec::::new())); + let callbacks = held.clone(); + cache::set_fetcher(move |_, done| callbacks.lock().unwrap().push(done)); + let mut app = RustGisApp::with_sample(); + app.ws.recording = Some(Vec::new()); + let params = json!({"url":"https://recording.invalid/places.geojson", "name":"Recorded download"}); + let live = Arc::new(Live(AtomicBool::new(true))); + if outcome == "revoked" { + let scope = policy::Scope::agent(policy::AgentAccess::Allow, false, "record-download", None, live.clone()); + app.session.with_scope(scope, |s| s.request_url(¶ms)).unwrap().unwrap(); + let request = app.session.take_url_requests().pop().unwrap(); + app.add_url(request.params(), Some(&request), None).unwrap(); + } else { + app.run("ui.addUrl", params).unwrap(); + } + assert_eq!(app.ui.downloads.len(), 1, "the real UI download is pending"); + assert_eq!(held.lock().unwrap().len(), 1); + assert!(app.ws.recording.as_ref().unwrap().is_empty()); + let mark = app.session.notice_mark(); + live.0.store(outcome != "revoked", Ordering::SeqCst); + let result = match outcome { + "transport failed" => Err("synthetic download failure".to_string()), + "decode failed" => Ok(b"not a GeoJSON document".to_vec()), + _ => Ok(br#"{"type":"Point","coordinates":[1,2]}"#.to_vec()), + }; + held.lock().unwrap().pop().unwrap()(result); + app.poll_data_downloads(); + let succeeded = outcome == "added"; + assert!(app.ui.downloads.is_empty()); + assert_eq!(app.session.map().layer_by_name("Recorded download").is_some(), succeeded); + assert!(app.ws.recording.as_ref().unwrap().is_empty()); + let notices = app.session.notices_since(mark); + let omitted = notices.iter().filter(|n| n.text.contains("isn't recorded") && n.text.contains("session only")).count(); + assert_eq!(omitted, usize::from(succeeded), "{outcome}: {notices:?}"); + assert_eq!(notices.iter().filter(|n| n.error).count(), usize::from(!succeeded), "{outcome}: {notices:?}"); + } } #[test] #[cfg(not(target_arch = "wasm32"))] fn an_anonymous_download_never_acquires_a_saved_login() { + let _turn = crate::fetching::turn(); use rustgis_engine::render::rustgis_tiles::{cache, logins}; use std::io::{Read, Write}; - let _turn = crate::fetching::turn(); let server = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); let origin = format!("http://{}", server.local_addr().unwrap()); let reply = std::thread::spawn(move || { - let mut stream = first_request(&server); + let mut stream = crate::fetching::accept(&server).unwrap(); stream.set_read_timeout(Some(std::time::Duration::from_secs(5))).unwrap(); let mut request = Vec::new(); let mut chunk = [0; 1024]; @@ -5391,15 +5592,15 @@ mod record_tests { #[test] #[cfg(not(target_arch = "wasm32"))] fn a_landed_download_says_how_it_went_in_the_dialog() { + let _turn = crate::fetching::turn(); use rustgis_engine::render::rustgis_tiles::cache; use std::io::{Read, Write}; - let _turn = crate::fetching::turn(); // A server answering one request with `status` and a point's GeoJSON. let serve = |status: &'static str| { let server = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); let origin = format!("http://{}", server.local_addr().unwrap()); let reply = std::thread::spawn(move || { - let mut stream = first_request(&server); + let mut stream = crate::fetching::accept(&server).unwrap(); stream.set_read_timeout(Some(std::time::Duration::from_secs(5))).unwrap(); let mut request = Vec::new(); let mut chunk = [0; 1024]; @@ -5448,23 +5649,47 @@ mod record_tests { let other = add("200 OK", crate::serviceui::downloading("Rivers")); assert_eq!(other.dialogs.info, Some(crate::serviceui::downloading("Rivers"))); assert_eq!(other.dialogs.error, None); + // Closing the originating project drops a late answer normally, with one informational notice. + let mut late = RustGisApp::with_sample(); + let id = late.session.download_started(); + late.run("project.new", json!({})).unwrap(); + let mark = late.session.notice_mark(); + late.add_landed( + crate::serviceui::Landed { id, url: "https://example.com/roads.geojson".into(), params: json!({"name":"Roads"}), origin: None }, + b"{}".to_vec(), + ); + let notices = late.session.notices_since(mark); + assert_eq!(notices.len(), 1, "a normal late drop must not also report a continuation error"); + assert!(!notices[0].error && notices[0].text.contains("project was closed")); + assert!(late.session.map().layer_by_name("Roads").is_none()); } /// A `ui.addUrl` the app runs that fails is said once, by running it: after a probe found a /// file to download whole, and for a request a command queued. #[test] fn a_failed_ui_add_url_the_app_runs_is_said_once() { + #[cfg(not(target_arch = "wasm32"))] + if crate::isolate_account_test("record_tests::a_failed_ui_add_url_the_app_runs_is_said_once") { + return; + } let mut app = RustGisApp::with_sample(); let p = json!({"url": "https://x.org/a.csv", "connection": "no such connection"}); let errors = |app: &RustGisApp, mark| app.session.notices_since(mark).iter().filter(|n| n.error).count(); let mark = app.session.notice_mark(); let probe: crate::Slot> = Default::default(); *probe.lock().unwrap() = Some(Ok(false)); - app.ui.cog_probes.push((p.clone(), probe)); + app.ui.cog_probes.push((p.clone(), None, probe)); app.poll_data_downloads(); assert_eq!(errors(&app, mark), 1, "{:?}", app.session.notices_since(mark)); + // A valid admitted request can still fail to start when all claim slots are occupied. + let mut claims = Vec::new(); + for number in 0..64 { + app.session.request_url(&json!({"url":format!("https://x.org/held-{number}.csv")})).unwrap(); + let request = app.session.take_url_requests().pop().unwrap(); + claims.push(app.session.download_started_for(&request).unwrap()); + } let mark = app.session.notice_mark(); - app.session.request_url(&p).unwrap(); + app.session.request_url(&json!({"url":"https://x.org/queued.csv"})).unwrap(); let mut h = egui_kittest::Harness::builder().build_ui_state( |ui, app: &mut RustGisApp| { let ctx = ui.ctx().clone(); @@ -5475,14 +5700,18 @@ mod record_tests { h.step(); let app = h.state(); assert_eq!(errors(app, mark), 1, "{:?}", app.session.notices_since(mark)); + for claim in claims { + assert!(h.state().session.download_is_pending(claim), "another request keeps its claim"); + h.state_mut().session.download_failed(claim); + } } #[test] fn a_probe_dropped_by_another_project_says_so() { let mut app = RustGisApp::with_sample(); let url = "https://example.com/elevation.tif?token=hidden-758"; - app.ui.cog_probes.push((json!({"url": url, "name": "Elevation"}), Default::default())); - app.ui.cog_probes.push((json!({"url": "https://example.com/slope.tif", "name": "Slope"}), Default::default())); + app.ui.cog_probes.push((json!({"url": url, "name": "Elevation"}), None, Default::default())); + app.ui.cog_probes.push((json!({"url": "https://example.com/slope.tif", "name": "Slope"}), None, Default::default())); app.dialogs.info = Some(crate::serviceui::downloading("Elevation")); app.run("project.new", json!({})).unwrap(); assert!(app.ui.cog_probes.is_empty()); @@ -5501,14 +5730,14 @@ mod record_tests { #[test] #[cfg(not(target_arch = "wasm32"))] fn whitespace_downloads_keep_their_login_choice_and_read_options() { + let _turn = crate::fetching::turn(); use rustgis_engine::render::rustgis_tiles::{cache, logins}; use std::io::{Read, Write}; - let _turn = crate::fetching::turn(); for anonymous in [false, true] { let server = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); let origin = format!("http://{}", server.local_addr().unwrap()); let reply = std::thread::spawn(move || { - let mut stream = first_request(&server); + let mut stream = crate::fetching::accept(&server).unwrap(); stream.set_read_timeout(Some(std::time::Duration::from_secs(5))).unwrap(); let mut request = Vec::new(); let mut chunk = [0; 1024]; @@ -5598,13 +5827,20 @@ mod download_service_tests { /// layer is added successfully. Input is already downloaded into the usual completion slot. #[test] fn tile_resolver_completion_reports_query_settings_it_withheld() { - for (query, count) in [("collections=roads", 0), ("token=RGSYNTH_ui_20261103&collections=roads", 1)] { + let mut missing_notices = Vec::new(); + for (query, destination, expected, count) in [ + ("collections=roads", "", "?collections=roads", 0), + ("token=RGSYNTH_ui_20261103&collections=roads", "", "?collections=roads", 1), + ("collections=roads", "?collections=z", "?collections=z", 1), + ("collections=roads", "?collections=roads", "?collections=roads", 0), + ] { let mut app = RustGisApp::new(rustgis_engine::Session::new(rustgis_engine::Project::default())); let list = format!("https://h.invalid/svc/tiles?{query}"); let capabilities = json!({"title":"roads", "links":[{"rel":"tilesets-map", "href":list}]}).to_string(); - let body = - br#"{"links":[{"rel":"item", "type":"image/png", "href":"https://elsewhere.invalid/{tileMatrix}/{tileRow}/{tileCol}"}]}"# - .to_vec(); + let body = json!({"links":[{"rel":"item", "type":"image/png", + "href":format!("https://elsewhere.invalid/{{tileMatrix}}/{{tileRow}}/{{tileCol}}{destination}")}]}) + .to_string() + .into_bytes(); let resolve = super::TilesResolve { params: json!({"url":"https://h.invalid/svc", "type":"ogcapi", "layer":"map", "text":capabilities}), list: list.clone(), @@ -5619,12 +5855,151 @@ mod download_service_tests { app.ui.service_steps.push(super::ServiceStep::Tiles { resolve, dialog: true, origin: None, opened: app.session.opened }); app.poll_service_steps(); assert!(app.ui.service_steps.is_empty()); - let notices = app.session.notices.iter().filter(|n| n.text.contains("address(es) the service listed")).count(); - assert_eq!(notices, count); + let notices: Vec<_> = app.session.notices.iter().filter(|n| n.text.contains("address(es) the service listed")).collect(); + if notices.len() != count { + missing_notices.push(format!("{query} -> {destination}: expected {count} notices, got {}", notices.len())); + } + if let Some(notice) = notices.first() { + assert!(notice.text.starts_with("1 address(es)"), "{notices:?}"); + } assert_eq!(app.session.map().tiles.len(), 1); - assert_eq!(app.session.map().tiles[0].url, "https://elsewhere.invalid/{z}/{y}/{x}?collections=roads"); + assert_eq!(app.session.map().tiles[0].url, format!("https://elsewhere.invalid/{{z}}/{{y}}/{{x}}{expected}")); + assert!(app.session.map().tiles.iter().all(|t| !t.url.contains("RGSYNTH_"))); assert!(app.dialogs.error.is_none(), "{:?}", app.dialogs.error); } + // Refusing the advertised next document still reports the loss already counted. + // This is the existing receiver slot: no fetch starts, and polling it again says nothing. + for (query, count) in [("collections=roads", 0), ("token=RGSYNTH_ui_refused&collections=roads", 1)] { + let mut app = RustGisApp::new(rustgis_engine::Session::new(rustgis_engine::Project::default())); + let list = format!("https://h.invalid/svc/tiles?{query}"); + let body = json!({"tilesets":[{"tileMatrixSetId":"WebMercatorQuad", + "links":[{"rel":"self", "href":"http://127.0.0.1/tiles"}]}]}) + .to_string() + .into_bytes(); + let resolve = super::TilesResolve { + params: json!({"url":"https://h.invalid/svc", "type":"ogcapi", "layer":"map"}), + list: list.clone(), + doc: list, + crossed_origin: false, + read: 0, + withheld_addresses: 0, + vector: false, + fallback: "https://h.invalid/svc/tiles/{z}/{y}/{x}".into(), + slot: std::sync::Arc::new(std::sync::Mutex::new(Some(Ok(body)))), + }; + app.ui.service_steps.push(super::ServiceStep::Tiles { resolve, dialog: false, origin: None, opened: app.session.opened }); + app.poll_service_steps(); + assert!(app.ui.service_steps.is_empty() && app.session.map().tiles.is_empty()); + let notices: Vec<_> = app.session.notices.iter().filter(|n| n.text.contains("address(es) the service listed")).collect(); + if notices.len() != count { + missing_notices.push(format!("refused next for {query}: expected {count} notices, got {}", notices.len())); + } + assert_eq!(app.session.notices.iter().filter(|n| n.error).count(), 1); + assert!(app.session.notices.iter().any(|n| n.error && n.text.contains("local network"))); + let before = app.session.notices.len(); + app.poll_service_steps(); + assert_eq!(app.session.notices.len(), before, "no repeated refusal or loss notice"); + } + assert!(missing_notices.is_empty(), "{missing_notices:?}"); + } + + /// Real UI discovery never restores a login after a foreign hop, while a public saved + /// connection on a same-origin chain can still gain its legitimate login later. + #[test] + fn tile_discovery_keeps_foreign_lineage_and_same_origin_connections() { + use rustgis_engine::render::rustgis_tiles::{cache, logins}; + use std::sync::{Arc, Mutex}; + let _turn = crate::fetching::turn(); + let a = "https://ui-tile-lineage-a.invalid"; + let b = "https://ui-tile-lineage-b.invalid"; + for (foreign, returns, login) in [(true, true, true), (true, false, true), (false, false, true), (false, false, false)] { + let middle = format!("{}/set", if foreign { b } else { a }); + let last = format!("{}/final", if foreign && !returns { b } else { a }); + let next = |url: &str| json!({"tilesets":[{"tileMatrixSetURI":"WebMercatorQuad", "links":[{"rel":"self","href":url}]}]}); + let documents = [ + (format!("{a}/list"), next(&middle).to_string().into_bytes()), + (middle.clone(), next(&last).to_string().into_bytes()), + (last.clone(), json!({"links":[{"rel":"item","type":"image/png", "href":format!("{a}/tiles/{{tileMatrix}}/{{tileRow}}/{{tileCol}}.png")}]}).to_string().into_bytes()), + ]; + let requests = Arc::new(Mutex::new(Vec::new())); + let seen = requests.clone(); + cache::set_fetcher(move |url, done| { + let (url, _) = cache::split_download_limit(&url); + let plain = logins::inner(url).split('?').next().unwrap(); + if ![a, b].iter().any(|origin| plain == *origin || plain.starts_with(&format!("{origin}/"))) { + done(Err("outside the lineage fixture".into())); + return; + } + seen.lock().unwrap().push((plain.to_string(), logins::apply(url).unwrap().is_some())); + done(Ok(documents.iter().find(|(at, _)| at == plain).map(|(_, body)| body.clone()).unwrap_or_default())); + }); + let mut app = RustGisApp::new(rustgis_engine::Session::new(rustgis_engine::Project::default())); + let mut connection = String::new(); + for (origin, authenticated) in [(a, login), (b, true)] { + let c = app + .run( + "connection.new", + json!({"kind":"ogcapi","address":origin, + "auth":if authenticated {"header"} else {"none"}, "keyName":if authenticated {"X-Key"} else {""}}), + ) + .unwrap(); + if authenticated { + app.run("connection.login", json!({"id":c["id"],"apiKey":"synthetic-ui-lineage"})).unwrap(); + } + if origin == a { + connection = c["id"].as_str().unwrap().to_string(); + } + } + let text = json!({"title":"Lineage","links":[{"rel":"tilesets-map","href":format!("{a}/list")}]}).to_string(); + let source = crate::serviceui::ServiceSource { url: a.into(), kind: "ogcapi", text: text.clone(), documents: Vec::new() }; + let listing = app.session.run("service.layers", &json!({"url":a,"type":"ogcapi","text":text,"connection":connection})).unwrap(); + crate::serviceui::add_layer_from(&mut app, &source, &listing["layers"][0], "", None, false, None, Some(&connection)).unwrap(); + app.ws.recording = Some(Vec::new()); + for _ in 0..3 { + app.poll_service_steps(); + } + assert!(app.ui.service_steps.is_empty()); + assert_eq!( + app.session.map().tiles.len(), + 1, + "foreign={foreign}, returns={returns}, login={login}: {:?}", + app.session.notices.iter().map(|n| &n.text).collect::>() + ); + let steps = app.ws.recording.take().unwrap(); + assert_eq!(steps.len(), usize::from(!foreign)); + assert_eq!( + app.session.notices.iter().filter(|n| n.text.contains("tile discovery crossed origins")).count(), + usize::from(foreign), + "foreign={foreign}, returns={returns}, login={login}: {:?}", + app.session.notices.iter().map(|n| &n.text).collect::>() + ); + if !foreign { + assert_eq!(steps[0].command, "service.add"); + assert_eq!(steps[0].params["connection"], connection); + assert!(!steps[0].params.to_string().contains(rustgis_engine::data::login_address::PREFIX)); + } + assert_eq!(*requests.lock().unwrap(), [(format!("{a}/list"), login), (middle, login && !foreign), (last, login && !foreign),]); + let tile = app.session.map().tiles.first().unwrap(); + assert_eq!(tile.anonymous, foreign); + assert_eq!(tile.connection, if foreign { "" } else { &connection }); + assert_eq!(logins::apply(&tile.url).unwrap().is_some(), login && !foreign); + app.run("connection.edit", json!({"id":connection,"auth":"header","keyName":"X-Key"})).unwrap(); + app.run("connection.login", json!({"id":connection,"apiKey":"synthetic-ui-lineage-later"})).unwrap(); + let (saved, _) = app.session.project.saved_text(None).unwrap(); + app.run("project.openText", json!({"text":saved})).unwrap(); + app.run("connection.login", json!({"id":connection,"apiKey":"synthetic-ui-lineage-reopened"})).unwrap(); + let tile = app.session.map().tiles.first().unwrap(); + assert_eq!(tile.anonymous, foreign); + assert_eq!(tile.connection, if foreign { "" } else { &connection }); + assert_eq!(logins::apply(&tile.url).unwrap().is_some(), !foreign); + let url = tile.url.replace("{z}", "0").replace("{y}", "0").replace("{x}", "0"); + cache::get(&url, cache::Want::Image); + assert_eq!( + requests.lock().unwrap().last(), + Some(&(logins::inner(&url).to_string(), !foreign)), + "render fetch preserves lineage after reload" + ); + } } #[test] @@ -5855,6 +6230,142 @@ mod download_service_tests { let mut app = RustGisApp::with_sample(); assert_eq!(app.run("gp.fileDelivered", json!({"id": u64::MAX})), Err("no pending file with that id".into())); assert_eq!(app.session.notices.iter().filter(|n| n.error && n.text == "no pending file with that id").count(), 1); + let mark = app.session.notice_mark(); + let error = app.acknowledge_gp_file(u64::MAX, None).unwrap_err(); + assert_eq!(error, "no pending file with that id"); + // A surrounding command uses the same reporter; the helper's notice must not be repeated. + app.command_failed("gp.fileDelivered", &error, mark); + assert_eq!(app.session.notices_since(mark).iter().filter(|n| n.error && n.text == error).count(), 1); + } +} + +#[cfg(test)] +mod peer_output_tests { + use super::*; + + /// Drives the actual receiving wrapper, including the UI state a direct Session call misses. + #[test] + fn peer_project_uses_import_authority_and_resets_old_ui_work() { + use rustgis_engine::render::rustgis_tiles::cache; + use std::sync::{Arc, Mutex}; + let _turn = crate::fetching::turn(); + let mut app = RustGisApp::with_sample(); + app.session.project.maps[0].layers[0].dataset.source = Some("https://h.invalid/?token=RGSYNTH_4".into()); + let (text, _) = app.session.project.saved_text(None).unwrap(); + app.session.run("task.new", &json!({"name":"prior","steps":[]})).unwrap(); + app.assistant.queued = Some("old project action".into()); + app.ui.web_file = Some("prior.rgis".into()); + let generation = app.session.opened; + app.ui.cog_probes.push(( + json!({"url":"https://peer-probe.invalid/elevation.tif?token=RGSYNTH_peer_probe","name":"Elevation"}), + None, + Default::default(), + )); + app.dialogs.info = Some(crate::serviceui::downloading("Elevation")); + let probe_mark = app.session.notice_mark(); + app.open_peer_text(&text).unwrap(); + assert_ne!(app.session.opened, generation); + assert!(app.ui.cog_probes.is_empty()); + assert!(app.dialogs.info.is_none(), "a retired peer probe cannot keep the reading message open"); + let retired = app + .session + .notices_since(probe_mark) + .iter() + .filter(|n| n.text.contains("peer-probe.invalid/elevation.tif") && n.text.contains("project was closed")) + .count(); + assert_eq!(retired, 1, "the peer replacement must say which pending read was omitted"); + assert!(app.session.notices_since(probe_mark).iter().all(|n| !n.text.contains("RGSYNTH_peer_probe"))); + let clean_mark = app.session.notice_mark(); + app.open_peer_text(&text).unwrap(); + assert!( + app.session.notices_since(clean_mark).iter().all(|n| !n.text.contains("project was closed")), + "a clean peer replacement does not replay a retired read" + ); + assert!(app.session.project.tasks.is_empty()); + assert!(app.ui.web_file.is_none() && app.assistant.queued.is_none()); + assert!(!serde_json::to_string(&app.session.project).unwrap().contains("RGSYNTH_4")); + + // Both replacement paths retire ready answers and reads still waiting for their document. + for peer in [false, true] { + for ready in [false, true] { + let sent = Arc::new(Mutex::new(Vec::new())); + let held = Arc::new(Mutex::new(Vec::<(String, cache::Done)>::new())); + let (seen, callbacks) = (sent.clone(), held.clone()); + cache::set_fetcher(move |url, done| { + let plain = cache::split_download_limit(&url).0.to_string(); + if !plain.starts_with("https://review29-service.invalid/") { + done(Err("outside the service fixture".into())); + return; + } + seen.lock().unwrap().push(plain.clone()); + callbacks.lock().unwrap().push((plain, done)); + }); + let mut app = RustGisApp::with_sample(); + let (text, _) = app.session.project.saved_text(None).unwrap(); + let api = "https://review29-service.invalid/collections"; + let caps = json!({"collections":[ + {"id":"next","links":[{"rel":"tilesets-map","href":format!("{api}/next/tiles")}]}, + {"id":"ready","links":[{"rel":"tilesets-map","href":format!("{api}/ready/tiles")}]} + ]}) + .to_string(); + let listing = app.session.run("service.layers", &json!({"url":api,"type":"ogcapi","text":caps})).unwrap(); + let source = crate::serviceui::ServiceSource { url: api.into(), kind: "ogcapi", text: caps, documents: Vec::new() }; + for layer in listing["layers"].as_array().unwrap() { + crate::serviceui::add_layer_from(&mut app, &source, layer, "", None, ready, None, None).unwrap(); + } + let caps = r#" + socOld coverage + 5 49.25.8 50 + "#; + let url = "https://review29-service.invalid/wcs"; + let listing = app.session.run("service.layers", &json!({"url":url,"type":"wcs","text":caps})).unwrap(); + let source = crate::serviceui::ServiceSource { url: url.into(), kind: "wcs", text: caps.into(), documents: Vec::new() }; + crate::serviceui::add_layer_from(&mut app, &source, &listing["layers"][0], "", None, ready, None, None).unwrap(); + assert_eq!(app.ui.service_steps.len(), 3); + assert_eq!(sent.lock().unwrap().len(), 3); + let finish = || { + for (url, done) in std::mem::take(&mut *held.lock().unwrap()) { + let body = if url.contains("/next/tiles") { + json!({"tilesets":[{"tileMatrixSetURI":"WebMercatorQuad","links":[{"rel":"self","href":"https://review29-service.invalid/next-document"}]}]}).to_string() + } else if url.contains("/ready/tiles") { + json!({"links":[{"rel":"item","type":"image/png","href":"https://review29-service.invalid/tiles/{tileMatrix}/{tileRow}/{tileCol}.png"}]}).to_string() + } else { + r#"49.2 550 5.8"#.into() + }; + done(Ok(body.into_bytes())); + } + }; + if ready { + finish(); + } + if peer { + app.open_peer_text(&text).unwrap(); + } else { + app.session.run("project.openText", &json!({"text":text})).unwrap(); + } + let (layers, tiles) = (app.session.map().layers.len(), app.session.map().tiles.len()); + app.dialogs.info = Some("Current project info".into()); + app.dialogs.error = Some("Current project error".into()); + let mark = app.session.notice_mark(); + app.poll_service_steps(); + let drained = app.ui.service_steps.is_empty(); + if !ready { + finish(); + } + app.poll_service_steps(); + // Drain any forbidden follow-up too, so a failing test leaves no in-flight fetch. + for (_, done) in std::mem::take(&mut *held.lock().unwrap()) { + done(Err("stale service request".into())); + } + assert!(drained && app.ui.service_steps.is_empty(), "old steps must retire before inspecting their slots"); + assert_eq!(sent.lock().unwrap().len(), 3, "no old next-document or coverage request may start"); + assert_eq!((app.session.map().layers.len(), app.session.map().tiles.len()), (layers, tiles)); + assert!(app.ui.downloads.is_empty() && app.ui.cog_probes.is_empty()); + assert_eq!(app.dialogs.info.as_deref(), Some("Current project info")); + assert_eq!(app.dialogs.error.as_deref(), Some("Current project error")); + assert_eq!(app.session.notices_since(mark).iter().filter(|n| n.text.contains("project was closed")).count(), 3); + } + } } } @@ -6265,53 +6776,3 @@ mod look_tests { assert!(app.use_theme("Gone").is_err(), "a name the list does not hold"); } } - -#[cfg(test)] -mod peer_output_tests { - use super::*; - - /// Drives the actual receiving wrapper, including the UI state a direct Session call misses. - #[test] - fn peer_project_uses_import_authority_and_resets_old_ui_work() { - let mut app = RustGisApp::with_sample(); - app.session.project.maps[0].layers[0].dataset.source = Some("https://h.invalid/?token=RGSYNTH_4".into()); - let (text, _) = app.session.project.saved_text(None).unwrap(); - app.session.trusted_tasks.insert("prior".into()); - app.assistant.queued = Some("old project action".into()); - app.ui.web_file = Some("prior.rgis".into()); - let generation = app.session.opened; - app.ui.cog_probes.push(( - json!({"url":"https://peer-probe.invalid/elevation.tif?token=RGSYNTH_peer_probe","name":"Elevation"}), - Default::default(), - )); - app.dialogs.info = Some(crate::serviceui::downloading("Elevation")); - let probe_mark = app.session.notice_mark(); - app.open_peer_text(&text).unwrap(); - assert_ne!(app.session.opened, generation); - assert!(app.ui.cog_probes.is_empty()); - assert!(app.dialogs.info.is_none(), "a retired peer probe cannot keep the reading message open"); - let retired = app - .session - .notices_since(probe_mark) - .iter() - .filter(|n| n.text.contains("peer-probe.invalid/elevation.tif") && n.text.contains("project was closed")) - .count(); - assert_eq!(retired, 1, "the peer replacement must say which pending read was omitted"); - assert!(app.session.notices_since(probe_mark).iter().all(|n| !n.text.contains("RGSYNTH_peer_probe"))); - let clean_mark = app.session.notice_mark(); - app.open_peer_text(&text).unwrap(); - assert!( - app.session.notices_since(clean_mark).iter().all(|n| !n.text.contains("project was closed")), - "a clean peer replacement does not replay a retired read" - ); - assert!(app.session.trusted_tasks.is_empty()); - assert!(app.ui.web_file.is_none() && app.assistant.queued.is_none()); - assert!(!serde_json::to_string(&app.session.project).unwrap().contains("RGSYNTH_4")); - } -} - -/// Resolved tile discovery shares the normal command bookkeeping. -enum AppDispatch { - Direct, - ResolvedTiles { template: String, crossed_origin: bool }, -} diff --git a/crates/ui-egui/src/localagents.rs b/crates/ui-egui/src/localagents.rs index 6552561883..e7eed9b0f1 100644 --- a/crates/ui-egui/src/localagents.rs +++ b/crates/ui-egui/src/localagents.rs @@ -222,16 +222,21 @@ impl Run { Ok(None) | Err(FramingError::Io(_)) => break, Err(_) => continue, }; - let (out, new) = match session.lock() { - Ok(mut s) => s.line(&line), + let new = match session.lock() { + Ok(mut s) => { + let (out, new) = s.line(&line); + // Queue writes under the state lock: auto-off cannot overtake a + // prompt that passed readiness, nor can that prompt follow cancel. + for l in out { + if let Err(why) = send(&stdin, &l) { + push(s.exited(false, &why)); + break 'reading; + } + } + new + } Err(_) => break, }; - for l in out { - if let Err(why) = send(&stdin, &l) { - push(session.lock().map(|mut s| s.exited(false, &why)).unwrap_or_default()); - break 'reading; - } - } push(new); } } @@ -251,26 +256,42 @@ impl Run { Ok(run) } - /// What it did and asked since last asked. + /// What it did and asked since last asked, including an unanswered mode-change timeout. pub fn events(&self) -> Vec { - self.events.lock().map(|mut e| std::mem::take(&mut *e)).unwrap_or_default() - } - - /// A follow-up task, once it has finished the last one; false when it can't take one. - pub fn follow_up(&self, task: &str) -> bool { - let lines = self.session.lock().ok().and_then(|mut s| s.follow_up(task)); - match lines { - Some(lines) => { - for line in lines { - if let Err(why) = send(&self.stdin, &line) { - self.input_failed(&why); - return false; - } + let mut new = self.session.lock().map(|mut s| s.poll()).unwrap_or_default(); + let mut events = self.events.lock().map(|mut e| std::mem::take(&mut *e)).unwrap_or_default(); + events.append(&mut new); + events + } + + /// Apply the current shared setting and start a follow-up under one session lock, then + /// queue their lines in order. A reply cannot release work between those two decisions. + pub fn follow_up(&self, task: &str, auto: bool) -> bool { + self._grant.set_auto(auto); + let Ok(mut s) = self.session.lock() else { return false }; + let (mut lines, mut new) = s.set_auto(auto); + let accepted = if let Some((follow, events)) = s.follow_up(task) { + lines.extend(follow); + new.extend(events); + true + } else { + false + }; + for line in lines { + if let Err(why) = send(&self.stdin, &line) { + new.extend(s.exited(false, &why)); + if let Ok(mut events) = self.events.lock() { + events.extend(new); } - true + return false; } - None => false, } + if let Ok(mut events) = self.events.lock() { + events.extend(new); + } + // Even an immediate readiness refusal was accepted as a turn: its Done event tells + // the caller why. Returning false would instead claim the thread had ended. + accepted } fn input_failed(&self, why: &str) { @@ -280,11 +301,21 @@ impl Run { } } - /// Whether the user lets it act without asking (its full-access mode), from its next task on. + /// Apply the shared approval setting immediately, forwarding cancellation and mode changes + /// in session order. A turn already running unsafely is stopped with a visible report. pub fn set_auto(&self, auto: bool) { self._grant.set_auto(auto); if let Ok(mut s) = self.session.lock() { - s.set_auto(auto); + let (lines, mut new) = s.set_auto(auto); + for line in lines { + if let Err(why) = send(&self.stdin, &line) { + new.extend(s.exited(false, &why)); + break; + } + } + if let Ok(mut events) = self.events.lock() { + events.extend(new); + } } } @@ -400,6 +431,7 @@ mod tests { /// The argument that makes this binary the fake agent (also a test filter matching nothing). const CHILD_MARK: &str = "rustgis-acp-child-mode"; + const HELD_MODES: &str = "rustgis-acp-held-modes"; /// The fake agent: ACP over standard input and output. A task with ASK asks for permission /// first; one with HANG works until it's cancelled; any other is done at once. @@ -408,6 +440,9 @@ mod tests { if !std::env::args().any(|a| a == CHILD_MARK) { return; } + let held_modes = std::env::args().any(|a| a == HELD_MODES); + let mut mode_reply: Option<(Value, String)> = None; + let mut prompts = 0; let mut out = std::io::stdout(); let _ = writeln!(out); let send = |out: &mut std::io::Stdout, v: Value| { @@ -416,6 +451,7 @@ mod tests { }; let update = |u: Value| json!({"jsonrpc": "2.0", "method": "session/update", "params": {"sessionId": "fake", "update": u}}); let say = |t: &str| update(json!({"sessionUpdate": "agent_message_chunk", "content": {"type": "text", "text": t}})); + let mark = |title: &str| update(json!({"sessionUpdate": "tool_call", "toolCallId": "fixture", "title": title})); let mut lines = std::io::stdin().lock().lines().map_while(Result::ok); let mut hanging: Option = None; while let Some(line) = lines.next() { @@ -426,13 +462,42 @@ mod tests { &mut out, json!({"jsonrpc": "2.0", "id": id, "result": {"protocolVersion": 1, "agentCapabilities": {}, "authMethods": []}}), ), - Some("session/new") => send(&mut out, json!({"jsonrpc": "2.0", "id": id, "result": {"sessionId": "fake"}})), + Some("session/new") => { + let mut modes = vec![json!({"id": "agent"})]; + if held_modes { + modes.push(json!({"id": "agent-full-access"})); + } + send( + &mut out, + json!({"jsonrpc": "2.0", "id": id, "result": {"sessionId": "fake", "modes": {"currentModeId": "agent", "availableModes": modes}}}), + ); + } + Some("session/set_mode") if held_modes => { + let mode = msg["params"]["modeId"].as_str().unwrap().to_string(); + send(&mut out, mark(&format!("held:{mode}"))); + assert!(mode_reply.replace((id, mode)).is_none(), "mode changes must stay serialized"); + } + Some("test/release_mode") if held_modes => { + let (id, mode) = mode_reply.take().unwrap(); + send(&mut out, json!({"jsonrpc": "2.0", "id": id, "result": {}})); + send(&mut out, mark(&format!("released:{mode}"))); + } + Some("test/full_mode") if held_modes => { + send(&mut out, update(json!({"sessionUpdate": "current_mode_update", "currentModeId": "agent-full-access"}))) + } + Some("test/barrier") if held_modes => { + send(&mut out, mark(&format!("{}:{prompts}", msg["params"]["name"].as_str().unwrap()))) + } Some("session/cancel") => { if let Some(pid) = hanging.take() { send(&mut out, json!({"jsonrpc": "2.0", "id": pid, "result": {"stopReason": "cancelled"}})); } } Some("session/prompt") => { + if held_modes { + prompts += 1; + send(&mut out, mark("prompt")); + } let task = msg["params"]["prompt"][0]["text"].as_str().unwrap_or("").to_string(); if task.contains("BLOCK") { send( @@ -502,6 +567,145 @@ mod tests { }) } + /// The actual checkbox must reach a held ACP upgrade before the next thread poll. The + /// child's FIFO barrier counts every prompt, including events a later app poll consumes. + #[test] + fn local_agents_checkbox_contains_held_upgrade_and_queued_follow_up() { + use egui_kittest::kittest::Queryable; + use rustgis_assistant::agent::{Agent, Step}; + use rustgis_assistant::provider::Config; + use std::sync::{Arc, Mutex}; + + /// Read the real process events without draining them before the app's next poll. + fn wait_mark(events: &Arc>>, prefix: &str) -> String { + let deadline = Instant::now() + Duration::from_secs(10); + loop { + if let Some(title) = events.lock().unwrap().iter().find_map(|e| match e { + Event::Tool(title) if title == prefix || (prefix.ends_with(':') && title.starts_with(prefix)) => Some(title.clone()), + _ => None, + }) { + return title; + } + assert!(Instant::now() < deadline, "fake ACP barrier timed out: {prefix}"); + std::thread::sleep(Duration::from_millis(5)); + } + } + /// Inject a provider reply through Agent's normal delegation queue, with no HTTP. + fn delegate(a: &mut Agent, s: &mut rustgis_engine::Session, thread: Option<&str>) { + let mut args = json!({"agent": "goose", "task": "Map the cities"}); + if let Some(thread) = thread { + args["thread"] = json!(thread); + } + let body = json!({"choices": [{"message": {"role": "assistant", "content": null, + "tool_calls": [{"id": "d", "type": "function", "function": {"name": "delegate", "arguments": args.to_string()}}]}, + "finish_reason": "tool_calls"}]}) + .to_string() + .into_bytes(); + assert!(matches!( + a.receive(a.generation(), Ok(rustgis_assistant::HttpResponse { status: 200, body, headers: vec![] }), s), + Step::Send(_) + )); + } + + let relay = rustgis_assistant::relay::Relay::start(0).unwrap(); + let work = tempfile::tempdir().unwrap(); + let exe = std::env::current_exe().unwrap(); + let command = vec![ + exe.display().to_string(), + "localagents::tests::fake_acp_agent".into(), + CHILD_MARK.into(), + HELD_MODES.into(), + "--exact".into(), + "--nocapture".into(), + "--test-threads=1".into(), + ]; + let ctx = egui::Context::default(); + let run = Run::start( + AgentSpec::from_id("goose").unwrap(), + &command, + "Map the cities", + &relay, + &exe, + work.path().to_path_buf(), + true, + ctx.clone(), + ) + .unwrap(); + let (events, input) = (run.events.clone(), run.stdin.clone()); + let mut app = crate::RustGisApp::with_sample(); + let prefs = Arc::new(Mutex::new(Vec::new())); + let saved = prefs.clone(); + app.services.save_pref = Some(Box::new(move |key, value| saved.lock().unwrap().push((key.to_string(), value.to_string())))); + crate::agentthreads::insert_test_run(&mut app, run); + let mut h = crate::assistantpane::tests::local_agents_harness(app); + h.run_steps(3); + wait_mark(&events, "held:agent-full-access"); + h.get_by_label("Approve agents' actions without asking").click(); + h.step(); + assert!(!h.state().assistant.agents_auto); + assert!(prefs.lock().unwrap().contains(&("assistant_agents_auto".into(), "off".into()))); + // No agentthreads poll occurs between the real checkbox callback and this reply. + let control = + |method: &str, name: &str| super::send(&input, &json!({"method": method, "params": {"name": name}}).to_string()).unwrap(); + control("test/release_mode", ""); + wait_mark(&events, "released:agent-full-access"); + control("test/barrier", "after-upgrade"); + assert_eq!(wait_mark(&events, "after-upgrade:"), "after-upgrade:0"); + assert_eq!(wait_mark(&events, "held:agent"), "held:agent"); + control("test/release_mode", ""); + wait_mark(&events, "released:agent"); + control("test/barrier", "after-asking"); + assert_eq!(wait_mark(&events, "after-asking:"), "after-asking:1"); + assert!(events.lock().unwrap().iter().any(|e| matches!(e, Event::Done { error: false, .. }))); + crate::agentthreads::poll(h.state_mut(), &ctx); + + // Queue a real parent-Agent follow-up while auto is on, then change the checkbox + // before poll consumes that queue. The Run must use the current shared flag. + h.get_by_label("Approve agents' actions without asking").click(); + h.step(); + assert!(h.state().assistant.agents_auto); + control("test/full_mode", ""); + control("test/barrier", "full-between-turns"); + assert_eq!(wait_mark(&events, "full-between-turns:"), "full-between-turns:1"); + let config = Config { + provider: "openai".into(), + base_url: "https://model.invalid/v1".into(), + model: "fixture".into(), + key: "synthetic-test".into(), + account: String::new(), + client_id: String::new(), + session: String::new(), + reasoning: None, + tool_output_token_limit: rustgis_assistant::tools::DEFAULT_OUTPUT_TOKENS, + }; + let app = h.state_mut(); + let mut a = Agent::new(config, &app.session); + a.set_harnesses(vec![AgentSpec::from_id("goose").unwrap()]); + a.set_agents_auto(true); + assert!(matches!(a.ask("Map it", &app.session), Step::Send(_))); + delegate(&mut a, &mut app.session, None); + let first = a.take_delegations(); + assert_eq!(first[0].thread, "goose-1"); + a.thread_report("goose-1", "Done".into(), false); + delegate(&mut a, &mut app.session, Some("goose-1")); + app.assistant.agent = Some(a); + h.get_by_label("Approve agents' actions without asking").click(); + h.step(); + assert!(!h.state().assistant.agents_auto); + crate::agentthreads::poll(h.state_mut(), &ctx); + control("test/barrier", "queued-follow-up"); + assert_eq!(wait_mark(&events, "queued-follow-up:"), "queued-follow-up:1", "a queued follow-up waits for asking mode"); + control("test/release_mode", ""); + // The response marker follows Session processing, so its generated prompt is + // already queued before this final FIFO barrier. + wait_mark(&events, "released:agent"); + control("test/barrier", "follow-up-done"); + assert_eq!(wait_mark(&events, "follow-up-done:"), "follow-up-done:2"); + drop(input); + drop(h); + super::finish_stops(); + } + /// Two tasks of one agent within the same second get folders of their own. #[test] fn work_folders_never_collide() { @@ -538,8 +742,8 @@ mod tests { assert_eq!(report(&ev), Some(("Did the task.".into(), false))); assert!(!run.finished()); // A follow-up that asks for permission: allowed. - assert!(run.follow_up("ASK to fetch")); - assert!(!run.follow_up("one at a time"), "a working thread takes no second task"); + assert!(run.follow_up("ASK to fetch", false)); + assert!(!run.follow_up("one at a time", false), "a working thread takes no second task"); let ev = until_done(&run); assert!(ev.contains(&Event::Tool("Fetch the gauges".into())), "{ev:?}"); let Some(Event::Permission(p)) = ev.iter().find(|e| matches!(e, Event::Permission(_))).cloned() else { @@ -549,14 +753,14 @@ mod tests { run.answer(&p, Choice::Allow); assert_eq!(report(&until_done(&run)), Some(("Allowed: fetched 3 gauges.".into(), false))); // Declined. - assert!(run.follow_up("ASK again")); + assert!(run.follow_up("ASK again", false)); let Some(Event::Permission(p)) = until_done(&run).into_iter().find(|e| matches!(e, Event::Permission(_))) else { panic!("no request") }; run.answer(&p, Choice::Reject); assert_eq!(report(&until_done(&run)), Some(("Declined.".into(), false))); // Its relay token is its own, revoked with it; Stop ends a task that never would. - assert!(run.follow_up("HANG")); + assert!(run.follow_up("HANG", false)); std::thread::sleep(Duration::from_millis(300)); let pid = run.pid; let grant_file = run._grant.token_file().to_path_buf(); diff --git a/crates/ui-egui/src/mapview.rs b/crates/ui-egui/src/mapview.rs index 7d8a573e7a..2a103cf477 100644 --- a/crates/ui-egui/src/mapview.rs +++ b/crates/ui-egui/src/mapview.rs @@ -4861,6 +4861,7 @@ mod tests { /// included, so every step of a slider drag made the map flicker. #[test] fn a_style_change_over_tiles_keeps_the_frame_instead_of_a_preview() { + let _turn = crate::fetching::turn(); let mut app = RustGisApp::with_sample(); // Nothing listens there: the tiles fail, the map still draws. app.run("map.addTiles", json!({"url": "http://127.0.0.1:9/{z}/{x}/{y}.png", "name": "Offline"})).expect("tile layer"); diff --git a/crates/ui-egui/src/modelbuilder.rs b/crates/ui-egui/src/modelbuilder.rs index 27bbeb0b42..5001ca555b 100644 --- a/crates/ui-egui/src/modelbuilder.rs +++ b/crates/ui-egui/src/modelbuilder.rs @@ -368,7 +368,7 @@ fn load_saved(app: &mut RustGisApp, name: &str) -> Result<(), String> { Some(graph) => graph.clone(), None => model::checked_steps_to_graph(&m.steps)?, }; - let imported = !app.session.trusted_models.contains(&m.name.to_ascii_lowercase()); + let imported = !app.session.item_trusted("model", &m.name); let n = m.name.clone(); app.mb.load(&n, Some(n.clone()), graph, imported); Ok(()) diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index de79614674..6a4a6a213d 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -2785,7 +2785,7 @@ pub fn poll_tool_run(app: &mut RustGisApp, ctx: &egui::Context) { }; 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}))); + let r = rustgis_engine::cmd::gp::tool_complete(&mut app.session, id, done).and_then(|_| app.finish_gp(id)); finished(app, tool, r); } @@ -2836,7 +2836,7 @@ pub fn poll_plugin_run(app: &mut RustGisApp, ctx: &egui::Context) { }; let (id, tool) = (job.id, job.tool); app.gp.plugin_run = None; - let r = rustgis_engine::cmd::gp::plugin_complete(&mut app.session, id, result).and_then(|_| app.run("gp.finish", json!({"id": id}))); + let r = rustgis_engine::cmd::gp::plugin_complete(&mut app.session, id, result).and_then(|_| app.finish_gp(id)); finished(app, tool, r); } @@ -2895,7 +2895,7 @@ pub fn poll_geolibre(app: &mut RustGisApp, ctx: &egui::Context) { } if let Some((id, tool, out)) = app.gp.web_run.as_ref().and_then(|(id, tool, slot)| take(slot).map(|o| (*id, *tool, o))) { app.gp.web_run = None; - let r = rustgis_engine::cmd::gp::external_complete(&mut app.session, id, out).and_then(|_| app.run("gp.finish", json!({"id": id}))); + let r = rustgis_engine::cmd::gp::external_complete(&mut app.session, id, out).and_then(|_| app.finish_gp(id)); finished(app, tool, r); } if app.gp.web_load.is_some() || app.gp.web_run.is_some() { @@ -7183,6 +7183,7 @@ mod tests { /// from its menu, and is dragged to another place. #[test] fn a_tile_layer_is_listed_and_moved_among_the_layers() { + let _turn = crate::fetching::turn(); use egui_kittest::kittest::Queryable; let mut app = RustGisApp::with_sample(); for c in ["Cities", "Countries"] { @@ -7307,6 +7308,7 @@ mod tests { /// mode identifies it), and clicking a layer's row picks that instead. #[test] fn a_tile_layers_row_is_picked_with_a_click() { + let _turn = crate::fetching::turn(); use egui_kittest::kittest::Queryable; let mut app = RustGisApp::with_sample(); app.run("map.addTiles", json!({"url": "https://wms.example.org/{z}/{x}/{y}.png", "name": "Depths", "overlay": true})).unwrap(); diff --git a/crates/ui-egui/src/pluginpanel.rs b/crates/ui-egui/src/pluginpanel.rs index 08c586655c..bd89cb016c 100644 --- a/crates/ui-egui/src/pluginpanel.rs +++ b/crates/ui-egui/src/pluginpanel.rs @@ -576,6 +576,78 @@ mod tests { plugins::unload("ui-url"); } + /// The real deferred request and probe loop refuse a withdrawn plugin before a fallback send. + #[test] + fn permission_policy_plugin_deferred_probe_refuses_before_fallback() { + let _turn = crate::fetching::turn(); + use rustgis_engine::{plugins, render::rustgis_tiles::cache}; + use std::sync::{ + Arc, Mutex, + atomic::{AtomicUsize, Ordering}, + }; + let sent = Arc::new(AtomicUsize::new(0)); + let count = sent.clone(); + let held = Arc::new(Mutex::new(Vec::new())); + let callbacks = held.clone(); + cache::set_fetcher(move |_, done| { + count.fetch_add(1, Ordering::SeqCst); + callbacks.lock().unwrap().push(done); + }); + let mut app = RustGisApp::with_sample(); + let load = || { + plugins::load(plugins::Package::from_zip(&plugins::testing::package("ui-policy-probe", &["edit", "network"])).unwrap(), "test") + .unwrap() + }; + let p = json!({"url":"http://127.0.0.1:9/policy-held.fgb","name":"Held source"}); + load(); + app.run("plugin:ui-policy-probe/f", json!({"op":"layer.addUrl","params":p})).unwrap(); + plugins::unload("ui-policy-probe"); + assert!(app.session.take_url_requests().is_empty()); + assert_eq!(sent.load(Ordering::SeqCst), 0); + load(); + app.run("plugin:ui-policy-probe/f", json!({"op":"layer.addUrl","params":p})).unwrap(); + let request = app.session.take_url_requests().pop().unwrap(); + app.add_url(request.params(), Some(&request), None).unwrap(); + assert_eq!(sent.load(Ordering::SeqCst), 1, "the actual probe starts under the live owner"); + assert_eq!(app.ui.cog_probes.len(), 1); + plugins::unload("ui-policy-probe"); + *app.ui.cog_probes[0].2.lock().unwrap() = Some(Ok(false)); + app.poll_data_downloads(); + assert_eq!(sent.load(Ordering::SeqCst), 1, "revoked probe starts no whole-file fallback"); + assert!(app.session.map().layer_by_name("Held source").is_none()); + for done in std::mem::take(&mut *held.lock().unwrap()) { + done(Err("probe revoked".into())); + } + // The same real probe can succeed while its owner remains loaded. Reuse the built-in + // FGB writer and installed fetcher; no server or replacement completion token is needed. + let bytes = rustgis_engine::data::source::fgb::write(&app.session.map().layer_by_name("Cities").unwrap().dataset).unwrap(); + cache::set_fetcher(move |url, done| { + let (url, _) = cache::split_download_limit(&url); + let (_, range) = cache::split_range(url); + let answer = match range { + Some((a, b)) => bytes.get(a as usize..b.saturating_add(1).min(bytes.len() as u64) as usize).unwrap_or_default().to_vec(), + None => bytes.clone(), + }; + done(Ok(answer)); + }); + load(); + let p = json!({"url":"http://127.0.0.1:9/policy-recorded.fgb", "name":"Recorded source"}); + app.run("plugin:ui-policy-probe/f", json!({"op":"layer.addUrl", "params":p})).unwrap(); + let request = app.session.take_url_requests().pop().unwrap(); + app.ws.recording = Some(Vec::new()); + app.add_url(request.params(), Some(&request), None).unwrap(); + assert_eq!(app.ui.cog_probes.len(), 1, "the actual live probe still lands through its UI receiver"); + app.poll_data_downloads(); + let steps = app.ws.recording.as_ref().unwrap(); + assert!(app.session.map().layer_by_name("Recorded source").is_some()); + assert_eq!(steps.len(), 1, "successful in-place delivery records its replayable command"); + assert_eq!(steps[0].command, "layer.addUrl"); + assert_eq!(steps[0].params["url"], p["url"]); + assert!(steps[0].params.get("id").is_none(), "opaque completion ids are not replay parameters"); + assert!(steps[0].params.get("request").is_none(), "request receipts are not replay parameters"); + plugins::unload("ui-policy-probe"); + } + /// A GeoTIFF asked for as a scan keeps only its pixels: the app downloads it whole, once, /// without first reading its header to see whether it could be read in place. #[test] @@ -627,6 +699,10 @@ mod tests { /// that is said once, with its address, whether or not that holds a key (#1202). #[test] fn a_late_download_is_refused_in_one_notice() { + #[cfg(not(target_arch = "wasm32"))] + if crate::isolate_account_test("pluginpanel::tests::a_late_download_is_refused_in_one_notice") { + return; + } use rustgis_engine::plugins; let pkg = plugins::Package::from_zip(&plugins::testing::package("ui-late-said", &["edit", "network"])).unwrap(); plugins::load(pkg, "test").unwrap(); @@ -636,12 +712,12 @@ mod tests { // The app takes it and starts downloading; the command is run again for another // address, so nothing waits for the first now. let taken = app.session.take_url_requests(); - assert_eq!(taken.len(), 1, "{taken:?}"); + assert_eq!(taken.len(), 1); + let id = app.session.download_started_for(&taken[0]).unwrap(); app.run("plugin:ui-late-said/f", json!({"op": "layer.addUrl", "params": {"url": "https://x.org/second.csv"}})).unwrap(); - let id = app.session.download_started(); let mark = app.session.notice_mark(); let layers = app.session.map().layers.len(); - let landed = crate::serviceui::Landed { id, url: first.to_string(), params: taken[0].clone(), origin: None }; + let landed = crate::serviceui::Landed { id, url: first.to_string(), params: taken[0].params().clone(), origin: None }; app.add_landed(landed, b"name,lon,lat\nA,10,50\n".to_vec()); assert_eq!(app.session.map().layers.len(), layers, "not added"); let said: Vec = app.session.notices_since(mark).iter().filter(|n| n.error).map(|n| n.text.clone()).collect(); @@ -656,6 +732,10 @@ mod tests { /// out, from what the app keeps of the request: a file read whole, and a service's page (#1202). #[test] fn a_failed_download_answers_the_command_waiting_at_once() { + #[cfg(not(target_arch = "wasm32"))] + if crate::isolate_account_test("pluginpanel::tests::a_failed_download_answers_the_command_waiting_at_once") { + return; + } use rustgis_engine::plugins; use rustgis_engine::render::rustgis_tiles::cache; let _turn = crate::fetching::turn(); @@ -666,8 +746,8 @@ mod tests { let mut app = RustGisApp::with_sample(); app.run("plugin:ui-fail/f", json!({"op": "layer.addUrl", "params": params})).unwrap(); let taken = app.session.take_url_requests(); - assert_eq!(taken.len(), 1, "{taken:?}"); - app.run("ui.addUrl", taken[0].clone()).unwrap(); + assert_eq!(taken.len(), 1); + app.add_url(taken[0].params(), Some(&taken[0]), None).unwrap(); let ctx = egui::Context::default(); let mut ran = Vec::new(); for _ in 0..400 { @@ -690,6 +770,10 @@ mod tests { /// it fails, not the wait running out (#1202). #[test] fn a_failed_service_read_answers_the_command_waiting() { + #[cfg(not(target_arch = "wasm32"))] + if crate::isolate_account_test("pluginpanel::tests::a_failed_service_read_answers_the_command_waiting") { + return; + } use rustgis_engine::plugins; let pkg = plugins::Package::from_zip(&plugins::testing::package("ui-pages", &["edit", "network"])).unwrap(); plugins::load(pkg, "test").unwrap(); @@ -697,9 +781,9 @@ mod tests { let url = "https://x.org/collections/a/items"; app.run("plugin:ui-pages/f", json!({"op": "layer.addUrl", "params": {"url": url, "service": true}})).unwrap(); let taken = app.session.take_url_requests(); - assert_eq!(taken.len(), 1, "{taken:?}"); - let id = app.session.download_started(); - let landed = crate::serviceui::Landed { id, url: url.to_string(), params: taken[0].clone(), origin: None }; + assert_eq!(taken.len(), 1); + let id = app.session.download_started_for(&taken[0]).unwrap(); + let landed = crate::serviceui::Landed { id, url: url.to_string(), params: taken[0].params().clone(), origin: None }; crate::serviceui::go_on(&mut app, landed, 0, rustgis_engine::data::ogc::Step::Failed("its second page failed".into())); let ran = app.session.poll_plugin_retries(); assert_eq!(ran.len(), 1, "{ran:?}"); diff --git a/crates/ui-egui/src/pluginstart.rs b/crates/ui-egui/src/pluginstart.rs index 8d7571391a..5d7ae00605 100644 --- a/crates/ui-egui/src/pluginstart.rs +++ b/crates/ui-egui/src/pluginstart.rs @@ -26,6 +26,7 @@ const STALE_AFTER: Duration = Duration::ZERO; struct Start { id: String, params: Value, + prepared: rustgis_engine::policy::PreparedCommand, frames: u8, /// The project it was asked for in ([`rustgis_engine::Session::opened`]). opened: u64, @@ -40,12 +41,19 @@ pub struct State { /// Leaves plugin command `id` to start once the window has shown it. The same command with the /// same parameters, already waiting, isn't asked twice. -pub(crate) fn ask(app: &mut RustGisApp, id: &str, params: Value) { +pub(crate) fn ask(app: &mut RustGisApp, id: &str, params: Value) -> Result<(), String> { let opened = app.session.opened; - let waiting = &mut app.ui.plugin_start.waiting; - if !waiting.iter().any(|s| s.id == id && s.params == params && s.opened == opened) { - waiting.push_back(Start { id: id.to_string(), params, frames: 0, opened }); + if let Some(start) = app.ui.plugin_start.waiting.iter_mut().find(|s| s.id == id && s.params == params && s.opened == opened) { + if !start.prepared.is_current() { + let prepared = app.session.prepare_command(id, ¶ms)?; + start.prepared = prepared; + start.frames = 0; + } + return Ok(()); } + let prepared = app.session.prepare_command(id, ¶ms)?; + app.ui.plugin_start.waiting.push_back(Start { id: id.to_string(), params, prepared, frames: 0, opened }); + Ok(()) } /// Drops the commands asked for in a project another has replaced, and says which: they would act @@ -79,7 +87,7 @@ pub(crate) fn run_next(app: &mut RustGisApp, ctx: &egui::Context) { let Some(start) = app.ui.plugin_start.waiting.pop_front() else { return }; let held = ctx.input(|i| i.pointer.any_down()); let began = web_time::Instant::now(); - let _ = app.run(&start.id, start.params); + let _ = app.run_prepared(&start.id, start.params, start.prepared); app.ui.plugin_start.clicks_stale = !held && began.elapsed() >= STALE_AFTER; } @@ -151,7 +159,11 @@ mod tests { use serde_json::json; fn launch(plugin: &str) -> egui_kittest::Harness<'static, RustGisApp> { - let pkg = plugins::Package::from_zip(&plugins::testing::package(plugin, &["edit"])).unwrap(); + launch_with_permissions(plugin, &["edit"]) + } + + fn launch_with_permissions(plugin: &str, permissions: &[&str]) -> egui_kittest::Harness<'static, RustGisApp> { + let pkg = plugins::Package::from_zip(&plugins::testing::package(plugin, permissions)).unwrap(); plugins::load(pkg, "test").unwrap(); let mut h = egui_kittest::Harness::builder().with_size(vec2(1400.0, 860.0)).with_max_steps(64).build_ui_state( |ui, app: &mut RustGisApp| { @@ -169,6 +181,109 @@ mod tests { json!({"op": "layer.rename", "params": {"layer": from, "name": to}}) } + /// Queued plugins leave owned URL requests to the app; string zoom options survive delivery. + #[test] + fn queued_plugin_downloads_keep_their_claim_and_zoom() { + let _turn = crate::fetching::turn(); + use rustgis_engine::render::rustgis_tiles::cache; + use std::sync::{Arc, Mutex}; + let held = Arc::new(Mutex::new(Vec::new())); + let callbacks = held.clone(); + cache::set_fetcher(move |url, done| callbacks.lock().unwrap().push((url, done))); + let mut h = launch_with_permissions("ui-queued-url", &["edit", "network"]); + let camera = h.state().session.map().camera; + for zoom in ["false", "true", "on"] { + let name = format!("Places {zoom}"); + let url = format!("http://127.0.0.1:9/queued-{zoom}.csv"); + let params = json!({"url":url,"name":name,"x":"east","y":"north","zoom":zoom}); + let mark = h.state().session.notice_mark(); + h.state_mut().run_by_user("plugin:ui-queued-url/f", json!({"op":"layer.addUrl","params":params})).unwrap(); + h.step(); + h.step(); + assert!(running(h.state()).is_some()); + run_next(h.state_mut(), &egui::Context::default()); + assert!(running(h.state()).is_none()); + assert!(held.lock().unwrap().is_empty(), "dispatch queues the request before any send"); + let requests = h.state_mut().session.take_url_requests(); + assert_eq!(requests.len(), 1, "the queued plugin leaves an opaque request for the frame loop"); + let request = &requests[0]; + // The opaque engine request adds its receipt; every caller option stays exact. + let receipt = request.params()["request"].as_u64().filter(|number| *number > 0).unwrap(); + let mut expected = params.clone(); + expected["anonymous"] = json!(true); + expected["request"] = json!(receipt); + assert_eq!(request.params(), &expected, "the claim preserves the caller's options, anonymous reads and engine receipt"); + let app = h.state_mut(); + app.session.map_mut().camera = camera; + app.ui.link_zoomed = false; + app.add_url(request.params(), Some(request), None).unwrap(); + let (asked, done) = held.lock().unwrap().pop().unwrap(); + let (asked, limit) = cache::split_download_limit(&asked); + assert_eq!(asked, url); + assert_eq!(limit, Some(cache::MAX_DOWNLOAD)); + assert_eq!(app.ui.downloads[0].2["zoom"], zoom, "completion preserves the claim's JSON representation"); + assert_eq!(app.ui.downloads[0].2["request"], receipt, "completion retains the opaque request's exact receipt"); + done(Ok(b"east,north\n1,2\n3,4\n".to_vec())); + app.poll_data_downloads(); + let places = app.session.map().layer_by_name(&name).unwrap(); + assert_eq!(places.dataset.features.len(), 2); + assert!(places.anonymous); + assert!(places.connection.is_empty()); + if zoom == "false" { + assert_eq!(app.session.map().camera, camera, "false keeps the view"); + } else { + assert!(app.session.map().camera.res < camera.res, "true and on frame the new points"); + assert!(app.ui.link_zoomed); + } + assert!(app.ui.downloads.is_empty()); + assert!(!app.session.notices_since(mark).iter().any(|n| n.error), "{:?}", app.session.notices_since(mark)); + } + plugins::unload("ui-queued-url"); + } + + /// The delay never transfers an admitted command to another load with the same plugin id. + #[test] + fn a_queued_command_refuses_a_replacement_plugin_load() { + let mut h = launch("ui-pinned"); + let mark = h.state().session.notice_mark(); + h.state_mut().run_by_user("plugin:ui-pinned/f", rename("Rivers", "Streams")).unwrap(); + plugins::unload("ui-pinned"); + let pkg = plugins::Package::from_zip(&plugins::testing::package("ui-pinned", &["edit"])).unwrap(); + plugins::load(pkg, "test").unwrap(); + h.run(); + assert!(h.state().session.map().layer_by_name("Rivers").is_some()); + assert!(h.state().session.map().layer_by_name("Streams").is_none()); + assert!(h.state().session.notices_since(mark).iter().any(|n| n.error)); + assert!(running(h.state()).is_none()); + plugins::unload("ui-pinned"); + let mark = h.state().session.notice_mark(); + assert!(h.state_mut().run_by_user("plugin:ui-pinned/f", rename("Rivers", "Streams")).is_err()); + assert_eq!(h.state().session.notices_since(mark).iter().filter(|n| n.error).count(), 1, "preparation failures are reported too"); + assert!(running(h.state()).is_none()); + } + + #[test] + fn a_fresh_click_replaces_a_stale_queued_plugin_load() { + let mut h = launch("ui-pinned-retry"); + let id = "plugin:ui-pinned-retry/f"; + let params = rename("Rivers", "Streams"); + let mark = h.state().session.notice_mark(); + h.state_mut().run_by_user(id, params.clone()).unwrap(); + h.state_mut().run_by_user(id, params.clone()).unwrap(); + assert_eq!(h.state().ui.plugin_start.waiting.len(), 1, "current identical clicks still deduplicate"); + plugins::unload("ui-pinned-retry"); + let pkg = plugins::Package::from_zip(&plugins::testing::package("ui-pinned-retry", &["edit"])).unwrap(); + plugins::load(pkg, "test").unwrap(); + h.state_mut().run_by_user(id, params).unwrap(); + assert_eq!(h.state().ui.plugin_start.waiting.len(), 1, "the explicit retry replaces the stale start"); + h.run(); + assert!(h.state().session.map().layer_by_name("Rivers").is_none()); + assert!(h.state().session.map().layer_by_name("Streams").is_some()); + assert!(!h.state().session.notices_since(mark).iter().any(|n| n.error)); + assert!(running(h.state()).is_none()); + plugins::unload("ui-pinned-retry"); + } + /// Asked for by the user, a command runs only after a frame has said so; asked twice, once. #[test] fn a_command_is_shown_running_before_it_runs() { @@ -319,6 +434,10 @@ mod tests { /// to reach egui. #[test] fn clicks_held_for_a_command_run_again_are_dropped() { + #[cfg(not(target_arch = "wasm32"))] + if crate::isolate_account_test("pluginstart::tests::clicks_held_for_a_command_run_again_are_dropped") { + return; + } let mut h = launch("ui-again"); let pkg = plugins::Package::from_zip(&plugins::testing::package("ui-again-net", &["edit", "network"])).unwrap(); plugins::load(pkg, "test").unwrap(); @@ -331,8 +450,8 @@ mod tests { h.state_mut().run("plugin:ui-again-net/f", params).unwrap(); // Taken before a frame would: it would start the download. let taken = h.state_mut().session.take_url_requests(); - assert_eq!(taken.len(), 1, "{taken:?}"); - let id = h.state_mut().session.download_started(); + assert_eq!(taken.len(), 1); + let id = h.state_mut().session.download_started_for(&taken[0]).unwrap(); h.step(); assert!(!h.state().ui.plugin_start.clicks_stale, "nothing ran"); assert!(h.state().session.plugin_retries_waiting()); @@ -340,7 +459,7 @@ mod tests { h.event(button(true)); h.step(); } - let landed = crate::serviceui::Landed { id, url, params: taken[0].clone(), origin: None }; + let landed = crate::serviceui::Landed { id, url, params: taken[0].params().clone(), origin: None }; h.state_mut().add_landed(landed, b"name,lon,lat\nA,10,50\n".to_vec()); h.step(); assert!(!h.state().session.plugin_retries_waiting(), "it ran again"); diff --git a/crates/ui-egui/src/readjob.rs b/crates/ui-egui/src/readjob.rs index 793ce16851..8bc14236f1 100644 --- a/crates/ui-egui/src/readjob.rs +++ b/crates/ui-egui/src/readjob.rs @@ -30,6 +30,8 @@ pub struct Reads { struct Asked { command: &'static str, params: Value, + /// Admitted when asked, including while another read runs; never recreated at start or landing. + ahead: Option, /// What it reads, as its notices name it: the file, or the layer. what: String, opened: u64, @@ -65,7 +67,7 @@ pub fn run(app: &mut RustGisApp, command: &'static str, p: Value) -> Result) -> Result { +pub fn run_then(app: &mut RustGisApp, command: &'static str, mut p: Value, then: Option) -> Result { if !app.reads_off_frame { let r = app.run(command, p); return answered(app, then, r); @@ -73,6 +75,28 @@ pub fn run_then(app: &mut RustGisApp, command: &'static str, p: Value, then: Opt let layer = (command == "layer.loadAll") .then(|| app.session.layer_id(&p).ok().and_then(|id| app.session.map().layer(id)).map(|l| (l.instance_id(), l.name.clone()))) .flatten(); + let notice_mark = app.session.notice_mark(); + if layer.is_some() { + let id = app.session.layer_id(&p)?; + if p.is_null() { + p = json!({}); + } + let Some(params) = p.as_object_mut() else { + let error = tl!("Load All parameters must be an object").to_string(); + app.command_failed(command, &error, notice_mark); + not_run(app, then, error.clone()); + return Err(error); + }; + params.insert("layer".into(), json!(id)); + } + let ahead = match reads::read_ahead(&mut app.session, command, &p) { + Ok(ahead) => ahead, + Err(error) => { + app.command_failed(command, &error, notice_mark); + not_run(app, then, error.clone()); + return Err(error); + } + }; // Load All asked again for a layer it is reading already. if let Some((instance, _)) = &layer // (One whose map or project went never loads it: a new request waits behind it.) @@ -89,6 +113,7 @@ pub fn run_then(app: &mut RustGisApp, command: &'static str, p: Value, then: Opt let asked = Asked { command, params: p, + ahead: Some(ahead), what, opened: app.session.opened, map: app.session.map().instance_id(), @@ -188,34 +213,60 @@ fn placed(app: &RustGisApp, asked: &Asked) -> Result { /// Starts `asked`'s read with `p`, or runs it here when it reads nothing ahead (or its thread /// can't start). -fn start(app: &mut RustGisApp, asked: Asked, p: Value) -> Result { - // Picked on this thread, so a bug in the picking must not take the app down: it reads here. - let picked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| reads::read_ahead(&app.session, asked.command, &p))); - let Ok(Some(ahead)) = picked else { - let r = app.run(asked.command, p); - return answered(app, asked.then, r); +fn start(app: &mut RustGisApp, mut asked: Asked, p: Value) -> Result { + let Some(ahead) = asked.ahead.take() else { + return complete_here(app, asked, p, Err(tl!("This read was already started").to_string())); }; let name = ahead.name; let control = Control::stoppable(); + if !ahead.reads() { + let read = ahead.run(&control); + return complete_here(app, asked, p, read); + } let (ctl, slot) = (control.clone(), crate::Slot::default()); let out = slot.clone(); - // A streamed layer's source read on the thread keeps its requests in the session's diagnostics. + // Retain the admitted operation if the OS cannot start its worker: the synchronous + // fallback must use that same owner, rather than dispatching a new native User command. + let held = crate::Slot::default(); + *held.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(ahead); + let work = held.clone(); let diagnostics = app.session.diagnostics.capture(); let spawned = std::thread::Builder::new().name(name.into()).spawn(move || { let _diagnostics = diagnostics.enter(); - let read = ahead.run(&ctl); - // Filled even when its lock is poisoned, or the read would show as running forever. + let ahead = work.lock().unwrap_or_else(std::sync::PoisonError::into_inner).take(); + let read = ahead.ok_or_else(|| tl!("This read was already started").to_string()).and_then(|ahead| ahead.run(&ctl)); *out.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(read); }); if let Err(e) = spawned { log::warn!("{name} reads here: its thread didn't start: {e}"); - let r = app.run(asked.command, p); - return answered(app, asked.then, r); + let read = held + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .take() + .ok_or_else(|| tl!("This read was already started").to_string()) + .and_then(|ahead| ahead.run(&control)); + return complete_here(app, asked, p, read); } app.reads.job = Some(ReadJob { asked, gone: None, name, control, slot }); Ok(json!({"started": name})) } +/// Completes the admitted read, then calls its callback with the actual outcome. +/// A pre-dispatch failure is reported once even when a queued start discards its error. +fn complete_here(app: &mut RustGisApp, asked: Asked, p: Value, done: Result) -> Result { + let read = match done { + Ok(read) => read, + Err(error) => { + let notice_mark = app.session.notice_mark(); + app.command_failed(asked.command, &error, notice_mark); + not_run(app, asked.then, error.clone()); + return Err(error); + } + }; + let r = app.run_read(asked.command, p, read); + answered(app, asked.then, r) +} + /// Runs the command a read was for once it's done (or says why it wasn't), then starts the next /// waiting: called every frame. `true` while one is under way or waiting. pub fn land(app: &mut RustGisApp) -> bool { @@ -260,36 +311,9 @@ pub fn land(app: &mut RustGisApp) -> bool { not_run(app, asked.then, left_too.clone()); } } else { - match done { - Ok(read) => { - read.placed(&mut app.session); - // A failure is said by the command. - let r = app.run(job.asked.command, p); - let _ = answered(app, job.asked.then, r); - app.session.drop_read(); - app.map.invalidate(); - } - // A bug in a model's or script's run on a copy of the session: it runs here, - // as it would have. - Err(e) if matches!(job.asked.command, "model.run" | "script.run") => { - app.session.notify( - crate::i18n::fmt( - "{tool} couldn't work in the background ({e}): it ran here", - &[("tool", tl_raw!(job.name)), ("e", &e)], - ), - false, - ); - let r = app.run(job.asked.command, p); - let _ = answered(app, job.asked.then, r); - app.map.invalidate(); - } - // A bug in a reader: said, and the next goes on. - Err(e) => { - let said = crate::i18n::fmt("{what}: {e}", &[("what", &job.asked.what), ("e", &e)]); - app.session.notify(said.clone(), true); - not_run(app, job.asked.then, said); - } - } + // The engine consumes the original owner before the callback sees its result. + let _ = complete_here(app, job.asked, p, done); + app.map.invalidate(); } } } diff --git a/crates/ui-egui/src/s3ui.rs b/crates/ui-egui/src/s3ui.rs index b8d8e2d27e..3716f850ec 100644 --- a/crates/ui-egui/src/s3ui.rs +++ b/crates/ui-egui/src/s3ui.rs @@ -655,8 +655,35 @@ fn size_text(n: u64) -> String { mod tests { use super::*; + /// Keeps S3 mutations out of parallel tests' process-wide account snapshots. + #[cfg(not(target_arch = "wasm32"))] + fn isolate_account_test(name: &str) -> bool { + if std::env::var("RUSTGIS_ACCOUNT_SCENARIO").as_deref() == Ok(name) { + return false; + } + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", name]) + .env("RUSTGIS_ACCOUNT_SCENARIO", name) + .output() + .unwrap(); + let stdout = String::from_utf8_lossy(&output.stdout); + let mut summaries = stdout.lines().filter(|line| line.starts_with("test result:")); + let ran_one = summaries.next().is_some_and(|line| line.starts_with("test result: ok. 1 passed; 0 failed; 0 ignored;")) + && summaries.next().is_none(); + assert!( + output.status.success() && ran_one, + "isolated account scenario failed or ran no test:\n{stdout}\n{}", + String::from_utf8_lossy(&output.stderr) + ); + true + } + #[test] fn anonymous_connection_can_be_kept_and_picked_after_disconnect() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_account_test("s3ui::tests::anonymous_connection_can_be_kept_and_picked_after_disconnect") { + return; + } let mut app = RustGisApp::with_sample(); app.ui.s3.name = "keep-s3-test".into(); app.ui.s3.endpoint = "https://s3.example.com".into(); @@ -684,6 +711,10 @@ mod tests { #[test] fn picking_typed_keys_clears_previous_secrets_and_waits_for_connect() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_account_test("s3ui::tests::picking_typed_keys_clears_previous_secrets_and_waits_for_connect") { + return; + } let mut app = RustGisApp::with_sample(); let v = app .run( @@ -719,6 +750,10 @@ mod tests { #[test] fn browsing_through_a_saved_login_names_it_and_a_session_connection_does_not() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_account_test("s3ui::tests::browsing_through_a_saved_login_names_it_and_a_session_connection_does_not") { + return; + } let mut app = RustGisApp::with_sample(); let v = app .run( @@ -742,6 +777,10 @@ mod tests { #[test] fn editing_or_removing_the_picked_connection_refills_the_form_and_drops_its_listing() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_account_test("s3ui::tests::editing_or_removing_the_picked_connection_refills_the_form_and_drops_its_listing") { + return; + } let mut app = RustGisApp::with_sample(); let v = app .run( @@ -778,6 +817,10 @@ mod tests { #[test] fn s3_saved_picker_and_connection_entry_reauthenticate_a_foreign_session_login() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_account_test("s3ui::tests::s3_saved_picker_and_connection_entry_reauthenticate_a_foreign_session_login") { + return; + } use rustgis_engine::data::secrets::MemorySecrets; use std::sync::Arc; let _turn = crate::fetching::turn(); @@ -814,6 +857,10 @@ mod tests { #[test] fn s3_saved_picker_retains_ordinary_session_login_semantics() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_account_test("s3ui::tests::s3_saved_picker_retains_ordinary_session_login_semantics") { + return; + } let mut app = RustGisApp::with_sample(); let name = "s3-ui-unmapped-session"; app.run( diff --git a/crates/ui-egui/src/serviceui.rs b/crates/ui-egui/src/serviceui.rs index 9351a935e2..5c3b238f28 100644 --- a/crates/ui-egui/src/serviceui.rs +++ b/crates/ui-egui/src/serviceui.rs @@ -34,6 +34,16 @@ pub struct ServiceSource { pub documents: Vec, } +/// Uses the same translated tileset progress text when discovery starts and when it retires. +pub(crate) fn reading_tileset(name: &str) -> String { + i18n::fmt("Reading the tileset of {name}", &[("name", name)]) +} + +/// Uses the same translated coverage progress text when discovery starts and when it retires. +pub(crate) fn describing(name: &str) -> String { + i18n::fmt("Describing {name}", &[("name", name)]) +} + /// A service type as the add dialog and `ui.addService` name it, `auto` for any other text. pub(crate) fn kind_of(t: &str) -> &'static str { match t { @@ -153,7 +163,7 @@ pub(crate) fn add_layer_from( }; let slot = crate::fetch_into(&describe); app.ui.service_steps.push(crate::ServiceStep::Coverage { params: p, slot, dialog, origin, opened: app.session.opened }); - return Ok(i18n::fmt("Describing {name}", &[("name", &title)])); + return Ok(describing(&title)); } let mut p = json!({"url": src.url, "layer": name, "text": src.text, "documents": src.documents, "source_url": l.get("source_url")}); if src.kind != "auto" { @@ -185,10 +195,10 @@ pub(crate) fn add_layer_from( let resolve = crate::TilesResolve { params: p, list: list.clone(), - doc: list.clone(), crossed_origin: anonymous || rustgis_engine::render::rustgis_tiles::logins::origin(&src.url) != rustgis_engine::render::rustgis_tiles::logins::origin(&list), + doc: list, read: 0, withheld_addresses: 0, vector, @@ -196,7 +206,7 @@ pub(crate) fn add_layer_from( slot, }; app.ui.service_steps.push(crate::ServiceStep::Tiles { resolve, dialog, origin, opened: app.session.opened }); - Ok(i18n::fmt("Reading the tileset of {name}", &[("name", &title)])) + Ok(reading_tileset(&title)) } None => crate::connectiontree::run_named(app, "service.add", p, origin.as_deref()).map(|_| added(&title)), } @@ -507,34 +517,43 @@ fn next_request(app: &mut RustGisApp, params: &Value, pages: &Pages) -> Result { - noted(app, &landed, note); - app.add_landed_read(landed, *dataset, report); - } - Step::Failed(e) => { - app.session.download_failed(landed.id); - app.session.request_failed(&landed.params, &e); - let url = &landed.url; - let e = if e.starts_with(url.as_str()) { e } else { format!("{url}: {e}") }; - app.landed_in_dialog(&landed.params, Err(&e)); - app.session.notify(e, true); - } - Step::Fetch(pages) => { - let next = next_request(app, &landed.params, &pages); - let slot = match next { - // A layer's description: small, and soon given up on. - Ok(url) if pages.describing() => fetch_description(&url, left.min(ogc::DESCRIPTION_MAX)), - Ok(url) => fetch_into_limited(&url, left), - Err(e) => Slot::new(std::sync::Mutex::new(Some(Err(e)))), - }; - app.ui.service_pages.push(PageRead { landed, pages, left, slot, asked: web_time::Instant::now() }); - } - Step::Done(bytes, note) => { - noted(app, &landed, note); - app.add_landed(landed, bytes); - } + if let Err(e) = app.session.download_continues(landed.id) { + app.session.request_failed(&landed.params, &e); + app.landed_in_dialog(&landed.params, Err(&e)); + app.session.notify(e, true); + return; } + let still = app.session.download_still(landed.id); + rustgis_engine::render::rustgis_tiles::cache::with_optional_still(still, || { + match step { + Step::Decoded(dataset, report, note) => { + noted(app, &landed, note); + app.add_landed_read(landed, *dataset, report); + } + Step::Failed(e) => { + app.session.request_failed(&landed.params, &e); + app.session.download_failed(landed.id); + let url = &landed.url; + let e = if e.starts_with(url.as_str()) { e } else { format!("{url}: {e}") }; + app.landed_in_dialog(&landed.params, Err(&e)); + app.session.notify(e, true); + } + Step::Fetch(pages) => { + let next = next_request(app, &landed.params, &pages); + let slot = match next { + // A layer's description: small, and soon given up on. + Ok(url) if pages.describing() => fetch_description(&url, left.min(ogc::DESCRIPTION_MAX)), + Ok(url) => fetch_into_limited(&url, left), + Err(e) => Slot::new(std::sync::Mutex::new(Some(Err(e)))), + }; + app.ui.service_pages.push(PageRead { landed, pages, left, slot, asked: web_time::Instant::now() }); + } + Step::Done(bytes, note) => { + noted(app, &landed, note); + app.add_landed(landed, bytes); + } + } + }); } /// Says what was left unread of a service layer's features, by its name. diff --git a/crates/ui-egui/src/stacui.rs b/crates/ui-egui/src/stacui.rs index 3bfa7c8a8a..d2df0d6d01 100644 --- a/crates/ui-egui/src/stacui.rs +++ b/crates/ui-egui/src/stacui.rs @@ -1243,6 +1243,7 @@ mod tests { #[test] fn a_deferred_token_add_keeps_its_original_connection_when_selection_changes() { + let _turn = crate::fetching::turn(); let mut app = RustGisApp::with_sample(); let api = "http://127.0.0.1:9/stac"; let one = app.run("connection.new", json!({"kind":"stac", "name":"One", "address":api})).unwrap(); @@ -1341,6 +1342,7 @@ mod tests { /// the dropped listing doesn't hold it up. #[test] fn a_catalog_picked_during_a_pending_listing_is_browsed() { + let _turn = crate::fetching::turn(); use egui_kittest::kittest::Queryable; let mut app = RustGisApp::with_sample(); // Nothing listens on port 9 of the loopback address: the browse is refused, harmlessly. diff --git a/crates/ui-egui/src/tilelegend.rs b/crates/ui-egui/src/tilelegend.rs index 5cdec9e229..f46e165c64 100644 --- a/crates/ui-egui/src/tilelegend.rs +++ b/crates/ui-egui/src/tilelegend.rs @@ -192,6 +192,7 @@ mod tests { /// command. #[test] fn a_tile_layers_legend_opens_under_it() { + let _turn = crate::fetching::turn(); let mut app = RustGisApp::with_sample(); for c in ["Cities", "Countries"] { app.session.run("layer.expand", &json!({"layer": c, "on": false})).unwrap(); diff --git a/docs/mcp.md b/docs/mcp.md index f0a72aeb07..c0fac2fc66 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -376,6 +376,10 @@ protocol. ### Local-agent approvals +These rules govern requests authenticated with the run's token. Local agent programs run with +your operating-system account and their own tool permissions; a program allowed to read the main +relay token can use its native-client access described above. + Local agents launched by the assistant use `Relay::grant_reviewed(auto)`, not an unrestricted run grant. With automatic approval off, gated tool calls wait in a RustGIS-side queue scoped to that grant and exact message, including all tool calls in a batch. `--yes` and the general relay @@ -385,6 +389,11 @@ other clients' reads can still run while it waits. Explicit automatic approval b extra queue for that run, but not the reserved-command refusals. Ordinary MCP clients retain the access policy described above. +The displayed batch is frozen with its effective parameters, targets and runtime loads. If +those change while it waits, RustGIS refuses it and the agent must request it again. Delayed +downloads, tool results and screenshots retain the original grant, check it before starting +another stage and before landing, and can be completed only once by their owner. + Stopping the relay through its command or status-bar control also turns off its remembered agent-control choice. Copyable setup commands are labelled for POSIX shells or PowerShell and quote the executable path literally; use the matching shell. diff --git a/docs/plugins.md b/docs/plugins.md index dd099e62bf..cb6daee07c 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -81,11 +81,17 @@ described below, and RustGIS checks each one: a map file) has it taken out again without `network`. - No plugin may run the user's scripts, tasks or models, create or save models, install or locate other software, manage plugins or connections and logins, replace the project, or - step through undo history. It cannot call another plugin's command directly. + step through undo history. It cannot call another plugin's command or tool. It may run its + own tools through `gp.run`, with the usual `edit` and file requirements. A [process plugin](#process-plugins) is the exception: it is a program on your computer, not sandboxed, and the install prompt says so. +Commands, tool schemas and deferred results stay bound to the exact enabled plugin load that +started them. Disabling, removing or reloading it prevents further stages and result application. +Development process programs remain unsandboxed; this load binding does not attest their +executable bytes or prevent a program from changing on disk. + The install prompt lists these permissions. Each call has a fuel budget, so a plugin stuck in a loop is stopped. Memory is capped at 512 MB, and requests and replies are size-limited. A plugin that traps, panics or returns garbage produces an error message, never a crash. diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index dac8bbda0b..956307d91e 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1238,7 +1238,7 @@ their name. where the command runs. - **Save** keeps the model in the project; **Export** writes it as a `*.model.json` file, which **Import** (or dropping the file on the window) opens again. A model from a file may not read - or write files. **Copy Command** copies a `model.run` command that runs the model from the + or write files, use the network or run other software, including plugin and GeoLibre tools. **Copy Command** copies a `model.run` command that runs the model from the Script pane, the command line or Python. Import checks declared model and web-tool versions before interpreting their contents. Unsupported diff --git a/docs/user-guide/assistant.md b/docs/user-guide/assistant.md index a7f12fb033..7f8fece0ce 100644 --- a/docs/user-guide/assistant.md +++ b/docs/user-guide/assistant.md @@ -21,9 +21,23 @@ than relying only on the model's reply. Commands that would read or write files, an address on the internet, run a plugin, script, task or model, or replace the project wait for you: **Allow** or **Decline**. SQL queries also ask, because SQL can access files and the network. **Allow such actions for this conversation** remembers the exact command and all -its parameters, not general access to a file or server. Plugin commands, user automation and +its parameters, not general access to a file or server. Plugin commands and tools, user automation and project replacement ask every time. +The approval freezes the ordered operations shown, including tool defaults, target layers, +selected layers used implicitly, and the current source and account choices. Changing those, +editing a task or notebook, reloading a plugin, or replacing the project requires a new review. +A remembered operation must match the same contents and targets; at most 64 are retained for +the conversation. Large or unresolved requests refuse before running: split them into smaller +steps or run them yourself. Reviews are bounded to 1,000 operations, four nested items, 64 KiB +of frozen parameters and bindings, and 4 KiB of displayed text. + +Queued tools and downloads keep the initiating caller's authority. Stop, a revoked agent grant, +a disabled plugin or project replacement prevents a new stage or result from being applied. +A network request already sent may still finish; its revoked result is discarded. Tasks, +notebooks and models opened from files, or made or changed by an assistant, agent or plugin, +remain restricted to project edits. Undo restores the provenance of the exact content it restores. + Enter sends, Shift+Enter starts a new line, and Up and Down recall earlier messages when the message box is empty or showing a recalled message. Drag the grip above it to resize it. **Stop** ends the current assistant run; background local-agent threads have their own Stop @@ -250,7 +264,11 @@ fetch the USGS stream gauges in Texas and add them to the map." file it writes) waits for **Allow**, **Always allow** (when the agent offers it) or **Decline** in its window. **Allow** never becomes a persistent approval: if no once-only option exists, use the explicitly labelled **Always allow** or decline. **Approve agents' actions without asking** lets them through - instead. Turn it on only if you trust the agent with your computer. + instead. Turn it on only if you trust the agent with your computer. With it off, RustGIS + sends a task only after confirming an asking or read-only mode; an agent with unknown or + missing modes must first report a compatible mode. Turning the setting off during a turn + that bypasses approval cancels that turn and requests an asking mode where available. + Cancellation cannot recall actions the agent already performed. - **RustGIS checks its own tools.** File, network, plugin-command and project-replacement requests from a local agent wait for a separate **Allow** or **Decline** in its window. Approval applies only to that exact MCP message and that agent's run; a batch is approved or @@ -262,7 +280,7 @@ fetch the USGS stream gauges in Texas and add them to the map." the agent and its inputs. It also selects the agent's own full-access mode where available. - **Its work is kept.** Each task gets a folder in `Documents/RustGIS/Agent work`, and what the agent adds to the map comes from there. The assistant's reply names the folder. -- **On the map, it has its thread's permission only.** It reaches the map through the MCP relay +- **Its relay token carries its thread's permission.** It reaches the map through the MCP relay with a token of its own, which RustGIS revokes when the thread ends. **Stop** in its window, or **×** in the list, revokes its token immediately and terminates its live process tree on a background thread. A reaped leader's old process ID is never signalled. @@ -363,3 +381,10 @@ ACP input uses a bounded background writer queue, so a stalled agent cannot bloc buttons or follow-up submission. Queue overflow reports an error instead of blocking. On Linux and macOS, an exited leader is observed without reaping until cleanup; its reserved identity lets Stop terminate surviving process-group descendants before finally reaping the leader. + +Exact approvals can follow an ordered activation of an existing map: later default layers use +that map's normal selection rules. Layer identities include their owning map, even when two +maps use the same numeric layer ids. A later step after a map creation, duplication, removal or +rename cannot yet be frozen without running the change, so an exact approval refuses that +combined request. Split it into requests, or perform the map change yourself first. Ordinary +native commands and already allowed execution keep their existing behavior. diff --git a/docs/user-guide/web-data.md b/docs/user-guide/web-data.md index 2accba00eb..e755526a37 100644 --- a/docs/user-guide/web-data.md +++ b/docs/user-guide/web-data.md @@ -495,10 +495,12 @@ rustgis-cli run --cmd 'portal.files={"dataset":"giswqs/geospatial"}' rustgis-cli run --sample --cmd 'portal.add={"url":"https://data.geopf.fr/telechargement/download/LiDARHD-NUALID/NUALHD_1-0__LAZ_LAMB93_KE_2025-06-06/LHD_FXX_0652_6864_PTS_LAMB93_IGN69.copc.laz","name":"LiDAR HD 0652-6864"}' ``` -Service discovery forwards only a small set of query options to addresses supplied by the -service: `collections`, `datetime`, `subset`, `subset-crs`, `crs`, `bgcolor` and `transparent`. +When forwarding query options to addresses supplied by a service, RustGIS copies only +`collections`, `datetime`, `subset`, `subset-crs`, `crs`, `bgcolor` and `transparent`. Other settings, including keys, are inherited only by sub-addresses RustGIS constructs within -the original source path. A counted notice says when a listed address did not receive settings. +the original source path. Some discovery paths report withheld settings in a counted notice. +An advertised WMS GetMap address can replace the discovery address without inheriting its query; +this does not produce that notice. If it needs authentication, supply an explicitly authorized address for that source rather than relying on a token being copied from the discovery address. Destination addresses keep settings they already contain. diff --git a/docs/user-guide/workspace.md b/docs/user-guide/workspace.md index bea32fc553..aa20d181a6 100644 --- a/docs/user-guide/workspace.md +++ b/docs/user-guide/workspace.md @@ -32,12 +32,17 @@ The browser app doesn't ask yet; it warns before you leave a page with unsaved c default), RustGIS keeps your changes as a [recovery copy](#autosave-and-recovery) and offers them back the next time it starts; with autosave off, they are lost, so save before you quit. -Supported layer and video address fields, and recognized task, model, notebook and history -parameters, omit recognized passwords, access keys, tokens and signatures. Notices count what was removed or could not -be checked. The open session keeps its credentials, but a saved workflow or protected source may -need them again when reopened. User notes, feature data and opaque content are preserved; see +Saving removes recognized credentials from supported layer and video addresses and from +recognized task, model, notebook and history parameters. Notices count detected removals and +content that could not be checked. Unrecognized credentials can remain in addresses without a +notice. Diagnostic logging uses a broader sensitive-name list: a parameter named `auth` or +`jwt` may be hidden in a diagnostic message while remaining in a saved address. Inspect +shared addresses when a service uses names outside the recognized saved-credential rules. +The open session keeps its credentials, but a saved workflow or protected source may +need them again when reopened. User notes, feature data and opaque content are preserved. +Review addresses and preserved content before sharing a file; see [Sharing projects and generated items](https://github.com/opengeos/RustGIS/blob/main/SECURITY.md#sharing-projects-and-generated-items) -before sharing a file. Review addresses before sharing too: an unrecognized credential can remain unchanged. +for the supported scope. ### Autosave and recovery @@ -45,7 +50,7 @@ The desktop app keeps a recovery copy of unsaved work. While the project has cha saved, it writes a copy every few minutes (5 by default), and only when something changed since the last one. Choose how often, or turn it off, in **Settings › Autosave** (`ui.autosave={"minutes": 10}`; `0` is off). The copy is written in the background, so a large -project doesn't hold up the app. It leaves out layer passwords, keys and tokens, as a saved project +project doesn't hold up the app. It removes recognized layer credentials, as a saved project does. It is kept in the `recovery` folder of the app's [data folder](../installation.md#settings-data-and-cache-folders), not beside your project. Saving, opening or starting another project, and choosing **Don't Save** when you quit remove it. @@ -402,8 +407,8 @@ duplication and nudging have their own [layout shortcuts](layouts.md#layouts). **Delete**. A recorded tool's step is named after the tool. - In the Browser pane, a click on a task opens its steps; **Run All Steps** is in its context menu. - - A task from a file (`.rgistask`, or inside a project or a package) can't read or write files - or run scripts. Its built-in analysis tools may still name their output layers with a plain + - A task from a file (`.rgistask`, or inside a project or a package) can't read or write files, + use the network, run other software or perform actions reserved to the user. Its built-in analysis tools may still name their output layers with a plain name: one with `/`, `\`, `.` or `:` (a folder, an extension or a drive) makes the task count as writing files, so write `Buffer 1500 m` rather than `Buffer 1.5 km`. GeoLibre and plugin tools' outputs are refused.