246 lines
11 KiB
Python
246 lines
11 KiB
Python
"""Protect the family recipes and implementation from incidental onboarding edits."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
TEMPLATES = ROOT / "tasks" / "templates"
|
|
|
|
# Refresh only when reviewing an authorized behavioral template revision.
|
|
# Include the vendored terms the templates call, so changes cannot bypass the
|
|
# lock by moving a reward, action, or randomization implementation upstream.
|
|
LOCK = {
|
|
"controller/controls.py":
|
|
"d3f887b81b0f5fbe9efebb884b5244ba934e81c072f7a9d8a0e2c95f64efb66b",
|
|
"controller/input.py":
|
|
"e3e8b77f581878cacd2cad73ec4c17a952348df2f04d03165304367a4016b62a",
|
|
"controller/vocabulary.json":
|
|
"5980699d91680ba75ee33390c87240018a61d6199d15f2f8ce9255dbca6e4d54",
|
|
"controller/vocabulary.py":
|
|
"3a810691d770d861697440d17361f054be079939874825159be65e09b6d98172",
|
|
"rl/mjlab/actuator/pd_actuator.py":
|
|
"b2c36f1759827890055cb3549aa7b441a50abd914c883fde8bbfd631e6f2f101",
|
|
"rl/mjlab/entity/entity.py":
|
|
"071815d918aa4d2412aa46998f30bbb1cdaf49a3f31aa09874df9d53a4ce7b98",
|
|
"rl/mjlab/envs/manager_based_rl_env.py":
|
|
"e5d455c8affba1fd9a9a8eb7ea138ee387a6c9741ae6058f82f6ecc08472f8ff",
|
|
"rl/mjlab/envs/mdp/actions/actions.py":
|
|
"9cae83a2b0006320a8e15e040a0edbf5b4ae76d023832563f1ac36630f3b28d1",
|
|
"rl/mjlab/envs/mdp/dr/actuator.py":
|
|
"20ecdff1cb0d590726b1f5ed9d4e9d1de5a6380ca51ec7b05c0cf2ce34812b9f",
|
|
"rl/mjlab/envs/mdp/dr/_core.py":
|
|
"8cc491dc57e64232b9ff9911cc5faa1970f2e30b954f4757f49494b1a4ee22ce",
|
|
"rl/mjlab/envs/mdp/dr/body.py":
|
|
"7bcd4565f68ead70186c82d392befc2597b5a21ec9afbb832f13a4cf7eb774d5",
|
|
"rl/mjlab/envs/mdp/dr/geom.py":
|
|
"07be4f166c68bb57f4145246be7518b8ec612614d01632e30662a36655e89e25",
|
|
"rl/mjlab/envs/mdp/dr/joint.py":
|
|
"8ca0f9626b5c8b5fedb57013d3ed229ca00fa397378cfde329ebbfe1b2617e19",
|
|
"rl/mjlab/envs/mdp/events.py":
|
|
"31051f65785d7d7818443796c5b477266fc139ad94e7d7b6df445d81ff65dd53",
|
|
"rl/mjlab/envs/mdp/observations.py":
|
|
"610b2682fada5fbd7784416e53ba3375cd9e480542737ee4bd5b1f5af79b4b72",
|
|
"rl/mjlab/envs/mdp/rewards.py":
|
|
"02e42a1e15dc1a27a0cce4e3d2863a165094914118789a1ec4cfbf6ed97a9c60",
|
|
"rl/mjlab/envs/mdp/terminations.py":
|
|
"3c0d64bdc9aa69d8fd372f343e47ab6f9da59a77d2efd8e3d860eff2c1ebf68a",
|
|
"rl/mjlab/managers/action_manager.py":
|
|
"fec79f933fd9437dc9fcb784e4f214b9bae4aad2252c2d2d1387b11b605a5332",
|
|
"rl/mjlab/managers/command_manager.py":
|
|
"e9cf2e6697722af77fa32060844819287e3a12fffb091234514b74c18762342a",
|
|
"rl/mjlab/managers/curriculum_manager.py":
|
|
"cc34f5b0897ac148d1a51503a63d987e8b549549fcb1713cd35c12e20147cf0a",
|
|
"rl/mjlab/managers/event_manager.py":
|
|
"c8d74e87aab629238ee6b4d0cb458e97a6938670fe492f76ce74bc0488401689",
|
|
"rl/mjlab/managers/observation_manager.py":
|
|
"10bebcef7cf49e9595a31bec462269faf8681eabad6ac28aa6c08ebb7111b95a",
|
|
"rl/mjlab/managers/reward_manager.py":
|
|
"a1e12ecdcacc8f90c5fe9e7bd014a6ddbc5afead45b822465257005865d9f401",
|
|
"rl/mjlab/managers/scene_entity_config.py":
|
|
"12f0ac1baf8894a6435b2733f148cb0282dab561e481e657e7324a40c1e7fe92",
|
|
"rl/mjlab/managers/termination_manager.py":
|
|
"87cc51aa9738e6d137ee936317d76d0b7472c24c58776b4f99dd1bde81d29a6a",
|
|
"rl/mjlab/rl/config.py":
|
|
"033c5e54427caff15bdf3213489e567de7831e703374bb1ab4915f58692a11e3",
|
|
"rl/mjlab/sensor/contact_sensor.py":
|
|
"8b55ab0d6a21d0256e2fc29187c00cbfe4412cd08993050386c6a1f08197fac7",
|
|
"rl/mjlab/tasks/velocity/mdp/rewards.py":
|
|
"5149776c24c8499c05c1f4941a295647779867efd645f78be02408fd85c96dfb",
|
|
"rl/mjlab/tasks/velocity/mdp/velocity_command.py":
|
|
"2aecdc17f74f6bcc281a2b5b3ae4879bcd56bb07e4c882b2f3d325b7982babfd",
|
|
"rl/mjlab/terrains/terrain_entity.py":
|
|
"272a6e1c39f4221ded391f75792982c31f6ede8f44393f8a705d6acf83be9ca1",
|
|
"rl/mjrl/tasks/actuator.py":
|
|
"56fcf2bf9e52204e02669bb10dd37c586f7d1b5b3160fbb5538ead6dde44a1bd",
|
|
"rl/mjrl/tasks/curriculum.py":
|
|
"e34c9dbf8a6ef7a93ad10d4b68687db128b527a3f8651bbdab619c36a20075d1",
|
|
"rl/mjrl/tasks/mdp.py":
|
|
"debf40561ec0a8b51deea56b2aa8fe8b3f3b24a07a2a119039a07cc0cb7678b1",
|
|
"rl/mjrl/tasks/operator.py":
|
|
"a10989e5bd0091ecce40e587824dae877dbf15d615f5fffc35e6d0e358dd89f2",
|
|
"rl/mjrl/tasks/runner.py":
|
|
"60a020d271fad67caaec5d63a1296d2716381d59737169b415b7468ebc34e3e1",
|
|
"tasks/__init__.py":
|
|
"64f5df0faac07fb5b207be3c55fe6cde0b65a253513107bb5e0ce3833ec15b86",
|
|
"tasks/registry.py":
|
|
"42285516f8365b21fc6b1a6b19dfa126cf3f7331cc8a3c00f855ffa51c4ec922",
|
|
"tasks/robot_spec.py":
|
|
"f0fc03fdc704e8fb1a2df618fe0c40224bb6c7812e514e6fc188b28c95acfcf5",
|
|
"tasks/templates/Biped_Template/AGENTS.md":
|
|
"91a6778d0cffc055e3d076df147b9e064c332c19c2beb8fe697c9260c479ee4d",
|
|
"tasks/templates/Biped_Template/__init__.py":
|
|
"9dbf382a9f5d58076fc9c81310fd69881ec550f4b391e0548c14df891ddeb6ad",
|
|
"tasks/templates/Biped_Template/env_cfg.py":
|
|
"8cfc44c601ff9db329d5ff60783c89731a043f4389c183000224681904d7ce1b",
|
|
"tasks/templates/Multipod_Template/AGENTS.md":
|
|
"8f51e0807e2ce4749cf0541d3863adf7652eb70a0918617cfb311544be8b4783",
|
|
"tasks/templates/Multipod_Template/__init__.py":
|
|
"7446cacff7e2a26fdc879f402ba84e29ed8d22e6f3fb1020afdcaa01c0c7a238",
|
|
"tasks/templates/Multipod_Template/env_cfg.py":
|
|
"7c94a284b88ea0013125d65e37325c7263222089460512bfc925fcfe607801cc",
|
|
"tasks/templates/Quadruped_Template/AGENTS.md":
|
|
"392e18f2fafa042042710297161abd7119eeee7d3ee9f03099a147c79160f844",
|
|
"tasks/templates/Quadruped_Template/__init__.py":
|
|
"71dc96fbd979f13f27db4f4a6d4fe4869b3c3572b8cc5b9d1957d63970e4573a",
|
|
"tasks/templates/Quadruped_Template/env_cfg.py":
|
|
"3a364d5118b7168723a74e68f97e0433f477dba55221e807cc1590c216dde16d",
|
|
"tasks/templates/Wheeled_Biped_Template/AGENTS.md":
|
|
"64a734ecdea5a8afa5400a0a6e1943f2d655d102ca2b332071be9017099c8c59",
|
|
"tasks/templates/Wheeled_Biped_Template/__init__.py":
|
|
"d988dedbd18f7c2817865fc5717ffe1b0102a0d5585c61ba2c743e817bfd33ba",
|
|
"tasks/templates/Wheeled_Biped_Template/env_cfg.py":
|
|
"777afc9186562b5bab382f615e248f26d6fae5587140446e84944107d7673a88",
|
|
"tasks/templates/__init__.py":
|
|
"e426b202e0457053ae2948d9194cd80e0a3149ab2b972e94b2c81396f1f89797",
|
|
"tasks/templates/_locomotion.py":
|
|
"7e25c2985b77a6ed8dfb296745ecbe98ee23fc992127967319308d539958018f",
|
|
}
|
|
|
|
|
|
def _changed(root: Path, lock: dict[str, str]) -> list[str]:
|
|
changed = []
|
|
for name, expected in lock.items():
|
|
path = root / name
|
|
if not path.is_file() or hashlib.sha256(path.read_bytes().replace(b"\r\n", b"\n")).hexdigest() != expected:
|
|
changed.append(name)
|
|
return changed
|
|
|
|
|
|
def test_locked_template_sources():
|
|
assert LOCK
|
|
assert not (changed := _changed(ROOT, LOCK)), (
|
|
f"Locked template files changed: {', '.join(changed)}. "
|
|
"Review the template revision before updating the lock."
|
|
)
|
|
|
|
|
|
def test_lock_covers_every_template_file():
|
|
actual = {
|
|
path.relative_to(ROOT).as_posix()
|
|
for path in TEMPLATES.rglob("*")
|
|
if path.is_file() and path.suffix in (".py", ".md")
|
|
}
|
|
locked = {name for name in LOCK if name.startswith("tasks/templates/")}
|
|
assert actual == locked
|
|
|
|
|
|
def test_lock_detects_a_changed_shared_dependency(tmp_path):
|
|
# The shared builder, rather than a family wrapper, is where most changes
|
|
# would land. A single-byte mutation must be reported by the same check.
|
|
name = "tasks/templates/_locomotion.py"
|
|
path = tmp_path / name
|
|
path.parent.mkdir(parents=True)
|
|
original = (ROOT / name).read_bytes()
|
|
path.write_bytes(original)
|
|
assert _changed(tmp_path, {name: LOCK[name]}) == []
|
|
path.write_bytes(original.replace(b"weight=2.0", b"weight=2.1", 1))
|
|
assert _changed(tmp_path, {name: LOCK[name]}) == [name]
|
|
|
|
|
|
def test_robot_directories_have_only_literal_python_specs():
|
|
from tasks.robot_spec import read_robot_spec
|
|
|
|
for path in (ROOT / "tasks").glob("*/robot_spec.py"):
|
|
read_robot_spec(path)
|
|
assert sorted(path.parent.rglob("*.py")) == [path]
|
|
|
|
|
|
def test_body_contacts_select_geometry_and_reject_overlap(neutral_robot):
|
|
from dataclasses import replace
|
|
|
|
pytest.importorskip("mjlab")
|
|
from tasks.templates.Biped_Template.env_cfg import env_cfg
|
|
from tasks.templates._locomotion import validate_robot_model
|
|
|
|
robot = replace(
|
|
neutral_robot,
|
|
foot_geoms=(),
|
|
foot_bodies=("leg_body_0", "leg_body_1"),
|
|
penalized_contact_geoms=("trunk",),
|
|
termination_contact_geoms=("trunk",),
|
|
)
|
|
validate_robot_model(robot)
|
|
cfg = env_cfg(robot)
|
|
sensors = {sensor.name: sensor for sensor in cfg.scene.sensors}
|
|
assert sensors["support_contacts"].primary.mode == "body"
|
|
assert cfg.events["support_friction"].params["asset_cfg"].geom_names == (
|
|
"leg_collision_0", "contact_0", "leg_collision_1", "contact_1",
|
|
)
|
|
assert cfg.rewards["undesired_contact"].params["sensor_name"] == "penalized_contacts"
|
|
assert cfg.terminations["undesired_contact"].params["sensor_name"] == "termination_contacts"
|
|
|
|
invalid = replace(robot, termination_contact_geoms=("contact_0",))
|
|
with pytest.raises(ValueError, match="support geoms cannot also be"):
|
|
validate_robot_model(invalid)
|
|
|
|
|
|
def test_fixed_foot_body_names_survive_authored_static_fusion(neutral_robot, tmp_path):
|
|
from dataclasses import replace
|
|
from xml.etree import ElementTree as ET
|
|
|
|
pytest.importorskip("mjlab")
|
|
from tasks.templates.Biped_Template.env_cfg import env_cfg
|
|
|
|
source = ET.parse(neutral_robot.model_path)
|
|
source.find("compiler").set("fusestatic", "true")
|
|
for index in range(2):
|
|
leg = source.find(f".//body[@name='leg_body_{index}']")
|
|
contact = leg.find(f"geom[@name='contact_{index}']")
|
|
leg.remove(contact)
|
|
ET.SubElement(leg, "body", name=f"foot_{index}", pos="0 0 -0.01").append(contact)
|
|
model = tmp_path / "fused.xml"
|
|
source.write(model)
|
|
|
|
robot = replace(neutral_robot, model_path=model, foot_geoms=(), foot_bodies=("foot_0", "foot_1"))
|
|
cfg = env_cfg(robot)
|
|
assert cfg.events["support_friction"].params["asset_cfg"].geom_names == ("contact_0", "contact_1")
|
|
compiled = cfg.scene.entities["robot"].spec_fn().compile()
|
|
assert compiled.body("foot_0").pos.tolist() == pytest.approx([0.0, 0.0, -0.01])
|
|
assert compiled.body("foot_1").pos.tolist() == pytest.approx([0.0, 0.0, -0.01])
|
|
|
|
|
|
def test_passive_joint_keeps_authored_nonzero_reference(neutral_robot, tmp_path):
|
|
from dataclasses import replace
|
|
from xml.etree import ElementTree as ET
|
|
|
|
pytest.importorskip("mjlab")
|
|
from tasks.templates.Biped_Template.env_cfg import env_cfg
|
|
|
|
source = ET.parse(neutral_robot.model_path)
|
|
passive = source.find(".//joint[@name='leg_1']")
|
|
passive.set("ref", "1")
|
|
passive.set("range", "0.5 1.5")
|
|
model = tmp_path / "passive.xml"
|
|
source.write(model)
|
|
robot = replace(neutral_robot, model_path=model, position_joints=neutral_robot.position_joints[:1])
|
|
entity = env_cfg(robot).scene.entities["robot"].build()
|
|
compiled = entity.spec.compile()
|
|
address = compiled.jnt_qposadr[compiled.joint("leg_1").id]
|
|
assert compiled.qpos0[address] == pytest.approx(1)
|
|
assert compiled.key("init_state").qpos[address] == pytest.approx(1)
|
|
assert compiled.nu == 1
|