Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
189 lines (135 loc) · 6.77 KB
/
Copy pathclient.py
File metadata and controls
189 lines (135 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""agent_sync.client — CLI client for agent coordination.
Each command connects, sends one request, prints the result, and exits.
Blocking commands (listen, barrier, wait_task) keep the connection open.
"""
from __future__ import annotations
import asyncio
import json
import sys
async def _request(msg: dict, host: str = "127.0.0.1", port: int = 9800,
timeout: float | None = None,
retry: bool = True, retry_interval: float = 3.0,
max_retries: int = 60) -> dict:
"""Send a single JSON message and return the response.
If retry=True, keeps trying to connect until the server is available.
"""
attempts = 0
while True:
try:
reader, writer = await asyncio.open_connection(host, port)
break
except (ConnectionRefusedError, OSError) as e:
attempts += 1
if not retry or attempts >= max_retries:
return {"ok": False, "error": f"cannot connect to server ({e})"}
print(f"[agent_sync] Waiting for server on port {port}... (attempt {attempts})",
file=sys.stderr, flush=True)
await asyncio.sleep(retry_interval)
try:
writer.write((json.dumps(msg) + "\n").encode())
await writer.drain()
if timeout:
line = await asyncio.wait_for(reader.readline(), timeout=timeout + 5)
else:
line = await reader.readline()
if not line:
return {"ok": False, "error": "connection closed"}
return json.loads(line.decode().strip())
finally:
writer.close()
try:
await writer.wait_closed()
except Exception:
pass
def _print_result(resp: dict) -> int:
"""Pretty-print response and return exit code."""
if resp.get("ok"):
# Remove 'ok' key for cleaner output
data = {k: v for k, v in resp.items() if k != "ok"}
if data:
print(json.dumps(data, ensure_ascii=False, indent=2))
else:
print("OK")
return 0
else:
print(f"ERROR: {resp.get('error', resp.get('message', 'unknown'))}", file=sys.stderr)
return 1
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Individual commands
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
async def cmd_join(agent: str, port: int) -> int:
return _print_result(await _request({"cmd": "join", "agent": agent}, port=port))
async def cmd_status(port: int) -> int:
return _print_result(await _request({"cmd": "status"}, port=port))
async def cmd_send(from_: str, to: str, text: str, port: int) -> int:
return _print_result(await _request(
{"cmd": "send", "agent": from_, "to": to, "text": text}, port=port))
async def cmd_broadcast(from_: str, text: str, port: int) -> int:
return _print_result(await _request(
{"cmd": "broadcast", "agent": from_, "text": text}, port=port))
async def cmd_listen(agent: str, timeout: int, port: int) -> int:
return _print_result(await _request(
{"cmd": "listen", "agent": agent, "timeout": timeout},
port=port, timeout=timeout))
async def cmd_barrier(barrier_id: str, agent: str, expected: int,
timeout: int, port: int) -> int:
return _print_result(await _request(
{"cmd": "barrier", "agent": agent, "barrier_id": barrier_id,
"expected": expected, "timeout": timeout},
port=port, timeout=timeout))
async def cmd_add_task(task_id: str, description: str, port: int) -> int:
return _print_result(await _request(
{"cmd": "add_task", "task_id": task_id, "description": description},
port=port))
async def cmd_wait_task(agent: str, timeout: int, port: int) -> int:
return _print_result(await _request(
{"cmd": "wait_task", "agent": agent, "timeout": timeout},
port=port, timeout=timeout))
async def cmd_done_task(agent: str, message: str, port: int) -> int:
return _print_result(await _request(
{"cmd": "done_task", "agent": agent, "message": message},
port=port))
async def cmd_heartbeat(agent: str, port: int) -> int:
return _print_result(await _request(
{"cmd": "heartbeat", "agent": agent}, port=port))
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# v2: Workflow commands
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
async def cmd_set_branch(agent: str, branch: str, port: int) -> int:
return _print_result(await _request(
{"cmd": "set_branch", "agent": agent, "branch": branch}, port=port))
async def cmd_merge_request(agent: str, branch: str, message: str, port: int) -> int:
return _print_result(await _request(
{"cmd": "merge_request", "agent": agent, "branch": branch,
"message": message}, port=port))
async def cmd_set_phase(phase: str, port: int) -> int:
return _print_result(await _request(
{"cmd": "set_phase", "phase": phase}, port=port))
async def cmd_wait_phase(agent: str, phase: str, timeout: int, port: int) -> int:
return _print_result(await _request(
{"cmd": "wait_phase", "agent": agent, "phase": phase, "timeout": timeout},
port=port, timeout=timeout))
async def cmd_test_result(passed: bool, output: str, failures: list, port: int) -> int:
return _print_result(await _request(
{"cmd": "test_result", "passed": passed, "output": output,
"failures": failures}, port=port))
async def cmd_discuss(agent: str, text: str, port: int) -> int:
return _print_result(await _request(
{"cmd": "discuss", "agent": agent, "text": text}, port=port))
async def cmd_propose(agent: str, text: str, port: int) -> int:
return _print_result(await _request(
{"cmd": "propose", "agent": agent, "text": text}, port=port))
async def cmd_approve(proposal_id: str, port: int) -> int:
return _print_result(await _request(
{"cmd": "approve", "proposal_id": proposal_id}, port=port))
async def cmd_reject(proposal_id: str, reason: str, port: int) -> int:
return _print_result(await _request(
{"cmd": "reject", "proposal_id": proposal_id, "reason": reason}, port=port))
async def cmd_get_discussion(round_num: int | None, port: int) -> int:
msg: dict = {"cmd": "get_discussion"}
if round_num is not None:
msg["round"] = round_num
return _print_result(await _request(msg, port=port))
async def cmd_shutdown(reason: str, port: int) -> int:
return _print_result(await _request(
{"cmd": "shutdown", "reason": reason}, port=port))