Files
chenlin c32df70326
Checks / Layout, translation and Colab checks (push) Has been cancelled
Checks / Ruff (push) Has been cancelled
Checks / Framework tests (native backend, CPU) (push) Has been cancelled
Checks / Portable Rust controller (push) Has been cancelled
release: v0.1 generic robot training and application platform
2026-11-05 11:05:01 +08:00

134 lines
5.6 KiB
Python

"""The training log directory structure: `logs/<model>/<task>/<date-time>`.
Each of the three segments has its own source, and nowhere is the full path
hardcoded:
logs/<model> `tasks.paths.log_root_for`, decided by `--model`
<task> the registered robot id
<date-time> generated as a timestamp by mjlab's train entry point
mjlab composes `<log_root>/<experiment_name>/<timestamp>` (see `log_root_path` in
`rl/mjlab/scripts/train.py`), so setting `--log-root` to `logs/<model>` produces
the structure wanted -- **with no change to the vendored code**.
"""
from __future__ import annotations
from pathlib import Path
import tasks
from tasks.paths import LOGS_DIRNAME, NO_MODEL, log_root_for
REPO = Path(__file__).resolve().parents[1]
# ── logs/<model> ────────────────────────────────────────────────────────
def test_root_is_logs_slash_model_stem() -> None:
assert log_root_for("/somewhere/robot.xml") == f"{LOGS_DIRNAME}/robot"
def test_root_accepts_path_objects() -> None:
assert log_root_for(Path("assets/smoke/smoke_biped.xml")) == f"{LOGS_DIRNAME}/smoke_biped"
def test_no_asset_keeps_the_depth() -> None:
"""A task with no asset still occupies a segment, or globs like
`logs/*/<task>/` stop matching."""
assert log_root_for(None) == f"{LOGS_DIRNAME}/{NO_MODEL}"
def test_different_models_do_not_share_a_subtree() -> None:
"""Training one task against different models must split into separate
subtrees -- which is why model is the outermost segment."""
assert log_root_for("a/robot.xml") != log_root_for("b/robot_v2.xml")
def test_an_onboarded_robot_yields_a_three_segment_path(neutral_robot, tmp_path, monkeypatch):
from tasks import registry
from tasks.robot_spec import scaffold_robot_spec
monkeypatch.setattr(registry, "_REGISTRY", {})
scaffold_robot_spec(neutral_robot, tasks_root=tmp_path)
registry.discover_robots(tmp_path)
spec = tasks.get(neutral_robot.robot_id)
full = f"{log_root_for(spec.resolve_asset(None))}/{spec.id}"
assert full == f"{LOGS_DIRNAME}/{neutral_robot.model_path.stem}/{neutral_robot.robot_id}"
# ── train.py composes the whole path ──────────────────────────────────────
def test_train_composes_all_three_segments() -> None:
"""`scripts/train.py` builds `logs/<model>/<task>/<timestamp>` itself.
The log root used to be injected as `--log-root` into an argv handed to
mjlab's entry point. Now train.py owns the whole path, so this checks the
composition at its source rather than through an argv fixture.
"""
train = (REPO / "scripts" / "train.py").read_text(encoding="utf-8")
assert "log_root_for(asset)" in train, "the first segment must come from log_root_for"
assert "/ spec.id /" in train, "the second segment must be the task id"
assert "_timestamp()" in train, "the third segment must be a timestamp"
def test_no_entry_point_lives_outside_scripts() -> None:
"""Every command-line entry point is a script directly under `scripts/`.
There used to be a second set under `scripts/bridge/` that reached mjlab's own
entry points, so "how do I replay a policy" had two answers and only one of
them went through this repository's backend resolution. Now there is one.
The closed list gives each operation a single entry point. Train, play and
export use registered tasks. Deploy builds an app. Onboard prepares and
measures a new robot before task registration or app assembly.
"""
assert not (REPO / "scripts" / "bridge").exists(), (
"scripts/bridge/ is retired; entry points live directly under scripts/"
)
entries = {p.name for p in (REPO / "scripts").glob("*.py")}
assert entries == {"_cli.py", "train.py", "play.py", "export.py", "deploy.py", "onboard.py"}, entries
def test_scripts_do_not_know_any_task_s_vocabulary() -> None:
"""Entry points discover robots and use task-provided behavior.
Looking up a literal manager term in a shared entry point couples it to
one task's configuration. The check inspects executable lines and allows
looking up a term by a name returned from the manager.
"""
import re
scripts = sorted((REPO / "scripts").glob("*.py"))
def code(path) -> str:
"""The lines that run: no docstrings, no comments."""
source = path.read_text(encoding="utf-8")
source = re.sub(r'"""[\s\S]*?"""', "", source)
source = re.sub(r"'''[\s\S]*?'''", "", source)
return "\n".join(
line
for line in source.splitlines()
if line.strip() and not line.strip().startswith("#")
)
# A direct lookup by literal key would depend on one task's configuration.
reach = re.compile(r"""(get_command|get_term)\(\s*["']""")
for script in scripts:
found = reach.search(code(script))
assert found is None, (
f"{script.name} calls {found.group(0)!r} -- a manager indexed by a name "
f"spelled here, which only one task defines. Look it up from the "
f"manager, or ask the task, the way `tasks.load_play_status` does"
)
# Entry points should discover registered IDs, even in a populated checkout.
for script in scripts:
running = code(script)
hits = sorted(t for t in tasks.list_ids() if t in running)
assert not hits, (
f"{script.name} names the task(s) {hits}. A script that names one "
f"cannot be true of the next"
)