feagi-evolutionary 0.0.11

Evolution and Genome Management - Genotype operations for FEAGI
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
// Copyright 2025 Neuraville Inc.
// SPDX-License-Identifier: Apache-2.0

//! V3 normalizer: cleans well-known bad values in a v3 genome before
//! validation.
//!
//! Mirrors the semantics of the legacy `crate::validator::auto_fix_genome`
//! function, which still operates on `RuntimeGenome` and is retained for
//! external consumers (Python bindings, CLI tools). The two paths are
//! intentionally redundant during the rollout window: the chain runner
//! uses this JSON-level normalizer; external callers that already use
//! `auto_fix_genome` continue to work unchanged.
//!
//! Field paths are the **hierarchical** v3 JSON shape (post-flat→hierarchical
//! conversion, post-`migrate_genome`):
//!
//! - `physiology.simulation_timestep` (f64): if present and ≤ 0, replace
//!   with the default. The legacy alias `physiology.burst_delay` is read
//!   only by the runtime parser; this normalizer does not synthesize one
//!   field from the other.
//! - `physiology.max_age` (u64): if present and == 0, replace with default.
//! - `physiology.quantization_precision` (string): if empty/non-canonical/
//!   invalid, normalize or replace with default.
//! - `blueprint[id].block_boundaries` (`[u32; 3]`): any zero element is
//!   replaced with 1.
//! - `blueprint[id].per_voxel_neuron_cnt` (u64): if present and == 0,
//!   replaced with 1.
//!
//! Missing fields are not added here; `parse_physiology` and the cortical
//! area parser already fill defaults during JSON→`RuntimeGenome`. Adding
//! defaults here would change the on-wire genome shape, which is out of
//! scope for a normalizer.

use serde_json::{json, Value};

use super::{NormalizationDiagnostics, Normalizer};
use crate::genome::migration::MigrationError;
use crate::genome::schema::{GenomeSchemaVersion, CURRENT_SCHEMA_VERSION};

/// Default quantization precision used when the field is empty or invalid.
/// Mirrors `crate::runtime::default_quantization_precision()` to avoid a
/// runtime crate dependency cycle in module init.
const DEFAULT_QUANTIZATION_PRECISION: &str = "fp32";

/// Default simulation timestep used when the field is present but ≤ 0.
/// Mirrors `crate::runtime::PhysiologyConfig::default().simulation_timestep`.
const DEFAULT_SIMULATION_TIMESTEP: f64 = 0.025;

/// Default max age used when the field is present but == 0.
/// Mirrors `crate::runtime::PhysiologyConfig::default().max_age`.
const DEFAULT_MAX_AGE: u64 = 10_000_000;

#[derive(Debug, Default, Clone, Copy)]
pub struct V3Normalizer;

impl V3Normalizer {
    pub const fn new() -> Self {
        Self
    }
}

impl Normalizer for V3Normalizer {
    fn schema_version(&self) -> GenomeSchemaVersion {
        CURRENT_SCHEMA_VERSION
    }

    fn name(&self) -> &'static str {
        "v3_normalizer"
    }

    fn normalize(&self, genome: &mut Value) -> Result<NormalizationDiagnostics, MigrationError> {
        let mut diag = NormalizationDiagnostics::new(CURRENT_SCHEMA_VERSION);

        normalize_physiology(genome, &mut diag);
        normalize_blueprint(genome, &mut diag);

        Ok(diag)
    }
}

/// Apply corrections to the `physiology` section if present.
///
/// Silent when `physiology` is absent: the parser will fill defaults.
fn normalize_physiology(genome: &mut Value, diag: &mut NormalizationDiagnostics) {
    let physiology = match genome.get_mut("physiology").and_then(Value::as_object_mut) {
        Some(p) => p,
        None => return,
    };

    if let Some(ts) = physiology
        .get("simulation_timestep")
        .and_then(Value::as_f64)
    {
        if ts <= 0.0 {
            physiology.insert(
                "simulation_timestep".to_string(),
                json!(DEFAULT_SIMULATION_TIMESTEP),
            );
            diag.record(format!(
                "physiology.simulation_timestep {ts} -> {DEFAULT_SIMULATION_TIMESTEP} (default)"
            ));
        }
    }

    if let Some(age) = physiology.get("max_age").and_then(Value::as_u64) {
        if age == 0 {
            physiology.insert("max_age".to_string(), json!(DEFAULT_MAX_AGE));
            diag.record(format!(
                "physiology.max_age 0 -> {DEFAULT_MAX_AGE} (default)"
            ));
        }
    }

    let precision_action = match physiology
        .get("quantization_precision")
        .and_then(Value::as_str)
    {
        Some("") => Some(PrecisionAction::ReplaceWithDefault {
            previous: String::new(),
        }),
        Some(other) => match canonicalize_precision(other) {
            Some(canonical) if canonical != other => Some(PrecisionAction::Normalize {
                previous: other.to_string(),
                canonical,
            }),
            Some(_) => None,
            None => Some(PrecisionAction::ReplaceWithDefault {
                previous: other.to_string(),
            }),
        },
        None => None,
    };

    if let Some(action) = precision_action {
        match action {
            PrecisionAction::Normalize {
                previous,
                canonical,
            } => {
                physiology.insert(
                    "quantization_precision".to_string(),
                    Value::String(canonical.clone()),
                );
                diag.record(format!(
                    "physiology.quantization_precision '{previous}' -> '{canonical}' (normalized)"
                ));
            }
            PrecisionAction::ReplaceWithDefault { previous } => {
                physiology.insert(
                    "quantization_precision".to_string(),
                    Value::String(DEFAULT_QUANTIZATION_PRECISION.to_string()),
                );
                diag.record(format!(
                    "physiology.quantization_precision '{previous}' -> '{DEFAULT_QUANTIZATION_PRECISION}' (default)"
                ));
            }
        }
    }
}

enum PrecisionAction {
    Normalize { previous: String, canonical: String },
    ReplaceWithDefault { previous: String },
}

/// Returns the canonical lowercase form of a known precision token, or
/// `None` if the input is unrecognized.
///
/// The set of recognized tokens mirrors `feagi_npu_neural::types::Precision`
/// without taking a dependency on it, since this normalizer should not
/// pull in NPU types just to canonicalize a string.
fn canonicalize_precision(input: &str) -> Option<String> {
    match input.to_lowercase().as_str() {
        "fp32" | "f32" => Some("fp32".to_string()),
        "fp16" | "f16" => Some("fp16".to_string()),
        "int8" => Some("int8".to_string()),
        _ => None,
    }
}

/// Apply per-cortical-area corrections to the `blueprint` section if
/// present.
fn normalize_blueprint(genome: &mut Value, diag: &mut NormalizationDiagnostics) {
    let blueprint = match genome.get_mut("blueprint").and_then(Value::as_object_mut) {
        Some(b) => b,
        None => return,
    };

    let area_ids: Vec<String> = blueprint.keys().cloned().collect();
    for cortical_id in area_ids {
        let area = match blueprint
            .get_mut(&cortical_id)
            .and_then(Value::as_object_mut)
        {
            Some(a) => a,
            None => continue,
        };

        normalize_block_boundaries(area, &cortical_id, diag);
        normalize_per_voxel_neuron_cnt(area, &cortical_id, diag);
    }
}

fn normalize_block_boundaries(
    area: &mut serde_json::Map<String, Value>,
    cortical_id: &str,
    diag: &mut NormalizationDiagnostics,
) {
    let boundaries = match area
        .get_mut("block_boundaries")
        .and_then(Value::as_array_mut)
    {
        Some(b) if b.len() == 3 => b,
        _ => return,
    };

    static AXIS_NAMES: [&str; 3] = ["width", "height", "depth"];
    for (i, slot) in boundaries.iter_mut().enumerate() {
        if slot.as_u64() == Some(0) {
            *slot = json!(1u32);
            diag.record(format!(
                "blueprint['{cortical_id}'].block_boundaries[{i}] ({}) 0 -> 1",
                AXIS_NAMES[i]
            ));
        }
    }
}

fn normalize_per_voxel_neuron_cnt(
    area: &mut serde_json::Map<String, Value>,
    cortical_id: &str,
    diag: &mut NormalizationDiagnostics,
) {
    if area.get("per_voxel_neuron_cnt").and_then(Value::as_u64) == Some(0) {
        area.insert("per_voxel_neuron_cnt".to_string(), json!(1u32));
        diag.record(format!(
            "blueprint['{cortical_id}'].per_voxel_neuron_cnt 0 -> 1"
        ));
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn reports_current_schema_version() {
        let n = V3Normalizer::new();
        assert_eq!(n.schema_version(), CURRENT_SCHEMA_VERSION);
        assert_eq!(n.name(), "v3_normalizer");
    }

    #[test]
    fn clean_genome_yields_clean_diagnostics() {
        let n = V3Normalizer::new();
        let mut g = json!({
            "physiology": {
                "simulation_timestep": 0.025,
                "max_age": 10_000_000,
                "quantization_precision": "fp32"
            },
            "blueprint": {
                "abc12345": {
                    "block_boundaries": [10, 10, 10],
                    "per_voxel_neuron_cnt": 1
                }
            }
        });
        let d = n.normalize(&mut g).unwrap();
        assert!(d.is_clean());
    }

    #[test]
    fn fixes_negative_simulation_timestep() {
        let n = V3Normalizer::new();
        let mut g = json!({
            "physiology": { "simulation_timestep": -0.1 }
        });
        let d = n.normalize(&mut g).unwrap();
        assert_eq!(g["physiology"]["simulation_timestep"], json!(0.025));
        assert_eq!(d.transformations.len(), 1);
        assert!(d.transformations[0].contains("simulation_timestep"));
    }

    #[test]
    fn fixes_zero_simulation_timestep() {
        let n = V3Normalizer::new();
        let mut g = json!({ "physiology": { "simulation_timestep": 0.0 } });
        let d = n.normalize(&mut g).unwrap();
        assert_eq!(g["physiology"]["simulation_timestep"], json!(0.025));
        assert!(!d.is_clean());
    }

    #[test]
    fn leaves_burst_delay_alone() {
        // The legacy alias is parser territory; the normalizer must not
        // synthesize fields. parse_physiology will fold burst_delay into
        // simulation_timestep at deserialize time.
        let n = V3Normalizer::new();
        let mut g = json!({ "physiology": { "burst_delay": 0.030 } });
        let d = n.normalize(&mut g).unwrap();
        assert!(d.is_clean());
        assert_eq!(g["physiology"]["burst_delay"], json!(0.030));
        assert!(g["physiology"].get("simulation_timestep").is_none());
    }

    #[test]
    fn fixes_zero_max_age() {
        let n = V3Normalizer::new();
        let mut g = json!({ "physiology": { "max_age": 0 } });
        let d = n.normalize(&mut g).unwrap();
        assert_eq!(g["physiology"]["max_age"], json!(DEFAULT_MAX_AGE));
        assert!(!d.is_clean());
    }

    #[test]
    fn replaces_empty_precision_with_default() {
        let n = V3Normalizer::new();
        let mut g = json!({ "physiology": { "quantization_precision": "" } });
        let d = n.normalize(&mut g).unwrap();
        assert_eq!(g["physiology"]["quantization_precision"], json!("fp32"));
        assert_eq!(d.transformations.len(), 1);
    }

    #[test]
    fn normalizes_uppercase_precision() {
        let n = V3Normalizer::new();
        let mut g = json!({ "physiology": { "quantization_precision": "FP32" } });
        let d = n.normalize(&mut g).unwrap();
        assert_eq!(g["physiology"]["quantization_precision"], json!("fp32"));
        assert!(d.transformations[0].contains("normalized"));
    }

    #[test]
    fn normalizes_f32_alias_precision() {
        let n = V3Normalizer::new();
        let mut g = json!({ "physiology": { "quantization_precision": "f32" } });
        let d = n.normalize(&mut g).unwrap();
        assert_eq!(g["physiology"]["quantization_precision"], json!("fp32"));
        assert!(d.transformations[0].contains("normalized"));
    }

    #[test]
    fn replaces_invalid_precision_with_default() {
        let n = V3Normalizer::new();
        let mut g = json!({ "physiology": { "quantization_precision": "garbage" } });
        let d = n.normalize(&mut g).unwrap();
        assert_eq!(g["physiology"]["quantization_precision"], json!("fp32"));
        assert!(d.transformations[0].contains("default"));
    }

    #[test]
    fn fixes_zero_block_boundaries_per_axis() {
        let n = V3Normalizer::new();
        let mut g = json!({
            "blueprint": {
                "abc12345": { "block_boundaries": [0, 5, 0] }
            }
        });
        let d = n.normalize(&mut g).unwrap();
        assert_eq!(
            g["blueprint"]["abc12345"]["block_boundaries"],
            json!([1, 5, 1])
        );
        assert_eq!(d.transformations.len(), 2);
    }

    #[test]
    fn fixes_zero_per_voxel_neuron_cnt() {
        let n = V3Normalizer::new();
        let mut g = json!({
            "blueprint": {
                "abc12345": { "per_voxel_neuron_cnt": 0 }
            }
        });
        let d = n.normalize(&mut g).unwrap();
        assert_eq!(g["blueprint"]["abc12345"]["per_voxel_neuron_cnt"], json!(1));
        assert_eq!(d.transformations.len(), 1);
    }

    #[test]
    fn handles_missing_fields_silently() {
        // Missing fields are parser territory; the normalizer must not
        // synthesize them. This pins the contract.
        let n = V3Normalizer::new();
        let mut g = json!({});
        let d = n.normalize(&mut g).unwrap();
        assert!(d.is_clean());
        assert_eq!(g, json!({}));
    }

    #[test]
    fn is_idempotent() {
        // Running the normalizer twice must produce the same output and
        // an empty diagnostics on the second pass.
        let n = V3Normalizer::new();
        let mut g = json!({
            "physiology": {
                "simulation_timestep": 0.0,
                "max_age": 0,
                "quantization_precision": ""
            },
            "blueprint": {
                "abc12345": {
                    "block_boundaries": [0, 0, 0],
                    "per_voxel_neuron_cnt": 0
                }
            }
        });

        let d1 = n.normalize(&mut g).unwrap();
        assert!(!d1.is_clean());
        let snapshot = g.clone();

        let d2 = n.normalize(&mut g).unwrap();
        assert!(d2.is_clean());
        assert_eq!(g, snapshot);
    }
}