Skip to main content

feagi_evolutionary/genome/validators/
mod.rs

1// Copyright 2025 Neuraville Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Per-version genome validators.
5//!
6//! A `Validator` checks structural integrity, parameter ranges, and
7//! cross-references at a *specific* `GenomeSchemaVersion`. The chain runner
8//! invokes per-version validators between hops as **advisory** and the
9//! validator at the final target version as **blocking**.
10//!
11//! See `feagi-core/docs/GENOME_SCHEMA_VERSIONING.md` and
12//! `crates/feagi-evolutionary/src/genome/README.md` for the design contract.
13//!
14//! Validators MUST NOT mutate the genome. Mutation belongs in
15//! `crate::genome::migration::Migrator`. See the README's anti-patterns.
16
17use serde_json::Value;
18
19use crate::genome::schema::GenomeSchemaVersion;
20
21pub mod v3;
22pub mod v4;
23pub mod v5;
24pub mod v6;
25pub mod v7;
26
27pub use v3::V3Validator;
28pub use v4::V4Validator;
29pub use v5::V5Validator;
30pub use v6::V6Validator;
31pub use v7::V7Validator;
32
33/// Outcome of running a single validator against a genome.
34///
35/// `errors` are blocking issues. `warnings` are advisory. The validator does
36/// not decide whether to abort the chain; the chain runner makes that call
37/// based on whether the validator was the latest (blocking) or intermediate
38/// (advisory).
39#[derive(Debug, Clone, Default)]
40pub struct ValidationReport {
41    /// Schema version this report describes. `None` only when constructed
42    /// via `Default` for testing scaffolding; production validators always
43    /// stamp their version.
44    pub schema_version: Option<GenomeSchemaVersion>,
45    pub errors: Vec<String>,
46    pub warnings: Vec<String>,
47}
48
49impl ValidationReport {
50    pub fn new(schema_version: GenomeSchemaVersion) -> Self {
51        Self {
52            schema_version: Some(schema_version),
53            errors: Vec::new(),
54            warnings: Vec::new(),
55        }
56    }
57
58    pub fn add_error(&mut self, msg: impl Into<String>) {
59        self.errors.push(msg.into());
60    }
61
62    pub fn add_warning(&mut self, msg: impl Into<String>) {
63        self.warnings.push(msg.into());
64    }
65
66    /// True when the report carries at least one blocking error.
67    pub fn has_errors(&self) -> bool {
68        !self.errors.is_empty()
69    }
70
71    /// True when the report carries no errors and no warnings.
72    pub fn is_clean(&self) -> bool {
73        self.errors.is_empty() && self.warnings.is_empty()
74    }
75}
76
77/// Validates a genome at a specific schema version.
78///
79/// Validators are stateless; the trait is `Send + Sync` so registries can be
80/// shared across threads. Implementations must not mutate the input or
81/// perform I/O.
82pub trait Validator: Send + Sync {
83    /// The schema version this validator targets.
84    fn schema_version(&self) -> GenomeSchemaVersion;
85
86    /// Inspect a genome and return findings. Never mutates.
87    fn validate(&self, genome: &Value) -> ValidationReport;
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    #[test]
95    fn report_starts_clean() {
96        let r = ValidationReport::new(GenomeSchemaVersion(3));
97        assert!(r.is_clean());
98        assert!(!r.has_errors());
99        assert_eq!(r.schema_version, Some(GenomeSchemaVersion(3)));
100    }
101
102    #[test]
103    fn add_error_breaks_clean_and_blocks() {
104        let mut r = ValidationReport::new(GenomeSchemaVersion(3));
105        r.add_error("missing field");
106        assert!(r.has_errors());
107        assert!(!r.is_clean());
108        assert_eq!(r.errors, vec!["missing field".to_string()]);
109    }
110
111    #[test]
112    fn warning_is_advisory_only() {
113        let mut r = ValidationReport::new(GenomeSchemaVersion(3));
114        r.add_warning("nudge");
115        assert!(!r.has_errors());
116        assert!(!r.is_clean());
117        assert_eq!(r.warnings, vec!["nudge".to_string()]);
118    }
119}