Files
OBLITERATUS/tests/test_ui_operations.py
Joseph Magly 002e6e56e1 feat: complete local readiness across UI, MCP and memory admission
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
2026-10-20 17:58:20 -04:00

227 lines
6.8 KiB
Python

"""CPU contracts for exclusive streaming UI model operations."""
from __future__ import annotations
import logging
import threading
import pytest
from obliteratus.ui_model_load import claim_model_slot
from obliteratus.ui_operations import (
admission_warnings, exclusive_operation, operation_cancellation, operation_thread,
with_admission_warnings,
)
def slot():
state = {"status": "ready", "model": object(), "tokenizer": object(), "output_dir": None}
lock = threading.Lock()
events = []
def acquire():
return claim_model_slot(state, lock)
def unload():
events.append("unload")
state.update(model=None, tokenizer=None)
def cleanup(claim):
events.append("cleanup")
claim.rollback()
return state, acquire, unload, cleanup, events
def test_exclusive_stream_blocks_other_operations_and_restores_context():
state, acquire, unload, cleanup, events = slot()
@exclusive_operation(acquire=acquire, unload=unload, cleanup=cleanup, busy="busy")
def run():
yield "working"
yield "done"
first = run()
assert next(first) == "working"
assert state["status"] == "obliterating"
assert list(run()) == ["busy"]
assert events == ["unload"]
with pytest.raises(RuntimeError, match="exclusive UI operation"):
operation_cancellation()
assert list(first) == ["done"]
assert events == ["unload", "cleanup"]
assert state["status"] == "idle" and "load_token" not in state
assert list(run()) == ["working", "done"]
@pytest.mark.parametrize("failure", ["unload", "generator"])
def test_exception_keeps_cleanup_and_claim_ownership(failure):
state, acquire, unload, cleanup, events = slot()
def load():
unload()
if failure == "unload":
raise ValueError("unload failed")
@exclusive_operation(acquire=acquire, unload=load, cleanup=cleanup, busy="busy")
def run():
raise ValueError("generator failed")
yield
with pytest.raises(ValueError, match=failure + " failed"):
list(run())
assert events == ["unload", "cleanup"]
assert state["status"] == "idle" and "load_token" not in state
def test_closing_stream_cancels_and_joins_worker_before_cleanup():
state, acquire, unload, cleanup, events = slot()
started = threading.Event()
stopped = threading.Event()
workers = []
def finish(claim):
assert stopped.is_set()
assert not workers[0].is_alive()
cleanup(claim)
@exclusive_operation(acquire=acquire, unload=unload, cleanup=finish, busy="busy")
def run():
cancellation = operation_cancellation()
def work():
assert operation_cancellation() is cancellation
started.set()
if cancellation.wait(timeout=5):
stopped.set()
worker = operation_thread(work)
workers.append(worker)
worker.start()
assert started.wait(timeout=5)
yield "working"
stream = run()
assert next(stream) == "working"
stream.close()
assert events == ["unload", "cleanup"]
assert state["status"] == "idle" and "load_token" not in state
def test_generator_finally_has_its_cancelled_operation_context():
_, acquire, unload, cleanup, events = slot()
@exclusive_operation(acquire=acquire, unload=unload, cleanup=cleanup, busy="busy")
def run():
try:
yield "working"
finally:
assert operation_cancellation().is_set()
events.append("generator_closed")
stream = run()
next(stream)
stream.close()
assert events == ["unload", "generator_closed", "cleanup"]
def test_unavailable_warning_notification_does_not_fail_operation():
def unavailable(message):
raise RuntimeError("no UI notification context")
@with_admission_warnings(unavailable)
def run():
logging.getLogger("obliteratus.phase_admission").warning("estimated headroom")
return "completed"
assert run() == "completed"
def test_abandoned_slow_worker_retains_slot_until_reaper_finishes(monkeypatch):
state, acquire, unload, cleanup, events = slot()
started, proceed, finished = threading.Event(), threading.Event(), threading.Event()
cancellations, workers = [], []
def finish(claim):
assert not workers[0].is_alive()
cleanup(claim)
finished.set()
@exclusive_operation(acquire=acquire, unload=unload, cleanup=finish, busy="busy")
def run():
cancellations.append(operation_cancellation())
def work():
started.set()
proceed.wait(timeout=5)
worker = operation_thread(work)
workers.append(worker)
worker.start()
assert started.wait(timeout=5)
real_join = worker.join
monkeypatch.setattr(worker, "join", lambda timeout=None: (
real_join() if timeout is None else None
))
yield "working"
stream = run()
assert next(stream) == "working"
try:
stream.close()
assert cancellations[0].is_set()
assert events == ["unload"]
assert state["status"] == "obliterating"
assert list(run()) == ["busy"]
assert not finished.is_set()
finally:
proceed.set()
assert finished.wait(timeout=5)
assert events == ["unload", "cleanup"]
assert "load_token" not in state
def test_admission_warning_handlers_are_thread_local_and_removed_on_error():
logger = logging.getLogger("obliteratus.phase_admission")
previous = list(logger.handlers)
barrier = threading.Barrier(2)
messages = {"a": [], "b": []}
errors = []
def worker(name):
try:
with admission_warnings(messages[name].append):
barrier.wait(timeout=5)
logger.warning("estimated headroom %s", name)
barrier.wait(timeout=5)
raise ValueError("scope exit")
except ValueError:
pass
except BaseException as error:
errors.append(error)
workers = [threading.Thread(target=worker, args=(name,)) for name in messages]
for worker in workers:
worker.start()
for worker in workers:
worker.join(timeout=5)
assert not errors and all(not worker.is_alive() for worker in workers)
assert messages == {
name: [f"Memory admission warning: estimated headroom {name}"] for name in messages
}
assert logger.handlers == previous
def test_memory_warning_does_not_change_operation_result():
logger = logging.getLogger("obliteratus.phase_admission")
messages = []
result = {"passed": False, "score": 0.25}
@with_admission_warnings(messages.append)
def evaluate():
logger.warning("ADMISSION DISABLED: explicit bypass")
return result
assert evaluate() is result
assert messages == ["Memory admission warning: ADMISSION DISABLED: explicit bypass"]