From 646eb97eaf79880439e3b58dc5241aefbc794b30 Mon Sep 17 00:00:00 2001 From: Dahak Builds Date: Fri, 30 Oct 2026 23:03:40 +0000 Subject: [PATCH] motion.list gives study keys in motion.study units (#9741) (#9760) * claim: #9741 * motion.list gives study keys in motion.study units Angle keys came back in radians while motion.study takes degrees, so an agent that listed a study and wrote it back changed every angle by a factor of 57.3, silently. Convert key values on the way out like joint.list does (degrees for angles, mm for distances), give each key a unit and its joint's name, and add a round-trip regression test. --------- Co-authored-by: Dahak Builds --- crates/engine/src/cmd/motion.rs | 25 ++++++++++++++++++++++- crates/engine/src/cmd/motion_tests.rs | 29 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/motion.rs b/crates/engine/src/cmd/motion.rs index 2580e8c7b..0e1e65a92 100644 --- a/crates/engine/src/cmd/motion.rs +++ b/crates/engine/src/cmd/motion.rs @@ -979,7 +979,30 @@ fn keep_sampled_trace( } fn list_studies(s: &mut Session, _p: &Value) -> Result { - Ok(json!({"studies": s.doc.assembly.studies, "traces": associative::list(s)})) + let studies: Vec = s.doc.assembly.studies.iter().map(|m| study_json(s, m)).collect(); + Ok(json!({"studies": studies, "traces": associative::list(s)})) +} + +/// A study as `motion.list` gives it: key values in the units `motion.study` takes — degrees +/// for angles, mm for distances, like `joint.list`'s values — each key with its joint's name, +/// so a listed study can be fed back to `motion.study` unchanged. +fn study_json(s: &Session, m: &MotionStudy) -> Value { + let keys: Vec = m + .keys + .iter() + .map(|k| { + let joint = s.doc.assembly.joints.iter().find(|j| j.id == k.joint); + let angle = joint.is_some_and(|j| j.kind.is_angle(k.index)); + json!({ + "joint": k.joint, + "joint_name": joint.map(|j| j.name.clone()).unwrap_or_else(|| k.joint.to_string()), + "index": k.index, + "unit": if angle { "deg" } else { "mm" }, + "points": k.points.iter().map(|(step, v)| json!([step, if angle { v.to_degrees() } else { *v }])).collect::>(), + }) + }) + .collect(); + json!({"id": m.id, "name": m.name, "steps": m.steps, "keys": keys}) } fn delete_study(s: &mut Session, p: &Value) -> Result { diff --git a/crates/engine/src/cmd/motion_tests.rs b/crates/engine/src/cmd/motion_tests.rs index f4e88fb36..558dbb48d 100644 --- a/crates/engine/src/cmd/motion_tests.rs +++ b/crates/engine/src/cmd/motion_tests.rs @@ -639,6 +639,35 @@ fn motion_study_plays_and_exports() { assert_eq!(run(&mut s, "motion.list", json!({}))["studies"].as_array().map(Vec::len), Some(0)); } +/// Regression (#9741): `motion.study` takes degrees for angle keys, so `motion.list` must give +/// the keys back in those same units — feeding a listed study straight back to `motion.study` +/// must not turn a 90° key into 1.57°. +#[test] +fn motion_list_round_trips_key_units_through_motion_study() { + let hinge_deg = |s: &mut Session| { + run(s, "joint.list", json!({}))["joints"] + .as_array() + .and_then(|a| a.iter().find(|j| j["name"] == "Hinge").cloned()) + .and_then(|j| j["values"][0]["value"].as_f64()) + .unwrap_or(f64::NAN) + }; + let mut s = hinged_plates(0.0); + run(&mut s, "motion.study", json!({"name": "Turn", "steps": 10, "keys": [{"joint": "Hinge", "points": [[0, 0], [10, 90]]}]})); + run(&mut s, "motion.play", json!({"study": "Turn", "step": 10})); + assert!((hinge_deg(&mut s) - 90.0).abs() < 1e-6, "playing step 10 gave {}°", hinge_deg(&mut s)); + let listed = run(&mut s, "motion.list", json!({})); + let key = &listed["studies"][0]["keys"][0]; + assert_eq!(key["joint_name"], "Hinge"); + assert_eq!(key["unit"], "deg"); + assert!((key["points"][1][1].as_f64().unwrap_or(f64::NAN) - 90.0).abs() < 1e-6, "{listed}"); + // The listed keys go back to `motion.study` unchanged and the study still plays 90°. + let keys = listed["studies"][0]["keys"].clone(); + run(&mut s, "motion.study", json!({"study": "Turn", "keys": keys})); + run(&mut s, "motion.play", json!({"study": "Turn", "step": 0})); + run(&mut s, "motion.play", json!({"study": "Turn", "step": 10})); + assert!((hinge_deg(&mut s) - 90.0).abs() < 1e-6, "the round-tripped study played {}°", hinge_deg(&mut s)); +} + #[test] fn exploded_views_show_without_changing_the_design() { let mut s = rail();