Skip to content

Commit 23ba041

Browse files
committed
fix(code-index): report incremental scan errors without clearing existing index
1 parent 505a799 commit 23ba041

5 files changed

Lines changed: 70 additions & 48 deletions

File tree

‎src/eslint-suppressions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@
13061306
},
13071307
"services/code-index/__tests__/orchestrator.spec.ts": {
13081308
"@typescript-eslint/no-explicit-any": {
1309-
"count": 25
1309+
"count": 23
13101310
}
13111311
},
13121312
"services/code-index/__tests__/service-factory.spec.ts": {

‎src/services/code-index/__tests__/code-index-scan-executor.spec.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,23 @@ describe("CodeIndexScanExecutor", () => {
136136
},
137137
)
138138

139-
it.each([0, 2])("preserves incremental batch-error tolerance with %s indexed blocks", async (indexed) => {
139+
it.each([0, 2])("rejects incremental batch errors with %s indexed blocks", async (indexed) => {
140140
const { executor, scanner } = setup()
141+
const failures = [new Error("embedding failure"), new Error("upsert failure"), new Error("embedding failure")]
141142
scanner.scanDirectory.mockImplementation(async (_path, onError, onIndexed, onParsed) => {
142143
onParsed?.(3)
143144
onIndexed?.(indexed)
144-
onError?.(new Error("batch failure"))
145+
for (const failure of failures) onError?.(failure)
145146
return { stats: { processed: 1, skipped: 0 }, totalBlockCount: 3 }
146147
})
147-
await expect(executor.runIncrementalScan(new AbortController().signal)).resolves.toBe(true)
148+
const result = executor.runIncrementalScan(new AbortController().signal)
149+
await expect(result).rejects.toBeInstanceOf(AggregateError)
150+
await expect(result).rejects.toMatchObject({
151+
errors: failures,
152+
message: "Incremental scan failed with 3 errors:\nembedding failure\nupsert failure",
153+
})
154+
await result.catch((error: AggregateError) => {
155+
for (const [index, failure] of failures.entries()) expect(error.errors[index]).toBe(failure)
156+
})
148157
})
149158
})

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

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ vi.mock("@roo-code/telemetry", () => ({
4242
}))
4343

4444
// Mock i18n translator used in orchestrator messages
45-
vi.mock("../../i18n", () => ({
46-
t: (key: string, params?: any) => {
45+
vi.mock("../../../i18n", () => ({
46+
t: (key: string, params?: { errorMessage?: string }) => {
4747
if (key === "embeddings:orchestrator.failedDuringInitialScan" && params?.errorMessage) {
4848
return `Failed during initial scan: ${params.errorMessage}`
4949
}
@@ -259,36 +259,49 @@ describe("CodeIndexOrchestrator - error path cleanup gating", () => {
259259
expect(calls[calls.length - 1]).toBe("Error")
260260
})
261261

262-
it("collects batch errors from incremental scan and still completes indexing", async () => {
263-
const batchError = new Error("incremental batch failure")
264-
vectorStore.initialize.mockResolvedValue(false) // existing collection
265-
vectorStore.hasIndexedData.mockResolvedValue(true) // force incremental scan path
266-
vectorStore.markIndexingIncomplete.mockResolvedValue(undefined)
267-
vectorStore.markIndexingComplete.mockResolvedValue(undefined)
268-
269-
// Incremental scan reports a batch error but returns a result — orchestrator completes normally
270-
scanner.scanDirectory.mockImplementation(async (_dir: string, onBatchError: (e: Error) => void) => {
271-
onBatchError(batchError)
272-
return { stats: { processed: 0, skipped: 0 }, totalBlockCount: 0 }
273-
})
262+
it.each([0, 2])(
263+
"preserves the existing index on incremental batch failure after %s indexed blocks",
264+
async (indexed) => {
265+
const batchError = new Error("incremental batch failure")
266+
vectorStore.initialize.mockResolvedValue(false) // existing collection
267+
vectorStore.hasIndexedData.mockResolvedValue(true) // force incremental scan path
268+
vectorStore.markIndexingIncomplete.mockResolvedValue(undefined)
269+
vectorStore.markIndexingComplete.mockResolvedValue(undefined)
270+
271+
scanner.scanDirectory.mockImplementation(
272+
async (_dir: string, onBatchError: (e: Error) => void, onIndexed: (count: number) => void) => {
273+
onIndexed(indexed)
274+
onBatchError(batchError)
275+
return { stats: { processed: 1, skipped: 0 }, totalBlockCount: 3 }
276+
},
277+
)
274278

275-
const orchestrator = new CodeIndexOrchestrator(
276-
configManager,
277-
stateManager,
278-
workspacePath,
279-
cacheManager,
280-
vectorStore,
281-
scanner,
282-
fileWatcher,
283-
)
279+
const orchestrator = new CodeIndexOrchestrator(
280+
configManager,
281+
stateManager,
282+
workspacePath,
283+
cacheManager,
284+
vectorStore,
285+
scanner,
286+
fileWatcher,
287+
)
284288

285-
await orchestrator.startIndexing()
289+
await orchestrator.startIndexing()
286290

287-
// Incremental scan doesn't gate on batch errors — Indexed state is still reached
288-
const calls = stateManager.setSystemState.mock.calls.map((c: any[]) => c[0])
289-
expect(calls[calls.length - 1]).toBe("Indexed")
290-
expect(calls).not.toContain("Error")
291-
})
291+
expect(orchestrator.state).toBe("Error")
292+
expect(stateManager.setSystemState).toHaveBeenLastCalledWith(
293+
"Error",
294+
expect.stringContaining(batchError.message),
295+
)
296+
expect(stateManager.setSystemState).not.toHaveBeenCalledWith("Indexed", expect.any(String))
297+
expect(vectorStore.markIndexingIncomplete).toHaveBeenCalledOnce()
298+
expect(vectorStore.markIndexingComplete).not.toHaveBeenCalled()
299+
expect(vectorStore.clearCollection).not.toHaveBeenCalled()
300+
expect(cacheManager.clearCacheFile).not.toHaveBeenCalled()
301+
expect(fileWatcher.initialize).not.toHaveBeenCalled()
302+
expect(fileWatcher.dispose).toHaveBeenCalledOnce()
303+
},
304+
)
292305
})
293306

294307
describe("CodeIndexOrchestrator - stopIndexing", () => {

‎src/services/code-index/code-index-scan-executor.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ export class CodeIndexScanExecutor {
2020
const summary = await this.scanWorkspace(signal, "incremental")
2121
if (!summary) return false
2222

23-
// Preserve the existing incremental policy: reported batch errors do not prevent completion.
23+
// Do not mark an existing index complete when some updates failed.
24+
if (summary.batchErrors.length > 0) {
25+
const messages = [...new Set(summary.batchErrors.map((error) => error.message))]
26+
throw new AggregateError(
27+
summary.batchErrors,
28+
`Incremental scan failed with ${summary.batchErrors.length} errors:\n${messages.join("\n")}`,
29+
)
30+
}
31+
2432
if (summary.found > 0) {
2533
console.log(
2634
`[CodeIndexOrchestrator] Incremental scan completed: ${summary.indexed} blocks indexed from new/changed files`,

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

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

133-
// Track whether we successfully connected to Qdrant and started indexing
134-
// This helps us decide whether to preserve cache on error
135-
let indexingStarted = false
133+
// Preserve data on connection failures and incremental updates; clean up failed rebuilds.
134+
let clearIndexOnError = false
136135

137136
try {
138137
const collectionCreated = await this.vectorStore.initialize()
139138

140139
// Successfully connected to Qdrant
141-
indexingStarted = true
140+
clearIndexOnError = true
142141

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

151150
if (hasExistingData && !collectionCreated) {
151+
clearIndexOnError = false
152152
if (!(await this.scanExecutor.runIncrementalScan(signal))) {
153153
await this.cacheManager.flush()
154154
this.stopWatcher()
@@ -193,7 +193,7 @@ export class CodeIndexOrchestrator {
193193
stack: error instanceof Error ? error.stack : undefined,
194194
location: "startIndexing",
195195
})
196-
if (indexingStarted) {
196+
if (clearIndexOnError) {
197197
try {
198198
await this.vectorStore.clearCollection()
199199
} catch (cleanupError) {
@@ -204,21 +204,13 @@ export class CodeIndexOrchestrator {
204204
location: "startIndexing.cleanup",
205205
})
206206
}
207-
}
208-
209-
// Only clear cache if indexing had started (Qdrant connection succeeded)
210-
// If we never connected to Qdrant, preserve cache for incremental scan when it comes back
211-
if (indexingStarted) {
212207
// Indexing started but failed mid-way - clear cache to avoid cache-Qdrant mismatch
213208
await this.cacheManager.clearCacheFile()
214209
console.log(
215210
"[CodeIndexOrchestrator] Indexing failed after starting. Clearing cache to avoid inconsistency.",
216211
)
217212
} else {
218-
// Never connected to Qdrant - preserve cache for future incremental scan
219-
console.log(
220-
"[CodeIndexOrchestrator] Failed to connect to Qdrant. Preserving cache for future incremental scan.",
221-
)
213+
console.log("[CodeIndexOrchestrator] Preserving existing index and cache for a retry.")
222214
}
223215

224216
this.stateManager.setSystemState(

0 commit comments

Comments
 (0)