The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(dashboard): resolve a channel's platform inside the target profile's scope
Teknium7b9cb2e39c9b
diff --git a/hermes_cli/web_routers/messaging.py b/hermes_cli/web_routers/messaging.pyindex d29a32a5cf8..ff286d97015 100644--- a/hermes_cli/web_routers/messaging.py+++ b/hermes_cli/web_routers/messaging.py@@ -104,10 +104,10 @@ def _env_value_rules(platform_id: str) -> dict[str, tuple[Any, str]]:"""``{env key: (accepts(value), 400 detail)}`` declared on the platform plugin's PlatformEntry(``env_value_rules``; e.g. Telegram/Slack reject credentials clearly in the wrong field).- Resolve this in the request's own scope, right after :func:`_require_platform`: the catalog build- there has just materialized every plugin entry, so the lookup imports nothing, whereas inside- ``_profile_scope`` the registry's profile overlay may not hold the entry and every rule would- silently stop applying. Without rules on the entry (the dashboard's own profile disabled the+ Resolve this in the target profile's scope, right after :func:`_require_platform`: the catalog build+ there has just materialized that profile's plugin entries, so the lookup imports nothing, whereas in+ a scope whose catalog was never built the registry's overlay may not hold the entry and every rule+ would silently stop applying. Without rules on the entry (the dashboard's own profile disabled theplugin, a user plugin overrides it) a bundled platform's ``dashboard_card.py`` answers, read fromits file: the rules guard the edited profile's values whatever the dashboard's profile loads."""from gateway.platform_env import bundled_sibling@@ -974,10 +974,15 @@ def _multiplex_port_binding_conflict(platform_id: str, requested_profile: Option@router.put("/api/messaging/platforms/{platform_id}")async def update_messaging_platform(platform_id: str, body: MessagingPlatformUpdate, profile: Optional[str] = None):- entry = _require_platform(platform_id)- env_value_rules = _env_value_rules(platform_id)-target_profile = body.profile or profile++ def _resolve():+ # Plugin platforms (and their value rules) are registered per profile: resolve them in the+ # target's scope, the catalog GET /api/messaging/platforms?profile= lists.+ with _profile_scope(target_profile):+ return _require_platform(platform_id), _env_value_rules(platform_id)++ entry, env_value_rules = await asyncio.to_thread(_resolve)if body.enabled:conflict = await asyncio.to_thread(_multiplex_port_binding_conflict, platform_id, target_profile)if conflict:@@ -1175,11 +1180,10 @@ def _messaging_platform_posture(@router.post("/api/messaging/platforms/{platform_id}/test")async def test_messaging_platform(platform_id: str, profile: Optional[str] = None):- entry = _require_platform(platform_id)- authz_env = _platform_authz_env_names(platform_id)-def _run():with _profile_scope(profile) as scoped_dir:+ entry = _require_platform(platform_id)+ authz_env = _platform_authz_env_names(platform_id)payload = _platform_payloads(scoped_dir, [entry])[0]posture = Noneif payload["state"] == "connected":@@ -1187,9 +1191,9 @@ async def test_messaging_platform(platform_id: str, profile: Optional[str] = Non# must resolve the ACTIVE profile's env/config/pairing store, not the# dashboard process's root home.posture = _messaging_platform_posture(platform_id, entry, scoped_dir is not None, authz_env)- return payload, posture+ return entry, payload, posture- payload, posture = await asyncio.to_thread(_run)+ entry, payload, posture = await asyncio.to_thread(_run)def result(ok: bool, message: str) -> dict[str, Any]:return {"ok": ok, "state": payload["state"], "message": message}diff --git a/tests/hermes_cli/test_web_messaging_platform_profile_scope.py b/tests/hermes_cli/test_web_messaging_platform_profile_scope.pynew file mode 100644index 00000000000..4e969163b80--- /dev/null+++ b/tests/hermes_cli/test_web_messaging_platform_profile_scope.py@@ -0,0 +1,77 @@+"""The dashboard's channel edit routes resolve the platform against the TARGET profile's platforms, the same+catalog ``GET /api/messaging/platforms?profile=`` shows: a platform plugin installed only in a secondary+profile is editable there (with its own value rules) and still unknown for the dashboard's own profile.+Real plugin directories, real loader, real config/.env files under a temp home.+"""++from __future__ import annotations++import hermes_yaml as yaml+import pytest++pytest.importorskip("fastapi")+from starlette.testclient import TestClient++PLATFORM_PLUGIN = '''+def register(ctx):+ ctx.register_platform(+ name="workchat", label="Work Chat", adapter_factory=lambda cfg: None, check_fn=lambda: True,+ required_env=["WORKCHAT_TOKEN"],+ env_value_rules={"WORKCHAT_TOKEN": (lambda v: v.startswith("wc-"), "WORKCHAT_TOKEN starts with wc-")})+'''+++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" / "workchat"+ plugin_dir.mkdir(parents=True)+ (plugin_dir / "plugin.yaml").write_text(+ yaml.safe_dump({"name": "workchat", "kind": "platform", "version": "1.0"}), encoding="utf-8")+ (plugin_dir / "__init__.py").write_text(PLATFORM_PLUGIN, encoding="utf-8")+ (work / "config.yaml").write_text(yaml.safe_dump({"plugins": {"enabled": ["workchat"]}}), encoding="utf-8")+ (root / "config.yaml").write_text(yaml.safe_dump({"model": {"default": "gpt-5"}}), encoding="utf-8")+ for home in (root, work):+ (home / ".env").write_text("", 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 test_a_platform_installed_only_in_the_target_profile_is_editable_there(client, homes):+ root, work = homes+ listed = client.get("/api/messaging/platforms?profile=work").json()+ assert "workchat" in {p["id"] for p in listed["platforms"]}++ # The target profile's plugin rules still guard its values.+ resp = client.put("/api/messaging/platforms/workchat?profile=work", json={"env": {"WORKCHAT_TOKEN": "nope"}})+ assert resp.status_code == 400 and "starts with wc-" in resp.text+ resp = client.put("/api/messaging/platforms/workchat?profile=work", json={"env": {"WORKCHAT_TOKEN": "wc-1"}})+ assert resp.status_code == 200, resp.text+ assert "WORKCHAT_TOKEN=wc-1" in (work / ".env").read_text(encoding="utf-8")+ assert client.post("/api/messaging/platforms/workchat/test?profile=work").status_code == 200++ # The dashboard's own profile does not have the plugin: still unknown there, .env untouched.+ resp = client.put("/api/messaging/platforms/workchat", json={"env": {"WORKCHAT_TOKEN": "wc-1"}})+ assert resp.status_code == 404 and "Unknown messaging platform" in resp.text+ assert client.post("/api/messaging/platforms/workchat/test").status_code == 404+ assert "WORKCHAT_TOKEN" not in (root / ".env").read_text(encoding="utf-8")