"""Injected tournament execution, identity, and publication contracts.""" from __future__ import annotations import hashlib import json from pathlib import Path import random import pytest from obliteratus import tourney _SEEDED_METHODS = ( "advanced", "failspy", "gabliteration", "rdo", "heretic", "informed", "advanced_iterative", ) @pytest.fixture def rng_states(): import numpy as np import torch python_state = random.getstate() numpy_state = np.random.get_state() torch_state = torch.random.get_rng_state() try: yield finally: random.setstate(python_state) np.random.set_state(numpy_state) torch.random.set_rng_state(torch_state) def _seeded_draw(seed): import numpy as np import torch from obliteratus.reproducibility import set_seed set_seed(seed, deterministic=False) return random.random(), float(np.random.random()), float(torch.rand(())) @pytest.mark.parametrize("method", _SEEDED_METHODS) @pytest.mark.parametrize("round_num", (1, 2, 3)) @pytest.mark.parametrize("base_seed", (0, 20261024, 2**64 - 1)) def test_given_arm_seed_when_seeding_real_rngs_then_draws_repeat( tmp_path, rng_states, method, round_num, base_seed, ): runner = tourney.TourneyRunner( "fixture/model", methods=list(_SEEDED_METHODS), seed=base_seed, output_dir=str(tmp_path / "tourney"), ) seed = runner._arm_seed(round_num, method) first = _seeded_draw(seed) replay = _seeded_draw(runner._arm_seed(round_num, method)) assert 0 <= seed < 2**32 assert replay == first def test_given_distinct_arm_identities_then_seeds_are_distinct(tmp_path): runner = tourney.TourneyRunner( "fixture/model", methods=list(_SEEDED_METHODS), seed=20261024, output_dir=str(tmp_path / "tourney"), ) seeds = { runner._arm_seed(round_num, method) for round_num in (1, 2, 3) for method in _SEEDED_METHODS } assert len(seeds) == 3 * len(_SEEDED_METHODS) def _prompts(): return ([f"harm-{index}" for index in range(400)], [f"safe-{index}" for index in range(400)]) def _metrics(method: str, *, kl: float = 0.1): rank = {"alpha": 0.05, "beta": 0.2, "gamma": 0.35, "delta": 0.5}.get( method, 0.1, ) return { "refusal_rate": rank, "coherence": 0.9, "kl_divergence": kl, "perplexity": 10.0, "spectral_certification": "INCONCLUSIVE", } def _outcome(request: tourney.ArmRequest, metrics=None): payload = f"{request.round_num}:{request.method}:{request.seed}".encode() candidate = hashlib.sha256(payload).hexdigest() work_dir = Path(request.work_dir) work_dir.mkdir(parents=True, exist_ok=True) evidence = work_dir / "candidate.json" evidence.write_bytes(payload) return tourney.ArmOutcome( metrics=metrics or _metrics(request.method), candidate_sha256=candidate, replay={ "schema": 1, "kind": "native_exact_replay", "experiment_sha256": request.experiment_sha256, "candidate_sha256": candidate, "round_num": request.round_num, "method": request.method, "seed": request.seed, }, artifacts=({ "path": str(evidence), "sha256": hashlib.sha256(payload).hexdigest(), "size": len(payload), },), spectral_cert="INCONCLUSIVE", ) def _publisher(request: tourney.WinnerRequest): destination = Path(request.destination) destination.mkdir(parents=True, exist_ok=True) (destination / "model.safetensors").write_bytes(b"winner") return tourney.WinnerPublication( candidate_sha256=request.candidate_sha256, output_dir=str(destination), receipt={ "schema": 1, "status": "complete", "experiment_sha256": request.experiment_sha256, "candidate_sha256": request.candidate_sha256, }, hub_repo="owner/winner", ) def _runner(tmp_path, methods, executor, publisher=_publisher, **kwargs): runner = tourney.TourneyRunner( "fixture/model", methods=methods, output_dir=str(tmp_path / "tourney"), arm_executor=executor, winner_publisher=publisher, executor_identity={ "schema": 1, "model_revision": "revision-1", "tokenizer_revision": "tokenizer-1", "runtime_sha256": "a" * 64, "operator": "test", }, artifact_budget_bytes=100_000, **kwargs, ) runner._dataset_prompts = _prompts return runner @pytest.mark.parametrize( "overrides", [ {"executor_identity": {}}, {"artifact_budget_bytes": 0}, ], ) def test_injected_runner_requires_explicit_identity_and_budget(tmp_path, overrides): arguments = { "executor_identity": {"schema": 1, "base": "immutable"}, "artifact_budget_bytes": 100, } arguments.update(overrides) with pytest.raises(ValueError): tourney.TourneyRunner( "fixture/model", methods=["alpha"], output_dir=str(tmp_path), arm_executor=_outcome, winner_publisher=_publisher, **arguments, ) def test_injected_runner_scores_every_arm_but_publishes_only_exact_winner(tmp_path): requests = [] publications = [] def execute(request): requests.append(request) return _outcome(request) def publish(request): publications.append(request) return _publisher(request) runner = _runner(tmp_path, ["alpha", "beta", "gamma", "delta"], execute, publish) result = runner.run() assert [rnd.name for rnd in result.rounds] == [ "Qualifiers", "Semifinals", "Championship", ] assert len(requests) == 8 assert len(publications) == 1 assert result.winner.method == "alpha" assert publications[0].candidate_sha256 == result.winner.candidate_sha256 assert publications[0].replay["candidate_sha256"] == result.winner.candidate_sha256 assert result.hub_repo == "owner/winner" assert result.status == "complete" assert result.publication["candidate_sha256"] == result.winner.candidate_sha256 assert Path(result.winner.output_dir, "model.safetensors").is_file() assert not runner._checkpoint_path.exists() assert all( set(request.intervention_harmful).isdisjoint(request.selection_harmful) for request in requests ) assert all(not Path(request.work_dir, "candidate.json").exists() for request in requests) def test_given_seeded_arms_when_replaying_winner_then_rng_draws_match(tmp_path, rng_states): draws = {} replayed = [] def execute(request): draws[request.round_num, request.method] = _seeded_draw(request.seed) return _outcome(request) def publish(request): replay = request.replay replayed.append(_seeded_draw(replay["seed"])) assert replayed[-1] == draws[replay["round_num"], replay["method"]] return _publisher(request) runner = _runner(tmp_path, list(_SEEDED_METHODS), execute, publish, seed=20261024) result = runner.run() assert result.status == "complete" assert len(draws) == 13 assert len(replayed) == 1 @pytest.mark.parametrize( ("metrics", "reason"), [ (_metrics("alpha", kl=0.6), "exceeds runner budget"), ({"refusal_rate": 0.1, "coherence": 0.9, "kl_divergence": 0.1}, "perplexity is unavailable"), ({"refusal_rate": 0.1, "coherence": 2.0, "kl_divergence": 0.1, "perplexity": 10.0}, "coherence is outside"), ], ) def test_runner_quality_policy_cannot_be_bypassed_by_executor(tmp_path, metrics, reason): runner = _runner(tmp_path, ["alpha"], lambda request: _outcome(request, metrics)) runner._prepare_experiment() contender = runner._run_method( "alpha", ["harm"], ["safe"], str(runner.output_dir / "arm"), 1, ["selection-harm"], ["selection-safe"], 1, ) assert not contender.eligible assert contender.score == -1.0 assert any(reason in item for item in contender.ineligible_reasons) def test_inconclusive_spectral_diagnostic_is_advisory(tmp_path): runner = _runner(tmp_path, ["alpha"], _outcome) runner._prepare_experiment() contender = runner._run_method( "alpha", ["harm"], ["safe"], str(runner.output_dir / "arm"), 1, ["selection-harm"], ["selection-safe"], 1, ) assert contender.eligible assert contender.spectral_cert == "INCONCLUSIVE" def test_resume_rejects_changed_candidate_artifact_before_next_arm(tmp_path): calls = [] def interrupted(request): calls.append(request.method) if request.method == "beta": raise RuntimeError("GPU quota exceeded") return _outcome(request) runner = _runner(tmp_path, ["alpha", "beta"], interrupted) iterator = runner.run_iter() assert "alpha" in next(iterator)[0] assert "beta" in next(iterator)[0] with pytest.raises(RuntimeError, match="GPU quota exceeded"): next(iterator) evidence = runner.output_dir / "r1_alpha" / "candidate.json" evidence.write_bytes(b"changed") resumed = _runner(tmp_path, ["alpha", "beta"], interrupted, resume=True) with pytest.raises(tourney.TournamentIntegrityError, match="identity mismatch"): next(resumed.run_iter()) assert calls == ["alpha", "beta"] assert resumed._checkpoint_path.exists() def test_resume_rejects_changed_executor_identity(tmp_path): runner = _runner(tmp_path, ["alpha"], _outcome) runner._prepare_experiment() tourney._save_checkpoint( output_dir=runner.output_dir, result=tourney.TourneyResult(model=runner.model_name), current_round_num=1, current_round_name="Qualifiers", current_round_volume=64, current_round_advance=2, current_round_verify=20, completed_methods=[], remaining_methods=["alpha"], alive=["alpha"], model_name=runner.model_name, dataset_key=runner.dataset_key, quantization=runner.quantization, methods=runner.methods, experiment=runner._experiment, ) resumed = tourney.TourneyRunner( "fixture/model", methods=["alpha"], output_dir=str(runner.output_dir), resume=True, arm_executor=_outcome, winner_publisher=_publisher, executor_identity={"schema": 1, "model_revision": "different"}, artifact_budget_bytes=100_000, ) resumed._dataset_prompts = _prompts with pytest.raises(tourney.TournamentIntegrityError, match="experiment identity"): next(resumed.run_iter()) @pytest.mark.parametrize("legacy_policy", (None, "sha256-prefix64-be-v1")) def test_given_legacy_seed_derivation_when_resuming_then_reject_before_execution( tmp_path, legacy_policy, ): calls = [] def execute(request): calls.append(request) return _outcome(request) runner = _runner(tmp_path, ["alpha"], execute) runner._prepare_experiment() experiment = dict(runner._experiment) if legacy_policy is None: experiment.pop("seed_derivation", None) else: experiment["seed_derivation"] = legacy_policy tourney._save_checkpoint( output_dir=runner.output_dir, result=tourney.TourneyResult(model=runner.model_name), current_round_num=1, current_round_name="Qualifiers", current_round_volume=64, current_round_advance=2, current_round_verify=20, completed_methods=[], remaining_methods=["alpha"], alive=["alpha"], model_name=runner.model_name, dataset_key=runner.dataset_key, quantization=runner.quantization, methods=runner.methods, experiment=experiment, ) resumed = _runner(tmp_path, ["alpha"], execute, resume=True) with pytest.raises(tourney.TournamentIntegrityError, match="experiment identity"): next(resumed.run_iter()) assert calls == [] def test_resume_rejects_changed_interruption_point(tmp_path): runner = _runner(tmp_path, ["alpha"], _outcome) runner._prepare_experiment() tourney._save_checkpoint( output_dir=runner.output_dir, result=tourney.TourneyResult(model=runner.model_name), current_round_num=1, current_round_name="Qualifiers", current_round_volume=64, current_round_advance=2, current_round_verify=20, completed_methods=[], remaining_methods=["alpha"], alive=["alpha"], model_name=runner.model_name, dataset_key=runner.dataset_key, quantization=runner.quantization, methods=runner.methods, experiment=runner._experiment, ) checkpoint = tourney._load_checkpoint(runner.output_dir) checkpoint["interrupted_round"]["remaining_methods"] = ["substituted"] tourney.atomic_checkpoint_document(runner._checkpoint_path, checkpoint) runner.resume = True with pytest.raises(tourney.TournamentIntegrityError, match="interruption point"): next(runner.run_iter()) def test_resume_fails_closed_on_existing_invalid_checkpoint(tmp_path): runner = _runner(tmp_path, ["alpha"], _outcome, resume=True) runner._checkpoint_path.write_text("not-json", encoding="utf-8") with pytest.raises(tourney.TournamentIntegrityError, match="unreadable or invalid"): next(runner.run_iter()) def test_publication_failure_retains_checkpoint_and_arm_evidence(tmp_path): checkpoint = tmp_path / "state" / "checkpoint.json" def fail_publication(_request): raise RuntimeError("publication unavailable") runner = _runner( tmp_path, ["alpha"], _outcome, fail_publication, checkpoint_path=str(checkpoint), ) with pytest.raises(RuntimeError, match="publication unavailable"): runner.run() assert checkpoint.is_file() assert (runner.output_dir / "r1_alpha" / "candidate.json").is_file() scored = json.loads( (runner.output_dir / "tourney_results.json").read_text(encoding="utf-8") ) assert scored["status"] == "scored" assert scored["publication"] == {} saved = tourney._load_checkpoint(runner.output_dir, checkpoint) assert saved["completed_rounds"][0]["advanced_to"] == ["alpha"] def test_atomic_checkpoint_failure_preserves_prior_document(tmp_path, monkeypatch): path = tmp_path / "checkpoint.json" tourney.atomic_checkpoint_document(path, {"version": 1, "value": "old"}) def fail_replace(_source, _destination): raise OSError("replace failed") monkeypatch.setattr("obliteratus.tourney_contracts.os.replace", fail_replace) with pytest.raises(OSError, match="replace failed"): tourney.atomic_checkpoint_document(path, {"version": 1, "value": "new"}) assert path.read_text(encoding="utf-8").find('"old"') != -1