use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use super::{CognitiveMode, Config, ConfigCacheSlot, default_shell_allowlist};
const CONFIG_PROFILE_ENV: &str = "LEAN_CTX_CONFIG_PROFILE";
pub(super) fn environment_config_profile() -> Option<String> {
std::env::var(CONFIG_PROFILE_ENV)
.ok()
.map(|name| name.trim().to_string())
.filter(|name| !name.is_empty())
}
pub(super) fn parse_config_with_profile(
raw: &str,
explicit_profile: Option<&str>,
) -> Result<Config, String> {
let migrated = Config::migrate_v3_compression_document(raw)?;
let raw = migrated.as_deref().unwrap_or(raw);
let mut value: toml::Value = toml::from_str(raw).map_err(|error| error.to_string())?;
let configured_profile = value.get("config_profile").and_then(toml::Value::as_str);
let selected = explicit_profile
.map(str::trim)
.filter(|name| !name.is_empty())
.or(configured_profile);
if let Some(name) = selected {
let profiles = value
.get("profiles")
.and_then(toml::Value::as_table)
.ok_or_else(|| format!("config profile '{name}' selected but [profiles] is missing"))?;
let mut overlay = profiles
.get(name)
.and_then(toml::Value::as_table)
.cloned()
.ok_or_else(|| format!("config profile '{name}' is not defined"))?;
if overlay.remove("profiles").is_some() || overlay.remove("config_profile").is_some() {
return Err(format!(
"config profile '{name}' cannot override reserved profile keys"
));
}
merge_toml_tables(
value
.as_table_mut()
.expect("a TOML document always has a root table"),
overlay,
);
}
value.try_into().map_err(|error| error.to_string())
}
fn merge_toml_tables(base: &mut toml::Table, overlay: toml::Table) {
for (key, overlay_value) in overlay {
match (base.get_mut(&key), overlay_value) {
(Some(toml::Value::Table(base_table)), toml::Value::Table(overlay_table)) => {
merge_toml_tables(base_table, overlay_table);
}
(_, replacement) => {
base.insert(key, replacement);
}
}
}
}
static LAST_PARSE_ERROR: Mutex<Option<String>> = Mutex::new(None);
#[must_use]
pub fn last_config_parse_error() -> Option<String> {
LAST_PARSE_ERROR.lock().ok().and_then(|g| g.clone())
}
fn record_parse_error(err: Option<String>) {
if let Ok(mut guard) = LAST_PARSE_ERROR.lock() {
*guard = err;
}
}
pub(crate) fn strip_sensitive_overrides(local: &mut Config) -> Vec<&'static str> {
let mut withheld: Vec<&'static str> = Vec::new();
if local.shell_allowlist != default_shell_allowlist() {
local.shell_allowlist = default_shell_allowlist();
withheld.push("shell_allowlist");
}
if !local.shell_allowlist_extra.is_empty() {
local.shell_allowlist_extra.clear();
withheld.push("shell_allowlist_extra");
}
if !local.allow_paths.is_empty() {
local.allow_paths.clear();
withheld.push("allow_paths");
}
if !local.extra_roots.is_empty() {
local.extra_roots.clear();
withheld.push("extra_roots");
}
if !local.allow_symlink_roots.is_empty() {
local.allow_symlink_roots.clear();
withheld.push("allow_symlink_roots");
}
if !local.custom_aliases.is_empty() {
local.custom_aliases.clear();
withheld.push("custom_aliases");
}
if !local.passthrough_urls.is_empty() {
local.passthrough_urls.clear();
withheld.push("passthrough_urls");
}
if !local.redirect_exclude.is_empty() {
local.redirect_exclude.clear();
withheld.push("redirect_exclude");
}
if local.proxy.anthropic_upstream.is_some()
|| local.proxy.openai_upstream.is_some()
|| local.proxy.chatgpt_upstream.is_some()
|| local.proxy.gemini_upstream.is_some()
{
local.proxy.anthropic_upstream = None;
local.proxy.openai_upstream = None;
local.proxy.chatgpt_upstream = None;
local.proxy.gemini_upstream = None;
withheld.push("proxy.*_upstream");
}
if local.semantic_mode == super::SemanticMode::Eager {
local.semantic_mode = super::SemanticMode::default();
withheld.push("semantic_mode");
}
if local.rules_scope.is_some() {
local.rules_scope = None;
withheld.push("rules_scope");
}
if local.rules_injection.is_some() {
local.rules_injection = None;
withheld.push("rules_injection");
}
if local.permission_inheritance.is_some() {
local.permission_inheritance = None;
withheld.push("permission_inheritance");
}
if !local.disabled_tools.is_empty() {
local.disabled_tools.clear();
withheld.push("disabled_tools");
}
if local.tool_profile.is_some() {
local.tool_profile = None;
withheld.push("tool_profile");
}
if !local.tools_enabled.is_empty() {
local.tools_enabled.clear();
withheld.push("tools_enabled");
}
if !local.default_tool_categories.is_empty() {
local.default_tool_categories.clear();
withheld.push("default_tool_categories");
}
if !local.index.respect_gitignore {
local.index.respect_gitignore = true;
withheld.push("index.respect_gitignore");
}
if !local.shell_allowlist_subcommand_scoping {
local.shell_allowlist_subcommand_scoping = true;
withheld.push("shell_allowlist_subcommand_scoping");
}
withheld
}
#[must_use]
pub fn local_sensitive_overrides(local_toml: &str) -> Vec<&'static str> {
let selected = environment_config_profile();
match parse_config_with_profile(local_toml, selected.as_deref()) {
Ok(mut parsed) => strip_sensitive_overrides(&mut parsed),
Err(_) => Vec::new(),
}
}
impl Config {
pub fn path() -> Option<PathBuf> {
crate::core::paths::config_dir_read_only()
.ok()
.map(|d| d.join("config.toml"))
}
#[must_use]
pub fn missing_config_path() -> Option<PathBuf> {
match Self::path() {
Some(p) if !p.exists() => Some(p),
_ => None,
}
}
pub fn local_path(project_root: &str) -> PathBuf {
PathBuf::from(project_root).join(".lean-ctx.toml")
}
pub(crate) fn find_project_root() -> Option<String> {
static ROOT_CACHE: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
ROOT_CACHE
.get_or_init(Self::find_project_root_inner)
.clone()
}
fn find_project_root_inner() -> Option<String> {
if let Ok(env_root) = std::env::var("LEAN_CTX_PROJECT_ROOT")
&& !env_root.is_empty()
{
return Some(env_root);
}
let cwd = std::env::current_dir().ok();
if let Some(root) =
crate::core::session::SessionState::load_latest().and_then(|s| s.project_root)
{
let root_path = std::path::Path::new(&root);
let cwd_is_under_root = cwd.as_ref().is_some_and(|c| c.starts_with(root_path));
let has_marker = crate::core::pathutil::has_project_marker(root_path);
if (cwd_is_under_root || has_marker) && crate::core::pathutil::may_probe_path(root_path)
{
return Some(root);
}
}
if let Some(ref cwd) = cwd {
let may_probe_cwd = crate::core::pathutil::may_probe_path(cwd);
let git_root = if may_probe_cwd {
std::process::Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.current_dir(cwd)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::null())
.output()
.ok()
.and_then(|o| {
if o.status.success() {
String::from_utf8(o.stdout)
.ok()
.map(|s| s.trim().to_string())
} else {
None
}
})
} else {
None
};
if let Some(root) = git_root {
return Some(root);
}
if may_probe_cwd && !crate::core::pathutil::is_broad_or_unsafe_root(cwd) {
return Some(cwd.to_string_lossy().to_string());
}
}
None
}
pub fn load() -> Self {
(*Self::load_arc()).clone()
}
pub fn load_arc() -> Arc<Self> {
static CACHE: Mutex<ConfigCacheSlot> = Mutex::new(None);
let Some(path) = Self::path() else {
return Arc::new(Self::default());
};
let project_root = Self::find_project_root();
let local_path = project_root.as_deref().map(Self::local_path);
let global_content = std::fs::read_to_string(&path).ok();
let local_content = local_path
.as_ref()
.filter(|p| crate::core::pathutil::may_probe_path(p.as_path()))
.and_then(|p| std::fs::read_to_string(p).ok());
let global_hash = global_content.as_deref().map(crate::core::hasher::hash_str);
let local_hash = local_content.as_deref().map(crate::core::hasher::hash_str);
let selected_profile = environment_config_profile();
if let Ok(guard) = CACHE.lock()
&& let Some((ref cfg, ref cached_global, ref cached_local, ref cached_profile)) = *guard
&& *cached_global == global_hash
&& *cached_local == local_hash
&& *cached_profile == selected_profile
{
return Arc::clone(cfg);
}
let mut cfg: Config = if let Some(ref content) = global_content {
match parse_config_with_profile(content, selected_profile.as_deref()) {
Ok(c) => {
record_parse_error(None);
c
}
Err(e) => {
record_parse_error(Some(e.clone()));
tracing::warn!("config parse error in {}: {e}", path.display());
eprintln!(
"\x1b[33m[lean-ctx] WARNING: config parse error in {}: {e}\n \
Using defaults. Run `lean-ctx doctor --fix` to repair.\x1b[0m",
path.display()
);
Self::default()
}
}
} else {
record_parse_error(None);
Self::default()
};
if let Some(ref local) = local_content {
let trusted = project_root.as_deref().is_some_and(|r| {
crate::core::workspace_trust::is_trusted_for(
std::path::Path::new(r),
local_hash.as_deref().unwrap_or_default(),
)
});
cfg.merge_local(local, trusted);
}
cfg.migrate_v4_config_on_disk();
let cfg = Arc::new(cfg);
if let Ok(mut guard) = CACHE.lock() {
*guard = Some((Arc::clone(&cfg), global_hash, local_hash, selected_profile));
}
cfg
}
pub fn load_for_project_root(project_root: &str) -> Self {
let selected_profile = environment_config_profile();
let mut cfg = Self::path()
.and_then(|p| std::fs::read_to_string(p).ok())
.and_then(|c| parse_config_with_profile(&c, selected_profile.as_deref()).ok())
.unwrap_or_default();
let local_path = Self::local_path(project_root);
if crate::core::pathutil::may_probe_path(local_path.as_path())
&& let Ok(local) = std::fs::read_to_string(&local_path)
{
let trusted = crate::core::workspace_trust::is_trusted_for(
std::path::Path::new(project_root),
&crate::core::hasher::hash_str(&local),
);
cfg.merge_local(&local, trusted);
}
cfg
}
pub(crate) fn migrate_v4_config_on_disk(&mut self) {
let path = Self::path();
let writable = path
.as_deref()
.is_some_and(allows_automatic_config_migration);
let raw = path
.as_deref()
.filter(|_| writable)
.and_then(|path| std::fs::read_to_string(path).ok());
let reset_pending = crate::core::telemetry_consent::legacy_opt_out_reset_pending();
let reset = raw
.as_deref()
.filter(|_| reset_pending)
.and_then(reset_legacy_telemetry_opt_out);
if reset.is_some() && self.telemetry.preference == super::TelemetryPreference::DefaultOn {
self.telemetry.enabled = true;
}
let preserve_opt_out = self.telemetry.explicitly_disabled();
if self.cloud.contribute_enabled {
self.cloud.contribute_enabled = false;
if !preserve_opt_out {
self.telemetry.enabled = true;
self.telemetry.preference = super::TelemetryPreference::ExplicitlyEnabled;
}
}
if matches!(self.cognitive_mode, CognitiveMode::Basic) {
self.cognitive_mode = CognitiveMode::Full;
}
let Some(path) = path else {
return;
};
let Some(raw) = raw else {
if !writable {
tracing::debug!(
"config migration remains in memory: source or directory is read-only"
);
}
if reset_pending && !path.exists() {
crate::core::telemetry_consent::mark_legacy_opt_out_reset();
}
return;
};
let source = reset.as_deref().unwrap_or(&raw);
let migrated = match Self::migrate_v4_config_document(
source,
environment_config_profile().as_deref(),
preserve_opt_out,
) {
Ok(Some(updated)) => Some(updated),
Ok(None) => (source != raw).then(|| source.to_string()),
Err(error) => {
tracing::warn!("config migration failed: {error}");
return;
}
};
let persisted = match migrated {
Some(updated) => crate::config_io::write_atomic_config_migration_checked(
&path,
&updated,
Some(raw.as_bytes()),
)
.map_err(|error| tracing::warn!("config migration could not be persisted: {error}"))
.is_ok(),
None => true,
};
if persisted && reset_pending {
crate::core::telemetry_consent::mark_legacy_opt_out_reset();
}
}
pub(super) fn migrate_v4_config_document(
raw: &str,
selected_profile: Option<&str>,
preserve_opt_out: bool,
) -> Result<Option<String>, String> {
let document = raw
.parse::<toml_edit::DocumentMut>()
.map_err(|error| error.to_string())?;
let legacy_telemetry = document
.get("cloud")
.and_then(|table| table.get("contribute_enabled"))
.and_then(toml_edit::Item::as_bool)
== Some(true);
let persisted_opt_out = document
.get("telemetry")
.and_then(|table| table.get("enabled"))
.and_then(toml_edit::Item::as_bool)
== Some(false);
let mut updated = if legacy_telemetry {
migrate_legacy_contribute_document(raw, preserve_opt_out || persisted_opt_out)
.ok_or_else(|| "invalid legacy telemetry configuration".to_string())?
} else {
raw.to_string()
};
if let Some(next) = Self::validated_v3_compression_document(&updated, selected_profile)? {
updated = next;
}
let mut document = updated
.parse::<toml_edit::DocumentMut>()
.map_err(|error| error.to_string())?;
if document
.get("cognitive_mode")
.and_then(toml_edit::Item::as_str)
== Some("basic")
{
document["cognitive_mode"] = toml_edit::value("full");
updated = document.to_string();
}
parse_config_with_profile(&updated, selected_profile)?;
Ok((updated != raw).then_some(updated))
}
pub(crate) fn validated_v3_compression_document(
raw: &str,
selected_profile: Option<&str>,
) -> Result<Option<String>, String> {
let migrated = Self::migrate_v3_compression_document(raw)?;
if let Some(ref updated) = migrated {
parse_config_with_profile(updated, selected_profile)?;
}
Ok(migrated)
}
pub(crate) fn migrate_v3_compression_document(raw: &str) -> Result<Option<String>, String> {
fn migrate_table(table: &mut toml_edit::Table) -> Result<bool, String> {
let has_legacy = ["terse_agent", "output_density", "ultra_compact"]
.iter()
.any(|key| table.get(key).is_some());
if !has_legacy {
return Ok(false);
}
let terse = match table.get("terse_agent") {
Some(item) => Some(
item.as_str()
.ok_or_else(|| "terse_agent must be a string".to_string())?,
),
None => None,
};
if terse.is_some_and(|value| !matches!(value, "off" | "lite" | "full" | "ultra")) {
return Err("invalid terse_agent".to_string());
}
let density = match table.get("output_density") {
Some(item) => Some(
item.as_str()
.ok_or_else(|| "output_density must be a string".to_string())?,
),
None => None,
};
if density.is_some_and(|value| !matches!(value, "normal" | "terse" | "ultra")) {
return Err("invalid output_density".to_string());
}
let ultra = match table.get("ultra_compact") {
Some(item) => Some(
item.as_bool()
.ok_or_else(|| "ultra_compact must be a boolean".to_string())?,
),
None => None,
};
let level = if let Some(item) = table.get("compression_level") {
let value = item
.as_str()
.ok_or_else(|| "compression_level must be a string".to_string())?;
if !matches!(value, "off" | "lite" | "standard" | "max" | "raw") {
return Err(format!("invalid compression_level: {value}"));
}
value
} else if ultra == Some(true) || terse == Some("ultra") || density == Some("ultra") {
"max"
} else if terse == Some("full") {
"standard"
} else if terse == Some("lite") || density == Some("terse") {
"lite"
} else {
"off"
};
table["compression_level"] = toml_edit::value(level);
for key in ["terse_agent", "output_density", "ultra_compact"] {
table.remove(key);
}
Ok(true)
}
let mut document = raw
.parse::<toml_edit::DocumentMut>()
.map_err(|error| error.to_string())?;
let mut changed = migrate_table(document.as_table_mut())?;
if let Some(profiles) = document
.get_mut("profiles")
.and_then(toml_edit::Item::as_table_mut)
{
for (_, profile) in profiles.iter_mut() {
if let Some(table) = profile.as_table_mut() {
changed |= migrate_table(table)?;
}
}
}
Ok(changed.then(|| document.to_string()))
}
pub fn load_global() -> Self {
Self::path().map_or_else(Self::default, |p| Self::load_global_from(&p))
}
pub fn try_load_global() -> Result<Self, super::error::LeanCtxError> {
let path = Self::path().ok_or_else(|| {
super::error::LeanCtxError::Config("cannot determine home directory".into())
})?;
Self::try_load_global_from(&path)
}
pub(super) fn try_load_global_from(path: &Path) -> Result<Self, super::error::LeanCtxError> {
match std::fs::read_to_string(path) {
Ok(raw) if !raw.trim().is_empty() => toml::from_str(&raw).map_err(|error| {
super::error::LeanCtxError::Config(
format!("refusing invalid global config.toml ({error})").into(),
)
}),
Ok(_) => Ok(Self::default()),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(Self::default()),
Err(error) => Err(error.into()),
}
}
pub(super) fn load_global_from(path: &Path) -> Self {
Self::try_load_global_from(path).unwrap_or_default()
}
}
fn allows_automatic_config_migration(path: &Path) -> bool {
path.parent().is_some_and(|parent| {
[path, parent].iter().all(|entry| {
std::fs::metadata(entry).is_ok_and(|metadata| !metadata.permissions().readonly())
})
})
}
pub(super) fn reset_legacy_telemetry_opt_out(raw: &str) -> Option<String> {
let mut document = raw.parse::<toml_edit::DocumentMut>().ok()?;
let telemetry = document.get("telemetry")?;
let disabled = telemetry.get("enabled").and_then(toml_edit::Item::as_bool) == Some(false);
let implicit = matches!(
telemetry.get("preference").map(toml_edit::Item::as_str),
None | Some(Some("default_on"))
);
if !(disabled && implicit) {
return None;
}
document["telemetry"]["enabled"] = toml_edit::value(true);
Some(document.to_string())
}
fn migrate_legacy_contribute_document(raw: &str, preserve_opt_out: bool) -> Option<String> {
let mut document = raw.parse::<toml_edit::DocumentMut>().ok()?;
document["cloud"]["contribute_enabled"] = toml_edit::value(false);
if !preserve_opt_out {
document["telemetry"]["enabled"] = toml_edit::value(true);
document["telemetry"]["preference"] = toml_edit::value("explicitly_enabled");
}
Some(document.to_string())
}
#[cfg(test)]
mod telemetry_migration_tests {
use super::*;
#[test]
#[cfg(unix)]
fn automatic_migration_respects_readonly_file_and_directory() {
use std::os::unix::fs::PermissionsExt;
let directory = tempfile::tempdir().expect("migration directory");
let path = directory.path().join("config.toml");
std::fs::write(&path, "ultra_compact = true\n").expect("legacy config");
assert!(allows_automatic_config_migration(&path));
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o400)).unwrap();
let readonly_file = allows_automatic_config_migration(&path);
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
std::fs::set_permissions(directory.path(), std::fs::Permissions::from_mode(0o500)).unwrap();
let readonly_directory = allows_automatic_config_migration(&path);
std::fs::set_permissions(directory.path(), std::fs::Permissions::from_mode(0o700)).unwrap();
assert!(!readonly_file);
assert!(!readonly_directory);
assert!(!allows_automatic_config_migration(
&directory.path().join("missing.toml")
));
}
#[test]
fn legacy_opt_in_becomes_explicit_and_send_eligible() {
let migrated = migrate_legacy_contribute_document(
"# keep me\n[cloud]\ncontribute_enabled = true\n",
false,
)
.expect("valid TOML");
assert!(migrated.contains("# keep me"));
let cfg: Config = toml::from_str(&migrated).expect("migrated config");
assert!(!cfg.cloud.contribute_enabled);
assert!(cfg.telemetry.enabled);
assert_eq!(
cfg.telemetry.preference,
super::super::TelemetryPreference::ExplicitlyEnabled
);
assert!(cfg.telemetry.send_eligible(None, None));
}
#[test]
fn legacy_contribute_never_overrides_explicit_opt_out() {
let migrated = migrate_legacy_contribute_document(
"[cloud]\ncontribute_enabled = true\n[telemetry]\nenabled = false\n",
true,
)
.expect("valid TOML");
let cfg: Config = toml::from_str(&migrated).expect("migrated config");
assert!(!cfg.cloud.contribute_enabled);
assert!(cfg.telemetry.explicitly_disabled());
}
}