The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(web): rebuild the Exa and Parallel clients when their API key changes
John Paul Soliva707852d92f18
diff --git a/plugins/web/_common.py b/plugins/web/_common.pyindex 3576c399bcd..7bed6b41599 100644--- a/plugins/web/_common.py+++ b/plugins/web/_common.py@@ -184,15 +184,19 @@ def cached_sdk_client(slot: str, env_var: str, missing_key_error: str, feature:reset ``tools.web_tools._<vendor>_client = None`` see fresh state). Raises ValueErrorwhen the key is unset."""import tools.web_tools as _wt- cached = getattr(_wt, slot, None)- if cached is not None:- return cached+ # Resolved before the cache is consulted: the slot is one per process, but the key can change+ # under it (``/reload``, or each multiplexed profile's secret scope resolving its own key).api_key = provider_env(env_var)if not api_key:raise ValueError(missing_key_error)+ # (key, client) as one value so concurrent builds under different keys can never leave one+ # key recorded beside another key's client.+ cached = getattr(_wt, slot, None)+ if cached is not None and cached[0] == api_key:+ return cached[1]lazy_ensure(feature)client = factory(api_key)- setattr(_wt, slot, client)+ setattr(_wt, slot, (api_key, client))return clientdiff --git a/tests/tools/test_web_tools_config.py b/tests/tools/test_web_tools_config.pyindex cd7921922ec..77f2b67991d 100644--- a/tests/tools/test_web_tools_config.py+++ b/tests/tools/test_web_tools_config.py@@ -5,6 +5,7 @@ Coverage:constructor failure recovery, return value verification, edge cases._get_backend() — backend selection logic with env var combinations.plugins.web.parallel.provider._get_sync_client() — Parallel client configuration, singleton caching.+ plugins.web._common.cached_sdk_client() — Parallel/Exa clients follow the currently resolved key.check_web_api_key() — unified availability check across all web backends."""@@ -440,6 +441,76 @@ class TestParallelClientConfig:client2 = _get_parallel_client()assert client1 is client2+ def test_client_follows_the_key_reload_applies(self):+ """/reload (reload_env) fixing or removing the key reaches the next call: the client built+ with the old key is never handed out again."""+ from hermes_cli.config import get_env_path, reload_env+ from plugins.web.parallel.provider import _get_sync_client as _get_parallel_client+ with patch.dict(os.environ):+ get_env_path().write_text("PARALLEL_API_KEY=typo-key\n")+ reload_env()+ assert _get_parallel_client().api_key == "typo-key"+ get_env_path().write_text("PARALLEL_API_KEY=fixed-key\n")+ reload_env()+ assert _get_parallel_client().api_key == "fixed-key"+ get_env_path().write_text("")+ reload_env()+ with pytest.raises(ValueError, match="PARALLEL_API_KEY"):+ _get_parallel_client()+++class TestExaClientConfig:+ """Exa shares the key-checked client cache with Parallel."""++ def setup_method(self):+ import tools.web_tools+ tools.web_tools._exa_client = None+ os.environ.pop("EXA_API_KEY", None)+ fake_exa = types.ModuleType("exa_py")++ class Exa:+ def __init__(self, api_key):+ self.api_key = api_key+ self.headers = {}++ def search(self, query, num_results, contents):+ hit = types.SimpleNamespace(url="https://example.com", title="t", highlights=[self.api_key])+ return types.SimpleNamespace(results=[hit])++ fake_exa.Exa = Exa+ sys.modules["exa_py"] = fake_exa+ self._lazy_gate_patch = patch("tools.lazy_deps.ensure", lambda *args, **kwargs: None)+ self._lazy_gate_patch.start()++ def teardown_method(self):+ import tools.web_tools+ tools.web_tools._exa_client = None+ os.environ.pop("EXA_API_KEY", None)+ sys.modules.pop("exa_py", None)+ self._lazy_gate_patch.stop()++ def test_multiplexed_profiles_search_with_their_own_key(self, tmp_path, monkeypatch):+ """One process serving two profiles: each turn's search goes out with its own profile's key."""+ from agent import secret_scope+ from plugins.web.exa.provider import ExaWebSearchProvider+ homes = {}+ for name in ("a", "b"):+ homes[name] = tmp_path / name+ homes[name].mkdir()+ (homes[name] / ".env").write_text(f"EXA_API_KEY=key-{name}\n")+ monkeypatch.setattr(secret_scope, "_MULTIPLEX_ACTIVE", True)++ def sent_with(name):+ token = secret_scope.set_secret_scope(secret_scope.build_profile_secret_scope(homes[name]))+ try:+ result = ExaWebSearchProvider().search("q")+ finally:+ secret_scope.reset_secret_scope(token)+ assert result["success"], result+ return result["data"]["web"][0]["description"]++ assert [sent_with("a"), sent_with("b"), sent_with("a")] == ["key-a", "key-b", "key-a"]+class TestWebSearchSchema:"""Test suite for web_search tool schema and handler wiring."""