Repository navigation
Description
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-002has 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:
- Broad candidate generation — vector search (high recall, low precision)
- Relevance filter/reranker — precision gate over candidates
- 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)
-
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.
-
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. -
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
- PR fix(memory): increase recall budget and add vector distance cutoff #677 (fixes truncation, does NOT include relevance fix)
- Slack thread: https://jr-cramer.slack.com/archives/C0B595QDZLL/p1782761766112369
Activity
fix(memory): increase recall budget and add vector distance cutoff (#677
fix(github-issues): ban conventional commit prefixes in issue titles (#…
sentry-junior commentedon Nov 9, 2026
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.