The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(dashboard): validate toolset names inside the target profile's scope
Teknium10a2efbaaab1
diff --git a/hermes_cli/web_routers/tools.py b/hermes_cli/web_routers/tools.pyindex bb1e8671f8e..db77075b9ff 100644--- a/hermes_cli/web_routers/tools.py+++ b/hermes_cli/web_routers/tools.py@@ -203,7 +203,8 @@ def _find_toolset_provider_row(def _require_known_toolset(name: str) -> None:- """400 for toolset keys outside the effective configurable set."""+ """400 for toolset keys outside the effective configurable set. Call it inside the target+ profile's scope, like ``GET /api/tools/toolsets``: plugin toolsets are registered per profile."""from hermes_cli.tools_config import _get_effective_configurable_toolsetsif name not in {ts_key for ts_key, _, _ in _get_effective_configurable_toolsets()}:@@ -286,12 +287,12 @@ async def toggle_toolset(name: str, body: ToolsetToggle, profile: Optional[str]_CONFIG_ONLY_TOOLSETS, _get_platform_tools, _save_platform_tools,_toolset_configuration_platform)- _require_known_toolset(name)target_platform = _toolset_configuration_platform(name)scope_profile = body.profile or profiledef _run():with config_write_scope(scope_profile):+ _require_known_toolset(name)config = load_config()if name in _CONFIG_ONLY_TOOLSETS:# Config-only capabilities (stt) toggle their own section's@@ -352,10 +353,9 @@ async def get_toolset_config(name: str, profile: Optional[str] = None):from hermes_cli.config import get_env_valuefrom hermes_cli.nous_subscription import get_nous_subscription_features- _require_known_toolset(name)-def _read():with _profile_scope(profile):+ _require_known_toolset(name)config = load_config()cat = TOOL_CATEGORIES.get(name)providers = []@@ -517,8 +517,6 @@ async def select_toolset_provider(from hermes_cli.nous_subscription import (MANAGED_FEATURE_COVERAGE_CATEGORY, get_nous_subscription_features)- _require_known_toolset(name)-if body.capability is not None:if name != "web":raise _bad_request("capability selection is only supported for the web toolset")@@ -532,6 +530,7 @@ async def select_toolset_provider(def _run():with _profile_scope(body.profile or profile):+ _require_known_toolset(name)with _CONFIG_MUTATION_LOCK:config = load_config()if body.capability is not None:@@ -594,10 +593,9 @@ async def save_toolset_env(name: str, body: ToolsetEnvUpdate, profile: Optional[can't write arbitrary env vars; a blank value means "leave unchanged"."""from hermes_cli.config import get_env_value, save_env_value- _require_known_toolset(name)-def _run():with _profile_scope(body.profile or profile):+ _require_known_toolset(name)config = load_config()allowed: set[str] = {e["key"]@@ -636,12 +634,17 @@ async def run_toolset_post_setup(``profile`` is threaded so hooks see the drawer's HERMES_HOME."""from hermes_cli.tools_config import valid_post_setup_keys- _require_known_toolset(name)- if body.key not in valid_post_setup_keys():- raise _bad_request(f"Unknown post-setup key: {body.key}")+ scope_profile = body.profile or profile++ def _validate():+ # Plugin toolsets and plugin providers' post-setup keys are both per profile.+ _require_known_toolset(name)+ if body.key not in valid_post_setup_keys():+ raise _bad_request(f"Unknown post-setup key: {body.key}")+ await scoped_to_thread(scope_profile, _validate)result = spawn_profile_action(- body.profile or profile, ["tools", "post-setup", body.key], "tools-post-setup",+ scope_profile, ["tools", "post-setup", body.key], "tools-post-setup",log_msg="Failed to spawn tools post-setup", prefix="Failed to run post-setup")result["key"] = body.keyreturn resultdiff --git a/tests/hermes_cli/test_web_toolset_profile_scope.py b/tests/hermes_cli/test_web_toolset_profile_scope.pynew file mode 100644index 00000000000..1f1cfe200b4--- /dev/null+++ b/tests/hermes_cli/test_web_toolset_profile_scope.py@@ -0,0 +1,95 @@+"""The dashboard's toolset routes validate the toolset name against the TARGET profile's toolsets, the same+list ``GET /api/tools/toolsets?profile=`` shows: a plugin toolset installed only in a secondary profile+(spotify, homeassistant, ...) is configurable there and still unknown for the dashboard's own profile.+Real plugin directories, real loader, real config files under a temp home.+"""++from __future__ import annotations++import hermes_yaml as yaml+import pytest++pytest.importorskip("fastapi")+from starlette.testclient import TestClient++TOOLSET_PLUGIN = '''+def register(ctx):+ ctx.register_tool(+ name="work_ping", toolset="worktools",+ schema={"name": "work_ping", "description": "ping", "parameters": {"type": "object", "properties": {}}},+ handler=lambda args, **kw: "pong")+'''+++def homes(tmp_path, monkeypatch):+ from hermes_cli import plugins as plugins_mod++ root = tmp_path / "hermes_home"+ work = root / "profiles" / "work"+ plugin_dir = work / "plugins" / "worktools"+ plugin_dir.mkdir(parents=True)+ (plugin_dir / "plugin.yaml").write_text(yaml.safe_dump({"name": "worktools", "version": "1.0"}), encoding="utf-8")+ (plugin_dir / "__init__.py").write_text(TOOLSET_PLUGIN, encoding="utf-8")+ (work / "config.yaml").write_text(yaml.safe_dump({"plugins": {"enabled": ["worktools"]}}), encoding="utf-8")+ (root / "config.yaml").write_text(yaml.safe_dump({"model": {"default": "gpt-5"}}), encoding="utf-8")+ empty_bundled = tmp_path / "bundled"+ empty_bundled.mkdir()+ monkeypatch.setenv("HERMES_HOME", str(root))+ monkeypatch.setattr(plugins_mod, "get_bundled_plugins_dir", lambda: empty_bundled)+ from agent import secret_scope+ from tui_gateway import launch_profile_policy as lpp+ monkeypatch.setattr(secret_scope, "_MULTIPLEX_ACTIVE", False)+ monkeypatch.setattr(lpp, "_snapshot", None)+ plugins_mod._reset_plugin_managers_for_tests()+ yield root, work+ plugins_mod._reset_plugin_managers_for_tests()+++def client(homes):+ from hermes_cli.web_server import _SESSION_HEADER_NAME, _SESSION_TOKEN, app+ c = TestClient(app)+ c.headers[_SESSION_HEADER_NAME] = _SESSION_TOKEN+ return c+++def _config(home) -> str:+ return (home / "config.yaml").read_text(encoding="utf-8")+++def test_a_toolset_installed_only_in_the_target_profile_can_be_enabled_there(client, homes):+ root, work = homes+ listed = client.get("/api/tools/toolsets?profile=work").json()+ assert "worktools" in {row["name"] for row in listed}++ resp = client.put("/api/tools/toolsets/worktools?profile=work", json={"enabled": True})+ assert resp.status_code == 200, resp.text+ cli_toolsets = (yaml.safe_load(_config(work)).get("platform_toolsets") or {}).get("cli") or []+ assert "worktools" in cli_toolsets++ # The dashboard's own profile does not have the plugin: still unknown there, config untouched.+ before = _config(root)+ resp = client.put("/api/tools/toolsets/worktools", json={"enabled": True})+ assert resp.status_code == 400+ assert "Unknown toolset" in resp.text+ assert _config(root) == before++[email protected](("method", "path", "body", "scoped_answer"), [+ ("get", "/api/tools/toolsets/worktools/config", None, None),+ ("put", "/api/tools/toolsets/worktools/env", {"env": {}}, None),+ ("put", "/api/tools/toolsets/worktools/provider", {"provider": "x"}, "no configurable category"),+ ("post", "/api/tools/toolsets/worktools/post-setup", {"key": "nope"}, "Unknown post-setup key"),+])+def test_every_toolset_route_resolves_the_name_in_the_target_profile(client, method, path, body, scoped_answer):+ kwargs = {"json": body} if body is not None else {}+ resp = client.request(method.upper(), f"{path}?profile=work", **kwargs)+ assert "Unknown toolset" not in resp.text+ if scoped_answer is None:+ assert resp.status_code == 200, resp.text+ else:+ assert resp.status_code == 400 and scoped_answer in resp.text++ resp = client.request(method.upper(), path, **kwargs)+ assert resp.status_code == 400 and "Unknown toolset" in resp.text