spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / src / propose / diagnose.rs
| 1 | use crate::evaluator::EvaluationReceipt; |
| 2 | use crate::propose::hypothesis::HypothesisContract; |
| 3 | use serde::{Deserialize, Serialize}; |
| 4 | |
| 5 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 6 | pub enum DefectCategory { |
| 7 | CompilationFailure, |
| 8 | HoldoutFailure, |
| 9 | InvariantViolation, |
| 10 | PerformanceRegression, |
| 11 | ResourceViolation, |
| 12 | StyleViolation, |
| 13 | SoulTensionDominance, |
| 14 | } |
| 15 | |
| 16 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 17 | pub struct DiagnosticContext { |
| 18 | pub cycle_id: String, |
| 19 | pub target_file: String, |
| 20 | pub current_content: String, |
| 21 | pub primary_defect: DefectCategory, |
| 22 | pub violations: Vec<String>, |
| 23 | pub past_lessons: Vec<String>, |
| 24 | #[serde(default)] |
| 25 | pub hypothesis: Option<HypothesisContract>, |
| 26 | } |
| 27 | |
| 28 | impl DiagnosticContext { |
| 29 | pub fn from_receipt( |
| 30 | cycle_id: &str, |
| 31 | receipt: &EvaluationReceipt, |
| 32 | target_file: &str, |
| 33 | current_content: &str, |
| 34 | past_lessons: Vec<String>, |
| 35 | ) -> Self { |
| 36 | let mut violations = Vec::new(); |
| 37 | let mut primary_defect = DefectCategory::HoldoutFailure; |
| 38 | |
| 39 | for layer in &receipt.layer_results { |
| 40 | if !layer.passed { |
| 41 | match layer.layer_name.as_str() { |
| 42 | "Correctness" => { |
| 43 | primary_defect = DefectCategory::CompilationFailure; |
| 44 | } |
| 45 | "Security" => { |
| 46 | primary_defect = DefectCategory::InvariantViolation; |
| 47 | } |
| 48 | "Style" => { |
| 49 | primary_defect = DefectCategory::StyleViolation; |
| 50 | } |
| 51 | "Performance" => { |
| 52 | primary_defect = DefectCategory::PerformanceRegression; |
| 53 | } |
| 54 | "ResourceEfficiency" => { |
| 55 | primary_defect = DefectCategory::ResourceViolation; |
| 56 | } |
| 57 | "LongitudinalReplay" => { |
| 58 | primary_defect = DefectCategory::HoldoutFailure; |
| 59 | } |
| 60 | _ => {} |
| 61 | } |
| 62 | for v in &layer.violations { |
| 63 | violations.push(format!("[{}] {}", layer.layer_name, v)); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if violations.is_empty() && !receipt.admitted { |
| 69 | violations.push( |
| 70 | "Evaluator rejected candidate without explicit layer violations.".to_string(), |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | Self { |
| 75 | cycle_id: cycle_id.to_string(), |
| 76 | target_file: target_file.to_string(), |
| 77 | current_content: current_content.to_string(), |
| 78 | primary_defect, |
| 79 | violations, |
| 80 | past_lessons, |
| 81 | hypothesis: None, |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | pub fn from_violations( |
| 86 | cycle_id: &str, |
| 87 | target_file: &str, |
| 88 | current_content: &str, |
| 89 | primary_defect: DefectCategory, |
| 90 | violations: Vec<String>, |
| 91 | past_lessons: Vec<String>, |
| 92 | ) -> Self { |
| 93 | Self { |
| 94 | cycle_id: cycle_id.to_string(), |
| 95 | target_file: target_file.to_string(), |
| 96 | current_content: current_content.to_string(), |
| 97 | primary_defect, |
| 98 | violations, |
| 99 | past_lessons, |
| 100 | hypothesis: None, |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | pub fn with_hypothesis(mut self, hypothesis: HypothesisContract) -> Self { |
| 105 | self.hypothesis = Some(hypothesis); |
| 106 | self |
| 107 | } |
| 108 | |
| 109 | pub fn build_prompts(&self) -> (String, String) { |
| 110 | let system_prompt = "You are the autonomous recursive code synthesizer for spark-rsi on NVIDIA DGX Spark.\n\ |
| 111 | Your role: generate a clean, compiling, verified replacement for the target file that resolves diagnosed defects.\n\ |
| 112 | \n\ |
| 113 | CRITICAL INVARIANTS:\n\ |
| 114 | 1. Output MUST contain ONLY the complete replacement file inside a single fenced code block (e.g. ```rust ... ```).\n\ |
| 115 | 2. Do NOT provide conversational filler, pleasantries, explanations, or text outside the code block.\n\ |
| 116 | 3. Anti-Slop Standard: ZERO em dashes (\\u{2014}), ZERO en dashes (\\u{2013}), ZERO marketing adjectives or decorative adjectives.\n\ |
| 117 | 4. Preserve existing public APIs and invariants.\n\ |
| 118 | 5. Be concise. Keep internal reasoning brief and emit the complete replacement file inside a single fenced code block.".to_string(); |
| 119 | |
| 120 | let mut user_prompt = format!( |
| 121 | "TARGET FILE: {}\n\ |
| 122 | PRIMARY DEFECT: {:?}\n\ |
| 123 | \n\ |
| 124 | DIAGNOSTIC VIOLATIONS DETECTED BY EVALUATOR:\n", |
| 125 | self.target_file, self.primary_defect |
| 126 | ); |
| 127 | |
| 128 | if self.violations.is_empty() { |
| 129 | user_prompt.push_str( |
| 130 | "- No explicit violations recorded; optimize code for robustness and efficiency.\n", |
| 131 | ); |
| 132 | } else { |
| 133 | for v in &self.violations { |
| 134 | user_prompt.push_str(&format!("- {}\n", v)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if let Some(ref h) = self.hypothesis { |
| 139 | user_prompt.push_str(&format!("\n{}\n", h.format_prompt_directive())); |
| 140 | } |
| 141 | |
| 142 | if !self.past_lessons.is_empty() { |
| 143 | user_prompt.push_str("\nCANONICAL LESSONS FROM SPARK CORTEX (ATLAS-MEMORY):\n"); |
| 144 | for lesson in &self.past_lessons { |
| 145 | user_prompt.push_str(&format!("* {}\n", lesson)); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | user_prompt.push_str(&format!( |
| 150 | "\nCURRENT FILE CONTENT:\n```\n{}\n```\n\nGenerate the updated file resolving the above defects:", |
| 151 | self.current_content |
| 152 | )); |
| 153 | |
| 154 | (system_prompt, user_prompt) |
| 155 | } |
| 156 | } |