Skip to main content

hippmem_engine/
inspect_api.rs

1//! Engine::inspect — diagnostics API (05 §6, 09 §4.5).
2
3use crate::{Engine, EngineResult, InspectQuery};
4use hippmem_core::model::links::RecallChannel;
5
6impl Engine {
7    /// Diagnostic query: StoreStats/QueueStatus, etc.
8    pub fn inspect(&self, query: InspectQuery) -> EngineResult<crate::InspectReport> {
9        match query {
10            InspectQuery::StoreStats => {
11                let units = crate::retrieve_api::load_all_units(self.store.db_arc());
12                let mut edge_count = 0u64;
13                for u in &units {
14                    edge_count += u.links.len() as u64;
15                }
16                Ok(crate::InspectReport::StoreStats(crate::StoreStats {
17                    memory_count: units.len() as u64,
18                    edge_count,
19                    observing_edge_count: 0,
20                    per_index_size: vec![
21                        (RecallChannel::Bm25, 0),
22                        (RecallChannel::EntityInverted, 0),
23                    ],
24                    queue_backlog: 0,
25                    store_bytes: 0,
26                }))
27            }
28            InspectQuery::QueueStatus => {
29                Ok(crate::InspectReport::QueueStatus(crate::QueueStatus {
30                    pending_enrich: 0,
31                    pending_consolidate: 0,
32                    in_flight: 0,
33                    oldest_pending_age_ms: 0,
34                }))
35            }
36            InspectQuery::Memory(id) => {
37                let units = crate::retrieve_api::load_all_units(self.store.db_arc());
38                let unit = units
39                    .iter()
40                    .find(|u| u.id == id)
41                    .cloned()
42                    .ok_or(crate::EngineError::NotFound(id))?;
43
44                let out_edges: Vec<crate::EdgeView> = unit
45                    .links
46                    .iter()
47                    .map(|l| crate::EdgeView {
48                        from: unit.id,
49                        to: l.target_id,
50                        link_type: l.link_type,
51                        strength: l.strength.value(),
52                        confidence: l.confidence.value(),
53                        activation_count: l.activation_count,
54                        evidence: l.evidence.note.clone().unwrap_or_default(),
55                    })
56                    .collect();
57
58                // In-edges: iterate over all memories, finding edges where target_id == queried id
59                let in_edges: Vec<crate::EdgeView> = units
60                    .iter()
61                    .flat_map(|other| {
62                        other.links.iter().filter_map(move |l| {
63                            if l.target_id == id {
64                                Some(crate::EdgeView {
65                                    from: other.id,
66                                    to: l.target_id,
67                                    link_type: l.link_type,
68                                    strength: l.strength.value(),
69                                    confidence: l.confidence.value(),
70                                    activation_count: l.activation_count,
71                                    evidence: l.evidence.note.clone().unwrap_or_default(),
72                                })
73                            } else {
74                                None
75                            }
76                        })
77                    })
78                    .collect();
79
80                let stage = unit.stage;
81                let lifecycle = unit.lifecycle.clone();
82
83                Ok(crate::InspectReport::Memory(Box::new(
84                    crate::MemoryInspect {
85                        unit,
86                        out_edges,
87                        in_edges,
88                        stage,
89                        lifecycle,
90                    },
91                )))
92            }
93            _ => Ok(crate::InspectReport::StoreStats(crate::StoreStats {
94                memory_count: 0,
95                edge_count: 0,
96                observing_edge_count: 0,
97                per_index_size: vec![],
98                queue_backlog: 0,
99                store_bytes: 0,
100            })),
101        }
102    }
103}