#!/usr/bin/env python3 """Offline CPU trace readback. Audit consistency is NOT skill acceptance.""" import argparse import hashlib import json from pathlib import Path import torch from runtime_common import write_json from SkillFactory.legs.contact_cycle.metrics import summarize def audit(run): run = Path(run) report = json.loads((run/"result.json").read_text()) cfg = json.loads((run/"config.json").read_text()) locked = json.loads((run/"invocation.json").read_text())["thresholds"]["gates"] trace = torch.load(run/"physics_trace.pt", map_location="cpu", weights_only=True) rows = summarize(trace, report["physics_dt"], locked, cfg["scenarios"]) samples = round(sum(cfg["phase_durations_s"])/report["physics_dt"]) gates = { "not_skill_acceptance": report["accepted_skill"] is False and report["trained_skill"] is False, "same_rows_from_raw_trace": rows == report["rows"], "summary_status_consistent": ( report["passed"] == all(report["gates"].values()) and report["gates"]["all_scenario_metrics"] == all(r["success"] for r in rows) and report["status"] == ("completed" if report["passed"] else "failed") ), "complete_duration": len(trace["time"]) == samples == report["physics_steps"], "monotonic_physics_clock": bool(torch.allclose(trace["time"][1:]-trace["time"][:-1], torch.full_like(trace["time"][1:], report["physics_dt"]), atol=1e-6, rtol=0)), } identity = json.loads((run/"identity.json").read_text()) snapshot_files = list((run/"source_snapshot").rglob("*")) hashes = {hashlib.sha256(p.read_bytes()).hexdigest() for p in snapshot_files if p.is_file()} gates["bound_source_snapshots"] = all(digest in hashes for digest in identity.values()) # Independent scalar loop for the slip integral (does not call summarize's implementation). slip = [] for e in range(trace["phase"].shape[1]): total = 0.0 for k in range(len(trace["time"])): phase, progress = int(trace["phase"][k, e]), float(trace["progress"][k, e]) stance = (phase == 0 and float(trace["time"][k, e]) >= 2) or (phase == 5 and progress >= 0.8) if stance and float(trace["normal"][k, e, 2]) > 2: total += float(trace["slip_speed"][k, e]) * report["physics_dt"] slip.append(total) gates["independent_slip_integral"] = all( not r["complete"] or not r["finite"] or abs(slip[i]-r["stance_slip_m"]) <= 1e-5 for i, r in enumerate(rows)) failures = [] for row in rows: if not row["success"]: e = row["env"] indices = torch.nonzero(trace["would_fail"][:, e]).flatten() failures.append({"env": e, "scenario": row["scenario"], "reasons": row["failure_reasons"], "first_task_failure_s": float(trace["time"][indices[0], e]) if len(indices) else None, "peak_height_error_s": float(trace["time"][trace["height_error"][:, e].argmax(), e])}) return {"passed": all(gates.values()), "accepted_skill": False, "audit_only": True, "original_physical_passed": report["passed"], "gates": gates, "failure_cases": failures, "trace_sha256": hashlib.sha256((run/"physics_trace.pt").read_bytes()).hexdigest(), "report_sha256": hashlib.sha256((run/"result.json").read_bytes()).hexdigest()} def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--run", type=Path, required=True) parser.add_argument("--output", type=Path, required=True) args = parser.parse_args() if args.output.exists(): parser.error("Refusing to overwrite audit evidence") result = audit(args.run) write_json(args.output, result) raise SystemExit(0 if result["passed"] else 2) if __name__ == "__main__": main()