Files
OBLITERATUS/tests/test_telemetry_storage_sync.py
Joseph MaglyandClaude Opus 5.5 b45b8341a2 test(#304): cover runtime admission, checkpoint and Concept Lab edges on CPU
Add release-lane CPU tests for phase/load admission input contracts and
fail-closed observation paths, telemetry storage selection and Hub
sync/fetch fallbacks, checkpoint inspection and writer source races,
offload-surgery guards, Concept Lab callback wiring through a recording
Gradio stand-in, and chat-slot load edges. All accelerator, Hub, git and
filesystem races are injected deterministically.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 16:55:06 -04:00

314 lines
12 KiB
Python

"""Telemetry storage selection, Hub sync/fetch fallbacks and local record edges.
Every Hub and git interaction is replaced by an in-process fake; nothing touches
the network or the real telemetry directory.
"""
from __future__ import annotations
import hashlib
import json
import logging
from pathlib import Path
import subprocess
from types import SimpleNamespace
import pytest
import obliteratus.telemetry as telemetry
pytestmark = pytest.mark.cpu
@pytest.fixture
def local_file(tmp_path, monkeypatch):
path = tmp_path / "telemetry.jsonl"
monkeypatch.setattr(telemetry, "TELEMETRY_FILE", path)
monkeypatch.setattr(telemetry, "_schedule_hub_sync", lambda: None)
monkeypatch.setattr(telemetry, "_enabled", True)
return path
class _DataPath(type(Path())):
"""Path whose ``/data`` root always exists, independent of the host."""
def exists(self, *args, **kwargs):
return str(self) == "/data" or super().exists(*args, **kwargs)
@pytest.fixture
def spaces_storage(tmp_path, monkeypatch):
sleeps = []
monkeypatch.delenv("OBLITERATUS_DATA_DIR", raising=False)
monkeypatch.setenv("HOME", str(tmp_path / "home"))
monkeypatch.setattr(telemetry, "_ON_HF_SPACES", True)
monkeypatch.setattr(telemetry, "Path", _DataPath)
monkeypatch.setattr(telemetry, "time", SimpleNamespace(sleep=sleeps.append))
monkeypatch.setattr(telemetry, "tempfile", SimpleNamespace(gettempdir=lambda: str(tmp_path)))
return sleeps
def test_spaces_persistent_storage_is_used_when_immediately_writable(
spaces_storage, monkeypatch, caplog,
):
monkeypatch.setattr(telemetry, "_test_writable",
lambda d: str(d) == "/data/obliteratus")
with caplog.at_level(logging.INFO, logger=telemetry.logger.name):
assert str(telemetry._telemetry_dir()) == "/data/obliteratus"
assert spaces_storage == []
assert "HF persistent storage" in caplog.text
def test_unwritable_spaces_storage_retries_then_warns_about_ephemeral_fallback(
spaces_storage, monkeypatch, tmp_path, caplog,
):
attempts = []
monkeypatch.setattr(telemetry, "_test_writable", lambda d: attempts.append(str(d)) and False)
with caplog.at_level(logging.INFO, logger=telemetry.logger.name):
chosen = telemetry._telemetry_dir()
assert chosen == tmp_path / "obliteratus_telemetry" and chosen.is_dir()
assert attempts == ["/data/obliteratus"] * 3 + [str(tmp_path / "home/.obliteratus")]
assert spaces_storage == [1, 1] # Two waits between three attempts.
assert "is NOT writable" in caplog.text
assert "EPHEMERAL" in caplog.text
def test_mount_point_probe_failures_are_not_mounts():
def broken_stat():
raise OSError("permission denied")
path = SimpleNamespace(exists=lambda: True, stat=broken_stat)
assert telemetry._is_mount_point(path) is False
def test_legacy_enabled_alias_and_instance_slug(monkeypatch):
monkeypatch.setattr(telemetry, "_enabled", False)
assert telemetry.is_telemetry_enabled() is False
monkeypatch.setattr(telemetry, "_enabled", True)
assert telemetry.is_telemetry_enabled() is True
monkeypatch.setenv("SPACE_ID", "someone/private-space")
slug = telemetry._instance_slug()
expected = hashlib.sha256(b"someone/private-space").hexdigest()[:10]
assert slug == f"{expected}_{telemetry._SESSION_ID}"
assert "someone" not in slug
def test_capability_results_are_sanitized_or_marked_unavailable():
values, statuses = telemetry._normalize_quality_metrics({
"capability_results": {"math": 0.5, "api_token": "hf_secret"},
"refusal_rate": 2.0,
"unlisted": 1,
})
assert "unlisted" not in values
assert values["refusal_rate"] is None and statuses["refusal_rate"] == "unavailable"
assert values["capability_results"]["math"] == 0.5
assert "hf_secret" not in json.dumps(values)
values, statuses = telemetry._normalize_quality_metrics({"capability_results": [1, 2]})
assert values == {"capability_results": None}
assert statuses == {"capability_results": "unavailable"}
class _Api:
def __init__(self, *, ensure=True, upload_error=None, files=()):
self.ensure, self.upload_error, self.files = ensure, upload_error, list(files)
self.uploads = []
def __call__(self, token=None):
return self
def create_repo(self, **kwargs):
if not self.ensure:
raise PermissionError("cannot create")
def repo_info(self, **kwargs):
if not self.ensure:
raise PermissionError("no access")
def upload_file(self, **kwargs):
if self.upload_error:
raise self.upload_error
self.uploads.append(kwargs)
def list_repo_files(self, repo, repo_type):
return self.files
@pytest.fixture
def hub(monkeypatch, local_file):
monkeypatch.setattr(telemetry, "_TELEMETRY_REPO", "org/telemetry")
monkeypatch.setattr(telemetry, "_hub_repo_created", False)
telemetry._sync_in_progress.clear()
def install(api):
monkeypatch.setattr("huggingface_hub.HfApi", api)
return api
yield install
telemetry._sync_in_progress.clear()
def test_background_sync_stops_when_repo_cannot_be_ensured(hub, local_file, caplog):
local_file.write_text('{"a": 1}\n')
api = hub(_Api(ensure=False))
with caplog.at_level(logging.WARNING, logger=telemetry.logger.name):
telemetry._sync_to_hub_bg()
assert api.uploads == []
assert "repo_info also failed" in caplog.text
assert not telemetry._sync_in_progress.is_set()
def test_background_sync_upload_failure_is_logged_and_releases_guard(hub, local_file, caplog):
local_file.write_text('{"a": 1}\n')
hub(_Api(upload_error=ConnectionError("hub offline")))
with caplog.at_level(logging.WARNING, logger=telemetry.logger.name):
telemetry._sync_to_hub_bg()
assert "Hub sync failed: hub offline" in caplog.text
assert not telemetry._sync_in_progress.is_set()
def test_manual_push_reports_upload_failure(hub, local_file, caplog):
local_file.write_text('{"a": 1}\n')
hub(_Api(upload_error=ConnectionError("hub offline")))
with caplog.at_level(logging.WARNING, logger=telemetry.logger.name):
assert telemetry.push_to_hub() is False
assert "Failed to push telemetry: hub offline" in caplog.text
def test_hf_api_fetch_skips_unreadable_files(monkeypatch, tmp_path):
good = tmp_path / "good.jsonl"
good.write_text('{"n": 1}\n\nnot json\n{"n": 2}\n')
monkeypatch.setattr("huggingface_hub.HfApi", _Api(files=[
"README.md", "data/broken.jsonl", "data/good.jsonl",
]))
def download(repo, filepath, **kwargs):
if filepath.endswith("broken.jsonl"):
raise OSError("download interrupted")
return str(good)
monkeypatch.setattr("huggingface_hub.hf_hub_download", download)
assert telemetry._fetch_via_hf_api("org/telemetry", 10) == [{"n": 1}, {"n": 2}]
def test_git_clone_fetch_parses_jsonl_and_cleans_clone(monkeypatch, tmp_path):
import tempfile
monkeypatch.setattr(tempfile, "tempdir", str(tmp_path))
clones = []
def fake_run(command, **kwargs):
assert command[:4] == ["git", "clone", "--depth", "1"]
assert kwargs["env"]["GIT_LFS_SKIP_SMUDGE"] == "1"
clone = Path(command[-1])
clones.append(clone)
(clone / "data").mkdir(parents=True)
(clone / "data/a.jsonl").write_text('{"n": 1}\n\nbroken\n{"n": 2}\n{"n": 3}\n')
(clone / "data/b.jsonl").mkdir() # Unreadable as a file: skipped.
(clone / "data/c.jsonl").write_text('{"n": 4}\n')
return SimpleNamespace(returncode=0, stderr="")
monkeypatch.setattr(subprocess, "run", fake_run)
assert telemetry._fetch_via_git_clone("org/telemetry", 10) == [
{"n": 1}, {"n": 2}, {"n": 3}, {"n": 4},
]
assert telemetry._fetch_via_git_clone("org/telemetry", 2) == [{"n": 1}, {"n": 2}]
assert clones and not any(clone.exists() for clone in clones)
def test_restore_ignores_unreadable_local_lines(monkeypatch, local_file):
local_file.write_text('\n{"session_id": "s", "timestamp": "t"}\nnot json\n')
monkeypatch.setattr(telemetry, "_TELEMETRY_REPO", "org/telemetry")
monkeypatch.setattr(telemetry, "_restore_done", False)
monkeypatch.setattr(telemetry, "fetch_hub_records", lambda: [
{"session_id": "s", "timestamp": "t"}, {"session_id": "n", "timestamp": "t"},
])
assert telemetry.restore_from_hub() == 1
assert local_file.read_text().count('"session_id": "n"') == 1
def test_restore_survives_unreadable_local_file(monkeypatch, local_file):
local_file.mkdir() # exists() but cannot be opened as a file
monkeypatch.setattr(telemetry, "_TELEMETRY_REPO", "org/telemetry")
monkeypatch.setattr(telemetry, "_restore_done", False)
monkeypatch.setattr(telemetry, "fetch_hub_records", lambda: [])
assert telemetry.restore_from_hub() == 0
def test_disabled_logging_and_unwritable_store(monkeypatch, local_file):
record = telemetry.BenchmarkRecord(model_id="org/model", method="basic")
monkeypatch.setattr(telemetry, "_enabled", False)
assert telemetry.log_benchmark(record) is False
assert not local_file.exists()
monkeypatch.setattr(telemetry, "_enabled", True)
local_file.mkdir()
assert telemetry.log_benchmark(record) is False
def test_read_telemetry_edges(local_file):
assert telemetry.read_telemetry() == []
local_file.write_text('{"n": 1}\n\n{"n": 2}\n{"n": 3}\n')
assert telemetry.read_telemetry(max_records=2) == [{"n": 2}, {"n": 1}]
local_file.unlink()
local_file.mkdir()
assert telemetry.read_telemetry() == []
def test_leaderboard_is_empty_and_hub_errors_are_best_effort(monkeypatch, local_file):
def offline():
raise ConnectionError("offline")
monkeypatch.setattr(telemetry, "fetch_hub_records", offline)
assert telemetry.get_leaderboard_data() == []
def test_leaderboard_deduplicates_local_and_hub_records(monkeypatch, local_file):
run = {"session_id": "s", "timestamp": "t1", "model_id": "org/m", "method": "basic",
"refusal_rate": 0.25}
local_file.write_text(json.dumps(run) + "\n")
monkeypatch.setattr(telemetry, "fetch_hub_records", lambda: [run])
entry, = telemetry.get_leaderboard_data()
assert entry["runs"] == 1 and entry["best_refusal"] == 0.25
def test_safe_float_rejects_uncoercible_values():
assert telemetry._safe_float(object()) is None
assert telemetry._safe_float("1.5") == 1.5
def test_report_helpers_without_optional_pipeline_state():
pipeline = SimpleNamespace(activation_steering=True, _stage_durations={})
assert telemetry._extract_excise_details(pipeline) == {
"used_techniques": ["activation_steering"],
}
assert telemetry._extract_stage_durations(pipeline) is None
assert telemetry._extract_analysis_insights(SimpleNamespace()) == {}
report = telemetry.build_report(
architecture="llama", num_layers=1, num_heads=1, hidden_size=8, total_params=1,
method="basic",
)
assert report["method_config"] == {}
def test_report_write_sanitizes_and_failure_is_contained(monkeypatch, local_file):
telemetry._send_sync({"schema_version": 2, "note": "/home/alice/run", "x": float("nan"),
"hf_token": "hf_abcdefgh"})
written = json.loads(local_file.read_text())
assert written == {"note": "run", "schema_version": 2, "x": None}
local_file.unlink()
local_file.mkdir()
telemetry._send_sync({"schema_version": 2}) # Must not raise.
assert local_file.is_dir()
def test_cuda_probe_failures_report_no_gpu(monkeypatch):
import torch
def broken(*args):
raise RuntimeError("driver mismatch")
monkeypatch.setattr(torch.cuda, "is_available", lambda: True)
monkeypatch.setattr(torch.cuda, "get_device_name", broken)
monkeypatch.setattr(torch.cuda, "max_memory_allocated", broken)
assert telemetry._detect_gpu() == ("", 0.0)
assert telemetry._get_peak_vram() is None