Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/state/hybrid-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Float32Array> };
Expand Down Expand Up @@ -103,7 +104,8 @@ export class HybridSearch {
request?: HybridSearchRequest,
): Promise<HybridSearchResult[]> {
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;
Expand All @@ -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
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -327,6 +329,11 @@ export class HybridSearch {
const sessionCounts = new Map<string, number>();

for (const r of results) {
if (r.obsId.startsWith("mem_")) {
selected.push(r);
if (selected.length >= limit) break;
continue;
Comment thread
rohitg00 marked this conversation as resolved.
Resolved
}
const count = sessionCounts.get(r.sessionId) || 0;
if (count >= maxPerSession) continue;
selected.push(r);
Expand All @@ -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);
}
}
Expand All @@ -358,7 +366,27 @@ export class HybridSearch {
}>,
limit: number,
): Promise<HybridSearchResult[]> {
const sliced = results.slice(0, limit);
const enriched: HybridSearchResult[] = [];
Comment thread
rohitg00 marked this conversation as resolved.
Resolved
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);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

private async enrichBatch(
sliced: Array<{
obsId: string;
sessionId: string;
bm25Score: number;
vectorScore: number;
graphScore: number;
combinedScore: number;
graphContext?: string;
}>,
): Promise<HybridSearchResult[]> {
const observations = await Promise.all(
sliced.map(async (r) => {
const obs = await this.kv
Expand Down
60 changes: 59 additions & 1 deletion test/hybrid-search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompressedObservation> = {},
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions test/search-project-isolation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
}
Expand All @@ -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]);
});
}

Expand Down
Loading