Skip to content

The agent that grows with you

refactor(telegram): import platform_ssl_context at module top in the adapter

kshitijk4poorcdd58da229d9
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index 3ab4c791fc2..5850c2083ad 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -18,6 +18,7 @@ from hermes_cli import setup_platforms
logger = logging.getLogger(__name__)
from agent.deadline import run_bounded_async
+from agent.ssl_verify import platform_ssl_context
from gateway.platforms._shared import (
decode_json_list_literal as _decode_json_list_literal,
extra_or_secret as _extra_or_secret, get_scoped_secret as _get_scoped_secret,
@@ -2986,17 +2987,13 @@ class TelegramAdapter(TelegramHeldInboundMixin, BasePlatformAdapter):
"TELEGRAM_PROXY", target_hosts=["api.telegram.org", *fallback_ips],
configured=self.config.extra.get("proxy_url"))
- from agent.ssl_verify import platform_ssl_context
- ssl_context = platform_ssl_context()
-
def _pair(general_httpx: dict, updates_httpx: dict, **extra) -> tuple:
- return (HTTPXRequest(**request_kwargs, **extra, httpx_kwargs={"verify": ssl_context, **general_httpx}),
- HTTPXRequest(**request_kwargs, **extra, httpx_kwargs={"verify": ssl_context, **updates_httpx}))
+ return (HTTPXRequest(**request_kwargs, **extra, httpx_kwargs={"verify": platform_ssl_context(), **general_httpx}),
+ HTTPXRequest(**request_kwargs, **extra, httpx_kwargs={"verify": platform_ssl_context(), **updates_httpx}))
if fallback_ips and not proxy_url and not disable_fallback:
logger.info("[%s] Telegram fallback IPs active: %s", self.name, ", ".join(fallback_ips))
- # Separate request/update pools reduce contention during polling reconnect + bootstrap calls.
- _transport_kwargs: dict = {"socket_options": tcp_keepalive_socket_options(), "verify": ssl_context}
+ _transport_kwargs: dict = {"socket_options": tcp_keepalive_socket_options(), "verify": platform_ssl_context()}
# Keep request/update pools separate to reduce contention during polling reconnect + bot API
# bootstrap/delete_webhook calls. httpx ignores the client-level `limits` kwarg when a custom
# `transport` is supplied (#58790). Unlike the proxy/direct branches (which inject limits at the
diff --git a/tests/gateway/test_telegram_closewait_limits.py b/tests/gateway/test_telegram_closewait_limits.py
index c47a00e7fc3..c806b4b56bb 100644
--- a/tests/gateway/test_telegram_closewait_limits.py
+++ b/tests/gateway/test_telegram_closewait_limits.py
@@ -147,7 +147,7 @@ def test_all_ptb_clients_share_the_platform_ssl_context(monkeypatch):
platform-trust context — a fresh verify=True context per client reloads CAs on the
event loop on every reconnect (#133339)."""
shared = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
- monkeypatch.setattr("agent.ssl_verify.platform_ssl_context", lambda: shared, raising=False)
+ monkeypatch.setattr(tg_adapter, "platform_ssl_context", lambda: shared, raising=False)
for proxy_url in ("http://127.0.0.1:9/", None):
instances = _drive_connect(monkeypatch, proxy_url=proxy_url)
assert len(instances) >= 2