helixforge-engine 1.5.2

HelixForge's engine: plans, runners, history, health checks. Part of the HelixForge application, not a general-purpose library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! How far each entry has been tested: real installs in distribution
//! containers (recorded by the Distributions workflow and carried by release
//! builds), installer tests with stand-in commands, and reports from virtual
//! and real machines (`catalog/validation.toml`), with this machine's own
//! runs beside them. Nothing here claims more than its record shows.

use crate::history;
use helixforge_core::{Action, Entry, Risk};
use serde::Deserialize;
use std::{
    collections::BTreeMap,
    path::{Path, PathBuf},
};

/// How far an entry has been validated, best first.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, serde::Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum Level {
    /// Run on a real machine, from a tester's report.
    HardwareTested,
    /// Run in a virtual machine, from a report.
    VmTested,
    /// Installed (or run) and verified in a distribution container by the
    /// Distributions workflow.
    ContainerTested,
    /// Its installer was run against stand-in commands and files.
    StandInTested,
    NotYetValidated,
}

impl Level {
    pub fn label(self) -> &'static str {
        match self {
            Level::HardwareTested => "Hardware Tested",
            Level::VmTested => "VM Tested",
            Level::ContainerTested => "Container Tested",
            Level::StandInTested => "Stand-in Tested",
            Level::NotYetValidated => "Not Yet Validated",
        }
    }

    pub fn explain(self) -> &'static str {
        match self {
            Level::HardwareTested => "A tester ran it on a real machine and reported the result.",
            Level::VmTested => {
                "It ran for real in a virtual machine: the Distributions workflow boots a distribution's cloud image or live ISO, runs the entry, reboots into the result and checks it (the configuration, the kernel command line, the boot images, the services, a graphical boot menu or login screen). Real graphics hardware and firmware are not covered."
            }
            Level::ContainerTested => {
                "The Distributions workflow ran it in distribution containers and HelixForge verified the result there. Containers have no boot loader, login screen or desktop, so those parts are not covered."
            }
            Level::StandInTested => {
                "Installer tests run its script, or the helpers it calls, against stand-in commands and files, on every change and in each distribution's container. They show what it would change, without changing a system."
            }
            Level::NotYetValidated => {
                "No automated test runs it and nobody has reported running it. Its plan is built and checked, like every entry's."
            }
        }
    }
}

/// `catalog/validation.toml`.
#[derive(Clone, Debug, Default, Deserialize)]
pub struct Claims {
    #[serde(default)]
    pub critical: Critical,
    #[serde(default, rename = "stand-ins")]
    pub stand_ins: Vec<StandIns>,
    #[serde(default)]
    pub hardware: Vec<Report>,
    #[serde(default)]
    pub vm: Vec<Report>,
}

#[derive(Clone, Debug, Default, Deserialize)]
pub struct Critical {
    #[serde(default)]
    pub entries: Vec<String>,
}

/// Installer tests that cover entries with stand-ins.
#[derive(Clone, Debug, Default, Deserialize)]
pub struct StandIns {
    pub entries: Vec<String>,
    pub tests: Vec<String>,
    #[serde(default)]
    pub covers: String,
}

/// A run reported from a virtual or real machine.
#[derive(Clone, Debug, Default, Deserialize)]
pub struct Report {
    pub entry: String,
    pub system: String,
    #[serde(default)]
    pub version: String,
    pub date: String,
    #[serde(default)]
    pub note: String,
}

/// What a Distributions run verified in each container, as release builds
/// carry it (`catalog/validation-results.json`).
#[derive(Clone, Debug, Default, Deserialize)]
pub struct Results {
    #[serde(default)]
    pub commit: String,
    #[serde(default)]
    pub run: u64,
    #[serde(default)]
    pub url: String,
    /// When the run finished, in Unix seconds.
    #[serde(default)]
    pub finished: u64,
    /// By container image: each entry and what was shown (`verified`, or
    /// `ran` for an entry with nothing to verify).
    #[serde(default)]
    pub images: BTreeMap<String, Image>,
    /// By virtual machine (arch, debian): the entries run there, rebooted
    /// into and checked by scripts/vm-tests.py.
    #[serde(default)]
    pub vms: BTreeMap<String, Image>,
}

#[derive(Clone, Debug, Default, Deserialize)]
pub struct Image {
    #[serde(default)]
    pub entries: BTreeMap<String, String>,
}

/// Everything known about testing, from this build's catalog.
#[derive(Clone, Debug, Default)]
pub struct Evidence {
    pub claims: Claims,
    pub results: Option<Results>,
    /// Why a file could not be read, if one could not.
    pub problems: Vec<String>,
}

/// One entry's standing.
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
pub struct Assessment {
    pub level: Level,
    /// Where the tests ran: container images or reported systems.
    pub systems: Vec<String>,
    /// When it was last verified, as a date, when known.
    pub verified: Option<String>,
    /// What shows it, line by line.
    pub evidence: Vec<String>,
    /// Its last successful run on this machine, in Unix seconds.
    pub here: Option<u64>,
    /// A boot, login-screen, driver or whole-system entry.
    pub critical: bool,
}

/// A virtual machine as people name it: "Arch VM".
pub fn vm_label(name: &str) -> String {
    match name {
        "arch" => "Arch VM".to_string(),
        "debian" => "Debian VM".to_string(),
        "fedora" => "Fedora VM".to_string(),
        "arch-uefi" => "Arch UEFI VM".to_string(),
        "arch-iso" => "Arch live ISO VM".to_string(),
        "steamdeck" => "Steam Deck stand-in VM".to_string(),
        other => format!("{} VM", helixforge_core::printable(other)),
    }
}

/// A container image as people name the system.
pub fn image_label(image: &str) -> String {
    match image {
        "archlinux:latest" => "Arch".to_string(),
        "debian:stable" => "Debian".to_string(),
        "fedora:latest" => "Fedora".to_string(),
        "opensuse/tumbleweed:latest" => "openSUSE Tumbleweed".to_string(),
        other => match other.split_once(':') {
            Some(("ubuntu", version)) => format!("Ubuntu {version}"),
            _ => helixforge_core::printable(other),
        },
    }
}

impl Evidence {
    /// Read this build's record: `catalog/validation.toml` and, in a
    /// release build, `catalog/validation-results.json`.
    pub fn load(root: &Path) -> Self {
        let mut evidence = Evidence::default();
        let catalog = root.join("catalog");
        let claims = catalog.join("validation.toml");
        match std::fs::read_to_string(&claims) {
            Ok(text) => match toml::from_str(&text) {
                Ok(claims) => evidence.claims = claims,
                Err(error) => evidence.problems.push(format!(
                    "{}: {}",
                    claims.display(),
                    helixforge_core::printable(error.message())
                )),
            },
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
            Err(error) => evidence
                .problems
                .push(format!("{}: {error}", claims.display())),
        }
        let results: PathBuf = catalog.join("validation-results.json");
        if let Ok(text) = std::fs::read_to_string(&results) {
            match serde_json::from_str(&text) {
                Ok(results) => evidence.results = Some(results),
                Err(error) => evidence
                    .problems
                    .push(format!("{}: {error}", results.display())),
            }
        }
        evidence
    }

    /// Whether failing would leave the machine without a working boot,
    /// login screen or display, or the entry changes disks or the whole
    /// system.
    pub fn critical(&self, entry: &Entry) -> bool {
        Risk::from_flags(&entry.flags) == Risk::High
            || self.claims.critical.entries.contains(&entry.id)
    }

    /// How an entry stands; `history` is this machine's, newest first.
    pub fn assess(&self, entry: &Entry, history: &[history::Record]) -> Assessment {
        let here = history
            .iter()
            .find(|record| record.success && record.entries.contains(&entry.id))
            .map(|record| record.unix_timestamp);
        let mut assessment = Assessment {
            level: Level::NotYetValidated,
            systems: Vec::new(),
            verified: None,
            evidence: Vec::new(),
            here,
            critical: self.critical(entry),
        };
        let better = |assessment: &mut Assessment, level: Level| {
            if level < assessment.level {
                assessment.level = level;
            }
        };
        for (kind, reports, level) in [
            (
                "a real machine",
                &self.claims.hardware,
                Level::HardwareTested,
            ),
            ("a virtual machine", &self.claims.vm, Level::VmTested),
        ] {
            for report in reports.iter().filter(|report| report.entry == entry.id) {
                better(&mut assessment, level);
                let system = helixforge_core::printable(&report.system);
                if !assessment.systems.contains(&system) {
                    assessment.systems.push(system.clone());
                }
                let date = helixforge_core::printable(&report.date);
                if assessment
                    .verified
                    .as_ref()
                    .is_none_or(|known| *known < date)
                {
                    assessment.verified = Some(date.clone());
                }
                let mut line = format!("Ran on {kind}: {system}, {date}");
                if !report.version.is_empty() {
                    line.push_str(&format!(
                        ", HelixForge {}",
                        helixforge_core::printable(&report.version)
                    ));
                }
                if !report.note.is_empty() {
                    line.push_str(&format!(" ({})", helixforge_core::printable(&report.note)));
                }
                assessment.evidence.push(line);
            }
        }
        if let Some(results) = &self.results {
            let date = crate::date(results.finished);
            let machines: Vec<String> = results
                .vms
                .iter()
                .filter(|(_, found)| {
                    found
                        .entries
                        .get(&entry.id)
                        .is_some_and(|outcome| outcome == "verified")
                })
                .map(|(name, _)| vm_label(name))
                .collect();
            if !machines.is_empty() {
                better(&mut assessment, Level::VmTested);
                for machine in &machines {
                    if !assessment.systems.contains(machine) {
                        assessment.systems.push(machine.clone());
                    }
                }
                if assessment
                    .verified
                    .as_ref()
                    .is_none_or(|known| *known < date)
                {
                    assessment.verified = Some(date.clone());
                }
                assessment.evidence.push(format!(
                    "In virtual machines, Distributions run {} on {date}: {}; run for real, rebooted into and checked",
                    results.run,
                    machines.join(", ")
                ));
            }
            let mut shown: Vec<String> = Vec::new();
            for (image, found) in &results.images {
                if let Some(outcome) = found.entries.get(&entry.id) {
                    shown.push(format!(
                        "{} ({})",
                        image_label(image),
                        if outcome == "verified" {
                            "installed and verified"
                        } else {
                            "ran"
                        }
                    ));
                    let label = image_label(image);
                    if !assessment.systems.contains(&label) {
                        assessment.systems.push(label);
                    }
                }
            }
            if !shown.is_empty() {
                better(&mut assessment, Level::ContainerTested);
                if assessment
                    .verified
                    .as_ref()
                    .is_none_or(|known| *known < date)
                {
                    assessment.verified = Some(date.clone());
                }
                assessment.evidence.push(format!(
                    "In containers, Distributions run {} on {date}: {}",
                    results.run,
                    shown.join(", ")
                ));
            }
        }
        for stand_ins in self
            .claims
            .stand_ins
            .iter()
            .filter(|group| group.entries.contains(&entry.id))
        {
            better(&mut assessment, Level::StandInTested);
            let mut line = format!(
                "Installer tests with stand-ins ({}): {}",
                stand_ins.tests.join(", "),
                helixforge_core::printable(&stand_ins.covers)
            );
            if let Some(results) = &self.results {
                let date = crate::date(results.finished);
                line.push_str(&format!(
                    "; they ran in all {} containers on {date}",
                    results.images.len()
                ));
                if assessment.level == Level::StandInTested {
                    assessment.verified = Some(date);
                    for image in results.images.keys() {
                        let label = image_label(image);
                        if !assessment.systems.contains(&label) {
                            assessment.systems.push(label);
                        }
                    }
                }
            }
            assessment.evidence.push(line);
        }
        if let Action::Package { .. } = entry.action {
            assessment.evidence.push(
                "Its commands are built by HelixForge from package names, and tested against harmless stand-ins on every change.".to_string(),
            );
        }
        if let Some(when) = here {
            assessment.evidence.push(format!(
                "Ran successfully on this machine, {}",
                crate::date(when)
            ));
        }
        assessment
    }

    /// A line on the record this build carries, for the screen's header.
    pub fn summary(&self) -> String {
        match &self.results {
            Some(results) => format!(
                "Container record: Distributions run {} on commit {}, {}, {} distributions{}.",
                results.run,
                results.commit.chars().take(12).collect::<String>(),
                crate::date(results.finished),
                results.images.len(),
                match results.vms.len() {
                    0 => String::new(),
                    1 => " and 1 virtual machine".to_string(),
                    count => format!(" and {count} virtual machines"),
                }
            ),
            None => "No container record in this build; release builds carry one.".to_string(),
        }
    }
}

/// Warnings for a plan's entries that are critical and not validated past
/// stand-ins, shown before it runs.
pub fn warnings<'a>(
    ids: impl IntoIterator<Item = &'a str>,
    lookup: &dyn Fn(&str) -> Option<&'a Entry>,
    evidence: &Evidence,
) -> Vec<String> {
    let mut warnings = Vec::new();
    for id in ids {
        let Some(entry) = lookup(id) else { continue };
        if !evidence.critical(entry) {
            continue;
        }
        let assessment = evidence.assess(entry, &[]);
        if assessment.level >= Level::ContainerTested {
            warnings.push(format!(
                "{} is {}: {}",
                entry.name,
                assessment.level.label(),
                match assessment.level {
                    Level::NotYetValidated => "nothing has tested it yet",
                    Level::StandInTested => "only tested with stand-ins, never on a real system",
                    _ => "containers cannot test boot, login or drivers",
                }
            ));
        }
    }
    warnings
}

#[cfg(test)]
mod tests {
    use super::*;
    use helixforge_catalog::Catalog;

    fn evidence(claims: &str, results: Option<&str>) -> Evidence {
        Evidence {
            claims: toml::from_str(claims).unwrap(),
            results: results.map(|text| serde_json::from_str(text).unwrap()),
            problems: Vec::new(),
        }
    }

    const RESULTS: &str = r#"{"format": 1, "commit": "0123456789abcdef0123", "run": 42,
        "finished": 1792172552,
        "images": {"archlinux:latest": {"entries": {"htop": "verified"}},
                   "ubuntu:24.04": {"entries": {"htop": "verified", "system-report": "ran"}}},
        "vms": {"arch": {"entries": {"sddm": "verified"}}}}"#;

    #[test]
    fn levels_come_from_the_record_never_beyond_it() {
        let catalog = Catalog::builtin(false).unwrap();
        let found = evidence(
            r#"
            [critical]
            entries = ["sddm", "grub"]
            [[stand-ins]]
            entries = ["sddm"]
            tests = ["test_sddm"]
            covers = "the theme"
            [[hardware]]
            entry = "grub"
            system = "Garuda Linux"
            date = "2026-10-20"
            "#,
            Some(RESULTS),
        );
        let assess = |id: &str| found.assess(catalog.find(id).unwrap(), &[]);
        let htop = assess("htop");
        assert_eq!(htop.level, Level::ContainerTested);
        assert_eq!(htop.systems, ["Arch", "Ubuntu 24.04"]);
        assert_eq!(htop.verified.as_deref(), Some("2026-10-16"));
        assert!(!htop.critical);
        assert_eq!(assess("system-report").level, Level::ContainerTested);
        // Verified in a virtual machine: above its stand-in tests.
        let sddm = assess("sddm");
        assert_eq!(sddm.level, Level::VmTested);
        assert!(sddm.critical);
        assert!(
            sddm.systems.contains(&"Arch VM".to_string()),
            "{:?}",
            sddm.systems
        );
        assert!(
            sddm.evidence[0].contains("virtual machines"),
            "{:?}",
            sddm.evidence
        );
        assert!(sddm.evidence.iter().any(|line| line.contains("test_sddm")));
        assert!(
            found.summary().contains(" and 1 virtual machine."),
            "{}",
            found.summary()
        );
        let grub = assess("grub");
        assert_eq!(grub.level, Level::HardwareTested);
        assert_eq!(grub.verified.as_deref(), Some("2026-10-20"));
        assert_eq!(assess("ripgrep").level, Level::NotYetValidated);
        // This machine's runs show beside the level, never raise it.
        let ran = [history::Record {
            unix_timestamp: 1_700_000_000,
            entries: vec!["ripgrep".into()],
            names: Vec::new(),
            success: true,
            result: String::new(),
            log: None,
        }];
        let ripgrep = found.assess(catalog.find("ripgrep").unwrap(), &ran);
        assert_eq!(ripgrep.level, Level::NotYetValidated);
        assert_eq!(ripgrep.here, Some(1_700_000_000));

        // Before a run, critical entries short of a real test are named.
        // SDDM, tested in a VM, and GRUB, on hardware, are not.
        let warned = warnings(
            ["sddm", "grub", "htop", "arch-server-setup"],
            &|id| catalog.find(id),
            &found,
        );
        assert_eq!(warned.len(), 1, "{warned:?}");
        assert!(warned[0].starts_with("Arch Server Setup"), "{warned:?}");
        // Whole-system entries count as critical by their risk.
        assert!(found.critical(catalog.find("arch-server-setup").unwrap()));
    }

    #[test]
    fn a_build_without_a_record_says_so() {
        let found = evidence("", None);
        assert!(found.summary().contains("No container record"));
        let catalog = Catalog::builtin(false).unwrap();
        assert_eq!(
            found.assess(catalog.find("htop").unwrap(), &[]).level,
            Level::NotYetValidated
        );
    }

    #[test]
    fn the_catalogs_own_record_reads_and_names_real_entries() {
        let catalog = Catalog::builtin(false).unwrap();
        let found = Evidence::load(catalog.root());
        assert!(found.problems.is_empty(), "{:?}", found.problems);
        assert!(!found.claims.stand_ins.is_empty());
        for id in found
            .claims
            .stand_ins
            .iter()
            .flat_map(|group| group.entries.iter())
            .chain(found.claims.critical.entries.iter())
            .chain(found.claims.hardware.iter().map(|report| &report.entry))
            .chain(found.claims.vm.iter().map(|report| &report.entry))
        {
            assert!(catalog.find(id).is_some(), "validation.toml names {id}");
        }
    }
}