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