alexmec commited on
Commit
c9ac693
·
verified ·
1 Parent(s): 603c4f3

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. core/a2a_with_tools.py +39 -5
core/a2a_with_tools.py CHANGED
@@ -11,6 +11,37 @@ from any_agent import AgentConfig, AnyAgent
11
  from any_agent.serving import A2AServingConfig
12
  from any_agent.tools import a2a_tool_async
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  # Define output types
16
  class PickDecision(BaseModel):
@@ -106,14 +137,17 @@ class SimpleA2ADraftCoordinator:
106
  self.agents['zero_rb'] = await create_zero_rb_agent()
107
  self.agents['robust_rb'] = await create_robust_rb_agent()
108
 
109
- # Wait for servers to start
110
- await asyncio.sleep(2)
 
 
 
111
 
112
- # Create a2a tools for each agent
113
- self.agent_tools['zero_rb'] = await a2a_tool_async(
114
  "http://localhost:5001/zero_rb_agent"
115
  )
116
- self.agent_tools['robust_rb'] = await a2a_tool_async(
117
  "http://localhost:5003/robust_rb_agent"
118
  )
119
 
 
11
  from any_agent.serving import A2AServingConfig
12
  from any_agent.tools import a2a_tool_async
13
 
14
+ # ----------------- Reliability Helpers -----------------
15
+ import socket
16
+
17
+
18
+ async def wait_for_port(port: int, host: str = "127.0.0.1", timeout: float = 20.0):
19
+ """Wait until a TCP port is accepting connections.
20
+
21
+ This prevents race conditions where we try to reach an agent whose
22
+ Uvicorn server has not finished booting yet.
23
+ """
24
+ start_time = asyncio.get_event_loop().time()
25
+ while (asyncio.get_event_loop().time() - start_time) < timeout:
26
+ try:
27
+ with socket.create_connection((host, port), timeout=0.5):
28
+ return # Port is open
29
+ except OSError:
30
+ await asyncio.sleep(0.25)
31
+ raise RuntimeError(f"Port {port} on {host} did not open within {timeout} seconds")
32
+
33
+
34
+ async def create_tool_with_retries(url: str, retries: int = 3, delay: float = 1.0):
35
+ """Wrapper around a2a_tool_async that retries on network errors."""
36
+ last_err = None
37
+ for _ in range(retries):
38
+ try:
39
+ return await a2a_tool_async(url)
40
+ except Exception as err: # Broad but intentional for transient startup errors
41
+ last_err = err
42
+ await asyncio.sleep(delay)
43
+ raise last_err
44
+
45
 
46
  # Define output types
47
  class PickDecision(BaseModel):
 
137
  self.agents['zero_rb'] = await create_zero_rb_agent()
138
  self.agents['robust_rb'] = await create_robust_rb_agent()
139
 
140
+ # Wait for servers to start robustly
141
+ await asyncio.gather(
142
+ wait_for_port(5001),
143
+ wait_for_port(5003),
144
+ )
145
 
146
+ # Create a2a tools for each agent (with retries)
147
+ self.agent_tools['zero_rb'] = await create_tool_with_retries(
148
  "http://localhost:5001/zero_rb_agent"
149
  )
150
+ self.agent_tools['robust_rb'] = await create_tool_with_retries(
151
  "http://localhost:5003/robust_rb_agent"
152
  )
153