interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / tools / aien-test / src / verdict.rs

808 lines · 28567 bytes Raw Blame History
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//! Verdict set and derivation (ADR 0028 Decision 5). A test program only
17//! reports observations; the verdict is derived here from the manifest's
18//! expectations and facts the runner measured. Only the last row can yield
19//! PASS.
20//!
21//! Scope (Slice B): rows 2 to 15 are implemented as below, including the
22//! hardware verdict (row 3) and the dependency verdicts (rows 4 and 5). Where
23//! the runner still cannot do what a row presumes (build, symbol checks,
24//! operator attestations) it refuses with NOT_RUN or BLOCKED_OPERATOR and a
25//! reason code; it never guesses a PASS.
26//!
27//! Deviation from the ADR table order (recorded): timeout (row 14) is tested
28//! before the exit status (row 10), because a killed process has no exit code
29//! and would otherwise always be reported as rc_nonzero.
30
31use crate::manifest::{parse_json_scalar, valid_obs_name, Manifest, Op, Quant, Rule};
32use serde_json::Value;
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub enum Verdict {
36 Pass,
37 Fail,
38 NotRun,
39 BlockedHardware,
40 BlockedOperator,
41 MissingImplementation,
42}
43
44impl Verdict {
45 pub const ALL: [Verdict; 6] = [
46 Verdict::Pass,
47 Verdict::Fail,
48 Verdict::NotRun,
49 Verdict::BlockedHardware,
50 Verdict::BlockedOperator,
51 Verdict::MissingImplementation,
52 ];
53
54 /// The verdict with this exact name, as written by `as_str`.
55 pub fn parse(s: &str) -> Option<Verdict> {
56 Verdict::ALL.into_iter().find(|v| v.as_str() == s)
57 }
58
59 pub fn as_str(self) -> &'static str {
60 match self {
61 Verdict::Pass => "PASS",
62 Verdict::Fail => "FAIL",
63 Verdict::NotRun => "NOT_RUN",
64 Verdict::BlockedHardware => "BLOCKED_HARDWARE",
65 Verdict::BlockedOperator => "BLOCKED_OPERATOR",
66 Verdict::MissingImplementation => "MISSING_IMPLEMENTATION",
67 }
68 }
69
70 /// CLI exit code: 0 PASS, 1 FAIL, 2 NOT_RUN, 3 BLOCKED_HARDWARE,
71 /// 4 BLOCKED_OPERATOR, 5 MISSING_IMPLEMENTATION.
72 pub fn exit_code(self) -> i32 {
73 match self {
74 Verdict::Pass => 0,
75 Verdict::Fail => 1,
76 Verdict::NotRun => 2,
77 Verdict::BlockedHardware => 3,
78 Verdict::BlockedOperator => 4,
79 Verdict::MissingImplementation => 5,
80 }
81 }
82}
83
84/// The verdict of a campaign of gates: any FAIL makes it FAIL, otherwise the
85/// highest-numbered of NOT_RUN, BLOCKED_HARDWARE, BLOCKED_OPERATOR and
86/// MISSING_IMPLEMENTATION (ADR 0028 Decision 5), otherwise PASS. An empty list
87/// is PASS here; a caller that ran nothing must say so itself.
88pub fn campaign_verdict(vs: &[Verdict]) -> Verdict {
89 if vs.contains(&Verdict::Fail) {
90 return Verdict::Fail;
91 }
92 vs.iter()
93 .copied()
94 .max_by_key(|v| v.exit_code())
95 .unwrap_or(Verdict::Pass)
96}
97
98#[derive(Debug, Clone, PartialEq)]
99pub struct Observation {
100 pub name: String,
101 pub value: Value,
102}
103
104/// Parse `AIEN-OBS <name> <json-value>` lines. Anything else, including a
105/// malformed AIEN-OBS line (bad name, bad JSON, float), is ordinary log text.
106pub fn parse_observations(stdout: &[u8]) -> Vec<Observation> {
107 let text = String::from_utf8_lossy(stdout);
108 let mut out = Vec::new();
109 for line in text.lines() {
110 let rest = match line.strip_prefix("AIEN-OBS ") {
111 Some(r) => r,
112 None => continue,
113 };
114 let (name, json) = match rest.split_once(' ') {
115 Some(p) => p,
116 None => continue,
117 };
118 if !valid_obs_name(name) {
119 continue;
120 }
121 if let Some(value) = parse_json_scalar(json.trim()) {
122 out.push(Observation {
123 name: name.to_string(),
124 value,
125 });
126 }
127 }
128 out
129}
130
131fn compare(op: Op, a: &Value, b: &Value) -> bool {
132 match op {
133 Op::Eq => a == b,
134 Op::Ne => a != b,
135 _ => match (a.as_i64(), b.as_i64()) {
136 (Some(x), Some(y)) => match op {
137 Op::Lt => x < y,
138 Op::Le => x <= y,
139 Op::Gt => x > y,
140 _ => x >= y,
141 },
142 _ => false,
143 },
144 }
145}
146
147pub fn eval_rule(rule: &Rule, obs: &[Observation]) -> bool {
148 let vals: Vec<&Value> = obs
149 .iter()
150 .filter(|o| o.name == rule.name)
151 .map(|o| &o.value)
152 .collect();
153 if vals.is_empty() {
154 return false;
155 }
156 match rule.quant {
157 Quant::Last => compare(rule.op, vals[vals.len() - 1], &rule.value),
158 Quant::All => vals.iter().all(|v| compare(rule.op, v, &rule.value)),
159 Quant::Any => vals.iter().any(|v| compare(rule.op, v, &rule.value)),
160 }
161}
162
163/// What the runner knows about one dependency of a gate: its current verdict
164/// (None when there is no usable receipt: absent, or not for this commit) and
165/// the receipt that verdict came from.
166#[derive(Debug, Clone, PartialEq, Eq)]
167pub struct DepFact {
168 pub gate: String,
169 pub verdict: Option<Verdict>,
170 pub receipt: Option<String>,
171}
172
173fn dep_verdict(deps: &[DepFact], gate: &str) -> Option<Verdict> {
174 deps.iter().find(|d| d.gate == gate).and_then(|d| d.verdict)
175}
176
177/// Facts the runner measured. For a gate that did not run, `exit_status` is
178/// None and `observations` is empty.
179#[derive(Debug, Clone, Default)]
180pub struct Facts {
181 /// Refusal id decided before running (dirty_tree, evidence_inside, ...).
182 pub refusal: Option<String>,
183 pub exec_exists: bool,
184 /// Current verdicts of the gate's `depends_on` gates. A dependency with no
185 /// entry here counts as having no verdict.
186 pub deps: Vec<DepFact>,
187 /// `machine.gb10_present` and `machine.qemu_available` (row 3).
188 pub gb10_present: bool,
189 pub qemu_available: bool,
190 pub exit_status: Option<i64>,
191 pub observations: Vec<Observation>,
192 pub timed_out: bool,
193 /// `git status --porcelain` differed after the run.
194 pub tree_changed_after: bool,
195 pub head_moved: bool,
196 /// The operator started this run by hand (`--operator`). It is the only
197 /// thing that lets a `requires: operator` gate run (ADR 0033 Decision 2).
198 pub operator: bool,
199 /// The manifest's `prepare` step ran and did not exit 0 (or timed out),
200 /// so `run` never started.
201 pub prepare_failed: bool,
202}
203
204#[derive(Debug, Clone, PartialEq)]
205pub struct Derived {
206 pub verdict: Verdict,
207 pub reason: String,
208 /// Each `expects.observe` rule and whether it held.
209 pub rules: Vec<(String, bool)>,
210}
211
212/// Rows 2 to 7: decided before anything runs. Some(..) means do not run.
213pub fn pre_run(m: &Manifest, f: &Facts) -> Option<(Verdict, String)> {
214 if !f.operator && m.requires.iter().any(|r| r == "operator") {
215 // Row 2: an operator gate is never scheduled automatically (ADR 0033
216 // Decision 2). The operator starting the run by hand (`--operator`)
217 // is the attestation; nothing else lifts this (sc #614).
218 return Some((
219 Verdict::BlockedOperator,
220 "OPERATOR_STEP_PENDING".to_string(),
221 ));
222 }
223 // Row 3: the gate's pool needs hardware this machine does not have.
224 match m.pool() {
225 "gb10" if !f.gb10_present => {
226 return Some((Verdict::BlockedHardware, "NO_GB10".to_string()));
227 }
228 "qemu" if !f.qemu_available => {
229 return Some((Verdict::BlockedHardware, "NO_QEMU".to_string()));
230 }
231 _ => {}
232 }
233 // Row 4 before row 5: a blocked dependency passes its blocked verdict on.
234 for d in &m.depends_on {
235 if let Some(v @ (Verdict::BlockedHardware | Verdict::BlockedOperator)) =
236 dep_verdict(&f.deps, d)
237 {
238 return Some((v, format!("DEP_BLOCKED:{d}")));
239 }
240 }
241 // Row 5: every other dependency must currently be PASS.
242 if let Some(d) = m
243 .depends_on
244 .iter()
245 .find(|d| dep_verdict(&f.deps, d) != Some(Verdict::Pass))
246 {
247 return Some((Verdict::NotRun, format!("DEP_NOT_PASS:{d}")));
248 }
249 if let Some(b) = &m.build {
250 if b.tool != "none" {
251 return Some((Verdict::NotRun, "build_unsupported".to_string()));
252 }
253 }
254 if let Some(c) = m.checks.iter().find(|c| c.as_str() != "clean_tree_after") {
255 return Some((Verdict::NotRun, format!("check_unsupported:{c}")));
256 }
257 if let Some(r) = &f.refusal {
258 return Some((Verdict::NotRun, r.clone()));
259 }
260 if !f.exec_exists {
261 return Some((Verdict::MissingImplementation, "NO_RUN_EXEC".to_string()));
262 }
263 None
264}
265
266/// Exit status with which a program says it could not run because an input it
267/// does not own is absent (a model checkpoint, say), together with a
268/// `not_run_reason` observation holding a plain lowercase name. The verdict is
269/// then NOT_RUN with that reason: a refusal, never PASS. Without a valid
270/// reason, or when the manifest itself expects 77, it is an ordinary exit.
271pub const SELF_NOT_RUN_EXIT: i64 = 77;
272
273fn self_not_run(m: &Manifest, f: &Facts) -> Option<String> {
274 if f.timed_out || f.exit_status != Some(SELF_NOT_RUN_EXIT) || m.expect_exit == SELF_NOT_RUN_EXIT
275 {
276 return None;
277 }
278 let o = f
279 .observations
280 .iter()
281 .rev()
282 .find(|o| o.name == "not_run_reason")?;
283 let s = o.value.as_str()?;
284 let plain = !s.is_empty()
285 && s.len() <= 64
286 && s.bytes()
287 .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_');
288 plain.then(|| s.to_string())
289}
290
291pub fn derive(m: &Manifest, f: &Facts) -> Derived {
292 if let Some((verdict, reason)) = pre_run(m, f) {
293 return Derived {
294 verdict,
295 reason,
296 rules: m.rules.iter().map(|r| (r.text.clone(), false)).collect(),
297 };
298 }
299 let rules: Vec<(String, bool)> = m
300 .rules
301 .iter()
302 .map(|r| (r.text.clone(), eval_rule(r, &f.observations)))
303 .collect();
304 let (verdict, reason) = if f.prepare_failed {
305 (Verdict::Fail, "prepare_failed".to_string())
306 } else if f.timed_out {
307 (Verdict::Fail, "timeout".to_string())
308 } else if let Some(r) = self_not_run(m, f) {
309 (Verdict::NotRun, r)
310 } else if f.exit_status != Some(m.expect_exit) {
311 (Verdict::Fail, "rc_nonzero".to_string())
312 } else if let Some(idx) = rules.iter().position(|r| !r.1) {
313 (Verdict::Fail, format!("rule:{idx}"))
314 } else if f.tree_changed_after {
315 (Verdict::Fail, "dirty_after".to_string())
316 } else if f.head_moved {
317 (Verdict::Fail, "head_moved".to_string())
318 } else {
319 (Verdict::Pass, String::new())
320 };
321 Derived {
322 verdict,
323 reason,
324 rules,
325 }
326}
327
328#[cfg(test)]
329mod tests {
330 use super::*;
331 use crate::manifest;
332 use serde_json::json;
333
334 fn m(extra_replace: Option<(&str, &str)>) -> Manifest {
335 let mut t = String::from(
336 "gate: G\nmanifest_version: 1\nowner: o\nrequires:\n - host\nrun:\n exec: x.sh\nexpects:\n exit: 0\n observe: [\"n == 3\", \"all k <= 2\"]\n verdict: PASS\ntimeout: 5s\n",
337 );
338 if let Some((a, b)) = extra_replace {
339 assert!(t.contains(a), "replace target missing: {a}");
340 t = t.replace(a, b);
341 }
342 manifest::parse(t.as_bytes()).unwrap()
343 }
344
345 fn obs(name: &str, v: Value) -> Observation {
346 Observation {
347 name: name.to_string(),
348 value: v,
349 }
350 }
351
352 fn good_facts() -> Facts {
353 Facts {
354 refusal: None,
355 exec_exists: true,
356 deps: Vec::new(),
357 gb10_present: true,
358 qemu_available: true,
359 exit_status: Some(0),
360 observations: vec![obs("n", json!(3)), obs("k", json!(1)), obs("k", json!(2))],
361 timed_out: false,
362 tree_changed_after: false,
363 head_moved: false,
364 operator: false,
365 prepare_failed: false,
366 }
367 }
368
369 #[test]
370 fn exit_codes_and_names_are_fixed() {
371 let all = [
372 (Verdict::Pass, "PASS", 0),
373 (Verdict::Fail, "FAIL", 1),
374 (Verdict::NotRun, "NOT_RUN", 2),
375 (Verdict::BlockedHardware, "BLOCKED_HARDWARE", 3),
376 (Verdict::BlockedOperator, "BLOCKED_OPERATOR", 4),
377 (Verdict::MissingImplementation, "MISSING_IMPLEMENTATION", 5),
378 ];
379 for (v, name, code) in all {
380 assert_eq!(v.as_str(), name);
381 assert_eq!(v.exit_code(), code);
382 }
383 }
384
385 #[test]
386 fn all_expectations_met_is_pass() {
387 let d = derive(&m(None), &good_facts());
388 assert_eq!(d.verdict, Verdict::Pass);
389 assert_eq!(d.reason, "");
390 assert!(d.rules.iter().all(|r| r.1));
391 }
392
393 #[test]
394 fn rule_mismatch_is_fail_with_index() {
395 let mut f = good_facts();
396 f.observations[0] = obs("n", json!(4));
397 let d = derive(&m(None), &f);
398 assert_eq!(d.verdict, Verdict::Fail);
399 assert_eq!(d.reason, "rule:0");
400 assert!(!d.rules[0].1);
401 assert!(d.rules[1].1);
402 }
403
404 #[test]
405 fn printing_pass_cannot_make_a_pass() {
406 let mut f = good_facts();
407 f.observations = parse_observations(b"PASS\nAIEN-OBS verdict \"PASS\"\n");
408 let d = derive(&m(None), &f);
409 assert_eq!(d.verdict, Verdict::Fail);
410 }
411
412 #[test]
413 fn missing_observation_does_not_hold() {
414 let mut f = good_facts();
415 f.observations.retain(|o| o.name != "n");
416 assert_eq!(derive(&m(None), &f).reason, "rule:0");
417 }
418
419 #[test]
420 fn exit_77_with_a_reason_is_not_run_and_nothing_else_is() {
421 let reason = |v: Value| Observation {
422 name: "not_run_reason".into(),
423 value: v,
424 };
425 let mut f = good_facts();
426 f.exit_status = Some(77);
427 f.observations.push(reason(json!("model_missing")));
428 let d = derive(&m(None), &f);
429 assert_eq!(
430 (d.verdict, d.reason.as_str()),
431 (Verdict::NotRun, "model_missing")
432 );
433 // No reason, or a reason that is not a plain name: an ordinary failure.
434 f.observations.retain(|o| o.name != "not_run_reason");
435 assert_eq!(derive(&m(None), &f).reason, "rc_nonzero");
436 for bad in [json!("Model Missing"), json!(""), json!(3), json!("a;b")] {
437 let mut g = f.clone();
438 g.observations.push(reason(bad));
439 assert_eq!(derive(&m(None), &g).reason, "rc_nonzero");
440 }
441 // The reason never turns another exit into NOT_RUN, and a timeout stays a timeout.
442 let mut g = good_facts();
443 g.exit_status = Some(1);
444 g.observations.push(reason(json!("model_missing")));
445 assert_eq!(derive(&m(None), &g).reason, "rc_nonzero");
446 let mut g = f.clone();
447 g.observations.push(reason(json!("model_missing")));
448 g.timed_out = true;
449 assert_eq!(derive(&m(None), &g).reason, "timeout");
450 // A manifest that expects 77 keeps its own meaning.
451 let m77 = m(Some(("exit: 0", "exit: 77")));
452 assert_eq!(derive(&m77, &g.clone()).reason, "timeout");
453 g.timed_out = false;
454 assert_eq!(derive(&m77, &g).verdict, Verdict::Pass);
455 }
456 #[test]
457 fn exit_status_must_match_expectation() {
458 let mut f = good_facts();
459 f.exit_status = Some(1);
460 let d = derive(&m(None), &f);
461 assert_eq!(
462 (d.verdict, d.reason.as_str()),
463 (Verdict::Fail, "rc_nonzero")
464 );
465 f.exit_status = None;
466 assert_eq!(derive(&m(None), &f).reason, "rc_nonzero");
467 // A manifest that expects exit 7 passes on 7 and fails on 0.
468 let m7 = m(Some(("exit: 0", "exit: 7")));
469 f.exit_status = Some(7);
470 assert_eq!(derive(&m7, &f).verdict, Verdict::Pass);
471 f.exit_status = Some(0);
472 assert_eq!(derive(&m7, &f).verdict, Verdict::Fail);
473 }
474
475 #[test]
476 fn timeout_beats_everything_after_run() {
477 let mut f = good_facts();
478 f.timed_out = true;
479 f.exit_status = None;
480 let d = derive(&m(None), &f);
481 assert_eq!((d.verdict, d.reason.as_str()), (Verdict::Fail, "timeout"));
482 }
483
484 #[test]
485 fn a_failed_prepare_fails_even_when_its_exit_matches() {
486 // The prepare exit is never compared with expects.exit: a gate that
487 // expects exit 1 must not pass because its build failed with 1.
488 let mo = m(Some((" exit: 0\n", " exit: 1\n")));
489 let mut f = good_facts();
490 f.prepare_failed = true;
491 f.exit_status = Some(1);
492 let d = derive(&mo, &f);
493 assert_eq!(
494 (d.verdict, d.reason.as_str()),
495 (Verdict::Fail, "prepare_failed")
496 );
497 }
498
499 #[test]
500 fn tree_and_head_changes_fail() {
501 let mut f = good_facts();
502 f.tree_changed_after = true;
503 assert_eq!(derive(&m(None), &f).reason, "dirty_after");
504 f.tree_changed_after = false;
505 f.head_moved = true;
506 assert_eq!(derive(&m(None), &f).reason, "head_moved");
507 }
508
509 #[test]
510 fn first_reason_wins() {
511 let mut f = good_facts();
512 f.exit_status = Some(9);
513 f.observations.clear();
514 f.tree_changed_after = true;
515 assert_eq!(derive(&m(None), &f).reason, "rc_nonzero");
516 }
517
518 #[test]
519 fn refusal_is_not_run() {
520 let mut f = good_facts();
521 f.refusal = Some("dirty_tree".to_string());
522 let d = derive(&m(None), &f);
523 assert_eq!(
524 (d.verdict, d.reason.as_str()),
525 (Verdict::NotRun, "dirty_tree")
526 );
527 }
528
529 #[test]
530 fn missing_exec_is_missing_implementation() {
531 let mut f = good_facts();
532 f.exec_exists = false;
533 let d = derive(&m(None), &f);
534 assert_eq!(d.verdict, Verdict::MissingImplementation);
535 assert_eq!(d.reason, "NO_RUN_EXEC");
536 }
537
538 #[test]
539 fn operator_gate_is_blocked_operator() {
540 let mo = m(Some((" - host\n", " - host\n - operator\n")));
541 let d = derive(&mo, &good_facts());
542 assert_eq!(d.verdict, Verdict::BlockedOperator);
543 assert_eq!(d.reason, "OPERATOR_STEP_PENDING");
544 }
545
546 #[test]
547 fn operator_gate_runs_when_the_operator_started_it() {
548 // sc #614: `--operator` is the operator starting the gate by hand
549 // (ADR 0033 Decision 2); the facts then decide like any other gate.
550 let mo = m(Some((" - host\n", " - host\n - operator\n")));
551 let f = Facts {
552 operator: true,
553 ..good_facts()
554 };
555 assert_eq!(pre_run(&mo, &f), None);
556 let d = derive(&mo, &f);
557 assert_eq!((d.verdict, d.reason.as_str()), (Verdict::Pass, ""));
558 // The other rows still apply to an operator-started gate.
559 let gb = gate_with(" - gb10\n - operator\n", " - OTHER\n");
560 let f = Facts {
561 operator: true,
562 gb10_present: false,
563 ..good_facts()
564 };
565 assert_eq!(derive(&gb, &f).reason, "NO_GB10");
566 }
567
568 #[test]
569 fn missing_hardware_blocks_the_gate() {
570 let mut f = good_facts();
571 let g = m(Some((" - host\n", " - gb10\n")));
572 assert_eq!(derive(&g, &f).verdict, Verdict::Pass);
573 f.gb10_present = false;
574 let d = derive(&g, &f);
575 assert_eq!(
576 (d.verdict, d.reason.as_str()),
577 (Verdict::BlockedHardware, "NO_GB10")
578 );
579 let q = m(Some((" - host\n", " - qemu\n")));
580 assert_eq!(
581 derive(&q, &f).verdict,
582 Verdict::Pass,
583 "gb10 is irrelevant to qemu"
584 );
585 f.qemu_available = false;
586 let d = derive(&q, &f);
587 assert_eq!(
588 (d.verdict, d.reason.as_str()),
589 (Verdict::BlockedHardware, "NO_QEMU")
590 );
591 // A host gate never needs the special hardware.
592 assert_eq!(derive(&m(None), &f).verdict, Verdict::Pass);
593 // gb10 beats qemu when both are listed (the scarcest pool decides).
594 let both = m(Some((" - host\n", " - qemu\n - gb10\n")));
595 f.qemu_available = true;
596 assert_eq!(derive(&both, &f).reason, "NO_GB10");
597 }
598
599 fn dep(gate: &str, verdict: Option<Verdict>) -> DepFact {
600 DepFact {
601 gate: gate.to_string(),
602 verdict,
603 receipt: verdict.map(|_| format!("digest-of-{gate}")),
604 }
605 }
606
607 fn gate_with(requires: &str, deps: &str) -> Manifest {
608 let dep_block = if deps.is_empty() {
609 String::new()
610 } else {
611 format!("depends_on:\n{deps}")
612 };
613 let text = format!(
614 "gate: G\nmanifest_version: 1\nowner: o\nrequires:\n{requires}run:\n exec: x.sh\nexpects:\n exit: 0\n observe: [\"n == 3\", \"all k <= 2\"]\n verdict: PASS\ntimeout: 5s\n{dep_block}"
615 );
616 manifest::parse(text.as_bytes()).unwrap()
617 }
618
619 fn with_deps(list: &str) -> Manifest {
620 gate_with(" - host\n", list)
621 }
622
623 #[test]
624 fn a_passing_dependency_lets_the_gate_run() {
625 let mut f = good_facts();
626 f.deps = vec![dep("OTHER", Some(Verdict::Pass))];
627 let d = derive(&with_deps(" - OTHER\n"), &f);
628 assert_eq!(d.verdict, Verdict::Pass);
629 }
630
631 #[test]
632 fn a_dependency_that_is_not_pass_makes_not_run() {
633 let one = with_deps(" - OTHER\n");
634 let mut f = good_facts();
635 // No fact at all (absent or stale).
636 let r = derive(&one, &f);
637 assert_eq!(
638 (r.verdict, r.reason.as_str()),
639 (Verdict::NotRun, "DEP_NOT_PASS:OTHER")
640 );
641 for v in [
642 None,
643 Some(Verdict::Fail),
644 Some(Verdict::NotRun),
645 Some(Verdict::MissingImplementation),
646 ] {
647 f.deps = vec![dep("OTHER", v)];
648 let r = derive(&one, &f);
649 assert_eq!(
650 (r.verdict, r.reason.as_str()),
651 (Verdict::NotRun, "DEP_NOT_PASS:OTHER"),
652 "dependency verdict {v:?}"
653 );
654 assert!(r.rules.iter().all(|x| !x.1), "nothing was checked");
655 }
656 }
657
658 #[test]
659 fn the_first_dependency_that_is_not_pass_is_named() {
660 let two = with_deps(" - FIRST\n - SECOND\n");
661 let mut f = good_facts();
662 f.deps = vec![dep("FIRST", Some(Verdict::Pass)), dep("SECOND", None)];
663 assert_eq!(derive(&two, &f).reason, "DEP_NOT_PASS:SECOND");
664 f.deps = vec![dep("FIRST", Some(Verdict::Fail)), dep("SECOND", None)];
665 assert_eq!(derive(&two, &f).reason, "DEP_NOT_PASS:FIRST");
666 }
667
668 #[test]
669 fn a_blocked_dependency_passes_its_verdict_on() {
670 let one = with_deps(" - OTHER\n");
671 let mut f = good_facts();
672 f.deps = vec![dep("OTHER", Some(Verdict::BlockedOperator))];
673 let r = derive(&one, &f);
674 assert_eq!(
675 (r.verdict, r.reason.as_str()),
676 (Verdict::BlockedOperator, "DEP_BLOCKED:OTHER")
677 );
678 f.deps = vec![dep("OTHER", Some(Verdict::BlockedHardware))];
679 let r = derive(&one, &f);
680 assert_eq!(
681 (r.verdict, r.reason.as_str()),
682 (Verdict::BlockedHardware, "DEP_BLOCKED:OTHER")
683 );
684 }
685
686 #[test]
687 fn blocked_beats_failed_across_dependencies() {
688 // Row 4 is checked before row 5, whatever the order in the manifest.
689 let two = with_deps(" - FAILED\n - BLOCKED\n");
690 let mut f = good_facts();
691 f.deps = vec![
692 dep("FAILED", Some(Verdict::Fail)),
693 dep("BLOCKED", Some(Verdict::BlockedHardware)),
694 ];
695 let r = derive(&two, &f);
696 assert_eq!(
697 (r.verdict, r.reason.as_str()),
698 (Verdict::BlockedHardware, "DEP_BLOCKED:BLOCKED")
699 );
700 }
701
702 #[test]
703 fn rows_are_checked_in_order() {
704 let mut f = good_facts();
705 f.gb10_present = false;
706 f.refusal = Some("dirty_tree".to_string());
707 // Row 2 (operator) beats row 3 (hardware).
708 let op = gate_with(" - gb10\n - operator\n", " - OTHER\n");
709 assert_eq!(derive(&op, &f).reason, "OPERATOR_STEP_PENDING");
710 // Row 3 (hardware) beats rows 4 and 5 (dependencies).
711 let gb = gate_with(" - gb10\n", " - OTHER\n");
712 assert_eq!(derive(&gb, &f).reason, "NO_GB10");
713 // Rows 4 and 5 beat the refusal (row 6).
714 let host = gate_with(" - host\n", " - OTHER\n");
715 assert_eq!(derive(&host, &f).reason, "DEP_NOT_PASS:OTHER");
716 f.deps = vec![dep("OTHER", Some(Verdict::Pass))];
717 assert_eq!(derive(&host, &f).reason, "dirty_tree");
718 // Row 6 beats row 7.
719 f.exec_exists = false;
720 assert_eq!(derive(&host, &f).reason, "dirty_tree");
721 f.refusal = None;
722 assert_eq!(derive(&host, &f).reason, "NO_RUN_EXEC");
723 }
724
725 #[test]
726 fn campaign_verdict_follows_the_exit_code_rule() {
727 use Verdict::*;
728 assert_eq!(campaign_verdict(&[]), Pass);
729 assert_eq!(campaign_verdict(&[Pass, Pass]), Pass);
730 assert_eq!(campaign_verdict(&[Pass, NotRun]), NotRun);
731 assert_eq!(
732 campaign_verdict(&[NotRun, BlockedHardware]),
733 BlockedHardware
734 );
735 assert_eq!(
736 campaign_verdict(&[BlockedOperator, MissingImplementation, NotRun]),
737 MissingImplementation
738 );
739 // Any FAIL wins, even over the higher-numbered codes.
740 assert_eq!(campaign_verdict(&[MissingImplementation, Fail, Pass]), Fail);
741 assert_eq!(campaign_verdict(&[Fail]).exit_code(), 1);
742 assert_eq!(campaign_verdict(&[BlockedOperator, Pass]).exit_code(), 4);
743 }
744
745 #[test]
746 fn verdict_names_parse_back() {
747 for v in Verdict::ALL {
748 assert_eq!(Verdict::parse(v.as_str()), Some(v));
749 }
750 assert_eq!(Verdict::parse("pass"), None);
751 assert_eq!(Verdict::parse(""), None);
752 assert_eq!(Verdict::ALL.len(), 6);
753 }
754
755 #[test]
756 fn build_and_checks_are_not_run() {
757 let b = m(Some((
758 "timeout: 5s\n",
759 "timeout: 5s\nbuild:\n target: t\n tool: make\n",
760 )));
761 assert_eq!(derive(&b, &good_facts()).reason, "build_unsupported");
762 let c = m(Some((
763 "timeout: 5s\n",
764 "timeout: 5s\nchecks:\n - no_libm_symbols\n",
765 )));
766 assert_eq!(
767 derive(&c, &good_facts()).reason,
768 "check_unsupported:no_libm_symbols"
769 );
770 }
771
772 #[test]
773 fn build_none_and_clean_tree_check_are_fine() {
774 let b = m(Some((
775 "timeout: 5s\n",
776 "timeout: 5s\nbuild:\n target: t\n tool: none\nchecks:\n - clean_tree_after\n",
777 )));
778 assert_eq!(derive(&b, &good_facts()).verdict, Verdict::Pass);
779 }
780
781 #[test]
782 fn observation_parsing() {
783 let o = parse_observations(
784 b"noise\nAIEN-OBS a 1\nAIEN-OBS b \"x y\"\nAIEN-OBS c 1.5\nAIEN-OBS BAD 1\nAIEN-OBS d\nAIEN-OBS a 2\nAIEN-OBS e true\nAIEN-OBS f nope\n",
785 );
786 let names: Vec<&str> = o.iter().map(|x| x.name.as_str()).collect();
787 assert_eq!(names, vec!["a", "b", "a", "e"]);
788 assert_eq!(o[1].value, json!("x y"));
789 }
790
791 #[test]
792 fn quantifiers_and_ordering() {
793 let r = manifest::parse_rule("any k > 5").unwrap();
794 let o = vec![obs("k", json!(1)), obs("k", json!(9))];
795 assert!(eval_rule(&r, &o));
796 let r = manifest::parse_rule("k > 5").unwrap();
797 assert!(eval_rule(&r, &o), "last value 9 > 5");
798 let r = manifest::parse_rule("all k > 5").unwrap();
799 assert!(!eval_rule(&r, &o));
800 let r = manifest::parse_rule("k < 5").unwrap();
801 assert!(!eval_rule(&r, &o));
802 // Ordering over strings never holds.
803 let r = manifest::parse_rule("s < 5").unwrap();
804 assert!(!eval_rule(&r, &[obs("s", json!("a"))]));
805 let r = manifest::parse_rule("s != \"a\"").unwrap();
806 assert!(eval_rule(&r, &[obs("s", json!("b"))]));
807 }
808}