The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): share the adapter-heal predicate between startup and the reconnect watcher
kshitijk4poorfa12a55314f2
diff --git a/gateway/run_adapters.py b/gateway/run_adapters.pyindex 4aca32939c2..0252ca9aa85 100644--- a/gateway/run_adapters.py+++ b/gateway/run_adapters.py@@ -774,6 +774,21 @@ class GatewayAdapterLifecycleMixin:info["next_retry"] = time.monotonic() + backoffreturn backoff+ def _adapter_may_heal(self, platform, platform_config) -> bool:+ """Whether a platform whose ``_create_adapter`` returned None can heal without a config change.++ Only an unregistered plugin can (a plugin load can fail transiently). A builtin whose probe fails+ (missing deps/creds), a registered plugin returning None, or an empty bot credential needs a config+ change: retrying it re-warns forever at the backoff cap (#5196 fleet nodes). Shared by startup and+ the reconnect watcher so both classify the same platform the same way."""+ from gateway.platform_registry import platform_registry+ from gateway.run import _BUILTIN_ADAPTERS, _platform_has_bot_credential+ return (+ platform not in _BUILTIN_ADAPTERS+ and not platform_registry.is_registered(platform.value)+ and _platform_has_bot_credential(platform, platform_config)+ )+async def _reconnect_failed_platform(self, platform, now: float) -> None:"""One watcher pass for a queued platform: gate, attempt, and record the outcome."""from gateway.run import _dispose_unused_adapter, _platform_has_bot_credential@@ -797,7 +812,16 @@ class GatewayAdapterLifecycleMixin:try:adapter = self._create_adapter(platform, platform_config)if not adapter:- # No adapter yet (plugin unregistered or deps missing): keep it queued so it heals once one appears.+ if not self._adapter_may_heal(platform, platform_config):+ # Became builtin/registered-but-None: a config change is needed, so stop retrying.+ self._update_platform_runtime_status(+ platform.value, platform_state="fatal", error_code="adapter_unavailable",+ error_message=_adapter_unavailable_message(platform, retrying=False),+ needs_attention=True,+ )+ self._drop_from_reconnect_queue(platform, "adapter creation returned None")+ return+ # Unregistered plugin: keep it queued so it heals once the plugin registers.backoff = self._bump_reconnect_backoff(platform, info, attempt, "adapter_unavailable", _adapter_unavailable_message(platform),)diff --git a/gateway/run_startup.py b/gateway/run_startup.pyindex 21beaf487bb..dc9963721d9 100644--- a/gateway/run_startup.py+++ b/gateway/run_startup.py@@ -1161,8 +1161,7 @@ class GatewayStartupMixin:async def _start_prefilter_platforms(self) -> Tuple[bool, int, list, list]:"""Create + wire an adapter per enabled platform (no connects). Returns(aborted, enabled_platform_count, multiplex_skipped_platforms, pending_connects)."""- from gateway.platform_registry import platform_registry- from gateway.run import _BUILTIN_ADAPTERS, _platform_has_bot_credential+ from gateway.run import _platform_has_bot_credentialfrom gateway.run_adapters import _adapter_unavailable_messageenabled_platform_count = 0_multiplex_on = self._multiplex_on()@@ -1198,16 +1197,9 @@ class GatewayStartupMixin:"No adapter for '%s' -- is the plugin installed? ""(platform is enabled in config.yaml but no plugin registered it)", platform.value,)- # Only an unregistered plugin can heal on its own (a plugin load can fail transiently), so- # only that case is queued for the reconnect watcher. A builtin whose probe fails (missing- # deps/creds), a registered plugin returning None, or an empty bot credential needs a config- # change: queueing it would re-warn forever at the backoff cap (#5196 fleet nodes). Either- # way flag it so the unserved enabled platform is visible.- heals = (- platform not in _BUILTIN_ADAPTERS- and not platform_registry.is_registered(platform.value)- and _platform_has_bot_credential(platform, platform_config)- )+ # Only a platform that can heal on its own is queued for the reconnect watcher; either way+ # flag it so the unserved enabled platform is visible.+ heals = self._adapter_may_heal(platform, platform_config)self._update_platform_runtime_status(platform.value, platform_state="retrying" if heals else "fatal",error_code="adapter_unavailable",diff --git a/tests/gateway/test_startup_failed_platform_reports_degraded.py b/tests/gateway/test_startup_failed_platform_reports_degraded.pyindex 3b0835b1c1d..940b6b577ab 100644--- a/tests/gateway/test_startup_failed_platform_reports_degraded.py+++ b/tests/gateway/test_startup_failed_platform_reports_degraded.py@@ -152,6 +152,9 @@ async def test_missing_adapter_degrades_only_the_unserved_enabled_platform(monkePlatform.TELEGRAM: PlatformConfig(enabled=True, token="***"),Platform.DISCORD: PlatformConfig(enabled=True, token="***"),Platform.SLACK: PlatformConfig(enabled=False, token="***"),+ # A builtin whose probe fails on missing creds needs a config change: flagged once, never+ # queued (else it re-warns forever at the backoff cap on fleet nodes, #5196).+ Platform.BLUEBUBBLES: PlatformConfig(enabled=True),},lambda platform, platform_config: _HealthyAdapter() if platform is Platform.TELEGRAM else None,)@@ -163,30 +166,23 @@ async def test_missing_adapter_degrades_only_the_unserved_enabled_platform(monkeassert state["platforms"]["discord"]["error_code"] == "adapter_unavailable"assert state["platforms"]["discord"]["needs_attention"] is Trueassert "slack" not in state["platforms"]+ bluebubbles = state["platforms"]["bluebubbles"]+ assert (bluebubbles["state"], bluebubbles["error_code"]) == ("fatal", "adapter_unavailable")+ assert bluebubbles["needs_attention"] is True+ assert "Retrying" not in bluebubbles["error_message"]assert list(runner._failed_platforms) == [Platform.DISCORD] # the reconnect watcher owns itfinally:await runner.stop()@pytest.mark.asyncio- "platform, platform_config, heals",- [- # An unregistered plugin can still register later: queued, retried, healed.- (Platform.DISCORD, PlatformConfig(enabled=True, token="***"), True),- # A builtin whose probe fails on missing creds needs a config change: flagged once, never queued- # (else it re-warns forever at the backoff cap on fleet nodes, #5196).- (Platform.BLUEBUBBLES, PlatformConfig(enabled=True), False),- ],- ids=["unregistered-plugin", "builtin-missing-creds"],-)-async def test_adapterless_platform_heals_once_its_adapter_appears(- monkeypatch, tmp_path, platform, platform_config, heals-):- """A plugin that registers late: the queued platform stays queued, then connects."""+async def test_adapterless_platform_heals_once_its_adapter_appears(monkeypatch, tmp_path):+ """A plugin that registers late: the queued platform stays queued, then connects. One that turns+ into a registered plugin still returning None needs a config change, so the watcher drops it."""+ platform = Platform.DISCORDadapters = {}runner = _runner(- monkeypatch, tmp_path, {platform: platform_config},+ monkeypatch, tmp_path, {platform: PlatformConfig(enabled=True, token="***")},lambda platform, platform_config: adapters.get(platform),)try:@@ -194,11 +190,6 @@ async def test_adapterless_platform_heals_once_its_adapter_appears(status = read_runtime_status()["platforms"][platform.value]assert status["error_code"] == "adapter_unavailable"assert status["needs_attention"] is True- if not heals:- assert runner._failed_platforms == {}- assert status["state"] == "fatal"- assert "Retrying" not in status["error_message"]- returnawait runner._reconnect_failed_platform(platform, time.monotonic() + 60)assert runner._failed_platforms[platform]["attempts"] == 2 # still missing: kept queuedassert "check the plugin" in read_runtime_status()["platforms"][platform.value]["error_message"]@@ -212,5 +203,17 @@ async def test_adapterless_platform_heals_once_its_adapter_appears(status = read_runtime_status()["platforms"][platform.value]assert status["state"] == "connected"assert status["needs_attention"] is False++ # Registered plugin whose factory returns None: the watcher marks it fatal and stops retrying.+ slack = Platform.SLACK+ runner._failed_platforms[slack] = runner._startup_retry_entry(+ slack, None, PlatformConfig(enabled=True, token="***"),+ )+ monkeypatch.setattr("gateway.platform_registry.platform_registry.is_registered", lambda name: True)+ await runner._reconnect_failed_platform(slack, time.monotonic() + 3600)+ assert slack not in runner._failed_platforms+ status = read_runtime_status()["platforms"][slack.value]+ assert (status["state"], status["error_code"]) == ("fatal", "adapter_unavailable")+ assert "Retrying" not in status["error_message"]finally:await runner.stop()