241 lines
8.8 KiB
Python
241 lines
8.8 KiB
Python
"""Upstream configs provide evidence without running their Python code."""
|
|
|
|
import hashlib
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from mjrl.onboarding.upstream import extract_policy_metadata, load_overrides
|
|
|
|
_YAML = """
|
|
policy:
|
|
wire_joint_order: [hip_left, knee_left]
|
|
obs_joint_order: [knee_left, hip_left]
|
|
action_joint_order: [hip_left, knee_left]
|
|
init_state:
|
|
default_joint_angles: {hip_left: 0.0, knee_left: 0.1}
|
|
control:
|
|
action_scale: {hip_left: 0.2, knee_left: 0.3}
|
|
stiffness: 40
|
|
damping: 2
|
|
torque_limits: 12
|
|
decimation: 4
|
|
sim:
|
|
dt: 0.005
|
|
observations:
|
|
actor:
|
|
terms:
|
|
joint_vel: {history_length: 2}
|
|
joint_pos: {history_length: 2, scale: 0.5}
|
|
term_order: [joint_pos, joint_vel]
|
|
source_order: frame_major
|
|
source_history_order: newest_first
|
|
critic:
|
|
terms: [ignored_privileged_height]
|
|
normalization:
|
|
obs_scales: {dof_pos: 0.5, dof_vel: 0.2}
|
|
"""
|
|
|
|
|
|
def _config(tmp_path, name, text):
|
|
path = tmp_path / name
|
|
path.write_text(text, encoding="utf-8")
|
|
return path
|
|
|
|
|
|
def test_nested_yaml_extraction_and_provenance(tmp_path):
|
|
source = _config(tmp_path, "policy.yaml", _YAML)
|
|
report = extract_policy_metadata([source])
|
|
assert report["ready"], report
|
|
assert report["missing_fields"] == report["conflicts"] == report["unresolved"] == []
|
|
doc = report["metadata"]
|
|
assert doc["schema"] == "mjrl-policy-adapter/1"
|
|
assert doc["wire_joint_order"] == ["hip_left", "knee_left"]
|
|
assert doc["obs_joint_order"] == ["knee_left", "hip_left"]
|
|
assert doc["control"] == {"kp": 40, "kd": 2, "effort_limit": 12, "control_hz": 50}
|
|
assert doc["observation"] == {
|
|
"terms": [
|
|
{"name": "joint_pos", "history_length": 2, "scale": 0.5},
|
|
{"name": "joint_vel", "history_length": 2, "scale": 0.2},
|
|
],
|
|
"source_order": "frame_major", "source_history_order": "newest_first",
|
|
}
|
|
sha = hashlib.sha256(source.read_bytes()).hexdigest()
|
|
assert report["sources"] == [{"path": str(source), "sha256": sha, "format": "yaml"}]
|
|
evidence = report["provenance"]["control.control_hz"][0]
|
|
assert evidence["sha256"] == sha
|
|
assert [item["path"] for item in evidence["inputs"]] == [
|
|
"policy.sim.dt", "policy.control.decimation",
|
|
]
|
|
|
|
|
|
def test_canonical_metadata_roundtrips_through_yaml(tmp_path):
|
|
import yaml
|
|
|
|
source = _config(tmp_path, "policy.yaml", _YAML)
|
|
expected = extract_policy_metadata([source])["metadata"]
|
|
canonical = _config(tmp_path, "metadata.yml", yaml.safe_dump(expected))
|
|
report = extract_policy_metadata([canonical])
|
|
assert report["ready"], report
|
|
assert report["metadata"] == expected
|
|
|
|
|
|
def test_python_legged_robot_static_assignments_and_gains_do_not_execute(tmp_path):
|
|
sentinel = tmp_path / "ran.txt"
|
|
source = _config(tmp_path, "legged_robot_config.py", f"""
|
|
from pathlib import Path
|
|
from external_legged_gym import LeggedRobotCfg
|
|
import math
|
|
Path({str(sentinel)!r}).write_text("executed")
|
|
DT = 0.01 / 2
|
|
RATE_DIVISOR = 2 ** 2
|
|
KNEE_ANGLE = math.pi / 8
|
|
|
|
class BipedCfg(LeggedRobotCfg):
|
|
wire_joint_order = ["hip_left", "knee_left"]
|
|
obs_joint_order = ("knee_left", "hip_left")
|
|
action_joint_order = ["hip_left", "knee_left"]
|
|
|
|
class init_state(LeggedRobotCfg.init_state):
|
|
default_angles = {{"hip_left": 0, "knee_left": KNEE_ANGLE}}
|
|
|
|
class sim(LeggedRobotCfg.sim):
|
|
dt = DT
|
|
|
|
class control(LeggedRobotCfg.control):
|
|
decimation: int = RATE_DIVISOR
|
|
action_scale = DT * 40
|
|
stiffness = {{"hip": 40, "knee": 50}}
|
|
damping = {{"hip": 2, "knee": 3}}
|
|
effort_limit = 20
|
|
|
|
class observation:
|
|
terms = ["joint_pos", "joint_vel"]
|
|
""")
|
|
model = pytest.importorskip("mujoco").MjModel.from_xml_string("""
|
|
<mujoco><worldbody><body><joint name="hip_left" range="-90 90"/><geom size=".1"/>
|
|
<body><joint name="knee_left" range="-90 90"/><geom size=".1"/></body>
|
|
</body></worldbody></mujoco>""")
|
|
report = extract_policy_metadata([source], model=model)
|
|
assert not sentinel.exists()
|
|
assert report["ready"], report
|
|
assert report["metadata"]["control"]["kp"] == {"hip_left": 40, "knee_left": 50}
|
|
assert report["metadata"]["control"]["control_hz"] == 50
|
|
assert report["metadata"]["default_joint_pos"]["knee_left"] == pytest.approx(0.39269908169872414)
|
|
assert report["provenance"]["control.kp"][0]["path"] == "BipedCfg.control.stiffness"
|
|
assert report["provenance"]["control.kp"][0]["line"] > 1
|
|
assert any("inheritance" in issue["reason"] for issue in report["unresolved"])
|
|
|
|
|
|
def test_unknown_observation_order_and_gain_patterns_stay_unresolved(tmp_path):
|
|
source = _config(tmp_path, "policy.yaml", """
|
|
init_state:
|
|
default_joint_angles: {hip_left: 0, knee_left: 0.1}
|
|
control:
|
|
action_scale: 0.2
|
|
stiffness: {"hip.*": 40, "knee.*": 50}
|
|
damping: 2
|
|
effort_limit: 20
|
|
control_hz: 50
|
|
observation:
|
|
terms:
|
|
joint_pos: {scale: 0.4}
|
|
joint_vel: {scale: 0.1}
|
|
model:
|
|
joint_names: [hip_left, knee_left]
|
|
num_observations: 4
|
|
""")
|
|
report = extract_policy_metadata([source])
|
|
assert not report["ready"]
|
|
assert set(report["missing_fields"]) == {
|
|
"wire_joint_order", "obs_joint_order", "action_joint_order",
|
|
"control.kp", "observation.terms",
|
|
}
|
|
assert any(issue["field"] == "observation.terms" for issue in report["unresolved"])
|
|
assert any(issue["field"] == "control.kp" for issue in report["unresolved"])
|
|
override = {
|
|
"wire_joint_order": ["hip_left", "knee_left"],
|
|
"obs_joint_order": ["hip_left", "knee_left"],
|
|
"action_joint_order": ["hip_left", "knee_left"],
|
|
"kp": 40,
|
|
"observation": {"terms": [{"name": "joint_pos"}, {"name": "joint_vel"}]},
|
|
}
|
|
assert extract_policy_metadata([source], overrides=override)["ready"]
|
|
|
|
|
|
def test_conflicting_sources_and_dynamic_mutation_require_explicit_override(tmp_path):
|
|
source = _config(tmp_path, "policy.yaml", _YAML)
|
|
other = _config(tmp_path, "override.py", """
|
|
class control:
|
|
kp = 41
|
|
|
|
if choose_gains():
|
|
control.kp = build_gains()
|
|
""")
|
|
report = extract_policy_metadata([source, other])
|
|
assert not report["ready"]
|
|
conflict = next(item for item in report["conflicts"] if item["field"] == "control.kp")
|
|
assert [entry["value"] for entry in conflict["candidates"]] == [40, 41]
|
|
assert any("dynamic assignment" in item["reason"] for item in report["unresolved"])
|
|
report = extract_policy_metadata([source, other], overrides={"control": {"kp": 42}})
|
|
assert report["ready"], report
|
|
assert report["metadata"]["control"]["kp"] == 42
|
|
assert report["provenance"]["control.kp"][0]["source"] == "overrides"
|
|
|
|
|
|
def test_stack_layout_and_unsupported_term_are_visible(tmp_path):
|
|
source = _config(
|
|
tmp_path, "stack.yaml",
|
|
_YAML.replace(" source_order: frame_major\n", "").replace(
|
|
" source_history_order: newest_first\n", ""
|
|
),
|
|
)
|
|
report = extract_policy_metadata([source])
|
|
assert not report["ready"]
|
|
assert {"observation.source_history_order", "observation.source_order"} <= {
|
|
item["field"] for item in report["unresolved"]
|
|
}
|
|
unsupported = _config(tmp_path, "unsupported.yaml", _YAML.replace("joint_vel", "foot_scan"))
|
|
report = extract_policy_metadata([unsupported])
|
|
assert not report["ready"]
|
|
assert any("unsupported observation term 'foot_scan'" in item["reason"] for item in report["unresolved"])
|
|
|
|
|
|
def test_known_unsupported_action_and_observation_transforms_are_visible(tmp_path):
|
|
source = _config(
|
|
tmp_path, "raw.yaml",
|
|
_YAML.replace(" stiffness: 40", " control_type: T\n stiffness: 40")
|
|
.replace(" normalization:", " normalization:\n clip_observations: 100"),
|
|
)
|
|
report = extract_policy_metadata([source])
|
|
assert not report["ready"]
|
|
assert {"action", "observation.terms"} <= {issue["field"] for issue in report["unresolved"]}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("name", "content"),
|
|
[
|
|
("duplicate.json", '{"kp": 1, "kp": 2}'),
|
|
("duplicate.yaml", "kp: 1\nkp: 2\n"),
|
|
("bool.yaml", "true: 5\n"),
|
|
("infinite.yaml", "kp: .inf\n"),
|
|
("recursive.yaml", "cfg: &cycle [*cycle]\n"),
|
|
("unsafe.yaml", "kp: !!python/object/apply:os.system ['echo unsafe']\n"),
|
|
],
|
|
)
|
|
def test_unsafe_or_ambiguous_documents_never_create_ready_metadata(tmp_path, name, content):
|
|
source = _config(tmp_path, name, content)
|
|
report = extract_policy_metadata([source])
|
|
assert not report["ready"] and report["unresolved"][0]["blocking"]
|
|
json.dumps(report, allow_nan=False)
|
|
with pytest.raises(ValueError):
|
|
load_overrides(source)
|
|
|
|
|
|
def test_override_yaml_and_static_python_boundary(tmp_path):
|
|
config = _config(tmp_path, "override.yml", "control:\n kp: 60\n")
|
|
assert load_overrides(config) == {"control": {"kp": 60}}
|
|
script = _config(tmp_path, "override.py", "kp = 60\n")
|
|
with pytest.raises(ValueError, match="expected a JSON or YAML"):
|
|
load_overrides(script)
|