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(langchain): recover stale OpenViking clients (#2246)
Hao Zhe7c0bf137aadd
diff --git a/openviking/integrations/langchain/client.py b/openviking/integrations/langchain/client.pyindex b4676ca17..fef6ff0f6 100644--- a/openviking/integrations/langchain/client.py+++ b/openviking/integrations/langchain/client.py@@ -12,6 +12,34 @@ from typing import Any, Iterable, Literallogger = logging.getLogger(__name__)+_RETRYABLE_READ_METHODS = {+ "abstract",+ "archive_expand",+ "archive_search",+ "find",+ "get_session",+ "get_session_archive",+ "get_session_context",+ "get_status",+ "glob",+ "health",+ "is_healthy",+ "ls",+ "overview",+ "read",+ "relations",+ "search",+ "session_exists",+ "stat",+}++_RECOVERABLE_OPENVIKING_CODES = {"DEADLINE_EXCEEDED", "UNAVAILABLE"}+_RECOVERABLE_RUNTIME_MESSAGE_FRAGMENTS = (+ "event loop is closed",+ "attached to a different loop",+ "bound to a different event loop",+)+class OptionalDependencyError(ImportError):"""Raised when an optional framework dependency is not installed."""@@ -50,29 +78,80 @@ class OpenVikingCommitPolicy:pending_token_threshold: int = 8_000+class OpenVikingClientHandle:+ """Lazy OpenViking client wrapper with one-shot recovery for safe reads."""++ def __init__(self, connection: OpenVikingConnection):+ self._connection = connection+ self._client: Any = None++ @property+ def _initialized(self) -> bool:+ client = self._client+ return bool(client is not None and getattr(client, "_initialized", False))++ def close(self) -> None:+ self.reset()++ def reset(self) -> None:+ client = self._client+ self._client = None+ if client is None:+ return+ close = getattr(client, "close", None)+ if not callable(close):+ return+ try:+ close()+ except Exception:+ logger.debug("OpenViking client close during recovery failed", exc_info=True)++ def get(self) -> Any:+ if self._client is None:+ self._client = _create_client_from_connection(self._connection)+ return self._client++ def _openviking_call(self, method_name: str, /, **kwargs: Any) -> Any:+ return self._call_with_recovery(method_name, **kwargs)++ def _call_with_recovery(self, method_name: str, /, *args: Any, **kwargs: Any) -> Any:+ try:+ return _call_client_method(self.get(), method_name, *args, **kwargs)+ except Exception as exc:+ if not _is_recoverable_client_error(exc):+ raise+ self.reset()+ if not _should_retry_method(method_name):+ raise+ try:+ return _call_client_method(self.get(), method_name, *args, **kwargs)+ except Exception as retry_exc:+ if _is_recoverable_client_error(retry_exc):+ self.reset()+ raise++ def __getattr__(self, name: str) -> Any:+ attr = getattr(self.get(), name)+ if not callable(attr):+ return attr++ def recovered_method(*args: Any, **kwargs: Any) -> Any:+ return self._call_with_recovery(name, *args, **kwargs)++ return recovered_method++def ensure_client(connection: OpenVikingConnection) -> Any:"""Return an initialized OpenViking client from explicit or connection settings."""client = connection.clientif client is None:if connection.url or connection.path is None:- from openviking.client import SyncHTTPClient-- client = SyncHTTPClient(- url=connection.url,- api_key=connection.api_key,- account=connection.account,- user=connection.user,- user_id=connection.user_id,- agent_id=connection.agent_id,- timeout=connection.timeout,- extra_headers=connection.extra_headers,- )- else:- from openviking.sync_client import SyncOpenViking-- client = SyncOpenViking(path=connection.path)-+ handle = OpenVikingClientHandle(connection)+ if connection.auto_initialize:+ handle.get()+ return handle+ return _create_client_from_connection(connection)if connection.auto_initialize and hasattr(client, "initialize"):if not getattr(client, "_initialized", False):client.initialize()@@ -118,11 +197,52 @@ def maybe_commit_session(def call_openviking(client: Any, method_name: str, /, **kwargs: Any) -> Any:"""Call a client method, filtering kwargs unsupported by local/HTTP variants."""+ openviking_call = getattr(client, "_openviking_call", None)+ if callable(openviking_call):+ return openviking_call(method_name, **kwargs)+ return _call_client_method(client, method_name, **kwargs)+++def _create_client_from_connection(connection: OpenVikingConnection) -> Any:+ if connection.url or connection.path is None:+ from openviking.client import SyncHTTPClient++ client = SyncHTTPClient(+ url=connection.url,+ api_key=connection.api_key,+ account=connection.account,+ user=connection.user,+ user_id=connection.user_id,+ agent_id=connection.agent_id,+ timeout=connection.timeout,+ extra_headers=connection.extra_headers,+ )+ else:+ from openviking.sync_client import SyncOpenViking++ client = SyncOpenViking(path=connection.path)++ if connection.auto_initialize and hasattr(client, "initialize"):+ if not getattr(client, "_initialized", False):+ client.initialize()+ return client+++def _call_client_method(+ client: Any,+ method_name: str,+ /,+ *args: Any,+ **kwargs: Any,+) -> Any:method = getattr(client, method_name)try:signature = inspect.signature(method)except (TypeError, ValueError):- return method(**{key: value for key, value in kwargs.items() if value is not None})+ return method(+ *args,+ **{key: value for key, value in kwargs.items() if value is not None},+ )accepts_kwargs = any(parameter.kind == inspect.Parameter.VAR_KEYWORD@@ -136,7 +256,27 @@ def call_openviking(client: Any, method_name: str, /, **kwargs: Any) -> Any:for key, value in kwargs.items()if value is not None and key in signature.parameters}- return method(**filtered)+ return method(*args, **filtered)+++def _should_retry_method(method_name: str) -> bool:+ return method_name in _RETRYABLE_READ_METHODS+++def _is_recoverable_client_error(exc: BaseException) -> bool:+ code = getattr(exc, "code", None)+ if isinstance(code, str) and code.upper() in _RECOVERABLE_OPENVIKING_CODES:+ return True+ if isinstance(exc, (ConnectionError, TimeoutError, OSError)):+ return True+ if isinstance(exc, RuntimeError):+ message = str(exc).lower()+ return any(fragment in message for fragment in _RECOVERABLE_RUNTIME_MESSAGE_FRAGMENTS)+ try:+ import httpx+ except Exception: # pragma: no cover - httpx is available in HTTP installs+ return False+ return isinstance(exc, (httpx.TimeoutException, httpx.TransportError))def result_groups(result: Any) -> list[tuple[str, list[Any]]]:diff --git a/openviking_cli/utils/async_utils.py b/openviking_cli/utils/async_utils.pyindex 86211773e..d025a99bd 100644--- a/openviking_cli/utils/async_utils.py+++ b/openviking_cli/utils/async_utils.py@@ -64,15 +64,16 @@ def run_async(coro: Coroutine[None, None, T]) -> T:Returns:The result of coroutine"""- # Detect re-entrancy: if the current thread already has a running event- # loop, we cannot use run_until_complete or block on the shared loop.- # Spawn a helper thread with its own loop instead.+ # Detect re-entrancy. If this is called from the shared loop's own thread we+ # cannot block on that same loop, so use a helper thread. Calls from other+ # async runtimes should still use the shared loop so stateful async clients+ # stay attached to the loop where they were initialized.try:running_loop = asyncio.get_running_loop()except RuntimeError:running_loop = None- if running_loop is not None:+ if running_loop is not None and running_loop is _loop:result_box: list = []error_box: list = []diff --git a/tests/client/test_rebuild_clients.py b/tests/client/test_rebuild_clients.pyindex 73ec6a0c3..66813137f 100644--- a/tests/client/test_rebuild_clients.py+++ b/tests/client/test_rebuild_clients.py@@ -1,9 +1,12 @@+import asyncio+import threadingfrom types import SimpleNamespacefrom unittest.mock import AsyncMock, patchimport pytestimport openviking_cli.client.http as http_module+import openviking_cli.utils.async_utils as async_utilsfrom openviking import AsyncOpenViking, SyncOpenVikingfrom openviking.client.local import LocalClientfrom openviking_cli.client.http import AsyncHTTPClient@@ -119,3 +122,22 @@ def test_sync_http_client_reindex_forwards_to_async_client():assert result == {"status": "accepted"}assert mock_run.calledassert mock_reindex.called+++def test_run_async_from_foreign_event_loop_uses_shared_background_loop():+ async_utils._shutdown_loop()+ seen_threads: list[int] = []++ async def _capture_thread_id():+ seen_threads.append(threading.get_ident())+ return "ok"++ async def _outer():+ return async_utils.run_async(_capture_thread_id())++ try:+ assert asyncio.run(_outer()) == "ok"+ assert async_utils._loop_thread is not None+ assert seen_threads == [async_utils._loop_thread.ident]+ finally:+ async_utils._shutdown_loop()diff --git a/tests/unit/test_langchain_integration.py b/tests/unit/test_langchain_integration.pyindex 3590aa9d6..eb3a84de3 100644--- a/tests/unit/test_langchain_integration.py+++ b/tests/unit/test_langchain_integration.py@@ -22,6 +22,7 @@ from openviking.integrations.langchain import ()from openviking.integrations.langchain.client import (OpenVikingConnection,+ call_openviking,ensure_client,maybe_commit_session,)@@ -182,14 +183,182 @@ def test_ensure_client_defaults_to_http_client(monkeypatch):))- assert isinstance(client, FakeHTTPClient)assert client._initialized is True+ assert isinstance(client.get(), FakeHTTPClient)assert created["api_key"] == "test-key"assert created["user_id"] == "test-user"assert created["agent_id"] == "test-agent"assert created["url"] is None+def test_ensure_client_keeps_local_path_clients_direct(monkeypatch, tmp_path):+ created = {}++ class FakeLocalClient:+ def __init__(self, path):+ created["path"] = path+ self._initialized = False++ def initialize(self):+ self._initialized = True++ import openviking.sync_client as sync_client_module++ monkeypatch.setattr(sync_client_module, "SyncOpenViking", FakeLocalClient)++ client = ensure_client(OpenVikingConnection(path=str(tmp_path)))++ assert isinstance(client, FakeLocalClient)+ assert client._initialized is True+ assert created["path"] == str(tmp_path)+++def test_openviking_client_retries_recoverable_read_with_fresh_client(monkeypatch):+ instances = []++ class FlakyHTTPClient:+ def __init__(self, **_kwargs):+ self.index = len(instances)+ self.closed = False+ self._initialized = False+ instances.append(self)++ def initialize(self):+ self._initialized = True++ def close(self):+ self.closed = True++ def find(self, **_kwargs):+ if self.index == 0:+ raise ConnectionError("OpenViking server was not ready")+ return {+ "memories": [+ {+ "uri": "viking://user/default/memories/profile.md",+ "abstract": "OpenViking recovered",+ "overview": "OpenViking recovered",+ }+ ],+ "resources": [],+ "skills": [],+ }++ import openviking.client as client_module++ monkeypatch.setattr(client_module, "SyncHTTPClient", FlakyHTTPClient)++ client = ensure_client(OpenVikingConnection(url="http://localhost:1933"))+ result = call_openviking(client, "find", query="recover")++ assert result["memories"][0]["abstract"] == "OpenViking recovered"+ assert len(instances) == 2+ assert instances[0].closed is True+ assert instances[1]._initialized is True+++def test_openviking_client_handle_filters_kwargs_for_direct_calls(monkeypatch):+ class FakeHTTPClient:+ def __init__(self, **_kwargs):+ self._initialized = False++ def initialize(self):+ self._initialized = True++ def find(self, query):+ return {"query": query}++ import openviking.client as client_module++ monkeypatch.setattr(client_module, "SyncHTTPClient", FakeHTTPClient)++ client = ensure_client(OpenVikingConnection(url="http://localhost:1933"))+ result = client.find("recover", unsupported="ignored")++ assert result == {"query": "recover"}+++def test_openviking_client_evicts_but_does_not_retry_mutating_call(monkeypatch):+ instances = []++ class FlakyHTTPClient:+ def __init__(self, **_kwargs):+ self.closed = False+ self._initialized = False+ instances.append(self)++ def initialize(self):+ self._initialized = True++ def close(self):+ self.closed = True++ def add_message(self, **_kwargs):+ raise ConnectionError("OpenViking connection dropped during write")++ import openviking.client as client_module++ monkeypatch.setattr(client_module, "SyncHTTPClient", FlakyHTTPClient)++ client = ensure_client(OpenVikingConnection(url="http://localhost:1933"))+ with pytest.raises(ConnectionError):+ call_openviking(+ client,+ "add_message",+ session_id="session-1",+ role="user",+ parts=[{"type": "text", "text": "hello"}],+ )++ assert len(instances) == 1+ assert instances[0].closed is True+++def test_retriever_recovers_from_stale_cached_remote_client(monkeypatch):+ instances = []++ class FlakyHTTPClient:+ def __init__(self, **_kwargs):+ self.index = len(instances)+ self.closed = False+ self._initialized = False+ instances.append(self)++ def initialize(self):+ self._initialized = True++ def close(self):+ self.closed = True++ def find(self, **_kwargs):+ if self.index == 0:+ raise RuntimeError("Event loop is closed")+ return {+ "memories": [],+ "resources": [+ {+ "uri": "viking://resources/recovered.md",+ "level": 1,+ "abstract": "Recovered resource",+ "overview": "Recovered resource overview",+ "score": 1.0,+ }+ ],+ "skills": [],+ }++ import openviking.client as client_module++ monkeypatch.setattr(client_module, "SyncHTTPClient", FlakyHTTPClient)++ retriever = OpenVikingRetriever(url="http://localhost:1933")+ docs = retriever.invoke("recover")++ assert [doc.metadata["openviking_uri"] for doc in docs] == ["viking://resources/recovered.md"]+ assert len(instances) == 2+ assert instances[0].closed is True++def test_chat_message_history_preserves_tool_parts():client = InMemoryOpenVikingClient()history = OpenVikingChatMessageHistory(session_id="history-session", client=client)