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            "enable_wakeup_ctx".into(),
152            key(
153                "bool",
154                serde_json::json!(cfg.enable_wakeup_ctx),
155                "Append wakeup briefing (facts, session summary) to ctx_overview output. Set false to reduce context bloat when calling ctx_overview frequently.",
156            ),
157        );
158        root.insert(
159            "redirect_exclude".into(),
160            key(
161                "string[]",
162                serde_json::json!(cfg.redirect_exclude),
163                "URL patterns to exclude from proxy redirection",
164            ),
165        );
166        root.insert(
167            "disabled_tools".into(),
168            key(
169                "string[]",
170                serde_json::json!(cfg.disabled_tools),
171                "Tools to exclude from the MCP tool list",
172            ),
173        );
174        root.insert(
175            "default_tool_categories".into(),
176            key(
177                "string[]",
178                serde_json::json!(cfg.default_tool_categories),
179                "Tool categories active by default (core, arch, debug, memory, metrics, session). Override via LCTX_DEFAULT_CATEGORIES",
180            ),
181        );
182        root.insert(
183            "no_degrade".into(),
184            key(
185                "boolean",
186                serde_json::json!(cfg.no_degrade),
187                "Disable all automatic read-mode degradation. Override via LCTX_NO_DEGRADE=1",
188            ),
189        );
190        root.insert(
191            "profile".into(),
192            key(
193                "string",
194                serde_json::json!(cfg.profile.as_deref().unwrap_or("")),
195                "Persistent profile name. Checked after LEAN_CTX_PROFILE env var. Set via: lean-ctx config set profile passthrough",
196            ),
197        );
198        root.insert(
199            "rules_scope".into(),
200            key_enum(
201                &["both", "global", "project"],
202                "both",
203                "Where agent rule files are installed. Override via LEAN_CTX_RULES_SCOPE",
204            ),
205        );
206        root.insert(
207            "extra_ignore_patterns".into(),
208            key(
209                "string[]",
210                serde_json::json!(cfg.extra_ignore_patterns),
211                "Extra glob patterns to ignore in graph/overview/preload",
212            ),
213        );
214        root.insert(
215            "terse_agent".into(),
216            key_enum_with_env(
217                &["off", "lite", "full", "ultra"],
218                "off",
219                "Controls agent output verbosity via instructions injection",
220                "LEAN_CTX_TERSE_AGENT",
221            ),
222        );
223        root.insert(
224            "compression_level".into(),
225            key_enum_with_env(
226                &["off", "lite", "standard", "max"],
227                "off",
228                "Unified compression level for all output",
229                "LEAN_CTX_COMPRESSION",
230            ),
231        );
232        root.insert(
233            "allow_paths".into(),
234            key_with_env(
235                "string[]",
236                serde_json::json!(cfg.allow_paths),
237                "Additional paths allowed by PathJail (absolute)",
238                "LEAN_CTX_ALLOW_PATH",
239            ),
240        );
241        root.insert(
242            "extra_roots".into(),
243            key_with_env(
244                "string[]",
245                serde_json::json!(cfg.extra_roots),
246                "Extra project roots for multi-root workspaces (auto-added to PathJail allow-list)",
247                "LEAN_CTX_EXTRA_ROOTS",
248            ),
249        );
250        root.insert(
251            "content_defined_chunking".into(),
252            key(
253                "bool",
254                serde_json::json!(false),
255                "Enable Rabin-Karp chunking for cache-optimal output ordering",
256            ),
257        );
258        root.insert(
259            "minimal_overhead".into(),
260            key_with_env(
261                "bool",
262                serde_json::json!(false),
263                "Skip session/knowledge/gotcha blocks in MCP instructions",
264                "LEAN_CTX_MINIMAL",
265            ),
266        );
267        root.insert(
268            "shell_hook_disabled".into(),
269            key_with_env(
270                "bool",
271                serde_json::json!(false),
272                "Disable shell hook injection",
273                "LEAN_CTX_NO_HOOK",
274            ),
275        );
276        root.insert(
277            "shell_activation".into(),
278            key_enum_with_env(
279                &["always", "agents-only", "off"],
280                "always",
281                "Controls when the shell hook auto-activates aliases",
282                "LEAN_CTX_SHELL_ACTIVATION",
283            ),
284        );
285        root.insert(
286            "update_check_disabled".into(),
287            key_with_env(
288                "bool",
289                serde_json::json!(false),
290                "Disable the daily version check",
291                "LEAN_CTX_NO_UPDATE_CHECK",
292            ),
293        );
294        root.insert(
295            "bm25_max_cache_mb".into(),
296            key_with_env(
297                "u64",
298                serde_json::json!(cfg.bm25_max_cache_mb),
299                "Maximum BM25 cache file size in MB",
300                "LEAN_CTX_BM25_MAX_CACHE_MB",
301            ),
302        );
303        root.insert(
304            "graph_index_max_files".into(),
305            key(
306                "u64",
307                serde_json::json!(cfg.graph_index_max_files),
308                "Maximum files in graph index. 0 = unlimited (default). Set >0 to cap for constrained systems",
309            ),
310        );
311        root.insert(
312            "memory_profile".into(),
313            key_enum_with_env(
314                &["low", "balanced", "performance"],
315                "balanced",
316                "Controls RAM vs feature trade-off",
317                "LEAN_CTX_MEMORY_PROFILE",
318            ),
319        );
320        root.insert(
321            "memory_cleanup".into(),
322            key_enum_with_env(
323                &["aggressive", "shared"],
324                "aggressive",
325                "Controls how aggressively memory is freed when idle",
326                "LEAN_CTX_MEMORY_CLEANUP",
327            ),
328        );
329        root.insert(
330            "savings_footer".into(),
331            key_enum_with_env(
332                &["auto", "always", "never"],
333                "never",
334                "Controls visibility of token savings footers: never (default, suppress everywhere), always, auto (context-dependent)",
335                "LEAN_CTX_SAVINGS_FOOTER",
336            ),
337        );
338        root.insert(
339            "max_ram_percent".into(),
340            key_with_env(
341                "u8",
342                serde_json::json!(cfg.max_ram_percent),
343                "Maximum percentage of system RAM that lean-ctx may use (1-50, default 5)",
344                "LEAN_CTX_MAX_RAM_PERCENT",
345            ),
346        );
347        root.insert(
348            "max_disk_mb".into(),
349            key_with_env(
350                "u64",
351                serde_json::json!(cfg.max_disk_mb),
352                "Simplified disk budget in MB (0 = disabled). Distributes: archive ~25%, BM25 ~10%",
353                "LEAN_CTX_MAX_DISK_MB",
354            ),
355        );
356        root.insert(
357            "max_staleness_days".into(),
358            key_with_env(
359                "u32",
360                serde_json::json!(cfg.max_staleness_days),
361                "Auto-purge data older than N days (0 = disabled). Flows into archive.max_age_hours",
362                "LEAN_CTX_MAX_STALENESS_DAYS",
363            ),
364        );
365        root.insert(
366            "project_root".into(),
367            key_with_env(
368                "string?",
369                serde_json::json!(null),
370                "Explicit project root directory. Prevents accidental home-directory scans",
371                "LEAN_CTX_PROJECT_ROOT",
372            ),
373        );
374        root.insert(
375            "proxy_enabled".into(),
376            key(
377                "bool?",
378                serde_json::json!(null),
379                "Enable/disable the proxy layer. null = auto-detect, true = force on, false = force off",
380            ),
381        );
382        root.insert(
383            "proxy_port".into(),
384            key(
385                "u16?",
386                serde_json::json!(null),
387                "Custom proxy port (default: 4444). Useful for multi-user systems. Env: LEAN_CTX_PROXY_PORT",
388            ),
389        );
390        root.insert(
391            "proxy_timeout_ms".into(),
392            key(
393                "u64?",
394                serde_json::json!(null),
395                "Proxy reachability timeout in ms (default: 200). Override via LEAN_CTX_PROXY_TIMEOUT_MS",
396            ),
397        );
398        root.insert(
399            "response_verbosity".into(),
400            key_enum_with_env(
401                &["normal", "compact", "minimal"],
402                "normal",
403                "Controls how verbose tool responses are",
404                "LEAN_CTX_RESPONSE_VERBOSITY",
405            ),
406        );
407        root.insert(
408            "allow_auto_reroot".into(),
409            key_with_env(
410                "bool",
411                serde_json::json!(false),
412                "Allow automatic project-root re-rooting when absolute paths outside the jail are seen",
413                "LEAN_CTX_ALLOW_REROOT",
414            ),
415        );
416        root.insert(
417            "sandbox_level".into(),
418            key_with_env(
419                "u8",
420                serde_json::json!(0),
421                "Sandbox strictness level (0=default, 1=strict, 2=paranoid)",
422                "LEAN_CTX_SANDBOX_LEVEL",
423            ),
424        );
425        root.insert(
426            "reference_results".into(),
427            key_with_env(
428                "bool",
429                serde_json::json!(false),
430                "Store large tool outputs as references instead of inline content",
431                "LEAN_CTX_REFERENCE_RESULTS",
432            ),
433        );
434        root.insert(
435            "agent_token_budget".into(),
436            key(
437                "usize",
438                serde_json::json!(0),
439                "Default per-agent token budget. 0 = unlimited",
440            ),
441        );
442        root.insert(
443            "shell_allowlist".into(),
444            key_with_env(
445                "array",
446                serde_json::json!([]),
447                "Optional shell command allowlist. When non-empty, only listed binaries are permitted",
448                "LEAN_CTX_SHELL_ALLOWLIST",
449            ),
450        );
451
452        sections.insert(
453            "root".into(),
454            SectionSchema {
455                description: "Top-level configuration keys".into(),
456                keys: root,
457            },
458        );
459
460        sections.insert(
461            "ide_paths".into(),
462            SectionSchema {
463                description: "Per-IDE allowed paths. Keys are agent names (cursor, codex, opencode, antigravity, etc.), values are arrays of paths to index for that agent".into(),
464                keys: BTreeMap::new(),
465            },
466        );
467
468        let mut lsp_keys = BTreeMap::new();
469        lsp_keys.insert(
470            "rust".into(),
471            key(
472                "string?",
473                serde_json::json!(null),
474                "Custom path to rust-analyzer binary",
475            ),
476        );
477        lsp_keys.insert(
478            "typescript".into(),
479            key(
480                "string?",
481                serde_json::json!(null),
482                "Custom path to typescript-language-server binary",
483            ),
484        );
485        lsp_keys.insert(
486            "python".into(),
487            key(
488                "string?",
489                serde_json::json!(null),
490                "Custom path to pylsp binary",
491            ),
492        );
493        lsp_keys.insert(
494            "go".into(),
495            key(
496                "string?",
497                serde_json::json!(null),
498                "Custom path to gopls binary",
499            ),
500        );
501        sections.insert(
502            "lsp".into(),
503            SectionSchema {
504                description: "LSP server binary overrides. Map language name to custom binary path"
505                    .into(),
506                keys: lsp_keys,
507            },
508        );
509
510        let mut archive = BTreeMap::new();
511        archive.insert(
512            "enabled".into(),
513            key(
514                "bool",
515                serde_json::json!(cfg.archive.enabled),
516                "Enable zero-loss compression archive",
517            ),
518        );
519        archive.insert(
520            "threshold_chars".into(),
521            key(
522                "usize",
523                serde_json::json!(cfg.archive.threshold_chars),
524                "Minimum output size (chars) to trigger archiving",
525            ),
526        );
527        archive.insert(
528            "max_age_hours".into(),
529            key(
530                "u64",
531                serde_json::json!(cfg.archive.max_age_hours),
532                "Maximum age of archived entries before cleanup",
533            ),
534        );
535        archive.insert(
536            "max_disk_mb".into(),
537            key(
538                "u64",
539                serde_json::json!(cfg.archive.max_disk_mb),
540                "Maximum total disk usage for the archive",
541            ),
542        );
543        sections.insert("archive".into(), SectionSchema {
544            description: "Settings for the zero-loss compression archive (large tool outputs saved to disk)".into(),
545            keys: archive,
546        });
547
548        let mut autonomy = BTreeMap::new();
549        autonomy.insert(
550            "enabled".into(),
551            key(
552                "bool",
553                serde_json::json!(cfg.autonomy.enabled),
554                "Enable autonomous background behaviors",
555            ),
556        );
557        autonomy.insert(
558            "auto_preload".into(),
559            key(
560                "bool",
561                serde_json::json!(cfg.autonomy.auto_preload),
562                "Auto-preload related files on first read",
563            ),
564        );
565        autonomy.insert(
566            "auto_dedup".into(),
567            key(
568                "bool",
569                serde_json::json!(cfg.autonomy.auto_dedup),
570                "Auto-deduplicate repeated reads",
571            ),
572        );
573        autonomy.insert(
574            "auto_related".into(),
575            key(
576                "bool",
577                serde_json::json!(cfg.autonomy.auto_related),
578                "Auto-load graph-related files",
579            ),
580        );
581        autonomy.insert(
582            "auto_consolidate".into(),
583            key(
584                "bool",
585                serde_json::json!(cfg.autonomy.auto_consolidate),
586                "Auto-consolidate knowledge periodically",
587            ),
588        );
589        autonomy.insert(
590            "silent_preload".into(),
591            key(
592                "bool",
593                serde_json::json!(cfg.autonomy.silent_preload),
594                "Suppress preload notifications in output",
595            ),
596        );
597        autonomy.insert(
598            "dedup_threshold".into(),
599            key(
600                "usize",
601                serde_json::json!(cfg.autonomy.dedup_threshold),
602                "Number of repeated reads before dedup triggers",
603            ),
604        );
605        autonomy.insert(
606            "consolidate_every_calls".into(),
607            key(
608                "u32",
609                serde_json::json!(cfg.autonomy.consolidate_every_calls),
610                "Consolidate knowledge every N tool calls",
611            ),
612        );
613        autonomy.insert(
614            "consolidate_cooldown_secs".into(),
615            key(
616                "u64",
617                serde_json::json!(cfg.autonomy.consolidate_cooldown_secs),
618                "Minimum seconds between consolidation runs",
619            ),
620        );
621        sections.insert(
622            "autonomy".into(),
623            SectionSchema {
624                description:
625                    "Controls autonomous background behaviors (preload, dedup, consolidation)"
626                        .into(),
627                keys: autonomy,
628            },
629        );
630
631        let mut providers = BTreeMap::new();
632        providers.insert(
633            "enabled".into(),
634            key(
635                "bool",
636                serde_json::json!(cfg.providers.enabled),
637                "Master switch for the provider subsystem (GitHub, GitLab, etc.)",
638            ),
639        );
640        providers.insert(
641            "auto_index".into(),
642            key(
643                "bool",
644                serde_json::json!(cfg.providers.auto_index),
645                "Auto-ingest provider results into BM25/embedding indexes",
646            ),
647        );
648        providers.insert(
649            "cache_ttl_secs".into(),
650            key(
651                "u64",
652                serde_json::json!(cfg.providers.cache_ttl_secs),
653                "Default cache TTL for provider results (seconds)",
654            ),
655        );
656        providers.insert(
657            "github.enabled".into(),
658            key(
659                "bool",
660                serde_json::json!(cfg.providers.github.enabled),
661                "Enable/disable GitHub provider",
662            ),
663        );
664        providers.insert(
665            "github.api_url".into(),
666            key(
667                "string",
668                serde_json::json!(cfg.providers.github.api_url),
669                "GitHub API base URL (for GitHub Enterprise)",
670            ),
671        );
672        providers.insert(
673            "gitlab.enabled".into(),
674            key(
675                "bool",
676                serde_json::json!(cfg.providers.gitlab.enabled),
677                "Enable/disable GitLab provider",
678            ),
679        );
680        providers.insert(
681            "gitlab.api_url".into(),
682            key(
683                "string",
684                serde_json::json!(cfg.providers.gitlab.api_url),
685                "GitLab API base URL (for self-hosted instances)",
686            ),
687        );
688        providers.insert(
689            "mcp_bridges.<name>.url".into(),
690            key(
691                "string",
692                serde_json::json!(null),
693                "HTTP/SSE URL for a remote MCP server",
694            ),
695        );
696        providers.insert(
697            "mcp_bridges.<name>.command".into(),
698            key(
699                "string",
700                serde_json::json!(null),
701                "Command to spawn a local MCP server (stdio transport)",
702            ),
703        );
704        providers.insert(
705            "mcp_bridges.<name>.args".into(),
706            key(
707                "array",
708                serde_json::json!([]),
709                "Arguments for the MCP server command",
710            ),
711        );
712        providers.insert(
713            "mcp_bridges.<name>.auth_env".into(),
714            key(
715                "string",
716                serde_json::json!(null),
717                "Environment variable name containing auth token for MCP server",
718            ),
719        );
720        sections.insert(
721            "providers".into(),
722            SectionSchema {
723                description:
724                    "External context providers (GitHub, GitLab, Jira, MCP bridges, etc.). Set tokens via env vars (GITHUB_TOKEN, GITLAB_TOKEN). MCP bridges connect external MCP servers as context sources."
725                        .into(),
726                keys: providers,
727            },
728        );
729
730        let mut loop_det = BTreeMap::new();
731        loop_det.insert(
732            "normal_threshold".into(),
733            key(
734                "u32",
735                serde_json::json!(cfg.loop_detection.normal_threshold),
736                "Repetitions before reducing output",
737            ),
738        );
739        loop_det.insert(
740            "reduced_threshold".into(),
741            key(
742                "u32",
743                serde_json::json!(cfg.loop_detection.reduced_threshold),
744                "Repetitions before further reducing output",
745            ),
746        );
747        loop_det.insert(
748            "blocked_threshold".into(),
749            key(
750                "u32",
751                serde_json::json!(cfg.loop_detection.blocked_threshold),
752                "Repetitions before blocking. 0 = disabled",
753            ),
754        );
755        loop_det.insert(
756            "window_secs".into(),
757            key(
758                "u64",
759                serde_json::json!(cfg.loop_detection.window_secs),
760                "Time window in seconds for loop detection",
761            ),
762        );
763        loop_det.insert(
764            "search_group_limit".into(),
765            key(
766                "u32",
767                serde_json::json!(cfg.loop_detection.search_group_limit),
768                "Maximum unique searches within a loop window",
769            ),
770        );
771        loop_det.insert(
772            "tool_total_limits".into(),
773            key(
774                "table",
775                serde_json::json!({"ctx_read": 100, "ctx_search": 80, "ctx_shell": 50, "ctx_semantic_search": 60}),
776                "Per-tool total call limits within a session. Keys are tool names, values are max calls",
777            ),
778        );
779        sections.insert(
780            "loop_detection".into(),
781            SectionSchema {
782                description: "Loop detection settings for preventing repeated identical tool calls"
783                    .into(),
784                keys: loop_det,
785            },
786        );
787
788        let mut updates = BTreeMap::new();
789        updates.insert(
790            "auto_update".into(),
791            key(
792                "bool",
793                serde_json::json!(cfg.updates.auto_update),
794                "Enable automatic updates (requires explicit opt-in)",
795            ),
796        );
797        updates.insert(
798            "check_interval_hours".into(),
799            key(
800                "u64",
801                serde_json::json!(cfg.updates.check_interval_hours),
802                "How often to check for updates (hours)",
803            ),
804        );
805        updates.insert(
806            "notify_only".into(),
807            key(
808                "bool",
809                serde_json::json!(cfg.updates.notify_only),
810                "Only notify about updates, don't install automatically",
811            ),
812        );
813        sections.insert(
814            "updates".into(),
815            SectionSchema {
816                description: "Automatic update configuration".into(),
817                keys: updates,
818            },
819        );
820
821        let mut boundary = BTreeMap::new();
822        boundary.insert(
823            "cross_project_search".into(),
824            key(
825                "bool",
826                serde_json::json!(cfg.boundary_policy.cross_project_search),
827                "Allow searching across project boundaries",
828            ),
829        );
830        boundary.insert(
831            "cross_project_import".into(),
832            key(
833                "bool",
834                serde_json::json!(cfg.boundary_policy.cross_project_import),
835                "Allow importing knowledge from other projects",
836            ),
837        );
838        boundary.insert(
839            "audit_cross_access".into(),
840            key(
841                "bool",
842                serde_json::json!(cfg.boundary_policy.audit_cross_access),
843                "Log audit events when cross-project access occurs",
844            ),
845        );
846        boundary.insert(
847            "universal_gotchas_enabled".into(),
848            key(
849                "bool",
850                serde_json::json!(cfg.boundary_policy.universal_gotchas_enabled),
851                "Load universal (cross-project) gotchas",
852            ),
853        );
854        sections.insert(
855            "boundary_policy".into(),
856            SectionSchema {
857                description: "Cross-project boundary and access control policies".into(),
858                keys: boundary,
859            },
860        );
861
862        let mut secret_det = BTreeMap::new();
863        secret_det.insert(
864            "enabled".into(),
865            key(
866                "bool",
867                serde_json::json!(cfg.secret_detection.enabled),
868                "Enable secret/credential detection in tool outputs",
869            ),
870        );
871        secret_det.insert(
872            "redact".into(),
873            key(
874                "bool",
875                serde_json::json!(cfg.secret_detection.redact),
876                "Redact detected secrets from output",
877            ),
878        );
879        secret_det.insert(
880            "custom_patterns".into(),
881            key(
882                "array",
883                serde_json::json!(cfg.secret_detection.custom_patterns),
884                "Additional regex patterns to detect as secrets",
885            ),
886        );
887        sections.insert(
888            "secret_detection".into(),
889            SectionSchema {
890                description: "Secret/credential detection and redaction settings".into(),
891                keys: secret_det,
892            },
893        );
894
895        let mut cloud = BTreeMap::new();
896        cloud.insert(
897            "contribute_enabled".into(),
898            key(
899                "bool",
900                serde_json::json!(cfg.cloud.contribute_enabled),
901                "Enable contributing anonymized stats to lean-ctx cloud",
902            ),
903        );
904        sections.insert(
905            "cloud".into(),
906            SectionSchema {
907                description: "Cloud feature settings".into(),
908                keys: cloud,
909            },
910        );
911
912        let mut proxy = BTreeMap::new();
913        proxy.insert(
914            "anthropic_upstream".into(),
915            key(
916                "string?",
917                serde_json::json!(cfg.proxy.anthropic_upstream),
918                "Custom upstream URL for Anthropic API proxy",
919            ),
920        );
921        proxy.insert(
922            "openai_upstream".into(),
923            key(
924                "string?",
925                serde_json::json!(cfg.proxy.openai_upstream),
926                "Custom upstream URL for OpenAI API proxy",
927            ),
928        );
929        proxy.insert(
930            "gemini_upstream".into(),
931            key(
932                "string?",
933                serde_json::json!(cfg.proxy.gemini_upstream),
934                "Custom upstream URL for Gemini API proxy",
935            ),
936        );
937        sections.insert(
938            "proxy".into(),
939            SectionSchema {
940                description: "Proxy upstream configuration for API routing".into(),
941                keys: proxy,
942            },
943        );
944
945        let mem = &cfg.memory;
946        let mut mem_knowledge = BTreeMap::new();
947        mem_knowledge.insert(
948            "max_facts".into(),
949            key(
950                "usize",
951                serde_json::json!(mem.knowledge.max_facts),
952                "Maximum number of knowledge facts stored per project",
953            ),
954        );
955        mem_knowledge.insert(
956            "max_patterns".into(),
957            key(
958                "usize",
959                serde_json::json!(mem.knowledge.max_patterns),
960                "Maximum number of patterns stored",
961            ),
962        );
963        mem_knowledge.insert(
964            "max_history".into(),
965            key(
966                "usize",
967                serde_json::json!(mem.knowledge.max_history),
968                "Maximum history entries retained",
969            ),
970        );
971        mem_knowledge.insert(
972            "contradiction_threshold".into(),
973            key(
974                "f32",
975                clean_f32(mem.knowledge.contradiction_threshold),
976                "Confidence threshold for contradiction detection",
977            ),
978        );
979        mem_knowledge.insert(
980            "recall_facts_limit".into(),
981            key(
982                "usize",
983                serde_json::json!(mem.knowledge.recall_facts_limit),
984                "Maximum facts returned per recall query",
985            ),
986        );
987        mem_knowledge.insert(
988            "rooms_limit".into(),
989            key(
990                "usize",
991                serde_json::json!(mem.knowledge.rooms_limit),
992                "Maximum number of rooms returned",
993            ),
994        );
995        mem_knowledge.insert(
996            "timeline_limit".into(),
997            key(
998                "usize",
999                serde_json::json!(mem.knowledge.timeline_limit),
1000                "Maximum number of timeline entries returned",
1001            ),
1002        );
1003        mem_knowledge.insert(
1004            "relations_limit".into(),
1005            key(
1006                "usize",
1007                serde_json::json!(mem.knowledge.relations_limit),
1008                "Maximum number of relations returned",
1009            ),
1010        );
1011        sections.insert(
1012            "memory.knowledge".into(),
1013            SectionSchema {
1014                description: "Knowledge memory budgets (facts, patterns, gotchas)".into(),
1015                keys: mem_knowledge,
1016            },
1017        );
1018
1019        let mut mem_episodic = BTreeMap::new();
1020        mem_episodic.insert(
1021            "max_episodes".into(),
1022            key(
1023                "usize",
1024                serde_json::json!(mem.episodic.max_episodes),
1025                "Maximum number of episodes retained",
1026            ),
1027        );
1028        mem_episodic.insert(
1029            "max_actions_per_episode".into(),
1030            key(
1031                "usize",
1032                serde_json::json!(mem.episodic.max_actions_per_episode),
1033                "Maximum actions tracked per episode",
1034            ),
1035        );
1036        mem_episodic.insert(
1037            "summary_max_chars".into(),
1038            key(
1039                "usize",
1040                serde_json::json!(mem.episodic.summary_max_chars),
1041                "Maximum characters in episode summary",
1042            ),
1043        );
1044        sections.insert(
1045            "memory.episodic".into(),
1046            SectionSchema {
1047                description: "Episodic memory budgets (session episodes)".into(),
1048                keys: mem_episodic,
1049            },
1050        );
1051
1052        let mut mem_procedural = BTreeMap::new();
1053        mem_procedural.insert(
1054            "max_procedures".into(),
1055            key(
1056                "usize",
1057                serde_json::json!(mem.procedural.max_procedures),
1058                "Maximum number of learned procedures stored",
1059            ),
1060        );
1061        mem_procedural.insert(
1062            "min_repetitions".into(),
1063            key(
1064                "usize",
1065                serde_json::json!(mem.procedural.min_repetitions),
1066                "Minimum repetitions before a pattern is stored",
1067            ),
1068        );
1069        mem_procedural.insert(
1070            "min_sequence_len".into(),
1071            key(
1072                "usize",
1073                serde_json::json!(mem.procedural.min_sequence_len),
1074                "Minimum sequence length for procedure detection",
1075            ),
1076        );
1077        mem_procedural.insert(
1078            "max_window_size".into(),
1079            key(
1080                "usize",
1081                serde_json::json!(mem.procedural.max_window_size),
1082                "Maximum window size for pattern analysis",
1083            ),
1084        );
1085        sections.insert(
1086            "memory.procedural".into(),
1087            SectionSchema {
1088                description: "Procedural memory budgets (learned patterns)".into(),
1089                keys: mem_procedural,
1090            },
1091        );
1092
1093        let mut mem_lifecycle = BTreeMap::new();
1094        mem_lifecycle.insert(
1095            "decay_rate".into(),
1096            key(
1097                "f32",
1098                clean_f32(mem.lifecycle.decay_rate),
1099                "Rate at which knowledge confidence decays over time",
1100            ),
1101        );
1102        mem_lifecycle.insert(
1103            "low_confidence_threshold".into(),
1104            key(
1105                "f32",
1106                clean_f32(mem.lifecycle.low_confidence_threshold),
1107                "Threshold below which facts are considered low-confidence",
1108            ),
1109        );
1110        mem_lifecycle.insert(
1111            "stale_days".into(),
1112            key(
1113                "i64",
1114                serde_json::json!(mem.lifecycle.stale_days),
1115                "Days after which unused facts are considered stale",
1116            ),
1117        );
1118        mem_lifecycle.insert(
1119            "similarity_threshold".into(),
1120            key(
1121                "f32",
1122                clean_f32(mem.lifecycle.similarity_threshold),
1123                "Similarity threshold for deduplication",
1124            ),
1125        );
1126        sections.insert(
1127            "memory.lifecycle".into(),
1128            SectionSchema {
1129                description: "Knowledge lifecycle policy (decay, staleness, dedup)".into(),
1130                keys: mem_lifecycle,
1131            },
1132        );
1133
1134        let mut mem_gotcha = BTreeMap::new();
1135        mem_gotcha.insert(
1136            "max_gotchas_per_project".into(),
1137            key(
1138                "usize",
1139                serde_json::json!(mem.gotcha.max_gotchas_per_project),
1140                "Maximum gotchas stored per project",
1141            ),
1142        );
1143        mem_gotcha.insert(
1144            "retrieval_budget_per_room".into(),
1145            key(
1146                "usize",
1147                serde_json::json!(mem.gotcha.retrieval_budget_per_room),
1148                "Maximum gotchas retrieved per room per query",
1149            ),
1150        );
1151        mem_gotcha.insert(
1152            "default_decay_rate".into(),
1153            key(
1154                "f32",
1155                clean_f32(mem.gotcha.default_decay_rate),
1156                "Default decay rate for gotcha importance",
1157            ),
1158        );
1159        sections.insert(
1160            "memory.gotcha".into(),
1161            SectionSchema {
1162                description: "Gotcha memory settings (project-specific warnings and pitfalls)"
1163                    .into(),
1164                keys: mem_gotcha,
1165            },
1166        );
1167
1168        let mut mem_embeddings = BTreeMap::new();
1169        mem_embeddings.insert(
1170            "max_facts".into(),
1171            key(
1172                "usize",
1173                serde_json::json!(mem.embeddings.max_facts),
1174                "Maximum number of embedding facts stored",
1175            ),
1176        );
1177        sections.insert(
1178            "memory.embeddings".into(),
1179            SectionSchema {
1180                description: "Embeddings memory settings for semantic search".into(),
1181                keys: mem_embeddings,
1182            },
1183        );
1184
1185        let mut aliases = BTreeMap::new();
1186        aliases.insert(
1187            "command".into(),
1188            key(
1189                "string",
1190                serde_json::json!(""),
1191                "The command pattern to match (e.g. 'deploy')",
1192            ),
1193        );
1194        aliases.insert(
1195            "alias".into(),
1196            key(
1197                "string",
1198                serde_json::json!(""),
1199                "The alias definition to execute",
1200            ),
1201        );
1202        sections.insert("custom_aliases".into(), SectionSchema {
1203            description: "Custom command aliases (array of {command, alias} entries). Note: field names are 'command' and 'alias' (not 'name')".into(),
1204            keys: aliases,
1205        });
1206
1207        if let Some(root_section) = sections.get_mut("root") {
1208            root_section.keys.insert(
1209                "custom_aliases".into(),
1210                key(
1211                    "array",
1212                    serde_json::json!([]),
1213                    "Custom command aliases (array of {command, alias} entries)",
1214                ),
1215            );
1216        }
1217
1218        ConfigSchema {
1219            version: 1,
1220            sections,
1221        }
1222    }
1223
1224    /// All known TOML keys (dot-separated) for validation.
1225    pub fn known_keys(&self) -> Vec<String> {
1226        let mut keys = Vec::new();
1227        for (section, schema) in &self.sections {
1228            if section == "root" {
1229                for key_name in schema.keys.keys() {
1230                    keys.push(key_name.clone());
1231                }
1232            } else {
1233                if schema.keys.is_empty() {
1234                    keys.push(section.clone());
1235                }
1236                for key_name in schema.keys.keys() {
1237                    keys.push(format!("{section}.{key_name}"));
1238                }
1239            }
1240        }
1241        keys
1242    }
1243}