refactor(memory): share stale index cleanup

This commit is contained in:
Peter Steinberger
2026-02-14 15:06:02 +00:00
parent cdc31903c2
commit bc4881ed0c
3 changed files with 64 additions and 54 deletions

42
src/memory/sync-stale.ts Normal file
View File

@@ -0,0 +1,42 @@
import type { DatabaseSync } from "node:sqlite";
export function deleteStaleIndexedPaths(params: {
db: DatabaseSync;
source: string;
activePaths: Set<string>;
vectorTable: string;
ftsTable: string;
ftsEnabled: boolean;
ftsAvailable: boolean;
model: string;
}) {
const staleRows = params.db
.prepare(`SELECT path FROM files WHERE source = ?`)
.all(params.source) as Array<{ path: string }>;
for (const stale of staleRows) {
if (params.activePaths.has(stale.path)) {
continue;
}
params.db
.prepare(`DELETE FROM files WHERE path = ? AND source = ?`)
.run(stale.path, params.source);
try {
params.db
.prepare(
`DELETE FROM ${params.vectorTable} WHERE id IN (SELECT id FROM chunks WHERE path = ? AND source = ?)`,
)
.run(stale.path, params.source);
} catch {}
params.db
.prepare(`DELETE FROM chunks WHERE path = ? AND source = ?`)
.run(stale.path, params.source);
if (params.ftsEnabled && params.ftsAvailable) {
try {
params.db
.prepare(`DELETE FROM ${params.ftsTable} WHERE path = ? AND source = ? AND model = ?`)
.run(stale.path, params.source, params.model);
} catch {}
}
}
}