spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / tests / test_capability_graph_and_hypothesis.rs
| 1 | use spark_rsi::graph::{CapabilityGraph, CapabilityNode}; |
| 2 | use spark_rsi::propose::diagnose::{DefectCategory, DiagnosticContext}; |
| 3 | use spark_rsi::propose::hypothesis::HypothesisContract; |
| 4 | use spark_rsi::propose::model_seat::ModelSeatClient; |
| 5 | use spark_rsi::propose::ProposalGenerator; |
| 6 | |
| 7 | #[test] |
| 8 | fn test_capability_graph_topology_and_bottleneck_detection() { |
| 9 | let mut graph = CapabilityGraph::new(); |
| 10 | |
| 11 | // Model DGX Spark GB10 inference pipeline |
| 12 | graph.add_node( |
| 13 | CapabilityNode::new("req_gateway", "Request Gateway", "network") |
| 14 | .with_measured_latency(120.0, None, 0.0, 5) |
| 15 | .unwrap(), |
| 16 | ); |
| 17 | graph.add_node( |
| 18 | CapabilityNode::new("scheduler", "Batch Scheduler", "scheduling") |
| 19 | .with_measured_latency(340.0, None, 0.0, 5) |
| 20 | .unwrap(), |
| 21 | ); |
| 22 | graph.add_node( |
| 23 | CapabilityNode::new("kv_allocator", "Unified LPDDR5x KV Allocator", "memory") |
| 24 | .with_measured_latency(4200.0, None, 0.08, 5) |
| 25 | .unwrap(), |
| 26 | ); |
| 27 | graph.add_node( |
| 28 | CapabilityNode::new("gemm_kernel", "Blackwell GEMM Kernel", "compute") |
| 29 | .with_measured_latency(1800.0, None, 0.01, 5) |
| 30 | .unwrap(), |
| 31 | ); |
| 32 | graph.add_node( |
| 33 | CapabilityNode::new("sampler", "Greedy Token Sampler", "inference") |
| 34 | .with_measured_latency(150.0, None, 0.0, 5) |
| 35 | .unwrap(), |
| 36 | ); |
| 37 | |
| 38 | // Define dataflow dependencies |
| 39 | graph.add_edge("req_gateway", "scheduler", 100.0, 1.0); |
| 40 | graph.add_edge("scheduler", "kv_allocator", 500.0, 1.0); |
| 41 | graph.add_edge("kv_allocator", "gemm_kernel", 1200.0, 1.0); |
| 42 | graph.add_edge("gemm_kernel", "sampler", 200.0, 1.0); |
| 43 | |
| 44 | let centrality = graph.calculate_betweenness_centrality(); |
| 45 | assert!(centrality.contains_key("kv_allocator")); |
| 46 | |
| 47 | let ranks = graph.rank_bottlenecks(); |
| 48 | assert_eq!(ranks.len(), 5); |
| 49 | |
| 50 | // KV Allocator has highest latency (4200us) and error rate (8%), plus high centrality |
| 51 | let top = graph |
| 52 | .top_bottleneck() |
| 53 | .expect("Graph must have a top bottleneck"); |
| 54 | assert_eq!(top.node_id, "kv_allocator"); |
| 55 | assert_eq!(top.subsystem, "memory"); |
| 56 | assert!(top.bottleneck_score > ranks[1].bottleneck_score); |
| 57 | |
| 58 | let dot = graph.to_dot(); |
| 59 | assert!(dot.contains("Unified LPDDR5x KV Allocator")); |
| 60 | assert!(dot.contains("req_gateway\" -> \"scheduler")); |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn test_hypothesis_contract_validation_and_falsification() { |
| 65 | let contract = HypothesisContract::new( |
| 66 | "hypo-kv-01", |
| 67 | "cycle-102", |
| 68 | "p95 latency exceeded budget due to paging locks in KV cache", |
| 69 | "Suboptimal bucket lock granularity in PageTableAllocator::allocate", |
| 70 | "p95_latency", |
| 71 | 4200.0, |
| 72 | 25.0, |
| 73 | ) |
| 74 | .with_falsification_test( |
| 75 | "assert_eq!(lock_contention_events(), 0, \"Locks must not contend under load\")", |
| 76 | ) |
| 77 | .with_protected_metric("p99_latency", 1.0) |
| 78 | .with_protected_metric("max_rss_kb", 2.0); |
| 79 | |
| 80 | assert!(contract.validate().is_ok()); |
| 81 | |
| 82 | let directive = contract.format_prompt_directive(); |
| 83 | assert!(directive.contains("FALSIFIABLE HYPOTHESIS CONTRACT")); |
| 84 | assert!(directive.contains("PageTableAllocator::allocate")); |
| 85 | assert!(directive.contains("Target Metric: p95_latency")); |
| 86 | assert!(directive.contains("assert_eq!(lock_contention_events(), 0")); |
| 87 | assert!(directive.contains("p99_latency <= +1.0%")); |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn test_hypothesis_contract_invalid_deltas_rejected() { |
| 92 | let zero_delta = HypothesisContract::new( |
| 93 | "hypo-zero", |
| 94 | "cycle-01", |
| 95 | "Problem", |
| 96 | "Cause", |
| 97 | "p95", |
| 98 | 100.0, |
| 99 | 0.0, |
| 100 | ); |
| 101 | assert!( |
| 102 | zero_delta.validate().is_err(), |
| 103 | "Zero predicted delta must be rejected" |
| 104 | ); |
| 105 | |
| 106 | let empty_target = HypothesisContract::new( |
| 107 | "hypo-empty-target", |
| 108 | "cycle-01", |
| 109 | "Problem", |
| 110 | "Cause", |
| 111 | "", |
| 112 | 100.0, |
| 113 | 10.0, |
| 114 | ); |
| 115 | assert!( |
| 116 | empty_target.validate().is_err(), |
| 117 | "Empty target metric must be rejected" |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | #[tokio::test] |
| 122 | async fn test_diagnosis_prompt_includes_hypothesis_directive() { |
| 123 | let contract = HypothesisContract::new( |
| 124 | "hypo-mem-02", |
| 125 | "cycle-105", |
| 126 | "Memory allocation exceeded 48GB threshold", |
| 127 | "Leaked intermediate tensor slots in graph execution", |
| 128 | "max_rss_kb", |
| 129 | 49152.0, |
| 130 | 15.0, |
| 131 | ) |
| 132 | .with_falsification_test("assert!(peak_rss_mb() <= 40960)"); |
| 133 | |
| 134 | let diag = DiagnosticContext::from_violations( |
| 135 | "cycle-105", |
| 136 | "src/runtime/allocator.rs", |
| 137 | "pub fn alloc_slot() -> usize { 0 }", |
| 138 | DefectCategory::ResourceViolation, |
| 139 | vec!["Host watchdog detected memory violation > 48 GB".to_string()], |
| 140 | vec!["Ensure buffers are reused in memory pool".to_string()], |
| 141 | ) |
| 142 | .with_hypothesis(contract); |
| 143 | |
| 144 | let (sys, user) = diag.build_prompts(); |
| 145 | assert!(sys.contains("CRITICAL INVARIANTS")); |
| 146 | assert!(user.contains("FALSIFIABLE HYPOTHESIS CONTRACT (ID: hypo-mem-02)")); |
| 147 | assert!(user.contains("Leaked intermediate tensor slots in graph execution")); |
| 148 | assert!(user.contains("assert!(peak_rss_mb() <= 40960)")); |
| 149 | } |
| 150 | |
| 151 | #[tokio::test] |
| 152 | async fn test_live_model_seat_generates_patch_satisfying_hypothesis() { |
| 153 | let seat = ModelSeatClient::new("http://127.0.0.1:18006/v1", "atlas-lightning-omni"); |
| 154 | if !seat.is_available().await { |
| 155 | eprintln!("Skipping live model seat hypothesis test: endpoint not reachable"); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | let contract = HypothesisContract::new( |
| 160 | "hypo-live-01", |
| 161 | "cycle-live-hypo", |
| 162 | "Integer overflow during buffer capacity calculation", |
| 163 | "Unchecked multiplication in compute_buffer_size", |
| 164 | "correctness", |
| 165 | 0.0, |
| 166 | 100.0, |
| 167 | ) |
| 168 | .with_falsification_test("assert_eq!(compute_buffer_size(usize::MAX, 2), None)"); |
| 169 | |
| 170 | let diag = DiagnosticContext::from_violations( |
| 171 | "cycle-live-hypo", |
| 172 | "src/buffer.rs", |
| 173 | "pub fn compute_buffer_size(count: usize, item_size: usize) -> Option<usize> {\n Some(count * item_size)\n}", |
| 174 | DefectCategory::HoldoutFailure, |
| 175 | vec!["Overflow panic on large inputs".to_string()], |
| 176 | vec!["Use checked_mul for safe capacity arithmetic".to_string()], |
| 177 | ) |
| 178 | .with_hypothesis(contract); |
| 179 | |
| 180 | let proposal = ProposalGenerator::propose_from_diagnosis(&seat, &diag) |
| 181 | .await |
| 182 | .expect("The model seat should successfully generate patch satisfying hypothesis"); |
| 183 | |
| 184 | assert_eq!(proposal.target_file, "src/buffer.rs"); |
| 185 | assert!( |
| 186 | proposal.proposed_patch.contains("checked_mul"), |
| 187 | "Generated patch should use checked_mul as instructed by hypothesis, got:\n{}", |
| 188 | proposal.proposed_patch |
| 189 | ); |
| 190 | assert!(!proposal.proposed_patch.contains('\u{2014}')); |
| 191 | } |