The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(discord): escalate the first socket_closed liveness strike
liuhao1024b1a69d81788d
diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.pyindex e0b91aafe81..777f576a6e1 100644--- a/plugins/platforms/discord/adapter.py+++ b/plugins/platforms/discord/adapter.py@@ -1753,6 +1753,12 @@ class DiscordAdapter(DiscordMediaMixin, BasePlatformAdapter):returnclient = self._clientif not self._running or client is None or self._disconnecting:+ # The probe must never disappear silently (#118487): an exit here leaves the+ # gateway alive with no watchdog, so say why before going away.+ logger.info(+ "[%s] Discord liveness probe exiting (running=%s, client=%s, disconnecting=%s)",+ self.name, self._running, client is not None, self._disconnecting,+ )returntry:healthy, reason = self._read_websocket_health(client)@@ -1761,6 +1767,14 @@ class DiscordAdapter(DiscordMediaMixin, BasePlatformAdapter):healthy = Falsereason = "health_check_error"if healthy:+ if failures:+ # discord.py swaps in a fresh socket while resuming, so a transport-side+ # sample can read healthy again while events never resumed (#118487) —+ # logging the reset keeps that distinguishable from a dead probe task.+ logger.info(+ "[%s] Discord Gateway WebSocket healthy again after %d unhealthy sample(s)",+ self.name, failures,+ )failures = 0continuefailures += 1@@ -1768,13 +1782,21 @@ class DiscordAdapter(DiscordMediaMixin, BasePlatformAdapter):"[%s] Discord Gateway WebSocket unhealthy (%s, %d/%d)", self.name, reason, failures,threshold,)- if failures < threshold:+ # A closed transport (``socket_closed``) is a confirmed death, not a suspicion:+ # the resume path replaces the socket, the next sample reads healthy, and this+ # counter silently resets — while a resumed-but-deaf session stays event-starved+ # until the multi-hour event-silence default elapses (#118487). Escalate the+ # first ``socket_closed`` strike; soft signals (ack staleness, latency, silence)+ # keep the confirmation threshold.+ if failures < threshold and reason != "socket_closed":continue# Mark recovery before closing: Bot.start()'s done callback must not overwrite this reason.self._disconnecting = Truelogger.error(- "[%s] Discord Gateway WebSocket remained unhealthy (%s); forcing reconnect",- self.name, reason,+ "[%s] Discord Gateway WebSocket %s (%s, %d/%d); forcing reconnect",+ self.name,+ "transport closed" if reason == "socket_closed" else "remained unhealthy",+ reason, failures, threshold,)self._set_fatal_error("discord_websocket_health_stale",diff --git a/tests/gateway/test_discord_liveness.py b/tests/gateway/test_discord_liveness.pyindex 04b6e5a3374..c962f6333b2 100644--- a/tests/gateway/test_discord_liveness.py+++ b/tests/gateway/test_discord_liveness.py@@ -355,3 +355,124 @@ async def test_disconnect_cancels_liveness_task(monkeypatch):await adapter.disconnect()assert task.done()assert adapter._liveness_task is None+++async def test_socket_closed_first_strike_forces_reconnect(monkeypatch, caplog):+ """A closed transport is a confirmed death — strike 1 must reconnect (#118487).++ Pre-fix, the first ``socket_closed`` strike logged ``1/2`` and waited for a+ confirming strike that never came: discord.py swaps in a fresh socket while+ resuming, the next transport-side sample reads healthy, and the counter+ silently resets while a resumed-but-deaf session stays event-starved until+ the multi-hour event-silence default elapses.+ """+ adapter = _make_adapter(monkeypatch, interval=0.01, threshold=2)+ handler = AsyncMock()+ adapter.set_fatal_error_handler(handler)++ calls = 0++ def factory(**kwargs):+ bot = _LiveBot(intents=kwargs["intents"], allowed_mentions=kwargs.get("allowed_mentions"))+ bot.fetch_user = AsyncMock()+ return bot++ def _probe(client):+ nonlocal calls+ calls += 1+ return False, "socket_closed"++ monkeypatch.setattr(adapter, "_read_websocket_health", _probe)++ with caplog.at_level("INFO", logger="plugins.platforms.discord.adapter"):+ await _connect(adapter, monkeypatch, factory)+ await _wait_until(+ lambda: handler.called,+ "fatal handler not called after first socket_closed strike",+ )++ assert calls == 1, "first socket_closed strike must escalate without a confirming strike"+ assert adapter._disconnecting is True+ errors = [r.getMessage() for r in caplog.records if r.levelname == "ERROR"]+ assert any("transport closed (socket_closed, 1/2)" in m for m in errors)++ await adapter.disconnect()+++async def test_soft_unhealthy_signal_still_requires_threshold_strikes(monkeypatch):+ """Soft signals (ack staleness, latency, silence) keep the confirmation threshold.++ The strike-1 escalation is reserved for ``socket_closed`` only; treating every+ unhealthy reason as terminal would turn one slow heartbeat sample into a reconnect.+ """+ adapter = _make_adapter(monkeypatch, interval=0.01, threshold=3)+ handler = AsyncMock()+ adapter.set_fatal_error_handler(handler)++ calls = 0++ def factory(**kwargs):+ bot = _LiveBot(intents=kwargs["intents"], allowed_mentions=kwargs.get("allowed_mentions"))+ bot.fetch_user = AsyncMock()+ return bot++ def _probe(client):+ nonlocal calls+ calls += 1+ return False, "ack_stale"++ monkeypatch.setattr(adapter, "_read_websocket_health", _probe)++ await _connect(adapter, monkeypatch, factory)+ await _wait_until(+ lambda: handler.called,+ "fatal handler not called after threshold strikes",+ )++ assert calls >= 3, "soft unhealthy signals must not reconnect before the threshold"++ await adapter.disconnect()+++async def test_recovery_after_unhealthy_streak_is_logged(monkeypatch, caplog):+ """A counter reset must leave a trace (#118487).++ Pre-fix, ``failures = 0`` on a healthy sample was silent, so an incident log+ showing one strike and then nothing was indistinguishable between "probe+ turned healthy again" and "probe task died".+ """+ adapter = _make_adapter(monkeypatch, interval=0.01, threshold=3)+ handler = AsyncMock()+ adapter.set_fatal_error_handler(handler)++ calls = 0++ def factory(**kwargs):+ bot = _LiveBot(intents=kwargs["intents"], allowed_mentions=kwargs.get("allowed_mentions"))+ bot.fetch_user = AsyncMock()+ return bot++ def _probe(client):+ nonlocal calls+ calls += 1+ # One soft unhealthy sample, then healthy forever: far below the threshold.+ return (False, "ack_stale") if calls == 1 else (True, "healthy")++ monkeypatch.setattr(adapter, "_read_websocket_health", _probe)++ with caplog.at_level("INFO", logger="plugins.platforms.discord.adapter"):+ await _connect(adapter, monkeypatch, factory)+ await _wait_until(+ lambda: any(+ "healthy again after 1 unhealthy sample" in r.getMessage()+ for r in caplog.records+ ),+ "counter reset after an unhealthy streak was never logged",+ )++ handler.assert_not_called()++ await adapter.disconnect()