Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(history): prefilter local companion vector candidates

Chrisb38ba2f2e21a
diff --git a/apps/desktop/src/main/localDb/__tests__/remoteHistoryPagination.test.ts b/apps/desktop/src/main/localDb/__tests__/remoteHistoryPagination.test.ts
index 1f03f38817..1e9e41b615 100644
--- a/apps/desktop/src/main/localDb/__tests__/remoteHistoryPagination.test.ts
+++ b/apps/desktop/src/main/localDb/__tests__/remoteHistoryPagination.test.ts
@@ -183,6 +183,22 @@ describe.each(modes)('%s remote history visibility', (mode) => {
expect((legacy as any).hits.map((hit: any) => hit.sessionId)).toEqual(['ordinary']);
});
+ // #6779: local semantic-only hits were starved by 300 closer vectors from other
+ // teammates before the 250-candidate KNN limit. Exercise the production engine
+ // with both real SQLite joins and native vec0; no FTS hit can mask the failure.
+ it.each([{ teammateId: 'visible' }, { source: 'telegram' }])(
+ 'filters local vector candidates before limiting: %j', async (filter) => {
+ db.prepare("UPDATE sessions SET source='telegram' WHERE id='visible'").run();
+ const result = await searchChatHistoryHybrid({
+ ...searchArgs, remoteVisibleOnly: false, query: 'semantic-only', ...filter,
+ });
+ expect(result.vectorUsed).toBe(true);
+ expect(result.hits.map(hit => hit.sessionId)).toEqual(['visible']);
+ expect(result.hits[0]).toMatchObject({ vectorRank: 1, context: [{ content: 'needle' }] });
+ expect(result.hits[0].ftsRank).toBeNull();
+ },
+ );
+
// Activity cursor must use the same clock as ordering, including userSendAt and ties.
it('paginates activity ties and preserves the existing scope intersection', async () => {
db.exec(`
diff --git a/apps/desktop/src/main/localDb/chatHistorySearch.ts b/apps/desktop/src/main/localDb/chatHistorySearch.ts
index 331cfc4f81..bf09d45c9b 100644
--- a/apps/desktop/src/main/localDb/chatHistorySearch.ts
+++ b/apps/desktop/src/main/localDb/chatHistorySearch.ts
@@ -333,7 +333,9 @@ async function runVectorArm(
const { clause, params } = buildFilterClause(args, workdirCandidates);
// The availability probe and KNN must see the same eligible rows. Probing the
// full vector table would expose hidden data through vector_used/skip_reason.
- const eligibleRowids = args.remoteVisibleOnly
+ // Local teammate/source filters also need preselection so other conversations
+ // cannot fill the KNN candidate pool before the requested scope is applied.
+ const eligibleRowids = args.remoteVisibleOnly || args.teammateId || args.source
? `rowid IN (
SELECT j.rowid FROM embedding_jobs j
JOIN messages m ON m.id = j.source_id
@@ -346,7 +348,7 @@ async function runVectorArm(
try {
const probe = await getDbClient().queryOne<{ rowid: number }>(
`SELECT rowid FROM "${CHAT_VEC_TABLE}" ${eligibleRowids ? `WHERE ${eligibleRowids}` : ''} LIMIT 1`,
- args.remoteVisibleOnly ? params : [],
+ eligibleRowids ? params : [],
);
if (!probe) {
return {
@@ -391,9 +393,9 @@ async function runVectorArm(
// float32 必须以 Buffer(little-endian)绑定为 BLOB(better-sqlite3 不接受裸 Float32Array)。
const f32 = Buffer.from(Float32Array.from(queryVec).buffer);
const vectorPoolLimit = clampInternalPoolLimit(args.vectorPoolLimit, ARM_POOL);
- // vec0 supports rowid IN within its KNN cursor. Apply the remote scope before
- // its candidate limit: filtering only outside the CTE lets hidden rows starve
- // visible semantic hits. Reuse the arm predicate; local search stays unchanged.
+ // vec0 supports rowid IN within its KNN cursor. Apply the requested scope before
+ // its candidate limit: filtering only outside the CTE lets unrelated rows starve
+ // eligible semantic hits. Reuse the arm predicate for both probe and retrieval.
const sql = `
WITH knn AS (
SELECT rowid, distance
@@ -424,7 +426,7 @@ async function runVectorArm(
distance: number;
}>(sql, [
f32,
- ...(args.remoteVisibleOnly ? params : []),
+ ...(eligibleRowids ? params : []),
vectorPoolLimit * VEC_OVERFETCH,
...params,
vectorPoolLimit,