Skip to content

The agent that grows with you

fix: keep standalone session search read-only

Teknium0e7c058897dd
diff --git a/tests/tools/test_session_search.py b/tests/tools/test_session_search.py
index 5eec8be08c9..b33476dfc8d 100644
--- a/tests/tools/test_session_search.py
+++ b/tests/tools/test_session_search.py
@@ -151,7 +151,7 @@ class TestBrowseShape:
return []
db = _DB()
- monkeypatch.setattr("hermes_state_registry.acquire", lambda: db)
+ monkeypatch.setattr("hermes_state.SessionDB", lambda **kwargs: db)
monkeypatch.setattr(
"hermes_state_registry.release_or_close",
lambda _: setattr(db, "released", db.released + 1),
diff --git a/tests/tools/test_session_search_readonly.py b/tests/tools/test_session_search_readonly.py
new file mode 100644
index 00000000000..c9f9db02c88
--- /dev/null
+++ b/tests/tools/test_session_search_readonly.py
@@ -0,0 +1,68 @@
+"""Search never becomes another session-database initializer."""
+
+import json
+from pathlib import Path
+import sqlite3
+
+
+
+def test_search_does_not_create_current_storage_for_empty_or_foreign_reads(tmp_path, monkeypatch):
+ from hermes_constants import reset_hermes_home_override, set_hermes_home_override
+ from hermes_state import SessionDB
+ import tools.session_search_tool # noqa: F401
+ from tools.registry import registry
+
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ token = set_hermes_home_override(home)
+ handler = registry.get_entry("session_search").handler
+ try:
+ empty = json.loads(handler({}))
+ assert not (home / "state.db").exists(), "a reader created the canonical database"
+ assert empty["success"] is False
+ other = home / "profiles" / "reader-target"
+ other.mkdir(parents=True)
+ db = SessionDB(other / "state.db")
+ try:
+ db.create_session("foreign-read", source="cli")
+ db.append_message("foreign-read", role="user", content="owned target marker")
+ finally:
+ db.close()
+ for args in [
+ {"session_id": "foreign-read", "profile": "reader-target"},
+ {"session_id": "reader-target/foreign-read"},
+ ]:
+ result = json.loads(handler(args))
+ assert result["success"] is True, result
+ assert result["messages"][0]["content"] == "owned target marker"
+ assert not (home / "state.db").exists()
+ finally:
+ reset_hermes_home_override(token)
+
+
+def test_search_reads_live_storage_without_a_write_commit(tmp_path, monkeypatch):
+ from hermes_constants import reset_hermes_home_override, set_hermes_home_override
+ from hermes_state import SessionDB
+ import tools.session_search_tool # noqa: F401
+ from tools.registry import registry
+
+ home = tmp_path / "profile"
+ home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ token = set_hermes_home_override(home)
+ owner = SessionDB(home / "state.db")
+ observer = sqlite3.connect(home / "state.db")
+ try:
+ owner.create_session("live-read", source="cli")
+ owner.append_message("live-read", role="user", content="live reader marker")
+ before = observer.execute("PRAGMA data_version").fetchone()[0]
+ result = json.loads(registry.get_entry("session_search").handler({"session_id": "live-read"}))
+ assert result["success"] is True, result
+ assert result["messages"][0]["content"] == "live reader marker"
+ assert observer.execute("PRAGMA data_version").fetchone()[0] == before
+ finally:
+ observer.close()
+ owner.close()
+ reset_hermes_home_override(token)
diff --git a/tools/session_search_tool.py b/tools/session_search_tool.py
index b8a34be6e8a..0de3f242bfd 100644
--- a/tools/session_search_tool.py
+++ b/tools/session_search_tool.py
@@ -495,26 +495,8 @@ def _scroll(db, session_id: str, around_message_id: int, window: int = 5,
def _dispatch(query, role_filter, limit, db, current_session_id, session_id,
- around_message_id, window, sort, profile, detail, owned_dbs) -> str:
- """Mode dispatch (see module docstring); scroll wins when an anchor is set.
- Profile DBs opened here are appended to *owned_dbs* for the caller to close."""
- # A raw `@session:<profile>/<id>` link as session_id: ids never contain "/", so
- # split on it and adopt the embedded profile only when none was passed.
- if isinstance(session_id, str) and "/" in session_id:
- emb_profile, _, emb_id = session_id.partition("/")
- if emb_id:
- session_id = emb_id
- if emb_profile and (profile is None or not str(profile).strip()):
- profile = emb_profile
- # Cross-profile: swap in the named profile's DB (read-only) for every shape;
- # current-lineage guards key off ids that won't collide, so they stay inert.
- try:
- profile_db = _resolve_profile_db(profile)
- except Exception as e:
- return tool_error(f"profile '{profile}': {e}", success=False)
- if profile_db is not None:
- db, current_session_id = profile_db, None
- owned_dbs.append(profile_db)
+ around_message_id, window, sort, profile, detail) -> str:
+ """Mode dispatch (see module docstring); scroll wins when an anchor is set."""
if isinstance(session_id, str) and session_id.strip():
if around_message_id is not None:
return _scroll(db, session_id.strip(), around_message_id, window, current_session_id)
@@ -534,17 +516,38 @@ def session_search(query: str = "", role_filter: str = None, limit: int = 3, db=
current_session_id: str = None, session_id: str = None, around_message_id: int = None,
window: int = 5, sort: str = None, profile: str = None, detail: str = "adaptive") -> str:
"""Run session search, closing DBs opened here. Positional order is frozen for old callers."""
- from hermes_state import format_session_db_unavailable
- from hermes_state_registry import acquire, release_or_close
+ from hermes_constants import get_hermes_home
+ from hermes_state import SessionDB, format_session_db_unavailable
+ from hermes_state_registry import release_or_close
owned_dbs: List[Any] = []
+ # A raw `@session:<profile>/<id>` link as session_id: ids never contain "/", so
+ # split on it and adopt the embedded profile only when none was passed.
+ if isinstance(session_id, str) and "/" in session_id:
+ emb_profile, _, emb_id = session_id.partition("/")
+ if emb_id:
+ session_id = emb_id
+ if emb_profile and (profile is None or not str(profile).strip()):
+ profile = emb_profile
+ # Cross-profile: swap in the named profile's DB (read-only) for every shape;
+ # current-lineage guards key off ids that won't collide, so they stay inert.
+ try:
+ profile_db = _resolve_profile_db(profile)
+ except Exception as e:
+ return tool_error(f"profile '{profile}': {e}", success=False)
+ if profile_db is not None:
+ db, current_session_id = profile_db, None
+ owned_dbs.append(profile_db)
if db is None:
- db = _quiet(acquire, None, "SessionDB unavailable for session_search")
+ db = _quiet(
+ lambda: SessionDB(db_path=get_hermes_home() / "state.db", read_only=True),
+ None, "SessionDB unavailable for session_search",
+ )
if db is None:
return tool_error(format_session_db_unavailable(), success=False)
owned_dbs.append(db)
try:
return _dispatch(query, role_filter, limit, db, current_session_id, session_id,
- around_message_id, window, sort, profile, detail, owned_dbs)
+ around_message_id, window, sort, profile, detail)
finally:
for owned_db in reversed(owned_dbs):
_quiet(lambda: release_or_close(owned_db), None, "Failed to close session_search SessionDB")