Skip to content

The agent that grows with you

fix(dashboard): backfill /api/sessions/search with title matches

Austin Pickett5515c4be557c
diff --git a/hermes_cli/web_routers/sessions.py b/hermes_cli/web_routers/sessions.py
index b1d19944d7c..35918598d9a 100644
--- a/hermes_cli/web_routers/sessions.py
+++ b/hermes_cli/web_routers/sessions.py
@@ -427,6 +427,34 @@ async def search_sessions(
add_lineage_result(
m["session_id"],
hit_payload(m, m.get("snippet", ""), m.get("role"), m.get("session_started")))
+
+ # Title matches fill any remaining slots (#66242): the FTS index
+ # only covers message content, so a term that lives solely in a
+ # manually-set sessions.title would otherwise return nothing. The
+ # DB layer already knows how to LIKE-match titles across the whole
+ # compression chain (list_sessions_rich(search_query=) — the same
+ # helper the sidebar listing uses), so reuse it rather than adding
+ # a second title query path here. Best-effort: an old/odd store
+ # that rejects the call just skips the lane.
+ if len(seen) < safe_limit:
+ try:
+ title_rows = db.list_sessions_rich(
+ search_query=q.strip(), include_archived=True, order_by_last_active=True,
+ source=source_filter, sources=source_list or None,
+ exclude_sources=exclude_list or None, limit=safe_limit)
+ except Exception: # health: allow BLE001 -- best-effort supplement lane: an old/odd store that rejects the search_query read must not fail the id+content results already collected
+ _log.debug("Title-match supplement skipped for %r", q[: 200])
+ title_rows = []
+ for row in title_rows:
+ if len(seen) >= safe_limit:
+ break
+ sid = row.get("id")
+ if not sid:
+ continue
+ preview = (row.get("preview") or "").strip()
+ add_lineage_result(
+ sid, hit_payload(row, preview or f"Session title matched: {q.strip()}",
+ None, row.get("started_at")))
return {"results": list(seen.values())}
# FTS over a large state.db is the slowest read here; keep it off the loop (#60747).
diff --git a/tests/hermes_cli/test_web_server_session_search_titles.py b/tests/hermes_cli/test_web_server_session_search_titles.py
new file mode 100644
index 00000000000..d3450be9add
--- /dev/null
+++ b/tests/hermes_cli/test_web_server_session_search_titles.py
@@ -0,0 +1,98 @@
+"""GET /api/sessions/search must match session titles, not only ids and FTS5 message content.
+
+Regression for #66242: the search endpoint composed only
+``search_sessions_by_id`` (id/lineage LIKE) with ``search_messages`` (FTS5 over
+message content), so a term that lived solely in a manually-set
+``sessions.title`` returned zero results. The DB layer already had title
+matching — ``list_sessions_rich(search_query=...)`` LIKE-matches titles across
+the whole compression chain — and the endpoint never called it. The fix
+backfills remaining result slots through that existing helper via the same
+add_lineage_result dedup path (content hits keep BM25 priority).
+
+These tests drive the real SessionDB through the real route handler (no fake
+DB) so the title lane is exercised end to end, including the SQLite
+read-only connection the endpoint opens.
+"""
+
+import asyncio
+import threading
+
+import pytest
+
+import hermes_cli.web_server_sessions as _web_server_sessions
+import hermes_cli.web_routers.sessions as _rt_sessions
+from hermes_state import SessionDB
+
+
+def real_db(tmp_path):
+ db = SessionDB(db_path=tmp_path / "state.db")
+ # A session whose message content never contains the search needle —
+ # only its (manually set) title does.
+ db.create_session(session_id="titled_session", source="cli", model="m")
+ db.set_session_title("titled_session", "Building the Modpack Server")
+ db.append_message("titled_session", role="user", content="hello world")
+ # A content-matching session: the needle appears in message text only.
+ db.create_session(session_id="content_session", source="cli", model="m")
+ db.append_message("content_session", role="user", content="modpack build notes")
+ # An unrelated session that must never surface.
+ db.create_session(session_id="other_session", source="cli", model="m")
+ db.append_message("other_session", role="user", content="completely unrelated")
+ yield db
+ try:
+ db.close()
+ except Exception:
+ pass
+
+
+def db_path_for_profile(real_db, monkeypatch):
+ """Point the endpoint's profile DB opener at the real temp state.db."""
+ monkeypatch.setattr(
+ _web_server_sessions, "_session_db_path_for_profile", lambda profile: real_db.db_path
+ )
+
+
+def test_search_finds_session_by_partial_title(db_path_for_profile):
+ """A term that appears ONLY in the title is findable (#66242); the snippet
+ falls back to the session preview, and rows carry the same shape as id hits."""
+ response = asyncio.run(_rt_sessions.search_sessions(q="Modpack", limit=20))
+ ids = [row["session_id"] for row in response["results"]]
+ assert "titled_session" in ids
+ row = next(r for r in response["results"] if r["session_id"] == "titled_session")
+ assert row["snippet"] # preview or the title-match fallback text
+ assert row["title"] == "Building the Modpack Server"
+
+
+def test_search_title_lane_keeps_content_hits_priority(db_path_for_profile):
+ """Content hits keep their BM25 priority; the title lane only fills
+ remaining slots and dedupes by lineage root against earlier lanes."""
+ response = asyncio.run(_rt_sessions.search_sessions(q="modpack", limit=20))
+ ids = [row["session_id"] for row in response["results"]]
+ assert set(ids) == {"content_session", "titled_session"}
+ assert ids[0] == "content_session" # FTS content hit outranks the title backfill
+ assert "other_session" not in ids
+
+
+def test_search_title_lane_respects_limit(db_path_for_profile):
+ """The merged result stays capped at limit across all lanes."""
+ response = asyncio.run(_rt_sessions.search_sessions(q="modpack", limit=1))
+ assert len(response["results"]) == 1
+
+
+def test_search_title_lane_runs_off_event_loop(db_path_for_profile, monkeypatch):
+ """The title backfill runs on the endpoint's worker thread, not the loop
+ thread (#60747 discipline): the whole _search body already runs in
+ asyncio.to_thread, so the real SessionDB calls must never land on the loop."""
+ loop_thread = threading.get_ident()
+ db_threads: list[int] = []
+ real_search_messages = SessionDB.search_messages
+
+ def spy(self, *args, **kwargs):
+ db_threads.append(threading.get_ident())
+ return real_search_messages(self, *args, **kwargs)
+
+ monkeypatch.setattr(SessionDB, "search_messages", spy)
+ asyncio.run(_rt_sessions.search_sessions(q="Modpack", limit=20))
+ assert db_threads
+ assert all(tid != loop_thread for tid in db_threads)