spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / src / soak / manifest.rs
| 1 | use chrono::Utc; |
| 2 | use serde::{Deserialize, Serialize}; |
| 3 | use sha2::Digest; |
| 4 | use std::collections::{HashMap, HashSet}; |
| 5 | use std::path::Path; |
| 6 | use std::process::Command; |
| 7 | |
| 8 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 9 | pub struct ThermalSnapshot { |
| 10 | pub timestamp: String, |
| 11 | pub gpu_temp_c: Option<f32>, |
| 12 | pub power_draw_w: Option<f32>, |
| 13 | pub gpu_util_pct: Option<f32>, |
| 14 | pub graphics_clock_mhz: Option<u32>, |
| 15 | pub memory_clock_mhz: Option<u32>, |
| 16 | } |
| 17 | |
| 18 | impl ThermalSnapshot { |
| 19 | pub fn capture() -> Option<Self> { |
| 20 | let output = Command::new("nvidia-smi") |
| 21 | .args([ |
| 22 | "--query-gpu=temperature.gpu,power.draw,utilization.gpu,clocks.current.graphics,clocks.current.memory", |
| 23 | "--format=csv,noheader,nounits", |
| 24 | ]) |
| 25 | .output() |
| 26 | .ok()?; |
| 27 | |
| 28 | if !output.status.success() { |
| 29 | return None; |
| 30 | } |
| 31 | |
| 32 | let raw = String::from_utf8_lossy(&output.stdout); |
| 33 | let first_line = raw.lines().next()?; |
| 34 | let parts: Vec<&str> = first_line.split(',').map(|s| s.trim()).collect(); |
| 35 | if parts.len() < 5 { |
| 36 | return None; |
| 37 | } |
| 38 | |
| 39 | Some(Self { |
| 40 | timestamp: Utc::now().to_rfc3339(), |
| 41 | gpu_temp_c: parts[0].parse::<f32>().ok(), |
| 42 | power_draw_w: parts[1].parse::<f32>().ok(), |
| 43 | gpu_util_pct: parts[2].parse::<f32>().ok(), |
| 44 | graphics_clock_mhz: parts[3].parse::<u32>().ok(), |
| 45 | memory_clock_mhz: parts[4].parse::<u32>().ok(), |
| 46 | }) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 51 | pub struct EngineNSnapshot { |
| 52 | pub commit_sha: String, |
| 53 | pub evaluator_version: String, |
| 54 | pub holdout_bundle_digest: String, |
| 55 | pub builder_digest: String, |
| 56 | /// Records written before 2026-10-15 named this `max_model`; that key is still read. |
| 57 | #[serde(alias = "max_model")] |
| 58 | pub model_id: String, |
| 59 | pub cortex_snapshot_ref: String, |
| 60 | pub operator_verifying_key: Option<String>, |
| 61 | } |
| 62 | |
| 63 | #[derive(Debug, Clone, Serialize, Deserialize, Default)] |
| 64 | pub struct HypothesisQuarantineTracker { |
| 65 | pub failure_counts: HashMap<String, usize>, |
| 66 | pub exhausted_pairings: HashSet<String>, |
| 67 | } |
| 68 | |
| 69 | impl HypothesisQuarantineTracker { |
| 70 | pub fn new() -> Self { |
| 71 | Self { |
| 72 | failure_counts: HashMap::new(), |
| 73 | exhausted_pairings: HashSet::new(), |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fn pairing_key(node_name: &str, problem_summary: &str) -> String { |
| 78 | let mut hasher = sha2::Sha256::new(); |
| 79 | hasher.update(problem_summary.as_bytes()); |
| 80 | let digest = hasher.finalize(); |
| 81 | format!("{}:{}", node_name, &hex::encode(digest)[..12]) |
| 82 | } |
| 83 | |
| 84 | pub fn is_exhausted(&self, node_name: &str, problem_summary: &str) -> bool { |
| 85 | let key = Self::pairing_key(node_name, problem_summary); |
| 86 | self.exhausted_pairings.contains(&key) |
| 87 | } |
| 88 | |
| 89 | pub fn record_failure( |
| 90 | &mut self, |
| 91 | node_name: &str, |
| 92 | problem_summary: &str, |
| 93 | _reason: &str, |
| 94 | ) -> bool { |
| 95 | let key = Self::pairing_key(node_name, problem_summary); |
| 96 | let count = self.failure_counts.entry(key.clone()).or_insert(0); |
| 97 | *count += 1; |
| 98 | if *count >= 3 { |
| 99 | self.exhausted_pairings.insert(key); |
| 100 | true |
| 101 | } else { |
| 102 | false |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | pub fn record_success(&mut self, node_name: &str, problem_summary: &str) { |
| 107 | let key = Self::pairing_key(node_name, problem_summary); |
| 108 | self.failure_counts.remove(&key); |
| 109 | self.exhausted_pairings.remove(&key); |
| 110 | } |
| 111 | |
| 112 | pub fn exhausted_list(&self) -> Vec<String> { |
| 113 | let mut list: Vec<String> = self.exhausted_pairings.iter().cloned().collect(); |
| 114 | list.sort(); |
| 115 | list |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 120 | pub struct CycleCandidateRecord { |
| 121 | pub candidate_id: String, |
| 122 | pub candidate_index: usize, |
| 123 | pub target_file: String, |
| 124 | pub proposed_patch_digest: String, |
| 125 | pub compilation_passed: bool, |
| 126 | pub invariants_passed: bool, |
| 127 | pub judge_admitted: bool, |
| 128 | pub primary_metric_name: String, |
| 129 | pub primary_metric_value: f64, |
| 130 | pub rejection_reason: Option<String>, |
| 131 | pub ledger_block_hash: Option<String>, |
| 132 | } |
| 133 | |
| 134 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 135 | pub struct CycleRecord { |
| 136 | pub cycle_index: usize, |
| 137 | pub cycle_id: String, |
| 138 | pub bottleneck_node: String, |
| 139 | pub hypothesis_id: String, |
| 140 | pub hypothesis_problem: String, |
| 141 | pub candidates: Vec<CycleCandidateRecord>, |
| 142 | pub winning_candidate_id: Option<String>, |
| 143 | pub merkle_checkpoint_hash: Option<String>, |
| 144 | pub thermal_before: Option<ThermalSnapshot>, |
| 145 | pub thermal_after: Option<ThermalSnapshot>, |
| 146 | pub cortex_receipt_id: Option<String>, |
| 147 | pub status: String, |
| 148 | } |
| 149 | |
| 150 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 151 | pub struct AdmittedMetaCandidate { |
| 152 | pub candidate_id: String, |
| 153 | pub ledger_block_hash: String, |
| 154 | pub staging_dir: String, |
| 155 | pub patch_digest: String, |
| 156 | pub criteria_1_novel_discovery: bool, |
| 157 | pub criteria_2_self_capability_gain: bool, |
| 158 | pub classification: String, |
| 159 | } |
| 160 | |
| 161 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 162 | pub struct SoakRunManifest { |
| 163 | pub manifest_version: String, |
| 164 | pub run_id: String, |
| 165 | pub started_at: String, |
| 166 | pub completed_at: String, |
| 167 | pub engine_n_snapshot: EngineNSnapshot, |
| 168 | pub cycles: Vec<CycleRecord>, |
| 169 | pub admitted_candidates: Vec<AdmittedMetaCandidate>, |
| 170 | pub exhausted_hypotheses: Vec<String>, |
| 171 | pub final_merkle_checkpoint: Option<String>, |
| 172 | pub total_candidates_evaluated: usize, |
| 173 | pub total_candidates_admitted: usize, |
| 174 | pub preflight_passed: bool, |
| 175 | pub overall_status: String, |
| 176 | } |
| 177 | |
| 178 | impl SoakRunManifest { |
| 179 | pub fn save_to_file(&self, path: &Path) -> Result<(), String> { |
| 180 | let json = serde_json::to_string_pretty(self) |
| 181 | .map_err(|e| format!("Failed to serialize manifest: {}", e))?; |
| 182 | if let Some(parent) = path.parent() { |
| 183 | std::fs::create_dir_all(parent) |
| 184 | .map_err(|e| format!("Failed to create manifest directory: {}", e))?; |
| 185 | } |
| 186 | std::fs::write(path, json) |
| 187 | .map_err(|e| format!("Failed to write manifest to {:?}: {}", path, e)) |
| 188 | } |
| 189 | } |