@@ -207,6 +207,9 @@ export class CognitiveMemoryManager implements ICognitiveMemoryManager {
207207 // Cognitive Mechanisms (optional)
208208 private mechanismsEngine : import ( './mechanisms/CognitiveMechanismsEngine.js' ) . CognitiveMechanismsEngine | null = null ;
209209
210+ // Optional neural reranker for post-retrieval quality improvement
211+ private rerankerService : import ( '../rag/reranking/RerankerService.js' ) . RerankerService | null = null ;
212+
210213 /**
211214 * Optional HyDE retriever for hypothesis-driven memory recall.
212215 *
@@ -244,6 +247,11 @@ export class CognitiveMemoryManager implements ICognitiveMemoryManager {
244247 this . store . setBrain ( config . brain ) ;
245248 }
246249
250+ // Optional neural reranker for post-retrieval quality improvement
251+ if ( config . rerankerService ) {
252+ this . rerankerService = config . rerankerService ;
253+ }
254+
247255 // Cognitive working memory (wraps the existing IWorkingMemory)
248256 this . workingMemory = new CognitiveWorkingMemory ( config . workingMemory , {
249257 baseCapacity : config . workingMemoryCapacity ?? 7 ,
@@ -548,6 +556,39 @@ export class CognitiveMemoryManager implements ICognitiveMemoryManager {
548556 }
549557 }
550558
559+ // --- Optional neural reranking ---
560+ // Blends Cohere/LLM-Judge cross-encoder scores with the existing
561+ // cognitive composite. Weight: 0.7 cognitive + 0.3 neural reranker.
562+ // This preserves decay, mood congruence, and graph activation signals
563+ // while boosting semantically relevant results the bi-encoder missed.
564+ if ( this . rerankerService && scored . length > 0 ) {
565+ try {
566+ const rerankerOutput = await this . rerankerService . rerank ( {
567+ query,
568+ documents : scored . map ( ( t ) => ( {
569+ id : t . id ,
570+ content : t . content ,
571+ originalScore : t . retrievalScore ,
572+ } ) ) ,
573+ } , { topN : options . topK ?? 10 } ) ;
574+
575+ const rerankedScores = new Map (
576+ rerankerOutput . results . map ( ( r ) => [ r . id , r . relevanceScore ] )
577+ ) ;
578+
579+ for ( const trace of scored ) {
580+ const neuralScore = rerankedScores . get ( trace . id ) ;
581+ if ( neuralScore !== undefined ) {
582+ trace . retrievalScore = 0.7 * trace . retrievalScore + 0.3 * neuralScore ;
583+ }
584+ }
585+
586+ scored . sort ( ( a , b ) => b . retrievalScore - a . retrievalScore ) ;
587+ } catch {
588+ // Reranking is non-critical — use cognitive scores as-is
589+ }
590+ }
591+
551592 // Record access for retrieved memories (spaced repetition)
552593 for ( const trace of scored . slice ( 0 , 5 ) ) {
553594 await this . store . recordAccess ( trace . id ) ;
0 commit comments