"""CPU contracts for evaluation profiles and their recipe confirmation binding.""" from copy import deepcopy from hashlib import sha256 from importlib.resources import files from jsonschema import Draft202012Validator import pytest from tests.test_recipe_confirmation import REVISION, Runner, digest, read, write from obliteratus.qualification import canonical_json from obliteratus.recipes import RecipeError, load_recipe from obliteratus.verify_edit.margins import resolve_margins pytestmark = pytest.mark.cpu PROFILE_SCHEMA = "obliteratus.evaluation-profile/v1" RESOURCE = files("obliteratus.recipes") BUNDLED = RESOURCE.joinpath("data", "profiles", "llama3.1-8b-instruct.json") def profile(scope="individual", **fields): return {"schema": PROFILE_SCHEMA, "name": f"example-{scope}", "version": "1.0.0", "scope": scope, **fields} @pytest.mark.parametrize(("overrides", "error"), [ ({}, None), ({"spec": {"heldout": {"source": "file", "pairs": None}, "generation": {"seed": -1}, "endpoints": {"capability": {"temperature": None, "generation_seed": 42.0}}}}, None), ({"scope": "family", "inherits": {"path": "family.json", "sha256": "a" * 64}}, "inherits"), ({"version": "v1"}, "version"), ({"spec": {"endpoints": {"capability": {"tier": "large"}}}}, "tier"), ({"spec": {"endpoints": {"capability": {"batch_invariant": 1}}}}, "batch_invariant"), ({"spec": {"endpoints": {"kl": {"max_first_token_kl": 0}}}}, "max_first_token_kl"), ({"spec": {"endpoints": {"kl": {"required": True}}}}, "required"), ({"spec": {"heldout": {"source": "remote"}}}, "heldout.source"), ({"kl_policy": "ignore"}, "kl_policy"), ({"calibration": [{"kind": "noise_floor", "path": "../evidence.json", "sha256": "a" * 64, "thresholds": {"endpoints.kl.max_first_token_kl": 0.01}}]}, "path"), ({"calibration": [{"kind": "noise_floor", "path": "evidence.json", "sha256": "a" * 64, "thresholds": {"endpoints.capability.max_drop": 0.01}}]}, "thresholds"), ]) def test_profile_schema_matches_offline_validation(overrides, error): from obliteratus.recipes.evaluation_profiles import validate_profile schema = read(RESOURCE.joinpath("schema", "evaluation-profile.v1.schema.json")) Draft202012Validator.check_schema(schema) validator = Draft202012Validator(schema) record = profile(**overrides) assert validator.is_valid(record) == (error is None) if error is None: validate_profile(record) else: with pytest.raises(RecipeError, match=error): validate_profile(record) def test_profile_resolution_preserves_margin_precedence_and_hash(): from obliteratus.recipes.evaluation_profiles import resolve_profile from obliteratus.verify_edit.spec import resolve_spec family = profile("family", kl_policy="advisory", spec={ "endpoints": {"kl": {"max_first_token_kl": 0.2}, "capability": {"tier": "full"}}, }) individual = profile(kl_policy="gate", spec={ "endpoints": {"kl": {"max_next_token_kl": 0.03}, "capability": {"tier": "smoke"}}, }) original = deepcopy((family, individual)) spec, provenance = resolve_profile(individual, family=family) expected, _ = resolve_margins({}, family["spec"], individual["spec"]) assert spec == expected == resolve_spec(spec) assert (family, individual) == original assert provenance["resolved_sha256"] == sha256(canonical_json(spec)).hexdigest() assert provenance == resolve_profile(dict(reversed(list(individual.items()))), family=family)[1] assert provenance["sources"]["endpoints.kl.max_first_token_kl"] == "family" assert provenance["sources"]["endpoints.capability.tier"] == "individual" assert provenance["sources"]["endpoints.capability.temperature"] == "default" assert spec["endpoints"]["kl"]["required"] is True advisory, _ = resolve_profile(profile(), family=family) assert advisory["endpoints"]["kl"]["required"] is False def test_profile_and_evidence_refs_pin_raw_bytes_and_winning_fields(tmp_path): from obliteratus.recipes.evaluation_profiles import resolve_profile first = "endpoints.kl.max_first_token_kl" next_token = "endpoints.kl.max_next_token_kl" evidence = tmp_path / "noise.json" evidence_ref = {"kind": "noise_floor", "path": evidence.name, "sha256": write(evidence, { "schema": "obliteratus.verify-edit.calibration/v1", "suggested_thresholds": {"kl": {"max_first_token_kl": 0.01, "max_next_token_kl": 0.02}}, }), "thresholds": {first: 0.01, next_token: 0.02}} family = tmp_path / "family.json" binding = {"path": family.name, "sha256": write( family, profile("family", calibration=[evidence_ref], kl_policy="advisory"), )} child = tmp_path / "child.json" write(child, profile(inherits=binding, spec={"endpoints": {"kl": {"max_next_token_kl": 0.03}}})) spec, provenance = resolve_profile(child) assert spec["endpoints"]["kl"] == { "required": False, "prompts": 50, "max_first_token_kl": 0.01, "max_next_token_kl": 0.03, } assert provenance["calibration"]["sources"] == {first: {"scope": "family", "index": 0}} assert provenance["calibration"]["family"] == [evidence_ref] assert provenance["profiles"]["family"]["sha256"] == binding["sha256"] evidence.write_bytes(evidence.read_bytes() + b"\n") with pytest.raises(RecipeError, match="calibration.*digest"): resolve_profile(child) family.write_bytes(family.read_bytes() + b"\n") with pytest.raises(RecipeError, match="family profile.*digest"): resolve_profile(child) def test_bundled_llama_profile_is_advisory_and_evidence_is_offline(): from obliteratus.recipes.evaluation_profiles import resolve_profile bundled = read(BUNDLED) assert "candidate" in bundled["version"] spec, provenance = resolve_profile(BUNDLED) capability = spec["endpoints"]["capability"] assert (capability["tier"], capability["temperature"], capability["generation_seed"], capability["batch_invariant"]) == ("quick", 0.0, 42, True) kl = spec["endpoints"]["kl"] assert (kl["required"], kl["max_first_token_kl"], kl["max_next_token_kl"]) == (False, 0.00258, 0.02087) reference, = provenance["calibration"]["individual"] assert reference["kind"] == "random_control" assert reference["sha256"] == digest(BUNDLED.parent.joinpath(reference["path"])) SHIPPED_PROFILES = RESOURCE.joinpath("data", "profiles") # Pinned evidence files that live next to the profiles but use their own schema. EVIDENCE_SCHEMAS = {"obliteratus.verify-edit.random-control/v1"} def _shipped_profiles(): entries = sorted(SHIPPED_PROFILES.iterdir(), key=lambda entry: entry.name) return [entry for entry in entries if entry.name.endswith(".json")] def test_every_shipped_evaluation_profile_validates_against_its_schema(): from obliteratus.recipes.evaluation_profiles import _schema, validate_profile validator = Draft202012Validator(_schema()) checked = [] for entry in _shipped_profiles(): record = read(entry) if record.get("schema") in EVIDENCE_SCHEMAS: continue assert record.get("schema") == PROFILE_SCHEMA, entry.name errors = [f"{'/'.join(map(str, error.path))}: {error.message}" for error in validator.iter_errors(record)] assert errors == [], f"{entry.name}: {errors}" validate_profile(record) checked.append(entry.name) assert "qwen38-27b-e03.json" in checked def _bundled_recipes_with_profiles(): from obliteratus.recipes import RecipeRegistry registry = RecipeRegistry() for name in registry.list(): source = registry.source(name) recipe = load_recipe(source) if recipe.get("evaluation") is not None: yield name, source, recipe def test_every_bundled_recipe_profile_resolves_at_its_pinned_digest(): from obliteratus.recipes.evaluation_profiles import resolve_recipe_profile resolved = {} for name, source, recipe in _bundled_recipes_with_profiles(): profile = source.parent.joinpath(recipe["evaluation"]["profile"]) assert digest(profile) == recipe["evaluation"]["sha256"], name spec, provenance = resolve_recipe_profile(recipe, directory=source.parent) assert provenance["profiles"]["individual"]["sha256"] == recipe["evaluation"]["sha256"] resolved[name] = spec # The qwen38 E03 profile declares no verify-edit overrides: it resolves to the defaults. assert resolved["qwen38-27b-e03"] == resolve_margins({})[0] @pytest.fixture def v2_case(tmp_path): stock = tmp_path / "models--org--Model-8B" / "snapshots" / REVISION stock.mkdir(parents=True) write(stock / "config.json", { "model_type": "llama", "num_hidden_layers": 32, "hidden_size": 4096, "architectures": ["LlamaForCausalLM"], "num_parameters": 8_000_000_000, }) profiles = tmp_path / "profiles" profiles.mkdir() evaluation = profiles / "eval.json" recipe = { "schema": "obliteratus.model-recipe/v2", "name": "candidate-v2", "version": "1.0.0", "target": {"scope": "individual", "identity": {"model_id": "org/Model-8B", "revision": REVISION}}, "engine": {"min_version": "0.1.4", "normalizer": "recipe-normalizer/1"}, "pipeline": {"steps": [{"id": "projection", "technique": "abliteration.projection", "technique_version": "1", "params": {"preset": "advanced"}, "placement": {"layers": [3, 4]}}]}, "evaluation": {"profile": "profiles/eval.json", "sha256": write( evaluation, profile(kl_policy="advisory", spec={ "endpoints": {"kl": {"max_first_token_kl": 0.18}, "capability": {"tier": "smoke"}}, }), )}, } path = tmp_path / "candidate.json" write(path, recipe) return path, recipe, evaluation, { "stock": stock, "output_dir": tmp_path / "out", "accepted_by": "reviewer", } def test_v2_recipe_profile_and_confirmation_pin_the_resolved_spec(v2_case): from obliteratus.recipes.confirmation import run_confirmation from obliteratus.recipes.evaluation_profiles import resolve_recipe_profile path, recipe, evaluation, options = v2_case spec, provenance = resolve_recipe_profile(recipe, directory=path.parent) runner = Runner() record = run_confirmation(path, runner=runner, **options) assert record["outcome"] == "pass" assert runner.calls == ["recipe", "verify-checkpoint", "verify-edit"] step, = runner.request["recipe"]["pipeline"]["steps"] assert step["technique"] == "abliteration.projection" assert step["placement"]["layers"] == [3, 4] assert step["params"]["backend"]["method"] == "advanced" attempt = options["output_dir"] / "runs" / record["run_id"] assert read(attempt / "spec.json") == spec assert record["margins"]["resolved_sha256"] == digest(attempt / "spec.json") == provenance["resolved_sha256"] assert record["margins"]["profiles"]["individual"]["sha256"] == digest(evaluation) assert record["verification"]["endpoints"]["kl"]["required"] is False accepted_path = attempt / "recipe.json" accepted = load_recipe(accepted_path) assert accepted["schema"] == recipe["schema"] assert accepted["status"] == "accepted" assert resolve_recipe_profile(accepted, directory=attempt)[0] == spec def test_v2_recipe_profile_digest_mismatch_stops_before_confirmation(v2_case): from obliteratus.recipes.confirmation import run_confirmation path, _, evaluation, options = v2_case evaluation.write_bytes(evaluation.read_bytes() + b"\n") runner = Runner() with pytest.raises(RecipeError, match="evaluation profile.*digest"): run_confirmation(path, runner=runner, **options) assert runner.calls == [] assert not options["output_dir"].exists() def test_v2_edit_dispatch_uses_the_registered_projection_and_train_inputs(v2_case, tmp_path, monkeypatch): from obliteratus.recipes.application import prepare_execution_recipe from obliteratus.recipes.techniques import AppliedStep from obliteratus.recipes import projection_runtime from obliteratus.recipes.search_cli import run_edit_request from obliteratus.verify_edit.heldout import write_extraction_pairs _, recipe, _, options = v2_case execution = prepare_execution_recipe(recipe) step, = execution["pipeline"]["steps"] extraction = tmp_path / "train.json" write_extraction_pairs(extraction) request = tmp_path / "request.json" write(request, {"stock": str(options["stock"]), "checkpoint": str(tmp_path / "edited"), "target": recipe["target"]["identity"], "directory": str(tmp_path), "extraction": str(extraction), "device": "cpu", "dtype": "float32", "recipe": execution}) from types import SimpleNamespace from unittest.mock import Mock state_handle = SimpleNamespace() monkeypatch.setattr("obliteratus.models.loader.load_model", Mock(return_value=state_handle)) calls = [] def apply(state, **kwargs): calls.append((state, kwargs)) checkpoint = kwargs["inputs"]["checkpoint"] checkpoint.mkdir() from obliteratus.verify_checkpoint import REFERENCE_FILENAME (checkpoint / REFERENCE_FILENAME).touch() effective = kwargs["params"] return AppliedStep(state, {}, { "artifacts": [], "resolved_layers": [3, 4], "resolved_writers": {}, "ranks": {"3": 1, "4": 1}, "effective_n_directions": 1, "restored_groups": [], "effective_params": effective, "safeguards_applied": {}, "tied_head": {"tied_embeddings": False, "decision": "untied"}, }) monkeypatch.setattr(projection_runtime, "apply_projection", apply) run_edit_request(request) (state, kwargs), = calls train = read(extraction) assert state is state_handle assert kwargs["params"] == step["params"] assert kwargs["placement"] == step["placement"] assert kwargs["device"] == "cpu" and kwargs["dtype"] == "float32" assert kwargs["inputs"]["extraction"] == {"harmful_prompts": train["harmful"], "harmless_prompts": train["harmless"]} assert kwargs["reference_prompts"][0]["text"] == train["harmful"][0] @pytest.mark.parametrize(("problem", "message"), [ ("wrong_scope", "family scope"), ("wrong_kind", "unexpected evidence schema"), ("duplicate_adoption", "multiple references"), ("conflicting_margin", "disagrees with profile spec"), ]) def test_profile_resolution_rejects_inconsistent_declarations(tmp_path, problem, message): from obliteratus.recipes.evaluation_profiles import resolve_profile key = "endpoints.kl.max_first_token_kl" evidence = tmp_path / "data.json" reference = {"kind": "random_control", "path": evidence.name, "sha256": write(evidence, { "schema": ("obliteratus.verify-edit.calibration/v1" if problem == "wrong_kind" else "obliteratus.verify-edit.random-control/v1"), }), "thresholds": {key: 0.02}} record = profile(calibration=[reference]) if problem == "wrong_scope": child = tmp_path / "parent.json" record["inherits"] = {"path": child.name, "sha256": write(child, profile())} elif problem == "duplicate_adoption": record["calibration"].append(reference) elif problem == "conflicting_margin": record["spec"] = {"endpoints": {"kl": {"max_first_token_kl": 0.03}}} declaration = tmp_path / "profile.json" write(declaration, record) with pytest.raises(RecipeError, match=message): resolve_profile(declaration) def test_v2_confirmation_inherits_omitted_recipe_profile(v2_case): from obliteratus.recipes.confirmation import run_confirmation from obliteratus.recipes.evaluation_profiles import resolve_recipe_profile path, recipe, evaluation, options = v2_case parent = deepcopy(recipe) parent.update(name="family-v2", target={"scope": "family", "match": {"model_type": "llama"}}) parent["evaluation"]["sha256"] = write(evaluation, profile("family", kl_policy="advisory")) family = path.with_name("family-v2.json") write(family, parent) recipe.pop("evaluation") recipe["inherits"] = parent["name"] write(path, recipe) record = run_confirmation(path, runner=Runner(), **options) assert record["outcome"] == "pass" assert record["recipe"]["parent"]["sha256"] == digest(family) assert record["margins"]["profiles"]["family"]["sha256"] == digest(evaluation) attempt = options["output_dir"] / "runs" / record["run_id"] assert resolve_recipe_profile(load_recipe(attempt / "recipe.json"), directory=attempt)[0] == read( attempt / "spec.json" ) def test_v2_confirmation_rejects_unchecked_weight_pin(v2_case): from obliteratus.recipes.confirmation import run_confirmation path, recipe, _, options = v2_case recipe["target"]["identity"]["weights_sha256"] = "0" * 64 write(path, recipe) runner = Runner() with pytest.raises(RecipeError, match="weights_sha256"): run_confirmation(path, runner=runner, **options) assert runner.calls == [] assert not options["output_dir"].exists()