| 40 | |
| 41 | |
| 42 | def create_session(debug_sock: Path, cwd: str) -> str: |
| 43 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 44 | sock.connect(str(debug_sock)) |
| 45 | req = {"type": "debug_command", "id": 1, "command": f"create_session:{cwd}"} |
| 46 | sock.sendall((json.dumps(req) + "\n").encode()) |
| 47 | buf = b"" |
| 48 | while True: |
| 49 | chunk = sock.recv(65536) |
| 50 | if not chunk: |
| 51 | break |
| 52 | buf += chunk |
| 53 | while b"\n" in buf: |
| 54 | line, buf = buf.split(b"\n", 1) |
| 55 | resp = json.loads(line.decode()) |
| 56 | if resp.get("type") == "ack": |
| 57 | continue |
| 58 | if resp.get("type") == "error": |
| 59 | raise RuntimeError(resp.get("message") or resp) |
| 60 | if resp.get("type") != "debug_response": |
| 61 | continue |
| 62 | if not resp.get("ok", True): |
| 63 | raise RuntimeError(resp.get("output") or resp) |
| 64 | output = json.loads(resp["output"]) |
| 65 | return output["session_id"] |
| 66 | raise RuntimeError("missing debug response") |
| 67 | |
| 68 | |
| 69 | def reply_queries(master_fd: int, buffer: bytes) -> bytes: |