test: compose a bounded agent with explicit local tool authority

This commit is contained in:
Marqdo Extension Fixtures committed 2026-11-22 20:33:32 +08:00
1 parent 943f3b5275
commit ababc398fa
5 files changed
+198 -17

No files matched your search

+66 -17
View File
@@ -1,10 +1,11 @@
"""Exercise the pure Marqdo network package against one frozen CLI."""
"""Exercise the pure Marqdo network and agent packages against one frozen CLI."""
from __future__ import annotations
import base64
import json
import os
import shutil
import socket
import ssl
import subprocess
@@ -65,7 +66,7 @@ def read_request(connection):
@contextmanager
def server(respond, *, tls=None, expect_tls_refusal=False):
def server(respond, *, tls=None, expect_tls_refusal=False, request_count=1):
errors = []
with socket.create_server(("127.0.0.1", 0)) as listener:
listener.settimeout(10)
@@ -74,21 +75,22 @@ def server(respond, *, tls=None, expect_tls_refusal=False):
def serve():
try:
connection, _ = listener.accept()
with connection:
connection.settimeout(8)
if tls is None:
respond(connection)
else:
try:
secured = tls.wrap_socket(connection, server_side=True)
except ssl.SSLError:
if expect_tls_refusal:
return
raise
with secured:
assert not expect_tls_refusal, "untrusted TLS was accepted"
respond(secured)
for _ in range(request_count):
connection, _ = listener.accept()
with connection:
connection.settimeout(8)
if tls is None:
respond(connection)
else:
try:
secured = tls.wrap_socket(connection, server_side=True)
except ssl.SSLError:
if expect_tls_refusal:
return
raise
with secured:
assert not expect_tls_refusal, "untrusted TLS was accepted"
respond(secured)
except Exception as exc:
errors.append(exc)
@@ -260,12 +262,59 @@ def timeout(cli):
assert result.returncode == 1 and "tcp read failed:" in result.stderr, result
def agent(cli):
with tempfile.TemporaryDirectory(prefix="marqdo-agent-") as name:
project = Path(name) / "agent"
shutil.copytree(ROOT / "marqdo-agent", project)
shutil.copytree(ROOT / "marqdo-net", project / "deps" / "net")
for command in (["lock"], ["sync"], ["check", "index.mq.md"]):
result = subprocess.run([str(cli), *command], cwd=project, text=True,
capture_output=True, timeout=10)
assert result.returncode == 0, result.stderr
def ask(url, tools):
command = args(cli, project / "index.mq.md", "ask", url,
query="How many?", tools=tools)
return typed_value(subprocess.run(command + ["--json"], cwd=project,
capture_output=True, text=True, timeout=20))
calls = []
def mock(connection):
line, headers, body = read_request(connection)
assert line == "POST / HTTP/1.1" and headers["Accept"] == "application/json"
item = json.loads(body)
calls.append(item)
if len(item["messages"]) == 1:
response = {"model": "test-model", "tool": "sum", "input": [20, 22]}
else:
assert item["tools"] == []
assert item["messages"][-1] == {"role": "tool", "tool": "sum", "value": 42}
response = {"model": "test-model", "answer": "42"}
connection.sendall(fixed(200, json.dumps(response).encode(), "application/json"))
with server(mock, request_count=2) as url:
successful = ask(url, ["sum"])
assert len(calls) == 2 and calls[0]["tools"] == ["sum"]
assert_result(successful["value"], answer="42", model="test-model",
tools=[{"model": "test-model", "tool": "sum", "output": 42}])
calls.clear()
with server(mock) as url:
rejected = ask(url, [])
assert_result(rejected, ok=False, code="agent.tool_denied")
assert len(calls) == 1
with server(reply([fixed(503, b"unavailable")])) as url:
unavailable = ask(url, ["sum"])
assert_result(unavailable, ok=False, code="agent.http")
CASES = {
"HTTP status, POST and bounded framing": http,
"URL validation and denial before connect": denied,
"TLS with an explicit local trust root": tls_http,
"live SSE visibility and caller close": live_sse,
"socket read timeout and teardown": timeout,
"agent tool authority and two-turn provenance": agent,
}