Skip to content

Commit 5284ffd

Browse files
committed
fix(code-index): preserve existing index across failed retries
1 parent 23ba041 commit 5284ffd

2 files changed

Lines changed: 60 additions & 20 deletions

File tree

‎src/services/code-index/__tests__/orchestrator.spec.ts‎

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,48 @@ describe("CodeIndexOrchestrator - error path cleanup gating", () => {
200200
expect(lastCall[0]).toBe("Error")
201201
})
202202

203-
it("should call clearCollection() and clear cache when an error occurs after initialize() succeeds (indexing started)", async () => {
204-
// Arrange: initialize succeeds; fail soon after to enter error path with indexingStarted=true
205-
vectorStore.initialize.mockResolvedValue(false) // existing collection
206-
vectorStore.hasIndexedData.mockResolvedValue(false) // force full scan path
207-
vectorStore.markIndexingIncomplete.mockRejectedValue(new Error("mark incomplete failure"))
203+
it.each([false, true])(
204+
"only cleans up a failed full scan when the collection was created by this run: %s",
205+
async (created) => {
206+
vectorStore.initialize.mockResolvedValue(created)
207+
vectorStore.hasIndexedData.mockResolvedValue(false) // force full scan path
208+
vectorStore.markIndexingIncomplete.mockRejectedValue(new Error("mark incomplete failure"))
209+
210+
const orchestrator = new CodeIndexOrchestrator(
211+
configManager,
212+
stateManager,
213+
workspacePath,
214+
cacheManager,
215+
vectorStore,
216+
scanner,
217+
fileWatcher,
218+
)
219+
220+
// Act
221+
await orchestrator.startIndexing()
222+
223+
expect(vectorStore.clearCollection).toHaveBeenCalledTimes(created ? 1 : 0)
224+
// A new collection clears stale cache at initialization and again on failure.
225+
expect(cacheManager.clearCacheFile).toHaveBeenCalledTimes(created ? 2 : 0)
226+
227+
// Error state should be set
228+
expect(stateManager.setSystemState).toHaveBeenCalled()
229+
const lastCall = stateManager.setSystemState.mock.calls[stateManager.setSystemState.mock.calls.length - 1]
230+
expect(lastCall[0]).toBe("Error")
231+
},
232+
)
208233

234+
it("preserves an existing index after an incremental failure and a failed full-scan retry", async () => {
235+
let complete = true
236+
vectorStore.initialize.mockResolvedValue(false)
237+
vectorStore.hasIndexedData.mockImplementation(async () => complete)
238+
vectorStore.markIndexingIncomplete.mockImplementation(async () => {
239+
complete = false
240+
})
241+
scanner.scanDirectory.mockImplementation(async (_dir: string, onError: (error: Error) => void) => {
242+
onError(new Error("embedding failed"))
243+
return { stats: { processed: 0, skipped: 0 }, totalBlockCount: 0 }
244+
})
209245
const orchestrator = new CodeIndexOrchestrator(
210246
configManager,
211247
stateManager,
@@ -216,17 +252,23 @@ describe("CodeIndexOrchestrator - error path cleanup gating", () => {
216252
fileWatcher,
217253
)
218254

219-
// Act
220-
await orchestrator.startIndexing()
221-
222-
// Assert: cleanup gated behind indexingStarted should have happened
223-
expect(vectorStore.clearCollection).toHaveBeenCalledTimes(1)
224-
expect(cacheManager.clearCacheFile).toHaveBeenCalledTimes(1)
225-
226-
// Error state should be set
227-
expect(stateManager.setSystemState).toHaveBeenCalled()
228-
const lastCall = stateManager.setSystemState.mock.calls[stateManager.setSystemState.mock.calls.length - 1]
229-
expect(lastCall[0]).toBe("Error")
255+
for (let attempt = 1; attempt <= 2; attempt++) {
256+
await orchestrator.startIndexing()
257+
expect(complete).toBe(false)
258+
expect(orchestrator.state).toBe("Error")
259+
expect(scanner.scanDirectory).toHaveBeenCalledTimes(attempt)
260+
expect(vectorStore.markIndexingIncomplete).toHaveBeenCalledTimes(attempt)
261+
expect(vectorStore.clearCollection).not.toHaveBeenCalled()
262+
expect(cacheManager.clearCacheFile).not.toHaveBeenCalled()
263+
expect(vectorStore.markIndexingComplete).not.toHaveBeenCalled()
264+
expect(fileWatcher.initialize).not.toHaveBeenCalled()
265+
}
266+
expect(stateManager.setSystemState).toHaveBeenCalledWith("Indexing", "Checking for new or modified files...")
267+
expect(stateManager.setSystemState).toHaveBeenCalledWith(
268+
"Indexing",
269+
"Services ready. Starting workspace scan...",
270+
)
271+
expect(stateManager.setSystemState).not.toHaveBeenCalledWith("Indexed", expect.any(String))
230272
})
231273

232274
it("collects batch errors from full scan and transitions to Error when all blocks fail", async () => {

‎src/services/code-index/orchestrator.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,13 @@ export class CodeIndexOrchestrator {
130130
const signal = this._abortController.signal
131131
this.stateManager.setSystemState("Indexing", "Initializing services...")
132132

133-
// Preserve data on connection failures and incremental updates; clean up failed rebuilds.
133+
// Only clean up collections created by this run; existing data must survive failed retries.
134134
let clearIndexOnError = false
135135

136136
try {
137137
const collectionCreated = await this.vectorStore.initialize()
138138

139-
// Successfully connected to Qdrant
140-
clearIndexOnError = true
139+
clearIndexOnError = collectionCreated
141140

142141
if (collectionCreated) {
143142
await this.cacheManager.clearCacheFile()
@@ -148,7 +147,6 @@ export class CodeIndexOrchestrator {
148147
const hasExistingData = await this.vectorStore.hasIndexedData()
149148

150149
if (hasExistingData && !collectionCreated) {
151-
clearIndexOnError = false
152150
if (!(await this.scanExecutor.runIncrementalScan(signal))) {
153151
await this.cacheManager.flush()
154152
this.stopWatcher()

0 commit comments

Comments
 (0)