Files
hx/tests/fake_m5.py
T
hermes f4e55c8dd3
ci / check (pull_request) Canceled after 0s
ci / check (push) Canceled after 0s
M5: affected tests at turn end (closes #35 #36 #37 #38 #39 #40 #41)
2026-09-30 15:03:17 +00:00

60 lines
2.4 KiB
Python

"""M5 e2e: fake model writes a Rust fn with a failing unit test; hx must inject the failing test at turn
end; the fake model only fixes the file if it sees the failure, then expects a green notice.
Usage: python3 tests/fake_m5.py PORT
"""
import http.server, json, sys
LOG = []
BAD = "pub fn add(a: u32) -> u32 {\n a + 2\n}\n#[test]\nfn one() {\n assert_eq!(add(1), 2);\n}\n"
GOOD = BAD.replace("a + 2", "a + 1")
def sse(ev, d):
return f"event: {ev}\ndata: {json.dumps(d)}\n\n".encode()
def last_user_text(body):
m = body["messages"][-1]
return "\n".join(b.get("text", "") for b in m["content"] if b.get("type") == "text")
class H(http.server.BaseHTTPRequestHandler):
def log_message(self, *a):
pass
def do_POST(self):
body = json.loads(self.rfile.read(int(self.headers["content-length"])))
LOG.append(body)
n = len(LOG)
lens = last_user_text(body)
if n == 1:
turn = ("tool", "Write", {"path": "src/lib.rs", "content": BAD})
elif "failing test" in lens and "one" in lens:
turn = ("tool", "Write", {"path": "src/lib.rs", "content": GOOD})
else:
turn = ("text", "FINISHED tests=" + ("green" if "pass now" in lens else "none"))
self.send_response(200)
self.send_header("content-type", "text/event-stream")
self.end_headers()
w = self.wfile.write
w(sse("message_start", {"message": {"usage": {"input_tokens": 1, "output_tokens": 1}}}))
if turn[0] == "text":
w(sse("content_block_start", {"index": 0, "content_block": {"type": "text", "text": ""}}))
w(sse("content_block_delta", {"index": 0, "delta": {"type": "text_delta", "text": turn[1]}}))
stop = "end_turn"
else:
w(sse("content_block_start", {"index": 0, "content_block": {"type": "tool_use", "id": f"t{n}", "name": turn[1], "input": {}}}))
w(sse("content_block_delta", {"index": 0, "delta": {"type": "input_json_delta", "partial_json": json.dumps(turn[2])}}))
stop = "tool_use"
w(sse("content_block_stop", {"index": 0}))
w(sse("message_delta", {"delta": {"stop_reason": stop}, "usage": {"output_tokens": 1}}))
w(sse("message_stop", {}))
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(json.dumps(LOG).encode())
http.server.ThreadingHTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever()