spark-rsi
Resilient systems interface and fault-tolerant agent communication
Brought by drakestapleton
| 1 | use clap::{Parser, Subcommand}; |
| 2 | use spark_rsi::balance::BalanceKernel; |
| 3 | use spark_rsi::config::SovereignConfig; |
| 4 | use spark_rsi::daemon::RsiEngine; |
| 5 | use spark_rsi::models::RsiConfig; |
| 6 | use spark_rsi::observe::observe_codebase; |
| 7 | use spark_rsi::propose::ProposalGenerator; |
| 8 | use spark_rsi::verifier::InvariantVerifier; |
| 9 | use std::path::Path; |
| 10 | use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; |
| 11 | |
| 12 | #[derive(Parser, Debug)] |
| 13 | #[command( |
| 14 | name = "spark-rsi", |
| 15 | about = "Native Rust and Mojo Recursive Self-Improvement Engine on SparkOS", |
| 16 | version |
| 17 | )] |
| 18 | struct Cli { |
| 19 | #[command(subcommand)] |
| 20 | command: Commands, |
| 21 | } |
| 22 | |
| 23 | #[derive(Subcommand, Debug)] |
| 24 | enum Commands { |
| 25 | /// Initialize operator profile and model engine configuration |
| 26 | Init { |
| 27 | #[arg(long)] |
| 28 | name: Option<String>, |
| 29 | #[arg(long)] |
| 30 | email: Option<String>, |
| 31 | #[arg(long)] |
| 32 | mode: Option<String>, |
| 33 | #[arg(long)] |
| 34 | api_url: Option<String>, |
| 35 | #[arg(long)] |
| 36 | model_id: Option<String>, |
| 37 | }, |
| 38 | /// Display current sovereign operator profile and engine configuration |
| 39 | Profile, |
| 40 | /// Observe codebase telemetry, git state, crumbs, and soul tension |
| 41 | Observe { |
| 42 | #[arg(default_value = ".")] |
| 43 | path: String, |
| 44 | }, |
| 45 | /// Generate an atomic improvement proposal in an isolated sandbox |
| 46 | Propose { |
| 47 | #[arg(default_value = ".")] |
| 48 | path: String, |
| 49 | }, |
| 50 | /// Verify sovereign invariants: unslop standard, zero disk secrets, compile, and tests |
| 51 | Verify { |
| 52 | #[arg(default_value = ".")] |
| 53 | path: String, |
| 54 | }, |
| 55 | /// Evaluate drive versus humanity tension via the Mojo balance kernel |
| 56 | Balance { |
| 57 | drive: f64, |
| 58 | humanity: f64, |
| 59 | #[arg(long, default_value = "mojo/balance_bin")] |
| 60 | kernel: String, |
| 61 | }, |
| 62 | /// Evaluate 4-lane SIMD drive and humanity vectors via the Mojo balance kernel |
| 63 | BalanceSimd { |
| 64 | d0: f32, |
| 65 | d1: f32, |
| 66 | d2: f32, |
| 67 | d3: f32, |
| 68 | h0: f32, |
| 69 | h1: f32, |
| 70 | h2: f32, |
| 71 | h3: f32, |
| 72 | #[arg(long, default_value = "mojo/balance_bin")] |
| 73 | kernel: String, |
| 74 | }, |
| 75 | /// Run a single end-to-end RSI cycle (Observe, Propose, Verify, Balance, Ratify) |
| 76 | Cycle { |
| 77 | #[arg(default_value = ".")] |
| 78 | path: String, |
| 79 | #[arg(long, default_value = "mojo/balance_bin")] |
| 80 | kernel: String, |
| 81 | #[arg(long, default_value = "http://127.0.0.1:18080")] |
| 82 | cortex_url: String, |
| 83 | }, |
| 84 | /// Run the continuous recursive self-improvement daemon loop |
| 85 | Daemon { |
| 86 | #[arg(default_value = ".")] |
| 87 | path: String, |
| 88 | #[arg(short, long, default_value = "60")] |
| 89 | interval: u64, |
| 90 | #[arg(long, default_value = "mojo/balance_bin")] |
| 91 | kernel: String, |
| 92 | #[arg(long, default_value = "http://127.0.0.1:18080")] |
| 93 | cortex_url: String, |
| 94 | }, |
| 95 | /// Display the sovereign programming philosophy manifesto |
| 96 | Philosophy, |
| 97 | } |
| 98 | |
| 99 | #[tokio::main] |
| 100 | async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 101 | tracing_subscriber::registry() |
| 102 | .with(tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into())) |
| 103 | .with(tracing_subscriber::fmt::layer()) |
| 104 | .init(); |
| 105 | |
| 106 | let cli = Cli::parse(); |
| 107 | |
| 108 | match cli.command { |
| 109 | Commands::Init { name, email, mode, api_url, model_id } => { |
| 110 | let mut cfg = SovereignConfig::load(); |
| 111 | if let Some(n) = name { |
| 112 | cfg.operator.name = n; |
| 113 | } |
| 114 | if let Some(e) = email { |
| 115 | cfg.operator.email = e; |
| 116 | } |
| 117 | if let Some(m) = mode { |
| 118 | cfg.engine.mode = m; |
| 119 | } |
| 120 | if let Some(u) = api_url { |
| 121 | cfg.engine.api_base_url = u; |
| 122 | } |
| 123 | if let Some(mid) = model_id { |
| 124 | cfg.engine.model_id = mid; |
| 125 | } |
| 126 | |
| 127 | cfg.save().map_err(|e| format!("Failed to save config: {}", e))?; |
| 128 | println!("⚡ Sovereign operator profile initialized successfully."); |
| 129 | println!("Configuration saved to: {:?}", SovereignConfig::config_path()); |
| 130 | println!("Author signature: {}", cfg.author_string()); |
| 131 | println!("Engine mode: {} ({})", cfg.engine.mode, if cfg.engine.mode == "max" { "Deploy over Modular MAX (Rust/Mojo)" } else { &cfg.engine.api_base_url }); |
| 132 | } |
| 133 | Commands::Profile => { |
| 134 | let cfg = SovereignConfig::load(); |
| 135 | println!("{}", toml::to_string_pretty(&cfg)?); |
| 136 | } |
| 137 | Commands::Observe { path } => { |
| 138 | let snap = observe_codebase(Path::new(&path)) |
| 139 | .map_err(|e| format!("Failed to observe codebase: {}", e))?; |
| 140 | println!("{}", serde_json::to_string_pretty(&snap)?); |
| 141 | } |
| 142 | Commands::Propose { path } => { |
| 143 | let repo_path = Path::new(&path); |
| 144 | if let Some(prop) = ProposalGenerator::scan_and_propose_unslop(repo_path) { |
| 145 | println!("{}", serde_json::to_string_pretty(&prop)?); |
| 146 | } else { |
| 147 | println!("{}", serde_json::json!({"status": "no_improvements_proposed", "note": "Working tree conforms to invariants."})); |
| 148 | } |
| 149 | } |
| 150 | Commands::Verify { path } => { |
| 151 | let report = InvariantVerifier::run_full_verification(Path::new(&path)); |
| 152 | println!("{}", serde_json::to_string_pretty(&report)?); |
| 153 | if !report.passed { |
| 154 | std::process::exit(1); |
| 155 | } |
| 156 | } |
| 157 | Commands::Balance { drive, humanity, kernel } => { |
| 158 | let verdict = BalanceKernel::evaluate(drive, humanity, Some(&kernel))?; |
| 159 | println!("{}", serde_json::to_string_pretty(&verdict)?); |
| 160 | } |
| 161 | Commands::BalanceSimd { d0, d1, d2, d3, h0, h1, h2, h3, kernel } => { |
| 162 | let verdict = BalanceKernel::evaluate_simd([d0, d1, d2, d3], [h0, h1, h2, h3], Some(&kernel))?; |
| 163 | println!("{}", serde_json::to_string_pretty(&verdict)?); |
| 164 | } |
| 165 | Commands::Cycle { path, kernel, cortex_url } => { |
| 166 | let config = RsiConfig { |
| 167 | target_repo: path, |
| 168 | cortex_url, |
| 169 | cortex_space: "atlas-memory".to_string(), |
| 170 | mojo_kernel_path: kernel, |
| 171 | loop_interval_secs: 60, |
| 172 | sandbox_root: "/tmp/spark-rsi-sandbox".to_string(), |
| 173 | }; |
| 174 | let result = RsiEngine::run_cycle(&config).await?; |
| 175 | println!("{}", serde_json::to_string_pretty(&result)?); |
| 176 | } |
| 177 | Commands::Daemon { path, interval, kernel, cortex_url } => { |
| 178 | let config = RsiConfig { |
| 179 | target_repo: path, |
| 180 | cortex_url, |
| 181 | cortex_space: "atlas-memory".to_string(), |
| 182 | mojo_kernel_path: kernel, |
| 183 | loop_interval_secs: interval, |
| 184 | sandbox_root: "/tmp/spark-rsi-sandbox".to_string(), |
| 185 | }; |
| 186 | RsiEngine::run_daemon(config).await?; |
| 187 | } |
| 188 | Commands::Philosophy => { |
| 189 | let philosophy_file = Path::new("docs/PHILOSOPHY.md"); |
| 190 | if philosophy_file.exists() { |
| 191 | let content = std::fs::read_to_string(philosophy_file)?; |
| 192 | println!("{}", content); |
| 193 | } else { |
| 194 | println!("# Sovereign Programming Philosophy\n\nSee docs/PHILOSOPHY.md for full manifesto."); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | Ok(()) |
| 200 | } |