Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(hermes): rotate read-only session state for recall (#5372)

z1gon53360209b99f
diff --git a/examples/hermes-plugin/__init__.py b/examples/hermes-plugin/__init__.py
index 0e1cefdfd..25ea4af8b 100644
--- a/examples/hermes-plugin/__init__.py
+++ b/examples/hermes-plugin/__init__.py
@@ -2660,27 +2660,26 @@ class OpenVikingMemoryProvider(MemoryProvider):
self._commit_session(sid, turn_count, context="on session end", scope=scope)
def on_session_switch(self, new_session_id: str, *, parent_session_id: str = "", reset: bool = False, **kwargs) -> None:
- """Commit the old session and rotate cached state to the new session_id.
+ """Rotate cached state to the new session_id; commit only when writes are enabled.
Fires on /resume, /branch, /reset, /new, and context compression. Without it
``_session_id`` stays stuck at the initialize() value, later sync_turn writes
land in the closed session and the new one never gets extracted. The old
session's drain+commit is offloaded so command threads never block.
+ Read-only contexts still rotate so deep search uses the current session.
The new session never accumulates messages, and memory extraction never fires for it. See
hermes-agent#28296.
"""
- if not self._writes_enabled:
- return
new_id = str(new_session_id or "").strip()
- if not new_id or not self._ensure_client():
+ if not new_id or (self._writes_enabled and not self._ensure_client()):
return
rewound = bool(kwargs.get("rewound"))
compression = kwargs.get("reason") == "compression"
# Rotate under the lock so a concurrent sync_turn lands fully under old or new.
with self._session_state_lock:
- scope = self._capture_commit_scope()
+ scope = self._capture_commit_scope() if self._writes_enabled else None
# Rotate cached session state synchronously (cheap, in-memory) and snapshot the old session
# under the lock so a concurrent sync_turn either lands fully before the rotation (counted under
# old) or fully after (counted under new) — never split. The OLD session's commit (drain +
@@ -2702,7 +2701,7 @@ class OpenVikingMemoryProvider(MemoryProvider):
# Re-inject the profile after compression; the prefetch key may be either id.
self._profile_prefetched_sessions.discard(old_session_id)
self._profile_prefetched_sessions.discard(new_id)
- if not rotate and old_session_id:
+ if not rotate and old_session_id and self._writes_enabled:
# In-place compression keeps the same (still live) sid, which compress_context()
# just committed and latched. Re-arm so later commits aren't rejected. Rotation
# mode is untouched: the old id stays latched to dedupe its async finalizer.
@@ -2711,7 +2710,7 @@ class OpenVikingMemoryProvider(MemoryProvider):
if not rotate:
logger.debug("OpenViking on_session_switch skipped rotation: session=%s rewound=%s", old_session_id, rewound)
return
- if old_session_id:
+ if old_session_id and self._writes_enabled:
self._finalize_session_async(old_session_id, old_turn_count, context="on switch", scope=scope)
logger.debug("OpenViking on_session_switch: old=%s new=%s parent=%s reset=%s", old_session_id, new_id, parent_session_id, reset)
diff --git a/examples/hermes-plugin/tests/test_standalone.py b/examples/hermes-plugin/tests/test_standalone.py
index dc3eb2873..b81923e68 100644
--- a/examples/hermes-plugin/tests/test_standalone.py
+++ b/examples/hermes-plugin/tests/test_standalone.py
@@ -705,6 +705,36 @@ def test_non_primary_contexts_skip_writes(external_provider, monkeypatch, agent_
assert provider._pending_sessions() == []
[email protected]("agent_context", ["cron", "subagent", "flush"])
+def test_non_primary_session_switch_keeps_search_on_current_session(external_provider, monkeypatch, agent_context):
+ """A read-only provider must still follow session changes for recall."""
+ from unittest.mock import Mock
+
+ from agent.memory_manager import MemoryManager
+
+ home, provider, module, _ = external_provider(f"read-only-switch-{agent_context}")
+ monkeypatch.setenv("OPENVIKING_ENDPOINT", "http://127.0.0.1:19531")
+ monkeypatch.setattr(module, "_classify_runtime_openviking_health", lambda *_: ("healthy", ""))
+ provider.initialize("old-sid", hermes_home=str(home), agent_context=agent_context)
+ client = Mock()
+ client.post.return_value = {"result": {"memories": []}}
+ provider._client = client
+ provider._ensure_client = lambda: client
+ provider._profile_prefetched_sessions.update({"old-sid", "new-sid"})
+
+ manager = MemoryManager()
+ manager.add_provider(provider)
+ manager.on_session_switch("new-sid", reason="compression")
+ assert provider._session_id == "new-sid"
+ assert provider._profile_prefetched_sessions == set()
+ provider.handle_tool_call("viking_search", {"query": "preferences", "mode": "deep"})
+ provider.on_session_end([])
+
+ client.post.assert_called_once_with(
+ "/api/v1/search/search", {"query": "preferences", "session_id": "new-sid"}
+ )
+
+
def test_live_commit_does_not_block_next_turn_or_lose_its_pending_marker(external_provider, monkeypatch):
from concurrent.futures import ThreadPoolExecutor