#!/usr/bin/env python3 """Supervised non-learning physics verification of the simulation reference/residual task.""" import argparse import hashlib import json import traceback from pathlib import Path from runtime_common import finish_run, write_json ROOT = Path(__file__).resolve().parents[1] def run(env, output): import torch from audit_contact_cycle_functional import evaluate from SkillFactory.legs.contact_cycle_sim.config import FEATURE_VERSION, pose_cost from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic, features from SkillFactory.tasks.manager_based.contact_cycle_sim.mdp import state from SkillFactory.tasks.manager_based.contact_cycle_sim.recording import SimCycleRecorder s = state(env) if s.robot.is_fixed_base or env.cfg.terminations.failed is None: raise RuntimeError('Require floating root and active physical failure termination') x = features(env.reset()[0]['policy']) zero = torch.zeros(env.num_envs, 3, device=env.device) gates = {'policy46': x.shape == (env.num_envs, 46), 'nominal_only': not bool(s.scenarios.any())} try: s.set_sampler(object()) except ValueError: gates['old_sampler_rejected'] = True else: gates['old_sampler_rejected'] = False rewards = [] with SimCycleRecorder(env) as recorder: initial = recorder.initial_state for k in range(900): recorder.before_control(None, zero) observation, reward, terminated, truncated, _ = env.step(zero) recorder.end_control(terminated, truncated, k == 899) if terminated.any() or (k != 899 and truncated.any()): raise RuntimeError(f'Zero-residual reference failed at control step{k}; no repaired autoresets') features(observation['policy']) rewards.append(reward.detach().cpu()) trace = {key: torch.stack([frame[key] for frame in recorder.frames]) for key in recorder.frames[0]} torch.save(trace, output / 'zero_residual_trace.pt') torch.save(initial, output / 'zero_residual_initial.pt') torch.save(torch.stack(rewards), output / 'control_rewards.pt') functional = json.loads((ROOT / 'configs/contact_cycle/simulation_functional_v1.json').read_text()) rows = [evaluate({k: v[:, e:e+1] for k, v in trace.items()}, functional, 'analytic_reference') for e in range(env.num_envs)] gates['full_functional_cycle'] = all(row['sim_functional_passed'] for row in rows) gates['zero_means_reference'] = bool((trace['effective_residual_rad'] == 0).all()) gates['no_hidden_disturbance'] = not bool(trace['external'].any()) # The complete9s trajectory was captured before the normal timeout reset, not after. gates['terminal46_at_timeout'] = env.extras['terminal_observation']['policy'].shape == (env.num_envs, 46) gates['all_timeouts_no_failures'] = bool(truncated.all()) and not bool(terminated.any()) env.reset() stored = s.controller.q_ik.clone(), s.controller.refreshes.clone(), s.controller.action.clone() for _ in range(5): env.observation_manager.compute(update_history=False) gates['observation_reads_do_not_advance_reference'] = all( torch.equal(a, b) for a, b in zip(stored, [s.controller.q_ik, s.controller.refreshes, s.controller.action])) before = s.substeps.clone() try: env.step(torch.full_like(zero, float('nan'))) except ValueError: gates['invalid_action_no_physics'] = torch.equal(before, s.substeps) else: gates['invalid_action_no_physics'] = False gates['outer_rejection_history_finite'] = bool(torch.isfinite(env.action_manager.action).all() and torch.isfinite(env.action_manager.prev_action).all()) # Low-level manager entry bypasses the outer env.step input guard and stores input before terms. try: env.action_manager.process_action(torch.full_like(zero, float('nan'))) except ValueError: gates['manager_rejection_rolls_back'] = bool(torch.isfinite(env.action_manager.action).all() and torch.equal(env.action_manager.action, env.action_manager.get_term('joint_position').raw_actions)) else: gates['manager_rejection_rolls_back'] = False gates['reward_after_rejected_input_finite'] = True # Deliberately beyond normalized actor bounds; projection must still keep <=.05rad residual. request = torch.tensor([100., -100., 100.], device=env.device).expand_as(zero) with SimCycleRecorder(env) as boundary: boundary_terminated = 0 for _ in range(20): boundary.before_control(None, request) _, reward, terminated, _, _ = env.step(request) gates['reward_after_rejected_input_finite'] &= bool(torch.isfinite(reward).all() and torch.isfinite(env.action_manager.prev_action).all()) boundary_terminated += int(terminated.sum()) bound_trace = {k: torch.stack([f[k] for f in boundary.frames]) for k in boundary.frames[0]} torch.save(bound_trace, output / 'residual_boundary_trace.pt') actual_delta = (bound_trace['processed_target'] - s.nominal.cpu() - .5 * bound_trace['reference_used_normalized']) maximum = float(actual_delta.abs().max()) gates['actual_target_residual_bound'] = maximum <= s.residual_cfg['residual_limit_rad'] + 1e-6 gates['delta_log_matches_applied_target'] = bool(torch.allclose( actual_delta, bound_trace['effective_residual_rad'], rtol=0, atol=1e-6)) others = torch.arange(1, env.num_envs, device=env.device) buffers = [s.controller.q_ik, s.controller.action, s.controller.refreshes, s.residual_request, s.reference_used, s.effective_residual_rad, s.substeps] copies = [b[others].clone() for b in buffers] env.reset(env_ids=torch.tensor([0], device=env.device, dtype=torch.int32)) gates['indexed_reset_isolated'] = all(torch.equal(old, b[others]) for old, b in zip(copies, buffers)) gates['indexed_residual_cleared'] = not bool(s.residual_request[0].any() or s.reference_used[0].any() or s.effective_residual_rad[0].any()) # A fresh untrained46D network checks the interface; no old checkpoint/optimizer is used. model = SimResidualActorCritic().to(env.device) x = features(env.reset()[0]['policy']) with torch.no_grad(): mean_action = model.act(x, deterministic=True)[0] gates['untrained_actor_is_zero_residual'] = not bool(mean_action.any()) gates['sim_truth_features_match'] = bool(torch.allclose(x[:, 32:35], s.errors()[0]) and torch.allclose(x[:, 35:38], s.errors()[1]) and torch.allclose(x[:, 38], s.contact()[:, 2])) for _ in range(2): env.step(zero) gates['reference_control_clock'] = bool((s.controller.refreshes == 3).all()) # reset + two controls c = s.residual_cfg['reward'] example_cost = pose_cost(torch.tensor([[0., 0., -.0996]]), torch.zeros(1, 3), c)[0].item() gates['sinking_not_high_reward_counterexample'] = .9255 - c['position_weight'] * example_cost < 0 from SkillFactory.tasks.manager_based.contact_cycle import mdp as old_mdp from SkillFactory.tasks.manager_based.contact_cycle.cfg import ContactCycleEnvCfg from SkillFactory.tasks.manager_based.loaded_leg.cfg import LoadedLegEnvCfg old, standing = ContactCycleEnvCfg(), LoadedLegEnvCfg() gates['historical_configs_unmodified'] = ( old.actions.joint_position.class_type is old_mdp.CycleAction and not hasattr(old.rewards, 'root_position') and old.scene is not env.cfg.scene and standing.scene.robot.init_state.pos[2] == standing.anchor_height + standing.support_parameters['initial_foot_clearance_m']) return {'passed': all(gates.values()), 'gates': gates, 'rows': rows, 'accepted_skill': False, 'sim_functional_passed': gates['full_functional_cycle'], 'controller_kind': 'analytic_reference', 'trained_residual_policy': False, 'optimizer_steps': 0, 'feature_version': FEATURE_VERSION, 'max_actual_residual_rad': maximum, 'boundary_terminated': boundary_terminated, 'zero_residual_cycle_s': 9, 'untrained_network_api_only': True, 'task_reward_minus_height_cost_at_minus99_6mm': .9255 - c['position_weight'] * example_cost, 'note': 'Nominal reference/residual interface verification, not evidence of learned-policy success.'} def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--output', type=Path, required=True) from isaaclab.app import AppLauncher AppLauncher.add_app_launcher_args(parser) args = parser.parse_args() frozen = (ROOT / 'releases').resolve() if args.output.resolve() == frozen or frozen in args.output.resolve().parents: parser.error('Verification must not write into frozen releases') args.output.mkdir(parents=True, exist_ok=False) from SkillFactory.legs.contact_cycle.training import load_training_config, source_identity old_cfg = ROOT / 'configs/contact_cycle/training_v1.json' identity = source_identity(ROOT, load_training_config(old_cfg, ROOT), old_cfg) extras = [Path(__file__), ROOT / 'scripts/audit_contact_cycle_functional.py', *list((ROOT / 'configs/contact_cycle').glob('*.json'))] identity.update({str(p.resolve().relative_to(ROOT)): hashlib.sha256(p.read_bytes()).hexdigest() for p in extras}) write_json(args.output / 'identity.json', identity) app = AppLauncher(args).app env = None result = {'passed': False, 'accepted_skill': False, 'optimizer_steps': 0} code = 1 try: from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit from SkillFactory.tasks.manager_based.contact_cycle_sim.cfg import SimContactCycleEnvCfg from SkillFactory.tasks.manager_based.contact_cycle_sim.env import SimContactCycleEnv cfg = SimContactCycleEnvCfg() cfg.scene.num_envs = 8 cfg.sim.device = args.device cfg.seed = 90311 env = SimContactCycleEnv(cfg=cfg) thresholds = json.loads((ROOT / 'configs/contact_cycle/acceptance_v1.json').read_text())['gates'] with PhysicalSafetyAudit(env, thresholds) as safety: result = run(env, args.output) safety.check() result['physics_safety'] = safety.report() if any(hashlib.sha256((ROOT / f).read_bytes()).hexdigest() != h for f, h in identity.items()): raise RuntimeError('Source changed during test') code = 0 if result['passed'] else 2 except BaseException: result['error'] = traceback.format_exc() traceback.print_exc() finally: finish_run(app, env, args.output / 'result.json', result, code) if __name__ == '__main__': main()