mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(memory): replace the HNSW vector when a memory is re-indexed
HNSWVectorIndex.index() on an existing id, update_embedding() and the update branch of index_batch() only cached the new embedding; the hnswlib graph kept the old vector. HierarchicalMemory.update, refresh and detach re-index under the same id, so on the HNSW backend an updated memory was found by its old content and not by its new one. hnswlib's add_items with an existing label replaces that element's vector (addPoint -> updatePoint), so call it with the existing label in all three paths. Ids, mappings and size are unchanged. Refs #4362 (item 5).
This commit is contained in:
@@ -494,11 +494,15 @@ class HNSWVectorIndex:
|
||||
self._metadata[memory.id] = IndexedMemoryMetadata.from_memory(memory)
|
||||
self._embeddings[memory.id] = embedding.copy()
|
||||
|
||||
# Handle updates (hnswlib doesn't support true updates, so we track separately)
|
||||
# Updates: add_items with an existing label replaces its vector.
|
||||
if update_memories:
|
||||
self._index.add_items(
|
||||
np.vstack([emb for _, emb in update_memories]).astype(np.float32),
|
||||
np.array([self._memory_to_hnsw[m.id] for m, _ in update_memories]),
|
||||
)
|
||||
for memory, embedding in update_memories:
|
||||
self._metadata[memory.id] = IndexedMemoryMetadata.from_memory(memory)
|
||||
self._embeddings[memory.id] = embedding.copy()
|
||||
# Note: HNSW embedding stays unchanged unless we remove and re-add
|
||||
|
||||
if self._auto_save and self._save_path:
|
||||
self.save_index(self._save_path)
|
||||
@@ -748,9 +752,7 @@ class HNSWVectorIndex:
|
||||
async def update_embedding(self, memory_id: str, embedding: np.ndarray) -> bool:
|
||||
"""Update the embedding for an indexed memory.
|
||||
|
||||
Note: hnswlib doesn't support in-place updates. This stores the new
|
||||
embedding but the HNSW graph uses the original. For full update,
|
||||
remove and re-index the memory.
|
||||
The new vector replaces the old one in the HNSW graph, so search uses it.
|
||||
|
||||
Args:
|
||||
memory_id: The unique identifier of the memory.
|
||||
@@ -777,15 +779,15 @@ class HNSWVectorIndex:
|
||||
async def _update_embedding_internal(self, memory_id: str, embedding: np.ndarray) -> bool:
|
||||
"""Internal embedding update without lock (caller must hold lock).
|
||||
|
||||
hnswlib doesn't support true in-place updates, so we:
|
||||
1. Store the new embedding in our local cache
|
||||
2. The HNSW index continues to use the old embedding for search
|
||||
|
||||
For a true update, the caller should remove and re-index.
|
||||
hnswlib's ``add_items`` with an existing label replaces that element's
|
||||
vector in place (``addPoint`` -> ``updatePoint``), so the label and the
|
||||
id mappings stay as they are and search sees the new vector.
|
||||
"""
|
||||
if memory_id not in self._memory_to_hnsw:
|
||||
hnsw_id = self._memory_to_hnsw.get(memory_id)
|
||||
if hnsw_id is None:
|
||||
return False
|
||||
|
||||
self._index.add_items(embedding.reshape(1, -1), np.array([hnsw_id]))
|
||||
self._embeddings[memory_id] = embedding.copy()
|
||||
return True
|
||||
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
"""Re-indexing a memory on the HNSW backend must move it to its new vector.
|
||||
|
||||
`HierarchicalMemory.update`, `refresh` and `detach` re-index a memory under the
|
||||
same id after its content (and so its embedding) changed. The HNSW adapter only
|
||||
cached the new embedding; the graph kept the old vector, so search still found
|
||||
the memory by its OLD content and not by the new one.
|
||||
See https://github.com/headroomlabs-ai/headroom/issues/4362 (item 5).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from headroom.memory.models import Memory
|
||||
from headroom.memory.ports import VectorFilter
|
||||
|
||||
try:
|
||||
from headroom.memory.adapters.hnsw import _check_hnswlib_available
|
||||
|
||||
HNSW_AVAILABLE = _check_hnswlib_available()
|
||||
except ImportError:
|
||||
HNSW_AVAILABLE = False
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.skipif(not HNSW_AVAILABLE, reason="hnswlib not installed"),
|
||||
pytest.mark.asyncio,
|
||||
]
|
||||
|
||||
DIM = 8
|
||||
OLD = np.eye(DIM, dtype=np.float32)[0]
|
||||
NEW = np.eye(DIM, dtype=np.float32)[1]
|
||||
|
||||
|
||||
def _index():
|
||||
from headroom.memory.adapters.hnsw import HNSWVectorIndex
|
||||
|
||||
return HNSWVectorIndex(dimension=DIM, max_elements=16)
|
||||
|
||||
|
||||
async def _top(index, vector: np.ndarray) -> tuple[str, float]:
|
||||
results = await index.search(VectorFilter(query_vector=vector, top_k=1))
|
||||
return results[0].memory.id, results[0].similarity
|
||||
|
||||
|
||||
async def _seed(index) -> Memory:
|
||||
memory = Memory(id="m1", content="old", user_id="u", embedding=OLD.copy())
|
||||
await index.index(memory)
|
||||
# A second memory so a stale vector cannot win by being the only entry.
|
||||
await index.index(
|
||||
Memory(id="m2", content="other", user_id="u", embedding=np.ones(DIM, np.float32))
|
||||
)
|
||||
return memory
|
||||
|
||||
|
||||
async def test_index_on_existing_id_replaces_the_vector() -> None:
|
||||
index = _index()
|
||||
memory = await _seed(index)
|
||||
|
||||
memory.embedding = NEW.copy()
|
||||
await index.index(memory)
|
||||
|
||||
assert index.size == 2
|
||||
found, similarity = await _top(index, NEW)
|
||||
assert found == "m1"
|
||||
assert similarity == pytest.approx(1.0, abs=1e-5)
|
||||
|
||||
|
||||
async def test_update_embedding_replaces_the_vector() -> None:
|
||||
index = _index()
|
||||
await _seed(index)
|
||||
|
||||
assert await index.update_embedding("m1", NEW.copy()) is True
|
||||
|
||||
found, similarity = await _top(index, NEW)
|
||||
assert found == "m1"
|
||||
assert similarity == pytest.approx(1.0, abs=1e-5)
|
||||
|
||||
|
||||
async def test_index_batch_on_existing_id_replaces_the_vector() -> None:
|
||||
index = _index()
|
||||
memory = await _seed(index)
|
||||
|
||||
memory.embedding = NEW.copy()
|
||||
assert await index.index_batch([memory]) == 1
|
||||
|
||||
assert index.size == 2
|
||||
found, similarity = await _top(index, NEW)
|
||||
assert found == "m1"
|
||||
assert similarity == pytest.approx(1.0, abs=1e-5)
|
||||
Reference in New Issue
Block a user