Skip to main content

lean_ctx/core/config/
schema.rs

1//! Auto-generated config schema from `Config` struct metadata.
2//!
3//! Used by `lean-ctx config schema` to emit JSON and by
4//! `lean-ctx config validate` to check user config.toml files.
5
6use serde::Serialize;
7use std::collections::BTreeMap;
8
9#[derive(Debug, Clone, Serialize)]
10pub struct ConfigSchema {
11    pub version: u32,
12    pub sections: BTreeMap<String, SectionSchema>,
13}
14
15#[derive(Debug, Clone, Serialize)]
16pub struct SectionSchema {
17    pub description: String,
18    pub keys: BTreeMap<String, KeySchema>,
19}
20
21#[derive(Debug, Clone, Serialize)]
22pub struct KeySchema {
23    #[serde(rename = "type")]
24    pub ty: String,
25    pub default: serde_json::Value,
26    pub description: String,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub values: Option<Vec<String>>,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub env_override: Option<String>,
31}
32
33fn clean_f32(v: f32) -> serde_json::Value {
34    let clean: f64 = format!("{v}").parse().unwrap_or(v as f64);
35    serde_json::json!(clean)
36}
37
38fn key(ty: &str, default: serde_json::Value, desc: &str) -> KeySchema {
39    KeySchema {
40        ty: ty.to_string(),
41        default,
42        description: desc.to_string(),
43        values: None,
44        env_override: None,
45    }
46}
47
48fn key_enum(values: &[&str], default: &str, desc: &str) -> KeySchema {
49    KeySchema {
50        ty: "enum".to_string(),
51        default: serde_json::Value::String(default.to_string()),
52        description: desc.to_string(),
53        values: Some(values.iter().map(ToString::to_string).collect()),
54        env_override: None,
55    }
56}
57
58fn key_with_env(ty: &str, default: serde_json::Value, desc: &str, env: &str) -> KeySchema {
59    KeySchema {
60        ty: ty.to_string(),
61        default,
62        description: desc.to_string(),
63        values: None,
64        env_override: Some(env.to_string()),
65    }
66}
67
68fn key_enum_with_env(values: &[&str], default: &str, desc: &str, env: &str) -> KeySchema {
69    KeySchema {
70        ty: "enum".to_string(),
71        default: serde_json::Value::String(default.to_string()),
72        description: desc.to_string(),
73        values: Some(values.iter().map(ToString::to_string).collect()),
74        env_override: Some(env.to_string()),
75    }
76}
77
78impl ConfigSchema {
79    pub fn generate() -> Self {
80        let cfg = super::Config::default();
81        let mut sections = BTreeMap::new();
82
83        let mut root = BTreeMap::new();
84        root.insert(
85            "ultra_compact".into(),
86            key(
87                "bool",
88                serde_json::json!(false),
89                "Legacy flag for maximum compression (use compression_level instead)",
90            ),
91        );
92        root.insert(
93            "tee_mode".into(),
94            key_enum(
95                &["never", "failures", "always"],
96                "failures",
97                "Controls when shell output is tee'd to disk for later retrieval",
98            ),
99        );
100        root.insert(
101            "output_density".into(),
102            key_enum_with_env(
103                &["normal", "terse", "ultra"],
104                "normal",
105                "Controls how dense/compact MCP tool output is formatted",
106                "LEAN_CTX_OUTPUT_DENSITY",
107            ),
108        );
109        root.insert(
110            "checkpoint_interval".into(),
111            key(
112                "u32",
113                serde_json::json!(cfg.checkpoint_interval),
114                "Session checkpoint interval in minutes",
115            ),
116        );
117        root.insert(
118            "excluded_commands".into(),
119            key(
120                "string[]",
121                serde_json::json!(cfg.excluded_commands),
122                "Commands to exclude from shell hook interception",
123            ),
124        );
125        root.insert(
126            "passthrough_urls".into(),
127            key(
128                "string[]",
129                serde_json::json!(cfg.passthrough_urls),
130                "URLs to pass through without proxy interception",
131            ),
132        );
133        root.insert("slow_command_threshold_ms".into(), key("u64", serde_json::json!(cfg.slow_command_threshold_ms), "Commands taking longer than this (ms) are recorded in the slow log. Set to 0 to disable"));
134        root.insert(
135            "theme".into(),
136            key(
137                "string",
138                serde_json::json!(cfg.theme),
139                "Dashboard color theme",
140            ),
141        );
142        root.insert(
143            "buddy_enabled".into(),
144            key(
145                "bool",
146                serde_json::json!(cfg.buddy_enabled),
147                "Enable the buddy system for multi-agent coordination",
148            ),
149        );
150        root.insert(
151            "redirect_exclude".into(),
152            key(
153                "string[]",
154                serde_json::json!(cfg.redirect_exclude),
155                "URL patterns to exclude from proxy redirection",
156            ),
157        );
158        root.insert(
159            "disabled_tools".into(),
160            key(
161                "string[]",
162                serde_json::json!(cfg.disabled_tools),
163                "Tools to exclude from the MCP tool list",
164            ),
165        );
166        root.insert(
167            "rules_scope".into(),
168            key_enum(
169                &["both", "global", "project"],
170                "both",
171                "Where agent rule files are installed. Override via LEAN_CTX_RULES_SCOPE",
172            ),
173        );
174        root.insert(
175            "extra_ignore_patterns".into(),
176            key(
177                "string[]",
178                serde_json::json!(cfg.extra_ignore_patterns),
179                "Extra glob patterns to ignore in graph/overview/preload",
180            ),
181        );
182        root.insert(
183            "terse_agent".into(),
184            key_enum_with_env(
185                &["off", "lite", "full", "ultra"],
186                "off",
187                "Controls agent output verbosity via instructions injection",
188                "LEAN_CTX_TERSE_AGENT",
189            ),
190        );
191        root.insert(
192            "compression_level".into(),
193            key_enum_with_env(
194                &["off", "lite", "standard", "max"],
195                "off",
196                "Unified compression level for all output",
197                "LEAN_CTX_COMPRESSION",
198            ),
199        );
200        root.insert(
201            "allow_paths".into(),
202            key_with_env(
203                "string[]",
204                serde_json::json!(cfg.allow_paths),
205                "Additional paths allowed by PathJail (absolute)",
206                "LEAN_CTX_ALLOW_PATH",
207            ),
208        );
209        root.insert(
210            "content_defined_chunking".into(),
211            key(
212                "bool",
213                serde_json::json!(false),
214                "Enable Rabin-Karp chunking for cache-optimal output ordering",
215            ),
216        );
217        root.insert(
218            "minimal_overhead".into(),
219            key_with_env(
220                "bool",
221                serde_json::json!(false),
222                "Skip session/knowledge/gotcha blocks in MCP instructions",
223                "LEAN_CTX_MINIMAL",
224            ),
225        );
226        root.insert(
227            "shell_hook_disabled".into(),
228            key_with_env(
229                "bool",
230                serde_json::json!(false),
231                "Disable shell hook injection",
232                "LEAN_CTX_NO_HOOK",
233            ),
234        );
235        root.insert(
236            "shell_activation".into(),
237            key_enum_with_env(
238                &["always", "agents-only", "off"],
239                "always",
240                "Controls when the shell hook auto-activates aliases",
241                "LEAN_CTX_SHELL_ACTIVATION",
242            ),
243        );
244        root.insert(
245            "update_check_disabled".into(),
246            key_with_env(
247                "bool",
248                serde_json::json!(false),
249                "Disable the daily version check",
250                "LEAN_CTX_NO_UPDATE_CHECK",
251            ),
252        );
253        root.insert(
254            "bm25_max_cache_mb".into(),
255            key_with_env(
256                "u64",
257                serde_json::json!(cfg.bm25_max_cache_mb),
258                "Maximum BM25 cache file size in MB",
259                "LEAN_CTX_BM25_MAX_CACHE_MB",
260            ),
261        );
262        root.insert(
263            "graph_index_max_files".into(),
264            key(
265                "u64",
266                serde_json::json!(cfg.graph_index_max_files),
267                "Maximum files in graph index. 0 = unlimited (default). Set >0 to cap for constrained systems",
268            ),
269        );
270        root.insert(
271            "memory_profile".into(),
272            key_enum_with_env(
273                &["low", "balanced", "performance"],
274                "balanced",
275                "Controls RAM vs feature trade-off",
276                "LEAN_CTX_MEMORY_PROFILE",
277            ),
278        );
279        root.insert(
280            "memory_cleanup".into(),
281            key_enum_with_env(
282                &["aggressive", "shared"],
283                "aggressive",
284                "Controls how aggressively memory is freed when idle",
285                "LEAN_CTX_MEMORY_CLEANUP",
286            ),
287        );
288        root.insert(
289            "savings_footer".into(),
290            key_enum_with_env(
291                &["auto", "always", "never"],
292                "auto",
293                "Controls visibility of token savings footers: auto (suppress in MCP, show in CLI), always, never",
294                "LEAN_CTX_SAVINGS_FOOTER",
295            ),
296        );
297        root.insert(
298            "max_ram_percent".into(),
299            key_with_env(
300                "u8",
301                serde_json::json!(cfg.max_ram_percent),
302                "Maximum percentage of system RAM that lean-ctx may use (1-50, default 5)",
303                "LEAN_CTX_MAX_RAM_PERCENT",
304            ),
305        );
306        sections.insert(
307            "root".into(),
308            SectionSchema {
309                description: "Top-level configuration keys".into(),
310                keys: root,
311            },
312        );
313
314        let mut archive = BTreeMap::new();
315        archive.insert(
316            "enabled".into(),
317            key(
318                "bool",
319                serde_json::json!(cfg.archive.enabled),
320                "Enable zero-loss compression archive",
321            ),
322        );
323        archive.insert(
324            "threshold_chars".into(),
325            key(
326                "usize",
327                serde_json::json!(cfg.archive.threshold_chars),
328                "Minimum output size (chars) to trigger archiving",
329            ),
330        );
331        archive.insert(
332            "max_age_hours".into(),
333            key(
334                "u64",
335                serde_json::json!(cfg.archive.max_age_hours),
336                "Maximum age of archived entries before cleanup",
337            ),
338        );
339        archive.insert(
340            "max_disk_mb".into(),
341            key(
342                "u64",
343                serde_json::json!(cfg.archive.max_disk_mb),
344                "Maximum total disk usage for the archive",
345            ),
346        );
347        sections.insert("archive".into(), SectionSchema {
348            description: "Settings for the zero-loss compression archive (large tool outputs saved to disk)".into(),
349            keys: archive,
350        });
351
352        let mut autonomy = BTreeMap::new();
353        autonomy.insert(
354            "enabled".into(),
355            key(
356                "bool",
357                serde_json::json!(cfg.autonomy.enabled),
358                "Enable autonomous background behaviors",
359            ),
360        );
361        autonomy.insert(
362            "auto_preload".into(),
363            key(
364                "bool",
365                serde_json::json!(cfg.autonomy.auto_preload),
366                "Auto-preload related files on first read",
367            ),
368        );
369        autonomy.insert(
370            "auto_dedup".into(),
371            key(
372                "bool",
373                serde_json::json!(cfg.autonomy.auto_dedup),
374                "Auto-deduplicate repeated reads",
375            ),
376        );
377        autonomy.insert(
378            "auto_related".into(),
379            key(
380                "bool",
381                serde_json::json!(cfg.autonomy.auto_related),
382                "Auto-load graph-related files",
383            ),
384        );
385        autonomy.insert(
386            "auto_consolidate".into(),
387            key(
388                "bool",
389                serde_json::json!(cfg.autonomy.auto_consolidate),
390                "Auto-consolidate knowledge periodically",
391            ),
392        );
393        autonomy.insert(
394            "silent_preload".into(),
395            key(
396                "bool",
397                serde_json::json!(cfg.autonomy.silent_preload),
398                "Suppress preload notifications in output",
399            ),
400        );
401        autonomy.insert(
402            "dedup_threshold".into(),
403            key(
404                "usize",
405                serde_json::json!(cfg.autonomy.dedup_threshold),
406                "Number of repeated reads before dedup triggers",
407            ),
408        );
409        autonomy.insert(
410            "consolidate_every_calls".into(),
411            key(
412                "u32",
413                serde_json::json!(cfg.autonomy.consolidate_every_calls),
414                "Consolidate knowledge every N tool calls",
415            ),
416        );
417        autonomy.insert(
418            "consolidate_cooldown_secs".into(),
419            key(
420                "u64",
421                serde_json::json!(cfg.autonomy.consolidate_cooldown_secs),
422                "Minimum seconds between consolidation runs",
423            ),
424        );
425        sections.insert(
426            "autonomy".into(),
427            SectionSchema {
428                description:
429                    "Controls autonomous background behaviors (preload, dedup, consolidation)"
430                        .into(),
431                keys: autonomy,
432            },
433        );
434
435        let mut loop_det = BTreeMap::new();
436        loop_det.insert(
437            "normal_threshold".into(),
438            key(
439                "u32",
440                serde_json::json!(cfg.loop_detection.normal_threshold),
441                "Repetitions before reducing output",
442            ),
443        );
444        loop_det.insert(
445            "reduced_threshold".into(),
446            key(
447                "u32",
448                serde_json::json!(cfg.loop_detection.reduced_threshold),
449                "Repetitions before further reducing output",
450            ),
451        );
452        loop_det.insert(
453            "blocked_threshold".into(),
454            key(
455                "u32",
456                serde_json::json!(cfg.loop_detection.blocked_threshold),
457                "Repetitions before blocking. 0 = disabled",
458            ),
459        );
460        loop_det.insert(
461            "window_secs".into(),
462            key(
463                "u64",
464                serde_json::json!(cfg.loop_detection.window_secs),
465                "Time window in seconds for loop detection",
466            ),
467        );
468        loop_det.insert(
469            "search_group_limit".into(),
470            key(
471                "u32",
472                serde_json::json!(cfg.loop_detection.search_group_limit),
473                "Maximum unique searches within a loop window",
474            ),
475        );
476        sections.insert(
477            "loop_detection".into(),
478            SectionSchema {
479                description: "Loop detection settings for preventing repeated identical tool calls"
480                    .into(),
481                keys: loop_det,
482            },
483        );
484
485        let mut cloud = BTreeMap::new();
486        cloud.insert(
487            "contribute_enabled".into(),
488            key(
489                "bool",
490                serde_json::json!(cfg.cloud.contribute_enabled),
491                "Enable contributing anonymized stats to lean-ctx cloud",
492            ),
493        );
494        sections.insert(
495            "cloud".into(),
496            SectionSchema {
497                description: "Cloud feature settings".into(),
498                keys: cloud,
499            },
500        );
501
502        let mut proxy = BTreeMap::new();
503        proxy.insert(
504            "anthropic_upstream".into(),
505            key(
506                "string?",
507                serde_json::json!(cfg.proxy.anthropic_upstream),
508                "Custom upstream URL for Anthropic API proxy",
509            ),
510        );
511        proxy.insert(
512            "openai_upstream".into(),
513            key(
514                "string?",
515                serde_json::json!(cfg.proxy.openai_upstream),
516                "Custom upstream URL for OpenAI API proxy",
517            ),
518        );
519        proxy.insert(
520            "gemini_upstream".into(),
521            key(
522                "string?",
523                serde_json::json!(cfg.proxy.gemini_upstream),
524                "Custom upstream URL for Gemini API proxy",
525            ),
526        );
527        sections.insert(
528            "proxy".into(),
529            SectionSchema {
530                description: "Proxy upstream configuration for API routing".into(),
531                keys: proxy,
532            },
533        );
534
535        let mem = &cfg.memory;
536        let mut mem_knowledge = BTreeMap::new();
537        mem_knowledge.insert(
538            "max_facts".into(),
539            key(
540                "usize",
541                serde_json::json!(mem.knowledge.max_facts),
542                "Maximum number of knowledge facts stored per project",
543            ),
544        );
545        mem_knowledge.insert(
546            "max_patterns".into(),
547            key(
548                "usize",
549                serde_json::json!(mem.knowledge.max_patterns),
550                "Maximum number of patterns stored",
551            ),
552        );
553        mem_knowledge.insert(
554            "max_history".into(),
555            key(
556                "usize",
557                serde_json::json!(mem.knowledge.max_history),
558                "Maximum history entries retained",
559            ),
560        );
561        mem_knowledge.insert(
562            "contradiction_threshold".into(),
563            key(
564                "f32",
565                clean_f32(mem.knowledge.contradiction_threshold),
566                "Confidence threshold for contradiction detection",
567            ),
568        );
569        mem_knowledge.insert(
570            "recall_facts_limit".into(),
571            key(
572                "usize",
573                serde_json::json!(mem.knowledge.recall_facts_limit),
574                "Maximum facts returned per recall query",
575            ),
576        );
577        mem_knowledge.insert(
578            "rooms_limit".into(),
579            key(
580                "usize",
581                serde_json::json!(mem.knowledge.rooms_limit),
582                "Maximum number of rooms returned",
583            ),
584        );
585        mem_knowledge.insert(
586            "timeline_limit".into(),
587            key(
588                "usize",
589                serde_json::json!(mem.knowledge.timeline_limit),
590                "Maximum number of timeline entries returned",
591            ),
592        );
593        mem_knowledge.insert(
594            "relations_limit".into(),
595            key(
596                "usize",
597                serde_json::json!(mem.knowledge.relations_limit),
598                "Maximum number of relations returned",
599            ),
600        );
601        sections.insert(
602            "memory.knowledge".into(),
603            SectionSchema {
604                description: "Knowledge memory budgets (facts, patterns, gotchas)".into(),
605                keys: mem_knowledge,
606            },
607        );
608
609        let mut mem_episodic = BTreeMap::new();
610        mem_episodic.insert(
611            "max_episodes".into(),
612            key(
613                "usize",
614                serde_json::json!(mem.episodic.max_episodes),
615                "Maximum number of episodes retained",
616            ),
617        );
618        mem_episodic.insert(
619            "max_actions_per_episode".into(),
620            key(
621                "usize",
622                serde_json::json!(mem.episodic.max_actions_per_episode),
623                "Maximum actions tracked per episode",
624            ),
625        );
626        mem_episodic.insert(
627            "summary_max_chars".into(),
628            key(
629                "usize",
630                serde_json::json!(mem.episodic.summary_max_chars),
631                "Maximum characters in episode summary",
632            ),
633        );
634        sections.insert(
635            "memory.episodic".into(),
636            SectionSchema {
637                description: "Episodic memory budgets (session episodes)".into(),
638                keys: mem_episodic,
639            },
640        );
641
642        let mut mem_procedural = BTreeMap::new();
643        mem_procedural.insert(
644            "max_procedures".into(),
645            key(
646                "usize",
647                serde_json::json!(mem.procedural.max_procedures),
648                "Maximum number of learned procedures stored",
649            ),
650        );
651        mem_procedural.insert(
652            "min_repetitions".into(),
653            key(
654                "usize",
655                serde_json::json!(mem.procedural.min_repetitions),
656                "Minimum repetitions before a pattern is stored",
657            ),
658        );
659        mem_procedural.insert(
660            "min_sequence_len".into(),
661            key(
662                "usize",
663                serde_json::json!(mem.procedural.min_sequence_len),
664                "Minimum sequence length for procedure detection",
665            ),
666        );
667        mem_procedural.insert(
668            "max_window_size".into(),
669            key(
670                "usize",
671                serde_json::json!(mem.procedural.max_window_size),
672                "Maximum window size for pattern analysis",
673            ),
674        );
675        sections.insert(
676            "memory.procedural".into(),
677            SectionSchema {
678                description: "Procedural memory budgets (learned patterns)".into(),
679                keys: mem_procedural,
680            },
681        );
682
683        let mut mem_lifecycle = BTreeMap::new();
684        mem_lifecycle.insert(
685            "decay_rate".into(),
686            key(
687                "f32",
688                clean_f32(mem.lifecycle.decay_rate),
689                "Rate at which knowledge confidence decays over time",
690            ),
691        );
692        mem_lifecycle.insert(
693            "low_confidence_threshold".into(),
694            key(
695                "f32",
696                clean_f32(mem.lifecycle.low_confidence_threshold),
697                "Threshold below which facts are considered low-confidence",
698            ),
699        );
700        mem_lifecycle.insert(
701            "stale_days".into(),
702            key(
703                "i64",
704                serde_json::json!(mem.lifecycle.stale_days),
705                "Days after which unused facts are considered stale",
706            ),
707        );
708        mem_lifecycle.insert(
709            "similarity_threshold".into(),
710            key(
711                "f32",
712                clean_f32(mem.lifecycle.similarity_threshold),
713                "Similarity threshold for deduplication",
714            ),
715        );
716        sections.insert(
717            "memory.lifecycle".into(),
718            SectionSchema {
719                description: "Knowledge lifecycle policy (decay, staleness, dedup)".into(),
720                keys: mem_lifecycle,
721            },
722        );
723
724        let mut mem_gotcha = BTreeMap::new();
725        mem_gotcha.insert(
726            "max_gotchas_per_project".into(),
727            key(
728                "usize",
729                serde_json::json!(mem.gotcha.max_gotchas_per_project),
730                "Maximum gotchas stored per project",
731            ),
732        );
733        mem_gotcha.insert(
734            "retrieval_budget_per_room".into(),
735            key(
736                "usize",
737                serde_json::json!(mem.gotcha.retrieval_budget_per_room),
738                "Maximum gotchas retrieved per room per query",
739            ),
740        );
741        mem_gotcha.insert(
742            "default_decay_rate".into(),
743            key(
744                "f32",
745                clean_f32(mem.gotcha.default_decay_rate),
746                "Default decay rate for gotcha importance",
747            ),
748        );
749        sections.insert(
750            "memory.gotcha".into(),
751            SectionSchema {
752                description: "Gotcha memory settings (project-specific warnings and pitfalls)"
753                    .into(),
754                keys: mem_gotcha,
755            },
756        );
757
758        let mut mem_embeddings = BTreeMap::new();
759        mem_embeddings.insert(
760            "max_facts".into(),
761            key(
762                "usize",
763                serde_json::json!(mem.embeddings.max_facts),
764                "Maximum number of embedding facts stored",
765            ),
766        );
767        sections.insert(
768            "memory.embeddings".into(),
769            SectionSchema {
770                description: "Embeddings memory settings for semantic search".into(),
771                keys: mem_embeddings,
772            },
773        );
774
775        let mut aliases = BTreeMap::new();
776        aliases.insert(
777            "command".into(),
778            key(
779                "string",
780                serde_json::json!(""),
781                "The command pattern to match (e.g. 'deploy')",
782            ),
783        );
784        aliases.insert(
785            "alias".into(),
786            key(
787                "string",
788                serde_json::json!(""),
789                "The alias definition to execute",
790            ),
791        );
792        sections.insert("custom_aliases".into(), SectionSchema {
793            description: "Custom command aliases (array of {command, alias} entries). Note: field names are 'command' and 'alias' (not 'name')".into(),
794            keys: aliases,
795        });
796
797        if let Some(root_section) = sections.get_mut("root") {
798            root_section.keys.insert(
799                "custom_aliases".into(),
800                key(
801                    "array",
802                    serde_json::json!([]),
803                    "Custom command aliases (array of {command, alias} entries)",
804                ),
805            );
806        }
807
808        ConfigSchema {
809            version: 1,
810            sections,
811        }
812    }
813
814    /// All known TOML keys (dot-separated) for validation.
815    pub fn known_keys(&self) -> Vec<String> {
816        let mut keys = Vec::new();
817        for (section, schema) in &self.sections {
818            for key_name in schema.keys.keys() {
819                if section == "root" {
820                    keys.push(key_name.clone());
821                } else {
822                    keys.push(format!("{section}.{key_name}"));
823                }
824            }
825        }
826        keys
827    }
828}