/** * SmartRetrieval — LongMemEval-derived retrieval pipeline (ADR-090) * * Wraps a raw HNSW `SearchFn` with the optimizations identified by the * ADR-088 LongMemEval benchmark: * * 1. Query expansion (template-based, no LLM) * 2. Multi-query fan-out + Reciprocal Rank Fusion * 3. Recency boost from metadata timestamps * 4. MMR diversity re-ranking (token-Jaccard proxy) * 5. Session round-robin for multi-session coverage * * The module is pluggable: callers provide a `SearchFn` that hits whatever * raw store they use (AgentDB HNSW, sql.js, a test fake, etc.). That keeps * `@claude-flow/memory` free of a hard dependency on the CLI's memory-initializer * and makes the pipeline easy to benchmark in isolation. */ export interface SearchCandidate { id: string; key: string; content: string; score: number; namespace: string; /** Optional metadata pulled through from the underlying store. */ metadata?: Record; /** Optional unix-ms timestamp used by the recency booster. */ createdAt?: number; /** Optional unix-ms timestamp; preferred over createdAt when present. */ updatedAt?: number; } export interface RawSearchRequest { query: string; namespace?: string; limit?: number; threshold?: number; } export interface RawSearchResponse { results: SearchCandidate[]; } /** Pluggable raw search function — typically wraps HNSW or a test fake. */ export type SearchFn = (req: RawSearchRequest) => Promise; export interface SmartSearchOptions { query: string; namespace?: string; /** Final number of results to return (default 10). */ limit?: number; /** Similarity floor applied to the raw store (default 0.3). */ threshold?: number; /** Fan out 2-3 expanded query variants and fuse with RRF. Default: true. */ multiQuery?: boolean; /** Re-score with recency boost using entry timestamps. Default: true. */ recencyBoost?: boolean; /** Apply MMR diversity re-ranking. Default: true. */ diversityMMR?: boolean; /** Round-robin across distinct session_ids. Default: true. */ sessionDiversity?: boolean; /** How many candidates to pull from the raw store per variant (default limit × 3). */ fanOutK?: number; /** RRF constant; 60 is the standard default. */ rrfK?: number; /** Recency half-life in days; older entries decay past this. Default 30. */ recencyHalfLifeDays?: number; /** Max recency multiplier applied to top of the curve. Default 0.2. */ recencyWeight?: number; /** MMR tradeoff λ — 1.0 = pure relevance, 0.0 = pure diversity. Default 0.7. */ mmrLambda?: number; /** Metadata key that identifies a session for round-robin. Default 'session_id'. */ sessionKey?: string; /** "Now" for recency decay — pass a fixed value in tests for determinism. */ now?: number; /** Override the default template-based expansions with your own set. */ queryExpansions?: (query: string) => string[]; } export interface SmartSearchStats { variantCount: number; variants: string[]; rawCandidateCount: number; afterRrfCount: number; afterRecencyCount: number; afterMmrCount: number; afterSessionCount: number; durationMs: number; } export interface SmartSearchResult { results: SearchCandidate[]; stats: SmartSearchStats; } /** * Default query expansion set. Keeps variants cheap (≤3) so we only pay * ~3× the HNSW cost on the hot path. */ export declare function defaultQueryExpansions(query: string): string[]; /** * Public-facing Reciprocal Rank Fusion. Takes any number of pre-sorted * candidate lists (best-first) and fuses them with the RRF score * `sum(1 / (k + rank_i))`. Used by the ADR-125 Phase 5 hybridSearch * controller to fuse dense + sparse arms. */ export declare function applyRRF(rankedLists: T[][], k?: number): Array<{ candidate: T; score: number; }>; /** * Public-facing MMR rerank. * * Re-exported as `applyMMR` for ADR-125 Phase 5 callers (the hybridSearch * controller). Takes already-scored candidates plus an MMR lambda * (1.0 = pure relevance, 0.0 = pure diversity). */ export declare function applyMMR(scored: Array<{ candidate: T; score: number; }>, lambda?: number, limit?: number): Array<{ candidate: T; score: number; }>; export declare function smartSearch(search: SearchFn, opts: SmartSearchOptions): Promise; //# sourceMappingURL=smart-retrieval.d.ts.map