From ababc398fa606bd314661b5f3428d556b03694d2 Mon Sep 17 00:00:00 2001 From: Marqdo Extension Fixtures Date: Sun, 22 Nov 2026 20:33:32 +0800 Subject: [PATCH] test: compose a bounded agent with explicit local tool authority --- marqdo-agent/.gitignore | 3 ++ marqdo-agent/README.md | 17 +++++++ marqdo-agent/index.mq.md | 105 +++++++++++++++++++++++++++++++++++++++ marqdo-agent/marqdo.yaml | 7 +++ tests/reference_apps.py | 83 ++++++++++++++++++++++++------- 5 files changed, 198 insertions(+), 17 deletions(-) create mode 100644 marqdo-agent/.gitignore create mode 100644 marqdo-agent/README.md create mode 100644 marqdo-agent/index.mq.md create mode 100644 marqdo-agent/marqdo.yaml diff --git a/marqdo-agent/.gitignore b/marqdo-agent/.gitignore new file mode 100644 index 0000000..836099c --- /dev/null +++ b/marqdo-agent/.gitignore @@ -0,0 +1,3 @@ +/marqdo.lock +/.marqdo/ +/deps/ diff --git a/marqdo-agent/README.md b/marqdo-agent/README.md new file mode 100644 index 0000000..073a850 --- /dev/null +++ b/marqdo-agent/README.md @@ -0,0 +1,17 @@ +# marqdo-agent reference package + +`ask(url, query, tools = [])` uses the pinned `marqdo-net` dependency to +perform a bounded two-turn conversation with the fixture model contract. +The endpoint receives JSON with `messages` and `tools` and responds with a +`model` identity plus an `answer` or a `tool` and `input`. The caller may +enable the pure `sum` tool. The tool input is bounded and checked before +action. A tool response and its model identity are included in provenance. +The second turn asks for the final answer and permits no additional action. + +For a local copy, put `marqdo-net` in `deps/net` under this package, then run +`marqdo lock`, `marqdo sync` and `marqdo check index.mq.md`. The external +conformance suite performs those steps in a temporary directory and operates +a loopback mock service without model credentials. + +The reference executes requests sequentially. The parallel success/failure/ +timeout scenario requires the proposed Task and cancellation contracts. diff --git a/marqdo-agent/index.mq.md b/marqdo-agent/index.mq.md new file mode 100644 index 0000000..be7f24e --- /dev/null +++ b/marqdo-agent/index.mq.md @@ -0,0 +1,105 @@ +--- +import: + http: "@net" + json: "std:data/json" +--- + +# @exec Agent reference + +## ask + +Use a bounded two-turn model conversation. The mock model may request one +local tool explicitly enabled by the caller. Both turns use the same endpoint. +The result includes the model identity and tool outcome as provenance. + +`params(url, query, tools = [])` +`if 长度(query) > 4096` +`return failure("agent.input_limit", "Prompt exceeds the reference agent limit")` +`end` +`first = exchange(url, [{role: "user", content: query}], tools)` +`if not first.ok` +`return first` +`end` +`reply = first.value` +`if map.has(reply, "answer")` +`return answer(reply, [])` +`end` +`if not map.has(reply, "tool") or 类型(reply.tool) != "Text" or not map.has(reply, "input")` +`return failure("agent.response", "Expected an answer or a tool request")` +`end` +`tool = local_tool(reply.tool, reply.input, tools)` +`if not tool.ok` +`return tool` +`end` +`transcript = [{role: "user", content: query}, {role: "assistant", tool: reply.tool, input: reply.input}, {role: "tool", tool: reply.tool, value: tool.value}]` +`second = exchange(url, transcript, [])` +`if not second.ok` +`return second` +`end` +`return answer(second.value, [{model: reply.model, tool: reply.tool, output: tool.value}])` + +## exchange + +`params(url, messages, tools)` +`request = json.格式化({messages: messages, tools: tools})` +`result = http.request(url, "POST", request, "application/json")` +`if not result.ok` +`return result` +`end` +`received = result.value` +`if received.status != 200` +`return failure("agent.http", "Model returned HTTP " + 文本(received.status))` +`end` +`parsed = json.解析(received.body)` +`if not parsed.ok` +`return failure("agent.response", "Model returned invalid JSON")` +`end` +`reply = parsed.value` +`if 类型(reply) != "Map" or not map.has(reply, "model") or 类型(reply.model) != "Text"` +`return failure("agent.response", "Model identity is missing")` +`end` +`return {ok: true, value: reply}` + +## local_tool + +The model describes a request. The caller supplies the explicit local +allowlist, and the library validates the data before choosing the action. + +`params(name, input, tools)` +`allowed = false` +`i = 0` +`while i < 长度(tools)` +`if tools[i] == name` +`allowed = true` +`end` +`i = i + 1` +`end` +`if not allowed or name != "sum"` +`return failure("agent.tool_denied", "Tool is unavailable for this conversation")` +`end` +`if 类型(input) != "List" or 长度(input) > 32` +`return failure("agent.tool_input", "sum expects up to 32 integers")` +`end` +`sum = 0` +`i = 0` +`while i < 长度(input)` +`if 类型(input[i]) != "Int"` +`return failure("agent.tool_input", "sum expects integers")` +`end` +`sum = sum + input[i]` +`i = i + 1` +`end` +`return {ok: true, value: sum}` + +## answer + +`params(reply, tools)` +`if not map.has(reply, "answer") or 类型(reply.answer) != "Text"` +`return failure("agent.response", "Expected the model's final answer")` +`end` +`return {ok: true, value: {answer: reply.answer, model: reply.model, tools: tools}}` + +## failure + +`params(code, message)` +`return {ok: false, code: code, message: message}` diff --git a/marqdo-agent/marqdo.yaml b/marqdo-agent/marqdo.yaml new file mode 100644 index 0000000..44d4889 --- /dev/null +++ b/marqdo-agent/marqdo.yaml @@ -0,0 +1,7 @@ +project: + name: marqdo-agent + version: 0.1.0 + marqdo: ">=3.0.0-beta.1,<4.0.0" +dependencies: + net: + path: "./deps/net" diff --git a/tests/reference_apps.py b/tests/reference_apps.py index e1c57df..3ee2d99 100644 --- a/tests/reference_apps.py +++ b/tests/reference_apps.py @@ -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, }