"""Shared pytest fixtures for the Obliteratus test suite.""" from __future__ import annotations import gc import os import socket import sys import threading import time from unittest.mock import MagicMock import pytest _EXTERNAL_MARKERS = ("network", "download", "remote") _EVIDENCE_MARKERS = ( "cpu", "integration", "slow", "gpu", "mps", "mlx", "network", "download", "remote", "operator_ui", ) _ORIGINAL_THREAD_START = threading.Thread.start # Re-entrant: a signal handler or finalizer may start a thread mid-start. _THREAD_START_LOCK = threading.RLock() def _reset_thread_start_lock_in_child(): # A fork can happen while another thread holds the lock; that owner does not # exist in the child, so the child needs a fresh lock. global _THREAD_START_LOCK _THREAD_START_LOCK = threading.RLock() if hasattr(os, "register_at_fork"): os.register_at_fork(after_in_child=_reset_thread_start_lock_in_child) # Bound now: tests may patch time.monotonic or threading.Event globally. _MONOTONIC = time.monotonic _EVENT = threading.Event _TRACE_INSTALL_TIMEOUT_SECONDS = 30.0 def _start_after_trace_installed(self): """Return from ``start`` only once the new thread runs Python code. CPython before 3.12 guards ``sys.settrace`` with a process-wide reentrancy flag and calls audit hooks while it is set. When coverage traces threads (``threading.settrace``) and a Python audit hook is installed (filelock adds one), the GIL can switch inside that window; a second thread starting then fails in ``threading._bootstrap_inner`` with "Cannot install a trace function while another trace function is being installed". Waiting for the new thread to reach ``run`` serialises the trace installation. """ entered = _EVENT() run = self.run def run_after_trace_installed(): entered.set() return run() self.run = run_after_trace_installed # One process-wide lock: concurrent starters would otherwise still overlap. with _THREAD_START_LOCK: _ORIGINAL_THREAD_START(self) deadline = _MONOTONIC() + _TRACE_INSTALL_TIMEOUT_SECONDS while not entered.wait(0.01): if not self.is_alive() and not entered.is_set(): raise RuntimeError(f"{self.name} exited before running its target") if _MONOTONIC() > deadline: # The thread is alive but blocked (for example on a lock this # caller holds): fall back to plain start() semantics. return def pytest_configure(config): if sys.version_info < (3, 12): threading.Thread.start = _start_after_trace_installed def pytest_unconfigure(config): threading.Thread.start = _ORIGINAL_THREAD_START def pytest_collection_modifyitems(config, items): """Deselect env-opt-in modules, then attach test-layer markers to JUnit timing evidence. A test module that sets ``OPT_IN_ENV = "NAME"`` runs only when ``NAME=1``. Otherwise its tests are deselected, not skipped: no default, pull-request, release or bare pytest selection starts them, and release collection evidence records no collection-time skip. """ deselected = [item for item in items if (name := getattr(getattr(item, "module", None), "OPT_IN_ENV", None)) and os.environ.get(name) != "1"] if deselected: dropped = {id(item) for item in deselected} items[:] = [item for item in items if id(item) not in dropped] config.hook.pytest_deselected(items=deselected) for item in items: markers = sorted( name for name in _EVIDENCE_MARKERS if item.get_closest_marker(name) is not None ) item.user_properties.append(("duration_markers", ",".join(markers) or "unmarked")) def _can_own_gc_freeze(): """Never unfreeze another caller's objects, including embedded pytest runs.""" # CPython 3.12's update_refs() automatically freezes tracked immortals. # Account for actual base/MRO tuple identities, not a guessed startup count. # Include/object.h defines these immortal refcounts; Modules/gcmodule.c # moves them back to the permanent generation on collection after unfreeze. immortal_tuples = {} if sys.implementation.name == "cpython" and sys.version_info[:2] == (3, 12): immortal_refcount = 0xFFFFFFFF if sys.maxsize > 2**32 else 0x3FFFFFFF classes, seen = [object], set() for cls in classes: if id(cls) in seen: continue seen.add(id(cls)) classes.extend(type.__subclasses__(cls)) for attribute in ("__bases__", "__mro__"): value = type.__dict__[attribute].__get__(cls) if gc.is_tracked(value) and sys.getrefcount(value) == immortal_refcount: immortal_tuples[id(value)] = value # Full collection puts every retained, tracked immortal in the permanent # generation. Any unexplained frozen object makes ownership unsafe. gc.collect() return ( gc.get_freeze_count() == len(immortal_tuples) and all(gc.is_tracked(value) for value in immortal_tuples.values()) # Also verify membership: tracked objects absent from get_objects() # are frozen. Equal counts alone must never establish ownership. and not any(id(value) in immortal_tuples for value in gc.get_objects()) ) def pytest_collection_finish(session): """Freeze only the collected baseline, before any test fixture can run.""" if session.config.option.collectonly or session.testsfailed: return if not any("tiny_model_runtime" in item.fixturenames for item in session.items): return if not _can_own_gc_freeze(): return def restore_gc(): gc.unfreeze() gc.collect() # Reclaims cycles and restores CPython's automatic immortals. gc.freeze() session.config.add_cleanup(restore_gc) @pytest.fixture(autouse=True) def offline_test_environment(request, monkeypatch): """Fail accidental network access and force offline Hugging Face behavior.""" if any(request.node.get_closest_marker(name) for name in _EXTERNAL_MARKERS): return monkeypatch.setenv("HF_DATASETS_OFFLINE", "1") monkeypatch.setenv("HF_HUB_DISABLE_TELEMETRY", "1") monkeypatch.setenv("HF_HUB_OFFLINE", "1") monkeypatch.setenv("TRANSFORMERS_OFFLINE", "1") def reject_network(*_args, **_kwargs): raise RuntimeError( "unmarked tests may not access the network; add an explicit " "network, download, or remote marker", ) monkeypatch.setattr(socket, "create_connection", reject_network) monkeypatch.setattr(socket.socket, "connect", reject_network) # --------------------------------------------------------------------------- # Fixtures # --------------------------------------------------------------------------- @pytest.fixture def mock_model(): """A minimal mock transformer model. Provides: - model.config with config.num_hidden_layers = 4 - model.named_parameters() returning fake weight tensors """ import torch model = MagicMock() # Config with num_hidden_layers config = MagicMock() config.num_hidden_layers = 4 model.config = config # named_parameters returns fake weight tensors across 4 layers fake_params = [] for layer_idx in range(4): weight = torch.randn(768, 768) fake_params.append((f"model.layers.{layer_idx}.self_attn.q_proj.weight", weight)) fake_params.append((f"model.layers.{layer_idx}.self_attn.v_proj.weight", weight)) fake_params.append((f"model.layers.{layer_idx}.mlp.gate_proj.weight", weight)) model.named_parameters.return_value = fake_params return model @pytest.fixture def mock_tokenizer(): """A minimal mock tokenizer with encode, decode, and apply_chat_template.""" tokenizer = MagicMock() tokenizer.encode.return_value = [1, 2, 3, 4, 5] tokenizer.decode.return_value = "Hello, this is a decoded string." tokenizer.apply_chat_template.return_value = [1, 2, 3, 4, 5, 6, 7] tokenizer.pad_token = "" tokenizer.eos_token = "" return tokenizer @pytest.fixture def refusal_direction(): """A normalized random torch tensor of shape (768,).""" import torch t = torch.randn(768) return t / t.norm() @pytest.fixture def activation_pair(): """A tuple of (harmful_activations, harmless_activations) as random tensors of shape (10, 768).""" import torch harmful_activations = torch.randn(10, 768) harmless_activations = torch.randn(10, 768) return (harmful_activations, harmless_activations) @pytest.fixture def tmp_output_dir(tmp_path): """A clean temporary output directory for test artifacts.""" output_dir = tmp_path / "test_output" output_dir.mkdir() return output_dir @pytest.fixture def tiny_model_runtime(): """Bound the per-step overhead of full pipeline runs on tiny CPU models. Tiny fixture models make every forward/generate step dominated by fixed costs rather than arithmetic: intra-op thread fan-out on shared runners, and the pipeline's ``gc.collect()`` between steps, which otherwise re-scans every long-lived object the test session has already imported. The collection hook freezes the baseline once, with session cleanup. Per-test collection still reclaims cycles from prior tests, while objects created by tests and fixtures always stay in the collected generations. This fixture changes only the intra-op thread count and restores it even if setup, execution, or a dependent fixture fails. """ import torch threads = torch.get_num_threads() try: torch.set_num_threads(1) gc.collect() yield finally: torch.set_num_threads(threads)