from __future__ import annotations import atexit from datetime import datetime, timezone import json import threading import time from types import SimpleNamespace import weakref import pytest from obliteratus.gpu_lifecycle import ( AdmissionError, BrokerGrantReadError, GpuLifecyclePublisher, MemoryUsage, current_process_grant, measure_torch_memory, ) from obliteratus.phase_admission import ( DemandComponent, DeviceDemand, MemoryObservation, PhaseAdmissionError, admit_phase, ) @pytest.fixture(autouse=True) def isolated_publishers(monkeypatch): """Keep process-global discovery and heartbeat threads local to each test.""" publishers = weakref.WeakSet() monkeypatch.setattr("obliteratus.gpu_lifecycle._publishers", publishers) yield for publisher in list(publishers): publisher.release(reason="test_complete") atexit.unregister(publisher.release) def granted_publisher(tmp_path, monkeypatch, *, granted=100): publisher = GpuLifecyclePublisher(tmp_path, run_id="grant-run") monkeypatch.setattr(publisher, "_start_heartbeat", lambda: None) (tmp_path / "ack.json").write_text(json.dumps({ "schema_version": 1, "run_id": "grant-run", "request_event_id": "grant-run:1", "decision": "grant", "lease_id": "lease-grant", "granted_vram_bytes": granted, })) publisher.loading("org/model") return publisher def test_process_grant_is_absent_without_enabled_publisher(): assert current_process_grant() is None publisher = GpuLifecyclePublisher(None) publisher.loading("model") publisher.ready() assert current_process_grant() is None def test_process_grant_is_live_and_release_removes_ownership(tmp_path, monkeypatch): publisher = granted_publisher(tmp_path, monkeypatch, granted=1234) assert current_process_grant() == 1234 publisher.ready(MemoryUsage(reserved_bytes=100)) assert current_process_grant() == 1234 publisher.release() assert current_process_grant() == 0 @pytest.mark.parametrize("phase", [ "released", "intent_published", "admission_denied", "admission_lost", ]) def test_process_grant_fails_closed_without_live_ownership(tmp_path, monkeypatch, phase): publisher = granted_publisher(tmp_path, monkeypatch) publisher._phase = phase assert current_process_grant() == 0 def test_process_grant_is_zero_before_admission(tmp_path): publisher = GpuLifecyclePublisher(tmp_path) assert publisher.enabled assert current_process_grant() == 0 @pytest.mark.parametrize("lease,grant", [ (None, 100), ("lease", True), ("lease", "100"), ("lease", -1), ]) def test_process_grant_rejects_invalid_ownership(tmp_path, monkeypatch, lease, grant): publisher = granted_publisher(tmp_path, monkeypatch) publisher._lease_id = lease publisher._granted_vram_bytes = grant assert current_process_grant() == 0 def test_process_grant_rejects_ambiguous_publishers(tmp_path, monkeypatch): first_dir = tmp_path / "first" second_dir = tmp_path / "second" first_dir.mkdir() second_dir.mkdir() first = granted_publisher(first_dir, monkeypatch) second = granted_publisher(second_dir, monkeypatch) assert current_process_grant() == 0 first.release() assert current_process_grant() == 100 second.release() assert current_process_grant() == 0 def test_process_grant_allows_new_session_after_previous_release(tmp_path, monkeypatch): previous_dir = tmp_path / "previous" current_dir = tmp_path / "current" previous_dir.mkdir() current_dir.mkdir() previous = granted_publisher(previous_dir, monkeypatch, granted=10) previous.release() assert current_process_grant() == 0 current = granted_publisher(current_dir, monkeypatch, granted=200) assert current.enabled assert current_process_grant() == 200 def test_briefly_busy_grant_waits_then_phase_admission_succeeds(tmp_path, monkeypatch): publisher = granted_publisher(tmp_path, monkeypatch, granted=1000) state_lock = publisher._lock locked = threading.Event() reading = threading.Event() def hold_state_lock(): with state_lock: locked.set() reading.wait(timeout=2) def wait_for_state(*, timeout): assert 0 < timeout <= 2 reading.set() return state_lock.acquire(timeout=timeout) worker = threading.Thread(target=hold_state_lock) worker.start() try: assert locked.wait(timeout=1) # Signal only when admission actually tries to acquire the held lock; # the worker then completes its publication without timing-based sleeps. with monkeypatch.context() as patch: patch.setattr(publisher, "_lock", SimpleNamespace( acquire=wait_for_state, release=state_lock.release, )) report = admit_phase( "probe", {"cuda:0": DeviceDemand(activation_bytes=10)}, observer=lambda device: MemoryObservation(device, 1000, 1000), safety_reserve_bytes=0, ) assert report.available_bytes["cuda:0"] == 1000 assert current_process_grant() == 1000 finally: reading.set() worker.join(timeout=2) assert not worker.is_alive() def test_process_grant_lock_timeout_is_distinct_from_zero_grant(tmp_path, monkeypatch): publisher = granted_publisher(tmp_path, monkeypatch) monkeypatch.setattr("obliteratus.gpu_lifecycle.BROKER_GRANT_READ_TIMEOUT_SECONDS", 0.01) locked = threading.Event() finish = threading.Event() def hold_state_lock(): with publisher._lock: locked.set() finish.wait(timeout=2) worker = threading.Thread(target=hold_state_lock) worker.start() try: assert locked.wait(timeout=1) with pytest.raises(BrokerGrantReadError, match="within 0.01s: lifecycle state is busy"): current_process_grant() estimated = admit_phase( "probe", {"cuda:0": DeviceDemand(activation_bytes=10)}, observer=lambda device: MemoryObservation(device, 1000, 1000), safety_reserve_bytes=0, ) assert estimated.warnings and "could not read broker grant" in estimated.warnings[0] with pytest.raises(PhaseAdmissionError, match="(?i)could not read broker grant") as caught: admit_phase( "probe", {"cuda:0": DeviceDemand(activation_bytes=10)}, observer=lambda device: MemoryObservation(device, 1000, 1000), safety_reserve_bytes=0, components=(DemandComponent("dense forward", "cuda:0", 10, "calibrated"),), ) assert isinstance(caught.value.__cause__, BrokerGrantReadError) finally: finish.set() worker.join(timeout=2) assert not worker.is_alive() @pytest.mark.parametrize("released", [False, True]) def test_actual_zero_or_released_grant_still_rejects_admission(tmp_path, monkeypatch, released): publisher = granted_publisher(tmp_path, monkeypatch, granted=1000 if released else 0) if released: publisher.release() assert current_process_grant() == 0 estimated = admit_phase( "probe", {"cuda:0": DeviceDemand(activation_bytes=10)}, observer=lambda device: MemoryObservation(device, 1000, 1000), safety_reserve_bytes=0, ) assert estimated.warnings and "broker grant" in estimated.warnings[0] with pytest.raises(PhaseAdmissionError, match="active GPU lifecycle broker grant"): admit_phase( "probe", {"cuda:0": DeviceDemand(activation_bytes=10)}, observer=lambda device: MemoryObservation(device, 1000, 1000), safety_reserve_bytes=0, components=(DemandComponent("dense forward", "cuda:0", 10, "calibrated"),), ) def test_fake_supervisor_observes_order_identity_and_recoverable_state(tmp_path): publisher = GpuLifecyclePublisher( tmp_path, heartbeat_seconds=0.01, clock=lambda: datetime(2026, 1, 2, tzinfo=timezone.utc), run_id="run-1", ) def acknowledge(): while not (tmp_path / "current.json").exists(): time.sleep(0.001) request = json.loads((tmp_path / "current.json").read_text()) (tmp_path / "ack.json").write_text(json.dumps({ "schema_version": 1, "run_id": "run-1", "request_event_id": request["event_id"], "decision": "grant", "lease_id": "lease-1", "granted_vram_bytes": 100, })) threading.Thread(target=acknowledge).start() memory = MemoryUsage(allocated_bytes=10, reserved_bytes=12, device_count=1) publisher.loading("org/model") publisher.resize(memory) publisher.ready(memory) time.sleep(1.05) publisher.release(reason="test_complete") assert publisher.release(reason="duplicate") is None events = [json.loads(line) for line in (tmp_path / "events.jsonl").read_text().splitlines()] assert [event["event"] for event in events] == [ "loading", "admission_granted", "allocation_started", "resize", "ready", "heartbeat", "release", ] assert [event["sequence"] for event in events] == list(range(1, 8)) assert len({event["event_id"] for event in events}) == 7 assert all(event["run_id"] == "run-1" for event in events) assert events[3]["reserved_vram_bytes"] == 12 current = json.loads((tmp_path / "current.json").read_text()) assert current["event"] == "release" assert current["reason"] == "test_complete" def test_disabled_publisher_is_noop(): publisher = GpuLifecyclePublisher(None) assert publisher.loading("model") is None assert publisher.resize(MemoryUsage()) is None assert publisher.ready() is None assert publisher.heartbeat() is None assert publisher.release() is None def test_allocation_phase_can_heartbeat_before_ready(tmp_path): publisher = GpuLifecyclePublisher(tmp_path, run_id="loading-run") def acknowledge(): while not (tmp_path / "current.json").exists(): time.sleep(0.001) request = json.loads((tmp_path / "current.json").read_text()) (tmp_path / "ack.json").write_text(json.dumps({ "schema_version": 1, "run_id": "loading-run", "request_event_id": request["event_id"], "decision": "grant", "lease_id": "lease-loading", "granted_vram_bytes": 100, })) thread = threading.Thread(target=acknowledge) thread.start() publisher.loading("org/model") event = publisher.heartbeat() publisher.release(reason="test_complete") thread.join() assert event is not None assert event["event"] == "heartbeat" assert event["run_id"] == "loading-run" def test_environment_uses_stable_experiment_run_id(tmp_path, monkeypatch): from obliteratus.gpu_lifecycle import from_environment monkeypatch.setenv("OBLITERATUS_GPU_LIFECYCLE_DIR", str(tmp_path)) monkeypatch.setenv("OBLITERATUS_RUN_ID", "run-stable") publisher = from_environment() assert publisher._run_id == "run-stable" def test_runtime_directory_must_exist(tmp_path): with pytest.raises(ValueError, match="must already exist"): GpuLifecyclePublisher(tmp_path / "missing") def test_runtime_directory_rejects_symlink(tmp_path): real = tmp_path / "real" real.mkdir() link = tmp_path / "link" link.symlink_to(real, target_is_directory=True) with pytest.raises(ValueError, match="must not be a symlink"): GpuLifecyclePublisher(link) def test_measure_torch_memory_aggregates_devices(): class FakeCuda: is_available = staticmethod(lambda: True) device_count = staticmethod(lambda: 2) memory_allocated = staticmethod(lambda index: (index + 1) * 10) memory_reserved = staticmethod(lambda index: (index + 1) * 20) usage = measure_torch_memory(type("Torch", (), {"cuda": FakeCuda})()) assert usage == MemoryUsage(allocated_bytes=30, reserved_bytes=60, device_count=2) @pytest.mark.parametrize("decision", ["deny", "timeout", "stale"]) def test_admission_failures_never_enter_allocation(tmp_path, decision): publisher = GpuLifecyclePublisher( tmp_path, admission_timeout_seconds=0.05, admission_poll_seconds=0.005, run_id="current-run", ) allocation_entered = False def acknowledge(): nonlocal allocation_entered while not (tmp_path / "current.json").exists(): time.sleep(0.001) request = json.loads((tmp_path / "current.json").read_text()) if decision != "timeout": (tmp_path / "ack.json").write_text(json.dumps({ "schema_version": 1, "run_id": "stale-run" if decision == "stale" else "current-run", "request_event_id": request["event_id"], "decision": "deny" if decision == "deny" else "grant", "lease_id": "lease-1", "granted_vram_bytes": 100, })) worker = threading.Thread(target=acknowledge) worker.start() with pytest.raises(AdmissionError): publisher.loading("org/model") allocation_entered = True worker.join() assert allocation_entered is False events = [ json.loads(line)["event"] for line in (tmp_path / "events.jsonl").read_text().splitlines() ] assert "allocation_started" not in events def test_timeout_error_contains_safe_correlated_diagnostics(tmp_path): publisher = GpuLifecyclePublisher( tmp_path, admission_timeout_seconds=0.02, admission_poll_seconds=0.002, run_id="diagnostic-run", ) with pytest.raises(AdmissionError) as captured: publisher.loading("org/model") error = captured.value assert error.reason == "ack_timeout" assert error.run_id == "diagnostic-run" assert error.request_event_id == "diagnostic-run:1" assert error.elapsed_seconds is not None assert error.timeout_seconds == 0.02 assert "No model weights were loaded or modified" in error.user_message() assert "run_id=diagnostic-run" in error.diagnostic_message() assert "lease" not in error.diagnostic_message() def test_supervisor_denial_reason_is_preserved(tmp_path): publisher = GpuLifecyclePublisher( tmp_path, admission_timeout_seconds=0.2, admission_poll_seconds=0.002, run_id="denied-run", ) def deny(): while not (tmp_path / "current.json").exists(): time.sleep(0.001) request = json.loads((tmp_path / "current.json").read_text()) (tmp_path / "ack.json").write_text(json.dumps({ "schema_version": 1, "run_id": "denied-run", "request_event_id": request["event_id"], "decision": "deny", "reason": "broker_admission_failed", })) threading.Thread(target=deny).start() with pytest.raises(AdmissionError) as captured: publisher.loading("org/model") assert captured.value.reason == "broker_admission_failed" assert "could not reserve" in captured.value.user_message() def test_delayed_ack_blocks_until_granted(tmp_path): publisher = GpuLifecyclePublisher( tmp_path, admission_timeout_seconds=1, admission_poll_seconds=0.005, run_id="run", ) entered = threading.Event() def load(): publisher.loading("model") entered.set() worker = threading.Thread(target=load) worker.start() time.sleep(0.03) assert not entered.is_set() request = json.loads((tmp_path / "current.json").read_text()) (tmp_path / "ack.json").write_text(json.dumps({ "schema_version": 1, "run_id": "run", "request_event_id": request["event_id"], "decision": "grant", "lease_id": "lease", "granted_vram_bytes": 1, })) worker.join(timeout=1) assert entered.is_set() def test_grant_ceiling_and_lease_identity_fail_closed(tmp_path): publisher = GpuLifecyclePublisher( tmp_path, admission_timeout_seconds=0.2, admission_poll_seconds=0.002, run_id="run", ) def grant(request_id, lease_id="lease-a", granted=20): (tmp_path / "ack.json").write_text(json.dumps({ "schema_version": 1, "run_id": "run", "request_event_id": request_id, "decision": "grant", "lease_id": lease_id, "granted_vram_bytes": granted, })) def acknowledge_first(): while not (tmp_path / "current.json").exists(): time.sleep(0.001) grant(json.loads((tmp_path / "current.json").read_text())["event_id"]) threading.Thread(target=acknowledge_first).start() publisher.loading("first") with pytest.raises(AdmissionError, match="exceeds"): publisher.resize(MemoryUsage(reserved_bytes=21)) def acknowledge_changed_owner(): seen = None while seen != "loading": current = json.loads((tmp_path / "current.json").read_text()) seen = current["event"] time.sleep(0.001) grant(current["event_id"], lease_id="lease-b") threading.Thread(target=acknowledge_changed_owner).start() with pytest.raises(AdmissionError, match="ownership changed"): publisher.loading("second")