feat: add /tools disable/enable/list slash commands with session reset (#1652)

Add in-session tool management via /tools disable/enable/list, plus
hermes tools list/disable/enable CLI subcommands. Supports both
built-in toolsets (web, memory) and MCP tools (github:create_issue).

To preserve prompt caching, /tools disable/enable in a chat session
saves the change to config and resets the session cleanly — the user
is asked to confirm before the reset happens.

Also improves prefix matching: /qui now dispatches to /quit instead
of showing ambiguous when longer skill commands like /quint-pipeline
are installed.

Based on PR #1520 by @YanSte.

Co-authored-by: Yannick Stephan <YanSte@users.noreply.github.com>
This commit is contained in:
Teknium 2026-03-17 02:05:26 -07:00 committed by GitHub
parent f2414bfd45
commit 49043b7b7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 606 additions and 7 deletions

View file

@ -0,0 +1,207 @@
"""Tests for hermes tools disable/enable/list command (backend)."""
from argparse import Namespace
from unittest.mock import patch
from hermes_cli.tools_config import tools_disable_enable_command
# ── Built-in toolset disable ────────────────────────────────────────────────
class TestToolsDisableBuiltin:
def test_disable_removes_toolset_from_platform(self):
config = {"platform_toolsets": {"cli": ["web", "memory", "terminal"]}}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config") as mock_save:
tools_disable_enable_command(Namespace(tools_action="disable", names=["web"], platform="cli"))
saved = mock_save.call_args[0][0]
assert "web" not in saved["platform_toolsets"]["cli"]
assert "memory" in saved["platform_toolsets"]["cli"]
def test_disable_multiple_toolsets(self):
config = {"platform_toolsets": {"cli": ["web", "memory", "terminal"]}}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config") as mock_save:
tools_disable_enable_command(Namespace(tools_action="disable", names=["web", "memory"], platform="cli"))
saved = mock_save.call_args[0][0]
assert "web" not in saved["platform_toolsets"]["cli"]
assert "memory" not in saved["platform_toolsets"]["cli"]
assert "terminal" in saved["platform_toolsets"]["cli"]
def test_disable_already_absent_is_idempotent(self):
config = {"platform_toolsets": {"cli": ["memory"]}}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config") as mock_save:
tools_disable_enable_command(Namespace(tools_action="disable", names=["web"], platform="cli"))
saved = mock_save.call_args[0][0]
assert "web" not in saved["platform_toolsets"]["cli"]
# ── Built-in toolset enable ─────────────────────────────────────────────────
class TestToolsEnableBuiltin:
def test_enable_adds_toolset_to_platform(self):
config = {"platform_toolsets": {"cli": ["memory"]}}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config") as mock_save:
tools_disable_enable_command(Namespace(tools_action="enable", names=["web"], platform="cli"))
saved = mock_save.call_args[0][0]
assert "web" in saved["platform_toolsets"]["cli"]
def test_enable_already_present_is_idempotent(self):
config = {"platform_toolsets": {"cli": ["web"]}}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config") as mock_save:
tools_disable_enable_command(Namespace(tools_action="enable", names=["web"], platform="cli"))
saved = mock_save.call_args[0][0]
assert saved["platform_toolsets"]["cli"].count("web") == 1
# ── MCP tool disable ────────────────────────────────────────────────────────
class TestToolsDisableMcp:
def test_disable_adds_to_exclude_list(self):
config = {"mcp_servers": {"github": {"command": "npx"}}}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config") as mock_save:
tools_disable_enable_command(
Namespace(tools_action="disable", names=["github:create_issue"], platform="cli")
)
saved = mock_save.call_args[0][0]
assert "create_issue" in saved["mcp_servers"]["github"]["tools"]["exclude"]
def test_disable_already_excluded_is_idempotent(self):
config = {"mcp_servers": {"github": {"tools": {"exclude": ["create_issue"]}}}}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config") as mock_save:
tools_disable_enable_command(
Namespace(tools_action="disable", names=["github:create_issue"], platform="cli")
)
saved = mock_save.call_args[0][0]
assert saved["mcp_servers"]["github"]["tools"]["exclude"].count("create_issue") == 1
def test_disable_unknown_server_prints_error(self, capsys):
config = {"mcp_servers": {}}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config"):
tools_disable_enable_command(
Namespace(tools_action="disable", names=["unknown:tool"], platform="cli")
)
out = capsys.readouterr().out
assert "MCP server 'unknown' not found in config" in out
# ── MCP tool enable ──────────────────────────────────────────────────────────
class TestToolsEnableMcp:
def test_enable_removes_from_exclude_list(self):
config = {"mcp_servers": {"github": {"tools": {"exclude": ["create_issue", "delete_branch"]}}}}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config") as mock_save:
tools_disable_enable_command(
Namespace(tools_action="enable", names=["github:create_issue"], platform="cli")
)
saved = mock_save.call_args[0][0]
assert "create_issue" not in saved["mcp_servers"]["github"]["tools"]["exclude"]
assert "delete_branch" in saved["mcp_servers"]["github"]["tools"]["exclude"]
# ── Mixed targets ────────────────────────────────────────────────────────────
class TestToolsMixedTargets:
def test_disable_builtin_and_mcp_together(self):
config = {
"platform_toolsets": {"cli": ["web", "memory"]},
"mcp_servers": {"github": {"command": "npx"}},
}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config") as mock_save:
tools_disable_enable_command(Namespace(
tools_action="disable",
names=["web", "github:create_issue"],
platform="cli",
))
saved = mock_save.call_args[0][0]
assert "web" not in saved["platform_toolsets"]["cli"]
assert "create_issue" in saved["mcp_servers"]["github"]["tools"]["exclude"]
# ── List output ──────────────────────────────────────────────────────────────
class TestToolsList:
def test_list_shows_enabled_toolsets(self, capsys):
config = {"platform_toolsets": {"cli": ["web", "memory"]}}
with patch("hermes_cli.tools_config.load_config", return_value=config):
tools_disable_enable_command(Namespace(tools_action="list", platform="cli"))
out = capsys.readouterr().out
assert "web" in out
assert "memory" in out
def test_list_shows_mcp_excluded_tools(self, capsys):
config = {
"mcp_servers": {"github": {"tools": {"exclude": ["create_issue"]}}},
}
with patch("hermes_cli.tools_config.load_config", return_value=config):
tools_disable_enable_command(Namespace(tools_action="list", platform="cli"))
out = capsys.readouterr().out
assert "github" in out
assert "create_issue" in out
# ── Validation ───────────────────────────────────────────────────────────────
class TestToolsValidation:
def test_unknown_platform_prints_error(self, capsys):
config = {}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config"):
tools_disable_enable_command(
Namespace(tools_action="disable", names=["web"], platform="invalid_platform")
)
out = capsys.readouterr().out
assert "Unknown platform 'invalid_platform'" in out
def test_unknown_toolset_prints_error(self, capsys):
config = {"platform_toolsets": {"cli": ["web"]}}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config"):
tools_disable_enable_command(
Namespace(tools_action="disable", names=["nonexistent_toolset"], platform="cli")
)
out = capsys.readouterr().out
assert "Unknown toolset 'nonexistent_toolset'" in out
def test_unknown_toolset_does_not_corrupt_config(self):
config = {"platform_toolsets": {"cli": ["web", "memory"]}}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config") as mock_save:
tools_disable_enable_command(
Namespace(tools_action="disable", names=["nonexistent_toolset"], platform="cli")
)
saved = mock_save.call_args[0][0]
assert "web" in saved["platform_toolsets"]["cli"]
assert "memory" in saved["platform_toolsets"]["cli"]
def test_mixed_valid_and_invalid_applies_valid_only(self):
config = {"platform_toolsets": {"cli": ["web", "memory"]}}
with patch("hermes_cli.tools_config.load_config", return_value=config), \
patch("hermes_cli.tools_config.save_config") as mock_save:
tools_disable_enable_command(
Namespace(tools_action="disable", names=["web", "bad_toolset"], platform="cli")
)
saved = mock_save.call_args[0][0]
assert "web" not in saved["platform_toolsets"]["cli"]
assert "memory" in saved["platform_toolsets"]["cli"]

View file

@ -121,3 +121,40 @@ class TestSlashCommandPrefixMatching:
mock_help.assert_called_once()
printed = " ".join(str(c) for c in cli_obj.console.print.call_args_list)
assert "Ambiguous" not in printed
def test_shortest_match_preferred_over_longer_skill(self):
"""/qui should dispatch to /quit (5 chars) not report ambiguous with /quint-pipeline (15 chars)."""
cli_obj = _make_cli()
fake_skill = {"/quint-pipeline": {"name": "Quint Pipeline", "description": "test"}}
import cli as cli_mod
with patch.object(cli_mod, '_skill_commands', fake_skill):
# /quit is caught by the exact "/quit" branch → process_command returns False
result = cli_obj.process_command("/qui")
# Returns False because /quit was dispatched (exits chat loop)
assert result is False
printed = " ".join(str(c) for c in cli_obj.console.print.call_args_list)
assert "Ambiguous" not in printed
def test_tied_shortest_matches_still_ambiguous(self):
"""/re matches /reset and /retry (both 6 chars) — no unique shortest, stays ambiguous."""
cli_obj = _make_cli()
printed = []
import cli as cli_mod
with patch.object(cli_mod, '_cprint', side_effect=lambda t: printed.append(t)):
cli_obj.process_command("/re")
combined = " ".join(printed)
assert "Ambiguous" in combined or "Did you mean" in combined
def test_exact_typed_name_dispatches_over_longer_match(self):
"""/help typed with /help-extra skill installed → exact match wins."""
cli_obj = _make_cli()
fake_skill = {"/help-extra": {"name": "Help Extra", "description": ""}}
import cli as cli_mod
with patch.object(cli_mod, '_skill_commands', fake_skill), \
patch.object(cli_obj, 'show_help') as mock_help:
cli_obj.process_command("/help")
mock_help.assert_called_once()
printed = " ".join(str(c) for c in cli_obj.console.print.call_args_list)
assert "Ambiguous" not in printed

View file

@ -0,0 +1,121 @@
"""Tests for /tools slash command handler in the interactive CLI."""
from unittest.mock import MagicMock, patch, call
from cli import HermesCLI
def _make_cli(enabled_toolsets=None):
"""Build a minimal HermesCLI stub without running __init__."""
cli_obj = HermesCLI.__new__(HermesCLI)
cli_obj.enabled_toolsets = set(enabled_toolsets or ["web", "memory"])
cli_obj._command_running = False
cli_obj.console = MagicMock()
return cli_obj
# ── /tools (no subcommand) ──────────────────────────────────────────────────
class TestToolsSlashNoSubcommand:
def test_bare_tools_shows_tool_list(self):
cli_obj = _make_cli()
with patch.object(cli_obj, "show_tools") as mock_show:
cli_obj._handle_tools_command("/tools")
mock_show.assert_called_once()
def test_unknown_subcommand_falls_back_to_show_tools(self):
cli_obj = _make_cli()
with patch.object(cli_obj, "show_tools") as mock_show:
cli_obj._handle_tools_command("/tools foobar")
mock_show.assert_called_once()
# ── /tools list ─────────────────────────────────────────────────────────────
class TestToolsSlashList:
def test_list_calls_backend(self, capsys):
cli_obj = _make_cli()
with patch("hermes_cli.tools_config.load_config",
return_value={"platform_toolsets": {"cli": ["web"]}}), \
patch("hermes_cli.tools_config.save_config"):
cli_obj._handle_tools_command("/tools list")
out = capsys.readouterr().out
assert "web" in out
def test_list_does_not_modify_enabled_toolsets(self):
"""List is read-only — self.enabled_toolsets must not change."""
cli_obj = _make_cli(["web", "memory"])
with patch("hermes_cli.tools_config.load_config",
return_value={"platform_toolsets": {"cli": ["web"]}}):
cli_obj._handle_tools_command("/tools list")
assert cli_obj.enabled_toolsets == {"web", "memory"}
# ── /tools disable (session reset) ──────────────────────────────────────────
class TestToolsSlashDisableWithReset:
def test_disable_confirms_then_resets_session(self):
cli_obj = _make_cli(["web", "memory"])
with patch("hermes_cli.tools_config.load_config",
return_value={"platform_toolsets": {"cli": ["web", "memory"]}}), \
patch("hermes_cli.tools_config.save_config"), \
patch("hermes_cli.tools_config._get_platform_tools", return_value={"memory"}), \
patch("hermes_cli.config.load_config", return_value={}), \
patch.object(cli_obj, "new_session") as mock_reset, \
patch("builtins.input", return_value="y"):
cli_obj._handle_tools_command("/tools disable web")
mock_reset.assert_called_once()
assert "web" not in cli_obj.enabled_toolsets
def test_disable_cancelled_does_not_reset(self):
cli_obj = _make_cli(["web", "memory"])
with patch.object(cli_obj, "new_session") as mock_reset, \
patch("builtins.input", return_value="n"):
cli_obj._handle_tools_command("/tools disable web")
mock_reset.assert_not_called()
# Toolsets unchanged
assert cli_obj.enabled_toolsets == {"web", "memory"}
def test_disable_eof_cancels(self):
cli_obj = _make_cli(["web", "memory"])
with patch.object(cli_obj, "new_session") as mock_reset, \
patch("builtins.input", side_effect=EOFError):
cli_obj._handle_tools_command("/tools disable web")
mock_reset.assert_not_called()
def test_disable_missing_name_prints_usage(self, capsys):
cli_obj = _make_cli()
cli_obj._handle_tools_command("/tools disable")
out = capsys.readouterr().out
assert "Usage" in out
# ── /tools enable (session reset) ───────────────────────────────────────────
class TestToolsSlashEnableWithReset:
def test_enable_confirms_then_resets_session(self):
cli_obj = _make_cli(["memory"])
with patch("hermes_cli.tools_config.load_config",
return_value={"platform_toolsets": {"cli": ["memory"]}}), \
patch("hermes_cli.tools_config.save_config"), \
patch("hermes_cli.tools_config._get_platform_tools", return_value={"memory", "web"}), \
patch("hermes_cli.config.load_config", return_value={}), \
patch.object(cli_obj, "new_session") as mock_reset, \
patch("builtins.input", return_value="y"):
cli_obj._handle_tools_command("/tools enable web")
mock_reset.assert_called_once()
assert "web" in cli_obj.enabled_toolsets
def test_enable_missing_name_prints_usage(self, capsys):
cli_obj = _make_cli()
cli_obj._handle_tools_command("/tools enable")
out = capsys.readouterr().out
assert "Usage" in out