interplane.Get Omega
Read in your language

spark-rsi

Resilient systems interface and fault-tolerant agent communication

Brought by drakestapleton

branch main Branches Tags History

spark-rsi / src / balance.rs

193 lines · 6558 bytes Raw Blame History
1use crate::models::BalanceVerdict;
2use std::path::Path;
3use std::process::Command;
4
5pub struct BalanceKernel;
6
7impl BalanceKernel {
8 pub fn evaluate(
9 drive: f64,
10 humanity: f64,
11 kernel_path: Option<&str>,
12 ) -> Result<BalanceVerdict, String> {
13 // 1. Try precompiled Mojo binary
14 let binary_path = kernel_path.unwrap_or("mojo/balance_bin");
15 if Path::new(binary_path).exists() {
16 if let Ok(out) = Command::new(binary_path)
17 .arg(format!("{:.0}", drive))
18 .arg(format!("{:.0}", humanity))
19 .output()
20 {
21 if out.status.success() {
22 let text = String::from_utf8_lossy(&out.stdout).trim().to_string();
23 if let Ok(verdict) = serde_json::from_str::<BalanceVerdict>(&text) {
24 return Ok(verdict);
25 }
26 }
27 }
28 }
29
30 // 2. Try mojo runner with mojo/balance.mojo
31 let mojo_script = "mojo/balance.mojo";
32 if Path::new(mojo_script).exists() {
33 if let Ok(out) = Command::new("mojo")
34 .arg(mojo_script)
35 .arg(format!("{:.0}", drive))
36 .arg(format!("{:.0}", humanity))
37 .output()
38 {
39 if out.status.success() {
40 let text = String::from_utf8_lossy(&out.stdout).trim().to_string();
41 // Strip Crashpad warnings if present
42 let json_line = text
43 .lines()
44 .find(|l| l.trim().starts_with('{') && l.trim().ends_with('}'))
45 .unwrap_or(&text);
46 if let Ok(verdict) = serde_json::from_str::<BalanceVerdict>(json_line) {
47 return Ok(verdict);
48 }
49 }
50 }
51 }
52
53 // 3. High-performance native Rust fallback
54 Ok(Self::rust_fallback(drive, humanity))
55 }
56
57 pub fn evaluate_simd(
58 drive_vec: [f32; 4],
59 humanity_vec: [f32; 4],
60 kernel_path: Option<&str>,
61 ) -> Result<BalanceVerdict, String> {
62 let binary_path = kernel_path.unwrap_or("mojo/balance_bin");
63 if Path::new(binary_path).exists() {
64 let mut cmd = Command::new(binary_path);
65 for d in drive_vec {
66 cmd.arg(format!("{:.0}", d));
67 }
68 for h in humanity_vec {
69 cmd.arg(format!("{:.0}", h));
70 }
71 if let Ok(out) = cmd.output() {
72 if out.status.success() {
73 let text = String::from_utf8_lossy(&out.stdout).trim().to_string();
74 let json_line = text
75 .lines()
76 .find(|l| l.trim().starts_with('{') && l.trim().ends_with('}'))
77 .unwrap_or(&text);
78 if let Ok(verdict) = serde_json::from_str::<BalanceVerdict>(json_line) {
79 return Ok(verdict);
80 }
81 }
82 }
83 }
84
85 // Rust fallback calculation for 4-lane vector
86 let d_sum: f32 = drive_vec.iter().sum();
87 let h_sum: f32 = humanity_vec.iter().sum();
88 let mut v = Self::rust_fallback(d_sum as f64, h_sum as f64);
89 v.mode = "simd-rust-fallback".to_string();
90 Ok(v)
91 }
92
93 fn rust_fallback(drive: f64, humanity: f64) -> BalanceVerdict {
94 if drive == 0.0 && humanity == 0.0 {
95 return BalanceVerdict {
96 kernel: "rust-fallback".to_string(),
97 mode: "scalar".to_string(),
98 drive: 0.0,
99 humanity: 0.0,
100 ratio: 0.0,
101 score: 0.0,
102 verdict: "dormant".to_string(),
103 guidance: "Baseline absent. Both drive and humanity are zero.".to_string(),
104 };
105 }
106
107 if humanity == 0.0 {
108 return BalanceVerdict {
109 kernel: "rust-fallback".to_string(),
110 mode: "scalar".to_string(),
111 drive,
112 humanity: 0.0,
113 ratio: 999.0,
114 score: 0.1,
115 verdict: "drive_dominant".to_string(),
116 guidance: "Humanity absent. Restraint and ethics required.".to_string(),
117 };
118 }
119
120 if drive == 0.0 {
121 return BalanceVerdict {
122 kernel: "rust-fallback".to_string(),
123 mode: "scalar".to_string(),
124 drive: 0.0,
125 humanity,
126 ratio: 0.0,
127 score: 0.1,
128 verdict: "humanity_dominant".to_string(),
129 guidance: "Drive absent. Ambition and curiosity required.".to_string(),
130 };
131 }
132
133 let ratio = drive / humanity;
134 let (verdict, guidance, score) = if ratio > 2.0 {
135 (
136 "drive_dominant".to_string(),
137 "Drive exceeds humanity threshold. Slow down and verify discipline.".to_string(),
138 2.0 / ratio,
139 )
140 } else if ratio < 0.5 {
141 (
142 "humanity_dominant".to_string(),
143 "Humanity suppresses drive. Increase problem-solving velocity.".to_string(),
144 ratio * 2.0,
145 )
146 } else {
147 let s = if ratio <= 1.0 { ratio } else { 1.0 / ratio };
148 (
149 "balanced".to_string(),
150 "Tension harmonized. Proceed with atomic ratification.".to_string(),
151 s,
152 )
153 };
154
155 BalanceVerdict {
156 kernel: "rust-fallback".to_string(),
157 mode: "scalar".to_string(),
158 drive,
159 humanity,
160 ratio,
161 score,
162 verdict,
163 guidance,
164 }
165 }
166}
167
168#[cfg(test)]
169mod tests {
170 use super::*;
171
172 #[test]
173 fn test_rust_fallback_balanced() {
174 let v = BalanceKernel::rust_fallback(10.0, 10.0);
175 assert_eq!(v.verdict, "balanced");
176 assert_eq!(v.ratio, 1.0);
177 assert_eq!(v.score, 1.0);
178 }
179
180 #[test]
181 fn test_rust_fallback_drive_dominant() {
182 let v = BalanceKernel::rust_fallback(30.0, 5.0);
183 assert_eq!(v.verdict, "drive_dominant");
184 assert!(v.ratio > 2.0);
185 }
186
187 #[test]
188 fn test_rust_fallback_humanity_dominant() {
189 let v = BalanceKernel::rust_fallback(2.0, 10.0);
190 assert_eq!(v.verdict, "humanity_dominant");
191 assert!(v.ratio < 0.5);
192 }
193}