do-memory-core 0.1.34

Core episodic learning system for AI agents with pattern extraction, reward scoring, and dual storage backend
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Cascading retrieval pipeline (WG-131).
//!
//! Implements a 4-tier retrieval cascade:
//! 1. BM25 keyword index (CPU-local, no API calls)
//! 2. HDC hyperdimensional encoding (CPU-local, no API calls)
//! 3. ConceptGraph ontology expansion (CPU-local, no API calls)
//! 4. API embedding fallback (external API call)
//!
//! The cascade eliminates 50-70% of embedding API calls by satisfying
//! queries from CPU-local tiers before falling back to the API.

mod concept_graph;
pub use concept_graph::ConceptGraph;

mod types;
pub use types::{CascadeConfig, CascadeResult, TierResult};

/// Cascading retrieval orchestrator.
///
/// Coordinates the 4-tier retrieval pipeline, falling back to API
/// only when CPU-local tiers cannot satisfy the query.
pub struct CascadeRetriever {
    config: CascadeConfig,
    /// Episode data indexed for retrieval (id -> text).
    episode_data: Vec<(String, String)>,
    /// Concept graph for ontology-based term expansion (Tier 3).
    #[cfg(feature = "csm")]
    concept_graph: ConceptGraph,
    #[cfg(feature = "csm")]
    bm25_index: super::Bm25Index,
    #[cfg(feature = "csm")]
    hdc_encoder: super::HdcEncoder,
    #[cfg(feature = "csm")]
    hdc_vectors: Vec<(String, super::HVec10240)>,
}

impl CascadeRetriever {
    /// Create a new cascade retriever with given configuration.
    pub fn new(config: CascadeConfig) -> Self {
        Self {
            config,
            episode_data: Vec::new(),
            #[cfg(feature = "csm")]
            concept_graph: ConceptGraph::from_embedded(),
            #[cfg(feature = "csm")]
            bm25_index: super::Bm25Index::new(),
            #[cfg(feature = "csm")]
            hdc_encoder: super::HdcEncoder::new(),
            #[cfg(feature = "csm")]
            hdc_vectors: Vec::new(),
        }
    }

    /// Create a new cascade retriever with default configuration.
    #[must_use]
    pub fn default_config() -> Self {
        Self::new(CascadeConfig::default())
    }

    /// Tokenize text for BM25 indexing/search.
    #[cfg(feature = "csm")]
    fn tokenize(text: &str) -> Vec<String> {
        // Use default tokenization: not code-aware, lowercase enabled
        super::HdcEncoder::tokenize(text, false, true)
    }

    /// Add an episode to the retrieval index.
    ///
    /// This indexes the episode in BM25 and encodes it for HDC similarity search.
    /// When the `csm` feature is not enabled, this just stores the episode data
    /// for later retrieval.
    pub fn add_episode(&mut self, id: &str, text: &str) {
        self.episode_data.push((id.to_string(), text.to_string()));

        #[cfg(feature = "csm")]
        {
            // Tokenize and add to BM25 index
            let tokens = Self::tokenize(text);
            self.bm25_index.add_document(id, &tokens);

            // Encode and store HDC vector
            let hdc_vector = self.hdc_encoder.encode(text);
            self.hdc_vectors.push((id.to_string(), hdc_vector));
        }
    }

    /// Clear all indexed episodes.
    pub fn clear(&mut self) {
        self.episode_data.clear();

        #[cfg(feature = "csm")]
        {
            self.bm25_index.clear();
            self.hdc_vectors.clear();
        }
    }

    /// Get the number of indexed episodes.
    #[must_use]
    pub fn len(&self) -> usize {
        self.episode_data.len()
    }

    /// Check if the index is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.episode_data.is_empty()
    }

    /// Execute the cascading retrieval pipeline.
    ///
    /// When the `csm` feature is enabled, this implements a 4-tier cascade:
    /// 1. BM25 keyword search (CPU-local, 0 API calls)
    /// 2. HDC similarity search (CPU-local, 0 API calls)
    /// 3. ConceptGraph expansion (CPU-local, 0 API calls)
    /// 4. API fallback (requires external embedding call)
    ///
    /// Without `csm`, returns empty results (placeholder behavior).
    pub fn retrieve(&self, query: &str) -> CascadeResult {
        #[cfg(feature = "csm")]
        {
            self.retrieve_with_csm(query)
        }

        #[cfg(not(feature = "csm"))]
        {
            tracing::warn!(
                "CSM feature not enabled; cascade retrieval returns empty results. \
                 Enable the `csm` feature for BM25/HDC/ConceptGraph retrieval."
            );
            let _ = query;
            CascadeResult {
                episode_ids: Vec::new(),
                scores: Vec::new(),
                contributing_tiers: Vec::new(),
                api_calls: 0,
            }
        }
    }

    /// Full cascade implementation using CSM components.
    #[cfg(feature = "csm")]
    fn retrieve_with_csm(&self, query: &str) -> CascadeResult {
        use super::{compute_weights, merge_results};

        // Tier 1: BM25 keyword search
        let bm25_results = self.retrieve_bm25(query);

        // Check if BM25 produced sufficient results
        if bm25_results.sufficient {
            return CascadeResult {
                episode_ids: bm25_results.ids(),
                scores: bm25_results.scores(),
                contributing_tiers: vec!["bm25".to_string()],
                api_calls: 0,
            };
        }

        // Tier 2: HDC similarity search
        let hdc_results = self.retrieve_hdc(query);

        // Check if HDC produced sufficient results (or merge with BM25)
        if self.config.merge_results && !bm25_results.is_empty() {
            // Merge BM25 and HDC results with query-length-dependent weights
            let weights = compute_weights(query.len());
            let merged = merge_results(&bm25_results.results, &hdc_results.results, weights);

            // Check if merged results are sufficient
            if merged.len() >= self.config.min_results {
                return CascadeResult {
                    episode_ids: merged.iter().map(|(id, _)| id.clone()).collect(),
                    scores: merged.iter().map(|(_, s)| *s).collect(),
                    contributing_tiers: vec!["bm25".to_string(), "hdc".to_string()],
                    api_calls: 0,
                };
            }
        } else if hdc_results.sufficient {
            return CascadeResult {
                episode_ids: hdc_results.ids(),
                scores: hdc_results.scores(),
                contributing_tiers: vec!["hdc".to_string()],
                api_calls: 0,
            };
        }

        // Tier 3: ConceptGraph expansion (optional)
        if self.config.enable_concept_expansion {
            let concept_results = self.retrieve_concept_graph(query);

            if concept_results.sufficient {
                return CascadeResult {
                    episode_ids: concept_results.ids(),
                    scores: concept_results.scores(),
                    contributing_tiers: vec!["concept_graph".to_string()],
                    api_calls: 0,
                };
            }
        }

        // Tier 4: API fallback - mark that we need an API call
        // Return best available results with api_calls = 1 indicator
        let best_results: Vec<(String, f32)> = if self.config.merge_results {
            let weights = compute_weights(query.len());
            merge_results(&bm25_results.results, &hdc_results.results, weights)
        } else if !hdc_results.is_empty() {
            hdc_results.results.clone()
        } else {
            bm25_results.results.clone()
        };

        CascadeResult {
            episode_ids: best_results.iter().map(|(id, _)| id.clone()).collect(),
            scores: best_results.iter().map(|(_, s)| *s).collect(),
            contributing_tiers: if best_results.is_empty() {
                vec!["none".to_string()]
            } else {
                // Preserve original tier attributions + note that API fallback was needed
                let mut tiers = Vec::new();
                if !bm25_results.is_empty() {
                    tiers.push("bm25".to_string());
                }
                if !hdc_results.is_empty() {
                    tiers.push("hdc".to_string());
                }
                tiers.push("api_fallback_needed".to_string());
                tiers
            },
            api_calls: 1, // Indicates API call would be needed
        }
    }

    /// BM25 keyword search (Tier 1).
    #[cfg(feature = "csm")]
    fn retrieve_bm25(&self, query: &str) -> TierResult {
        // Tokenize query for BM25 search
        let query_tokens = Self::tokenize(query);
        let raw_results = self.bm25_index.search(&query_tokens, self.config.top_k);

        // Normalize BM25 scores to 0.0-1.0 range
        let results = super::normalize_scores(&raw_results);

        // Determine if results are sufficient
        let sufficient = results.len() >= self.config.min_results
            && results
                .iter()
                .any(|(_, s)| *s >= self.config.bm25_threshold);

        TierResult {
            tier: "bm25".to_string(),
            results,
            sufficient,
        }
    }

    /// HDC hyperdimensional similarity search (Tier 2).
    #[cfg(feature = "csm")]
    fn retrieve_hdc(&self, query: &str) -> TierResult {
        // Encode query to HDC vector
        let query_vector = self.hdc_encoder.encode(query);

        // Compute similarities with all indexed vectors
        let mut similarities: Vec<(String, f32)> = self
            .hdc_vectors
            .iter()
            .map(|(id, vec)| {
                // Use cosine similarity (normalized hamming distance)
                let sim = query_vector.cosine_similarity(vec);
                (id.clone(), sim)
            })
            .collect();

        // Select top-k by similarity (highest first)
        // Optimization: O(N + k log k) instead of O(N log N)
        let top_k = self.config.top_k;
        let similarities = crate::search::select_top_k(&mut similarities, top_k, |a, b| {
            b.1.partial_cmp(&a.1)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| a.0.cmp(&b.0))
        });

        // Determine if results are sufficient
        let sufficient = similarities.len() >= self.config.min_results
            && similarities
                .iter()
                .any(|(_, s)| *s >= self.config.hdc_threshold);

        TierResult {
            tier: "hdc".to_string(),
            results: similarities,
            sufficient,
        }
    }

    /// ConceptGraph expansion search (Tier 3).
    ///
    /// Uses the embedded coding-agent domain ontology to expand query terms
    /// and match against indexed episode data using expanded terminology.
    ///
    /// # How it works
    ///
    /// 1. Expand query terms using the ontology (e.g., "auth" → "authentication")
    /// 2. Match expanded terms against episode text
    /// 3. Score results based on term overlap density
    #[cfg(feature = "csm")]
    fn retrieve_concept_graph(&self, query: &str) -> TierResult {
        // Expand query terms using the ontology
        let expanded_terms = self.concept_graph.expand_terms(query);

        if expanded_terms.is_empty() {
            return TierResult {
                tier: "concept_graph".to_string(),
                results: Vec::new(),
                sufficient: false,
            };
        }

        // Match expanded terms against episode data
        let mut scored: Vec<(String, f32)> = self
            .episode_data
            .iter()
            .map(|(id, text)| {
                let text_lower = text.to_lowercase();
                let match_count = expanded_terms
                    .iter()
                    .filter(|term| text_lower.split_whitespace().any(|w| w == term.as_str()))
                    .count();

                // Score based on term overlap density
                let score = if expanded_terms.is_empty() {
                    0.0
                } else {
                    match_count as f32 / expanded_terms.len() as f32
                };

                (id.clone(), score)
            })
            .filter(|(_, s)| *s >= self.config.concept_graph_threshold)
            .collect();

        // Select top-k by score using the shared select_top_k utility
        // (consistent with the HDC tier's approach)
        let top_k = self.config.top_k;
        let scored = crate::search::select_top_k(&mut scored, top_k, |a, b| {
            b.1.partial_cmp(&a.1)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| a.0.cmp(&b.0))
        });

        let sufficient = scored.len() >= self.config.min_results;

        TierResult {
            tier: "concept_graph".to_string(),
            results: scored,
            sufficient,
        }
    }

    /// Get the configuration for this retriever.
    pub fn config(&self) -> &CascadeConfig {
        &self.config
    }

    /// Estimate the probability that a query would require an API call.
    ///
    /// Returns a value in [0.0, 1.0] where:
    /// - 0.0 means CPU-local tiers (BM25/HDC/ConceptGraph) are very likely to suffice
    /// - 1.0 means an API embedding call is almost certainly needed
    ///
    /// Heuristic: short keyword-rich queries resolve via BM25 (low probability);
    /// long abstract queries with few known terms need semantic embedding (high probability).
    pub fn estimate_api_call_probability(&self, query: &str) -> f32 {
        let len = query.len() as f32;
        let word_count = query.split_whitespace().count() as f32;

        // Base probability from query length — short queries favor BM25
        let length_factor: f32 = if len < 20.0 {
            0.1
        } else if len < 50.0 {
            0.25
        } else if len < 100.0 {
            0.5
        } else {
            0.7
        };

        // Keyword density — queries with many short words are more BM25-friendly
        let avg_word_len = if word_count > 0.0 {
            len / word_count
        } else {
            10.0
        };
        let keyword_factor: f32 = if avg_word_len < 5.0 {
            0.0 // Short words = good keyword match candidates
        } else if avg_word_len < 8.0 {
            0.15
        } else {
            0.3 // Long words = more semantic, harder for BM25
        };

        // Concept-level boost — code-like tokens (identifiers, paths) are BM25-friendly
        let code_token_count = query
            .split_whitespace()
            .filter(|w| w.contains('_') || w.contains("::") || w.contains('/'))
            .count() as f32;
        let code_factor: f32 = if word_count > 0.0 && code_token_count / word_count > 0.3 {
            -0.15 // Many code tokens boost BM25 relevance
        } else {
            0.0
        };

        (length_factor + keyword_factor + code_factor).clamp(0.0, 1.0)
    }
}

#[cfg(test)]
mod tests;
#[cfg(feature = "csm")]
pub mod weights;
#[cfg(feature = "csm")]
pub use weights::compute_tier_weights;