Skip to content

Commit 6fffd85

Browse files
Merge #726
2 parents 5b241d1 + a6f3720 commit 6fffd85

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

‎src/agent_env/agents/agents/claude-code/claude_bridge.py‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import annotations
55

66
import asyncio
7+
import concurrent.futures
78
import contextlib
89
import ipaddress
910
import json
@@ -217,18 +218,29 @@ def clock_armed(page_path: str | None = None) -> bool:
217218
return bool(armed) # a writer that died mid-write: the next write takes the page up; its armed flag says the most
218219

219220

221+
#: How long a CLI's start waits for its model host's addresses, once per host per process (``_GLOBAL_HOSTS``): the
222+
#: lookup runs on a thread of its own, so a slow resolver holds the server's loop this long at most, once.
223+
_RESOLVE_TIMEOUT_S = 2.0
224+
_GLOBAL_HOSTS: dict[str, bool] = {}
225+
_RESOLVER = concurrent.futures.ThreadPoolExecutor(max_workers=1, thread_name_prefix="model-host")
226+
227+
220228
def _global_host(host: str) -> bool:
221229
"""Whether ``host`` is a public address, or a name every address of resolves to one: a host the egress proxy
222-
reaches. A name that doesn't resolve here is taken as public, for the proxy to resolve."""
230+
reaches. A name that doesn't resolve here, or not within ``_RESOLVE_TIMEOUT_S``, is taken as public, for the proxy
231+
to resolve; each name's answer is kept."""
223232
try:
224233
return ipaddress.ip_address(host).is_global
225234
except ValueError:
226235
pass
227-
try:
228-
addresses = {info[4][0] for info in socket.getaddrinfo(host, None)}
229-
except OSError:
230-
return True
231-
return all(ipaddress.ip_address(address.split("%")[0]).is_global for address in addresses)
236+
if host not in _GLOBAL_HOSTS:
237+
try:
238+
infos = _RESOLVER.submit(socket.getaddrinfo, host, None).result(timeout=_RESOLVE_TIMEOUT_S)
239+
addresses = {info[4][0] for info in infos}
240+
_GLOBAL_HOSTS[host] = all(ipaddress.ip_address(a.split("%")[0]).is_global for a in addresses)
241+
except (OSError, concurrent.futures.TimeoutError):
242+
_GLOBAL_HOSTS[host] = True
243+
return _GLOBAL_HOSTS[host]
232244

233245

234246
def model_direct_hosts(base_url: str | None) -> list[str]:

‎tst/unit/agents/claude_code/test_env_clock_image.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def test_hosts_the_agent_was_deployed_to_reach_directly_stay_direct_on_an_armed_
4848
def test_a_model_endpoint_over_plain_http_or_at_a_private_address_is_reached_directly_on_an_armed_clock(
4949
monkeypatch, base_url, direct):
5050
resolves = {"litellm.internal": "10.1.2.3", "litellm.example.com": "93.184.216.34"}
51+
monkeypatch.setattr(claude_bridge, "_GLOBAL_HOSTS", {})
5152
monkeypatch.setattr(claude_bridge.socket, "getaddrinfo",
5253
lambda host, port: [(None, None, None, "", (resolves[host], 0))])
5354
env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY, "LITELLM_BASE_URL": base_url,
@@ -102,3 +103,25 @@ def test_while_the_follower_reconnects_or_restarts_the_page_still_routes_the_cli
102103
page.write_bytes(_page(1))
103104
env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY}, clock_armed=claude_bridge.clock_armed(str(page)))
104105
assert env["HTTPS_PROXY"] == PROXY
106+
107+
108+
def test_a_model_host_that_doesnt_resolve_in_time_is_left_to_the_proxy_and_asked_once(monkeypatch):
109+
# A slow resolver holds a CLI's start for _RESOLVE_TIMEOUT_S at most, and only the first time.
110+
import threading
111+
112+
asked, release = [], threading.Event()
113+
114+
def slow(host, port):
115+
asked.append(host)
116+
release.wait(5)
117+
return [(None, None, None, "", ("10.1.2.3", 0))]
118+
119+
monkeypatch.setattr(claude_bridge, "_GLOBAL_HOSTS", {})
120+
monkeypatch.setattr(claude_bridge, "_RESOLVE_TIMEOUT_S", 0.05)
121+
monkeypatch.setattr(claude_bridge.socket, "getaddrinfo", slow)
122+
try:
123+
assert claude_bridge.model_direct_hosts("https://slow.internal") == []
124+
assert claude_bridge.model_direct_hosts("https://slow.internal") == []
125+
finally:
126+
release.set()
127+
assert asked == ["slow.internal"]

0 commit comments

Comments
 (0)