lean_ctx/tools/
server_lifecycle.rs1use std::path::Path;
2use std::sync::atomic::AtomicUsize;
3use std::sync::Arc;
4use std::time::Instant;
5use tokio::sync::RwLock;
6
7use crate::core::cache::SessionCache;
8use crate::core::session::SessionState;
9
10use super::autonomy;
11use super::server::{LeanCtxServer, SessionMode};
12use super::startup::detect_startup_context;
13
14impl Default for LeanCtxServer {
15 fn default() -> Self {
16 Self::new()
17 }
18}
19
20impl LeanCtxServer {
21 pub fn new() -> Self {
23 Self::new_with_project_root(None)
24 }
25
26 pub fn new_with_project_root(project_root: Option<&str>) -> Self {
28 Self::new_with_startup(
29 project_root,
30 std::env::current_dir().ok().as_deref(),
31 SessionMode::Personal,
32 "default",
33 "default",
34 )
35 }
36
37 pub fn new_shared_with_context(
39 project_root: &str,
40 workspace_id: &str,
41 channel_id: &str,
42 ) -> Self {
43 Self::new_with_startup(
44 Some(project_root),
45 std::env::current_dir().ok().as_deref(),
46 SessionMode::Shared,
47 workspace_id,
48 channel_id,
49 )
50 }
51
52 pub(crate) fn new_with_startup(
53 project_root: Option<&str>,
54 startup_cwd: Option<&Path>,
55 session_mode: SessionMode,
56 workspace_id: &str,
57 channel_id: &str,
58 ) -> Self {
59 let ttl = std::env::var("LEAN_CTX_CACHE_TTL")
60 .ok()
61 .and_then(|v| v.parse().ok())
62 .unwrap_or_else(|| {
63 let cfg = crate::core::config::Config::load();
64 crate::core::config::MemoryCleanup::effective(&cfg).idle_ttl_secs()
65 });
66
67 let startup = detect_startup_context(project_root, startup_cwd);
68 let (session, context_os) = match session_mode {
69 SessionMode::Personal => {
70 let mut session = if let Some(ref root) = startup.project_root {
71 SessionState::load_latest_for_project_root(root).unwrap_or_default()
72 } else {
73 SessionState::load_latest().unwrap_or_default()
74 };
75 if let Some(ref root) = startup.project_root {
76 session.project_root = Some(root.clone());
77 }
78 if let Some(ref cwd) = startup.shell_cwd {
79 session.shell_cwd = Some(cwd.clone());
80 }
81 (Arc::new(RwLock::new(session)), None)
82 }
83 SessionMode::Shared => {
84 let Some(ref root) = startup.project_root else {
85 return Self::new_with_startup(
87 project_root,
88 startup_cwd,
89 SessionMode::Personal,
90 workspace_id,
91 channel_id,
92 );
93 };
94 let rt = crate::core::context_os::runtime();
95 let session = rt
96 .shared_sessions
97 .get_or_load(root, workspace_id, channel_id);
98 rt.metrics.record_session_loaded();
99 if let Some(ref cwd) = startup.shell_cwd {
101 if let Ok(mut s) = session.try_write() {
102 s.shell_cwd = Some(cwd.clone());
103 }
104 }
105 (session, Some(rt))
106 }
107 };
108
109 Self {
110 cache: Arc::new(RwLock::new(SessionCache::new())),
111 session,
112 tool_calls: Arc::new(RwLock::new(Vec::new())),
113 call_count: Arc::new(AtomicUsize::new(0)),
114 cache_ttl_secs: ttl,
115 last_call: Arc::new(RwLock::new(Instant::now())),
116 agent_id: Arc::new(RwLock::new(None)),
117 client_name: Arc::new(RwLock::new(String::new())),
118 autonomy: Arc::new(autonomy::AutonomyState::new()),
119 loop_detector: Arc::new(RwLock::new(
120 crate::core::loop_detection::LoopDetector::with_config(
121 &crate::core::config::Config::load().loop_detection,
122 ),
123 )),
124 workflow: Arc::new(RwLock::new(
125 crate::core::workflow::load_active().ok().flatten(),
126 )),
127 ledger: Arc::new(RwLock::new(
128 crate::core::context_ledger::ContextLedger::new(),
129 )),
130 pipeline_stats: Arc::new(RwLock::new(crate::core::pipeline::PipelineStats::new())),
131 session_mode,
132 workspace_id: if workspace_id.trim().is_empty() {
133 "default".to_string()
134 } else {
135 workspace_id.trim().to_string()
136 },
137 channel_id: if channel_id.trim().is_empty() {
138 "default".to_string()
139 } else {
140 channel_id.trim().to_string()
141 },
142 context_os,
143 context_ir: None,
144 registry: Some(std::sync::Arc::new(
145 crate::server::registry::build_registry(),
146 )),
147 rules_stale_checked: Arc::new(std::sync::atomic::AtomicBool::new(false)),
148 last_seen_event_id: Arc::new(std::sync::atomic::AtomicI64::new(0)),
149 startup_project_root: startup.project_root,
150 startup_shell_cwd: startup.shell_cwd,
151 }
152 }
153
154 pub async fn check_idle_expiry(&self) {
156 if self.cache_ttl_secs == 0 {
157 return;
158 }
159 let last = *self.last_call.read().await;
160 if last.elapsed().as_secs() >= self.cache_ttl_secs {
161 {
162 let mut session = self.session.write().await;
163 let _ = session.save();
164 }
165 let mut cache = self.cache.write().await;
166 let count = cache.clear();
167 if count > 0 {
168 tracing::info!(
169 "Cache auto-cleared after {}s idle ({count} file(s))",
170 self.cache_ttl_secs
171 );
172 }
173 }
174 *self.last_call.write().await = Instant::now();
175 }
176
177 pub async fn shutdown(&self) {
179 {
180 let mut session = self.session.write().await;
181 let _ = session.save();
182 }
183 {
184 let mut cache = self.cache.write().await;
185 let count = cache.clear();
186 if count > 0 {
187 tracing::info!("[shutdown] cleared {count} cached file(s)");
188 }
189 }
190 crate::core::memory_guard::force_purge();
191 }
192}