Skip to content

The agent that grows with you

fix(dashboard): guard empty-required_env platforms in the scoped enablement fallback

liuhao10249098c9a65af8
diff --git a/hermes_cli/web_routers/messaging.py b/hermes_cli/web_routers/messaging.py
index ccdbd85c652..f91650b133f 100644
--- a/hermes_cli/web_routers/messaging.py
+++ b/hermes_cli/web_routers/messaging.py
@@ -175,16 +175,23 @@ def _platform_enablement(
# .env and never a platforms: entry (#104614) — while an explicit
# enabled: false still wins, exactly like the real gateway config. Only the
# profile's own .env (env_on_disk) is consulted; os.environ would leak the
- # root install's credentials into this profile's state.
+ # root install's credentials into this profile's state. `bool(required)`
+ # guards the empty-required_env platforms (whatsapp, api_server, webhook,
+ # ...): `all()` over an empty tuple is True, which would report them
+ # enabled/configured with nothing set up — the unscoped branch reports
+ # enabled=False for that shape, so the scoped branch must agree.
raw_enabled = plat_cfg.get("enabled")
if raw_enabled is False:
enabled = False
else:
- enabled = bool(raw_enabled) or all(env_on_disk.get(key) for key in required)
+ enabled = bool(raw_enabled) or (
+ bool(required)
+ and all(env_on_disk.get(key) for key in required)
+ )
home_channel = hc if isinstance(hc, dict) else None
except Exception:
enabled, home_channel = False, None
- return enabled, all(env_on_disk.get(key) for key in required), home_channel
+ return enabled, bool(required) and all(env_on_disk.get(key) for key in required), home_channel
try:
from gateway.config import Platform, load_gateway_config
diff --git a/tests/hermes_cli/test_web_server_scoped_enablement.py b/tests/hermes_cli/test_web_server_scoped_enablement.py
new file mode 100644
index 00000000000..f976228eba8
--- /dev/null
+++ b/tests/hermes_cli/test_web_server_scoped_enablement.py
@@ -0,0 +1,89 @@
+"""Regression tests for the scoped messaging enablement fix (#104619).
+
+The scoped credential fallback must NOT report an empty-`required_env`
+platform (whatsapp, api_server, webhook) as enabled when nothing
+is configured. `all()` over an empty tuple would otherwise evaluate True.
+"""
+
+from hermes_cli.web_routers.messaging import _platform_enablement
+
+
+def _entry(required_env=("DISCORD_BOT_TOKEN",)):
+ return {"required_env": required_env}
+
+
+class TestScopedEmptyRequiredEnv:
+ def test_empty_required_env_with_no_config_is_NOT_enabled(self, monkeypatch):
+ monkeypatch.setattr(
+ "hermes_cli.web_routers.messaging.load_config", lambda: {}
+ )
+ enabled, configured, _ = _platform_enablement(
+ "whatsapp", _entry(required_env=()), {}, scoped=True
+ )
+ assert enabled is False
+
+ def test_empty_required_env_with_explicit_enabled_false(self, monkeypatch):
+ monkeypatch.setattr(
+ "hermes_cli.web_routers.messaging.load_config",
+ lambda: {"platforms": {"whatsapp": {"enabled": False}}},
+ )
+ enabled, _, _ = _platform_enablement(
+ "whatsapp", _entry(required_env=()), {}, scoped=True
+ )
+ assert enabled is False
+
+ def test_empty_required_env_with_explicit_enabled_true(self, monkeypatch):
+ monkeypatch.setattr(
+ "hermes_cli.web_routers.messaging.load_config",
+ lambda: {"platforms": {"whatsapp": {"enabled": True}}},
+ )
+ enabled, _, _ = _platform_enablement(
+ "whatsapp", _entry(required_env=()), {}, scoped=True
+ )
+ assert enabled is True
+
+
+class TestScopedWithCredentials:
+ def test_env_credentials_alone_enable(self, monkeypatch):
+ monkeypatch.setattr(
+ "hermes_cli.web_routers.messaging.load_config", lambda: {}
+ )
+ enabled, configured, _ = _platform_enablement(
+ "discord", _entry(), {"DISCORD_BOT_TOKEN": "tok"}, scoped=True
+ )
+ assert enabled is True
+ assert configured is True
+
+ def test_missing_credentials_stay_disabled(self, monkeypatch):
+ monkeypatch.setattr(
+ "hermes_cli.web_routers.messaging.load_config", lambda: {}
+ )
+ enabled, configured, _ = _platform_enablement(
+ "discord", _entry(), {}, scoped=True
+ )
+ assert enabled is False
+ assert configured is False
+
+
+class TestScopedConfiguredField:
+ # `configured` came from the same `all()`-over-empty expression, so an
+ # empty-`required_env` platform with nothing set up also reported
+ # configured=True; it must stay False for that shape too.
+ def test_empty_required_env_configured_stays_false(self, monkeypatch):
+ monkeypatch.setattr(
+ "hermes_cli.web_routers.messaging.load_config", lambda: {}
+ )
+ enabled, configured, _ = _platform_enablement(
+ "whatsapp", _entry(required_env=()), {}, scoped=True
+ )
+ assert enabled is False
+ assert configured is False
+
+ def test_credentials_report_configured_true(self, monkeypatch):
+ monkeypatch.setattr(
+ "hermes_cli.web_routers.messaging.load_config", lambda: {}
+ )
+ _, configured, _ = _platform_enablement(
+ "discord", _entry(), {"DISCORD_BOT_TOKEN": "tok"}, scoped=True
+ )
+ assert configured is True