XMem icon indicating copy to clipboard operation
XMem copied to clipboard

Add fast memory search mode

Open Amhitox opened this issue 6 months ago • 2 comments

This PR adds a fast search path for memory lookup without forcing the full agentic retrieval flow every time. The default behavior is raw search results, and callers can opt into answer synthesis with answer=true when they still want an LLM-generated response.

What changed:

  • expanded /v1/memory/search to return ranked hits from profile, temporal, summary, snippet, and code memory
  • kept raw search as the default path, with optional answer generation behind answer=true
  • added per-domain timing data and p50/p95/p99 latency snapshots for raw vs answer mode
  • added short-lived caching for profile catalogs and retrieval plans to avoid repeated planning work
  • covered the raw and answer search behavior with focused API tests

Tested with:

.venv/bin/python -m pytest tests/api/test_dependencies_and_routes.py
.venv/bin/python -m py_compile src/api/schemas.py src/api/routes/memory.py src/pipelines/retrieval.py tests/api/test_dependencies_and_routes.py

Fixes #163

Amhitox avatar May 20 '26 13:05 Amhitox

Greptile Summary

This PR adds a "fast" search path to /v1/memory/search that fans out across profile, temporal, summary, snippet, and code domains, sorts by score, and optionally synthesizes an LLM answer. Short-lived caches on the singleton RetrievalPipeline are introduced to avoid re-planning on repeated queries.

  • SearchRequest gains answer, org_id, repo, and project_id fields; SearchResponse gains mode, answer, confidence, and latency_ms.
  • Per-domain helper functions are wrapped with _timed_sync/_timed_async and their elapsed times are returned alongside rolling p50/p95/p99 percentiles tracked in a module-level deque.
  • The _retrieval_plan_cache on the singleton pipeline uses (user_id, query, catalog_text, top_k) as a key with no size cap, which will grow indefinitely in production since free-text queries rarely repeat within the TTL window.

Confidence Score: 3/5

Safe to merge for non-production use; the retrieval plan cache needs a size bound before running under real query volume.

The singleton RetrievalPipeline now holds a dict keyed by full query strings with no eviction path — every unique user query adds a new AIMessage entry that stays in memory for the process lifetime. Under realistic multi-user traffic this will grow continuously and eventually exhaust heap. Separately, the domain searches run in series rather than concurrently, so the latency improvement over the full agentic flow is smaller than the PR description implies.

src/pipelines/retrieval.py — the new _retrieval_plan_cache needs a max-size bound or an LRU eviction strategy before this ships to production.

Important Files Changed

Filename Overview
src/pipelines/retrieval.py Adds profile catalog and retrieval plan caches to the singleton pipeline; plan cache keyed on arbitrary query strings grows without bound and never evicts stale entries, creating a memory leak.
src/api/routes/memory.py Adds per-domain search helpers, timing instrumentation, and optional LLM answer synthesis to /v1/memory/search; domain searches run sequentially instead of concurrently, and the confidence calculation ignores actual scores.
src/api/schemas.py Extends SearchRequest with answer, org_id, repo, project_id fields and SearchResponse with mode, answer, confidence, and latency_ms; validator updated to allow snippet and code domains.
tests/api/test_dependencies_and_routes.py Adds two focused API tests covering raw search result ordering/latency fields and answer synthesis mode; fake pipeline stubs are clean and cover all new code paths.

Sequence Diagram

sequenceDiagram
    participant C as Client
    participant R as /v1/memory/search
    participant P as RetrievalPipeline (singleton)
    participant V as VectorStore
    participant N as Neo4j
    participant M as LLM (answer=true only)

    C->>R: "POST /v1/memory/search {query, domains, answer?}"
    R->>P: _fetch_profile_catalog(user_id) [cache 60s]
    P-->>R: catalog + raw records
    R->>N: search_events_by_embedding (temporal)
    N-->>R: events[]
    R->>V: search_by_text (summary)
    V-->>R: summary hits[]
    R->>P: _search_snippet (snippet)
    P-->>R: snippet hits[]
    Note over R: sort all_results by score desc
    alt "answer=true"
        R->>M: ainvoke(ANSWER_PROMPT + context)
        M-->>R: synthesized answer text
    end
    R->>R: _record_search_latency (update global deque)
    R-->>C: "SearchResponse {results, mode, answer?, confidence, latency_ms}"

Fix All in Cursor Fix All in Codex Fix All in Claude Code

Reviews (1): Last reviewed commit: "Add fast memory search mode" | Re-trigger Greptile

greptile-apps avatar May 23 '26 09:05 greptile-apps

@Amhitox please resolve the conflicts and have a look at the suggestions pls resolve them and add comments on them

ved015 avatar Jun 01 '26 11:06 ved015