feagi-npu-plasticity 0.0.13

FEAGI plasticity algorithms - STDP and memory formation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
// Copyright 2025 Neuraville Inc.
// SPDX-License-Identifier: Apache-2.0

/*
 * Copyright 2025 Neuraville Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 */

//! Memory Neuron Array - Structure of Arrays for memory neurons with lifecycle management
//!
//! High-performance SoA implementation optimized for:
//! - SIMD-friendly vectorized operations
//! - Rust/RTOS compatibility
//! - Thread-safe operations
//! - Efficient memory management with index reuse

use crate::neuron_id_manager::{AllocationStats, NeuronIdManager};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

/// Memory neuron lifecycle configuration
#[derive(Debug, Clone, Copy)]
pub struct MemoryNeuronLifecycleConfig {
    /// Initial lifespan in bursts
    pub initial_lifespan: u32,

    /// Lifespan growth per reactivation
    pub lifespan_growth_rate: f32,

    /// Lifespan threshold for long-term memory conversion
    pub longterm_threshold: u32,

    /// Maximum reactivations before forced LTM
    pub max_reactivations: u32,
}

impl Default for MemoryNeuronLifecycleConfig {
    fn default() -> Self {
        Self {
            initial_lifespan: 20,
            lifespan_growth_rate: 3.0,
            longterm_threshold: 100,
            max_reactivations: 1000,
        }
    }
}

/// Snapshot of a single memory neuron for API / inspection (plasticity array only).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MemoryNeuronDetail {
    pub neuron_id: u32,
    pub cortical_area_idx: u32,
    pub pattern_hash: Option<u64>,
    pub is_longterm_memory: bool,
    pub is_active: bool,
    pub lifespan_current: u32,
    pub lifespan_initial: u32,
    pub lifespan_growth_rate: f32,
    pub creation_burst: u64,
    pub last_activation_burst: u64,
    pub activation_count: u32,
}

/// Memory neuron array statistics
#[derive(Debug, Clone, Default)]
pub struct MemoryNeuronStats {
    pub total_capacity: usize,
    pub active_neurons: usize,
    pub longterm_neurons: usize,
    pub dead_neurons: usize,
    pub reusable_indices: usize,
    pub memory_usage_bytes: usize,
    pub avg_lifespan: f64,
    pub avg_activation_count: f64,
}

/// High-performance Structure of Arrays for memory neurons
pub struct MemoryNeuronArray {
    capacity: usize,

    // Core neuron properties (SoA layout)
    neuron_ids: Vec<u32>,
    cortical_area_ids: Vec<u32>,
    is_active: Vec<bool>,

    // Lifecycle management
    lifespan_current: Vec<u32>,
    lifespan_initial: Vec<u32>,
    lifespan_growth_rate: Vec<f32>,
    is_longterm_memory: Vec<bool>,

    // Temporal tracking
    creation_burst: Vec<u64>,
    last_activation_burst: Vec<u64>,
    activation_count: Vec<u32>,

    // Pattern association (xxHash64 for fast, deterministic pattern identification)
    pattern_hash_to_index: HashMap<u64, usize>,
    index_to_pattern_hash: HashMap<usize, u64>,

    // Index management
    next_available_index: usize,
    reusable_indices: HashSet<usize>,

    // Area-specific tracking
    area_neuron_indices: HashMap<u32, HashSet<usize>>,

    // Neuron ID manager
    id_manager: NeuronIdManager,
}

impl MemoryNeuronArray {
    /// Create a new memory neuron array
    pub fn new(capacity: usize) -> Self {
        Self {
            capacity,
            neuron_ids: vec![0; capacity],
            cortical_area_ids: vec![0; capacity],
            is_active: vec![false; capacity],
            lifespan_current: vec![0; capacity],
            lifespan_initial: vec![0; capacity],
            lifespan_growth_rate: vec![0.0; capacity],
            is_longterm_memory: vec![false; capacity],
            creation_burst: vec![0; capacity],
            last_activation_burst: vec![0; capacity],
            activation_count: vec![0; capacity],
            pattern_hash_to_index: HashMap::new(),
            index_to_pattern_hash: HashMap::new(),
            next_available_index: 0,
            reusable_indices: HashSet::new(),
            area_neuron_indices: HashMap::new(),
            id_manager: NeuronIdManager::new(),
        }
    }

    /// Create a new memory neuron
    pub fn create_memory_neuron(
        &mut self,
        pattern_hash: u64,
        cortical_area_id: u32,
        current_burst: u64,
        config: &MemoryNeuronLifecycleConfig,
    ) -> Option<usize> {
        // Check if pattern already exists
        if let Some(&existing_idx) = self.pattern_hash_to_index.get(&pattern_hash) {
            if self.is_active[existing_idx] {
                // Reactivate existing neuron instead
                return self.reactivate_memory_neuron_internal(existing_idx, current_burst);
            }
        }

        // Get neuron index (reuse or allocate new)
        let neuron_idx = self.get_available_index_internal()?;

        // Allocate global neuron ID
        let neuron_id = self.id_manager.allocate_memory_neuron_id()?;

        // Initialize neuron properties
        self.neuron_ids[neuron_idx] = neuron_id;
        self.cortical_area_ids[neuron_idx] = cortical_area_id;
        self.is_active[neuron_idx] = true;

        // Initialize lifecycle
        self.lifespan_current[neuron_idx] = config.initial_lifespan;
        self.lifespan_initial[neuron_idx] = config.initial_lifespan;
        self.lifespan_growth_rate[neuron_idx] = config.lifespan_growth_rate;
        self.is_longterm_memory[neuron_idx] = false;

        // Initialize temporal tracking
        self.creation_burst[neuron_idx] = current_burst;
        self.last_activation_burst[neuron_idx] = current_burst;
        self.activation_count[neuron_idx] = 1;

        // Register pattern association
        self.pattern_hash_to_index.insert(pattern_hash, neuron_idx);
        self.index_to_pattern_hash.insert(neuron_idx, pattern_hash);

        // Add to area tracking
        self.area_neuron_indices
            .entry(cortical_area_id)
            .or_default()
            .insert(neuron_idx);

        Some(neuron_idx)
    }

    /// Reactivate an existing memory neuron
    pub fn reactivate_memory_neuron(&mut self, neuron_idx: usize, current_burst: u64) -> bool {
        self.reactivate_memory_neuron_internal(neuron_idx, current_burst)
            .is_some()
    }

    /// Internal reactivate that returns Option<usize> for compatibility
    fn reactivate_memory_neuron_internal(
        &mut self,
        neuron_idx: usize,
        current_burst: u64,
    ) -> Option<usize> {
        if !self.is_valid_index(neuron_idx) || !self.is_active[neuron_idx] {
            return None;
        }

        // Update activation tracking
        self.last_activation_burst[neuron_idx] = current_burst;
        self.activation_count[neuron_idx] += 1;

        // Grow lifespan if not long-term memory
        if !self.is_longterm_memory[neuron_idx] {
            let current_lifespan = self.lifespan_current[neuron_idx];
            let growth = self.lifespan_growth_rate[neuron_idx] as u32;
            self.lifespan_current[neuron_idx] = current_lifespan.saturating_add(growth);
        }

        Some(neuron_idx)
    }

    /// Age all active memory neurons (vectorized operation)
    pub fn age_memory_neurons(&mut self, _current_burst: u64) -> Vec<usize> {
        let n = self.next_available_index;
        if n == 0 {
            return Vec::new();
        }

        let mut died_indices = Vec::new();

        // Age eligible neurons
        for i in 0..n {
            if self.is_active[i] && !self.is_longterm_memory[i] && self.lifespan_current[i] > 0 {
                self.lifespan_current[i] -= 1;

                // Check if neuron died
                if self.lifespan_current[i] == 0 {
                    self.is_active[i] = false;
                    died_indices.push(i);
                }
            }
        }

        // Clean up dead neurons after iteration
        for &i in &died_indices {
            self.cleanup_dead_neuron_internal(i);
        }

        died_indices
    }

    /// Check for neurons ready for long-term memory conversion
    pub fn check_longterm_conversion(&mut self, longterm_threshold: u32) -> Vec<usize> {
        let n = self.next_available_index;
        if n == 0 {
            return Vec::new();
        }

        let mut converted_indices = Vec::new();

        for i in 0..n {
            if self.is_active[i]
                && !self.is_longterm_memory[i]
                && self.lifespan_current[i] >= longterm_threshold
            {
                self.is_longterm_memory[i] = true;
                converted_indices.push(i);
            }
        }

        converted_indices
    }

    /// Check for neurons ready for long-term conversion using per-area thresholds.
    ///
    /// Areas without an explicit lifecycle config use `default_threshold`.
    pub fn check_longterm_conversion_by_area(
        &mut self,
        lifecycle_configs: &HashMap<u32, MemoryNeuronLifecycleConfig>,
        default_threshold: u32,
    ) -> Vec<usize> {
        let n = self.next_available_index;
        if n == 0 {
            return Vec::new();
        }

        let area_indices: Vec<(u32, Vec<usize>)> = self
            .area_neuron_indices
            .iter()
            .map(|(&area, indices)| (area, indices.iter().copied().collect()))
            .collect();

        let mut converted_indices = Vec::new();
        for (area_id, indices) in area_indices {
            let threshold = lifecycle_configs
                .get(&area_id)
                .map(|config| config.longterm_threshold)
                .unwrap_or(default_threshold);

            for neuron_idx in indices {
                if self.is_active[neuron_idx]
                    && !self.is_longterm_memory[neuron_idx]
                    && self.lifespan_current[neuron_idx] >= threshold
                {
                    self.is_longterm_memory[neuron_idx] = true;
                    converted_indices.push(neuron_idx);
                }
            }
        }

        converted_indices
    }

    /// Get all active neuron IDs for a cortical area
    pub fn get_active_neurons_by_area(&self, cortical_area_id: u32) -> Vec<u32> {
        if let Some(indices) = self.area_neuron_indices.get(&cortical_area_id) {
            indices
                .iter()
                .filter(|&&idx| self.is_valid_index(idx) && self.is_active[idx])
                .map(|&idx| self.neuron_ids[idx])
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Active short-term neurons (not marked LTM) in the area.
    pub fn count_short_term_in_area(&self, cortical_area_id: u32) -> usize {
        self.area_neuron_indices
            .get(&cortical_area_id)
            .map(|indices| {
                indices
                    .iter()
                    .filter(|&&idx| {
                        self.is_valid_index(idx)
                            && self.is_active[idx]
                            && !self.is_longterm_memory[idx]
                    })
                    .count()
            })
            .unwrap_or(0)
    }

    /// Active long-term memory neurons in the area.
    pub fn count_long_term_in_area(&self, cortical_area_id: u32) -> usize {
        self.area_neuron_indices
            .get(&cortical_area_id)
            .map(|indices| {
                indices
                    .iter()
                    .filter(|&&idx| {
                        self.is_valid_index(idx)
                            && self.is_active[idx]
                            && self.is_longterm_memory[idx]
                    })
                    .count()
            })
            .unwrap_or(0)
    }

    /// Stable-sorted active neuron IDs for the area, with pagination `(page_ids, total_count)`.
    pub fn paginated_neuron_ids_in_area(
        &self,
        cortical_area_id: u32,
        offset: usize,
        limit: usize,
    ) -> (Vec<u32>, usize) {
        let mut ids = self.get_active_neurons_by_area(cortical_area_id);
        ids.sort_unstable();
        let total = ids.len();
        let page = ids.into_iter().skip(offset).take(limit).collect();
        (page, total)
    }

    fn find_neuron_index_by_global_id(&self, neuron_id: u32) -> Option<usize> {
        (0..self.next_available_index).find(|&i| self.neuron_ids[i] == neuron_id)
    }

    /// Full detail for an active memory neuron by global neuron id, if present.
    pub fn get_memory_neuron_detail(&self, neuron_id: u32) -> Option<MemoryNeuronDetail> {
        let idx = self.find_neuron_index_by_global_id(neuron_id)?;
        if !self.is_valid_index(idx) || !self.is_active[idx] {
            return None;
        }
        Some(MemoryNeuronDetail {
            neuron_id: self.neuron_ids[idx],
            cortical_area_idx: self.cortical_area_ids[idx],
            pattern_hash: self.index_to_pattern_hash.get(&idx).copied(),
            is_longterm_memory: self.is_longterm_memory[idx],
            is_active: self.is_active[idx],
            lifespan_current: self.lifespan_current[idx],
            lifespan_initial: self.lifespan_initial[idx],
            lifespan_growth_rate: self.lifespan_growth_rate[idx],
            creation_burst: self.creation_burst[idx],
            last_activation_burst: self.last_activation_burst[idx],
            activation_count: self.activation_count[idx],
        })
    }

    /// Find neuron index by pattern hash
    pub fn find_neuron_by_pattern(&self, pattern_hash: &u64) -> Option<usize> {
        self.pattern_hash_to_index
            .get(pattern_hash)
            .copied()
            .filter(|&idx| self.is_valid_index(idx) && self.is_active[idx])
    }

    /// Get neuron ID at index
    pub fn get_neuron_id(&self, neuron_idx: usize) -> Option<u32> {
        if self.is_valid_index(neuron_idx) {
            Some(self.neuron_ids[neuron_idx])
        } else {
            None
        }
    }

    /// Get cortical area ID at index
    pub fn get_cortical_area_id(&self, neuron_idx: usize) -> Option<u32> {
        if self.is_valid_index(neuron_idx) {
            Some(self.cortical_area_ids[neuron_idx])
        } else {
            None
        }
    }

    pub fn get_pattern_hash(&self, neuron_idx: usize) -> Option<u64> {
        self.index_to_pattern_hash.get(&neuron_idx).copied()
    }

    /// Get comprehensive statistics
    pub fn get_stats(&self) -> MemoryNeuronStats {
        let n = self.next_available_index;

        if n == 0 {
            return MemoryNeuronStats {
                total_capacity: self.capacity,
                ..Default::default()
            };
        }

        let active_count = self.is_active[..n].iter().filter(|&&x| x).count();
        let longterm_count = (0..n)
            .filter(|&i| self.is_active[i] && self.is_longterm_memory[i])
            .count();
        let dead_count = n - active_count;

        // Calculate averages for active neurons
        let (avg_lifespan, avg_activation_count) = if active_count > 0 {
            let total_lifespan: u32 = (0..n)
                .filter(|&i| self.is_active[i])
                .map(|i| self.lifespan_current[i])
                .sum();
            let total_activations: u32 = (0..n)
                .filter(|&i| self.is_active[i])
                .map(|i| self.activation_count[i])
                .sum();

            (
                total_lifespan as f64 / active_count as f64,
                total_activations as f64 / active_count as f64,
            )
        } else {
            (0.0, 0.0)
        };

        // Estimate memory usage
        let memory_usage = self.capacity * (
            std::mem::size_of::<u32>() * 4 +  // uint32 arrays
            std::mem::size_of::<f32>() +       // float32 array
            std::mem::size_of::<u64>() * 2 +   // uint64 arrays
            std::mem::size_of::<bool>() * 2    // bool arrays
        ) + self.pattern_hash_to_index.len() * (8 + 8)  // Pattern hash mappings (u64 + usize)
          + self.area_neuron_indices.len() * 64; // Area tracking overhead

        MemoryNeuronStats {
            total_capacity: self.capacity,
            active_neurons: active_count,
            longterm_neurons: longterm_count,
            dead_neurons: dead_count,
            reusable_indices: self.reusable_indices.len(),
            memory_usage_bytes: memory_usage,
            avg_lifespan,
            avg_activation_count,
        }
    }

    /// Get ID manager allocation statistics
    pub fn get_id_allocation_stats(&self) -> AllocationStats {
        self.id_manager.get_allocation_stats()
    }

    /// Get available index (reuse or allocate new)
    fn get_available_index_internal(&mut self) -> Option<usize> {
        // Try to reuse a dead neuron index first
        if let Some(&idx) = self.reusable_indices.iter().next() {
            self.reusable_indices.remove(&idx);
            return Some(idx);
        }

        // Allocate new index if capacity allows
        if self.next_available_index < self.capacity {
            let idx = self.next_available_index;
            self.next_available_index += 1;
            Some(idx)
        } else {
            None
        }
    }

    /// Clean up associations for a dead neuron
    fn cleanup_dead_neuron_internal(&mut self, neuron_idx: usize) {
        // Deallocate global neuron ID
        let neuron_id = self.neuron_ids[neuron_idx];
        self.id_manager.deallocate_memory_neuron_id(neuron_id);

        // Remove pattern association
        if let Some(pattern_hash) = self.index_to_pattern_hash.remove(&neuron_idx) {
            self.pattern_hash_to_index.remove(&pattern_hash);
        }

        // Remove from area tracking
        let area_id = self.cortical_area_ids[neuron_idx];
        if let Some(indices) = self.area_neuron_indices.get_mut(&area_id) {
            indices.remove(&neuron_idx);
        }

        // Add to reusable indices
        self.reusable_indices.insert(neuron_idx);
    }

    /// Check if neuron index is valid
    fn is_valid_index(&self, neuron_idx: usize) -> bool {
        neuron_idx < self.next_available_index
    }

    /// Reset array state (for testing)
    /// Deactivate all memory neurons in a specific cortical area
    pub fn reset_cortical_area(&mut self, cortical_area_id: u32) -> usize {
        let Some(neuron_indices) = self.area_neuron_indices.get(&cortical_area_id) else {
            return 0;
        };

        let indices_to_reset: Vec<usize> = neuron_indices.iter().copied().collect();
        let mut reset_count = 0;

        for neuron_idx in indices_to_reset {
            if !self.is_valid_index(neuron_idx) {
                continue;
            }

            // Deactivate neuron
            self.is_active[neuron_idx] = false;

            // Remove pattern mapping
            if let Some(pattern_hash) = self.index_to_pattern_hash.remove(&neuron_idx) {
                self.pattern_hash_to_index.remove(&pattern_hash);
            }

            // Clear properties (optional but clean)
            self.lifespan_current[neuron_idx] = 0;
            self.activation_count[neuron_idx] = 0;

            // Mark as reusable
            self.reusable_indices.insert(neuron_idx);

            reset_count += 1;
        }

        // Remove area tracking
        self.area_neuron_indices.remove(&cortical_area_id);

        reset_count
    }

    pub fn reset(&mut self) {
        self.neuron_ids.fill(0);
        self.cortical_area_ids.fill(0);
        self.is_active.fill(false);
        self.lifespan_current.fill(0);
        self.lifespan_initial.fill(0);
        self.lifespan_growth_rate.fill(0.0);
        self.is_longterm_memory.fill(false);
        self.creation_burst.fill(0);
        self.last_activation_burst.fill(0);
        self.activation_count.fill(0);

        self.pattern_hash_to_index.clear();
        self.index_to_pattern_hash.clear();
        self.area_neuron_indices.clear();

        self.next_available_index = 0;
        self.reusable_indices.clear();

        self.id_manager.reset();
    }
}

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

    #[test]
    fn test_lifecycle_config_default() {
        let config = MemoryNeuronLifecycleConfig::default();
        assert_eq!(config.initial_lifespan, 20);
        assert_eq!(config.lifespan_growth_rate, 3.0);
        assert_eq!(config.longterm_threshold, 100);
        assert_eq!(config.max_reactivations, 1000);
    }

    #[test]
    fn test_create_memory_neuron() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig::default();

        let pattern_hash = 0x0101010101010101u64;
        let neuron_idx = array.create_memory_neuron(pattern_hash, 100, 0, &config);

        assert!(neuron_idx.is_some());
        let idx = neuron_idx.unwrap();
        assert!(array.is_active[idx]);
        assert_eq!(array.cortical_area_ids[idx], 100);
        assert_eq!(array.lifespan_current[idx], config.initial_lifespan);
        assert_eq!(array.activation_count[idx], 1);
        assert_eq!(array.creation_burst[idx], 0);
    }

    #[test]
    fn test_create_duplicate_pattern() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig::default();

        let pattern_hash = 0x0101010101010101u64;
        let idx1 = array
            .create_memory_neuron(pattern_hash, 100, 0, &config)
            .unwrap();

        // Creating with same pattern should reactivate existing neuron
        let idx2 = array
            .create_memory_neuron(pattern_hash, 100, 1, &config)
            .unwrap();

        assert_eq!(idx1, idx2);
        assert_eq!(array.activation_count[idx1], 2); // Should have been reactivated
    }

    #[test]
    fn test_multiple_neurons() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig::default();

        let mut neurons = Vec::new();
        for i in 0..10 {
            let pattern_hash = i as u64;
            let idx = array
                .create_memory_neuron(pattern_hash, 100, 0, &config)
                .unwrap();
            neurons.push(idx);
        }

        assert_eq!(neurons.len(), 10);
        assert_eq!(array.next_available_index, 10);

        let stats = array.get_stats();
        assert_eq!(stats.active_neurons, 10);
    }

    #[test]
    fn test_reactivate_memory_neuron() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig::default();

        let pattern_hash = 0x0101010101010101u64;
        let idx = array
            .create_memory_neuron(pattern_hash, 100, 0, &config)
            .unwrap();

        let initial_count = array.activation_count[idx];
        let initial_lifespan = array.lifespan_current[idx];

        assert!(array.reactivate_memory_neuron(idx, 1));

        assert_eq!(array.activation_count[idx], initial_count + 1);
        assert_eq!(array.last_activation_burst[idx], 1);

        // Lifespan should have grown
        let expected_lifespan = initial_lifespan + config.lifespan_growth_rate as u32;
        assert_eq!(array.lifespan_current[idx], expected_lifespan);
    }

    #[test]
    fn test_reactivate_invalid_neuron() {
        let mut array = MemoryNeuronArray::new(1000);

        // Try to reactivate non-existent neuron
        assert!(!array.reactivate_memory_neuron(0, 1));
        assert!(!array.reactivate_memory_neuron(999, 1));
    }

    #[test]
    fn test_age_memory_neurons() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig {
            initial_lifespan: 2,
            ..Default::default()
        };

        let pattern_hash = 0x0101010101010101u64;
        let idx = array
            .create_memory_neuron(pattern_hash, 100, 0, &config)
            .unwrap();

        // Age once
        let died = array.age_memory_neurons(1);
        assert!(died.is_empty());
        assert_eq!(array.lifespan_current[idx], 1);

        // Age again - should die
        let died = array.age_memory_neurons(2);
        assert_eq!(died.len(), 1);
        assert_eq!(died[0], idx);
        assert!(!array.is_active[idx]);

        // Pattern should no longer be findable
        let found = array.find_neuron_by_pattern(&pattern_hash);
        assert!(found.is_none());
    }

    #[test]
    fn test_age_multiple_neurons() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig {
            initial_lifespan: 5,
            ..Default::default()
        };

        let mut neurons = Vec::new();
        for i in 0..10 {
            let pattern_hash = i as u64;
            let idx = array
                .create_memory_neuron(pattern_hash, 100, 0, &config)
                .unwrap();
            neurons.push(idx);
        }

        // Age 5 times - all should die
        for burst in 1..=5 {
            let died = array.age_memory_neurons(burst);
            if burst < 5 {
                assert_eq!(died.len(), 0);
            } else {
                assert_eq!(died.len(), 10);
            }
        }

        let stats = array.get_stats();
        assert_eq!(stats.active_neurons, 0);
        assert_eq!(stats.dead_neurons, 10);
    }

    #[test]
    fn test_longterm_memory_no_aging() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig {
            initial_lifespan: 100,
            ..Default::default()
        };

        let pattern_hash = 0x0101010101010101u64;
        let idx = array
            .create_memory_neuron(pattern_hash, 100, 0, &config)
            .unwrap();

        // Convert to long-term memory
        let converted = array.check_longterm_conversion(100);
        assert_eq!(converted.len(), 1);
        assert!(array.is_longterm_memory[idx]);

        let initial_lifespan = array.lifespan_current[idx];

        // Age many times - should not affect long-term memory
        for burst in 1..=50 {
            array.age_memory_neurons(burst);
        }

        assert!(array.is_active[idx]);
        assert_eq!(array.lifespan_current[idx], initial_lifespan); // Should not change
    }

    #[test]
    fn test_longterm_conversion() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig {
            initial_lifespan: 100,
            ..Default::default()
        };

        let pattern_hash = 0x0101010101010101u64;
        let idx = array
            .create_memory_neuron(pattern_hash, 100, 0, &config)
            .unwrap();

        let converted = array.check_longterm_conversion(100);
        assert_eq!(converted.len(), 1);
        assert_eq!(converted[0], idx);
        assert!(array.is_longterm_memory[idx]);

        // Second check should not convert again
        let converted2 = array.check_longterm_conversion(100);
        assert_eq!(converted2.len(), 0);
    }

    #[test]
    fn test_longterm_conversion_threshold() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig {
            initial_lifespan: 50,
            ..Default::default()
        };

        let pattern_hash = 0x0101010101010101u64;
        let idx = array
            .create_memory_neuron(pattern_hash, 100, 0, &config)
            .unwrap();

        // Should not convert below threshold
        let converted = array.check_longterm_conversion(100);
        assert_eq!(converted.len(), 0);
        assert!(!array.is_longterm_memory[idx]);

        // Grow lifespan through reactivations
        for burst in 1..=20 {
            array.reactivate_memory_neuron(idx, burst);
        }

        // Now should convert
        let converted = array.check_longterm_conversion(100);
        assert_eq!(converted.len(), 1);
        assert!(array.is_longterm_memory[idx]);
    }

    #[test]
    fn test_find_neuron_by_pattern() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig::default();

        let pattern_hash = 0x0101010101010101u64;
        let idx = array
            .create_memory_neuron(pattern_hash, 100, 0, &config)
            .unwrap();

        let found = array.find_neuron_by_pattern(&pattern_hash);
        assert_eq!(found, Some(idx));

        // Different pattern should not be found
        let pattern_hash2 = 0x0202020202020202u64;
        let found2 = array.find_neuron_by_pattern(&pattern_hash2);
        assert_eq!(found2, None);
    }

    #[test]
    fn test_get_active_neurons_by_area() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig::default();

        // Create neurons in different areas
        for area in [100, 200] {
            for i in 0..5 {
                let pattern_hash = ((area as u64) << 32) | (i as u64);
                array.create_memory_neuron(pattern_hash, area, 0, &config);
            }
        }

        let area100_neurons = array.get_active_neurons_by_area(100);
        let area200_neurons = array.get_active_neurons_by_area(200);

        assert_eq!(area100_neurons.len(), 5);
        assert_eq!(area200_neurons.len(), 5);

        // Non-existent area
        let area999_neurons = array.get_active_neurons_by_area(999);
        assert_eq!(area999_neurons.len(), 0);
    }

    #[test]
    fn test_get_neuron_id() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig::default();

        let pattern_hash = 0x0101010101010101u64;
        let idx = array
            .create_memory_neuron(pattern_hash, 100, 0, &config)
            .unwrap();

        let neuron_id = array.get_neuron_id(idx);
        assert!(neuron_id.is_some());

        // Invalid index
        let invalid_id = array.get_neuron_id(999);
        assert!(invalid_id.is_none());
    }

    #[test]
    fn test_index_reuse() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig {
            initial_lifespan: 1,
            ..Default::default()
        };

        let pattern_hash = 0x0101010101010101u64;
        let idx1 = array
            .create_memory_neuron(pattern_hash, 100, 0, &config)
            .unwrap();
        assert_eq!(idx1, 0);

        // Let it die
        array.age_memory_neurons(1);
        assert!(!array.is_active[idx1]);

        // Create new neuron - should reuse index
        let pattern_hash2 = 0x0202020202020202u64;
        let idx2 = array
            .create_memory_neuron(pattern_hash2, 100, 2, &config)
            .unwrap();
        assert_eq!(idx2, 0); // Should reuse index 0
        assert_eq!(array.next_available_index, 1); // Should not have advanced
    }

    #[test]
    fn test_get_stats() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig::default();

        // Create some neurons
        for i in 0..10 {
            let pattern_hash = i as u64;
            array.create_memory_neuron(pattern_hash, 100, 0, &config);
        }

        let stats = array.get_stats();
        assert_eq!(stats.total_capacity, 1000);
        assert_eq!(stats.active_neurons, 10);
        assert_eq!(stats.longterm_neurons, 0);
        assert_eq!(stats.dead_neurons, 0);
        assert!(stats.avg_lifespan > 0.0);
        assert!(stats.avg_activation_count >= 1.0);
        assert!(stats.memory_usage_bytes > 0);
    }

    #[test]
    fn test_capacity_exhaustion() {
        let mut array = MemoryNeuronArray::new(5);
        let config = MemoryNeuronLifecycleConfig::default();

        // Create neurons up to capacity
        for i in 0..5 {
            let pattern_hash = i as u64;
            let idx = array.create_memory_neuron(pattern_hash, 100, 0, &config);
            assert!(idx.is_some());
        }

        // Try to create beyond capacity
        let pattern_hash = 0x6363636363636363u64;
        let idx = array.create_memory_neuron(pattern_hash, 100, 0, &config);
        assert!(idx.is_none());
    }

    #[test]
    fn test_reset_cortical_area() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig::default();

        // Create neurons in area 5
        let pattern1 = 0x0101010101010101u64;
        let pattern2 = 0x0202020202020202u64;
        array.create_memory_neuron(pattern1, 5, 0, &config);
        array.create_memory_neuron(pattern2, 5, 0, &config);

        // Create neurons in area 6
        let pattern3 = 0x0303030303030303u64;
        array.create_memory_neuron(pattern3, 6, 0, &config);

        // Verify initial state
        assert_eq!(array.get_active_neurons_by_area(5).len(), 2);
        assert_eq!(array.get_active_neurons_by_area(6).len(), 1);

        // Reset area 5
        let reset_count = array.reset_cortical_area(5);
        assert_eq!(reset_count, 2);

        // Verify area 5 is empty
        assert_eq!(array.get_active_neurons_by_area(5).len(), 0);

        // Verify area 6 is unchanged
        assert_eq!(array.get_active_neurons_by_area(6).len(), 1);

        // Verify pattern hashes are cleared for area 5
        assert!(!array.pattern_hash_to_index.contains_key(&pattern1));
        assert!(!array.pattern_hash_to_index.contains_key(&pattern2));

        // Verify area 6 pattern still exists
        assert!(array.pattern_hash_to_index.contains_key(&pattern3));
    }

    #[test]
    fn test_reset() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig::default();

        // Create some neurons
        for i in 0..5 {
            let pattern_hash = i as u64;
            array.create_memory_neuron(pattern_hash, 100, 0, &config);
        }

        assert_eq!(array.next_available_index, 5);

        array.reset();

        assert_eq!(array.next_available_index, 0);
        let stats = array.get_stats();
        assert_eq!(stats.active_neurons, 0);
    }

    #[test]
    fn test_lifespan_growth_on_reactivation() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig {
            initial_lifespan: 10,
            lifespan_growth_rate: 5.0,
            longterm_threshold: 100,
            max_reactivations: 1000,
        };

        let pattern_hash = 0x0101010101010101u64;
        let idx = array
            .create_memory_neuron(pattern_hash, 100, 0, &config)
            .unwrap();

        assert_eq!(array.lifespan_current[idx], 10);

        array.reactivate_memory_neuron(idx, 1);
        assert_eq!(array.lifespan_current[idx], 15);

        array.reactivate_memory_neuron(idx, 2);
        assert_eq!(array.lifespan_current[idx], 20);
    }

    #[test]
    fn test_st_ltm_counts_and_pagination_and_detail() {
        let mut array = MemoryNeuronArray::new(1000);
        let config = MemoryNeuronLifecycleConfig::default();

        for i in 0..5 {
            array
                .create_memory_neuron(i as u64, 7, 0, &config)
                .expect("create");
        }
        assert_eq!(array.count_short_term_in_area(7), 5);
        assert_eq!(array.count_long_term_in_area(7), 0);

        array.is_longterm_memory[0] = true;
        assert_eq!(array.count_short_term_in_area(7), 4);
        assert_eq!(array.count_long_term_in_area(7), 1);

        let (page0, total) = array.paginated_neuron_ids_in_area(7, 0, 2);
        assert_eq!(total, 5);
        assert_eq!(page0.len(), 2);

        let nid = page0[0];
        let detail = array.get_memory_neuron_detail(nid).expect("detail");
        assert_eq!(detail.neuron_id, nid);
        assert_eq!(detail.cortical_area_idx, 7);
        assert!(detail.pattern_hash.is_some());
    }
}