do-memory-core 0.1.31

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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Pattern extraction and management
//!
//! This module provides types and functions for working with patterns extracted from episodes.

mod heuristic;
mod similarity;
mod types;

pub use heuristic::Heuristic;
pub use types::{Pattern, PatternEffectiveness};

use crate::types::TaskContext;
use chrono::Duration;

impl Pattern {
    /// Check if this pattern is relevant to a given context
    #[must_use]
    pub fn is_relevant_to(&self, query_context: &TaskContext) -> bool {
        if let Some(pattern_context) = self.context() {
            // Match on domain
            if pattern_context.domain == query_context.domain {
                return true;
            }

            // Match on language
            if pattern_context.language == query_context.language
                && pattern_context.language.is_some()
            {
                return true;
            }

            // Match on tags
            let common_tags: Vec<_> = pattern_context
                .tags
                .iter()
                .filter(|t| query_context.tags.contains(t))
                .collect();

            if !common_tags.is_empty() {
                return true;
            }
        }

        false
    }

    /// Get a similarity key for pattern deduplication
    /// Patterns with identical keys are considered duplicates
    #[must_use]
    pub fn similarity_key(&self) -> String {
        match self {
            Pattern::ToolSequence { tools, context, .. } => {
                format!("tool_seq:{}:{}", tools.join(","), context.domain)
            }
            Pattern::DecisionPoint {
                condition,
                action,
                context,
                ..
            } => {
                format!("decision:{}:{}:{}", condition, action, context.domain)
            }
            Pattern::ErrorRecovery {
                error_type,
                recovery_steps,
                context,
                ..
            } => {
                format!(
                    "error_recovery:{}:{}:{}",
                    error_type,
                    recovery_steps.join(","),
                    context.domain
                )
            }
            Pattern::ContextPattern {
                context_features,
                recommended_approach,
                ..
            } => {
                format!(
                    "context:{}:{}",
                    context_features.join(","),
                    recommended_approach
                )
            }
        }
    }

    /// Calculate similarity score between this pattern and another (0.0 to 1.0)
    /// Uses edit distance for sequences and context matching
    #[must_use]
    pub fn similarity_score(&self, other: &Self) -> f32 {
        // Different pattern types have zero similarity
        if std::mem::discriminant(self) != std::mem::discriminant(other) {
            return 0.0;
        }

        match (self, other) {
            (
                Pattern::ToolSequence {
                    tools: tools1,
                    context: ctx1,
                    ..
                },
                Pattern::ToolSequence {
                    tools: tools2,
                    context: ctx2,
                    ..
                },
            ) => {
                let sequence_similarity = similarity::sequence_similarity(tools1, tools2);
                let context_similarity = similarity::context_similarity(ctx1, ctx2);
                // Weight: 70% sequence, 30% context
                sequence_similarity * 0.7 + context_similarity * 0.3
            }
            (
                Pattern::DecisionPoint {
                    condition: cond1,
                    action: act1,
                    context: ctx1,
                    ..
                },
                Pattern::DecisionPoint {
                    condition: cond2,
                    action: act2,
                    context: ctx2,
                    ..
                },
            ) => {
                let condition_sim = similarity::string_similarity(cond1, cond2);
                let action_sim = similarity::string_similarity(act1, act2);
                let context_sim = similarity::context_similarity(ctx1, ctx2);
                // Weight: 40% condition, 40% action, 20% context
                condition_sim * 0.4 + action_sim * 0.4 + context_sim * 0.2
            }
            (
                Pattern::ErrorRecovery {
                    error_type: err1,
                    recovery_steps: steps1,
                    context: ctx1,
                    ..
                },
                Pattern::ErrorRecovery {
                    error_type: err2,
                    recovery_steps: steps2,
                    context: ctx2,
                    ..
                },
            ) => {
                let error_sim = similarity::string_similarity(err1, err2);
                let steps_sim = similarity::sequence_similarity(steps1, steps2);
                let context_sim = similarity::context_similarity(ctx1, ctx2);
                // Weight: 40% error type, 40% recovery steps, 20% context
                error_sim * 0.4 + steps_sim * 0.4 + context_sim * 0.2
            }
            (
                Pattern::ContextPattern {
                    context_features: feat1,
                    recommended_approach: rec1,
                    ..
                },
                Pattern::ContextPattern {
                    context_features: feat2,
                    recommended_approach: rec2,
                    ..
                },
            ) => {
                let features_sim = similarity::sequence_similarity(feat1, feat2);
                let approach_sim = similarity::string_similarity(rec1, rec2);
                // Weight: 60% features, 40% approach
                features_sim * 0.6 + approach_sim * 0.4
            }
            _ => 0.0,
        }
    }

    /// Calculate confidence score for this pattern
    /// Confidence = `success_rate` * `sqrt(sample_size)`
    #[must_use]
    pub fn confidence(&self) -> f32 {
        let success_rate = self.success_rate();
        let sample_size = self.sample_size() as f32;

        if sample_size == 0.0 {
            return 0.0;
        }

        success_rate * sample_size.sqrt()
    }

    /// Merge this pattern with another similar pattern
    /// Combines evidence and updates statistics
    pub fn merge_with(&mut self, other: &Self) {
        // Can only merge patterns of the same type
        if std::mem::discriminant(self) != std::mem::discriminant(other) {
            return;
        }

        match (self, other) {
            (
                Pattern::ToolSequence {
                    success_rate: sr1,
                    occurrence_count: oc1,
                    avg_latency: lat1,
                    ..
                },
                Pattern::ToolSequence {
                    success_rate: sr2,
                    occurrence_count: oc2,
                    avg_latency: lat2,
                    ..
                },
            ) => {
                let total_count = *oc1 + *oc2;
                // Weighted average of success rates
                *sr1 = (*sr1 * *oc1 as f32 + *sr2 * *oc2 as f32) / total_count as f32;
                // Weighted average of latencies
                *lat1 = Duration::milliseconds(
                    (lat1.num_milliseconds() * *oc1 as i64 + lat2.num_milliseconds() * *oc2 as i64)
                        / total_count as i64,
                );
                *oc1 = total_count;
            }
            (
                Pattern::DecisionPoint {
                    outcome_stats: stats1,
                    ..
                },
                Pattern::DecisionPoint {
                    outcome_stats: stats2,
                    ..
                },
            ) => {
                stats1.success_count += stats2.success_count;
                stats1.failure_count += stats2.failure_count;
                stats1.total_count += stats2.total_count;
                // Weighted average of durations
                stats1.avg_duration_secs = (stats1.avg_duration_secs
                    * (stats1.total_count - stats2.total_count) as f32
                    + stats2.avg_duration_secs * stats2.total_count as f32)
                    / stats1.total_count as f32;
            }
            (
                Pattern::ErrorRecovery {
                    success_rate: sr1, ..
                },
                Pattern::ErrorRecovery {
                    success_rate: sr2, ..
                },
            ) => {
                // Simple average for error recovery patterns
                *sr1 = (*sr1 + *sr2) / 2.0;
                // Keep the richer context (more tags)
                // Context is already part of self
            }
            (
                Pattern::ContextPattern {
                    evidence: ev1,
                    success_rate: sr1,
                    ..
                },
                Pattern::ContextPattern {
                    evidence: ev2,
                    success_rate: sr2,
                    ..
                },
            ) => {
                let size1 = ev1.len();
                let size2 = ev2.len();
                // Combine evidence
                ev1.extend_from_slice(ev2);
                // Weighted average of success rates
                *sr1 = (*sr1 * size1 as f32 + *sr2 * size2 as f32) / (size1 + size2) as f32;
            }
            _ => {}
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::ComplexityLevel;
    use uuid::Uuid;

    #[test]
    fn test_pattern_id() {
        let pattern = Pattern::ToolSequence {
            id: Uuid::new_v4(),
            tools: vec!["tool1".to_string(), "tool2".to_string()],
            context: TaskContext::default(),
            success_rate: 0.9,
            avg_latency: Duration::milliseconds(100),
            occurrence_count: 5,
            effectiveness: PatternEffectiveness::new(),
        };

        assert!(pattern.success_rate() > 0.8);
        assert!(pattern.context().is_some());
    }

    #[test]
    fn test_pattern_similarity_key() {
        let pattern1 = Pattern::ToolSequence {
            id: Uuid::new_v4(),
            tools: vec!["read".to_string(), "write".to_string()],
            context: TaskContext {
                domain: "web-api".to_string(),
                ..Default::default()
            },
            success_rate: 0.9,
            avg_latency: Duration::milliseconds(100),
            occurrence_count: 5,
            effectiveness: PatternEffectiveness::new(),
        };

        let pattern2 = Pattern::ToolSequence {
            id: Uuid::new_v4(),
            tools: vec!["read".to_string(), "write".to_string()],
            context: TaskContext {
                domain: "web-api".to_string(),
                ..Default::default()
            },
            success_rate: 0.8,
            avg_latency: Duration::milliseconds(120),
            occurrence_count: 3,
            effectiveness: PatternEffectiveness::new(),
        };

        // Same tools and domain = same key
        assert_eq!(pattern1.similarity_key(), pattern2.similarity_key());
    }

    #[test]
    fn test_pattern_similarity_score() {
        let pattern1 = Pattern::ToolSequence {
            id: Uuid::new_v4(),
            tools: vec!["read".to_string(), "write".to_string()],
            context: TaskContext {
                domain: "web-api".to_string(),
                language: Some("rust".to_string()),
                ..Default::default()
            },
            success_rate: 0.9,
            avg_latency: Duration::milliseconds(100),
            occurrence_count: 5,
            effectiveness: PatternEffectiveness::new(),
        };

        let pattern2 = Pattern::ToolSequence {
            id: Uuid::new_v4(),
            tools: vec!["read".to_string(), "write".to_string()],
            context: TaskContext {
                domain: "web-api".to_string(),
                language: Some("rust".to_string()),
                ..Default::default()
            },
            success_rate: 0.8,
            avg_latency: Duration::milliseconds(120),
            occurrence_count: 3,
            effectiveness: PatternEffectiveness::new(),
        };

        let similarity = pattern1.similarity_score(&pattern2);

        // Identical tools and context should have high similarity
        assert!(similarity > 0.9);
    }

    #[test]
    fn test_pattern_confidence() {
        let pattern = Pattern::ToolSequence {
            id: Uuid::new_v4(),
            tools: vec!["tool1".to_string()],
            context: TaskContext::default(),
            success_rate: 0.8,
            avg_latency: Duration::milliseconds(100),
            occurrence_count: 16, // sqrt(16) = 4
            effectiveness: PatternEffectiveness::new(),
        };

        let confidence = pattern.confidence();

        // 0.8 * sqrt(16) = 0.8 * 4 = 3.2
        assert!((confidence - 3.2).abs() < 0.01);
    }

    #[test]
    fn test_pattern_merge() {
        let mut pattern1 = Pattern::ToolSequence {
            id: Uuid::new_v4(),
            tools: vec!["read".to_string(), "write".to_string()],
            context: TaskContext::default(),
            success_rate: 0.8,
            avg_latency: Duration::milliseconds(100),
            occurrence_count: 10,
            effectiveness: PatternEffectiveness::new(),
        };

        let pattern2 = Pattern::ToolSequence {
            id: Uuid::new_v4(),
            tools: vec!["read".to_string(), "write".to_string()],
            context: TaskContext::default(),
            success_rate: 0.9,
            avg_latency: Duration::milliseconds(200),
            occurrence_count: 10,
            effectiveness: PatternEffectiveness::new(),
        };

        pattern1.merge_with(&pattern2);

        // Should have combined occurrence count
        match pattern1 {
            Pattern::ToolSequence {
                occurrence_count,
                success_rate,
                ..
            } => {
                assert_eq!(occurrence_count, 20);
                // Average: (0.8 * 10 + 0.9 * 10) / 20 = 0.85
                assert!((success_rate - 0.85).abs() < 0.01);
            }
            _ => panic!("Expected ToolSequence"),
        }
    }

    #[test]
    fn test_pattern_relevance() {
        let pattern_context = TaskContext {
            language: Some("rust".to_string()),
            framework: None,
            complexity: ComplexityLevel::Moderate,
            domain: "web-api".to_string(),
            tags: vec!["async".to_string()],
        };

        let pattern = Pattern::ToolSequence {
            id: Uuid::new_v4(),
            tools: vec![],
            context: pattern_context.clone(),
            success_rate: 0.9,
            avg_latency: Duration::milliseconds(100),
            occurrence_count: 1,
            effectiveness: PatternEffectiveness::new(),
        };

        // Should match on domain
        let query_context = TaskContext {
            domain: "web-api".to_string(),
            ..Default::default()
        };
        assert!(pattern.is_relevant_to(&query_context));

        // Should match on language
        let query_context2 = TaskContext {
            language: Some("rust".to_string()),
            domain: "cli".to_string(),
            ..Default::default()
        };
        assert!(pattern.is_relevant_to(&query_context2));

        // Should not match
        let query_context3 = TaskContext {
            language: Some("python".to_string()),
            domain: "data-science".to_string(),
            ..Default::default()
        };
        assert!(!pattern.is_relevant_to(&query_context3));
    }

    #[test]
    fn test_heuristic_evidence_update() {
        let mut heuristic = Heuristic::new(
            "When refactoring async code".to_string(),
            "Use tokio::spawn for CPU-intensive tasks".to_string(),
            0.7,
        );

        assert_eq!(heuristic.evidence.sample_size, 0);

        // Add successful evidence
        heuristic.update_evidence(Uuid::new_v4(), true);
        assert_eq!(heuristic.evidence.sample_size, 1);
        assert_eq!(heuristic.evidence.success_rate, 1.0);

        // Add failed evidence
        heuristic.update_evidence(Uuid::new_v4(), false);
        assert_eq!(heuristic.evidence.sample_size, 2);
        assert_eq!(heuristic.evidence.success_rate, 0.5);

        // Add more successful evidence
        heuristic.update_evidence(Uuid::new_v4(), true);
        assert_eq!(heuristic.evidence.sample_size, 3);
        assert!((heuristic.evidence.success_rate - 0.666).abs() < 0.01);
    }
}