"""M4 e2e fake model (python project): turn 1: write badly formatted p.py with an unused import -> expect ruff F401 in lens report, file formatted turn 2: edit p.py (must not be blocked by the formatter's change: read-guard refreshed) turn 3: diag_mark the remaining finding as false positive turn 4: finish; report what it saw Usage: python3 tests/fake_m4.py PORT """ import http.server, json, sys LOG = [] def sse(ev, d): return f"event: {ev}\ndata: {json.dumps(d)}\n\n".encode() def texts(msg): out = [] for b in msg["content"]: if b.get("type") == "text": out.append(b["text"]) if b.get("type") == "tool_result": out.append(("ERR:" if b.get("is_error") else "OK:") + b["content"]) return "\n".join(out) 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(texts(body["messages"][-1])) n = len(LOG) if n == 1: turn = ("Write", {"path": "p.py", "content": "import os\nx = 1\nprint( x )\n"}) elif n == 2: turn = ("Edit", {"path": "p.py", "old_string": "x = 1", "new_string": "x = 2"}) elif n == 3: turn = ("diag_mark", {"path": "p.py", "line": 1, "mark": "false_positive", "reason": "needed for side effects"}) else: turn = 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 is None: w(sse("content_block_start", {"index": 0, "content_block": {"type": "text", "text": ""}})) w(sse("content_block_delta", {"index": 0, "delta": {"type": "text_delta", "text": "FINISHED"}})) stop = "end_turn" else: w(sse("content_block_start", {"index": 0, "content_block": {"type": "tool_use", "id": f"t{n}", "name": turn[0], "input": {}}})) w(sse("content_block_delta", {"index": 0, "delta": {"type": "input_json_delta", "partial_json": json.dumps(turn[1])}})) 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()