093ab883be
Add loaded-standing physics, PPO training, fixed-candidate acceptance, isolated inference, and fail-closed publication. Preserve the original 120-second drift failure and bind the operator-approved exception to the exact candidate and evidence. Include pending Reach training/release infrastructure and archived packages. Leave pre-existing short-worker visualization changes and large generated training artifacts out of this commit.
162 lines
6.0 KiB
Python
162 lines
6.0 KiB
Python
"""The explicit operator waiver can never become a general relaxed threshold."""
|
|
|
|
import copy
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
|
|
from standing_deviation import apply_drift_waiver, load_waiver, sha256
|
|
|
|
|
|
@pytest.fixture
|
|
def case(tmp_path):
|
|
candidate = tmp_path / "candidate"
|
|
candidate.mkdir()
|
|
(candidate / "manifest.json").write_text("{}")
|
|
(candidate / "actor.pt").write_bytes(b"fixture")
|
|
(candidate / "binding.json").write_text(json.dumps({"checkpoint_sha256": "b" * 64}))
|
|
names = [
|
|
"stability/result.json",
|
|
"stability/metadata.json",
|
|
"stability/physics_trace.pt",
|
|
"stability/memory.json",
|
|
"stability_process.json",
|
|
"stability.log",
|
|
]
|
|
for name in names:
|
|
path = tmp_path / name
|
|
path.parent.mkdir(exist_ok=True)
|
|
path.write_text("recorded evidence")
|
|
rows = [{"max_foot_drift_m": 0.004} for _ in range(26)]
|
|
rows[6]["max_foot_drift_m"], rows[19]["max_foot_drift_m"] = 0.030620924223339147, 0.03078258298377699
|
|
waiver = {
|
|
"format": "skillfactory.standing-deviation.v1",
|
|
"authorization": "explicit_user_instruction",
|
|
"operator_statement": "Accept this recorded deviation only",
|
|
"approved": True,
|
|
"scope": "exact_candidate_and_trial_only",
|
|
"waived_gate": "stability.foot_drift",
|
|
"original_limit_m": 0.03,
|
|
"duration_s": 120.0,
|
|
"num_envs": 26,
|
|
"physical_samples": 60000,
|
|
"candidate_manifest_sha256": sha256(candidate / "manifest.json"),
|
|
"actor_sha256": sha256(candidate / "actor.pt"),
|
|
"checkpoint_sha256": "b" * 64,
|
|
"evidence_sha256": {p: sha256(tmp_path / p) for p in names},
|
|
"observed_failures": [{"environment": i, "max_foot_drift_m": rows[i]["max_foot_drift_m"]} for i in (6, 19)],
|
|
}
|
|
path = tmp_path / "waiver.json"
|
|
path.write_text(json.dumps(waiver))
|
|
gates = {"foot_drift": False, "height": True, "finite": True, "memory": True}
|
|
cfg = {"foot_drift_limit_m": 0.03}
|
|
meta = {"inputs": {"mode": "stability", "duration_s": 120.0, "count": 26}}
|
|
return tmp_path, candidate, path, waiver, gates, rows, cfg, meta
|
|
|
|
|
|
def test_explicit_exact_waiver_preserves_raw_failure(case):
|
|
root, candidate, path, original, gates, rows, cfg, meta = case
|
|
before = copy.deepcopy((gates, rows, cfg))
|
|
waiver = load_waiver(path, sha256(path), root, candidate)
|
|
receipt = apply_drift_waiver(waiver, gates, rows, cfg, meta)
|
|
assert receipt["accepted_by_operator_waiver"] and receipt["raw_gate_passed"] is False
|
|
assert (gates, rows, cfg) == before and gates["foot_drift"] is False
|
|
assert load_waiver(None, None, root, candidate) is None
|
|
with pytest.raises(ValueError, match="pinned"):
|
|
load_waiver(path, None, root, candidate)
|
|
with pytest.raises(ValueError, match="pinned"):
|
|
load_waiver(path, "0" * 64, root, candidate)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"key,value",
|
|
[
|
|
("approved", False),
|
|
("approved", 1),
|
|
("waived_gate", "stability.limits"),
|
|
("scope", "all_future_trials"),
|
|
("original_limit_m", 0.031),
|
|
("num_envs", 13),
|
|
("candidate_manifest_sha256", "0" * 64),
|
|
("actor_sha256", "0" * 64),
|
|
("checkpoint_sha256", "0" * 64),
|
|
],
|
|
)
|
|
def test_wrong_waiver_scope_or_binding_rejected_even_with_explicit_hash(case, key, value):
|
|
root, candidate, path, waiver, *_ = case
|
|
waiver[key] = value
|
|
path.write_text(json.dumps(waiver))
|
|
with pytest.raises(ValueError):
|
|
load_waiver(path, sha256(path), root, candidate)
|
|
|
|
|
|
def test_parsed_authorization_is_from_the_single_pinned_read(case, monkeypatch):
|
|
root, candidate, path, waiver, *_ = case
|
|
original = path.read_bytes()
|
|
pin = sha256(path)
|
|
replaced = {**waiver, "approved": False}
|
|
read_bytes = Path.read_bytes
|
|
|
|
def replace_after_read(file):
|
|
content = read_bytes(file)
|
|
if file == path:
|
|
path.write_text(json.dumps(replaced))
|
|
return content
|
|
|
|
monkeypatch.setattr(Path, "read_bytes", replace_after_read)
|
|
try:
|
|
consumed = load_waiver(path, pin, root, candidate)
|
|
finally:
|
|
path.write_bytes(original)
|
|
assert consumed == waiver and consumed["approved"] is True
|
|
assert read_bytes(path) == original
|
|
|
|
|
|
def test_replacement_and_restore_during_copy_cannot_package_unpinned_waiver(case, monkeypatch):
|
|
import publish_standing as publish
|
|
|
|
root, _, path, waiver, *_ = case
|
|
original = path.read_bytes()
|
|
pin = sha256(path)
|
|
destination = root / "staged_waiver.json"
|
|
copy_file = publish.shutil.copyfile
|
|
|
|
def substituted(source, target):
|
|
try:
|
|
path.write_text(json.dumps({**waiver, "approved": False}))
|
|
return copy_file(source, target)
|
|
finally:
|
|
path.write_bytes(original)
|
|
|
|
monkeypatch.setattr(publish.shutil, "copyfile", substituted)
|
|
with pytest.raises(ValueError, match="Staged waiver"):
|
|
publish.copy_pinned_waiver(path, destination, pin)
|
|
assert sha256(path) == pin and sha256(destination) != pin
|
|
|
|
|
|
def test_changed_physical_evidence_rejected(case):
|
|
root, candidate, path, *_ = case
|
|
(root / "stability/physics_trace.pt").write_bytes(b"changed")
|
|
with pytest.raises(ValueError, match="evidence changed"):
|
|
load_waiver(path, sha256(path), root, candidate)
|
|
|
|
|
|
@pytest.mark.parametrize("mutation", ["extra_gate", "larger_drift", "additional_environment", "threshold", "duration"])
|
|
def test_waiver_does_not_cover_any_extra_failure(case, mutation):
|
|
_, _, _, waiver, gates, rows, cfg, meta = case
|
|
if mutation == "extra_gate":
|
|
gates["memory"] = False
|
|
elif mutation == "larger_drift":
|
|
rows[19]["max_foot_drift_m"] = 0.0309
|
|
elif mutation == "additional_environment":
|
|
rows[0]["max_foot_drift_m"] = 0.0301
|
|
elif mutation == "threshold":
|
|
cfg["foot_drift_limit_m"] = 0.031
|
|
else:
|
|
meta["inputs"]["duration_s"] = 119.0
|
|
with pytest.raises(ValueError):
|
|
apply_drift_waiver(waiver, gates, rows, cfg, meta)
|