From b02d799cc83937b9a26747fc4b3056ea3d412f42 Mon Sep 17 00:00:00 2001 From: ruan-cat <1219043956@qq.com> Date: Thu, 26 Nov 2026 19:13:18 +0800 Subject: [PATCH] fix(search): fuse Lance with FTS for projects under 10000 The hydration threshold was also blocking the fallback after an empty Lance neighbor list, so small projects dropped into an Orama index with no vectors. Co-authored-by: Cursor --- src/store/orama-store.ts | 13 +-- .../small-corpus-lance-fallback.test.ts | 90 +++++++++++++++++++ 2 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 tests/search/small-corpus-lance-fallback.test.ts diff --git a/src/store/orama-store.ts b/src/store/orama-store.ts index 75ad3faf..c33af7e0 100644 --- a/src/store/orama-store.ts +++ b/src/store/orama-store.ts @@ -1232,11 +1232,14 @@ export async function searchObservationsDetailed(options: SearchOptions): Promis // deterministic empty result despite the FTS5 index having real matches. const canUsePersistentLexical = persistentCorpusEligible && Boolean(hasQuery) && !queryVector && (quality === 'fast' || tier === 'fast' || !embeddingEnabled || embeddingUnavailable); - if (!persistent) persistent = persistentCorpusEligible && queryVector && embeddingProviderForSearch - ? await searchPersistentSemantically(options, projectIds, requestLimit, queryVector, embeddingProviderForSearch) - : canUsePersistentLexical - ? await searchPersistentLexically(options, projectIds, requestLimit) - : null; + // 10000 only decides whether the whole corpus is hydrated into Orama. + // A small project still fuses Lance with FTS once a query vector exists. + const lexicalReady = Boolean(getOptionalObservationStore()?.hasLexicalIndex?.()); + if (!persistent && queryVector && embeddingProviderForSearch && (persistentCorpusEligible || lexicalReady)) { + persistent = await searchPersistentSemantically(options, projectIds, requestLimit, queryVector, embeddingProviderForSearch); + } else if (!persistent && canUsePersistentLexical) { + persistent = await searchPersistentLexically(options, projectIds, requestLimit); + } // In lazy large-corpus mode Orama is intentionally empty. If a semantic // shadow index has not been built yet, retain a correct lexical answer diff --git a/tests/search/small-corpus-lance-fallback.test.ts b/tests/search/small-corpus-lance-fallback.test.ts new file mode 100644 index 00000000..23b78217 --- /dev/null +++ b/tests/search/small-corpus-lance-fallback.test.ts @@ -0,0 +1,90 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { mkdtemp, rm } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +import type { Observation } from '../../src/types.js'; + +const state = vi.hoisted(() => ({ + semanticHits: vi.fn(), + provider: { + name: 'small-corpus-provider', + dimensions: 2, + embed: vi.fn(async () => [1, 0]), + }, +})); + +vi.mock('../../src/embedding/provider.js', () => ({ + getEmbeddingProvider: vi.fn(async () => state.provider), + isEmbeddingExplicitlyDisabled: vi.fn(() => false), + resetProvider: vi.fn(), +})); + +vi.mock('../../src/search/semantic-index.js', () => ({ + createSemanticIndexProfile: vi.fn(() => ({ key: 'small-corpus-provider:2', provider: 'small-corpus-provider', dimensions: 2 })), + searchSemanticVectors: state.semanticHits, + upsertSemanticVectors: vi.fn(async () => undefined), +})); + +import { closeAllDatabases } from '../../src/store/sqlite-db.js'; +import { + getObservationStore, + initObservationStore, + resetObservationStore, +} from '../../src/store/obs-store.js'; +import { initObservations, resetObservationRuntime } from '../../src/memory/observations.js'; +import { resetDb, searchObservations } from '../../src/store/orama-store.js'; + +function observation(): Observation { + return { + id: 7, + entityName: 'small-project', + type: 'problem-solution', + title: 'Maintenance search recovery', + narrative: 'A project under the Orama hydration threshold still keeps its lexical row.', + facts: ['fallback=lexical'], + filesModified: [], + concepts: ['maintenance', 'recovery'], + tokens: 20, + createdAt: '2026-11-26T00:00:00.000Z', + projectId: 'small-project', + status: 'active', + }; +} + +describe('small corpus Lance fallback', () => { + let dataDir = ''; + const previousThreshold = process.env.MEMORIX_ORAMA_HYDRATION_THRESHOLD; + + beforeEach(async () => { + dataDir = await mkdtemp(path.join(tmpdir(), 'memorix-small-corpus-')); + delete process.env.MEMORIX_ORAMA_HYDRATION_THRESHOLD; + await initObservationStore(dataDir); + await getObservationStore().insert(observation()); + await initObservations(dataDir); + await resetDb(); + state.semanticHits.mockResolvedValue([]); + }); + + afterEach(async () => { + resetObservationRuntime(); + resetObservationStore(); + closeAllDatabases(); + await resetDb(); + state.semanticHits.mockReset(); + if (previousThreshold === undefined) delete process.env.MEMORIX_ORAMA_HYDRATION_THRESHOLD; + else process.env.MEMORIX_ORAMA_HYDRATION_THRESHOLD = previousThreshold; + if (dataDir) await rm(dataDir, { recursive: true, force: true }); + dataDir = ''; + }); + + it('fuses an empty Lance hit list with FTS when the project is under 10000', async () => { + const entries = await searchObservations({ + query: 'maintenance recovery', + projectId: 'small-project', + limit: 5, + }); + + expect(state.semanticHits).toHaveBeenCalled(); + expect(entries.map((entry) => entry.title)).toContain('Maintenance search recovery'); + }); +});