lean_ctx/core/context_kernel/
health.rs1#[derive(Debug, Clone, serde::Serialize)]
5pub struct HealthReport {
6 pub initialized: bool,
8 pub kernel_enabled: bool,
10 pub dedup_hit_rate: f64,
12 pub dedup_total_checks: usize,
14 pub schema_optimizations: usize,
16 pub schema_tokens_saved: usize,
18 pub evidence_total_envelopes: usize,
20 pub evidence_chain_entries: usize,
22 pub config_source: String,
24 pub subsystem_count: usize,
26}
27
28#[must_use]
30pub fn kernel_health() -> HealthReport {
31 let startup_status = super::startup::status();
32 let initialized = super::startup::is_initialized() && startup_status.initialized;
33 let configured_features = super::kernel_config::features();
34 let kernel_enabled = super::kernel_config::is_enabled()
35 && startup_status.kernel_enabled
36 && configured_features.enabled;
37 let dedup = super::dedup_wiring::dedup_stats();
38 let schema = super::schema_wiring::schema_savings();
39 let evidence = super::envelope_wiring::evidence_summary();
40 let (effective_features, config_source) = super::config_bridge::effective_config();
41
42 HealthReport {
43 initialized,
44 kernel_enabled: kernel_enabled && effective_features.enabled,
45 dedup_hit_rate: dedup.hit_rate,
46 dedup_total_checks: dedup.total_checks,
47 schema_optimizations: schema.optimizations_applied,
48 schema_tokens_saved: schema.total_tokens_saved,
49 evidence_total_envelopes: evidence.total_envelopes,
50 evidence_chain_entries: evidence.chain_entries,
51 config_source: format!("{config_source:?}"),
52 subsystem_count: subsystem_names().len(),
53 }
54}
55
56#[must_use]
58pub fn is_healthy() -> bool {
59 let report = kernel_health();
60 report.initialized && report.kernel_enabled
61}
62
63#[must_use]
65pub fn format_health() -> String {
66 let report = kernel_health();
67 let state = if report.kernel_enabled { "ON" } else { "OFF" };
68 format!(
69 "Kernel: {state} | Dedup: {:.0}% hit | Schema: {} opts, {} tok saved | Evidence: {} entries",
70 report.dedup_hit_rate * 100.0,
71 report.schema_optimizations,
72 report.schema_tokens_saved,
73 report.evidence_chain_entries,
74 )
75}
76
77#[must_use]
79pub fn subsystem_names() -> &'static [&'static str] {
80 &[
81 "startup",
82 "kernel_config",
83 "dedup",
84 "schema",
85 "evidence",
86 "config_bridge",
87 ]
88}
89
90#[cfg(test)]
91mod tests {
92 use super::{format_health, is_healthy, kernel_health};
93 use crate::core::context_kernel::{
94 dedup_wiring, envelope_wiring, kernel_config, schema_wiring, startup,
95 };
96
97 fn isolated() -> std::sync::MutexGuard<'static, ()> {
98 let guard = kernel_config::KERNEL_TEST_LOCK
99 .lock()
100 .unwrap_or_else(std::sync::PoisonError::into_inner);
101 kernel_config::reset_features();
102 startup::reset();
103 dedup_wiring::reset_dedup();
104 schema_wiring::reset_schema_state();
105 envelope_wiring::reset_evidence();
106 guard
107 }
108
109 #[test]
110 fn health_after_init() {
111 let _guard = isolated();
112 startup::initialize();
113 assert!(kernel_health().initialized);
114 }
115
116 #[test]
117 fn health_shows_dedup_stats() {
118 let _guard = isolated();
119 let _ = dedup_wiring::check_content("health.rs", "same");
120 let _ = dedup_wiring::check_content("health.rs", "same");
121 let report = kernel_health();
122 assert!(report.dedup_hit_rate > 0.0);
123 assert_eq!(report.dedup_total_checks, 2);
124 }
125
126 #[test]
127 fn healthy_when_enabled() {
128 let _guard = isolated();
129 startup::initialize();
130 assert!(is_healthy());
131 }
132
133 #[test]
134 fn format_readable() {
135 let _guard = isolated();
136 startup::initialize();
137 assert!(format_health().contains("Kernel:"));
138 }
139}