Kevin van Dijk 4 месяцев назад
Родитель
Сommit
88bcde96fd

+ 2 - 0
src/core/webview/webviewMessageHandler.ts

@@ -3059,6 +3059,7 @@ export const webviewMessageHandler = async (
 			}
 			break
 		}
+		// kilocode_change start
 		case "cancelIndexing": {
 			try {
 				const manager = provider.getCurrentWorkspaceCodeIndexManager()
@@ -3089,6 +3090,7 @@ export const webviewMessageHandler = async (
 			}
 			break
 		}
+		// kilocode_change end
 		case "clearIndexData": {
 			try {
 				const manager = provider.getCurrentWorkspaceCodeIndexManager()

+ 2 - 0
src/services/code-index/manager.ts

@@ -203,6 +203,7 @@ export class CodeIndexManager {
 		}
 	}
 
+	// kilocode_change start
 	/**
 	 * Cancel any active indexing activity immediately.
 	 */
@@ -214,6 +215,7 @@ export class CodeIndexManager {
 			this._orchestrator.cancelIndexing()
 		}
 	}
+	// kilocode_change end
 
 	/**
 	 * Recovers from error state by clearing the error and resetting internal state.

+ 11 - 4
src/services/code-index/orchestrator.ts

@@ -15,7 +15,7 @@ import { t } from "../../i18n"
 export class CodeIndexOrchestrator {
 	private _fileWatcherSubscriptions: vscode.Disposable[] = []
 	private _isProcessing: boolean = false
-	private _cancelRequested: boolean = false
+	private _cancelRequested: boolean = false // kilocode_change
 
 	constructor(
 		private readonly configManager: CodeIndexConfigManager,
@@ -121,7 +121,7 @@ export class CodeIndexOrchestrator {
 			return
 		}
 
-		this._cancelRequested = false
+		this._cancelRequested = false // kilocode_change
 		this._isProcessing = true
 		this.stateManager.setSystemState("Indexing", "Initializing services...")
 
@@ -132,11 +132,14 @@ export class CodeIndexOrchestrator {
 				await this.cacheManager.clearCacheFile()
 			}
 
+			// kilocode_change start
 			if (this._cancelRequested) {
 				this._isProcessing = false
 				this.stateManager.setSystemState("Standby", t("embeddings:orchestrator.indexingCancelled"))
 				return
 			}
+			// kilocode_change end
+
 			this.stateManager.setSystemState("Indexing", "Services ready. Starting workspace scan...")
 
 			let cumulativeBlocksIndexed = 0
@@ -144,13 +147,13 @@ export class CodeIndexOrchestrator {
 			let batchErrors: Error[] = []
 
 			const handleFileParsed = (fileBlockCount: number) => {
-				if (this._cancelRequested) return
+				if (this._cancelRequested) return // kilocode_change
 				cumulativeBlocksFoundSoFar += fileBlockCount
 				this.stateManager.reportBlockIndexingProgress(cumulativeBlocksIndexed, cumulativeBlocksFoundSoFar)
 			}
 
 			const handleBlocksIndexed = (indexedCount: number) => {
-				if (this._cancelRequested) return
+				if (this._cancelRequested) return // kilocode_change
 				cumulativeBlocksIndexed += indexedCount
 				this.stateManager.reportBlockIndexingProgress(cumulativeBlocksIndexed, cumulativeBlocksFoundSoFar)
 			}
@@ -172,6 +175,7 @@ export class CodeIndexOrchestrator {
 				throw new Error("Scan failed, is scanner initialized?")
 			}
 
+			// kilocode_change start
 			if (this._cancelRequested || this.scanner.isCancelled) {
 				this._isProcessing = false
 				if (this.stateManager.state !== "Error") {
@@ -179,6 +183,7 @@ export class CodeIndexOrchestrator {
 				}
 				return
 			}
+			// kilocode_change end
 
 			const { stats } = result
 
@@ -266,6 +271,7 @@ export class CodeIndexOrchestrator {
 		this._isProcessing = false
 	}
 
+	// kilocode_change start
 	/**
 	 * Gracefully cancels any ongoing indexing work.
 	 * - Stops the watcher if active
@@ -289,6 +295,7 @@ export class CodeIndexOrchestrator {
 		// Clear processing flag
 		this._isProcessing = false
 	}
+	// kilocode_change end
 
 	/**
 	 * Clears all index data by stopping the watcher, clearing the vector store,

+ 37 - 14
src/services/code-index/processors/scanner.ts

@@ -32,7 +32,7 @@ import { sanitizeErrorMessage } from "../shared/validation-helpers"
 import { Package } from "../../../shared/package"
 
 export class DirectoryScanner implements IDirectoryScanner {
-	private _cancelled = false
+	private _cancelled = false // kilocode_change
 	private readonly batchSegmentThreshold: number
 
 	constructor(
@@ -59,6 +59,7 @@ export class DirectoryScanner implements IDirectoryScanner {
 		}
 	}
 
+	// kilocode_change start
 	/**
 	 * Request cooperative cancellation of any in-flight scanning work.
 	 * The scanDirectory and batch operations periodically check this flag
@@ -67,9 +68,11 @@ export class DirectoryScanner implements IDirectoryScanner {
 	public cancel(): void {
 		this._cancelled = true
 	}
+
 	public get isCancelled(): boolean {
 		return this._cancelled
 	}
+	// kilocode_change end
 
 	/**
 	 * Recursively scans a directory for code blocks in supported files.
@@ -85,8 +88,10 @@ export class DirectoryScanner implements IDirectoryScanner {
 		onBlocksIndexed?: (indexedCount: number) => void,
 		onFileParsed?: (fileBlockCount: number) => void,
 	): Promise<{ stats: { processed: number; skipped: number }; totalBlockCount: number }> {
+		// kilocode_change start
 		// reset cooperative cancel flag on new full scan
 		this._cancelled = false
+		// kilocode_change end
 
 		const directoryPath = directory
 		// Capture workspace context at scan start
@@ -142,16 +147,22 @@ export class DirectoryScanner implements IDirectoryScanner {
 		// Process all files in parallel with concurrency control
 		const parsePromises = supportedPaths.map((filePath) =>
 			parseLimiter(async () => {
+				// kilocode_change start
 				// Early exit if cancellation requested
 				if (this._cancelled) {
 					return
 				}
+				// kilocode_change end
+
 				try {
 					// Check file size
 					const stats = await stat(filePath)
+					// kilocode_change start
 					if (this._cancelled) {
 						return
 					}
+					// kilocode_change end
+
 					if (stats.size > MAX_FILE_SIZE_BYTES) {
 						skippedCount++ // Skip large files
 						return
@@ -161,9 +172,12 @@ export class DirectoryScanner implements IDirectoryScanner {
 					const content = await vscode.workspace.fs
 						.readFile(vscode.Uri.file(filePath))
 						.then((buffer) => Buffer.from(buffer).toString("utf-8"))
+
+					// kilocode_change start
 					if (this._cancelled) {
 						return
 					}
+					// kilocode_change end
 
 					// Calculate current hash
 					const currentFileHash = createHash("sha256").update(content).digest("hex")
@@ -180,9 +194,13 @@ export class DirectoryScanner implements IDirectoryScanner {
 
 					// File is new or changed - parse it using the injected parser function
 					const blocks = await this.codeParser.parseFile(filePath, { content, fileHash: currentFileHash })
+
+					// kilocode_change start
 					if (this._cancelled) {
 						return
 					}
+					// kilocode_change end
+
 					const fileBlockCount = blocks.length
 					onFileParsed?.(fileBlockCount)
 					processedCount++
@@ -192,15 +210,17 @@ export class DirectoryScanner implements IDirectoryScanner {
 						// Add to batch accumulators
 						let addedBlocksFromFile = false
 						for (const block of blocks) {
-							if (this._cancelled) break
+							if (this._cancelled) break // kilocode_change
 							const trimmedContent = block.content.trim()
 							if (trimmedContent) {
 								const release = await mutex.acquire()
 								try {
+									// kilocode_change start
 									if (this._cancelled) {
 										// Abort adding more items if cancelled
 										break
 									}
+									// kilocode_change end
 									currentBatchBlocks.push(block)
 									currentBatchTexts.push(trimmedContent)
 									addedBlocksFromFile = true
@@ -209,13 +229,16 @@ export class DirectoryScanner implements IDirectoryScanner {
 									if (currentBatchBlocks.length >= this.batchSegmentThreshold) {
 										// Wait if we've reached the maximum pending batches
 										while (pendingBatchCount >= MAX_PENDING_BATCHES) {
-											if (this._cancelled) break
+											if (this._cancelled) break // kilocode_change
 											// Wait for at least one batch to complete
 											await Promise.race(activeBatchPromises)
 										}
+
+										// kilocode_change start
 										if (this._cancelled) {
 											break
 										}
+										// kilocode_change end
 
 										// Copy current batch data and clear accumulators
 										const batchBlocks = [...currentBatchBlocks]
@@ -296,6 +319,7 @@ export class DirectoryScanner implements IDirectoryScanner {
 		await Promise.all(parsePromises)
 
 		// Process any remaining items in batch
+		// kilocode_change: add !this._cancelled
 		if (!this._cancelled && currentBatchBlocks.length > 0) {
 			const release = await mutex.acquire()
 			try {
@@ -326,11 +350,7 @@ export class DirectoryScanner implements IDirectoryScanner {
 			}
 		}
 
-		// Wait for all batch processing to complete (skip if cancelled to return early)
-		if (!this._cancelled) {
-			await Promise.all(activeBatchPromises)
-		}
-
+		// kilocode_change start
 		// Short-circuit if cancelled before handling deletions
 		if (this._cancelled) {
 			return {
@@ -340,7 +360,10 @@ export class DirectoryScanner implements IDirectoryScanner {
 				},
 				totalBlockCount,
 			}
+		} else {
+			await Promise.all(activeBatchPromises)
 		}
+		// kilocode_change end
 
 		// Handle deleted files
 		const oldHashes = this.cacheManager.getAllHashes()
@@ -405,10 +428,10 @@ export class DirectoryScanner implements IDirectoryScanner {
 		onError?: (error: Error) => void,
 		onBlocksIndexed?: (indexedCount: number) => void,
 	): Promise<void> {
+		// kilocode_change start
 		// Respect cooperative cancellation
 		if (this._cancelled || batchBlocks.length === 0) return
 
-		// kilocode_change start
 		if (batchBlocks.length === 0) {
 			console.debug("[DirectoryScanner] Skipping empty batch processing")
 			return
@@ -425,15 +448,15 @@ export class DirectoryScanner implements IDirectoryScanner {
 
 		while (attempts < MAX_BATCH_RETRIES && !success) {
 			attempts++
-      
-			if (this._cancelled) return
 
 			// kilocode_change start
+			if (this._cancelled) return
+
 			console.debug(
 				`[DirectoryScanner] Processing batch attempt ${attempts}/${MAX_BATCH_RETRIES} for ${batchBlocks.length} blocks`,
 			)
 			// kilocode_change end
-      
+
 			try {
 				// --- Deletion Step ---
 				console.debug("[DirectoryScanner] Starting deletion step for modified files") // kilocode_change
@@ -489,7 +512,7 @@ export class DirectoryScanner implements IDirectoryScanner {
 				// --- End Deletion Step ---
 
 				// Create embeddings for batch
-				if (this._cancelled) return
+				if (this._cancelled) return // kilocode_change
 
 				console.debug(`[DirectoryScanner] Creating embeddings for ${batchTexts.length} texts`) // kilocode_change
 
@@ -519,7 +542,7 @@ export class DirectoryScanner implements IDirectoryScanner {
 				console.debug(`[DirectoryScanner] Prepared ${points.length} points for Qdrant`) // kilocode_change
 
 				// Upsert points to Qdrant
-				if (this._cancelled) return
+				if (this._cancelled) return // kilocode_change
 
 				console.debug("[DirectoryScanner] Starting Qdrant upsert") // kilocode_change
 

+ 1 - 1
src/shared/WebviewMessage.ts

@@ -227,7 +227,7 @@ export interface WebviewMessage {
 		| "condenseTaskContextRequest"
 		| "requestIndexingStatus"
 		| "startIndexing"
-		| "cancelIndexing"
+		| "cancelIndexing" // kilocode_change
 		| "clearIndexData"
 		| "indexingStatusUpdate"
 		| "indexCleared"

+ 4 - 0
webview-ui/src/components/chat/CodeIndexPopover.tsx

@@ -482,6 +482,7 @@ export const CodeIndexPopover: React.FC<CodeIndexPopoverProps> = ({
 	// Use the shared ESC key handler hook - respects unsaved changes logic
 	useEscapeKey(open, handlePopoverClose)
 
+	// kilocode_change start
 	const handleCancelIndexing = useCallback(() => {
 		// Optimistically update UI while backend cancels
 		setIndexingStatus((prev) => ({
@@ -490,6 +491,7 @@ export const CodeIndexPopover: React.FC<CodeIndexPopoverProps> = ({
 		}))
 		vscode.postMessage({ type: "cancelIndexing" })
 	}, [t])
+	// kilocode_change end
 
 	const handleSaveSettings = () => {
 		// Validate settings before saving
@@ -1303,6 +1305,7 @@ export const CodeIndexPopover: React.FC<CodeIndexPopoverProps> = ({
 						{/* Action Buttons */}
 						<div className="flex items-center justify-between gap-2 pt-6">
 							<div className="flex gap-2">
+								{/* kilocode_change start */}
 								{currentSettings.codebaseIndexEnabled && indexingStatus.systemStatus === "Indexing" && (
 									<VSCodeButton
 										appearance="secondary"
@@ -1311,6 +1314,7 @@ export const CodeIndexPopover: React.FC<CodeIndexPopoverProps> = ({
 										{t("settings:codeIndex.cancelIndexingButton")}
 									</VSCodeButton>
 								)}
+								{/* kilocode_change end */}
 								{currentSettings.codebaseIndexEnabled &&
 									(indexingStatus.systemStatus === "Error" ||
 										indexingStatus.systemStatus === "Standby") && (