Files
OBLITERATUS/tests/test_analysis_profiling.py
Joseph Magly 545ed4281d fix(ci): clear gitleaks false positives without suppression
All 22 supply-chain secret-scan hits were false positives: a public
Llama-3.2-1B commit id under a "tokenizer_revision" key, and SHA-256 digests of
tokenizer fixture files keyed by filename. numeric_geometry_spec uses one
module constant for the commit (values unchanged). The analysis-first evidence
JSONs record hashes as ordered {"path","sha256"} lists (all 63 values
preserved, verified), and the two writers emit the same shape. CI scans the
clean checkout before uv sync builds into the workspace. No allowlist, ignore
file or inline allow comment is added.

Refs #679
2026-11-25 15:13:02 -05:00

286 lines
13 KiB
Python

"""Deterministic local cost contracts and offline observational baseline wiring."""
import json
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
from obliteratus.analysis.profiling import LocalCostProfile, encode_profile, process_resources
from obliteratus.concepts.baseline import run_baseline
from tests.test_concept_baseline import synthetic_pack as synthetic_pack
pytestmark = pytest.mark.cpu
class Clock:
def __init__(self):
self.now = 0.0
def __call__(self):
self.now += 1.0
return self.now
def profiler(monkeypatch):
monkeypatch.setattr("obliteratus.analysis.profiling._runtime", lambda: {"fixture": True})
samples = iter([
{"user_cpu_seconds": 2.0, "system_cpu_seconds": 1.0, "peak_rss_bytes": 4096},
{"user_cpu_seconds": 5.0, "system_cpu_seconds": 1.5, "peak_rss_bytes": 8192},
])
return LocalCostProfile(clock=Clock(), resources=lambda: next(samples))
def test_injected_clock_resources_and_unknown_counters(tmp_path, monkeypatch):
profile = profiler(monkeypatch)
path = tmp_path / "profile.json"
with profile.invocation(path, model_id="local", pack_id="synthetic", settings={}):
with profile.stage("probe"):
pass
report = json.loads(path.read_text())
assert report["schema_version"] == 1
assert report["wall_seconds"] == 6
assert report["stages"] == [{"name": "probe", "wall_seconds": 1.0,
"status": "success", "error_type": None}]
assert report["cpu_seconds"] == {"user_cpu_seconds": 3, "system_cpu_seconds": .5}
assert report["resources_after"]["peak_rss_bytes"] == 8192
assert report["profiling_overhead"]["measured_bookkeeping_wall_seconds"] == 3
assert report["profiling_overhead"]["total_wall_seconds"] is None
assert set(report["counters"].values()) == {None}
assert set(report["cache"].values()) == {None}
assert report["energy_joules"] is report["gpu_seconds"] is report["dollar_cost"] is None
assert "unsynchronized" in report["timing_scope"]
assert "no children" in report["cpu_scope"]
with pytest.raises(ValueError, match="single-use"):
with profile.invocation(path, model_id="", pack_id="", settings={}):
pass
@pytest.mark.parametrize("value", [float("nan"), float("inf"), -float("inf")])
def test_nonfinite_rejected(value):
with pytest.raises(ValueError, match="JSON compliant"):
encode_profile({"schema_version": 1, "nested": [{"value": value}]})
@pytest.mark.parametrize("version", [True, "1", 2, None])
def test_unknown_schema_rejected(version):
with pytest.raises(ValueError, match="schema_version"):
encode_profile({"schema_version": version})
def test_process_resource_units_and_scope(monkeypatch):
resource = pytest.importorskip("resource")
def sample(scope):
assert scope == resource.RUSAGE_SELF
return SimpleNamespace(ru_utime=1.25, ru_stime=.25, ru_maxrss=42)
monkeypatch.setattr(resource, "getrusage", sample)
for system, expected in [("Linux", 42 * 1024), ("Darwin", 42), ("Other", None)]:
monkeypatch.setattr("platform.system", lambda: system)
result = process_resources()
assert result == {"user_cpu_seconds": 1.25, "system_cpu_seconds": .25,
"peak_rss_bytes": expected}
monkeypatch.setattr(resource, "getrusage", Mock(side_effect=OSError))
assert set(process_resources().values()) == {None}
def fake_baseline(monkeypatch, failure=None):
target = SimpleNamespace(recipe=SimpleNamespace(to_dict=lambda: {"method": "diff_means"}))
pack = SimpleNamespace(fetch=Mock(), to_target=Mock(return_value=target), sha256="a" * 64)
pipeline = Mock()
pipeline.handle.source_model_revision = "b" * 40
pipeline.concept_direction = Mock()
for name in ("_summon", "_probe", "_distill", "_validate_concept_gate",
"cleanup_failed_run"):
if failure == name:
getattr(pipeline, name).side_effect = RuntimeError("private prompt token fragment")
if failure == "save":
pipeline.concept_direction.save.side_effect = OSError("private credential")
if failure == "fetch":
pack.fetch.side_effect = ValueError("private source URL")
monkeypatch.setattr("obliteratus.concepts.baseline._resolve", lambda value: pack)
factory = Mock(return_value=pipeline)
monkeypatch.setattr("obliteratus.concepts.baseline.AbliterationPipeline", factory)
return pack, pipeline, factory
@pytest.mark.parametrize("failure,stage", [
(None, None), ("fetch", "resolve_fetch"), ("_summon", "load"),
("_probe", "probe"), ("_distill", "distill"),
("_validate_concept_gate", "gate"), ("save", "save"),
("cleanup_failed_run", "cleanup"),
])
def test_baseline_stage_errors_cleanup_and_privacy(tmp_path, monkeypatch, failure, stage):
pack, pipeline, factory = fake_baseline(monkeypatch, failure)
output = tmp_path / "out"
kwargs = {"profile": profiler(monkeypatch), "seed": 17, "device": "cpu", "dtype": "float32"}
if failure:
with pytest.raises((RuntimeError, ValueError, OSError)):
run_baseline("private URL", "private model URL", output, **kwargs)
else:
assert run_baseline("private URL", "private model URL", output, **kwargs) == output
text = (output / "cost-profile.json").read_text()
assert "private" not in text
report = json.loads(text)
assert report["status"] == ("failure" if failure else "success")
assert report["settings"]["seed"] == 17
if stage:
assert next(row for row in report["stages"] if row["name"] == stage)["status"] == "failure"
if failure == "fetch":
factory.assert_not_called()
pipeline.cleanup_failed_run.assert_not_called()
else:
pipeline.cleanup_failed_run.assert_called_once()
assert factory.call_args.kwargs["gate_mode"] == "report"
assert factory.call_args.kwargs["device"] == "cpu"
assert report["identities"]["pack_sha256"] == pack.sha256
if failure is None:
assert [row["name"] for row in report["stages"]] == [
"resolve_fetch", "setup", "load", "probe", "distill", "gate", "save", "cleanup"]
pipeline._excise.assert_not_called()
pipeline._rebirth.assert_not_called()
def test_disabled_does_not_construct_profiler(tmp_path, monkeypatch):
fake_baseline(monkeypatch)
monkeypatch.setattr("obliteratus.analysis.profiling.LocalCostProfile",
Mock(side_effect=AssertionError("profiling is disabled")))
run_baseline("pack", "model", tmp_path)
assert not (tmp_path / "cost-profile.json").exists()
def test_sidecar_failure_preserves_workflow_error(tmp_path, monkeypatch):
profile = profiler(monkeypatch)
monkeypatch.setattr(profile, "_finish", Mock(side_effect=OSError("private")))
with pytest.warns(RuntimeWarning, match="preserving workflow error"):
with pytest.raises(ValueError, match="original"):
with profile.invocation(tmp_path, model_id="", pack_id="", settings={}):
raise ValueError("original")
@pytest.mark.parametrize("flags,expected", [
([], {}), (["--profile"], {"profile": True, "profile_output": None}),
(["--profile-output", "selected.json"], {"profile": True, "profile_output": "selected.json"}),
])
def test_cli_profile_propagation(tmp_path, monkeypatch, capsys, flags, expected):
from obliteratus.cli import main
runner = Mock(return_value=tmp_path)
monkeypatch.setattr("obliteratus.concepts.baseline.run_baseline", runner)
main(["concepts", "baseline", "pack", "model", "--output-dir", str(tmp_path), *flags])
actual = runner.call_args.kwargs
assert {key: actual[key] for key in ("profile", "profile_output") if key in actual} == expected
assert "report" in capsys.readouterr().out
@pytest.mark.integration
@pytest.mark.usefixtures("tiny_model_runtime")
def test_offline_reference_baseline_profile(tmp_path, synthetic_pack, monkeypatch):
import torch
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
from obliteratus.abliterate import AbliterationPipeline
previous = torch.get_num_threads()
torch.set_num_threads(1)
source, payload = synthetic_pack
model = build_tiny_offline_model(tmp_path / "model")
def forbidden(*args, **kwargs):
pytest.fail("observational baseline attempted mutation")
monkeypatch.setattr(AbliterationPipeline, "_excise", forbidden)
monkeypatch.setattr(AbliterationPipeline, "_rebirth", forbidden)
try:
output = run_baseline(source, model, tmp_path / "out", profile=True,
cache_dir=tmp_path / "cache", max_seq_length=8,
fetcher=lambda url, limit: payload.read_bytes())
finally:
torch.set_num_threads(previous)
report = json.loads((output / "cost-profile.json").read_text())
assert report["status"] == "success"
assert report["runtime"]["tool"]["obliteratus_version"]
assert report["runtime"]["packages"]["torch"]
assert all(row["wall_seconds"] >= 0 for row in report["stages"])
assert report["wall_seconds"] >= sum(row["wall_seconds"] for row in report["stages"])
assert report["profiling_overhead"]["measured_bookkeeping_wall_seconds"] >= 0
assert json.loads((output / "validation.json").read_text())["mode"] == "report"
@pytest.mark.parametrize("name", ["direction.json", "direction.safetensors", "validation.json"])
def test_profile_cannot_overwrite_baseline_artifacts(tmp_path, monkeypatch, name):
_, _, factory = fake_baseline(monkeypatch)
with pytest.raises(ValueError, match="overwrite"):
run_baseline("pack", "model", tmp_path, profile_output=tmp_path / name)
factory.assert_not_called()
def test_profile_write_failure_is_reported_on_success(tmp_path, monkeypatch):
profile = profiler(monkeypatch)
monkeypatch.setattr(profile, "_finish", Mock(side_effect=OSError("write failed")))
with pytest.raises(OSError, match="write failed"):
with profile.invocation(tmp_path, model_id="", pack_id="", settings={}):
pass
def test_cli_failure_writes_sidecar(tmp_path, monkeypatch):
from obliteratus.cli import main
fake_baseline(monkeypatch, "fetch")
path = tmp_path / "selected.json"
with pytest.raises(SystemExit) as exc:
main(["concepts", "baseline", "pack", "model", "--output-dir", str(tmp_path / "out"),
"--profile-output", str(path)])
assert exc.value.code == 4
report = json.loads(path.read_text())
assert report["status"] == "failure"
assert report["stages"][0]["error_type"] == "ValueError"
def test_setup_failure_recorded(tmp_path, monkeypatch):
_, _, factory = fake_baseline(monkeypatch)
factory.side_effect = RuntimeError("setup failed")
with pytest.raises(RuntimeError, match="setup failed"):
run_baseline("pack", "model", tmp_path, profile=True)
report = json.loads((tmp_path / "cost-profile.json").read_text())
assert report["stages"][-1]["name"] == "setup"
assert report["stages"][-1]["status"] == "failure"
def test_primary_and_cleanup_errors_both_retained(tmp_path, monkeypatch):
_, pipeline, _ = fake_baseline(monkeypatch, "_probe")
pipeline.cleanup_failed_run.side_effect = OSError("cleanup failed")
with pytest.raises(OSError, match="cleanup failed"):
run_baseline("pack", "model", tmp_path, profile=True)
report = json.loads((tmp_path / "cost-profile.json").read_text())
assert [(row["name"], row["error_type"]) for row in report["stages"]
if row["status"] == "failure"] == [("probe", "RuntimeError"), ("cleanup", "OSError")]
def test_unavailable_resources_remain_null(tmp_path, monkeypatch):
monkeypatch.setattr("obliteratus.analysis.profiling._runtime", lambda: {})
profile = LocalCostProfile(clock=Clock(), resources=lambda: {
"user_cpu_seconds": None, "system_cpu_seconds": None, "peak_rss_bytes": None})
with profile.invocation(tmp_path / "profile.json", model_id="", pack_id="", settings={}):
pass
assert set(profile.report["cpu_seconds"].values()) == {None}
assert profile.report["resources_after"]["peak_rss_bytes"] is None
def test_baseline_example_records_fixture_hashes_as_ordered_path_records(tmp_path):
"""The example writes [{"path", "sha256"}] in sorted path order, not a name-keyed map (#679)."""
import hashlib
import importlib.util
from pathlib import Path
source = Path(__file__).parents[1] / "examples" / "analysis_first_baseline.py"
spec = importlib.util.spec_from_file_location("analysis_first_baseline_example", source)
example = importlib.util.module_from_spec(spec)
spec.loader.exec_module(example)
files = {"model/tokenizer.json": b"t", "model/config.json": b"c", "pack/manifest.yaml": b"m",
"pack/controls/neutral.jsonl": b"n", "other/ignored.txt": b"x"}
for name, payload in files.items():
(tmp_path / name).parent.mkdir(parents=True, exist_ok=True)
(tmp_path / name).write_bytes(payload)
entries = example.file_sha256_entries(tmp_path, ["model", "pack"])
assert entries == [
{"path": name, "sha256": hashlib.sha256(files[name]).hexdigest()}
for name in ["model/config.json", "model/tokenizer.json",
"pack/controls/neutral.jsonl", "pack/manifest.yaml"]
]