spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / tests / test_model_seat_inference_and_diagnosis.rs
| 1 | use spark_rsi::evaluator::layers::LayerResult; |
| 2 | use spark_rsi::evaluator::EvaluationReceipt; |
| 3 | use spark_rsi::models::ProposalKind; |
| 4 | use spark_rsi::propose::cortex::CortexExperienceClient; |
| 5 | use spark_rsi::propose::diagnose::{DefectCategory, DiagnosticContext}; |
| 6 | use spark_rsi::propose::model_seat::{ChatMessage, ModelSeatClient}; |
| 7 | use spark_rsi::propose::ProposalGenerator; |
| 8 | |
| 9 | #[tokio::test] |
| 10 | async fn test_model_seat_availability_and_completion() { |
| 11 | let client = ModelSeatClient::new("http://127.0.0.1:18006/v1", "atlas-lightning-omni"); |
| 12 | let is_avail = client.is_available().await; |
| 13 | if !is_avail { |
| 14 | eprintln!( |
| 15 | "Skipping live model seat availability test: service not reachable on port 18006" |
| 16 | ); |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | let messages = vec![ |
| 21 | ChatMessage { |
| 22 | role: "system".to_string(), |
| 23 | content: "You are a code synthesis test engine. Respond with: PONG".to_string(), |
| 24 | }, |
| 25 | ChatMessage { |
| 26 | role: "user".to_string(), |
| 27 | content: "ping".to_string(), |
| 28 | }, |
| 29 | ]; |
| 30 | |
| 31 | let resp = client |
| 32 | .complete(&messages, 256, 0.0) |
| 33 | .await |
| 34 | .expect("Model seat completion failed"); |
| 35 | assert!( |
| 36 | resp.contains("PONG"), |
| 37 | "Expected PONG in completion, got: {}", |
| 38 | resp |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn test_code_block_extraction() { |
| 44 | let raw = "Here is the repaired file:\n```rust\npub fn fixed_logic() -> bool {\n true\n}\n```\nAll done."; |
| 45 | let extracted = ProposalGenerator::extract_code_block(raw); |
| 46 | assert_eq!(extracted, "pub fn fixed_logic() -> bool {\n true\n}"); |
| 47 | |
| 48 | let plain = "pub fn simple() -> bool { false }"; |
| 49 | assert_eq!(ProposalGenerator::extract_code_block(plain), plain); |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | fn test_diagnostic_context_from_failing_receipt() { |
| 54 | let receipt = EvaluationReceipt { |
| 55 | cycle_id: "cycle-test-diag".to_string(), |
| 56 | candidate_id: "cand-test-diag".to_string(), |
| 57 | parent_id: "parent-test".to_string(), |
| 58 | evaluated_at: chrono::Utc::now().to_rfc3339(), |
| 59 | evaluator_version: "0.1.0".to_string(), |
| 60 | passed_all_hard_invariants: false, |
| 61 | passed_statistical_gates: false, |
| 62 | admitted: false, |
| 63 | layer_results: vec![ |
| 64 | LayerResult { |
| 65 | layer_name: "Correctness".to_string(), |
| 66 | is_hard_invariant: true, |
| 67 | passed: false, |
| 68 | score: 0.0, |
| 69 | summary: "Compilation failed".to_string(), |
| 70 | violations: vec!["E0308: mismatched types".to_string()], |
| 71 | }, |
| 72 | LayerResult { |
| 73 | layer_name: "Style".to_string(), |
| 74 | is_hard_invariant: true, |
| 75 | passed: false, |
| 76 | score: 0.0, |
| 77 | summary: "Style violation".to_string(), |
| 78 | violations: vec!["Em dash detected at line 14".to_string()], |
| 79 | }, |
| 80 | ], |
| 81 | metrics_summary: None, |
| 82 | receipt_digest: "abcd1234".to_string(), |
| 83 | signature: None, |
| 84 | format_version: 1, |
| 85 | binding: None, |
| 86 | }; |
| 87 | |
| 88 | let target_file = "src/example.rs"; |
| 89 | let content = "pub fn example() {}"; |
| 90 | let lessons = vec!["Avoid em dashes in comments".to_string()]; |
| 91 | |
| 92 | let diag = |
| 93 | DiagnosticContext::from_receipt("cycle-test-diag", &receipt, target_file, content, lessons); |
| 94 | |
| 95 | assert_eq!(diag.target_file, "src/example.rs"); |
| 96 | assert_eq!(diag.primary_defect, DefectCategory::StyleViolation); |
| 97 | assert_eq!(diag.violations.len(), 2); |
| 98 | assert_eq!(diag.past_lessons.len(), 1); |
| 99 | |
| 100 | let (sys, user) = diag.build_prompts(); |
| 101 | assert!(sys.contains("CRITICAL INVARIANTS")); |
| 102 | assert!(sys.contains("Anti-Slop")); |
| 103 | assert!(user.contains("TARGET FILE: src/example.rs")); |
| 104 | assert!(user.contains("E0308: mismatched types")); |
| 105 | assert!(user.contains("Em dash detected at line 14")); |
| 106 | assert!(user.contains("Avoid em dashes in comments")); |
| 107 | } |
| 108 | |
| 109 | #[tokio::test] |
| 110 | async fn test_cortex_experience_client_query() { |
| 111 | let cortex = CortexExperienceClient::new("http://127.0.0.1:18080"); |
| 112 | let lessons = cortex.recall_lessons("spark-rsi", 3).await; |
| 113 | for l in &lessons { |
| 114 | assert!(!l.is_empty()); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | #[tokio::test] |
| 119 | async fn test_propose_from_diagnosis_live_model_seat() { |
| 120 | let seat = ModelSeatClient::new("http://127.0.0.1:18006/v1", "atlas-lightning-omni"); |
| 121 | if !seat.is_available().await { |
| 122 | eprintln!("Skipping live model seat test: service not running"); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | let diag = DiagnosticContext::from_violations( |
| 127 | "cycle-diag-live", |
| 128 | "src/calc.rs", |
| 129 | "pub fn add(a: i32, b: i32) -> i32 {\n a - b\n}", |
| 130 | DefectCategory::HoldoutFailure, |
| 131 | vec!["Holdout 'ADD-001' failed: expected 5, got -1".to_string()], |
| 132 | vec!["Verify operator signs in arithmetic functions".to_string()], |
| 133 | ); |
| 134 | |
| 135 | let proposal = ProposalGenerator::propose_from_diagnosis(&seat, &diag) |
| 136 | .await |
| 137 | .expect("Failed to generate proposal from diagnosis"); |
| 138 | |
| 139 | assert_eq!(proposal.target_file, "src/calc.rs"); |
| 140 | assert_eq!(proposal.kind, ProposalKind::InvariantFix); |
| 141 | assert!(!proposal.proposed_patch.is_empty()); |
| 142 | // Ensure no forbidden unicode dashes |
| 143 | assert!(!proposal.proposed_patch.contains('\u{2014}')); |
| 144 | assert!(!proposal.proposed_patch.contains('\u{2013}')); |
| 145 | } |