Files
OBLITERATUS/obliteratus/mcp_execution.py

267 lines
12 KiB
Python

"""Single-use local execution plans. No SDK, torch, Hub or model imports."""
from __future__ import annotations
import hashlib
import json
import os
import secrets
import signal
import stat
import threading
import time
import uuid
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Literal
from obliteratus.mcp_policy import McpPolicy
from obliteratus.mcp_tools import ReadTools, RunSummary, _summary
from obliteratus.read_boundary import child_directory, open_directory
BWRAP = Path('/usr/bin/bwrap')
def require_pidfd_support() -> None:
"""Refuse execution unless this interpreter/kernel can control pidfds.
A Linux or Python version does not establish that the bindings were built
or that the syscalls are permitted. Signal 0 checks only our own process;
worker authorization remains the cancellation path's responsibility.
"""
message = 'MCP execution requires working os.pidfd_open and signal.pidfd_send_signal'
open_pidfd = getattr(os, 'pidfd_open', None)
send_signal = getattr(signal, 'pidfd_send_signal', None)
if not callable(open_pidfd) or not callable(send_signal):
raise ValueError(message)
try:
fd = open_pidfd(os.getpid())
try:
send_signal(fd, 0)
finally:
os.close(fd)
except (OSError, NotImplementedError):
raise ValueError(message) from None
@dataclass(frozen=True)
class ModelInput:
kind: Literal['local', 'remote']
identifier: str
@dataclass(frozen=True)
class RunResources:
parameters: int
memory_bytes: int = 16 << 30
work_bytes: int = 512 << 20
runtime_seconds: int = 3600
threads: int = 2
sequence_length: int = 128
generation_tokens: int = 32
@dataclass(frozen=True)
class RunPlan:
token: str
run_id: str
destination: str
expires_in_seconds: int
estimated_weight_bytes: int
resources: RunResources
network: bool
side_effects: tuple[str, ...]
@dataclass(frozen=True)
class LaunchSpec:
run_id: str
model: ModelInput
method: Literal['basic', 'advanced']
output_root: str
resources: RunResources
network: bool
root_identity: tuple[int, int]
model_fingerprint: str
sandbox_identity: tuple[int, int, int, int]
concurrency: int
def directory_identity(path: Path) -> tuple[int, int]:
with open_directory(path) as fd:
value = os.fstat(fd)
return value.st_dev, value.st_ino
def sandbox_identity() -> tuple[int, int, int, int]:
if os.name != 'posix' or not Path('/proc/self/fd').is_dir():
raise ValueError('execution requires Linux and Bubblewrap')
with open_directory(BWRAP.parent) as fd:
value = os.stat(BWRAP.name, dir_fd=fd, follow_symlinks=False)
if not stat.S_ISREG(value.st_mode) or value.st_uid != 0 or value.st_mode & 0o022:
raise ValueError('untrusted sandbox executable')
return value.st_dev, value.st_ino, value.st_size, value.st_mtime_ns
def model_fingerprint(path: Path, max_total_bytes: int = 1 << 40) -> str:
"""Bounded metadata identity, without opening tensor payloads or config."""
digest = hashlib.sha256()
seen = 0
tensors = 0
config = False
total_bytes = 0
def visit(fd: int, depth: int) -> None:
nonlocal seen, tensors, config, total_bytes
if depth > 16:
raise ValueError('model depth limit')
entries = []
with os.scandir(fd) as iterator:
for entry in iterator:
seen += 1
if seen > 1000:
raise ValueError('model file limit')
entries.append(entry.name)
for name in sorted(entries):
value = os.stat(name, dir_fd=fd, follow_symlinks=False)
digest.update(json.dumps([name, value.st_dev, value.st_ino, value.st_size,
value.st_mtime_ns, value.st_mode]).encode())
if stat.S_ISDIR(value.st_mode):
with child_directory(fd, name) as child:
visit(child, depth + 1)
elif stat.S_ISREG(value.st_mode):
total_bytes += value.st_size
if total_bytes > max_total_bytes:
raise ValueError('model input byte ceiling')
if Path(name).suffix in {'.bin', '.pt', '.pth', '.pkl', '.pickle', '.py'}:
raise ValueError('execution accepts inert safetensors models only')
tensors += int(name.endswith('.safetensors'))
config |= depth == 0 and name == 'config.json'
else:
raise ValueError('model links/special files denied')
with open_directory(path) as fd:
root = os.fstat(fd)
digest.update(json.dumps([root.st_dev, root.st_ino]).encode())
visit(fd, 0)
if not tensors or not config:
raise ValueError('model requires config.json and safetensors')
return digest.hexdigest()
class ExecutionTools(ReadTools):
def __init__(self, policy: McpPolicy):
super().__init__(policy)
if not policy.execution_enabled:
raise ValueError('execution disabled')
require_pidfd_support()
self._plans: dict[str, tuple[float, LaunchSpec]] = {}
self._lock = threading.Lock()
def _validate(self, model: ModelInput, method: str, output_root: str,
resources: RunResources) -> tuple[tuple[int, int], str]:
policy = self.policy
if not isinstance(model, ModelInput) or not isinstance(resources, RunResources):
raise ValueError('typed model/resources required')
if method not in {'basic', 'advanced'} or output_root != str(policy.archive_root):
raise ValueError('method/output denied')
maxima = {'parameters': 10**13, 'memory_bytes': policy.max_memory_bytes,
'work_bytes': policy.max_work_bytes, 'runtime_seconds': policy.max_runtime_seconds,
'threads': policy.max_threads, 'sequence_length': policy.max_sequence_length,
'generation_tokens': policy.max_generation_tokens}
for name, maximum in maxima.items():
value = getattr(resources, name)
if type(value) is not int or not 1 <= value <= maximum:
raise ValueError('resource ceiling exceeded')
if resources.work_bytes > resources.memory_bytes or resources.parameters * 4 > resources.memory_bytes:
raise ValueError('weight/work budget exceeds memory ceiling')
identifier = model.identifier
if type(identifier) is not str or not 1 <= len(identifier) <= 1024 or '\x00' in identifier:
raise ValueError('invalid model identifier')
fingerprint = ''
if model.kind == 'local':
path = Path(identifier)
if not path.is_absolute() or '..' in path.parts or not any(
path.is_relative_to(root) for root in policy.allowed_roots
):
raise ValueError('local model outside allowed roots')
if path == policy.archive_root or path.is_relative_to(policy.archive_root):
raise ValueError('model must be outside archive')
fingerprint = model_fingerprint(path, resources.memory_bytes)
elif model.kind == 'remote':
from obliteratus.mcp_catalog import CATALOG
if not (policy.network_enabled and policy.remote_models_enabled and policy.downloads_enabled):
raise ValueError('remote model/download policy denied')
if identifier not in dict(CATALOG['model']):
raise ValueError('remote model is not bundled/allowlisted')
else:
raise ValueError('invalid model kind')
with open_directory(policy.archive_root) as fd:
root = os.fstat(fd)
if root.st_uid != os.getuid() or root.st_mode & 0o022:
raise ValueError('archive must be owned by caller and not group/world writable')
return directory_identity(policy.archive_root), fingerprint
def plan_run(self, model: ModelInput, method: Literal['basic', 'advanced'],
output_root: str, resources: RunResources) -> RunPlan:
require_pidfd_support()
root, fingerprint = self._validate(model, method, output_root, resources)
sandbox = sandbox_identity()
run_id = 'run-' + uuid.uuid4().hex
network = model.kind == 'remote'
spec = LaunchSpec(run_id, model, method, output_root, resources, network,
root, fingerprint, sandbox, self.policy.max_concurrent_runs)
token = secrets.token_urlsafe(32)
plan = RunPlan(token, run_id, str(self.policy.archive_root / run_id / 'checkpoint'),
self.policy.plan_ttl_seconds, resources.parameters * 4, resources, network,
('Create managed run manifest, events and checkpoint on launch.',
'Run CPU float32 surgery in Bubblewrap; no remote code or shell.',
'Use a size-limited tmpfs for temporary files, caches and staged output.',
'Download bundled model into isolated cache.' if network else 'Network namespace is isolated.'))
if len(json.dumps(asdict(plan)).encode()) * 3 + 1024 > self.policy.max_output_bytes:
raise ValueError('plan response exceeds output ceiling')
with self._lock:
now = time.monotonic()
self._plans = {key: value for key, value in self._plans.items() if value[0] > now}
if len(self._plans) >= 64:
raise ValueError('pending plan ceiling exceeded')
self._plans[token] = (now + self.policy.plan_ttl_seconds, spec)
return plan
def launch_run(self, token: str) -> RunSummary:
if type(token) is not str or len(token) > 128:
raise ValueError('invalid plan token')
with self._lock:
entry = self._plans.pop(token, None) # consume even if revalidation fails
if entry is None or entry[0] <= time.monotonic():
raise ValueError('invalid or expired plan token')
spec = entry[1]
root, fingerprint = self._validate(spec.model, spec.method, spec.output_root, spec.resources)
if root != spec.root_identity or fingerprint != spec.model_fingerprint or sandbox_identity() != spec.sandbox_identity:
raise ValueError('plan inputs changed')
from obliteratus.run_archive import RunArchive
archive = RunArchive(self.policy.archive_root, read_only=True)
run_id = archive.launch(build_arguments(spec), guard=spec)
return RunSummary(run_id, 'queued', False)
def cancel_run(self, run_id: str) -> RunSummary:
from obliteratus.run_archive import RunArchive
archive = RunArchive(self.policy.archive_root, read_only=True)
return _summary(archive.cancel(run_id, guarded=True))
def _snapshot(self, run_id: str) -> dict:
from obliteratus.mcp_worker import recover
return recover(self.policy.archive_root, run_id)
def build_arguments(spec: LaunchSpec) -> list[str]:
"""Only these CLI fields can cross the subprocess boundary."""
resources = spec.resources
model = '/tmp/mcp-model' if spec.model.kind == 'local' else spec.model.identifier
return [model, '--method', spec.method, '--device', 'cpu', '--dtype', 'float32',
'--seed', '0', '--dataset', 'builtin', '--skip-repair', '--skip-collateral',
'--max-seq-length', str(resources.sequence_length),
'--refusal-max-tokens', str(resources.generation_tokens),
'--refusal-eval-max-new-tokens', str(resources.generation_tokens),
'--refusal-eval-size', '1',
'--verify-sample-size', '1']