Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Timeline marker: refuse a negative position, and name the command asked for (#4712) (#4749)

dudedagger4284cad431544f
diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs
index 351020f574..713ae2c395 100644
--- a/crates/engine/src/cmd/edit.rs
+++ b/crates/engine/src/cmd/edit.rs
@@ -42,7 +42,7 @@ pub static COMMANDS: &[CommandSpec] = &[
"features?: [id|name], groups?: [folder ids], group?: folder id, position: insertion slot, active?: bool (true = the moved contents end up before the history marker, false = behind it; other features keep their state); atomically changes folder membership and moves all selected contents as one block",
),
CommandSpec::new("timeline.rollback", "Move Timeline Marker", rollback).icon("rollback").params("position: number of features to keep active, or \"end\" (omit = end)"),
- CommandSpec::new("timeline.roll_to", "Roll History Marker", rollback).icon("rollback")
+ CommandSpec::new("timeline.roll_to", "Roll History Marker", roll_to).icon("rollback")
.params("position: number of features to keep active, or \"end\" (omit = end) | feature: id|name (the marker goes right after it)"),
CommandSpec::new("timeline.reorder", "Reorder Feature", reorder).icon("reorder")
.params("feature: id|name, position: its new index, active?: bool (true = it ends up before the history marker, false = behind it; other features keep their state); refused when a feature that worked would fail"),
@@ -501,10 +501,19 @@ fn move_many(s: &mut Session, p: &Value) -> Result<Value> {
}
fn rollback(s: &mut Session, p: &Value) -> Result<Value> {
+ move_marker(s, p, "timeline.rollback")
+}
+
+fn roll_to(s: &mut Session, p: &Value) -> Result<Value> {
+ move_marker(s, p, "timeline.roll_to")
+}
+
+/// `timeline.rollback` and `timeline.roll_to`, refusing in the name of the one asked for (#4712).
+fn move_marker(s: &mut Session, p: &Value, cmd: &str) -> Result<Value> {
let n = s.doc.features.len();
let pos = match p.get("feature") {
Some(f) => {
- let id = feature_id(s, Some(f), "timeline.roll_to")?;
+ let id = feature_id(s, Some(f), cmd)?;
s.doc.feature_index(id).map(|i| i + 1)
}
// A number of features, or "end" (as omitting it); anything else is refused rather than
@@ -515,7 +524,13 @@ fn rollback(s: &mut Session, p: &Value) -> Result<Value> {
Some(v) => {
let x = v.as_f64().or_else(|| v.as_str().and_then(|t| t.trim().parse::<f64>().ok())).and_then(crate::params::finite);
let why = "`position` must be a number of features to keep active, or \"end\"";
- Some(x.map(|x| x.max(0.0) as usize).ok_or_else(|| bad("timeline.roll_to", why))?)
+ let x = x.ok_or_else(|| bad(cmd, why))?;
+ // A negative position was taken as 0, rolling the whole timeline back: an agent
+ // meaning "one step back" by -1 lost every body without a word (#4712).
+ if x < 0.0 {
+ return Err(bad(cmd, format!("`position` {x} is negative: it counts the features to keep active, 0 to {n} (or \"end\")")));
+ }
+ Some(x as usize)
}
},
};
diff --git a/crates/engine/src/cmd/edit_tests.rs b/crates/engine/src/cmd/edit_tests.rs
index 15b60ec680..d9c9734759 100644
--- a/crates/engine/src/cmd/edit_tests.rs
+++ b/crates/engine/src/cmd/edit_tests.rs
@@ -27,6 +27,16 @@ fn roll_to_takes_a_number_or_end_and_refuses_anything_else() {
assert_eq!(s.doc.marker, Some(1), "{bad}: the refused call left the marker where it was");
}
assert_eq!(run(&mut s, cmd, json!({}))["marker"], 2, "omitted: the end");
+ // #4712: a negative position is refused (it rolled the whole timeline back), in the
+ // name of the command asked for.
+ run(&mut s, cmd, json!({"position": 1}));
+ for neg in [json!(-1), json!("-1"), json!(-0.5)] {
+ let e = s.execute(cmd, &json!({"position": neg})).unwrap_err().to_string();
+ assert!(e.contains("negative") && e.contains(&format!("`{cmd}`")), "{neg}: {e}");
+ assert_eq!(s.doc.marker, Some(1), "{neg}: the marker stays");
+ }
+ let e = s.execute(cmd, &json!({"position": "start"})).unwrap_err().to_string();
+ assert!(e.contains(&format!("`{cmd}`")), "named as asked: {e}");
}
}