Files
OBLITERATUS/tests/test_runner_boundaries.py
Joseph Magly c21275ae5f fix(models): never let snapshot cleanup replace an in-flight error; close interrupt windows
Muse review r1 and the Muse BT6 audit for #659: _close_restore_point stores a
plain closed/unavailable policy naming the snapshot path right after
detaching, and reports closed only after close() returns; DiskSnapshot.close()
marks the snapshot close_failed before removing anything, so an interrupted
removal can never leave a snapshot that is accepted again. New
cleanup_on_exit/cleanup_during_error helpers attach a cleanup failure as a
note while an error is propagating and raise it otherwise; every production
ModelHandle cleanup site in a finally or failure path uses them. Removal clears
Windows read-only bits once and reports a count and the first failing paths;
the entry-point test registers cleanup before summoning and checks its own
paths.
2026-11-25 03:47:19 -05:00

238 lines
9.1 KiB
Python

"""Focused orchestration contracts for study execution."""
from __future__ import annotations
from unittest.mock import Mock
import pytest
from obliteratus import runner
from obliteratus.config import DatasetConfig, ModelConfig, StrategyConfig, StudyConfig
def test_run_study_passes_configured_quantization_to_loader(tmp_path, monkeypatch):
config = StudyConfig(
model=ModelConfig(name="fixture", quantization="4bit"),
dataset=DatasetConfig(name="fixture"),
strategies=[StrategyConfig(name="layer_removal")],
output_dir=str(tmp_path),
)
load_model = Mock(side_effect=RuntimeError("stop after loader boundary"))
monkeypatch.setattr(runner, "load_model", load_model)
with pytest.raises(RuntimeError, match="stop after loader boundary"):
runner.run_study(config)
assert load_model.call_args.kwargs["quantization"] == "4bit"
@pytest.mark.parametrize("failure", [None, "apply", "evaluate", "restore", "admission"])
def test_study_restore_lifecycle_with_skipped_initial_snapshot(tmp_path, monkeypatch, failure):
from types import SimpleNamespace
import torch
from obliteratus.models.loader import ModelHandle
from obliteratus.models.snapshot import SnapshotError
model = torch.nn.Linear(3, 2)
pristine = model.weight.detach().clone()
handle = ModelHandle(model, None, SimpleNamespace(model_type="fixture"), "fixture", "causal_lm")
config = StudyConfig(
model=ModelConfig(name="fixture"), dataset=DatasetConfig(name="fixture"),
strategies=[StrategyConfig(name="layer_removal")], output_dir=str(tmp_path),
)
evaluations = []
class Evaluation:
def __init__(self, **kwargs):
pass
def evaluate(self):
assert handle._restore_point.state == "ready"
evaluations.append(model.weight.detach().clone())
if len(evaluations) == 2 and failure == "evaluate":
raise RuntimeError("evaluate failure")
return {"perplexity": 1.0}
spec = SimpleNamespace(component="fixture", strategy_name="layer_removal", description="fixture", metadata={})
def apply(*_):
assert handle._restore_point.state == "ready"
with torch.no_grad():
model.weight.zero_()
if failure == "apply":
raise RuntimeError("apply failure")
monkeypatch.setattr(runner, "load_model", lambda **_: handle)
monkeypatch.setattr(runner, "load_dataset", lambda **_: ["fixture"])
monkeypatch.setattr(runner, "Evaluator", Evaluation)
monkeypatch.setattr(runner, "get_strategy", lambda _: SimpleNamespace(enumerate=lambda *_: [spec], apply=apply))
report = Mock()
monkeypatch.setattr(runner, "AblationReport", lambda **_: report)
if failure == "admission":
monkeypatch.setattr(handle, "ensure_snapshot", Mock(side_effect=SnapshotError("admission failure")))
if failure == "restore":
monkeypatch.setattr(handle, "restore", Mock(side_effect=SnapshotError("restore failure")))
if failure:
with pytest.raises(RuntimeError, match=failure):
runner.run_study(config)
report.save_json.assert_not_called()
report.add_result.assert_not_called()
else:
assert runner.run_study(config) is report
report.add_result.assert_called_once()
report.save_json.assert_called_once()
if failure != "restore":
torch.testing.assert_close(model.weight, pristine)
assert handle._restore_point is None
assert len(evaluations) == (0 if failure == "admission" else 1 if failure == "apply" else 2)
def test_batch_strategy_is_applied_and_evaluated_as_one_composed_edit(tmp_path, monkeypatch):
from types import SimpleNamespace
import torch
from obliteratus.models.loader import ModelHandle
model = torch.nn.Linear(3, 2)
pristine = model.weight.detach().clone()
handle = ModelHandle(model, None, SimpleNamespace(model_type="fixture"), "fixture", "causal_lm")
config = StudyConfig(
model=ModelConfig(name="fixture"),
dataset=DatasetConfig(name="fixture"),
strategies=[StrategyConfig(name="vocab_surgery")],
output_dir=str(tmp_path),
)
evaluations = []
class Evaluation:
def __init__(self, **kwargs):
pass
def evaluate(self):
evaluations.append(model.weight.detach().clone())
return {"perplexity": 1.0}
specs = [
SimpleNamespace(component="token_1", strategy_name="vocab_surgery"),
SimpleNamespace(component="token_2", strategy_name="vocab_surgery"),
]
apply_batch = Mock()
def mutate(_handle, received):
assert received == specs
with torch.no_grad():
model.weight.zero_()
return {"unique_tokens_modified": 2}
apply_batch.side_effect = mutate
strategy = SimpleNamespace(
batch_apply=True,
enumerate=lambda *_args, **_kwargs: specs,
apply_batch=apply_batch,
)
monkeypatch.setattr(runner, "load_model", lambda **_: handle)
monkeypatch.setattr(runner, "load_dataset", lambda **_: ["fixture"])
monkeypatch.setattr(runner, "Evaluator", Evaluation)
monkeypatch.setattr(runner, "get_strategy", lambda _: strategy)
report = Mock()
monkeypatch.setattr(runner, "AblationReport", lambda **_: report)
assert runner.run_study(config) is report
apply_batch.assert_called_once_with(handle, specs)
assert len(evaluations) == 2
torch.testing.assert_close(evaluations[0], pristine)
assert torch.count_nonzero(evaluations[1]) == 0
torch.testing.assert_close(model.weight, pristine)
report.add_result.assert_called_once()
result = report.add_result.call_args.args[0]
assert result.component == "batch"
assert result.metadata["summary"]["unique_tokens_modified"] == 2
def test_run_study_records_and_plots_the_configured_impact_metric(tmp_path, monkeypatch):
"""#606: the first configured metric is saved as the replayable impact metric."""
import json
from types import SimpleNamespace
import torch
from obliteratus.models.loader import ModelHandle
from obliteratus.reporting.report import AblationReport
model = torch.nn.Linear(3, 2)
handle = ModelHandle(model, None, SimpleNamespace(model_type="fixture"), "fixture", "causal_lm")
config = StudyConfig(
model=ModelConfig(name="fixture"), dataset=DatasetConfig(name="fixture"),
strategies=[StrategyConfig(name="layer_removal")], output_dir=str(tmp_path),
metrics=["f1", "accuracy"],
)
scores = iter([{"f1": 1.0, "accuracy": 1.0}, {"f1": 0.73, "accuracy": 0.75}])
class Evaluation:
def __init__(self, **kwargs):
pass
def evaluate(self):
return next(scores)
spec = SimpleNamespace(
component="layer_0", strategy_name="layer_removal", description="d", metadata={},
)
strategy = SimpleNamespace(enumerate=lambda *_: [spec], apply=lambda *_: None)
monkeypatch.setattr(runner, "load_model", lambda **_: handle)
monkeypatch.setattr(runner, "load_dataset", lambda **_: ["fixture"])
monkeypatch.setattr(runner, "Evaluator", Evaluation)
monkeypatch.setattr(runner, "get_strategy", lambda _: strategy)
plotted = []
original = AblationReport.plot_impact
def spy(self, metric=None, output_path=None, **kwargs):
plotted.append(metric)
return original(self, metric=metric, output_path=output_path, **kwargs)
monkeypatch.setattr(AblationReport, "plot_impact", spy)
runner.run_study(config)
assert json.loads((tmp_path / "results.json").read_text())["primary_metric"] == "f1"
assert plotted == ["f1"]
assert (tmp_path / "impact.png").is_file() and (tmp_path / "heatmap.png").is_file()
@pytest.mark.parametrize("study_failure", [True, False])
def test_study_cleanup_failure_is_noted_on_inflight_error_and_raised_otherwise(
tmp_path, monkeypatch, study_failure,
):
"""#659 review: a cleanup SnapshotError never replaces the study's own
failure, but is raised when the study itself succeeded."""
from types import SimpleNamespace
from obliteratus.models.snapshot import SnapshotError
cleanup = Mock(side_effect=SnapshotError("files could not be fully removed from /x/snap"))
handle = SimpleNamespace(ensure_snapshot=Mock(), cleanup=cleanup)
monkeypatch.setattr(runner, "load_model", lambda **_: handle)
original = ValueError("study failed")
report = object()
def run_loaded(*_):
if study_failure:
raise original
return report
monkeypatch.setattr(runner, "_run_loaded_study", run_loaded)
config = StudyConfig(
model=ModelConfig(name="fixture"), dataset=DatasetConfig(name="fixture"),
strategies=[StrategyConfig(name="layer_removal")], output_dir=str(tmp_path),
)
if study_failure:
with pytest.raises(ValueError, match="study failed") as raised:
runner.run_study(config)
assert raised.value is original
assert any("/x/snap" in note for note in getattr(original, "__notes__", []))
else:
with pytest.raises(SnapshotError, match="/x/snap"):
runner.run_study(config)
cleanup.assert_called_once_with()