Files
OBLITERATUS/tests/test_bound_presets.py
Joseph Magly 134bd2c32c feat(recipes): move recipe tools to v2 pipelines; v1 read-only
recipe search, confirm, promote and obliterate --recipe now operate on
v2 recipes: search grids are v2 step variations (recipe-search-grid v2,
same preregistered selection rule), confirmation and promotion consume
evaluation profiles and bind evidence to applied resolution records,
and --recipe applies pipelines through apply_recipe. v1 recipes and
grids load through mechanical migration with a notice; writing v1 is
refused. Shipped v1 data is migrated to v2, and the two recipes
confirmed on the protocol final test in the initial core run ship as
accepted v2 data: llama3.1-8b-e03-reg02 and qwen2.5-1.5b-mid10-18-dim1,
with KL-advisory profiles and value-only confirmation evidence.

Fixes #571
2026-11-21 01:27:13 -05:00

250 lines
10 KiB
Python

"""Offline edit and evaluation contracts for model-bound method presets."""
from hashlib import sha256
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
import torch
from obliteratus import abliterate, checkpoint_evaluation, cli
from obliteratus.recipes import RecipeError, RecipeRegistry
from tests.test_recipe_application import MODEL, REVISION, recipe
pytestmark = pytest.mark.cpu
STOCK = "Qwen/Qwen3.8-27B"
STOCK_REVISION = "1d4bf0f2ff6012fd82039f2fa52739d0dd7c60c0"
@pytest.fixture
def edit_stages(monkeypatch):
for name in (
"_summon", "_free_gpu_memory", "_admit_pipeline_phase", "_capture_stock_baseline",
"_validate_architecture_surgery_support", "_probe", "_distill", "_validate_concept_gate",
"_prepare_concept_preservation", "_capture_concept_evaluation", "_capture_concept_repair",
"_capture_baseline_kl_logits", "_verify",
):
monkeypatch.setattr(abliterate.AbliterationPipeline, name, Mock())
model = torch.nn.Linear(2, 2, bias=False)
model.weight.data.fill_(1)
def edit():
model.weight.data.zero_()
excise = Mock(side_effect=edit)
monkeypatch.setattr(abliterate.AbliterationPipeline, "_excise", excise)
monkeypatch.setattr(
abliterate.AbliterationPipeline, "_rebirth", lambda self: self.output_dir,
)
return model, excise
@pytest.mark.parametrize("method", ["qwen38_e01", "qwen38_e02", "qwen38_e03"])
@pytest.mark.parametrize("identity", ["other-model", "other-revision", "unknown"])
def test_bound_preset_rejects_unbound_edits_before_excise(tmp_path, edit_stages, method, identity):
model_id, revision = {
"other-model": ("other/Model", STOCK_REVISION),
"other-revision": (STOCK, "a" * 40),
"unknown": (None, None),
}[identity]
options = {} if identity == "unknown" else {
"recipe_model_id": model_id, "recipe_revision": revision,
}
pipeline = abliterate.AbliterationPipeline(str(tmp_path), method=method, **options)
model, excise = edit_stages
with pytest.raises(RecipeError, match="allow-recipe-transfer"):
pipeline.run()
excise.assert_not_called()
assert torch.equal(model.weight, torch.ones_like(model.weight))
@pytest.mark.parametrize("method", ["qwen38_e01", "qwen38_e02", "qwen38_e03"])
def test_pinned_snapshot_can_edit_without_transfer(tmp_path, edit_stages, method):
snapshot = tmp_path / "models--Qwen--Qwen3.8-27B" / "snapshots" / STOCK_REVISION
snapshot.mkdir(parents=True)
pipeline = abliterate.AbliterationPipeline(str(snapshot), method=method)
model, excise = edit_stages
assert pipeline.run() == pipeline.output_dir
excise.assert_called_once()
assert torch.count_nonzero(model.weight) == 0
assert pipeline._build_metadata().get("preset_binding", {}).get("application") != "transfer"
def test_explicit_local_identity_can_edit_without_transfer(tmp_path, edit_stages):
pipeline = abliterate.AbliterationPipeline(
str(tmp_path), method="qwen38_e03", recipe_model_id=STOCK, recipe_revision=STOCK_REVISION,
)
assert pipeline.run() == pipeline.output_dir
edit_stages[1].assert_called_once()
def test_snapshot_identity_cannot_be_overridden_to_bypass_binding(tmp_path, edit_stages):
snapshot = tmp_path / "models--org--Model" / "snapshots" / REVISION
snapshot.mkdir(parents=True)
pipeline = abliterate.AbliterationPipeline(
str(snapshot), method="qwen38_e03", recipe_model_id=STOCK, recipe_revision=STOCK_REVISION,
)
with pytest.raises(RecipeError, match="allow-recipe-transfer"):
pipeline.run()
edit_stages[1].assert_not_called()
def test_hub_identity_uses_the_pinned_revision_for_loading(monkeypatch):
load = Mock(side_effect=RuntimeError("stop at load"))
monkeypatch.setattr(abliterate, "load_model", load)
pipeline = abliterate.AbliterationPipeline(STOCK, method="qwen38_e03", recipe_revision=STOCK_REVISION)
with pytest.raises(RuntimeError, match="stop at load"):
pipeline.run()
assert load.call_args.kwargs["model_name"] == STOCK
assert load.call_args.kwargs["revision"] == STOCK_REVISION
@pytest.mark.parametrize("identity", [{}, {"recipe_model_id": MODEL, "recipe_revision": REVISION}])
def test_explicit_transfer_edits_and_records_provenance(tmp_path, edit_stages, caplog, identity):
pipeline = abliterate.AbliterationPipeline(
str(tmp_path), method="qwen38_e03", allow_recipe_transfer=True, **identity,
)
pipeline.run()
edit_stages[1].assert_called_once()
assert pipeline._build_metadata()["preset_binding"]["application"] == "transfer"
assert "transfer" in caplog.text.lower()
@pytest.mark.parametrize("allow_transfer", [False, True])
def test_cli_forwards_preset_identity_and_transfer(tmp_path, monkeypatch, allow_transfer):
pipeline = Mock(return_value=SimpleNamespace(run=Mock(return_value=tmp_path)))
monkeypatch.setattr(abliterate, "AbliterationPipeline", pipeline)
monkeypatch.setattr("obliteratus.telemetry.maybe_send_pipeline_report", Mock())
flags = ["--allow-recipe-transfer"] if allow_transfer else []
cli.main([
"obliterate", str(tmp_path), "--method", "qwen38_e03",
"--recipe-model-id", MODEL, "--recipe-revision", REVISION, *flags,
])
assert pipeline.call_args.kwargs["recipe_model_id"] == MODEL
assert pipeline.call_args.kwargs["recipe_revision"] == REVISION
assert pipeline.call_args.kwargs["allow_recipe_transfer"] is allow_transfer
def test_bound_preset_cli_rejects_remote_dispatch(monkeypatch, capsys):
remote = Mock()
monkeypatch.setattr(cli, "_cmd_remote_abliterate", remote)
with pytest.raises(SystemExit) as error:
cli.main(["obliterate", STOCK, "--method", "qwen38_e03", "--remote", "host"])
assert error.value.code == 2
assert "local obliterate run" in capsys.readouterr().err
remote.assert_not_called()
def test_checkpoint_evaluation_uses_real_pipeline_without_enforcing_edit_binding(tmp_path, monkeypatch):
pipeline_type = abliterate.AbliterationPipeline
stages = []
def verify(pipeline):
stages.append("verify")
pipeline._quality_metrics.update(
refusal_rate=0.0, coherence=1.0, total_count=1,
capability_total_count=0, capability_completed_count=0,
)
def configure(pipeline):
assert isinstance(pipeline, pipeline_type)
assert pipeline.method == "qwen38_e03"
assert pipeline.model_name == str(tmp_path)
stages.append("configure")
monkeypatch.setattr(checkpoint_evaluation, "from_environment", Mock())
monkeypatch.setattr(checkpoint_evaluation, "measure_torch_memory", Mock())
monkeypatch.setattr(pipeline_type, "_summon", lambda self: stages.append("summon"))
monkeypatch.setattr(pipeline_type, "_admit_pipeline_phase", Mock())
monkeypatch.setattr(pipeline_type, "_verify", verify)
monkeypatch.setattr(pipeline_type, "run", Mock(side_effect=AssertionError("evaluation cannot edit")))
metrics = checkpoint_evaluation.evaluate_checkpoint(
tmp_path, partition="optimizer_tune", max_prompts=1, configure=configure,
source_metrics={"baseline_perplexity": 3.0, "baseline_coherence": 1.0},
)
assert stages == ["summon", "configure", "verify"]
assert metrics["passes_objective"] and metrics["checkpoint_reloaded"]
@pytest.mark.parametrize(
("confirmation_case", "expected"),
[
("passing", "confirmed-family-member"),
("absent", "family-application"),
("failed", "family-application"),
("other-version", "family-application"),
("other-digest", "family-application"),
("other-model", "family-application"),
("other-revision", "family-application"),
("overridden", "family-application"),
],
)
def test_family_label_requires_passing_confirmation_of_unchanged_recipe(
tmp_path, monkeypatch, confirmation_case, expected,
):
from obliteratus.recipes import application
from obliteratus.recipes.confirmation import run_confirmation
from tests.test_recipe_confirmation import Runner, write
record = recipe("family", match={"model_type": "llama"}, regularization=0.25)
directory = tmp_path / "recipes"
directory.mkdir()
path = directory / "family.json"
margins = directory / record["margins"]["spec_path"]
margins.parent.mkdir()
record["margins"]["spec_sha256"] = write(margins, {})
write(path, record)
stock = tmp_path / "stock"
stock.mkdir()
write(stock / "config.json", {
"model_type": "llama", "num_hidden_layers": 8, "hidden_size": 4096,
"architectures": ["LlamaForCausalLM"], "num_parameters": 8_000_000_000,
})
confirmed_id = "other/Model" if confirmation_case == "other-model" else MODEL
confirmed_revision = "b" * 40 if confirmation_case == "other-revision" else REVISION
confirmation = run_confirmation(
path, stock=stock, output_dir=tmp_path / "runs", model_id=confirmed_id,
revision=confirmed_revision, runner=Runner(verdict="fail" if confirmation_case == "failed" else "pass"),
)
assert confirmation["outcome"] == ("fail" if confirmation_case == "failed" else "pass")
assert confirmation["recipe"]["source"]["sha256"] == sha256(path.read_bytes()).hexdigest()
if confirmation_case == "other-version":
confirmation["recipe"]["version"] = "1.0.0"
if confirmation_case == "other-digest":
confirmation["recipe"]["source"]["sha256"] = "0" * 64
registry = RecipeRegistry(directory)
registry._confirmations = () if confirmation_case == "absent" else (confirmation,)
monkeypatch.setattr(application, "RecipeRegistry", lambda _: registry)
monkeypatch.setattr(
application, "_profile_recipe_source",
lambda *args: SimpleNamespace(model=MODEL, model_type="llama", num_layers=8),
)
overrides = {"regularization": 0.5} if confirmation_case == "overridden" else {}
args = SimpleNamespace(
recipe="auto", model=str(tmp_path), recipe_model_id=MODEL, recipe_revision=REVISION,
allow_candidate_recipe=False, allow_recipe_transfer=False, allow_recipe_override=True,
_explicit_recipe_options=overrides,
)
_, _, metadata = application.prepare_recipe(args, directory=directory)
assert metadata["application"] == expected