#!/usr/bin/env python3 """CPU-only function-first trace audit. Never starts Kit or grants execution. Checks one or three *globally clocked* cycles; never joins separate episodes. A tensor trace cannot prove the absence of simulator state writes. Therefore trace_functional_passed is NOT a runtime-continuity or new simulation claim. """ import argparse import hashlib import importlib import importlib.util import json import sys from pathlib import Path import torch from audit_contact_cycle_functional import evaluate as evaluate_cycle ROOT = Path(__file__).resolve().parents[1] PHASES = ('support', 'unload', 'lift', 'swing', 'landing', 'reload') FAILURES = ('nonfinite', 'contact_acquisition_timeout', 'premature_separation', 'lift_deadline', 'swing_clearance', 'reload_deadline', 'low_height', 'tilt') DEFAULT_CONFIG = ROOT / 'configs/contact_cycle/functional_flow_v2.json' def cpu_protocol(): """Private CPU namespace only; NEVER a runtime/binding import alias.""" name = '_functional_flow_cpu' directory = ROOT / 'source/SkillFactory/SkillFactory/legs/contact_cycle_sim' if name not in sys.modules: spec = importlib.util.spec_from_file_location(name, directory / '__init__.py', submodule_search_locations=[str(directory)]) module = importlib.util.module_from_spec(spec) sys.modules[name] = module spec.loader.exec_module(module) return importlib.import_module(name + '.functional_flow_v2') def root_error(trace): for key in ('root_position_error_m', 'root_position_error'): if key in trace: return trace[key] if 'policy_snapshot' in trace and trace['policy_snapshot'].shape[-1] == 46: return trace['policy_snapshot'][..., 32:35] return None def phase_summary(trace): result = {} error = root_error(trace) for phase, name in enumerate(PHASES): mask = trace['phase'][:, 0] == phase if not mask.any(): result[name] = {'samples': 0, 'status': 'not_evaluated'} continue fn = trace['normal'][mask, 0, 2] gap = trace['gap'][mask, 0] foot = trace['foot'][mask, 0] result[name] = { 'samples': int(mask.sum()), 'normal_N_min_mean_max': [float(fn.min()), float(fn.mean()), float(fn.max())], 'mean_load_W': float((fn / trace['weight'][mask, 0]).mean()), 'gap_m_min_max': [float(gap.min()), float(gap.max())], 'foot_net_xyz_m': (foot[-1] - foot[0]).tolist(), 'root_xy_error_max_m': None if error is None else float(error[mask, 0, :2].norm(dim=-1).max()), } return result def evaluate_flow(trace, cfg, controller_kind, cycles): api = cpu_protocol() api.validate_contract(cfg) protocol = api.FunctionalFlowProtocol(cfg, cycles=cycles, controller_kind=controller_kind) out = {'format': cfg['format'], 'controller_kind': controller_kind, 'requested_cycles': cycles, 'trace_functional_passed': False, 'runtime_continuity_verified': False, 'accepted_skill': False, 'new_simulation_steps': 0, 'new_optimizer_steps': 0, 'gates': {}, 'cycles': [], 'limitation': 'Offline tensor evidence only. Full runtime pass additionally requires fresh ' 'no-autoreset hooks, seam carry-state, budget and clean terminal evidence.'} if 'time' not in trace or not trace['time'].numel(): out['error'] = 'No physics samples' return out required = {'time', 'phase', 'scenario', 'weight', 'normal', 'foot', 'target', 'gap', 'signed_height_error', 'tilt', 'external', 'guide', 'auxiliary', 'joint_excess', 'limit_ratio', 'contact_acquired', 'actor_metadata_available', 'control_action_available'} if not required <= trace.keys(): out['missing_fields'] = sorted(required - trace.keys()) return out n = len(trace['time']) if any(not isinstance(v, torch.Tensor) or v.shape[:2] != (n, 1) for v in trace.values()): out['error'] = 'Require single-trial [T,1,...] tensors' return out if not all(bool(torch.isfinite(v).all()) for v in trace.values()): out['error'] = 'Nonfinite raw evidence; no passing prefix substituted' return out t = trace['time'][:, 0] expected = torch.arange(1, cycles * 4500 + 1, dtype=t.dtype) * cfg['physics_dt_s'] out['gates']['global_clock_without_restart'] = n == len(expected) and bool( torch.allclose(t.cpu(), expected, atol=1e-5, rtol=0)) out['observed_end_s'] = float(t[-1]) runtime_fields = ('failure_flags', 'would_fail', 'acquisition_failed', 'premature_separation') out['runtime_failure_fields_missing'] = [k for k in runtime_fields if k not in trace] out['gates']['no_recorded_runtime_failure'] = all( not bool(trace[k].any()) for k in runtime_fields if k in trace) out['first_recorded_failure'] = None if 'failure_flags' in trace: flags = trace['failure_flags'][:, 0] if flags.shape != (n, len(FAILURES)): out['error'] = 'Invalid failure flag schema' return out hits = flags.any(-1).nonzero().flatten() if len(hits): i = int(hits[0]) out['first_recorded_failure'] = {'time_s': float(t[i]), 'phase': PHASES[int(trace['phase'][i, 0])], 'causes': [k for k, hit in zip(FAILURES, flags[i]) if hit]} out['controller_kind_is_metadata_only'] = True out['recorded_max_effective_residual_rad'] = (None if 'effective_residual_rad' not in trace else float(trace['effective_residual_rad'].abs().max())) if controller_kind == 'analytic_reference': out['gates']['no_nonzero_recorded_reference_residual'] = all( not bool(trace[k].abs().gt(1e-6).any()) for k in ('requested_action', 'effective_residual_rad', 'residual_request_normalized') if k in trace) if controller_kind == 'learned_policy' and {'requested_action', 'actor_mean'} <= trace.keys(): out['gates']['recorded_deterministic_mean_action'] = bool(torch.allclose( trace['requested_action'], trace['actor_mean'].tanh(), atol=1e-6, rtol=0)) error = root_error(trace) out['root_xy_error_max_m'] = None if error is None else float(error[..., :2].norm(dim=-1).max()) out['root_xy_30mm_exceeded_diagnostic_only'] = None if error is None else bool( (error[..., :2].norm(dim=-1) > .03).any()) # No resampling, truncation-to-success, reanchoring or phase relabeling. # Only after the GLOBAL clock is verified, derive local time from integer # ticks: float32 (18 + 4.6) - 18 can otherwise misclassify an exact boundary. legacy = {'format': 'skillfactory.contact-cycle-simulation-functional.v1', 'physics_dt_s': cfg['physics_dt_s'], 'phase_durations_s': cfg['phase_durations_s'], 'nominal_scenario_id': 0, 'gates': cfg['functional_gates']} for c in range(cycles): start, end = c * 4500, min((c + 1) * 4500, n) if start >= n: out['cycles'].append({'cycle_index': c, 'status': 'not_evaluated'}) continue local = {k: v[start:end].clone() for k, v in trace.items()} local['time'] -= c * 9.0 if out['gates']['global_clock_without_restart']: local['time'] = (torch.arange(1, end - start + 1, dtype=t.dtype, device=t.device) * cfg['physics_dt_s'])[:, None] audit = evaluate_cycle(local, legacy, controller_kind) out['cycles'].append({'cycle_index': c, 'audit': audit, 'phases': phase_summary(local)}) out['trace_functional_passed'] = bool(all(out['gates'].values()) and all( row.get('audit', {}).get('sim_functional_passed', False) for row in out['cycles'])) return out def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--trace', type=Path, required=True) parser.add_argument('--config', type=Path, default=DEFAULT_CONFIG) parser.add_argument('--controller-kind', choices=('analytic_reference', 'learned_policy'), required=True) parser.add_argument('--cycles', type=int, choices=(1, 3), required=True) parser.add_argument('--output', type=Path, required=True) args = parser.parse_args() if args.output.exists(): raise FileExistsError('Never overwrite an earlier audit') cfg = cpu_protocol().load_contract(args.config) raw = torch.load(args.trace, map_location='cpu', weights_only=True) traces = ({str(e): {k: v[:, e:e+1] for k, v in raw.items()} for e in range(raw['time'].shape[1])} if 'time' in raw else raw) result = {'config_sha256': hashlib.sha256(args.config.read_bytes()).hexdigest(), 'audit_sha256': hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), 'trace': str(args.trace.resolve()), 'trace_sha256': hashlib.sha256(args.trace.read_bytes()).hexdigest(), 'rows': {e: evaluate_flow(d, cfg, args.controller_kind, args.cycles) for e, d in traces.items()}, 'new_simulation_steps': 0, 'new_optimizer_steps': 0, 'runtime_continuity_verified': False} args.output.parent.mkdir(parents=True, exist_ok=True) with args.output.open('x') as stream: json.dump(result, stream, indent=2, allow_nan=False) stream.write('\n') print(json.dumps({'output': str(args.output), 'rows': len(result['rows']), 'trace_functional_passed': sum(r['trace_functional_passed'] for r in result['rows'].values()), 'runtime_continuity_verified': False})) return 0 if all(r['trace_functional_passed'] for r in result['rows'].values()) else 2 if __name__ == '__main__': raise SystemExit(main())