Skip to main content

lean_ctx/core/context_kernel/
startup.rs

1//! Context Kernel startup initialization and status reporting.
2
3use std::sync::atomic::{AtomicBool, Ordering};
4
5static INITIALIZED: AtomicBool = AtomicBool::new(false);
6
7/// Snapshot of Context Kernel startup and configuration state.
8#[derive(Debug, Clone, serde::Serialize)]
9pub struct StartupStatus {
10    /// Whether [`initialize`] has completed.
11    pub initialized: bool,
12    /// Whether the Context Kernel master switch is enabled.
13    pub kernel_enabled: bool,
14    /// Effective Context Kernel feature configuration.
15    pub features: super::kernel_config::KernelFeatures,
16    /// Source of the effective configuration.
17    pub config_source: super::config_bridge::ConfigSource,
18}
19
20/// Loads Context Kernel configuration and marks startup initialization complete.
21pub fn initialize() {
22    super::config_bridge::apply_config();
23    INITIALIZED.store(true, Ordering::Release);
24    tracing::info!(
25        kernel_enabled = super::kernel_config::is_enabled(),
26        content_dedup = super::kernel_config::features().content_dedup,
27        schema_optimization = super::kernel_config::features().schema_optimization,
28        "Context Kernel initialized"
29    );
30}
31
32/// Reloads Context Kernel configuration without changing startup state.
33pub fn reinitialize() {
34    super::config_bridge::apply_config();
35}
36
37/// Returns whether startup initialization has completed.
38#[must_use]
39pub fn is_initialized() -> bool {
40    INITIALIZED.load(Ordering::Acquire)
41}
42
43/// Returns a snapshot of Context Kernel startup and configuration state.
44#[must_use]
45pub fn status() -> StartupStatus {
46    let (features, config_source) = super::config_bridge::effective_config();
47    StartupStatus {
48        initialized: is_initialized(),
49        kernel_enabled: features.enabled,
50        features,
51        config_source,
52    }
53}
54
55/// Clears startup initialization state for tests.
56pub fn reset() {
57    INITIALIZED.store(false, Ordering::Release);
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    fn isolated() -> std::sync::MutexGuard<'static, ()> {
65        let guard = super::super::kernel_config::KERNEL_TEST_LOCK
66            .lock()
67            .unwrap_or_else(std::sync::PoisonError::into_inner);
68        super::super::kernel_config::reset_features();
69        reset();
70        guard
71    }
72
73    #[test]
74    fn initialize_sets_flag() {
75        let _guard = isolated();
76        initialize();
77        assert!(is_initialized());
78    }
79
80    #[test]
81    fn status_reflects_config() {
82        let _guard = isolated();
83        let startup = status();
84        assert!(startup.kernel_enabled);
85        assert!(startup.features.enabled);
86    }
87
88    #[test]
89    fn reinitialize_updates() {
90        let _guard = isolated();
91        let changed = super::super::kernel_config::KernelFeatures {
92            content_dedup: false,
93            ..super::super::kernel_config::KernelFeatures::default()
94        };
95        super::super::kernel_config::update_features(changed);
96        reinitialize();
97        assert_eq!(
98            super::super::kernel_config::features().content_dedup,
99            super::super::kernel_config::from_env().content_dedup
100        );
101    }
102
103    #[test]
104    fn double_init_safe() {
105        let _guard = isolated();
106        initialize();
107        initialize();
108        assert!(is_initialized());
109    }
110}