Files
hermes-agent/tests/gateway/test_startup_no_eager_platform_install.py
T
kshitij 0d32607c62 fix(gateway): split check_fn (passive probe) from ensure_deps_fn (active installer)
PlatformEntry.check_fn served three contradictory roles: adapter-creation
gate, config auto-enablement gate, and status display. Plugins had to pick
one function for all three:

- Active installer as check_fn (discord/slack/telegram/matrix/dingtalk/
  feishu): every status display could pip-install SDKs as a side effect
  (the desktop 94% boot-loop class).
- Passive probe as check_fn (teams, wecom_callback): create_adapter()
  returned None before connect() could lazy-install, so the SDK never
  installed (#79812 deadlock; wecom_callback's platform.wecom_callback
  LAZY_DEPS entry was dead code).

The split makes both call sites correct by construction:

- check_fn is now contractually PASSIVE (probe only, never installs).
- New optional PlatformEntry.ensure_deps_fn is the ACTIVE installer;
  create_adapter() runs it exactly when check_fn is False — the platform
  is enabled+configured and the gateway is about to connect it.
- Config enablement keeps a configured platform whose deps are missing
  but installable; the install itself is deferred to create_adapter().
- Status surfaces (_platform_status, hermes status) read only the
  passive probe and can never trigger pip.

Migrated all lazy-installable platform plugins to the split; platforms
with no optional deps (irc/ntfy/buzz/simplex/line/a2a/...) are unchanged
— no ensure_deps_fn means a False check_fn stays a hard block.
wecom_callback gains a working installer for the first time.

Builds on @xxxigm's #79812 (both commits cherry-picked with authorship
preserved), reworking the check_fn swap into the two-field split so the
Teams fix doesn't reintroduce install-on-status.
2026-08-07 13:28:43 +05:30

74 lines
2.9 KiB
Python

"""Regression tests: ``_apply_env_overrides`` must not lazy-install platform
SDKs for platforms the user has not configured.
Historically ``PlatformEntry.check_fn`` doubled as the lazy-installer
(it pip-installed the platform SDK as a side effect). The enablement sweep
in ``_apply_env_overrides`` used to call ``check_fn`` for *every* registered
plugin platform unconditionally, so a single ``load_gateway_config()`` —
which the desktop/dashboard readiness probe (``GET /api/status``) awaits
synchronously — pip-installed Discord, Telegram, Slack, Feishu and Dingtalk
even with ``platforms: none``. That blocked startup until every install
finished and made the desktop app time out and boot-loop (stuck at 94%).
Two layers of protection now exist:
1. The sweep consults the cheap ``is_connected`` credential check FIRST and
only reaches the dependency check for platforms that are already enabled
or actually configured (this file pins that contract).
2. ``check_fn`` is now defined as a PASSIVE probe; the ACTIVE installer
lives on ``ensure_deps_fn`` and only runs from
``platform_registry.create_adapter()`` (#79812).
"""
from unittest.mock import MagicMock, patch
import pytest
from gateway.config import GatewayConfig, Platform, PlatformConfig, _apply_env_overrides
from gateway.platform_registry import PlatformEntry, platform_registry
@pytest.fixture
def isolated_registry():
"""Run with a registry containing only the entries the test registers."""
original = dict(platform_registry._entries)
platform_registry._entries.clear()
try:
# ``_apply_env_overrides`` calls ``discover_plugins()`` (idempotent),
# which would re-register the real bundled platforms and clobber the
# fakes below. Neutralize it so the test controls the registry.
with patch("hermes_cli.plugins.discover_plugins", lambda *a, **k: None):
yield platform_registry
finally:
platform_registry._entries.clear()
platform_registry._entries.update(original)
def _register_fake_platform(name, *, check_fn, is_connected):
platform_registry.register(
PlatformEntry(
name=name,
label=name.title(),
adapter_factory=lambda cfg: MagicMock(),
check_fn=check_fn,
is_connected=is_connected,
source="plugin",
)
)
def test_unconfigured_platform_is_not_probed_for_install(isolated_registry):
# is_connected reports "no credentials" → the platform must be skipped
# without ever calling check_fn (which would lazy-install the SDK).
check_fn = MagicMock(return_value=True)
_register_fake_platform(
"discord", check_fn=check_fn, is_connected=lambda cfg: False
)
config = GatewayConfig()
_apply_env_overrides(config)
check_fn.assert_not_called()
assert not config.platforms.get(Platform.DISCORD, PlatformConfig()).enabled