Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(memory): dispatch core text search to the async FTS5 adapter (#4365)

xiaoheiai4719-collab3b8dfa5e3ad7
diff --git a/headroom/memory/core.py b/headroom/memory/core.py
index 0b7131b54..862dc1762 100644
--- a/headroom/memory/core.py
+++ b/headroom/memory/core.py
@@ -13,7 +13,7 @@ from datetime import datetime
from typing import TYPE_CHECKING, Any
from headroom.memory.config import MemoryConfig
-from headroom.memory.factory import create_memory_system
+from headroom.memory.factory import _adapt_text_search, create_memory_system
from headroom.memory.models import Memory, ScopeLevel
from headroom.memory.ports import MemoryFilter, TextFilter, VectorFilter
@@ -95,6 +95,7 @@ class HierarchicalMemory:
self._store = store
self._vector_index = vector_index
self._text_index = text_index
+ self._search_text = _adapt_text_search(text_index)
self._embedder = embedder
self._cache = cache
self._config = config or MemoryConfig()
@@ -438,7 +439,7 @@ class HierarchicalMemory:
limit=limit,
)
- return await self._text_index.search(text_filter)
+ return await self._search_text(text_filter)
# =========================================================================
# Memory Updates
diff --git a/headroom/memory/factory.py b/headroom/memory/factory.py
index 249cfa116..869051964 100644
--- a/headroom/memory/factory.py
+++ b/headroom/memory/factory.py
@@ -9,6 +9,7 @@ from __future__ import annotations
import threading
from importlib.metadata import entry_points
+from inspect import iscoroutinefunction
from pathlib import Path
from typing import TYPE_CHECKING, Any
@@ -21,7 +22,17 @@ from headroom.memory.config import (
)
if TYPE_CHECKING:
- from headroom.memory.ports import Embedder, MemoryCache, MemoryStore, TextIndex, VectorIndex
+ from collections.abc import Awaitable, Callable
+
+ from headroom.memory.ports import (
+ Embedder,
+ MemoryCache,
+ MemoryStore,
+ TextFilter,
+ TextIndex,
+ TextSearchResult,
+ VectorIndex,
+ )
# Extension groups for memory backends registered via setuptools entry points.
@@ -349,6 +360,27 @@ def _create_text_index(config: MemoryConfig) -> TextIndex:
raise ValueError(f"Unknown text backend: {config.text_backend}")
+def _adapt_text_search(
+ text_index: TextIndex,
+) -> Callable[[TextFilter], Awaitable[list[TextSearchResult]]]:
+ """Adapt built-in legacy search without changing the supplied index.
+
+ FTS5 keeps its synchronous string API; external backends use the TextIndex
+ search contract even when they have an unrelated ``search_memories`` helper.
+ An FTS5 subclass with an async search override also uses that contract.
+ Look up the method when called so construction does not require search for
+ callers that only use indexing, and method replacements remain visible.
+ """
+ from headroom.memory.adapters.fts5 import FTS5TextIndex
+
+ def search(filter: TextFilter) -> Awaitable[list[TextSearchResult]]:
+ if isinstance(text_index, FTS5TextIndex) and not iscoroutinefunction(text_index.search):
+ return text_index.search_memories(filter)
+ return text_index.search(filter)
+
+ return search
+
+
def _create_cache(config: MemoryConfig) -> MemoryCache:
"""Create a memory cache.
diff --git a/tests/test_memory/test_core_text_search.py b/tests/test_memory/test_core_text_search.py
new file mode 100644
index 000000000..3c398a20e
--- /dev/null
+++ b/tests/test_memory/test_core_text_search.py
@@ -0,0 +1,251 @@
+"""Core text search must work with the factory's FTS5 and external adapters."""
+
+from types import SimpleNamespace
+
+import pytest
+
+from headroom.memory.adapters.fts5 import FTS5TextIndex
+from headroom.memory.adapters.sqlite import SQLiteMemoryStore
+from headroom.memory.config import MemoryConfig, TextBackend
+from headroom.memory.core import HierarchicalMemory
+from headroom.memory.factory import _create_text_index
+from headroom.memory.models import Memory
+from headroom.memory.ports import TextIndex, TextSearchResult
+
+
[email protected]("use_factory", [True, False], ids=["factory", "direct"])
+ ("query", "session_id", "limit", "expected_ids"),
+ [
+ ("Python", "first", 10, {"alice-first"}),
+ ("Python", None, 10, {"alice-first", "alice-second"}),
+ ("Python", None, 0, set()),
+ ("unmatched", None, 10, set()),
+ ],
+)
+async def test_text_search_uses_factory_fts5_adapter(
+ tmp_path, query, session_id, limit, expected_ids, use_factory
+):
+ path = tmp_path / "memory.db"
+ text_index = (
+ _create_text_index(MemoryConfig(db_path=path)) if use_factory else FTS5TextIndex(path)
+ )
+ store = SQLiteMemoryStore(path)
+ for user, session in [("alice", "first"), ("alice", "second"), ("bob", "first")]:
+ memory = Memory(
+ id=f"{user}-{session}",
+ content="Python programming preference",
+ user_id=user,
+ session_id=session,
+ )
+ await store.save(memory)
+ await text_index.index_memory(memory)
+ # Vector search and embeddings are not involved in a text-only request.
+ system = HierarchicalMemory(store, None, text_index, None)
+ assert system.text_index is text_index
+
+ results = await system.text_search(query, user_id="alice", session_id=session_id, limit=limit)
+
+ assert {result.memory.id for result in results} == expected_ids
+ assert all(isinstance(result, TextSearchResult) for result in results)
+ assert all(result.memory.user_id == "alice" and result.score > 0 for result in results)
+ assert [result.rank for result in results] == list(range(1, len(results) + 1))
+ # Keep the pre-existing synchronous string-search API working too.
+ assert len(text_index.search("Python", k=10)) == 3
+
+
[email protected]("extra_helper", ["absent", "sync", "async", "noncallable"])
+async def test_text_search_preserves_external_text_index_protocol(extra_helper):
+ result = TextSearchResult(
+ memory=Memory(content="external result", user_id="alice"), score=1.0, rank=1
+ )
+
+ class ExternalTextIndex:
+ async def index(self, memory):
+ pass
+
+ async def index_batch(self, memories):
+ return len(memories)
+
+ async def remove(self, memory_id):
+ return False
+
+ async def remove_batch(self, memory_ids):
+ return 0
+
+ async def update_content(self, memory_id, content):
+ return False
+
+ async def search(self, filter):
+ self.seen_filter = filter
+ return [result]
+
+ text_index = ExternalTextIndex()
+ # Extra names are not part of TextIndex and must not affect dispatch.
+ if extra_helper == "sync":
+ text_index.search_memories = lambda query: ["unrelated helper"]
+ elif extra_helper == "async":
+
+ async def unrelated_helper(query):
+ return []
+
+ text_index.search_memories = unrelated_helper
+ elif extra_helper == "noncallable":
+ text_index.search_memories = None
+ assert isinstance(text_index, TextIndex)
+ system = HierarchicalMemory(None, None, text_index, None)
+
+ assert await system.text_search("Python", "alice", "session", 7) == [result]
+ assert text_index.seen_filter.query == "Python"
+ assert text_index.seen_filter.user_id == "alice"
+ assert text_index.seen_filter.session_id == "session"
+ assert text_index.seen_filter.limit == 7
+
+
+async def test_text_search_keeps_direct_fts5_indexing_and_lifecycle(tmp_path):
+ class ManagedFTS5TextIndex(FTS5TextIndex):
+ indexed_ids = None
+ closed = False
+
+ async def index_memory(self, memory):
+ self.indexed_ids = [memory.id]
+ await super().index_memory(memory)
+
+ def search(self, query, k=10, filter=None):
+ return super().search(query, k=k, filter=filter)
+
+ async def close(self):
+ self.closed = True
+
+ path = tmp_path / "memory.db"
+ text_index = ManagedFTS5TextIndex(path)
+ store = SQLiteMemoryStore(path)
+ async with HierarchicalMemory(store, None, text_index, None) as system:
+ memory = await system.add("Python preference", "alice", auto_embed=False, auto_bubble=False)
+ assert system.text_index is text_index
+ assert text_index.indexed_ids == [memory.id]
+ assert [result.memory.id for result in await system.text_search("Python", "alice")] == [
+ memory.id
+ ]
+ assert [result.memory_id for result in text_index.search("Python")] == [memory.id]
+ assert not text_index.closed
+ assert text_index.closed
+
+
[email protected]("use_fts5", [True, False], ids=["fts5", "external"])
+async def test_text_search_observes_replaced_adapter_method(tmp_path, monkeypatch, use_fts5):
+ if use_fts5:
+ text_index = FTS5TextIndex(tmp_path / "memory.db")
+ method_name = "search_memories"
+ else:
+
+ class ExternalTextIndex:
+ async def search(self, filter):
+ raise AssertionError("the replaced method must not be called")
+
+ text_index = ExternalTextIndex()
+ method_name = "search"
+ system = HierarchicalMemory(None, None, text_index, None)
+ seen = []
+
+ async def replacement(filter):
+ seen.append(filter.query)
+ return []
+
+ monkeypatch.setattr(text_index, method_name, replacement)
+ assert await system.text_search("Python") == []
+ assert seen == ["Python"]
+
+
[email protected]("use_factory", [True, False], ids=["external-factory", "direct"])
+async def test_text_search_preserves_async_fts5_subclass(tmp_path, monkeypatch, use_factory):
+ class ProtocolFTS5TextIndex(FTS5TextIndex):
+ async def search(self, filter):
+ self.seen_filter = filter
+ matches = super().search(filter.query, k=filter.limit, filter=filter)
+ return [
+ TextSearchResult(
+ memory=Memory(id=match.memory_id, content=match.content, user_id="alice"),
+ score=match.score,
+ rank=rank,
+ )
+ for rank, match in enumerate(matches, start=1)
+ ]
+
+ path = tmp_path / "memory.db"
+ if use_factory:
+ entry_point = SimpleNamespace(
+ name="protocol-fts5", load=lambda: lambda config: ProtocolFTS5TextIndex(config.db_path)
+ )
+ monkeypatch.setattr(
+ "headroom.memory.factory.entry_points",
+ lambda *, group: [entry_point] if group == "headroom.memory_text" else [],
+ )
+ text_index = _create_text_index(
+ MemoryConfig(
+ db_path=path, text_backend=TextBackend.EXTERNAL, text_backend_name="protocol-fts5"
+ )
+ )
+ else:
+ text_index = ProtocolFTS5TextIndex(path)
+ memory = Memory(content="Python preference", user_id="alice", session_id="session")
+ await text_index.index_memory(memory)
+ system = HierarchicalMemory(None, None, text_index, None)
+
+ results = await system.text_search("Python", "alice", "session", 7)
+
+ assert system.text_index is text_index
+ assert [result.memory.id for result in results] == [memory.id]
+ assert text_index.seen_filter.query == "Python"
+ assert text_index.seen_filter.user_id == "alice"
+ assert text_index.seen_filter.session_id == "session"
+ assert text_index.seen_filter.limit == 7
+
+
+async def test_text_search_observes_fts5_search_api_replacement(tmp_path, monkeypatch):
+ text_index = FTS5TextIndex(tmp_path / "memory.db")
+ memory = Memory(content="Python preference", user_id="alice")
+ await text_index.index_memory(memory)
+ system = HierarchicalMemory(None, None, text_index, None)
+ original_search = text_index.search
+ seen = []
+
+ async def protocol_search(filter):
+ seen.append(filter.query)
+ return []
+
+ monkeypatch.setattr(text_index, "search", protocol_search)
+ assert await system.text_search("Python", "alice") == []
+ assert seen == ["Python"]
+
+ monkeypatch.setattr(text_index, "search", original_search)
+ assert [result.memory.id for result in await system.text_search("Python", "alice")] == [
+ memory.id
+ ]
+
+
+async def test_text_search_is_not_required_until_called(monkeypatch):
+ class IndexOnly:
+ async def index_memory(self, memory):
+ self.indexed_id = memory.id
+
+ text_index = IndexOnly()
+ system = HierarchicalMemory(None, None, text_index, None)
+ memory = Memory(content="Python preference", user_id="alice")
+ await system.text_index.index_memory(memory)
+ assert text_index.indexed_id == memory.id
+ with pytest.raises(AttributeError, match="search"):
+ await system.text_search("Python")
+
+ async def protocol_search(filter):
+ return []
+
+ monkeypatch.setattr(text_index, "search", protocol_search, raising=False)
+ assert await system.text_search("Python") == []