feagi-evolutionary 0.0.60

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
// Copyright 2026 Neuraville Inc.
// SPDX-License-Identifier: Apache-2.0

//! V7 normalizer: repairs well-known bad values in a genome stamped v7.
//!
//! Three corrections, applied in this order, each recorded by id:
//!
//! 1. `leak_coefficient` values in `(1, 100]` are legacy percents and become
//!    fractions (`v / 100`). Values above 100, negative or non-finite values are
//!    not converted: they stay in the genome and remain blocking validator errors.
//! 2. Memory twin areas whose `memory_twin_of` area is not defined are dropped.
//!    This discards the twin's neurons and learned rows, so each dropped twin is
//!    reported by id. References to a dropped twin (`memory_twin_areas` values
//!    and `cortical_mapping_dst` keys on other areas) are removed with it.
//! 3. Brain-region membership and IO lists (`cortical_areas`, `inputs`,
//!    `outputs`, `designated_inputs`, `designated_outputs`) lose entries that
//!    reference no defined area.

use std::collections::HashSet;

use serde_json::{Map, Value};

use super::{NormalizationDiagnostics, Normalizer};
use crate::genome::migration::MigrationError;
use crate::genome::parser::string_to_cortical_id;
use crate::genome::schema::GenomeSchemaVersion;
use crate::validator::LEAK_FRACTION_MAX;

/// Legacy leak values were percents; dividing by this scale gives the fraction.
const LEGACY_PERCENT_SCALE: f64 = 100.0;

/// Region list keys that hold cortical area ids.
const REGION_AREA_LIST_KEYS: [&str; 5] = [
    "cortical_areas",
    "inputs",
    "outputs",
    "designated_inputs",
    "designated_outputs",
];

/// Normalizer for schema v7.
#[derive(Debug, Default, Clone, Copy)]
pub struct V7Normalizer;

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

impl Normalizer for V7Normalizer {
    fn schema_version(&self) -> GenomeSchemaVersion {
        GenomeSchemaVersion(7)
    }

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

    fn normalize(&self, genome: &mut Value) -> Result<NormalizationDiagnostics, MigrationError> {
        let mut diag = NormalizationDiagnostics::new(GenomeSchemaVersion(7));
        let Some(root) = genome.as_object_mut() else {
            return Ok(diag);
        };
        let Some(Value::Object(blueprint)) = root.get_mut("blueprint") else {
            return Ok(diag);
        };
        convert_legacy_percent_leak(blueprint, &mut diag);
        drop_orphan_memory_twins(blueprint, &mut diag);
        let defined = defined_area_ids(blueprint);
        if let Some(Value::Object(regions)) = root.get_mut("brain_regions") {
            prune_region_references(regions, &defined, &mut diag);
        }
        Ok(diag)
    }
}

/// Canonical (base64) form of an area id, so raw and encoded spellings compare equal.
fn canonical_id(raw: &str) -> String {
    string_to_cortical_id(raw)
        .map(|id| id.as_base_64())
        .unwrap_or_else(|_| raw.to_string())
}

/// Canonical ids of every area in the blueprint.
fn defined_area_ids(blueprint: &Map<String, Value>) -> HashSet<String> {
    blueprint.keys().map(|key| canonical_id(key)).collect()
}

/// Sorted keys, so diagnostics and results are deterministic.
fn sorted_keys(map: &Map<String, Value>) -> Vec<String> {
    let mut keys: Vec<String> = map.keys().cloned().collect();
    keys.sort();
    keys
}

/// Convert legacy percent leaks in `(1, 100]` to fractions.
fn convert_legacy_percent_leak(
    blueprint: &mut Map<String, Value>,
    diag: &mut NormalizationDiagnostics,
) {
    for area_id in sorted_keys(blueprint) {
        let Some(area) = blueprint.get_mut(&area_id).and_then(|v| v.as_object_mut()) else {
            continue;
        };
        let Some(leak) = area.get("leak_coefficient").and_then(|v| v.as_f64()) else {
            continue;
        };
        if !leak.is_finite() || leak <= LEAK_FRACTION_MAX || leak > LEGACY_PERCENT_SCALE {
            continue;
        }
        let fraction = leak / LEGACY_PERCENT_SCALE;
        area.insert("leak_coefficient".to_string(), Value::from(fraction));
        diag.record(format!(
            "area {area_id}: leak_coefficient {leak} is a legacy percent; converted to fraction {fraction}"
        ));
    }
}

/// Drop twin areas whose `memory_twin_of` area is not defined, repeating until stable
/// so a twin of a dropped twin goes too.
fn drop_orphan_memory_twins(
    blueprint: &mut Map<String, Value>,
    diag: &mut NormalizationDiagnostics,
) {
    loop {
        let defined = defined_area_ids(blueprint);
        let mut orphans: Vec<(String, String)> = Vec::new();
        for area_id in sorted_keys(blueprint) {
            let twin_of = blueprint[&area_id]
                .get("memory_twin_of")
                .and_then(|v| v.as_str())
                .filter(|id| !id.is_empty());
            if let Some(twin_of) = twin_of {
                if !defined.contains(&canonical_id(twin_of)) {
                    orphans.push((area_id, twin_of.to_string()));
                }
            }
        }
        if orphans.is_empty() {
            return;
        }
        let mut dropped: HashSet<String> = HashSet::new();
        for (twin_id, twin_of) in orphans {
            blueprint.remove(&twin_id);
            dropped.insert(canonical_id(&twin_id));
            diag.record(format!(
                "dropped memory twin area {twin_id}: its memory_twin_of area {twin_of} is not defined (twin neurons and learned rows are discarded)"
            ));
        }
        scrub_dropped_twin_references(blueprint, &dropped, diag);
    }
}

/// Remove `memory_twin_areas` values and `cortical_mapping_dst` keys that name a dropped twin.
fn scrub_dropped_twin_references(
    blueprint: &mut Map<String, Value>,
    dropped: &HashSet<String>,
    diag: &mut NormalizationDiagnostics,
) {
    for owner_id in sorted_keys(blueprint) {
        let Some(owner) = blueprint.get_mut(&owner_id).and_then(|v| v.as_object_mut()) else {
            continue;
        };
        if let Some(Value::Object(index)) = owner.get_mut("memory_twin_areas") {
            for upstream_id in sorted_keys(index) {
                let names_dropped = index[&upstream_id]
                    .as_str()
                    .is_some_and(|twin| dropped.contains(&canonical_id(twin)));
                if names_dropped {
                    if let Some(removed) = index.remove(&upstream_id) {
                        diag.record(format!(
                            "area {owner_id}: removed memory_twin_areas entry {upstream_id} -> {} (twin dropped)",
                            removed.as_str().unwrap_or_default()
                        ));
                    }
                }
            }
        }
        if let Some(Value::Object(dst)) = owner.get_mut("cortical_mapping_dst") {
            for dst_id in sorted_keys(dst) {
                if dropped.contains(&canonical_id(&dst_id)) {
                    dst.remove(&dst_id);
                    diag.record(format!(
                        "area {owner_id}: removed cortical_mapping_dst entry to dropped twin {dst_id}"
                    ));
                }
            }
        }
    }
}

/// Prune region area-id lists, at the region level and under a nested `properties` map.
fn prune_region_references(
    regions: &mut Map<String, Value>,
    defined: &HashSet<String>,
    diag: &mut NormalizationDiagnostics,
) {
    for region_id in sorted_keys(regions) {
        let Some(region) = regions.get_mut(&region_id).and_then(|v| v.as_object_mut()) else {
            continue;
        };
        prune_area_lists(region, &region_id, "", defined, diag);
        if let Some(Value::Object(props)) = region.get_mut("properties") {
            prune_area_lists(props, &region_id, "properties.", defined, diag);
        }
    }
}

/// Drop string entries that name no defined area from each area-id list in `holder`.
fn prune_area_lists(
    holder: &mut Map<String, Value>,
    region_id: &str,
    label_prefix: &str,
    defined: &HashSet<String>,
    diag: &mut NormalizationDiagnostics,
) {
    for key in REGION_AREA_LIST_KEYS {
        let Some(Value::Array(entries)) = holder.get_mut(key) else {
            continue;
        };
        entries.retain(|entry| {
            let Some(id) = entry.as_str() else {
                return true;
            };
            if defined.contains(&canonical_id(id)) {
                return true;
            }
            diag.record(format!(
                "brain region {region_id}: pruned {label_prefix}{key} entry {id} (no defined area)"
            ));
            false
        });
    }
}

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

    fn leak_genome(leak: Value) -> Value {
        json!({
            "genome_schema_version": 7,
            "blueprint": { "area_a": { "leak_coefficient": leak } },
            "brain_regions": {}
        })
    }

    #[test]
    fn fraction_leak_is_unchanged() {
        let mut genome = leak_genome(json!(1.0));
        let diag = V7Normalizer::new().normalize(&mut genome).unwrap();
        assert!(diag.is_clean());
        assert_eq!(
            genome["blueprint"]["area_a"]["leak_coefficient"],
            json!(1.0)
        );
    }

    #[test]
    fn legacy_percent_leak_is_converted_and_recorded() {
        let mut genome = leak_genome(json!(10));
        let diag = V7Normalizer::new().normalize(&mut genome).unwrap();
        assert_eq!(
            genome["blueprint"]["area_a"]["leak_coefficient"],
            json!(0.1)
        );
        assert_eq!(diag.transformations.len(), 1);
        assert!(diag.transformations[0].contains("area_a"));

        let mut full = leak_genome(json!(100));
        V7Normalizer::new().normalize(&mut full).unwrap();
        assert_eq!(full["blueprint"]["area_a"]["leak_coefficient"], json!(1.0));
    }

    #[test]
    fn leak_outside_percent_range_is_not_converted() {
        for bad in [json!(150), json!(-0.5), json!(100.5)] {
            let mut genome = leak_genome(bad.clone());
            let diag = V7Normalizer::new().normalize(&mut genome).unwrap();
            assert!(diag.is_clean());
            assert_eq!(genome["blueprint"]["area_a"]["leak_coefficient"], bad);
        }
    }

    #[test]
    fn normalizing_twice_changes_nothing_more() {
        let mut genome = leak_genome(json!(10));
        V7Normalizer::new().normalize(&mut genome).unwrap();
        let again = V7Normalizer::new().normalize(&mut genome).unwrap();
        assert!(again.is_clean());
        assert_eq!(
            genome["blueprint"]["area_a"]["leak_coefficient"],
            json!(0.1)
        );
    }

    #[test]
    fn region_entries_without_a_defined_area_are_pruned_by_id() {
        let mut genome = json!({
            "blueprint": { "area_a": {}, "area_b": {} },
            "brain_regions": {
                "root": {
                    "cortical_areas": ["area_a", "gone_aa"],
                    "inputs": ["gone_bb"],
                    "outputs": ["area_b"],
                    "designated_inputs": ["area_a", "gone_cc"],
                    "designated_outputs": ["gone_dd"],
                    "properties": { "inputs": ["area_a", "gone_ee"] }
                }
            }
        });
        let diag = V7Normalizer::new().normalize(&mut genome).unwrap();
        let root = &genome["brain_regions"]["root"];
        assert_eq!(root["cortical_areas"], json!(["area_a"]));
        assert_eq!(root["inputs"], json!([]));
        assert_eq!(root["outputs"], json!(["area_b"]));
        assert_eq!(root["designated_inputs"], json!(["area_a"]));
        assert_eq!(root["designated_outputs"], json!([]));
        assert_eq!(root["properties"]["inputs"], json!(["area_a"]));
        for id in ["gone_aa", "gone_bb", "gone_cc", "gone_dd", "gone_ee"] {
            assert!(
                diag.transformations.iter().any(|t| t.contains(id)),
                "{id} must be listed"
            );
        }
        assert_eq!(diag.transformations.len(), 5);
    }

    #[test]
    fn region_membership_matches_across_raw_and_encoded_ids() {
        let power = string_to_cortical_id("_power").unwrap().as_base_64();
        let mut genome = json!({
            "blueprint": { "_power": {} },
            "brain_regions": { "root": { "cortical_areas": [power, "_power"] } }
        });
        let diag = V7Normalizer::new().normalize(&mut genome).unwrap();
        assert!(diag.is_clean());
    }

    #[test]
    fn orphan_twin_is_dropped_by_id_with_its_references() {
        let mut genome = json!({
            "blueprint": {
                "area_a": {},
                "memory_m": {
                    "memory_twin_areas": { "gone_up": "twin_t", "area_a": "twin_ok" },
                    "cortical_mapping_dst": { "twin_t": [{}], "twin_ok": [{}] }
                },
                "twin_t": { "memory_twin_of": "gone_up" },
                "twin_ok": { "memory_twin_of": "area_a" },
                "plain": { "memory_twin_of": null }
            },
            "brain_regions": { "root": { "cortical_areas": ["area_a", "twin_t", "twin_ok"] } }
        });
        let diag = V7Normalizer::new().normalize(&mut genome).unwrap();
        assert!(genome["blueprint"].get("twin_t").is_none());
        assert!(genome["blueprint"].get("twin_ok").is_some());
        assert!(genome["blueprint"].get("plain").is_some());
        assert_eq!(
            genome["blueprint"]["memory_m"]["memory_twin_areas"],
            json!({ "area_a": "twin_ok" })
        );
        assert_eq!(
            genome["blueprint"]["memory_m"]["cortical_mapping_dst"],
            json!({ "twin_ok": [{}] })
        );
        assert_eq!(
            genome["brain_regions"]["root"]["cortical_areas"],
            json!(["area_a", "twin_ok"])
        );
        assert!(diag
            .transformations
            .iter()
            .any(|t| t.contains("dropped memory twin area twin_t")));
        assert!(diag
            .transformations
            .iter()
            .any(|t| t.contains("pruned cortical_areas entry twin_t")));
    }

    #[test]
    fn twin_of_a_dropped_twin_is_dropped_too() {
        let mut genome = json!({
            "blueprint": {
                "twin_one": { "memory_twin_of": "gone_up" },
                "twin_two": { "memory_twin_of": "twin_one" }
            },
            "brain_regions": {}
        });
        let diag = V7Normalizer::new().normalize(&mut genome).unwrap();
        assert!(genome["blueprint"].as_object().unwrap().is_empty());
        assert!(diag.transformations.iter().any(|t| t.contains("twin_one")));
        assert!(diag.transformations.iter().any(|t| t.contains("twin_two")));
    }

    #[test]
    fn genome_without_a_blueprint_is_left_alone() {
        let mut genome = json!({
            "brain_regions": { "root": { "cortical_areas": ["anything"] } }
        });
        let diag = V7Normalizer::new().normalize(&mut genome).unwrap();
        assert!(diag.is_clean());
        assert_eq!(
            genome["brain_regions"]["root"]["cortical_areas"],
            json!(["anything"])
        );
    }
}