From a218f0c4435bcabb200570b87a957e35524ff5df Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 4 Nov 2026 10:38:28 -0600 Subject: [PATCH 01/14] fix(security): keep credentials within runtime boundaries Classify supported address fields consistently, validate service forwarding, and report counted removals through rollback-safe notices. Prepare durable output transactionally and bound runtime restoration for saved/widget views. --- SECURITY.md | 12 + apps/rustgis-python/python/rustgis/_embed.py | 14 +- apps/rustgis-python/src/lib.rs | 33 +- apps/rustgis-python/tests/test_widget.py | 43 ++ apps/rustgis-web/src/embed.rs | 5 + apps/rustgis-web/src/web.rs | 11 +- crates/assistant/src/agent.rs | 15 + crates/assistant/src/audit.rs | 136 ++-- crates/assistant/src/tests.rs | 18 + crates/assistant/src/tools.rs | 14 +- crates/data/src/address.rs | 676 ++++++++++++++++++ crates/data/src/diagnostics.rs | 70 +- crates/data/src/lib.rs | 1 + crates/data/src/ogc.rs | 132 ++-- crates/data/src/source/credentials.rs | 98 +-- crates/data/src/source/pgconn.rs | 50 +- crates/engine/src/cmd/gp.rs | 9 +- crates/engine/src/cmd/history.rs | 110 +-- crates/engine/src/cmd/items.rs | 66 +- crates/engine/src/cmd/notebook.rs | 62 +- crates/engine/src/cmd/presentation.rs | 63 +- crates/engine/src/cmd/project.rs | 9 +- crates/engine/src/cmd/service.rs | 40 +- crates/engine/src/cmd/sharing.rs | 39 +- crates/engine/src/cmd/workflows.rs | 21 +- crates/engine/src/durable.rs | 700 +++++++++++++++++++ crates/engine/src/lib.rs | 127 +++- crates/engine/src/parameters.rs | 141 ++++ crates/engine/src/peer.rs | 300 ++++++++ crates/engine/src/project.rs | 366 ++++++---- crates/engine/src/project_log.rs | 28 +- crates/engine/src/roles.rs | 374 ++++++++++ crates/engine/src/sources.rs | 48 +- crates/engine/src/tests.rs | 161 +++-- crates/engine/src/tests/credential_output.rs | 315 +++++++++ crates/map/src/lib.rs | 206 ++++++ crates/map/src/raster.rs | 37 + crates/map/src/slots.rs | 46 ++ crates/map/src/source.rs | 68 ++ crates/map/src/tiles.rs | 58 ++ crates/tiles/src/arcgis_auth.rs | 2 +- crates/tiles/src/cache.rs | 6 +- crates/ui-egui/src/assistantpane.rs | 11 + crates/ui-egui/src/connectiontree.rs | 12 +- crates/ui-egui/src/lib.rs | 102 ++- crates/ui-egui/src/serviceui.rs | 11 +- docs/contributing.md | 8 + docs/mcp.md | 6 +- docs/python.md | 14 +- docs/user-guide/data.md | 18 +- docs/user-guide/web-data.md | 8 + docs/user-guide/workspace.md | 9 +- 52 files changed, 4246 insertions(+), 683 deletions(-) create mode 100644 crates/data/src/address.rs create mode 100644 crates/engine/src/durable.rs create mode 100644 crates/engine/src/parameters.rs create mode 100644 crates/engine/src/peer.rs create mode 100644 crates/engine/src/roles.rs create mode 100644 crates/engine/src/tests/credential_output.rs create mode 100644 crates/map/src/slots.rs diff --git a/SECURITY.md b/SECURITY.md index 78792ab8f2..a371f52077 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -19,6 +19,18 @@ RustGIS is released often and fixes go into the next release; older releases don patches. Check that the problem still happens with the [latest release](https://github.com/opengeos/RustGIS/releases/latest) or `main`. +## Sharing projects and generated items + +Saving removes credentials from supported address fields and recognized executable parameters, +and reports counted removals. Feature data, user notes, foreign-language notebook code and opaque +plugin content can still contain sensitive information. An unchecked-content notice means that +content was preserved, not verified safe. Review it before sharing; saving is not a general +secret scanner. + +For an older file or log that exposed a key, revoke or rotate it with its service. Saving again +cleans only recognized fields and cannot recall shared copies. For a Jupyter notebook, also clear +saved widget state and save again; printed cell output and user data require your own review. + ## 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-python/python/rustgis/_embed.py b/apps/rustgis-python/python/rustgis/_embed.py index 5f79c7af1a..0d15bdd3b5 100644 --- a/apps/rustgis-python/python/rustgis/_embed.py +++ b/apps/rustgis-python/python/rustgis/_embed.py @@ -67,6 +67,7 @@ def _start( # Notices waiting to be read. The app's messages may arrive on another thread (Jupyter's). self._held: List[Tuple[str, bool]] = [] self._held_lock = threading.Lock() + self._sent_keys = 0 if sample: self._session.run("project.sample") if data is not None: @@ -201,7 +202,11 @@ def to_png( def _push(self) -> None: """Sends the session's project to the app, when it changed.""" try: - text = self._session.project_json() + text, keys, notices = self._session._engine._peer_text() + self._hold([(notice, False) for notice in notices]) + if keys > self._sent_keys: + self._hold([(f"{keys} layer address(es) went to the map widget without their keys: those layers stay empty there", False)]) + self._sent_keys = keys except RustGISError as e: self._hold([(f"couldn't show the project in the app: {e}", True)]) return @@ -232,16 +237,15 @@ def _from_app(self, content: Any) -> None: text = content.get("text") if not isinstance(text, str) or text == self._project: return - # Opening it reports "Opened ...": not news here, so it is dropped, and what was - # waiting to be read is kept. + # Peer input is an import: keep its containment notices and invalidate old trust. self._hold(self._session.notices()) try: - self._session.run("project.openText", text=text) + self._session._engine._open_peer_text(text) except RustGISError as e: self._hold([(f"the app's project couldn't be read: {e}", True)]) return finally: - self._session.notices() + self._hold(self._session.notices()) # The app's own text, not the session's re-serialised copy: the app then sees its # project and doesn't open it again. self._project = text diff --git a/apps/rustgis-python/src/lib.rs b/apps/rustgis-python/src/lib.rs index 6488627722..e8600a6d03 100644 --- a/apps/rustgis-python/src/lib.rs +++ b/apps/rustgis-python/src/lib.rs @@ -179,7 +179,38 @@ impl Engine { /// The project as `.rgis` JSON text, as `project.saveAs` writes it (what `project.openText` /// opens). fn project_json(&self, py: Python<'_>) -> PyResult { - let result = self.with_session(py, |session| serde_json::to_string(&session.project).map_err(|e| e.to_string())); + let result = self.with_session(py, |session| { + let (text, report) = session.project.saved_text(None)?; + for notice in report.notices() { + session.notify(notice, false); + } + Ok::<_, String>(text) + }); + self.flush(py); + result.map_err(RustGISError::new_err) + } + + /// Private bridge output: never remembered, and never includes keys in widget state. + fn _peer_text(&self, py: Python<'_>) -> PyResult<(String, u32, Vec)> { + let result = self.with_session(py, |session| { + let (text, report) = session.peer_text(rustgis_engine::Peer::Bridge)?; + let addresses = report.layer_addresses(); + let notices = report.notices().into_iter().skip(usize::from(addresses > 0)).collect(); + Ok::<_, String>((text, addresses, notices)) + }); + self.flush(py); + result.map_err(RustGISError::new_err) + } + + /// Private bridge ingress bypasses the saved-text cache and creates no User authority. + fn _open_peer_text(&self, py: Python<'_>, text: String) -> PyResult<()> { + let result = self.with_session(py, |session| { + let report = session.open_peer_text(rustgis_engine::Peer::Bridge, &text)?; + for notice in report.notices() { + session.notify(notice, false); + } + Ok::<_, String>(()) + }); self.flush(py); result.map_err(RustGISError::new_err) } diff --git a/apps/rustgis-python/tests/test_widget.py b/apps/rustgis-python/tests/test_widget.py index 888ef3e42d..cd41943359 100644 --- a/apps/rustgis-python/tests/test_widget.py +++ b/apps/rustgis-python/tests/test_widget.py @@ -98,3 +98,46 @@ def test_layers_of_a_map_change_the_app(): sites.rename("Places") assert m._project != before and "Places" in m._project assert m.layer_names() == ["Places"] + + +def test_widget_exchange_never_sends_keys_and_checks_the_current_slot(): + """The real receiving method keeps only keys still present on the last pushed slot.""" + secret = "RGSYNTH_widget_20261103" + s = rustgis.Session() + s.add_geojson(POINT, name="Sites") + project = json.loads(s.project_json()) + project["maps"][0]["layers"][0]["dataset"]["source"] = f"https://h.invalid/a?token={secret}" + s.run("project.openText", text=json.dumps(project)) + saved = s.project_json() + assert secret not in saved + other = rustgis.Session() + other.run("project.openText", text=saved) + assert other._engine._peer_text()[1] == 1 + m = rustgis.Map(session=s) + assert secret not in m._project + assert any("without their keys" in notice for notice, _ in m.notices()) + incoming = json.loads(m._project) + incoming["maps"][0]["layers"][0]["name"] = "Styled by app" + from_app(m, json.dumps(incoming)) + assert m.session._engine._peer_text()[1] == 1 + assert not any("lost their keys" in notice for notice, _ in m.notices()) + # Direct session changes do not call the widget's push. Its stale record cannot resurrect + # the removed source when the app sends the old slot again. + m.session.run("layer.remove", layer="Styled by app") + incoming["maps"][0]["layers"][0]["name"] = "Stale app slot" + from_app(m, json.dumps(incoming)) + assert m.session._engine._peer_text()[1] == 0 + + +def test_widget_repeated_table_id_cannot_gain_a_retained_key(): + """The receiving census sees duplicate table ids before project normalization renumbers them.""" + s = rustgis.Session() + project = json.loads(s.project_json()) + project["tables"] = [{"id":7, "name":"Private table", "geometry_type":"None", "fields":[], "features":[], "source":"https://h.invalid/table?token=RGSYNTH_table_20261103"}] + s.run("project.openText", text=json.dumps(project)) + m = rustgis.Map(session=s) + incoming = json.loads(m._project) + incoming["tables"].append(dict(incoming["tables"][0])) + from_app(m, json.dumps(incoming)) + assert s._engine._peer_text()[1] == 0 + assert any("lost their keys" in notice for notice, _ in m.notices()) diff --git a/apps/rustgis-web/src/embed.rs b/apps/rustgis-web/src/embed.rs index dbe30f7c49..4ab16a601b 100644 --- a/apps/rustgis-web/src/embed.rs +++ b/apps/rustgis-web/src/embed.rs @@ -17,6 +17,11 @@ //! | `{"rustgis": "project", "text": }` | the project changed here (an edit, a pan) | //! | `{"rustgis": "result", "id", "ok": true, "value"}` or `{…, "ok": false, "error"}` | a `run`'s outcome | //! +//! Projects exchanged here contain public configuration without credentials. Incoming projects +//! use peer/import semantics: no identical-save restore, item trust or earlier approvals. Parent +//! origin checks identify the peer; they grant no user authority. The existing `ui.*` command +//! policy is separate from this project-exchange contract. +//! //! Only messages from the parent window are taken, and the app answers only the origin that sent //! the first of them. Any page may frame the app, so `run` takes only the interface's commands //! (`ui.*`: panes, the language, the theme); everything else changes the project, which the page diff --git a/apps/rustgis-web/src/web.rs b/apps/rustgis-web/src/web.rs index dd3d530696..0970278444 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -395,7 +395,7 @@ impl Embed { let queued = std::mem::take(&mut *self.queue.borrow_mut()); for value in queued { match parse(&value) { - Some(Msg::Project(text)) => match app.run("project.openText", serde_json::json!({"text": text})) { + Some(Msg::Project(text)) => match app.open_peer_text(&text) { Ok(_) => self.changes.seen(key(&app.session)), Err(e) => app.session.notify(e, true), }, @@ -411,8 +411,13 @@ impl Embed { } } match self.changes.due(key(&app.session), web_time::Instant::now()) { - (true, _) => match serde_json::to_string(&app.session.project) { - Ok(text) => self.answer(&crate::embed::project(&text)), + (true, _) => match app.session.peer_text(rustgis_engine::Peer::Embedder) { + Ok((text, report)) => { + self.answer(&crate::embed::project(&text)); + for notice in report.notices() { + app.session.notify(notice, false); + } + } Err(e) => log::error!("couldn't send the project: {e}"), }, (false, Some(wait)) => ctx.request_repaint_after(wait), diff --git a/crates/assistant/src/agent.rs b/crates/assistant/src/agent.rs index 444a6500d2..2a337d25c9 100644 --- a/crates/assistant/src/agent.rs +++ b/crates/assistant/src/agent.rs @@ -942,6 +942,21 @@ impl Agent { self.drain(s) } + /// Peer replacement invalidates pending work and remembered approvals without deleting the + /// conversation. An outstanding provider reply belongs to the previous project generation. + pub fn project_replaced(&mut self) { + self.stop(); + self.state = State::Idle; + self.always.clear(); + self.last_context.clear(); + self.preview = None; + self.retrying_summary = None; + self.child = None; + self.delegations.clear(); + self.reports.clear(); + self.generation = self.generation.wrapping_add(1); + } + /// Stops the run: a request out is forgotten, calls not yet run are skipped. pub fn stop(&mut self) { if !self.busy() { diff --git a/crates/assistant/src/audit.rs b/crates/assistant/src/audit.rs index 2c2523fc71..b047aafdbc 100644 --- a/crates/assistant/src/audit.rs +++ b/crates/assistant/src/audit.rs @@ -89,7 +89,8 @@ pub fn entries(source: &str, request: &str, reply: Option<&str>, took: Duration, .filter(|m| m.get("method").and_then(Value::as_str) == Some("tools/call") && m.get("id").is_some()) .map(|m| { let params = m.get("params").cloned().unwrap_or(Value::Null); - let args = redact(params.get("arguments").cloned().unwrap_or(Value::Null), 0); + let args = + normalized_args(params.get("name").and_then(Value::as_str).unwrap_or(""), params.get("arguments").unwrap_or(&Value::Null)); let r = replies.iter().find(|r| r.get("id") == m.get("id")); let failed = r.is_none_or(|r| r.get("error").is_some() || r.pointer("/result/isError") == Some(&Value::Bool(true))); let error = failed.then(|| { @@ -138,56 +139,24 @@ pub fn entries(source: &str, request: &str, reply: Option<&str>, took: Duration, .collect() } -/// The words that name a secret in a key's name, whole words and their plurals. -const SECRET_WORDS: &[&str] = &[ - "passphrase", - "jwt", - "privatekey", - "password", - "passwd", - "pwd", - "secret", - "token", - "apikey", - "auth", - "authorization", - "bearer", - "credential", - "signature", - "cookie", - "sas", - "dsn", -]; -/// Words before "key" that make it a secret key (`api_key`, `accessKey`), not a sort or join key. -const KEY_KINDS: &[&str] = &["api", "access", "private", "secret", "account", "sas", "subscription", "client"]; - -/// Whether a key's name names a secret: its words (split at `_`, `-`, `.` and camelCase), one of -/// [`SECRET_WORDS`] or its plural, or "key" after one of [`KEY_KINDS`] (or alone). -fn secret_key(name: &str) -> bool { - let mut words: Vec = Vec::new(); - let mut cur = String::new(); - let mut prev_lower = false; - for c in name.chars() { - if c == '_' || c == '-' || c == '.' || c == ' ' { - words.push(std::mem::take(&mut cur)); - prev_lower = false; - continue; - } - if c.is_ascii_uppercase() && prev_lower { - words.push(std::mem::take(&mut cur)); +/// Arguments as the executor reads them. Only this output copy is redacted; it is never +/// supplied to the executor, the permission planner or approval equality. +fn normalized_args(tool: &str, arguments: &Value) -> Value { + match tool { + "run_command" | "run_tool" => { + let params = match rustgis_engine::params_envelope(arguments.get("params")) { + Ok(params) => rustgis_engine::log_params(¶ms).into_log_value(), + Err(_) => json!("[parameters not JSON; withheld]"), + }; + let key = if tool == "run_command" { "command" } else { "tool" }; + let mut out = serde_json::Map::new(); + out.insert(key.into(), redact(arguments.get(key).cloned().unwrap_or(Value::Null), 0)); + out.insert("params".into(), params); + Value::Object(out) } - prev_lower = c.is_ascii_lowercase() || c.is_ascii_digit(); - cur.push(c.to_ascii_lowercase()); + "run" => json!({"script": redact_script(arguments.get("script").and_then(Value::as_str).unwrap_or(""))}), + _ => redact(arguments.clone(), 0), } - words.push(cur); - let words: Vec<&str> = words.iter().map(String::as_str).filter(|w| !w.is_empty()).collect(); - let is = |w: &str, s: &str| w == s || w.strip_suffix('s') == Some(s); - words.iter().enumerate().any(|(i, w)| { - SECRET_WORDS.iter().any(|s| is(w, s)) - // Run together without a separator (`accesstoken`, `clientsecret`, `dbpassword`). - || ["token", "secret", "password", "passphrase"].iter().any(|s| w.len() > s.len() && (w.ends_with(s) || w.ends_with(&format!("{s}s")))) - || (is(w, "key") && (words.len() == 1 || i.checked_sub(1).and_then(|j| words.get(j)).is_some_and(|p| KEY_KINDS.contains(p)))) - }) } /// A string argument without its secrets: an address (one word with a scheme, or a database @@ -215,7 +184,15 @@ fn redact_string(s: &str) -> String { // Text with an address in it (a query, a description): each address word that carries // credentials (a user and password, or a key or token in its query) loses them; the rest, // and the spacing, stay as they were. - let carries = |w: &str| address(w) && (w.contains('@') || mentions_secret(w) || w.to_ascii_lowercase().contains("key=")); + let carries = |w: &str| { + let q = w.split_once('?').map_or("", |(_, q)| q.split('#').next().unwrap_or(q)); + let names = rustgis_engine::data::address::QueryNames::of(q); + let secret_pair = q.split(['&', ';']).any(|pair| { + let name = pair.split_once('=').map_or(pair, |(k, _)| k); + rustgis_engine::data::address::name_class(name, &names) != rustgis_engine::data::address::NameClass::Plain + }); + address(w) && (w.contains('@') || mentions_secret(w) || secret_pair) + }; if t.split_whitespace().any(carries) { let mut out = String::with_capacity(s.len()); let mut word = String::new(); @@ -239,7 +216,7 @@ fn redact_string(s: &str) -> String { s.to_string() } -/// Whether free text names a secret. Narrower than [`SECRET_WORDS`]: a plain "key" in an error +/// Whether free text names a secret. Narrower than the structured-name list: a plain "key" in an error /// ("primary key") isn't one. fn mentions_secret(text: &str) -> bool { let lower = text.to_ascii_lowercase(); @@ -254,18 +231,16 @@ fn mentions_secret(text: &str) -> bool { fn redact_script(script: &str) -> String { script .lines() - .map(|line| { - let line = line.trim(); - if line.is_empty() || line.starts_with('#') { - return line.to_string(); + .take(rustgis_engine::cmd::workflows::MAX_SCRIPT_LINES) + .map(|line| match rustgis_engine::script_line(line) { + None => line.trim().to_string(), + Some(Ok((id, params))) => { + let id = redact_string(&id); + if params.is_null() { id } else { format!("{id} {}", rustgis_engine::log_params(¶ms).into_log_value()) } } - let (id, rest) = line.split_once(char::is_whitespace).unwrap_or((line, "")); - if rest.trim().is_empty() { - return id.to_string(); - } - match serde_json::from_str::(rest.trim()) { - Ok(params) => format!("{id} {}", redact(params, 0)), - Err(_) => format!("{id} [parameters not JSON; withheld]"), + Some(Err(_)) => { + let id = line.trim().split_once(char::is_whitespace).map_or(line.trim(), |(id, _)| id).trim_end_matches('='); + format!("{} [parameters not JSON; withheld]", redact_string(id)) } }) .collect::>() @@ -283,7 +258,7 @@ fn redact(v: Value, depth: usize) -> Value { Value::Array(a) if depth < 16 => Value::Array(a.into_iter().map(|x| redact(x, depth + 1)).collect()), Value::Object(o) if depth < 16 => Value::Object( o.into_iter() - .map(|(k, x)| match (secret_key(&k), k.as_str(), x) { + .map(|(k, x)| match (rustgis_engine::data::address::log_sensitive(&k), k.as_str(), x) { (true, _, _) => (k, json!("[redacted]")), (false, "script", Value::String(s)) => (k, json!(redact_script(&s))), (false, _, x) => (k, redact(x, depth + 1)), @@ -329,6 +304,37 @@ fn prune(dir: &Path, secs: i64) { mod tests { use super::*; + #[test] + fn audit_logs_the_executed_envelope_and_shared_script_grammar() { + let params = + json!({"url": "https://h.invalid/a?%74oken=RGSYNTH_audit_20261103", "pass%77ord": "RGSYNTH_option_20261103", "layer": "roads"}); + for tool in ["run_command", "run_tool"] { + let object = json!({"command": "layer.addUrl", "tool": "buffer", "params": params}); + let mut string = object.clone(); + string["params"] = json!(params.to_string()); + let logged = normalized_args(tool, &object); + assert_eq!(logged, normalized_args(tool, &string)); + assert_eq!(logged["params"], rustgis_engine::log_params(¶ms).into_log_value()); + assert!(!logged.to_string().contains("RGSYNTH_")); + for bad in [json!("{broken RGSYNTH_bad_20261103"), json!("[1]"), json!(42)] { + let mut bad_args = object.clone(); + bad_args["params"] = bad; + assert_eq!(normalized_args(tool, &bad_args)["params"], "[parameters not JSON; withheld]"); + } + } + assert!(!redact_string("failed https://h.invalid/a?%74oken=RGSYNTH_error_20261103").contains("RGSYNTH_")); + let script = "map.zoomIn= {}\nlayer.addUrl {broken RGSYNTH_bad_20261103\nmap.zoomOut {}"; + let logged = redact_script(script); + assert!(logged.starts_with("map.zoomIn {}\nlayer.addUrl [parameters not JSON; withheld]\nmap.zoomOut {}")); + assert!(!logged.contains("RGSYNTH_")); + let document = + json!({"type": "FeatureCollection", "features": [], "padding": "x".repeat(250), "token": "RGSYNTH_data_20261103"}).to_string(); + let arguments = json!({"command": "layer.addDataText", "params": {"text": document}}); + let logged = normalized_args("run_command", &arguments); + assert!(logged["params"]["text"].as_str().is_some_and(|s| s.starts_with("(a document of "))); + assert_eq!(arguments["params"]["text"], document, "the executed data document stays intact"); + } + /// A line for each tool call: what ran, whether it worked, how long it took; secrets in its /// arguments are not written. #[test] @@ -389,10 +395,10 @@ mod tests { "passphrase", "jwt", ] { - assert!(secret_key(k), "{k}"); + assert!(rustgis_engine::data::address::log_sensitive(k), "{k}"); } for k in ["primary_key", "sort_key", "keys_column", "texas", "name"] { - assert!(!secret_key(k), "{k}"); + assert!(!rustgis_engine::data::address::log_sensitive(k), "{k}"); } // Free text keeps its shape; a keyword connection string loses its password. assert_eq!(redact_string("SELECT * FROM t WHERE url = 'http://x?a=1#b'"), "SELECT * FROM t WHERE url = 'http://x?a=1#b'"); diff --git a/crates/assistant/src/tests.rs b/crates/assistant/src/tests.rs index 17883012e9..7a4c514cf0 100644 --- a/crates/assistant/src/tests.rs +++ b/crates/assistant/src/tests.rs @@ -4300,3 +4300,21 @@ fn geolibre_download_callbacks_require_import_approval() { assert!(tools::gate(&s, &call).is_some(), "{key}"); } } + +/// A peer project invalidates queued work and replies while keeping the readable conversation. +#[test] +fn peer_replacement_cancels_approvals_and_stale_replies() { + let mut s = sample(); + let mut agent = Agent::new(cfg("openai"), &s); + assert!(matches!(agent.ask("export the map", &s), Step::Send(_))); + let pending = + call("peer-old", "run_command", json!({"command":"share.exportMap", "params":{"path":"RGSYNTH_never_written_20261103.png"}})); + assert_eq!(agent.receive(agent.generation(), ok(pending), &mut s), Step::Confirm); + let generation = agent.generation(); + let transcript = agent.transcript.len(); + agent.project_replaced(); + assert!(agent.generation() != generation && !agent.busy() && agent.pending(&s).is_none()); + assert!(agent.transcript.len() >= transcript); + let _ = agent.receive(generation, ok(say("stale")), &mut s); + assert!(!agent.busy() && agent.pending(&s).is_none()); +} diff --git a/crates/assistant/src/tools.rs b/crates/assistant/src/tools.rs index 97e93b57f9..c25b42dffd 100644 --- a/crates/assistant/src/tools.rs +++ b/crates/assistant/src/tools.rs @@ -387,20 +387,10 @@ fn str_arg(a: &Value, k: &str) -> Option { /// The `params` a command or tool runs with: an object, or JSON text of one (some models send /// that). Anything else is an error: running with defaults instead could act on the wrong layer. fn params_arg(a: &Value) -> Result { - match a.get("params") { - None | Some(Value::Null) => Ok(json!({})), - Some(v @ Value::Object(_)) => Ok(v.clone()), - Some(Value::String(t)) if t.trim().is_empty() => Ok(json!({})), - Some(Value::String(t)) => match serde_json::from_str::(t) { - Ok(v @ Value::Object(_)) => Ok(v), - Ok(_) => Err("params must be a JSON object".into()), - Err(e) => Err(format!("params is not valid JSON: {e}")), - }, - Some(_) => Err("params must be a JSON object".into()), - } + rustgis_engine::params_envelope(a.get("params")) } -/// An object parameter, which some models send as JSON text (leniently, for display and approval). +/// An object parameter, which some models send as JSON text (leniently, for display only). fn obj_arg(a: &Value, k: &str) -> Value { match a.get(k) { Some(Value::String(t)) if !t.trim().is_empty() => serde_json::from_str(t).unwrap_or(Value::Null), diff --git a/crates/data/src/address.rs b/crates/data/src/address.rs new file mode 100644 index 0000000000..17a82f52d6 --- /dev/null +++ b/crates/data/src/address.rs @@ -0,0 +1,676 @@ +//! Credential names and public address text, shared by sources, diagnostics and writers. +//! +//! A plain name is not a promise about its value. In particular it does not authorize sending +//! that value to an address advertised by a service. These helpers never inspect dataset text. + +use std::borrow::Cow; + +/// Maximum decoding work for one name; anything still encoded is withheld. +pub const MAX_DECODE_PASSES: usize = 4; +pub const MAX_NAME_BYTES: usize = 16 * 1024; + +/// Classification after bounded decoding, without decoding a pair's value. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum NameClass { + Credential, + /// Not recognized; this alone never authorizes forwarding. + Plain, + /// Too long, or still decodable after the budget. Never forwarded, even under a scope. + Unresolved, +} + +/// Parses only a complete hexadecimal escape supplied by the byte scanner. +fn hex(bytes: &[u8]) -> Option { + bytes.iter().try_fold(0, |n, b| Some(n * 16 + char::from(*b).to_digit(16)?)) +} + +/// One byte-preserving pass. Lossy UTF-8 is applied only after all passes, so percent-encoded +/// bytes and literal Unicode take the same route. Invalid escapes remain literal. +fn decode_pass(bytes: &[u8]) -> Vec { + let mut out = Vec::with_capacity(bytes.len()); + let mut i = 0; + while let Some(&b) = bytes.get(i) { + if b == b'%' + && let Some(n) = bytes.get(i + 1..i + 3).and_then(hex) + { + out.push(n as u8); + i += 3; + } else if b == b'\\' + && bytes.get(i + 1) == Some(&b'u') + && let Some(c) = bytes.get(i + 2..i + 6).and_then(hex).and_then(char::from_u32) + { + out.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes()); + i += 6; + } else { + out.push(if b == b'+' { b' ' } else { b }); + i += 1; + } + } + out +} + +/// Repeated decoding stops at a fixed point; a fifth changing pass is unresolved. +fn decoded_name(raw: &str) -> Option> { + if raw.len() > MAX_NAME_BYTES { + return None; + } + let mut name = raw.as_bytes().to_vec(); + for _ in 0..MAX_DECODE_PASSES { + let next = decode_pass(&name); + if next == name { + return Some(name); + } + name = next; + } + (decode_pass(&name) == name).then_some(name) +} + +/// Canonical comparison spelling; invalid UTF-8 contributes no ASCII name characters. +fn letters(bytes: &[u8]) -> String { + String::from_utf8_lossy(bytes).chars().filter(char::is_ascii_alphanumeric).map(|c| c.to_ascii_lowercase()).collect() +} + +/// ASCII letters and digits after bounded decoding, lower-cased; `None` means Unresolved. +pub fn normalized_name(raw: &str) -> Option { + decoded_name(raw).map(|s| letters(&s)) +} + +/// Context is collected from decoded query names, including legacy semicolon-separated pairs. +#[derive(Default)] +pub struct QueryNames { + sig: bool, + sv: bool, + state: bool, +} + +impl QueryNames { + /// Collects context flags without decoding values or retaining query payloads. + pub fn of(query: &str) -> Self { + let mut names = Self::default(); + for pair in query.split(['&', ';']) { + if let Some(decoded) = decoded_name(pair_name(pair)) { + let k = letters(decoded.split(|b| *b == b'=').next().unwrap_or(&decoded)); + names.sig |= k == "sig"; + names.sv |= k == "sv"; + names.state |= k == "state"; + } + } + names + } +} + +const SAS_KEYS: &[&str] = + &["sv", "se", "sp", "sr", "st", "spr", "sip", "si", "ss", "srt", "skoid", "sktid", "skt", "ske", "sks", "skv", "sdd"]; + +/// Exact and containment rules over canonical names, plus query-wide context. +fn credential(k: &str, query: &QueryNames) -> bool { + matches!(k, "key" | "pwd" | "sig" | "signature" | "sas" | "authorization" | "proxyauthorization" | "cookie" | "setcookie" | "bearer") + // These public refusal and tracing headers share the signing family's prefix. + || (k.starts_with("xamz") && !matches!(k, "xamznrequestid" | "xamznwafaction" | "xamzcfid")) + || k.starts_with("xgoog") + || [ + "password", + "passwd", + "secret", + "token", + "credential", + "apikey", + "accesskey", + "privatekey", + "accountkey", + "sharedaccess", + "connectionstring", + ] + .iter() + .any(|w| k.contains(w)) + || (query.sig && query.sv && SAS_KEYS.contains(&k)) + || (k == "code" && query.state) +} + +/// Classifies a raw name. An encoded equals sign is checked both as a name boundary and as +/// part of the whole name; values after a literal equals sign are never decoded here. +pub fn name_class(raw: &str, query: &QueryNames) -> NameClass { + let Some(decoded) = decoded_name(raw) else { return NameClass::Unresolved }; + let before = decoded.split(|b| *b == b'=').next().unwrap_or(&decoded); + if credential(&letters(&decoded), query) || credential(&letters(before), query) { NameClass::Credential } else { NameClass::Plain } +} + +/// True for recognized credentials and unresolved names; no query context is assumed. +pub fn secret_key(raw: &str) -> bool { + name_class(raw, &QueryNames::default()) != NameClass::Plain +} + +/// Names logs also omit, beyond credential-bearing address options. Logs are output only. +pub fn log_sensitive(raw: &str) -> bool { + if secret_key(raw) { + return true; + } + let Some(decoded) = decoded_name(raw) else { return true }; + let name = String::from_utf8_lossy(&decoded); + let k = letters(&decoded); + if k.contains("body") + || matches!(k.as_str(), "basic" | "authentication" | "wwwauthenticate" | "proxyauthenticate" | "userid" | "username") + { + return true; + } + // The audit's existing word policy, including plurals and camelCase, is shared here with + // diagnostics. It deliberately omits more than replayable state does. + let mut words = Vec::new(); + let mut word = String::new(); + let mut prev_lower = false; + for c in name.chars() { + if (!c.is_ascii_alphanumeric() || (c.is_ascii_uppercase() && prev_lower)) && !word.is_empty() { + words.push(std::mem::take(&mut word)); + } + prev_lower = c.is_ascii_lowercase() || c.is_ascii_digit(); + if c.is_ascii_alphanumeric() { + word.push(c.to_ascii_lowercase()); + } + } + if !word.is_empty() { + words.push(word); + } + words.iter().enumerate().any(|(i, word)| { + let w = word.strip_suffix('s').unwrap_or(word); + matches!( + w, + "passphrase" + | "jwt" + | "privatekey" + | "password" + | "passwd" + | "pwd" + | "secret" + | "token" + | "apikey" + | "auth" + | "authorization" + | "bearer" + | "credential" + | "signature" + | "cookie" + | "sas" + | "dsn" + ) || ["token", "secret", "password", "passphrase"].iter().any(|s| w.ends_with(s)) + || (w == "key" + && (words.len() == 1 + || i.checked_sub(1).and_then(|j| words.get(j)).is_some_and(|p| { + ["api", "access", "private", "secret", "account", "sas", "subscription", "client"].contains(&p.as_str()) + }))) + }) +} + +/// Forwarding options compare their complete decoded spelling, including punctuation. The +/// broader credential normalization must not make an unknown name into an allowlisted one. +fn option_name(raw: &str) -> Option { + decoded_name(raw).map(|s| String::from_utf8_lossy(&s).to_ascii_lowercase()) +} + +/// Only a literal equals sign separates a raw name from its value. +fn pair_name(pair: &str) -> &str { + pair.split_once('=').map_or(pair, |(k, _)| k) +} + +/// A legacy semicolon credential taints the whole ampersand pair. +fn pair_class(pair: &str, names: &QueryNames) -> NameClass { + let mut class = NameClass::Plain; + for sub in pair.split(';') { + match name_class(pair_name(sub), names) { + NameClass::Unresolved => return NameClass::Unresolved, + NameClass::Credential => class = NameClass::Credential, + NameClass::Plain => {} + } + } + class +} + +/// Query text ends at the fragment, which is never interpreted as another query. +fn query(address: &str) -> &str { + address.split('#').next().and_then(|s| s.split_once('?')).map_or("", |(_, q)| q) +} + +/// Removes credential and unresolved pairs; retained pairs keep their original bytes. The +/// caller handles user-info and fragments. A legacy `;token=…` removes its entire `&` pair. +pub fn redact_query(address: &mut String) { + let Some(at) = address.find('?').filter(|at| address.find('#').is_none_or(|end| *at < end)) else { return }; + let q = query(address); + let names = QueryNames::of(q); + if q.split('&').all(|p| !p.is_empty() && pair_class(p, &names) == NameClass::Plain) { + return; + } + let kept = q.split('&').filter(|p| !p.is_empty() && pair_class(p, &names) == NameClass::Plain).collect::>().join("&"); + let fragment = address.find('#').and_then(|i| address.get(i..)).unwrap_or("").to_string(); + address.truncate(at); + if !kept.is_empty() { + address.push('?'); + address.push_str(&kept); + } + address.push_str(&fragment); +} + +/// Public form of one web or S3 address. Local paths and other unrecognized text are untouched. +#[derive(Debug, PartialEq, Eq)] +pub enum Public { + Unchanged, + Changed { + address: String, + removed: u16, + }, + /// A recognized address whose authority cannot safely be delimited; retain only its prefix. + Withheld, +} + +/// Masks supported tile placeholders in a validation copy of the authority. Equal byte lengths +/// preserve user-info offsets; the public address always retains the original template spelling. +/// Switch choices may name host labels, but cannot introduce URL delimiters or credentials. +fn template_authority(rest: &str) -> Cow<'_, str> { + let end = rest.find(['/', '?', '#']).unwrap_or(rest.len()); + let Some(authority) = rest.get(..end) else { return Cow::Borrowed(rest) }; + let mut masked = None; + let mut copied = 0; + let mut scan = 0; + while let Some(open) = authority.get(scan..).and_then(|s| s.find('{')).map(|at| scan + at) { + let Some(close) = authority.get(open + 1..).and_then(|s| s.find('}')).map(|at| open + 1 + at) else { break }; + let token = authority.get(open + 1..close).unwrap_or(""); + let known = matches!( + token, + "z" | "zoom" | "x" | "y" | "-y" | "q" | "quadkey" | "s" | "bbox-epsg-3857" | "width" | "height" | "r" | "ratio" + ) || token.strip_prefix("switch:").is_some_and(|choices| { + choices.split(',').all(|choice| { + let choice = choice.trim(); + !choice.is_empty() && choice.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'-' | b'_')) + }) + }); + if known { + let out = masked.get_or_insert_with(|| String::with_capacity(rest.len())); + out.push_str(authority.get(copied..open).unwrap_or("")); + out.extend(std::iter::repeat_n('x', close + 1 - open)); + copied = close + 1; + } + scan = close + 1; + } + match masked { + Some(mut out) => { + out.push_str(rest.get(copied..).unwrap_or("")); + Cow::Owned(out) + } + None => Cow::Borrowed(rest), + } +} + +/// Removes user-info, fragments and secret query pairs, or withholds an unreadable authority. +pub fn public_web_address(address: &str) -> Public { + let text = address.trim(); + let Some((protocol, _)) = text.split_once(':') else { return Public::Unchanged }; + if !["http", "https", "s3"].iter().any(|p| protocol.eq_ignore_ascii_case(p)) { + return Public::Unchanged; + } + let Some((scheme, rest)) = text.split_once("://") else { return Public::Withheld }; + let at = crate::source::pgconn::userinfo_end(&template_authority(rest)); + let rest = at.and_then(|i| rest.get(i + 1..)).unwrap_or(rest); + let no_fragment = rest.split('#').next().unwrap_or(rest); + let candidate = format!("{scheme}://{no_fragment}"); + if no_fragment.split(['/', '?']).next().is_none_or(str::is_empty) + // Backslashes can change authority/path parsing, but query backslashes are data: + // escaped credential names still need the bounded query decoder below. + || candidate.split('?').next().is_some_and(|head| head.contains('\\')) + || candidate.chars().any(char::is_control) + || url::Url::parse(&format!("{scheme}://{}", template_authority(no_fragment))) + .ok() + .is_none_or(|u| u.host_str().is_none() || !u.username().is_empty() || u.password().is_some()) + { + return Public::Withheld; + } + let names = QueryNames::of(query(&candidate)); + let removed = query(&candidate).split('&').filter(|p| !p.is_empty() && pair_class(p, &names) != NameClass::Plain).count(); + let removed = + u16::try_from(removed).unwrap_or(u16::MAX).saturating_add(u16::from(at.is_some())).saturating_add(u16::from(rest.contains('#'))); + let mut public = candidate; + redact_query(&mut public); + if public == address { Public::Unchanged } else { Public::Changed { address: public, removed } } +} + +/// Cleans address words in generated text and counts changed words. All spacing and all other +/// text stay byte-for-byte. Never apply this to user-authored text or dataset values. +pub fn scrub_addresses(text: &str) -> (Cow<'_, str>, u32) { + let mut changed = 0u32; + let mut out = String::new(); + let mut start = 0; + for word in text.split_inclusive(char::is_whitespace) { + let unspaced = word.trim_end_matches(char::is_whitespace); + // Recognize supported prefixes before parsing: malformed delimiters must not turn a + // recognized address into retained text. The preceding scheme characters establish the + // same word boundary for a bare address, a quoted one and an address inside a wrapper. + let Some((begin, scheme_at)) = unspaced.match_indices(':').find_map(|(at, _)| { + let begin = unspaced + .get(..at)? + .char_indices() + .rev() + .find(|(_, c)| !c.is_ascii_alphanumeric() && !"+.-".contains(*c)) + .map_or(0, |(i, c)| i + c.len_utf8()); + let scheme = unspaced.get(begin..at)?; + ["http", "https", "s3"].iter().any(|s| scheme.eq_ignore_ascii_case(s)).then_some((begin, at)) + }) else { + if changed > 0 { + out.push_str(word); + } + start += word.len(); + continue; + }; + let end = unspaced.trim_end_matches([':', ',', ';', '.', ')', ']', '>', '"', '\'', '»', '›', '”', '’']).len().max(scheme_at + 1); + let address = word.get(begin..end).unwrap_or(""); + let public = public_web_address(address); + if !matches!(public, Public::Unchanged) { + if changed == 0 { + out.push_str(text.get(..start).unwrap_or("")); + } + changed = changed.saturating_add(1); + } + if changed > 0 { + out.push_str(word.get(..begin).unwrap_or("")); + match public { + Public::Unchanged => out.push_str(address), + Public::Changed { address, .. } => out.push_str(&address), + Public::Withheld => { + out.push_str(address.split(':').next().unwrap_or("https")); + out.push(':'); + } + } + out.push_str(word.get(end..).unwrap_or("")); + } + start += word.len(); + } + (if changed == 0 { Cow::Borrowed(text) } else { Cow::Owned(out) }, changed) +} + +/// Mechanical path containment for the exact address fetched. Containment alone grants no +/// authority: advertised destinations still use F3 unless a deployment was explicitly reviewed. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Scope { + scheme: String, + host: String, + port: u16, + segments: Vec, +} + +impl Scope { + /// Reads authority with the fetch parser, and path segments from the original bytes. + pub fn of(address: &str) -> Option { + if address.trim() != address || address.chars().any(char::is_control) || address.contains('\\') { + return None; + } + let parsed = url::Url::parse(address).ok()?; + if !matches!(parsed.scheme(), "http" | "https") || !parsed.username().is_empty() || parsed.password().is_some() { + return None; + } + let (_, rest) = address.split_once("://")?; + let end = rest.find(['/', '?', '#']).unwrap_or(rest.len()); + if end == 0 || rest.get(..end)?.contains('@') { + return None; + } + let path = rest.get(end..)?.split(['?', '#']).next()?; + let path = path.strip_prefix('/').unwrap_or(path); + let path = path.strip_suffix('/').unwrap_or(path); + let mut segments = Vec::new(); + if !path.is_empty() { + for segment in path.split('/') { + let lower = segment.to_ascii_lowercase(); + if segment.is_empty() || matches!(segment, "." | "..") || ["%2e", "%2f", "%5c", "%25"].iter().any(|s| lower.contains(s)) { + return None; + } + segments.push(segment.to_string()); + } + } else if rest.get(end..)?.starts_with("//") { + return None; + } + Some(Self { + scheme: parsed.scheme().to_string(), + host: parsed.host_str()?.trim_end_matches('.').to_ascii_lowercase(), + port: parsed.port_or_known_default()?, + segments, + }) + } + + /// Whole raw segments must match; ambiguous paths and user-info are outside every scope. + pub fn covers_below(&self, to: &str) -> bool { + Self::of(to).is_some_and(|other| { + self.scheme == other.scheme && self.host == other.host && self.port == other.port && other.segments.starts_with(&self.segments) + }) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Relation { + Constructed, + Advertised, +} + +pub const SAFE_OPTIONS: &[&str] = &["collections", "datetime", "subset", "subset-crs", "crs", "bgcolor", "transparent"]; + +/// Only a reviewed deployment may authorize full-query inheritance for advertised links. Root +/// entries require separate whole-origin agreement, including every tenant on that origin. +pub struct ReviewedScope { + pub directory: &'static str, + pub whole_origin: bool, +} +pub const F2_SCOPES: &[ReviewedScope] = &[]; + +#[derive(Debug, PartialEq, Eq)] +pub struct Inherited { + pub url: String, + pub withheld: bool, +} + +/// Adds missing pairs, preserving their bytes. Unresolved pairs never follow; F3 sends only +/// SAFE_OPTIONS to advertised addresses. Only checked constructed sub-addresses get other pairs. +pub fn inherit_query(from: &str, to: &str, relation: Relation, skip: &[&str]) -> Inherited { + inherit_reviewed(from, to, relation, skip, F2_SCOPES) +} + +/// Applies the fixed policy with an explicit reviewed list; production passes the empty list. +fn inherit_reviewed(from: &str, to: &str, relation: Relation, skip: &[&str], reviewed: &[ReviewedScope]) -> Inherited { + let full = match relation { + Relation::Constructed => Scope::of(from).is_some_and(|s| s.covers_below(to)), + Relation::Advertised => reviewed.iter().any(|r| { + Scope::of(r.directory).is_some_and(|s| (!s.segments.is_empty() || r.whole_origin) && s.covers_below(from) && s.covers_below(to)) + }), + }; + let q = query(from); + let names = QueryNames::of(q); + let mut out = Inherited { url: to.to_string(), withheld: false }; + let mut present: Vec = query(to).split(['&', ';']).filter_map(|p| option_name(pair_name(p))).collect(); + for pair in q.split('&').filter(|p| !p.is_empty()) { + let class = pair_class(pair, &names); + if class == NameClass::Unresolved { + out.withheld = true; + continue; + } + let keys: Vec = pair.split(';').filter_map(|p| option_name(pair_name(p))).collect(); + let skipped = |k: &String| skip.iter().any(|s| option_name(s).as_ref() == Some(k)); + if keys.iter().any(skipped) { + out.withheld |= keys.iter().any(|k| !skipped(k)); + continue; + } + if keys.iter().any(|k| present.contains(k)) { + // Never overwrite a destination's pair, including a legacy sub-pair. If only some + // names overlap, the rest of this indivisible raw pair were withheld too. + out.withheld |= keys.iter().any(|k| !present.contains(k)); + continue; + } + let safe = class == NameClass::Plain && keys.iter().all(|k| SAFE_OPTIONS.contains(&k.as_str())); + if pair.contains(['{', '}']) || !(full || safe) { + out.withheld = true; + continue; + } + let fragment = out.url.find('#').unwrap_or(out.url.len()); + let separator = if out.url.get(..fragment).is_some_and(|u| u.contains('?')) { '&' } else { '?' }; + out.url.insert(fragment, separator); + out.url.insert_str(fragment + 1, pair); + present.extend(keys); + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn canonical_names_remove_encoded_and_unresolved_pairs_without_touching_values() { + let over = "a".repeat(MAX_NAME_BYTES + 1); + for key in [ + "%74oken", + "%2574oken", + "to%6Ben", + "pass%77ord", + "+token", + "token%3DM", + "%FFtoken", + "ok=1;token", + "X-Amz-%53ignature", + "X-Amz-%53ecurity-Token", + "X-Amz-Credential", + "X-Amzn-RequestId-Token", + "X-Amz-Cf-Id-Secret", + "sig=M&%73v", + "\\u0074oken", + "accessKeyId", + "%2525252574oken", + over.as_str(), + ] { + let address = format!("https://h.invalid/a?{key}=RGSYNTH_name_20261103&layer=a%20b&f=json&%zz=1"); + let Public::Changed { address: public, removed, .. } = public_web_address(&address) else { panic!("{key}") }; + assert!(!public.contains("RGSYNTH_name_20261103"), "{key}: {public}"); + assert!(public.ends_with("layer=a%20b&f=json&%zz=1"), "{public}"); + assert!(removed > 0); + } + for query in ["layer=a%20b&f=json&%zz=1", "layer=token%3DM", "code=EPSG:4326", "sv=2026&layer=1", r"layer=a\b&f=json"] { + let address = format!("https://h.invalid/a?{query}"); + assert_eq!(public_web_address(&address), Public::Unchanged); + } + for name in ["X-Amzn-RequestId", "X-Amzn-Waf-Action", "X-Amz-Cf-Id", "X-%2541mzn-RequestId"] { + assert_eq!(name_class(name, &QueryNames::default()), NameClass::Plain, "{name}"); + assert!(!log_sensitive(name), "{name}"); + } + for address in [r"https://h.invalid\a?token=RGSYNTH_boundary", r"https://h.invalid/a\b?token=RGSYNTH_boundary"] { + assert_eq!(public_web_address(address), Public::Withheld); + } + for template in ["https://{switch:a,b}.host/{z}/{x}/{y}@2x.png?map=m", "https://{s}.host/{zoom}/{x}/{y}@2x.png"] { + assert_eq!(public_web_address(template), Public::Unchanged); + } + assert_eq!( + public_web_address( + "https://u:RGSYNTH_template_20261104@{switch:a,b}.host/{z}/{x}/{y}@2x.png?token=RGSYNTH_query_20261104&map=m#fragment" + ), + Public::Changed { address: "https://{switch:a,b}.host/{z}/{x}/{y}@2x.png?map=m".into(), removed: 3 } + ); + assert_eq!(name_class("%2525252574oken", &QueryNames::default()), NameClass::Unresolved); + assert_eq!(name_class(&over, &QueryNames::default()), NameClass::Unresolved); + let mut callback = "https://h.invalid/cb?code=RGSYNTH_code_20261103&%73tate=flow".to_string(); + redact_query(&mut callback); + assert_eq!(callback, "https://h.invalid/cb?%73tate=flow"); + let (clean, count) = scrub_addresses( + "failed https://u:RGSYNTH_user_20261103@h.invalid/a?%74oken=RGSYNTH_query_20261103 then https://h.invalid/b?layer=a%20b\n", + ); + assert_eq!(clean, "failed https://h.invalid/a then https://h.invalid/b?layer=a%20b\n"); + assert_eq!(count, 1); + assert_eq!(scrub_addresses("(https://h.invalid/a?token=RGSYNTH_punctuation_20261103), done").0, "(https://h.invalid/a), done"); + for (text, public, count) in [ + ("read https:/h.invalid/x?token=RGSYNTH_0", "read https:", 1), + ("\t(HTTP:/h.invalid/x?token=RGSYNTH_0), done\n", "\t(HTTP:), done\n", 1), + ("[s3:bucket/x?token=RGSYNTH_0]", "[s3:]", 1), + ("https:x", "https:", 1), + ("s3:", "s3:", 1), + ("read https:/h.invalid/x then https://h.invalid/b?layer=a%20b", "read https: then https://h.invalid/b?layer=a%20b", 1), + ( + "http://h.invalid/a https://h.invalid/b?layer=a%20b s3://bucket/x", + "http://h.invalid/a https://h.invalid/b?layer=a%20b s3://bucket/x", + 0, + ), + ("myhttps:/h.invalid/x?token=RGSYNTH_unrelated_20261104", "myhttps:/h.invalid/x?token=RGSYNTH_unrelated_20261104", 0), + ("dataset token=RGSYNTH_data_20261104 key password", "dataset token=RGSYNTH_data_20261104 key password", 0), + ] { + let (scrubbed, changed) = scrub_addresses(text); + assert_eq!((scrubbed.as_ref(), changed), (public, count), "{text}"); + if count == 0 { + assert!(matches!(scrubbed, Cow::Borrowed(_))); + } + } + assert!(matches!(scrub_addresses("dataset token key password").0, Cow::Borrowed(_))); + assert_eq!(public_web_address("/data/token?key=password"), Public::Unchanged); + assert_eq!(public_web_address("https:///path"), Public::Withheld); + assert_eq!(public_web_address("https://u:RGSYNTH_malformed_20261103/path"), Public::Withheld); + assert_eq!( + public_web_address("https://u:RGSYNTH_malformed_20261103/part@h.invalid/a"), + Public::Changed { address: "https://h.invalid/a".into(), removed: 1 } + ); + } + + #[test] + fn inherited_queries_require_the_reviewed_relationship_and_unambiguous_path() { + let source = "https://h.invalid/svc?token=RGSYNTH_inherit_20261103&k=RGSYNTH_unknown_20261103&collections=roads&f=json"; + let target = "https://h.invalid/svc/tiles/{z}/{y}/{x}"; + let kept = inherit_query(source, target, Relation::Constructed, &["f"]); + assert!(!kept.withheld); + assert!(kept.url.ends_with("?token=RGSYNTH_inherit_20261103&k=RGSYNTH_unknown_20261103&collections=roads")); + for destination in [ + target, + "https://other.invalid/svc/tiles", + "http://h.invalid/svc/tiles", + "https://h.invalid:444/svc/tiles", + "https://u@h.invalid/svc/tiles", + "https://h.invalid/svc-other/tiles", + ] { + let f3 = inherit_query(source, destination, Relation::Advertised, &["f"]); + assert_eq!(f3.url, format!("{destination}?collections=roads")); + assert!(f3.withheld); + if destination != target { + let constructed = inherit_query(source, destination, Relation::Constructed, &["f"]); + assert_eq!(constructed, f3); + } + } + for suffix in + ["../tiles", "./tiles", "%2e%2e/tiles", "%2Ftiles", "%5Ctiles", "\\tiles", "%252e/tiles", "/tiles", "tiles//x", "tiles/\nnext"] + { + let to = format!("https://h.invalid/svc/{suffix}"); + let inherited = inherit_query(source, &to, Relation::Constructed, &["f"]); + assert!(inherited.withheld && !inherited.url.contains("RGSYNTH_"), "{to}"); + } + for name in ["%2525252574oken".to_string(), "a".repeat(MAX_NAME_BYTES + 1)] { + let source = format!("https://h.invalid/svc?{name}=RGSYNTH_unresolved_20261103"); + for relation in [Relation::Constructed, Relation::Advertised] { + assert_eq!(inherit_query(&source, target, relation, &[]), Inherited { url: target.into(), withheld: true }); + } + } + let root = "https://h.invalid/?token=RGSYNTH_root_20261103&collections=x"; + assert_eq!(inherit_query(root, target, Relation::Advertised, &[]).url, format!("{target}?collections=x")); + let reviewed = [ReviewedScope { directory: "https://h.invalid/svc/", whole_origin: false }]; + assert_eq!(inherit_reviewed(source, target, Relation::Advertised, &["f"], &reviewed), kept); + assert!(inherit_reviewed(source, "https://h.invalid/svc-other/a", Relation::Advertised, &["f"], &reviewed).withheld); + let unreviewed_root = [ReviewedScope { directory: "https://h.invalid/", whole_origin: false }]; + assert!(inherit_reviewed(source, target, Relation::Advertised, &["f"], &unreviewed_root).withheld); + assert!(F2_SCOPES.is_empty()); + let to = format!("{target}?%74oken=destination-owned#look"); + let own = inherit_query(source, &to, Relation::Constructed, &["f"]); + assert!(own.url.contains("%74oken=destination-owned") && !own.url.contains("RGSYNTH_inherit")); + assert!(own.url.ends_with("#look")); + assert!(!inherit_query("https://h.invalid/svc?collections=x", target, Relation::Advertised, &[]).withheld); + for key in ["c_r_s", "%FFcollections", "+collections", "f_"] { + let source = format!("https://h.invalid/svc?{key}=RGSYNTH_alias_20261103"); + let inherited = inherit_query(&source, target, Relation::Advertised, &["f"]); + assert_eq!(inherited, Inherited { url: target.into(), withheld: true }); + } + assert_eq!( + inherit_query("https://h.invalid/svc?%63ollections=x&subset-crs=y", target, Relation::Advertised, &[]).url, + format!("{target}?%63ollections=x&subset-crs=y") + ); + let duplicate = inherit_query( + "https://h.invalid/svc?token=source;collections=x", + &format!("{target}?token=destination"), + Relation::Constructed, + &[], + ); + assert_eq!(duplicate.url, format!("{target}?token=destination")); + assert!(duplicate.withheld); + } +} diff --git a/crates/data/src/diagnostics.rs b/crates/data/src/diagnostics.rs index f75553520b..b383e88b7e 100644 --- a/crates/data/src/diagnostics.rs +++ b/crates/data/src/diagnostics.rs @@ -491,73 +491,12 @@ fn normalized_key(key: &str) -> impl Iterator + '_ { /// Whether `key` is `target` (lower case, letters and digits) however it is written. fn key_is(key: &str, target: &str) -> bool { - normalized_key(key).eq(target.chars()) -} - -/// A name with its percent and `\u` escapes decoded, to the depth an attacker could nest them. -fn decoded_name(key: &str) -> String { - let mut name = key.to_string(); - for _ in 0..4 { - let bytes = name.as_bytes(); - let mut decoded = String::with_capacity(name.len()); - let mut i = 0; - while let Some(&b) = bytes.get(i) { - if b == b'%' - && let Some(hex) = name.get(i + 1..i + 3) - && let Ok(value) = u8::from_str_radix(hex, 16) - { - decoded.push(char::from(value)); - i += 3; - } else if b == b'\\' - && bytes.get(i + 1) == Some(&b'u') - && let Some(hex) = name.get(i + 2..i + 6) - && let Ok(value) = u32::from_str_radix(hex, 16) - && let Some(c) = char::from_u32(value) - { - decoded.push(c); - i += 6; - } else if let Some(c) = name.get(i..).and_then(|s| s.chars().next()) { - decoded.push(c); - i += c.len_utf8(); - } else { - break; - } - } - if decoded == name { - break; - } - name = decoded; - } - name + crate::address::normalized_name(key).is_some_and(|name| name == target) } +/// Shared bounded name decoding; diagnostics additionally omit log-only fields. fn secret_name(key: &str) -> bool { - // Decode only names, never values: decoding a URL value could introduce whitespace and - // incorrectly end its redacted query. Work and allocation are capped by MAX_INPUT_BYTES, and - // a name with nothing to decode is not copied. - let decoded; - let name = if key.contains(['%', '\\']) { - decoded = decoded_name(key); - decoded.as_str() - } else { - key - }; - let normalized: String = normalized_key(name).collect(); - crate::source::credentials::secret_key(name) - || crate::source::credentials::secret_key(&normalized) - || normalized.contains("credential") - || normalized.contains("connectionstring") - || normalized.contains("privatekey") - || normalized.contains("accountkey") - // A prefixed name is still one: MAPBOX_API_KEY, X_API_KEY, AWS_ACCESS_KEY_ID. - || normalized.contains("apikey") - || normalized.contains("accesskey") - || normalized.contains("sharedaccess") - || normalized.contains("body") - || matches!( - normalized.as_str(), - "basic" | "bearer" | "authentication" | "wwwauthenticate" | "proxyauthenticate" | "userid" | "username" - ) + crate::address::log_sensitive(key) } /// Whether the name is an HTTP credential header: its value is always a scheme and a credential @@ -1246,6 +1185,9 @@ mod tests { assert_eq!(redact("server: B.earer hidden"), "server: [REDACTED]"); assert_eq!(redact("server: Ba-sic dXNlcjpwYXNz"), "server: [REDACTED]"); assert_eq!(redact("pass%77ord=x"), "[REDACTED]"); + for key in ["%2574oken", "%2525252574oken"] { + assert!(!redact(&format!("{key}=RGSYNTH_diagnostic_20261103")).contains("RGSYNTH_")); + } } #[test] diff --git a/crates/data/src/lib.rs b/crates/data/src/lib.rs index cf7e0e9adb..7577644cc6 100644 --- a/crates/data/src/lib.rs +++ b/crates/data/src/lib.rs @@ -4,6 +4,7 @@ //! bad features are skipped and counted in [`ReadReport`]. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +pub mod address; pub mod cityjson; pub mod color; mod dbf; diff --git a/crates/data/src/ogc.rs b/crates/data/src/ogc.rs index ae6147b401..540a8579d4 100644 --- a/crates/data/src/ogc.rs +++ b/crates/data/src/ogc.rs @@ -442,6 +442,8 @@ pub struct Service { /// Some advertised collections were left out (past the listing limit, or with an items /// address RustGIS doesn't fetch): the listing isn't the whole service. pub incomplete: bool, + /// Destinations that did not inherit one or more query pairs; reported by the caller. + pub withheld_addresses: u32, } /// Most layers listed from one service. @@ -580,7 +582,8 @@ fn query_value<'a>(url: &'a str, key: &str) -> Option<&'a str> { let q = url.split('#').next()?.split_once('?')?.1; q.split('&').find_map(|kv| { let (k, v) = kv.split_once('=').unwrap_or((kv, "")); - k.eq_ignore_ascii_case(key).then_some(v) + (if key == "token" { crate::address::normalized_name(k).as_deref() == Some("token") } else { k.eq_ignore_ascii_case(key) }) + .then_some(v) }) } @@ -2205,7 +2208,8 @@ fn arcgis(url: &str, v: &serde_json::Value) -> Result { max_paged_features() )); } - // A secured service's token goes with every request. + // Offers are constructed under the service. A suffix supplied by its document still has + // to pass the raw-path check before it may receive the service's token. if let Some(token) = query_value(url, "token").filter(|t| !t.is_empty()) { for l in &mut service.layers { match &mut l.offer { @@ -2214,7 +2218,13 @@ fn arcgis(url: &str, v: &serde_json::Value) -> Result { | Offer::Style { url: u } | Offer::VectorTiles { template: u, .. } | Offer::Service { url: u } - | Offer::Coverage(Coverage { endpoint: u, .. }) => *u = with_query(u, &format!("token={token}")), + | Offer::Coverage(Coverage { endpoint: u, .. }) => { + if crate::address::Scope::of(url).is_some_and(|scope| scope.covers_below(u)) { + *u = with_query(u, &format!("token={token}")); + } else { + service.withheld_addresses = service.withheld_addresses.saturating_add(1); + } + } } } } @@ -2328,7 +2338,8 @@ fn ogc_api(url: &str, v: &serde_json::Value) -> Result { service.collections_url = collections_link(url, &links); let name = if title.is_empty() { "tiles".to_string() } else { title }; // The dataset's layers aren't listed here, so its vector tiles can't be styled: map tiles only. - if let Some((t, list)) = tileset(url, &links, TileRel::Map) { + if let Some((t, list, withheld)) = tileset(url, &links, TileRel::Map) { + service.withheld_addresses = service.withheld_addresses.saturating_add(u32::from(withheld)); service.layers.push(ServiceLayer { zooms: None, name: "map".into(), @@ -2382,7 +2393,8 @@ fn ogc_api(url: &str, v: &serde_json::Value) -> Result { // tiles' layer is named after the collection, as the standard has it. let map = tileset(url, &links, TileRel::Map); let vector = tileset(url, &links, TileRel::Vector); - if let Some((t, list)) = &map { + if let Some((t, list, withheld)) = &map { + service.withheld_addresses = service.withheld_addresses.saturating_add(u32::from(*withheld)); service.layers.push(ServiceLayer { zooms: None, name: format!("{id} (map tiles)"), @@ -2395,7 +2407,8 @@ fn ogc_api(url: &str, v: &serde_json::Value) -> Result { legends: Vec::new(), }); } - if let Some((t, list)) = &vector { + if let Some((t, list, withheld)) = &vector { + service.withheld_addresses = service.withheld_addresses.saturating_add(u32::from(*withheld)); service.layers.push(ServiceLayer { zooms: None, name: format!("{id} (vector tiles)"), @@ -2508,9 +2521,9 @@ enum TileRel { /// `tilesets-vector` link: the template at the usual path under the tileset list /// (`…/WebMercatorQuad/{tileMatrix}/{tileRow}/{tileCol}`), and the list's address (JSON), whose /// WebMercatorQuad tileset advertises the template [`resolve_tiles`] reads (the usual path is what -/// is used when that can't be read). Both keep the link's query (a token, a collections choice), -/// all but `f`. -fn tileset(from: &str, links: &[serde_json::Value], which: TileRel) -> Option<(String, String)> { +/// is used when that can't be read). The constructed template keeps the link's query except +/// `f` only under its checked source path; the third result counts a withheld destination. +fn tileset(from: &str, links: &[serde_json::Value], which: TileRel) -> Option<(String, String, bool)> { let suffix = match which { TileRel::Map => "tilesets-map", TileRel::Vector => "tilesets-vector", @@ -2534,23 +2547,12 @@ fn tileset(from: &str, links: &[serde_json::Value], which: TileRel) -> Option<(S TileRel::Vector => format!("{at}/WebMercatorQuad/{{z}}/{{y}}/{{x}}?f=mvt"), }; let list = if query_value(&href, "f").is_some() { href.clone() } else { with_query(&href, "f=json") }; - Some((carry_query(&template, &href), list)) + let inherited = crate::address::inherit_query(&href, &template, crate::address::Relation::Constructed, &["f"]); + Some((inherited.url, list, inherited.withheld)) } -/// `template` with the query parameters of `link` it doesn't have, but `f` (the representation -/// the link was for). -fn carry_query(template: &str, link: &str) -> String { - let q = link.split('#').next().and_then(|u| u.split_once('?')).map(|(_, q)| q).unwrap_or(""); - let mut out = template.to_string(); - for kv in q.split('&').take(32) { - let k = kv.split_once('=').map(|(k, _)| k).unwrap_or(kv); - if kv.is_empty() || k.eq_ignore_ascii_case("f") || query_value(&out, k).is_some() || kv.contains(['{', '}']) { - continue; - } - out = with_query(&out, kv); - } - out -} +/// Notice template shared by native discovery and the translated asynchronous UI resolver. +pub const QUERY_WITHHELD_NOTICE: &str = "{n} address(es) the service listed didn't get its keys or other settings: RustGIS sends those only to addresses it builds itself from the one given; add such an address with its own key if it needs one"; /// What an OGC API tiles document says on the way to a tile template: the template, or the /// address of the document to read next (a tileset list's WebMercatorQuad tileset). @@ -2560,15 +2562,36 @@ pub enum TilesStep { Next(String), } +/// One discovery result and the number of destinations that lost query settings. Callers must +/// report this count, including when a later fetch fails and falls back to a constructed URL. +#[derive(Clone, Debug, PartialEq)] +pub struct TilesResolution { + pub step: TilesStep, + pub withheld_addresses: u32, +} + +/// Applies the advertised-link policy; the document cannot claim a constructed relationship. +fn advertised_tiles(list: &str, to: &str, next: bool) -> TilesResolution { + let inherited = crate::address::inherit_query(list, to, crate::address::Relation::Advertised, &["f"]); + let url = inherited.url; + let step = if next { + TilesStep::Next(if query_value(&url, "f").is_some() { url } else { with_query(&url, "f=json") }) + } else { + TilesStep::Template(url) + }; + TilesResolution { step, withheld_addresses: u32::from(inherited.withheld) } +} + /// One step of reading an OGC API tile template: `body` is the tileset list (or a tileset) -/// fetched from `doc`; `list` is the list's address, whose query the template keeps. `None` when +/// fetched from `doc`; `list` is the query source. Advertised links receive SAFE_OPTIONS only; +/// withheld settings are counted for the caller's notice. `None` when /// the document names no WebMercatorQuad tileset or tile template. -pub fn resolve_tiles(list: &str, doc: &str, body: &[u8], vector: bool) -> Option { +pub fn resolve_tiles(list: &str, doc: &str, body: &[u8], vector: bool) -> Option { let v: serde_json::Value = serde_json::from_slice(body).ok()?; let links = |v: &serde_json::Value| v.get("links").and_then(|l| l.as_array()).cloned().unwrap_or_default(); let Some(sets) = v.get("tilesets").and_then(|t| t.as_array()) else { // A tileset: its tile template. - return item_template(doc, &links(&v), vector).map(|t| TilesStep::Template(carry_query(&t, list))); + return item_template(doc, &links(&v), vector).map(|t| advertised_tiles(list, &t, false)); }; let mercator = |t: &serde_json::Value| { let tiling = links(t); @@ -2592,14 +2615,13 @@ pub fn resolve_tiles(list: &str, doc: &str, body: &[u8], vector: bool) -> Option let t = sets.iter().take(200).find(|t| mercator(t) && kind_fits(t))?; let ls = links(t); if let Some(template) = item_template(doc, &ls, vector) { - return Some(TilesStep::Template(carry_query(&template, list))); + return Some(advertised_tiles(list, &template, false)); } let next = ls .iter() .find(|l| l.get("rel").and_then(|r| r.as_str()).is_some_and(|r| r == "self" || r.ends_with("/tileset"))) .and_then(|l| resolve(doc, l.get("href")?.as_str()?))?; - let next = carry_query(&next, list); - Some(TilesStep::Next(if query_value(&next, "f").is_some() { next } else { with_query(&next, "f=json") })) + Some(advertised_tiles(list, &next, true)) } /// A tileset's advertised tile template (its `item` link, `{tileMatrix}/{tileRow}/{tileCol}`) as @@ -6000,35 +6022,65 @@ mod tests { {"tileMatrixSetURI": "http://www.opengis.net/def/tilematrixset/OGC/1.0/WorldCRS84Quad", "links": [{"rel": "self", "href": "crs84"}]}, {"dataType": "vector", "tileMatrixSetURI": "http://www.opengis.net/def/tilematrixset/OGC/1.0/WebMercatorQuad", "links": [{"rel": "self", "href": "https://x.org/c/roads/tiles/smerc"}]}]}"#; assert_eq!( - resolve_tiles(list, list, sets, true), - Some(TilesStep::Next("https://x.org/c/roads/tiles/smerc?token=abc&f=json".into())) + resolve_tiles(list, list, sets, true).map(|r| r.step), + Some(TilesStep::Next("https://x.org/c/roads/tiles/smerc?f=json".into())) ); - assert_eq!(resolve_tiles(list, list, sets, false), None, "no map tileset in WebMercatorQuad"); - // The tileset's item links: MVT for vector tiles, PNG first for map tiles, the token kept. + assert_eq!(resolve_tiles(list, list, sets, false).map(|r| r.step), None, "no map tileset in WebMercatorQuad"); + // The tileset's item links: MVT for vector tiles, PNG first for map tiles, the list's token withheld under F3. let tileset = br#"{"links": [ {"rel": "item", "type": "image/jpeg", "templated": true, "href": "https://x.org/c/roads/tiles/smerc/{tileMatrix}/{tileRow}/{tileCol}.jpg"}, {"rel": "item", "type": "image/png", "templated": true, "href": "https://x.org/c/roads/tiles/smerc/{tileMatrix}/{tileRow}/{tileCol}.png"}, {"rel": "item", "type": "application/vnd.mapbox-vector-tile", "templated": true, "href": "smerc/{tileMatrix}/{tileRow}/{tileCol}.mvt"}]}"#; let doc = "https://x.org/c/roads/tiles/smerc?token=abc&f=json"; assert_eq!( - resolve_tiles(list, doc, tileset, true), - Some(TilesStep::Template("https://x.org/c/roads/tiles/smerc/{z}/{y}/{x}.mvt?token=abc".into())) + resolve_tiles(list, doc, tileset, true).map(|r| r.step), + Some(TilesStep::Template("https://x.org/c/roads/tiles/smerc/{z}/{y}/{x}.mvt".into())) ); assert_eq!( - resolve_tiles(list, doc, tileset, false), - Some(TilesStep::Template("https://x.org/c/roads/tiles/smerc/{z}/{y}/{x}.png?token=abc".into())) + resolve_tiles(list, doc, tileset, false).map(|r| r.step), + Some(TilesStep::Template("https://x.org/c/roads/tiles/smerc/{z}/{y}/{x}.png".into())) ); // A list that carries its tilesets' item links needs no second document. let inline = br#"{"tilesets": [{"tileMatrixSetId": "WebMercatorQuad", "links": [ {"rel": "item", "type": "image/png", "href": "https://x.org/t/{tileMatrix}/{tileRow}/{tileCol}"}]}]}"#; - assert_eq!(resolve_tiles(list, list, inline, false), Some(TilesStep::Template("https://x.org/t/{z}/{y}/{x}?token=abc".into()))); + assert_eq!( + resolve_tiles(list, list, inline, false).map(|r| r.step), + Some(TilesStep::Template("https://x.org/t/{z}/{y}/{x}".into())) + ); // A template with other variables, or no web address, isn't taken. for bad in [ br#"{"links": [{"rel": "item", "href": "https://x.org/{style}/{tileMatrix}/{tileRow}/{tileCol}"}]}"#.as_slice(), br#"{"links": [{"rel": "item", "href": "javascript:{tileMatrix}"}]}"#.as_slice(), b"not json", ] { - assert_eq!(resolve_tiles(list, doc, bad, false), None); + assert_eq!(resolve_tiles(list, doc, bad, false).map(|r| r.step), None); + } + // F3 is independent of origin and directory containment. Destination-owned keys stay; + // only the allowlisted collection choice may follow from the list. + let list = "https://h.invalid/svc?token=RGSYNTH_list_20261103&k=RGSYNTH_unknown_20261103&collections=roads"; + for host in ["h.invalid", "elsewhere.invalid"] { + let doc = format!( + r#"{{"links":[{{"rel":"item","type":"image/png","href":"https://{host}/svc/{{tileMatrix}}/{{tileRow}}/{{tileCol}}?own=destination"}}]}}"# + ); + let resolved = resolve_tiles(list, list, doc.as_bytes(), false).unwrap(); + assert_eq!(resolved.withheld_addresses, 1); + let TilesStep::Template(template) = resolved.step else { panic!("expected template") }; + assert_eq!(template, format!("https://{host}/svc/{{z}}/{{y}}/{{x}}?own=destination&collections=roads")); + } + let clean = "https://h.invalid/svc?collections=roads"; + assert_eq!(resolve_tiles(clean, clean, tileset, false).unwrap().withheld_addresses, 0); + // Even a suffix appended by RustGIS must pass the raw-path check. The normal style + // stays under its service; an advertised '..' style does not inherit its token. + for (style, withheld) in [("resources/styles", 0), ("../other/styles", 1), ("%2e%2e/other/styles", 1)] { + let description = serde_json::json!({"defaultStyles": style}); + let service = parse( + "https://h.invalid/rest/services/a/VectorTileServer?token=RGSYNTH_style_20261103", + description.to_string().as_bytes(), + ) + .unwrap(); + assert_eq!(service.withheld_addresses, withheld); + let Offer::Style { url } = &service.layers[0].offer else { panic!("expected style") }; + assert_eq!(url.contains("RGSYNTH_style_20261103"), withheld == 0); } } diff --git a/crates/data/src/source/credentials.rs b/crates/data/src/source/credentials.rs index cee9f13b4f..e649530f8d 100644 --- a/crates/data/src/source/credentials.rs +++ b/crates/data/src/source/credentials.rs @@ -1,97 +1,3 @@ -//! Secret-name and query filtering shared by connection addresses and saved sources. +//! Compatibility path for the shared credential-name and query policy. -/// Whether a key names a secret, wherever it is: an exact list (case aside), and any key -/// naming a password, secret or token. Context-dependent query keys are handled by [`redact_query`]. -pub fn secret_key(k: &str) -> bool { - secret_key_lower(&k.to_ascii_lowercase()) -} - -fn secret_key_lower(k: &str) -> bool { - matches!( - k, - "token" - | "access_token" - | "id_token" - | "refresh_token" - | "api_key" - | "apikey" - | "api-key" - | "x-api-key" - | "key" - | "access_key" - | "accesskey" - | "access_key_id" - | "accesskeyid" - | "secret" - | "client_secret" - | "password" - | "passwd" - | "pwd" - | "sig" - | "signature" - | "sas" - | "authorization" - | "proxy-authorization" - | "cookie" - | "set-cookie" - | "bearer" - ) || k.starts_with("x-amz-") - || k.starts_with("x-goog-") - || k.contains("password") - || k.contains("secret") - || k.contains("token") -} - -/// The query keys of an Azure shared access signature, secret only with both `sig` and `sv`. -const SAS_KEYS: &[&str] = - &["sv", "se", "sp", "sr", "st", "spr", "sip", "si", "ss", "srt", "skoid", "sktid", "skt", "ske", "sks", "skv", "sdd"]; - -fn secret_in(k: &str, keys: &[String]) -> bool { - let has = |x: &str| keys.iter().any(|q| q == x); - secret_key_lower(k) || (has("sig") && has("sv") && SAS_KEYS.contains(&k)) || (k == "code" && has("state")) -} - -/// Removes secret query pairs without changing the address's user-info or connection options. -/// Callers must remove passwords from user-info and fragments before calling this function. -pub fn redact_query(address: &mut String) { - let Some(at) = address.find('?') else { return }; - let query = address.get(at + 1..).unwrap_or(""); - let keys: Vec = query.split('&').map(|kv| kv.split_once('=').map_or(kv, |(k, _)| k).to_ascii_lowercase()).collect(); - if query.split('&').zip(&keys).all(|(kv, key)| !kv.is_empty() && !secret_in(key, &keys)) { - return; - } - let mut kept = String::with_capacity(query.len()); - for (kv, key) in query.split('&').zip(&keys) { - if kv.is_empty() || secret_in(key, &keys) { - continue; - } - if !kept.is_empty() { - kept.push('&'); - } - kept.push_str(kv); - } - address.truncate(at); - if !kept.is_empty() { - address.push('?'); - address.push_str(&kept); - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn access_key_ids_and_their_secrets_are_secret_names() { - for k in ["accessKeyId", "access_key_id", "AccessKeyID", "secretAccessKey", "sessionToken", "X-Amz-Credential"] { - assert!(secret_key(k), "{k}"); - } - for k in ["remember", "region", "buckets", "endpoint"] { - assert!(!secret_key(k), "{k}"); - } - // In a query, the key id goes with the signature. - let mut a = "https://s3.example.org/b/k?accessKeyId=AKIDEXAMPLE&prefix=x".to_string(); - redact_query(&mut a); - assert_eq!(a, "https://s3.example.org/b/k?prefix=x"); - } -} +pub use crate::address::{NameClass, QueryNames, log_sensitive, name_class, normalized_name, redact_query, secret_key}; diff --git a/crates/data/src/source/pgconn.rs b/crates/data/src/source/pgconn.rs index 972b41930c..0f37e6e964 100644 --- a/crates/data/src/source/pgconn.rs +++ b/crates/data/src/source/pgconn.rs @@ -14,21 +14,19 @@ pub fn public_label(s: &str) -> Option { let t = s.trim(); let lower = t.to_ascii_lowercase(); if lower.starts_with("postgresql://") || lower.starts_with("postgres://") { - let (no_userinfo, _) = split_secrets(t, &["password"]); - // A secret in the query (`?password=…`) goes too. - return Some(match no_userinfo.split_once('?') { - Some((base, q)) => { - let kept: Vec<&str> = q - .split('&') - .filter(|kv| { - let k = kv.split_once('=').map(|(k, _)| k).unwrap_or(kv); - !kv.is_empty() && !SECRET_KEYS.iter().any(|s| k.eq_ignore_ascii_case(s)) - }) - .collect(); - if kept.is_empty() { base.to_string() } else { format!("{base}?{}", kept.join("&")) } - } - None => no_userinfo, - }); + let public = split_secrets(t, SECRET_KEYS).0; + let (_, rest) = public.split_once("://")?; + let authority = rest.split(['/', '?', '#']).next()?; + let hosts = authority.rsplit_once('@').map_or(authority, |(_, hosts)| hosts); + // A non-numeric port with no delimited password is an unreadable authority. Multi-host + // URIs, IPv6 and empty hosts (the local socket) keep libpq's existing spelling. + if hosts.split(',').any(|host| { + let port = if host.starts_with('[') { host.split_once(']').map(|(_, rest)| rest) } else { Some(host) }; + port.and_then(|h| h.split_once(':')).is_some_and(|(_, p)| !p.bytes().all(|b| b.is_ascii_digit())) + }) { + return None; + } + return Some(public); } let rest = ["postgresql:", "postgres:", "pg:"].iter().find(|p| lower.starts_with(**p)).and_then(|p| t.get(p.len()..))?; let (public, _) = split_secrets(rest.trim(), SECRET_KEYS); @@ -122,13 +120,17 @@ fn query_secrets(s: &str, secrets: &[&str]) -> (String, Option) { let mut kept = Vec::new(); for kv in query.split('&').filter(|kv| !kv.is_empty()) { let (k, v) = kv.split_once('=').unwrap_or((kv, "")); - if k.eq_ignore_ascii_case("password") { + if crate::address::normalized_name(k).as_deref() == Some("password") { + // The URI driver decodes the name and password once; keyword strings below are + // intentionally not percent-decoded. password = Some(percent_decode(v)); - } else if !secrets.iter().any(|s| k.eq_ignore_ascii_case(s)) { + } else if !secrets.iter().any(|s| crate::address::normalized_name(k).as_deref() == Some(s)) { kept.push(kv); } } - (if kept.is_empty() { base.to_string() } else { format!("{base}?{}", kept.join("&")) }, password) + let mut public = if kept.is_empty() { base.to_string() } else { format!("{base}?{}", kept.join("&")) }; + crate::address::redact_query(&mut public); + (public, password) } /// A libpq keyword/value connection string's pairs (password included), in order; a value is a @@ -221,5 +223,17 @@ mod tests { assert_eq!(pw.as_deref(), Some("s@c")); let (public, pw) = super::split_secrets("postgresql://h/g?password=x", &["password"]); assert_eq!((public.as_str(), pw.as_deref()), ("postgresql://h/g", Some("x"))); + let uri = "postgresql://u@h.invalid/db?pass%77ord=RGSYNTH_pg_20261103&sslmode=require&pass%66ile=RGSYNTH_file_20261103"; + assert_eq!(super::public_label(uri).as_deref(), Some("postgresql://u@h.invalid/db?sslmode=require")); + let (public, password) = super::query_secrets(uri, super::SECRET_KEYS); + assert_eq!(public, "postgresql://u@h.invalid/db?sslmode=require"); + assert_eq!(password.as_deref(), Some("RGSYNTH_pg_20261103")); + let clean = "postgresql://u@h.invalid/db?sslmode=require"; + assert_eq!(super::public_label(clean).as_deref(), Some(clean)); + assert_eq!(super::public_label("postgresql://u:RGSYNTH_unreadable_20261103/path"), None); + let keyword = "host=h.invalid dbname='a%20b' user=u password='RGSYNTH_keyword_20261103'"; + let (public, password) = super::split_secrets(keyword, super::SECRET_KEYS); + assert_eq!(public, "host=h.invalid dbname=a%20b user=u"); + assert_eq!(password.as_deref(), Some("RGSYNTH_keyword_20261103")); } } diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index c18cf994ed..7fd5045ffb 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -1080,6 +1080,7 @@ fn queue_file(s: &mut Session, file_name: String, bytes: Vec, record: GpReco Ok(json!({"pending": true, "download": id, "file": file_name})) } +/// Records a completed file delivery through the generated-message history seam. 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")?; @@ -1099,7 +1100,7 @@ fn file_delivered(s: &mut Session, p: &Value) -> CmdResult { s.notify(message.clone(), !file.record.ok); } let out = json!({"file": file.file_name, "delivered": file.record.ok, "messages": file.record.messages}); - s.project.gp_history.push(file.record); + s.push_gp_record(file.record); if s.project.gp_history.len() > 500 { s.project.gp_history.remove(0); } @@ -1191,7 +1192,7 @@ fn apply(s: &mut Session, ran: Ran, params: Value, res: Result { - s.project.gp_history.push(GpRecord { + s.push_gp_record(GpRecord { tool: name, id, params, @@ -1223,7 +1224,7 @@ fn apply(s: &mut Session, ran: Ran, params: Value, res: Result Vec::new(), }; let outputs = ids.iter().filter_map(|id| s.map().layer(*id).map(|l| l.name.clone())).collect(); - s.project.gp_history.push(GpRecord { tool: name, id, params, command, at, ok: true, messages: result.messages, millis, outputs }); + s.push_gp_record(GpRecord { tool: name, id, params, command, at, ok: true, messages: result.messages, millis, outputs }); if s.project.gp_history.len() > 500 { s.project.gp_history.remove(0); } diff --git a/crates/engine/src/cmd/history.rs b/crates/engine/src/cmd/history.rs index 3a22bd151f..aa3fab750f 100644 --- a/crates/engine/src/cmd/history.rs +++ b/crates/engine/src/cmd/history.rs @@ -20,64 +20,75 @@ pub fn commands() -> Vec { ] } +/// Formats a replayable code fragment inside the guarded history projection. fn code(s: &mut Session, p: &Value) -> CmdResult { let i = uint(p, "run").and_then(|i| usize::try_from(i).ok()).ok_or("run is required: a run's place in the history, from 0")?; - let r = s.project.gp_history.get(i).ok_or("no run there in the history")?; - let (cmd, params) = replay(r)?; - let text = match ps(p, "as").unwrap_or("python") { - "python" => python(&cmd, ¶ms), - "command" => format!("{cmd} {params}"), - "cli" => format!("rustgis-cli run --project {} --cmd {}", shell(&project_file(s)), shell(&format!("{cmd}={params}"))), - other => return Err(format!("unknown kind '{other}': python, command or cli")), - }; + let project = project_file(s); + let r = s.project.gp_history.get_mut(i).ok_or("no run there in the history")?; + let (text, report) = crate::durable::output(r, None, |r| { + let (cmd, params) = replay_written(r)?; + match ps(p, "as").unwrap_or("python") { + "python" => Ok(python(&cmd, ¶ms)), + "command" => Ok(format!("{cmd} {params}")), + "cli" => Ok(format!("rustgis-cli run --project {} --cmd {}", shell(&project), shell(&format!("{cmd}={params}")))), + other => Err(format!("unknown kind '{other}': python, command or cli")), + } + })?; + crate::durable::notify(s, &report); Ok(json!({"code": text})) } /// The history as a Python script that opens the project and runs each tool again, in order. A /// run that failed, or can't be run again, is there as a comment. fn script(s: &mut Session, _: &Value) -> CmdResult { - let runs = &s.project.gp_history; - if runs.is_empty() { + if s.project.gp_history.is_empty() { return Err("the history is empty: run a tool first".into()); } - let name = &s.project.name; - let mut text = format!( - "# The geoprocessing history of the RustGIS project {}, run again in order.\n\ + let name = s.project.name.clone(); + let project = project_file(s); + let unsaved = s.path.is_none(); + let dirty = s.dirty(); + let (result, report) = crate::durable::output(&mut s.project.gp_history, None, |runs| { + let mut text = format!( + "# The geoprocessing history of the RustGIS project {}, run again in order.\n\ # Layers are named as they were then, so this opens the project they belong to.\n", - quoted(name) - ); - // The script opens the saved file: a layer added since, which a run may have used, isn't - // there until the project is saved again. - if s.path.is_none() { - text.push_str("# The project hasn't been saved yet: save it as this file first.\n"); - } else if s.dirty() { - text.push_str("# The project has changes that aren't saved: save it first, so its layers are there.\n"); - } - text.push_str(&format!("import rustgis\n\ns = rustgis.Session({})\n", quoted(&project_file(s)))); - for (n, r) in runs.iter().enumerate() { - let when = if r.at > 0 { - let (date, time) = rustgis_map::graphics::civil(r.at); - format!(" ({date} {time} UTC)") - } else { - String::new() - }; - text.push_str(&format!("\n# {}. {}{when}", n + 1, line(&r.tool))); - match replay(r) { - Ok((cmd, params)) if r.ok => text.push_str(&format!("\n{}\n", python(&cmd, ¶ms))), - Ok((cmd, params)) => { - // An error may quote a connection, password and all: redacted as a parameter's - // value is, not for its addresses only. - let why = r.messages.first().map(|m| line(&redacted_text(m))).unwrap_or_default(); - text.push_str(&format!(": failed when it ran ({why})\n")); - for l in python(&cmd, ¶ms).lines() { - text.push_str(&format!("# {l}\n")); + quoted(&name) + ); + // The script opens the saved file: a layer added since, which a run may have used, isn't + // there until the project is saved again. + if unsaved { + text.push_str("# The project hasn't been saved yet: save it as this file first.\n"); + } else if dirty { + text.push_str("# The project has changes that aren't saved: save it first, so its layers are there.\n"); + } + text.push_str(&format!("import rustgis\n\ns = rustgis.Session({})\n", quoted(&project))); + for (n, r) in runs.iter().enumerate() { + let when = if r.at > 0 { + let (date, time) = rustgis_map::graphics::civil(r.at); + format!(" ({date} {time} UTC)") + } else { + String::new() + }; + text.push_str(&format!("\n# {}. {}{when}", n + 1, line(&r.tool))); + match replay_written(r) { + Ok((cmd, params)) if r.ok => text.push_str(&format!("\n{}\n", python(&cmd, ¶ms))), + Ok((cmd, params)) => { + // An error may quote a connection, password and all: redacted as a parameter's + // value is, not for its addresses only. + let why = r.messages.first().map(|m| line(&redacted_text(m))).unwrap_or_default(); + text.push_str(&format!(": failed when it ran ({why})\n")); + for l in python(&cmd, ¶ms).lines() { + text.push_str(&format!("# {l}\n")); + } } + Err(e) => text.push_str(&format!(": {}\n", line(&e))), } - Err(e) => text.push_str(&format!(": {}\n", line(&e))), } - } - let file = format!("{}_history.py", file_stem(name)); - Ok(json!({"fileName": file, "text": text})) + let file = format!("{}_history.py", file_stem(&name)); + Ok(json!({"fileName": file, "text": text})) + })?; + crate::durable::notify(s, &report); + Ok(result) } /// The commands other than `gp.run` a run is copied as. The history comes from the project file, @@ -88,14 +99,21 @@ const REPLAYED: &[&str] = &["gp.bufferPoint"]; /// The command that runs `r` again, with its parameters: `gp.run` with the tool, or the command /// that ran it (a point buffered where the map was clicked, `gp.bufferPoint`). pub fn replay(r: &GpRecord) -> Result<(String, Value), String> { + let (id, params) = replay_written(r)?; + let (clean, _) = crate::roles::command(&id, ¶ms, 0); + Ok((id, clean.unwrap_or(params))) +} + +/// Formats a record already inside the durable output transaction; data stays replayable. +fn replay_written(r: &GpRecord) -> Result<(String, Value), String> { if let Some((cmd, params)) = &r.command { let Some(cmd) = REPLAYED.iter().find(|c| **c == cmd.as_str()) else { return Err(format!("{} can't be run again: the history names a command that isn't a geoprocessing one", r.tool)); }; - return Ok((cmd.to_string(), redacted(params))); + return Ok((cmd.to_string(), params.clone())); } let id = tool_of(r).ok_or_else(|| format!("{} can't be run again: the history doesn't say which tool it was", r.tool))?; - Ok(("gp.run".into(), json!({"tool": id, "params": redacted(&r.params)}))) + Ok(("gp.run".into(), json!({"tool": id, "params": r.params}))) } /// The id of the tool `r` ran with `gp.run`: kept with the run, or, in projects from before it diff --git a/crates/engine/src/cmd/items.rs b/crates/engine/src/cmd/items.rs index 4bbd792570..82df1221e8 100644 --- a/crates/engine/src/cmd/items.rs +++ b/crates/engine/src/cmd/items.rs @@ -517,15 +517,20 @@ fn import_styles(s: &mut Session, p: &Value) -> CmdResult { Ok(json!({"imported": n})) } -/// The project without its features (layer schemas, symbology, layouts and styles stay). +/// The project without features or sources that would reload them; schemas and styles stay. fn template(p: &Project) -> Project { let mut t = p.clone(); t.gp_history.clear(); + t.offline_areas.clear(); for m in &mut t.maps { for l in &mut m.layers { l.dataset.features.clear(); l.selection.clear(); l.dataset.source = None; + l.source = None; + if let Some(archive) = &mut l.design.archive { + archive.clear(); + } // A feature-less template has no loaded rows to save back to a source. l.postgis = None; l.gpkg = None; @@ -540,29 +545,30 @@ fn template(p: &Project) -> Project { } for t in &mut t.tables { t.features.clear(); + t.source = None; } t } /// Packages only completed imports; runtime download reservations cannot be reopened. /// Refuses JSON beyond the project or ZIP inflation limit before creating the archive. -fn package_project(s: &Session) -> Result, String> { +fn package_project(s: &mut Session) -> Result, String> { s.project.ensure_imports_complete()?; let mut p = s.project.clone(); let mut budget = MAX_EMBED; for m in &mut p.maps { *m = embedded_map(m, &mut budget); } - crate::project::strip_addresses(p.addresses_mut()); - let text = serde_json::to_string(&p).map_err(|e| e.to_string())?; + let name = p.name.clone(); + let (text, report) = crate::durable::output_owned(p, |p| serde_json::to_string(p).map_err(|e| e.to_string()))?; + crate::durable::notify(s, &report); let limit = crate::project::MAX_BYTES.min(rustgis_data::zip::MAX_TOTAL as u64); if text.len() as u64 > limit { return Err(format!("the project is too large to package (maximum {} MiB)", limit >> 20)); } - let readme = - format!("{} — a RustGIS project package.\nOpen it with RustGIS (Project › Open), or unzip it for {}.rgis.\n", p.name, p.name); + let readme = format!("{} — a RustGIS project package.\nOpen it with RustGIS (Project › Open), or unzip it for {}.rgis.\n", name, name); rustgis_data::zip::write(&[ - (format!("{}.{}", safe(&p.name), crate::project::EXTENSION), text.into_bytes()), + (format!("{}.{}", safe(&name), crate::project::EXTENSION), text.into_bytes()), ("README.txt".into(), readme.into_bytes()), ]) } @@ -573,7 +579,7 @@ fn safe(name: &str) -> String { } /// A package of the active map; `mobile` drops hidden layers and simplifies geometry. -fn package_map(s: &Session, mobile: bool, tol: f64) -> Result, String> { +fn package_map(s: &mut Session, mobile: bool, tol: f64) -> Result, String> { let mut m = embedded_map(s.map(), &mut MAX_EMBED.clone()); if mobile { // Groups folded into their layers: a hidden group's layers are dropped with the others. @@ -600,19 +606,23 @@ fn package_map(s: &Session, mobile: bool, tol: f64) -> Result, String> { } } crate::project::ensure_map_imports_complete(&m)?; - crate::project::strip_addresses(m.addresses_mut()); - let text = serde_json::to_string(&m).map_err(|e| e.to_string())?; + let name = m.name.clone(); + let (text, report) = crate::durable::output_owned(m, |m| serde_json::to_string(m).map_err(|e| e.to_string()))?; + crate::durable::notify(s, &report); let kind = if mobile { "mobile map package" } else { "map package" }; - let readme = format!("{} — a RustGIS {kind}.\nAdd it to a project with Data › Import.\n", m.name); - rustgis_data::zip::write(&[(format!("{}.rgismap", safe(&m.name)), text.into_bytes()), ("README.txt".into(), readme.into_bytes())]) + let readme = format!("{} — a RustGIS {kind}.\nAdd it to a project with Data › Import.\n", name); + rustgis_data::zip::write(&[(format!("{}.rgismap", safe(&name)), text.into_bytes()), ("README.txt".into(), readme.into_bytes())]) } /// The geoprocessing history with the layers its runs made, as GeoJSON. -fn package_gp(s: &Session) -> Result, String> { +fn package_gp(s: &mut Session) -> Result, String> { if s.project.gp_history.is_empty() { return Err("no geoprocessing has been run yet".into()); } - let mut entries = vec![("history.json".to_string(), serde_json::to_vec_pretty(&s.project.gp_history).map_err(|e| e.to_string())?)]; + let (history, report) = + 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) { @@ -1019,9 +1029,9 @@ pub fn item_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Vec None => s.map(), }; crate::project::ensure_map_imports_complete(map)?; - let mut map = embedded_map(map, &mut MAX_EMBED.clone()); - crate::project::strip_addresses(map.addresses_mut()); - (map.name.clone(), to_json(&map)?) + let map = embedded_map(map, &mut MAX_EMBED.clone()); + let name = map.name.clone(); + (name, durable_bytes(s, map)?) } "layer" => { let id = s.layer_id(p)?; @@ -1029,9 +1039,9 @@ pub fn item_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Vec if l.dataset.source.as_deref().is_some_and(|source| source.starts_with("geolibre-pending:")) { return Err("this layer is still downloading; wait for it to finish before saving".into()); } - let mut layer = embed_layer(l, &mut MAX_EMBED.clone()); - crate::project::strip_addresses(layer.addresses_mut().collect()); - (layer.name.clone(), to_json(&layer)?) + let layer = embed_layer(l, &mut MAX_EMBED.clone()); + let name = layer.name.clone(); + (name, durable_bytes(s, layer)?) } "styles" => { if s.project.styles.is_empty() { @@ -1051,7 +1061,7 @@ pub fn item_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Vec if let Some(n) = ps(p, "name").map(str::trim).filter(|n| !n.is_empty()) { t.name = n.chars().take(200).collect(); } - (t.name.clone(), to_json(&t)?) + (t.name.clone(), durable_bytes(s, t)?) } "projectPackage" => (s.project.name.clone(), package_project(s)?), "mapPackage" | "mobilePackage" => { @@ -1061,8 +1071,11 @@ pub fn item_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Vec "gpPackage" => ("results".to_string(), package_gp(s)?), "bookmarks" => (format!("{} bookmarks", s.map().name), super::navigate::bookmarks_file(s.map())?), "task" => { - let t = task_ref(s, p)?; - (t.name.clone(), to_json(t)?) + let t = task_mut(s, p)?; + let name = t.name.clone(); + let (bytes, report) = crate::durable::output(t, None, to_json)?; + crate::durable::notify(s, &report); + (name, bytes) } "obj" => { let id = s.layer_id(p)?; @@ -1114,3 +1127,10 @@ pub(crate) fn save_item(s: &mut Session, kind: &str, p: &Value) -> CmdResult { rustgis_data::io::write_atomic(Path::new(&path), &bytes).map_err(|e| format!("{path}: {e}"))?; Ok(json!({"path": path, "bytes": bytes.len()})) } + +/// Consumes the existing item draft, emitting losses after serialization has completed. +fn durable_bytes(s: &mut Session, value: T) -> Result, String> { + let (bytes, report) = crate::durable::output_owned(value, to_json)?; + crate::durable::notify(s, &report); + Ok(bytes) +} diff --git a/crates/engine/src/cmd/notebook.rs b/crates/engine/src/cmd/notebook.rs index b9d99e8940..cbe7abf8c4 100644 --- a/crates/engine/src/cmd/notebook.rs +++ b/crates/engine/src/cmd/notebook.rs @@ -278,6 +278,7 @@ fn run(s: &mut Session, p: &Value) -> CmdResult { r } +/// Runs command cells and cleans generated output before applying its retained-size bound. fn run_cells(s: &mut Session, p: &Value) -> CmdResult { let i = index(s, p)?; let cells: Vec = match p.get("cell") { @@ -328,8 +329,9 @@ 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) => e, + Err(e) => rustgis_data::diagnostics::Redacted::new(&e).into_string(), }; + let text = rustgis_data::address::scrub_addresses(&text).0.into_owned(); // 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| { @@ -578,8 +580,58 @@ fn jupyter_file(nb: &Notebook) -> Value { } /// The notebook as a RustGIS notebook or (nbformat 4) Jupyter notebook. -pub fn notebook_file(s: &Session, i: usize, jupyter: bool) -> Result, String> { - let nb = s.project.notebooks.get(i).ok_or("no notebook")?; - let v = if jupyter { jupyter_file(nb) } else { serde_json::to_value(nb).map_err(|e| e.to_string())? }; - serde_json::to_vec_pretty(&v).map_err(|e| e.to_string()) +pub fn notebook_file(s: &mut Session, i: usize, jupyter: bool) -> Result, String> { + let nb = s.project.notebooks.get_mut(i).ok_or("no notebook")?; + let (bytes, report) = crate::durable::output(nb, None, |nb| { + let v = if jupyter { jupyter_file(nb) } else { serde_json::to_value(nb).map_err(|e| e.to_string())? }; + serde_json::to_vec_pretty(&v).map_err(|e| e.to_string()) + })?; + crate::durable::notify(s, &report); + Ok(bytes) +} + +impl crate::durable::Durable for Notebook { + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut crate::durable::Prepare<'_>) { + use crate::durable::Leaf; + let Self { name: _, cells, jupyter: _ } = self; + let commands = self.language().is_none(); + for (i, Cell { kind, source, output, error: _, jupyter: _ }) in cells.iter().enumerate() { + if commands && *kind == CellKind::Code { + p.script(Leaf::CellSource(i), source); + } + if let Some(output) = output { + p.generated(Leaf::CellOutput(i), output); + } + } + } + + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[crate::durable::Owner], leaf: &crate::durable::Leaf) -> Option> { + use crate::durable::{FieldMut, Leaf}; + if !owners.is_empty() { + return None; + } + match leaf { + Leaf::CellSource(i) => Some(FieldMut::Text(&mut self.cells.get_mut(*i)?.source)), + Leaf::CellOutput(i) => self.cells.get_mut(*i)?.output.as_mut().map(FieldMut::Text), + _ => None, + } + } +} + +/// Cleans a notebook document on its parsed copy, preserving foreign kernels and opaque metadata. +pub(crate) fn durable_document(text: &str, depth: usize) -> Result<(String, crate::durable::Report), String> { + let nb = parse(text, "Notebook")?; + let jupyter = serde_json::from_str::(text).is_ok_and(|v| v.get("nbformat").is_some()); + crate::durable::output_owned_changed(nb, depth, |nb, changed| { + if !changed { + return Ok(text.into()); + } + if jupyter { + serde_json::to_string(&jupyter_file(nb)).map_err(|e| e.to_string()) + } else { + serde_json::to_string(nb).map_err(|e| e.to_string()) + } + }) } diff --git a/crates/engine/src/cmd/presentation.rs b/crates/engine/src/cmd/presentation.rs index 8b59ace7f1..16d9e3d1c9 100644 --- a/crates/engine/src/cmd/presentation.rs +++ b/crates/engine/src/cmd/presentation.rs @@ -22,6 +22,7 @@ fn need_presentation(s: &Session) -> Option<&'static str> { if s.project.presentations.is_empty() { Some("add a presentation first (Publish › New Presentation)") } else { None } } +/// Registers presentation actions, with public generated video labels and guarded outputs. pub fn commands() -> Vec { vec![ C::new("presentation.new", "New Presentation", "Publish › Presentation", |s, p| { @@ -116,7 +117,7 @@ pub fn commands() -> Vec { (None, None) => None, }; 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(|| file_name(&source)); + 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 }) }) .when(need_presentation) @@ -166,8 +167,26 @@ pub fn commands() -> Vec { ] } -fn file_name(source: &str) -> String { - source.rsplit(['/', '\\']).next().filter(|n| !n.is_empty()).unwrap_or(source).chars().take(200).collect() +/// Derives generated labels from the public address before extracting a path or host. User +/// titles/captions and local filenames (including '?' and '#') are data and retain their bytes. +pub(crate) fn video_label(source: &str) -> String { + use crate::durable::{Kept, public_address}; + let clean = match public_address(source) { + Kept::Unchanged => source.to_string(), + Kept::Changed(s) => s, + Kept::Withheld(_) => return "Video".into(), + }; + let inner = crate::sources::template_archive(&clean).map_or(clean.as_str(), |(_, a)| a); + let inner = rustgis_data::s3_address::parse(inner).ok().flatten().map_or(inner, |b| b.url); + let inner = inner.strip_prefix(crate::render::rustgis_tiles::s3::ANONYMOUS).unwrap_or(inner); + let remote = inner.split_once("://").filter(|(s, _)| ["http", "https", "s3"].iter().any(|p| p.eq_ignore_ascii_case(s))); + let name = if let Some((_, rest)) = remote { + let rest = rest.split(['?', '#']).next().unwrap_or(""); + rest.rsplit('/').find(|s| !s.is_empty()).unwrap_or("Video") + } else { + inner.rsplit(['/', '\\']).next().filter(|s| !s.is_empty()).unwrap_or(inner) + }; + name.chars().take(200).collect() } /// The presentation named or numbered by `presentation`, else the active one, else the last. @@ -268,9 +287,11 @@ fn update_page(s: &mut Session, p: &Value) -> CmdResult { } /// A presentation file. -pub fn presentation_file(s: &Session, i: usize) -> Result, String> { - let pres = s.project.presentations.get(i).ok_or("no presentation")?; - serde_json::to_vec_pretty(pres).map_err(|e| e.to_string()) +pub fn presentation_file(s: &mut Session, i: usize) -> Result, String> { + let pres = s.project.presentations.get_mut(i).ok_or("no presentation")?; + let (bytes, report) = crate::durable::output(pres, None, |p| serde_json::to_vec_pretty(p).map_err(|e| e.to_string()))?; + crate::durable::notify(s, &report); + Ok(bytes) } /// A page drawn as a page layout: its title across the top and its content below. @@ -342,7 +363,7 @@ pub fn page_layout(s: &Session, pres: &Presentation, page: &Page) -> Layout { l.add( "Play", (m + bw * 0.3, top + bh / 2.0 - 6.0, bw * 0.4, 12.0), - text(&format!("Video: {}", file_name(source)), 18.0, dark, true), + text(&format!("Video: {}", video_label(source)), 18.0, dark, true), ); } if cap_h > 0.0 { @@ -359,7 +380,7 @@ pub fn page_layout(s: &Session, pres: &Presentation, page: &Page) -> Layout { l.add( "Play", (m + bw * 0.3, top + bh / 2.0 - 6.0, bw * 0.4, 12.0), - text(&format!("Video: {}", file_name(source)), 20.0, Rgba::rgb(0xFF, 0xFF, 0xFF), true), + text(&format!("Video: {}", video_label(source)), 20.0, Rgba::rgb(0xFF, 0xFF, 0xFF), true), ); if !caption.trim().is_empty() { l.add("Caption", (m, h - m - h * 0.07, w - 2.0 * m, h * 0.07), text(caption, 15.0, Rgba::rgb(0x55, 0x5B, 0x66), false)); @@ -430,6 +451,7 @@ pub fn export_bytes(s: &mut Session, i: usize, format: &str, dpi: f64) -> Result } let mut pdf_pages = Vec::new(); let mut slides = String::new(); + let mut output_report = crate::durable::Report::default(); let mut held = 0usize; let (mut frames_pictured, mut pages_pictured) = (Vec::new(), Vec::new()); for k in 0..n { @@ -452,11 +474,25 @@ pub fn export_bytes(s: &mut Session, i: usize, format: &str, dpi: f64) -> Result let body = match &page.kind { // The poster is the drawn page, which already shows the title and caption. The // source is played as given (a file next to the slideshow or a web address). - PageKind::Video { source, .. } => format!( - "", - html_escape(&page.title), - html_escape(playable(source)) - ), + PageKind::Video { source, .. } => { + let source = match crate::durable::public_address(source) { + crate::durable::Kept::Unchanged => source.clone(), + crate::durable::Kept::Changed(s) => { + output_report.videos = output_report.videos.saturating_add(1); + s + } + crate::durable::Kept::Withheld(prefix) => { + output_report.videos = output_report.videos.saturating_add(1); + output_report.withheld = output_report.withheld.saturating_add(1); + prefix.into() + } + }; + format!( + "", + html_escape(&page.title), + html_escape(playable(&source)) + ) + } _ => format!("\"{}\"", html_escape(&page.title)), }; let notes = if page.notes.is_empty() { String::new() } else { format!("", html_escape(&page.notes)) }; @@ -466,6 +502,7 @@ pub fn export_bytes(s: &mut Session, i: usize, format: &str, dpi: f64) -> Result return Err(format!("the export passed {} MB at page {}; export at a lower dpi", BUDGET >> 20, k + 1)); } } + crate::durable::notify(s, &output_report); if format == "pdf" { super::layout::pictured_notices(s, &frames_pictured, &pages_pictured); return Ok((rustgis_render::pdf::write(&pdf_pages, &name), n)); diff --git a/crates/engine/src/cmd/project.rs b/crates/engine/src/cmd/project.rs index 99a5d38c55..7577b233b3 100644 --- a/crates/engine/src/cmd/project.rs +++ b/crates/engine/src/cmd/project.rs @@ -148,7 +148,9 @@ pub(crate) fn reset_to(s: &mut Session, p: Project) { reset(s, p); } +/// Replaces project authority, clearing the exchange record, item trust and active connections. fn reset(s: &mut Session, p: Project) { + s.exchange = None; super::connections::clear_active(); s.database_logins.clear_activations(); s.database_logins.clear_requests(); @@ -302,13 +304,12 @@ fn open_text(s: &mut Session, p: &Value) -> CmdResult { fn write(s: &mut Session, path: PathBuf) -> CmdResult { let path = if path.extension().is_none() { path.with_extension(crate::project::EXTENSION) } else { path }; let destination = std::path::absolute(&path).map_err(|error| format!("{}: {error}", path.display()))?; - let stripped = s.project.save(&destination)?; + let report = s.project.save(&destination)?; + let stripped = report.layer_addresses(); s.mark_saved(); s.edit_counts.clear(); s.notify(format!("Saved {}", path.display()), false); - if stripped > 0 { - s.notify(format!("{stripped} layer address(es) were saved without their keys, tokens or passwords: after RustGIS closes, add those layers again to give them"), false); - } + crate::durable::notify(s, &report); s.path = Some(destination); Ok(json!({"path": path.display().to_string(), "stripped": stripped})) } diff --git a/crates/engine/src/cmd/service.rs b/crates/engine/src/cmd/service.rs index 88fcda57f4..9c54789392 100644 --- a/crates/engine/src/cmd/service.rs +++ b/crates/engine/src/cmd/service.rs @@ -55,9 +55,9 @@ fn url_of(p: &Value) -> Result<&str, String> { pub(crate) fn without_token(p: &Value) -> Result, String> { use crate::render::rustgis_tiles::arcgis_auth; let Some(url) = ps(p, "url").map(str::trim) else { return Ok(None) }; - let has_token = url - .split_once('?') - .is_some_and(|(_, q)| q.split('&').any(|kv| kv.split('=').next().is_some_and(|k| k.eq_ignore_ascii_case("token")))); + let has_token = url.split_once('?').is_some_and(|(_, q)| { + q.split('&').any(|kv| kv.split('=').next().is_some_and(|k| rustgis_data::address::normalized_name(k).as_deref() == Some("token"))) + }); // ArcGIS REST addresses only: another kind of service's `token` stays in its address. let path = url.split(['?', '#']).next().unwrap_or(url).to_ascii_lowercase(); let arcgis = match ps(p, "type") { @@ -169,6 +169,7 @@ fn capabilities(p: &Value) -> Result { result.service.layers.push(layer); result.sources.push(address.clone()); } + result.service.withheld_addresses = result.service.withheld_addresses.saturating_add(service.withheld_addresses); result.service.notes.extend(service.notes); let features = result.service.layers.iter().any(|l| matches!(l.offer, Offer::Features { .. })); let tiles = result.service.layers.iter().any(|l| matches!(l.offer, Offer::Tiles { .. } | Offer::VectorTiles { .. })); @@ -330,13 +331,15 @@ fn checked_template(service: &str, t: &str) -> Result { /// The tile template an OGC API tileset list advertises (its WebMercatorQuad tileset's), read now /// (the list, then the tileset when the list doesn't carry it). `None` when it can't be read. #[cfg(not(target_arch = "wasm32"))] -fn advertised_template(list: &str, vector: bool) -> Option { +fn advertised_template(list: &str, vector: bool, withheld: &mut u32) -> Option { use rustgis_data::ogc::{TilesStep, resolve_tiles}; let mut doc = list.to_string(); for _ in 0..2 { crate::render::rustgis_tiles::address::check(&doc, Some(list)).ok()?; let body = crate::render::rustgis_tiles::cache::download_blocking(&doc).ok()?; - match resolve_tiles(list, &doc, &body, vector)? { + let resolved = resolve_tiles(list, &doc, &body, vector)?; + *withheld = withheld.saturating_add(resolved.withheld_addresses); + match resolved.step { TilesStep::Template(t) => return checked_template(list, &t).ok(), TilesStep::Next(next) => doc = next, } @@ -346,7 +349,7 @@ fn advertised_template(list: &str, vector: bool) -> Option { /// In the browser nothing waits: the app reads the template itself and passes it as `"tiles"`. #[cfg(target_arch = "wasm32")] -fn advertised_template(_: &str, _: bool) -> Option { +fn advertised_template(_: &str, _: bool, _: &mut u32) -> Option { None } @@ -384,13 +387,18 @@ pub fn service_json(svc: &Service) -> Value { layer }) .collect(); - json!({"kind": svc.kind, "title": svc.title, "count": layers.len(), "layers": layers, "notes": svc.notes}) + let mut notes = svc.notes.clone(); + if svc.withheld_addresses > 0 { + notes.push(rustgis_data::ogc::QUERY_WITHHELD_NOTICE.replace("{n}", &svc.withheld_addresses.to_string())); + } + json!({"kind": svc.kind, "title": svc.title, "count": layers.len(), "layers": layers, "notes": notes}) } -pub(crate) fn layers(_s: &mut Session, p: &Value) -> CmdResult { +pub(crate) fn layers(s: &mut Session, p: &Value) -> CmdResult { let owned = without_token(p)?; let p = owned.as_ref().unwrap_or(p); let found = capabilities(p)?; + notify_withheld(s, found.service.withheld_addresses); let mut out = service_json(&found.service); if let Some(layers) = out.get_mut("layers").and_then(Value::as_array_mut) { for (layer, source) in layers.iter_mut().zip(found.sources) { @@ -403,6 +411,11 @@ pub(crate) fn layers(_s: &mut Session, p: &Value) -> CmdResult { Ok(out) } +/// Counts destinations, not pairs; clean discovery emits no credential-forwarding notice. +fn notify_withheld(s: &mut Session, count: u32) { + s.notify_loss(rustgis_data::ogc::QUERY_WITHHELD_NOTICE, count); +} + /// Adds one of a service's layers: map layers as tiles (an overlay), a style as a style, features /// downloaded. pub(crate) fn add(s: &mut Session, p: &Value) -> CmdResult { @@ -411,6 +424,15 @@ pub(crate) fn add(s: &mut Session, p: &Value) -> CmdResult { let url = url_of(p)?.to_string(); let want = ps(p, "layer").map(str::trim).filter(|l| !l.is_empty()).ok_or("layer is required")?; let found = capabilities(p)?; + let mut withheld = found.service.withheld_addresses; + let result = add_discovered(s, p, &url, want, &found, &mut withheld); + notify_withheld(s, withheld); + result +} + +/// Adds from an already-read listing, accumulating resolver losses for `add`'s single notice +/// epilogue. Validation and execution errors return unchanged through that epilogue too. +fn add_discovered(s: &mut Session, p: &Value, url: &str, want: &str, found: &Discovery, withheld: &mut u32) -> CmdResult { let candidates = || found.service.layers.iter().zip(&found.sources).filter(|(_, source)| ps(p, "source_url").is_none_or(|s| s == source.as_str())); let (layer, source) = candidates() @@ -480,7 +502,7 @@ pub(crate) fn add(s: &mut Session, p: &Value) -> CmdResult { // the usual path. let advertised = match (&layer.tilesets, ps(p, "tiles")) { (Some(_), Some(t)) => Some(checked_template(source, t)?), - (Some(list), None) => advertised_template(list, matches!(layer.offer, Offer::VectorTiles { .. })), + (Some(list), None) => advertised_template(list, matches!(layer.offer, Offer::VectorTiles { .. }), withheld), _ => None, }; match &layer.offer { diff --git a/crates/engine/src/cmd/sharing.rs b/crates/engine/src/cmd/sharing.rs index c857c4eb62..0b68baa00f 100644 --- a/crates/engine/src/cmd/sharing.rs +++ b/crates/engine/src/cmd/sharing.rs @@ -204,25 +204,30 @@ pub fn share_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Ve Ok((m.name.clone(), ogc_zip(&m.name, &layers, &[])?, m.name.clone())) } "webTool" => { - let ms = &s.project.models; + let ms = &mut s.project.models; let m = match ps(p, "model") { - Some(n) => ms.iter().find(|m| m.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no model '{n}'"))?, - None => ms.last().ok_or("make a model first (ModelBuilder)")?, + 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 params: Vec = m - .steps - .iter() - .enumerate() - .flat_map(|(i, st)| { - st.params - .as_object() - .into_iter() - .flatten() - .map(move |(k, v)| json!({"step": i + 1, "tool": st.tool, "name": k, "default": v})) - }) - .collect(); - let tool = json!({"format": "RustGIS web tool", "version": 1, "name": m.name, "steps": m.steps, "parameters": params}); - Ok((m.name.clone(), serde_json::to_vec_pretty(&tool).map_err(|e| e.to_string())?, m.name.clone())) + let name = m.name.clone(); + let (bytes, report) = crate::durable::output(m, None, |m| { + let params: Vec = m + .steps + .iter() + .enumerate() + .flat_map(|(i, st)| { + st.params + .as_object() + .into_iter() + .flatten() + .map(move |(k, v)| json!({"step": i + 1, "tool": st.tool, "name": k, "default": v})) + }) + .collect(); + let tool = json!({"format": "RustGIS web tool", "version": 1, "name": m.name, "steps": m.steps, "parameters": params}); + serde_json::to_vec_pretty(&tool).map_err(|e| e.to_string()) + })?; + crate::durable::notify(s, &report); + Ok((name.clone(), bytes, name)) } "webScene" => { let mut m = map_of(s, p)?; diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index f1d0d33aa1..d659d09a5f 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -522,7 +522,9 @@ fn save_model(s: &mut Session, p: &Value) -> CmdResult { /// A model as a `*.model.json` file: written to `path`, or its text returned. fn export_model(s: &mut Session, p: &Value) -> CmdResult { let (name, g, _) = model_graph(s, p)?; - let text = serde_json::to_string_pretty(&crate::model::to_file(&name, &g)).map_err(|e| e.to_string())?; + let (text, report) = + crate::durable::output_owned(g, |g| serde_json::to_string_pretty(&crate::model::to_file(&name, g)).map_err(|e| e.to_string()))?; + crate::durable::notify(s, &report); let file: String = name.chars().map(|c| if c.is_alphanumeric() || c == '-' || c == '_' { c } else { '_' }).collect(); match ps(p, "path") { Some(path) => { @@ -630,21 +632,10 @@ pub fn refused_in_script(id: &str) -> bool { pub fn script_commands(script: &str) -> Result, String> { let mut out = Vec::new(); for (n, line) in script.lines().enumerate().take(MAX_SCRIPT_LINES) { - let line = line.trim(); - if line.is_empty() || line.starts_with('#') { - continue; - } - let (id, rest) = line.split_once(char::is_whitespace).unwrap_or((line, "")); - let id = id.trim_end_matches('='); - let params: Value = if rest.trim().is_empty() { - Value::Null - } else { - serde_json::from_str(rest.trim()).map_err(|e| format!("line {}: {e}", n + 1))? - }; - if refused_in_script(id) { - return Err(format!("line {}: '{id}' can't run in a script", n + 1)); + 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)); } - out.push((n + 1, id.to_string(), params)); } Ok(out) } diff --git a/crates/engine/src/durable.rs b/crates/engine/src/durable.rs new file mode 100644 index 0000000000..635ff2973c --- /dev/null +++ b/crates/engine/src/durable.rs @@ -0,0 +1,700 @@ +//! Controlled, synchronous durable output. Preparation owns every replacement before the first +//! mutation. The edit list is also the rollback journal; applying and undoing it only swaps +//! disjoint whole fields at fixed locations. No observer, policy planner or callback may see the +//! temporary projection. Formatters are internal, borrow only the projection and never reenter. + +use std::collections::{HashSet, hash_map::RandomState}; +use std::hash::BuildHasher; +use std::sync::{Mutex, OnceLock}; + +use serde_json::Value; + +/// Counted losses at the output boundary. Generated display text is cleaned without a notice. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Report { + pub(crate) addresses: u32, + pub(crate) videos: u32, + pub(crate) steps: u32, + pub(crate) lines: u32, + pub(crate) withheld: u32, + pub(crate) unchecked: u32, + pub(crate) not_remembered: bool, + pub(crate) lost: u32, + pub(crate) not_retained: u32, +} + +impl Report { + /// The existing `project.save` result's `stripped` count: changed layer addresses only. + pub fn layer_addresses(&self) -> u32 { + self.addresses + } + + /// Addresses whose keys the receiving session lost during peer replacement. + pub fn lost(&self) -> u32 { + self.lost + } + + /// One notice for each kind of loss, and none for a clean output. + pub fn notices(&self) -> Vec { + let mut out = Vec::new(); + for (n, text) in [ + ( + self.addresses, + "layer address(es) were saved without their keys, tokens or passwords: after RustGIS closes, add those layers again to give them", + ), + (self.videos, "video address(es) were written without their keys, tokens or passwords"), + (self.steps, "executable step(s) were written without their credentials; supply them again before replay"), + (self.lines, "command line(s) were written without their credentials; supply them again before replay"), + (self.withheld, "credential-bearing slot(s) could not be read and were withheld"), + (self.unchecked, "step(s) contain opaque parameters that may hold credentials and were kept unchecked"), + (self.not_retained, "address(es) exceeded the bridge retention limit; their keys will not survive a round trip"), + (self.lost, "layer address(es) lost their keys when a peer sent back its project; add them again in the receiving session"), + ] { + if n > 0 { + out.push(format!("{n} {text}")); + } + } + if self.not_remembered { + out.push("This output's credential restoration exceeds the memory limit; reopening it will not bring its keys back".into()); + } + out + } + + /// Adds the independently prepared losses of a nested typed document. + pub(crate) fn merge(&mut self, other: &Self) { + self.addresses = self.addresses.saturating_add(other.addresses); + self.videos = self.videos.saturating_add(other.videos); + self.steps = self.steps.saturating_add(other.steps); + self.lines = self.lines.saturating_add(other.lines); + self.withheld = self.withheld.saturating_add(other.withheld); + self.unchecked = self.unchecked.saturating_add(other.unchecked); + self.not_remembered |= other.not_remembered; + self.lost = self.lost.saturating_add(other.lost); + self.not_retained = self.not_retained.saturating_add(other.not_retained); + } +} + +/// A child owner in a typed location, independent of any field's current contents. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub(crate) enum Owner { + Map(usize), + Area(usize), + Table(usize), + Task(usize), + Model(usize), + History(usize), + Notebook(usize), + Presentation(usize), + Graph, +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub(crate) enum Leaf { + Map(rustgis_map::SlotPos), + Layer(rustgis_map::LayerRole), + Table, + Task(usize), + ModelStep(usize), + ModelNode(usize), + HistoryParams, + HistoryCommand, + Message(usize), + CellSource(usize), + CellOutput(usize), + Video(usize), +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub(crate) struct At { + pub(crate) owners: Vec, + pub(crate) leaf: Leaf, +} + +#[derive(Clone, Debug, PartialEq)] +enum Payload { + Text(String), + Json(Value), +} + +pub(crate) enum FieldMut<'a> { + Text(&'a mut String), + Json(&'a mut Value), +} + +impl FieldMut<'_> { + /// Type-checks before swapping; after the swap the caller immediately advances its journal. + fn swap(self, held: &mut Payload) -> bool { + match (self, held) { + (Self::Text(field), Payload::Text(value)) => std::mem::swap(field, value), + (Self::Json(field), Payload::Json(value)) => std::mem::swap(field, value), + _ => return false, + } + true + } + + /// Clones only one changed owning field, after the live projection has been rolled back. + fn payload(&self) -> Payload { + match self { + Self::Text(v) => Payload::Text((**v).clone()), + Self::Json(v) => Payload::Json((**v).clone()), + } + } + + /// Compares the actual parsed field to the recorded written value without policy dispatch. + fn equals(&self, value: &Payload) -> bool { + match (self, value) { + (Self::Text(a), Payload::Text(b)) => **a == *b, + (Self::Json(a), Payload::Json(b)) => **a == *b, + _ => false, + } + } + + /// Counts retained text/JSON bytes before copying into the bounded reopen memory. + fn bytes(&self) -> usize { + match self { + Self::Text(v) => v.len(), + Self::Json(v) => json_bytes(v), + } + } +} + +/// Counts payload without allocating a serialized copy, including JSON punctuation/escaping. +fn json_bytes(v: &Value) -> usize { + struct Counter(usize); + impl std::io::Write for Counter { + /// Counts the serializer's bytes with saturating arithmetic, retaining no payload. + fn write(&mut self, b: &[u8]) -> std::io::Result { + self.0 = self.0.saturating_add(b.len()); + Ok(b.len()) + } + /// The counting writer has no buffered bytes to flush. + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + let mut c = Counter(0); + if serde_json::to_writer(&mut c, v).is_err() { + return usize::MAX; + } + c.0 +} + +pub(crate) trait Durable { + /// Preparation is read-only, exhaustive over owning fields, and offers each leaf once. + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut Prepare<'_>); + /// Resolves recorded indices only, through plain owned fields, without allocation or policy. + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[Owner], leaf: &Leaf) -> Option>; +} + +#[derive(Clone, Copy, PartialEq, Eq)] +enum Kind { + Credential, + Path, + Generated, +} +struct Edit { + at: At, + held: Payload, + kind: Kind, +} + +pub(crate) struct Prepare<'a> { + owners: Vec, + edits: Vec, + pub(crate) report: Report, + relative_to: Option<&'a str>, + pub(crate) depth: usize, +} + +impl<'a> Prepare<'a> { + /// Makes every replacement and journal allocation before the live borrow is mutated. + fn new(relative_to: Option<&'a str>, depth: usize) -> Self { + Self { owners: Vec::new(), edits: Vec::new(), report: Report::default(), relative_to, depth } + } + + /// Prepares one child under a fixed, typed owning location. + pub(crate) fn child(&mut self, owner: Owner, value: &impl Durable) { + self.owners.push(owner); + value.prepare(self); + self.owners.pop(); + } + + /// Records a single replacement of a whole owning field, never a subfield of an edit. + fn edit(&mut self, leaf: Leaf, held: Payload, kind: Kind) { + self.edits.push(Edit { at: At { owners: self.owners.clone(), leaf }, held, kind }); + } + + /// Cleans an address and then makes its local path relative, in one journal entry. + pub(crate) fn string(&mut self, leaf: Leaf, value: &str, address: bool, local_path: bool) { + let mut replacement = None; + let mut kind = Kind::Path; + if address { + replacement = match public_address(value) { + Kept::Unchanged => None, + Kept::Changed(s) => Some(s), + Kept::Withheld(prefix) if prefix != value => { + self.report.withheld = self.report.withheld.saturating_add(1); + Some(prefix.into()) + } + Kept::Withheld(_) => None, + }; + if replacement.is_some() { + kind = Kind::Credential; + if matches!(leaf, Leaf::Video(_)) { + self.report.videos = self.report.videos.saturating_add(1); + } else { + self.report.addresses = self.report.addresses.saturating_add(1); + } + } + } + let current = replacement.as_deref().unwrap_or(value); + if local_path + && let Some(dir) = self.relative_to + && rustgis_map::paths::is_absolute_local(current) + && let Some(relative) = rustgis_map::paths::relative_to(dir, current).filter(|r| rustgis_map::paths::is_local(r)) + { + replacement = Some(relative); + } + if let Some(s) = replacement.filter(|s| s != value) { + self.edit(leaf, Payload::Text(s), kind); + } + } + + /// Prepares a command's replayable parameters using explicit data/configuration roles. + pub(crate) fn command(&mut self, leaf: Leaf, id: &str, value: &Value) { + let (clean, report) = crate::roles::command(id, value, self.depth); + self.report.merge(&report); + if let Some(clean) = clean { + self.report.steps = self.report.steps.saturating_add(1); + self.edit(leaf, Payload::Json(clean), Kind::Credential); + } + } + + /// Model and history tool params have the same roles as the corresponding `gp.run` params. + pub(crate) fn tool(&mut self, leaf: Leaf, id: &str, value: &Value) { + let (clean, report) = crate::roles::tool(id, value, self.depth); + self.report.merge(&report); + if let Some(clean) = clean { + self.report.steps = self.report.steps.saturating_add(1); + self.edit(leaf, Payload::Json(clean), Kind::Credential); + } + } + + /// Rewrites only changed command lines; comments, data and foreign-language cells stay exact. + pub(crate) fn script(&mut self, leaf: Leaf, source: &str) { + let (clean, report) = crate::roles::script(source, self.depth); + self.report.merge(&report); + if let Some(clean) = clean { + self.edit(leaf, Payload::Text(clean), Kind::Credential); + } + } + + /// Cleans only addresses in text RustGIS generated, without claiming opaque text detection. + pub(crate) fn generated(&mut self, leaf: Leaf, source: &str) { + let (clean, n) = rustgis_data::address::scrub_addresses(source); + if n > 0 { + self.edit(leaf, Payload::Text(clean.into_owned()), Kind::Generated); + } + } +} + +struct Transaction<'a, T: Durable> { + live: &'a mut T, + edits: Vec, + applied: usize, + defect: bool, +} + +impl Transaction<'_, T> { + /// Guard is already armed. The original becomes journal-owned at each infallible swap. + fn apply(&mut self) -> Result<(), String> { + for edit in &mut self.edits { + let field = self.live.field_mut(&edit.at.owners, &edit.at.leaf).ok_or("internal output location changed")?; + if !field.swap(&mut edit.held) { + return Err("internal output field type changed".into()); + } + self.applied += 1; + } + Ok(()) + } + + /// Restores the applied prefix in reverse, with no discovery, parsing or allocation on the + /// valid path. A resolver defect is an internal error, never a successful partial output. + fn rollback(&mut self) { + while self.applied > 0 { + self.applied -= 1; + if let Some(edit) = self.edits.get_mut(self.applied) { + match self.live.field_mut(&edit.at.owners, &edit.at.leaf) { + Some(field) => self.defect |= !field.swap(&mut edit.held), + None => self.defect = true, + } + } else { + self.defect = true; + } + } + } +} + +impl Drop for Transaction<'_, T> { + /// Restores the applied journal before releasing the live borrow, including native unwinding. + fn drop(&mut self) { + self.rollback(); + } +} + +/// Rejects repeated locations before arming, so one edit can never overwrite another's original. +fn prepared<'a>(value: &impl Durable, relative_to: Option<&'a str>, depth: usize) -> Result, String> { + let mut p = Prepare::new(relative_to, depth); + value.prepare(&mut p); + let mut seen = HashSet::new(); + if p.edits.iter().any(|e| !seen.insert(&e.at)) { + return Err("internal duplicate output location".into()); + } + drop(seen); + Ok(p) +} + +/// No whole-project clone: every live field is restored on success, error and native unwind. +/// Abort/OOM/process death cannot run Drop; no temporarily edited state is persisted by them. +pub(crate) fn output( + value: &mut T, + relative_to: Option<&str>, + f: impl FnOnce(&T) -> Result, +) -> Result<(R, Report), String> { + let p = prepared(value, relative_to, 0)?; + let mut t = Transaction { live: value, edits: p.edits, applied: 0, defect: false }; + t.apply()?; + let result = f(t.live); + t.rollback(); + if t.defect { + return Err("internal output rollback failed".into()); + } + result.map(|r| (r, p.report)) +} + +/// Consumes a draft the writer already owns, after structural edits; no extra project clone. +pub(crate) fn output_owned(value: T, f: impl FnOnce(&T) -> Result) -> Result<(R, Report), String> { + output_owned_at(value, 0, f) +} + +/// Nested documents share the executable nesting budget, instead of starting a fresh one. +pub(crate) fn output_owned_at( + value: T, + depth: usize, + f: impl FnOnce(&T) -> Result, +) -> Result<(R, Report), String> { + output_owned_changed(value, depth, |value, _| f(value)) +} + +/// Typed documents serialize only when an actual field changed, independently of notices. +pub(crate) fn output_owned_changed( + mut value: T, + depth: usize, + f: impl FnOnce(&T, bool) -> Result, +) -> Result<(R, Report), String> { + let p = prepared(&value, None, depth)?; + let changed = !p.edits.is_empty(); + for mut edit in p.edits { + if !value.field_mut(&edit.at.owners, &edit.at.leaf).ok_or("internal output location changed")?.swap(&mut edit.held) { + return Err("internal output field type changed".into()); + } + } + f(&value, changed).map(|r| (r, p.report)) +} + +struct Remembered { + at: At, + written: Payload, + original: Payload, +} +struct Written { + len: usize, + hash: u64, + bytes: usize, + edits: Vec, +} +const MAX_ENTRY: usize = 1 << 20; +const MAX_TOTAL: usize = 8 << 20; +const MAX_ENTRIES: usize = 32; + +/// Process-local identity, never a public hash or a peer's authority. +fn identity(text: &str) -> (usize, u64) { + static KEY: OnceLock = OnceLock::new(); + (text.len(), KEY.get_or_init(RandomState::new).hash_one(text.as_bytes())) +} + +/// The explicit-save cache is independent of the output rollback journal. +fn kept() -> std::sync::MutexGuard<'static, Vec> { + static KEPT: OnceLock>> = OnceLock::new(); + KEPT.get_or_init(Default::default).lock().unwrap_or_else(|e| e.into_inner()) +} + +/// Remembers only credential edits after rollback, with exact written-value equality on reopen. +pub(crate) fn output_remembered( + value: &mut T, + dir: Option<&str>, + f: impl FnOnce(&T) -> Result, +) -> Result<(String, Report), String> { + let p = prepared(value, dir, 0)?; + let mut t = Transaction { live: value, edits: p.edits, applied: 0, defect: false }; + t.apply()?; + let result = f(t.live); + t.rollback(); + if t.defect { + return Err("internal output rollback failed".into()); + } + let text = result?; + let mut report = p.report; + let (len, hash) = identity(&text); + let mut bytes = 0usize; + for e in t.edits.iter().filter(|e| e.kind == Kind::Credential) { + let original = t.live.field_mut(&e.at.owners, &e.at.leaf).ok_or("internal output location changed")?; + let written = match &e.held { + Payload::Text(v) => v.len(), + Payload::Json(v) => json_bytes(v), + }; + bytes = bytes + .saturating_add(original.bytes()) + .saturating_add(written) + .saturating_add(std::mem::size_of::()) + .saturating_add(e.at.owners.len().saturating_mul(std::mem::size_of::())); + } + let mut cache = kept(); + cache.retain(|e| (e.len, e.hash) != (len, hash)); + if bytes > MAX_ENTRY { + report.not_remembered = true; + } else if bytes > 0 { + let mut edits = Vec::new(); + for e in std::mem::take(&mut t.edits).into_iter().filter(|e| e.kind == Kind::Credential) { + let original = t.live.field_mut(&e.at.owners, &e.at.leaf).ok_or("internal output location changed")?.payload(); + edits.push(Remembered { at: e.at, written: e.held, original }); + } + insert(&mut cache, Written { len, hash, bytes, edits }); + } + Ok((text, report)) +} + +/// Oldest explicit saves go first, bounded by both count and total retained payload. +fn insert(cache: &mut Vec, entry: Written) { + while cache.len() >= MAX_ENTRIES || cache.iter().fold(entry.bytes, |n, e| n.saturating_add(e.bytes)) > MAX_TOTAL { + cache.remove(0); + } + cache.push(entry); +} + +/// Runs before normalize/path resolution. Imports and peer ingress never call this function. +pub(crate) fn restore(value: &mut impl Durable, text: &str) { + let id = identity(text); + let cache = kept(); + if let Some(entry) = cache.iter().find(|e| (e.len, e.hash) == id) { + for e in &entry.edits { + if let Some(field) = value.field_mut(&e.at.owners, &e.at.leaf) + && field.equals(&e.written) + { + let mut original = e.original.clone(); + let _ = field.swap(&mut original); + } + } + } +} + +pub(crate) enum Kept { + Unchanged, + Changed(String), + Withheld(&'static str), +} + +/// Prefix recognition precedes parsing: malformed recognized wrappers never become opaque. +pub(crate) fn public_address(address: &str) -> Kept { + public_address_at(address, 0) +} + +/// Wrapper recursion is bounded independently of executable document nesting. +fn public_address_at(address: &str, depth: usize) -> Kept { + use rustgis_data::{ + address::{Public, public_web_address}, + s3_address, + }; + if let Some((public, _)) = rustgis_data::source::myconn::split_password(address) { + return if public == address { Kept::Unchanged } else { Kept::Changed(public) }; + } + if let Some((public, _)) = rustgis_data::source::msconn::split_password(address) { + return if public == address { Kept::Unchanged } else { Kept::Changed(public) }; + } + let text = address.trim_start(); + let lower = text.to_ascii_lowercase(); + let recognized = ["rustgis-anonymous:", "rustgis-s3:", "pmtiles:", "cog:", "postgresql:", "postgres:", "pg:", "https:", "http:", "s3:"] + .into_iter() + .find(|p| lower.starts_with(p)); + let Some(prefix) = recognized else { return Kept::Unchanged }; + if depth >= 8 { + return Kept::Withheld(prefix); + } + if prefix == "rustgis-anonymous:" { + let Some(inner) = text.get(prefix.len()..) else { return Kept::Withheld(prefix) }; + return match public_address_at(inner, depth + 1) { + Kept::Unchanged => Kept::Unchanged, + Kept::Changed(s) => Kept::Changed(format!("{prefix}{s}")), + Kept::Withheld(_) => Kept::Withheld(prefix), + }; + } + if matches!(prefix, "pmtiles:" | "cog:") { + let Some((head, archive)) = crate::sources::template_archive(text) else { return Kept::Withheld(prefix) }; + let file = crate::render::rustgis_tiles::cog::split(archive).0; + let look = archive.get(file.len()..).unwrap_or(""); + return match public_address_at(file, depth + 1) { + Kept::Unchanged => Kept::Unchanged, + Kept::Changed(s) => Kept::Changed(format!("{head}{s}{look}")), + Kept::Withheld(_) => Kept::Withheld(prefix), + }; + } + if prefix == "rustgis-s3:" { + let file = crate::render::rustgis_tiles::cog::split(text).0; + let look = text.get(file.len()..).unwrap_or(""); + let Ok(Some(b)) = s3_address::parse(file) else { return Kept::Withheld(prefix) }; + return match public_address_at(b.url, depth + 1) { + Kept::Unchanged => Kept::Unchanged, + Kept::Changed(s) => match s3_address::bind(b.binding, b.endpoint, &s) { + Ok(s) => Kept::Changed(format!("{s}{look}")), + Err(_) => Kept::Withheld(prefix), + }, + Kept::Withheld(_) => Kept::Withheld(prefix), + }; + } + if matches!(prefix, "postgresql:" | "postgres:" | "pg:") { + return match rustgis_data::source::pgconn::public_label(text) { + Some(s) if s == address => Kept::Unchanged, + Some(s) => Kept::Changed(s), + None => Kept::Withheld(prefix), + }; + } + match public_web_address(address) { + Public::Unchanged => Kept::Unchanged, + Public::Changed { address, .. } => Kept::Changed(address), + Public::Withheld => Kept::Withheld(prefix), + } +} + +/// Emits the report only after rollback, when the session is whole again. +pub(crate) fn notify(s: &mut crate::Session, report: &Report) { + for text in report.notices() { + s.notify(text, false); + } +} + +impl Durable for Vec { + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut Prepare<'_>) { + for (i, v) in self.iter().enumerate() { + p.child(Owner::History(i), v); + } + } + + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[Owner], leaf: &Leaf) -> Option> { + let (Owner::History(i), rest) = owners.split_first()? else { return None }; + self.get_mut(*i)?.field_mut(rest, leaf) + } +} + +impl Durable for rustgis_map::Map { + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut Prepare<'_>) { + for s in self.slots() { + p.string(Leaf::Map(s.at), s.value, s.address, s.local_path); + } + } + + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[Owner], leaf: &Leaf) -> Option> { + if !owners.is_empty() { + return None; + } + let Leaf::Map(at) = leaf else { return None }; + self.slot_mut(at).map(FieldMut::Text) + } +} + +impl Durable for rustgis_map::Layer { + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut Prepare<'_>) { + for (role, value, address, path) in self.output_slots() { + p.string(Leaf::Layer(role), value, address, path); + } + } + + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[Owner], leaf: &Leaf) -> Option> { + if !owners.is_empty() { + return None; + } + let Leaf::Layer(at) = leaf else { return None }; + self.output_slot_mut(*at).map(FieldMut::Text) + } +} + +impl Durable for rustgis_map::presentation::Presentation { + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut Prepare<'_>) { + use rustgis_map::presentation::{Page, PageKind}; + let Self { name: _, width: _, height: _, pages } = self; + for (i, Page { title: _, kind, notes: _, seconds: _ }) in pages.iter().enumerate() { + match kind { + PageKind::Video { source, poster: _, caption: _ } => p.string(Leaf::Video(i), source, true, false), + PageKind::Map { map: _, center: _, width_m: _ } + | PageKind::Blank { text: _, background: _ } + | PageKind::Image { image: _, caption: _ } => {} + } + } + } + + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[Owner], leaf: &Leaf) -> Option> { + if !owners.is_empty() { + return None; + } + let Leaf::Video(i) = leaf else { return None }; + match &mut self.pages.get_mut(*i)?.kind { + rustgis_map::presentation::PageKind::Video { source, .. } => Some(FieldMut::Text(source)), + _ => None, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn restore_memory_has_entry_total_and_count_bounds() { + let marker = "RGSYNTH_1"; + let mut p = crate::Project::default(); + p.tasks.push(crate::project::Task { + name: "large".into(), + description: String::new(), + steps: vec![crate::project::TaskStep { + title: String::new(), + instructions: String::new(), + command: "layer.addUrl".into(), + params: serde_json::json!({"token": marker.repeat(MAX_ENTRY / marker.len() + 1)}), + }], + }); + let (text, report) = p.saved_text(None).unwrap(); + assert!(report.not_remembered && report.notices().iter().any(|n| n.contains("memory limit"))); + assert!(crate::Project::from_json(&text).unwrap().tasks[0].steps[0].params["token"].as_str().unwrap().is_empty()); + let mut cache = Vec::new(); + for hash in 0..40 { + insert(&mut cache, Written { len: 0, hash, bytes: MAX_ENTRY, edits: Vec::new() }); + } + assert_eq!(cache.len(), MAX_TOTAL / MAX_ENTRY); + assert_eq!(cache.first().unwrap().hash, 32); + let mut cache = Vec::new(); + for hash in 0..40 { + insert(&mut cache, Written { len: 0, hash, bytes: 1, edits: Vec::new() }); + } + assert_eq!(cache.len(), MAX_ENTRIES); + assert_eq!(cache.first().unwrap().hash, 8); + } +} diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index f26ecc2289..673da4aed8 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -7,12 +7,19 @@ pub mod cmd; pub mod connections; +pub mod durable; pub mod geometry_calc; pub mod model; +pub mod peer; #[cfg(not(target_arch = "wasm32"))] pub mod prefs; pub mod project; +mod roles; +pub use peer::Peer; +mod parameters; pub mod project_log; +pub use parameters::{params_envelope, script_line}; +pub use project_log::{LogCopy, log_params}; pub mod s3_connections; pub mod s3_keys; #[cfg(test)] @@ -432,6 +439,18 @@ pub struct Notice { pub error: bool, } +/// One counted, already-redacted loss, independent of the changes a command can roll back. +struct LossNotice { + template: &'static str, + count: u32, + text: rustgis_data::diagnostics::Redacted, + source: Option<&'static str>, +} + +/// At most 256 KiB of redacted text per outer command (32 kinds, redaction's 8 KiB bound). +/// Further losses are counted in one overflow notice rather than silently discarded. +const MAX_LOSS_NOTICE_KINDS: usize = 32; + const UNDO_LIMIT: usize = 40; struct ProjectFiles { @@ -551,6 +570,8 @@ pub struct Session { /// the command came (the app, an agent, a script): the interface drops what it kept for the /// old one. pub opened: u64, + /// Session-local last bridge exchange; absent from project files and undo snapshots. + pub(crate) exchange: Option, pub tool: Tool, /// The layer selected in the Layers pane (the one the Style and Data tabs act on). pub active_layer: Option, @@ -567,6 +588,10 @@ pub struct Session { /// camera then: moved since, the map stays. pub style_view: Option<(String, Camera)>, pub notices: Vec, + /// Counted losses await the outermost guard's cleanup, including failed nested commands. + /// Runtime-only: never in Project, undo or PutAside, so replacement cannot erase them. + loss_notices: Vec, + loss_notice_overflow: u32, /// Troubleshooting history, independent of the active pane, project files and undo. pub diagnostics: rustgis_data::diagnostics::History, /// Notices given in this session, including ones no longer kept (see [`Session::notices_since`]), @@ -876,6 +901,7 @@ impl Session { registry: Registry { commands: cmd::all() }, revision: 1, opened: 0, + exchange: None, tool: Tool::Explore, active_layer: None, layer_selection: Vec::new(), @@ -883,6 +909,8 @@ impl Session { view_size: (1200.0, 720.0), style_view: None, notices: Vec::new(), + loss_notices: Vec::new(), + loss_notice_overflow: 0, diagnostics: rustgis_data::diagnostics::History::default(), notices_given: 0, table_layer: None, @@ -1351,7 +1379,7 @@ impl Session { .skip(history_from) .filter(|record| !record.ok && rustgis_analysis::tool(&record.id).is_some_and(cmd::gp::writes_files)) { - self.project.gp_history.push(record); + self.push_gp_record(record); recorded_failure = true; } if recorded_failure { @@ -1380,6 +1408,11 @@ impl Session { } self.fix_active_layer(); self.revision = self.revision.wrapping_add(1); + // Nested guards leave their losses pending: only after the outermost rollback has + // withdrawn completion notices may these go to ordinary notice consumers. + if self.depth == 0 { + self.flush_loss_notices(); + } res } @@ -1545,8 +1578,61 @@ impl Session { self.notice_redacted(text, if error { Severity::Error } else { Severity::Info }); } + /// A counted loss that remains true after rollback, unlike a completion such as "Deleted + /// 3 features". Only static engine templates with `{n}` and a count enter this channel. + /// Counts saturate and repeated templates aggregate once per outer command. Text is redacted + /// before retention; at most 32 kinds are held, with excess counts reported as one overflow. + /// Outside a guard it is published immediately. A native panic caught by `guarded` follows + /// the same outermost drain as an error; nothing is saved in project or undo state. + pub(crate) fn notify_loss(&mut self, template: &'static str, count: u32) { + use rustgis_data::diagnostics::{Redacted, Severity}; + if count == 0 { + return; + } + if self.depth == 0 { + self.notice_redacted(Redacted::new(&template.replace("{n}", &count.to_string())), Severity::Info); + } else if let Some(loss) = self.loss_notices.iter_mut().find(|loss| loss.template == template) { + loss.count = loss.count.saturating_add(count); + loss.text = Redacted::new(&template.replace("{n}", &loss.count.to_string())); + } else if self.loss_notices.len() < MAX_LOSS_NOTICE_KINDS { + self.loss_notices.push(LossNotice { + template, + count, + text: Redacted::new(&template.replace("{n}", &count.to_string())), + source: self.diagnostic_source, + }); + } else { + self.loss_notice_overflow = self.loss_notice_overflow.saturating_add(count); + } + } + + /// Publishes and empties runtime losses only after outermost success/error/panic cleanup. + /// Stored attribution keeps nested command losses assigned to their original command. + fn flush_loss_notices(&mut self) { + use rustgis_data::diagnostics::Severity; + for loss in std::mem::take(&mut self.loss_notices) { + self.notice_redacted_from(loss.text, Severity::Info, loss.source); + } + let overflow = std::mem::take(&mut self.loss_notice_overflow); + if overflow > 0 { + self.notice(format!("{overflow} further loss(es) could not be described"), Severity::Info); + } + } + + /// Uses current attribution for ordinary notices; delayed losses retain their own source. fn notice_redacted(&mut self, text: rustgis_data::diagnostics::Redacted, severity: rustgis_data::diagnostics::Severity) { - self.diagnostics.push_redacted(severity, self.diagnostic_source.unwrap_or("Application"), &text); + self.notice_redacted_from(text, severity, self.diagnostic_source); + } + + /// The one publication path for the ordinary notice queue and diagnostics; called once per + /// loss at drain, so neither sink sees a duplicate from queuing and publication. + fn notice_redacted_from( + &mut self, + text: rustgis_data::diagnostics::Redacted, + severity: rustgis_data::diagnostics::Severity, + source: Option<&'static str>, + ) { + self.diagnostics.push_redacted(severity, source.unwrap_or("Application"), &text); self.notices.push(Notice { text: text.into_string(), error: severity == rustgis_data::diagnostics::Severity::Error }); self.notices_given += 1; if self.notices.len() > 200 { @@ -1962,3 +2048,40 @@ impl Session { self.project.name.clone() } } + +impl durable::Durable for GpRecord { + fn prepare(&self, p: &mut durable::Prepare<'_>) { + use durable::Leaf; + let Self { tool, id, params, command, at: _, ok: _, messages, millis: _, outputs: _ } = self; + let tool = if id.is_empty() { cmd::gp::tool_id(tool).unwrap_or("") } else { id }; + p.tool(Leaf::HistoryParams, tool, params); + if let Some((id, params)) = command { + p.command(Leaf::HistoryCommand, id, params); + } + for (i, message) in messages.iter().enumerate() { + p.generated(Leaf::Message(i), message); + } + } + fn field_mut(&mut self, owners: &[durable::Owner], leaf: &durable::Leaf) -> Option> { + use durable::{FieldMut, Leaf}; + if !owners.is_empty() { + return None; + } + match leaf { + Leaf::HistoryParams => Some(FieldMut::Json(&mut self.params)), + Leaf::HistoryCommand => Some(FieldMut::Json(&mut self.command.as_mut()?.1)), + Leaf::Message(i) => self.messages.get_mut(*i).map(FieldMut::Text), + _ => None, + } + } +} + +impl Session { + /// The single history production seam: retained generated messages never keep address keys. + pub(crate) fn push_gp_record(&mut self, mut record: GpRecord) { + for message in &mut record.messages { + *message = rustgis_data::address::scrub_addresses(message).0.into_owned(); + } + self.project.gp_history.push(record); + } +} diff --git a/crates/engine/src/parameters.rs b/crates/engine/src/parameters.rs new file mode 100644 index 0000000000..410aff8c50 --- /dev/null +++ b/crates/engine/src/parameters.rs @@ -0,0 +1,141 @@ +//! Authoritative adapter envelope and script parsing. Redacted log/display copies never enter +//! this module: callers retain the returned private value for planning and execution. + +use serde_json::{Value, json}; + +/// The parameters an adapter executes: an object or JSON text of one. Missing, null and blank +/// mean `{}`; other input fails, so a malformed request cannot act with unintended defaults. +pub fn params_envelope(params: Option<&Value>) -> Result { + match params { + None | Some(Value::Null) => Ok(json!({})), + Some(v @ Value::Object(_)) => Ok(v.clone()), + Some(Value::String(t)) if t.trim().is_empty() => Ok(json!({})), + Some(Value::String(t)) => match serde_json::from_str::(t) { + Ok(v @ Value::Object(_)) => Ok(v), + Ok(_) => Err("params must be a JSON object".into()), + Err(e) => Err(format!("params is not valid JSON: {e}")), + }, + Some(_) => Err("params must be a JSON object".into()), + } +} + +/// One executable script line, preserving the executor's `id[=] {json}` grammar. Unlike an +/// adapter envelope, a script's JSON value is passed as written; no parameters means null. +/// Blank and comment lines have no command. Errors are annotated with line numbers by callers. +pub fn script_line(line: &str) -> Option> { + let line = line.trim(); + if line.is_empty() || line.starts_with('#') { + return None; + } + let (id, rest) = line.split_once(char::is_whitespace).unwrap_or((line, "")); + let id = id.trim_end_matches('='); + let params = if rest.trim().is_empty() { + Value::Null + } else { + match serde_json::from_str(rest.trim()) { + Ok(value) => value, + Err(e) => return Some(Err(e.to_string())), + } + }; + if crate::cmd::workflows::refused_in_script(id) { + return Some(Err(format!("'{id}' can't run in a script"))); + } + Some(Ok((id.to_string(), params))) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn envelopes_and_script_lines_keep_the_executor_grammar() { + let object = json!({"layer": "private source", "token": "RGSYNTH_params_20261103"}); + assert_eq!(params_envelope(Some(&object)), params_envelope(Some(&json!(object.to_string())))); + for value in [json!(42), json!([]), json!("[1]"), json!("false"), json!("{bad")] { + assert!(params_envelope(Some(&value)).is_err(), "{value}"); + } + for value in [Value::Null, json!(" \t")] { + assert_eq!(params_envelope(Some(&value)).unwrap(), json!({})); + } + assert_eq!(params_envelope(None).unwrap(), json!({})); + assert!(script_line(" # comment").is_none()); + assert_eq!(script_line(" map.zoomIn= {} ").unwrap().unwrap(), ("map.zoomIn".into(), json!({}))); + assert_eq!(script_line("map.zoomIn").unwrap().unwrap().1, Value::Null); + assert_eq!(script_line("map.zoomIn []").unwrap().unwrap().1, json!([])); + assert!(script_line("map.zoomIn {bad").unwrap().is_err()); + assert!(script_line("script.run {}").unwrap().is_err()); + } + + /// Supplied documents and an explicitly supplied final template keep this command test + /// network-free while exercising the real Session notice boundary. + #[test] + fn service_discovery_reports_withheld_destinations_and_clean_input_is_silent() { + for (path, count) in [("tiles", 0), ("%252e/tiles", 1)] { + let href = format!("https://h.invalid/svc/{path}?token=RGSYNTH_notice_20261103"); + let document = json!({"title": "roads", "links": [{"rel": "tilesets-map", "href": href}]}).to_string(); + let mut params = json!({"url": "https://h.invalid/svc", "type": "ogcapi", "text": document}); + let mut session = crate::Session::new(crate::Project::default()); + session.run("service.layers", ¶ms).unwrap(); + let notices = std::mem::take(&mut session.notices); + assert_eq!(notices.len(), count); + assert!(notices.iter().all(|n| n.text.contains("1 address(es)") && !n.text.contains("RGSYNTH_"))); + params["layer"] = json!("map"); + params["tiles"] = json!("https://h.invalid/explicit/{z}/{y}/{x}"); + session.run("service.add", ¶ms).unwrap(); + let notices = std::mem::take(&mut session.notices); + assert_eq!(notices.len(), count); + assert!(notices.iter().all(|n| n.text.contains("1 address(es)") && !n.error)); + // All fail after discovery counted its withheld destinations. The forwarding notice + // is independent of the unchanged command error, and is emitted once on either path. + for (changes, expected) in [ + (json!({"time": {}}), "roads (map tiles): time is a date, \"slider\" or \"default\""), + (json!({"tiles": "not-a-template"}), "not-a-template: not a tile template"), + (json!({"layer": "missing"}), "https://h.invalid/svc: no layer 'missing' (service.layers lists them)"), + ] { + let mut bad = params.clone(); + for (key, value) in changes.as_object().unwrap() { + bad[key] = value.clone(); + } + assert_eq!(session.run("service.add", &bad).unwrap_err(), expected); + let notices = std::mem::take(&mut session.notices); + let forwarding = notices.iter().filter(|n| !n.error && n.text.contains("address(es) the service listed")).count(); + assert_eq!(forwarding, count); + assert_eq!(notices.iter().filter(|n| n.error).count(), 1, "the original error is still reported"); + assert_eq!(notices.len(), count + 1, "one aggregated forwarding notice, clean errors silent about forwarding"); + } + } + } + + /// Nested success and a later caught panic must retain one counted loss, while the + /// completion that was rolled back disappears. The next guard must publish nothing stale. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn nested_loss_notices_drain_once_after_panic_cleanup() { + let mut session = crate::Session::new(crate::Project::default()); + let name = session.project.name.clone(); + let mark = session.notice_mark(); + const LOSS: &str = "{n} resource(s) were omitted at https://h.invalid/x?token=RGSYNTH_2"; + let result = session.guarded("Loss rollback", true, |s| { + s.guarded("Nested loss", true, |s| { + s.notify_loss(LOSS, 1); + Ok(Value::Null) + })?; + assert!(s.notices_since(mark).is_empty(), "the outer guard has not finished"); + s.notify_loss(LOSS, 2); + s.project.name = "temporary".into(); + s.notify("Deleted 3 features", false); + panic!("synthetic loss rollback"); + }); + assert_eq!(result.unwrap_err(), "Loss rollback failed unexpectedly; the project was restored"); + assert_eq!(session.project.name, name); + let notices = session.notices_since(mark); + assert_eq!(notices.iter().filter(|n| !n.error).count(), 1); + assert!(notices.iter().any(|n| !n.error && n.text.starts_with("3 resource(s)"))); + assert!(notices.iter().all(|n| !n.text.contains("RGSYNTH_") && n.text != "Deleted 3 features")); + let diagnostic_losses = session.diagnostics.records().into_iter().filter(|r| r.message.starts_with("3 resource(s)")).count(); + assert_eq!(diagnostic_losses, 1); + let mark = session.notice_mark(); + session.guarded("Next command", false, |_| Ok(Value::Null)).unwrap(); + assert!(session.notices_since(mark).is_empty()); + } +} diff --git a/crates/engine/src/peer.rs b/crates/engine/src/peer.rs new file mode 100644 index 0000000000..89bf1a6f57 --- /dev/null +++ b/crates/engine/src/peer.rs @@ -0,0 +1,300 @@ +//! Project exchange with a peer is an import, never an explicit saved-file reopen. A framing +//! page receives no credentials and cannot borrow the process's identical-text restore memory. + +use crate::{ + Project, Session, + durable::{self, Report}, +}; +use durable::{At, Durable, FieldMut, Kept, Leaf, Owner}; +use std::collections::{HashMap, HashSet}; + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +enum SlotKey { + Map { index: usize, name: String, at: rustgis_map::SlotAt }, + Area { index: usize, at: rustgis_map::SlotAt }, + Table(u32), + Video { presentation: usize, page: usize }, +} + +struct Slot<'a> { + key: SlotKey, + at: At, + value: &'a str, +} +struct Entry { + key: SlotKey, + public: String, + full: String, + ambiguous: bool, +} +pub(crate) struct Exchange { + generation: u64, + entries: Vec, +} +const MAX_ENTRIES: usize = 4096; +const MAX_BYTES: usize = 1 << 20; + +/// Enumerates slot identities before any normalization. Duplicate identities are retained in the +/// census: they must never be made unique by dropping or renumbering a hostile duplicate. +fn slots(p: &Project) -> Vec> { + let mut out = Vec::new(); + for (i, m) in p.maps.iter().enumerate() { + for slot in m.slots().into_iter().filter(|s| s.address) { + if let Some(at) = m.slot_identity(&slot.at) { + out.push(Slot { + key: SlotKey::Map { index: i, name: m.name.clone(), at }, + at: At { owners: vec![Owner::Map(i)], leaf: Leaf::Map(slot.at) }, + value: slot.value, + }); + } + } + } + for (i, area) in p.offline_areas.iter().enumerate() { + for slot in area.data.slots().into_iter().filter(|s| s.address) { + if let Some(at) = area.data.slot_identity(&slot.at) { + out.push(Slot { + key: SlotKey::Area { index: i, at }, + at: At { owners: vec![Owner::Area(i)], leaf: Leaf::Map(slot.at) }, + value: slot.value, + }); + } + } + } + for (i, table) in p.tables.iter().enumerate() { + if let Some(value) = &table.source { + out.push(Slot { key: SlotKey::Table(table.id), at: At { owners: vec![Owner::Table(i)], leaf: Leaf::Table }, value }); + } + } + for (i, pres) in p.presentations.iter().enumerate() { + for (page, p) in pres.pages.iter().enumerate() { + if let rustgis_map::presentation::PageKind::Video { source, .. } = &p.kind { + out.push(Slot { + key: SlotKey::Video { presentation: i, page }, + at: At { owners: vec![Owner::Presentation(i)], leaf: Leaf::Video(page) }, + value: source, + }); + } + } + } + out +} + +/// A wrapper's binding belongs to S3 connection resolution, never exchange retention. +fn wrapped(value: &str) -> bool { + let value = crate::sources::template_archive(value).map_or(value, |(_, v)| v); + value.trim_start().get(..11).is_some_and(|v| v.eq_ignore_ascii_case("rustgis-s3:")) +} + +/// Charges both forms and the complete identity payload, bounded before copying either form. +fn key_bytes(key: &SlotKey) -> usize { + let extra = match key { + SlotKey::Map { name, at, .. } => name.len().saturating_add(match at { + rustgis_map::SlotAt::Tile { id, .. } => id.len(), + _ => 0, + }), + SlotKey::Area { at: rustgis_map::SlotAt::Tile { id, .. }, .. } => id.len(), + _ => 0, + }; + std::mem::size_of::().saturating_add(extra) +} + +impl Exchange { + /// Recorded only after output rollback. Every entry is session-local and replaced per push. + fn record(p: &Project, generation: u64) -> (Self, u32) { + let mut entries: Vec = Vec::new(); + let mut recording = true; + let mut held = 0usize; + let mut omitted = 0u32; + for slot in slots(p) { + if wrapped(slot.value) { + continue; + } + let public = match durable::public_address(slot.value) { + Kept::Unchanged => continue, + Kept::Changed(s) => s, + Kept::Withheld(prefix) => prefix.into(), + }; + // Even an account omitted by the cap cannot make an earlier recorded public form + // unambiguous. After the first overflow, no later slot is recorded. + for e in entries.iter_mut().filter(|e| e.public == public) { + e.ambiguous |= e.full != slot.value; + } + let ambiguous = entries.iter().any(|e| e.public == public && e.ambiguous); + let bytes = slot.value.len().saturating_add(public.len()).saturating_add(key_bytes(&slot.key)); + if !recording || entries.len() >= MAX_ENTRIES || held.saturating_add(bytes) > MAX_BYTES { + recording = false; + omitted = omitted.saturating_add(1); + continue; + } + held += bytes; + entries.push(Entry { key: slot.key, public, full: slot.value.into(), ambiguous }); + } + (Self { generation, entries }, omitted) + } + + /// The pre-normalization census must contain exactly one byte-equal public slot. + fn incoming(&self, p: &Project) -> HashSet { + let census = census(p); + self.entries + .iter() + .filter(|e| census.get(&e.key).is_some_and(|(n, value)| *n == 1 && *value == e.public)) + .map(|e| e.key.clone()) + .collect() + } + + /// Checks last exchange, both incoming censuses, unique account and the exact current live + /// value in one synchronous replacement. Public URL or incoming id alone grants nothing. + fn retain(&self, live: &Project, incoming: &mut Project, raw: &HashSet) { + let current = census(live); + let normalized = census(incoming); + let mut records: HashMap<&SlotKey, usize> = HashMap::new(); + let mut accounts: HashMap<&str, Option<&str>> = HashMap::new(); + for e in &self.entries { + *records.entry(&e.key).or_default() += 1; + let account = accounts.entry(&e.public).or_insert(Some(&e.full)); + if *account != Some(e.full.as_str()) { + *account = None; + } + } + let edits: Vec<_> = slots(incoming) + .into_iter() + .filter_map(|slot| { + let e = self.entries.iter().find(|e| e.key == slot.key)?; + let unique = |census: &HashMap, expected: &str| { + census.get(&slot.key).is_some_and(|(n, value)| *n == 1 && *value == expected) + }; + (!e.ambiguous + && raw.contains(&slot.key) + && records.get(&slot.key) == Some(&1) + && accounts.get(e.public.as_str()) == Some(&Some(e.full.as_str())) + && unique(¤t, &e.full) + && unique(&normalized, &e.public)) + .then(|| (slot.at, e.full.clone())) + }) + .collect(); + drop(normalized); + for (at, full) in edits { + if let Some(FieldMut::Text(slot)) = incoming.field_mut(&at.owners, &at.leaf) { + *slot = full; + } + reopen(incoming, &at); + } + } +} + +/// A retained address must reopen its own reader, which normalization previously opened with +/// the public form. Only the exact retained owner/role is touched; S3 wrappers are excluded. +fn reopen(p: &mut Project, at: &At) { + let Some(owner) = at.owners.first() else { return }; + let map = match owner { + Owner::Map(i) => p.maps.get_mut(*i), + Owner::Area(i) => p.offline_areas.get_mut(*i).map(|a| &mut a.data), + _ => None, + }; + let Some(map) = map else { return }; + let Leaf::Map(rustgis_map::SlotPos::Layer { layer, role }) = &at.leaf else { return }; + let Some(layer) = map.layers.get_mut(*layer) else { return }; + match role { + rustgis_map::LayerRole::Streamed => { + if let Some(source) = &mut layer.source + && let Err(e) = source.reopen() + { + source.error = Some(rustgis_data::diagnostics::Redacted::new(&e).into_string()); + } + } + rustgis_map::LayerRole::Raster => { + if let Some(raster) = &mut layer.raster + && let Err(e) = raster.reload() + { + raster.error = Some(rustgis_data::diagnostics::Redacted::new(&e).into_string()); + } + } + _ => {} + } +} + +/// Values borrow the project, but identities include all duplicates; no normalization runs here. +fn census(p: &Project) -> HashMap { + let mut out = HashMap::new(); + for slot in slots(p) { + let entry = out.entry(slot.key).or_insert((0usize, slot.value)); + entry.0 = entry.0.saturating_add(1); + } + out +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Peer { + Bridge, + Embedder, +} + +impl Session { + /// Writes a credential-free project for a peer, without evicting explicit-save restoration. + /// Pending downloads require a structural draft; settled projects use the live journal. + pub fn peer_text(&mut self, peer: Peer) -> Result<(String, Report), String> { + let (text, mut report) = if self.project.ensure_imports_complete().is_err() { + let mut draft = self.project.clone(); + draft.omit_pending_imports(); + durable::output_owned(draft, text) + } else { + durable::output(&mut self.project, None, text) + }?; + self.exchange = if peer == Peer::Bridge { + let (record, omitted) = Exchange::record(&self.project, self.opened); + report.not_retained = omitted; + Some(record) + } else { + None + }; + Ok((text, report)) + } + + /// 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 { + 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)); + crate::cmd::connections::clear_active(); + p.normalize(); + let mut report = Report::default(); + + if let (Some(exchange), Some(raw)) = (exchange, raw) { + exchange.retain(&self.project, &mut p, &raw); + } + let incoming = census(&p); + report.lost = slots(&self.project) + .into_iter() + .filter(|slot| { + !matches!(slot.key, SlotKey::Video { .. }) + && !matches!(durable::public_address(slot.value), Kept::Unchanged) + && !incoming.get(&slot.key).is_some_and(|(count, value)| *count == 1 && *value == slot.value) + }) + .count() + .try_into() + .unwrap_or(u32::MAX); + drop(incoming); + crate::cmd::project::reset_to(self, p); + self.path = None; + crate::cmd::connections::resolve_project(self); + crate::cmd::connections::note_waiting(self); + self.revision = self.revision.wrapping_add(1); + if peer == Peer::Bridge { + let (exchange, omitted) = Exchange::record(&self.project, self.opened); + self.exchange = Some(exchange); + report.not_retained = omitted; + } + Ok(report) + } +} + +/// The same opening bound applies to peers; no destination is written on a formatting failure. +fn text(p: &Project) -> Result { + let text = serde_json::to_string(p).map_err(|e| e.to_string())?; + if text.len() as u64 > crate::project::MAX_BYTES { + return Err("the project is too large to exchange".into()); + } + Ok(text) +} diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 80aa45d328..217965ef2c 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -1,9 +1,6 @@ //! The project: maps, standalone tables and geoprocessing history, saved as JSON (`.rgis`). -use std::collections::hash_map::RandomState; -use std::hash::BuildHasher; use std::path::Path; -use std::sync::{Mutex, MutexGuard, OnceLock}; use rustgis_data::{Dataset, Feature, Field, GeometryType}; use rustgis_map::Map; @@ -58,69 +55,6 @@ pub const FORMAT: u32 = 1; /// Largest project file read or saved (512 MiB). pub const MAX_BYTES: u64 = 512 << 20; -/// The address a saved file keeps. Only web addresses can contain layer credentials. A tile -/// template (`pmtiles:…@archive`, `cog:…@archive`) is judged by its archive, and an address read -/// through a saved S3 connection by the address it wraps, the wrapper rebuilt around it (its -/// binding and endpoint are provenance, not secrets); a COG's look after either is kept. -fn saved_address(address: &str) -> Option { - use rustgis_data::s3_address; - if let Some((head, archive)) = crate::sources::template_archive(address) { - let (file, look) = split_look(archive); - return saved_address(file).map(|saved| format!("{head}{saved}{look}")); - } - if s3_address::is_wrapper(address) { - let (file, look) = split_look(address); - // One that doesn't parse stays as it is (and blocked): it names no connection. - let b = s3_address::parse(file).ok().flatten()?; - let inner = saved_address(b.url)?; - return s3_address::bind(b.binding, b.endpoint, &inner).ok().map(|saved| format!("{saved}{look}")); - } - let inner = address.strip_prefix(crate::render::rustgis_tiles::s3::ANONYMOUS).unwrap_or(address); - let (protocol, _) = inner.split_once("://")?; - if !["http", "https", "s3"].iter().any(|s| protocol.eq_ignore_ascii_case(s)) { - return None; - } - let (scheme, mut rest) = address.split_once("://")?; - let end = rest.find(['/', '?', '#']).unwrap_or(rest.len()); - if let Some(at) = rest.get(..end).and_then(|authority| authority.rfind('@')) { - rest = rest.get(at + 1..)?; - } - let rest = rest.split('#').next().unwrap_or(rest); - let mut out = format!("{scheme}://{rest}"); - rustgis_data::source::credentials::redact_query(&mut out); - (out != address).then_some(out) -} - -/// A COG address without its look (`…#bands=…`), and the look as written. -fn split_look(address: &str) -> (&str, &str) { - let file = crate::render::rustgis_tiles::cog::split(address).0; - (file, address.get(file.len()..).unwrap_or("")) -} - -/// Strips layer credentials, keeping originals in the visitor's order for the caller to restore. -/// Packaging calls this without registering the resulting text as a file this process wrote. -pub(crate) fn strip_addresses(addresses: Vec<&mut String>) -> Vec> { - addresses.into_iter().map(|a| saved_address(a).map(|redacted| std::mem::replace(a, redacted))).collect() -} - -struct Written { - len: usize, - hash: u64, - was: Vec>, -} - -const MAX_WRITTEN: usize = 32; - -fn kept() -> MutexGuard<'static, Vec> { - static KEPT: OnceLock>> = OnceLock::new(); - KEPT.get_or_init(Default::default).lock().unwrap_or_else(|e| e.into_inner()) -} - -fn identity(text: &str) -> (usize, u64) { - static KEY: OnceLock = OnceLock::new(); - (text.len(), KEY.get_or_init(RandomState::new).hash_one(text.as_bytes())) -} - #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Project { #[serde(default = "format")] @@ -762,70 +696,8 @@ impl Project { Ok(()) } - /// The file keeps no passwords, keys, tokens or signatures in layer addresses. The open project - /// keeps them. This process remembers the 32 most recent distinct saved snapshots with stripped - /// addresses: opening identical plain project text restores them until that snapshot is evicted - /// or RustGIS closes. Packages and imports never restore them. - /// Refuses text larger than the opening limit, restoring live paths and addresses on failure. - pub fn saved_text(&mut self, dir: Option<&Path>) -> Result<(String, usize), String> { - self.ensure_imports_complete()?; - let was = strip_addresses(self.addresses_mut()); - let n = was.iter().flatten().count(); - let originals: Vec> = if let Some(d) = dir.and_then(Path::to_str) { - self.file_paths_mut() - .into_iter() - .map(|(p, _)| { - let relative = rustgis_map::paths::is_absolute_local(p) - .then(|| rustgis_map::paths::relative_to(d, p)) - .flatten() - // Keep the traversal stable during restoration, even for an unusually deep - // destination whose relative spelling exceeds the local-path size bound. - .filter(|r| rustgis_map::paths::is_local(r)); - relative.map(|r| std::mem::replace(p, r)) - }) - .collect() - } else { - Vec::new() - }; - let text = serde_json::to_string(self).map_err(|e| e.to_string()); - if !originals.is_empty() { - for ((p, _), original) in self.file_paths_mut().into_iter().zip(originals) { - if let Some(original) = original { - *p = original; - } - } - } - // Serialization can fail: put the live addresses back before handling its result. - for (address, original) in self.addresses_mut().into_iter().zip(&was) { - if let Some(original) = original { - *address = original.clone(); - } - } - let text = text?; - if text.len() as u64 > MAX_BYTES { - return Err(format!("the project is too large to save (maximum {} MiB)", MAX_BYTES >> 20)); - } - if n > 0 { - let (len, hash) = identity(&text); - let mut kept = kept(); - kept.retain(|entry| (entry.len, entry.hash) != (len, hash)); - if kept.len() >= MAX_WRITTEN { - kept.remove(0); - } - kept.push(Written { len, hash, was }); - } - Ok((text, n)) - } - - /// The project as an autosaved recovery copy keeps it: its text without passwords, keys, tokens - /// or signatures in layer addresses, from a copy of the project (autosave writes it off the - /// interface thread). Unlike [`Self::saved_text`], local file paths stay as the open project - /// holds them, absolute: the copy lives in the app's recovery folder and is opened from there, - /// not from the project's folder. Nor is it remembered as a file this process wrote, so - /// frequent copies can't push the user's own saves out of the snapshots that restore secrets. - /// Runtime import reservations are omitted while completed work remains recoverable. - /// Text too large to open again is refused. - pub fn recovery_text(mut self) -> Result { + /// Omits runtime-only reservations on an owned recovery or bridge draft. + pub(crate) fn omit_pending_imports(&mut self) { // A recovery copy preserves completed work without persisting runtime-only reservations. for map in self.maps.iter_mut().chain(self.offline_areas.iter_mut().map(|area| &mut area.data)) { let pending: std::collections::HashSet<_> = map @@ -840,28 +712,34 @@ impl Project { } map.tiles.retain(|tile| !tile.id.starts_with("geolibre-pending:")); } - strip_addresses(self.addresses_mut()); - let text = serde_json::to_string(&self).map_err(|e| e.to_string())?; + } + + /// Writes controlled durable fields and relative paths in one guarded transaction. Live + /// fields are restored on every return and native unwind; only explicit saves are remembered. + pub fn saved_text(&mut self, dir: Option<&Path>) -> Result<(String, crate::durable::Report), String> { + self.ensure_imports_complete()?; + crate::durable::output_remembered(self, dir.and_then(Path::to_str), project_text) + } + + /// The project as an autosaved recovery copy keeps it: its text without passwords, keys, tokens + /// or signatures in layer addresses, from a copy of the project (autosave writes it off the + /// interface thread). Unlike [`Self::saved_text`], local file paths stay as the open project + /// holds them, absolute: the copy lives in the app's recovery folder and is opened from there, + /// not from the project's folder. Nor is it remembered as a file this process wrote, so + /// frequent copies can't push the user's own saves out of the snapshots that restore secrets. + /// Runtime import reservations are omitted while completed work remains recoverable. + /// Text too large to open again is refused. + pub fn recovery_text(mut self) -> Result { + self.omit_pending_imports(); + let (text, _) = crate::durable::output_owned(self, project_text)?; if text.len() as u64 > MAX_BYTES { return Err("the project is too large to keep a recovery copy".into()); } Ok(text) } - fn restore_addresses(&mut self, text: &str) { - let id = identity(text); - let kept = kept(); - let Some(entry) = kept.iter().find(|entry| (entry.len, entry.hash) == id) else { return }; - for (address, original) in self.addresses_mut().into_iter().zip(&entry.was) { - if let Some(full) = original - && saved_address(full).as_deref() == Some(address.as_str()) - { - *address = full.clone(); - } - } - } - - pub fn save(&mut self, path: &Path) -> Result { + /// Writes checked durable text atomically and returns counted losses from the guarded output. + pub fn save(&mut self, path: &Path) -> Result { let dir = std::path::absolute(path).ok().and_then(|p| p.parent().map(Path::to_path_buf)); let (text, stripped) = self.saved_text(dir.as_deref())?; rustgis_data::io::write_atomic(path, text.as_bytes()).map_err(|e| format!("could not save {}: {e}", path.display()))?; @@ -870,15 +748,22 @@ impl Project { /// Reads a project from its JSON text (a browser upload). pub fn from_json(text: &str) -> Result { - Self::read_json(text, true, None) + Self::read_json(text, true, None, true) } /// Reads imported JSON (including package contents) without restoring a saved file's secrets. pub(crate) fn from_import_json(text: &str, dir: Option<&Path>) -> Result { - Self::read_json(text, false, dir) + Self::read_json(text, false, dir, true) } - fn read_json(text: &str, restore: bool, dir: Option<&Path>) -> Result { + /// Peer census must precede normalization: duplicate ids cannot become proof of a unique + /// matching slot after the normalizer renumbers or drops them. Saved-text restore is absent. + pub(crate) fn from_peer_raw(text: &str) -> Result { + Self::read_json(text, false, None, false) + } + + /// Checks project size and version; explicit saves may restore exact fields before normalization. + fn read_json(text: &str, restore: bool, dir: Option<&Path>, normalize: bool) -> Result { if text.len() as u64 > MAX_BYTES { return Err("the project file is too large".into()); } @@ -901,7 +786,7 @@ impl Project { return Err(format!("this project was saved by a newer RustGIS (format {}). Update RustGIS to open it.", p.format)); } if restore { - p.restore_addresses(text); + crate::durable::restore(&mut p, text); } // A binding is this computer's runtime token for a login: one in a file is no authority // to use any login here. @@ -909,7 +794,9 @@ impl Project { if let Some(d) = dir.and_then(Path::to_str) { p.resolve_paths(d); } - p.normalize(); + if normalize { + p.normalize(); + } Ok(p) } @@ -919,7 +806,7 @@ impl Project { let bytes = read_file(path, MAX_BYTES)?; let text = String::from_utf8(bytes).map_err(|_| format!("{} isn't a RustGIS project", path.display()))?; let dir = std::path::absolute(path).ok().and_then(|p| p.parent().map(Path::to_path_buf)); - Self::read_json(&text, true, dir.as_deref()) + Self::read_json(&text, true, dir.as_deref(), true) } } @@ -974,3 +861,178 @@ fn unique_names<'a>(names: impl Iterator) { } } } + +// Durable ownership is classified here so private fields cannot disappear from the inventory. +use crate::durable::{Durable, FieldMut, Leaf, Owner, Prepare}; + +impl Durable for Project { + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut Prepare<'_>) { + let Self { + format: _, + name: _, + maps, + active_map: _, + tables, + next_table_id: _, + gp_history, + layouts: _, + folders: _, + styles: _, + ramps: _, + tasks, + gp_env: _, + models, + workspaces: _, + link_views: _, + thumbnail: _, + jobs: _, + offline_areas, + reports: _, + presentations, + notebooks, + dl_models: _, + shared: _, + data_stores: _, + } = self; + for (i, v) in maps.iter().enumerate() { + p.child(Owner::Map(i), v); + } + for (i, v) in offline_areas.iter().enumerate() { + let OfflineArea { name: _, map: _, extent: _, created: _, synced: _, data } = v; + p.child(Owner::Area(i), data); + } + for (i, v) in tables.iter().enumerate() { + p.child(Owner::Table(i), v); + } + for (i, v) in tasks.iter().enumerate() { + p.child(Owner::Task(i), v); + } + for (i, v) in models.iter().enumerate() { + p.child(Owner::Model(i), v); + } + for (i, v) in gp_history.iter().enumerate() { + p.child(Owner::History(i), v); + } + for (i, v) in notebooks.iter().enumerate() { + p.child(Owner::Notebook(i), v); + } + for (i, v) in presentations.iter().enumerate() { + p.child(Owner::Presentation(i), v); + } + } + + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[Owner], leaf: &Leaf) -> Option> { + let (owner, rest) = owners.split_first()?; + match owner { + Owner::Map(i) => self.maps.get_mut(*i)?.field_mut(rest, leaf), + Owner::Area(i) => self.offline_areas.get_mut(*i)?.data.field_mut(rest, leaf), + Owner::Table(i) => self.tables.get_mut(*i)?.field_mut(rest, leaf), + Owner::Task(i) => self.tasks.get_mut(*i)?.field_mut(rest, leaf), + Owner::Model(i) => self.models.get_mut(*i)?.field_mut(rest, leaf), + Owner::History(i) => self.gp_history.get_mut(*i)?.field_mut(rest, leaf), + Owner::Notebook(i) => self.notebooks.get_mut(*i)?.field_mut(rest, leaf), + Owner::Presentation(i) => self.presentations.get_mut(*i)?.field_mut(rest, leaf), + Owner::Graph => None, + } + } +} + +impl Durable for Table { + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut Prepare<'_>) { + let Self { id: _, dataset } = self; + let Dataset { + name: _, + geometry_type: _, + fields: _, + features: _, + source: provenance, + crs: _, + crs_incomplete: _, + zm_dropped: _, + windowed: _, + } = dataset; + if let Some(v) = provenance { + p.string(Leaf::Table, v, true, rustgis_map::paths::is_local(v)); + } + } + + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[Owner], leaf: &Leaf) -> Option> { + if owners.is_empty() && *leaf == Leaf::Table { self.dataset.source.as_mut().map(FieldMut::Text) } else { None } + } +} + +impl Durable for Task { + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut Prepare<'_>) { + let Self { name: _, description: _, steps } = self; + for (i, TaskStep { title: _, instructions: _, command, params }) in steps.iter().enumerate() { + p.command(Leaf::Task(i), command, params); + } + } + + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[Owner], leaf: &Leaf) -> Option> { + if !owners.is_empty() { + return None; + } + let Leaf::Task(i) = leaf else { return None }; + Some(FieldMut::Json(&mut self.steps.get_mut(*i)?.params)) + } +} + +impl Durable for Model { + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut Prepare<'_>) { + let Self { name: _, steps, graph } = self; + for (i, ModelStep { tool, params }) in steps.iter().enumerate() { + p.tool(Leaf::ModelStep(i), tool, params); + } + if let Some(g) = graph { + p.child(Owner::Graph, g); + } + } + + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[Owner], leaf: &Leaf) -> Option> { + if let Some((Owner::Graph, rest)) = owners.split_first() { + return self.graph.as_mut()?.field_mut(rest, leaf); + } + if !owners.is_empty() { + return None; + } + let Leaf::ModelStep(i) = leaf else { return None }; + Some(FieldMut::Json(&mut self.steps.get_mut(*i)?.params)) + } +} + +impl Durable for ModelGraph { + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut Prepare<'_>) { + let Self { nodes, edges: _ } = self; + for (i, ModelNode { id: _, kind: _, x: _, y: _, layer: _, tool, params, name: _ }) in nodes.iter().enumerate() { + p.tool(Leaf::ModelNode(i), tool.as_deref().unwrap_or(""), params); + } + } + + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[Owner], leaf: &Leaf) -> Option> { + if !owners.is_empty() { + return None; + } + let Leaf::ModelNode(i) = leaf else { return None }; + Some(FieldMut::Json(&mut self.nodes.get_mut(*i)?.params)) + } +} + +/// Enforces the opening bound inside the formatter, before any destination is replaced. +fn project_text(p: &Project) -> Result { + let text = serde_json::to_string(p).map_err(|e| e.to_string())?; + if text.len() as u64 > MAX_BYTES { + return Err(format!("the project is too large to save (maximum {} MiB)", MAX_BYTES >> 20)); + } + Ok(text) +} diff --git a/crates/engine/src/project_log.rs b/crates/engine/src/project_log.rs index 12ac74c07a..cec01e8d03 100644 --- a/crates/engine/src/project_log.rs +++ b/crates/engine/src/project_log.rs @@ -6,6 +6,22 @@ use serde_json::Value; use crate::{CmdResult, Session}; +/// A redacted output projection. Never use it as executable parameters or approval identity. +pub struct LogCopy(Value); + +impl LogCopy { + /// Consumes the output-only wrapper for serialization. + pub fn into_log_value(self) -> Value { + self.0 + } +} + +/// The session log's bounded, document-aware parameter policy, shared with the agent audit. +/// The source value is never mutated and remains the only input to execution and planning. +pub fn log_params(params: &Value) -> LogCopy { + LogCopy(kept_params(params)) +} + /// Now, in milliseconds since 1970 (UTC). pub(crate) fn now_ms() -> u64 { web_time::SystemTime::now().duration_since(web_time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0) @@ -126,7 +142,17 @@ fn embedded_redacted(v: &Value, depth: usize) -> Value { Value::String(sql_secrets_removed(&t)) } Value::Array(a) if depth < 8 => Value::Array(a.iter().map(|x| embedded_redacted(x, depth + 1)).collect()), - Value::Object(o) if depth < 8 => Value::Object(o.iter().map(|(k, x)| (k.clone(), embedded_redacted(x, depth + 1))).collect()), + Value::Object(o) if depth < 8 => Value::Object( + o.iter() + .map(|(k, x)| { + // Preserve History's plain-key refinement; the log-only fields share diagnostics' + // canonical list. Clipping already bounds recursion and text before this pass. + let sensitive = + rustgis_data::address::normalized_name(k).as_deref() != Some("key") && rustgis_data::address::log_sensitive(k); + (k.clone(), if sensitive { Value::String(String::new()) } else { embedded_redacted(x, depth + 1) }) + }) + .collect(), + ), other => other.clone(), } } diff --git a/crates/engine/src/roles.rs b/crates/engine/src/roles.rs new file mode 100644 index 0000000000..3a1df9f9f8 --- /dev/null +++ b/crates/engine/src/roles.rs @@ -0,0 +1,374 @@ +//! Replayable parameters are cleaned by role, never by recursively deleting secret-named data +//! properties. Unknown nested content and foreign notebook cells remain opaque, with honest +//! notices. Logs deliberately have a separate, stricter policy. + +use std::collections::HashSet; +use std::sync::OnceLock; + +use serde::{Serialize, de::DeserializeOwned}; +use serde_json::Value; + +use crate::durable::{self, Durable, Kept, Report}; + +const WITHHELD: &str = "[parameters withheld]"; +const NESTED: &str = "[withheld: nested too deep]"; +const DATA: &[&str] = &[ + "text", + "data", + "values", + "value", + "attributes", + "properties", + "features", + "feature", + "geometry", + "rows", + "expression", + "where", + "html", + "markdown", + "caption", + "title", + "notes", + "description", + "label", + "name", + "field", + "fields", +]; +const ADDRESS: &[&str] = + &["url", "urls", "href", "source", "template", "tiles", "store", "endpoint", "address", "uri", "connection", "database"]; +const CONFIG: &[&str] = &["headers", "options", "auth", "connection", "source", "sources"]; + +/// Registered commands are classified once; plugin commands keep their nested content opaque. +fn known(id: &str) -> bool { + static IDS: OnceLock> = OnceLock::new(); + IDS.get_or_init(|| crate::cmd::all().into_iter().map(|c| c.id).collect()).contains(id) +} + +/// A conservative bounded read-only check, used solely to announce unchecked/withheld content. +fn suspicious(v: &Value) -> bool { + crate::sources::holds_secret(v) + || v.as_str().is_some_and(|s| { + s.split(|c: char| !(c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '%'))) + .take(65_536) + .any(|word| !word.is_empty() && rustgis_data::address::secret_key(word)) + }) +} + +/// Keeps invalid executable content invalid while withholding credential-bearing text. +fn invalid(value: &Value, report: &mut Report) -> Option { + if suspicious(value) { + report.withheld = report.withheld.saturating_add(1); + Some(Value::String(WITHHELD.into())) + } else { + None + } +} + +/// Cleans one command without treating parameter JSON strings as executor envelopes. +pub(crate) fn command(id: &str, params: &Value, depth: usize) -> (Option, Report) { + let mut report = Report::default(); + let changed = object(id, params, depth, 0, known(id), &mut report); + (changed, report) +} + +/// Tools use command roles too, including the data-valued `input`/`values` parameters. +pub(crate) fn tool(id: &str, params: &Value, depth: usize) -> (Option, Report) { + let mut report = Report::default(); + let builtin = rustgis_analysis::TOOLS.iter().any(|t| t.id == id); + let changed = object("", params, depth, 0, builtin, &mut report); + (changed, report) +} + +/// Lazily copies only changed owning parameter objects. Configuration recursion is bounded. +fn object(id: &str, value: &Value, depth: usize, config_depth: usize, registered: bool, report: &mut Report) -> Option { + if value.is_null() { + return None; + } + let Some(obj) = value.as_object() else { return invalid(value, report) }; + if config_depth >= 8 { + return invalid(value, report); + } + let mut replacement = None; + let mut unchecked = false; + for (key, v) in obj { + unchecked |= !matches!(durable::public_address(key), Kept::Unchanged); + let k = rustgis_data::address::normalized_name(key).unwrap_or_else(|| key.to_ascii_lowercase()); + let executable = registered + && matches!( + (id, k.as_str()), + ("gp.run", "params") + | ("script.run", "script") + | ("task.new", "steps") + | ("model.new" | "model.save" | "model.run" | "model.validate" | "model.export", "graph") + | ("model.new", "steps") + ); + let document = registered + && k == "text" + && matches!(id, "project.openText" | "map.import" | "task.import" | "notebook.open" | "presentation.import" | "model.import"); + let clean = if executable || document { + if depth >= 4 { + report.withheld = report.withheld.saturating_add(1); + Some(Value::String(NESTED.into())) + } else { + envelope(id, &k, v, obj, depth + 1, report) + } + } else if rustgis_data::address::secret_key(key) + && (k != "key" + || crate::sources::looks_like_a_key(v.as_str().unwrap_or("")) + || obj.iter().any(|(other, value)| other != key && !DATA.contains(&other.as_str()) && address_like(value))) + { + match v { + Value::String(s) if !s.is_empty() => Some(Value::String(String::new())), + Value::Number(_) => Some(Value::String(String::new())), + _ => None, + } + } else if DATA.contains(&k.as_str()) { + None + } else if registered && id.starts_with("sql.") && matches!(k.as_str(), "sql" | "query") { + v.as_str().and_then(|s| { + let clean = crate::project_log::sql_secrets_removed(s); + (clean != s).then_some(Value::String(clean)) + }) + } else if ADDRESS.contains(&k.as_str()) || address_like(v) { + if v.is_object() && registered && CONFIG.contains(&k.as_str()) { + object("", v, depth, config_depth + 1, true, report) + } else { + address(v, report) + } + } else if registered && CONFIG.contains(&k.as_str()) && v.is_object() { + object("", v, depth, config_depth + 1, true, report) + } else { + unchecked |= suspicious(v); + None + }; + if !registered && !DATA.contains(&k.as_str()) && (v.is_object() || v.is_array()) { + unchecked |= suspicious(v); + } + if let Some(clean) = clean.filter(|clean| clean != v) { + replacement.get_or_insert_with(|| obj.clone()).insert(key.clone(), clean); + } + } + if unchecked { + report.unchecked = report.unchecked.saturating_add(1); + } + replacement.map(Value::Object) +} + +/// Recognition precedes parsing, including malformed wrappers and differently cased prefixes. +fn address_like(v: &Value) -> bool { + match v { + Value::String(s) => { + let s = s.trim_start(); + ["http:", "https:", "s3:", "rustgis-anonymous:", "rustgis-s3:", "pmtiles:", "cog:", "postgres:", "postgresql:", "pg:"] + .iter() + .any(|p| s.get(..p.len()).is_some_and(|s| s.eq_ignore_ascii_case(p))) + } + Value::Array(a) => a.iter().any(|v| v.is_string() && address_like(v)), + _ => false, + } +} + +/// Array addresses are string leaves only; data and nested opaque objects are never walked. +fn address(v: &Value, report: &mut Report) -> Option { + match v { + Value::String(s) => match durable::public_address(s) { + Kept::Unchanged => None, + Kept::Changed(s) => Some(Value::String(s)), + Kept::Withheld(prefix) if prefix != s => { + report.withheld = report.withheld.saturating_add(1); + Some(Value::String(prefix.into())) + } + Kept::Withheld(_) => None, + }, + Value::Array(a) => { + let mut changed = None; + for (i, v) in a.iter().enumerate() { + if v.is_string() + && let Some(clean) = address(v, report) + && let Some(slot) = changed.get_or_insert_with(|| a.clone()).get_mut(i) + { + *slot = clean; + } + } + changed.map(Value::Array) + } + _ => None, + } +} + +/// Explicit executable and document roles are the only route into nested executable content. +fn envelope(id: &str, key: &str, v: &Value, siblings: &serde_json::Map, depth: usize, report: &mut Report) -> Option { + match (id, key) { + ("gp.run", "params") => { + let (clean, r) = tool(siblings.get("tool").and_then(Value::as_str).unwrap_or(""), v, depth); + report.merge(&r); + clean + } + ("script.run", "script") => match v.as_str() { + Some(s) => { + let (clean, r) = script(s, depth); + report.merge(&r); + clean.map(Value::String) + } + None => invalid(v, report), + }, + ("task.new", "steps") => typed_value::(v, depth, report), + ("model.new", "steps") => typed_value::(v, depth, report), + (_, "graph") => typed_value::(v, depth, report), + (_, "text") => document(id, v, depth, report), + _ => None, + } +} + +/// A typed envelope is prepared on its own copy, preserving the live outer params as a whole. +fn typed_value(v: &Value, depth: usize, report: &mut Report) -> Option { + let Ok(parsed) = serde_json::from_value::(v.clone()) else { return invalid(v, report) }; + let Ok((clean, r)) = durable::output_owned_changed(parsed, depth, |v, changed| { + if changed { serde_json::to_value(v).map(Some).map_err(|e| e.to_string()) } else { Ok(None) } + }) else { + return invalid(v, report); + }; + report.merge(&r); + clean +} + +#[derive(serde::Serialize, serde::Deserialize)] +#[serde(transparent)] +struct VecTask(Vec); + +impl Durable for VecTask { + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut durable::Prepare<'_>) { + for (i, st) in self.0.iter().enumerate() { + p.command(durable::Leaf::Task(i), &st.command, &st.params); + } + } + + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[durable::Owner], leaf: &durable::Leaf) -> Option> { + if !owners.is_empty() { + return None; + } + let durable::Leaf::Task(i) = leaf else { return None }; + Some(durable::FieldMut::Json(&mut self.0.get_mut(*i)?.params)) + } +} + +#[derive(serde::Serialize, serde::Deserialize)] +#[serde(transparent)] +struct VecSteps(Vec); + +impl Durable for VecSteps { + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut durable::Prepare<'_>) { + for (i, st) in self.0.iter().enumerate() { + p.tool(durable::Leaf::ModelStep(i), &st.tool, &st.params); + } + } + + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[durable::Owner], leaf: &durable::Leaf) -> Option> { + if !owners.is_empty() { + return None; + } + let durable::Leaf::ModelStep(i) = leaf else { return None }; + Some(durable::FieldMut::Json(&mut self.0.get_mut(*i)?.params)) + } +} + +/// Reads documents at the existing reader bound, without normalization, trust or restore-cache +/// access. Unchanged documents keep their exact text; only changed typed copies are serialized. +fn document(id: &str, v: &Value, depth: usize, report: &mut Report) -> Option { + let Some(text) = v.as_str().filter(|s| s.len() as u64 <= crate::project::MAX_BYTES) else { return invalid(v, report) }; + let result = match id { + "project.openText" => text_document::(text, depth), + "map.import" => { + if serde_json::from_str::(text).is_ok() { + text_document::(text, depth) + } else { + text_document::(text, depth) + } + } + "task.import" => text_document::(text, depth), + "presentation.import" => text_document::(text, depth), + "notebook.open" => crate::cmd::notebook::durable_document(text, depth), + "model.import" => model_document(text, depth), + _ => return None, + }; + match result { + Ok((clean, r)) => { + let changed = clean != text; + report.merge(&r); + changed.then_some(Value::String(clean)) + } + Err(_) => invalid(v, report), + } +} + +/// Uses the owning type's ordinary deserializer, without losing unchanged text representation. +fn text_document(text: &str, depth: usize) -> Result<(String, Report), String> { + let parsed = serde_json::from_str::(text).map_err(|e| e.to_string())?; + durable::output_owned_changed(parsed, depth, |value, changed| { + if changed { serde_json::to_string(value).map_err(|e| e.to_string()) } else { Ok(text.into()) } + }) +} + +/// Model import has two envelopes; keeps the envelope's other fields and cleans only its model. +fn model_document(text: &str, depth: usize) -> Result<(String, Report), String> { + let mut v: Value = serde_json::from_str(text).map_err(|e| e.to_string())?; + let mut report = Report::default(); + let Some(obj) = v.as_object_mut() else { return Err("not a model".into()) }; + let (key, graph) = if obj.contains_key("graph") { ("graph", true) } else { ("steps", false) }; + let Some(field) = obj.get(key) else { return Err("not a model".into()) }; + let clean = if graph { + typed_value::(field, depth, &mut report) + } else { + typed_value::(field, depth, &mut report) + }; + if let Some(clean) = clean { + obj.insert(key.into(), clean); + Ok((v.to_string(), report)) + } else { + Ok((text.into(), report)) + } +} + +/// Uses the executor's line parser, preserving comments, blank lines and unchanged formatting. +pub(crate) fn script(source: &str, depth: usize) -> (Option, Report) { + let mut report = Report::default(); + let mut out = None; + let mut offset = 0usize; + for line in source.split_inclusive('\n') { + let ending = if line.ends_with("\r\n") { + "\r\n" + } else if line.ends_with('\n') { + "\n" + } else { + "" + }; + let replacement = match crate::script_line(line) { + None => None, + Some(Ok((id, params))) => { + let (clean, r) = command(&id, ¶ms, depth); + report.merge(&r); + clean.map(|params| format!("{id} {params}{ending}")) + } + Some(Err(_)) if suspicious(&Value::String(line.into())) => { + report.withheld = report.withheld.saturating_add(1); + let id = line.trim_start().split(char::is_whitespace).next().unwrap_or(""); + Some(format!("{id} {WITHHELD}{ending}")) + } + Some(Err(_)) => None, + }; + if let Some(clean) = replacement { + report.lines = report.lines.saturating_add(1); + out.get_or_insert_with(|| source.get(..offset).unwrap_or("").to_string()).push_str(&clean); + } else if let Some(out) = &mut out { + out.push_str(line); + } + offset += line.len(); + } + (out, report) +} diff --git a/crates/engine/src/sources.rs b/crates/engine/src/sources.rs index 998d21d5f9..9711096164 100644 --- a/crates/engine/src/sources.rs +++ b/crates/engine/src/sources.rs @@ -160,7 +160,13 @@ pub fn clean(mut s: Source) -> Result { if !KINDS.contains(&s.kind.as_str()) { return Err(format!("unknown kind '{}' ({})", s.kind, KINDS.join(", "))); } - s.address = redact(s.address.trim()); + let address = s.address.trim(); + // A display-only prefix is safe to show, but cannot be used as a saved source. + s.address = match crate::durable::public_address(address) { + crate::durable::Kept::Withheld(_) => return Err("a source needs an address (a path, web address or connection)".into()), + crate::durable::Kept::Changed(public) => public, + crate::durable::Kept::Unchanged => redact(address), + }; // Provenance has its own structural bound; it must not consume the destination's limit. let (head, archive) = template_archive(&s.address).unwrap_or(("", s.address.as_str())); let bounded = match rustgis_data::s3_address::parse(archive) { @@ -344,15 +350,20 @@ pub(crate) fn template_archive(address: &str) -> Option<(&str, &str)> { /// saved S3 connection is redacted inside (its inner address), and its length-prefixed form /// rebuilt; a tile template, in its archive. pub fn redact(address: &str) -> String { - if let Some((head, archive)) = template_archive(address) - && rustgis_data::s3_address::is_wrapper(archive) - { - return format!("{head}{}", redact(archive)); - } - if let Ok(Some(b)) = rustgis_data::s3_address::parse(address) - && let Ok(rebuilt) = rustgis_data::s3_address::bind(b.binding, b.endpoint, &redact(b.url)) + match crate::durable::public_address(address) { + crate::durable::Kept::Changed(s) => return s, + crate::durable::Kept::Withheld(prefix) => return prefix.into(), + crate::durable::Kept::Unchanged => {} + } + // A clean recognized wrapper or database label already passed the shared owner. Keep its + // layout, including a COG look; the generic scheme fallback is for older connection forms. + if template_archive(address).is_some() + || rustgis_data::s3_address::is_wrapper(address) + || ["rustgis-anonymous:", "postgres:", "postgresql:", "pg:"] + .iter() + .any(|p| address.trim_start().get(..p.len()).is_some_and(|s| s.eq_ignore_ascii_case(p))) { - return rebuilt; + return address.to_string(); } if let Some((public, _)) = rustgis_data::source::myconn::split_password(address) { return public; @@ -360,13 +371,6 @@ pub fn redact(address: &str) -> String { if let Some((public, _)) = rustgis_data::source::msconn::split_password(address) { return public; } - let lower = address.to_ascii_lowercase(); - // A PostgreSQL connection (`postgres:` or `postgresql:` keyword strings, GDAL's `PG:`, URIs): - // libpq's own rules (quoted values, escapes, `key = value`), without its secrets. - if ["postgres:", "postgresql:", "pg:"].iter().any(|p| lower.starts_with(p)) { - // One that can't be taken apart isn't kept (an empty address, refused by `clean`). - return rustgis_data::source::pgconn::public_label(address).unwrap_or_default(); - } let Some((scheme, rest)) = address.split_once("://") else { return address.to_string() }; // The user and password go first, before anything splits the address: a password may hold // an unencoded `/`, `?`, `#` or `@`. They end at the last `@` before the first `/`, or (a `/` @@ -746,9 +750,15 @@ MIIE for f in ["#Access_Token=t", "#sig=s", "#password=p", "#id_token=i", "#map=4/1/2"] { assert_eq!(redact(&format!("https://h.example/x.fgb{f}")), "https://h.example/x.fgb", "{f}"); } - // A connection with nothing to take apart isn't kept. - for bare in ["postgres:dbname", "PG:", "postgres:password='x"] { - assert!(clean(Source { kind: "database".into(), address: bare.into(), ..Source::default() }).is_err(), "{bare}"); + // Unreadable connections, web addresses and wrappers cannot become saved sources. + for (kind, bare) in [ + ("database", "postgres:dbname"), + ("database", "PG:"), + ("database", "postgres:password='x"), + ("url", "https:///path?token=RGSYNTH_unusable_source_20261104"), + ("tiles", "pmtiles:unreadable"), + ] { + assert!(clean(Source { kind: kind.into(), address: bare.into(), ..Source::default() }).is_err(), "{bare}"); } assert_eq!( redact("https://acct.blob.core.windows.net/c/a.tif?se=2026&sp=r&sv=1&sig=zzz"), diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 1d3f4e273a..e897563eb7 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -5,6 +5,9 @@ use crate::{Project, Session, sample}; #[path = "tests/task_reentry.rs"] mod task_reentry; +#[path = "tests/credential_output.rs"] +mod credential_output; + #[cfg(not(target_arch = "wasm32"))] #[path = "tests/remembered_database.rs"] mod remembered_database; @@ -9145,7 +9148,7 @@ fn jupyter_python_notebooks_survive_open_and_save() { let nb = s.project.notebooks.last().unwrap(); let _ = (nb.language_label(), nb.cells.iter().map(crate::cmd::notebook::Cell::images).count()); let i = s.project.notebooks.len() - 1; - assert!(crate::cmd::notebook::notebook_file(&s, i, true).is_ok(), "{junk}"); + assert!(crate::cmd::notebook::notebook_file(&mut s, i, true).is_ok(), "{junk}"); s.run("notebook.remove", &json!({"notebook": i})).unwrap(); } } @@ -9156,7 +9159,7 @@ fn jupyter_python_notebooks_survive_open_and_save() { s.run("notebook.open", &json!({"text": cmds.to_string()})).unwrap(); s.run("notebook.run", &json!({"cell": 0})).unwrap(); let i = s.project.notebooks.len() - 1; - let saved: Value = serde_json::from_slice(&crate::cmd::notebook::notebook_file(&s, i, true).unwrap()).unwrap(); + let saved: Value = serde_json::from_slice(&crate::cmd::notebook::notebook_file(&mut s, i, true).unwrap()).unwrap(); let cell = &saved["cells"][0]; assert_eq!(cell["id"], "go"); assert_eq!(cell["execution_count"], Value::Null); @@ -11311,35 +11314,31 @@ fn adding_a_collection_of_3d_tiles_says_why_it_is_left_out() { fn an_arcgis_token_in_a_service_address_reaches_its_layers() { use crate::render::rustgis_tiles::arcgis_auth; let mut s = Session::new(Project::default()); - let base = "https://secure914.example.org/server/rest/services/Roads/MapServer"; + let base = "https://secure914.invalid/server/rest/services/Roads/MapServer"; let doc = r#"{"currentVersion": 11, "mapName": "Roads", "capabilities": "Map,Query,Data", "singleFusedMapCache": true, "tileInfo": {"rows": 256, "spatialReference": {"wkid": 102100}, "lods": [{"level": 0, "resolution": 156543.033928041}]}, "layers": [{"id": 0, "name": "Highways"}]}"#; - let v = s.run("service.layers", &json!({"url": format!("{base}?f=json&token=secret.Token-914"), "text": doc})).unwrap(); - assert!(!v.to_string().contains("secret.Token-914"), "{v}"); + let v = s.run("service.layers", &json!({"url": format!("{base}?f=json&%2574oken=RGSYNTH_3"), "text": doc})).unwrap(); + assert!(!v.to_string().contains("RGSYNTH_3"), "{v}"); let tiles = format!("{base}/tile/0/0/0"); let query = format!("{base}/0/query?where=1%3D1&f=geojson"); - assert_eq!(arcgis_auth::authorization(&tiles).as_deref(), Some("Bearer secret.Token-914")); - assert_eq!(arcgis_auth::authorization(&query).as_deref(), Some("Bearer secret.Token-914")); + assert_eq!(arcgis_auth::authorization(&tiles).as_deref(), Some("Bearer RGSYNTH_3")); + assert_eq!(arcgis_auth::authorization(&query).as_deref(), Some("Bearer RGSYNTH_3")); // Another server never gets it. - assert_eq!(arcgis_auth::authorization("https://other914.example.org/server/rest/services/Roads/MapServer/tile/0/0/0"), None); + assert_eq!(arcgis_auth::authorization("https://other914.invalid/server/rest/services/Roads/MapServer/tile/0/0/0"), None); // Added, the tile layer keeps the address without the token. let layer = v["layers"].as_array().unwrap().iter().find(|l| l["offer"]["type"] == "tiles").unwrap()["name"].as_str().unwrap().to_string(); - s.run("service.add", &json!({"url": format!("{base}?token=secret.Token-914"), "layer": layer, "text": doc})).unwrap(); + s.run("service.add", &json!({"url": format!("{base}?token=RGSYNTH_3"), "layer": layer, "text": doc})).unwrap(); let project = format!("{:?}", s.project); - assert!(project.contains("secure914.example.org") && !project.contains("secret.Token-914")); + assert!(project.contains("secure914.invalid") && !project.contains("RGSYNTH_3")); // Listed and forgotten by arcgis.status and arcgis.signOut. let status = s.run("arcgis.status", &json!({})).unwrap(); assert!( - status["sessions"] - .as_array() - .unwrap() - .iter() - .any(|x| x["portal"] == "https://secure914.example.org/server" && x["kind"] == "token") + status["sessions"].as_array().unwrap().iter().any(|x| x["portal"] == "https://secure914.invalid/server" && x["kind"] == "token") ); - assert!(!status.to_string().contains("secret.Token-914")); - s.run("arcgis.signOut", &json!({"portal": "https://secure914.example.org/server/"})).unwrap(); + assert!(!status.to_string().contains("RGSYNTH_3")); + s.run("arcgis.signOut", &json!({"portal": "https://secure914.invalid/server/"})).unwrap(); assert_eq!(arcgis_auth::authorization(&tiles), None); // Junk is refused rather than taken for "every session". for junk in [json!({"portal": 7}), json!({"portal": [true]}), json!({"portal": {"a": 1}})] { @@ -15896,14 +15895,20 @@ fn layer_address_redaction_preserves_templates_and_local_paths() { ("upload:map?token=local", "upload:map?token=local"), ("postgres:host=localhost dbname=gis", "postgres:host=localhost dbname=gis"), ]; - let mut addresses: Vec = cases.iter().map(|(a, _)| a.to_string()).collect(); - crate::project::strip_addresses(addresses.iter_mut().collect()); - for (address, (_, expected)) in addresses.iter().zip(cases) { + for (original, expected) in cases { + let address = match crate::durable::public_address(original) { + crate::durable::Kept::Unchanged => original.to_owned(), + crate::durable::Kept::Changed(address) => address, + crate::durable::Kept::Withheld(_) => panic!("valid address was withheld: {original}"), + }; assert_eq!(address, expected); } let prefix = crate::render::rustgis_tiles::s3::ANONYMOUS; - let mut anonymous = format!("{prefix}https://bucket.example/a?token=secret"); - crate::project::strip_addresses(vec![&mut anonymous]); + let crate::durable::Kept::Changed(anonymous) = + crate::durable::public_address(&format!("{prefix}https://bucket.example/a?token=secret")) + else { + panic!("anonymous address was not redacted") + }; assert_eq!(anonymous, format!("{prefix}https://bucket.example/a")); } @@ -16079,40 +16084,74 @@ fn recovery_copies_keep_no_layer_tokens_and_restore_none() { #[test] fn saved_projects_keep_no_layer_tokens() { let mut s = world(); - let url = "https://gis.example/arcgis/rest/services/a/MapServer/tile/{z}/{y}/{x}?token=s3cret"; + let url = "https://gis.example/arcgis/rest/services/a/MapServer/tile/{z}/{y}/{x}?token=s3cret-tile"; // Its legend, built from the service's address, with the key in it too. - let legend = "https://gis.example/wms?key=l3g&SERVICE=WMS&REQUEST=GetLegendGraphic&LAYER=a"; + let legend = "https://gis.example/wms?key=l3g-secret&SERVICE=WMS&REQUEST=GetLegendGraphic&LAYER=a"; s.run("map.addTiles", &json!({"url":url,"name":"Secured","legend":legend})).unwrap(); let layer = s.map().layers[0].id; - s.map_mut().layers[0].dataset.source = Some("https://u:pw@gis.example/f?f=json&access_token=tok2".into()); + s.map_mut().layers[0].dataset.source = Some("https://u:pw-secret@gis.example/f?f=json&access_token=tok2-secret".into()); s.map_mut().service_groups.push(rustgis_map::ServiceGroup { id: 123, name: "Protected service".into(), - url: "https://gis.example/wfs?key=k3y&map=m".into(), + url: "https://gis.example/wfs?key=k3y-secret&map=m".into(), kind: "wfs".into(), expanded: true, children: vec![rustgis_map::ServiceChild { layer, collection: "roads".into(), - source_url: "https://gis.example/roads?sig=x&sv=1".into(), + source_url: "https://gis.example/roads?sig=x-secret&sv=1".into(), }], }); let path = std::env::temp_dir().join(format!("rustgis-token-audit-{}.rgis", std::process::id())); let result = s.run("project.saveAs", &json!({"path":path})).unwrap(); let text = std::fs::read_to_string(&path).unwrap(); - for secret in ["s3cret", "pw@", "tok2", "k3y", "sig=x", "l3g"] { + for secret in ["s3cret-tile", "pw-secret@", "tok2-secret", "k3y-secret", "sig=x-secret", "l3g-secret"] { assert!(!text.contains(secret), "{secret} leaked into the file"); } assert!(text.contains("MapServer/tile/{z}/{y}/{x}") && text.contains("map=m") && text.contains("REQUEST=GetLegendGraphic")); assert!(result["stripped"].as_u64().unwrap() >= 4); assert!(s.notices.iter().any(|n| n.text.contains("saved without their keys"))); assert!(s.map().tiles.iter().any(|t| t.url == url)); - for kind in ["map", "projectPackage", "mapPackage", "mobilePackage"] { - let (_, bytes) = crate::cmd::items::item_bytes(&mut s, kind, &Value::Null).unwrap(); - let packaged = - if kind == "map" { String::from_utf8(bytes.clone()).unwrap() } else { crate::cmd::items::package_text(&bytes).unwrap() }; - for secret in ["s3cret", "pw@", "tok2", "k3y", "sig=x", "l3g"] { - assert!(!packaged.contains(secret), "{kind} leaked {secret}"); + // Every kind of item file, each with something in the project to write. + s.run("style.new", &json!({"layer": "Cities", "name": "Cities"})).unwrap(); + s.run("gp.run", &json!({"tool": "buffer", "params": {"input": "Cities", "distance": 100}})).unwrap(); + s.run("task.new", &Value::Null).unwrap(); + s.run("model.new", &json!({"fromHistory": true})).unwrap(); + s.run("report.new", &json!({"layer": "Cities"})).unwrap(); + s.run("presentation.new", &Value::Null).unwrap(); + s.run("notebook.new", &Value::Null).unwrap(); + // Kinds that hold no layer addresses, or need more than the sample gives. + let exempt = [ + ("ramps", "colour ramps hold no addresses"), + ("dlPackage", "a trained model holds no addresses"), + ("hostedImagery", "tiles rendered from a raster layer: pictures, not addresses"), + ("linkChart", "needs relationships the sample lacks"), + ]; + let mut report = Vec::new(); + for &(kind, _, _) in crate::cmd::items::ITEMS { + if exempt.iter().any(|(k, _)| *k == kind) { + continue; + } + let bytes = match crate::cmd::items::item_bytes(&mut s, kind, &Value::Null) { + Ok((_, bytes)) => bytes, + Err(e) => { + report.push(format!("{kind}: {e}")); + continue; + } + }; + let packaged = if bytes.starts_with(b"PK") { + rustgis_data::zip::read_matching(&bytes, |_| true) + .unwrap() + .into_iter() + .map(|(_, b)| String::from_utf8_lossy(&b).into_owned()) + .collect::() + } else { + String::from_utf8_lossy(&bytes).into_owned() + }; + for secret in ["s3cret-tile", "pw-secret@", "tok2-secret", "k3y-secret", "sig=x-secret", "l3g-secret"] { + if packaged.contains(secret) { + report.push(format!("{kind}: leaked {secret}")); + } } if kind == "projectPackage" { // Even a hostile archive carrying the exact registered JSON must not restore it. @@ -16121,21 +16160,22 @@ fn saved_projects_keep_no_layer_tokens() { std::fs::write(&package_path, hostile).unwrap(); let mut opened = Session::new(Project::default()); opened.run("project.open", &json!({"path":package_path})).unwrap(); - assert!(!serde_json::to_string(&opened.project).unwrap().contains("s3cret")); + assert!(!serde_json::to_string(&opened.project).unwrap().contains("s3cret-tile")); opened.run("project.openText", &json!({"text":text,"package":true})).unwrap(); - assert!(!serde_json::to_string(&opened.project).unwrap().contains("s3cret")); + assert!(!serde_json::to_string(&opened.project).unwrap().contains("s3cret-tile")); std::fs::remove_file(package_path).unwrap(); } } + assert!(report.is_empty(), "{report:#?}"); let mut t = Session::new(Project::default()); t.run("project.open", &json!({"path":path})).unwrap(); assert!(t.map().tiles.iter().any(|t| t.url == url)); - assert!(t.map().layers[0].dataset.source.as_deref().unwrap().contains("tok2")); + assert!(t.map().layers[0].dataset.source.as_deref().unwrap().contains("tok2-secret")); let modified = text.replace("\"name\":\"Secured\"", "\"name\":\"Secures\""); assert_ne!(text, modified); t.run("project.openText", &json!({"text":modified})).unwrap(); let live = serde_json::to_string(&t.project).unwrap(); - for secret in ["s3cret", "pw@", "tok2", "k3y", "sig=x"] { + for secret in ["s3cret-tile", "pw-secret@", "tok2-secret", "k3y-secret", "sig=x-secret"] { assert!(!live.contains(secret), "{secret} restored into a modified hostile file"); } let public = url.split('?').next().unwrap(); @@ -16143,7 +16183,7 @@ fn saved_projects_keep_no_layer_tokens() { hand.run("map.addTiles", &json!({"url":public,"name":"Hand made"})).unwrap(); t.run("project.openText", &json!({"text":serde_json::to_string(&hand.project).unwrap()})).unwrap(); assert!(t.map().tiles.iter().any(|t| t.url == public)); - assert!(!serde_json::to_string(&t.project).unwrap().contains("s3cret")); + assert!(!serde_json::to_string(&t.project).unwrap().contains("s3cret-tile")); std::fs::remove_file(path).unwrap(); } @@ -18810,6 +18850,11 @@ fn a_run_is_copied_from_the_history_as_code() { assert_eq!((tool_of(&runs[0]).as_deref(), tool_of(&runs[1]), tool_of(&runs[2]).as_deref()), (Some("buffer"), None, Some("buffer"))); let py = code(&mut s, 2, "python").unwrap(); assert!(py.contains("tool=\"buffer\"") && py.contains("https://h.example/a") && !py.contains("s3cr3t"), "{py}"); + // Replay keeps opaque text and unknown nested content, with an honest warning. The log's + // separate, stricter projection still exercises the legacy connection/text cases below. + assert!(py.contains("odbcpw") && py.contains("libpqpw") && py.contains("shortk"), "{py}"); + assert!(s.notices.iter().any(|n| n.text.contains("opaque parameters"))); + let py = serde_json::to_string_pretty(&crate::cmd::history::redacted(&old.params)).unwrap(); // A libpq connection keeps its words but its password, though spaces part them. assert!(py.contains("host=db") && !py.contains("hunter2"), "{py}"); // A connection with no prefix `redact` knows keeps its words but its secret-named values. @@ -18910,6 +18955,9 @@ fn secrets_in_a_query_or_a_run_s_error_stay_out_of_copied_code() { outputs: Vec::new(), }); let py = s.run("gp.historyCode", &json!({"run": 0})).unwrap()["code"].as_str().unwrap().to_string(); + assert!(py.contains("password=qp"), "opaque query text stays replayable: {py}"); + assert!(s.notices.iter().any(|n| n.text.contains("opaque parameters"))); + let py = serde_json::to_string_pretty(&crate::cmd::history::redacted(&s.project.gp_history[0].params)).unwrap(); assert!(py.contains("a=1&password=&b=2") && !py.contains("qp"), "{py}"); // An `&` in a value, or in quotes, isn't another pair: the whole value goes. So does an ODBC // braced value with an escaped `}}` in it. @@ -22923,6 +22971,41 @@ fn geopackage_trigger_rebuild_rejects_a_valid_envelope_with_unreadable_wkb() { assert_eq!(s.run("gpkg.pendingEdits", &json!({"layer":"R"})).unwrap()["updates"], 1); } +/// A template keeps no copies of the data: offline snapshots, archived edits and a streamed +/// layer's file would each bring features back when it opens. +#[test] +fn project_template_carries_no_data() { + let path = grid_fgb("template-streamed", 20); + let mut s = with_streamed(&path); + s.run("offline.download", &json!({"name": "Paris", "extent": [1.0, 48.0, 4.0, 50.0]})).unwrap(); + s.run("data.archive", &json!({"layer": "Cities"})).unwrap(); + s.run("edit.setValue", &json!({"layer": "Cities", "row": 0, "field": "name", "value": "Renamed"})).unwrap(); + s.run("graphics.add", &json!({"shape": "rectangle", "points": [[0, 45], [10, 55]]})).unwrap(); + let mut table = rustgis_data::Dataset::new( + "Visits", + rustgis_data::GeometryType::None, + vec![rustgis_data::Field::new("n", rustgis_data::FieldType::Integer)], + ); + table.push(None, vec![rustgis_data::Value::Int(1)]); + table.source = Some(path.display().to_string()); + s.project.add_table(table); + assert!(!s.project.offline_areas.is_empty()); + let (_, bytes) = crate::cmd::items::item_bytes(&mut s, "template", &Value::Null).unwrap(); + 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(); + assert!(t.offline_areas.is_empty()); + assert!(t.tables.iter().all(|t| t.dataset.features.is_empty() && t.dataset.source.is_none())); + assert!(t.maps.iter().flat_map(|m| &m.layers).all(|l| l.dataset.features.is_empty()), "graphics and features stay out"); + let cities = t.maps[0].layer_by_name("Cities").unwrap(); + assert_eq!(cities.design.archive.as_ref().map(Vec::len), Some(0), "archiving stays on, without its edits"); + let streamed = t.maps[0].layer_by_name("Streamed").unwrap(); + assert!(streamed.source.is_none() && streamed.dataset.features.is_empty()); + 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())); +} + #[cfg(not(target_arch = "wasm32"))] #[test] fn geopackage_template_drops_edit_binding() { diff --git a/crates/engine/src/tests/credential_output.rs b/crates/engine/src/tests/credential_output.rs new file mode 100644 index 0000000000..d9d87d7a3d --- /dev/null +++ b/crates/engine/src/tests/credential_output.rs @@ -0,0 +1,315 @@ +use super::*; +use crate::cmd::notebook::{Cell, CellKind, Notebook}; +use crate::{ + GpRecord, Peer, durable, + project::{Model, ModelGraph, ModelNode, ModelStep, Task, TaskStep}, +}; +use rustgis_map::presentation::{Page, PageKind, Presentation}; + +const SECRET: &str = "RGSYNTH_output_20261103"; +const DATA: &str = "RGSYNTH_data_20261103"; + +/// Inline synthetic state exercises owning fields without contacting any service. +fn marked() -> Session { + let mut s = world(); + let url = format!("https://h.invalid/a?token={SECRET}&layer=a%20b"); + s.project.maps[0].layers[0].dataset.source = Some(url.clone()); + s.project.tasks.push(Task { name: "Credentials".into(), description: DATA.into(), steps: vec![ + TaskStep { title: DATA.into(), instructions: DATA.into(), command: "layer.addUrl".into(), params: json!({"url": url}) }, + TaskStep { title: "Malformed wrapper".into(), instructions: String::new(), command: "layer.addUrl".into(), params: json!({"url": format!("rustgis-s3:broken:{SECRET}")}) }, + TaskStep { title: "Data".into(), instructions: String::new(), command: "layer.addDataText".into(), params: json!({"text": format!(r#"{{"type":"Feature","geometry":null,"properties":{{"token":"{DATA}","key":"{DATA}","password":"{DATA}","url":"https://h.invalid/?token={DATA}"}}}}"#)}) }, + ] }); + let params = json!({"input": "Cities", "token": SECRET, "values": {"token": DATA, "key": DATA, "password": DATA}}); + s.project.models.push(Model { + name: "Credentials".into(), + steps: vec![ModelStep { tool: "buffer".into(), params: params.clone() }], + graph: Some(ModelGraph { + nodes: vec![ModelNode { id: "tool1".into(), tool: Some("buffer".into()), params: params.clone(), ..Default::default() }], + edges: Vec::new(), + }), + }); + s.project.gp_history.push(GpRecord { + tool: "Buffer".into(), + id: "buffer".into(), + params, + command: Some(("gp.bufferPoint".into(), json!({"token": SECRET}))), + at: 0, + ok: true, + messages: vec![format!("source {url}")], + millis: 0, + outputs: vec!["Cities".into()], + }); + s.project.notebooks.push(Notebook { + name: "Credentials".into(), + jupyter: None, + cells: vec![Cell { + kind: CellKind::Code, + source: format!("# {DATA}\nlayer.addUrl {{\"url\":\"{url}\"}}\nlayer.addUrl {{\"url\":\"{url}\"\n"), + output: Some(format!("source {url}")), + error: true, + jupyter: None, + }], + }); + s.project.presentations.push(Presentation { + name: "Credentials".into(), + pages: vec![Page { + title: DATA.into(), + kind: PageKind::Video { source: url, poster: None, caption: DATA.into() }, + notes: DATA.into(), + seconds: 5.0, + }], + ..Default::default() + }); + s +} + +#[test] +fn durable_writers_clean_all_owned_roles_and_preserve_the_live_project() { + let mut s = marked(); + let before = serde_json::to_value(&s.project).unwrap(); + let (text, report) = s.project.saved_text(None).unwrap(); + assert!(!text.contains(SECRET)); + assert!(report.layer_addresses() > 0 && report.steps > 0 && report.lines > 0 && report.videos > 0 && report.withheld > 0); + assert!(text.contains(DATA)); + assert_eq!(before, serde_json::to_value(&s.project).unwrap()); + let opened = Project::from_json(&text).unwrap(); + assert_eq!(opened.tasks[0].steps, s.project.tasks[0].steps); + assert_eq!(opened.models[0], s.project.models[0]); + assert_eq!(opened.notebooks[0].cells[0].source, s.project.notebooks[0].cells[0].source); + assert!(!opened.notebooks[0].cells[0].output.as_ref().unwrap().contains(SECRET), "generated text is never restored"); + assert!(!s.project.clone().recovery_text().unwrap().contains(SECRET)); + for kind in [ + "map", + "layer", + "template", + "projectPackage", + "mapPackage", + "mobilePackage", + "gpPackage", + "task", + "notebook", + "jupyter", + "presentation", + "webTool", + ] { + let (_, bytes) = crate::cmd::items::item_bytes(&mut s, kind, &Value::Null).unwrap(); + let text = if bytes.starts_with(b"PK") { + rustgis_data::zip::read_matching(&bytes, |_| true) + .unwrap() + .into_iter() + .map(|(_, b)| String::from_utf8_lossy(&b).into_owned()) + .collect::() + } else { + String::from_utf8_lossy(&bytes).into_owned() + }; + assert!(!text.contains(SECRET), "{kind}"); + assert_eq!(before, serde_json::to_value(&s.project).unwrap(), "{kind} mutated live fields"); + } + let code = s.run("gp.historyCode", &json!({"run": 0, "as": "command"})).unwrap(); + assert!(!code.to_string().contains(SECRET)); + s.project.gp_history[0].command = None; + let code = s.run("gp.historyScript", &Value::Null).unwrap(); + assert!(!code.to_string().contains(SECRET)); + assert!(code.to_string().contains(DATA), "replayable data properties must remain"); +} + +#[test] +fn durable_clean_and_opaque_content_keep_their_meaning() { + let mut s = marked(); + let original = s.project.tasks[0].steps[2].params.clone(); + let (clean, report) = crate::roles::command("layer.addDataText", &original, 0); + assert!(clean.is_none() && report.notices().is_empty()); + let source = format!("print('https://h.invalid/?token={SECRET}')"); + let notebook = Notebook { + name: "Python".into(), + jupyter: Some(std::sync::Arc::new(json!({"metadata":{"kernelspec":{"language":"python"}}}))), + cells: vec![Cell { kind: CellKind::Code, source: source.clone(), output: None, error: false, jupyter: None }], + }; + let (text, report) = durable::output_owned(notebook, |n| serde_json::to_string(n).map_err(|e| e.to_string())).unwrap(); + assert!(text.contains(SECRET) && report.notices().is_empty()); + let params = json!({"nested": {"token": SECRET}}); + let (clean, report) = crate::roles::command("plugin.unknown", ¶ms, 0); + assert!(clean.is_none() && report.unchecked == 1); + let clean = Project::default(); + let expected = serde_json::to_string(&clean).unwrap(); + let (text, report) = durable::output_owned(clean, |p| serde_json::to_string(p).map_err(|e| e.to_string())).unwrap(); + assert_eq!(text, expected); + assert!(report.notices().is_empty()); + s.project.tasks[0].steps[0].params = json!({"url": "rustgis-s3:broken:RGSYNTH_output_20261103"}); + let (text, report) = s.project.saved_text(None).unwrap(); + assert!(!text.contains(SECRET) && report.withheld > 0); +} + +#[test] +fn durable_formatter_errors_unwinds_and_apply_failures_restore_whole_fields() { + use durable::{Durable, FieldMut, Leaf, Owner, Prepare}; + let mut s = marked(); + s.project.maps[0].layers[0].dataset.source = Some("/tmp/rustgis-output/data.geojson".into()); + let mut document = Project::default(); + document.tasks = s.project.tasks.clone(); + let text = serde_json::to_string_pretty(&document).unwrap(); + let mut params = json!({"text": text}); + let mut id = "project.openText"; + for _ in 0..6 { + params = json!({"steps": [{"title":"nested", "command":id, "params":params}]}); + id = "task.new"; + } + s.project.tasks[0].steps[0].command = id.into(); + s.project.tasks[0].steps[0].params = params; + // A separate document is actually parsed and rewritten before the deeper envelope withholds. + s.project.tasks[0].steps.push(TaskStep { + title: "document".into(), + instructions: String::new(), + command: "project.openText".into(), + params: json!({"text":text}), + }); + let before = serde_json::to_value(&s.project).unwrap(); + let result = durable::output(&mut s.project, Some("/tmp/rustgis-output"), |p| { + assert_eq!(p.maps[0].layers[0].dataset.source.as_deref(), Some("./data.geojson")); + assert!(!serde_json::to_string(p).unwrap().contains(SECRET)); + Err::<(), _>("formatter failed".into()) + }); + assert!(result.is_err()); + assert_eq!(before, serde_json::to_value(&s.project).unwrap()); + #[cfg(not(target_arch = "wasm32"))] + { + let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let _ = durable::output(&mut s.project, Some("/tmp/rustgis-output"), |_| -> Result<(), String> { + std::panic::resume_unwind(Box::new("formatter failed")) + }); + })); + assert!(unwound.is_err()); + assert_eq!(before, serde_json::to_value(&s.project).unwrap()); + } + struct Bad(Project); + impl Durable for Bad { + /// Offers classified whole fields once, before any live mutation. + fn prepare(&self, p: &mut Prepare<'_>) { + self.0.prepare(p); + p.string(Leaf::Video(usize::MAX), "https://h.invalid/?token=RGSYNTH_output_20261103", true, false); + } + + /// Resolves a prepared location through owned fields, without allocation or policy. + fn field_mut(&mut self, owners: &[Owner], leaf: &Leaf) -> Option> { + self.0.field_mut(owners, leaf) + } + } + let mut bad = Bad(s.project); + assert!(durable::output(&mut bad, Some("/tmp/rustgis-output"), |_| Ok(())).is_err()); + assert_eq!(before, serde_json::to_value(&bad.0).unwrap()); +} + +#[test] +fn model_and_presentation_non_item_writers_use_public_forms() { + let mut s = marked(); + let graph = s.project.models[0].graph.clone().unwrap(); + let out = s.run("model.export", &json!({"model":"Credentials"})).unwrap(); + assert!(!out["text"].as_str().unwrap().contains(SECRET)); + let file = std::env::temp_dir().join(format!("rustgis-output-model-{}.json", std::process::id())); + s.run("model.export", &json!({"model":"Credentials", "path":file})).unwrap(); + assert!(!std::fs::read_to_string(&file).unwrap().contains(SECRET)); + std::fs::remove_file(file).unwrap(); + assert_eq!(s.project.models[0].graph.as_ref(), Some(&graph)); + let userinfo = format!("https://u:{SECRET}@h.invalid"); + assert_eq!(crate::cmd::presentation::video_label(&userinfo), "h.invalid"); + assert_eq!(crate::cmd::presentation::video_label("/tmp/movie?cut#1.mp4"), "movie?cut#1.mp4"); + s.run("presentation.videoPage", &json!({"source":userinfo})).unwrap(); + assert_eq!(s.project.presentations[0].pages.last().unwrap().title, "h.invalid"); + let (bytes, _) = crate::cmd::presentation::export_bytes(&mut s, 0, "html", 48.0).unwrap(); + let html = String::from_utf8(bytes).unwrap(); + assert!(!html.contains(SECRET)); + assert!(html.contains(DATA)); + assert!(s.notices.iter().any(|n| n.text.contains("video address(es)"))); +} + +#[test] +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()); + 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)); + for kind in ["notebook", "jupyter"] { + assert!(!String::from_utf8_lossy(&crate::cmd::items::item_bytes(&mut s, kind, &Value::Null).unwrap().1).contains(SECRET)); + } + // Failed file tools retain their failure history through the command rollback. The missing + // input is a layer name, so this reaches the production error path without any network I/O. + let history = s.project.gp_history.len(); + assert!( + s.run( + "gp.run", + &json!({"tool":"vectorToPmtiles", "params":{"input":format!("https://h.invalid/?token={SECRET}"), "minZoom":0, "maxZoom":0}}) + ) + .is_err() + ); + assert_eq!(s.project.gp_history.len(), history + 1); + let record = s.project.gp_history.last().unwrap(); + assert_eq!(record.id, "vectorToPmtiles"); + assert!(!record.ok && !record.messages.join("\n").contains(SECRET)); +} + +#[test] +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()); + let generation = s.opened; + s.open_peer_text(Peer::Embedder, &saved).unwrap(); + assert!(s.opened != generation && s.trusted_tasks.is_empty()); + assert!(!serde_json::to_string(&s.project).unwrap().contains(SECRET)); + for scenario in ["unchanged", "rotation", "removed", "inserted", "moved", "accounts", "generation", "duplicate_table"] { + let mut s = marked(); + s.project.tasks.clear(); + s.project.models.clear(); + s.project.gp_history.clear(); + s.project.notebooks.clear(); + s.project.presentations.clear(); + if scenario == "accounts" { + let mut other = s.project.maps[0].layers[0].clone(); + other.id = 9000; + other.dataset.source = Some("https://h.invalid/a?token=RGSYNTH_other_20261103&layer=a%20b".to_string()); + s.project.maps[0].layers.push(other); + } + if scenario == "duplicate_table" { + let mut table = rustgis_data::Dataset::new("Table", rustgis_data::GeometryType::None, vec![]); + table.source = Some(format!("https://h.invalid/table?token={SECRET}")); + s.project.add_table(table); + } + let (public, _) = s.peer_text(Peer::Bridge).unwrap(); + assert!(!public.contains(SECRET)); + let mut incoming: Value = serde_json::from_str(&public).unwrap(); + match scenario { + "rotation" => { + s.project.maps[0].layers[0].dataset.source = Some("https://h.invalid/a?token=RGSYNTH_rotated_20261103&layer=a%20b".into()) + } + "removed" => { + s.project.maps[0].layers.remove(0); + } + "inserted" => incoming["maps"][0]["layers"][0]["id"] = json!(9001), + "moved" => { + incoming["maps"][0]["name"] = json!("Another map"); + } + "generation" => { + s.run("project.new", &Value::Null).unwrap(); + } + "duplicate_table" => { + let table = incoming["tables"][0].clone(); + incoming["tables"].as_array_mut().unwrap().push(table); + } + _ => {} + } + s.open_peer_text(Peer::Bridge, &incoming.to_string()).unwrap(); + let full = serde_json::to_string(&s.project).unwrap(); + if scenario == "unchanged" || scenario == "duplicate_table" { + assert!(full.contains(SECRET), "{scenario}: ordinary layer stays"); + } else { + assert!(!full.contains(SECRET), "{scenario}"); + } + if scenario == "duplicate_table" { + assert!(s.project.tables.iter().all(|t| !t.source.as_deref().unwrap_or("").contains(SECRET))); + } + assert!(s.trusted_tasks.is_empty() && s.trusted_models.is_empty() && s.trusted_notebooks.is_empty()); + } +} diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index a2d4a1912e..6399ca49c0 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -22,7 +22,9 @@ pub mod projection; pub mod qgis; pub mod raster; pub mod report; +pub mod slots; pub mod source; +pub use slots::{LayerRole, Slot, SlotAt, SlotPos, TileRole}; pub mod symbol_by; pub mod symbology; pub mod tiles; @@ -2693,6 +2695,210 @@ fn de_reference_scale<'de, D: serde::Deserializer<'de>>(d: D) -> Result Vec> { + let Map { + name: _, + layers, + service_groups, + groups: _, + basemap: _, + tiles, + camera: _, + bookmarks: _, + graticule: _, + projection: _, + popup_mode: _, + label_display: _, + label_lock: _, + label_lock_line_anchors: _, + editing: _, + swipe: _, + view, + thumbnail: _, + animation: _, + time: _, + body: _, + next_layer_id: _, + instance_id: _, + } = self; + let mut out = Vec::new(); + for (group, g) in service_groups.iter().enumerate() { + let ServiceGroup { id: _, name: _, url, kind: _, expanded: _, children } = g; + out.push(Slot { at: SlotPos::Group { group, child: None }, value: url, address: true, local_path: false }); + for (child, c) in children.iter().enumerate() { + let ServiceChild { layer: _, collection: _, source_url } = c; + out.push(Slot { at: SlotPos::Group { group, child: Some(child) }, value: source_url, address: true, local_path: false }); + } + } + for (tile, t) in tiles.iter().enumerate() { + out.extend(t.output_slots().into_iter().map(|(role, value, local_path)| Slot { + at: SlotPos::Tile { tile, role }, + value, + address: true, + local_path, + })); + } + let view::ViewSettings { + effects: _, + color_vision: _, + wireframe: _, + clip_distance: _, + terrain, + fast: _, + navigator: _, + first_person: _, + } = view; + if let Some(view::Terrain { source, exaggeration: _ }) = terrain { + match source { + view::TerrainSource::Tiles { url, encoding: _ } => { + out.push(Slot { at: SlotPos::Terrain, value: url, address: true, local_path: false }) + } + view::TerrainSource::Layer(_) => {} + } + } + for (layer, l) in layers.iter().enumerate() { + out.extend(l.output_slots().into_iter().map(|(role, value, address, local_path)| Slot { + at: SlotPos::Layer { layer, role }, + value, + address, + local_path, + })); + } + out + } + + /// Resolves only the recorded location; never rediscovers or reclassifies a written value. + pub fn slot_mut(&mut self, at: &SlotPos) -> Option<&mut String> { + match at { + SlotPos::Group { group, child } => { + let g = self.service_groups.get_mut(*group)?; + match child { + Some(c) => Some(&mut g.children.get_mut(*c)?.source_url), + None => Some(&mut g.url), + } + } + SlotPos::Tile { tile, role } => self.tiles.get_mut(*tile)?.output_slot_mut(*role), + SlotPos::Terrain => match &mut self.view.terrain.as_mut()?.source { + view::TerrainSource::Tiles { url, encoding: _ } => Some(url), + view::TerrainSource::Layer(_) => None, + }, + SlotPos::Layer { layer, role } => self.layers.get_mut(*layer)?.output_slot_mut(*role), + } + } +} + +impl Layer { + /// Classifies whole owning fields; a source serving as provenance and a path occurs once. + pub fn output_slots(&self) -> Vec<(LayerRole, &str, bool, bool)> { + let Layer { + id: _, + name: _, + visible: _, + dataset, + renderer: _, + labels: _, + transparency: _, + min_scale: _, + max_scale: _, + definition_query: _, + time: _, + selection: _, + expanded: _, + selectable: _, + graphics: _, + blend: _, + mask: _, + display_filters: _, + aggregation: _, + extrusion: _, + line_strokes: _, + fill_layers: _, + no_fill: _, + stroke_levels: _, + marker_rotation: _, + symbol_by: _, + reference_scale: _, + priority: _, + design: _, + raster, + source, + postgis: _, + gpkg, + instance_id: _, + projected: _, + display: _, + display_rejected: _, + display_levels: _, + projected_extents: _, + projected_vertices: _, + thinned: _, + projected_rev: _, + projected_for: _, + view_memory: _, + geometry_kinds: _, + geometry_rev: _, + } = self; + let mut out = Vec::new(); + let rustgis_data::Dataset { + name: _, + geometry_type: _, + fields: _, + features: _, + source: provenance, + crs: _, + crs_incomplete: _, + zm_dropped: _, + windowed: _, + } = dataset; + if let Some(v) = provenance { + out.push((LayerRole::Dataset, v.as_str(), true, paths::is_local(v))); + } + if let Some(s) = source { + out.extend(s.output_slots()); + } + if let Some(r) = raster { + out.extend(r.output_slots()); + } + if let Some(g) = gpkg { + out.push((LayerRole::GpkgPath, g.path.as_str(), false, paths::is_local(&g.path))); + } + out + } + + /// Allocation-free resolution, valid while the layer's structure is held unchanged. + pub fn output_slot_mut(&mut self, role: LayerRole) -> Option<&mut String> { + match role { + LayerRole::Dataset => self.dataset.source.as_mut(), + LayerRole::Streamed | LayerRole::GdalPath | LayerRole::DuckDbDatabase => self.source.as_mut()?.output_slot_mut(role), + LayerRole::Raster | LayerRole::ZarrStore => self.raster.as_mut()?.output_slot_mut(role), + LayerRole::GpkgPath => Some(&mut self.gpkg.as_mut()?.path), + } + } +} + +impl Map { + /// Bridge identity is independent of the address value; duplicate ids remain ambiguous. + pub fn slot_identity(&self, at: &SlotPos) -> Option { + match at { + SlotPos::Group { group, child } => { + let g = self.service_groups.get(*group)?; + Some(SlotAt::Group { + id: g.id, + child: match child { + Some(i) => Some(g.children.get(*i)?.layer), + None => None, + }, + }) + } + SlotPos::Tile { tile, role } => Some(SlotAt::Tile { id: self.tiles.get(*tile)?.id.clone(), role: *role }), + SlotPos::Terrain => Some(SlotAt::Terrain), + SlotPos::Layer { layer, role } => Some(SlotAt::Layer { id: self.layers.get(*layer)?.id, role: *role }), + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/map/src/raster.rs b/crates/map/src/raster.rs index ae357e7b19..dde6cc2b59 100644 --- a/crates/map/src/raster.rs +++ b/crates/map/src/raster.rs @@ -1079,6 +1079,43 @@ impl RasterLayer { } } +impl RasterLayer { + /// Only owning address/path strings are offered; embedded bytes and render state stay opaque. + pub(crate) fn output_slots(&self) -> Vec<(crate::LayerRole, &str, bool, bool)> { + let RasterLayer { + path, + file_name: _, + embedded, + subdataset: _, + crs: _, + time_zarr, + placement: _, + style: _, + data: _, + error: _, + masked: _, + opening: _, + } = self; + let mut out = Vec::new(); + if let Some(p) = path { + out.push((crate::LayerRole::Raster, p.as_str(), true, embedded.is_none() && crate::paths::is_local(p))); + } + if let Some(ZarrTime { store, variable: _, index: _, dim: _, offset: _, dates: _, crs: _, shown: _ }) = time_zarr { + out.push((crate::LayerRole::ZarrStore, store.as_str(), true, crate::paths::is_local(store))); + } + out + } + + /// Looks up a prepared address/path field without allocation. + pub(crate) fn output_slot_mut(&mut self, role: crate::LayerRole) -> Option<&mut String> { + match role { + crate::LayerRole::Raster => self.path.as_mut(), + crate::LayerRole::ZarrStore => Some(&mut self.time_zarr.as_mut()?.store), + _ => None, + } + } +} + #[cfg(test)] mod tests { use rustgis_data::raster::{Crs, GeoTransform}; diff --git a/crates/map/src/slots.rs b/crates/map/src/slots.rs new file mode 100644 index 0000000000..48219ab17d --- /dev/null +++ b/crates/map/src/slots.rs @@ -0,0 +1,46 @@ +//! Stable leaf locations for a synchronous output transaction. Resolving a location neither +//! discovers slots nor changes structure; addresses and local paths share a single location. + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub enum SlotPos { + Group { group: usize, child: Option }, + Tile { tile: usize, role: TileRole }, + Terrain, + Layer { layer: usize, role: LayerRole }, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum LayerRole { + Dataset, + Streamed, + Raster, + ZarrStore, + GdalPath, + DuckDbDatabase, + GpkgPath, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum TileRole { + Url, + TimeUrl, + TimeAt(usize), + Legend(usize), +} + +/// A slot's identity across a bridge exchange. Positions are deliberately absent, so reordering +/// layers preserves identity while changing their owner, role or id does not. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub enum SlotAt { + Layer { id: u32, role: LayerRole }, + Tile { id: String, role: TileRole }, + Group { id: u32, child: Option }, + Terrain, +} + +pub struct Slot<'a> { + pub at: SlotPos, + pub value: &'a str, + pub address: bool, + pub local_path: bool, +} diff --git a/crates/map/src/source.rs b/crates/map/src/source.rs index 83223919a5..ddf80fed0c 100644 --- a/crates/map/src/source.rs +++ b/crates/map/src/source.rs @@ -1120,6 +1120,74 @@ pub fn generation() -> u64 { GENERATION.load(Ordering::Relaxed) } +impl SourceLayer { + /// Persisted source address and local driver paths; handles and embedded data are opaque. + pub(crate) fn output_slots(&self) -> Vec<(crate::LayerRole, &str, bool, bool)> { + let SourceLayer { + path, + file_name: _, + table: _, + shapefile_crs: _, + embedded, + handle: _, + window: _, + render_lod: _, + pending: _, + projection: _, + ready_cache: _, + byte_budget: _, + estimated_row_bytes: _, + full_read: _, + saved_selection: _, + window_fields: _, + opening: _, + error: _, + login: _, + limit: _, + reads: _, + gdal, + duckdb, + postgis, + mysql, + sqlserver, + } = self; + use crate::{LayerRole as R, paths}; + let mut out = Vec::new(); + if let Some(p) = path { + out.push(( + R::Streamed, + p.as_str(), + true, + embedded.is_none() + && gdal.is_none() + && duckdb.is_none() + && postgis.is_none() + && mysql.is_none() + && sqlserver.is_none() + && paths::is_local(p), + )); + } + if let Some(g) = gdal { + out.push((R::GdalPath, g.path.as_str(), false, paths::is_local(&g.path))); + } else if let Some(d) = duckdb + && let Some(p) = &d.database + { + out.push((R::DuckDbDatabase, p.as_str(), false, paths::is_local(p))); + } + out + } + + /// Resolves a prepared field without examining its written contents. + pub(crate) fn output_slot_mut(&mut self, role: crate::LayerRole) -> Option<&mut String> { + match role { + crate::LayerRole::Streamed => self.path.as_mut(), + crate::LayerRole::GdalPath => Some(&mut self.gdal.as_mut()?.path), + crate::LayerRole::DuckDbDatabase => self.duckdb.as_mut()?.database.as_mut(), + _ => None, + } + } +} + #[cfg(test)] mod tests { /// Reopening this same layer must invalidate populated decisions even without `reset`. diff --git a/crates/map/src/tiles.rs b/crates/map/src/tiles.rs index 1c73affea8..4c8879e400 100644 --- a/crates/map/src/tiles.rs +++ b/crates/map/src/tiles.rs @@ -552,6 +552,64 @@ pub fn host_of(url: &str) -> Option<&str> { Some(host.split(':').next().unwrap_or(host)) } +impl TileLayer { + /// All persisted tile addresses, with the archive URL also classified as a local path. + pub(crate) fn output_slots(&self) -> Vec<(crate::TileRole, &str, bool)> { + let TileLayer { + name: _, + kind: _, + url, + bounds: _, + subdomains: _, + min_zoom: _, + max_zoom: _, + tile_size: _, + tms: _, + attribution: _, + visible: _, + opacity: _, + overlay: _, + over: _, + blend: _, + id: _, + layers: _, + style_layers: _, + hidden_layers: _, + time_url, + time_at: _, + time_urls, + time_hidden: _, + error: _, + feature_info: _, + legends, + expanded: _, + selected: _, + single_image: _, + } = self; + use crate::TileRole as R; + let mut out = vec![(R::Url, url.as_str(), self.archive_file().is_some())]; + if let Some(v) = time_url { + out.push((R::TimeUrl, v.as_str(), false)); + } + out.extend(time_urls.iter().enumerate().map(|(i, (_, v))| (R::TimeAt(i), v.as_str(), false))); + out.extend(legends.iter().enumerate().map(|(i, v)| { + let rustgis_data::ogc::Legend { url, min_scale: _, max_scale: _ } = v; + (R::Legend(i), url.as_str(), false) + })); + out + } + + /// Resolves a fixed location, independent of the address's current spelling. + pub(crate) fn output_slot_mut(&mut self, role: crate::TileRole) -> Option<&mut String> { + match role { + crate::TileRole::Url => Some(&mut self.url), + crate::TileRole::TimeUrl => self.time_url.as_mut(), + crate::TileRole::TimeAt(i) => Some(&mut self.time_urls.get_mut(i)?.1), + crate::TileRole::Legend(i) => Some(&mut self.legends.get_mut(i)?.url), + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/tiles/src/arcgis_auth.rs b/crates/tiles/src/arcgis_auth.rs index 98c38e3f3f..24795839a7 100644 --- a/crates/tiles/src/arcgis_auth.rs +++ b/crates/tiles/src/arcgis_auth.rs @@ -454,7 +454,7 @@ pub fn adopt_url_token(url: &str) -> Result { .split('&') .filter(|p| { let (k, v) = p.split_once('=').unwrap_or((p, "")); - if k.eq_ignore_ascii_case("token") { + if rustgis_data::address::normalized_name(k).as_deref() == Some("token") { // A token is pasted, not form-encoded: its `+` stays a `+`. token = Some(decode_with(v, false)); false diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index ca8cfe3500..b18f098d20 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -1927,7 +1927,7 @@ mod tests { let history = rustgis_data::diagnostics::History::default(); let _scope = history.enter(); let base = serve(vec![ - "HTTP/1.1 403 Forbidden\r\nWWW-Authenticate: Basic realm=\"private-realm\"\r\nRetry-After: 30\r\ncf-ray: 8a1-BOG\r\nX-Amzn-RequestId: amzn-1155\r\nContent-Length: 0\r\nConnection: close\r\n\r\n" + "HTTP/1.1 403 Forbidden\r\nWWW-Authenticate: Basic realm=\"private-realm\"\r\nRetry-After: 30\r\ncf-ray: 8a1-BOG\r\nX-Amzn-RequestId: amzn-1155\r\nX-Amz-Cf-Id: trace-1155\r\nX-Amzn-Waf-Action: block\r\nContent-Length: 0\r\nConnection: close\r\n\r\n" .into(), ]); let url = format!("{base}/refused"); @@ -1935,7 +1935,9 @@ mod tests { let records = network_records(&history, &url); assert_eq!(records.len(), 1); let details = &records[0].details; - for kept in ["retry-after: 30", "cf-ray: 8a1-BOG", "x-amzn-requestid: amzn-1155"] { + for kept in + ["retry-after: 30", "cf-ray: 8a1-BOG", "x-amzn-requestid: amzn-1155", "x-amz-cf-id: trace-1155", "x-amzn-waf-action: block"] + { assert!(details.contains(kept), "{kept} in {details:?}"); } assert!(!details.contains("private-realm"), "{details:?}"); diff --git a/crates/ui-egui/src/assistantpane.rs b/crates/ui-egui/src/assistantpane.rs index 3f5e04f143..8de0d33b24 100644 --- a/crates/ui-egui/src/assistantpane.rs +++ b/crates/ui-egui/src/assistantpane.rs @@ -1790,6 +1790,17 @@ fn inline(ui: &Ui, text: &str, t: &Tokens, size: f32, heading: bool) -> egui::te job } +/// A peer's project cannot inherit a pending approval, provider reply or queued user action. +pub(crate) fn peer_replaced(app: &mut RustGisApp) { + if let Some(agent) = &mut app.assistant.agent { + agent.project_replaced(); + } + app.assistant.inflight = None; + app.assistant.chunks = None; + app.assistant.queued = None; + app.assistant.always = false; +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/ui-egui/src/connectiontree.rs b/crates/ui-egui/src/connectiontree.rs index 85bc152236..11e457f1a9 100644 --- a/crates/ui-egui/src/connectiontree.rs +++ b/crates/ui-egui/src/connectiontree.rs @@ -787,8 +787,16 @@ mod tests { let slot = Slot::default(); *slot.lock().unwrap() = Some(Err("unreachable".to_string())); let params = json!({"url": api, "layer": "relief (map tiles)", "text": tiled, "name": "Relief"}); - let resolve = - crate::TilesResolve { params, list: "l".into(), doc: "l".into(), read: 0, vector: false, fallback: fallback.into(), slot }; + let resolve = crate::TilesResolve { + params, + list: "l".into(), + doc: "l".into(), + read: 0, + withheld_addresses: 0, + vector: false, + fallback: fallback.into(), + slot, + }; crate::ServiceStep::Tiles { resolve, dialog: false, origin: origin.map(str::to_string) } }; let before = h.state().session.sources.recent.clone(); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index ef1cbb7197..efaf01ebef 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -411,11 +411,13 @@ pub struct ServiceBrowse { pub struct TilesResolve { /// `service.add`'s parameters, given the template as `"tiles"` once read. pub params: Value, - /// The tileset list, whose query the template keeps; the document being read; how many have + /// The tileset list, whose safe query options the template may inherit; the document being read; how many have /// been. pub list: String, pub doc: String, pub read: u8, + /// Destinations that lost inherited query settings across both document reads. + pub withheld_addresses: u32, pub vector: bool, /// The usual-path template, when the advertised one can't be read. pub fallback: String, @@ -1556,7 +1558,7 @@ impl RustGisApp { // Not an undoable edit: saving names the project, as `project.saveAs` does, once the // file is written. let old = std::mem::replace(&mut self.session.project.name, stem.clone()); - let (text, stripped) = match self.session.project.saved_text(None) { + let (text, report) = match self.session.project.saved_text(None) { Ok(saved) => saved, Err(e) => { self.session.project.name = old; @@ -1569,14 +1571,8 @@ impl RustGisApp { return Err(e); } self.session.notify(crate::i18n::fmt("Downloaded {name}", &[("name", &file)]), false); - if stripped > 0 { - self.session.notify( - crate::i18n::fmt( - "{n} layer address(es) were saved without their keys, tokens or passwords: after RustGIS closes, add those layers again to give them", - &[("n", &stripped.to_string())], - ), - false, - ); + for notice in report.notices() { + self.session.notify(notice, false); } self.session.mark_saved(); self.ui.web_file = Some(file); @@ -1797,6 +1793,29 @@ impl RustGisApp { self.run(id, params) } + /// Imports a peer's project without going through User command dispatch or restore memory. + pub fn open_peer_text(&mut self, text: &str) -> Result<(), String> { + let report = self.session.open_peer_text(rustgis_engine::Peer::Embedder, text)?; + assistantpane::peer_replaced(self); + self.ui.web_file = None; + self.map.deleting = None; + self.ui.cog_probes.clear(); + if let Some(steps) = self.ws.recording.take() { + self.session.notify( + crate::i18n::fmt( + "Recording stopped: another project opened, and its {n} steps were for the previous one.", + &[("n", &steps.len().to_string())], + ), + true, + ); + } + layoutview::follow_project(self); + for notice in report.notices() { + self.session.notify(notice, false); + } + Ok(()) + } + /// Runs a command by id: `ui.*` here, everything else in the engine. pub fn run(&mut self, id: &str, params: Value) -> Result { let _diagnostics = self.session.diagnostics.enter(); @@ -3773,6 +3792,10 @@ impl RustGisApp { continue; }; let next = r.ok().and_then(|body| resolve_tiles(&resolve.list, &resolve.doc, &body, resolve.vector)); + let next = next.map(|resolved| { + resolve.withheld_addresses = resolve.withheld_addresses.saturating_add(resolved.withheld_addresses); + resolved.step + }); let template = match next { Some(TilesStep::Next(next)) if resolve.read < 2 => { resolve.read += 1; @@ -3784,6 +3807,12 @@ impl RustGisApp { Some(TilesStep::Template(t)) => t, _ => resolve.fallback.clone(), }; + if resolve.withheld_addresses > 0 { + self.session.notify( + i18n::fmt(rustgis_engine::data::ogc::QUERY_WITHHELD_NOTICE, &[("n", &resolve.withheld_addresses.to_string())]), + false, + ); + } let mut p = resolve.params; p["tiles"] = json!(template); let title = p["name"].as_str().unwrap_or("").to_string(); @@ -4537,6 +4566,37 @@ mod download_service_tests { use super::{RustGisApp, Services, complete_gp_file}; use serde_json::json; + /// The real asynchronous completion receiver owns the forwarding notice, even when the + /// 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 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 resolve = super::TilesResolve { + params: json!({"url":"https://h.invalid/svc", "type":"ogcapi", "layer":"map", "text":capabilities}), + list: list.clone(), + doc: list, + 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: true, origin: None }); + 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); + assert!(app.session.map().tiles.iter().all(|t| !t.url.contains("RGSYNTH_"))); + assert!(app.dialogs.error.is_none(), "{:?}", app.dialogs.error); + } + } + #[test] fn rejected_download_does_not_announce_success_or_rename_project() { let mut app = RustGisApp::with_sample() @@ -4669,3 +4729,25 @@ mod download_service_tests { assert_eq!(app.session.notices.iter().filter(|n| n.error && n.text == "no pending file with that id").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() { + 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.open_peer_text(&text).unwrap(); + assert_ne!(app.session.opened, generation); + 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")); + } +} diff --git a/crates/ui-egui/src/serviceui.rs b/crates/ui-egui/src/serviceui.rs index 2e48a44243..10c0377497 100644 --- a/crates/ui-egui/src/serviceui.rs +++ b/crates/ui-egui/src/serviceui.rs @@ -119,7 +119,16 @@ pub(crate) fn add_layer_from( let fallback = offer.get("url").and_then(Value::as_str).unwrap_or("").to_string(); let vector = offer.get("type").and_then(Value::as_str) == Some("vectorTiles"); let slot = crate::fetch_into(list); - let resolve = crate::TilesResolve { params: p, list: list.to_string(), doc: list.to_string(), read: 0, vector, fallback, slot }; + let resolve = crate::TilesResolve { + params: p, + list: list.to_string(), + doc: list.to_string(), + read: 0, + withheld_addresses: 0, + vector, + fallback, + slot, + }; app.ui.service_steps.push(crate::ServiceStep::Tiles { resolve, dialog, origin }); Ok(tl!("Reading the tileset of")) } diff --git a/docs/contributing.md b/docs/contributing.md index 496098f869..d62568486d 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -33,6 +33,14 @@ them together. Reply on the comment that it's left for the release quality pass reply with ``), so it can be found. Bugs, crashes, silent data loss and breaks of the rules in AGENTS.md are fixed in the pull request. +Changes to saved or generated output must classify each new owning field: address, executable +parameters, generated text or user data. Use the shared durable-output transaction and keep +credential removal separate from execution and approval identity. Review every writer that +consumes the owner, including packages and peer exchange. Reuse a focused regression that checks +the written output, restoration of live fields on failure, counted notices and clean-input +silence. Opaque data needs an explicit preservation policy; recursive removal by key name can +destroy user data. + ## Developer checks `cargo xtask ci` runs `cargo fmt --all --check`, diff --git a/docs/mcp.md b/docs/mcp.md index baf2b57b89..b12a6f3db8 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -156,7 +156,11 @@ Passwords, keys and tokens in the arguments, a `run` script's lines included, ar they are written. On Linux and macOS the folder and files are readable by you only; on Windows they're protected by your user profile's permissions. A batch's calls share the batch's `duration_s` and are marked `"batch": true`. An error whose text mentions a credential (a token, a -password, a bearer…) is logged as withheld rather than risk keeping the credential. Days older than 30 are removed. +password, a bearer…) is logged as withheld rather than risk keeping the credential. Log days older +than 30 days are pruned when a logged call creates a new day's file, not by a timer; older files +can remain while logging is idle. You can remove unwanted day files from this folder yourself. +Updating does not rewrite old logs. If an older log exposed a credential, rotate it with its +service before sharing the log. ### MCP over HTTP diff --git a/docs/python.md b/docs/python.md index 7d8e967252..74af594698 100644 --- a/docs/python.md +++ b/docs/python.md @@ -139,6 +139,13 @@ The project, with the data that travels with it, goes to that app through the br (`postMessage`); nothing goes to a server. The app runs in your browser, so with the default address your data stays on your machine. Point `url=` only at a copy of the app you trust. +The widget receives public addresses and cleaned executable parameters. Credentials held by +Python are not sent to it, so protected layers can remain empty there; a warning counts affected +layer addresses. When the widget sends a project back, Python can retain credentials only for +unchanged, uniquely matched sources from its last exchange that still match its current project. +Changing or replacing a source can require signing in again in Python. Exchange notices are +available through `m.notices()` on the `Map` or `MapWidget`. + ## The app in a Qt application `rustgis.qt.MapWidget` is the same app in a Qt widget, for a desktop tool or a script that opens a @@ -287,9 +294,10 @@ last view in memory; run `layer.loadAll` on it before `to_geojson` or `to_geopan of them. Open an existing project with `Session(project="atlas.rgis")` or `s.open("atlas.rgis")`. -`s.project_json()` returns its JSON text; `s.run("project.openText", text=...)` opens project -JSON without an intermediate file. `to_png()` returns PNG bytes even when it also writes a -file; omit the path to keep the image in memory. +`s.project_json()` returns JSON with the same credential removal as Save, with notices available +through `s.notices()`. It refuses unfinished imports instead of writing runtime reservations. +`s.run("project.openText", text=...)` opens project JSON without an intermediate file. `to_png()` +returns PNG bytes even when it also writes a file; omit the path to keep the image in memory. ## From source diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index b29cc2b652..b3ecf69815 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -1108,13 +1108,17 @@ routes, areas, observations…) to draw into. Add Folder connects a folder to th also lists the project's layouts, styles and tasks. A task's steps each run a command; New Task From History turns the geoprocessing history into a task you can replay. -Saved projects, map and layer files, and packages keep no password, key, token or signature in -layer web addresses. The open project keeps them for this session. RustGIS remembers the 32 most -recent distinct saved snapshots whose addresses needed stripping. Opening a plain `.rgis` file -whose bytes exactly match one of those snapshots restores its secrets; saving more distinct -snapshots evicts the oldest. Changed files, packages, imports and files saved by another process -never restore secrets. After a snapshot is evicted or RustGIS closes, add those layers again with -their credentials. +Saved projects, templates, map and layer files, and packages remove credentials from supported +addresses and recognized executable parameters. The open project keeps its credentials. Removal +notices describe what a shared workflow or video will need again; an unchecked-content notice +means opaque content was kept and needs your review. + +RustGIS remembers up to 32 distinct saved snapshots for explicit project reopening, with at most +1 MiB of restoration data per snapshot and 8 MiB in total. A snapshot must match, and only its +recorded, unchanged fields can regain credentials. Larger restoration records are not remembered; +a notice says so. Imports, packages and peer exchanges do not use this memory. After eviction or +closing RustGIS, sign in again or add the protected sources with their credentials. Feature data +and user text remain data, so review them before sharing. ### Complete streamed Shapefile rendering diff --git a/docs/user-guide/web-data.md b/docs/user-guide/web-data.md index d09d2b8d43..84f75a7fad 100644 --- a/docs/user-guide/web-data.md +++ b/docs/user-guide/web-data.md @@ -495,6 +495,14 @@ 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`. +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. +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. + ### Layers that change with time A WMS or WMTS layer with a time dimension, from any service, goes on the diff --git a/docs/user-guide/workspace.md b/docs/user-guide/workspace.md index f9111a45e6..fcd8b71172 100644 --- a/docs/user-guide/workspace.md +++ b/docs/user-guide/workspace.md @@ -32,9 +32,12 @@ 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. -Saved layer addresses omit passwords, access keys, tokens and signatures. The open session -keeps them, but after restarting RustGIS a protected source may need you to sign in to its -connection again or add it again with credentials. +Saved layer and video addresses, and recognized task, model, notebook and history parameters, +omit 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 +[Sharing projects and generated items](https://github.com/opengeos/RustGIS/blob/main/SECURITY.md#sharing-projects-and-generated-items) +before sharing a file. ### Autosave and recovery From 5d1df586c3dbbbf3c1b156e4d04f534b25853d9e Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 4 Nov 2026 16:33:03 -0600 Subject: [PATCH 02/14] fix(security): bind approvals to exact execution scopes Centralize command and tool policy for untrusted callers. Bind approvals to parameters, effective targets, content, schemas and current runtime identity. Carry originating authority through deferred work and keep item trust tied to unchanged executable content. --- AGENTS.md | 4 + apps/rustgis-web/src/arcgis.rs | 20 +- apps/rustgis-web/src/geolibre.rs | 29 +- apps/rustgis-web/src/main.rs | 2 + apps/rustgis-web/src/request.rs | 179 +++ apps/rustgis-web/src/web.rs | 244 +-- crates/analysis/src/lib.rs | 161 +- crates/analysis/tests/resolved_tools.rs | 119 ++ crates/assistant/Cargo.toml | 4 + crates/assistant/src/acp.rs | 300 +++- crates/assistant/src/agent.rs | 96 +- crates/assistant/src/filebridge.rs | 39 +- crates/assistant/src/mcp.rs | 234 +-- crates/assistant/src/relay.rs | 260 +++- crates/assistant/src/relay/http.rs | 1 + crates/assistant/src/tests.rs | 486 +++++- crates/assistant/src/tools.rs | 186 +-- crates/data/src/source/remote.rs | 136 +- crates/engine/Cargo.toml | 2 + crates/engine/src/cmd/arcgis.rs | 10 +- crates/engine/src/cmd/catalogs.rs | 6 +- crates/engine/src/cmd/clipboard.rs | 24 +- crates/engine/src/cmd/connections.rs | 22 +- crates/engine/src/cmd/cube.rs | 142 +- crates/engine/src/cmd/cube_catalog.rs | 4 +- crates/engine/src/cmd/deeplearning.rs | 32 +- crates/engine/src/cmd/design.rs | 190 ++- crates/engine/src/cmd/directions.rs | 2 +- crates/engine/src/cmd/earthdata.rs | 10 +- crates/engine/src/cmd/edit.rs | 14 +- crates/engine/src/cmd/effects.rs | 78 +- crates/engine/src/cmd/embeddings.rs | 6 +- crates/engine/src/cmd/geocode.rs | 17 +- crates/engine/src/cmd/geolibre.rs | 23 +- crates/engine/src/cmd/gp.rs | 396 +++-- crates/engine/src/cmd/gpkg.rs | 8 +- crates/engine/src/cmd/graphics.rs | 10 +- crates/engine/src/cmd/group.rs | 178 ++- crates/engine/src/cmd/history.rs | 7 +- crates/engine/src/cmd/imagery.rs | 190 ++- crates/engine/src/cmd/items.rs | 257 ++-- crates/engine/src/cmd/labels.rs | 30 +- crates/engine/src/cmd/layer.rs | 109 +- crates/engine/src/cmd/layer_copy.rs | 116 +- crates/engine/src/cmd/layout.rs | 559 +++++-- crates/engine/src/cmd/mod.rs | 158 ++ crates/engine/src/cmd/navigate.rs | 170 ++- crates/engine/src/cmd/nldi.rs | 10 +- crates/engine/src/cmd/notebook.rs | 43 +- crates/engine/src/cmd/pg.rs | 16 +- crates/engine/src/cmd/plugin_fetch.rs | 11 +- crates/engine/src/cmd/plugins.rs | 246 +-- crates/engine/src/cmd/portals.rs | 12 +- crates/engine/src/cmd/presentation.rs | 102 +- crates/engine/src/cmd/project.rs | 60 +- crates/engine/src/cmd/qgis.rs | 4 +- crates/engine/src/cmd/raster.rs | 40 +- crates/engine/src/cmd/repair.rs | 4 +- crates/engine/src/cmd/report.rs | 354 ++++- crates/engine/src/cmd/s3.rs | 8 +- crates/engine/src/cmd/search.rs | 2 +- crates/engine/src/cmd/select.rs | 26 +- crates/engine/src/cmd/select_info.rs | 2 +- crates/engine/src/cmd/service.rs | 8 +- crates/engine/src/cmd/service_group.rs | 8 +- crates/engine/src/cmd/share.rs | 6 +- crates/engine/src/cmd/sharing.rs | 324 +++- crates/engine/src/cmd/sources.rs | 8 +- crates/engine/src/cmd/sql.rs | 4 +- crates/engine/src/cmd/stac.rs | 10 +- crates/engine/src/cmd/tiles.rs | 18 +- crates/engine/src/cmd/time.rs | 32 +- crates/engine/src/cmd/view.rs | 24 +- crates/engine/src/cmd/webdata.rs | 6 +- crates/engine/src/cmd/workflows.rs | 198 ++- crates/engine/src/cmd/workspace.rs | 63 +- crates/engine/src/cmd/zarr.rs | 147 +- crates/engine/src/lib.rs | 207 ++- crates/engine/src/model.rs | 36 +- crates/engine/src/peer.rs | 4 + crates/engine/src/policy/decide.rs | 188 +++ crates/engine/src/policy/dispatch.rs | 886 +++++++++++ crates/engine/src/policy/downloads.rs | 219 +++ crates/engine/src/policy/effects.rs | 111 ++ crates/engine/src/policy/handler.rs | 20 + crates/engine/src/policy/mod.rs | 47 + crates/engine/src/policy/plan.rs | 1405 ++++++++++++++++++ crates/engine/src/policy/provenance.rs | 99 ++ crates/engine/src/policy/scope.rs | 227 +++ crates/engine/src/project_log.rs | 59 + crates/engine/src/roles.rs | 113 +- crates/engine/src/tests.rs | 279 +++- crates/engine/src/tests/credential_output.rs | 4 +- crates/engine/src/tests/permission_policy.rs | 907 +++++++++++ crates/geolibre/src/lib.rs | 56 +- crates/geolibre/src/native.rs | 9 +- crates/geolibre/src/runner.rs | 76 +- crates/map/src/source.rs | 143 +- crates/plugins/src/lib.rs | 11 + crates/plugins/src/tests.rs | 93 +- crates/plugins/src/tools.rs | 42 +- crates/render/src/layout.rs | 7 +- crates/tiles/src/arcgis_auth.rs | 264 +++- crates/tiles/src/arcgis_auth/tests.rs | 49 + crates/tiles/src/cache.rs | 115 +- crates/tiles/src/earthdata.rs | 64 + crates/ui-egui/src/agentthreads.rs | 56 +- crates/ui-egui/src/analysispane.rs | 8 +- crates/ui-egui/src/approvalcard.rs | 93 ++ crates/ui-egui/src/assistantpane.rs | 135 +- crates/ui-egui/src/geolibreui.rs | 116 +- crates/ui-egui/src/lib.rs | 330 ++-- crates/ui-egui/src/localagents.rs | 266 +++- crates/ui-egui/src/modelbuilder.rs | 2 +- crates/ui-egui/src/panes.rs | 6 +- crates/ui-egui/src/pluginpanel.rs | 44 + crates/ui-egui/src/serviceui.rs | 53 +- docs/mcp.md | 5 + docs/plugins.md | 8 +- docs/user-guide/analysis.md | 2 +- docs/user-guide/assistant.md | 29 +- docs/user-guide/workspace.md | 4 +- 122 files changed, 11530 insertions(+), 2433 deletions(-) create mode 100644 apps/rustgis-web/src/request.rs create mode 100644 crates/analysis/tests/resolved_tools.rs create mode 100644 crates/engine/src/policy/decide.rs create mode 100644 crates/engine/src/policy/dispatch.rs create mode 100644 crates/engine/src/policy/downloads.rs create mode 100644 crates/engine/src/policy/effects.rs create mode 100644 crates/engine/src/policy/handler.rs create mode 100644 crates/engine/src/policy/mod.rs create mode 100644 crates/engine/src/policy/plan.rs create mode 100644 crates/engine/src/policy/provenance.rs create mode 100644 crates/engine/src/policy/scope.rs create mode 100644 crates/engine/src/tests/permission_policy.rs create mode 100644 crates/ui-egui/src/approvalcard.rs diff --git a/AGENTS.md b/AGENTS.md index 66d84b51c6..0d6f427b9b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -41,6 +41,10 @@ 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`. Generic embedding-parent `ui.*` RUN authority remains deferred D12. - **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/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..ddb24b44cb 100644 --- a/apps/rustgis-web/src/geolibre.rs +++ b/apps/rustgis-web/src/geolibre.rs @@ -17,10 +17,12 @@ use wasm_bindgen::prelude::*; #[wasm_bindgen(inline_js = r#" const BASE = "https://cdn.jsdelivr.net/npm/geolibre-wasm@1.8.0"; let ready = null; -function tools() { +function tools(still = () => true) { if (!ready) { ready = (async () => { + if (!still()) throw new Error("GeoLibre run cancelled before loading"); const mod = await import(`${BASE}/tools.mjs/+esm`); + if (!still()) throw new Error("GeoLibre run cancelled before initialization"); await mod.initTools(`${BASE}/geolibre-cli.wasm`); return mod; })(); @@ -66,8 +68,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 +83,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 +124,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..72cfb2edae --- /dev/null +++ b/apps/rustgis-web/src/request.rs @@ -0,0 +1,179 @@ +//! 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 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 0970278444..e793beb36a 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -579,65 +579,83 @@ async fn unless_stalled( /// `rustgis_tiles::cache::split_range`). /// A status in `omit` is an expected answer (a tile past a map's edge): its failure isn't /// recorded, only noted once per server, as on the desktop. -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 err = |e: wasm_bindgen::JsValue| format!("{e:?}"); - let window = web_sys::window().ok_or("no window")?; use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache; - // 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). - let prepared = rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::s3::prepare(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 arcgis = - if prepared.signed { None } else { rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::arcgis_auth::with_token(url) }; - // Only the request carries it: messages name the address without it. - let request = arcgis.as_deref().unwrap_or(url); - 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 err = |e: wasm_bindgen::JsValue| format!("{e:?}"); + let (promise, abort, range, tail, max, url) = crate::request::prepare_and_send( + &still, + || async { + let window = web_sys::window().ok_or("no window")?; + use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache; + // 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). + let prepared = rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::s3::prepare(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; + } + let arcgis = if prepared.signed { + None + } else { + rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::arcgis_auth::with_token(url) + }; + // Only the request carries it: messages name the address without it. + let request = arcgis.as_deref().unwrap_or(url); + 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() { + 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 { + headers.set(k, v).map_err(err)?; + } + init.set_headers(&headers); + } + // A signed or authorized request goes only where its credentials are for. + if prepared.signed || arcgis.is_some() { + init.set_redirect(web_sys::RequestRedirect::Error); + } + // The token's address goes nowhere else: no referrer from it, no cookies with it. + if arcgis.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)) + }, + |(window, request, init, abort, range, tail, max, url)| { + (window.fetch_with_str_and_init(&request, &init), abort, range, tail, max, url) + }, + ) + .await?; let stalled = || format!("nothing came in {} seconds", STALL_MS / 1000); - if asked.is_some() || !prepared.headers.is_empty() { - 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 { - headers.set(k, v).map_err(err)?; - } - init.set_headers(&headers); - } - // A signed or authorized request goes only where its credentials are for. - if prepared.signed || arcgis.is_some() { - init.set_redirect(web_sys::RequestRedirect::Error); - } - // The token's address goes nowhere else: no referrer from it, no cookies with it. - if arcgis.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 refused fetch is an opaque TypeError ("Failed to fetch") by design: the browser doesn't // say why, so the likely reasons are named, with the browser's own message but not its stack. let resp: web_sys::Response = unless_stalled(promise, abort.as_ref()) @@ -731,22 +749,35 @@ 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; let err = |e: wasm_bindgen::JsValue| format!("{e:?}"); - let window = web_sys::window().ok_or("no window")?; - 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)?; - headers.set("Content-Type", "application/json").map_err(err)?; - headers.set("Accept", "application/json").map_err(err)?; - init.set_headers(&headers); - init.set_body(&wasm_bindgen::JsValue::from_str(body)); - 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 { + let window = web_sys::window().ok_or("no window")?; + 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)?; + headers.set("Content-Type", "application/json").map_err(err)?; + headers.set("Accept", "application/json").map_err(err)?; + init.set_headers(&headers); + init.set_body(&wasm_bindgen::JsValue::from_str(body)); + Ok((window, init)) + }, + |(window, init)| window.fetch_with_str_and_init(url, &init), + ) + .await?; + 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() @@ -769,28 +800,42 @@ 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, s3}; + use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache; let err = |e: wasm_bindgen::JsValue| format!("{e:?}"); - let window = web_sys::window().ok_or("no window")?; - let prepared = s3::prepare(url)?; - // A request signed for S3 is signed for a GET. - if prepared.signed { - return Ok(cache::Head::default()); - } - let init = web_sys::RequestInit::new(); - init.set_method("HEAD"); - if !prepared.headers.is_empty() { - let headers = web_sys::Headers::new().map_err(err)?; - for (k, v) in &prepared.headers { - headers.set(k, v).map_err(err)?; - } - init.set_headers(&headers); - } - let resp: web_sys::Response = wasm_bindgen_futures::JsFuture::from(window.fetch_with_str_and_init(&prepared.url, &init)) + let promise = crate::request::prepare_and_send( + &still, + || async { + use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::s3; + let window = web_sys::window().ok_or("no window")?; + let prepared = s3::prepare(url)?; + // A request signed for S3 is signed for a GET. + if prepared.signed { + return Ok(None); + } + let init = web_sys::RequestInit::new(); + init.set_method("HEAD"); + if !prepared.headers.is_empty() { + let headers = web_sys::Headers::new().map_err(err)?; + for (k, v) in &prepared.headers { + headers.set(k, v).map_err(err)?; + } + init.set_headers(&headers); + } + Ok(Some((window, prepared.url, init))) + }, + |request| request.map(|(window, url, init)| window.fetch_with_str_and_init(&url, &init)), + ) + .await?; + let Some(promise) = promise else { return Ok(cache::Head::default()) }; + 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() @@ -882,12 +927,10 @@ fn clipboard() -> 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| { @@ -904,21 +947,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(); @@ -975,7 +1014,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(); @@ -1073,8 +1112,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/lib.rs b/crates/analysis/src/lib.rs index 2771d6a335..34b0a3ed49 100644 --- a/crates/analysis/src/lib.rs +++ b/crates/analysis/src/lib.rs @@ -225,6 +225,7 @@ struct Source { provider: ExternalProvider, runner: ExternalRunner, tools: Option<&'static [ToolSpec]>, + generation: u64, } static SOURCES: std::sync::RwLock> = std::sync::RwLock::new(Vec::new()); @@ -236,43 +237,39 @@ 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 => &[], }, - Err(_) => tools, + Err(_) => &[], } } @@ -287,6 +284,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); } } } @@ -298,6 +296,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); } } @@ -335,6 +334,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()) @@ -344,9 +432,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); } @@ -472,6 +563,24 @@ 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` 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..3fcc67b203 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,154 @@ 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 => { + events.extend(self.refuse_mode("RustGIS can't confirm that the agent asks before acting")); + 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 +661,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 +713,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 +803,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 9f9583ccb0..b707b1ce95 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,11 +82,6 @@ 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"]; - impl Server { /// A server with the built-in skills. `rustgis-cli mcp` and the desktop's relay replace them /// with the skills switched on (`skills::installed`): the user's, the plugins' and shared ones. @@ -96,6 +92,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 +115,26 @@ 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().filter_map(|m| self.handle_value(s, m, &scope, &mut permits, request)).collect(); if replies.is_empty() { return None; } Value::Array(replies) } - _ => self.handle_value(s, &msg)?, + _ => self.handle_value(s, &msg, &scope, &mut permits, request)?, }; 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 +151,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, &format!("{request}/{}", id)) { 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 +199,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 +230,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 +240,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 +253,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 +423,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 +439,46 @@ 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. +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 +486,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), @@ -510,3 +533,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..cc2d26a6b4 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(); } @@ -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,34 @@ 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: format!("{}/{}", req.id, id), 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 +934,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 +968,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 +1302,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 +1458,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(); + let mut plans = Vec::new(); for msg in messages { - if msg.get("method").and_then(Value::as_str) != Some("tools/call") || msg.get("id").is_none() { + 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: format!("{request}/{id}"), 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 +1518,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 +1591,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 +1618,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,6 +1661,7 @@ 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")); @@ -2104,6 +2237,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 +2799,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 7a4c514cf0..c1e80d25a5 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); @@ -313,6 +332,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(); @@ -332,7 +416,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()); @@ -343,23 +427,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"); @@ -375,13 +463,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}"); @@ -397,7 +485,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}"); } @@ -409,56 +497,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); @@ -473,10 +562,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; @@ -503,7 +592,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(); @@ -515,7 +604,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}"); @@ -523,20 +612,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}"); @@ -546,8 +635,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 @@ -561,31 +650,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}"); } @@ -593,15 +682,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})), ) @@ -611,9 +700,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}"); } @@ -645,13 +734,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] @@ -881,7 +970,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:?}"); @@ -938,13 +1027,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] @@ -1452,13 +1550,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); } @@ -2246,12 +2344,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, @@ -2269,7 +2368,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(), @@ -2349,15 +2448,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] @@ -3178,7 +3277,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); @@ -3222,7 +3323,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")); @@ -3261,7 +3364,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")) @@ -3294,7 +3397,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"))); @@ -3324,6 +3427,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"); @@ -3335,13 +3441,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"}}"#); @@ -3800,7 +3996,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()); @@ -4263,7 +4459,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}"); @@ -4318,3 +4514,141 @@ fn peer_replacement_cancels_approvals_and_stale_replies() { let _ = agent.receive(generation, ok(say("stale")), &mut s); assert!(!agent.busy() && agent.pending(&s).is_none()); } + +/// 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 c25b42dffd..afa18af2ca 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,12 +217,35 @@ 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 { + 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(bounded_json(&with_notices(with_new_layers(result, s, &before), s, mark), tokens)); + } + if call.name == "run" { + return run_script(s, call, tokens, scope, permit); + } + if permit.is_some() { + return Err("this approval is for an executable call".into()); + } + s.with_scope(scope, |s| run_read(s, call, tokens))? +} + +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() { @@ -269,27 +256,9 @@ pub fn run_with_budget(s: &mut Session, call: &ToolCall, tokens: usize) -> Resul "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))?; - with_notices(with_new_layers(r, s, &before), s, mark) - } - "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))?; - with_notices(with_new_layers(r, s, &before), s, mark) - } "read_features" => read_features(s, a)?, // MCP's `run` (not offered to the in-app assistant): a script in one call. - "run" => return run_script(s, a, tokens), + "search_docs" => crate::docs::search(&str_arg(a, "query").unwrap_or_default(), limit_arg(a, 8, 30))?, "read_doc" => { return crate::docs::read_with_budget( @@ -434,7 +403,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") @@ -443,7 +413,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 { @@ -546,7 +520,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]); @@ -560,7 +534,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() diff --git a/crates/data/src/source/remote.rs b/crates/data/src/source/remote.rs index 6cf7fd3f79..2dd6fdf6e6 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)); + }) + }); let f = fetcher().lock().unwrap_or_else(|e| e.into_inner()).clone(); match f { Some(f) => f(url, Want::Whole, done), @@ -419,23 +464,41 @@ 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; + } let r = { let _running = Running::enter(drive); work() }; 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. @@ -461,42 +524,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 3d7fc6ab97..08b7034595 100644 --- a/crates/engine/src/cmd/arcgis.rs +++ b/crates/engine/src/cmd/arcgis.rs @@ -10,22 +10,22 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("arcgis.signIn", "Sign In to ArcGIS", "Home › Add › Service", sign_in).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)).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).view().params( + C::new("arcgis.token", "Use ArcGIS Token", "Home › Add › Service", token).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).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) + C::new("arcgis.signOut", "Sign Out of ArcGIS", "Home › Add › Service", sign_out).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)) .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(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("arcgis.status", "ArcGIS Sign-In Status", "Home › Add › Service", |_s, _p| Ok(status())).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 3e13c7d135..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).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) + 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).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 5d67bfd752..d9e4c1b08b 100644 --- a/crates/engine/src/cmd/clipboard.rs +++ b/crates/engine/src/cmd/clipboard.rs @@ -20,15 +20,33 @@ pub struct Clipboard { pub fn commands() -> Vec { vec![ - C::new("edit.copy", "Copy", "Edit › Clipboard", |s, _| copy(s)).view().key("Mod+C").when(need_selection), - C::new("edit.cut", "Cut", "Edit › Clipboard", cut).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).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 1ebe809452..37086eee49 100644 --- a/crates/engine/src/cmd/connections.rs +++ b/crates/engine/src/cmd/connections.rs @@ -24,31 +24,31 @@ mod connection_use; pub fn commands() -> Vec { vec![ - C::new("connection.list", "List Connections", "Home › Add › Data Sources", list).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).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)}"#, ), - C::new("connection.edit", "Edit Connection", "Home › Add › Data Sources", edit).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)", "kind"?: "its kind (unchanged)"}"#, ), - C::new("connection.duplicate", "Duplicate Connection", "Home › Add › Data Sources", duplicate).view().params(r#"{"id": "a connection's id or name", "name"?: text}"#), - C::new("connection.remove", "Remove Connection", "Home › Add › Data Sources", remove).view().params(r#"{"id": "a connection's id or name"}"#), - C::new("connection.login", "Log In to Connection", "Home › Add › Data Sources", login_command).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"}"#), + 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 or saved credentials", "password"?: "a database password, session-only by default", "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). No credentials are stored in connections or projects; a failed save preserves the session login"}"#, ), - C::new("connection.forgetSavedKeys", "Forget Saved Keys", "Home › Add › Data Sources", forget_saved_keys).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).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}: explicitly grants this bucket and opens only its waiting sources"#, ), - C::new("connection.forgetPassword", "Forget Saved Password", "Home › Add › Data Sources", forget_password).view().params(r#"{"id": "a database connection's id or name"}"#), - C::new("connection.test", "Test Connection", "Home › Add › Data Sources", test).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)", "password"?: "for a named database, authentication refusal restores prior session credentials; a later listing failure keeps the new password; never persisted", "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).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 856867411c..0f3aa34984 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).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).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: &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: &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 98fe7a617f..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).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).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 0af98d2dd6..23017aa771 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) - }) + }).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).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(&[]).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(&[]).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(&[]).effects(crate::policy::Effects::NONE) .when(need_model) .params(r#"{"model": name}"#), - C::new("imagery.hostedImagery", "Create Hosted Imagery", "Analyze › Imagery Sharing", hosted).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}'")), @@ -183,7 +185,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) @@ -471,3 +473,17 @@ 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)?]) +} +/// Binds the raster and actual named/last model choice, without copying trained weights. +fn classify_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + 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 a6dea36096..1144135a94 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) } @@ -225,16 +225,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).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).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).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).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| { @@ -243,7 +243,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(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "on"?: bool}"#), C::new("data.archiveHistory", "Archive History", "Data › Archiving", |s, p| { @@ -257,23 +257,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})) - }).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) + 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).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) + 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(&[]).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; @@ -281,7 +281,7 @@ pub fn commands() -> Vec { set.show_vertices = v; } Ok(json!({"enabled": set.enabled, "showVertices": set.show_vertices})) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"showVertices"?: bool}"#), C::new("edit.status", "Edit Status", "Edit › Manage Edits", |s, _| { let rows: Vec = s @@ -290,9 +290,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(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("edit.template", "Manage Templates", "Edit › Manage Edits", template).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. @@ -308,9 +308,9 @@ pub fn commands() -> Vec { } } Ok(json!({"enabled": sn.enabled, "tolerancePx": sn.tolerance_px, "vertex": sn.vertex, "edge": sn.edge, "end": sn.end})) - }) + }).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).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)?; @@ -322,7 +322,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(&[]).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| { @@ -342,7 +342,7 @@ pub fn commands() -> Vec { merc_to_lonlat(Coord::new(c.x + x * cos - y * sin, c.y + x * sin + y * cos)) })) }) - }) + }).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| { @@ -358,7 +358,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(&[]).effects(crate::policy::Effects::NONE) .when(need_editable_selection) .params(r#"{"factor": number}"#), C::new("edit.flip", "Flip Line", "Edit › Features › Modify", |s, _| { @@ -366,36 +366,36 @@ pub fn commands() -> Vec { Geometry::Line(parts) => Some(Geometry::Line(parts.iter().rev().map(|p| p.iter().rev().copied().collect()).collect())), _ => None, }) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_editable_selection), - C::new("edit.setGeometry", "Set Geometry", "Edit › Features › Modify", set_geometry) + 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).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).when(need_editable_selection), - C::new("edit.moveSharedVertex", "Move Shared Vertex", "Edit › Features › Modify", shared_vertex::run).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).when(need_editable_selection), - C::new("edit.split", "Split", "Edit › Features › Modify", split).when(need_editable_selection).params(r#"{"lon", "lat"}"#), - C::new("edit.cutPolygon", "Cut Polygon", "Edit › Features › Modify", sketch::cut_polygon) + 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) + 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).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).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(&[]).effects(crate::policy::Effects::NONE) .view(), // Elevation and corrections. C::new("edit.elevation", "Elevation", "Edit › Elevation", |s, p| { @@ -411,14 +411,14 @@ pub fn commands() -> Vec { }, "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 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) - }) + }).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); @@ -429,13 +429,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(&[]).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).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).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)}"#), ] } @@ -1030,9 +1030,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 }); @@ -1041,18 +1041,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()); } @@ -1245,8 +1234,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")?; @@ -1321,3 +1311,99 @@ 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()) +} +/// Only surface mode consumes a layer; constant and none are parameter-only. +fn elevation_inputs(input: &super::InputContext<'_>, p: &Json) -> Result { + let s = input; + Ok(super::Inputs::layers( + if ps(p, "mode") == Some("surface") { + surface_layer(input, ps(p, "surface").ok_or("surface (a raster layer) is required")?) + } else { + None + } + .map(|l| (s.map(), l)), + )) +} +/// 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/directions.rs b/crates/engine/src/cmd/directions.rs index adf38de1f2..08ddf283bb 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).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 b9f6e67ce9..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) + 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).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).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) + 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(&[]).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 44ecad79fb..053827fb87 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) + 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) + 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).key("Delete").when(super::design::need_editable_selection), - C::new("edit.setValue", "Edit Attribute", "Attribute Table", set_value) + 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) + 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) + 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) + 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 64c4eea5cc..56a92c9e8d 100644 --- a/crates/engine/src/cmd/effects.rs +++ b/crates/engine/src/cmd/effects.rs @@ -33,28 +33,18 @@ pub fn commands() -> Vec { let mode = Blend::from_code(m).ok_or_else(|| format!("unknown blend mode '{m}'"))?; 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| { @@ -70,6 +60,8 @@ 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| { @@ -80,9 +72,13 @@ pub fn commands() -> Vec { rl.style.rotation = a.rem_euclid(360.0); 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| { @@ -99,11 +95,17 @@ 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).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| { @@ -115,6 +117,8 @@ 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| { @@ -128,6 +132,8 @@ pub fn commands() -> Vec { } }) }) + .roles(&[]) + .effects(crate::policy::Effects::NONE) .when(need_polygons) .params(r#"{"layer"?, "lighting"?: bool, "shadows"?: bool}"#), ] @@ -201,3 +207,37 @@ fn change_extrusion(s: &mut Session, p: &Value, f: impl FnOnce(&mut Extrusion)) f(x); Ok(Value::Null) } + +/// Binds the primary target and polygon mask selected by the actual mask validator. +fn mask_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { + let s = input; + let mut inputs = super::Inputs::primary(s, p)?; + let id = s.layer_id(p)?; + if let Some(n) = mask_of(input, p, id)? { + inputs.layers.extend(super::Inputs::named(s.map(), [n.as_str()])?.layers); + } + Ok(inputs) +} + +/// Validates a literal polygon mask, including the existing self-mask refusal. +fn mask_of(input: &impl super::InputSource, p: &Value, id: u32) -> Result, String> { + let context = input.input_context(); + let s = &context; + 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()), + }; + Ok(mask) +} diff --git a/crates/engine/src/cmd/embeddings.rs b/crates/engine/src/cmd/embeddings.rs index 7013a83a47..cc50d2b13e 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(&[]).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).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).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/geocode.rs b/crates/engine/src/cmd/geocode.rs index 5bc5992e81..b8a01c61a1 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).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", "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).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", "url"?: "a geocoding server of your own (more than 100 addresses)", "name"?: text, "places"?: "answers already looked up, {address: {lon, lat, name} | null} (the app's background run; nothing is fetched)"}"#, ), ] @@ -62,7 +62,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)?; @@ -274,3 +276,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 1b4f3a9901..83a09f8c3c 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).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,7 +64,7 @@ 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(); @@ -291,7 +291,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"); @@ -339,8 +339,17 @@ fn add_layer(s: &mut Session, r: &Value, dir: Option<&str>, report: &mut Report, let remote = rustgis_map::tiles::is_web(&address); 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")] + { + true + } + #[cfg(not(target_arch = "wasm32"))] + { + s.leave_downloads + } + }; + if defer && remote && kind != "cog" && !(kind == "raster" && super::layer::is_tiff_url(&address)) { // 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(); @@ -362,7 +371,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)); } @@ -627,7 +636,7 @@ 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(); diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 7fd5045ffb..f1b463e7c6 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).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) + 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.with(crate::policy::Effect::Files)) .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) + C::new("gp.zonalStats", "Zonal Statistics", "Analyze › Raster", zonal_stats).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .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).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(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("gp.geolibre", "GeoLibre Tools", "Analyze › Geoprocessing", |_, _| Ok(geolibre_status())).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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)) .view(), - C::new("gp.uninstallGeolibre", "Uninstall GeoLibre Tools", "Analyze › Geoprocessing", |_, _| uninstall_geolibre()).view(), - C::new("gp.finish", "Finish Tool Run", "Analyze › Geoprocessing", finish) + 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) + 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) + 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(&[]).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"))] @@ -533,7 +613,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; } @@ -578,6 +658,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(), @@ -655,8 +748,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 @@ -745,10 +842,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); @@ -804,7 +901,7 @@ fn streamed_inputs( /// 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), @@ -904,9 +1001,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) @@ -949,9 +1044,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. @@ -990,7 +1091,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) @@ -1020,27 +1126,53 @@ 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(); 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 inputs = map_inputs(s, rustgis_analysis::tool(&tool), ¶ms)?; - let mut res = rustgis_analysis::run_with(&tool, &inputs, ¶ms, control); + let continuation = s.tool_continuation(&op); + 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_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()); } let ids = full_ids(&inputs.loaded); drop(inputs); + pin.current()?; fit_to_windows(s, &mut res, &ids); let millis = now_ms().saturating_sub(t0); - let spec = rustgis_analysis::tool(&tool); - let name = spec.map(|t| t.name).unwrap_or("Tool").to_string(); - apply(s, Ran { name, id: tool, command: None, started: t0 }, recorded(spec, params), res, millis) + 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. @@ -1055,6 +1187,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>, @@ -1075,7 +1208,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})) } @@ -1084,6 +1217,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.continuation_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()); } @@ -1188,6 +1322,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 { @@ -1373,6 +1509,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(), @@ -1442,12 +1590,43 @@ mod file_tests { assert_eq!(s.undo_depth(), depth); } + /// 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; + impl crate::policy::Revocation for Live { + fn allows(&self, _: crate::policy::Effects) -> bool { + true + } + } + let mut s = task_session(json!([])); + let owner = crate::policy::Scope::agent( + crate::policy::AgentAccess::Allow, + false, + "file-owner", + Some("file-owner".into()), + std::sync::Arc::new(Live), + ); + let foreign = + crate::policy::Scope::agent(crate::policy::AgentAccess::Allow, false, "file-foreign", None, std::sync::Arc::new(Live)); + 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, &crate::policy::Call::command("gp.fileDelivered", json!({"id":id}))).is_err()); + 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, &crate::policy::Call::command("edit.undo", Value::Null)).unwrap(); + } + #[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()); @@ -2009,15 +2188,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()); @@ -2027,7 +2211,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]; @@ -2043,7 +2227,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)); @@ -2060,7 +2244,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")] { @@ -2069,7 +2253,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))); } @@ -2081,3 +2265,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 0c3959dfbc..c1a409e80d 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).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).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) + 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) + 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 88a203ca7c..4261342fa7 100644 --- a/crates/engine/src/cmd/graphics.rs +++ b/crates/engine/src/cmd/graphics.rs @@ -22,21 +22,21 @@ pub fn commands() -> Vec { 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(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name"?: string}"#), - C::new("graphics.add", "Add Graphic", "Edit › Graphics", add).params( + C::new("graphics.add", "Add Graphic", "Edit › Graphics", add).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).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) + 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).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(&[]).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 1c6063c311..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).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).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) + 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).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).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).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) + 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) + 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) + 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).params(r#"{"group": name|id} (and its layers and subgroups)"#), - C::new("layer.group", "Move To Group", "Layers", move_to_group) + 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) + 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) + 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 aa3fab750f..087a07ac35 100644 --- a/crates/engine/src/cmd/history.rs +++ b/crates/engine/src/cmd/history.rs @@ -14,9 +14,14 @@ use crate::{CmdResult, CommandSpec as C, GpRecord, Session}; 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).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 69a905e223..122cb78cca 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -38,39 +38,39 @@ fn first() -> String { pub fn commands() -> Vec { vec![ - C::new("raster.georeference", "Georeference", "Analyze › Alignment", georeference).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).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}"#, ), // 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).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) + 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"?}"#), - C::new("raster.spectral", "Spectral Analysis", "Analyze › Image Analysis", spectral) + 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, super::workflows::CLASSIFY, classify_work) - }) + }).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) + 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}"#), - C::new("raster.index", "Indices", "Analyze › Imagery Tools", index).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}"#, ), - C::new("raster.process", "Process", "Analyze › Imagery Tools", process) + 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}"#), - C::new("raster.mosaic", "Mosaic Rasters", "Analyze › Imagery Tools", raster_mosaic).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) + 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).view().params( + C::new("raster.measure", "Mensuration Tools", "Analyze › Mensuration", measure).inputs(raster_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| { @@ -78,10 +78,10 @@ pub fn commands() -> Vec { s.measurements.clear(); } Ok(Value::Array(s.measurements.clone())) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"clear"?: bool}"#), - C::new("imagery.workspace", "New Workspace", "Analyze › Ortho Mapping", new_workspace).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| { @@ -94,21 +94,23 @@ pub fn commands() -> Vec { w.method = method(p, "first")?; } serde_json::to_value(&*w).map_err(|e| e.to_string()) - }) + }).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).params(r#"{"name": workspace}"#), - C::new("imagery.mesh", "Export Mesh", "Analyze › Reality Mapping", mesh) + C::new("imagery.mosaic", "Build Mosaic", "Analyze › Ortho Mapping", mosaic).inputs(workspace_mosaic_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"name": workspace}"#), + 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 { @@ -372,10 +374,7 @@ fn project_raster(s: &mut Session, p: &Value) -> CmdResult { Some(v) => v.as_str().and_then(warp::Method::parse).ok_or("resampling is nearest, bilinear, cubic, average or mode")?, }; // 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)?; let grid = match template { Some(template) => { if ["crs", "cellSize", "origin", "extent"].iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())) { @@ -413,7 +412,8 @@ fn project_raster(s: &mut Session, p: &Value) -> CmdResult { }; 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() }; @@ -569,8 +569,9 @@ fn gcp_fit(_: &mut Session, p: &Value) -> CmdResult { } fn change(s: &mut Session, p: &Value) -> CmdResult { - 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"); if !matches!(method, "difference" | "ratio" | "percent") { @@ -599,7 +600,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) @@ -624,7 +625,7 @@ fn supervised(s: &mut Session, p: &Value) -> CmdResult { // Training polygons on the map mean nothing on a scan laid there to look at. r.require_coordinates()?; let bands = bands_param(p, &r)?; - let tl = ps(p, "training").ok_or("training (a polygon layer) is required")?; + let tl = training_name(p)?; let l = s .map() .layer_by_name(tl) @@ -706,7 +707,7 @@ fn process(s: &mut Session, p: &Value) -> CmdResult { "clip" if p.get("polygon").is_some_and(|v| !v.is_null()) => { // 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) @@ -880,8 +881,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 @@ -1324,16 +1325,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()); } @@ -1356,8 +1348,7 @@ fn new_workspace(s: &mut Session, p: &Value) -> CmdResult { /// Mosaics a workspace's images onto one longitude/latitude grid. fn mosaic(s: &mut Session, p: &Value) -> CmdResult { - 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 { @@ -1469,3 +1460,116 @@ 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)?]) +} +/// 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)))) +} +/// 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 82df1221e8..5ee2f69c01 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(&[]).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(&[]).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(&[]).effects(crate::policy::Effects::NONE) .view(), // Map and layer files. - C::new("map.save", "Save Map File", "Publish › Save As", |s, p| save_item(s, "map", p)) + 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).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)) + 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(&[]).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(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "name"?: string}"#), C::new("style.add", "Add Style", "Style › Styles", |s, p| { @@ -86,9 +86,9 @@ pub fn commands() -> Vec { n += 1; } Ok(json!({"added": n})) - }) + }).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).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}'"))?; @@ -112,7 +112,7 @@ pub fn commands() -> Vec { s.map_mut().label_lock_line_anchors.clear(); } Ok(Value::Null) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "name": style}"#), C::new("style.remove", "Delete Style", "Browser", |s, p| { @@ -120,9 +120,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(&[]).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)) + 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. @@ -155,7 +155,7 @@ pub fn commands() -> Vec { s.project.ramps.push(r); } Ok(json!({"added": names})) - }) + }).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"}"#, ), @@ -164,16 +164,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(&[]).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)) + 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)) + 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)) + 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| { @@ -192,21 +192,21 @@ pub fn commands() -> Vec { super::connections::resolve_project(s); super::connections::note_waiting(s); Ok(Value::Null) - }) + }).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)) + 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)) + 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)) + 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) + 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| { @@ -240,12 +240,10 @@ pub fn commands() -> Vec { } // 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()); - } + let _ = from_file_history; 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 }]).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}"))?; @@ -258,7 +256,7 @@ pub fn commands() -> Vec { } t.steps.push(step); Ok(json!({"steps": t.steps.len()})) - }) + }).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. @@ -283,7 +281,7 @@ pub fn commands() -> Vec { st.title = title; st.instructions = instructions; Ok(json!({"step": step})) - }) + }).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") { @@ -301,7 +299,7 @@ pub fn commands() -> Vec { let st = t.steps.remove(step); t.steps.insert(to, st); Ok(json!({"step": to})) - }) + }).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)?; @@ -311,9 +309,9 @@ pub fn commands() -> Vec { } t.steps.remove(step); Ok(json!({"steps": t.steps.len()})) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"task"?: name, "step": number (from 0)}"#), - C::new("task.run", "Run Task", "Tasks", run_task).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(); @@ -321,24 +319,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(&[]).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)) + 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) }]).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) + 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}"#), @@ -357,16 +355,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()), @@ -623,25 +613,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) } @@ -673,7 +655,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()), @@ -703,8 +685,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 } @@ -728,16 +717,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) @@ -751,9 +740,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})); @@ -762,22 +754,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 @@ -794,7 +784,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); @@ -1024,10 +1014,7 @@ pub const ITEMS: &[(&str, &str, &str)] = &[ pub fn item_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Vec), String> { let (name, bytes) = match kind { "map" => { - 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 = saved_map(s, p)?; crate::project::ensure_map_imports_complete(map)?; let map = embedded_map(map, &mut MAX_EMBED.clone()); let name = map.name.clone(); @@ -1134,3 +1121,81 @@ fn durable_bytes(s: &mut Session, crate::durable::notify(s, &report); Ok(bytes) } + +/// 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 6a4ae98c7a..34a403c2e0 100644 --- a/crates/engine/src/cmd/labels.rs +++ b/crates/engine/src/cmd/labels.rs @@ -91,21 +91,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(&[]).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(&[]).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(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"on"?: bool}"#), C::new("labels.fonts", "Label Fonts", "View › Windows › Labeling", |_, p| { @@ -117,16 +117,16 @@ 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(&[]).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) + C::new("labels.convert", "Convert To Annotation", "Labeling › Map Labeling", convert).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "name"?: string}"#), - C::new("labels.scaleRange", "Label Visibility Range", "Labeling › Label Range", scale_range) + C::new("labels.scaleRange", "Label Visibility Range", "Labeling › Label Range", scale_range).roles(&[]).effects(crate::policy::Effects::NONE) .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) + C::new("labels.distanceRange", "Label Distance Range", "Labeling › Label Range", distance_range).roles(&[]).effects(crate::policy::Effects::NONE) .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| { @@ -182,14 +182,14 @@ pub fn commands() -> Vec { } Ok(()) }) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE) .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 or otherPositions), "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)}"#, ), // 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) + C::new("labels.class.add", "Add Label Class", "View › Windows › Labeling", add_class).roles(&[]).effects(crate::policy::Effects::NONE) .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| { @@ -199,7 +199,7 @@ pub fn commands() -> Vec { labels.classes.remove(i); Ok(()) }) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE) .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. @@ -211,7 +211,7 @@ pub fn commands() -> Vec { Ok(()) })?; Ok(json!({"removed": removed})) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?}"#), C::new("labels.class.rename", "Rename Label Class", "View › Windows › Labeling", |s, p| { @@ -222,7 +222,7 @@ pub fn commands() -> Vec { labels.class_mut(&class).ok_or_else(|| format!("no label class named '{class}'"))?.name = name; Ok(()) }) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_feature_layer) .params(r#"{"layer"?, "class": name, "name": string}"#), C::new("labels.class.update", "Update Label Class", "View › Windows › Labeling", |s, p| { @@ -237,7 +237,7 @@ pub fn commands() -> Vec { c.visible = v; } }) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE) .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| { @@ -250,10 +250,10 @@ pub fn commands() -> Vec { labels.classes.insert(to, c); Ok(()) }) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE) .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) + C::new("labels.by", "Labels by Expression", "View › Windows › Labeling", labels_by).roles(&[]).effects(crate::policy::Effects::NONE) .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 582bade011..4c7d43f543 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) + 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).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) + 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).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).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).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) + 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) + 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).when(need_streamed).params(r#"{"layer"?: name|id}"#), - C::new("layer.addUrl", "Add Data From URL", "Home › Add › Add Data", add_url).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|.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 zooms to what was added; false keeps the view of a GeoParquet or FlatGeobuf read in place (zoomed to only when the map was empty)", "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)", "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"}"#, ), - C::new("layer.addDownload", "Add Downloaded Data", "Home › Add", add_download) + 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 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)}"#), - C::new("layer.addEncodedPolyline", "Add Encoded Polyline", "Home › Add", add_encoded_polyline).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).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).params(r#"{"name": "countries"|"cities"|"rivers"|"lakes"}"#), - C::new("layer.remove", "Remove", "Layers", remove) + 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) + 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) - }).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) - }).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)).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)).params(r#"{"layer"?: name|id (default: the active layer)}"#).when(need_layer), - C::new("layer.move", "Move", "Layers", move_to) + 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) - }).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) + 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 { } } }) - }).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) + 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(&[]).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(&[]).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(&[]).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) + 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) + 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(&[]).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) + 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(&[]).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(&[]).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(&[]).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(&[]).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(&[]).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) - }).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)) - }).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(&[]).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(()) }) - }).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(&[]).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| { @@ -500,20 +500,20 @@ pub fn commands() -> Vec { rustgis_data::query::parse(&q, &l.dataset)?; } with(s, p, |l| l.definition_query = if q.is_empty() { None } else { Some(q.clone()) }) - }) + }).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) - }).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(&[]).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 @@ -524,13 +524,13 @@ pub fn commands() -> Vec { let i = s.table_index(p)?; s.project.tables.remove(i); Ok(Value::Null) - }) + }).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::>())) - }).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), ] @@ -2875,7 +2875,7 @@ pub fn url_file_name(url: &str, bytes: &[u8]) -> Result { /// request is kept for the app ([`Session::request_url`]), which downloads and adds it as its own /// Home › Add › Add Data › From URL does, so a plugin, a script or the assistant can add data by address too. pub(crate) fn add_url(s: &mut Session, p: &Value) -> CmdResult { - 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")?; @@ -3778,7 +3778,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() { @@ -4150,3 +4152,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 c05c8a58da..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).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).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 b53ea41c09..e1d7ccfb65 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).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).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).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})) - }) + }).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) - }) + }).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) - }) + }).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})) - }) + }).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})) - }) + }).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).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).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).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", "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), "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), "units"?: "auto"|"kilometers"|"meters"|"miles"|"feet", "divisions"?: 1..10 (scaleBar), "x"?, "y"?, "w"?, "h"?: mm}"##, ), - C::new("layout.update", "Element Properties", "Publish › Element", update).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), "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; for a rectangle or ellipse, color sets it when outline is not given), "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).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).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).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}")) } - }) + }).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).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).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).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"}))) + 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", export) + C::new("layout.export", "Export Layout", "Publish › Output", 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})) - }) + }).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(&[]).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; @@ -2229,18 +2178,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 { @@ -2308,14 +2245,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 @@ -2446,7 +2383,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()), @@ -2732,7 +2671,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 { @@ -2854,50 +2795,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")?; @@ -3185,16 +3083,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); @@ -3245,7 +3136,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()); @@ -3281,7 +3172,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) { @@ -3533,3 +3426,363 @@ 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.maps.push(map.instance_id()); + inputs.layers.extend(map.layers.iter().map(|l| (map.instance_id(), l.id))); + } + } + 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}); + 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 f7c4efd28f..6bfd2dba0c 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -194,3 +194,161 @@ pub(crate) fn need_layers(s: &Session) -> Option<&'static str> { 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 { + /// 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 86b2b24562..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).view(), - C::new("map.zoomIn", "Fixed Zoom In", "Home › Navigate", |s, _| zoom(s, 2.0)).view().key("Mod+="), - C::new("map.zoomOut", "Fixed Zoom Out", "Home › Navigate", |s, _| zoom(s, 0.5)).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(&[]).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(&[]).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(&[]).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(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("map.zoomToExtent", "Zoom To Extent", "Home › Navigate", zoom_to_extent) + 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) + 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) + 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(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("map.setBearing", "Rotate Map", "Home › Navigate", set_bearing) + 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) + 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(&[]).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(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"scale": number}"#), - C::new("map.basemap", "Basemap", "Home › Add", basemap) + 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(&[]).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(&[]).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(&[]).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(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("bookmark.add", "New Bookmark", "Home › Navigate › Bookmarks", add_bookmark).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) + 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) - }) + }).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})) - }) + }).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) - }) + }).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) - }) + }).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) - }) + }).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)) + 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) + 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).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) - }) + }).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})) - }) + }).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) + 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) - }) + }).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 4f7bb71d22..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).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) + 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).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).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).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 cbe7abf8c4..9b50c011ab 100644 --- a/crates/engine/src/cmd/notebook.rs +++ b/crates/engine/src/cmd/notebook.rs @@ -128,9 +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)?; @@ -143,12 +145,16 @@ pub fn commands() -> Vec { s.trusted_notebooks.remove(&nb.name.to_ascii_lowercase()); 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)?; s.active_notebook = Some(i); Ok(json!({"notebook": i})) }) + .roles(&[]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_notebook) .params(r#"{"notebook": name|index}"#), @@ -159,6 +165,8 @@ pub fn commands() -> Vec { s.active_notebook = None; 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| { @@ -177,6 +185,8 @@ pub fn commands() -> Vec { nb.cells.insert(at, Cell::new(kind, ps(p, "source").unwrap_or(""))); Ok(json!({"cell": at, "cells": nb.cells.len()})) }) + .roles(&[]) + .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| { @@ -186,6 +196,8 @@ pub fn commands() -> Vec { c.source = source.chars().take(MAX_SOURCE).collect(); Ok(json!({"cell": k})) }) + .roles(&[]) + .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| { @@ -194,9 +206,13 @@ pub fn commands() -> Vec { nb.cells.remove(k); 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::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| { @@ -206,6 +222,8 @@ pub fn commands() -> Vec { rustgis_data::io::write_atomic(Path::new(path), &bytes).map_err(|e| format!("{path}: {e}"))?; 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"}"#), @@ -272,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") { @@ -307,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})), }; @@ -356,7 +381,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('#') { @@ -365,7 +390,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 5978683507..7d93ae84aa 100644 --- a/crates/engine/src/cmd/pg.rs +++ b/crates/engine/src/cmd/pg.rs @@ -32,18 +32,18 @@ const MAX_LISTED: usize = 5_000; pub fn commands() -> Vec { vec![ - C::new("pg.connect", "Connect PostGIS", "Home › Add › Add Data", connect).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::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).view().when(need_connection), - C::new("pg.status", "PostGIS Connection", "Home › Add › Add Data", status).view(), - C::new("pg.tables", "List PostGIS Tables", "Home › Add › Add Data", tables).view().when(need_connection).params(r#"{"schema"?: string}"#), - C::new("pg.pendingEdits", "PostGIS Edits", "Edit › Manage Edits", pending_edits).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) + 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)) .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) + 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).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 6249680c43..cacaa084e2 100644 --- a/crates/engine/src/cmd/plugin_fetch.rs +++ b/crates/engine/src/cmd/plugin_fetch.rs @@ -274,6 +274,7 @@ impl Calls { /// A plugin command waiting for its fetches before it runs again (the browser). pub(crate) struct Retry { + continuation: crate::policy::Continuation, id: String, params: Value, urls: Vec, @@ -301,7 +302,13 @@ impl Session { // the addresses. let fetching: Vec<&str> = urls.iter().map(|k| k.split_once(' ').map_or(k.as_str(), |(_, u)| u)).collect(); let answer = json!({"pending": true, "fetching": fetching}); - self.plugin_retries.push(Retry { id: id.to_string(), params: params.clone(), urls, tries: tries + 1 }); + self.plugin_retries.push(Retry { + id: id.to_string(), + params: params.clone(), + urls, + tries: tries + 1, + continuation: self.continuation()?, + }); Ok(answer) } @@ -326,7 +333,7 @@ impl Session { .into_iter() .map(|r| { self.retry_tries = r.tries; - let res = self.run(&r.id, &r.params); + let res = self.retry_continuation(&r.continuation, &r.params); self.retry_tries = 0; (r.id, res) }) diff --git a/crates/engine/src/cmd/plugins.rs b/crates/engine/src/cmd/plugins.rs index 2a6e156732..70458c3910 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).view(), - C::new("plugin.install", "Install Plugin", "Plugins › Manage", install) + 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).view().params(r#"{"id": plugin id}"#), - C::new("plugin.enable", "Enable Plugin", "Plugins › Manage", enable).view().params(r#"{"id": plugin id, "enabled"?: bool}"#), - C::new("plugin.registry", "Browse Plugins", "Plugins › Manage", browse) + 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) + 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).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) + 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,189 +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.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); + 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)); } - 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 and projecting write a file only when given a `path`; the rest of them stays - // in the project. - if spec.id == "raster.georeference" || spec.id == "raster.project" { - return super::items::names_place(params, 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)); - } - // 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. @@ -449,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 { @@ -523,10 +376,7 @@ impl Bridge<'_> { return Err("plugins can't run other plugins' commands".into()); } 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); - } - let (enabled, run, label, mutates) = (spec.enabled, spec.run, spec.label, spec.mutates); + 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() { @@ -550,8 +400,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; - let mut res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(self.s, params))) - .unwrap_or_else(|_| Err(format!("{label} failed unexpectedly"))); + let mut res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let op = self.s.authorize(id, params)?; + self.s.invoke(op, crate::policy::Txn::Shared) + })) + .unwrap_or_else(|_| Err(format!("{label} failed unexpectedly"))); let requested = std::mem::take(&mut self.s.request_only); self.requested |= requested; self.changed |= mutates && !requested; @@ -593,7 +446,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), @@ -607,13 +462,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(); diff --git a/crates/engine/src/cmd/portals.rs b/crates/engine/src/cmd/portals.rs index 2e0a07d7e2..4e90719282 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).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).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).view(), - C::new("portal.search", "Search Open Data", "Home › Add › Web Data", portal_search).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).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).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 16d9e3d1c9..42ffb5e1b8 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(&[]).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) }]).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})) - }) + }).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) - }) + }).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 }) - }) + }).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 }) - }) + }).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 }) - }) + }).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 }) - }) + }).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).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()})) - }) + }).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})) - }) + }).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()})) - }) + }).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,63 @@ 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.maps.push(map.instance_id()); + inputs.layers.extend(map.layers.iter().map(|l| (map.instance_id(), l.id))); + } + } + 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::>()}); + 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 7577b233b3..dde82a9cfe 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).key("Mod+N").view().params(r#"{"name"?: string}"#), - C::new("project.sample", "World Atlas Sample", "Project", sample).view(), - C::new("project.open", "Open Project", "Project", open).key("Mod+O").view().params(r#"{"path": string}"#), - C::new("project.openText", "Open Project (from text)", "Project", open_text).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).key("Mod+S").view().params(r#"{"path"?: string}"#), - C::new("project.saveAs", "Save Project As", "Project", save_as).key("Mod+Shift+S").view().params(r#"{"path": string}"#), - C::new("project.rename", "Rename Project", "Project", rename).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()) }) + 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()) }) + 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) + 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).view().params(r#"{"nameFilter"?: string, "includeFields"?: bool}"#), - C::new("info.commands", "List Commands", "Help", list).view(), + C::new("info.project", "Project Info", "Project", info).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).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) + 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).view(), - C::new("project.logCommands", "Log Commands", "Project", log_commands).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)}"#), ] } @@ -159,6 +159,7 @@ fn reset(s: &mut Session, p: Project) { s.project = p; 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; @@ -323,10 +324,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) } @@ -515,3 +513,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 8d7c296cb9..2a6259157b 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).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).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 912696c85a..4cfd7a8863 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) + 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, "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).when(need_raster).params( + C::new("raster.symbology", "Symbology", "Style › Rendering", symbology).roles(&[]).effects(crate::policy::Effects::NONE).when(need_raster).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) + 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(&[]).effects(crate::policy::Effects::NONE) .when(need_raster) .params(r#"{"layer"?, "on"?: bool}"#), - C::new("raster.mask", "Mask Values", "Style › Rendering", mask).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(&[]).effects(crate::policy::Effects::NONE) .when(need_raster) .params(r#"{"layer"?, "type": "nearest"|"bilinear"}"#), - C::new("raster.bands", "Band Combination", "Style › Rendering", bands) + 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(&[]).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).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).view().when(need_raster).params(r#"{"layer"?}"#), - C::new("share.exportRaster", "Export Raster", "Data › Information", export).view().when(need_raster).params( + C::new("raster.info", "Raster Information", "Data › Information", info).roles(&[]).effects(crate::policy::Effects::NONE).view().when(need_raster).params(r#"{"layer"?}"#), + C::new("share.exportRaster", "Export Raster", "Data › Information", export).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).view().when(need_raster).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; @@ -967,6 +966,21 @@ pub fn identify(s: &Session, lon: f64, lat: f64) -> 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::primary(s, p)?; + if let Some(name) = like_name(p)? { + inputs.layers.extend(super::Inputs::named(s.map(), [name])?.layers); + } + 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/repair.rs b/crates/engine/src/cmd/repair.rs index 4ab30da480..f8cb5f45e0 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).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).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 95376803ae..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).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).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})) - }) + }).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) - }) + }).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) }) - }) + }).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) }) - }) + }).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) }) - }) + }).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 }) - }) + }).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) + 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()})) - }) + }).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})) - }) + }).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})) - }) + }).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()})) - }) + }).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})) - }) + }).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})) - }) + }).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})) - }) + }).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()})) - }) + }).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()})) - }) + }).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)) + 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)) + 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).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 974cb13d40..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).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).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(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("s3.list", "List S3 Bucket", "Home › Add › S3", list).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 9c5ec3b904..6ee418891c 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).params( + C::new("map.search", "Search", "Map", search_cmd).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}"#, ), ] diff --git a/crates/engine/src/cmd/select.rs b/crates/engine/src/cmd/select.rs index c1f80a88fe..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) + 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) + 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) + 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) + 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).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(&[]).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(&[]).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) - }).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) - }).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(&[]).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})) - }).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).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) + 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 2c831aa6c9..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).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 9c54789392..011b6e9f04 100644 --- a/crates/engine/src/cmd/service.rs +++ b/crates/engine/src/cmd/service.rs @@ -23,16 +23,16 @@ pub const MAX_GROUP_BYTES: usize = 64 << 20; pub fn commands() -> Vec { let mut commands = vec![ - C::new("service.layers", "Service Layers", "Home › Add", layers).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"}]} → layers with source_url, and next when supplied documents need another fetch"#, ), - C::new("service.add", "Add Service Layer", "Home › Add", add).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)}"#, ), - C::new("service.coverage", "WCS Coverage Request", "Home › Add", coverage).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"} → the GetCoverage address of a GeoTIFF (layer.addUrl adds it)"#, ), - C::new("catalog.search", "Search Catalogue", "Home › Add", catalog_search).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"} → records with their service and download links (service.add or layer.addUrl add them)"#, ), ]; diff --git a/crates/engine/src/cmd/service_group.rs b/crates/engine/src/cmd/service_group.rs index bfb6edb475..2a9fdd6e10 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).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"}]} ("group" and "layer" are rejected: use service.refresh)"#, ), - C::new("service.refresh", "Refresh", "Layers", refresh).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"}"#, ), - C::new("service.groupPlan", "Service Import Requests", "Layers", plan).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"?} → request, layers, and next while discovery is incomplete"#, ), - C::new("service.group", "Service Group", "Layers", set_group).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 d830fa05d2..c11a0b0bfb 100644 --- a/crates/engine/src/cmd/share.rs +++ b/crates/engine/src/cmd/share.rs @@ -12,10 +12,10 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("share.exportMap", "Export Map", "Publish › Output", export_map) + C::new("share.exportMap", "Export Map", "Publish › Output", export_map).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", export_layer) + C::new("share.exportLayer", "Export Features", "Publish › Output", export_layer).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)"}"#), @@ -38,7 +38,7 @@ pub fn commands() -> Vec { s.notify(note, false); } Ok(json!({"path": path})) - }) + }).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"}"#, ) diff --git a/crates/engine/src/cmd/sharing.rs b/crates/engine/src/cmd/sharing.rs index 0b68baa00f..04bb591177 100644 --- a/crates/engine/src/cmd/sharing.rs +++ b/crates/engine/src/cmd/sharing.rs @@ -128,7 +128,20 @@ const MANAGE: &str = r#"{"item"?: id|name (default: the last of its kind), "map" 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).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)}"#), @@ -142,19 +155,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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view(), - C::new("share.saveWebMap", "Save Web Map", "Publish › Manage", |s, p| update_item(s, p, "webMap", ps(p, "map"))).when(need_items).params(MANAGE), - C::new("share.replaceWebLayer", "Replace Web Layer", "Publish › Manage", |s, p| update_item(s, p, "webLayer", ps(p, "layer"))) + 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).params(r#"{"name"?, "path"?: folder, "remove"?: name (no parameters: list them)}"#), - C::new("share.upload", "Upload File", "Publish › Manage", upload).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"))).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"))) + 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).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}"))?; @@ -165,7 +178,7 @@ pub fn commands() -> Vec { } let steps = v.get("steps").cloned().ok_or("not a RustGIS model or web tool (no graph or steps)")?; s.run("model.new", &json!({"name": name, "steps": steps, "imported": true})) - }) + }).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}"#), ] } @@ -175,7 +188,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)) } @@ -198,17 +211,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 @@ -231,7 +241,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" => { @@ -248,10 +258,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) } @@ -264,7 +271,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() { @@ -274,7 +283,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()); } @@ -330,25 +341,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) } @@ -881,23 +874,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; @@ -1172,25 +1149,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(); @@ -1289,3 +1249,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 e0632eea35..0c69da0579 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).view(), - C::new("sources.save", "Save Data Source", "Home › Add › Data Sources", |s, p| keep(s, p, "saved")).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) + C::new("sources.favourite", "Favourite Data Source", "Home › Add › Data Sources", favourite).roles(&[]).effects(crate::policy::Effects::NONE) .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) + C::new("sources.remove", "Remove Data Source", "Home › Add › Data Sources", remove).roles(&[]).effects(crate::policy::Effects::NONE) .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 89f133d47b..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).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) + 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 74b8cddda8..6464543b68 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) + 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)"}"#), - C::new("stac.search", "STAC Search", "Home › Add › Web Data › STAC Search", search).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)}"#, ), - C::new("stac.addFootprints", "Add STAC Footprints", "Home › Add › Web Data › STAC Search", add_footprints_cmd) + 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).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} → 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).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}"#, ), ] diff --git a/crates/engine/src/cmd/tiles.rs b/crates/engine/src/cmd/tiles.rs index f4c48b0132..d72e85bd48 100644 --- a/crates/engine/src/cmd/tiles.rs +++ b/crates/engine/src/cmd/tiles.rs @@ -64,15 +64,15 @@ pub const NETWORK: &[&str] = &[ pub fn commands() -> Vec { vec![ - C::new("map.addTiles", "Add Tile Layer", "Home › Add", add).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)}"#, ), - C::new("map.removeTiles", "Remove Tile Layer", "Home › Add", remove) + 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).params( + C::new("map.setTiles", "Tile Layer Properties", "Home › Add", set).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))).view(), + C::new("map.tiles", "Tile Layers", "Home › Add", |s, _| Ok(list(s, false))).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 +80,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(&[]).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) + 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) + C::new("map.zoomToTiles", "Zoom To Tile Layer", "Home › Add", zoom_to).roles(&[]).effects(crate::policy::Effects::NONE) .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).params( + C::new("map.body", "Celestial Body", "Home › Add", body).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Network)).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))).view(), + C::new("map.bodies", "Celestial Bodies", "Home › Add", |s, _| Ok(bodies(s))).roles(&[]).effects(crate::policy::Effects::NONE).view(), ] } diff --git a/crates/engine/src/cmd/time.rs b/crates/engine/src/cmd/time.rs index 9912e7e072..a20e6763f5 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) + 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(&[]).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(&[]).effects(crate::policy::Effects::NONE) .view() .when(need_feature_layer) .params(r#"{"layer"?}"#), - C::new("time.set", "Time Slider Settings", "View › Time", set).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(&[]).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(&[]).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))).view(), - C::new("time.export", "Export Time Animation", "View › Time", export) + 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).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)}"#), @@ -334,3 +334,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 ce1ae6f8cf..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).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(&[]).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(&[]).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(&[]).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(&[]).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).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(&[]).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(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"on"?: bool}"#), - C::new("map.camera", "Camera", "View › Navigation", camera).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(&[]).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(&[]).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).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 a045b97575..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(&[]).effects(crate::policy::Effects::NONE) .view(), - C::new("nationalMap.add", "Add The National Map Service", "Home › Add › Web Data › United States", national_map_add).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).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 d659d09a5f..8801d5bed1 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).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).view().params(r#"{"kind": "ready"|"feature"|"raster"}"#), - C::new("model.new", "ModelBuilder", "Analyze › Geoprocessing", new_model).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) + 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) + 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 }]).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) + 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(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"name": model}"#), - C::new("script.run", "Run Script", "Analyze › Geoprocessing", run_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).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}"#, ), - C::new("raster.functionChain", "Function Editor", "Analyze › Raster", function_chain) + 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}"#), - C::new("analysis.suitability", "Suitability Modeler", "Analyze › Workflows", suitability).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}"#, ), - C::new("analysis.viewshed", "Visibility Analysis", "Analyze › Workflows", viewshed) + 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).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).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).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) + 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).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(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"layer"?}"#), - C::new("data.explore", "Exploratory Analysis", "Analyze › Workflows", explore) + 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) + 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) + 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}"#), ] @@ -134,6 +134,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 @@ -154,7 +155,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, or the layer and what the @@ -186,8 +187,10 @@ pub(crate) fn made_or_ran(s: &mut Session, p: &Value, name: &str, work: fn(&Sess 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))?; } add_made(s, work(s, p)?()?) } @@ -205,6 +208,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"))] @@ -212,11 +216,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 }) } } @@ -225,6 +234,15 @@ 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, work): (&'static str, Work) = match command { "analysis.viewshed" => (VIEWSHED, viewshed_work(s, p)?), "analysis.interpolate" => (INTERPOLATE, interpolate_work(s, p)?), @@ -239,21 +257,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"))] @@ -262,9 +280,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() @@ -371,7 +397,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" { @@ -384,7 +410,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"})), @@ -408,7 +434,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!( @@ -456,9 +482,7 @@ fn new_model(s: &mut Session, p: &Value) -> CmdResult { } // 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 _ = file_history; let n = steps.len(); s.project.models.push(Model { name: name.clone(), steps, graph }); Ok(json!({"model": name, "steps": n})) @@ -466,7 +490,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)); @@ -476,7 +500,7 @@ fn model_graph(s: &Session, p: &Value) -> Result<(String, crate::project::ModelG None => s.project.models.last().ok_or("the project has no models")?, }; let g = model.graph.clone().unwrap_or_else(|| crate::model::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. @@ -488,11 +512,21 @@ fn run_model(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))? + } } /// Saves a graph as a model, replacing the one of that name (or adding it). @@ -507,15 +541,13 @@ 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()); + let was_untrusted = i.is_some() && !s.item_trusted("model", &name); 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()); - } + let _ = was_untrusted; Ok(json!({"model": new_name})) } @@ -543,15 +575,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 )); @@ -597,7 +629,7 @@ pub(crate) fn run_script(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); // As when typed directly, a line naming a layer is checked against that layer. if params.get("layer").is_some() && let Ok(lid) = s.layer_id(¶ms) @@ -611,7 +643,8 @@ pub(crate) fn run_script(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 = 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 = 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})) @@ -677,7 +710,7 @@ fn apply_function(s: &Session, r: &Raster, p: &Value) -> Result, String rasterops::remap(r, band, &ranges, b(p, "keep").unwrap_or(false)) } "resample" => { - let t = raster_of(s, ps(p, "target").ok_or("target (a raster to match) is required")?)?; + let t = raster_of(s, resample_target(p)?.ok_or("target (a raster to match) is required")?)?; if t.width as usize * t.height as usize > rasterops::max_pixels() { return Err(format!("the target grid is larger than this computer's {} cells", rasterops::max_pixels())); } @@ -691,7 +724,7 @@ fn raster_function(s: &mut Session, p: &Value) -> CmdResult { let src = raster_of(s, ps(p, "layer").ok_or("layer is required")?)?; let func = ps(p, "function").unwrap_or(""); let pixels = apply_function(s, &src, p)?; - let grid = if func == "resample" { raster_of(s, ps(p, "target").unwrap_or(""))? } else { src.clone() }; + let grid = if func == "resample" { raster_of(s, resample_target(p)?.unwrap_or(""))? } else { src.clone() }; let r = rasterops::derive(&grid, &out_name(p, format!("{}_{func}", src.name)), vec![pixels])?; add_raster(s, r) } @@ -709,7 +742,7 @@ fn function_chain(s: &mut Session, p: &Value) -> CmdResult { let pixels = apply_function(s, &cur, st).map_err(|e| format!("step {} ({func}): {e}", i + 1))?; // Only a resample takes another raster's grid; otherwise the grid is the current one. cur = if func == "resample" { - let target = raster_of(s, st.get("target").and_then(Value::as_str).unwrap_or(""))?; + let target = raster_of(s, resample_target(st)?.unwrap_or(""))?; rasterops::derive(&target, &cur.name, vec![pixels])? } else { rasterops::derive(&cur, &cur.name, vec![pixels])? @@ -725,11 +758,10 @@ fn suitability(s: &mut Session, p: &Value) -> CmdResult { 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 scored = 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 on_grid = rasterops::resample_to(&r, band, &first)?; let grid = rasterops::derive(&first, "c", vec![on_grid])?; @@ -1073,3 +1105,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(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(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(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 5b707550d2..8eecf64c55 100644 --- a/crates/engine/src/cmd/workspace.rs +++ b/crates/engine/src/cmd/workspace.rs @@ -71,19 +71,19 @@ pub fn commands() -> Vec { s.sync_linked_views(); } Ok(json!({"linked": s.project.link_views})) - }) + }).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(&[]).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(&[]).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 +105,7 @@ pub fn commands() -> Vec { updated: t, }); Ok(json!({"job": id})) - }) + }).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 +125,14 @@ pub fn commands() -> Vec { } j.updated = now(); Ok(Value::Null) - }) + }).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(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"job": id}"#), // Animation. C::new("animation.add", "Add Keyframe", "View › Animation", |s, p| { @@ -144,7 +144,7 @@ pub fn commands() -> Vec { } m.animation.push(k); Ok(json!({"keyframes": m.animation.len()})) - }) + }).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 +159,21 @@ pub fn commands() -> Vec { } } Ok(json!({"keyframes": m.animation.len()})) - }) + }).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(&[]).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(&[]).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 +191,7 @@ pub fn commands() -> Vec { t *= 1.1; } profile(s, p, &[a, Coord::new(a.x + dx * t, a.y + dy * t)]) - }) + }).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 +207,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(&[]).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 +216,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) - }) + }).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)) - }) + }).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) + 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))).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) + C::new("offline.download", "Download Map", "Data › Offline", offline_download).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"name"?: string, "extent"?: [minLon, minLat, maxLon, maxLat] (the map view when omitted)}"#), - C::new("offline.sync", "Sync", "Data › Offline", offline_sync).params(r#"{"name": offline area}"#), + C::new("offline.sync", "Sync", "Data › Offline", offline_sync).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).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 +255,7 @@ pub fn commands() -> Vec { } } Ok(Value::Null) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .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 +282,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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view(), C::new("cache.empty", "Empty Cache", "Data › Feature Cache", |s, _| { let mut n = 0; @@ -299,17 +299,17 @@ pub fn commands() -> Vec { } } Ok(json!({"emptied": n})) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .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).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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .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 +317,7 @@ pub fn commands() -> Vec { fill_cache(s); } Ok(json!({"autoCache": s.auto_cache})) - }) + }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .params(r#"{"on"?: bool}"#), ] @@ -410,7 +410,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 +776,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 d8add19688..2f706dc27f 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) + 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).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).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).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 { @@ -396,16 +454,19 @@ 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(); - release(&keys[n..=n]); - 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(); + release(&keys[n..=n]); + if !ok { + break; + } } - } + }) }); // No thread to be had: nothing is read ahead, and its keys and place are given back. if spawned.is_err() { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 673da4aed8..5fad9edd8d 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -11,11 +11,13 @@ 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; mod roles; pub use peer::Peer; +pub use roles::{Document, ParamRole, Role}; mod parameters; pub mod project_log; pub use parameters::{params_envelope, script_line}; @@ -42,6 +44,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 command. @@ -60,7 +74,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, + /// Durable-output parameter roles, separate from permission effects. None is unreviewed. + pub 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> { @@ -80,7 +101,43 @@ 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 } + 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 durable-output roles. An empty list adopts the reviewed common parameter rules. + pub 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; @@ -462,7 +519,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 @@ -483,9 +541,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, @@ -507,11 +565,12 @@ 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, + url_requests: Vec, + policy_work: policy::downloads::Work, plugin_retries: Vec, /// A style of the project still loading, which mustn't move the next one's view. style_view: Option<(String, Camera)>, @@ -519,11 +578,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 @@ -622,12 +682,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, @@ -635,7 +695,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, @@ -688,7 +748,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 @@ -701,7 +761,7 @@ pub struct Session { pub(crate) downloaded: Vec<(u64, Vec)>, /// 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, @@ -727,7 +787,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 @@ -741,6 +801,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. @@ -873,10 +934,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. render::rustgis_tiles::remote::install(); rustgis_data::diagnostics::set_clock(|| { @@ -919,6 +981,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(), @@ -996,7 +1059,8 @@ 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.forget_download_claim(retired); } self.downloading.push(self.next_pending); self.next_pending @@ -1005,6 +1069,10 @@ impl Session { /// Hands over a download's bytes, for `layer.addDownload {"id"}`; `false` when it was dropped /// (another project was opened meanwhile). pub fn download_landed(&mut self, id: u64, bytes: Vec) -> bool { + if let Err(e) = self.download_continues(id) { + self.notify(e, true); + return false; + } let Some(i) = self.downloading.iter().position(|d| *d == id) else { return false }; self.downloading.remove(i); self.downloaded.push((id, bytes)); @@ -1013,6 +1081,7 @@ impl Session { /// Forgets a download that failed. pub fn download_failed(&mut self, id: u64) { + self.forget_download_claim(id); self.downloading.retain(|d| *d != id); } @@ -1029,18 +1098,6 @@ impl Session { /// 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. - pub fn request_url(&mut self, p: &serde_json::Value) -> Result { - let url = p.get("url").and_then(serde_json::Value::as_str).unwrap_or("").to_string(); - if !self.url_requests.iter().any(|q| q == p) { - 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")); - } - self.url_requests.push(p.clone()); - } - self.request_only = true; - Ok(serde_json::json!({"pending": true, "url": url})) - } - /// 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 @@ -1052,15 +1109,21 @@ impl Session { 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. 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(); + for mut file in std::mem::take(&mut self.file_outputs) { + if let Err(e) = self.check_continuation(&file.continuation) { + self.notify(e, true); + continue; + } + if let Some(bytes) = file.bytes.take() { + live.push((file.id, file.file_name.clone(), bytes)); + } + self.file_outputs.push(file); + } + live } /// Whether background work still belongs to this project. @@ -1098,6 +1161,11 @@ 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| s.run_command(id, params)) + } + + /// 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 _diagnostic_scope = self.diagnostics.enter(); let source = self.registry.get(id).map(|spec| spec.id).or_else(|| id.starts_with(rustgis_plugins::PREFIX).then_some("Plugin")); // Restored on every path, including panics outside `guarded`, so a later notice is not @@ -1106,7 +1174,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 { @@ -1118,7 +1186,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()); - let res = self.run_command(id, params); + let res = run(self); // A plugin's command waiting for its fetches answers `pending` and runs again when they // land: logged then, once. (Another command's `pending`, a download the web app makes, is // logged as it is asked for: nothing runs it again.) @@ -1173,7 +1241,8 @@ impl Session { } if id.starts_with(rustgis_plugins::PREFIX) { self.merge = merge; - return self.run_plugin(id, params); + let op = self.authorize(id, params)?; + return self.invoke(op, policy::Txn::Own); } let Some(&spec) = self.registry.get(id) else { return Err(format!("unknown command '{id}'")) }; // Keys the command does not read: said, so a script or an agent can correct itself. @@ -1207,14 +1276,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); } self.merge = merge; - let res = self.guarded(label, mutates, |s| run(s, params)); + let op = self.authorize(id, params)?; + 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) @@ -1254,6 +1324,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. @@ -1275,6 +1346,7 @@ impl Session { 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 queued_authority = self.queued_authority(); let file_outputs = self.file_outputs.len(); let said = self.notices_given; let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(self))) @@ -1326,7 +1398,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); } @@ -1350,6 +1422,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. @@ -1362,6 +1435,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; @@ -1374,11 +1448,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; } @@ -1394,6 +1466,7 @@ impl Session { } if res.is_err() { self.url_requests.truncate(requests); + self.rollback_queued_authority(&queued_authority); self.file_outputs.truncate(file_outputs); } let quiet = std::mem::take(&mut self.quiet_failure); @@ -1469,6 +1542,7 @@ impl Session { downloading: take(&mut self.downloading), downloaded: take(&mut self.downloaded), url_requests: take(&mut self.url_requests), + policy_work: self.take_policy_work(), plugin_retries: take(&mut self.plugin_retries), style_view: self.style_view.take(), #[cfg(not(target_arch = "wasm32"))] @@ -1519,6 +1593,7 @@ impl Session { downloading, downloaded, url_requests, + policy_work, plugin_retries, style_view, #[cfg(not(target_arch = "wasm32"))] @@ -1542,6 +1617,7 @@ impl Session { (self.pending, self.plugin_runs, self.plugin_retries) = (pending, plugin_runs, plugin_retries); self.file_outputs = file_outputs; (self.downloading, self.downloaded, self.url_requests) = (downloading, downloaded, url_requests); + self.restore_policy_work(policy_work); (self.trend_results, self.raster_results, self.raster_runs) = (trend_results, raster_results, raster_runs); #[cfg(feature = "geolibre")] { @@ -1724,9 +1800,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); @@ -1736,16 +1817,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); @@ -1755,7 +1841,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 } @@ -1907,9 +1993,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 @@ -1989,22 +2073,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 bc48297e7a..c7d2e03b3e 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -69,7 +69,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. @@ -503,9 +503,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"; @@ -520,20 +521,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(); @@ -725,6 +718,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": [ @@ -743,7 +749,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); diff --git a/crates/engine/src/peer.rs b/crates/engine/src/peer.rs index 89bf1a6f57..f4c6070bd5 100644 --- a/crates/engine/src/peer.rs +++ b/crates/engine/src/peer.rs @@ -254,6 +254,10 @@ impl Session { /// 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..5d1c403070 --- /dev/null +++ b/crates/engine/src/policy/dispatch.rs @@ -0,0 +1,886 @@ +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 }, +} +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 { + /// 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(super) fn policy_ingress_idle(&self) -> bool { + self.policy.scopes.is_empty() + } + /// Only the native user scope may record executable-content provenance. + pub(super) 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)?; + 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 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 continuation = (!matches!(self.scope().kind, Kind::User)).then(|| self.continuation_for(node)); + 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.continuation_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()); + let run = |s: &mut Session| { + s.with_node(node, |s| { + let editor = s.trust_editor(spec.id, ¶ms); + let result = spec.run.call(s, ¶ms); + if let Ok(value) = &result { + s.trust_after(spec.id, ¶ms, value, editor); + } + result + }) + }; + match txn { + Txn::Own => self.guarded(spec.label, spec.mutates, run), + Txn::Shared => run(self), + } + } + } + } + /// 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) -> Continuation { + self.continuation_for(op.node.clone()) + } + /// Own private identity snapshots cross workers; display copies never become authority. + fn continuation_for(&self, node: Arc) -> Continuation { + 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))); + } + } + } + _ => {} + } + } + } + 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::connections), + } + } + /// 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(); + if !self.policy.scopes.is_empty() { + self.continuation_owner(&c)?; + } + self.resume(&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")?; + Ok(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) + } + /// Native workers may deliver at root; an existing ingress must own the stored continuation. + pub(crate) fn check_delivery(&self, c: &Continuation) -> Result<(), String> { + if self.policy.scopes.is_empty() { self.check_continuation(c) } else { self.continuation_owner(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..8c5130b9e7 --- /dev/null +++ b/crates/engine/src/policy/downloads.rs @@ -0,0 +1,219 @@ +//! 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); + } +} +pub(super) struct Queued { + c: std::sync::Weak, + import: bool, +} +#[derive(Default)] +pub(crate) struct Work { + pub(super) queued: BTreeMap, + pub(super) claims: 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() + } + /// Retires only newly issued queued keys after failure, even if a caller retained its request. + pub(crate) fn rollback_queued_authority(&mut self, before: &[u64]) { + self.policy.work.queued.retain(|key, _| before.contains(key)); + self.retire_abandoned_requests(); + } + /// 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); + } + /// 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 c = Arc::new(self.continuation()?); + self.check_continuation(&c)?; + self.retire_abandoned_requests(); + let duplicate = self.url_requests.iter().any(|q| q.params == *p && q.authority.scope.same_caller(&c.scope)); + if !duplicate { + 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)); + } + 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 }); + self.url_requests.push(UrlRequest { key, params: p.clone(), authority: c }); + } + self.request_only = true; + Ok(json!({"pending":true,"url":p.get("url")})) + } + /// Taking an opaque request sends nothing. Authority is checked again when it is claimed. + pub fn take_url_requests(&mut self) -> Vec { + let mut live = Vec::new(); + for request in std::mem::take(&mut self.url_requests) { + 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.policy.work.queued.remove(&request.key); + self.notify("a deferred URL request was revoked before it started", true); + } + } + live + } + /// Claims one engine-issued request immediately before its first I/O. + pub fn download_started_for(&mut self, request: &UrlRequest) -> Result { + if self.policy.work.claims.len() >= 64 { + return Err("64 downloads are already awaiting completion".into()); + } + 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")?; + self.check_delivery(&c)?; + let import = q.import; + self.policy.work.queued.remove(&request.key); + 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> { + if !self.download_is_pending(id) && !self.downloaded.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) + { + self.policy.work.claims.remove(&id); + self.downloading.retain(|n| *n != id); + self.downloaded.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" && !in_place && 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; + } + /// Retires a failed or consumed 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..d967afb207 --- /dev/null +++ b/crates/engine/src/policy/effects.rs @@ -0,0 +1,111 @@ +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 == "cube.open" && crate::cmd::cube_catalog::opens_zarr(params) { + effects = effects.with(Effect::Files).with(Effect::Network); + } + let params = crate::cmd::items::layer_name_left_out(spec.id, params); + if !effects.contains(Effect::Network) && crate::cmd::items::names_place(¶ms, PLACE_KEYS, 0) { + effects = effects.with(Effect::Files); + } + // In-memory georeferencing/projecting is an edit; a named output reaches the file system. + if matches!(spec.id, "raster.georeference" | "raster.project") && 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) -> Effects { + let mut effects = declared(spec, params); + if spec.id == "layer.loadAll" + && let Ok(id) = s.layer_id(params) + && let Some(layer) = s.map().layer(id) + && let Some(source) = &layer.source + { + if source.path.is_some() || source.gdal.is_some() || source.duckdb.is_some() { + effects = effects.with(Effect::Files); + } + if source.path.as_deref().is_some_and(rustgis_data::source::is_remote_name) + || 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); + } + } + 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 +} +/// 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..1327813a25 --- /dev/null +++ b/crates/engine/src/policy/mod.rs @@ -0,0 +1,47 @@ +//! 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; + +#[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..ea79a72097 --- /dev/null +++ b/crates/engine/src/policy/plan.rs @@ -0,0 +1,1405 @@ +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 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); + 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 self.strict + && 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::(); + 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_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_lowercase())); + self.table_outputs.insert(name.trim().to_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); + } + // The same selector/selection predicate as inputs_of: selected in-memory windows + // need no extra source read, while a whole streamed input does. + for name in crate::cmd::gp::layer_names(Some(pin.spec()), ¶ms) { + if let Some(l) = self + .context() + .map() + .layer_by_name(name) + .filter(|l| l.is_streamed() && (l.selection.is_empty() || pin.spec().id == "vectorToPmtiles")) + && let Some(source) = &l.source + { + if source.path.is_some() || source.gdal.is_some() || source.duckdb.is_some() { + effects = effects.with(Effect::Files); + } + if source.path.as_deref().is_some_and(rustgis_data::source::is_remote_name) + || 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); + } + } + } + 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_lowercase())); + self.table_outputs.insert(name.trim().to_lowercase()); + self.dataset_outputs.insert(name.trim().to_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()?)); + } + 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.symbols.get(&(self.context().map().instance_id(), name.to_lowercase())) { + 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_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_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 { + if let Op::Command(spec) = &n.op + && let Some(inputs) = spec.inputs + { + let inputs = match inputs(&self.context(), params) { + 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.symbols.get(&(self.context().map().instance_id(), name.to_lowercase())) { + n.targets.push(self.bound_symbol(target, reads)); + } else if self.outputs.contains(&(self.context().map().instance_id(), name.to_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 !matches!(&n.op, Op::Command(spec) if spec.inputs.is_some()) + && 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_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(()) + } + /// 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.symbols.get(&(self.context().map().instance_id(), name.to_lowercase()))) { + 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_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!( + self.connections + .connections + .iter() + .map(|c| json!({"definition":c,"password":self.database_logins.known_password(&c.id).map(|p|p.expose())})) + .collect::>() + ) + } +} + +/// 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..a8140294b1 --- /dev/null +++ b/crates/engine/src/policy/provenance.rs @@ -0,0 +1,99 @@ +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 = 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 = p.get("rename").and_then(Value::as_str).map(str::to_string).or_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..7f81fd3bdf --- /dev/null +++ b/crates/engine/src/policy/scope.rs @@ -0,0 +1,227 @@ +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, +} +impl RuntimeAccounts { + /// Dynamic commands may choose multiple URLs, so their entire active stores are bound. + pub(super) fn capture() -> Result { + Ok(Self { + arcgis: crate::render::rustgis_tiles::arcgis_auth::account_identity()?, + earthdata: crate::render::rustgis_tiles::earthdata::account_identity()?, + }) + } + /// 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() + } +} + +#[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. + 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(|old| *old != crate::render::rustgis_tiles::s3::connections()) { + 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 cec01e8d03..ef37fd426a 100644 --- a/crates/engine/src/project_log.rs +++ b/crates/engine/src/project_log.rs @@ -131,6 +131,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/roles.rs b/crates/engine/src/roles.rs index 3a1df9f9f8..e0e3c89b99 100644 --- a/crates/engine/src/roles.rs +++ b/crates/engine/src/roles.rs @@ -2,7 +2,7 @@ //! properties. Unknown nested content and foreign notebook cells remain opaque, with honest //! notices. Logs deliberately have a separate, stricter policy. -use std::collections::HashSet; +use std::collections::HashMap; use std::sync::OnceLock; use serde::{Serialize, de::DeserializeOwned}; @@ -10,6 +10,37 @@ use serde_json::Value; use crate::durable::{self, Durable, Kept, Report}; +/// A replayable parameter's durable-output role. Permission policy does not consume roles. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ParamRole { + pub key: &'static str, + pub role: Role, +} +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Role { + Address, + Path, + Secret, + Dataset, + Configuration, + Sql, + ToolParams, + Script, + TaskSteps, + ModelSteps, + ModelGraph, + Document(Document), +} +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Document { + Project, + Map, + Task, + Notebook, + Presentation, + Model, +} + const WITHHELD: &str = "[parameters withheld]"; const NESTED: &str = "[withheld: nested too deep]"; const DATA: &[&str] = &[ @@ -40,10 +71,11 @@ const ADDRESS: &[&str] = &["url", "urls", "href", "source", "template", "tiles", "store", "endpoint", "address", "uri", "connection", "database"]; const CONFIG: &[&str] = &["headers", "options", "auth", "connection", "source", "sources"]; -/// Registered commands are classified once; plugin commands keep their nested content opaque. -fn known(id: &str) -> bool { - static IDS: OnceLock> = OnceLock::new(); - IDS.get_or_init(|| crate::cmd::all().into_iter().map(|c| c.id).collect()).contains(id) +/// Durable output reads its own field from the immutable command registry. An explicit empty +/// declaration retains the reviewed common scalar/address/configuration rules below. +fn metadata(id: &str) -> Option<&'static [ParamRole]> { + static ROLES: OnceLock> = OnceLock::new(); + ROLES.get_or_init(|| crate::cmd::all().into_iter().filter_map(|c| c.roles.map(|r| (c.id, r))).collect()).get(id).copied() } /// A conservative bounded read-only check, used solely to announce unchecked/withheld content. @@ -69,7 +101,7 @@ fn invalid(value: &Value, report: &mut Report) -> Option { /// Cleans one command without treating parameter JSON strings as executor envelopes. pub(crate) fn command(id: &str, params: &Value, depth: usize) -> (Option, Report) { let mut report = Report::default(); - let changed = object(id, params, depth, 0, known(id), &mut report); + let changed = object(id, params, depth, 0, metadata(id).is_some(), &mut report); (changed, report) } @@ -95,26 +127,22 @@ fn object(id: &str, value: &Value, depth: usize, config_depth: usize, registered for (key, v) in obj { unchecked |= !matches!(durable::public_address(key), Kept::Unchanged); let k = rustgis_data::address::normalized_name(key).unwrap_or_else(|| key.to_ascii_lowercase()); - let executable = registered - && matches!( - (id, k.as_str()), - ("gp.run", "params") - | ("script.run", "script") - | ("task.new", "steps") - | ("model.new" | "model.save" | "model.run" | "model.validate" | "model.export", "graph") - | ("model.new", "steps") - ); - let document = registered - && k == "text" - && matches!(id, "project.openText" | "map.import" | "task.import" | "notebook.open" | "presentation.import" | "model.import"); - let clean = if executable || document { + let role = if registered && config_depth == 0 { + metadata(id).and_then(|roles| roles.iter().find(|r| r.key == k).map(|r| r.role)) + } else { + None + }; + let clean = if let Some( + role @ (Role::ToolParams | Role::Script | Role::TaskSteps | Role::ModelSteps | Role::ModelGraph | Role::Document(_)), + ) = role + { if depth >= 4 { report.withheld = report.withheld.saturating_add(1); Some(Value::String(NESTED.into())) } else { - envelope(id, &k, v, obj, depth + 1, report) + envelope(role, v, obj, depth + 1, report) } - } else if rustgis_data::address::secret_key(key) + } else if (role == Some(Role::Secret) || rustgis_data::address::secret_key(key)) && (k != "key" || crate::sources::looks_like_a_key(v.as_str().unwrap_or("")) || obj.iter().any(|(other, value)| other != key && !DATA.contains(&other.as_str()) && address_like(value))) @@ -124,20 +152,20 @@ fn object(id: &str, value: &Value, depth: usize, config_depth: usize, registered Value::Number(_) => Some(Value::String(String::new())), _ => None, } - } else if DATA.contains(&k.as_str()) { + } else if role == Some(Role::Dataset) || DATA.contains(&k.as_str()) { None - } else if registered && id.starts_with("sql.") && matches!(k.as_str(), "sql" | "query") { + } else if role == Some(Role::Sql) { v.as_str().and_then(|s| { let clean = crate::project_log::sql_secrets_removed(s); (clean != s).then_some(Value::String(clean)) }) - } else if ADDRESS.contains(&k.as_str()) || address_like(v) { - if v.is_object() && registered && CONFIG.contains(&k.as_str()) { + } else if matches!(role, Some(Role::Address | Role::Path)) || ADDRESS.contains(&k.as_str()) || address_like(v) { + if v.is_object() && registered && (role == Some(Role::Configuration) || CONFIG.contains(&k.as_str())) { object("", v, depth, config_depth + 1, true, report) } else { address(v, report) } - } else if registered && CONFIG.contains(&k.as_str()) && v.is_object() { + } else if registered && (role == Some(Role::Configuration) || CONFIG.contains(&k.as_str())) && v.is_object() { object("", v, depth, config_depth + 1, true, report) } else { unchecked |= suspicious(v); @@ -199,14 +227,14 @@ fn address(v: &Value, report: &mut Report) -> Option { } /// Explicit executable and document roles are the only route into nested executable content. -fn envelope(id: &str, key: &str, v: &Value, siblings: &serde_json::Map, depth: usize, report: &mut Report) -> Option { - match (id, key) { - ("gp.run", "params") => { +fn envelope(role: Role, v: &Value, siblings: &serde_json::Map, depth: usize, report: &mut Report) -> Option { + match role { + Role::ToolParams => { let (clean, r) = tool(siblings.get("tool").and_then(Value::as_str).unwrap_or(""), v, depth); report.merge(&r); clean } - ("script.run", "script") => match v.as_str() { + Role::Script => match v.as_str() { Some(s) => { let (clean, r) = script(s, depth); report.merge(&r); @@ -214,10 +242,10 @@ fn envelope(id: &str, key: &str, v: &Value, siblings: &serde_json::Map invalid(v, report), }, - ("task.new", "steps") => typed_value::(v, depth, report), - ("model.new", "steps") => typed_value::(v, depth, report), - (_, "graph") => typed_value::(v, depth, report), - (_, "text") => document(id, v, depth, report), + Role::TaskSteps => typed_value::(v, depth, report), + Role::ModelSteps => typed_value::(v, depth, report), + Role::ModelGraph => typed_value::(v, depth, report), + Role::Document(kind) => document(kind, v, depth, report), _ => None, } } @@ -280,22 +308,21 @@ impl Durable for VecSteps { /// Reads documents at the existing reader bound, without normalization, trust or restore-cache /// access. Unchanged documents keep their exact text; only changed typed copies are serialized. -fn document(id: &str, v: &Value, depth: usize, report: &mut Report) -> Option { +fn document(kind: Document, v: &Value, depth: usize, report: &mut Report) -> Option { let Some(text) = v.as_str().filter(|s| s.len() as u64 <= crate::project::MAX_BYTES) else { return invalid(v, report) }; - let result = match id { - "project.openText" => text_document::(text, depth), - "map.import" => { + let result = match kind { + Document::Project => text_document::(text, depth), + Document::Map => { if serde_json::from_str::(text).is_ok() { text_document::(text, depth) } else { text_document::(text, depth) } } - "task.import" => text_document::(text, depth), - "presentation.import" => text_document::(text, depth), - "notebook.open" => crate::cmd::notebook::durable_document(text, depth), - "model.import" => model_document(text, depth), - _ => return None, + Document::Task => text_document::(text, depth), + Document::Presentation => text_document::(text, depth), + Document::Notebook => crate::cmd::notebook::durable_document(text, depth), + Document::Model => model_document(text, depth), }; match result { Ok((clean, r)) => { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index e897563eb7..0941244b77 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -2,6 +2,9 @@ use serde_json::{Value, json}; use crate::{Project, Session, sample}; +#[path = "tests/permission_policy.rs"] +mod permission_policy; + #[path = "tests/task_reentry.rs"] mod task_reentry; @@ -2184,6 +2187,78 @@ 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() { + 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()); @@ -2198,8 +2273,8 @@ 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"}}))); // Only a plain layer name, only gp.run's, only for RustGIS's own tools. for (command, params) in [ ("gp.run", json!({"tool": "buffer", "params": {"output": "x.shp"}})), @@ -2213,7 +2288,7 @@ fn imported_tasks_name_outputs_and_steps_are_edited() { ("gp.run", json!({"tool": "buffer", "params": {"input": "Cities", "nested": {"output": "x"}}})), ("layer.rename", json!({"layer": "Cities", "output": "x"})), ] { - 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. @@ -2258,7 +2333,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] @@ -2931,7 +3006,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"); @@ -6612,11 +6687,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(); @@ -8121,6 +8196,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"); @@ -8130,8 +8216,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}"); @@ -8139,6 +8226,60 @@ 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() { + 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 @@ -8155,7 +8296,10 @@ 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_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!(s.run("plugin:eng-url/f", &p).is_err(), "run otherwise, it waits"); assert!(s.take_url_requests().is_empty()); // An edit run that way is an undo step still. @@ -8461,6 +8605,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(); @@ -8480,6 +8625,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); @@ -8492,6 +8642,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(); @@ -8519,6 +8698,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(); @@ -8625,14 +8817,17 @@ fn plugin_file_parameters_reach_files_and_stay_out_of_the_history() { 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(); @@ -10809,10 +11004,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}"); } @@ -10821,8 +11017,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`) @@ -10867,8 +11067,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. @@ -12574,7 +12774,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); @@ -12633,7 +12834,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); @@ -18752,8 +18954,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| { @@ -18765,7 +18967,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"); @@ -18801,7 +19003,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 @@ -18819,11 +19021,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. @@ -20864,7 +21071,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"), @@ -21020,9 +21227,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. @@ -25818,6 +26025,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(); diff --git a/crates/engine/src/tests/credential_output.rs b/crates/engine/src/tests/credential_output.rs index d9d87d7a3d..c94c35f8a8 100644 --- a/crates/engine/src/tests/credential_output.rs +++ b/crates/engine/src/tests/credential_output.rs @@ -227,7 +227,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)); @@ -254,7 +254,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()); diff --git a/crates/engine/src/tests/permission_policy.rs b/crates/engine/src/tests/permission_policy.rs new file mode 100644 index 0000000000..7d5fcc7f23 --- /dev/null +++ b/crates/engine/src/tests/permission_policy.rs @@ -0,0 +1,907 @@ +//! 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"), + } +} + +/// 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 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 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 call = Call::command("layer.addUrl", json!({"url":"https://example.invalid/data.geojson"})); + 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(); + 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() { + 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()); +} + +/// 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"})); + assert!( + s.run("model.new", &json!({"name":"Cycle","graph":cycle})).is_err() + || matches!(s.review(&scope, &Call::command("model.run", json!({"name":"Cycle"}))), Outcome::Refused(_)) + ); + 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() { + use rustgis_data::source::{Batch, FeatureSource, Request, SourceInfo}; + use std::{ + io::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); + release.send(()).unwrap(); + 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.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok").unwrap(); + } + let result = worker.join().unwrap(); + assert_eq!(result.is_err(), revoke); + assert!(matches!(listener.accept(), Err(e) if e.kind() == std::io::ErrorKind::WouldBlock)); + } + rustgis_plugins::unload(plugin); +} 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/source.rs b/crates/map/src/source.rs index ddf80fed0c..7d2bd80158 100644 --- a/crates/map/src/source.rs +++ b/crates/map/src/source.rs @@ -520,10 +520,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() + } +} /// Tells the app something landed (a read, or a source opened by its address). pub(crate) fn landed() { @@ -682,6 +697,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 let Some(url) = self.path.clone().filter(|p| rustgis_data::source::is_remote_name(p)) { return self.open_remote(&url); } @@ -765,6 +781,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| { @@ -789,9 +809,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( @@ -820,22 +840,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); - } - landed(); - }), - ); + 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); + } + landed(); + }), + ); + }); Ok(()) } @@ -968,7 +991,8 @@ impl SourceLayer { let estimate = self.estimated_row_bytes; let full_read = self.full_read; let fields = self.window_fields; - 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. @@ -995,14 +1019,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) - })) - .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) + })) + .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 { @@ -1038,7 +1068,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; @@ -1190,6 +1220,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/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/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..ae0ee43f9d 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) { @@ -203,6 +227,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 +254,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 +277,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)) } @@ -1925,6 +1925,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 b18f098d20..0ee811b2a3 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -54,6 +54,12 @@ 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()) +} + 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). @@ -765,6 +771,12 @@ pub fn download_detailed(url: &str, max: usize, done: DoneDetailed) { /// Like [`download_detailed`], giving up after `timeout` natively. fn download_detailed_within(url: &str, max: usize, timeout: std::time::Duration, 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; @@ -936,6 +948,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; @@ -956,6 +974,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))); let f = fetcher().lock().unwrap_or_else(|e| e.into_inner()).clone(); match f { Some(f) => f(url, done, omit), @@ -1154,7 +1178,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, } @@ -1221,13 +1245,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())); } } @@ -1315,11 +1339,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); + }) }); } @@ -1341,6 +1368,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()); @@ -1379,9 +1407,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))); + }); }); } @@ -1391,6 +1422,7 @@ mod native { use std::io::Read; let mut diagnostics = super::RequestDiagnostics::new(url, "POST"); let answer = (|| -> Result, Failure> { + super::check_current()?; let mut resp = agent .post(url) .header("Content-Type", "application/json") @@ -1426,9 +1458,13 @@ mod native { pub(super) fn download_within(url: String, timeout: std::time::Duration, done: DoneDetailed) { let capture = Capture::current(); + let still = super::current_still(); std::thread::spawn(move || { - let _scope = capture.enter(); - done(get_detailed(&download_agent(timeout), &url)); + super::with_optional_still(still, || { + let _scope = capture.enter(); + let r = super::check_current().map_err(Failure::from).and_then(|_| get_detailed(&download_agent(timeout), &url)); + done(r); + }) }); } @@ -1436,20 +1472,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) { @@ -1461,7 +1500,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)); } } @@ -1527,6 +1566,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); } @@ -1578,6 +1618,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); @@ -1738,6 +1779,46 @@ mod native { mod tests { 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() } 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/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 65bb6d4dd4..a2f0317886 100644 --- a/crates/ui-egui/src/analysispane.rs +++ b/crates/ui-egui/src/analysispane.rs @@ -292,15 +292,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/geolibreui.rs b/crates/ui-egui/src/geolibreui.rs index 001ebe605d..bd6f195a4b 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 { @@ -308,52 +317,67 @@ mod tests { #[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(); + // 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, + }); + } + } + 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.org/overflow.zarr"}}], "layerGroups":[{"id":"folder","name":"Folder"}] + }).to_string()}), + ) + .unwrap(); + let target = app.session.project.active_map; + let id = app.session.map().layer_by_name("Overflow").unwrap().id; + 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(); + 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()); } - 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()); } /// 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 +388,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 efaf01ebef..29d97ac0cd 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; @@ -676,7 +677,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, @@ -1612,7 +1613,7 @@ impl RustGisApp { if let Err(error) = &delivered { params["error"] = json!(error); } - let completed = self.session.run("gp.fileDelivered", ¶ms); + let completed = self.session.acknowledge_gp_file(id, params.get("error").and_then(Value::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()), @@ -1977,6 +1978,26 @@ 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. + 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 @@ -2588,130 +2609,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. - "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(url) = p.get("url").and_then(Value::as_str).map(str::trim).filter(|u| !u.is_empty()) else { - return self.run("ui.dialog", json!({"name": "addUrl"})); - }; - 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()); - } - // 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( - url, - 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( - url, - 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(); - 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 = url.to_string(); - let mut q = json!({}); - for k in ["name", "layer", "crs"] { - 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 p.get("zoom").and_then(Value::as_bool) == Some(true) { - q["zoom"] = json!(true); - } - for k in ["service", "coverage"] { - 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(); - rustgis_engine::render::rustgis_tiles::cache::download_limited( - &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)?; @@ -3682,18 +3580,166 @@ 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); + 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(url) = p.get("url").and_then(Value::as_str).map(str::trim).filter(|u| !u.is_empty()) else { + return self.run("ui.dialog", json!({"name": "addUrl"})); + }; + if url.to_ascii_lowercase().split(['?', '#']).next().is_some_and(|u| u.ends_with(".pmtiles")) { + return if let Some(id) = claim { + self.session.add_in_place_for(id, p) + } else { + connectiontree::run_named(self, "layer.addUrl", p.clone(), origin.as_deref()) + }; + } + // 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( + url, + 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( + url, + 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(); + 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 = url.to_string(); + let mut q = json!({}); + for k in ["name", "layer", "crs"] { + 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 p.get("zoom").and_then(Value::as_bool) == Some(true) { + q["zoom"] = json!(true); + } + for k in ["service", "coverage"] { + 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(); + rustgis_engine::render::rustgis_tiles::cache::download_limited( + &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.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 { @@ -3703,16 +3749,26 @@ impl RustGisApp { p["cog"] = json!(true); } let before = self.session.sources.generation; - if self.run("layer.addUrl", p).is_ok() { + let added = if let Some(id) = claim { self.session.add_in_place_for(id, &p) } else { self.run("layer.addUrl", p) }; + if added.is_ok() { connectiontree::name_recent(self, before, origin.as_deref()); self.map.invalidate(); } } Ok(false) => { p["whole"] = json!(true); - let _ = connectiontree::run_add_url(self, p, origin.as_deref()); + if claim.is_some() { + let _ = self.add_url(&p, None, claim); + } else { + let _ = connectiontree::run_add_url(self, p, origin.as_deref()); + } + } + Err(e) => { + if let Some(id) = claim { + self.session.download_failed(id); + } + self.session.notify(if e.starts_with(&url) { e } else { format!("{url}: {e}") }, true) } - Err(e) => self.session.notify(if e.starts_with(&url) { e } else { format!("{url}: {e}") }, true), } } let mut landed = Vec::new(); @@ -3752,7 +3808,7 @@ impl RustGisApp { } let before = self.session.sources.generation; let pending = p.clone(); - match self.run("layer.addDownload", p) { + match self.session.complete_download(id, &p) { Ok(r) => { connectiontree::name_recent(self, before, origin.as_deref()); if r.get("zoomed").and_then(Value::as_bool) == Some(true) { @@ -3941,8 +3997,8 @@ impl RustGisApp { } // 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) { + for request in self.session.take_url_requests() { + if let Err(e) = self.add_url(request.params(), Some(&request), None) { self.session.notify(e, true); } } @@ -4740,13 +4796,13 @@ mod peer_output_tests { 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.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.open_peer_text(&text).unwrap(); assert_ne!(app.session.opened, generation); - assert!(app.session.trusted_tasks.is_empty()); + 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")); } 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/modelbuilder.rs b/crates/ui-egui/src/modelbuilder.rs index 013fa79e6d..ad897c5585 100644 --- a/crates/ui-egui/src/modelbuilder.rs +++ b/crates/ui-egui/src/modelbuilder.rs @@ -355,7 +355,7 @@ fn untitled(app: &RustGisApp) -> String { fn load_saved(app: &mut RustGisApp, name: &str) { let Some(m) = app.session.project.models.iter().find(|m| m.name.eq_ignore_ascii_case(name)) else { return }; let graph = m.graph.clone().unwrap_or_else(|| model::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); } diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 7dc974e37a..a84e3408a5 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -2751,7 +2751,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); } @@ -2802,7 +2802,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); } @@ -2861,7 +2861,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() { diff --git a/crates/ui-egui/src/pluginpanel.rs b/crates/ui-egui/src/pluginpanel.rs index 5642f530e5..665f106805 100644 --- a/crates/ui-egui/src/pluginpanel.rs +++ b/crates/ui-egui/src/pluginpanel.rs @@ -576,6 +576,50 @@ 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())); + } + } + /// 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] diff --git a/crates/ui-egui/src/serviceui.rs b/crates/ui-egui/src/serviceui.rs index 10c0377497..69243ee2ef 100644 --- a/crates/ui-egui/src/serviceui.rs +++ b/crates/ui-egui/src/serviceui.rs @@ -365,29 +365,36 @@ pub fn poll_pages(app: &mut RustGisApp, ctx: &egui::Context) { /// The next answer fetched, or the collection added (with a note when some features were left /// unread), or why it couldn't be fetched said. fn go_on(app: &mut RustGisApp, landed: Landed, left: usize, step: Step) { - match step { - // Not given by `ogc::begin`, which reads in GeoJSON. - Step::Decoded(..) => go_on(app, landed, left, Step::Failed("the pages were read as protocol buffers".into())), - Step::Failed(e) => { - app.session.download_failed(landed.id); - let url = &landed.url; - app.session.notify(if e.starts_with(url.as_str()) { e } else { format!("{url}: {e}") }, true); - } - Step::Fetch(pages) => { - let slot = match rustgis_engine::render::rustgis_tiles::address::check(pages.next_url(), Some(pages.referrer())) { - // A layer's description: small, and soon given up on. - Ok(()) if pages.describing() => fetch_description(pages.next_url(), left.min(ogc::DESCRIPTION_MAX)), - Ok(()) => fetch_into_limited(pages.next_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) => { - if let Some(note) = note { - let name = landed.params.get("name").and_then(Value::as_str).filter(|n| !n.is_empty()).unwrap_or(&landed.url); - app.session.notify(format!("{name}: {note}"), false); + if let Err(e) = app.session.download_continues(landed.id) { + 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 { + // Not given by `ogc::begin`, which reads in GeoJSON. + Step::Decoded(..) => go_on(app, landed, left, Step::Failed("the pages were read as protocol buffers".into())), + Step::Failed(e) => { + app.session.download_failed(landed.id); + let url = &landed.url; + app.session.notify(if e.starts_with(url.as_str()) { e } else { format!("{url}: {e}") }, true); + } + Step::Fetch(pages) => { + let slot = match rustgis_engine::render::rustgis_tiles::address::check(pages.next_url(), Some(pages.referrer())) { + // A layer's description: small, and soon given up on. + Ok(()) if pages.describing() => fetch_description(pages.next_url(), left.min(ogc::DESCRIPTION_MAX)), + Ok(()) => fetch_into_limited(pages.next_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) => { + if let Some(note) = note { + let name = landed.params.get("name").and_then(Value::as_str).filter(|n| !n.is_empty()).unwrap_or(&landed.url); + app.session.notify(format!("{name}: {note}"), false); + } + app.add_landed(landed, bytes); } - app.add_landed(landed, bytes); } - } + }); } diff --git a/docs/mcp.md b/docs/mcp.md index b12a6f3db8..305d6cca6d 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -383,6 +383,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 78c7b901f2..e819e7f9fc 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 1d777f0547..ae8d02845a 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1034,7 +1034,7 @@ their name. of the flow, stay on the map. A run that fails is undone as a whole. - **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. The Workflows pane's Models section lists the project's models, and New Model From History turns diff --git a/docs/user-guide/assistant.md b/docs/user-guide/assistant.md index 956c02ed33..1a3bdf66a3 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 @@ -245,7 +259,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 @@ -358,3 +376,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/workspace.md b/docs/user-guide/workspace.md index fcd8b71172..68bfa51636 100644 --- a/docs/user-guide/workspace.md +++ b/docs/user-guide/workspace.md @@ -397,8 +397,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. From d1a5804c6a993f6026a8a724f99d0df152f2c39c Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 15:56:07 -0600 Subject: [PATCH 03/14] fix(data): preserve clean queries and complete HTTP test responses Keep credential-free query bytes unchanged and retain counted loss reporting for real removals. Exercise clean and lossy saves and rejection of non-object native background raster parameters. Make the plugin preparation fixture consume bounded request headers on a blocking socket and retain the peer through worker completion. Keep allowed/revoked assertions and expose errors and elapsed time. --- crates/data/src/address.rs | 14 ++++++- crates/engine/src/tests/credential_output.rs | 24 +++++++++++ crates/engine/src/tests/permission_policy.rs | 42 ++++++++++++++++++-- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/crates/data/src/address.rs b/crates/data/src/address.rs index 014ecbff18..ff290ba15f 100644 --- a/crates/data/src/address.rs +++ b/crates/data/src/address.rs @@ -235,7 +235,7 @@ pub fn redact_query(address: &mut String) { let Some(at) = address.find('?').filter(|at| address.find('#').is_none_or(|end| *at < end)) else { return }; let q = query(address); let names = QueryNames::of(q); - if q.split('&').all(|p| !p.is_empty() && pair_class(p, &names) == NameClass::Plain) { + if q.split('&').all(|p| pair_class(p, &names) == NameClass::Plain) { return; } let kept = q.split('&').filter(|p| !p.is_empty() && pair_class(p, &names) == NameClass::Plain).collect::>().join("&"); @@ -552,7 +552,17 @@ mod tests { assert!(public.ends_with("layer=a%20b&f=json&%zz=1"), "{public}"); assert!(removed > 0); } - for query in ["layer=a%20b&f=json&%zz=1", "layer=token%3DM", "code=EPSG:4326", "sv=2026&layer=1", r"layer=a\b&f=json"] { + for query in [ + "layer=a%20b&f=json&%zz=1", + "layer=token%3DM", + "code=EPSG:4326", + "sv=2026&layer=1", + r"layer=a\b&f=json", + "", + "&", + "layer=1&&f=json", + "layer=1&", + ] { let address = format!("https://h.invalid/a?{query}"); assert_eq!(public_web_address(&address), Public::Unchanged); } diff --git a/crates/engine/src/tests/credential_output.rs b/crates/engine/src/tests/credential_output.rs index 8104432498..34e9904786 100644 --- a/crates/engine/src/tests/credential_output.rs +++ b/crates/engine/src/tests/credential_output.rs @@ -113,6 +113,30 @@ fn durable_writers_clean_all_owned_roles_and_preserve_the_live_project() { assert!(code.to_string().contains(DATA), "replayable data properties must remain"); } +/// Empty query pairs survive clean saves; an actual token still gives one counted loss. +#[test] +fn durable_empty_query_pairs_preserve_clean_urls_and_report_real_loss() { + for (query, kept, losses) in [ + ("", "", 0), + ("&", "&", 0), + ("layer=1&&f=json", "layer=1&&f=json", 0), + ("layer=1&", "layer=1&", 0), + ("layer=1&&token=RGSYNTH_empty_query&", "layer=1", 1), + ] { + let url = format!("https://h.invalid/tiles/{{z}}/{{x}}/{{y}}.png?{query}"); + let mut project = Project::default(); + project.map_mut().tiles.push(rustgis_map::TileLayer::new("Clean", rustgis_map::TileKind::Raster, &url)); + let before = serde_json::to_value(&project).unwrap(); + let (saved, report) = project.saved_text(None).unwrap(); + assert_eq!(report.layer_addresses(), losses, "{url}"); + assert_eq!(report.notices().len(), usize::from(losses > 0), "{url}: {:?}", report.notices()); + let saved: Value = serde_json::from_str(&saved).unwrap(); + let expected = format!("https://h.invalid/tiles/{{z}}/{{x}}/{{y}}.png?{kept}"); + assert_eq!(saved["maps"][0]["tiles"][0]["url"], expected); + assert_eq!(serde_json::to_value(&project).unwrap(), before); + } +} + #[test] fn durable_clean_and_opaque_content_keep_their_meaning() { let mut s = marked(); diff --git a/crates/engine/src/tests/permission_policy.rs b/crates/engine/src/tests/permission_policy.rs index 4d3eaa7071..2a4ae79944 100644 --- a/crates/engine/src/tests/permission_policy.rs +++ b/crates/engine/src/tests/permission_policy.rs @@ -333,6 +333,25 @@ fn permission_policy_native_classification_keeps_scope_through_completion() { 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"))] @@ -991,7 +1010,7 @@ fn permission_policy_stored_cross_map_clipboard_and_managed_share_targets() { fn permission_policy_plugin_preparation_keeps_owner_before_remote_send() { use rustgis_data::source::{Batch, FeatureSource, Request, SourceInfo}; use std::{ - io::Write, + io::{Read, Write}, sync::{Mutex, mpsc}, time::Duration, }; @@ -1047,8 +1066,9 @@ fn permission_policy_plugin_preparation_keeps_owner_before_remote_send() { }); waiting.recv_timeout(Duration::from_secs(3)).unwrap(); live.0.store(!revoke, Ordering::SeqCst); + let released_at = std::time::Instant::now(); release.send(()).unwrap(); - if !revoke { + let peer = if !revoke { let start = std::time::Instant::now(); let mut peer = loop { match listener.accept() { @@ -1059,10 +1079,24 @@ fn permission_policy_plugin_preparation_keeps_owner_before_remote_send() { 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(); - assert_eq!(result.is_err(), revoke); + 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); From c4734e0139f669b5b078dd896b775b14fea2c95b Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 16:56:09 -0600 Subject: [PATCH 04/14] fix(plugins): preserve caller cancellation in pinned tools Run pinned synchronous plugin work under the caller control and let plugin host polling observe the existing data progress carrier. Keep the exact admitted load and the explicit plugin control. Exercise real engine and analysis process calls: cancel after guest entry, before its valid response is released, then require prompt failure with no applied output and a failed engine history record. --- crates/engine/src/cmd/gp.rs | 4 +- crates/engine/src/tests/permission_policy.rs | 71 ++++++++++++++++++++ crates/plugins/src/testing.rs | 10 +++ crates/plugins/src/tools.rs | 3 +- 4 files changed, 86 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index b46f673ee0..e00e11876b 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -1158,7 +1158,9 @@ pub(crate) fn execute( 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_plugins::tools::prepare_pinned(plugin.plugin(), tool, &inputs, params)?.run(&rustgis_plugins::tools::Control::default()) + 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()) }; diff --git a/crates/engine/src/tests/permission_policy.rs b/crates/engine/src/tests/permission_policy.rs index 2a4ae79944..49b56b8ba4 100644 --- a/crates/engine/src/tests/permission_policy.rs +++ b/crates/engine/src/tests/permission_policy.rs @@ -1102,6 +1102,77 @@ fn permission_policy_plugin_preparation_keeps_owner_before_remote_send() { 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() { + 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() { diff --git a/crates/plugins/src/testing.rs b/crates/plugins/src/testing.rs index ef36c8bea0..73dcc0076d 100644 --- a/crates/plugins/src/testing.rs +++ b/crates/plugins/src/testing.rs @@ -335,6 +335,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/tools.rs b/crates/plugins/src/tools.rs index ae0ee43f9d..b61dd40f4d 100644 --- a/crates/plugins/src/tools.rs +++ b/crates/plugins/src/tools.rs @@ -186,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() } } From 107781468f0a1fba59189bfc6eff754602875546 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 20:09:34 -0600 Subject: [PATCH 05/14] fix(engine): align approval with command execution reach Use executor-owned selectors for classification inputs, normalized model names, rendered time frames and tile metadata. Keep exact raw routes bound to approval and require actual file/network/automation reach while allowing memory-only commands without file permission. Preserve queued retries on failed preparation, report early file acknowledgment failures, and avoid duplicate late-download errors. Normalize accepted fixture sockets for portable HTTP reads and qualify the documented credential and discovery-notice scope. --- crates/engine/src/cmd/deeplearning.rs | 5 +- crates/engine/src/cmd/gp.rs | 28 ++- crates/engine/src/cmd/notebook.rs | 2 +- crates/engine/src/cmd/pg.rs | 2 +- crates/engine/src/cmd/plugin_fetch.rs | 9 +- crates/engine/src/cmd/project.rs | 2 +- crates/engine/src/cmd/search.rs | 42 +++- crates/engine/src/cmd/sharing.rs | 2 +- crates/engine/src/cmd/sources.rs | 4 +- crates/engine/src/cmd/tiles.rs | 114 +++++++++-- crates/engine/src/cmd/time.rs | 80 ++++++-- crates/engine/src/cmd/workflows.rs | 10 +- crates/engine/src/cmd/workspace.rs | 36 +++- crates/engine/src/lib.rs | 5 +- crates/engine/src/policy/effects.rs | 20 +- crates/engine/src/policy/provenance.rs | 12 +- crates/engine/src/tests.rs | 4 + crates/engine/src/tests/permission_policy.rs | 37 ++++ crates/engine/src/tests/review_boundaries.rs | 200 ++++++++++++++++++- crates/engine/src/tests/service_logins.rs | 2 + crates/engine/src/tests/task_reentry.rs | 160 +++++++++++++++ crates/map/src/lib.rs | 23 +-- crates/map/src/tiles.rs | 24 +++ crates/ui-egui/src/lib.rs | 39 +++- docs/user-guide/data.md | 7 +- docs/user-guide/web-data.md | 8 +- docs/user-guide/workspace.md | 14 +- 27 files changed, 767 insertions(+), 124 deletions(-) diff --git a/crates/engine/src/cmd/deeplearning.rs b/crates/engine/src/cmd/deeplearning.rs index 192c943dc2..f28008829f 100644 --- a/crates/engine/src/cmd/deeplearning.rs +++ b/crates/engine/src/cmd/deeplearning.rs @@ -486,8 +486,11 @@ fn training_inputs(input: &super::InputContext<'_>, p: &Value) -> Result, 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)?; diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index e00e11876b..68a56d7a7b 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -1599,22 +1599,23 @@ mod file_tests { /// 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; + struct Live(std::sync::atomic::AtomicBool); impl crate::policy::Revocation for Live { fn allows(&self, _: crate::policy::Effects) -> bool { - true + self.0.load(std::sync::atomic::Ordering::SeqCst) } } let mut s = task_session(json!([])); - let owner = crate::policy::Scope::agent( + 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-owner", - Some("file-owner".into()), - std::sync::Arc::new(Live), + "file-foreign", + None, + std::sync::Arc::new(Live(std::sync::atomic::AtomicBool::new(true))), ); - let foreign = - crate::policy::Scope::agent(crate::policy::AgentAccess::Allow, false, "file-foreign", None, std::sync::Arc::new(Live)); 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(); @@ -1624,7 +1625,16 @@ mod file_tests { 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, &crate::policy::Call::command("edit.undo", Value::Null)).unwrap(); + s.run_scoped(owner.clone(), &crate::policy::Call::command("edit.undo", Value::Null)).unwrap(); + // A host callback may revoke authority after taking the bytes but before acknowledgment. + let pending = s.run_scoped(owner, &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"); } #[test] diff --git a/crates/engine/src/cmd/notebook.rs b/crates/engine/src/cmd/notebook.rs index 4f6f43e4f0..9ea1bd0320 100644 --- a/crates/engine/src/cmd/notebook.rs +++ b/crates/engine/src/cmd/notebook.rs @@ -212,7 +212,7 @@ pub fn commands() -> Vec { .params(r#"{"notebook"?, "cell": index}"#), C::new("notebook.run", "Run Cells", "Notebook", run) .roles(&[]) - .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) + .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| { diff --git a/crates/engine/src/cmd/pg.rs b/crates/engine/src/cmd/pg.rs index 019f710122..5f396c805e 100644 --- a/crates/engine/src/cmd/pg.rs +++ b/crates/engine/src/cmd/pg.rs @@ -33,7 +33,7 @@ const MAX_LISTED: usize = 5_000; /// Keeps database writes reserved and declares their network reach for live permission checks. pub fn commands() -> Vec { vec![ - 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::Files)).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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().when(need_connection), diff --git a/crates/engine/src/cmd/plugin_fetch.rs b/crates/engine/src/cmd/plugin_fetch.rs index cacaa084e2..58cb413de1 100644 --- a/crates/engine/src/cmd/plugin_fetch.rs +++ b/crates/engine/src/cmd/plugin_fetch.rs @@ -289,6 +289,7 @@ impl Session { if tries >= MAX_RETRIES { return Err(format!("{id}: gave up after fetching {MAX_RETRIES} times")); } + let continuation = self.continuation()?; // One waiting run per command: the latest call's, which replaces an earlier one. if let Some(i) = self.plugin_retries.iter().position(|r| r.id == id) { self.plugin_retries.remove(i); @@ -302,13 +303,7 @@ impl Session { // the addresses. let fetching: Vec<&str> = urls.iter().map(|k| k.split_once(' ').map_or(k.as_str(), |(_, u)| u)).collect(); let answer = json!({"pending": true, "fetching": fetching}); - self.plugin_retries.push(Retry { - id: id.to_string(), - params: params.clone(), - urls, - tries: tries + 1, - continuation: self.continuation()?, - }); + self.plugin_retries.push(Retry { id: id.to_string(), params: params.clone(), urls, tries: tries + 1, continuation }); Ok(answer) } diff --git a/crates/engine/src/cmd/project.rs b/crates/engine/src/cmd/project.rs index eee7161b22..02d85f160f 100644 --- a/crates/engine/src/cmd/project.rs +++ b/crates/engine/src/cmd/project.rs @@ -29,7 +29,7 @@ pub fn commands() -> Vec { 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(&[]).effects(crate::policy::Effects::NONE).view().params(r#"{"nameFilter"?: string, "includeFields"?: bool}"#), + 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. diff --git a/crates/engine/src/cmd/search.rs b/crates/engine/src/cmd/search.rs index 6ee418891c..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(&[]).effects(crate::policy::Effects::NONE).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/sharing.rs b/crates/engine/src/cmd/sharing.rs index 219d50fce8..437ad59710 100644 --- a/crates/engine/src/cmd/sharing.rs +++ b/crates/engine/src/cmd/sharing.rs @@ -156,7 +156,7 @@ 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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) + }).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"))).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)) diff --git a/crates/engine/src/cmd/sources.rs b/crates/engine/src/cmd/sources.rs index 0c69da0579..7498992862 100644 --- a/crates/engine/src/cmd/sources.rs +++ b/crates/engine/src/cmd/sources.rs @@ -14,10 +14,10 @@ pub fn commands() -> Vec { 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(&[]).effects(crate::policy::Effects::NONE) + 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(&[]).effects(crate::policy::Effects::NONE) + 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/tiles.rs b/crates/engine/src/cmd/tiles.rs index d5dd821895..969864ae9e 100644 --- a/crates/engine/src/cmd/tiles.rs +++ b/crates/engine/src/cmd/tiles.rs @@ -70,10 +70,10 @@ pub fn commands() -> Vec { ), 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(&[]).effects(crate::policy::Effects::NONE).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(&[]).effects(crate::policy::Effects::NONE).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")?; @@ -205,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); @@ -550,13 +564,44 @@ fn tile_source_state(t: &TileLayer) -> Value { /// 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")?; - let map = input.map(); - let tile = map.tiles.get(i).ok_or("no tile layer at that 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()); - inputs.state = json!({"map":map.instance_id(),"index":i,"tile":tile_source_state(tile)}); - inputs.review_places.push(crate::sources::redact(&tile.url)); - Ok(inputs) + 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. @@ -580,12 +625,37 @@ fn tile_source_reach(t: &TileLayer) -> crate::policy::Effects { 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)); - match tile { - Some(t) if t.bounds.is_none() && matches!(t.kind, TileKind::PmTiles | TileKind::MbTiles | TileKind::Cog) => tile_source_reach(t), - _ => crate::policy::Effects::NONE, + 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 { @@ -602,14 +672,16 @@ pub(crate) fn render_inputs(map: &rustgis_map::Map) -> super::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 { - let mut reach = crate::policy::Effects::NONE; - for tile in map.tiles.iter().filter(|t| t.draws() && t.opacity > 0.0) { - reach = reach.union(tile_source_reach(tile)); - if !tile.legends.is_empty() { - reach = reach.with(crate::policy::Effect::Network); - } + 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; } - reach + 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. diff --git a/crates/engine/src/cmd/time.rs b/crates/engine/src/cmd/time.rs index dbef9600b3..205c28a362 100644 --- a/crates/engine/src/cmd/time.rs +++ b/crates/engine/src/cmd/time.rs @@ -81,7 +81,7 @@ pub fn commands() -> Vec { .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(&[]).effects(crate::policy::Effects::NONE).view(), - C::new("time.export", "Export Time Animation", "View › Time", export).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) + 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)))); diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index 72357812ca..421ed61458 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -556,11 +556,17 @@ fn run_model(s: &mut Session, p: &Value) -> CmdResult { } } +/// 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")); diff --git a/crates/engine/src/cmd/workspace.rs b/crates/engine/src/cmd/workspace.rs index 16a1a99c6e..7d9add4cf1 100644 --- a/crates/engine/src/cmd/workspace.rs +++ b/crates/engine/src/cmd/workspace.rs @@ -78,13 +78,13 @@ pub fn commands() -> Vec { let t = thumbnail(s, p)?; s.project.thumbnail = t; Ok(json!({"thumbnail": s.project.thumbnail.is_some()})) - }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) + }).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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) + }).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| { @@ -167,7 +167,7 @@ pub fn commands() -> Vec { 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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) + }).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. @@ -233,9 +233,9 @@ pub fn commands() -> Vec { .params(r#"{"layer"? (every visible feature layer when omitted)}"#), 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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) + 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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).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(); @@ -256,7 +256,7 @@ pub fn commands() -> Vec { } } Ok(Value::Null) - }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) + }).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")?; @@ -283,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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) + }).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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view(), C::new("cache.empty", "Empty Cache", "Data › Feature Cache", |s, _| { let mut n = 0; @@ -318,7 +318,7 @@ pub fn commands() -> Vec { fill_cache(s); } Ok(json!({"autoCache": s.auto_cache})) - }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) + }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params(r#"{"on"?: bool}"#), ] @@ -360,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))); } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index b8385b7802..8a85a9e07c 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -1155,8 +1155,11 @@ 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 { + let pending = self.download_is_pending(id); if let Err(e) = self.download_continues(id) { - self.notify(e, true); + if pending { + self.notify(e, true); + } return false; } let Some(i) = self.downloading.iter().position(|d| *d == id) else { return false }; diff --git a/crates/engine/src/policy/effects.rs b/crates/engine/src/policy/effects.rs index ffbfb09c76..11d4d45cdc 100644 --- a/crates/engine/src/policy/effects.rs +++ b/crates/engine/src/policy/effects.rs @@ -60,6 +60,9 @@ pub(super) fn declared(spec: &CommandSpec, params: &Value) -> Effects { 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. @@ -81,11 +84,18 @@ pub(super) fn declared(spec: &CommandSpec, params: &Value) -> 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) -> Effects { let mut effects = declared(spec, params); - if spec.id == "map.zoomToTiles" { - effects = effects.union(crate::cmd::tiles::metadata_reach(s, params)); - } - if matches!(spec.id, "layout.export" | "presentation.export" | "share.exportMap") - && let Some(select) = spec.inputs + 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. diff --git a/crates/engine/src/policy/provenance.rs b/crates/engine/src/policy/provenance.rs index a8140294b1..3cc980f063 100644 --- a/crates/engine/src/policy/provenance.rs +++ b/crates/engine/src/policy/provenance.rs @@ -57,7 +57,11 @@ impl Session { "model.save" => "model", _ => return None, }; - let name = self.item_name(kind, p).ok().or_else(|| p.get("name").and_then(Value::as_str).map(str::to_string))?; + 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)) @@ -68,7 +72,11 @@ impl Session { return; } if let Some((kind, _, true)) = editor { - let name = p.get("rename").and_then(Value::as_str).map(str::to_string).or_else(|| self.item_name(kind, p).ok()); + let name = if id == "model.save" { + result.get("model").and_then(Value::as_str).map(str::to_owned) + } else { + p.get("rename").and_then(Value::as_str).map(str::to_string).or_else(|| self.item_name(kind, p).ok()) + }; if let Some(name) = name { self.trust_record(kind, &name); } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 6634b75318..5e18830d4d 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -12240,10 +12240,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(); diff --git a/crates/engine/src/tests/permission_policy.rs b/crates/engine/src/tests/permission_policy.rs index 49b56b8ba4..cbc00816ee 100644 --- a/crates/engine/src/tests/permission_policy.rs +++ b/crates/engine/src/tests/permission_policy.rs @@ -24,6 +24,43 @@ fn pending(s: &Session, scope: &Scope, call: &Call) -> Pending { } } +/// 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() { diff --git a/crates/engine/src/tests/review_boundaries.rs b/crates/engine/src/tests/review_boundaries.rs index 30e0c21599..89e019c720 100644 --- a/crates/engine/src/tests/review_boundaries.rs +++ b/crates/engine/src/tests/review_boundaries.rs @@ -1,4 +1,4 @@ -//! Review regressions exercise public admission and opaque download ownership without I/O. +//! 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; @@ -142,3 +142,201 @@ fn permission_review_non_s3_download_survives_an_unrelated_storage_login() { 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:?}"); +} diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index 0239088fdd..a0e7b73e71 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -44,6 +44,7 @@ struct LoginFile { #[cfg(not(target_arch = "wasm32"))] impl LoginFile { + /// Reads bounded complete headers on a blocking accepted socket on every native platform. fn new(body: Vec) -> Self { use std::io::{Read, Write}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -63,6 +64,7 @@ impl LoginFile { } Err(_) => break, }; + stream.set_nonblocking(false).unwrap(); stream.set_read_timeout(Some(std::time::Duration::from_secs(5))).unwrap(); let mut request = Vec::new(); let mut chunk = [0u8; 4096]; diff --git a/crates/engine/src/tests/task_reentry.rs b/crates/engine/src/tests/task_reentry.rs index 97b5b05cbd..08576b127a 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_account_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_account_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_account_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/map/src/lib.rs b/crates/map/src/lib.rs index ebce751cef..3666fe22c2 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -1494,35 +1494,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()); } } 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/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index eb0adf880c..e2f2dbeb4d 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1695,6 +1695,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 { @@ -1703,18 +1713,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.acknowledge_gp_file(id, params.get("error").and_then(Value::as_str)); + // 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 @@ -5465,6 +5471,19 @@ 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()); } #[test] @@ -5885,6 +5904,12 @@ 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); } } diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index 228c585120..abdc10fdae 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -1203,10 +1203,11 @@ routes, areas, observations…) to draw into. Add Folder connects a folder to th also lists the project's layouts, styles and tasks. A task's steps each run a command; New Task From History turns the geoprocessing history into a task you can replay. -Saved projects, templates, map and layer files, and packages remove credentials from supported -addresses and recognized executable parameters. The open project keeps its credentials. Removal +Saved projects, templates, map and layer files, and packages remove recognized credentials from +supported addresses and recognized executable parameters. The open project keeps its credentials. Removal notices describe what a shared workflow or video will need again; an unchecked-content notice -means opaque content was kept and needs your review. +means opaque content was kept and needs your review. Unrecognized credentials can remain in +addresses without a notice, so review addresses and preserved content before sharing. RustGIS remembers up to 32 distinct saved snapshots for explicit project reopening, with at most 1 MiB of restoration data per snapshot and 8 MiB in total. A snapshot must match, and only its diff --git a/docs/user-guide/web-data.md b/docs/user-guide/web-data.md index 84f75a7fad..a8fb1c4c9c 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 345db1dba8..eb8fe30786 100644 --- a/docs/user-guide/workspace.md +++ b/docs/user-guide/workspace.md @@ -32,12 +32,14 @@ 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. -Saved layer and video addresses, and recognized task, model, notebook and history parameters, -omit 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. 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. +for the supported scope. ### Autosave and recovery @@ -45,7 +47,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. From 55a69f5a1f972ddebd37f43d9b4969ee8a9644fe Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 21:19:59 -0600 Subject: [PATCH 06/14] fix(security): retain project ownership through service discovery Bind deferred OGC, coverage and asset-signing completions to the project that started them before consuming responses, caching tokens or starting follow-up work. Report retired reads once without overwriting current dialogs. Preserve clean padded URL bytes without false credential-loss notices, and declare file reach for PostGIS edit connections. --- crates/data/src/address.rs | 8 +- crates/engine/src/cmd/pg.rs | 4 +- crates/engine/src/tests/credential_output.rs | 3 +- crates/engine/src/tests/review_boundaries.rs | 14 ++- crates/ui-egui/src/connectiontree.rs | 73 +++++++++++- crates/ui-egui/src/lib.rs | 116 +++++++++++++++++-- crates/ui-egui/src/serviceui.rs | 4 +- 7 files changed, 199 insertions(+), 23 deletions(-) diff --git a/crates/data/src/address.rs b/crates/data/src/address.rs index ff290ba15f..687f664081 100644 --- a/crates/data/src/address.rs +++ b/crates/data/src/address.rs @@ -327,7 +327,7 @@ pub fn public_web_address(address: &str) -> Public { u16::try_from(removed).unwrap_or(u16::MAX).saturating_add(u16::from(at.is_some())).saturating_add(u16::from(rest.contains('#'))); let mut public = candidate; redact_query(&mut public); - if public == address { Public::Unchanged } else { Public::Changed { address: public, removed } } + if public == text { Public::Unchanged } else { Public::Changed { address: public, removed } } } /// Cleans addresses in generated text and counts changed addresses. All spacing and all other @@ -573,7 +573,11 @@ mod tests { for address in [r"https://h.invalid\a?token=RGSYNTH_boundary", r"https://h.invalid/a\b?token=RGSYNTH_boundary"] { assert_eq!(public_web_address(address), Public::Withheld); } - for template in ["https://{switch:a,b}.host/{z}/{x}/{y}@2x.png?map=m", "https://{s}.host/{zoom}/{x}/{y}@2x.png"] { + for template in [ + "https://{switch:a,b}.host/{z}/{x}/{y}@2x.png?map=m", + "https://{s}.host/{zoom}/{x}/{y}@2x.png", + " \thttps://h.invalid/a?f=json \n", + ] { assert_eq!(public_web_address(template), Public::Unchanged); } assert_eq!( diff --git a/crates/engine/src/cmd/pg.rs b/crates/engine/src/cmd/pg.rs index 5f396c805e..e1e8f1f9b2 100644 --- a/crates/engine/src/cmd/pg.rs +++ b/crates/engine/src/cmd/pg.rs @@ -30,7 +30,7 @@ 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 their network reach for live permission checks. +/// 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(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Network).with(crate::policy::Effect::Files)).view().params( @@ -40,7 +40,7 @@ pub fn commands() -> Vec { 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)) + 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(&[]).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)"}"#), diff --git a/crates/engine/src/tests/credential_output.rs b/crates/engine/src/tests/credential_output.rs index 34e9904786..9bb43993e5 100644 --- a/crates/engine/src/tests/credential_output.rs +++ b/crates/engine/src/tests/credential_output.rs @@ -113,7 +113,7 @@ fn durable_writers_clean_all_owned_roles_and_preserve_the_live_project() { assert!(code.to_string().contains(DATA), "replayable data properties must remain"); } -/// Empty query pairs survive clean saves; an actual token still gives one counted loss. +/// Empty query pairs and clean padding survive saves; an actual token gives one counted loss. #[test] fn durable_empty_query_pairs_preserve_clean_urls_and_report_real_loss() { for (query, kept, losses) in [ @@ -121,6 +121,7 @@ fn durable_empty_query_pairs_preserve_clean_urls_and_report_real_loss() { ("&", "&", 0), ("layer=1&&f=json", "layer=1&&f=json", 0), ("layer=1&", "layer=1&", 0), + ("layer=1 \t", "layer=1 \t", 0), ("layer=1&&token=RGSYNTH_empty_query&", "layer=1", 1), ] { let url = format!("https://h.invalid/tiles/{{z}}/{{x}}/{{y}}.png?{query}"); diff --git a/crates/engine/src/tests/review_boundaries.rs b/crates/engine/src/tests/review_boundaries.rs index 89e019c720..41879e2e6e 100644 --- a/crates/engine/src/tests/review_boundaries.rs +++ b/crates/engine/src/tests/review_boundaries.rs @@ -61,17 +61,19 @@ fn permission_review_memory_only_commands_need_no_external_access() { #[test] fn permission_review_postgis_write_declares_network_reach() { - struct NoNetwork; - impl Revocation for NoNetwork { + struct Without(Effect); + impl Revocation for Without { fn allows(&self, effects: Effects) -> bool { - !effects.contains(Effect::Network) + !effects.contains(self.0) } } let s = world(); - let scope = Scope::assistant(1462, Arc::new(NoNetwork)); let call = Call::command("pg.saveEdits", json!({"layer":"Cities"})); - assert!(matches!(s.review(&scope, &call), Outcome::Refused(_)), "a database write must check live network authority before any I/O"); - assert!(matches!(s.review(&scope, &Call::command("pg.pendingEdits", json!({"layer":"Cities"}))), Outcome::Allowed)); + 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] diff --git a/crates/ui-egui/src/connectiontree.rs b/crates/ui-egui/src/connectiontree.rs index da36cb6ce3..268cc4ea64 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) { + 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(()) } @@ -1038,6 +1044,7 @@ mod tests { 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"}]}]}"#; + let opened = h.state().session.opened; let step = |fallback: &str, origin: Option<&str>| { let slot = Slot::default(); *slot.lock().unwrap() = Some(Err("unreachable".to_string())); @@ -1053,7 +1060,7 @@ mod tests { fallback: fallback.into(), slot, }; - crate::ServiceStep::Tiles { resolve, dialog: false, origin: origin.map(str::to_string) } + crate::ServiceStep::Tiles { resolve, dialog: false, origin: origin.map(str::to_string), opened } }; let before = h.state().session.sources.recent.clone(); h.state_mut().ui.service_steps.push(step("http://127.0.0.1/{z}/{x}/{y}", Some("Atlas"))); @@ -1129,6 +1136,68 @@ 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 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| { + seen.lock().unwrap().push(url); + callbacks.lock().unwrap().push(done); + }); + let mut app = RustGisApp::with_sample(); + let (text, _) = app.session.project.saved_text(None).unwrap(); + let href = format!("https://review29{case}.blob.core.windows.net/old-assets/points.csv"); + let token_url = stac::sas_token_url(&href).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().len(), 1); + 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().len(), 1, "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().len(), 2, "a current signing response starts 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/lib.rs b/crates/ui-egui/src/lib.rs index e2f2dbeb4d..1e2a380455 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -438,12 +438,13 @@ 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 }, + Tiles { resolve: TilesResolve, dialog: bool, origin: Option, opened: u64 }, /// A WCS coverage whose description is being read before it is asked for: `service.coverage`'s /// parameters and the description on its way. - Coverage { params: Value, slot: Slot, String>>, dialog: bool, origin: Option }, + Coverage { params: Value, slot: Slot, String>>, dialog: bool, origin: Option, opened: u64 }, } /// Fetches an address into a slot, off the frame. @@ -4463,6 +4464,18 @@ impl RustGisApp { } } + /// Retires an old service read before its response can start work in another project. + /// The existing closed-project message is informational and emitted once per retired item. + fn service_read_current(&mut self, opened: u64, address: &str) -> bool { + if opened == self.session.opened { + return true; + } + let url = serviceui::address_in_message(address); + let why = i18n::fmt("{url}: its project was closed while it was read, so it wasn't added", &[("url", &url)]); + self.session.notify(why, false); + false + } + /// Moves the service layers waiting on a document along: an OGC API layer's tile template /// (read from its tileset list, then the tileset when the list doesn't carry it; the layer is /// added with the template read, or the usual path) and a WCS coverage's description (the @@ -4471,9 +4484,12 @@ impl RustGisApp { use rustgis_engine::data::ogc::{TilesStep, resolve_tiles}; for step in std::mem::take(&mut self.ui.service_steps) { match step { - ServiceStep::Tiles { mut resolve, dialog, origin } => { + ServiceStep::Tiles { mut resolve, dialog, origin, opened } => { + if !self.service_read_current(opened, &resolve.doc) { + continue; + } let Some(r) = resolve.slot.lock().unwrap_or_else(|e| e.into_inner()).take() else { - self.ui.service_steps.push(ServiceStep::Tiles { resolve, dialog, origin }); + self.ui.service_steps.push(ServiceStep::Tiles { resolve, dialog, origin, opened }); continue; }; let next = r.ok().and_then(|body| resolve_tiles(&resolve.list, &resolve.doc, &body, resolve.vector)); @@ -4498,7 +4514,7 @@ impl RustGisApp { continue; } } - self.ui.service_steps.push(ServiceStep::Tiles { resolve, dialog, origin }); + self.ui.service_steps.push(ServiceStep::Tiles { resolve, dialog, origin, opened }); continue; } Some(TilesStep::Template(t)) => t, @@ -4531,9 +4547,12 @@ impl RustGisApp { self.report_step(dialog, r); self.map.invalidate(); } - ServiceStep::Coverage { mut params, slot, dialog, origin } => { + ServiceStep::Coverage { mut params, slot, dialog, origin, opened } => { + if !self.service_read_current(opened, params.get("url").and_then(Value::as_str).unwrap_or("")) { + continue; + } let Some(r) = slot.lock().unwrap_or_else(|e| e.into_inner()).take() else { - self.ui.service_steps.push(ServiceStep::Coverage { params, slot, dialog, origin }); + self.ui.service_steps.push(ServiceStep::Coverage { params, slot, dialog, origin, opened }); continue; }; let title = params["name"].as_str().unwrap_or("").to_string(); @@ -5625,7 +5644,7 @@ mod download_service_tests { 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: true, origin: None }); + 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(); @@ -5920,6 +5939,9 @@ mod peer_output_tests { /// 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(); @@ -5932,6 +5954,84 @@ mod peer_output_tests { 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(); + 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); + } + } } } diff --git a/crates/ui-egui/src/serviceui.rs b/crates/ui-egui/src/serviceui.rs index 936f30ca6d..9a1c4a27f7 100644 --- a/crates/ui-egui/src/serviceui.rs +++ b/crates/ui-egui/src/serviceui.rs @@ -139,7 +139,7 @@ pub(crate) fn add_layer_from( } let describe = request_from(app, connection, &src.url, &describe)?; let slot = crate::fetch_into(&describe); - app.ui.service_steps.push(crate::ServiceStep::Coverage { params: p, slot, dialog, origin }); + 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)])); } let mut p = json!({"url": src.url, "layer": name, "text": src.text, "documents": src.documents, "source_url": l.get("source_url")}); @@ -173,7 +173,7 @@ pub(crate) fn add_layer_from( fallback, slot, }; - app.ui.service_steps.push(crate::ServiceStep::Tiles { resolve, dialog, origin }); + 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)])) } None => crate::connectiontree::run_named(app, "service.add", p, origin.as_deref()).map(|_| added(&title)), From 8387479f36d10898c1cba2980bbe9095531dee34 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 22:48:48 -0600 Subject: [PATCH 07/14] fix(test): isolate plugin publication and fetch spy scenarios Run stable plugin-tool scenarios in their own process so parallel registry reloads cannot clear their published schemas. Keep the existing exact-load and in-scenario revocation assertions. Count and hold only each UI fixture's requests, including both signing tokens and assets; reject unrelated background callbacks without treating them as duplicate fixture sends. --- crates/engine/src/tests.rs | 22 ++++++++++++++++++- crates/engine/src/tests/permission_policy.rs | 12 ++++++++++ crates/ui-egui/src/connectiontree.rs | 23 +++++++++++++++----- crates/ui-egui/src/lib.rs | 8 +++++++ 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 5e18830d4d..c21c734875 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -40,7 +40,7 @@ fn world() -> Session { s } -/// Separates a scenario's process-wide account changes and reads from parallel tests. +/// Separates a scenario's process-wide account and registry state from parallel tests. #[cfg(not(target_arch = "wasm32"))] pub(crate) fn isolate_account_test(name: &str) -> bool { if std::env::var("RUSTGIS_ACCOUNT_SCENARIO").as_deref() == Ok(name) { @@ -2420,6 +2420,10 @@ fn imported_tasks_and_notebooks_reject_network_and_reserved_actions() { /// 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_account_test("tests::imported_items_reject_resolved_plugin_tools") { + return; + } let mut s = world(); plugin("eng-tool-containment", &[]); let tool = "plugin:eng-tool-containment/m"; @@ -8953,6 +8957,10 @@ fn plugin_tools_name_their_output_layer_with_edit_alone() { /// 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_account_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(); @@ -9405,6 +9413,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_account_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(); @@ -9536,6 +9548,10 @@ 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_account_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"]); @@ -9586,6 +9602,10 @@ fn plugin_process_child() { #[test] fn process_plugins_reach_the_project_like_modules() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_account_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(); diff --git a/crates/engine/src/tests/permission_policy.rs b/crates/engine/src/tests/permission_policy.rs index cbc00816ee..b663c6ccec 100644 --- a/crates/engine/src/tests/permission_policy.rs +++ b/crates/engine/src/tests/permission_policy.rs @@ -319,6 +319,10 @@ fn permission_policy_metadata_presence_and_unreviewed_default() { /// 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_account_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"] { @@ -1045,6 +1049,10 @@ fn permission_policy_stored_cross_map_clipboard_and_managed_share_targets() { #[cfg(not(target_arch = "wasm32"))] #[test] fn permission_policy_plugin_preparation_keeps_owner_before_remote_send() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_account_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}, @@ -1144,6 +1152,10 @@ fn permission_policy_plugin_preparation_keeps_owner_before_remote_send() { #[cfg(not(target_arch = "wasm32"))] #[test] fn permission_policy_plugin_tools_follow_the_callers_control_while_running() { + #[cfg(not(target_arch = "wasm32"))] + if isolate_account_test("tests::permission_policy::permission_policy_plugin_tools_follow_the_callers_control_while_running") { + return; + } use std::{ sync::mpsc, time::{Duration, Instant}, diff --git a/crates/ui-egui/src/connectiontree.rs b/crates/ui-egui/src/connectiontree.rs index 268cc4ea64..ad210b233c 100644 --- a/crates/ui-egui/src/connectiontree.rs +++ b/crates/ui-egui/src/connectiontree.rs @@ -1145,20 +1145,27 @@ mod tests { 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| { - seen.lock().unwrap().push(url); + 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(); - let href = format!("https://review29{case}.blob.core.windows.net/old-assets/points.csv"); - let token_url = stac::sas_token_url(&href).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().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())); @@ -1186,12 +1193,16 @@ mod tests { } 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().len(), 1, "the old asset must not be requested"); + 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().len(), 2, "a current signing response starts its asset download"); + 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()); } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 1e2a380455..123492c594 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -5677,6 +5677,10 @@ mod download_service_tests { 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())); }); @@ -5963,6 +5967,10 @@ mod peer_output_tests { 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)); }); From 76fabbe165f19e3a25970bfbadc2a7482670dac9 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 23:47:47 -0600 Subject: [PATCH 08/14] fix(engine): report completed exports and template omissions Retain a confirmed written path when late authority checks refuse delivery, without invoking the refused session finalizer. Count offline-area and archived-edit omissions from saved templates. Strengthen the existing guarded-command and model-cycle assertions. --- crates/engine/src/cmd/exports.rs | 19 ++++++- crates/engine/src/cmd/items.rs | 38 +++++++++++--- crates/engine/src/cmd/raster.rs | 1 + crates/engine/src/cmd/share.rs | 4 +- crates/engine/src/tests.rs | 24 +++++++++ crates/engine/src/tests/permission_policy.rs | 55 +++++++++++++++----- 6 files changed, 118 insertions(+), 23 deletions(-) diff --git a/crates/engine/src/cmd/exports.rs b/crates/engine/src/cmd/exports.rs index 89f3d6a14a..6122412017 100644 --- a/crates/engine/src/cmd/exports.rs +++ b/crates/engine/src/cmd/exports.rs @@ -15,6 +15,8 @@ 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, @@ -61,10 +63,18 @@ pub(crate) fn write_here(s: &mut Session, p: &Value, command: &str) -> CmdResult let work = start(s, p)?; written_under(name, &control, work) })?; - s.check_continuation(&owner)?; + 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 { @@ -112,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; @@ -149,6 +160,7 @@ pub(crate) fn drawn( Ok(Wrote { answer, notes: Vec::new(), + written_path, then: Some(then), #[cfg(not(target_arch = "wasm32"))] owner: None, @@ -243,7 +255,9 @@ pub fn export_land(s: &mut Session, started: Started, done: Result (Project, usize) { +fn template(p: &Project) -> (Project, TemplateLosses) { let mut t = p.clone(); t.gp_history.clear(); // Saved label moves name features, and a template has none: the layers', the saved styles' @@ -580,6 +586,14 @@ fn template(p: &Project) -> (Project, usize) { c.moved.clear(); } } + let offline_areas = t.offline_areas.len(); + let archived_edits = t + .maps + .iter() + .chain(t.offline_areas.iter().map(|a| &a.data)) + .flat_map(|m| &m.layers) + .filter_map(|l| l.design.archive.as_ref()) + .fold(0usize, |count, archive| count.saturating_add(archive.len())); t.offline_areas.clear(); for m in &mut t.maps { for l in &mut m.layers { @@ -606,7 +620,7 @@ fn template(p: &Project) -> (Project, usize) { t.features.clear(); t.source = None; } - (t, moves) + (t, TemplateLosses { label_moves: moves, offline_areas, archived_edits }) } /// Packages only completed imports; runtime download reservations cannot be reopened. @@ -1113,14 +1127,22 @@ pub fn item_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Vec } "template" => { s.project.ensure_imports_complete()?; - let (mut t, moves) = template(&s.project); - if moves > 0 { - s.notify(format!("{moves} saved label move(s) left out of the template, which keeps no features"), false); - } + let (mut t, TemplateLosses { label_moves, offline_areas, archived_edits }) = template(&s.project); if let Some(n) = ps(p, "name").map(str::trim).filter(|n| !n.is_empty()) { t.name = n.chars().take(200).collect(); } - (t.name.clone(), durable_bytes(s, t)?) + let name = t.name.clone(); + let bytes = durable_bytes(s, t)?; + if label_moves > 0 { + s.notify(format!("{label_moves} saved label move(s) left out of the template, which keeps no features"), false); + } + if offline_areas > 0 { + s.notify(format!("{offline_areas} offline area(s) left out of the template, which keeps no features"), false); + } + if archived_edits > 0 { + s.notify(format!("{archived_edits} archived edit record(s) left out of the template, which keeps no features"), false); + } + (name, bytes) } "projectPackage" => (s.project.name.clone(), package_project(s)?), "mapPackage" | "mobilePackage" => { diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 876aed00a0..902d7e8b99 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -995,6 +995,7 @@ fn write_geotiff(r: &Raster, bi: usize, cog: bool, path: &str) -> Result 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)?; + 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); } @@ -289,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| { @@ -299,6 +300,7 @@ fn write_layer(l: &rustgis_map::Layer, path: &str, asked: Option, gpkg: Gpk Ok(Wrote { answer, notes: said, + written_path, then, #[cfg(not(target_arch = "wasm32"))] owner: None, diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index c21c734875..155225c4fb 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -25168,7 +25168,27 @@ 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"); + for (kind, count) in [("offline area(s)", offline), ("archived edit record(s)", archived)] { + 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(); @@ -25182,6 +25202,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"))] diff --git a/crates/engine/src/tests/permission_policy.rs b/crates/engine/src/tests/permission_policy.rs index b663c6ccec..5d094a250c 100644 --- a/crates/engine/src/tests/permission_policy.rs +++ b/crates/engine/src/tests/permission_policy.rs @@ -418,9 +418,39 @@ fn permission_policy_export_worker_keeps_its_initiating_owner() { 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(); - assert!(export_land(&mut s, started, wrote).unwrap_err().contains("replaced project")); + 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(); @@ -453,13 +483,13 @@ fn permission_policy_prepared_plugin_keeps_scope_and_pre_handler_boundary() { 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); - assert!( - s.guarded("Transaction", true, |s| { - assert!(s.prepare_command(id, ¶ms).err().unwrap().contains("before handler")); - Err("a later transaction failure".into()) - }) - .is_err() - ); + 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"); } @@ -946,10 +976,11 @@ fn permission_policy_notebook_self_edit_model_outputs_and_intersections() { 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"})); - assert!( - s.run("model.new", &json!({"name":"Cycle","graph":cycle})).is_err() - || matches!(s.review(&scope, &Call::command("model.run", json!({"name":"Cycle"}))), Outcome::Refused(_)) - ); + 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"}}]}), From a0197cc30b720618724b591fa1d77b2dec55551f Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 6 Nov 2026 04:17:29 -0600 Subject: [PATCH 09/14] fix(security): preserve opaque output and bound service choices Count revoked downloads once per drain and announce unchecked address shapes. Preserve newer embedded document bytes before decoding them. Keep bound service tokens under the existing containment check, refuse ambiguous PostgreSQL labels and name omitted URL fragments accurately. --- apps/rustgis-python/python/rustgis/_embed.py | 2 +- crates/data/src/address.rs | 1 + crates/data/src/diagnostics.rs | 2 +- crates/data/src/ogc.rs | 35 +++++-- crates/data/src/source/pgconn.rs | 9 +- crates/engine/src/durable.rs | 8 +- crates/engine/src/policy/downloads.rs | 8 +- crates/engine/src/roles.rs | 16 +++ crates/engine/src/tests/credential_output.rs | 105 +++++++++++++++++++ crates/engine/src/tests/permission_policy.rs | 39 +++++++ crates/ui-egui/tests/autosave.rs | 9 +- 11 files changed, 212 insertions(+), 22 deletions(-) diff --git a/apps/rustgis-python/python/rustgis/_embed.py b/apps/rustgis-python/python/rustgis/_embed.py index bedc5c242c..e448e691cd 100644 --- a/apps/rustgis-python/python/rustgis/_embed.py +++ b/apps/rustgis-python/python/rustgis/_embed.py @@ -209,7 +209,7 @@ def _push(self) -> None: self._hold([(notice, False) for notice in notices]) self._sent_notices = notices if keys > self._sent_keys: - self._hold([(f"{keys} layer address(es) went to the map widget without their keys: those layers stay empty there", False)]) + self._hold([(f"{keys} layer address(es) went to the map widget without their keys or URL fragments; restore missing values if needed", False)]) self._sent_keys = keys except RustGISError as e: self._hold([(f"couldn't show the project in the app: {e}", True)]) diff --git a/crates/data/src/address.rs b/crates/data/src/address.rs index 687f664081..f5dc139fe7 100644 --- a/crates/data/src/address.rs +++ b/crates/data/src/address.rs @@ -254,6 +254,7 @@ pub enum Public { Unchanged, Changed { address: String, + /// Omitted user-info, query pairs and fragment components; not a credential count. removed: u16, }, /// A recognized address whose authority cannot safely be delimited; retain only its prefix. diff --git a/crates/data/src/diagnostics.rs b/crates/data/src/diagnostics.rs index 26a86fbd08..9734088c96 100644 --- a/crates/data/src/diagnostics.rs +++ b/crates/data/src/diagnostics.rs @@ -910,7 +910,7 @@ mod tests { // swallow the rest of the message. for text in [ "The saved password is missing or doesn't match this connection.", - "4 layer address(es) were saved without their keys, tokens or passwords: after RustGIS closes, add those layers again to give them", + "4 layer address(es) were saved without their keys, tokens, passwords or URL fragments; after RustGIS closes, add those layers again to restore missing values", "3 layer(s) read the database of the connection 'db' and need its password: right-click the connection in Data Sources › Connections, or one of the layers, and choose Log In…", "Set Color's step keeps its key parameter. If that is an access key, delete the step (Edit Steps) before sharing the task.", "Exported /data/a[1].png", diff --git a/crates/data/src/ogc.rs b/crates/data/src/ogc.rs index 3ee2d7d9df..4a3e3729a0 100644 --- a/crates/data/src/ogc.rs +++ b/crates/data/src/ogc.rs @@ -2289,7 +2289,9 @@ fn arcgis(url: &str, v: &serde_json::Value) -> Result { | Offer::VectorTiles { template: u, .. } | Offer::Service { url: u } | Offer::Coverage(Coverage { endpoint: u, .. }) => { - if crate::address::Scope::of(url).is_some_and(|scope| scope.covers_below(u)) { + if crate::address::Scope::of(crate::login_address::inner(url)) + .is_some_and(|scope| scope.covers_below(crate::login_address::inner(u))) + { *u = with_query(u, &format!("token={token}")); } else { service.withheld_addresses = service.withheld_addresses.saturating_add(1); @@ -6224,6 +6226,21 @@ mod tests { let s = parse("https://gis.example.org/arcgis/rest/services/P/FeatureServer?token=abc", fs).unwrap(); let Offer::Features { url } = &s.layers[0].offer else { panic!() }; assert!(url.ends_with("&token=abc"), "{url}"); + // A bound service constructs the same requests while retaining only its own login. + for address in [ + "https://gis.example.org/arcgis/rest/services/P/FeatureServer", + "https://gis.example.org/arcgis/rest/services/P/FeatureServer?token=abc", + ] { + let expected = parse(address, fs).unwrap(); + let bound = crate::login_address::bind("0123456789abcdef", "https://gis.example.org", address).unwrap(); + let service = parse(&bound, fs).unwrap(); + let Offer::Features { url } = &service.layers[0].offer else { panic!("features expected") }; + let offered = crate::login_address::parse(url).unwrap().unwrap(); + assert_eq!((offered.binding, offered.origin), ("0123456789abcdef", "https://gis.example.org")); + let Offer::Features { url: expected } = &expected.layers[0].offer else { panic!("features expected") }; + assert_eq!(offered.url, expected, "{address}"); + assert_eq!(service.withheld_addresses, 0, "{address}"); + } } #[test] @@ -6438,16 +6455,16 @@ mod tests { assert_eq!(resolve_tiles(clean, clean, tileset, false).unwrap().withheld_addresses, 0); // Even a suffix appended by RustGIS must pass the raw-path check. The normal style // stays under its service; an advertised '..' style does not inherit its token. + let address = "https://h.invalid/rest/services/a/VectorTileServer?token=RGSYNTH_style_20261103"; + let bound = crate::login_address::bind("0123456789abcdef", "https://h.invalid", address).unwrap(); for (style, withheld) in [("resources/styles", 0), ("../other/styles", 1), ("%2e%2e/other/styles", 1)] { let description = serde_json::json!({"defaultStyles": style}); - let service = parse( - "https://h.invalid/rest/services/a/VectorTileServer?token=RGSYNTH_style_20261103", - description.to_string().as_bytes(), - ) - .unwrap(); - assert_eq!(service.withheld_addresses, withheld); - let Offer::Style { url } = &service.layers[0].offer else { panic!("expected style") }; - assert_eq!(url.contains("RGSYNTH_style_20261103"), withheld == 0); + for address in [address, bound.as_str()] { + let service = parse(address, description.to_string().as_bytes()).unwrap(); + assert_eq!(service.withheld_addresses, withheld, "{address}: {style}"); + let Offer::Style { url } = &service.layers[0].offer else { panic!("expected style") }; + assert_eq!(url.contains("RGSYNTH_style_20261103"), withheld == 0, "{address}: {style}"); + } } } diff --git a/crates/data/src/source/pgconn.rs b/crates/data/src/source/pgconn.rs index fdcc6a2060..09d2210ea2 100644 --- a/crates/data/src/source/pgconn.rs +++ b/crates/data/src/source/pgconn.rs @@ -17,9 +17,10 @@ pub fn public_label(s: &str) -> Option { let (_, raw) = t.split_once("://")?; let authority = raw.split(['/', '?', '#']).next()?; let path = raw.get(authority.len()..)?.split(['?', '#']).next()?; - // A path `@` after userinfo may be a malformed password tail. Refuse ambiguous output - // rather than choosing another host; percent-encode a database name's `@` to keep it. - if authority.contains('@') && path.contains('@') { + // A path `@` after userinfo, or several path `@`s, can be a malformed password tail. + // Refuse ambiguous output rather than choosing another host; percent-encode a + // database name's `@` to keep it. + if (authority.contains('@') && path.contains('@')) || path.matches('@').nth(1).is_some() { return None; } let public = split_secrets(t, SECRET_KEYS).0; @@ -245,6 +246,8 @@ mod tests { "postgresql://user:p@ss/word@host.invalid/db", "postgresql://user@ss/word@host.invalid/db", "postgresql://user@host.invalid/db@tag", + "postgresql://user:pass/word@host.invalid/db@tag", + "postgresql://user:pass/word@host.invalid/db@tag@tail", ] { assert_eq!(super::public_label(ambiguous), None, "{ambiguous}"); } diff --git a/crates/engine/src/durable.rs b/crates/engine/src/durable.rs index 98b4b217d1..2d89a93fdf 100644 --- a/crates/engine/src/durable.rs +++ b/crates/engine/src/durable.rs @@ -42,11 +42,11 @@ impl Report { for (n, text) in [ ( self.addresses, - "layer address(es) were saved without their keys, tokens or passwords: after RustGIS closes, add those layers again to give them", + "layer address(es) were saved without their keys, tokens, passwords or URL fragments; after RustGIS closes, add those layers again to restore missing values", ), - (self.videos, "video address(es) were written without their keys, tokens or passwords"), - (self.steps, "executable step(s) were written without their credentials; supply them again before replay"), - (self.lines, "command line(s) were written without their credentials; supply them again before replay"), + (self.videos, "video address(es) were written without their keys, tokens, passwords or URL fragments"), + (self.steps, "executable step(s) were written without credentials or URL fragments; supply missing values again before replay"), + (self.lines, "command line(s) were written without credentials or URL fragments; supply missing values again before replay"), (self.withheld, "credential-bearing slot(s) could not be read and were withheld"), (self.unchecked, "step(s) contain opaque parameters that may hold credentials and were kept unchecked"), (self.not_retained, "address(es) exceeded the bridge retention limit; their keys will not survive a round trip"), diff --git a/crates/engine/src/policy/downloads.rs b/crates/engine/src/policy/downloads.rs index 7b00d88271..84ede56345 100644 --- a/crates/engine/src/policy/downloads.rs +++ b/crates/engine/src/policy/downloads.rs @@ -111,6 +111,7 @@ impl Session { /// Taking an opaque request sends nothing. Authority is checked again when it is claimed. 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) { let ok = self .policy @@ -124,9 +125,14 @@ impl Session { live.push(request); } else { self.policy.work.queued.remove(&request.key); - self.notify("a deferred URL request was revoked before it started", true); + 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. diff --git a/crates/engine/src/roles.rs b/crates/engine/src/roles.rs index e2a94742d7..9569286fe1 100644 --- a/crates/engine/src/roles.rs +++ b/crates/engine/src/roles.rs @@ -41,6 +41,12 @@ pub enum Document { Model, } +/// Compatibility metadata is read before any embedded document uses this version's types. +#[derive(serde::Deserialize)] +struct DocumentFormat { + format: u64, +} + const WITHHELD: &str = "[parameters withheld]"; const NESTED: &str = "[withheld: nested too deep]"; const DATA: &[&str] = &[ @@ -172,6 +178,12 @@ fn object(id: &str, value: &Value, depth: usize, config_depth: usize, registered if v.is_object() && registered && (role == Some(Role::Configuration) || CONFIG.contains(&k.as_str())) { object("", v, depth, config_depth + 1, true, report) } else { + // String leaves are cleaned below; opaque shapes keep their meaning. + unchecked |= match v { + Value::Object(_) => suspicious(v), + Value::Array(a) => a.iter().any(|v| !v.is_string() && suspicious(v)), + _ => false, + }; address(v, report) } } else if registered && (role == Some(Role::Configuration) || CONFIG.contains(&k.as_str())) && v.is_object() { @@ -331,6 +343,10 @@ impl Durable for VecSteps { /// access. Unchanged documents keep their exact text; only changed typed copies are serialized. fn document(kind: Document, v: &Value, depth: usize, report: &mut Report) -> Option { let Some(text) = v.as_str().filter(|s| s.len() as u64 <= crate::project::MAX_BYTES) else { return invalid(v, report) }; + if serde_json::from_str::(text).is_ok_and(|f| f.format > u64::from(crate::project::FORMAT)) { + report.unchecked = report.unchecked.saturating_add(1); + return None; + } let result = match kind { Document::Project => text_document::(text, depth), Document::Map => { diff --git a/crates/engine/src/tests/credential_output.rs b/crates/engine/src/tests/credential_output.rs index 9bb43993e5..3074a4ca41 100644 --- a/crates/engine/src/tests/credential_output.rs +++ b/crates/engine/src/tests/credential_output.rs @@ -138,6 +138,28 @@ fn durable_empty_query_pairs_preserve_clean_urls_and_report_real_loss() { } } +/// Fragments are deliberately omitted, but their notice must not call them credentials. +#[test] +fn durable_fragment_removal_is_reported_accurately_and_clean_addresses_stay_quiet() { + for fragment in ["", "#section", "#token=RGSYNTH_fragment"] { + let url = format!("https://h.invalid/tiles/{{z}}/{{x}}/{{y}}.png{fragment}"); + let mut project = Project::default(); + project.map_mut().tiles.push(rustgis_map::TileLayer::new("Fragment", rustgis_map::TileKind::Raster, &url)); + let (saved, report) = project.saved_text(None).unwrap(); + let saved: Value = serde_json::from_str(&saved).unwrap(); + assert_eq!(saved["maps"][0]["tiles"][0]["url"], "https://h.invalid/tiles/{z}/{x}/{y}.png"); + assert_eq!(project.map().tiles[0].url, url); + assert_eq!(report.layer_addresses(), u32::from(!fragment.is_empty())); + if fragment.is_empty() { + assert!(report.notices().is_empty()); + } else { + let notices = report.notices(); + assert_eq!(notices.len(), 1); + assert!(notices[0].contains("URL fragments"), "{notices:?}"); + } + } +} + #[test] fn durable_clean_and_opaque_content_keep_their_meaning() { let mut s = marked(); @@ -205,6 +227,89 @@ fn durable_clean_and_opaque_content_keep_their_meaning() { assert!(!text.contains(SECRET) && report.withheld > 0); } +/// Address lists clean string leaves, while opaque values keep their data and say so. +#[test] +fn durable_address_roles_report_opaque_shapes_without_rewriting_data() { + let public = "https://h.invalid/a?layer=1"; + let secret = format!("{public}&token={SECRET}"); + let opaque = json!({"url":{"href":secret}}); + let nested = json!({"urls":[[secret]]}); + let clean_object = json!({"url":{"href":public}}); + for (params, expected, steps, unchecked) in [ + (json!({"url":public}), json!({"url":public}), 0, 0), + (json!({"urls":[secret]}), json!({"urls":[public]}), 1, 0), + (clean_object.clone(), clean_object, 0, 0), + (opaque.clone(), opaque, 0, 1), + (nested.clone(), nested, 0, 1), + ] { + let mut project = Project::default(); + project.tasks.push(Task { + name: "Address shapes".into(), + description: String::new(), + steps: vec![TaskStep { + title: String::new(), + instructions: String::new(), + command: "layer.addUrl".into(), + params: params.clone(), + }], + }); + let before = serde_json::to_value(&project).unwrap(); + let (text, report) = project.saved_text(None).unwrap(); + let written: Value = serde_json::from_str(&text).unwrap(); + assert_eq!(written["tasks"][0]["steps"][0]["params"], expected); + assert_eq!(serde_json::to_value(&project).unwrap(), before); + assert_eq!((report.steps, report.unchecked, report.withheld), (steps, unchecked, 0), "{params}"); + assert_eq!(report.notices().len(), usize::from(steps > 0) + usize::from(unchecked > 0)); + if unchecked > 0 { + assert_eq!(report.notices(), ["1 step(s) contain opaque parameters that may hold credentials and were kept unchecked"]); + } + assert!(!crate::log_params(¶ms).into_log_value().to_string().contains(SECRET), "logs have a separate stricter policy"); + } +} + +/// A newer embedded project must not be rewritten with this writer's schema or version. +#[test] +fn durable_future_documents_keep_their_exact_text_unchecked() { + let mut future = Project::default(); + future.map_mut().tiles.push(rustgis_map::TileLayer::new( + "Future", + rustgis_map::TileKind::Raster, + format!("https://h.invalid/{{z}}/{{x}}/{{y}}.png?token={SECRET}"), + )); + let mut document = serde_json::to_value(&future).unwrap(); + let format = crate::project::FORMAT + 1; + document["format"] = json!(format); + document["futureRequired"] = json!({"meaning":"preserve this unknown field"}); + let source = serde_json::to_string_pretty(&document).unwrap(); + for command in ["project.openText", "map.import"] { + let mut project = Project::default(); + project.tasks.push(Task { + name: "Future document".into(), + description: String::new(), + steps: vec![TaskStep { + title: String::new(), + instructions: String::new(), + command: command.into(), + params: json!({"text":source}), + }], + }); + let before = serde_json::to_value(&project).unwrap(); + let (text, report) = project.saved_text(None).unwrap(); + let written: Value = serde_json::from_str(&text).unwrap(); + assert_eq!(written["tasks"][0]["steps"][0]["params"]["text"], source, "{command}: future document bytes stay exact"); + let embedded: Value = serde_json::from_str(written["tasks"][0]["steps"][0]["params"]["text"].as_str().unwrap()).unwrap(); + assert_eq!(embedded["format"], format); + assert_eq!(embedded["futureRequired"], document["futureRequired"]); + assert_eq!(serde_json::to_value(&project).unwrap(), before); + assert_eq!((report.steps, report.unchecked, report.withheld), (0, 1, 0)); + assert_eq!(report.notices(), ["1 step(s) contain opaque parameters that may hold credentials and were kept unchecked"]); + } + let malformed = source.strip_suffix('}').unwrap(); + let (clean, report) = crate::roles::command("project.openText", &json!({"text":malformed}), 0); + assert_eq!(clean.unwrap()["text"], "[parameters withheld]", "malformed credential-bearing text keeps its existing handling"); + assert_eq!((report.withheld, report.unchecked), (1, 0)); +} + #[test] fn durable_formatter_errors_unwinds_and_apply_failures_restore_whole_fields() { use durable::{Durable, FieldMut, Leaf, Owner, Prepare}; diff --git a/crates/engine/src/tests/permission_policy.rs b/crates/engine/src/tests/permission_policy.rs index 5d094a250c..a640bd49b3 100644 --- a/crates/engine/src/tests/permission_policy.rs +++ b/crates/engine/src/tests/permission_policy.rs @@ -211,6 +211,45 @@ fn permission_policy_task_content_and_trust_follow_undo() { 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() { diff --git a/crates/ui-egui/tests/autosave.rs b/crates/ui-egui/tests/autosave.rs index 6bd1fe912d..30d64ec44f 100644 --- a/crates/ui-egui/tests/autosave.rs +++ b/crates/ui-egui/tests/autosave.rs @@ -140,7 +140,7 @@ fn unsaved_work_is_kept_and_offered_back_after_a_crash() { assert_eq!(app.session.path.as_deref(), Some(std::path::absolute(&project).unwrap().as_path()), "Save writes back to its file"); assert!(app.session.dirty(), "recovered work is unsaved"); assert!( - !app.session.notices.iter().any(|n| n.text.contains("without their credentials")), + !app.session.notices.iter().any(|n| n.text.contains("without credentials or URL fragments")), "a clean recovery reports no credential loss" ); // The copy is this run's now, unchanged, until the work is saved. @@ -179,9 +179,12 @@ fn unsaved_work_is_kept_and_offered_back_after_a_crash() { let app = h.state(); assert!(app.session.map().layer_by_name("Credential recovery").is_some()); assert!(!serde_json::to_string(&app.session.project).unwrap().contains(secret), "the recovered state contains no token"); - let losses: Vec<_> = app.session.notices.iter().filter(|n| n.text.contains("without their credentials")).collect(); + let losses: Vec<_> = app.session.notices.iter().filter(|n| n.text.contains("without credentials or URL fragments")).collect(); assert_eq!(losses.len(), 1, "recovery reports the credential loss once"); - assert_eq!(losses[0].text, "1 executable step(s) were written without their credentials; supply them again before replay"); + assert_eq!( + losses[0].text, + "1 executable step(s) were written without credentials or URL fragments; supply missing values again before replay" + ); assert!(!losses[0].error); } From ca78800046ed3e2c91bdbe550fa8d4b9530a373a Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 6 Nov 2026 13:35:43 -0600 Subject: [PATCH 10/14] fix(security): preserve admission and metadata boundaries --- crates/engine/src/cmd/geolibre.rs | 134 +++++++++++++------ crates/engine/src/cmd/layer.rs | 25 ++-- crates/engine/src/cmd/service.rs | 2 +- crates/engine/src/cmd/tiles.rs | 2 +- crates/engine/src/lib.rs | 4 +- crates/engine/src/policy/downloads.rs | 7 +- crates/engine/src/tests.rs | 46 ++++++- crates/engine/src/tests/permission_policy.rs | 7 +- crates/tiles/src/mbtiles.rs | 61 ++++++++- crates/ui-egui/src/geolibreui.rs | 33 ++++- 10 files changed, 250 insertions(+), 71 deletions(-) diff --git a/crates/engine/src/cmd/geolibre.rs b/crates/engine/src/cmd/geolibre.rs index d16e0463ea..7db50f3572 100644 --- a/crates/engine/src/cmd/geolibre.rs +++ b/crates/engine/src/cmd/geolibre.rs @@ -69,8 +69,9 @@ fn import_project(s: &mut Session, p: &Value) -> CmdResult { 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()) @@ -205,29 +206,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 )); } @@ -341,17 +351,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); - let defer = { - #[cfg(target_arch = "wasm32")] - { - true - } - #[cfg(not(target_arch = "wasm32"))] - { - s.leave_downloads - } - }; - if defer && 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(); @@ -583,19 +584,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] @@ -611,11 +654,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. @@ -641,13 +693,13 @@ mod admission_tests { 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/layer.rs b/crates/engine/src/cmd/layer.rs index 52bc1fb445..c6f6076222 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -3617,16 +3617,19 @@ pub(crate) fn add_cog(s: &mut Session, p: &Value, url: &str, probed: bool) -> Cm // A plugin's command in the app doesn't wait (it runs on the frame), as in the browser. let probed = probed && !s.leave_downloads; // A dated address is framed on the file of the date it shows; why its file couldn't be read. - let (url, probed, mut why) = match r.get("url").and_then(Value::as_str) { - Some(dated) => { - // Its header read now (natively) as an undated address's is before it is added. - #[cfg(not(target_arch = "wasm32"))] - let probe = if s.leave_downloads { Ok(false) } else { crate::render::rustgis_tiles::cog::probe_blocking(dated) }; - #[cfg(target_arch = "wasm32")] - let probe: Result = Ok(false); - (dated.to_string(), probe == Ok(true), probe.err()) - } - None => (url.to_string(), probed, None), + let (url, probed, mut why) = if r.get("dated").and_then(Value::as_bool) == Some(true) { + // The public answer is redacted; metadata uses the installed tile's exact address. + let index = + r.get("index").and_then(Value::as_u64).and_then(|i| usize::try_from(i).ok()).ok_or("added GeoTIFF has no tile index")?; + let dated = s.map().tiles.get(index).ok_or("added GeoTIFF tile is missing")?.url.clone(); + // Its header read now (natively) as an undated address's is before it is added. + #[cfg(not(target_arch = "wasm32"))] + let probe = if s.leave_downloads { Ok(false) } else { crate::render::rustgis_tiles::cog::probe_blocking(&dated) }; + #[cfg(target_arch = "wasm32")] + let probe: Result = Ok(false); + (dated, probe == Ok(true), probe.err()) + } else { + (url.to_string(), probed, None) }; #[cfg(not(target_arch = "wasm32"))] let info = if probed { @@ -3680,7 +3683,7 @@ pub(crate) fn add_cog(s: &mut Session, p: &Value, url: &str, probed: bool) -> Cm crate::sources::redact(&url), crate::sources::redact_text(&e) ), - None => format!("{url} couldn't be read yet, so the map wasn't moved to it"), + None => format!("{} couldn't be read yet, so the map wasn't moved to it", crate::sources::redact(&url)), }); } r["inPlace"] = json!(true); diff --git a/crates/engine/src/cmd/service.rs b/crates/engine/src/cmd/service.rs index f31e447a6f..17fece2112 100644 --- a/crates/engine/src/cmd/service.rs +++ b/crates/engine/src/cmd/service.rs @@ -682,7 +682,7 @@ fn add_discovered(s: &mut Session, p: &Value, url: &str, want: &str, found: &Dis super::time::set(s, &json!({"date": at}))?; // Its address at the step the slider went to. if let Some(t) = added.and_then(|i| s.map().tiles.get(i)) { - r["url"] = json!(t.url); + r["url"] = json!(crate::sources::redact(&t.url)); } } // A slider already on keeps its steps: the service may have no map for some of them. diff --git a/crates/engine/src/cmd/tiles.rs b/crates/engine/src/cmd/tiles.rs index 969864ae9e..d4003e483f 100644 --- a/crates/engine/src/cmd/tiles.rs +++ b/crates/engine/src/cmd/tiles.rs @@ -336,7 +336,7 @@ pub(crate) fn add_with_metadata_wait(s: &mut Session, p: &Value, metadata_wait: let at = if top { 0 } else { m.tiles.iter().position(|o| !o.overlay && o.over.is_none()).unwrap_or(m.tiles.len()) }; let mut out = json!({"name": t.name, "kind": t.kind.code(), "index": at}); if t.time_url.is_some() { - out["url"] = json!(t.url); + out["url"] = json!(crate::sources::redact(&t.url)); out["dated"] = json!(true); } // A file added as data (a catalog's archive, kept where the user put it, excepted) frames the diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 05e41f3c7d..a44b4e233a 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -1417,8 +1417,8 @@ impl Session { self.follow_style_view(); } if id.starts_with(rustgis_plugins::PREFIX) { - self.merge = merge; let op = self.authorize(id, params)?; + self.merge = merge; return self.invoke(op, policy::Txn::Own); } let Some(&spec) = self.registry.get(id) else { return Err(format!("unknown command '{id}'")) }; @@ -1459,8 +1459,8 @@ impl Session { self.notify(e.clone(), true); return Err(e); } - self.merge = merge; let op = self.authorize(id, params)?; + self.merge = merge; let res = self.invoke(op, policy::Txn::Own); if id == "layer.addDownload" && res.is_ok() diff --git a/crates/engine/src/policy/downloads.rs b/crates/engine/src/policy/downloads.rs index 7885a9526a..4af7726896 100644 --- a/crates/engine/src/policy/downloads.rs +++ b/crates/engine/src/policy/downloads.rs @@ -184,6 +184,11 @@ impl Session { 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) @@ -267,7 +272,7 @@ impl Session { asked_now.push((number, asked)); } self.request_only = true; - Ok(json!({"pending":true,"url":p.get("url")})) + 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. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 1af6ae9074..085d860478 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -13576,6 +13576,12 @@ fn wms_layers_with_time() { let mut s = Session::new(Project::default()); let added = s.run("service.add", &json!({"url": url, "layer": "population", "text": WMS, "time": "slider"})).unwrap(); assert!(added["url"].as_str().is_some_and(|u| u.ends_with("&TIME=2025")), "{added}"); + let mut keyed = Session::new(Project::default()); + let capabilities = WMS.replace("https://maps.example.com/wms?", "https://maps.example.com/wms?token=RGSYNTH_service&"); + let public = keyed.run("service.add", &json!({"url":url,"layer":"population","text":capabilities,"time":"slider"})).unwrap(); + let live = &keyed.map().tiles[public["index"].as_u64().unwrap() as usize]; + assert!(live.url.contains("RGSYNTH_service") && live.url.ends_with("TIME=2025")); + assert!(!public["url"].as_str().unwrap().contains("RGSYNTH_service"), "moving to the default time cannot expose the live address"); let year = |y: i32| rustgis_data::time::parse_text(&format!("{y}-01-01")).unwrap(); let slider = s.map().time.clone().unwrap(); assert_eq!((slider.steps(), slider.current), (vec![year(1990), year(2000), year(2025)], year(2025))); @@ -16280,11 +16286,22 @@ fn a_plugins_dated_cog_is_added_without_waiting() { let base = format!("http://{}", server.local_addr().unwrap()); let mut s = Session::new(Project::default()); let start = std::time::Instant::now(); - let r = - s.run_leaving_downloads("layer.addUrl", &json!({"url": format!("{base}/{{date:YYYY}}.tif"), "start": 2020, "end": 2021})).unwrap(); + let r = s + .run_leaving_downloads( + "layer.addUrl", + &json!({"url": format!("{base}/{{date:YYYY}}.tif?token=RGSYNTH_dated_cog"), "start": 2020, "end": 2021}), + ) + .unwrap(); assert!(start.elapsed() < std::time::Duration::from_secs(10), "waited {:?}", start.elapsed()); assert_eq!(r["inPlace"], true, "{r}"); assert!(r["warning"].as_str().is_some_and(|w| w.contains("wasn't moved")), "{r}"); + let tile = &s.map().tiles[r["index"].as_u64().unwrap() as usize]; + assert!( + tile.url.contains(".tif?token=RGSYNTH_dated_cog") && !tile.url.contains("{date"), + "the live tile keeps the effective private address" + ); + let clean = ["warning", "url"].map(|key| r[key].as_str().is_some_and(|text| !text.contains("RGSYNTH_dated_cog"))); + assert_eq!(clean, [true, true], "both public warning and URL must omit credentials"); drop(server); } @@ -20961,6 +20978,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] @@ -24542,6 +24580,10 @@ fn time_slider_fills_dated_tile_addresses_and_exports_its_steps() { ) .unwrap(); assert_eq!(r["url"], "https://example.com/2023-08-01/{z}/{x}/{y}.png"); + let mut keyed_session = timed_events(); + let keyed = keyed_session.run("map.addTiles", &json!({"url":"https://example.com/{date:YYYY-MM-DD}/{z}/{x}/{y}.png?token=RGSYNTH_dated_tiles","start":"2023-08-01","end":"2023-08-31"})).unwrap(); + assert!(!keyed["url"].as_str().unwrap().contains("RGSYNTH_dated_tiles")); + assert!(keyed_session.map().tiles[keyed["index"].as_u64().unwrap() as usize].url.contains("RGSYNTH_dated_tiles")); let i = s.run("time.info", &Value::Null).unwrap(); assert_eq!((i["unit"].as_str(), i["steps"].as_u64()), (Some("day"), Some(31)), "{i}"); // Only tile kinds that fill dates take a dated address. diff --git a/crates/engine/src/tests/permission_policy.rs b/crates/engine/src/tests/permission_policy.rs index 84687c545a..d569d27847 100644 --- a/crates/engine/src/tests/permission_policy.rs +++ b/crates/engine/src/tests/permission_policy.rs @@ -260,13 +260,16 @@ fn permission_policy_deferred_url_claim_keeps_owner_and_generation() { } let (scope, live) = assistant(3); let (foreign, _) = assistant(4); - let call = Call::command("layer.addUrl", json!({"url":"https://example.invalid/data.geojson"})); + 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); - s.run_permitted(scope.clone(), p.allow(), &call).unwrap(); + 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 id = s.download_started_for(&q).unwrap(); assert!(s.download_started_for(&q).is_err()); let still = s.download_still(id).unwrap(); diff --git a/crates/tiles/src/mbtiles.rs b/crates/tiles/src/mbtiles.rs index 44ef2fa507..e6a77bfa3c 100644 --- a/crates/tiles/src/mbtiles.rs +++ b/crates/tiles/src/mbtiles.rs @@ -129,8 +129,11 @@ 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 || crate::cache::check_still(&crate::cache::current_still()).is_err() { + if EPOCH.load(Ordering::SeqCst) != epoch { return; } if all.len() >= 64 { @@ -164,6 +167,13 @@ 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. @@ -182,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); @@ -219,7 +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); } @@ -286,6 +296,49 @@ 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] diff --git a/crates/ui-egui/src/geolibreui.rs b/crates/ui-egui/src/geolibreui.rs index 32c5b6f943..0b77da5e0b 100644 --- a/crates/ui-egui/src/geolibreui.rs +++ b/crates/ui-egui/src/geolibreui.rs @@ -335,6 +335,8 @@ mod tests { fn geolibre_zarr_start_failure_removes_reservation_through_app_frame() { 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 { @@ -358,7 +360,6 @@ mod tests { assert!(app.session.download_is_pending(unrelated), "an unclaimed failure must leave other downloads alone"); app.session.download_failed(unrelated); } - let (target, id) = deferred_zarr(&mut app); 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 @@ -432,15 +433,35 @@ mod tests { } else { None }; + let (target, id) = deferred_zarr(&mut app); let mut claims = Vec::new(); if case == "capacity" { - for n in 0..64 { - app.session.request_url(&json!({"url":format!("https://example.invalid/held-{n}.csv")})).unwrap(); - let request = app.session.take_url_requests().pop().unwrap(); - claims.push(app.session.download_started_for(&request).unwrap()); + 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 (target, id) = deferred_zarr(&mut app); 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. From 1ded5bdde7797983b2851eb73c237660e3c576f6 Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 6 Nov 2026 19:39:21 -0600 Subject: [PATCH 11/14] fix(security): preserve exact sources and counted output losses --- apps/rustgis-python/python/rustgis/_embed.py | 2 +- apps/rustgis-python/tests/test_widget.py | 8 + apps/rustgis-web/src/web.rs | 2 +- crates/assistant/src/audit.rs | 9 +- crates/assistant/src/tests.rs | 7 + crates/assistant/src/tools.rs | 112 ++++++++- crates/data/src/address.rs | 20 +- crates/data/src/ogc.rs | 31 ++- crates/engine/src/cmd/history.rs | 3 +- crates/engine/src/cmd/notebook.rs | 4 +- crates/engine/src/cmd/service.rs | 93 +++++--- crates/engine/src/cmd/service_group.rs | 43 ++-- crates/engine/src/durable.rs | 66 ++++- crates/engine/src/lib.rs | 10 +- crates/engine/src/peer.rs | 238 ++++++++++++++++--- crates/engine/src/project.rs | 47 ++-- crates/engine/src/project_log.rs | 19 ++ crates/engine/src/roles.rs | 16 +- crates/engine/src/tests/credential_output.rs | 148 +++++++++++- crates/engine/src/tests/service_logins.rs | 54 +++++ crates/map/src/source.rs | 2 +- crates/ui-egui/src/lib.rs | 62 ++++- crates/ui-egui/src/serviceui.rs | 31 ++- docs/python.md | 8 +- docs/user-guide/workspace.md | 6 +- 25 files changed, 881 insertions(+), 160 deletions(-) diff --git a/apps/rustgis-python/python/rustgis/_embed.py b/apps/rustgis-python/python/rustgis/_embed.py index c3072d757b..d2c04867de 100644 --- a/apps/rustgis-python/python/rustgis/_embed.py +++ b/apps/rustgis-python/python/rustgis/_embed.py @@ -208,7 +208,7 @@ def _push(self) -> None: if text != self._project or notices != self._sent_notices: self._hold([(notice, False) for notice in notices]) self._sent_notices = notices - if keys > self._sent_keys: + if keys and (text != self._project or keys > self._sent_keys): self._hold([(f"{keys} layer address(es) went to the map widget without their keys, tokens, passwords or URL fragments; restore missing values if needed", False)]) self._sent_keys = keys except RustGISError as e: diff --git a/apps/rustgis-python/tests/test_widget.py b/apps/rustgis-python/tests/test_widget.py index 66d93945a0..f8c608a884 100644 --- a/apps/rustgis-python/tests/test_widget.py +++ b/apps/rustgis-python/tests/test_widget.py @@ -120,6 +120,14 @@ def test_widget_exchange_never_sends_keys_and_checks_the_current_slot(): m.run("info.project") assert m._project == before assert m.notices() == [] + # A different protected source still has one omitted address and needs a fresh warning. + project = json.loads(m._project) + project["maps"][0]["layers"][0]["dataset"]["source"] = f"https://h.invalid/b?token={secret}" + s.run("project.openText", text=json.dumps(project)) + m.run("info.project") + assert any("without their keys" in notice for notice, _ in m.notices()) + m.run("info.project") + assert m.notices() == [] incoming = json.loads(m._project) incoming["maps"][0]["layers"][0]["name"] = "Styled by app" from_app(m, json.dumps(incoming)) diff --git a/apps/rustgis-web/src/web.rs b/apps/rustgis-web/src/web.rs index d920d6b317..867a6259c9 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -418,7 +418,7 @@ impl Embed { app.session.notify(notice, false); } } - Err(e) => log::error!("couldn't send the project: {e}"), + Err(e) => app.session.notify(e, true), }, (false, Some(wait)) => ctx.request_repaint_after(wait), (false, None) => {} diff --git a/crates/assistant/src/audit.rs b/crates/assistant/src/audit.rs index d22b2a38ae..9b93da7d56 100644 --- a/crates/assistant/src/audit.rs +++ b/crates/assistant/src/audit.rs @@ -234,7 +234,7 @@ fn redact_script(script: &str) -> String { .lines() .take(rustgis_engine::cmd::workflows::MAX_SCRIPT_LINES) .map(|line| match rustgis_engine::script_line(line) { - None => line.trim().to_string(), + None => redact_string(line.trim()), Some(Ok((id, params))) => { let id = redact_string(&id); if params.is_null() { id } else { format!("{id} {}", rustgis_engine::log_params(¶ms).into_log_value()) } @@ -329,6 +329,13 @@ mod tests { let logged = redact_script(script); assert!(logged.starts_with("map.zoomIn {}\nlayer.addUrl [parameters not JSON; withheld]\nmap.zoomOut {}")); assert!(!logged.contains("RGSYNTH_")); + let comments = "# clean comment\n# https://h.invalid/a?token=RGSYNTH_comment_20261106"; + let args = normalized_args("run", &json!({"script": comments})); + assert!(!args.to_string().contains("RGSYNTH_comment")); + assert!(!redact_script(comments).contains("RGSYNTH_comment")); + assert!(redact_script(comments).starts_with("# clean comment\n")); + let nested = json!({"key":"abc", "source":{"url":"https://h.invalid/data"}}); + assert_eq!(normalized_args("run_command", &json!({"command":"plugin.unknown", "params":nested}))["params"]["key"], ""); let document = json!({"type": "FeatureCollection", "features": [], "padding": "x".repeat(250), "token": "RGSYNTH_data_20261103"}).to_string(); let arguments = json!({"command": "layer.addDataText", "params": {"text": document}}); diff --git a/crates/assistant/src/tests.rs b/crates/assistant/src/tests.rs index 8ed0af6f81..e04703a63f 100644 --- a/crates/assistant/src/tests.rs +++ b/crates/assistant/src/tests.rs @@ -4345,4 +4345,11 @@ fn peer_replacement_cancels_approvals_and_stale_replies() { assert!(agent.transcript.len() >= transcript); let _ = agent.receive(generation, ok(say("stale")), &mut s); assert!(!agent.busy() && agent.pending(&s).is_none()); + assert!(matches!(agent.ask("start a new request", &s), Step::Send(_))); + let current = agent.generation(); + let transcript = agent.transcript.len(); + let _ = agent.receive(generation, ok(say("stale while waiting")), &mut s); + assert_eq!(agent.generation(), current); + assert!(agent.waiting() && agent.pending(&s).is_none()); + assert_eq!(agent.transcript.len(), transcript); } diff --git a/crates/assistant/src/tools.rs b/crates/assistant/src/tools.rs index 359adae7b9..15a20386d6 100644 --- a/crates/assistant/src/tools.rs +++ b/crates/assistant/src/tools.rs @@ -279,7 +279,7 @@ pub fn run_with_budget(s: &mut Session, call: &ToolCall, tokens: usize) -> Resul } let (before, mark) = (layer_snap(s), s.notice_mark()); let r = s.run(&id, ¶ms_arg(a)?).map_err(|e| public_text(error_with_notices(e, s, mark)))?; - with_notices(with_new_layers(r, s, &before), s, mark) + return Ok(command_result(s, r, &before, mark, tokens)); } "run_tool" => { let tool = str_arg(a, "tool").ok_or("tool is required")?; @@ -287,7 +287,7 @@ pub fn run_with_budget(s: &mut Session, call: &ToolCall, tokens: usize) -> Resul let r = s .run("gp.run", &json!({"tool": tool, "params": params_arg(a)?})) .map_err(|e| public_text(error_with_notices(e, s, mark)))?; - with_notices(with_new_layers(r, s, &before), s, mark) + return Ok(command_result(s, r, &before, mark, tokens)); } "read_features" => read_features(s, a)?, // MCP's `run` (not offered to the in-app assistant): a script in one call. @@ -312,7 +312,13 @@ pub fn run_with_budget(s: &mut Session, call: &ToolCall, tokens: usize) -> Resul "test_panic" => panic!("a test tool panicked"), other => return Err(format!("unknown tool '{other}'")), }; - Ok(public_result(s, v, tokens)) + if call.name == "read_features" { + Ok(public_envelope(s, v, tokens)) + } else { + let mut withheld = Withheld::default(); + let v = public_metadata(v, 0, &mut withheld); + Ok(finish_result(s, v, tokens, withheld)) + } } /// How deep a result is searched for runtime login addresses; what is deeper is withheld. @@ -326,9 +332,51 @@ fn public_text(text: String) -> String { } /// Publishes a completed result, reporting withheld subtrees before enforcing the response budget. +#[cfg(test)] fn public_result(s: &mut Session, value: Value, tokens: usize) -> String { let mut withheld = Withheld::default(); - let mut value = public_addresses(value, 0, &mut withheld); + let value = public_addresses(value, 0, &mut withheld); + finish_result(s, value, tokens, withheld) +} + +/// These fields belong to the adapter's response schema; only their values contain data. +fn public_envelope(s: &mut Session, value: Value, tokens: usize) -> String { + let mut withheld = Withheld::default(); + let value = match value { + Value::Object(o) => Value::Object(o.into_iter().map(|(key, value)| (key, public_addresses(value, 1, &mut withheld))).collect()), + value => public_addresses(value, 0, &mut withheld), + }; + finish_result(s, value, tokens, withheld) +} + +/// Generated search, tool and project metadata has a fixed schema at every level. Its +/// strings may hold private data, but its keys cannot be login values. Row/property maps +/// and arbitrary command results use `public_addresses` instead. +fn public_metadata(value: Value, depth: usize, withheld: &mut Withheld) -> Value { + match value { + Value::String(text) => Value::String(public_text(text)), + Value::Array(a) if depth < MAX_DEPTH => Value::Array(a.into_iter().map(|v| public_metadata(v, depth + 1, withheld)).collect()), + Value::Object(o) if depth < MAX_DEPTH => { + Value::Object(o.into_iter().map(|(key, v)| (key, public_metadata(v, depth + 1, withheld))).collect()) + } + Value::Array(_) | Value::Object(_) => { + withheld.depth = withheld.depth.saturating_add(1); + json!("[too deep]") + } + other => other, + } +} + +/// Cleans the arbitrary command payload before adding adapter-owned notices/layer metadata. +fn command_result(s: &mut Session, value: Value, before: &[(String, Vec)], mark: u64, tokens: usize) -> String { + let mut withheld = Withheld::default(); + let value = public_addresses(value, 0, &mut withheld); + let value = with_notices(with_new_layers(value, s, before), s, mark); + finish_result(s, value, tokens, withheld) +} + +/// Publishes counted projection losses without sending the completed envelope through a second redactor. +fn finish_result(s: &mut Session, mut value: Value, tokens: usize, withheld: Withheld) -> String { if withheld.depth > 0 || withheld.keys > 0 { let mark = s.notice_mark(); if withheld.depth > 0 { @@ -488,7 +536,7 @@ fn with_new_layers(r: Value, s: &Session, before: &[(String, Vec)]) -> Valu let same = before.iter().find(|(name, _)| name.eq_ignore_ascii_case(&m.name)).or_else(|| if renamed { before.get(active) } else { None }); let had = same.map_or(&[][..], |(_, ids)| ids.as_slice()); - let added: Vec = m.layers.iter().filter(|l| !had.contains(&l.id)).map(|l| l.name.clone()).collect(); + let added: Vec = m.layers.iter().filter(|l| !had.contains(&l.id)).map(|l| public_text(l.name.clone())).collect(); if added.is_empty() { return if r.is_null() { json!({"ok": true}) } else { r }; } @@ -519,7 +567,7 @@ fn run_script(s: &mut Session, a: &Value, tokens: usize) -> Result { // Nothing was kept, so no line ran and no layer was added. @@ -528,7 +576,7 @@ fn run_script(s: &mut Session, a: &Value, tokens: usize) -> Result = notices.iter().filter(|t| **t != e && **t != told).collect(); let out = json!({"success": false, "result": null, "lines": 0, "layersAdded": [], "notices": said, "error": clip(&e, 8000), "duration_s": secs}); - Err(public_result(s, out, tokens)) + Err(public_envelope(s, out, tokens)) } } } @@ -536,7 +584,7 @@ fn run_script(s: &mut Session, a: &Value, tokens: usize) -> Result Value { - let said: Vec<&str> = s.notices_since(mark).iter().map(|n| n.text.as_str()).collect(); + let said: Vec = s.notices_since(mark).iter().map(|n| public_text(n.text.clone())).collect(); if said.is_empty() { return r; } @@ -901,6 +949,54 @@ mod tests { assert!(listed.contains(&plain), "the model still learns the layer's address: {listed}"); } + /// The known response schema survives a secret that happens to name one of its fields. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn run_envelope_keys_are_not_data_derived_login_values() { + if std::env::var_os("RUSTGIS_TEST_RUN_ENVELOPE").is_none() { + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "tools::tests::run_envelope_keys_are_not_data_derived_login_values"]) + .env("RUSTGIS_TEST_RUN_ENVELOPE", "1") + .output() + .unwrap(); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!( + output.status.success() && stdout.contains("1 passed; 0 failed"), + "{stdout}\n{}", + String::from_utf8_lossy(&output.stderr) + ); + return; + } + let mut s = Session::new(rustgis_engine::Project::default()); + let id = s + .run("connection.new", &json!({"kind":"wms", "address":"https://envelope.invalid/wms", "auth":"basic", "user":"reader"})) + .unwrap()["id"] + .clone(); + s.run("connection.login", &json!({"id":id, "password":"success"})).unwrap(); + 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 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()); + } + let data = public_addresses(json!({"success":"private field"}), 0, &mut Withheld::default()); + assert_eq!(data["[redacted]"], "private field", "the same spelling in dynamic data is still scrubbed"); + s.run("connection.login", &json!({"id":id, "password":"id"})).unwrap(); + for (name, args, array) in + [("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 out: Value = serde_json::from_str(&text).unwrap(); + let entries = out.get(array).and_then(Value::as_array).unwrap(); + assert!(!entries.is_empty(), "{text}"); + assert!(entries.iter().all(|entry| entry.get("id").is_some()), "fixed nested schema keys survive: {text}"); + } + let data = public_addresses(json!({"id":"private field"}), 0, &mut Withheld::default()); + assert_eq!(data["[redacted]"], "private field"); + } + #[test] fn wrappers_after_a_prefix_are_made_plain_too() { let origin = "https://assistant-login.example"; diff --git a/crates/data/src/address.rs b/crates/data/src/address.rs index 84ba823d7d..0eba9a766c 100644 --- a/crates/data/src/address.rs +++ b/crates/data/src/address.rs @@ -104,7 +104,7 @@ const SAS_KEYS: &[&str] = /// Exact and containment rules over canonical names, plus query-wide context. fn credential(k: &str, query: &QueryNames) -> bool { - matches!(k, "key" | "pwd" | "sig" | "signature" | "sas" | "authorization" | "proxyauthorization" | "cookie" | "setcookie" | "bearer") + matches!(k, "key" | "appid" | "subscriptionkey" | "clientkey" | "saskey" | "pwd" | "sig" | "signature" | "sas" | "authorization" | "proxyauthorization" | "cookie" | "setcookie" | "bearer") // These public refusal and tracing headers share the signing family's prefix. || (k.starts_with("xamz") && !matches!(k, "xamznrequestid" | "xamznwafaction" | "xamzcfid")) || k.starts_with("xgoog") @@ -527,6 +527,7 @@ mod tests { #[test] fn canonical_names_remove_encoded_and_unresolved_pairs_without_touching_values() { let over = "a".repeat(MAX_NAME_BYTES + 1); + let mut unredacted = Vec::new(); for key in [ "%74oken", "%2574oken", @@ -544,16 +545,31 @@ mod tests { "sig=M&%73v", "\\u0074oken", "accessKeyId", + "appid", + "APP_ID", + "%2561ppid", + "subscription-key", + "Subscription_Key", + "%73ubscription%2Dkey", + "client-key", + "clientKey", + "sas-key", + "SAS_Key", "%2525252574oken", over.as_str(), ] { let address = format!("https://h.invalid/a?{key}=RGSYNTH_name_20261103&layer=a%20b&f=json&%zz=1"); - let Public::Changed { address: public, removed, .. } = public_web_address(&address) else { panic!("{key}") }; + let Public::Changed { address: public, removed, .. } = public_web_address(&address) else { + unredacted.push(key.to_string()); + continue; + }; assert!(!public.contains("RGSYNTH_name_20261103"), "{key}: {public}"); assert!(public.ends_with("layer=a%20b&f=json&%zz=1"), "{public}"); assert!(removed > 0); } + assert!(unredacted.is_empty(), "credential names left unchanged: {unredacted:?}"); for query in [ + "client_id=public&application_id=public&map=1", "layer=a%20b&f=json&%zz=1", "layer=token%3DM", "code=EPSG:4326", diff --git a/crates/data/src/ogc.rs b/crates/data/src/ogc.rs index 4a3e3729a0..fa7684594d 100644 --- a/crates/data/src/ogc.rs +++ b/crates/data/src/ogc.rs @@ -533,7 +533,7 @@ fn is_data_link(link: &serde_json::Value) -> bool { } /// Chooses JSON before an untyped data link, without guessing an endpoint or copying credentials. -fn collections_link(address: &str, links: &[serde_json::Value]) -> Option { +fn collections_link(address: &str, links: &[serde_json::Value]) -> Option<(String, bool)> { let base = url::Url::parse(crate::login_address::inner(address)).ok()?; links .iter() @@ -555,7 +555,12 @@ fn collections_link(address: &str, links: &[serde_json::Value]) -> Option Result { let title = v.get("title").and_then(|t| t.as_str()).unwrap_or_default().to_string(); let mut service = Service { kind: "OGC API Tiles".into(), title: title.clone(), ..Service::default() }; let links = v.get("links").and_then(|l| l.as_array()).cloned().unwrap_or_default(); - service.collections_url = collections_link(url, &links); + if let Some((next, withheld)) = collections_link(url, &links) { + service.collections_url = Some(next); + service.withheld_addresses = u32::from(withheld); + } let name = if title.is_empty() { "tiles".to_string() } else { title }; // The dataset's layers aren't listed here, so its vector tiles can't be styled: map tiles only. if let Some((t, list, withheld)) = tileset(url, &links, TileRel::Map) { @@ -3992,11 +4000,12 @@ mod tests { let address = crate::login_address::bind(binding, origin, "https://maps.example.org/api?key=a%2Fb").unwrap(); for href in ["/collections", "https://MAPS.example.org:443/collections"] { let links = serde_json::json!([{"rel":"data","type":"application/json","href":href}]); - let next = collections_link(&address, links.as_array().unwrap()).unwrap(); + let (next, withheld) = collections_link(&address, links.as_array().unwrap()).unwrap(); assert_eq!(crate::login_address::parse(&next).unwrap().unwrap().binding, binding); + assert!(withheld, "the advertised destination omits the original key"); } let links = serde_json::json!([{"rel":"data","type":"application/json","href":"https://other.example.org/collections"}]); - assert_eq!(collections_link(&address, links.as_array().unwrap()).unwrap(), "https://other.example.org/collections"); + assert_eq!(collections_link(&address, links.as_array().unwrap()).unwrap(), ("https://other.example.org/collections".into(), true)); } #[test] @@ -6301,6 +6310,18 @@ mod tests { assert_eq!(chosen.collections_url.as_deref(), Some("https://example.org/json")); } + #[test] + fn collections_links_count_only_settings_the_selected_link_omits() { + let body = br#"{"links":[{"rel":"data","type":"application/json","href":"/catalog?f=json"}]}"#; + for (from, count) in + [("https://collections.invalid/root?f=json", 0), ("https://collections.invalid/root?f=json&token=RGSYNTH_link", 1)] + { + let service = parse(from, body).unwrap(); + assert_eq!(service.collections_url.as_deref(), Some("https://collections.invalid/catalog?f=json")); + assert_eq!(service.withheld_addresses, count); + } + } + #[test] fn ogc_json_format_is_idempotent() { let url = capabilities_url("https://example.org/api?f=html&key=a%2Fb&f=json#fragment", Some("ogcapi")); diff --git a/crates/engine/src/cmd/history.rs b/crates/engine/src/cmd/history.rs index 5c4584136c..f1eed6a751 100644 --- a/crates/engine/src/cmd/history.rs +++ b/crates/engine/src/cmd/history.rs @@ -138,8 +138,7 @@ pub(crate) fn redacted(v: &Value) -> Value { Value::String(t) => Value::String(redacted_text(t)), Value::Array(a) => Value::Array(a.iter().map(redacted).collect()), Value::Object(o) => { - let web = |v: &Value| v.as_str().is_some_and(|t| t.trim_start().get(..4).is_some_and(|p| p.eq_ignore_ascii_case("http"))); - let beside_address = |k: &str| o.iter().any(|(sibling, v)| sibling != k && web(v)); + let beside_address = |k: &str| o.iter().any(|(sibling, v)| sibling != k && crate::project_log::address_context(sibling, v, 0)); let secret = |k: &str, v: &Value| { secret_key(k) && !matches!(v, Value::Bool(_) | Value::Null) diff --git a/crates/engine/src/cmd/notebook.rs b/crates/engine/src/cmd/notebook.rs index a60c46e7f6..0f3bbfa624 100644 --- a/crates/engine/src/cmd/notebook.rs +++ b/crates/engine/src/cmd/notebook.rs @@ -181,7 +181,7 @@ pub fn commands() -> Vec { nb.cells.insert(at, Cell::new(kind, ps(p, "source").unwrap_or(""))); Ok(json!({"cell": at, "cells": nb.cells.len()})) }) - .roles(&[]) + .roles(&[crate::ParamRole { key: "source", role: crate::Role::Opaque }]) .when(need_notebook) .params(r#"{"notebook"?, "kind"?: "code"|"markdown", "source"?, "at"?: index}"#), C::new("notebook.editCell", "Edit Cell", "Notebook", |s, p| { @@ -191,7 +191,7 @@ pub fn commands() -> Vec { c.source = source.chars().take(MAX_SOURCE).collect(); Ok(json!({"cell": k})) }) - .roles(&[]) + .roles(&[crate::ParamRole { key: "source", role: crate::Role::Opaque }]) .when(need_notebook) .params(r#"{"notebook"?, "cell": index, "source": text}"#), C::new("notebook.removeCell", "Delete Cell", "Notebook", |s, p| { diff --git a/crates/engine/src/cmd/service.rs b/crates/engine/src/cmd/service.rs index 91ad36d15c..4d7e8b6179 100644 --- a/crates/engine/src/cmd/service.rs +++ b/crates/engine/src/cmd/service.rs @@ -103,8 +103,9 @@ struct Discovery { } impl Discovery { - fn failed(mut self, error: String) -> Result { + fn failed(mut self, s: &mut Session, error: String) -> Result { if self.service.layers.is_empty() { + notify_withheld(s, self.service.withheld_addresses); Err(error) } else { self.error = Some(error.clone()); @@ -117,7 +118,7 @@ impl Discovery { const MAX_SERVICE_DOCUMENTS: usize = 4; -fn capabilities(p: &Value) -> Result { +fn capabilities(s: &mut Session, p: &Value) -> Result { let original = url_of(p)?; let mut address = crate::render::rustgis_tiles::logins::inherit(original, &rustgis_data::ogc::capabilities_url(original, ps(p, "type"))); @@ -140,13 +141,13 @@ fn capabilities(p: &Value) -> Result { loop { crossed_origin |= crosses_origin(&parent, &address); if let Err(e) = crate::render::rustgis_tiles::address::check(&address, Some(&parent)) { - return result.failed(e); + return result.failed(s, e); } if seen.contains(&address) { - return result.failed("the service's collections links form a loop".into()); + return result.failed(s, "the service's collections links form a loop".into()); } if seen.len() >= MAX_SERVICE_DOCUMENTS { - return result.failed("the service's collections discovery exceeds four documents".into()); + return result.failed(s, "the service's collections discovery exceeds four documents".into()); } let parsed = if let Some(root) = supplied { let text = if seen.is_empty() { @@ -172,7 +173,7 @@ fn capabilities(p: &Value) -> Result { result.excluded_layers |= vetted.layers.len() != before; vetted } - Err(e) => return result.failed(e), + Err(e) => return result.failed(s, e), }; seen.insert(address.clone()); if result.service.kind.is_empty() { @@ -258,9 +259,14 @@ pub(crate) fn mark_service_source(s: &mut Session, r: &Value, url: &str) { .filter_map(Value::as_u64) .filter_map(|i| u32::try_from(i).ok()) .collect(); + let anonymous = s.web_logins.is_anonymous(); for id in ids { if let Some(l) = s.map_mut().layer_mut(id) { l.dataset.source = Some(url.to_string()); + if anonymous { + l.anonymous = true; + l.connection.clear(); + } } } } @@ -517,12 +523,12 @@ pub(crate) fn layers(s: &mut Session, p: &Value) -> CmdResult { let p = owned.as_ref().unwrap_or(p); let bound = crate::service_logins::request_params(s, p)?; let p = bound.as_ref(); - let found = capabilities(p)?; + let found = capabilities(s, p)?; notify_withheld(s, found.service.withheld_addresses); let mut out = service_json(&found.service); if let Some(layers) = out.get_mut("layers").and_then(Value::as_array_mut) { for (layer, source) in layers.iter_mut().zip(found.sources) { - layer["source_url"] = json!(source.address); + layer_source(layer, source); } } if let Some(next) = found.next { @@ -531,6 +537,23 @@ pub(crate) fn layers(s: &mut Session, p: &Value) -> CmdResult { Ok(out) } +/// Publishes the selected document and its already-resolved feature login provenance. +fn layer_source(layer: &mut Value, source: CapabilitySource) { + let foreign_features = layer.get("offer").is_some_and(|offer| { + offer.get("type").and_then(Value::as_str) == Some("features") + && ps(offer, "url").is_some_and(|url| crosses_origin(&source.address, url)) + }); + layer["source_url"] = json!(source.address); + if source.crossed_origin || foreign_features { + layer["anonymous"] = json!(true); + if let Some(url) = layer.get_mut("offer").and_then(|o| o.get_mut("url")) + && let Some(value) = url.as_str() + { + *url = json!(crate::render::rustgis_tiles::logins::inner(value)); + } + } +} + /// Counts destinations, not pairs; clean discovery emits no credential-forwarding notice. fn notify_withheld(s: &mut Session, count: u32) { s.notify_loss(rustgis_data::ogc::QUERY_WITHHELD_NOTICE, count); @@ -545,7 +568,7 @@ pub(crate) fn add(s: &mut Session, p: &Value) -> CmdResult { let p = bound.as_ref(); let url = url_of(p)?.to_string(); let want = ps(p, "layer").map(str::trim).filter(|l| !l.is_empty()).ok_or("layer is required")?; - let found = capabilities(p)?; + let found = capabilities(s, p)?; let mut withheld = found.service.withheld_addresses; let result = add_discovered(s, p, &url, want, &found, &mut withheld); notify_withheld(s, withheld); @@ -778,29 +801,35 @@ fn add_discovered(s: &mut Session, p: &Value, url: &str, want: &str, found: &Dis } let max = crate::render::rustgis_tiles::cache::MAX_DOWNLOAD; let from = asked.as_deref().unwrap_or(items); - // An area request keeps the view until the requested box is applied below. - let keep_view = asked.is_some().then_some(false); - let mut r = match download_layer(s, &name, from, max, true).map_err(|e| slow_whole_layer(e, given))? { - Downloaded::Bytes(bytes) => { - let bytes = features_answer(s, &name, bytes, layer.bbox); - super::raster::add_named_bytes(s, &json!({"name": name}), format!("{name}.geojson"), bytes, keep_view)? + let crossed = capability_crossed || crosses_origin(source, from); + let from = if crossed { crate::render::rustgis_tiles::logins::inner(from) } else { from }; + let anonymous = crossed || s.web_logins.is_anonymous(); + crate::service_logins::anonymously(s, anonymous, |s| { + // An area request keeps the view until the requested box is applied below. + let keep_view = asked.is_some().then_some(false); + let mut r = match download_layer(s, &name, from, max, true).map_err(|e| slow_whole_layer(e, given))? { + Downloaded::Bytes(bytes) => { + let bytes = features_answer(s, &name, bytes, layer.bbox); + super::raster::add_named_bytes(s, &json!({"name": name}), format!("{name}.geojson"), bytes, keep_view)? + } + // Read in protocol buffer pages: their positions as the service's system + // places them, so there are no axes to check. + Downloaded::Read(read) => super::layer::add_read(s, &json!({"name": name}), *read, keep_view)?, + }; + mark_service_source(s, &r, from); + // Over an area, the features crossing it: the view stays, or goes to a box given + // (the whole layer's bounds would show a country to fill one state). + match (asked.is_some(), area, p.get("bbox")) { + (true, Some([w, so, e, n]), Some(Value::Array(_))) => s.zoom_to(&rustgis_geom::Extent::new(w, so, e, n)), + (true, ..) => {} + _ => zoom(s), } - // Read in protocol buffer pages: their positions as the service's system - // places them, so there are no axes to check. - Downloaded::Read(read) => super::layer::add_read(s, &json!({"name": name}), *read, keep_view)?, - }; - mark_service_source(s, &r, from); - // Over an area, the features crossing it: the view stays, or goes to a box given - // (the whole layer's bounds would show a country to fill one state). - match (asked.is_some(), area, p.get("bbox")) { - (true, Some([w, so, e, n]), Some(Value::Array(_))) => s.zoom_to(&rustgis_geom::Extent::new(w, so, e, n)), - (true, ..) => {} - _ => zoom(s), - } - if let (Some(_), Some(out)) = (&asked, r.as_object_mut()) { - out.insert("area".into(), json!(area)); - } - Ok(r) + if let (Some(_), Some(out)) = (&asked, r.as_object_mut()) { + out.insert("area".into(), json!(area)); + } + Ok(r) + }) + .unwrap_or_else(|e| std::panic::resume_unwind(e)) } } } @@ -875,7 +904,7 @@ fn coverage(s: &mut Session, p: &Value) -> CmdResult { let p = bound.as_ref(); let url = url_of(p)?.to_string(); let want = ps(p, "layer").map(str::trim).filter(|l| !l.is_empty()).ok_or("layer is required")?; - let found = capabilities(p)?; + let found = capabilities(s, p)?; let layer = found .service .layers diff --git a/crates/engine/src/cmd/service_group.rs b/crates/engine/src/cmd/service_group.rs index 300e6ddc7d..f1025a6bb1 100644 --- a/crates/engine/src/cmd/service_group.rs +++ b/crates/engine/src/cmd/service_group.rs @@ -81,8 +81,8 @@ fn valid_connection(s: &Session, id: &str, url: &str) -> bool { }) } -fn record_connection(s: &mut Session, layer: u32, request: &Value) { - if request.get("anonymous").and_then(Value::as_bool) == Some(true) { +fn record_connection(s: &mut Session, layer: u32, request: &Value, anonymous: bool) { + if anonymous || request.get("anonymous").and_then(Value::as_bool) == Some(true) { if let Some(layer) = s.map_mut().layer_mut(layer) { layer.anonymous = true; layer.connection.clear(); @@ -107,7 +107,8 @@ fn plan(s: &mut Session, p: &Value) -> CmdResult { fn listing(s: &mut Session, p: &Value) -> CmdResult { let q = request(s, p)?; - let mut found = capabilities(&q)?; + let mut found = capabilities(s, &q)?; + super::notify_withheld(s, found.service.withheld_addresses); if let Some(error) = found.error { return Err(error); } @@ -122,15 +123,24 @@ fn listing(s: &mut Session, p: &Value) -> CmdResult { "the service lists collections RustGIS cannot read (more than 2000, or unreadable addresses); the group was not changed".into(), ); } - let child = target(s, p)?.and_then(|(g, id)| id.and_then(|id| g.children.into_iter().find(|c| c.layer == id))); + let previous = target(s, p)?; + let child = previous.as_ref().and_then(|(g, id)| id.and_then(|id| g.children.iter().find(|c| c.layer == id).cloned())); + let anonymous: std::collections::HashSet<&str> = previous + .as_ref() + .into_iter() + .flat_map(|(g, _)| &g.children) + .filter(|c| s.map().layer(c.layer).is_some_and(|l| l.anonymous)) + .map(|c| c.collection.as_str()) + .collect(); let mut identities = std::collections::HashSet::new(); let mut offered = Vec::new(); for (layer, source) in found.service.layers.into_iter().zip(found.sources) { - let source = source.address; + let mut source = source; if !matches!(layer.offer, Offer::Features { .. }) { continue; } - if !identities.insert((source.clone(), layer.name.clone())) { + source.crossed_origin |= anonymous.contains(layer.name.as_str()); + if !identities.insert((source.address.clone(), layer.name.clone())) { return Err("the service advertises duplicate collection identifiers".into()); } offered.push((layer, source)); @@ -141,7 +151,7 @@ fn listing(s: &mut Session, p: &Value) -> CmdResult { // One child: its collection at its saved source, or else the one collection with its id (a // rotated query or a moved collections document keeps the child's identity). if let Some(c) = &child { - let exact = offered.iter().position(|(l, src)| l.name == c.collection && super::same_address(src, &c.source_url)); + let exact = offered.iter().position(|(l, src)| l.name == c.collection && super::same_address(&src.address, &c.source_url)); let pos = match exact { Some(i) => Some(i), None => { @@ -163,7 +173,7 @@ fn listing(s: &mut Session, p: &Value) -> CmdResult { let mut out = service_json(&found.service); if let Some(layers) = out.get_mut("layers").and_then(Value::as_array_mut) { for (layer, source) in layers.iter_mut().zip(sources) { - layer["source_url"] = json!(source); + super::layer_source(layer, source); } } if let Some(next) = found.next { @@ -188,7 +198,7 @@ fn download(_: &mut Session, _: &str, _: &str, _: usize) -> Result, Stri Err("the browser must supply downloaded feature responses".into()) } -fn read(s: &mut Session, p: &Value) -> Result<(Value, Vec<(ServiceChild, Dataset)>), String> { +fn read(s: &mut Session, p: &Value) -> Result<(Value, Vec<(ServiceChild, Dataset, bool)>), String> { let listing = listing(s, p)?; if listing.get("next").is_some() { return Err("complete service discovery before importing or refreshing".into()); @@ -241,6 +251,7 @@ fn read(s: &mut Session, p: &Value) -> Result<(Value, Vec<(ServiceChild, Dataset source_url: ps(layer, "source_url").unwrap_or_default().to_string(), }, dataset, + layer.get("anonymous").and_then(Value::as_bool) == Some(true), )); } Ok((listing, reads)) @@ -267,10 +278,10 @@ fn import(s: &mut Session, p: &Value) -> CmdResult { } let mut children = Vec::new(); let mut extent = rustgis_geom::Extent::EMPTY; - for (mut child, dataset) in reads { + for (mut child, dataset, anonymous) in reads { child.layer = crate::cmd::layer::add_dataset(s, dataset); if let Some(request) = listing.get("request") { - record_connection(s, child.layer, request); + record_connection(s, child.layer, request, anonymous); } if let Some(layer) = s.map().layer(child.layer) { extent = extent.union(&layer.extent()); @@ -306,7 +317,7 @@ fn refresh(s: &mut Session, p: &Value) -> CmdResult { if !s.map().service_groups.iter().any(|g| g.id == old.id) { return Err("the group was removed".into()); } - let read_count = |name: &str| reads.iter().filter(|(c, _)| c.collection == name).count(); + let read_count = |name: &str| reads.iter().filter(|(c, _, _)| c.collection == name).count(); let old_count = |name: &str| old.children.iter().filter(|c| c.collection == name).count(); // A child refresh updates that child. A group refresh keeps a child whose collection is at the // same source, or else whose collection id is unique on both sides (the source address may @@ -315,7 +326,7 @@ fn refresh(s: &mut Session, p: &Value) -> CmdResult { Some(id) => reads.iter().map(|_| Some(id)).collect(), None => reads .iter() - .map(|(child, _)| { + .map(|(child, _, _)| { old.children .iter() .find(|c| c.collection == child.collection && super::same_address(&c.source_url, &child.source_url)) @@ -333,7 +344,7 @@ fn refresh(s: &mut Session, p: &Value) -> CmdResult { let mut notes = Vec::new(); let mut singled = Vec::new(); let mut moved = 0; - for ((mut child, mut dataset), existing) in reads.into_iter().zip(matches) { + for ((mut child, mut dataset, anonymous), existing) in reads.into_iter().zip(matches) { let existing = existing.and_then(|id| s.map_mut().layer_mut(id)); if let Some(layer) = existing { if dataset.geometry_type == rustgis_data::GeometryType::None { @@ -404,12 +415,12 @@ fn refresh(s: &mut Session, p: &Value) -> CmdResult { lock.retain(|((id, _, _, _), _)| *id != child.layer); } if let Some(request) = listing.get("request") { - record_connection(s, child.layer, request); + record_connection(s, child.layer, request, anonymous); } } else { child.layer = crate::cmd::layer::add_dataset(s, dataset); if let Some(request) = listing.get("request") { - record_connection(s, child.layer, request); + record_connection(s, child.layer, request, anonymous); } if hidden && let Some(layer) = s.map_mut().layer_mut(child.layer) { layer.visible = false; diff --git a/crates/engine/src/durable.rs b/crates/engine/src/durable.rs index 39420c3772..dceb7dd9c1 100644 --- a/crates/engine/src/durable.rs +++ b/crates/engine/src/durable.rs @@ -21,6 +21,7 @@ pub struct Report { pub(crate) not_remembered: bool, pub(crate) lost: u32, pub(crate) not_retained: u32, + pub(crate) executable_changes: u32, } impl Report { @@ -51,6 +52,10 @@ impl Report { (self.unchecked, "step(s) contain opaque parameters that may hold credentials and were kept unchecked"), (self.not_retained, "address(es) exceeded the bridge retention limit; their keys will not survive a round trip"), (self.lost, "address(es) lost their keys when a peer sent back its project; add them again in the receiving session"), + ( + self.executable_changes, + "credential-bearing executable field(s) changed when a peer sent back its project; supply missing values before replay", + ), ] { if n > 0 { out.push(format!("{n} {text}")); @@ -81,6 +86,7 @@ impl Report { self.not_remembered |= other.not_remembered; self.lost = self.lost.saturating_add(other.lost); self.not_retained = self.not_retained.saturating_add(other.not_retained); + self.executable_changes = self.executable_changes.saturating_add(other.executable_changes); } } @@ -259,7 +265,7 @@ impl<'a> Prepare<'a> { if binding_only { kind = Kind::Runtime; } - if !binding_only { + if !binding_only || matches!(leaf, Leaf::Video(_)) { if matches!(leaf, Leaf::Video(_)) { self.report.videos = self.report.videos.saturating_add(1); } else { @@ -621,13 +627,25 @@ fn public_address_at(address: &str, depth: usize) -> Kept { let file = crate::render::rustgis_tiles::cog::split(text).0; let look = text.get(file.len()..).unwrap_or(""); let Ok(Some(b)) = s3_address::parse(file) else { return Kept::Withheld(prefix) }; - return match public_address_at(b.url, depth + 1) { - Kept::Unchanged => Kept::Unchanged, - Kept::Changed(s) => match s3_address::bind(b.binding, b.endpoint, &s) { - Ok(s) => Kept::Changed(format!("{s}{look}")), - Err(_) => Kept::Withheld(prefix), - }, - Kept::Withheld(_) => Kept::Withheld(prefix), + let endpoint = public_address_at(b.endpoint, depth + 1); + let url = public_address_at(b.url, depth + 1); + if matches!(endpoint, Kept::Withheld(_)) || matches!(url, Kept::Withheld(_)) { + return Kept::Withheld(prefix); + } + if matches!((&endpoint, &url), (Kept::Unchanged, Kept::Unchanged)) { + return Kept::Unchanged; + } + let endpoint = match &endpoint { + Kept::Changed(s) => s.as_str(), + _ => b.endpoint, + }; + let url = match &url { + Kept::Changed(s) => s.as_str(), + _ => b.url, + }; + return match s3_address::bind(b.binding, endpoint, url) { + Ok(s) => Kept::Changed(format!("{s}{look}")), + Err(_) => Kept::Withheld(prefix), }; } if matches!(prefix, "postgresql:" | "postgres:" | "pg:") { @@ -647,8 +665,38 @@ fn public_address_at(address: &str, depth: usize) -> Kept { /// Emits the report only after rollback, when the session is whole again. pub(crate) fn notify(s: &mut crate::Session, report: &Report) { for text in report.notices() { - s.notify(text, false); + s.notify(format!("Prepared output: {text}"), false); + } +} + +/// Compares credential-bearing executable leaves through the same typed output inventory. +/// Peer input never restores these credentials; a changed or removed field is reported once. +pub(crate) fn changed_executables(old: &mut crate::Project, incoming: &mut crate::Project) -> Result { + let prepared = prepared(old, None, 0)?; + let mut count = 0u32; + for edit in prepared.edits { + if edit.kind != Kind::Credential + || !matches!( + edit.at.leaf, + Leaf::Task(_) + | Leaf::ModelStep(_) + | Leaf::ModelNode(_) + | Leaf::ModelLayer(_) + | Leaf::HistoryParams + | Leaf::HistoryCommand + | Leaf::CellSource(_) + ) + { + continue; + } + let same = match (old.field_mut(&edit.at.owners, &edit.at.leaf), incoming.field_mut(&edit.at.owners, &edit.at.leaf)) { + (Some(FieldMut::Text(a)), Some(FieldMut::Text(b))) => a == b, + (Some(FieldMut::Json(a)), Some(FieldMut::Json(b))) => a == b, + _ => false, + }; + count = count.saturating_add(u32::from(!same)); } + Ok(count) } impl Durable for Vec { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 9159a673dc..299b1c4d40 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -16,7 +16,7 @@ pub mod prefs; pub mod project; mod roles; pub use peer::Peer; -pub use roles::{Document, ParamRole, Role}; +pub(crate) use roles::{Document, ParamRole, Role}; mod parameters; pub mod project_log; pub use parameters::{params_envelope, script_line}; @@ -66,8 +66,8 @@ pub struct CommandSpec { /// `None` = enabled; `Some(reason)` = disabled. pub enabled: fn(&Session) -> Option<&'static str>, pub run: fn(&mut Session, &Value) -> CmdResult, - /// Durable-output roles. None keeps unreviewed extension parameters opaque. - pub roles: Option<&'static [ParamRole]>, + /// Reviewed builtin durable-output roles. Extension parameters remain opaque. + pub(crate) roles: Option<&'static [ParamRole]>, } fn always(_: &Session) -> Option<&'static str> { @@ -89,8 +89,8 @@ 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 } } - /// Declares durable-output roles; an empty list uses reviewed common parameter rules. - pub const fn roles(mut self, roles: &'static [ParamRole]) -> 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 } diff --git a/crates/engine/src/peer.rs b/crates/engine/src/peer.rs index 099c2fc6d8..58b4e7a9cc 100644 --- a/crates/engine/src/peer.rs +++ b/crates/engine/src/peer.rs @@ -6,23 +6,52 @@ use crate::{ durable::{self, Report}, }; use durable::{At, Durable, FieldMut, Kept, Leaf, Owner}; +use std::borrow::Cow; use std::collections::{HashMap, HashSet}; #[derive(Clone, Debug, PartialEq, Eq, Hash)] -enum SlotKey { - Map { index: usize, name: String, at: rustgis_map::SlotAt }, - Area { index: usize, at: rustgis_map::SlotAt }, +enum Position<'a> { + Layer { id: u32, role: rustgis_map::LayerRole }, + Tile { id: Cow<'a, str>, role: rustgis_map::TileRole }, + Group { id: u32, child: Option }, + Terrain, +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +enum SlotKey<'a> { + Map { index: usize, name: Cow<'a, str>, at: Position<'a> }, + Area { index: usize, at: Position<'a> }, Table(u32), Video { presentation: usize, page: usize }, } +impl SlotKey<'_> { + /// Only retained, budgeted identities own copies; raw censuses borrow hostile names. + fn owned(&self) -> SlotKey<'static> { + let position = |at: &Position<'_>| match at { + Position::Layer { id, role } => Position::Layer { id: *id, role: *role }, + Position::Tile { id, role } => Position::Tile { id: Cow::Owned(id.to_string()), role: *role }, + Position::Group { id, child } => Position::Group { id: *id, child: *child }, + Position::Terrain => Position::Terrain, + }; + match self { + Self::Map { index, name, at } => SlotKey::Map { index: *index, name: Cow::Owned(name.to_string()), at: position(at) }, + Self::Area { index, at } => SlotKey::Area { index: *index, at: position(at) }, + Self::Table(id) => SlotKey::Table(*id), + Self::Video { presentation, page } => SlotKey::Video { presentation: *presentation, page: *page }, + } + } +} + struct Slot<'a> { - key: SlotKey, + key: SlotKey<'a>, at: At, value: &'a str, + layer: Option<&'a rustgis_map::Layer>, } struct Entry { - key: SlotKey, + key: SlotKey<'static>, + choice: Option, public: String, full: String, ambiguous: bool, @@ -40,29 +69,42 @@ fn slots(p: &Project) -> Vec> { let mut out = Vec::new(); for (i, m) in p.maps.iter().enumerate() { for slot in m.slots().into_iter().filter(|s| s.address) { - if let Some(at) = m.slot_identity(&slot.at) { + if let Some(at) = position(m, &slot.at) { out.push(Slot { - key: SlotKey::Map { index: i, name: m.name.clone(), at }, - at: At { owners: vec![Owner::Map(i)], leaf: Leaf::Map(slot.at) }, + key: SlotKey::Map { index: i, name: Cow::Borrowed(&m.name), at }, + at: At { owners: vec![Owner::Map(i)], leaf: Leaf::Map(slot.at.clone()) }, value: slot.value, + layer: match slot.at { + rustgis_map::SlotPos::Layer { layer, .. } => m.layers.get(layer), + _ => None, + }, }); } } } for (i, area) in p.offline_areas.iter().enumerate() { for slot in area.data.slots().into_iter().filter(|s| s.address) { - if let Some(at) = area.data.slot_identity(&slot.at) { + if let Some(at) = position(&area.data, &slot.at) { out.push(Slot { key: SlotKey::Area { index: i, at }, - at: At { owners: vec![Owner::Area(i)], leaf: Leaf::Map(slot.at) }, + at: At { owners: vec![Owner::Area(i)], leaf: Leaf::Map(slot.at.clone()) }, value: slot.value, + layer: match slot.at { + rustgis_map::SlotPos::Layer { layer, .. } => area.data.layers.get(layer), + _ => None, + }, }); } } } for (i, table) in p.tables.iter().enumerate() { if let Some(value) = &table.source { - out.push(Slot { key: SlotKey::Table(table.id), at: At { owners: vec![Owner::Table(i)], leaf: Leaf::Table }, value }); + out.push(Slot { + key: SlotKey::Table(table.id), + at: At { owners: vec![Owner::Table(i)], leaf: Leaf::Table }, + value, + layer: None, + }); } } for (i, pres) in p.presentations.iter().enumerate() { @@ -72,6 +114,7 @@ fn slots(p: &Project) -> Vec> { key: SlotKey::Video { presentation: i, page }, at: At { owners: vec![Owner::Presentation(i)], leaf: Leaf::Video(page) }, value: source, + layer: None, }); } } @@ -79,6 +122,88 @@ fn slots(p: &Project) -> Vec> { out } +/// Resolves raw owner identity without copying untrusted map or tile names. +fn position<'a>(map: &'a rustgis_map::Map, at: &rustgis_map::SlotPos) -> Option> { + use rustgis_map::SlotPos; + Some(match at { + SlotPos::Layer { layer, role } => Position::Layer { id: map.layers.get(*layer)?.id, role: *role }, + SlotPos::Tile { tile, role } => Position::Tile { id: Cow::Borrowed(&map.tiles.get(*tile)?.id), role: *role }, + SlotPos::Group { group, child } => { + let group = map.service_groups.get(*group)?; + Position::Group { + id: group.id, + child: match child { + Some(i) => Some(group.children.get(*i)?.layer), + None => None, + }, + } + } + SlotPos::Terrain => Position::Terrain, + }) +} + +/// Read selection, not drawing style or cached rows. The last exchange, live source and raw +/// incoming source must agree before normalization or a full address can be retained. +fn source_choice(layer: Option<&rustgis_map::Layer>) -> Result, ()> { + let Some(layer) = layer.filter(|l| l.source.is_some() || l.raster.is_some() || l.postgis.is_some()) else { return Ok(None) }; + if layer.output_slots().iter().any(|(_, value, _, _)| value.len() > MAX_BYTES) + || layer.source.as_ref().and_then(|s| s.embedded.as_ref()).is_some_and(|b| b.len() > MAX_BYTES) + || layer.raster.as_ref().and_then(|r| r.embedded.as_ref()).is_some_and(|b| b.len() > MAX_BYTES) + { + return Err(()); + } + let public = |s: &str| match durable::public_address(s) { + Kept::Unchanged | Kept::Withheld(_) => s.to_string(), + Kept::Changed(s) => s, + }; + let source = layer.source.as_ref().map(|s| { + ( + s.path.as_deref().map(public), + &s.file_name, + &s.table, + &s.shapefile_crs, + s.embedded.as_deref(), + s.gdal.as_ref().map(|g| (public(&g.path), &g.layer, g.family, &g.crs)), + s.duckdb.as_ref().map(|d| (&d.sql, d.database.as_deref().map(public), &d.table, &d.crs, d.family)), + s.postgis.as_ref().map(|d| (public(&d.database), &d.table, d.family, &d.crs)), + s.mysql.as_ref().map(|d| (public(&d.database), &d.table, d.family, &d.crs)), + s.sqlserver.as_ref().map(|d| (public(&d.database), &d.table, d.family, &d.crs)), + ) + }); + let raster = layer.raster.as_ref().map(|r| { + ( + r.path.as_deref().map(public), + &r.file_name, + r.embedded.as_deref(), + &r.subdataset, + &r.crs, + r.placement, + r.time_zarr.as_ref().map(|z| (public(&z.store), &z.variable, &z.index, &z.dim, z.offset, &z.dates, &z.crs)), + ) + }); + let edit = layer.postgis.as_ref().map(|p| { + (public(&p.origin.database), &p.origin.table, p.origin.family, &p.origin.crs, &p.key, p.srid, p.writable, &p.condition, p.limit) + }); + struct Bounded(Vec); + impl std::io::Write for Bounded { + /// Stops at the retention budget before allocating another chunk. + fn write(&mut self, bytes: &[u8]) -> std::io::Result { + if self.0.len().saturating_add(bytes.len()) > MAX_BYTES { + return Err(std::io::Error::other("peer source identity exceeds retention limit")); + } + self.0.extend_from_slice(bytes); + Ok(bytes.len()) + } + /// The bounded buffer has no pending I/O. + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + let mut out = Bounded(Vec::new()); + serde_json::to_writer(&mut out, &(source, raster, edit)).map_err(|_| ())?; + String::from_utf8(out.0).map(Some).map_err(|_| ()) +} + /// A wrapper's binding belongs to S3 connection resolution, never exchange retention. fn wrapped(value: &str) -> bool { let value = crate::sources::template_archive(value).map_or(value, |(_, v)| v); @@ -86,16 +211,16 @@ fn wrapped(value: &str) -> bool { } /// Charges both forms and the complete identity payload, bounded before copying either form. -fn key_bytes(key: &SlotKey) -> usize { +fn key_bytes(key: &SlotKey<'_>) -> usize { let extra = match key { SlotKey::Map { name, at, .. } => name.len().saturating_add(match at { - rustgis_map::SlotAt::Tile { id, .. } => id.len(), + Position::Tile { id, .. } => id.len(), _ => 0, }), - SlotKey::Area { at: rustgis_map::SlotAt::Tile { id, .. }, .. } => id.len(), + SlotKey::Area { at: Position::Tile { id, .. }, .. } => id.len(), _ => 0, }; - std::mem::size_of::().saturating_add(extra) + std::mem::size_of::>().saturating_add(extra) } impl Exchange { @@ -120,7 +245,16 @@ impl Exchange { if let Some((first, ambiguous)) = forms.get_mut(&public) { *ambiguous |= entries.get(*first).is_none_or(|e| e.full != slot.value); } - let bytes = slot.value.len().saturating_add(public.len()).saturating_add(key_bytes(&slot.key)); + let Ok(choice) = source_choice(slot.layer) else { + omitted = omitted.saturating_add(1); + continue; + }; + let bytes = slot + .value + .len() + .saturating_add(public.len()) + .saturating_add(key_bytes(&slot.key)) + .saturating_add(choice.as_ref().map_or(0, String::len)); if !recording || entries.len() >= MAX_ENTRIES || held.saturating_add(bytes) > MAX_BYTES { recording = false; omitted = omitted.saturating_add(1); @@ -128,7 +262,7 @@ impl Exchange { } held += bytes; forms.entry(public.clone()).or_insert((entries.len(), false)); - entries.push(Entry { key: slot.key, public, full: slot.value.into(), ambiguous: false }); + entries.push(Entry { key: slot.key.owned(), choice, public, full: slot.value.into(), ambiguous: false }); } for entry in &mut entries { entry.ambiguous = forms.get(&entry.public).is_none_or(|(_, ambiguous)| *ambiguous); @@ -137,24 +271,28 @@ impl Exchange { } /// The pre-normalization census must contain exactly one byte-equal public slot. - fn incoming(&self, p: &Project) -> HashSet { + fn incoming(&self, p: &Project) -> HashSet> { let census = census(p); self.entries .iter() - .filter(|e| census.get(&e.key).is_some_and(|(n, value)| *n == 1 && *value == e.public)) + .filter(|e| { + census.get(&e.key).is_some_and(|(n, value, layer)| { + *n == 1 && *value == e.public && source_choice(*layer).ok().as_ref() == Some(&e.choice) + }) + }) .map(|e| e.key.clone()) .collect() } /// Checks last exchange, both incoming censuses, unique account and the exact current live /// value in one synchronous replacement. Public URL or incoming id alone grants nothing. - fn retain(&self, live: &Project, incoming: &mut Project, raw: &HashSet) { + fn retain(&self, live: &Project, incoming: &mut Project, raw: &HashSet>) { let current = census(live); let normalized = census(incoming); - let mut records: HashMap<&SlotKey, (usize, &Entry)> = HashMap::new(); + let mut records: HashMap, (usize, &Entry)> = HashMap::new(); let mut accounts: HashMap<&str, Option<&str>> = HashMap::new(); for e in &self.entries { - records.entry(&e.key).or_insert((0, e)).0 += 1; + records.entry(e.key.clone()).or_insert((0, e)).0 += 1; let account = accounts.entry(&e.public).or_insert(Some(&e.full)); if *account != Some(e.full.as_str()) { *account = None; @@ -164,11 +302,13 @@ impl Exchange { .into_iter() .filter_map(|slot| { let (count, e) = records.get(&slot.key)?; - let unique = |census: &HashMap, expected: &str| { - census.get(&slot.key).is_some_and(|(n, value)| *n == 1 && *value == expected) + let unique = |census: &Census<'_>, expected: &str| { + census.get(&slot.key).is_some_and(|(n, value, layer)| { + *n == 1 && *value == expected && source_choice(*layer).ok().as_ref() == Some(&e.choice) + }) }; (!e.ambiguous - && raw.contains(&slot.key) + && raw.contains(&e.key) && *count == 1 && accounts.get(e.public.as_str()) == Some(&Some(e.full.as_str())) && unique(¤t, &e.full) @@ -221,10 +361,13 @@ fn reopen(p: &mut Project, at: &At) { } /// Values borrow the project, but identities include all duplicates; no normalization runs here. -fn census(p: &Project) -> HashMap { +type Census<'a> = HashMap, (usize, &'a str, Option<&'a rustgis_map::Layer>)>; + +/// Counts raw duplicates while borrowing all untrusted identity text. +fn census(p: &Project) -> Census<'_> { let mut out = HashMap::new(); for slot in slots(p) { - let entry = out.entry(slot.key).or_insert((0usize, slot.value)); + let entry = out.entry(slot.key).or_insert((0usize, slot.value, slot.layer)); entry.0 = entry.0.saturating_add(1); } out @@ -240,6 +383,7 @@ impl Session { /// Writes a credential-free project for a peer, without evicting explicit-save restoration. /// Pending downloads require a structural draft; settled projects use the live journal. pub fn peer_text(&mut self, peer: Peer) -> Result<(String, Report), String> { + let runtime = self.project.bound_address_losses(true); let (text, mut report) = if self.project.ensure_imports_complete().is_err() { let mut draft = self.project.clone(); draft.omit_pending_imports(); @@ -247,6 +391,7 @@ impl Session { } else { durable::output(&mut self.project, None, text) }?; + report.addresses = report.addresses.saturating_add(u32::try_from(runtime).unwrap_or(u32::MAX)); self.exchange = if peer == Peer::Bridge { let (record, omitted) = Exchange::record(&self.project, self.opened); report.not_retained = omitted; @@ -275,12 +420,13 @@ impl Session { .into_iter() .filter(|slot| { !matches!(durable::public_address(slot.value), Kept::Unchanged) - && !incoming.get(&slot.key).is_some_and(|(count, value)| *count == 1 && *value == slot.value) + && !incoming.get(&slot.key).is_some_and(|(count, value, _)| *count == 1 && *value == slot.value) }) .count() .try_into() .unwrap_or(u32::MAX); drop(incoming); + report.executable_changes = durable::changed_executables(&mut self.project, &mut p)?; crate::cmd::project::reset_to(self, p); self.path = None; crate::cmd::connections::resolve_project(self); @@ -303,3 +449,39 @@ fn text(p: &Project) -> Result { } Ok(text) } + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + /// Table/family/CRS are part of a retained reader, even before normalization opens it. + #[test] + fn bridge_census_binds_the_full_source_choice_before_normalization() { + let mut p = Project::default(); + let id = p.map_mut().add_dataset(rustgis_data::Dataset::new("Database", rustgis_data::GeometryType::None, vec![])); + let mut source = rustgis_map::source::SourceLayer::default(); + source.postgis = Some(rustgis_map::source::PostgisOrigin { + database: "postgresql://reader:RGSYNTH_peer_choice@db.invalid/gis".into(), + table: "approved".into(), + family: None, + crs: None, + }); + p.map_mut().layer_mut(id).unwrap().source = Some(source); + let mut s = Session::new(p); + let (public, _) = s.peer_text(Peer::Bridge).unwrap(); + let exchange = s.exchange.as_ref().unwrap(); + for change in ["style", "table", "family", "crs"] { + let mut p = Project::from_peer_raw(&public).unwrap(); + let layer = &mut p.maps[0].layers[0]; + let pg = layer.source.as_mut().unwrap().postgis.as_mut().unwrap(); + match change { + "table" => pg.table = "different".into(), + "family" => pg.family = Some(rustgis_data::source::Family::Points), + "crs" => pg.crs = Some("EPSG:4326".into()), + _ => layer.visible = false, + } + assert_eq!(exchange.incoming(&p).len(), usize::from(change == "style"), "{change}"); + } + } +} diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 06e9d7b436..9243b1c47c 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -653,30 +653,35 @@ impl Project { addresses } - /// How many layers and tile layers read through a login binding but record no connection: - /// nothing would tell the opened project which login they need. One whose address also holds - /// a token isn't counted here: the durable address projection counts it already. - fn unrecorded_bound(&mut self) -> usize { + /// Runtime bindings that this output cannot restore: peer receivers never share a login; + /// explicit files can resolve layers and tile owners with a recorded saved connection. + pub(crate) fn bound_address_losses(&self, include_recorded: bool) -> usize { let only_bound = |a: &str| { without_login_binding(a) .is_some_and(|public| matches!(crate::durable::public_address(&public), crate::durable::Kept::Unchanged)) }; - let mut n = 0; - for map in self.maps.iter_mut().chain(self.offline_areas.iter_mut().map(|area| &mut area.data)) { - let tiles: std::collections::BTreeSet<_> = map - .slots() - .into_iter() - .filter_map(|slot| { - let rustgis_map::SlotPos::Tile { tile, .. } = slot.at else { return None }; - map.tiles.get(tile).filter(|t| t.connection.is_empty() && only_bound(slot.value)).map(|_| tile) - }) - .collect(); - n += tiles.len(); - for layer in map.layers.iter_mut().filter(|l| l.connection.is_empty()) { - n += usize::from(layer.addresses_mut().any(|a| only_bound(a))); + let mut n = 0usize; + for map in self.maps.iter().chain(self.offline_areas.iter().map(|area| &area.data)) { + let mut layers = std::collections::BTreeSet::new(); + let mut tiles = std::collections::BTreeSet::new(); + for slot in map.slots().into_iter().filter(|slot| slot.address && only_bound(slot.value)) { + match slot.at { + rustgis_map::SlotPos::Layer { layer, .. } => { + if map.layers.get(layer).is_some_and(|l| include_recorded || l.connection.is_empty()) { + layers.insert(layer); + } + } + rustgis_map::SlotPos::Tile { tile, .. } => { + if map.tiles.get(tile).is_some_and(|t| include_recorded || t.connection.is_empty()) { + tiles.insert(tile); + } + } + rustgis_map::SlotPos::Group { .. } | rustgis_map::SlotPos::Terrain => n = n.saturating_add(1), + } } + n = n.saturating_add(layers.len()).saturating_add(tiles.len()); } - n + n.saturating_add(self.tables.iter().filter(|table| table.source.as_deref().is_some_and(only_bound)).count()) } /// The standalone table of an id. @@ -948,7 +953,7 @@ impl Project { /// fields are restored on every return and native unwind; only explicit saves are remembered. pub fn saved_text(&mut self, dir: Option<&Path>) -> Result<(String, crate::durable::Report), String> { self.ensure_imports_complete()?; - let unrecorded = self.unrecorded_bound(); + let unrecorded = self.bound_address_losses(false); let (text, mut report) = crate::durable::output_remembered(self, dir.and_then(Path::to_str), project_text)?; report.addresses = report.addresses.saturating_add(u32::try_from(unrecorded).unwrap_or(u32::MAX)); Ok((text, report)) @@ -970,7 +975,9 @@ impl Project { /// Text too large to open again is refused. Returns the text and its counted output notices. pub fn recovery_text_with_report(mut self) -> Result<(String, crate::durable::Report), String> { self.omit_pending_imports(); - let (text, report) = crate::durable::output_owned(self, project_text)?; + let unrecorded = self.bound_address_losses(false); + let (text, mut report) = crate::durable::output_owned(self, project_text)?; + report.addresses = report.addresses.saturating_add(u32::try_from(unrecorded).unwrap_or(u32::MAX)); if text.len() as u64 > MAX_BYTES { return Err("the project is too large to keep a recovery copy".into()); } diff --git a/crates/engine/src/project_log.rs b/crates/engine/src/project_log.rs index 431d5dd8ff..57b73dde59 100644 --- a/crates/engine/src/project_log.rs +++ b/crates/engine/src/project_log.rs @@ -135,6 +135,25 @@ fn redacted(v: &Value) -> Value { crate::cmd::history::redacted(&embedded_redacted(v, 0)) } +/// A bounded log-only context check for a short key beside nested connection settings. +/// User data/documents do not make an ordinary join or lookup key a credential. +pub(crate) fn address_context(key: &str, v: &Value, depth: usize) -> bool { + if depth >= 8 || matches!(key, "data" | "text" | "values" | "attributes" | "properties" | "features" | "geometry") { + return false; + } + match v { + Value::String(s) => { + let s = s.trim_start(); + ["http://", "https://", "s3://", "rustgis-login:", "rustgis-s3:", "postgres:", "postgresql:", "mysql:", "mssql:"] + .iter() + .any(|prefix| s.get(..prefix.len()).is_some_and(|head| head.eq_ignore_ascii_case(prefix))) + } + Value::Array(values) => values.iter().take(LOG_LIST_ITEMS).any(|v| address_context("", v, depth + 1)), + Value::Object(values) => values.iter().any(|(key, v)| address_context(key, v, depth + 1)), + _ => false, + } +} + /// Scrubs bounded nested log parameters and JSON embedded in text before the log retains them. fn embedded_redacted(v: &Value, depth: usize) -> Value { match v { diff --git a/crates/engine/src/roles.rs b/crates/engine/src/roles.rs index 7574e4fee8..3d7c069fa8 100644 --- a/crates/engine/src/roles.rs +++ b/crates/engine/src/roles.rs @@ -12,18 +12,17 @@ use crate::durable::{self, Durable, Kept, Report}; /// A replayable parameter's durable-output role. Permission policy does not consume roles. #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct ParamRole { +pub(crate) struct ParamRole { pub key: &'static str, pub role: Role, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum Role { - Address, - Path, +pub(crate) enum Role { Secret, Dataset, Configuration, Sql, + Opaque, ToolParams, Script, TaskSteps, @@ -32,7 +31,7 @@ pub enum Role { Document(Document), } #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum Document { +pub(crate) enum Document { Project, Map, Task, @@ -77,7 +76,7 @@ const ADDRESS: &[&str] = &["url", "urls", "href", "source", "template", "tiles", "store", "endpoint", "address", "uri", "connection", "database"]; const CONFIG: &[&str] = &["headers", "options", "auth", "connection", "source", "sources"]; -/// Durable output reads its own field from the immutable command registry. An explicit empty +/// Durable output reads its own field from reviewed builtin registrations. An explicit empty /// declaration retains the reviewed common scalar/address/configuration rules below. fn metadata(id: &str) -> Option<&'static [ParamRole]> { static ROLES: OnceLock> = OnceLock::new(); @@ -149,6 +148,9 @@ fn object(id: &str, value: &Value, depth: usize, config_depth: usize, registered } else { envelope(role, v, obj, depth + 1, report) } + } else if role == Some(Role::Opaque) { + unchecked |= suspicious(v); + None } else if (role == Some(Role::Secret) || rustgis_data::address::secret_key(key)) && (k != "key" || crate::sources::looks_like_a_key(v.as_str().unwrap_or("")) @@ -175,7 +177,7 @@ fn object(id: &str, value: &Value, depth: usize, config_depth: usize, registered let clean = crate::project_log::sql_secrets_removed(s); (clean != s).then_some(Value::String(clean)) }) - } else if matches!(role, Some(Role::Address | Role::Path)) || ADDRESS.contains(&k.as_str()) || address_like(v) { + } else if ADDRESS.contains(&k.as_str()) || address_like(v) { if v.is_object() && registered && (role == Some(Role::Configuration) || CONFIG.contains(&k.as_str())) { object("", v, depth, config_depth + 1, true, report) } else { diff --git a/crates/engine/src/tests/credential_output.rs b/crates/engine/src/tests/credential_output.rs index d9cc5be2aa..7163d3c22a 100644 --- a/crates/engine/src/tests/credential_output.rs +++ b/crates/engine/src/tests/credential_output.rs @@ -123,6 +123,12 @@ fn durable_empty_query_pairs_preserve_clean_urls_and_report_real_loss() { ("layer=1&", "layer=1&", 0), ("layer=1 \t", "layer=1 \t", 0), ("layer=1&&token=RGSYNTH_empty_query&", "layer=1", 1), + ("layer=1&appid=RGSYNTH_empty_query", "layer=1", 1), + ("layer=1&%61ppid=RGSYNTH_empty_query", "layer=1", 1), + ("layer=1&subscription-key=RGSYNTH_empty_query", "layer=1", 1), + ("layer=1&client_key=RGSYNTH_empty_query", "layer=1", 1), + ("layer=1&sas-key=RGSYNTH_empty_query", "layer=1", 1), + ("client_id=public&application_id=public&map=1", "client_id=public&application_id=public&map=1", 0), ] { let url = format!("https://h.invalid/tiles/{{z}}/{{x}}/{{y}}.png?{query}"); let mut project = Project::default(); @@ -559,6 +565,7 @@ fn durable_database_origins_clean_each_owned_field_without_opening_a_database() ("postgis", "postgresql", "/source/postgis/database"), ("mysql", "mysql", "/source/mysql/database"), ("sqlserver", "mssql", "/source/sqlserver/database"), + ("duckdb", "postgresql", "/source/duckdb/database"), ] { for private in [false, true] { let public = format!("{scheme}://reader@db-origin.invalid/gis"); @@ -566,7 +573,10 @@ fn durable_database_origins_clean_each_owned_field_without_opening_a_database() if private { format!("{scheme}://reader:RGSYNTH_db_origin_20261106@db-origin.invalid/gis") } else { public.clone() }; let dataset = rustgis_data::Dataset::new("Origin", rustgis_data::GeometryType::None, Vec::new()); let mut layer = serde_json::to_value(rustgis_map::Layer::new(1, dataset)).unwrap(); - let origin = json!({"database":address, "table":"rows"}); + let mut origin = json!({"database":address, "table":"rows"}); + if role == "duckdb" { + origin["family"] = json!("Points"); + } if role == "edit" { layer["postgis"] = json!({"origin":origin, "key":"id", "srid":4326, "writable":false}); } else { @@ -606,3 +616,139 @@ fn durable_database_origins_clean_each_owned_field_without_opening_a_database() } } } + +/// Runtime-only login omissions and executable replacement losses use different notice kinds. +#[test] +fn peer_output_counts_runtime_logins_and_changed_executable_fields() { + let public = "https://peer-runtime.invalid/data"; + let bound = rustgis_data::login_address::bind("0123456789abcdef", "https://peer-runtime.invalid", public).unwrap(); + let mut s = Session::new(Project::default()); + let mut dataset = rustgis_data::Dataset::new("Bound", rustgis_data::GeometryType::None, Vec::new()); + dataset.source = Some(bound.clone()); + let id = s.map_mut().add_dataset(dataset); + s.map_mut().layer_mut(id).unwrap().connection = "saved-connection".into(); + s.project.tasks.push(Task { + name: "Login".into(), + description: String::new(), + steps: vec![TaskStep { + title: String::new(), + instructions: String::new(), + command: "connection.login".into(), + params: json!({"apiKey":SECRET}), + }], + }); + let (text, report) = s.peer_text(Peer::Embedder).unwrap(); + assert_eq!(report.layer_addresses(), 1, "a foreign receiver cannot use the recorded connection's live binding"); + assert!(!text.contains("rustgis-login:") && !text.contains(SECRET)); + assert_eq!(s.map().layer(id).unwrap().dataset.source.as_deref(), Some(bound.as_str())); + let report = s.open_peer_text(Peer::Embedder, &text).unwrap(); + assert_eq!(report.executable_changes, 1); + assert!(report.notices().iter().any(|n| n.contains("executable field(s) changed"))); + assert_eq!(s.project.tasks[0].steps[0].params["apiKey"], ""); +} + +/// A valid S3 wrapper's endpoint is also an address, not an opaque place to store a password. +#[test] +fn durable_s3_endpoints_clean_recognized_credentials_and_keep_provenance() { + for (endpoint, expected, count) in [ + ("https://reader:RGSYNTH_endpoint@h.invalid", "https://h.invalid", 1), + ("https://h.invalid?token=RGSYNTH_endpoint", "https://h.invalid", 1), + ("https://h.invalid", "https://h.invalid", 0), + ("", "", 0), + ] { + let address = rustgis_data::s3_address::bind("0123456789abcdef", endpoint, "s3://bucket/data").unwrap(); + let mut p = Project::default(); + let mut table = rustgis_data::Dataset::new("S3", rustgis_data::GeometryType::None, Vec::new()); + table.source = Some(address.clone()); + p.add_table(table); + let (text, report) = p.saved_text(None).unwrap(); + assert!(!text.contains("RGSYNTH_endpoint")); + assert_eq!(report.layer_addresses(), count); + let value: Value = serde_json::from_str(&text).unwrap(); + let parsed = rustgis_data::s3_address::parse(value["tables"][0]["source"].as_str().unwrap()).unwrap().unwrap(); + assert_eq!((parsed.binding, parsed.endpoint, parsed.url), ("0123456789abcdef", expected, "s3://bucket/data")); + assert_eq!(p.tables[0].source.as_deref(), Some(address.as_str())); + } +} + +#[test] +fn nested_notebook_source_stays_opaque_but_is_reported_unchecked() { + for command in ["notebook.addCell", "notebook.editCell"] { + for source in ["layer.addUrl {\"url\":\"https://h.invalid/?token=RGSYNTH_nested\"}", "print('RGSYNTH_nested token')", "clean prose"] + { + let mut p = Project::default(); + p.tasks.push(Task { + name: "Nested".into(), + description: String::new(), + steps: vec![TaskStep { + title: String::new(), + instructions: String::new(), + command: command.into(), + params: json!({"source":source}), + }], + }); + let (text, report) = p.saved_text(None).unwrap(); + let written: Value = serde_json::from_str(&text).unwrap(); + assert_eq!(written["tasks"][0]["steps"][0]["params"]["source"], source); + assert_eq!(report.unchecked, u32::from(source != "clean prose")); + assert_eq!(report.steps, 0); + } + } + assert_eq!(crate::log_params(&json!({"key":"abc", "source":{"url":"https://h.invalid/a"}})).into_log_value()["key"], ""); + assert_eq!(crate::log_params(&json!({"key":"abc", "data":{"url":"https://h.invalid/a"}})).into_log_value()["key"], "abc"); +} + +/// A retained address must actually reopen its protected reader, not merely survive as text. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn bridge_round_trip_reopens_its_protected_reader() { + if super::isolate_account_test("tests::credential_output::bridge_round_trip_reopens_its_protected_reader") { + return; + } + use rustgis_data::source::{fgb, remote}; + let (dataset, _) = rustgis_data::io::read_geojson(r#"{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":{"value":7}}]}"#, "Protected").unwrap(); + let bytes = fgb::write(&dataset).unwrap(); + let original = fgb::FgbSource::open_bytes(bytes.clone(), "Protected").unwrap(); + let full = "https://protected-reader.invalid/data.fgb?token=RGSYNTH_reader"; + // Session construction installs the native fetcher; replace it only after that setup. + let mut s = Session::new(Project::default()); + remote::set_fetcher(move |url, want, done| { + if url != full { + done(Err(remote::Failed::Other("protected reader needs its token".into()))); + } else if matches!(want, remote::Want::Whole) { + done(Ok(bytes.clone())); + } else { + done(Err(remote::Failed::NoRanges)); + } + }); + let id = s.map_mut().add_dataset(dataset); + s.map_mut().layer_mut(id).unwrap().source = Some(rustgis_map::source::SourceLayer::from_path(full, None, Box::new(original))); + let (text, _) = s.peer_text(Peer::Bridge).unwrap(); + assert!(!text.contains("RGSYNTH_reader")); + let report = s.open_peer_text(Peer::Bridge, &text).unwrap(); + assert_eq!(report.lost, 0, "the unchanged streamed source retains its address"); + let source = s.map_mut().layer_mut(id).unwrap().source.as_mut().unwrap(); + assert_eq!(source.path.as_deref(), Some(full), "the exact source keeps its protected address"); + source.wait_open(std::time::Duration::from_secs(5)); + assert!(source.is_open(), "retained reader failed to open: {:?}", source.error); + let (read, _) = source.read_all().unwrap(); + assert_eq!(read.features.len(), 1); + assert_eq!(read.features[0].attrs[0], rustgis_data::Value::Int(7)); +} + +/// Discovery must announce omitted settings even when the following supplied document is invalid. +#[test] +fn failed_collections_discovery_retains_its_counted_query_notice() { + for (query, count) in [("", 0), ("?token=RGSYNTH_discovery", 1)] { + let mut s = Session::new(Project::default()); + let p = json!({"url":format!("https://discovery.invalid/root{query}"), "type":"ogcapi", + "text":json!({"links":[{"rel":"data", "type":"application/json", "href":"/catalog"}]}).to_string(), + "documents":[{"url":"https://discovery.invalid/catalog", "text":"invalid capabilities"}]}); + assert!(s.run("service.layers", &p).is_err()); + let messages: Vec<_> = s.notices.iter().filter(|n| n.text.contains("address(es) the service listed")).collect(); + assert_eq!(messages.len(), count); + if count > 0 { + assert!(messages[0].text.starts_with("1 address(es)")); + } + } +} diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index 404785318d..0a5b63e4b2 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -1602,3 +1602,57 @@ fn capabilities_styles_keep_foreign_lineage() { } assert!(results.iter().all(|(_, _, kept)| *kept), "style discovery forgot its foreign hop: {results:?}"); } + +/// Features reached through foreign capabilities keep the same opt-out as tile discovery. +#[test] +fn feature_group_plans_and_imports_keep_foreign_capabilities_anonymous() { + #[cfg(not(target_arch = "wasm32"))] + if super::isolate_account_test("tests::service_logins::feature_group_plans_and_imports_keep_foreign_capabilities_anonymous") { + return; + } + for (document_crossed, items_crossed, authenticated) in + [(false, false, false), (false, false, true), (true, false, true), (false, true, true)] + { + let crossed = document_crossed || items_crossed; + let mut s = session(None); + let a = "https://feature-lineage-a.invalid"; + let b = "https://feature-lineage-b.invalid"; + let bridge = if document_crossed { format!("{b}/bridge") } else { format!("{a}/bridge") }; + let items = if items_crossed { format!("{b}/items") } else { format!("{a}/items") }; + let login = new(&mut s, json!({"kind":"ogcapi", "address":a, "auth":if authenticated {"header"} else {"none"}, "keyName":"X-Key"})); + if authenticated { + s.run("connection.login", &json!({"id":login, "apiKey":"RGSYNTH_feature_lineage"})).unwrap(); + } + let other = new(&mut s, json!({"kind":"ogcapi", "address":b, "auth":"header", "keyName":"X-Key"})); + s.run("connection.login", &json!({"id":other, "apiKey":"RGSYNTH_feature_destination"})).unwrap(); + let link = |url: &str| json!({"links":[{"rel":"data", "type":"application/json", "href":url}]}).to_string(); + let mut params = json!({"url":format!("{a}/root"), "type":"ogcapi", "connection":login, + "text":link(&bridge), "documents":[ + {"url":bridge,"text":link(&format!("{a}/returned"))}, + {"url":format!("{a}/returned"),"text":json!({"collections":[{"id":"roads", "links":[{"rel":"items", "type":"application/geo+json", "href":items}]}]}).to_string()} + ]}); + let plan = s.run("service.groupPlan", ¶ms).unwrap(); + let feature = &plan["layers"][0]; + assert_eq!(feature.get("anonymous").and_then(Value::as_bool).unwrap_or(false), crossed); + let listed = s.run("service.layers", ¶ms).unwrap(); + assert_eq!(listed["layers"][0].get("anonymous").and_then(Value::as_bool).unwrap_or(false), crossed); + params["responses"] = json!([{"url":feature["offer"]["url"], "text":r#"{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":{}}]}"#}]); + s.run("service.import", ¶ms).unwrap(); + let layer = &s.map().layers[0]; + assert_eq!(layer.anonymous, crossed); + assert_eq!(layer.connection.is_empty(), crossed); + assert_eq!(header(layer.dataset.source.as_deref().unwrap()).is_none(), crossed || !authenticated); + assert_eq!(layer.dataset.features.len(), 1); + params["group"] = json!(s.map().service_groups[0].id); + s.run("service.refresh", ¶ms).unwrap(); + let layer = &s.map().layers[0]; + assert_eq!(layer.anonymous, crossed); + assert_eq!(layer.connection.is_empty(), crossed); + assert_eq!(header(layer.dataset.source.as_deref().unwrap()).is_none(), crossed || !authenticated); + let saved = s.project.saved_text(None).unwrap().0; + s.run("project.openText", &json!({"text":saved})).unwrap(); + let layer = &s.map().layers[0]; + assert_eq!(layer.anonymous, crossed); + assert_eq!(layer.connection.is_empty(), crossed); + } +} diff --git a/crates/map/src/source.rs b/crates/map/src/source.rs index 4751f0f182..01fcc8774c 100644 --- a/crates/map/src/source.rs +++ b/crates/map/src/source.rs @@ -1209,7 +1209,7 @@ impl SourceLayer { } else if let Some(d) = duckdb && let Some(p) = &d.database { - out.push((R::DuckDbDatabase, p.as_str(), false, paths::is_local(p))); + out.push((R::DuckDbDatabase, p.as_str(), true, paths::is_local(p))); } if let Some(pg) = postgis { out.push((R::PostgisDatabase, pg.database.as_str(), true, false)); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index b961aca7f3..0e2feffef0 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -4390,12 +4390,15 @@ impl RustGisApp { } /// Refuses answers from a replaced project and reports the retired read without starting follow-up work. - fn service_read_current(&mut self, opened: u64, address: &str) -> bool { + fn service_read_current(&mut self, opened: u64, address: &str, pending: Option<&str>) -> bool { if opened == self.session.opened { return true; } let url = serviceui::address_in_message(address); let why = i18n::fmt("{url}: its project was closed while it was read, so it wasn't added", &[("url", &url)]); + if pending.is_some() && self.dialogs.info.as_deref() == pending { + self.dialogs.info = None; + } self.session.notify(why, false); false } @@ -4409,7 +4412,13 @@ impl RustGisApp { for step in std::mem::take(&mut self.ui.service_steps) { match step { ServiceStep::Tiles { mut resolve, dialog, origin, opened } => { - if !self.service_read_current(opened, &resolve.doc) { + 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()) { continue; } let Some(r) = resolve.slot.lock().unwrap_or_else(|e| e.into_inner()).take() else { @@ -4468,7 +4477,9 @@ impl RustGisApp { self.map.invalidate(); } ServiceStep::Coverage { mut params, slot, dialog, origin, opened } => { - if !self.service_read_current(opened, params.get("url").and_then(Value::as_str).unwrap_or("")) { + 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(""), pending.as_deref()) { continue; } let Some(r) = slot.lock().unwrap_or_else(|e| e.into_inner()).take() else { @@ -5581,11 +5592,54 @@ mod download_service_tests { 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); - assert!(app.session.map().tiles.iter().all(|t| !t.url.contains("RGSYNTH_"))); + 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!(app.dialogs.error.is_none(), "{:?}", app.dialogs.error); } } + #[test] + fn retired_service_steps_clear_only_their_own_pending_dialog() { + for coverage in [false, true] { + for newer in [false, true] { + let mut app = RustGisApp::new(rustgis_engine::Session::new(rustgis_engine::Project::default())); + let opened = app.session.opened; + let params = json!({"url":"https://retired.invalid/service", "name":"Old read"}); + let slot = std::sync::Arc::new(std::sync::Mutex::new(None)); + let step = if coverage { + super::ServiceStep::Coverage { params, slot: slot.clone(), dialog: true, origin: None, opened } + } else { + super::ServiceStep::Tiles { + resolve: super::TilesResolve { + params, + list: "https://retired.invalid/list".into(), + doc: "https://retired.invalid/list".into(), + crossed_origin: false, + read: 0, + withheld_addresses: 0, + vector: false, + fallback: "https://retired.invalid/{z}/{y}/{x}".into(), + slot: slot.clone(), + }, + dialog: true, + origin: None, + opened, + } + }; + app.ui.service_steps.push(step); + app.session.run("project.new", &json!({})).unwrap(); + let pending = if coverage { "Describing Old read" } else { "Reading the tileset of Old read" }; + app.dialogs.info = Some(if newer { "New operation" } else { pending }.into()); + *slot.lock().unwrap() = Some(Ok(b"late completion".to_vec())); + app.poll_service_steps(); + assert!(app.ui.service_steps.is_empty()); + assert!(app.session.map().tiles.is_empty() && app.session.map().layers.is_empty()); + assert!(app.session.notices.iter().any(|n| n.text.contains("its project was closed while it was read"))); + assert_eq!(app.dialogs.info.as_deref(), if newer { Some("New operation") } else { None }); + } + } + } + #[test] fn rejected_download_does_not_announce_success_or_rename_project() { let mut app = RustGisApp::with_sample() diff --git a/crates/ui-egui/src/serviceui.rs b/crates/ui-egui/src/serviceui.rs index 6247ac1f2d..ca641ceb4b 100644 --- a/crates/ui-egui/src/serviceui.rs +++ b/crates/ui-egui/src/serviceui.rs @@ -117,7 +117,13 @@ pub(crate) fn add_layer_from( // The layer's bounds go along, for its features' axes (#1053). let bounds = l.get("bbox").cloned().unwrap_or_default(); let mut params = json!({"url": items, "name": title, "service": true, "bounds": bounds}); - if let Some(connection) = connection { + let anonymous = l.get("anonymous").and_then(Value::as_bool) == Some(true) + || rustgis_engine::render::rustgis_tiles::logins::origin(&src.url) + != rustgis_engine::render::rustgis_tiles::logins::origin(items); + if anonymous { + params["url"] = json!(rustgis_engine::data::login_address::inner(items)); + params["anonymous"] = json!(true); + } else if let Some(connection) = connection { params["connection"] = json!(connection); } // An area asked for keeps the view, even on an empty map, as `service.add` does. @@ -180,6 +186,7 @@ pub(crate) fn add_layer_from( } } +/// Starts grouped discovery under its original request, retaining the import target and byte budget. pub fn start(app: &mut RustGisApp, p: &Value, refresh: bool) -> Result<(), String> { if !p.is_object() { return Err(tl!("Give the service's web address").into()); @@ -230,7 +237,7 @@ pub fn start(app: &mut RustGisApp, p: &Value, refresh: bool) -> Result<(), Strin let url = task.params.get("url").and_then(Value::as_str).unwrap_or_default(); let kind = task.params.get("type").and_then(Value::as_str); let caps = rustgis_engine::data::ogc::capabilities_url(url, kind); - let caps = match task.request(app, &caps) { + let caps = match task.request(&caps) { Ok(caps) => caps, Err(e) => { app.session.download_failed(task.token); @@ -247,14 +254,18 @@ pub fn start(app: &mut RustGisApp, p: &Value, refresh: bool) -> Result<(), Strin impl Import { /// Anonymous service provenance forbids selecting a login, including for same-origin links. - fn request(&self, app: &mut RustGisApp, url: &str) -> Result { + fn request(&self, url: &str) -> Result { if self.params.get("anonymous").and_then(Value::as_bool) == Some(true) { return Ok(rustgis_engine::data::login_address::inner(url).to_string()); } let base = self.params.get("url").and_then(Value::as_str).unwrap_or_default(); - request_from(app, self.connection.as_ref().and_then(Value::as_str), base, url) + // The plan already resolved this exact destination and its login (or absence). + // Re-selecting from the original saved connection would undo a foreign hop. + rustgis_engine::render::rustgis_tiles::address::check(url, Some(base)).map_err(|e| crate::connectionui::translated(&e))?; + Ok(url.to_string()) } + /// Consumes the engine plan verbatim: foreign-hop anonymity cannot reselect a login. fn discover(&mut self, app: &mut RustGisApp) -> Result<(), String> { let listing = app.session.run("service.groupPlan", &self.params)?; if listing.get("request").and_then(|p| p.get("anonymous")).and_then(Value::as_bool) == Some(true) { @@ -265,7 +276,7 @@ impl Import { self.connection = None; } if let Some(next) = listing.get("next").and_then(Value::as_str) { - let next = self.request(app, next)?; + let next = self.request(next)?; self.slot = fetch_into(&next); self.document = Some(next); } else { @@ -276,20 +287,22 @@ impl Import { .flatten() .filter_map(|l| l.get("offer").and_then(|o| o.get("url")).and_then(Value::as_str).map(str::to_string)) .collect(); - self.next(app)?; + self.next()?; } Ok(()) } - fn next(&mut self, app: &mut RustGisApp) -> Result<(), String> { + /// Fetches one exact planned collection within the remaining shared byte budget. + fn next(&mut self) -> Result<(), String> { self.current = self.remaining.pop_front(); if let Some(url) = &self.current { - let url = self.request(app, url)?; + let url = self.request(url)?; self.slot = fetch_into_limited(&url, rustgis_engine::cmd::service::MAX_GROUP_BYTES.saturating_sub(self.bytes)); } Ok(()) } + /// Accumulates supplied documents or feature responses before the engine import/refresh. fn landed(&mut self, app: &mut RustGisApp, bytes: Vec) -> Result<(), String> { if let Some(document) = self.document.take() { let text = crate::service_text(bytes).map_err(|_| tl!("The service's answer isn't UTF-8 text").to_string())?; @@ -309,7 +322,7 @@ impl Import { } let text = String::from_utf8(bytes).map_err(|_| tl!("The service's answer isn't UTF-8 text").to_string())?; self.responses.push(json!({"url":self.current.take(),"text":text})); - self.next(app)?; + self.next()?; Ok(()) } } diff --git a/docs/python.md b/docs/python.md index 52d15915ad..c367c7b86c 100644 --- a/docs/python.md +++ b/docs/python.md @@ -139,9 +139,11 @@ The project, with the data that travels with it, goes to that app through the br (`postMessage`); nothing goes to a server. The app runs in your browser, so with the default address your data stays on your machine. Point `url=` only at a copy of the app you trust. -The widget receives public addresses and cleaned executable parameters. Credentials held by -Python are not sent to it, so protected layers can remain empty there; a warning counts affected -layer addresses. When the widget sends a project back, Python can retain credentials only for +The widget receives public addresses and cleaned executable parameters. Recognized credentials +in supported fields are removed, so protected layers can remain empty there; a warning counts +affected layer addresses. User data, foreign notebook source and opaque parameters are kept +unchanged and may contain credentials: review them before sharing the project with a widget. +When the widget sends a project back, Python can retain credentials only for unchanged, uniquely matched sources from its last exchange that still match its current project. Changing or replacing a source can require signing in again in Python. Exchange notices are available through `m.notices()` on the `Map` or `MapWidget`. diff --git a/docs/user-guide/workspace.md b/docs/user-guide/workspace.md index 3efe327bee..bea32fc553 100644 --- a/docs/user-guide/workspace.md +++ b/docs/user-guide/workspace.md @@ -32,12 +32,12 @@ 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. -Saved layer and video addresses, and recognized task, model, notebook and history parameters, -omit passwords, access keys, tokens and signatures. Notices count what was removed or could not +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 [Sharing projects and generated items](https://github.com/opengeos/RustGIS/blob/main/SECURITY.md#sharing-projects-and-generated-items) -before sharing a file. +before sharing a file. Review addresses before sharing too: an unrecognized credential can remain unchanged. ### Autosave and recovery From d533b89d3a34bff694cf3e2e1b41205a989988d8 Mon Sep 17 00:00:00 2001 From: mavrick Date: Sat, 7 Nov 2026 01:39:06 -0600 Subject: [PATCH 12/14] fix(security): close identifier and generated-output leaks --- crates/assistant/src/audit.rs | 55 ++++-- crates/data/src/address.rs | 14 ++ crates/data/src/diagnostics.rs | 12 ++ crates/engine/src/durable.rs | 50 +++--- crates/engine/src/lib.rs | 23 ++- crates/engine/src/project.rs | 32 ++-- crates/engine/src/project_log.rs | 7 + crates/engine/src/roles.rs | 52 +++++- crates/engine/src/sources.rs | 3 +- crates/engine/src/tests/credential_output.rs | 177 +++++++++++++++++++ crates/engine/src/tests/service_logins.rs | 13 +- crates/tiles/src/cache.rs | 43 +++-- crates/tiles/src/logins.rs | 3 + 13 files changed, 393 insertions(+), 91 deletions(-) diff --git a/crates/assistant/src/audit.rs b/crates/assistant/src/audit.rs index 3f9d40f827..952191ed37 100644 --- a/crates/assistant/src/audit.rs +++ b/crates/assistant/src/audit.rs @@ -150,7 +150,11 @@ fn normalized_args(tool: &str, arguments: &Value) -> Value { }; let key = if tool == "run_command" { "command" } else { "tool" }; let mut out = serde_json::Map::new(); - out.insert(key.into(), redact(arguments.get(key).cloned().unwrap_or(Value::Null), 0)); + let id = match arguments.get(key) { + Some(Value::String(id)) => json!(rustgis_engine::log_identifier(id)), + other => redact(other.cloned().unwrap_or(Value::Null), 0), + }; + out.insert(key.into(), id); out.insert("params".into(), params); Value::Object(out) } @@ -233,15 +237,22 @@ fn redact_script(script: &str) -> String { let output = script .lines() .take(rustgis_engine::cmd::workflows::MAX_SCRIPT_LINES) - .map(|line| match rustgis_engine::script_line(line) { - None => redact_string(line.trim()), - Some(Ok((id, params))) => { - let id = redact_string(&id); - if params.is_null() { id } else { format!("{id} {}", rustgis_engine::log_params(¶ms).into_log_value()) } - } - Some(Err(_)) => { - let id = line.trim().split_once(char::is_whitespace).map_or(line.trim(), |(id, _)| id).trim_end_matches('='); - format!("{} [parameters not JSON; withheld]", redact_string(id)) + .map(|line| { + let line = line.trim(); + // Display only: a malformed identifier may carry JSON glued after `=`. It must + // not be copied as an id; execution still uses the shared parser unchanged. + let head = line.split(|c: char| c.is_whitespace() || c == '=').next().unwrap_or(line); + let glued = line + .get(head.len()..) + .and_then(|r| r.strip_prefix('=')) + .is_some_and(|r| !r.is_empty() && !r.starts_with(char::is_whitespace)); + match rustgis_engine::script_line(line) { + None => redact_string(line), + Some(Ok((id, params))) if !glued => { + let id = rustgis_engine::log_identifier(&id); + if params.is_null() { id } else { format!("{id} {}", rustgis_engine::log_params(¶ms).into_log_value()) } + } + Some(_) => format!("{} [parameters not JSON; withheld]", rustgis_engine::log_identifier(head)), } }) .collect::>() @@ -308,6 +319,30 @@ mod tests { #[test] fn audit_logs_the_executed_envelope_and_shared_script_grammar() { + for line in [ + "sql.connect={\"password\":\"RGSYNTH_glued_audit\"}", + "sql.connect={\"password\":\"RGSYNTH_glued_audit\"} junk", + "sql.connect={\"password\":\"RGSYNTH_glued_audit\"", + ] { + let output = redact_script(line); + assert!(!output.contains("RGSYNTH_glued_audit"), "{output}"); + } + for id in [ + "sql.connect={\"password\":\"RGSYNTH_glued_audit\"}", + "sql.connect {\"password\":\"RGSYNTH_glued_audit\"}", + "sql.connect:{\"password\":\"RGSYNTH_glued_audit\"}", + "sql.connect:password:RGSYNTH_glued_audit", + "sql.connect:token:RGSYNTH_glued_audit", + "password:RGSYNTH_glued_audit", + ] { + for tool in ["run_command", "run_tool"] { + let key = if tool == "run_command" { "command" } else { "tool" }; + let mut args = json!({"params":null}); + args[key] = json!(id); + let logged = normalized_args(tool, &args); + assert!(!logged.to_string().contains("RGSYNTH_glued_audit"), "{tool}: {logged}"); + } + } let params = json!({"url": "https://h.invalid/a?%74oken=RGSYNTH_audit_20261103", "pass%77ord": "RGSYNTH_option_20261103", "layer": "roads"}); for tool in ["run_command", "run_tool"] { diff --git a/crates/data/src/address.rs b/crates/data/src/address.rs index 0eba9a766c..c85d7ea1e3 100644 --- a/crates/data/src/address.rs +++ b/crates/data/src/address.rs @@ -331,6 +331,11 @@ pub fn public_web_address(address: &str) -> Public { if public == text { Public::Unchanged } else { Public::Changed { address: public, removed } } } +/// A bare supported scheme is a prose example with no authority or credential to remove. +pub(crate) fn scheme_example(text: &str) -> bool { + ["http://", "https://", "s3://"].iter().any(|scheme| text.eq_ignore_ascii_case(scheme)) +} + /// Cleans addresses in generated text and counts changed addresses. All spacing and all other /// text stay byte-for-byte. Never apply this to user-authored text or dataset values. pub fn scrub_addresses(text: &str) -> (Cow<'_, str>, u32) { @@ -365,6 +370,10 @@ pub fn scrub_addresses(text: &str) -> (Cow<'_, str>, u32) { .len() .max(scheme_at + 1); let address = word.get(begin..end).unwrap_or(""); + // A bare scheme in diagnostic prose has no address or credentials to remove. + if scheme_example(address) { + continue; + } let public = public_web_address(address); if matches!(public, Public::Unchanged) { continue; @@ -462,6 +471,8 @@ pub struct ReviewedScope { pub directory: &'static str, pub whole_origin: bool, } +/// No deployment has been authorized for full-query inheritance. Keep this list empty until +/// that review exists; the explicit-list tests exercise the same deny boundary. pub const F2_SCOPES: &[ReviewedScope] = &[]; #[derive(Debug, PartialEq, Eq)] @@ -620,6 +631,9 @@ mod tests { ("[s3:bucket/x?token=RGSYNTH_0]", "[s3:]", 1), ("https:x", "https:", 1), ("s3:", "s3:", 1), + ("plain http://, confirm before sending", "plain http://, confirm before sending", 0), + ("use https:// for encryption", "use https:// for encryption", 0), + ("an S3:// address", "an S3:// address", 0), ("read https:/h.invalid/x then https://h.invalid/b?layer=a%20b", "read https: then https://h.invalid/b?layer=a%20b", 1), ( "http://h.invalid/a https://h.invalid/b?layer=a%20b s3://bucket/x", diff --git a/crates/data/src/diagnostics.rs b/crates/data/src/diagnostics.rs index 9734088c96..7da31f998e 100644 --- a/crates/data/src/diagnostics.rs +++ b/crates/data/src/diagnostics.rs @@ -639,6 +639,12 @@ pub fn redact(text: &str) -> String { if after.starts_with("://") { let url_end = rest.find(|c: char| c.is_whitespace() || matches!(c, '"' | '\'' | '<' | '>' | '`')).unwrap_or(rest.len()); let address = rest.get(..url_end).unwrap_or(""); + if crate::address::scheme_example(address.trim_end_matches([':', ',', ';', '.', ')', ']'])) { + out.push_str(address); + i += url_end; + field_before = false; + continue; + } match url::Url::parse(address) { // Nothing to take out: the address stays as written, its punctuation too // ("https://example.org: timeout"), unless it holds control characters. @@ -1148,6 +1154,12 @@ mod tests { fn addresses_without_anything_to_take_out_stay_as_written() { assert_eq!(redact("Could not reach https://example.org: timeout"), "Could not reach https://example.org: timeout"); assert_eq!(redact("failed https://example.org/a/b, retrying"), "failed https://example.org/a/b, retrying"); + for text in ["plain http://, confirm before sending", "use https:// for encryption", "an S3:// address"] { + assert_eq!(redact(text), text); + } + for address in ["https://?token=RGSYNTH_example", "s3://#RGSYNTH_example", "https://u:RGSYNTH_example@"] { + assert!(!redact(address).contains("RGSYNTH_example"), "an incomplete address with content is still withheld"); + } } /// Taking user information out rewrites the address, and the punctuation that ended it in the diff --git a/crates/engine/src/durable.rs b/crates/engine/src/durable.rs index e1e4b88b12..939022141e 100644 --- a/crates/engine/src/durable.rs +++ b/crates/engine/src/durable.rs @@ -113,11 +113,15 @@ pub(crate) enum Leaf { Layer(rustgis_map::LayerRole), Table, Task(usize), + TaskCommand(usize), ModelStep(usize), + ModelStepTool(usize), ModelNode(usize), - ModelLayer(usize), + ModelNodeTool(usize), HistoryParams, + HistoryId, HistoryCommand, + HistoryCommandId, Message(usize), CellSource(usize), CellOutput(usize), @@ -201,10 +205,8 @@ fn json_bytes(v: &Value) -> usize { pub(crate) trait Durable { /// Preparation is read-only, exhaustive over owning fields, and offers each leaf once. - /// Offers classified whole fields once, before any live mutation. fn prepare(&self, p: &mut Prepare<'_>); /// Resolves recorded indices only, through plain owned fields, without allocation or policy. - /// Resolves a prepared location through owned fields, without allocation or policy. fn field_mut(&mut self, owners: &[Owner], leaf: &Leaf) -> Option>; } @@ -290,32 +292,33 @@ impl<'a> Prepare<'a> { } } - /// Prepares a command's replayable parameters using explicit data/configuration roles. - pub(crate) fn command(&mut self, leaf: Leaf, id: &str, value: &Value) { - let (clean, report) = crate::roles::command(id, value, self.depth); - let clean = match clean { - Some(clean) => Some(crate::project::public_login_params(&clean).unwrap_or(clean)), - None => crate::project::public_login_params(value), - }; - self.report.merge(&report); - if let Some(clean) = clean { - self.report.steps = self.report.steps.saturating_add(1); - self.edit(leaf, Payload::Json(clean), Kind::Credential); - } + /// Prepares both fields of one replayable command, counting the owning step once. + pub(crate) fn command(&mut self, leaf: Leaf, id_leaf: Leaf, id: &str, value: &Value) { + self.executable((leaf, id_leaf), id, value, crate::roles::command(id, value, self.depth)); + } + + /// Model and history tool params use the same common roles as `gp.run` params. + pub(crate) fn tool(&mut self, leaf: Leaf, id_leaf: Leaf, id: &str, value: &Value) { + self.executable((leaf, id_leaf), id, value, crate::roles::tool(id, value, self.depth)); } - /// Model and history tool params have the same roles as the corresponding `gp.run` params. - pub(crate) fn tool(&mut self, leaf: Leaf, id: &str, value: &Value) { - let (clean, report) = crate::roles::tool(id, value, self.depth); + /// Journals the identifier and params independently while reporting their shared owner. + fn executable(&mut self, (leaf, id_leaf): (Leaf, Leaf), id: &str, value: &Value, (clean, report): (Option, Report)) { let clean = match clean { Some(clean) => Some(crate::project::public_login_params(&clean).unwrap_or(clean)), None => crate::project::public_login_params(value), }; self.report.merge(&report); - if let Some(clean) = clean { + let clean_id = crate::roles::identifier(id, &mut self.report); + if clean.is_some() || clean_id.is_some() { self.report.steps = self.report.steps.saturating_add(1); + } + if let Some(clean) = clean { self.edit(leaf, Payload::Json(clean), Kind::Credential); } + if let Some(clean) = clean_id { + self.edit(id_leaf, Payload::Text(clean), Kind::Credential); + } } /// Rewrites only changed command lines; comments, data and foreign-language cells stay exact. @@ -574,7 +577,8 @@ fn public_address_at(address: &str, depth: usize) -> Kept { address::{Public, public_web_address}, s3_address, }; - if let Some((public, _)) = rustgis_data::source::myconn::split_password(address) { + if let Some((mut public, _)) = rustgis_data::source::myconn::split_password(address) { + rustgis_data::address::redact_query(&mut public); return if public == address { Kept::Unchanged } else { Kept::Changed(public) }; } if let Some((public, _)) = rustgis_data::source::msconn::split_password(address) { @@ -683,11 +687,15 @@ pub(crate) fn changed_executables(old: &mut crate::Project, incoming: &mut crate || !matches!( edit.at.leaf, Leaf::Task(_) + | Leaf::TaskCommand(_) | Leaf::ModelStep(_) + | Leaf::ModelStepTool(_) | Leaf::ModelNode(_) - | Leaf::ModelLayer(_) + | Leaf::ModelNodeTool(_) | Leaf::HistoryParams + | Leaf::HistoryId | Leaf::HistoryCommand + | Leaf::HistoryCommandId | Leaf::CellSource(_) ) { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 0ed79a97e5..7941d0f85f 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -20,7 +20,7 @@ pub(crate) use roles::{Document, ParamRole, Role}; mod parameters; pub mod project_log; pub use parameters::{params_envelope, script_line}; -pub use project_log::{LogCopy, log_params}; +pub use project_log::{LogCopy, log_identifier, log_params}; pub mod s3_connections; pub mod s3_keys; #[cfg(test)] @@ -1466,7 +1466,12 @@ impl Session { } } + /// Cleans every returned error, including early refusal and plugin paths. fn run_command(&mut self, id: &str, params: &Value) -> CmdResult { + self.run_command_inner(id, params).map_err(|e| durable::generated_text(&e)) + } + + fn run_command_inner(&mut self, id: &str, params: &Value) -> CmdResult { // The merge key asked for ([`Session::merge_undo`]) goes to this command if it runs, and is // gone if it's refused first, so it can't merge a later command into the last step. let merge = if self.depth == 0 { self.merge.take() } else { None }; @@ -1478,7 +1483,7 @@ impl Session { self.merge = merge; return self.run_plugin(id, params); } - let Some(&spec) = self.registry.get(id) else { return Err(format!("unknown command '{id}'")) }; + 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. if let (Value::Object(given), Some(known)) = (params, spec.param_keys()) { // Bounded: the keys come from the caller (the first few, each clipped). @@ -1545,8 +1550,7 @@ impl Session { { self.sources.note(src); } - // An error never quotes a web-service connection's password or key. - res.map_err(|e| render::rustgis_tiles::logins::scrub(&e)) + res } /// Retires only this consumed reservation, preserving unrelated pending imports in history. @@ -2026,7 +2030,7 @@ impl Session { /// redacted: what a notice or history record may show. Login values are known only here, so /// a failure redacted by [`Session::notify_redacted`]'s callers goes through this first. pub fn redacted(text: &str) -> rustgis_data::diagnostics::Redacted { - rustgis_data::diagnostics::Redacted::new(&render::rustgis_tiles::logins::scrub(text)) + rustgis_data::diagnostics::Redacted::new(&durable::generated_text(text)) } /// [`Session::notify`] for text already scrubbed and redacted by [`Session::redacted`] (a @@ -2521,9 +2525,9 @@ impl durable::Durable for GpRecord { use durable::Leaf; let Self { tool, id, params, command, at: _, ok: _, messages, millis: _, outputs: _ } = self; let tool = if id.is_empty() { cmd::gp::tool_id(tool).unwrap_or("") } else { id }; - p.tool(Leaf::HistoryParams, tool, params); + p.tool(Leaf::HistoryParams, Leaf::HistoryId, tool, params); if let Some((id, params)) = command { - p.command(Leaf::HistoryCommand, id, params); + p.command(Leaf::HistoryCommand, Leaf::HistoryCommandId, id, params); } for (i, message) in messages.iter().enumerate() { p.generated(Leaf::Message(i), message); @@ -2536,7 +2540,9 @@ impl durable::Durable for GpRecord { } match leaf { Leaf::HistoryParams => Some(FieldMut::Json(&mut self.params)), + Leaf::HistoryId => Some(FieldMut::Text(&mut self.id)), Leaf::HistoryCommand => Some(FieldMut::Json(&mut self.command.as_mut()?.1)), + Leaf::HistoryCommandId => Some(FieldMut::Text(&mut self.command.as_mut()?.0)), Leaf::Message(i) => self.messages.get_mut(*i).map(FieldMut::Text), _ => None, } @@ -2547,8 +2553,7 @@ impl Session { /// The single history production seam: retained generated messages never keep address keys. pub(crate) fn push_gp_record(&mut self, mut record: GpRecord) { for message in &mut record.messages { - let scrubbed = render::rustgis_tiles::logins::scrub(message); - *message = rustgis_data::address::scrub_addresses(&scrubbed).0.into_owned(); + *message = durable::generated_text(message); } self.project.gp_history.push(record); } diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 08bb586e2d..8c7a4c5dae 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -650,10 +650,7 @@ impl Project { if let Some(graph) = &mut model.graph { for node in &mut graph.nodes { recorded_addresses(&mut node.params, &mut addresses); - // A node's input may be an address rather than a layer's name. - if let Some(layer) = &mut node.layer { - addresses.push(layer); - } + // node.layer selects a name, including names that resemble runtime URLs. } } } @@ -1287,7 +1284,7 @@ impl Durable for Task { fn prepare(&self, p: &mut Prepare<'_>) { let Self { name: _, description: _, steps } = self; for (i, TaskStep { title: _, instructions: _, command, params }) in steps.iter().enumerate() { - p.command(Leaf::Task(i), command, params); + p.command(Leaf::Task(i), Leaf::TaskCommand(i), command, params); } } @@ -1296,8 +1293,11 @@ impl Durable for Task { if !owners.is_empty() { return None; } - let Leaf::Task(i) = leaf else { return None }; - Some(FieldMut::Json(&mut self.steps.get_mut(*i)?.params)) + match leaf { + Leaf::Task(i) => Some(FieldMut::Json(&mut self.steps.get_mut(*i)?.params)), + Leaf::TaskCommand(i) => Some(FieldMut::Text(&mut self.steps.get_mut(*i)?.command)), + _ => None, + } } } @@ -1306,7 +1306,7 @@ impl Durable for Model { fn prepare(&self, p: &mut Prepare<'_>) { let Self { name: _, steps, graph } = self; for (i, ModelStep { tool, params }) in steps.iter().enumerate() { - p.tool(Leaf::ModelStep(i), tool, params); + p.tool(Leaf::ModelStep(i), Leaf::ModelStepTool(i), tool, params); } if let Some(g) = graph { p.child(Owner::Graph, g); @@ -1321,8 +1321,11 @@ impl Durable for Model { if !owners.is_empty() { return None; } - let Leaf::ModelStep(i) = leaf else { return None }; - Some(FieldMut::Json(&mut self.steps.get_mut(*i)?.params)) + match leaf { + Leaf::ModelStep(i) => Some(FieldMut::Json(&mut self.steps.get_mut(*i)?.params)), + Leaf::ModelStepTool(i) => Some(FieldMut::Text(&mut self.steps.get_mut(*i)?.tool)), + _ => None, + } } } @@ -1330,11 +1333,8 @@ impl Durable for ModelGraph { /// Offers classified whole fields once, before any live mutation. fn prepare(&self, p: &mut Prepare<'_>) { let Self { nodes, edges: _ } = self; - for (i, ModelNode { id: _, kind: _, x: _, y: _, layer, tool, params, name: _ }) in nodes.iter().enumerate() { - p.tool(Leaf::ModelNode(i), tool.as_deref().unwrap_or(""), params); - if let Some(layer) = layer { - p.string(Leaf::ModelLayer(i), layer, true, false); - } + for (i, ModelNode { id: _, kind: _, x: _, y: _, layer: _, tool, params, name: _ }) in nodes.iter().enumerate() { + p.tool(Leaf::ModelNode(i), Leaf::ModelNodeTool(i), tool.as_deref().unwrap_or(""), params); } } @@ -1345,7 +1345,7 @@ impl Durable for ModelGraph { } match leaf { Leaf::ModelNode(i) => Some(FieldMut::Json(&mut self.nodes.get_mut(*i)?.params)), - Leaf::ModelLayer(i) => self.nodes.get_mut(*i)?.layer.as_mut().map(FieldMut::Text), + Leaf::ModelNodeTool(i) => self.nodes.get_mut(*i)?.tool.as_mut().map(FieldMut::Text), _ => None, } } diff --git a/crates/engine/src/project_log.rs b/crates/engine/src/project_log.rs index 7fbead77f4..e1114e025e 100644 --- a/crates/engine/src/project_log.rs +++ b/crates/engine/src/project_log.rs @@ -22,6 +22,13 @@ pub fn log_params(params: &Value) -> LogCopy { LogCopy(kept_params(params)) } +/// Cleans a structural command/tool identifier for display and audit output only. +/// The original identifier remains the sole input to lookup, execution and approval. +pub fn log_identifier(id: &str) -> String { + let public = crate::roles::identifier(id, &mut crate::durable::Report::default()); + crate::durable::generated_text(public.as_deref().unwrap_or(id)) +} + /// Now, in milliseconds since 1970 (UTC). pub(crate) fn now_ms() -> u64 { web_time::SystemTime::now().duration_since(web_time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0) diff --git a/crates/engine/src/roles.rs b/crates/engine/src/roles.rs index 3d7c069fa8..cc38976289 100644 --- a/crates/engine/src/roles.rs +++ b/crates/engine/src/roles.rs @@ -119,6 +119,31 @@ pub(crate) fn tool(id: &str, params: &Value, depth: usize) -> (Option, Re (changed, report) } +/// Identifiers are structural executable fields, not layer names or user data. Recognized +/// addresses are cleaned; credential-bearing malformed payloads are withheld as a whole. +pub(crate) fn identifier(id: &str, report: &mut Report) -> Option { + // A colon-separated credential key/value is payload; a qualified plugin/tool id alone is not. + let colon_value = id + .split(':') + .zip(id.split(':').skip(1)) + .take(65_536) + .any(|(key, value)| !value.is_empty() && rustgis_data::address::secret_key(key)); + match durable::public_address(id) { + Kept::Changed(public) => Some(public), + Kept::Withheld(prefix) if prefix != id => { + report.withheld = report.withheld.saturating_add(1); + Some(prefix.into()) + } + _ if (colon_value || id.contains(['=', '{', '[', '"']) || id.contains(char::is_whitespace)) + && suspicious(&Value::String(id.into())) => + { + report.withheld = report.withheld.saturating_add(1); + Some("[command withheld]".into()) + } + _ => None, + } +} + /// Lazily copies only changed owning parameter objects. Configuration recursion is bounded. fn object(id: &str, value: &Value, depth: usize, config_depth: usize, registered: bool, report: &mut Report) -> Option { if value.is_null() { @@ -306,7 +331,7 @@ impl Durable for VecTask { /// Offers classified whole fields once, before any live mutation. fn prepare(&self, p: &mut durable::Prepare<'_>) { for (i, st) in self.0.iter().enumerate() { - p.command(durable::Leaf::Task(i), &st.command, &st.params); + p.command(durable::Leaf::Task(i), durable::Leaf::TaskCommand(i), &st.command, &st.params); } } @@ -315,8 +340,11 @@ impl Durable for VecTask { if !owners.is_empty() { return None; } - let durable::Leaf::Task(i) = leaf else { return None }; - Some(durable::FieldMut::Json(&mut self.0.get_mut(*i)?.params)) + match leaf { + durable::Leaf::Task(i) => Some(durable::FieldMut::Json(&mut self.0.get_mut(*i)?.params)), + durable::Leaf::TaskCommand(i) => Some(durable::FieldMut::Text(&mut self.0.get_mut(*i)?.command)), + _ => None, + } } } @@ -328,7 +356,7 @@ impl Durable for VecSteps { /// Offers classified whole fields once, before any live mutation. fn prepare(&self, p: &mut durable::Prepare<'_>) { for (i, st) in self.0.iter().enumerate() { - p.tool(durable::Leaf::ModelStep(i), &st.tool, &st.params); + p.tool(durable::Leaf::ModelStep(i), durable::Leaf::ModelStepTool(i), &st.tool, &st.params); } } @@ -337,8 +365,11 @@ impl Durable for VecSteps { if !owners.is_empty() { return None; } - let durable::Leaf::ModelStep(i) = leaf else { return None }; - Some(durable::FieldMut::Json(&mut self.0.get_mut(*i)?.params)) + match leaf { + durable::Leaf::ModelStep(i) => Some(durable::FieldMut::Json(&mut self.0.get_mut(*i)?.params)), + durable::Leaf::ModelStepTool(i) => Some(durable::FieldMut::Text(&mut self.0.get_mut(*i)?.tool)), + _ => None, + } } } @@ -418,14 +449,19 @@ pub(crate) fn script(source: &str, depth: usize) -> (Option, Report) { let replacement = match crate::script_line(line) { None => None, Some(Ok((id, params))) => { + let public_id = identifier(&id, &mut report); let (clean, r) = command(&id, ¶ms, depth); report.merge(&r); - clean.map(|params| format!("{id} {params}{ending}")) + match clean { + Some(params) => Some(format!("{} {params}{ending}", public_id.as_deref().unwrap_or(&id))), + None => public_id.map(|public| line.replacen(&id, &public, 1)), + } } Some(Err(_)) if suspicious(&Value::String(line.into())) => { report.withheld = report.withheld.saturating_add(1); let id = line.trim_start().split(char::is_whitespace).next().unwrap_or(""); - Some(format!("{id} {WITHHELD}{ending}")) + let public_id = identifier(id, &mut report); + Some(format!("{} {WITHHELD}{ending}", public_id.as_deref().unwrap_or(id))) } Some(Err(_)) => None, }; diff --git a/crates/engine/src/sources.rs b/crates/engine/src/sources.rs index a196e295d1..e8315d6e69 100644 --- a/crates/engine/src/sources.rs +++ b/crates/engine/src/sources.rs @@ -365,7 +365,8 @@ pub fn redact(address: &str) -> String { { return address.to_string(); } - if let Some((public, _)) = rustgis_data::source::myconn::split_password(address) { + if let Some((mut public, _)) = rustgis_data::source::myconn::split_password(address) { + rustgis_data::address::redact_query(&mut public); return public; } if let Some((public, _)) = rustgis_data::source::msconn::split_password(address) { diff --git a/crates/engine/src/tests/credential_output.rs b/crates/engine/src/tests/credential_output.rs index 76f5c7bba2..da66e7789e 100644 --- a/crates/engine/src/tests/credential_output.rs +++ b/crates/engine/src/tests/credential_output.rs @@ -9,6 +9,183 @@ use rustgis_map::presentation::{Page, PageKind, Presentation}; const SECRET: &str = "RGSYNTH_output_20261103"; const DATA: &str = "RGSYNTH_data_20261103"; +/// Database labels and durable addresses share query-key classification, without changing +/// the executable connection string or dropping clean connection options. +#[test] +fn durable_database_query_aliases_match_public_labels() { + for scheme in ["mysql", "mariadb", "mssql", "postgresql"] { + for key in ["access_token", "subscription-key", "api_key"] { + let public = format!("{scheme}://reader@db.invalid/gis?ssl=true"); + let private = format!("{public}&{key}={SECRET}"); + let kept = match durable::public_address(&private) { + durable::Kept::Changed(s) => s, + _ => private.clone(), + }; + assert_eq!(kept, public, "durable {scheme}/{key}"); + assert_eq!(crate::sources::redact(&private), public, "public label {scheme}/{key}"); + assert!(matches!(durable::public_address(&public), durable::Kept::Unchanged)); + assert_eq!(crate::sources::redact(&public), public); + } + } +} + +/// Graph input fields select existing names; source-address cleaning must not rename those +/// references when saved text is opened without this process's credential restore memory. +#[test] +fn durable_model_input_names_survive_a_cold_round_trip() { + let bound = rustgis_data::login_address::bind("0123456789abcdef", "https://example.invalid", "https://example.invalid/roads").unwrap(); + for name in ["https://example.invalid/roads#north", bound.as_str()] { + let mut s = world(); + s.map_mut().layers[0].name = name.into(); + let graph = ModelGraph { + nodes: vec![ModelNode { + id: "input1".into(), + kind: crate::project::ModelNodeKind::Input, + layer: Some(name.into()), + ..Default::default() + }], + edges: Vec::new(), + }; + s.project.models.push(Model { name: "Names".into(), steps: Vec::new(), graph: Some(graph) }); + let (text, _) = s.project.saved_text(None).unwrap(); + for cold in [Project::from_json(&text).unwrap(), Project::from_import_json(&text, None).unwrap()] { + let model = cold.models.last().unwrap().graph.as_ref().unwrap(); + let selector = model.nodes[0].layer.as_deref().unwrap(); + assert_eq!(selector, name, "a graph input selects a name, even when it resembles an address or runtime wrapper"); + assert!(cold.map().layer_by_name(selector).is_some(), "the opened graph still resolves its real input"); + } + } +} + +/// Address parsing must precede active-pattern masking at every generated history/error +/// boundary, even when the active key is part of the URL scheme itself. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn generated_errors_and_history_clean_addresses_before_active_patterns() { + if isolate_process_test("tests::credential_output::generated_errors_and_history_clean_addresses_before_active_patterns") { + return; + } + use crate::render::rustgis_tiles::logins; + let binding = logins::activate( + "https://history-collision.invalid", + "synthetic-history-collision", + logins::Method::Header("X-Key".into()), + "https", + false, + ) + .unwrap(); + let raw = format!("failed at https://h.invalid/a?token={SECRET}&layer=roads"); + let mut s = world(); + s.push_gp_record(GpRecord { + tool: "Unknown".into(), + id: "unknown".into(), + params: Value::Null, + command: None, + at: 0, + ok: false, + messages: vec![raw.clone()], + millis: 0, + outputs: Vec::new(), + }); + s.notify(&raw, true); + let error = s.run("layer.rename", &json!({"layer":format!("https://h.invalid/a?token={SECRET}"),"name":"unused"})).unwrap_err(); + let history = s.project.gp_history.last().unwrap().messages[0].clone(); + let notice = s.notices.last().unwrap().text.clone(); + assert!(logins::deactivate(&binding)); + assert!(!history.contains(SECRET), "history: {history}"); + assert!(!notice.contains(SECRET), "notice: {notice}"); + assert!(!error.contains(SECRET), "command error: {error}"); + for id in [ + format!("https://h.invalid/a?token={SECRET}"), + format!("sql.connect={{\"password\":\"{SECRET}\"}}"), + format!("sql.connect {{\"password\":\"{SECRET}\"}}"), + ] { + let error = s.run(&id, &Value::Null).unwrap_err(); + assert!(!error.contains(SECRET), "unknown command error: {error}"); + } + assert!(!s.project.saved_text(None).unwrap().0.contains(SECRET)); +} + +/// Invalid command/tool identifiers are executable slots too: an address there must not +/// bypass cleaning through a null parameter or a malformed script line. +#[test] +fn durable_identifier_addresses_are_cleaned_and_counted() { + let url = format!("https://identifier.invalid/a?token={SECRET}&layer=roads"); + let source = format!( + "# clean comment\r\n{url}\r\n{url} {{broken\nsql.connect={{\"password\":\"{SECRET}\"}}\nsql.connect={{\"password\":\"{SECRET}\"}} junk\nmap.zoomIn {{}}\n" + ); + let mut project = Project::default(); + project.notebooks.push(Notebook { + name: "Identifiers".into(), + jupyter: None, + cells: vec![Cell { kind: CellKind::Code, source, output: None, error: false, jupyter: None }], + }); + project.tasks.push(Task { + name: "Identifiers".into(), + description: String::new(), + steps: [ + url.clone(), + format!("sql.connect {{\"password\":\"{SECRET}\"}}"), + format!("sql.connect:{{\"password\":\"{SECRET}\"}}"), + format!("sql.connect:password:{SECRET}"), + format!("sql.connect:token:{SECRET}"), + ] + .into_iter() + .map(|command| TaskStep { title: String::new(), instructions: String::new(), command, params: Value::Null }) + .collect(), + }); + project.models.push(Model { + name: "Identifiers".into(), + steps: vec![ModelStep { tool: url.clone(), params: Value::Null }], + graph: Some(ModelGraph { + nodes: vec![ModelNode { id: "tool1".into(), tool: Some(url.clone()), ..Default::default() }], + edges: Vec::new(), + }), + }); + let mut s = Session::new(project); + s.project.gp_history.push(GpRecord { + tool: "Unknown".into(), + id: url.clone(), + params: Value::Null, + command: Some((url, Value::Null)), + at: 0, + ok: false, + messages: Vec::new(), + millis: 0, + outputs: Vec::new(), + }); + let original = serde_json::to_value(&s.project).unwrap(); + let (text, report) = s.project.saved_text(None).unwrap(); + assert!(!text.contains(SECRET), "an identifier bypassed durable projection: {text}"); + assert!(report.lines >= 2, "{report:?}"); + assert_eq!(report.steps, 9, "each changed executable is counted once"); + assert_eq!(serde_json::to_value(&s.project).unwrap(), original, "live identifiers are restored exactly"); + let (clean, report) = crate::roles::script( + "# clean comment\r\nmap.zoomIn {}\r\nmap.zoomOut\nplugin:token-tools/read\nplugin:demo/token\ngeolibre:token\n", + 0, + ); + assert!(clean.is_none() && report.notices().is_empty(), "ordinary ids and formatting stay exact"); +} + +/// Cleaning both fields of one operation still affects one executable step. +#[test] +fn durable_executable_count_is_per_step() { + let mut project = Project::default(); + project.tasks.push(Task { + name: "One step".into(), + description: String::new(), + steps: vec![TaskStep { + title: String::new(), + instructions: String::new(), + command: format!("https://identifier.invalid/?token={SECRET}"), + params: json!({"password":SECRET}), + }], + }); + let (text, report) = project.saved_text(None).unwrap(); + assert!(!text.contains(SECRET)); + assert_eq!(report.steps, 1, "identifier and params belong to the same step: {report:?}"); +} + /// Inline synthetic state exercises owning fields without contacting any service. fn marked() -> Session { let mut s = world(); diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index 784d6b1888..3d96572550 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -1279,16 +1279,21 @@ fn templates_tasks_and_models_written_out_keep_no_login_binding() { let tile = bound(&mut s, &id, "https://export757.example.org/tiles/{z}/{x}/{y}.png"); s.run("map.addTiles", &json!({"url": tile, "name": "Bound"})).unwrap(); s.run("task.new", &json!({"name":"Fetch","steps":[{"title":"Add","command":"map.addTiles","params":{"url": tile}}]})).unwrap(); - s.run("model.new", &json!({"name":"Tiles","steps":[{"tool":"buffer","params":{"input": tile, "distance": 1}}]})).unwrap(); - // A model drawn as a graph keeps its input's address in the node itself. - let graph = json!({"nodes":[{"id":"in","kind":"input","layer": tile}], "edges": []}); + // Keep the synthetic address in a tool's parameters, not a layer-name selector. + s.run("model.new", &json!({"name":"Tiles","graph":{"nodes":[{"id":"tool1","kind":"tool","tool":"buffer","params":{"input": tile,"distance":1}}],"edges":[]}})).unwrap(); + // A graph input selects an existing layer by name; its actual source fields are cleaned. + let graph = json!({"nodes":[{"id":"in","kind":"input","layer": "Cities"}], "edges": []}); s.run("model.new", &json!({"name":"Drawn","graph": graph})).unwrap(); let dir = std::env::temp_dir().join(format!("export757-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); let (template, task) = (dir.join("bound.rgistemplate"), dir.join("bound.rgistask")); s.run("project.saveTemplate", &json!({"path": template.to_string_lossy()})).unwrap(); s.run("task.save", &json!({"task":"Fetch","path": task.to_string_lossy()})).unwrap(); - let model = s.run("model.export", &json!({"model":"Tiles"})).unwrap()["text"].as_str().unwrap().to_string(); + let model = s.run("model.export", &json!({"name":"Tiles"})).unwrap()["text"].as_str().unwrap().to_string(); + let drawn = s.run("model.export", &json!({"name":"Drawn"})).unwrap()["text"].as_str().unwrap().to_string(); + let graph: Value = serde_json::from_str(&drawn).unwrap(); + assert_eq!(graph["graph"]["nodes"][0]["layer"], "Cities"); + assert!(!drawn.contains(rustgis_data::login_address::PREFIX) && !drawn.contains("export-sentinel-757")); let written = [std::fs::read_to_string(&template).unwrap(), std::fs::read_to_string(&task).unwrap(), model]; let _ = std::fs::remove_dir_all(&dir); for text in &written { diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index 80e3133b6c..763f9b455e 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -267,13 +267,12 @@ pub fn generation() -> u64 { let now = alarm_at(Instant::now()); let next = { let mut a = alarms().lock().unwrap_or_else(|e| e.into_inner()); - if a.first().is_some_and(|at| *at <= now) { + let (over, next) = take_due(&mut a, now); + if over { // Every wait over is one draw: the generation goes up once for them all. - let later = a.split_off(&now.saturating_add(1)); - *a = later; GENERATION.fetch_add(1, Ordering::Relaxed); } - a.first().copied() + next }; if let Some(at) = next { ring(Duration::from_millis(at.saturating_sub(now))); @@ -281,6 +280,16 @@ pub fn generation() -> u64 { GENERATION.load(Ordering::Relaxed) } +/// Consumes every elapsed deadline together and returns the next one to rearm. Keeping the +/// clock as an input lets the ordering contract be tested without scheduler deadlines. +fn take_due(alarms: &mut std::collections::BTreeSet, now: u64) -> (bool, Option) { + let over = alarms.first().is_some_and(|at| *at <= now); + if over { + *alarms = alarms.split_off(&now.saturating_add(1)); + } + (over, alarms.first().copied()) +} + /// How long until the earliest wait pending; none when there is none. #[cfg(test)] pub(crate) fn next_alarm() -> Option { @@ -2532,24 +2541,14 @@ mod tests { /// its elevation source waits), so the later is kept, and the app asked for it then. #[test] fn a_later_wait_isnt_lost_behind_an_earlier_one() { - let _alarm = super::alarm_turn(); - let asked = std::sync::Arc::new(Mutex::new(Vec::new())); - let noted = asked.clone(); - super::set_alarm(move |after| noted.lock().unwrap_or_else(|e| e.into_inner()).push(after)); - super::clear_alarms(); - let before = generation(); - notify_at(Instant::now() + Duration::from_millis(20)); - notify_at(Instant::now() + Duration::from_millis(150)); - std::thread::sleep(std::time::Duration::from_millis(50)); - let earlier = generation(); - assert!(earlier > before, "the earlier wait over"); - // The later is still pending, and asked of the app as the earlier was over. - assert!(super::next_alarm().is_some_and(|d| d <= Duration::from_millis(150)), "{:?}", super::next_alarm()); - let later = |asked: &Vec| asked.iter().any(|d| (Duration::from_millis(30)..=Duration::from_millis(150)).contains(d)); - assert!(later(&asked.lock().unwrap_or_else(|e| e.into_inner())), "{:?}", asked.lock().unwrap_or_else(|e| e.into_inner())); - std::thread::sleep(std::time::Duration::from_millis(150)); - assert!(generation() > earlier, "the later wait over"); - super::clear_alarm(); + let mut alarms = std::collections::BTreeSet::from([20, 150]); + assert_eq!(super::take_due(&mut alarms, 10), (false, Some(20))); + assert_eq!(super::take_due(&mut alarms, 50), (true, Some(150)), "rearm the later wait after the first expires"); + assert_eq!(super::take_due(&mut alarms, 100), (false, Some(150))); + assert_eq!(super::take_due(&mut alarms, 200), (true, None)); + assert_eq!(super::take_due(&mut alarms, 300), (false, None), "expired deadlines are consumed once"); + let mut delayed = std::collections::BTreeSet::from([20, 150]); + assert_eq!(super::take_due(&mut delayed, 200), (true, None), "a delayed poll correctly consumes both waits"); } /// A draw before a wait is over asks the app for it again: the app's request lasts one frame, diff --git a/crates/tiles/src/logins.rs b/crates/tiles/src/logins.rs index 420d3a26ea..aaaaa99991 100644 --- a/crates/tiles/src/logins.rs +++ b/crates/tiles/src/logins.rs @@ -277,6 +277,9 @@ fn append_percent_encoded(result: &mut String, value: &str) { /// prevents overlapping values or a credential resembling `[redacted]` from leaking secrets. pub fn scrub(text: &str) -> String { let entries = entries(); + if entries.is_empty() && !text.contains(rustgis_data::login_address::PREFIX) { + return text.to_string(); + } let mut patterns: Vec<_> = entries.values().flat_map(|entry| &entry.redactions).collect(); patterns.sort_unstable_by_key(|pattern| std::cmp::Reverse(pattern.value.expose().len())); // Strip capabilities even after revocation, when there are no active credential patterns. From a00df9112b4ef21e3d5b4905a4c45428e65a6a1c Mon Sep 17 00:00:00 2001 From: mavrick Date: Sat, 7 Nov 2026 03:18:47 -0600 Subject: [PATCH 13/14] fix(security): redact credentials from returned errors --- apps/rustgis-web/src/web.rs | 6 +-- crates/assistant/src/mcp.rs | 2 + crates/assistant/src/tests.rs | 12 +++++ crates/assistant/src/tools.rs | 84 +++++++++++++++++++++++++++++++++-- crates/tiles/src/cache.rs | 38 ++++++++++++---- crates/tiles/src/cog.rs | 2 +- crates/tiles/src/failure.rs | 6 ++- crates/tiles/src/logins.rs | 13 ++++++ crates/tiles/src/pmtiles.rs | 2 +- 9 files changed, 146 insertions(+), 19 deletions(-) diff --git a/apps/rustgis-web/src/web.rs b/apps/rustgis-web/src/web.rs index 0245bfb57d..7d639e163f 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -756,7 +756,7 @@ async fn fetch_bytes(url: &str, capture: Capture, omit: &'static [u16]) -> Resul } .await; logins::check(bound).map_err(str::to_string)?; - result.map_err(|error| if login.is_some() { logins::scrub(&format!("{url}: {error}")) } else { error }) + result.map_err(|error| if login.is_some() { logins::scrub_error(&format!("{url}: {error}")) } else { error }) } .await; diagnostics.record(answer.map_err(|e| Failure::new(url, e))).map_err(|failure| failure.message) @@ -820,7 +820,7 @@ async fn post_json(url: &str, body: &str, capture: Capture) -> Result, S } .await; logins::check(url).map_err(str::to_string)?; - result.map_err(|error| if login.is_some() { logins::scrub(&format!("{url}: {error}")) } else { error }) + result.map_err(|error| if login.is_some() { logins::scrub_error(&format!("{url}: {error}")) } else { error }) } .await; diagnostics.record(answer.map_err(|e| Failure::new(url, e))).map_err(|failure| failure.message) @@ -889,7 +889,7 @@ async fn head(url: &str, capture: Capture) -> Result bool { } } +/// Projects generated RPC error text while retaining the exact correlation id and code. fn error_value(id: &Value, code: i64, message: &str) -> Value { + let message = rustgis_engine::render::rustgis_tiles::logins::scrub_error(message); json!({"jsonrpc": "2.0", "id": id, "error": {"code": code, "message": message}}) } diff --git a/crates/assistant/src/tests.rs b/crates/assistant/src/tests.rs index e04703a63f..80cddbf730 100644 --- a/crates/assistant/src/tests.rs +++ b/crates/assistant/src/tests.rs @@ -1224,6 +1224,18 @@ fn mcp_protocol_errors() { assert_eq!(rpc(&server, &mut s, "tools/call", json!({"name": "no_such_tool"}))["error"]["code"], -32602); assert_eq!(rpc(&server, &mut s, "tools/call", json!({"name": "list_layers", "arguments": [1]}))["error"]["code"], -32602); assert_eq!(rpc(&server, &mut s, "resources/read", json!({"uri": "rustgis://docs/nope.md"}))["error"]["code"], -32002); + // Protocol validation fails before tool dispatch; its generated text is public too. + let private = "https://rpc-error.invalid/a?token=RGSYNTH_rpc_error&layer=roads"; + for (method, params, code) in [ + ("tools/call", json!({"name":private,"arguments":{}}), -32602), + ("resources/read", json!({"uri":private}), -32002), + (private, json!({}), -32601), + ] { + let reply = rpc(&server, &mut s, method, params); + assert_eq!(reply["error"]["code"], code); + assert!(!reply["error"]["message"].as_str().unwrap().contains("RGSYNTH_rpc_error")); + } + assert_eq!(rpc(&server, &mut s, "ping", json!({})), json!({})); // Batches: one reply per request, none for notifications only. let batch = r#"[{"jsonrpc":"2.0","id":1,"method":"ping"},{"jsonrpc":"2.0","method":"notifications/initialized"},{"jsonrpc":"2.0","id":2,"method":"nope"}]"#; let r: Value = serde_json::from_str(&server.handle(&mut s, batch).unwrap()).unwrap(); diff --git a/crates/assistant/src/tools.rs b/crates/assistant/src/tools.rs index 15a20386d6..0dc2aa58a5 100644 --- a/crates/assistant/src/tools.rs +++ b/crates/assistant/src/tools.rs @@ -259,6 +259,15 @@ pub fn run(s: &mut Session, call: &ToolCall) -> Result { /// 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 { + // Script failures are structured JSON: clean their error field before serialization. + if call.name == "run" { + return run_script(s, &call.args, tokens); + } + run_with_budget_inner(s, call, tokens).map_err(|e| public_error(&e)) +} + +/// 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 { let max_bytes = output_bytes(tokens); let a = &call.args; let v = match call.name.as_str() { @@ -278,7 +287,7 @@ pub fn run_with_budget(s: &mut Session, call: &ToolCall, tokens: usize) -> Resul 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| public_text(error_with_notices(e, s, 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" => { @@ -286,12 +295,10 @@ pub fn run_with_budget(s: &mut Session, call: &ToolCall, tokens: usize) -> Resul 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| public_text(error_with_notices(e, s, mark)))?; + .map_err(|e| error_with_notices(e, s, mark))?; return Ok(command_result(s, r, &before, mark, tokens)); } "read_features" => read_features(s, a)?, - // MCP's `run` (not offered to the in-app assistant): a script in one call. - "run" => return run_script(s, a, tokens), "search_docs" => crate::docs::search(&str_arg(a, "query").unwrap_or_default(), limit_arg(a, 8, 30))?, "read_doc" => { return crate::docs::read_with_budget( @@ -331,6 +338,11 @@ fn public_text(text: String) -> String { rustgis_engine::render::rustgis_tiles::logins::scrub(&text) } +/// Generated errors may quote arbitrary addresses; successful values remain data. +fn public_error(text: &str) -> String { + rustgis_engine::render::rustgis_tiles::logins::scrub_error(text) +} + /// Publishes a completed result, reporting withheld subtrees before enforcing the response budget. #[cfg(test)] fn public_result(s: &mut Session, value: Value, tokens: usize) -> String { @@ -570,6 +582,7 @@ fn run_script(s: &mut Session, a: &Value, tokens: usize) -> Result { + let e = public_error(&e); // Nothing was kept, so no line ran and no layer was added. // The notice of the failure is the error redacted: compared as such, it isn't repeated. let told = rustgis_engine::data::diagnostics::redact(&e); @@ -921,6 +934,69 @@ pub fn context(s: &Session) -> String { #[cfg(test)] #[allow(clippy::unwrap_used)] mod tests { + /// Returned tool and fetch errors are cleaned before active patterns can damage URLs; + /// successful values and the original call remain outside the generated-error projection. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn generated_tool_and_fetch_errors_clean_addresses_before_active_patterns() { + const TEST: &str = "tools::tests::generated_tool_and_fetch_errors_clean_addresses_before_active_patterns"; + if std::env::var("RUSTGIS_ISOLATED_TOOL_ERRORS").as_deref() != Ok(TEST) { + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", TEST]) + .env("RUSTGIS_ISOLATED_TOOL_ERRORS", TEST) + .output() + .unwrap(); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!( + output.status.success() && stdout.contains("1 passed; 0 failed"), + "{stdout}\n{}", + String::from_utf8_lossy(&output.stderr) + ); + return; + } + use rustgis_engine::render::rustgis_tiles::{failure::Failure, logins}; + let mut s = Session::new(rustgis_engine::Project::default()); + let url = "https://returned-error.invalid/a?token=RGSYNTH_returned_error&layer=roads"; + for active in [false, true] { + let binding = active.then(|| { + logins::activate( + "https://returned-error.invalid", + "synthetic-returned-error", + logins::Method::Header("X-Key".into()), + "https", + false, + ) + .unwrap() + }); + for (name, args) in [ + ("read_features", json!({"layer":url})), + ("describe_tool", json!({"tool":url})), + (url, json!({})), + ("run", json!({"script":url})), + ] { + 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(); + assert!(!error.contains("RGSYNTH_returned_error"), "{name}, active={active}: {error}"); + assert_eq!(call, original); + if name == "run" { + let reply: Value = serde_json::from_str(&error).unwrap(); + assert_eq!(reply["success"], false); + assert!(reply["error"].as_str().is_some()); + } + } + let address = binding.as_ref().map_or_else(|| url.to_string(), |binding| logins::bind(binding, url).unwrap()); + let failure = Failure::new(&address, format!("failed to fetch {address}")); + assert!(!failure.message.contains("RGSYNTH_returned_error")); + if let Some(binding) = binding { + assert!(logins::deactivate(&binding)); + } + } + 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_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"); + } use super::*; use rustgis_engine::render::rustgis_tiles::logins; diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index 763f9b455e..1e404fbbf7 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -145,7 +145,8 @@ pub fn clear_fetcher() { /// on whichever thread is starting them already, so one test's fetch may run on another's thread, /// later. Tests that end a login take turns too, as that empties the cache ([`clear`]), and so do /// tests that wait for or plant what the cache holds (an archive read, a style resolved), which -/// that would drop. The turn puts the native fetcher back when it ends. +/// that would drop. Tests that inspect clean failure text also take turns: a concurrent login +/// can redact even a single letter from that text. The turn puts the native fetcher back when it ends. #[cfg(test)] pub(crate) fn turn() -> Turn { static TURN: Mutex<()> = Mutex::new(()); @@ -692,7 +693,7 @@ fn fetch_tile(url: String, host: String, want: Want, omit: &'static [u16]) { Entry::Ready(Arc::new(p), size) } Err(e) => { - log::debug!("{}", crate::logins::scrub(&format!("{}: {e}", answer.key))); + log::debug!("{}", crate::logins::scrub_error(&format!("{}: {e}", answer.key))); // Refused until a time its server gave: the map draws again then. if let Some(wait) = retry_after(&e) { notify_at(Instant::now() + wait); @@ -876,7 +877,7 @@ impl<'a> RequestDiagnostics<'a> { let url = crate::logins::inner(url); let url = if rustgis_data::login_address::is_wrapper(url) { "[address omitted]" } else { url }; let clean = |text: &str| { - if carried || text.contains(rustgis_data::login_address::PREFIX) { crate::logins::scrub(text) } else { text.to_string() } + if carried || text.contains(rustgis_data::login_address::PREFIX) { crate::logins::scrub_error(text) } else { text.to_string() } }; // A status the caller expects is an answer, not a failure for the history; a caller // that treats the refusal as an error says so itself. One bounded record per server says @@ -1626,7 +1627,9 @@ mod native { } Err("too many redirects".into()) })(); - diagnostics.record(answer).map_err(|failure| if carried_login { crate::logins::scrub(&failure.message) } else { failure.message }) + diagnostics + .record(answer) + .map_err(|failure| if carried_login { crate::logins::scrub_error(&failure.message) } else { failure.message }) } /// POSTs a JSON document on a thread of its own. @@ -1682,7 +1685,9 @@ mod native { crate::logins::check(url).map_err(str::to_string)?; Ok(buf) })(); - diagnostics.record(answer).map_err(|failure| if carried_login { crate::logins::scrub(&failure.message) } else { failure.message }) + diagnostics + .record(answer) + .map_err(|failure| if carried_login { crate::logins::scrub_error(&failure.message) } else { failure.message }) } /// Downloads on a thread of its own, so a large file doesn't hold up the tiles. @@ -1785,7 +1790,7 @@ mod native { /// [`get_detailed`], a status in `omit` not recorded as a failure. pub(super) fn get_detailed_omitting(agent: &ureq::Agent, url: &str, omit: &'static [u16]) -> Result, Failure> { fetch_answer(agent, url, true, omit).map_err(|f| Failure { - url: if url.contains(rustgis_data::login_address::PREFIX) { crate::logins::scrub(url) } else { url.to_string() }, + url: if url.contains(rustgis_data::login_address::PREFIX) { crate::logins::scrub_error(url) } else { url.to_string() }, ..f }) } @@ -2004,8 +2009,8 @@ mod native { let result = crate::logins::check(super::resource_of(url)).map_err(Failure::from).and(result); result.map_err(|mut f| { if carried_login { - f.message = crate::logins::scrub(&f.message); - f.url = crate::logins::scrub(&f.url); + f.message = crate::logins::scrub_error(&f.message); + f.url = crate::logins::scrub_error(&f.url); } f }) @@ -2052,6 +2057,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn network_diagnostics_capture_background_failures_once() { + let _turn = turn(); use rustgis_data::diagnostics::{History, NetworkKind}; let history = History::default(); let _scope = history.enter(); @@ -2072,6 +2078,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn network_diagnostics_interleaved_sessions_keep_their_requests_private() { + let _turn = turn(); use rustgis_data::diagnostics::History; use std::io::{BufRead, BufReader, Write}; let first = History::default(); @@ -2132,6 +2139,7 @@ mod tests { #[test] fn network_diagnostics_explicit_capture_outlives_dispatch_scope_not_owner() { + let _turn = turn(); use rustgis_data::diagnostics::History; let first = History::default(); let second = History::default(); @@ -2158,6 +2166,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn network_diagnostics_redact_downloads_without_retaining_bodies() { + let _turn = turn(); use rustgis_data::diagnostics::History; let history = History::default(); let _scope = history.enter(); @@ -2208,6 +2217,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn network_diagnostics_skip_gone_map_tiles_and_keep_server_errors() { + let _turn = turn(); let history = rustgis_data::diagnostics::History::default(); let _scope = history.enter(); let answer = |status: &str| format!("HTTP/1.1 {status}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"); @@ -2229,6 +2239,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn network_diagnostics_keep_headers_after_a_credential_header() { + let _turn = turn(); let history = rustgis_data::diagnostics::History::default(); let _scope = history.enter(); let base = serve(vec![ @@ -2251,6 +2262,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn network_diagnostics_skip_routine_tile_404s_and_keep_other_statuses() { + let _turn = turn(); use rustgis_data::diagnostics::{History, Severity}; let history = History::default(); let _scope = history.enter(); @@ -2309,6 +2321,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn network_diagnostics_keep_observed_status_on_blocking_body_failure() { + let _turn = turn(); use rustgis_data::diagnostics::{History, NetworkKind}; let history = History::default(); let _scope = history.enter(); @@ -2326,6 +2339,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn network_diagnostics_preserve_connection_and_timeout_causes() { + let _turn = turn(); use rustgis_data::diagnostics::{History, NetworkKind}; let history = History::default(); let _scope = history.enter(); @@ -2360,6 +2374,7 @@ mod tests { /// history keeps no record of it. #[test] fn network_diagnostics_skip_failed_local_reads() { + let _turn = turn(); let history = rustgis_data::diagnostics::History::default(); let _scope = history.enter(); native::get(&native::agent(), "local-file:/nonexistent-1155.pmtiles", &[]).unwrap_err(); @@ -2368,6 +2383,7 @@ mod tests { #[test] fn network_diagnostics_ignore_cancellation_and_keep_browser_causes_truthful() { + let _turn = turn(); use rustgis_data::diagnostics::{History, NetworkKind}; let history = History::default(); let _scope = history.enter(); @@ -2395,6 +2411,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn public_downloads_refuse_names_that_resolve_locally() { + let _turn = turn(); let err = download_public_within("http://localhost:9/doc.json", 1024, std::time::Duration::from_secs(5)).unwrap_err(); assert!(err.contains("resolves to this computer or the local network"), "{err}"); } @@ -2422,6 +2439,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn downloads_stop_at_the_callers_budget() { + let _turn = turn(); for framing in ["Content-Length: 4\r\n", "", "Transfer-Encoding: chunked\r\n"] { let body = if framing.starts_with("Transfer") { "4\r\ntile\r\n0\r\n\r\n" } else { "tile" }; for (limit, ok) in [(0, false), (3, false), (4, true), (5, true)] { @@ -2442,6 +2460,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn download_budget_also_bounds_decompressed_bytes() { + let _turn = turn(); let data = vec![b'x'; 4096]; // The ZIP writer supplies the CRC-32 for a valid, generated gzip trailer. let zip = rustgis_data::zip::write(&[("body".into(), data.clone())]).unwrap(); @@ -3080,6 +3099,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn the_length_asked_for_follows_redirects_and_keeps_refusals() { + let _turn = turn(); let no_suffix = "HTTP/1.1 501 Not Implemented\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_string(); let agent = native::agent(); let base = serve(vec![ @@ -3114,6 +3134,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn a_refused_download_says_who_refused_it() { + let _turn = turn(); let page = "Attention Required! | CloudflareSorry, you have been blocked"; let blocked = format!( "HTTP/1.1 403 Forbidden\r\nServer: cloudflare\r\nCF-RAY: a4c9-BOG\r\nContent-Type: text/html\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{page}", @@ -3135,6 +3156,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn follows_only_allowed_redirects() { + let _turn = turn(); let redirect = |to: &str| format!("HTTP/1.1 302 Found\r\nLocation: {to}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"); let ok = "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ntile".to_string(); let agent = native::agent(); diff --git a/crates/tiles/src/cog.rs b/crates/tiles/src/cog.rs index f1dc6fe9a1..09c867cf95 100644 --- a/crates/tiles/src/cog.rs +++ b/crates/tiles/src/cog.rs @@ -404,7 +404,7 @@ fn with_info(url: &str, then: Box, String>) + Send>) 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(&e) } else { e }); + .map_err(|e| if rustgis_data::login_address::is_wrapper(&key) { crate::logins::scrub_error(&e) } else { e }); let waiting = { let mut i = infos().lock().unwrap_or_else(|e| e.into_inner()); let next = match &r { diff --git a/crates/tiles/src/failure.rs b/crates/tiles/src/failure.rs index 0f5c94eef1..58cbf0b3a5 100644 --- a/crates/tiles/src/failure.rs +++ b/crates/tiles/src/failure.rs @@ -68,9 +68,9 @@ impl Failure { /// the browser's `HTTP 403`). pub fn new(url: &str, message: impl Into) -> Self { let message = message.into(); - let message = if message.contains(rustgis_data::login_address::PREFIX) { crate::logins::scrub(&message) } else { message }; let reason = crate::cache::http_status(&message).filter(|s| *s >= 400).map_or(Reason::Other, Reason::Status); - let url = if url.contains(rustgis_data::login_address::PREFIX) { crate::logins::scrub(url) } else { url.to_string() }; + let message = crate::logins::scrub_error(&message); + let url = if url.contains(rustgis_data::login_address::PREFIX) { crate::logins::scrub_error(url) } else { url.to_string() }; Failure { url, message, reason, answer: None } } @@ -375,6 +375,7 @@ mod tests { #[test] fn a_cloudflare_block_is_named_with_its_page_title() { + let _turn = crate::cache::turn(); let body = b"Attention Required! | CloudflareSorry, you have been blocked"; let f = Failure::answered( "https://example.org/MapServer?f=json", @@ -462,6 +463,7 @@ mod tests { /// read, say nothing more. #[test] fn retry_after_on_429_and_503() { + let _turn = crate::cache::turn(); let base = || "http status: 503".to_string(); assert_eq!(with_retry_after(base(), 503, Some("30")), "http status: 503 (retry after 30 s)"); assert_eq!(with_retry_after("HTTP 429".into(), 429, Some(" 2 ")), "HTTP 429 (retry after 2 s)"); diff --git a/crates/tiles/src/logins.rs b/crates/tiles/src/logins.rs index aaaaa99991..7c92a34980 100644 --- a/crates/tiles/src/logins.rs +++ b/crates/tiles/src/logins.rs @@ -282,6 +282,19 @@ pub fn scrub(text: &str) -> String { } let mut patterns: Vec<_> = entries.values().flat_map(|entry| &entry.redactions).collect(); patterns.sort_unstable_by_key(|pattern| std::cmp::Reverse(pattern.value.expose().len())); + scrub_patterns(text, &patterns) +} + +/// Cleans generated error text before active values can obscure its address syntax. +/// Runtime request bindings and successful data values must not use this output projection. +pub fn scrub_error(text: &str) -> String { + let unbound = scrub_patterns(text, &[]); + let (public, _) = rustgis_data::address::scrub_addresses(&unbound); + scrub(&public) +} + +/// Removes runtime capability headers and the supplied active patterns in one pass. +fn scrub_patterns(text: &str, patterns: &[&Redaction]) -> String { // Strip capabilities even after revocation, when there are no active credential patterns. let mut result = String::with_capacity(text.len()); let mut rest = text; diff --git a/crates/tiles/src/pmtiles.rs b/crates/tiles/src/pmtiles.rs index 1176b8548a..8a5cb2dc5c 100644 --- a/crates/tiles/src/pmtiles.rs +++ b/crates/tiles/src/pmtiles.rs @@ -512,7 +512,7 @@ fn finish(archive: &str, r: Result) { .map_err(str::to_string) .and(r) .map(Arc::new) - .map_err(|e| if rustgis_data::login_address::is_wrapper(archive) { crate::logins::scrub(&e) } else { e }); + .map_err(|e| if rustgis_data::login_address::is_wrapper(archive) { crate::logins::scrub_error(&e) } else { e }); let waiting = { let mut all = archives().lock().unwrap_or_else(|e| e.into_inner()); let old = match &r { From aafb06b76064bb21909ab41e33914ca71ceb7c8c Mon Sep 17 00:00:00 2001 From: mavrick Date: Sat, 7 Nov 2026 04:19:04 -0600 Subject: [PATCH 14/14] fix(tiles): avoid caching retired request failures --- crates/tiles/src/cache.rs | 73 +++++++++++++++++++++++++++++++-- crates/tiles/src/cog.rs | 50 +++++++++++++++++----- crates/tiles/src/cog/raster.rs | 10 +++-- crates/tiles/src/pmtiles.rs | 8 ++-- crates/ui-egui/src/lib.rs | 36 ++++++++-------- crates/ui-egui/src/serviceui.rs | 14 ++++++- 6 files changed, 152 insertions(+), 39 deletions(-) diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index e7d2948210..4851eeffae 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -60,6 +60,12 @@ 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() +} + 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). @@ -684,9 +690,15 @@ impl Drop for Answer { } } +/// Retains the initiating lifetime through deferred decoding, so only resource failures are +/// shared with later callers; a retired owner's unanswered slot is released by `Answer`. 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; } @@ -699,6 +711,8 @@ fn fetch_tile(url: String, host: String, want: Want, omit: &'static [u16]) { 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. @@ -3343,6 +3357,53 @@ mod tests { assert!(most <= native::cap(&addr.to_string()), "{most} at a time"); } + /// 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"))] @@ -3424,8 +3485,6 @@ mod tests { let Some((_, done)) = HELD.lock().unwrap().pop() else { break }; done(Ok(b"3".to_vec())); }; - let cancelled = - |url: &str| lock().slots.get(url).is_some_and(|slot| matches!(&slot.entry, Entry::Failed(e, _) if e.contains("cancelled"))); let owner = rustgis_data::diagnostics::History::default(); let launcher = rustgis_data::diagnostics::History::default(); let live = Arc::new(std::sync::atomic::AtomicBool::new(true)); @@ -3452,7 +3511,10 @@ mod tests { 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!(cancelled(revoked), "the refused request keeps its cached cancellation"); + 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"); @@ -3467,7 +3529,10 @@ mod tests { with_still(Arc::new(|| false), drain_held); assert!(matches!(admitted, State::Pending)); assert!(!seen.lock().unwrap().iter().any(|url| url == floor)); - assert!(cancelled(floor), "the unrelated launcher's revocation still applies"); + 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: diff --git a/crates/tiles/src/cog.rs b/crates/tiles/src/cog.rs index 5b8199890d..1706792320 100644 --- a/crates/tiles/src/cog.rs +++ b/crates/tiles/src/cog.rs @@ -379,6 +379,7 @@ fn caller_callback(then: Box) + }) } +/// 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()) { @@ -422,18 +423,20 @@ fn with_info(url: &str, then: Box, String>) + Send>) .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(), } @@ -720,6 +723,7 @@ 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()) { @@ -767,24 +771,25 @@ fn with_stretch(info: Arc, file: String, look: Look, then: StretchThen) { let key_aef = look.aef; let done = move |r: Result, String>| { 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( @@ -1605,10 +1610,35 @@ mod tests { // 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, || get_tile(info, url, 0, 0, Box::new(move |r| done.send(r).unwrap()))); + 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()); + } } assert!(failures.is_empty(), "{failures:?}"); } 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/pmtiles.rs b/crates/tiles/src/pmtiles.rs index 2996292f6a..47b7057413 100644 --- a/crates/tiles/src/pmtiles.rs +++ b/crates/tiles/src/pmtiles.rs @@ -507,19 +507,21 @@ fn load(archive: String) { ); } +/// Settles archive waiters and keeps resource failures, forgetting dropped or retired reads. fn finish(archive: &str, r: Result) { 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). diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 55055b4af8..7a51eb24f7 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -455,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) @@ -4543,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; } @@ -4622,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(""), diff --git a/crates/ui-egui/src/serviceui.rs b/crates/ui-egui/src/serviceui.rs index fc51c773c4..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" { @@ -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)), }