Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(memory): retain current FTS search results

Lanre Shittu14f218d2dd45
diff --git a/agent/src/memory/persistent.py b/agent/src/memory/persistent.py
index 6529b48b3..1af6c7705 100644
--- a/agent/src/memory/persistent.py
+++ b/agent/src/memory/persistent.py
@@ -370,7 +370,11 @@ class PersistentMemory:
try:
from src.memory.search_index import get_shared_index
index = get_shared_index()
- matches = index.search(query, max_results=max_results)
+ # The on-disk memory files are canonical. The FTS database can
+ # briefly retain rows for files removed outside this process,
+ # so fetch a small candidate buffer before discarding rows that
+ # no longer map to a current entry.
+ matches = index.search(query, max_results=max_results * 4)
# Auto-rebuild on first empty search if entries exist on disk
all_entries = None
diff --git a/agent/tests/memory/test_tier2_integration.py b/agent/tests/memory/test_tier2_integration.py
index 832e9f828..64b61b12a 100644
--- a/agent/tests/memory/test_tier2_integration.py
+++ b/agent/tests/memory/test_tier2_integration.py
@@ -193,6 +193,36 @@ class TestFtsAddThenSearchFinds:
assert len(results) >= 1
assert any("quant" in e.title.lower() for e in results)
+ def test_fts_stale_rows_do_not_displace_current_memories(self, tmp_path, monkeypatch, fts_db):
+ """Discarding a stale FTS row must not shrink the requested result set."""
+ monkeypatch.setenv("VT_MEMORY_FTS_INDEX", "false")
+ reset_env_config()
+
+ mem = PersistentMemory(tmp_path)
+ mem.add("first recall", "evidence retrieval", "project")
+ mem.add("second recall", "evidence retrieval", "project")
+ entries = {entry.title: entry for entry in mem.list_entries()}
+
+ from src.memory.search_index import MemoryMatch
+ import src.memory.search_index as search_index
+
+ class _Index:
+ def search(self, query, max_results):
+ candidates = [
+ MemoryMatch("stale0", "removed memory", "", -10.0),
+ MemoryMatch(entries["first recall"].id, "first recall", "", -9.0),
+ MemoryMatch(entries["second recall"].id, "second recall", "", -8.0),
+ ]
+ return candidates[:max_results]
+
+ monkeypatch.setattr(search_index, "get_shared_index", lambda: _Index())
+ monkeypatch.setenv("VT_MEMORY_FTS_INDEX", "true")
+ reset_env_config()
+
+ results = mem.find_relevant("evidence retrieval", max_results=2)
+
+ assert [entry.title for entry in results] == ["first recall", "second recall"]
+
# ---------------------------------------------------------------------------
# 4a. Fallback token-scan must find the same short-token content the FTS5