From 9e7eefc668cae13e612b8614679d6f7ec5acf274 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Fri, 6 Nov 2026 18:02:49 +0530 Subject: [PATCH 1/3] fix(search): stop capping saved memories at three per search 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. --- src/state/hybrid-search.ts | 11 +++++++++-- test/hybrid-search.test.ts | 39 +++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/state/hybrid-search.ts b/src/state/hybrid-search.ts index caeb8f03b8..3288b8f6a5 100644 --- a/src/state/hybrid-search.ts +++ b/src/state/hybrid-search.ts @@ -23,6 +23,7 @@ import { logger } from "../logger.js"; const RRF_K = 60; const GRAPH_BUDGET_MS = 500; +const MIN_FUSION_POOL = 40; export interface HybridSearchRequest { embedding?: { query: string; value: Promise }; @@ -103,7 +104,8 @@ export class HybridSearch { request?: HybridSearchRequest, ): Promise { const retrievalLimit = this.rerankEnabled ? Math.max(limit, 20) : limit; - const bm25Results = this.bm25.search(query, retrievalLimit * 2, targetLayer); + const poolLimit = Math.max(retrievalLimit * 2, MIN_FUSION_POOL); + const bm25Results = this.bm25.search(query, poolLimit, targetLayer); let vectorResults: Array<{ obsId: string; @@ -128,7 +130,7 @@ export class HybridSearch { } else { queryEmbedding = await this.embeddingProvider.embed(query); } - vectorResults = this.vector.search(queryEmbedding, retrievalLimit * 2, targetLayer, (id) => this.bm25.layerOf(id)); + vectorResults = this.vector.search(queryEmbedding, poolLimit, targetLayer, (id) => this.bm25.layerOf(id)); } catch { // fall through to BM25-only } @@ -327,6 +329,11 @@ export class HybridSearch { const sessionCounts = new Map(); for (const r of results) { + if (r.obsId.startsWith("mem_")) { + selected.push(r); + if (selected.length >= limit) break; + continue; + } const count = sessionCounts.get(r.sessionId) || 0; if (count >= maxPerSession) continue; selected.push(r); diff --git a/test/hybrid-search.test.ts b/test/hybrid-search.test.ts index fde7aa2deb..60ffda0c85 100644 --- a/test/hybrid-search.test.ts +++ b/test/hybrid-search.test.ts @@ -3,7 +3,8 @@ import { VectorIndex } from "../src/state/vector-index.js"; import { HybridSearch } from "../src/state/hybrid-search.js"; import { KV } from "../src/state/schema.js"; import { SearchIndex } from "../src/state/search-index.js"; -import type { CompressedObservation, EmbeddingProvider } from "../src/types.js"; +import type { CompressedObservation, EmbeddingProvider, Memory } from "../src/types.js"; +import { memoryToObservation } from "../src/state/memory-utils.js"; function makeObs( overrides: Partial = {}, @@ -54,6 +55,42 @@ describe("HybridSearch", () => { kv = mockKV(); }); + it("does not cap saved memories at three by their shared synthetic session", async () => { + for (let i = 1; i <= 6; i++) { + const memory: Memory = { + id: `mem_${i}`, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + type: "fact", + title: `quasar policy ${i}`, + content: `quasar quasar quasar policy rule ${i}`, + concepts: [], + files: [], + sessionIds: [], + strength: 7, + version: 1, + isLatest: true, + }; + await kv.set(KV.memories, memory.id, memory); + bm25.add(memoryToObservation(memory), "memory"); + } + for (let i = 1; i <= 5; i++) { + const obs = makeObs({ id: `obs_${i}`, sessionId: `ses_${i}`, title: `quasar note ${i}`, narrative: `quasar mention ${i}`, facts: [], concepts: [], files: [] }); + bm25.add(obs); + await kv.set(KV.observations(obs.sessionId), obs.id, obs); + } + const hybrid = new HybridSearch(bm25, null, null, kv as never, 0.4, 0.6, 0, false); + const results = await hybrid.search("quasar policy", 6); + expect(results.map((r) => r.observation.id).filter((id) => id.startsWith("mem_"))).toHaveLength(6); + }); + + it("fuses from a fixed minimum candidate pool when the limit is small", async () => { + const search = vi.spyOn(bm25, "search"); + const hybrid = new HybridSearch(bm25, null, null, kv as never, 0.4, 0.6, 0, false); + await hybrid.search("anything", 5); + expect(search.mock.calls[0]![1]).toBeGreaterThanOrEqual(40); + }); + it("reuses query embeddings only within a recall request", async () => { const obs = makeObs(); bm25.add(obs); From fe77d7d5b33c142462c91ac9865c26d8a0ecb912 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sat, 7 Nov 2026 00:35:07 +0530 Subject: [PATCH 2/3] fix(search): fill result slots that stale index entries leave empty 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. --- src/state/hybrid-search.ts | 27 ++++++++++++++++++++++--- test/hybrid-search.test.ts | 29 +++++++++++++++++++++++---- test/search-project-isolation.test.ts | 4 ++-- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/state/hybrid-search.ts b/src/state/hybrid-search.ts index 3288b8f6a5..37c024a1fa 100644 --- a/src/state/hybrid-search.ts +++ b/src/state/hybrid-search.ts @@ -257,7 +257,7 @@ export class HybridSearch { if (request) request.candidateCount = combined.length; const retrievalDepth = Math.max(limit, 20); - const diversified = this.diversifyBySession(combined, retrievalDepth); + const diversified = this.diversifyBySession(combined, combined.length); const enriched = await this.enrichResults(diversified, retrievalDepth); return applyReranking ? this.rerankResults(query, enriched, limit) : enriched.slice(0, limit); @@ -342,9 +342,10 @@ export class HybridSearch { } if (selected.length < limit) { + const picked = new Set(selected.map((s) => s.obsId)); for (const r of results) { if (selected.length >= limit) break; - if (!selected.some(s => s.obsId === r.obsId)) { + if (!picked.has(r.obsId)) { selected.push(r); } } @@ -365,7 +366,27 @@ export class HybridSearch { }>, limit: number, ): Promise { - const sliced = results.slice(0, limit); + const enriched: HybridSearchResult[] = []; + let next = 0; + while (next < results.length && enriched.length < limit) { + const batch = results.slice(next, next + limit - enriched.length); + next += batch.length; + enriched.push(...(await this.enrichBatch(batch))); + } + return enriched; + } + + private async enrichBatch( + sliced: Array<{ + obsId: string; + sessionId: string; + bm25Score: number; + vectorScore: number; + graphScore: number; + combinedScore: number; + graphContext?: string; + }>, + ): Promise { const observations = await Promise.all( sliced.map(async (r) => { const obs = await this.kv diff --git a/test/hybrid-search.test.ts b/test/hybrid-search.test.ts index 60ffda0c85..2467c37359 100644 --- a/test/hybrid-search.test.ts +++ b/test/hybrid-search.test.ts @@ -84,11 +84,32 @@ describe("HybridSearch", () => { expect(results.map((r) => r.observation.id).filter((id) => id.startsWith("mem_"))).toHaveLength(6); }); - it("fuses from a fixed minimum candidate pool when the limit is small", async () => { - const search = vi.spyOn(bm25, "search"); + it("ranks a record first when it scores well in both keyword and vector search, even for a one-result search", async () => { + const keywordOnly = makeObs({ id: "obs_keyword", sessionId: "ses_k", title: "zephyr zephyr zephyr", narrative: "zephyr zephyr zephyr", facts: [], concepts: [], files: [] }); + const keywordSecond = makeObs({ id: "obs_keyword2", sessionId: "ses_k2", title: "zephyr zephyr notes", narrative: "zephyr zephyr notes", facts: [], concepts: [], files: [] }); + const both = makeObs({ id: "obs_both", sessionId: "ses_b", title: "zephyr notes notes", narrative: "zephyr notes notes", facts: [], concepts: [], files: [] }); + const vectorOnly = makeObs({ id: "obs_vector", sessionId: "ses_v", title: "unrelated", narrative: "unrelated", facts: [], concepts: [], files: [] }); + for (const obs of [keywordOnly, keywordSecond, both]) bm25.add(obs); + for (const obs of [keywordOnly, keywordSecond, both, vectorOnly]) await kv.set(KV.observations(obs.sessionId), obs.id, obs); + const vector = new VectorIndex(); + vector.add(vectorOnly.id, vectorOnly.sessionId, new Float32Array([1, 0])); + vector.add(both.id, both.sessionId, new Float32Array([0.9, 0.1])); + const provider = { name: "test", dimensions: 2, embed: vi.fn(async () => new Float32Array([1, 0])), embedBatch: vi.fn() }; + const hybrid = new HybridSearch(bm25, vector, provider, kv as never, 0.4, 0.6, 0, false); + const results = await hybrid.search("zephyr", 1); + expect(results.map((r) => r.observation.id)).toEqual(["obs_both"]); + }); + + it("returns a valid lower-ranked record when stale index entries fill the first enrichment pass", async () => { + for (let i = 0; i < 25; i++) { + bm25.add(makeObs({ id: `obs_stale_${i}`, sessionId: `ses_stale_${i}`, title: "nebula nebula nebula", narrative: "nebula nebula nebula", facts: [], concepts: [], files: [] })); + } + const valid = makeObs({ id: "obs_valid", sessionId: "ses_valid", title: "nebula notes notes", narrative: "nebula notes notes", facts: [], concepts: [], files: [] }); + bm25.add(valid); + await kv.set(KV.observations(valid.sessionId), valid.id, valid); const hybrid = new HybridSearch(bm25, null, null, kv as never, 0.4, 0.6, 0, false); - await hybrid.search("anything", 5); - expect(search.mock.calls[0]![1]).toBeGreaterThanOrEqual(40); + const results = await hybrid.search("nebula", 5); + expect(results.map((r) => r.observation.id)).toEqual(["obs_valid"]); }); it("reuses query embeddings only within a recall request", async () => { diff --git a/test/search-project-isolation.test.ts b/test/search-project-isolation.test.ts index 99069dd7eb..83a4d9b413 100644 --- a/test/search-project-isolation.test.ts +++ b/test/search-project-isolation.test.ts @@ -198,7 +198,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( }); if (ranking === "hybrid") { - it("expands a real hybrid window shortened by a missing record", async () => { + it("fills a real hybrid window shortened by a missing record", async () => { for (let i = 0; i < 100; i++) { addMemory({ id: `mem_foreign_${i.toString().padStart(3, "0")}`, project: "project-b" }); } @@ -210,7 +210,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( const ranker = vi.fn(hybrid.search.bind(hybrid)); setHybridRanker(ranker); expect(await recall({ limit: 1 })).toEqual([matching.id]); - expect(ranker.mock.calls.map((call) => call[1])).toEqual([100, 200]); + expect(ranker.mock.calls.map((call) => call[1])).toEqual([100]); }); } From 3b40410b08507749e112e770fc94a4c252adc14d Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sat, 7 Nov 2026 00:48:32 +0530 Subject: [PATCH 3/3] perf(search): keep enrichment batches full when entries are stale 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. --- src/state/hybrid-search.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/state/hybrid-search.ts b/src/state/hybrid-search.ts index 37c024a1fa..2c7eb4e2ad 100644 --- a/src/state/hybrid-search.ts +++ b/src/state/hybrid-search.ts @@ -369,11 +369,11 @@ export class HybridSearch { const enriched: HybridSearchResult[] = []; let next = 0; while (next < results.length && enriched.length < limit) { - const batch = results.slice(next, next + limit - enriched.length); + const batch = results.slice(next, next + limit); next += batch.length; enriched.push(...(await this.enrichBatch(batch))); } - return enriched; + return enriched.slice(0, limit); } private async enrichBatch(