Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions crates/engine/src/cmd/sharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C> {
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)
Expand Down Expand Up @@ -158,13 +159,19 @@ pub fn commands() -> Vec<C> {
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::<String>();
// 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").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);
}
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}"#),
]
Expand Down Expand Up @@ -221,7 +228,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" => {
Expand Down
5 changes: 4 additions & 1 deletion crates/engine/src/cmd/workflows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

model.validate now returns an error instead of a problem list for step models that can't become a graph. model_graph now calls checked_steps_to_graph for steps models, so model.validate (its handler at line 29 is unchanged, but it goes through this function) fails with the conversion message for a model over 1,000 steps, one with a forward or invalid %n, or one with non-object params. Before, the same model returned {"ok": ..., "problems": [...]}, and for over 1,000 steps it was validated against a silently truncated graph. The error beats the silent path, but this command's contract is to report problems, and the PR's docs don't mention validate. Suggest mapping the conversion error to one problem entry (for example code not-a-graph) in the validate command so the response shape stays the same, and noting the change in the docs.

Confidence: medium (behaviour change, not data loss).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reproduced with the CLI: model.validate returns the checked-conversion error for 1,001 steps or a forward reference, while the original steps remain intact. The command is already fallible for malformed graph input or a missing named model; {ok, problems} is the result after a graph can be obtained. Keeping the conversion failure avoids reporting on a truncated graph. The guide now explicitly includes model.validate among graph conversions that can fail. Mapping conversion failures to a structured problem entry is left for the release quality pass.

};
Ok((model.name.clone(), g, s.trusted_models.contains(&model.name.to_ascii_lowercase())))
}

Expand Down
220 changes: 214 additions & 6 deletions crates/engine/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -377,6 +380,64 @@ pub fn steps_to_graph(steps: &[ModelStep]) -> ModelGraph {
g
}

/// 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<ModelGraph, String> {
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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ;/%n port parsing is now written twice, once here and once in steps_to_graph (lines 353–356). Both must agree on what a reference is, what is skipped, and how inputs are keyed. Right now they do. If one changes, checked_steps_to_graph can accept a step that steps_to_graph then drops silently, which reintroduces the loss this PR is closing. A small shared helper that yields the parts for a port and their step references would keep the two paths in step.

Confidence: medium-high that the duplication is a drift risk. Quality point, not a current bug.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two parsers agree today. Consolidating them is left for the release quality pass; this batch keeps their current syntax and adds the demonstrated import-name refusal.

if port.many && part.is_empty() {
continue;
}
match part.strip_prefix('%').and_then(|n| n.parse::<usize>().ok()) {
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(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());
}
}
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<ModelStep> {
Expand Down Expand Up @@ -414,6 +475,7 @@ pub fn graph_to_steps(g: &ModelGraph) -> Vec<ModelStep> {

/// 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<ModelGraph, String> {
let nodes = v.get("nodes").and_then(Value::as_array).ok_or("a model graph needs \"nodes\"")?;
if nodes.len() > MAX_NODES {
Expand All @@ -426,16 +488,16 @@ pub fn graph_from_json(v: &Value) -> Result<ModelGraph, String> {
let mut g = ModelGraph::default();
for n in nodes {
let Ok(mut node) = serde_json::from_value::<ModelNode>(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;
}
if !node.x.is_finite() || !node.y.is_finite() {
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;
}
Expand All @@ -444,7 +506,9 @@ pub fn graph_from_json(v: &Value) -> Result<ModelGraph, String> {
for e in edges {
let Ok(e) = serde_json::from_value::<ModelEdge>(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 =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeated connections are now kept for every graph caller, so a file can multiply data at run time. Up to MAX_EDGES (4000) identical connections into one many-valued port now survive graph_from_json. A Merge fed the same layer 4000 times would multiply its features when run, and nothing caps the repeat count. The steps form ("A;A;…") already allowed this with no cap, so it's not new capability. It's a new file form of an existing one.

Confidence: low on impact, since I did not check the merge tool's own input limits. This is a note for the author, not a request to change the design. A per-port repeat cap, or a notice when a port repeats a source more than a handful of times, would close it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeated inputs preserve the existing step semantics: the writer already turns merge.inputs: "A;A" into two connections. Deduplicating those connections changes the saved workflow. The regression verifies preservation through import, save, rename and both export paths.

Graphs retain the existing 4,000-connection limit. Merge uses checked feature-count accumulation and fallible allocation, and already reports its output feature count and input-layer count in ToolResult::messages (crates/analysis/src/merge.rs). A new per-port repeat cap would change supported workflow semantics, so I am leaving that design change outside this compatibility fix.

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));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if e.from != e.to && has(&e.from) && has(&e.to) && !e.to_port.is_empty() && (repeated_input || !g.edges.contains(&e)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repeated-input change reaches every direct graph command, not only file import. graph_from_json is the reader for model.new (workflows.rs:416), model.save (:519) and model_graph (:490), so a graph passed to any of them now keeps duplicate edges into many-valued ports. The PR description says direct graph commands keep their existing sanitization, and the reply on the earlier thread says "the ordinary graph parser keeps its existing deduplication" and cites a graph_from_import reader. No such function exists in this head (a grep of crates/ finds none), so that reply describes code that isn't here.

The behaviour may well be the right one (it matches how steps write A;A), but it is a change to the direct path that the description says doesn't happen and no test covers. Either say so in the description, or split an import-only reader out of graph_from_json so the direct commands keep their current behaviour.

Confidence: high on the mismatch, medium on whether it is harmful.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The earlier description and import-only-reader reply described 0a9da8fe, not the current implementation. The PR description now reflects the shared-reader fix in e9fad955: repeated connections to a many-valued input are preserved for every caller. The existing steps writer emits these connections, and the validator/runtime already support them. Restricting this to import makes model.save and the UI’s explicit-graph export lose the repeated input again. The regression exercises import, save with rename, named export and explicit-graph export. Single-input duplicate cleanup and trust handling are unchanged. The save-path thread also confirms that the import-only recommendation is no longer needed: #1474 (comment).

g.edges.push(e);
}
}
Expand Down Expand Up @@ -685,8 +749,152 @@ fn rename(s: &mut Session, id: u32, wanted: &str) -> Option<String> {
/// 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;

/// Keep unknown fields visible at the file boundary without changing the command data types.
#[derive(Deserialize)]
struct ImportItem<T> {
#[serde(flatten)]
known: T,
#[serde(flatten)]
extra: HashMap<String, Value>,
}

/// 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> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unknown fields on steps and on the graph itself still import silently. check_file refuses unknown fields only on nodes and connections. A web tool (no graph) returns Ok right after the version check, so its steps are read later by serde_json::from_value::<ModelStep> in new_model. ModelStep (project.rs:527) has no deny_unknown_fields, so a step written by a newer RustGIS with an extra field, for example "when": …, imports with that field gone and no notice. The same applies to any key the graph object carries beyond nodes and edges: read_graph ignores it.

That is the same loss the PR's refusal covers for nodes, and AGENTS.md says a reader that drops data must say what, how much and where. A fix in the same shape as the node check would be ImportItem::<ModelStep> for each entry in steps, plus a check on the keys of graph other than nodes/edges, refusing with "Update RustGIS to import this model." and a count. Add a clean-input assertion that no notice is raised.

Confidence: medium-high that this is a gap; the code path is clear from reading.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reproduced both field losses through CLI import/save, plus an oversized step file silently becoming 2,000 steps. Fixed in e9fad955. The file guard refuses unknown graph keys and unknown or unreadable step fields, and checks the existing step limit before truncation. The regression covers text, base64 and path inputs with unchanged project/path/saved state/history; actual current and legacy writer round trips remain quiet. Full native CI (4,190 tests), WebAssembly and hooks passed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Top-level keys beyond graph/steps import silently. The unknown-field refusal covers node, connection and graph-object keys, but not the file's own top-level keys. A model file with graph and steps both present imports the graph and ignores steps. A future top-level key (for example a description) is dropped without a notice. Current writers emit only $schema, version, name, graph (or format, version, name, steps, parameters), so this only bites a file from a newer writer. The version check is meant to catch that, so I rate this low confidence.

Possible fix: refuse a file that has both graph and steps, and refuse top-level keys outside each format's known set, with the same "Update RustGIS to import this model." wording.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9fc66b5. File import now refuses an envelope containing both graph and steps, and unknown top-level fields outside the keys emitted by each current writer. The refusal regression covers graph/web-tool unknown fields and mixed payloads through text, base64 and file input with unchanged session state. Current and unversioned actual writer output still round-trips. Native CI and wasm passed.

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 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] =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-model JSON now gets "Update RustGIS" advice instead of "not a RustGIS model". A file with neither graph nor steps (for example a GeoJSON FeatureCollection) fails the top-level field check here, so model.import returns "model file has unsupported fields. Update RustGIS to import this model." Before this PR the same file got "not a RustGIS model or web tool (no graph or steps)", which is the accurate message. No test pins either wording, so this is not caught.

Suggested fix: refuse the no-graph/no-steps case before the field check, so the original message comes back:

Suggested change
let fields: &[&str] =
if graph.is_none() && v.get("steps").is_none() {
return Err("not a RustGIS model or web tool (no graph or steps)".into());
}
let fields: &[&str] =

Confidence: high on the behaviour (read from the code path); low impact, since it only affects the wrong file being chosen.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reproduced with a GeoJSON FeatureCollection: import refuses it before model creation, but the update advice is misleading. Restoring the more specific wrong-file diagnostic is left for the release quality pass.

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 {
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() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File import still changes node data without a notice. check_file now counts dropped nodes and connections, but graph_from_json (unchanged code, model.rs:430) trims and truncates every id to 100 characters, and clamps x/y to ±1e6 at lines 438–439. Neither is counted. A file with an isolated node whose id is 150 characters imports with a 100-character id and no warning. An edge that uses the long id is dropped and refused, but the refusal text says "empty or duplicate node IDs" and never mentions length.

AGENTS.md says a reader that approximates data must say so. Possible fix: in this node loop, refuse any node whose raw id differs from its normalized form, with a count. Positions can be reported the same way, or left as cosmetic if you decide they don't count as data.

Confidence: medium-high. I did not run it; this is from reading the code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4171071. Import now refuses IDs needing trimming/truncation and nonfinite/out-of-bounds canvas positions before the graph reader can change them. Regressions cover padded/long IDs and clamped/reset positions through text, base64 and file input with unchanged session state. My earlier normalization deferral was too broad and is withdrawn. Native CI, wasm and publication checks passed.

let node = ImportItem::<ModelNode>::deserialize(node)
.map_err(|_| format!("model node {} is unsupported or malformed. Update RustGIS to import this model.", index + 1))?;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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));
}
}
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::<ModelEdge>::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)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A cyclic graph imports with its steps silently emptied. check_file never checks for a loop. new_model then derives steps with graph_to_steps, which returns Vec::new() when topological_order is None (model.rs:443-444). The model is stored with an empty steps list and no notice. share.webTool exports m.steps (cmd/sharing.rs:213-231), so the web tool comes out with "steps": [] and nothing says so.

The graph itself is kept, and model.rs:238 already reports the loop at validation time, so this is a silent loss on the import path the PR is hardening. Either refuse the file here when topological_order(&normalized).is_none(), or raise a notice that steps cannot be derived from this graph.

Confidence: medium. I read this from the code paths and did not run it. The same gap exists for direct model.new with a graph, which is outside this diff.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3af35df. Import now checks the normalized graph with the existing topological-order function and refuses loops before creating the model. The error explains that the graph cannot be converted to steps and asks for the loop to be fixed.

The regression first reproduced a successful import with zero steps, then verifies refusal through text, base64 and path imports without changing the project or undo/redo history. Supported writer round trips remain quiet. Native CI, wasm and publication hooks passed.

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."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refusal wording doesn't name exact duplicate connections. A file with two identical connections into a single-input port (for example in on an output node) used to import, because graph_from_json deduplicated it. It is now refused by the lost_edges count, but this message only mentions node IDs and "invalid connections", so the user is told to fix the wrong thing. The editor never writes such duplicates (connect is a no-op for an existing edge), so this only hits hand-written or older files. Whether to refuse them or accept them with a notice is a policy choice. The wording is a quality point, so it can wait for the release quality pass if the maintainers prefer, but the current text points at the wrong cause.

Confidence: high that the message is misleading; medium that refusing exact duplicates is the desired behaviour.

Suggested change
"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."
"this model would discard {lost_nodes} node(s) and {lost_edges} connection(s): empty or duplicate node IDs, repeated connections into a single-input port, or invalid connections. Fix them before importing it."

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refusal is intentional: an imported file is rejected before the graph reader can discard connections. This includes duplicate connections into a single-input port; repeated connections into many-valued ports remain supported. The existing refusal regression covers that distinction.

Naming exact duplicate connections in the diagnostic is a wording improvement, left for the release quality pass.

));
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A connection from an input with no layer is accepted on import, then silently dropped from the steps.

check_file checks each connection's ports but never checks the layer of an input node that feeds it. validate flags such an input as missing-layer, yet import accepts the file. new_model then calls graph_to_steps, and its filter_map (model.rs around line 461, ModelNodeKind::Input => up.layer.clone()) returns None for that source. The connection disappears from the derived steps, and the port falls back to its default or stays unset. share.webTool exports m.steps, so the shared web tool loses the input with no notice. The graph copy keeps the edge, which is why the loss is quiet.

Example: an input node with no layer, wired to a Buffer's input port, imports with no warning. The web tool then runs Buffer with no input.

AGENTS.md says a reader that drops data must refuse it or say so, and this PR's own description claims graphs that would lose connections are refused. This path isn't covered. Suggested fix: refuse a connection whose source is an input with a missing or blank layer, with the connection index, before graph_to_steps runs. Add a regression that imports such a file through text, base64 and path, and checks that the session is unchanged.

Confidence: medium-high. I read this from the code paths and did not run it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c84266c. Import now refuses a connection from an input with a missing or blank layer name, using the validator's existing definition of an unassigned input. The indexed error asks to choose a layer before importing. This happens before model creation and step derivation.

The regression reproduced a successful import of the missing-layer case, then checks missing and blank names through text, base64 and path imports with the project and undo/redo history unchanged. Supported writer round trips, including named layers absent from the current project and repeated A;A inputs, remain quiet. Unconnected placeholders are unaffected. Native CI, wasm and publication hooks passed; targeted Standards and Spec reviews found no issues.

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_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 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::<usize>().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!(
"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());
}
} 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"));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
for (index, step) in steps.iter().enumerate() {
let step = ImportItem::<ModelStep>::deserialize(step)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
.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));
}
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(())
}

/// 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)]
Expand Down
Loading
Loading