omega-mcp
aien-dev/omega-mcp
Brought by drakestapleton
omega-mcp / gates / upgrade.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Upgrade-under-a-running-server gate for omega-mcp (verification oracle; not part of the server). |
| 3 | |
| 4 | A running server re-executes its own executable file as the in-sandbox reaper of every compile and run, so an |
| 5 | install that puts a different file at the path a running server started from breaks that server (observed |
| 6 | 2026-11-11: "the sandbox ended without a status record"). tools/install.sh therefore only adds releases. In a |
| 7 | temporary PREFIX this gate checks, through tools/call: |
| 8 | 1 a legacy server started from PREFIX/omega-mcp (the pre-release layout) keeps compiling and running across |
| 9 | two installs, and the legacy files are unchanged; |
| 10 | 2 a server started from PREFIX/current/omega-mcp keeps compiling and running after the next install switches |
| 11 | PREFIX/current, while a fresh start of PREFIX/current/omega-mcp runs from the new release; |
| 12 | 3 a second install of a release a live server runs from is refused (exit 2) and names that server's pid, and so |
| 13 | is an install into a PREFIX whose current is not a symlink; |
| 14 | 4 tools/releases.sh lists the live pids per release and the legacy files, and a release becomes RETIRABLE only |
| 15 | once its last server has exited and it is not current. |
| 16 | Negative control: the in-place procedure this gate exists to catch. The executable file of a running server is |
| 17 | replaced (by rename, so the process keeps its old image) with a copy of /bin/true; that server's next compile |
| 18 | must fail, so a gate that could not see the breakage fails. |
| 19 | |
| 20 | python3 gates/upgrade.py [--out evidence/upgrade-gate.txt] |
| 21 | Needs build/ from ./build.sh. Exit 0 PASS, 1 FAIL. |
| 22 | """ |
| 23 | import argparse, json, os, shutil, subprocess, sys, tempfile |
| 24 | |
| 25 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 26 | ROOT = os.path.dirname(HERE) |
| 27 | PIN = open(os.path.join(ROOT, "PIN")).read().split()[3] # `omega-mv REV` / `omc-sha256 SHA` |
| 28 | SRC = "fn main to int\nret 7\nend\n" |
| 29 | |
| 30 | |
| 31 | class Server: |
| 32 | def __init__(self, path): |
| 33 | self.p = subprocess.Popen([path], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 34 | self.i = 0 |
| 35 | self.call("initialize", {"protocolVersion": "2025-06-18", "capabilities": {}, |
| 36 | "clientInfo": {"name": "omega-mcp-upgrade", "version": "1"}}) |
| 37 | self.p.stdin.write(b'{"jsonrpc":"2.0","method":"notifications/initialized"}\n') |
| 38 | self.p.stdin.flush() |
| 39 | |
| 40 | def call(self, method, params): |
| 41 | self.i += 1 |
| 42 | self.p.stdin.write((json.dumps({"jsonrpc": "2.0", "id": self.i, "method": method, "params": params}) + "\n").encode()) |
| 43 | self.p.stdin.flush() |
| 44 | return json.loads(self.p.stdout.readline()) |
| 45 | |
| 46 | def works(self): |
| 47 | """(compile ok and run exit normal 7, compiler_sha256)""" |
| 48 | c = self.call("tools/call", {"name": "compile", "arguments": {"source": SRC}})["result"]["structuredContent"] |
| 49 | r = self.call("tools/call", {"name": "run", "arguments": {"source": SRC}})["result"]["structuredContent"] |
| 50 | return c.get("ok") is True and r.get("exit") == {"kind": "normal", "code": 7}, c.get("compiler_sha256") |
| 51 | |
| 52 | def exe(self): |
| 53 | return os.readlink(f"/proc/{self.p.pid}/exe") |
| 54 | |
| 55 | def stop(self): |
| 56 | self.p.stdin.close() |
| 57 | self.p.wait(30) |
| 58 | |
| 59 | |
| 60 | def sha(path): |
| 61 | return subprocess.run(["sha256sum", path], capture_output=True, text=True).stdout[:64] |
| 62 | |
| 63 | |
| 64 | def main(): |
| 65 | ap = argparse.ArgumentParser() |
| 66 | ap.add_argument("--out", default=None) |
| 67 | a = ap.parse_args() |
| 68 | lines, fails = [], [] |
| 69 | |
| 70 | def check(name, ok): |
| 71 | line = ("PASS " if ok else "FAIL ") + name |
| 72 | print(line, flush=True) |
| 73 | lines.append(line) |
| 74 | if not ok: |
| 75 | fails.append(name) |
| 76 | |
| 77 | install = os.path.join(ROOT, "tools", "install.sh") |
| 78 | releases = os.path.join(ROOT, "tools", "releases.sh") |
| 79 | build = os.path.join(ROOT, "build") |
| 80 | with tempfile.TemporaryDirectory() as t: |
| 81 | pre = os.path.join(t, "build") |
| 82 | os.mkdir(pre) |
| 83 | # the pre-release layout: the server, its compiler and data directly in PREFIX |
| 84 | for f in ("omega-mcp", "omc"): |
| 85 | shutil.copy2(os.path.join(build, f), os.path.join(pre, f)) |
| 86 | shutil.copytree(os.path.join(build, "data"), os.path.join(pre, "data")) |
| 87 | legacy_sha = sha(os.path.join(pre, "omega-mcp")) |
| 88 | inst = lambda name: subprocess.run([install, pre, name], capture_output=True, text=True) |
| 89 | rel = lambda: subprocess.run([releases, pre], capture_output=True, text=True).stdout |
| 90 | |
| 91 | s0 = Server(os.path.join(pre, "omega-mcp")) |
| 92 | ok, h = s0.works() |
| 93 | check(f"legacy server (exe PREFIX/omega-mcp) compiles and runs before any install ({h[:8]})", ok and h == PIN) |
| 94 | r = inst("r1") |
| 95 | check("install r1: exit 0, PREFIX/current -> releases/r1", |
| 96 | r.returncode == 0 and os.readlink(os.path.join(pre, "current")) == "releases/r1") |
| 97 | s1 = Server(os.path.join(pre, "current", "omega-mcp")) |
| 98 | check("server started from PREFIX/current runs from releases/r1", |
| 99 | s1.exe() == os.path.join(pre, "releases", "r1", "omega-mcp")) |
| 100 | r = inst("r2") |
| 101 | check("install r2: exit 0, PREFIX/current -> releases/r2, current.rollback -> releases/r1", |
| 102 | r.returncode == 0 and os.readlink(os.path.join(pre, "current")) == "releases/r2" |
| 103 | and os.readlink(os.path.join(pre, "current.rollback")) == "releases/r1") |
| 104 | ok, h = s0.works() |
| 105 | check(f"legacy server still compiles and runs after two installs ({h[:8]})", ok and h == PIN) |
| 106 | check("legacy PREFIX/omega-mcp unchanged", sha(os.path.join(pre, "omega-mcp")) == legacy_sha) |
| 107 | ok, h = s1.works() |
| 108 | check(f"r1 server still compiles and runs after the switch to r2 ({h[:8]})", ok and h == PIN) |
| 109 | s2 = Server(os.path.join(pre, "current", "omega-mcp")) |
| 110 | ok, h = s2.works() |
| 111 | check(f"fresh start of PREFIX/current runs from releases/r2 and compiles ({h[:8]})", |
| 112 | ok and h == PIN and s2.exe() == os.path.join(pre, "releases", "r2", "omega-mcp")) |
| 113 | r = inst("r2") |
| 114 | check(f"second install of r2 refused (exit 2) naming live pid {s2.p.pid}", |
| 115 | r.returncode == 2 and "refused" in r.stderr and str(s2.p.pid) in r.stderr) |
| 116 | out = rel() |
| 117 | check("releases.sh: legacy, r1 and r2 IN USE with their pids, retirable 0", |
| 118 | f"pids: {s0.p.pid}" in out and f"pids: {s1.p.pid}" in out and f"pids: {s2.p.pid}" in out |
| 119 | and out.strip().endswith("retirable 0")) |
| 120 | s1.stop() |
| 121 | out = rel() |
| 122 | r1row = [l for l in out.splitlines() if "/releases/r1 " in l] |
| 123 | check("after the r1 server exits, releases.sh marks r1 RETIRABLE (and deletes nothing)", |
| 124 | len(r1row) == 1 and r1row[0].endswith("RETIRABLE") and out.strip().endswith("retirable 1") |
| 125 | and os.path.isdir(os.path.join(pre, "releases", "r1"))) |
| 126 | other = os.path.join(t, "other") |
| 127 | os.makedirs(os.path.join(other, "current")) |
| 128 | r = subprocess.run([install, other, "r1"], capture_output=True, text=True) |
| 129 | check("install into a PREFIX whose current is a directory is refused (exit 2)", r.returncode == 2 and "refused" in r.stderr) |
| 130 | |
| 131 | # negative control: replace a running server's executable file in place (rename over it) |
| 132 | exe = os.path.join(pre, "releases", "r2", "omega-mcp") |
| 133 | shutil.copy2("/bin/true", exe + ".swap") |
| 134 | os.rename(exe + ".swap", exe) |
| 135 | ok, h = s2.works() |
| 136 | check("negative control: after an in-place replacement of its executable the r2 server can no longer compile (detected)", not ok) |
| 137 | ok, h = s0.works() |
| 138 | check("legacy server unaffected by the control", ok) |
| 139 | s2.stop() |
| 140 | s0.stop() |
| 141 | print("PASS" if not fails else "FAIL", flush=True) |
| 142 | if a.out: |
| 143 | with open(a.out, "w") as f: |
| 144 | f.write("\n".join(lines) + "\n" + ("PASS" if not fails else "FAIL") + "\n") |
| 145 | return 0 if not fails else 1 |
| 146 | |
| 147 | |
| 148 | if __name__ == "__main__": |
| 149 | sys.exit(main()) |