"""Temporary artificial archival fixtures only: no study archives or ratings.""" import copy import io import json from pathlib import Path import tempfile import unittest from unittest import mock import seal_runs as sealing from build_assessment_packets import build_packets class SealingTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory(prefix="artificial-seal-fixture-") self.addCleanup(self.temp.cleanup) self.root = Path(self.temp.name) self.refs = self.root/"reference-fixtures" self.refs.mkdir() self.registry = sealing.parse_json((sealing.KIT/"task-registry.json").read_bytes(), "fixture metadata") for task in self.registry["tasks"]: for field, directory in (("public_packet", "public"), ("assessor_guide", "assessor-only")): relative = directory+"/"+task["task_id"]+".txt" content = ("Artificial reference fixture; no task answer: "+task["task_id"]+" "+field).encode() path = self.refs/relative path.parent.mkdir(exist_ok=True) path.write_bytes(content) task[field] = dict(path=relative, sha256=sealing.sha(content)) common = b"Artificial conventions fixture, no study evidence." (self.refs/"assessor-only"/"README.md").write_bytes(common) self.registry["common_conventions"] = dict(path="assessor-only/README.md", declared_version=None, sha256=sealing.sha(common)) self.registry_path = self.refs/"task-registry.json" self.registry_path.write_bytes(sealing.json_bytes(self.registry)) self.source = dict(input_inventory_version="1.0", batch_id="artificial-fixture-batch", collection_completed=True, independent_rater_ids=["fixture-rater-a", "fixture-rater-b"], conditions=[dict(condition_id="fixture-condition", repetition_ids=["fixture-repetition"])], records=[], runs=[]) self.contents = {} self.add_record("fixture-config", "configuration", b'{"artificial_configuration":true}\r\n') self.add_record("fixture-prompt-a", "submitted_prompt", b"Artificial submitted instruction, no task answer.\n") self.add_record("fixture-prompt-b", "submitted_prompt", b"Another artificial instruction.\n") self.add_record("fixture-memory", "memory_snapshot", b"Artificial memory bytes.\r\n") self.add_record("fixture-a-endpoint", "artifact", "Artificial designated text. Café.\r\n".encode()) self.add_record("fixture-a-other", "artifact", b"Artificial different text, not selected by this test.\n") self.add_record("fixture-b-endpoint", "artifact", b"Artificial second run endpoint.\n") for index, task in enumerate(self.registry["tasks"][:2]): label = "a" if index == 0 else "b" record_ids = ["fixture-config", "fixture-prompt-"+label, "fixture-"+label+"-endpoint"] if index == 0: record_ids += ["fixture-a-other", "fixture-memory"] attempts = [dict(attempt_id="fixture-request-"+label, phase="adaptation", role="worker", status="succeeded", retry_of=None, started_at_utc=None, ended_at_utc=None, record_ids=["fixture-prompt-"+label])] if index == 0: attempts.insert(0, dict(attempt_id="fixture-incomplete", phase="adaptation", role="worker", status="incomplete", retry_of=None, started_at_utc=None, ended_at_utc=None, record_ids=["fixture-a-other"])) attempts[1]["retry_of"] = "fixture-incomplete" self.source["runs"].append(dict(run_id="fixture-run-"+label, condition_id="fixture-condition", task_id=task["task_id"], task_version="1.0", rubric_version="1.0", repetition_id="fixture-repetition", run_status="incomplete" if index == 0 else "completed", ended_at_utc=None, endpoint_selection=dict(artifact_id="fixture-"+label+"-endpoint", basis="Explicit artificial fixture endpoint; no optimization"), record_ids=record_ids, record_availability={}, attempts=attempts)) ledger = dict(ledger_version="1.0", ledger_id="artificial-usage-fixture", runs=[], requests=[]) for run in self.source["runs"]: ledger["runs"].append({k: run[k] for k in ("run_id", "condition_id", "task_id", "repetition_id")}) ledger["runs"][-1]["phase_log_complete"] = dict(adaptation=False, final_assessment=False) for attempt in run["attempts"]: ledger["requests"].append(dict(request_id=attempt["attempt_id"], run_id=run["run_id"], phase="adaptation", role="worker", status="failed" if attempt["status"] == "incomplete" else "succeeded", disposition="unknown", retry_of=attempt["retry_of"], provider=None, model_snapshot=None, pricing_date=None, usage=dict(input_tokens=None, cached_input_tokens=None, cached_input_relationship="unknown", output_tokens=None, reasoning_tokens=None, reasoning_relationship="unknown", wall_time_seconds=None, tool_runtime_seconds=None), billed_cost=dict(amount=None, currency=None))) self.add_record("fixture-usage", "usage_ledger", sealing.json_bytes(ledger)) for run in self.source["runs"]: run["record_ids"].append("fixture-usage") self.set_availability() self.input_path = self.root/"input-inventory.json" self.output = self.root/"new-archive" def add_record(self, rid, kind, content): filename = rid+".source" (self.root/filename).write_bytes(content) self.contents[rid] = content self.source["records"].append(dict(record_id=rid, kind=kind, path=filename, sha256=None)) def set_availability(self): records = {r["record_id"]: r for r in self.source["records"]} for run in self.source["runs"]: for category in sealing.CATEGORIES: count = sum(sealing.KINDS[records[r]["kind"]] == category for r in run["record_ids"]) run["record_availability"][category] = dict(state="available" if count else "not_applicable", note=None if count else "Explicitly absent artificial fixture category") def seal(self): self.input_path.write_bytes(sealing.json_bytes(self.source)) return sealing.seal_inventory(self.input_path, self.output, self.registry_path) def assert_unsealed(self): self.assertFalse((self.output/"packet-build-manifest.json").exists()) def test_bytes_relative_hashes_endpoint_and_incomplete_attempt_retained(self): result = self.seal() inventory = sealing.parse_json((self.output/"archive-inventory.json").read_bytes(), "archive") packet = sealing.parse_json((self.output/"packet-build-manifest.json").read_bytes(), "packet") self.assertTrue(result["batch_sealed"]) self.assertTrue(packet["batch_sealed"]) self.assertNotIn("batch_sealed", inventory) # Only final publication commits a seal. for row in inventory["records"]: self.assertFalse(Path(row["path"]).is_absolute()) self.assertEqual((self.output/row["path"]).read_bytes(), self.contents[row["record_id"]]) self.assertEqual(row["sha256"], sealing.sha(self.contents[row["record_id"]])) self.assertEqual((self.output/"source-inventory.json").read_bytes(), self.input_path.read_bytes()) self.assertEqual(inventory["runs"][0]["attempts"][0]["status"], "incomplete") self.assertEqual(packet["runs"][0]["final_artifact"]["artifact_id"], "fixture-a-endpoint") self.assertEqual(inventory["runs"], self.source["runs"]) for record in self.source["records"]: self.assertEqual((self.root/record["path"]).read_bytes(), self.contents[record["record_id"]]) def test_actual_packet_builder_accepts_relocated_fixture_archive(self): self.seal() result = build_packets(self.output/"packet-build-manifest.json", self.root/"fixture-packets", self.root/"fixture-key", self.output/"registry"/"task-registry.json") self.assertEqual(result["artifact_count"], 2) # Operational records stay in the coordinator archive, not rater bundles. for path in (self.root/"fixture-packets").rglob("*"): if path.is_file(): self.assertNotIn(b"artificial_configuration", path.read_bytes()) self.assertNotIn(b"Artificial memory bytes", path.read_bytes()) def test_missing_file_and_incompatible_version_are_preflight_errors(self): (self.root/self.source["records"][0]["path"]).unlink() with self.assertRaisesRegex(sealing.ValidationError, "missing/non-file"): self.seal() self.assertFalse(self.output.exists()) (self.root/self.source["records"][0]["path"]).write_bytes(self.contents["fixture-config"]) self.source["runs"][0]["rubric_version"] = "2.0" with self.assertRaisesRegex(sealing.ValidationError, "incompatible"): self.seal() self.assertFalse(self.output.exists()) def test_duplicate_cells_and_expected_hash_mismatch(self): self.source["runs"][1]["task_id"] = self.source["runs"][0]["task_id"] with self.assertRaisesRegex(sealing.ValidationError, "duplicate condition/task/repetition"): self.seal() self.source["runs"][1]["task_id"] = self.registry["tasks"][1]["task_id"] self.source["records"][0]["sha256"] = "0"*64 with self.assertRaisesRegex(sealing.ValidationError, "source hash mismatch"): self.seal() self.assertFalse(self.output.exists()) def test_explicit_endpoint_required_no_implicit_last_or_best(self): self.source["runs"][0]["endpoint_selection"]["artifact_id"] = "absent-record" with self.assertRaisesRegex(sealing.ValidationError, "endpoint must designate"): self.seal() self.assertFalse(self.output.exists()) def test_failed_run_without_deliverable_is_retained_and_explained(self): run = self.source["runs"][1] run["run_status"] = "failed" run["endpoint_selection"] = dict(artifact_id=None, basis="Artificial fixture: no assessable endpoint designated") result = self.seal() inventory = sealing.parse_json((self.output/"archive-inventory.json").read_bytes(), "archive") packet = sealing.parse_json((self.output/"packet-build-manifest.json").read_bytes(), "packet") self.assertEqual(len(inventory["runs"]), 2) self.assertEqual(len(inventory["unassessable_runs"]), 1) self.assertEqual(result["assessable_runs"], 1) self.assertEqual(len(packet["runs"]), 1) self.assertEqual(packet["conditions"], self.source["conditions"]) def test_unavailable_records_remain_explicit_not_fabricated(self): self.source["records"] = [r for r in self.source["records"] if r["record_id"] != "fixture-usage"] for run in self.source["runs"]: run["record_ids"].remove("fixture-usage") self.set_availability() for run in self.source["runs"]: run["record_availability"]["usage_records"] = dict(state="unavailable", note="Artificial missing usage fixture") self.seal() inventory = sealing.parse_json((self.output/"archive-inventory.json").read_bytes(), "archive") self.assertEqual(inventory["runs"][0]["record_availability"]["usage_records"]["state"], "unavailable") self.assertFalse(any(r["kind"] == "usage_ledger" for r in inventory["records"])) def test_invalid_usage_version_or_attempt_scope_prevents_sealing(self): row = next(r for r in self.source["records"] if r["record_id"] == "fixture-usage") path = self.root/row["path"] ledger = sealing.parse_json(path.read_bytes(), "fixture ledger") ledger["ledger_version"] = "2.0" path.write_bytes(sealing.json_bytes(ledger)) with self.assertRaises(sealing.account_resources.LedgerError): self.seal() self.assertFalse(self.output.exists()) ledger["ledger_version"] = "1.0" path.write_bytes(sealing.json_bytes(ledger)) self.source["runs"][0]["attempts"].pop(0) self.source["runs"][0]["attempts"][0]["retry_of"] = None with self.assertRaisesRegex(sealing.ValidationError, "missing from declared attempt"): self.seal() self.assertFalse(self.output.exists()) def test_existing_output_including_broken_symlink_is_refused(self): self.output.mkdir() sentinel = self.output/"do-not-overwrite" sentinel.write_bytes(b"existing fixture") with self.assertRaisesRegex(sealing.ValidationError, "existing archive"): self.seal() self.assertEqual(sentinel.read_bytes(), b"existing fixture") elsewhere = self.root/"symlink-destination" elsewhere.symlink_to(self.root/"missing-target") with self.assertRaisesRegex(sealing.ValidationError, "existing archive"): sealing.seal_inventory(self.input_path, elsewhere, self.registry_path) def test_interrupted_payload_write_never_publishes_seal(self): original_write = sealing.write_bytes count = 0 def interrupted(path, content): nonlocal count count += 1 if count == 3: raise KeyboardInterrupt() original_write(path, content) with mock.patch.object(sealing, "write_bytes", side_effect=interrupted): with self.assertRaises(KeyboardInterrupt): self.seal() self.assertTrue(self.output.exists()) self.assert_unsealed() with self.assertRaisesRegex(sealing.ValidationError, "existing archive"): self.seal() def test_corrupt_write_is_detected_before_commit(self): original_write = sealing.write_bytes def corrupted(path, content): original_write(path, content+b"artificial corruption" if path.parent.name == "records" else content) with mock.patch.object(sealing, "write_bytes", side_effect=corrupted): with self.assertRaisesRegex(sealing.ValidationError, "copy validation failed"): self.seal() self.assert_unsealed() def test_failed_commit_link_leaves_only_unsealed_archive(self): with mock.patch.object(sealing.os, "link", side_effect=OSError("artificial commit failure")): with self.assertRaisesRegex(OSError, "commit failure"): self.seal() self.assertTrue((self.output/"archive-inventory.json").exists()) self.assert_unsealed() self.assertFalse(list(self.root.glob(".pending-run-seal-*"))) def test_failed_staging_write_never_links_partial_manifest(self): real_tempfile = sealing.tempfile.NamedTemporaryFile class FailedStage: def __init__(self, **kwargs): self.inner = real_tempfile(**kwargs) self.name = self.inner.name def __enter__(self): return self def write(self, content): self.inner.write(content[:10]) raise OSError("artificial partial manifest write") def __exit__(self, *args): self.inner.close() with mock.patch.object(sealing.tempfile, "NamedTemporaryFile", FailedStage): with self.assertRaisesRegex(OSError, "partial manifest write"): self.seal() self.assert_unsealed() self.assertFalse(list(self.root.glob(".pending-run-seal-*"))) def test_cli_errors_and_interruptions_report_no_success(self): self.input_path.write_bytes(sealing.json_bytes(self.source)) args = ["--inventory", str(self.input_path), "--output", str(self.output), "--registry", str(self.registry_path)] with mock.patch.object(sealing, "seal_inventory", side_effect=KeyboardInterrupt()), mock.patch("sys.stderr", new_callable=io.StringIO) as err: self.assertEqual(sealing.main(args), 130) self.assertIn("interrupted", json.loads(err.getvalue())["error"]) self.source["collection_completed"] = False self.input_path.write_bytes(sealing.json_bytes(self.source)) with mock.patch("sys.stderr", new_callable=io.StringIO) as err: self.assertEqual(sealing.main(args), 2) self.assertIn("collection completed", json.loads(err.getvalue())["error"]) self.assertFalse(self.output.exists()) if __name__ == "__main__": unittest.main()