"""CPU-only cooperative cancellation and transient tournament model ownership.""" from __future__ import annotations import gc import base64 import pickle from pathlib import Path import subprocess import sys from threading import Event from types import SimpleNamespace from unittest.mock import Mock import weakref import pytest from obliteratus.abliterate import PipelineCancelledError from obliteratus.tourney import Contender, TourneyCancellation, TourneyRunner, _noop_log @pytest.mark.parametrize("method", ["advanced", "informed"]) @pytest.mark.parametrize("outcome", ["success", "failure", "quota", "cancel", "cleanup_failure"]) def test_method_forwards_lifecycle_and_drops_pipeline_before_allocator_cleanup( tmp_path, monkeypatch, method, outcome, ): cancellation, stage = Event(), Mock() runner = TourneyRunner("fake/model", methods=[method], output_dir=str(tmp_path / "run"), cancellation_event=cancellation, on_stage=stage) calls, model_refs, pipeline_refs, cleanup_calls = [], [], [], [] class Model: pass class Pipeline: def __init__(self, **kwargs): calls.append(kwargs) self.kwargs = kwargs self.handle = SimpleNamespace(model=Model(), tokenizer=Model()) self._quality_metrics = { "refusal_rate": 0.1, "coherence": 0.9, "kl_divergence": 0.1, "perplexity": 10.0, } model_refs.extend([weakref.ref(self.handle.model), weakref.ref(self.handle.tokenizer)]) pipeline_refs.append(weakref.ref(self)) def run(self): self.kwargs["on_stage"](SimpleNamespace(stage="summon", status="done")) model_in_traceback = self.handle.model if outcome == "cancel": cancellation.set() raise PipelineCancelledError("cancelled", stage="verify") if outcome in {"failure", "quota"}: message = "GPU quota exceeded" if outcome == "quota" else "forced failure" raise RuntimeError(message) output = Path(self.kwargs["output_dir"]) output.mkdir(parents=True, exist_ok=True) (output / "model.bin").write_bytes(b"candidate") assert model_in_traceback is not None run_informed = run def cleanup_failed_run(self): cleanup_calls.append(True) if outcome == "cleanup_failure": raise RuntimeError("cleanup failed before dropping the handle") self.handle.model = self.handle.tokenizer = None monkeypatch.setattr("obliteratus.abliterate.AbliterationPipeline", Pipeline) monkeypatch.setattr("obliteratus.informed_pipeline.InformedAbliterationPipeline", Pipeline) cleanup_observations = [] def empty_cache(): gc.collect() cleanup_observations.append(all(reference() is None for reference in model_refs + pipeline_refs)) allocator = Mock(side_effect=empty_cache) monkeypatch.setattr("obliteratus.device.empty_cache", allocator) if outcome in {"cancel", "quota"}: expected = PipelineCancelledError if outcome == "cancel" else RuntimeError with pytest.raises(expected): runner._run_method(method, ["target"], ["control"], str(runner.output_dir / "saved")) else: contender = runner._run_method( method, ["target"], ["control"], str(runner.output_dir / "saved") ) if outcome == "failure": assert contender.score == -1 and "forced failure" in contender.error else: assert contender.error is None and contender.metrics["coherence"] == 0.9 assert calls[0]["cancellation_event"] is cancellation assert calls[0]["on_stage"] is stage stage.assert_called_once() assert cleanup_calls == [True] allocator.assert_called_once() assert cleanup_observations == [True] assert all(reference() is None for reference in model_refs + pipeline_refs) @pytest.mark.parametrize("streaming", [False, True]) @pytest.mark.parametrize("cancel_when", ["before", "after_method"]) @pytest.mark.parametrize("signal", ["local", "shared-file"]) def test_tournament_cancellation_aborts_without_ranking_or_starting_next_contender( tmp_path, monkeypatch, streaming, cancel_when, signal, ): cancellation = Event() if signal == "local" else TourneyCancellation.create(tmp_path) rounds = [] runner = TourneyRunner("fake/model", methods=["advanced", "aggressive"], output_dir=str(tmp_path / "run"), cancellation_event=cancellation, on_round=rounds.append) monkeypatch.setattr(runner, "_load_prompts", lambda volume: (["target"], ["control"])) calls = [] def run_method(method, *args): calls.append(method) cancellation.set() return Contender(method=method, score=1.0, metrics={"coherence": 1.0}) monkeypatch.setattr(runner, "_run_method", run_method) if cancel_when == "before": cancellation.set() with pytest.raises(PipelineCancelledError, match="Tournament cancelled"): if streaming: list(runner.run_iter()) else: runner.run() assert calls == ([] if cancel_when == "before" else ["advanced"]) assert not rounds assert not (runner.output_dir / "tourney_results.json").exists() assert not (runner.output_dir / "tourney_bracket.md").exists() def test_default_runner_bound_gpu_callable_is_pickleable(tmp_path): runner = TourneyRunner("fixture/model", output_dir=str(tmp_path / "run"), on_stage=_noop_log) restored = pickle.loads(pickle.dumps(runner._run_method)) assert restored.__self__ is not runner assert restored.__self__.model_name == "fixture/model" assert restored.__self__._cancellation_event is None assert restored.__self__._on_stage is _noop_log assert restored.__func__ is TourneyRunner._run_method @pytest.mark.parametrize("cancel_from", ["parent", "worker"]) def test_file_cancellation_survives_actual_bound_callable_process_boundary(tmp_path, cancel_from): cancellation = TourneyCancellation.create(tmp_path) runner = TourneyRunner("fixture/model", output_dir=str(tmp_path / "run"), cancellation_event=cancellation, on_stage=_noop_log) payload = base64.b64encode(pickle.dumps(runner._run_method)).decode("ascii") assert not cancellation.is_set() and not cancellation.wait(timeout=0) if cancel_from == "parent": cancellation.set() script = r''' import base64 import pickle import sys from obliteratus.abliterate import PipelineCancelledError method = pickle.loads(base64.b64decode(sys.argv[1])) signal = method.__self__._cancellation_event if sys.argv[2] == "parent": assert signal.is_set(), "parent signal was lost in serialized state" else: assert not signal.is_set() signal.set() try: method("advanced", ["target"], ["control"], "unused") except PipelineCancelledError: print("cancelled before allocating a pipeline") else: raise AssertionError("serialized contender ignored cancellation") ''' result = subprocess.run([sys.executable, "-c", script, payload, cancel_from], capture_output=True, text=True, timeout=60, check=False) assert result.returncode == 0, result.stdout + result.stderr assert "cancelled before allocating a pipeline" in result.stdout assert cancellation.is_set() and cancellation.wait(timeout=0) rebound = pickle.loads(base64.b64decode(payload)) with pytest.raises(PipelineCancelledError, match="Tournament cancelled"): rebound("advanced", ["target"], ["control"], "unused") def test_local_event_serialization_rejects_unsupported_process_boundary(tmp_path): runner = TourneyRunner("fixture/model", output_dir=str(tmp_path / "run"), cancellation_event=Event()) with pytest.raises(TypeError, match="bridge it to TourneyCancellation on a shared filesystem"): pickle.dumps(runner._run_method) @pytest.mark.parametrize("callback", ["on_log", "on_stage", "on_round"]) def test_process_boundary_does_not_silently_drop_unpickleable_callbacks(tmp_path, callback): runner = TourneyRunner("fixture/model", output_dir=str(tmp_path / "run"), **{callback: lambda value: None}) with pytest.raises((AttributeError, pickle.PicklingError), match="pickle"): pickle.dumps(runner._run_method) def test_cancellation_directory_removal_fails_closed(tmp_path): directory = tmp_path / "signal" directory.mkdir() cancellation = TourneyCancellation.create(directory) directory.rmdir() assert cancellation.is_set() def test_cancellation_requires_absolute_shared_path(): with pytest.raises(ValueError, match="absolute shared filesystem path"): TourneyCancellation("relative/signal")