Skip to main content

lean_ctx/core/config/
mod.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use std::path::PathBuf;
4use std::sync::Mutex;
5
6use super::memory_policy::MemoryPolicy;
7
8mod defaults_allowlist;
9mod enums;
10mod memory;
11mod proxy;
12pub mod schema;
13mod sections;
14mod serde_defaults;
15pub mod setter;
16mod shell_activation;
17pub use sections::*;
18#[cfg(test)]
19mod tests;
20
21pub(crate) use defaults_allowlist::{cloud_infra_commands, default_shell_allowlist};
22pub use enums::{
23    CompressionLevel, OutputDensity, PermissionInheritance, ResponseVerbosity, RulesInjection,
24    RulesScope, TeeMode, TerseAgent,
25};
26pub use memory::{MemoryCleanup, MemoryGuardConfig, MemoryProfile, SavingsFooter};
27pub use proxy::{
28    is_local_proxy_url, normalize_url, normalize_url_opt, HistoryMode, ProxyConfig, ProxyProvider,
29};
30pub use shell_activation::ShellActivation;
31
32/// Default BM25 cache cap from config (also used by `bm25_index` heuristics).
33pub fn default_bm25_max_cache_mb() -> u64 {
34    serde_defaults::default_bm25_max_cache_mb()
35}
36
37/// Effective on-disk ceiling (MB) for the persisted BM25 index when nothing is
38/// explicitly configured (no `bm25_max_cache_mb`, no `max_disk_mb` budget).
39///
40/// Deliberately decoupled from the RAM `MemoryProfile` (64/128/512 MB): this is
41/// a *disk* file, and tying it to the profile silently refused persistence on
42/// large repos under Low/Balanced, forcing a cold rebuild on every call (the
43/// perpetual "index warming" of issue #249). 512 MB compressed covers
44/// essentially every real repo; RAM pressure is governed separately by the
45/// eviction orchestrator (which measures real heap).
46pub const DEFAULT_BM25_PERSIST_MB: u64 = 512;
47
48// Compile-time regression guard (#249): the default disk ceiling must stay well
49// above the old RAM-profile caps (64/128 MB) that starved large repos.
50const _: () = assert!(DEFAULT_BM25_PERSIST_MB >= 512);
51
52/// Global lean-ctx configuration loaded from `config.toml`, merged with project-local overrides.
53#[derive(Debug, Clone, Serialize, Deserialize)]
54#[serde(default)]
55pub struct Config {
56    pub ultra_compact: bool,
57    #[serde(default, deserialize_with = "serde_defaults::deserialize_tee_mode")]
58    pub tee_mode: TeeMode,
59    #[serde(default)]
60    pub output_density: OutputDensity,
61    pub checkpoint_interval: u32,
62    pub excluded_commands: Vec<String>,
63    pub passthrough_urls: Vec<String>,
64    pub custom_aliases: Vec<AliasEntry>,
65    /// Output formats that are already compact/token-oriented and must be
66    /// preserved verbatim instead of being recompressed (#342). Matched against
67    /// the *output shape* (not the command name), so any tool emitting the
68    /// format is covered without enumerating commands in `excluded_commands`.
69    /// Default: `["toon"]`. Set to `[]` to disable and always recompress.
70    #[serde(default = "serde_defaults::default_preserve_compact_formats")]
71    pub preserve_compact_formats: Vec<String>,
72    /// Commands taking longer than this threshold (ms) are recorded in the slow log.
73    /// Set to 0 to disable slow logging.
74    pub slow_command_threshold_ms: u64,
75    #[serde(default = "serde_defaults::default_theme")]
76    pub theme: String,
77    #[serde(default)]
78    pub cloud: CloudConfig,
79    #[serde(default)]
80    pub gain: GainConfig,
81    #[serde(default)]
82    pub autonomy: AutonomyConfig,
83    #[serde(default)]
84    pub providers: ProvidersConfig,
85    #[serde(default)]
86    pub proxy: ProxyConfig,
87    /// Whether the API proxy is enabled. Tri-state:
88    /// - None: undecided (fresh install, will prompt on interactive setup)
89    /// - Some(true): user opted in, proxy managed by lean-ctx
90    /// - Some(false): user opted out, never touch proxy or endpoints
91    #[serde(default)]
92    pub proxy_enabled: Option<bool>,
93    #[serde(default)]
94    pub proxy_port: Option<u16>,
95    /// Proxy reachability timeout in milliseconds. Default: 200.
96    /// Override via LEAN_CTX_PROXY_TIMEOUT_MS env var.
97    #[serde(default)]
98    pub proxy_timeout_ms: Option<u64>,
99    #[serde(default = "serde_defaults::default_buddy_enabled")]
100    pub buddy_enabled: bool,
101    #[serde(default = "serde_defaults::default_true")]
102    pub enable_wakeup_ctx: bool,
103    #[serde(default)]
104    pub redirect_exclude: Vec<String>,
105    /// Tools to exclude from the MCP tool list returned by list_tools.
106    /// Accepts exact tool names (e.g. `["ctx_graph", "ctx_agent"]`).
107    /// Empty by default — all tools listed, no behaviour change.
108    #[serde(default)]
109    pub disabled_tools: Vec<String>,
110    /// Tool categories to activate by default for dynamic-tool-capable clients.
111    /// Values: "core" (always on), "arch", "debug", "memory", "metrics", "session".
112    /// Example: `default_tool_categories = ["core", "arch", "memory"]`
113    /// Override via LCTX_DEFAULT_CATEGORIES env var (comma-separated).
114    /// Empty = lean-ctx default (core + session).
115    #[serde(default)]
116    pub default_tool_categories: Vec<String>,
117    /// Disable all automatic read-mode degradation (auto_degrade + context_gate pressure).
118    /// When true, lean-ctx never downgrades requested read modes regardless of pressure.
119    /// Override via LCTX_NO_DEGRADE=1 env var.
120    #[serde(default)]
121    pub no_degrade: bool,
122    /// Persistent profile name. Checked after LEAN_CTX_PROFILE env var.
123    /// Set via `lean-ctx config set profile passthrough` or editing config.toml.
124    #[serde(default)]
125    pub profile: Option<String>,
126    /// Tool visibility profile: "minimal" (6), "standard" (22), or "power" (all).
127    /// Override via LEAN_CTX_TOOL_PROFILE env var.
128    /// Existing installs default to "power" (backward compat).
129    #[serde(default)]
130    pub tool_profile: Option<String>,
131    /// Explicit list of enabled tool names (overrides tool_profile when non-empty).
132    /// Example: `tools_enabled = ["ctx_read", "ctx_shell", "ctx_search"]`
133    #[serde(default)]
134    pub tools_enabled: Vec<String>,
135    /// Active context persona (`persona-spec-v1`). Selects the domain bundle —
136    /// tool surface, read-mode/compressor/chunker defaults, intent taxonomy,
137    /// sensitivity floor. Override via `LEAN_CTX_PERSONA`. Defaults to `coding`.
138    #[serde(default)]
139    pub persona: Option<String>,
140    #[serde(default)]
141    pub loop_detection: LoopDetectionConfig,
142    /// Controls where lean-ctx installs agent rule files.
143    /// Values: "both" (default), "global" (home-dir only), "project" (repo-local only).
144    /// Override via LEAN_CTX_RULES_SCOPE env var.
145    #[serde(default)]
146    pub rules_scope: Option<String>,
147    /// Controls how rules are injected for shared-instruction-file agents.
148    /// Values: "shared" (default, marker block in CLAUDE.md/AGENTS.md/GEMINI.md),
149    /// "dedicated" (never touch those files; use each agent's config-driven
150    /// auto-load: SessionStart hook / instructions[] / context.fileName, #343), or
151    /// "off" (write no rules file at all — for hosts that supply their own
152    /// tool-steering workflow or phase-isolated/non-caching harnesses, #361).
153    /// Override via LEAN_CTX_RULES_INJECTION env var.
154    #[serde(default)]
155    pub rules_injection: Option<String>,
156    /// Mirror the host IDE's tool-permission rules onto lean-ctx's own MCP tools.
157    /// Values: "off" (default) or "on". When "on", lean-ctx reads the active
158    /// IDE's permission config (v1: OpenCode) and applies the equivalent
159    /// deny/ask/allow decision to the matching lean-ctx tool — so `ctx_shell`
160    /// honors your `bash`/`rm *` rules instead of bypassing them.
161    /// Override via LEAN_CTX_PERMISSION_INHERITANCE env var.
162    #[serde(default)]
163    pub permission_inheritance: Option<String>,
164    /// Extra glob patterns to ignore in graph/overview/preload (repo-local).
165    /// Example: `["externals/**", "target/**", "temp/**"]`
166    #[serde(default)]
167    pub extra_ignore_patterns: Vec<String>,
168    /// Controls agent output verbosity via instructions injection.
169    /// Values: "off" (default), "lite", "full", "ultra".
170    /// Override via LEAN_CTX_TERSE_AGENT env var.
171    #[serde(default)]
172    pub terse_agent: TerseAgent,
173    /// Unified compression level (replaces separate terse_agent + output_density).
174    /// Values: "off" (default), "lite", "standard", "max".
175    /// Override via LEAN_CTX_COMPRESSION env var.
176    #[serde(default)]
177    pub compression_level: CompressionLevel,
178    /// Archive configuration for zero-loss compression.
179    #[serde(default)]
180    pub archive: ArchiveConfig,
181    /// Memory policy (knowledge/episodic/procedural/lifecycle budgets & thresholds).
182    #[serde(default)]
183    pub memory: MemoryPolicy,
184    /// Additional paths allowed by PathJail (absolute).
185    /// Useful for multi-project workspaces where the jail root is a parent directory.
186    /// Override via LEAN_CTX_ALLOW_PATH env var (path-list separator).
187    #[serde(default)]
188    pub allow_paths: Vec<String>,
189    /// Allow jailed tool access to home-level IDE config dirs (~/.cursor,
190    /// ~/.claude, …). Default false: those dirs expose other projects'
191    /// sessions, MCP configs and credentials. `~/.lean-ctx` (own data dir)
192    /// is always allowed. Override via LEAN_CTX_ALLOW_IDE_DIRS=1.
193    #[serde(default)]
194    pub allow_ide_config_dirs: bool,
195    /// Extra project roots for multi-root workspaces.
196    /// Tools like ctx_tree and ctx_search can scan across all roots in a single call.
197    /// These paths are automatically added to PathJail's allow-list.
198    /// Override via LEAN_CTX_EXTRA_ROOTS env var (path-list separator).
199    #[serde(default)]
200    pub extra_roots: Vec<String>,
201    /// Enable content-defined chunking (Rabin-Karp) for cache-optimal output ordering.
202    /// Stable chunks are emitted first to maximize prompt cache hits.
203    #[serde(default)]
204    pub content_defined_chunking: bool,
205    /// Skip session/knowledge/gotcha blocks in MCP instructions to minimize token overhead.
206    /// Override via LEAN_CTX_MINIMAL env var.
207    #[serde(default)]
208    pub minimal_overhead: bool,
209    /// Opt-in: substitute long identifiers with short α-codes (+ a `§MAP` table)
210    /// in `aggressive` reads for projects with >50 source files. Off by default —
211    /// the abbreviated form is confusing for editing/refactoring, where the agent
212    /// needs the real package and symbol names. Enable for max exploration savings.
213    #[serde(default)]
214    pub symbol_map_auto: bool,
215    /// Team server URL for opt-in savings roll-up.
216    /// Set via `lean-ctx config set team_url https://...` or `[team] url` in config.toml.
217    /// Override via LEAN_CTX_TEAM_URL env var.
218    #[serde(default)]
219    pub team_url: Option<String>,
220    /// Bearer token for the team server (Authorization header on savings push /
221    /// pull). Set via `lean-ctx config set team_token <tok>` or `team_token` in
222    /// config.toml. Override via the LEAN_CTX_TEAM_TOKEN env var.
223    #[serde(default)]
224    pub team_token: Option<String>,
225    /// Opt-in: when true, the running daemon periodically pushes this machine's
226    /// signed savings batch to `team_url` so the team roll-up fills itself (no
227    /// manual `savings push` per dev). Off by default; requires `team_url` +
228    /// `team_token`. Set via `lean-ctx config set team_auto_push true`.
229    #[serde(default)]
230    pub team_auto_push: bool,
231    /// Enable human-readable activity journal (~/.lean-ctx/journal.md).
232    #[serde(default)]
233    pub journal_enabled: bool,
234    /// Opt-in: auto-persist interesting findings as knowledge facts.
235    #[serde(default)]
236    pub auto_capture: bool,
237    /// Hybrid search weights (BM25/dense/candidates).
238    #[serde(default)]
239    pub search: crate::core::hybrid_search::HybridConfig,
240    /// Code-graph settings, including traversal (co-access) edges (#289).
241    #[serde(default)]
242    pub graph: GraphConfig,
243    /// Skillify miner settings (#290): codify recurring patterns into rules.
244    #[serde(default)]
245    pub skillify: SkillifyConfig,
246    /// AI session-summary settings (#292): periodic, semantically-recallable summaries.
247    #[serde(default)]
248    pub summaries: SummariesConfig,
249    /// Optional LLM enhancement (query expansion, contradiction explanation).
250    #[serde(default)]
251    pub llm: crate::core::llm_enhance::LlmConfig,
252    /// Semantic-embedding engine settings (which local ONNX model to use).
253    #[serde(default)]
254    pub embedding: EmbeddingConfig,
255    /// Disable shell hook injection (the _lc() function that wraps CLI commands).
256    /// Override via LEAN_CTX_NO_HOOK env var.
257    #[serde(default)]
258    pub shell_hook_disabled: bool,
259    /// Shadow mode: transparently intercepts native tool calls (Read/Grep/Shell)
260    /// via hooks, strengthens MCP instructions to MUST-level, and activates
261    /// immediate bypass hints on first native tool use. Enables "transparent
262    /// replacement" so agents use ctx_* without explicit opt-in.
263    #[serde(default)]
264    pub shadow_mode: bool,
265    /// Controls when the shell hook auto-activates aliases.
266    /// - `always`: (Default) Aliases active in every interactive shell.
267    /// - `agents-only`: Aliases only active when an AI agent env var is detected.
268    /// - `off`: Aliases never auto-activate (user must call `lean-ctx-on` manually).
269    ///
270    /// Override via `LEAN_CTX_SHELL_ACTIVATION` env var.
271    #[serde(default)]
272    pub shell_activation: ShellActivation,
273    /// Disable the daily version check against leanctx.com/version.txt.
274    /// Override via LEAN_CTX_NO_UPDATE_CHECK env var.
275    #[serde(default)]
276    pub update_check_disabled: bool,
277    #[serde(default)]
278    pub updates: UpdatesConfig,
279    /// Maximum BM25 cache file size in MB. Indexes exceeding this are quarantined on load
280    /// and refused on save. Override via LEAN_CTX_BM25_MAX_CACHE_MB env var.
281    #[serde(default = "serde_defaults::default_bm25_max_cache_mb")]
282    pub bm25_max_cache_mb: u64,
283    /// Maximum number of files scanned by the lightweight JSON graph index.
284    /// 0 = unlimited (default). Set >0 to cap for constrained systems.
285    #[serde(default = "serde_defaults::default_graph_index_max_files")]
286    pub graph_index_max_files: u64,
287    /// Controls RAM vs feature trade-off. Values: "low", "balanced" (default), "performance".
288    /// Override via LEAN_CTX_MEMORY_PROFILE env var.
289    #[serde(default)]
290    pub memory_profile: MemoryProfile,
291    /// Controls how aggressively memory is freed when idle.
292    /// Values: "aggressive" (default, 5 min TTL), "shared" (30 min TTL for multi-IDE use).
293    /// Override via LEAN_CTX_MEMORY_CLEANUP env var.
294    #[serde(default)]
295    pub memory_cleanup: MemoryCleanup,
296    /// Maximum percentage of system RAM that lean-ctx may use (default: 5).
297    /// Override via LEAN_CTX_MAX_RAM_PERCENT env var.
298    #[serde(default = "serde_defaults::default_max_ram_percent")]
299    pub max_ram_percent: u8,
300    /// Simplified disk budget (MB). When set and detail values are at defaults,
301    /// distributes proportionally: archive=25%, bm25=10%, remainder for stores.
302    /// 0 = disabled (use individual settings). Override via LEAN_CTX_MAX_DISK_MB.
303    #[serde(default)]
304    pub max_disk_mb: u64,
305    /// Auto-purge data older than this many days. 0 = disabled.
306    /// Flows into archive.max_age_hours and lifecycle idle TTL.
307    #[serde(default)]
308    pub max_staleness_days: u32,
309    /// Controls visibility of token savings footers in tool output.
310    /// Values: "always" (default, show on every response), "never", "auto" (legacy compatibility).
311    /// Override via LEAN_CTX_SAVINGS_FOOTER or LEAN_CTX_SHOW_SAVINGS=1|0 env var.
312    #[serde(default)]
313    pub savings_footer: SavingsFooter,
314    /// Explicit project root override. When set, lean-ctx uses this instead of auto-detection.
315    /// This prevents accidental home-directory scans when running from $HOME.
316    /// Override via LEAN_CTX_PROJECT_ROOT env var.
317    #[serde(default)]
318    pub project_root: Option<String>,
319    /// LSP server overrides. Map language name to custom binary path.
320    /// Example: `[lsp]\nrust = "/opt/rust-analyzer"\npython = "~/.venvs/main/bin/pylsp"`
321    #[serde(default)]
322    pub lsp: std::collections::HashMap<String, String>,
323    /// Per-IDE allowed paths. Restricts which directories lean-ctx will scan/index for each IDE.
324    /// Example: `[ide_paths]\ncursor = ["/home/user/projects/app1"]\ncodex = ["/home/user/codex"]`
325    /// When set, only these paths are indexed for the matching agent. Global `allow_paths` still applies.
326    #[serde(default)]
327    pub ide_paths: HashMap<String, Vec<String>>,
328    /// Custom model context window overrides.
329    /// Example: `[model_context_windows]\n"my-custom-model" = 500000`
330    #[serde(default)]
331    pub model_context_windows: HashMap<String, usize>,
332    /// Controls how much detail tool responses include.
333    ///
334    /// - `full` (default): complete compressed output
335    /// - `headers_only`: metadata line only (path, mode, token count)
336    ///
337    /// Override via `LEAN_CTX_RESPONSE_VERBOSITY` env var.
338    #[serde(default)]
339    pub response_verbosity: ResponseVerbosity,
340    /// Bypass hint mode. When agents use native Read/Grep instead of lean-ctx tools,
341    /// a hint is appended to the next tool response.
342    /// Values: "on" (default), "off", "aggressive" (hint on every call, no cooldown).
343    /// Override via LEAN_CTX_BYPASS_HINTS env var.
344    #[serde(default)]
345    pub bypass_hints: Option<String>,
346    /// Cache policy for ctx_read. Controls behavior on cache hits.
347    /// Values: "aggressive" (default, 13-tok stubs + compaction-aware reset),
348    /// "safe" (delivers map instead of stub), "off" (no caching, always disk read).
349    /// Override via LEAN_CTX_CACHE_POLICY env var.
350    #[serde(default)]
351    pub cache_policy: Option<String>,
352    /// Cross-project boundary policy.
353    /// Controls whether cross-project search/import is allowed and whether access is audited.
354    #[serde(default)]
355    pub boundary_policy: crate::core::memory_boundary::BoundaryPolicy,
356    #[serde(default)]
357    pub secret_detection: SecretDetectionConfig,
358    /// Per-item sensitivity model with a uniform policy floor (#212).
359    /// Disabled by default → fully no-op until `sensitivity.enabled = true`.
360    #[serde(default)]
361    pub sensitivity: crate::core::sensitivity::SensitivityConfig,
362    /// MCP Tool-Catalog Gateway (#210): aggregate + query-route downstream MCP
363    /// servers. Global-only (never merged from project-local config) and a full
364    /// no-op until `gateway.enabled = true`.
365    #[serde(default)]
366    pub gateway: crate::core::gateway::GatewayConfig,
367    /// Allow automatic project-root re-rooting when absolute paths outside the jail are seen.
368    /// When false (default), absolute paths outside the jail are rejected without re-rooting.
369    /// Override via LEAN_CTX_ALLOW_REROOT env var.
370    #[serde(default)]
371    pub allow_auto_reroot: bool,
372    /// Disable PathJail entirely by setting `path_jail = false` in config.toml.
373    /// Useful in container/Docker environments where the sandbox is the boundary.
374    /// (The former `LEAN_CTX_NO_JAIL=1` env override was removed in v3.7.3.)
375    #[serde(default)]
376    pub path_jail: Option<bool>,
377    /// Sandbox level for code execution (ctx_exec).
378    /// 0 = subprocess only (current), 1 = OS-level restriction (Seatbelt/Landlock).
379    /// Override via LEAN_CTX_SANDBOX_LEVEL env var.
380    #[serde(default)]
381    pub sandbox_level: u8,
382    /// When true, large tool outputs (>4000 chars) are stored as references
383    /// and a short URI is returned instead of the full content.
384    /// Override via LEAN_CTX_REFERENCE_RESULTS env var.
385    #[serde(default)]
386    pub reference_results: bool,
387    /// Default per-agent token budget. 0 means unlimited.
388    /// Override per-agent via ctx_session or programmatically.
389    #[serde(default)]
390    pub agent_token_budget: usize,
391    /// Optional shell command allowlist. When non-empty, only commands whose base binary
392    /// is in this list are permitted by ctx_shell. Empty = disable allowlist (allow all).
393    /// Default includes common dev tools. Set to `[]` to disable.
394    /// Override via LEAN_CTX_SHELL_ALLOWLIST env var (comma-separated).
395    #[serde(default = "default_shell_allowlist")]
396    pub shell_allowlist: Vec<String>,
397
398    /// Extra commands MERGED on top of the effective `shell_allowlist` without replacing
399    /// the defaults. Setting `shell_allowlist` replaces the whole built-in list (a common
400    /// footgun); entries here are purely additive, which is what `lean-ctx allow <cmd>`
401    /// writes. Only applied in restricted mode (when the base allowlist is non-empty).
402    #[serde(default)]
403    pub shell_allowlist_extra: Vec<String>,
404
405    /// When true, block command substitution ($(), backticks) and process substitution
406    /// (<(), >()) in shell arguments. When false (default), only warn via tracing.
407    /// Default false preserves backward compatibility — set true for maximum security.
408    #[serde(default)]
409    pub shell_strict_mode: bool,
410    /// Setup behavior: controls what gets injected during setup and updates.
411    #[serde(default)]
412    pub setup: SetupConfig,
413}
414
415impl Default for Config {
416    fn default() -> Self {
417        Self {
418            ultra_compact: false,
419            tee_mode: TeeMode::default(),
420            output_density: OutputDensity::default(),
421            checkpoint_interval: 15,
422            excluded_commands: Vec::new(),
423            passthrough_urls: Vec::new(),
424            custom_aliases: Vec::new(),
425            preserve_compact_formats: serde_defaults::default_preserve_compact_formats(),
426            slow_command_threshold_ms: 5000,
427            theme: serde_defaults::default_theme(),
428            cloud: CloudConfig::default(),
429            gain: GainConfig::default(),
430            autonomy: AutonomyConfig::default(),
431            providers: ProvidersConfig::default(),
432            proxy: ProxyConfig::default(),
433            proxy_enabled: None,
434            proxy_port: None,
435            proxy_timeout_ms: None,
436            buddy_enabled: serde_defaults::default_buddy_enabled(),
437            enable_wakeup_ctx: true,
438            redirect_exclude: Vec::new(),
439            disabled_tools: Vec::new(),
440            default_tool_categories: Vec::new(),
441            no_degrade: false,
442            profile: None,
443            tool_profile: None,
444            tools_enabled: Vec::new(),
445            persona: None,
446            loop_detection: LoopDetectionConfig::default(),
447            rules_scope: None,
448            rules_injection: None,
449            permission_inheritance: None,
450            extra_ignore_patterns: Vec::new(),
451            terse_agent: TerseAgent::default(),
452            compression_level: CompressionLevel::default(),
453            archive: ArchiveConfig::default(),
454            memory: MemoryPolicy::default(),
455            allow_paths: Vec::new(),
456            allow_ide_config_dirs: false,
457            extra_roots: Vec::new(),
458            content_defined_chunking: false,
459            minimal_overhead: true,
460            symbol_map_auto: false,
461            team_url: None,
462            team_token: None,
463            team_auto_push: false,
464            journal_enabled: true,
465            auto_capture: true,
466            search: crate::core::hybrid_search::HybridConfig::default(),
467            graph: GraphConfig::default(),
468            skillify: SkillifyConfig::default(),
469            summaries: SummariesConfig::default(),
470            llm: crate::core::llm_enhance::LlmConfig::default(),
471            embedding: EmbeddingConfig::default(),
472            shell_hook_disabled: false,
473            shadow_mode: false,
474            shell_activation: ShellActivation::default(),
475            update_check_disabled: false,
476            updates: UpdatesConfig::default(),
477            graph_index_max_files: serde_defaults::default_graph_index_max_files(),
478            bm25_max_cache_mb: serde_defaults::default_bm25_max_cache_mb(),
479            memory_profile: MemoryProfile::default(),
480            memory_cleanup: MemoryCleanup::default(),
481            max_ram_percent: serde_defaults::default_max_ram_percent(),
482            max_disk_mb: 0,
483            max_staleness_days: 0,
484            savings_footer: SavingsFooter::default(),
485            project_root: None,
486            lsp: std::collections::HashMap::new(),
487            ide_paths: HashMap::new(),
488            model_context_windows: HashMap::new(),
489            response_verbosity: ResponseVerbosity::default(),
490            bypass_hints: None,
491            cache_policy: None,
492            boundary_policy: crate::core::memory_boundary::BoundaryPolicy::default(),
493            secret_detection: SecretDetectionConfig::default(),
494            sensitivity: crate::core::sensitivity::SensitivityConfig::default(),
495            gateway: crate::core::gateway::GatewayConfig::default(),
496            allow_auto_reroot: false,
497            path_jail: None,
498            sandbox_level: 0,
499            reference_results: false,
500            agent_token_budget: 0,
501            shell_allowlist: default_shell_allowlist(),
502            shell_allowlist_extra: Vec::new(),
503            shell_strict_mode: false,
504            setup: SetupConfig::default(),
505        }
506    }
507}
508
509/// Holds the most recent global `config.toml` parse error, if the file currently
510/// fails to parse. When that happens `Config::load()` silently falls back to the
511/// built-in defaults and only logs to stderr — which is invisible over an MCP/stdio
512/// transport. Recording it here lets callers (e.g. the shell-allowlist diagnostic
513/// and `lean-ctx doctor`) surface "you're on defaults because your config is broken".
514static LAST_PARSE_ERROR: Mutex<Option<String>> = Mutex::new(None);
515
516/// Returns the most recent global config parse error, or `None` if the current
517/// `config.toml` parsed successfully (or no config file exists).
518#[must_use]
519pub fn last_config_parse_error() -> Option<String> {
520    LAST_PARSE_ERROR.lock().ok().and_then(|g| g.clone())
521}
522
523fn record_parse_error(err: Option<String>) {
524    if let Ok(mut guard) = LAST_PARSE_ERROR.lock() {
525        *guard = err;
526    }
527}
528
529impl Config {
530    /// Returns the effective rules scope, preferring env var over config file.
531    pub fn rules_scope_effective(&self) -> RulesScope {
532        let raw = std::env::var("LEAN_CTX_RULES_SCOPE")
533            .ok()
534            .or_else(|| self.rules_scope.clone())
535            .unwrap_or_default();
536        match raw.trim().to_lowercase().as_str() {
537            "global" => RulesScope::Global,
538            "project" => RulesScope::Project,
539            _ => RulesScope::Both,
540        }
541    }
542
543    /// Returns the effective rules injection mode, preferring env var over config.
544    /// Default is `Shared` (zero-config discovery via a CLAUDE.md/AGENTS.md block).
545    pub fn rules_injection_effective(&self) -> RulesInjection {
546        let raw = std::env::var("LEAN_CTX_RULES_INJECTION")
547            .ok()
548            .or_else(|| self.rules_injection.clone())
549            .unwrap_or_default();
550        match raw.trim().to_lowercase().as_str() {
551            "dedicated" => RulesInjection::Dedicated,
552            "off" | "none" | "disabled" => RulesInjection::Off,
553            _ => RulesInjection::Shared,
554        }
555    }
556
557    /// Returns the effective permission-inheritance mode, preferring the
558    /// `LEAN_CTX_PERMISSION_INHERITANCE` env var over config. Default is `Off`.
559    /// Accepts `on`/`true`/`1` as enabled.
560    #[must_use]
561    pub fn permission_inheritance_effective(&self) -> PermissionInheritance {
562        let raw = std::env::var("LEAN_CTX_PERMISSION_INHERITANCE")
563            .ok()
564            .or_else(|| self.permission_inheritance.clone())
565            .unwrap_or_default();
566        match raw.trim().to_lowercase().as_str() {
567            "on" | "true" | "1" | "inherit" => PermissionInheritance::On,
568            _ => PermissionInheritance::Off,
569        }
570    }
571
572    /// True when lean-ctx should inject its rules via each agent's dedicated,
573    /// non-polluting auto-load path *and* global rules are in scope.
574    ///
575    /// Gates the Claude/Codex `SessionStart` `additionalContext` summary: it
576    /// stands in for the (now-skipped) shared CLAUDE.md/AGENTS.md block, so it
577    /// only fires when injection is `Dedicated` and the scope isn't project-only.
578    #[must_use]
579    pub fn dedicated_session_context_active(&self) -> bool {
580        self.rules_injection_effective() == RulesInjection::Dedicated
581            && self.rules_scope_effective() != RulesScope::Project
582    }
583
584    fn parse_disabled_tools_env(val: &str) -> Vec<String> {
585        val.split(',')
586            .map(|s| s.trim().to_string())
587            .filter(|s| !s.is_empty())
588            .collect()
589    }
590
591    /// Returns the effective disabled tools list, preferring env var over config file.
592    pub fn disabled_tools_effective(&self) -> Vec<String> {
593        if let Ok(val) = std::env::var("LEAN_CTX_DISABLED_TOOLS") {
594            Self::parse_disabled_tools_env(&val)
595        } else {
596            self.disabled_tools.clone()
597        }
598    }
599
600    /// Returns `true` if minimal overhead is enabled via env var or config.
601    pub fn minimal_overhead_effective(&self) -> bool {
602        std::env::var("LEAN_CTX_MINIMAL").is_ok() || self.minimal_overhead
603    }
604
605    /// Returns `true` if minimal overhead should be enabled for this MCP client.
606    ///
607    /// This is a superset of `minimal_overhead_effective()`:
608    /// - `LEAN_CTX_OVERHEAD_MODE=minimal` forces minimal overhead
609    /// - `LEAN_CTX_OVERHEAD_MODE=full` disables client/model heuristics (still honors LEAN_CTX_MINIMAL / config)
610    /// - In auto mode (default), certain low-context clients/models are treated as minimal to prevent
611    ///   large metadata blocks from destabilizing smaller context windows (e.g. Hermes + MiniMax).
612    pub fn minimal_overhead_effective_for_client(&self, client_name: &str) -> bool {
613        if let Ok(raw) = std::env::var("LEAN_CTX_OVERHEAD_MODE") {
614            match raw.trim().to_lowercase().as_str() {
615                "minimal" => return true,
616                "full" => return self.minimal_overhead_effective(),
617                _ => {}
618            }
619        }
620
621        if self.minimal_overhead_effective() {
622            return true;
623        }
624
625        let client_lower = client_name.trim().to_lowercase();
626        if !client_lower.is_empty() {
627            if let Ok(list) = std::env::var("LEAN_CTX_MINIMAL_CLIENTS") {
628                for needle in list.split(',').map(|s| s.trim().to_lowercase()) {
629                    if !needle.is_empty() && client_lower.contains(&needle) {
630                        return true;
631                    }
632                }
633            } else if client_lower.contains("hermes") || client_lower.contains("minimax") {
634                return true;
635            }
636        }
637
638        let model = std::env::var("LEAN_CTX_MODEL")
639            .or_else(|_| std::env::var("LCTX_MODEL"))
640            .unwrap_or_default();
641        let model = model.trim().to_lowercase();
642        if !model.is_empty() {
643            let m = model.replace(['_', ' '], "-");
644            if m.contains("minimax")
645                || m.contains("mini-max")
646                || m.contains("m2.7")
647                || m.contains("m2-7")
648            {
649                return true;
650            }
651        }
652
653        false
654    }
655
656    /// Returns `true` if shell hook injection is disabled via env var or config.
657    pub fn shell_hook_disabled_effective(&self) -> bool {
658        std::env::var("LEAN_CTX_NO_HOOK").is_ok() || self.shell_hook_disabled
659    }
660
661    /// Returns the effective shell activation mode (env var > config > default).
662    pub fn shell_activation_effective(&self) -> ShellActivation {
663        ShellActivation::effective(self)
664    }
665
666    /// Returns `true` if the daily update check is disabled via env var or config.
667    pub fn update_check_disabled_effective(&self) -> bool {
668        std::env::var("LEAN_CTX_NO_UPDATE_CHECK").is_ok() || self.update_check_disabled
669    }
670
671    pub fn memory_policy_effective(&self) -> Result<MemoryPolicy, String> {
672        let mut policy = self.memory.clone();
673        policy.apply_env_overrides();
674
675        // Scale memory limits proportionally when max_disk_mb is set
676        // and individual limits are still at their defaults.
677        let budget = self.max_disk_mb_effective();
678        if budget > 0 {
679            let scale_factor = (budget as f64 / 500.0).clamp(0.5, 10.0);
680            let default_policy = MemoryPolicy::default();
681            if policy.knowledge.max_facts == default_policy.knowledge.max_facts {
682                policy.knowledge.max_facts = (200.0 * scale_factor) as usize;
683            }
684            if policy.knowledge.max_patterns == default_policy.knowledge.max_patterns {
685                policy.knowledge.max_patterns = (50.0 * scale_factor) as usize;
686            }
687            if policy.episodic.max_episodes == default_policy.episodic.max_episodes {
688                policy.episodic.max_episodes = (500.0 * scale_factor) as usize;
689            }
690            if policy.procedural.max_procedures == default_policy.procedural.max_procedures {
691                policy.procedural.max_procedures = (100.0 * scale_factor) as usize;
692            }
693        }
694
695        policy.validate()?;
696        Ok(policy)
697    }
698
699    /// Returns the effective set of default tool categories.
700    /// Priority: LCTX_DEFAULT_CATEGORIES env var > config.toml > hardcoded default.
701    pub fn default_tool_categories_effective(&self) -> Vec<String> {
702        if let Ok(val) = std::env::var("LCTX_DEFAULT_CATEGORIES") {
703            return val
704                .split(',')
705                .map(|s| s.trim().to_lowercase())
706                .filter(|s| !s.is_empty())
707                .collect();
708        }
709        if !self.default_tool_categories.is_empty() {
710            return self
711                .default_tool_categories
712                .iter()
713                .map(|s| s.to_lowercase())
714                .collect();
715        }
716        vec!["core".to_string(), "session".to_string()]
717    }
718
719    /// Returns the effective tool profile.
720    /// Priority: LEAN_CTX_TOOL_PROFILE env > config tool_profile > config
721    /// tools_enabled > active persona's tool surface > power.
722    ///
723    /// Explicit settings win (backward compatible); when none are set, the
724    /// active persona supplies the tool surface (the `coding` default resolves
725    /// to `power`, so existing installs are unaffected).
726    pub fn tool_profile_effective(&self) -> super::tool_profiles::ToolProfile {
727        super::persona::Persona::resolve(self).effective_tool_profile(self)
728    }
729
730    /// Returns `true` if all automatic read-mode degradation is disabled.
731    /// Checks LCTX_NO_DEGRADE env var first, then config.toml field.
732    pub fn no_degrade_effective(&self) -> bool {
733        if let Ok(val) = std::env::var("LCTX_NO_DEGRADE") {
734            return val == "1" || val.eq_ignore_ascii_case("true");
735        }
736        self.no_degrade
737    }
738
739    /// Effective max_disk_mb from env or config.
740    pub fn max_disk_mb_effective(&self) -> u64 {
741        std::env::var("LEAN_CTX_MAX_DISK_MB")
742            .ok()
743            .and_then(|v| v.parse().ok())
744            .unwrap_or(self.max_disk_mb)
745    }
746
747    /// Effective max_staleness_days from env or config.
748    pub fn max_staleness_days_effective(&self) -> u32 {
749        std::env::var("LEAN_CTX_MAX_STALENESS_DAYS")
750            .ok()
751            .and_then(|v| v.parse().ok())
752            .unwrap_or(self.max_staleness_days)
753    }
754
755    /// Archive max_disk_mb derived from simplified max_disk_mb if the detail
756    /// value is still at its default. Explicit overrides take priority.
757    pub fn archive_max_disk_mb_effective(&self) -> u64 {
758        let budget = self.max_disk_mb_effective();
759        if budget > 0 && self.archive.max_disk_mb == ArchiveConfig::default().max_disk_mb {
760            budget * 25 / 100
761        } else {
762            self.archive.max_disk_mb
763        }
764    }
765
766    /// Archive max_age_hours derived from max_staleness_days if the detail
767    /// value is still at its default. Explicit overrides take priority.
768    pub fn archive_max_age_hours_effective(&self) -> u64 {
769        let staleness = self.max_staleness_days_effective();
770        if staleness > 0 && self.archive.max_age_hours == ArchiveConfig::default().max_age_hours {
771            staleness as u64 * 24
772        } else {
773            self.archive.max_age_hours
774        }
775    }
776
777    /// Effective on-disk ceiling (MB) for the persisted BM25 index. Single source
778    /// of truth for `save`/`load`, `cache prune`, and the doctor health check.
779    ///
780    /// Priority: explicit `bm25_max_cache_mb` › `max_disk_mb` budget (10%) ›
781    /// generous default ([`DEFAULT_BM25_PERSIST_MB`]). The default is decoupled
782    /// from the RAM profile so large repos persist instead of rebuilding forever
783    /// (issue #249).
784    pub fn bm25_max_cache_mb_effective(&self) -> u64 {
785        // Explicit per-key override always wins.
786        if self.bm25_max_cache_mb != serde_defaults::default_bm25_max_cache_mb() {
787            return self.bm25_max_cache_mb;
788        }
789        // Otherwise derive from an explicit overall disk budget when present …
790        let budget = self.max_disk_mb_effective();
791        if budget > 0 {
792            return budget * 10 / 100;
793        }
794        // … else fall back to the generous, profile-independent disk default.
795        DEFAULT_BM25_PERSIST_MB
796    }
797}
798
799impl Config {
800    /// Returns the path to the global config file (`$XDG_CONFIG_HOME/lean-ctx/config.toml`).
801    ///
802    /// Resolves via [`crate::core::paths::config_dir`] so config lives in the
803    /// RO-safe config category. Behavior-neutral today: `config_dir()` equals the
804    /// legacy data dir for existing/single-dir installs (GH #408 / GL #602).
805    pub fn path() -> Option<PathBuf> {
806        crate::core::paths::config_dir()
807            .ok()
808            .map(|d| d.join("config.toml"))
809    }
810
811    /// Returns the path to the project-local config override file.
812    pub fn local_path(project_root: &str) -> PathBuf {
813        PathBuf::from(project_root).join(".lean-ctx.toml")
814    }
815
816    fn find_project_root() -> Option<String> {
817        static ROOT_CACHE: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
818        ROOT_CACHE
819            .get_or_init(Self::find_project_root_inner)
820            .clone()
821    }
822
823    fn find_project_root_inner() -> Option<String> {
824        if let Ok(env_root) = std::env::var("LEAN_CTX_PROJECT_ROOT") {
825            if !env_root.is_empty() {
826                return Some(env_root);
827            }
828        }
829
830        let cwd = std::env::current_dir().ok();
831
832        if let Some(root) =
833            crate::core::session::SessionState::load_latest().and_then(|s| s.project_root)
834        {
835            let root_path = std::path::Path::new(&root);
836            let cwd_is_under_root = cwd.as_ref().is_some_and(|c| c.starts_with(root_path));
837            let has_marker = root_path.join(".git").exists()
838                || root_path.join("Cargo.toml").exists()
839                || root_path.join("package.json").exists()
840                || root_path.join("go.mod").exists()
841                || root_path.join("pyproject.toml").exists()
842                || root_path.join(".lean-ctx.toml").exists();
843
844            if cwd_is_under_root || has_marker {
845                return Some(root);
846            }
847        }
848
849        if let Some(ref cwd) = cwd {
850            let git_root = std::process::Command::new("git")
851                .args(["rev-parse", "--show-toplevel"])
852                .current_dir(cwd)
853                .stdout(std::process::Stdio::piped())
854                .stderr(std::process::Stdio::null())
855                .output()
856                .ok()
857                .and_then(|o| {
858                    if o.status.success() {
859                        String::from_utf8(o.stdout)
860                            .ok()
861                            .map(|s| s.trim().to_string())
862                    } else {
863                        None
864                    }
865                });
866            if let Some(root) = git_root {
867                return Some(root);
868            }
869            if !crate::core::pathutil::is_broad_or_unsafe_root(cwd) {
870                return Some(cwd.to_string_lossy().to_string());
871            }
872        }
873        None
874    }
875
876    /// Loads config from disk with caching, merging global + project-local overrides.
877    ///
878    /// The cache is keyed on a **content hash** of the global + project-local
879    /// files, not their mtime. mtime-only invalidation silently served a stale
880    /// `Config` whenever a content edit preserved the mtime (coarse filesystem
881    /// mtime resolution, `cp -p`, atomic save-then-rename, two edits within the
882    /// same second). A long-lived MCP server then kept the old value (e.g.
883    /// `path_jail`) while a fresh `lean-ctx doctor` process — with an empty
884    /// cache — saw the new one (#406). Config files are tiny, so reading +
885    /// hashing them on every load is negligible and guarantees liveness.
886    pub fn load() -> Self {
887        static CACHE: Mutex<Option<(Config, Option<String>, Option<String>)>> = Mutex::new(None);
888
889        let Some(path) = Self::path() else {
890            return Self::default();
891        };
892
893        let local_path = Self::find_project_root().map(|r| Self::local_path(&r));
894
895        // Read raw content up front so the cache key is a content hash.
896        let global_content = std::fs::read_to_string(&path).ok();
897        let local_content = local_path
898            .as_ref()
899            .and_then(|p| std::fs::read_to_string(p).ok());
900
901        let global_hash = global_content.as_deref().map(crate::core::hasher::hash_str);
902        let local_hash = local_content.as_deref().map(crate::core::hasher::hash_str);
903
904        if let Ok(guard) = CACHE.lock() {
905            if let Some((ref cfg, ref cached_global, ref cached_local)) = *guard {
906                if *cached_global == global_hash && *cached_local == local_hash {
907                    return cfg.clone();
908                }
909            }
910        }
911
912        let mut cfg: Config = if let Some(ref content) = global_content {
913            match toml::from_str(content) {
914                Ok(c) => {
915                    record_parse_error(None);
916                    c
917                }
918                Err(e) => {
919                    record_parse_error(Some(format!("{e}")));
920                    tracing::warn!("config parse error in {}: {e}", path.display());
921                    eprintln!(
922                        "\x1b[33m[lean-ctx] WARNING: config parse error in {}: {e}\n  \
923                         Using defaults. Run `lean-ctx doctor --fix` to repair.\x1b[0m",
924                        path.display()
925                    );
926                    Self::default()
927                }
928            }
929        } else {
930            record_parse_error(None);
931            Self::default()
932        };
933
934        if let Some(ref local) = local_content {
935            cfg.merge_local(local);
936        }
937
938        if let Ok(mut guard) = CACHE.lock() {
939            *guard = Some((cfg.clone(), global_hash, local_hash));
940        }
941
942        cfg
943    }
944
945    fn merge_local(&mut self, local_toml: &str) {
946        let local: Config = match toml::from_str(local_toml) {
947            Ok(c) => c,
948            Err(e) => {
949                tracing::warn!("local config parse error: {e}");
950                eprintln!(
951                    "\x1b[33m[lean-ctx] WARNING: local .lean-ctx.toml parse error: {e}\n  \
952                     Local overrides skipped.\x1b[0m"
953                );
954                return;
955            }
956        };
957        if local.ultra_compact {
958            self.ultra_compact = true;
959        }
960        if local.tee_mode != TeeMode::default() {
961            self.tee_mode = local.tee_mode;
962        }
963        if local.output_density != OutputDensity::default() {
964            self.output_density = local.output_density;
965        }
966        if local.checkpoint_interval != 15 {
967            self.checkpoint_interval = local.checkpoint_interval;
968        }
969        if !local.excluded_commands.is_empty() {
970            self.excluded_commands.extend(local.excluded_commands);
971        }
972        if !local.passthrough_urls.is_empty() {
973            self.passthrough_urls.extend(local.passthrough_urls);
974        }
975        if !local.custom_aliases.is_empty() {
976            self.custom_aliases.extend(local.custom_aliases);
977        }
978        // Additive merge with dedup: project-local config can add formats on top
979        // of the global default (`["toon"]`) without re-listing it.
980        for fmt in local.preserve_compact_formats {
981            if !self
982                .preserve_compact_formats
983                .iter()
984                .any(|f| f.eq_ignore_ascii_case(&fmt))
985            {
986                self.preserve_compact_formats.push(fmt);
987            }
988        }
989        if local.slow_command_threshold_ms != 5000 {
990            self.slow_command_threshold_ms = local.slow_command_threshold_ms;
991        }
992        if local.theme != "default" {
993            self.theme = local.theme;
994        }
995        if !local.buddy_enabled {
996            self.buddy_enabled = false;
997        }
998        if !local.enable_wakeup_ctx {
999            self.enable_wakeup_ctx = false;
1000        }
1001        if !local.redirect_exclude.is_empty() {
1002            self.redirect_exclude.extend(local.redirect_exclude);
1003        }
1004        if !local.disabled_tools.is_empty() {
1005            self.disabled_tools.extend(local.disabled_tools);
1006        }
1007        if !local.extra_ignore_patterns.is_empty() {
1008            self.extra_ignore_patterns
1009                .extend(local.extra_ignore_patterns);
1010        }
1011        if local.rules_scope.is_some() {
1012            self.rules_scope = local.rules_scope;
1013        }
1014        if local.rules_injection.is_some() {
1015            self.rules_injection = local.rules_injection;
1016        }
1017        if local.permission_inheritance.is_some() {
1018            self.permission_inheritance = local.permission_inheritance;
1019        }
1020        if local.proxy.anthropic_upstream.is_some() {
1021            self.proxy.anthropic_upstream = local.proxy.anthropic_upstream;
1022        }
1023        if local.proxy.openai_upstream.is_some() {
1024            self.proxy.openai_upstream = local.proxy.openai_upstream;
1025        }
1026        if local.proxy.gemini_upstream.is_some() {
1027            self.proxy.gemini_upstream = local.proxy.gemini_upstream;
1028        }
1029        if !local.autonomy.enabled {
1030            self.autonomy.enabled = false;
1031        }
1032        if !local.autonomy.auto_preload {
1033            self.autonomy.auto_preload = false;
1034        }
1035        if !local.autonomy.auto_dedup {
1036            self.autonomy.auto_dedup = false;
1037        }
1038        if !local.autonomy.auto_related {
1039            self.autonomy.auto_related = false;
1040        }
1041        if !local.autonomy.auto_consolidate {
1042            self.autonomy.auto_consolidate = false;
1043        }
1044        if local.autonomy.silent_preload {
1045            self.autonomy.silent_preload = true;
1046        }
1047        if !local.autonomy.silent_preload && self.autonomy.silent_preload {
1048            self.autonomy.silent_preload = false;
1049        }
1050        if local.autonomy.dedup_threshold != AutonomyConfig::default().dedup_threshold {
1051            self.autonomy.dedup_threshold = local.autonomy.dedup_threshold;
1052        }
1053        if local.autonomy.consolidate_every_calls
1054            != AutonomyConfig::default().consolidate_every_calls
1055        {
1056            self.autonomy.consolidate_every_calls = local.autonomy.consolidate_every_calls;
1057        }
1058        if local.autonomy.consolidate_cooldown_secs
1059            != AutonomyConfig::default().consolidate_cooldown_secs
1060        {
1061            self.autonomy.consolidate_cooldown_secs = local.autonomy.consolidate_cooldown_secs;
1062        }
1063        if !local.autonomy.cognition_loop_enabled {
1064            self.autonomy.cognition_loop_enabled = false;
1065        }
1066        if local.autonomy.cognition_loop_interval_secs
1067            != AutonomyConfig::default().cognition_loop_interval_secs
1068        {
1069            self.autonomy.cognition_loop_interval_secs =
1070                local.autonomy.cognition_loop_interval_secs;
1071        }
1072        if local.autonomy.cognition_loop_max_steps
1073            != AutonomyConfig::default().cognition_loop_max_steps
1074        {
1075            self.autonomy.cognition_loop_max_steps = local.autonomy.cognition_loop_max_steps;
1076        }
1077        if local_toml.contains("compression_level") {
1078            self.compression_level = local.compression_level;
1079        }
1080        if local_toml.contains("terse_agent") {
1081            self.terse_agent = local.terse_agent;
1082        }
1083        if !local.archive.enabled {
1084            self.archive.enabled = false;
1085        }
1086        if local.archive.threshold_chars != ArchiveConfig::default().threshold_chars {
1087            self.archive.threshold_chars = local.archive.threshold_chars;
1088        }
1089        if local.archive.max_age_hours != ArchiveConfig::default().max_age_hours {
1090            self.archive.max_age_hours = local.archive.max_age_hours;
1091        }
1092        if local.archive.max_disk_mb != ArchiveConfig::default().max_disk_mb {
1093            self.archive.max_disk_mb = local.archive.max_disk_mb;
1094        }
1095        if !local.archive.ephemeral {
1096            self.archive.ephemeral = false;
1097        }
1098        if local.archive.ephemeral_min_tokens != ArchiveConfig::default().ephemeral_min_tokens {
1099            self.archive.ephemeral_min_tokens = local.archive.ephemeral_min_tokens;
1100        }
1101        let mem_def = MemoryPolicy::default();
1102        if local.memory.knowledge.max_facts != mem_def.knowledge.max_facts {
1103            self.memory.knowledge.max_facts = local.memory.knowledge.max_facts;
1104        }
1105        if local.memory.knowledge.max_patterns != mem_def.knowledge.max_patterns {
1106            self.memory.knowledge.max_patterns = local.memory.knowledge.max_patterns;
1107        }
1108        if local.memory.knowledge.max_history != mem_def.knowledge.max_history {
1109            self.memory.knowledge.max_history = local.memory.knowledge.max_history;
1110        }
1111        if local.memory.knowledge.contradiction_threshold
1112            != mem_def.knowledge.contradiction_threshold
1113        {
1114            self.memory.knowledge.contradiction_threshold =
1115                local.memory.knowledge.contradiction_threshold;
1116        }
1117
1118        if local.memory.episodic.max_episodes != mem_def.episodic.max_episodes {
1119            self.memory.episodic.max_episodes = local.memory.episodic.max_episodes;
1120        }
1121        if local.memory.episodic.max_actions_per_episode != mem_def.episodic.max_actions_per_episode
1122        {
1123            self.memory.episodic.max_actions_per_episode =
1124                local.memory.episodic.max_actions_per_episode;
1125        }
1126        if local.memory.episodic.summary_max_chars != mem_def.episodic.summary_max_chars {
1127            self.memory.episodic.summary_max_chars = local.memory.episodic.summary_max_chars;
1128        }
1129
1130        if local.memory.procedural.min_repetitions != mem_def.procedural.min_repetitions {
1131            self.memory.procedural.min_repetitions = local.memory.procedural.min_repetitions;
1132        }
1133        if local.memory.procedural.min_sequence_len != mem_def.procedural.min_sequence_len {
1134            self.memory.procedural.min_sequence_len = local.memory.procedural.min_sequence_len;
1135        }
1136        if local.memory.procedural.max_procedures != mem_def.procedural.max_procedures {
1137            self.memory.procedural.max_procedures = local.memory.procedural.max_procedures;
1138        }
1139        if local.memory.procedural.max_window_size != mem_def.procedural.max_window_size {
1140            self.memory.procedural.max_window_size = local.memory.procedural.max_window_size;
1141        }
1142
1143        if local.memory.lifecycle.decay_rate != mem_def.lifecycle.decay_rate {
1144            self.memory.lifecycle.decay_rate = local.memory.lifecycle.decay_rate;
1145        }
1146        if local.memory.lifecycle.low_confidence_threshold
1147            != mem_def.lifecycle.low_confidence_threshold
1148        {
1149            self.memory.lifecycle.low_confidence_threshold =
1150                local.memory.lifecycle.low_confidence_threshold;
1151        }
1152        if local.memory.lifecycle.stale_days != mem_def.lifecycle.stale_days {
1153            self.memory.lifecycle.stale_days = local.memory.lifecycle.stale_days;
1154        }
1155        if local.memory.lifecycle.similarity_threshold != mem_def.lifecycle.similarity_threshold {
1156            self.memory.lifecycle.similarity_threshold =
1157                local.memory.lifecycle.similarity_threshold;
1158        }
1159
1160        if local.memory.embeddings.max_facts != mem_def.embeddings.max_facts {
1161            self.memory.embeddings.max_facts = local.memory.embeddings.max_facts;
1162        }
1163        if !local.allow_paths.is_empty() {
1164            self.allow_paths.extend(local.allow_paths);
1165        }
1166        if !local.extra_roots.is_empty() {
1167            self.extra_roots.extend(local.extra_roots);
1168        }
1169        if local.minimal_overhead {
1170            self.minimal_overhead = true;
1171        }
1172        if local.shell_hook_disabled {
1173            self.shell_hook_disabled = true;
1174        }
1175        if local.shell_activation != ShellActivation::default() {
1176            self.shell_activation = local.shell_activation.clone();
1177        }
1178        if local.bm25_max_cache_mb != default_bm25_max_cache_mb() {
1179            self.bm25_max_cache_mb = local.bm25_max_cache_mb;
1180        }
1181        if local.memory_profile != MemoryProfile::default() {
1182            self.memory_profile = local.memory_profile;
1183        }
1184        if local.memory_cleanup != MemoryCleanup::default() {
1185            self.memory_cleanup = local.memory_cleanup;
1186        }
1187        // Only override when the local file actually defines `shell_allowlist`.
1188        // The field carries `#[serde(default = "default_shell_allowlist")]`, so a
1189        // local `.lean-ctx.toml` that omits the key still deserializes to the full
1190        // 201-entry built-in list — an `is_empty()` guard would then silently clobber
1191        // a deliberately shorter global allowlist with the defaults. Comparing against
1192        // the default (the same pattern used for every other merged field) treats
1193        // "omitted" as "no override".
1194        if local.shell_allowlist != default_shell_allowlist() {
1195            self.shell_allowlist = local.shell_allowlist;
1196        }
1197        if !local.shell_allowlist_extra.is_empty() {
1198            self.shell_allowlist_extra
1199                .extend(local.shell_allowlist_extra);
1200        }
1201        if !local.default_tool_categories.is_empty() {
1202            self.default_tool_categories = local.default_tool_categories;
1203        }
1204        if local.tool_profile.is_some() {
1205            self.tool_profile = local.tool_profile;
1206        }
1207        if !local.tools_enabled.is_empty() {
1208            self.tools_enabled = local.tools_enabled;
1209        }
1210        if local.no_degrade {
1211            self.no_degrade = true;
1212        }
1213        if local.profile.is_some() {
1214            self.profile = local.profile;
1215        }
1216        if local.proxy_timeout_ms.is_some() {
1217            self.proxy_timeout_ms = local.proxy_timeout_ms;
1218        }
1219    }
1220
1221    /// Persists the current config to the global config file.
1222    ///
1223    /// Preserves user comments, formatting, and unknown keys, keeps the file
1224    /// minimal (defaults that were never set on disk stay implicit), and writes
1225    /// atomically with a `.bak` backup so customizations are always recoverable.
1226    pub fn save(&self) -> std::result::Result<(), super::error::LeanCtxError> {
1227        let path = Self::path().ok_or_else(|| {
1228            super::error::LeanCtxError::Config("cannot determine home directory".into())
1229        })?;
1230        if let Some(parent) = path.parent() {
1231            std::fs::create_dir_all(parent)?;
1232        }
1233        let content = toml::to_string_pretty(self)
1234            .map_err(|e| super::error::LeanCtxError::Config(e.to_string()))?;
1235        // Baseline = what loading an empty config yields. This honors serde's
1236        // field-level `#[serde(default)]` (which can diverge from the struct's
1237        // `Default` impl), so minimal mode skips exactly the keys that a fresh
1238        // load would produce — no spurious lines on save.
1239        let baseline = toml::from_str::<Self>("").unwrap_or_else(|_| Self::default());
1240        let defaults = toml::to_string_pretty(&baseline)
1241            .map_err(|e| super::error::LeanCtxError::Config(e.to_string()))?;
1242        crate::config_io::write_toml_preserving_minimal(&path, &content, &defaults)
1243            .map_err(super::error::LeanCtxError::Config)?;
1244        Ok(())
1245    }
1246
1247    /// Formats the current config as a human-readable string with file paths.
1248    pub fn show(&self) -> String {
1249        let global_path = Self::path().map_or_else(
1250            || "~/.lean-ctx/config.toml".to_string(),
1251            |p| p.to_string_lossy().to_string(),
1252        );
1253        let content = toml::to_string_pretty(self).unwrap_or_default();
1254        let mut out = format!("Global config: {global_path}\n\n{content}");
1255
1256        if let Some(root) = Self::find_project_root() {
1257            let local = Self::local_path(&root);
1258            if local.exists() {
1259                out.push_str(&format!("\n\nLocal config (merged): {}\n", local.display()));
1260            } else {
1261                out.push_str(&format!(
1262                    "\n\nLocal config: not found (create {} to override per-project)\n",
1263                    local.display()
1264                ));
1265            }
1266        }
1267        out
1268    }
1269}