Skip to content

The agent that grows with you

fix(tui_gateway): tools.list/tools.show read back the session's effective toolsets (#117977, salvage #118020)

kokhlo0543bfd6fd2b
diff --git a/tests/tui_gateway/test_tools_readback_scope.py b/tests/tui_gateway/test_tools_readback_scope.py
new file mode 100644
index 00000000000..7dad6206788
--- /dev/null
+++ b/tests/tui_gateway/test_tools_readback_scope.py
@@ -0,0 +1,81 @@
+"""tools.list / tools.show read back the SESSION's effective toolsets (#117977).
+
+``profiles.configure`` pins ``platform_toolsets.cli`` in the profile's own config.yaml — the key the
+agent build reads through ``_load_enabled_toolsets`` under the session's profile scope. The read-back
+RPCs consulted only a BUILT agent; a session whose agent had not been built yet (``session.create``
+with no prompt, exactly the editor's flow) read back as "everything enabled", and a session-less call
+resolved against the launch home. Invariant: for a session_id, both RPCs answer with the toolsets the
+session's own profile would build with; the launch profile's session still sees its own pin (A→B→A).
+"""
+
+from __future__ import annotations
+
+import hermes_yaml as yaml
+import pytest
+
+import tui_gateway.server as server
+from tui_gateway.methods_profiles import _save_toolset_pin
+
+LAUNCH_PIN = ["web", "browser", "terminal"]
+WORKER_PIN = ["file", "clarify"]
+
+
+def _pin(home, names):
+ """Write the pin the way ``profiles.configure`` does (its writer, into that home's config.yaml)."""
+ home.mkdir(parents=True, exist_ok=True)
+ _save_toolset_pin({}, names, save_config=lambda cfg: (home / "config.yaml").write_text(
+ yaml.safe_dump(cfg), encoding="utf-8"))
+
+
+def two_homes(tmp_path, monkeypatch):
+ """Launch home pinned to LAUNCH_PIN, secondary ``profiles/work`` pinned to WORKER_PIN; one
+ multiplexing backend serves an agent-less session per home."""
+ root = tmp_path / "hermes_home"
+ worker = root / "profiles" / "work"
+ _pin(root, LAUNCH_PIN)
+ _pin(worker, WORKER_PIN)
+ monkeypatch.setenv("HERMES_HOME", str(root))
+ monkeypatch.delenv("HERMES_TUI_TOOLSETS", raising=False)
+ monkeypatch.setattr(server, "_hermes_home", root)
+ from agent import secret_scope
+ monkeypatch.setattr(secret_scope, "_MULTIPLEX_ACTIVE", True)
+ server._cfg_cache = server._cfg_mtime = server._cfg_path = None
+ sessions = {
+ "launch": {"agent": None, "profile_home": None, "cwd": str(tmp_path), "source": "tui"},
+ "work": {"agent": None, "profile_home": str(worker), "cwd": str(tmp_path), "source": "tui"},
+ }
+ monkeypatch.setattr(server, "_sessions", sessions)
+ return sessions
+
+
+def _enabled(sid: str) -> set[str]:
+ resp = server._methods["tools.list"]("rid", {"session_id": sid})
+ assert "error" not in resp, resp
+ return {row["name"] for row in resp["result"]["toolsets"] if row["enabled"]}
+
+
+def _sections(sid: str) -> set[str]:
+ resp = server._methods["tools.show"]("rid", {"session_id": sid})
+ assert "error" not in resp, resp
+ return {section["name"] for section in resp["result"]["sections"]}
+
+
+def test_tools_list_reads_back_the_sessions_own_pin_a_b_a(two_homes):
+ from hermes_constants import get_hermes_home_override
+
+ work_first = _enabled("work")
+ launch = _enabled("launch")
+ work_again = _enabled("work")
+ assert set(WORKER_PIN) <= work_first == work_again, sorted(work_first)
+ assert work_first.isdisjoint({"web", "browser", "terminal"}), sorted(work_first)
+ assert set(LAUNCH_PIN) <= launch and "file" not in launch, sorted(launch)
+ assert get_hermes_home_override() is None # scope released after each answer
+
+
+def test_tools_show_sections_follow_the_sessions_pin(two_homes):
+ """Sections are per resolved TOOL, so credential-gated toolsets (web, browser) may be absent
+ from both; ``terminal`` vs ``file`` is the pair that tells the two pins apart."""
+ work, launch = _sections("work"), _sections("launch")
+ assert "file" in work and "terminal" not in work, sorted(work)
+ assert "terminal" in launch and "file" not in launch, sorted(launch)
diff --git a/tui_gateway/methods_tools.py b/tui_gateway/methods_tools.py
index 04de37b13aa..7238c7f0904 100644
--- a/tui_gateway/methods_tools.py
+++ b/tui_gateway/methods_tools.py
@@ -215,10 +215,24 @@ def _joined_output(r) -> str:
return "\n".join(p for p in (r.stdout or "", r.stderr or "") if p).strip()
+def _session_toolsets(session) -> tuple:
+ """``(enabled, disabled)`` a session's read-back RPCs must reflect: the live agent's sets once built,
+ else what that session's profile would build with — ``_load_enabled_toolsets`` under the session's
+ profile scope, the same read ``_build_agent``/``_refresh_live_sessions`` make and the key
+ ``profiles.configure`` pins. A session whose agent is not built yet (``session.create`` with no
+ prompt) otherwise read back as "everything enabled" (#117977). ``None`` = all toolsets.
+ No session → the launch profile's config."""
+ agent = session.get("agent") if session else None
+ if agent is not None:
+ return getattr(agent, "enabled_toolsets", None), getattr(agent, "disabled_toolsets", None)
+ with _session_profile_runtime_scope(session or {}):
+ return _load_enabled_toolsets(_resolve_agent_platform(_session_source(session))), _load_disabled_toolsets()
+
+
def _toolset_rows(params: dict, *, with_tools: bool) -> list[dict]:
toolsets = _tools_mod("toolsets")
session = _sessions.get(params.get("session_id", ""))
- enabled = set((getattr(session["agent"], "enabled_toolsets", []) if session else _load_enabled_toolsets()) or [])
+ enabled = set(_session_toolsets(session)[0] or [])
items = []
for name in sorted(toolsets.get_all_toolsets().keys()):
if info := toolsets.get_toolset_info(name):
@@ -1177,8 +1191,7 @@ def _(rid, params: dict) -> dict:
def _(rid, params: dict) -> dict:
mt = _tools_mod("model_tools")
session = _sessions.get(params.get("session_id", ""))
- enabled = getattr(session["agent"], "enabled_toolsets", None) if session else _load_enabled_toolsets()
- disabled = getattr(session["agent"], "disabled_toolsets", None) if session else _load_disabled_toolsets()
+ enabled, disabled = _session_toolsets(session)
# Pre-assembly list: /tools must also show tools deferred behind the tool_search bridge (as the CLI).
tools = mt.get_tool_definitions(enabled_toolsets=enabled, disabled_toolsets=disabled, quiet_mode=True,
skip_tool_search_assembly=True)