fix: patch skills module-level caches on per-request profile switch

Per-request profile switches (process_wide=False, introduced in #1700)
update os.environ['HERMES_HOME'] but skip _set_hermes_home(), which is
responsible for monkeypatching module-level caches.

Both tools/skills_tool.py and tools/skill_manager_tool.py set
HERMES_HOME and SKILLS_DIR once at import time. When a non-default
profile is active in the WebUI, os.environ['HERMES_HOME'] is correctly
updated per-turn in the _ENV_LOCK block, but the module-level
constants still point at the root profile. All agent-side skill
operations — skills_list(), skill_view(), skill_manage() — read and
write to the wrong directory.

Add the same monkeypatching that _set_hermes_home() already performs
(profiles.py line ~620) to the per-turn env setup block in
streaming.py, covering both skills_tool and skill_manager_tool.

The WebUI display half was already fixed in #1917 via
_active_skills_dir() in routes.py. This patch fixes the agent-side
half so the running agent resolves skills from the correct profile.
This commit is contained in:
qxxaa authored and GitHub committed 2026-05-10 09:02:49 +01:00
1 parent e4a9c5b7f5
commit 7ee41c9b12
1 file changed
+17
+17
View File
@@ -2046,6 +2046,23 @@ def _run_agent_streaming(
os.environ['HERMES_SESSION_KEY'] = session_id
if _profile_home:
os.environ['HERMES_HOME'] = _profile_home
# Patch module-level caches to match the active profile.
# _set_hermes_home() does this for process-wide switches
# but per-request switches skip it (#1700).
from pathlib import Path as _P
_ph = _P(_profile_home)
try:
import tools.skills_tool as _sk
_sk.HERMES_HOME = _ph
_sk.SKILLS_DIR = _ph / 'skills'
except (ImportError, AttributeError):
pass
try:
import tools.skill_manager_tool as _sm
_sm.HERMES_HOME = _ph
_sm.SKILLS_DIR = _ph / 'skills'
except (ImportError, AttributeError):
pass
# Lock released — agent runs without holding it
# ── MCP Server Discovery (lazy import, idempotent) ──
# MUST run AFTER the HERMES_HOME mutation above — `discover_mcp_tools()`