@@ -20,12 +20,13 @@ import {
2020 Resource
2121} from '@modelcontextprotocol/sdk/types.js' ;
2222import { CodebaseIndexer } from './core/indexer.js' ;
23- import { IndexingStats } from './types/index.js' ;
23+ import { IndexingStats , SearchResult } from './types/index.js' ;
2424import { CodebaseSearcher } from './core/search.js' ;
2525import { analyzerRegistry } from './core/analyzer-registry.js' ;
2626import { AngularAnalyzer } from './analyzers/angular/index.js' ;
2727import { GenericAnalyzer } from './analyzers/generic/index.js' ;
2828import { InternalFileGraph } from './utils/usage-tracker.js' ;
29+ import { IndexCorruptedError } from './errors/index.js' ;
2930
3031analyzerRegistry . register ( new AngularAnalyzer ( ) ) ;
3132analyzerRegistry . register ( new GenericAnalyzer ( ) ) ;
@@ -62,11 +63,10 @@ const indexState: IndexState = {
6263 status : 'idle'
6364} ;
6465
65-
6666const server : Server = new Server (
6767 {
6868 name : 'codebase-context' ,
69- version : '1.3.0 '
69+ version : '1.3.1 '
7070 } ,
7171 {
7272 capabilities : {
@@ -492,7 +492,62 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
492492 }
493493
494494 const searcher = new CodebaseSearcher ( ROOT_PATH ) ;
495- const results = await searcher . search ( query , limit || 5 , filters ) ;
495+ let results : SearchResult [ ] ;
496+
497+ try {
498+ results = await searcher . search ( query , limit || 5 , filters ) ;
499+ } catch ( error ) {
500+ if ( error instanceof IndexCorruptedError ) {
501+ console . error ( '[Auto-Heal] Index corrupted. Triggering full re-index...' ) ;
502+
503+ await performIndexing ( ) ;
504+
505+ if ( indexState . status === 'ready' ) {
506+ console . error ( '[Auto-Heal] Success. Retrying search...' ) ;
507+ const freshSearcher = new CodebaseSearcher ( ROOT_PATH ) ;
508+ try {
509+ results = await freshSearcher . search ( query , limit || 5 , filters ) ;
510+ } catch ( retryError ) {
511+ return {
512+ content : [
513+ {
514+ type : 'text' ,
515+ text : JSON . stringify (
516+ {
517+ status : 'error' ,
518+ message : `Auto-heal retry failed: ${
519+ retryError instanceof Error ? retryError . message : String ( retryError )
520+ } `
521+ } ,
522+ null ,
523+ 2
524+ )
525+ }
526+ ]
527+ } ;
528+ }
529+ } else {
530+ return {
531+ content : [
532+ {
533+ type : 'text' ,
534+ text : JSON . stringify (
535+ {
536+ status : 'error' ,
537+ message : `Auto-heal failed: Indexing ended with status '${ indexState . status } '` ,
538+ error : indexState . error
539+ } ,
540+ null ,
541+ 2
542+ )
543+ }
544+ ]
545+ } ;
546+ }
547+ } else {
548+ throw error ; // Propagate unexpected errors
549+ }
550+ }
496551
497552 return {
498553 content : [
@@ -538,19 +593,19 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
538593 lastIndexed : indexState . lastIndexed ?. toISOString ( ) ,
539594 stats : indexState . stats
540595 ? {
541- totalFiles : indexState . stats . totalFiles ,
542- indexedFiles : indexState . stats . indexedFiles ,
543- totalChunks : indexState . stats . totalChunks ,
544- duration : `${ ( indexState . stats . duration / 1000 ) . toFixed ( 2 ) } s`
545- }
596+ totalFiles : indexState . stats . totalFiles ,
597+ indexedFiles : indexState . stats . indexedFiles ,
598+ totalChunks : indexState . stats . totalChunks ,
599+ duration : `${ ( indexState . stats . duration / 1000 ) . toFixed ( 2 ) } s`
600+ }
546601 : undefined ,
547602 progress : progress
548603 ? {
549- phase : progress . phase ,
550- percentage : progress . percentage ,
551- filesProcessed : progress . filesProcessed ,
552- totalFiles : progress . totalFiles
553- }
604+ phase : progress . phase ,
605+ percentage : progress . percentage ,
606+ filesProcessed : progress . filesProcessed ,
607+ totalFiles : progress . totalFiles
608+ }
554609 : undefined ,
555610 error : indexState . error ,
556611 hint : 'Use refresh_index to manually trigger re-indexing when needed.'
0 commit comments