Skip to main content

agentic_reality/engine/write/
deployment.rs

1//! Deployment write operations.
2
3use crate::engine::write::WriteEngine;
4use crate::types::deployment::*;
5use crate::types::error::RealityResult;
6use crate::types::ids::IncarnationId;
7
8impl<'a> WriteEngine<'a> {
9    /// Initialize the deployment soul — the agent's existential identity.
10    pub fn initialize_soul(&mut self, soul: DeploymentSoul) -> RealityResult<IncarnationId> {
11        if self.engine.deployment_store.soul.is_some() {
12            return Err(crate::types::error::RealityError::AlreadyInitialized(
13                "deployment soul".into(),
14            ));
15        }
16        let id = soul.incarnation_id;
17        self.engine.deployment_store.soul = Some(soul);
18        self.engine.mark_dirty();
19        Ok(id)
20    }
21
22    /// Update the physical substrate.
23    pub fn update_substrate(&mut self, substrate: PhysicalSubstrate) -> RealityResult<()> {
24        let soul = self.engine.deployment_store.soul.as_mut().ok_or_else(|| {
25            crate::types::error::RealityError::NotInitialized("deployment soul".into())
26        })?;
27        soul.substrate = substrate;
28        self.engine.mark_dirty();
29        Ok(())
30    }
31
32    /// Record birth context.
33    pub fn record_birth(&mut self, birth: BirthContext) -> RealityResult<()> {
34        let soul = self.engine.deployment_store.soul.as_mut().ok_or_else(|| {
35            crate::types::error::RealityError::NotInitialized("deployment soul".into())
36        })?;
37        soul.birth = birth;
38        self.engine.mark_dirty();
39        Ok(())
40    }
41
42    /// Update soul vitals.
43    pub fn update_vitals(&mut self, vitals: SoulVitals) -> RealityResult<()> {
44        let soul = self.engine.deployment_store.soul.as_mut().ok_or_else(|| {
45            crate::types::error::RealityError::NotInitialized("deployment soul".into())
46        })?;
47        soul.vitals = vitals;
48        self.engine.mark_dirty();
49        Ok(())
50    }
51
52    /// Record death of the current incarnation.
53    pub fn record_death(&mut self, death: DeathRecord) -> RealityResult<()> {
54        let mem = self
55            .engine
56            .deployment_store
57            .incarnation_memory
58            .as_mut()
59            .ok_or_else(|| {
60                crate::types::error::RealityError::NotInitialized("incarnation memory".into())
61            })?;
62        if let Some(soul) = &self.engine.deployment_store.soul {
63            let past = PastIncarnation {
64                incarnation_id: soul.incarnation_id,
65                birth: soul.birth.spawned_at,
66                death,
67                lessons: vec![],
68                substrate_tier: format!("{:?}", soul.substrate.tier),
69            };
70            mem.past_lives.push(past);
71            mem.total_incarnations += 1;
72        }
73        self.engine.mark_dirty();
74        Ok(())
75    }
76
77    /// Add a past incarnation.
78    pub fn add_past_life(&mut self, past: PastIncarnation) -> RealityResult<()> {
79        let mem = self
80            .engine
81            .deployment_store
82            .incarnation_memory
83            .get_or_insert_with(|| IncarnationMemory {
84                current: self
85                    .engine
86                    .deployment_store
87                    .soul
88                    .as_ref()
89                    .map(|s| s.incarnation_id)
90                    .unwrap_or_default(),
91                past_lives: vec![],
92                total_incarnations: 0,
93                total_uptime_secs: 0,
94                wisdom: vec![],
95                karma: IncarnationKarma {
96                    score: 0.0,
97                    good_deeds: 0,
98                    incidents: 0,
99                    trend: KarmaTrend::Stable,
100                },
101            });
102        mem.past_lives.push(past);
103        mem.total_incarnations += 1;
104        self.engine.mark_dirty();
105        Ok(())
106    }
107
108    /// Update deployment lineage.
109    pub fn update_lineage(&mut self, lineage: DeploymentLineage) -> RealityResult<()> {
110        let soul = self.engine.deployment_store.soul.as_mut().ok_or_else(|| {
111            crate::types::error::RealityError::NotInitialized("deployment soul".into())
112        })?;
113        soul.lineage = lineage;
114        self.engine.mark_dirty();
115        Ok(())
116    }
117
118    /// Set the deployment role.
119    pub fn set_role(&mut self, role: DeploymentRole) -> RealityResult<()> {
120        let soul = self.engine.deployment_store.soul.as_mut().ok_or_else(|| {
121            crate::types::error::RealityError::NotInitialized("deployment soul".into())
122        })?;
123        soul.role = role;
124        self.engine.mark_dirty();
125        Ok(())
126    }
127
128    /// Set the existential nature.
129    pub fn set_nature(&mut self, nature: ExistentialNature) -> RealityResult<()> {
130        let soul = self.engine.deployment_store.soul.as_mut().ok_or_else(|| {
131            crate::types::error::RealityError::NotInitialized("deployment soul".into())
132        })?;
133        soul.nature = nature;
134        self.engine.mark_dirty();
135        Ok(())
136    }
137
138    /// Update cardinality.
139    pub fn update_cardinality(&mut self, cardinality: Cardinality) -> RealityResult<()> {
140        let soul = self.engine.deployment_store.soul.as_mut().ok_or_else(|| {
141            crate::types::error::RealityError::NotInitialized("deployment soul".into())
142        })?;
143        soul.nature.cardinality = cardinality;
144        self.engine.mark_dirty();
145        Ok(())
146    }
147
148    /// Record a piece of wisdom from a past incarnation.
149    pub fn record_wisdom(&mut self, wisdom: IncarnationWisdom) -> RealityResult<()> {
150        let soul = self.engine.deployment_store.soul.as_mut().ok_or_else(|| {
151            crate::types::error::RealityError::NotInitialized("deployment soul".into())
152        })?;
153        soul.lineage.wisdom.push(wisdom);
154        self.engine.mark_dirty();
155        Ok(())
156    }
157
158    /// Update incarnation karma.
159    pub fn update_karma(&mut self, karma: IncarnationKarma) -> RealityResult<()> {
160        let soul = self.engine.deployment_store.soul.as_mut().ok_or_else(|| {
161            crate::types::error::RealityError::NotInitialized("deployment soul".into())
162        })?;
163        soul.lineage.karma = karma;
164        self.engine.mark_dirty();
165        Ok(())
166    }
167}