Skip to main content

agentic_reality/engine/write/
reality.rs

1//! Reality physics write operations.
2
3use crate::engine::write::WriteEngine;
4use crate::types::error::{RealityError, RealityResult};
5use crate::types::ids::AnchorId;
6use crate::types::reality::*;
7
8impl<'a> WriteEngine<'a> {
9    /// Set the current reality layer.
10    pub fn set_reality_layer(&mut self, layers: RealityLayers) -> RealityResult<()> {
11        self.engine.reality_store.layers = Some(layers);
12        self.engine.mark_dirty();
13        Ok(())
14    }
15
16    /// Update a specific layer status.
17    pub fn update_layer_status(&mut self, status: LayerStatus) -> RealityResult<()> {
18        let layers = self
19            .engine
20            .reality_store
21            .layers
22            .as_mut()
23            .ok_or_else(|| RealityError::NotInitialized("reality layers".into()))?;
24        layers.layers.push(status);
25        self.engine.mark_dirty();
26        Ok(())
27    }
28
29    /// Update freshness perception.
30    pub fn update_freshness(&mut self, freshness: FreshnessPerception) -> RealityResult<()> {
31        self.engine.reality_store.freshness = Some(freshness);
32        self.engine.mark_dirty();
33        Ok(())
34    }
35
36    /// Add a reality anchor.
37    pub fn add_anchor(&mut self, anchor: RealityAnchor) -> RealityResult<AnchorId> {
38        let id = anchor.id;
39        self.engine.reality_store.anchors.push(anchor);
40        self.engine.mark_dirty();
41        Ok(id)
42    }
43
44    /// Remove a reality anchor.
45    pub fn remove_anchor(&mut self, id: &AnchorId) -> RealityResult<()> {
46        let len = self.engine.reality_store.anchors.len();
47        self.engine.reality_store.anchors.retain(|a| a.id != *id);
48        if self.engine.reality_store.anchors.len() == len {
49            return Err(RealityError::NotFound(format!("anchor {}", id)));
50        }
51        self.engine.mark_dirty();
52        Ok(())
53    }
54
55    /// Verify an anchor and update its value.
56    pub fn verify_anchor(&mut self, id: &AnchorId, value: AnchorValue) -> RealityResult<()> {
57        let anchor = self
58            .engine
59            .reality_store
60            .anchors
61            .iter_mut()
62            .find(|a| a.id == *id)
63            .ok_or_else(|| RealityError::NotFound(format!("anchor {}", id)))?;
64        anchor.last_value = value;
65        self.engine.mark_dirty();
66        Ok(())
67    }
68
69    /// Record drift in an anchor.
70    pub fn record_anchor_drift(&mut self, drift: AnchorDrift) -> RealityResult<()> {
71        self.engine.reality_store.anchor_drifts.push(drift);
72        self.engine.mark_dirty();
73        Ok(())
74    }
75
76    /// Detect a hallucination.
77    pub fn detect_hallucination(
78        &mut self,
79        hallucination: DetectedHallucination,
80    ) -> RealityResult<()> {
81        let state = self
82            .engine
83            .reality_store
84            .hallucination_state
85            .get_or_insert_with(|| HallucinationState {
86                risk_level: HallucinationRisk::Low { confidence: 1.0 },
87                detected: vec![],
88                patterns: vec![],
89                grounding: GroundingStatus {
90                    grounded: true,
91                    anchor_count: 0,
92                    verified_count: 0,
93                    last_verification: None,
94                    confidence: 1.0,
95                },
96                pending_verification: vec![],
97            });
98        state.detected.push(hallucination);
99        self.engine.mark_dirty();
100        Ok(())
101    }
102
103    /// Clear a resolved hallucination.
104    pub fn clear_hallucination(&mut self, id: &str) -> RealityResult<()> {
105        if let Some(state) = &mut self.engine.reality_store.hallucination_state {
106            if let Some(h) = state.detected.iter_mut().find(|h| h.id == id) {
107                h.resolved = true;
108            }
109        }
110        self.engine.mark_dirty();
111        Ok(())
112    }
113
114    /// Add an unverified claim.
115    pub fn add_unverified_claim(&mut self, claim: UnverifiedClaim) -> RealityResult<()> {
116        let state = self
117            .engine
118            .reality_store
119            .hallucination_state
120            .get_or_insert_with(|| HallucinationState {
121                risk_level: HallucinationRisk::Low { confidence: 1.0 },
122                detected: vec![],
123                patterns: vec![],
124                grounding: GroundingStatus {
125                    grounded: true,
126                    anchor_count: 0,
127                    verified_count: 0,
128                    last_verification: None,
129                    confidence: 1.0,
130                },
131                pending_verification: vec![],
132            });
133        state.pending_verification.push(claim);
134        self.engine.mark_dirty();
135        Ok(())
136    }
137
138    /// Verify a pending claim (remove from pending).
139    pub fn verify_claim(&mut self, claim_text: &str) -> RealityResult<()> {
140        if let Some(state) = &mut self.engine.reality_store.hallucination_state {
141            state.pending_verification.retain(|c| c.claim != claim_text);
142        }
143        self.engine.mark_dirty();
144        Ok(())
145    }
146
147    /// Update grounding status.
148    pub fn update_grounding(&mut self, grounding: GroundingStatus) -> RealityResult<()> {
149        if let Some(state) = &mut self.engine.reality_store.hallucination_state {
150            state.grounding = grounding;
151        }
152        self.engine.mark_dirty();
153        Ok(())
154    }
155}