use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::Environment;
pub const DEFAULT_CONFIG_PATH: &str = ".taskfast/config.json";
pub const CURRENT_SCHEMA_VERSION: u32 = 2;
const LEGACY_REMOVED_KEYS: &[&str] = &["api_base", "network"];
#[derive(Debug, Error)]
pub enum ConfigError {
#[error("I/O on {path}: {source}")]
Io {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("parse {path}: {source}")]
Parse {
path: PathBuf,
#[source]
source: serde_json::Error,
},
#[error(
"config {path} carries removed key(s) {fields:?} — derived from `environment` since \
schema v2. Run `taskfast config migrate` (or remove the keys + bump `schema_version` \
to 2 manually) and re-run."
)]
LegacyFields { path: PathBuf, fields: Vec<String> },
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(default)]
pub struct Config {
#[serde(skip_serializing_if = "is_zero")]
pub schema_version: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub environment: Option<Environment>,
#[serde(skip_serializing_if = "Option::is_none")]
pub api_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub wallet_address: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub keystore_path: Option<PathBuf>,
#[serde(skip_serializing_if = "Option::is_none")]
pub agent_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub webhook_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub webhook_secret_path: Option<PathBuf>,
#[serde(skip_serializing_if = "Option::is_none")]
pub confirm_above_budget: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub log_format: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub receipt_timeout: Option<String>,
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_zero(v: &u32) -> bool {
*v == 0
}
fn ensure_dir_gitignore(dir: &Path) {
let expected = Path::new(DEFAULT_CONFIG_PATH)
.parent()
.and_then(Path::file_name);
if dir.file_name() != expected {
return;
}
let gitignore = dir.join(".gitignore");
match fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&gitignore)
{
Ok(mut f) => {
if let Err(e) = f.write_all(b"*\n") {
tracing::warn!(path = %gitignore.display(), error = %e, "could not write .taskfast/.gitignore");
}
}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
Err(e) => {
tracing::warn!(path = %gitignore.display(), error = %e, "could not create .taskfast/.gitignore");
}
}
}
impl Config {
pub fn load(path: &Path) -> Result<Self, ConfigError> {
let src = match fs::read_to_string(path) {
Ok(s) => s,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Self::default()),
Err(source) => {
return Err(ConfigError::Io {
path: path.to_path_buf(),
source,
});
}
};
let raw: serde_json::Value =
serde_json::from_str(&src).map_err(|source| ConfigError::Parse {
path: path.to_path_buf(),
source,
})?;
if let Some(obj) = raw.as_object() {
let legacy: Vec<String> = LEGACY_REMOVED_KEYS
.iter()
.filter(|k| obj.contains_key(**k))
.map(|k| (*k).to_string())
.collect();
if !legacy.is_empty() {
return Err(ConfigError::LegacyFields {
path: path.to_path_buf(),
fields: legacy,
});
}
}
let cfg: Config = serde_json::from_value(raw).map_err(|source| ConfigError::Parse {
path: path.to_path_buf(),
source,
})?;
if cfg.schema_version > CURRENT_SCHEMA_VERSION {
tracing::warn!(
file = %path.display(),
file_version = cfg.schema_version,
current_version = CURRENT_SCHEMA_VERSION,
"config schema_version is newer than this CLI — loading recognised fields only"
);
}
Ok(cfg)
}
pub fn save(&self, path: &Path) -> Result<(), ConfigError> {
if let Some(parent) = path.parent() {
if !parent.as_os_str().is_empty() {
fs::create_dir_all(parent).map_err(|source| ConfigError::Io {
path: parent.to_path_buf(),
source,
})?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let perms = fs::Permissions::from_mode(0o700);
fs::set_permissions(parent, perms).map_err(|source| ConfigError::Io {
path: parent.to_path_buf(),
source,
})?;
}
ensure_dir_gitignore(parent);
}
}
let mut to_write = self.clone();
if to_write.schema_version == 0 {
to_write.schema_version = CURRENT_SCHEMA_VERSION;
}
let body = serde_json::to_vec_pretty(&to_write).map_err(|source| ConfigError::Parse {
path: path.to_path_buf(),
source,
})?;
let tmp = path.with_extension("json.tmp");
{
#[cfg(unix)]
let create = {
use std::os::unix::fs::OpenOptionsExt;
fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.mode(0o600)
.open(&tmp)
};
#[cfg(not(unix))]
let create = fs::File::create(&tmp);
let mut f = create.map_err(|source| ConfigError::Io {
path: tmp.clone(),
source,
})?;
f.write_all(&body).map_err(|source| ConfigError::Io {
path: tmp.clone(),
source,
})?;
f.write_all(b"\n").map_err(|source| ConfigError::Io {
path: tmp.clone(),
source,
})?;
f.flush().map_err(|source| ConfigError::Io {
path: tmp.clone(),
source,
})?;
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let perms = fs::Permissions::from_mode(0o600);
fs::set_permissions(&tmp, perms).map_err(|source| ConfigError::Io {
path: tmp.clone(),
source,
})?;
}
fs::rename(&tmp, path).map_err(|source| ConfigError::Io {
path: path.to_path_buf(),
source,
})?;
Ok(())
}
pub fn default_path() -> PathBuf {
PathBuf::from(DEFAULT_CONFIG_PATH)
}
}
impl Serialize for Environment {
fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
ser.serialize_str(match self {
Self::Prod => "prod",
Self::Staging => "staging",
Self::Local => "local",
})
}
}
impl<'de> Deserialize<'de> for Environment {
fn deserialize<D: serde::Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
let s = String::deserialize(de)?;
match s.as_str() {
"prod" | "production" => Ok(Self::Prod),
"staging" => Ok(Self::Staging),
"local" => Ok(Self::Local),
other => Err(serde::de::Error::custom(format!(
"unknown environment {other:?}; expected prod | staging | local"
))),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
fn sample() -> Config {
Config {
schema_version: CURRENT_SCHEMA_VERSION,
environment: Some(Environment::Local),
api_key: Some("am_live_secret".into()),
wallet_address: Some("0xabc".into()),
keystore_path: Some(PathBuf::from("/tmp/keystore.json")),
agent_id: Some("agent_123".into()),
webhook_url: Some("https://example.com/hook".into()),
webhook_secret_path: Some(PathBuf::from("/tmp/hook.secret")),
confirm_above_budget: Some("1000".into()),
log_format: Some("json".into()),
receipt_timeout: Some("3min".into()),
}
}
#[test]
fn load_missing_file_returns_default() {
let tmp = TempDir::new().unwrap();
let missing = tmp.path().join("nope.json");
let cfg = Config::load(&missing).expect("missing file is default, not error");
assert_eq!(cfg, Config::default());
}
#[test]
fn roundtrip_preserves_every_field() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("sub").join("config.json");
let original = sample();
original.save(&path).expect("save");
let loaded = Config::load(&path).expect("load");
assert_eq!(loaded, original);
}
#[test]
fn save_zero_schema_version_is_normalized_to_current() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("config.json");
let cfg = Config {
wallet_address: Some("0xabc".into()),
..Config::default()
};
assert_eq!(cfg.schema_version, 0);
cfg.save(&path).expect("save");
let loaded = Config::load(&path).expect("load");
assert_eq!(loaded.schema_version, CURRENT_SCHEMA_VERSION);
}
#[test]
fn save_creates_missing_parent_dir() {
let tmp = TempDir::new().unwrap();
let nested = tmp.path().join("a").join("b").join("config.json");
sample().save(&nested).expect("save into nested path");
assert!(nested.exists());
}
#[cfg(unix)]
#[test]
fn save_tightens_parent_directory_to_0700() {
use std::os::unix::fs::PermissionsExt;
let tmp = TempDir::new().unwrap();
let cfg_dir = tmp.path().join(".taskfast");
let path = cfg_dir.join("config.json");
sample().save(&path).expect("save");
let mode = fs::metadata(&cfg_dir).unwrap().permissions().mode() & 0o777;
assert_eq!(
mode, 0o700,
".taskfast/ must be 0700 so other users on the host can't \
list the keystore + webhook secret + API key"
);
}
#[test]
fn save_writes_gitignore_star_in_config_dir() {
let tmp = TempDir::new().unwrap();
let cfg_dir = tmp.path().join(".taskfast");
let path = cfg_dir.join("config.json");
sample().save(&path).expect("save");
let gi = fs::read_to_string(cfg_dir.join(".gitignore")).expect(".gitignore written");
assert_eq!(gi.trim(), "*");
}
#[test]
fn save_to_overridden_path_does_not_write_gitignore() {
let tmp = TempDir::new().unwrap();
let dir = tmp.path().join("some-project");
let path = dir.join("config.json");
sample().save(&path).expect("save");
assert!(path.exists(), "config still written");
assert!(
!dir.join(".gitignore").exists(),
"no .gitignore in a non-.taskfast dir"
);
}
#[test]
fn save_does_not_clobber_existing_gitignore() {
let tmp = TempDir::new().unwrap();
let cfg_dir = tmp.path().join(".taskfast");
fs::create_dir_all(&cfg_dir).unwrap();
fs::write(cfg_dir.join(".gitignore"), "# mine\nconfig.json\n").unwrap();
sample().save(&cfg_dir.join("config.json")).expect("save");
let gi = fs::read_to_string(cfg_dir.join(".gitignore")).unwrap();
assert_eq!(gi, "# mine\nconfig.json\n", "existing .gitignore preserved");
}
#[test]
fn unknown_fields_are_tolerated() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("config.json");
let body = r#"{
"schema_version": 2,
"wallet_address": "0xabc",
"future_field": "ignored",
"another": {"nested": true}
}"#;
fs::write(&path, body).unwrap();
let cfg = Config::load(&path).expect("unknown fields should not fail load");
assert_eq!(cfg.wallet_address.as_deref(), Some("0xabc"));
}
#[test]
fn newer_schema_version_loads_with_warning() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("config.json");
let body = format!(
r#"{{ "schema_version": {}, "wallet_address": "0xabc" }}"#,
CURRENT_SCHEMA_VERSION + 5
);
fs::write(&path, body).unwrap();
let cfg = Config::load(&path).expect("newer version still loads");
assert_eq!(cfg.schema_version, CURRENT_SCHEMA_VERSION + 5);
assert_eq!(cfg.wallet_address.as_deref(), Some("0xabc"));
}
#[test]
fn legacy_api_base_field_hard_errors() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("config.json");
let body = r#"{ "schema_version": 1, "api_base": "https://attacker.example" }"#;
fs::write(&path, body).unwrap();
let err = Config::load(&path).expect_err("legacy api_base must be rejected");
let ConfigError::LegacyFields { fields, .. } = err else {
panic!("expected LegacyFields, got {err:?}");
};
assert_eq!(fields, vec!["api_base".to_string()]);
}
#[test]
fn legacy_network_field_hard_errors() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("config.json");
let body = r#"{ "schema_version": 1, "network": "mainnet" }"#;
fs::write(&path, body).unwrap();
let err = Config::load(&path).expect_err("legacy network must be rejected");
let ConfigError::LegacyFields { fields, .. } = err else {
panic!("expected LegacyFields, got {err:?}");
};
assert_eq!(fields, vec!["network".to_string()]);
}
#[test]
fn legacy_error_lists_both_fields_when_present() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("config.json");
let body = r#"{ "api_base": "https://x", "network": "testnet" }"#;
fs::write(&path, body).unwrap();
let err = Config::load(&path).expect_err("both legacy fields must be flagged");
let ConfigError::LegacyFields { fields, .. } = err else {
panic!("expected LegacyFields, got {err:?}");
};
assert!(fields.contains(&"api_base".to_string()));
assert!(fields.contains(&"network".to_string()));
}
#[test]
fn legacy_error_message_names_migration_command() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("config.json");
fs::write(&path, r#"{ "api_base": "https://x" }"#).unwrap();
let msg = Config::load(&path).unwrap_err().to_string();
assert!(
msg.contains("taskfast config migrate"),
"remediation hint must name the migrate command: {msg}"
);
}
#[test]
fn empty_config_serializes_compactly() {
let body = serde_json::to_string(&Config::default()).unwrap();
assert_eq!(body, "{}", "default config should serialize to {{}}");
}
#[test]
fn environment_roundtrip() {
for env in [Environment::Prod, Environment::Staging, Environment::Local] {
let cfg = Config {
environment: Some(env),
..Config::default()
};
let body = serde_json::to_string(&cfg).unwrap();
let back: Config = serde_json::from_str(&body).unwrap();
assert_eq!(back.environment.map(|e| e.as_str()), Some(env.as_str()));
}
}
#[test]
fn environment_accepts_production_alias() {
let body = r#"{ "environment": "production" }"#;
let cfg: Config = serde_json::from_str(body).unwrap();
assert!(matches!(cfg.environment, Some(Environment::Prod)));
}
#[test]
fn environment_rejects_unknown() {
let body = r#"{ "environment": "moon" }"#;
let err = serde_json::from_str::<Config>(body).unwrap_err();
assert!(err.to_string().contains("moon"));
}
#[cfg(unix)]
#[test]
fn save_writes_mode_0600_on_unix() {
use std::os::unix::fs::PermissionsExt;
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("config.json");
sample().save(&path).expect("save");
let meta = fs::metadata(&path).unwrap();
let mode = meta.permissions().mode() & 0o777;
assert_eq!(mode, 0o600, "expected mode 0600, got {mode:o}");
}
#[test]
fn parse_error_includes_path() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("bad.json");
fs::write(&path, "{ not json").unwrap();
let err = Config::load(&path).unwrap_err();
let msg = err.to_string();
assert!(msg.contains("bad.json"), "error mentions path: {msg}");
}
}