Skip to content

The agent that grows with you

fix(cli): resolve deferred platform plugin for its top-level CLI command

Bartok9bff3aa3c1e78
diff --git a/hermes_cli/main.py b/hermes_cli/main.py
index 2ccfe808d4f..ce6ca6f2bbf 100644
--- a/hermes_cli/main.py
+++ b/hermes_cli/main.py
@@ -15245,6 +15245,35 @@ def _plugin_cli_discovery_needed() -> bool:
return True
+def _resolve_deferred_platform_cli_command(command_name: str | None) -> None:
+ """Materialize the deferred platform whose top-level CLI command matches.
+
+ Bundled platform plugins are cheap-registered as *deferred* entries to
+ avoid importing every gateway SDK during normal startup. A platform that
+ registers a top-level ``hermes <name>`` command (e.g. Photon ->
+ ``ctx.register_cli_command(name="photon", ...)``) only runs that side
+ effect when its module is imported. On the unknown-top-level-command slow
+ path, ``discover_plugins()`` records the deferred loader but does not
+ import it, so the CLI registration never happens and ``hermes photon``
+ fails with argparse ``invalid choice`` (issue #54678).
+
+ Resolving only the platform whose name matches the first positional token
+ keeps normal startup cheap while making the targeted command available.
+ """
+ if not command_name:
+ return
+ try:
+ from gateway.platform_registry import platform_registry
+
+ platform_registry.get(command_name)
+ except Exception as exc:
+ logging.getLogger(__name__).debug(
+ "Deferred platform CLI resolution failed for %s: %s",
+ command_name,
+ exc,
+ )
+
+
_AGENT_COMMANDS = {None, "chat", "acp", "rl"}
_AGENT_SUBCOMMANDS = {
"cron": ("cron_command", {"run", "tick"}),
@@ -16105,6 +16134,11 @@ def main():
seen_plugin_commands.add(cmd_info["name"])
discover_plugins()
+ # A bundled platform whose top-level CLI command is the one being
+ # invoked is still only a deferred entry at this point; import it
+ # so its register_cli_command side effect runs before we read
+ # _cli_commands (issue #54678).
+ _resolve_deferred_platform_cli_command(_first_positional_argv())
for cmd_info in get_plugin_manager()._cli_commands.values():
if cmd_info["name"] in seen_plugin_commands:
continue
diff --git a/tests/hermes_cli/test_startup_plugin_gating.py b/tests/hermes_cli/test_startup_plugin_gating.py
index 6028b3ea2d1..813e40b51f6 100644
--- a/tests/hermes_cli/test_startup_plugin_gating.py
+++ b/tests/hermes_cli/test_startup_plugin_gating.py
@@ -32,6 +32,7 @@ from hermes_cli.main import (
_BUILTIN_SUBCOMMANDS,
_first_positional_argv,
_plugin_cli_discovery_needed,
+ _resolve_deferred_platform_cli_command,
)
@@ -178,3 +179,73 @@ def test_builtin_set_has_no_phantom_entries():
f"_BUILTIN_SUBCOMMANDS has entries that are not registered as "
f"top-level subparsers: {sorted(phantom)}"
)
+
+
+# ── _resolve_deferred_platform_cli_command (issue #54678) ──────────────────
+
+
+def test_deferred_platform_cli_resolution_targets_matching_platform():
+ """The slow path must import the deferred platform whose name matches the
+ invoked command, so its register_cli_command side effect fires.
+
+ Photon registers ``hermes photon`` only when its adapter module is
+ imported; on the unknown-command slow path the platform is still a
+ deferred entry, so without this resolution step the CLI command stays
+ absent and argparse rejects ``photon`` (issue #54678).
+ """
+ from hermes_cli import main as _main
+
+ class _FakeRegistry:
+ def __init__(self):
+ self.resolved: list[str] = []
+
+ def get(self, name):
+ self.resolved.append(name)
+ return None
+
+ fake = _FakeRegistry()
+ fake_module = type(sys)("gateway.platform_registry")
+ fake_module.platform_registry = fake
+ with patch.dict(sys.modules, {"gateway.platform_registry": fake_module}):
+ _resolve_deferred_platform_cli_command("photon")
+
+ assert fake.resolved == ["photon"]
+
+
+def test_deferred_platform_cli_resolution_ignores_empty_command():
+ """A None/empty first token (bare ``hermes`` / flags only) must not touch
+ the registry — normal startup stays cheap."""
+ from hermes_cli import main as _main
+
+ class _FakeRegistry:
+ def __init__(self):
+ self.resolved: list[str] = []
+
+ def get(self, name): # pragma: no cover - must not be called
+ self.resolved.append(name)
+ return None
+
+ fake = _FakeRegistry()
+ fake_module = type(sys)("gateway.platform_registry")
+ fake_module.platform_registry = fake
+ with patch.dict(sys.modules, {"gateway.platform_registry": fake_module}):
+ _resolve_deferred_platform_cli_command(None)
+ _resolve_deferred_platform_cli_command("")
+
+ assert fake.resolved == []
+
+
+def test_deferred_platform_cli_resolution_swallows_registry_errors():
+ """A registry/import failure must be logged-and-ignored, never crash the
+ CLI startup path."""
+ from hermes_cli import main as _main
+
+ class _BoomRegistry:
+ def get(self, name):
+ raise RuntimeError("boom")
+
+ fake_module = type(sys)("gateway.platform_registry")
+ fake_module.platform_registry = _BoomRegistry()
+ with patch.dict(sys.modules, {"gateway.platform_registry": fake_module}):
+ # Must not raise.
+ _resolve_deferred_platform_cli_command("photon")