1use std::sync::atomic::AtomicUsize;
2use std::sync::Arc;
3use std::time::Instant;
4use tokio::sync::RwLock;
5
6use crate::core::cache::SessionCache;
7use crate::core::session::SessionState;
8
9pub(super) struct CepComputedStats {
10 pub(super) cep_score: u32,
11 pub(super) cache_util: u32,
12 pub(super) mode_diversity: u32,
13 pub(super) compression_rate: u32,
14 pub(super) total_original: u64,
15 pub(super) total_compressed: u64,
16 pub(super) total_saved: u64,
17 pub(super) mode_counts: std::collections::HashMap<String, u64>,
18 pub(super) complexity: String,
19 pub(super) cache_hits: u64,
20 pub(super) total_reads: u64,
21 pub(super) tool_call_count: u64,
22}
23
24pub use crate::core::protocol::CrpMode;
25impl CrpMode {
29 pub fn effective() -> Self {
31 if let Ok(v) = std::env::var("LEAN_CTX_CRP_MODE") {
32 if !v.trim().is_empty() {
33 return Self::parse(&v).unwrap_or(Self::Tdd);
34 }
35 }
36 let p = crate::core::profiles::active_profile();
37 Self::parse(p.compression.crp_mode_effective()).unwrap_or(Self::Tdd)
38 }
39
40 pub fn is_tdd(&self) -> bool {
42 *self == Self::Tdd
43 }
44}
45
46pub type SharedCache = Arc<RwLock<SessionCache>>;
48
49#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub enum SessionMode {
51 Personal,
53 Shared,
55}
56
57#[derive(Clone)]
59pub struct LeanCtxServer {
60 pub cache: SharedCache,
61 pub session: Arc<RwLock<SessionState>>,
62 pub tool_calls: Arc<RwLock<Vec<ToolCallRecord>>>,
63 pub call_count: Arc<AtomicUsize>,
64 pub cache_ttl_secs: u64,
65 pub last_call: Arc<RwLock<Instant>>,
66 pub agent_id: Arc<RwLock<Option<String>>>,
67 pub client_name: Arc<RwLock<String>>,
68 pub autonomy: Arc<super::autonomy::AutonomyState>,
69 pub loop_detector: Arc<RwLock<crate::core::loop_detection::LoopDetector>>,
70 pub workflow: Arc<RwLock<Option<crate::core::workflow::WorkflowRun>>>,
71 pub ledger: Arc<RwLock<crate::core::context_ledger::ContextLedger>>,
72 pub pipeline_stats: Arc<RwLock<crate::core::pipeline::PipelineStats>>,
73 pub session_mode: SessionMode,
74 pub workspace_id: String,
75 pub channel_id: String,
76 pub context_os: Option<Arc<crate::core::context_os::ContextOsRuntime>>,
77 pub context_ir: Option<Arc<RwLock<crate::core::context_ir::ContextIrV1>>>,
78 pub registry: Option<Arc<crate::server::registry::ToolRegistry>>,
79 pub(crate) rules_stale_checked: Arc<std::sync::atomic::AtomicBool>,
80 pub(crate) last_seen_event_id: Arc<std::sync::atomic::AtomicI64>,
81 pub(crate) startup_project_root: Option<String>,
82 pub(crate) startup_shell_cwd: Option<String>,
83}
84
85pub use crate::core::protocol::ToolCallRecord;