Skip to main content

agentic_evolve_core/collective/
promotion.rs

1//! PromotionEngine — promotes high-confidence patterns and demotes poor ones.
2
3use crate::types::pattern::Pattern;
4
5/// Configuration for promotion behavior.
6#[derive(Debug, Clone)]
7pub struct PromotionConfig {
8    pub promote_threshold: f64,
9    pub demote_threshold: f64,
10    pub min_uses_for_promotion: u64,
11    pub min_uses_for_demotion: u64,
12}
13
14impl Default for PromotionConfig {
15    fn default() -> Self {
16        Self {
17            promote_threshold: 0.9,
18            demote_threshold: 0.3,
19            min_uses_for_promotion: 5,
20            min_uses_for_demotion: 3,
21        }
22    }
23}
24
25/// Evaluates patterns for promotion or demotion.
26#[derive(Debug)]
27pub struct PromotionEngine {
28    config: PromotionConfig,
29}
30
31/// Promotion decision for a pattern.
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub enum PromotionDecision {
34    Promote,
35    Demote,
36    Maintain,
37    Prune,
38}
39
40impl PromotionEngine {
41    pub fn new(config: PromotionConfig) -> Self {
42        Self { config }
43    }
44
45    pub fn evaluate(&self, pattern: &Pattern) -> PromotionDecision {
46        let success_rate = pattern.success_rate();
47
48        if pattern.usage_count >= self.config.min_uses_for_promotion
49            && success_rate >= self.config.promote_threshold
50        {
51            return PromotionDecision::Promote;
52        }
53
54        if pattern.usage_count >= self.config.min_uses_for_demotion
55            && success_rate < self.config.demote_threshold
56        {
57            if pattern.confidence < 0.1 {
58                return PromotionDecision::Prune;
59            }
60            return PromotionDecision::Demote;
61        }
62
63        PromotionDecision::Maintain
64    }
65
66    pub fn apply_promotion(&self, pattern: &mut Pattern) -> PromotionDecision {
67        let decision = self.evaluate(pattern);
68        match decision {
69            PromotionDecision::Promote => {
70                pattern.confidence = (pattern.confidence + 0.1).min(1.0);
71            }
72            PromotionDecision::Demote => {
73                pattern.confidence = (pattern.confidence - 0.1).max(0.0);
74            }
75            PromotionDecision::Prune => {
76                pattern.confidence = 0.0;
77            }
78            PromotionDecision::Maintain => {}
79        }
80        decision
81    }
82
83    pub fn batch_evaluate(&self, patterns: &[&Pattern]) -> Vec<(String, PromotionDecision)> {
84        patterns
85            .iter()
86            .map(|p| (p.id.as_str().to_string(), self.evaluate(p)))
87            .collect()
88    }
89}
90
91impl Default for PromotionEngine {
92    fn default() -> Self {
93        Self::new(PromotionConfig::default())
94    }
95}