omega-mcp
aien-dev/omega-mcp
Brought by drakestapleton
| 1 | #!/usr/bin/env python3 |
| 2 | """Stdio smoke test for omega-mcp (verification oracle; a minimal MCP client, not part of the server). |
| 3 | |
| 4 | Starts build/omega-mcp, speaks newline-delimited JSON-RPC 2.0 over its stdin/stdout the way an MCP |
| 5 | client does, and records every line in both directions: |
| 6 | initialize -> notifications/initialized -> tools/list -> tools/call compile hello.om -> |
| 7 | tools/call run hello.om -> run (trap) -> compile (error 3) -> explain_error -> lookup, |
| 8 | plus protocol negatives (unknown method, unparseable line, unknown argument, unknown tool). |
| 9 | |
| 10 | The pinned omc is the one the PIN file names; every compile result must report its sha256. |
| 11 | Pin case: a program with a 20-byte variable name and `for i ulong n bound 8` must compile and run through |
| 12 | tools/call (exit 10), and the same loop past its bound must trap 4. Its negative control runs the omc of |
| 13 | gates/fixtures/pin-case-refuser (an older compiler, read with `git show` from the omega-mv clone given by |
| 14 | --omega-src or $OMEGA_SRC) on the same source and requires `omc: line 3: error 13`, |
| 15 | so the case cannot pass against a compiler that does not accept long names. No omega-mv clone is a FAIL. |
| 16 | No-caps case: a unit with 20,000 `text` records and a main with 20,000 calls must compile and run through |
| 17 | tools/call (stdout 'ok\n', exit 36), inside the server's request limits (maxsrc and maxline, read from |
| 18 | src/state.om and printed with the sizes). Its negative control runs the omc of |
| 19 | gates/fixtures/nocaps-case-refuser (8192 names in scope) on the same unit and requires |
| 20 | `omc: line 8193: error 14`. A transcript line over 64 KiB is recorded as its byte count and sha256. |
| 21 | |
| 22 | python3 gates/smoke.py [--server build/omega-mcp] [--out evidence/smoke-transcript.jsonl] [--omega-src DIR] |
| 23 | Exit 0 PASS, 1 FAIL. |
| 24 | """ |
| 25 | import argparse, hashlib, json, os, subprocess, sys, tempfile |
| 26 | |
| 27 | |
| 28 | def read_pin(path): |
| 29 | """(omega-mv revision, omc sha256) from a file in the PIN format: `omega-mv REV` and `omc-sha256 SHA`.""" |
| 30 | k = open(path).read().split() |
| 31 | if len(k) != 4 or k[0] != "omega-mv" or k[2] != "omc-sha256": |
| 32 | raise SystemExit(f"{path}: expected `omega-mv REV` and `omc-sha256 SHA`") |
| 33 | return k[1], k[3] |
| 34 | |
| 35 | HELLO = 'text msg "hello\\n"\nfn main to int\nw = write 1 msg 0 len msg\nret 0\nend\n' |
| 36 | DIV0 = 'fn main to int\nz = int 0\nret div 7 z\nend\n' |
| 37 | BADNAME = 'fn main to int\nret x\nend\n' |
| 38 | ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") |
| 39 | PIN = read_pin(os.path.join(ROOT, "PIN"))[1] |
| 40 | LONGNAME = "twenty_byte_variable" |
| 41 | assert len(LONGNAME) == 20 |
| 42 | PINCASE = ("fn main to int\nn = ulong 5\n" + LONGNAME + " = ulong 0\nfor i ulong n bound 8\n" |
| 43 | + LONGNAME + " = add " + LONGNAME + " i\nend\nexit int " + LONGNAME + "\nend\n") |
| 44 | OVERBOUND = PINCASE.replace("n = ulong 5", "n = ulong 50") |
| 45 | OLD_REV, OLD_SHA = read_pin(os.path.join(ROOT, "gates", "fixtures", "pin-case-refuser")) |
| 46 | NC_N = 20000 |
| 47 | |
| 48 | |
| 49 | def nocaps_unit(n): |
| 50 | """n texts t0..t(n-1) and a main with n calls of inc; it writes the last text 'ok\\n' and exits |
| 51 | (n + len t0 + len t(n-1)) mod 256. The omc of gates/fixtures/nocaps-case-refuser held 8192 names in |
| 52 | scope, so text 8193 (line 8193) is its error 14.""" |
| 53 | u = ['text t0 "a"'] + [f'text t{i} "x"' for i in range(1, n - 1)] + [f'text t{n - 1} "ok\\n"'] |
| 54 | u += ["fn inc x ulong to ulong", "ret add x 1", "end", "fn main to int", "x = ulong 0"] + ["x = inc x"] * n |
| 55 | u += [f"w = write 1 t{n - 1} 0 len t{n - 1}", f"x = add x add len t0 len t{n - 1}", "ret int rem x 256", "end"] |
| 56 | return "\n".join(u) + "\n" |
| 57 | |
| 58 | |
| 59 | NOCAPS = nocaps_unit(NC_N) |
| 60 | NC_EXIT = (NC_N + 1 + 3) % 256 |
| 61 | NC_REV, NC_SHA = read_pin(os.path.join(ROOT, "gates", "fixtures", "nocaps-case-refuser")) |
| 62 | |
| 63 | |
| 64 | def server_limit(name): |
| 65 | """A `const NAME ulong N` of src/state.om (the server's request limits).""" |
| 66 | for line in open(os.path.join(ROOT, "src", "state.om")): |
| 67 | k = line.split() |
| 68 | if len(k) == 4 and k[0] == "const" and k[1] == name and k[2] == "ulong": |
| 69 | return int(k[3]) |
| 70 | raise SystemExit(f"src/state.om has no const {name}") |
| 71 | |
| 72 | |
| 73 | def old_omc_refuses(omega_src, rev, sha, unit, code, line): |
| 74 | """Negative control: omc `sha` at omega-mv `rev` must refuse `unit` with error `code` on `line`.""" |
| 75 | if not omega_src: |
| 76 | return False, "no omega-mv clone (--omega-src or OMEGA_SRC)" |
| 77 | r = subprocess.run(["git", "-C", omega_src, "show", rev + ":seed/out/omc"], capture_output=True) |
| 78 | if r.returncode != 0 or hashlib.sha256(r.stdout).hexdigest() != sha: |
| 79 | return False, f"could not read omc {sha[:8]} at omega-mv {rev[:8]}" |
| 80 | with tempfile.TemporaryDirectory() as d: |
| 81 | omc = os.path.join(d, "omc-old") |
| 82 | with open(omc, "wb") as f: |
| 83 | f.write(r.stdout) |
| 84 | os.chmod(omc, 0o700) |
| 85 | c = subprocess.run([omc], input=unit.encode(), capture_output=True, timeout=60) |
| 86 | want = f"omc: line {line}: error {code}\n".encode() |
| 87 | return c.returncode == code and c.stderr.startswith(want), f"rc {c.returncode} {c.stderr[:60]!r}" |
| 88 | |
| 89 | |
| 90 | def main(): |
| 91 | here = os.path.dirname(os.path.abspath(__file__)) |
| 92 | ap = argparse.ArgumentParser() |
| 93 | ap.add_argument("--server", default=os.path.join(here, "..", "build", "omega-mcp")) |
| 94 | ap.add_argument("--out", default=os.path.join(here, "..", "evidence", "smoke-transcript.jsonl")) |
| 95 | ap.add_argument("--omega-src", default=os.environ.get("OMEGA_SRC")) |
| 96 | a = ap.parse_args() |
| 97 | p = subprocess.Popen([a.server], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 98 | log, fails, sent = [], [], [0] |
| 99 | |
| 100 | def note(d, line): |
| 101 | b = line.encode() |
| 102 | if len(b) > 65536: |
| 103 | log.append({"dir": d, "bytes": len(b), "sha256": hashlib.sha256(b).hexdigest()}) |
| 104 | else: |
| 105 | log.append({"dir": d, "line": line}) |
| 106 | |
| 107 | def send(raw): |
| 108 | note("client->server", raw) |
| 109 | sent[0] = len(raw.encode()) + 1 |
| 110 | p.stdin.write((raw + "\n").encode()) |
| 111 | p.stdin.flush() |
| 112 | |
| 113 | def recv(): |
| 114 | line = p.stdout.readline().decode().rstrip("\n") |
| 115 | note("server->client", line) |
| 116 | return json.loads(line) |
| 117 | |
| 118 | def call(i, method, params=None): |
| 119 | m = {"jsonrpc": "2.0", "id": i, "method": method} |
| 120 | if params is not None: |
| 121 | m["params"] = params |
| 122 | send(json.dumps(m)) |
| 123 | r = recv() |
| 124 | if r.get("id") != i or r.get("jsonrpc") != "2.0": |
| 125 | fails.append(f"id {i}: bad envelope {r}") |
| 126 | return r |
| 127 | |
| 128 | def check(name, ok): |
| 129 | print(("PASS " if ok else "FAIL ") + name, flush=True) |
| 130 | if not ok: |
| 131 | fails.append(name) |
| 132 | |
| 133 | tool = lambda i, n, args: call(i, "tools/call", {"name": n, "arguments": args}) |
| 134 | |
| 135 | r = call(1, "initialize", {"protocolVersion": "2025-06-18", "capabilities": {}, |
| 136 | "clientInfo": {"name": "omega-mcp-smoke", "version": "1"}}) |
| 137 | res = r.get("result", {}) |
| 138 | check("initialize: protocolVersion 2025-06-18, serverInfo omega-mcp, tools capability", |
| 139 | res.get("protocolVersion") == "2025-06-18" and res.get("serverInfo", {}).get("name") == "omega-mcp" |
| 140 | and "tools" in res.get("capabilities", {})) |
| 141 | send(json.dumps({"jsonrpc": "2.0", "method": "notifications/initialized"})) |
| 142 | |
| 143 | r = call(2, "tools/list", {}) |
| 144 | names = [t["name"] for t in r.get("result", {}).get("tools", [])] |
| 145 | check("tools/list: compile, run, explain_error, lookup", names == ["compile", "run", "explain_error", "lookup"]) |
| 146 | |
| 147 | sc = tool(3, "compile", {"source": HELLO})["result"]["structuredContent"] |
| 148 | check("compile hello.om: ok, pinned compiler", |
| 149 | sc.get("ok") is True and sc.get("compiler_sha256") == PIN) |
| 150 | |
| 151 | r = tool(4, "run", {"source": HELLO})["result"] |
| 152 | sc = r["structuredContent"] |
| 153 | check("run hello.om: stdout 'hello\\n', exit normal 0, isError false", |
| 154 | sc.get("stdout") == "hello\n" and sc.get("exit") == {"kind": "normal", "code": 0} and r.get("isError") is False) |
| 155 | |
| 156 | sc = tool(5, "run", {"source": DIV0})["result"]["structuredContent"] |
| 157 | check("run div0: trap 2, exit 102", sc.get("exit", {}).get("kind") == "trap" and sc["exit"].get("trap") == 2 |
| 158 | and sc["exit"].get("exit_code") == 102) |
| 159 | |
| 160 | r = tool(6, "compile", {"source": BADNAME})["result"] |
| 161 | sc = r["structuredContent"] |
| 162 | check("compile unknown name: error 3 line 2 (a diagnostic is a tool result: isError false)", |
| 163 | sc.get("ok") is False and sc.get("error", {}).get("code") == 3 and sc["error"].get("line") == 2 |
| 164 | and r.get("isError") is False) |
| 165 | |
| 166 | sc = tool(7, "explain_error", {"query": "omc: line 2: error 3"})["result"]["structuredContent"] |
| 167 | check("explain_error 'omc: line 2: error 3': compile_error 3 with citations", |
| 168 | sc.get("kind") == "compile_error" and sc.get("code") == 3 and len(sc.get("citations", [])) > 0) |
| 169 | |
| 170 | sc = tool(8, "lookup", {"query": "while"})["result"]["structuredContent"] |
| 171 | check("lookup while: exact keyword", sc.get("match") == "keyword" and sc.get("name") == "while") |
| 172 | |
| 173 | sc = tool(12, "compile", {"source": PINCASE})["result"]["structuredContent"] |
| 174 | check("pin case compile: 20-byte name and for ... bound 8 accepted by the pinned compiler", |
| 175 | sc.get("ok") is True and sc.get("compiler_sha256") == PIN) |
| 176 | r = tool(13, "run", {"source": PINCASE})["result"] |
| 177 | sc = r["structuredContent"] |
| 178 | check("pin case run: 0+1+2+3+4, exit normal 10", sc.get("exit") == {"kind": "normal", "code": 10} and r.get("isError") is False) |
| 179 | sc = tool(14, "run", {"source": OVERBOUND})["result"]["structuredContent"] |
| 180 | check("pin case past its bound (n 50, bound 8): trap 4, exit 104", |
| 181 | sc.get("exit", {}).get("kind") == "trap" and sc["exit"].get("trap") == 4 and sc["exit"].get("exit_code") == 104) |
| 182 | ok, why = old_omc_refuses(a.omega_src, OLD_REV, OLD_SHA, PINCASE, 13, 3) |
| 183 | check(f"pin case negative control: omc {OLD_SHA[:8]} (omega-mv {OLD_REV[:8]}) refuses it, error 13 line 3 ({why})", ok) |
| 184 | |
| 185 | sc = tool(15, "compile", {"source": NOCAPS})["result"]["structuredContent"] |
| 186 | line, maxsrc, maxline = sent[0], server_limit("maxsrc"), server_limit("maxline") |
| 187 | check(f"no-caps case compile: {NC_N} texts and {NC_N} calls accepted by the pinned compiler" |
| 188 | f" ({sc.get('error')!r})", sc.get("ok") is True and sc.get("compiler_sha256") == PIN) |
| 189 | check(f"no-caps case size: source {len(NOCAPS)} bytes of maxsrc {maxsrc}, request line {line} bytes" |
| 190 | f" of maxline {maxline} (src/state.om)", len(NOCAPS) <= maxsrc and line <= maxline) |
| 191 | r = tool(16, "run", {"source": NOCAPS})["result"] |
| 192 | sc = r["structuredContent"] |
| 193 | check(f"no-caps case run: stdout 'ok\\n', exit normal {NC_EXIT} ({NC_N} + 1 + 3 mod 256)", |
| 194 | sc.get("stdout") == "ok\n" and sc.get("exit") == {"kind": "normal", "code": NC_EXIT} and r.get("isError") is False) |
| 195 | ok, why = old_omc_refuses(a.omega_src, NC_REV, NC_SHA, NOCAPS, 14, 8193) |
| 196 | check(f"no-caps case negative control: omc {NC_SHA[:8]} (omega-mv {NC_REV[:8]}) refuses it, error 14 line 8193 ({why})", ok) |
| 197 | |
| 198 | r = call(9, "no/such/method", {}) |
| 199 | check("unknown method: -32601", r.get("error", {}).get("code") == -32601) |
| 200 | send("not json{") |
| 201 | r = recv() |
| 202 | check("unparseable line: -32700, id null", r.get("error", {}).get("code") == -32700 and r.get("id") is None) |
| 203 | r = tool(10, "lookup", {"word": "while"}) |
| 204 | check("unknown argument: -32602", r.get("error", {}).get("code") == -32602) |
| 205 | r = tool(11, "format_disk", {}) |
| 206 | check("unknown tool: -32602", r.get("error", {}).get("code") == -32602) |
| 207 | |
| 208 | p.stdin.close() |
| 209 | extra = p.stdout.read().decode() |
| 210 | err = p.stderr.read().decode() |
| 211 | rc = p.wait(30) |
| 212 | check("no reply to the notification and clean exit 0 at end of input", extra == "" and rc == 0) |
| 213 | note("server stderr", err) |
| 214 | os.makedirs(os.path.dirname(os.path.abspath(a.out)), exist_ok=True) |
| 215 | with open(a.out, "w") as f: |
| 216 | for e in log: |
| 217 | f.write(json.dumps(e) + "\n") |
| 218 | print("PASS" if not fails else "FAIL", f"transcript {a.out}") |
| 219 | return 0 if not fails else 1 |
| 220 | |
| 221 | |
| 222 | if __name__ == "__main__": |
| 223 | sys.exit(main()) |