m1nd-core 0.7.0

Core graph engine and reasoning primitives for m1nd.
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
// === crates/m1nd-core/src/activation.rs ===

use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap};
use std::time::Instant;

use crate::error::M1ndResult;
use crate::graph::Graph;
use crate::types::*;

// ---------------------------------------------------------------------------
// BloomFilter — probabilistic visited set (engine_fast.py BloomFilter)
// Replaces: engine_fast.py BloomFilter class
// ---------------------------------------------------------------------------

/// Double-hashing Bloom filter for fast visited checks.
/// FPR ~ (1 - e^(-kn/m))^k where k=hash count, n=insertions, m=bits.
pub struct BloomFilter {
    bits: Vec<u64>,
    num_bits: usize,
    num_hashes: u32,
}

impl BloomFilter {
    /// Create with capacity for `expected_items` at the given false-positive rate.
    pub fn with_capacity(expected_items: usize, fpr: f64) -> Self {
        let expected = expected_items.max(1);
        // m = -(n * ln(p)) / (ln(2)^2)
        let m = (-(expected as f64) * fpr.ln() / (2.0f64.ln().powi(2))) as usize;
        let num_bits = m.max(64);
        let num_words = num_bits.div_ceil(64);
        // k = (m/n) * ln(2)
        let k = ((num_bits as f64 / expected as f64) * 2.0f64.ln()).ceil() as u32;
        let num_hashes = k.clamp(1, 16);
        Self {
            bits: vec![0u64; num_words],
            num_bits,
            num_hashes,
        }
    }

    #[inline]
    fn compute_hashes(&self, item: u32, out: &mut [usize; 16]) -> u32 {
        let h1 = item.wrapping_mul(2654435761) as usize;
        let h2 = item.wrapping_mul(2246822519).wrapping_add(1) as usize;
        let m = self.num_bits;
        let k = self.num_hashes.min(16);
        for (i, slot) in out.iter_mut().enumerate().take(k as usize) {
            *slot = h1.wrapping_add(i.wrapping_mul(h2)) % m;
        }
        k
    }

    pub fn insert(&mut self, item: NodeId) {
        let mut hashes = [0usize; 16];
        let k = self.compute_hashes(item.0, &mut hashes);
        for &h in hashes.iter().take(k as usize) {
            self.bits[h >> 6] |= 1u64 << (h & 63);
        }
    }

    pub fn probably_contains(&self, item: NodeId) -> bool {
        let mut hashes = [0usize; 16];
        let k = self.compute_hashes(item.0, &mut hashes);
        for &h in hashes.iter().take(k as usize) {
            if self.bits[h >> 6] & (1u64 << (h & 63)) == 0 {
                return false;
            }
        }
        true
    }

    pub fn clear(&mut self) {
        self.bits.fill(0);
    }
}

// ---------------------------------------------------------------------------
// ActivationResult — query output (04-SPEC Section 4.2)
// Replaces: engine_v2.py ConnectomeEngine.query() return value
// ---------------------------------------------------------------------------

/// Per-node activation detail in a query result.
#[derive(Clone, Debug)]
pub struct ActivatedNode {
    pub node: NodeId,
    /// Combined activation score after dimension weighting + resonance bonus.
    pub activation: FiniteF32,
    /// Per-dimension scores [structural, semantic, temporal, causal].
    pub dimensions: [FiniteF32; 4],
    /// Number of dimensions that contributed above threshold.
    pub active_dimension_count: u8,
}

/// Result of a full activation query.
#[derive(Clone, Debug)]
pub struct ActivationResult {
    /// Activated nodes sorted by activation descending. Truncated to top_k.
    pub activated: Vec<ActivatedNode>,
    /// Seed nodes that initiated propagation.
    pub seeds: Vec<(NodeId, FiniteF32)>,
    /// Wall-clock time in nanoseconds.
    pub elapsed_ns: u64,
    /// Whether XLR over-cancellation fallback was triggered (FM-XLR-010).
    pub xlr_fallback_used: bool,
}

/// Per-dimension raw result before merging.
/// Replaces: engine_v2.py DimensionResult
#[derive(Clone, Debug)]
pub struct DimensionResult {
    /// Sparse map: node -> raw score for this dimension.
    pub scores: Vec<(NodeId, FiniteF32)>,
    /// Dimension that produced this result.
    pub dimension: Dimension,
    /// Elapsed nanoseconds for this dimension.
    pub elapsed_ns: u64,
}

// ---------------------------------------------------------------------------
// ActivationEngine — trait for propagation strategies
// Replaces: engine_fast.py HeapActivationEngine, WavefrontEngine, HybridEngine
// ---------------------------------------------------------------------------

/// Propagation strategy for structural activation (D1).
/// Concrete impls below — no dyn dispatch.
pub trait ActivationEngine: Send + Sync {
    /// Propagate from seeds through the graph. Returns sparse activation map.
    /// Replaces: engine_v2.py D1_Structural.activate()
    fn propagate(
        &self,
        graph: &Graph,
        seeds: &[(NodeId, FiniteF32)],
        config: &PropagationConfig,
    ) -> M1ndResult<DimensionResult>;
}

// ---------------------------------------------------------------------------
// WavefrontEngine — BFS depth-parallel (04-SPEC Section 2.1)
// Replaces: engine_fast.py WavefrontEngine
// ---------------------------------------------------------------------------

/// Breadth-first, depth-parallel spreading activation.
/// All active nodes at current depth fire simultaneously.
/// Signal accumulated via scatter-max into next depth's buffer.
pub struct WavefrontEngine;

impl Default for WavefrontEngine {
    fn default() -> Self {
        Self
    }
}

impl WavefrontEngine {
    pub fn new() -> Self {
        Self
    }
}

impl ActivationEngine for WavefrontEngine {
    fn propagate(
        &self,
        graph: &Graph,
        seeds: &[(NodeId, FiniteF32)],
        config: &PropagationConfig,
    ) -> M1ndResult<DimensionResult> {
        let start = Instant::now();
        let n = graph.num_nodes() as usize;
        if n == 0 || seeds.is_empty() {
            return Ok(DimensionResult {
                scores: Vec::new(),
                dimension: Dimension::Structural,
                elapsed_ns: start.elapsed().as_nanos() as u64,
            });
        }

        let threshold = config.threshold.get();
        let decay = config.decay.get();
        let max_depth = config.max_depth.min(20) as usize; // FM-ACT-012 cap

        // Dense activation buffer (scatter-max target)
        let mut activation = vec![0.0f32; n];
        let mut visited = vec![false; n];

        // Init seeds
        let mut frontier: Vec<NodeId> = Vec::new();
        for &(node, score) in seeds {
            let idx = node.as_usize();
            if idx < n {
                let s = score.get().min(config.saturation_cap.get());
                if s > activation[idx] {
                    activation[idx] = s;
                }
                if !visited[idx] {
                    frontier.push(node);
                    visited[idx] = true;
                }
            }
        }

        // BFS by depth
        for _depth in 0..max_depth {
            if frontier.is_empty() {
                break;
            }
            let mut next_frontier: Vec<NodeId> = Vec::new();

            for &src in &frontier {
                let src_act = activation[src.as_usize()];
                if src_act < threshold {
                    continue;
                }

                let range = graph.csr.out_range(src);
                for j in range {
                    let tgt = graph.csr.targets[j];
                    let w = graph.csr.read_weight(EdgeIdx::new(j as u32)).get();
                    let is_inhib = graph.csr.inhibitory[j];

                    let mut signal = src_act * w * decay;
                    if is_inhib {
                        // DEC-054: capped proportional suppression
                        signal = -signal * config.inhibitory_factor.get();
                    }

                    let tgt_idx = tgt.as_usize();
                    if tgt_idx >= n {
                        continue;
                    }

                    if !is_inhib && signal > threshold {
                        // Scatter-max: keep strongest arrival
                        if signal > activation[tgt_idx] {
                            activation[tgt_idx] = signal;
                        }
                        if !visited[tgt_idx] {
                            visited[tgt_idx] = true;
                            next_frontier.push(tgt);
                        }
                    } else if is_inhib {
                        // Inhibitory: subtract (but floor at 0)
                        activation[tgt_idx] = (activation[tgt_idx] + signal).max(0.0);
                    }
                }
            }

            frontier = next_frontier;
        }

        // Collect non-zero activations
        let mut scores: Vec<(NodeId, FiniteF32)> = activation
            .iter()
            .enumerate()
            .filter(|(_, &v)| v > 0.0)
            .map(|(i, &v)| (NodeId::new(i as u32), FiniteF32::new(v)))
            .collect();
        scores.sort_by(|a, b| b.1.cmp(&a.1));

        Ok(DimensionResult {
            scores,
            dimension: Dimension::Structural,
            elapsed_ns: start.elapsed().as_nanos() as u64,
        })
    }
}

// ---------------------------------------------------------------------------
// HeapEngine — priority-queue activation (04-SPEC Section 2.2)
// Replaces: engine_fast.py HeapActivationEngine
// ---------------------------------------------------------------------------

/// Entry in the max-heap for HeapEngine.
#[derive(Clone, Copy)]
struct HeapEntry {
    node: NodeId,
    activation: f32,
}

impl PartialEq for HeapEntry {
    fn eq(&self, other: &Self) -> bool {
        self.activation == other.activation
    }
}
impl Eq for HeapEntry {}
impl PartialOrd for HeapEntry {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for HeapEntry {
    fn cmp(&self, other: &Self) -> Ordering {
        self.activation.total_cmp(&other.activation)
    }
}

/// Max-heap priority queue propagation. Processes strongest signal first.
/// Early-terminates when heap top drops below threshold.
/// Uses BloomFilter for fast visited checks.
pub struct HeapEngine;

impl Default for HeapEngine {
    fn default() -> Self {
        Self
    }
}

impl HeapEngine {
    pub fn new() -> Self {
        Self
    }
}

impl ActivationEngine for HeapEngine {
    fn propagate(
        &self,
        graph: &Graph,
        seeds: &[(NodeId, FiniteF32)],
        config: &PropagationConfig,
    ) -> M1ndResult<DimensionResult> {
        let start = Instant::now();
        let n = graph.num_nodes() as usize;
        if n == 0 || seeds.is_empty() {
            return Ok(DimensionResult {
                scores: Vec::new(),
                dimension: Dimension::Structural,
                elapsed_ns: start.elapsed().as_nanos() as u64,
            });
        }

        let threshold = config.threshold.get();
        let decay = config.decay.get();

        let mut activation = vec![0.0f32; n];
        let mut bloom = BloomFilter::with_capacity(n, 0.01);
        let mut heap = BinaryHeap::new();

        // Init seeds
        for &(node, score) in seeds {
            let idx = node.as_usize();
            if idx < n {
                let s = score.get().min(config.saturation_cap.get());
                activation[idx] = s;
                heap.push(HeapEntry {
                    node,
                    activation: s,
                });
                bloom.insert(node);
            }
        }

        let mut depth_counter = 0u32;
        let max_ops = (n as u32)
            .saturating_mul(config.max_depth as u32)
            .max(10000);

        while let Some(entry) = heap.pop() {
            if entry.activation < threshold {
                break; // Early termination
            }
            depth_counter += 1;
            if depth_counter > max_ops {
                break;
            }

            let src = entry.node;
            let src_act = activation[src.as_usize()];

            let range = graph.csr.out_range(src);
            for j in range {
                let tgt = graph.csr.targets[j];
                let w = graph.csr.read_weight(EdgeIdx::new(j as u32)).get();
                let is_inhib = graph.csr.inhibitory[j];

                let mut signal = src_act * w * decay;
                if is_inhib {
                    signal = -signal * config.inhibitory_factor.get();
                }

                let tgt_idx = tgt.as_usize();
                if tgt_idx >= n {
                    continue;
                }

                if !is_inhib && signal > threshold && signal > activation[tgt_idx] {
                    activation[tgt_idx] = signal;
                    if !bloom.probably_contains(tgt) {
                        bloom.insert(tgt);
                        heap.push(HeapEntry {
                            node: tgt,
                            activation: signal,
                        });
                    }
                } else if is_inhib {
                    activation[tgt_idx] = (activation[tgt_idx] + signal).max(0.0);
                }
            }
        }

        // Collect results
        let mut scores: Vec<(NodeId, FiniteF32)> = activation
            .iter()
            .enumerate()
            .filter(|(_, &v)| v > 0.0)
            .map(|(i, &v)| (NodeId::new(i as u32), FiniteF32::new(v)))
            .collect();
        scores.sort_by(|a, b| b.1.cmp(&a.1));

        Ok(DimensionResult {
            scores,
            dimension: Dimension::Structural,
            elapsed_ns: start.elapsed().as_nanos() as u64,
        })
    }
}

// ---------------------------------------------------------------------------
// HybridEngine — auto-select wavefront vs heap (04-SPEC Section 2.2)
// Replaces: engine_fast.py HybridEngine
// Heuristic: seed_ratio < 0.001 AND avg_degree < 8 -> heap, else wavefront
// ---------------------------------------------------------------------------

/// Selects HeapEngine or WavefrontEngine at runtime based on graph topology.
pub struct HybridEngine {
    wavefront: WavefrontEngine,
    heap: HeapEngine,
}

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

impl HybridEngine {
    pub fn new() -> Self {
        Self {
            wavefront: WavefrontEngine::new(),
            heap: HeapEngine::new(),
        }
    }

    /// Returns true if heap is preferred for these parameters.
    /// Heuristic from engine_fast.py: seed_ratio < 0.001 AND avg_degree < 8.
    fn prefer_heap(graph: &Graph, seed_count: usize) -> bool {
        let seed_ratio = seed_count as f64 / graph.num_nodes().max(1) as f64;
        seed_ratio < 0.001 && graph.avg_degree() < 8.0
    }
}

impl ActivationEngine for HybridEngine {
    fn propagate(
        &self,
        graph: &Graph,
        seeds: &[(NodeId, FiniteF32)],
        config: &PropagationConfig,
    ) -> M1ndResult<DimensionResult> {
        if Self::prefer_heap(graph, seeds.len()) {
            self.heap.propagate(graph, seeds, config)
        } else {
            self.wavefront.propagate(graph, seeds, config)
        }
    }
}

// ---------------------------------------------------------------------------
// D2 Semantic dimension wrapper
// Replaces: engine_v2.py D2_Semantic.activate()
// ---------------------------------------------------------------------------

/// Semantic dimension scoring. Delegates to SemanticEngine.
/// Returns DimensionResult with Dimension::Semantic.
pub fn activate_semantic(
    _graph: &Graph,
    semantic: &crate::semantic::SemanticEngine,
    query: &str,
    top_k: usize,
) -> M1ndResult<DimensionResult> {
    let start = Instant::now();
    let scores = semantic.query_fast(_graph, query, top_k)?;
    Ok(DimensionResult {
        scores,
        dimension: Dimension::Semantic,
        elapsed_ns: start.elapsed().as_nanos() as u64,
    })
}

// ---------------------------------------------------------------------------
// D3 Temporal dimension wrapper
// Replaces: engine_v2.py D3_Temporal.activate()
// ---------------------------------------------------------------------------

/// Temporal dimension scoring. Combines recency * weight + frequency * weight.
/// Replaces: engine_v2.py D3_Temporal.activate()
pub fn activate_temporal(
    graph: &Graph,
    seeds: &[(NodeId, FiniteF32)],
    weights: &TemporalWeights,
) -> M1ndResult<DimensionResult> {
    let start = Instant::now();
    let n = graph.num_nodes() as usize;
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs_f64())
        .unwrap_or(0.0);

    let half_life_secs = 168.0 * 3600.0; // 7 days in seconds
    let k = std::f64::consts::LN_2 / half_life_secs;

    let mut scores = Vec::new();
    for &(node, seed_strength) in seeds {
        let idx = node.as_usize();
        if idx >= n {
            continue;
        }
        let last_mod = graph.nodes.last_modified[idx];
        let age_secs = (now - last_mod).max(0.0);
        let recency = (-k * age_secs).exp() as f32;
        let frequency = graph.nodes.change_frequency[idx].get();

        let score = recency * weights.recency.get() + frequency * weights.frequency.get();
        let combined = score * seed_strength.get();
        if combined > 0.0 {
            scores.push((node, FiniteF32::new(combined)));
        }
    }
    scores.sort_by(|a, b| b.1.cmp(&a.1));

    Ok(DimensionResult {
        scores,
        dimension: Dimension::Temporal,
        elapsed_ns: start.elapsed().as_nanos() as u64,
    })
}

// ---------------------------------------------------------------------------
// D4 Causal dimension wrapper
// Replaces: engine_v2.py D4_Causal.activate()
// ---------------------------------------------------------------------------

/// Causal dimension scoring. Forward causal + backward causal * 0.7.
/// Replaces: engine_v2.py D4_Causal.activate()
pub fn activate_causal(
    graph: &Graph,
    seeds: &[(NodeId, FiniteF32)],
    config: &PropagationConfig,
) -> M1ndResult<DimensionResult> {
    let start = Instant::now();
    let n = graph.num_nodes() as usize;
    if n == 0 || seeds.is_empty() {
        return Ok(DimensionResult {
            scores: Vec::new(),
            dimension: Dimension::Causal,
            elapsed_ns: start.elapsed().as_nanos() as u64,
        });
    }

    let threshold = config.threshold.get();
    let decay = config.decay.get();
    let max_depth = config.max_depth.min(20) as usize;

    // Forward causal propagation: only follow edges with causal_strength > 0
    let mut activation = vec![0.0f32; n];
    let mut frontier: Vec<NodeId> = Vec::new();
    let mut visited = vec![false; n];

    for &(node, score) in seeds {
        let idx = node.as_usize();
        if idx < n {
            activation[idx] = score.get();
            if !visited[idx] {
                frontier.push(node);
                visited[idx] = true;
            }
        }
    }

    for _depth in 0..max_depth {
        if frontier.is_empty() {
            break;
        }
        let mut next_frontier = Vec::new();
        for &src in &frontier {
            let src_act = activation[src.as_usize()];
            if src_act < threshold {
                continue;
            }
            let range = graph.csr.out_range(src);
            for j in range {
                let causal = graph.csr.causal_strengths[j].get();
                if causal <= 0.0 {
                    continue; // Skip non-causal edges
                }
                let tgt = graph.csr.targets[j];
                let w = graph.csr.read_weight(EdgeIdx::new(j as u32)).get();
                let signal = src_act * w * causal * decay;
                let tgt_idx = tgt.as_usize();
                if tgt_idx < n && signal > threshold && signal > activation[tgt_idx] {
                    activation[tgt_idx] = signal;
                    if !visited[tgt_idx] {
                        visited[tgt_idx] = true;
                        next_frontier.push(tgt);
                    }
                }
            }
        }
        frontier = next_frontier;
    }

    // Backward causal (reverse CSR) with 0.7 multiplier
    let mut back_frontier: Vec<NodeId> = Vec::new();
    let mut back_visited = vec![false; n];
    for &(node, _) in seeds {
        let idx = node.as_usize();
        if idx < n && !back_visited[idx] {
            back_frontier.push(node);
            back_visited[idx] = true;
        }
    }

    for _depth in 0..max_depth {
        if back_frontier.is_empty() {
            break;
        }
        let mut next = Vec::new();
        for &src in &back_frontier {
            let src_act = activation[src.as_usize()]
                .max(seeds.iter().find(|s| s.0 == src).map_or(0.0, |s| s.1.get()));
            if src_act < threshold {
                continue;
            }
            let range = graph.csr.in_range(src);
            for j in range {
                let fwd_idx = graph.csr.rev_edge_idx[j];
                let causal = graph.csr.causal_strengths[fwd_idx.as_usize()].get();
                if causal <= 0.0 {
                    continue;
                }
                let rev_src = graph.csr.rev_sources[j];
                let w = graph.csr.read_weight(fwd_idx).get();
                let signal = src_act * w * causal * decay * 0.7; // backward discount
                let idx = rev_src.as_usize();
                if idx < n && signal > threshold && signal > activation[idx] {
                    activation[idx] = signal;
                    if !back_visited[idx] {
                        back_visited[idx] = true;
                        next.push(rev_src);
                    }
                }
            }
        }
        back_frontier = next;
    }

    let mut scores: Vec<(NodeId, FiniteF32)> = activation
        .iter()
        .enumerate()
        .filter(|(_, &v)| v > 0.0)
        .map(|(i, &v)| (NodeId::new(i as u32), FiniteF32::new(v)))
        .collect();
    scores.sort_by(|a, b| b.1.cmp(&a.1));

    Ok(DimensionResult {
        scores,
        dimension: Dimension::Causal,
        elapsed_ns: start.elapsed().as_nanos() as u64,
    })
}

// ---------------------------------------------------------------------------
// Dimension merger — combine 4 dimensions + resonance bonus
// Replaces: engine_v2.py ConnectomeEngine._merge_dimensions()
// FM-ACT-001 FIX: check 4-dim BEFORE 3-dim (dead elif in Python)
// ---------------------------------------------------------------------------

/// PageRank boost factor.
const PAGERANK_BOOST: f32 = 0.1;
/// Minimum score threshold for dimension contribution.
const DIM_CONTRIBUTION_THRESHOLD: f32 = 0.01;

/// Merge four DimensionResults into a single ranked ActivationResult.
/// Applies DIMENSION_WEIGHTS, resonance bonus (FM-ACT-001 fix: 4-dim checked first).
pub fn merge_dimensions(
    results: &[DimensionResult; 4],
    top_k: usize,
) -> M1ndResult<ActivationResult> {
    let start = Instant::now();

    // Compute adaptive weights: if a dimension is empty, redistribute
    let mut weights = DIMENSION_WEIGHTS;
    let mut total_active_weight = 0.0f32;
    let mut active_mask = [false; 4];
    for (i, r) in results.iter().enumerate() {
        if !r.scores.is_empty() {
            active_mask[i] = true;
            total_active_weight += weights[i];
        }
    }

    // DEC-049: adaptive redistribution
    if total_active_weight > 0.0 && total_active_weight < 1.0 {
        for i in 0..4 {
            if active_mask[i] {
                weights[i] /= total_active_weight;
            } else {
                weights[i] = 0.0;
            }
        }
    }

    // Merge into per-node combined scores
    let mut node_scores: HashMap<u32, [f32; 4]> = HashMap::new();
    for (dim_idx, result) in results.iter().enumerate() {
        for &(node, score) in &result.scores {
            let entry = node_scores.entry(node.0).or_insert([0.0; 4]);
            entry[dim_idx] = score.get();
        }
    }

    // Build activated nodes
    let mut activated: Vec<ActivatedNode> = node_scores
        .iter()
        .map(|(&node_id, dims)| {
            // Weighted sum
            let mut combined = 0.0f32;
            let mut dim_count = 0u8;
            for i in 0..4 {
                combined += dims[i] * weights[i];
                if dims[i] > DIM_CONTRIBUTION_THRESHOLD {
                    dim_count += 1;
                }
            }

            // FM-ACT-001 FIX: check 4-dim BEFORE 3-dim (dead elif in Python)
            if dim_count >= 4 {
                combined *= RESONANCE_BONUS_4DIM;
            } else if dim_count >= 3 {
                combined *= RESONANCE_BONUS_3DIM;
            }

            ActivatedNode {
                node: NodeId::new(node_id),
                activation: FiniteF32::new(combined),
                dimensions: [
                    FiniteF32::new(dims[0]),
                    FiniteF32::new(dims[1]),
                    FiniteF32::new(dims[2]),
                    FiniteF32::new(dims[3]),
                ],
                active_dimension_count: dim_count,
            }
        })
        .collect();

    // Sort descending, truncate to top_k
    activated.sort_by(|a, b| b.activation.cmp(&a.activation));
    activated.truncate(top_k);

    Ok(ActivationResult {
        activated,
        seeds: Vec::new(), // filled by caller
        elapsed_ns: start.elapsed().as_nanos() as u64,
        xlr_fallback_used: false,
    })
}