omega-cognitive-kernel 0.6.1

Online strategy-patch runtime: training-free RuleEffect adaptation with audit and rollback
Documentation
#[allow(unused_imports)]
use crate::prelude::*;

use alloc::collections::BTreeMap;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GoalStatus {
    Active,
    Achieved,
    Suspended,
    Failed,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GoalNode {
    pub id: String,
    pub targets: BTreeMap<String, f64>,
    pub weight: f64,
    pub priority: i32,
    pub status: GoalStatus,
    pub progress: f64,
    pub parent_id: Option<String>,
    pub children: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GoalTreeSummary {
    pub total_nodes: usize,
    pub achieved_nodes: usize,
    pub active_id: Option<String>,
    pub root_progress: f64,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct GoalTree {
    pub nodes: BTreeMap<String, GoalNode>,
    pub root_id: Option<String>,
    pub active_id: Option<String>,
}

impl GoalTree {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn from_flat(targets: BTreeMap<String, f64>) -> Self {
        let mut tree = Self::new();
        tree.add_root("root", targets, 1);
        tree
    }

    pub fn to_flat(&self) -> BTreeMap<String, f64> {
        self.active_id
            .as_ref()
            .and_then(|id| self.nodes.get(id))
            .map(|node| node.targets.clone())
            .or_else(|| {
                self.root_id
                    .as_ref()
                    .and_then(|id| self.nodes.get(id))
                    .map(|node| node.targets.clone())
            })
            .unwrap_or_default()
    }

    pub fn add_root(
        &mut self,
        id: impl Into<String>,
        targets: BTreeMap<String, f64>,
        priority: i32,
    ) {
        let id_str = id.into();
        let node = GoalNode {
            id: id_str.clone(),
            targets,
            weight: 1.0,
            priority,
            status: GoalStatus::Active,
            progress: 0.0,
            parent_id: None,
            children: Vec::new(),
        };
        self.nodes.insert(id_str.clone(), node);
        self.root_id = Some(id_str.clone());
        if self.active_id.is_none() {
            self.active_id = Some(id_str);
        }
    }

    pub fn add_child(
        &mut self,
        parent_id: &str,
        id: impl Into<String>,
        targets: BTreeMap<String, f64>,
        weight: f64,
        priority: i32,
    ) -> bool {
        if !self.nodes.contains_key(parent_id) {
            return false;
        }
        let child_id = id.into();
        let node = GoalNode {
            id: child_id.clone(),
            targets,
            weight: if weight.is_finite() && weight > 0.0 {
                weight
            } else {
                1.0
            },
            priority,
            status: GoalStatus::Active,
            progress: 0.0,
            parent_id: Some(parent_id.to_string()),
            children: Vec::new(),
        };
        self.nodes.insert(child_id.clone(), node);
        if let Some(parent) = self.nodes.get_mut(parent_id) {
            if !parent.children.iter().any(|c| c == &child_id) {
                parent.children.push(child_id);
            }
        }
        true
    }

    pub fn update_from_state(&mut self, state: &BTreeMap<String, f64>, tolerance: f64) {
        let tol = if tolerance.is_finite() && tolerance > 0.0 {
            tolerance
        } else {
            0.05
        };
        let leaf_ids: Vec<String> = self
            .nodes
            .iter()
            .filter(|(_, n)| n.children.is_empty())
            .map(|(id, _)| id.clone())
            .collect();

        for id in leaf_ids {
            if let Some(node) = self.nodes.get_mut(&id) {
                if node.targets.is_empty() {
                    continue;
                }
                let mut total_dist = 0.0;
                for (k, target) in &node.targets {
                    let cur = state.get(k).copied().unwrap_or(0.0);
                    total_dist += (cur - target).abs();
                }
                let mean_dist = total_dist / node.targets.len() as f64;
                let prog = (1.0 - mean_dist).clamp(0.0, 1.0);
                node.progress = prog;
                if mean_dist <= tol && node.status == GoalStatus::Active {
                    node.status = GoalStatus::Achieved;
                }
            }
        }

        if let Some(root) = self.root_id.clone() {
            self.rollup_node(&root);
        }
    }

    fn rollup_node(&mut self, id: &str) -> f64 {
        let children = match self.nodes.get(id) {
            Some(n) if !n.children.is_empty() => n.children.clone(),
            Some(n) => return n.progress,
            None => return 0.0,
        };
        let mut weighted_sum = 0.0;
        let mut weight_total = 0.0;
        let mut all_achieved = true;
        for child_id in &children {
            let child_prog = self.rollup_node(child_id);
            if let Some(child) = self.nodes.get(child_id) {
                weighted_sum += child_prog * child.weight;
                weight_total += child.weight;
                if child.status != GoalStatus::Achieved {
                    all_achieved = false;
                }
            }
        }
        let prog = if weight_total > 0.0 {
            (weighted_sum / weight_total).clamp(0.0, 1.0)
        } else {
            0.0
        };
        if let Some(node) = self.nodes.get_mut(id) {
            node.progress = prog;
            if all_achieved && node.status == GoalStatus::Active {
                node.status = GoalStatus::Achieved;
            }
        }
        prog
    }

    pub fn select_active(&mut self) -> Option<&GoalNode> {
        let best_id = self
            .nodes
            .values()
            .filter(|n| n.status == GoalStatus::Active && n.children.is_empty())
            .max_by(|a, b| {
                a.priority
                    .cmp(&b.priority)
                    .then_with(|| b.progress.total_cmp(&a.progress))
            })
            .map(|n| n.id.clone());

        if let Some(id) = best_id {
            self.active_id = Some(id);
        }
        self.active_id.as_ref().and_then(|id| self.nodes.get(id))
    }

    pub fn summary(&self) -> GoalTreeSummary {
        let achieved_nodes = self
            .nodes
            .values()
            .filter(|n| n.status == GoalStatus::Achieved)
            .count();
        let root_progress = self
            .root_id
            .as_ref()
            .and_then(|id| self.nodes.get(id))
            .map(|n| n.progress)
            .unwrap_or(0.0);
        GoalTreeSummary {
            total_nodes: self.nodes.len(),
            achieved_nodes,
            active_id: self.active_id.clone(),
            root_progress,
        }
    }
}

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

    #[test]
    fn hierarchical_rollup_and_active_selection() {
        let mut tree = GoalTree::new();
        tree.add_root("root", BTreeMap::new(), 1);

        let mut t1 = BTreeMap::new();
        t1.insert("x".into(), 1.0);
        assert!(tree.add_child("root", "sub1", t1, 1.0, 10));

        let mut t2 = BTreeMap::new();
        t2.insert("y".into(), 1.0);
        assert!(tree.add_child("root", "sub2", t2, 1.0, 5));

        let mut state = BTreeMap::new();
        state.insert("x".into(), 1.0);
        state.insert("y".into(), 0.0);
        tree.update_from_state(&state, 0.05);

        assert_eq!(tree.nodes["sub1"].status, GoalStatus::Achieved);
        assert_eq!(tree.nodes["sub2"].status, GoalStatus::Active);
        assert!((tree.summary().root_progress - 0.5).abs() < 1e-9);
        assert_eq!(tree.select_active().map(|n| n.id.as_str()), Some("sub2"));
    }
}