Skip to main content

feagi_brain_development/
connectome_manager.rs

1// Copyright 2025 Neuraville Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4/*!
5ConnectomeManager - Core brain connectivity manager.
6
7This is the central orchestrator for the FEAGI connectome, managing:
8- Cortical areas and their metadata
9- Brain regions and hierarchy
10- Neuron/synapse queries (delegates to NPU for actual data)
11- Genome loading and persistence
12
13## Architecture
14
15The ConnectomeManager is a **metadata manager** that:
161. Stores cortical area/region definitions
172. Provides a high-level API for brain structure queries
183. Delegates neuron/synapse CRUD to the NPU (Structure of Arrays)
19
20## Design Principles
21
22- **Singleton**: One global instance per FEAGI process
23- **Thread-safe**: Uses RwLock for concurrent reads
24- **Performance**: Optimized for hot-path queries (area lookups)
25- **NPU Delegation**: Neuron/synapse data lives in NPU, not here
26
27Copyright 2025 Neuraville Inc.
28Licensed under the Apache License, Version 2.0
29*/
30
31use once_cell::sync::Lazy;
32use parking_lot::RwLock;
33use serde::{Deserialize, Serialize};
34use std::collections::HashMap;
35use std::hash::Hasher;
36use std::sync::atomic::{AtomicUsize, Ordering};
37use std::sync::{Arc, Mutex};
38use tracing::{debug, error, info, trace, warn};
39use xxhash_rust::xxh64::Xxh64;
40
41/// Merged region `inputs` / `outputs` (base64 cortical IDs) after `recompute_brain_region_io_registry`.
42pub type BrainRegionIoRegistry = HashMap<String, (Vec<String>, Vec<String>)>;
43
44use crate::models::{BrainRegion, BrainRegionHierarchy, CorticalArea, CorticalAreaDimensions};
45use crate::types::{BduError, BduResult};
46use feagi_npu_neural::synapse::SYNAPSE_EDGE_ASSOCIATIVE_MEMORY;
47use feagi_npu_neural::types::NeuronId;
48use feagi_structures::genomic::cortical_area::{
49    CoreCorticalType, CorticalAreaType, CorticalID, CustomCorticalType,
50};
51use feagi_structures::genomic::descriptors::GenomeCoordinate3D;
52
53// State manager access for fatigue calculation
54// Note: feagi-state-manager is always available when std is enabled (it's a default feature)
55use feagi_state_manager::StateManager;
56
57const DATA_HASH_SEED: u64 = 0;
58// JSON number precision (Godot) is limited to 53 bits; mask to keep hashes stable across transports.
59const HASH_SAFE_MASK: u64 = (1u64 << 53) - 1;
60
61// NPU integration (optional dependency)
62// use feagi_npu_burst_engine::RustNPU; // Now using DynamicNPU
63
64/// Global singleton instance of ConnectomeManager
65static INSTANCE: Lazy<Arc<RwLock<ConnectomeManager>>> =
66    Lazy::new(|| Arc::new(RwLock::new(ConnectomeManager::new())));
67
68/// Configuration for ConnectomeManager
69#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct ConnectomeConfig {
71    /// Maximum number of neurons (for NPU sizing)
72    pub max_neurons: usize,
73
74    /// Maximum number of synapses (for NPU sizing)
75    pub max_synapses: usize,
76
77    /// Backend type ("cpu", "cuda", "wgpu")
78    pub backend: String,
79}
80
81impl Default for ConnectomeConfig {
82    fn default() -> Self {
83        Self {
84            max_neurons: 10_000_000,
85            max_synapses: 100_000_000,
86            backend: "cpu".to_string(),
87        }
88    }
89}
90
91/// One-call diagnostic for memory twin creation/health on a mapping pair.
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct MemoryTwinDiagnostic {
94    pub src_cortical_area: String,
95    pub dst_cortical_area: String,
96    pub src_cortical_type: String,
97    pub dst_cortical_type: String,
98    pub mapping_exists: bool,
99    pub episodic_rule_count: usize,
100    pub twin_expected: bool,
101    pub twin_present: bool,
102    pub twin_cortical_area: Option<String>,
103    pub reason: Option<String>,
104    pub src_parent_region_id: Option<String>,
105    pub dst_parent_region_id: Option<String>,
106    pub twin_parent_region_id: Option<String>,
107    pub twin_parent_matches_src_parent: Option<bool>,
108}
109
110/// Central manager for the FEAGI connectome
111///
112/// ## Responsibilities
113///
114/// 1. **Cortical Area Management**: Add, remove, query cortical areas
115/// 2. **Brain Region Management**: Hierarchical organization
116/// 3. **Neuron/Synapse Queries**: High-level API (delegates to NPU)
117/// 4. **Genome I/O**: Load/save brain structure
118///
119/// ## Data Storage
120///
121/// - **Cortical areas**: Stored in HashMap for O(1) lookup
122/// - **Brain regions**: Stored in BrainRegionHierarchy
123/// - **Neuron data**: Lives in NPU (not stored here)
124/// - **Synapse data**: Lives in NPU (not stored here)
125///
126/// ## Thread Safety
127///
128/// Uses `RwLock` for concurrent reads with exclusive writes.
129/// Multiple threads can read simultaneously, but writes block.
130///
131pub struct ConnectomeManager {
132    /// Map of cortical_id -> CorticalArea metadata
133    cortical_areas: HashMap<CorticalID, CorticalArea>,
134
135    /// Map of cortical_id -> cortical_idx (fast reverse lookup)
136    cortical_id_to_idx: HashMap<CorticalID, u32>,
137
138    /// Map of cortical_idx -> cortical_id (fast reverse lookup)
139    cortical_idx_to_id: HashMap<u32, CorticalID>,
140
141    /// Next available cortical index
142    next_cortical_idx: u32,
143
144    /// Brain region hierarchy
145    brain_regions: BrainRegionHierarchy,
146
147    /// Morphology registry from loaded genome
148    morphology_registry: feagi_evolutionary::MorphologyRegistry,
149
150    /// Configuration
151    config: ConnectomeConfig,
152
153    /// Optional reference to the Rust NPU for neuron/synapse queries
154    ///
155    /// This is set by the Python process manager after NPU initialization.
156    /// All neuron/synapse data queries delegate to the NPU.
157    /// Wrapped in TracingMutex to automatically log all lock acquisitions
158    npu: Option<Arc<feagi_npu_burst_engine::TracingMutex<feagi_npu_burst_engine::DynamicNPU>>>,
159
160    /// Plasticity executor reference (optional, only when plasticity feature is enabled)
161    #[cfg(feature = "plasticity")]
162    plasticity_executor:
163        Option<Arc<std::sync::Mutex<feagi_npu_plasticity::AsyncPlasticityExecutor>>>,
164
165    /// Cached neuron count (lock-free read) - updated by burst engine
166    /// This prevents health checks from blocking on NPU lock
167    cached_neuron_count: Arc<AtomicUsize>,
168
169    /// Cached synapse count (lock-free read) - updated by burst engine
170    /// This prevents health checks from blocking on NPU lock
171    cached_synapse_count: Arc<AtomicUsize>,
172
173    /// Per-area neuron count cache (lock-free reads) - updated when neurons are created/deleted
174    /// This prevents health checks from blocking on NPU lock
175    cached_neuron_counts_per_area: Arc<RwLock<HashMap<CorticalID, AtomicUsize>>>,
176
177    /// Per-area synapse count cache (lock-free reads) - updated when synapses are created/deleted
178    /// This prevents health checks from blocking on NPU lock
179    cached_synapse_counts_per_area: Arc<RwLock<HashMap<CorticalID, AtomicUsize>>>,
180
181    /// Is the connectome initialized (has cortical areas)?
182    initialized: bool,
183
184    /// Last fatigue index calculation time (for rate limiting)
185    last_fatigue_calculation: Arc<Mutex<std::time::Instant>>,
186}
187
188/// Type alias for neuron batch data: (x, y, z, threshold, threshold_limit, leak, resting, neuron_type, refractory_period, excitability, consecutive_fire_limit, snooze_period, mp_charge_accumulation)
189type NeuronData = (
190    u32,
191    u32,
192    u32,
193    f32,
194    f32,
195    f32,
196    f32,
197    i32,
198    u16,
199    f32,
200    u16,
201    u16,
202    bool,
203);
204
205impl ConnectomeManager {
206    fn get_mapping_rules_for_destination<'a>(
207        mapping_dst: &'a serde_json::Map<String, serde_json::Value>,
208        dst_area_id: &CorticalID,
209    ) -> Option<&'a Vec<serde_json::Value>> {
210        if let Some(rules) = mapping_dst
211            .get(&dst_area_id.as_base_64())
212            .and_then(|value| value.as_array())
213        {
214            return Some(rules);
215        }
216
217        // Compatibility path: some legacy genomes may still store destination IDs
218        // as 6/8-char ASCII keys instead of base64. Resolve by semantic ID equality.
219        for (raw_dst_key, rules_value) in mapping_dst {
220            let parsed_dst = CorticalID::try_from_base_64(raw_dst_key)
221                .or_else(|_| CorticalID::try_from_legacy_ascii(raw_dst_key));
222            if parsed_dst.as_ref().ok() != Some(dst_area_id) {
223                continue;
224            }
225            if let Some(rules) = rules_value.as_array() {
226                return Some(rules);
227            }
228        }
229
230        None
231    }
232
233    /// Create a new ConnectomeManager (private - use `instance()`)
234    fn new() -> Self {
235        Self {
236            cortical_areas: HashMap::new(),
237            cortical_id_to_idx: HashMap::new(),
238            cortical_idx_to_id: HashMap::new(),
239            // CRITICAL: Reserve indices for invariant core areas (0..=6).
240            next_cortical_idx: 7, // Reserve 0=_death, 1=_power, 2=_fatigue, 3=_pain, 4=_pleasure, 5=_fear, 6=_hope
241            brain_regions: BrainRegionHierarchy::new(),
242            morphology_registry: feagi_evolutionary::MorphologyRegistry::new(),
243            config: ConnectomeConfig::default(),
244            npu: None,
245            #[cfg(feature = "plasticity")]
246            plasticity_executor: None,
247            cached_neuron_count: Arc::new(AtomicUsize::new(0)),
248            cached_synapse_count: Arc::new(AtomicUsize::new(0)),
249            cached_neuron_counts_per_area: Arc::new(RwLock::new(HashMap::new())),
250            cached_synapse_counts_per_area: Arc::new(RwLock::new(HashMap::new())),
251            initialized: false,
252            last_fatigue_calculation: Arc::new(Mutex::new(
253                std::time::Instant::now() - std::time::Duration::from_secs(10),
254            )), // Initialize to allow first calculation
255        }
256    }
257
258    /// Get the global singleton instance
259    ///
260    /// # Returns
261    ///
262    /// Arc to the ConnectomeManager wrapped in RwLock
263    ///
264    /// # Example
265    ///
266    /// ```ignore
267    /// use feagi_brain_development::ConnectomeManager;
268    ///
269    /// let manager = ConnectomeManager::instance();
270    /// let read_lock = manager.read();
271    /// let area_count = read_lock.get_cortical_area_count();
272    /// ```
273    ///
274    pub fn instance() -> Arc<RwLock<ConnectomeManager>> {
275        // Note: Singleton is always f32 for backward compatibility
276        // New code should use ConnectomeManager::<T>::new_for_testing_with_npu() for custom types
277        Arc::clone(&*INSTANCE)
278    }
279
280    /// Calculate optimal visualization voxel granularity for a cortical area
281    ///
282    /// This function determines the granularity for aggregated rendering based on:
283    /// - Total voxel count (larger areas get larger chunks)
284    /// - Aspect ratio (handles thin dimensions like 1024×900×3)
285    /// - Target chunk count (~2k-10k chunks for manageable message size)
286    ///
287    /// # Arguments
288    ///
289    /// * `dimensions` - The cortical area dimensions (width, height, depth)
290    ///
291    /// # Returns
292    ///
293    /// Tuple of (chunk_x, chunk_y, chunk_z) that divides evenly into dimensions
294    ///
295    ///
296    /// Create a new isolated instance for testing
297    ///
298    /// This bypasses the singleton pattern and creates a fresh instance.
299    /// Use this in tests to avoid conflicts between parallel test runs.
300    ///
301    /// # Example
302    ///
303    /// ```rust
304    /// let manager = ConnectomeManager::new_for_testing();
305    /// // Use manager in isolated test
306    /// ```
307    pub fn new_for_testing() -> Self {
308        Self {
309            cortical_areas: HashMap::new(),
310            cortical_id_to_idx: HashMap::new(),
311            cortical_idx_to_id: HashMap::new(),
312            next_cortical_idx: 0,
313            brain_regions: BrainRegionHierarchy::new(),
314            morphology_registry: feagi_evolutionary::MorphologyRegistry::new(),
315            config: ConnectomeConfig::default(),
316            npu: None,
317            #[cfg(feature = "plasticity")]
318            plasticity_executor: None,
319            cached_neuron_count: Arc::new(AtomicUsize::new(0)),
320            cached_synapse_count: Arc::new(AtomicUsize::new(0)),
321            cached_neuron_counts_per_area: Arc::new(RwLock::new(HashMap::new())),
322            cached_synapse_counts_per_area: Arc::new(RwLock::new(HashMap::new())),
323            initialized: false,
324            last_fatigue_calculation: Arc::new(Mutex::new(
325                std::time::Instant::now() - std::time::Duration::from_secs(10),
326            )),
327        }
328    }
329
330    /// Create a new isolated instance for testing with NPU
331    ///
332    /// This bypasses the singleton pattern and creates a fresh instance with NPU connected.
333    /// Use this in tests to avoid conflicts between parallel test runs.
334    ///
335    /// # Arguments
336    ///
337    /// * `npu` - Arc<TracingMutex<DynamicNPU>> to connect to this manager
338    ///
339    /// # Example
340    ///
341    /// ```rust
342    /// let npu = Arc::new(TracingMutex::new(RustNPU::new(1_000_000, 10_000_000, 10), "NPU"));
343    /// let manager = ConnectomeManager::new_for_testing_with_npu(npu);
344    /// ```
345    pub fn new_for_testing_with_npu(
346        npu: Arc<feagi_npu_burst_engine::TracingMutex<feagi_npu_burst_engine::DynamicNPU>>,
347    ) -> Self {
348        Self {
349            cortical_areas: HashMap::new(),
350            cortical_id_to_idx: HashMap::new(),
351            cortical_idx_to_id: HashMap::new(),
352            next_cortical_idx: 7,
353            brain_regions: BrainRegionHierarchy::new(),
354            morphology_registry: feagi_evolutionary::MorphologyRegistry::new(),
355            config: ConnectomeConfig::default(),
356            npu: Some(npu),
357            #[cfg(feature = "plasticity")]
358            plasticity_executor: None,
359            cached_neuron_count: Arc::new(AtomicUsize::new(0)),
360            cached_synapse_count: Arc::new(AtomicUsize::new(0)),
361            cached_neuron_counts_per_area: Arc::new(RwLock::new(HashMap::new())),
362            cached_synapse_counts_per_area: Arc::new(RwLock::new(HashMap::new())),
363            initialized: false,
364            last_fatigue_calculation: Arc::new(Mutex::new(
365                std::time::Instant::now() - std::time::Duration::from_secs(10),
366            )),
367        }
368    }
369
370    /// Set up core morphologies in the registry (for testing only)
371    ///
372    /// This is a test helper to set up core morphologies (projector, block_to_block, etc.)
373    /// in the morphology registry so that synaptogenesis tests can run.
374    ///
375    /// # Note
376    ///
377    /// This should only be called in tests. Morphologies are typically loaded from genome files.
378    /// This method is public to allow integration tests to access it.
379    pub fn setup_core_morphologies_for_testing(&mut self) {
380        feagi_evolutionary::add_core_morphologies(&mut self.morphology_registry);
381    }
382
383    /// Reset the singleton (for testing only)
384    ///
385    /// # Safety
386    ///
387    /// This should only be called in tests to reset state between test runs.
388    /// Calling this in production code will cause all references to the old
389    /// instance to become stale.
390    ///
391    #[cfg(test)]
392    pub fn reset_for_testing() {
393        let mut instance = INSTANCE.write();
394        *instance = Self::new();
395    }
396
397    // ======================================================================
398    // Data Hashing (event-driven updates for health_check)
399    // ======================================================================
400
401    /// Update stored hashes for data types that have changed.
402    fn update_state_hashes(
403        &self,
404        brain_regions: Option<u64>,
405        cortical_areas: Option<u64>,
406        brain_geometry: Option<u64>,
407        morphologies: Option<u64>,
408        cortical_mappings: Option<u64>,
409    ) {
410        let state_manager = StateManager::instance();
411        let state_manager = state_manager.read();
412        if let Some(value) = brain_regions {
413            state_manager.set_brain_regions_hash(value);
414        }
415        if let Some(value) = cortical_areas {
416            state_manager.set_cortical_areas_hash(value);
417        }
418        if let Some(value) = brain_geometry {
419            state_manager.set_brain_geometry_hash(value);
420        }
421        if let Some(value) = morphologies {
422            state_manager.set_morphologies_hash(value);
423        }
424        if let Some(value) = cortical_mappings {
425            state_manager.set_cortical_mappings_hash(value);
426        }
427    }
428
429    /// Refresh the brain regions hash (hierarchy, membership, and properties).
430    fn refresh_brain_regions_hash(&self) {
431        let hash = self.compute_brain_regions_hash();
432        self.update_state_hashes(Some(hash), None, None, None, None);
433    }
434
435    #[allow(dead_code)]
436    /// Refresh the cortical areas hash (metadata and properties).
437    fn refresh_cortical_areas_hash(&self) {
438        let hash = self.compute_cortical_areas_hash();
439        self.update_state_hashes(None, Some(hash), None, None, None);
440    }
441
442    #[allow(dead_code)]
443    /// Refresh the brain geometry hash (positions, dimensions, 2D coordinates).
444    fn refresh_brain_geometry_hash(&self) {
445        let hash = self.compute_brain_geometry_hash();
446        self.update_state_hashes(None, None, Some(hash), None, None);
447    }
448
449    /// Refresh the morphologies hash.
450    fn refresh_morphologies_hash(&self) {
451        let hash = self.compute_morphologies_hash();
452        self.update_state_hashes(None, None, None, Some(hash), None);
453    }
454
455    /// Refresh the cortical mappings hash.
456    pub fn refresh_cortical_mappings_hash(&self) {
457        let hash = self.compute_cortical_mappings_hash();
458        self.update_state_hashes(None, None, None, None, Some(hash));
459    }
460
461    /// Refresh cortical area-related hashes based on the affected data.
462    pub fn refresh_cortical_area_hashes(&self, properties_changed: bool, geometry_changed: bool) {
463        let cortical_hash = if properties_changed {
464            Some(self.compute_cortical_areas_hash())
465        } else {
466            None
467        };
468        let geometry_hash = if geometry_changed {
469            Some(self.compute_brain_geometry_hash())
470        } else {
471            None
472        };
473        self.update_state_hashes(None, cortical_hash, geometry_hash, None, None);
474    }
475
476    /// Compute hash for brain regions (hierarchy, membership, and properties).
477    fn compute_brain_regions_hash(&self) -> u64 {
478        let mut hasher = Xxh64::new(DATA_HASH_SEED);
479        let mut region_ids: Vec<String> = self
480            .brain_regions
481            .get_all_region_ids()
482            .into_iter()
483            .cloned()
484            .collect();
485        region_ids.sort();
486
487        for region_id in region_ids {
488            let Some(region) = self.brain_regions.get_region(&region_id) else {
489                continue;
490            };
491            Self::hash_str(&mut hasher, &region_id);
492            Self::hash_str(&mut hasher, &region.name);
493            Self::hash_str(&mut hasher, &region.region_type.to_string());
494            let parent_id = self.brain_regions.get_parent(&region_id);
495            match parent_id {
496                Some(parent) => Self::hash_str(&mut hasher, parent),
497                None => Self::hash_str(&mut hasher, "null"),
498            }
499
500            let mut cortical_ids: Vec<String> = region
501                .cortical_areas
502                .iter()
503                .map(|id| id.as_base_64())
504                .collect();
505            cortical_ids.sort();
506            for cortical_id in cortical_ids {
507                Self::hash_str(&mut hasher, &cortical_id);
508            }
509
510            Self::hash_properties_filtered(&mut hasher, &region.properties, &[]);
511        }
512
513        hasher.finish() & HASH_SAFE_MASK
514    }
515
516    /// Compute hash for cortical areas and properties (excluding mappings).
517    fn compute_cortical_areas_hash(&self) -> u64 {
518        let mut hasher = Xxh64::new(DATA_HASH_SEED);
519        let mut areas: Vec<&CorticalArea> = self.cortical_areas.values().collect();
520        areas.sort_by_key(|area| area.cortical_id.as_base_64());
521
522        for area in areas {
523            let cortical_id = area.cortical_id.as_base_64();
524            Self::hash_str(&mut hasher, &cortical_id);
525            hasher.write_u32(area.cortical_idx);
526            Self::hash_str(&mut hasher, &area.name);
527            Self::hash_str(&mut hasher, &area.cortical_type.to_string());
528
529            let excluded = ["cortical_mapping_dst", "upstream_cortical_areas"];
530            Self::hash_properties_filtered(&mut hasher, &area.properties, &excluded);
531        }
532
533        hasher.finish() & HASH_SAFE_MASK
534    }
535
536    /// Compute hash for brain geometry (area positions, dimensions, and 2D coordinates).
537    fn compute_brain_geometry_hash(&self) -> u64 {
538        let mut hasher = Xxh64::new(DATA_HASH_SEED);
539        let mut areas: Vec<&CorticalArea> = self.cortical_areas.values().collect();
540        areas.sort_by_key(|area| area.cortical_id.as_base_64());
541
542        for area in areas {
543            let cortical_id = area.cortical_id.as_base_64();
544            Self::hash_str(&mut hasher, &cortical_id);
545
546            Self::hash_i32(&mut hasher, area.position.x);
547            Self::hash_i32(&mut hasher, area.position.y);
548            Self::hash_i32(&mut hasher, area.position.z);
549
550            Self::hash_u32(&mut hasher, area.dimensions.width);
551            Self::hash_u32(&mut hasher, area.dimensions.height);
552            Self::hash_u32(&mut hasher, area.dimensions.depth);
553
554            let coord_2d = area
555                .properties
556                .get("coordinate_2d")
557                .or_else(|| area.properties.get("coordinates_2d"));
558            match coord_2d {
559                Some(value) => Self::hash_json_value(&mut hasher, value),
560                None => Self::hash_str(&mut hasher, "null"),
561            }
562        }
563
564        hasher.finish() & HASH_SAFE_MASK
565    }
566
567    /// Compute hash for morphologies.
568    fn compute_morphologies_hash(&self) -> u64 {
569        let mut hasher = Xxh64::new(DATA_HASH_SEED);
570        let mut morphology_ids = self.morphology_registry.morphology_ids();
571        morphology_ids.sort();
572
573        for morphology_id in morphology_ids {
574            if let Some(morphology) = self.morphology_registry.get(&morphology_id) {
575                Self::hash_str(&mut hasher, &morphology_id);
576                Self::hash_str(&mut hasher, &format!("{:?}", morphology.morphology_type));
577                Self::hash_str(&mut hasher, &morphology.class);
578                if let Ok(value) = serde_json::to_value(&morphology.parameters) {
579                    Self::hash_json_value(&mut hasher, &value);
580                }
581            }
582        }
583
584        hasher.finish() & HASH_SAFE_MASK
585    }
586
587    /// Compute hash for cortical mappings (cortical_mapping_dst).
588    fn compute_cortical_mappings_hash(&self) -> u64 {
589        let mut hasher = Xxh64::new(DATA_HASH_SEED);
590        let mut areas: Vec<&CorticalArea> = self.cortical_areas.values().collect();
591        areas.sort_by_key(|area| area.cortical_id.as_base_64());
592
593        for area in areas {
594            let cortical_id = area.cortical_id.as_base_64();
595            Self::hash_str(&mut hasher, &cortical_id);
596            if let Some(serde_json::Value::Object(map)) =
597                area.properties.get("cortical_mapping_dst")
598            {
599                let mut dest_ids: Vec<&String> = map.keys().collect();
600                dest_ids.sort();
601                for dest_id in dest_ids {
602                    Self::hash_str(&mut hasher, dest_id);
603                    if let Some(value) = map.get(dest_id) {
604                        Self::hash_json_value(&mut hasher, value);
605                    }
606                }
607            } else {
608                Self::hash_str(&mut hasher, "null");
609            }
610        }
611
612        hasher.finish() & HASH_SAFE_MASK
613    }
614
615    /// Hash a string with a separator to avoid concatenation collisions.
616    fn hash_str(hasher: &mut Xxh64, value: &str) {
617        hasher.write(value.as_bytes());
618        hasher.write_u8(0);
619    }
620
621    /// Hash a signed 32-bit integer deterministically.
622    fn hash_i32(hasher: &mut Xxh64, value: i32) {
623        hasher.write(&value.to_le_bytes());
624    }
625
626    /// Hash an unsigned 32-bit integer deterministically.
627    fn hash_u32(hasher: &mut Xxh64, value: u32) {
628        hasher.write(&value.to_le_bytes());
629    }
630
631    /// Hash JSON values deterministically with sorted object keys.
632    fn hash_json_value(hasher: &mut Xxh64, value: &serde_json::Value) {
633        match value {
634            serde_json::Value::Null => {
635                hasher.write_u8(0);
636            }
637            serde_json::Value::Bool(val) => {
638                hasher.write_u8(1);
639                hasher.write_u8(*val as u8);
640            }
641            serde_json::Value::Number(num) => {
642                hasher.write_u8(2);
643                Self::hash_str(hasher, &num.to_string());
644            }
645            serde_json::Value::String(val) => {
646                hasher.write_u8(3);
647                Self::hash_str(hasher, val);
648            }
649            serde_json::Value::Array(items) => {
650                hasher.write_u8(4);
651                for item in items {
652                    Self::hash_json_value(hasher, item);
653                }
654            }
655            serde_json::Value::Object(map) => {
656                hasher.write_u8(5);
657                let mut keys: Vec<&String> = map.keys().collect();
658                keys.sort();
659                for key in keys {
660                    Self::hash_str(hasher, key);
661                    if let Some(val) = map.get(key) {
662                        Self::hash_json_value(hasher, val);
663                    }
664                }
665            }
666        }
667    }
668
669    /// Hash JSON properties deterministically, excluding specific keys.
670    fn hash_properties_filtered(
671        hasher: &mut Xxh64,
672        properties: &HashMap<String, serde_json::Value>,
673        excluded_keys: &[&str],
674    ) {
675        let mut keys: Vec<&String> = properties.keys().collect();
676        keys.sort();
677        for key in keys {
678            if excluded_keys.contains(&key.as_str()) {
679                continue;
680            }
681            Self::hash_str(hasher, key);
682            if let Some(value) = properties.get(key) {
683                Self::hash_json_value(hasher, value);
684            }
685        }
686    }
687
688    // ======================================================================
689    // Cortical Area Management
690    // ======================================================================
691
692    /// Add a new cortical area
693    ///
694    /// # Arguments
695    ///
696    /// * `area` - The cortical area to add
697    ///
698    /// # Returns
699    ///
700    /// The assigned cortical index
701    ///
702    /// # Errors
703    ///
704    /// Returns error if:
705    /// - An area with the same cortical_id already exists
706    /// - The area's cortical_idx conflicts with an existing area
707    ///
708    pub fn add_cortical_area(&mut self, mut area: CorticalArea) -> BduResult<u32> {
709        // Check if area already exists
710        if self.cortical_areas.contains_key(&area.cortical_id) {
711            return Err(BduError::InvalidArea(format!(
712                "Cortical area {} already exists",
713                area.cortical_id
714            )));
715        }
716
717        // CRITICAL: Reserve cortical_idx 0..=6 for invariant core areas.
718        // Use feagi-data-processing types as single source of truth
719        use feagi_structures::genomic::cortical_area::CoreCorticalType;
720
721        let death_id = CoreCorticalType::Death.to_cortical_id();
722        let power_id = CoreCorticalType::Power.to_cortical_id();
723        let fatigue_id = CoreCorticalType::Fatigue.to_cortical_id();
724        let pain_id = CoreCorticalType::Pain.to_cortical_id();
725        let pleasure_id = CoreCorticalType::Pleasure.to_cortical_id();
726        let fear_id = CoreCorticalType::Fear.to_cortical_id();
727        let hope_id = CoreCorticalType::Hope.to_cortical_id();
728
729        let is_death_area = area.cortical_id == death_id;
730        let is_power_area = area.cortical_id == power_id;
731        let is_fatigue_area = area.cortical_id == fatigue_id;
732        let is_pain_area = area.cortical_id == pain_id;
733        let is_pleasure_area = area.cortical_id == pleasure_id;
734        let is_fear_area = area.cortical_id == fear_id;
735        let is_hope_area = area.cortical_id == hope_id;
736
737        if is_death_area {
738            trace!(
739                target: "feagi-bdu",
740                "[CORE-AREA] Assigning RESERVED cortical_idx=0 to _death area (id={})",
741                area.cortical_id
742            );
743            area.cortical_idx = 0;
744        } else if is_power_area {
745            trace!(
746                target: "feagi-bdu",
747                "[CORE-AREA] Assigning RESERVED cortical_idx=1 to _power area (id={})",
748                area.cortical_id
749            );
750            area.cortical_idx = 1;
751        } else if is_fatigue_area {
752            trace!(
753                target: "feagi-bdu",
754                "[CORE-AREA] Assigning RESERVED cortical_idx=2 to _fatigue area (id={})",
755                area.cortical_id
756            );
757            area.cortical_idx = 2;
758        } else if is_pain_area {
759            trace!(
760                target: "feagi-bdu",
761                "[CORE-AREA] Assigning RESERVED cortical_idx=3 to _pain area (id={})",
762                area.cortical_id
763            );
764            area.cortical_idx = 3;
765        } else if is_pleasure_area {
766            trace!(
767                target: "feagi-bdu",
768                "[CORE-AREA] Assigning RESERVED cortical_idx=4 to _pleasure area (id={})",
769                area.cortical_id
770            );
771            area.cortical_idx = 4;
772        } else if is_fear_area {
773            trace!(
774                target: "feagi-bdu",
775                "[CORE-AREA] Assigning RESERVED cortical_idx=5 to _fear area (id={})",
776                area.cortical_id
777            );
778            area.cortical_idx = 5;
779        } else if is_hope_area {
780            trace!(
781                target: "feagi-bdu",
782                "[CORE-AREA] Assigning RESERVED cortical_idx=6 to _hope area (id={})",
783                area.cortical_id
784            );
785            area.cortical_idx = 6;
786        } else {
787            // Regular areas: assign cortical_idx if not set (will be >=7 due to reservation)
788            if area.cortical_idx == 0 {
789                area.cortical_idx = self.next_cortical_idx;
790                self.next_cortical_idx += 1;
791                trace!(
792                    target: "feagi-bdu",
793                    "[REGULAR-AREA] Assigned cortical_idx={} to area '{}' (should be >=7)",
794                    area.cortical_idx,
795                    area.cortical_id.as_base_64()
796                );
797            } else {
798                // Check for reserved index collision
799                if area.cortical_idx <= 6 {
800                    warn!(
801                        "Regular area '{}' attempted to use RESERVED cortical_idx={}! Reassigning to next available.",
802                        area.cortical_id, area.cortical_idx);
803                    area.cortical_idx = self.next_cortical_idx;
804                    self.next_cortical_idx += 1;
805                    info!(
806                        "   Reassigned '{}' to cortical_idx={}",
807                        area.cortical_id, area.cortical_idx
808                    );
809                } else if self.cortical_idx_to_id.contains_key(&area.cortical_idx) {
810                    return Err(BduError::InvalidArea(format!(
811                        "Cortical index {} is already in use",
812                        area.cortical_idx
813                    )));
814                }
815
816                // Update next_cortical_idx if needed
817                if area.cortical_idx >= self.next_cortical_idx {
818                    self.next_cortical_idx = area.cortical_idx + 1;
819                }
820            }
821        }
822
823        let cortical_id = area.cortical_id;
824        let cortical_idx = area.cortical_idx;
825
826        // Update lookup maps
827        self.cortical_id_to_idx.insert(cortical_id, cortical_idx);
828        self.cortical_idx_to_id.insert(cortical_idx, cortical_id);
829
830        // Initialize upstream_cortical_areas property (empty array for O(1) lookup)
831        area.properties
832            .insert("upstream_cortical_areas".to_string(), serde_json::json!([]));
833
834        // Default visualization voxel granularity is 1x1x1 (assumed, not stored)
835        // User overrides are stored in properties["visualization_voxel_granularity"] only if != 1x1x1
836
837        // If the caller provided a parent brain region ID, persist the association in the
838        // BrainRegionHierarchy membership set (this drives /v1/region/regions_members).
839        //
840        // IMPORTANT: This is separate from storing "parent_region_id" in the cortical area's
841        // properties. BV may show that property even if the hierarchy isn't updated.
842        let parent_region_id = area
843            .properties
844            .get("parent_region_id")
845            .and_then(|v| v.as_str())
846            .map(|s| s.to_string());
847
848        // Store area
849        self.cortical_areas.insert(cortical_id, area);
850
851        // Update region membership (source of truth for region->areas listing)
852        if let Some(region_id) = parent_region_id {
853            let region = self
854                .brain_regions
855                .get_region_mut(&region_id)
856                .ok_or_else(|| {
857                    BduError::InvalidArea(format!(
858                        "Unknown parent_region_id '{}' for cortical area {}",
859                        region_id,
860                        cortical_id.as_base_64()
861                    ))
862                })?;
863            region.add_area(cortical_id);
864        }
865
866        // CRITICAL: Initialize per-area count caches to 0 (lock-free for readers)
867        // This allows healthcheck endpoints to read counts without NPU lock
868        {
869            let mut neuron_cache = self.cached_neuron_counts_per_area.write();
870            neuron_cache.insert(cortical_id, AtomicUsize::new(0));
871            let mut synapse_cache = self.cached_synapse_counts_per_area.write();
872            synapse_cache.insert(cortical_id, AtomicUsize::new(0));
873        }
874        // @cursor:critical-path - BV pulls per-area stats from StateManager without NPU lock.
875        let state_manager = StateManager::instance();
876        let state_manager = state_manager.read();
877        state_manager.init_cortical_area_stats(&cortical_id.as_base_64());
878
879        // CRITICAL: Register cortical area in NPU during corticogenesis
880        // This must happen BEFORE neurogenesis so neurons can look up their cortical IDs
881        // Use base64 format for proper CorticalID conversion
882        if let Some(ref npu) = self.npu {
883            trace!(target: "feagi-bdu", "[LOCK-TRACE] add_cortical_area: attempting NPU lock for registration");
884            if let Ok(mut npu_lock) = npu.lock() {
885                trace!(target: "feagi-bdu", "[LOCK-TRACE] add_cortical_area: acquired NPU lock for registration");
886                npu_lock.register_cortical_area(cortical_idx, cortical_id.as_base_64());
887                trace!(
888                    target: "feagi-bdu",
889                    "Registered cortical area idx={} -> '{}' in NPU",
890                    cortical_idx,
891                    cortical_id.as_base_64()
892                );
893            }
894        }
895
896        // Synchronize cortical area flags with NPU (psp_uniform_distribution, mp_driven_psp, etc.)
897        self.sync_cortical_area_flags_to_npu()?;
898
899        self.initialized = true;
900
901        self.refresh_cortical_area_hashes(true, true);
902        self.refresh_brain_regions_hash();
903
904        Ok(cortical_idx)
905    }
906
907    /// Remove a cortical area by ID
908    ///
909    /// # Arguments
910    ///
911    /// * `cortical_id` - ID of the cortical area to remove
912    ///
913    /// # Returns
914    ///
915    /// `Ok(())` if removed, error if area doesn't exist
916    ///
917    /// # Note
918    ///
919    /// This does NOT remove neurons from the NPU - that must be done separately.
920    ///
921    pub fn remove_cortical_area(&mut self, cortical_id: &CorticalID) -> BduResult<()> {
922        let area = self.cortical_areas.remove(cortical_id).ok_or_else(|| {
923            BduError::InvalidArea(format!("Cortical area {} does not exist", cortical_id))
924        })?;
925
926        // Remove from lookup maps
927        self.cortical_id_to_idx.remove(cortical_id);
928        self.cortical_idx_to_id.remove(&area.cortical_idx);
929
930        self.refresh_cortical_area_hashes(true, true);
931        Ok(())
932    }
933
934    /// Update a cortical area ID without changing its cortical_idx.
935    ///
936    /// This remaps internal lookup tables, brain-region membership, and mapping keys.
937    pub fn rename_cortical_area_id(
938        &mut self,
939        old_id: &CorticalID,
940        new_id: CorticalID,
941        new_cortical_type: CorticalAreaType,
942    ) -> BduResult<()> {
943        self.rename_cortical_area_id_with_options(old_id, new_id, new_cortical_type, true)
944    }
945
946    /// Update a cortical area ID without changing its cortical_idx, with optional NPU registry update.
947    pub fn rename_cortical_area_id_with_options(
948        &mut self,
949        old_id: &CorticalID,
950        new_id: CorticalID,
951        new_cortical_type: CorticalAreaType,
952        update_npu_registry: bool,
953    ) -> BduResult<()> {
954        if !self.cortical_areas.contains_key(old_id) {
955            return Err(BduError::InvalidArea(format!(
956                "Cortical area {} does not exist",
957                old_id
958            )));
959        }
960        if self.cortical_areas.contains_key(&new_id) {
961            return Err(BduError::InvalidArea(format!(
962                "Cortical area {} already exists",
963                new_id
964            )));
965        }
966
967        let mut area = self.cortical_areas.remove(old_id).ok_or_else(|| {
968            BduError::InvalidArea(format!("Cortical area {} does not exist", old_id))
969        })?;
970        let cortical_idx = area.cortical_idx;
971        area.cortical_id = new_id;
972        area.cortical_type = new_cortical_type;
973
974        self.cortical_areas.insert(new_id, area);
975        self.cortical_id_to_idx.remove(old_id);
976        self.cortical_id_to_idx.insert(new_id, cortical_idx);
977        self.cortical_idx_to_id.insert(cortical_idx, new_id);
978
979        // Update per-area count caches
980        {
981            let mut neuron_cache = self.cached_neuron_counts_per_area.write();
982            if let Some(value) = neuron_cache.remove(old_id) {
983                neuron_cache.insert(new_id, value);
984            }
985            let mut synapse_cache = self.cached_synapse_counts_per_area.write();
986            if let Some(value) = synapse_cache.remove(old_id) {
987                synapse_cache.insert(new_id, value);
988            }
989        }
990
991        // Update brain-region membership
992        self.brain_regions.rename_cortical_area_id(old_id, new_id);
993
994        // Update cortical mapping properties referencing the old ID
995        let old_id_str = old_id.as_base_64();
996        let new_id_str = new_id.as_base_64();
997        for area in self.cortical_areas.values_mut() {
998            if let Some(mapping) = area
999                .properties
1000                .get_mut("cortical_mapping_dst")
1001                .and_then(|v| v.as_object_mut())
1002            {
1003                if let Some(value) = mapping.remove(&old_id_str) {
1004                    mapping.insert(new_id_str.clone(), value);
1005                }
1006            }
1007        }
1008
1009        // Update NPU cortical_id registry if requested
1010        if update_npu_registry {
1011            if let Some(ref npu) = self.npu {
1012                if let Ok(mut npu_lock) = npu.lock() {
1013                    npu_lock.register_cortical_area(cortical_idx, new_id.as_base_64());
1014                }
1015            }
1016        }
1017
1018        self.refresh_cortical_area_hashes(true, true);
1019        self.refresh_brain_regions_hash();
1020        self.refresh_cortical_mappings_hash();
1021
1022        Ok(())
1023    }
1024
1025    /// Get a cortical area by ID
1026    pub fn get_cortical_area(&self, cortical_id: &CorticalID) -> Option<&CorticalArea> {
1027        self.cortical_areas.get(cortical_id)
1028    }
1029
1030    /// Get a mutable reference to a cortical area
1031    pub fn get_cortical_area_mut(&mut self, cortical_id: &CorticalID) -> Option<&mut CorticalArea> {
1032        self.cortical_areas.get_mut(cortical_id)
1033    }
1034
1035    /// Get cortical index by ID
1036    pub fn get_cortical_idx(&self, cortical_id: &CorticalID) -> Option<u32> {
1037        self.cortical_id_to_idx.get(cortical_id).copied()
1038    }
1039
1040    /// Find which brain region contains a cortical area
1041    ///
1042    /// This is used to populate `parent_region_id` in API responses for Brain Visualizer.
1043    /// Delegates to BrainRegionHierarchy for the actual search.
1044    ///
1045    /// # Arguments
1046    /// * `cortical_id` - Cortical area to search for
1047    ///
1048    /// # Returns
1049    /// * `Option<String>` - Parent region ID (UUID string) if found
1050    ///
1051    pub fn get_parent_region_id_for_area(&self, cortical_id: &CorticalID) -> Option<String> {
1052        self.brain_regions.find_region_containing_area(cortical_id)
1053    }
1054
1055    /// True if `area` has at least one efferent mapping to a cortical area outside its brain region
1056    /// (including destinations not assigned to any region).
1057    pub fn has_cross_region_outgoing(&self, area: &CorticalID) -> bool {
1058        let Some(my_region) = self.brain_regions.find_region_containing_area(area) else {
1059            return false;
1060        };
1061        let Some(src_area) = self.cortical_areas.get(area) else {
1062            return false;
1063        };
1064        let Some(dst_obj) = src_area
1065            .properties
1066            .get("cortical_mapping_dst")
1067            .and_then(|v| v.as_object())
1068        else {
1069            return false;
1070        };
1071        for dst_key in dst_obj.keys() {
1072            let Ok(dst_id) = CorticalID::try_from_base_64(dst_key) else {
1073                continue;
1074            };
1075            match self.brain_regions.find_region_containing_area(&dst_id) {
1076                None => return true,
1077                Some(rid) if rid != my_region => return true,
1078                _ => {}
1079            }
1080        }
1081        false
1082    }
1083
1084    /// True if `area` has at least one afferent mapping from a cortical area outside its brain region.
1085    pub fn has_cross_region_incoming(&self, area: &CorticalID) -> bool {
1086        let Some(my_region) = self.brain_regions.find_region_containing_area(area) else {
1087            return false;
1088        };
1089        let my_b64 = area.as_base_64();
1090        for (src_id, src_area) in &self.cortical_areas {
1091            if src_id == area {
1092                continue;
1093            }
1094            let Some(dst_map) = src_area
1095                .properties
1096                .get("cortical_mapping_dst")
1097                .and_then(|v| v.as_object())
1098            else {
1099                continue;
1100            };
1101            if !dst_map.contains_key(&my_b64) {
1102                continue;
1103            }
1104            match self.brain_regions.find_region_containing_area(src_id) {
1105                None => return true,
1106                Some(rid) if rid != my_region => return true,
1107                _ => {}
1108            }
1109        }
1110        false
1111    }
1112
1113    /// Recompute brain-region `inputs`/`outputs` registries from current cortical mappings.
1114    ///
1115    /// This drives `/v1/region/regions_members` (via `BrainRegion.properties["inputs"/"outputs"]`).
1116    ///
1117    /// Semantics (matches Python `_auto_assign_region_io()` and BV expectations):
1118    /// - **outputs**: Any cortical area *in the region* that connects to an area outside the region
1119    /// - **inputs**: Any cortical area *in the region* that receives a connection from outside the region
1120    ///
1121    /// This function updates the hierarchy in-place and returns the computed base64 ID lists
1122    /// for downstream persistence into `RuntimeGenome`.
1123    pub fn recompute_brain_region_io_registry(&mut self) -> BduResult<BrainRegionIoRegistry> {
1124        use std::collections::HashSet;
1125
1126        let region_ids: Vec<String> = self
1127            .brain_regions
1128            .get_all_region_ids()
1129            .into_iter()
1130            .cloned()
1131            .collect();
1132
1133        let mut inputs_by_region: HashMap<String, HashSet<String>> = HashMap::new();
1134        let mut outputs_by_region: HashMap<String, HashSet<String>> = HashMap::new();
1135
1136        // Initialize so regions with no IO still get cleared deterministically.
1137        for rid in &region_ids {
1138            inputs_by_region.insert(rid.clone(), HashSet::new());
1139            outputs_by_region.insert(rid.clone(), HashSet::new());
1140        }
1141
1142        for (src_id, src_area) in &self.cortical_areas {
1143            let Some(dstmap) = src_area
1144                .properties
1145                .get("cortical_mapping_dst")
1146                .and_then(|v| v.as_object())
1147            else {
1148                continue;
1149            };
1150
1151            let Some(src_region_id) = self.brain_regions.find_region_containing_area(src_id) else {
1152                debug!(
1153                    target: "feagi-bdu",
1154                    "Skipping region IO for source area {} (not in any region)",
1155                    src_id.as_base_64()
1156                );
1157                continue;
1158            };
1159
1160            for dst_id_str in dstmap.keys() {
1161                let dst_id = CorticalID::try_from_base_64(dst_id_str).map_err(|e| {
1162                    BduError::InvalidArea(format!(
1163                        "Unable to recompute region IO: invalid destination cortical id '{}' in cortical_mapping_dst for {}: {}",
1164                        dst_id_str,
1165                        src_id.as_base_64(),
1166                        e
1167                    ))
1168                })?;
1169
1170                let Some(dst_region_id) = self.brain_regions.find_region_containing_area(&dst_id)
1171                else {
1172                    warn!(
1173                        target: "feagi-bdu",
1174                        "Skipping region IO for destination area {} (not in any region)",
1175                        dst_id.as_base_64()
1176                    );
1177                    continue;
1178                };
1179
1180                if src_region_id == dst_region_id {
1181                    continue;
1182                }
1183
1184                outputs_by_region
1185                    .entry(src_region_id.clone())
1186                    .or_default()
1187                    .insert(src_id.as_base_64());
1188                inputs_by_region
1189                    .entry(dst_region_id.clone())
1190                    .or_default()
1191                    .insert(dst_id.as_base_64());
1192            }
1193        }
1194
1195        // Union in declared interface lists so export/import and pre-wired regions show IO without edges.
1196        for rid in &region_ids {
1197            let Some(region) = self.brain_regions.get_region(rid) else {
1198                continue;
1199            };
1200            let in_ids = crate::region_io_designation::parse_designated_id_list(
1201                region
1202                    .properties
1203                    .get(crate::region_io_designation::DESIGNATED_INPUTS_KEY),
1204            )?;
1205            let out_ids = crate::region_io_designation::parse_designated_id_list(
1206                region
1207                    .properties
1208                    .get(crate::region_io_designation::DESIGNATED_OUTPUTS_KEY),
1209            )?;
1210            for id in in_ids {
1211                inputs_by_region
1212                    .entry(rid.clone())
1213                    .or_default()
1214                    .insert(id.as_base_64());
1215            }
1216            for id in out_ids {
1217                outputs_by_region
1218                    .entry(rid.clone())
1219                    .or_default()
1220                    .insert(id.as_base_64());
1221            }
1222        }
1223
1224        let mut computed: HashMap<String, (Vec<String>, Vec<String>)> = HashMap::new();
1225        for rid in region_ids {
1226            let mut inputs: Vec<String> = inputs_by_region
1227                .remove(&rid)
1228                .unwrap_or_default()
1229                .into_iter()
1230                .collect();
1231            let mut outputs: Vec<String> = outputs_by_region
1232                .remove(&rid)
1233                .unwrap_or_default()
1234                .into_iter()
1235                .collect();
1236
1237            inputs.sort();
1238            outputs.sort();
1239
1240            let region = self.brain_regions.get_region_mut(&rid).ok_or_else(|| {
1241                BduError::InvalidArea(format!(
1242                    "Unable to recompute region IO: region '{}' not found in hierarchy",
1243                    rid
1244                ))
1245            })?;
1246
1247            if inputs.is_empty() {
1248                region.properties.remove("inputs");
1249            } else {
1250                region
1251                    .properties
1252                    .insert("inputs".to_string(), serde_json::json!(inputs.clone()));
1253            }
1254
1255            if outputs.is_empty() {
1256                region.properties.remove("outputs");
1257            } else {
1258                region
1259                    .properties
1260                    .insert("outputs".to_string(), serde_json::json!(outputs.clone()));
1261            }
1262
1263            computed.insert(rid, (inputs, outputs));
1264        }
1265
1266        self.refresh_brain_regions_hash();
1267
1268        Ok(computed)
1269    }
1270
1271    /// Get the root brain region ID (region with no parent)
1272    ///
1273    /// # Returns
1274    /// * `Option<String>` - Root region ID (UUID string) if found
1275    ///
1276    pub fn get_root_region_id(&self) -> Option<String> {
1277        self.brain_regions.get_root_region_id()
1278    }
1279
1280    /// Get cortical ID by index
1281    pub fn get_cortical_id(&self, cortical_idx: u32) -> Option<&CorticalID> {
1282        self.cortical_idx_to_id.get(&cortical_idx)
1283    }
1284
1285    /// Get all cortical_idx -> cortical_id mappings (for burst loop caching)
1286    /// Returns a HashMap of cortical_idx -> cortical_id (base64 string)
1287    pub fn get_all_cortical_idx_to_id_mappings(&self) -> ahash::AHashMap<u32, String> {
1288        self.cortical_idx_to_id
1289            .iter()
1290            .map(|(idx, id)| (*idx, id.as_base_64()))
1291            .collect()
1292    }
1293
1294    /// Get all cortical_idx -> visualization_voxel_granularity mappings
1295    ///
1296    /// Returns a map of cortical_idx to (granularity_x, granularity_y, granularity_z) for areas that have
1297    /// visualization voxel granularity configured.
1298    pub fn get_all_visualization_granularities(&self) -> ahash::AHashMap<u32, (u32, u32, u32)> {
1299        let mut granularities = ahash::AHashMap::new();
1300        for (cortical_id, area) in &self.cortical_areas {
1301            let cortical_idx = self
1302                .cortical_id_to_idx
1303                .get(cortical_id)
1304                .copied()
1305                .unwrap_or(0);
1306
1307            // Extract visualization granularity overrides from properties.
1308            // Default is 1x1x1 (assumed, not stored) so we only include non-default overrides.
1309            if let Some(granularity_json) = area.properties.get("visualization_voxel_granularity") {
1310                if let Some(arr) = granularity_json.as_array() {
1311                    if arr.len() == 3 {
1312                        let x_opt = arr[0]
1313                            .as_u64()
1314                            .or_else(|| arr[0].as_f64().map(|f| f as u64));
1315                        let y_opt = arr[1]
1316                            .as_u64()
1317                            .or_else(|| arr[1].as_f64().map(|f| f as u64));
1318                        let z_opt = arr[2]
1319                            .as_u64()
1320                            .or_else(|| arr[2].as_f64().map(|f| f as u64));
1321
1322                        if let (Some(x), Some(y), Some(z)) = (x_opt, y_opt, z_opt) {
1323                            let granularity = (x as u32, y as u32, z as u32);
1324                            // Only include overrides (non-default)
1325                            if granularity != (1, 1, 1) {
1326                                granularities.insert(cortical_idx, granularity);
1327                            }
1328                        }
1329                    }
1330                }
1331            }
1332        }
1333        granularities
1334    }
1335
1336    /// Get all cortical area IDs
1337    pub fn get_cortical_area_ids(&self) -> Vec<&CorticalID> {
1338        self.cortical_areas.keys().collect()
1339    }
1340
1341    /// Get the number of cortical areas
1342    pub fn get_cortical_area_count(&self) -> usize {
1343        self.cortical_areas.len()
1344    }
1345
1346    /// Get all cortical areas that have synapses targeting the specified area (upstream/afferent areas)
1347    ///
1348    /// Reads from the `upstream_cortical_areas` property stored on the cortical area.
1349    /// This property is maintained by `add_upstream_area()` and `remove_upstream_area()`.
1350    ///
1351    /// # Arguments
1352    ///
1353    /// * `target_cortical_id` - The cortical area ID to find upstream connections for
1354    ///
1355    /// # Returns
1356    ///
1357    /// Vec of cortical_idx values for all upstream areas
1358    ///
1359    pub fn get_upstream_cortical_areas(&self, target_cortical_id: &CorticalID) -> Vec<u32> {
1360        if let Some(area) = self.cortical_areas.get(target_cortical_id) {
1361            if let Some(upstream_prop) = area.properties.get("upstream_cortical_areas") {
1362                if let Some(upstream_array) = upstream_prop.as_array() {
1363                    return upstream_array
1364                        .iter()
1365                        .filter_map(|v| v.as_u64().map(|n| n as u32))
1366                        .collect();
1367                }
1368            }
1369
1370            // Property missing - data integrity issue
1371            warn!(target: "feagi-bdu",
1372                "Cortical area '{}' missing 'upstream_cortical_areas' property - treating as empty",
1373                target_cortical_id.as_base_64()
1374            );
1375        }
1376
1377        Vec::new()
1378    }
1379
1380    /// Get only episodic-memory upstream cortical indices for a target memory area.
1381    ///
1382    /// This filters upstream sources to mappings that explicitly include an
1383    /// `episodic_memory` morphology rule; associative-only upstream links are excluded.
1384    pub fn get_episodic_memory_upstream_cortical_areas(
1385        &self,
1386        target_cortical_id: &CorticalID,
1387    ) -> Vec<u32> {
1388        let mut episodic_upstream = Vec::new();
1389        let upstream = self.get_upstream_cortical_areas(target_cortical_id);
1390
1391        for src_idx in upstream {
1392            let Some(src_id) = self.cortical_idx_to_id.get(&src_idx) else {
1393                continue;
1394            };
1395            let Some(src_area) = self.cortical_areas.get(src_id) else {
1396                continue;
1397            };
1398            let Some(mapping_dst) = src_area
1399                .properties
1400                .get("cortical_mapping_dst")
1401                .and_then(|v| v.as_object())
1402            else {
1403                continue;
1404            };
1405            let Some(rules) =
1406                Self::get_mapping_rules_for_destination(mapping_dst, target_cortical_id)
1407            else {
1408                continue;
1409            };
1410
1411            let has_episodic_rule = rules.iter().any(|rule| {
1412                let morphology_id = rule
1413                    .as_object()
1414                    .and_then(|obj| obj.get("morphology_id"))
1415                    .and_then(|v| v.as_str())
1416                    .or_else(|| {
1417                        rule.as_array()
1418                            .and_then(|arr| arr.first())
1419                            .and_then(|v| v.as_str())
1420                    });
1421                morphology_id == Some("episodic_memory")
1422            });
1423
1424            if has_episodic_rule {
1425                episodic_upstream.push(src_idx);
1426            }
1427        }
1428
1429        episodic_upstream.sort_unstable();
1430        episodic_upstream.dedup();
1431        episodic_upstream
1432    }
1433
1434    /// Filter upstream cortical indices to exclude memory areas.
1435    pub fn filter_non_memory_upstream_areas(&self, upstream: &[u32]) -> Vec<u32> {
1436        upstream
1437            .iter()
1438            .filter_map(|idx| {
1439                let cortical_id = self.cortical_idx_to_id.get(idx)?;
1440                let area = self.cortical_areas.get(cortical_id)?;
1441                if matches!(area.cortical_type, CorticalAreaType::Memory(_)) {
1442                    None
1443                } else {
1444                    Some(*idx)
1445                }
1446            })
1447            .collect()
1448    }
1449
1450    /// Recompute and persist upstream_cortical_areas for a target area from mapping properties.
1451    ///
1452    /// This is a recovery path for stale upstream tracking after connectome or mapping edits.
1453    pub fn refresh_upstream_cortical_areas_from_mappings(
1454        &mut self,
1455        target_cortical_id: &CorticalID,
1456    ) -> Vec<u32> {
1457        use std::collections::HashSet;
1458        let target_id_str = target_cortical_id.as_base_64();
1459        let mut upstream_idxs = HashSet::new();
1460        for (src_id, src_area) in &self.cortical_areas {
1461            if src_id == target_cortical_id {
1462                continue;
1463            }
1464            if let Some(mapping) = src_area
1465                .properties
1466                .get("cortical_mapping_dst")
1467                .and_then(|v| v.as_object())
1468            {
1469                if mapping.contains_key(&target_id_str) {
1470                    upstream_idxs.insert(src_area.cortical_idx);
1471                }
1472            }
1473        }
1474
1475        let mut upstream_list: Vec<u32> = upstream_idxs.into_iter().collect();
1476        upstream_list.sort_unstable();
1477
1478        if let Some(target_area) = self.cortical_areas.get_mut(target_cortical_id) {
1479            target_area.properties.insert(
1480                "upstream_cortical_areas".to_string(),
1481                serde_json::json!(upstream_list),
1482            );
1483        }
1484
1485        self.get_upstream_cortical_areas(target_cortical_id)
1486    }
1487
1488    /// Add an upstream cortical area to a target area's upstream list
1489    ///
1490    /// This should be called when synapses are created from src_cortical_idx to target_cortical_id.
1491    ///
1492    /// # Arguments
1493    ///
1494    /// * `target_cortical_id` - The cortical area receiving connections
1495    /// * `src_cortical_idx` - The cortical index of the source area
1496    ///
1497    pub fn add_upstream_area(&mut self, target_cortical_id: &CorticalID, src_cortical_idx: u32) {
1498        if let Some(area) = self.cortical_areas.get_mut(target_cortical_id) {
1499            let upstream_array = area
1500                .properties
1501                .entry("upstream_cortical_areas".to_string())
1502                .or_insert_with(|| serde_json::json!([]));
1503
1504            if let Some(arr) = upstream_array.as_array_mut() {
1505                let src_value = serde_json::json!(src_cortical_idx);
1506                if !arr.contains(&src_value) {
1507                    arr.push(src_value);
1508                    debug!(target: "feagi-bdu",
1509                        "Added upstream area idx={} to cortical area '{}'",
1510                        src_cortical_idx, target_cortical_id.as_base_64()
1511                    );
1512                }
1513            }
1514        }
1515    }
1516
1517    /// Get the memory twin cortical ID for a given memory area and upstream area.
1518    pub fn get_memory_twin_for_upstream_idx(
1519        &self,
1520        memory_area_idx: u32,
1521        upstream_idx: u32,
1522    ) -> Option<CorticalID> {
1523        let memory_id = self.cortical_idx_to_id.get(&memory_area_idx)?;
1524        let upstream_id = self.cortical_idx_to_id.get(&upstream_idx)?;
1525        let area = self.cortical_areas.get(memory_id)?;
1526        let mapping = area
1527            .properties
1528            .get("memory_twin_areas")
1529            .and_then(|v| v.as_object())?;
1530        let twin_b64 = mapping.get(&upstream_id.as_base_64())?.as_str()?;
1531        CorticalID::try_from_base_64(twin_b64).ok()
1532    }
1533
1534    /// Diagnose memory-twin eligibility and status for a specific mapping pair.
1535    ///
1536    /// This gives API/MCP a one-call answer to:
1537    /// - whether a twin is expected,
1538    /// - whether one exists,
1539    /// - and the precise blocker when it does not.
1540    pub fn diagnose_memory_twin_for_mapping(
1541        &self,
1542        src_area_id: &CorticalID,
1543        dst_area_id: &CorticalID,
1544    ) -> BduResult<MemoryTwinDiagnostic> {
1545        let src_area = self.cortical_areas.get(src_area_id).ok_or_else(|| {
1546            BduError::InvalidArea(format!(
1547                "Source area not found: {}",
1548                src_area_id.as_base_64()
1549            ))
1550        })?;
1551        let dst_area = self.cortical_areas.get(dst_area_id).ok_or_else(|| {
1552            BduError::InvalidArea(format!(
1553                "Destination area not found: {}",
1554                dst_area_id.as_base_64()
1555            ))
1556        })?;
1557
1558        let src_is_memory = matches!(src_area.cortical_type, CorticalAreaType::Memory(_));
1559        let dst_is_memory = matches!(dst_area.cortical_type, CorticalAreaType::Memory(_));
1560
1561        let rules_opt = src_area
1562            .properties
1563            .get("cortical_mapping_dst")
1564            .and_then(|v| v.as_object())
1565            .and_then(|mapping_dst| {
1566                Self::get_mapping_rules_for_destination(mapping_dst, dst_area_id)
1567            });
1568        let mapping_exists = rules_opt.is_some();
1569        let episodic_rule_count = rules_opt
1570            .map(|rules| {
1571                rules
1572                    .iter()
1573                    .filter(|rule| {
1574                        let morphology_id = rule
1575                            .as_object()
1576                            .and_then(|obj| obj.get("morphology_id"))
1577                            .and_then(|v| v.as_str())
1578                            .or_else(|| {
1579                                rule.as_array()
1580                                    .and_then(|arr| arr.first())
1581                                    .and_then(|v| v.as_str())
1582                            });
1583                        morphology_id == Some("episodic_memory")
1584                    })
1585                    .count()
1586            })
1587            .unwrap_or(0);
1588
1589        let twin_expected =
1590            mapping_exists && episodic_rule_count > 0 && dst_is_memory && !src_is_memory;
1591        let twin_cortical_area = dst_area
1592            .properties
1593            .get("memory_twin_areas")
1594            .and_then(|v| v.as_object())
1595            .and_then(|map| map.get(&src_area_id.as_base_64()))
1596            .and_then(|v| v.as_str())
1597            .map(ToString::to_string);
1598        let twin_present = twin_cortical_area.is_some();
1599
1600        let twin_parent_region_id = twin_cortical_area.as_ref().and_then(|twin_b64| {
1601            CorticalID::try_from_base_64(twin_b64)
1602                .ok()
1603                .and_then(|twin_id| {
1604                    self.cortical_areas.get(&twin_id).and_then(|area| {
1605                        area.properties
1606                            .get("parent_region_id")
1607                            .and_then(|v| v.as_str())
1608                            .map(ToString::to_string)
1609                    })
1610                })
1611        });
1612        let src_parent_region_id = src_area
1613            .properties
1614            .get("parent_region_id")
1615            .and_then(|v| v.as_str())
1616            .map(ToString::to_string);
1617        let dst_parent_region_id = dst_area
1618            .properties
1619            .get("parent_region_id")
1620            .and_then(|v| v.as_str())
1621            .map(ToString::to_string);
1622        let twin_parent_matches_src_parent = match (&twin_parent_region_id, &src_parent_region_id) {
1623            (Some(twin_parent), Some(src_parent)) => Some(twin_parent == src_parent),
1624            (None, None) if twin_present => Some(true),
1625            _ if twin_present => Some(false),
1626            _ => None,
1627        };
1628
1629        let reason = if !mapping_exists {
1630            Some("no_mapping_rule_between_src_and_dst".to_string())
1631        } else if episodic_rule_count == 0 {
1632            Some("mapping_exists_but_not_episodic_memory".to_string())
1633        } else if !dst_is_memory {
1634            Some("destination_is_not_memory_area".to_string())
1635        } else if src_is_memory {
1636            Some("upstream_is_memory_area_twin_not_supported".to_string())
1637        } else if !twin_present {
1638            Some("twin_expected_but_missing".to_string())
1639        } else if twin_parent_matches_src_parent == Some(false) {
1640            Some("twin_parent_region_mismatch_with_source".to_string())
1641        } else {
1642            None
1643        };
1644
1645        Ok(MemoryTwinDiagnostic {
1646            src_cortical_area: src_area_id.as_base_64(),
1647            dst_cortical_area: dst_area_id.as_base_64(),
1648            src_cortical_type: src_area.cortical_type.to_string(),
1649            dst_cortical_type: dst_area.cortical_type.to_string(),
1650            mapping_exists,
1651            episodic_rule_count,
1652            twin_expected,
1653            twin_present,
1654            twin_cortical_area,
1655            reason,
1656            src_parent_region_id,
1657            dst_parent_region_id,
1658            twin_parent_region_id,
1659            twin_parent_matches_src_parent,
1660        })
1661    }
1662
1663    /// Ensure a memory twin area exists for the given upstream and memory areas.
1664    pub fn ensure_memory_twin_area(
1665        &mut self,
1666        memory_area_id: &CorticalID,
1667        upstream_area_id: &CorticalID,
1668    ) -> BduResult<CorticalID> {
1669        use crate::models::CorticalAreaExt;
1670
1671        let register_replay_mapping = |manager: &mut ConnectomeManager,
1672                                       twin_id: &CorticalID|
1673         -> BduResult<()> {
1674            let Some(npu) = manager.npu.as_ref() else {
1675                return Ok(());
1676            };
1677            let memory_area_idx =
1678                *manager
1679                    .cortical_id_to_idx
1680                    .get(memory_area_id)
1681                    .ok_or_else(|| {
1682                        BduError::InvalidArea(format!(
1683                            "Memory area idx missing for {}",
1684                            memory_area_id.as_base_64()
1685                        ))
1686                    })?;
1687            let upstream_area_idx = *manager
1688                .cortical_id_to_idx
1689                .get(upstream_area_id)
1690                .ok_or_else(|| {
1691                    BduError::InvalidArea(format!(
1692                        "Upstream area idx missing for {}",
1693                        upstream_area_id.as_base_64()
1694                    ))
1695                })?;
1696            let twin_area_idx = *manager.cortical_id_to_idx.get(twin_id).ok_or_else(|| {
1697                BduError::InvalidArea(format!(
1698                    "Twin area idx missing for {}",
1699                    twin_id.as_base_64()
1700                ))
1701            })?;
1702            let twin_area = manager.cortical_areas.get(twin_id).ok_or_else(|| {
1703                BduError::InvalidArea(format!("Twin area {} not found", twin_id.as_base_64()))
1704            })?;
1705            let potential = twin_area.firing_threshold() + twin_area.firing_threshold_increment();
1706            if let Ok(mut npu_lock) = npu.lock() {
1707                npu_lock.register_memory_twin_mapping(
1708                    memory_area_idx,
1709                    upstream_area_idx,
1710                    twin_area_idx,
1711                    potential,
1712                );
1713            }
1714            Ok(())
1715        };
1716
1717        let memory_area = self.cortical_areas.get(memory_area_id).ok_or_else(|| {
1718            BduError::InvalidArea(format!(
1719                "Memory area {} not found",
1720                memory_area_id.as_base_64()
1721            ))
1722        })?;
1723        let upstream_area = self.cortical_areas.get(upstream_area_id).ok_or_else(|| {
1724            BduError::InvalidArea(format!(
1725                "Upstream area {} not found",
1726                upstream_area_id.as_base_64()
1727            ))
1728        })?;
1729
1730        if matches!(upstream_area.cortical_type, CorticalAreaType::Memory(_)) {
1731            return Err(BduError::InvalidArea(format!(
1732                "Upstream area {} is memory type; twin creation is only for non-memory areas",
1733                upstream_area_id.as_base_64()
1734            )));
1735        }
1736
1737        if let Some(existing) = memory_area
1738            .properties
1739            .get("memory_twin_areas")
1740            .and_then(|v| v.as_object())
1741            .and_then(|map| map.get(&upstream_area_id.as_base_64()))
1742            .and_then(|v| v.as_str())
1743            .and_then(|s| CorticalID::try_from_base_64(s).ok())
1744        {
1745            self.ensure_memory_replay_mapping(memory_area_id, &existing)?;
1746            register_replay_mapping(self, &existing)?;
1747            self.refresh_cortical_mappings_hash();
1748            return Ok(existing);
1749        }
1750
1751        let twin_id = self.build_memory_twin_id(memory_area_id, upstream_area_id)?;
1752        if self.cortical_areas.contains_key(&twin_id) {
1753            if let Some(existing) = self.cortical_areas.get_mut(&twin_id) {
1754                let expected_source = upstream_area_id.as_base_64();
1755                let expected_target = memory_area_id.as_base_64();
1756                let existing_source = existing
1757                    .properties
1758                    .get("memory_twin_of")
1759                    .and_then(|v| v.as_str());
1760                let existing_target = existing
1761                    .properties
1762                    .get("memory_twin_for")
1763                    .and_then(|v| v.as_str());
1764                if existing_source != Some(expected_source.as_str())
1765                    || existing_target != Some(expected_target.as_str())
1766                {
1767                    warn!(
1768                        target: "feagi-bdu",
1769                        "Twin cortical ID properties missing/mismatched for {} -> {}; repairing",
1770                        upstream_area_id.as_base_64(),
1771                        memory_area_id.as_base_64()
1772                    );
1773                    existing.properties.insert(
1774                        "memory_twin_of".to_string(),
1775                        serde_json::json!(expected_source),
1776                    );
1777                    existing.properties.insert(
1778                        "memory_twin_for".to_string(),
1779                        serde_json::json!(expected_target),
1780                    );
1781                }
1782            }
1783            self.set_memory_twin_mapping(memory_area_id, upstream_area_id, &twin_id);
1784            self.ensure_memory_replay_mapping(memory_area_id, &twin_id)?;
1785            register_replay_mapping(self, &twin_id)?;
1786            self.refresh_cortical_mappings_hash();
1787            return Ok(twin_id);
1788        }
1789
1790        let twin_name = format!("{}_twin", upstream_area.name.replace(' ', "_"));
1791        let twin_type = CorticalAreaType::Custom(CustomCorticalType::LeakyIntegrateFire);
1792        let twin_position = self.build_memory_twin_position(memory_area, upstream_area);
1793        let mut twin_area = CorticalArea::new(
1794            twin_id,
1795            0,
1796            twin_name,
1797            upstream_area.dimensions,
1798            twin_position,
1799            twin_type,
1800        )?;
1801        twin_area.properties = self.build_memory_twin_properties(
1802            memory_area,
1803            upstream_area,
1804            memory_area_id,
1805            upstream_area_id,
1806        );
1807
1808        let _twin_idx = self.add_cortical_area(twin_area)?;
1809        let _ = self.create_neurons_for_area(&twin_id);
1810
1811        self.set_memory_twin_mapping(memory_area_id, upstream_area_id, &twin_id);
1812        self.ensure_memory_replay_mapping(memory_area_id, &twin_id)?;
1813        register_replay_mapping(self, &twin_id)?;
1814        self.refresh_cortical_mappings_hash();
1815        Ok(twin_id)
1816    }
1817
1818    fn build_memory_twin_position(
1819        &self,
1820        _memory_area: &CorticalArea,
1821        upstream_area: &CorticalArea,
1822    ) -> GenomeCoordinate3D {
1823        let width = upstream_area.dimensions.width as f32;
1824        let margin = (width * 0.25).ceil() as i32;
1825        let offset = upstream_area.dimensions.width as i32 + margin;
1826        GenomeCoordinate3D::new(
1827            upstream_area.position.x + offset,
1828            upstream_area.position.y,
1829            upstream_area.position.z,
1830        )
1831    }
1832
1833    fn build_memory_twin_id(
1834        &self,
1835        memory_area_id: &CorticalID,
1836        upstream_area_id: &CorticalID,
1837    ) -> BduResult<CorticalID> {
1838        let mut hasher = Xxh64::new(DATA_HASH_SEED);
1839        hasher.write(memory_area_id.as_base_64().as_bytes());
1840        hasher.write(upstream_area_id.as_base_64().as_bytes());
1841        hasher.write(b"memory_twin");
1842        let hash = hasher.finish();
1843        let mut bytes = hash.to_be_bytes();
1844        bytes[0] = b'c';
1845        CorticalID::try_from_bytes(&bytes)
1846            .map_err(|e| BduError::Internal(format!("Failed to build twin cortical ID: {}", e)))
1847    }
1848
1849    fn build_memory_twin_properties(
1850        &self,
1851        _memory_area: &CorticalArea,
1852        upstream_area: &CorticalArea,
1853        memory_area_id: &CorticalID,
1854        upstream_area_id: &CorticalID,
1855    ) -> HashMap<String, serde_json::Value> {
1856        let mut props = upstream_area.properties.clone();
1857        props.remove("cortical_mapping_dst");
1858        props.remove("upstream_cortical_areas");
1859        props.remove("parent_region_id");
1860        props.insert("cortical_group".to_string(), serde_json::json!("CUSTOM"));
1861        props.insert("is_mem_type".to_string(), serde_json::json!(false));
1862        props.insert(
1863            "memory_twin_of".to_string(),
1864            serde_json::json!(upstream_area_id.as_base_64()),
1865        );
1866        props.insert(
1867            "memory_twin_for".to_string(),
1868            serde_json::json!(memory_area_id.as_base_64()),
1869        );
1870        if let Some(parent_region_id) = upstream_area
1871            .properties
1872            .get("parent_region_id")
1873            .and_then(|v| v.as_str())
1874        {
1875            props.insert(
1876                "parent_region_id".to_string(),
1877                serde_json::json!(parent_region_id),
1878            );
1879        }
1880        props
1881    }
1882
1883    fn set_memory_twin_mapping(
1884        &mut self,
1885        memory_area_id: &CorticalID,
1886        upstream_area_id: &CorticalID,
1887        twin_id: &CorticalID,
1888    ) {
1889        if let Some(memory_area) = self.cortical_areas.get_mut(memory_area_id) {
1890            let mapping = memory_area
1891                .properties
1892                .entry("memory_twin_areas".to_string())
1893                .or_insert_with(|| serde_json::json!({}));
1894            if let Some(map) = mapping.as_object_mut() {
1895                map.insert(
1896                    upstream_area_id.as_base_64(),
1897                    serde_json::json!(twin_id.as_base_64()),
1898                );
1899            }
1900        }
1901    }
1902
1903    fn ensure_memory_replay_mapping(
1904        &mut self,
1905        memory_area_id: &CorticalID,
1906        twin_id: &CorticalID,
1907    ) -> BduResult<()> {
1908        if !self.morphology_registry.contains("memory_replay") {
1909            feagi_evolutionary::add_core_morphologies(&mut self.morphology_registry);
1910        }
1911        self.refresh_morphologies_hash();
1912        let mapping_data = vec![serde_json::json!({
1913            "morphology_id": "memory_replay",
1914            "morphology_scalar": [1, 1, 1],
1915            "postSynapticCurrent_multiplier": 1,
1916            "plasticity_flag": false,
1917            "plasticity_constant": 0,
1918            "ltp_multiplier": 0,
1919            "ltd_multiplier": 0,
1920            "plasticity_window": 0,
1921        })];
1922        self.update_cortical_mapping(memory_area_id, twin_id, mapping_data)?;
1923        let _ = self.regenerate_synapses_for_mapping(memory_area_id, twin_id)?;
1924        // Update cortical area hash so BV refreshes area details (outgoing mappings).
1925        self.refresh_cortical_area_hashes(true, false);
1926        Ok(())
1927    }
1928
1929    /// Remove an upstream cortical area from a target area's upstream list
1930    ///
1931    /// This should be called when all synapses from src_cortical_idx to target_cortical_id are deleted.
1932    ///
1933    /// # Arguments
1934    ///
1935    /// * `target_cortical_id` - The cortical area that had connections
1936    /// * `src_cortical_idx` - The cortical index of the source area to remove
1937    ///
1938    pub fn remove_upstream_area(&mut self, target_cortical_id: &CorticalID, src_cortical_idx: u32) {
1939        if let Some(area) = self.cortical_areas.get_mut(target_cortical_id) {
1940            if let Some(upstream_prop) = area.properties.get_mut("upstream_cortical_areas") {
1941                if let Some(arr) = upstream_prop.as_array_mut() {
1942                    let src_value = serde_json::json!(src_cortical_idx);
1943                    if let Some(pos) = arr.iter().position(|v| v == &src_value) {
1944                        arr.remove(pos);
1945                        debug!(target: "feagi-bdu",
1946                            "Removed upstream area idx={} from cortical area '{}'",
1947                            src_cortical_idx, target_cortical_id.as_base_64()
1948                        );
1949                    }
1950                }
1951            }
1952        }
1953    }
1954
1955    /// Check if a cortical area exists
1956    pub fn has_cortical_area(&self, cortical_id: &CorticalID) -> bool {
1957        self.cortical_areas.contains_key(cortical_id)
1958    }
1959
1960    /// Check if the connectome is initialized (has areas)
1961    pub fn is_initialized(&self) -> bool {
1962        self.initialized && !self.cortical_areas.is_empty()
1963    }
1964
1965    // ======================================================================
1966    // Brain Region Management
1967    // ======================================================================
1968
1969    /// Add a brain region
1970    pub fn add_brain_region(
1971        &mut self,
1972        region: BrainRegion,
1973        parent_id: Option<String>,
1974    ) -> BduResult<()> {
1975        self.brain_regions.add_region(region, parent_id)?;
1976        self.refresh_brain_regions_hash();
1977        Ok(())
1978    }
1979
1980    /// Remove a brain region
1981    pub fn remove_brain_region(&mut self, region_id: &str) -> BduResult<()> {
1982        self.brain_regions.remove_region(region_id)?;
1983        self.refresh_brain_regions_hash();
1984        Ok(())
1985    }
1986
1987    /// Change the parent of an existing brain region.
1988    pub fn change_brain_region_parent(
1989        &mut self,
1990        region_id: &str,
1991        new_parent_id: &str,
1992    ) -> BduResult<()> {
1993        self.brain_regions.change_parent(region_id, new_parent_id)?;
1994        self.refresh_brain_regions_hash();
1995        Ok(())
1996    }
1997
1998    /// Get a brain region by ID
1999    pub fn get_brain_region(&self, region_id: &str) -> Option<&BrainRegion> {
2000        self.brain_regions.get_region(region_id)
2001    }
2002
2003    /// Get a mutable reference to a brain region
2004    pub fn get_brain_region_mut(&mut self, region_id: &str) -> Option<&mut BrainRegion> {
2005        self.brain_regions.get_region_mut(region_id)
2006    }
2007
2008    /// Get all brain region IDs
2009    pub fn get_brain_region_ids(&self) -> Vec<&String> {
2010        self.brain_regions.get_all_region_ids()
2011    }
2012
2013    /// Get the brain region hierarchy
2014    pub fn get_brain_region_hierarchy(&self) -> &BrainRegionHierarchy {
2015        &self.brain_regions
2016    }
2017
2018    // ========================================================================
2019    // MORPHOLOGY ACCESS
2020    // ========================================================================
2021
2022    /// Get all morphologies from the loaded genome
2023    pub fn get_morphologies(&self) -> &feagi_evolutionary::MorphologyRegistry {
2024        &self.morphology_registry
2025    }
2026
2027    /// Get morphology count
2028    pub fn get_morphology_count(&self) -> usize {
2029        self.morphology_registry.count()
2030    }
2031
2032    /// Insert or overwrite a morphology definition in the in-memory registry.
2033    ///
2034    /// NOTE: This updates the runtime registry used by mapping/synapse generation.
2035    /// Callers that also maintain a RuntimeGenome (source of truth) MUST update it too.
2036    pub fn upsert_morphology(
2037        &mut self,
2038        morphology_id: String,
2039        morphology: feagi_evolutionary::Morphology,
2040    ) {
2041        self.morphology_registry
2042            .add_morphology(morphology_id, morphology);
2043        self.refresh_morphologies_hash();
2044    }
2045
2046    /// Remove a morphology definition from the in-memory registry.
2047    ///
2048    /// Returns true if the morphology existed and was removed.
2049    ///
2050    /// NOTE: Callers that also maintain a RuntimeGenome (source of truth) MUST update it too.
2051    pub fn remove_morphology(&mut self, morphology_id: &str) -> bool {
2052        let removed = self.morphology_registry.remove_morphology(morphology_id);
2053        if removed {
2054            self.refresh_morphologies_hash();
2055        }
2056        removed
2057    }
2058
2059    // ========================================================================
2060    // CORTICAL MAPPING UPDATES
2061    // ========================================================================
2062
2063    /// Update cortical mapping properties between two cortical areas
2064    ///
2065    /// Updates only the source area's `cortical_mapping_dst` entry for this destination.
2066    /// Associative memory mappings are **directed**: a reverse edge (if any) is stored only when
2067    /// the client updates that pair explicitly (separate PUT).
2068    ///
2069    /// # Arguments
2070    /// * `src_area_id` - Source cortical area ID
2071    /// * `dst_area_id` - Destination cortical area ID
2072    /// * `mapping_data` - List of connection specifications
2073    ///
2074    /// # Returns
2075    /// * `BduResult<()>` - Ok if successful, Err otherwise
2076    pub fn update_cortical_mapping(
2077        &mut self,
2078        src_area_id: &CorticalID,
2079        dst_area_id: &CorticalID,
2080        mapping_data: Vec<serde_json::Value>,
2081    ) -> BduResult<()> {
2082        use tracing::info;
2083
2084        crate::region_io_designation::validate_cross_region_mapping_proposal(
2085            self,
2086            src_area_id,
2087            dst_area_id,
2088            &mapping_data,
2089        )?;
2090
2091        debug!(target: "feagi-bdu", "Updating cortical mapping: {} -> {}", src_area_id, dst_area_id);
2092
2093        {
2094            // Get source area (must exist)
2095            let src_area = self.cortical_areas.get_mut(src_area_id).ok_or_else(|| {
2096                crate::types::BduError::InvalidArea(format!(
2097                    "Source area not found: {}",
2098                    src_area_id
2099                ))
2100            })?;
2101
2102            // Get or create cortical_mapping_dst property
2103            let cortical_mapping_dst =
2104                if let Some(existing) = src_area.properties.get_mut("cortical_mapping_dst") {
2105                    existing.as_object_mut().ok_or_else(|| {
2106                        crate::types::BduError::InvalidMorphology(
2107                            "cortical_mapping_dst is not an object".to_string(),
2108                        )
2109                    })?
2110                } else {
2111                    // Create new cortical_mapping_dst
2112                    src_area
2113                        .properties
2114                        .insert("cortical_mapping_dst".to_string(), serde_json::json!({}));
2115                    src_area
2116                        .properties
2117                        .get_mut("cortical_mapping_dst")
2118                        .unwrap()
2119                        .as_object_mut()
2120                        .unwrap()
2121                };
2122
2123            // Update or add the mapping for this destination
2124            if mapping_data.is_empty() {
2125                // Empty mapping_data = delete the connection
2126                cortical_mapping_dst.remove(&dst_area_id.as_base_64());
2127                info!(target: "feagi-bdu", "Removed mapping from {} to {}", src_area_id, dst_area_id);
2128            } else {
2129                cortical_mapping_dst.insert(
2130                    dst_area_id.as_base_64(),
2131                    serde_json::Value::Array(mapping_data.clone()),
2132                );
2133                debug!(target: "feagi-bdu", "Updated mapping from {} to {} with {} connections",
2134                      src_area_id, dst_area_id, mapping_data.len());
2135            }
2136        }
2137
2138        self.refresh_cortical_mappings_hash();
2139
2140        Ok(())
2141    }
2142
2143    /// Regenerate synapses for a specific cortical mapping
2144    ///
2145    /// Creates new synapses based on mapping rules. Only removes existing synapses if
2146    /// a mapping already existed (update case), not for new mappings (allows multiple
2147    /// synapses between the same neurons).
2148    ///
2149    /// # Arguments
2150    /// * `src_area_id` - Source cortical area ID
2151    /// * `dst_area_id` - Destination cortical area ID
2152    ///
2153    /// # Returns
2154    /// * `BduResult<usize>` - Number of synapses created
2155    pub fn regenerate_synapses_for_mapping(
2156        &mut self,
2157        src_area_id: &CorticalID,
2158        dst_area_id: &CorticalID,
2159    ) -> BduResult<usize> {
2160        use tracing::info;
2161
2162        debug!(target: "feagi-bdu", "Regenerating synapses: {} -> {}", src_area_id, dst_area_id);
2163
2164        let mapping_rules_len = self
2165            .cortical_areas
2166            .get(src_area_id)
2167            .and_then(|area| area.properties.get("cortical_mapping_dst"))
2168            .and_then(|v| v.as_object())
2169            .and_then(|map| map.get(&dst_area_id.as_base_64()))
2170            .and_then(|v| v.as_array())
2171            .map(|arr| arr.len())
2172            .unwrap_or(0);
2173        tracing::debug!(
2174            target: "feagi-bdu",
2175            "Mapping rules for {} -> {}: {}",
2176            src_area_id,
2177            dst_area_id,
2178            mapping_rules_len
2179        );
2180
2181        // If NPU is available, regenerate synapses
2182        let Some(npu_arc) = self.npu.clone() else {
2183            info!(target: "feagi-bdu", "NPU not available - skipping synapse regeneration");
2184            return Ok(0);
2185        };
2186
2187        // Mapping regeneration must be deterministic:
2188        // - On mapping deletion: prune all synapses from A→B, then attempt synaptogenesis (which yields 0).
2189        // - On rule removal/updates: prune all synapses from A→B, then re-run synaptogenesis using the *current*
2190        //   mapping rules. This guarantees stale synapses from removed rules do not persist, while preserving
2191        //   other A→B mappings by re-creating them from the remaining rules.
2192        //
2193        // NOTE: This pruning requires retrieving neuron IDs in each area. Today, that can be O(all_neurons)
2194        // via `get_neurons_in_cortical_area()`. This is the safest correctness-first behavior.
2195
2196        let src_idx = *self.cortical_id_to_idx.get(src_area_id).ok_or_else(|| {
2197            BduError::InvalidArea(format!("No cortical idx for source area {}", src_area_id))
2198        })?;
2199        let dst_idx = *self.cortical_id_to_idx.get(dst_area_id).ok_or_else(|| {
2200            BduError::InvalidArea(format!(
2201                "No cortical idx for destination area {}",
2202                dst_area_id
2203            ))
2204        })?;
2205
2206        // Prune all existing synapses from src_area→dst_area before (re)creating based on current rules.
2207        // This prevents stale synapses when rules are removed/edited.
2208        let mut pruned_synapse_count: usize = 0;
2209        use std::time::Instant;
2210        let start = Instant::now();
2211
2212        // Get neuron lists (may be slow; see note above).
2213        //
2214        // IMPORTANT: Do not rely on per-area cached neuron counts here. Pruning must be correct even if
2215        // caches are stale (e.g., in tests or during partial initialization). If either side is empty,
2216        // pruning is a no-op anyway.
2217        let (sources, targets) = {
2218            let lock_start = std::time::Instant::now();
2219            let npu = npu_arc.lock().unwrap();
2220            let lock_wait = lock_start.elapsed();
2221            tracing::debug!(
2222                target: "feagi-bdu",
2223                "[NPU-LOCK] prune list lock wait {:.2}ms for {} -> {}",
2224                lock_wait.as_secs_f64() * 1000.0,
2225                src_area_id,
2226                dst_area_id
2227            );
2228            let sources: Vec<NeuronId> = npu
2229                .get_neurons_in_cortical_area(src_idx)
2230                .into_iter()
2231                .map(NeuronId)
2232                .collect();
2233            let targets: Vec<NeuronId> = npu
2234                .get_neurons_in_cortical_area(dst_idx)
2235                .into_iter()
2236                .map(NeuronId)
2237                .collect();
2238            (sources, targets)
2239        };
2240
2241        tracing::debug!(
2242            target: "feagi-bdu",
2243            "Prune synapses: {} sources, {} targets",
2244            sources.len(),
2245            targets.len()
2246        );
2247
2248        if !sources.is_empty() && !targets.is_empty() {
2249            let remove_start = Instant::now();
2250            pruned_synapse_count = {
2251                let lock_start = std::time::Instant::now();
2252                let mut npu = npu_arc.lock().unwrap();
2253                let lock_wait = lock_start.elapsed();
2254                tracing::debug!(
2255                    target: "feagi-bdu",
2256                    "[NPU-LOCK] prune remove lock wait {:.2}ms for {} -> {}",
2257                    lock_wait.as_secs_f64() * 1000.0,
2258                    src_area_id,
2259                    dst_area_id
2260                );
2261                // Use direct source-target batch removal here rather than index-based removal.
2262                // This avoids false "Pruned 0" outcomes when propagation synapse_index is stale
2263                // during repeated rapid remap operations.
2264                npu.remove_synapses_between(sources, targets)
2265            };
2266            let remove_time = remove_start.elapsed();
2267            let total_time = start.elapsed();
2268
2269            info!(
2270                target: "feagi-bdu",
2271                "Pruned {} existing synapses for mapping {} -> {} (total={}ms, remove={}ms)",
2272                pruned_synapse_count,
2273                src_area_id,
2274                dst_area_id,
2275                total_time.as_millis(),
2276                remove_time.as_millis()
2277            );
2278
2279            // Update StateManager synapse count (health_check endpoint)
2280            if pruned_synapse_count > 0 {
2281                let pruned_u32 = u32::try_from(pruned_synapse_count).map_err(|_| {
2282                    BduError::Internal(format!(
2283                        "Pruned synapse count overflow (usize -> u32): {}",
2284                        pruned_synapse_count
2285                    ))
2286                })?;
2287                let state_manager = StateManager::instance();
2288                let state_manager = state_manager.read();
2289                let core_state = state_manager.get_core_state();
2290                core_state.subtract_synapse_count(pruned_u32);
2291                state_manager.subtract_cortical_area_outgoing_synapses(
2292                    &src_area_id.as_base_64(),
2293                    pruned_synapse_count,
2294                );
2295                state_manager.subtract_cortical_area_incoming_synapses(
2296                    &dst_area_id.as_base_64(),
2297                    pruned_synapse_count,
2298                );
2299
2300                // Best-effort: adjust per-area outgoing synapse count cache for the source area.
2301                // (Cache is used for lock-free health-check reads; correctness is eventually
2302                // consistent via periodic refresh of global count from NPU.)
2303                {
2304                    let mut cache = self.cached_synapse_counts_per_area.write();
2305                    let entry = cache
2306                        .entry(*src_area_id)
2307                        .or_insert_with(|| AtomicUsize::new(0));
2308                    let mut current = entry.load(Ordering::Relaxed);
2309                    loop {
2310                        let next = current.saturating_sub(pruned_synapse_count);
2311                        match entry.compare_exchange(
2312                            current,
2313                            next,
2314                            Ordering::Relaxed,
2315                            Ordering::Relaxed,
2316                        ) {
2317                            Ok(_) => break,
2318                            Err(v) => current = v,
2319                        }
2320                    }
2321                }
2322            }
2323        }
2324
2325        // Apply cortical mapping rules to create synapses (may be 0 for memory areas).
2326        //
2327        // IMPORTANT:
2328        // - We already pruned A→B synapses above to ensure no stale synapses remain after a rule removal/update.
2329        // - `apply_cortical_mapping_for_pair()` returns the created synapse count but does not update
2330        //   StateManager/caches; we do that immediately after the call.
2331        let synapse_count = self.apply_cortical_mapping_for_pair(src_area_id, dst_area_id)?;
2332        tracing::debug!(
2333            target: "feagi-bdu",
2334            "Synaptogenesis created {} synapses for {} -> {}",
2335            synapse_count,
2336            src_area_id,
2337            dst_area_id
2338        );
2339
2340        // Update synapse count caches and StateManager based on synapses created.
2341        // NOTE: apply_cortical_mapping_for_pair() does not touch caches/StateManager.
2342        if synapse_count > 0 {
2343            let created_u32 = u32::try_from(synapse_count).map_err(|_| {
2344                BduError::Internal(format!(
2345                    "Created synapse count overflow (usize -> u32): {}",
2346                    synapse_count
2347                ))
2348            })?;
2349
2350            // Update per-area outgoing synapse count cache (source area)
2351            {
2352                let mut cache = self.cached_synapse_counts_per_area.write();
2353                cache
2354                    .entry(*src_area_id)
2355                    .or_insert_with(|| AtomicUsize::new(0))
2356                    .fetch_add(synapse_count, Ordering::Relaxed);
2357            }
2358
2359            // Update StateManager synapse count (health_check endpoint)
2360            let state_manager = StateManager::instance();
2361            let state_manager = state_manager.read();
2362            let core_state = state_manager.get_core_state();
2363            core_state.add_synapse_count(created_u32);
2364            state_manager
2365                .add_cortical_area_outgoing_synapses(&src_area_id.as_base_64(), synapse_count);
2366            state_manager
2367                .add_cortical_area_incoming_synapses(&dst_area_id.as_base_64(), synapse_count);
2368        }
2369
2370        // Update upstream area tracking based on MAPPING existence, not synapse count
2371        // Memory areas have 0 synapses but still need upstream tracking for pattern detection
2372        let src_idx_for_upstream = src_idx;
2373
2374        // Check if mapping exists by looking at cortical_mapping_dst property (after update)
2375        let has_mapping = self
2376            .cortical_areas
2377            .get(src_area_id)
2378            .and_then(|area| area.properties.get("cortical_mapping_dst"))
2379            .and_then(|v| v.as_object())
2380            .and_then(|map| map.get(&dst_area_id.as_base_64()))
2381            .is_some();
2382
2383        debug!(target: "feagi-bdu",
2384            "Mapping result: {} synapses, {} -> {} (mapping_exists={}, will {}update upstream)",
2385            synapse_count,
2386            src_area_id.as_base_64(),
2387            dst_area_id.as_base_64(),
2388            has_mapping,
2389            if has_mapping { "" } else { "NOT " }
2390        );
2391
2392        if has_mapping {
2393            // Mapping exists - add to upstream tracking (for both memory and regular areas)
2394            self.add_upstream_area(dst_area_id, src_idx_for_upstream);
2395
2396            if let Some(dst_area) = self.cortical_areas.get(dst_area_id) {
2397                if matches!(dst_area.cortical_type, CorticalAreaType::Memory(_)) {
2398                    if let Err(e) = self.ensure_memory_twin_area(dst_area_id, src_area_id) {
2399                        warn!(
2400                            target: "feagi-bdu",
2401                            "Failed to ensure memory twin for {} -> {}: {}",
2402                            src_area_id.as_base_64(),
2403                            dst_area_id.as_base_64(),
2404                            e
2405                        );
2406                    }
2407                }
2408            }
2409
2410            // If destination is a memory area, register it with PlasticityExecutor (automatic)
2411            #[cfg(feature = "plasticity")]
2412            if let Some(ref executor) = self.plasticity_executor {
2413                use feagi_evolutionary::extract_memory_properties;
2414
2415                if let Some(dst_area) = self.cortical_areas.get(dst_area_id) {
2416                    if let Some(mem_props) = extract_memory_properties(&dst_area.properties) {
2417                        let upstream_areas =
2418                            self.get_episodic_memory_upstream_cortical_areas(dst_area_id);
2419                        debug!(
2420                            target: "feagi-bdu",
2421                            "Registering memory area idx={} id={} upstream={} depth={}",
2422                            dst_area.cortical_idx,
2423                            dst_area_id.as_base_64(),
2424                            upstream_areas.len(),
2425                            mem_props.temporal_depth
2426                        );
2427
2428                        // Ensure FireLedger tracks the upstream areas with at least the required temporal depth.
2429                        // Dense, burst-aligned tracking is required for correct memory pattern hashing.
2430                        if let Some(ref npu_arc) = self.npu {
2431                            if let Ok(mut npu) = npu_arc.lock() {
2432                                let existing_configs = npu.get_all_fire_ledger_configs();
2433                                for &upstream_idx in &upstream_areas {
2434                                    let existing = existing_configs
2435                                        .iter()
2436                                        .find(|(idx, _)| *idx == upstream_idx)
2437                                        .map(|(_, w)| *w)
2438                                        .unwrap_or(0);
2439
2440                                    let desired = mem_props.temporal_depth as usize;
2441                                    let resolved = existing.max(desired);
2442                                    if resolved != existing {
2443                                        if let Err(e) =
2444                                            npu.configure_fire_ledger_window(upstream_idx, resolved)
2445                                        {
2446                                            warn!(
2447                                                target: "feagi-bdu",
2448                                                "Failed to configure FireLedger window for upstream area idx={} (requested={}): {}",
2449                                                upstream_idx,
2450                                                resolved,
2451                                                e
2452                                            );
2453                                        }
2454                                    }
2455                                }
2456                            } else {
2457                                warn!(target: "feagi-bdu", "Failed to lock NPU for FireLedger configuration");
2458                            }
2459                        }
2460
2461                        if let Ok(exec) = executor.lock() {
2462                            use feagi_npu_plasticity::{
2463                                MemoryNeuronLifecycleConfig, PlasticityExecutor,
2464                            };
2465
2466                            let lifecycle_config = MemoryNeuronLifecycleConfig {
2467                                initial_lifespan: mem_props.init_lifespan,
2468                                lifespan_growth_rate: mem_props.lifespan_growth_rate,
2469                                longterm_threshold: mem_props.longterm_threshold,
2470                                max_reactivations: 1000,
2471                            };
2472
2473                            exec.register_memory_area(
2474                                dst_area.cortical_idx,
2475                                dst_area_id.as_base_64(),
2476                                mem_props.temporal_depth,
2477                                upstream_areas.clone(),
2478                                Some(lifecycle_config),
2479                                mem_props.mp_learning_enabled,
2480                            );
2481                        } else {
2482                            warn!(target: "feagi-bdu", "Failed to lock PlasticityExecutor");
2483                        }
2484                    } else {
2485                        debug!(
2486                            target: "feagi-bdu",
2487                            "Skipping plasticity registration: no memory properties for area {}",
2488                            dst_area_id.as_base_64()
2489                        );
2490                    }
2491                } else {
2492                    warn!(target: "feagi-bdu", "Destination area {} not found in cortical_areas", dst_area_id.as_base_64());
2493                }
2494            } else {
2495                warn!(
2496                    target: "feagi-bdu",
2497                    "PlasticityExecutor not available; memory area {} not registered",
2498                    dst_area_id.as_base_64()
2499                );
2500            }
2501
2502            #[cfg(not(feature = "plasticity"))]
2503            {
2504                info!(target: "feagi-bdu", "Plasticity feature disabled at compile time");
2505            }
2506        } else {
2507            // Mapping deleted - remove from upstream tracking
2508            self.remove_upstream_area(dst_area_id, src_idx_for_upstream);
2509
2510            // Ensure any STDP mapping parameters for this pair are removed when the mapping is gone.
2511            let mut npu = npu_arc.lock().unwrap();
2512            let _was_registered = npu.unregister_stdp_mapping(src_idx, dst_idx);
2513        }
2514
2515        debug!(
2516            target: "feagi-bdu",
2517            "Created {} new synapses: {} -> {}",
2518            synapse_count,
2519            src_area_id,
2520            dst_area_id
2521        );
2522
2523        // CRITICAL: Rebuild synapse index so removals are reflected in propagation and query paths.
2524        // Many morphology paths rebuild the index after creation, but pruning requires an explicit rebuild.
2525        if pruned_synapse_count > 0 || synapse_count == 0 {
2526            let mut npu = npu_arc.lock().unwrap();
2527            npu.rebuild_synapse_index();
2528            info!(
2529                target: "feagi-bdu",
2530                "Rebuilt synapse index after regenerating {} -> {} (pruned={}, created={})",
2531                src_area_id,
2532                dst_area_id,
2533                pruned_synapse_count,
2534                synapse_count
2535            );
2536        } else {
2537            debug!(
2538                target: "feagi-bdu",
2539                "Skipped synapse index rebuild for mapping {} -> {} (created={}, pruned=0; index rebuilt during synaptogenesis)",
2540                src_area_id,
2541                dst_area_id,
2542                synapse_count
2543            );
2544        }
2545
2546        // Refresh the global synapse count cache from NPU (deterministic after prune/create).
2547        {
2548            let npu = npu_arc.lock().unwrap();
2549            let fresh_count = npu.get_synapse_count();
2550            self.cached_synapse_count
2551                .store(fresh_count, Ordering::Relaxed);
2552        }
2553
2554        Ok(synapse_count)
2555    }
2556
2557    /// Whole numbers often arrive as JSON floats (e.g. `1.0`); `as_i64`/`as_u64` return None for those.
2558    fn json_number_as_i64_for_stdp(v: &serde_json::Value) -> Option<i64> {
2559        v.as_i64().or_else(|| v.as_f64().map(|f| f as i64))
2560    }
2561
2562    fn json_number_as_usize_for_stdp(v: &serde_json::Value) -> Option<usize> {
2563        v.as_u64()
2564            .map(|n| n as usize)
2565            .or_else(|| v.as_f64().map(|f| f as usize))
2566    }
2567
2568    /// Register STDP mapping parameters for a plastic rule
2569    #[allow(clippy::too_many_arguments)]
2570    fn register_stdp_mapping_for_rule(
2571        npu: &Arc<feagi_npu_burst_engine::TracingMutex<feagi_npu_burst_engine::DynamicNPU>>,
2572        src_area_id: &CorticalID,
2573        dst_area_id: &CorticalID,
2574        src_cortical_idx: u32,
2575        dst_cortical_idx: u32,
2576        rule_obj: &serde_json::Map<String, serde_json::Value>,
2577        bidirectional_stdp: bool,
2578        synapse_psp: f32,
2579        synapse_type: feagi_npu_neural::SynapseType,
2580    ) -> BduResult<()> {
2581        let plasticity_window = rule_obj
2582            .get("plasticity_window")
2583            .and_then(Self::json_number_as_usize_for_stdp)
2584            .ok_or_else(|| {
2585                BduError::Internal(format!(
2586                    "Missing plasticity_window in plastic mapping rule {} -> {}",
2587                    src_area_id, dst_area_id
2588                ))
2589            })?;
2590        let plasticity_constant = rule_obj
2591            .get("plasticity_constant")
2592            .and_then(Self::json_number_as_i64_for_stdp)
2593            .ok_or_else(|| {
2594                BduError::Internal(format!(
2595                    "Missing plasticity_constant in plastic mapping rule {} -> {}",
2596                    src_area_id, dst_area_id
2597                ))
2598            })?;
2599        let ltp_i64 = rule_obj
2600            .get("ltp_multiplier")
2601            .and_then(Self::json_number_as_i64_for_stdp)
2602            .ok_or_else(|| {
2603                BduError::Internal(format!(
2604                    "Missing ltp_multiplier in plastic mapping rule {} -> {}",
2605                    src_area_id, dst_area_id
2606                ))
2607            })?;
2608        let ltp_multiplier = i8::try_from(ltp_i64).map_err(|_| {
2609            BduError::Internal(format!(
2610                "ltp_multiplier must fit in i8 range {}..={} (got {}) on mapping {} -> {}",
2611                i8::MIN,
2612                i8::MAX,
2613                ltp_i64,
2614                src_area_id,
2615                dst_area_id
2616            ))
2617        })?;
2618        let ltd_i64 = rule_obj
2619            .get("ltd_multiplier")
2620            .and_then(Self::json_number_as_i64_for_stdp)
2621            .ok_or_else(|| {
2622                BduError::Internal(format!(
2623                    "Missing ltd_multiplier in plastic mapping rule {} -> {}",
2624                    src_area_id, dst_area_id
2625                ))
2626            })?;
2627        let ltd_multiplier = i8::try_from(ltd_i64).map_err(|_| {
2628            BduError::Internal(format!(
2629                "ltd_multiplier must fit in i8 range {}..={} (got {}) on mapping {} -> {}",
2630                i8::MIN,
2631                i8::MAX,
2632                ltd_i64,
2633                src_area_id,
2634                dst_area_id
2635            ))
2636        })?;
2637
2638        // Resolve plasticity_mode with legacy fallback (auto-migrate strategy):
2639        //   - new genomes set `plasticity_mode: "off" | "stdp" | "rstdp"` directly;
2640        //   - legacy genomes only have `plasticity_flag: bool` -> Stdp / Off mapping.
2641        let plasticity_mode = match rule_obj.get("plasticity_mode").and_then(|v| v.as_str()) {
2642            Some(s) if s.eq_ignore_ascii_case("rstdp") || s.eq_ignore_ascii_case("r-stdp") => {
2643                feagi_npu_burst_engine::npu::PlasticityMode::RStdp
2644            }
2645            Some(s) if s.eq_ignore_ascii_case("stdp") => {
2646                feagi_npu_burst_engine::npu::PlasticityMode::Stdp
2647            }
2648            Some(s) if s.eq_ignore_ascii_case("off") => {
2649                feagi_npu_burst_engine::npu::PlasticityMode::Off
2650            }
2651            Some(other) => {
2652                return Err(BduError::Internal(format!(
2653                    "Unknown plasticity_mode '{}' in mapping rule {} -> {}",
2654                    other, src_area_id, dst_area_id
2655                )));
2656            }
2657            None => feagi_npu_burst_engine::npu::PlasticityMode::Stdp,
2658        };
2659
2660        // R-STDP-only fields. Strings name cortical areas by 6-char base-64 ID; resolve via NPU.
2661        let eligibility_decay_bursts = rule_obj
2662            .get("eligibility_decay_bursts")
2663            .and_then(|v| v.as_u64())
2664            .map(|n| n as u32)
2665            .unwrap_or(0);
2666        let reward_source_area_id = rule_obj
2667            .get("reward_source_area")
2668            .and_then(|v| v.as_str())
2669            .map(str::to_string);
2670        let punishment_source_area_id = rule_obj
2671            .get("punishment_source_area")
2672            .and_then(|v| v.as_str())
2673            .map(str::to_string);
2674
2675        // Optional upper-bound clamp for plasticity weight commits. Absent / null means no
2676        // clamp (legacy unbounded behaviour). When provided, must be a strictly positive
2677        // f32 (finite or `+inf`); `NaN`, zero, and negatives are rejected so the runtime
2678        // never sees a malformed sentinel.
2679        let max_weight_provided = rule_obj.get("max_weight").is_some()
2680            && !rule_obj
2681                .get("max_weight")
2682                .map(|v| v.is_null())
2683                .unwrap_or(true);
2684        let max_weight: f32 = if max_weight_provided {
2685            let raw = rule_obj
2686                .get("max_weight")
2687                .and_then(|v| v.as_f64())
2688                .ok_or_else(|| {
2689                    BduError::Internal(format!(
2690                        "max_weight must be a number on mapping {} -> {}",
2691                        src_area_id, dst_area_id
2692                    ))
2693                })?;
2694            if raw.is_nan() || raw <= 0.0 {
2695                return Err(BduError::Internal(format!(
2696                    "max_weight must be strictly positive (got {}) on mapping {} -> {}",
2697                    raw, src_area_id, dst_area_id
2698                )));
2699            }
2700            raw as f32
2701        } else {
2702            f32::INFINITY
2703        };
2704
2705        // Optional f32 learning-rate scale on the end-of-burst weight commit: w += eta * R * e.
2706        // Omitted / null → 1.0. Must be finite, strictly positive, and not +inf.
2707        let plasticity_eta_provided = rule_obj.get("plasticity_eta").is_some()
2708            && !rule_obj
2709                .get("plasticity_eta")
2710                .map(|v| v.is_null())
2711                .unwrap_or(true);
2712        let plasticity_eta: f32 = if plasticity_eta_provided {
2713            let raw = rule_obj
2714                .get("plasticity_eta")
2715                .and_then(|v| v.as_f64())
2716                .ok_or_else(|| {
2717                    BduError::Internal(format!(
2718                        "plasticity_eta must be a number on mapping {} -> {}",
2719                        src_area_id, dst_area_id
2720                    ))
2721                })?;
2722            if raw.is_nan() || raw <= 0.0 || !raw.is_finite() {
2723                return Err(BduError::Internal(format!(
2724                    "plasticity_eta must be finite and strictly positive (got {}) on mapping {} -> {}",
2725                    raw, src_area_id, dst_area_id
2726                )));
2727            }
2728            raw as f32
2729        } else {
2730            1.0
2731        };
2732
2733        // Validate R-STDP fields are absent when not in RStdp mode (catches genome typos early).
2734        if !matches!(
2735            plasticity_mode,
2736            feagi_npu_burst_engine::npu::PlasticityMode::RStdp
2737        ) && (reward_source_area_id.is_some()
2738            || punishment_source_area_id.is_some()
2739            || eligibility_decay_bursts != 0)
2740        {
2741            return Err(BduError::Internal(format!(
2742                "R-STDP fields (reward_source_area / punishment_source_area / eligibility_decay_bursts) \
2743                 only valid when plasticity_mode='rstdp' on mapping {} -> {}",
2744                src_area_id, dst_area_id
2745            )));
2746        }
2747
2748        // `max_weight` is only meaningful when plasticity is active. Reject explicit values
2749        // on Off-mode mappings to surface genome typos early; an absent field silently
2750        // resolves to `f32::INFINITY` above and is fine.
2751        if matches!(
2752            plasticity_mode,
2753            feagi_npu_burst_engine::npu::PlasticityMode::Off
2754        ) && max_weight_provided
2755        {
2756            return Err(BduError::Internal(format!(
2757                "max_weight is only valid when plasticity_mode is 'stdp' or 'rstdp' (got off) on mapping {} -> {}",
2758                src_area_id, dst_area_id
2759            )));
2760        }
2761
2762        if matches!(
2763            plasticity_mode,
2764            feagi_npu_burst_engine::npu::PlasticityMode::Off
2765        ) && plasticity_eta_provided
2766        {
2767            return Err(BduError::Internal(format!(
2768                "plasticity_eta is only valid when plasticity_mode is 'stdp' or 'rstdp' (got off) on mapping {} -> {}",
2769                src_area_id, dst_area_id
2770            )));
2771        }
2772
2773        trace!(target: "feagi-bdu", "[LOCK-TRACE] create_neurons_for_area: attempting NPU lock");
2774        let mut npu_lock = npu
2775            .lock()
2776            .map_err(|e| BduError::Internal(format!("Failed to lock NPU: {}", e)))?;
2777        trace!(target: "feagi-bdu", "[LOCK-TRACE] create_neurons_for_area: acquired NPU lock");
2778
2779        // Resolve reward/punishment area names to cortical_idx (R-STDP only). The detector
2780        // areas must already be registered with the NPU before this mapping is parsed; the
2781        // genome ordering normally handles this because cortical areas are processed before
2782        // their cross-area mapping rules.
2783        let resolve_optional_area =
2784            |label: &str, name_opt: &Option<String>| -> BduResult<Option<u32>> {
2785                let Some(name) = name_opt else {
2786                    return Ok(None);
2787                };
2788                match npu_lock.get_cortical_area_id(name.as_str()) {
2789                    Some(idx) => Ok(Some(idx)),
2790                    None => Err(BduError::Internal(format!(
2791                        "Unknown {} cortical area '{}' on R-STDP mapping {} -> {}",
2792                        label, name, src_area_id, dst_area_id
2793                    ))),
2794                }
2795            };
2796        let reward_source_area =
2797            resolve_optional_area("reward_source_area", &reward_source_area_id)?;
2798        let punishment_source_area =
2799            resolve_optional_area("punishment_source_area", &punishment_source_area_id)?;
2800
2801        // Off-mode mappings are skipped (no NPU registration, no fire-ledger tracking).
2802        if matches!(
2803            plasticity_mode,
2804            feagi_npu_burst_engine::npu::PlasticityMode::Off
2805        ) {
2806            return Ok(());
2807        }
2808
2809        let params = feagi_npu_burst_engine::npu::StdpMappingParams {
2810            plasticity_window,
2811            plasticity_constant,
2812            ltp_multiplier,
2813            ltd_multiplier,
2814            bidirectional_stdp,
2815            synapse_psp,
2816            synapse_type,
2817            plasticity_mode,
2818            eligibility_decay_bursts,
2819            reward_source_area,
2820            punishment_source_area,
2821            max_weight,
2822            plasticity_eta,
2823        };
2824
2825        npu_lock
2826            .register_stdp_mapping(src_cortical_idx, dst_cortical_idx, params)
2827            .map_err(|e| {
2828                BduError::Internal(format!(
2829                    "Failed to register STDP mapping {} -> {}: {}",
2830                    src_area_id, dst_area_id, e
2831                ))
2832            })?;
2833
2834        // FireLedger tracking. Plain STDP needs depth=plasticity_window on src+dst. R-STDP
2835        // additionally needs depth>=1 on reward/punishment source areas so `activity_density`
2836        // can sample current-burst firing.
2837        let mut areas_to_track: Vec<(u32, usize)> = vec![
2838            (src_cortical_idx, plasticity_window),
2839            (dst_cortical_idx, plasticity_window),
2840        ];
2841        if let Some(area) = reward_source_area {
2842            areas_to_track.push((area, 1));
2843        }
2844        if let Some(area) = punishment_source_area {
2845            areas_to_track.push((area, 1));
2846        }
2847
2848        let existing_configs = npu_lock.get_all_fire_ledger_configs();
2849        for (area_idx, required_depth) in areas_to_track {
2850            let existing = existing_configs
2851                .iter()
2852                .find(|(idx, _)| *idx == area_idx)
2853                .map(|(_, w)| *w)
2854                .unwrap_or(0);
2855            let resolved = existing.max(required_depth);
2856            if resolved != existing {
2857                npu_lock
2858                    .configure_fire_ledger_window(area_idx, resolved)
2859                    .map_err(|e| {
2860                        BduError::Internal(format!(
2861                            "Failed to configure FireLedger window for area idx={} (requested={}): {}",
2862                            area_idx, resolved, e
2863                        ))
2864                    })?;
2865            }
2866        }
2867
2868        Ok(())
2869    }
2870
2871    /// Resolve synapse weight, PSP, type, and per-synapse delay (bursts) from a mapping rule.
2872    fn resolve_synapse_params_for_rule(
2873        &self,
2874        src_area_id: &CorticalID,
2875        rule: &serde_json::Value,
2876    ) -> BduResult<(f32, f32, feagi_npu_neural::SynapseType, u8)> {
2877        // Get source area to access PSP property
2878        let src_area = self.cortical_areas.get(src_area_id).ok_or_else(|| {
2879            crate::types::BduError::InvalidArea(format!("Source area not found: {}", src_area_id))
2880        })?;
2881
2882        // Extract weight from rule (`postSynapticCurrent_multiplier`) as full-precision float.
2883        let (weight, synapse_type) = {
2884            let parse_f64 = |v: &serde_json::Value| -> Option<f64> {
2885                if let Some(i) = v.as_i64() {
2886                    return Some(i as f64);
2887                }
2888                v.as_f64()
2889            };
2890
2891            let mult: f64 = if let Some(obj) = rule.as_object() {
2892                obj.get("postSynapticCurrent_multiplier")
2893                    .and_then(parse_f64)
2894                    .unwrap_or(1.0)
2895            } else if let Some(arr) = rule.as_array() {
2896                arr.get(2).and_then(parse_f64).unwrap_or(1.0)
2897            } else {
2898                128.0
2899            };
2900
2901            if mult < 0.0 {
2902                (mult.abs() as f32, feagi_npu_neural::SynapseType::Inhibitory)
2903            } else {
2904                (mult as f32, feagi_npu_neural::SynapseType::Excitatory)
2905            }
2906        };
2907
2908        // PSP from source cortical area (float; stored as f32 on synapses)
2909        use crate::models::cortical_area::CorticalAreaExt;
2910        let psp_f32 = src_area.postsynaptic_current();
2911
2912        let delay_bursts: u8 = if let Some(obj) = rule.as_object() {
2913            obj.get("synaptic_delay_bursts")
2914                .and_then(|v| v.as_u64())
2915                .map(|d| u8::try_from(d).unwrap_or(1))
2916                .unwrap_or(1)
2917        } else if let Some(arr) = rule.as_array() {
2918            arr.get(8)
2919                .and_then(|v| v.as_u64())
2920                .map(|d| u8::try_from(d).unwrap_or(1))
2921                .unwrap_or(1)
2922        } else {
2923            1
2924        };
2925        if delay_bursts < 1 {
2926            return Err(crate::types::BduError::Internal(format!(
2927                "synaptic_delay_bursts must be >= 1 (src area {})",
2928                src_area_id.as_base_64()
2929            )));
2930        }
2931
2932        tracing::debug!(
2933            target: "feagi-bdu",
2934            "Resolved synapse params src={} weight={} psp={} type={:?} delay_bursts={}",
2935            src_area_id.as_base_64(),
2936            weight,
2937            psp_f32,
2938            synapse_type,
2939            delay_bursts
2940        );
2941
2942        Ok((weight, psp_f32, synapse_type, delay_bursts))
2943    }
2944
2945    /// Apply cortical mapping for a specific area pair
2946    fn apply_cortical_mapping_for_pair(
2947        &mut self,
2948        src_area_id: &CorticalID,
2949        dst_area_id: &CorticalID,
2950    ) -> BduResult<usize> {
2951        // Clone the rules to avoid borrow checker issues.
2952        //
2953        // IMPORTANT: absence of mapping rules is a valid state (e.g. mapping deletion).
2954        // In that case, return Ok(0) rather than an error so API callers can treat
2955        // "deleted mapping" as success (and BV can update its cache/UI).
2956        let rules = {
2957            let src_area = self.cortical_areas.get(src_area_id).ok_or_else(|| {
2958                crate::types::BduError::InvalidArea(format!(
2959                    "Source area not found: {}",
2960                    src_area_id
2961                ))
2962            })?;
2963
2964            let Some(mapping_dst) = src_area
2965                .properties
2966                .get("cortical_mapping_dst")
2967                .and_then(|v| v.as_object())
2968            else {
2969                return Ok(0);
2970            };
2971
2972            let Some(rules) = Self::get_mapping_rules_for_destination(mapping_dst, dst_area_id)
2973            else {
2974                return Ok(0);
2975            };
2976
2977            rules.clone()
2978        }; // Borrow ends here
2979
2980        if rules.is_empty() {
2981            return Ok(0);
2982        }
2983
2984        // Get indices for STDP handling
2985        let src_cortical_idx = *self.cortical_id_to_idx.get(src_area_id).ok_or_else(|| {
2986            crate::types::BduError::InvalidArea(format!("No index for {}", src_area_id))
2987        })?;
2988        let dst_cortical_idx = *self.cortical_id_to_idx.get(dst_area_id).ok_or_else(|| {
2989            crate::types::BduError::InvalidArea(format!("No index for {}", dst_area_id))
2990        })?;
2991
2992        // Clone NPU Arc for STDP handling (Arc::clone is cheap - just increments ref count)
2993        let npu_arc = self
2994            .npu
2995            .as_ref()
2996            .ok_or_else(|| crate::types::BduError::Internal("NPU not connected".to_string()))?
2997            .clone();
2998
2999        tracing::debug!(
3000            target: "feagi-bdu",
3001            "Applying {} mapping rule(s) for {} -> {}",
3002            rules.len(),
3003            src_area_id,
3004            dst_area_id
3005        );
3006        // Apply each morphology rule
3007        let mut total_synapses = 0;
3008        for rule in &rules {
3009            let morphology_id = if let Some(rule_obj) = rule.as_object() {
3010                rule_obj
3011                    .get("morphology_id")
3012                    .and_then(|v| v.as_str())
3013                    .unwrap_or("unknown")
3014                    .to_string()
3015            } else if let Some(rule_arr) = rule.as_array() {
3016                rule_arr
3017                    .first()
3018                    .and_then(|v| v.as_str())
3019                    .unwrap_or("unknown")
3020                    .to_string()
3021            } else {
3022                "unknown".to_string()
3023            };
3024
3025            let rule_keys: Vec<String> = rule
3026                .as_object()
3027                .map(|obj| obj.keys().cloned().collect())
3028                .unwrap_or_default();
3029
3030            // Handle STDP/plasticity configuration if needed
3031            let mut plasticity_flag = rule
3032                .as_object()
3033                .and_then(|obj| obj.get("plasticity_flag"))
3034                .and_then(|v| v.as_bool())
3035                .unwrap_or(false);
3036            if morphology_id == "associative_memory" {
3037                plasticity_flag = true;
3038            }
3039            if plasticity_flag {
3040                let Some(rule_obj) = rule.as_object() else {
3041                    return Err(crate::types::BduError::InvalidMorphology(
3042                        "Plasticity mapping rule must be an object format".to_string(),
3043                    ));
3044                };
3045                let (_weight, psp, synapse_type, _delay_bursts) =
3046                    self.resolve_synapse_params_for_rule(src_area_id, rule)?;
3047                // Associative-memory mappings are directional.
3048                // Bidirectional behavior requires two explicit mappings (A->B and B->A).
3049                let bidirectional_stdp = false;
3050                if let Err(e) = Self::register_stdp_mapping_for_rule(
3051                    &npu_arc,
3052                    src_area_id,
3053                    dst_area_id,
3054                    src_cortical_idx,
3055                    dst_cortical_idx,
3056                    rule_obj,
3057                    bidirectional_stdp,
3058                    psp,
3059                    synapse_type,
3060                ) {
3061                    tracing::error!(
3062                        target: "feagi-bdu",
3063                        "STDP mapping registration failed for {} -> {} (morphology={}, keys={:?}): {}",
3064                        src_area_id,
3065                        dst_area_id,
3066                        morphology_id,
3067                        rule_keys,
3068                        e
3069                    );
3070                    return Err(e);
3071                }
3072            }
3073
3074            // Handle conditional gate (transistor synapse) configuration if present.
3075            // The gate_source_area field specifies a cortical area whose firing activity
3076            // gates propagation through all synapses created by this mapping rule.
3077            if let Some(gate_area_str) = rule
3078                .as_object()
3079                .and_then(|obj| obj.get("gate_source_area"))
3080                .and_then(|v| v.as_str())
3081            {
3082                let gate_area_id = CorticalID::try_from_base_64(gate_area_str).map_err(|_| {
3083                    crate::types::BduError::Internal(format!(
3084                        "Invalid gate_source_area '{}' on mapping {} -> {}",
3085                        gate_area_str, src_area_id, dst_area_id
3086                    ))
3087                })?;
3088                let gate_cortical_idx =
3089                    self.cortical_id_to_idx.get(&gate_area_id).ok_or_else(|| {
3090                        crate::types::BduError::Internal(format!(
3091                            "Unknown gate_source_area '{}' on mapping {} -> {}",
3092                            gate_area_str, src_area_id, dst_area_id
3093                        ))
3094                    })?;
3095
3096                let mut npu_lock = npu_arc.lock().map_err(|_| {
3097                    crate::types::BduError::Internal(
3098                        "Failed to acquire NPU lock for gate registration".to_string(),
3099                    )
3100                })?;
3101                if let Err(e) = npu_lock.register_gate_mapping(
3102                    src_cortical_idx,
3103                    dst_cortical_idx,
3104                    *gate_cortical_idx,
3105                ) {
3106                    tracing::error!(
3107                        target: "feagi-bdu",
3108                        "Gate mapping registration failed for {} -> {} (gate={}): {}",
3109                        src_area_id,
3110                        dst_area_id,
3111                        gate_area_str,
3112                        e
3113                    );
3114                    return Err(crate::types::BduError::Internal(format!(
3115                        "Gate registration failed: {}",
3116                        e
3117                    )));
3118                }
3119            }
3120
3121            // Apply the morphology rule
3122            let synapse_count = match self.apply_single_morphology_rule(
3123                src_area_id,
3124                dst_area_id,
3125                rule,
3126            ) {
3127                Ok(count) => count,
3128                Err(e) => {
3129                    tracing::error!(
3130                        target: "feagi-bdu",
3131                        "Mapping rule application failed for {} -> {} (morphology={}, keys={:?}): {}",
3132                        src_area_id,
3133                        dst_area_id,
3134                        morphology_id,
3135                        rule_keys,
3136                        e
3137                    );
3138                    return Err(e);
3139                }
3140            };
3141            total_synapses += synapse_count;
3142            tracing::debug!(
3143                target: "feagi-bdu",
3144                "Rule {} created {} synapses for {} -> {}",
3145                morphology_id,
3146                synapse_count,
3147                src_area_id,
3148                dst_area_id
3149            );
3150        }
3151
3152        Ok(total_synapses)
3153    }
3154
3155    /// Apply a function-type morphology (projector, memory, block_to_block, etc.)
3156    ///
3157    /// This helper consolidates all function-type morphology logic in one place.
3158    /// Function-type morphologies are code-driven and require code changes to add new ones.
3159    ///
3160    /// # Arguments
3161    /// * `morphology_id` - The morphology ID string (e.g., "projector", "block_to_block")
3162    /// * `rule` - The morphology rule JSON value
3163    /// * `npu_arc` - Arc to the NPU (for batched operations)
3164    /// * `npu` - Locked NPU reference
3165    /// * `src_area_id`, `dst_area_id` - Source and destination area IDs
3166    /// * `src_idx`, `dst_idx` - Source and destination area indices
3167    /// * `weight`, `psp`, `synapse_attractivity` - Synapse parameters
3168    #[allow(clippy::too_many_arguments)]
3169    fn apply_function_morphology(
3170        &self,
3171        morphology_id: &str,
3172        rule: &serde_json::Value,
3173        npu_arc: &Arc<feagi_npu_burst_engine::TracingMutex<feagi_npu_burst_engine::DynamicNPU>>,
3174        npu: &mut feagi_npu_burst_engine::DynamicNPU,
3175        src_area_id: &CorticalID,
3176        dst_area_id: &CorticalID,
3177        src_idx: u32,
3178        dst_idx: u32,
3179        weight: f32,
3180        psp: f32,
3181        synapse_attractivity: u8,
3182        synapse_type: feagi_npu_neural::SynapseType,
3183        delay_bursts: u8,
3184    ) -> BduResult<usize> {
3185        match morphology_id {
3186            "projector" | "transpose_xy" | "transpose_yz" | "transpose_xz" => {
3187                // Get dimensions from cortical areas (no neuron scanning!)
3188                let src_area = self.cortical_areas.get(src_area_id).ok_or_else(|| {
3189                    crate::types::BduError::InvalidArea(format!(
3190                        "Source area not found: {}",
3191                        src_area_id
3192                    ))
3193                })?;
3194                let dst_area = self.cortical_areas.get(dst_area_id).ok_or_else(|| {
3195                    crate::types::BduError::InvalidArea(format!(
3196                        "Destination area not found: {}",
3197                        dst_area_id
3198                    ))
3199                })?;
3200
3201                let src_dimensions = (
3202                    src_area.dimensions.width as usize,
3203                    src_area.dimensions.height as usize,
3204                    src_area.dimensions.depth as usize,
3205                );
3206                let dst_dimensions = (
3207                    dst_area.dimensions.width as usize,
3208                    dst_area.dimensions.height as usize,
3209                    dst_area.dimensions.depth as usize,
3210                );
3211
3212                // Legacy-compatible transpose mappings from Python FEAGI:
3213                // projector_xy -> (y, x, z), projector_yz -> (x, z, y), projector_xz -> (z, y, x)
3214                let transpose = match morphology_id {
3215                    "transpose_xy" => Some((1, 0, 2)),
3216                    "transpose_yz" => Some((0, 2, 1)),
3217                    "transpose_xz" => Some((2, 1, 0)),
3218                    _ => None,
3219                };
3220
3221                use crate::connectivity::core_morphologies::apply_projector_morphology_with_dimensions;
3222                let count = apply_projector_morphology_with_dimensions(
3223                    npu,
3224                    src_idx,
3225                    dst_idx,
3226                    src_dimensions,
3227                    dst_dimensions,
3228                    transpose,
3229                    None, // project_last_layer_of
3230                    weight,
3231                    psp,
3232                    synapse_attractivity,
3233                    synapse_type,
3234                    0,
3235                    delay_bursts,
3236                )?;
3237                // Ensure the propagation engine sees the newly created synapses immediately
3238                npu.rebuild_synapse_index();
3239                Ok(count as usize)
3240            }
3241            "centered_projector" => {
3242                let src_area = self.cortical_areas.get(src_area_id).ok_or_else(|| {
3243                    crate::types::BduError::InvalidArea(format!(
3244                        "Source area not found: {}",
3245                        src_area_id
3246                    ))
3247                })?;
3248                let dst_area = self.cortical_areas.get(dst_area_id).ok_or_else(|| {
3249                    crate::types::BduError::InvalidArea(format!(
3250                        "Destination area not found: {}",
3251                        dst_area_id
3252                    ))
3253                })?;
3254
3255                let src_dimensions = (
3256                    src_area.dimensions.width as usize,
3257                    src_area.dimensions.height as usize,
3258                    src_area.dimensions.depth as usize,
3259                );
3260                let dst_dimensions = (
3261                    dst_area.dimensions.width as usize,
3262                    dst_area.dimensions.height as usize,
3263                    dst_area.dimensions.depth as usize,
3264                );
3265
3266                let count = crate::connectivity::core_morphologies::apply_centered_projector_morphology_with_dimensions(
3267                    npu,
3268                    src_idx,
3269                    dst_idx,
3270                    src_dimensions,
3271                    dst_dimensions,
3272                    weight,
3273                    psp,
3274                    synapse_attractivity,
3275                    synapse_type,
3276                    delay_bursts,
3277                )?;
3278                if count > 0 {
3279                    npu.rebuild_synapse_index();
3280                }
3281                Ok(count as usize)
3282            }
3283            "episodic_memory" => {
3284                // Episodic memory morphology: No physical synapses created
3285                // Pattern detection and memory neuron creation handled by PlasticityService
3286                use tracing::trace;
3287                trace!(
3288                    target: "feagi-bdu",
3289                    "Episodic memory morphology: {} -> {} (no physical synapses, plasticity-driven)",
3290                    src_idx, dst_idx
3291                );
3292                Ok(0)
3293            }
3294            "memory_replay" => {
3295                // Replay mapping: semantic only, no physical synapses
3296                use tracing::trace;
3297                trace!(
3298                    target: "feagi-bdu",
3299                    "Memory replay morphology: {} -> {} (no physical synapses)",
3300                    src_idx, dst_idx
3301                );
3302                Ok(0)
3303            }
3304            "associative_memory" => {
3305                // Associative memory (bi-directional STDP) mapping:
3306                // If both ends are memory areas, create synapses between LIF twins to enable associations.
3307                // Otherwise, no initial synapses are created (STDP will update existing synapses).
3308                let src_area = self.cortical_areas.get(src_area_id).ok_or_else(|| {
3309                    crate::types::BduError::InvalidArea(format!(
3310                        "Source area not found: {}",
3311                        src_area_id
3312                    ))
3313                })?;
3314                let dst_area = self.cortical_areas.get(dst_area_id).ok_or_else(|| {
3315                    crate::types::BduError::InvalidArea(format!(
3316                        "Destination area not found: {}",
3317                        dst_area_id
3318                    ))
3319                })?;
3320
3321                if matches!(src_area.cortical_type, CorticalAreaType::Memory(_))
3322                    && matches!(dst_area.cortical_type, CorticalAreaType::Memory(_))
3323                {
3324                    let src_dimensions = (
3325                        src_area.dimensions.width as usize,
3326                        src_area.dimensions.height as usize,
3327                        src_area.dimensions.depth as usize,
3328                    );
3329                    let dst_dimensions = (
3330                        dst_area.dimensions.width as usize,
3331                        dst_area.dimensions.height as usize,
3332                        dst_area.dimensions.depth as usize,
3333                    );
3334                    use crate::connectivity::core_morphologies::apply_projector_morphology_with_dimensions;
3335                    let count = apply_projector_morphology_with_dimensions(
3336                        npu,
3337                        src_idx,
3338                        dst_idx,
3339                        src_dimensions,
3340                        dst_dimensions,
3341                        None,
3342                        None,
3343                        weight,
3344                        psp,
3345                        synapse_attractivity,
3346                        synapse_type,
3347                        SYNAPSE_EDGE_ASSOCIATIVE_MEMORY,
3348                        delay_bursts,
3349                    )?;
3350                    npu.rebuild_synapse_index();
3351                    Ok(count as usize)
3352                } else {
3353                    Ok(0)
3354                }
3355            }
3356            "block_to_block" => {
3357                tracing::warn!(
3358                    target: "feagi-bdu",
3359                    "🔍 DEBUG apply_function_morphology: block_to_block case reached with src_idx={}, dst_idx={}",
3360                    src_idx, dst_idx
3361                );
3362                // Get dimensions from cortical areas (no neuron scanning!)
3363                let src_area = self.cortical_areas.get(src_area_id).ok_or_else(|| {
3364                    crate::types::BduError::InvalidArea(format!(
3365                        "Source area not found: {}",
3366                        src_area_id
3367                    ))
3368                })?;
3369                let dst_area = self.cortical_areas.get(dst_area_id).ok_or_else(|| {
3370                    crate::types::BduError::InvalidArea(format!(
3371                        "Destination area not found: {}",
3372                        dst_area_id
3373                    ))
3374                })?;
3375
3376                let src_dimensions = (
3377                    src_area.dimensions.width as usize,
3378                    src_area.dimensions.height as usize,
3379                    src_area.dimensions.depth as usize,
3380                );
3381                let dst_dimensions = (
3382                    dst_area.dimensions.width as usize,
3383                    dst_area.dimensions.height as usize,
3384                    dst_area.dimensions.depth as usize,
3385                );
3386
3387                // Extract scalar from rule (morphology_scalar)
3388                let scalar = if let Some(obj) = rule.as_object() {
3389                    // Object format: get from morphology_scalar array
3390                    if let Some(scalar_arr) =
3391                        obj.get("morphology_scalar").and_then(|v| v.as_array())
3392                    {
3393                        // Use first element as scalar (or default to 1)
3394                        scalar_arr.first().and_then(|v| v.as_i64()).unwrap_or(1) as u32
3395                    } else {
3396                        1 // @architecture:acceptable - default scalar
3397                    }
3398                } else if let Some(arr) = rule.as_array() {
3399                    // Array format: [morphology_id, scalar, multiplier, ...]
3400                    arr.get(1).and_then(|v| v.as_i64()).unwrap_or(1) as u32
3401                } else {
3402                    1 // @architecture:acceptable - default scalar
3403                };
3404
3405                // CRITICAL: Do NOT call get_neurons_in_cortical_area to check neuron count!
3406                // Use dimensions to estimate: if area is large, use batched version
3407                let estimated_neurons = src_dimensions.0 * src_dimensions.1 * src_dimensions.2;
3408                let count = if estimated_neurons > 100_000 {
3409                    // Release lock and use batched version
3410                    let _ = npu;
3411
3412                    crate::connectivity::synaptogenesis::apply_block_connection_morphology_batched(
3413                        npu_arc,
3414                        src_idx,
3415                        dst_idx,
3416                        src_dimensions,
3417                        dst_dimensions,
3418                        scalar, // scaling_factor
3419                        weight,
3420                        psp,
3421                        synapse_attractivity,
3422                        synapse_type,
3423                        delay_bursts,
3424                    )? as usize
3425                } else {
3426                    // Small area: use regular version (faster for small counts)
3427                    tracing::warn!(
3428                        target: "feagi-bdu",
3429                        "🔍 DEBUG connectome_manager: Calling apply_block_connection_morphology with src_idx={}, dst_idx={}, src_dim={:?}, dst_dim={:?}",
3430                        src_idx, dst_idx, src_dimensions, dst_dimensions
3431                    );
3432                    let count =
3433                        crate::connectivity::synaptogenesis::apply_block_connection_morphology(
3434                            npu,
3435                            src_idx,
3436                            dst_idx,
3437                            src_dimensions,
3438                            dst_dimensions,
3439                            scalar, // scaling_factor
3440                            weight,
3441                            psp,
3442                            synapse_attractivity,
3443                            synapse_type,
3444                            delay_bursts,
3445                        )? as usize;
3446                    tracing::warn!(
3447                        target: "feagi-bdu",
3448                        "🔍 DEBUG connectome_manager: apply_block_connection_morphology returned count={}",
3449                        count
3450                    );
3451                    // Rebuild synapse index while we still have the lock
3452                    if count > 0 {
3453                        npu.rebuild_synapse_index();
3454                    }
3455                    count
3456                };
3457
3458                // Ensure the propagation engine sees the newly created synapses immediately (batched version only)
3459                if count > 0 && estimated_neurons > 100_000 {
3460                    let mut npu_lock = npu_arc.lock().unwrap();
3461                    npu_lock.rebuild_synapse_index();
3462                }
3463
3464                Ok(count)
3465            }
3466            "bitmask_encoder_x" | "bitmask_encoder_y" | "bitmask_encoder_z"
3467            | "bitmask_decoder_x" | "bitmask_decoder_y" | "bitmask_decoder_z" => {
3468                let src_area = self.cortical_areas.get(src_area_id).ok_or_else(|| {
3469                    crate::types::BduError::InvalidArea(format!(
3470                        "Source area not found: {}",
3471                        src_area_id
3472                    ))
3473                })?;
3474                let dst_area = self.cortical_areas.get(dst_area_id).ok_or_else(|| {
3475                    crate::types::BduError::InvalidArea(format!(
3476                        "Destination area not found: {}",
3477                        dst_area_id
3478                    ))
3479                })?;
3480
3481                let src_dimensions = (
3482                    src_area.dimensions.width as usize,
3483                    src_area.dimensions.height as usize,
3484                    src_area.dimensions.depth as usize,
3485                );
3486                let dst_dimensions = (
3487                    dst_area.dimensions.width as usize,
3488                    dst_area.dimensions.height as usize,
3489                    dst_area.dimensions.depth as usize,
3490                );
3491
3492                let (axis, mode) = match morphology_id {
3493                    "bitmask_encoder_x" => (
3494                        crate::connectivity::core_morphologies::BitmaskAxis::X,
3495                        crate::connectivity::core_morphologies::BitmaskMode::Encoder,
3496                    ),
3497                    "bitmask_encoder_y" => (
3498                        crate::connectivity::core_morphologies::BitmaskAxis::Y,
3499                        crate::connectivity::core_morphologies::BitmaskMode::Encoder,
3500                    ),
3501                    "bitmask_encoder_z" => (
3502                        crate::connectivity::core_morphologies::BitmaskAxis::Z,
3503                        crate::connectivity::core_morphologies::BitmaskMode::Encoder,
3504                    ),
3505                    "bitmask_decoder_x" => (
3506                        crate::connectivity::core_morphologies::BitmaskAxis::X,
3507                        crate::connectivity::core_morphologies::BitmaskMode::Decoder,
3508                    ),
3509                    "bitmask_decoder_y" => (
3510                        crate::connectivity::core_morphologies::BitmaskAxis::Y,
3511                        crate::connectivity::core_morphologies::BitmaskMode::Decoder,
3512                    ),
3513                    "bitmask_decoder_z" => (
3514                        crate::connectivity::core_morphologies::BitmaskAxis::Z,
3515                        crate::connectivity::core_morphologies::BitmaskMode::Decoder,
3516                    ),
3517                    _ => unreachable!("matched bitmask morphology above"),
3518                };
3519
3520                let count =
3521                    crate::connectivity::core_morphologies::apply_bitmask_morphology_with_dimensions(
3522                        npu,
3523                        src_idx,
3524                        dst_idx,
3525                        src_dimensions,
3526                        dst_dimensions,
3527                        axis,
3528                        mode,
3529                        weight,
3530                        psp,
3531                        synapse_attractivity,
3532                        synapse_type,
3533                        delay_bursts,
3534                    )?;
3535                if count > 0 {
3536                    npu.rebuild_synapse_index();
3537                }
3538                Ok(count as usize)
3539            }
3540            "sweeper" => {
3541                let dst_area = self.cortical_areas.get(dst_area_id).ok_or_else(|| {
3542                    crate::types::BduError::InvalidArea(format!(
3543                        "Destination area not found: {}",
3544                        dst_area_id
3545                    ))
3546                })?;
3547                let dst_dimensions = (
3548                    dst_area.dimensions.width as usize,
3549                    dst_area.dimensions.height as usize,
3550                    dst_area.dimensions.depth as usize,
3551                );
3552
3553                let count =
3554                    crate::connectivity::core_morphologies::apply_sweeper_morphology_with_dimensions(
3555                        npu,
3556                        src_idx,
3557                        dst_idx,
3558                        dst_dimensions,
3559                        weight,
3560                        psp,
3561                        synapse_attractivity,
3562                        synapse_type,
3563                        delay_bursts,
3564                    )?;
3565                if count > 0 {
3566                    npu.rebuild_synapse_index();
3567                }
3568                Ok(count as usize)
3569            }
3570            "last_to_first" => {
3571                let src_area = self.cortical_areas.get(src_area_id).ok_or_else(|| {
3572                    crate::types::BduError::InvalidArea(format!(
3573                        "Source area not found: {}",
3574                        src_area_id
3575                    ))
3576                })?;
3577                let dst_area = self.cortical_areas.get(dst_area_id).ok_or_else(|| {
3578                    crate::types::BduError::InvalidArea(format!(
3579                        "Destination area not found: {}",
3580                        dst_area_id
3581                    ))
3582                })?;
3583                let src_dimensions = (
3584                    src_area.dimensions.width as usize,
3585                    src_area.dimensions.height as usize,
3586                    src_area.dimensions.depth as usize,
3587                );
3588                let dst_dimensions = (
3589                    dst_area.dimensions.width as usize,
3590                    dst_area.dimensions.height as usize,
3591                    dst_area.dimensions.depth as usize,
3592                );
3593
3594                let count = crate::connectivity::core_morphologies::apply_last_to_first_morphology_with_dimensions(
3595                    npu,
3596                    src_idx,
3597                    dst_idx,
3598                    src_dimensions,
3599                    dst_dimensions,
3600                    weight,
3601                    psp,
3602                    synapse_attractivity,
3603                    synapse_type,
3604                    delay_bursts,
3605                )?;
3606                if count > 0 {
3607                    npu.rebuild_synapse_index();
3608                }
3609                Ok(count as usize)
3610            }
3611            "first_to_last" => {
3612                let src_area = self.cortical_areas.get(src_area_id).ok_or_else(|| {
3613                    crate::types::BduError::InvalidArea(format!(
3614                        "Source area not found: {}",
3615                        src_area_id
3616                    ))
3617                })?;
3618                let dst_area = self.cortical_areas.get(dst_area_id).ok_or_else(|| {
3619                    crate::types::BduError::InvalidArea(format!(
3620                        "Destination area not found: {}",
3621                        dst_area_id
3622                    ))
3623                })?;
3624                let src_dimensions = (
3625                    src_area.dimensions.width as usize,
3626                    src_area.dimensions.height as usize,
3627                    src_area.dimensions.depth as usize,
3628                );
3629                let dst_dimensions = (
3630                    dst_area.dimensions.width as usize,
3631                    dst_area.dimensions.height as usize,
3632                    dst_area.dimensions.depth as usize,
3633                );
3634
3635                let count = crate::connectivity::core_morphologies::apply_first_to_last_morphology_with_dimensions(
3636                    npu,
3637                    src_idx,
3638                    dst_idx,
3639                    src_dimensions,
3640                    dst_dimensions,
3641                    weight,
3642                    psp,
3643                    synapse_attractivity,
3644                    synapse_type,
3645                    delay_bursts,
3646                )?;
3647                if count > 0 {
3648                    npu.rebuild_synapse_index();
3649                }
3650                Ok(count as usize)
3651            }
3652            "rotator_z" => {
3653                let src_area = self.cortical_areas.get(src_area_id).ok_or_else(|| {
3654                    crate::types::BduError::InvalidArea(format!(
3655                        "Source area not found: {}",
3656                        src_area_id
3657                    ))
3658                })?;
3659                let dst_area = self.cortical_areas.get(dst_area_id).ok_or_else(|| {
3660                    crate::types::BduError::InvalidArea(format!(
3661                        "Destination area not found: {}",
3662                        dst_area_id
3663                    ))
3664                })?;
3665                let src_dimensions = (
3666                    src_area.dimensions.width as usize,
3667                    src_area.dimensions.height as usize,
3668                    src_area.dimensions.depth as usize,
3669                );
3670                let dst_dimensions = (
3671                    dst_area.dimensions.width as usize,
3672                    dst_area.dimensions.height as usize,
3673                    dst_area.dimensions.depth as usize,
3674                );
3675
3676                let count = crate::connectivity::core_morphologies::apply_rotator_z_morphology_with_dimensions(
3677                    npu,
3678                    src_idx,
3679                    dst_idx,
3680                    src_dimensions,
3681                    dst_dimensions,
3682                    weight,
3683                    psp,
3684                    synapse_attractivity,
3685                    synapse_type,
3686                    delay_bursts,
3687                )?;
3688                if count > 0 {
3689                    npu.rebuild_synapse_index();
3690                }
3691                Ok(count as usize)
3692            }
3693            _ => {
3694                // Other function morphologies not yet implemented
3695                // NOTE: To add a new function-type morphology, add a case here
3696                use tracing::debug;
3697                debug!(target: "feagi-bdu", "Function morphology {} not yet implemented", morphology_id);
3698                Ok(0)
3699            }
3700        }
3701    }
3702
3703    /// Apply a single morphology rule
3704    fn apply_single_morphology_rule(
3705        &mut self,
3706        src_area_id: &CorticalID,
3707        dst_area_id: &CorticalID,
3708        rule: &serde_json::Value,
3709    ) -> BduResult<usize> {
3710        // Extract morphology_id from rule (array or dict format)
3711        let morphology_id = if let Some(arr) = rule.as_array() {
3712            arr.first().and_then(|v| v.as_str()).unwrap_or("")
3713        } else if let Some(obj) = rule.as_object() {
3714            obj.get("morphology_id")
3715                .and_then(|v| v.as_str())
3716                .unwrap_or("")
3717        } else {
3718            return Ok(0);
3719        };
3720
3721        if morphology_id.is_empty() {
3722            return Ok(0);
3723        }
3724
3725        // Get morphology from registry
3726        let morphology = self.morphology_registry.get(morphology_id).ok_or_else(|| {
3727            crate::types::BduError::InvalidMorphology(format!(
3728                "Morphology not found: {}",
3729                morphology_id
3730            ))
3731        })?;
3732
3733        // Convert area IDs to cortical indices (required by NPU functions)
3734        let src_idx = self.cortical_id_to_idx.get(src_area_id).ok_or_else(|| {
3735            crate::types::BduError::InvalidArea(format!(
3736                "Source area ID not found: {}",
3737                src_area_id
3738            ))
3739        })?;
3740        let dst_idx = self.cortical_id_to_idx.get(dst_area_id).ok_or_else(|| {
3741            crate::types::BduError::InvalidArea(format!(
3742                "Destination area ID not found: {}",
3743                dst_area_id
3744            ))
3745        })?;
3746
3747        // Apply morphology based on type
3748        if let Some(ref npu_arc) = self.npu {
3749            let lock_start = std::time::Instant::now();
3750            let mut npu = npu_arc.lock().unwrap();
3751            let lock_wait = lock_start.elapsed();
3752            tracing::debug!(
3753                target: "feagi-bdu",
3754                "[NPU-LOCK] synaptogenesis lock wait {:.2}ms for {} -> {} (morphology={})",
3755                lock_wait.as_secs_f64() * 1000.0,
3756                src_area_id,
3757                dst_area_id,
3758                morphology_id
3759            );
3760
3761            let (weight, psp, synapse_type, delay_bursts) =
3762                self.resolve_synapse_params_for_rule(src_area_id, rule)?;
3763
3764            // Extract synapse_attractivity from rule (probability 0-100)
3765            let synapse_attractivity = if let Some(obj) = rule.as_object() {
3766                obj.get("synapse_attractivity")
3767                    .and_then(|v| v.as_u64())
3768                    .unwrap_or(100) as u8
3769            } else {
3770                100 // @architecture:acceptable - default to always create when not specified
3771            };
3772
3773            match morphology.morphology_type {
3774                feagi_evolutionary::MorphologyType::Functions => {
3775                    tracing::debug!(
3776                        target: "feagi-bdu",
3777                        "apply_single_morphology_rule: Functions type, morphology_id={}, calling apply_function_morphology",
3778                        morphology_id
3779                    );
3780                    // Function-based morphologies (projector, memory, block_to_block, etc.)
3781                    // Delegate to helper function to consolidate all function-type logic
3782                    self.apply_function_morphology(
3783                        morphology_id,
3784                        rule,
3785                        npu_arc,
3786                        &mut npu,
3787                        src_area_id,
3788                        dst_area_id,
3789                        *src_idx,
3790                        *dst_idx,
3791                        weight,
3792                        psp,
3793                        synapse_attractivity,
3794                        synapse_type,
3795                        delay_bursts,
3796                    )
3797                }
3798                feagi_evolutionary::MorphologyType::Vectors => {
3799                    use crate::connectivity::synaptogenesis::apply_vectors_morphology_with_dimensions;
3800
3801                    // Get dimensions from cortical areas (no neuron scanning!)
3802                    let dst_area = self.cortical_areas.get(dst_area_id).ok_or_else(|| {
3803                        crate::types::BduError::InvalidArea(format!(
3804                            "Destination area not found: {}",
3805                            dst_area_id
3806                        ))
3807                    })?;
3808
3809                    let dst_dimensions = (
3810                        dst_area.dimensions.width as usize,
3811                        dst_area.dimensions.height as usize,
3812                        dst_area.dimensions.depth as usize,
3813                    );
3814
3815                    if let feagi_evolutionary::MorphologyParameters::Vectors { ref vectors } =
3816                        morphology.parameters
3817                    {
3818                        // Convert Vec<[i32; 3]> to Vec<(i32, i32, i32)>
3819                        let vectors_tuples: Vec<(i32, i32, i32)> =
3820                            vectors.iter().map(|v| (v[0], v[1], v[2])).collect();
3821
3822                        let count = apply_vectors_morphology_with_dimensions(
3823                            &mut npu,
3824                            *src_idx,
3825                            *dst_idx,
3826                            vectors_tuples,
3827                            dst_dimensions,
3828                            weight,               // From rule, not hardcoded
3829                            psp,                  // PSP from source area, NOT hardcoded!
3830                            synapse_attractivity, // From rule, not hardcoded
3831                            synapse_type,
3832                            delay_bursts,
3833                        )?;
3834                        // Ensure the propagation engine sees the newly created synapses immediately,
3835                        // and avoid a second outer NPU mutex acquisition later in the mapping update path.
3836                        npu.rebuild_synapse_index();
3837                        Ok(count as usize)
3838                    } else {
3839                        Ok(0)
3840                    }
3841                }
3842                feagi_evolutionary::MorphologyType::Patterns => {
3843                    use crate::connectivity::core_morphologies::apply_patterns_morphology;
3844                    use crate::connectivity::rules::patterns::{
3845                        Pattern3D, PatternElement as RulePatternElement,
3846                    };
3847                    use feagi_evolutionary::PatternElement as EvoPatternElement;
3848
3849                    let feagi_evolutionary::MorphologyParameters::Patterns { ref patterns } =
3850                        morphology.parameters
3851                    else {
3852                        return Ok(0);
3853                    };
3854
3855                    let convert_element =
3856                        |element: &EvoPatternElement|
3857                         -> crate::types::BduResult<RulePatternElement> {
3858                            match element {
3859                                EvoPatternElement::Value(value) => {
3860                                    if *value < 0 {
3861                                        return Err(crate::types::BduError::InvalidMorphology(
3862                                            format!(
3863                                                "Pattern morphology {} contains negative voxel coordinate {}",
3864                                                morphology_id, value
3865                                            ),
3866                                        ));
3867                                    }
3868                                    Ok(RulePatternElement::Exact(*value))
3869                                }
3870                                EvoPatternElement::Wildcard => Ok(RulePatternElement::Wildcard),
3871                                EvoPatternElement::Skip => Ok(RulePatternElement::Skip),
3872                                EvoPatternElement::Exclude => Ok(RulePatternElement::Exclude),
3873                                EvoPatternElement::DirectionPositive => {
3874                                    Ok(RulePatternElement::DirectionExclusive(
3875                                        crate::connectivity::rules::patterns::Direction::Positive,
3876                                    ))
3877                                }
3878                                EvoPatternElement::DirectionNegative => {
3879                                    Ok(RulePatternElement::DirectionExclusive(
3880                                        crate::connectivity::rules::patterns::Direction::Negative,
3881                                    ))
3882                                }
3883                                EvoPatternElement::DirectionPositiveInclusive => {
3884                                    Ok(RulePatternElement::DirectionInclusive(
3885                                        crate::connectivity::rules::patterns::Direction::Positive,
3886                                    ))
3887                                }
3888                                EvoPatternElement::DirectionNegativeInclusive => {
3889                                    Ok(RulePatternElement::DirectionInclusive(
3890                                        crate::connectivity::rules::patterns::Direction::Negative,
3891                                    ))
3892                                }
3893                                EvoPatternElement::Offset(off) => {
3894                                    Ok(RulePatternElement::Offset(*off))
3895                                }
3896                                EvoPatternElement::Range(lo, hi) => {
3897                                    Ok(RulePatternElement::Range(*lo, *hi))
3898                                }
3899                            }
3900                        };
3901
3902                    let mut converted_patterns = Vec::with_capacity(patterns.len());
3903                    for pattern_pair in patterns {
3904                        if pattern_pair.len() != 2 {
3905                            return Err(crate::types::BduError::InvalidMorphology(format!(
3906                                "Pattern morphology {} must contain [src, dst] pairs",
3907                                morphology_id
3908                            )));
3909                        }
3910
3911                        let src_pattern = &pattern_pair[0];
3912                        let dst_pattern = &pattern_pair[1];
3913
3914                        if src_pattern.len() != 3 || dst_pattern.len() != 3 {
3915                            return Err(crate::types::BduError::InvalidMorphology(format!(
3916                                "Pattern morphology {} requires 3-axis patterns",
3917                                morphology_id
3918                            )));
3919                        }
3920
3921                        let src: Pattern3D = (
3922                            convert_element(&src_pattern[0])?,
3923                            convert_element(&src_pattern[1])?,
3924                            convert_element(&src_pattern[2])?,
3925                        );
3926                        let dst: Pattern3D = (
3927                            convert_element(&dst_pattern[0])?,
3928                            convert_element(&dst_pattern[1])?,
3929                            convert_element(&dst_pattern[2])?,
3930                        );
3931
3932                        converted_patterns.push((src, dst));
3933                    }
3934
3935                    let count = apply_patterns_morphology(
3936                        &mut npu,
3937                        *src_idx,
3938                        *dst_idx,
3939                        converted_patterns,
3940                        weight,
3941                        psp,
3942                        synapse_attractivity,
3943                        synapse_type,
3944                        delay_bursts,
3945                    )?;
3946                    if count > 0 {
3947                        npu.rebuild_synapse_index();
3948                    }
3949                    Ok(count as usize)
3950                }
3951                feagi_evolutionary::MorphologyType::Composite => {
3952                    let feagi_evolutionary::MorphologyParameters::Composite { .. } =
3953                        morphology.parameters
3954                    else {
3955                        return Ok(0);
3956                    };
3957
3958                    if morphology_id != "tile" {
3959                        use tracing::debug;
3960                        debug!(
3961                            target: "feagi-bdu",
3962                            "Composite morphology {} not yet implemented",
3963                            morphology_id
3964                        );
3965                        return Ok(0);
3966                    }
3967
3968                    let src_area = self.cortical_areas.get(src_area_id).ok_or_else(|| {
3969                        crate::types::BduError::InvalidArea(format!(
3970                            "Source area not found: {}",
3971                            src_area_id
3972                        ))
3973                    })?;
3974                    let dst_area = self.cortical_areas.get(dst_area_id).ok_or_else(|| {
3975                        crate::types::BduError::InvalidArea(format!(
3976                            "Destination area not found: {}",
3977                            dst_area_id
3978                        ))
3979                    })?;
3980                    let src_dimensions = (
3981                        src_area.dimensions.width as usize,
3982                        src_area.dimensions.height as usize,
3983                        src_area.dimensions.depth as usize,
3984                    );
3985                    let dst_dimensions = (
3986                        dst_area.dimensions.width as usize,
3987                        dst_area.dimensions.height as usize,
3988                        dst_area.dimensions.depth as usize,
3989                    );
3990
3991                    let count =
3992                        crate::connectivity::core_morphologies::apply_tile_morphology_with_dimensions(
3993                            &mut npu,
3994                            *src_idx,
3995                            *dst_idx,
3996                            src_dimensions,
3997                            dst_dimensions,
3998                            weight,
3999                            psp,
4000                            synapse_attractivity,
4001                            synapse_type,
4002                            delay_bursts,
4003                        )?;
4004                    if count > 0 {
4005                        npu.rebuild_synapse_index();
4006                    }
4007                    Ok(count as usize)
4008                }
4009            }
4010        } else {
4011            Ok(0) // NPU not available
4012        }
4013    }
4014
4015    // ======================================================================
4016    // NPU Integration
4017    // ======================================================================
4018
4019    /// Set the NPU reference for neuron/synapse queries
4020    ///
4021    /// This should be called once during FEAGI initialization after the NPU is created.
4022    ///
4023    /// # Arguments
4024    ///
4025    /// * `npu` - Arc to the Rust NPU (wrapped in TracingMutex for automatic lock tracing)
4026    ///
4027    pub fn set_npu(
4028        &mut self,
4029        npu: Arc<feagi_npu_burst_engine::TracingMutex<feagi_npu_burst_engine::DynamicNPU>>,
4030    ) {
4031        self.npu = Some(Arc::clone(&npu));
4032        info!(target: "feagi-bdu","🔗 ConnectomeManager: NPU reference set");
4033
4034        // CRITICAL: Update State Manager with capacity values (from config, never changes)
4035        // This ensures health check endpoint can read capacity without acquiring NPU lock
4036        #[cfg(not(feature = "wasm"))]
4037        {
4038            use feagi_state_manager::StateManager;
4039            let state_manager = StateManager::instance();
4040            let state_manager = state_manager.read();
4041            let core_state = state_manager.get_core_state();
4042            // Capacity comes from config (set at initialization, never changes)
4043            core_state.set_neuron_capacity(self.config.max_neurons as u32);
4044            core_state.set_synapse_capacity(self.config.max_synapses as u32);
4045            info!(
4046                target: "feagi-bdu",
4047                "📊 Updated State Manager with capacity: {} neurons, {} synapses",
4048                self.config.max_neurons, self.config.max_synapses
4049            );
4050        }
4051
4052        // CRITICAL: Backfill cortical area registrations into NPU.
4053        //
4054        // Cortical areas can be created/loaded before the NPU is attached (startup ordering).
4055        // Those areas won't be registered via `add_cortical_area()` (it registers only if NPU is present),
4056        // which causes visualization encoding to fall back to "area_{idx}" and subsequently drop the area
4057        // (base64 decode fails), making BV appear to "miss" firing activity for that cortical area.
4058        let existing_area_count = self.cortical_id_to_idx.len();
4059        if existing_area_count > 0 {
4060            match npu.lock() {
4061                Ok(mut npu_lock) => {
4062                    for (cortical_id, cortical_idx) in self.cortical_id_to_idx.iter() {
4063                        npu_lock.register_cortical_area(*cortical_idx, cortical_id.as_base_64());
4064                    }
4065                    info!(
4066                        target: "feagi-bdu",
4067                        "🔁 Backfilled {} cortical area registrations into NPU",
4068                        existing_area_count
4069                    );
4070                }
4071                Err(e) => {
4072                    warn!(
4073                        target: "feagi-bdu",
4074                        "⚠️ Failed to lock NPU for cortical area backfill registration: {}",
4075                        e
4076                    );
4077                }
4078            }
4079        }
4080
4081        // Initialize cached stats immediately
4082        self.update_all_cached_stats();
4083        info!(target: "feagi-bdu","📊 Initialized cached stats: {} neurons, {} synapses",
4084            self.get_neuron_count(), self.get_synapse_count());
4085    }
4086
4087    /// Check if NPU is connected
4088    pub fn has_npu(&self) -> bool {
4089        self.npu.is_some()
4090    }
4091
4092    /// Get NPU reference (read-only access for queries)
4093    ///
4094    /// # Returns
4095    ///
4096    /// * `Option<&Arc<Mutex<RustNPU>>>` - Reference to NPU if connected
4097    ///
4098    pub fn get_npu(
4099        &self,
4100    ) -> Option<&Arc<feagi_npu_burst_engine::TracingMutex<feagi_npu_burst_engine::DynamicNPU>>>
4101    {
4102        self.npu.as_ref()
4103    }
4104
4105    /// Set the PlasticityExecutor reference (optional, only if plasticity feature enabled)
4106    /// The executor is passed as Arc<Mutex<dyn Any>> for feature-gating compatibility
4107    #[cfg(feature = "plasticity")]
4108    pub fn set_plasticity_executor(
4109        &mut self,
4110        executor: Arc<std::sync::Mutex<feagi_npu_plasticity::AsyncPlasticityExecutor>>,
4111    ) {
4112        if let Ok(mut exec) = executor.lock() {
4113            use feagi_npu_plasticity::executor::PlasticityExecutor;
4114            if !exec.is_running() {
4115                exec.start();
4116            }
4117        } else {
4118            warn!(target: "feagi-bdu", "⚠️ Failed to lock PlasticityExecutor for startup");
4119        }
4120        self.plasticity_executor = Some(executor);
4121        info!(target: "feagi-bdu", "🔗 ConnectomeManager: PlasticityExecutor reference set");
4122    }
4123
4124    /// Get the PlasticityExecutor reference (if plasticity feature enabled)
4125    #[cfg(feature = "plasticity")]
4126    pub fn get_plasticity_executor(
4127        &self,
4128    ) -> Option<&Arc<std::sync::Mutex<feagi_npu_plasticity::AsyncPlasticityExecutor>>> {
4129        self.plasticity_executor.as_ref()
4130    }
4131
4132    /// Get neuron capacity from config (lock-free, never acquires NPU lock)
4133    ///
4134    /// # Returns
4135    ///
4136    /// * `usize` - Maximum neuron capacity from config (single source of truth)
4137    ///
4138    /// # Performance
4139    ///
4140    /// This is a lock-free read from config that never blocks, even during burst processing.
4141    /// Capacity values are set at NPU initialization and never change.
4142    ///
4143    pub fn get_neuron_capacity(&self) -> usize {
4144        // CRITICAL: Read from config, NOT NPU - capacity never changes and should not acquire locks
4145        self.config.max_neurons
4146    }
4147
4148    /// Get synapse capacity from config (lock-free, never acquires NPU lock)
4149    ///
4150    /// # Returns
4151    ///
4152    /// * `usize` - Maximum synapse capacity from config (single source of truth)
4153    ///
4154    /// # Performance
4155    ///
4156    /// This is a lock-free read from config that never blocks, even during burst processing.
4157    /// Capacity values are set at NPU initialization and never change.
4158    ///
4159    pub fn get_synapse_capacity(&self) -> usize {
4160        // CRITICAL: Read from config, NOT NPU - capacity never changes and should not acquire locks
4161        self.config.max_synapses
4162    }
4163
4164    /// Update fatigue index based on utilization of neuron and synapse arrays
4165    ///
4166    /// Calculates fatigue index as max(regular_neuron_util%, memory_neuron_util%, synapse_util%)
4167    /// Applies hysteresis: triggers at 85%, clears at 80%
4168    /// Rate limited to max once per 2 seconds to protect against rapid changes
4169    ///
4170    /// # Safety
4171    ///
4172    /// This method is completely non-blocking and safe to call during genome loading.
4173    /// If StateManager is unavailable or locked, it will skip the calculation gracefully.
4174    ///
4175    /// # Returns
4176    ///
4177    /// * `Option<u8>` - New fatigue index (0-100) if calculation was performed, None if rate limited or StateManager unavailable
4178    pub fn update_fatigue_index(&self) -> Option<u8> {
4179        // Rate limiting: max once per 2 seconds
4180        let mut last_calc = match self.last_fatigue_calculation.lock() {
4181            Ok(guard) => guard,
4182            Err(_) => return None, // Lock poisoned, skip calculation
4183        };
4184
4185        let now = std::time::Instant::now();
4186        if now.duration_since(*last_calc).as_secs() < 2 {
4187            return None; // Rate limited
4188        }
4189        *last_calc = now;
4190        drop(last_calc);
4191
4192        // Get regular neuron utilization
4193        let regular_neuron_count = self.get_neuron_count();
4194        let regular_neuron_capacity = self.get_neuron_capacity();
4195        let regular_neuron_util = if regular_neuron_capacity > 0 {
4196            ((regular_neuron_count as f64 / regular_neuron_capacity as f64) * 100.0).round() as u8
4197        } else {
4198            0
4199        };
4200
4201        // Get memory neuron utilization from state manager
4202        // Use try_read() to avoid blocking during neurogenesis
4203        // If StateManager singleton initialization fails or is locked, skip calculation entirely
4204        let memory_neuron_util = match StateManager::instance().try_read() {
4205            Some(state_manager) => state_manager.get_core_state().get_memory_neuron_util(),
4206            None => {
4207                // StateManager is locked or not ready - skip fatigue calculation
4208                return None;
4209            }
4210        };
4211
4212        // Get synapse utilization
4213        let synapse_count = self.get_synapse_count();
4214        let synapse_capacity = self.get_synapse_capacity();
4215        let synapse_util = if synapse_capacity > 0 {
4216            ((synapse_count as f64 / synapse_capacity as f64) * 100.0).round() as u8
4217        } else {
4218            0
4219        };
4220
4221        // Calculate fatigue index as max of all utilizations
4222        let fatigue_index = regular_neuron_util
4223            .max(memory_neuron_util)
4224            .max(synapse_util);
4225
4226        // Apply hysteresis: trigger at 85%, clear at 80%
4227        let current_fatigue_active = {
4228            // Try to read current state - if unavailable, assume false
4229            StateManager::instance()
4230                .try_read()
4231                .map(|m| m.get_core_state().is_fatigue_active())
4232                .unwrap_or(false)
4233        };
4234
4235        let new_fatigue_active = if fatigue_index >= 85 {
4236            true
4237        } else if fatigue_index < 80 {
4238            false
4239        } else {
4240            current_fatigue_active // Keep current state in hysteresis zone
4241        };
4242
4243        // Update state manager with all values
4244        // Use try_write() to avoid blocking during neurogenesis
4245        // If StateManager is unavailable, skip update (non-blocking)
4246        if let Some(state_manager) = StateManager::instance().try_write() {
4247            let core_state = state_manager.get_core_state();
4248            core_state.set_fatigue_index(fatigue_index);
4249            core_state.set_fatigue_active(new_fatigue_active);
4250            core_state.set_regular_neuron_util(regular_neuron_util);
4251            core_state.set_memory_neuron_util(memory_neuron_util);
4252            core_state.set_synapse_util(synapse_util);
4253        } else {
4254            // StateManager is locked or not ready - skip update (non-blocking)
4255            trace!(target: "feagi-bdu", "[FATIGUE] StateManager unavailable, skipping update");
4256        }
4257
4258        // Update NPU's atomic boolean
4259        if let Some(ref npu) = self.npu {
4260            if let Ok(mut npu_lock) = npu.lock() {
4261                npu_lock.set_fatigue_active(new_fatigue_active);
4262            }
4263        }
4264
4265        trace!(
4266            target: "feagi-bdu",
4267            "[FATIGUE] Index={}, Active={}, Regular={}%, Memory={}%, Synapse={}%",
4268            fatigue_index, new_fatigue_active, regular_neuron_util, memory_neuron_util, synapse_util
4269        );
4270
4271        Some(fatigue_index)
4272    }
4273
4274    // ======================================================================
4275    // Neuron/Synapse Creation Methods (Delegates to NPU)
4276    // ======================================================================
4277
4278    /// Create neurons for a cortical area
4279    ///
4280    /// This delegates to the NPU's optimized batch creation function.
4281    ///
4282    /// # Arguments
4283    ///
4284    /// * `cortical_id` - Cortical area ID (6-character string)
4285    ///
4286    /// # Returns
4287    ///
4288    /// Number of neurons created
4289    ///
4290    pub fn create_neurons_for_area(&mut self, cortical_id: &CorticalID) -> BduResult<u32> {
4291        // Get cortical area
4292        let area = self
4293            .cortical_areas
4294            .get(cortical_id)
4295            .ok_or_else(|| {
4296                BduError::InvalidArea(format!("Cortical area {} not found", cortical_id))
4297            })?
4298            .clone();
4299
4300        // Get cortical index
4301        let cortical_idx = self.cortical_id_to_idx.get(cortical_id).ok_or_else(|| {
4302            BduError::InvalidArea(format!("No index for cortical area {}", cortical_id))
4303        })?;
4304
4305        // Get NPU
4306        let npu = self
4307            .npu
4308            .as_ref()
4309            .ok_or_else(|| BduError::Internal("NPU not connected".to_string()))?;
4310
4311        // Extract neural parameters from area properties using CorticalAreaExt trait
4312        // This ensures consistent defaults across the codebase
4313        use crate::models::CorticalAreaExt;
4314        let per_voxel_cnt = area.neurons_per_voxel();
4315        let firing_threshold = area.firing_threshold();
4316        let firing_threshold_increment_x = area.firing_threshold_increment_x();
4317        let firing_threshold_increment_y = area.firing_threshold_increment_y();
4318        let firing_threshold_increment_z = area.firing_threshold_increment_z();
4319        // SIMD-friendly encoding: 0.0 means no limit, convert to MAX
4320        let firing_threshold_limit_raw = area.firing_threshold_limit();
4321        let firing_threshold_limit = if firing_threshold_limit_raw == 0.0 {
4322            f32::MAX // SIMD-friendly encoding: MAX = no limit
4323        } else {
4324            firing_threshold_limit_raw
4325        };
4326
4327        // DEBUG: Log the increment values
4328        if firing_threshold_increment_x != 0.0
4329            || firing_threshold_increment_y != 0.0
4330            || firing_threshold_increment_z != 0.0
4331        {
4332            info!(
4333                target: "feagi-bdu",
4334                "🔍 [DEBUG] Area {}: firing_threshold_increment = [{}, {}, {}]",
4335                cortical_id.as_base_64(),
4336                firing_threshold_increment_x,
4337                firing_threshold_increment_y,
4338                firing_threshold_increment_z
4339            );
4340        } else {
4341            // Check if properties exist but are just 0
4342            if area.properties.contains_key("firing_threshold_increment_x")
4343                || area.properties.contains_key("firing_threshold_increment_y")
4344                || area.properties.contains_key("firing_threshold_increment_z")
4345            {
4346                info!(
4347                    target: "feagi-bdu",
4348                    "🔍 [DEBUG] Area {}: INCREMENT PROPERTIES FOUND: x={:?}, y={:?}, z={:?}",
4349                    cortical_id.as_base_64(),
4350                    area.properties.get("firing_threshold_increment_x"),
4351                    area.properties.get("firing_threshold_increment_y"),
4352                    area.properties.get("firing_threshold_increment_z")
4353                );
4354            }
4355        }
4356
4357        let leak_coefficient = area.leak_coefficient();
4358        let excitability = area.neuron_excitability();
4359        let refractory_period = area.refractory_period();
4360        // SIMD-friendly encoding: 0 means no limit, convert to MAX
4361        let consecutive_fire_limit_raw = area.consecutive_fire_count() as u16;
4362        let consecutive_fire_limit = if consecutive_fire_limit_raw == 0 {
4363            u16::MAX // SIMD-friendly encoding: MAX = no limit
4364        } else {
4365            consecutive_fire_limit_raw
4366        };
4367        let snooze_length = area.snooze_period();
4368        let mp_charge_accumulation = area.mp_charge_accumulation();
4369
4370        // Calculate expected neuron count for logging
4371        let voxels = area.dimensions.width as usize
4372            * area.dimensions.height as usize
4373            * area.dimensions.depth as usize;
4374        let expected_neurons = voxels * per_voxel_cnt as usize;
4375
4376        trace!(
4377            target: "feagi-bdu",
4378            "Creating neurons for area {}: {}x{}x{} voxels × {} neurons/voxel = {} total neurons",
4379            cortical_id.as_base_64(),
4380            area.dimensions.width,
4381            area.dimensions.height,
4382            area.dimensions.depth,
4383            per_voxel_cnt,
4384            expected_neurons
4385        );
4386
4387        // Call NPU to create neurons
4388        // NOTE: Cortical area should already be registered in NPU during corticogenesis
4389        // Scope the lock so it is released before the rate_modulated_leak block below, which
4390        // must take the same NPU mutex again (second lock while npu_lock lived = deadlock).
4391        let neuron_count: u32 = {
4392            let mut npu_lock = npu
4393                .lock()
4394                .map_err(|e| BduError::Internal(format!("Failed to lock NPU: {}", e)))?;
4395            npu_lock
4396                .create_cortical_area_neurons(
4397                    *cortical_idx,
4398                    area.dimensions.width,
4399                    area.dimensions.height,
4400                    area.dimensions.depth,
4401                    per_voxel_cnt,
4402                    firing_threshold,
4403                    firing_threshold_increment_x,
4404                    firing_threshold_increment_y,
4405                    firing_threshold_increment_z,
4406                    firing_threshold_limit,
4407                    leak_coefficient,
4408                    0.0, // resting_potential (LIF default)
4409                    0,   // neuron_type (excitatory)
4410                    refractory_period,
4411                    excitability,
4412                    consecutive_fire_limit,
4413                    snooze_length,
4414                    mp_charge_accumulation,
4415                )
4416                .map_err(|e| BduError::Internal(format!("NPU neuron creation failed: {}", e)))?
4417        };
4418
4419        trace!(
4420            target: "feagi-bdu",
4421            "Created {} neurons for area {} via NPU",
4422            neuron_count,
4423            cortical_id.as_base_64()
4424        );
4425
4426        // CRITICAL: Update per-area neuron count cache (lock-free for readers)
4427        // This allows healthcheck endpoints to read counts without NPU lock
4428        {
4429            let mut cache = self.cached_neuron_counts_per_area.write();
4430            cache
4431                .entry(*cortical_id)
4432                .or_insert_with(|| AtomicUsize::new(0))
4433                .store(neuron_count as usize, Ordering::Relaxed);
4434        }
4435
4436        // @cursor:critical-path - Keep BV-facing stats in StateManager.
4437        let state_manager = StateManager::instance();
4438        let state_manager = state_manager.read();
4439        state_manager
4440            .set_cortical_area_neuron_count(&cortical_id.as_base_64(), neuron_count as usize);
4441
4442        // Update total neuron count cache
4443        self.cached_neuron_count
4444            .fetch_add(neuron_count as usize, Ordering::Relaxed);
4445
4446        // CRITICAL: Update StateManager neuron count (for health_check endpoint)
4447        let state_manager = StateManager::instance();
4448        let state_manager = state_manager.read();
4449        let core_state = state_manager.get_core_state();
4450        core_state.add_neuron_count(neuron_count);
4451        core_state.add_regular_neuron_count(neuron_count);
4452
4453        // Opt-in homeostatic leak: register on NPU (cold pass only when enabled; see `neural/docs/rate_modulated_leak.md`).
4454        if let Some(npu) = &self.npu {
4455            if let Ok(mut npl) = npu.lock() {
4456                if let Some(v) = area.properties.get("rate_modulated_leak") {
4457                    use crate::models::CorticalAreaExt;
4458                    let idxs: Vec<usize> = npl
4459                        .get_neurons_in_cortical_area(*cortical_idx)
4460                        .into_iter()
4461                        .map(|id| id as usize)
4462                        .collect();
4463                    npl.sync_rate_modulated_leak_from_cortical_property(
4464                        *cortical_idx,
4465                        v,
4466                        area.leak_coefficient(),
4467                        idxs,
4468                    );
4469                } else {
4470                    npl.remove_rate_modulated_leak(*cortical_idx);
4471                }
4472            }
4473        }
4474
4475        // Trigger fatigue index recalculation after neuron creation
4476        // NOTE: Disabled during genome loading to prevent blocking
4477        // Fatigue calculation will be enabled after genome loading completes
4478        // if neuron_count > 0 {
4479        //     let _ = self.update_fatigue_index();
4480        // }
4481
4482        Ok(neuron_count)
4483    }
4484
4485    /// Add a single neuron to a cortical area
4486    ///
4487    /// # Arguments
4488    ///
4489    /// * `cortical_id` - Cortical area ID
4490    /// * `x` - X coordinate
4491    /// * `y` - Y coordinate
4492    /// * `z` - Z coordinate
4493    /// * `firing_threshold` - Firing threshold (minimum MP to fire)
4494    /// * `firing_threshold_limit` - Firing threshold limit (maximum MP to fire, 0 = no limit)
4495    /// * `leak_coefficient` - Leak coefficient
4496    /// * `resting_potential` - Resting membrane potential
4497    /// * `neuron_type` - Neuron type (0=excitatory, 1=inhibitory)
4498    /// * `refractory_period` - Refractory period
4499    /// * `excitability` - Excitability multiplier
4500    /// * `consecutive_fire_limit` - Maximum consecutive fires
4501    /// * `snooze_length` - Snooze duration after consecutive fire limit
4502    /// * `mp_charge_accumulation` - Whether membrane potential accumulates
4503    ///
4504    /// # Returns
4505    ///
4506    /// The newly created neuron ID
4507    ///
4508    #[allow(clippy::too_many_arguments)]
4509    pub fn add_neuron(
4510        &mut self,
4511        cortical_id: &CorticalID,
4512        x: u32,
4513        y: u32,
4514        z: u32,
4515        firing_threshold: f32,
4516        firing_threshold_limit: f32,
4517        leak_coefficient: f32,
4518        resting_potential: f32,
4519        neuron_type: u8,
4520        refractory_period: u16,
4521        excitability: f32,
4522        consecutive_fire_limit: u16,
4523        snooze_length: u16,
4524        mp_charge_accumulation: bool,
4525    ) -> BduResult<u64> {
4526        // Validate cortical area exists
4527        if !self.cortical_areas.contains_key(cortical_id) {
4528            return Err(BduError::InvalidArea(format!(
4529                "Cortical area {} not found",
4530                cortical_id
4531            )));
4532        }
4533
4534        let cortical_idx = *self
4535            .cortical_id_to_idx
4536            .get(cortical_id)
4537            .ok_or_else(|| BduError::InvalidArea(format!("No index for {}", cortical_id)))?;
4538
4539        // Get NPU
4540        let npu = self
4541            .npu
4542            .as_ref()
4543            .ok_or_else(|| BduError::Internal("NPU not connected".to_string()))?;
4544
4545        let mut npu_lock = npu
4546            .lock()
4547            .map_err(|e| BduError::Internal(format!("Failed to lock NPU: {}", e)))?;
4548
4549        // Add neuron via NPU
4550        let neuron_id = npu_lock
4551            .add_neuron(
4552                firing_threshold,
4553                firing_threshold_limit,
4554                leak_coefficient,
4555                resting_potential,
4556                neuron_type as i32,
4557                refractory_period,
4558                excitability,
4559                consecutive_fire_limit,
4560                snooze_length,
4561                mp_charge_accumulation,
4562                cortical_idx,
4563                x,
4564                y,
4565                z,
4566            )
4567            .map_err(|e| BduError::Internal(format!("Failed to add neuron: {}", e)))?;
4568
4569        trace!(
4570            target: "feagi-bdu",
4571            "Created neuron {} in area {} at ({}, {}, {})",
4572            neuron_id.0,
4573            cortical_id,
4574            x,
4575            y,
4576            z
4577        );
4578
4579        // CRITICAL: Update StateManager neuron count (for health_check endpoint)
4580        let state_manager = StateManager::instance();
4581        let state_manager = state_manager.read();
4582        let core_state = state_manager.get_core_state();
4583        core_state.add_neuron_count(1);
4584        core_state.add_regular_neuron_count(1);
4585        state_manager.add_cortical_area_neuron_count(&cortical_id.as_base_64(), 1);
4586
4587        Ok(neuron_id.0 as u64)
4588    }
4589
4590    /// Delete a neuron by ID
4591    ///
4592    /// # Arguments
4593    ///
4594    /// * `neuron_id` - Global neuron ID
4595    ///
4596    /// # Returns
4597    ///
4598    /// `true` if the neuron was deleted, `false` if it didn't exist
4599    ///
4600    pub fn delete_neuron(&mut self, neuron_id: u64) -> BduResult<bool> {
4601        // Get NPU
4602        let npu = self
4603            .npu
4604            .as_ref()
4605            .ok_or_else(|| BduError::Internal("NPU not connected".to_string()))?;
4606
4607        let mut npu_lock = npu
4608            .lock()
4609            .map_err(|e| BduError::Internal(format!("Failed to lock NPU: {}", e)))?;
4610
4611        let cortical_idx = npu_lock.get_neuron_cortical_area(neuron_id as u32);
4612        let cortical_id = cortical_idx.and_then(|idx| self.cortical_idx_to_id.get(&idx).cloned());
4613
4614        let deleted = npu_lock.delete_neuron(neuron_id as u32);
4615
4616        if deleted {
4617            trace!(target: "feagi-bdu", "Deleted neuron {}", neuron_id);
4618
4619            // CRITICAL: Update StateManager neuron count (for health_check endpoint)
4620            let state_manager = StateManager::instance();
4621            let state_manager = state_manager.read();
4622            let core_state = state_manager.get_core_state();
4623            core_state.subtract_neuron_count(1);
4624            core_state.subtract_regular_neuron_count(1);
4625            if let Some(cortical_id) = cortical_id {
4626                state_manager.subtract_cortical_area_neuron_count(&cortical_id.as_base_64(), 1);
4627            }
4628
4629            // Trigger fatigue index recalculation after neuron deletion
4630            // NOTE: Disabled during genome loading to prevent blocking
4631            // let _ = self.update_fatigue_index();
4632        }
4633
4634        Ok(deleted)
4635    }
4636
4637    /// Apply cortical mapping rules (dstmap) to create synapses
4638    ///
4639    /// This parses the destination mapping rules from a source area and
4640    /// creates synapses using the NPU's synaptogenesis functions.
4641    ///
4642    /// # Arguments
4643    ///
4644    /// * `src_cortical_id` - Source cortical area ID
4645    ///
4646    /// # Returns
4647    ///
4648    /// Number of synapses created
4649    ///
4650    pub fn apply_cortical_mapping(&mut self, src_cortical_id: &CorticalID) -> BduResult<u32> {
4651        // Get source area
4652        let src_area = self
4653            .cortical_areas
4654            .get(src_cortical_id)
4655            .ok_or_else(|| {
4656                BduError::InvalidArea(format!("Source area {} not found", src_cortical_id))
4657            })?
4658            .clone();
4659
4660        // Get dstmap from area properties
4661        let dstmap = match src_area.properties.get("cortical_mapping_dst") {
4662            Some(serde_json::Value::Object(map)) if !map.is_empty() => map,
4663            _ => return Ok(0), // No mappings
4664        };
4665
4666        let src_cortical_idx = *self
4667            .cortical_id_to_idx
4668            .get(src_cortical_id)
4669            .ok_or_else(|| BduError::InvalidArea(format!("No index for {}", src_cortical_id)))?;
4670
4671        let mut total_synapses = 0u32;
4672        let mut upstream_updates: Vec<(CorticalID, u32)> = Vec::new(); // Collect updates to apply later
4673
4674        // Process each destination area using the unified path
4675        for (dst_cortical_id_str, _rules) in dstmap {
4676            // Convert string to CorticalID
4677            let dst_cortical_id = match CorticalID::try_from_base_64(dst_cortical_id_str) {
4678                Ok(id) => id,
4679                Err(_) => {
4680                    warn!(target: "feagi-bdu","Invalid cortical ID format: {}, skipping", dst_cortical_id_str);
4681                    continue;
4682                }
4683            };
4684
4685            // Verify destination area exists
4686            if !self.cortical_id_to_idx.contains_key(&dst_cortical_id) {
4687                warn!(target: "feagi-bdu","Destination area {} not found, skipping", dst_cortical_id);
4688                continue;
4689            }
4690
4691            // Apply cortical mapping for this pair (handles STDP and all morphology rules)
4692            let synapse_count =
4693                self.apply_cortical_mapping_for_pair(src_cortical_id, &dst_cortical_id)?;
4694            total_synapses += synapse_count as u32;
4695
4696            // Queue upstream area update for ANY mapping (even if no synapses created)
4697            // This is critical for memory areas which have mappings but no physical synapses
4698            upstream_updates.push((dst_cortical_id, src_cortical_idx));
4699        }
4700
4701        // Apply all upstream area updates now that NPU borrows are complete
4702        for (dst_id, src_idx) in upstream_updates {
4703            self.add_upstream_area(&dst_id, src_idx);
4704        }
4705
4706        trace!(
4707            target: "feagi-bdu",
4708            "Created {} synapses for area {} via NPU",
4709            total_synapses,
4710            src_cortical_id
4711        );
4712
4713        // CRITICAL: Update per-area synapse count cache (lock-free for readers)
4714        // This allows healthcheck endpoints to read counts without NPU lock
4715        if total_synapses > 0 {
4716            let mut cache = self.cached_synapse_counts_per_area.write();
4717            cache
4718                .entry(*src_cortical_id)
4719                .or_insert_with(|| AtomicUsize::new(0))
4720                .fetch_add(total_synapses as usize, Ordering::Relaxed);
4721        }
4722
4723        // Update total synapse count cache
4724        self.cached_synapse_count
4725            .fetch_add(total_synapses as usize, Ordering::Relaxed);
4726
4727        // CRITICAL: Update StateManager synapse count (for health_check endpoint)
4728        if total_synapses > 0 {
4729            let state_manager = StateManager::instance();
4730            let state_manager = state_manager.read();
4731            let core_state = state_manager.get_core_state();
4732            core_state.add_synapse_count(total_synapses);
4733        }
4734
4735        Ok(total_synapses)
4736    }
4737
4738    // ======================================================================
4739    // Neuron Query Methods (Delegates to NPU)
4740    // ======================================================================
4741
4742    /// Check if a neuron exists
4743    ///
4744    /// # Arguments
4745    ///
4746    /// * `neuron_id` - The neuron ID to check
4747    ///
4748    /// # Returns
4749    ///
4750    /// `true` if the neuron exists in the NPU, `false` otherwise
4751    ///
4752    /// # Note
4753    ///
4754    /// Returns `false` if NPU is not connected
4755    ///
4756    pub fn has_neuron(&self, neuron_id: u64) -> bool {
4757        if let Some(ref npu) = self.npu {
4758            if let Ok(npu_lock) = npu.lock() {
4759                // Check if neuron exists AND is valid (not deleted)
4760                npu_lock.is_neuron_valid(neuron_id as u32)
4761            } else {
4762                false
4763            }
4764        } else {
4765            false
4766        }
4767    }
4768
4769    /// Get total number of active neurons (lock-free cached read with opportunistic update)
4770    ///
4771    /// # Returns
4772    ///
4773    /// The total number of neurons (from cache)
4774    ///
4775    /// # Performance
4776    ///
4777    /// This is a lock-free atomic read that never blocks, even during burst processing.
4778    /// Opportunistically updates cache if NPU is available (non-blocking try_lock).
4779    ///
4780    pub fn get_neuron_count(&self) -> usize {
4781        // Opportunistically update cache if NPU is available (non-blocking)
4782        if let Some(ref npu) = self.npu {
4783            if let Ok(npu_lock) = npu.try_lock() {
4784                let fresh_count = npu_lock.get_neuron_count();
4785                self.cached_neuron_count
4786                    .store(fresh_count, Ordering::Relaxed);
4787            }
4788            // If NPU is busy, just use cached value
4789        }
4790
4791        // Always return cached value (never blocks)
4792        self.cached_neuron_count.load(Ordering::Relaxed)
4793    }
4794
4795    /// Update the cached neuron count (explicit update)
4796    ///
4797    /// Use this if you want to force a cache update. Most callers should just
4798    /// use get_neuron_count() which updates opportunistically.
4799    ///
4800    pub fn update_cached_neuron_count(&self) {
4801        if let Some(ref npu) = self.npu {
4802            if let Ok(npu_lock) = npu.try_lock() {
4803                let count = npu_lock.get_neuron_count();
4804                self.cached_neuron_count.store(count, Ordering::Relaxed);
4805            }
4806        }
4807    }
4808
4809    /// Refresh cached neuron count for a single cortical area from the NPU.
4810    ///
4811    /// Returns the refreshed count if successful.
4812    pub fn refresh_neuron_count_for_area(&self, cortical_id: &CorticalID) -> Option<usize> {
4813        let npu = self.npu.as_ref()?;
4814        let cortical_idx = *self.cortical_id_to_idx.get(cortical_id)?;
4815        let npu_lock = npu.lock().ok()?;
4816        let count = npu_lock.get_neurons_in_cortical_area(cortical_idx).len();
4817        drop(npu_lock);
4818
4819        let mut cache = self.cached_neuron_counts_per_area.write();
4820        cache
4821            .entry(*cortical_id)
4822            .or_insert_with(|| AtomicUsize::new(0))
4823            .store(count, Ordering::Relaxed);
4824
4825        // @cursor:critical-path - Keep BV-facing stats in StateManager.
4826        let state_manager = StateManager::instance();
4827        let state_manager = state_manager.read();
4828        state_manager.set_cortical_area_neuron_count(&cortical_id.as_base_64(), count);
4829
4830        self.update_cached_neuron_count();
4831
4832        Some(count)
4833    }
4834
4835    /// Get total number of synapses (lock-free cached read with opportunistic update)
4836    ///
4837    /// # Returns
4838    ///
4839    /// The total number of synapses (from cache)
4840    ///
4841    /// # Performance
4842    ///
4843    /// This is a lock-free atomic read that never blocks, even during burst processing.
4844    /// Opportunistically updates cache if NPU is available (non-blocking try_lock).
4845    ///
4846    pub fn get_synapse_count(&self) -> usize {
4847        // Opportunistically update cache if NPU is available (non-blocking)
4848        if let Some(ref npu) = self.npu {
4849            if let Ok(npu_lock) = npu.try_lock() {
4850                let fresh_count = npu_lock.get_synapse_count();
4851                self.cached_synapse_count
4852                    .store(fresh_count, Ordering::Relaxed);
4853            }
4854            // If NPU is busy, just use cached value
4855        }
4856
4857        // Always return cached value (never blocks)
4858        self.cached_synapse_count.load(Ordering::Relaxed)
4859    }
4860
4861    /// Update the cached synapse count (explicit update)
4862    ///
4863    /// Use this if you want to force a cache update. Most callers should just
4864    /// use get_synapse_count() which updates opportunistically.
4865    ///
4866    pub fn update_cached_synapse_count(&self) {
4867        if let Some(ref npu) = self.npu {
4868            if let Ok(npu_lock) = npu.try_lock() {
4869                let count = npu_lock.get_synapse_count();
4870                self.cached_synapse_count.store(count, Ordering::Relaxed);
4871            }
4872        }
4873    }
4874
4875    /// Update all cached stats (neuron and synapse counts)
4876    ///
4877    /// This is called automatically when NPU is connected and can be called
4878    /// explicitly if you want to force a cache refresh.
4879    ///
4880    pub fn update_all_cached_stats(&self) {
4881        self.update_cached_neuron_count();
4882        self.update_cached_synapse_count();
4883    }
4884
4885    /// Get neuron coordinates (x, y, z)
4886    ///
4887    /// # Arguments
4888    ///
4889    /// * `neuron_id` - The neuron ID to query
4890    ///
4891    /// # Returns
4892    ///
4893    /// Coordinates as (x, y, z), or (0, 0, 0) if neuron doesn't exist or NPU not connected
4894    ///
4895    pub fn get_neuron_coordinates(&self, neuron_id: u64) -> (u32, u32, u32) {
4896        // Memory neurons live in the plasticity MemoryNeuronArray, not the NPU dense neuron array.
4897        // Do not take the NPU mutex here: synapse inspector paths (`peer_cortical_voxel_fields`)
4898        // resolve cortical idx via the plasticity lock first, then coordinates. The burst thread
4899        // holds NPU while notifying plasticity — taking NPU after plasticity would deadlock.
4900        #[cfg(feature = "plasticity")]
4901        {
4902            if feagi_npu_plasticity::NeuronIdManager::is_memory_neuron_id(neuron_id as u32) {
4903                return (0, 0, 0);
4904            }
4905        }
4906        if let Some(ref npu) = self.npu {
4907            if let Ok(npu_lock) = npu.lock() {
4908                npu_lock
4909                    .get_neuron_coordinates(neuron_id as u32)
4910                    .unwrap_or((0, 0, 0))
4911            } else {
4912                (0, 0, 0)
4913            }
4914        } else {
4915            (0, 0, 0)
4916        }
4917    }
4918
4919    /// Get the cortical area index for a neuron
4920    ///
4921    /// # Arguments
4922    ///
4923    /// * `neuron_id` - The neuron ID to query
4924    ///
4925    /// # Returns
4926    ///
4927    /// Cortical area index, or 0 if neuron doesn't exist or NPU not connected
4928    ///
4929    pub fn get_neuron_cortical_idx(&self, neuron_id: u64) -> u32 {
4930        self.get_neuron_cortical_idx_opt(neuron_id).unwrap_or(0)
4931    }
4932
4933    /// Cortical area index for a neuron, or `None` if the neuron slot is invalid / NPU unavailable.
4934    ///
4935    /// Memory neurons (global ids in `50_000_000..=99_999_999`) are not stored in the dense
4936    /// [`NeuronArray`] index space; their cortical membership is resolved via the plasticity
4937    /// [`MemoryNeuronArray`] when the plasticity feature is enabled.
4938    pub fn get_neuron_cortical_idx_opt(&self, neuron_id: u64) -> Option<u32> {
4939        #[cfg(feature = "plasticity")]
4940        {
4941            if feagi_npu_plasticity::NeuronIdManager::is_memory_neuron_id(neuron_id as u32) {
4942                return self.memory_neuron_cortical_idx_opt(neuron_id as u32);
4943            }
4944        }
4945        if let Some(ref npu) = self.npu {
4946            if let Ok(npu_lock) = npu.lock() {
4947                npu_lock.get_neuron_cortical_area(neuron_id as u32)
4948            } else {
4949                None
4950            }
4951        } else {
4952            None
4953        }
4954    }
4955
4956    /// Resolve cortical index for a memory-neuron global id through the plasticity executor.
4957    #[cfg(feature = "plasticity")]
4958    fn memory_neuron_cortical_idx_opt(&self, neuron_id: u32) -> Option<u32> {
4959        let exec = self.get_plasticity_executor()?;
4960        let guard = exec.lock().ok()?;
4961        guard
4962            .memory_neuron_detail(neuron_id)
4963            .map(|d| d.cortical_area_idx)
4964    }
4965
4966    /// Get all neuron IDs in a specific cortical area
4967    ///
4968    /// # Arguments
4969    ///
4970    /// * `cortical_id` - The cortical area ID (string)
4971    ///
4972    /// # Returns
4973    ///
4974    /// Vec of neuron IDs in the area, or empty vec if area doesn't exist or NPU not connected
4975    ///
4976    pub fn get_neurons_in_area(&self, cortical_id: &CorticalID) -> Vec<u64> {
4977        // Get cortical_idx from cortical_id
4978        let cortical_idx = match self.cortical_id_to_idx.get(cortical_id) {
4979            Some(idx) => *idx,
4980            None => return Vec::new(),
4981        };
4982
4983        if let Some(ref npu) = self.npu {
4984            if let Ok(npu_lock) = npu.lock() {
4985                // Convert Vec<u32> to Vec<u64>
4986                npu_lock
4987                    .get_neurons_in_cortical_area(cortical_idx)
4988                    .into_iter()
4989                    .map(|id| id as u64)
4990                    .collect()
4991            } else {
4992                Vec::new()
4993            }
4994        } else {
4995            Vec::new()
4996        }
4997    }
4998
4999    /// Get all outgoing synapses from a source neuron
5000    ///
5001    /// # Arguments
5002    ///
5003    /// * `source_neuron_id` - The source neuron ID
5004    ///
5005    /// # Returns
5006    ///
5007    /// Vec of (target_neuron_id, weight, psp, synapse_type), or empty if NPU not connected
5008    ///
5009    pub fn get_outgoing_synapses(&self, source_neuron_id: u64) -> Vec<(u32, f32, f32, u8)> {
5010        if let Some(ref npu) = self.npu {
5011            if let Ok(npu_lock) = npu.lock() {
5012                npu_lock.get_outgoing_synapses(source_neuron_id as u32)
5013            } else {
5014                Vec::new()
5015            }
5016        } else {
5017            Vec::new()
5018        }
5019    }
5020
5021    /// Get all incoming synapses to a target neuron
5022    ///
5023    /// # Arguments
5024    ///
5025    /// * `target_neuron_id` - The target neuron ID
5026    ///
5027    /// # Returns
5028    ///
5029    /// Vec of (source_neuron_id, weight, psp, synapse_type), or empty if NPU not connected
5030    ///
5031    pub fn get_incoming_synapses(&self, target_neuron_id: u64) -> Vec<(u32, f32, f32, u8)> {
5032        if let Some(ref npu) = self.npu {
5033            if let Ok(npu_lock) = npu.lock() {
5034                npu_lock.get_incoming_synapses(target_neuron_id as u32)
5035            } else {
5036                Vec::new()
5037            }
5038        } else {
5039            Vec::new()
5040        }
5041    }
5042
5043    /// Get neuron count for a specific cortical area
5044    ///
5045    /// # Arguments
5046    ///
5047    /// * `cortical_id` - The cortical area ID (string)
5048    ///
5049    /// # Returns
5050    ///
5051    /// Number of neurons in the area, or 0 if area doesn't exist or NPU not connected
5052    ///
5053    /// Get neuron count for a specific cortical area (lock-free cached read)
5054    ///
5055    /// # Arguments
5056    ///
5057    /// * `cortical_id` - The cortical area ID
5058    ///
5059    /// # Returns
5060    ///
5061    /// The number of neurons in the area (from cache, never blocks on NPU lock)
5062    ///
5063    /// # Performance
5064    ///
5065    /// This is a lock-free atomic read that never blocks, even during burst processing.
5066    /// Count is maintained in ConnectomeManager and updated when neurons are created/deleted.
5067    ///
5068    pub fn get_neuron_count_in_area(&self, cortical_id: &CorticalID) -> usize {
5069        // CRITICAL: Read from cache (lock-free) - never query NPU for healthcheck endpoints
5070        let cache = self.cached_neuron_counts_per_area.read();
5071        let base_count = cache
5072            .get(cortical_id)
5073            .map(|count| count.load(Ordering::Relaxed))
5074            .unwrap_or(0);
5075
5076        // Memory areas maintain neurons outside the NPU; add their count from StateManager.
5077        let memory_count = self
5078            .cortical_areas
5079            .get(cortical_id)
5080            .and_then(|area| feagi_evolutionary::extract_memory_properties(&area.properties))
5081            .and_then(|_| {
5082                StateManager::instance()
5083                    .try_read()
5084                    .and_then(|state_manager| {
5085                        state_manager.get_cortical_area_stats(&cortical_id.as_base_64())
5086                    })
5087            })
5088            .map(|stats| stats.neuron_count)
5089            .unwrap_or(0);
5090
5091        base_count.saturating_add(memory_count)
5092    }
5093
5094    /// Get all cortical areas that have neurons
5095    ///
5096    /// # Returns
5097    ///
5098    /// Vec of (cortical_id, neuron_count) for areas with at least one neuron
5099    ///
5100    pub fn get_populated_areas(&self) -> Vec<(String, usize)> {
5101        let mut result = Vec::new();
5102
5103        for cortical_id in self.cortical_areas.keys() {
5104            let count = self.get_neuron_count_in_area(cortical_id);
5105            if count > 0 {
5106                result.push((cortical_id.to_string(), count));
5107            }
5108        }
5109
5110        result
5111    }
5112
5113    /// Check if a cortical area has any neurons
5114    ///
5115    /// # Arguments
5116    ///
5117    /// * `cortical_id` - The cortical area ID
5118    ///
5119    /// # Returns
5120    ///
5121    /// `true` if the area has at least one neuron, `false` otherwise
5122    ///
5123    pub fn is_area_populated(&self, cortical_id: &CorticalID) -> bool {
5124        self.get_neuron_count_in_area(cortical_id) > 0
5125    }
5126
5127    /// Get total synapse count for a specific cortical area (outgoing only) - lock-free cached read
5128    ///
5129    /// # Arguments
5130    ///
5131    /// * `cortical_id` - The cortical area ID
5132    ///
5133    /// # Returns
5134    ///
5135    /// Total number of outgoing synapses from neurons in this area (from cache, never blocks on NPU lock)
5136    ///
5137    /// # Performance
5138    ///
5139    /// This is a lock-free atomic read that never blocks, even during burst processing.
5140    /// Count is maintained in ConnectomeManager and updated when synapses are created/deleted.
5141    ///
5142    pub fn get_synapse_count_in_area(&self, cortical_id: &CorticalID) -> usize {
5143        // CRITICAL: Read from cache (lock-free) - never query NPU for healthcheck endpoints
5144        let cache = self.cached_synapse_counts_per_area.read();
5145        cache
5146            .get(cortical_id)
5147            .map(|count| count.load(Ordering::Relaxed))
5148            .unwrap_or(0)
5149    }
5150
5151    /// Get total incoming synapse count for a specific cortical area.
5152    ///
5153    /// # Arguments
5154    ///
5155    /// * `cortical_id` - The cortical area ID
5156    ///
5157    /// # Returns
5158    ///
5159    /// Total number of incoming synapses targeting neurons in this area.
5160    pub fn get_incoming_synapse_count_in_area(&self, cortical_id: &CorticalID) -> usize {
5161        if !self.cortical_id_to_idx.contains_key(cortical_id) {
5162            return 0;
5163        }
5164
5165        if let Some(state_manager) = StateManager::instance().try_read() {
5166            if let Some(stats) = state_manager.get_cortical_area_stats(&cortical_id.as_base_64()) {
5167                return stats.incoming_synapse_count;
5168            }
5169        }
5170
5171        0
5172    }
5173
5174    /// Get total outgoing synapse count for a specific cortical area.
5175    ///
5176    /// # Arguments
5177    ///
5178    /// * `cortical_id` - The cortical area ID
5179    ///
5180    /// # Returns
5181    ///
5182    /// Total number of outgoing synapses originating from neurons in this area.
5183    pub fn get_outgoing_synapse_count_in_area(&self, cortical_id: &CorticalID) -> usize {
5184        if !self.cortical_id_to_idx.contains_key(cortical_id) {
5185            return 0;
5186        }
5187
5188        if let Some(state_manager) = StateManager::instance().try_read() {
5189            if let Some(stats) = state_manager.get_cortical_area_stats(&cortical_id.as_base_64()) {
5190                return stats.outgoing_synapse_count;
5191            }
5192        }
5193
5194        0
5195    }
5196
5197    /// Check if two neurons are connected (source → target)
5198    ///
5199    /// # Arguments
5200    ///
5201    /// * `source_neuron_id` - The source neuron ID
5202    /// * `target_neuron_id` - The target neuron ID
5203    ///
5204    /// # Returns
5205    ///
5206    /// `true` if there is a synapse from source to target, `false` otherwise
5207    ///
5208    pub fn are_neurons_connected(&self, source_neuron_id: u64, target_neuron_id: u64) -> bool {
5209        let synapses = self.get_outgoing_synapses(source_neuron_id);
5210        synapses
5211            .iter()
5212            .any(|(target, _, _, _)| *target == target_neuron_id as u32)
5213    }
5214
5215    /// Get connection strength (weight) between two neurons
5216    ///
5217    /// # Arguments
5218    ///
5219    /// * `source_neuron_id` - The source neuron ID
5220    /// * `target_neuron_id` - The target neuron ID
5221    ///
5222    /// # Returns
5223    ///
5224    /// Synapse weight (`f32`), or None if no connection exists
5225    ///
5226    pub fn get_connection_weight(
5227        &self,
5228        source_neuron_id: u64,
5229        target_neuron_id: u64,
5230    ) -> Option<f32> {
5231        let synapses = self.get_outgoing_synapses(source_neuron_id);
5232        synapses
5233            .iter()
5234            .find(|(target, _, _, _)| *target == target_neuron_id as u32)
5235            .map(|(_, weight, _, _)| *weight)
5236    }
5237
5238    /// Get connectivity statistics for a cortical area
5239    ///
5240    /// # Arguments
5241    ///
5242    /// * `cortical_id` - The cortical area ID
5243    ///
5244    /// # Returns
5245    ///
5246    /// (neuron_count, total_synapses, avg_synapses_per_neuron)
5247    ///
5248    pub fn get_area_connectivity_stats(&self, cortical_id: &CorticalID) -> (usize, usize, f32) {
5249        let neurons = self.get_neurons_in_area(cortical_id);
5250        let neuron_count = neurons.len();
5251
5252        if neuron_count == 0 {
5253            return (0, 0, 0.0);
5254        }
5255
5256        let mut total_synapses = 0;
5257        for neuron_id in neurons {
5258            total_synapses += self.get_outgoing_synapses(neuron_id).len();
5259        }
5260
5261        let avg_synapses = total_synapses as f32 / neuron_count as f32;
5262
5263        (neuron_count, total_synapses, avg_synapses)
5264    }
5265
5266    /// Get the cortical area ID (string) for a neuron
5267    ///
5268    /// # Arguments
5269    ///
5270    /// * `neuron_id` - The neuron ID
5271    ///
5272    /// # Returns
5273    ///
5274    /// The cortical area ID, or None if neuron doesn't exist
5275    ///
5276    pub fn get_neuron_cortical_id(&self, neuron_id: u64) -> Option<CorticalID> {
5277        let cortical_idx = self.get_neuron_cortical_idx_opt(neuron_id)?;
5278        self.cortical_idx_to_id.get(&cortical_idx).copied()
5279    }
5280
5281    /// Get neuron density (neurons per voxel) for a cortical area
5282    ///
5283    /// # Arguments
5284    ///
5285    /// * `cortical_id` - The cortical area ID
5286    ///
5287    /// # Returns
5288    ///
5289    /// Neuron density (neurons per voxel), or 0.0 if area doesn't exist
5290    ///
5291    pub fn get_neuron_density(&self, cortical_id: &CorticalID) -> f32 {
5292        let area = match self.cortical_areas.get(cortical_id) {
5293            Some(a) => a,
5294            None => return 0.0,
5295        };
5296
5297        let neuron_count = self.get_neuron_count_in_area(cortical_id);
5298        let volume = area.dimensions.width * area.dimensions.height * area.dimensions.depth;
5299
5300        if volume == 0 {
5301            return 0.0;
5302        }
5303
5304        neuron_count as f32 / volume as f32
5305    }
5306
5307    /// Get all cortical areas with connectivity statistics
5308    ///
5309    /// # Returns
5310    ///
5311    /// Vec of (cortical_id, neuron_count, synapse_count, density)
5312    ///
5313    pub fn get_all_area_stats(&self) -> Vec<(String, usize, usize, f32)> {
5314        let mut stats = Vec::new();
5315
5316        for cortical_id in self.cortical_areas.keys() {
5317            let neuron_count = self.get_neuron_count_in_area(cortical_id);
5318            let synapse_count = self.get_synapse_count_in_area(cortical_id);
5319            let density = self.get_neuron_density(cortical_id);
5320
5321            stats.push((
5322                cortical_id.to_string(),
5323                neuron_count,
5324                synapse_count,
5325                density,
5326            ));
5327        }
5328
5329        stats
5330    }
5331
5332    // ======================================================================
5333    // Configuration
5334    // ======================================================================
5335
5336    /// Get the configuration
5337    pub fn get_config(&self) -> &ConnectomeConfig {
5338        &self.config
5339    }
5340
5341    /// Update configuration
5342    pub fn set_config(&mut self, config: ConnectomeConfig) {
5343        self.config = config;
5344    }
5345
5346    // ======================================================================
5347    // Genome I/O
5348    // ======================================================================
5349
5350    /// Ensure core cortical areas (_death, _power, _fatigue, _pain, _pleasure, _fear, _hope) exist
5351    ///
5352    /// Core areas are required for brain operation:
5353    /// - `_death` (cortical_idx=0): Manages neuron death and cleanup
5354    /// - `_power` (cortical_idx=1): Provides power injection for burst engine
5355    /// - `_fatigue` (cortical_idx=2): Monitors brain fatigue and triggers sleep mode
5356    /// - `_pain` (cortical_idx=3): Pain signal processing
5357    /// - `_pleasure` (cortical_idx=4): Pleasure signal processing
5358    /// - `_fear` (cortical_idx=5): Fear signal processing
5359    /// - `_hope` (cortical_idx=6): Hope signal processing
5360    ///
5361    /// If any core area is missing from the genome, it will be automatically created
5362    /// with default properties (1x1x1 dimensions, minimal configuration).
5363    ///
5364    /// # Returns
5365    ///
5366    /// * `Ok(())` if all core areas exist or were successfully created
5367    /// * `Err(BduError)` if creation fails
5368    pub fn ensure_core_cortical_areas(&mut self) -> BduResult<()> {
5369        info!(target: "feagi-bdu", "🔧 [CORE-AREA] Ensuring core cortical areas exist...");
5370
5371        use feagi_structures::genomic::cortical_area::{
5372            CoreCorticalType, CorticalArea, CorticalAreaDimensions, CorticalAreaType,
5373        };
5374
5375        // Core areas are always 1x1x1 as per requirements
5376        let core_dimensions = CorticalAreaDimensions::new(1, 1, 1).map_err(|e| {
5377            BduError::Internal(format!("Failed to create core area dimensions: {}", e))
5378        })?;
5379
5380        // Default position for core areas (origin)
5381        let core_position = (0, 0, 0).into();
5382
5383        // Check and create _death (cortical_idx=0)
5384        let death_id = CoreCorticalType::Death.to_cortical_id();
5385        if !self.cortical_areas.contains_key(&death_id) {
5386            info!(target: "feagi-bdu", "🔧 [CORE-AREA] Creating missing _death area (cortical_idx=0)");
5387            let death_area = CorticalArea::new(
5388                death_id,
5389                0, // Will be overridden by add_cortical_area to 0
5390                "_death".to_string(),
5391                core_dimensions,
5392                core_position,
5393                CorticalAreaType::Core(CoreCorticalType::Death),
5394            )
5395            .map_err(|e| BduError::Internal(format!("Failed to create _death area: {}", e)))?;
5396            match self.add_cortical_area(death_area) {
5397                Ok(idx) => {
5398                    info!(target: "feagi-bdu", "  ✅ Created _death area with cortical_idx={}", idx);
5399                }
5400                Err(e) => {
5401                    error!(target: "feagi-bdu", "  ❌ Failed to add _death area: {}", e);
5402                    return Err(e);
5403                }
5404            }
5405        } else {
5406            info!(target: "feagi-bdu", "  ✓ _death area already exists");
5407        }
5408
5409        // Check and create _power (cortical_idx=1)
5410        let power_id = CoreCorticalType::Power.to_cortical_id();
5411        if !self.cortical_areas.contains_key(&power_id) {
5412            info!(target: "feagi-bdu", "🔧 [CORE-AREA] Creating missing _power area (cortical_idx=1)");
5413            let power_area = CorticalArea::new(
5414                power_id,
5415                1, // Will be overridden by add_cortical_area to 1
5416                "_power".to_string(),
5417                core_dimensions,
5418                core_position,
5419                CorticalAreaType::Core(CoreCorticalType::Power),
5420            )
5421            .map_err(|e| BduError::Internal(format!("Failed to create _power area: {}", e)))?;
5422            match self.add_cortical_area(power_area) {
5423                Ok(idx) => {
5424                    info!(target: "feagi-bdu", "  ✅ Created _power area with cortical_idx={}", idx);
5425                }
5426                Err(e) => {
5427                    error!(target: "feagi-bdu", "  ❌ Failed to add _power area: {}", e);
5428                    return Err(e);
5429                }
5430            }
5431        } else {
5432            info!(target: "feagi-bdu", "  ✓ _power area already exists");
5433        }
5434
5435        // Check and create _fatigue (cortical_idx=2)
5436        let fatigue_id = CoreCorticalType::Fatigue.to_cortical_id();
5437        let pain_id = CoreCorticalType::Pain.to_cortical_id();
5438        let pleasure_id = CoreCorticalType::Pleasure.to_cortical_id();
5439        let fear_id = CoreCorticalType::Fear.to_cortical_id();
5440        let hope_id = CoreCorticalType::Hope.to_cortical_id();
5441        if !self.cortical_areas.contains_key(&fatigue_id) {
5442            info!(target: "feagi-bdu", "🔧 [CORE-AREA] Creating missing _fatigue area (cortical_idx=2)");
5443            let fatigue_area = CorticalArea::new(
5444                fatigue_id,
5445                2, // Will be overridden by add_cortical_area to 2
5446                "_fatigue".to_string(),
5447                core_dimensions,
5448                core_position,
5449                CorticalAreaType::Core(CoreCorticalType::Fatigue),
5450            )
5451            .map_err(|e| BduError::Internal(format!("Failed to create _fatigue area: {}", e)))?;
5452            match self.add_cortical_area(fatigue_area) {
5453                Ok(idx) => {
5454                    info!(target: "feagi-bdu", "  ✅ Created _fatigue area with cortical_idx={}", idx);
5455                }
5456                Err(e) => {
5457                    error!(target: "feagi-bdu", "  ❌ Failed to add _fatigue area: {}", e);
5458                    return Err(e);
5459                }
5460            }
5461        } else {
5462            info!(target: "feagi-bdu", "  ✓ _fatigue area already exists");
5463        }
5464
5465        // Check and create _pain (cortical_idx=3)
5466        if !self.cortical_areas.contains_key(&pain_id) {
5467            info!(target: "feagi-bdu", "🔧 [CORE-AREA] Creating missing _pain area (cortical_idx=3)");
5468            let pain_area = CorticalArea::new(
5469                pain_id,
5470                3, // Will be overridden by add_cortical_area to 3
5471                "_pain".to_string(),
5472                core_dimensions,
5473                core_position,
5474                CorticalAreaType::Core(CoreCorticalType::Pain),
5475            )
5476            .map_err(|e| BduError::Internal(format!("Failed to create _pain area: {}", e)))?;
5477            match self.add_cortical_area(pain_area) {
5478                Ok(idx) => {
5479                    info!(target: "feagi-bdu", "  ✅ Created _pain area with cortical_idx={}", idx);
5480                }
5481                Err(e) => {
5482                    error!(target: "feagi-bdu", "  ❌ Failed to add _pain area: {}", e);
5483                    return Err(e);
5484                }
5485            }
5486        } else {
5487            info!(target: "feagi-bdu", "  ✓ _pain area already exists");
5488        }
5489
5490        // Check and create _pleasure (cortical_idx=4)
5491        if !self.cortical_areas.contains_key(&pleasure_id) {
5492            info!(target: "feagi-bdu", "🔧 [CORE-AREA] Creating missing _pleasure area (cortical_idx=4)");
5493            let pleasure_area = CorticalArea::new(
5494                pleasure_id,
5495                4, // Will be overridden by add_cortical_area to 4
5496                "_pleasure".to_string(),
5497                core_dimensions,
5498                core_position,
5499                CorticalAreaType::Core(CoreCorticalType::Pleasure),
5500            )
5501            .map_err(|e| BduError::Internal(format!("Failed to create _pleasure area: {}", e)))?;
5502            match self.add_cortical_area(pleasure_area) {
5503                Ok(idx) => {
5504                    info!(target: "feagi-bdu", "  ✅ Created _pleasure area with cortical_idx={}", idx);
5505                }
5506                Err(e) => {
5507                    error!(target: "feagi-bdu", "  ❌ Failed to add _pleasure area: {}", e);
5508                    return Err(e);
5509                }
5510            }
5511        } else {
5512            info!(target: "feagi-bdu", "  ✓ _pleasure area already exists");
5513        }
5514
5515        // Check and create _fear (cortical_idx=5)
5516        if !self.cortical_areas.contains_key(&fear_id) {
5517            info!(target: "feagi-bdu", "🔧 [CORE-AREA] Creating missing _fear area (cortical_idx=5)");
5518            let fear_area = CorticalArea::new(
5519                fear_id,
5520                5, // Will be overridden by add_cortical_area to 5
5521                "_fear".to_string(),
5522                core_dimensions,
5523                core_position,
5524                CorticalAreaType::Core(CoreCorticalType::Fear),
5525            )
5526            .map_err(|e| BduError::Internal(format!("Failed to create _fear area: {}", e)))?;
5527            match self.add_cortical_area(fear_area) {
5528                Ok(idx) => {
5529                    info!(target: "feagi-bdu", "  ✅ Created _fear area with cortical_idx={}", idx);
5530                }
5531                Err(e) => {
5532                    error!(target: "feagi-bdu", "  ❌ Failed to add _fear area: {}", e);
5533                    return Err(e);
5534                }
5535            }
5536        } else {
5537            info!(target: "feagi-bdu", "  ✓ _fear area already exists");
5538        }
5539
5540        // Check and create _hope (cortical_idx=6)
5541        if !self.cortical_areas.contains_key(&hope_id) {
5542            info!(target: "feagi-bdu", "🔧 [CORE-AREA] Creating missing _hope area (cortical_idx=6)");
5543            let hope_area = CorticalArea::new(
5544                hope_id,
5545                6, // Will be overridden by add_cortical_area to 6
5546                "_hope".to_string(),
5547                core_dimensions,
5548                core_position,
5549                CorticalAreaType::Core(CoreCorticalType::Hope),
5550            )
5551            .map_err(|e| BduError::Internal(format!("Failed to create _hope area: {}", e)))?;
5552            match self.add_cortical_area(hope_area) {
5553                Ok(idx) => {
5554                    info!(target: "feagi-bdu", "  ✅ Created _hope area with cortical_idx={}", idx);
5555                }
5556                Err(e) => {
5557                    error!(target: "feagi-bdu", "  ❌ Failed to add _hope area: {}", e);
5558                    return Err(e);
5559                }
5560            }
5561        } else {
5562            info!(target: "feagi-bdu", "  ✓ _hope area already exists");
5563        }
5564
5565        info!(target: "feagi-bdu", "🔧 [CORE-AREA] Core area check complete");
5566        Ok(())
5567    }
5568
5569    /// Save the connectome as a genome JSON
5570    ///
5571    /// **DEPRECATED**: This method produces incomplete hierarchical format v2.1 without morphologies/physiology.
5572    /// Use `GenomeService::save_genome()` instead, which produces complete flat format v3.0.
5573    ///
5574    /// This method is kept only for legacy tests. Production code MUST use GenomeService.
5575    ///
5576    /// # Arguments
5577    ///
5578    /// * `genome_id` - Optional custom genome ID (generates timestamp-based ID if None)
5579    /// * `genome_title` - Optional custom genome title
5580    ///
5581    /// # Returns
5582    ///
5583    /// JSON string representation of the genome (hierarchical v2.1, incomplete)
5584    ///
5585    #[deprecated(
5586        note = "Use GenomeService::save_genome() instead. This produces incomplete v2.1 format without morphologies/physiology."
5587    )]
5588    #[allow(deprecated)]
5589    pub fn save_genome_to_json(
5590        &self,
5591        genome_id: Option<String>,
5592        genome_title: Option<String>,
5593    ) -> BduResult<String> {
5594        // Build parent map from brain region hierarchy
5595        let mut brain_regions_with_parents = std::collections::HashMap::new();
5596
5597        for region_id in self.brain_regions.get_all_region_ids() {
5598            if let Some(region) = self.brain_regions.get_region(region_id) {
5599                let parent_id = self
5600                    .brain_regions
5601                    .get_parent(region_id)
5602                    .map(|s| s.to_string());
5603                brain_regions_with_parents
5604                    .insert(region_id.to_string(), (region.clone(), parent_id));
5605            }
5606        }
5607
5608        // Generate and return JSON
5609        Ok(feagi_evolutionary::GenomeSaver::save_to_json(
5610            &self.cortical_areas,
5611            &brain_regions_with_parents,
5612            genome_id,
5613            genome_title,
5614        )?)
5615    }
5616
5617    // Load genome from file and develop brain
5618    //
5619    // This was a high-level convenience method that:
5620    // 1. Loads genome from JSON file
5621    // 2. Prepares for new genome (clears existing state)
5622    // 3. Runs neuroembryogenesis to develop the brain
5623    //
5624    // # Arguments
5625    //
5626    // * `genome_path` - Path to genome JSON file
5627    //
5628    // # Returns
5629    //
5630    // Development progress information
5631    //
5632    // NOTE: load_from_genome_file() and load_from_genome() have been REMOVED.
5633    // All genome loading must now go through GenomeService::load_genome() which:
5634    // - Stores RuntimeGenome for persistence
5635    // - Updates genome metadata
5636    // - Provides async/await support
5637    // - Includes timeout protection
5638    // - Ensures core cortical areas exist
5639    //
5640    // See: feagi-services/src/impls/genome_service_impl.rs::load_genome()
5641
5642    /// Prepare for loading a new genome
5643    ///
5644    /// Clears all existing cortical areas, brain regions, and resets state.
5645    /// This is typically called before loading a new genome.
5646    ///
5647    pub fn prepare_for_new_genome(&mut self) -> BduResult<()> {
5648        info!(target: "feagi-bdu","Preparing for new genome (clearing existing state)");
5649
5650        // Clear cortical areas
5651        self.cortical_areas.clear();
5652        self.cortical_id_to_idx.clear();
5653        self.cortical_idx_to_id.clear();
5654        // CRITICAL: Reserve 0..=6 for invariant core areas.
5655        self.next_cortical_idx = 7;
5656        info!("🔧 [BRAIN-RESET] Cortical mapping cleared, next_cortical_idx reset to 7 (reserves 0=_death, 1=_power, 2=_fatigue, 3=_pain, 4=_pleasure, 5=_fear, 6=_hope)");
5657
5658        // Clear brain regions
5659        self.brain_regions = BrainRegionHierarchy::new();
5660
5661        // Reset NPU runtime state to prevent old neurons/synapses from leaking into the next genome.
5662        if let Some(ref npu) = self.npu {
5663            let mut npu_lock = npu
5664                .lock()
5665                .map_err(|e| BduError::Internal(format!("Failed to lock NPU: {}", e)))?;
5666            npu_lock
5667                .reset_for_new_genome()
5668                .map_err(|e| BduError::Internal(format!("Failed to reset NPU: {}", e)))?;
5669        }
5670
5671        info!(target: "feagi-bdu","✅ Connectome cleared and ready for new genome");
5672        Ok(())
5673    }
5674
5675    /// Calculate and resize memory for a genome
5676    ///
5677    /// Analyzes the genome to determine memory requirements and
5678    /// prepares the NPU for the expected neuron/synapse counts.
5679    ///
5680    /// # Arguments
5681    ///
5682    /// * `genome` - Genome to analyze for memory requirements
5683    ///
5684    pub fn resize_for_genome(
5685        &mut self,
5686        genome: &feagi_evolutionary::RuntimeGenome,
5687    ) -> BduResult<()> {
5688        // Store morphologies from genome
5689        self.morphology_registry = genome.morphologies.clone();
5690        info!(target: "feagi-bdu", "Stored {} morphologies from genome", self.morphology_registry.count());
5691
5692        // Calculate required capacity from genome stats
5693        let required_neurons = genome.stats.innate_neuron_count;
5694        let required_synapses = genome.stats.innate_synapse_count;
5695
5696        info!(target: "feagi-bdu",
5697            "Genome requires: {} neurons, {} synapses",
5698            required_neurons,
5699            required_synapses
5700        );
5701
5702        // Calculate total voxels from all cortical areas
5703        let mut total_voxels = 0;
5704        for area in genome.cortical_areas.values() {
5705            total_voxels += area.dimensions.width * area.dimensions.height * area.dimensions.depth;
5706        }
5707
5708        info!(target: "feagi-bdu",
5709            "Genome has {} cortical areas with {} total voxels",
5710            genome.cortical_areas.len(),
5711            total_voxels
5712        );
5713
5714        // TODO: Resize NPU if needed
5715        // For now, we assume NPU has sufficient capacity
5716        // In the future, we may want to dynamically resize the NPU based on genome requirements
5717
5718        Ok(())
5719    }
5720
5721    // ========================================================================
5722    // SYNAPSE OPERATIONS
5723    // ========================================================================
5724
5725    /// Create a synapse between two neurons
5726    ///
5727    /// # Arguments
5728    ///
5729    /// * `source_neuron_id` - Source neuron ID
5730    /// * `target_neuron_id` - Target neuron ID
5731    /// * `weight` - Synapse weight (`f32`)
5732    /// * `psp` - Synapse PSP (`f32`)
5733    /// * `synapse_type` - Synapse type (0=excitatory, 1=inhibitory)
5734    ///
5735    /// # Returns
5736    ///
5737    /// `Ok(())` if synapse created successfully
5738    ///
5739    pub fn create_synapse(
5740        &mut self,
5741        source_neuron_id: u64,
5742        target_neuron_id: u64,
5743        weight: f32,
5744        psp: f32,
5745        synapse_type: u8,
5746    ) -> BduResult<()> {
5747        // Get NPU
5748        let npu = self
5749            .npu
5750            .as_ref()
5751            .ok_or_else(|| BduError::Internal("NPU not connected".to_string()))?;
5752
5753        let mut npu_lock = npu
5754            .lock()
5755            .map_err(|e| BduError::Internal(format!("Failed to lock NPU: {}", e)))?;
5756
5757        // Verify both neurons exist
5758        let source_exists = (source_neuron_id as u32) < npu_lock.get_neuron_count() as u32;
5759        let target_exists = (target_neuron_id as u32) < npu_lock.get_neuron_count() as u32;
5760
5761        if !source_exists {
5762            return Err(BduError::InvalidNeuron(format!(
5763                "Source neuron {} not found",
5764                source_neuron_id
5765            )));
5766        }
5767        if !target_exists {
5768            return Err(BduError::InvalidNeuron(format!(
5769                "Target neuron {} not found",
5770                target_neuron_id
5771            )));
5772        }
5773
5774        // Create synapse via NPU
5775        let syn_type = if synapse_type == 0 {
5776            feagi_npu_neural::synapse::SynapseType::Excitatory
5777        } else {
5778            feagi_npu_neural::synapse::SynapseType::Inhibitory
5779        };
5780
5781        let synapse_idx = npu_lock
5782            .add_synapse(
5783                NeuronId(source_neuron_id as u32),
5784                NeuronId(target_neuron_id as u32),
5785                feagi_npu_neural::types::SynapticWeight(weight),
5786                feagi_npu_neural::types::SynapticPsp(psp),
5787                syn_type,
5788                0,
5789                1,
5790            )
5791            .map_err(|e| BduError::Internal(format!("Failed to create synapse: {}", e)))?;
5792
5793        debug!(target: "feagi-bdu", "Created synapse: {} -> {} (weight: {}, psp: {}, type: {}, idx: {})",
5794            source_neuron_id, target_neuron_id, weight, psp, synapse_type, synapse_idx);
5795
5796        let source_cortical_idx = npu_lock.get_neuron_cortical_area(source_neuron_id as u32);
5797        let target_cortical_idx = npu_lock.get_neuron_cortical_area(target_neuron_id as u32);
5798        let source_cortical_id =
5799            source_cortical_idx.and_then(|idx| self.cortical_idx_to_id.get(&idx).cloned());
5800        let target_cortical_id =
5801            target_cortical_idx.and_then(|idx| self.cortical_idx_to_id.get(&idx).cloned());
5802
5803        let state_manager = StateManager::instance();
5804        let state_manager = state_manager.read();
5805        let core_state = state_manager.get_core_state();
5806        core_state.add_synapse_count(1);
5807        if let Some(cortical_id) = source_cortical_id {
5808            state_manager.add_cortical_area_outgoing_synapses(&cortical_id.as_base_64(), 1);
5809        }
5810        if let Some(cortical_id) = target_cortical_id {
5811            state_manager.add_cortical_area_incoming_synapses(&cortical_id.as_base_64(), 1);
5812        }
5813
5814        // Trigger fatigue index recalculation after synapse creation
5815        // NOTE: Disabled during genome loading to prevent blocking
5816        // let _ = self.update_fatigue_index();
5817
5818        Ok(())
5819    }
5820
5821    /// Synchronize cortical area flags with NPU
5822    /// This should be called after adding/updating cortical areas
5823    fn sync_cortical_area_flags_to_npu(&mut self) -> BduResult<()> {
5824        if let Some(ref npu) = self.npu {
5825            if let Ok(mut npu_lock) = npu.lock() {
5826                // Build psp_uniform_distribution flags map
5827                let mut psp_uniform_flags = ahash::AHashMap::new();
5828                let mut mp_driven_psp_flags = ahash::AHashMap::new();
5829                let mut postsynaptic_current_flags = ahash::AHashMap::new();
5830                let mut degeneration_flags = ahash::AHashMap::new();
5831
5832                for (cortical_id, area) in &self.cortical_areas {
5833                    // When the property is absent: Power and Memory cortical areas default to uniform
5834                    // PSP (full PSP per synapse); other areas default to divided PSP.
5835                    let default_psp_uniform = *cortical_id
5836                        == CoreCorticalType::Power.to_cortical_id()
5837                        || matches!(area.cortical_type, CorticalAreaType::Memory(_));
5838                    let psp_uniform = area
5839                        .get_property("psp_uniform_distribution")
5840                        .and_then(|v| v.as_bool())
5841                        .unwrap_or(default_psp_uniform);
5842                    psp_uniform_flags.insert(*cortical_id, psp_uniform);
5843
5844                    // Get mp_driven_psp flag (default to false)
5845                    let mp_driven_psp = area
5846                        .get_property("mp_driven_psp")
5847                        .and_then(|v| v.as_bool())
5848                        .unwrap_or(false);
5849                    mp_driven_psp_flags.insert(*cortical_id, mp_driven_psp);
5850
5851                    // Store configured baseline PSP for reset-time restoration.
5852                    let postsynaptic_current = area
5853                        .get_property("postsynaptic_current")
5854                        .and_then(|v| v.as_f64())
5855                        .unwrap_or(1.0) as f32;
5856                    postsynaptic_current_flags.insert(*cortical_id, postsynaptic_current);
5857
5858                    // Get degeneration coefficient (default 0.0 = disabled)
5859                    let degeneration = area
5860                        .get_property("degeneration")
5861                        .and_then(|v| v.as_f64())
5862                        .unwrap_or(0.0) as f32;
5863                    if degeneration > 0.0 {
5864                        degeneration_flags.insert(*cortical_id, degeneration);
5865                    }
5866                }
5867
5868                // Update NPU with flags
5869                npu_lock.set_psp_uniform_distribution_flags(psp_uniform_flags);
5870                npu_lock.set_mp_driven_psp_flags(mp_driven_psp_flags);
5871                npu_lock.set_postsynaptic_current_flags(postsynaptic_current_flags);
5872                npu_lock.set_degeneration_flags(degeneration_flags);
5873
5874                trace!(
5875                    target: "feagi-bdu",
5876                    "Synchronized cortical area flags to NPU ({} areas)",
5877                    self.cortical_areas.len()
5878                );
5879            }
5880        }
5881
5882        Ok(())
5883    }
5884
5885    /// Get synapse information between two neurons
5886    ///
5887    /// # Arguments
5888    ///
5889    /// * `source_neuron_id` - Source neuron ID
5890    /// * `target_neuron_id` - Target neuron ID
5891    ///
5892    /// # Returns
5893    ///
5894    /// `Some((weight, psp, type))` if synapse exists, `None` otherwise
5895    ///
5896    pub fn get_synapse(
5897        &self,
5898        source_neuron_id: u64,
5899        target_neuron_id: u64,
5900    ) -> Option<(f32, f32, u8)> {
5901        // Get NPU
5902        let npu = self.npu.as_ref()?;
5903        let npu_lock = npu.lock().ok()?;
5904
5905        // Use get_incoming_synapses and filter by source
5906        // (This does O(n) scan of synapse_array, but works even when propagation engine isn't updated)
5907        let incoming = npu_lock.get_incoming_synapses(target_neuron_id as u32);
5908
5909        // Find the synapse from our specific source
5910        for (source_id, weight, psp, synapse_type) in incoming {
5911            if source_id == source_neuron_id as u32 {
5912                return Some((weight, psp, synapse_type));
5913            }
5914        }
5915
5916        None
5917    }
5918
5919    /// Update the weight of an existing synapse
5920    ///
5921    /// # Arguments
5922    ///
5923    /// * `source_neuron_id` - Source neuron ID
5924    /// * `target_neuron_id` - Target neuron ID
5925    /// * `new_weight` - New synapse weight (0-255)
5926    ///
5927    /// # Returns
5928    ///
5929    /// `Ok(())` if synapse updated, `Err` if synapse not found
5930    ///
5931    pub fn update_synapse_weight(
5932        &mut self,
5933        source_neuron_id: u64,
5934        target_neuron_id: u64,
5935        new_weight: f32,
5936    ) -> BduResult<()> {
5937        // Get NPU
5938        let npu = self
5939            .npu
5940            .as_ref()
5941            .ok_or_else(|| BduError::Internal("NPU not connected".to_string()))?;
5942
5943        let mut npu_lock = npu
5944            .lock()
5945            .map_err(|e| BduError::Internal(format!("Failed to lock NPU: {}", e)))?;
5946
5947        // Update synapse weight via NPU
5948        let updated = npu_lock.update_synapse_weight(
5949            NeuronId(source_neuron_id as u32),
5950            NeuronId(target_neuron_id as u32),
5951            feagi_npu_neural::types::SynapticWeight(new_weight),
5952        );
5953
5954        if updated {
5955            debug!(target: "feagi-bdu","Updated synapse weight: {} -> {} = {}", source_neuron_id, target_neuron_id, new_weight);
5956            Ok(())
5957        } else {
5958            Err(BduError::InvalidSynapse(format!(
5959                "Synapse {} -> {} not found",
5960                source_neuron_id, target_neuron_id
5961            )))
5962        }
5963    }
5964
5965    /// Remove a synapse between two neurons
5966    ///
5967    /// # Arguments
5968    ///
5969    /// * `source_neuron_id` - Source neuron ID
5970    /// * `target_neuron_id` - Target neuron ID
5971    ///
5972    /// # Returns
5973    ///
5974    /// `Ok(true)` if synapse removed, `Ok(false)` if synapse didn't exist
5975    ///
5976    pub fn remove_synapse(
5977        &mut self,
5978        source_neuron_id: u64,
5979        target_neuron_id: u64,
5980    ) -> BduResult<bool> {
5981        // Get NPU
5982        let npu = self
5983            .npu
5984            .as_ref()
5985            .ok_or_else(|| BduError::Internal("NPU not connected".to_string()))?;
5986
5987        let mut npu_lock = npu
5988            .lock()
5989            .map_err(|e| BduError::Internal(format!("Failed to lock NPU: {}", e)))?;
5990
5991        let source_cortical_idx = npu_lock.get_neuron_cortical_area(source_neuron_id as u32);
5992        let target_cortical_idx = npu_lock.get_neuron_cortical_area(target_neuron_id as u32);
5993        let source_cortical_id =
5994            source_cortical_idx.and_then(|idx| self.cortical_idx_to_id.get(&idx).cloned());
5995        let target_cortical_id =
5996            target_cortical_idx.and_then(|idx| self.cortical_idx_to_id.get(&idx).cloned());
5997
5998        // Remove synapse via NPU
5999        let removed = npu_lock.remove_synapse(
6000            NeuronId(source_neuron_id as u32),
6001            NeuronId(target_neuron_id as u32),
6002        );
6003
6004        if removed {
6005            debug!(target: "feagi-bdu","Removed synapse: {} -> {}", source_neuron_id, target_neuron_id);
6006
6007            // CRITICAL: Update StateManager synapse count (for health_check endpoint)
6008            let state_manager = StateManager::instance();
6009            let state_manager = state_manager.read();
6010            let core_state = state_manager.get_core_state();
6011            core_state.subtract_synapse_count(1);
6012            if let Some(cortical_id) = source_cortical_id {
6013                state_manager
6014                    .subtract_cortical_area_outgoing_synapses(&cortical_id.as_base_64(), 1);
6015            }
6016            if let Some(cortical_id) = target_cortical_id {
6017                state_manager
6018                    .subtract_cortical_area_incoming_synapses(&cortical_id.as_base_64(), 1);
6019            }
6020        }
6021
6022        Ok(removed)
6023    }
6024
6025    // ========================================================================
6026    // BATCH OPERATIONS
6027    // ========================================================================
6028
6029    /// Batch create multiple neurons at once (SIMD-optimized)
6030    ///
6031    /// This is significantly faster than calling `add_neuron()` in a loop
6032    ///
6033    /// # Arguments
6034    ///
6035    /// * `cortical_id` - Target cortical area
6036    /// * `neurons` - Vector of neuron parameters (x, y, z, firing_threshold, leak, resting_potential, etc.)
6037    ///
6038    /// # Returns
6039    ///
6040    /// Vector of created neuron IDs
6041    ///
6042    pub fn batch_create_neurons(
6043        &mut self,
6044        cortical_id: &CorticalID,
6045        neurons: Vec<NeuronData>,
6046    ) -> BduResult<Vec<u64>> {
6047        // Get NPU
6048        let npu = self
6049            .npu
6050            .as_ref()
6051            .ok_or_else(|| BduError::Internal("NPU not connected".to_string()))?;
6052
6053        let mut npu_lock = npu
6054            .lock()
6055            .map_err(|e| BduError::Internal(format!("Failed to lock NPU: {}", e)))?;
6056
6057        // Get cortical area to verify it exists and get its index
6058        let area = self.get_cortical_area(cortical_id).ok_or_else(|| {
6059            BduError::InvalidArea(format!("Cortical area {} not found", cortical_id))
6060        })?;
6061        let cortical_idx = area.cortical_idx;
6062
6063        let count = neurons.len();
6064
6065        // Extract parameters into separate vectors for batch operation
6066        let mut x_coords = Vec::with_capacity(count);
6067        let mut y_coords = Vec::with_capacity(count);
6068        let mut z_coords = Vec::with_capacity(count);
6069        let mut firing_thresholds = Vec::with_capacity(count);
6070        let mut threshold_limits = Vec::with_capacity(count);
6071        let mut leak_coeffs = Vec::with_capacity(count);
6072        let mut resting_potentials = Vec::with_capacity(count);
6073        let mut neuron_types = Vec::with_capacity(count);
6074        let mut refractory_periods = Vec::with_capacity(count);
6075        let mut excitabilities = Vec::with_capacity(count);
6076        let mut consec_fire_limits = Vec::with_capacity(count);
6077        let mut snooze_lengths = Vec::with_capacity(count);
6078        let mut mp_accums = Vec::with_capacity(count);
6079        let mut cortical_areas = Vec::with_capacity(count);
6080
6081        for (
6082            x,
6083            y,
6084            z,
6085            threshold,
6086            threshold_limit,
6087            leak,
6088            resting,
6089            ntype,
6090            refract,
6091            excit,
6092            consec_limit,
6093            snooze,
6094            mp_accum,
6095        ) in neurons
6096        {
6097            x_coords.push(x);
6098            y_coords.push(y);
6099            z_coords.push(z);
6100            firing_thresholds.push(threshold);
6101            threshold_limits.push(threshold_limit);
6102            leak_coeffs.push(leak);
6103            resting_potentials.push(resting);
6104            neuron_types.push(ntype);
6105            refractory_periods.push(refract);
6106            excitabilities.push(excit);
6107            consec_fire_limits.push(consec_limit);
6108            snooze_lengths.push(snooze);
6109            mp_accums.push(mp_accum);
6110            cortical_areas.push(cortical_idx);
6111        }
6112
6113        // Get the current neuron count - this will be the first ID of our batch
6114        let first_neuron_id = npu_lock.get_neuron_count() as u32;
6115
6116        // Call NPU batch creation (SIMD-optimized)
6117        // Signature: (thresholds, threshold_limits, leak_coeffs, resting_pots, neuron_types, refract, excit, consec_limits, snooze, mp_accums, cortical_areas, x, y, z)
6118        // Convert f32 vectors to T
6119        // DynamicNPU will handle f32 inputs and convert internally based on its precision
6120        let firing_thresholds_t = firing_thresholds;
6121        let threshold_limits_t = threshold_limits;
6122        let resting_potentials_t = resting_potentials;
6123        let (neurons_created, _indices) = npu_lock.add_neurons_batch(
6124            firing_thresholds_t,
6125            threshold_limits_t,
6126            leak_coeffs,
6127            resting_potentials_t,
6128            neuron_types,
6129            refractory_periods,
6130            excitabilities,
6131            consec_fire_limits,
6132            snooze_lengths,
6133            mp_accums,
6134            cortical_areas,
6135            x_coords,
6136            y_coords,
6137            z_coords,
6138        );
6139
6140        // Generate neuron IDs (they are sequential starting from first_neuron_id)
6141        let mut neuron_ids = Vec::with_capacity(count);
6142        for i in 0..neurons_created {
6143            neuron_ids.push((first_neuron_id + i) as u64);
6144        }
6145
6146        info!(target: "feagi-bdu","Batch created {} neurons in cortical area {}", count, cortical_id);
6147
6148        // CRITICAL: Update StateManager neuron count (for health_check endpoint)
6149        let state_manager = StateManager::instance();
6150        let state_manager = state_manager.read();
6151        let core_state = state_manager.get_core_state();
6152        core_state.add_neuron_count(neurons_created);
6153        core_state.add_regular_neuron_count(neurons_created);
6154        state_manager.add_cortical_area_neuron_count(&cortical_id.as_base_64(), count);
6155
6156        // Best-effort: keep per-area cache in sync for lock-free reads.
6157        {
6158            let mut cache = self.cached_neuron_counts_per_area.write();
6159            cache
6160                .entry(*cortical_id)
6161                .or_insert_with(|| AtomicUsize::new(0))
6162                .fetch_add(count, Ordering::Relaxed);
6163        }
6164
6165        Ok(neuron_ids)
6166    }
6167
6168    /// Delete multiple neurons at once (batch operation)
6169    ///
6170    /// # Arguments
6171    ///
6172    /// * `neuron_ids` - Vector of neuron IDs to delete
6173    ///
6174    /// # Returns
6175    ///
6176    /// Number of neurons actually deleted
6177    ///
6178    pub fn delete_neurons_batch(&mut self, neuron_ids: Vec<u64>) -> BduResult<usize> {
6179        // Get NPU
6180        let npu = self
6181            .npu
6182            .as_ref()
6183            .ok_or_else(|| BduError::Internal("NPU not connected".to_string()))?;
6184
6185        let mut npu_lock = npu
6186            .lock()
6187            .map_err(|e| BduError::Internal(format!("Failed to lock NPU: {}", e)))?;
6188
6189        let mut deleted_count = 0;
6190        let mut per_area_deleted: std::collections::HashMap<String, usize> =
6191            std::collections::HashMap::new();
6192
6193        // Delete each neuron
6194        // Note: Could be optimized with a batch delete method in NPU if needed
6195        for neuron_id in neuron_ids {
6196            let cortical_idx = npu_lock.get_neuron_cortical_area(neuron_id as u32);
6197            let cortical_id =
6198                cortical_idx.and_then(|idx| self.cortical_idx_to_id.get(&idx).cloned());
6199
6200            if npu_lock.delete_neuron(neuron_id as u32) {
6201                deleted_count += 1;
6202                if let Some(cortical_id) = cortical_id {
6203                    let key = cortical_id.as_base_64();
6204                    *per_area_deleted.entry(key).or_insert(0) += 1;
6205                }
6206            }
6207        }
6208
6209        info!(target: "feagi-bdu","Batch deleted {} neurons", deleted_count);
6210
6211        // CRITICAL: Update StateManager neuron count (for health_check endpoint)
6212        if deleted_count > 0 {
6213            let state_manager = StateManager::instance();
6214            let state_manager = state_manager.read();
6215            let core_state = state_manager.get_core_state();
6216            core_state.subtract_neuron_count(deleted_count as u32);
6217            core_state.subtract_regular_neuron_count(deleted_count as u32);
6218            for (cortical_id, count) in per_area_deleted {
6219                state_manager.subtract_cortical_area_neuron_count(&cortical_id, count);
6220            }
6221        }
6222
6223        // Trigger fatigue index recalculation after batch neuron deletion
6224        // NOTE: Disabled during genome loading to prevent blocking
6225        // if deleted_count > 0 {
6226        //     let _ = self.update_fatigue_index();
6227        // }
6228
6229        Ok(deleted_count)
6230    }
6231
6232    // ========================================================================
6233    // NEURON UPDATE OPERATIONS
6234    // ========================================================================
6235
6236    /// Update properties of an existing neuron
6237    ///
6238    /// # Arguments
6239    ///
6240    /// * `neuron_id` - Target neuron ID
6241    /// * `firing_threshold` - Optional new firing threshold
6242    /// * `leak_coefficient` - Optional new leak coefficient
6243    /// * `resting_potential` - Optional new resting potential
6244    /// * `excitability` - Optional new excitability
6245    ///
6246    /// # Returns
6247    ///
6248    /// `Ok(())` if neuron updated successfully
6249    ///
6250    pub fn update_neuron_properties(
6251        &mut self,
6252        neuron_id: u64,
6253        firing_threshold: Option<f32>,
6254        leak_coefficient: Option<f32>,
6255        resting_potential: Option<f32>,
6256        excitability: Option<f32>,
6257    ) -> BduResult<()> {
6258        // Get NPU
6259        let npu = self
6260            .npu
6261            .as_ref()
6262            .ok_or_else(|| BduError::Internal("NPU not connected".to_string()))?;
6263
6264        let mut npu_lock = npu
6265            .lock()
6266            .map_err(|e| BduError::Internal(format!("Failed to lock NPU: {}", e)))?;
6267
6268        let neuron_id_u32 = neuron_id as u32;
6269
6270        // Verify neuron exists by trying to update at least one property
6271        let mut updated = false;
6272
6273        // Update properties if provided
6274        if let Some(threshold) = firing_threshold {
6275            if npu_lock.update_neuron_threshold(neuron_id_u32, threshold) {
6276                updated = true;
6277                debug!(target: "feagi-bdu","Updated neuron {} firing_threshold = {}", neuron_id, threshold);
6278            } else if !updated {
6279                return Err(BduError::InvalidNeuron(format!(
6280                    "Neuron {} not found",
6281                    neuron_id
6282                )));
6283            }
6284        }
6285
6286        if let Some(leak) = leak_coefficient {
6287            if npu_lock.update_neuron_leak(neuron_id_u32, leak) {
6288                updated = true;
6289                debug!(target: "feagi-bdu","Updated neuron {} leak_coefficient = {}", neuron_id, leak);
6290            } else if !updated {
6291                return Err(BduError::InvalidNeuron(format!(
6292                    "Neuron {} not found",
6293                    neuron_id
6294                )));
6295            }
6296        }
6297
6298        if let Some(resting) = resting_potential {
6299            if npu_lock.update_neuron_resting_potential(neuron_id_u32, resting) {
6300                updated = true;
6301                debug!(target: "feagi-bdu","Updated neuron {} resting_potential = {}", neuron_id, resting);
6302            } else if !updated {
6303                return Err(BduError::InvalidNeuron(format!(
6304                    "Neuron {} not found",
6305                    neuron_id
6306                )));
6307            }
6308        }
6309
6310        if let Some(excit) = excitability {
6311            if npu_lock.update_neuron_excitability(neuron_id_u32, excit) {
6312                updated = true;
6313                debug!(target: "feagi-bdu","Updated neuron {} excitability = {}", neuron_id, excit);
6314            } else if !updated {
6315                return Err(BduError::InvalidNeuron(format!(
6316                    "Neuron {} not found",
6317                    neuron_id
6318                )));
6319            }
6320        }
6321
6322        if !updated {
6323            return Err(BduError::Internal(
6324                "No properties provided for update".to_string(),
6325            ));
6326        }
6327
6328        info!(target: "feagi-bdu","Updated properties for neuron {}", neuron_id);
6329
6330        Ok(())
6331    }
6332
6333    /// Update the firing threshold of a specific neuron
6334    ///
6335    /// # Arguments
6336    ///
6337    /// * `neuron_id` - Target neuron ID
6338    /// * `new_threshold` - New firing threshold value
6339    ///
6340    /// # Returns
6341    ///
6342    /// `Ok(())` if threshold updated successfully
6343    ///
6344    pub fn set_neuron_firing_threshold(
6345        &mut self,
6346        neuron_id: u64,
6347        new_threshold: f32,
6348    ) -> BduResult<()> {
6349        // Get NPU
6350        let npu = self
6351            .npu
6352            .as_ref()
6353            .ok_or_else(|| BduError::Internal("NPU not connected".to_string()))?;
6354
6355        let mut npu_lock = npu
6356            .lock()
6357            .map_err(|e| BduError::Internal(format!("Failed to lock NPU: {}", e)))?;
6358
6359        // Update threshold via NPU
6360        if npu_lock.update_neuron_threshold(neuron_id as u32, new_threshold) {
6361            debug!(target: "feagi-bdu","Set neuron {} firing threshold = {}", neuron_id, new_threshold);
6362            Ok(())
6363        } else {
6364            Err(BduError::InvalidNeuron(format!(
6365                "Neuron {} not found",
6366                neuron_id
6367            )))
6368        }
6369    }
6370
6371    // ========================================================================
6372    // AREA MANAGEMENT & QUERIES
6373    // ========================================================================
6374
6375    /// Get cortical area by name (alternative to ID lookup)
6376    ///
6377    /// # Arguments
6378    ///
6379    /// * `name` - Human-readable area name
6380    ///
6381    /// # Returns
6382    ///
6383    /// `Some(CorticalArea)` if found, `None` otherwise
6384    ///
6385    pub fn get_cortical_area_by_name(&self, name: &str) -> Option<CorticalArea> {
6386        self.cortical_areas
6387            .values()
6388            .find(|area| area.name == name)
6389            .cloned()
6390    }
6391
6392    /// Resize a cortical area (changes dimensions, may require neuron reallocation)
6393    ///
6394    /// # Arguments
6395    ///
6396    /// * `cortical_id` - Target cortical area ID
6397    /// * `new_dimensions` - New dimensions (width, height, depth)
6398    ///
6399    /// # Returns
6400    ///
6401    /// `Ok(())` if resized successfully
6402    ///
6403    /// # Note
6404    ///
6405    /// This does NOT automatically create/delete neurons. It only updates metadata.
6406    /// Caller must handle neuron population separately.
6407    ///
6408    pub fn resize_cortical_area(
6409        &mut self,
6410        cortical_id: &CorticalID,
6411        new_dimensions: CorticalAreaDimensions,
6412    ) -> BduResult<()> {
6413        // Validate dimensions
6414        if new_dimensions.width == 0 || new_dimensions.height == 0 || new_dimensions.depth == 0 {
6415            return Err(BduError::InvalidArea(format!(
6416                "Invalid dimensions: {:?} (all must be > 0)",
6417                new_dimensions
6418            )));
6419        }
6420
6421        // Get and update area
6422        let area = self.cortical_areas.get_mut(cortical_id).ok_or_else(|| {
6423            BduError::InvalidArea(format!("Cortical area {} not found", cortical_id))
6424        })?;
6425
6426        let old_dimensions = area.dimensions;
6427        area.dimensions = new_dimensions;
6428
6429        // Note: Visualization voxel granularity is user-driven, not recalculated on resize
6430        // If user had set a custom value, it remains; otherwise defaults to 1x1x1
6431
6432        info!(target: "feagi-bdu",
6433            "Resized cortical area {} from {:?} to {:?}",
6434            cortical_id,
6435            old_dimensions,
6436            new_dimensions
6437        );
6438
6439        self.refresh_cortical_area_hashes(false, true);
6440
6441        Ok(())
6442    }
6443
6444    /// Get all cortical areas in a brain region
6445    ///
6446    /// # Arguments
6447    ///
6448    /// * `region_id` - Brain region ID
6449    ///
6450    /// # Returns
6451    ///
6452    /// Vector of cortical area IDs in the region
6453    ///
6454    pub fn get_areas_in_region(&self, region_id: &str) -> BduResult<Vec<String>> {
6455        let region = self.brain_regions.get_region(region_id).ok_or_else(|| {
6456            BduError::InvalidArea(format!("Brain region {} not found", region_id))
6457        })?;
6458
6459        // Convert CorticalID to base64 strings
6460        Ok(region
6461            .cortical_areas
6462            .iter()
6463            .map(|id| id.as_base_64())
6464            .collect())
6465    }
6466
6467    /// Update brain region properties
6468    ///
6469    /// # Arguments
6470    ///
6471    /// * `region_id` - Target region ID
6472    /// * `new_name` - Optional new name
6473    /// * `new_description` - Optional new description
6474    ///
6475    /// # Returns
6476    ///
6477    /// `Ok(())` if updated successfully
6478    ///
6479    pub fn update_brain_region(
6480        &mut self,
6481        region_id: &str,
6482        new_name: Option<String>,
6483        new_description: Option<String>,
6484    ) -> BduResult<()> {
6485        let region = self
6486            .brain_regions
6487            .get_region_mut(region_id)
6488            .ok_or_else(|| {
6489                BduError::InvalidArea(format!("Brain region {} not found", region_id))
6490            })?;
6491
6492        if let Some(name) = new_name {
6493            region.name = name;
6494            debug!(target: "feagi-bdu","Updated brain region {} name", region_id);
6495        }
6496
6497        if let Some(desc) = new_description {
6498            // BrainRegion doesn't have a description field in the struct, so we'll store it in properties
6499            region
6500                .properties
6501                .insert("description".to_string(), serde_json::json!(desc));
6502            debug!(target: "feagi-bdu","Updated brain region {} description", region_id);
6503        }
6504
6505        info!(target: "feagi-bdu","Updated brain region {}", region_id);
6506
6507        self.refresh_brain_regions_hash();
6508
6509        Ok(())
6510    }
6511
6512    /// Update brain region properties with generic property map
6513    ///
6514    /// Supports updating any brain region property including coordinates, title, description, etc.
6515    ///
6516    /// # Arguments
6517    ///
6518    /// * `region_id` - Target region ID
6519    /// * `properties` - Map of property names to new values
6520    ///
6521    /// # Returns
6522    ///
6523    /// `Ok(())` if updated successfully
6524    ///
6525    pub fn update_brain_region_properties(
6526        &mut self,
6527        region_id: &str,
6528        properties: std::collections::HashMap<String, serde_json::Value>,
6529    ) -> BduResult<Option<BrainRegionIoRegistry>> {
6530        use tracing::{debug, info};
6531
6532        let should_recompute_io = properties
6533            .contains_key(crate::region_io_designation::DESIGNATED_INPUTS_KEY)
6534            || properties.contains_key(crate::region_io_designation::DESIGNATED_OUTPUTS_KEY);
6535
6536        if properties.contains_key(crate::region_io_designation::DESIGNATED_INPUTS_KEY)
6537            || properties.contains_key(crate::region_io_designation::DESIGNATED_OUTPUTS_KEY)
6538        {
6539            let region_snapshot = self
6540                .brain_regions
6541                .get_region(region_id)
6542                .ok_or_else(|| {
6543                    BduError::InvalidArea(format!("Brain region {} not found", region_id))
6544                })?
6545                .clone();
6546            let (merged_in, merged_out) = crate::region_io_designation::merged_designated_lists(
6547                &region_snapshot,
6548                &properties,
6549            )?;
6550            crate::region_io_designation::validate_merged_designations_against_connectivity(
6551                self,
6552                &region_snapshot,
6553                &merged_in,
6554                &merged_out,
6555            )?;
6556        }
6557
6558        let region = self
6559            .brain_regions
6560            .get_region_mut(region_id)
6561            .ok_or_else(|| {
6562                BduError::InvalidArea(format!("Brain region {} not found", region_id))
6563            })?;
6564
6565        for (key, value) in properties {
6566            match key.as_str() {
6567                // BV (FEAGIRequests.edit_region_object) sends `region_title`; other clients use `title` / `name`.
6568                "title" | "name" | "region_title" => {
6569                    if let Some(name) = value.as_str() {
6570                        region.name = name.to_string();
6571                        debug!(target: "feagi-bdu", "Updated brain region {} name = {}", region_id, name);
6572                    }
6573                }
6574                "coordinate_3d" | "coordinates_3d" => {
6575                    region
6576                        .properties
6577                        .insert("coordinate_3d".to_string(), value.clone());
6578                    debug!(target: "feagi-bdu", "Updated brain region {} coordinate_3d = {:?}", region_id, value);
6579                }
6580                "coordinate_2d" | "coordinates_2d" => {
6581                    region
6582                        .properties
6583                        .insert("coordinate_2d".to_string(), value.clone());
6584                    debug!(target: "feagi-bdu", "Updated brain region {} coordinate_2d = {:?}", region_id, value);
6585                }
6586                "description" => {
6587                    region
6588                        .properties
6589                        .insert("description".to_string(), value.clone());
6590                    debug!(target: "feagi-bdu", "Updated brain region {} description", region_id);
6591                }
6592                "region_type" => {
6593                    if let Some(type_str) = value.as_str() {
6594                        // Note: RegionType is currently a placeholder (Undefined only)
6595                        // Specific region types will be added in the future
6596                        region.region_type = feagi_structures::genomic::RegionType::Undefined;
6597                        debug!(target: "feagi-bdu", "Updated brain region {} type = {}", region_id, type_str);
6598                    }
6599                }
6600                // Store any other properties in the properties map
6601                _ => {
6602                    region.properties.insert(key.clone(), value.clone());
6603                    debug!(target: "feagi-bdu", "Updated brain region {} property {} = {:?}", region_id, key, value);
6604                }
6605            }
6606        }
6607
6608        info!(target: "feagi-bdu", "Updated brain region {} properties", region_id);
6609
6610        // Designated IO affects merged inputs/outputs used by regions_members and BV plates; recompute
6611        // so connectivity-derived and declared lists stay merged in region.properties.
6612        if should_recompute_io {
6613            let registry = self.recompute_brain_region_io_registry()?;
6614            return Ok(Some(registry));
6615        }
6616
6617        // Keep StateManager health hashes in sync so clients (e.g. Brain Visualizer) detect changes via
6618        // brain_regions_hash on the next health poll. Without this, PUT /v1/region/region updates
6619        // (coordinates, title, etc.) do not bump the hash — same as update_brain_region for name/description.
6620        self.refresh_brain_regions_hash();
6621
6622        Ok(None)
6623    }
6624
6625    // ========================================================================
6626    // NEURON QUERY METHODS (P6)
6627    // ========================================================================
6628
6629    /// Get neuron by 3D coordinates within a cortical area
6630    ///
6631    /// # Arguments
6632    ///
6633    /// * `cortical_id` - Cortical area ID
6634    /// * `x` - X coordinate
6635    /// * `y` - Y coordinate
6636    /// * `z` - Z coordinate
6637    ///
6638    /// # Returns
6639    ///
6640    /// `Some(neuron_id)` if found, `None` otherwise
6641    ///
6642    pub fn get_neuron_by_coordinates(
6643        &self,
6644        cortical_id: &CorticalID,
6645        x: u32,
6646        y: u32,
6647        z: u32,
6648    ) -> Option<u64> {
6649        // Get cortical area to get its index
6650        let area = self.get_cortical_area(cortical_id)?;
6651        let cortical_idx = area.cortical_idx;
6652
6653        // Query NPU via public method
6654        let npu = self.npu.as_ref()?;
6655        let npu_lock = npu.lock().ok()?;
6656
6657        npu_lock
6658            .get_neuron_id_at_coordinate(cortical_idx, x, y, z)
6659            .map(|id| id as u64)
6660    }
6661
6662    /// Get the position (coordinates) of a neuron
6663    ///
6664    /// # Arguments
6665    ///
6666    /// * `neuron_id` - Neuron ID
6667    ///
6668    /// # Returns
6669    ///
6670    /// `Some((x, y, z))` if found, `None` otherwise
6671    ///
6672    pub fn get_neuron_position(&self, neuron_id: u64) -> Option<(u32, u32, u32)> {
6673        let npu = self.npu.as_ref()?;
6674        let npu_lock = npu.lock().ok()?;
6675
6676        // Verify neuron exists and get coordinates
6677        let neuron_count = npu_lock.get_neuron_count();
6678        if (neuron_id as usize) >= neuron_count {
6679            return None;
6680        }
6681
6682        Some(
6683            npu_lock
6684                .get_neuron_coordinates(neuron_id as u32)
6685                .unwrap_or((0, 0, 0)),
6686        )
6687    }
6688
6689    /// Get which cortical area contains a specific neuron
6690    ///
6691    /// # Arguments
6692    ///
6693    /// * `neuron_id` - Neuron ID
6694    ///
6695    /// # Returns
6696    ///
6697    /// `Some(cortical_id)` if found, `None` otherwise
6698    ///
6699    pub fn get_cortical_area_for_neuron(&self, neuron_id: u64) -> Option<CorticalID> {
6700        let npu = self.npu.as_ref()?;
6701        let npu_lock = npu.lock().ok()?;
6702
6703        // Verify neuron exists
6704        let neuron_count = npu_lock.get_neuron_count();
6705        if (neuron_id as usize) >= neuron_count {
6706            return None;
6707        }
6708
6709        let cortical_idx = npu_lock.get_neuron_cortical_area(neuron_id as u32)?;
6710
6711        // Look up cortical_id from index
6712        self.cortical_areas
6713            .values()
6714            .find(|area| area.cortical_idx == cortical_idx)
6715            .map(|area| area.cortical_id)
6716    }
6717
6718    /// Get all properties of a neuron
6719    ///
6720    /// # Arguments
6721    ///
6722    /// * `neuron_id` - Neuron ID
6723    ///
6724    /// # Returns
6725    ///
6726    /// `Some(properties)` if found, `None` otherwise
6727    ///
6728    pub fn get_neuron_properties(
6729        &self,
6730        neuron_id: u64,
6731    ) -> Option<std::collections::HashMap<String, serde_json::Value>> {
6732        let npu = self.npu.as_ref()?;
6733        let npu_lock = npu.lock().ok()?;
6734
6735        let neuron_id_u32 = neuron_id as u32;
6736        let idx = neuron_id as usize;
6737
6738        // Verify neuron exists
6739        let neuron_count = npu_lock.get_neuron_count();
6740        if idx >= neuron_count {
6741            return None;
6742        }
6743
6744        let mut properties = std::collections::HashMap::new();
6745
6746        // Basic info
6747        properties.insert("neuron_id".to_string(), serde_json::json!(neuron_id));
6748
6749        // Get coordinates
6750        let (x, y, z) = npu_lock.get_neuron_coordinates(neuron_id_u32)?;
6751        properties.insert("x".to_string(), serde_json::json!(x));
6752        properties.insert("y".to_string(), serde_json::json!(y));
6753        properties.insert("z".to_string(), serde_json::json!(z));
6754
6755        // Get cortical area
6756        let cortical_idx = npu_lock.get_neuron_cortical_area(neuron_id_u32)?;
6757        properties.insert("cortical_area".to_string(), serde_json::json!(cortical_idx));
6758
6759        // Per-neuron dynamics flags + cortical-level propagation flags (synaptic engine).
6760        properties.insert(
6761            "mp_charge_accumulation".to_string(),
6762            serde_json::json!(npu_lock.get_mp_charge_accumulation_at(idx).unwrap_or(false)),
6763        );
6764        properties.insert(
6765            "neuron_type".to_string(),
6766            serde_json::json!(npu_lock.get_neuron_type_at(idx).unwrap_or(0)),
6767        );
6768        let (mp_drv, psp_uni) = self
6769            .cortical_idx_to_id
6770            .get(&cortical_idx)
6771            .map(|cid| {
6772                (
6773                    npu_lock.get_mp_driven_psp_for_cortical(cid),
6774                    npu_lock.get_psp_uniform_distribution_for_cortical(cid),
6775                )
6776            })
6777            .unwrap_or((false, false));
6778        properties.insert("mp_driven_psp".to_string(), serde_json::json!(mp_drv));
6779        properties.insert(
6780            "psp_uniform_distribution".to_string(),
6781            serde_json::json!(psp_uni),
6782        );
6783
6784        // Neuron state: always expose the same keys (stable JSON for clients) even when
6785        // `get_neuron_state` is unavailable (e.g. invalid mask / edge indexing).
6786        let (consec_count, consec_limit, snooze, mp, threshold, refract_countdown) = npu_lock
6787            .get_neuron_state(NeuronId(neuron_id_u32))
6788            .unwrap_or((0u16, 0u16, 0u16, 0.0f32, 0.0f32, 0u16));
6789        properties.insert(
6790            "consecutive_fire_count".to_string(),
6791            serde_json::json!(consec_count),
6792        );
6793        properties.insert(
6794            "consecutive_fire_limit".to_string(),
6795            serde_json::json!(consec_limit),
6796        );
6797        properties.insert("snooze_period".to_string(), serde_json::json!(snooze));
6798        properties.insert("membrane_potential".to_string(), serde_json::json!(mp));
6799        properties.insert("threshold".to_string(), serde_json::json!(threshold));
6800        properties.insert(
6801            "refractory_countdown".to_string(),
6802            serde_json::json!(refract_countdown),
6803        );
6804
6805        // Scalar neuron parameters (stable keys; default when storage omits a value).
6806        properties.insert(
6807            "leak_coefficient".to_string(),
6808            serde_json::json!(npu_lock
6809                .get_neuron_property_by_index(idx, "leak_coefficient")
6810                .unwrap_or(0.0)),
6811        );
6812        properties.insert(
6813            "resting_potential".to_string(),
6814            serde_json::json!(npu_lock
6815                .get_neuron_property_by_index(idx, "resting_potential")
6816                .unwrap_or(0.0)),
6817        );
6818        properties.insert(
6819            "excitability".to_string(),
6820            serde_json::json!(npu_lock
6821                .get_neuron_property_by_index(idx, "excitability")
6822                .unwrap_or(0.0)),
6823        );
6824        properties.insert(
6825            "threshold_limit".to_string(),
6826            serde_json::json!(npu_lock
6827                .get_neuron_property_by_index(idx, "threshold_limit")
6828                .unwrap_or(0.0)),
6829        );
6830        properties.insert(
6831            "refractory_period".to_string(),
6832            serde_json::json!(npu_lock
6833                .get_neuron_property_u16_by_index(idx, "refractory_period")
6834                .unwrap_or(0)),
6835        );
6836
6837        Some(properties)
6838    }
6839
6840    /// Get a specific property of a neuron
6841    ///
6842    /// # Arguments
6843    ///
6844    /// * `neuron_id` - Neuron ID
6845    /// * `property_name` - Name of the property to retrieve
6846    ///
6847    /// # Returns
6848    ///
6849    /// `Some(value)` if found, `None` otherwise
6850    ///
6851    pub fn get_neuron_property(
6852        &self,
6853        neuron_id: u64,
6854        property_name: &str,
6855    ) -> Option<serde_json::Value> {
6856        self.get_neuron_properties(neuron_id)?
6857            .get(property_name)
6858            .cloned()
6859    }
6860
6861    // ========================================================================
6862    // CORTICAL AREA LIST/QUERY METHODS (P6)
6863    // ========================================================================
6864
6865    /// Get all cortical area IDs
6866    ///
6867    /// # Returns
6868    ///
6869    /// Vector of all cortical area IDs
6870    ///
6871    pub fn get_all_cortical_ids(&self) -> Vec<CorticalID> {
6872        self.cortical_areas.keys().copied().collect()
6873    }
6874
6875    /// Get all cortical area indices
6876    ///
6877    /// # Returns
6878    ///
6879    /// Vector of all cortical area indices
6880    ///
6881    pub fn get_all_cortical_indices(&self) -> Vec<u32> {
6882        self.cortical_areas
6883            .values()
6884            .map(|area| area.cortical_idx)
6885            .collect()
6886    }
6887
6888    /// Get all cortical area names
6889    ///
6890    /// # Returns
6891    ///
6892    /// Vector of all cortical area names
6893    ///
6894    pub fn get_cortical_area_names(&self) -> Vec<String> {
6895        self.cortical_areas
6896            .values()
6897            .map(|area| area.name.clone())
6898            .collect()
6899    }
6900
6901    /// List all input (IPU/sensory) cortical areas
6902    ///
6903    /// # Returns
6904    ///
6905    /// Vector of IPU/sensory area IDs
6906    ///
6907    pub fn list_ipu_areas(&self) -> Vec<CorticalID> {
6908        use crate::models::CorticalAreaExt;
6909        self.cortical_areas
6910            .values()
6911            .filter(|area| area.is_input_area())
6912            .map(|area| area.cortical_id)
6913            .collect()
6914    }
6915
6916    /// List all output (OPU/motor) cortical areas
6917    ///
6918    /// # Returns
6919    ///
6920    /// Vector of OPU/motor area IDs
6921    ///
6922    pub fn list_opu_areas(&self) -> Vec<CorticalID> {
6923        use crate::models::CorticalAreaExt;
6924        self.cortical_areas
6925            .values()
6926            .filter(|area| area.is_output_area())
6927            .map(|area| area.cortical_id)
6928            .collect()
6929    }
6930
6931    /// Get maximum dimensions across all cortical areas
6932    ///
6933    /// # Returns
6934    ///
6935    /// (max_width, max_height, max_depth)
6936    ///
6937    pub fn get_max_cortical_area_dimensions(&self) -> (usize, usize, usize) {
6938        self.cortical_areas
6939            .values()
6940            .fold((0, 0, 0), |(max_w, max_h, max_d), area| {
6941                (
6942                    max_w.max(area.dimensions.width as usize),
6943                    max_h.max(area.dimensions.height as usize),
6944                    max_d.max(area.dimensions.depth as usize),
6945                )
6946            })
6947    }
6948
6949    /// Get all properties of a cortical area as a JSON-serializable map
6950    ///
6951    /// # Arguments
6952    ///
6953    /// * `cortical_id` - Cortical area ID
6954    ///
6955    /// # Returns
6956    ///
6957    /// `Some(properties)` if found, `None` otherwise
6958    ///
6959    pub fn get_cortical_area_properties(
6960        &self,
6961        cortical_id: &CorticalID,
6962    ) -> Option<std::collections::HashMap<String, serde_json::Value>> {
6963        let area = self.get_cortical_area(cortical_id)?;
6964
6965        let mut properties = std::collections::HashMap::new();
6966        properties.insert(
6967            "cortical_id".to_string(),
6968            serde_json::json!(area.cortical_id),
6969        );
6970        properties.insert(
6971            "cortical_id_s".to_string(),
6972            serde_json::json!(area.cortical_id.to_string()),
6973        );
6974        properties.insert(
6975            "cortical_idx".to_string(),
6976            serde_json::json!(area.cortical_idx),
6977        );
6978        properties.insert("name".to_string(), serde_json::json!(area.name));
6979        use crate::models::CorticalAreaExt;
6980        properties.insert(
6981            "area_type".to_string(),
6982            serde_json::json!(area.get_cortical_group()),
6983        );
6984        properties.insert(
6985            "dimensions".to_string(),
6986            serde_json::json!({
6987                "width": area.dimensions.width,
6988                "height": area.dimensions.height,
6989                "depth": area.dimensions.depth,
6990            }),
6991        );
6992        properties.insert("position".to_string(), serde_json::json!(area.position));
6993
6994        // Copy all properties from area.properties to the response
6995        for (key, value) in &area.properties {
6996            properties.insert(key.clone(), value.clone());
6997        }
6998
6999        // Add custom properties
7000        properties.extend(area.properties.clone());
7001
7002        Some(properties)
7003    }
7004
7005    /// Get properties of all cortical areas
7006    ///
7007    /// # Returns
7008    ///
7009    /// Vector of property maps for all areas
7010    ///
7011    pub fn get_all_cortical_area_properties(
7012        &self,
7013    ) -> Vec<std::collections::HashMap<String, serde_json::Value>> {
7014        self.cortical_areas
7015            .keys()
7016            .filter_map(|id| self.get_cortical_area_properties(id))
7017            .collect()
7018    }
7019
7020    // ========================================================================
7021    // BRAIN REGION QUERY METHODS (P6)
7022    // ========================================================================
7023
7024    /// Get all brain region IDs
7025    ///
7026    /// # Returns
7027    ///
7028    /// Vector of all brain region IDs
7029    ///
7030    pub fn get_all_brain_region_ids(&self) -> Vec<String> {
7031        self.brain_regions
7032            .get_all_region_ids()
7033            .into_iter()
7034            .cloned()
7035            .collect()
7036    }
7037
7038    /// Get all brain region names
7039    ///
7040    /// # Returns
7041    ///
7042    /// Vector of all brain region names
7043    ///
7044    pub fn get_brain_region_names(&self) -> Vec<String> {
7045        self.brain_regions
7046            .get_all_region_ids()
7047            .iter()
7048            .filter_map(|id| {
7049                self.brain_regions
7050                    .get_region(id)
7051                    .map(|region| region.name.clone())
7052            })
7053            .collect()
7054    }
7055
7056    /// Get properties of a brain region
7057    ///
7058    /// # Arguments
7059    ///
7060    /// * `region_id` - Brain region ID
7061    ///
7062    /// # Returns
7063    ///
7064    /// `Some(properties)` if found, `None` otherwise
7065    ///
7066    pub fn get_brain_region_properties(
7067        &self,
7068        region_id: &str,
7069    ) -> Option<std::collections::HashMap<String, serde_json::Value>> {
7070        let region = self.brain_regions.get_region(region_id)?;
7071
7072        let mut properties = std::collections::HashMap::new();
7073        properties.insert("region_id".to_string(), serde_json::json!(region.region_id));
7074        properties.insert("name".to_string(), serde_json::json!(region.name));
7075        properties.insert(
7076            "region_type".to_string(),
7077            serde_json::json!(format!("{:?}", region.region_type)),
7078        );
7079        properties.insert(
7080            "cortical_areas".to_string(),
7081            serde_json::json!(region.cortical_areas.iter().collect::<Vec<_>>()),
7082        );
7083
7084        // Add custom properties
7085        properties.extend(region.properties.clone());
7086
7087        Some(properties)
7088    }
7089
7090    /// Check if a cortical area exists
7091    ///
7092    /// # Arguments
7093    ///
7094    /// * `cortical_id` - Cortical area ID to check
7095    ///
7096    /// # Returns
7097    ///
7098    /// `true` if area exists, `false` otherwise
7099    ///
7100    pub fn cortical_area_exists(&self, cortical_id: &CorticalID) -> bool {
7101        self.cortical_areas.contains_key(cortical_id)
7102    }
7103
7104    /// Check if a brain region exists
7105    ///
7106    /// # Arguments
7107    ///
7108    /// * `region_id` - Brain region ID to check
7109    ///
7110    /// # Returns
7111    ///
7112    /// `true` if region exists, `false` otherwise
7113    ///
7114    pub fn brain_region_exists(&self, region_id: &str) -> bool {
7115        self.brain_regions.get_region(region_id).is_some()
7116    }
7117
7118    /// Get the total number of brain regions
7119    ///
7120    /// # Returns
7121    ///
7122    /// Number of brain regions
7123    ///
7124    pub fn get_brain_region_count(&self) -> usize {
7125        self.brain_regions.region_count()
7126    }
7127
7128    /// Get neurons by cortical area (alias for get_neurons_in_area for API compatibility)
7129    ///
7130    /// # Arguments
7131    ///
7132    /// * `cortical_id` - Cortical area ID
7133    ///
7134    /// # Returns
7135    ///
7136    /// Vector of neuron IDs in the area
7137    ///
7138    pub fn get_neurons_by_cortical_area(&self, cortical_id: &CorticalID) -> Vec<u64> {
7139        // This is an alias for get_neurons_in_area, which already exists
7140        // Keeping it for Python API compatibility
7141        // Note: The signature says Vec<NeuronId> but implementation returns Vec<u64>
7142        self.get_neurons_in_area(cortical_id)
7143    }
7144}
7145
7146// Manual Debug implementation (RustNPU doesn't implement Debug)
7147impl std::fmt::Debug for ConnectomeManager {
7148    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
7149        f.debug_struct("ConnectomeManager")
7150            .field("cortical_areas", &self.cortical_areas.len())
7151            .field("next_cortical_idx", &self.next_cortical_idx)
7152            .field("brain_regions", &self.brain_regions)
7153            .field(
7154                "npu",
7155                &if self.npu.is_some() {
7156                    "Connected"
7157                } else {
7158                    "Not connected"
7159                },
7160            )
7161            .field("initialized", &self.initialized)
7162            .finish()
7163    }
7164}
7165
7166#[cfg(test)]
7167mod tests {
7168    use super::*;
7169    use feagi_structures::genomic::cortical_area::CoreCorticalType;
7170
7171    #[test]
7172    fn test_singleton_instance() {
7173        let instance1 = ConnectomeManager::instance();
7174        let instance2 = ConnectomeManager::instance();
7175
7176        // Both should point to the same instance
7177        assert_eq!(Arc::strong_count(&instance1), Arc::strong_count(&instance2));
7178    }
7179
7180    #[test]
7181    fn test_add_cortical_area() {
7182        ConnectomeManager::reset_for_testing();
7183
7184        let instance = ConnectomeManager::instance();
7185        let mut manager = instance.write();
7186
7187        use feagi_structures::genomic::cortical_area::{
7188            CorticalAreaType, IOCorticalAreaConfigurationFlag,
7189        };
7190        let cortical_id = CorticalID::try_from_bytes(b"cst_add_").unwrap(); // Use unique custom ID
7191        let cortical_type = CorticalAreaType::BrainInput(IOCorticalAreaConfigurationFlag::Boolean);
7192        let area = CorticalArea::new(
7193            cortical_id,
7194            0,
7195            "Visual Input".to_string(),
7196            CorticalAreaDimensions::new(128, 128, 20).unwrap(),
7197            (0, 0, 0).into(),
7198            cortical_type,
7199        )
7200        .unwrap();
7201
7202        let initial_count = manager.get_cortical_area_count();
7203        let _cortical_idx = manager.add_cortical_area(area).unwrap();
7204
7205        assert_eq!(manager.get_cortical_area_count(), initial_count + 1);
7206        assert!(manager.has_cortical_area(&cortical_id));
7207        assert!(manager.is_initialized());
7208    }
7209
7210    #[test]
7211    fn test_cortical_area_lookups() {
7212        ConnectomeManager::reset_for_testing();
7213
7214        let instance = ConnectomeManager::instance();
7215        let mut manager = instance.write();
7216
7217        use feagi_structures::genomic::cortical_area::{
7218            CorticalAreaType, IOCorticalAreaConfigurationFlag,
7219        };
7220        let cortical_id = CorticalID::try_from_bytes(b"cst_look").unwrap(); // Use unique custom ID
7221        let cortical_type = CorticalAreaType::BrainInput(IOCorticalAreaConfigurationFlag::Boolean);
7222        let area = CorticalArea::new(
7223            cortical_id,
7224            0,
7225            "Test Area".to_string(),
7226            CorticalAreaDimensions::new(10, 10, 10).unwrap(),
7227            (0, 0, 0).into(),
7228            cortical_type,
7229        )
7230        .unwrap();
7231
7232        let cortical_idx = manager.add_cortical_area(area).unwrap();
7233
7234        // ID -> idx lookup
7235        assert_eq!(manager.get_cortical_idx(&cortical_id), Some(cortical_idx));
7236
7237        // idx -> ID lookup
7238        assert_eq!(manager.get_cortical_id(cortical_idx), Some(&cortical_id));
7239
7240        // Get area
7241        let retrieved_area = manager.get_cortical_area(&cortical_id).unwrap();
7242        assert_eq!(retrieved_area.name, "Test Area");
7243    }
7244
7245    #[test]
7246    fn test_remove_cortical_area() {
7247        ConnectomeManager::reset_for_testing();
7248
7249        let instance = ConnectomeManager::instance();
7250        let mut manager = instance.write();
7251
7252        use feagi_structures::genomic::cortical_area::{
7253            CorticalAreaType, IOCorticalAreaConfigurationFlag,
7254        };
7255        let cortical_id = CoreCorticalType::Power.to_cortical_id();
7256
7257        // Remove area if it already exists from previous tests
7258        if manager.has_cortical_area(&cortical_id) {
7259            manager.remove_cortical_area(&cortical_id).unwrap();
7260        }
7261
7262        let cortical_type = CorticalAreaType::BrainInput(IOCorticalAreaConfigurationFlag::Boolean);
7263        let area = CorticalArea::new(
7264            cortical_id,
7265            0,
7266            "Test".to_string(),
7267            CorticalAreaDimensions::new(10, 10, 10).unwrap(),
7268            (0, 0, 0).into(),
7269            cortical_type,
7270        )
7271        .unwrap();
7272
7273        let initial_count = manager.get_cortical_area_count();
7274        manager.add_cortical_area(area).unwrap();
7275        assert_eq!(manager.get_cortical_area_count(), initial_count + 1);
7276
7277        manager.remove_cortical_area(&cortical_id).unwrap();
7278        assert_eq!(manager.get_cortical_area_count(), initial_count);
7279        assert!(!manager.has_cortical_area(&cortical_id));
7280    }
7281
7282    #[test]
7283    fn test_duplicate_area_error() {
7284        ConnectomeManager::reset_for_testing();
7285
7286        let instance = ConnectomeManager::instance();
7287        let mut manager = instance.write();
7288
7289        use feagi_structures::genomic::cortical_area::{
7290            CorticalAreaType, IOCorticalAreaConfigurationFlag,
7291        };
7292        // Use a unique ID only for this test to avoid collisions with other tests (e.g. Power)
7293        // when tests run in parallel; we still test duplicate by adding the same ID twice.
7294        let cortical_id = CorticalID::try_from_bytes(b"cst_dup1").unwrap();
7295        let cortical_type = CorticalAreaType::BrainInput(IOCorticalAreaConfigurationFlag::Boolean);
7296        let area1 = CorticalArea::new(
7297            cortical_id,
7298            0,
7299            "First".to_string(),
7300            CorticalAreaDimensions::new(10, 10, 10).unwrap(),
7301            (0, 0, 0).into(),
7302            cortical_type,
7303        )
7304        .unwrap();
7305
7306        let area2 = CorticalArea::new(
7307            cortical_id, // Same ID - duplicate
7308            1,
7309            "Second".to_string(),
7310            CorticalAreaDimensions::new(10, 10, 10).unwrap(),
7311            (0, 0, 0).into(),
7312            cortical_type,
7313        )
7314        .unwrap();
7315
7316        manager.add_cortical_area(area1).unwrap();
7317        let result = manager.add_cortical_area(area2);
7318
7319        assert!(result.is_err());
7320    }
7321
7322    #[test]
7323    fn test_brain_region_management() {
7324        ConnectomeManager::reset_for_testing();
7325
7326        let instance = ConnectomeManager::instance();
7327        let mut manager = instance.write();
7328
7329        let region_id = feagi_structures::genomic::brain_regions::RegionID::new();
7330        let region_id_str = region_id.to_string();
7331        let root = BrainRegion::new(
7332            region_id,
7333            "Root".to_string(),
7334            feagi_structures::genomic::brain_regions::RegionType::Undefined,
7335        )
7336        .unwrap();
7337
7338        let initial_count = manager.get_brain_region_ids().len();
7339        manager.add_brain_region(root, None).unwrap();
7340
7341        assert_eq!(manager.get_brain_region_ids().len(), initial_count + 1);
7342        assert!(manager.get_brain_region(&region_id_str).is_some());
7343    }
7344
7345    #[test]
7346    fn test_synapse_operations() {
7347        use feagi_npu_burst_engine::npu::RustNPU;
7348        use feagi_npu_burst_engine::TracingMutex;
7349        use std::sync::Arc;
7350
7351        // Create NPU and manager for isolated test state
7352        use feagi_npu_burst_engine::backend::CPUBackend;
7353        use feagi_npu_burst_engine::DynamicNPU;
7354        use feagi_npu_runtime::StdRuntime;
7355
7356        let runtime = StdRuntime;
7357        let backend = CPUBackend::new();
7358        let npu_result =
7359            RustNPU::new(runtime, backend, 100, 1000, 10).expect("Failed to create NPU");
7360        let npu = Arc::new(TracingMutex::new(DynamicNPU::F32(npu_result), "TestNPU"));
7361        let mut manager = ConnectomeManager::new_for_testing_with_npu(npu.clone());
7362
7363        // First create a cortical area to add neurons to
7364        use feagi_structures::genomic::cortical_area::{
7365            CorticalAreaType, IOCorticalAreaConfigurationFlag,
7366        };
7367        let cortical_id = CorticalID::try_from_bytes(b"cst_syn_").unwrap(); // Use unique custom ID
7368        let cortical_type = CorticalAreaType::BrainInput(IOCorticalAreaConfigurationFlag::Boolean);
7369        let area = CorticalArea::new(
7370            cortical_id,
7371            0, // cortical_idx
7372            "Test Area".to_string(),
7373            CorticalAreaDimensions::new(10, 10, 1).unwrap(),
7374            (0, 0, 0).into(), // position
7375            cortical_type,
7376        )
7377        .unwrap();
7378        let cortical_idx = manager.add_cortical_area(area).unwrap();
7379
7380        // Register the cortical area with the NPU using the cortical ID's base64 representation
7381        if let Some(npu_arc) = manager.get_npu() {
7382            if let Ok(mut npu_guard) = npu_arc.try_lock() {
7383                if let DynamicNPU::F32(ref mut npu) = *npu_guard {
7384                    npu.register_cortical_area(cortical_idx, cortical_id.as_base_64());
7385                }
7386            }
7387        }
7388
7389        // Create two neurons
7390        let neuron1_id = manager
7391            .add_neuron(
7392                &cortical_id,
7393                0,
7394                0,
7395                0,     // coordinates
7396                100.0, // firing_threshold
7397                0.0,   // firing_threshold_limit (0 = no limit)
7398                0.1,   // leak_coefficient
7399                -60.0, // resting_potential
7400                0,     // neuron_type
7401                2,     // refractory_period
7402                1.0,   // excitability
7403                5,     // consecutive_fire_limit
7404                10,    // snooze_length
7405                false, // mp_charge_accumulation
7406            )
7407            .unwrap();
7408
7409        let neuron2_id = manager
7410            .add_neuron(
7411                &cortical_id,
7412                1,
7413                0,
7414                0, // coordinates
7415                100.0,
7416                f32::MAX, // firing_threshold_limit (MAX = no limit, SIMD-friendly encoding)
7417                0.1,
7418                -60.0,
7419                0,
7420                2,
7421                1.0,
7422                5,
7423                10,
7424                false,
7425            )
7426            .unwrap();
7427
7428        // Test create_synapse (creation should succeed)
7429        manager
7430            .create_synapse(
7431                neuron1_id, neuron2_id, 128.0, // weight
7432                64.0,  // psp
7433                0,     // excitatory
7434            )
7435            .unwrap();
7436
7437        // Note: Synapse retrieval/update/removal tests require full NPU propagation engine initialization
7438        // which is beyond the scope of this unit test. The important part is that create_synapse succeeds.
7439        println!("✅ Synapse creation test passed");
7440    }
7441
7442    #[test]
7443    fn test_apply_cortical_mapping_missing_rules_is_ok() {
7444        // This guards against a regression where deleting a mapping causes a 500 because
7445        // synapse regeneration treats "no mapping rules" as an error.
7446        let mut manager = ConnectomeManager::new_for_testing();
7447
7448        use feagi_structures::genomic::cortical_area::{
7449            CorticalAreaType, IOCorticalAreaConfigurationFlag,
7450        };
7451
7452        let src_id = CorticalID::try_from_bytes(b"map_src_").unwrap();
7453        let dst_id = CorticalID::try_from_bytes(b"map_dst_").unwrap();
7454
7455        let src_area = CorticalArea::new(
7456            src_id,
7457            0,
7458            "src".to_string(),
7459            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
7460            (0, 0, 0).into(),
7461            CorticalAreaType::BrainInput(IOCorticalAreaConfigurationFlag::Boolean),
7462        )
7463        .unwrap();
7464
7465        let dst_area = CorticalArea::new(
7466            dst_id,
7467            1,
7468            "dst".to_string(),
7469            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
7470            (0, 0, 0).into(),
7471            CorticalAreaType::BrainOutput(IOCorticalAreaConfigurationFlag::Boolean),
7472        )
7473        .unwrap();
7474
7475        manager.add_cortical_area(src_area).unwrap();
7476        manager.add_cortical_area(dst_area).unwrap();
7477
7478        // No cortical_mapping_dst property set -> should be Ok(0), not an error
7479        let count = manager
7480            .apply_cortical_mapping_for_pair(&src_id, &dst_id)
7481            .unwrap();
7482        assert_eq!(count, 0);
7483
7484        // Now create then delete mapping; missing destination rules should still be Ok(0)
7485        manager
7486            .update_cortical_mapping(
7487                &src_id,
7488                &dst_id,
7489                vec![serde_json::json!({"morphology_id":"m1"})],
7490            )
7491            .unwrap();
7492        manager
7493            .update_cortical_mapping(&src_id, &dst_id, vec![])
7494            .unwrap();
7495
7496        let count2 = manager
7497            .apply_cortical_mapping_for_pair(&src_id, &dst_id)
7498            .unwrap();
7499        assert_eq!(count2, 0);
7500    }
7501
7502    #[test]
7503    fn test_get_mapping_rules_for_destination_supports_legacy_key() {
7504        let dst_id = CorticalID::try_from_bytes(b"csrc0002").unwrap();
7505        let mapping_dst = serde_json::json!({
7506            "csrc0002": [
7507                {"morphology_id": "m1"}
7508            ]
7509        });
7510        let mapping_obj = mapping_dst.as_object().expect("mapping must be an object");
7511
7512        let rules = ConnectomeManager::get_mapping_rules_for_destination(mapping_obj, &dst_id)
7513            .expect("legacy destination key should resolve");
7514        assert_eq!(rules.len(), 1);
7515        assert_eq!(
7516            rules[0].get("morphology_id").and_then(|v| v.as_str()),
7517            Some("m1")
7518        );
7519    }
7520
7521    #[test]
7522    fn test_get_neuron_properties_always_includes_neuron_state_keys() {
7523        use feagi_npu_burst_engine::backend::CPUBackend;
7524        use feagi_npu_burst_engine::RustNPU;
7525        use feagi_npu_burst_engine::TracingMutex;
7526        use feagi_npu_runtime::StdRuntime;
7527        use feagi_structures::genomic::cortical_area::{
7528            CorticalAreaDimensions, CorticalAreaType, IOCorticalAreaConfigurationFlag,
7529        };
7530        use std::sync::Arc;
7531
7532        let runtime = StdRuntime;
7533        let backend = CPUBackend::new();
7534        let npu = RustNPU::new(runtime, backend, 10_000, 10_000, 10).expect("Failed to create NPU");
7535        let dyn_npu = Arc::new(TracingMutex::new(
7536            feagi_npu_burst_engine::DynamicNPU::F32(npu),
7537            "TestNPU",
7538        ));
7539        let mut manager = ConnectomeManager::new_for_testing_with_npu(dyn_npu.clone());
7540
7541        let area_id = CorticalID::try_from_bytes(b"cst_nsp_").unwrap();
7542        let area = CorticalArea::new(
7543            area_id,
7544            0,
7545            "n".to_string(),
7546            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
7547            (0, 0, 0).into(),
7548            CorticalAreaType::BrainInput(IOCorticalAreaConfigurationFlag::Boolean),
7549        )
7550        .unwrap();
7551
7552        manager.add_cortical_area(area).unwrap();
7553        let nid = manager
7554            .add_neuron(
7555                &area_id, 0, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false,
7556            )
7557            .unwrap();
7558
7559        let props = manager
7560            .get_neuron_properties(nid)
7561            .expect("neuron properties");
7562        for key in [
7563            "consecutive_fire_count",
7564            "consecutive_fire_limit",
7565            "snooze_period",
7566            "membrane_potential",
7567            "threshold",
7568            "refractory_countdown",
7569            "mp_charge_accumulation",
7570            "neuron_type",
7571            "mp_driven_psp",
7572            "psp_uniform_distribution",
7573            "leak_coefficient",
7574            "resting_potential",
7575            "excitability",
7576            "threshold_limit",
7577            "refractory_period",
7578        ] {
7579            assert!(props.contains_key(key), "missing neuron state key: {key}");
7580        }
7581    }
7582
7583    #[test]
7584    fn test_mapping_deletion_prunes_synapses_between_areas() {
7585        use feagi_npu_burst_engine::backend::CPUBackend;
7586        use feagi_npu_burst_engine::RustNPU;
7587        use feagi_npu_burst_engine::TracingMutex;
7588        use feagi_npu_runtime::StdRuntime;
7589        use feagi_structures::genomic::cortical_area::{
7590            CorticalAreaDimensions, CorticalAreaType, IOCorticalAreaConfigurationFlag,
7591        };
7592        use std::sync::Arc;
7593
7594        // Create NPU and manager (small capacities for a deterministic unit test)
7595        let runtime = StdRuntime;
7596        let backend = CPUBackend::new();
7597        let npu = RustNPU::new(runtime, backend, 10_000, 10_000, 10).expect("Failed to create NPU");
7598        let dyn_npu = Arc::new(TracingMutex::new(
7599            feagi_npu_burst_engine::DynamicNPU::F32(npu),
7600            "TestNPU",
7601        ));
7602        let mut manager = ConnectomeManager::new_for_testing_with_npu(dyn_npu.clone());
7603
7604        // Create two cortical areas
7605        let src_id = CorticalID::try_from_bytes(b"cst_src_").unwrap();
7606        let dst_id = CorticalID::try_from_bytes(b"cst_dst_").unwrap();
7607
7608        let src_area = CorticalArea::new(
7609            src_id,
7610            0,
7611            "src".to_string(),
7612            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
7613            (0, 0, 0).into(),
7614            CorticalAreaType::BrainInput(IOCorticalAreaConfigurationFlag::Boolean),
7615        )
7616        .unwrap();
7617        let dst_area = CorticalArea::new(
7618            dst_id,
7619            1,
7620            "dst".to_string(),
7621            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
7622            (0, 0, 0).into(),
7623            CorticalAreaType::BrainOutput(IOCorticalAreaConfigurationFlag::Boolean),
7624        )
7625        .unwrap();
7626
7627        manager.add_cortical_area(src_area).unwrap();
7628        manager.add_cortical_area(dst_area).unwrap();
7629
7630        // Add a couple neurons to each area
7631        let s0 = manager
7632            .add_neuron(&src_id, 0, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false)
7633            .unwrap();
7634        let s1 = manager
7635            .add_neuron(&src_id, 1, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false)
7636            .unwrap();
7637        let t0 = manager
7638            .add_neuron(&dst_id, 0, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false)
7639            .unwrap();
7640        let t1 = manager
7641            .add_neuron(&dst_id, 1, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false)
7642            .unwrap();
7643
7644        // Create synapses that represent an established mapping between the two areas
7645        manager.create_synapse(s0, t0, 128.0, 200.0, 0).unwrap();
7646        manager.create_synapse(s1, t1, 128.0, 200.0, 0).unwrap();
7647
7648        // Build index once before pruning
7649        {
7650            let mut npu = dyn_npu.lock().unwrap();
7651            npu.rebuild_synapse_index();
7652            assert_eq!(npu.get_synapse_count(), 2);
7653        }
7654
7655        // Simulate mapping deletion and regeneration: should prune synapses and not re-add any
7656        manager
7657            .update_cortical_mapping(&src_id, &dst_id, vec![])
7658            .unwrap();
7659        let created = manager
7660            .regenerate_synapses_for_mapping(&src_id, &dst_id)
7661            .unwrap();
7662        assert_eq!(created, 0);
7663
7664        // Verify synapses are gone (invalidated) and no outgoing synapses remain from the sources
7665        {
7666            let mut npu = dyn_npu.lock().unwrap();
7667            // Pruning invalidates synapses; rebuild the index so counts/outgoing queries reflect the current state.
7668            npu.rebuild_synapse_index();
7669            assert_eq!(npu.get_synapse_count(), 0);
7670            assert!(npu.get_outgoing_synapses(s0 as u32).is_empty());
7671            assert!(npu.get_outgoing_synapses(s1 as u32).is_empty());
7672        }
7673    }
7674
7675    #[test]
7676    fn test_mapping_update_prunes_synapses_between_areas() {
7677        use feagi_npu_burst_engine::backend::CPUBackend;
7678        use feagi_npu_burst_engine::RustNPU;
7679        use feagi_npu_burst_engine::TracingMutex;
7680        use feagi_npu_runtime::StdRuntime;
7681        use feagi_structures::genomic::cortical_area::{
7682            CorticalAreaDimensions, CorticalAreaType, IOCorticalAreaConfigurationFlag,
7683        };
7684        use std::sync::Arc;
7685
7686        // Create NPU and manager (small capacities for a deterministic unit test)
7687        let runtime = StdRuntime;
7688        let backend = CPUBackend::new();
7689        let npu = RustNPU::new(runtime, backend, 10_000, 10_000, 10).expect("Failed to create NPU");
7690        let dyn_npu = Arc::new(TracingMutex::new(
7691            feagi_npu_burst_engine::DynamicNPU::F32(npu),
7692            "TestNPU",
7693        ));
7694        let mut manager = ConnectomeManager::new_for_testing_with_npu(dyn_npu.clone());
7695
7696        // Seed core morphologies so mapping regeneration can resolve function morphologies (e.g. "episodic_memory").
7697        feagi_evolutionary::templates::add_core_morphologies(&mut manager.morphology_registry);
7698
7699        // Create two cortical areas
7700        // Use valid custom cortical IDs (the `cst...` namespace).
7701        let src_id = CorticalID::try_from_bytes(b"cstupds1").unwrap();
7702        let dst_id = CorticalID::try_from_bytes(b"cstupdt1").unwrap();
7703
7704        let src_area = CorticalArea::new(
7705            src_id,
7706            0,
7707            "src".to_string(),
7708            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
7709            (0, 0, 0).into(),
7710            CorticalAreaType::BrainInput(IOCorticalAreaConfigurationFlag::Boolean),
7711        )
7712        .unwrap();
7713        let dst_area = CorticalArea::new(
7714            dst_id,
7715            0,
7716            "dst".to_string(),
7717            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
7718            (0, 0, 0).into(),
7719            CorticalAreaType::BrainOutput(IOCorticalAreaConfigurationFlag::Boolean),
7720        )
7721        .unwrap();
7722
7723        manager.add_cortical_area(src_area).unwrap();
7724        manager.add_cortical_area(dst_area).unwrap();
7725
7726        // Add a couple neurons to each area
7727        let s0 = manager
7728            .add_neuron(&src_id, 0, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false)
7729            .unwrap();
7730        let s1 = manager
7731            .add_neuron(&src_id, 1, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false)
7732            .unwrap();
7733        let t0 = manager
7734            .add_neuron(&dst_id, 0, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false)
7735            .unwrap();
7736        let t1 = manager
7737            .add_neuron(&dst_id, 1, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false)
7738            .unwrap();
7739
7740        // Create synapses that represent an established mapping between the two areas
7741        manager.create_synapse(s0, t0, 128.0, 200.0, 0).unwrap();
7742        manager.create_synapse(s1, t1, 128.0, 200.0, 0).unwrap();
7743
7744        // Build index once before pruning
7745        {
7746            let mut npu = dyn_npu.lock().unwrap();
7747            npu.rebuild_synapse_index();
7748            assert_eq!(npu.get_synapse_count(), 2);
7749        }
7750
7751        // Update mapping rules (non-empty) and regenerate.
7752        // This should prune the existing A→B synapses before re-applying the mapping.
7753        //
7754        // Use "episodic_memory" morphology to avoid creating physical synapses; the key assertion is that
7755        // the pre-existing synapses were pruned on update.
7756        manager
7757            .update_cortical_mapping(
7758                &src_id,
7759                &dst_id,
7760                vec![serde_json::json!({
7761                    "morphology_id": "episodic_memory",
7762                    "morphology_scalar": [1],
7763                    "postSynapticCurrent_multiplier": 1,
7764                    "plasticity_flag": false,
7765                    "plasticity_constant": 0,
7766                    "ltp_multiplier": 0,
7767                    "ltd_multiplier": 0,
7768                    "plasticity_window": 0,
7769                })],
7770            )
7771            .unwrap();
7772        let created = manager
7773            .regenerate_synapses_for_mapping(&src_id, &dst_id)
7774            .unwrap();
7775        assert_eq!(created, 0);
7776
7777        // Verify synapses are gone and no outgoing synapses remain from the sources
7778        {
7779            let mut npu = dyn_npu.lock().unwrap();
7780            // Pruning invalidates synapses; rebuild the index so counts/outgoing queries reflect the current state.
7781            npu.rebuild_synapse_index();
7782            assert_eq!(npu.get_synapse_count(), 0);
7783            assert!(npu.get_outgoing_synapses(s0 as u32).is_empty());
7784            assert!(npu.get_outgoing_synapses(s1 as u32).is_empty());
7785        }
7786    }
7787
7788    #[test]
7789    fn test_upstream_area_tracking() {
7790        // Test that upstream_cortical_areas property is maintained correctly
7791        use crate::models::cortical_area::CorticalArea;
7792        use feagi_npu_burst_engine::backend::CPUBackend;
7793        use feagi_npu_burst_engine::TracingMutex;
7794        use feagi_npu_burst_engine::{DynamicNPU, RustNPU};
7795        use feagi_npu_runtime::StdRuntime;
7796        use feagi_structures::genomic::cortical_area::{
7797            CorticalAreaDimensions, CorticalAreaType, CorticalID,
7798        };
7799
7800        // Create test manager with NPU
7801        let runtime = StdRuntime;
7802        let backend = CPUBackend::new();
7803        let npu = RustNPU::new(runtime, backend, 10_000, 10_000, 10).expect("Failed to create NPU");
7804        let dyn_npu = Arc::new(TracingMutex::new(DynamicNPU::F32(npu), "TestNPU"));
7805        let mut manager = ConnectomeManager::new_for_testing_with_npu(dyn_npu.clone());
7806
7807        // Seed the morphology registry with core morphologies so mapping regeneration can run.
7808        // (new_for_testing_with_npu() intentionally starts empty.)
7809        feagi_evolutionary::templates::add_core_morphologies(&mut manager.morphology_registry);
7810
7811        // Create source area
7812        let src_id = CorticalID::try_from_bytes(b"csrc0000").unwrap();
7813        let src_area = CorticalArea::new(
7814            src_id,
7815            0,
7816            "Source Area".to_string(),
7817            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
7818            (0, 0, 0).into(),
7819            CorticalAreaType::Custom(
7820                feagi_structures::genomic::cortical_area::CustomCorticalType::LeakyIntegrateFire,
7821            ),
7822        )
7823        .unwrap();
7824        let src_idx = manager.add_cortical_area(src_area).unwrap();
7825
7826        // Create destination area (memory area)
7827        let dst_id = CorticalID::try_from_bytes(b"cdst0000").unwrap();
7828        let dst_area = CorticalArea::new(
7829            dst_id,
7830            0,
7831            "Dest Area".to_string(),
7832            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
7833            (0, 0, 0).into(),
7834            CorticalAreaType::Custom(
7835                feagi_structures::genomic::cortical_area::CustomCorticalType::LeakyIntegrateFire,
7836            ),
7837        )
7838        .unwrap();
7839        manager.add_cortical_area(dst_area).unwrap();
7840
7841        // Verify upstream_cortical_areas property was initialized to empty array
7842        {
7843            let dst_area = manager.get_cortical_area(&dst_id).unwrap();
7844            let upstream = dst_area.properties.get("upstream_cortical_areas").unwrap();
7845            assert!(
7846                upstream.as_array().unwrap().is_empty(),
7847                "Upstream areas should be empty initially"
7848            );
7849        }
7850
7851        // Create a mapping from src to dst
7852        let mapping_data = vec![serde_json::json!({
7853            "morphology_id": "episodic_memory",
7854            "morphology_scalar": 1,
7855            "postSynapticCurrent_multiplier": 1.0,
7856        })];
7857        manager
7858            .update_cortical_mapping(&src_id, &dst_id, mapping_data)
7859            .unwrap();
7860        manager
7861            .regenerate_synapses_for_mapping(&src_id, &dst_id)
7862            .unwrap();
7863
7864        // Verify src_idx was added to dst's upstream_cortical_areas
7865        {
7866            let upstream_areas = manager.get_upstream_cortical_areas(&dst_id);
7867            assert_eq!(upstream_areas.len(), 1, "Should have 1 upstream area");
7868            assert_eq!(
7869                upstream_areas[0], src_idx,
7870                "Upstream area should be src_idx"
7871            );
7872        }
7873
7874        // Delete the mapping
7875        manager
7876            .update_cortical_mapping(&src_id, &dst_id, vec![])
7877            .unwrap();
7878        manager
7879            .regenerate_synapses_for_mapping(&src_id, &dst_id)
7880            .unwrap();
7881
7882        // Verify src_idx was removed from dst's upstream_cortical_areas
7883        {
7884            let upstream_areas = manager.get_upstream_cortical_areas(&dst_id);
7885            assert_eq!(
7886                upstream_areas.len(),
7887                0,
7888                "Should have 0 upstream areas after deletion"
7889            );
7890        }
7891    }
7892
7893    #[test]
7894    fn test_refresh_upstream_areas_for_associative_memory_pairs() {
7895        use crate::models::cortical_area::CorticalArea;
7896        use feagi_npu_burst_engine::backend::CPUBackend;
7897        use feagi_npu_burst_engine::TracingMutex;
7898        use feagi_npu_burst_engine::{DynamicNPU, RustNPU};
7899        use feagi_npu_runtime::StdRuntime;
7900        use feagi_structures::genomic::cortical_area::{
7901            CorticalAreaDimensions, CorticalAreaType, CorticalID, MemoryCorticalType,
7902        };
7903        use std::sync::Arc;
7904
7905        let runtime = StdRuntime;
7906        let backend = CPUBackend::new();
7907        let npu = RustNPU::new(runtime, backend, 10_000, 10_000, 10).expect("Failed to create NPU");
7908        let dyn_npu = Arc::new(TracingMutex::new(DynamicNPU::F32(npu), "TestNPU"));
7909        let mut manager = ConnectomeManager::new_for_testing_with_npu(dyn_npu.clone());
7910        feagi_evolutionary::templates::add_core_morphologies(&mut manager.morphology_registry);
7911
7912        let a1_id = CorticalID::try_from_bytes(b"csrc0002").unwrap();
7913        let a2_id = CorticalID::try_from_bytes(b"csrc0003").unwrap();
7914        let m1_id = CorticalID::try_from_bytes(b"mmem0002").unwrap();
7915        let m2_id = CorticalID::try_from_bytes(b"mmem0003").unwrap();
7916
7917        let a1_area = CorticalArea::new(
7918            a1_id,
7919            0,
7920            "A1".to_string(),
7921            CorticalAreaDimensions::new(1, 1, 1).unwrap(),
7922            (0, 0, 0).into(),
7923            CorticalAreaType::Custom(
7924                feagi_structures::genomic::cortical_area::CustomCorticalType::LeakyIntegrateFire,
7925            ),
7926        )
7927        .unwrap();
7928        let a2_area = CorticalArea::new(
7929            a2_id,
7930            0,
7931            "A2".to_string(),
7932            CorticalAreaDimensions::new(1, 1, 1).unwrap(),
7933            (0, 0, 0).into(),
7934            CorticalAreaType::Custom(
7935                feagi_structures::genomic::cortical_area::CustomCorticalType::LeakyIntegrateFire,
7936            ),
7937        )
7938        .unwrap();
7939
7940        let mut m1_area = CorticalArea::new(
7941            m1_id,
7942            0,
7943            "M1".to_string(),
7944            CorticalAreaDimensions::new(1, 1, 1).unwrap(),
7945            (0, 0, 0).into(),
7946            CorticalAreaType::Memory(MemoryCorticalType::Memory),
7947        )
7948        .unwrap();
7949        m1_area
7950            .properties
7951            .insert("is_mem_type".to_string(), serde_json::json!(true));
7952        m1_area
7953            .properties
7954            .insert("temporal_depth".to_string(), serde_json::json!(1));
7955
7956        let mut m2_area = CorticalArea::new(
7957            m2_id,
7958            0,
7959            "M2".to_string(),
7960            CorticalAreaDimensions::new(1, 1, 1).unwrap(),
7961            (0, 0, 0).into(),
7962            CorticalAreaType::Memory(MemoryCorticalType::Memory),
7963        )
7964        .unwrap();
7965        m2_area
7966            .properties
7967            .insert("is_mem_type".to_string(), serde_json::json!(true));
7968        m2_area
7969            .properties
7970            .insert("temporal_depth".to_string(), serde_json::json!(1));
7971
7972        let a1_idx = manager.add_cortical_area(a1_area).unwrap();
7973        let a2_idx = manager.add_cortical_area(a2_area).unwrap();
7974        let m1_idx = manager.add_cortical_area(m1_area).unwrap();
7975        let m2_idx = manager.add_cortical_area(m2_area).unwrap();
7976
7977        manager
7978            .add_neuron(&a1_id, 0, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false)
7979            .unwrap();
7980        manager
7981            .add_neuron(&a2_id, 0, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false)
7982            .unwrap();
7983
7984        let episodic_mapping = vec![serde_json::json!({
7985            "morphology_id": "episodic_memory",
7986            "morphology_scalar": 1,
7987            "postSynapticCurrent_multiplier": 1.0,
7988        })];
7989        manager
7990            .update_cortical_mapping(&a1_id, &m1_id, episodic_mapping.clone())
7991            .unwrap();
7992        manager
7993            .regenerate_synapses_for_mapping(&a1_id, &m1_id)
7994            .unwrap();
7995        manager
7996            .update_cortical_mapping(&a2_id, &m2_id, episodic_mapping)
7997            .unwrap();
7998        manager
7999            .regenerate_synapses_for_mapping(&a2_id, &m2_id)
8000            .unwrap();
8001
8002        let assoc_mapping = vec![serde_json::json!({
8003            "morphology_id": "associative_memory",
8004            "morphology_scalar": 1,
8005            "postSynapticCurrent_multiplier": 1.0,
8006            "plasticity_flag": true,
8007            "plasticity_constant": 1,
8008            "ltp_multiplier": 1,
8009            "ltd_multiplier": 1,
8010            "plasticity_window": 5,
8011        })];
8012        manager
8013            .update_cortical_mapping(&m1_id, &m2_id, assoc_mapping.clone())
8014            .unwrap();
8015        manager
8016            .regenerate_synapses_for_mapping(&m1_id, &m2_id)
8017            .unwrap();
8018        // Second directed edge (bidirectional link is two explicit mappings, not auto-mirror).
8019        manager
8020            .update_cortical_mapping(&m2_id, &m1_id, assoc_mapping)
8021            .unwrap();
8022        manager
8023            .regenerate_synapses_for_mapping(&m2_id, &m1_id)
8024            .unwrap();
8025
8026        let upstream_m1 = manager.get_upstream_cortical_areas(&m1_id);
8027        let upstream_m2 = manager.get_upstream_cortical_areas(&m2_id);
8028        assert_eq!(
8029            upstream_m1.len(),
8030            2,
8031            "M1 should have A1 and M2 as upstreams once both directed associative edges exist"
8032        );
8033        assert_eq!(
8034            upstream_m2.len(),
8035            2,
8036            "M2 should have A2 and M1 as upstreams"
8037        );
8038
8039        manager.refresh_upstream_cortical_areas_from_mappings(&m1_id);
8040        manager.refresh_upstream_cortical_areas_from_mappings(&m2_id);
8041
8042        let upstream_m1 = manager.get_upstream_cortical_areas(&m1_id);
8043        let upstream_m2 = manager.get_upstream_cortical_areas(&m2_id);
8044        assert_eq!(upstream_m1.len(), 2, "M1 upstreams unchanged after refresh");
8045        assert_eq!(upstream_m2.len(), 2, "M2 upstreams unchanged after refresh");
8046        assert!(upstream_m1.contains(&a1_idx));
8047        assert!(upstream_m1.contains(&m2_idx));
8048        assert!(upstream_m2.contains(&a2_idx));
8049        assert!(upstream_m2.contains(&m1_idx));
8050
8051        // Fire upstream neurons and ensure burst processing works without altering upstream tracking.
8052        {
8053            let mut npu_lock = dyn_npu.lock().unwrap();
8054            let injected_a1 = npu_lock.inject_sensory_xyzp_by_id(&a1_id, &[(0, 0, 0, 1.0)]);
8055            let injected_a2 = npu_lock.inject_sensory_xyzp_by_id(&a2_id, &[(0, 0, 0, 1.0)]);
8056            assert_eq!(injected_a1, 1, "Expected A1 injection to match one neuron");
8057            assert_eq!(injected_a2, 1, "Expected A2 injection to match one neuron");
8058            npu_lock.process_burst().expect("Burst processing failed");
8059        }
8060
8061        let upstream_m1 = manager.get_upstream_cortical_areas(&m1_id);
8062        let upstream_m2 = manager.get_upstream_cortical_areas(&m2_id);
8063        assert_eq!(
8064            upstream_m1.len(),
8065            2,
8066            "M1 should keep 2 upstreams after firing"
8067        );
8068        assert_eq!(
8069            upstream_m2.len(),
8070            2,
8071            "M2 should keep 2 upstreams after firing"
8072        );
8073
8074        let episodic_upstream_m1 = manager.get_episodic_memory_upstream_cortical_areas(&m1_id);
8075        let episodic_upstream_m2 = manager.get_episodic_memory_upstream_cortical_areas(&m2_id);
8076        assert_eq!(
8077            episodic_upstream_m1,
8078            vec![a1_idx],
8079            "Episodic upstream list for M1 should exclude associative-only memory source M2"
8080        );
8081        assert_eq!(
8082            episodic_upstream_m2,
8083            vec![a2_idx],
8084            "Episodic upstream list for M2 should exclude associative-only memory source M1"
8085        );
8086    }
8087
8088    #[test]
8089    fn test_memory_twin_created_for_memory_mapping() {
8090        use crate::models::cortical_area::CorticalArea;
8091        use feagi_npu_burst_engine::backend::CPUBackend;
8092        use feagi_npu_burst_engine::TracingMutex;
8093        use feagi_npu_burst_engine::{DynamicNPU, RustNPU};
8094        use feagi_npu_runtime::StdRuntime;
8095        use feagi_structures::genomic::cortical_area::{
8096            CorticalAreaDimensions, CorticalAreaType, CorticalID, IOCorticalAreaConfigurationFlag,
8097            MemoryCorticalType,
8098        };
8099        use std::sync::Arc;
8100
8101        let runtime = StdRuntime;
8102        let backend = CPUBackend::new();
8103        let npu = RustNPU::new(runtime, backend, 10_000, 10_000, 10).expect("Failed to create NPU");
8104        let dyn_npu = Arc::new(TracingMutex::new(DynamicNPU::F32(npu), "TestNPU"));
8105        let mut manager = ConnectomeManager::new_for_testing_with_npu(dyn_npu.clone());
8106        feagi_evolutionary::templates::add_core_morphologies(&mut manager.morphology_registry);
8107
8108        let src_id = CorticalID::try_from_bytes(b"csrc0001").unwrap();
8109        let dst_id = CorticalID::try_from_bytes(b"mmem0001").unwrap();
8110
8111        let src_area = CorticalArea::new(
8112            src_id,
8113            0,
8114            "Source Area".to_string(),
8115            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
8116            (0, 0, 0).into(),
8117            CorticalAreaType::BrainInput(IOCorticalAreaConfigurationFlag::Boolean),
8118        )
8119        .unwrap();
8120        let mut dst_area = CorticalArea::new(
8121            dst_id,
8122            0,
8123            "Memory Area".to_string(),
8124            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
8125            (0, 0, 0).into(),
8126            CorticalAreaType::Memory(MemoryCorticalType::Memory),
8127        )
8128        .unwrap();
8129        dst_area
8130            .properties
8131            .insert("is_mem_type".to_string(), serde_json::json!(true));
8132        dst_area
8133            .properties
8134            .insert("temporal_depth".to_string(), serde_json::json!(1));
8135
8136        manager.add_cortical_area(src_area).unwrap();
8137        manager.add_cortical_area(dst_area).unwrap();
8138
8139        let mapping_data = vec![serde_json::json!({
8140            "morphology_id": "episodic_memory",
8141            "morphology_scalar": 1,
8142            "postSynapticCurrent_multiplier": 1.0,
8143        })];
8144        manager
8145            .update_cortical_mapping(&src_id, &dst_id, mapping_data)
8146            .unwrap();
8147        manager
8148            .regenerate_synapses_for_mapping(&src_id, &dst_id)
8149            .unwrap();
8150
8151        let memory_area = manager.get_cortical_area(&dst_id).unwrap();
8152        let twin_map = memory_area
8153            .properties
8154            .get("memory_twin_areas")
8155            .and_then(|v| v.as_object())
8156            .expect("memory_twin_areas should be set");
8157        let twin_id_str = twin_map
8158            .get(&src_id.as_base_64())
8159            .and_then(|v| v.as_str())
8160            .expect("Missing twin entry for upstream area");
8161        let twin_id = CorticalID::try_from_base_64(twin_id_str).unwrap();
8162        let mapping = memory_area
8163            .properties
8164            .get("cortical_mapping_dst")
8165            .and_then(|v| v.as_object())
8166            .and_then(|map| map.get(&twin_id.as_base_64()))
8167            .and_then(|v| v.as_array())
8168            .expect("Missing memory replay mapping for twin area");
8169        let uses_replay = mapping.iter().any(|rule| {
8170            rule.get("morphology_id")
8171                .and_then(|v| v.as_str())
8172                .is_some_and(|id| id == "memory_replay")
8173        });
8174        assert!(uses_replay, "Expected memory_replay mapping for twin area");
8175
8176        let twin_area = manager.get_cortical_area(&twin_id).unwrap();
8177        assert!(matches!(
8178            twin_area.cortical_type,
8179            CorticalAreaType::Custom(_)
8180        ));
8181        assert_eq!(
8182            twin_area
8183                .properties
8184                .get("memory_twin_of")
8185                .and_then(|v| v.as_str()),
8186            Some(src_id.as_base_64().as_str())
8187        );
8188        assert_eq!(
8189            twin_area
8190                .properties
8191                .get("memory_twin_for")
8192                .and_then(|v| v.as_str()),
8193            Some(dst_id.as_base_64().as_str())
8194        );
8195    }
8196
8197    #[test]
8198    fn test_associative_memory_between_memory_areas_creates_synapses() {
8199        use crate::models::cortical_area::CorticalArea;
8200        use feagi_npu_burst_engine::backend::CPUBackend;
8201        use feagi_npu_burst_engine::TracingMutex;
8202        use feagi_npu_burst_engine::{DynamicNPU, RustNPU};
8203        use feagi_npu_runtime::StdRuntime;
8204        use feagi_structures::genomic::cortical_area::{
8205            CorticalAreaDimensions, CorticalAreaType, CorticalID, MemoryCorticalType,
8206        };
8207        use std::sync::Arc;
8208
8209        let runtime = StdRuntime;
8210        let backend = CPUBackend::new();
8211        let npu = RustNPU::new(runtime, backend, 10_000, 10_000, 10).expect("Failed to create NPU");
8212        let dyn_npu = Arc::new(TracingMutex::new(DynamicNPU::F32(npu), "TestNPU"));
8213        let mut manager = ConnectomeManager::new_for_testing_with_npu(dyn_npu.clone());
8214        feagi_evolutionary::templates::add_core_morphologies(&mut manager.morphology_registry);
8215
8216        let m1_id = CorticalID::try_from_bytes(b"mmem0402").unwrap();
8217        let m2_id = CorticalID::try_from_bytes(b"mmem0403").unwrap();
8218
8219        let mut m1_area = CorticalArea::new(
8220            m1_id,
8221            0,
8222            "Memory M1".to_string(),
8223            CorticalAreaDimensions::new(1, 1, 1).unwrap(),
8224            (0, 0, 0).into(),
8225            CorticalAreaType::Memory(MemoryCorticalType::Memory),
8226        )
8227        .unwrap();
8228        m1_area
8229            .properties
8230            .insert("is_mem_type".to_string(), serde_json::json!(true));
8231        m1_area
8232            .properties
8233            .insert("temporal_depth".to_string(), serde_json::json!(1));
8234
8235        let mut m2_area = CorticalArea::new(
8236            m2_id,
8237            0,
8238            "Memory M2".to_string(),
8239            CorticalAreaDimensions::new(1, 1, 1).unwrap(),
8240            (0, 0, 0).into(),
8241            CorticalAreaType::Memory(MemoryCorticalType::Memory),
8242        )
8243        .unwrap();
8244        m2_area
8245            .properties
8246            .insert("is_mem_type".to_string(), serde_json::json!(true));
8247        m2_area
8248            .properties
8249            .insert("temporal_depth".to_string(), serde_json::json!(1));
8250
8251        manager.add_cortical_area(m1_area).unwrap();
8252        manager.add_cortical_area(m2_area).unwrap();
8253
8254        manager
8255            .add_neuron(&m1_id, 0, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false)
8256            .unwrap();
8257        manager
8258            .add_neuron(&m2_id, 0, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false)
8259            .unwrap();
8260
8261        let mapping_data = vec![serde_json::json!({
8262            "morphology_id": "associative_memory",
8263            "morphology_scalar": 1,
8264            "postSynapticCurrent_multiplier": 1.0,
8265            "plasticity_flag": true,
8266            "plasticity_constant": 1,
8267            "ltp_multiplier": 1,
8268            "ltd_multiplier": 1,
8269            "plasticity_window": 5,
8270        })];
8271        manager
8272            .update_cortical_mapping(&m1_id, &m2_id, mapping_data)
8273            .unwrap();
8274        let created = manager
8275            .regenerate_synapses_for_mapping(&m1_id, &m2_id)
8276            .unwrap();
8277        assert!(
8278            created > 0,
8279            "Expected associative memory mapping between memory areas to create synapses"
8280        );
8281        let npu_guard = dyn_npu.lock().unwrap();
8282        let assoc_tagged =
8283            npu_guard.count_synapses_with_edge_flag_bits(SYNAPSE_EDGE_ASSOCIATIVE_MEMORY);
8284        assert!(
8285            assoc_tagged >= 1,
8286            "associative_memory connectome path should stamp SYNAPSE_EDGE_ASSOCIATIVE_MEMORY on created synapses"
8287        );
8288    }
8289
8290    #[test]
8291    fn test_memory_twin_repair_on_load_preserves_replay_mapping() {
8292        use crate::models::cortical_area::CorticalArea;
8293        use feagi_npu_burst_engine::backend::CPUBackend;
8294        use feagi_npu_burst_engine::TracingMutex;
8295        use feagi_npu_burst_engine::{DynamicNPU, RustNPU};
8296        use feagi_npu_runtime::StdRuntime;
8297        use feagi_structures::genomic::cortical_area::{
8298            CorticalAreaDimensions, CorticalAreaType, CorticalID, IOCorticalAreaConfigurationFlag,
8299            MemoryCorticalType,
8300        };
8301        use std::sync::Arc;
8302
8303        let runtime = StdRuntime;
8304        let backend = CPUBackend::new();
8305        let npu = RustNPU::new(runtime, backend, 10_000, 10_000, 10).expect("Failed to create NPU");
8306        let dyn_npu = Arc::new(TracingMutex::new(DynamicNPU::F32(npu), "TestNPU"));
8307        let mut manager = ConnectomeManager::new_for_testing_with_npu(dyn_npu.clone());
8308        feagi_evolutionary::templates::add_core_morphologies(&mut manager.morphology_registry);
8309
8310        let src_id = CorticalID::try_from_bytes(b"csrc0002").unwrap();
8311        let mem_id = CorticalID::try_from_bytes(b"mmem0002").unwrap();
8312
8313        let src_area = CorticalArea::new(
8314            src_id,
8315            0,
8316            "Source Area".to_string(),
8317            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
8318            (0, 0, 0).into(),
8319            CorticalAreaType::BrainInput(IOCorticalAreaConfigurationFlag::Boolean),
8320        )
8321        .unwrap();
8322        let mut mem_area = CorticalArea::new(
8323            mem_id,
8324            0,
8325            "Memory Area".to_string(),
8326            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
8327            (0, 0, 0).into(),
8328            CorticalAreaType::Memory(MemoryCorticalType::Memory),
8329        )
8330        .unwrap();
8331        mem_area
8332            .properties
8333            .insert("is_mem_type".to_string(), serde_json::json!(true));
8334        mem_area
8335            .properties
8336            .insert("temporal_depth".to_string(), serde_json::json!(1));
8337
8338        manager.add_cortical_area(src_area).unwrap();
8339        manager.add_cortical_area(mem_area).unwrap();
8340
8341        let twin_id = manager
8342            .build_memory_twin_id(&mem_id, &src_id)
8343            .expect("Failed to build twin id");
8344        let twin_area = CorticalArea::new(
8345            twin_id,
8346            0,
8347            "Source Area_twin".to_string(),
8348            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
8349            (0, 0, 0).into(),
8350            CorticalAreaType::Custom(
8351                feagi_structures::genomic::cortical_area::CustomCorticalType::LeakyIntegrateFire,
8352            ),
8353        )
8354        .unwrap();
8355        manager.add_cortical_area(twin_area).unwrap();
8356
8357        let repaired = manager
8358            .ensure_memory_twin_area(&mem_id, &src_id)
8359            .expect("Failed to repair twin");
8360        assert_eq!(repaired, twin_id);
8361
8362        let mem_area = manager.get_cortical_area(&mem_id).unwrap();
8363        let twin_map = mem_area
8364            .properties
8365            .get("memory_twin_areas")
8366            .and_then(|v| v.as_object())
8367            .expect("memory_twin_areas should be set");
8368        let twin_id_str = twin_map
8369            .get(&src_id.as_base_64())
8370            .and_then(|v| v.as_str())
8371            .expect("Missing twin entry for upstream area");
8372        assert_eq!(twin_id_str, twin_id.as_base_64());
8373
8374        let replay_map = mem_area
8375            .properties
8376            .get("cortical_mapping_dst")
8377            .and_then(|v| v.as_object())
8378            .and_then(|map| map.get(&twin_id.as_base_64()))
8379            .and_then(|v| v.as_array())
8380            .expect("Missing memory replay mapping for twin area");
8381        let uses_replay = replay_map.iter().any(|rule| {
8382            rule.get("morphology_id")
8383                .and_then(|v| v.as_str())
8384                .is_some_and(|id| id == "memory_replay")
8385        });
8386        assert!(uses_replay, "Expected memory_replay mapping for twin area");
8387
8388        let twin_area = manager.get_cortical_area(&twin_id).unwrap();
8389        assert_eq!(
8390            twin_area
8391                .properties
8392                .get("memory_twin_of")
8393                .and_then(|v| v.as_str()),
8394            Some(src_id.as_base_64().as_str())
8395        );
8396        assert_eq!(
8397            twin_area
8398                .properties
8399                .get("memory_twin_for")
8400                .and_then(|v| v.as_str()),
8401            Some(mem_id.as_base_64().as_str())
8402        );
8403    }
8404
8405    #[test]
8406    fn test_memory_twin_inherits_upstream_parent_region() {
8407        use crate::models::cortical_area::CorticalArea;
8408        use crate::models::BrainRegion;
8409        use feagi_npu_burst_engine::backend::CPUBackend;
8410        use feagi_npu_burst_engine::TracingMutex;
8411        use feagi_npu_burst_engine::{DynamicNPU, RustNPU};
8412        use feagi_npu_runtime::StdRuntime;
8413        use feagi_structures::genomic::brain_regions::{RegionID, RegionType};
8414        use feagi_structures::genomic::cortical_area::{
8415            CorticalAreaDimensions, CorticalAreaType, CorticalID, IOCorticalAreaConfigurationFlag,
8416            MemoryCorticalType,
8417        };
8418        use std::sync::Arc;
8419
8420        let runtime = StdRuntime;
8421        let backend = CPUBackend::new();
8422        let npu = RustNPU::new(runtime, backend, 10_000, 10_000, 10).expect("Failed to create NPU");
8423        let dyn_npu = Arc::new(TracingMutex::new(DynamicNPU::F32(npu), "TestNPU"));
8424        let mut manager = ConnectomeManager::new_for_testing_with_npu(dyn_npu.clone());
8425        feagi_evolutionary::templates::add_core_morphologies(&mut manager.morphology_registry);
8426        let src_region_id = RegionID::new();
8427        let src_region_id_str = src_region_id.to_string();
8428        manager
8429            .add_brain_region(
8430                BrainRegion::new(
8431                    src_region_id,
8432                    "Src Region".to_string(),
8433                    RegionType::Undefined,
8434                )
8435                .unwrap(),
8436                None,
8437            )
8438            .unwrap();
8439        let mem_region_id = RegionID::new();
8440        let mem_region_id_str = mem_region_id.to_string();
8441        manager
8442            .add_brain_region(
8443                BrainRegion::new(
8444                    mem_region_id,
8445                    "Mem Region".to_string(),
8446                    RegionType::Undefined,
8447                )
8448                .unwrap(),
8449                None,
8450            )
8451            .unwrap();
8452
8453        let src_id = CorticalID::try_from_bytes(b"csrc1001").unwrap();
8454        let mem_id = CorticalID::try_from_bytes(b"mmem1001").unwrap();
8455
8456        let mut src_area = CorticalArea::new(
8457            src_id,
8458            0,
8459            "Origin".to_string(),
8460            CorticalAreaDimensions::new(4, 4, 1).unwrap(),
8461            (10, 20, 0).into(),
8462            CorticalAreaType::BrainInput(IOCorticalAreaConfigurationFlag::Boolean),
8463        )
8464        .unwrap();
8465        src_area.properties.insert(
8466            "parent_region_id".to_string(),
8467            serde_json::json!(src_region_id_str.clone()),
8468        );
8469
8470        let mut mem_area = CorticalArea::new(
8471            mem_id,
8472            0,
8473            "Memory".to_string(),
8474            CorticalAreaDimensions::new(2, 2, 1).unwrap(),
8475            (200, 300, 0).into(),
8476            CorticalAreaType::Memory(MemoryCorticalType::Memory),
8477        )
8478        .unwrap();
8479        mem_area
8480            .properties
8481            .insert("is_mem_type".to_string(), serde_json::json!(true));
8482        mem_area
8483            .properties
8484            .insert("temporal_depth".to_string(), serde_json::json!(1));
8485        mem_area.properties.insert(
8486            "parent_region_id".to_string(),
8487            serde_json::json!(mem_region_id_str.clone()),
8488        );
8489
8490        manager.add_cortical_area(src_area).unwrap();
8491        manager.add_cortical_area(mem_area).unwrap();
8492
8493        let mapping_data = vec![serde_json::json!({
8494            "morphology_id": "episodic_memory",
8495            "morphology_scalar": 1,
8496            "postSynapticCurrent_multiplier": 1.0,
8497        })];
8498        manager
8499            .update_cortical_mapping(&src_id, &mem_id, mapping_data)
8500            .unwrap();
8501        manager
8502            .regenerate_synapses_for_mapping(&src_id, &mem_id)
8503            .unwrap();
8504
8505        let memory_area = manager.get_cortical_area(&mem_id).unwrap();
8506        let twin_map = memory_area
8507            .properties
8508            .get("memory_twin_areas")
8509            .and_then(|v| v.as_object())
8510            .expect("memory_twin_areas should be set");
8511        let twin_id = CorticalID::try_from_base_64(
8512            twin_map
8513                .get(&src_id.as_base_64())
8514                .and_then(|v| v.as_str())
8515                .expect("missing twin for source"),
8516        )
8517        .unwrap();
8518        let twin_area = manager.get_cortical_area(&twin_id).unwrap();
8519
8520        assert_eq!(
8521            twin_area
8522                .properties
8523                .get("parent_region_id")
8524                .and_then(|v| v.as_str()),
8525            Some(src_region_id_str.as_str()),
8526            "Twin should inherit upstream/source parent region"
8527        );
8528        assert_ne!(
8529            twin_area
8530                .properties
8531                .get("parent_region_id")
8532                .and_then(|v| v.as_str()),
8533            Some(mem_region_id_str.as_str()),
8534            "Twin must not inherit memory area's parent region"
8535        );
8536    }
8537
8538    #[test]
8539    fn test_memory_twin_diagnostic_explains_memory_upstream_blocker() {
8540        use crate::models::cortical_area::CorticalArea;
8541        use feagi_npu_burst_engine::backend::CPUBackend;
8542        use feagi_npu_burst_engine::TracingMutex;
8543        use feagi_npu_burst_engine::{DynamicNPU, RustNPU};
8544        use feagi_npu_runtime::StdRuntime;
8545        use feagi_structures::genomic::cortical_area::{
8546            CorticalAreaDimensions, CorticalAreaType, CorticalID, MemoryCorticalType,
8547        };
8548        use std::sync::Arc;
8549
8550        let runtime = StdRuntime;
8551        let backend = CPUBackend::new();
8552        let npu = RustNPU::new(runtime, backend, 10_000, 10_000, 10).expect("Failed to create NPU");
8553        let dyn_npu = Arc::new(TracingMutex::new(DynamicNPU::F32(npu), "TestNPU"));
8554        let mut manager = ConnectomeManager::new_for_testing_with_npu(dyn_npu.clone());
8555        feagi_evolutionary::templates::add_core_morphologies(&mut manager.morphology_registry);
8556
8557        let m1_id = CorticalID::try_from_bytes(b"mmem2001").unwrap();
8558        let m2_id = CorticalID::try_from_bytes(b"mmem2002").unwrap();
8559
8560        let mut m1_area = CorticalArea::new(
8561            m1_id,
8562            0,
8563            "Memory A".to_string(),
8564            CorticalAreaDimensions::new(1, 1, 1).unwrap(),
8565            (0, 0, 0).into(),
8566            CorticalAreaType::Memory(MemoryCorticalType::Memory),
8567        )
8568        .unwrap();
8569        m1_area
8570            .properties
8571            .insert("is_mem_type".to_string(), serde_json::json!(true));
8572        m1_area
8573            .properties
8574            .insert("temporal_depth".to_string(), serde_json::json!(1));
8575
8576        let mut m2_area = CorticalArea::new(
8577            m2_id,
8578            0,
8579            "Memory B".to_string(),
8580            CorticalAreaDimensions::new(1, 1, 1).unwrap(),
8581            (0, 0, 0).into(),
8582            CorticalAreaType::Memory(MemoryCorticalType::Memory),
8583        )
8584        .unwrap();
8585        m2_area
8586            .properties
8587            .insert("is_mem_type".to_string(), serde_json::json!(true));
8588        m2_area
8589            .properties
8590            .insert("temporal_depth".to_string(), serde_json::json!(1));
8591
8592        manager.add_cortical_area(m1_area).unwrap();
8593        manager.add_cortical_area(m2_area).unwrap();
8594
8595        let mapping_data = vec![serde_json::json!({
8596            "morphology_id": "episodic_memory",
8597            "morphology_scalar": 1,
8598            "postSynapticCurrent_multiplier": 1.0,
8599        })];
8600        manager
8601            .update_cortical_mapping(&m1_id, &m2_id, mapping_data)
8602            .unwrap();
8603        manager
8604            .regenerate_synapses_for_mapping(&m1_id, &m2_id)
8605            .unwrap();
8606
8607        let diagnostic = manager
8608            .diagnose_memory_twin_for_mapping(&m1_id, &m2_id)
8609            .expect("diagnostic should succeed");
8610        assert!(diagnostic.mapping_exists);
8611        assert_eq!(diagnostic.episodic_rule_count, 1);
8612        assert!(!diagnostic.twin_expected);
8613        assert!(!diagnostic.twin_present);
8614        assert_eq!(
8615            diagnostic.reason.as_deref(),
8616            Some("upstream_is_memory_area_twin_not_supported")
8617        );
8618    }
8619
8620    /// Helper for the `max_weight` validation tests below: stand up a minimal connectome with
8621    /// a plastic mapping `src -> dst` plus the two detector areas required for R-STDP rules.
8622    /// Returns the manager (so individual tests can drive `update_cortical_mapping` against
8623    /// it) along with the four cortical IDs in (src, dst, reward, pain) order.
8624    fn build_max_weight_test_manager() -> (
8625        ConnectomeManager,
8626        CorticalID,
8627        CorticalID,
8628        CorticalID,
8629        CorticalID,
8630    ) {
8631        use feagi_npu_burst_engine::backend::CPUBackend;
8632        use feagi_npu_burst_engine::TracingMutex;
8633        use feagi_npu_burst_engine::{DynamicNPU, RustNPU};
8634        use feagi_npu_runtime::StdRuntime;
8635        use feagi_structures::genomic::cortical_area::{
8636            CorticalAreaType, IOCorticalAreaConfigurationFlag,
8637        };
8638
8639        let runtime = StdRuntime;
8640        let backend = CPUBackend::new();
8641        let npu = RustNPU::new(runtime, backend, 10_000, 10_000, 10).expect("npu");
8642        let dyn_npu = Arc::new(TracingMutex::new(DynamicNPU::F32(npu), "TestNPU"));
8643        let mut mgr = ConnectomeManager::new_for_testing_with_npu(dyn_npu);
8644        // Seed the core morphology registry; `all_to_all` is the simplest plastic morphology
8645        // available and is required to exercise the STDP rule parser path in
8646        // `regenerate_synapses_for_mapping`.
8647        feagi_evolutionary::templates::add_core_morphologies(&mut mgr.morphology_registry);
8648
8649        let src = CorticalID::try_from_bytes(b"cstmwsrc").unwrap();
8650        let dst = CorticalID::try_from_bytes(b"cstmwdst").unwrap();
8651        let reward = CorticalID::try_from_bytes(b"cstmwrwd").unwrap();
8652        let pain = CorticalID::try_from_bytes(b"cstmwpan").unwrap();
8653
8654        for (id, label, kind) in [
8655            (
8656                src,
8657                "src",
8658                CorticalAreaType::BrainInput(IOCorticalAreaConfigurationFlag::Boolean),
8659            ),
8660            (
8661                dst,
8662                "dst",
8663                CorticalAreaType::BrainOutput(IOCorticalAreaConfigurationFlag::Boolean),
8664            ),
8665            (
8666                reward,
8667                "reward",
8668                CorticalAreaType::Custom(
8669                    feagi_structures::genomic::cortical_area::CustomCorticalType::LeakyIntegrateFire,
8670                ),
8671            ),
8672            (
8673                pain,
8674                "pain",
8675                CorticalAreaType::Custom(
8676                    feagi_structures::genomic::cortical_area::CustomCorticalType::LeakyIntegrateFire,
8677                ),
8678            ),
8679        ] {
8680            mgr.add_cortical_area(
8681                CorticalArea::new(
8682                    id,
8683                    0,
8684                    label.to_string(),
8685                    CorticalAreaDimensions::new(1, 1, 1).unwrap(),
8686                    (0, 0, 0).into(),
8687                    kind,
8688                )
8689                .unwrap(),
8690            )
8691            .unwrap();
8692            mgr.add_neuron(&id, 0, 0, 0, 1.0, 0.0, 0.1, 0.0, 0, 1, 1.0, 3, 1, false)
8693                .unwrap();
8694        }
8695        (mgr, src, dst, reward, pain)
8696    }
8697
8698    /// Drive the full BDU mapping pipeline (store rules then regenerate synapses, which is
8699    /// where the STDP rule parser actually runs) so the validation tests below exercise the
8700    /// same code path as a `PUT /v1/cortical_mapping/mapping_properties` followed by the
8701    /// regeneration step kicked off by the connectome service.
8702    fn write_and_regenerate_mapping(
8703        mgr: &mut ConnectomeManager,
8704        src: &CorticalID,
8705        dst: &CorticalID,
8706        rule: serde_json::Value,
8707    ) -> BduResult<usize> {
8708        mgr.update_cortical_mapping(src, dst, vec![rule])?;
8709        mgr.regenerate_synapses_for_mapping(src, dst)
8710    }
8711
8712    /// Acceptance test: an R-STDP mapping rule with a finite, positive `max_weight` parses
8713    /// cleanly through the BDU pipeline used by `PUT /v1/cortical_mapping/mapping_properties`
8714    /// + the post-write regeneration step.
8715    #[test]
8716    fn test_max_weight_finite_positive_accepted_on_rstdp_rule() {
8717        let (mut mgr, src, dst, reward, pain) = build_max_weight_test_manager();
8718
8719        let result = write_and_regenerate_mapping(
8720            &mut mgr,
8721            &src,
8722            &dst,
8723            serde_json::json!({
8724                "morphology_id": "block_to_block",
8725                "morphology_scalar": [1, 1, 1],
8726                "postSynapticCurrent_multiplier": 1,
8727                "plasticity_flag": true,
8728                "plasticity_constant": 1,
8729                "ltp_multiplier": 1,
8730                "ltd_multiplier": 1,
8731                "plasticity_window": 10,
8732                "synaptic_delay_bursts": 1,
8733                "plasticity_mode": "rstdp",
8734                "eligibility_decay_bursts": 50,
8735                "reward_source_area": reward.as_base_64(),
8736                "punishment_source_area": pain.as_base_64(),
8737                "max_weight": 12.5,
8738            }),
8739        );
8740        assert!(
8741            result.is_ok(),
8742            "valid max_weight=12.5 must be accepted, got {:?}",
8743            result
8744        );
8745    }
8746
8747    /// Validation test: zero, negative, and non-numeric `max_weight` values must be rejected
8748    /// at parse time so the runtime never sees a malformed sentinel. (`NaN` and `Infinity`
8749    /// cannot appear in valid JSON -- `serde_json::json!(f64::NAN)` already serializes to
8750    /// `Null` -- so we cover the in-band wrong-type case via a string instead.)
8751    #[test]
8752    fn test_max_weight_invalid_values_rejected() {
8753        for bad in &[
8754            serde_json::json!(0.0),
8755            serde_json::json!(-1.5),
8756            serde_json::json!("not_a_number"),
8757        ] {
8758            let (mut mgr, src, dst, reward, pain) = build_max_weight_test_manager();
8759            let result = write_and_regenerate_mapping(
8760                &mut mgr,
8761                &src,
8762                &dst,
8763                serde_json::json!({
8764                    "morphology_id": "block_to_block",
8765                    "morphology_scalar": [1, 1, 1],
8766                    "postSynapticCurrent_multiplier": 1,
8767                    "plasticity_flag": true,
8768                    "plasticity_constant": 1,
8769                    "ltp_multiplier": 1,
8770                    "ltd_multiplier": 1,
8771                    "plasticity_window": 10,
8772                    "synaptic_delay_bursts": 1,
8773                    "plasticity_mode": "rstdp",
8774                    "eligibility_decay_bursts": 50,
8775                    "reward_source_area": reward.as_base_64(),
8776                    "punishment_source_area": pain.as_base_64(),
8777                    "max_weight": bad,
8778                }),
8779            );
8780            assert!(
8781                result.is_err(),
8782                "max_weight={:?} should have been rejected, got {:?}",
8783                bad,
8784                result
8785            );
8786        }
8787    }
8788
8789    /// `ltp_multiplier` / `ltd_multiplier` are stored as `i8` in the NPU; values outside
8790    /// `-128..=127` must fail at BDU parse time.
8791    #[test]
8792    fn test_ltp_ltd_multiplier_out_of_i8_range_rejected() {
8793        let (mut mgr, src, dst, reward, pain) = build_max_weight_test_manager();
8794        let result = write_and_regenerate_mapping(
8795            &mut mgr,
8796            &src,
8797            &dst,
8798            serde_json::json!({
8799                "morphology_id": "block_to_block",
8800                "morphology_scalar": [1, 1, 1],
8801                "postSynapticCurrent_multiplier": 1,
8802                "plasticity_flag": true,
8803                "plasticity_constant": 1,
8804                "ltp_multiplier": 200,
8805                "ltd_multiplier": 1,
8806                "plasticity_window": 10,
8807                "synaptic_delay_bursts": 1,
8808                "plasticity_mode": "rstdp",
8809                "eligibility_decay_bursts": 50,
8810                "reward_source_area": reward.as_base_64(),
8811                "punishment_source_area": pain.as_base_64(),
8812            }),
8813        );
8814        assert!(
8815            result.is_err(),
8816            "ltp_multiplier=200 must be rejected (i8 range); got {:?}",
8817            result
8818        );
8819    }
8820
8821    /// Validation test: setting an explicit `max_weight` on an off-mode (non-plastic) rule
8822    /// is meaningless and must surface as a clear error instead of being silently ignored.
8823    #[test]
8824    fn test_max_weight_rejected_when_plasticity_off() {
8825        let (mut mgr, src, dst, _reward, _pain) = build_max_weight_test_manager();
8826
8827        let result = write_and_regenerate_mapping(
8828            &mut mgr,
8829            &src,
8830            &dst,
8831            serde_json::json!({
8832                "morphology_id": "block_to_block",
8833                "morphology_scalar": [1, 1, 1],
8834                "postSynapticCurrent_multiplier": 1,
8835                // `plasticity_flag: true` is required to enter the rule-parsing branch in
8836                // `regenerate_synapses_for_mapping`; the off-mode validation is then driven
8837                // by the explicit `plasticity_mode: "off"` selector below, which is the
8838                // canonical successor of the legacy boolean flag.
8839                "plasticity_flag": true,
8840                "plasticity_constant": 0,
8841                "ltp_multiplier": 0,
8842                "ltd_multiplier": 0,
8843                "plasticity_window": 0,
8844                "synaptic_delay_bursts": 1,
8845                "plasticity_mode": "off",
8846                "max_weight": 10.0,
8847            }),
8848        );
8849        assert!(
8850            result.is_err(),
8851            "max_weight on off-mode rule must be rejected; got {:?}",
8852            result
8853        );
8854    }
8855
8856    #[test]
8857    fn test_plasticity_eta_rejected_when_plasticity_off() {
8858        let (mut mgr, src, dst, _reward, _pain) = build_max_weight_test_manager();
8859
8860        let result = write_and_regenerate_mapping(
8861            &mut mgr,
8862            &src,
8863            &dst,
8864            serde_json::json!({
8865                "morphology_id": "block_to_block",
8866                "morphology_scalar": [1, 1, 1],
8867                "postSynapticCurrent_multiplier": 1,
8868                "plasticity_flag": true,
8869                "plasticity_constant": 0,
8870                "ltp_multiplier": 0,
8871                "ltd_multiplier": 0,
8872                "plasticity_window": 0,
8873                "synaptic_delay_bursts": 1,
8874                "plasticity_mode": "off",
8875                "plasticity_eta": 0.5,
8876            }),
8877        );
8878        assert!(
8879            result.is_err(),
8880            "plasticity_eta on off-mode rule must be rejected; got {:?}",
8881            result
8882        );
8883    }
8884
8885    #[test]
8886    fn test_plasticity_eta_non_positive_rejected() {
8887        let (mut mgr, src, dst, reward, pain) = build_max_weight_test_manager();
8888
8889        let result = write_and_regenerate_mapping(
8890            &mut mgr,
8891            &src,
8892            &dst,
8893            serde_json::json!({
8894                "morphology_id": "block_to_block",
8895                "morphology_scalar": [1, 1, 1],
8896                "postSynapticCurrent_multiplier": 1,
8897                "plasticity_flag": true,
8898                "plasticity_constant": 1,
8899                "ltp_multiplier": 1,
8900                "ltd_multiplier": 1,
8901                "plasticity_window": 10,
8902                "synaptic_delay_bursts": 1,
8903                "plasticity_mode": "rstdp",
8904                "eligibility_decay_bursts": 50,
8905                "reward_source_area": reward.as_base_64(),
8906                "punishment_source_area": pain.as_base_64(),
8907                "plasticity_eta": 0.0,
8908            }),
8909        );
8910        assert!(
8911            result.is_err(),
8912            "plasticity_eta=0 must be rejected; got {:?}",
8913            result
8914        );
8915    }
8916}