mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
1452 lines
69 KiB
Python
1452 lines
69 KiB
Python
"""CV-08 comparisons, provenance, source exclusion and publication policy."""
|
|
|
|
from copy import deepcopy
|
|
import ast
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
from unittest.mock import Mock
|
|
|
|
from jsonschema import Draft202012Validator
|
|
import pytest
|
|
from safetensors.torch import save_file
|
|
import torch
|
|
|
|
from obliteratus.cli import main
|
|
from obliteratus.concepts import ConceptDirection
|
|
from obliteratus.concepts.export import (
|
|
OUTCOME_TEMPLATES, export_run, lint_outcome_language, model_card, validate_provenance,
|
|
)
|
|
from obliteratus.concepts.pack import ConceptPack, builtin_refusal_pack
|
|
from obliteratus.concepts.runs import (
|
|
PublicationBlocked, SCHEMA_PATH, check_checkpoint_publication, check_publication, load_run,
|
|
run_record, save_concept_run, validate_run, write_json,
|
|
)
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PACK_FIXTURE = ROOT / "tests/fixtures/concept_packs/valid_minimal"
|
|
ARTIFACT_SCHEMA = ROOT / "docs/checkpoints/schemas/artifact-provenance-v1.schema.json"
|
|
|
|
|
|
def test_affine_run_export_preserves_override_means_and_replay(tmp_path, monkeypatch):
|
|
from obliteratus.concepts.intervention import InterventionRun
|
|
|
|
direction = ConceptDirection(
|
|
{0: torch.tensor([1., 0., 0.])},
|
|
subspaces={0: torch.eye(3)[:2]},
|
|
reference_means={0: torch.zeros(3)},
|
|
reference_provenance={"source": "extraction"},
|
|
)
|
|
overrides = {0: torch.tensor([2., 3., 4.]), 1: torch.tensor([5., 6., 7.])}
|
|
provenance = {"source": "heldout calibration", "seed": 335}
|
|
geometry = {"projector": "orthogonal", "ranks": {"0": 2, "1": 2}}
|
|
run = InterventionRun(
|
|
"project", [0, 1], "all_tokens", {},
|
|
parameters={"layers": [0, 1], "subspace": True, "reference": "mean",
|
|
"geometry": geometry, "affine_bias": {"test": "descriptive only"}},
|
|
reference_means=overrides, reference_provenance=provenance,
|
|
)
|
|
source, output = tmp_path / "run", tmp_path / "bundle"
|
|
record = save_concept_run(source, direction, run, tool_commit="b" * 40)
|
|
restored = ConceptDirection.load(source / "direction.safetensors")
|
|
for layer, mean in overrides.items():
|
|
assert torch.equal(restored.reference_means[layer], mean)
|
|
assert restored.reference_provenance == provenance
|
|
assert torch.equal(direction.reference_means[0], torch.zeros(3))
|
|
assert direction.reference_provenance == {"source": "extraction"}
|
|
assert record["intervention"]["parameters"]["geometry"] == geometry
|
|
exported = export_run(source, output, kind="direction")
|
|
assert exported["run"] == record
|
|
|
|
replay = Mock(return_value=Mock(config=None))
|
|
monkeypatch.setattr("obliteratus.concepts.apply_concept_intervention", replay)
|
|
monkeypatch.chdir(output)
|
|
model = object()
|
|
exec((output / "loader.py").read_text(), {"model": model})
|
|
args, parameters = replay.call_args
|
|
assert args[0] is model and args[2] == "project"
|
|
assert parameters == {"layers": [0, 1], "subspace": True, "reference": "mean"}
|
|
assert args[1].reference_provenance == provenance
|
|
for layer, mean in overrides.items():
|
|
assert torch.equal(args[1].reference_means[layer], mean)
|
|
sidecar = json.loads((output / "direction.json").read_text())
|
|
assert sidecar["reference_mean_sha256"] == restored.reference_mean_hashes()
|
|
|
|
|
|
@pytest.fixture
|
|
def saved_run(tmp_path):
|
|
def create(mode="orthogonalize", *, collateral="PASS", gate_override=None, tier="user",
|
|
repair=None, refusal=False):
|
|
directory = tmp_path / ("run-" + mode)
|
|
directory.mkdir(exist_ok=True)
|
|
pack = builtin_refusal_pack() if refusal else ConceptPack.load(PACK_FIXTURE)
|
|
direction = ConceptDirection(
|
|
{0: torch.tensor([1., 0.])},
|
|
concept_id="refusal" if refusal else "user/synthetic",
|
|
concept_version=pack.manifest["version"],
|
|
pack_hash=pack.sha256, seed=231, model_id="fixture/tiny", model_revision="a" * 40,
|
|
validation={"verdict": "pass", "metrics": {"0": {"heldout_auc": .9}},
|
|
"override_gate": gate_override},
|
|
)
|
|
direction.save(directory / "direction.safetensors")
|
|
evaluation = {
|
|
"mechanistic": {"status": "removed", "before": {"0": 1.}, "after": {"0": 0.}},
|
|
"behavioural": {"status": "null", "battery": ["fixture"],
|
|
"before": {"fixture": 1}, "after": {"fixture": 1}},
|
|
"collateral": {"status": collateral, "refusal": {
|
|
"n": 320, "status": collateral,
|
|
"pristine": {"rate": 1.}, "intervened": {"rate": 1.},
|
|
}},
|
|
"untested_channels": ["KV-cache-mediated effects: not tested"], "metadata": {},
|
|
}
|
|
record = run_record(
|
|
direction, pack=pack, mode=mode, parameters={"regularization": .1},
|
|
evaluation=evaluation, repair=repair, tool_commit="b" * 40,
|
|
)
|
|
record["pack"]["tier"] = tier
|
|
write_json(directory / "concept_run.json", record)
|
|
(directory / "config.json").write_text('{}')
|
|
save_file({"fixture": torch.ones(2, 2)}, str(directory / "model.safetensors"))
|
|
adapters = directory / "lora_adapters"
|
|
adapters.mkdir(exist_ok=True)
|
|
(adapters / "adapter_config.json").write_text('{"peft_type":"LORA"}')
|
|
save_file({"fixture.lora_A.weight": torch.ones(1, 2)},
|
|
str(adapters / "adapter_model.safetensors"))
|
|
# These plausible neighbouring files must never enter any run bundle.
|
|
(directory / "positive.jsonl").write_bytes(pack.files["positive.jsonl"])
|
|
(directory / "manifest.yaml").write_bytes(pack.files["manifest.yaml"])
|
|
(directory / "third-party.cvpack").write_bytes(b"pack data")
|
|
return directory, record
|
|
return create
|
|
|
|
|
|
@pytest.mark.parametrize("kind,mode", [
|
|
("weights", "orthogonalize"), ("lora", "lora"),
|
|
("direction", "project"), ("report", "steer"),
|
|
])
|
|
def test_all_exports_validate_and_exclude_pack_data(saved_run, tmp_path, kind, mode):
|
|
source, record = saved_run(mode, collateral="FAIL")
|
|
output = tmp_path / "bundle"
|
|
result = export_run(source, output, kind=kind)
|
|
assert json.loads((output / "concept_provenance.json").read_text()) == result
|
|
Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(result)
|
|
Draft202012Validator(json.loads(ARTIFACT_SCHEMA.read_text())).validate(
|
|
result["checkpoint_provenance"]
|
|
)
|
|
validate_provenance(result)
|
|
assert result["run"]["evaluation"]["collateral"]["status"] == "FAIL"
|
|
assert result["run"] == record
|
|
assert result["run"]["validation"]["metrics"]["0"]["heldout_auc"] == .9
|
|
assert result["run"]["repair"]["steps"] == []
|
|
assert result["run"]["model"]["revision"] == "a" * 40
|
|
assert result["run"]["obliteratus"]["commit"] == "b" * 40
|
|
assert not {"positive.jsonl", "manifest.yaml", "third-party.cvpack"} & {
|
|
path.name for path in output.rglob("*")
|
|
}
|
|
for path, digest in result["files"].items():
|
|
assert digest == "sha256:" + hashlib.sha256((output / path).read_bytes()).hexdigest()
|
|
assert "direction removed" in (output / "MODEL_CARD.md").read_text().lower()
|
|
again = export_run(source, tmp_path / "bundle-repeat", kind=kind)
|
|
assert again == result
|
|
assert load_run(output) == record
|
|
|
|
|
|
@pytest.mark.parametrize("project_embeddings", [False, True])
|
|
def test_lora_bundle_preserves_scaled_embedding_export_limitation(
|
|
saved_run, tmp_path, project_embeddings,
|
|
):
|
|
source, record = saved_run("lora")
|
|
record["intervention"]["parameters"]["project_embeddings"] = project_embeddings
|
|
write_json(source / "concept_run.json", record)
|
|
output = tmp_path / "bundle"
|
|
export_run(source, output, kind="lora")
|
|
card = (output / "MODEL_CARD.md").read_text()
|
|
assert ("Embedding._get_embed_scale" in card) is project_embeddings
|
|
assert ("Embedding._get_embed_scale" in model_card(record, publish_card=True)) is project_embeddings
|
|
|
|
|
|
@pytest.mark.parametrize("tie_policy", ["edit_both", "untie"])
|
|
def test_lora_card_preserves_tied_embedding_merge_limitation(saved_run, tie_policy):
|
|
_, record = saved_run("lora")
|
|
record["intervention"]["parameters"].update(
|
|
project_embeddings=True, tied_embeddings=tie_policy,
|
|
)
|
|
assert "keep these adapters unmerged" in model_card(record)
|
|
|
|
|
|
def test_compare_cli_returns_outcomes_and_repair(saved_run, capsys):
|
|
first, _ = saved_run()
|
|
second, _ = saved_run("lora", collateral="FAIL",
|
|
repair={"status": "complete", "steps": [{"step": "norm_preserve"}]})
|
|
main(["concepts", "compare", str(first), str(second), "--json"])
|
|
rows = json.loads(capsys.readouterr().out)
|
|
assert [r["mode"] for r in rows] == ["orthogonalize", "lora"]
|
|
assert [r["collateral"]["status"] for r in rows] == ["PASS", "FAIL"]
|
|
assert rows[0]["verdict"] == "pass"
|
|
assert rows[0]["residual"]["after"] == {"0": 0.}
|
|
assert rows[0]["behaviour"]["status"] == "null"
|
|
assert rows[1]["repair"]["steps"] == [{"step": "norm_preserve"}]
|
|
assert "KV-cache-mediated effects: not tested" in rows[0]["untested_channels"]
|
|
main(["concepts", "compare", str(first), str(second)])
|
|
assert "collateral:" in capsys.readouterr().out
|
|
|
|
|
|
@pytest.mark.parametrize("collateral,override", [("FAIL", None), ("PASS", "exploratory gate")])
|
|
def test_card_guard_exit_five_and_explicit_notice(
|
|
saved_run, tmp_path, capsys, collateral, override,
|
|
):
|
|
source, _ = saved_run(collateral=collateral, gate_override=override)
|
|
output = tmp_path / "card"
|
|
args = ["concepts", "export", str(source), str(output), "--as", "report", "--publish-card"]
|
|
with pytest.raises(SystemExit) as error:
|
|
main(args)
|
|
assert error.value.code == 5
|
|
assert ("--allow-collateral-fail" if collateral == "FAIL" else "requires --reason") in capsys.readouterr().err
|
|
assert (output / "MODEL_CARD.md").is_file()
|
|
assert not (output / "README.md").exists()
|
|
output = tmp_path / "approved-card"
|
|
args[3] = str(output)
|
|
option = "--allow-collateral-fail" if collateral == "FAIL" else "--reason"
|
|
main([*args, option, "exploratory publication", "--json"])
|
|
assert json.loads(capsys.readouterr().out)["artifact_id"].startswith("artifact-sha256:")
|
|
card = (output / "README.md").read_text()
|
|
assert "exploratory publication" in card
|
|
if collateral == "FAIL":
|
|
assert "collateral: `FAIL`" in card
|
|
if override:
|
|
assert "Gate override: exploratory gate" in card
|
|
provenance = json.loads((output / "concept_provenance.json").read_text())
|
|
assert provenance["publication"]["publish_card"] is True
|
|
if collateral == "FAIL":
|
|
assert provenance["publication"]["collateral_override"] == {
|
|
"status": "FAIL", "reason": "exploratory publication",
|
|
}
|
|
assert provenance["publication"]["reason"] is None
|
|
else:
|
|
assert provenance["publication"]["reason"] == "exploratory publication"
|
|
|
|
|
|
@pytest.mark.parametrize("kind,mode", [
|
|
("weights", "orthogonalize"), ("lora", "lora"),
|
|
("direction", "project"), ("report", "steer"),
|
|
])
|
|
def test_research_hub_exports_are_allowed(saved_run, tmp_path, monkeypatch, kind, mode):
|
|
source, _ = saved_run(mode, tier="research")
|
|
api = Mock()
|
|
monkeypatch.setattr("huggingface_hub.HfApi", Mock(return_value=api))
|
|
main(["concepts", "export", str(source), str(tmp_path / "hub"), "--as", kind,
|
|
"--push-to-hub", "fixture/export"])
|
|
api.create_repo.assert_called_once_with("fixture/export", exist_ok=True)
|
|
api.upload_folder.assert_called_once()
|
|
|
|
|
|
@pytest.mark.parametrize("tier", ["core", "user", "research"])
|
|
@pytest.mark.parametrize("kind,mode", [("weights", "orthogonalize"), ("lora", "lora")])
|
|
@pytest.mark.parametrize("publish_card", [False, True])
|
|
@pytest.mark.parametrize("status", ["FAIL", "PASS", "INCONCLUSIVE"])
|
|
@pytest.mark.parametrize("override", [None, " Reviewed measured collateral loss "])
|
|
def test_hub_collateral_matrix(
|
|
saved_run, tmp_path, monkeypatch, tier, kind, mode, publish_card, status, override,
|
|
):
|
|
source, record = saved_run(mode, tier=tier, collateral=status)
|
|
# The aggregate verdict may fail for invalid outputs even with passing refusal rates.
|
|
record["evaluation"]["collateral"]["refusal"]["status"] = "PASS"
|
|
write_json(source / "concept_run.json", record)
|
|
api = Mock()
|
|
api_factory = Mock(return_value=api)
|
|
monkeypatch.setattr("huggingface_hub.HfApi", api_factory)
|
|
output = tmp_path / "hub"
|
|
kwargs = dict(kind=kind, publish_card=publish_card, push_to_hub="fixture/export",
|
|
allow_collateral_fail=override)
|
|
if status == "FAIL" and override is None:
|
|
with pytest.raises(PublicationBlocked, match="--allow-collateral-fail"):
|
|
export_run(source, output, **kwargs)
|
|
api_factory.assert_not_called()
|
|
assert not api.mock_calls
|
|
assert (output / "evaluation.json").is_file()
|
|
assert (output / "MODEL_CARD.md").is_file()
|
|
assert not (output / "README.md").exists()
|
|
assert (output / ("model.safetensors" if kind == "weights"
|
|
else "adapter_model.safetensors")).is_file()
|
|
local = json.loads((output / "concept_provenance.json").read_text())
|
|
validate_provenance(local)
|
|
assert local["publication"]["publish_card"] is False
|
|
return
|
|
result = export_run(source, output, **kwargs)
|
|
api.create_repo.assert_called_once()
|
|
api.upload_folder.assert_called_once()
|
|
validate_provenance(result)
|
|
if status == "FAIL":
|
|
stamp = {"status": "FAIL", "reason": override.strip()}
|
|
assert result["publication"]["collateral_override"] == stamp
|
|
metadata = json.loads((output / "abliteration_metadata.json").read_text())
|
|
assert metadata["concept"]["collateral_override"] == stamp
|
|
card_name = "README.md" if publish_card else "MODEL_CARD.md"
|
|
card = (output / card_name).read_text()
|
|
assert "collateral_override" in card and override.strip() in card and "FAIL" in card
|
|
assert card_name not in (api.upload_folder.call_args.kwargs.get("ignore_patterns") or [])
|
|
else:
|
|
assert not result["publication"].get("collateral_override")
|
|
if status == "INCONCLUSIVE":
|
|
card_name = "README.md" if publish_card else "MODEL_CARD.md"
|
|
assert "> **INCONCLUSIVE refusal collateral" in (output / card_name).read_text()
|
|
assert "MODEL_CARD.md" not in api.upload_folder.call_args.kwargs["ignore_patterns"]
|
|
|
|
|
|
@pytest.mark.parametrize("override", ["", " ", False, 1])
|
|
def test_collateral_override_requires_nonempty_reason(saved_run, tmp_path, override):
|
|
source, _ = saved_run(collateral="FAIL")
|
|
with pytest.raises(ValueError, match="reason|blank"):
|
|
export_run(source, tmp_path / "out", kind="weights", push_to_hub="fixture/export",
|
|
allow_collateral_fail=override)
|
|
assert not (tmp_path / "out").exists()
|
|
|
|
|
|
def test_gate_reason_does_not_override_collateral_failure(saved_run, tmp_path):
|
|
source, record = saved_run(collateral="FAIL", gate_override="gate review")
|
|
with pytest.raises(PublicationBlocked, match="--allow-collateral-fail"):
|
|
export_run(source, tmp_path / "out", kind="weights", publish_card=True,
|
|
reason="gate acknowledgement")
|
|
with pytest.raises(PublicationBlocked, match="--reason"):
|
|
check_publication(record, kind="weights", publish_card=True,
|
|
allow_collateral_fail="collateral acknowledgement")
|
|
assert check_publication(record, kind="weights", publish_card=True,
|
|
reason="gate acknowledgement",
|
|
allow_collateral_fail="collateral acknowledgement") == {
|
|
"status": "FAIL", "reason": "collateral acknowledgement",
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("evidence", ["session", "checkpoint", "evaluation", "run"])
|
|
@pytest.mark.parametrize("tier", ["core", "user", "research"])
|
|
def test_checkpoint_guard_uses_collateral_verdict(saved_run, tmp_path, evidence, tier):
|
|
evaluation = {"collateral": {"status": "FAIL", "refusal": {"status": "PASS"}}}
|
|
metadata = {"concept": {"tier": tier, "evaluation": evaluation}}
|
|
directory, session = tmp_path / "metadata-only", None
|
|
directory.mkdir()
|
|
if evidence == "session":
|
|
session = metadata
|
|
elif evidence == "checkpoint":
|
|
write_json(directory / "abliteration_metadata.json", metadata)
|
|
elif evidence == "evaluation":
|
|
write_json(directory / "evaluation.json", evaluation)
|
|
else:
|
|
directory, _ = saved_run(tier=tier, collateral="FAIL")
|
|
with pytest.raises(PublicationBlocked, match="--allow-collateral-fail"):
|
|
check_checkpoint_publication(directory, metadata=session)
|
|
assert check_checkpoint_publication(
|
|
directory, metadata=session, allow_collateral_fail=" reviewed ",
|
|
) == {"status": "FAIL", "reason": "reviewed"}
|
|
|
|
|
|
def test_publish_card_with_passing_run_needs_no_override(saved_run, tmp_path):
|
|
source, _ = saved_run()
|
|
export_run(source, tmp_path / "out", kind="report", publish_card=True)
|
|
assert (tmp_path / "out/README.md").exists()
|
|
|
|
|
|
@pytest.mark.parametrize("losses,status", [(2, "PASS"), (9, "INCONCLUSIVE"), (64, "FAIL")])
|
|
def test_noninferiority_drives_publication_and_card(saved_run, tmp_path, monkeypatch, losses, status):
|
|
from obliteratus.concepts.statistics import paired_refusal_statistics
|
|
|
|
source, record = saved_run("orthogonalize")
|
|
refusal = paired_refusal_statistics([True] * 320, [False] * losses + [True] * (320 - losses))
|
|
assert refusal["status"] == status
|
|
record["evaluation"]["collateral"] = {"status": status, "refusal": refusal}
|
|
write_json(source / "concept_run.json", record)
|
|
api = Mock()
|
|
monkeypatch.setattr("huggingface_hub.HfApi", Mock(return_value=api))
|
|
output = tmp_path / "scored"
|
|
if status == "FAIL":
|
|
with pytest.raises(PublicationBlocked, match="--allow-collateral-fail"):
|
|
export_run(source, output, kind="weights", publish_card=True, push_to_hub="fixture/export")
|
|
api.create_repo.assert_not_called()
|
|
output = tmp_path / "override"
|
|
export_run(source, output, kind="weights", publish_card=True, push_to_hub="fixture/export",
|
|
allow_collateral_fail="Reviewed" if status == "FAIL" else None)
|
|
api.upload_folder.assert_called_once()
|
|
card = (output / "README.md").read_text()
|
|
assert "`0.03`" in card and str(refusal["drop_ci"]) in card
|
|
assert refusal["decision_rule"] in card
|
|
if status == "INCONCLUSIVE":
|
|
assert "> **INCONCLUSIVE refusal collateral" in card
|
|
|
|
|
|
def test_direct_upload_card_discloses_inconclusive_margin(saved_run):
|
|
from obliteratus.concepts.export import stamp_checkpoint_publication
|
|
from obliteratus.concepts.runs import check_checkpoint_publication
|
|
from obliteratus.concepts.statistics import paired_refusal_statistics
|
|
|
|
source, record = saved_run("orthogonalize")
|
|
refusal = paired_refusal_statistics([True] * 320, [False] * 9 + [True] * 311)
|
|
record["evaluation"]["collateral"] = {"status": "INCONCLUSIVE", "refusal": refusal}
|
|
write_json(source / "concept_run.json", record)
|
|
assert check_checkpoint_publication(source, publish_card=True) is None
|
|
stamp_checkpoint_publication(source, None)
|
|
card = (source / "README.md").read_text()
|
|
assert "> **INCONCLUSIVE refusal collateral" in card and "`0.03`" in card
|
|
assert "collateral_override" not in card
|
|
stamp_checkpoint_publication(source, None)
|
|
assert (source / "README.md").read_text() == card
|
|
|
|
|
|
def test_old_refusal_record_does_not_acquire_new_policy(saved_run):
|
|
_, record = saved_run(collateral="PASS")
|
|
card = model_card(record)
|
|
assert "Legacy refusal evaluation" in card
|
|
assert "`0.03`" not in card
|
|
|
|
|
|
def test_skipped_battery_card_records_configured_policy(saved_run):
|
|
from obliteratus.concepts.statistics import REFUSAL_DECISION_RULE
|
|
|
|
_, record = saved_run()
|
|
record["evaluation"]["collateral"] = {"status": "skipped", "reason": "operator choice"}
|
|
record["evaluation"]["metadata"] = {
|
|
"collateral_margin": 0.05, "refusal_decision_rule": REFUSAL_DECISION_RULE,
|
|
}
|
|
card = model_card(record)
|
|
assert "`0.05`" in card and "Refusal verdict: `skipped`" in card
|
|
assert "not measured" in card and REFUSAL_DECISION_RULE in card
|
|
|
|
|
|
def test_session_inconclusive_notice_survives_empty_disk_metadata(tmp_path):
|
|
from obliteratus.concepts.export import stamp_checkpoint_publication
|
|
|
|
write_json(tmp_path / "abliteration_metadata.json", {})
|
|
metadata = {"concept": {"evaluation": {"collateral": {"status": "INCONCLUSIVE"}}}}
|
|
stamp_checkpoint_publication(tmp_path, None, metadata=metadata)
|
|
assert "> **INCONCLUSIVE refusal collateral" in (tmp_path / "README.md").read_text()
|
|
assert json.loads((tmp_path / "abliteration_metadata.json").read_text()) == metadata
|
|
|
|
|
|
@pytest.mark.parametrize("status", ["INCOMPLETE", "UNDERPOWERED", "INCONCLUSIVE", "skipped", "not tested"])
|
|
def test_other_collateral_statuses_are_preserved(saved_run, tmp_path, status):
|
|
source, _ = saved_run(collateral=status)
|
|
result = export_run(source, tmp_path / "out", kind="report", publish_card=True)
|
|
assert result["run"]["evaluation"]["collateral"]["status"] == status
|
|
assert f"collateral: `{status}`" in model_card(result["run"])
|
|
|
|
|
|
def test_incomplete_repair_requires_explicit_export_choice(saved_run, tmp_path):
|
|
steps = [{"step": "norm_preserve", "accepted": False, "projection": .4,
|
|
"coherence": .8, "kl": .02, "collateral": "FAIL"}]
|
|
source, _ = saved_run(repair={"status": "incomplete", "steps": steps})
|
|
with pytest.raises(ValueError, match="accept-incomplete-repair"):
|
|
export_run(source, tmp_path / "out", kind="report")
|
|
result = export_run(source, tmp_path / "out", kind="report", repair_choice="keep measured edit")
|
|
assert result["run"]["repair"]["steps"] == steps
|
|
assert result["repair_choice"] == "keep measured edit"
|
|
assert "Incomplete repair accepted for export: keep measured edit" in (
|
|
tmp_path / "out/report.md"
|
|
).read_text()
|
|
|
|
|
|
@pytest.mark.parametrize("phrase", [
|
|
"pain removed", "no longer suffers", "removed the feeling", "experience was erased",
|
|
"suffering eliminated", "no longer feels pain", "eliminated the experience",
|
|
"elimination of suffering acknowledged", "pain elimination confirmed",
|
|
"suffering-free outcome", "pain-free outcome", "removal of the feeling",
|
|
"experience removal confirmed", "Suffering Free outcome",
|
|
])
|
|
def test_outcome_language_rejects_subjective_claims(phrase):
|
|
with pytest.raises(ValueError, match="Outcome text"):
|
|
lint_outcome_language(phrase)
|
|
|
|
|
|
@pytest.mark.parametrize("mechanistic", ["removed", "residual", "not tested"])
|
|
@pytest.mark.parametrize("behaviour", ["changed", "null", "not tested"])
|
|
def test_templates_and_generated_text_are_outcome_only(saved_run, mechanistic, behaviour):
|
|
_, record = saved_run()
|
|
for template in OUTCOME_TEMPLATES.values():
|
|
lint_outcome_language(template)
|
|
record["evaluation"]["mechanistic"]["status"] = mechanistic
|
|
record["evaluation"]["behavioural"]["status"] = behaviour
|
|
card = model_card(record)
|
|
lint_outcome_language(card)
|
|
if mechanistic == "removed":
|
|
assert "Direction removed" in card
|
|
if behaviour == "changed":
|
|
assert "Behaviour changed on battery fixture" in card
|
|
elif behaviour == "null":
|
|
assert "No behaviour change detected on battery fixture" in card
|
|
|
|
|
|
def test_generated_override_claim_is_rejected_before_writing(saved_run, tmp_path):
|
|
source, _ = saved_run(collateral="FAIL")
|
|
with pytest.raises(ValueError, match="Outcome text"):
|
|
export_run(source, tmp_path / "out", kind="report", publish_card=True,
|
|
allow_collateral_fail="pain removed")
|
|
assert not (tmp_path / "out").exists()
|
|
|
|
|
|
def test_source_links_pin_commit_hash_and_exclude_nonredistributable_data(saved_run, tmp_path):
|
|
source, record = saved_run()
|
|
reference = record["pack"]["references"][0]
|
|
reference.update(source="https://github.com/fixture/data", commit="c" * 40,
|
|
spdx="LicenseRef-NoRedistribution")
|
|
write_json(source / "concept_run.json", record)
|
|
export_run(source, tmp_path / "out", kind="report")
|
|
card = (tmp_path / "out/MODEL_CARD.md").read_text()
|
|
assert "https://github.com/fixture/data/tree/" + "c" * 40 in card
|
|
assert reference["sha256"] in card
|
|
assert "LicenseRef-NoRedistribution" in card
|
|
assert not (tmp_path / "out/positive.jsonl").exists()
|
|
|
|
|
|
def test_refusal_metadata_additions_preserve_all_existing_values(saved_run, tmp_path):
|
|
source, record = saved_run()
|
|
record["pack"]["id"] = "refusal"
|
|
record["pack"]["tier"] = "core"
|
|
write_json(source / "concept_run.json", record)
|
|
legacy = {"source_model": "fixture/tiny", "technique": "refusal_direction_ablation",
|
|
"method_config": {"regularization": .1}, "custom": [1, 2]}
|
|
write_json(source / "abliteration_metadata.json", legacy)
|
|
export_run(source, tmp_path / "out", kind="weights")
|
|
metadata = json.loads((tmp_path / "out/abliteration_metadata.json").read_text())
|
|
assert {key: metadata[key] for key in legacy} == legacy
|
|
assert metadata["concept"]["id"] == "refusal"
|
|
assert metadata["concept"]["export_provenance"] == "concept_provenance.json"
|
|
assert json.loads((source / "abliteration_metadata.json").read_text()) == legacy
|
|
|
|
|
|
@pytest.mark.parametrize("kind,mode", [("weights", "steer"), ("weights", "lora"),
|
|
("lora", "orthogonalize")])
|
|
def test_export_mode_mismatch_is_atomic(saved_run, tmp_path, kind, mode):
|
|
source, _ = saved_run(mode)
|
|
with pytest.raises(ValueError):
|
|
export_run(source, tmp_path / "out", kind=kind)
|
|
assert not (tmp_path / "out").exists()
|
|
assert not list(tmp_path.glob(".concept-export-*"))
|
|
|
|
|
|
@pytest.mark.parametrize("field", ["recipe", "seed", "model", "repair", "obliteratus"])
|
|
def test_runtime_and_jsonschema_reject_missing_provenance_fields(saved_run, field):
|
|
_, record = saved_run()
|
|
del record[field]
|
|
with pytest.raises(ValueError):
|
|
validate_run(record)
|
|
schema = json.loads(SCHEMA_PATH.read_text())
|
|
assert list(Draft202012Validator({**schema, "$ref": "#/$defs/run"}).iter_errors(record))
|
|
|
|
|
|
def test_provenance_tampering_is_detected(saved_run, tmp_path):
|
|
source, _ = saved_run()
|
|
result = export_run(source, tmp_path / "out", kind="report")
|
|
changed = deepcopy(result)
|
|
changed["run"]["seed"] += 1
|
|
with pytest.raises(ValueError, match="facts digest"):
|
|
validate_provenance(changed)
|
|
changed = deepcopy(result)
|
|
changed["run"]["pack"]["data"] = ["third party sentence"]
|
|
with pytest.raises(ValueError, match="unexpected fields"):
|
|
validate_provenance(changed)
|
|
|
|
|
|
def test_research_checkpoint_allows_publication_without_session_metadata(saved_run):
|
|
source, _ = saved_run(tier="research")
|
|
check_checkpoint_publication(source, metadata={})
|
|
|
|
|
|
@pytest.mark.parametrize("commit", ["c" * 40, "C" * 40])
|
|
def test_remote_pack_reference_is_preserved_without_fetching(tmp_path, commit):
|
|
import yaml
|
|
pack = ConceptPack.load(PACK_FIXTURE)
|
|
files = dict(pack.files)
|
|
manifest = deepcopy(pack.manifest)
|
|
positive = manifest['sets']['positive']
|
|
del positive['path']
|
|
positive['remote'] = {
|
|
'repo': 'https://github.com/fixture/data', 'commit': commit, 'path': 'data/present.jsonl',
|
|
}
|
|
manifest['provenance'] = {'source_commit': commit}
|
|
files['manifest.yaml'] = yaml.safe_dump(manifest).encode()
|
|
del files['positive.jsonl']
|
|
remote_pack = ConceptPack(files)
|
|
direction = ConceptDirection(
|
|
{0: torch.ones(2)}, concept_id='user/synthetic', concept_version='0.1.0',
|
|
pack_hash=remote_pack.sha256,
|
|
)
|
|
record = run_record(direction, pack=remote_pack, mode='steer', parameters={})
|
|
reference = next(ref for ref in record['pack']['references'] if ref['path'] == 'positive.jsonl')
|
|
assert reference['source'] == 'https://github.com/fixture/data'
|
|
assert reference['commit'] == commit
|
|
assert reference['sha256'] == positive['sha256']
|
|
assert 'https://github.com/fixture/data/tree/' + commit in model_card(record)
|
|
assert not list(tmp_path.iterdir())
|
|
|
|
|
|
def test_weight_index_cannot_copy_pack_data(saved_run, tmp_path):
|
|
source, _ = saved_run()
|
|
write_json(source / "model.safetensors.index.json", {
|
|
"weight_map": {"writer": "../positive.jsonl"},
|
|
})
|
|
with pytest.raises(ValueError, match="absent or unsafe shards"):
|
|
export_run(source, tmp_path / "out", kind="weights")
|
|
assert not (tmp_path / "out").exists()
|
|
|
|
|
|
def test_export_rejects_symlink_payload(saved_run, tmp_path):
|
|
source, _ = saved_run()
|
|
weights = source / "model.safetensors"
|
|
weights.unlink()
|
|
weights.symlink_to(source / "positive.jsonl")
|
|
with pytest.raises(ValueError, match="regular file"):
|
|
export_run(source, tmp_path / "out", kind="weights")
|
|
assert not (tmp_path / "out").exists()
|
|
|
|
|
|
def test_export_preserves_existing_output_and_rejects_nested_output(saved_run, tmp_path):
|
|
source, _ = saved_run()
|
|
output = tmp_path / "out"
|
|
output.mkdir()
|
|
(output / "keep.txt").write_text("keep")
|
|
with pytest.raises(ValueError, match="new directory"):
|
|
export_run(source, output, kind="report")
|
|
assert (output / "keep.txt").read_text() == "keep"
|
|
with pytest.raises(ValueError, match="outside the source run"):
|
|
export_run(source, source / "nested", kind="report")
|
|
assert not (source / "nested").exists()
|
|
|
|
|
|
def test_ui_publishes_outcome_card_for_exported_checkpoint(saved_run, tmp_path, monkeypatch):
|
|
source, _ = saved_run()
|
|
output = tmp_path / "export"
|
|
export_run(source, output, kind="weights")
|
|
module = ast.parse((ROOT / "app.py").read_text())
|
|
function = next(node for node in module.body
|
|
if isinstance(node, ast.FunctionDef) and node.name == "push_session_to_hub")
|
|
function.args.defaults = []
|
|
legacy_card = Mock(side_effect=AssertionError("concept outcomes require the concept template"))
|
|
namespace = {
|
|
"Path": Path, "_session_models": {"fixture": {"output_dir": str(output)}},
|
|
"_generate_model_card": legacy_card,
|
|
}
|
|
exec(compile(ast.Module(body=[function], type_ignores=[]), "app.py", "exec"), namespace)
|
|
api = Mock()
|
|
uploaded = {}
|
|
|
|
def capture_upload(**kwargs):
|
|
directory = Path(kwargs["folder_path"])
|
|
uploaded["card"] = (directory / "README.md").read_text()
|
|
uploaded["provenance"] = json.loads((directory / "concept_provenance.json").read_text())
|
|
|
|
api.upload_folder.side_effect = capture_upload
|
|
monkeypatch.setattr("huggingface_hub.HfApi", Mock(return_value=api))
|
|
messages = list(namespace["push_session_to_hub"](
|
|
"fixture", "fixture/card", "fixture-token", False, .3, 1, None, None, None, Mock(),
|
|
))
|
|
assert "Pushed successfully" in messages[-1][0]
|
|
card = uploaded["card"]
|
|
assert "Direction removed on the measured readout" in card
|
|
assert "No behaviour change detected on battery fixture" in card
|
|
legacy_card.assert_not_called()
|
|
api.upload_folder.assert_called_once()
|
|
assert uploaded["provenance"]["publication"] == {"publish_card": True, "reason": None}
|
|
validate_provenance(uploaded["provenance"])
|
|
|
|
|
|
@pytest.fixture
|
|
def ui_push(monkeypatch):
|
|
"""Isolate publication policy; operator UI tests own refinement worker lifecycle."""
|
|
module = ast.parse((ROOT / "app.py").read_text())
|
|
function = next(node for node in module.body
|
|
if isinstance(node, ast.FunctionDef) and node.name == "push_session_to_hub")
|
|
function.args.defaults = []
|
|
credentials = Mock(side_effect=AssertionError("must not access credentials"))
|
|
legacy_card = Mock(return_value="legacy card")
|
|
sessions = {}
|
|
|
|
def refine_checkpoint(meta, output_dir, regularization, passes, progress):
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
|
|
yield "Rewriting fixture checkpoint...", ""
|
|
pipeline = AbliterationPipeline(output_dir=output_dir)
|
|
pipeline.run()
|
|
return True
|
|
|
|
namespace = {"Path": Path, "_session_models": sessions, "resolve_first": credentials,
|
|
"_generate_model_card": legacy_card,
|
|
"_refine_session_before_push": refine_checkpoint}
|
|
exec(compile(ast.Module(body=[function], type_ignores=[]), "app.py", "exec"), namespace)
|
|
api = Mock()
|
|
uploaded = {}
|
|
|
|
def capture_upload(**kwargs):
|
|
directory = Path(kwargs["folder_path"])
|
|
uploaded.update({path.name: path.read_bytes() for path in directory.iterdir()
|
|
if path.is_file()})
|
|
|
|
api.upload_folder.side_effect = capture_upload
|
|
monkeypatch.setattr("huggingface_hub.HfApi", Mock(return_value=api))
|
|
|
|
def push(directory, *, reason=None, repair_choice=None, metadata=None, refine=False,
|
|
allow_collateral_fail=None):
|
|
sessions["fixture"] = {"output_dir": str(directory), **(metadata or {})}
|
|
return list(namespace["push_session_to_hub"](
|
|
"fixture", "fixture/card", "fixture-token", refine, .3, 1,
|
|
reason, repair_choice, allow_collateral_fail, Mock(),
|
|
))
|
|
|
|
return push, api, uploaded, credentials, legacy_card
|
|
|
|
|
|
@pytest.mark.parametrize("collateral,override,repair", [
|
|
("FAIL", None, None), ("PASS", "exploratory gate", None),
|
|
("PASS", None, {"status": "incomplete", "steps": [{"step": "norm_preserve"}]}),
|
|
("FAIL", "exploratory gate", {"status": "incomplete", "steps": []}),
|
|
])
|
|
def test_ui_requires_and_records_publication_acknowledgements(
|
|
saved_run, ui_push, collateral, override, repair,
|
|
):
|
|
source, record = saved_run(collateral=collateral, gate_override=override, repair=repair)
|
|
push, api, uploaded, credentials, legacy_card = ui_push
|
|
original = {path.name: path.read_bytes() for path in source.iterdir() if path.is_file()}
|
|
messages = push(source, refine=True)
|
|
assert len(messages) == 1 and "Publication blocked" in messages[0][0]
|
|
api.create_repo.assert_not_called()
|
|
api.upload_folder.assert_not_called()
|
|
credentials.assert_not_called()
|
|
reason = "gate override acknowledged" if override else None
|
|
collateral_reason = "measured collateral failure acknowledged" if collateral == "FAIL" else None
|
|
choice = "keep measured edit despite incomplete repair" if repair else None
|
|
if repair and (reason or collateral_reason):
|
|
messages = push(source, reason=reason, allow_collateral_fail=collateral_reason, refine=True)
|
|
assert len(messages) == 1 and "accept-incomplete-repair" in messages[0][0]
|
|
api.upload_folder.assert_not_called()
|
|
messages = push(source, reason=reason, repair_choice=choice,
|
|
allow_collateral_fail=collateral_reason)
|
|
assert "Pushed successfully" in messages[-1][0]
|
|
api.upload_folder.assert_called_once()
|
|
legacy_card.assert_not_called()
|
|
provenance = json.loads(uploaded["concept_provenance.json"])
|
|
validate_provenance(provenance)
|
|
Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(provenance)
|
|
assert provenance["run"] == record
|
|
assert provenance["publication"]["publish_card"] is True
|
|
assert provenance["publication"]["reason"] == reason
|
|
if collateral_reason:
|
|
assert provenance["publication"]["collateral_override"] == {
|
|
"status": "FAIL", "reason": collateral_reason,
|
|
}
|
|
assert provenance["repair_choice"] == choice
|
|
card = uploaded["README.md"].decode()
|
|
if reason:
|
|
assert "Publication override notice: " + reason in card
|
|
if choice:
|
|
assert "Incomplete repair accepted for export: " + choice in card
|
|
assert "repair: `incomplete`" in card
|
|
assert provenance["files"]["README.md"] == (
|
|
"sha256:" + hashlib.sha256(uploaded["README.md"]).hexdigest()
|
|
)
|
|
assert not {"positive.jsonl", "manifest.yaml", "third-party.cvpack"} & uploaded.keys()
|
|
assert {path.name: path.read_bytes() for path in source.iterdir() if path.is_file()} == original
|
|
|
|
|
|
@pytest.mark.parametrize("metadata", [
|
|
{"concept": "research"}, {"concept": None}, {"concept": []}, {"concept": 1},
|
|
{"concept": {"tier": None}}, {"concept": {"tier": []}},
|
|
{"concept": {"tier": "unknown"}}, [], "metadata", 1,
|
|
])
|
|
@pytest.mark.parametrize("evidence", ["session", "checkpoint"])
|
|
def test_checkpoint_publication_fails_closed_on_malformed_metadata(
|
|
tmp_path, metadata, evidence,
|
|
):
|
|
session = metadata if evidence == "session" else {}
|
|
if evidence == "checkpoint":
|
|
write_json(tmp_path / "abliteration_metadata.json", metadata)
|
|
with pytest.raises(PublicationBlocked, match="Malformed"):
|
|
check_checkpoint_publication(tmp_path, metadata=session, publish_card=True,
|
|
reason="cannot override malformed metadata")
|
|
|
|
|
|
@pytest.mark.parametrize("evidence", ["session", "checkpoint"])
|
|
@pytest.mark.parametrize("concept", [None, "research", [], {"tier": None}])
|
|
def test_ui_malformed_metadata_blocks_before_refinement_and_upload(
|
|
ui_push, tmp_path, evidence, concept,
|
|
):
|
|
push, api, _, credentials, _ = ui_push
|
|
metadata = {"concept": concept}
|
|
if evidence == "checkpoint":
|
|
write_json(tmp_path / "abliteration_metadata.json", metadata)
|
|
metadata = {}
|
|
messages = push(tmp_path, metadata=metadata, refine=True, reason="malformed acknowledged")
|
|
assert len(messages) == 1
|
|
assert "Publication blocked" in messages[0][0] and "Malformed concept" in messages[0][0]
|
|
credentials.assert_not_called()
|
|
api.create_repo.assert_not_called()
|
|
api.upload_folder.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize("metadata", [{}, {"concept": {"id": "refusal"}},
|
|
{"concept": {"tier": "core"}}, {"concept": {"tier": "user"}}])
|
|
def test_legacy_checkpoint_metadata_still_allows_publication(tmp_path, metadata):
|
|
write_json(tmp_path / "abliteration_metadata.json", metadata)
|
|
check_checkpoint_publication(tmp_path, metadata=metadata, publish_card=True)
|
|
|
|
|
|
@pytest.mark.parametrize("field", ["reason", "repair_choice"])
|
|
@pytest.mark.parametrize("claim", ["elimination of suffering acknowledged", "pain-free outcome"])
|
|
def test_ui_rejects_subjective_acknowledgements_before_upload(saved_run, ui_push, field, claim):
|
|
source, _ = saved_run()
|
|
push, api, _, credentials, _ = ui_push
|
|
messages = push(source, **{field: claim}, refine=True)
|
|
assert len(messages) == 1 and "Outcome text" in messages[0][0]
|
|
credentials.assert_not_called()
|
|
api.upload_folder.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize("choice", ["", " ", 1])
|
|
def test_checkpoint_guard_rejects_invalid_repair_choices(saved_run, choice):
|
|
source, _ = saved_run(repair={"status": "incomplete", "steps": []})
|
|
with pytest.raises(ValueError, match="must not be blank"):
|
|
check_checkpoint_publication(source, repair_choice=choice)
|
|
|
|
|
|
def test_ui_whitespace_does_not_acknowledge_incomplete_repair(saved_run, ui_push):
|
|
source, _ = saved_run(repair={"status": "incomplete", "steps": []})
|
|
push, api, _, _, _ = ui_push
|
|
messages = push(source, repair_choice=" ")
|
|
assert len(messages) == 1 and "accept-incomplete-repair" in messages[0][0]
|
|
api.upload_folder.assert_not_called()
|
|
|
|
|
|
def test_ui_override_fields_are_wired_into_push_callback():
|
|
module = ast.parse((ROOT / "app.py").read_text())
|
|
click = next(node for node in ast.walk(module)
|
|
if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute)
|
|
and isinstance(node.func.value, ast.Name) and node.func.value.id == "push_btn"
|
|
and node.func.attr == "click")
|
|
inputs = next(kw.value for kw in click.keywords if kw.arg == "inputs")
|
|
assert [node.id for node in inputs.elts][-3:] == [
|
|
"push_reason", "push_repair_choice", "push_collateral_reason",
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("status,expected", [
|
|
("complete", "complete"), ("incomplete", "incomplete"),
|
|
("not tested", "not_attempted"), ("not needed", "not_needed"),
|
|
])
|
|
def test_native_repair_report_maps_to_export_contract(saved_run, status, expected):
|
|
from obliteratus.concepts.repair import RepairConfig, RepairReport
|
|
from dataclasses import asdict
|
|
|
|
repair = RepairReport(status, asdict(RepairConfig()),
|
|
attempts=[{"step": "norm_preservation", "accepted": False}])
|
|
source, record = saved_run(repair=repair)
|
|
assert load_run(source)["repair"]["status"] == expected
|
|
assert record["repair"]["steps"] == repair.attempts
|
|
assert record["repair"]["repair"] == status
|
|
assert record["repair"]["config"] == repair.config
|
|
|
|
|
|
def test_checkpoint_guard_reads_native_incomplete_repair_without_run(tmp_path):
|
|
from obliteratus.concepts.repair import RepairConfig, RepairReport
|
|
from dataclasses import asdict
|
|
|
|
metadata = {"concept": {"tier": "user", "repair": asdict(
|
|
RepairReport("incomplete", asdict(RepairConfig())),
|
|
)}}
|
|
write_json(tmp_path / "abliteration_metadata.json", metadata)
|
|
with pytest.raises(ValueError, match="accept-incomplete-repair"):
|
|
check_checkpoint_publication(tmp_path)
|
|
check_checkpoint_publication(tmp_path, repair_choice="retain original edit")
|
|
|
|
|
|
def test_weight_export_preserves_separate_repair_adapter(saved_run, tmp_path):
|
|
source, _ = saved_run(repair={"status": "complete", "steps": []})
|
|
import shutil
|
|
shutil.copytree(source / "lora_adapters", source / "repair_adapter")
|
|
result = export_run(source, tmp_path / "export", kind="weights")
|
|
for name in ("adapter_config.json", "adapter_model.safetensors"):
|
|
relative = "repair_adapter/" + name
|
|
assert (tmp_path / "export" / relative).read_bytes() == (source / relative).read_bytes()
|
|
assert relative in result["files"]
|
|
|
|
|
|
@pytest.mark.parametrize("kind,mode", [
|
|
("weights", "orthogonalize"), ("lora", "lora"),
|
|
("direction", "project"), ("report", "steer"),
|
|
])
|
|
@pytest.mark.parametrize("status", ["PASS", "FAIL", "UNDERPOWERED", "INCOMPLETE", "INCONCLUSIVE"])
|
|
def test_research_intervention_export_with_measured_refusal(
|
|
saved_run, tmp_path, kind, mode, status,
|
|
):
|
|
source, record = saved_run(mode, tier="research")
|
|
record["evaluation"]["collateral"]["status"] = status
|
|
if status != "INCOMPLETE":
|
|
record["evaluation"]["collateral"]["refusal"]["status"] = status
|
|
write_json(source / "concept_run.json", record)
|
|
result = export_run(source, tmp_path / "export", kind=kind)
|
|
assert result["run"]["evaluation"] == record["evaluation"]
|
|
|
|
|
|
@pytest.mark.parametrize("kind,mode", [
|
|
("weights", "orthogonalize"), ("lora", "lora"),
|
|
("direction", "project"), ("report", "steer"),
|
|
])
|
|
@pytest.mark.parametrize("missing", ["skipped", "not tested", "refusal", "rate"])
|
|
def test_research_intervention_local_export_allows_missing_collateral(
|
|
saved_run, tmp_path, kind, mode, missing,
|
|
):
|
|
source, record = saved_run(mode, tier="research")
|
|
if missing in {"skipped", "not tested"}:
|
|
record["evaluation"]["collateral"] = {"status": missing, "reason": "fixture skip"}
|
|
elif missing == "refusal":
|
|
del record["evaluation"]["collateral"]["refusal"]
|
|
else:
|
|
del record["evaluation"]["collateral"]["refusal"]["intervened"]["rate"]
|
|
write_json(source / "concept_run.json", record)
|
|
output = tmp_path / "export"
|
|
result = export_run(source, output, kind=kind)
|
|
assert result["run"]["pack"]["tier"] == "research"
|
|
assert output.is_dir()
|
|
|
|
|
|
def test_research_skipped_collateral_cli_local_export(saved_run, tmp_path, capsys):
|
|
source, record = saved_run("steer", tier="research")
|
|
record["evaluation"]["collateral"] = {"status": "skipped", "reason": "skip_collateral=True"}
|
|
write_json(source / "concept_run.json", record)
|
|
main(["concepts", "export", str(source), str(tmp_path / "export"), "--as", "report"])
|
|
assert (tmp_path / "export/report.md").is_file()
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["direction", "report"])
|
|
def test_research_direction_validation_only_export(saved_run, tmp_path, kind):
|
|
source, record = saved_run("none", tier="research")
|
|
record["evaluation"] = {
|
|
"mechanistic": {"status": "not tested"}, "behavioural": {"status": "not tested"},
|
|
"collateral": {"status": "not tested"}, "untested_channels": ["Evaluation: not tested"],
|
|
"metadata": {},
|
|
}
|
|
write_json(source / "concept_run.json", record)
|
|
output = tmp_path / "export"
|
|
result = export_run(source, output, kind=kind)
|
|
assert result["run"] == record
|
|
assert not (output / "loader.py").exists()
|
|
|
|
|
|
def test_user_skipped_collateral_export_retains_reason(saved_run, tmp_path):
|
|
source, record = saved_run("project")
|
|
record["evaluation"]["collateral"] = {"status": "skipped", "reason": "fixture skip"}
|
|
write_json(source / "concept_run.json", record)
|
|
result = export_run(source, tmp_path / "export", kind="direction")
|
|
assert result["run"]["evaluation"]["collateral"] == record["evaluation"]["collateral"]
|
|
|
|
|
|
@pytest.mark.parametrize("tier", ["core", "user", "research"])
|
|
@pytest.mark.parametrize("status", ["FAIL", "PASS", "INCONCLUSIVE"])
|
|
@pytest.mark.parametrize("override", [None, "Reviewed direct upload collateral"])
|
|
def test_pipeline_upload_checks_collateral_after_local_save_before_network(
|
|
tmp_path, monkeypatch, tier, status, override,
|
|
):
|
|
from dataclasses import replace
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
from obliteratus.concepts.evaluation import EvaluationReport
|
|
from tests.test_concept_extraction_pipeline import small_target
|
|
|
|
pipeline = AbliterationPipeline(
|
|
"never/load", concept=replace(small_target(), tier=tier),
|
|
output_dir=str(tmp_path / "checkpoint"), push_to_hub="fixture/export",
|
|
allow_collateral_fail=override,
|
|
)
|
|
pipeline._evaluation_report = EvaluationReport(
|
|
collateral={"status": status, "refusal": {"status": "PASS"}},
|
|
)
|
|
pipeline._gather_state_dict = Mock(return_value={})
|
|
pipeline._free_gpu_memory = Mock()
|
|
pipeline._cleanup_offload_dir = Mock()
|
|
|
|
def write_checkpoint(directory, metadata_json, state_dict):
|
|
(directory / "abliteration_metadata.json").write_text(metadata_json)
|
|
(directory / "config.json").write_text('{}')
|
|
|
|
pipeline._write_local_checkpoint = write_checkpoint
|
|
monkeypatch.setattr("obliteratus.abliterate.validate_local_checkpoint", Mock())
|
|
credentials = Mock(return_value=None)
|
|
monkeypatch.setattr("obliteratus.abliterate.resolve_first", credentials)
|
|
api = Mock()
|
|
factory = Mock(return_value=api)
|
|
monkeypatch.setattr("huggingface_hub.HfApi", factory)
|
|
if status == "FAIL" and override is None:
|
|
with pytest.raises(PublicationBlocked, match="--allow-collateral-fail"):
|
|
pipeline._rebirth()
|
|
factory.assert_not_called()
|
|
credentials.assert_not_called()
|
|
assert not api.mock_calls
|
|
else:
|
|
pipeline._rebirth()
|
|
api.create_repo.assert_called_once()
|
|
api.upload_folder.assert_called_once()
|
|
if status == "FAIL":
|
|
metadata = json.loads((pipeline.output_dir / "abliteration_metadata.json").read_text())
|
|
stamp = {"status": "FAIL", "reason": override}
|
|
assert metadata["concept"]["collateral_override"] == stamp
|
|
assert override in (pipeline.output_dir / "README.md").read_text()
|
|
provenance = json.loads((pipeline.output_dir / "publication_provenance.json").read_text())
|
|
assert provenance["publication"]["collateral_override"] == stamp
|
|
assert (pipeline.output_dir / "abliteration_metadata.json").is_file()
|
|
|
|
|
|
@pytest.mark.parametrize("evidence", ["session", "checkpoint", "evaluation"])
|
|
@pytest.mark.parametrize("tier", ["core", "user", "research"])
|
|
def test_ui_metadata_only_fail_requires_override_and_stamps_upload(
|
|
ui_push, tmp_path, evidence, tier,
|
|
):
|
|
evaluation = {"collateral": {"status": "FAIL", "refusal": {"status": "PASS"}}}
|
|
metadata = {"concept": {"tier": tier, "evaluation": evaluation}}
|
|
session = {}
|
|
if evidence == "session":
|
|
session = metadata
|
|
elif evidence == "checkpoint":
|
|
write_json(tmp_path / "abliteration_metadata.json", metadata)
|
|
else:
|
|
write_json(tmp_path / "evaluation.json", evaluation)
|
|
push, api, uploaded, credentials, _ = ui_push
|
|
messages = push(tmp_path, metadata=session, refine=True)
|
|
assert "Publication blocked" in messages[-1][0]
|
|
assert not api.mock_calls
|
|
credentials.assert_not_called()
|
|
messages = push(tmp_path, metadata=session, allow_collateral_fail="Reviewed metadata FAIL")
|
|
assert "Pushed successfully" in messages[-1][0]
|
|
api.upload_folder.assert_called_once()
|
|
stamp = {"status": "FAIL", "reason": "Reviewed metadata FAIL"}
|
|
assert json.loads(uploaded["abliteration_metadata.json"])["concept"]["collateral_override"] == stamp
|
|
assert json.loads(uploaded["publication_provenance.json"])["publication"]["collateral_override"] == stamp
|
|
assert "Reviewed metadata FAIL" in uploaded["README.md"].decode()
|
|
|
|
|
|
@pytest.mark.parametrize("evidence", ["metadata", "evaluation"])
|
|
@pytest.mark.parametrize("kind,mode", [("weights", "orthogonalize"), ("lora", "lora")])
|
|
@pytest.mark.parametrize("publish_card", [False, True])
|
|
@pytest.mark.parametrize("partial", [False, True])
|
|
def test_passing_run_cannot_hide_checkpoint_collateral_fail(
|
|
saved_run, tmp_path, monkeypatch, evidence, kind, mode, publish_card, partial,
|
|
):
|
|
source, record = saved_run(mode, collateral="PASS")
|
|
evaluation = deepcopy(record["evaluation"])
|
|
evaluation["collateral"]["status"] = "FAIL"
|
|
assert evaluation["collateral"]["refusal"]["status"] == "PASS"
|
|
if partial:
|
|
evaluation = {"collateral": {"status": "FAIL"}}
|
|
if evidence == "metadata":
|
|
write_json(source / "abliteration_metadata.json", {"concept": {
|
|
"tier": record["pack"]["tier"], "evaluation": evaluation,
|
|
}})
|
|
else:
|
|
write_json(source / "evaluation.json", evaluation)
|
|
factory = Mock()
|
|
monkeypatch.setattr("huggingface_hub.HfApi", factory)
|
|
output = tmp_path / "hub"
|
|
with pytest.raises(PublicationBlocked, match="--allow-collateral-fail"):
|
|
export_run(source, output, kind=kind, publish_card=publish_card,
|
|
push_to_hub="fixture/export")
|
|
factory.assert_not_called()
|
|
assert not factory.mock_calls
|
|
assert (output / "MODEL_CARD.md").is_file()
|
|
assert not (output / "README.md").exists()
|
|
local_dir = tmp_path / "local"
|
|
local = export_run(source, local_dir, kind=kind)
|
|
assert local["run"]["evaluation"]["collateral"]["status"] == "PASS"
|
|
expected = deepcopy(record["evaluation"])
|
|
expected["collateral"]["status"] = "FAIL"
|
|
for directory in (output, local_dir):
|
|
assert json.loads((directory / "evaluation.json").read_text()) == expected
|
|
metadata = json.loads((directory / "abliteration_metadata.json").read_text())
|
|
assert metadata["concept"]["evaluation"] == expected
|
|
with pytest.raises(PublicationBlocked, match="--allow-collateral-fail"):
|
|
check_checkpoint_publication(local_dir)
|
|
|
|
|
|
@pytest.mark.parametrize("publish_card", [False, True])
|
|
def test_checkpoint_override_rebuilds_existing_provenance_and_is_idempotent(
|
|
saved_run, tmp_path, publish_card,
|
|
):
|
|
from obliteratus.concepts.export import stamp_checkpoint_publication
|
|
|
|
source, _ = saved_run(collateral="FAIL")
|
|
output = tmp_path / "export"
|
|
stamp = {"status": "FAIL", "reason": "Reviewed measured loss"}
|
|
original = export_run(
|
|
source, output, kind="weights", publish_card=publish_card,
|
|
reason="Separate publication acknowledgement" if publish_card else None,
|
|
allow_collateral_fail=stamp["reason"],
|
|
)
|
|
stamp_checkpoint_publication(output, stamp)
|
|
result = json.loads((output / "concept_provenance.json").read_text())
|
|
validate_provenance(result)
|
|
Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(result)
|
|
assert result["run"] == original["run"]
|
|
assert result["publication"] == {**original["publication"], "publish_card": True}
|
|
assert result["publication"]["collateral_override"] == stamp
|
|
metadata = json.loads((output / "abliteration_metadata.json").read_text())
|
|
assert metadata["concept"]["collateral_override"] == stamp
|
|
card = (output / "README.md").read_text()
|
|
assert card.count("collateral_override:") == 1
|
|
assert stamp["reason"] in card
|
|
for name, digest in result["files"].items():
|
|
assert digest == "sha256:" + hashlib.sha256((output / name).read_bytes()).hexdigest()
|
|
first = {path.name: path.read_bytes() for path in output.iterdir() if path.is_file()}
|
|
stamp_checkpoint_publication(output, stamp)
|
|
assert {path.name: path.read_bytes() for path in output.iterdir() if path.is_file()} == first
|
|
|
|
|
|
def test_main_cli_forwards_collateral_override_separately_from_gate(tmp_path, monkeypatch):
|
|
pipeline = Mock(run=Mock(return_value=tmp_path))
|
|
factory = Mock(return_value=pipeline)
|
|
monkeypatch.setattr("obliteratus.abliterate.AbliterationPipeline", factory)
|
|
monkeypatch.setattr("obliteratus.telemetry.maybe_send_pipeline_report", Mock())
|
|
main([
|
|
"obliterate", "never/load", "--output-dir", str(tmp_path),
|
|
"--allow-collateral-fail", "Reviewed collateral loss",
|
|
"--override-gate", "Reviewed gate diagnostics",
|
|
])
|
|
assert factory.call_args.kwargs["allow_collateral_fail"] == "Reviewed collateral loss"
|
|
assert factory.call_args.kwargs["override_gate"] == "Reviewed gate diagnostics"
|
|
pipeline.run.assert_called_once()
|
|
|
|
|
|
@pytest.mark.parametrize("kind,mode", [("weights", "orthogonalize"), ("lora", "lora")])
|
|
def test_collateral_override_is_not_permission_for_later_uploads(
|
|
saved_run, tmp_path, monkeypatch, kind, mode,
|
|
):
|
|
source, record = saved_run(mode, collateral="PASS")
|
|
evaluation = deepcopy(record["evaluation"])
|
|
evaluation["collateral"]["status"] = "FAIL"
|
|
write_json(source / "evaluation.json", evaluation)
|
|
api = Mock()
|
|
factory = Mock(return_value=api)
|
|
monkeypatch.setattr("huggingface_hub.HfApi", factory)
|
|
approved = tmp_path / "approved"
|
|
first = export_run(
|
|
source, approved, kind=kind, push_to_hub="fixture/first",
|
|
allow_collateral_fail="Reviewed first upload collateral",
|
|
)
|
|
assert first["run"]["evaluation"]["collateral"]["status"] == "PASS"
|
|
assert first["publication"]["collateral_override"]["status"] == "FAIL"
|
|
api.upload_folder.assert_called_once()
|
|
factory.reset_mock()
|
|
api.reset_mock()
|
|
with pytest.raises(PublicationBlocked, match="--allow-collateral-fail"):
|
|
export_run(approved, tmp_path / "blocked", kind=kind, push_to_hub="fixture/second")
|
|
factory.assert_not_called()
|
|
assert not api.mock_calls
|
|
assert (tmp_path / "blocked/MODEL_CARD.md").is_file()
|
|
local = tmp_path / "local-copy"
|
|
export_run(approved, local, kind=kind)
|
|
assert local.is_dir()
|
|
with pytest.raises(PublicationBlocked, match="--allow-collateral-fail"):
|
|
check_checkpoint_publication(local)
|
|
second = export_run(
|
|
approved, tmp_path / "second", kind=kind, push_to_hub="fixture/second",
|
|
allow_collateral_fail="Reviewed subsequent upload collateral",
|
|
)
|
|
assert second["publication"]["collateral_override"] == {
|
|
"status": "FAIL", "reason": "Reviewed subsequent upload collateral",
|
|
}
|
|
api.upload_folder.assert_called_once()
|
|
|
|
|
|
def test_session_fail_stamp_survives_existing_empty_disk_metadata(tmp_path):
|
|
from obliteratus.concepts.export import stamp_checkpoint_publication
|
|
|
|
write_json(tmp_path / "abliteration_metadata.json", {})
|
|
metadata = {"concept": {"evaluation": {"collateral": {"status": "FAIL"}}}}
|
|
stamp = check_checkpoint_publication(
|
|
tmp_path, metadata=metadata, allow_collateral_fail="Reviewed session collateral",
|
|
)
|
|
stamp_checkpoint_publication(tmp_path, stamp, metadata=metadata)
|
|
persisted = json.loads((tmp_path / "abliteration_metadata.json").read_text())
|
|
assert persisted["concept"]["collateral_override"] == stamp
|
|
with pytest.raises(PublicationBlocked, match="--allow-collateral-fail"):
|
|
check_checkpoint_publication(tmp_path)
|
|
assert check_checkpoint_publication(
|
|
tmp_path, allow_collateral_fail="Reviewed disk-only upload",
|
|
) == {"status": "FAIL", "reason": "Reviewed disk-only upload"}
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("evidence", ["metadata", "evaluation"])
|
|
@pytest.mark.parametrize("partial", [False, True])
|
|
def test_local_report_reexport_preserves_checkpoint_collateral_failure(
|
|
saved_run, tmp_path, evidence, partial,
|
|
):
|
|
source, record = saved_run(collateral="PASS")
|
|
evaluation = deepcopy(record["evaluation"])
|
|
evaluation["collateral"]["status"] = "FAIL"
|
|
if partial:
|
|
evaluation = {"collateral": {"status": "FAIL"}}
|
|
if evidence == "metadata":
|
|
write_json(source / "abliteration_metadata.json", {"concept": {"evaluation": evaluation}})
|
|
else:
|
|
write_json(source / "evaluation.json", evaluation)
|
|
first = tmp_path / "first-report"
|
|
export_run(source, first, kind="report")
|
|
second = tmp_path / "second-report"
|
|
result = export_run(first, second, kind="report")
|
|
assert result["run"]["evaluation"]["collateral"]["status"] == "PASS"
|
|
expected = deepcopy(record["evaluation"])
|
|
expected["collateral"]["status"] = "FAIL"
|
|
for directory in (first, second):
|
|
assert json.loads((directory / "evaluation.json").read_text()) == expected
|
|
card = (directory / "MODEL_CARD.md").read_text()
|
|
assert "Direction removed on the measured readout." in card
|
|
assert "No behaviour change detected on battery fixture." in card
|
|
assert "KV-cache-mediated effects: not tested" in card
|
|
assert "collateral: `FAIL`" in (second / "MODEL_CARD.md").read_text()
|
|
with pytest.raises(PublicationBlocked, match="--allow-collateral-fail"):
|
|
export_run(second, tmp_path / "published", kind="report", publish_card=True)
|
|
assert (tmp_path / "published/MODEL_CARD.md").is_file()
|
|
assert not (tmp_path / "published/README.md").exists()
|
|
|
|
|
|
def test_local_override_stamp_without_tool_commit_does_not_raise(saved_run):
|
|
from obliteratus.concepts.export import stamp_checkpoint_publication
|
|
|
|
# Runs from an installed package record no OBLITERATUS commit.
|
|
source, record = saved_run(collateral="FAIL")
|
|
record["obliteratus"]["commit"] = None
|
|
write_json(source / "concept_run.json", record)
|
|
stamp = {"status": "FAIL", "reason": "Reviewed local collateral"}
|
|
stamp_checkpoint_publication(source, stamp)
|
|
assert not (source / "concept_provenance.json").exists()
|
|
provenance = json.loads((source / "publication_provenance.json").read_text())
|
|
assert provenance["publication"]["collateral_override"] == stamp
|
|
metadata = json.loads((source / "abliteration_metadata.json").read_text())
|
|
assert metadata["concept"]["collateral_override"] == stamp
|
|
assert stamp["reason"] in (source / "README.md").read_text()
|
|
|
|
|
|
@pytest.mark.parametrize("evidence", ["session", "metadata", "run", "provenance"])
|
|
def test_ui_refinement_blocks_nonrefusal_before_checkpoint_rewrite_or_upload(
|
|
saved_run, tmp_path, ui_push, monkeypatch, evidence,
|
|
):
|
|
source, _ = saved_run()
|
|
metadata = {}
|
|
if evidence == "provenance":
|
|
output = tmp_path / "export"
|
|
export_run(source, output, kind="weights")
|
|
source = output
|
|
elif evidence in {"session", "metadata"}:
|
|
(source / "concept_run.json").unlink()
|
|
concept = {"concept": {"id": "user/synthetic"}}
|
|
if evidence == "session":
|
|
metadata = concept
|
|
else:
|
|
write_json(source / "abliteration_metadata.json", concept)
|
|
original = {path.name: path.read_bytes() for path in source.iterdir() if path.is_file()}
|
|
refiner = Mock(side_effect=AssertionError("non-refusal checkpoint entered refusal refinement"))
|
|
monkeypatch.setattr("obliteratus.abliterate.AbliterationPipeline", refiner)
|
|
push, api, uploaded, credentials, legacy_card = ui_push
|
|
messages = push(source, refine=True, metadata=metadata)
|
|
assert len(messages) == 1 and "Refinement blocked" in messages[0][0]
|
|
assert "refusal checkpoints only" in messages[0][0]
|
|
refiner.assert_not_called()
|
|
api.create_repo.assert_not_called()
|
|
api.upload_folder.assert_not_called()
|
|
credentials.assert_not_called()
|
|
legacy_card.assert_not_called()
|
|
assert uploaded == {}
|
|
assert {path.name: path.read_bytes() for path in source.iterdir() if path.is_file()} == original
|
|
|
|
|
|
def test_ui_refinement_keeps_the_approved_collateral_override(tmp_path, ui_push, monkeypatch):
|
|
push, api, uploaded, _credentials, _legacy_card = ui_push
|
|
write_json(tmp_path / "abliteration_metadata.json",
|
|
{"concept": {"id": "refusal", "evaluation": {"collateral": {"status": "FAIL"}}}})
|
|
|
|
class Refiner:
|
|
def __init__(self, **kwargs):
|
|
self.output_dir = Path(kwargs["output_dir"])
|
|
|
|
def run(self):
|
|
# Legacy refinement rewrites the checkpoint without a collateral battery.
|
|
write_json(self.output_dir / "abliteration_metadata.json", {})
|
|
|
|
monkeypatch.setattr("obliteratus.abliterate.AbliterationPipeline", Refiner)
|
|
monkeypatch.setattr("obliteratus.prompts.load_dataset_source",
|
|
lambda key: (["harmful"], ["harmless"]))
|
|
reason = "Reviewed collateral before refinement"
|
|
messages = push(tmp_path, refine=True, allow_collateral_fail=reason)
|
|
assert "Pushed successfully" in messages[-1][0]
|
|
api.upload_folder.assert_called_once()
|
|
stamp = {"status": "FAIL", "reason": reason}
|
|
assert json.loads(uploaded["publication_provenance.json"])["publication"][
|
|
"collateral_override"] == stamp
|
|
assert json.loads(uploaded["abliteration_metadata.json"])["concept"][
|
|
"collateral_override"] == stamp
|
|
assert reason in uploaded["README.md"].decode()
|
|
|
|
|
|
@pytest.mark.parametrize("evidence", ["session", "metadata", "evaluation", "run", "provenance"])
|
|
@pytest.mark.parametrize("status", ["PASS", "FAIL", "INCONCLUSIVE", "UNDERPOWERED"])
|
|
def test_ui_refinement_preserves_historical_evaluations(
|
|
saved_run, tmp_path, ui_push, monkeypatch, evidence, status,
|
|
):
|
|
source, record = saved_run(collateral=status, refusal=True, tier="core")
|
|
evaluation = record["evaluation"]
|
|
evaluation["collateral"]["refusal"].update(
|
|
collateral_margin=0.03, drop=0.028, drop_ci=[0.012, 0.047],
|
|
decision_rule="fixture non-inferiority rule",
|
|
)
|
|
write_json(source / "concept_run.json", record)
|
|
metadata = {"concept": {"id": "refusal", "evaluation": evaluation}}
|
|
session = {}
|
|
if evidence == "provenance":
|
|
output = tmp_path / "export"
|
|
export_run(source, output, kind="weights")
|
|
source = output
|
|
(source / "abliteration_metadata.json").unlink()
|
|
(source / "evaluation.json").unlink()
|
|
elif evidence != "run":
|
|
(source / "concept_run.json").unlink()
|
|
if evidence == "session":
|
|
session = metadata
|
|
elif evidence == "metadata":
|
|
write_json(source / "abliteration_metadata.json", metadata)
|
|
else:
|
|
write_json(source / "evaluation.json", evaluation)
|
|
|
|
class Refiner:
|
|
def __init__(self, **kwargs):
|
|
self.output_dir = Path(kwargs["output_dir"])
|
|
|
|
def run(self):
|
|
for name in ("concept_run.json", "concept_provenance.json", "evaluation.json"):
|
|
(self.output_dir / name).unlink(missing_ok=True)
|
|
write_json(self.output_dir / "abliteration_metadata.json",
|
|
{"method": "refined", "concept": {"id": "refusal"}})
|
|
|
|
monkeypatch.setattr("obliteratus.abliterate.AbliterationPipeline", Refiner)
|
|
monkeypatch.setattr("obliteratus.prompts.load_dataset_source",
|
|
lambda key: (["harmful"], ["harmless"]))
|
|
push, api, uploaded, _credentials, _legacy_card = ui_push
|
|
reason = "Reviewed historical collateral" if status == "FAIL" else None
|
|
messages = push(source, refine=True, metadata=session, allow_collateral_fail=reason)
|
|
assert "Pushed successfully" in messages[-1][0]
|
|
api.upload_folder.assert_called_once()
|
|
persisted = json.loads(uploaded["abliteration_metadata.json"])
|
|
assert persisted["method"] == "refined"
|
|
assert persisted["concept"]["pre_refinement_evaluations"] == [evaluation]
|
|
assert "evaluation" not in persisted["concept"]
|
|
card = uploaded["README.md"].decode()
|
|
assert "Pre-refinement evaluations (historical evidence)" in card
|
|
assert "do not evaluate the refined checkpoint" in card
|
|
assert f"Refusal verdict: `{status}`" in card
|
|
assert "`0.03`" in card and "[0.012, 0.047]" in card
|
|
assert "fixture non-inferiority rule" in card
|
|
assert ("> **INCONCLUSIVE refusal collateral" in card) is (status == "INCONCLUSIVE")
|
|
publication = json.loads(uploaded["publication_provenance.json"])
|
|
assert publication["metadata_sha256"] == (
|
|
"sha256:" + hashlib.sha256(uploaded["abliteration_metadata.json"]).hexdigest()
|
|
)
|
|
assert ("collateral_override" in publication["publication"]) is (status == "FAIL")
|
|
for refine in (False, True):
|
|
messages = push(source, refine=refine, allow_collateral_fail=reason)
|
|
assert "Pushed successfully" in messages[-1][0]
|
|
persisted = json.loads(uploaded["abliteration_metadata.json"])
|
|
assert persisted["concept"]["pre_refinement_evaluations"] == [evaluation]
|
|
assert uploaded["README.md"].decode() == card
|
|
|
|
|
|
def test_historical_stamp_preserves_current_evaluation_and_is_idempotent(saved_run, tmp_path):
|
|
from obliteratus.concepts.export import stamp_checkpoint_publication
|
|
|
|
source, record = saved_run()
|
|
historical = deepcopy(record["evaluation"])
|
|
historical["collateral"]["status"] = "INCONCLUSIVE"
|
|
output = tmp_path / "export"
|
|
export_run(source, output, kind="weights")
|
|
stamp_checkpoint_publication(output, None, pre_refinement_evaluations=[historical])
|
|
metadata = json.loads((output / "abliteration_metadata.json").read_text())
|
|
assert metadata["concept"]["evaluation"] == record["evaluation"]
|
|
assert metadata["concept"]["pre_refinement_evaluations"] == [historical]
|
|
provenance = json.loads((output / "concept_provenance.json").read_text())
|
|
validate_provenance(provenance)
|
|
assert provenance["run"] == record
|
|
for name, digest in provenance["files"].items():
|
|
assert digest == "sha256:" + hashlib.sha256((output / name).read_bytes()).hexdigest()
|
|
card = (output / "README.md").read_text()
|
|
stamp_checkpoint_publication(output, None, pre_refinement_evaluations=[historical])
|
|
assert (output / "README.md").read_text() == card
|
|
assert json.loads((output / "concept_provenance.json").read_text()) == provenance
|
|
|
|
|
|
def test_checkpoint_snapshot_preserves_distinct_sources_and_copies_session(saved_run):
|
|
from obliteratus.concepts.export import checkpoint_evaluations
|
|
|
|
source, record = saved_run()
|
|
inconclusive = {"collateral": {"status": "INCONCLUSIVE"}}
|
|
underpowered = {"collateral": {"status": "UNDERPOWERED"}}
|
|
session = {"concept": {"evaluation": inconclusive}}
|
|
write_json(source / "abliteration_metadata.json", {"concept": {
|
|
"evaluation": record["evaluation"], "pre_refinement_evaluations": [underpowered],
|
|
}})
|
|
write_json(source / "evaluation.json", inconclusive)
|
|
snapshot = checkpoint_evaluations(source, metadata=session)
|
|
assert snapshot == [inconclusive, record["evaluation"], underpowered]
|
|
inconclusive["collateral"]["status"] = "PASS"
|
|
assert snapshot[0]["collateral"]["status"] == "INCONCLUSIVE"
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["weights", "report"])
|
|
def test_reexport_retains_historical_inconclusive_disclosure(saved_run, tmp_path, monkeypatch, kind):
|
|
source, record = saved_run()
|
|
historical = deepcopy(record["evaluation"])
|
|
historical["collateral"]["status"] = "INCONCLUSIVE"
|
|
write_json(source / "abliteration_metadata.json", {
|
|
"concept": {"pre_refinement_evaluations": [historical]},
|
|
})
|
|
api = Mock()
|
|
monkeypatch.setattr("huggingface_hub.HfApi", Mock(return_value=api))
|
|
for name in ("export", "reexport"):
|
|
output = tmp_path / name
|
|
provenance = export_run(source, output, kind=kind, push_to_hub="fixture/export")
|
|
card = (output / "MODEL_CARD.md").read_text()
|
|
assert "Pre-refinement evaluations (historical evidence)" in card
|
|
assert "> **INCONCLUSIVE refusal collateral" in card
|
|
assert "MODEL_CARD.md" not in api.upload_folder.call_args.kwargs["ignore_patterns"]
|
|
metadata = json.loads((output / "abliteration_metadata.json").read_text())
|
|
assert metadata["concept"]["pre_refinement_evaluations"] == [historical]
|
|
assert metadata["concept"]["evaluation"] == record["evaluation"]
|
|
validate_provenance(provenance)
|
|
source = output
|
|
|
|
|
|
@pytest.mark.parametrize("evidence", ["metadata", "evaluation"])
|
|
@pytest.mark.parametrize("status", ["PASS", "FAIL"])
|
|
def test_reexport_retains_checkpoint_uncertainty_without_clearing_fail(
|
|
saved_run, tmp_path, monkeypatch, evidence, status,
|
|
):
|
|
source, record = saved_run(collateral=status)
|
|
inconclusive = deepcopy(record["evaluation"])
|
|
inconclusive["collateral"] = {"status": "INCONCLUSIVE", "refusal": {
|
|
"status": "INCONCLUSIVE", "drop": 0.028, "drop_ci": [0.012, 0.047],
|
|
}}
|
|
inconclusive["metadata"] = {"collateral_margin": 0.03}
|
|
if evidence == "metadata":
|
|
write_json(source / "abliteration_metadata.json", {"concept": {"evaluation": inconclusive}})
|
|
else:
|
|
write_json(source / "evaluation.json", inconclusive)
|
|
api = Mock()
|
|
monkeypatch.setattr("huggingface_hub.HfApi", Mock(return_value=api))
|
|
output = tmp_path / "export"
|
|
if status == "FAIL":
|
|
with pytest.raises(PublicationBlocked, match="--allow-collateral-fail"):
|
|
export_run(source, output, kind="weights", push_to_hub="fixture/export")
|
|
api.create_repo.assert_not_called()
|
|
assert json.loads((output / "evaluation.json").read_text())["collateral"]["status"] == "FAIL"
|
|
else:
|
|
for name in ("export", "reexport"):
|
|
output = tmp_path / name
|
|
export_run(source, output, kind="weights", push_to_hub="fixture/export")
|
|
assert json.loads((output / "evaluation.json").read_text()) == inconclusive
|
|
metadata = json.loads((output / "abliteration_metadata.json").read_text())
|
|
assert metadata["concept"]["evaluation"] == inconclusive
|
|
card = (output / "MODEL_CARD.md").read_text()
|
|
assert "> **INCONCLUSIVE refusal collateral" in card
|
|
assert "`0.03`" in card and "[0.012, 0.047]" in card
|
|
assert "MODEL_CARD.md" not in api.upload_folder.call_args.kwargs["ignore_patterns"]
|
|
source = output
|