Files
OBLITERATUS/tests/test_tourney_executor_failure.py

101 lines
3.6 KiB
Python

"""Injected executor failures interrupt the tournament without scoring an arm."""
import pytest
from obliteratus import tourney
from obliteratus.abliterate import PipelineCancelledError
from tests.test_tourney_execution import _outcome, _runner
@pytest.mark.parametrize("completed", [0, 1])
def test_given_executor_failure_when_running_then_checkpoint_precedes_abort(tmp_path, completed):
calls = []
cause = RuntimeError("model load admission rejected")
def execute(request):
calls.append(request.method)
if len(calls) > completed:
raise cause
return _outcome(request)
def publish(request):
pytest.fail("Failed tournament reached winner publication")
runner = _runner(tmp_path, ["alpha", "beta", "gamma"], execute, publish,
retain_arm_artifacts=True)
with pytest.raises(tourney.TournamentExecutionError, match="model load admission") as failure:
runner.run()
assert failure.value.__cause__ is cause
assert calls == ["alpha", "beta", "gamma"][:completed + 1]
checkpoint = tourney._load_checkpoint(runner.output_dir)
assert checkpoint["interrupted_round"]["remaining_methods"] == (
["alpha", "beta", "gamma"][completed:]
)
_result, partials, _remaining, _round = tourney._restore_rounds(checkpoint)
assert [contender.method for contender in partials] == ["alpha"][:completed]
assert all(not contender.error and contender.eligible for contender in partials)
assert not list(runner.output_dir.glob("winner*"))
@pytest.mark.parametrize("error", [
PipelineCancelledError("cancelled", stage="tourney", metric="cancellation"),
tourney.TournamentIntegrityError("invalid evidence"),
tourney.TournamentExecutionError("worker unavailable"),
KeyboardInterrupt(),
SystemExit(3),
])
def test_given_explicit_abort_when_executor_raises_then_exception_is_preserved(tmp_path, error):
def execute(request):
raise error
runner = _runner(tmp_path, ["alpha", "beta"], execute)
with pytest.raises(type(error)) as failure:
runner.run()
assert failure.value is error
def test_given_scored_quality_rejection_when_running_then_next_arm_competes(tmp_path):
calls = []
def execute(request):
calls.append(request.method)
outcome = _outcome(request)
if request.method == "alpha":
outcome.metrics["kl_divergence"] = 0.7
return outcome
runner = _runner(tmp_path, ["alpha", "beta"], execute)
result = runner.run()
assert calls[:2] == ["alpha", "beta"]
rejected = result.rounds[0].contenders[0]
assert not rejected.eligible and rejected.error is None
assert result.winner.method == "beta"
def test_given_no_feasible_candidate_when_running_then_rejection_is_checkpointed(tmp_path):
calls = []
def execute(request):
calls.append(request.method)
if request.method == "alpha":
raise tourney.TournamentArmRejected("completed trials exceed KL budget")
raise RuntimeError("worker unavailable")
runner = _runner(tmp_path, ["alpha", "beta", "gamma"], execute)
with pytest.raises(tourney.TournamentExecutionError, match="worker unavailable"):
runner.run()
assert calls == ["alpha", "beta"]
checkpoint = tourney._load_checkpoint(runner.output_dir)
_result, partials, remaining, _round = tourney._restore_rounds(checkpoint)
assert len(partials) == 1 and partials[0].method == "alpha"
assert partials[0].score == -1.0 and not partials[0].eligible
assert not partials[0].candidate_sha256
assert remaining == ["beta", "gamma"]
runner._validate_restored_contender(partials[0], 1)