From 79967c0cbe2cf2a4f66fe9660f8c70d39f9cef40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Manzaba?= Date: Thu, 18 Sep 2025 16:51:58 -0500 Subject: [PATCH] feat(mcp): implement persistent codebase status and info retrieval from file - add file-based persistence for codebase status and information - support both v1 and v2 snapshot formats for backward compatibility - fallback to memory if file reading fails - improve error handling and logging for snapshot operations --- packages/mcp/src/snapshot.ts | 99 ++++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 5 deletions(-) diff --git a/packages/mcp/src/snapshot.ts b/packages/mcp/src/snapshot.ts index 81982b7b..ebfb462d 100644 --- a/packages/mcp/src/snapshot.ts +++ b/packages/mcp/src/snapshot.ts @@ -403,16 +403,105 @@ export class SnapshotManager { * Get codebase status */ public getCodebaseStatus(codebasePath: string): 'indexed' | 'indexing' | 'indexfailed' | 'not_found' { - const info = this.codebaseInfoMap.get(codebasePath); - if (!info) return 'not_found'; - return info.status; + // Read from JSON file to ensure consistency and persistence + try { + if (!fs.existsSync(this.snapshotFilePath)) { + return 'not_found'; + } + + const snapshotData = fs.readFileSync(this.snapshotFilePath, 'utf8'); + const snapshot: CodebaseSnapshot = JSON.parse(snapshotData); + + if (this.isV2Format(snapshot)) { + const info = snapshot.codebases[codebasePath]; + if (!info) return 'not_found'; + return info.status; + } else { + // V1 format compatibility + const indexedCodebases = snapshot.indexedCodebases || []; + if (indexedCodebases.includes(codebasePath)) { + return 'indexed'; + } + + // Check indexing codebases (handle both array and object formats) + let indexingCodebases: string[] = []; + if (Array.isArray(snapshot.indexingCodebases)) { + indexingCodebases = snapshot.indexingCodebases; + } else if (snapshot.indexingCodebases && typeof snapshot.indexingCodebases === 'object') { + indexingCodebases = Object.keys(snapshot.indexingCodebases); + } + + if (indexingCodebases.includes(codebasePath)) { + return 'indexing'; + } + + return 'not_found'; + } + } catch (error) { + console.warn(`[SNAPSHOT-DEBUG] Error reading codebase status from file for ${codebasePath}:`, error); + // Fallback to memory if file reading fails + const info = this.codebaseInfoMap.get(codebasePath); + if (!info) return 'not_found'; + return info.status; + } } /** * Get complete codebase information */ public getCodebaseInfo(codebasePath: string): CodebaseInfo | undefined { - return this.codebaseInfoMap.get(codebasePath); + // Read from JSON file to ensure consistency and persistence + try { + if (!fs.existsSync(this.snapshotFilePath)) { + return undefined; + } + + const snapshotData = fs.readFileSync(this.snapshotFilePath, 'utf8'); + const snapshot: CodebaseSnapshot = JSON.parse(snapshotData); + + if (this.isV2Format(snapshot)) { + return snapshot.codebases[codebasePath]; + } else { + // V1 format compatibility - construct info from available data + const indexedCodebases = snapshot.indexedCodebases || []; + if (indexedCodebases.includes(codebasePath)) { + const info: CodebaseInfoIndexed = { + status: 'indexed', + indexedFiles: 0, // Unknown in v1 format + totalChunks: 0, // Unknown in v1 format + indexStatus: 'completed', + lastUpdated: new Date().toISOString() + }; + return info; + } + + // Check indexing codebases + let indexingCodebases: string[] = []; + let progress = 0; + + if (Array.isArray(snapshot.indexingCodebases)) { + indexingCodebases = snapshot.indexingCodebases; + } else if (snapshot.indexingCodebases && typeof snapshot.indexingCodebases === 'object') { + indexingCodebases = Object.keys(snapshot.indexingCodebases); + progress = snapshot.indexingCodebases[codebasePath] || 0; + } + + if (indexingCodebases.includes(codebasePath)) { + const info: CodebaseInfoIndexing = { + status: 'indexing', + indexingPercentage: progress, + lastUpdated: new Date().toISOString() + }; + return info; + } + + return undefined; + } + } catch (error) { + console.warn(`[SNAPSHOT-DEBUG] Error reading codebase info from file for ${codebasePath}:`, error); + // Fallback to memory if file reading fails + return this.codebaseInfoMap.get(codebasePath); + } } /** @@ -503,4 +592,4 @@ export class SnapshotManager { console.error('[SNAPSHOT-DEBUG] Error saving snapshot:', error); } } -} \ No newline at end of file +}