"""Explicit, hash-pinned operator exception for one recorded drift failure. No threshold edits.""" import hashlib import json from pathlib import Path def sha256(path): return hashlib.sha256(Path(path).read_bytes()).hexdigest() def reject_constant(value): raise ValueError(f"Nonfinite waiver JSON: {value}") def load_waiver(path, expected_sha256, run, candidate): if path is None and expected_sha256 is None: return None if path is None or expected_sha256 is None: raise ValueError("Explicit pinned waiver identity required") content = Path(path).read_bytes() if hashlib.sha256(content).hexdigest() != expected_sha256: raise ValueError("Explicit pinned waiver identity required") waiver = json.loads(content, parse_constant=reject_constant) binding = json.loads((candidate / "binding.json").read_text()) if ( waiver["format"] != "skillfactory.standing-deviation.v1" or waiver["authorization"] != "explicit_user_instruction" or waiver["approved"] is not True or not waiver["operator_statement"].strip() or waiver["scope"] != "exact_candidate_and_trial_only" or waiver["waived_gate"] != "stability.foot_drift" or waiver["original_limit_m"] != 0.03 or waiver["duration_s"] != 120.0 or waiver["num_envs"] != 26 or waiver["physical_samples"] != 60000 ): raise ValueError("Unsupported waiver scope") for key, actual in ( ("candidate_manifest_sha256", sha256(candidate / "manifest.json")), ("actor_sha256", sha256(candidate / "actor.pt")), ("checkpoint_sha256", binding["checkpoint_sha256"]), ): if waiver[key] != actual: raise ValueError("Waiver is for a different candidate") required = { "stability/result.json", "stability/metadata.json", "stability/physics_trace.pt", "stability/memory.json", "stability_process.json", "stability.log", } if set(waiver["evidence_sha256"]) != required: raise ValueError("Incomplete waiver evidence identity") for name, digest in waiver["evidence_sha256"].items(): if sha256(run / name) != digest: raise ValueError("Waived evidence changed") return waiver def apply_drift_waiver(waiver, gates, rows, cfg, metadata): """Return an exception receipt; never mutate raw gates, rows, or original limits.""" if ( {k for k, v in gates.items() if not v} != {"foot_drift"} or cfg["foot_drift_limit_m"] != waiver["original_limit_m"] or metadata["inputs"]["mode"] != "stability" or metadata["inputs"]["duration_s"] != waiver["duration_s"] or metadata["inputs"]["count"] != waiver["num_envs"] or len(rows) != waiver["num_envs"] ): raise ValueError("Waiver cannot cover additional failures or a different protocol") failures = [ {"environment": i, "max_foot_drift_m": row["max_foot_drift_m"]} for i, row in enumerate(rows) if row["max_foot_drift_m"] > waiver["original_limit_m"] ] if failures != waiver["observed_failures"] or [r["environment"] for r in failures] != [6, 19]: raise ValueError("Waiver cannot cover different drift observations") return { "accepted_by_operator_waiver": True, "raw_gate_passed": False, "gate": "stability.foot_drift", "original_limit_m": waiver["original_limit_m"], "observed_failures": failures, }