lean_ctx/tools/
startup.rs1use super::server::LeanCtxServer;
2
3#[derive(Clone, Debug, Default)]
4pub(super) struct StartupContext {
5 pub(super) project_root: Option<String>,
6 pub(super) shell_cwd: Option<String>,
7}
8
9pub fn create_server() -> LeanCtxServer {
11 LeanCtxServer::new()
12}
13
14pub(super) fn has_project_marker(dir: &std::path::Path) -> bool {
15 crate::core::pathutil::has_project_marker(dir)
16}
17
18pub(super) fn is_suspicious_root(dir: &std::path::Path) -> bool {
19 crate::core::pathutil::is_agent_config_dir(dir)
20}
21
22pub(super) fn canonicalize_path(path: &std::path::Path) -> String {
23 crate::core::pathutil::safe_canonicalize_or_self(path)
24 .to_string_lossy()
25 .to_string()
26}
27
28pub(super) fn detect_startup_context(
29 explicit_project_root: Option<&str>,
30 startup_cwd: Option<&std::path::Path>,
31) -> StartupContext {
32 let shell_cwd = startup_cwd.map(canonicalize_path);
33 let project_root = explicit_project_root
34 .map(|root| canonicalize_path(std::path::Path::new(root)))
35 .or_else(|| {
36 startup_cwd
37 .and_then(maybe_derive_project_root_from_absolute)
38 .map(|p| canonicalize_path(&p))
39 });
40
41 let shell_cwd = match (shell_cwd, project_root.as_ref()) {
42 (Some(cwd), Some(root))
43 if std::path::Path::new(&cwd).starts_with(std::path::Path::new(root)) =>
44 {
45 Some(cwd)
46 }
47 (_, Some(root)) => Some(root.clone()),
48 (cwd, None) => cwd,
49 };
50
51 StartupContext {
52 project_root,
53 shell_cwd,
54 }
55}
56
57pub(super) fn maybe_derive_project_root_from_absolute(
58 abs: &std::path::Path,
59) -> Option<std::path::PathBuf> {
60 let mut cur = if abs.is_dir() {
61 abs.to_path_buf()
62 } else {
63 abs.parent()?.to_path_buf()
64 };
65 loop {
66 if has_project_marker(&cur) {
67 return Some(crate::core::pathutil::safe_canonicalize_or_self(&cur));
68 }
69 if !cur.pop() {
70 break;
71 }
72 }
73 None
74}
75
76pub(crate) fn auto_consolidate_knowledge(project_root: &str) {
82 let _ = crate::tools::ctx_knowledge::consolidate_project_knowledge_with(
83 project_root,
84 &crate::core::consolidation_engine::ConsolidateOptions::incremental_auto(),
85 );
86}