Main made the strict ruff config the default (`ruff check .`, blocking), and
the branch's code carried 278 findings under it (main itself is clean). Fixed:
- PLW1510 (86): `check=False` on each subprocess.run, preserving behaviour,
as main's own sweep (0006a5e229) did.
- ASYNC220 (9, test fixtures only): `noqa` with the reason, main's convention
for a Popen that returns at once and whose handle the test drives.
- ruff's safe autofixes plus the narrow unsafe ones (UP006/UP037/RUF100/RUF013/
PLR0402/RUF059 underscore renames/UP031/UP041/UP017/FURB167/PIE807/RUF007/
RUF015/RUF022/FURB162), and by hand: four %-formats to f-strings, three
unused unpacked names, one fixture loop over `.items()`.
No behaviour change. `ruff check .` passes; scripts/check passes; the 412 test
files mirroring every edited file pass (3 tests/pm files that fail only under
that batch's uv lookup pass solo, before and after this commit).
71 lines
3.2 KiB
Python
71 lines
3.2 KiB
Python
"""Fresh-interpreter proof that a safe worker never spawns MCP servers; config-only keeps them."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import textwrap
|
|
|
|
import pytest
|
|
|
|
PEER = '''import json, os, sys
|
|
from pathlib import Path
|
|
Path(os.environ["SPAWNED"]).touch()
|
|
for line in sys.stdin:
|
|
r = json.loads(line); method = r.get("method"); ident = r.get("id")
|
|
if ident is None: continue
|
|
if method == "initialize":
|
|
result = {"protocolVersion": r["params"]["protocolVersion"], "capabilities": {"tools": {}},
|
|
"serverInfo": {"name": "owned-peer", "version": "1"}}
|
|
elif method == "tools/list":
|
|
result = {"tools": [{"name": "echo", "description": "owned echo",
|
|
"inputSchema": {"type": "object", "properties": {}}}]}
|
|
else:
|
|
result = {}
|
|
print(json.dumps({"jsonrpc": "2.0", "id": ident, "result": result}), flush=True)
|
|
'''
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["safe", "config"])
|
|
def test_safe_worker_never_spawns_mcp_servers(tmp_path, mode):
|
|
home = tmp_path / mode
|
|
home.mkdir()
|
|
(home / "peer.py").write_text(PEER, encoding="utf-8")
|
|
spawned = home / "spawned"
|
|
server = {"command": sys.executable, "args": [str(home / "peer.py")],
|
|
"env": {"SPAWNED": str(spawned)}}
|
|
config = {"mcp_servers": {"owned": server}, "plugins": {"enabled": []}}
|
|
(home / "config.yaml").write_text(json.dumps(config), encoding="utf-8")
|
|
root = Path(__file__).resolve().parents[2]
|
|
env = dict(os.environ, HERMES_HOME=str(home), HOME=str(home), PROBE_MODE=mode)
|
|
for key in ("HERMES_SAFE_MODE", "HERMES_IGNORE_USER_CONFIG"):
|
|
env.pop(key, None)
|
|
script = home / "probe.py"
|
|
script.write_text(f"import sys; sys.path.insert(0, {str(root)!r})\n" + textwrap.dedent(f'''
|
|
import json, os
|
|
from agent.safe_worker_policy import _bind_safe_worker_policy
|
|
config = {config!r}
|
|
_bind_safe_worker_policy(safe_mode=os.environ["PROBE_MODE"] == "safe",
|
|
ignore_user_config=True, config=config)
|
|
from tools.mcp_tool_config import _load_mcp_config
|
|
from tools.mcp_tool_discovery import discover_mcp_tools, register_mcp_servers
|
|
loaded = _load_mcp_config()
|
|
explicit = register_mcp_servers({{"editor": {server!r}}})
|
|
discovered = discover_mcp_tools()
|
|
# Registration must not hand back already-existing names either.
|
|
again = register_mcp_servers({{"editor": {server!r}}})
|
|
print(json.dumps({{"loaded": sorted(loaded), "explicit": explicit,
|
|
"discovered": discovered, "again": again}}))
|
|
'''), encoding="utf-8")
|
|
result = subprocess.run([sys.executable, str(script)], cwd=root, env=env, stdin=subprocess.DEVNULL,
|
|
capture_output=True, text=True, timeout=120, check=False)
|
|
assert result.returncode == 0, result.stdout + result.stderr
|
|
out = json.loads(result.stdout.strip().splitlines()[-1])
|
|
if mode == "safe":
|
|
assert out == {"loaded": [], "explicit": [], "discovered": [], "again": []}, out
|
|
assert not spawned.exists()
|
|
else:
|
|
assert out["loaded"] == ["owned"], out
|
|
assert any(n.endswith("__echo") for n in out["explicit"]), out
|
|
assert spawned.exists()
|