terraphim_agent_evolution 1.20.1

Agent evolution system for Terraphim AI
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//! Agent lessons learned evolution with comprehensive learning management

use std::collections::{BTreeMap, HashMap};

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use terraphim_persistence::Persistable;
use uuid::Uuid;

use crate::{AgentId, EvolutionError, EvolutionResult, LessonId, MemoryId, TaskId};

/// Versioned lessons learned evolution system
#[derive(Debug, Clone)]
pub struct LessonsEvolution {
    pub agent_id: AgentId,
    pub current_state: LessonsState,
    pub history: BTreeMap<DateTime<Utc>, LessonsState>,
}

impl LessonsEvolution {
    /// Create a new lessons evolution tracker
    pub fn new(agent_id: AgentId) -> Self {
        Self {
            agent_id,
            current_state: LessonsState::default(),
            history: BTreeMap::new(),
        }
    }

    /// Add a new lesson
    pub async fn add_lesson(&mut self, lesson: Lesson) -> EvolutionResult<()> {
        log::info!("Adding lesson: {} - {}", lesson.id, lesson.title);

        self.current_state.add_lesson(lesson);
        self.save_current_state().await?;

        Ok(())
    }

    /// Apply a lesson to a task or situation
    pub async fn apply_lesson(
        &mut self,
        lesson_id: &LessonId,
        context: &str,
    ) -> EvolutionResult<ApplicationResult> {
        log::debug!("Applying lesson {} in context: {}", lesson_id, context);

        let result = self.current_state.apply_lesson(lesson_id, context)?;
        self.save_current_state().await?;

        Ok(result)
    }

    /// Validate a lesson with evidence
    pub async fn validate_lesson(
        &mut self,
        lesson_id: &LessonId,
        evidence: Evidence,
    ) -> EvolutionResult<()> {
        log::debug!("Validating lesson {} with evidence", lesson_id);

        self.current_state.validate_lesson(lesson_id, evidence)?;
        self.save_current_state().await?;

        Ok(())
    }

    /// Find applicable lessons for a given context
    pub async fn find_applicable_lessons(&self, context: &str) -> EvolutionResult<Vec<Lesson>> {
        Ok(self.current_state.find_applicable_lessons(context))
    }

    /// Get lessons by tag
    pub async fn get_lessons_by_tag(&self, tag: &str) -> EvolutionResult<Vec<Lesson>> {
        Ok(self.current_state.get_lessons_by_tag(tag))
    }

    /// Get lessons by multiple tags
    pub async fn get_lessons_by_tags(&self, tags: &[&str]) -> EvolutionResult<Vec<Lesson>> {
        Ok(self.current_state.get_lessons_by_tags(tags))
    }

    /// Save a versioned snapshot
    pub async fn save_version(&self, timestamp: DateTime<Utc>) -> EvolutionResult<()> {
        let versioned_lessons = VersionedLessons {
            agent_id: self.agent_id.clone(),
            timestamp,
            state: self.current_state.clone(),
        };

        versioned_lessons.save().await?;
        log::debug!(
            "Saved lessons version for agent {} at {}",
            self.agent_id,
            timestamp
        );

        Ok(())
    }

    /// Load lessons state at a specific time
    pub async fn load_version(&self, timestamp: DateTime<Utc>) -> EvolutionResult<LessonsState> {
        let mut versioned_lessons = VersionedLessons::new(self.get_version_key(timestamp));
        let loaded = versioned_lessons.load().await?;
        Ok(loaded.state)
    }

    /// Get the storage key for a specific version
    pub fn get_version_key(&self, timestamp: DateTime<Utc>) -> String {
        format!(
            "agent_{}/lessons/v_{}",
            self.agent_id,
            timestamp.timestamp()
        )
    }

    /// Save the current state
    async fn save_current_state(&self) -> EvolutionResult<()> {
        let current_lessons = CurrentLessonsState {
            agent_id: self.agent_id.clone(),
            state: self.current_state.clone(),
        };

        current_lessons.save().await?;
        Ok(())
    }
}

/// Current lessons state of an agent
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct LessonsState {
    pub technical_lessons: Vec<Lesson>,
    pub process_lessons: Vec<Lesson>,
    pub domain_lessons: Vec<Lesson>,
    pub failure_lessons: Vec<Lesson>,
    pub success_patterns: Vec<Lesson>,
    pub lesson_index: HashMap<String, Vec<LessonId>>, // Tag -> Lesson IDs
    pub metadata: LessonsMetadata,
}

impl LessonsState {
    /// Add a new lesson
    pub fn add_lesson(&mut self, lesson: Lesson) {
        // Categorize the lesson
        match lesson.category {
            LessonCategory::Technical => self.technical_lessons.push(lesson.clone()),
            LessonCategory::Process => self.process_lessons.push(lesson.clone()),
            LessonCategory::Domain => self.domain_lessons.push(lesson.clone()),
            LessonCategory::Failure => self.failure_lessons.push(lesson.clone()),
            LessonCategory::SuccessPattern => self.success_patterns.push(lesson.clone()),
        }

        // Update index
        for tag in &lesson.tags {
            self.lesson_index
                .entry(tag.clone())
                .or_default()
                .push(lesson.id.clone());
        }

        self.metadata.last_updated = Utc::now();
        self.metadata.total_lessons += 1;
    }

    /// Apply a lesson and track its usage
    pub fn apply_lesson(
        &mut self,
        lesson_id: &LessonId,
        context: &str,
    ) -> EvolutionResult<ApplicationResult> {
        if let Some(lesson) = self.find_lesson_mut(lesson_id) {
            lesson.applied_count += 1;
            lesson.last_applied = Some(Utc::now());
            lesson.contexts.push(context.to_string());

            // Keep contexts bounded
            if lesson.contexts.len() > 10 {
                lesson.contexts.remove(0);
            }

            let previous_applications = lesson.applied_count - 1;
            let success_rate = lesson.success_rate;

            self.metadata.last_updated = Utc::now();
            self.metadata.total_applications += 1;

            Ok(ApplicationResult {
                lesson_id: lesson_id.clone(),
                applied_at: Utc::now(),
                context: context.to_string(),
                previous_applications,
                success_rate,
            })
        } else {
            Err(EvolutionError::LessonNotFound(lesson_id.clone()))
        }
    }

    /// Validate a lesson with evidence
    pub fn validate_lesson(
        &mut self,
        lesson_id: &LessonId,
        evidence: Evidence,
    ) -> EvolutionResult<()> {
        if let Some(lesson) = self.find_lesson_mut(lesson_id) {
            lesson.evidence.push(evidence.clone());
            lesson.validated = true;
            lesson.last_validated = Some(Utc::now());

            // Update success rate based on evidence
            lesson.update_success_rate(&evidence);

            self.metadata.last_updated = Utc::now();
            self.metadata.validated_lessons += 1;

            Ok(())
        } else {
            Err(EvolutionError::LessonNotFound(lesson_id.clone()))
        }
    }

    /// Find applicable lessons for a context
    pub fn find_applicable_lessons(&self, context: &str) -> Vec<Lesson> {
        let mut applicable = Vec::new();
        let context_lower = context.to_lowercase();

        // Search in all categories
        let all_lessons = self.get_all_lessons();

        for lesson in all_lessons {
            // Check if context matches lesson context or tags
            let context_match = lesson.context.to_lowercase().contains(&context_lower)
                || lesson.insight.to_lowercase().contains(&context_lower);

            let tag_match = lesson.tags.iter().any(|tag| {
                context_lower.contains(&tag.to_lowercase())
                    || tag.to_lowercase().contains(&context_lower)
            });

            if context_match || tag_match {
                applicable.push(lesson.clone());
            }
        }

        // Sort by relevance (success rate and application count)
        #[allow(clippy::unnecessary_sort_by)]
        applicable.sort_by(|a, b| {
            let a_score = a.success_rate * (1.0 + (a.applied_count as f64 / 10.0));
            let b_score = b.success_rate * (1.0 + (b.applied_count as f64 / 10.0));
            b_score
                .partial_cmp(&a_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        applicable
    }

    /// Get lessons by tag
    pub fn get_lessons_by_tag(&self, tag: &str) -> Vec<Lesson> {
        if let Some(lesson_ids) = self.lesson_index.get(tag) {
            lesson_ids
                .iter()
                .filter_map(|id| self.find_lesson(id))
                .cloned()
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Get lessons by multiple tags
    pub fn get_lessons_by_tags(&self, tags: &[&str]) -> Vec<Lesson> {
        let mut lessons = Vec::new();

        for tag in tags {
            lessons.extend(self.get_lessons_by_tag(tag));
        }

        // Remove duplicates
        lessons.dedup_by(|a, b| a.id == b.id);
        lessons
    }

    /// Get total number of lessons
    pub fn total_lessons(&self) -> usize {
        self.technical_lessons.len()
            + self.process_lessons.len()
            + self.domain_lessons.len()
            + self.failure_lessons.len()
            + self.success_patterns.len()
    }

    /// Get all lessons
    fn get_all_lessons(&self) -> Vec<&Lesson> {
        let mut all_lessons = Vec::new();
        all_lessons.extend(&self.technical_lessons);
        all_lessons.extend(&self.process_lessons);
        all_lessons.extend(&self.domain_lessons);
        all_lessons.extend(&self.failure_lessons);
        all_lessons.extend(&self.success_patterns);
        all_lessons
    }

    /// Find a lesson by ID
    fn find_lesson(&self, lesson_id: &LessonId) -> Option<&Lesson> {
        self.get_all_lessons()
            .into_iter()
            .find(|l| l.id == *lesson_id)
    }

    /// Find a mutable lesson by ID
    fn find_lesson_mut(&mut self, lesson_id: &LessonId) -> Option<&mut Lesson> {
        if let Some(lesson) = self
            .technical_lessons
            .iter_mut()
            .find(|l| l.id == *lesson_id)
        {
            return Some(lesson);
        }
        if let Some(lesson) = self.process_lessons.iter_mut().find(|l| l.id == *lesson_id) {
            return Some(lesson);
        }
        if let Some(lesson) = self.domain_lessons.iter_mut().find(|l| l.id == *lesson_id) {
            return Some(lesson);
        }
        if let Some(lesson) = self.failure_lessons.iter_mut().find(|l| l.id == *lesson_id) {
            return Some(lesson);
        }
        if let Some(lesson) = self
            .success_patterns
            .iter_mut()
            .find(|l| l.id == *lesson_id)
        {
            return Some(lesson);
        }
        None
    }

    /// Calculate success rate of all lessons
    pub fn calculate_success_rate(&self) -> f64 {
        let all_lessons = self.get_all_lessons();
        if all_lessons.is_empty() {
            0.0
        } else {
            let total_success_rate: f64 =
                all_lessons.iter().map(|lesson| lesson.success_rate).sum();
            total_success_rate / all_lessons.len() as f64
        }
    }

    /// Calculate knowledge coverage (percentage of different categories covered)
    pub fn calculate_knowledge_coverage(&self) -> f64 {
        let mut covered_categories = 0;
        let total_categories = 5; // Technical, Process, Domain, Failure, SuccessPattern

        if !self.technical_lessons.is_empty() {
            covered_categories += 1;
        }
        if !self.process_lessons.is_empty() {
            covered_categories += 1;
        }
        if !self.domain_lessons.is_empty() {
            covered_categories += 1;
        }
        if !self.failure_lessons.is_empty() {
            covered_categories += 1;
        }
        if !self.success_patterns.is_empty() {
            covered_categories += 1;
        }

        (covered_categories as f64 / total_categories as f64) * 100.0
    }

    /// Get lessons by category using existing categorized vectors
    pub fn get_lessons_by_category(&self, category: &str) -> Vec<&Lesson> {
        match category.to_lowercase().as_str() {
            "technical" => self.technical_lessons.iter().collect(),
            "process" => self.process_lessons.iter().collect(),
            "domain" => self.domain_lessons.iter().collect(),
            "failure" => self.failure_lessons.iter().collect(),
            "success" | "successpattern" => self.success_patterns.iter().collect(),
            _ => Vec::new(),
        }
    }
}

/// Individual lesson learned
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Lesson {
    pub id: LessonId,
    pub title: String,
    pub context: String,
    pub insight: String,
    pub category: LessonCategory,
    pub evidence: Vec<Evidence>,
    pub impact: ImpactLevel,
    pub confidence: f64,
    pub learned_at: DateTime<Utc>,
    pub last_applied: Option<DateTime<Utc>>,
    pub last_validated: Option<DateTime<Utc>>,
    pub validated: bool,
    pub applied_count: u32,
    pub success_rate: f64,
    pub related_tasks: Vec<TaskId>,
    pub related_memories: Vec<MemoryId>,
    pub knowledge_graph_refs: Vec<String>,
    pub tags: Vec<String>,
    pub contexts: Vec<String>, // Contexts where this lesson was applied
    pub metadata: HashMap<String, serde_json::Value>,
}

impl Lesson {
    /// Create a new lesson
    pub fn new(title: String, context: String, insight: String, category: LessonCategory) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            title,
            context,
            insight,
            category,
            evidence: Vec::new(),
            impact: ImpactLevel::Medium,
            confidence: 0.7,
            learned_at: Utc::now(),
            last_applied: None,
            last_validated: None,
            validated: false,
            applied_count: 0,
            success_rate: 0.5,
            related_tasks: Vec::new(),
            related_memories: Vec::new(),
            knowledge_graph_refs: Vec::new(),
            tags: Vec::new(),
            contexts: Vec::new(),
            metadata: HashMap::new(),
        }
    }

    /// Update success rate based on evidence
    pub fn update_success_rate(&mut self, evidence: &Evidence) {
        let weight = 0.2; // How much new evidence affects the rate
        let evidence_success = if evidence.outcome == EvidenceOutcome::Success {
            1.0
        } else {
            0.0
        };

        self.success_rate = (1.0 - weight) * self.success_rate + weight * evidence_success;

        // Update confidence based on evidence count
        let evidence_factor = (self.evidence.len() as f64 / 10.0).min(1.0);
        self.confidence = 0.5 + 0.4 * evidence_factor + 0.1 * self.success_rate;
    }

    /// Check if lesson is relevant for a context
    pub fn is_relevant_for(&self, context: &str) -> bool {
        let context_lower = context.to_lowercase();

        self.context.to_lowercase().contains(&context_lower)
            || self.insight.to_lowercase().contains(&context_lower)
            || self.tags.iter().any(|tag| {
                context_lower.contains(&tag.to_lowercase())
                    || tag.to_lowercase().contains(&context_lower)
            })
    }
}

/// Lesson categories
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LessonCategory {
    Technical,      // Code/implementation insights
    Process,        // Workflow improvements
    Domain,         // Subject matter insights
    Failure,        // What went wrong and why
    SuccessPattern, // What worked well
}

/// Impact levels for lessons
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum ImpactLevel {
    Low,
    Medium,
    High,
    Critical,
}

/// Evidence supporting a lesson
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Evidence {
    pub description: String,
    pub source: EvidenceSource,
    pub outcome: EvidenceOutcome,
    pub confidence: f64,
    pub timestamp: DateTime<Utc>,
    pub metadata: HashMap<String, String>,
}

/// Sources of evidence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EvidenceSource {
    TaskExecution,
    UserFeedback,
    PerformanceMetric,
    ExternalValidation,
    SelfReflection,
}

/// Evidence outcomes
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum EvidenceOutcome {
    Success,
    Failure,
    Mixed,
    Inconclusive,
}

/// Result of applying a lesson
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApplicationResult {
    pub lesson_id: LessonId,
    pub applied_at: DateTime<Utc>,
    pub context: String,
    pub previous_applications: u32,
    pub success_rate: f64,
}

/// Lessons metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LessonsMetadata {
    pub created_at: DateTime<Utc>,
    pub last_updated: DateTime<Utc>,
    pub total_lessons: u32,
    pub validated_lessons: u32,
    pub total_applications: u32,
    pub average_success_rate: f64,
}

impl Default for LessonsMetadata {
    fn default() -> Self {
        let now = Utc::now();
        Self {
            created_at: now,
            last_updated: now,
            total_lessons: 0,
            validated_lessons: 0,
            total_applications: 0,
            average_success_rate: 0.0,
        }
    }
}

/// Versioned lessons for persistence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VersionedLessons {
    pub agent_id: AgentId,
    pub timestamp: DateTime<Utc>,
    pub state: LessonsState,
}

#[async_trait]
impl Persistable for VersionedLessons {
    fn new(_key: String) -> Self {
        Self {
            agent_id: String::new(),
            timestamp: Utc::now(),
            state: LessonsState::default(),
        }
    }

    async fn save(&self) -> terraphim_persistence::Result<()> {
        self.save_to_all().await
    }

    async fn save_to_one(&self, profile_name: &str) -> terraphim_persistence::Result<()> {
        self.save_to_profile(profile_name).await
    }

    async fn load(&mut self) -> terraphim_persistence::Result<Self> {
        let key = self.get_key();
        self.load_from_operator(
            &key,
            &terraphim_persistence::DeviceStorage::instance()
                .await?
                .fastest_op,
        )
        .await
    }

    fn get_key(&self) -> String {
        format!(
            "agent_{}/lessons/v_{}",
            self.agent_id,
            self.timestamp.timestamp()
        )
    }
}

/// Current lessons state for persistence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CurrentLessonsState {
    pub agent_id: AgentId,
    pub state: LessonsState,
}

#[async_trait]
impl Persistable for CurrentLessonsState {
    fn new(key: String) -> Self {
        Self {
            agent_id: key,
            state: LessonsState::default(),
        }
    }

    async fn save(&self) -> terraphim_persistence::Result<()> {
        self.save_to_all().await
    }

    async fn save_to_one(&self, profile_name: &str) -> terraphim_persistence::Result<()> {
        self.save_to_profile(profile_name).await
    }

    async fn load(&mut self) -> terraphim_persistence::Result<Self> {
        let key = self.get_key();
        self.load_from_operator(
            &key,
            &terraphim_persistence::DeviceStorage::instance()
                .await?
                .fastest_op,
        )
        .await
    }

    fn get_key(&self) -> String {
        format!("agent_{}/lessons/current", self.agent_id)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_lessons_evolution_creation() {
        let agent_id = "test_agent".to_string();
        let lessons = LessonsEvolution::new(agent_id.clone());

        assert_eq!(lessons.agent_id, agent_id);
        assert_eq!(lessons.current_state.total_lessons(), 0);
    }

    #[tokio::test]
    async fn test_add_lesson() {
        let mut lessons = LessonsEvolution::new("test_agent".to_string());

        let lesson = Lesson::new(
            "Test lesson".to_string(),
            "Testing context".to_string(),
            "Testing is important".to_string(),
            LessonCategory::Technical,
        );

        lessons.add_lesson(lesson).await.unwrap();
        assert_eq!(lessons.current_state.technical_lessons.len(), 1);
        assert_eq!(lessons.current_state.total_lessons(), 1);
    }

    #[tokio::test]
    async fn test_lesson_application() {
        let mut lessons_state = LessonsState::default();

        let mut lesson = Lesson::new(
            "Test lesson".to_string(),
            "Testing context".to_string(),
            "Testing is important".to_string(),
            LessonCategory::Technical,
        );
        lesson.tags.push("testing".to_string());

        let lesson_id = lesson.id.clone();
        lessons_state.add_lesson(lesson);

        let result = lessons_state
            .apply_lesson(&lesson_id, "Unit testing")
            .unwrap();
        assert_eq!(result.previous_applications, 0);

        let lesson = lessons_state.find_lesson(&lesson_id).unwrap();
        assert_eq!(lesson.applied_count, 1);
    }

    #[tokio::test]
    async fn test_find_applicable_lessons() {
        let mut lessons_state = LessonsState::default();

        let mut lesson1 = Lesson::new(
            "Testing lesson".to_string(),
            "Unit testing context".to_string(),
            "Unit tests prevent bugs".to_string(),
            LessonCategory::Technical,
        );
        lesson1.tags.push("testing".to_string());
        lesson1.tags.push("quality".to_string());

        let mut lesson2 = Lesson::new(
            "Performance lesson".to_string(),
            "Optimization context".to_string(),
            "Profile before optimizing".to_string(),
            LessonCategory::Technical,
        );
        lesson2.tags.push("performance".to_string());

        lessons_state.add_lesson(lesson1);
        lessons_state.add_lesson(lesson2);

        let applicable = lessons_state.find_applicable_lessons("testing code quality");
        assert_eq!(applicable.len(), 1);
        assert!(applicable[0].title.contains("Testing"));

        let performance_lessons = lessons_state.find_applicable_lessons("performance optimization");
        assert_eq!(performance_lessons.len(), 1);
        assert!(performance_lessons[0].title.contains("Performance"));
    }

    #[tokio::test]
    async fn test_lesson_validation() {
        let mut lessons_state = LessonsState::default();

        let lesson = Lesson::new(
            "Test lesson".to_string(),
            "Testing context".to_string(),
            "Testing is important".to_string(),
            LessonCategory::Technical,
        );
        let lesson_id = lesson.id.clone();

        lessons_state.add_lesson(lesson);

        let evidence = Evidence {
            description: "Unit tests caught 5 bugs".to_string(),
            source: EvidenceSource::TaskExecution,
            outcome: EvidenceOutcome::Success,
            confidence: 0.9,
            timestamp: Utc::now(),
            metadata: HashMap::new(),
        };

        lessons_state.validate_lesson(&lesson_id, evidence).unwrap();

        let validated_lesson = lessons_state.find_lesson(&lesson_id).unwrap();
        assert!(validated_lesson.validated);
        assert_eq!(validated_lesson.evidence.len(), 1);
        assert!(validated_lesson.success_rate > 0.5); // Should improve with successful evidence
    }
}