From cfb5fd618c227752d753910fd294086abc2aa2a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 22 Nov 2026 10:20:23 -0500 Subject: [PATCH 1/2] fix(engine): the declaration check reports past a task's catch (#2696) invoke notes a command declared as not mutating that changed the layers on the session, and command_epilogue raises it when the top-level command returns, whichever entry ran it (run, a prepared run, a read ahead finishing, a continuation), and drops it on a panic so it never reaches the next command. A task step's catch_unwind no longer swallows it. Tests cover a command by itself, a task's step, a script's line and a plugin's host call. --- crates/engine/src/cmd/plugins.rs | 22 +++++++++++ crates/engine/src/lib.rs | 16 +++++++- crates/engine/src/policy/dispatch.rs | 6 ++- crates/engine/src/tests.rs | 59 +++++++++++++++++++--------- 4 files changed, 82 insertions(+), 21 deletions(-) diff --git a/crates/engine/src/cmd/plugins.rs b/crates/engine/src/cmd/plugins.rs index 165350128c..13c114babc 100644 --- a/crates/engine/src/cmd/plugins.rs +++ b/crates/engine/src/cmd/plugins.rs @@ -1381,6 +1381,28 @@ mod tests { assert_ne!(edited.layers, undone.layers); } + /// The declaration check sees a plugin's host call too (#2696): a command declared as not + /// mutating that adds a layer is noted, and fails the command the user ran when it returns. + #[test] + fn a_host_call_that_changes_layers_is_noted() { + let pkg = rustgis_plugins::Package::from_zip(&rustgis_plugins::testing::package("eng-declared", &["edit"])).unwrap(); + rustgis_plugins::load(pkg, "test").unwrap(); + let plugin = rustgis_plugins::get("eng-declared").unwrap(); + let mut s = Session::new(crate::Project::default()); + s.registry.commands.push( + crate::CommandSpec::new("test.quietMap", "Quiet map", "Test", |s, _| { + crate::cmd::layer::add_dataset(s, rustgis_data::Dataset::new("Quiet", rustgis_data::GeometryType::Point, vec![])); + Ok(Value::Null) + }) + .view(), + ); + let fetches = fetch::Calls::for_plugin("eng-declared"); + let mut bridge = Bridge { s: &mut s, plugin, fetches }; + let _ = bridge.call("test.quietMap", &json!({})); + assert!(bridge.s.declaration_broken.as_deref().is_some_and(|e| e.contains("test.quietMap"))); + rustgis_plugins::unload("eng-declared"); + } + /// A command that only left a download for the front end changed nothing, and a later one /// doesn't hide an earlier edit: the plugin's command keeps its undo step. #[test] diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index b9cc75fd12..e66af84683 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -972,6 +972,11 @@ pub struct Session { /// A task is executing: its steps may not start another task, even through scripts. /// Kept across project replacement until the active call returns; never part of undo. pub(crate) task_running: bool, + /// A command declared as not mutating changed the layers (AGENTS.md): raised when the top-level + /// command returns ([`Session::command_epilogue`], every entry), past any step's catch (a + /// task's), so the tests fail on it. + #[cfg(any(test, feature = "declaration-check"))] + pub(crate) declaration_broken: Option, /// Models made in this session (lower-cased names); the steps of others may not touch files. trusted_models: policy::Trust, /// What the last model run did, node by node (`model::RunReport`), kept even when the run @@ -1481,6 +1486,8 @@ impl Session { browse_response: None, trusted_tasks: Default::default(), task_running: false, + #[cfg(any(test, feature = "declaration-check"))] + declaration_broken: None, trusted_models: Default::default(), model_run: None, project_starts: 0, @@ -1949,8 +1956,15 @@ impl Session { self.merge = None; self.beyond_said = None; } + // Taken on every top-level return, a panic's too, so it never reaches the next command. + #[cfg(any(test, feature = "declaration-check"))] + let broken = outermost.then(|| self.declaration_broken.take()).flatten(); match result { - Ok(result) => result, + Ok(result) => { + #[cfg(any(test, feature = "declaration-check"))] + assert!(broken.is_none(), "{}", broken.unwrap_or_default()); + result + } Err(payload) => std::panic::resume_unwind(payload), } } diff --git a/crates/engine/src/policy/dispatch.rs b/crates/engine/src/policy/dispatch.rs index 4c10e21699..c13ba2f632 100644 --- a/crates/engine/src/policy/dispatch.rs +++ b/crates/engine/src/policy/dispatch.rs @@ -1010,8 +1010,10 @@ impl Session { crate::cmd::project::take_first_layer_system(self, &before); } #[cfg(any(test, feature = "declaration-check"))] - if let Some(before) = declared { - assert!(self.restored().layers == before, "{} doesn't mutate but changed the layers", spec.id); + if let Some(before) = declared + && self.restored().layers != before + { + self.declaration_broken.get_or_insert_with(|| format!("{} doesn't mutate but changed the layers", spec.id)); } self.url_completion_landed(spec.id, ¶ms, &result); if result.is_ok() && (spec.mutates || spec.effects.contains(crate::policy::Effect::History)) && spec.id != "data.similar" { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 4716190df8..c28a03623a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -417,38 +417,61 @@ fn shared_step_measures_the_layers_a_step_changes() { let mut s = world(); let spec = *s.registry.get("map.zoomFull").unwrap(); let (count, ds) = (s.map().layers.len(), s.map().layers[0].dataset.clone()); - let quiet_layer = |s: &mut Session| -> crate::CmdResult { + let add_layer = |s: &mut Session| -> crate::CmdResult { crate::cmd::layer::add_dataset(s, ds); Ok(Value::Null) }; - s.shared_step(&spec, quiet_layer).unwrap(); + s.shared_step(&spec, add_layer).unwrap(); assert_eq!((s.shared_steps.edited, s.map().layers.len()), (true, count + 1)); } +/// A command declared as not mutating that adds a layer, marks the project unsaved, makes an edit +/// on the way and then fails: none of it excuses the layer it added. +fn quiet_layer() -> crate::CommandSpec { + crate::CommandSpec::new("test.quietLayer", "Quiet layer", "Test", |s, _| { + let ds = s.map().layers[0].dataset.clone(); + crate::cmd::layer::add_dataset(s, ds); + s.mark_unsaved(); + s.guarded("Rename", true, |s| { + s.map_mut().name = "Renamed on the way".into(); + Ok(Value::Null) + })?; + Err("failed after adding it".into()) + }) + .view() +} + /// A command declared as not mutating that adds, removes or reorders layers fails the tests -/// (#2645), by itself or as a script's line; an edit it makes on the way, marking the project -/// unsaved or failing afterwards doesn't excuse it. A layer's name, look or features aren't -/// measured. +/// (#2645). A layer's name, look or features aren't measured. #[test] #[should_panic(expected = "test.quietLayer doesn't mutate but changed the layers")] fn a_command_that_changes_layers_must_say_it_mutates() { let mut s = world(); - s.registry.commands.push( - crate::CommandSpec::new("test.quietLayer", "Quiet layer", "Test", |s, _| { - let ds = s.map().layers[0].dataset.clone(); - crate::cmd::layer::add_dataset(s, ds); - s.mark_unsaved(); - s.guarded("Rename", true, |s| { - s.map_mut().name = "Renamed on the way".into(); - Ok(Value::Null) - })?; - Err("failed after adding it".into()) - }) - .view(), - ); + s.registry.commands.push(quiet_layer()); let _ = s.run("test.quietLayer", &Value::Null); } +/// The same as a task's step, whose panics the task catches: the check is raised when the command +/// the user ran returns, not inside the step (#2696). +#[test] +#[should_panic(expected = "test.quietLayer doesn't mutate but changed the layers")] +fn a_task_step_that_changes_layers_must_say_it_mutates() { + let mut s = world(); + s.registry.commands.push(quiet_layer()); + let steps = json!([{"title": "Quiet", "command": "test.quietLayer", "params": {}}]); + s.run("task.new", &json!({"name": "Quiet", "steps": steps})).unwrap(); + let _ = s.run("task.run", &json!({"task": "Quiet"})); +} + +/// And as a script's line (#2696). +#[test] +#[should_panic(expected = "test.quietLayer doesn't mutate but changed the layers")] +fn a_script_line_that_changes_layers_must_say_it_mutates() { + let mut s = world(); + s.registry.commands.push(quiet_layer()); + let _ = s.run("script.run", &json!({"script": "test.quietLayer {}"})); +} + /// A task's steps are counted as a script's lines: an undo step only when one edited (#2621). #[test] fn task_takes_an_undo_step_only_when_a_step_edits() { From 159f1127f49d8ffe9fdb79adadd37da77e746914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 22 Nov 2026 12:13:33 -0500 Subject: [PATCH 2/2] fix(engine): a scoped script raises its lines' declaration check (#2696) --- crates/engine/src/policy/dispatch.rs | 11 +++++++++-- crates/engine/src/tests.rs | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/policy/dispatch.rs b/crates/engine/src/policy/dispatch.rs index c13ba2f632..6d0c433689 100644 --- a/crates/engine/src/policy/dispatch.rs +++ b/crates/engine/src/policy/dispatch.rs @@ -517,11 +517,18 @@ impl Session { } let node = self.admit(node)?; // Its lines run nested: what they couldn't read as true or false is said here (#1600). - self.with_unread_bools(Some("script.run"), |s| { + let result = self.with_unread_bools(Some("script.run"), |s| { s.with_node(node, |s| { s.guarded("Run Script", true, |s| crate::cmd::workflows::run_script(s, &json!({"script":script}))) }) - }) + }); + // Not a command, so no `command_epilogue`: its lines' declaration check is raised here. + #[cfg(any(test, feature = "declaration-check"))] + if self.depth == 0 { + let broken = self.declaration_broken.take(); + assert!(broken.is_none(), "{}", broken.unwrap_or_default()); + } + result } CallKind::Adapter { .. } => { let node = self.call_node(&self.scope(), call)?; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index c28a03623a..b0c773636c 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -472,6 +472,23 @@ fn a_script_line_that_changes_layers_must_say_it_mutates() { let _ = s.run("script.run", &json!({"script": "test.quietLayer {}"})); } +/// And as a line of a script an agent or the assistant runs, which doesn't go through +/// `command_epilogue` (#2696). +#[test] +#[should_panic(expected = "test.quietLayer doesn't mutate but changed the layers")] +fn a_scoped_script_line_that_changes_layers_must_say_it_mutates() { + struct Live; + impl crate::policy::Revocation for Live { + fn allows(&self, _: crate::policy::Effects) -> bool { + true + } + } + let mut s = world(); + s.registry.commands.push(quiet_layer()); + let scope = crate::policy::Scope::agent(crate::policy::AgentAccess::Full, true, "declared", None, std::sync::Arc::new(Live)); + let _ = s.run_scoped(scope, &crate::policy::Call::script("test.quietLayer {}")); +} + /// A task's steps are counted as a script's lines: an undo step only when one edited (#2621). #[test] fn task_takes_an_undo_step_only_when_a_step_edits() {