spark-rsi
Resilient systems interface and fault-tolerant agent communication
Brought by drakestapleton
| 1 | use crate::balance::BalanceKernel; |
| 2 | use crate::models::{RsiConfig, RsiCycleResult}; |
| 3 | use crate::observe::observe_codebase; |
| 4 | use crate::propose::ProposalGenerator; |
| 5 | use crate::ratify::Ratifier; |
| 6 | use crate::verifier::InvariantVerifier; |
| 7 | use std::path::Path; |
| 8 | use std::time::Instant; |
| 9 | use uuid::Uuid; |
| 10 | |
| 11 | pub struct RsiEngine; |
| 12 | |
| 13 | impl RsiEngine { |
| 14 | pub async fn run_cycle(config: &RsiConfig) -> Result<RsiCycleResult, String> { |
| 15 | let start = Instant::now(); |
| 16 | let cycle_id = format!("cycle-{}", Uuid::new_v4().simple()); |
| 17 | let repo_path = Path::new(&config.target_repo); |
| 18 | |
| 19 | // 1. Observe |
| 20 | let telemetry = observe_codebase(repo_path)?; |
| 21 | |
| 22 | // 2. Propose (check if an unslop sanitization or optimization proposal is needed) |
| 23 | let mut maybe_proposal = ProposalGenerator::scan_and_propose_unslop(repo_path); |
| 24 | |
| 25 | let mut maybe_invariants = None; |
| 26 | let maybe_balance; |
| 27 | let mut maybe_ratification = None; |
| 28 | let mut success = true; |
| 29 | |
| 30 | if let Some(ref mut proposal) = maybe_proposal { |
| 31 | let sandbox_base = Path::new(&config.sandbox_root); |
| 32 | let sandbox_dir = ProposalGenerator::stage_in_sandbox(proposal, repo_path, sandbox_base)?; |
| 33 | |
| 34 | // 3. Verify Invariants in isolated sandbox |
| 35 | let inv_report = InvariantVerifier::run_full_verification(&sandbox_dir); |
| 36 | let passed = inv_report.passed; |
| 37 | maybe_invariants = Some(inv_report.clone()); |
| 38 | |
| 39 | // 4. Balance: evaluate soul tension via Mojo kernel |
| 40 | let balance_verdict = BalanceKernel::evaluate( |
| 41 | telemetry.soul_tension.drive_score, |
| 42 | telemetry.soul_tension.humanity_score, |
| 43 | Some(&config.mojo_kernel_path), |
| 44 | )?; |
| 45 | let is_balanced = balance_verdict.verdict == "balanced"; |
| 46 | maybe_balance = Some(balance_verdict); |
| 47 | |
| 48 | // 5. Ratify: if invariants passed and balance is maintained |
| 49 | if passed && is_balanced { |
| 50 | let rat = Ratifier::ratify_proposal( |
| 51 | proposal, |
| 52 | &inv_report, |
| 53 | repo_path, |
| 54 | &config.cortex_url, |
| 55 | &config.cortex_space, |
| 56 | ) |
| 57 | .await?; |
| 58 | maybe_ratification = Some(rat); |
| 59 | } else { |
| 60 | success = false; |
| 61 | } |
| 62 | } else { |
| 63 | // Still run balance check on current state |
| 64 | let balance_verdict = BalanceKernel::evaluate( |
| 65 | telemetry.soul_tension.drive_score, |
| 66 | telemetry.soul_tension.humanity_score, |
| 67 | Some(&config.mojo_kernel_path), |
| 68 | )?; |
| 69 | maybe_balance = Some(balance_verdict); |
| 70 | } |
| 71 | |
| 72 | let elapsed_ms = start.elapsed().as_secs_f64() * 1000.0; |
| 73 | |
| 74 | Ok(RsiCycleResult { |
| 75 | cycle_id, |
| 76 | telemetry, |
| 77 | proposal: maybe_proposal, |
| 78 | invariants: maybe_invariants, |
| 79 | balance: maybe_balance, |
| 80 | ratification: maybe_ratification, |
| 81 | success, |
| 82 | elapsed_ms, |
| 83 | }) |
| 84 | } |
| 85 | |
| 86 | pub async fn run_daemon(config: RsiConfig) -> Result<(), String> { |
| 87 | tracing::info!( |
| 88 | "Starting continuous RSI daemon for repository {:?} with interval {}s", |
| 89 | config.target_repo, |
| 90 | config.loop_interval_secs |
| 91 | ); |
| 92 | |
| 93 | loop { |
| 94 | match Self::run_cycle(&config).await { |
| 95 | Ok(res) => { |
| 96 | tracing::info!( |
| 97 | "RSI cycle {} completed in {:.2}ms (success: {}, proposed: {})", |
| 98 | res.cycle_id, |
| 99 | res.elapsed_ms, |
| 100 | res.success, |
| 101 | res.proposal.is_some() |
| 102 | ); |
| 103 | } |
| 104 | Err(e) => { |
| 105 | tracing::error!("RSI cycle failed with error: {}", e); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | tokio::time::sleep(tokio::time::Duration::from_secs(config.loop_interval_secs)).await; |
| 110 | } |
| 111 | } |
| 112 | } |