"""Retained-evidence integrity checks remain active with Python optimization.""" import json from pathlib import Path import shutil import subprocess import sys import tempfile import unittest class RescoreIntegrity(unittest.TestCase): def test_optimized_rescoring_rejects_tampering(self): source = Path(__file__).resolve().parent cases = { "unchanged": None, "report": "Original result hash mismatch", "answer": "Retained output hash mismatch", "count": "Expected exactly eight", "identity": "trial identities differ", "verifier": "Original verifier result mismatch", } for case, message in cases.items(): with self.subTest(case=case), tempfile.TemporaryDirectory() as directory: root = Path(directory) for name in ["rescore.py", "verify.py", "verify_v2.py", "expected.json", "result.json", "final-answers.json", "baseline"]: path = source / name if path.is_dir(): shutil.copytree(path, root / name) else: shutil.copy2(path, root / name) answers_path = root / "final-answers.json" answers = json.loads(answers_path.read_text()) if case == "report": with (root / "result.json").open("a") as stream: stream.write(" ") elif case == "answer": answers["answers"][0]["output"] += " tampered" elif case == "count": answers["answers"].pop() elif case == "identity": answers["answers"][0]["trialId"] = "unrecorded-trial" elif case == "verifier": with (root / "verify.py").open("a") as stream: stream.write("\ndef verify(output):\n return {'passed': True, 'reason': 'modified'}\n") answers_path.write_text(json.dumps(answers)) result = subprocess.run([sys.executable, "-O", str(root / "rescore.py")], capture_output=True, text=True, timeout=20) if message: self.assertNotEqual(result.returncode, 0) self.assertIn(message, result.stderr) self.assertEqual(result.stdout, "") else: self.assertEqual(result.returncode, 0, result.stderr) artifact = json.loads(result.stdout) self.assertEqual(len(artifact["trials"]), 8) self.assertEqual(artifact["counts"]["baseline"]["postHocAccepted"], 2) self.assertEqual(artifact["counts"]["candidate"]["postHocAccepted"], 4) if __name__ == "__main__": unittest.main()