"""Concept steering CLI contracts without model loading or inference.""" import argparse from copy import deepcopy import json from pathlib import Path from types import SimpleNamespace from unittest.mock import Mock from jsonschema import Draft202012Validator, ValidationError import pytest import torch import yaml from obliteratus.concepts import ConceptDirection, ExtractionRecipe from obliteratus.concepts import cli, intervention, runs from obliteratus.concepts.pack import ConceptPack from obliteratus.concepts.validation import PackError, SCHEMA_PATH, validate_manifest from obliteratus.models import loader pytestmark = pytest.mark.cpu FIXTURES = Path(__file__).parent / "fixtures/concept_packs" LEGACY_RECIPE = { "method": "diff_means", "n_directions": 1, "pool_controls": "mean_of_means", "readout": {"position": "final_token", "suffix": ""}, "denoise": None, "layer_select": {"strategy": "preset"}, "intervention_layer": {"rule": "same"}, "use_chat_template": None, "collateral_margin": 0.03, } def parse(*options): parser = argparse.ArgumentParser() cli.add_parser(parser.add_subparsers(dest="command", required=True)) return parser.parse_args([ "concepts", "steer", "local/model", "direction.safetensors", "--prompt", "first prompt", "--output-dir", "run", *options, ]) @pytest.fixture def harness(tmp_path, monkeypatch): direction = ConceptDirection( directions={1: torch.tensor([3.0, 4.0])}, extraction_layers=[1], intervention_layers=[1], concept_id="user/synthetic", model_id="local/model", recipe=ExtractionRecipe(steering_scale="vector"), validation={"concept_id": "user/synthetic", "pack_hash": None, "verdict": "warn"}, ) path = tmp_path / "source.safetensors" direction.save(path) args = parse() args.direction = str(path) args.output_dir = str(tmp_path / "run") inputs = {"input_ids": torch.tensor([[1, 2]]), "attention_mask": torch.tensor([[1, 1]])} batch = Mock() batch.to.return_value = inputs tokenizer = Mock(pad_token_id=0) tokenizer.return_value = batch tokenizer.decode.return_value = "completion" model = Mock(device=torch.device("cpu")) input_device = Mock(return_value=torch.device("cpu")) monkeypatch.setattr("obliteratus.device.resolve_input_execution_device", input_device) model.generate.return_value = torch.tensor([[1, 2, 3, 4]]) handle = SimpleNamespace(model=model, tokenizer=tokenizer, cleanup=Mock(), snapshot=Mock(), source_model_revision="b" * 40) load = Mock(return_value=handle) monkeypatch.setattr(loader, "load_model", load) monkeypatch.setattr(runs, "tool_identity", lambda commit=None: { "version": "test", "commit": None, }) manager = Mock() manager.measurements.return_value = {"1": { "observed_tokens": 0, "effective_vector_norm": None, "residual_norm": None, "vector_residual_ratio": None, "undefined_ratio_count": 0, "nonfinite_count": 0, }} state = SimpleNamespace( args=args, direction=direction, handle=handle, load=load, batch=batch, inputs=inputs, manager=manager, input_device=input_device, ) def apply(model, direction, mode, **kwargs): state.run = intervention.InterventionRun( mode=mode, layers=[1], positions=kwargs["positions"], validation=intervention._validation(direction, None, kwargs["override_gate"]), manager=manager, parameters={ **kwargs, "scale": kwargs["scale"] or direction.recipe.steering_scale, }, ) return state.run state.apply = Mock(side_effect=apply) monkeypatch.setattr(intervention, "apply_concept_intervention", state.apply) return state def test_steer_parser_defaults_and_choices(): args = parse() assert args.scale is None assert (args.alpha, args.rho, args.layers, args.positions) == ( -1.0, 1.0, "intervention", "all_tokens", ) assert (args.device, args.dtype, args.max_new_tokens) == ("cpu", "float32", 120) for scale in ("residual_norm", "raw", "vector"): assert parse("--scale", scale).scale == scale assert parse("--positions", "final_token").positions == "final_token" @pytest.mark.parametrize("enabled", [False, True]) def test_steer_uses_explicit_snapshot_policy_and_cap(harness, enabled): harness.args.plan_snapshot = "disk" if enabled else "disabled" harness.args.plan_snapshot_max_gib = 256 cli._steer(harness.args) assert harness.load.call_args.kwargs["skip_snapshot"] is True if enabled: harness.handle.snapshot.assert_called_once_with(max_bytes=256 * 1024**3) else: harness.handle.snapshot.assert_not_called() harness.handle.cleanup.assert_called_once_with() @pytest.mark.parametrize("option,value", [ ("--scale", "normalized"), ("--positions", "mean_pool"), ("--dtype", "int8"), ]) def test_steer_parser_rejects_unknown_choices(option, value): with pytest.raises(SystemExit) as exc: parse(option, value) assert exc.value.code == 2 @pytest.mark.parametrize("missing", ["--prompt", "--output-dir"]) def test_steer_requires_prompt_and_output(missing): parser = argparse.ArgumentParser() cli.add_parser(parser.add_subparsers(dest="command", required=True)) options = ["concepts", "steer", "local/model", "direction.safetensors"] if missing != "--prompt": options.extend(["--prompt", "text"]) if missing != "--output-dir": options.extend(["--output-dir", "run"]) with pytest.raises(SystemExit) as exc: parser.parse_args(options) assert exc.value.code == 2 def test_steer_recipe_default_saves_run_and_generated_suffix(harness, capsys): harness.args.prompt.append("second prompt") harness.args.json = True cli.run(harness.args) result = json.loads(capsys.readouterr().out) assert result["scale"] == "vector" harness.load.assert_called_once_with( "local/model", device="cpu", dtype="float32", skip_snapshot=True, ) assert harness.apply.call_args.kwargs["scale"] is None harness.handle.model.eval.assert_called_once_with() assert harness.handle.tokenizer.call_args_list[0].args == ("first prompt",) assert harness.handle.tokenizer.call_args_list[1].args == ("second prompt",) for call in harness.handle.tokenizer.call_args_list: assert call.kwargs == {"return_tensors": "pt"} for call in harness.batch.to.call_args_list: assert call.args == (torch.device("cpu"),) for call in harness.handle.model.generate.call_args_list: assert call.kwargs["max_new_tokens"] == 120 assert call.kwargs["do_sample"] is False assert call.kwargs["num_beams"] == call.kwargs["num_return_sequences"] == 1 assert call.kwargs["pad_token_id"] == 0 assert call.kwargs["return_dict_in_generate"] is False for call in harness.handle.tokenizer.decode.call_args_list: assert torch.equal(call.args[0], torch.tensor([3, 4])) assert call.kwargs == {"skip_special_tokens": True} output = Path(harness.args.output_dir) assert json.loads((output / "generations.json").read_text()) == result["generations"] == [ {"prompt": "first prompt", "generation": "completion"}, {"prompt": "second prompt", "generation": "completion"}, ] record = runs.load_run(output) assert record["intervention"]["parameters"]["scale"] == "vector" assert record["intervention"]["steering"] == harness.run.steering_metadata() assert record["intervention"]["runtime"]["model"] == { "id": "local/model", "revision": "b" * 40, } assert record["evaluation"]["collateral"] == {"status": "not tested"} restored = ConceptDirection.load(output / "direction.safetensors") assert restored.recipe.steering_scale == "vector" assert torch.equal(restored.directions[1], harness.direction.directions[1]) harness.manager.remove.assert_called_once_with() harness.handle.cleanup.assert_called_once_with() def test_steer_routes_tokens_to_execution_device_instead_of_model_storage(harness): harness.handle.model.device = torch.device("meta") cli._steer(harness.args) harness.input_device.assert_called_once_with(harness.handle.model) harness.batch.to.assert_called_once_with(torch.device("cpu")) def test_steer_records_runtime_model_without_relabelling_extraction(harness): harness.args.model_id = "local/another-model" cli._steer(harness.args) record = runs.load_run(harness.args.output_dir) assert record["model"]["id"] == "local/model" assert record["intervention"]["runtime"]["model"]["id"] == "local/another-model" assert ConceptDirection.load(Path(harness.args.output_dir) / "direction.safetensors").model_id == ( "local/model" ) @pytest.mark.parametrize("layers,expected", [ ("2,4", [2, 4]), ("extraction", "extraction"), ("all", "all"), ("band(2,4)", "band(2,4)"), ]) def test_steer_explicit_options_override_recipe_and_preserve_gate(harness, layers, expected): harness.direction.validation["verdict"] = "fail" harness.direction.save(harness.args.direction) harness.args.scale = "raw" harness.args.alpha = 8.5 harness.args.rho = 0.25 harness.args.layers = layers harness.args.positions = "final_token" harness.args.override_gate = "exploratory dose calibration" harness.args.max_new_tokens = 7 result = cli._steer(harness.args) assert result["scale"] == "raw" assert harness.apply.call_args.args[0] is harness.handle.model assert harness.apply.call_args.args[2] == "steer" assert harness.apply.call_args.kwargs == { "scale": "raw", "alpha": 8.5, "rho": 0.25, "layers": expected, "positions": "final_token", "override_gate": "exploratory dose calibration", } assert harness.handle.model.generate.call_args.kwargs["max_new_tokens"] == 7 record = runs.load_run(harness.args.output_dir) assert record["validation"]["verdict"] == "fail" assert record["validation"]["override_gate"] == "exploratory dose calibration" assert record["recipe"]["steering_scale"] == "vector" assert record["intervention"]["parameters"]["scale"] == "raw" @pytest.mark.parametrize("field,value", [ ("alpha", float("nan")), ("alpha", float("inf")), ("rho", -1), ("rho", float("nan")), ("rho", float("inf")), ("max_new_tokens", 0), ("layers", "not-a-selector"), ("layers", "band(foo)"), ("layers", "band(4,2)"), ("layers", "1,1"), ("layers", "-1"), ("layers", ""), ("override_gate", " "), ]) def test_invalid_options_fail_before_loading(harness, field, value): setattr(harness.args, field, value) with pytest.raises(ValueError): cli._steer(harness.args) harness.load.assert_not_called() harness.apply.assert_not_called() assert not Path(harness.args.output_dir).exists() def test_failed_gate_is_reported_before_loading(harness, capsys): from obliteratus.cli import main harness.direction.validation["verdict"] = "fail" harness.direction.save(harness.args.direction) with pytest.raises(SystemExit) as exc: main([ "concepts", "steer", "local/model", harness.args.direction, "--prompt", "first prompt", "--output-dir", harness.args.output_dir, "--json", ]) assert exc.value.code == 3 assert "passing gate or recorded override_gate" in capsys.readouterr().err harness.load.assert_not_called() @pytest.mark.parametrize("failure", ["generate", "save", "apply"]) def test_steer_cleans_up_on_failure(harness, monkeypatch, failure): if failure == "generate": harness.handle.model.generate.side_effect = RuntimeError("generation failed") elif failure == "save": monkeypatch.setattr(runs, "save_concept_run", Mock(side_effect=RuntimeError("save failed"))) else: harness.apply.side_effect = RuntimeError("apply failed") with pytest.raises(RuntimeError, match="failed"): cli._steer(harness.args) harness.handle.cleanup.assert_called_once_with() if failure != "apply": harness.manager.remove.assert_called_once_with() assert not Path(harness.args.output_dir).exists() @pytest.mark.parametrize("scale", ["residual_norm", "raw", "vector"]) def test_pack_schema_accepts_steering_scales(scale): pack = ConceptPack.load(FIXTURES / "valid_minimal") manifest = deepcopy(pack.manifest) manifest["recipe"]["steering_scale"] = scale manifest["recipe"]["collateral_margin"] = 0.07 validate_manifest(manifest) Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(manifest) files = dict(pack.files) files["manifest.yaml"] = yaml.safe_dump(manifest).encode() recipe = ConceptPack(files).to_target().recipe assert recipe.steering_scale == scale assert recipe.collateral_margin == 0.07 assert ExtractionRecipe(**recipe.to_dict()) == recipe @pytest.mark.parametrize("scale", ["unit", "", None, 1, True, [], {}]) def test_pack_schema_rejects_invalid_steering_scales(scale): manifest = deepcopy(ConceptPack.load(FIXTURES / "valid_minimal").manifest) manifest["recipe"]["steering_scale"] = scale with pytest.raises(PackError): validate_manifest(manifest) with pytest.raises(ValidationError): Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(manifest) with pytest.raises(ValueError, match="steering_scale"): ExtractionRecipe(steering_scale=scale) def test_omitted_and_explicit_default_keep_legacy_recipe_serialization(): recipe = ConceptPack.load(FIXTURES / "valid_minimal").to_target().recipe assert recipe.steering_scale == "residual_norm" assert recipe.to_dict() == LEGACY_RECIPE assert ExtractionRecipe(steering_scale="residual_norm").to_dict() == LEGACY_RECIPE @pytest.mark.parametrize("scale", ["residual_norm", "raw", "vector"]) @pytest.mark.parametrize("policy", [None, {"bos": "require"}]) @pytest.mark.parametrize("pin", [None, {"fixture/model": 0}]) def test_recipe_roundtrip_keeps_margin_scale_and_optional_token_policy(scale, policy, pin): recipe = ExtractionRecipe( steering_scale=scale, collateral_margin=0.07, extraction_layer=pin, layer_select={"strategy": "cv_auc", "group_by": "item"}, entry_point={"special_token_policy": policy}, ) serialized = recipe.to_dict() assert serialized["collateral_margin"] == 0.07 if scale == "residual_norm": assert "steering_scale" not in serialized else: assert serialized["steering_scale"] == scale if policy is None: assert "special_token_policy" not in serialized["entry_point"] else: assert serialized["entry_point"]["special_token_policy"] == policy if pin is None: assert "extraction_layer" not in serialized else: assert serialized["extraction_layer"] == pin assert ExtractionRecipe(**serialized) == recipe assert ExtractionRecipe(**serialized).group_by == "item" @pytest.mark.parametrize("options,field", [ ({"steering_scale": "raw", "collateral_margin": -0.01}, "collateral_margin"), ({"steering_scale": "invalid", "collateral_margin": 0.07}, "steering_scale"), ]) def test_recipe_validates_margin_and_scale_together(options, field): with pytest.raises(ValueError, match=field): ExtractionRecipe(**options)