Skip to content

Commit 313e586

Browse files
committed
feat(rag): wire HyDE retrieval into RetrievalAugmentor, multimodal search, and memory recall
HyDE (Hypothetical Document Embedding) was fully implemented as a standalone HydeRetriever class but never called in any actual retrieval pipeline. Wire it into three pipelines: 1. RetrievalAugmentor.retrieveContext() — main RAG pipeline - Add setHydeLlmCaller() for LLM registration - When options.hyde.enabled, generate hypothesis and embed it instead of the raw query - Support pre-supplied hypotheses (skip LLM call) - Graceful fallback to direct embedding on any failure - HyDE diagnostics in RagRetrievalDiagnostics - Audit trail integration (operation type 'hyde') 2. MultimodalIndexer.search() — cross-modal search - Add setHydeRetriever() to attach a HyDE retriever - When opts.hyde.enabled, delegate to HydeRetriever.retrieve() with adaptive thresholding - Support pre-supplied hypotheses and modality filtering 3. CognitiveMemoryManager.retrieve() — memory recall - Add setHydeRetriever() / getHydeRetriever() - When options.hyde is true, generate a hypothetical memory trace and search using that instead of the raw query - Non-critical: failures fall back silently to raw query Also: - Add 'hyde' to RAGOperationEntry.operationType union - Add hydeDetails to RAGOperationEntry for audit transparency - Add setHydeDetails() to RAGOperationHandle - Add hyde diagnostics to RagRetrievalDiagnostics - Add hyde option to CognitiveRetrievalOptions - Add hyde option to MultimodalSearchOptions - 10 new tests covering all HyDE integration paths
1 parent 7b80ed6 commit 313e586

10 files changed

Lines changed: 740 additions & 32 deletions

src/memory/CognitiveMemoryManager.ts

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ import { ContextWindowManager } from './context/ContextWindowManager.js';
6161
import type { ContextMessage, CompactionEntry } from './context/types.js';
6262
import type { ContextWindowStats } from './context/ContextWindowManager.js';
6363

64+
// HyDE (Hypothetical Document Embedding) for improved memory retrieval
65+
import type { HydeRetriever } from '../rag/HydeRetriever.js';
66+
6467
// ---------------------------------------------------------------------------
6568
// Interface
6669
// ---------------------------------------------------------------------------
@@ -151,6 +154,15 @@ export interface ICognitiveMemoryManager {
151154
/** Get prospective-memory manager when enabled. */
152155
getProspective(): ProspectiveMemoryManager | null;
153156

157+
/**
158+
* Attach a HyDE retriever for hypothesis-driven memory recall.
159+
* Pass `null` to disable.
160+
*/
161+
setHydeRetriever?(retriever: HydeRetriever | null): void;
162+
163+
/** Get the HyDE retriever if configured, or `null`. */
164+
getHydeRetriever?(): HydeRetriever | null;
165+
154166
/** Get infinite-context runtime stats when enabled. */
155167
getContextWindowStats(): ContextWindowStats | null;
156168

@@ -191,6 +203,16 @@ export class CognitiveMemoryManager implements ICognitiveMemoryManager {
191203
// Batch 3: Infinite Context (optional)
192204
private contextWindow: ContextWindowManager | null = null;
193205

206+
/**
207+
* Optional HyDE retriever for hypothesis-driven memory recall.
208+
*
209+
* When set and `options.hyde` is `true` on a `retrieve()` call, the manager
210+
* generates a hypothetical memory trace via LLM and uses that text for the
211+
* embedding-based memory search. This improves recall for vague or abstract
212+
* queries (e.g. "that deployment discussion last week").
213+
*/
214+
private hydeRetriever: HydeRetriever | null = null;
215+
194216
async initialize(config: CognitiveMemoryConfig): Promise<void> {
195217
this.config = config;
196218

@@ -405,7 +427,26 @@ export class CognitiveMemoryManager implements ICognitiveMemoryManager {
405427

406428
const startTime = Date.now();
407429

408-
const { scored, partial } = await this.store.query(query, mood, options);
430+
// When HyDE is enabled and a retriever is available, generate a
431+
// hypothetical memory trace and use it as the search query. The
432+
// hypothesis is a plausible memory that the agent *would* have stored,
433+
// producing an embedding that's semantically closer to actual stored
434+
// traces than the raw recall query.
435+
let effectiveQuery = query;
436+
if (options.hyde && this.hydeRetriever) {
437+
try {
438+
const hypoResult = await this.hydeRetriever.generateHypothesis(
439+
`Recall a memory about: ${query}`,
440+
);
441+
if (hypoResult.hypothesis) {
442+
effectiveQuery = hypoResult.hypothesis;
443+
}
444+
} catch {
445+
// HyDE generation is non-critical — fall through to raw query.
446+
}
447+
}
448+
449+
const { scored, partial } = await this.store.query(effectiveQuery, mood, options);
409450

410451
// --- Batch 2: Spreading activation ---
411452
if (this.graph && scored.length > 0) {
@@ -805,6 +846,35 @@ export class CognitiveMemoryManager implements ICognitiveMemoryManager {
805846
return this.prospective;
806847
}
807848

849+
/**
850+
* Attach a HyDE retriever to enable hypothesis-driven memory recall.
851+
*
852+
* When set, the `retrieve()` and `assembleForPrompt()` methods can accept
853+
* `options.hyde = true` to generate a hypothetical memory trace before
854+
* searching. This improves recall for vague or abstract queries by
855+
* producing embeddings that are semantically closer to stored traces.
856+
*
857+
* @param retriever - A pre-configured HydeRetriever instance, or `null`
858+
* to disable HyDE.
859+
*
860+
* @example
861+
* ```typescript
862+
* memoryManager.setHydeRetriever(new HydeRetriever({
863+
* llmCaller: myLlmCaller,
864+
* embeddingManager: myEmbeddingManager,
865+
* config: { enabled: true },
866+
* }));
867+
* ```
868+
*/
869+
setHydeRetriever(retriever: HydeRetriever | null): void {
870+
this.hydeRetriever = retriever;
871+
}
872+
873+
/** Get the HyDE retriever if configured, or `null`. */
874+
getHydeRetriever(): HydeRetriever | null {
875+
return this.hydeRetriever;
876+
}
877+
808878
// =========================================================================
809879
// Internal
810880
// =========================================================================

src/memory/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,21 @@ export interface CognitiveRetrievalOptions {
188188
timeRange?: { after?: number; before?: number };
189189
/** If true, skip emotional congruence bias (useful for factual lookups). */
190190
neutralMood?: boolean;
191+
/**
192+
* Enable HyDE (Hypothetical Document Embedding) for memory retrieval.
193+
*
194+
* When `true` and a HyDE retriever is configured on the memory manager,
195+
* the system generates a hypothetical memory trace matching the query
196+
* before embedding. This produces embeddings that are closer to actual
197+
* stored memories, improving recall — especially for vague or abstract
198+
* recall prompts (e.g. "that thing we discussed about deployment").
199+
*
200+
* Adds one LLM call per retrieval. Use for important lookups where
201+
* recall quality matters more than latency.
202+
*
203+
* @default false
204+
*/
205+
hyde?: boolean;
191206
}
192207

193208
export interface ScoredMemoryTrace extends MemoryTrace {

src/rag/IRetrievalAugmentor.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ export interface RagRetrievalDiagnostics {
134134
dataSourceHits?: Record<string, number>;
135135
effectiveDataSourceIds?: string[];
136136
messages?: string[];
137+
/**
138+
* HyDE-specific diagnostics, populated when HyDE retrieval is active.
139+
*
140+
* - `hypothesis`: The generated (or pre-supplied) hypothetical answer.
141+
* - `hypothesisLatencyMs`: Time spent generating the hypothesis via LLM.
142+
* - `effectiveThreshold`: Final similarity threshold after adaptive stepping.
143+
* - `thresholdSteps`: Number of times the threshold was lowered before results
144+
* were found (0 means the initial threshold succeeded).
145+
*/
146+
hyde?: {
147+
hypothesis: string;
148+
hypothesisLatencyMs: number;
149+
effectiveThreshold: number;
150+
thresholdSteps: number;
151+
};
137152
}
138153

139154
/**

0 commit comments

Comments
 (0)