@@ -705,6 +705,23 @@ export abstract class MemoryManagerSyncOps {
705705 log . debug ( "Skipping memory file sync in FTS-only mode (no embedding provider)" ) ;
706706 return ;
707707 }
708+ const selectSourceFileState = this . db . prepare ( `SELECT path, hash FROM files WHERE source = ?` ) ;
709+ const deleteFileByPathAndSource = this . db . prepare (
710+ `DELETE FROM files WHERE path = ? AND source = ?` ,
711+ ) ;
712+ const deleteChunksByPathAndSource = this . db . prepare (
713+ `DELETE FROM chunks WHERE path = ? AND source = ?` ,
714+ ) ;
715+ const deleteVectorRowsByPathAndSource =
716+ this . vector . enabled && this . vector . available
717+ ? this . db . prepare (
718+ `DELETE FROM ${ VECTOR_TABLE } WHERE id IN (SELECT id FROM chunks WHERE path = ? AND source = ?)` ,
719+ )
720+ : null ;
721+ const deleteFtsRowsByPathSourceAndModel =
722+ this . fts . enabled && this . fts . available
723+ ? this . db . prepare ( `DELETE FROM ${ FTS_TABLE } WHERE path = ? AND source = ? AND model = ?` )
724+ : null ;
708725
709726 const files = await listMemoryFiles (
710727 this . workspaceDir ,
@@ -726,6 +743,11 @@ export abstract class MemoryManagerSyncOps {
726743 batch : this . batch . enabled ,
727744 concurrency : this . getIndexConcurrency ( ) ,
728745 } ) ;
746+ const existingRows = selectSourceFileState . all ( "memory" ) as Array < {
747+ path : string ;
748+ hash : string ;
749+ } > ;
750+ const existingHashes = new Map ( existingRows . map ( ( row ) => [ row . path , row . hash ] ) ) ;
729751 const activePaths = new Set ( fileEntries . map ( ( entry ) => entry . path ) ) ;
730752 if ( params . progress ) {
731753 params . progress . total += fileEntries . length ;
@@ -737,10 +759,7 @@ export abstract class MemoryManagerSyncOps {
737759 }
738760
739761 const tasks = fileEntries . map ( ( entry ) => async ( ) => {
740- const record = this . db
741- . prepare ( `SELECT hash FROM files WHERE path = ? AND source = ?` )
742- . get ( entry . path , "memory" ) as { hash : string } | undefined ;
743- if ( ! params . needsFullReindex && record ?. hash === entry . hash ) {
762+ if ( ! params . needsFullReindex && existingHashes . get ( entry . path ) === entry . hash ) {
744763 if ( params . progress ) {
745764 params . progress . completed += 1 ;
746765 params . progress . report ( {
@@ -761,27 +780,20 @@ export abstract class MemoryManagerSyncOps {
761780 } ) ;
762781 await runWithConcurrency ( tasks , this . getIndexConcurrency ( ) ) ;
763782
764- const staleRows = this . db
765- . prepare ( `SELECT path FROM files WHERE source = ?` )
766- . all ( "memory" ) as Array < { path : string } > ;
767- for ( const stale of staleRows ) {
783+ for ( const stale of existingRows ) {
768784 if ( activePaths . has ( stale . path ) ) {
769785 continue ;
770786 }
771- this . db . prepare ( `DELETE FROM files WHERE path = ? AND source = ?` ) . run ( stale . path , "memory" ) ;
772- try {
773- this . db
774- . prepare (
775- `DELETE FROM ${ VECTOR_TABLE } WHERE id IN (SELECT id FROM chunks WHERE path = ? AND source = ?)` ,
776- )
777- . run ( stale . path , "memory" ) ;
778- } catch { }
779- this . db . prepare ( `DELETE FROM chunks WHERE path = ? AND source = ?` ) . run ( stale . path , "memory" ) ;
780- if ( this . fts . enabled && this . fts . available ) {
787+ deleteFileByPathAndSource . run ( stale . path , "memory" ) ;
788+ if ( deleteVectorRowsByPathAndSource ) {
789+ try {
790+ deleteVectorRowsByPathAndSource . run ( stale . path , "memory" ) ;
791+ } catch { }
792+ }
793+ deleteChunksByPathAndSource . run ( stale . path , "memory" ) ;
794+ if ( deleteFtsRowsByPathSourceAndModel ) {
781795 try {
782- this . db
783- . prepare ( `DELETE FROM ${ FTS_TABLE } WHERE path = ? AND source = ? AND model = ?` )
784- . run ( stale . path , "memory" , this . provider . model ) ;
796+ deleteFtsRowsByPathSourceAndModel . run ( stale . path , "memory" , this . provider . model ) ;
785797 } catch { }
786798 }
787799 }
@@ -797,6 +809,24 @@ export abstract class MemoryManagerSyncOps {
797809 log . debug ( "Skipping session file sync in FTS-only mode (no embedding provider)" ) ;
798810 return ;
799811 }
812+ const selectFileHash = this . db . prepare ( `SELECT hash FROM files WHERE path = ? AND source = ?` ) ;
813+ const selectSourceFileState = this . db . prepare ( `SELECT path, hash FROM files WHERE source = ?` ) ;
814+ const deleteFileByPathAndSource = this . db . prepare (
815+ `DELETE FROM files WHERE path = ? AND source = ?` ,
816+ ) ;
817+ const deleteChunksByPathAndSource = this . db . prepare (
818+ `DELETE FROM chunks WHERE path = ? AND source = ?` ,
819+ ) ;
820+ const deleteVectorRowsByPathAndSource =
821+ this . vector . enabled && this . vector . available
822+ ? this . db . prepare (
823+ `DELETE FROM ${ VECTOR_TABLE } WHERE id IN (SELECT id FROM chunks WHERE path = ? AND source = ?)` ,
824+ )
825+ : null ;
826+ const deleteFtsRowsByPathSourceAndModel =
827+ this . fts . enabled && this . fts . available
828+ ? this . db . prepare ( `DELETE FROM ${ FTS_TABLE } WHERE path = ? AND source = ? AND model = ?` )
829+ : null ;
800830
801831 const targetSessionFiles = params . needsFullReindex
802832 ? null
@@ -807,6 +837,15 @@ export abstract class MemoryManagerSyncOps {
807837 const activePaths = targetSessionFiles
808838 ? null
809839 : new Set ( files . map ( ( file ) => sessionPathForFile ( file ) ) ) ;
840+ const existingRows =
841+ activePaths === null
842+ ? null
843+ : ( selectSourceFileState . all ( "sessions" ) as Array < {
844+ path : string ;
845+ hash : string ;
846+ } > ) ;
847+ const existingHashes =
848+ existingRows === null ? null : new Map ( existingRows . map ( ( row ) => [ row . path , row . hash ] ) ) ;
810849 const indexAll =
811850 params . needsFullReindex || Boolean ( targetSessionFiles ) || this . sessionsDirtyFiles . size === 0 ;
812851 log . debug ( "memory sync: indexing session files" , {
@@ -848,10 +887,16 @@ export abstract class MemoryManagerSyncOps {
848887 }
849888 return ;
850889 }
851- const record = this . db
852- . prepare ( `SELECT hash FROM files WHERE path = ? AND source = ?` )
853- . get ( entry . path , "sessions" ) as { hash : string } | undefined ;
854- if ( ! params . needsFullReindex && record ?. hash === entry . hash ) {
890+ const existingHash =
891+ existingHashes ?. get ( entry . path ) ??
892+ (
893+ selectFileHash . get ( entry . path , "sessions" ) as
894+ | {
895+ hash : string ;
896+ }
897+ | undefined
898+ ) ?. hash ;
899+ if ( ! params . needsFullReindex && existingHash === entry . hash ) {
855900 if ( params . progress ) {
856901 params . progress . completed += 1 ;
857902 params . progress . report ( {
@@ -880,31 +925,20 @@ export abstract class MemoryManagerSyncOps {
880925 return ;
881926 }
882927
883- const staleRows = this . db
884- . prepare ( `SELECT path FROM files WHERE source = ?` )
885- . all ( "sessions" ) as Array < { path : string } > ;
886- for ( const stale of staleRows ) {
928+ for ( const stale of existingRows ?? [ ] ) {
887929 if ( activePaths . has ( stale . path ) ) {
888930 continue ;
889931 }
890- this . db
891- . prepare ( `DELETE FROM files WHERE path = ? AND source = ?` )
892- . run ( stale . path , "sessions" ) ;
893- try {
894- this . db
895- . prepare (
896- `DELETE FROM ${ VECTOR_TABLE } WHERE id IN (SELECT id FROM chunks WHERE path = ? AND source = ?)` ,
897- )
898- . run ( stale . path , "sessions" ) ;
899- } catch { }
900- this . db
901- . prepare ( `DELETE FROM chunks WHERE path = ? AND source = ?` )
902- . run ( stale . path , "sessions" ) ;
903- if ( this . fts . enabled && this . fts . available ) {
932+ deleteFileByPathAndSource . run ( stale . path , "sessions" ) ;
933+ if ( deleteVectorRowsByPathAndSource ) {
934+ try {
935+ deleteVectorRowsByPathAndSource . run ( stale . path , "sessions" ) ;
936+ } catch { }
937+ }
938+ deleteChunksByPathAndSource . run ( stale . path , "sessions" ) ;
939+ if ( deleteFtsRowsByPathSourceAndModel ) {
904940 try {
905- this . db
906- . prepare ( `DELETE FROM ${ FTS_TABLE } WHERE path = ? AND source = ? AND model = ?` )
907- . run ( stale . path , "sessions" , this . provider . model ) ;
941+ deleteFtsRowsByPathSourceAndModel . run ( stale . path , "sessions" , this . provider . model ) ;
908942 } catch { }
909943 }
910944 }
0 commit comments