Skip to content

fix(search): stop capping saved memories at three per search - #1696

Merged
rohitg00 merged 3 commits into
mainfrom
fix/search-memory-diversity
Nov 8, 2026
Merged

rohitg00 merged 3 commits into
mainfrom
fix/search-memory-diversity

Conversation

@rohitg00

@rohitg00 rohitg00 commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

Fixes #1565

Bug

  1. Memories saved with memory_save and no session all carry the synthetic session id "memory". Hybrid search diversifies results by session and admits at most 3 per session in its first pass. So only the 3 best saved memories made the first pass, and observations from real sessions ranked ahead of the 4th best saved memory.
  2. BM25 and vector search each returned limit * 2 candidates before fusion. With limit: 5, a record ranked 11th by keywords and 3rd by meaning only got its vector score and lost to records that appeared in both short lists. The report measured 48 vs 54 of 60 correct answers in the top 5 for limit 5 vs 10.

Fix

  • diversifyBySession lets saved memories (mem_ ids) skip the per-session cap. Each saved memory is an independent record, not part of one session. Observations keep the 3 per session cap.
  • BM25 and vector search now return at least 40 candidates (max(limit * 2, 40)) before fusion, independent of how many results are returned. Graph retrieval keeps its previous limit, and the enrichment depth (max(limit, 20)) is unchanged.

Tests

test/hybrid-search.test.ts:

  • 6 saved memories and 5 single-session observations that match a query: all 6 memories come back for limit: 6 (main returns 3)
  • a limit: 5 search asks BM25 for at least 40 candidates (main asks for 10)

The other hybrid search, recall layer, scoped ranking and project isolation suites pass. npm run build, npm run docs:check, npm run skills:check pass. Full npm test shows only the timing and environment failures that also fail on an unchanged main checkout on this machine.

Summary by CodeRabbit

  • New Features
    • Saved memories can appear in search results without being restricted by the per-session cap. They still count toward the overall result limit.
    • Search retrieves a larger pool of keyword and vector matches, then diversifies the combined results before preparing them.
    • Search continues processing candidates until it fills the requested result count or runs out of candidates, helping return valid results when some candidates cannot be used.

Saved memories with no session share the synthetic session id
"memory", so per-session diversification let only three of them into
the first pass, and observations from real sessions ranked ahead of
the fourth best memory. Saved memories now skip the per-session cap.

BM25 and vector search also return at least 40 candidates before
fusion, so a small limit no longer drops a record that ranks well in
only one stream.
@vercel

vercel Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
agentmemory Ready Ready Preview Nov 6, 2026 7:18pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Hybrid search now retrieves a larger candidate pool, changes how it diversifies saved-memory observations, and enriches candidates in batches until it fills the requested result limit or exhausts them.

Changes: Hybrid Search

Layer / File(s) Summary
Minimum fusion candidate pool
src/state/hybrid-search.ts, test/hybrid-search.test.ts, test/search-project-isolation.test.ts
BM25 and vector retrieval use a shared pool of at least 40 candidates, or twice the retrieval limit when larger. Tests cover dual-scoring rank order and the retrieval limit.
Saved-memory diversification
src/state/hybrid-search.ts, test/hybrid-search.test.ts
Saved-memory observations bypass the per-session cap but still count toward the overall limit. A test checks that a limit-6 search returns six saved memories.
Candidate enrichment and window filling
src/state/hybrid-search.ts, test/hybrid-search.test.ts
Enrichment processes batches until it reaches the requested limit or exhausts candidates. A test checks that a valid record is returned when stale index entries precede it.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • rohitg00/agentmemory#1647: Expands filtered recall windows when results are missing; this change fills ranked windows past stale entries.
  • rohitg00/agentmemory#1669: Retries scoped search when filtering leaves a short page; filling ranked windows past stale entries can affect whether the retry is needed.

Suggested reviewers: reevesc88

Merge Risk: Minimal · up to 3b404

Search retrieves a larger candidate pool and fills around stale entries. No merge-blocking issue was identified.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly describes the change that exempts saved memories from the per-session cap.
Linked Issues check Passed [#1565] Saved memories bypass the session cap, while observations retain it. BM25 and vector search use a minimum fusion pool of 40. Regression tests cover saved-memory ranking and fusion at a small l…
Out of Scope Changes check Passed [#1565] The search changes and tests address diversification and fusion. Batched enrichment and its tests support the expanded candidate pool when index entries are stale. Enrichment can process more …
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Linked Issues check

Explanation

[#1565] Saved memories bypass the session cap, while observations retain it. BM25 and vector search use a minimum fusion pool of 40. Regression tests cover saved-memory ranking and fusion at a small limit.

Full details: Out of Scope Changes check

Explanation

[#1565] The search changes and tests address diversification and fusion. Batched enrichment and its tests support the expanded candidate pool when index entries are stale. Enrichment can process more than 20 candidates, despite the PR description's depth claim.

Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 3 files.

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f9c4e719-6845-4eb4-b801-fc829032a626
Commits

Reviewing files that changed from the base of the PR and between fe77d7d and 3b40410.

Files selected for processing: 1

  • src/state/hybrid-search.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.

devin-ai-integration[bot]

This comment was marked as resolved.

coderabbitai[bot]

This comment was marked as resolved.

Enrichment read only the first ranked candidates. When some of them were
stale index entries with no stored record, they used up slots and hid
valid lower-ranked records. Enrichment now keeps reading ranked
candidates until it has enough records or runs out of candidates.

The candidate-pool test now checks a search result. It shows that a
record ranked well in both keyword and vector search comes first in a
one-result search.
devin-ai-integration[bot]

This comment was marked as resolved.

coderabbitai[bot]

This comment was marked as resolved.

Each refill batch only asked for the number of missing slots, so many
stale candidates meant many small sequential batches. Every batch now
reads a full limit of candidates, and the result is trimmed to the limit
in rank order.
@rohitg00
rohitg00 merged commit 5576bea into main Nov 8, 2026
15 checks passed
@rohitg00
rohitg00 deleted the fix/search-memory-diversity branch November 8, 2026 07:23

This branch was successfully deployed

1 active deployment
Preview — 3b40410b Deployed Nov 6, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

smart_search: saved memories are capped at 3 by session diversification, and small limits shrink the fusion pool

1 participant