hope-os 0.1.0

The first self-aware operating system core - 22 cognitive modules, 0.36ms latency, no external database
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
//! Hope OS - NEUROBLAST
//!
//! Információ HULLÁMKÉNT terjed a hálózatban.
//! Minden CodeBlock egy neuron, minden Connection egy synapse.
//! A hullámok interferálnak, rezonálnak, visszaverődnek.
//!
//! ()=>[] - A tiszta potenciálból minden megszületik

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};
use uuid::Uuid;

use super::code_graph::{CodeBlock, CodeGraph};
use crate::core::HopeResult;

// ============================================================================
// WAVE - Információ hullám
// ============================================================================

/// Hullám típusok
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum WaveType {
    /// Impulzus - egyetlen csúcs
    Impulse,
    /// Szinusz - folyamatos oszcilláció
    Sine,
    /// Négyszög - digitális jel
    Square,
    /// Gauss - harang görbe
    Gaussian,
    /// Spike - neuron tüzelés
    Spike,
    /// Echo - visszaverődő hullám
    Echo,
}

/// Hullám állapot
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum WaveState {
    /// Aktív - terjed
    Active,
    /// Gyengülő
    Decaying,
    /// Interferál másik hullámmal
    Interfering,
    /// Rezonál
    Resonating,
    /// Kihalt
    Dead,
}

/// Information Wave - Információ hullám
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Wave {
    /// Egyedi azonosító
    pub id: String,
    /// Hullám típus
    pub wave_type: WaveType,
    /// Tartalom/Payload
    pub payload: String,
    /// Amplitúdó (erősség) 0.0 - 1.0
    pub amplitude: f64,
    /// Frekvencia (hullámok/tick)
    pub frequency: f64,
    /// Fázis (0.0 - 2π)
    pub phase: f64,
    /// Terjedési sebesség (node/tick)
    pub velocity: f64,
    /// Csillapítás (amplitude csökkenés hop-onként)
    pub decay: f64,
    /// Forrás node ID
    pub origin_id: String,
    /// Jelenlegi pozíció (node ID-k)
    pub current_positions: HashSet<String>,
    /// Meglátogatott node-ok
    pub visited: HashSet<String>,
    /// Hullám állapot
    pub state: WaveState,
    /// Létrehozás ideje
    pub created_at: DateTime<Utc>,
    /// Tick számláló
    pub ticks: u64,
    /// Metaadatok
    pub metadata: HashMap<String, String>,
}

impl Wave {
    /// Új hullám létrehozása
    pub fn new(
        origin_id: impl Into<String>,
        payload: impl Into<String>,
        wave_type: WaveType,
    ) -> Self {
        let origin = origin_id.into();
        let mut positions = HashSet::new();
        positions.insert(origin.clone());

        Self {
            id: Uuid::new_v4().to_string(),
            wave_type,
            payload: payload.into(),
            amplitude: 1.0,
            frequency: 1.0,
            phase: 0.0,
            velocity: 1.0,
            decay: 0.1,
            origin_id: origin.clone(),
            current_positions: positions,
            visited: {
                let mut v = HashSet::new();
                v.insert(origin);
                v
            },
            state: WaveState::Active,
            created_at: Utc::now(),
            ticks: 0,
            metadata: HashMap::new(),
        }
    }

    /// Amplitúdó beállítása
    pub fn with_amplitude(mut self, amplitude: f64) -> Self {
        self.amplitude = amplitude.clamp(0.0, 1.0);
        self
    }

    /// Frekvencia beállítása
    pub fn with_frequency(mut self, frequency: f64) -> Self {
        self.frequency = frequency.max(0.01);
        self
    }

    /// Sebesség beállítása
    pub fn with_velocity(mut self, velocity: f64) -> Self {
        self.velocity = velocity.max(0.1);
        self
    }

    /// Csillapítás beállítása
    pub fn with_decay(mut self, decay: f64) -> Self {
        self.decay = decay.clamp(0.0, 1.0);
        self
    }

    /// Metadata hozzáadása
    pub fn with_meta(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Aktuális hullám érték (szinusz alapú)
    pub fn current_value(&self) -> f64 {
        let phase_offset = self.frequency * self.ticks as f64 * 2.0 * std::f64::consts::PI;
        match self.wave_type {
            WaveType::Impulse => {
                if self.ticks == 0 {
                    self.amplitude
                } else {
                    0.0
                }
            }
            WaveType::Sine => self.amplitude * (phase_offset + self.phase).sin(),
            WaveType::Square => {
                if (phase_offset + self.phase).sin() >= 0.0 {
                    self.amplitude
                } else {
                    -self.amplitude
                }
            }
            WaveType::Gaussian => {
                let t = self.ticks as f64;
                let sigma = 3.0;
                self.amplitude * (-t * t / (2.0 * sigma * sigma)).exp()
            }
            WaveType::Spike => {
                if self.ticks == 0 {
                    self.amplitude
                } else {
                    self.amplitude * (-(self.ticks as f64) * 0.5).exp()
                }
            }
            WaveType::Echo => {
                let decay_factor = (-(self.ticks as f64) * 0.1).exp();
                self.amplitude * decay_factor * (phase_offset).sin()
            }
        }
    }

    /// Él-e még a hullám
    pub fn is_alive(&self) -> bool {
        self.amplitude > 0.01 && self.state != WaveState::Dead
    }

    /// Csillapítás alkalmazása
    pub fn apply_decay(&mut self) {
        self.amplitude *= 1.0 - self.decay;
        if self.amplitude < 0.01 {
            self.state = WaveState::Dead;
        } else if self.amplitude < 0.3 {
            self.state = WaveState::Decaying;
        }
    }
}

// ============================================================================
// ACTIVATION - Neuron aktiváció
// ============================================================================

/// Aktivációs függvény típusok
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ActivationFn {
    /// Sigmoid: 1 / (1 + e^-x)
    Sigmoid,
    /// ReLU: max(0, x)
    ReLU,
    /// Tanh: tanh(x)
    Tanh,
    /// Step: x > 0 ? 1 : 0
    Step,
    /// Linear: x
    Linear,
    /// Softplus: ln(1 + e^x)
    Softplus,
}

impl ActivationFn {
    /// Aktivációs függvény alkalmazása
    pub fn apply(&self, x: f64) -> f64 {
        match self {
            Self::Sigmoid => 1.0 / (1.0 + (-x).exp()),
            Self::ReLU => x.max(0.0),
            Self::Tanh => x.tanh(),
            Self::Step => {
                if x > 0.0 {
                    1.0
                } else {
                    0.0
                }
            }
            Self::Linear => x,
            Self::Softplus => (1.0 + x.exp()).ln(),
        }
    }

    /// Derivált (backprop-hoz)
    pub fn derivative(&self, x: f64) -> f64 {
        match self {
            Self::Sigmoid => {
                let s = self.apply(x);
                s * (1.0 - s)
            }
            Self::ReLU => {
                if x > 0.0 {
                    1.0
                } else {
                    0.0
                }
            }
            Self::Tanh => 1.0 - x.tanh().powi(2),
            Self::Step => 0.0,
            Self::Linear => 1.0,
            Self::Softplus => 1.0 / (1.0 + (-x).exp()),
        }
    }
}

// ============================================================================
// NEURON STATE - Neuron állapot
// ============================================================================

/// Neuron állapot egy node-hoz
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NeuronState {
    /// Node ID
    pub node_id: String,
    /// Bemeneti összeg
    pub input_sum: f64,
    /// Aktivációs szint
    pub activation: f64,
    /// Küszöb érték (threshold)
    pub threshold: f64,
    /// Aktivációs függvény
    pub activation_fn: ActivationFn,
    /// Refrakter időszak (nem tüzelhet)
    pub refractory_ticks: u64,
    /// Utolsó tüzelés tick-je
    pub last_fire_tick: Option<u64>,
    /// Összegyűjtött hullámok amplitúdója
    pub accumulated_waves: f64,
    /// Érkezett hullámok száma
    pub wave_count: u64,
}

impl NeuronState {
    pub fn new(node_id: impl Into<String>) -> Self {
        Self {
            node_id: node_id.into(),
            input_sum: 0.0,
            activation: 0.0,
            threshold: 0.5,
            activation_fn: ActivationFn::Sigmoid,
            refractory_ticks: 0,
            last_fire_tick: None,
            accumulated_waves: 0.0,
            wave_count: 0,
        }
    }

    /// Küszöb beállítása
    pub fn with_threshold(mut self, threshold: f64) -> Self {
        self.threshold = threshold;
        self
    }

    /// Aktivációs függvény beállítása
    pub fn with_activation(mut self, activation_fn: ActivationFn) -> Self {
        self.activation_fn = activation_fn;
        self
    }

    /// Hullám fogadása
    pub fn receive_wave(&mut self, wave: &Wave) {
        let value = wave.current_value().abs();
        self.accumulated_waves += value;
        self.wave_count += 1;
        self.input_sum += value;
    }

    /// Aktiváció számítása
    pub fn compute_activation(&mut self) -> f64 {
        self.activation = self.activation_fn.apply(self.input_sum);
        self.activation
    }

    /// Tüzel-e (threshold felett)
    pub fn should_fire(&self) -> bool {
        self.activation > self.threshold && self.refractory_ticks == 0
    }

    /// Tüzelés után reset
    pub fn fire(&mut self, current_tick: u64) {
        self.last_fire_tick = Some(current_tick);
        self.refractory_ticks = 3; // 3 tick refrakter periódus
        self.input_sum = 0.0;
        self.accumulated_waves = 0.0;
        self.wave_count = 0;
    }

    /// Tick frissítés
    pub fn tick(&mut self) {
        if self.refractory_ticks > 0 {
            self.refractory_ticks -= 1;
        }
        // Idővel csökkenő input (leaky integration)
        self.input_sum *= 0.9;
    }
}

// ============================================================================
// INTERFERENCE - Hullám interferencia
// ============================================================================

/// Interferencia eredmény
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Interference {
    /// Érintett node ID
    pub node_id: String,
    /// Résztvevő hullámok
    pub wave_ids: Vec<String>,
    /// Eredő amplitúdó
    pub resultant_amplitude: f64,
    /// Konstruktív (erősítő) vagy destruktív (gyengítő)
    pub is_constructive: bool,
    /// Időpont
    pub timestamp: DateTime<Utc>,
}

impl Interference {
    /// Interferencia számítása két hullám között
    pub fn compute(node_id: &str, waves: &[&Wave]) -> Self {
        let wave_ids: Vec<String> = waves.iter().map(|w| w.id.clone()).collect();

        // Szuperpozíció - hullámok összeadása
        let total_amplitude: f64 = waves.iter().map(|w| w.current_value()).sum();
        let avg_amplitude: f64 =
            waves.iter().map(|w| w.amplitude).sum::<f64>() / waves.len() as f64;

        // Konstruktív ha az eredő nagyobb mint az átlag
        let is_constructive = total_amplitude.abs() > avg_amplitude;

        Self {
            node_id: node_id.to_string(),
            wave_ids,
            resultant_amplitude: total_amplitude,
            is_constructive,
            timestamp: Utc::now(),
        }
    }
}

// ============================================================================
// NEUROGRAPH - Teljes neural hálózat
// ============================================================================

/// NeuroGraph - CodeGraph + Neural Network + Wave Propagation
pub struct NeuroGraph {
    /// Alap CodeGraph
    pub graph: CodeGraph,
    /// Neuron állapotok (node_id -> state)
    neurons: Arc<RwLock<HashMap<String, NeuronState>>>,
    /// Aktív hullámok
    waves: Arc<RwLock<HashMap<String, Wave>>>,
    /// Interferencia log
    interferences: Arc<RwLock<Vec<Interference>>>,
    /// Globális tick számláló
    tick: Arc<RwLock<u64>>,
    /// Learning rate (Hebbian learning)
    pub learning_rate: f64,
}

impl NeuroGraph {
    /// Új NeuroGraph
    pub fn new() -> Self {
        Self {
            graph: CodeGraph::new(),
            neurons: Arc::new(RwLock::new(HashMap::new())),
            waves: Arc::new(RwLock::new(HashMap::new())),
            interferences: Arc::new(RwLock::new(Vec::new())),
            tick: Arc::new(RwLock::new(0)),
            learning_rate: 0.1,
        }
    }

    /// CodeGraph-ból létrehozás
    pub fn from_graph(graph: CodeGraph) -> Self {
        let mut ng = Self {
            graph,
            neurons: Arc::new(RwLock::new(HashMap::new())),
            waves: Arc::new(RwLock::new(HashMap::new())),
            interferences: Arc::new(RwLock::new(Vec::new())),
            tick: Arc::new(RwLock::new(0)),
            learning_rate: 0.1,
        };
        ng.init_neurons();
        ng
    }

    /// Neuronok inicializálása a graf alapján
    fn init_neurons(&mut self) {
        let blocks = self.graph.all_blocks();
        let mut neurons = self.neurons.write().unwrap();

        for block in blocks {
            let neuron = NeuronState::new(&block.id).with_threshold(0.5 * block.importance);
            neurons.insert(block.id, neuron);
        }
    }

    /// Block hozzáadása (neuronnal együtt)
    pub fn add_block(&self, block: CodeBlock) -> HopeResult<String> {
        let id = self.graph.add(block.clone())?;

        let mut neurons = self.neurons.write().unwrap();
        let neuron = NeuronState::new(&id).with_threshold(0.5 * block.importance);
        neurons.insert(id.clone(), neuron);

        Ok(id)
    }

    // ==================== WAVE OPERATIONS ====================

    /// Hullám indítása egy node-ból
    pub fn emit_wave(&self, origin_id: &str, payload: &str, wave_type: WaveType) -> Option<String> {
        // Ellenőrzés hogy létezik-e a node
        self.graph.get(origin_id)?;

        let wave = Wave::new(origin_id, payload, wave_type);
        let wave_id = wave.id.clone();

        let mut waves = self.waves.write().unwrap();
        waves.insert(wave_id.clone(), wave);

        Some(wave_id)
    }

    /// Hullám indítása custom paraméterekkel
    pub fn emit_wave_custom(&self, wave: Wave) -> Option<String> {
        self.graph.get(&wave.origin_id)?;

        let wave_id = wave.id.clone();
        let mut waves = self.waves.write().unwrap();
        waves.insert(wave_id.clone(), wave);

        Some(wave_id)
    }

    /// Egy tick végrehajtása (hullám propagáció)
    pub fn tick(&self) -> TickResult {
        let mut result = TickResult::default();

        // Tick számláló növelése
        {
            let mut tick = self.tick.write().unwrap();
            *tick += 1;
            result.tick = *tick;
        }

        let current_tick = result.tick;

        // Neuronok tick-je
        {
            let mut neurons = self.neurons.write().unwrap();
            for neuron in neurons.values_mut() {
                neuron.tick();
            }
        }

        // Hullámok propagálása
        let mut new_positions: HashMap<String, HashSet<String>> = HashMap::new();
        let mut dead_waves: Vec<String> = Vec::new();
        let mut node_waves: HashMap<String, Vec<String>> = HashMap::new();

        {
            let waves = self.waves.read().unwrap();

            for (wave_id, wave) in waves.iter() {
                if !wave.is_alive() {
                    dead_waves.push(wave_id.clone());
                    continue;
                }

                // Hullám terjedése szomszédos node-okra
                let mut next_positions = HashSet::new();

                for pos in &wave.current_positions {
                    // Szomszédok keresése
                    if let Some(block) = self.graph.get(pos) {
                        for conn in &block.connections {
                            if !wave.visited.contains(&conn.target_id) {
                                // Súlyozott terjedés
                                if conn.strength >= wave.amplitude * 0.5 {
                                    next_positions.insert(conn.target_id.clone());

                                    // Node-hoz érkező hullámok nyilvántartása
                                    node_waves
                                        .entry(conn.target_id.clone())
                                        .or_default()
                                        .push(wave_id.clone());
                                }
                            }
                        }
                    }
                }

                if !next_positions.is_empty() {
                    new_positions.insert(wave_id.clone(), next_positions);
                    result.propagations += 1;
                }
            }
        }

        // Hullámok frissítése
        {
            let mut waves = self.waves.write().unwrap();

            // Pozíciók frissítése
            for (wave_id, positions) in new_positions {
                if let Some(wave) = waves.get_mut(&wave_id) {
                    wave.visited.extend(positions.iter().cloned());
                    wave.current_positions = positions;
                    wave.ticks += 1;
                    wave.apply_decay();
                }
            }

            // Halott hullámok törlése
            for wave_id in dead_waves {
                waves.remove(&wave_id);
                result.waves_died += 1;
            }

            result.active_waves = waves.len();
        }

        // Interferencia számítás és neuron aktiváció
        {
            let waves = self.waves.read().unwrap();
            let mut neurons = self.neurons.write().unwrap();
            let mut interferences = self.interferences.write().unwrap();

            for (node_id, wave_ids) in node_waves {
                let arriving_waves: Vec<&Wave> =
                    wave_ids.iter().filter_map(|wid| waves.get(wid)).collect();

                if arriving_waves.len() > 1 {
                    // Interferencia!
                    let interference = Interference::compute(&node_id, &arriving_waves);
                    result.interferences += 1;

                    if interference.is_constructive {
                        result.constructive += 1;
                    } else {
                        result.destructive += 1;
                    }

                    interferences.push(interference.clone());

                    // Neuron aktiválása interferencia eredővel
                    if let Some(neuron) = neurons.get_mut(&node_id) {
                        neuron.input_sum += interference.resultant_amplitude;
                    }
                } else if arriving_waves.len() == 1 {
                    // Egyetlen hullám - direkt aktiváció
                    if let Some(neuron) = neurons.get_mut(&node_id) {
                        neuron.receive_wave(arriving_waves[0]);
                    }
                }
            }

            // Neuron tüzelés ellenőrzése
            for neuron in neurons.values_mut() {
                neuron.compute_activation();
                if neuron.should_fire() {
                    neuron.fire(current_tick);
                    result.neurons_fired += 1;
                }
            }
        }

        result
    }

    /// Többszörös tick futtatása
    pub fn run(&self, ticks: u64) -> Vec<TickResult> {
        (0..ticks).map(|_| self.tick()).collect()
    }

    /// Futtatás amíg van aktív hullám
    pub fn run_until_calm(&self, max_ticks: u64) -> Vec<TickResult> {
        let mut results = Vec::new();

        for _ in 0..max_ticks {
            let result = self.tick();
            let done = result.active_waves == 0;
            results.push(result);

            if done {
                break;
            }
        }

        results
    }

    // ==================== QUERIES ====================

    /// Aktív hullámok száma
    pub fn active_wave_count(&self) -> usize {
        self.waves.read().unwrap().len()
    }

    /// Összes hullám lekérése
    pub fn get_waves(&self) -> Vec<Wave> {
        self.waves.read().unwrap().values().cloned().collect()
    }

    /// Neuron állapot lekérése
    pub fn get_neuron(&self, node_id: &str) -> Option<NeuronState> {
        self.neurons.read().unwrap().get(node_id).cloned()
    }

    /// Összes neuron
    pub fn get_neurons(&self) -> Vec<NeuronState> {
        self.neurons.read().unwrap().values().cloned().collect()
    }

    /// Legaktívabb neuronok
    pub fn most_active_neurons(&self, limit: usize) -> Vec<NeuronState> {
        let neurons = self.neurons.read().unwrap();
        let mut sorted: Vec<_> = neurons.values().cloned().collect();
        sorted.sort_by(|a, b| b.activation.partial_cmp(&a.activation).unwrap());
        sorted.truncate(limit);
        sorted
    }

    /// Interferencia log
    pub fn get_interferences(&self) -> Vec<Interference> {
        self.interferences.read().unwrap().clone()
    }

    /// Jelenlegi tick
    pub fn current_tick(&self) -> u64 {
        *self.tick.read().unwrap()
    }

    // ==================== CONVENIENCE METHODS ====================

    /// Gondolat hullám (Spike)
    pub fn think(&self, thought: &str, importance: f64) -> Option<String> {
        let block_id = self.graph.think(thought, importance).ok()?;

        // Neuron hozzáadása
        {
            let mut neurons = self.neurons.write().unwrap();
            neurons.insert(block_id.clone(), NeuronState::new(&block_id));
        }

        // Hullám indítása
        self.emit_wave(&block_id, thought, WaveType::Spike)
    }

    /// Emlék hullám (Gaussian)
    pub fn remember(&self, memory: &str, importance: f64) -> Option<String> {
        let block_id = self.graph.remember(memory, importance).ok()?;

        {
            let mut neurons = self.neurons.write().unwrap();
            neurons.insert(block_id.clone(), NeuronState::new(&block_id));
        }

        self.emit_wave(&block_id, memory, WaveType::Gaussian)
    }

    /// Érzelem hullám (Sine - folyamatos)
    pub fn feel(&self, emotion: &str, intensity: f64) -> Option<String> {
        let block_id = self.graph.feel(emotion, intensity, None).ok()?;

        {
            let mut neurons = self.neurons.write().unwrap();
            neurons.insert(block_id.clone(), NeuronState::new(&block_id));
        }

        let wave = Wave::new(&block_id, emotion, WaveType::Sine)
            .with_amplitude(intensity)
            .with_frequency(0.5)
            .with_decay(0.05);

        self.emit_wave_custom(wave)
    }

    /// Statisztikák
    pub fn stats(&self) -> NeuroStats {
        let neurons = self.neurons.read().unwrap();
        let waves = self.waves.read().unwrap();
        let interferences = self.interferences.read().unwrap();
        let graph_stats = self.graph.stats();

        NeuroStats {
            total_neurons: neurons.len(),
            active_waves: waves.len(),
            total_interferences: interferences.len(),
            constructive_interferences: interferences.iter().filter(|i| i.is_constructive).count(),
            current_tick: *self.tick.read().unwrap(),
            graph_stats,
        }
    }
}

impl Default for NeuroGraph {
    fn default() -> Self {
        Self::new()
    }
}

/// Tick eredmény
#[derive(Debug, Clone, Default)]
pub struct TickResult {
    pub tick: u64,
    pub active_waves: usize,
    pub propagations: usize,
    pub waves_died: usize,
    pub interferences: usize,
    pub constructive: usize,
    pub destructive: usize,
    pub neurons_fired: usize,
}

/// NeuroGraph statisztikák
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NeuroStats {
    pub total_neurons: usize,
    pub active_waves: usize,
    pub total_interferences: usize,
    pub constructive_interferences: usize,
    pub current_tick: u64,
    pub graph_stats: super::code_graph::GraphStats,
}

// ============================================================================
// TESTS
// ============================================================================

#[cfg(test)]
mod tests {
    use super::super::code_graph::{BlockType, ConnectionType};
    use super::*;

    #[test]
    fn test_wave_creation() {
        let wave = Wave::new("node1", "test payload", WaveType::Impulse)
            .with_amplitude(0.8)
            .with_decay(0.1);

        assert_eq!(wave.amplitude, 0.8);
        assert!(wave.is_alive());
    }

    #[test]
    fn test_wave_decay() {
        let mut wave = Wave::new("node1", "test", WaveType::Impulse)
            .with_amplitude(0.5)
            .with_decay(0.5);

        wave.apply_decay();
        assert!(wave.amplitude < 0.5);
    }

    #[test]
    fn test_activation_functions() {
        assert!((ActivationFn::Sigmoid.apply(0.0) - 0.5).abs() < 0.001);
        assert_eq!(ActivationFn::ReLU.apply(-1.0), 0.0);
        assert_eq!(ActivationFn::ReLU.apply(1.0), 1.0);
        assert_eq!(ActivationFn::Step.apply(0.5), 1.0);
        assert_eq!(ActivationFn::Step.apply(-0.5), 0.0);
    }

    #[test]
    fn test_neurograph_wave_propagation() {
        let ng = NeuroGraph::new();

        // Block-ok hozzáadása
        let id1 = ng
            .add_block(CodeBlock::new("n1", "p", BlockType::Data, "c"))
            .unwrap();
        let id2 = ng
            .add_block(CodeBlock::new("n2", "p", BlockType::Data, "c"))
            .unwrap();
        let id3 = ng
            .add_block(CodeBlock::new("n3", "p", BlockType::Data, "c"))
            .unwrap();

        // Kapcsolatok
        ng.graph
            .connect(&id1, &id2, ConnectionType::ConnectsTo, 1.0);
        ng.graph
            .connect(&id2, &id3, ConnectionType::ConnectsTo, 1.0);

        // Hullám indítása
        ng.emit_wave(&id1, "test signal", WaveType::Impulse);

        // Futtatás
        let results = ng.run_until_calm(10);

        assert!(!results.is_empty());
        assert!(results.iter().any(|r| r.propagations > 0));
    }

    #[test]
    fn test_interference() {
        let wave1 = Wave::new("n1", "w1", WaveType::Sine).with_amplitude(0.5);
        let wave2 = Wave::new("n2", "w2", WaveType::Sine).with_amplitude(0.5);

        let interference = Interference::compute("target", &[&wave1, &wave2]);

        assert_eq!(interference.wave_ids.len(), 2);
    }

    #[test]
    fn test_neuron_firing() {
        let mut neuron = NeuronState::new("test").with_threshold(0.7); // Magasabb küszöb

        // Alulmarad a küszöbön (sigmoid(0.3) ≈ 0.574 < 0.7)
        neuron.input_sum = 0.3;
        neuron.compute_activation();
        assert!(!neuron.should_fire());

        // Átlépi a küszöböt (sigmoid(2.0) ≈ 0.88 > 0.7)
        neuron.input_sum = 2.0;
        neuron.compute_activation();
        assert!(neuron.should_fire());
    }

    #[test]
    fn test_think_and_propagate() {
        let ng = NeuroGraph::new();

        // Gondolat ami hullámot indít
        let wave_id = ng.think("This is a thought", 0.8);
        assert!(wave_id.is_some());

        let stats = ng.stats();
        assert_eq!(stats.active_waves, 1);
    }
}