mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
89 lines
3.1 KiB
Python
89 lines
3.1 KiB
Python
"""Bounded MCP dispatch refuses untyped input and delegates stdio without widening policy."""
|
|
|
|
import asyncio
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, Mock
|
|
|
|
import pytest
|
|
|
|
from obliteratus import mcp_server
|
|
from obliteratus.mcp_policy import policy_from_args
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
@pytest.fixture
|
|
def policy(tmp_path):
|
|
return policy_from_args(mcp_server.build_arg_parser().parse_args([
|
|
"--archive-root", str(tmp_path),
|
|
]))
|
|
|
|
|
|
@pytest.fixture
|
|
def sdk_server(policy):
|
|
pytest.importorskip("mcp")
|
|
return mcp_server.create_server(policy)
|
|
|
|
|
|
@pytest.mark.parametrize("error", [None, RuntimeError("private stdio failure"), asyncio.CancelledError()])
|
|
def test_bounded_stdio_delegates_exact_policy_and_preserves_errors(sdk_server, policy, monkeypatch, error):
|
|
from obliteratus import mcp_transport
|
|
|
|
capabilities = policy.capabilities()
|
|
delegate = AsyncMock(side_effect=error)
|
|
monkeypatch.setattr(mcp_transport, "run_stdio", delegate)
|
|
|
|
async def check_delegation():
|
|
if error is None:
|
|
assert await sdk_server.run_stdio_async() is None
|
|
else:
|
|
# Check the wrapper boundary before Python 3.10's task cancellation
|
|
# machinery replaces CancelledError with a new instance.
|
|
with pytest.raises(type(error)) as failure:
|
|
await sdk_server.run_stdio_async()
|
|
assert failure.value is error
|
|
|
|
asyncio.run(check_delegation())
|
|
|
|
delegate.assert_awaited_once_with(sdk_server, policy)
|
|
assert delegate.await_args.args[1] is policy
|
|
assert policy.capabilities() == capabilities
|
|
assert list(policy.archive_root.iterdir()) == []
|
|
|
|
|
|
@pytest.mark.parametrize("tool,key,other", [
|
|
("list_catalog", "kind", {}),
|
|
("inspect_checkpoint", "path", {}),
|
|
("get_run_status", "run_id", {}),
|
|
("estimate_gpu", "dtype", {"parameters": 1}),
|
|
])
|
|
@pytest.mark.parametrize("value", [None, True, 17, ["private-input"], {"private-input": "secret"}])
|
|
def test_nonstring_read_arguments_are_redacted_before_sdk_dispatch(
|
|
sdk_server, policy, monkeypatch, tool, key, other, value,
|
|
):
|
|
delegate = AsyncMock()
|
|
monkeypatch.setattr(type(sdk_server).__mro__[1], "call_tool", delegate)
|
|
|
|
result = asyncio.run(sdk_server.call_tool(tool, {**other, key: value}))
|
|
|
|
assert result.is_error
|
|
assert len(result.content) == 1
|
|
assert result.content[0].text == "MCP request rejected (invalid, unavailable, or exceeds limits)."
|
|
assert "private-input" not in result.model_dump_json()
|
|
assert "secret" not in result.model_dump_json()
|
|
delegate.assert_not_awaited()
|
|
assert list(policy.archive_root.iterdir()) == []
|
|
|
|
|
|
def test_keyboard_interrupt_stops_stdio_cleanly(policy, monkeypatch, capsys):
|
|
server = SimpleNamespace(run=Mock(side_effect=KeyboardInterrupt))
|
|
create = Mock(return_value=server)
|
|
monkeypatch.setattr(mcp_server, "create_server", create)
|
|
|
|
assert mcp_server.serve(policy) == 0
|
|
|
|
create.assert_called_once_with(policy)
|
|
server.run.assert_called_once_with(transport="stdio")
|
|
assert capsys.readouterr() == ("", "")
|
|
assert list(policy.archive_root.iterdir()) == []
|