From b0cf2597c2dbb9dacd5c4f063ae7e71587f02c2e Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Tue, 25 Aug 2026 12:13:49 +0530 Subject: [PATCH] =?UTF-8?q?fix:=20follow-up=20for=20salvaged=20PR=20#93985?= =?UTF-8?q?=20=E2=80=94=20cache=20key,=20snapshot,=20dead=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Key _user_space_cache on _conn_snapshot instead of client object identity, so _new_client() results from the same connection share the cached user (previously every on_memory_write triggered an uncached /api/v1/system/status probe with a 30s default timeout) - Thread a short timeout (0.05s) through the write-path identity probe - Harden _tool_remember to snapshot the client before URI construction + POST, matching the pattern already established in on_memory_write - Remove dead instance method _user_scoped_uri (zero callers; all call sites use the module-level function directly) Co-authored-by: ehz0ah --- plugins/memory/openviking/__init__.py | 48 +++++++++++++++------------ 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/plugins/memory/openviking/__init__.py b/plugins/memory/openviking/__init__.py index 5886295bbd..f46052bae3 100644 --- a/plugins/memory/openviking/__init__.py +++ b/plugins/memory/openviking/__init__.py @@ -2271,11 +2271,11 @@ class OpenVikingMemoryProvider(MemoryProvider): self._agent = "" self._session_id = "" self._turn_count = 0 - # Server-asserted user space for explicit-uid URIs (#91995). Bind the - # cached value to the client object that supplied it: /reload can swap - # endpoint, credentials, and identity on this provider instance, and - # an in-flight probe from the old client must not populate the new - # connection's cache. + # Server-asserted user space for explicit-uid URIs (#91995). Key the + # cache on the connection snapshot so all clients built from the same + # snapshot share the resolved user. /reload can swap endpoint, + # credentials, and identity on this provider instance — a different + # snapshot invalidates the cache automatically. self._user_space_cache: Optional[tuple[Any, str]] = None self._hermes_home = "" self._run_id = uuid.uuid4().hex @@ -3912,21 +3912,25 @@ class OpenVikingMemoryProvider(MemoryProvider): return f"{head}{marker}{tail}" if tail else _head_only() def _user_space(self, client=None, *, timeout: Optional[float] = None) -> str: - """Resolve the user space, caching only a confirmed client identity.""" + """Resolve the user space, caching only a confirmed connection identity.""" active = client if client is not None else getattr(self, "_client", None) + # Key the cache on the connection snapshot, not the client object. + # _new_client() builds fresh _VikingClient objects from the same + # snapshot, so object-identity keying would miss the cache on every + # write. The snapshot tuple is published atomically under + # _client_refresh_lock and changes on every config reload. + snapshot = getattr(self, "_conn_snapshot", None) cached = getattr(self, "_user_space_cache", None) - if active is not None and cached is not None and cached[0] is active: + if active is not None and cached is not None and cached[0] == snapshot: return cached[1] if active is not None: resolved = _resolve_user_space(active, timeout=timeout) if resolved: - # The probe can overlap a config reload. Only publish it when - # this is still the provider's active client. Old in-flight - # work can use its resolved value without contaminating the - # replacement client's cache. - if active is getattr(self, "_client", None): - self._user_space_cache = (active, resolved) + # Only publish when the snapshot hasn't changed under us. + current_snapshot = getattr(self, "_conn_snapshot", None) + if snapshot is not None and snapshot is current_snapshot: + self._user_space_cache = (snapshot, resolved) return resolved configured = str( @@ -3936,9 +3940,6 @@ class OpenVikingMemoryProvider(MemoryProvider): ).strip() return configured or "default" - def _user_scoped_uri(self, suffix: str, client=None) -> str: - return _user_scoped_uri(self._user_space(client), suffix) - def _session_start_uris(self, user: Optional[str] = None) -> tuple: user = user or self._user_space() return ( @@ -4872,7 +4873,7 @@ class OpenVikingMemoryProvider(MemoryProvider): old_session_id, new_id, parent_session_id, reset, ) - def _build_memory_uri(self, subdir: str, *, client=None) -> str: + def _build_memory_uri(self, subdir: str, *, client=None, timeout: Optional[float] = None) -> str: """Build a viking:// memory URI under the configured peer namespace.""" slug = uuid.uuid4().hex[:12] # Explicit-uid URIs are canonical under every auth mode; the uid-less @@ -4885,7 +4886,7 @@ class OpenVikingMemoryProvider(MemoryProvider): or _DEFAULT_AGENT ).strip() return _user_scoped_uri( - self._user_space(active_client), + self._user_space(active_client, timeout=timeout), f"peers/{agent}/memories/{subdir}/mem_{slug}.md", ) @@ -4911,7 +4912,9 @@ class OpenVikingMemoryProvider(MemoryProvider): def _write(): try: - uri = self._build_memory_uri(subdir, client=client) + uri = self._build_memory_uri( + subdir, client=client, timeout=_RECALL_MIN_TIMEOUT_SECONDS, + ) client.post("/api/v1/content/write", { "uri": uri, "content": content, @@ -5263,13 +5266,16 @@ class OpenVikingMemoryProvider(MemoryProvider): category = args.get("category", "") subdir = _CATEGORY_SUBDIR_MAP.get(category, _DEFAULT_MEMORY_SUBDIR) - uri = self._build_memory_uri(subdir) + client = self._ensure_client() + if not client: + return tool_error("OpenViking server not connected") + uri = self._build_memory_uri(subdir, client=client) # Write directly via content/write API. # This creates the file, stores the content, and queues vector indexing # in a single call — no dependency on session commit / VLM extraction. try: - result = self._client.post("/api/v1/content/write", { + result = client.post("/api/v1/content/write", { "uri": uri, "content": content, "mode": "create",