"""Interrupted searches and invalid executors cannot leave model edits installed.""" import json from unittest.mock import Mock import optuna import pytest import torch from obliteratus import bayesian_optimizer as bo from tests.test_bayesian_budget import _assert_state, _pipeline, _state pytestmark = pytest.mark.cpu @pytest.fixture def search(monkeypatch, tmp_path): pipeline = _pipeline(tmp_path) pristine = _state(pipeline) executions = [] factory = bo.bayesian_execution_for def track(pipeline): execution = factory(pipeline) execution.release = Mock(wraps=execution.release) executions.append(execution) return execution monkeypatch.setattr(bo, "bayesian_execution_for", track) monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *args: .1) yield pipeline, pristine, executions # Also release snapshots if an assertion fails before the normal cleanup. bo._reset_bayesian_state(pipeline) def _run(pipeline): return bo.run_bayesian_optimization(pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1) def _assert_rolled_back(search): pipeline, pristine, executions = search _assert_state(pipeline, pristine) assert pipeline._bayesian_execution is None assert pipeline._bayesian_execution_captured is False assert len(executions) == 1 executions[0].release.assert_called_once_with() assert executions[0]._snapshots == [] @pytest.mark.parametrize("stage", ["identity", "enqueue", "selected-receipt", "final-cleanup"]) @pytest.mark.parametrize("error_type", [OSError, KeyboardInterrupt]) def test_search_failure_preserves_weights_and_releases_snapshots(search, monkeypatch, tmp_path, stage, error_type): pipeline, _, _ = search error = error_type(f"interrupted {stage}") fail = Mock(side_effect=error) if stage == "identity": monkeypatch.setattr(bo, "_study_identity", fail) elif stage == "enqueue": monkeypatch.setattr(optuna.study.Study, "enqueue_trial", fail) elif stage == "selected-receipt": publish = bo._atomic_json def publish_trial_only(path, payload): if "selected_plan" in payload: fail() return publish(path, payload) monkeypatch.setattr(bo, "_atomic_json", publish_trial_only) else: def cleanup(): if getattr(pipeline, "_bayesian_plan", None) is not None: fail() pipeline._free_gpu_memory = cleanup with pytest.raises(error_type) as caught: _run(pipeline) assert caught.value is error fail.assert_called_once() _assert_rolled_back(search) if stage in {"selected-receipt", "final-cleanup"}: # Completed trial evidence remains usable even when finalization fails. root = tmp_path / ".obliteratus-bayesian" receipts = list(root.glob("*.trial-0.json")) assert len(receipts) == 1 receipt = json.loads(receipts[0].read_text()) assert receipt["state"] == "COMPLETE" assert (root / receipt["study_backup"]).is_file() assert list(root.glob("*.json")) == receipts @pytest.mark.parametrize("budget", [-.1, float("nan"), float("inf")]) def test_invalid_kl_budget_cannot_leave_a_captured_execution(search, budget): pipeline, _, _ = search pipeline.kl_budget = budget with pytest.raises(ValueError, match="KL budget must be finite and nonnegative"): _run(pipeline) assert pipeline.handle.model.generation_calls == 0 _assert_rolled_back(search) def test_existing_study_with_different_identity_cannot_run_a_trial(search, monkeypatch): pipeline, _, _ = search create_study = optuna.create_study def mismatched_study(**kwargs): study = create_study(**kwargs) study.set_user_attr("study_identity", "another-run") return study monkeypatch.setattr(optuna, "create_study", mismatched_study) with pytest.raises(RuntimeError, match="study identity does not match"): _run(pipeline) assert pipeline.handle.model.generation_calls == 0 _assert_rolled_back(search) @pytest.mark.parametrize("objective", [-.1, float("nan"), float("inf")]) def test_invalid_native_kl_objective_cannot_select_a_plan(search, objective): pipeline, _, _ = search pipeline._kl_eval_prompts = ["native reference"] pipeline._baseline_token_logits = [torch.zeros(2, 2)] pipeline._measure_sequence_token_kl = Mock(return_value=objective) with pytest.raises(RuntimeError, match="KL objective is not finite and nonnegative"): _run(pipeline) pipeline._measure_sequence_token_kl.assert_called_once_with() assert pipeline._bayesian_plan is None _assert_rolled_back(search) @pytest.mark.parametrize("phase,fault", [ ("trial", "mutated-plan"), ("trial", "no-target"), ("replay", "mutated-plan"), ("replay", "no-target"), ("replay", "changed-scope"), ]) def test_invalid_executor_result_is_rolled_back(search, monkeypatch, phase, fault): pipeline, pristine, _ = search tracked_factory = bo.bayesian_execution_for def corrupting_factory(pipeline): execution = tracked_factory(pipeline) apply = execution.apply calls = 0 def corrupt(plan): nonlocal calls calls += 1 result = apply(plan) assert any(not torch.equal(value, pristine[name]) for name, value in _state(pipeline).items()) if calls == (1 if phase == "trial" else 2): if fault == "mutated-plan": plan.directions[0].add_(.25) elif fault == "no-target": return bo.BayesianApplyResult(0, ()) else: return bo.BayesianApplyResult(result.applied_count, ("unscored.target",)) return result execution.apply = corrupt return execution monkeypatch.setattr(bo, "bayesian_execution_for", corrupting_factory) message = { "mutated-plan": "mutated the .* plan", "no-target": "changed no target", "changed-scope": "target scope differs from the scored trial", }[fault] with pytest.raises(RuntimeError, match=message): _run(pipeline) assert phase == "replay" bo.replay_bayesian_plan(pipeline) _assert_rolled_back(search)