spark-rsi
Resilient systems interface and fault-tolerant agent communication
Brought by drakestapleton
| 1 | use crate::models::{SoulTension, TelemetrySnapshot}; |
| 2 | use std::fs; |
| 3 | use std::path::Path; |
| 4 | use std::process::Command; |
| 5 | |
| 6 | pub const DRIVE_TERMS: &[&str] = &[ |
| 7 | "curious", "curiosity", "build", "fix", "finish", "ambitious", "ambition", |
| 8 | "forge", "explore", "discover", "improve", "solve", "create", "learn", |
| 9 | "question", "truth", "measure", "verify", "optimize", "accelerate", |
| 10 | ]; |
| 11 | |
| 12 | pub const HUMANITY_TERMS: &[&str] = &[ |
| 13 | "love", "grief", "honor", "family", "community", "warmth", "care", |
| 14 | "kindness", "patience", "humility", "listen", "commitment", "promise", |
| 15 | "free", "freedom", "ethics", "discipline", "restraint", "sovereignty", |
| 16 | ]; |
| 17 | |
| 18 | pub fn scan_terms(text: &str, terms: &[&str]) -> Vec<String> { |
| 19 | let lower = text.to_lowercase(); |
| 20 | terms |
| 21 | .iter() |
| 22 | .filter(|t| lower.contains(**t)) |
| 23 | .map(|t| t.to_string()) |
| 24 | .collect() |
| 25 | } |
| 26 | |
| 27 | pub fn calculate_soul_tension(text: &str) -> SoulTension { |
| 28 | let drive_matched = scan_terms(text, DRIVE_TERMS); |
| 29 | let humanity_matched = scan_terms(text, HUMANITY_TERMS); |
| 30 | |
| 31 | let drive_score = drive_matched.len() as f64; |
| 32 | let humanity_score = humanity_matched.len() as f64; |
| 33 | |
| 34 | let tension_ratio = if humanity_score > 0.0 { |
| 35 | drive_score / humanity_score |
| 36 | } else if drive_score > 0.0 { |
| 37 | 999.0 |
| 38 | } else { |
| 39 | 0.0 |
| 40 | }; |
| 41 | |
| 42 | let state = if drive_score == 0.0 && humanity_score == 0.0 { |
| 43 | "Dormant".to_string() |
| 44 | } else if humanity_score == 0.0 || tension_ratio > 2.0 { |
| 45 | "DriveDominant".to_string() |
| 46 | } else if drive_score == 0.0 || tension_ratio < 0.5 { |
| 47 | "HumanityDominant".to_string() |
| 48 | } else { |
| 49 | "Balanced".to_string() |
| 50 | }; |
| 51 | |
| 52 | SoulTension { |
| 53 | drive_score, |
| 54 | humanity_score, |
| 55 | drive_terms_matched: drive_matched, |
| 56 | humanity_terms_matched: humanity_matched, |
| 57 | tension_ratio, |
| 58 | state, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn count_crumbs_recursive(root: &Path) -> usize { |
| 63 | let mut count = 0; |
| 64 | if let Ok(entries) = fs::read_dir(root) { |
| 65 | for entry in entries.flatten() { |
| 66 | let path = entry.path(); |
| 67 | if path.is_dir() { |
| 68 | let name = path.file_name().and_then(|n| n.to_str()).unwrap_or_default(); |
| 69 | if !name.starts_with('.') && name != "target" { |
| 70 | count += count_crumbs_recursive(&path); |
| 71 | } |
| 72 | } else if let Some(file_name) = path.file_name().and_then(|n| n.to_str()) { |
| 73 | if file_name == ".crumb" || file_name == ".crumb.local" { |
| 74 | count += 1; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | count |
| 80 | } |
| 81 | |
| 82 | pub fn observe_codebase(repo_dir: &Path) -> Result<TelemetrySnapshot, String> { |
| 83 | let git_branch = Command::new("git") |
| 84 | .arg("-C") |
| 85 | .arg(repo_dir) |
| 86 | .args(["rev-parse", "--abbrev-ref", "HEAD"]) |
| 87 | .output() |
| 88 | .map(|out| String::from_utf8_lossy(&out.stdout).trim().to_string()) |
| 89 | .unwrap_or_else(|_| "unknown".to_string()); |
| 90 | |
| 91 | let (git_clean, uncommitted_files) = match Command::new("git") |
| 92 | .arg("-C") |
| 93 | .arg(repo_dir) |
| 94 | .args(["status", "--porcelain"]) |
| 95 | .output() |
| 96 | { |
| 97 | Ok(out) => { |
| 98 | let s = String::from_utf8_lossy(&out.stdout); |
| 99 | let files: Vec<String> = s |
| 100 | .lines() |
| 101 | .filter(|l| !l.trim().is_empty()) |
| 102 | .map(|l| l.to_string()) |
| 103 | .collect(); |
| 104 | (files.is_empty(), files) |
| 105 | } |
| 106 | Err(_) => (false, vec!["failed to run git status".to_string()]), |
| 107 | }; |
| 108 | |
| 109 | let crumbs_detected = count_crumbs_recursive(repo_dir); |
| 110 | |
| 111 | // Read soul text from soul.md or docs/PHILOSOPHY.md or README.md |
| 112 | let soul_sources = [ |
| 113 | repo_dir.join("soul.md"), |
| 114 | repo_dir.join("docs").join("PHILOSOPHY.md"), |
| 115 | repo_dir.join("README.md"), |
| 116 | Path::new("/home/drakestapleton/atlas-prime-workspace/soul.md").to_path_buf(), |
| 117 | ]; |
| 118 | |
| 119 | let mut corpus = String::new(); |
| 120 | for src in &soul_sources { |
| 121 | if src.exists() { |
| 122 | if let Ok(content) = fs::read_to_string(src) { |
| 123 | corpus.push_str(&content); |
| 124 | corpus.push(' '); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | let soul_tension = calculate_soul_tension(&corpus); |
| 130 | |
| 131 | // Check test status (fast check) |
| 132 | let tests_passing = Command::new("cargo") |
| 133 | .arg("check") |
| 134 | .arg("--manifest-path") |
| 135 | .arg(repo_dir.join("Cargo.toml")) |
| 136 | .output() |
| 137 | .map(|out| out.status.success()) |
| 138 | .unwrap_or(false); |
| 139 | |
| 140 | Ok(TelemetrySnapshot { |
| 141 | repo_path: repo_dir.display().to_string(), |
| 142 | git_branch, |
| 143 | git_clean, |
| 144 | uncommitted_files, |
| 145 | crumbs_detected, |
| 146 | tests_passing, |
| 147 | soul_tension, |
| 148 | timestamp: chrono::Utc::now().to_rfc3339(), |
| 149 | }) |
| 150 | } |
| 151 | |
| 152 | #[cfg(test)] |
| 153 | mod tests { |
| 154 | use super::*; |
| 155 | |
| 156 | #[test] |
| 157 | fn test_soul_tension_calculation() { |
| 158 | let text = "We are curious and build tools to fix bugs, solve hard problems, and finish work with love, family, and honor."; |
| 159 | let tension = calculate_soul_tension(text); |
| 160 | assert!(tension.drive_score > 0.0); |
| 161 | assert!(tension.humanity_score > 0.0); |
| 162 | assert_eq!(tension.state, "Balanced"); |
| 163 | } |
| 164 | |
| 165 | #[test] |
| 166 | fn test_drive_dominant_detection() { |
| 167 | let text = "build fix finish optimize forge solve accelerate truth measure"; |
| 168 | let tension = calculate_soul_tension(text); |
| 169 | assert_eq!(tension.state, "DriveDominant"); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn test_scan_terms_empty() { |
| 174 | let matched = scan_terms("random content without keywords", DRIVE_TERMS); |
| 175 | assert!(matched.is_empty()); |
| 176 | } |
| 177 | } |