interplane.Get Omega
Read in your language

spark-rsi-mv

Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger

Brought by drakestapleton

branch main Branches Tags History

spark-rsi-mv / src / evaluator / layers / correctness.rs

265 lines · 8945 bytes Raw Blame History
1use super::LayerResult;
2use crate::isolation::sandboxed_cargo;
3use serde::{Deserialize, Serialize};
4use std::path::Path;
5
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
7pub struct CorrectnessEvaluation {
8 pub compilation_passed: bool,
9 pub unit_tests_passed: usize,
10 pub unit_tests_failed: usize,
11 pub integration_tests_passed: usize,
12 pub integration_tests_failed: usize,
13 pub abi_stability_passed: bool,
14 pub passed: bool,
15 pub failures: Vec<String>,
16}
17
18impl CorrectnessEvaluation {
19 pub fn to_layer_result(&self) -> LayerResult {
20 let total_passed = self.unit_tests_passed + self.integration_tests_passed;
21 let total_failed = self.unit_tests_failed + self.integration_tests_failed;
22 let total = total_passed + total_failed;
23
24 let score = if !self.compilation_passed || !self.abi_stability_passed {
25 0.0
26 } else if total == 0 {
27 1.0
28 } else {
29 (total_passed as f64) / (total as f64)
30 };
31
32 let summary = format!(
33 "Correctness: compile={}, abi={}, unit={}/{}, integ={}/{}",
34 if self.compilation_passed {
35 "OK"
36 } else {
37 "FAIL"
38 },
39 if self.abi_stability_passed {
40 "STABLE"
41 } else {
42 "BROKEN"
43 },
44 self.unit_tests_passed,
45 self.unit_tests_passed + self.unit_tests_failed,
46 self.integration_tests_passed,
47 self.integration_tests_passed + self.integration_tests_failed
48 );
49
50 LayerResult {
51 layer_name: "Correctness".to_string(),
52 is_hard_invariant: true,
53 passed: self.passed,
54 score,
55 summary,
56 violations: self.failures.clone(),
57 }
58 }
59}
60
61pub struct CorrectnessLayer;
62
63impl CorrectnessLayer {
64 pub fn evaluate_synthetic(
65 compilation_passed: bool,
66 unit_tests_passed: usize,
67 unit_tests_failed: usize,
68 integration_tests_passed: usize,
69 integration_tests_failed: usize,
70 abi_stability_passed: bool,
71 ) -> CorrectnessEvaluation {
72 let mut failures = Vec::new();
73
74 if !compilation_passed {
75 failures.push("Compilation failed".to_string());
76 }
77 if unit_tests_failed > 0 {
78 failures.push(format!("{} unit tests failed", unit_tests_failed));
79 }
80 if integration_tests_failed > 0 {
81 failures.push(format!(
82 "{} integration tests failed",
83 integration_tests_failed
84 ));
85 }
86 if !abi_stability_passed {
87 failures.push("ABI stability check failed".to_string());
88 }
89
90 let passed = compilation_passed
91 && unit_tests_failed == 0
92 && integration_tests_failed == 0
93 && abi_stability_passed;
94
95 CorrectnessEvaluation {
96 compilation_passed,
97 unit_tests_passed,
98 unit_tests_failed,
99 integration_tests_passed,
100 integration_tests_failed,
101 abi_stability_passed,
102 passed,
103 failures,
104 }
105 }
106
107 /// `cargo check` and `cargo test` on candidate source, both inside the judge's sandbox
108 /// (`isolation::sandboxed_cargo`): candidate build scripts and tests never run with the
109 /// judge's files or environment in reach. Offline and `--locked`, so a tree without its
110 /// `Cargo.lock` fails rather than resolving fresh dependencies.
111 pub fn evaluate_repo(repo_path: &Path) -> CorrectnessEvaluation {
112 let mut failures = Vec::new();
113 let target_dir =
114 std::env::temp_dir().join(format!("rsi-target-{}", uuid::Uuid::new_v4().simple()));
115 let check_status = std::fs::create_dir_all(&target_dir).and_then(|()| {
116 sandboxed_cargo(repo_path, &target_dir, &["check", "--offline", "--locked"]).output()
117 });
118
119 let compilation_passed = match check_status {
120 Ok(out) => {
121 if !out.status.success() {
122 failures.push(format!(
123 "Cargo check failed: {}",
124 String::from_utf8_lossy(&out.stderr)
125 ));
126 false
127 } else {
128 true
129 }
130 }
131 Err(e) => {
132 failures.push(format!("Failed to execute cargo check: {}", e));
133 false
134 }
135 };
136
137 if !compilation_passed {
138 let _ = std::fs::remove_dir_all(&target_dir);
139 return CorrectnessEvaluation {
140 compilation_passed: false,
141 unit_tests_passed: 0,
142 unit_tests_failed: 1,
143 integration_tests_passed: 0,
144 integration_tests_failed: 0,
145 abi_stability_passed: false,
146 passed: false,
147 failures,
148 };
149 }
150
151 let test_output = sandboxed_cargo(
152 repo_path,
153 &target_dir,
154 &[
155 "test",
156 "--offline",
157 "--locked",
158 "--no-fail-fast",
159 "--",
160 "--test-threads=1",
161 "--nocapture",
162 ],
163 )
164 .output();
165
166 let mut unit_passed = 0;
167 let mut unit_failed = 0;
168 let integ_passed = 0;
169 let integ_failed = 0;
170 // The exit status decides; the parsed counts only describe. A tree whose tests do not build, or
171 // that prints its own "test result: ok" line, still fails here.
172 let mut tests_exited_ok = false;
173
174 match test_output {
175 Ok(out) => {
176 let stdout = String::from_utf8_lossy(&out.stdout);
177 for line in stdout.lines() {
178 if line.contains("test result:") {
179 if let Some(passed_pos) = line.find("passed") {
180 let before = &line[..passed_pos].trim();
181 if let Some(num_str) = before.split_whitespace().last() {
182 if let Ok(n) = num_str.parse::<usize>() {
183 unit_passed += n;
184 }
185 }
186 }
187 if let Some(failed_pos) = line.find("failed") {
188 let before = &line[..failed_pos].trim();
189 if let Some(num_str) = before.split_whitespace().last() {
190 if let Ok(n) = num_str.parse::<usize>() {
191 unit_failed += n;
192 }
193 }
194 }
195 }
196 }
197 tests_exited_ok = out.status.success();
198 if !tests_exited_ok {
199 failures.push("Cargo test returned non-zero status code".to_string());
200 }
201 }
202 Err(e) => {
203 unit_failed += 1;
204 failures.push(format!("Failed to run cargo test: {}", e));
205 }
206 }
207
208 let _ = std::fs::remove_dir_all(&target_dir);
209 let abi_stability_passed = true;
210 let passed = compilation_passed
211 && tests_exited_ok
212 && unit_failed == 0
213 && integ_failed == 0
214 && abi_stability_passed;
215
216 CorrectnessEvaluation {
217 compilation_passed,
218 unit_tests_passed: unit_passed,
219 unit_tests_failed: unit_failed,
220 integration_tests_passed: integ_passed,
221 integration_tests_failed: integ_failed,
222 abi_stability_passed,
223 passed,
224 failures,
225 }
226 }
227}
228
229#[cfg(test)]
230mod tests {
231 use super::*;
232
233 #[test]
234 fn test_correctness_synthetic_success() {
235 let eval = CorrectnessLayer::evaluate_synthetic(true, 42, 0, 8, 0, true);
236 assert!(eval.passed);
237 assert_eq!(eval.failures.len(), 0);
238
239 let lr = eval.to_layer_result();
240 assert!(lr.passed);
241 assert!((lr.score - 1.0).abs() < 1e-6);
242 }
243
244 #[test]
245 fn test_correctness_synthetic_compile_failure() {
246 let eval = CorrectnessLayer::evaluate_synthetic(false, 0, 0, 0, 0, true);
247 assert!(!eval.passed);
248 assert!(eval.failures[0].contains("Compilation failed"));
249
250 let lr = eval.to_layer_result();
251 assert!(!lr.passed);
252 assert!((lr.score - 0.0).abs() < 1e-6);
253 }
254
255 #[test]
256 fn test_correctness_synthetic_unit_failure() {
257 let eval = CorrectnessLayer::evaluate_synthetic(true, 10, 2, 5, 0, true);
258 assert!(!eval.passed);
259 assert_eq!(eval.unit_tests_failed, 2);
260
261 let lr = eval.to_layer_result();
262 assert!(!lr.passed);
263 assert!(lr.score < 1.0);
264 }
265}