fix(rag): stop superseded indexing runs and refuse to wipe chunks on an empty reindex

- Each document gets an indexing run number; a reindex, synced replacement or
  delete makes the older run stop instead of embedding stale chunks, reporting
  'indexed' early and leaving a progress bar for a deleted document
- Reindexing a file that is now empty throws like addDocument instead of
  replacing the stored chunks with nothing
This commit is contained in:
Yun Chan 2026-10-21 00:24:38 +09:00
parent 45d8b1ddb1
commit c46713536d
2 changed files with 83 additions and 1 deletions

View file

@ -394,3 +394,62 @@ describe('OllamaEmbeddingAdapter', () => {
expect(isSameOllamaModel('nomic-embed-text:v1.5', 'nomic-embed-text')).toBe(false)
})
})
describe('RAGService 색인 실행 겹침', () => {
function openGate(h: Harness): () => void {
let release: () => void = () => undefined
h.embedder.gate = new Promise<void>((r) => {
release = r
})
return () => {
h.embedder.gate = null
release()
}
}
it('색인 중에 재색인하면 앞선 실행은 멈추고, 끝 알림은 새 실행이 한 번만 보낸다', async () => {
const h = harness()
const release = openGate(h)
const doc = await h.service.addDocument(writeTemp('a.txt', LONG_TEXT))
await settle()
const reindexing = h.service.reindex(doc.id)
await settle()
release()
await reindexing
await settle()
const completes = h.events.filter((e) => e.channel === IPC_CHANNELS.RAG.INDEX_COMPLETE)
expect(completes).toHaveLength(1)
expect(h.store.listChunks(doc.id).every((c) => c.embedding.length > 0)).toBe(true)
expect(h.store.getDocument(doc.id)?.indexed).toBe(true)
})
it('색인 중에 문서를 지우면 남은 청크를 임베딩하지 않고 진행 표시를 닫는다', async () => {
const h = harness()
const release = openGate(h)
const doc = await h.service.addDocument(writeTemp('a.txt', LONG_TEXT))
await settle()
h.service.removeDocument(doc.id)
release()
await settle()
expect(h.embedder.embedCalls).toBe(1)
expect(h.events.at(-1)).toEqual({
channel: IPC_CHANNELS.RAG.INDEX_COMPLETE,
data: { documentId: doc.id, fileName: doc.fileName },
})
})
it('원본이 비어 있으면 재색인은 거부하고 저장된 청크를 지우지 않는다', async () => {
const h = harness()
const file = writeTemp('a.txt', LONG_TEXT)
const doc = await h.service.addDocument(file)
await settle()
const before = h.store.listChunks(doc.id).length
fs.writeFileSync(file, ' \n', 'utf-8')
await expect(h.service.reindex(doc.id)).rejects.toMatchObject({ code: ErrorCode.RAGIndexingFailed })
expect(h.store.listChunks(doc.id)).toHaveLength(before)
expect(h.store.getDocument(doc.id)?.indexed).toBe(true)
})
})