Skip to content
Open
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
2 changes: 2 additions & 0 deletions crates/engine/src/cmd/geolibre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ fn import_project(s: &mut Session, p: &Value) -> CmdResult {
s.wait_in_place_reads();
order.reverse();
s.map_mut().layers.sort_by_key(|l| order.iter().position(|id| *id == l.id).unwrap_or(usize::MAX));
// Its own direct `request_import_url` calls (a remote layer left for the app) set the flag, but
// this import edited the project: it keeps its undo step.
s.request_only = false;
s.map_mut().camera = project.camera;
s.map_mut().bookmarks = project.bookmarks;
Expand Down
10 changes: 8 additions & 2 deletions crates/engine/src/cmd/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,13 @@ impl Session {
bridge.s.quiet_failure = true;
return Err(String::new());
}
// Nor is a command that only left downloads for the front end: they add their
// layers, each its own undo step, when they land.
// Nor is a command that only left downloads for the front end: they add their layers, each
// its own undo step, when they land. Any other plugin command that may edit keeps its step
// even if it changed nothing, as a built-in command that mutates does unless it says it
// changed nothing, which a plugin's answer can't (`a_pending_answer_is_no_undo_step`); two
// tests pin it (`plugin_settings_are_kept_per_plugin_across_sessions_updates_and_toggles`,
// `shape_conversion_plugin_handled_rejection_is_atomic_and_has_no_inner_undo`). A script or
// task takes none when no line edited (see #2622).
let steps = bridge.s.shared_steps;
bridge.s.request_only = res.is_ok() && (state_command || steps.requested) && !steps.edited;
res
Expand Down Expand Up @@ -1095,6 +1100,7 @@ impl Host for Between<'_> {
let res = s.guarded(leak(&plugin.manifest.name), mutates, |s| {
let (res, changed, requested) = bridge(s);
// Only left downloads for the app (they add their layers when they land): no step.
// The same rule as a plugin command's (see `run_plugin`).
s.request_only = res.is_ok() && requested && !changed;
// The answer goes to the program, which says what it makes of a refusal.
s.quiet_failure = res.is_err();
Expand Down
6 changes: 5 additions & 1 deletion crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,8 @@ pub struct Session {
/// Set by [`Session::request_url`] for [`Session::guarded`]: the command running changed
/// nothing, it only left a request (a plugin's own answer can't claim that). Also set by a move
/// that left everything where it was (`layout.arrange`, `layer.move`): no step to undo.
/// Each command has its own: [`Session::guarded`] clears it on entry and puts the enclosing
/// command's back on exit.
request_only: bool,
/// What the commands run as steps of the transaction running now did ([`Session::shared_step`]):
/// the one account scripts, tasks and plugins decide their undo step from.
Expand Down Expand Up @@ -2356,7 +2358,8 @@ impl Session {
self.restorable += u32::from(mutates);
// Edit counts roll back with the project when a command fails part way.
let counts = mutates.then(|| self.edit_counts.clone());
self.request_only = false;
// Its flag is its own: the enclosing command's comes back after it, whatever it left.
let outer_request_only = std::mem::take(&mut self.request_only);

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.

Possible regression, low confidence (about 30%), not reproduced.

Before this change, a non-mutating nested command's "left a request" flag leaked up to whatever step ran it. shared_step reads that flag as unchanged and feeds shared_steps.requested and edited. Now guarded_inner restores the enclosing flag on exit, so a Shared step (Txn::Shared in dispatch.rs) whose handler runs a non-mutating nested command that only left a download will see unchanged == false. If the step is declared mutates, edited becomes true, and the script or plugin then takes an empty undo step, where it previously took none.

The PR description says "Nothing would read it", but shared_step (lib.rs ~2316) does read the signal. I did not find a shipped handler that combines a non-mutating nested request with a mutating step, so the impact may be hypothetical. A test would settle it: a Registered mutating command whose handler calls s.guarded("x", false, |s| s.request_url(..)) and makes no edits, run as a script line. Expected result: no undo step. If that case is intended to stay "no step", the isolation needs to carry the nested signal up to shared_steps.requested rather than dropping it.

Category: bug

// Its steps' account is its own: an enclosing command's comes back after it.
let outer_steps = std::mem::take(&mut self.shared_steps);
self.quiet_failure = false;
Expand Down Expand Up @@ -2513,6 +2516,7 @@ impl Session {
}
_ => {}
}
self.request_only = outer_request_only;
if res.is_err() {
self.restore_url_request_checkpoint(requests);
(self.plugin_retries, self.url_waits) = retry_state;
Expand Down
4 changes: 1 addition & 3 deletions crates/engine/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,9 +843,7 @@ fn run_into(s: &mut Session, g: &ModelGraph, report: &mut RunReport) -> Result<V
s.active_layer = s.map().layers.first().map(|l| l.id);
}
// Queued bytes alone are request-only; completed tools still need the model's undo step.
if tool_completed && file_pending {
s.request_only = false;
}
s.request_only = file_pending && !tool_completed;
Ok(ran)
}

Expand Down
97 changes: 97 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23809,6 +23809,90 @@ fn a_pending_answer_is_no_undo_step() {
assert_eq!(s.take_url_requests().len(), 1);
}

/// A command's "changed nothing" flag is its own: a nested command that only left a request (not
/// mutating, so nothing takes the flag) or set it and failed (the caller handles the error) must
/// not make the command that ran it skip its undo step (#2637).
#[test]
fn a_nested_commands_request_flag_is_not_the_enclosing_commands() {
let mut s = Session::new(Project::default());
s.map_mut().name = "Before".into();
let undo = s.undo.len();
let url = json!({"url": "https://x.org/a.geojson"});
let r = s.guarded("Outer", true, |s| {
s.guarded("Quiet request", false, |s| s.request_url(&url))?;
s.map_mut().name = "After".into();
Ok(Value::Null)
});
assert!(r.is_ok());
assert_eq!(s.undo.len(), undo + 1, "the outer edit takes its step");
assert!(s.undo());
assert_eq!(s.map().name, "Before");
// Set and then failed (`layer.addUrl` for a download added to another map), and the outer
// command went on.
let undo = s.undo.len();
let r = s.guarded("Outer", true, |s| {
let failed = s.guarded("Failed request", true, |s| {
s.request_only = true;
Err("no".to_string())
});
assert!(failed.is_err());
s.map_mut().name = "After".into();
Ok(Value::Null)
});
assert!(r.is_ok());
assert_eq!(s.undo.len(), undo + 1, "a failed nested request doesn't hide the outer edit");
// The command's own claim still stands after a nested run.
let undo = s.undo.len();
let r = s.guarded("Outer", true, |s| {
s.guarded("Quiet", false, |_| Ok(Value::Null))?;
s.request_only = true;
Ok(Value::Null)
});
assert!(r.is_ok());
assert_eq!(s.undo.len(), undo, "a command that left only a request takes no step");
}

/// A nested command that sets the flag and then panics: the panic becomes its error, the flag goes
/// with it, and the enclosing command that goes on keeps its undo step (#2637).
#[test]
fn a_nested_panic_leaves_the_enclosing_commands_request_flag_alone() {
let mut s = Session::new(Project::default());
s.map_mut().name = "Before".into();
let undo = s.undo.len();
let r = s.guarded("Outer", true, |s| {
let panicked = s.guarded("Panics", true, |s| {
s.request_only = true;
panic!("nested command panicked");
});
assert!(panicked.is_err());
s.map_mut().name = "After".into();
Ok(Value::Null)
});
assert!(r.is_ok());
assert_eq!(s.undo.len(), undo + 1, "the outer edit keeps its step");
assert!(s.undo());
assert_eq!(s.map().name, "Before");
}

/// A script whose lines leave a request (`layer.addUrl` held for the app, no network) and then edit
/// is one undo step; one that only left the request has none (#2637).
#[test]
fn a_script_that_leaves_a_request_and_then_edits_is_one_undo_step() {
let mut s = Session::new(Project::default());
s.leave_downloads = true;
let undo = s.undo.len();
let ask = r#"layer.addUrl {"url": "https://x.org/a.geojson"}"#;
s.run("script.run", &json!({"script": ask})).unwrap();
assert_eq!(s.take_url_requests().len(), 1);
assert_eq!(s.undo.len(), undo, "a request alone is no step");
let maps = s.project.maps.len();
s.run("script.run", &json!({"script": format!("{ask}\nmap.new {{\"name\": \"B\"}}")})).unwrap();
assert_eq!(s.take_url_requests().len(), 1);
assert_eq!(s.undo.len(), undo + 1, "the edit after the request takes the script's step");
assert!(s.undo());
assert_eq!(s.project.maps.len(), maps);
}

/// A command that left a request for the front end and then failed takes the request back with
/// its other changes: nothing is downloaded for a command that reported failure.
#[test]
Expand Down Expand Up @@ -47756,6 +47840,19 @@ fn geolibre_download_finishes_in_its_original_map_and_position() {
assert_eq!(s.project.maps[target].layer(id).unwrap().dataset.features.len(), 1);
}

/// The import calls `request_import_url` itself, which sets the flag in the import's own scope: it
/// edited the project, so it keeps its undo step (#2637).
#[test]
fn geolibre_import_that_leaves_a_download_is_one_undo_step() {
let mut s = world();
s.leave_downloads = true;
let undo = s.undo.len();
let text = json!({"version":"0.2.0","name":"Remote","mapView":{},"basemapStyleUrl":"","layers":[{"id":"g","name":"Remote","type":"geojson","source":{"url":"https://example.org/a.geojson"}}]}).to_string();
s.run("project.importGeolibre", &json!({"text": text})).unwrap();
assert_eq!(s.take_url_requests().len(), 1);
assert_eq!(s.undo.len(), undo + 1, "the import takes its step");
}

#[test]
fn geolibre_tile_sources_keep_visibility_opacity_and_order() {
let mut s = world();
Expand Down
Loading