use crate::error::{Error, Result};
use crate::permissions::PermissionMode;
use crate::permissions::{LayeredPermissionsConfig, PermissionLayer, RuleSource};
use serde::Deserialize;
use std::path::{Path, PathBuf};
pub fn config_file_path() -> Option<PathBuf> {
if let Some(custom) = std::env::var_os("RECURSIVE_HOME") {
return Some(PathBuf::from(custom).join(".recursive").join("config.toml"));
}
dirs::home_dir().map(|h| h.join(".recursive").join("config.toml"))
}
#[derive(Debug, Default, Deserialize)]
pub struct FileConfig {
pub provider: Option<ProviderSection>,
pub agent: Option<AgentSection>,
pub permissions: Option<PermissionsSection>,
}
#[derive(Debug, Deserialize)]
pub struct ProviderSection {
#[serde(rename = "type")]
pub provider_type: Option<String>,
pub api_key: Option<String>,
pub api_base: Option<String>,
pub model: Option<String>,
#[serde(default)]
pub preset: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct AgentSection {
pub max_steps: Option<usize>,
pub temperature: Option<f64>,
pub shell_timeout_secs: Option<u64>,
}
#[derive(Debug, Default, Deserialize, Clone)]
pub struct PermissionsSection {
#[serde(default)]
pub allow: Vec<String>,
#[serde(default)]
pub deny: Vec<String>,
#[serde(default)]
pub interactive: Vec<String>,
#[serde(default)]
pub plan: Vec<String>,
#[serde(default)]
pub mode: Option<PermissionMode>,
}
impl FileConfig {
pub fn load() -> Result<Option<Self>> {
let path = match config_file_path() {
Some(p) => p,
None => return Ok(None),
};
Self::load_from(&path)
}
pub fn load_from(path: &Path) -> Result<Option<Self>> {
if !path.exists() {
return Ok(None);
}
let content = std::fs::read_to_string(path).map_err(Error::Io)?;
let config: FileConfig = toml::from_str(&content).map_err(|e| Error::Config {
message: format!("failed to parse config file {}: {}", path.display(), e),
})?;
Ok(Some(config))
}
}
pub fn set_value(key: &str, value: &str) -> Result<()> {
let path = config_file_path().ok_or_else(|| Error::Config {
message: "cannot determine home directory".into(),
})?;
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(Error::Io)?;
}
let content = if path.exists() {
std::fs::read_to_string(&path).map_err(Error::Io)?
} else {
String::new()
};
let mut doc: toml::Table = content.parse::<toml::Table>().unwrap_or_default();
let parts: Vec<&str> = key.splitn(2, '.').collect();
match parts.as_slice() {
[section, field] => {
let table = doc
.entry(*section)
.or_insert_with(|| toml::Value::Table(toml::Table::new()));
if let toml::Value::Table(t) = table {
t.insert(field.to_string(), smart_value(value));
}
}
[field] => {
doc.insert(field.to_string(), smart_value(value));
}
_ => {
return Err(Error::Config {
message: format!("invalid key format: {key}"),
})
}
}
let toml_str = toml::to_string_pretty(&doc).map_err(|e| Error::Config {
message: format!("failed to serialize config: {}", e),
})?;
std::fs::write(&path, toml_str).map_err(Error::Io)?;
Ok(())
}
fn smart_value(s: &str) -> toml::Value {
if let Ok(i) = s.parse::<i64>() {
toml::Value::Integer(i)
} else if let Ok(f) = s.parse::<f64>() {
toml::Value::Float(f)
} else if s == "true" || s == "false" {
toml::Value::Boolean(s == "true")
} else {
toml::Value::String(s.to_string())
}
}
pub fn load_layered_permissions(workspace: &Path) -> LayeredPermissionsConfig {
let mut config = LayeredPermissionsConfig::default();
if let Some(home) = std::env::var("HOME")
.ok()
.map(PathBuf::from)
.or_else(dirs::home_dir)
{
let path = home.join(".recursive").join("config.toml");
if let Some(layer) = load_permission_layer(&path, RuleSource::User) {
config.layers.push(layer);
}
}
let project_path = workspace.join(".recursive").join("config.toml");
if let Some(layer) = load_permission_layer(&project_path, RuleSource::Project) {
config.layers.push(layer);
}
config.layers.push(PermissionLayer {
source: RuleSource::Session,
..Default::default()
});
config
}
fn load_permission_layer(path: &Path, source: RuleSource) -> Option<PermissionLayer> {
if !path.exists() {
return None;
}
let content = std::fs::read_to_string(path).ok()?;
let file_config: FileConfig = toml::from_str(&content).ok()?;
let section = file_config.permissions?;
Some(PermissionLayer {
source,
allow: section.allow,
deny: section.deny,
interactive: section.interactive,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn load_returns_none_when_missing() {
let result = FileConfig::load_from(Path::new("/nonexistent/path.toml")).unwrap();
assert!(result.is_none());
}
#[test]
fn load_parses_valid_toml() {
let tmp = tempfile::NamedTempFile::new().unwrap();
std::fs::write(
tmp.path(),
r#"
[provider]
type = "openai"
api_key = "sk-test"
api_base = "https://api.deepseek.com"
model = "deepseek-chat"
[agent]
max_steps = 64
temperature = 0.5
"#,
)
.unwrap();
let config = FileConfig::load_from(tmp.path()).unwrap();
assert!(config.is_some());
let c = config.unwrap();
let p = c.provider.unwrap();
assert_eq!(p.provider_type.as_deref(), Some("openai"));
assert_eq!(p.api_key.as_deref(), Some("sk-test"));
assert_eq!(p.api_base.as_deref(), Some("https://api.deepseek.com"));
assert_eq!(p.model.as_deref(), Some("deepseek-chat"));
let a = c.agent.unwrap();
assert_eq!(a.max_steps, Some(64));
assert_eq!(a.temperature, Some(0.5));
}
#[test]
fn load_errors_on_malformed() {
let tmp = tempfile::NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "this is [[[not valid toml").unwrap();
let result = FileConfig::load_from(tmp.path());
assert!(result.is_err());
}
#[test]
fn smart_value_parses_types() {
assert_eq!(smart_value("42"), toml::Value::Integer(42));
assert_eq!(smart_value("0.5"), toml::Value::Float(0.5));
assert_eq!(smart_value("true"), toml::Value::Boolean(true));
assert_eq!(smart_value("hello"), toml::Value::String("hello".into()));
}
#[test]
fn parse_provider_section_with_preset() {
let tmp = tempfile::NamedTempFile::new().unwrap();
std::fs::write(
tmp.path(),
r#"
[provider]
preset = "deepseek"
"#,
)
.unwrap();
let config = FileConfig::load_from(tmp.path()).unwrap().unwrap();
let p = config.provider.unwrap();
assert_eq!(p.preset.as_deref(), Some("deepseek"));
assert!(p.provider_type.is_none());
assert!(p.api_base.is_none());
assert!(p.model.is_none());
assert!(p.api_key.is_none());
}
#[test]
fn set_value_preset_round_trips() {
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("config.toml");
std::fs::create_dir_all(tmp.path()).unwrap();
std::fs::write(&path, "[provider]\nmodel = \"x\"\n").unwrap();
let content = std::fs::read_to_string(&path).unwrap();
let mut doc: toml::Table = content.parse().unwrap();
let parts: Vec<&str> = "provider.preset".splitn(2, '.').collect();
if let [section, field] = parts.as_slice() {
let table = doc
.entry(*section)
.or_insert_with(|| toml::Value::Table(toml::Table::new()));
if let toml::Value::Table(t) = table {
t.insert(field.to_string(), smart_value("anthropic"));
}
}
std::fs::write(&path, toml::to_string_pretty(&doc).unwrap()).unwrap();
let loaded = FileConfig::load_from(&path).unwrap().unwrap();
assert_eq!(
loaded.provider.unwrap().preset.as_deref(),
Some("anthropic")
);
}
#[test]
fn set_value_creates_file_and_writes() {
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("config.toml");
std::fs::create_dir_all(tmp.path()).unwrap();
let content = String::new();
let mut doc: toml::Table = content.parse::<toml::Table>().unwrap_or_default();
let parts: Vec<&str> = "provider.model".splitn(2, '.').collect();
if let [section, field] = parts.as_slice() {
let table = doc
.entry(*section)
.or_insert_with(|| toml::Value::Table(toml::Table::new()));
if let toml::Value::Table(t) = table {
t.insert(field.to_string(), smart_value("deepseek-chat"));
}
}
let output = toml::to_string_pretty(&doc).unwrap();
std::fs::write(&path, &output).unwrap();
let loaded = FileConfig::load_from(&path).unwrap().unwrap();
assert_eq!(
loaded.provider.unwrap().model.as_deref(),
Some("deepseek-chat")
);
}
#[test]
fn test_load_layered_permissions_session_layer_always_present() {
let tmp = tempfile::tempdir().unwrap();
let workspace = tmp.path().join("workspace");
std::fs::create_dir_all(&workspace).unwrap();
let fake_home = tmp.path().join("home");
std::fs::create_dir_all(&fake_home).unwrap();
let _pin = crate::test_util::PinnedHome::new(&fake_home);
let config = load_layered_permissions(&workspace);
assert!(config
.layers
.iter()
.any(|l| l.source == RuleSource::Session));
assert_eq!(config.layers.len(), 1);
assert_eq!(config.layers[0].source, RuleSource::Session);
}
#[test]
fn test_load_layered_permissions_loads_user_and_project() {
let tmp = tempfile::tempdir().unwrap();
let home = tmp.path().join("home");
let project = tmp.path().join("project");
std::fs::create_dir_all(home.join(".recursive")).unwrap();
std::fs::write(
home.join(".recursive").join("config.toml"),
r#"
[permissions]
allow = ["read_file"]
deny = ["run_shell"]
"#,
)
.unwrap();
std::fs::create_dir_all(project.join(".recursive")).unwrap();
std::fs::write(
project.join(".recursive").join("config.toml"),
r#"
[permissions]
allow = ["write_file"]
interactive = ["delete_file"]
"#,
)
.unwrap();
let old_home = std::env::var("HOME").ok();
std::env::set_var("HOME", &home);
let config = load_layered_permissions(&project);
if let Some(h) = old_home {
std::env::set_var("HOME", h);
} else {
std::env::remove_var("HOME");
}
assert_eq!(config.layers.len(), 3);
assert_eq!(config.layers[0].source, RuleSource::User);
assert_eq!(config.layers[1].source, RuleSource::Project);
assert_eq!(config.layers[2].source, RuleSource::Session);
assert_eq!(config.layers[0].allow, vec!["read_file"]);
assert_eq!(config.layers[0].deny, vec!["run_shell"]);
assert_eq!(config.layers[1].allow, vec!["write_file"]);
assert_eq!(config.layers[1].interactive, vec!["delete_file"]);
}
}