"""Offline source-version regressions; bundles are temporary data, never executed.""" from copy import deepcopy import hashlib import json from pathlib import Path import tempfile import unittest from benchkit.audits.closed_run import ClosedFiles, audit_model_source from benchkit.model_profiles import capture_policy_source, create_policy, verify_policy_source from benchkit.source_snapshot import inventory_digest PREFIX = "AgentPolicies/gpt6_astra/" V2_FILES = {PREFIX + name for name in ( "methods/codex_planned_v2.py", "configs/codex_planned_v2_config.json", "prompts/codex_planned_v2_prompt.txt")} V3_FILES = {PREFIX + name for name in ( "methods/codex_planned_v3.py", "configs/codex_planned_v3_config.json", "prompts/codex_planned_v3_prompt.txt")} def sha(payload): return hashlib.sha256(payload).hexdigest() def write_json(path, value): path.write_text(json.dumps(value, sort_keys=True) + "\n", encoding="utf-8") class ModelSourceVersionsTests(unittest.TestCase): def capture(self, method): temporary = tempfile.TemporaryDirectory() self.addCleanup(temporary.cleanup) root = Path(temporary.name) policy = create_policy("gpt6_astra", control_method=method) snapshot = capture_policy_source(root, policy) self.assertEqual(snapshot["schema_version"], "starvla-rsi.model-source/v8") return root, policy.descriptor.to_dict(), snapshot def rebind_fixture(self, root, descriptor, snapshot, version, removed=()): """Build historical-shaped data without calling the producer's allowlist. All rebindings stay in this test's temporary directory. This proves schema compatibility, not historical runtime or provider attestation. """ source = root / "model_source/files" index_path = root / "model_source/index.json" index = json.loads(index_path.read_text()) dependencies_path = source / PREFIX / "dependencies.json" dependencies = json.loads(dependencies_path.read_text()) for relative in removed: (source / relative).unlink() dependencies["files"] = [name for name in dependencies["files"] if name not in removed] write_json(dependencies_path, dependencies) rows = [] for row in index["files"]: if row["path"] not in removed: payload = (source / row["path"]).read_bytes() rows.append(dict(path=row["path"], size_bytes=len(payload), sha256=sha(payload))) identity = deepcopy(index["profile"]) identity["dependencies_sha256"] = sha(dependencies_path.read_bytes()) identity["source_inventory_sha256"] = inventory_digest( [row for row in rows if row["path"] != "campaign_prompt.txt"]) schema = f"starvla-rsi.model-source/v{version}" index.update(schema_version=schema, profile=identity, files=rows, file_count=len(rows), total_bytes=sum(row["size_bytes"] for row in rows), inventory_sha256=inventory_digest(rows)) write_json(index_path, index) snapshot.update({key: index[key] for key in ( "schema_version", "file_count", "total_bytes", "inventory_sha256")}) snapshot["index_sha256"] = sha(index_path.read_bytes()) descriptor["parameters"]["model_profile"] = identity def assert_valid(self, root, descriptor, snapshot): verify_policy_source(root, descriptor, snapshot) self.assertGreater(audit_model_source(ClosedFiles(root), {"policy": descriptor, "model_source": snapshot}), 0) def assert_invalid(self, root, descriptor, snapshot, message): with self.assertRaisesRegex(ValueError, message): verify_policy_source(root, descriptor, snapshot) with self.assertRaises(ValueError): audit_model_source(ClosedFiles(root), {"policy": descriptor, "model_source": snapshot}) def test_v8_captures_and_verifies_v3(self): self.assert_valid(*self.capture("codex-planned/v3")) def test_historical_v7_inventory_verifies_v2_without_v3_files(self): root, descriptor, snapshot = self.capture("codex-planned/v2") self.rebind_fixture(root, descriptor, snapshot, 7, V3_FILES) self.assert_valid(root, descriptor, snapshot) def test_historical_v6_inventory_still_verifies_direct_v3(self): root, descriptor, snapshot = self.capture("codex-direct/v3") self.rebind_fixture(root, descriptor, snapshot, 6, V2_FILES | V3_FILES) self.assert_valid(root, descriptor, snapshot) def test_v7_rejects_v3_method_even_with_consistent_v7_inventory(self): root, descriptor, snapshot = self.capture("codex-planned/v3") self.rebind_fixture(root, descriptor, snapshot, 7, V3_FILES) self.assert_invalid(root, descriptor, snapshot, "unsupported or mismatched versioned model method") def test_v7_rejects_added_v3_files_even_when_all_hashes_match(self): root, descriptor, snapshot = self.capture("codex-planned/v2") self.rebind_fixture(root, descriptor, snapshot, 7) self.assert_invalid(root, descriptor, snapshot, "model source index inventory or identity changed") if __name__ == "__main__": unittest.main()