"""Public failure cleanup for real folded-writer CPU recipes.""" import pytest import torch from obliteratus.abliterate import PipelineFailure from tests.test_concept_intervention import _tiny_residual_model from tests.test_excise_failures import TARGETS, inject_projection_failure, prepare_public_run from tests.test_pipeline_post_norms import _pipeline @pytest.mark.parametrize("architecture", ["gemma2", "gemma3_text", "olmo2"]) @pytest.mark.parametrize("target", TARGETS) @pytest.mark.parametrize("error_type", [RuntimeError, AttributeError, torch.OutOfMemoryError]) def test_folded_projection_failures_use_the_public_terminal_contract( monkeypatch, tmp_path, architecture, target, error_type, ): # Match the existing folded-recipe fixtures: Gemma3 text uses the generic # layer locator, which requires a multi-layer ModuleList. pipeline = _pipeline(_tiny_residual_model(architecture, layers=2), use_sae_features=True, projection_target="all", output_dir=str(tmp_path / "checkpoint")) pipeline._strong_layers = [0] direction, sae = torch.eye(16)[:2] pipeline.concept_directions = {0: direction} pipeline.concept_subspaces = {0: direction[None]} pipeline._sae_directions = {0: sae[None]} handle = pipeline.handle events = [] pipeline._on_stage = events.append error = error_type("injected folded writer failure") injected = inject_projection_failure(monkeypatch, pipeline, target, error) verify, export = prepare_public_run(pipeline, monkeypatch) with pytest.raises(PipelineFailure) as caught: pipeline.run() assert injected == [target] assert pipeline._folded_layer_scales[0] # Exercise folding, not the ordinary warning path. assert caught.value.stage == "excise" if error_type is torch.OutOfMemoryError: assert caught.value.metric == "memory_exhaustion" assert caught.value.__cause__ is error else: assert caught.value.metric == "projection" assert "post-norm folding failed" in str(caught.value) assert isinstance(caught.value.__cause__, ValueError) assert caught.value.__cause__.__cause__ is error assert handle.model is None and handle.tokenizer is None assert [event.status for event in events if event.stage == "excise"] == ["running", "error"] assert pipeline._excise_modified_count is None assert not pipeline.output_dir.exists() verify.assert_not_called() export.assert_not_called()