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 / workflow_gates.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 M1 (#533): completion gates are enforced, fail closed, and |
| 17 | //! bound to a harness-minted aien-proof receipt. |
| 18 | |
| 19 | use aien_proof::evidence::{self, Verdict}; |
| 20 | use serde_json::{json, Value}; |
| 21 | use spark_harness::completion::{ |
| 22 | artifact_digest, verify_completion, CompletionError, CompletionSubmission, |
| 23 | }; |
| 24 | use spark_harness::{HarnessEngine, WorkflowState, DONE_STATE}; |
| 25 | use std::fs; |
| 26 | use std::path::{Path, PathBuf}; |
| 27 | |
| 28 | const COMMIT: &str = "1111111111111111111111111111111111111111"; |
| 29 | const OTHER_COMMIT: &str = "2222222222222222222222222222222222222222"; |
| 30 | |
| 31 | /// Temp copy of the shipped definitions, removed on drop. |
| 32 | struct Fixture { |
| 33 | dir: PathBuf, |
| 34 | } |
| 35 | |
| 36 | impl Fixture { |
| 37 | fn new() -> Self { |
| 38 | let dir = |
| 39 | std::env::temp_dir().join(format!("spark-harness-gates-{}", uuid::Uuid::new_v4())); |
| 40 | let src = Path::new(env!("CARGO_MANIFEST_DIR")); |
| 41 | fs::create_dir_all(dir.join("schemas")).unwrap(); |
| 42 | for f in ["workflows.json", "evals.json"] { |
| 43 | fs::copy(src.join(f), dir.join(f)).unwrap(); |
| 44 | } |
| 45 | for entry in fs::read_dir(src.join("schemas")).unwrap() { |
| 46 | let entry = entry.unwrap(); |
| 47 | fs::copy(entry.path(), dir.join("schemas").join(entry.file_name())).unwrap(); |
| 48 | } |
| 49 | Fixture { dir } |
| 50 | } |
| 51 | |
| 52 | fn engine(&self) -> HarnessEngine { |
| 53 | HarnessEngine::load(&self.dir).expect("shipped definitions load") |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | impl Drop for Fixture { |
| 58 | fn drop(&mut self) { |
| 59 | let _ = fs::remove_dir_all(&self.dir); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn wf_at(workflow_type: &str, state: &str) -> WorkflowState { |
| 64 | WorkflowState { |
| 65 | id: format!("wf-{workflow_type}"), |
| 66 | workflow_type: workflow_type.to_string(), |
| 67 | current_state: state.to_string(), |
| 68 | history: Vec::new(), |
| 69 | created_at: "2026-10-15T00:00:00Z".to_string(), |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | fn sub(task: &str, generation: u64, artifact: Value) -> CompletionSubmission { |
| 74 | CompletionSubmission { |
| 75 | task_id: task.to_string(), |
| 76 | generation, |
| 77 | source_commit: COMMIT.to_string(), |
| 78 | artifact, |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn engineering_fix_artifact() -> Value { |
| 83 | json!({ |
| 84 | "bug": "Parser rejects valid token sequence", |
| 85 | "reproduction_steps": ["Run parser test"], |
| 86 | "failure_evidence": "thread panicked at line 42", |
| 87 | "regression_test": "tests::test_parser", |
| 88 | "diff_summary": "Fixed token parser logic", |
| 89 | "retest_evidence": "All tests passing cleanly" |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | fn research_handoff_artifact() -> Value { |
| 94 | json!({ |
| 95 | "question": "Which serde_json feature changes map ordering?", |
| 96 | "claims": [{ |
| 97 | "claim": "preserve_order switches Map to an insertion ordered map", |
| 98 | "source_url": "https://docs.rs/serde_json", |
| 99 | "excerpt": "preserve_order: use IndexMap instead of BTreeMap" |
| 100 | }], |
| 101 | "sources": ["https://docs.rs/serde_json"], |
| 102 | "open_questions": [], |
| 103 | "evidence": ["Cargo.lock serde_json entry lists no indexmap"] |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | fn harness_change_artifact() -> Value { |
| 108 | json!({ |
| 109 | "change": "Tighten the slop gate phrase list", |
| 110 | "why": "Replay showed two missed tropes", |
| 111 | "replay_report": "12 of 12 replayed failures now caught", |
| 112 | "eval_results": ["slop_gate: pass"], |
| 113 | "rollback": "Revert the evals.json commit", |
| 114 | "evidence": ["replay log 2026-10-15"] |
| 115 | }) |
| 116 | } |
| 117 | |
| 118 | fn python_result_artifact() -> Value { |
| 119 | json!({ |
| 120 | "goal": "Sum a column", |
| 121 | "code": "print(sum([1, 2, 3]))", |
| 122 | "stdout": "6\n", |
| 123 | "ok": "yes", |
| 124 | "evidence": ["stdout matched expected 6"] |
| 125 | }) |
| 126 | } |
| 127 | |
| 128 | fn receipt_count(engine: &HarnessEngine) -> usize { |
| 129 | evidence::list_receipts(&engine.completion_store).len() |
| 130 | } |
| 131 | |
| 132 | fn gate_failures(err: CompletionError) -> (String, Vec<String>) { |
| 133 | match err { |
| 134 | CompletionError::GatesFailed { |
| 135 | receipt_id, |
| 136 | failures, |
| 137 | } => (receipt_id, failures), |
| 138 | other => panic!("expected GatesFailed, got {other:?}"), |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // ---------- success for all four shipped workflows ---------- |
| 143 | |
| 144 | fn assert_completes(workflow_type: &str, pre_done: &str, artifact: Value) { |
| 145 | let fx = Fixture::new(); |
| 146 | let engine = fx.engine(); |
| 147 | let mut wf = wf_at(workflow_type, pre_done); |
| 148 | let s = sub(&format!("task-{workflow_type}"), 1, artifact.clone()); |
| 149 | let rec = engine.complete_verified_workflow(&mut wf, &s).unwrap(); |
| 150 | assert_eq!(wf.current_state, DONE_STATE); |
| 151 | assert_eq!(wf.history, vec![pre_done.to_string()]); |
| 152 | assert_eq!(rec.artifact_digest, artifact_digest(&artifact)); |
| 153 | assert_eq!(receipt_count(&engine), 1); |
| 154 | |
| 155 | let receipt = evidence::load_receipt(&engine.completion_store, &rec.receipt_id).unwrap(); |
| 156 | assert_eq!(receipt.result, Verdict::Pass); |
| 157 | assert_eq!(receipt.commit, COMMIT); |
| 158 | assert_eq!( |
| 159 | receipt.output_artifacts, |
| 160 | vec![format!("blake3:{}", rec.artifact_digest)] |
| 161 | ); |
| 162 | let ids: Vec<&str> = receipt.assertions.iter().map(|a| a.id.as_str()).collect(); |
| 163 | let def = &engine.workflows[workflow_type]; |
| 164 | assert!(ids.contains(&format!("schema:{}", def["required_schema"].as_str().unwrap()).as_str())); |
| 165 | for e in def["required_evals_on_done"].as_array().unwrap() { |
| 166 | assert!(ids.contains(&format!("eval:{}", e.as_str().unwrap()).as_str())); |
| 167 | } |
| 168 | |
| 169 | let v = verify_completion( |
| 170 | &engine.completion_store, |
| 171 | &rec.receipt_id, |
| 172 | &s.task_id, |
| 173 | 1, |
| 174 | COMMIT, |
| 175 | &rec.artifact_digest, |
| 176 | ) |
| 177 | .unwrap(); |
| 178 | assert_eq!(v.workflow_id, wf.id); |
| 179 | assert_eq!(v.generation, 1); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn engineering_fix_completes() { |
| 184 | assert_completes("engineering_fix", "review", engineering_fix_artifact()); |
| 185 | } |
| 186 | |
| 187 | #[test] |
| 188 | fn research_handoff_completes() { |
| 189 | assert_completes("research_handoff", "deliver", research_handoff_artifact()); |
| 190 | } |
| 191 | |
| 192 | #[test] |
| 193 | fn harness_change_completes() { |
| 194 | assert_completes("harness_change", "verify", harness_change_artifact()); |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn python_pipeline_completes() { |
| 199 | assert_completes("python_pipeline", "deliver", python_result_artifact()); |
| 200 | } |
| 201 | |
| 202 | // ---------- gate failures ---------- |
| 203 | |
| 204 | fn assert_fails_gate( |
| 205 | engine: &HarnessEngine, |
| 206 | workflow_type: &str, |
| 207 | pre_done: &str, |
| 208 | artifact: Value, |
| 209 | failing: &str, |
| 210 | ) { |
| 211 | let mut wf = wf_at(workflow_type, pre_done); |
| 212 | let before = receipt_count(engine); |
| 213 | let err = engine |
| 214 | .complete_verified_workflow(&mut wf, &sub("task-fail", 1, artifact.clone())) |
| 215 | .unwrap_err(); |
| 216 | let (receipt_id, failures) = gate_failures(err); |
| 217 | assert!( |
| 218 | failures.iter().any(|f| f.starts_with(failing)), |
| 219 | "expected {failing} in {failures:?}" |
| 220 | ); |
| 221 | assert_eq!(wf.current_state, pre_done, "state must not move on failure"); |
| 222 | assert!(wf.history.is_empty()); |
| 223 | // The failure is recorded as a FAIL receipt, which never verifies. |
| 224 | assert_eq!(receipt_count(engine), before + 1); |
| 225 | let r = evidence::load_receipt(&engine.completion_store, &receipt_id).unwrap(); |
| 226 | assert_eq!(r.result, Verdict::Fail); |
| 227 | let v = verify_completion( |
| 228 | &engine.completion_store, |
| 229 | &receipt_id, |
| 230 | "task-fail", |
| 231 | 1, |
| 232 | COMMIT, |
| 233 | &artifact_digest(&artifact), |
| 234 | ); |
| 235 | assert!(matches!(v, Err(CompletionError::NotPass(_))), "{v:?}"); |
| 236 | // A failure does not consume the generation: a fixed artifact may follow. |
| 237 | } |
| 238 | |
| 239 | #[test] |
| 240 | fn schema_failure_blocks_done() { |
| 241 | let fx = Fixture::new(); |
| 242 | let engine = fx.engine(); |
| 243 | let mut art = engineering_fix_artifact(); |
| 244 | art.as_object_mut().unwrap().remove("retest_evidence"); |
| 245 | assert_fails_gate( |
| 246 | &engine, |
| 247 | "engineering_fix", |
| 248 | "review", |
| 249 | art, |
| 250 | "schema:engineering_fix", |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | #[test] |
| 255 | fn reproduction_present_failure_blocks_done() { |
| 256 | let fx = Fixture::new(); |
| 257 | let engine = fx.engine(); |
| 258 | let mut art = engineering_fix_artifact(); |
| 259 | // Passes the schema (minLength 8) but is blank to the eval. |
| 260 | art["failure_evidence"] = json!(" "); |
| 261 | assert!(engine.validate_schema("engineering_fix", &art).is_ok()); |
| 262 | assert_fails_gate( |
| 263 | &engine, |
| 264 | "engineering_fix", |
| 265 | "review", |
| 266 | art, |
| 267 | "eval:reproduction_present", |
| 268 | ); |
| 269 | } |
| 270 | |
| 271 | #[test] |
| 272 | fn required_evidence_failure_blocks_done() { |
| 273 | let fx = Fixture::new(); |
| 274 | let engine = fx.engine(); |
| 275 | let mut art = harness_change_artifact(); |
| 276 | art.as_object_mut().unwrap().remove("evidence"); |
| 277 | assert!(engine.validate_schema("harness_change", &art).is_ok()); |
| 278 | assert_fails_gate( |
| 279 | &engine, |
| 280 | "harness_change", |
| 281 | "verify", |
| 282 | art, |
| 283 | "eval:required_evidence", |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | #[test] |
| 288 | fn slop_gate_failure_blocks_done() { |
| 289 | let fx = Fixture::new(); |
| 290 | let engine = fx.engine(); |
| 291 | let mut art = research_handoff_artifact(); |
| 292 | art["claims"][0]["claim"] = json!("This finding is a game-changer for ordering"); |
| 293 | assert!(engine.validate_schema("research_handoff", &art).is_ok()); |
| 294 | assert_fails_gate( |
| 295 | &engine, |
| 296 | "research_handoff", |
| 297 | "deliver", |
| 298 | art, |
| 299 | "eval:slop_gate", |
| 300 | ); |
| 301 | } |
| 302 | |
| 303 | #[test] |
| 304 | fn unknown_eval_rule_fails_closed() { |
| 305 | let fx = Fixture::new(); |
| 306 | let mut engine = fx.engine(); |
| 307 | engine.workflows.get_mut("python_pipeline").unwrap()["required_evals_on_done"] = |
| 308 | json!(["required_evidence", "no_such_eval"]); |
| 309 | assert_fails_gate( |
| 310 | &engine, |
| 311 | "python_pipeline", |
| 312 | "deliver", |
| 313 | python_result_artifact(), |
| 314 | "eval:no_such_eval", |
| 315 | ); |
| 316 | } |
| 317 | |
| 318 | #[test] |
| 319 | fn judgment_eval_cannot_gate_completion() { |
| 320 | let fx = Fixture::new(); |
| 321 | let mut engine = fx.engine(); |
| 322 | engine.workflows.get_mut("python_pipeline").unwrap()["required_evals_on_done"] = |
| 323 | json!(["source_supports_claim"]); |
| 324 | assert_fails_gate( |
| 325 | &engine, |
| 326 | "python_pipeline", |
| 327 | "deliver", |
| 328 | python_result_artifact(), |
| 329 | "eval:source_supports_claim", |
| 330 | ); |
| 331 | } |
| 332 | |
| 333 | #[test] |
| 334 | fn eval_without_definition_fails_closed() { |
| 335 | let fx = Fixture::new(); |
| 336 | let mut engine = fx.engine(); |
| 337 | engine.evals.remove("required_evidence"); |
| 338 | assert_fails_gate( |
| 339 | &engine, |
| 340 | "python_pipeline", |
| 341 | "deliver", |
| 342 | python_result_artifact(), |
| 343 | "eval:required_evidence", |
| 344 | ); |
| 345 | } |
| 346 | |
| 347 | #[test] |
| 348 | fn missing_schema_file_fails_closed() { |
| 349 | let fx = Fixture::new(); |
| 350 | fs::remove_file(fx.dir.join("schemas/engineering_fix.json")).unwrap(); |
| 351 | let engine = fx.engine(); |
| 352 | assert_fails_gate( |
| 353 | &engine, |
| 354 | "engineering_fix", |
| 355 | "review", |
| 356 | engineering_fix_artifact(), |
| 357 | "schema:engineering_fix", |
| 358 | ); |
| 359 | } |
| 360 | |
| 361 | #[test] |
| 362 | fn invalid_schema_file_fails_closed() { |
| 363 | let fx = Fixture::new(); |
| 364 | fs::write(fx.dir.join("schemas/python_result.json"), "{ not json").unwrap(); |
| 365 | let engine = fx.engine(); |
| 366 | assert_fails_gate( |
| 367 | &engine, |
| 368 | "python_pipeline", |
| 369 | "deliver", |
| 370 | python_result_artifact(), |
| 371 | "schema:python_result", |
| 372 | ); |
| 373 | fs::write(fx.dir.join("schemas/python_result.json"), r#"{"type": 12}"#).unwrap(); |
| 374 | assert_fails_gate( |
| 375 | &engine, |
| 376 | "python_pipeline", |
| 377 | "deliver", |
| 378 | python_result_artifact(), |
| 379 | "schema:python_result", |
| 380 | ); |
| 381 | } |
| 382 | |
| 383 | // ---------- fail-closed loading ---------- |
| 384 | |
| 385 | #[test] |
| 386 | fn malformed_workflows_json_blocks_completion() { |
| 387 | let fx = Fixture::new(); |
| 388 | fs::write(fx.dir.join("workflows.json"), "{ \"engineering_fix\": ").unwrap(); |
| 389 | assert!(HarnessEngine::load(&fx.dir).is_err()); |
| 390 | let engine = HarnessEngine::new(&fx.dir); |
| 391 | assert!(!engine.load_errors.is_empty()); |
| 392 | let mut wf = wf_at("engineering_fix", "review"); |
| 393 | let err = engine |
| 394 | .complete_verified_workflow(&mut wf, &sub("t", 1, engineering_fix_artifact())) |
| 395 | .unwrap_err(); |
| 396 | assert!( |
| 397 | matches!(err, CompletionError::DefinitionsNotLoaded(_)), |
| 398 | "{err:?}" |
| 399 | ); |
| 400 | assert_eq!(wf.current_state, "review"); |
| 401 | assert_eq!(receipt_count(&engine), 0); |
| 402 | } |
| 403 | |
| 404 | #[test] |
| 405 | fn structurally_bad_workflow_rejected_at_load() { |
| 406 | let fx = Fixture::new(); |
| 407 | let mut defs: Value = |
| 408 | serde_json::from_str(&fs::read_to_string(fx.dir.join("workflows.json")).unwrap()).unwrap(); |
| 409 | defs["engineering_fix"] |
| 410 | .as_object_mut() |
| 411 | .unwrap() |
| 412 | .remove("required_schema"); |
| 413 | fs::write(fx.dir.join("workflows.json"), defs.to_string()).unwrap(); |
| 414 | let err = HarnessEngine::load(&fx.dir).unwrap_err(); |
| 415 | assert!(err.contains("required_schema"), "{err}"); |
| 416 | } |
| 417 | |
| 418 | #[test] |
| 419 | fn missing_evals_json_blocks_completion() { |
| 420 | let fx = Fixture::new(); |
| 421 | fs::remove_file(fx.dir.join("evals.json")).unwrap(); |
| 422 | assert!(HarnessEngine::load(&fx.dir).is_err()); |
| 423 | let engine = HarnessEngine::new(&fx.dir); |
| 424 | let mut wf = wf_at("python_pipeline", "deliver"); |
| 425 | let err = engine |
| 426 | .complete_verified_workflow(&mut wf, &sub("t", 1, python_result_artifact())) |
| 427 | .unwrap_err(); |
| 428 | assert!( |
| 429 | matches!(err, CompletionError::DefinitionsNotLoaded(_)), |
| 430 | "{err:?}" |
| 431 | ); |
| 432 | } |
| 433 | |
| 434 | // ---------- forged success flags ---------- |
| 435 | |
| 436 | #[test] |
| 437 | fn forged_success_flag_in_artifact_is_not_proof() { |
| 438 | let fx = Fixture::new(); |
| 439 | let engine = fx.engine(); |
| 440 | let mut art = python_result_artifact(); |
| 441 | art["evidence"] = json!([]); |
| 442 | art["passed"] = json!(true); |
| 443 | art["verified"] = json!(true); |
| 444 | art["receipt"] = json!({"result": "PASS"}); |
| 445 | assert_fails_gate( |
| 446 | &engine, |
| 447 | "python_pipeline", |
| 448 | "deliver", |
| 449 | art, |
| 450 | "schema:python_result", |
| 451 | ); |
| 452 | } |
| 453 | |
| 454 | #[test] |
| 455 | fn forged_fields_in_submission_are_rejected() { |
| 456 | for extra in ["passed", "receipt", "verdict"] { |
| 457 | let mut raw = json!({ |
| 458 | "task_id": "t", |
| 459 | "generation": 1, |
| 460 | "source_commit": COMMIT, |
| 461 | "artifact": python_result_artifact() |
| 462 | }); |
| 463 | raw[extra] = json!(true); |
| 464 | assert!( |
| 465 | serde_json::from_value::<CompletionSubmission>(raw).is_err(), |
| 466 | "{extra}" |
| 467 | ); |
| 468 | } |
| 469 | // Same through the MCP adapter, which must answer with an explicit error. |
| 470 | let fx = Fixture::new(); |
| 471 | let engine = fx.engine(); |
| 472 | let args = json!({ |
| 473 | "workflow": wf_at("python_pipeline", "deliver"), |
| 474 | "submission": { |
| 475 | "task_id": "t", "generation": 1, "source_commit": COMMIT, |
| 476 | "artifact": python_result_artifact(), "passed": true |
| 477 | } |
| 478 | }); |
| 479 | let err = engine.handle_complete_request(&args).unwrap_err(); |
| 480 | assert!(matches!(err, CompletionError::InvalidSubmission(_))); |
| 481 | assert_eq!(err.rpc_error()["code"], json!(-32602)); |
| 482 | assert_eq!(receipt_count(&engine), 0); |
| 483 | } |
| 484 | |
| 485 | #[test] |
| 486 | fn bad_commit_is_rejected_before_evaluation() { |
| 487 | let fx = Fixture::new(); |
| 488 | let engine = fx.engine(); |
| 489 | let mut wf = wf_at("python_pipeline", "deliver"); |
| 490 | let mut s = sub("t", 1, python_result_artifact()); |
| 491 | s.source_commit = "HEAD".to_string(); |
| 492 | let err = engine.complete_verified_workflow(&mut wf, &s).unwrap_err(); |
| 493 | assert!( |
| 494 | matches!(err, CompletionError::InvalidSubmission(_)), |
| 495 | "{err:?}" |
| 496 | ); |
| 497 | assert_eq!(receipt_count(&engine), 0); |
| 498 | } |
| 499 | |
| 500 | // ---------- state progression ---------- |
| 501 | |
| 502 | #[test] |
| 503 | fn advance_workflow_to_done_is_refused() { |
| 504 | let fx = Fixture::new(); |
| 505 | let engine = fx.engine(); |
| 506 | let mut wf = wf_at("engineering_fix", "review"); |
| 507 | let err = engine.advance_workflow(&mut wf, DONE_STATE).unwrap_err(); |
| 508 | assert!(err.contains("complete_verified_workflow"), "{err}"); |
| 509 | assert_eq!(wf.current_state, "review"); |
| 510 | assert!(wf.history.is_empty()); |
| 511 | // Other legal transitions still work. |
| 512 | engine.advance_workflow(&mut wf, "implement").unwrap(); |
| 513 | assert_eq!(wf.current_state, "implement"); |
| 514 | } |
| 515 | |
| 516 | #[test] |
| 517 | fn completion_from_state_that_cannot_reach_done() { |
| 518 | let fx = Fixture::new(); |
| 519 | let engine = fx.engine(); |
| 520 | for state in ["implement", "nonexistent_state"] { |
| 521 | let mut wf = wf_at("engineering_fix", state); |
| 522 | let err = engine |
| 523 | .complete_verified_workflow(&mut wf, &sub("t", 1, engineering_fix_artifact())) |
| 524 | .unwrap_err(); |
| 525 | assert!( |
| 526 | matches!(err, CompletionError::IllegalTransition { .. }), |
| 527 | "{err:?}" |
| 528 | ); |
| 529 | assert_eq!(wf.current_state, state); |
| 530 | } |
| 531 | assert_eq!(receipt_count(&engine), 0); |
| 532 | } |
| 533 | |
| 534 | #[test] |
| 535 | fn unknown_workflow_type_is_refused() { |
| 536 | let fx = Fixture::new(); |
| 537 | let engine = fx.engine(); |
| 538 | let mut wf = wf_at("no_such_flow", "review"); |
| 539 | let err = engine |
| 540 | .complete_verified_workflow(&mut wf, &sub("t", 1, engineering_fix_artifact())) |
| 541 | .unwrap_err(); |
| 542 | assert!( |
| 543 | matches!(err, CompletionError::UnknownWorkflow(_)), |
| 544 | "{err:?}" |
| 545 | ); |
| 546 | } |
| 547 | |
| 548 | // ---------- duplicates, replay, generations ---------- |
| 549 | |
| 550 | #[test] |
| 551 | fn duplicate_completion_on_done_workflow_is_refused() { |
| 552 | let fx = Fixture::new(); |
| 553 | let engine = fx.engine(); |
| 554 | let mut wf = wf_at("engineering_fix", "review"); |
| 555 | let s = sub("dup", 1, engineering_fix_artifact()); |
| 556 | engine.complete_verified_workflow(&mut wf, &s).unwrap(); |
| 557 | let err = engine.complete_verified_workflow(&mut wf, &s).unwrap_err(); |
| 558 | assert!( |
| 559 | matches!(err, CompletionError::IllegalTransition { .. }), |
| 560 | "{err:?}" |
| 561 | ); |
| 562 | assert_eq!(receipt_count(&engine), 1); |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn replayed_submission_mints_no_second_receipt() { |
| 567 | let fx = Fixture::new(); |
| 568 | let engine = fx.engine(); |
| 569 | let snapshot = wf_at("engineering_fix", "review"); |
| 570 | let s = sub("replay", 1, engineering_fix_artifact()); |
| 571 | let mut first = snapshot.clone(); |
| 572 | let rec = engine.complete_verified_workflow(&mut first, &s).unwrap(); |
| 573 | |
| 574 | // Replay with a stale copy of the workflow that still says "review". |
| 575 | let mut replay = snapshot.clone(); |
| 576 | let err = engine |
| 577 | .complete_verified_workflow(&mut replay, &s) |
| 578 | .unwrap_err(); |
| 579 | match err { |
| 580 | CompletionError::DuplicateCompletion { |
| 581 | receipt_id, |
| 582 | generation, |
| 583 | .. |
| 584 | } => { |
| 585 | assert_eq!(receipt_id, rec.receipt_id); |
| 586 | assert_eq!(generation, 1); |
| 587 | } |
| 588 | other => panic!("expected DuplicateCompletion, got {other:?}"), |
| 589 | } |
| 590 | assert_eq!(replay.current_state, "review"); |
| 591 | assert_eq!( |
| 592 | evidence::list_receipts(&engine.completion_store), |
| 593 | vec![rec.receipt_id.clone()] |
| 594 | ); |
| 595 | |
| 596 | // A fresh engine on the same store sees the durable record too. |
| 597 | let engine2 = fx.engine(); |
| 598 | let mut replay2 = snapshot.clone(); |
| 599 | let err = engine2 |
| 600 | .complete_verified_workflow(&mut replay2, &s) |
| 601 | .unwrap_err(); |
| 602 | assert!( |
| 603 | matches!(err, CompletionError::DuplicateCompletion { .. }), |
| 604 | "{err:?}" |
| 605 | ); |
| 606 | assert_eq!(receipt_count(&engine2), 1); |
| 607 | |
| 608 | // Through MCP the replay is an explicit JSON-RPC error. |
| 609 | let args = json!({ "workflow": snapshot, "submission": s }); |
| 610 | let rpc = engine2 |
| 611 | .handle_complete_request(&args) |
| 612 | .unwrap_err() |
| 613 | .rpc_error(); |
| 614 | assert_eq!(rpc["code"], json!(-32005)); |
| 615 | assert_eq!(rpc["data"]["kind"], json!("duplicate_completion")); |
| 616 | } |
| 617 | |
| 618 | #[test] |
| 619 | fn stale_and_newer_generations() { |
| 620 | let fx = Fixture::new(); |
| 621 | let engine = fx.engine(); |
| 622 | let mut wf = wf_at("engineering_fix", "review"); |
| 623 | engine |
| 624 | .complete_verified_workflow(&mut wf, &sub("gen", 5, engineering_fix_artifact())) |
| 625 | .unwrap(); |
| 626 | |
| 627 | let mut lower = wf_at("engineering_fix", "review"); |
| 628 | let err = engine |
| 629 | .complete_verified_workflow(&mut lower, &sub("gen", 3, engineering_fix_artifact())) |
| 630 | .unwrap_err(); |
| 631 | assert_eq!( |
| 632 | err, |
| 633 | CompletionError::StaleGeneration { |
| 634 | task_id: "gen".to_string(), |
| 635 | submitted: 3, |
| 636 | recorded: 5 |
| 637 | } |
| 638 | ); |
| 639 | assert_eq!(receipt_count(&engine), 1); |
| 640 | |
| 641 | // A newer generation (reworked task) is a new completion. |
| 642 | let mut newer = wf_at("engineering_fix", "review"); |
| 643 | let rec = engine |
| 644 | .complete_verified_workflow(&mut newer, &sub("gen", 6, engineering_fix_artifact())) |
| 645 | .unwrap(); |
| 646 | assert_eq!(receipt_count(&engine), 2); |
| 647 | let mut back = wf_at("engineering_fix", "review"); |
| 648 | assert!(matches!( |
| 649 | engine.complete_verified_workflow(&mut back, &sub("gen", 5, engineering_fix_artifact())), |
| 650 | Err(CompletionError::StaleGeneration { recorded: 6, .. }) |
| 651 | )); |
| 652 | // The receipt for generation 6 does not verify as generation 5. |
| 653 | assert!(matches!( |
| 654 | verify_completion( |
| 655 | &engine.completion_store, |
| 656 | &rec.receipt_id, |
| 657 | "gen", |
| 658 | 5, |
| 659 | COMMIT, |
| 660 | &rec.artifact_digest |
| 661 | ), |
| 662 | Err(CompletionError::BindingMismatch { |
| 663 | field: "generation", |
| 664 | .. |
| 665 | }) |
| 666 | )); |
| 667 | } |
| 668 | |
| 669 | #[test] |
| 670 | fn corrupt_consumed_record_fails_closed() { |
| 671 | let fx = Fixture::new(); |
| 672 | let engine = fx.engine(); |
| 673 | let mut wf = wf_at("engineering_fix", "review"); |
| 674 | engine |
| 675 | .complete_verified_workflow(&mut wf, &sub("corrupt", 1, engineering_fix_artifact())) |
| 676 | .unwrap(); |
| 677 | let dir = engine.completion_store.join("completions"); |
| 678 | for entry in fs::read_dir(&dir).unwrap() { |
| 679 | let p = entry.unwrap().path(); |
| 680 | if p.extension().is_some_and(|e| e == "json") { |
| 681 | fs::write(&p, "garbage").unwrap(); |
| 682 | } |
| 683 | } |
| 684 | let mut again = wf_at("engineering_fix", "review"); |
| 685 | let err = engine |
| 686 | .complete_verified_workflow(&mut again, &sub("corrupt", 2, engineering_fix_artifact())) |
| 687 | .unwrap_err(); |
| 688 | assert!(matches!(err, CompletionError::Store(_)), "{err:?}"); |
| 689 | assert_eq!(again.current_state, "review"); |
| 690 | } |
| 691 | |
| 692 | // ---------- verify_completion bindings ---------- |
| 693 | |
| 694 | fn completed() -> (Fixture, HarnessEngine, String, String) { |
| 695 | let fx = Fixture::new(); |
| 696 | let engine = fx.engine(); |
| 697 | let mut wf = wf_at("python_pipeline", "deliver"); |
| 698 | let rec = engine |
| 699 | .complete_verified_workflow(&mut wf, &sub("verify-me", 7, python_result_artifact())) |
| 700 | .unwrap(); |
| 701 | (fx, engine, rec.receipt_id, rec.artifact_digest) |
| 702 | } |
| 703 | |
| 704 | #[test] |
| 705 | fn verify_completion_accepts_exact_bindings() { |
| 706 | let (_fx, engine, id, digest) = completed(); |
| 707 | let v = verify_completion( |
| 708 | &engine.completion_store, |
| 709 | &id, |
| 710 | "verify-me", |
| 711 | 7, |
| 712 | COMMIT, |
| 713 | &digest, |
| 714 | ) |
| 715 | .unwrap(); |
| 716 | assert_eq!(v.receipt_id, id); |
| 717 | let prefixed = format!("blake3:{digest}"); |
| 718 | assert!(verify_completion( |
| 719 | &engine.completion_store, |
| 720 | &id, |
| 721 | "verify-me", |
| 722 | 7, |
| 723 | COMMIT, |
| 724 | &prefixed |
| 725 | ) |
| 726 | .is_ok()); |
| 727 | } |
| 728 | |
| 729 | #[test] |
| 730 | fn verify_completion_wrong_digest() { |
| 731 | let (_fx, engine, id, _) = completed(); |
| 732 | let mut other = python_result_artifact(); |
| 733 | other["stdout"] = json!("7\n"); |
| 734 | let err = verify_completion( |
| 735 | &engine.completion_store, |
| 736 | &id, |
| 737 | "verify-me", |
| 738 | 7, |
| 739 | COMMIT, |
| 740 | &artifact_digest(&other), |
| 741 | ) |
| 742 | .unwrap_err(); |
| 743 | assert!( |
| 744 | matches!( |
| 745 | err, |
| 746 | CompletionError::BindingMismatch { |
| 747 | field: "artifact_digest", |
| 748 | .. |
| 749 | } |
| 750 | ), |
| 751 | "{err:?}" |
| 752 | ); |
| 753 | } |
| 754 | |
| 755 | #[test] |
| 756 | fn verify_completion_wrong_commit() { |
| 757 | let (_fx, engine, id, digest) = completed(); |
| 758 | let err = verify_completion( |
| 759 | &engine.completion_store, |
| 760 | &id, |
| 761 | "verify-me", |
| 762 | 7, |
| 763 | OTHER_COMMIT, |
| 764 | &digest, |
| 765 | ) |
| 766 | .unwrap_err(); |
| 767 | assert!( |
| 768 | matches!( |
| 769 | err, |
| 770 | CompletionError::BindingMismatch { |
| 771 | field: "commit", |
| 772 | .. |
| 773 | } |
| 774 | ), |
| 775 | "{err:?}" |
| 776 | ); |
| 777 | } |
| 778 | |
| 779 | #[test] |
| 780 | fn verify_completion_wrong_task() { |
| 781 | let (_fx, engine, id, digest) = completed(); |
| 782 | let err = verify_completion( |
| 783 | &engine.completion_store, |
| 784 | &id, |
| 785 | "someone-else", |
| 786 | 7, |
| 787 | COMMIT, |
| 788 | &digest, |
| 789 | ) |
| 790 | .unwrap_err(); |
| 791 | assert!( |
| 792 | matches!(err, CompletionError::BindingMismatch { field: "task", .. }), |
| 793 | "{err:?}" |
| 794 | ); |
| 795 | } |
| 796 | |
| 797 | #[test] |
| 798 | fn verify_completion_missing_receipt() { |
| 799 | let (_fx, engine, _, digest) = completed(); |
| 800 | let err = verify_completion( |
| 801 | &engine.completion_store, |
| 802 | &"a".repeat(64), |
| 803 | "verify-me", |
| 804 | 7, |
| 805 | COMMIT, |
| 806 | &digest, |
| 807 | ) |
| 808 | .unwrap_err(); |
| 809 | assert!(matches!(err, CompletionError::ReceiptInvalid(_)), "{err:?}"); |
| 810 | } |
| 811 | |
| 812 | #[test] |
| 813 | fn verify_completion_tampered_receipt_file() { |
| 814 | let (_fx, engine, id, digest) = completed(); |
| 815 | let path = engine |
| 816 | .completion_store |
| 817 | .join(evidence::RECEIPT_DIR) |
| 818 | .join(format!("{id}.json")); |
| 819 | let mut r: Value = serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); |
| 820 | // Rebind the receipt to another task without re-minting it. |
| 821 | r["external_refs"] = json!([ |
| 822 | "task:someone-else", |
| 823 | "workflow:wf-python_pipeline", |
| 824 | "generation:7" |
| 825 | ]); |
| 826 | fs::write(&path, serde_json::to_string_pretty(&r).unwrap()).unwrap(); |
| 827 | let err = verify_completion( |
| 828 | &engine.completion_store, |
| 829 | &id, |
| 830 | "someone-else", |
| 831 | 7, |
| 832 | COMMIT, |
| 833 | &digest, |
| 834 | ) |
| 835 | .unwrap_err(); |
| 836 | assert!(matches!(err, CompletionError::ReceiptInvalid(_)), "{err:?}"); |
| 837 | } |
| 838 | |
| 839 | #[test] |
| 840 | fn mcp_success_returns_record_and_done_state() { |
| 841 | let fx = Fixture::new(); |
| 842 | let engine = fx.engine(); |
| 843 | let args = json!({ |
| 844 | "workflow": wf_at("harness_change", "verify"), |
| 845 | "submission": sub("mcp-task", 1, harness_change_artifact()) |
| 846 | }); |
| 847 | let out = engine.handle_complete_request(&args).unwrap(); |
| 848 | assert_eq!(out["workflow"]["current_state"], json!(DONE_STATE)); |
| 849 | let id = out["record"]["receipt_id"].as_str().unwrap(); |
| 850 | let digest = out["record"]["artifact_digest"].as_str().unwrap(); |
| 851 | assert!(verify_completion(&engine.completion_store, id, "mcp-task", 1, COMMIT, digest).is_ok()); |
| 852 | } |
| 853 | |
| 854 | // ---------- consumed record is the authority (inspector finding 1) ---------- |
| 855 | |
| 856 | fn consumed_records(engine: &HarnessEngine) -> Vec<PathBuf> { |
| 857 | let dir = engine.completion_store.join("completions"); |
| 858 | fs::read_dir(&dir) |
| 859 | .map(|it| { |
| 860 | it.flatten() |
| 861 | .map(|e| e.path()) |
| 862 | .filter(|p| p.extension().is_some_and(|e| e == "json")) |
| 863 | .collect() |
| 864 | }) |
| 865 | .unwrap_or_default() |
| 866 | } |
| 867 | |
| 868 | fn assert_not_current(r: Result<spark_harness::completion::VerifiedCompletion, CompletionError>) { |
| 869 | match r { |
| 870 | Err(CompletionError::NotCurrentCompletion(_)) => {} |
| 871 | other => panic!("expected NotCurrentCompletion, got {other:?}"), |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | /// Real write failure: the consumed-record directory is read-only, so |
| 876 | /// write_consumed fails after store_receipt succeeded. |
| 877 | #[test] |
| 878 | fn orphan_receipt_from_failed_record_write_never_verifies() { |
| 879 | use std::os::unix::fs::PermissionsExt; |
| 880 | let fx = Fixture::new(); |
| 881 | let mut engine = fx.engine(); |
| 882 | engine.completion_clock = || 1_000; |
| 883 | let dir = engine.completion_store.join("completions"); |
| 884 | fs::create_dir_all(&dir).unwrap(); |
| 885 | fs::File::create(dir.join(".lock")).unwrap(); |
| 886 | fs::set_permissions(&dir, fs::Permissions::from_mode(0o555)).unwrap(); |
| 887 | let probe = dir.join("probe"); |
| 888 | if fs::File::create(&probe).is_ok() { |
| 889 | // Running with permission override (root): the injection cannot work. |
| 890 | let _ = fs::remove_file(&probe); |
| 891 | fs::set_permissions(&dir, fs::Permissions::from_mode(0o755)).unwrap(); |
| 892 | eprintln!("skipping permission injection: directory stayed writable"); |
| 893 | return; |
| 894 | } |
| 895 | |
| 896 | let art_a = python_result_artifact(); |
| 897 | let mut wf = wf_at("python_pipeline", "deliver"); |
| 898 | let err = engine |
| 899 | .complete_verified_workflow(&mut wf, &sub("orphan", 1, art_a.clone())) |
| 900 | .unwrap_err(); |
| 901 | fs::set_permissions(&dir, fs::Permissions::from_mode(0o755)).unwrap(); |
| 902 | assert!(matches!(err, CompletionError::Store(_)), "{err:?}"); |
| 903 | assert_eq!( |
| 904 | wf.current_state, "deliver", |
| 905 | "no done without a consumed record" |
| 906 | ); |
| 907 | let ids = evidence::list_receipts(&engine.completion_store); |
| 908 | assert_eq!(ids.len(), 1); |
| 909 | let orphan = ids[0].clone(); |
| 910 | let r = evidence::load_receipt(&engine.completion_store, &orphan).unwrap(); |
| 911 | assert_eq!( |
| 912 | r.result, |
| 913 | Verdict::Pass, |
| 914 | "the orphan is a well-formed PASS receipt" |
| 915 | ); |
| 916 | assert_not_current(verify_completion( |
| 917 | &engine.completion_store, |
| 918 | &orphan, |
| 919 | "orphan", |
| 920 | 1, |
| 921 | COMMIT, |
| 922 | &artifact_digest(&art_a), |
| 923 | )); |
| 924 | |
| 925 | // Retry: the SAME task, generation, commit and artifact, one second |
| 926 | // later. Every binding the verifier checks is identical for both |
| 927 | // receipts; only the timestamp (and therefore the ID) differs. |
| 928 | engine.completion_clock = || 1_001; |
| 929 | let mut wf2 = wf_at("python_pipeline", "deliver"); |
| 930 | let rec = engine |
| 931 | .complete_verified_workflow(&mut wf2, &sub("orphan", 1, art_a.clone())) |
| 932 | .unwrap(); |
| 933 | assert_eq!(wf2.current_state, "done"); |
| 934 | assert_ne!(rec.receipt_id, orphan, "retry must mint a distinct receipt"); |
| 935 | assert_eq!(receipt_count(&engine), 2); |
| 936 | // Exactly one of the two PASS receipts verifies: the receipt-id match |
| 937 | // against the consumed record is the only thing that separates them. |
| 938 | assert_not_current(verify_completion( |
| 939 | &engine.completion_store, |
| 940 | &orphan, |
| 941 | "orphan", |
| 942 | 1, |
| 943 | COMMIT, |
| 944 | &artifact_digest(&art_a), |
| 945 | )); |
| 946 | assert!(verify_completion( |
| 947 | &engine.completion_store, |
| 948 | &rec.receipt_id, |
| 949 | "orphan", |
| 950 | 1, |
| 951 | COMMIT, |
| 952 | &artifact_digest(&art_a) |
| 953 | ) |
| 954 | .is_ok()); |
| 955 | } |
| 956 | |
| 957 | /// Simulated write failure: the consumed record vanishes after a completion. |
| 958 | #[test] |
| 959 | fn receipt_without_consumed_record_never_verifies() { |
| 960 | let (_fx, engine, id, digest) = completed(); |
| 961 | for p in consumed_records(&engine) { |
| 962 | fs::remove_file(p).unwrap(); |
| 963 | } |
| 964 | assert_not_current(verify_completion( |
| 965 | &engine.completion_store, |
| 966 | &id, |
| 967 | "verify-me", |
| 968 | 7, |
| 969 | COMMIT, |
| 970 | &digest, |
| 971 | )); |
| 972 | } |
| 973 | |
| 974 | #[test] |
| 975 | fn corrupt_consumed_record_blocks_verification() { |
| 976 | let (_fx, engine, id, digest) = completed(); |
| 977 | for p in consumed_records(&engine) { |
| 978 | fs::write(p, "garbage").unwrap(); |
| 979 | } |
| 980 | let err = verify_completion( |
| 981 | &engine.completion_store, |
| 982 | &id, |
| 983 | "verify-me", |
| 984 | 7, |
| 985 | COMMIT, |
| 986 | &digest, |
| 987 | ) |
| 988 | .unwrap_err(); |
| 989 | assert!(matches!(err, CompletionError::Store(_)), "{err:?}"); |
| 990 | } |
| 991 | |
| 992 | #[test] |
| 993 | fn superseded_generation_stops_verifying() { |
| 994 | let fx = Fixture::new(); |
| 995 | let engine = fx.engine(); |
| 996 | let art = engineering_fix_artifact(); |
| 997 | let mut wf1 = wf_at("engineering_fix", "review"); |
| 998 | let r1 = engine |
| 999 | .complete_verified_workflow(&mut wf1, &sub("super", 1, art.clone())) |
| 1000 | .unwrap(); |
| 1001 | assert!(verify_completion( |
| 1002 | &engine.completion_store, |
| 1003 | &r1.receipt_id, |
| 1004 | "super", |
| 1005 | 1, |
| 1006 | COMMIT, |
| 1007 | &r1.artifact_digest |
| 1008 | ) |
| 1009 | .is_ok()); |
| 1010 | let mut wf2 = wf_at("engineering_fix", "review"); |
| 1011 | let r2 = engine |
| 1012 | .complete_verified_workflow(&mut wf2, &sub("super", 2, art)) |
| 1013 | .unwrap(); |
| 1014 | assert_not_current(verify_completion( |
| 1015 | &engine.completion_store, |
| 1016 | &r1.receipt_id, |
| 1017 | "super", |
| 1018 | 1, |
| 1019 | COMMIT, |
| 1020 | &r1.artifact_digest, |
| 1021 | )); |
| 1022 | assert!(verify_completion( |
| 1023 | &engine.completion_store, |
| 1024 | &r2.receipt_id, |
| 1025 | "super", |
| 1026 | 2, |
| 1027 | COMMIT, |
| 1028 | &r2.artifact_digest |
| 1029 | ) |
| 1030 | .is_ok()); |
| 1031 | } |
| 1032 | |
| 1033 | // ---------- deterministic-kind guard (inspector finding 3) ---------- |
| 1034 | |
| 1035 | #[test] |
| 1036 | fn executable_eval_marked_judgment_is_refused() { |
| 1037 | let fx = Fixture::new(); |
| 1038 | let mut engine = fx.engine(); |
| 1039 | // run_eval can execute required_evidence and would pass this artifact; |
| 1040 | // only the kind guard can refuse it. |
| 1041 | let art = python_result_artifact(); |
| 1042 | assert!(engine.run_eval("required_evidence", &art).passed); |
| 1043 | engine.evals.get_mut("required_evidence").unwrap()["kind"] = json!("judgment"); |
| 1044 | let mut wf = wf_at("python_pipeline", "deliver"); |
| 1045 | let err = engine |
| 1046 | .complete_verified_workflow(&mut wf, &sub("kind", 1, art)) |
| 1047 | .unwrap_err(); |
| 1048 | let (_, failures) = gate_failures(err); |
| 1049 | assert!( |
| 1050 | failures |
| 1051 | .iter() |
| 1052 | .any(|f| f.starts_with("eval:required_evidence") && f.contains("judgment")), |
| 1053 | "{failures:?}" |
| 1054 | ); |
| 1055 | assert_eq!(wf.current_state, "deliver"); |
| 1056 | } |