"""CPU-only frozen replay interfaces and failure artifacts; no Kit or optimizer.""" import copy import importlib.util import json import sys from pathlib import Path from types import SimpleNamespace import pytest import torch from SkillFactory.legs.contact_cycle_sim import replay from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "scripts")) import replay_cycle_sim_residual as cli @pytest.fixture(autouse=True) def cpu_only(monkeypatch): old = torch.get_num_threads() torch.set_num_threads(1) def forbidden(*args, **kwargs): raise AssertionError("Optimizer construction is forbidden in frozen replay") monkeypatch.setattr(torch.optim.Adam, "__init__", forbidden) yield torch.set_num_threads(old) def write_json(path, data): path.parent.mkdir(parents=True, exist_ok=True) path.write_text(json.dumps(data)) @pytest.fixture def bundle(tmp_path, monkeypatch): """Synthetic trusted root; production CLI never exposes hash override flags.""" root, prepared = tmp_path / "repo", tmp_path / "frozen" root.mkdir() prepared.mkdir() config_path = "configs/contact_cycle/simulation_residual_v1.json" files = {"AGENT.md": b"instructions", "source/runtime.py": b"runtime", config_path: (ROOT / config_path).read_bytes()} for relative, payload in files.items(): for base in (root, prepared / "source_snapshot"): path = base / relative path.parent.mkdir(parents=True, exist_ok=True) path.write_bytes(payload) for relative in replay.REPLAY_SOURCES: path = root / relative path.parent.mkdir(parents=True, exist_ok=True) path.write_text("synthetic replay source") manifest = {"contract": replay.CONTRACT, "task_id": replay.TASK_ID, "features": replay.FEATURE_VERSION, "model_config": replay.MODEL_CONFIG, "training": {"task_config": config_path}, "sources": {p: replay.sha256(v) for p, v in files.items()}} write_json(prepared / "manifest.json", manifest) monkeypatch.setattr(replay, "MANIFEST_SHA256", replay.sha256(replay.canonical(manifest).encode())) policy = SimResidualActorCritic(**replay.MODEL_CONFIG) with torch.no_grad(): policy.actor[-1].bias.copy_(torch.tensor([.1, -.2, .3])) data = {"format": replay.CHECKPOINT_FORMAT, "contract": replay.CONTRACT, "features": replay.FEATURE_VERSION, "model_config": replay.MODEL_CONFIG, "update": 256, "accepted_skill": False, "model": policy.state_dict(), "identity": {"manifest": manifest, "model_config": replay.MODEL_CONFIG, "seed": 91501}, "optimizer": {"ignored": "must never instantiate or restore"}, "rng": {"ignored": "must never restore"}} checkpoint = prepared / "candidate_u0256.pt" torch.save(data, checkpoint) monkeypatch.setattr(replay, "CANDIDATE_SHA256", replay.sha256(checkpoint.read_bytes())) monkeypatch.setattr(cli, "ROOT", root) return SimpleNamespace(root=root, prepared=prepared, checkpoint=checkpoint, manifest=manifest, data=data) def test_frozen_loading_is_inference_only_and_deterministic(bundle): before = bundle.checkpoint.read_bytes() manifest, binding = replay.verify_bundle(bundle.root, bundle.checkpoint) rng = torch.get_rng_state().clone() policy = replay.load_frozen_policy(bundle.checkpoint, manifest) assert torch.equal(rng, torch.get_rng_state()) assert not policy.training and not any(p.requires_grad for p in policy.parameters()) x = torch.zeros(2, 46) action, mean, std = replay.inference_action(x, "learned", policy) assert torch.equal(action, policy.act(x, deterministic=True)[0]) assert torch.equal(action, mean.tanh()) and action.any() and std.shape == (3,) assert not action.requires_grad assert torch.equal(action, replay.inference_action(x, "learned", policy)[0]) assert bundle.checkpoint.read_bytes() == before assert binding["snapshot_files_verified"] == 3 and not binding["live_source_exceptions"] assert set(binding["replay_sources"]) == set(replay.REPLAY_SOURCES) def test_agent_live_drift_is_the_only_explicit_exception(bundle): (bundle.root / "AGENT.md").write_text("changed live instructions") _, binding = replay.verify_bundle(bundle.root, bundle.checkpoint) exception, = binding["live_source_exceptions"] assert exception["path"] == "AGENT.md" and exception["reason"] assert exception["snapshot_sha256"] == bundle.manifest["sources"]["AGENT.md"] assert exception["live_sha256"] == replay.sha256(b"changed live instructions") @pytest.mark.parametrize("relative", ["AGENT.md", "source/runtime.py", "configs/contact_cycle/simulation_residual_v1.json"]) def test_any_snapshot_tampering_fails_including_agent(bundle, relative): (bundle.prepared / "source_snapshot" / relative).write_text("tampered") with pytest.raises(ValueError, match="snapshot mismatch"): replay.verify_bundle(bundle.root, bundle.checkpoint) @pytest.mark.parametrize("relative", ["source/runtime.py", "configs/contact_cycle/simulation_residual_v1.json"]) def test_live_runtime_and_config_drift_fail_closed(bundle, relative): (bundle.root / relative).write_text("changed") with pytest.raises(ValueError, match="Live bound source/config mismatch"): replay.verify_bundle(bundle.root, bundle.checkpoint) def test_manifest_tampering_rejected(bundle): write_json(bundle.prepared / "manifest.json", {**bundle.manifest, "features": "policy32"}) with pytest.raises(ValueError, match="manifest SHA256"): replay.verify_bundle(bundle.root, bundle.checkpoint) def test_candidate_sha_checked_before_deserialization(bundle, monkeypatch): bundle.checkpoint.write_bytes(b"not the frozen candidate") monkeypatch.setattr(torch, "load", lambda *a, **k: pytest.fail("Must verify SHA before deserialization")) with pytest.raises(ValueError, match="candidate SHA256"): replay.load_frozen_policy(bundle.checkpoint, bundle.manifest) with pytest.raises(ValueError, match="candidate SHA256"): replay.verify_bundle(bundle.root, bundle.checkpoint) @pytest.mark.parametrize("fault", ["legacy", "features32", "update0", "update_bool", "model_config", "manifest", "seed", "shape32", "dtype", "nan", "normalization", "keys"]) def test_checkpoint_contract_rejects_incompatible_payload(bundle, monkeypatch, fault): data = copy.deepcopy(bundle.data) if fault == "legacy": data["format"] = "old32" elif fault == "features32": data["features"] = "policy32" elif fault == "update0": data["update"] = 0 elif fault == "update_bool": data["update"] = True elif fault == "model_config": data["model_config"]["hidden"] = 32 elif fault == "manifest": data["identity"]["manifest"] = {} elif fault == "seed": data["identity"]["seed"] = 1 elif fault == "shape32": data["model"]["actor.0.weight"] = torch.zeros(128, 32) elif fault == "dtype": data["model"]["actor.0.weight"] = data["model"]["actor.0.weight"].double() elif fault == "nan": data["model"]["actor.0.bias"][0] = float("nan") elif fault == "normalization": data["model"]["scale"][0] += 1 else: del data["model"]["log_std"] torch.save(data, bundle.checkpoint) monkeypatch.setattr(replay, "CANDIDATE_SHA256", replay.sha256(bundle.checkpoint.read_bytes())) with pytest.raises(ValueError): replay.load_frozen_policy(bundle.checkpoint, bundle.manifest) @pytest.mark.parametrize("shape,dtype", [((1, 32), torch.float32), ((0, 46), torch.float32), ((1, 46), torch.float64), ((46,), torch.float32)]) def test_invalid_observation_never_migrated(shape, dtype): with pytest.raises(ValueError, match="46"): replay.inference_action(torch.zeros(shape, dtype=dtype), "reference") def test_invalid_actions_and_missing_policy_never_fall_back(): x = torch.zeros(1, 46) with pytest.raises(ValueError, match="fallback"): replay.inference_action(x, "learned") for action in (None, [float("nan"), 0, 0], [float("inf"), 0, 0], [1.01, 0, 0], [0, 0]): with pytest.raises(ValueError, match="Script requires"): replay.inference_action(x, "script", script_action=action) x[0, 0] = float("nan") with pytest.raises(ValueError): replay.inference_action(x, "reference") def test_controller_identity_cannot_confuse_script_reference_and_learned(): identities = [replay.controller_identity("learned"), replay.controller_identity("reference"), replay.controller_identity("script", [.5, 0, -.5])] assert len({row["controller_kind"] for row in identities}) == 3 assert [row["trained_residual_policy"] for row in identities] == [True, False, False] assert all(row["optimizer_steps"] == 0 and not row["accepted_skill"] for row in identities) x = torch.zeros(2, 46) assert not replay.inference_action(x, "reference")[0].any() assert torch.equal(replay.inference_action(x, "script", script_action=[.5, 0, -.5])[0], torch.tensor([[.5, 0, -.5]]).expand(2, 3)) with pytest.raises(ValueError): replay.inference_action(x, "reference", policy=object()) with pytest.raises(ValueError): replay.controller_identity("learned", [0, 0, 0]) @pytest.mark.parametrize("protected", ["releases/run", "logs/contact_cycle/sim_residual_training_v1/new"]) def test_output_cannot_modify_historical_trees(bundle, protected): with pytest.raises(ValueError, match="independent"): replay.create_output(bundle.root, bundle.root / protected, bundle.checkpoint) assert not (bundle.root / protected).exists() def test_output_exclusive_and_symlink_protected(bundle, tmp_path): output = replay.create_output(bundle.root, tmp_path / "new", bundle.checkpoint) (output / "evidence").write_text("preserve") with pytest.raises(FileExistsError): replay.create_output(bundle.root, output, bundle.checkpoint) assert (output / "evidence").read_text() == "preserve" link = tmp_path / "link" link.symlink_to(bundle.prepared, target_is_directory=True) with pytest.raises(ValueError, match="independent"): replay.create_output(bundle.root, link / "new", bundle.checkpoint) def test_cli_preflight_is_cpu_only_and_preserves_failure(bundle, tmp_path, monkeypatch): # A fake launcher turns any accidental Kit path into a test failure. monkeypatch.setitem(sys.modules, "isaaclab.app", SimpleNamespace( AppLauncher=lambda *a, **k: pytest.fail("CPU preflight imported Kit"))) output = tmp_path / "check" args = ["--check-only", "--output", str(output), "--checkpoint", str(bundle.checkpoint)] assert cli.main(args) == 0 result = json.loads((output / "result.json").read_text()) assert result["preflight_passed"] and not result["physics_executed"] assert result["trained_residual_policy"] and result["optimizer_steps"] == 0 assert (output / "identity.json").is_file() bundle.checkpoint.write_bytes(b"bad checkpoint") failed = tmp_path / "failure" args[2] = str(failed) assert cli.main(args) == 1 result = json.loads((failed / "result.json").read_text()) assert not result["passed"] and not result["physics_executed"] assert result["controller_mode"] == "learned" and "SHA256 mismatch" in result["error"] assert (output / "result.json").is_file() def test_cli_bad_script_recorded_without_launch(bundle, tmp_path): output = tmp_path / "bad_script" assert cli.main(["--replay", "--controller", "script", "--checkpoint", str(bundle.checkpoint), "--output", str(output)]) == 1 result = json.loads((output / "result.json").read_text()) assert not result["physics_executed"] and not result["passed"] def synthetic_recorder(): spec = importlib.util.spec_from_file_location( "synthetic_functional", ROOT / "tests/test_contact_cycle_functional.py") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) trace = module.example() from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES for name, shape in {"policy_snapshot": (46,), "actor_input": (46,), "requested_action": (3,), "actor_mean": (3,), "processed_target": (3,), "reference_used_normalized": (3,), "effective_residual_rad": (3,)}.items(): trace[name] = torch.zeros(4500, 1, *shape) trace["failure_flags"] = torch.zeros(4500, 1, len(FAILURE_NAMES), dtype=torch.bool) trace["would_fail"] = torch.zeros(4500, 1, dtype=torch.bool) recorder = SimpleNamespace(initial_state={k: v[0] for k, v in trace.items()}, stops=[4500], early=[False], frames=[{k: v[i] for k, v in trace.items()} for i in range(4500)]) return recorder, module.CFG @pytest.mark.parametrize("mode", ["reference", "script", "learned"]) def test_recording_full_six_phases_and_explicit_controller(tmp_path, mode): recorder, functional = synthetic_recorder() if mode == "learned": for frame in recorder.frames: frame["actor_metadata_available"].fill_(True) rows = cli.record_episodes(recorder, tmp_path, mode, functional, torch.zeros(3), [{"reason": "timeout", "control_step": 900}]) assert rows[0]["success"] and all(rows[0]["phase_samples"].values()) trace = torch.load(tmp_path / "first_episodes.pt", weights_only=True)["0"] assert {"normal", "gap", "root_position_error_m", "root_velocity_error_m_s", "requested_action", "reference_used_normalized", "effective_residual_rad", "failure_flags"} <= trace.keys() assert len(trace["time"]) == 4500 and rows[0]["terminal_causes"] == [] assert json.loads((tmp_path / "episodes.json").read_text())["controller_mode"] == mode def test_recording_keeps_failure_and_never_splices_reset_episode(tmp_path): recorder, functional = synthetic_recorder() recorder.stops, recorder.early = [12], [True] recorder.frames[10]["failure_flags"][0, 0] = True rows = cli.record_episodes(recorder, tmp_path, "reference", functional, torch.zeros(3), [{"reason": "physical_failure", "control_step": 3}]) assert not rows[0]["success"] and rows[0]["terminal_causes"] assert rows[0]["first_failure_time_s"] == pytest.approx(.022) trace = torch.load(tmp_path / "first_episodes.pt", weights_only=True)["0"] assert len(trace["time"]) == 12 def test_recording_exception_partial_trace_stays_failed(tmp_path): recorder, functional = synthetic_recorder() recorder.stops = [None] recorder.frames = recorder.frames[:7] rows = cli.record_episodes(recorder, tmp_path, "reference", functional, torch.zeros(3), [{"reason": "incomplete", "control_step": None}]) assert not rows[0]["success"] and rows[0]["first_episode_samples"] == 7 assert (tmp_path / "first_episodes.pt").is_file() def test_actual_frozen_candidate_cpu_only_when_artifacts_present(): checkpoint = ROOT / replay.CANDIDATE if not checkpoint.is_file(): pytest.skip("Frozen training artifacts must be copied separately; never synthesize a candidate") manifest, binding = replay.verify_bundle(ROOT, checkpoint) policy = replay.load_frozen_policy(checkpoint, manifest) action, _, _ = replay.inference_action(torch.zeros(1, 46), "learned", policy) assert action.shape == (1, 3) and torch.isfinite(action).all() assert binding["candidate_sha256"] == "c088bb3aecaca751867118858626fd508f7a3983303802d7524353f80802cf99" def test_snapshot_symlink_rejected_even_with_identical_bytes(bundle, tmp_path): path = bundle.prepared / "source_snapshot/AGENT.md" copy_path = tmp_path / "same_bytes" copy_path.write_bytes(path.read_bytes()) path.unlink() path.symlink_to(copy_path) with pytest.raises(ValueError, match="Unsafe bound source"): replay.verify_bundle(bundle.root, bundle.checkpoint) def test_cli_startup_failure_is_not_success_or_reference_fallback(bundle, tmp_path, monkeypatch): def fail_launcher(**kwargs): raise RuntimeError("synthetic launcher failure, no Kit instantiated") monkeypatch.chdir(tmp_path) monkeypatch.setitem(sys.modules, "isaaclab.app", SimpleNamespace(AppLauncher=fail_launcher)) output = tmp_path / "startup_failure" assert cli.main(["--replay", "--output", str(output), "--checkpoint", str(bundle.checkpoint)]) == 1 result = json.loads((output / "result.json").read_text()) assert result["preflight_passed"] and not result["passed"] and not result["physics_executed"] assert result["controller_mode"] == "learned" and "synthetic launcher failure" in result["error"] def test_cli_constructor_failure_records_possible_physics_before_entry(bundle, tmp_path, monkeypatch): output = tmp_path / "constructor_failure" closed = [] monkeypatch.chdir(tmp_path) manifest = {**bundle.manifest, "training": {**bundle.manifest["training"], "validation_seed": 91502}} monkeypatch.setattr(replay, "verify_bundle", lambda *args: (manifest, {})) monkeypatch.setattr(replay, "load_frozen_policy", lambda *args: object()) monkeypatch.setitem(sys.modules, "isaaclab.app", SimpleNamespace( AppLauncher=lambda **kwargs: SimpleNamespace(app=SimpleNamespace( close=lambda **kwargs: closed.append(kwargs))))) monkeypatch.setitem(sys.modules, "SkillFactory.tasks.manager_based.contact_cycle.recording", SimpleNamespace(PhysicalSafetyAudit=object)) monkeypatch.setitem(sys.modules, "SkillFactory.tasks.manager_based.contact_cycle_sim.cfg", SimpleNamespace( SimContactCycleEnvCfg=lambda **kwargs: SimpleNamespace(scene=SimpleNamespace(), sim=SimpleNamespace()))) def fail_constructor(**kwargs): # Persisted before entry, including if construction never returns to Python. entered = json.loads((output / "result.json").read_text()) assert entered["initialization_entered"] is True assert entered["physics_executed"] is None and entered["status"] == "initializing" raise RuntimeError("synthetic constructor failure after possible PhysX warmup") monkeypatch.setitem(sys.modules, "SkillFactory.tasks.manager_based.contact_cycle_sim.env", SimpleNamespace(SimContactCycleEnv=fail_constructor)) assert cli.main(["--replay", "--output", str(output), "--checkpoint", str(bundle.checkpoint)]) == 1 result = json.loads((output / "result.json").read_text()) assert result["initialization_entered"] is True and result["physics_executed"] is None assert result["status"] == "failed" and not result["passed"] assert result["controller_mode"] == "learned" and "synthetic constructor failure" in result["error"] assert closed == [{"exit_code": 1}] @pytest.mark.parametrize("fault", ["physical_failure", "step_exception", "guard_exception"]) def test_run_episode_failure_saves_partial_recording_without_second_episode(tmp_path, monkeypatch, fault): recorder, functional = synthetic_recorder() templates = recorder.frames recorder.frames = [] recorder.stops, recorder.early = [None], [False] class FakeRecorder: def __init__(self, env): self.__dict__.update(recorder.__dict__) def __enter__(self): return self def __exit__(self, *args): return False def before_control(self, *args): pass def end_control(self, terminated, truncated, final): if bool((terminated | truncated).any()): self.stops[0], self.early[0] = len(self.frames), True calls = {"step": 0, "reset": 0, "guard": 0} def reset(): calls["reset"] += 1 return {"policy": torch.zeros(1, 46)}, {} def step(action): calls["step"] += 1 start = len(recorder.frames) recorder.frames.extend(templates[start:start + 5]) if fault == "step_exception" and calls["step"] == 2: raise RuntimeError("synthetic step failure") terminated = torch.tensor([fault == "physical_failure" and calls["step"] == 2]) return {"policy": torch.zeros(1, 46)}, torch.zeros(1), terminated, torch.tensor([False]), {} def guard(): calls["guard"] += 1 if fault == "guard_exception" and calls["guard"] == 4: raise RuntimeError("synthetic safety guard failure") state = SimpleNamespace(robot=SimpleNamespace(is_fixed_base=False), scenarios=torch.zeros(1), stand_only=torch.zeros(1, dtype=torch.bool), nominal=torch.zeros(3)) monkeypatch.setitem(sys.modules, "SkillFactory.tasks.manager_based.contact_cycle_sim.mdp", SimpleNamespace(state=lambda env: state)) monkeypatch.setitem(sys.modules, "SkillFactory.tasks.manager_based.contact_cycle_sim.recording", SimpleNamespace(SimCycleRecorder=FakeRecorder)) cfg = SimpleNamespace(sim=SimpleNamespace(physics=SimpleNamespace( solver_type=0, enable_external_forces_every_iteration=False)), terminations=SimpleNamespace(failed=object()), episode_length_s=9) env = SimpleNamespace(cfg=cfg, physics_dt=.002, step_dt=.01, num_envs=1, reset=reset, step=step) args = SimpleNamespace(controller="reference", script_action=None, output=tmp_path) if fault == "physical_failure": result = cli.run_episode(env, args, None, functional, guard) assert not result["passed"] and result["control_steps"] == 2 else: with pytest.raises(RuntimeError, match="synthetic"): cli.run_episode(env, args, None, functional, guard) assert calls["reset"] == 1 and calls["step"] == 2 rows = json.loads((tmp_path / "episodes.json").read_text())["rows"] assert not rows[0]["success"] and rows[0]["first_episode_samples"] == 10 assert rows[0]["termination"]["reason"] == ("physical_failure" if fault == "physical_failure" else "exception") assert (tmp_path / "first_episodes.pt").is_file()