Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions hermes_cli/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1764,16 +1764,55 @@ def _load_plugin(self, manifest: PluginManifest) -> None:
if self._plugin_commands[c].get("plugin") == manifest.name
]
loaded.enabled = True

# Warn when all required env vars are present but the plugin
# registered nothing on any surface. Normalize requires_env
# entries (str or dict) before checking the process environment,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This validation runs after register_fn(ctx). Move missing-env normalization and its warning before registration: a plugin that accesses a missing required variable directly will raise into the generic load-error path before this branch can name the declared variable.

# mirroring hermes_cli/plugins_cmd.py:307-314.
_missing_env: list[str] = []
for _entry in manifest.requires_env or []:
if isinstance(_entry, str):
_var_name = _entry
elif isinstance(_entry, dict):
_var_name = _entry.get("name", "")
else:
_var_name = ""
if _var_name and not os.environ.get(_var_name):
_missing_env.append(_var_name)

if _missing_env:
logger.warning(
"Plugin '%s': required env var(s) not set: %s "
"-- plugin may be non-functional",
manifest.name, ", ".join(_missing_env),
)

_cli_cmd_count = sum(
1 for c in self._cli_commands
if self._cli_commands[c].get("plugin") == manifest.name
)
_nothing_registered = not any([
loaded.tools_registered,
loaded.hooks_registered,
loaded.middleware_registered,
loaded.commands_registered,
_cli_cmd_count,
])
if _nothing_registered and not manifest.requires_env:
logger.debug(
"Plugin '%s' registered no tools, hooks, middleware, "
"or commands -- it may register a provider or be a "
"no-op stub",
manifest.name,
)

logger.debug(
" registered: %d tool(s), %d hook(s), %d middleware, %d slash command(s), %d CLI command(s)",
len(loaded.tools_registered),
len(loaded.hooks_registered),
len(loaded.middleware_registered),
len(loaded.commands_registered),
sum(
1 for c in self._cli_commands
if self._cli_commands[c].get("plugin") == manifest.name
),
_cli_cmd_count,
)

except Exception as exc:
Expand Down
122 changes: 122 additions & 0 deletions tests/hermes_cli/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2201,3 +2201,125 @@ def test_dispatch_tool_invokes_handler_without_cli_ref(self):
assert calls[0][1].get("parent_agent") is None
finally:
registry.deregister("_test_dispatch_probe")


class TestRequiresEnvWarning:
"""Regression tests for issue #2768: _load_plugin() warns when required
env vars are missing, handling both str and dict manifest entries.
"""

def _make_manager(self, tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
mgr = PluginManager()
return mgr

def test_requires_env_str_missing_emits_warning(self, tmp_path, monkeypatch, caplog):
"""A missing env var declared as a plain string must produce a WARNING."""
import logging
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
monkeypatch.delenv("_TEST_MISSING_VAR_2768", raising=False)

plugin_dir = tmp_path / "plugins" / "envtest"
plugin_dir.mkdir(parents=True)
(plugin_dir / "__init__.py").write_text("def register(ctx): pass\n")

mgr = PluginManager()
manifest = PluginManifest(
name="envtest",
source="user",
path=str(plugin_dir),
requires_env=["_TEST_MISSING_VAR_2768"],
)

with caplog.at_level(logging.WARNING, logger="hermes_cli.plugins"):
mgr._load_plugin(manifest)

assert any(
"_TEST_MISSING_VAR_2768" in r.message
for r in caplog.records
if r.levelno == logging.WARNING
), "Expected WARNING about missing env var, got: " + str([r.message for r in caplog.records])

def test_requires_env_dict_missing_emits_warning(self, tmp_path, monkeypatch, caplog):
"""A missing env var declared as a dict {name: ...} must also warn."""
import logging
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
monkeypatch.delenv("_TEST_DICT_VAR_2768", raising=False)

plugin_dir = tmp_path / "plugins" / "envtest_dict"
plugin_dir.mkdir(parents=True)
(plugin_dir / "__init__.py").write_text("def register(ctx): pass\n")

mgr = PluginManager()
manifest = PluginManifest(
name="envtest_dict",
source="user",
path=str(plugin_dir),
requires_env=[{"name": "_TEST_DICT_VAR_2768", "description": "test var"}],
)

with caplog.at_level(logging.WARNING, logger="hermes_cli.plugins"):
mgr._load_plugin(manifest)

assert any(
"_TEST_DICT_VAR_2768" in r.message
for r in caplog.records
if r.levelno == logging.WARNING
)

def test_requires_env_present_no_warning(self, tmp_path, monkeypatch, caplog):
"""When the required env var IS set, no missing-env warning must fire."""
import logging
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
monkeypatch.setenv("_TEST_PRESENT_VAR_2768", "set")

plugin_dir = tmp_path / "plugins" / "envtest_ok"
plugin_dir.mkdir(parents=True)
(plugin_dir / "__init__.py").write_text("def register(ctx): pass\n")

mgr = PluginManager()
manifest = PluginManifest(
name="envtest_ok",
source="user",
path=str(plugin_dir),
requires_env=["_TEST_PRESENT_VAR_2768"],
)

with caplog.at_level(logging.WARNING, logger="hermes_cli.plugins"):
mgr._load_plugin(manifest)

assert not any(
"_TEST_PRESENT_VAR_2768" in r.message and r.levelno == logging.WARNING
for r in caplog.records
)

def test_provider_only_plugin_no_false_positive(self, tmp_path, monkeypatch, caplog):
"""A plugin that registers only a provider (no tools/hooks/commands)
must not produce a spurious empty-registration warning -- provider
registrations are not tracked in LoadedPlugin.
"""
import logging
monkeypatch.setenv("HERMES_HOME", str(tmp_path))

plugin_dir = tmp_path / "plugins" / "provider_only"
plugin_dir.mkdir(parents=True)
# Simulates a provider-only plugin like plugins/image_gen/fal
(plugin_dir / "__init__.py").write_text(
"def register(ctx):\n pass # registers a provider internally\n"
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a no-op registration rather than a provider registration, so it cannot verify the stated provider-only case. Exercise a real supported provider registration path, or name this test as a no-op plugin test.

mgr = PluginManager()
manifest = PluginManifest(
name="provider_only",
source="user",
path=str(plugin_dir),
requires_env=[],
)

with caplog.at_level(logging.WARNING, logger="hermes_cli.plugins"):
mgr._load_plugin(manifest)

warning_msgs = [r.message for r in caplog.records if r.levelno == logging.WARNING]
assert not any("registered no tools" in m for m in warning_msgs), (
f"False positive warning for provider-only plugin: {warning_msgs}"
)
Loading