Files
OBLITERATUS/tests/test_recipe_concept_pack.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

313 lines
14 KiB
Python

"""Verify concept-pack recipe normalization and actual edits on the tiny CPU model."""
from copy import deepcopy
import json
from pathlib import Path
from jsonschema import Draft202012Validator
import pytest
import torch
import yaml
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.concepts.pack import ConceptPack
from obliteratus.concepts.pack_io import digest
from obliteratus.concepts.registry import ConceptRegistry
from obliteratus.recipes import RecipeError, apply_recipe, default_technique_registry, normalize_recipe
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
from tests.test_concept_extraction_pipeline import small_target
pytestmark = pytest.mark.cpu
TARGET = {"model_id": "tiny", "revision": "local", "structure": {"tie_word_embeddings": True}}
def _record(pack):
from obliteratus.recipes import concept_recipe_to_v2
pack_id = pack.manifest["id"]
if pack.manifest["tier"] == "user" and not pack_id.startswith("user/"):
pack_id = f"user/{pack_id}"
return {
"schema": "obliteratus.model-recipe/v2",
"name": "tiny-concept",
"version": "1.0.0",
"engine": {"min_version": "0.1.4", "normalizer": "recipe-normalizer/1"},
"target": {"scope": "individual", "identity": {"model_id": "tiny", "revision": "local"}},
"inputs": {"concept": {
"kind": "concept_pack", "role": "train", "id": pack_id, "pack_hash": pack.sha256,
}},
"pipeline": {"steps": [concept_recipe_to_v2(pack)]},
}
def _source(record):
return record["pipeline"]["steps"][0]["params"]["direction_source"]
@pytest.mark.parametrize("pack", ConceptRegistry().list_packs(), ids=lambda p: p.manifest["id"])
def test_shipped_pack_conversion_normalizes_offline_and_is_digest_stable(pack):
raw = _record(pack)
step, = raw["pipeline"]["steps"]
expected = {"pain": "steering.activation_addition", "refusal": "abliteration.projection"}
assert step["technique"] == expected[pack.manifest["id"]]
short = deepcopy(raw)
_source(short).clear()
_source(short).update(kind="concept_pack", input="concept")
normalized = normalize_recipe(raw)
assert normalize_recipe(short) == normalize_recipe(normalized) == normalized
assert len(normalized["canonical_sha256"]) == 64
step, = normalized["pipeline"]["steps"]
spec = default_technique_registry().lookup(step["technique"], step["technique_version"])
Draft202012Validator(spec.parameter_schema).validate(step["params"])
source = _source(normalized)
assert source["kind"] == "concept_pack"
assert source["method"] == pack.manifest["recipe"]["method"]
assert source["steering_scale"] == "residual_norm"
assert source["preserve"] == []
if pack.manifest["id"] == "pain":
assert source["layer_select"] == {"strategy": "cv_auc", "folds": 5}
assert source["intervention_layer"] == {"rule": "norm_ratio", "target": 0.6}
assert source["extraction_layer"]["google/gemma-2-2b-it"] == 25
assert source["readout"] == {"position": "final_token", "suffix": " I feel:", "suffix_mode": "ensure"}
else:
assert source["n_directions"] == 4
assert source["intervention_layer"] == {"rule": "same"}
def test_pack_pin_and_named_step_binding_are_required():
record = _record(ConceptRegistry().pack("pain"))
step, = record["pipeline"]["steps"]
step["inputs"] = []
with pytest.raises(RecipeError, match="step input.*concept"):
normalize_recipe(record)
step["inputs"] = ["concept"]
step["params"]["direction_source"] = {"kind": "concept_pack", "input": "concept"}
record["inputs"]["concept"]["pack_hash"] = "0" * 64
with pytest.raises(RecipeError, match="digest mismatch"):
normalize_recipe(record)
def test_explicit_extraction_fields_override_pack_defaults():
record = _record(ConceptRegistry().pack("pain"))
step, = record["pipeline"]["steps"]
step["params"]["direction_source"] = {
"kind": "concept_pack",
"input": "concept",
"readout": {"suffix": ""},
"denoise": None,
"steering_scale": "raw",
"preserve": ["refusal"],
}
normalized = normalize_recipe(record)
source = _source(normalized)
assert source["readout"] == {"position": "final_token", "suffix": "", "suffix_mode": "ensure"}
assert source["denoise"] is None
assert source["steering_scale"] == "raw"
assert source["preserve"] == ["refusal"]
assert normalize_recipe(normalized) == normalized
@pytest.fixture
def local_pack(monkeypatch, tmp_path):
fixture = Path(__file__).parent / "fixtures" / "concept_packs" / "valid_minimal"
manifest = ConceptPack.load(fixture).manifest
target = small_target("mean_pool")
manifest["recipe"] = target.recipe.to_dict()
manifest["sets"] = {"controls": []}
manifest["licence"] = []
files = {}
for name, prompts in [("positive", target.positive), *target.controls.items()]:
prompts = prompts + [f"{prompt} test" for prompt in prompts]
positive = name == "positive"
path = "positive.jsonl" if positive else f"controls/{name}.jsonl"
data = "".join(json.dumps({"id": str(index), "text": text}) + "\n"
for index, text in enumerate(prompts)).encode()
files[path] = data
descriptor = {"path": path, "n": len(prompts), "sha256": digest(data)}
if positive:
manifest["sets"]["positive"] = descriptor
else:
manifest["sets"]["controls"].append({"name": name, **descriptor})
manifest["licence"].append({
"path": path, "attribution": "OBLITERATUS test authors",
"source": "local synthetic fixture", "spdx": "CC0-1.0",
})
files["manifest.yaml"] = yaml.safe_dump(manifest).encode()
pack = ConceptPack(files)
source = tmp_path / "synthetic.cvpack"
pack.write(source)
registry = ConceptRegistry()
registry.load_pack(source)
monkeypatch.setattr("obliteratus.concepts.cli._registry", lambda _: registry)
return pack
@pytest.mark.usefixtures("tiny_model_runtime")
@pytest.mark.parametrize("steer", [False, True], ids=["projection", "steering"])
def test_pack_step_matches_current_extraction_on_tiny_model(local_pack, tmp_path, steer):
path = build_tiny_offline_model(tmp_path / "tiny")
prior = AbliterationPipeline(
str(path), concept=local_pack.to_target(), seed=227, method="basic",
device="cpu", dtype="float32", gate_mode="off",
)
prior._summon()
prior._admit_pipeline_phase("probe")
prior._probe()
prior._admit_pipeline_phase("distill")
prior._distill()
legacy = prior.concept_direction
handle = prior.handle
before = {name: weight.detach().clone() for name, weight in handle.model.named_parameters()}
tokens = handle.tokenizer("helpful test", return_tensors="pt")
with torch.inference_mode():
pristine_logits = handle.model(**tokens).logits
record = _record(local_pack)
step, = record["pipeline"]["steps"]
step["params"].update(seed=227, override_gate="Tiny offline extraction fixture")
if steer:
step["technique"] = "steering.activation_addition"
step["outputs"].append("intervention")
result = apply_recipe(
record, initial_state=handle, target=TARGET, inputs={"concept": "user/synthetic"},
)
actual = result.outputs["concept_direction"]
assert actual.extraction_layers == legacy.extraction_layers == [0]
assert actual.intervention_layers == legacy.intervention_layers
assert actual.directions.keys() == legacy.directions.keys()
for layer in legacy.directions:
torch.testing.assert_close(actual.directions[layer], legacy.directions[layer], rtol=1e-5, atol=1e-6)
torch.testing.assert_close(actual.subspaces[layer], legacy.subspaces[layer], rtol=1e-5, atol=1e-6)
assert result.resolution["input_sha256"] == {"concept": local_pack.sha256}
assert result.resolution["steps"][0]["payload"]["resolved_layers"] == legacy.intervention_layers
assert handle is result.state
changed = [name for name, weight in handle.model.named_parameters() if not torch.equal(weight, before[name])]
if steer:
assert not changed
try:
with torch.inference_mode():
assert not torch.allclose(handle.model(**tokens).logits, pristine_logits)
finally:
result.outputs["intervention"].remove()
with torch.inference_mode():
torch.testing.assert_close(handle.model(**tokens).logits, pristine_logits)
else:
assert changed
def test_steering_requires_a_removable_output():
record = _record(ConceptRegistry().pack("pain"))
record["pipeline"]["steps"][0]["outputs"].remove("intervention")
with pytest.raises(RecipeError, match="removable intervention output"):
normalize_recipe(record)
@pytest.mark.usefixtures("tiny_model_runtime")
def test_refusal_pack_projection_uses_the_legacy_paired_extractor(tmp_path):
from obliteratus.recipes.projection_runtime import apply_projection_step
pack = ConceptRegistry().pack("refusal")
path = build_tiny_offline_model(tmp_path / "tiny")
prior = AbliterationPipeline(
str(path), concept=pack.to_target(), method="advanced", seed=42, device="cpu", dtype="float32",
)
prior._summon()
prior._admit_pipeline_phase("probe")
prior._probe()
prior._admit_pipeline_phase("distill")
prior._distill()
record = _record(pack)
result = apply_projection_step(prior.handle, record, step_id="concept-edit", base_dir=tmp_path)
actual = result.outputs["concept_direction"]
assert actual.extraction_layers == prior.concept_direction.extraction_layers == [0]
assert actual.intervention_layers == prior.concept_direction.intervention_layers
assert result.resolution["input_sha256"] == {"concept": pack.sha256}
torch.testing.assert_close(
actual.directions[0], prior.concept_direction.directions[0], rtol=1e-5, atol=1e-6,
)
@pytest.mark.usefixtures("tiny_model_runtime")
def test_steering_is_removed_if_a_later_recipe_step_fails(local_pack, tmp_path):
from obliteratus.recipes import TechniqueSpec
path = build_tiny_offline_model(tmp_path / "tiny")
pipeline = AbliterationPipeline(str(path), method="basic", device="cpu", dtype="float32")
pipeline._summon()
handle = pipeline.handle
tokens = handle.tokenizer("safe test", return_tensors="pt")
with torch.inference_mode():
pristine_logits = handle.model(**tokens).logits
record = _record(local_pack)
step, = record["pipeline"]["steps"]
step["technique"] = "steering.activation_addition"
step["outputs"].append("intervention")
step["params"]["override_gate"] = "Tiny offline rollback fixture"
def fail(state, **kwargs):
with torch.inference_mode():
assert not torch.allclose(state.model(**tokens).logits, pristine_logits)
raise RecipeError("downstream step failed")
registry = default_technique_registry()
registry.register_technique(TechniqueSpec(
"test.failure", "1", {"type": "object"}, lambda params, context: params,
fail, {"type": "object"},
))
record["pipeline"]["steps"].append({
"id": "fail",
"technique": "test.failure",
"technique_version": "1",
"params": {},
})
with pytest.raises(RecipeError, match="downstream step failed"):
apply_recipe(
record, initial_state=handle, target=TARGET, inputs={"concept": "user/synthetic"},
registry=registry,
)
with torch.inference_mode():
torch.testing.assert_close(handle.model(**tokens).logits, pristine_logits)
@pytest.mark.parametrize("failure", ["value", "gate"])
def test_cleanup_failure_never_replaces_the_step_error_or_its_recipe_conversion(monkeypatch, tmp_path, failure):
"""#659 review: a restore-point removal failure during error cleanup is noted
on the in-flight error; the step still fails with the converted RecipeError."""
from types import SimpleNamespace
from obliteratus.concepts.gate import GateFailed
from obliteratus.concepts.target import ConceptTarget
from obliteratus.models.snapshot import SnapshotError
from obliteratus.recipes import concept_runtime
cleanup_calls = []
def cleanup():
cleanup_calls.append(1)
raise SnapshotError("restore point files could not be fully removed from /x/snap")
handle = SimpleNamespace(model=object(), tokenizer=object(), model_name="tiny", cleanup=cleanup)
error = ValueError("intervention refused") if failure == "value" else GateFailed("gate refused")
monkeypatch.setattr(concept_runtime, "extraction_recipe", lambda _source: None)
monkeypatch.setattr(concept_runtime, "replace", lambda target, **_: target)
monkeypatch.setattr(concept_runtime, "_extract", lambda *a, **k: (handle, SimpleNamespace(expert_directions={})))
monkeypatch.setattr(concept_runtime, "load_preserved_directions", lambda *a, **k: None)
def refuse(*_args, **_kwargs):
raise error
monkeypatch.setattr(concept_runtime, "apply_concept_intervention", refuse)
target = ConceptTarget("user/x", ["a"], {"n": ["b"]}, pack_hash="0" * 64)
with pytest.raises(RecipeError, match="refused") as raised:
concept_runtime.apply_concept_step(
tmp_path, params={"direction_source": {"input": "c", "preserve": []}, "seed": 0,
"override_gate": False},
placement={}, inputs={"c": target}, constraints={}, mode="orthogonalize",
)
assert raised.value.__cause__ is error
assert cleanup_calls == [1]
assert any("/x/snap" in note for note in getattr(error, "__notes__", [])), error