Skip to content

The agent that grows with you

fix(dashboard): validate a terminal backend pick inside the target profile's scope

Teknium94e3a916f2fe
diff --git a/hermes_cli/web_routers/tools.py b/hermes_cli/web_routers/tools.py
index 6c73851cc8e..cb01e4c2010 100644
--- a/hermes_cli/web_routers/tools.py
+++ b/hermes_cli/web_routers/tools.py
@@ -673,16 +673,18 @@ async def get_terminal_backends(profile: Optional[str] = None):
async def select_terminal_backend(
body: TerminalBackendSelect, profile: Optional[str] = None):
"""Persist ``terminal.backend``. A backend that still needs setup is
- allowed — the picker shows guidance instead of blocking, like the CLI."""
+ allowed — the picker shows guidance instead of blocking, like the CLI.
+ Validated inside the target profile's scope, like the GET: plugin backends
+ are registered per profile."""
backend = (body.backend or "").strip().lower()
- valid_names = {row["name"] for row in _terminal_backend_rows()}
- if backend not in valid_names:
- raise _bad_request(
- f"Unknown terminal backend: {body.backend!r}. "
- f"Use one of: {', '.join(sorted(valid_names))}")
def _run():
with config_write_scope(body.profile or profile):
+ valid_names = {row["name"] for row in _terminal_backend_rows()}
+ if backend not in valid_names:
+ raise _bad_request(
+ f"Unknown terminal backend: {body.backend!r}. "
+ f"Use one of: {', '.join(sorted(valid_names))}")
config = load_config()
_dict_section(config, "terminal")["backend"] = backend
save_config(config)
diff --git a/tests/hermes_cli/test_web_terminal_backend_profile_scope.py b/tests/hermes_cli/test_web_terminal_backend_profile_scope.py
new file mode 100644
index 00000000000..92d84e9e59b
--- /dev/null
+++ b/tests/hermes_cli/test_web_terminal_backend_profile_scope.py
@@ -0,0 +1,83 @@
+"""``PUT /api/tools/terminal/backend`` validates the backend against the TARGET profile's plugin
+backends, the same list ``GET /api/tools/terminal/backends?profile=`` shows: a backend plugin installed
+only in a secondary profile (the ``singularity`` plugin after the backend left core) is selectable
+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
+
+BACKEND_PLUGIN = '''
+from agent.terminal_env_provider import TerminalEnvironmentProvider
+
+class Box(TerminalEnvironmentProvider):
+ @property
+ def name(self):
+ return "workbox"
+ def is_available(self):
+ return True
+ def create_environment(self, **kw):
+ raise NotImplementedError
+
+def register(ctx):
+ ctx.register_terminal_environment_provider(Box())
+'''
+
+
+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" / "workbox"
+ plugin_dir.mkdir(parents=True)
+ (plugin_dir / "plugin.yaml").write_text(yaml.safe_dump({"name": "workbox", "version": "1.0"}), encoding="utf-8")
+ (plugin_dir / "__init__.py").write_text(BACKEND_PLUGIN, encoding="utf-8")
+ (work / "config.yaml").write_text(yaml.safe_dump({"plugins": {"enabled": ["workbox"]}}), encoding="utf-8")
+ (root / "config.yaml").write_text(yaml.safe_dump({"terminal": {"backend": "local"}}), 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 _backend(home):
+ return (yaml.safe_load((home / "config.yaml").read_text(encoding="utf-8")).get("terminal") or {}).get("backend")
+
+
+def test_a_backend_installed_only_in_the_target_profile_is_selectable_there(client, homes):
+ root, work = homes
+ listed = client.get("/api/tools/terminal/backends?profile=work").json()
+ assert "workbox" in {row["name"] for row in listed["backends"]}
+
+ resp = client.put("/api/tools/terminal/backend?profile=work", json={"backend": "workbox"})
+ assert resp.status_code == 200, resp.text
+ assert _backend(work) == "workbox"
+
+ # The dashboard's own profile does not have the plugin: still unknown there, config untouched.
+ resp = client.put("/api/tools/terminal/backend", json={"backend": "workbox"})
+ assert resp.status_code == 400
+ assert "Unknown terminal backend" in resp.text
+ assert _backend(root) == "local"