Skip to content

feat(memory): add recall-layer relevance gate to filter low-relevance vector matches #679

Closed

Description

@dcramer

Problem

Low-relevance memories are surfacing in recall for unrelated queries. Example from production: a query about "CI in getsentry/junior" surfaced memories about sentry autofix implementation details and single-tenant access flows — none of which are relevant.

The root cause is that vector search retrieves the top-N results by cosine distance, with no minimum relevance check. The existing mergeSearchCandidates function handles ordering and fusion between vector+lexical, but it doesn't filter out "these memories are irrelevant to this request" — it only sorts what it has.

Why not a static threshold

There's no universal absolute cosine distance cutoff:

  • text-embedding-ada-002 has a random-pair similarity floor near 0.68 (distance ≈ 0.32)
  • Newer text-embedding-3-* models have a lower floor
  • "Getsentry/sentry programming memories" and "getsentry/junior CI memories" can have high cosine similarity because they share vocabulary (GitHub, PR, CI, repo, test) even though they're semantically distinct for the purpose of the user's current request

A relative-ratio approach (score >= maxScore * ratio) also fails when all candidates are weakly related — if the best result is irrelevant, 80% of irrelevant is still irrelevant.

Principled fix: recall-layer relevance gate

The standard IR/RAG pattern is:

  1. Broad candidate generation — vector search (high recall, low precision)
  2. Relevance filter/reranker — precision gate over candidates
  3. Prompt injection — only high-confidence relevant context

The relevance filter should live in recall.ts (not in the generic store.ts search), because relevance is a prompt-injection concern, not a storage concern.

Proposed interface

// In store.ts — expose scored candidates for recall-layer decisions
interface SearchMemoryCandidate {
  memory: MemoryRecord;
  score: number;
  sources: {
    vector?: { rank: number; distance: number; };
    lexical?: { score: number; };
  };
}

async searchMemoryCandidates(input: SearchMemoriesInput): Promise<SearchMemoryCandidate[]>
// In recall.ts — apply a relevance gate before building the prompt
const candidates = await store.searchMemoryCandidates({
  query: userRequest,
  limit: DEFAULT_RECALL_LIMIT * 4,
});

const relevant = await relevanceGate({
  userRequest,
  candidates,
  limit: DEFAULT_RECALL_LIMIT,
});

return renderMemoryPrompt(relevant);

Relevance gate options (in order of principled-ness)

  1. LLM judge (most principled): batch the top-K candidates with the user request into a model call, ask it to score/select which memories are directly helpful for this request. This handles anisotropy, hub memories, and all model-agnostic requirements.

  2. Cross-encoder reranker: a small dedicated model (e.g. ms-marco-based) that scores (query, memory) pairs by relevance. More accurate than bi-encoder cosine similarity, no anisotropy problems.

  3. Lexical-priority heuristic (cheapest fallback): prefer candidates with both vector AND lexical support; treat pure-vector-only candidates as low confidence unless they rank first.

The LLM judge is probably the right answer here given that:

  • Memory recall already happens at agent-turn time, so latency budget is available
  • The batch is small (5–20 memories max)
  • Existing agent infrastructure is already available in the plugin context

Policy for the judge

Select memories that would directly help answer the user's current request. Reject memories that merely share the same company, product family, language, or general engineering context. Prefer specific matches on: the exact repo, workflow, command, test, CI setup, or user preference being asked about.

Zero memories is a valid output

The gate must be allowed to return an empty set (no memory prompt contribution).

Related

Activity

  1. added
    enhancement
    on Jun 29, 2026
  2. sentry-junior commented on Nov 9, 2026

    @sentry-junior
    Contributor

    Fixed on main by #2585: automatic recall is gated by a reranker score (junior-memory/src/recall.ts, RECALL_MIN_RERANK_SCORE).

    Closed during a tracker cleanup. Reopen if something here is still missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions