Files
OBLITERATUS/obliteratus/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

131 lines
4.1 KiB
Python

"""Exclusive UI operations and cooperative worker ownership, without Gradio."""
from __future__ import annotations
from contextlib import contextmanager
from contextvars import ContextVar, copy_context
from functools import wraps
import logging
import threading
_operation = ContextVar("ui_operation", default=None)
@contextmanager
def admission_warnings(on_warning):
"""Forward this thread's admission warnings without changing their severity.
A warning means incomplete/estimated headroom or an explicit bypass. It is
neither a capacity guarantee nor an evaluation failure.
"""
owner = threading.get_ident()
class Handler(logging.Handler):
def emit(self, record):
if threading.get_ident() == owner:
try:
on_warning(f"Memory admission warning: {record.getMessage()}")
except Exception:
pass
logger = logging.getLogger("obliteratus.phase_admission")
handler = Handler(level=logging.WARNING)
logger.addHandler(handler)
try:
yield
finally:
logger.removeHandler(handler)
def with_admission_warnings(on_warning):
def decorate(function):
@wraps(function)
def wrapped(*args, **kwargs):
with admission_warnings(on_warning):
return function(*args, **kwargs)
return wrapped
return decorate
class OperationWorkers:
"""Keep the model slot until every worker has stopped using tensors."""
def __init__(self):
self.cancellation = threading.Event()
self.workers = []
def thread(self, target, *, kwargs=None, daemon=True):
context = copy_context()
worker = threading.Thread(
target=lambda: context.run(target, **(kwargs or {})), daemon=daemon,
)
self.workers.append(worker)
return worker
def finish(self, cleanup):
self.cancellation.set()
alive = [worker for worker in self.workers if worker.ident is not None and worker.is_alive()]
for worker in alive:
worker.join(timeout=1)
alive = [worker for worker in alive if worker.is_alive()]
if alive:
def reap():
for worker in alive:
worker.join()
cleanup()
threading.Thread(target=reap, daemon=True).start()
else:
cleanup()
def operation_thread(target, *, kwargs=None, daemon=True):
owner = _operation.get()
if owner is None:
raise RuntimeError("Model worker requires an exclusive UI operation")
return owner.thread(target, kwargs=kwargs, daemon=daemon)
def operation_cancellation():
owner = _operation.get()
if owner is None:
raise RuntimeError("Cancellation requires an exclusive UI operation")
return owner.cancellation
def exclusive_operation(*, acquire, unload, cleanup, busy):
"""Decorate a streaming operation, retaining its claim through worker exit."""
def decorate(function):
@wraps(function)
def wrapped(*args, **kwargs):
claim = acquire()
if claim is None:
yield busy
return
owner = OperationWorkers()
generator = None
try:
unload()
generator = function(*args, **kwargs)
while True:
token = _operation.set(owner)
try:
value = next(generator)
except StopIteration as completed:
return completed.value
finally:
_operation.reset(token)
yield value
finally:
try:
owner.cancellation.set()
if generator is not None:
token = _operation.set(owner)
try:
generator.close()
finally:
_operation.reset(token)
finally:
owner.finish(lambda: cleanup(claim))
return wrapped
return decorate