From 6419f6b8b51b95b8b9c9ec14510d36897693eb46 Mon Sep 17 00:00:00 2001 From: Dmitrii Zhukov Date: Tue, 3 Nov 2026 17:11:43 +0000 Subject: [PATCH 1/3] fix(search): rank scoped smart-search again when the first window leaves the page short With a project or agent filter, smart-search over-fetches at most 300 hybrid results and filters them afterwards, so a few hundred stronger rows from other projects or agents left the scoped page empty even when matching rows existed. When the filtered page is shorter than the limit and the search index holds more entries than the first window, rank once more with a 1,000-candidate window. Unscoped searches, scoped searches that fill their page and indexes the first window already covers keep a single ranking call. Signed-off-by: Dmitrii Zhukov --- src/functions/smart-search.ts | 40 ++++-- test/smart-search-scoped-ranking.test.ts | 173 +++++++++++++++++++++++ 2 files changed, 198 insertions(+), 15 deletions(-) create mode 100644 test/smart-search-scoped-ranking.test.ts diff --git a/src/functions/smart-search.ts b/src/functions/smart-search.ts index 19318b0669..bf02720f18 100644 --- a/src/functions/smart-search.ts +++ b/src/functions/smart-search.ts @@ -76,6 +76,7 @@ export function resetFollowupStatsForTests(): void { // Compact mode trims each lesson's content for at-a-glance display. The // full content is fetched via memory_lesson_recall when the caller needs it. const LESSON_CONTENT_PREVIEW_CHARS = 240; +const SCOPED_RETRY_CANDIDATES = 1000; export function registerSmartSearchFunction( sdk: IIIClient, @@ -211,21 +212,30 @@ export function registerSmartSearchFunction( : Promise.resolve([]), ]); - const layerResults = hybridResults.filter((result) => matchesSearchLayer( - result.observation.id, - result.sessionId, - targetLayer, - result.layer ?? getSearchIndex().layerOf(result.observation.id), - )); - const projectResults = matchesProject - ? await filterProjectResults(layerResults, (result) => matchesProject(result.observation.id, result.sessionId)) - : layerResults; - - const filteredHybrid = filterAgentId - ? projectResults - .filter((r) => r.observation.agentId === filterAgentId) - .slice(0, limit) - : projectResults.slice(0, limit); + const scopeResults = async (results: HybridSearchResult[]) => { + const layerResults = results.filter((result) => matchesSearchLayer( + result.observation.id, + result.sessionId, + targetLayer, + result.layer ?? getSearchIndex().layerOf(result.observation.id), + )); + const projectResults = matchesProject + ? await filterProjectResults(layerResults, (result) => matchesProject(result.observation.id, result.sessionId)) + : layerResults; + + return filterAgentId + ? projectResults + .filter((r) => r.observation.agentId === filterAgentId) + .slice(0, limit) + : projectResults.slice(0, limit); + }; + + let filteredHybrid = await scopeResults(hybridResults); + if ((filterAgentId || project) && filteredHybrid.length < limit && getSearchIndex().size > overFetchLimit) { + filteredHybrid = await scopeResults( + await searchFn(data.query, SCOPED_RETRY_CANDIDATES, targetLayer, authorizeGraph), + ); + } const compact: CompactSearchResult[] = filteredHybrid.map((r) => ({ obsId: r.observation.id, diff --git a/test/smart-search-scoped-ranking.test.ts b/test/smart-search-scoped-ranking.test.ts new file mode 100644 index 0000000000..024cd52068 --- /dev/null +++ b/test/smart-search-scoped-ranking.test.ts @@ -0,0 +1,173 @@ +import { afterEach, describe, it, expect, vi } from "vitest"; + +vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() } })); + +import { registerSmartSearchFunction } from "../src/functions/smart-search.js"; +import { getSearchIndex } from "../src/functions/search.js"; +import { HybridSearch } from "../src/state/hybrid-search.js"; +import { KV } from "../src/state/schema.js"; +import { memoryToObservation } from "../src/state/memory-utils.js"; +import type { CompressedObservation, Memory } from "../src/types.js"; + +function fixture() { + const store = new Map>(); + const kv = { + get: async (scope: string, key: string) => store.get(scope)?.get(key) ?? null, + set: async (scope: string, key: string, value: unknown) => { + if (!store.has(scope)) store.set(scope, new Map()); + store.get(scope)!.set(key, value); + return value; + }, + delete: async (scope: string, key: string) => { + store.get(scope)?.delete(key); + }, + list: async (scope: string) => Array.from(store.get(scope)?.values() ?? []), + }; + const index = getSearchIndex(); + const hybrid = new HybridSearch(index, null, null, kv as never, 0.4, 0.6, 0); + const searchFn = vi.fn((...args: Parameters) => hybrid.search(...args)); + let handle!: (data: Record) => Promise<{ results: Array<{ obsId: string }> }>; + const sdk = { + registerFunction: (_id: string, fn: typeof handle) => { handle = fn; }, + trigger: vi.fn(async () => ({ success: true, lessons: [] })), + }; + registerSmartSearchFunction(sdk as never, kv as never, searchFn); + + const add = async (id: string, sessionId: string, project: string, agentId: string, text: string) => { + const observation: CompressedObservation = { + id, sessionId, agentId, timestamp: "2026-01-01T00:00:00Z", type: "discovery", + title: text, narrative: text, facts: [], concepts: [], files: [], importance: 5, + }; + index.add(observation); + await kv.set(KV.observations(sessionId), id, observation); + await kv.set(KV.sessions, sessionId, { id: sessionId, project }); + }; + const addStale = (id: string, sessionId: string, text: string) => { + index.add({ + id, sessionId, timestamp: "2026-01-01T00:00:00Z", type: "discovery", + title: text, narrative: text, facts: [], concepts: [], files: [], importance: 5, + }); + }; + const addMemory = async (id: string, project: string, agentId: string, text: string) => { + const memory: Memory = { + id, createdAt: "2026-01-01T00:00:00Z", updatedAt: "2026-01-01T00:00:00Z", type: "fact", + title: text, content: text, concepts: [], files: [], sessionIds: [], strength: 5, + version: 1, isLatest: true, agentId, project, + }; + index.add(memoryToObservation(memory), "memory"); + await kv.set(KV.memories, id, memory); + }; + const query = async (data: Record) => + (await handle({ query: "deploy rollback", includeLessons: false, ...data })).results.map((r) => r.obsId).sort(); + return { add, addStale, addMemory, query, searchFn }; +} + +describe("smart search scoped ranking", () => { + afterEach(() => getSearchIndex().clear()); + + const weaker = "deploy rollback notes kept for the alpha service while the staging cluster was drained overnight"; + + it.each([ + ["project", { project: "alpha" }], + ["agent", { agentId: "agent-a" }], + ])("returns in-scope observations ranked below 997 stronger out-of-scope ones (%s filter)", async (_name, scope) => { + const f = fixture(); + for (let i = 0; i < 997; i++) await f.add(`obs_b_${i}`, `ses_b_${i}`, "beta", "agent-b", "deploy rollback"); + for (let i = 0; i < 3; i++) await f.add(`obs_a_${i}`, "ses_a", "alpha", "agent-a", weaker); + + for (const limit of [5, 20, 100]) { + expect(await f.query({ ...scope, limit })).toEqual(["obs_a_0", "obs_a_1", "obs_a_2"]); + } + expect(f.searchFn).toHaveBeenCalledTimes(6); + }); + + it.each([ + ["project", { project: "alpha" }], + ["agent", { agentId: "agent-a" }], + ])("returns in-scope saved memories ranked below 997 stronger out-of-scope ones (%s filter)", async (_name, scope) => { + const f = fixture(); + for (let i = 0; i < 997; i++) await f.addMemory(`mem_b_${i}`, "beta", "agent-b", "deploy rollback"); + for (let i = 0; i < 3; i++) await f.addMemory(`mem_a_${i}`, "alpha", "agent-a", weaker); + + for (const limit of [5, 20, 100]) { + expect(await f.query({ ...scope, limit })).toEqual(["mem_a_0", "mem_a_1", "mem_a_2"]); + } + expect(f.searchFn).toHaveBeenCalledTimes(6); + }); + + it("completes a page that the first window left one row short", async () => { + const f = fixture(); + for (let i = 0; i < 181; i++) await f.add(`obs_b_${i}`, `ses_b_${i}`, "beta", "agent-b", "deploy rollback"); + for (let i = 0; i < 20; i++) await f.add(`obs_a_${i}`, `ses_a_${i}`, "alpha", "agent-a", weaker); + + expect(await f.query({ project: "alpha", limit: 20 })).toHaveLength(20); + expect(f.searchFn).toHaveBeenCalledTimes(2); + }); + + it("ranks again when an index entry without a stored record shortens the first window", async () => { + const f = fixture(); + f.addStale("obs_gone", "ses_gone", "deploy rollback deploy rollback"); + for (let i = 0; i < 400; i++) await f.add(`obs_b_${i}`, `ses_b_${i}`, "beta", "agent-b", "deploy rollback"); + for (let i = 0; i < 3; i++) await f.add(`obs_a_${i}`, "ses_a", "alpha", "agent-a", weaker); + + expect(await f.query({ project: "alpha", limit: 20 })).toEqual(["obs_a_0", "obs_a_1", "obs_a_2"]); + }); + + it("keeps the requested layer on the deeper ranking", async () => { + const f = fixture(); + for (let i = 0; i < 997; i++) await f.add(`obs_o_${i}`, `ses_o_${i}`, "alpha", "agent-a", "deploy rollback"); + for (let i = 0; i < 400; i++) await f.addMemory(`mem_b_${i}`, "beta", "agent-b", "deploy rollback"); + for (let i = 0; i < 3; i++) await f.addMemory(`mem_a_${i}`, "alpha", "agent-a", weaker); + + expect(await f.query({ project: "alpha", targetLayer: "memory" })).toEqual(["mem_a_0", "mem_a_1", "mem_a_2"]); + }); + + it("keeps a scoped search that fills its page to one ranking pass", async () => { + const f = fixture(); + for (let i = 0; i < 250; i++) await f.add(`obs_a_${i}`, `ses_a_${i}`, "alpha", "agent-a", "deploy rollback"); + + expect(await f.query({ project: "alpha", limit: 20 })).toHaveLength(20); + expect(f.searchFn).toHaveBeenCalledTimes(1); + }); + + it("keeps one ranking pass when a stale index entry sits inside a window that covers the index", async () => { + const f = fixture(); + f.addStale("obs_gone", "ses_gone", "deploy rollback deploy rollback"); + for (let i = 0; i < 150; i++) await f.add(`obs_b_${i}`, `ses_b_${i}`, "beta", "agent-b", "deploy rollback"); + for (let i = 0; i < 3; i++) await f.add(`obs_a_${i}`, "ses_a", "alpha", "agent-a", weaker); + + expect(await f.query({ project: "alpha", limit: 20 })).toEqual(["obs_a_0", "obs_a_1", "obs_a_2"]); + expect(f.searchFn).toHaveBeenCalledTimes(1); + }); + + describe.each([ + ["project", { project: "alpha" }], + ["agent", { agentId: "agent-a" }], + ])("first window boundary (%s filter)", (_name, scope) => { + it.each([ + [153, 20, 1], + [200, 20, 1], + [201, 20, 2], + [100, 5, 1], + [101, 5, 2], + [300, 40, 1], + [301, 40, 2], + ])("an index of %i entries at limit %i ranks %i time(s)", async (indexSize, limit, calls) => { + const f = fixture(); + for (let i = 0; i < indexSize - 3; i++) await f.add(`obs_b_${i}`, `ses_b_${i}`, "beta", "agent-b", "deploy rollback"); + for (let i = 0; i < 3; i++) await f.add(`obs_a_${i}`, "ses_a", "alpha", "agent-a", weaker); + + expect(await f.query({ ...scope, limit })).toEqual(["obs_a_0", "obs_a_1", "obs_a_2"]); + expect(f.searchFn).toHaveBeenCalledTimes(calls); + }); + }); + + it("leaves an unscoped search at one ranking pass with the requested limit", async () => { + const f = fixture(); + for (let i = 0; i < 5; i++) await f.add(`obs_a_${i}`, `ses_a_${i}`, "alpha", "agent-a", "deploy rollback"); + for (let i = 0; i < 30; i++) await f.add(`obs_x_${i}`, `ses_x_${i}`, "alpha", "agent-a", "cache warmup schedule"); + + expect(await f.query({ limit: 20 })).toHaveLength(5); + expect(f.searchFn.mock.calls.map((call) => call[1])).toEqual([20]); + }); +}); From 5ceca75088d9cc00517ecba894414c4f2164ad4d Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Wed, 4 Nov 2026 11:44:00 +0530 Subject: [PATCH 2/3] fix(search): keep first-pass scoped hits when the deeper ranking returns fewer The scoped retry replaced the first-pass results outright. Graph retrieval has its own time budget on each call, so a retry that loses graph-only hits could return fewer results than the first pass. The deeper ranking now leads, and first-pass hits it missed fill the rest of the page. --- src/functions/smart-search.ts | 7 ++++++- test/smart-search-scoped-ranking.test.ts | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/functions/smart-search.ts b/src/functions/smart-search.ts index bf02720f18..b751ad9324 100644 --- a/src/functions/smart-search.ts +++ b/src/functions/smart-search.ts @@ -232,9 +232,14 @@ export function registerSmartSearchFunction( let filteredHybrid = await scopeResults(hybridResults); if ((filterAgentId || project) && filteredHybrid.length < limit && getSearchIndex().size > overFetchLimit) { - filteredHybrid = await scopeResults( + const deeper = await scopeResults( await searchFn(data.query, SCOPED_RETRY_CANDIDATES, targetLayer, authorizeGraph), ); + const seen = new Set(deeper.map((result) => result.observation.id)); + filteredHybrid = [ + ...deeper, + ...filteredHybrid.filter((result) => !seen.has(result.observation.id)), + ].slice(0, limit); } const compact: CompactSearchResult[] = filteredHybrid.map((r) => ({ diff --git a/test/smart-search-scoped-ranking.test.ts b/test/smart-search-scoped-ranking.test.ts index 024cd52068..cd8c82df3c 100644 --- a/test/smart-search-scoped-ranking.test.ts +++ b/test/smart-search-scoped-ranking.test.ts @@ -113,6 +113,17 @@ describe("smart search scoped ranking", () => { expect(await f.query({ project: "alpha", limit: 20 })).toEqual(["obs_a_0", "obs_a_1", "obs_a_2"]); }); + it("keeps first-pass hits when the deeper ranking returns fewer", async () => { + const f = fixture(); + for (let i = 0; i < 400; i++) await f.add(`obs_b_${i}`, `ses_b_${i}`, "beta", "agent-b", "deploy rollback"); + for (let i = 0; i < 3; i++) await f.add(`obs_a_${i}`, "ses_a", "alpha", "agent-a", "deploy rollback deploy rollback"); + const first = f.searchFn.getMockImplementation()!; + f.searchFn.mockImplementationOnce(first).mockImplementationOnce(async () => []); + + expect(await f.query({ project: "alpha", limit: 20 })).toEqual(["obs_a_0", "obs_a_1", "obs_a_2"]); + expect(f.searchFn).toHaveBeenCalledTimes(2); + }); + it("keeps the requested layer on the deeper ranking", async () => { const f = fixture(); for (let i = 0; i < 997; i++) await f.add(`obs_o_${i}`, `ses_o_${i}`, "alpha", "agent-a", "deploy rollback"); From 455367d1dd6a5ccca81572bdc8ea16bbc1ae4dad Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Wed, 4 Nov 2026 12:11:28 +0530 Subject: [PATCH 3/3] fix(search): keep first-pass hits when the deeper scoped ranking fails A failure in the deeper scoped retry rejected the whole smart-search call and discarded the results the first pass had already found. The retry now logs the error and keeps the first-pass results. --- src/functions/smart-search.ts | 22 ++++++++++++++-------- test/smart-search-scoped-ranking.test.ts | 13 +++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/functions/smart-search.ts b/src/functions/smart-search.ts index b751ad9324..89f38f2053 100644 --- a/src/functions/smart-search.ts +++ b/src/functions/smart-search.ts @@ -232,14 +232,20 @@ export function registerSmartSearchFunction( let filteredHybrid = await scopeResults(hybridResults); if ((filterAgentId || project) && filteredHybrid.length < limit && getSearchIndex().size > overFetchLimit) { - const deeper = await scopeResults( - await searchFn(data.query, SCOPED_RETRY_CANDIDATES, targetLayer, authorizeGraph), - ); - const seen = new Set(deeper.map((result) => result.observation.id)); - filteredHybrid = [ - ...deeper, - ...filteredHybrid.filter((result) => !seen.has(result.observation.id)), - ].slice(0, limit); + try { + const deeper = await scopeResults( + await searchFn(data.query, SCOPED_RETRY_CANDIDATES, targetLayer, authorizeGraph), + ); + const seen = new Set(deeper.map((result) => result.observation.id)); + filteredHybrid = [ + ...deeper, + ...filteredHybrid.filter((result) => !seen.has(result.observation.id)), + ].slice(0, limit); + } catch (err) { + logger.warn("Smart search deeper scoped ranking failed; keeping first-pass results", { + error: err instanceof Error ? err.message : String(err), + }); + } } const compact: CompactSearchResult[] = filteredHybrid.map((r) => ({ diff --git a/test/smart-search-scoped-ranking.test.ts b/test/smart-search-scoped-ranking.test.ts index cd8c82df3c..14fc05baa4 100644 --- a/test/smart-search-scoped-ranking.test.ts +++ b/test/smart-search-scoped-ranking.test.ts @@ -124,6 +124,19 @@ describe("smart search scoped ranking", () => { expect(f.searchFn).toHaveBeenCalledTimes(2); }); + it("keeps first-pass hits when the deeper ranking fails", async () => { + const f = fixture(); + for (let i = 0; i < 400; i++) await f.add(`obs_b_${i}`, `ses_b_${i}`, "beta", "agent-b", "deploy rollback"); + for (let i = 0; i < 3; i++) await f.add(`obs_a_${i}`, "ses_a", "alpha", "agent-a", "deploy rollback deploy rollback"); + const first = f.searchFn.getMockImplementation()!; + f.searchFn.mockImplementationOnce(first).mockImplementationOnce(async () => { + throw new Error("ranker unavailable"); + }); + + expect(await f.query({ project: "alpha", limit: 20 })).toEqual(["obs_a_0", "obs_a_1", "obs_a_2"]); + expect(f.searchFn).toHaveBeenCalledTimes(2); + }); + it("keeps the requested layer on the deeper ranking", async () => { const f = fixture(); for (let i = 0; i < 997; i++) await f.add(`obs_o_${i}`, `ses_o_${i}`, "alpha", "agent-a", "deploy rollback");