spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / tests / test_m1_measurement.rs
| 1 | //! M1 Measurement acceptance: stage samples from completed prior cycles drive the ranking. |
| 2 | |
| 3 | use spark_rsi::explain::{evidence_graph, MetricsOrigin, RsiCycleEvidence}; |
| 4 | use spark_rsi::graph::{Measurement, RankingBasis}; |
| 5 | use spark_rsi::measure::profile::{normalize_remote, ExecutionConfig, ExecutionProfile}; |
| 6 | use spark_rsi::measure::select::try_measure; |
| 7 | use spark_rsi::measure::{ |
| 8 | build_capability_graph, check_claim, measure_stages, median, p95_if_sufficient, |
| 9 | percentile_nearest_rank, CycleSampleRecord, MeasureContext, SampleLog, Stage, StageClock, |
| 10 | StageMeasurement, StageOutcome, StageSample, SCHEMA_VERSION, |
| 11 | }; |
| 12 | use spark_rsi::models::{ProposalKind, RsiConfig}; |
| 13 | use spark_rsi::propose::hypothesis::HypothesisContract; |
| 14 | use std::collections::BTreeMap; |
| 15 | use std::time::Duration; |
| 16 | |
| 17 | fn profile(config: &RsiConfig) -> ExecutionProfile { |
| 18 | ExecutionProfile { |
| 19 | model: config.model_id.clone(), |
| 20 | execution_config: ExecutionConfig::from_config(config), |
| 21 | cpu_count: Some(20), |
| 22 | memory_total_kib: Some(128 * 1024 * 1024), |
| 23 | gpu: "NVIDIA GB10".to_string(), |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | fn ctx(config: &RsiConfig) -> MeasureContext { |
| 28 | MeasureContext::from_parts( |
| 29 | "remote:example.org/repo", |
| 30 | profile(config), |
| 31 | Some("abc".into()), |
| 32 | ) |
| 33 | } |
| 34 | |
| 35 | fn record(ctx: &MeasureContext, id: &str, durations: [u64; 6]) -> CycleSampleRecord { |
| 36 | CycleSampleRecord { |
| 37 | schema_version: SCHEMA_VERSION, |
| 38 | cycle_id: id.to_string(), |
| 39 | workload_key: ctx.workload_key.clone(), |
| 40 | execution_profile_fingerprint: ctx.fingerprint.clone(), |
| 41 | execution_profile: ctx.profile.clone(), |
| 42 | stages: Stage::ALL |
| 43 | .iter() |
| 44 | .zip(durations) |
| 45 | .map(|(stage, d)| StageSample { |
| 46 | stage: *stage, |
| 47 | duration_us: d, |
| 48 | outcome: StageOutcome::Ok, |
| 49 | canary_protocol_version: None, |
| 50 | }) |
| 51 | .collect(), |
| 52 | source_commit: Some("abc".into()), |
| 53 | recorded_at: "2026-10-12T00:00:00Z".to_string(), |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | fn history(ctx: &MeasureContext, durations: [u64; 6], n: usize) -> Vec<CycleSampleRecord> { |
| 58 | (0..n) |
| 59 | .map(|i| record(ctx, &format!("cycle-{i}"), durations)) |
| 60 | .collect() |
| 61 | } |
| 62 | |
| 63 | fn rank_ids(measurements: &BTreeMap<Stage, StageMeasurement>) -> Vec<String> { |
| 64 | build_capability_graph(measurements) |
| 65 | .unwrap() |
| 66 | .rank_bottlenecks() |
| 67 | .into_iter() |
| 68 | .map(|r| r.node_id) |
| 69 | .collect() |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn different_valid_sample_sets_change_the_ranking_deterministically() { |
| 74 | let config = RsiConfig::default(); |
| 75 | let c = ctx(&config); |
| 76 | let slow_build = history(&c, [10, 20, 90_000, 30, 10, 10], 5); |
| 77 | let slow_propose = history(&c, [10, 90_000, 20, 30, 10, 10], 5); |
| 78 | let a = try_measure(&slow_build, "now", &c, 3, 20, 20).unwrap(); |
| 79 | let b = try_measure(&slow_propose, "now", &c, 3, 20, 20).unwrap(); |
| 80 | let order_a = rank_ids(&a); |
| 81 | let order_b = rank_ids(&b); |
| 82 | assert_ne!(order_a, order_b); |
| 83 | for _ in 0..25 { |
| 84 | assert_eq!(rank_ids(&a), order_a); |
| 85 | assert_eq!(rank_ids(&b), order_b); |
| 86 | } |
| 87 | let graph = build_capability_graph(&a).unwrap(); |
| 88 | let top = graph.top_bottleneck().unwrap(); |
| 89 | assert_eq!(top.node_id, "BuildJail"); |
| 90 | assert_eq!(top.ranking_basis, RankingBasis::Measured); |
| 91 | assert_eq!(top.latency_median_us, Some(90_000.0)); |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn removing_samples_gives_cold_start_structural_ranking_never_old_constants() { |
| 96 | let config = RsiConfig::default(); |
| 97 | let c = ctx(&config); |
| 98 | let measured = try_measure( |
| 99 | &history(&c, [90_000, 20, 10, 30, 10, 10], 5), |
| 100 | "now", |
| 101 | &c, |
| 102 | 3, |
| 103 | 20, |
| 104 | 20, |
| 105 | ) |
| 106 | .unwrap(); |
| 107 | let cold = measure_stages(Ok(Vec::new()), "now", &c, &config); |
| 108 | let graph = build_capability_graph(&cold).unwrap(); |
| 109 | let ranks = graph.rank_bottlenecks(); |
| 110 | assert_eq!(ranks.len(), 6); |
| 111 | for r in &ranks { |
| 112 | assert_eq!(r.measurement, Measurement::Unavailable); |
| 113 | assert_eq!(r.ranking_basis, RankingBasis::Structural); |
| 114 | assert_eq!(r.latency_median_us, None); |
| 115 | assert_eq!(r.error_rate_measured, None); |
| 116 | assert!(r |
| 117 | .unavailable_reason |
| 118 | .as_deref() |
| 119 | .unwrap() |
| 120 | .contains("fewer than 3 compatible completed samples in the last 20 cycles")); |
| 121 | // Score is centrality alone, so none of the old constants can have fed it. |
| 122 | assert_eq!(r.bottleneck_score, r.centrality); |
| 123 | } |
| 124 | assert_ne!(rank_ids(&measured), rank_ids(&cold)); |
| 125 | // Ties break by stage id ascending (byte order). |
| 126 | let tied: Vec<_> = ranks |
| 127 | .windows(2) |
| 128 | .filter(|w| (w[0].bottleneck_score - w[1].bottleneck_score).abs() < 1e-9) |
| 129 | .collect(); |
| 130 | for w in tied { |
| 131 | assert!(w[0].node_id < w[1].node_id); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn one_unavailable_stage_forces_structural_ranking_for_all() { |
| 137 | let config = RsiConfig::default(); |
| 138 | let c = ctx(&config); |
| 139 | let mut recs = history(&c, [10, 20, 90_000, 30, 10, 10], 5); |
| 140 | for r in &mut recs { |
| 141 | r.stages.retain(|s| s.stage != Stage::SupervisorCanary); |
| 142 | } |
| 143 | let m = try_measure(&recs, "now", &c, 3, 20, 20).unwrap(); |
| 144 | let ranks = build_capability_graph(&m).unwrap().rank_bottlenecks(); |
| 145 | assert!(ranks |
| 146 | .iter() |
| 147 | .all(|r| r.ranking_basis == RankingBasis::Structural)); |
| 148 | let canary = ranks |
| 149 | .iter() |
| 150 | .find(|r| r.node_id == "SupervisorCanary") |
| 151 | .unwrap(); |
| 152 | assert_eq!(canary.measurement, Measurement::Unavailable); |
| 153 | let build = ranks.iter().find(|r| r.node_id == "BuildJail").unwrap(); |
| 154 | assert_eq!(build.measurement, Measurement::Measured); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn explanation_distinguishes_measured_from_unavailable_for_every_stage() { |
| 159 | let config = RsiConfig::default(); |
| 160 | let c = ctx(&config); |
| 161 | let mut recs = history(&c, [10, 20, 90_000, 30, 10, 10], 5); |
| 162 | for r in &mut recs { |
| 163 | r.stages.retain(|s| s.stage != Stage::CortexCommit); |
| 164 | } |
| 165 | let m = try_measure(&recs, "now", &c, 3, 20, 20).unwrap(); |
| 166 | let graph = build_capability_graph(&m).unwrap(); |
| 167 | let ranks = graph.rank_bottlenecks(); |
| 168 | let doc = evidence_graph(&RsiCycleEvidence { |
| 169 | cycle_id: "now", |
| 170 | graph: Some(&graph), |
| 171 | metrics_origin: MetricsOrigin::Measured, |
| 172 | bottlenecks: &ranks, |
| 173 | hypothesis: None, |
| 174 | diagnostic: None, |
| 175 | receipt: None, |
| 176 | ratification: None, |
| 177 | }); |
| 178 | let caps: Vec<_> = doc |
| 179 | .nodes |
| 180 | .iter() |
| 181 | .filter(|n| n.id.starts_with("rsi:capability:")) |
| 182 | .collect(); |
| 183 | assert_eq!(caps.len(), 6); |
| 184 | for n in caps { |
| 185 | let measured = n.measurements.iter().any(|x| x.name == "latency_median_us"); |
| 186 | let marked = n |
| 187 | .missing |
| 188 | .iter() |
| 189 | .any(|x| x.expected.contains("measured median latency")); |
| 190 | assert_ne!(measured, marked, "{} must be exactly one of the two", n.id); |
| 191 | if n.id.ends_with("CortexCommit") { |
| 192 | assert!(marked); |
| 193 | assert!(n.missing[0] |
| 194 | .reason |
| 195 | .contains("fewer than 3 compatible completed samples")); |
| 196 | assert_eq!(n.status, 0); |
| 197 | } else { |
| 198 | assert!(measured); |
| 199 | assert_eq!(n.status, 1); |
| 200 | // 5 samples: p95 is unavailable and says why. |
| 201 | assert!(n.missing.iter().any(|x| x.expected.contains("p95"))); |
| 202 | } |
| 203 | } |
| 204 | let analysis = doc |
| 205 | .nodes |
| 206 | .iter() |
| 207 | .find(|n| n.id == "rsi:bottleneck:now") |
| 208 | .unwrap(); |
| 209 | assert!(analysis |
| 210 | .missing |
| 211 | .iter() |
| 212 | .any(|x| x.expected.contains("latency and error terms"))); |
| 213 | } |
| 214 | |
| 215 | #[test] |
| 216 | fn cold_start_hypothesis_claims_nothing_and_measured_one_uses_the_median() { |
| 217 | let config = RsiConfig::default(); |
| 218 | let c = ctx(&config); |
| 219 | let cold = measure_stages(Ok(Vec::new()), "now", &c, &config); |
| 220 | let top = build_capability_graph(&cold) |
| 221 | .unwrap() |
| 222 | .top_bottleneck() |
| 223 | .unwrap(); |
| 224 | let h = HypothesisContract::from_bottleneck("h1", "now", &top, 5.0); |
| 225 | assert!(h.is_exploratory()); |
| 226 | assert_eq!(h.latency_claim(), None); |
| 227 | assert_eq!(h.target_metric, "structural_ranking"); |
| 228 | assert!(h.validate().is_ok()); |
| 229 | |
| 230 | let m = try_measure( |
| 231 | &history(&c, [10, 20, 90_000, 30, 10, 10], 5), |
| 232 | "now", |
| 233 | &c, |
| 234 | 3, |
| 235 | 20, |
| 236 | 20, |
| 237 | ) |
| 238 | .unwrap(); |
| 239 | let top = build_capability_graph(&m) |
| 240 | .unwrap() |
| 241 | .top_bottleneck() |
| 242 | .unwrap(); |
| 243 | let h = HypothesisContract::from_bottleneck("h2", "now", &top, 5.0); |
| 244 | assert!(!h.is_exploratory()); |
| 245 | assert_eq!(h.latency_claim(), Some((90_000.0, 5.0))); |
| 246 | assert_eq!(h.baseline_value, 90_000.0); |
| 247 | assert!(h.validate().is_ok()); |
| 248 | } |
| 249 | |
| 250 | #[test] |
| 251 | fn optimization_and_latency_policy_candidates_need_a_measured_diagnosis() { |
| 252 | assert!(check_claim(&ProposalKind::Optimization, false, false).is_err()); |
| 253 | assert!(check_claim(&ProposalKind::Optimization, false, true).is_ok()); |
| 254 | for kind in [ |
| 255 | ProposalKind::Refactor, |
| 256 | ProposalKind::Documentation, |
| 257 | ProposalKind::UnslopSanitization, |
| 258 | ProposalKind::InvariantFix, |
| 259 | ProposalKind::MetaEngineImprovement, |
| 260 | ] { |
| 261 | assert!(check_claim(&kind, false, false).is_ok()); |
| 262 | let refused = check_claim(&kind, true, false).unwrap_err(); |
| 263 | assert!(refused.contains("require_latency_improvement")); |
| 264 | assert!(check_claim(&kind, true, true).is_ok()); |
| 265 | } |
| 266 | let why = check_claim(&ProposalKind::Optimization, false, false).unwrap_err(); |
| 267 | assert!(why.contains("unavailable diagnosis")); |
| 268 | } |
| 269 | |
| 270 | #[test] |
| 271 | fn percentile_matches_pinned_golden_values() { |
| 272 | let one_to_twenty: Vec<f64> = (1..=20).map(f64::from).collect(); |
| 273 | let one_to_hundred: Vec<f64> = (1..=100).map(f64::from).collect(); |
| 274 | assert_eq!(percentile_nearest_rank(&one_to_twenty, 95.0).unwrap(), 19.0); |
| 275 | assert_eq!(percentile_nearest_rank(&one_to_twenty, 50.0).unwrap(), 10.0); |
| 276 | assert_eq!( |
| 277 | percentile_nearest_rank(&one_to_twenty, 100.0).unwrap(), |
| 278 | 20.0 |
| 279 | ); |
| 280 | assert_eq!(percentile_nearest_rank(&one_to_twenty, 0.0).unwrap(), 1.0); |
| 281 | assert_eq!( |
| 282 | percentile_nearest_rank(&one_to_hundred, 95.0).unwrap(), |
| 283 | 95.0 |
| 284 | ); |
| 285 | assert_eq!( |
| 286 | percentile_nearest_rank(&one_to_hundred, 99.0).unwrap(), |
| 287 | 99.0 |
| 288 | ); |
| 289 | assert_eq!(percentile_nearest_rank(&[7.0], 95.0).unwrap(), 7.0); |
| 290 | assert_eq!( |
| 291 | percentile_nearest_rank(&[30.0, 10.0, 20.0], 50.0).unwrap(), |
| 292 | 20.0 |
| 293 | ); |
| 294 | // Even count: the lower of the two middle values. |
| 295 | assert_eq!(median(&[40.0, 10.0, 30.0, 20.0]).unwrap(), 20.0); |
| 296 | assert_eq!( |
| 297 | percentile_nearest_rank(&[10.0, 20.0, 30.0, 40.0, 50.0], 95.0).unwrap(), |
| 298 | 50.0 |
| 299 | ); |
| 300 | } |
| 301 | |
| 302 | #[test] |
| 303 | fn p95_is_unavailable_below_twenty_samples() { |
| 304 | let nineteen: Vec<f64> = (1..=19).map(f64::from).collect(); |
| 305 | let twenty: Vec<f64> = (1..=20).map(f64::from).collect(); |
| 306 | assert_eq!(p95_if_sufficient(&nineteen, 20).unwrap(), None); |
| 307 | assert_eq!(p95_if_sufficient(&twenty, 20).unwrap(), Some(19.0)); |
| 308 | } |
| 309 | |
| 310 | #[test] |
| 311 | fn non_finite_and_negative_measurements_are_rejected_not_clamped() { |
| 312 | assert!(median(&[1.0, f64::NAN]).is_err()); |
| 313 | assert!(median(&[1.0, f64::INFINITY]).is_err()); |
| 314 | assert!(median(&[1.0, -2.0]).is_err()); |
| 315 | assert!(median(&[]).is_err()); |
| 316 | assert!(percentile_nearest_rank(&[1.0], 101.0).is_err()); |
| 317 | assert!(p95_if_sufficient(&[f64::NEG_INFINITY], 1).is_err()); |
| 318 | } |
| 319 | |
| 320 | #[test] |
| 321 | fn partial_cycles_are_stored_separately_and_never_ranked() { |
| 322 | let tmp = tempfile::tempdir().unwrap(); |
| 323 | let config = RsiConfig::default(); |
| 324 | let c = ctx(&config); |
| 325 | let log = SampleLog::new(tmp.path()); |
| 326 | for r in history(&c, [10, 20, 30, 40, 50, 60], 2) { |
| 327 | log.append_complete(&r).unwrap(); |
| 328 | } |
| 329 | // A failed cycle with huge durations goes to the partial log only. |
| 330 | log.append_partial(&record(&c, "cycle-failed", [9_999_999; 6])) |
| 331 | .unwrap(); |
| 332 | assert_eq!(log.read_complete().unwrap().len(), 2); |
| 333 | assert_eq!(log.read_partial().unwrap().len(), 1); |
| 334 | assert!(log.complete_path().ends_with("measure/stage-samples.jsonl")); |
| 335 | assert!(log.partial_path().ends_with("measure/partial-cycles.jsonl")); |
| 336 | |
| 337 | log.append_complete(&record(&c, "cycle-2", [10, 20, 30, 40, 50, 60])) |
| 338 | .unwrap(); |
| 339 | let m = measure_stages(log.read_complete(), "now", &c, &config); |
| 340 | match &m[&Stage::Observe] { |
| 341 | StageMeasurement::Measured { |
| 342 | latency_median_us, |
| 343 | sample_count, |
| 344 | .. |
| 345 | } => { |
| 346 | assert_eq!(*latency_median_us, 10.0); |
| 347 | assert_eq!(*sample_count, 3); |
| 348 | } |
| 349 | other => panic!("expected measured, got {other:?}"), |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | #[test] |
| 354 | fn different_fingerprint_workload_key_or_current_cycle_are_excluded() { |
| 355 | let config = RsiConfig::default(); |
| 356 | let c = ctx(&config); |
| 357 | let mut recs = history(&c, [10, 20, 30, 40, 50, 60], 2); |
| 358 | |
| 359 | let mut other_gpu = profile(&config); |
| 360 | other_gpu.gpu = "none".to_string(); |
| 361 | let other_hw = MeasureContext::from_parts("remote:example.org/repo", other_gpu, None); |
| 362 | assert_eq!( |
| 363 | other_hw.workload_key, c.workload_key, |
| 364 | "hardware is not in the key" |
| 365 | ); |
| 366 | assert_ne!(other_hw.fingerprint, c.fingerprint); |
| 367 | recs.push(record(&other_hw, "cycle-other-hw", [1; 6])); |
| 368 | |
| 369 | let other_repo = MeasureContext::from_parts("remote:example.org/other", profile(&config), None); |
| 370 | assert_ne!(other_repo.workload_key, c.workload_key); |
| 371 | recs.push(record(&other_repo, "cycle-other-repo", [1; 6])); |
| 372 | |
| 373 | recs.push(record(&c, "now", [1; 6])); |
| 374 | |
| 375 | // Only two compatible prior samples remain, below the minimum of three. |
| 376 | let m = try_measure(&recs, "now", &c, 3, 20, 20).unwrap(); |
| 377 | for s in Stage::ALL { |
| 378 | assert!(matches!( |
| 379 | &m[&s], |
| 380 | StageMeasurement::Unavailable { |
| 381 | sample_count: 2, |
| 382 | .. |
| 383 | } |
| 384 | )); |
| 385 | } |
| 386 | // With a minimum of two the median comes only from the two compatible prior cycles. |
| 387 | let m = try_measure(&recs, "now", &c, 2, 20, 20).unwrap(); |
| 388 | assert!(matches!( |
| 389 | &m[&Stage::Observe], |
| 390 | StageMeasurement::Measured { latency_median_us, sample_count: 2, .. } if *latency_median_us == 10.0 |
| 391 | )); |
| 392 | } |
| 393 | |
| 394 | #[test] |
| 395 | fn window_keeps_only_the_most_recent_compatible_cycles() { |
| 396 | let config = RsiConfig::default(); |
| 397 | let c = ctx(&config); |
| 398 | let mut recs = history(&c, [1_000, 1, 1, 1, 1, 1], 4); |
| 399 | recs.extend((0..3).map(|i| record(&c, &format!("recent-{i}"), [5, 1, 1, 1, 1, 1]))); |
| 400 | let m = try_measure(&recs, "now", &c, 3, 3, 20).unwrap(); |
| 401 | assert!(matches!( |
| 402 | &m[&Stage::Observe], |
| 403 | StageMeasurement::Measured { latency_median_us, sample_count: 3, .. } if *latency_median_us == 5.0 |
| 404 | )); |
| 405 | } |
| 406 | |
| 407 | #[test] |
| 408 | fn error_rate_counts_failed_outcomes_only() { |
| 409 | let config = RsiConfig::default(); |
| 410 | let c = ctx(&config); |
| 411 | let mut recs = history(&c, [10, 20, 30, 40, 50, 60], 4); |
| 412 | recs[0].stages[2].outcome = StageOutcome::Failed; |
| 413 | recs[1].stages[2].outcome = StageOutcome::Refused; |
| 414 | let m = try_measure(&recs, "now", &c, 3, 20, 20).unwrap(); |
| 415 | assert!(matches!( |
| 416 | &m[&Stage::BuildJail], |
| 417 | StageMeasurement::Measured { error_rate, .. } if *error_rate == 0.25 |
| 418 | )); |
| 419 | } |
| 420 | |
| 421 | #[test] |
| 422 | fn log_is_strict_but_skips_a_torn_trailing_line_and_drops_it_on_append() { |
| 423 | let tmp = tempfile::tempdir().unwrap(); |
| 424 | let config = RsiConfig::default(); |
| 425 | let c = ctx(&config); |
| 426 | let log = SampleLog::new(tmp.path()); |
| 427 | log.append_complete(&record(&c, "cycle-a", [1; 6])).unwrap(); |
| 428 | |
| 429 | let path = log.complete_path(); |
| 430 | let mut torn = std::fs::read(&path).unwrap(); |
| 431 | torn.extend_from_slice(b"{\"schema_version\":1,\"cycle_id\":\"cycle-b\",\"wor"); |
| 432 | std::fs::write(&path, &torn).unwrap(); |
| 433 | assert_eq!( |
| 434 | log.read_complete().unwrap().len(), |
| 435 | 1, |
| 436 | "torn tail is skipped" |
| 437 | ); |
| 438 | |
| 439 | log.append_complete(&record(&c, "cycle-c", [2; 6])).unwrap(); |
| 440 | let ids: Vec<_> = log |
| 441 | .read_complete() |
| 442 | .unwrap() |
| 443 | .into_iter() |
| 444 | .map(|r| r.cycle_id) |
| 445 | .collect(); |
| 446 | assert_eq!(ids, ["cycle-a", "cycle-c"]); |
| 447 | |
| 448 | // A terminated line that does not parse is corruption, not a torn write. |
| 449 | let mut bad = std::fs::read(&path).unwrap(); |
| 450 | bad.extend_from_slice(b"not json\n"); |
| 451 | std::fs::write(&path, &bad).unwrap(); |
| 452 | assert!(log.read_complete().is_err()); |
| 453 | let m = measure_stages(log.read_complete(), "now", &c, &config); |
| 454 | assert!(m.values().all(|v| matches!( |
| 455 | v, |
| 456 | StageMeasurement::Unavailable { reason, .. } if reason.contains("rejected") |
| 457 | ))); |
| 458 | |
| 459 | // Unknown fields and other schema versions are refused too. |
| 460 | let mut other = record(&c, "cycle-other", [1; 6]); |
| 461 | other.schema_version = SCHEMA_VERSION + 1; |
| 462 | assert!(log.append_complete(&other).is_err()); |
| 463 | std::fs::write(&path, b"{\"schema_version\":1,\"extra\":true}\n").unwrap(); |
| 464 | assert!(log.read_complete().is_err()); |
| 465 | } |
| 466 | |
| 467 | #[test] |
| 468 | fn workload_key_and_fingerprint_follow_the_documented_fields() { |
| 469 | let base = RsiConfig::default(); |
| 470 | let c = ctx(&base); |
| 471 | |
| 472 | // Evaluation policy is not an execution mode. |
| 473 | let mut policy = base.clone(); |
| 474 | policy.require_latency_improvement = true; |
| 475 | policy.non_inferiority_margin = Some(42.0); |
| 476 | policy.measure_min_samples = 9; |
| 477 | let p = ctx(&policy); |
| 478 | assert_eq!(p.workload_key, c.workload_key); |
| 479 | assert_eq!(p.fingerprint, c.fingerprint); |
| 480 | |
| 481 | // Model and execution configuration change both. |
| 482 | let mut model = base.clone(); |
| 483 | model.model_id = "another-model".into(); |
| 484 | assert_ne!(ctx(&model).workload_key, c.workload_key); |
| 485 | assert_ne!(ctx(&model).fingerprint, c.fingerprint); |
| 486 | let mut exec = base.clone(); |
| 487 | exec.canary_target = 77; |
| 488 | assert_ne!(ctx(&exec).workload_key, c.workload_key); |
| 489 | |
| 490 | // The target commit is provenance only. |
| 491 | let moved = MeasureContext::from_parts( |
| 492 | "remote:example.org/repo", |
| 493 | profile(&base), |
| 494 | Some("def".into()), |
| 495 | ); |
| 496 | assert_eq!(moved.workload_key, c.workload_key); |
| 497 | assert_eq!(moved.fingerprint, c.fingerprint); |
| 498 | |
| 499 | // Machine facts change the fingerprint only. |
| 500 | let mut small = profile(&base); |
| 501 | small.cpu_count = Some(4); |
| 502 | let s = MeasureContext::from_parts("remote:example.org/repo", small, None); |
| 503 | assert_eq!(s.workload_key, c.workload_key); |
| 504 | assert_ne!(s.fingerprint, c.fingerprint); |
| 505 | } |
| 506 | |
| 507 | #[test] |
| 508 | fn remote_urls_are_normalized_without_credentials() { |
| 509 | assert_eq!( |
| 510 | normalize_remote("https://user:token@github.com/aien-dev/spark-rsi.git/"), |
| 511 | "https://github.com/aien-dev/spark-rsi" |
| 512 | ); |
| 513 | assert_eq!( |
| 514 | normalize_remote("git@github.com:aien-dev/spark-rsi.git"), |
| 515 | "git@github.com:aien-dev/spark-rsi" |
| 516 | ); |
| 517 | } |
| 518 | |
| 519 | #[test] |
| 520 | fn canary_protocol_version_is_recorded_on_canary_samples_only() { |
| 521 | let mut clock = StageClock::new(); |
| 522 | clock.record(Stage::Observe, Duration::from_micros(5), StageOutcome::Ok); |
| 523 | clock.record(Stage::BuildJail, Duration::from_micros(7), StageOutcome::Ok); |
| 524 | clock.record( |
| 525 | Stage::BuildJail, |
| 526 | Duration::from_micros(3), |
| 527 | StageOutcome::Failed, |
| 528 | ); |
| 529 | clock.record( |
| 530 | Stage::SupervisorCanary, |
| 531 | Duration::from_micros(9), |
| 532 | StageOutcome::Refused, |
| 533 | ); |
| 534 | let samples = clock.samples(); |
| 535 | assert_eq!(samples.len(), 3); |
| 536 | let build = samples |
| 537 | .iter() |
| 538 | .find(|s| s.stage == Stage::BuildJail) |
| 539 | .unwrap(); |
| 540 | assert_eq!( |
| 541 | (build.duration_us, build.outcome), |
| 542 | (10, StageOutcome::Failed) |
| 543 | ); |
| 544 | for s in &samples { |
| 545 | assert_eq!( |
| 546 | s.canary_protocol_version.is_some(), |
| 547 | s.stage == Stage::SupervisorCanary |
| 548 | ); |
| 549 | } |
| 550 | assert_eq!( |
| 551 | samples.last().unwrap().canary_protocol_version, |
| 552 | Some(spark_rsi::canary::CANARY_PROTOCOL_VERSION) |
| 553 | ); |
| 554 | } |
| 555 | |
| 556 | #[tokio::test] |
| 557 | async fn a_completed_cycle_appends_one_record_to_the_sample_log() { |
| 558 | let tmp = tempfile::tempdir().unwrap(); |
| 559 | let rsi_root = tmp.path().join(".rsi"); |
| 560 | std::fs::create_dir_all(&rsi_root).unwrap(); |
| 561 | spark_rsi::ledger::ImprovementLedger::open(&rsi_root).unwrap(); |
| 562 | let holdouts = rsi_root.join("holdouts"); |
| 563 | std::fs::create_dir_all(&holdouts).unwrap(); |
| 564 | for s in spark_rsi::actor::judge::HoldoutSuite::builtin_suites() { |
| 565 | s.save_to_dir(&holdouts).unwrap(); |
| 566 | } |
| 567 | let key = p256::ecdsa::SigningKey::from_bytes(&[88u8; 32].into()).unwrap(); |
| 568 | let config = RsiConfig { |
| 569 | target_repo: ".".to_string(), |
| 570 | sandbox_root: tmp.path().join("sandbox").display().to_string(), |
| 571 | rsi_root: rsi_root.display().to_string(), |
| 572 | holdouts_dir: Some(holdouts.display().to_string()), |
| 573 | signing_key_hex: Some(hex::encode(key.to_bytes())), |
| 574 | model_url: "http://127.0.0.1:9999/v1".to_string(), |
| 575 | non_inferiority_margin: Some(150.0), |
| 576 | ..Default::default() |
| 577 | }; |
| 578 | let result = spark_rsi::daemon::RsiEngine::run_cycle(&config) |
| 579 | .await |
| 580 | .unwrap(); |
| 581 | let log = SampleLog::new(&rsi_root); |
| 582 | let records = log.read_complete().unwrap(); |
| 583 | assert_eq!(records.len(), 1); |
| 584 | assert_eq!(records[0].cycle_id, result.cycle_id); |
| 585 | assert!(records[0].stages.iter().any(|s| s.stage == Stage::Observe)); |
| 586 | assert!(records[0].stages.iter().any(|s| s.stage == Stage::Propose)); |
| 587 | assert!(log.read_partial().unwrap().is_empty()); |
| 588 | } |