"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(session): clear the search index when a session is deleted
Rudy Celekli4d9c25bbfd6a
diff --git a/agent/src/session/search.py b/agent/src/session/search.pyindex e0718715a..aae272f08 100644--- a/agent/src/session/search.py+++ b/agent/src/session/search.py@@ -301,6 +301,24 @@ class SessionSearchIndex:)conn.commit()+ @_serialized_operation+ def remove_session(self, session_id: str) -> None:+ """Remove a session and all of its messages from the index.++ The ``messages_ad`` trigger propagates each message deletion into+ ``messages_fts``, so the derived index clears with the base rows.++ Args:+ session_id: Session ID.+ """+ conn = self._get_conn()+ try:+ conn.execute("DELETE FROM messages WHERE session_id = ?", (session_id,))+ conn.execute("DELETE FROM sessions WHERE id = ?", (session_id,))+ conn.commit()+ except sqlite3.OperationalError as exc:+ logger.debug("remove_session failed for %s: %s", session_id, exc)+@staticmethoddef _sanitize_fts_query(query: str) -> str:"""Sanitize a user query for FTS5 MATCH syntax.diff --git a/agent/src/session/service.py b/agent/src/session/service.pyindex 8d8bb0a2f..983b938d1 100644--- a/agent/src/session/service.py+++ b/agent/src/session/service.py@@ -244,7 +244,19 @@ class SessionService:def delete_session(self, session_id: str) -> bool:"""Delete a session."""self.event_bus.clear(session_id)- return self.store.delete_session(session_id)+ deleted = self.store.delete_session(session_id)+ if deleted:+ # The search index is always on, so leftover rows would keep the+ # deleted conversation's title and text searchable.+ try:+ self._search_index.remove_session(session_id)+ except Exception: # noqa: BLE001+ logger.debug(+ "Search index remove_session failed for %s",+ session_id,+ exc_info=True,+ )+ return deletedasync def send_message(self,diff --git a/agent/tests/test_session_delete_clears_search_index.py b/agent/tests/test_session_delete_clears_search_index.pynew file mode 100644index 000000000..4fdf466b3--- /dev/null+++ b/agent/tests/test_session_delete_clears_search_index.py@@ -0,0 +1,112 @@+"""Deleting a session must purge its text from the always-on search index."""++from __future__ import annotations++import json++import pytest+from src.session.events import EventBus+from src.session.models import Message+from src.session.search import SessionSearchIndex+from src.session.service import SessionService+from src.session.store import SessionStore+from src.tools.session_search_tool import SessionSearchTool++DELETED_TERM = "vegacarry"+SIBLING_TERM = "gammascalp"+++def owned_delete_index(tmp_path, monkeypatch):+ """Real service, real index, real search tool over one owned database."""+ index = SessionSearchIndex(tmp_path / "search.db")+ monkeypatch.setattr("src.session.service.get_shared_index", lambda: index)+ monkeypatch.setattr("src.session.search.get_shared_index", lambda: index)+ service = SessionService(+ store=SessionStore(tmp_path / "sessions"),+ event_bus=EventBus(),+ runs_dir=tmp_path / "runs",+ )+ doomed = service.create_session(title="Volatility carry study")+ sibling = service.create_session(title="Unrelated conversation")+ # Messages are admitted straight to the store and index so fixture setup+ # never invokes an agent or a model provider.+ for session, text in (+ (doomed, f"my secret thesis: short {DELETED_TERM} into earnings"),+ (sibling, f"unrelated {SIBLING_TERM} notes"),+ ):+ service.store.append_message(+ Message(session_id=session.session_id, role="user", content=text)+ )+ index.index_message(session.session_id, "user", text)+ try:+ yield service, index, doomed, sibling+ finally:+ index.close()+++def _search_session_ids(query: str) -> list[str]:+ reply = json.loads(SessionSearchTool().execute(query=query))+ assert reply["status"] == "ok", reply+ return [match["session_id"] for match in reply["results"]]+++def _index_rows(index: SessionSearchIndex, session_id: str) -> tuple[int, int]:+ conn = index._get_conn()+ sessions = conn.execute(+ "SELECT COUNT(*) FROM sessions WHERE id = ?", (session_id,)+ ).fetchone()[0]+ messages = conn.execute(+ "SELECT COUNT(*) FROM messages WHERE session_id = ?", (session_id,)+ ).fetchone()[0]+ return sessions, messages+++def _fts_hits(index: SessionSearchIndex, term: str) -> int:+ return index._get_conn().execute(+ "SELECT COUNT(*) FROM messages_fts WHERE messages_fts MATCH ?",+ (f'"{term}"',),+ ).fetchone()[0]+++def test_deleted_session_text_is_no_longer_searchable(owned_delete_index):+ service, index, doomed, sibling = owned_delete_index+ assert _search_session_ids(DELETED_TERM) == [doomed.session_id]++ assert service.delete_session(doomed.session_id) is True++ assert service.store.get_session(doomed.session_id) is None+ assert _search_session_ids(DELETED_TERM) == []+ assert _fts_hits(index, DELETED_TERM) == 0+ assert _index_rows(index, doomed.session_id) == (0, 0)+++def test_delete_leaves_an_unrelated_session_fully_indexed(owned_delete_index):+ service, index, doomed, sibling = owned_delete_index++ assert service.delete_session(doomed.session_id) is True++ assert _search_session_ids(SIBLING_TERM) == [sibling.session_id]+ assert _fts_hits(index, SIBLING_TERM) == 1+ assert _index_rows(index, sibling.session_id) == (1, 1)+ assert (+ index._get_conn()+ .execute("SELECT title FROM sessions WHERE id = ?", (sibling.session_id,))+ .fetchone()[0]+ == "Unrelated conversation"+ )+++def test_delete_succeeds_when_the_search_index_is_unavailable(+ owned_delete_index, monkeypatch+):+ """An index failure must not turn a successful on-disk delete into a failure."""+ service, index, doomed, _sibling = owned_delete_index++ def _boom(session_id: str) -> None:+ raise RuntimeError("index offline")++ monkeypatch.setattr(index, "remove_session", _boom, raising=False)++ assert service.delete_session(doomed.session_id) is True+ assert service.store.get_session(doomed.session_id) is None