spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / examples / seed_ledger.rs
| 1 | use p256::ecdsa::SigningKey; |
| 2 | use spark_rsi::evaluator::{EvaluationMetricsSummary, EvaluationReceipt, LayerResult}; |
| 3 | use spark_rsi::ledger::{BlockType, ImprovementLedger, PromotionEvidencePayload}; |
| 4 | use std::fs; |
| 5 | use std::path::Path; |
| 6 | |
| 7 | fn make_layer(name: &str, hard: bool, passed: bool, score: f64, summary: &str) -> LayerResult { |
| 8 | LayerResult { |
| 9 | layer_name: name.to_string(), |
| 10 | is_hard_invariant: hard, |
| 11 | passed, |
| 12 | score, |
| 13 | summary: summary.to_string(), |
| 14 | violations: if passed { |
| 15 | Vec::new() |
| 16 | } else { |
| 17 | vec![summary.to_string()] |
| 18 | }, |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 23 | let rsi_root = Path::new(".rsi"); |
| 24 | let eval_out_dir = rsi_root.join("eval_outputs"); |
| 25 | fs::create_dir_all(&eval_out_dir)?; |
| 26 | |
| 27 | let ledger = ImprovementLedger::open(rsi_root)?; |
| 28 | println!( |
| 29 | "Opened ledger. Current blocks: {}", |
| 30 | ledger.all_blocks()?.len() |
| 31 | ); |
| 32 | |
| 33 | let signing_key = SigningKey::from_bytes(&[42u8; 32].into())?; |
| 34 | |
| 35 | // Cycle 1: cand-kernel-opt-01 (Admitted) |
| 36 | let receipt1 = EvaluationReceipt { |
| 37 | cycle_id: "cycle-001".to_string(), |
| 38 | candidate_id: "cand-kernel-opt-01".to_string(), |
| 39 | parent_id: "parent-genesis-00".to_string(), |
| 40 | evaluated_at: "2026-09-19T10:00:00Z".to_string(), |
| 41 | evaluator_version: "1.0.0".to_string(), |
| 42 | passed_all_hard_invariants: true, |
| 43 | passed_statistical_gates: true, |
| 44 | admitted: true, |
| 45 | layer_results: vec![ |
| 46 | make_layer("correctness", true, true, 1.0, "All holdout tests passed"), |
| 47 | make_layer("security", true, true, 1.0, "No leaks, sandbox intact"), |
| 48 | make_layer("style", true, true, 1.0, "Clean unslop text"), |
| 49 | make_layer("performance", false, true, 1.0, "Bootstrap delta -6.8%"), |
| 50 | make_layer("resource_efficiency", false, true, 1.0, "RSS growth 0.2%"), |
| 51 | make_layer( |
| 52 | "longitudinal_replay", |
| 53 | true, |
| 54 | true, |
| 55 | 1.0, |
| 56 | "Zero regression on prior cycles", |
| 57 | ), |
| 58 | ], |
| 59 | metrics_summary: Some(EvaluationMetricsSummary { |
| 60 | latency_delta_pct: -6.8, |
| 61 | p_value: 0.0004, |
| 62 | p95_ci_upper_degradation_pct: 0.15, |
| 63 | p99_ci_upper_degradation_pct: 0.35, |
| 64 | rss_growth_pct: 0.2, |
| 65 | candidate_resident_mb: 410, |
| 66 | }), |
| 67 | receipt_digest: "digest-cycle-001".to_string(), |
| 68 | signature: None, |
| 69 | format_version: 1, |
| 70 | binding: None, |
| 71 | }; |
| 72 | receipt1.save_to_file(&eval_out_dir.join("cycle-001.json"))?; |
| 73 | let raw1 = serde_json::to_vec(&receipt1)?; |
| 74 | ledger.append_evaluation(&receipt1, Some(&raw1))?; |
| 75 | ledger.append_block( |
| 76 | BlockType::Promotion, |
| 77 | "{\"candidate_id\": \"cand-kernel-opt-01\", \"state\": \"DURABLE\", \"canary_quota\": 5000}" |
| 78 | .to_string(), |
| 79 | vec![], |
| 80 | )?; |
| 81 | |
| 82 | // Cycle 2: cand-bottleneck-sch-02 (Admitted) |
| 83 | let receipt2 = EvaluationReceipt { |
| 84 | cycle_id: "cycle-002".to_string(), |
| 85 | candidate_id: "cand-bottleneck-sch-02".to_string(), |
| 86 | parent_id: "cand-kernel-opt-01".to_string(), |
| 87 | evaluated_at: "2026-09-19T11:00:00Z".to_string(), |
| 88 | evaluator_version: "1.0.0".to_string(), |
| 89 | passed_all_hard_invariants: true, |
| 90 | passed_statistical_gates: true, |
| 91 | admitted: true, |
| 92 | layer_results: vec![ |
| 93 | make_layer("correctness", true, true, 1.0, "Passed 14 holdouts"), |
| 94 | make_layer("security", true, true, 1.0, "No host leakage"), |
| 95 | make_layer("style", true, true, 1.0, "Verified declarative proof"), |
| 96 | make_layer("performance", false, true, 1.0, "Bootstrap delta -11.4%"), |
| 97 | make_layer("resource_efficiency", false, true, 1.0, "RSS growth 0.1%"), |
| 98 | make_layer("longitudinal_replay", true, true, 1.0, "Pass"), |
| 99 | ], |
| 100 | metrics_summary: Some(EvaluationMetricsSummary { |
| 101 | latency_delta_pct: -11.4, |
| 102 | p_value: 0.0001, |
| 103 | p95_ci_upper_degradation_pct: -0.42, |
| 104 | p99_ci_upper_degradation_pct: -0.21, |
| 105 | rss_growth_pct: 0.1, |
| 106 | candidate_resident_mb: 412, |
| 107 | }), |
| 108 | receipt_digest: "digest-cycle-002".to_string(), |
| 109 | signature: None, |
| 110 | format_version: 1, |
| 111 | binding: None, |
| 112 | }; |
| 113 | receipt2.save_to_file(&eval_out_dir.join("cycle-002.json"))?; |
| 114 | let raw2 = serde_json::to_vec(&receipt2)?; |
| 115 | ledger.append_evaluation(&receipt2, Some(&raw2))?; |
| 116 | ledger.append_block(BlockType::Promotion, "{\"candidate_id\": \"cand-bottleneck-sch-02\", \"state\": \"DURABLE\", \"canary_quota\": 5000}".to_string(), vec![])?; |
| 117 | |
| 118 | // Cycle 3: cand-memleak-03 (Rejected: Resource Efficiency) |
| 119 | let receipt3 = EvaluationReceipt { |
| 120 | cycle_id: "cycle-003".to_string(), |
| 121 | candidate_id: "cand-memleak-03".to_string(), |
| 122 | parent_id: "cand-bottleneck-sch-02".to_string(), |
| 123 | evaluated_at: "2026-09-19T11:45:00Z".to_string(), |
| 124 | evaluator_version: "1.0.0".to_string(), |
| 125 | passed_all_hard_invariants: true, |
| 126 | passed_statistical_gates: false, |
| 127 | admitted: false, |
| 128 | layer_results: vec![ |
| 129 | make_layer("correctness", true, true, 1.0, "Passed"), |
| 130 | make_layer("security", true, true, 1.0, "Passed"), |
| 131 | make_layer("style", true, true, 1.0, "Passed"), |
| 132 | make_layer("performance", false, true, 1.0, "Pass"), |
| 133 | make_layer( |
| 134 | "resource_efficiency", |
| 135 | false, |
| 136 | false, |
| 137 | 0.0, |
| 138 | "RSS growth +42.0% exceeded limit 5.0%", |
| 139 | ), |
| 140 | make_layer("longitudinal_replay", true, true, 1.0, "Pass"), |
| 141 | ], |
| 142 | metrics_summary: Some(EvaluationMetricsSummary { |
| 143 | latency_delta_pct: -3.2, |
| 144 | p_value: 0.02, |
| 145 | p95_ci_upper_degradation_pct: 0.45, |
| 146 | p99_ci_upper_degradation_pct: 0.65, |
| 147 | rss_growth_pct: 42.0, |
| 148 | candidate_resident_mb: 585, |
| 149 | }), |
| 150 | receipt_digest: "digest-cycle-003".to_string(), |
| 151 | signature: None, |
| 152 | format_version: 1, |
| 153 | binding: None, |
| 154 | }; |
| 155 | receipt3.save_to_file(&eval_out_dir.join("cycle-003.json"))?; |
| 156 | let raw3 = serde_json::to_vec(&receipt3)?; |
| 157 | ledger.append_evaluation(&receipt3, Some(&raw3))?; |
| 158 | ledger.append_block(BlockType::Rollback, "{\"candidate_id\": \"cand-memleak-03\", \"reason\": \"ResourceEfficiencyViolation: RSS growth +42%\"}".to_string(), vec![])?; |
| 159 | |
| 160 | // Cycle 4: cand-unslop-04 (Rejected: Style Layer) |
| 161 | let receipt4 = EvaluationReceipt { |
| 162 | cycle_id: "cycle-004".to_string(), |
| 163 | candidate_id: "cand-unslop-04".to_string(), |
| 164 | parent_id: "cand-bottleneck-sch-02".to_string(), |
| 165 | evaluated_at: "2026-09-19T12:15:00Z".to_string(), |
| 166 | evaluator_version: "1.0.0".to_string(), |
| 167 | passed_all_hard_invariants: false, |
| 168 | passed_statistical_gates: true, |
| 169 | admitted: false, |
| 170 | layer_results: vec![ |
| 171 | make_layer("correctness", true, true, 1.0, "Passed"), |
| 172 | make_layer("security", true, true, 1.0, "Passed"), |
| 173 | make_layer( |
| 174 | "style", |
| 175 | true, |
| 176 | false, |
| 177 | 0.0, |
| 178 | "Unslop violation: em-dash found in docstring", |
| 179 | ), |
| 180 | make_layer("performance", false, true, 1.0, "Pass"), |
| 181 | make_layer("resource_efficiency", false, true, 1.0, "Pass"), |
| 182 | make_layer("longitudinal_replay", true, true, 1.0, "Pass"), |
| 183 | ], |
| 184 | metrics_summary: Some(EvaluationMetricsSummary { |
| 185 | latency_delta_pct: -5.0, |
| 186 | p_value: 0.001, |
| 187 | p95_ci_upper_degradation_pct: 0.1, |
| 188 | p99_ci_upper_degradation_pct: 0.2, |
| 189 | rss_growth_pct: 0.3, |
| 190 | candidate_resident_mb: 413, |
| 191 | }), |
| 192 | receipt_digest: "digest-cycle-004".to_string(), |
| 193 | signature: None, |
| 194 | format_version: 1, |
| 195 | binding: None, |
| 196 | }; |
| 197 | receipt4.save_to_file(&eval_out_dir.join("cycle-004.json"))?; |
| 198 | let raw4 = serde_json::to_vec(&receipt4)?; |
| 199 | ledger.append_evaluation(&receipt4, Some(&raw4))?; |
| 200 | ledger.append_block(BlockType::Rollback, "{\"candidate_id\": \"cand-unslop-04\", \"reason\": \"HardInvariantFailed: Style unslop violation\"}".to_string(), vec![])?; |
| 201 | |
| 202 | // Cycle 5: cand-holdout-fail-05 (Rejected: Correctness) |
| 203 | let receipt5 = EvaluationReceipt { |
| 204 | cycle_id: "cycle-005".to_string(), |
| 205 | candidate_id: "cand-holdout-fail-05".to_string(), |
| 206 | parent_id: "cand-bottleneck-sch-02".to_string(), |
| 207 | evaluated_at: "2026-09-19T12:45:00Z".to_string(), |
| 208 | evaluator_version: "1.0.0".to_string(), |
| 209 | passed_all_hard_invariants: false, |
| 210 | passed_statistical_gates: true, |
| 211 | admitted: false, |
| 212 | layer_results: vec![ |
| 213 | make_layer( |
| 214 | "correctness", |
| 215 | true, |
| 216 | false, |
| 217 | 0.0, |
| 218 | "Holdout suite 02_edge_cases failed: output mismatch", |
| 219 | ), |
| 220 | make_layer("security", true, true, 1.0, "Passed"), |
| 221 | make_layer("style", true, true, 1.0, "Passed"), |
| 222 | make_layer("performance", false, true, 1.0, "Pass"), |
| 223 | make_layer("resource_efficiency", false, true, 1.0, "Pass"), |
| 224 | make_layer("longitudinal_replay", true, true, 1.0, "Pass"), |
| 225 | ], |
| 226 | metrics_summary: Some(EvaluationMetricsSummary { |
| 227 | latency_delta_pct: -15.0, |
| 228 | p_value: 0.0001, |
| 229 | p95_ci_upper_degradation_pct: -0.5, |
| 230 | p99_ci_upper_degradation_pct: -0.3, |
| 231 | rss_growth_pct: 0.1, |
| 232 | candidate_resident_mb: 412, |
| 233 | }), |
| 234 | receipt_digest: "digest-cycle-005".to_string(), |
| 235 | signature: None, |
| 236 | format_version: 1, |
| 237 | binding: None, |
| 238 | }; |
| 239 | receipt5.save_to_file(&eval_out_dir.join("cycle-005.json"))?; |
| 240 | let raw5 = serde_json::to_vec(&receipt5)?; |
| 241 | ledger.append_evaluation(&receipt5, Some(&raw5))?; |
| 242 | ledger.append_block(BlockType::Rollback, "{\"candidate_id\": \"cand-holdout-fail-05\", \"reason\": \"CorrectnessFailure: blind holdout suite failed\"}".to_string(), vec![])?; |
| 243 | |
| 244 | // Checkpoint 1 |
| 245 | let cp1 = ledger.checkpoint(Some(&signing_key))?; |
| 246 | println!( |
| 247 | "Checkpoint 1 created up to seq {}: Merkle Root {}", |
| 248 | cp1.up_to_sequence, cp1.merkle_root |
| 249 | ); |
| 250 | |
| 251 | // Cycle 6: cand-kv-cache-simd-06 (Admitted) |
| 252 | let receipt6 = EvaluationReceipt { |
| 253 | cycle_id: "cycle-006".to_string(), |
| 254 | candidate_id: "cand-kv-cache-simd-06".to_string(), |
| 255 | parent_id: "cand-bottleneck-sch-02".to_string(), |
| 256 | evaluated_at: "2026-09-19T13:30:00Z".to_string(), |
| 257 | evaluator_version: "1.0.0".to_string(), |
| 258 | passed_all_hard_invariants: true, |
| 259 | passed_statistical_gates: true, |
| 260 | admitted: true, |
| 261 | layer_results: vec![ |
| 262 | make_layer("correctness", true, true, 1.0, "Passed 14 holdouts"), |
| 263 | make_layer("security", true, true, 1.0, "Passed"), |
| 264 | make_layer("style", true, true, 1.0, "Passed"), |
| 265 | make_layer("performance", false, true, 1.0, "Bootstrap delta -18.2%"), |
| 266 | make_layer("resource_efficiency", false, true, 1.0, "RSS growth 0.05%"), |
| 267 | make_layer("longitudinal_replay", true, true, 1.0, "Pass"), |
| 268 | ], |
| 269 | metrics_summary: Some(EvaluationMetricsSummary { |
| 270 | latency_delta_pct: -18.2, |
| 271 | p_value: 0.00001, |
| 272 | p95_ci_upper_degradation_pct: -1.15, |
| 273 | p99_ci_upper_degradation_pct: -0.92, |
| 274 | rss_growth_pct: 0.05, |
| 275 | candidate_resident_mb: 414, |
| 276 | }), |
| 277 | receipt_digest: "digest-cycle-006".to_string(), |
| 278 | signature: None, |
| 279 | format_version: 1, |
| 280 | binding: None, |
| 281 | }; |
| 282 | receipt6.save_to_file(&eval_out_dir.join("cycle-006.json"))?; |
| 283 | let raw6 = serde_json::to_vec(&receipt6)?; |
| 284 | ledger.append_evaluation(&receipt6, Some(&raw6))?; |
| 285 | ledger.append_block(BlockType::Promotion, "{\"candidate_id\": \"cand-kv-cache-simd-06\", \"state\": \"DURABLE\", \"canary_quota\": 5000}".to_string(), vec![])?; |
| 286 | |
| 287 | // Cycle 7: cand-max-context-diag-07 (Admitted) |
| 288 | let receipt7 = EvaluationReceipt { |
| 289 | cycle_id: "cycle-007".to_string(), |
| 290 | candidate_id: "cand-max-context-diag-07".to_string(), |
| 291 | parent_id: "cand-kv-cache-simd-06".to_string(), |
| 292 | evaluated_at: "2026-09-19T14:15:00Z".to_string(), |
| 293 | evaluator_version: "1.0.0".to_string(), |
| 294 | passed_all_hard_invariants: true, |
| 295 | passed_statistical_gates: true, |
| 296 | admitted: true, |
| 297 | layer_results: vec![ |
| 298 | make_layer("correctness", true, true, 1.0, "Passed"), |
| 299 | make_layer("security", true, true, 1.0, "Passed"), |
| 300 | make_layer("style", true, true, 1.0, "Passed"), |
| 301 | make_layer("performance", false, true, 1.0, "Bootstrap delta -8.5%"), |
| 302 | make_layer("resource_efficiency", false, true, 1.0, "Pass"), |
| 303 | make_layer("longitudinal_replay", true, true, 1.0, "Pass"), |
| 304 | ], |
| 305 | metrics_summary: Some(EvaluationMetricsSummary { |
| 306 | latency_delta_pct: -8.5, |
| 307 | p_value: 0.0002, |
| 308 | p95_ci_upper_degradation_pct: -0.35, |
| 309 | p99_ci_upper_degradation_pct: -0.15, |
| 310 | rss_growth_pct: 0.1, |
| 311 | candidate_resident_mb: 415, |
| 312 | }), |
| 313 | receipt_digest: "digest-cycle-007".to_string(), |
| 314 | signature: None, |
| 315 | format_version: 1, |
| 316 | binding: None, |
| 317 | }; |
| 318 | receipt7.save_to_file(&eval_out_dir.join("cycle-007.json"))?; |
| 319 | let raw7 = serde_json::to_vec(&receipt7)?; |
| 320 | ledger.append_evaluation(&receipt7, Some(&raw7))?; |
| 321 | ledger.append_block(BlockType::Promotion, "{\"candidate_id\": \"cand-max-context-diag-07\", \"state\": \"DURABLE\", \"canary_quota\": 5000}".to_string(), vec![])?; |
| 322 | |
| 323 | // Cycle 8: cand-meta-evaluator-08 (Tier 2 Meta Candidate) |
| 324 | let meta_block = ledger.append_block( |
| 325 | BlockType::Promotion, |
| 326 | "{\"candidate_id\": \"cand-meta-evaluator-08\", \"classification\": \"META_CANDIDATE\", \"tier\": 2, \"novel_discovery\": true, \"self_capability_delta_pct\": 7.4}".to_string(), |
| 327 | vec![], |
| 328 | )?; |
| 329 | |
| 330 | // Cycle 9: cand-downstream-opt-09 (Downstream Candidate benefiting from Gen 8) |
| 331 | let downstream_block = ledger.append_block( |
| 332 | BlockType::Evaluation, |
| 333 | "{\"cycle_id\": \"cycle-009\", \"candidate_id\": \"cand-downstream-opt-09\", \"parent_id\": \"cand-meta-evaluator-08\", \"delta_pct\": -14.1, \"discovery_rate\": 0.85}".to_string(), |
| 334 | vec![], |
| 335 | )?; |
| 336 | |
| 337 | // Cycle 10: PromotionEvidence Payload (True RSI Criterion 3 Proof) |
| 338 | let evidence_payload = PromotionEvidencePayload { |
| 339 | meta_candidate_block_hash: meta_block.block_hash.clone(), |
| 340 | downstream_cycle_id: "cycle-009".to_string(), |
| 341 | downstream_ledger_block_hash: downstream_block.block_hash.clone(), |
| 342 | capability_improvement_proof: "Downstream generation 9 utilized enhanced hypothesis generation primitive introduced in generation 8 to discover and repair bottleneck with 14.1% latency improvement".to_string(), |
| 343 | final_classification: "TRUE_RSI".to_string(), |
| 344 | }; |
| 345 | ledger.append_promotion_evidence(&evidence_payload)?; |
| 346 | |
| 347 | // Checkpoint 2 |
| 348 | let cp2 = ledger.checkpoint(Some(&signing_key))?; |
| 349 | println!( |
| 350 | "Checkpoint 2 created up to seq {}: Merkle Root {}", |
| 351 | cp2.up_to_sequence, cp2.merkle_root |
| 352 | ); |
| 353 | |
| 354 | let audit = ledger.verify_chain_integrity(None)?; |
| 355 | println!( |
| 356 | "Final chain audit: valid={}, total_blocks={}, total_blobs={}", |
| 357 | audit.chain_valid, audit.total_blocks, audit.total_blobs |
| 358 | ); |
| 359 | Ok(()) |
| 360 | } |