The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): run dashboard plugin install/update in the profile's secret scope
KeyArgo7ddf71d395bb
diff --git a/hermes_cli/web_routers/dashboard_ui.py b/hermes_cli/web_routers/dashboard_ui.pyindex 62736a38f0a..2acbd76ce7e 100644--- a/hermes_cli/web_routers/dashboard_ui.py+++ b/hermes_cli/web_routers/dashboard_ui.py@@ -30,6 +30,10 @@ _get_dashboard_plugins = late("_get_dashboard_plugins")_require_token = late("_require_token")load_config = late("load_config", "hermes_cli.config")save_config = late("save_config", "hermes_cli.config")+# Home + secret scope for one request. NOT ``_profile_scope``: the bodies below clone/pull over the+# network and hold the scope for their whole duration, and ``_profile_scope`` also holds the+# process-global skills lock.+_config_profile_scope = late("_config_profile_scope", "hermes_cli.web_server_profiles")_CONFIG_MUTATION_LOCK = LateState("_CONFIG_MUTATION_LOCK")@@ -194,6 +198,23 @@ async def get_plugins_catalog(request: Request):raise HTTPException(status_code=500, detail="Failed to build plugins catalog.") from exc+async def _plugin_mutation(fn: Callable[[], dict]) -> dict:+ """Run a dashboard plugin mutation inside the dashboard profile's home + secret scope, off the+ event loop.++ Scope: the git-credential path reaches ``get_secret("GITHUB_TOKEN")``, which fails closed once+ the process serves more than its launch profile — an unhandled ``UnscopedSecretError`` was the+ bare 500 on install/update (#115256). Off-loop: the bodies clone/pull and rescan the plugins+ directory. ``_config_profile_scope`` not ``_profile_scope``: the latter holds the process-wide+ skills lock for the whole (network-bound) body.+ """+ def _run():+ with _config_profile_scope(None):+ return fn()++ return await asyncio.to_thread(_run)++@router.post("/api/dashboard/agent-plugins/install")async def post_agent_plugin_install(request: Request, body: _AgentPluginInstallBody):_require_token(request)@@ -203,9 +224,11 @@ async def post_agent_plugin_install(request: Request, body: _AgentPluginInstallBidentifier = body.identifier.strip()if not identifier and not catalog_name:raise HTTPException(status_code=400, detail="Provide an identifier or a catalog_name.")- result = dashboard_install_plugin(- identifier, force=body.force, enable=body.enable, catalog_name=catalog_name or None, ref=body.ref)- result = _plugin_action(result, "Install failed.", rescan=True)+ result = await _plugin_mutation(lambda: _plugin_action(+ dashboard_install_plugin(+ identifier, force=body.force, enable=body.enable, catalog_name=catalog_name or None,+ ref=body.ref),+ "Install failed.", rescan=True))# Strip internal paths from the responseresult.pop("after_install_path", None)return result@@ -219,35 +242,36 @@ def _validate_plugin_name(name: str) -> str:return name-def _named_plugin_action(request: Request, name: str, action: Callable[[str], dict], fallback_error: str, *, rescan: bool) -> dict:+async def _named_plugin_action(request: Request, name: str, action: Callable[[str], dict], fallback_error: str, *, rescan: bool) -> dict:_require_token(request)- return _plugin_action(action(_validate_plugin_name(name)), fallback_error, rescan=rescan)+ return await _plugin_mutation(lambda: _plugin_action(+ action(_validate_plugin_name(name)), fallback_error, rescan=rescan))@router.post("/api/dashboard/agent-plugins/{name:path}/enable")async def post_agent_plugin_enable(request: Request, name: str):from hermes_cli.plugins_cmd import dashboard_set_agent_plugin_enabled- return _named_plugin_action(request, name, lambda n: dashboard_set_agent_plugin_enabled(n, enabled=True),- "Enable failed.", rescan=False)+ return await _named_plugin_action(request, name, lambda n: dashboard_set_agent_plugin_enabled(n, enabled=True),+ "Enable failed.", rescan=False)@router.post("/api/dashboard/agent-plugins/{name:path}/disable")async def post_agent_plugin_disable(request: Request, name: str):from hermes_cli.plugins_cmd import dashboard_set_agent_plugin_enabled- return _named_plugin_action(request, name, lambda n: dashboard_set_agent_plugin_enabled(n, enabled=False),- "Disable failed.", rescan=False)+ return await _named_plugin_action(request, name, lambda n: dashboard_set_agent_plugin_enabled(n, enabled=False),+ "Disable failed.", rescan=False)@router.post("/api/dashboard/agent-plugins/{name:path}/update")async def post_agent_plugin_update(request: Request, name: str):from hermes_cli.plugins_cmd import dashboard_update_user_plugin- return _named_plugin_action(request, name, dashboard_update_user_plugin, "Update failed.", rescan=True)+ return await _named_plugin_action(request, name, dashboard_update_user_plugin, "Update failed.", rescan=True)@router.delete("/api/dashboard/agent-plugins/{name:path}")async def delete_agent_plugin(request: Request, name: str):from hermes_cli.plugins_cmd import dashboard_remove_user_plugin- return _named_plugin_action(request, name, dashboard_remove_user_plugin, "Remove failed.", rescan=True)+ return await _named_plugin_action(request, name, dashboard_remove_user_plugin, "Remove failed.", rescan=True)@router.put("/api/dashboard/plugin-providers")diff --git a/tests/hermes_cli/test_dashboard_plugin_routes_secret_scope.py b/tests/hermes_cli/test_dashboard_plugin_routes_secret_scope.pynew file mode 100644index 00000000000..5dcf59e201f--- /dev/null+++ b/tests/hermes_cli/test_dashboard_plugin_routes_secret_scope.py@@ -0,0 +1,110 @@+"""Dashboard agent-plugin mutations run in the request's profile scope, off the event loop.++Regression for #115256: install/update reached ``git_credentials._github_token`` →+``get_secret("GITHUB_TOKEN")`` with no profile secret scope bound. Once the process serves more+than its launch profile (any cross-profile request flips multi-profile hosting on) that read fails+closed with ``UnscopedSecretError``, so both routes answered a bare 500 for every repo-backed+plugin; both also cloned/pulled and rescanned on the event loop.++Driven through the real route handlers over the real app, with the real scope helpers and the real+``get_secret``; the plugin action itself is stubbed so nothing touches the network.+"""++from __future__ import annotations++import asyncio++import pytest+++def client(monkeypatch, _isolate_hermes_home):+ try:+ from starlette.testclient import TestClient+ except ImportError:+ pytest.skip("fastapi/starlette not installed")++ import hermes_state+ from hermes_cli.web_server import app, _SESSION_HEADER_NAME, _SESSION_TOKEN+ from hermes_constants import get_hermes_home++ monkeypatch.setattr(hermes_state, "DEFAULT_DB_PATH", get_hermes_home() / "state.db")+ # The launch profile's own credential source: what a scoped read must return.+ (get_hermes_home() / ".env").write_text("GITHUB_TOKEN=ghp_launch_token\n", encoding="utf-8")+ return TestClient(app, raise_server_exceptions=False,+ headers={_SESSION_HEADER_NAME: _SESSION_TOKEN})+++def multiplexed():+ """The dashboard hosting a profile other than its launch one (the reported trigger)."""+ from agent.secret_scope import set_multiplex_active++ set_multiplex_active(True)+ try:+ yield+ finally:+ set_multiplex_active(False)+++def _stub_action(monkeypatch, name: str, seen: dict, result: dict | None = None):+ """Stand in for the plugin action: read the credential like the git path does, note the thread."""+ import hermes_cli.plugins_cmd as plugins_cmd+ from agent.secret_scope import get_secret++ def _stub(*_args, **_kwargs):+ try:+ asyncio.get_running_loop()+ seen["on_event_loop"] = True+ except RuntimeError:+ seen["on_event_loop"] = False+ seen["token"] = get_secret("GITHUB_TOKEN")+ return result if result is not None else {"ok": True, "name": name}++ monkeypatch.setattr(plugins_cmd, name, _stub)+++def test_install_route_reads_the_git_credential_in_profile_scope(client, multiplexed, monkeypatch):+ seen: dict = {}+ _stub_action(monkeypatch, "dashboard_install_plugin", seen)++ resp = client.post("/api/dashboard/agent-plugins/install", json={"identifier": "owner/repo"})++ assert resp.status_code == 200, resp.text+ assert seen["token"] == "ghp_launch_token"+++def test_update_route_reads_the_git_credential_in_profile_scope(client, multiplexed, monkeypatch):+ seen: dict = {}+ _stub_action(monkeypatch, "dashboard_update_user_plugin", seen)++ resp = client.post("/api/dashboard/agent-plugins/probe/update")++ assert resp.status_code == 200, resp.text+ assert seen["token"] == "ghp_launch_token"+++def test_plugin_mutation_keeps_blocking_work_off_the_event_loop(client, multiplexed, monkeypatch):+ seen: dict = {}+ _stub_action(monkeypatch, "dashboard_install_plugin", seen)++ assert client.post(+ "/api/dashboard/agent-plugins/install", json={"identifier": "owner/repo"}+ ).status_code == 200+ assert seen["on_event_loop"] is False+++def test_failure_and_request_shape_errors_are_unchanged(client, multiplexed, monkeypatch):+ """Backward compat: a failed action is still a 400 carrying its own message, and the+ identifier/catalog_name check still precedes the scoped body."""+ seen: dict = {}+ _stub_action(monkeypatch, "dashboard_install_plugin", seen,+ result={"ok": False, "error": "Install failed: no such plugin."})++ failed = client.post("/api/dashboard/agent-plugins/install", json={"identifier": "owner/repo"})+ assert failed.status_code == 400+ assert failed.json()["detail"] == "Install failed: no such plugin."++ malformed = client.post("/api/dashboard/agent-plugins/install", json={"identifier": ""})+ assert malformed.status_code == 400+ assert "catalog_name" in malformed.json()["detail"]