Skip to content

Commit 5222e5e

Browse files
committed
fix: preserve code index and cache after extension updates
- Enhanced cache manager to better handle cache file loading with improved logging - Added comprehensive logging throughout the indexing lifecycle for better debugging - Improved collection detection in QdrantVectorStore to properly reuse existing collections - Added logging to track when existing collections are found and reused vs created new This ensures that the code index is preserved across extension updates instead of starting from scratch each time. Fixes #7088
1 parent dcbb7a6 commit 5222e5e

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

src/services/code-index/cache-manager.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,27 @@ export class CacheManager implements ICacheManager {
3939
try {
4040
const cacheData = await vscode.workspace.fs.readFile(this.cachePath)
4141
this.fileHashes = JSON.parse(cacheData.toString())
42+
console.log(
43+
`[CacheManager] Successfully loaded cache with ${Object.keys(this.fileHashes).length} file hashes from ${this.cachePath.fsPath}`,
44+
)
4245
} catch (error) {
46+
// Check if the error is because the file doesn't exist (expected on first run)
47+
const isFileNotFound =
48+
error instanceof Error && (error.message.includes("FileNotFound") || error.message.includes("ENOENT"))
49+
50+
if (isFileNotFound) {
51+
console.log(
52+
`[CacheManager] Cache file not found at ${this.cachePath.fsPath}, starting with empty cache (this is normal on first run)`,
53+
)
54+
} else {
55+
console.warn(`[CacheManager] Error loading cache from ${this.cachePath.fsPath}:`, error)
56+
TelemetryService.instance.captureEvent(TelemetryEventName.CODE_INDEX_ERROR, {
57+
error: error instanceof Error ? error.message : String(error),
58+
stack: error instanceof Error ? error.stack : undefined,
59+
location: "initialize",
60+
})
61+
}
4362
this.fileHashes = {}
44-
TelemetryService.instance.captureEvent(TelemetryEventName.CODE_INDEX_ERROR, {
45-
error: error instanceof Error ? error.message : String(error),
46-
stack: error instanceof Error ? error.stack : undefined,
47-
location: "initialize",
48-
})
4963
}
5064
}
5165

src/services/code-index/manager.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ export class CodeIndexManager {
117117
* @returns Object indicating if a restart is needed
118118
*/
119119
public async initialize(contextProxy: ContextProxy): Promise<{ requiresRestart: boolean }> {
120+
console.log(`[CodeIndexManager] Initializing for workspace: ${this.workspacePath}`)
121+
120122
// 1. ConfigManager Initialization and Configuration Loading
121123
if (!this._configManager) {
122124
this._configManager = new CodeIndexConfigManager(contextProxy)
@@ -126,6 +128,7 @@ export class CodeIndexManager {
126128

127129
// 2. Check if feature is enabled
128130
if (!this.isFeatureEnabled) {
131+
console.log("[CodeIndexManager] Code indexing feature is disabled")
129132
if (this._orchestrator) {
130133
this._orchestrator.stopWatcher()
131134
}
@@ -135,21 +138,28 @@ export class CodeIndexManager {
135138
// 3. Check if workspace is available
136139
const workspacePath = getWorkspacePath()
137140
if (!workspacePath) {
141+
console.log("[CodeIndexManager] No workspace folder open")
138142
this._stateManager.setSystemState("Standby", "No workspace folder open")
139143
return { requiresRestart }
140144
}
141145

142146
// 4. CacheManager Initialization
143147
if (!this._cacheManager) {
148+
console.log("[CodeIndexManager] Initializing cache manager")
144149
this._cacheManager = new CacheManager(this.context, this.workspacePath)
145150
await this._cacheManager.initialize()
151+
} else {
152+
console.log("[CodeIndexManager] Cache manager already initialized")
146153
}
147154

148155
// 4. Determine if Core Services Need Recreation
149156
const needsServiceRecreation = !this._serviceFactory || requiresRestart
150157

151158
if (needsServiceRecreation) {
159+
console.log(`[CodeIndexManager] Recreating services (requiresRestart: ${requiresRestart})`)
152160
await this._recreateServices()
161+
} else {
162+
console.log("[CodeIndexManager] Services already exist, no recreation needed")
153163
}
154164

155165
// 5. Handle Indexing Start/Restart
@@ -160,7 +170,10 @@ export class CodeIndexManager {
160170
(needsServiceRecreation && (!this._orchestrator || this._orchestrator.state !== "Indexing"))
161171

162172
if (shouldStartOrRestartIndexing) {
173+
console.log("[CodeIndexManager] Starting/restarting indexing process")
163174
this._orchestrator?.startIndexing() // This method is async, but we don't await it here
175+
} else {
176+
console.log(`[CodeIndexManager] No indexing restart needed (current state: ${this._orchestrator?.state})`)
164177
}
165178

166179
return { requiresRestart }

src/services/code-index/orchestrator.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,18 @@ export class CodeIndexOrchestrator {
127127
const collectionCreated = await this.vectorStore.initialize()
128128

129129
if (collectionCreated) {
130+
console.log("[CodeIndexOrchestrator] New collection created, clearing cache file")
130131
await this.cacheManager.clearCacheFile()
132+
} else {
133+
console.log("[CodeIndexOrchestrator] Existing collection found and reused")
134+
// Check if we have cached data
135+
const cachedHashes = this.cacheManager.getAllHashes()
136+
const cachedFileCount = Object.keys(cachedHashes).length
137+
if (cachedFileCount > 0) {
138+
console.log(
139+
`[CodeIndexOrchestrator] Found ${cachedFileCount} files in cache, will skip unchanged files during scan`,
140+
)
141+
}
131142
}
132143

133144
this.stateManager.setSystemState("Indexing", "Services ready. Starting workspace scan...")
@@ -164,6 +175,9 @@ export class CodeIndexOrchestrator {
164175
}
165176

166177
const { stats } = result
178+
console.log(
179+
`[CodeIndexOrchestrator] Initial scan completed. Files processed: ${stats.processed}, Files skipped: ${stats.skipped}, Blocks indexed: ${cumulativeBlocksIndexed}`,
180+
)
167181

168182
// Check if any blocks were actually indexed successfully
169183
// If no blocks were indexed but blocks were found, it means all batches failed

src/services/code-index/vector-store/qdrant-client.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,22 @@ export class QdrantVectorStore implements IVectorStore {
147147
async initialize(): Promise<boolean> {
148148
let created = false
149149
try {
150+
console.log(
151+
`[QdrantVectorStore] Initializing collection ${this.collectionName} with vector size ${this.vectorSize}`,
152+
)
150153
const collectionInfo = await this.getCollectionInfo()
151154

152155
if (collectionInfo === null) {
153156
// Collection info not retrieved (assume not found or inaccessible), create it
157+
console.log(`[QdrantVectorStore] Collection ${this.collectionName} not found, creating new collection`)
154158
await this.client.createCollection(this.collectionName, {
155159
vectors: {
156160
size: this.vectorSize,
157161
distance: this.DISTANCE_METRIC,
158162
},
159163
})
160164
created = true
165+
console.log(`[QdrantVectorStore] Successfully created new collection ${this.collectionName}`)
161166
} else {
162167
// Collection exists, check vector size
163168
const vectorsConfig = collectionInfo.config?.params?.vectors
@@ -177,9 +182,15 @@ export class QdrantVectorStore implements IVectorStore {
177182
}
178183

179184
if (existingVectorSize === this.vectorSize) {
185+
console.log(
186+
`[QdrantVectorStore] Found existing collection ${this.collectionName} with matching vector size ${existingVectorSize}. Reusing existing collection.`,
187+
)
180188
created = false // Exists and correct
181189
} else {
182190
// Exists but wrong vector size, recreate with enhanced error handling
191+
console.log(
192+
`[QdrantVectorStore] Found existing collection ${this.collectionName} with mismatched vector size (expected: ${this.vectorSize}, found: ${existingVectorSize})`,
193+
)
183194
created = await this._recreateCollectionWithNewDimension(existingVectorSize)
184195
}
185196
}

0 commit comments

Comments
 (0)