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
22 changes: 22 additions & 0 deletions crates/engine/src/cmd/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
}

/// 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]
Expand Down
16 changes: 15 additions & 1 deletion crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// 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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
}
}
Expand Down
17 changes: 13 additions & 4 deletions crates/engine/src/policy/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -1010,8 +1017,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));
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
}
self.url_completion_landed(spec.id, &params, &result);
if result.is_ok() && (spec.mutates || spec.effects.contains(crate::policy::Effect::History)) && spec.id != "data.similar" {
Expand Down
76 changes: 58 additions & 18 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,38 +417,78 @@ 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 {}"}));
}

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