Skip to content

Commit 79967c0

Browse files
committed
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
1 parent 2484ae2 commit 79967c0

File tree

1 file changed

+94
-5
lines changed

1 file changed

+94
-5
lines changed

packages/mcp/src/snapshot.ts

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -403,16 +403,105 @@ export class SnapshotManager {
403403
* Get codebase status
404404
*/
405405
public getCodebaseStatus(codebasePath: string): 'indexed' | 'indexing' | 'indexfailed' | 'not_found' {
406-
const info = this.codebaseInfoMap.get(codebasePath);
407-
if (!info) return 'not_found';
408-
return info.status;
406+
// Read from JSON file to ensure consistency and persistence
407+
try {
408+
if (!fs.existsSync(this.snapshotFilePath)) {
409+
return 'not_found';
410+
}
411+
412+
const snapshotData = fs.readFileSync(this.snapshotFilePath, 'utf8');
413+
const snapshot: CodebaseSnapshot = JSON.parse(snapshotData);
414+
415+
if (this.isV2Format(snapshot)) {
416+
const info = snapshot.codebases[codebasePath];
417+
if (!info) return 'not_found';
418+
return info.status;
419+
} else {
420+
// V1 format compatibility
421+
const indexedCodebases = snapshot.indexedCodebases || [];
422+
if (indexedCodebases.includes(codebasePath)) {
423+
return 'indexed';
424+
}
425+
426+
// Check indexing codebases (handle both array and object formats)
427+
let indexingCodebases: string[] = [];
428+
if (Array.isArray(snapshot.indexingCodebases)) {
429+
indexingCodebases = snapshot.indexingCodebases;
430+
} else if (snapshot.indexingCodebases && typeof snapshot.indexingCodebases === 'object') {
431+
indexingCodebases = Object.keys(snapshot.indexingCodebases);
432+
}
433+
434+
if (indexingCodebases.includes(codebasePath)) {
435+
return 'indexing';
436+
}
437+
438+
return 'not_found';
439+
}
440+
} catch (error) {
441+
console.warn(`[SNAPSHOT-DEBUG] Error reading codebase status from file for ${codebasePath}:`, error);
442+
// Fallback to memory if file reading fails
443+
const info = this.codebaseInfoMap.get(codebasePath);
444+
if (!info) return 'not_found';
445+
return info.status;
446+
}
409447
}
410448

411449
/**
412450
* Get complete codebase information
413451
*/
414452
public getCodebaseInfo(codebasePath: string): CodebaseInfo | undefined {
415-
return this.codebaseInfoMap.get(codebasePath);
453+
// Read from JSON file to ensure consistency and persistence
454+
try {
455+
if (!fs.existsSync(this.snapshotFilePath)) {
456+
return undefined;
457+
}
458+
459+
const snapshotData = fs.readFileSync(this.snapshotFilePath, 'utf8');
460+
const snapshot: CodebaseSnapshot = JSON.parse(snapshotData);
461+
462+
if (this.isV2Format(snapshot)) {
463+
return snapshot.codebases[codebasePath];
464+
} else {
465+
// V1 format compatibility - construct info from available data
466+
const indexedCodebases = snapshot.indexedCodebases || [];
467+
if (indexedCodebases.includes(codebasePath)) {
468+
const info: CodebaseInfoIndexed = {
469+
status: 'indexed',
470+
indexedFiles: 0, // Unknown in v1 format
471+
totalChunks: 0, // Unknown in v1 format
472+
indexStatus: 'completed',
473+
lastUpdated: new Date().toISOString()
474+
};
475+
return info;
476+
}
477+
478+
// Check indexing codebases
479+
let indexingCodebases: string[] = [];
480+
let progress = 0;
481+
482+
if (Array.isArray(snapshot.indexingCodebases)) {
483+
indexingCodebases = snapshot.indexingCodebases;
484+
} else if (snapshot.indexingCodebases && typeof snapshot.indexingCodebases === 'object') {
485+
indexingCodebases = Object.keys(snapshot.indexingCodebases);
486+
progress = snapshot.indexingCodebases[codebasePath] || 0;
487+
}
488+
489+
if (indexingCodebases.includes(codebasePath)) {
490+
const info: CodebaseInfoIndexing = {
491+
status: 'indexing',
492+
indexingPercentage: progress,
493+
lastUpdated: new Date().toISOString()
494+
};
495+
return info;
496+
}
497+
498+
return undefined;
499+
}
500+
} catch (error) {
501+
console.warn(`[SNAPSHOT-DEBUG] Error reading codebase info from file for ${codebasePath}:`, error);
502+
// Fallback to memory if file reading fails
503+
return this.codebaseInfoMap.get(codebasePath);
504+
}
416505
}
417506

418507
/**
@@ -503,4 +592,4 @@ export class SnapshotManager {
503592
console.error('[SNAPSHOT-DEBUG] Error saving snapshot:', error);
504593
}
505594
}
506-
}
595+
}

0 commit comments

Comments
 (0)