aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / spark-harness / tests / provisional_scheduler.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Headstart M2 (#535): provisional scheduling on verified interfaces, |
| 17 | //! final completion only on verified producer completions. |
| 18 | |
| 19 | mod common; |
| 20 | |
| 21 | use common::{enabled, req, COMMIT, H}; |
| 22 | use serde_json::json; |
| 23 | use spark_harness::completion::CompletionError; |
| 24 | use spark_harness::dependency::GraphError; |
| 25 | use spark_harness::execution::TaskState; |
| 26 | use spark_harness::readiness::{ |
| 27 | verify_interface, InterfaceSubmission, OperationClass, ProofClaim, ReadinessLevel, |
| 28 | }; |
| 29 | use spark_harness::scheduler::{ |
| 30 | BindingLevel, Event, EventKind, Outcome, ProvisionalConfig, ResourceLimits, SchedError, |
| 31 | }; |
| 32 | |
| 33 | const IRREVERSIBLE: [OperationClass; 4] = [ |
| 34 | OperationClass::Commit, |
| 35 | OperationClass::Publish, |
| 36 | OperationClass::PersistentMutation, |
| 37 | OperationClass::ExternalEffect, |
| 38 | ]; |
| 39 | |
| 40 | #[test] |
| 41 | fn finalize_rechecks_the_interface_the_consumer_prepared_against() { |
| 42 | let mut h = H::new(enabled()); |
| 43 | two_task(&mut h); |
| 44 | h.publish("a", 1); |
| 45 | h.work_done("b"); |
| 46 | h.finish("a"); |
| 47 | assert!(h.sched.may_finalize("b").is_ok()); |
| 48 | // a's interface changes in the store; the event has not arrived yet. |
| 49 | h.mint_interface("a", 2); |
| 50 | assert!(matches!( |
| 51 | h.sched.may_finalize("b"), |
| 52 | Err(SchedError::Proof { .. }) |
| 53 | )); |
| 54 | assert!(h |
| 55 | .send(EventKind::Finalize { |
| 56 | task: "b".into(), |
| 57 | generation: h.generation("b") |
| 58 | }) |
| 59 | .is_err()); |
| 60 | assert_eq!(state(&h, "b"), TaskState::AwaitingFinalProof); |
| 61 | } |
| 62 | |
| 63 | fn state(h: &H, t: &str) -> TaskState { |
| 64 | h.sched.state(t).unwrap() |
| 65 | } |
| 66 | |
| 67 | fn two_task(h: &mut H) { |
| 68 | h.register("a", 1, &[]); |
| 69 | h.register("b", 1, &["a"]); |
| 70 | } |
| 71 | |
| 72 | fn chain(h: &mut H) { |
| 73 | h.register("a", 1, &[]); |
| 74 | h.register("b", 1, &["a"]); |
| 75 | h.register("c", 1, &["b"]); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn disabled_is_the_default() { |
| 80 | assert!(!ProvisionalConfig::default().enabled); |
| 81 | } |
| 82 | |
| 83 | #[test] |
| 84 | fn two_task_dag_overlaps_preparation_then_finalizes_on_final_proof() { |
| 85 | let mut h = H::new(enabled()); |
| 86 | two_task(&mut h); |
| 87 | assert_eq!(state(&h, "a"), TaskState::Provisional); |
| 88 | assert_eq!(state(&h, "b"), TaskState::Blocked); |
| 89 | assert!(matches!( |
| 90 | h.sched.may_start("b", OperationClass::ReadOnlyAnalysis), |
| 91 | Err(SchedError::WrongState { .. }) |
| 92 | )); |
| 93 | |
| 94 | let (_, fx) = h.publish("a", 1); |
| 95 | assert_eq!(fx.dispatched, vec!["b".to_string()]); |
| 96 | assert_eq!(state(&h, "b"), TaskState::Provisional); |
| 97 | assert_eq!( |
| 98 | h.sched.task("b").unwrap().bindings["a"].level, |
| 99 | BindingLevel::Interface |
| 100 | ); |
| 101 | let grant = h |
| 102 | .sched |
| 103 | .may_start("b", OperationClass::SandboxedCheck) |
| 104 | .unwrap(); |
| 105 | assert_eq!(grant[0].level(), ReadinessLevel::InterfaceVerified); |
| 106 | assert_eq!(grant[0].producer_task(), "a"); |
| 107 | assert_eq!(grant[0].producer_generation(), 1); |
| 108 | |
| 109 | h.advance(100); |
| 110 | h.work_done("b"); |
| 111 | assert_eq!(state(&h, "b"), TaskState::AwaitingFinalProof); |
| 112 | assert!(matches!( |
| 113 | h.sched.may_finalize("b"), |
| 114 | Err(SchedError::ProducerNotFinal { .. }) |
| 115 | )); |
| 116 | assert!(matches!( |
| 117 | h.send(EventKind::Finalize { |
| 118 | task: "b".into(), |
| 119 | generation: h.generation("b") |
| 120 | }), |
| 121 | Err(SchedError::ProducerNotFinal { .. }) |
| 122 | )); |
| 123 | |
| 124 | h.advance(50); |
| 125 | h.finish("a"); |
| 126 | let fin = h.sched.may_finalize("b").unwrap(); |
| 127 | assert_eq!(fin[0].level(), ReadinessLevel::FinalVerified); |
| 128 | h.finalize("b"); |
| 129 | assert_eq!(state(&h, "b"), TaskState::Committable); |
| 130 | |
| 131 | let trace = h.sched.timing_trace(); |
| 132 | println!( |
| 133 | "M2_TIMING_TRACE {}", |
| 134 | serde_json::to_string_pretty(&trace).unwrap() |
| 135 | ); |
| 136 | let b = &trace["tasks"][1]; |
| 137 | assert_eq!(b["task"], "b"); |
| 138 | assert!(b["provisional"].as_u64().unwrap() > 0); |
| 139 | assert!(b["blocked"].as_u64().unwrap() > 0); |
| 140 | // b started while a was still provisional: overlap. |
| 141 | let a_done = h.sched.task("a").unwrap().timing.committable_at.unwrap(); |
| 142 | assert!(h.sched.task("b").unwrap().timing.first_dispatch_at.unwrap() < a_done); |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn three_task_chain_prepares_concurrently_and_finalizes_in_order() { |
| 147 | let mut h = H::new(enabled()); |
| 148 | chain(&mut h); |
| 149 | h.publish("a", 1); |
| 150 | h.publish("b", 1); |
| 151 | for t in ["a", "b", "c"] { |
| 152 | assert_eq!(state(&h, t), TaskState::Provisional, "{t}"); |
| 153 | } |
| 154 | assert_eq!(h.sched.permits_in_use().running, 3); |
| 155 | for op in IRREVERSIBLE { |
| 156 | assert!(h.sched.may_start("c", op).is_err()); |
| 157 | } |
| 158 | // b finished and completed but a is not final: b is not final for c. |
| 159 | h.work_done("b"); |
| 160 | assert!(h |
| 161 | .send(EventKind::Finalize { |
| 162 | task: "b".into(), |
| 163 | generation: h.generation("b") |
| 164 | }) |
| 165 | .is_err()); |
| 166 | h.finish("a"); |
| 167 | h.finalize("b"); |
| 168 | h.complete("b"); |
| 169 | // c's interface binding is upgraded to final once both are final. |
| 170 | let grant = h.sched.may_start("c", OperationClass::Publish).unwrap(); |
| 171 | assert_eq!(grant[0].level(), ReadinessLevel::FinalVerified); |
| 172 | h.work_done("c"); |
| 173 | h.finalize("c"); |
| 174 | assert_eq!(state(&h, "c"), TaskState::Committable); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn no_irreversible_op_from_interface_verified_alone() { |
| 179 | let mut h = H::new(enabled()); |
| 180 | two_task(&mut h); |
| 181 | h.publish("a", 1); |
| 182 | for op in IRREVERSIBLE { |
| 183 | match h.sched.may_start("b", op) { |
| 184 | Err(SchedError::NotPermitted { level, .. }) => { |
| 185 | assert_eq!(level, ReadinessLevel::InterfaceVerified) |
| 186 | } |
| 187 | other => panic!("{op:?} must be refused, got {other:?}"), |
| 188 | } |
| 189 | assert!(h.sched.may_start_named("b", op.as_str()).is_err()); |
| 190 | } |
| 191 | // Finalized without an M1 completion is still not final. |
| 192 | h.work_done("a"); |
| 193 | h.finalize("a"); |
| 194 | for op in IRREVERSIBLE { |
| 195 | assert!(h.sched.may_start("b", op).is_err()); |
| 196 | } |
| 197 | // Only the M1 completion makes it final. |
| 198 | h.complete("a"); |
| 199 | for op in IRREVERSIBLE { |
| 200 | assert!(h.sched.may_start("b", op).is_ok()); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn unknown_operation_names_fail_closed() { |
| 206 | let mut h = H::new(enabled()); |
| 207 | two_task(&mut h); |
| 208 | h.publish("a", 1); |
| 209 | assert!(h.sched.may_start_named("b", "read_only_analysis").is_ok()); |
| 210 | for bad in [ |
| 211 | "deploy", |
| 212 | "Commit", |
| 213 | "READ_ONLY_ANALYSIS", |
| 214 | "", |
| 215 | "sandboxed-check", |
| 216 | ] { |
| 217 | assert!( |
| 218 | matches!( |
| 219 | h.sched.may_start_named("b", bad), |
| 220 | Err(SchedError::UnknownOperation(_)) |
| 221 | ), |
| 222 | "{bad:?}" |
| 223 | ); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | #[test] |
| 228 | fn disabled_flag_keeps_consumers_blocked_until_final() { |
| 229 | let mut h = H::new(ProvisionalConfig { |
| 230 | enabled: false, |
| 231 | ..enabled() |
| 232 | }); |
| 233 | two_task(&mut h); |
| 234 | h.publish("a", 1); |
| 235 | let b = h.sched.task("b").unwrap(); |
| 236 | assert_eq!(b.state, TaskState::Blocked); |
| 237 | assert!(!b.queued); |
| 238 | assert_eq!(h.sched.permits_in_use().running, 1); |
| 239 | h.work_done("a"); |
| 240 | h.finalize("a"); |
| 241 | assert_eq!(state(&h, "b"), TaskState::Blocked); |
| 242 | let generation = h.generation("a"); |
| 243 | let claim = h.mint_completion("a"); |
| 244 | let fx = h.ok(EventKind::CompletionVerified { |
| 245 | task: "a".into(), |
| 246 | generation, |
| 247 | claim, |
| 248 | }); |
| 249 | assert_eq!(fx.dispatched, vec!["b".to_string()]); |
| 250 | assert_eq!( |
| 251 | h.sched.task("b").unwrap().bindings["a"].level, |
| 252 | BindingLevel::Final |
| 253 | ); |
| 254 | assert!(h.sched.may_start("b", OperationClass::Commit).is_ok()); |
| 255 | } |
| 256 | |
| 257 | #[test] |
| 258 | fn cycles_and_missing_nodes_are_refused_without_change() { |
| 259 | let mut h = H::new(enabled()); |
| 260 | chain(&mut h); |
| 261 | let before = h.sched.snapshot(); |
| 262 | let r = h.send(EventKind::AddDependency { |
| 263 | producer: "c".into(), |
| 264 | consumer: "a".into(), |
| 265 | }); |
| 266 | assert!( |
| 267 | matches!(&r, Err(SchedError::Graph(GraphError::Cycle(p))) if p.len() == 4), |
| 268 | "{r:?}" |
| 269 | ); |
| 270 | let r = h.send(EventKind::AddDependency { |
| 271 | producer: "zz".into(), |
| 272 | consumer: "b".into(), |
| 273 | }); |
| 274 | assert!( |
| 275 | matches!(r, Err(SchedError::Graph(GraphError::MissingNode(_)))), |
| 276 | "{r:?}" |
| 277 | ); |
| 278 | let r = h.send(EventKind::RegisterTask { |
| 279 | task: "d".into(), |
| 280 | resources: req(1), |
| 281 | producers: vec!["a".into(), "nope".into()], |
| 282 | }); |
| 283 | assert!( |
| 284 | matches!(r, Err(SchedError::Graph(GraphError::MissingNode(_)))), |
| 285 | "{r:?}" |
| 286 | ); |
| 287 | assert_eq!(h.sched.snapshot(), before); |
| 288 | // A running consumer cannot gain a dependency; a blocked one can. |
| 289 | h.register("e", 1, &[]); |
| 290 | assert!(matches!( |
| 291 | h.send(EventKind::AddDependency { |
| 292 | producer: "b".into(), |
| 293 | consumer: "e".into(), |
| 294 | }), |
| 295 | Err(SchedError::WrongState { .. }) |
| 296 | )); |
| 297 | h.edge("e", "c"); |
| 298 | h.publish("a", 1); |
| 299 | h.publish("b", 1); |
| 300 | assert_eq!(state(&h, "c"), TaskState::Blocked, "c still waits for e"); |
| 301 | h.publish("e", 1); |
| 302 | assert_eq!(state(&h, "c"), TaskState::Provisional); |
| 303 | } |
| 304 | |
| 305 | #[test] |
| 306 | fn cancellation_propagates_to_descendants_and_frees_permits() { |
| 307 | let mut h = H::new(enabled()); |
| 308 | chain(&mut h); |
| 309 | h.register("d", 1, &[]); |
| 310 | h.publish("a", 1); |
| 311 | h.publish("b", 1); |
| 312 | let fx = h.ok(EventKind::Cancel { |
| 313 | task: "a".into(), |
| 314 | generation: h.generation("a"), |
| 315 | }); |
| 316 | assert_eq!(fx.aborted, vec!["a", "b", "c"]); |
| 317 | for t in ["a", "b", "c"] { |
| 318 | assert_eq!(state(&h, t), TaskState::Aborted); |
| 319 | } |
| 320 | assert_eq!(state(&h, "d"), TaskState::Provisional); |
| 321 | assert_eq!(h.sched.permits_in_use().running, 1); |
| 322 | } |
| 323 | |
| 324 | #[test] |
| 325 | fn stale_generations_are_rejected_and_cannot_finalize() { |
| 326 | let mut h = H::new(enabled()); |
| 327 | two_task(&mut h); |
| 328 | let (old_claim, _) = h.publish("a", 1); |
| 329 | h.work_done("b"); |
| 330 | let old_completion = h.mint_completion("a"); |
| 331 | let fx = h.ok(EventKind::NewGeneration { |
| 332 | task: "a".into(), |
| 333 | generation: 2, |
| 334 | }); |
| 335 | assert_eq!(fx.revoked, vec!["a", "b"]); |
| 336 | let b = h.sched.task("b").unwrap(); |
| 337 | assert_eq!(b.state, TaskState::Blocked); |
| 338 | assert_eq!(b.generation, 2); |
| 339 | assert!(b.provisional_result.is_none()); |
| 340 | for kind in [ |
| 341 | EventKind::InterfacePublished { |
| 342 | task: "a".into(), |
| 343 | generation: 1, |
| 344 | claim: old_claim.clone(), |
| 345 | }, |
| 346 | EventKind::CompletionVerified { |
| 347 | task: "a".into(), |
| 348 | generation: 1, |
| 349 | claim: old_completion.clone(), |
| 350 | }, |
| 351 | EventKind::NewGeneration { |
| 352 | task: "a".into(), |
| 353 | generation: 2, |
| 354 | }, |
| 355 | ] { |
| 356 | assert!( |
| 357 | matches!(h.send(kind), Err(SchedError::StaleGeneration { .. })), |
| 358 | "stale event accepted" |
| 359 | ); |
| 360 | } |
| 361 | // A generation-1 receipt claimed as generation 2 fails the binding. |
| 362 | let r = h.send(EventKind::InterfacePublished { |
| 363 | task: "a".into(), |
| 364 | generation: 2, |
| 365 | claim: old_claim, |
| 366 | }); |
| 367 | assert!(matches!(r, Err(SchedError::Proof { .. })), "{r:?}"); |
| 368 | let r = h.send(EventKind::WorkDone { |
| 369 | task: "b".into(), |
| 370 | generation: 1, |
| 371 | result_digest: "late".into(), |
| 372 | }); |
| 373 | assert!( |
| 374 | matches!(r, Err(SchedError::StaleGeneration { .. })), |
| 375 | "{r:?}" |
| 376 | ); |
| 377 | // The engine itself refuses a stale interface generation. |
| 378 | let mut stale = InterfaceSubmission { |
| 379 | task_id: "a".into(), |
| 380 | generation: 2, |
| 381 | source_commit: COMMIT.into(), |
| 382 | interface: json!({"v": 2}), |
| 383 | }; |
| 384 | h.engine.publish_interface(&stale).unwrap(); |
| 385 | stale.generation = 1; |
| 386 | assert!(matches!( |
| 387 | h.engine.publish_interface(&stale), |
| 388 | Err(CompletionError::StaleGeneration { .. }) |
| 389 | )); |
| 390 | } |
| 391 | |
| 392 | #[test] |
| 393 | fn producer_failure_revokes_descendants_and_discards_results() { |
| 394 | let mut h = H::new(enabled()); |
| 395 | chain(&mut h); |
| 396 | h.publish("a", 1); |
| 397 | h.publish("b", 1); |
| 398 | h.work_done("b"); |
| 399 | assert!(h.sched.task("b").unwrap().provisional_result.is_some()); |
| 400 | let fx = h.ok(EventKind::ProducerFailed { |
| 401 | task: "a".into(), |
| 402 | generation: 1, |
| 403 | reason: "build broke".into(), |
| 404 | }); |
| 405 | assert_eq!(fx.aborted, vec!["a"]); |
| 406 | assert_eq!(fx.revoked, vec!["b", "c"]); |
| 407 | for t in ["b", "c"] { |
| 408 | let e = h.sched.task(t).unwrap(); |
| 409 | assert_eq!(e.state, TaskState::Blocked); |
| 410 | assert!(e.provisional_result.is_none() && e.bindings.is_empty() && e.interface.is_none()); |
| 411 | assert_eq!(e.generation, 2); |
| 412 | } |
| 413 | assert_eq!(h.sched.permits_in_use().running, 0); |
| 414 | } |
| 415 | |
| 416 | #[test] |
| 417 | fn retry_after_failure_runs_to_committable() { |
| 418 | let mut h = H::new(enabled()); |
| 419 | two_task(&mut h); |
| 420 | h.publish("a", 1); |
| 421 | h.ok(EventKind::ProducerFailed { |
| 422 | task: "a".into(), |
| 423 | generation: 1, |
| 424 | reason: "flaky".into(), |
| 425 | }); |
| 426 | assert!(h |
| 427 | .send(EventKind::Retry { |
| 428 | task: "b".into(), |
| 429 | generation: h.generation("b") |
| 430 | }) |
| 431 | .is_err()); |
| 432 | let fx = h.ok(EventKind::Retry { |
| 433 | task: "a".into(), |
| 434 | generation: h.generation("a"), |
| 435 | }); |
| 436 | assert_eq!(fx.dispatched, vec!["a"]); |
| 437 | assert_eq!(h.generation("a"), 2); |
| 438 | h.publish("a", 1); |
| 439 | assert_eq!(state(&h, "b"), TaskState::Provisional); |
| 440 | assert_eq!(h.sched.task("b").unwrap().bindings["a"].generation, 2); |
| 441 | h.work_done("b"); |
| 442 | h.finish("a"); |
| 443 | h.finalize("b"); |
| 444 | assert_eq!(state(&h, "b"), TaskState::Committable); |
| 445 | } |
| 446 | |
| 447 | #[test] |
| 448 | fn interface_change_at_same_generation_revokes_and_rebinds() { |
| 449 | let mut h = H::new(enabled()); |
| 450 | two_task(&mut h); |
| 451 | let (v1, _) = h.publish("a", 1); |
| 452 | h.work_done("b"); |
| 453 | let (v2, fx) = h.publish("a", 2); |
| 454 | assert_ne!(v1, v2); |
| 455 | assert_eq!(fx.revoked, vec!["b"]); |
| 456 | assert_eq!(fx.dispatched, vec!["b"]); |
| 457 | let b = h.sched.task("b").unwrap(); |
| 458 | assert_eq!(b.bindings["a"].claim, v2); |
| 459 | assert!(b.provisional_result.is_none()); |
| 460 | assert_eq!((b.timing.revocations, b.timing.dispatches), (1, 2)); |
| 461 | let store = &h.engine.completion_store; |
| 462 | assert!(matches!( |
| 463 | verify_interface(store, "a", 1, &v1), |
| 464 | Err(CompletionError::NotCurrentCompletion(_)) |
| 465 | )); |
| 466 | // Republishing the same interface is idempotent: same receipt. |
| 467 | assert_eq!(h.mint_interface("a", 2), v2); |
| 468 | } |
| 469 | |
| 470 | #[test] |
| 471 | fn duplicate_event_delivery_is_idempotent() { |
| 472 | let mut h = H::new(enabled()); |
| 473 | let ev = Event { |
| 474 | id: "reg-a".into(), |
| 475 | kind: EventKind::RegisterTask { |
| 476 | task: "a".into(), |
| 477 | resources: req(1), |
| 478 | producers: vec![], |
| 479 | }, |
| 480 | }; |
| 481 | assert!(matches!( |
| 482 | h.sched.deliver(ev.clone()), |
| 483 | Ok(Outcome::Applied(_)) |
| 484 | )); |
| 485 | let before = h.sched.snapshot(); |
| 486 | assert_eq!(h.sched.deliver(ev), Ok(Outcome::Duplicate)); |
| 487 | assert_eq!(h.sched.snapshot(), before); |
| 488 | let conflict = Event { |
| 489 | id: "reg-a".into(), |
| 490 | kind: EventKind::RegisterTask { |
| 491 | task: "other".into(), |
| 492 | resources: req(1), |
| 493 | producers: vec![], |
| 494 | }, |
| 495 | }; |
| 496 | assert!(matches!( |
| 497 | h.sched.deliver(conflict), |
| 498 | Err(SchedError::EventIdConflict(_)) |
| 499 | )); |
| 500 | assert!(h.sched.task("other").is_none()); |
| 501 | } |
| 502 | |
| 503 | #[test] |
| 504 | fn forged_digest_missing_and_wrong_kind_proofs_are_refused() { |
| 505 | let mut h = H::new(enabled()); |
| 506 | two_task(&mut h); |
| 507 | let good = h.mint_interface("a", 1); |
| 508 | let completion = h.mint_completion("a"); |
| 509 | let forged_digest = ProofClaim { |
| 510 | digest: "0".repeat(64), |
| 511 | ..good.clone() |
| 512 | }; |
| 513 | let missing = ProofClaim { |
| 514 | receipt_id: "a".repeat(64), |
| 515 | ..good.clone() |
| 516 | }; |
| 517 | let wrong_commit = ProofClaim { |
| 518 | commit: "2".repeat(40), |
| 519 | ..good.clone() |
| 520 | }; |
| 521 | let before = h.sched.snapshot(); |
| 522 | for (claim, interface) in [ |
| 523 | (forged_digest, true), |
| 524 | (missing, true), |
| 525 | (wrong_commit, true), |
| 526 | (completion.clone(), true), |
| 527 | (good.clone(), false), |
| 528 | ] { |
| 529 | let kind = if interface { |
| 530 | EventKind::InterfacePublished { |
| 531 | task: "a".into(), |
| 532 | generation: 1, |
| 533 | claim, |
| 534 | } |
| 535 | } else { |
| 536 | EventKind::CompletionVerified { |
| 537 | task: "a".into(), |
| 538 | generation: 1, |
| 539 | claim, |
| 540 | } |
| 541 | }; |
| 542 | let r = h.send(kind); |
| 543 | assert!(matches!(r, Err(SchedError::Proof { .. })), "{r:?}"); |
| 544 | } |
| 545 | assert_eq!(h.sched.snapshot(), before); |
| 546 | assert_eq!(state(&h, "b"), TaskState::Blocked); |
| 547 | |
| 548 | // Receipts cannot be smuggled in through the submission. |
| 549 | let smuggled = json!({ |
| 550 | "task_id": "a", "generation": 1, "source_commit": COMMIT, |
| 551 | "interface": {"x": 1}, "receipt_id": good.receipt_id |
| 552 | }); |
| 553 | assert!(serde_json::from_value::<InterfaceSubmission>(smuggled).is_err()); |
| 554 | let not_object = InterfaceSubmission { |
| 555 | task_id: "a".into(), |
| 556 | generation: 1, |
| 557 | source_commit: COMMIT.into(), |
| 558 | interface: json!("just a string"), |
| 559 | }; |
| 560 | assert!(matches!( |
| 561 | h.engine.publish_interface(¬_object), |
| 562 | Err(CompletionError::InvalidSubmission(_)) |
| 563 | )); |
| 564 | } |
| 565 | |
| 566 | #[test] |
| 567 | fn tampered_store_fails_gates_and_audit_revokes() { |
| 568 | let mut h = H::new(enabled()); |
| 569 | two_task(&mut h); |
| 570 | let (claim, _) = h.publish("a", 1); |
| 571 | assert!(h |
| 572 | .sched |
| 573 | .may_start("b", OperationClass::ReadOnlyAnalysis) |
| 574 | .is_ok()); |
| 575 | let path = h |
| 576 | .engine |
| 577 | .completion_store |
| 578 | .join("receipts") |
| 579 | .join(format!("{}.json", claim.receipt_id)); |
| 580 | let text = std::fs::read_to_string(&path).unwrap(); |
| 581 | std::fs::write(&path, text.replace("interface:shape", "interface:shapf")).unwrap(); |
| 582 | assert!(matches!( |
| 583 | h.sched.may_start("b", OperationClass::ReadOnlyAnalysis), |
| 584 | Err(SchedError::Proof { .. }) |
| 585 | )); |
| 586 | // The producer whose own claim fails is revoked, which cascades to b; |
| 587 | // a re-runs at a new generation, b waits for a new interface. |
| 588 | let revoked = h.sched.audit().unwrap(); |
| 589 | assert_eq!(revoked, vec!["a", "b"]); |
| 590 | assert_eq!(state(&h, "a"), TaskState::Provisional); |
| 591 | assert_eq!(h.generation("a"), 2); |
| 592 | assert_eq!(state(&h, "b"), TaskState::Blocked); |
| 593 | assert_eq!(h.sched.audit().unwrap(), Vec::<String>::new()); |
| 594 | h.sched.check_invariants().unwrap(); |
| 595 | } |
| 596 | |
| 597 | #[test] |
| 598 | fn resource_budget_bounds_queue_and_permits() { |
| 599 | let mut h = H::new(ProvisionalConfig { |
| 600 | enabled: true, |
| 601 | queue_capacity: 1, |
| 602 | limits: ResourceLimits { |
| 603 | cpu_slots: 3, |
| 604 | memory_bytes: 1 << 30, |
| 605 | max_concurrency: 2, |
| 606 | }, |
| 607 | lease_ttl: 1_000, |
| 608 | dedup_window: 64, |
| 609 | }); |
| 610 | let r = h.send(EventKind::RegisterTask { |
| 611 | task: "huge".into(), |
| 612 | resources: req(4), |
| 613 | producers: vec![], |
| 614 | }); |
| 615 | assert!(matches!(r, Err(SchedError::ExceedsCapacity(_))), "{r:?}"); |
| 616 | for t in ["x", "y", "z"] { |
| 617 | h.register(t, 2, &[]); |
| 618 | } |
| 619 | assert_eq!(state(&h, "x"), TaskState::Provisional); |
| 620 | assert_eq!(h.sched.queue(), vec!["y"]); |
| 621 | let z = h.sched.task("z").unwrap(); |
| 622 | assert_eq!((z.state, z.queued), (TaskState::Blocked, false)); |
| 623 | assert_eq!(h.sched.permits_in_use().cpu_in_use, 2); |
| 624 | let fx = h.work_done("x"); |
| 625 | assert_eq!(fx.dispatched, vec!["y"]); |
| 626 | assert_eq!(h.sched.queue(), vec!["z"]); |
| 627 | h.work_done("y"); |
| 628 | assert_eq!(state(&h, "z"), TaskState::Provisional); |
| 629 | h.register("w", 1, &[]); |
| 630 | assert_eq!(h.sched.permits_in_use().cpu_in_use, 3); |
| 631 | // Concurrency bound: after z finishes, two 1-slot tasks would fit the |
| 632 | // CPU budget next to w, but only max_concurrency = 2 tasks run. |
| 633 | h.work_done("z"); |
| 634 | h.register("u", 1, &[]); |
| 635 | h.register("v", 1, &[]); |
| 636 | let pool = h.sched.permits_in_use(); |
| 637 | assert_eq!((pool.running, pool.cpu_in_use), (2, 2)); |
| 638 | assert_eq!(h.sched.queue(), vec!["v"]); |
| 639 | assert_eq!(state(&h, "v"), TaskState::Blocked); |
| 640 | } |
| 641 | |
| 642 | #[test] |
| 643 | fn blocked_tasks_hold_no_permits() { |
| 644 | let mut h = H::new(enabled()); |
| 645 | h.register("a", 3, &[]); |
| 646 | h.register("b", 3, &["a"]); |
| 647 | h.register("c", 3, &["a"]); |
| 648 | let pool = h.sched.permits_in_use(); |
| 649 | assert_eq!((pool.running, pool.cpu_in_use), (1, 3)); |
| 650 | h.publish("a", 1); |
| 651 | // b fits (6 of 8 slots); c would need 9 and waits in the queue. |
| 652 | assert_eq!(state(&h, "b"), TaskState::Provisional); |
| 653 | let c = h.sched.task("c").unwrap(); |
| 654 | assert_eq!((c.state, c.queued), (TaskState::Blocked, true)); |
| 655 | assert!(c.lease_expires_at.is_none()); |
| 656 | assert_eq!(h.sched.permits_in_use().cpu_in_use, 6); |
| 657 | // Revocation returns b to Blocked and frees its permits; c is then |
| 658 | // blocked too (a has no interface at its new generation). |
| 659 | h.ok(EventKind::NewGeneration { |
| 660 | task: "a".into(), |
| 661 | generation: 2, |
| 662 | }); |
| 663 | for t in ["b", "c"] { |
| 664 | assert_eq!(state(&h, t), TaskState::Blocked); |
| 665 | } |
| 666 | let pool = h.sched.permits_in_use(); |
| 667 | assert_eq!((pool.running, pool.cpu_in_use), (1, 3)); |
| 668 | } |
| 669 | |
| 670 | #[test] |
| 671 | fn leases_expire_into_abort_and_heartbeats_renew() { |
| 672 | let mut h = H::new(enabled()); |
| 673 | two_task(&mut h); |
| 674 | h.publish("a", 1); |
| 675 | h.advance(900); |
| 676 | h.ok(EventKind::Heartbeat { |
| 677 | task: "a".into(), |
| 678 | generation: h.generation("a"), |
| 679 | beat: 1, |
| 680 | }); |
| 681 | h.advance(900); |
| 682 | assert!(h.sched.may_start("a", OperationClass::Commit).is_ok()); |
| 683 | h.advance(200); |
| 684 | assert!(matches!( |
| 685 | h.sched.may_start("a", OperationClass::Commit), |
| 686 | Err(SchedError::LeaseExpired(_)) |
| 687 | )); |
| 688 | assert!(matches!( |
| 689 | h.send(EventKind::WorkDone { |
| 690 | task: "a".into(), |
| 691 | generation: 1, |
| 692 | result_digest: "late".into() |
| 693 | }), |
| 694 | Err(SchedError::LeaseExpired(_)) |
| 695 | )); |
| 696 | let fx = h.ok(EventKind::Tick); |
| 697 | assert_eq!(fx.aborted, vec!["a"]); |
| 698 | assert_eq!(fx.revoked, vec!["b"]); |
| 699 | assert_eq!(h.sched.permits_in_use().running, 0); |
| 700 | } |