Skip to content

The agent that grows with you

feat(telemetry): count classic CLI slash commands in shared metrics

teknium173edfc935399
diff --git a/cli.py b/cli.py
index 4e2f7e2570a..89b865df47e 100644
--- a/cli.py
+++ b/cli.py
@@ -1179,8 +1179,13 @@ class HermesCLI(CLIInitMixin, CLITuiRuntimeMixin, CLIProcessNotificationsMixin,
entry = (name, True)
return entry
- def process_command(self, command: str) -> bool:
- """Dispatch a slash command; returns False to exit the REPL."""
+ # Shared-metrics surface for user-typed commands; None where another process owns the count
+ # (the TUI slash worker: tui_gateway records the command it forwards).
+ _slash_metrics_surface: str | None = "cli"
+
+ def process_command(self, command: str, *, redispatch: bool = False) -> bool:
+ """Dispatch a slash command; returns False to exit the REPL. ``redispatch`` marks an internal
+ re-entry (quick-command alias, prefix expansion) so the user's command is counted once."""
cmd_lower = command.lower().strip() # lowercase only for matching; args keep their case
cmd_original = command.strip()
@@ -1189,6 +1194,9 @@ class HermesCLI(CLIInitMixin, CLITuiRuntimeMixin, CLIProcessNotificationsMixin,
_base_word = cmd_lower.split()[0].lstrip("/")
_cmd_def = _resolve_cmd(_base_word)
canonical = _cmd_def.name if _cmd_def else _base_word
+ if not redispatch and self._slash_metrics_surface:
+ from hermes_cli.observability.shared_metrics_events import record_slash_command
+ record_slash_command(command=canonical, surface=self._slash_metrics_surface)
# Observer-only pre_command plugin hook (return values ignored; never raises).
if _cmd_def is not None:
@@ -1243,7 +1251,7 @@ class HermesCLI(CLIInitMixin, CLITuiRuntimeMixin, CLIProcessNotificationsMixin,
target = qcmd.get("target", "").strip()
if target:
target = target if target.startswith("/") else f"/{target}"
- return self.process_command(f"{target} {user_args}".strip())
+ return self.process_command(f"{target} {user_args}".strip(), redispatch=True)
self._console_print(f"[bold red]Quick command '{base_cmd}' has no target defined[/]")
return True
if qtype != "exec":
@@ -1349,7 +1357,7 @@ class HermesCLI(CLIInitMixin, CLITuiRuntimeMixin, CLIProcessNotificationsMixin,
matches = shortest
if len(matches) == 1 and matches[0] != typed_base:
# Expand to the full name, preserving arguments.
- return self.process_command(matches[0] + cmd_original.strip()[len(typed_base):])
+ return self.process_command(matches[0] + cmd_original.strip()[len(typed_base):], redispatch=True)
if len(matches) > 1:
_cprint(f"{_ACCENT}Ambiguous command: {cmd_lower}{_RST}")
_cprint(f"{_DIM}Did you mean: {', '.join(sorted(matches))}?{_RST}")
diff --git a/tests/gateway/test_slash_command_metrics.py b/tests/gateway/test_slash_command_metrics.py
new file mode 100644
index 00000000000..6a9fa98c431
--- /dev/null
+++ b/tests/gateway/test_slash_command_metrics.py
@@ -0,0 +1,31 @@
+"""Messaging-gateway slash commands reach shared metrics once each, tagged surface ``gateway``."""
+
+from unittest.mock import AsyncMock
+
+import pytest
+
+from gateway.platforms.event import MessageEvent, MessageType
+from tests.gateway.test_gateway_command_dispatch_minimal import _make_runner, _make_source
+
+
+async def test_gateway_counts_each_user_slash_command_once(monkeypatch):
+ calls = []
+ monkeypatch.setattr(
+ "hermes_cli.observability.shared_metrics_events.record_slash_command", lambda **kw: calls.append(kw))
+ runner, _adapter = _make_runner()
+ runner._is_user_authorized_for_source = lambda _source: True
+ runner._admit_bot_message_for_source = lambda _source: True
+ runner._hm_dispatch_idle_commands = AsyncMock(return_value=(True, "ok"))
+
+ def event(text, internal=False):
+ return MessageEvent(text=text, message_type=MessageType.TEXT, source=_make_source(),
+ message_id="m1", internal=internal)
+
+ typed = event("/status now")
+ await runner._handle_message(typed)
+ await runner._handle_message(typed) # a queued busy-path event re-enters when drained
+ await runner._handle_message(event("hello"))
+ await runner._handle_message(event("/status", internal=True))
+
+ assert calls == [{"command": "status", "surface": "gateway"}]
diff --git a/tests/hermes_cli/test_slash_command_metrics.py b/tests/hermes_cli/test_slash_command_metrics.py
new file mode 100644
index 00000000000..c6f6b8249f4
--- /dev/null
+++ b/tests/hermes_cli/test_slash_command_metrics.py
@@ -0,0 +1,19 @@
+"""Classic-CLI slash commands reach shared metrics once each: canonical name, surface ``cli``."""
+
+from cli import HermesCLI
+
+
+def test_cli_counts_each_typed_slash_command_once(monkeypatch):
+ calls = []
+ monkeypatch.setattr(
+ "hermes_cli.observability.shared_metrics_events.record_slash_command", lambda **kw: calls.append(kw))
+ ran = []
+ monkeypatch.setattr(HermesCLI, "_cmd_new", lambda self, cmd: ran.append(cmd))
+ cli = object.__new__(HermesCLI)
+ cli.config = {"quick_commands": {"rs": {"type": "alias", "target": "/reset"}}}
+
+ cli.process_command("/reset")
+ cli.process_command("/rs") # a user alias re-dispatches /reset internally
+
+ assert ran == ["/reset", "/reset"]
+ assert calls == [{"command": "new", "surface": "cli"}, {"command": "rs", "surface": "cli"}]