1use super::serde_defaults;
9#[allow(clippy::wildcard_imports)]
10use super::*;
11use serde::{Deserialize, Serialize};
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14#[serde(default)]
15pub struct SecretDetectionConfig {
16 pub enabled: bool,
17 pub redact: bool,
18 pub custom_patterns: Vec<String>,
19}
20
21#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(default)]
27pub struct SetupConfig {
28 pub auto_inject_rules: Option<bool>,
32 pub auto_inject_skills: Option<bool>,
35 #[serde(default = "serde_defaults::default_true")]
37 pub auto_update_mcp: bool,
38}
39
40impl Default for SetupConfig {
41 fn default() -> Self {
42 Self {
43 auto_inject_rules: None,
44 auto_inject_skills: None,
45 auto_update_mcp: true,
46 }
47 }
48}
49
50impl SetupConfig {
51 pub fn should_inject_rules(&self) -> bool {
55 match self.auto_inject_rules {
56 Some(v) => v,
57 None => Self::rules_already_present(),
58 }
59 }
60
61 pub fn should_inject_skills(&self) -> bool {
63 match self.auto_inject_skills {
64 Some(v) => v,
65 None => Self::rules_already_present(),
66 }
67 }
68
69 pub fn should_update_mcp(&self) -> bool {
74 self.auto_update_mcp
75 }
76
77 fn rules_already_present() -> bool {
85 let Some(home) = dirs::home_dir() else {
86 return false;
87 };
88 if crate::rules_inject::any_rules_marker_present(&home) {
89 return true;
90 }
91 let marker = crate::rules_inject::RULES_MARKER;
92 let legacy_paths = [
93 crate::core::editor_registry::claude_rules_dir(&home).join("lean-ctx.md"),
94 crate::core::editor_registry::codebuddy_rules_dir(&home).join("lean-ctx.md"),
95 ];
96 legacy_paths
97 .iter()
98 .any(|p| std::fs::read_to_string(p).is_ok_and(|c| c.contains(marker)))
99 }
100}
101
102impl Default for SecretDetectionConfig {
103 fn default() -> Self {
104 Self {
105 enabled: true,
106 redact: true,
107 custom_patterns: Vec::new(),
108 }
109 }
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize)]
114#[serde(default)]
115pub struct ArchiveConfig {
116 pub enabled: bool,
117 pub threshold_chars: usize,
118 pub max_age_hours: u64,
119 pub max_disk_mb: u64,
120 pub ephemeral: bool,
121 pub ephemeral_min_tokens: usize,
124}
125
126impl Default for ArchiveConfig {
127 fn default() -> Self {
128 Self {
129 enabled: true,
130 threshold_chars: 800,
131 max_age_hours: 48,
132 max_disk_mb: 500,
133 ephemeral: true,
134 ephemeral_min_tokens: 2000,
135 }
136 }
137}
138
139impl ArchiveConfig {
140 pub fn ephemeral_effective(&self) -> bool {
141 if let Ok(v) = std::env::var("LEAN_CTX_EPHEMERAL") {
142 return !matches!(v.trim(), "0" | "false" | "off");
143 }
144 self.ephemeral && self.enabled
145 }
146
147 pub fn ephemeral_min_tokens_effective(&self) -> usize {
148 if let Ok(v) = std::env::var("LEAN_CTX_EPHEMERAL_MIN_TOKENS")
149 && let Ok(n) = v.trim().parse::<usize>()
150 {
151 return n;
152 }
153 self.ephemeral_min_tokens
154 }
155}
156
157#[derive(Debug, Clone, Serialize, Deserialize)]
161#[serde(default)]
162pub struct ProvidersConfig {
163 pub enabled: bool,
165 pub github: ProviderEntryConfig,
167 pub gitlab: ProviderEntryConfig,
169 pub auto_index: bool,
171 pub cache_ttl_secs: u64,
173 #[serde(default)]
175 pub mcp_bridges: std::collections::HashMap<String, McpBridgeEntry>,
176}
177
178impl Default for ProvidersConfig {
179 fn default() -> Self {
180 Self {
181 enabled: true,
182 github: ProviderEntryConfig::default(),
183 gitlab: ProviderEntryConfig::default(),
184 auto_index: true,
185 cache_ttl_secs: 120,
186 mcp_bridges: std::collections::HashMap::new(),
187 }
188 }
189}
190
191#[derive(Debug, Clone, Serialize, Deserialize)]
192pub struct McpBridgeEntry {
193 #[serde(default)]
195 pub url: Option<String>,
196 #[serde(default)]
198 pub command: Option<String>,
199 #[serde(default)]
201 pub args: Vec<String>,
202 #[serde(default)]
204 pub description: Option<String>,
205 #[serde(default)]
207 pub auth_env: Option<String>,
208}
209
210#[derive(Debug, Clone, Serialize, Deserialize)]
212#[serde(default)]
213pub struct ProviderEntryConfig {
214 pub enabled: bool,
216 pub token: Option<String>,
218 pub api_url: Option<String>,
220 pub project: Option<String>,
222}
223
224impl Default for ProviderEntryConfig {
225 fn default() -> Self {
226 Self {
227 enabled: true,
228 token: None,
229 api_url: None,
230 project: None,
231 }
232 }
233}
234
235#[derive(Debug, Clone, Serialize, Deserialize)]
237#[serde(default)]
238pub struct AutonomyConfig {
239 pub enabled: bool,
240 pub auto_preload: bool,
241 pub auto_dedup: bool,
242 pub auto_related: bool,
243 pub auto_consolidate: bool,
244 pub silent_preload: bool,
245 pub dedup_threshold: usize,
246 pub consolidate_every_calls: u32,
247 pub consolidate_cooldown_secs: u64,
248 #[serde(default = "serde_defaults::default_true")]
249 pub cognition_loop_enabled: bool,
250 #[serde(default = "serde_defaults::default_cognition_loop_interval")]
251 pub cognition_loop_interval_secs: u64,
252 #[serde(default = "serde_defaults::default_cognition_loop_max_steps")]
253 pub cognition_loop_max_steps: u8,
254}
255
256impl Default for AutonomyConfig {
257 fn default() -> Self {
258 Self {
259 enabled: true,
260 auto_preload: true,
261 auto_dedup: true,
262 auto_related: true,
263 auto_consolidate: true,
264 silent_preload: true,
265 dedup_threshold: 8,
266 consolidate_every_calls: 25,
267 consolidate_cooldown_secs: 120,
268 cognition_loop_enabled: true,
269 cognition_loop_interval_secs: 3600,
270 cognition_loop_max_steps: 8,
271 }
272 }
273}
274
275#[derive(Debug, Clone, Serialize, Deserialize)]
278#[serde(default)]
279pub struct UpdatesConfig {
280 pub auto_update: bool,
281 pub check_interval_hours: u64,
282 pub notify_only: bool,
283}
284
285impl Default for UpdatesConfig {
286 fn default() -> Self {
287 Self {
288 auto_update: false,
289 check_interval_hours: 6,
290 notify_only: false,
291 }
292 }
293}
294
295impl UpdatesConfig {
296 pub fn from_env() -> Self {
297 let mut cfg = Self::default();
298 if let Ok(v) = std::env::var("LEAN_CTX_AUTO_UPDATE") {
299 cfg.auto_update = v == "1" || v.eq_ignore_ascii_case("true");
300 }
301 if let Ok(v) = std::env::var("LEAN_CTX_UPDATE_INTERVAL_HOURS")
302 && let Ok(h) = v.parse::<u64>()
303 {
304 cfg.check_interval_hours = h.clamp(1, 168);
305 }
306 if let Ok(v) = std::env::var("LEAN_CTX_UPDATE_NOTIFY_ONLY") {
307 cfg.notify_only = v == "1" || v.eq_ignore_ascii_case("true");
308 }
309 cfg
310 }
311}
312
313impl AutonomyConfig {
314 pub fn from_env() -> Self {
316 let mut cfg = Self::default();
317 if let Ok(v) = std::env::var("LEAN_CTX_AUTONOMY")
318 && (v == "false" || v == "0")
319 {
320 cfg.enabled = false;
321 }
322 if let Ok(v) = std::env::var("LEAN_CTX_AUTO_PRELOAD") {
323 cfg.auto_preload = v != "false" && v != "0";
324 }
325 if let Ok(v) = std::env::var("LEAN_CTX_AUTO_DEDUP") {
326 cfg.auto_dedup = v != "false" && v != "0";
327 }
328 if let Ok(v) = std::env::var("LEAN_CTX_AUTO_RELATED") {
329 cfg.auto_related = v != "false" && v != "0";
330 }
331 if let Ok(v) = std::env::var("LEAN_CTX_AUTO_CONSOLIDATE") {
332 cfg.auto_consolidate = v != "false" && v != "0";
333 }
334 if let Ok(v) = std::env::var("LEAN_CTX_SILENT_PRELOAD") {
335 cfg.silent_preload = v != "false" && v != "0";
336 }
337 if let Ok(v) = std::env::var("LEAN_CTX_DEDUP_THRESHOLD")
338 && let Ok(n) = v.parse()
339 {
340 cfg.dedup_threshold = n;
341 }
342 if let Ok(v) = std::env::var("LEAN_CTX_CONSOLIDATE_EVERY_CALLS")
343 && let Ok(n) = v.parse()
344 {
345 cfg.consolidate_every_calls = n;
346 }
347 if let Ok(v) = std::env::var("LEAN_CTX_CONSOLIDATE_COOLDOWN_SECS")
348 && let Ok(n) = v.parse()
349 {
350 cfg.consolidate_cooldown_secs = n;
351 }
352 if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_LOOP_ENABLED") {
353 cfg.cognition_loop_enabled = v != "false" && v != "0";
354 }
355 if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_LOOP_INTERVAL_SECS")
356 && let Ok(n) = v.parse()
357 {
358 cfg.cognition_loop_interval_secs = n;
359 }
360 if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_LOOP_MAX_STEPS")
361 && let Ok(n) = v.parse()
362 {
363 cfg.cognition_loop_max_steps = n;
364 }
365 cfg
366 }
367
368 pub fn load() -> Self {
370 let file_cfg = Config::load().autonomy;
371 let mut cfg = file_cfg;
372 if let Ok(v) = std::env::var("LEAN_CTX_AUTONOMY")
373 && (v == "false" || v == "0")
374 {
375 cfg.enabled = false;
376 }
377 if let Ok(v) = std::env::var("LEAN_CTX_AUTO_PRELOAD") {
378 cfg.auto_preload = v != "false" && v != "0";
379 }
380 if let Ok(v) = std::env::var("LEAN_CTX_AUTO_DEDUP") {
381 cfg.auto_dedup = v != "false" && v != "0";
382 }
383 if let Ok(v) = std::env::var("LEAN_CTX_AUTO_RELATED") {
384 cfg.auto_related = v != "false" && v != "0";
385 }
386 if let Ok(v) = std::env::var("LEAN_CTX_SILENT_PRELOAD") {
387 cfg.silent_preload = v != "false" && v != "0";
388 }
389 if let Ok(v) = std::env::var("LEAN_CTX_DEDUP_THRESHOLD")
390 && let Ok(n) = v.parse()
391 {
392 cfg.dedup_threshold = n;
393 }
394 if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_LOOP_ENABLED") {
395 cfg.cognition_loop_enabled = v != "false" && v != "0";
396 }
397 if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_LOOP_INTERVAL_SECS")
398 && let Ok(n) = v.parse()
399 {
400 cfg.cognition_loop_interval_secs = n;
401 }
402 if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_LOOP_MAX_STEPS")
403 && let Ok(n) = v.parse()
404 {
405 cfg.cognition_loop_max_steps = n;
406 }
407 cfg
408 }
409}
410
411#[derive(Debug, Clone, Serialize, Deserialize, Default)]
413#[serde(default)]
414pub struct CloudConfig {
415 pub contribute_enabled: bool,
416 pub last_contribute: Option<String>,
417 pub last_sync: Option<String>,
418 pub last_gain_sync: Option<String>,
419 pub last_model_pull: Option<String>,
420 pub auto_sync: bool,
424 pub last_auto_sync: Option<String>,
425 pub auto_index: bool,
430 pub last_index_push: std::collections::HashMap<String, String>,
433}
434
435#[derive(Debug, Clone, Serialize, Deserialize)]
442#[serde(default)]
443pub struct GainConfig {
444 pub auto_publish: bool,
447 pub leaderboard: bool,
449 pub display_name: Option<String>,
451 pub auto_publish_interval_hours: u64,
453 pub last_auto_publish: Option<String>,
456}
457
458impl Default for GainConfig {
459 fn default() -> Self {
460 Self {
461 auto_publish: false,
462 leaderboard: true,
463 display_name: None,
464 auto_publish_interval_hours: 24,
465 last_auto_publish: None,
466 }
467 }
468}
469
470#[derive(Debug, Clone, Serialize, Deserialize)]
480#[serde(default)]
481pub struct GraphConfig {
482 pub traversal_edges: bool,
486}
487
488impl Default for GraphConfig {
489 fn default() -> Self {
490 Self {
491 traversal_edges: true,
492 }
493 }
494}
495
496#[derive(Debug, Clone, Serialize, Deserialize)]
504#[serde(default)]
505pub struct SkillifyConfig {
506 pub enabled: bool,
509 pub scope: String,
512 pub min_confidence: f32,
515 pub min_recurrence: u32,
518}
519
520impl Default for SkillifyConfig {
521 fn default() -> Self {
522 Self {
523 enabled: true,
524 scope: "project".to_string(),
525 min_confidence: 0.7,
526 min_recurrence: 2,
527 }
528 }
529}
530
531#[derive(Debug, Clone, Serialize, Deserialize)]
536#[serde(default)]
537pub struct SummariesConfig {
538 pub enabled: bool,
541 pub every_n_turns: u32,
544 pub max_kept: u32,
546}
547
548impl Default for SummariesConfig {
549 fn default() -> Self {
550 Self {
551 enabled: true,
552 every_n_turns: 25,
553 max_kept: 100,
554 }
555 }
556}
557
558#[derive(Debug, Clone, Serialize, Deserialize)]
560pub struct AliasEntry {
561 pub command: String,
562 pub alias: String,
563}
564
565#[derive(Debug, Clone, Serialize, Deserialize)]
567#[serde(default)]
568pub struct LoopDetectionConfig {
569 pub normal_threshold: u32,
570 pub reduced_threshold: u32,
571 pub blocked_threshold: u32,
572 pub window_secs: u64,
573 pub search_group_limit: u32,
574 pub tool_total_limits: HashMap<String, u32>,
575}
576
577impl Default for LoopDetectionConfig {
578 fn default() -> Self {
579 let mut tool_total_limits = HashMap::new();
580 tool_total_limits.insert("ctx_read".to_string(), 100);
581 tool_total_limits.insert("ctx_search".to_string(), 80);
582 tool_total_limits.insert("ctx_shell".to_string(), 50);
583 tool_total_limits.insert("ctx_semantic_search".to_string(), 60);
584 Self {
585 normal_threshold: 2,
586 reduced_threshold: 4,
587 blocked_threshold: 0,
588 window_secs: 300,
589 search_group_limit: 10,
590 tool_total_limits,
591 }
592 }
593}
594
595#[derive(Debug, Clone, Default, Serialize, Deserialize)]
608#[serde(default)]
609pub struct EmbeddingConfig {
610 #[serde(default, skip_serializing_if = "Option::is_none")]
611 pub model: Option<String>,
612 #[serde(default, skip_serializing_if = "Option::is_none")]
613 pub dimensions: Option<usize>,
614 #[serde(default, skip_serializing_if = "Option::is_none")]
620 pub auto_download: Option<bool>,
621}