Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(hermes): isolate cached user identities across reloads (#5261)
Hao Zhe172c1050716f
diff --git a/examples/hermes-plugin/__init__.py b/examples/hermes-plugin/__init__.pyindex 59f62a683..e6666048f 100644--- a/examples/hermes-plugin/__init__.py+++ b/examples/hermes-plugin/__init__.py@@ -250,6 +250,8 @@ class _VikingClient:self._account = account or get_secret("OPENVIKING_ACCOUNT", "") or "default"self._user = user or get_secret("OPENVIKING_USER", "") or "default"self._agent = agent if agent is not None else (get_secret("OPENVIKING_AGENT", "") or _DEFAULT_AGENT)+ # Every client owns its resolved identity, including clients retained across reloads.+ self._conn_snapshot = (self._endpoint, self._api_key, self._account, self._user, self._agent)self._httpx = _get_httpx()if self._httpx is None:raise ImportError("httpx is required for OpenViking: pip install httpx")@@ -1813,17 +1815,16 @@ class OpenVikingMemoryProvider(MemoryProvider):def _user_space(self, client=None, *, timeout: Optional[float] = None) -> str:"""Resolve the user space, caching only a confirmed connection identity.- Cache is keyed on the connection snapshot, not the client object:- _new_client() builds fresh clients from the same snapshot on every write.- getattr() throughout: hand-wired providers (``__new__``) may lack these fields.+ Use the client's snapshot even when a reload has replaced the active connection.+ Clients with the same resolved settings share the cache; unbound clients do not."""active = client if client is not None else getattr(self, "_client", None)- snapshot = getattr(self, "_conn_snapshot", None)+ snapshot = getattr(active, "_conn_snapshot", None)cached = getattr(self, "_user_space_cache", None)- if active is not None and cached is not None and cached[0] == snapshot:+ if snapshot is not None and cached is not None and cached[0] == snapshot:return cached[1]if active is not None and (resolved := _resolve_user_space(active, timeout=timeout)):- if snapshot is not None and snapshot is getattr(self, "_conn_snapshot", None): # unchanged under us+ if snapshot is not None:self._user_space_cache = (snapshot, resolved)return resolvedreturn str(getattr(active, "_user", "") or getattr(self, "_user", "") or "default").strip() or "default"diff --git a/examples/hermes-plugin/tests/test_standalone.py b/examples/hermes-plugin/tests/test_standalone.pyindex 2cead086f..b4cfdac39 100644--- a/examples/hermes-plugin/tests/test_standalone.py+++ b/examples/hermes-plugin/tests/test_standalone.py@@ -339,3 +339,74 @@ def test_external_provider_forget_keeps_verified_connection(external_provider, userver.server_close()for server_thread in server_threads:server_thread.join(timeout=5)++[email protected]("operation", ["mirror", "recall"])+def test_external_provider_keeps_user_identity_across_reload(+ external_provider, monkeypatch, operation+):+ _, provider, module, _ = external_provider("identity-reload")+ captured = threading.Event()+ resume = threading.Event()+ requests = []++ def get(client, path, params=None, **_kwargs):+ requests.append((client._user, path, params))+ if path == "/api/v1/system/status":+ return {"result": {"user": client._user}}+ return {"result": {"content": f"Profile for {client._user}"}}++ monkeypatch.setattr(module._VikingClient, "get", get)+ monkeypatch.setattr(module._VikingClient, "post", lambda *_args, **_kwargs: {})+ resolve = provider._user_space++ def delayed_identity(client=None, **kwargs):+ if client is not None and client._user == "alice":+ captured.set()+ assert resume.wait(timeout=10)+ return resolve(client, **kwargs)++ monkeypatch.setattr(provider, "_user_space", delayed_identity)++ def publish(user):+ provider._endpoint, provider._api_key = "http://127.0.0.1:1933", ""+ provider._account, provider._user, provider._agent = "test", user, "hermes"+ provider._publish_client(provider._build_client(), provider._endpoint)++ publish("alice")+ worker = threading.Thread(+ target=lambda: (+ provider.on_memory_write("add", "memory", "Alice prefers tea")+ if operation == "mirror"+ else provider.prefetch("", session_id="alice-session")+ )+ )+ worker.start()+ try:+ assert captured.wait(timeout=10)+ publish("bob")+ resume.set()+ worker.join(timeout=10)+ assert not worker.is_alive()+ assert provider._join_all(lambda: list(provider._memory_write_threads), 10)+ block = provider.prefetch("", session_id="bob-session")+ assert "viking://user/bob/memories/profile.md" in block+ assert "viking://user/alice/" not in block+ assert any(user == "bob" and path == "/api/v1/system/status" for user, path, _ in requests)+ finally:+ resume.set()+ worker.join(timeout=10)+++def test_external_provider_does_not_cache_unbound_client_identity(external_provider):+ from types import SimpleNamespace++ _, provider, module, _ = external_provider("unbound-identity")+ provider._client = module._VikingClient("http://127.0.0.1:1933", user="bob")+ provider._conn_snapshot = ("http://127.0.0.1:1933", "", "default", "bob", "hermes")+ provider._client.get = lambda *_args, **_kwargs: {"result": {"user": "bob"}}+ unbound = SimpleNamespace(get=lambda *_args, **_kwargs: {"result": {"user": "alice"}})+ assert provider._user_space(unbound) == "alice"+ assert provider._user_space() == "bob"+ assert provider._user_space(unbound) == "alice"+ assert provider._user_space() == "bob"