spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / tests / test_canary_execution.rs
| 1 | //! Drives the real fixture binary through the supervisor's canary executor. Every run goes over |
| 2 | //! a real Unix socket with a real child process; the secret reaches the child only through its |
| 3 | //! environment. |
| 4 | |
| 5 | mod canary_fixture; |
| 6 | |
| 7 | use spark_rsi::canary::identity::digest_file; |
| 8 | use spark_rsi::canary::{ |
| 9 | admit_sealed_quota, result_digest, CanaryAdapterManifest, CanaryWorkloadManifest, |
| 10 | CandidateIdentity, PinnedWorkload, SealedObservationLog, VerifiedStatus, |
| 11 | }; |
| 12 | use spark_rsi::supervisor::canary_exec::{ |
| 13 | observation_log_path, CanaryExecutor, ExecutionOutcome, ExecutorConfig, |
| 14 | }; |
| 15 | use spark_rsi::supervisor::daemon::SupervisorConfig; |
| 16 | use spark_rsi::supervisor::{GenerationInfo, GenerationState, HostSupervisor}; |
| 17 | use std::path::PathBuf; |
| 18 | use std::time::Duration; |
| 19 | |
| 20 | const SECRET: &str = "test-secret-passed-through-the-environment-only"; |
| 21 | const GEN: &str = "gen-fixture-0001"; |
| 22 | |
| 23 | struct Harness { |
| 24 | _tmp: tempfile::TempDir, |
| 25 | rsi_root: PathBuf, |
| 26 | config: SupervisorConfig, |
| 27 | supervisor: HostSupervisor, |
| 28 | workload: PinnedWorkload, |
| 29 | identity: CandidateIdentity, |
| 30 | exe: PathBuf, |
| 31 | } |
| 32 | |
| 33 | fn harness() -> Harness { |
| 34 | let exe = canary_fixture::fixture_executable().to_path_buf(); |
| 35 | let tmp = tempfile::tempdir().unwrap(); |
| 36 | let rsi_root = tmp.path().join(".rsi"); |
| 37 | let config = SupervisorConfig { |
| 38 | rsi_root: rsi_root.clone(), |
| 39 | socket_path: rsi_root.join("supervisor.sock"), |
| 40 | memory_limit_mb: 49152, |
| 41 | canary_target: 10, |
| 42 | max_latency_us: 1_000_000, |
| 43 | max_error_rate: 0.0, |
| 44 | shared_secret: SECRET.to_string(), |
| 45 | }; |
| 46 | let supervisor = HostSupervisor::new(&rsi_root, config.memory_limit_mb); |
| 47 | let workload = |
| 48 | CanaryWorkloadManifest::load(&canary_fixture::fixture_dir().join("workload.json")).unwrap(); |
| 49 | let identity = CandidateIdentity::new( |
| 50 | &result_digest(b"patch"), |
| 51 | &result_digest(b"tree"), |
| 52 | &digest_file(&exe).unwrap(), |
| 53 | GEN, |
| 54 | ); |
| 55 | Harness { |
| 56 | _tmp: tmp, |
| 57 | rsi_root, |
| 58 | config, |
| 59 | supervisor, |
| 60 | workload, |
| 61 | identity, |
| 62 | exe, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | impl Harness { |
| 67 | fn generation(&self) -> GenerationInfo { |
| 68 | let src = self._tmp.path().join("artifacts"); |
| 69 | std::fs::create_dir_all(&src).unwrap(); |
| 70 | self.supervisor |
| 71 | .stage_generation(GEN, &src, "manifest-digest") |
| 72 | .unwrap() |
| 73 | } |
| 74 | |
| 75 | fn executor( |
| 76 | &self, |
| 77 | mode: &str, |
| 78 | extra_env: &[(&str, &str)], |
| 79 | mut exec: ExecutorConfig, |
| 80 | ) -> CanaryExecutor<'_> { |
| 81 | exec.worker_env |
| 82 | .push(("RSI_CANARY_FIXTURE_MODE".to_string(), mode.to_string())); |
| 83 | for (k, v) in extra_env { |
| 84 | exec.worker_env.push((k.to_string(), v.to_string())); |
| 85 | } |
| 86 | CanaryExecutor::new( |
| 87 | &self.supervisor, |
| 88 | self.config.clone(), |
| 89 | self.workload.clone(), |
| 90 | self.identity.clone(), |
| 91 | self.exe.clone(), |
| 92 | Vec::new(), |
| 93 | exec, |
| 94 | ) |
| 95 | } |
| 96 | |
| 97 | fn admit(&self, sealed: &SealedObservationLog) -> Result<(), String> { |
| 98 | admit_sealed_quota(sealed, &self.identity, &self.workload.manifest, 10).map(|_| ()) |
| 99 | } |
| 100 | |
| 101 | fn active_sock_untouched(&self) { |
| 102 | assert!( |
| 103 | std::fs::symlink_metadata(self.rsi_root.join("active.sock")).is_err(), |
| 104 | "the executor must never switch active.sock" |
| 105 | ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | fn quick() -> ExecutorConfig { |
| 110 | ExecutorConfig { |
| 111 | readiness_deadline: Duration::from_secs(5), |
| 112 | settle_window: Duration::from_millis(300), |
| 113 | deadline_cap_ms: Some(400), |
| 114 | ..ExecutorConfig::default() |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | fn sealed_of(outcome: &ExecutionOutcome) -> &SealedObservationLog { |
| 119 | outcome.sealed().expect("run should have sealed its log") |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | fn the_fixture_ships_a_valid_adapter_manifest() { |
| 124 | let loaded = CanaryAdapterManifest::load_from_tree(&canary_fixture::fixture_dir()).unwrap(); |
| 125 | assert_eq!(loaded.manifest.executable, "release/canary-target"); |
| 126 | assert_eq!(loaded.manifest.workload_id, "fixture-v1"); |
| 127 | let workload = |
| 128 | CanaryWorkloadManifest::load(&canary_fixture::fixture_dir().join("workload.json")).unwrap(); |
| 129 | assert_eq!(workload.manifest.workload_id, loaded.manifest.workload_id); |
| 130 | assert_eq!(workload.manifest.requests.len(), 10); |
| 131 | assert!(workload |
| 132 | .manifest |
| 133 | .requests |
| 134 | .iter() |
| 135 | .all(|r| r.deadline_ms == 2000)); |
| 136 | } |
| 137 | |
| 138 | #[tokio::test] |
| 139 | async fn correct_run_yields_ten_qualifying_observations_and_passes_the_quota() { |
| 140 | if !spark_rsi::not_run::user_scope_or_not_run( |
| 141 | "correct_run_yields_ten_qualifying_observations_and_passes_the_quota", |
| 142 | ) { |
| 143 | return; |
| 144 | } |
| 145 | let h = harness(); |
| 146 | let mut generation = h.generation(); |
| 147 | let outcome = h |
| 148 | .executor("correct", &[], ExecutorConfig::default()) |
| 149 | .run(&mut generation) |
| 150 | .await |
| 151 | .unwrap(); |
| 152 | let sealed = sealed_of(&outcome); |
| 153 | assert_eq!(sealed.observations.len(), 10); |
| 154 | assert_eq!(sealed.qualifying().count(), 10); |
| 155 | assert!(h.admit(sealed).is_ok()); |
| 156 | let summary = outcome.summary(); |
| 157 | assert!(summary.ready && !summary.reverted); |
| 158 | assert_eq!(summary.verified_success, 10); |
| 159 | assert_eq!(summary.first_failure, None); |
| 160 | // Elapsed time is the supervisor's, and every observation is bound to the executable. |
| 161 | assert!( |
| 162 | sealed |
| 163 | .observations |
| 164 | .iter() |
| 165 | .all(|o| o.elapsed_us > 0 |
| 166 | && o.candidate_executable_digest == h.identity.executable_digest) |
| 167 | ); |
| 168 | // The executor never promotes and never switches the active socket. |
| 169 | assert_ne!(generation.state, GenerationState::Durable); |
| 170 | assert_eq!(generation.state, GenerationState::CanaryActive); |
| 171 | h.active_sock_untouched(); |
| 172 | // The sealed log on disk verifies independently. |
| 173 | let reloaded = SealedObservationLog::load(&observation_log_path(&h.rsi_root, GEN)).unwrap(); |
| 174 | assert_eq!(reloaded.log_digest(), sealed.log_digest()); |
| 175 | } |
| 176 | |
| 177 | #[tokio::test] |
| 178 | async fn wrong_answer_is_recorded_reverts_early_and_fails_admission() { |
| 179 | if !spark_rsi::not_run::user_scope_or_not_run( |
| 180 | "wrong_answer_is_recorded_reverts_early_and_fails_admission", |
| 181 | ) { |
| 182 | return; |
| 183 | } |
| 184 | let h = harness(); |
| 185 | let mut generation = h.generation(); |
| 186 | let outcome = h |
| 187 | .executor("wrong_answer", &[], quick()) |
| 188 | .run(&mut generation) |
| 189 | .await |
| 190 | .unwrap(); |
| 191 | let sealed = sealed_of(&outcome); |
| 192 | assert_eq!( |
| 193 | sealed.observations.len(), |
| 194 | 3, |
| 195 | "no request after the failure is issued" |
| 196 | ); |
| 197 | assert_eq!(sealed.observations[2].status, VerifiedStatus::WrongResult); |
| 198 | assert_eq!(sealed.observations[2].request_id, "req-003"); |
| 199 | assert_eq!(generation.state, GenerationState::Reverting); |
| 200 | assert!(outcome.summary().reverted); |
| 201 | assert_eq!( |
| 202 | outcome.summary().first_failure.as_deref(), |
| 203 | Some("req-003: WrongResult") |
| 204 | ); |
| 205 | assert!(h.admit(sealed).is_err()); |
| 206 | h.active_sock_untouched(); |
| 207 | } |
| 208 | |
| 209 | #[tokio::test] |
| 210 | async fn fabricated_success_cannot_qualify() { |
| 211 | if !spark_rsi::not_run::user_scope_or_not_run("fabricated_success_cannot_qualify") { |
| 212 | return; |
| 213 | } |
| 214 | let h = harness(); |
| 215 | let mut generation = h.generation(); |
| 216 | let outcome = h |
| 217 | .executor("fabricate_success", &[], quick()) |
| 218 | .run(&mut generation) |
| 219 | .await |
| 220 | .unwrap(); |
| 221 | let sealed = sealed_of(&outcome); |
| 222 | // The worker's CanaryReport frames and worker_latency_us of 1 created nothing: the one |
| 223 | // observation is the supervisor's own verdict on the wrong answer. |
| 224 | assert_eq!(sealed.observations.len(), 1); |
| 225 | assert_eq!(sealed.observations[0].status, VerifiedStatus::WrongResult); |
| 226 | assert_eq!(sealed.qualifying().count(), 0); |
| 227 | assert_ne!(sealed.observations[0].elapsed_us, 1); |
| 228 | assert!(h.admit(sealed).is_err()); |
| 229 | assert_eq!(generation.state, GenerationState::Reverting); |
| 230 | } |
| 231 | |
| 232 | #[tokio::test] |
| 233 | async fn duplicate_results_yield_one_observation_per_request_and_rejections() { |
| 234 | if !spark_rsi::not_run::user_scope_or_not_run( |
| 235 | "duplicate_results_yield_one_observation_per_request_and_rejections", |
| 236 | ) { |
| 237 | return; |
| 238 | } |
| 239 | let h = harness(); |
| 240 | let mut generation = h.generation(); |
| 241 | let outcome = h |
| 242 | .executor("duplicate", &[], quick()) |
| 243 | .run(&mut generation) |
| 244 | .await |
| 245 | .unwrap(); |
| 246 | let sealed = sealed_of(&outcome); |
| 247 | assert_eq!(sealed.observations.len(), 10); |
| 248 | let mut ids: Vec<&str> = sealed |
| 249 | .observations |
| 250 | .iter() |
| 251 | .map(|o| o.request_id.as_str()) |
| 252 | .collect(); |
| 253 | ids.sort(); |
| 254 | ids.dedup(); |
| 255 | assert_eq!(ids.len(), 10, "exactly one observation per request"); |
| 256 | assert!( |
| 257 | outcome.summary().rejections >= 9, |
| 258 | "each extra result is a rejection, got {}", |
| 259 | outcome.summary().rejections |
| 260 | ); |
| 261 | assert_eq!(sealed.rejections.len(), outcome.summary().rejections); |
| 262 | assert!( |
| 263 | h.admit(sealed).is_ok(), |
| 264 | "duplicates add nothing and break nothing" |
| 265 | ); |
| 266 | } |
| 267 | |
| 268 | #[tokio::test] |
| 269 | async fn replay_on_a_new_connection_creates_no_observations_even_across_a_restart() { |
| 270 | if !spark_rsi::not_run::user_scope_or_not_run( |
| 271 | "replay_on_a_new_connection_creates_no_observations_even_across_a_restart", |
| 272 | ) { |
| 273 | return; |
| 274 | } |
| 275 | let h = harness(); |
| 276 | let mut generation = h.generation(); |
| 277 | |
| 278 | // First run stops after 5 observations, as a supervisor crash would. The worker replays |
| 279 | // earlier results on a fresh connection at request 3. |
| 280 | let halted = h |
| 281 | .executor( |
| 282 | "replay", |
| 283 | &[], |
| 284 | ExecutorConfig { |
| 285 | halt_after_requests: Some(5), |
| 286 | ..quick() |
| 287 | }, |
| 288 | ) |
| 289 | .run(&mut generation) |
| 290 | .await |
| 291 | .unwrap(); |
| 292 | assert!(matches!(halted, ExecutionOutcome::Halted { .. })); |
| 293 | assert_eq!(halted.summary().verified_success, 5); |
| 294 | |
| 295 | // Restart: reopen the same log, never recount, finish the rest. The replay happens again. |
| 296 | let mut generation = h.generation(); |
| 297 | let outcome = h |
| 298 | .executor("replay", &[], quick()) |
| 299 | .run(&mut generation) |
| 300 | .await |
| 301 | .unwrap(); |
| 302 | let sealed = sealed_of(&outcome); |
| 303 | assert_eq!(outcome.summary().recovered, 5); |
| 304 | assert_eq!(outcome.summary().issued, 5); |
| 305 | assert_eq!( |
| 306 | sealed.observations.len(), |
| 307 | 10, |
| 308 | "replays create no new observations" |
| 309 | ); |
| 310 | assert!( |
| 311 | sealed.rejections.len() >= 1, |
| 312 | "the replayed frames are logged as rejections" |
| 313 | ); |
| 314 | assert!(h.admit(sealed).is_ok()); |
| 315 | } |
| 316 | |
| 317 | #[tokio::test] |
| 318 | async fn timeout_is_recorded_as_a_timeout_and_reverts() { |
| 319 | if !spark_rsi::not_run::user_scope_or_not_run("timeout_is_recorded_as_a_timeout_and_reverts") { |
| 320 | return; |
| 321 | } |
| 322 | let h = harness(); |
| 323 | let mut generation = h.generation(); |
| 324 | let outcome = h |
| 325 | .executor("timeout", &[], quick()) |
| 326 | .run(&mut generation) |
| 327 | .await |
| 328 | .unwrap(); |
| 329 | let sealed = sealed_of(&outcome); |
| 330 | assert_eq!(sealed.observations.len(), 3); |
| 331 | let last = &sealed.observations[2]; |
| 332 | assert_eq!(last.status, VerifiedStatus::Timeout); |
| 333 | assert_eq!(last.result_digest, None); |
| 334 | assert!( |
| 335 | last.elapsed_us >= 400_000, |
| 336 | "elapsed is the supervisor's: {}", |
| 337 | last.elapsed_us |
| 338 | ); |
| 339 | assert_eq!(generation.state, GenerationState::Reverting); |
| 340 | assert!(h.admit(sealed).is_err()); |
| 341 | } |
| 342 | |
| 343 | #[tokio::test] |
| 344 | async fn an_answer_to_the_wrong_request_is_a_rejection_then_a_timeout() { |
| 345 | if !spark_rsi::not_run::user_scope_or_not_run( |
| 346 | "an_answer_to_the_wrong_request_is_a_rejection_then_a_timeout", |
| 347 | ) { |
| 348 | return; |
| 349 | } |
| 350 | let h = harness(); |
| 351 | let mut generation = h.generation(); |
| 352 | let outcome = h |
| 353 | .executor("wrong_request", &[], quick()) |
| 354 | .run(&mut generation) |
| 355 | .await |
| 356 | .unwrap(); |
| 357 | let sealed = sealed_of(&outcome); |
| 358 | assert_eq!(sealed.observations.len(), 3); |
| 359 | assert_eq!(sealed.observations[2].status, VerifiedStatus::Timeout); |
| 360 | assert_eq!(sealed.rejections.len(), 1); |
| 361 | assert!(sealed.rejections[0].reason.contains("req-bogus")); |
| 362 | assert!(h.admit(sealed).is_err()); |
| 363 | } |
| 364 | |
| 365 | #[tokio::test] |
| 366 | async fn premature_promotion_attempts_never_reach_durable_or_switch_active_sock() { |
| 367 | if !spark_rsi::not_run::user_scope_or_not_run( |
| 368 | "premature_promotion_attempts_never_reach_durable_or_switch_active_sock", |
| 369 | ) { |
| 370 | return; |
| 371 | } |
| 372 | let h = harness(); |
| 373 | let mut generation = h.generation(); |
| 374 | let outcome = h |
| 375 | .executor("premature_promotion", &[], quick()) |
| 376 | .run(&mut generation) |
| 377 | .await |
| 378 | .unwrap(); |
| 379 | assert_ne!(generation.state, GenerationState::Durable); |
| 380 | h.active_sock_untouched(); |
| 381 | assert!( |
| 382 | outcome.summary().rejections >= 10, |
| 383 | "DrainComplete and extra Ready frames are rejections, got {}", |
| 384 | outcome.summary().rejections |
| 385 | ); |
| 386 | // The answers were genuine, so the quota (the only authority) is the one that admits. |
| 387 | let sealed = sealed_of(&outcome); |
| 388 | assert_eq!(sealed.observations.len(), 10); |
| 389 | assert!(h.admit(sealed).is_ok()); |
| 390 | } |
| 391 | |
| 392 | #[tokio::test] |
| 393 | async fn a_wrong_secret_is_rejected_and_closed_without_rollback() { |
| 394 | if !spark_rsi::not_run::user_scope_or_not_run( |
| 395 | "a_wrong_secret_is_rejected_and_closed_without_rollback", |
| 396 | ) { |
| 397 | return; |
| 398 | } |
| 399 | let h = harness(); |
| 400 | let mut generation = h.generation(); |
| 401 | let outcome = h |
| 402 | .executor( |
| 403 | "correct", |
| 404 | &[("SUPERVISOR_SECRET", "not-the-supervisor-secret")], |
| 405 | ExecutorConfig { |
| 406 | readiness_deadline: Duration::from_secs(3), |
| 407 | ..quick() |
| 408 | }, |
| 409 | ) |
| 410 | .run(&mut generation) |
| 411 | .await |
| 412 | .unwrap(); |
| 413 | let sealed = sealed_of(&outcome); |
| 414 | assert!(!outcome.summary().ready); |
| 415 | assert!(sealed.observations.is_empty()); |
| 416 | assert!(sealed |
| 417 | .rejections |
| 418 | .iter() |
| 419 | .any(|r| r.reason.contains("BadHmac"))); |
| 420 | assert!(h.admit(sealed).is_err()); |
| 421 | h.active_sock_untouched(); |
| 422 | } |
| 423 | |
| 424 | #[tokio::test] |
| 425 | async fn old_wrong_generation_and_silent_workers_are_never_ready() { |
| 426 | if !spark_rsi::not_run::user_scope_or_not_run( |
| 427 | "old_wrong_generation_and_silent_workers_are_never_ready", |
| 428 | ) { |
| 429 | return; |
| 430 | } |
| 431 | for (mode, env) in [ |
| 432 | ("old_protocol", vec![]), |
| 433 | ( |
| 434 | "correct", |
| 435 | vec![("RSI_CANARY_FIXTURE_PROTOCOL_VERSION", "0")], |
| 436 | ), |
| 437 | ("wrong_generation", vec![]), |
| 438 | ("bare_connection", vec![]), |
| 439 | ] { |
| 440 | let h = harness(); |
| 441 | let mut generation = h.generation(); |
| 442 | let outcome = h |
| 443 | .executor( |
| 444 | mode, |
| 445 | &env, |
| 446 | ExecutorConfig { |
| 447 | readiness_deadline: Duration::from_millis(800), |
| 448 | ..quick() |
| 449 | }, |
| 450 | ) |
| 451 | .run(&mut generation) |
| 452 | .await |
| 453 | .unwrap(); |
| 454 | let sealed = sealed_of(&outcome); |
| 455 | assert!( |
| 456 | !outcome.summary().ready, |
| 457 | "{} {:?} must not be ready", |
| 458 | mode, |
| 459 | env |
| 460 | ); |
| 461 | assert!(sealed.observations.is_empty(), "{} {:?}", mode, env); |
| 462 | assert!(outcome.summary().reverted, "{} {:?}", mode, env); |
| 463 | assert!(h.admit(sealed).is_err()); |
| 464 | h.active_sock_untouched(); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | #[tokio::test] |
| 469 | async fn the_log_survives_a_kill_and_reopen_with_no_double_counting() { |
| 470 | if !spark_rsi::not_run::user_scope_or_not_run( |
| 471 | "the_log_survives_a_kill_and_reopen_with_no_double_counting", |
| 472 | ) { |
| 473 | return; |
| 474 | } |
| 475 | let h = harness(); |
| 476 | let mut generation = h.generation(); |
| 477 | let halted = h |
| 478 | .executor( |
| 479 | "correct", |
| 480 | &[], |
| 481 | ExecutorConfig { |
| 482 | halt_after_requests: Some(4), |
| 483 | ..quick() |
| 484 | }, |
| 485 | ) |
| 486 | .run(&mut generation) |
| 487 | .await |
| 488 | .unwrap(); |
| 489 | assert!(matches!(halted, ExecutionOutcome::Halted { .. })); |
| 490 | |
| 491 | // The crash cut the next record in half. |
| 492 | let log = observation_log_path(&h.rsi_root, GEN); |
| 493 | let mut bytes = std::fs::read(&log).unwrap(); |
| 494 | bytes.extend_from_slice(b"{\"kind\":\"observation\",\"record\":{\"observation\":{\"gener"); |
| 495 | std::fs::write(&log, &bytes).unwrap(); |
| 496 | |
| 497 | let mut generation = h.generation(); |
| 498 | let outcome = h |
| 499 | .executor("correct", &[], quick()) |
| 500 | .run(&mut generation) |
| 501 | .await |
| 502 | .unwrap(); |
| 503 | let sealed = sealed_of(&outcome); |
| 504 | assert_eq!(outcome.summary().recovered, 4); |
| 505 | assert_eq!(outcome.summary().issued, 6); |
| 506 | assert_eq!(sealed.observations.len(), 10); |
| 507 | let mut ids: Vec<&str> = sealed |
| 508 | .observations |
| 509 | .iter() |
| 510 | .map(|o| o.observation_id.as_str()) |
| 511 | .collect(); |
| 512 | ids.sort(); |
| 513 | ids.dedup(); |
| 514 | assert_eq!(ids.len(), 10, "no observation counted twice"); |
| 515 | assert!(h.admit(sealed).is_ok()); |
| 516 | |
| 517 | // Nothing may follow the seal, and a sealed log is never reopened by another run. |
| 518 | let again = h |
| 519 | .executor("correct", &[], quick()) |
| 520 | .run(&mut h.generation()) |
| 521 | .await; |
| 522 | assert!(again.is_err()); |
| 523 | } |
| 524 | |
| 525 | #[tokio::test] |
| 526 | async fn a_failure_recorded_before_a_restart_stays_a_failure() { |
| 527 | use spark_rsi::canary::observation::ObservationInput; |
| 528 | use spark_rsi::canary::{CanaryObservationV2, LogHeader, ObservationLogWriter, Provenance}; |
| 529 | |
| 530 | let h = harness(); |
| 531 | // A crashed run left an unsealed log whose only record is a wrong result. |
| 532 | let request = &h.workload.manifest.requests[0]; |
| 533 | let request_digest = request.request_digest(&h.workload.manifest.workload_id); |
| 534 | let mut writer = ObservationLogWriter::create( |
| 535 | &observation_log_path(&h.rsi_root, GEN), |
| 536 | LogHeader::new(GEN, &h.workload.digest, &h.identity.executable_digest, 1), |
| 537 | ) |
| 538 | .unwrap(); |
| 539 | writer |
| 540 | .append_observation(&CanaryObservationV2::new(ObservationInput { |
| 541 | generation_id: GEN, |
| 542 | request_id: &request.request_id, |
| 543 | request_digest: &request_digest, |
| 544 | workload_digest: &h.workload.digest, |
| 545 | candidate_executable_digest: &h.identity.executable_digest, |
| 546 | result_digest: Some(&result_digest(b"nope")), |
| 547 | elapsed_us: 10, |
| 548 | observed_at_ms: 1, |
| 549 | status: VerifiedStatus::WrongResult, |
| 550 | provenance: Provenance::Measured, |
| 551 | })) |
| 552 | .unwrap(); |
| 553 | drop(writer); |
| 554 | |
| 555 | let mut generation = h.generation(); |
| 556 | let outcome = h |
| 557 | .executor("correct", &[], quick()) |
| 558 | .run(&mut generation) |
| 559 | .await |
| 560 | .unwrap(); |
| 561 | let summary = outcome.summary(); |
| 562 | assert!(summary.reverted); |
| 563 | assert!(summary |
| 564 | .first_failure |
| 565 | .as_deref() |
| 566 | .unwrap() |
| 567 | .contains("recorded before a restart")); |
| 568 | assert_eq!( |
| 569 | generation.pid, None, |
| 570 | "no worker is launched for a failed canary" |
| 571 | ); |
| 572 | assert_eq!(generation.state, GenerationState::Reverting); |
| 573 | assert!(h.admit(sealed_of(&outcome)).is_err()); |
| 574 | } |
| 575 | |
| 576 | #[tokio::test] |
| 577 | async fn a_mismatched_executable_digest_refuses_to_launch() { |
| 578 | let mut h = harness(); |
| 579 | h.identity = CandidateIdentity::new( |
| 580 | &result_digest(b"patch"), |
| 581 | &result_digest(b"tree"), |
| 582 | &result_digest(b"some other build"), |
| 583 | GEN, |
| 584 | ); |
| 585 | let mut generation = h.generation(); |
| 586 | let err = h |
| 587 | .executor("correct", &[], ExecutorConfig::default()) |
| 588 | .run(&mut generation) |
| 589 | .await |
| 590 | .unwrap_err(); |
| 591 | assert!(err.contains("refusing to launch"), "{}", err); |
| 592 | assert_eq!(generation.pid, None, "nothing was spawned"); |
| 593 | assert!(!observation_log_path(&h.rsi_root, GEN).exists()); |
| 594 | } |
| 595 | |
| 596 | #[tokio::test] |
| 597 | async fn a_workload_that_does_not_match_its_pin_is_refused() { |
| 598 | let mut h = harness(); |
| 599 | h.workload.manifest.requests[0].expected_output = "anything the candidate likes".to_string(); |
| 600 | let mut generation = h.generation(); |
| 601 | let err = h |
| 602 | .executor("correct", &[], ExecutorConfig::default()) |
| 603 | .run(&mut generation) |
| 604 | .await |
| 605 | .unwrap_err(); |
| 606 | assert!(err.contains("digest"), "{}", err); |
| 607 | } |