Skip to content

The agent that grows with you

fix(whatsapp): name a missing aiohttp in the bridge preflight (salvage of #126557)

liuhao1024f5e21913ccf5
diff --git a/plugins/platforms/whatsapp/adapter.py b/plugins/platforms/whatsapp/adapter.py
index 4a8b085979f..35a20d37b13 100644
--- a/plugins/platforms/whatsapp/adapter.py
+++ b/plugins/platforms/whatsapp/adapter.py
@@ -264,6 +264,14 @@ def check_whatsapp_requirements() -> bool:
return False
+def _aiohttp_available() -> bool:
+ try:
+ import aiohttp # noqa: F401
+ return True
+ except ImportError:
+ return False
+
+
# Env vars bridge.js consumes; injected because a multiplexed subprocess's os.environ lacks the secondary profile's .env.
_BRIDGE_PASSTHROUGH_ENV = (
"WHATSAPP_ALLOWED_USERS", "WHATSAPP_ALLOW_FROM", "WHATSAPP_DM_POLICY", "WHATSAPP_GROUP_POLICY",
@@ -527,12 +535,18 @@ class WhatsAppAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):
return True
def _preflight(self) -> bool:
- """Node + bridge script + creds.json present, else a non-retryable fatal error (an unpaired bridge only prints QR codes; retries would pay 30s each)."""
+ """Node + aiohttp + bridge script + creds.json present, else a non-retryable fatal error (an unpaired bridge only prints QR codes; retries would pay 30s each)."""
bridge_path = Path(self._bridge_script)
creds_path = self._session_path / "creds.json"
checks = (
(check_whatsapp_requirements, ("[%s] Node.js not found. WhatsApp requires Node.js.", self.name),
"whatsapp_node_missing", "Node.js is not installed — install Node.js and re-run `hermes gateway`."),
+ # Every bridge health poll imports aiohttp; a sealed env that shipped a partial messaging extra
+ # used to swallow the ModuleNotFoundError and loop forever on "did not start in 15s" (#126358).
+ (_aiohttp_available, ("[%s] aiohttp not installed — the WhatsApp bridge health probe needs it.", self.name),
+ "whatsapp_aiohttp_missing",
+ "aiohttp is not installed — the WhatsApp bridge health probe needs it. "
+ "Run `hermes update` (or `uv pip install \"aiohttp==3.14.3\"`), then restart `hermes gateway`."),
(bridge_path.exists, ("[%s] Bridge script not found: %s", self.name, bridge_path),
"whatsapp_bridge_missing", f"WhatsApp bridge script missing at {bridge_path}."),
(creds_path.exists, ("[%s] WhatsApp is enabled but not paired (no creds.json at %s). Pair from the dashboard or run "
diff --git a/tests/gateway/test_whatsapp_aiohttp_missing.py b/tests/gateway/test_whatsapp_aiohttp_missing.py
new file mode 100644
index 00000000000..ec5b0f47215
--- /dev/null
+++ b/tests/gateway/test_whatsapp_aiohttp_missing.py
@@ -0,0 +1,110 @@
+"""WhatsApp must name a missing aiohttp instead of looping forever (#126358).
+
+A sealed env built by ``hermes update`` can ship a partial messaging extra
+without aiohttp. The adapter's requirement check only verified Node.js, so
+``connect()`` spawned the bridge and every health poll raised
+``ModuleNotFoundError`` — swallowed by ``except Exception: continue`` — and
+the gateway looped forever on the misleading
+"Bridge HTTP server did not start in 15s".
+"""
+
+import asyncio
+import sys
+from pathlib import Path
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from gateway.config import Platform
+
+
+def _make_adapter():
+ """Create a WhatsAppAdapter with test attributes (bypass __init__)."""
+ from plugins.platforms.whatsapp.adapter import WhatsAppAdapter
+
+ adapter = WhatsAppAdapter.__new__(WhatsAppAdapter)
+ adapter.platform = Platform.WHATSAPP
+ adapter.config = MagicMock()
+ adapter._bridge_port = 19877
+ adapter._bridge_script = "/tmp/test-bridge-126358.js"
+ adapter._session_path = Path("/tmp/test-wa-session-126358")
+ adapter._bridge_log_fh = None
+ adapter._bridge_log = None
+ adapter._bridge_process = None
+ adapter._reply_prefix = None
+ adapter._send_read_receipts = False
+ adapter._dm_policy = adapter._group_policy = "pairing"
+ adapter._allow_from = adapter._group_allow_from = set()
+ adapter._running = False
+ adapter._message_handler = None
+ adapter._fatal_error_code = None
+ adapter._fatal_error_message = None
+ adapter._fatal_error_retryable = True
+ adapter._fatal_error_handler = None
+ adapter._active_sessions = {}
+ adapter._pending_messages = {}
+ adapter._background_tasks = set()
+ adapter._auto_tts_disabled_chats = set()
+ adapter._message_queue = asyncio.Queue()
+ adapter._http_session = None
+ return adapter
+
+
+class TestMissingAiohttpNamed:
+ @pytest.mark.asyncio
+ async def test_connect_fails_fast_with_named_aiohttp_error(self):
+ """Node present + aiohttp missing -> non-retryable fatal naming aiohttp, no bridge spawn."""
+ from plugins.platforms.whatsapp.adapter import WhatsAppAdapter
+
+ adapter = _make_adapter()
+
+ with patch(
+ "plugins.platforms.whatsapp.adapter.find_node_executable",
+ return_value="/fake/node",
+ ), patch.object(Path, "exists", return_value=True), patch(
+ "subprocess.run", return_value=MagicMock(returncode=0)
+ ), patch(
+ "subprocess.Popen", return_value=MagicMock()
+ ) as mock_popen, patch(
+ "plugins.platforms.whatsapp.adapter.asyncio.sleep",
+ new_callable=AsyncMock,
+ ), patch.dict(
+ sys.modules, {"aiohttp": None}
+ ):
+ result = await adapter.connect()
+
+ assert result is False
+ # Fail fast: the bridge process must never be spawned.
+ mock_popen.assert_not_called()
+ # Named error, not the misleading "did not start in 15s" loop.
+ assert adapter.fatal_error_code == "whatsapp_aiohttp_missing"
+ assert "aiohttp" in (adapter.fatal_error_message or "").lower()
+ assert adapter.fatal_error_retryable is False
+
+ def test_preflight_names_missing_aiohttp(self):
+ """_preflight alone reports whatsapp_aiohttp_missing when the import fails."""
+ adapter = _make_adapter()
+
+ with patch(
+ "plugins.platforms.whatsapp.adapter.check_whatsapp_requirements",
+ return_value=True,
+ ), patch.object(Path, "exists", return_value=True), patch.dict(
+ sys.modules, {"aiohttp": None}
+ ):
+ ok = adapter._preflight()
+
+ assert ok is False
+ assert adapter.fatal_error_code == "whatsapp_aiohttp_missing"
+
+ def test_preflight_passes_with_aiohttp(self):
+ """With aiohttp importable the preflight keeps its Node/bridge/creds semantics."""
+ adapter = _make_adapter()
+
+ with patch(
+ "plugins.platforms.whatsapp.adapter.check_whatsapp_requirements",
+ return_value=True,
+ ), patch.object(Path, "exists", return_value=True):
+ ok = adapter._preflight()
+
+ assert ok is True
+ assert adapter.fatal_error_code is None