"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(session): apply search limits to distinct conversations
Rudy Celeklifa016870b5ec
diff --git a/agent/src/session/search.py b/agent/src/session/search.pyindex b1ca0e457..98bb74986 100644--- a/agent/src/session/search.py+++ b/agent/src/session/search.py@@ -19,7 +19,6 @@ import timefrom dataclasses import dataclassfrom datetime import datetimefrom pathlib import Path-from typing import List, Optionalfrom src.config.paths import get_runtime_root@@ -41,7 +40,11 @@ def _prepare_search_text(text: str) -> str:def _search_snippet(content: str, query: str) -> str:"""Highlight query terms in the original text, never in expanded tokens."""terms = sorted(set(_QUERY_TERM.findall(query)), key=len, reverse=True)- pattern = re.compile("|".join(re.escape(term) for term in terms), re.IGNORECASE) if terms else None+ pattern = (+ re.compile("|".join(re.escape(term) for term in terms), re.IGNORECASE)+ if terms+ else None+ )first = pattern.search(content) if pattern else Nonestart = max(0, first.start() - 100) if first else 0end = min(len(content), start + 400)@@ -99,7 +102,7 @@ class SessionSearchIndex:"""self.db_path = db_pathself.db_path.parent.mkdir(parents=True, exist_ok=True)- self._conn: Optional[sqlite3.Connection] = None+ self._conn: sqlite3.Connection | None = Noneself._init_db()def _get_conn(self) -> sqlite3.Connection:@@ -135,7 +138,9 @@ class SessionSearchIndex:""")# Serialize upgrade across processes. Keep original messages for readable# snippets; rebuild the derived index once, including historical rows.- conn.create_function("prepare_session_text", 1, _prepare_search_text, deterministic=True)+ conn.create_function(+ "prepare_session_text", 1, _prepare_search_text, deterministic=True+ )with conn:conn.execute("BEGIN IMMEDIATE")columns = {row[1] for row in conn.execute("PRAGMA table_info(messages)")}@@ -143,28 +148,38 @@ class SessionSearchIndex:if migrating:for name in ("messages_ai", "messages_ad", "messages_au"):conn.execute(f"DROP TRIGGER IF EXISTS {name}")- conn.execute("ALTER TABLE messages ADD COLUMN search_content TEXT NOT NULL DEFAULT ''")- conn.execute("UPDATE messages SET search_content = prepare_session_text(content)")+ conn.execute(+ "ALTER TABLE messages ADD COLUMN search_content TEXT NOT NULL DEFAULT ''"+ )+ conn.execute(+ "UPDATE messages SET search_content = prepare_session_text(content)"+ )conn.execute("DROP TABLE IF EXISTS messages_fts")conn.execute("""CREATE VIRTUAL TABLE IF NOT EXISTS messages_ftsUSING fts5(search_content, content=messages, content_rowid=id)""")- conn.execute("""CREATE TRIGGER IF NOT EXISTS messages_ai AFTER INSERT ON messages BEGIN+ conn.execute(+ """CREATE TRIGGER IF NOT EXISTS messages_ai AFTER INSERT ON messages BEGININSERT INTO messages_fts(rowid, search_content) VALUES (new.id, new.search_content);- END""")- conn.execute("""CREATE TRIGGER IF NOT EXISTS messages_ad AFTER DELETE ON messages BEGIN+ END"""+ )+ conn.execute(+ """CREATE TRIGGER IF NOT EXISTS messages_ad AFTER DELETE ON messages BEGININSERT INTO messages_fts(messages_fts, rowid, search_content)VALUES ('delete', old.id, old.search_content);- END""")+ END"""+ )if migrating:- conn.execute("INSERT INTO messages_fts(messages_fts) VALUES ('rebuild')")+ conn.execute(+ "INSERT INTO messages_fts(messages_fts) VALUES ('rebuild')"+ )def index_session(self,session_id: str,title: str = "",- ts: Optional[float] = None,+ ts: float | None = None,) -> None:"""Upsert a session record.@@ -193,8 +208,9 @@ class SessionSearchIndex:)conn.commit()- def index_message(self, session_id: str, role: str, content: str,- tool_name: Optional[str] = None) -> None:+ def index_message(+ self, session_id: str, role: str, content: str, tool_name: str | None = None+ ) -> None:"""Index a single message.Args:@@ -234,10 +250,12 @@ class SessionSearchIndex:Returns:FTS5-safe MATCH expression."""- tokens = [_prepare_search_text(term).strip() for term in _QUERY_TERM.findall(query)]+ tokens = [+ _prepare_search_text(term).strip() for term in _QUERY_TERM.findall(query)+ ]return " OR ".join(f'"{token}"' for token in tokens) if tokens else '""'- def search(self, query: str, max_sessions: int = 3) -> List[SearchMatch]:+ def search(self, query: str, max_sessions: int = 3) -> list[SearchMatch]:"""Full-text search across all sessions.Args:@@ -264,16 +282,15 @@ class SessionSearchIndex:JOIN sessions s ON s.id = m.session_idWHERE messages_fts MATCH ?ORDER BY rank- LIMIT ?""",- (fts_query, max_sessions * 5),+ (fts_query,),)except sqlite3.OperationalError as exc:logger.warning("FTS5 search failed: %s", exc)return []seen: dict[str, SearchMatch] = {}- for row in cursor.fetchall():+ for row in cursor:sid = row[0]if sid in seen:continue@@ -328,14 +345,18 @@ class SessionSearchIndex:title = session_data.get("title", "")try:- ts = datetime.fromisoformat(session_data.get("created_at", "")).timestamp()+ ts = datetime.fromisoformat(+ session_data.get("created_at", "")+ ).timestamp()except (ValueError, TypeError):ts = time.time()self.index_session(sid, title, ts=ts)if messages_file.exists():- for line in messages_file.read_text(encoding="utf-8").strip().splitlines():+ for line in (+ messages_file.read_text(encoding="utf-8").strip().splitlines()+ ):if not line.strip():continuetry:@@ -347,7 +368,8 @@ class SessionSearchIndex:count += 1except json.JSONDecodeError:continue- except Exception as exc:+ # One corrupt session must not abort the remaining reindex.+ except Exception as exc: # noqa: BLE001logger.warning("Failed to index session %s: %s", session_dir.name, exc)return count@@ -367,7 +389,7 @@ class SessionSearchIndex:self._conn = None-_shared_index: Optional[SessionSearchIndex] = None+_shared_index: SessionSearchIndex | None = None_shared_lock = _threading.Lock()diff --git a/agent/tests/test_session_search_distinct_limit.py b/agent/tests/test_session_search_distinct_limit.pynew file mode 100644index 000000000..ea0a2c643--- /dev/null+++ b/agent/tests/test_session_search_distinct_limit.py@@ -0,0 +1,75 @@+"""Session search limits sessions rather than an approximate message sample."""++import json+from pathlib import Path++from src.session.search import SessionSearchIndex+from src.tools.session_search_tool import SessionSearchTool+++def _populate(index: SessionSearchIndex) -> None:+ for session_id, count in [("long", 25), ("second", 1), ("third", 1)]:+ index.index_session(session_id, session_id, ts=1_700_000_000)+ for _ in range(count):+ index.index_message(session_id, "user", "portfolio volatility")+++def test_actual_tool_returns_distinct_sessions_after_long_conversation(+ tmp_path: Path, monkeypatch+) -> None:+ index = SessionSearchIndex(tmp_path / "sessions.db")+ try:+ _populate(index)+ monkeypatch.setattr("src.session.search.get_shared_index", lambda: index)+ result = json.loads(+ SessionSearchTool().execute(query="volatility", max_results=3)+ )+ assert result["status"] == "ok"+ assert {row["session_id"] for row in result["results"]} == {+ "long",+ "second",+ "third",+ }+ assert len(result["results"]) == 3+ assert all(">>>volatility<<<" in row["snippet"] for row in result["results"])+ assert len(index.search("volatility", max_sessions=1)) == 1+ finally:+ index.close()+++def test_ranked_cursor_stops_after_requested_distinct_sessions(tmp_path: Path) -> None:+ index = SessionSearchIndex(tmp_path / "sessions.db")+ try:+ for i in range(30):+ index.index_session(str(i), str(i))+ index.index_message(str(i), "user", "volatility")+ real = index._get_conn()+ consumed = []++ class Cursor:+ def __iter__(self):+ for row in self.cursor:+ consumed.append(row)+ yield row++ def fetchall(self):+ raise AssertionError("Search must not materialize every message")++ class Connection:+ def execute(self, sql, params):+ wrapper = Cursor()+ wrapper.cursor = real.execute(sql, params)+ return wrapper++ index._conn = Connection()+ matches = index.search("volatility", max_sessions=3)+ index._conn = real+ assert len(matches) == 3+ assert len(consumed) == 3+ assert [match.rank for match in matches] == sorted(+ match.rank for match in matches+ )+ finally:+ if index._conn is not real:+ index._conn = real+ index.close()