"""``experiments select-examples``: Python API and CLI on a captured planted labeled set. Uses the planted model and dataset pack from ``tests/test_labeled_activations.py``: roles ``fit`` (selection pool) and ``select`` (independent evaluation) are captured once, verified on reuse, and the report is written next to the run or into a chosen directory. Synthetic fixture: not evidence for real models. """ from __future__ import annotations import json from pathlib import Path import pytest from obliteratus.analysis.example_selection_study import run_selection_study from obliteratus.cli import main from obliteratus.concepts.pack import ConceptPack from obliteratus.experiments import SpecError from obliteratus.experiments.select_examples import resolve_and_run, run_select_examples from tests.test_labeled_activations import ( LEGACY, PlantedModel, WordTokenizer, build_pack, capture, load, ) pytestmark = pytest.mark.cpu CONFIG = {"budgets": [6, 12], "seeds": 3} MODEL = PlantedModel() # one instance: its head is randomly initialised def factory(section): return type("Handle", (), {"model": MODEL, "tokenizer": WordTokenizer(), "cleanup": lambda self: None})() def test_python_api_captures_once_reuses_and_writes_a_text_free_report(tmp_path): pack = build_pack(tmp_path / "pack") model = PlantedModel() summary = run_select_examples(pack, model, WordTokenizer(), 0, tmp_path / "api", model_id="local/planted", config=CONFIG) assert not summary["reused_labeled_set"] and summary["recommendation"] in { "no improvement", "improvement"} report = json.loads((tmp_path / "api" / "layer.0.json").read_text()) assert report["schema"] == "obliteratus.example-selection/1" assert report["evaluation"]["source"] == "role:select" assert report["pool"] == {**report["pool"], "rows": 60, "positive": 20, "negative": 40, "lexical_features": True} assert report["labeled_set"]["roles"] == ["fit", "select"] capture_cost = report["host_measurements"]["capture"] assert capture_cost["measured"] and capture_cost["rows"] == 280 assert not capture_cost["reused_labeled_set"] assert report["host_measurements"]["lexical_feature_seconds"] >= 0 assert "alpha sample" not in json.dumps(report) again = run_select_examples(pack, model, WordTokenizer(), 0, tmp_path / "api", model_id="local/planted", config=CONFIG) assert again["reused_labeled_set"] and again["report_sha256"] == summary["report_sha256"] def test_cli_and_run_directory_entry_points(tmp_path, capsys, monkeypatch): from tests.test_observation_runs import make_spec build_pack(tmp_path / "pack") (tmp_path / "config.json").write_text(json.dumps(CONFIG)) monkeypatch.setattr("obliteratus.experiments.inputs.default_model_factory", factory) argv = ["experiments", "select-examples", "--pack", str(tmp_path / "pack"), "--model", "local/planted", "--layer", "0", "--config", str(tmp_path / "config.json"), "--out", str(tmp_path / "cli"), "--no-timings"] main(argv) printed = json.loads(capsys.readouterr().out) first = (tmp_path / "cli" / "layer.0.json").read_bytes() assert printed["report"].endswith("layer.0.json") and not printed["reused_labeled_set"] assert json.loads(first)["host_measurements"] == {"recorded": False} main(argv) assert json.loads(capsys.readouterr().out)["reused_labeled_set"] assert (tmp_path / "cli" / "layer.0.json").read_bytes() == first # identical for fixed seeds with pytest.raises(SystemExit) as refused: main(["experiments", "select-examples", "--pack", str(tmp_path / "pack"), "--layer", "0"]) assert refused.value.code == 4 and "--model" in capsys.readouterr().err run = tmp_path / "run" run.mkdir() (run / "spec.json").write_text(json.dumps(make_spec(tmp_path / "pack", capture=None))) summary = resolve_and_run(layer=0, run=run, config=CONFIG, model_factory=factory, pack_resolver=ConceptPack.load) assert summary["report"].endswith("example-selection/layer.0.json") with pytest.raises(SpecError, match="not both"): resolve_and_run(layer=0, run=run, pack="p") other = build_pack(tmp_path / "other", lambda layout: layout["positive.jsonl"][30].update( text="alpha sample 398")) with pytest.raises(SpecError, match="revision differs"): resolve_and_run(layer=0, run=run, model_factory=factory, pack_resolver=lambda _r: other) def test_legacy_pack_uses_a_holdout_fold_and_locked_test_sets_are_refused(tmp_path): summary = run_select_examples(ConceptPack.load(LEGACY), PlantedModel(), WordTokenizer(), 0, tmp_path / "legacy", model_id="local/planted", config={"budgets": [4], "seeds": 3}, timings=False) report = json.loads(Path(summary["report"]).read_text()) assert report["evaluation"]["source"].startswith("group-disjoint holdout fold 0 of 5") assert report["labeled_set"]["partitioned"] is False pack, model = build_pack(tmp_path / "pack"), PlantedModel() capture(model, pack, tmp_path / "test", roles=["fit", "test"], allow_locked_test=True) labeled = load(model, pack, tmp_path / "test", allow_locked_test=True) with pytest.raises(ValueError, match="locked test"): run_selection_study(labeled, "layer.0", config=CONFIG)