Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/spark-rsi-mv/blob/main/src/config.rs # spark-rsi-mv Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `spark-rsi-mv / src / config.rs` 192 lines ยท 6064 bytes Raw Blame History 1 use serde::{Deserialize, Serialize}; 2 use std::fs; 3 use std::path::PathBuf; 4 use std::process::Command; 5 6 #[derive(Debug, Clone, Serialize, Deserialize)] 7 pub struct OperatorProfile { 8 #[serde(default = "default_operator_name")] 9 pub name: String, 10 #[serde(default = "default_operator_email")] 11 pub email: String, 12 #[serde(default = "default_operator_handle")] 13 pub handle: String, 14 #[serde(default = "default_sign_commits")] 15 pub sign_commits: bool, 16 } 17 18 impl Default for OperatorProfile { 19 fn default() -> Self { 20 Self { 21 name: default_operator_name(), 22 email: default_operator_email(), 23 handle: default_operator_handle(), 24 sign_commits: default_sign_commits(), 25 } 26 } 27 } 28 29 fn default_operator_name() -> String { 30 "Sovereign Operator".to_string() 31 } 32 fn default_operator_email() -> String { 33 "operator@local".to_string() 34 } 35 fn default_operator_handle() -> String { 36 "operator".to_string() 37 } 38 fn default_sign_commits() -> bool { 39 false 40 } 41 42 #[derive(Debug, Clone, Serialize, Deserialize)] 43 pub struct EngineConfig { 44 #[serde(default = "default_engine_mode")] 45 pub mode: String, // "local" (the local model seat at api_base_url) or "api" 46 #[serde(default = "default_api_base_url")] 47 pub api_base_url: String, 48 #[serde(default)] 49 pub api_key: String, 50 #[serde(default = "default_model_id")] 51 pub model_id: String, 52 #[serde(default = "default_context_window")] 53 pub context_window: usize, 54 #[serde(default = "default_temperature")] 55 pub temperature: f32, 56 } 57 58 impl Default for EngineConfig { 59 fn default() -> Self { 60 Self { 61 mode: default_engine_mode(), 62 api_base_url: default_api_base_url(), 63 api_key: String::new(), 64 model_id: default_model_id(), 65 context_window: default_context_window(), 66 temperature: default_temperature(), 67 } 68 } 69 } 70 71 fn default_engine_mode() -> String { 72 "local".to_string() 73 } 74 fn default_api_base_url() -> String { 75 "http://127.0.0.1:18006/v1".to_string() 76 } 77 fn default_model_id() -> String { 78 "nvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-BF16".to_string() 79 } 80 fn default_context_window() -> usize { 81 32768 82 } 83 fn default_temperature() -> f32 { 84 0.7 85 } 86 87 #[derive(Debug, Clone, Serialize, Deserialize, Default)] 88 pub struct SovereignConfig { 89 #[serde(default)] 90 pub operator: OperatorProfile, 91 #[serde(default)] 92 pub engine: EngineConfig, 93 } 94 95 impl SovereignConfig { 96 pub fn config_path() -> PathBuf { 97 if let Ok(path) = std::env::var("SOVEREIGN_CONFIG_PATH") { 98 return PathBuf::from(path); 99 } 100 let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string()); 101 PathBuf::from(home).join(".config/sovereign/operator.toml") 102 } 103 104 pub fn load() -> Self { 105 let path = Self::config_path(); 106 let mut config = if path.exists() { 107 match fs::read_to_string(&path) { 108 Ok(content) => toml::from_str(&content).unwrap_or_default(), 109 Err(_) => Self::default(), 110 } 111 } else { 112 Self::default() 113 }; 114 115 // Environment overrides 116 if let Ok(name) = std::env::var("SOVEREIGN_OPERATOR_NAME") { 117 config.operator.name = name; 118 } 119 if let Ok(email) = std::env::var("SOVEREIGN_OPERATOR_EMAIL") { 120 config.operator.email = email; 121 } 122 if let Ok(handle) = std::env::var("SOVEREIGN_OPERATOR_HANDLE") { 123 config.operator.handle = handle; 124 } 125 if let Ok(mode) = std::env::var("SOVEREIGN_ENGINE_MODE") { 126 config.engine.mode = mode; 127 } 128 if let Ok(url) = std::env::var("SOVEREIGN_API_BASE_URL") { 129 config.engine.api_base_url = url; 130 } 131 132 // Fallback to git config if name or email still default 133 if config.operator.name == "Sovereign Operator" { 134 if let Ok(out) = Command::new("git").args(["config", "user.name"]).output() { 135 let git_name = String::from_utf8_lossy(&out.stdout).trim().to_string(); 136 if !git_name.is_empty() { 137 config.operator.name = git_name; 138 } 139 } 140 } 141 if config.operator.email == "operator@local" { 142 if let Ok(out) = Command::new("git").args(["config", "user.email"]).output() { 143 let git_email = String::from_utf8_lossy(&out.stdout).trim().to_string(); 144 if !git_email.is_empty() { 145 config.operator.email = git_email; 146 } 147 } 148 } 149 150 config 151 } 152 153 pub fn save(&self) -> Result<(), String> { 154 let path = Self::config_path(); 155 if let Some(parent) = path.parent() { 156 fs::create_dir_all(parent) 157 .map_err(|e| format!("Failed to create config dir: {}", e))?; 158 } 159 let serialized = toml::to_string_pretty(self) 160 .map_err(|e| format!("Failed to serialize config: {}", e))?; 161 fs::write(&path, serialized) 162 .map_err(|e| format!("Failed to write config to {:?}: {}", path, e))?; 163 Ok(()) 164 } 165 166 pub fn author_string(&self) -> String { 167 format!("{} <{}>", self.operator.name, self.operator.email) 168 } 169 } 170 171 #[cfg(test)] 172 mod tests { 173 use super::*; 174 175 #[test] 176 fn test_default_config_and_author_string() { 177 let mut cfg = SovereignConfig::default(); 178 cfg.operator.name = "Alice".to_string(); 179 cfg.operator.email = "alice@domain.org".to_string(); 180 assert_eq!(cfg.author_string(), "Alice "); 181 assert_eq!(cfg.engine.mode, "local"); 182 } 183 184 #[test] 185 fn test_serialization_roundtrip() { 186 let cfg = SovereignConfig::default(); 187 let serialized = toml::to_string_pretty(&cfg).unwrap(); 188 let deserialized: SovereignConfig = toml::from_str(&serialized).unwrap(); 189 assert_eq!(deserialized.operator.name, cfg.operator.name); 190 assert_eq!(deserialized.engine.mode, cfg.engine.mode); 191 } 192 }