fleet-memory/hindsight-api/hindsight_api/engine/search
Nicolò Boschi ad2cf72aab
perf: add GIN index on source_memory_ids for observation lookup (#485)
* perf: add GIN index on source_memory_ids for observation lookup

Addresses a 927x performance regression (45ms → 0.049ms) reported by a
user with ~77k observations. The array overlap operator (&&) on
source_memory_ids was doing a full sequential scan over all observations,
causing recall timeouts (57-64s) and slow user recall (18-27s avg).

The partial GIN index reduces consolidation recall from timeout to ~15s
and user recall to ~6s.

* fix: use pre-bounded memory_links for observation graph expansion

Replace raw unit_entities join in _expand_observations() with the same
memory_links entity graph used by non-observation fact types. The previous
approach joined unit_entities twice (seeds→entities→connected_sources),
which explodes at scale (30-70s at 100k observations). The LIMIT 500
workaround was non-deterministic and dropped valid results.

Using memory_links (pre-bounded to MAX_LINKS_PER_ENTITY=50 at retain time)
is algorithmically identical to the non-observation entity expansion and
keeps graph retrieval at ~2s p50 even at 100k observations.

Also fix migration down_revision (z1u2v3w4x5y6 → d2e3f4a5b6c7) and add
observation generation + fact-type filtering to the recall perf benchmark.
2026-03-05 10:15:40 +01:00
..
__init__.py chore: remove dead code (#356) 2026-02-12 14:44:00 +01:00
fusion.py fix: doc build and lint files (#34) 2025-12-16 13:49:09 +01:00
graph_retrieval.py fix: improve memory footprint of recall (#423) 2026-02-23 11:30:16 +01:00
link_expansion_retrieval.py perf: add GIN index on source_memory_ids for observation lookup (#485) 2026-03-05 10:15:40 +01:00
mpfp_retrieval.py fix: improve memory footprint of recall (#423) 2026-02-23 11:30:16 +01:00
reranking.py fix: improve tei client parameters (#137) 2026-01-09 11:31:22 +01:00
retrieval.py fix(performance): improve recall and retain performance on large banks (#469) 2026-03-03 13:35:22 +01:00
tags.py feat: add memory tags (#152) 2026-01-13 18:28:40 +01:00
temporal_extraction.py fix: doc build and lint files (#34) 2025-12-16 13:49:09 +01:00
think_utils.py fix(ci): resolve flaky test failures in api tests (#311) 2026-02-06 13:56:59 +01:00
trace.py chore: drop unused access_count column (#178) 2026-01-20 10:36:02 +01:00
tracer.py feat: revisit mental models, directives and reflections (#179) 2026-01-22 17:13:16 +01:00
types.py fix: improve memory footprint of recall (#423) 2026-02-23 11:30:16 +01:00