Skip to main content

mimirs_yggdrasil/
lib.rs

1//! Yggdrasil - The Global Self-Model (L7).
2//!
3//! Named after the world tree that connects all realms, Yggdrasil
4//! represents the agent's persistent identity, strategic hierarchy,
5//! and cognitive continuity.
6
7use mimirs_core::{AgentId, BlockId, IdentityManifold, MemoryBlock, MimirError};
8use serde::{Deserialize, Serialize};
9
10pub mod identity;
11pub mod manifold;
12pub mod tree;
13pub use identity::Identity;
14pub use manifold::{KnotRegion, ManifoldIndex};
15pub use tree::ClusterNode;
16
17/// Yggdrasil - The Cognitive Identity Engine (L7).
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct Yggdrasil {
20    /// The persistent self-model manifold.
21    pub identity: IdentityManifold,
22    /// The global strategic hierarchy of goal attractors.
23    pub strategic_horizon: Vec<mimirs_core::GoalId>,
24    /// Continuity version / epoch.
25    pub epoch: u64,
26}
27
28impl Yggdrasil {
29    /// Opna - Creates a new Yggdrasil identity for an agent.
30    pub fn new(agent_id: AgentId) -> Self {
31        Self {
32            identity: IdentityManifold {
33                id: agent_id,
34                beliefs: Vec::new(),
35                values: Vec::new(),
36                personality_manifold: vec![0.0; 128],
37                strategy: "Persistent autonomous exploration.".to_string(),
38                blocks: std::collections::HashMap::new(),
39            },
40            strategic_horizon: Vec::new(),
41            epoch: 0,
42        }
43    }
44
45    /// Attach a memory block to the agent's identity (persisted in Yggdrasil).
46    pub fn attach_block(&mut self, block: MemoryBlock) -> Result<BlockId, MimirError> {
47        let block_id = block.id;
48        self.identity.blocks.insert(block.id, block);
49        self.epoch += 1;
50
51        Ok(block_id)
52    }
53
54    /// List all blocks attached to this identity.
55    pub fn list_blocks(&self) -> Vec<MemoryBlock> {
56        self.identity.blocks.values().cloned().collect()
57    }
58
59    /// Detach a block from the agent's identity.
60    pub fn detach_block(&mut self, block_id: BlockId) -> Result<MemoryBlock, MimirError> {
61        self.identity
62            .blocks
63            .remove(&block_id)
64            .ok_or_else(|| MimirError::NotFound(format!("Block {} not found", block_id)))
65    }
66
67    /// Vaxa - Grow/Evolve the identity manifold based on reflection outputs.
68    /// DERIVATION: P[t+1] = P[t] + δP.
69    /// The identity manifold evolves linearly with reflection-distilled drift,
70    /// representing slow-updating semantic priors of the agent self-model.
71    pub fn vaxa(&mut self, manifold_drift: &[f32]) {
72        // Apply manifold drift to the personality vector.
73        for (i, &drift) in manifold_drift.iter().enumerate() {
74            if i < self.identity.personality_manifold.len() {
75                self.identity.personality_manifold[i] += drift;
76            }
77        }
78        self.epoch += 1;
79    }
80
81    /// Skoda - Inspect the current self-model.
82    pub fn skoda(&self) -> &IdentityManifold {
83        &self.identity
84    }
85}