Timeline: a reorder evaluates the design once, from the cache it already has (#6186) (#6187)

* claim: #6186

* Timeline: a reorder evaluates the design once, from the cache it already has (#6186)

timeline.reorder checked the move on a copy of the model, then dropped it,
so setting the reordered design evaluated the moved part of the timeline
again; reorder_many (and moving features into a group) checked on an empty
model, recomputing the whole design even for a move near the end. The
check now starts from the session's own model, and the session keeps the
model it was checked on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Marker is Copy: drop the clone

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
This commit is contained in:
DuBearte
2026-10-25 20:55:52 +00:00
committed by GitHub
parent 57d787f5ba
commit dba8108369
2 changed files with 59 additions and 13 deletions
+35 -13
View File
@@ -518,9 +518,14 @@ fn move_many(s: &mut Session, p: &Value) -> Result<Value> {
sort_group_features(&mut next);
let slot = num(p, "position").map(|x| x.max(0.0) as usize).ok_or_else(|| bad(cmd, "`position` must be a number"))?;
let active = active_param(p, cmd)?;
let (mut next, ordered, _) = reorder_features(s, next, &moving, slot, false, cmd)?;
let (mut next, ordered, _, checked) = reorder_features(s, next, &moving, slot, false, cmd)?;
let marker = next.marker;
place_marker(s, &mut next, &moving, active, cmd)?;
let checked = checked.filter(|_| next.marker == marker);
if next != *s.doc {
if let Some(model) = checked {
s.model = model;
}
*s.doc_mut() = next;
}
Ok(json!({"features": ordered, "groups": groups, "group": target, "position": slot}))
@@ -1175,8 +1180,15 @@ fn reorder(s: &mut Session, p: &Value) -> Result<Value> {
let list: Vec<String> = broken.iter().map(|(n, e)| format!("{n} ({e})")).collect();
return Err(bad(cmd, format!("moving {name} there breaks {}", list.join("; "))));
}
let marker = next.marker;
place_marker(s, &mut next, &[id], active, cmd)?;
if next != *s.doc {
// The check evaluated the new order: keep its results, so setting the design doesn't
// evaluate the moved part of the timeline a second time (#6186). Placing the marker
// elsewhere makes them stale.
if next.marker == marker {
s.model = after;
}
*s.doc_mut() = next;
}
let mut out = json!({"feature": id, "position": to});
@@ -1232,9 +1244,15 @@ fn reorder_many(s: &mut Session, p: &Value) -> Result<Value> {
let slot = num(p, "position").map(|x| x.max(0.0) as usize).ok_or_else(|| bad(cmd, "`position` must be a number"))?;
let active = active_param(p, cmd)?;
let allow = allow_errors(p, cmd)?;
let (mut next, ordered, broken) = reorder_features(s, (*s.doc).clone(), &ids, slot, allow, cmd)?;
let (mut next, ordered, broken, checked) = reorder_features(s, (*s.doc).clone(), &ids, slot, allow, cmd)?;
let marker = next.marker;
place_marker(s, &mut next, &ordered, active, cmd)?;
// The checked results stand only when placing the marker left the design as they were made for.
let checked = checked.filter(|_| next.marker == marker);
if next != *s.doc {
if let Some(model) = checked {
s.model = model;
}
*s.doc_mut() = next;
}
let mut out = json!({"features": ordered, "position": slot.min(s.doc.features.len())});
@@ -1244,6 +1262,8 @@ fn reorder_many(s: &mut Session, p: &Value) -> Result<Value> {
Ok(out)
}
/// The reordered design, the moved features in order, and the model the move was checked on
/// (none when the order didn't change), for the caller to keep (#6186).
fn reorder_features(
s: &Session,
mut next: solidcraft_doc::Document,
@@ -1251,7 +1271,7 @@ fn reorder_features(
slot: usize,
allow_errors: bool,
cmd: &str,
) -> Result<(solidcraft_doc::Document, Vec<u64>, Vec<(String, String)>)> {
) -> Result<(solidcraft_doc::Document, Vec<u64>, Vec<(String, String)>, Option<solidcraft_doc::Model>)> {
let n = next.features.len();
let slot = slot.min(n);
let selected: std::collections::BTreeSet<u64> = ids.iter().copied().collect();
@@ -1276,17 +1296,19 @@ fn reorder_features(
next.features.splice(insert_at..insert_at, moving);
sort_group_features(&mut next);
require_contiguous_groups(&next, cmd)?;
let mut broken = Vec::new();
if !unchanged {
let mut after = solidcraft_doc::Model::new();
after.evaluate(&next);
broken = newly_broken(&s.model, &after);
if !broken.is_empty() && !allow_errors {
let list: Vec<String> = broken.iter().map(|(name, error)| format!("{name} ({error})")).collect();
return Err(bad(cmd, format!("moving the selected features there breaks {}", list.join("; "))));
}
if unchanged {
return Ok((next, ordered, Vec::new(), None));
}
Ok((next, ordered, broken))
// From the session's own results: the features before the first one moved are kept, not
// evaluated again from an empty model (#6186).
let mut after = s.model.clone();
after.evaluate(&next);
let broken = newly_broken(&s.model, &after);
if !broken.is_empty() && !allow_errors {
let list: Vec<String> = broken.iter().map(|(name, error)| format!("{name} ({error})")).collect();
return Err(bad(cmd, format!("moving the selected features there breaks {}", list.join("; "))));
}
Ok((next, ordered, broken, Some(after)))
}
fn input_keys(kind: &FeatureKind) -> Vec<(String, solidcraft_doc::expr::Kind)> {
+24
View File
@@ -1009,3 +1009,27 @@ fn renaming_a_plane_keeps_its_projections_canvases_and_hidden_state() {
assert_eq!(s.visibility.hidden_origin, ["Rail"]);
assert_eq!(plane_link(&s), "Ledge");
}
/// #6186: a reorder evaluates the design once. The model the move was checked on is the one the
/// session keeps, so setting the reordered design finds every result ready: nothing is
/// recomputed or taken from the cache again. The same for `timeline.reorder_many`, whose check
/// starts from the session's results rather than an empty model.
#[test]
fn a_reorder_keeps_the_model_it_was_checked_on() {
let mut s = Session::default();
for i in 0..4 {
run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5 + i, "corner": [i * 10, 0, 0], "operation": "new"}));
}
let bodies = |s: &Session| s.model.state().bodies.len();
run(&mut s, "timeline.reorder", json!({"feature": "Box2", "position": 0}));
assert_eq!((s.model.last_recomputed, s.model.last_restored), (0, 0), "the checked model was kept");
assert_eq!(s.doc.features.first().map(|f| f.name.as_str()), Some("Box2"));
assert_eq!(bodies(&s), 4);
run(&mut s, "timeline.reorder_many", json!({"features": ["Box4"], "position": 2}));
assert_eq!((s.model.last_recomputed, s.model.last_restored), (0, 0), "the checked model was kept");
assert_eq!(s.doc.features.get(2).map(|f| f.name.as_str()), Some("Box4"));
// The kept model is the design's: evaluating it from scratch gives the same bodies.
let mut fresh = solidcraft_doc::Model::new();
fresh.evaluate(&s.doc);
assert_eq!(fresh.state().bodies.len(), bodies(&s));
}