process_bootstrap.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. """Process-level bootstrap helpers for ``run_agent``.
  2. Lazy OpenAI SDK import (``_OpenAIProxy`` keeps ``isinstance`` and
  3. ``patch("agent.process_bootstrap.OpenAI")`` working), crash-resistant stdio
  4. (``_SafeWriter``), env-only HTTP proxy resolution, and the httpcore backend that
  5. runs sync httpx connects through the process-wide Happy Eyeballs racer
  6. (``hermes_bootstrap``).
  7. """
  8. from __future__ import annotations
  9. import functools
  10. import socket
  11. import sys
  12. import threading
  13. from typing import Any, Optional
  14. from hermes_bootstrap import _happy_eyeballs_create_connection
  15. from utils import base_url_hostname, normalize_proxy_url
  16. from agent.proxy_bypass import first_proxy_env_value, should_bypass_proxy
  17. from agent.thread_scoped_output import _ThreadRoutingStream
  18. _OPENAI_CLS_CACHE = None
  19. # Process-wide pool of sync ``httpx.HTTPTransport`` objects shared by every
  20. # keepalive client with the same (verify, proxy, happy-eyeballs) identity.
  21. # Each delegated child AIAgent used to get its own transport = its own TLS
  22. # pool, so a fan-out of N children held N separate socket sets to the same
  23. # provider. Bounded: past the cap, callers get a private transport again.
  24. _SHARED_TRANSPORTS: dict[tuple, Any] = {}
  25. _SHARED_TRANSPORTS_LOCK = threading.Lock()
  26. _SHARED_TRANSPORTS_MAX = 32
  27. # ``request.extensions`` key stamped by ``_SharedTransport.handle_request``;
  28. # the socket-abort walker in agent_runtime_helpers uses it to find only the
  29. # owning client's in-flight connections on a shared pool.
  30. HERMES_TRANSPORT_OWNER_EXT = "hermes_transport_owner"
  31. class _HappyEyeballsSyncBackend:
  32. """httpcore sync backend with concurrent IPv6/IPv4 connection fallback."""
  33. def __init__(self):
  34. self._fallback = None
  35. def _default_backend(self):
  36. if self._fallback is None:
  37. from httpcore import SyncBackend
  38. self._fallback = SyncBackend()
  39. return self._fallback
  40. def connect_tcp(self, host: str, port: int, timeout: Optional[float] = None, local_address: Optional[str] = None,
  41. socket_options=None):
  42. from httpcore import ConnectError, ConnectTimeout
  43. from httpcore._backends.sync import SyncStream
  44. source_address = None if local_address is None else (local_address, 0)
  45. try:
  46. sock = _happy_eyeballs_create_connection((host, port), timeout, source_address=source_address,
  47. socket_options=socket_options or ())
  48. except TimeoutError as exc:
  49. raise ConnectTimeout(str(exc)) from exc
  50. except OSError as exc:
  51. raise ConnectError(str(exc)) from exc
  52. return SyncStream(sock)
  53. def connect_unix_socket(self, *args, **kwargs):
  54. return self._default_backend().connect_unix_socket(*args, **kwargs)
  55. def sleep(self, seconds: float) -> None:
  56. self._default_backend().sleep(seconds)
  57. def _uses_codex_cloud_transport(base_url: str) -> bool:
  58. return base_url_hostname(base_url).lower() == "chatgpt.com" and "/backend-api/codex" in str(base_url).lower()
  59. def _enable_happy_eyeballs(transport, skip_pool_types: tuple = ()) -> None:
  60. """Install the racing backend on one httpx transport.
  61. Reaches into private ``transport._pool._network_backend`` (httpcore pinned
  62. 1.0.x); hasattr-guarded so an incompatible httpcore degrades to the default
  63. serial backend. Pools of ``skip_pool_types`` (proxies) are left alone.
  64. """
  65. pool = getattr(transport, "_pool", None)
  66. if pool is not None and hasattr(pool, "_network_backend") and not (skip_pool_types and isinstance(pool, skip_pool_types)):
  67. pool._network_backend = _HappyEyeballsSyncBackend()
  68. def enable_happy_eyeballs_on_client(client) -> None:
  69. """Install the racing backend on every direct transport of a ready-built httpx.Client.
  70. For callers that build clients inline (Codex OAuth/device-login). Proxy-backed
  71. pools are skipped (TCP connect goes to the proxy host); async clients need
  72. nothing (anyio already races per RFC 8305). Best-effort.
  73. Proxy-backed transports (``httpcore.HTTPProxy`` / SOCKS pools) are left untouched: with a proxy in play
  74. the TCP connect goes to the proxy host, which is out of scope for the direct-transport racing added in
  75. #94388. Async clients are also left untouched — httpcore's async backend already performs RFC 8305
  76. racing natively via ``anyio.connect_tcp(happy_eyeballs_delay=0.25)``.
  77. """
  78. try:
  79. import httpcore
  80. proxy_pool_types = tuple(
  81. t for t in (getattr(httpcore, "HTTPProxy", None), getattr(httpcore, "SOCKSProxy", None)) if t is not None)
  82. except Exception:
  83. return
  84. for transport in (getattr(client, "_transport", None), *(getattr(client, "_mounts", None) or {}).values()):
  85. _enable_happy_eyeballs(transport, proxy_pool_types)
  86. def _load_openai_cls() -> type:
  87. """Import and cache ``openai.OpenAI``."""
  88. global _OPENAI_CLS_CACHE
  89. if _OPENAI_CLS_CACHE is None:
  90. from openai import OpenAI as _OPENAI_CLS_CACHE
  91. return _OPENAI_CLS_CACHE
  92. class _OpenAIProxy:
  93. """Module-level proxy that looks like ``openai.OpenAI`` but imports lazily."""
  94. __slots__ = ()
  95. def __call__(self, *args, **kwargs):
  96. return _load_openai_cls()(*args, **kwargs)
  97. def __instancecheck__(self, obj):
  98. return isinstance(obj, _load_openai_cls())
  99. def __repr__(self):
  100. return "<lazy openai.OpenAI proxy>"
  101. class _SafeWriter:
  102. """Transparent stdio wrapper swallowing OSError/ValueError from broken pipes.
  103. Headless runs (systemd, Docker) lose the stdout pipe → ``OSError: [Errno 5]``;
  104. subagent threads can see the shared handle close → ``ValueError``. Either
  105. would otherwise crash the agent (often via double-fault in an except handler).
  106. """
  107. __slots__ = ("_inner",)
  108. def __init__(self, inner):
  109. object.__setattr__(self, "_inner", inner)
  110. def write(self, data):
  111. try:
  112. return self._inner.write(data)
  113. except (OSError, ValueError):
  114. return len(data) if isinstance(data, str) else 0
  115. def flush(self):
  116. try:
  117. self._inner.flush()
  118. except (OSError, ValueError):
  119. pass
  120. def fileno(self):
  121. return self._inner.fileno()
  122. def isatty(self):
  123. try:
  124. return self._inner.isatty()
  125. except (OSError, ValueError):
  126. return False
  127. def __getattr__(self, name):
  128. return getattr(self._inner, name)
  129. def _get_proxy_from_env() -> Optional[str]:
  130. """First configured proxy URL from HTTPS_PROXY / HTTP_PROXY / ALL_PROXY (any case), or None."""
  131. value = first_proxy_env_value()
  132. return normalize_proxy_url(value) if value else None
  133. def _get_proxy_for_base_url(base_url: Optional[str]) -> Optional[str]:
  134. """Env-configured proxy unless NO_PROXY excludes this base URL (same matcher as the
  135. gateway adapters: CIDR, ``*.`` wildcards and host:port entries all count)."""
  136. proxy = _get_proxy_from_env()
  137. if not (proxy and base_url):
  138. return proxy
  139. raw = base_url.strip()
  140. return None if should_bypass_proxy(raw if "://" in raw else f"//{raw}") else proxy
  141. def _shared_transport_cls():
  142. """Lazily define the per-client transport view (httpx import is deferred)."""
  143. global _SharedTransport
  144. if _SharedTransport is not None:
  145. return _SharedTransport
  146. import httpx
  147. class _SharedTransportImpl(httpx.BaseTransport):
  148. """Per-client view of a process-shared ``httpx.HTTPTransport``.
  149. ``httpx.Client.close()`` closes every mounted transport, and each OpenAI client still
  150. owns its own ``httpx.Client`` (closing one client must never poison the next), so the
  151. mounted object absorbs that close while the shared pool keeps serving other clients.
  152. ``handle_request`` stamps the owning view into ``request.extensions`` so socket-abort
  153. sweeps target only this client's in-flight connections on the shared pool.
  154. See #10933.
  155. """
  156. __slots__ = ("_closed", "_inner")
  157. def __init__(self, inner: Any) -> None:
  158. self._inner = inner
  159. self._closed = False
  160. @property
  161. def _pool(self) -> Any: # httpx-private; socket walkers and tests introspect it
  162. return getattr(self._inner, "_pool", None)
  163. def handle_request(self, request: Any) -> Any:
  164. if self._closed:
  165. raise RuntimeError("Cannot send a request, as the client has been closed.")
  166. request.extensions[HERMES_TRANSPORT_OWNER_EXT] = id(self)
  167. return self._inner.handle_request(request)
  168. def close(self) -> None:
  169. # Never closes the shared ``_inner``; idle connections are reaped by keepalive_expiry
  170. # and the pool lives for the process (see ``close_shared_transports``).
  171. self._closed = True
  172. _SharedTransportImpl.__name__ = _SharedTransportImpl.__qualname__ = "_SharedTransport"
  173. _SharedTransport = _SharedTransportImpl
  174. return _SharedTransport
  175. _SharedTransport: Any = None
  176. def _shared_transport_key(base_url: str, verify: Any, proxy: Optional[str]) -> tuple:
  177. """Identity under which sync direct transports are pooled process-wide."""
  178. if verify is True or verify is False:
  179. verify_key: Any = verify
  180. elif isinstance(verify, str):
  181. verify_key = ("path", verify)
  182. else:
  183. verify_key = ("id", id(verify)) # SSLContext / custom object: share by identity only
  184. return (verify_key, proxy, _uses_codex_cloud_transport(base_url))
  185. def _get_shared_transport(key: tuple, build) -> Any:
  186. with _SHARED_TRANSPORTS_LOCK:
  187. transport = _SHARED_TRANSPORTS.get(key)
  188. if transport is None:
  189. transport = build()
  190. if len(_SHARED_TRANSPORTS) < _SHARED_TRANSPORTS_MAX:
  191. _SHARED_TRANSPORTS[key] = transport
  192. return transport
  193. def close_shared_transports() -> int:
  194. """Really close every process-shared transport (test teardown / atexit)."""
  195. with _SHARED_TRANSPORTS_LOCK:
  196. transports = list(_SHARED_TRANSPORTS.values())
  197. _SHARED_TRANSPORTS.clear()
  198. for transport in transports:
  199. try:
  200. transport.close()
  201. except Exception:
  202. pass
  203. return len(transports)
  204. def build_keepalive_http_client(base_url: str = "", *, async_mode: bool = False, verify: Any = True) -> Optional[Any]:
  205. """httpx client for OpenAI SDK calls with env-only proxy policy (None on failure).
  206. Explicit no-proxy mounts disable httpx's ``trust_env`` path so macOS system
  207. proxies (which omit the ExceptionsList) are never applied. ``keepalive_expiry``
  208. reaps idle connections before reverse proxies' 30-60 s timeouts (a custom
  209. socket_options transport broke streaming and stripped TCP_NODELAY). ``verify``
  210. goes on the client AND the mounts, since a mounted transport owns its SSL context.
  211. Every call returns a NEW ``httpx.Client`` (per-client close semantics), but sync clients
  212. with the same (verify, proxy, happy-eyeballs) identity mount the SAME underlying
  213. ``HTTPTransport`` through a ``_SharedTransport`` view, so N delegated children share one
  214. connection pool + SSL context. Async clients are never shared: an httpcore async pool is
  215. bound to the event loop that first used it. Proxy-backed clients keep httpx's own transport.
  216. See #12952, #54049.
  217. See #10933.
  218. """
  219. try:
  220. import httpx
  221. proxy = _get_proxy_for_base_url(base_url)
  222. limits = httpx.Limits(max_keepalive_connections=20, max_connections=100, keepalive_expiry=20.0)
  223. timeout = httpx.Timeout(connect=15.0, read=None, write=15.0, pool=10.0) # read=None for SSE streaming
  224. transport_cls = httpx.AsyncHTTPTransport if async_mode else httpx.HTTPTransport
  225. client_cls = httpx.AsyncClient if async_mode else httpx.Client
  226. mounts = None
  227. # Official Codex backend only: zstd request bodies when the stdlib codec exists (None otherwise).
  228. from agent.codex_headers import codex_request_compression_hooks
  229. hooks = codex_request_compression_hooks(base_url, async_mode=async_mode)
  230. if hooks:
  231. client_cls = functools.partial(client_cls, event_hooks=hooks)
  232. if proxy is None:
  233. happy_eyeballs = not async_mode and _uses_codex_cloud_transport(base_url)
  234. # One pool serves every agent in the process, so its ceiling must cover a whole
  235. # fan-out of concurrently streaming children. (Client-level ``limits`` never reach
  236. # mounted transports — they used to run on httpx defaults, keepalive_expiry=5s.)
  237. direct_limits = limits if async_mode else httpx.Limits(
  238. max_keepalive_connections=50, max_connections=1000, keepalive_expiry=20.0,
  239. )
  240. def _build_direct():
  241. transport = transport_cls(verify=verify, limits=direct_limits)
  242. # Async transports race natively (anyio happy_eyeballs_delay=0.25).
  243. if happy_eyeballs:
  244. _enable_happy_eyeballs(transport)
  245. return transport
  246. if async_mode:
  247. mounts = {"http://": _build_direct(), "https://": _build_direct()}
  248. else:
  249. key = _shared_transport_key(base_url, verify, proxy)
  250. view_cls = _shared_transport_cls()
  251. mounts = {
  252. f"{scheme}://": view_cls(_get_shared_transport((scheme, *key), _build_direct))
  253. for scheme in ("http", "https")
  254. }
  255. # Default transport = the https view; otherwise httpx builds a third, never-used
  256. # direct transport (pool + SSL context) per client.
  257. return client_cls(limits=limits, timeout=timeout, transport=mounts["https://"], mounts=mounts)
  258. return client_cls(limits=limits, timeout=timeout, proxy=proxy, mounts=mounts or None, verify=verify)
  259. except Exception:
  260. return None
  261. def _install_safe_stdio() -> None:
  262. """Wrap stdout/stderr so best-effort console output cannot crash the agent."""
  263. for stream_name in ("stdout", "stderr"):
  264. stream = getattr(sys, stream_name, None)
  265. # The routing proxy already absorbs these errors; wrapping it stacks a new proxy per build (#139687).
  266. if stream is not None and not isinstance(stream, (_SafeWriter, _ThreadRoutingStream)):
  267. setattr(sys, stream_name, _SafeWriter(stream))
  268. # Drop-in for ``openai.OpenAI``.
  269. OpenAI = _OpenAIProxy()
  270. __all__ = [
  271. "OpenAI",
  272. "_OpenAIProxy",
  273. "_SafeWriter",
  274. "_get_proxy_for_base_url",
  275. "_get_proxy_from_env",
  276. "_install_safe_stdio",
  277. "_load_openai_cls",
  278. "build_keepalive_http_client",
  279. "close_shared_transports",
  280. "enable_happy_eyeballs_on_client",
  281. ]