rtreesearch 1.1.2

Structure-aware document search library and optional CLI over hierarchical document trees.
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
//! Tree Walker — Best-First Search over document trees.
//!
//! Core algorithm (ported from Python tree_searcher.py):
//! 1. Anchor Retrieval: use host-provided scores to find high-value entry nodes
//! 2. Tree Walk: BFS expansion from anchors along parent/child/sibling edges
//! 3. Path Aggregation: select best root-to-leaf paths as results

use std::collections::{BinaryHeap, HashMap, HashSet};

use crate::config::TreeSearchConfig;
use crate::document::{Document, Node};
use crate::scorer::heuristics::{
    build_query_plan_with_mode, check_phrase_match, check_title_match, compute_term_overlap,
    estimate_idf, is_generic_section, score_anchor, score_path, score_walk_node_with_input,
    QueryPlan, WalkScoreInput,
};

/// State in the Best-First Search frontier.
#[derive(Debug, Clone)]
pub struct SearchState {
    pub doc_id: String,
    pub node_id: String,
    pub score: f64,
    pub hop: usize,
    pub source: String,
    pub path: Vec<String>,
    pub max_ancestor_score: f64,
    /// Original retrieval anchor that initiated this walk branch.
    pub anchor_node_id: String,
}

impl PartialEq for SearchState {
    fn eq(&self, other: &Self) -> bool {
        self.cmp(other) == std::cmp::Ordering::Equal
    }
}
impl Eq for SearchState {}

impl PartialOrd for SearchState {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for SearchState {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // Max-heap: higher score first, then lower hop and lexical identity.
        self.score
            .total_cmp(&other.score)
            .then_with(|| other.hop.cmp(&self.hop))
            .then_with(|| other.doc_id.cmp(&self.doc_id))
            .then_with(|| other.node_id.cmp(&self.node_id))
            .then_with(|| self.max_ancestor_score.total_cmp(&other.max_ancestor_score))
            .then_with(|| other.anchor_node_id.cmp(&self.anchor_node_id))
            .then_with(|| other.source.cmp(&self.source))
            .then_with(|| other.path.cmp(&self.path))
    }
}

/// A scored root-to-answer path.
#[derive(Debug, Clone)]
pub struct PathResult {
    pub doc_id: String,
    pub doc_name: String,
    pub score: f64,
    pub anchor_node_id: String,
    pub target_node_id: String,
    pub path: Vec<PathNode>,
    pub snippet: String,
}

#[derive(Debug, Clone)]
pub struct PathNode {
    pub node_id: String,
    pub title: String,
}

/// Flat node from tree walker reranking.
#[derive(Debug, Clone)]
pub struct FlatNode {
    pub node_id: String,
    pub doc_id: String,
    pub doc_name: String,
    pub title: String,
    pub score: f64,
    pub text: String,
}

/// Tree searcher engine.
pub struct TreeSearcher<'a> {
    config: &'a TreeSearchConfig,
}

/// Reuses document lookups across every phase of one tree-search request.
struct DocumentTreeIndex<'a> {
    document: &'a Document,
    nodes: HashMap<&'a str, &'a Node>,
    parents: HashMap<String, Option<String>>,
    children: HashMap<String, Vec<String>>,
    depths: HashMap<String, u32>,
}

impl<'a> DocumentTreeIndex<'a> {
    /// Builds all structural maps in linear time for one document.
    fn new(document: &'a Document) -> Self {
        Self {
            document,
            nodes: document.build_node_map(),
            parents: document.build_parent_map(),
            children: document.build_children_map(),
            depths: document.build_depth_map(),
        }
    }

    /// Returns one uniquely identified node from the prebuilt lookup.
    fn node(&self, node_id: &str) -> Option<&'a Node> {
        self.nodes.get(node_id).copied()
    }
}

impl<'a> TreeSearcher<'a> {
    pub fn new(config: &'a TreeSearchConfig) -> Self {
        Self { config }
    }

    /// Run tree search across documents.
    pub fn search(
        &self,
        query: &str,
        documents: &[Document],
        initial_score_map: &HashMap<String, HashMap<String, f64>>,
    ) -> (Vec<PathResult>, Vec<FlatNode>) {
        let normalized_scores = initial_score_map
            .values()
            .flat_map(HashMap::values)
            .any(|score| !score.is_finite() || *score <= 0.0 || *score > 1.0)
            .then(|| normalize_initial_scores(initial_score_map));
        let initial_score_map = normalized_scores.as_ref().unwrap_or(initial_score_map);
        let plan = build_query_plan_with_mode(query, self.config.cjk_tokenizer);
        let mut all_paths: Vec<PathResult> = Vec::new();
        let mut all_walked_nodes: Vec<(String, String, f64, f64, usize)> = Vec::new();
        let indexes: Vec<DocumentTreeIndex<'_>> =
            documents.iter().map(DocumentTreeIndex::new).collect();

        // Sort documents by maximum initial score descending.
        let mut scored_docs: Vec<(f64, &DocumentTreeIndex<'_>, &HashMap<String, f64>)> = indexes
            .iter()
            .filter_map(|index| {
                let scores = initial_score_map.get(&index.document.doc_id)?;
                if scores.is_empty() {
                    return None;
                }
                let max_score = scores.values().cloned().fold(0.0_f64, f64::max);
                Some((max_score, index, scores))
            })
            .collect();
        scored_docs.sort_by(|left, right| {
            right
                .0
                .total_cmp(&left.0)
                .then_with(|| left.1.document.doc_id.cmp(&right.1.document.doc_id))
        });
        scored_docs.truncate(self.config.top_k_docs);

        for (_, index, doc_scores) in &scored_docs {
            // IDF estimation for large documents
            let idf = if !plan.terms.is_empty() && index.nodes.len() > 20 && doc_scores.len() >= 5 {
                let corpus: Vec<&str> = index
                    .nodes
                    .values()
                    .map(|node| bounded_prefix(&node.text, self.config.max_node_chars))
                    .collect();
                Some(estimate_idf(&plan.terms, &corpus))
            } else {
                None
            };

            // Stage 1: Anchor retrieval
            let anchors = self.select_anchors(index, doc_scores, &plan, idf.as_ref());
            if anchors.is_empty() {
                continue;
            }

            // Stage 2: Tree walk
            let (doc_paths, walked_states) =
                self.tree_walk(index, &anchors, doc_scores, &plan, idf.as_ref());
            all_paths.extend(doc_paths);

            for state in &walked_states {
                let initial_score = doc_scores.get(&state.node_id).copied().unwrap_or(0.0);
                let combined = 0.3 * state.score + 0.7 * initial_score;
                all_walked_nodes.push((
                    index.document.doc_id.clone(),
                    state.node_id.clone(),
                    combined,
                    initial_score,
                    state.hop,
                ));
            }
        }

        // Stage 3: Select top paths globally
        all_paths.sort_by(compare_paths);
        all_paths.truncate(self.config.path_top_k);

        // Build flat nodes with reranking
        let document_indexes: HashMap<&str, &DocumentTreeIndex<'_>> = indexes
            .iter()
            .map(|index| (index.document.doc_id.as_str(), index))
            .collect();
        let flat_nodes = self.build_flat_nodes(
            &all_paths,
            &all_walked_nodes,
            &document_indexes,
            initial_score_map,
            &plan,
        );

        (all_paths, flat_nodes)
    }

    // ---------------------------------------------------------------
    // Stage 1: Anchor Retrieval
    // ---------------------------------------------------------------

    fn select_anchors(
        &self,
        index: &DocumentTreeIndex<'_>,
        doc_scores: &HashMap<String, f64>,
        plan: &QueryPlan,
        idf: Option<&HashMap<String, f64>>,
    ) -> Vec<SearchState> {
        let max_candidates = self.config.anchor_top_k.saturating_mul(3);
        let threshold = if doc_scores.len() > max_candidates {
            let mut scores: Vec<f64> = doc_scores.values().copied().collect();
            scores.sort_by(|left, right| right.total_cmp(left));
            scores
                .get(max_candidates.saturating_sub(1))
                .copied()
                .unwrap_or(0.0)
        } else {
            0.0
        };

        let mut candidates: Vec<(f64, String, &Node)> = Vec::new();
        for (nid, &initial_score) in doc_scores {
            if initial_score < threshold {
                continue;
            }
            let node = match index.node(nid) {
                Some(n) => n,
                None => continue,
            };
            let depth = index.depths.get(nid).copied().unwrap_or(0);
            let node_text = bounded_prefix(&node.text, self.config.max_node_chars);
            let full_text = format!("{} {}", node.title, node_text);
            let a_score = score_anchor(
                initial_score,
                depth,
                check_title_match(&node.title, &plan.terms),
                check_phrase_match(&full_text, &plan.phrases),
                compute_term_overlap(node_text, &plan.terms, idf),
                6,
            );
            candidates.push((a_score, nid.clone(), node));
        }

        candidates.sort_by(|left, right| {
            right
                .0
                .total_cmp(&left.0)
                .then_with(|| left.1.cmp(&right.1))
        });

        let mut selected: Vec<SearchState> = Vec::new();
        let mut selected_paths: HashSet<String> = HashSet::new();

        let anchor_limit = self.config.anchor_top_k.min(self.config.max_anchor_per_doc);
        for (a_score, nid, _node) in &candidates {
            if selected.len() >= anchor_limit {
                break;
            }
            let path_to_root = path_to_root_via_map(nid, &index.parents);
            let path_key = path_to_root
                .iter()
                .take(3)
                .cloned()
                .collect::<Vec<_>>()
                .join(">");
            if selected_paths.contains(&path_key) {
                continue;
            }
            selected_paths.insert(path_key);

            selected.push(SearchState {
                doc_id: index.document.doc_id.clone(),
                node_id: nid.clone(),
                score: *a_score,
                hop: 0,
                source: "anchor".into(),
                path: path_to_root,
                max_ancestor_score: maximum_ancestor_score(nid, &index.parents, doc_scores),
                anchor_node_id: nid.clone(),
            });
        }

        selected
    }

    // ---------------------------------------------------------------
    // Stage 2: Tree Walk (Best-First Search)
    // ---------------------------------------------------------------

    fn tree_walk(
        &self,
        index: &DocumentTreeIndex<'_>,
        anchors: &[SearchState],
        doc_scores: &HashMap<String, f64>,
        plan: &QueryPlan,
        idf: Option<&HashMap<String, f64>>,
    ) -> (Vec<PathResult>, Vec<SearchState>) {
        let mut visited: HashSet<String> = HashSet::new();
        let mut frontier: BinaryHeap<SearchState> = BinaryHeap::new();
        let mut best_states: Vec<SearchState> = Vec::new();
        let mut expansion_count = 0;

        // Pre-cache term overlap for initially scored nodes.
        let mut overlap_cache: HashMap<String, f64> = HashMap::new();
        if !plan.terms.is_empty() {
            for nid in doc_scores.keys() {
                if let Some(node) = index.node(nid) {
                    overlap_cache.insert(
                        nid.clone(),
                        compute_term_overlap(
                            bounded_prefix(&node.text, self.config.max_node_chars),
                            &plan.terms,
                            idf,
                        ),
                    );
                }
            }
        }

        // Initialize frontier
        for anchor in anchors {
            frontier.push(anchor.clone());
        }

        while let Some(state) = frontier.pop() {
            if expansion_count >= self.config.max_expansions {
                break;
            }
            if visited.contains(&state.node_id) {
                continue;
            }
            visited.insert(state.node_id.clone());
            best_states.push(state.clone());
            expansion_count += 1;

            if state.score >= self.config.early_stop_score {
                break;
            }
            if state.score < self.config.min_frontier_score {
                continue;
            }
            if state.hop >= self.config.max_hops {
                continue;
            }

            // Expand neighbors
            let neighbors = get_neighbors(
                &state.node_id,
                &index.parents,
                &index.children,
                self.config.max_siblings,
            );
            for (nid, relation) in neighbors {
                if visited.contains(&nid) {
                    continue;
                }
                let node = match index.node(&nid) {
                    Some(n) => n,
                    None => continue,
                };
                let lexical = doc_scores.get(&nid).copied().unwrap_or(0.0);
                let overlap = overlap_cache.get(&nid).copied().unwrap_or_else(|| {
                    if !plan.terms.is_empty() {
                        let ov = compute_term_overlap(
                            bounded_prefix(&node.text, self.config.max_node_chars),
                            &plan.terms,
                            idf,
                        );
                        overlap_cache.insert(nid.clone(), ov);
                        ov
                    } else {
                        0.0
                    }
                });
                let new_max_anc = maximum_ancestor_score(&nid, &index.parents, doc_scores);
                let full_text = format!(
                    "{} {}",
                    node.title,
                    bounded_prefix(&node.text, self.config.max_node_chars)
                );
                let w_score = score_walk_node_with_input(WalkScoreInput {
                    lexical_score: lexical,
                    has_title_match: check_title_match(&node.title, &plan.terms),
                    has_phrase_match: check_phrase_match(&full_text, &plan.phrases),
                    body_term_overlap: overlap,
                    ancestor_support: new_max_anc,
                    hop: u32::try_from(state.hop.saturating_add(1)).unwrap_or(u32::MAX),
                    is_redundant: false,
                    max_hops: u32::try_from(self.config.max_hops).unwrap_or(u32::MAX),
                });

                let new_path = if relation == "child" {
                    let mut p = state.path.clone();
                    p.push(nid.clone());
                    p
                } else {
                    path_to_root_via_map(&nid, &index.parents)
                };

                frontier.push(SearchState {
                    doc_id: index.document.doc_id.clone(),
                    node_id: nid,
                    score: w_score,
                    hop: state.hop.saturating_add(1),
                    source: relation,
                    path: new_path,
                    max_ancestor_score: new_max_anc,
                    anchor_node_id: state.anchor_node_id.clone(),
                });
            }
        }

        let paths = self.states_to_paths(index, &mut best_states, doc_scores, plan);
        (paths, best_states)
    }

    fn states_to_paths(
        &self,
        index: &DocumentTreeIndex<'_>,
        states: &mut [SearchState],
        doc_scores: &HashMap<String, f64>,
        plan: &QueryPlan,
    ) -> Vec<PathResult> {
        states.sort_by(compare_states);
        let mut results: Vec<PathResult> = Vec::new();
        let mut seen_targets: HashSet<String> = HashSet::new();
        let max_to_process = self.config.path_top_k.saturating_mul(2);

        for state in states.iter() {
            if results.len() >= max_to_process {
                break;
            }
            if seen_targets.contains(&state.node_id) {
                continue;
            }
            seen_targets.insert(state.node_id.clone());

            let full_path = path_to_root_via_map(&state.node_id, &index.parents);
            let mut path_titles = Vec::new();
            let mut path_texts = Vec::new();
            let mut path_nodes = Vec::new();
            for pid in &full_path {
                if let Some(pnode) = index.node(pid) {
                    path_titles.push(pnode.title.clone());
                    path_texts
                        .push(bounded_prefix(&pnode.text, self.config.max_node_chars).to_string());
                    path_nodes.push(PathNode {
                        node_id: pid.clone(),
                        title: pnode.title.clone(),
                    });
                }
            }

            let p_score = score_path(
                state.score,
                &path_titles,
                &path_texts,
                &plan.terms,
                full_path.len(),
                doc_scores.get(&state.node_id).copied().unwrap_or(0.0),
                6,
            );

            let snippet = index
                .node(&state.node_id)
                .map(|node| {
                    bounded_prefix(&node.text, self.config.max_result_chars.min(300)).to_string()
                })
                .unwrap_or_default();

            results.push(PathResult {
                doc_id: index.document.doc_id.clone(),
                doc_name: index.document.doc_name.clone(),
                score: (p_score * 10000.0).round() / 10000.0,
                anchor_node_id: state.anchor_node_id.clone(),
                target_node_id: state.node_id.clone(),
                path: path_nodes,
                snippet,
            });
        }

        results.sort_by(compare_paths);
        results.truncate(self.config.path_top_k);
        results
    }

    // ---------------------------------------------------------------
    // Stage 3: Build flat nodes with reranking
    // ---------------------------------------------------------------

    fn build_flat_nodes(
        &self,
        _paths: &[PathResult],
        walked_nodes: &[(String, String, f64, f64, usize)],
        document_indexes: &HashMap<&str, &DocumentTreeIndex<'_>>,
        initial_score_map: &HashMap<String, HashMap<String, f64>>,
        plan: &QueryPlan,
    ) -> Vec<FlatNode> {
        let mut node_scores: HashMap<(String, String), f64> = HashMap::new();
        // 1. Base: host-provided initial scores.
        for (doc_id, doc_scores) in initial_score_map {
            for (nid, &initial_score) in doc_scores {
                node_scores.insert((doc_id.clone(), nid.clone()), initial_score);
            }
        }

        // 2. Generic section demotion + leaf preference (merged pass)
        for ((doc_id, nid), score) in node_scores.iter_mut() {
            let index = match document_indexes.get(doc_id.as_str()) {
                Some(index) => index,
                None => continue,
            };
            let node = match index.node(nid) {
                Some(n) => n,
                None => continue,
            };
            let depth = index.depths.get(nid.as_str()).copied().unwrap_or(0);

            // Generic section demotion
            if depth > 0 && is_generic_section(&node.title, depth) {
                let demote = if !plan.terms.is_empty() {
                    let base_title = node.title.to_lowercase();
                    !plan.terms.iter().any(|t| base_title.contains(t.as_str()))
                } else {
                    true
                };
                if demote {
                    *score *= 0.70;
                }
            }

            // Leaf preference
            if node.children.is_empty() && node.text.len() > 100 {
                *score *= 1.08;
            }
        }

        // 3. Walk boost
        for (doc_id, nid, combined_score, _initial_score, _hop) in walked_nodes {
            let key = (doc_id.clone(), nid.clone());
            if let Some(score) = node_scores.get_mut(&key) {
                *score += 0.15 * combined_score;
            }
        }

        // 4. Title match boost
        if !plan.terms.is_empty() {
            let keys: Vec<(String, String)> = node_scores.keys().cloned().collect();
            for key in &keys {
                let score = match node_scores.get(key) {
                    Some(&s) if s >= 0.05 => s,
                    _ => continue,
                };
                let index = match document_indexes.get(key.0.as_str()) {
                    Some(index) => index,
                    None => continue,
                };
                let node = match index.node(&key.1) {
                    Some(n) => n,
                    None => continue,
                };
                let title_lower = node.title.to_lowercase();
                let title_hits = plan
                    .terms
                    .iter()
                    .filter(|t| title_lower.contains(t.as_str()))
                    .count();
                if title_hits > 0 {
                    let title_overlap = title_hits as f64 / plan.terms.len() as f64;
                    let title_bonus = 0.15 * title_overlap * score.max(0.10);
                    node_scores.insert(key.clone(), score + title_bonus);
                }
            }
        }

        // Preserve relative ordering while restoring the public score range
        // after leaf, walk, and title boosts.
        let global_max = node_scores.values().copied().fold(0.0_f64, f64::max);
        if global_max > 1.0 {
            for score in node_scores.values_mut() {
                *score /= global_max;
            }
        }

        // Build flat node list
        let mut flat_nodes: Vec<FlatNode> = node_scores
            .into_iter()
            .filter_map(|((doc_id, nid), score)| {
                let index = document_indexes.get(doc_id.as_str())?;
                let node = index.node(&nid)?;
                Some(FlatNode {
                    node_id: nid,
                    doc_id: doc_id.clone(),
                    doc_name: index.document.doc_name.clone(),
                    title: node.title.clone(),
                    score: (score * 10000.0).round() / 10000.0,
                    text: bounded_prefix(&node.text, self.config.max_result_chars).to_string(),
                })
            })
            .collect();

        flat_nodes.sort_by(|left, right| {
            right
                .score
                .total_cmp(&left.score)
                .then_with(|| left.doc_id.cmp(&right.doc_id))
                .then_with(|| left.node_id.cmp(&right.node_id))
        });
        flat_nodes
    }
}

// ---------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------

/// Filters invalid direct walker inputs and globally calibrates raw scores.
fn normalize_initial_scores(
    scores: &HashMap<String, HashMap<String, f64>>,
) -> HashMap<String, HashMap<String, f64>> {
    let mut normalized: HashMap<String, HashMap<String, f64>> = scores
        .iter()
        .filter_map(|(document_id, node_scores)| {
            let valid: HashMap<String, f64> = node_scores
                .iter()
                .filter(|(_, score)| score.is_finite() && **score > 0.0)
                .map(|(node_id, score)| (node_id.clone(), *score))
                .collect();
            (!valid.is_empty()).then_some((document_id.clone(), valid))
        })
        .collect();
    let global_max = normalized
        .values()
        .flat_map(HashMap::values)
        .copied()
        .fold(0.0_f64, f64::max);
    if global_max > 1.0 {
        for score in normalized.values_mut().flat_map(HashMap::values_mut) {
            *score /= global_max;
        }
    }
    normalized
}

/// Returns at most `max_characters` without splitting a UTF-8 code point.
fn bounded_prefix(text: &str, max_characters: usize) -> &str {
    match text.char_indices().nth(max_characters) {
        Some((end, _)) => &text[..end],
        None => text,
    }
}

fn path_to_root_via_map(
    node_id: &str,
    parent_map: &HashMap<String, Option<String>>,
) -> Vec<String> {
    let mut path = Vec::new();
    let mut visited = HashSet::new();
    let mut current = Some(node_id.to_string());
    while let Some(nid) = current {
        if !visited.insert(nid.clone()) {
            return Vec::new();
        }
        path.push(nid.clone());
        current = parent_map.get(&nid).and_then(|p| p.clone());
    }
    path.reverse();
    path
}

/// Returns the strongest score on the strict ancestor chain of one node.
fn maximum_ancestor_score(
    node_id: &str,
    parent_map: &HashMap<String, Option<String>>,
    scores: &HashMap<String, f64>,
) -> f64 {
    let mut maximum = 0.0_f64;
    let mut visited = HashSet::new();
    let mut current = parent_map.get(node_id).and_then(Clone::clone);
    while let Some(node_id) = current {
        if !visited.insert(node_id.clone()) {
            break;
        }
        maximum = maximum.max(scores.get(&node_id).copied().unwrap_or(0.0));
        current = parent_map.get(&node_id).and_then(Clone::clone);
    }
    maximum
}

/// Orders traversal states deterministically for stable ranking and tests.
fn compare_states(left: &SearchState, right: &SearchState) -> std::cmp::Ordering {
    right
        .score
        .total_cmp(&left.score)
        .then_with(|| left.hop.cmp(&right.hop))
        .then_with(|| left.doc_id.cmp(&right.doc_id))
        .then_with(|| left.node_id.cmp(&right.node_id))
        .then_with(|| left.anchor_node_id.cmp(&right.anchor_node_id))
}

/// Orders path results by score and stable transport identities.
fn compare_paths(left: &PathResult, right: &PathResult) -> std::cmp::Ordering {
    right
        .score
        .total_cmp(&left.score)
        .then_with(|| left.doc_id.cmp(&right.doc_id))
        .then_with(|| left.target_node_id.cmp(&right.target_node_id))
        .then_with(|| left.anchor_node_id.cmp(&right.anchor_node_id))
}

fn get_neighbors(
    node_id: &str,
    parent_map: &HashMap<String, Option<String>>,
    children_map: &HashMap<String, Vec<String>>,
    max_siblings: usize,
) -> Vec<(String, String)> {
    let mut neighbors = Vec::new();

    // Children
    if let Some(children) = children_map.get(node_id) {
        for cid in children {
            neighbors.push((cid.clone(), "child".into()));
        }
    }

    // Parent
    if let Some(Some(pid)) = parent_map.get(node_id) {
        neighbors.push((pid.clone(), "parent".into()));

        // Siblings (via parent's children)
        if let Some(siblings) = children_map.get(pid.as_str()) {
            let mut count = 0;
            for sid in siblings {
                if sid != node_id && count < max_siblings {
                    neighbors.push((sid.clone(), "sibling".into()));
                    count += 1;
                }
            }
        }
    }

    neighbors
}

#[cfg(test)]
mod tests {
    use super::normalize_initial_scores;
    use std::collections::HashMap;

    #[test]
    fn direct_walker_scores_are_filtered_and_globally_calibrated() {
        let scores = HashMap::from([
            (
                "a".to_string(),
                HashMap::from([
                    ("high".to_string(), 20.0),
                    ("low".to_string(), 10.0),
                    ("nan".to_string(), f64::NAN),
                    ("negative".to_string(), -1.0),
                ]),
            ),
            (
                "b".to_string(),
                HashMap::from([("infinite".to_string(), f64::INFINITY)]),
            ),
        ]);

        let normalized = normalize_initial_scores(&scores);
        assert_eq!(normalized["a"]["high"], 1.0);
        assert_eq!(normalized["a"]["low"], 0.5);
        assert!(!normalized["a"].contains_key("nan"));
        assert!(!normalized["a"].contains_key("negative"));
        assert!(!normalized.contains_key("b"));
    }
}