File size: 8,717 Bytes
0ced74c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
"""LedgerBench-100 world service: Streamable HTTP MCP bridge + token-gated verifier.

One process serves the task's eight finance MCP servers (imported in-process from the
pack's own runtime/) over per-server Streamable HTTP endpoints:

    POST /mcp/<server>   JSON-RPC: initialize | ping | tools/list | tools/call
    GET  /health         readiness for the compose healthcheck
    POST /verify         token-gated deterministic verification (X-Verify-Token)

The agent container never sees the verification token: the pack bakes only its
SHA-256 digest into spec.json. tests/test.sh (copied into the container only at
verification time) holds the actual token.

Everything is offline and deterministic: SQLite world state, frozen WORLD_NOW
clock, stdlib-only, no LLM anywhere.
"""
from __future__ import annotations

import gzip
import hashlib
import importlib.util
import json
import os
import shutil
import sys
from http import HTTPStatus
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from typing import Any

HERE = Path(__file__).resolve().parent
SPEC = json.loads((HERE / "spec.json").read_text(encoding="utf-8"))
RUNTIME = HERE / "runtime"
RUN_DIR = Path(os.environ.get("LEDGERBENCH_RUN_DIR", "/opt/world/run"))
PROTOCOL_VERSION = "2025-06-18"

_SERVERS: dict[str, Any] = {}


def load_servers() -> dict[str, Any]:
    """Import the pack's MCP server modules in-process (same handlers, same trace)."""
    if _SERVERS:
        return _SERVERS
    sys.path.insert(0, str(RUNTIME / "lib"))
    for name in SPEC["servers"]:
        spec = importlib.util.spec_from_file_location(
            f"lgr_{name}_server", RUNTIME / "servers" / f"{name}_server.py"
        )
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        _SERVERS[name] = module.S
    return _SERVERS


def reset_run_dir() -> None:
    """Materialize a pristine run: fresh world DB, empty trace, initial hashes."""
    RUN_DIR.mkdir(parents=True, exist_ok=True)
    state = HERE / "state"
    db_gz = state / "world.sqlite.gz"
    db = state / "world.sqlite"
    target = RUN_DIR / "world.sqlite"
    if db.exists():
        shutil.copyfile(db, target)
    else:
        with gzip.open(db_gz, "rb") as fin, open(target, "wb") as fout:
            shutil.copyfileobj(fin, fout)
    shutil.copyfile(state / "initial_state.json", RUN_DIR / "initial_state.json")
    (RUN_DIR / "trace.jsonl").write_text("")
    os.environ["WORLD_DB"] = str(target)
    os.environ["WORLD_NOW"] = SPEC["world_now"]
    os.environ["WORLD_ROLE"] = SPEC["world_role"]
    os.environ["TRACE_FILE"] = str(RUN_DIR / "trace.jsonl")


def build_report(task_dir: Path, run_dir: Path, task_id: str) -> dict[str, Any]:
    """Deterministic verification report (no clock, no randomness, no network)."""
    sys.path.insert(0, str(RUNTIME))
    from vcode import verify_all  # the pack's own deterministic verifier engine

    verdict = verify_all(str(task_dir), str(run_dir))
    report = {
        "benchmark": SPEC["benchmark"],
        "version": SPEC["version"],
        "task_id": task_id,
        "passed": verdict["reward"] == 1,
        "reward": float(verdict["reward"]),
        "failed_checks": sorted(verdict["failed"]),
        "n_tool_calls": verdict["n_tool_calls"],
        "servers_used": verdict["servers_used"],
        "steps_graded": verdict.get("steps", 1),
    }
    report["report_sha256"] = hashlib.sha256(
        json.dumps(report, sort_keys=True, separators=(",", ":")).encode("utf-8")
    ).hexdigest()
    return report


def rpc_response(server_name: str, request: dict[str, Any]) -> dict[str, Any] | None:
    request_id = request.get("id")
    method = request.get("method")
    if request_id is None and isinstance(method, str) and method.startswith("notifications/"):
        return None
    if request.get("jsonrpc") != "2.0" or not isinstance(method, str):
        return {"jsonrpc": "2.0", "id": request_id,
                "error": {"code": -32600, "message": "Invalid Request"}}
    servers = load_servers()
    if server_name not in servers:
        return {"jsonrpc": "2.0", "id": request_id,
                "error": {"code": -32601, "message": f"unknown server {server_name!r}"}}
    srv = servers[server_name]
    if method == "initialize":
        params = request.get("params") or {}
        return {"jsonrpc": "2.0", "id": request_id, "result": {
            "protocolVersion": params.get("protocolVersion", PROTOCOL_VERSION),
            "capabilities": {"tools": {}},
            "serverInfo": {"name": srv.name, "version": "1.0.0"},
            "instructions": srv.description,
        }}
    if method == "ping":
        return {"jsonrpc": "2.0", "id": request_id, "result": {}}
    if method == "tools/list":
        return {"jsonrpc": "2.0", "id": request_id,
                "result": {"tools": [schema for _, schema in srv.tools.values()]}}
    if method == "tools/call":
        params = request.get("params") or {}
        tool = params.get("name")
        arguments = params.get("arguments") or {}
        if tool not in srv.tools:
            return {"jsonrpc": "2.0", "id": request_id, "result": {
                "content": [{"type": "text", "text": f"unknown tool {tool}"}],
                "isError": True}}
        try:
            out = srv.call(tool, arguments)
            return {"jsonrpc": "2.0", "id": request_id, "result": {
                "content": [{"type": "text", "text": json.dumps(out, default=str)}],
                "isError": False}}
        except Exception as error:  # noqa: BLE001 - surface tool failure to the agent
            return {"jsonrpc": "2.0", "id": request_id, "result": {
                "content": [{"type": "text", "text": f"error: {error!r}"}],
                "isError": True}}
    return {"jsonrpc": "2.0", "id": request_id,
            "error": {"code": -32601, "message": "Method not found"}}


class Handler(BaseHTTPRequestHandler):
    server_version = "LedgerBenchWorld/1.0"

    def log_message(self, fmt: str, *args: Any) -> None:  # noqa: A003
        return

    def _json(self, status: int, value: Any) -> None:
        payload = json.dumps(value, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
        self.send_response(status)
        self.send_header("Content-Type", "application/json")
        self.send_header("Content-Length", str(len(payload)))
        self.send_header("MCP-Protocol-Version", PROTOCOL_VERSION)
        self.end_headers()
        self.wfile.write(payload)

    def do_GET(self) -> None:  # noqa: N802
        if self.path == "/health":
            self._json(HTTPStatus.OK, {"status": "ok", "task_id": SPEC["task_id"]})
            return
        self._json(HTTPStatus.NOT_FOUND, {"error": "not_found"})

    def do_POST(self) -> None:  # noqa: N802
        length = int(self.headers.get("Content-Length", "0"))
        body = self.rfile.read(length)
        if self.path == "/verify":
            token = self.headers.get("X-Verify-Token") or ""
            digest = hashlib.sha256(token.encode("utf-8")).hexdigest()
            if digest != SPEC["verify_token_sha256"]:
                self._json(HTTPStatus.NOT_FOUND, {"error": "not_found"})
                return
            self._json(HTTPStatus.OK,
                       build_report(HERE / "taskspec", RUN_DIR, SPEC["task_id"]))
            return
        if not self.path.startswith("/mcp/"):
            self._json(HTTPStatus.NOT_FOUND, {"error": "not_found"})
            return
        server_name = self.path[len("/mcp/"):].strip("/")
        try:
            request = json.loads(body.decode("utf-8"))
        except (UnicodeError, json.JSONDecodeError):
            self._json(HTTPStatus.BAD_REQUEST,
                       {"jsonrpc": "2.0", "id": None,
                        "error": {"code": -32700, "message": "Parse error"}})
            return
        if isinstance(request, list):
            responses = [r for item in request
                         if (r := rpc_response(server_name, item)) is not None]
            self._json(HTTPStatus.OK, responses)
            return
        response = rpc_response(server_name, request)
        if response is None:
            self.send_response(HTTPStatus.ACCEPTED)
            self.send_header("Content-Length", "0")
            self.end_headers()
            return
        self._json(HTTPStatus.OK, response)


def main() -> None:
    reset_run_dir()
    load_servers()
    host = os.environ.get("LEDGERBENCH_HOST", "0.0.0.0")
    port = int(os.environ.get("LEDGERBENCH_PORT", "8974"))
    ThreadingHTTPServer((host, port), Handler).serve_forever()


if __name__ == "__main__":
    main()