@@ -252,6 +252,8 @@ const indexState: IndexState = {
252252 status : 'idle'
253253} ;
254254
255+ let autoRefreshQueued = false ;
256+
255257const server : Server = new Server (
256258 {
257259 name : 'codebase-context' ,
@@ -512,7 +514,7 @@ async function extractGitMemories(): Promise<number> {
512514 return added ;
513515}
514516
515- async function performIndexing ( incrementalOnly ? : boolean ) : Promise < void > {
517+ async function performIndexingOnce ( incrementalOnly ? : boolean ) : Promise < void > {
516518 indexState . status = 'indexing' ;
517519 const mode = incrementalOnly ? 'incremental' : 'full' ;
518520 console . error ( `Indexing (${ mode } ): ${ ROOT_PATH } ` ) ;
@@ -566,6 +568,22 @@ async function performIndexing(incrementalOnly?: boolean): Promise<void> {
566568 }
567569}
568570
571+ async function performIndexing ( incrementalOnly ?: boolean ) : Promise < void > {
572+ let nextMode = incrementalOnly ;
573+ for ( ; ; ) {
574+ await performIndexingOnce ( nextMode ) ;
575+
576+ const shouldRunQueuedRefresh = autoRefreshQueued && indexState . status === 'ready' ;
577+ autoRefreshQueued = false ;
578+ if ( ! shouldRunQueuedRefresh ) return ;
579+
580+ if ( process . env . CODEBASE_CONTEXT_DEBUG ) {
581+ console . error ( '[file-watcher] Running queued auto-refresh' ) ;
582+ }
583+ nextMode = true ;
584+ }
585+ }
586+
569587async function shouldReindex ( ) : Promise < boolean > {
570588 const indexPath = PATHS . keywordIndex ;
571589 try {
@@ -735,13 +753,16 @@ async function main() {
735753 debounceMs,
736754 onChanged : ( ) => {
737755 if ( indexState . status === 'indexing' ) {
756+ autoRefreshQueued = true ;
738757 if ( process . env . CODEBASE_CONTEXT_DEBUG ) {
739- console . error ( '[file-watcher] Index in progress — skipping auto-refresh' ) ;
758+ console . error ( '[file-watcher] Index in progress — queueing auto-refresh' ) ;
740759 }
741760 return ;
742761 }
743- console . error ( '[file-watcher] Changes detected — incremental reindex starting' ) ;
744- performIndexing ( true ) ;
762+ if ( process . env . CODEBASE_CONTEXT_DEBUG ) {
763+ console . error ( '[file-watcher] Changes detected — incremental reindex starting' ) ;
764+ }
765+ void performIndexing ( true ) ;
745766 }
746767 } ) ;
747768
0 commit comments