fixture_test_policy.py (2053B)
1 """Standalone fixed fixture budgets; explicit short functional cases stay short.""" 2 3 from __future__ import annotations 4 5 import hashlib 6 import json 7 import math 8 from pathlib import Path 9 10 POLICY_PATH = Path(__file__).with_name("fixture_test_policy.v1.json") 11 12 13 def unique_keys(pairs): 14 result = {} 15 for key, value in pairs: 16 if key in result: 17 raise ValueError("duplicate fixture policy key") 18 result[key] = value 19 return result 20 21 22 def validate_identity(policy): 23 if ( 24 policy.get("schema") != "radroots.tera.fixture-test-policy.v1" 25 or policy.get("id") != "tera_fixture_busy_workstation_v1" 26 or policy.get("phase") != "pre_mvp_20261008" 27 or policy.get("functional_short_intervals_unchanged") is not True 28 or policy.get("quiet_workstation_required") is not False 29 ): 30 raise ValueError("unknown fixture policy or altered functional controls") 31 32 33 def valid_budget(value): 34 return type(value) in (int, float) and math.isfinite(value) and 0 < value <= 900 35 36 37 def load_policy(): 38 if POLICY_PATH.is_symlink(): 39 raise ValueError("fixture policy must be a regular source file") 40 with POLICY_PATH.open("rb") as stream: 41 raw = stream.read(16385) 42 if len(raw) > 16384: 43 raise ValueError("fixture policy exceeds its source bound") 44 policy = json.loads(raw, object_pairs_hook=unique_keys) 45 if not isinstance(policy, dict): 46 raise ValueError("fixture policy must be an object") 47 validate_identity(policy) 48 limits = policy["limits"] 49 if not isinstance(limits, dict) or not limits: 50 raise ValueError("fixture budgets are missing") 51 if not all(valid_budget(value) for value in limits.values()): 52 raise ValueError("fixture budget is outside its finite bound") 53 if policy.get("caps") != {"stdout": 1048576, "stderr": 65536}: 54 raise ValueError("fixture output caps changed") 55 policy["source_sha256"] = hashlib.sha256(raw).hexdigest() 56 return policy 57 58 59 def limit(name): 60 return load_policy()["limits"][name]