Skip to content

The agent that grows with you

fix(session_search): read the state DB in a managed worker instead of the store

686f6c61222563468ef2
diff --git a/tests/tools/test_session_search_worker_store.py b/tests/tools/test_session_search_worker_store.py
new file mode 100644
index 00000000000..f055932f57f
--- /dev/null
+++ b/tests/tools/test_session_search_worker_store.py
@@ -0,0 +1,64 @@
+"""Recall in a gateway-managed worker (#141705).
+
+The worker's agent is constructed with ``session_db=RuntimeSessionStore`` (``agent/managed_worker.py``),
+a facade over the owner's RPC that mirrors the write and lineage surface but not the FTS read API.
+Handing it to ``session_search`` made every recall inside a worker answer
+``'RuntimeSessionStore' object has no attribute 'search_messages'``; recall now opens its own read-only
+handle on the profile's state DB, the same one the no-db path uses.
+"""
+import json
+
+import pytest
+
+from agent.runtime_session_store import RuntimeSessionStore
+from hermes_state import SessionDB
+from tools.session_search_tool import session_search
+
+
+def home(tmp_path, monkeypatch):
+ """The worker's profile home: ``get_hermes_home()`` resolves to it, so its state.db is the DB."""
+ import hermes_constants
+
+ monkeypatch.setattr(hermes_constants, "get_hermes_home", lambda: tmp_path)
+ return tmp_path
+
+
+def _seed(path, session_id, content):
+ db = SessionDB(path / "state.db")
+ db.create_session(session_id, source="cli")
+ db.append_message(session_id, role="user", content=content)
+ db.append_message(session_id, role="assistant", content="noted")
+ return db
+
+
+def _worker_store(home):
+ """The real facade, with a no-op owner RPC: only the capability matters here."""
+ return RuntimeSessionStore(lambda method, **params: {}, {"session_id": "worker-s"}, home / "outbox")
+
+
+def test_worker_recall_reads_the_state_db_instead_of_the_store(home):
+ db = _seed(home, "worker-s", "the zanzibar rollout is blocked on the store")
+ try:
+ store = _worker_store(home)
+ # The capability the worker's handle lacks — the AttributeError the report quotes.
+ assert not hasattr(store, "search_messages")
+ payload = json.loads(session_search(query="zanzibar", db=store))
+ finally:
+ db.close()
+ assert payload["success"] is True, payload
+ assert [r["session_id"] for r in payload["results"]] == ["worker-s"]
+
+
+def test_a_readable_handle_is_still_used_as_given(home, tmp_path):
+ """A real SessionDB passed by a caller is not swapped for the home DB."""
+ home_db = _seed(home, "home-session", "zanzibar appears only in the home transcript")
+ elsewhere = tmp_path / "elsewhere"
+ elsewhere.mkdir()
+ passed = _seed(elsewhere, "passed-in", "zanzibar from the caller's handle")
+ try:
+ payload = json.loads(session_search(query="zanzibar", db=passed))
+ finally:
+ passed.close()
+ home_db.close()
+ assert [r["session_id"] for r in payload["results"]] == ["passed-in"]
diff --git a/tools/session_search_tool.py b/tools/session_search_tool.py
index fcfba5b2e0b..ef5d9572e79 100644
--- a/tools/session_search_tool.py
+++ b/tools/session_search_tool.py
@@ -75,6 +75,18 @@ def _loud(fn, log_msg, error_prefix, *log_args):
return None, tool_error(f"{error_prefix}: {e}", success=False)
+def _is_worker_store(db) -> bool:
+ """True for the managed-worker facade (``RuntimeSessionStore``), which has no FTS read API.
+
+ A worker's agent is built with that store as its ``session_db``; recall reads the profile's
+ state DB directly instead of going through the owner RPC (see ``session_search``)."""
+ try:
+ from agent.runtime_session_store import RuntimeSessionStore
+ except ImportError: # pragma: no cover - tools-only install without the worker package
+ return False
+ return isinstance(db, RuntimeSessionStore)
+
+
def _format_timestamp(ts: float | str | None) -> str:
"""Unix timestamp -> readable date; ISO strings pass through; "unknown" for None."""
if ts is None:
@@ -626,6 +638,13 @@ def session_search(query: str = "", role_filter: str | None = None, limit: int =
if profile_db is not None:
db, current_session_id = profile_db, None
owned_dbs.append(profile_db)
+ # A managed worker's handle is a RuntimeSessionStore: it mirrors the owner's write and lineage
+ # surface, not the FTS read API, and every read is an owner RPC. Handing it here made recall in a
+ # worker fail with `'RuntimeSessionStore' object has no attribute 'search_messages'` (#141705), so
+ # recall opens its own read-only handle — the same one the no-db path below uses.
+ if _is_worker_store(db):
+ logging.debug("session_search: reading a read-only handle instead of the worker's store")
+ db = None
if db is None:
db = _quiet(
lambda: SessionDB(db_path=get_hermes_home() / "state.db", read_only=True),