Add fast raw memory search path
Replaces #189, which was closed accidentally and could not be reopened.
Closes #163
Summary
- expands
/v1/memory/searchinto a low-latency raw search path across profile, temporal, summary, snippet, and code domains - keeps answer synthesis optional via
answer=true - adds cached profile catalogs and cached raw retrieval plans
- returns per-mode latency timings plus rolling p50/p95/p99 stats
Follow-up after code-assist review
- guards latency sample writes/snapshots with a lock
- runs raw domain searches concurrently with
asyncio.gather - bounds profile catalog cache with TTL plus max-user eviction
Tests
pytest tests/api/test_dependencies_and_routes.py tests/unit/test_schemas.py -qpython3 -m ruff check src/api/schemas.py src/api/routes/memory.py src/pipelines/retrieval.py tests/api/test_dependencies_and_routes.py
Rebased this replacement PR on the latest main and resolved the conflict from the new durable memory job routes. The branch is mergeable again.
Latest local validation after rebase:
pytest tests/api/test_dependencies_and_routes.py tests/unit/test_schemas.py -q-> 8 passedpython3 -m ruff check src/api/schemas.py src/api/routes/memory.py src/pipelines/retrieval.py tests/api/test_dependencies_and_routes.py-> passed
Pushed follow-up commit 42b5f20 addressing the latest code-assist feedback:
- added locks around
RetrievalPipelineprofile catalog and raw retrieval plan caches - added type hints for
_timed - switched awaitable detection to
inspect.isawaitable - changed profile cache hit handling to pop/reinsert so bounded eviction behaves as LRU rather than FIFO
Validation:
pytest tests/api/test_dependencies_and_routes.py tests/unit/test_schemas.py -q-> 8 passedpython3 -m ruff check src/api/schemas.py src/api/routes/memory.py src/pipelines/retrieval.py tests/api/test_dependencies_and_routes.py-> passed
This PR expands Safe to merge for non-answer requests; when answer=true with the profile domain, every request issues two identical vector store queries — once via the concurrent raw path and again inside pipeline.run — undercutting the new cache entirely on that path. The concurrent gather, latency tracking, and schema changes are solid. The profile double-fetch when answer=True is a real efficiency defect introduced by this PR: _search_profile goes directly to the vector store while _fetch_profile_catalog (called inside pipeline.run) holds the TTL cache. Both hit the same query on every request, meaning the cache the PR advertises never fires for the raw profile path. The plan-cache key bug and the stampede window in _fetch_profile_catalog are additional correctness-adjacent issues that compound the concern. src/api/routes/memory.py (_search_profile bypasses cache) and src/pipelines/retrieval.py (raw_retrieval_plan key, _fetch_profile_catalog double-check)Greptile Summary
/v1/memory/search from a sequential 3-domain fetch into a concurrent 5-domain raw search path, adds optional LLM answer synthesis via answer=true, and introduces per-domain latency tracking with rolling p50/p95/p99 stats backed by process-level deques.
profile, temporal, summary, snippet, code) now run in parallel via asyncio.gather; sync domains are offloaded to threads with asyncio.to_thread._timed helper records per-domain elapsed time, which feeds into SearchResponse.latency_ms and latency_stats using a thread-safe locked deque per mode._fetch_profile_catalog gains a TTL+LRU cache (60 s, max 256 users) and raw_retrieval_plan adds a locked dict cache for normalized domain tuples — but the raw _search_profile path bypasses the catalog cache entirely, so the caching benefit is not realized for profile on the raw search path.Confidence Score: 3/5
Important Files Changed
Filename
Overview
src/api/routes/memory.py
Refactors
search_memory to run all domain searches concurrently via asyncio.gather, adds _timed helper for per-domain latency tracking, and adds snippet/code search paths; the raw profile search bypasses the new pipeline cache (causing double DB queries when answer=True).
src/pipelines/retrieval.py
Adds TTL+LRU cache for
_fetch_profile_catalog and a locked plan cache via raw_retrieval_plan; the plan cache key includes answer which never changes the returned value (wasted entries), and the profile cache write lacks a double-check that risks stampede overwrites.
src/api/schemas.py
Extends
SearchRequest with answer flag and deduplicated domains, adds SearchLatencySummary model, and expands SearchResponse with answer/confidence/latency fields; no issues found.
tests/api/test_dependencies_and_routes.py
Adds a complete
FakeRetrievalPipeline with all five domain search stubs and a new integration test covering raw hits, per-domain latency, and optional answer synthesis; coverage looks solid for the happy path.Sequence Diagram
sequenceDiagram
participant Client
participant Route as search_memory (route)
participant Plan as raw_retrieval_plan (cache)
participant Gather as asyncio.gather
participant VS as vector_store
participant Neo4j as neo4j
participant Pipeline as pipeline.run (answer path)
Client->>Route: POST /v1/memory/search
Route->>Plan: raw_retrieval_plan(domains, answer)
Plan-->>Route: normalized domain tuple
Route->>Gather: gather(_timed tasks for all domains)
par profile (thread)
Gather->>VS: search_by_metadata (profile)
VS-->>Gather: raw results
and temporal (thread)
Gather->>Neo4j: search_events_by_embedding
Neo4j-->>Gather: raw events
and summary/snippet/code (async)
Gather->>VS: search_by_text (summary / code)
Gather->>VS: _search_snippet (sandboxed ns)
VS-->>Gather: raw results
end
Gather-->>Route: all domain results + latency
opt "answer=true"
Route->>Pipeline: pipeline.run(query, user_id, top_k)
Note over Pipeline,VS: _fetch_profile_catalog checks cache,\nthen queries VS again (cache miss on first call)
Pipeline->>VS: search_by_metadata (profile) duplicate fetch
Pipeline-->>Route: answer + sources + confidence
end
Route-->>Client: SearchResponse(results, latency_ms, latency_stats, [answer])
Comments Outside Diff (1)
src/api/routes/memory.py, line 1041-1049 (link) Raw profile search bypasses the new cache on every request
_search_profile calls pipeline.vector_store.search_by_metadata directly, completely bypassing _fetch_profile_catalog's TTL cache that this PR added. When answer=True and "profile" is in the requested domains, both the raw concurrent task and the subsequent pipeline.run call independently hit the vector store with the identical query (filters={"user_id": user_id, "domain": "profile"}, top_k=100). That's two redundant round-trips per request, every time — the cache never benefits the raw path. The fix would be to have _search_profile call pipeline._fetch_profile_catalog(user_id) and extract results from the cached return value instead of re-querying.
Reviews (1): Last reviewed commit: "Address cache review feedback" | Re-trigger Greptile