The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui_gateway): plugin slash commands run under the session's HERMES_SESSION_* binding
teknium135fdb4608aa8
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.pyindex 27d0d7754ef..a061a2f0edd 100644--- a/tests/tui_gateway/test_tui_gateway_server.py+++ b/tests/tui_gateway/test_tui_gateway_server.py@@ -11918,6 +11918,30 @@ def test_commands_catalog_includes_plugin_commands(monkeypatch):assert "/lcm" in dict(plugin_cat["pairs"])+def test_plugin_slash_command_runs_under_the_session_env(monkeypatch):+ # TUI/Desktop sibling of #108698: command.dispatch and slash.exec ran plugin handlers on the RPC+ # thread with no HERMES_SESSION_* binding, so a handler reading get_session_env() saw "" (or the+ # launch process's inherited values) instead of the session it was invoked from.+ from gateway.session_context import get_session_env++ seen = {}++ def handler(arg):+ seen["key"] = get_session_env("HERMES_SESSION_KEY")+ return f"ok:{arg}"++ monkeypatch.setattr("hermes_cli.plugins.get_plugin_command_handler",+ lambda name: handler if name == "whoami" else None)+ monkeypatch.setattr(server, "_sessions", {"sid-p": {"session_key": "agent:tui:key-p", "cwd": ""}})++ res = server._methods["command.dispatch"]("d", {"name": "whoami", "arg": "x", "session_id": "sid-p"})++ assert res["result"] == {"type": "plugin", "output": "ok:x"}+ assert seen["key"] == "agent:tui:key-p"+ # Nothing leaks past the RPC.+ assert get_session_env("HERMES_SESSION_KEY") in ("", None)++def test_session_status_reads_live_gateway_agent(monkeypatch):agent = types.SimpleNamespace(model="live-model",diff --git a/tui_gateway/methods_tools.py b/tui_gateway/methods_tools.pyindex ae7bfc4e569..69262407979 100644--- a/tui_gateway/methods_tools.py+++ b/tui_gateway/methods_tools.py@@ -527,8 +527,19 @@ def _plugin_command_handler(name: str):return None-def _run_plugin_command(handler, arg: str) -> str:- return str(_tools_mod("hermes_cli.plugins").resolve_plugin_command_result(handler(arg)) or "")+def _run_plugin_command(handler, arg: str, session=None) -> str:+ """Run a plugin slash-command handler under the session's ``HERMES_SESSION_*`` binding.++ Plugin handlers read ``get_session_env()`` for the chat/session they serve; these RPCs run on+ the socket/worker thread where nothing upstream binds it (only the turn path does), so a handler+ saw ``""`` or the launch process's inherited values. Same class as the messaging gateway's+ #108698; ``_set_session_context`` is the turn path's own seam."""+ plugins = _tools_mod("hermes_cli.plugins")+ tokens = _set_session_context(session.get("session_key", "") or "", cwd=str(session.get("cwd") or "")) if session else []+ try:+ return str(plugins.resolve_plugin_command_result(handler(arg)) or "")+ finally:+ _clear_session_context(tokens)@contextlib.contextmanager@@ -569,7 +580,7 @@ def _is_profile_skill_command(session: dict, base: str) -> bool:def _dispatch_plugin(rid, params, session, name, arg):if handler := _plugin_command_handler(name):with contextlib.suppress(Exception):- return _ok(rid, {"type": "plugin", "output": _run_plugin_command(handler, arg)})+ return _ok(rid, {"type": "plugin", "output": _run_plugin_command(handler, arg, session)})return None@@ -905,7 +916,7 @@ def _(rid, params: dict) -> dict:return _err(rid, 4018, f"skill command: use command.dispatch for /{base}")if plugin_handler := _plugin_command_handler(base) if base else None:try:- return _ok(rid, {"output": _run_plugin_command(plugin_handler, arg) or "(no output)"})+ return _ok(rid, {"output": _run_plugin_command(plugin_handler, arg, session) or "(no output)"})except Exception as e:return _ok(rid, {"output": f"Plugin command error: {e}"})worker = session.get("slash_worker")