Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(memory): align fallback tokenizer minimum with FTS5 sanitizer

Lanre Shittufdb4cdd95189
diff --git a/agent/src/memory/compression.py b/agent/src/memory/compression.py
index 37ec4fccc..6bd3b14de 100644
--- a/agent/src/memory/compression.py
+++ b/agent/src/memory/compression.py
@@ -60,8 +60,11 @@ _SENTENCE_SPLIT_RE = re.compile(r"(?<=[.!?。!?])\s+|\n+")
def _tokenize_for_tfidf(text: str) -> List[str]:
"""Tokenize text for TF-IDF scoring.
- Uses same regex pattern as persistent.py: ASCII words >= 3 chars
- or individual non-Latin script characters.
+ ASCII words >= 3 chars or individual non-Latin script characters.
+ Deliberately a higher minimum than persistent.py's retrieval tokenizer:
+ this only ranks sentences for summarization, not retrieval, so dropping
+ short tokens from scoring doesn't create the silent-miss failure mode
+ that a stricter retrieval tokenizer would.
"""
return _TOKEN_RE.findall(text.lower())
diff --git a/agent/src/memory/persistent.py b/agent/src/memory/persistent.py
index 4b6af3232..6496fe180 100644
--- a/agent/src/memory/persistent.py
+++ b/agent/src/memory/persistent.py
@@ -115,7 +115,11 @@ _NON_LATIN_SCRIPT_RANGES = (
"Ѐ-ӿ" # Cyrillic
)
-_TOKEN_RE = re.compile(rf"[a-zA-Z0-9]{{3,}}|[{_NON_LATIN_SCRIPT_RANGES}]")
+# Matches search_index.py's FTS5 query sanitizer minimum (2+ ASCII chars) so
+# the token-scan fallback in find_relevant() finds the same content the FTS5
+# path does — short tokens like ticker symbols ("GE") or country codes ("US")
+# must be retrievable regardless of whether VT_MEMORY_FTS_INDEX is enabled.
+_TOKEN_RE = re.compile(rf"[a-zA-Z0-9]{{2,}}|[{_NON_LATIN_SCRIPT_RANGES}]")
_SLUG_DISALLOWED_RE = re.compile(rf"[^a-z0-9_\-{_NON_LATIN_SCRIPT_RANGES}]")
@@ -143,7 +147,7 @@ class MemoryEntry:
def _tokenize(text: str) -> set[str]:
- """Split text into searchable tokens (ASCII >=3 chars + non-Latin chars)."""
+ """Split text into searchable tokens (ASCII >=2 chars + non-Latin chars)."""
return set(_TOKEN_RE.findall(text.lower()))
diff --git a/agent/tests/memory/test_tier2_integration.py b/agent/tests/memory/test_tier2_integration.py
index 82cb00141..832e9f828 100644
--- a/agent/tests/memory/test_tier2_integration.py
+++ b/agent/tests/memory/test_tier2_integration.py
@@ -194,6 +194,52 @@ class TestFtsAddThenSearchFinds:
assert any("quant" in e.title.lower() for e in results)
+# ---------------------------------------------------------------------------
+# 4a. Fallback token-scan must find the same short-token content the FTS5
+# path does — find_relevant() must not silently miss entries just because
+# VT_MEMORY_FTS_INDEX happens to be off.
+# ---------------------------------------------------------------------------
+
+
+class TestFallbackMatchesFtsForShortTokens:
+ """A 2-character query (e.g. a ticker symbol or country code) must be
+ retrievable through the token-scan fallback exactly as it is through the
+ FTS5 index, regardless of which VT_MEMORY_FTS_INDEX setting is active."""
+
+ def test_fallback_finds_short_token_entry(self, tmp_path, monkeypatch):
+ monkeypatch.setenv("VT_MEMORY_FTS_INDEX", "false")
+ reset_env_config()
+
+ mem = PersistentMemory(tmp_path)
+ mem.add(
+ "GE earnings note",
+ "GE reported strong earnings this quarter, beating estimates.",
+ "project",
+ )
+
+ results = mem.find_relevant("GE")
+ assert len(results) == 1
+ assert results[0].title == "GE earnings note"
+
+ def test_fallback_and_fts_agree_on_short_token_query(self, tmp_path, monkeypatch, fts_db):
+ mem = PersistentMemory(tmp_path)
+ mem.add(
+ "GE earnings note",
+ "GE reported strong earnings this quarter, beating estimates.",
+ "project",
+ )
+
+ monkeypatch.setenv("VT_MEMORY_FTS_INDEX", "false")
+ reset_env_config()
+ fallback_titles = {e.title for e in mem.find_relevant("GE")}
+
+ monkeypatch.setenv("VT_MEMORY_FTS_INDEX", "true")
+ reset_env_config()
+ fts_titles = {e.title for e in mem.find_relevant("GE")}
+
+ assert fallback_titles == fts_titles == {"GE earnings note"}
+
+
# ---------------------------------------------------------------------------
# 4b. FTS: importance decay must still influence ranking (VT_MEMORY=full
# enables fts_index_enabled and decay_enabled together)
diff --git a/agent/tests/test_persistent_memory.py b/agent/tests/test_persistent_memory.py
index 6df9ba5c2..160f960cc 100644
--- a/agent/tests/test_persistent_memory.py
+++ b/agent/tests/test_persistent_memory.py
@@ -63,11 +63,19 @@ class TestTokenize:
assert "world" in tokens
assert "testing" in tokens
- def test_short_words_excluded(self) -> None:
- tokens = _tokenize("I am ok no")
- # All < 3 chars, should be excluded
+ def test_single_char_words_excluded(self) -> None:
+ tokens = _tokenize("I a")
+ # Single ASCII chars carry no discriminating value, should be excluded
assert len(tokens) == 0
+ def test_two_char_words_included(self) -> None:
+ # Regression: the fallback token-scan used a 3-char minimum while
+ # search_index.py's FTS5 sanitizer used 2 chars, so short tokens like
+ # ticker symbols ("GE") or country codes ("US") were retrievable via
+ # FTS but silently invisible whenever VT_MEMORY_FTS_INDEX was off.
+ tokens = _tokenize("GE US ok no")
+ assert tokens == {"ge", "us", "ok", "no"}
+
def test_cjk_characters(self) -> None:
tokens = _tokenize("比特币价格分析")
assert "比" in tokens