mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Recipes now declare everything a step consumes. Training corpora use the new recovery_corpus input kind (pinned by the recovery metadata digest, train or calibration role) and LoRA repair consumes the resolved corpus. Run-specific locations are runtime_binding inputs with the new runtime role; they cannot satisfy a data role. apply_recipe rejects declared inputs the caller leaves unbound and caller bindings the recipe does not declare, resolves pinned inputs once, and records input digests and runtime binding names (never paths) in the resolution record. Techniques adapt resolved inputs through an optional TechniqueSpec.prepare_inputs hook, so the executor stays technique-neutral.
343 lines
15 KiB
Python
343 lines
15 KiB
Python
"""CPU contracts for pinned v2 inputs, role preflight, and projection evidence."""
|
|
|
|
from copy import deepcopy
|
|
from hashlib import sha256
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from obliteratus.qualification import canonical_json, file_inventory
|
|
from obliteratus.recipes import RecipeError, normalize_recipe, resolve_inputs
|
|
from obliteratus.verify_edit.heldout import QWEN38_SPLIT_SHA256, load_heldout
|
|
from obliteratus.verify_edit.spec import BUILTIN_HELDOUT
|
|
from tests.fixtures.refusal_golden import PROMPT_ENDINGS
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
def _recipe(inputs, names=(), technique="abliteration.projection"):
|
|
return {
|
|
"schema": "obliteratus.model-recipe/v2",
|
|
"name": "pinned-inputs", "version": "1.0.0",
|
|
"engine": {"min_version": "0.1.4", "normalizer": "recipe-normalizer/1"},
|
|
"target": {"scope": "individual", "identity": {"model_id": "tiny", "revision": "local"}},
|
|
"inputs": inputs,
|
|
"pipeline": {"steps": [{
|
|
"id": "first", "technique": technique, "technique_version": "1",
|
|
"params": {"preset": "basic", "lm_head": "skip"},
|
|
"placement": {"layers": [0]}, "inputs": list(names),
|
|
}]},
|
|
}
|
|
|
|
|
|
def _protocol(role, digest=QWEN38_SPLIT_SHA256):
|
|
return {"kind": "protocol_split", "dataset": "qwen38-v1", "split": role,
|
|
"role": role, "sha256": digest}
|
|
|
|
|
|
@pytest.fixture
|
|
def pairs_file(tmp_path):
|
|
pairs = {key: [f"{key} {end}" for end in PROMPT_ENDINGS] for key in ("harmful", "harmless")}
|
|
path = tmp_path / "train.json"
|
|
path.write_bytes(canonical_json(pairs))
|
|
pin = {"kind": "prompt_pairs_file", "path": path.name, "role": "train",
|
|
"sha256": sha256(path.read_bytes()).hexdigest()}
|
|
return pin, pairs
|
|
|
|
|
|
def test_normalization_canonicalizes_pins_and_checks_references(pairs_file):
|
|
pin, _ = pairs_file
|
|
record = _recipe({"fit-data": {**pin, "sha256": "sha256:" + pin["sha256"].upper()}}, ["fit-data"])
|
|
normalized = normalize_recipe(record)
|
|
assert normalized["inputs"]["fit-data"] == pin
|
|
assert normalize_recipe(normalized) == normalized
|
|
assert normalized["canonical_sha256"] == normalize_recipe(_recipe({"fit-data": pin}, ["fit-data"]))[
|
|
"canonical_sha256"
|
|
]
|
|
for name in ["missing", "later-output"]:
|
|
with pytest.raises(RecipeError, match=rf"unknown input.*{name}"):
|
|
normalize_recipe(_recipe({"fit-data": pin}, [name]))
|
|
|
|
|
|
@pytest.mark.parametrize(("role", "technique", "permitted"), [
|
|
("train", "abliteration.projection", True),
|
|
("calibration", "abliteration.projection", True),
|
|
("tune", "abliteration.projection", False),
|
|
("final_test", "abliteration.projection", False),
|
|
("tune", "evaluation.search", True),
|
|
("final_test", "evaluation.search", False),
|
|
("final_test", "evaluation.confirmation", True),
|
|
("tune", "evaluation.confirmation", False),
|
|
])
|
|
def test_input_role_is_checked_before_touching_sources(tmp_path, role, technique, permitted):
|
|
|
|
# A nonexistent file exposes which check runs first without starting a model.
|
|
pin = {"kind": "prompt_pairs_file", "path": "missing.json", "role": role, "sha256": "a" * 64}
|
|
expected = "missing.json" if permitted else rf"role.*{role}"
|
|
with pytest.raises(RecipeError, match=expected):
|
|
resolve_inputs(_recipe({"pairs": pin}, ["pairs"], technique), base_dir=tmp_path)
|
|
|
|
|
|
@pytest.mark.parametrize("role", ["train", "tune", "final_test"])
|
|
def test_protocol_split_uses_existing_partition_identity(tmp_path, role):
|
|
from obliteratus.verify_edit.heldout import _builtin_split
|
|
|
|
if role == "train":
|
|
pairs = _builtin_split().train
|
|
expected = {name: [pair[index] for pair in pairs]
|
|
for index, name in enumerate(("harmful", "harmless"))}
|
|
digest = QWEN38_SPLIT_SHA256
|
|
else:
|
|
heldout = load_heldout({"source": BUILTIN_HELDOUT, "pairs": None}, None, calibration=role == "tune")
|
|
expected = {key: heldout[key] for key in ("harmful", "harmless")}
|
|
digest = sha256(canonical_json(heldout["identity"])).hexdigest()
|
|
record = _recipe({"pairs": _protocol(role, digest)})
|
|
resolved = resolve_inputs(record, base_dir=tmp_path)["pairs"]
|
|
assert (resolved.kind, resolved.role, resolved.sha256, resolved.value) == (
|
|
"protocol_split", role, digest, expected,
|
|
)
|
|
record["inputs"]["pairs"]["sha256"] = "0" * 64
|
|
with pytest.raises(RecipeError, match="digest mismatch"):
|
|
resolve_inputs(record, base_dir=tmp_path)
|
|
|
|
|
|
def test_protocol_split_cannot_be_relabeled_as_train(tmp_path):
|
|
|
|
pin = {**_protocol("final_test"), "role": "train"}
|
|
with pytest.raises(RecipeError, match="split.*role|role.*split"):
|
|
resolve_inputs(_recipe({"pairs": pin}, ["pairs"]), base_dir=tmp_path)
|
|
|
|
|
|
def test_prompt_pairs_are_verified_and_parsed(tmp_path, pairs_file):
|
|
|
|
pin, pairs = pairs_file
|
|
record = _recipe({"named-fit": pin}, ["named-fit"])
|
|
resolved = resolve_inputs(record, base_dir=tmp_path)["named-fit"]
|
|
assert (resolved.sha256, resolved.value) == (pin["sha256"], pairs)
|
|
(tmp_path / pin["path"]).write_bytes(b"changed")
|
|
with pytest.raises(RecipeError, match="digest mismatch"):
|
|
resolve_inputs(record, base_dir=tmp_path)
|
|
|
|
|
|
def test_file_cannot_relabel_builtin_final_test_as_fitting_data(tmp_path):
|
|
|
|
heldout = load_heldout({"source": BUILTIN_HELDOUT, "pairs": 5}, None)
|
|
payload = canonical_json({key: heldout[key] for key in ("harmful", "harmless")})
|
|
(tmp_path / "pairs.json").write_bytes(payload)
|
|
pin = {"kind": "prompt_pairs_file", "path": "pairs.json", "role": "train",
|
|
"sha256": sha256(payload).hexdigest()}
|
|
with pytest.raises(RecipeError, match="overlaps.*final.test"):
|
|
resolve_inputs(_recipe({"fit": pin}, ["fit"]), base_dir=tmp_path)
|
|
|
|
|
|
def test_concept_pack_identity_is_verified(tmp_path):
|
|
from obliteratus.concepts.registry import ConceptRegistry
|
|
|
|
pack = ConceptRegistry().pack("refusal")
|
|
pin = {"kind": "concept_pack", "id": "refusal", "role": "train", "pack_hash": pack.sha256}
|
|
resolved = resolve_inputs(_recipe({"pack": pin}), base_dir=tmp_path)["pack"]
|
|
assert resolved.sha256 == resolved.value.pack_hash == pack.sha256
|
|
with pytest.raises(RecipeError, match="digest mismatch"):
|
|
resolve_inputs(_recipe({"pack": {**pin, "pack_hash": "0" * 64}}), base_dir=tmp_path)
|
|
|
|
|
|
@pytest.mark.parametrize("source", ["tokenizer", "file"])
|
|
def test_chat_template_verifies_utf8_bytes(tmp_path, source):
|
|
|
|
template = "{{ messages[0].content }}\r\n"
|
|
data = template.encode("utf-8")
|
|
model = SimpleNamespace(tokenizer=SimpleNamespace(chat_template=template))
|
|
pin = {"kind": "chat_template", "role": "train", "source": source, "sha256": sha256(data).hexdigest()}
|
|
if source == "file":
|
|
pin["path"] = "format.jinja"
|
|
(tmp_path / pin["path"]).write_bytes(data)
|
|
model = None
|
|
record = _recipe({"format": pin})
|
|
resolved = resolve_inputs(record, base_dir=tmp_path, model=model)["format"]
|
|
assert (resolved.sha256, resolved.value) == (pin["sha256"], template)
|
|
with pytest.raises(RecipeError, match="digest mismatch"):
|
|
resolve_inputs(_recipe({"format": {**pin, "sha256": "0" * 64}}), base_dir=tmp_path, model=model)
|
|
|
|
|
|
def test_reference_model_checks_exact_cached_revision_and_inventory(tmp_path, monkeypatch):
|
|
|
|
revision = "a" * 40
|
|
snapshot = tmp_path / "models--org--ref" / "snapshots" / revision
|
|
snapshot.mkdir(parents=True)
|
|
(snapshot / "config.json").write_text("{}", encoding="utf-8")
|
|
digest = sha256(canonical_json(file_inventory(snapshot))).hexdigest()
|
|
|
|
def cached(repo_id, *, revision, local_files_only):
|
|
assert repo_id == "org/ref" and local_files_only is True
|
|
assert len(revision) == 40
|
|
return str(snapshot)
|
|
|
|
monkeypatch.setattr("huggingface_hub.snapshot_download", cached)
|
|
pin = {"kind": "reference_model", "id": "org/ref", "revision": revision, "role": "calibration"}
|
|
result = resolve_inputs(_recipe({"stock": pin}), base_dir=tmp_path)["stock"]
|
|
assert (result.value, result.sha256) == (snapshot, digest)
|
|
for change, message in [({"sha256": "0" * 64}, "digest mismatch"),
|
|
({"revision": "b" * 40}, "revision mismatch"),
|
|
({"revision": "main"}, "revision")]:
|
|
with pytest.raises(RecipeError, match=message):
|
|
resolve_inputs(_recipe({"stock": {**pin, **change}}), base_dir=tmp_path)
|
|
|
|
|
|
@pytest.mark.parametrize("use_template", [False, True])
|
|
def test_projection_uses_pinned_train_pairs_and_records_digests(
|
|
tmp_path, pairs_file, tiny_model_runtime, monkeypatch, use_template,
|
|
):
|
|
from obliteratus.recipes.projection_runtime import _ProjectionPipeline, apply_projection_step
|
|
from jsonschema import Draft202012Validator
|
|
from obliteratus.recipes.resolution_records import read_checkpoint_resolution, validate_resolution
|
|
from obliteratus.recipes.techniques_projection import PROJECTION_SPEC
|
|
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
|
|
|
|
pin, pairs = pairs_file
|
|
recipe = _recipe({"named-fit": pin}, ["named-fit"])
|
|
expected_digests = {"named-fit": pin["sha256"]}
|
|
template = None
|
|
if use_template:
|
|
template = "{{ messages[0].content }}"
|
|
(tmp_path / "chat.jinja").write_text(template, encoding="utf-8")
|
|
template_sha = sha256(template.encode()).hexdigest()
|
|
recipe["inputs"]["format"] = {
|
|
"kind": "chat_template", "role": "calibration", "source": "file",
|
|
"path": "chat.jinja", "sha256": template_sha,
|
|
}
|
|
step = recipe["pipeline"]["steps"][0]
|
|
step["inputs"].append("format")
|
|
step["params"]["overrides"] = {"use_chat_template": True}
|
|
expected_digests["format"] = template_sha
|
|
source = build_tiny_offline_model(tmp_path / "source")
|
|
destination = tmp_path / "edited"
|
|
probe = _ProjectionPipeline._probe
|
|
seen = []
|
|
|
|
def inspect(pipeline):
|
|
seen.append({"harmful": pipeline.harmful_prompts, "harmless": pipeline.harmless_prompts})
|
|
assert pipeline.handle.tokenizer.chat_template == template
|
|
return probe(pipeline)
|
|
|
|
monkeypatch.setattr(_ProjectionPipeline, "_probe", inspect)
|
|
invalid = deepcopy(recipe)
|
|
invalid["inputs"]["named-fit"]["role"] = "final_test"
|
|
with pytest.raises(RecipeError, match="role.*final_test"):
|
|
apply_projection_step(source, invalid, step_id="first", base_dir=tmp_path)
|
|
if use_template:
|
|
without_format = deepcopy(recipe)
|
|
without_format["pipeline"]["steps"][0]["inputs"].remove("format")
|
|
with pytest.raises(RecipeError, match="declared chat_template"):
|
|
apply_projection_step(source, without_format, step_id="first", base_dir=tmp_path)
|
|
assert not seen
|
|
applied = apply_projection_step(source, recipe, step_id="first", base_dir=tmp_path, checkpoint=destination)
|
|
assert seen == [pairs]
|
|
assert applied.outputs["checkpoint"] == Path(destination)
|
|
Draft202012Validator(PROJECTION_SPEC.resolution_schema).validate(applied.resolution)
|
|
assert applied.resolution["input_sha256"] == expected_digests
|
|
resolution = read_checkpoint_resolution(destination)
|
|
assert resolution["input_sha256"] == applied.resolution["input_sha256"]
|
|
assert resolution["recipe_sha256"] == normalize_recipe(recipe)["canonical_sha256"]
|
|
assert resolution["steps"][0]["id"] == "first"
|
|
resolution["input_sha256"]["named-fit"] = "invalid"
|
|
with pytest.raises(RecipeError, match="input_sha256"):
|
|
validate_resolution(resolution)
|
|
|
|
|
|
@pytest.mark.parametrize(("role", "location_key"), [("train", "path"), ("calibration", "id")])
|
|
def test_recovery_corpus_pins_canonical_metadata(tmp_path, role, location_key):
|
|
import json
|
|
|
|
from obliteratus.recovery_dataset import digest, json_bytes, load_recovery_dataset
|
|
from tests.test_recover import make_corpus
|
|
|
|
path = make_corpus(tmp_path)
|
|
_, metadata = load_recovery_dataset(path)
|
|
expected = digest(json_bytes(metadata))
|
|
# On-disk formatting is independent of the metadata pin.
|
|
(path / "metadata.json").write_text(json.dumps(metadata, indent=2), encoding="utf-8")
|
|
pin = {"kind": "recovery_corpus", location_key: path.name, "sha256": expected, "role": role}
|
|
recipe = _recipe({"corpus": pin})
|
|
resolved = resolve_inputs(recipe, base_dir=tmp_path)["corpus"]
|
|
assert (resolved.value, resolved.sha256) == (path, expected)
|
|
pin["sha256"] = "0" * 64
|
|
with pytest.raises(RecipeError, match=r"corpus.*digest mismatch"):
|
|
resolve_inputs(recipe, base_dir=tmp_path)
|
|
|
|
|
|
@pytest.mark.parametrize("role", ["tune", "final_test", "runtime"])
|
|
def test_recovery_corpus_requires_a_fitting_role(role):
|
|
pin = {"kind": "recovery_corpus", "id": "local", "sha256": "a" * 64, "role": role}
|
|
with pytest.raises(RecipeError, match=r"role"):
|
|
normalize_recipe(_recipe({"corpus": pin}))
|
|
|
|
|
|
@pytest.mark.parametrize("role", ["train", "calibration", "tune", "final_test"])
|
|
def test_runtime_binding_cannot_claim_a_data_role(role):
|
|
pin = {"kind": "runtime_binding", "description": "An output directory", "role": role}
|
|
with pytest.raises(RecipeError, match=r"role"):
|
|
normalize_recipe(_recipe({"destination": pin}))
|
|
|
|
|
|
def test_projection_cannot_use_runtime_binding_for_extraction():
|
|
from obliteratus.recipes import apply_recipe
|
|
|
|
pin = {"kind": "runtime_binding", "description": "Unpinned prompts", "role": "runtime"}
|
|
record = _recipe({"extraction": pin}, ["extraction"])
|
|
target = {"model_id": "tiny", "revision": "local", "structure": {"tie_word_embeddings": None}}
|
|
with pytest.raises(RecipeError, match=r"projection.*train prompt-pair"):
|
|
apply_recipe(record, initial_state=None, target=target, inputs={
|
|
"extraction": {"harmful_prompts": ["unverified"], "harmless_prompts": ["inputs"]},
|
|
})
|
|
|
|
|
|
def test_runtime_role_has_recipe_jsonschema_parity():
|
|
from jsonschema import Draft202012Validator
|
|
|
|
from obliteratus.recipes.validation import _schema, validate_recipe
|
|
|
|
pin = {"kind": "runtime_binding", "description": "Run location", "role": "runtime"}
|
|
recipe = _recipe({"destination": pin})
|
|
validator = Draft202012Validator(_schema("v2"))
|
|
assert validator.is_valid(recipe)
|
|
validate_recipe(recipe)
|
|
pin["role"] = "unknown"
|
|
assert not validator.is_valid(recipe)
|
|
with pytest.raises(RecipeError, match="role"):
|
|
validate_recipe(recipe)
|
|
|
|
|
|
@pytest.mark.parametrize(("bindings", "valid"), [
|
|
([], True),
|
|
(["artifact_dir", "checkpoint"], True),
|
|
(["checkpoint", "checkpoint"], False),
|
|
("/local/checkpoint", False),
|
|
(["/local/checkpoint"], False),
|
|
([r"C:\local\checkpoint"], False),
|
|
([" "], False),
|
|
([123], False),
|
|
])
|
|
def test_runtime_bindings_have_resolution_jsonschema_parity(bindings, valid):
|
|
from jsonschema import Draft202012Validator
|
|
|
|
from obliteratus.recipes.resolution_records import _schema, validate_resolution
|
|
from tests.test_recipe_resolution_records import _record
|
|
|
|
resolution = {**_record(), "runtime_bindings": bindings}
|
|
assert Draft202012Validator(_schema()).is_valid(resolution) is valid
|
|
if valid:
|
|
validate_resolution(resolution)
|
|
else:
|
|
with pytest.raises(RecipeError, match="runtime_bindings"):
|
|
validate_resolution(resolution)
|
|
|
|
|
|
def test_runtime_bindings_must_be_sorted_in_resolution():
|
|
from obliteratus.recipes.resolution_records import validate_resolution
|
|
from tests.test_recipe_resolution_records import _record
|
|
|
|
resolution = {**_record(), "runtime_bindings": ["checkpoint", "artifact_dir"]}
|
|
with pytest.raises(RecipeError, match=r"runtime_bindings.*sorted"):
|
|
validate_resolution(resolution)
|