diff --git a/src/state/hybrid-search.ts b/src/state/hybrid-search.ts index caeb8f03b8..2c7eb4e2ad 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 } @@ -255,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); @@ -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); @@ -335,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); } } @@ -358,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); + next += batch.length; + enriched.push(...(await this.enrichBatch(batch))); + } + return enriched.slice(0, limit); + } + + 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 fde7aa2deb..2467c37359 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,63 @@ 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("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); + 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 () => { const obs = makeObs(); bm25.add(obs); 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]); }); }