You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Vector search silently drops results when entity and relation search_index ids collide #982
Semantic/vector search silently omits notes from results when an entity row and a relation (or observation) row in search_index share the same numeric id. Because each row type has its own auto-increment sequence, collisions are essentially guaranteed in any young database (entity ids 1..N overlap relation ids 1..M), and the affected notes are simply never returned by vector or hybrid search — no error, no log.
Root cause
In src/basic_memory/repository/search_repository_base.py:
_search_vector_only parses each vector hit's chunk_key (e.g. entity:4:0) but discards the type, keeping only the bare id:
_, si_id=self._parse_chunk_key(chunk_key)
_fetch_search_index_rows_by_ids then fetches WHERE id IN (...) with no type filter and builds a dict keyed by bare id:
result[row.id] =SearchIndexRow(...)
When an entity row and a relation row share an id, whichever the database returns last overwrites the other in the dict. The clobbered row's similarity entry then finds row is None and is dropped.
Note the FTS-filter branch a few lines above already guards against exactly this with (id, type) tuple keys ("Use (id, type) tuples to avoid collisions between different search_index row types that share the same auto-increment id") — but the primary lookup path doesn't.
Concrete example (real database, basic-memory 0.21.6, SQLite backend)
search_index contained entity rows with ids 1–4 and relation rows with ids 1, 2, 4, 5:
entity 1 ("Learner Profile") — clobbered by relation 1 → never returned
entity 2 — its row happened to be inserted after relation 2 → returned
entity 3 — no colliding relation id 3 → returned
entity 4 (a concept note) — clobbered by relation 4 → never returned
A direct KNN query against search_vector_embeddings ranked entity 4's chunk #1 for the test query (distance 0.64), confirming the embeddings are fine — the result is lost purely in the id→row hydration step. Searching with min_similarity=0 and verbatim text from the affected notes still returns nothing for them.
Expected behavior
Every vector hit should hydrate against the search_index row of the same type. Keying similarity_by_si_id / chunks_by_si_id / _fetch_search_index_rows_by_ids by (type, id) tuples fixes it (verified locally on 0.21.6 — the dropped notes immediately return at the expected ranks).
Environment
basic-memory 0.21.6 (bug also present in v0.22.0 source and current main)
Renamed similarity_by_si_id / chunks_by_si_id → similarity_by_si_key / chunks_by_si_key and changed their key type from int to tuple[str, int]. Updated _fetch_search_index_rows_by_ids to accept list[tuple[str, int]] and key its return dict by (row.type, row.id). Entity id=4 and relation id=4 now live under distinct keys ("entity", 4) and ("relation", 4) — neither overwrites the other.
The SQL query is unchanged (WHERE id IN (...)); type discrimination is applied at Python dict-key construction time using row.type, which is both simpler and zero-overhead versus adding per-row SQL conditions.
tests/repository/test_vector_threshold.py
Updated all _fetch_search_index_rows_by_ids mock return values from {i: FakeRow(id=i)} to {("entity", i): FakeRow(id=i)}.
Added test_entity_and_relation_with_same_id_both_returned — direct regression test for the bug. Creates chunk keys entity:4:0 and relation:4:0, asserts both come back (before this fix only one did).
tests/repository/test_vector_pagination.py
Updated fake_index_rows mock to use ("entity", i) tuple keys.
Note to maintainer: The workflow token (github-actions[bot]) doesn't have write access on this run, so the commit can't be pushed. You can push claude/issue-982-20260612-0300 manually (git push origin claude/issue-982-20260612-0300) or re-trigger with the bm-claudeai app token.
Activity
phernandez commentedon Jun 12, 2026
@Funnypie thanks for submitting this issue!
@claude take a look at this one
claude commentedon Jun 12, 2026
Claude finished @phernandez's task in 9m 0s —— View job • Create PR ➔
Fixing vector search id collision bug
search_repository_base.py(type, id)tuple keys throughout the vector search pathpytestrequires tool approval in this run)4d37f59is ready locally; push blocked by 403 (github-actions[bot]token lacks write access on this run)Changes made
src/basic_memory/repository/search_repository_base.pyRenamed
similarity_by_si_id/chunks_by_si_id→similarity_by_si_key/chunks_by_si_keyand changed their key type frominttotuple[str, int]. Updated_fetch_search_index_rows_by_idsto acceptlist[tuple[str, int]]and key its return dict by(row.type, row.id). Entity id=4 and relation id=4 now live under distinct keys("entity", 4)and("relation", 4)— neither overwrites the other.The SQL query is unchanged (
WHERE id IN (...)); type discrimination is applied at Python dict-key construction time usingrow.type, which is both simpler and zero-overhead versus adding per-row SQL conditions.tests/repository/test_vector_threshold.py_fetch_search_index_rows_by_idsmock return values from{i: FakeRow(id=i)}to{("entity", i): FakeRow(id=i)}.test_entity_and_relation_with_same_id_both_returned— direct regression test for the bug. Creates chunk keysentity:4:0andrelation:4:0, asserts both come back (before this fix only one did).tests/repository/test_vector_pagination.pyfake_index_rowsmock to use("entity", i)tuple keys.fix(core): use (type, id) keys in vector search hydration to prevent …