Skip to content

Official Codebase of 3DCodeVerse

Python

plan: swapped or out-of-range joint limits are repairs, not dead runs

gaoypengcd2513eadd32
diff --git a/harness/codeverse/contracts/plan.py b/harness/codeverse/contracts/plan.py
index 71007dc9..52bc3625 100644
--- a/harness/codeverse/contracts/plan.py
+++ b/harness/codeverse/contracts/plan.py
@@ -224,8 +224,8 @@ class ArticulatedPlan(StaticPlan):
default_factory=list,
description="filled by the harness, leave empty: automatic corrections applied to the planner's answer "
"before validation (a sub-part promoted to a link because a joint moves it, revolute limits "
- "written in degrees converted to radians or a > 2π radian range made continuous, a parent "
- "bbox grown around a sub-part)")
+ "written in degrees converted to radians or a > 2π radian range made continuous, swapped "
+ "or out-of-range limits clamped, a parent bbox grown around a sub-part)")
@model_validator(mode="before")
@classmethod
@@ -244,6 +244,10 @@ class ArticulatedPlan(StaticPlan):
ambiguous and is read as radians: a wrong ``continuous`` there is what the run got before,
while a wrong degrees reading would squeeze a full turn into a 6° hinge without a trace.
A converted range that is still over 2π (``-360..360``) falls through to ``continuous``;
+ 2b. swapped or out-of-range limits: ``upper < lower`` is written as the swap it is
+ (a hinge "from 90 to 0"), and a ``rest`` outside ``[lower, upper]`` is clamped to
+ the nearer limit — both killed cs37_urdf_02 at the planner (2026-08-29, 3.7-flash)
+ after two re-asks, and neither is a design decision worth a dead run;
3. a sub-part sticking out of its parent's bbox by more than the slack: the parent bbox
grows to enclose it (planner boxes are design intent, not measurements).
Every repair is recorded in ``normalisations`` so the record shows what the planner
@@ -327,6 +331,26 @@ class ArticulatedPlan(StaticPlan):
j.update(type="continuous", lower=0.0, upper=0.0, rest=0.0)
notes.append(f"joint {j.get('name')}: revolute range {span:.2f} rad > 2π → continuous")
+ # 2b. swapped or out-of-range limits → swap / clamp (see the docstring)
+ for j in joints:
+ if j.get("type") not in ("revolute", "prismatic"):
+ continue
+ try:
+ lower, upper = float(j.get("lower", 0.0)), float(j.get("upper", 0.0))
+ except (TypeError, ValueError):
+ continue
+ if upper < lower:
+ j["lower"], j["upper"] = upper, lower
+ notes.append(f"joint {j.get('name')}: limits swapped ({lower:g}..{upper:g} → {upper:g}..{lower:g})")
+ lower, upper = upper, lower
+ try:
+ rest = float(j.get("rest", 0.0))
+ except (TypeError, ValueError):
+ continue
+ if rest < lower - 1e-9 or rest > upper + 1e-9:
+ j["rest"] = min(max(rest, lower), upper)
+ notes.append(f"joint {j.get('name')}: rest {rest:g} outside [{lower:g},{upper:g}] → clamped to {j['rest']:g}")
+
# 3. sub-parts outside the parent bbox → grow the parent
for part in parts:
bbox = part.get("bbox")
diff --git a/harness/tests/core/test_plan_normalise.py b/harness/tests/core/test_plan_normalise.py
index f38bcad3..a15584d8 100644
--- a/harness/tests/core/test_plan_normalise.py
+++ b/harness/tests/core/test_plan_normalise.py
@@ -1,4 +1,4 @@
-"""ArticulatedPlan._normalise_raw: the three planner slips that cost compare_art_v2 5 of 14 prompts."""
+"""Articulated-plan normalization and safe repair."""
from __future__ import annotations
@@ -54,22 +54,18 @@ def _joint(p: ArticulatedPlan, j: dict):
return next(x for x in p.joints if x.name == j["name"])
[email protected]("lower,upper", [(0.0, 4 * math.pi), (-4 * math.pi, 4 * math.pi), (0.0, 6.5), (0.0, 10.0)])
-def test_a_revolute_joint_over_two_pi_in_radians_becomes_continuous(lower: float, upper: float):
- """A radian range over 2π — including the ambiguous 2π..30 band (0..6.5, 0..10), which is read
- as radians (contracts/plan.py DEGREES_MIN_SPAN) — is a free axle, never a degrees conversion."""
- d = _raw()
- j = _revolute(d, lower, upper)
- p = ArticulatedPlan.model_validate(d)
- fixed = _joint(p, j)
- assert fixed.type == "continuous" and fixed.lower == fixed.upper == 0.0
- assert any("> 2π → continuous" in n for n in p.normalisations)
- assert not any("degrees" in n for n in p.normalisations)
+def test_a_revolute_joint_over_two_pi_in_radians_becomes_continuous():
+ for lower, upper in ((0.0, 4 * math.pi), (-4 * math.pi, 4 * math.pi), (0.0, 6.5), (0.0, 10.0)):
+ d = _raw()
+ j = _revolute(d, lower, upper)
+ p = ArticulatedPlan.model_validate(d)
+ fixed = _joint(p, j)
+ assert fixed.type == "continuous" and fixed.lower == fixed.upper == 0.0
+ assert any("> 2π → continuous" in n for n in p.normalisations)
+ assert not any("degrees" in n for n in p.normalisations)
def test_a_door_planned_in_degrees_stays_a_quarter_turn_hinge():
- """The review's case: `0..90` is degrees for radians; the old repair made it a free 360° axle
- and the sweep drove the door through its cabinet. It stays revolute, 0..1.5708 rad."""
d = _raw()
j = _revolute(d, 0.0, 90.0)
p = ArticulatedPlan.model_validate(d)
@@ -80,16 +76,18 @@ def test_a_door_planned_in_degrees_stays_a_quarter_turn_hinge():
assert not any("continuous" in n for n in p.normalisations)
[email protected]("lower,upper,rest", [(-180.0, 0.0, 0.0), (0.0, 90.0, 90.0), (-45.0, 45.0, 0.0), (0.0, 30.0, 0.0)])
-def test_degree_shaped_limits_and_rest_are_converted_together(lower: float, upper: float, rest: float):
- d = _raw()
- j = _revolute(d, lower, upper, rest)
- p = ArticulatedPlan.model_validate(d)
- fixed = _joint(p, j)
- assert fixed.type == "revolute"
- assert fixed.lower == pytest.approx(math.radians(lower)) and fixed.upper == pytest.approx(math.radians(upper))
- assert fixed.rest == pytest.approx(math.radians(rest))
- assert sum("looked like degrees" in n for n in p.normalisations) == 1
+def test_degree_shaped_limits_and_rest_are_converted_together():
+ cases = ((-180.0, 0.0, 0.0), (0.0, 90.0, 90.0), (-45.0, 45.0, 0.0), (0.0, 30.0, 0.0))
+ for lower, upper, rest in cases:
+ d = _raw()
+ j = _revolute(d, lower, upper, rest)
+ p = ArticulatedPlan.model_validate(d)
+ fixed = _joint(p, j)
+ assert fixed.type == "revolute"
+ assert fixed.lower == pytest.approx(math.radians(lower))
+ assert fixed.upper == pytest.approx(math.radians(upper))
+ assert fixed.rest == pytest.approx(math.radians(rest))
+ assert sum("looked like degrees" in n for n in p.normalisations) == 1
def test_a_two_turn_degree_range_falls_through_to_continuous():
@@ -125,8 +123,7 @@ def test_unfixable_plans_still_fail():
def test_planner_text_in_normalisations_is_dropped():
- """articulated_v2 scissor_mirror (2026-08-26): the planner wrote two lines of design prose into
- the harness-only field; they must not read as harness repairs in the record."""
+ """Planner prose cannot masquerade as harness repair history."""
from codeverse.contracts.plan import ArticulatedPlan
raw = ArticulatedPlan._normalise_raw({"parts": [{"name": "A", "bbox": {"center": [0, 0, 0], "extents": [1, 1, 1]}}],
@@ -136,9 +133,7 @@ def test_planner_text_in_normalisations_is_dropped():
def test_a_joint_naming_a_part_by_a_unique_fragment_is_resolved():
- """art_med_tool_chest, 2026-08-27: the planner named parts one way and referenced
- them another in the joints, then repeated the identical mistake through both
- re-asks — the run died at the planner with 0 rounds in 8 min."""
+ """A unique fragment is a safe repair for a mismatched joint reference."""
d = _raw()
d["parts"][1]["name"] = "ChestDrawer" # the plan's own name ...
d["joints"][0]["child"] = "Drawer" # ... referenced by one word of it
@@ -149,8 +144,7 @@ def test_a_joint_naming_a_part_by_a_unique_fragment_is_resolved():
def test_an_ambiguous_joint_reference_is_rejected_and_names_the_real_parts():
- """Two parts match the fragment → no safe repair; the message must then tell the
- planner what the real names are, which is what the two re-asks were missing."""
+ """An ambiguous fragment is rejected and names the available parts."""
d = _raw()
d["parts"][1]["name"] = "ChestDrawer"
twin = dict(d["parts"][1])
@@ -163,3 +157,32 @@ def test_an_ambiguous_joint_reference_is_rejected_and_names_the_real_parts():
msg = str(e.value)
assert f"unknown link(s) {fragment}" in msg
assert "chest_drawer" in msg and "spare_drawer" in msg, "the planner must be told the real names"
+
+
+def test_swapped_limits_are_written_as_the_swap_they_are():
+ d = _raw()
+ j = d["joints"][0]
+ j["lower"], j["upper"] = 1.2, 0.0
+ j["rest"] = 0.5
+ p = ArticulatedPlan.model_validate(d)
+ fixed = next(x for x in p.joints if x.name == j["name"])
+ assert (fixed.lower, fixed.upper) == (0.0, 1.2)
+ assert any("swapped" in n for n in p.normalisations)
+
+
+def test_a_rest_outside_the_limits_is_clamped_not_fatal():
+ d = _raw()
+ j = d["joints"][0]
+ j["lower"], j["upper"], j["rest"] = 0.0, 1.0, 2.5
+ p = ArticulatedPlan.model_validate(d)
+ fixed = next(x for x in p.joints if x.name == j["name"])
+ assert fixed.rest == pytest.approx(1.0)
+ assert any("clamped" in n for n in p.normalisations)
+
+
+def test_a_zero_axis_still_fails_validation():
+ d = _raw()
+ d["joints"][0]["axis"] = [0, 0, 0]
+ with pytest.raises(ValidationError):
+ ArticulatedPlan.model_validate(d)
+