Skip to content

Commit ee11490

Browse files
committed
feat: add optional reranker stage to CognitiveMemoryManager.retrieve()
1 parent fb92343 commit ee11490

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/memory/CognitiveMemoryManager.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/memory/core/config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,22 @@ export interface CognitiveMemoryConfig {
271271
* @see {@link SqliteBrain} — the cross-platform persistence layer
272272
*/
273273
brain?: import('../retrieval/store/SqliteBrain.js').SqliteBrain;
274+
275+
/**
276+
* Optional reranker service for post-retrieval quality improvement.
277+
*
278+
* When provided, retrieved memory traces are reranked after the
279+
* cognitive scoring pipeline (vector similarity + strength + recency +
280+
* emotional congruence + graph activation + importance). The reranker
281+
* score is blended with the existing composite score at a 0.7/0.3
282+
* weighting to preserve cognitive signals while boosting semantically
283+
* relevant results.
284+
*
285+
* Recommended: Cohere rerank-v3.5 primary, LLM-Judge fallback.
286+
*
287+
* @default undefined (no reranking)
288+
*/
289+
rerankerService?: import('../../rag/reranking/RerankerService.js').RerankerService;
274290
}
275291

276292
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)