Skip to content

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

fix(hermes): defer pending recovery in non-primary contexts (#5917)

Yuchuan He79ecf74f9333
diff --git a/examples/hermes-plugin/CONFIGURATION.md b/examples/hermes-plugin/CONFIGURATION.md
index cf749967f..05ed556fc 100644
--- a/examples/hermes-plugin/CONFIGURATION.md
+++ b/examples/hermes-plugin/CONFIGURATION.md
@@ -360,7 +360,8 @@ exceed it, and the server may include other context during extraction.
When Hermes initializes the provider with `agent_context` set to `cron`, `subagent`,
or `flush`, recall and profile reads keep working. Automatic turn uploads,
session-end/switch commits, and native memory mirroring are skipped for that
-context. Startup recovery can still commit pending messages from earlier sessions.
+context. Startup recovery also skips commits in these contexts. Pending messages
+remain available for recovery when the next primary context starts.
Explicit `viking_*` tools keep their normal behavior, including writes and deletes.
Interactive sessions (and hosts that predate `agent_context`) keep the previous
automatic write behavior.
diff --git a/examples/hermes-plugin/__init__.py b/examples/hermes-plugin/__init__.py
index 18ede0808..7462db7f0 100644
--- a/examples/hermes-plugin/__init__.py
+++ b/examples/hermes-plugin/__init__.py
@@ -2912,6 +2912,8 @@ class OpenVikingMemoryProvider(MemoryProvider):
def _recover_pending_sessions(self) -> None:
"""Commit sessions left pending by dead runs, one thread per former owner."""
+ if not self._writes_enabled:
+ return
scope = self._capture_commit_scope()
if not scope.client:
return
diff --git a/examples/hermes-plugin/tests/test_standalone.py b/examples/hermes-plugin/tests/test_standalone.py
index 6ba9c798f..07450e44e 100644
--- a/examples/hermes-plugin/tests/test_standalone.py
+++ b/examples/hermes-plugin/tests/test_standalone.py
@@ -1128,6 +1128,38 @@ def test_reload_keeps_new_connection_pending_after_old_commit(reload_provider, m
assert commits[0].args == (f"/api/v1/sessions/{sid}/commit", {"keep_recent_count": 0})
[email protected]("agent_context", ["primary", "cron", "subagent", "flush"])
[email protected]("owner_run_id", ["", "dead-owner"])
+def test_startup_recovery_respects_write_context(external_provider, monkeypatch, agent_context, owner_run_id):
+ from unittest.mock import Mock
+
+ home, provider, module, _ = external_provider(f"recovery-{agent_context}")
+ if owner_run_id and module.fcntl is None:
+ pytest.skip("Scoped recovery requires POSIX advisory locks")
+ monkeypatch.setenv("OPENVIKING_ENDPOINT", "http://127.0.0.1:19531")
+ monkeypatch.setattr(module, "_classify_runtime_openviking_health", lambda *_: ("healthy", ""))
+ client = Mock()
+ client.post.return_value = {}
+ monkeypatch.setattr(provider, "_build_client", lambda: client)
+ monkeypatch.setattr(provider, "_new_client", lambda **_: client)
+ provider.initialize("live-sid", hermes_home=str(home))
+ marker = provider._state_path("pending", "previous-sid")
+ marker.parent.mkdir(parents=True, exist_ok=True)
+ marker.write_text(json.dumps({"session_id": "previous-sid", "owner_run_id": owner_run_id}))
+ provider.initialize("live-sid", hermes_home=str(home), agent_context=agent_context)
+ assert provider._drain_finalizers(timeout=5)
+ if agent_context == "primary":
+ client.post.assert_called_once_with("/api/v1/sessions/previous-sid/commit", {"keep_recent_count": 0})
+ assert not marker.exists()
+ else:
+ client.post.assert_not_called()
+ assert marker.exists()
+ provider.initialize("live-sid", hermes_home=str(home), agent_context="primary")
+ assert provider._drain_finalizers(timeout=5)
+ client.post.assert_called_once_with("/api/v1/sessions/previous-sid/commit", {"keep_recent_count": 0})
+ assert not marker.exists()
+
+
def test_reload_recovery_preserves_markers_for_other_connections(reload_provider, monkeypatch):
"""Failed commits survive restart and recover only with matching credentials."""
home, provider, module, backends, reload = reload_provider