The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(telegram): do not publish connected for a degraded, unconfirmed polling path
chelsealongce52ae21a75c
diff --git a/gateway/platforms/base.py b/gateway/platforms/base.pyindex fb38e27c488..9a30b74b408 100644--- a/gateway/platforms/base.py+++ b/gateway/platforms/base.py@@ -3669,12 +3669,25 @@ class BasePlatformAdapter(ABC):def set_fatal_error_handler(self, handler: Callable[["BasePlatformAdapter"], Awaitable[None] | None]) -> None:self._fatal_error_handler = handler- def _mark_connected(self) -> None:+ def _mark_connected(self, *, degraded: bool = False) -> None:self._running = Trueself._fatal_error_code = Noneself._fatal_error_message = Noneself._fatal_error_retryable = True- self._write_runtime_status_safe("connected", platform_state="connected", error_code=None, error_message=None)+ if degraded:+ # connect() succeeded but the adapter knows its send/receive path+ # is not actually confirmed active yet (e.g. Telegram polling+ # never proved a first getUpdates round-trip). Publishing+ # "connected" here would be indistinguishable from a healthy+ # adapter to anything reading gateway_state.json (#101391).+ self._write_runtime_status_safe(+ "connected_degraded",+ platform_state="retrying",+ error_code=None,+ error_message="connected but not yet confirmed active; recovering in background",+ )+ else:+ self._write_runtime_status_safe("connected", platform_state="connected", error_code=None, error_message=None)def _mark_disconnected(self) -> None:self._running = Falsediff --git a/gateway/run.py b/gateway/run.pyindex bebb709220f..182ebf40ebd 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -16203,15 +16203,31 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewself._bind_voice_input_callback(adapter)self.delivery_router.adapters = self.adaptersdel self._failed_platforms[platform]+ # connect() returning True does not mean the adapter's+ # send/receive path is actually confirmed active --+ # Telegram's degraded-polling reconnect intentionally+ # returns True so the gateway stays up while its own+ # background ladder retries. Stamping "connected"+ # unconditionally here would silently undo the+ # adapter's own accurate status write (#101391).+ _degraded = getattr(adapter, "_send_path_degraded", False)self._update_platform_runtime_status(platform.value,- platform_state="connected",+ platform_state="retrying" if _degraded else "connected",error_code=None,- error_message=None,+ error_message=(+ "connected but not yet confirmed active; recovering in background"+ if _degraded else None+ ),needs_attention=False,retrying_since=None,)- logger.info("✓ %s reconnected successfully", platform.value)+ logger.info(+ "%s %s reconnected%s",+ "⚠" if _degraded else "✓",+ platform.value,+ " in degraded mode (send/receive path not yet confirmed)" if _degraded else " successfully",+ )# Final responses rejected while this adapter was down# are still owned by this live process, so startupdiff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.pyindex a2ba4e7b511..fc2920dc707 100644--- a/plugins/platforms/telegram/adapter.py+++ b/plugins/platforms/telegram/adapter.py@@ -902,7 +902,11 @@ class TelegramAdapter(BasePlatformAdapter):def _mark_connected(self) -> None:self._drop_delayed_deliveries = False- super()._mark_connected()+ # _send_path_degraded is already true whenever this connect's+ # polling generation has not proven a first getUpdates round-trip+ # (set at generation start, cleared in _record_polling_progress) —+ # publish that instead of an unconditional "connected" (#101391).+ super()._mark_connected(degraded=getattr(self, "_send_path_degraded", False))# Drain anything held while we were down. PTB will not redeliver —# these events exist only in our hold queue now.self._schedule_held_inbound_redispatch()@@ -2694,6 +2698,16 @@ class TelegramAdapter(BasePlatformAdapter):self._polling_conflict_recovery_generation = Noneelse:self._polling_conflict_count = 0+ # If connect() already published "connected" while polling was+ # confirmed degraded (or the reconnect watcher stamped that state+ # once connect() returned), gateway_state.json is still showing the+ # pre-recovery status. This is the first proof getUpdates is+ # actually flowing again, so flip it back now instead of leaving it+ # wedged at "retrying" until the next disconnect/reconnect (#101391).+ if self._send_path_degraded and getattr(self, "_running", False) and not self.has_fatal_error:+ self._write_runtime_status_safe(+ "connected", platform_state="connected", error_code=None, error_message=None,+ )self._send_path_degraded = Falsedef _observe_polling_request_result(self, request, generation, result):diff --git a/tests/gateway/test_telegram_send_path_health.py b/tests/gateway/test_telegram_send_path_health.pyindex a16faa4ecd5..0e50312e974 100644--- a/tests/gateway/test_telegram_send_path_health.py+++ b/tests/gateway/test_telegram_send_path_health.py@@ -76,3 +76,49 @@ async def test_send_short_flood_still_retries_inline(monkeypatch):sleep.assert_awaited_once_with(2.0)+def test_mark_connected_publishes_connected_when_healthy():+ """A normal connect (never degraded) still publishes platform_state=connected."""+ adapter = _make_adapter()+ adapter._send_path_degraded = False++ with patch.object(adapter, "_write_runtime_status_safe") as write_status:+ adapter._mark_connected()++ write_status.assert_called_once()+ _, kwargs = write_status.call_args+ assert kwargs["platform_state"] == "connected"+++def test_mark_connected_publishes_retrying_when_send_path_degraded():+ """connect() can return True while polling never proved a first getUpdates+ round-trip (the degraded branch, or a reconnect where require_progress is+ skipped). _mark_connected() must not publish "connected" for that case --+ it is indistinguishable from a healthy adapter to anything reading+ gateway_state.json (#101391)."""+ adapter = _make_adapter()+ adapter._send_path_degraded = True++ with patch.object(adapter, "_write_runtime_status_safe") as write_status:+ adapter._mark_connected()++ write_status.assert_called_once()+ _, kwargs = write_status.call_args+ assert kwargs["platform_state"] == "retrying"+++def test_record_polling_progress_republishes_connected_after_degraded_connect():+ """Once getUpdates actually proves a round-trip after a degraded connect,+ the previously-published "retrying" status must be corrected back to+ "connected" -- otherwise it stays wedged until the next disconnect."""+ adapter = _make_adapter()+ generation, _event = adapter._begin_polling_generation()+ # Simulate connect() having already run and published the degraded state.+ adapter._running = True++ with patch.object(adapter, "_write_runtime_status_safe") as write_status:+ adapter._record_polling_progress(generation)++ write_status.assert_called_once()+ _, kwargs = write_status.call_args+ assert kwargs["platform_state"] == "connected"+ assert adapter._send_path_degraded is False