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

205 lines
8.6 KiB
Python

"""Offline read-only MCP operations. No SDK or model runtime imports."""
from __future__ import annotations
import math
from dataclasses import dataclass
from fractions import Fraction
from pathlib import Path
from typing import Literal
from obliteratus.mcp_policy import McpPolicy
from obliteratus.checkpoint_service import BoundedCheckpointDescriptor
CatalogKind = Literal['model', 'study', 'strategy']
Dtype = Literal['float32', 'float16', 'bfloat16', 'int8', 'int4']
MAX_PAGE = 100
MAX_OFFSET = 1000
RunState = Literal['queued', 'running', 'cancelling', 'succeeded', 'failed', 'cancelled']
RUN_STATES = frozenset({'queued', 'running', 'cancelling', 'succeeded', 'failed', 'cancelled'})
@dataclass(frozen=True)
class CatalogItem:
id: str
name: str
@dataclass(frozen=True)
class CatalogResult:
kind: CatalogKind
items: list[CatalogItem]
total: int
next_offset: int | None
@dataclass(frozen=True)
class GpuEstimate:
total_parameters: int
active_parameters: int
dtype: Dtype
bits_per_parameter: int
copies: int
weight_bytes: int
estimated_bytes: int
active_weight_bytes: int
overhead_fraction: float
reserve_bytes: int
required_gpu_bytes: int
gpu_memory_bytes: int | None
fits_budget: bool | None
required_gpu_gib: float
units: str = 'Counts are parameters; memory is bytes; GiB = 2**30 bytes.'
basis: str = ('All total weights remain resident, including inactive MoE experts. '
'required_gpu_bytes = ceil(weight_bytes * copies * (1 + overhead_fraction)) '
'+ reserve_bytes. Overhead and reserve are caller assumptions, not measured peaks; '
'excludes explicit KV cache, activation, training/optimizer and quantization metadata sizing. '
'fits_budget compares this assumed budget only; it does not guarantee a model will run.')
@dataclass(frozen=True)
class RunSummary:
run_id: str
status: RunState
result_available: bool
@dataclass(frozen=True)
class RunsResult:
runs: list[RunSummary]
total: int
next_offset: int | None
@dataclass(frozen=True)
class RunResult:
run: RunSummary
metrics: dict[str, float]
def _page(offset: int, limit: int) -> None:
if type(offset) is not int or not 0 <= offset <= MAX_OFFSET:
raise ValueError('invalid offset')
if type(limit) is not int or not 1 <= limit <= MAX_PAGE:
raise ValueError('invalid limit')
def _next(offset: int, limit: int, total: int) -> int | None:
return offset + limit if offset + limit < total else None
def _summary(value: dict) -> RunSummary:
status = value.get('status')
if not isinstance(status, str) or status not in RUN_STATES:
raise ValueError('invalid archived state')
return RunSummary(value['run_id'], status, status in {'succeeded', 'failed', 'cancelled'})
class ReadTools:
def __init__(self, policy: McpPolicy):
self.policy = policy
def list_catalog(self, kind: CatalogKind, offset: int = 0, limit: int = 50) -> CatalogResult:
_page(offset, limit)
from obliteratus.mcp_catalog import CATALOG
if not isinstance(kind, str) or kind not in CATALOG:
raise ValueError('invalid catalog kind')
items = [CatalogItem(key, name) for key, name in CATALOG[kind]]
return CatalogResult(kind, items[offset:offset + limit], len(items), _next(offset, limit, len(items)))
def estimate_gpu(
self, parameters: float | None = None, bits: Literal[4, 8, 16, 32] | None = None,
copies: int = 1, total_parameters: float | None = None,
active_parameters: float | None = None,
dtype: Dtype | None = None,
gpu_memory_bytes: float | None = None, overhead_fraction: float = 0.2,
reserve_bytes: float = 1073741824,
) -> GpuEstimate:
def count(value, maximum, *, zero=False):
if (type(value) not in {int, float} or not (0 <= value <= maximum if zero else 0 < value <= maximum)
or not math.isfinite(value) or value != int(value)):
raise ValueError('invalid finite whole count')
return int(value)
if parameters is not None and total_parameters is not None:
raise ValueError('use total_parameters or the legacy parameters alias')
total = count(total_parameters if total_parameters is not None else parameters, 10**13)
active = total if active_parameters is None else count(active_parameters, total)
dtypes = {'float32': 32, 'float16': 16, 'bfloat16': 16, 'int8': 8, 'int4': 4}
if dtype is not None and (type(dtype) is not str or dtype not in dtypes):
raise ValueError('invalid dtype')
if bits is not None and (type(bits) is not int or bits not in {4, 8, 16, 32}):
raise ValueError('invalid bits')
if dtype is not None and bits is not None and dtypes[dtype] != bits:
raise ValueError('conflicting dtype and bits')
dtype = dtype or {4: 'int4', 8: 'int8', 16: 'float16', 32: 'float32'}[bits or 16]
bits = dtypes[dtype]
if type(copies) is not int or not 1 <= copies <= 16:
raise ValueError('invalid copies')
capacity = None if gpu_memory_bytes is None else count(gpu_memory_bytes, 1 << 40)
reserve = count(reserve_bytes, 1 << 40, zero=True)
if (type(overhead_fraction) not in {int, float} or not 0 <= overhead_fraction <= 4
or not math.isfinite(overhead_fraction)):
raise ValueError('invalid overhead fraction')
weights = (total * bits + 7) // 8
resident = weights * copies
overhead = Fraction(str(overhead_fraction))
required = (resident * (overhead.denominator + overhead.numerator)
+ overhead.denominator - 1) // overhead.denominator + reserve
return GpuEstimate(total, active, dtype, bits, copies, weights, resident,
(active * bits + 7) // 8, float(overhead_fraction), reserve,
required, capacity, None if capacity is None else required <= capacity,
required / (1 << 30))
def inspect_checkpoint(self, path: str) -> BoundedCheckpointDescriptor:
from obliteratus.checkpoint_inspection import InspectionLimits
from obliteratus.checkpoint_service import CheckpointService
if not isinstance(path, str) or len(path) > 4096 or '\x00' in path:
raise ValueError('invalid checkpoint path')
source = Path(path)
if not source.is_absolute() or '..' in source.parts:
raise ValueError('invalid checkpoint path')
if not any(source.is_relative_to(root) for root in self.policy.allowed_roots):
raise ValueError('checkpoint path outside allowed roots')
service = CheckpointService(inspection_limits=InspectionLimits(
max_files=1000, max_directories=100, max_total_bytes=1 << 40,
max_json_bytes=min(self.policy.max_input_bytes, 16384),
))
return service.inspect(source, mode='bounded_metadata')
def _archive(self):
from obliteratus.run_archive import RunArchive
return RunArchive(self.policy.archive_root, read_only=True)
def _snapshot(self, run_id: str) -> dict:
if not isinstance(run_id, str) or len(run_id) != 36:
raise ValueError('invalid run ID')
return self._archive().read_snapshot(run_id, max_bytes=min(self.policy.max_input_bytes, 1 << 20))
def list_runs(self, offset: int = 0, limit: int = 50) -> RunsResult:
_page(offset, limit)
archive = self._archive()
ids = archive.read_ids()
runs = [_summary(archive.read_snapshot(key, max_bytes=min(self.policy.max_input_bytes, 1 << 20)))
for key in ids[offset:offset + limit]]
return RunsResult(runs, len(ids), _next(offset, limit, len(ids)))
def get_run_status(self, run_id: str) -> RunSummary:
return _summary(self._snapshot(run_id))
def get_run_result(self, run_id: str) -> RunResult:
value = self._snapshot(run_id)
summary = _summary(value)
if not summary.result_available:
raise ValueError('run has no terminal result')
result = value.get('result')
metrics = result.get('metrics') if isinstance(result, dict) else None
safe = {}
if isinstance(metrics, dict):
for key in ('perplexity', 'refusal_rate', 'accuracy', 'kl_divergence'):
number = metrics.get(key)
if type(number) in {float, int} and abs(number) <= 1e100 and math.isfinite(number):
safe[key] = float(number)
return RunResult(summary, safe)