From 8b664868c0e550a6126f2376cf3ab4922bd29801 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 07:22:31 -0600 Subject: [PATCH 01/12] fix(engine): validate model files before importing --- crates/engine/src/cmd/sharing.rs | 4 +- crates/engine/src/model.rs | 33 ++++++++++- crates/engine/tests/workflows.rs | 95 ++++++++++++++++++++++++++++++++ docs/user-guide/analysis.md | 4 ++ 4 files changed, 133 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/sharing.rs b/crates/engine/src/cmd/sharing.rs index c857c4eb62..8a01045966 100644 --- a/crates/engine/src/cmd/sharing.rs +++ b/crates/engine/src/cmd/sharing.rs @@ -126,6 +126,7 @@ fn web_link_chart(s: &mut Session, p: &Value) -> CmdResult { } const MANAGE: &str = r#"{"item"?: id|name (default: the last of its kind), "map"?: re-share from another map}"#; +/// Publish shared items and import models, checking file compatibility before creating a model. pub fn commands() -> Vec { let share_as = |id: &'static str, label: &'static str, run: fn(&mut Session, &Value) -> CmdResult, params: &'static str| { C::new(id, label, "Publish › Share As", run).params(params) @@ -158,6 +159,7 @@ pub fn commands() -> Vec { 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}"))?; + crate::model::check_file(&v)?; let name = v["name"].as_str().unwrap_or("Tool").chars().take(200).collect::(); // A model file (`*.model.json`) holds a graph; a web tool its steps. if let Some(graph) = v.get("graph") { @@ -221,7 +223,7 @@ pub fn share_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Ve .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}); + let tool = json!({"format": "RustGIS web tool", "version": crate::model::WEB_TOOL_VERSION, "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())) } "webScene" => { diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index bc48297e7a..e30784a74c 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -10,7 +10,7 @@ use std::collections::{HashMap, HashSet}; use rustgis_analysis::{ParamKind, ToolSpec}; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; use crate::Session; @@ -685,8 +685,37 @@ fn rename(s: &mut Session, id: u32, wanted: &str) -> Option { /// The JSON a model file holds (`*.model.json`). pub const SCHEMA: &str = "https://rustgis.app/schemas/model-graph-v1.json"; +/// The model-file version understood by this reader and emitted by this writer. +pub(crate) const FILE_VERSION: &str = "1.0.0"; +/// Web tools store steps under a separate, integer version marker. +pub(crate) const WEB_TOOL_VERSION: u64 = 1; + +/// Check declared file versions and node representations before import can discard nodes. +/// Missing versions are legacy files. Direct graph commands keep their existing sanitization. +pub(crate) fn check_file(v: &Value) -> Result<(), String> { + let graph = v.get("graph"); + if let Some(version) = v.get("version") { + let supported = if graph.is_some() { version.as_str() == Some(FILE_VERSION) } else { version.as_u64() == Some(WEB_TOOL_VERSION) }; + if !supported { + return Err("this model or web tool has an unsupported version. Update RustGIS to import it.".into()); + } + } + if let Some(graph) = graph { + let nodes = graph.get("nodes").and_then(Value::as_array).ok_or("a model graph needs \"nodes\"")?; + if nodes.len() > MAX_NODES { + return Err(format!("a model can have at most {MAX_NODES} nodes")); + } + for (index, node) in nodes.iter().enumerate() { + ModelNode::deserialize(node) + .map_err(|_| format!("model node {} is unsupported or malformed. Update RustGIS to import this model.", index + 1))?; + } + } + Ok(()) +} + +/// A graph's current file envelope; imports also accept legacy envelopes without a version. pub fn to_file(name: &str, g: &ModelGraph) -> Value { - json!({"$schema": SCHEMA, "version": "1.0.0", "name": name, "graph": g}) + json!({"$schema": SCHEMA, "version": FILE_VERSION, "name": name, "graph": g}) } #[cfg(test)] #[allow(clippy::unwrap_used)] diff --git a/crates/engine/tests/workflows.rs b/crates/engine/tests/workflows.rs index 8d71711d1d..7e7a34fd7c 100644 --- a/crates/engine/tests/workflows.rs +++ b/crates/engine/tests/workflows.rs @@ -9,6 +9,101 @@ use rustgis_engine::{Project, Session}; use serde_json::{Value, json}; use std::path::{Path, PathBuf}; +/// Declared versions are checked before payloads, and unreadable nodes cannot disappear on import. +#[test] +fn model_import_refuses_unsupported_files_without_changing_session() { + use base64::Engine; + let scratch = Scratch::new("model-compatibility"); + let mut s = Session::new(Project::new("Original")); + s.run("model.new", &json!({"name": "Keep", "steps": []})).unwrap(); + s.run("project.rename", &json!({"name": "Redo"})).unwrap(); + s.run("edit.undo", &Value::Null).unwrap(); + s.path = Some(scratch.path("original.rgis")); + s.mark_saved(); + let before = serde_json::to_value(&s.project).unwrap(); + let history = (s.undo_depth(), s.undo_label(), s.redo_label()); + let graph = json!({"nodes": [ + {"id": "i", "kind": "input", "layer": "Points"}, + {"id": "future", "kind": "branch"}, + {"id": "o", "kind": "output", "name": "Result"} + ], "edges": [ + {"from": "i", "fromPort": "out", "to": "future", "toPort": "input"}, + {"from": "future", "fromPort": "out", "to": "o", "toPort": "in"} + ]}); + for (file, message) in [ + (json!({"version": "2.0.0", "graph": "unreadable future graph"}), "unsupported version"), + (json!({"version": 2, "steps": "unreadable future steps"}), "unsupported version"), + (json!({"version": null, "graph": {"nodes": []}}), "unsupported version"), + (json!({"version": "1", "steps": []}), "unsupported version"), + (json!({"version": "1.0.0", "graph": graph}), "node 2"), + (json!({"graph": {"nodes": [{"id": "t", "kind": "tool", "tool": {}}]}}), "node 1"), + ] { + let text = file.to_string(); + let path = scratch.path("unsupported.model.json"); + std::fs::write(&path, &text).unwrap(); + for params in + [json!({"text": text}), json!({"data": base64::engine::general_purpose::STANDARD.encode(&text)}), json!({"path": path})] + { + let error = s.run("model.import", ¶ms).unwrap_err(); + assert!(error.contains(message) && error.contains("Update RustGIS"), "{error}"); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before); + assert_eq!((s.undo_depth(), s.undo_label(), s.redo_label()), history); + assert!(s.can_undo() && s.can_redo()); + assert_eq!(s.path.as_ref(), Some(&scratch.path("original.rgis"))); + assert!(!s.dirty()); + } + } + // Direct graph commands retain their existing sanitization, outside the file boundary. + s.run("model.new", &json!({"name": "Direct", "graph": graph})).unwrap(); +} + +/// Actual graph and web-tool writers still round-trip, with or without their legacy version marker. +#[test] +fn model_import_preserves_supported_and_legacy_files() { + use base64::Engine; + let scratch = Scratch::new("model-roundtrip"); + let mut original = Session::new(Project::new("Original")); + original + .run( + "model.new", + &json!({"name": "Graph", "graph": {"nodes": [ + {"id": "i", "kind": "input", "layer": "Points"}, + {"id": "t", "kind": "tool", "tool": "buffer", "params": {"distance": 37}}, + {"id": "o", "kind": "output", "name": "Near points"} + ], "edges": [ + {"from": "i", "fromPort": "out", "to": "t", "toPort": "input"}, + {"from": "t", "fromPort": "out", "to": "o", "toPort": "in"} + ]}}), + ) + .unwrap(); + original + .run("model.new", &json!({"name": "Steps", "steps": [{"tool": "buffer", "params": {"input": "Points", "distance": 37}}]})) + .unwrap(); + let graph = original.run("model.export", &json!({"name": "Graph"})).unwrap(); + let (_, web, _) = rustgis_engine::cmd::sharing::share_bytes(&mut original, "webTool", &json!({"model": "Steps"})).unwrap(); + for (index, text) in [graph["text"].as_str().unwrap().to_string(), String::from_utf8(web).unwrap()].into_iter().enumerate() { + let file: Value = serde_json::from_str(&text).unwrap(); + assert_eq!(file["version"], if index == 0 { json!("1.0.0") } else { json!(1) }); + for legacy in [false, true] { + let mut file = file.clone(); + if legacy { + file.as_object_mut().unwrap().remove("version"); + } + let text = file.to_string(); + let path = scratch.path(if index == 0 { "graph.model.json" } else { "steps.rgistool" }); + std::fs::write(&path, &text).unwrap(); + for params in + [json!({"text": text}), json!({"data": base64::engine::general_purpose::STANDARD.encode(&text)}), json!({"path": path})] + { + let mut loaded = Session::new(Project::new("Loaded")); + loaded.run("model.import", ¶ms).unwrap(); + assert_eq!(loaded.project.models[0], original.project.models[index]); + assert!(loaded.notices.is_empty(), "supported files need no warning"); + } + } + } +} + /// The parcels' south-west corner, in Memphis, and their size in degrees: 4 columns by 3 rows. const WEST: f64 = -90.05; const SOUTH: f64 = 35.10; diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 7d727b597b..1b90f54663 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1177,6 +1177,10 @@ their name. or write files. **Copy Command** copies a `model.run` command that runs the model from the Script pane, the command line or Python. +Import checks declared model and web-tool versions before interpreting their contents. Unsupported +versions or unreadable graph nodes are refused with advice to update RustGIS, leaving the current +project unchanged. Supported files and legacy files without a version marker continue to import. + The Workflows pane's Models section lists the project's models, and New Model From History turns the geoprocessing you've run into one. Models made as steps (`model.new` with `steps`, where `%1` stands for step 1's output) run as they always have until they are saved from ModelBuilder, which From d4f1c85d4bbbb4da79bf3345fb414617777cb6c0 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 07:58:06 -0600 Subject: [PATCH 02/12] fix(engine): refuse lossy model graph imports --- crates/engine/src/model.rs | 38 +++++++++++++++++++++++++++++++- crates/engine/tests/workflows.rs | 31 +++++++++++++++++++++++++- docs/user-guide/analysis.md | 6 +++-- 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index e30784a74c..f2aeb25dde 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -690,6 +690,15 @@ pub(crate) const FILE_VERSION: &str = "1.0.0"; /// Web tools store steps under a separate, integer version marker. pub(crate) const WEB_TOOL_VERSION: u64 = 1; +/// Keep unknown fields visible at the file boundary without changing the command data types. +#[derive(Deserialize)] +struct ImportItem { + #[serde(flatten)] + known: T, + #[serde(flatten)] + extra: HashMap, +} + /// Check declared file versions and node representations before import can discard nodes. /// Missing versions are legacy files. Direct graph commands keep their existing sanitization. pub(crate) fn check_file(v: &Value) -> Result<(), String> { @@ -706,8 +715,35 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> { return Err(format!("a model can have at most {MAX_NODES} nodes")); } for (index, node) in nodes.iter().enumerate() { - ModelNode::deserialize(node) + let node = ImportItem::::deserialize(node) .map_err(|_| format!("model node {} is unsupported or malformed. Update RustGIS to import this model.", index + 1))?; + if !node.extra.is_empty() { + return Err(format!("model node {} has unsupported fields. Update RustGIS to import this model.", index + 1)); + } + if !node.known.params.is_null() && !node.known.params.is_object() { + return Err(format!("model node {} needs an object for its parameters", index + 1)); + } + } + let edges = match graph.get("edges") { + None => &[][..], + Some(edges) => edges.as_array().map(Vec::as_slice).ok_or("a model graph's connections must be an array")?, + }; + if edges.len() > MAX_EDGES { + return Err(format!("a model can have at most {MAX_EDGES} connections")); + } + for (index, edge) in edges.iter().enumerate() { + let edge = ImportItem::::deserialize(edge).map_err(|_| format!("model connection {} is malformed", index + 1))?; + if !edge.extra.is_empty() { + return Err(format!("model connection {} has unsupported fields. Update RustGIS to import this model.", index + 1)); + } + } + let normalized = graph_from_json(graph)?; + let lost_nodes = nodes.len().saturating_sub(normalized.nodes.len()); + let lost_edges = edges.len().saturating_sub(normalized.edges.len()); + if lost_nodes > 0 || lost_edges > 0 { + return Err(format!( + "this model would discard {lost_nodes} node(s) and {lost_edges} connection(s). Fix empty or duplicate node IDs and invalid connections before importing it." + )); } } Ok(()) diff --git a/crates/engine/tests/workflows.rs b/crates/engine/tests/workflows.rs index 7e7a34fd7c..519c192cf5 100644 --- a/crates/engine/tests/workflows.rs +++ b/crates/engine/tests/workflows.rs @@ -37,6 +37,32 @@ fn model_import_refuses_unsupported_files_without_changing_session() { (json!({"version": "1", "steps": []}), "unsupported version"), (json!({"version": "1.0.0", "graph": graph}), "node 2"), (json!({"graph": {"nodes": [{"id": "t", "kind": "tool", "tool": {}}]}}), "node 1"), + (json!({"graph": {"nodes": [{"id": " ", "kind": "input"}]}}), "discard 1 node(s)"), + (json!({"graph": {"nodes": [{"id": "t", "kind": "tool"}, {"id": "t", "kind": "output"}]}}), "discard 1 node(s)"), + (json!({"graph": {"nodes": [{"id": "t", "kind": "tool", "parms": {"distance": 37}}]}}), "unsupported fields"), + (json!({"graph": {"nodes": [{"id": "t", "kind": "tool", "params": [37]}]}}), "needs an object"), + (json!({"graph": {"nodes": [], "edges": {}}}), "connections must be an array"), + (json!({"graph": {"nodes": [], "edges": ["bad edge"]}}), "connection 1 is malformed"), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}, {"id": "b", "kind": "output"}], + "edges": [{"from": "a", "to": "b"}]}}), + "1 connection(s)", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}], + "edges": [{"from": "a", "to": "missing", "toPort": "input"}]}}), + "1 connection(s)", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}, {"id": "b", "kind": "output"}], + "edges": [{"from": "a", "to": "b", "toPort": "in"}, {"from": "a", "to": "b", "toPort": "in"}]}}), + "1 connection(s)", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}, {"id": "b", "kind": "output"}], + "edges": [{"from": "a", "to": "b", "toPort": "in", "future": true}]}}), + "unsupported fields", + ), ] { let text = file.to_string(); let path = scratch.path("unsupported.model.json"); @@ -45,7 +71,10 @@ fn model_import_refuses_unsupported_files_without_changing_session() { [json!({"text": text}), json!({"data": base64::engine::general_purpose::STANDARD.encode(&text)}), json!({"path": path})] { let error = s.run("model.import", ¶ms).unwrap_err(); - assert!(error.contains(message) && error.contains("Update RustGIS"), "{error}"); + assert!(error.contains(message), "{error}"); + if message == "unsupported version" || message.starts_with("node ") || message == "unsupported fields" { + assert!(error.contains("Update RustGIS"), "{error}"); + } assert_eq!(serde_json::to_value(&s.project).unwrap(), before); assert_eq!((s.undo_depth(), s.undo_label(), s.redo_label()), history); assert!(s.can_undo() && s.can_redo()); diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 1b90f54663..e80799efe4 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1178,8 +1178,10 @@ their name. Script pane, the command line or Python. Import checks declared model and web-tool versions before interpreting their contents. Unsupported -versions or unreadable graph nodes are refused with advice to update RustGIS, leaving the current -project unchanged. Supported files and legacy files without a version marker continue to import. +versions, unreadable nodes or connections, and unknown node or connection fields are refused. Graphs +that would lose nodes or connections during import are refused with a count and advice to fix the +file, leaving the current project unchanged. Supported files and legacy files without a version +marker continue to import. The Workflows pane's Models section lists the project's models, and New Model From History turns the geoprocessing you've run into one. Models made as steps (`model.new` with `steps`, where `%1` From 0a9da8fecec4cae7ebbf60f8dddc86a16d2ffe01 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 08:38:45 -0600 Subject: [PATCH 03/12] fix(engine): preserve repeated inputs on model import --- crates/engine/src/cmd/workflows.rs | 3 ++- crates/engine/src/model.rs | 16 ++++++++++++++-- crates/engine/tests/workflows.rs | 25 +++++++++++++++++++++++++ docs/user-guide/analysis.md | 2 +- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index 0dabd83a86..744ccbcb09 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -413,7 +413,8 @@ pub fn gallery_list(kind: &str) -> CmdResult { fn new_model(s: &mut Session, p: &Value) -> CmdResult { let base = ps(p, "name").map(str::trim).filter(|n| !n.is_empty()).unwrap_or("Model").chars().take(200).collect::(); - let graph = p.get("graph").filter(|g| !g.is_null()).map(crate::model::graph_from_json).transpose()?; + let read_graph = if b(p, "imported").unwrap_or(false) { crate::model::graph_from_import } else { crate::model::graph_from_json }; + let graph = p.get("graph").filter(|g| !g.is_null()).map(read_graph).transpose()?; let steps: Vec = match p.get("steps") { _ if graph.is_some() => graph.as_ref().map(crate::model::graph_to_steps).unwrap_or_default(), Some(v) => serde_json::from_value(v.clone()).map_err(|e| format!("steps: {e}"))?, diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index f2aeb25dde..c6cc068168 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -415,6 +415,16 @@ pub fn graph_to_steps(g: &ModelGraph) -> Vec { /// Reads a graph from JSON, dropping what can't be used: nodes without an id, repeated ids, /// edges to missing nodes or to themselves. Bounded in size. pub fn graph_from_json(v: &Value) -> Result { + read_graph(v, false) +} + +/// Reads an imported graph, preserving repeated inputs that the step-to-graph writer emits. +pub(crate) fn graph_from_import(v: &Value) -> Result { + read_graph(v, true) +} + +/// Shares graph sanitization; only file imports retain repeated edges into many-valued ports. +fn read_graph(v: &Value, imported: bool) -> Result { let nodes = v.get("nodes").and_then(Value::as_array).ok_or("a model graph needs \"nodes\"")?; if nodes.len() > MAX_NODES { return Err(format!("a model can have at most {MAX_NODES} nodes")); @@ -444,7 +454,9 @@ pub fn graph_from_json(v: &Value) -> Result { for e in edges { let Ok(e) = serde_json::from_value::(e.clone()) else { continue }; let has = |id: &str| g.nodes.iter().any(|n| n.id == id); - if e.from != e.to && has(&e.from) && has(&e.to) && !e.to_port.is_empty() && !g.edges.contains(&e) { + let repeated_input = + imported && g.nodes.iter().find(|n| n.id == e.to).is_some_and(|n| input_ports(n).iter().any(|p| p.id == e.to_port && p.many)); + if e.from != e.to && has(&e.from) && has(&e.to) && !e.to_port.is_empty() && (repeated_input || !g.edges.contains(&e)) { g.edges.push(e); } } @@ -737,7 +749,7 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> { return Err(format!("model connection {} has unsupported fields. Update RustGIS to import this model.", index + 1)); } } - let normalized = graph_from_json(graph)?; + let normalized = graph_from_import(graph)?; let lost_nodes = nodes.len().saturating_sub(normalized.nodes.len()); let lost_edges = edges.len().saturating_sub(normalized.edges.len()); if lost_nodes > 0 || lost_edges > 0 { diff --git a/crates/engine/tests/workflows.rs b/crates/engine/tests/workflows.rs index 519c192cf5..3d7da53850 100644 --- a/crates/engine/tests/workflows.rs +++ b/crates/engine/tests/workflows.rs @@ -133,6 +133,31 @@ fn model_import_preserves_supported_and_legacy_files() { } } +/// Repeated merge inputs emitted by the graph writer retain their multiplicity after import. +#[test] +fn model_import_round_trips_repeated_step_inputs() { + let mut original = Session::new(Project::new("Original")); + let steps = json!([{"tool": "merge", "params": {"inputs": "A;A"}}]); + original.run("model.new", &json!({"name": "Repeated", "steps": steps})).unwrap(); + let exported = original.run("model.export", &json!({"name": "Repeated"})).unwrap(); + let mut file: Value = serde_json::from_str(exported["text"].as_str().unwrap()).unwrap(); + assert_eq!(file["graph"]["edges"].as_array().unwrap().len(), 2); + for legacy in [false, true] { + if legacy { + file.as_object_mut().unwrap().remove("version"); + } + let mut loaded = Session::new(Project::new("Loaded")); + loaded.run("model.import", &json!({"text": file.to_string()})).unwrap(); + let model = &loaded.project.models[0]; + assert_eq!(serde_json::to_value(&model.steps).unwrap(), steps); + assert_eq!(serde_json::to_value(model.graph.as_ref().unwrap()).unwrap(), file["graph"]); + assert!(loaded.notices.is_empty(), "supported files need no warning"); + let reexported = loaded.run("model.export", &json!({"name": "Repeated"})).unwrap(); + let reexported: Value = serde_json::from_str(reexported["text"].as_str().unwrap()).unwrap(); + assert_eq!(reexported["graph"], file["graph"]); + } +} + /// The parcels' south-west corner, in Memphis, and their size in degrees: 4 columns by 3 rows. const WEST: f64 = -90.05; const SOUTH: f64 = 35.10; diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index e80799efe4..75d47b9c22 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1181,7 +1181,7 @@ Import checks declared model and web-tool versions before interpreting their con versions, unreadable nodes or connections, and unknown node or connection fields are refused. Graphs that would lose nodes or connections during import are refused with a count and advice to fix the file, leaving the current project unchanged. Supported files and legacy files without a version -marker continue to import. +marker continue to import, including repeated sources in multi-input tools such as Merge. The Workflows pane's Models section lists the project's models, and New Model From History turns the geoprocessing you've run into one. Models made as steps (`model.new` with `steps`, where `%1` From e9fad9559482ef47760a65d35eb886618aed6247 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 09:29:30 -0600 Subject: [PATCH 04/12] fix(engine): guard model contents and preserve repeated inputs --- crates/engine/src/cmd/workflows.rs | 3 +-- crates/engine/src/model.rs | 34 ++++++++++++++++++------------ crates/engine/tests/workflows.rs | 18 +++++++++++----- docs/user-guide/analysis.md | 9 ++++---- 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index 744ccbcb09..0dabd83a86 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -413,8 +413,7 @@ pub fn gallery_list(kind: &str) -> CmdResult { fn new_model(s: &mut Session, p: &Value) -> CmdResult { let base = ps(p, "name").map(str::trim).filter(|n| !n.is_empty()).unwrap_or("Model").chars().take(200).collect::(); - let read_graph = if b(p, "imported").unwrap_or(false) { crate::model::graph_from_import } else { crate::model::graph_from_json }; - let graph = p.get("graph").filter(|g| !g.is_null()).map(read_graph).transpose()?; + let graph = p.get("graph").filter(|g| !g.is_null()).map(crate::model::graph_from_json).transpose()?; let steps: Vec = match p.get("steps") { _ if graph.is_some() => graph.as_ref().map(crate::model::graph_to_steps).unwrap_or_default(), Some(v) => serde_json::from_value(v.clone()).map_err(|e| format!("steps: {e}"))?, diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index c6cc068168..04a219f33c 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -414,17 +414,8 @@ pub fn graph_to_steps(g: &ModelGraph) -> Vec { /// Reads a graph from JSON, dropping what can't be used: nodes without an id, repeated ids, /// edges to missing nodes or to themselves. Bounded in size. +/// Repeated connections into many-valued input ports retain their multiplicity. pub fn graph_from_json(v: &Value) -> Result { - read_graph(v, false) -} - -/// Reads an imported graph, preserving repeated inputs that the step-to-graph writer emits. -pub(crate) fn graph_from_import(v: &Value) -> Result { - read_graph(v, true) -} - -/// Shares graph sanitization; only file imports retain repeated edges into many-valued ports. -fn read_graph(v: &Value, imported: bool) -> Result { let nodes = v.get("nodes").and_then(Value::as_array).ok_or("a model graph needs \"nodes\"")?; if nodes.len() > MAX_NODES { return Err(format!("a model can have at most {MAX_NODES} nodes")); @@ -455,7 +446,7 @@ fn read_graph(v: &Value, imported: bool) -> Result { let Ok(e) = serde_json::from_value::(e.clone()) else { continue }; let has = |id: &str| g.nodes.iter().any(|n| n.id == id); let repeated_input = - imported && g.nodes.iter().find(|n| n.id == e.to).is_some_and(|n| input_ports(n).iter().any(|p| p.id == e.to_port && p.many)); + g.nodes.iter().find(|n| n.id == e.to).is_some_and(|n| input_ports(n).iter().any(|p| p.id == e.to_port && p.many)); if e.from != e.to && has(&e.from) && has(&e.to) && !e.to_port.is_empty() && (repeated_input || !g.edges.contains(&e)) { g.edges.push(e); } @@ -711,8 +702,8 @@ struct ImportItem { extra: HashMap, } -/// Check declared file versions and node representations before import can discard nodes. -/// Missing versions are legacy files. Direct graph commands keep their existing sanitization. +/// Check declared versions and model contents before import can discard data. +/// Missing versions are legacy files. The stricter field checks apply only to file import. pub(crate) fn check_file(v: &Value) -> Result<(), String> { let graph = v.get("graph"); if let Some(version) = v.get("version") { @@ -726,6 +717,9 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> { if nodes.len() > MAX_NODES { return Err(format!("a model can have at most {MAX_NODES} nodes")); } + if graph.as_object().is_some_and(|g| g.keys().any(|k| !matches!(k.as_str(), "nodes" | "edges"))) { + return Err("model graph has unsupported fields. Update RustGIS to import this model.".into()); + } for (index, node) in nodes.iter().enumerate() { let node = ImportItem::::deserialize(node) .map_err(|_| format!("model node {} is unsupported or malformed. Update RustGIS to import this model.", index + 1))?; @@ -749,7 +743,7 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> { return Err(format!("model connection {} has unsupported fields. Update RustGIS to import this model.", index + 1)); } } - let normalized = graph_from_import(graph)?; + let normalized = graph_from_json(graph)?; let lost_nodes = nodes.len().saturating_sub(normalized.nodes.len()); let lost_edges = edges.len().saturating_sub(normalized.edges.len()); if lost_nodes > 0 || lost_edges > 0 { @@ -757,6 +751,18 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> { "this model would discard {lost_nodes} node(s) and {lost_edges} connection(s). Fix empty or duplicate node IDs and invalid connections before importing it." )); } + } else if let Some(steps) = v.get("steps") { + let steps = steps.as_array().ok_or("model steps must be an array")?; + if steps.len() > MAX_NODES { + return Err(format!("a model can have at most {MAX_NODES} steps")); + } + for (index, step) in steps.iter().enumerate() { + let step = ImportItem::::deserialize(step) + .map_err(|_| format!("model step {} is unsupported or malformed. Update RustGIS to import this model.", index + 1))?; + if !step.extra.is_empty() { + return Err(format!("model step {} has unsupported fields. Update RustGIS to import this model.", index + 1)); + } + } } Ok(()) } diff --git a/crates/engine/tests/workflows.rs b/crates/engine/tests/workflows.rs index 3d7da53850..4c8e5423b3 100644 --- a/crates/engine/tests/workflows.rs +++ b/crates/engine/tests/workflows.rs @@ -35,6 +35,9 @@ fn model_import_refuses_unsupported_files_without_changing_session() { (json!({"version": 2, "steps": "unreadable future steps"}), "unsupported version"), (json!({"version": null, "graph": {"nodes": []}}), "unsupported version"), (json!({"version": "1", "steps": []}), "unsupported version"), + (json!({"steps": [{"tool": "buffer", "params": {}, "when": {"enabled": true}}]}), "step 1 has unsupported fields"), + (json!({"steps": vec![json!({"tool": "buffer", "params": {}}); 2001]}), "at most 2000 steps"), + (json!({"graph": {"nodes": [], "edges": [], "future": {"enabled": true}}}), "graph has unsupported fields"), (json!({"version": "1.0.0", "graph": graph}), "node 2"), (json!({"graph": {"nodes": [{"id": "t", "kind": "tool", "tool": {}}]}}), "node 1"), (json!({"graph": {"nodes": [{"id": " ", "kind": "input"}]}}), "discard 1 node(s)"), @@ -72,7 +75,7 @@ fn model_import_refuses_unsupported_files_without_changing_session() { { let error = s.run("model.import", ¶ms).unwrap_err(); assert!(error.contains(message), "{error}"); - if message == "unsupported version" || message.starts_with("node ") || message == "unsupported fields" { + if message == "unsupported version" || message.starts_with("node ") || message.contains("unsupported fields") { assert!(error.contains("Update RustGIS"), "{error}"); } assert_eq!(serde_json::to_value(&s.project).unwrap(), before); @@ -133,7 +136,7 @@ fn model_import_preserves_supported_and_legacy_files() { } } -/// Repeated merge inputs emitted by the graph writer retain their multiplicity after import. +/// Repeated merge inputs survive import, saving under a new name and re-export. #[test] fn model_import_round_trips_repeated_step_inputs() { let mut original = Session::new(Project::new("Original")); @@ -151,10 +154,15 @@ fn model_import_round_trips_repeated_step_inputs() { let model = &loaded.project.models[0]; assert_eq!(serde_json::to_value(&model.steps).unwrap(), steps); assert_eq!(serde_json::to_value(model.graph.as_ref().unwrap()).unwrap(), file["graph"]); + loaded.run("model.save", &json!({"name": "Repeated", "rename": "Resaved", "graph": file["graph"]})).unwrap(); + assert_eq!(serde_json::to_value(&loaded.project.models[0].steps).unwrap(), steps); + assert_eq!(serde_json::to_value(loaded.project.models[0].graph.as_ref().unwrap()).unwrap(), file["graph"]); assert!(loaded.notices.is_empty(), "supported files need no warning"); - let reexported = loaded.run("model.export", &json!({"name": "Repeated"})).unwrap(); - let reexported: Value = serde_json::from_str(reexported["text"].as_str().unwrap()).unwrap(); - assert_eq!(reexported["graph"], file["graph"]); + for params in [json!({"name": "Resaved"}), json!({"name": "Resaved", "graph": file["graph"]})] { + let reexported = loaded.run("model.export", ¶ms).unwrap(); + let reexported: Value = serde_json::from_str(reexported["text"].as_str().unwrap()).unwrap(); + assert_eq!(reexported["graph"], file["graph"]); + } } } diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 75d47b9c22..2eb9adc701 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1178,10 +1178,11 @@ their name. Script pane, the command line or Python. Import checks declared model and web-tool versions before interpreting their contents. Unsupported -versions, unreadable nodes or connections, and unknown node or connection fields are refused. Graphs -that would lose nodes or connections during import are refused with a count and advice to fix the -file, leaving the current project unchanged. Supported files and legacy files without a version -marker continue to import, including repeated sources in multi-input tools such as Merge. +versions, unreadable nodes, connections or steps, and unknown graph, node, connection or step fields +are refused. Graphs that would lose nodes or connections during import are refused with a count and +advice to fix the file, leaving the current project unchanged. Supported files and legacy files +without a version marker continue to import, including repeated sources in multi-input tools such +as Merge. Repeated inputs stay intact when an imported model is saved or renamed. The Workflows pane's Models section lists the project's models, and New Model From History turns the geoprocessing you've run into one. Models made as steps (`model.new` with `steps`, where `%1` From 4171071f48faffcfc95b1c9e659550c57c533d7d Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 10:48:01 -0600 Subject: [PATCH 05/12] fix(engine): refuse lossy model conversions --- crates/engine/src/cmd/workflows.rs | 5 ++- crates/engine/src/model.rs | 67 ++++++++++++++++++++++++++++-- crates/engine/tests/workflows.rs | 34 +++++++++++++-- crates/ui-egui/src/modelbuilder.rs | 54 +++++++++++++++++++++--- docs/user-guide/analysis.md | 8 +++- 5 files changed, 153 insertions(+), 15 deletions(-) diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index 0dabd83a86..35f1d1f0ff 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -493,7 +493,10 @@ fn model_graph(s: &Session, p: &Value) -> Result<(String, crate::project::ModelG Some(n) => s.project.models.iter().find(|m| m.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no model '{n}'"))?, 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)); + let g = match &model.graph { + Some(graph) => graph.clone(), + None => crate::model::checked_steps_to_graph(&model.steps)?, + }; Ok((model.name.clone(), g, s.trusted_models.contains(&model.name.to_ascii_lowercase()))) } diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index 04a219f33c..dafae992af 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -19,6 +19,9 @@ use crate::project::{ModelEdge, ModelGraph, ModelNode, ModelNodeKind, ModelStep} /// The most nodes and edges a graph may have (files and parameters are hostile). pub const MAX_NODES: usize = 2000; pub const MAX_EDGES: usize = 4000; +/// Identifier length and canvas coordinate limits used by graph commands and file checks. +const MAX_NODE_ID: usize = 100; +const MAX_NODE_POSITION: f32 = 1e6; /// The output port of input and tool nodes. pub const OUT: &str = "out"; /// The input port of output nodes. @@ -377,6 +380,47 @@ pub fn steps_to_graph(steps: &[ModelStep]) -> ModelGraph { g } +/// Converts steps within the graph's supported limits, returning an error instead of truncating. +pub fn checked_steps_to_graph(steps: &[ModelStep]) -> Result { + let limit = MAX_NODES / 2; + if steps.len() > limit { + return Err(format!( + "this model has {} steps; graph conversion supports at most {limit}. The original steps have not been changed.", + steps.len() + )); + } + let mut inputs = HashSet::new(); + let mut edges = 0; + for (index, step) in steps.iter().enumerate() { + if !step.params.is_null() && !step.params.is_object() { + return Err(format!("model step {} needs an object for its parameters", index + 1)); + } + let node = ModelNode { tool: Some(step.tool.clone()), kind: ModelNodeKind::Tool, ..Default::default() }; + for port in input_ports(&node) { + let Some(value) = step.params.get(port.id).and_then(Value::as_str) else { continue }; + for part in value.split(|c| port.many && c == ';').map(str::trim) { + if port.many && part.is_empty() { + continue; + } + match part.strip_prefix('%').and_then(|n| n.parse::().ok()) { + Some(n) if !(1..=index).contains(&n) => continue, + Some(_) => {} + None => { + inputs.insert(part.to_ascii_lowercase()); + } + } + edges += 1; + if steps.len() + inputs.len() > MAX_NODES || edges > MAX_EDGES { + return Err(format!( + "this model exceeds the graph limits of {MAX_NODES} nodes and {MAX_EDGES} connections. The original steps have not been changed." + )); + } + } + } + } + Ok(steps_to_graph(steps)) +} + /// The graph's tools as steps in run order, edges as layer names (from inputs) and `%n` (from /// step n); empty when the graph has a loop. pub fn graph_to_steps(g: &ModelGraph) -> Vec { @@ -427,7 +471,7 @@ pub fn graph_from_json(v: &Value) -> Result { let mut g = ModelGraph::default(); for n in nodes { let Ok(mut node) = serde_json::from_value::(n.clone()) else { continue }; - node.id = node.id.trim().chars().take(100).collect(); + node.id = node.id.trim().chars().take(MAX_NODE_ID).collect(); if node.id.is_empty() || g.nodes.iter().any(|m| m.id == node.id) { continue; } @@ -435,8 +479,8 @@ pub fn graph_from_json(v: &Value) -> Result { node.x = 0.0; node.y = 0.0; } - node.x = node.x.clamp(-1e6, 1e6); - node.y = node.y.clamp(-1e6, 1e6); + node.x = node.x.clamp(-MAX_NODE_POSITION, MAX_NODE_POSITION); + node.y = node.y.clamp(-MAX_NODE_POSITION, MAX_NODE_POSITION); if !node.params.is_object() { node.params = Value::Null; } @@ -726,6 +770,20 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> { if !node.extra.is_empty() { return Err(format!("model node {} has unsupported fields. Update RustGIS to import this model.", index + 1)); } + if node.known.id.trim() != node.known.id || node.known.id.chars().nth(MAX_NODE_ID).is_some() { + return Err(format!( + "model node {} has an ID that would be trimmed or truncated. Use at most {MAX_NODE_ID} characters without surrounding whitespace.", + index + 1 + )); + } + if !(-MAX_NODE_POSITION..=MAX_NODE_POSITION).contains(&node.known.x) + || !(-MAX_NODE_POSITION..=MAX_NODE_POSITION).contains(&node.known.y) + { + return Err(format!( + "model node {} has a position outside the supported canvas bounds (-{MAX_NODE_POSITION} to {MAX_NODE_POSITION})", + index + 1 + )); + } if !node.known.params.is_null() && !node.known.params.is_object() { return Err(format!("model node {} needs an object for its parameters", index + 1)); } @@ -762,6 +820,9 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> { if !step.extra.is_empty() { return Err(format!("model step {} has unsupported fields. Update RustGIS to import this model.", index + 1)); } + if !step.known.params.is_null() && !step.known.params.is_object() { + return Err(format!("model step {} needs an object for its parameters", index + 1)); + } } } Ok(()) diff --git a/crates/engine/tests/workflows.rs b/crates/engine/tests/workflows.rs index 4c8e5423b3..e72e829532 100644 --- a/crates/engine/tests/workflows.rs +++ b/crates/engine/tests/workflows.rs @@ -36,11 +36,16 @@ fn model_import_refuses_unsupported_files_without_changing_session() { (json!({"version": null, "graph": {"nodes": []}}), "unsupported version"), (json!({"version": "1", "steps": []}), "unsupported version"), (json!({"steps": [{"tool": "buffer", "params": {}, "when": {"enabled": true}}]}), "step 1 has unsupported fields"), + (json!({"steps": [{"tool": "buffer", "params": ["input"]}]}), "step 1 needs an object"), (json!({"steps": vec![json!({"tool": "buffer", "params": {}}); 2001]}), "at most 2000 steps"), (json!({"graph": {"nodes": [], "edges": [], "future": {"enabled": true}}}), "graph has unsupported fields"), (json!({"version": "1.0.0", "graph": graph}), "node 2"), (json!({"graph": {"nodes": [{"id": "t", "kind": "tool", "tool": {}}]}}), "node 1"), - (json!({"graph": {"nodes": [{"id": " ", "kind": "input"}]}}), "discard 1 node(s)"), + (json!({"graph": {"nodes": [{"id": "", "kind": "input"}]}}), "discard 1 node(s)"), + (json!({"graph": {"nodes": [{"id": " padded ", "kind": "input"}]}}), "would be trimmed or truncated"), + (json!({"graph": {"nodes": [{"id": "a".repeat(101), "kind": "input"}]}}), "would be trimmed or truncated"), + (json!({"graph": {"nodes": [{"id": "a", "kind": "input", "x": 1000001}]}}), "outside the supported canvas bounds"), + (json!({"graph": {"nodes": [{"id": "a", "kind": "input", "y": 1e300}]}}), "outside the supported canvas bounds"), (json!({"graph": {"nodes": [{"id": "t", "kind": "tool"}, {"id": "t", "kind": "output"}]}}), "discard 1 node(s)"), (json!({"graph": {"nodes": [{"id": "t", "kind": "tool", "parms": {"distance": 37}}]}}), "unsupported fields"), (json!({"graph": {"nodes": [{"id": "t", "kind": "tool", "params": [37]}]}}), "needs an object"), @@ -85,8 +90,6 @@ fn model_import_refuses_unsupported_files_without_changing_session() { assert!(!s.dirty()); } } - // Direct graph commands retain their existing sanitization, outside the file boundary. - s.run("model.new", &json!({"name": "Direct", "graph": graph})).unwrap(); } /// Actual graph and web-tool writers still round-trip, with or without their legacy version marker. @@ -113,7 +116,12 @@ fn model_import_preserves_supported_and_legacy_files() { .unwrap(); let graph = original.run("model.export", &json!({"name": "Graph"})).unwrap(); let (_, web, _) = rustgis_engine::cmd::sharing::share_bytes(&mut original, "webTool", &json!({"model": "Steps"})).unwrap(); - for (index, text) in [graph["text"].as_str().unwrap().to_string(), String::from_utf8(web).unwrap()].into_iter().enumerate() { + original.run("model.new", &json!({"name": "Long Steps", "steps": vec![json!({"tool": "buffer", "params": {}}); 1001]})).unwrap(); + let (_, long, _) = rustgis_engine::cmd::sharing::share_bytes(&mut original, "webTool", &json!({"model": "Long Steps"})).unwrap(); + for (index, text) in [graph["text"].as_str().unwrap().to_string(), String::from_utf8(web).unwrap(), String::from_utf8(long).unwrap()] + .into_iter() + .enumerate() + { let file: Value = serde_json::from_str(&text).unwrap(); assert_eq!(file["version"], if index == 0 { json!("1.0.0") } else { json!(1) }); for legacy in [false, true] { @@ -131,9 +139,27 @@ fn model_import_preserves_supported_and_legacy_files() { loaded.run("model.import", ¶ms).unwrap(); assert_eq!(loaded.project.models[0], original.project.models[index]); assert!(loaded.notices.is_empty(), "supported files need no warning"); + if index == 2 { + let output = scratch.path("keep.model.json"); + std::fs::write(&output, "keep").unwrap(); + let error = loaded.run("model.export", &json!({"path": output})).unwrap_err(); + assert!(error.contains("1001 steps") && error.contains("at most 1000"), "{error}"); + assert_eq!(std::fs::read_to_string(output).unwrap(), "keep"); + assert_eq!(loaded.project.models[0], original.project.models[index]); + } } } } + // Graph capacity is checked before building or laying out a large multi-input step. + for (name, inputs) in + [("Connections", "A;".repeat(4001)), ("Nodes", (0..2001).map(|i| format!("Layer{i}")).collect::>().join(";"))] + { + original.run("model.new", &json!({"name": name, "steps": [{"tool": "merge", "params": {"inputs": inputs}}]})).unwrap(); + let before = original.project.models.clone(); + let error = original.run("model.export", &json!({"name": name})).unwrap_err(); + assert!(error.contains("graph limits"), "{error}"); + assert_eq!(original.project.models, before); + } } /// Repeated merge inputs survive import, saving under a new name and re-export. diff --git a/crates/ui-egui/src/modelbuilder.rs b/crates/ui-egui/src/modelbuilder.rs index 013fa79e6d..20a9775d90 100644 --- a/crates/ui-egui/src/modelbuilder.rs +++ b/crates/ui-egui/src/modelbuilder.rs @@ -322,7 +322,7 @@ pub fn open(app: &mut RustGisApp, name: Option<&str>) -> Result<(), String> { if app.mb.open && app.mb.dirty() && !app.mb.saved_as.as_deref().is_some_and(|s| s.eq_ignore_ascii_case(n)) { app.mb.confirm = Some(Pending::Open(n.to_string())); } else if !(app.mb.open && app.mb.saved_as.as_deref().is_some_and(|s| s.eq_ignore_ascii_case(n))) { - load_saved(app, n); + load_saved(app, n)?; } } else if !app.mb.open { let name = untitled(app); @@ -352,12 +352,19 @@ fn untitled(app: &RustGisApp) -> String { name } -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)); +/// Loads a saved model only when its graph can be shown without truncating its steps. +fn load_saved(app: &mut RustGisApp, name: &str) -> Result<(), String> { + let Some(m) = app.session.project.models.iter().find(|m| m.name.eq_ignore_ascii_case(name)) else { + return Err(format!("no model '{name}'")); + }; + let graph = match &m.graph { + Some(graph) => graph.clone(), + None => model::checked_steps_to_graph(&m.steps)?, + }; let imported = !app.session.trusted_models.contains(&m.name.to_ascii_lowercase()); let n = m.name.clone(); app.mb.load(&n, Some(n.clone()), graph, imported); + Ok(()) } /// Saves the canvas as a project model. @@ -474,7 +481,10 @@ pub(crate) fn import(app: &mut RustGisApp) { /// Opens a model `model.import` just added. pub fn imported(app: &mut RustGisApp, v: &Value) { if let Some(name) = v["model"].as_str() { - load_saved(app, name); + if let Err(error) = load_saved(app, name) { + app.session.notify(error, true); + return; + } app.mb.open = true; show_view(app); } @@ -487,7 +497,11 @@ fn proceed(app: &mut RustGisApp, p: Pending) { let name = untitled(app); app.mb.load(&name, None, ModelGraph::default(), false); } - Pending::Open(n) => load_saved(app, &n), + Pending::Open(n) => { + if let Err(error) = load_saved(app, &n) { + app.session.notify(error, true); + } + } Pending::Close => { app.mb.open = false; app.mb.active = false; @@ -1897,4 +1911,32 @@ mod tests { assert_eq!(app.mb.graph.edges.len(), 2); assert!(!app.mb.imported); } + + /// A step model beyond the conversion limit keeps its steps and the current canvas on + /// every open route, including import and an accepted discard prompt. + #[test] + fn oversized_step_models_do_not_replace_the_canvas() { + let mut app = RustGisApp::with_sample(); + app.run("model.new", json!({"name": "Keep", "steps": [{"tool": "buffer", "params": {"input": "Cities"}}]})).unwrap(); + app.run("ui.modelBuilder", json!({"name": "Keep"})).unwrap(); + let graph = app.mb.graph.clone(); + let steps = vec![json!({"tool": "buffer", "params": {}}); 1001]; + app.run("model.new", json!({"name": "Large", "steps": steps})).unwrap(); + let models = app.session.project.models.clone(); + + let error = app.run("ui.modelBuilder", json!({"name": "Large"})).unwrap_err(); + assert!(error.contains("1001 steps") && error.contains("at most 1000"), "{error}"); + for route in 0..2 { + app.session.notices.clear(); + if route == 0 { + imported(&mut app, &json!({"model": "Large"})); + } else { + proceed(&mut app, Pending::Open("Large".into())); + } + assert!(app.session.notices.iter().any(|notice| notice.error && notice.text.contains("1001 steps"))); + assert_eq!(app.mb.graph, graph); + assert_eq!(app.mb.saved_as.as_deref(), Some("Keep")); + assert_eq!(app.session.project.models, models); + } + } } diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 2eb9adc701..117be839ea 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1182,7 +1182,13 @@ versions, unreadable nodes, connections or steps, and unknown graph, node, conne are refused. Graphs that would lose nodes or connections during import are refused with a count and advice to fix the file, leaving the current project unchanged. Supported files and legacy files without a version marker continue to import, including repeated sources in multi-input tools such -as Merge. Repeated inputs stay intact when an imported model is saved or renamed. +as Merge. IDs that need trimming or exceed 100 characters, and positions outside the supported +canvas bounds, are refused before they can be changed. Repeated inputs stay intact when an imported +model is saved or renamed. + +Step files keep the existing 2,000-step limit. Converting steps to a ModelBuilder graph has a +1,000-step limit: opening or exporting a larger model as a graph gives an error and leaves the +original steps intact. The model can still run as steps or be shared as a web tool. The Workflows pane's Models section lists the project's models, and New Model From History turns the geoprocessing you've run into one. Models made as steps (`model.new` with `steps`, where `%1` From 9fc66b5424dbda169763a001e9fe9464dcc4c918 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 11:57:57 -0600 Subject: [PATCH 06/12] fix(engine): refuse ignored model file contents --- crates/engine/src/cmd/sharing.rs | 13 +++++++---- crates/engine/src/model.rs | 15 ++++++++++++- crates/engine/tests/workflows.rs | 37 ++++++++++++++++++++++++++++++++ docs/user-guide/analysis.md | 11 ++++++---- 4 files changed, 67 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/cmd/sharing.rs b/crates/engine/src/cmd/sharing.rs index 8a01045966..264d859dfc 100644 --- a/crates/engine/src/cmd/sharing.rs +++ b/crates/engine/src/cmd/sharing.rs @@ -162,11 +162,16 @@ pub fn commands() -> Vec { crate::model::check_file(&v)?; let name = v["name"].as_str().unwrap_or("Tool").chars().take(200).collect::(); // A model file (`*.model.json`) holds a graph; a web tool its steps. - if let Some(graph) = v.get("graph") { - return s.run("model.new", &json!({"name": name, "graph": graph, "imported": true})); + let imported = if let Some(graph) = v.get("graph") { + s.run("model.new", &json!({"name": name, "graph": graph, "imported": true}))? + } else { + 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}))? + }; + if v.get("name").is_some_and(|name| name.as_str() != imported.get("model").and_then(Value::as_str)) { + s.notify("1 model name was trimmed, shortened, replaced or made unique during import.", false); } - 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})) + Ok(imported) }) .params(r#"{"path"?: "*.model.json" | "*.rgistool", "text"?: file JSON, "data"?: base64 file}"#), ] diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index dafae992af..ed1c2df309 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -403,7 +403,12 @@ pub fn checked_steps_to_graph(steps: &[ModelStep]) -> Result continue; } match part.strip_prefix('%').and_then(|n| n.parse::().ok()) { - Some(n) if !(1..=index).contains(&n) => continue, + Some(n) if !(1..=index).contains(&n) => { + return Err(format!( + "model step {} uses %{n}, which is not an earlier step. The original steps have not been changed.", + index + 1 + )); + } Some(_) => {} None => { inputs.insert(part.to_ascii_lowercase()); @@ -756,6 +761,14 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> { return Err("this model or web tool has an unsupported version. Update RustGIS to import it.".into()); } } + if graph.is_some() && v.get("steps").is_some() { + return Err("a model file cannot contain both a graph and steps. Choose one representation before importing it.".into()); + } + let fields: &[&str] = + if graph.is_some() { &["$schema", "version", "name", "graph"] } else { &["format", "version", "name", "steps", "parameters"] }; + if v.as_object().is_some_and(|file| file.keys().any(|key| !fields.contains(&key.as_str()))) { + return Err("model file has unsupported fields. Update RustGIS to import this model.".into()); + } if let Some(graph) = graph { let nodes = graph.get("nodes").and_then(Value::as_array).ok_or("a model graph needs \"nodes\"")?; if nodes.len() > MAX_NODES { diff --git a/crates/engine/tests/workflows.rs b/crates/engine/tests/workflows.rs index e72e829532..454f0e3015 100644 --- a/crates/engine/tests/workflows.rs +++ b/crates/engine/tests/workflows.rs @@ -35,6 +35,9 @@ fn model_import_refuses_unsupported_files_without_changing_session() { (json!({"version": 2, "steps": "unreadable future steps"}), "unsupported version"), (json!({"version": null, "graph": {"nodes": []}}), "unsupported version"), (json!({"version": "1", "steps": []}), "unsupported version"), + (json!({"graph": {"nodes": []}, "description": "Keep this"}), "file has unsupported fields"), + (json!({"steps": [], "future": true}), "file has unsupported fields"), + (json!({"graph": {"nodes": []}, "steps": [{"tool": "buffer", "params": {}}]}), "both a graph and steps"), (json!({"steps": [{"tool": "buffer", "params": {}, "when": {"enabled": true}}]}), "step 1 has unsupported fields"), (json!({"steps": [{"tool": "buffer", "params": ["input"]}]}), "step 1 needs an object"), (json!({"steps": vec![json!({"tool": "buffer", "params": {}}); 2001]}), "at most 2000 steps"), @@ -160,6 +163,40 @@ fn model_import_preserves_supported_and_legacy_files() { assert!(error.contains("graph limits"), "{error}"); assert_eq!(original.project.models, before); } + for reference in ["%0", "%2"] { + let mut loaded = Session::new(Project::new("Invalid reference")); + let file = json!({"steps": [{"tool": "buffer", "params": {"input": reference}}]}); + loaded.run("model.import", &json!({"text": file.to_string()})).unwrap(); + let before = loaded.project.models.clone(); + let output = scratch.path("keep-reference.model.json"); + std::fs::write(&output, "keep").unwrap(); + let error = loaded.run("model.export", &json!({"path": output})).unwrap_err(); + assert!(error.contains(reference) && error.contains("not an earlier step"), "{error}"); + assert_eq!(std::fs::read_to_string(output).unwrap(), "keep"); + assert_eq!(loaded.project.models, before); + } +} + +/// Even a name emitted by the writer can exceed the import naming limit after disambiguation. +#[test] +fn model_import_reports_changed_names() { + let mut original = Session::new(Project::new("Original")); + for _ in 0..2 { + original.run("model.new", &json!({"name": "N".repeat(200), "steps": [{"tool": "buffer", "params": {"input": "Cities"}}]})).unwrap(); + } + let name = original.project.models[1].name.clone(); + assert!(name.chars().count() > 200); + let exported = original.run("model.export", &json!({"name": name})).unwrap(); + let mut file: Value = serde_json::from_str(exported["text"].as_str().unwrap()).unwrap(); + for name in [file["name"].clone(), json!(" Padded "), json!(17)] { + file["name"] = name.clone(); + let mut loaded = Session::new(Project::new("Loaded")); + loaded.run("model.import", &json!({"text": file.to_string()})).unwrap(); + assert_ne!(Some(loaded.project.models[0].name.as_str()), name.as_str()); + assert_eq!(loaded.notices.len(), 1); + assert!(!loaded.notices[0].error && loaded.notices[0].text.contains("1 model name")); + assert_eq!(serde_json::to_value(loaded.project.models[0].graph.as_ref().unwrap()).unwrap(), file["graph"]); + } } /// Repeated merge inputs survive import, saving under a new name and re-export. diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 117be839ea..baf6abb7fb 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1178,17 +1178,20 @@ their name. Script pane, the command line or Python. Import checks declared model and web-tool versions before interpreting their contents. Unsupported -versions, unreadable nodes, connections or steps, and unknown graph, node, connection or step fields -are refused. Graphs that would lose nodes or connections during import are refused with a count and +versions, unreadable nodes, connections or steps, and unknown file, graph, node, connection or step +fields are refused. A file containing both a graph and steps is refused rather than choosing one. +Graphs that would lose nodes or connections during import are refused with a count and advice to fix the file, leaving the current project unchanged. Supported files and legacy files without a version marker continue to import, including repeated sources in multi-input tools such as Merge. IDs that need trimming or exceed 100 characters, and positions outside the supported canvas bounds, are refused before they can be changed. Repeated inputs stay intact when an imported -model is saved or renamed. +model is saved or renamed. If import must trim, shorten or replace a model name, or make it unique, +it reports the change with a notice. Step files keep the existing 2,000-step limit. Converting steps to a ModelBuilder graph has a 1,000-step limit: opening or exporting a larger model as a graph gives an error and leaves the -original steps intact. The model can still run as steps or be shared as a web tool. +original steps intact. References to a step that is not earlier in the model also stop graph +conversion before data can be dropped. Larger models can still run as steps or be shared as a web tool. The Workflows pane's Models section lists the project's models, and New Model From History turns the geoprocessing you've run into one. Models made as steps (`model.new` with `steps`, where `%1` From cd15d0394f1b34fd974c4abc29158588b17165ee Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 12:33:02 -0600 Subject: [PATCH 07/12] fix(engine): refuse unsupported model connection ports --- crates/engine/src/model.rs | 11 +++++++++++ crates/engine/tests/workflows.rs | 16 ++++++++++++++++ crates/ui-egui/src/modelbuilder.rs | 4 ++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index ed1c2df309..6965455067 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -822,6 +822,17 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> { "this model would discard {lost_nodes} node(s) and {lost_edges} connection(s). Fix empty or duplicate node IDs and invalid connections before importing it." )); } + let by_id: HashMap<&str, &ModelNode> = normalized.nodes.iter().map(|n| (n.id.as_str(), n)).collect(); + for (index, edge) in normalized.edges.iter().enumerate() { + let from_known = by_id.get(edge.from.as_str()).is_some_and(|n| output_ports(n).iter().any(|p| p.id == edge.from_port)); + let to_known = by_id.get(edge.to.as_str()).is_some_and(|n| input_ports(n).iter().any(|p| p.id == edge.to_port)); + if !from_known || !to_known { + return Err(format!( + "model connection {} refers to an unsupported port. Update RustGIS or fix the connection before importing this model.", + index + 1 + )); + } + } } else if let Some(steps) = v.get("steps") { let steps = steps.as_array().ok_or("model steps must be an array")?; if steps.len() > MAX_NODES { diff --git a/crates/engine/tests/workflows.rs b/crates/engine/tests/workflows.rs index 454f0e3015..26dec6b377 100644 --- a/crates/engine/tests/workflows.rs +++ b/crates/engine/tests/workflows.rs @@ -74,6 +74,22 @@ fn model_import_refuses_unsupported_files_without_changing_session() { "edges": [{"from": "a", "to": "b", "toPort": "in", "future": true}]}}), "unsupported fields", ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input", "layer": "Points"}, + {"id": "b", "kind": "tool", "tool": "buffer"}], + "edges": [{"from": "a", "fromPort": "out", "to": "b", "toPort": "future"}]}}), + "connection 1 refers to an unsupported port", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}, {"id": "b", "kind": "output"}], + "edges": [{"from": "a", "fromPort": "out", "to": "b", "toPort": "future"}]}}), + "connection 1 refers to an unsupported port", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}, {"id": "b", "kind": "output"}], + "edges": [{"from": "a", "fromPort": "future", "to": "b", "toPort": "in"}]}}), + "connection 1 refers to an unsupported port", + ), ] { let text = file.to_string(); let path = scratch.path("unsupported.model.json"); diff --git a/crates/ui-egui/src/modelbuilder.rs b/crates/ui-egui/src/modelbuilder.rs index 20a9775d90..46d8e124fa 100644 --- a/crates/ui-egui/src/modelbuilder.rs +++ b/crates/ui-egui/src/modelbuilder.rs @@ -317,7 +317,7 @@ pub fn open(app: &mut RustGisApp, name: Option<&str>) -> Result<(), String> { forget_old_project(app); if let Some(n) = name { if !app.session.project.models.iter().any(|m| m.name.eq_ignore_ascii_case(n)) { - return Err(format!("no model '{n}'")); + return Err(crate::i18n::fmt("no model '{name}'", &[("name", n)])); } if app.mb.open && app.mb.dirty() && !app.mb.saved_as.as_deref().is_some_and(|s| s.eq_ignore_ascii_case(n)) { app.mb.confirm = Some(Pending::Open(n.to_string())); @@ -355,7 +355,7 @@ fn untitled(app: &RustGisApp) -> String { /// Loads a saved model only when its graph can be shown without truncating its steps. fn load_saved(app: &mut RustGisApp, name: &str) -> Result<(), String> { let Some(m) = app.session.project.models.iter().find(|m| m.name.eq_ignore_ascii_case(name)) else { - return Err(format!("no model '{name}'")); + return Err(crate::i18n::fmt("no model '{name}'", &[("name", name)])); }; let graph = match &m.graph { Some(graph) => graph.clone(), From 8b817604f89979e2ac417e12898356f31c47026a Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 13:47:20 -0600 Subject: [PATCH 08/12] fix(engine): reject references to steps without layer output --- crates/engine/src/model.rs | 14 +++++++++++++- crates/engine/tests/workflows.rs | 29 +++++++++++++++++++++++++---- docs/user-guide/analysis.md | 5 +++-- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index 6965455067..b8317dab50 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -381,6 +381,7 @@ pub fn steps_to_graph(steps: &[ModelStep]) -> ModelGraph { } /// Converts steps within the graph's supported limits, returning an error instead of truncating. +/// Referenced steps must be earlier and have a layer output. pub fn checked_steps_to_graph(steps: &[ModelStep]) -> Result { let limit = MAX_NODES / 2; if steps.len() > limit { @@ -409,7 +410,18 @@ pub fn checked_steps_to_graph(steps: &[ModelStep]) -> Result index + 1 )); } - Some(_) => {} + Some(n) => { + let has_no_output = steps.get(n.saturating_sub(1)).is_some_and(|previous| { + let node = ModelNode { tool: Some(previous.tool.clone()), kind: ModelNodeKind::Tool, ..Default::default() }; + output_ports(&node).is_empty() + }); + if has_no_output { + return Err(format!( + "model step {} uses %{n}, but step {n} has no layer output. The original steps have not been changed.", + index + 1 + )); + } + } None => { inputs.insert(part.to_ascii_lowercase()); } diff --git a/crates/engine/tests/workflows.rs b/crates/engine/tests/workflows.rs index 26dec6b377..c646ca8715 100644 --- a/crates/engine/tests/workflows.rs +++ b/crates/engine/tests/workflows.rs @@ -131,7 +131,13 @@ fn model_import_preserves_supported_and_legacy_files() { ) .unwrap(); original - .run("model.new", &json!({"name": "Steps", "steps": [{"tool": "buffer", "params": {"input": "Points", "distance": 37}}]})) + .run( + "model.new", + &json!({"name": "Steps", "steps": [ + {"tool": "buffer", "params": {"input": "Points", "distance": 37}}, + {"tool": "buffer", "params": {"input": "%1", "distance": 74}} + ]}), + ) .unwrap(); let graph = original.run("model.export", &json!({"name": "Graph"})).unwrap(); let (_, web, _) = rustgis_engine::cmd::sharing::share_bytes(&mut original, "webTool", &json!({"model": "Steps"})).unwrap(); @@ -158,6 +164,13 @@ fn model_import_preserves_supported_and_legacy_files() { loaded.run("model.import", ¶ms).unwrap(); assert_eq!(loaded.project.models[0], original.project.models[index]); assert!(loaded.notices.is_empty(), "supported files need no warning"); + if index == 1 { + let graph = loaded.run("model.export", &json!({})).unwrap(); + let mut reopened = Session::new(Project::new("Graph")); + reopened.run("model.import", &json!({"text": graph["text"]})).unwrap(); + assert_eq!(reopened.project.models[0].steps, original.project.models[index].steps); + assert!(reopened.notices.is_empty(), "supported references need no warning"); + } if index == 2 { let output = scratch.path("keep.model.json"); std::fs::write(&output, "keep").unwrap(); @@ -179,15 +192,23 @@ fn model_import_preserves_supported_and_legacy_files() { assert!(error.contains("graph limits"), "{error}"); assert_eq!(original.project.models, before); } - for reference in ["%0", "%2"] { + for (steps, reference, message) in [ + (json!([{"tool": "buffer", "params": {"input": "%0"}}]), "%0", "not an earlier step"), + (json!([{"tool": "buffer", "params": {"input": "%2"}}]), "%2", "not an earlier step"), + ( + json!([{"tool": "vectorToPmtiles", "params": {"input": "Points"}}, {"tool": "buffer", "params": {"input": "%1"}}]), + "%1", + "has no layer output", + ), + ] { let mut loaded = Session::new(Project::new("Invalid reference")); - let file = json!({"steps": [{"tool": "buffer", "params": {"input": reference}}]}); + let file = json!({"steps": steps}); loaded.run("model.import", &json!({"text": file.to_string()})).unwrap(); let before = loaded.project.models.clone(); let output = scratch.path("keep-reference.model.json"); std::fs::write(&output, "keep").unwrap(); let error = loaded.run("model.export", &json!({"path": output})).unwrap_err(); - assert!(error.contains(reference) && error.contains("not an earlier step"), "{error}"); + assert!(error.contains(reference) && error.contains(message), "{error}"); assert_eq!(std::fs::read_to_string(output).unwrap(), "keep"); assert_eq!(loaded.project.models, before); } diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index baf6abb7fb..c636c5ce61 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1190,8 +1190,9 @@ it reports the change with a notice. Step files keep the existing 2,000-step limit. Converting steps to a ModelBuilder graph has a 1,000-step limit: opening or exporting a larger model as a graph gives an error and leaves the -original steps intact. References to a step that is not earlier in the model also stop graph -conversion before data can be dropped. Larger models can still run as steps or be shared as a web tool. +original steps intact. References to a step that is not earlier in the model, or has no layer +output, also stop graph conversion before data can be dropped. Larger models can still run as +steps or be shared as a web tool. The Workflows pane's Models section lists the project's models, and New Model From History turns the geoprocessing you've run into one. Models made as steps (`model.new` with `steps`, where `%1` From 3af35df79047fb7038a44c2abaa2a38c3ce014fc Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 14:25:50 -0600 Subject: [PATCH 09/12] fix(engine): refuse cyclic model imports and report default renames --- crates/engine/src/cmd/sharing.rs | 2 +- crates/engine/src/model.rs | 3 +++ crates/engine/tests/workflows.rs | 18 ++++++++++++++++++ docs/user-guide/analysis.md | 3 ++- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/sharing.rs b/crates/engine/src/cmd/sharing.rs index 264d859dfc..2dd654c9cf 100644 --- a/crates/engine/src/cmd/sharing.rs +++ b/crates/engine/src/cmd/sharing.rs @@ -168,7 +168,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}))? }; - if v.get("name").is_some_and(|name| name.as_str() != imported.get("model").and_then(Value::as_str)) { + if v.get("name").map_or(Some("Tool"), Value::as_str) != imported.get("model").and_then(Value::as_str) { s.notify("1 model name was trimmed, shortened, replaced or made unique during import.", false); } Ok(imported) diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index b8317dab50..92494861e8 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -845,6 +845,9 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> { )); } } + if topological_order(&normalized).is_none() { + return Err("this model contains a loop and cannot be converted to steps. Fix the loop before importing it.".into()); + } } else if let Some(steps) = v.get("steps") { let steps = steps.as_array().ok_or("model steps must be an array")?; if steps.len() > MAX_NODES { diff --git a/crates/engine/tests/workflows.rs b/crates/engine/tests/workflows.rs index c646ca8715..f4829e7965 100644 --- a/crates/engine/tests/workflows.rs +++ b/crates/engine/tests/workflows.rs @@ -90,6 +90,14 @@ fn model_import_refuses_unsupported_files_without_changing_session() { "edges": [{"from": "a", "fromPort": "future", "to": "b", "toPort": "in"}]}}), "connection 1 refers to an unsupported port", ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "tool", "tool": "buffer"}, + {"id": "b", "kind": "tool", "tool": "buffer"}], "edges": [ + {"from": "a", "fromPort": "out", "to": "b", "toPort": "input"}, + {"from": "b", "fromPort": "out", "to": "a", "toPort": "input"} + ]}}), + "contains a loop", + ), ] { let text = file.to_string(); let path = scratch.path("unsupported.model.json"); @@ -234,6 +242,16 @@ fn model_import_reports_changed_names() { assert!(!loaded.notices[0].error && loaded.notices[0].text.contains("1 model name")); assert_eq!(serde_json::to_value(loaded.project.models[0].graph.as_ref().unwrap()).unwrap(), file["graph"]); } + file.as_object_mut().unwrap().remove("name"); + let mut loaded = Session::new(Project::new("Loaded")); + loaded.run("model.import", &json!({"text": file.to_string()})).unwrap(); + assert_eq!(loaded.project.models[0].name, "Tool"); + assert!(loaded.notices.is_empty(), "a missing name alone needs no warning"); + loaded.run("model.import", &json!({"text": file.to_string()})).unwrap(); + assert_eq!(loaded.project.models[1].name, "Tool 2"); + assert_eq!(loaded.notices.len(), 1); + assert!(!loaded.notices[0].error && loaded.notices[0].text.contains("1 model name")); + assert_eq!(serde_json::to_value(loaded.project.models[1].graph.as_ref().unwrap()).unwrap(), file["graph"]); } /// Repeated merge inputs survive import, saving under a new name and re-export. diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index c636c5ce61..e0eec8ca26 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1183,7 +1183,8 @@ fields are refused. A file containing both a graph and steps is refused rather t Graphs that would lose nodes or connections during import are refused with a count and advice to fix the file, leaving the current project unchanged. Supported files and legacy files without a version marker continue to import, including repeated sources in multi-input tools such -as Merge. IDs that need trimming or exceed 100 characters, and positions outside the supported +as Merge. Graphs containing a loop are refused rather than becoming empty steps. IDs that need +trimming or exceed 100 characters, and positions outside the supported canvas bounds, are refused before they can be changed. Repeated inputs stay intact when an imported model is saved or renamed. If import must trim, shorten or replace a model name, or make it unique, it reports the change with a notice. From 6fc29eb0038c8d149c8466ccb2401ffed22112e1 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 15:06:11 -0600 Subject: [PATCH 10/12] fix(engine): refuse multiple sources for single-valued model ports --- crates/engine/src/model.rs | 11 +++++++++-- crates/engine/tests/workflows.rs | 8 ++++++++ docs/user-guide/analysis.md | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index 92494861e8..5bb07279aa 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -835,15 +835,22 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> { )); } let by_id: HashMap<&str, &ModelNode> = normalized.nodes.iter().map(|n| (n.id.as_str(), n)).collect(); + let mut single_inputs = HashSet::new(); for (index, edge) in normalized.edges.iter().enumerate() { let from_known = by_id.get(edge.from.as_str()).is_some_and(|n| output_ports(n).iter().any(|p| p.id == edge.from_port)); - let to_known = by_id.get(edge.to.as_str()).is_some_and(|n| input_ports(n).iter().any(|p| p.id == edge.to_port)); - if !from_known || !to_known { + let to_port = by_id.get(edge.to.as_str()).and_then(|n| input_ports(n).into_iter().find(|p| p.id == edge.to_port)); + if !from_known || to_port.is_none() { return Err(format!( "model connection {} refers to an unsupported port. Update RustGIS or fix the connection before importing this model.", index + 1 )); } + if to_port.is_some_and(|p| !p.many) && !single_inputs.insert((edge.to.as_str(), edge.to_port.as_str())) { + return Err(format!( + "model connection {} duplicates a single-valued input port. Keep one connection to this port before importing this model.", + index + 1 + )); + } } if topological_order(&normalized).is_none() { return Err("this model contains a loop and cannot be converted to steps. Fix the loop before importing it.".into()); diff --git a/crates/engine/tests/workflows.rs b/crates/engine/tests/workflows.rs index f4829e7965..7bf3c58d34 100644 --- a/crates/engine/tests/workflows.rs +++ b/crates/engine/tests/workflows.rs @@ -90,6 +90,14 @@ fn model_import_refuses_unsupported_files_without_changing_session() { "edges": [{"from": "a", "fromPort": "future", "to": "b", "toPort": "in"}]}}), "connection 1 refers to an unsupported port", ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input", "layer": "A"}, + {"id": "b", "kind": "input", "layer": "B"}, {"id": "t", "kind": "tool", "tool": "buffer"}], "edges": [ + {"from": "a", "fromPort": "out", "to": "t", "toPort": "input"}, + {"from": "b", "fromPort": "out", "to": "t", "toPort": "input"} + ]}}), + "connection 2 duplicates a single-valued input port", + ), ( json!({"graph": {"nodes": [{"id": "a", "kind": "tool", "tool": "buffer"}, {"id": "b", "kind": "tool", "tool": "buffer"}], "edges": [ diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index e0eec8ca26..dde11dd86b 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1180,6 +1180,7 @@ their name. Import checks declared model and web-tool versions before interpreting their contents. Unsupported versions, unreadable nodes, connections or steps, and unknown file, graph, node, connection or step fields are refused. A file containing both a graph and steps is refused rather than choosing one. +Connections using unknown ports or feeding multiple sources into a single-valued port are refused. Graphs that would lose nodes or connections during import are refused with a count and advice to fix the file, leaving the current project unchanged. Supported files and legacy files without a version marker continue to import, including repeated sources in multi-input tools such From c84266c53ee3205028efd5834dd27e5a7f87b99d Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 15:55:51 -0600 Subject: [PATCH 11/12] fix(engine): refuse connections from unassigned model inputs --- crates/engine/src/model.rs | 9 +++++++++ crates/engine/tests/workflows.rs | 10 ++++++++++ docs/user-guide/analysis.md | 1 + 3 files changed, 20 insertions(+) diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index 5bb07279aa..1f15ca7cf3 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -845,6 +845,15 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> { index + 1 )); } + if by_id + .get(edge.from.as_str()) + .is_some_and(|n| n.kind == ModelNodeKind::Input && n.layer.as_deref().is_none_or(|layer| layer.trim().is_empty())) + { + return Err(format!( + "model connection {} has an input without a layer. Choose a layer for this input before importing this model.", + index + 1 + )); + } if to_port.is_some_and(|p| !p.many) && !single_inputs.insert((edge.to.as_str(), edge.to_port.as_str())) { return Err(format!( "model connection {} duplicates a single-valued input port. Keep one connection to this port before importing this model.", diff --git a/crates/engine/tests/workflows.rs b/crates/engine/tests/workflows.rs index 7bf3c58d34..2987dc3a38 100644 --- a/crates/engine/tests/workflows.rs +++ b/crates/engine/tests/workflows.rs @@ -98,6 +98,16 @@ fn model_import_refuses_unsupported_files_without_changing_session() { ]}}), "connection 2 duplicates a single-valued input port", ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}, {"id": "t", "kind": "tool", "tool": "buffer"}], + "edges": [{"from": "a", "fromPort": "out", "to": "t", "toPort": "input"}]}}), + "connection 1 has an input without a layer", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input", "layer": ""}, {"id": "t", "kind": "tool", "tool": "buffer"}], + "edges": [{"from": "a", "fromPort": "out", "to": "t", "toPort": "input"}]}}), + "connection 1 has an input without a layer", + ), ( json!({"graph": {"nodes": [{"id": "a", "kind": "tool", "tool": "buffer"}, {"id": "b", "kind": "tool", "tool": "buffer"}], "edges": [ diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index dde11dd86b..3e89c58d3e 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1181,6 +1181,7 @@ Import checks declared model and web-tool versions before interpreting their con versions, unreadable nodes, connections or steps, and unknown file, graph, node, connection or step fields are refused. A file containing both a graph and steps is refused rather than choosing one. Connections using unknown ports or feeding multiple sources into a single-valued port are refused. +Connections from inputs with missing or blank layer names are also refused. Graphs that would lose nodes or connections during import are refused with a count and advice to fix the file, leaving the current project unchanged. Supported files and legacy files without a version marker continue to import, including repeated sources in multi-input tools such From ff2f874255908ffd451a5df9fb665183f9b85eb2 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 5 Nov 2026 16:35:29 -0600 Subject: [PATCH 12/12] fix(engine): refuse ambiguous model input layer names --- crates/engine/src/model.rs | 25 +++++++++++++++++-------- crates/engine/tests/workflows.rs | 30 ++++++++++++++++++++++++++++++ docs/user-guide/analysis.md | 7 +++++-- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index 1f15ca7cf3..74676d0268 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -845,14 +845,23 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> { index + 1 )); } - if by_id - .get(edge.from.as_str()) - .is_some_and(|n| n.kind == ModelNodeKind::Input && n.layer.as_deref().is_none_or(|layer| layer.trim().is_empty())) - { - return Err(format!( - "model connection {} has an input without a layer. Choose a layer for this input before importing this model.", - index + 1 - )); + if let Some(input) = by_id.get(edge.from.as_str()).filter(|n| n.kind == ModelNodeKind::Input) { + let layer = input.layer.as_deref().filter(|layer| !layer.trim().is_empty()).ok_or_else(|| { + format!( + "model connection {} has an input without a layer. Choose a layer for this input before importing this model.", + index + 1 + ) + })?; + if by_id.get(edge.to.as_str()).is_some_and(|n| n.kind == ModelNodeKind::Tool) + && (layer.trim() != layer + || to_port.as_ref().is_some_and(|port| port.many && layer.contains(';')) + || layer.strip_prefix('%').and_then(|n| n.parse::().ok()).is_some()) + { + return Err(format!( + "model connection {} has an input layer name that cannot be represented in steps. Rename the layer to avoid surrounding whitespace, numeric %n references, or semicolons on multi-input ports before importing this model.", + index + 1 + )); + } } if to_port.is_some_and(|p| !p.many) && !single_inputs.insert((edge.to.as_str(), edge.to_port.as_str())) { return Err(format!( diff --git a/crates/engine/tests/workflows.rs b/crates/engine/tests/workflows.rs index 2987dc3a38..8e040efb5f 100644 --- a/crates/engine/tests/workflows.rs +++ b/crates/engine/tests/workflows.rs @@ -108,6 +108,21 @@ fn model_import_refuses_unsupported_files_without_changing_session() { "edges": [{"from": "a", "fromPort": "out", "to": "t", "toPort": "input"}]}}), "connection 1 has an input without a layer", ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input", "layer": "East;West"}, {"id": "t", "kind": "tool", "tool": "merge"}], + "edges": [{"from": "a", "fromPort": "out", "to": "t", "toPort": "inputs"}]}}), + "connection 1 has an input layer name that cannot be represented in steps", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input", "layer": "%1"}, {"id": "t", "kind": "tool", "tool": "buffer"}], + "edges": [{"from": "a", "fromPort": "out", "to": "t", "toPort": "input"}]}}), + "connection 1 has an input layer name that cannot be represented in steps", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input", "layer": " Points "}, {"id": "t", "kind": "tool", "tool": "buffer"}], + "edges": [{"from": "a", "fromPort": "out", "to": "t", "toPort": "input"}]}}), + "connection 1 has an input layer name that cannot be represented in steps", + ), ( json!({"graph": {"nodes": [{"id": "a", "kind": "tool", "tool": "buffer"}, {"id": "b", "kind": "tool", "tool": "buffer"}], "edges": [ @@ -208,6 +223,21 @@ fn model_import_preserves_supported_and_legacy_files() { } } } + // Step syntax stays literal for semicolons on single-input ports and nonnumeric percent names. + for (layer, tool, port) in [("East;West", "buffer", "input"), ("%north", "merge", "inputs")] { + let graph = json!({"nodes": [{"id": "i", "kind": "input", "layer": layer}, {"id": "t", "kind": "tool", "tool": tool}], + "edges": [{"from": "i", "fromPort": "out", "to": "t", "toPort": port}]}); + let mut loaded = Session::new(Project::default()); + loaded.run("model.import", &json!({"text": json!({"graph": graph}).to_string()})).unwrap(); + assert_eq!(loaded.project.models[0].steps[0].params[port], layer); + let (_, web, _) = rustgis_engine::cmd::sharing::share_bytes(&mut loaded, "webTool", &json!({})).unwrap(); + let mut reopened = Session::new(Project::default()); + reopened.run("model.import", &json!({"text": String::from_utf8(web).unwrap()})).unwrap(); + let exported = reopened.run("model.export", &json!({})).unwrap(); + let file: Value = serde_json::from_str(exported["text"].as_str().unwrap()).unwrap(); + assert!(file["graph"]["nodes"].as_array().unwrap().iter().any(|node| node["layer"] == layer)); + assert!(loaded.notices.is_empty() && reopened.notices.is_empty()); + } // Graph capacity is checked before building or laying out a large multi-input step. for (name, inputs) in [("Connections", "A;".repeat(4001)), ("Nodes", (0..2001).map(|i| format!("Layer{i}")).collect::>().join(";"))] diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 3e89c58d3e..87b48b7c7e 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1181,7 +1181,9 @@ Import checks declared model and web-tool versions before interpreting their con versions, unreadable nodes, connections or steps, and unknown file, graph, node, connection or step fields are refused. A file containing both a graph and steps is refused rather than choosing one. Connections using unknown ports or feeding multiple sources into a single-valued port are refused. -Connections from inputs with missing or blank layer names are also refused. +Connections from inputs with missing or blank layer names are also refused. For connections to tools, +layer names must have no surrounding whitespace, must not look like numeric step references (`%1`), +and must not contain semicolons on multi-input ports; these names cannot be preserved in steps. Graphs that would lose nodes or connections during import are refused with a count and advice to fix the file, leaving the current project unchanged. Supported files and legacy files without a version marker continue to import, including repeated sources in multi-input tools such @@ -1192,7 +1194,8 @@ model is saved or renamed. If import must trim, shorten or replace a model name, it reports the change with a notice. Step files keep the existing 2,000-step limit. Converting steps to a ModelBuilder graph has a -1,000-step limit: opening or exporting a larger model as a graph gives an error and leaves the +1,000-step limit: opening, exporting or checking (`model.validate`) a larger model as a graph +gives an error and leaves the original steps intact. References to a step that is not earlier in the model, or has no layer output, also stop graph conversion before data can be dropped. Larger models can still run as steps or be shared as a web tool.