lean_ctx/tools/
server_paths.rs1use super::server::LeanCtxServer;
2use super::startup::{
3 has_project_marker, is_suspicious_root, maybe_derive_project_root_from_absolute,
4};
5
6impl LeanCtxServer {
7 pub fn checkpoint_interval_effective() -> usize {
8 if let Ok(v) = std::env::var("LEAN_CTX_CHECKPOINT_INTERVAL")
9 && let Ok(parsed) = v.trim().parse::<usize>()
10 {
11 return parsed;
12 }
13 let profile_interval = crate::core::profiles::active_profile()
14 .autonomy
15 .checkpoint_interval_effective();
16 if profile_interval > 0 {
17 return profile_interval as usize;
18 }
19 crate::core::config::Config::load().checkpoint_interval as usize
20 }
21
22 pub async fn resolve_path(&self, path: &str) -> Result<String, String> {
26 let normalized = crate::core::pathutil::normalize_tool_path(path);
27 if normalized.is_empty() || normalized == "." {
28 return Ok(normalized);
29 }
30 let p = std::path::Path::new(&normalized);
31
32 let (resolved, jail_root, extra_roots) = {
33 let session = self.session.read().await;
34 let jail_root = session
35 .project_root
36 .as_deref()
37 .or(session.shell_cwd.as_deref())
38 .unwrap_or(".")
39 .to_string();
40
41 let worktree_cwd = if p.is_absolute() {
49 None
50 } else {
51 session
52 .project_root
53 .as_deref()
54 .zip(session.shell_cwd.as_deref())
55 .filter(|(root, cwd)| {
56 crate::core::path_resolve::shell_cwd_is_divergent_checkout(root, cwd)
57 })
58 .map(|(_, cwd)| std::path::Path::new(cwd).join(&normalized))
59 };
60
61 let resolved = if let Some(overridden) = worktree_cwd {
62 overridden
63 } else if p.is_absolute() || p.exists() {
64 std::path::PathBuf::from(&normalized)
65 } else if let Some(ref root) = session.project_root {
66 let joined = std::path::Path::new(root).join(&normalized);
67 if joined.exists() {
68 joined
69 } else if let Some(ref cwd) = session.shell_cwd {
70 std::path::Path::new(cwd).join(&normalized)
71 } else {
72 std::path::Path::new(&jail_root).join(&normalized)
73 }
74 } else if let Some(ref cwd) = session.shell_cwd {
75 std::path::Path::new(cwd).join(&normalized)
76 } else {
77 std::path::Path::new(&jail_root).join(&normalized)
78 };
79
80 (resolved, jail_root, session.extra_roots.clone())
83 };
84
85 let jail_root_path = std::path::Path::new(&jail_root);
86 let jailed = match crate::core::pathjail::jail_path_with_roots(
87 &resolved,
88 jail_root_path,
89 &extra_roots,
90 ) {
91 Ok(p) => self
92 .maybe_reroot_for_absolute_path(&resolved, jail_root_path, &extra_roots)
93 .await?
94 .unwrap_or(p),
95 Err(e) => {
96 if let Some((label, cache_root)) =
104 crate::core::pathjail::detect_language_cache_root(&resolved)
105 && crate::core::pathjail::register_session_read_only_root(&cache_root)
106 {
107 let Ok(jailed) = crate::core::pathjail::jail_path_with_roots(
108 &resolved,
109 jail_root_path,
110 &extra_roots,
111 ) else {
112 return Err(format!(
113 "Auto-detected {label} at {} — added as a read-only root for \
114 this session. Retry the read.",
115 cache_root.display()
116 ));
117 };
118 tracing::info!(
119 "auto-registered {label} at {} as a read-only root",
120 cache_root.display()
121 );
122 jailed
123 } else if let Some(jailed) = self
124 .maybe_reroot_for_absolute_path(&resolved, jail_root_path, &extra_roots)
125 .await?
126 {
127 jailed
128 } else {
129 crate::core::security_events::record_one(
132 "resolve_path",
133 "unknown",
134 crate::core::security_events::SecurityKind::PathBlocked,
135 );
136 return Err(e.to_string());
137 }
138 }
139 };
140
141 crate::core::io_boundary::check_secret_path_for_tool("resolve_path", &jailed)?;
142
143 Ok(crate::core::pathutil::normalize_tool_path(
144 &jailed.to_string_lossy().replace('\\', "/"),
145 ))
146 }
147
148 async fn maybe_reroot_for_absolute_path(
149 &self,
150 resolved: &std::path::Path,
151 jail_root_path: &std::path::Path,
152 extra_roots: &[String],
153 ) -> Result<Option<std::path::PathBuf>, String> {
154 if !resolved.is_absolute() {
155 return Ok(None);
156 }
157 let Some(new_root) = maybe_derive_project_root_from_absolute(resolved) else {
158 return Ok(None);
159 };
160 let jail_is_real_project =
161 has_project_marker(jail_root_path) && !is_suspicious_root(jail_root_path);
162 let candidate_under_jail = resolved.starts_with(jail_root_path);
163 if jail_is_real_project
166 && new_root.starts_with(jail_root_path)
167 && new_root != jail_root_path
168 {
169 return Ok(None);
170 }
171 let allow_reroot = if candidate_under_jail && jail_is_real_project {
175 false
176 } else {
177 !jail_is_real_project || self.session.read().await.project_root.is_none()
178 };
179
180 if !allow_reroot {
181 return Ok(None);
182 }
183
184 self.reroot_to_project(&new_root).await;
185 crate::core::pathjail::jail_path_with_roots(resolved, &new_root, extra_roots)
186 .map(Some)
187 .map_err(|e| e.to_string())
188 }
189
190 async fn reroot_to_project(&self, new_root: &std::path::Path) {
191 let mut session = self.session.write().await;
192 let new_root_str = new_root.to_string_lossy().to_string();
193 session.project_root = Some(new_root_str.clone());
194 session.shell_cwd = self
195 .startup_shell_cwd
196 .as_ref()
197 .filter(|cwd| std::path::Path::new(cwd).starts_with(new_root))
198 .cloned()
199 .or_else(|| Some(new_root_str.clone()));
200 let _ = session.save();
201 }
202
203 pub async fn resolve_path_or_passthrough(&self, path: &str) -> String {
205 self.resolve_path(path)
206 .await
207 .unwrap_or_else(|_| path.to_string())
208 }
209}