"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(memory): apply importance decay to FTS5 ranking
Lanre Shittu454364ebfa40
diff --git a/agent/src/memory/persistent.py b/agent/src/memory/persistent.pyindex c083f8c34..4b6af3232 100644--- a/agent/src/memory/persistent.py+++ b/agent/src/memory/persistent.py@@ -386,8 +386,21 @@ class PersistentMemory:if all_entries is None:all_entries = self._scan_entries()entry_map = {e.id: e for e in all_entries}- fts_results = [entry_map[m.entry_id] for m in matches if m.entry_id in entry_map]- if fts_results:+ fts_pairs = [+ (m, entry_map[m.entry_id]) for m in matches if m.entry_id in entry_map+ ]+ if fts_pairs:+ if _is_decay_enabled():+ # FTS5's rank is more negative for stronger matches;+ # negate it onto the same scale the token-scan+ # fallback below uses and apply the same importance+ # weighting, so decay isn't silently inert whenever+ # FTS produces a result.+ fts_pairs.sort(+ key=lambda pair: -pair[0].rank * (0.5 + 0.5 * pair[1].importance),+ reverse=True,+ )+ fts_results = [entry for _, entry in fts_pairs]return fts_results[:max_results]except Exception:logger.debug("FTS5 search failed, falling back to scan", exc_info=True)diff --git a/agent/tests/memory/test_tier2_integration.py b/agent/tests/memory/test_tier2_integration.pyindex c4240cf3d..82cb00141 100644--- a/agent/tests/memory/test_tier2_integration.py+++ b/agent/tests/memory/test_tier2_integration.py@@ -10,6 +10,7 @@ from __future__ import annotationsimport jsonimport time+from datetime import datetime, timedeltafrom pathlib import Pathimport pytest@@ -193,6 +194,112 @@ class TestFtsAddThenSearchFinds:assert any("quant" in e.title.lower() for e in results)+# ---------------------------------------------------------------------------+# 4b. FTS: importance decay must still influence ranking (VT_MEMORY=full+# enables fts_index_enabled and decay_enabled together)+# ---------------------------------------------------------------------------+++class TestFtsRankingRespectsImportanceDecay:+ """FTS5 candidate retrieval must not silently bypass importance-decay+ weighting when both features are enabled together, the exact combination+ the documented VT_MEMORY=full preset turns on."""++ @staticmethod+ def _write_entry(tmp_path, filename, name, quality_score, days_ago, access_count, body, entry_id):+ last_accessed = (datetime.now() - timedelta(days=days_ago)).isoformat()+ created = datetime.now().isoformat()+ path = tmp_path / filename+ path.write_text(+ "---\n"+ f"name: {name}\n"+ f"description: {name}\n"+ "type: project\n"+ f"id: {entry_id}\n"+ f"created_at: {created}\n"+ f"updated_at: {created}\n"+ "keywords: []\n"+ f"quality_score: {quality_score}\n"+ f"access_count: {access_count}\n"+ f"last_accessed: {last_accessed}\n"+ "importance: 0.5\n"+ "related_memories: []\n"+ "---\n\n"+ f"{body}\n",+ encoding="utf-8",+ )+ return path++ def _seed_entries(self, tmp_path):+ # Fresh, heavily-accessed entry: high decayed importance, weaker+ # lexical match (query term appears once, diluted by other words).+ self._write_entry(+ tmp_path, "project_fresh.md", "fresh important entry",+ quality_score=0.95, days_ago=0, access_count=50,+ body="trading context and other unrelated filler words here",+ entry_id="fresh1",+ )+ # Ancient, never-accessed entry: near-zero decayed importance,+ # stronger lexical match (query term repeated in a short body).+ self._write_entry(+ tmp_path, "project_stale.md", "stale unimportant entry",+ quality_score=0.05, days_ago=400, access_count=0,+ body="trading trading focus notes only",+ entry_id="stale1",+ )++ def test_fts_ranking_reordered_by_importance_when_decay_enabled(self, tmp_path, monkeypatch, fts_db):+ """With FTS and decay both on, an entry with much higher decayed+ importance should outrank a purely stronger lexical match, not just+ get returned somewhere in the result set."""+ monkeypatch.setenv("VT_MEMORY_FTS_INDEX", "true")+ monkeypatch.setenv("VT_MEMORY_DECAY", "true")+ reset_env_config()++ self._seed_entries(tmp_path)+ mem = PersistentMemory(tmp_path)+ entries = mem._scan_entries()++ # Sanity check: the importance gap this test relies on is real.+ importance = {e.title: e.importance for e in entries}+ assert importance["fresh important entry"] > 0.9+ assert importance["stale unimportant entry"] < 0.01++ from src.memory.search_index import get_shared_index+ get_shared_index().rebuild_all(+ [(e.id, e.title, e.description, "", e.body) for e in entries]+ )++ results = mem.find_relevant("trading")+ titles = [r.title for r in results]+ assert titles[0] == "fresh important entry", (+ "decayed importance should outrank a purely stronger lexical match "+ f"once FTS and decay are combined, got order: {titles}"+ )++ def test_fts_ranking_unchanged_when_decay_disabled(self, tmp_path, monkeypatch, fts_db):+ """Control: with decay off, FTS ordering is untouched by this fix,+ pure lexical relevance still wins, exactly as before the change."""+ monkeypatch.setenv("VT_MEMORY_FTS_INDEX", "true")+ monkeypatch.setenv("VT_MEMORY_DECAY", "false")+ reset_env_config()++ self._seed_entries(tmp_path)+ mem = PersistentMemory(tmp_path)+ entries = mem._scan_entries()++ from src.memory.search_index import get_shared_index+ get_shared_index().rebuild_all(+ [(e.id, e.title, e.description, "", e.body) for e in entries]+ )++ results = mem.find_relevant("trading")+ titles = [r.title for r in results]+ assert titles[0] == "stale unimportant entry", (+ f"with decay disabled, pure FTS lexical relevance should win, got order: {titles}"+ )++# ---------------------------------------------------------------------------# 5. FTS: remove cleans index# ---------------------------------------------------------------------------