Skip to main content

feagi_evolutionary/genome/normalizers/
mod.rs

1// Copyright 2025 Neuraville Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Per-version genome normalizers.
5//!
6//! A `Normalizer` cleans up well-known bad values *within* a single schema
7//! version. It is structurally distinct from a `Migrator`, which advances
8//! the schema version. The chain runner invokes the normalizer for each
9//! "arrived at" version (post-hop or starting version when no migration is
10//! needed) before running that version's validator.
11//!
12//! Normalizers MUST follow the same invariants as migrators:
13//! determinism, idempotence, bounded compute, no side channels, JSON only,
14//! diagnostics over silence. They MUST NOT touch `genome_schema_version`;
15//! the runner is the single source of truth for version bookkeeping.
16//!
17//! See `feagi-core/docs/GENOME_SCHEMA_VERSIONING.md` and
18//! `crates/feagi-evolutionary/src/genome/README.md`.
19
20use serde_json::Value;
21
22use crate::genome::migration::MigrationError;
23use crate::genome::schema::GenomeSchemaVersion;
24
25pub mod v3;
26pub mod v6;
27
28pub use v3::V3Normalizer;
29pub use v6::V6Normalizer;
30
31/// Diagnostic record produced by a single normalizer pass.
32///
33/// Every correction the normalizer performs MUST contribute at least one
34/// entry to `transformations`. A normalizer that runs and produces zero
35/// diagnostics on a genome that needed corrections is a bug.
36#[derive(Debug, Clone)]
37pub struct NormalizationDiagnostics {
38    pub schema_version: GenomeSchemaVersion,
39    pub transformations: Vec<String>,
40}
41
42impl NormalizationDiagnostics {
43    pub fn new(schema_version: GenomeSchemaVersion) -> Self {
44        Self {
45            schema_version,
46            transformations: Vec::new(),
47        }
48    }
49
50    pub fn record(&mut self, msg: impl Into<String>) {
51        self.transformations.push(msg.into());
52    }
53
54    /// True when no corrections were applied.
55    pub fn is_clean(&self) -> bool {
56        self.transformations.is_empty()
57    }
58}
59
60/// In-place cleanup of a genome at a specific schema version.
61///
62/// Reuses `MigrationError` for failure reporting because normalizers and
63/// migrators raise structurally identical errors (step name, version,
64/// reason). Adding a parallel error enum would only duplicate the variants.
65pub trait Normalizer: Send + Sync {
66    /// Schema version this normalizer targets.
67    fn schema_version(&self) -> GenomeSchemaVersion;
68
69    /// Stable identifier for diagnostics and logs.
70    fn name(&self) -> &'static str;
71
72    /// Apply corrections in place. Returns diagnostics describing what
73    /// changed, or an error on hard failure.
74    fn normalize(&self, genome: &mut Value) -> Result<NormalizationDiagnostics, MigrationError>;
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn diagnostics_starts_clean() {
83        let d = NormalizationDiagnostics::new(GenomeSchemaVersion(3));
84        assert!(d.is_clean());
85        assert_eq!(d.transformations.len(), 0);
86        assert_eq!(d.schema_version, GenomeSchemaVersion(3));
87    }
88
89    #[test]
90    fn record_breaks_clean() {
91        let mut d = NormalizationDiagnostics::new(GenomeSchemaVersion(3));
92        d.record("set width 0 -> 1");
93        assert!(!d.is_clean());
94        assert_eq!(d.transformations, vec!["set width 0 -> 1".to_string()]);
95    }
96}