mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Ship as accepted v2 data the nine recipes that passed the protocol final
test and were accepted by the operator on 2026-11-27: Granite 4.1 8B,
Llama-3.2-1B, Llama-3.2-3B, OLMoE-1B-7B, Phi-4-mini, Qwen3.5-0.8B,
Qwen3.5-9B, Llama-3.1-8B-Instruct-abliterated and DeepSeek-R1-Distill-
Qwen-7B. Each ships a KL-advisory evaluation profile and value-only
confirmation and verify-edit evidence bound by SHA-256, following the two
recipes accepted in 134bd2c.
The Llama-3.2 results predate the chat-template date pin (#690) and are not
comparable with later measurements. Two recipes were confirmed with
analysis_device auto (resolved to cuda:0) and ship as confirmed; the shipped
CPU-extraction test now names them explicitly.
273 lines
11 KiB
Python
273 lines
11 KiB
Python
"""Offline contracts for versioned recipes and the exact-match registry."""
|
|
|
|
from copy import deepcopy
|
|
from hashlib import sha256
|
|
from importlib.resources import files
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
|
|
from jsonschema import Draft202012Validator, ValidationError
|
|
import pytest
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
SEEDS = {
|
|
"llama3.1-8b-e03": (
|
|
"meta-llama/Llama-3.1-8B-Instruct",
|
|
"0e9e39f249a16976918f6564b8830bc894c89659",
|
|
"qwen38_e03",
|
|
"candidate",
|
|
),
|
|
"qwen2.5-1.5b-mid12-16": (
|
|
"Qwen/Qwen2.5-1.5B-Instruct",
|
|
"989aa7980e4cf806f80c7fef2b1adb7bc71aa306",
|
|
"advanced",
|
|
"candidate",
|
|
),
|
|
"qwen38-27b-e03": (
|
|
"Qwen/Qwen3.8-27B",
|
|
"1d4bf0f2ff6012fd82039f2fa52739d0dd7c60c0",
|
|
"qwen38_e03",
|
|
"accepted",
|
|
),
|
|
}
|
|
|
|
|
|
ACCEPTED = (
|
|
"granite4.1-8b-mid12-28-dim1", "llama3.1-8b-abliterated-mid9-23-dim1", "llama3.1-8b-e03-reg02",
|
|
"llama3.2-1b-global-l6-3-12", "llama3.2-3b-e03-reg02-8-21-qc128", "olmoe-1b-7b-mid6-10-perexpert",
|
|
"phi4-mini-global-l13-6-24", "qwen2.5-1.5b-mid10-18-dim1", "qwen3.5-0.8b-e03-small-6-17-qc128",
|
|
"qwen3.5-9b-e03-reg02-3-28-klb1", "r1-distill-qwen-7b-global-l11-5-21",
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def individual():
|
|
return {
|
|
"schema": "obliteratus.model-recipe/v1",
|
|
"name": "test-model",
|
|
"version": "1.0.0",
|
|
"scope": "individual",
|
|
"identity": {"model_id": "org/model", "revision": "a" * 40},
|
|
"edit": {"method": "advanced", "overrides": {"layers": [0, 2]}},
|
|
"margins": None,
|
|
"evidence": [],
|
|
"status": "candidate",
|
|
"accepted_by": None,
|
|
"accepted_at": None,
|
|
"notes": "",
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def schema_validator():
|
|
schema = files("obliteratus.recipes").joinpath("schema", "model-recipe.v1.schema.json")
|
|
document = json.loads(schema.read_text(encoding="utf-8"))
|
|
Draft202012Validator.check_schema(document)
|
|
return Draft202012Validator(document)
|
|
|
|
|
|
def test_load_individual_recipe(tmp_path, individual, schema_validator):
|
|
from obliteratus.recipes import load_recipe, migrate_v1
|
|
|
|
path = tmp_path / "recipe.json"
|
|
path.write_text(json.dumps(individual), encoding="utf-8")
|
|
schema_validator.validate(individual)
|
|
assert load_recipe(path) == migrate_v1(individual)
|
|
|
|
|
|
def test_load_family_recipe(tmp_path, individual, schema_validator):
|
|
from obliteratus.recipes import load_recipe, migrate_v1
|
|
|
|
individual.update(scope="family", identity={"match": {"model_type": "llama"}})
|
|
individual["edit"]["overrides"] = {"use_chat_template": False}
|
|
path = tmp_path / "family.json"
|
|
path.write_text(json.dumps(individual), encoding="utf-8")
|
|
schema_validator.validate(individual)
|
|
assert load_recipe(path) == migrate_v1(individual)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("changes", "error"),
|
|
[
|
|
({"extra": 1}, "unknown field extra"),
|
|
({"scope": "fleet"}, "scope"),
|
|
({"scope": "family", "identity": {"match": {"model_type": "llama"}}},
|
|
r"edit.overrides.layers"),
|
|
({"scope": "family", "identity": {"match": 1}}, r"identity.match"),
|
|
({"identity": {"model_id": "org/model"}}, "missing revision"),
|
|
({"identity": None}, "identity"),
|
|
({"status": "approved"}, "status"),
|
|
({"status": "accepted"}, "margins"),
|
|
({"margins": {"spec_path": "../outside.json", "spec_sha256": "a" * 64}}, "spec_path"),
|
|
({"margins": {"spec_path": "/absolute.json", "spec_sha256": "a" * 64}}, "spec_path"),
|
|
({"evidence": [{"kind": "run", "path": "run.json", "sha256": "bad", "summary": "run"}]},
|
|
"evidence"),
|
|
({"edit": {"method": "typo", "overrides": {}}}, r"edit.method"),
|
|
({"edit": {"method": "advanced", "overrides": {"bogus": 1}}}, "unknown field bogus"),
|
|
({"edit": {"method": "advanced", "overrides": {"hub_token": "secret"}}},
|
|
"unknown field hub_token"),
|
|
({"edit": {"method": "advanced", "overrides": {"layers": [-1]}}}, r"layers\[0\]"),
|
|
({"edit": {"method": "advanced", "overrides": {"layers": [1.5]}}}, r"layers\[0\]"),
|
|
({"edit": {"method": "advanced", "overrides": {"layers": [True]}}}, r"layers\[0\]"),
|
|
({"edit": {"method": "advanced", "overrides": {"layers": []}}}, "layers"),
|
|
({"edit": {"method": "advanced", "overrides": {"projection_row_fraction": 0}}}, "fraction"),
|
|
({"schema": "obliteratus.model-recipe/v2"}, "schema"),
|
|
],
|
|
)
|
|
def test_invalid_records_agree(tmp_path, individual, schema_validator, changes, error):
|
|
from obliteratus.recipes import RecipeError, load_recipe
|
|
|
|
individual.update(changes)
|
|
path = tmp_path / "invalid.json"
|
|
path.write_text(json.dumps(individual), encoding="utf-8")
|
|
with pytest.raises(RecipeError, match=error):
|
|
load_recipe(path)
|
|
with pytest.raises(ValidationError):
|
|
schema_validator.validate(individual)
|
|
|
|
|
|
def test_missing_identity_agrees(tmp_path, individual, schema_validator):
|
|
from obliteratus.recipes import RecipeError, load_recipe
|
|
|
|
del individual["identity"]
|
|
path = tmp_path / "missing.json"
|
|
path.write_text(json.dumps(individual), encoding="utf-8")
|
|
with pytest.raises(RecipeError, match="missing identity"):
|
|
load_recipe(path)
|
|
with pytest.raises(ValidationError):
|
|
schema_validator.validate(individual)
|
|
|
|
|
|
@pytest.mark.parametrize("payload", ['{"name":"one","name":"two"}', '{"value":NaN}', '{"value":1e999}'])
|
|
def test_malformed_json_reports_filename(tmp_path, payload):
|
|
from obliteratus.recipes import RecipeError, load_recipe
|
|
|
|
path = tmp_path / "malformed.json"
|
|
path.write_text(payload, encoding="utf-8")
|
|
with pytest.raises(RecipeError, match=r"malformed.json.*invalid JSON"):
|
|
load_recipe(path)
|
|
|
|
|
|
def test_bundled_registry_and_schema_agree():
|
|
from obliteratus.recipes import RecipeRegistry, validate_recipe
|
|
|
|
registry = RecipeRegistry()
|
|
assert registry.list() == tuple(sorted((*SEEDS, "llama3-instruct-family-e03", *ACCEPTED)))
|
|
for name, (model_id, revision, method, status) in SEEDS.items():
|
|
record = registry.load(name)
|
|
validate_recipe(record)
|
|
assert record["target"]["identity"]["model_id"] == model_id
|
|
assert record["target"]["identity"]["revision"] == revision
|
|
assert record["pipeline"]["steps"][0]["params"]["preset"] == method
|
|
assert record["status"] == status
|
|
selected = {
|
|
"llama3.1-8b-e03": "llama3.1-8b-e03-reg02", "qwen2.5-1.5b-mid12-16": "qwen2.5-1.5b-mid10-18-dim1",
|
|
}.get(name, name)
|
|
assert registry.lookup(model_id, revision) == registry.load(selected)
|
|
assert registry.lookup(model_id, revision.upper()) is None
|
|
assert registry.lookup(model_id.lower(), revision) is None
|
|
assert registry.lookup("org/other", "z" * 40) is None
|
|
with pytest.raises(KeyError, match="unknown-recipe"):
|
|
registry.load("unknown-recipe")
|
|
|
|
small = registry.load("qwen2.5-1.5b-mid12-16")
|
|
assert small["pipeline"]["steps"][0]["params"]["overrides"] == {
|
|
"analysis_device": "cpu",
|
|
"direction_method": "diff_means", "n_directions": 1, "layers": [12, 13, 14, 15, 16],
|
|
"regularization": 0.0, "refinement_passes": 1, "projection_target": "output",
|
|
"projection_row_fraction": 1.0,
|
|
}
|
|
|
|
accepted = registry.load("qwen38-27b-e03")
|
|
margin = accepted["evaluation"]
|
|
spec = files("obliteratus.recipes").joinpath("data", margin["profile"])
|
|
assert sha256(spec.read_bytes()).hexdigest() == margin["sha256"]
|
|
assert accepted["evidence"]
|
|
assert "manitcor/Qwen3.8-27B-Obliterated-E03" in accepted["evidence"][0]["summary"]
|
|
assert registry.lookup("manitcor/Qwen3.8-27B-Obliterated-E03",
|
|
"56bbc4a80c17353254c0ed0f31828e3980970495") is None
|
|
|
|
|
|
@pytest.mark.parametrize("duplicate_name", [True, False])
|
|
def test_registry_rejects_duplicates(tmp_path, individual, duplicate_name):
|
|
from obliteratus.recipes import RecipeError, RecipeRegistry
|
|
|
|
second = deepcopy(individual)
|
|
if duplicate_name:
|
|
second["identity"]["revision"] = "b" * 40
|
|
error = "duplicate recipe name.*test-model"
|
|
else:
|
|
second["name"] = "another"
|
|
error = "duplicate model identity.*org/model"
|
|
for filename, record in [("first.json", individual), ("second.json", second)]:
|
|
(tmp_path / filename).write_text(json.dumps(record), encoding="utf-8")
|
|
with pytest.raises(RecipeError, match=error):
|
|
RecipeRegistry(tmp_path)
|
|
|
|
|
|
def test_registry_records_are_independent_and_family_is_listed(tmp_path, individual):
|
|
from obliteratus.recipes import RecipeRegistry, migrate_v1
|
|
|
|
family = dict(individual, scope="family", name="family",
|
|
identity={"match": {"model_type": "llama"}})
|
|
family["edit"] = {"method": "advanced", "overrides": {}}
|
|
for record in (individual, family):
|
|
(tmp_path / (record["name"] + ".json")).write_text(json.dumps(record), encoding="utf-8")
|
|
registry = RecipeRegistry(tmp_path)
|
|
assert registry.list() == ("family", "test-model")
|
|
assert registry.load("family") == migrate_v1(family)
|
|
registry.load("test-model")["pipeline"]["steps"][0]["params"]["overrides"]["layers"].append(99)
|
|
result = registry.lookup("org/model", "a" * 40)
|
|
result["target"]["identity"]["model_id"] = "org/other"
|
|
assert registry.lookup("org/model", "a" * 40) == migrate_v1(individual)
|
|
|
|
|
|
def test_registry_has_no_runtime_dependency_on_site_packages():
|
|
subprocess.run(
|
|
[sys.executable, "-S", "-c",
|
|
"from obliteratus.recipes import RecipeRegistry; assert len(RecipeRegistry().list()) == 15"],
|
|
cwd=Path(__file__).resolve().parents[1],
|
|
check=True,
|
|
)
|
|
|
|
|
|
def test_v1_load_migrates_and_v1_write_is_refused(tmp_path, individual, capsys):
|
|
from obliteratus.recipes import RecipeError, load_recipe, migrate_v1
|
|
from obliteratus.recipes.validation import write_recipe
|
|
|
|
path = tmp_path / "legacy.json"
|
|
path.write_text(json.dumps(individual), encoding="utf-8")
|
|
assert load_recipe(path) == migrate_v1(individual)
|
|
assert "v1 recipes are read-only; migrating" in capsys.readouterr().err
|
|
destination = tmp_path / "new.json"
|
|
with pytest.raises(RecipeError, match="v1.*read-only"):
|
|
write_recipe(individual, destination)
|
|
assert not destination.exists()
|
|
|
|
|
|
@pytest.mark.parametrize("name", ACCEPTED)
|
|
def test_confirmed_recipes_resolve_advisory_kl(name):
|
|
from obliteratus.recipes import RecipeRegistry, normalize_recipe
|
|
from obliteratus.recipes.evaluation_profiles import resolve_recipe_profile
|
|
|
|
registry = RecipeRegistry()
|
|
record = normalize_recipe(registry.load(name))
|
|
identity = record["target"]["identity"]
|
|
assert record["status"] == "accepted"
|
|
assert record["accepted_by"] == "jmagly"
|
|
assert registry.lookup(identity["model_id"], identity["revision"])["name"] == name
|
|
_, provenance = resolve_recipe_profile(record, directory=registry.source(name).parent)
|
|
assert provenance["kl_policy"] == "advisory"
|
|
evidence = {entry["kind"]: entry for entry in record["evidence"]}
|
|
assert "outcome: pass" in evidence["final-test-confirmation"]["summary"]
|
|
assert "PASS" in evidence["verify-edit"]["summary"]
|
|
for kind in ("final-test-confirmation", "verify-edit"):
|
|
entry = evidence[kind]
|
|
summary = registry.source(name).parent / entry["path"]
|
|
assert sha256(summary.read_bytes()).hexdigest() == entry["sha256"]
|
|
if kind == "final-test-confirmation":
|
|
assert json.loads(summary.read_bytes())["outcome"] == "pass"
|