The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): do not hand an installed reconnect adapter's hold queue back to its predecessor
kshitijk4poor7748c7f4848f
diff --git a/gateway/run_adapters.py b/gateway/run_adapters.pyindex 6b298972b56..2a229da3bc5 100644--- a/gateway/run_adapters.py+++ b/gateway/run_adapters.py@@ -876,7 +876,8 @@ class GatewayAdapterLifecycleMixin:if adapter is not None:# An exception escaping connect leaves the adapter in the same unowned state.await _dispose_unused_adapter(adapter)- hand_over_held_inbound(adapter, info.get("predecessor"))+ if self.adapters.get(platform) is not adapter: # an installed adapter owns its queue+ hand_over_held_inbound(adapter, info.get("predecessor"))# A reconnect exception is transient; keep retrying at the cap rather than auto-pausing.backoff = self._bump_reconnect_backoff(platform, info, attempt, None, str(e))logger.warning("Reconnect %s error: %s, next retry in %ds", platform.value, e, backoff)@@ -1504,7 +1505,8 @@ class GatewayAdapterLifecycleMixin:except BaseException as exc:if adapter is not None:await self._safe_adapter_disconnect(adapter, platform)- hand_over_held_inbound(adapter, predecessor)+ if self._profile_adapters.get(profile_name, {}).get(platform) is not adapter:+ hand_over_held_inbound(adapter, predecessor) # an installed adapter owns its queueif not isinstance(exc, Exception):raise # CancelledError (and other BaseExceptions) propagate after releaselogger.debug(diff --git a/plugins/platforms/telegram/telegram_held_inbound.py b/plugins/platforms/telegram/telegram_held_inbound.pyindex 3bedaa411e8..e9819bc3173 100644--- a/plugins/platforms/telegram/telegram_held_inbound.py+++ b/plugins/platforms/telegram/telegram_held_inbound.py@@ -75,6 +75,7 @@ class TelegramHeldInboundMixin:def adopt_held_inbound(self, predecessor: "TelegramHeldInboundMixin") -> None:"""Take over the hold queue of the instance the runner just replaced with us (#132829): it onlydrains on its own ``_mark_connected``, which never comes; later holds there forward here."""+ self._held_inbound_successor = None # we own the queue again: a reverse link would forward in a cyclepredecessor._held_inbound_successor = weakref.ref(self)held = getattr(predecessor, "_held_inbound_events", None) or []events = list(held)diff --git a/tests/gateway/test_platform_reconnect.py b/tests/gateway/test_platform_reconnect.pyindex 9e419b8e957..8c5e04ab746 100644--- a/tests/gateway/test_platform_reconnect.py+++ b/tests/gateway/test_platform_reconnect.py@@ -513,27 +513,37 @@ class TestReconnectKeepsInboundDedup:assert old._held_inbound_events == failed._held_inbound_events == new._held_inbound_events == []@pytest.mark.asyncio- async def test_failed_secondary_candidate_returns_held_inbound_to_predecessor(self):+ @pytest.mark.parametrize("mode", ["not-installed", "cancel-after-install"])+ async def test_failed_secondary_candidate_returns_held_inbound_to_predecessor(self, mode):"""A secondary reconnect candidate that is not installed must hand its held inbound back to the- retained predecessor instead of discarding it with the candidate (#133399)."""+ retained predecessor instead of discarding it with the candidate (#133399); an INSTALLED one+ cancelled mid-redeliver keeps its queue -- handing back would forward in a cycle forever."""_Telegram = self._telegram_cls()runner = _make_runner()runner._profile_adapters, runner._profile_failed_platforms = {}, {}+ runner._sync_voice_mode_state_to_adapter = MagicMock()in_connect, salvaged = self._event("candidate"), self._event("salvage")- predecessor, failed = _Telegram(True), _Telegram(False, hold=in_connect, salvage=salvaged)+ installed = mode == "cancel-after-install"+ predecessor = _Telegram(True)+ failed = _Telegram(installed or False, hold=in_connect, salvage=salvaged)predecessor._set_fatal_error("telegram_network_error", "stall", retryable=True)async def attempt(*_args):- await failed.connect(is_reconnect=True)- runner._running = False # stop after this one failed attempt- return failed, False+ success = await failed.connect(is_reconnect=True)+ runner._running = installed # stop after this one failed attempt+ return failed, successrunner._secondary_reconnect_attempt = attempt- await runner._run_secondary_profile_reconnect("coder", Platform.TELEGRAM, predecessor)-- assert failed._held_inbound_events == []- assert predecessor._held_inbound_events == [in_connect, salvaged]- assert in_connect.source._transport_adapter_ref() is predecessor+ runner._redeliver_failed_obligations_for_platform = AsyncMock(side_effect=asyncio.CancelledError)+ if not installed:+ await runner._run_secondary_profile_reconnect("coder", Platform.TELEGRAM, predecessor)+ assert failed._held_inbound_events == []+ assert predecessor._held_inbound_events == [in_connect, salvaged]+ assert in_connect.source._transport_adapter_ref() is predecessor+ return+ with pytest.raises(asyncio.CancelledError):+ await runner._run_secondary_profile_reconnect("coder", Platform.TELEGRAM, predecessor)+ assert (predecessor._held_inbound_events or []) + (failed._held_inbound_events or []) == [salvaged]# --- Pause / resume circuit breaker ---