Files
OBLITERATUS/tests/test_mcp_read_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

491 lines
22 KiB
Python

"""Synthetic, offline contracts for the read-only MCP boundary (#210)."""
from __future__ import annotations
import asyncio
import json
import os
import subprocess
import sys
from dataclasses import asdict
import pytest
from obliteratus.mcp_policy import McpPolicy, READ_TOOLS
from obliteratus.mcp_tools import ReadTools
RUN_ID = 'run-' + 'a' * 32
def _tools(tmp_path, **kwargs):
return ReadTools(McpPolicy(archive_root=tmp_path / 'archive', allowed_roots=(tmp_path,), **kwargs))
def _manifest(tmp_path, **overrides):
directory = tmp_path / 'archive' / RUN_ID
directory.mkdir(parents=True, exist_ok=True)
value = {'run_id': RUN_ID, 'status': 'succeeded', 'result': {'metrics': {'accuracy': .75}}}
value.update(overrides)
path = directory / 'manifest.json'
path.write_text(json.dumps(value))
return path
@pytest.mark.parametrize('kind', ['model', 'study', 'strategy'])
def test_catalog_deterministic_bounded_pages(tmp_path, kind):
tools = _tools(tmp_path)
first = tools.list_catalog(kind, limit=2)
assert first == tools.list_catalog(kind, limit=2)
assert len(first.items) <= 2
assert [item.id for item in first.items] == sorted(item.id for item in first.items)
second = tools.list_catalog(kind, offset=2, limit=2)
assert not {item.id for item in first.items}.intersection(item.id for item in second.items)
assert first.total >= len(first.items)
assert first.next_offset == (2 if first.total > 2 else None)
@pytest.mark.parametrize('kwargs', [
{'kind': 'unknown'}, {'kind': 'model', 'limit': 101}, {'kind': 'model', 'offset': -1},
{'kind': 'model', 'limit': True}, {'kind': 'model', 'offset': 1001},
])
def test_catalog_rejects_bad_requests(tmp_path, kwargs):
with pytest.raises(ValueError):
_tools(tmp_path).list_catalog(**kwargs)
@pytest.mark.parametrize('parameters', [0, -1, float('nan'), float('inf'), 10**13 + 1, 10**1000, .5, True, '7B'])
def test_estimate_rejects_nonfinite_nonpositive_or_excessive_counts(tmp_path, parameters):
with pytest.raises(ValueError):
_tools(tmp_path).estimate_gpu(parameters)
@pytest.mark.parametrize('kwargs', [{'bits': 3}, {'bits': True}, {'copies': 0}, {'copies': 17}, {'copies': 1.5}])
def test_estimate_rejects_invalid_resources(tmp_path, kwargs):
with pytest.raises(ValueError):
_tools(tmp_path).estimate_gpu(1000, **kwargs)
def test_estimate_is_arithmetic_only(tmp_path, monkeypatch):
import socket
monkeypatch.setattr(socket.socket, 'connect', lambda *a: pytest.fail('network access'))
result = _tools(tmp_path).estimate_gpu(7e9, bits=4, copies=2)
assert result.weight_bytes == 3_500_000_000
assert result.estimated_bytes == 7_000_000_000
assert 'excludes' in result.basis
def test_gpu_budget_keeps_inactive_experts_resident_and_compares_declared_capacity(tmp_path):
tools = _tools(tmp_path)
dense = tools.estimate_gpu(total_parameters=8_000_000_000, dtype='bfloat16',
gpu_memory_bytes=24 << 30, overhead_fraction=.25, reserve_bytes=1 << 30)
moe = tools.estimate_gpu(total_parameters=8_000_000_000, active_parameters=2_000_000_000,
dtype='bfloat16', gpu_memory_bytes=16 << 30,
overhead_fraction=.25, reserve_bytes=1 << 30)
assert dense.active_parameters == dense.total_parameters
assert dense.required_gpu_bytes == moe.required_gpu_bytes == 21_073_741_824
assert dense.weight_bytes == 16_000_000_000
assert moe.active_weight_bytes == 4_000_000_000
assert dense.fits_budget is True and moe.fits_budget is False
assert dense.required_gpu_gib == dense.required_gpu_bytes / 2**30
assert 'GiB' in dense.units and 'not guarantee' in dense.basis
assert tools.estimate_gpu(total_parameters=3, dtype='int4', reserve_bytes=0,
overhead_fraction=0, gpu_memory_bytes=2).fits_budget is True
assert tools.estimate_gpu(total_parameters=3).fits_budget is None
@pytest.mark.parametrize('kwargs', [
{'active_parameters': 1001}, {'active_parameters': 0}, {'active_parameters': True},
{'active_parameters': float('inf')}, {'active_parameters': 1.5},
{'total_parameters': 1000, 'parameters': 1000}, {'dtype': 'fp-ish'},
{'dtype': 'float16', 'bits': 4}, {'dtype': True}, {'gpu_memory_bytes': 0},
{'gpu_memory_bytes': (1 << 40) + 1}, {'gpu_memory_bytes': float('nan')},
{'gpu_memory_bytes': True}, {'gpu_memory_bytes': .5}, {'reserve_bytes': -1},
{'reserve_bytes': float('inf')}, {'reserve_bytes': True}, {'reserve_bytes': (1 << 40) + 1},
{'overhead_fraction': -1}, {'overhead_fraction': 4.1}, {'overhead_fraction': True},
{'overhead_fraction': float('nan')}, {'overhead_fraction': 10**1000},
])
def test_gpu_budget_rejects_invalid_or_ambiguous_assumptions(tmp_path, kwargs):
values = {'total_parameters': 1000, **kwargs}
with pytest.raises(ValueError):
_tools(tmp_path).estimate_gpu(**values)
@pytest.mark.parametrize('dtype,bits', [('float32', 32), ('float16', 16), ('bfloat16', 16),
('int8', 8), ('int4', 4)])
def test_gpu_budget_dtype_and_ceiling_arithmetic(tmp_path, dtype, bits):
result = _tools(tmp_path).estimate_gpu(total_parameters=10**13, active_parameters=1,
dtype=dtype, bits=bits, copies=16,
gpu_memory_bytes=1 << 40, reserve_bytes=1 << 40,
overhead_fraction=4)
assert result.dtype == dtype and result.bits_per_parameter == bits
assert result.required_gpu_bytes == (10**13 * bits // 8) * 16 * 5 + (1 << 40)
assert result.fits_budget is False
def test_checkpoint_descriptor_reads_only_recognized_config_without_tensor_contents(tmp_path, monkeypatch):
checkpoint = tmp_path / 'checkpoint'
checkpoint.mkdir()
(checkpoint / 'weights.safetensors').write_bytes(b'not a tensor; never parse me')
(checkpoint / 'config.json').write_text('{"model_type":"llama","torch_dtype":"bfloat16",'
'"hidden_size":128,"num_hidden_layers":2,"private":"secret"}')
tools = _tools(tmp_path)
real_open = os.open
def directories_only(path, flags, *args, **kwargs):
assert flags & os.O_DIRECTORY or path == 'config.json', 'opened a tensor or arbitrary payload'
return real_open(path, flags, *args, **kwargs)
monkeypatch.setattr(os, 'open', directories_only)
summary = tools.inspect_checkpoint(str(checkpoint))
assert summary.files == 2
assert summary.tensor_files == 1
assert summary.total_bytes > 0
assert summary.descriptor_available is True
assert summary.primary_format == 'safetensors'
assert summary.configuration.model_type == 'llama'
assert summary.configuration.hidden_size == 128
assert summary.payloads_read is False and summary.tensor_headers_read is False
assert summary.integrity_verified is False and summary.loadability_verified is False
assert 'secret' not in json.dumps(asdict(summary))
assert tools.inspect_checkpoint(str(checkpoint / 'weights.safetensors')).files == 1
@pytest.mark.parametrize('kind', ['oversized', 'duplicate', 'nonfinite', 'malformed', 'hardlink'])
def test_checkpoint_descriptor_rejects_unsafe_config_and_never_opens_tensors(tmp_path, monkeypatch, kind):
checkpoint = tmp_path / 'checkpoint'
checkpoint.mkdir()
config = checkpoint / 'config.json'
config.write_text({'oversized': ' ' * 16385, 'duplicate': '{"model_type":"a","model_type":"b"}',
'nonfinite': '{"hidden_size":NaN}', 'malformed': '{', 'hardlink': '{}'}[kind])
if kind == 'hardlink':
os.link(config, checkpoint / 'shared')
(checkpoint / 'model.safetensors').write_bytes(b'tensor payload must never be opened')
real_open = os.open
def bounded_open(path, flags, *args, **kwargs):
assert flags & os.O_DIRECTORY or path == 'config.json'
return real_open(path, flags, *args, **kwargs)
monkeypatch.setattr(os, 'open', bounded_open)
with pytest.raises((ValueError, OSError)):
ReadTools(McpPolicy(archive_root=tmp_path / 'archive', allowed_roots=(checkpoint,))).inspect_checkpoint(str(checkpoint))
def test_bounded_checkpoint_service_descriptor_config_replacement_is_pinned(tmp_path, monkeypatch):
from obliteratus.checkpoint_service import CheckpointService
source = tmp_path / 'source'
source.mkdir()
(source / 'config.json').write_text('{"model_type":"llama","unknown":"secret"}')
(source / 'model.bin').write_bytes(b'never unpickle')
real_open = os.open
replaced = False
def replace_root(path, flags, *args, **kwargs):
nonlocal replaced
if path == 'config.json' and not replaced:
replaced = True
source.rename(tmp_path / 'pinned')
source.mkdir()
(source / 'config.json').symlink_to('/etc/passwd')
return real_open(path, flags, *args, **kwargs)
monkeypatch.setattr(os, 'open', replace_root)
descriptor = CheckpointService().inspect(source, mode='bounded_metadata')
assert descriptor.configuration.model_type == 'llama'
assert descriptor.components[0].trust_required is True
assert 'secret' not in json.dumps(asdict(descriptor))
@pytest.mark.parametrize('kind', ['traversal', 'outside', 'symlink', 'parent_symlink', 'nested_link', 'fifo'])
def test_checkpoint_rejects_escapes_and_special_files(tmp_path, kind):
allowed = tmp_path / 'allowed'
outside = tmp_path / 'outside'
allowed.mkdir()
outside.mkdir()
tools = ReadTools(McpPolicy(archive_root=tmp_path / 'archive', allowed_roots=(allowed,)))
path = allowed
if kind == 'traversal':
path = allowed / '..' / 'outside'
elif kind == 'outside':
path = outside
elif kind == 'symlink':
path = allowed / 'link'
path.symlink_to(outside, target_is_directory=True)
elif kind == 'parent_symlink':
(outside / 'child').mkdir()
(allowed / 'link').symlink_to(outside, target_is_directory=True)
path = allowed / 'link' / 'child'
elif kind == 'nested_link':
(allowed / 'secret').symlink_to(outside, target_is_directory=True)
else:
os.mkfifo(allowed / 'fifo')
with pytest.raises((ValueError, OSError)):
tools.inspect_checkpoint(str(path))
def test_checkpoint_inventory_limits(tmp_path):
from obliteratus.checkpoint_inspection import InspectionLimits
from obliteratus.checkpoint_service import CheckpointService
(tmp_path / 'a').write_bytes(b'123')
(tmp_path / 'b').write_bytes(b'123')
with pytest.raises(ValueError, match='limit'):
CheckpointService(inspection_limits=InspectionLimits(max_files=1)).inspect_metadata(tmp_path)
with pytest.raises(ValueError, match='limit'):
CheckpointService(inspection_limits=InspectionLimits(max_total_bytes=1)).inspect_metadata(tmp_path)
def test_missing_archive_not_created(tmp_path):
tools = _tools(tmp_path)
assert tools.list_runs().runs == []
assert not (tmp_path / 'archive').exists()
with pytest.raises(OSError):
tools.get_run_status(RUN_ID)
assert not (tmp_path / 'archive').exists()
def test_run_reads_redact_and_do_not_recover_lost_worker(tmp_path, monkeypatch):
path = _manifest(tmp_path, status='running', worker={'pid': 99999999},
arguments=['secret'], failure={'message': 'secret'})
before = path.read_bytes()
monkeypatch.setattr(os, 'kill', lambda *a: pytest.fail('process probe'))
tools = _tools(tmp_path)
assert tools.get_run_status(RUN_ID).status == 'running'
assert tools.list_runs().runs[0].run_id == RUN_ID
assert path.read_bytes() == before
assert list(path.parent.iterdir()) == [path]
with pytest.raises(ValueError):
tools.get_run_result(RUN_ID)
def test_terminal_result_allowlist(tmp_path):
_manifest(tmp_path, result={'checkpoint': '/secret/path', 'metrics': {
'accuracy': .75, 'perplexity': float('nan'), 'refusal_rate': True,
'kl_divergence': 'secret', 'secret_key': 1,
}}, metadata={'token': 'secret'}, failure={'message': 'secret'})
value = _tools(tmp_path).get_run_result(RUN_ID)
assert value.metrics == {'accuracy': .75}
assert 'secret' not in json.dumps(asdict(value))
@pytest.mark.parametrize('run_id', ['../secret', '/secret', 'run-' + 'b' * 33, RUN_ID + '\n', 'run-' + 'A' * 32])
def test_forged_run_ids_rejected(tmp_path, run_id):
with pytest.raises(ValueError):
_tools(tmp_path).get_run_status(run_id)
@pytest.mark.parametrize('kind', ['manifest_link', 'run_link', 'root_link', 'ancestor_link', 'fifo', 'oversize', 'mismatch', 'malformed'])
def test_archive_rejects_untrusted_metadata(tmp_path, kind):
path = _manifest(tmp_path)
tools = _tools(tmp_path, max_input_bytes=200)
if kind == 'manifest_link':
target = tmp_path / 'private.json'
target.write_bytes(path.read_bytes())
path.unlink()
path.symlink_to(target)
elif kind == 'run_link':
target = tmp_path / 'private_run'
path.parent.rename(target)
path.parent.symlink_to(target, target_is_directory=True)
elif kind == 'root_link':
archive = tmp_path / 'archive'
archive.rename(tmp_path / 'other')
archive.symlink_to(tmp_path / 'other', target_is_directory=True)
elif kind == 'ancestor_link':
link = tmp_path / 'link'
link.symlink_to(tmp_path / 'archive', target_is_directory=True)
tools = ReadTools(McpPolicy(archive_root=link / 'nested'))
elif kind == 'fifo':
path.unlink()
os.mkfifo(path)
elif kind == 'oversize':
path.write_bytes(b' ' * 201)
elif kind == 'mismatch':
path.write_text(json.dumps({'run_id': 'run-' + 'b' * 32}))
else:
path.write_text('{not json')
with pytest.raises((ValueError, OSError)):
tools.get_run_status(RUN_ID)
def test_archive_enumeration_is_bounded(tmp_path):
from obliteratus.run_archive import RunArchive
archive = tmp_path / 'archive'
archive.mkdir()
(archive / 'a').touch()
(archive / 'b').touch()
with pytest.raises(ValueError, match='limit'):
RunArchive(archive, read_only=True).read_ids(maximum=1)
def test_readonly_archive_refuses_legacy_recovery_or_writes(tmp_path):
from obliteratus.run_archive import RunArchive
_manifest(tmp_path)
archive = RunArchive(tmp_path / 'archive', read_only=True)
with pytest.raises(ValueError, match='read-only'):
archive.status(RUN_ID)
with pytest.raises(ValueError, match='read-only'):
archive.begin(['synthetic/model'])
def test_read_tools_import_no_model_runtimes():
code = '''
import sys
from pathlib import Path
from obliteratus.mcp_tools import ReadTools
from obliteratus.mcp_policy import McpPolicy
tools = ReadTools(McpPolicy(archive_root=Path('/nonexistent/archive')))
for kind in ('model', 'study', 'strategy'):
tools.list_catalog(kind)
tools.estimate_gpu(100)
assert 'obliteratus.presets' not in sys.modules
assert 'obliteratus.bestiary_sync' not in sys.modules
assert not {'torch', 'transformers', 'huggingface_hub', 'mcp'}.intersection(sys.modules)
'''
result = subprocess.run([sys.executable, '-c', code], capture_output=True, text=True, timeout=30)
assert result.returncode == 0, result.stderr
def test_sdk_read_tools_schemas_limits_redaction_and_hints(tmp_path):
pytest.importorskip('mcp')
from obliteratus.mcp_server import create_server
_manifest(tmp_path)
server = create_server(_tools(tmp_path).policy)
async def scenario():
tools = await server.list_tools()
assert {tool.name for tool in tools} == set(READ_TOOLS)
for tool in tools:
assert tool.annotations.read_only_hint is True
assert tool.annotations.open_world_hint is False
assert tool.output_schema is not None
cases = [
('list_catalog', {'kind': 'model', 'limit': 2}),
('estimate_gpu', {'parameters': 1e9}),
('estimate_gpu', {'total_parameters': 1e9, 'active_parameters': 1e8,
'dtype': 'bfloat16', 'gpu_memory_bytes': 4 << 30}),
('inspect_checkpoint', {'path': str(tmp_path)}),
('list_runs', {}), ('get_run_status', {'run_id': RUN_ID}),
('get_run_result', {'run_id': RUN_ID}),
]
for name, arguments in cases:
result = await server.call_tool(name, arguments)
assert not result.is_error, (name, result)
for name, arguments in [
('get_run_status', {'run_id': 'secret'}),
('get_run_status', {'run_id': RUN_ID, 'secret': 'secret'}),
('inspect_checkpoint', {'path': '/secret/path'}),
('estimate_gpu', {'parameters': float('nan')}),
('estimate_gpu', {'parameters': True}),
('estimate_gpu', {'parameters': 10, 'copies': True}),
('estimate_gpu', {'total_parameters': 10, 'active_parameters': True}),
('estimate_gpu', {'total_parameters': 10, 'gpu_memory_bytes': True}),
('estimate_gpu', {'total_parameters': 10, 'overhead_fraction': True}),
('estimate_gpu', {'total_parameters': 10, 'reserve_bytes': True}),
('estimate_gpu', {'total_parameters': 10, 'dtype': 'secret'}),
('list_catalog', {'kind': 'secret'}),
('secret_unknown_tool', {}),
]:
result = await server.call_tool(name, arguments)
assert result.is_error
assert 'secret' not in result.model_dump_json()
limited = create_server(_tools(tmp_path, max_input_bytes=100, max_output_bytes=200).policy)
large_input = await limited.call_tool('get_run_status', {'run_id': 'secret' * 200})
assert large_input.is_error
large_output = await limited.call_tool('list_catalog', {'kind': 'model', 'limit': 100})
assert large_output.is_error
assert 'secret' not in large_input.model_dump_json()
assert (await limited.call_tool('estimate_gpu', {'parameters': 0})).is_error
asyncio.run(scenario())
def test_catalog_snapshot_matches_bundled_sources_without_importing_them():
import ast
from pathlib import Path
from obliteratus.mcp_catalog import CATALOG
root = Path(__file__).resolve().parents[1] / 'obliteratus'
for kind, filename, constructor, key in [
('model', 'presets.py', 'ModelPreset', 'hf_id'),
('study', 'study_presets.py', 'StudyPreset', 'key'),
]:
values = []
for node in ast.walk(ast.parse((root / filename).read_text())):
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == constructor:
fields = {kw.arg: ast.literal_eval(kw.value) for kw in node.keywords if kw.arg in {key, 'name'}}
values.append((fields[key], fields['name']))
assert tuple(sorted(values)) == CATALOG[kind]
strategies = []
for path in (root / 'strategies').glob('*.py'):
for node in ast.walk(ast.parse(path.read_text())):
if isinstance(node, ast.Assign) and any(isinstance(t, ast.Name) and t.id == 'name' for t in node.targets):
if isinstance(node.value, ast.Constant) and node.value.value:
strategies.append(node.value.value)
assert tuple((name, name.replace('_', ' ')) for name in sorted(strategies)) == CATALOG['strategy']
def test_checkpoint_directory_and_depth_bounds(tmp_path):
from obliteratus.checkpoint_inspection import InspectionLimits
from obliteratus.checkpoint_service import CheckpointService
(tmp_path / 'child').mkdir()
with pytest.raises(ValueError, match='directory limit'):
CheckpointService(inspection_limits=InspectionLimits(max_directories=1)).inspect_metadata(tmp_path)
current = tmp_path / 'child'
for _ in range(33):
current = current / 'd'
current.mkdir()
with pytest.raises(ValueError, match='depth limit'):
CheckpointService().inspect_metadata(tmp_path)
def test_empty_allowed_roots_deny_checkpoint_access(tmp_path):
tools = ReadTools(McpPolicy(archive_root=tmp_path))
with pytest.raises(ValueError, match='outside'):
tools.inspect_checkpoint(str(tmp_path))
def test_metadata_directory_replacement_cannot_follow_link(tmp_path, monkeypatch):
allowed = tmp_path / 'allowed'
child = allowed / 'child'
child.mkdir(parents=True)
outside = tmp_path / 'private'
outside.mkdir()
(outside / 'secret').write_bytes(b'secret')
real_open = os.open
swapped = False
def replace_child(path, flags, *args, **kwargs):
nonlocal swapped
if path == 'child' and kwargs.get('dir_fd') is not None and not swapped:
swapped = True
child.rename(allowed / 'old-child')
child.symlink_to(outside, target_is_directory=True)
return real_open(path, flags, *args, **kwargs)
monkeypatch.setattr(os, 'open', replace_child)
with pytest.raises((ValueError, OSError)):
_tools(tmp_path).inspect_checkpoint(str(allowed))
assert swapped
def test_manifest_growth_is_bounded_after_stat(tmp_path, monkeypatch):
from types import SimpleNamespace
from obliteratus.read_boundary import open_directory, read_json_bytes
(tmp_path / 'manifest.json').write_bytes(b'x' * 201)
real_fstat = os.fstat
def small_initial_size(fd):
value = real_fstat(fd)
return SimpleNamespace(st_mode=value.st_mode, st_nlink=1, st_size=0)
monkeypatch.setattr(os, 'fstat', small_initial_size)
with open_directory(tmp_path) as fd:
with pytest.raises(ValueError, match='limit'):
read_json_bytes(fd, 'manifest.json', 100)
def test_hardlinked_archive_metadata_is_not_followed(tmp_path):
path = _manifest(tmp_path)
os.link(path, tmp_path / 'outside-manifest')
with pytest.raises(ValueError):
_tools(tmp_path).get_run_status(RUN_ID)