Skip to content
Merged
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
20 changes: 17 additions & 3 deletions hermes_plugin_kit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ def register(ctx):
_MAX_LOG_CHARS = 200
_TOOL_NAME_RE = re.compile(r"^[a-z][a-z0-9_]*$")
_COMMAND_NAME_RE = re.compile(r"^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$")
_AGENT_LOOP_TOOL_NAMES = frozenset({"todo", "memory", "session_search", "delegate_task"})
# Fallback for _agent_loop_tool_names() outside a Hermes host; matches v0.22.5.
_AGENT_LOOP_TOOL_NAMES = frozenset({
"todo_list", "memory", "session_search", "delegate_task",
"todo", "cronjob", "process", "tour", "tip",
})
_RESERVED_NAMESPACE_PREFIXES = ("memory_",)
_SKILL_NAME_RE = re.compile(r"^[a-zA-Z0-9_-]+$")
_HOST_TOOL_IMPORTS = {
Expand Down Expand Up @@ -704,6 +708,15 @@ def as_dict(self) -> dict[str, Any]:
# Tool naming
# ---------------------------------------------------------------------------

def _agent_loop_tool_names() -> frozenset[str]:
"""Names Hermes intercepts or rewrites before registry dispatch, read from the host itself."""
try:
from model_tools import _AGENT_LOOP_TOOLS, _LEGACY_TOOL_ALIASES
except ImportError: # not inside a Hermes host (unit tests, tooling)
return _AGENT_LOOP_TOOL_NAMES
return frozenset(_AGENT_LOOP_TOOLS) | frozenset(_LEGACY_TOOL_ALIASES)


def validate_tool_name(name: str, *, namespace: str | None = None) -> str:
"""Validate a Hermes plugin tool name and return it unchanged.

Expand All @@ -717,7 +730,8 @@ def validate_tool_name(name: str, *, namespace: str | None = None) -> str:
raise ValueError(
f"tool name {name!r} must match {_TOOL_NAME_RE.pattern!r}"
)
if name in _AGENT_LOOP_TOOL_NAMES:
reserved = _agent_loop_tool_names()
if name in reserved:
raise ValueError(
f"tool name {name!r} is reserved by the Hermes agent loop"
)
Expand All @@ -731,7 +745,7 @@ def validate_tool_name(name: str, *, namespace: str | None = None) -> str:
raise ValueError(
f"tool namespace {namespace!r} must match {_TOOL_NAME_RE.pattern!r}"
)
if namespace in _AGENT_LOOP_TOOL_NAMES:
if namespace in reserved:
raise ValueError(
f"tool namespace {namespace!r} is reserved by the Hermes agent loop"
)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_hermes_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,14 @@ def test_lifecycle_calls_bind_to_real_plugincontext_signatures(self) -> None:
for method in ("launch", "status", "wait", "cancel", "result", "reconnect"):
self.assertTrue(callable(getattr(service, method)))

def test_reserved_tool_names_match_the_host(self) -> None:
"""The kit's offline fallback must equal what the deployed Hermes intercepts."""
from model_tools import _AGENT_LOOP_TOOLS, _LEGACY_TOOL_ALIASES # type: ignore

host = frozenset(_AGENT_LOOP_TOOLS) | frozenset(_LEGACY_TOOL_ALIASES)
self.assertEqual(hpk._agent_loop_tool_names(), host)
self.assertEqual(hpk._AGENT_LOOP_TOOL_NAMES, host)

def test_host_tool_invocation_reaches_real_telegram_media_contract(self) -> None:
"""Exercise Hermes media parsing and Telegram formatting without network I/O."""
import asyncio
Expand Down
6 changes: 6 additions & 0 deletions tests/test_kit.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ def reserved(args, **kwargs):
"""Reserved."""
return {}

def test_renamed_and_legacy_agent_loop_names_rejected(self) -> None:
# Hermes v0.22 intercepts todo_list and rewrites legacy names such as cronjob.
for name in ("todo_list", "cronjob", "tip"):
with self.subTest(name=name), self.assertRaisesRegex(ValueError, "reserved"):
hpk.validate_tool_name(name)

def test_reserved_core_namespace_prefix_rejected(self) -> None:
with self.assertRaisesRegex(ValueError, "reserved Hermes core namespace"):
hpk.tool_name("memory", "write", "entry")
Expand Down
Loading