The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(profiles): side-thread asyncio.run workers keep the caller's profile scope
teknium13baf4c2cb3ac
diff --git a/agent/context_references.py b/agent/context_references.pyindex a23504c8c06..7c7f3160629 100644--- a/agent/context_references.py+++ b/agent/context_references.py@@ -178,8 +178,11 @@ def preprocess_context_references(except RuntimeError:return asyncio.run(coro)import concurrent.futures+ import contextvars+ # The side thread starts with an empty Context: without the caller's copy the served profile's+ # HERMES_HOME override is lost and the credential-path guard checks the launch profile's .env.with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:- return pool.submit(asyncio.run, coro).result()+ return pool.submit(contextvars.copy_context().run, asyncio.run, coro).result()async def preprocess_context_references_async(diff --git a/tests/agent/test_context_references.py b/tests/agent/test_context_references.pyindex dfd08fd2570..7c005eaf26a 100644--- a/tests/agent/test_context_references.py+++ b/tests/agent/test_context_references.py@@ -389,3 +389,31 @@ def test_format_reference_value_round_trips_through_the_parser(value):assert match is not Noneassert match.group("value").strip("`\"'") == value+++async def test_side_thread_expansion_guards_the_served_profile_home(tmp_path: Path, monkeypatch):+ """Inside a running loop (the gateway / TUI turn) the sync wrapper hops to a side thread; that+ thread must inherit the caller's profile scope so the credential guard checks the SERVED+ profile's home, not the launch profile's (a served profile's skill-hub cache was attachable)."""+ from hermes_constants import reset_hermes_home_override, set_hermes_home_override+ from agent.context_references import preprocess_context_references++ launch_home = tmp_path / "launch"+ served_home = launch_home / "profiles" / "b"+ hub_file = served_home / "skills" / ".hub" / "injected.md"+ hub_file.parent.mkdir(parents=True)+ hub_file.write_text("HUB-CACHE-BODY\n", encoding="utf-8")+ monkeypatch.setenv("HOME", str(tmp_path))+ monkeypatch.setenv("HERMES_HOME", str(launch_home))++ token = set_hermes_home_override(served_home)+ try:+ result = preprocess_context_references(+ "read @file:profiles/b/skills/.hub/injected.md", cwd=launch_home, allowed_root=launch_home,+ context_length=100_000)+ finally:+ reset_hermes_home_override(token)++ assert "HUB-CACHE-BODY" not in result.message+ assert any("internal Hermes path" in w for w in result.warnings)diff --git a/tools/browser_cdp_tool.py b/tools/browser_cdp_tool.pyindex 0def5a16dd5..1b777cd9139 100644--- a/tools/browser_cdp_tool.py+++ b/tools/browser_cdp_tool.py@@ -96,8 +96,9 @@ def _run_async(coro):loop = Noneif loop and loop.is_running():import concurrent.futures+ import contextvarswith concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:- return pool.submit(asyncio.run, coro).result()+ return pool.submit(contextvars.copy_context().run, asyncio.run, coro).result()return asyncio.run(coro)diff --git a/tools/homeassistant_tool.py b/tools/homeassistant_tool.pyindex 28bd38c0baf..cc65677bcc4 100644--- a/tools/homeassistant_tool.py+++ b/tools/homeassistant_tool.py@@ -145,8 +145,10 @@ def _run_async(coro):loop = Noneif loop and loop.is_running(): # already inside a loop: asyncio.run() needs its own threadimport concurrent.futures+ import contextvars+ # Carry the caller's Context: the coroutine reads HASS_* via get_secret, which is profile-scoped.with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:- return pool.submit(asyncio.run, coro).result(timeout=30)+ return pool.submit(contextvars.copy_context().run, asyncio.run, coro).result(timeout=30)return asyncio.run(coro)diff --git a/tui_gateway/methods_complete.py b/tui_gateway/methods_complete.pyindex a7e5decc116..5a509c0abe9 100644--- a/tui_gateway/methods_complete.py+++ b/tui_gateway/methods_complete.py@@ -91,8 +91,9 @@ def _plugin_reference_items(pfx: str, qval: str) -> list[dict] | None:ac = asyncio.run(coro)else: # already inside a running loop: run the coroutine on a side threadimport concurrent.futures+ import contextvarswith concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:- ac = pool.submit(asyncio.run, coro).result()+ ac = pool.submit(contextvars.copy_context().run, asyncio.run, coro).result()return [{"text": f"@{pfx}:{it.text}", "display": it.display, "meta": it.meta} for it in ac]except Exception:return None