This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathfile-watcher.ts
More file actions
628 lines (567 loc) · 20 KB
/
Copy pathfile-watcher.ts
File metadata and controls
628 lines (567 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import * as path from "path"
import * as vscode from "vscode"
import {
QDRANT_CODE_BLOCK_NAMESPACE,
MAX_FILE_SIZE_BYTES,
BATCH_SEGMENT_THRESHOLD,
MAX_BATCH_RETRIES,
INITIAL_RETRY_DELAY_MS,
} from "../constants"
import { createHash } from "crypto"
import { RooIgnoreController } from "../../../core/ignore/RooIgnoreController"
import { v5 as uuidv5 } from "uuid"
import { Ignore } from "ignore"
import { scannerExtensions } from "../shared/supported-extensions"
import {
IFileWatcher,
FileProcessingResult,
IEmbedder,
IVectorStore,
PointStruct,
BatchProcessingSummary,
} from "../interfaces"
import { codeParser } from "./parser"
import { CacheManager } from "../cache-manager"
import { generateNormalizedAbsolutePath, generateRelativeFilePath } from "../shared/get-relative-path"
import { isPathInIgnoredDirectory } from "../../glob/ignore-utils"
import { TelemetryService } from "@roo-code/telemetry"
import { TelemetryEventName } from "@roo-code/types"
import { sanitizeErrorMessage } from "../shared/validation-helpers"
import { Package } from "../../../shared/package"
/**
* Implementation of the file watcher interface
*/
export class FileWatcher implements IFileWatcher {
private ignoreInstance?: Ignore
private fileWatcher?: vscode.FileSystemWatcher
private ignoreController: RooIgnoreController
private accumulatedEvents: Map<string, { uri: vscode.Uri; type: "create" | "change" | "delete" }> = new Map()
private batchProcessDebounceTimer?: NodeJS.Timeout
private readonly BATCH_DEBOUNCE_DELAY_MS = 500
private readonly FILE_PROCESSING_CONCURRENCY_LIMIT = 10
private readonly batchSegmentThreshold: number
private readonly _onDidStartBatchProcessing = new vscode.EventEmitter<string[]>()
private readonly _onBatchProgressUpdate = new vscode.EventEmitter<{
processedInBatch: number
totalInBatch: number
currentFile?: string
}>()
private readonly _onDidFinishBatchProcessing = new vscode.EventEmitter<BatchProcessingSummary>()
/**
* Event emitted when a batch of files begins processing
*/
public readonly onDidStartBatchProcessing = this._onDidStartBatchProcessing.event
/**
* Event emitted to report progress during batch processing
*/
public readonly onBatchProgressUpdate = this._onBatchProgressUpdate.event
/**
* Event emitted when a batch of files has finished processing
*/
public readonly onDidFinishBatchProcessing = this._onDidFinishBatchProcessing.event
/**
* Creates a new file watcher
* @param workspacePath Path to the workspace
* @param context VS Code extension context
* @param embedder Optional embedder
* @param vectorStore Optional vector store
* @param cacheManager Cache manager
*/
constructor(
private workspacePath: string,
private context: vscode.ExtensionContext,
private readonly cacheManager: CacheManager,
private embedder?: IEmbedder,
private vectorStore?: IVectorStore,
ignoreInstance?: Ignore,
ignoreController?: RooIgnoreController,
batchSegmentThreshold?: number,
) {
this.ignoreController = ignoreController || new RooIgnoreController(workspacePath)
if (ignoreInstance) {
this.ignoreInstance = ignoreInstance
}
// Get the configurable batch size from VSCode settings, fallback to default
// If not provided in constructor, try to get from VSCode settings
if (batchSegmentThreshold !== undefined) {
this.batchSegmentThreshold = batchSegmentThreshold
} else {
try {
this.batchSegmentThreshold = vscode.workspace
.getConfiguration(Package.name)
.get<number>("codeIndex.embeddingBatchSize", BATCH_SEGMENT_THRESHOLD)
} catch {
// In test environment, vscode.workspace might not be available
this.batchSegmentThreshold = BATCH_SEGMENT_THRESHOLD
}
}
}
/**
* Initializes the file watcher
*/
async initialize(): Promise<void> {
// Create file watcher
const filePattern = new vscode.RelativePattern(
this.workspacePath,
`**/*{${scannerExtensions.map((e) => e.substring(1)).join(",")}}`,
)
this.fileWatcher = vscode.workspace.createFileSystemWatcher(filePattern)
// Register event handlers
this.fileWatcher.onDidCreate(this.handleFileCreated.bind(this))
this.fileWatcher.onDidChange(this.handleFileChanged.bind(this))
this.fileWatcher.onDidDelete(this.handleFileDeleted.bind(this))
}
/**
* Disposes the file watcher
*/
dispose(): void {
this.fileWatcher?.dispose()
if (this.batchProcessDebounceTimer) {
clearTimeout(this.batchProcessDebounceTimer)
}
this._onDidStartBatchProcessing.dispose()
this._onBatchProgressUpdate.dispose()
this._onDidFinishBatchProcessing.dispose()
this.accumulatedEvents.clear()
}
/**
* Handles file creation events
* @param uri URI of the created file
*/
private async handleFileCreated(uri: vscode.Uri): Promise<void> {
this.accumulatedEvents.set(uri.fsPath, { uri, type: "create" })
this.scheduleBatchProcessing()
}
/**
* Handles file change events
* @param uri URI of the changed file
*/
private async handleFileChanged(uri: vscode.Uri): Promise<void> {
this.accumulatedEvents.set(uri.fsPath, { uri, type: "change" })
this.scheduleBatchProcessing()
}
/**
* Handles file deletion events.
* When a directory is deleted, VSCode's FileSystemWatcher may not fire
* individual delete events for each file inside it. This method detects
* directory deletions by checking the cache for any files whose paths
* start with the deleted path prefix, and queues them all for deletion.
* @param uri URI of the deleted file or directory
*/
private async handleFileDeleted(uri: vscode.Uri): Promise<void> {
const deletedPath = uri.fsPath
// Check if any cached files have this as a prefix (directory deletion)
const allHashes = this.cacheManager.getAllHashes()
const childPaths = Object.keys(allHashes).filter(
(cachedPath) => cachedPath.startsWith(deletedPath + path.sep) || cachedPath === deletedPath,
)
if (childPaths.length > 1) {
// Directory was deleted - queue all child files for deletion
for (const childPath of childPaths) {
this.accumulatedEvents.set(childPath, {
uri: vscode.Uri.file(childPath),
type: "delete",
})
}
} else {
// Single file deletion (or a file matching exactly)
this.accumulatedEvents.set(deletedPath, { uri, type: "delete" })
}
this.scheduleBatchProcessing()
}
/**
* Schedules batch processing with debounce
*/
private scheduleBatchProcessing(): void {
if (this.batchProcessDebounceTimer) {
clearTimeout(this.batchProcessDebounceTimer)
}
this.batchProcessDebounceTimer = setTimeout(() => this.triggerBatchProcessing(), this.BATCH_DEBOUNCE_DELAY_MS)
}
/**
* Triggers processing of accumulated events
*/
private async triggerBatchProcessing(): Promise<void> {
if (this.accumulatedEvents.size === 0) {
return
}
const eventsToProcess = new Map(this.accumulatedEvents)
this.accumulatedEvents.clear()
const filePathsInBatch = Array.from(eventsToProcess.keys())
this._onDidStartBatchProcessing.fire(filePathsInBatch)
await this.processBatch(eventsToProcess)
}
/**
* Processes a batch of accumulated events
* @param eventsToProcess Map of events to process
*/
private async _handleBatchDeletions(
batchResults: FileProcessingResult[],
processedCountInBatch: number,
totalFilesInBatch: number,
pathsToExplicitlyDelete: string[],
filesToUpsertDetails: Array<{ path: string; uri: vscode.Uri; originalType: "create" | "change" }>,
): Promise<{ overallBatchError?: Error; clearedPaths: Set<string>; processedCount: number }> {
let overallBatchError: Error | undefined
const allPathsToClearFromDB = new Set<string>(pathsToExplicitlyDelete)
for (const fileDetail of filesToUpsertDetails) {
if (fileDetail.originalType === "change") {
allPathsToClearFromDB.add(fileDetail.path)
}
}
if (allPathsToClearFromDB.size > 0 && this.vectorStore) {
try {
await this.vectorStore.deletePointsByMultipleFilePaths(Array.from(allPathsToClearFromDB))
for (const path of pathsToExplicitlyDelete) {
this.cacheManager.deleteHash(path)
batchResults.push({ path, status: "success" })
processedCountInBatch++
this._onBatchProgressUpdate.fire({
processedInBatch: processedCountInBatch,
totalInBatch: totalFilesInBatch,
currentFile: path,
})
}
} catch (error: any) {
const errorStatus = error?.status || error?.response?.status || error?.statusCode
const errorMessage = error instanceof Error ? error.message : String(error)
// Log telemetry for deletion error
TelemetryService.instance.captureEvent(TelemetryEventName.CODE_INDEX_ERROR, {
error: sanitizeErrorMessage(errorMessage),
location: "deletePointsByMultipleFilePaths",
errorType: "deletion_error",
errorStatus: errorStatus,
})
// Mark all paths as error
overallBatchError = error as Error
for (const path of pathsToExplicitlyDelete) {
batchResults.push({ path, status: "error", error: error as Error })
processedCountInBatch++
this._onBatchProgressUpdate.fire({
processedInBatch: processedCountInBatch,
totalInBatch: totalFilesInBatch,
currentFile: path,
})
}
}
}
return { overallBatchError, clearedPaths: allPathsToClearFromDB, processedCount: processedCountInBatch }
}
private async _processFilesAndPrepareUpserts(
filesToUpsertDetails: Array<{ path: string; uri: vscode.Uri; originalType: "create" | "change" }>,
batchResults: FileProcessingResult[],
processedCountInBatch: number,
totalFilesInBatch: number,
pathsToExplicitlyDelete: string[],
): Promise<{
pointsForBatchUpsert: PointStruct[]
successfullyProcessedForUpsert: Array<{ path: string; newHash?: string }>
processedCount: number
}> {
const pointsForBatchUpsert: PointStruct[] = []
const successfullyProcessedForUpsert: Array<{ path: string; newHash?: string }> = []
const filesToProcessConcurrently = [...filesToUpsertDetails]
for (let i = 0; i < filesToProcessConcurrently.length; i += this.FILE_PROCESSING_CONCURRENCY_LIMIT) {
const chunkToProcess = filesToProcessConcurrently.slice(i, i + this.FILE_PROCESSING_CONCURRENCY_LIMIT)
const chunkProcessingPromises = chunkToProcess.map(async (fileDetail) => {
this._onBatchProgressUpdate.fire({
processedInBatch: processedCountInBatch,
totalInBatch: totalFilesInBatch,
currentFile: fileDetail.path,
})
try {
const result = await this.processFile(fileDetail.path)
return { path: fileDetail.path, result: result, error: undefined }
} catch (e) {
const error = e as Error
console.error(`[FileWatcher] Unhandled exception processing file ${fileDetail.path}:`, e)
return { path: fileDetail.path, result: undefined, error: error }
}
})
const settledChunkResults = await Promise.allSettled(chunkProcessingPromises)
for (const settledResult of settledChunkResults) {
let resultPath: string | undefined
if (settledResult.status === "fulfilled") {
const { path, result, error: directError } = settledResult.value
resultPath = path
if (directError) {
batchResults.push({ path, status: "error", error: directError })
} else if (result) {
if (result.status === "skipped" || result.status === "local_error") {
batchResults.push(result)
} else if (result.status === "processed_for_batching" && result.pointsToUpsert) {
pointsForBatchUpsert.push(...result.pointsToUpsert)
if (result.path && result.newHash) {
successfullyProcessedForUpsert.push({ path: result.path, newHash: result.newHash })
} else if (result.path && !result.newHash) {
successfullyProcessedForUpsert.push({ path: result.path })
}
} else {
batchResults.push({
path,
status: "error",
error: new Error(
`Unexpected result status from processFile: ${result.status} for file ${path}`,
),
})
}
} else {
batchResults.push({
path,
status: "error",
error: new Error(`Fulfilled promise with no result or error for file ${path}`),
})
}
} else {
const error = settledResult.reason as Error
const rejectedPath = (settledResult.reason as any)?.path || "unknown"
console.error("[FileWatcher] A file processing promise was rejected:", settledResult.reason)
batchResults.push({
path: rejectedPath,
status: "error",
error: error,
})
}
if (!pathsToExplicitlyDelete.includes(resultPath || "")) {
processedCountInBatch++
}
this._onBatchProgressUpdate.fire({
processedInBatch: processedCountInBatch,
totalInBatch: totalFilesInBatch,
currentFile: resultPath,
})
}
}
return {
pointsForBatchUpsert,
successfullyProcessedForUpsert,
processedCount: processedCountInBatch,
}
}
private async _executeBatchUpsertOperations(
pointsForBatchUpsert: PointStruct[],
successfullyProcessedForUpsert: Array<{ path: string; newHash?: string }>,
batchResults: FileProcessingResult[],
overallBatchError?: Error,
): Promise<Error | undefined> {
if (pointsForBatchUpsert.length > 0 && this.vectorStore && !overallBatchError) {
try {
for (let i = 0; i < pointsForBatchUpsert.length; i += this.batchSegmentThreshold) {
const batch = pointsForBatchUpsert.slice(i, i + this.batchSegmentThreshold)
let retryCount = 0
let upsertError: Error | undefined
while (retryCount < MAX_BATCH_RETRIES) {
try {
await this.vectorStore.upsertPoints(batch)
break
} catch (error) {
upsertError = error as Error
retryCount++
if (retryCount === MAX_BATCH_RETRIES) {
// Log telemetry for upsert failure
TelemetryService.instance.captureEvent(TelemetryEventName.CODE_INDEX_ERROR, {
error: sanitizeErrorMessage(upsertError.message),
location: "upsertPoints",
errorType: "upsert_retry_exhausted",
retryCount: MAX_BATCH_RETRIES,
})
throw new Error(
`Failed to upsert batch after ${MAX_BATCH_RETRIES} retries: ${upsertError.message}`,
)
}
await new Promise((resolve) =>
setTimeout(resolve, INITIAL_RETRY_DELAY_MS * Math.pow(2, retryCount - 1)),
)
}
}
}
for (const { path, newHash } of successfullyProcessedForUpsert) {
if (newHash) {
this.cacheManager.updateHash(path, newHash)
}
batchResults.push({ path, status: "success" })
}
} catch (error) {
const err = error as Error
overallBatchError = overallBatchError || err
// Log telemetry for batch upsert error
TelemetryService.instance.captureEvent(TelemetryEventName.CODE_INDEX_ERROR, {
error: sanitizeErrorMessage(err.message),
location: "executeBatchUpsertOperations",
errorType: "batch_upsert_error",
affectedFiles: successfullyProcessedForUpsert.length,
})
for (const { path } of successfullyProcessedForUpsert) {
batchResults.push({ path, status: "error", error: err })
}
}
} else if (overallBatchError && pointsForBatchUpsert.length > 0) {
for (const { path } of successfullyProcessedForUpsert) {
batchResults.push({ path, status: "error", error: overallBatchError })
}
}
return overallBatchError
}
private async processBatch(
eventsToProcess: Map<string, { uri: vscode.Uri; type: "create" | "change" | "delete" }>,
): Promise<void> {
const batchResults: FileProcessingResult[] = []
let processedCountInBatch = 0
const totalFilesInBatch = eventsToProcess.size
let overallBatchError: Error | undefined
// Initial progress update
this._onBatchProgressUpdate.fire({
processedInBatch: 0,
totalInBatch: totalFilesInBatch,
currentFile: undefined,
})
// Categorize events
const pathsToExplicitlyDelete: string[] = []
const filesToUpsertDetails: Array<{ path: string; uri: vscode.Uri; originalType: "create" | "change" }> = []
for (const event of eventsToProcess.values()) {
if (event.type === "delete") {
pathsToExplicitlyDelete.push(event.uri.fsPath)
} else {
filesToUpsertDetails.push({
path: event.uri.fsPath,
uri: event.uri,
originalType: event.type,
})
}
}
// Phase 1: Handle deletions
const { overallBatchError: deletionError, processedCount: deletionCount } = await this._handleBatchDeletions(
batchResults,
processedCountInBatch,
totalFilesInBatch,
pathsToExplicitlyDelete,
filesToUpsertDetails,
)
overallBatchError = deletionError
processedCountInBatch = deletionCount
// Phase 2: Process files and prepare upserts
const {
pointsForBatchUpsert,
successfullyProcessedForUpsert,
processedCount: upsertCount,
} = await this._processFilesAndPrepareUpserts(
filesToUpsertDetails,
batchResults,
processedCountInBatch,
totalFilesInBatch,
pathsToExplicitlyDelete,
)
processedCountInBatch = upsertCount
// Phase 3: Execute batch upsert
overallBatchError = await this._executeBatchUpsertOperations(
pointsForBatchUpsert,
successfullyProcessedForUpsert,
batchResults,
overallBatchError,
)
// Finalize
this._onDidFinishBatchProcessing.fire({
processedFiles: batchResults,
batchError: overallBatchError,
})
this._onBatchProgressUpdate.fire({
processedInBatch: totalFilesInBatch,
totalInBatch: totalFilesInBatch,
})
if (this.accumulatedEvents.size === 0) {
this._onBatchProgressUpdate.fire({
processedInBatch: 0,
totalInBatch: 0,
currentFile: undefined,
})
}
}
/**
* Processes a file
* @param filePath Path to the file to process
* @returns Promise resolving to processing result
*/
async processFile(filePath: string): Promise<FileProcessingResult> {
try {
// Get relative path for ignore checks
const relativeFilePath = generateRelativeFilePath(filePath, this.workspacePath)
// Check if file is in an ignored directory
// Use relative path to avoid matching parent directories outside the workspace
if (isPathInIgnoredDirectory(relativeFilePath)) {
return {
path: filePath,
status: "skipped" as const,
reason: "File is in an ignored directory",
}
}
// Check if file should be ignored
if (
!this.ignoreController.validateAccess(filePath) ||
(this.ignoreInstance && this.ignoreInstance.ignores(relativeFilePath))
) {
return {
path: filePath,
status: "skipped" as const,
reason: "File is ignored by .rooignore or .gitignore",
}
}
// Check file size
const fileStat = await vscode.workspace.fs.stat(vscode.Uri.file(filePath))
if (fileStat.size > MAX_FILE_SIZE_BYTES) {
return {
path: filePath,
status: "skipped" as const,
reason: "File is too large",
}
}
// Read file content
const fileContent = await vscode.workspace.fs.readFile(vscode.Uri.file(filePath))
const content = fileContent.toString()
// Calculate hash
const newHash = createHash("sha256").update(content).digest("hex")
// Check if file has changed
if (this.cacheManager.getHash(filePath) === newHash) {
return {
path: filePath,
status: "skipped" as const,
reason: "File has not changed",
}
}
// Parse file
const blocks = await codeParser.parseFile(filePath, { content, fileHash: newHash })
// Prepare points for batch processing
let pointsToUpsert: PointStruct[] = []
if (this.embedder && blocks.length > 0) {
const texts = blocks.map((block) => block.content)
const { embeddings } = await this.embedder.createEmbeddings(texts)
pointsToUpsert = blocks.map((block, index) => {
const normalizedAbsolutePath = generateNormalizedAbsolutePath(block.file_path, this.workspacePath)
const stableName = `${normalizedAbsolutePath}:${block.start_line}`
const pointId = uuidv5(stableName, QDRANT_CODE_BLOCK_NAMESPACE)
return {
id: pointId,
vector: embeddings[index],
payload: {
filePath: generateRelativeFilePath(normalizedAbsolutePath, this.workspacePath),
codeChunk: block.content,
startLine: block.start_line,
endLine: block.end_line,
},
}
})
}
return {
path: filePath,
status: "processed_for_batching" as const,
newHash,
pointsToUpsert,
}
} catch (error) {
return {
path: filePath,
status: "local_error" as const,
error: error as Error,
}
}
}
}