sheaf 0.1.7

Hierarchical structure, community detection, reconciliation, and conformal prediction
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
//! Tree validation and health checking utilities.
//!
//! Provides tools to verify tree structure integrity and detect common issues:
//! - Orphaned nodes (no parent connection)
//! - Cycles in the tree
//! - Missing children references
//! - Inconsistent depth calculations
//!
//! # Example
//!
//! ```rust,ignore
//! use sheaf::tree::{RaptorTree, HealthCheck};
//!
//! let tree = RaptorTree::new(config);
//! // ... build tree ...
//!
//! let report = tree.health_check();
//! if !report.is_healthy() {
//!     for issue in report.issues {
//!         eprintln!("{}: {}", issue.severity, issue.message);
//!     }
//! }
//! ```

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

use super::RaptorTree;

/// Severity level for validation issues.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Severity {
    /// Informational, not a problem.
    Info,
    /// Something unusual but not necessarily wrong.
    Warning,
    /// A problem that should be fixed.
    Error,
    /// A critical issue that may cause failures.
    Critical,
}

impl std::fmt::Display for Severity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Severity::Info => write!(f, "INFO"),
            Severity::Warning => write!(f, "WARN"),
            Severity::Error => write!(f, "ERROR"),
            Severity::Critical => write!(f, "CRITICAL"),
        }
    }
}

/// A single validation issue found during health check.
#[derive(Debug, Clone)]
pub struct ValidationIssue {
    /// Severity of the issue.
    pub severity: Severity,
    /// Human-readable description.
    pub message: String,
    /// Optional node ID involved.
    pub node_id: Option<usize>,
    /// Optional additional context.
    pub context: Option<String>,
}

impl ValidationIssue {
    /// Create a new validation issue.
    pub fn new(severity: Severity, message: impl Into<String>) -> Self {
        Self {
            severity,
            message: message.into(),
            node_id: None,
            context: None,
        }
    }

    /// Add a node ID to this issue.
    pub fn with_node(mut self, id: usize) -> Self {
        self.node_id = Some(id);
        self
    }

    /// Add context to this issue.
    pub fn with_context(mut self, ctx: impl Into<String>) -> Self {
        self.context = Some(ctx.into());
        self
    }
}

impl std::fmt::Display for ValidationIssue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[{}] {}", self.severity, self.message)?;
        if let Some(id) = self.node_id {
            write!(f, " (node {})", id)?;
        }
        if let Some(ctx) = &self.context {
            write!(f, " - {}", ctx)?;
        }
        Ok(())
    }
}

/// Report from a validation/health check.
#[derive(Debug, Clone, Default)]
pub struct ValidationReport {
    /// All issues found.
    pub issues: Vec<ValidationIssue>,
}

impl ValidationReport {
    /// Create an empty report.
    pub fn new() -> Self {
        Self { issues: Vec::new() }
    }

    /// Add an issue to the report.
    pub fn add(&mut self, issue: ValidationIssue) {
        self.issues.push(issue);
    }

    /// Add an info-level issue.
    pub fn info(&mut self, message: impl Into<String>) {
        self.add(ValidationIssue::new(Severity::Info, message));
    }

    /// Add a warning-level issue.
    pub fn warn(&mut self, message: impl Into<String>) {
        self.add(ValidationIssue::new(Severity::Warning, message));
    }

    /// Add an error-level issue.
    pub fn error(&mut self, message: impl Into<String>) {
        self.add(ValidationIssue::new(Severity::Error, message));
    }

    /// Add a critical-level issue.
    pub fn critical(&mut self, message: impl Into<String>) {
        self.add(ValidationIssue::new(Severity::Critical, message));
    }

    /// Check if the report contains no errors or critical issues.
    pub fn is_healthy(&self) -> bool {
        !self.issues.iter().any(|i| i.severity >= Severity::Error)
    }

    /// Check if there are any issues at all.
    pub fn is_clean(&self) -> bool {
        self.issues.is_empty()
    }

    /// Get issues of a specific severity or higher.
    pub fn issues_at_level(&self, min_severity: Severity) -> Vec<&ValidationIssue> {
        self.issues
            .iter()
            .filter(|i| i.severity >= min_severity)
            .collect()
    }

    /// Count issues by severity.
    pub fn counts(&self) -> HashMap<Severity, usize> {
        let mut counts = HashMap::new();
        for issue in &self.issues {
            *counts.entry(issue.severity).or_default() += 1;
        }
        counts
    }
}

impl std::fmt::Display for ValidationReport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.is_clean() {
            return write!(f, "Validation passed: no issues found");
        }

        let counts = self.counts();
        write!(f, "Validation report: ")?;

        let parts: Vec<String> = [
            (Severity::Critical, "critical"),
            (Severity::Error, "errors"),
            (Severity::Warning, "warnings"),
            (Severity::Info, "info"),
        ]
        .iter()
        .filter_map(|(sev, name)| counts.get(sev).map(|c| format!("{} {}", c, name)))
        .collect();

        writeln!(f, "{}", parts.join(", "))?;

        for issue in &self.issues {
            writeln!(f, "  {}", issue)?;
        }

        Ok(())
    }
}

/// Health report with additional statistics.
#[derive(Debug, Clone)]
pub struct HealthReport {
    /// Validation issues.
    pub validation: ValidationReport,
    /// Total number of nodes.
    pub node_count: usize,
    /// Number of leaf nodes.
    pub leaf_count: usize,
    /// Maximum depth of the tree.
    pub max_depth: usize,
    /// Average branching factor.
    pub avg_branching_factor: f64,
}

impl HealthReport {
    /// Check if the tree is healthy (no errors or critical issues).
    pub fn is_healthy(&self) -> bool {
        self.validation.is_healthy()
    }
}

impl std::fmt::Display for HealthReport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Tree Health Report")?;
        writeln!(f, "==================")?;
        writeln!(f, "Nodes: {} ({} leaves)", self.node_count, self.leaf_count)?;
        writeln!(f, "Max depth: {}", self.max_depth)?;
        writeln!(f, "Avg branching factor: {:.2}", self.avg_branching_factor)?;
        writeln!(f)?;
        write!(f, "{}", self.validation)
    }
}

/// Trait for types that can be health-checked.
pub trait HealthCheck {
    /// Perform a health check and return a report.
    fn health_check(&self) -> HealthReport;

    /// Quick check: returns true if healthy.
    fn is_healthy(&self) -> bool {
        self.health_check().is_healthy()
    }
}

impl<T, S> HealthCheck for RaptorTree<T, S> {
    fn health_check(&self) -> HealthReport {
        let node_count = self.len();

        // Build parent/child maps from the node adjacency.
        let mut parents: HashMap<usize, usize> = HashMap::new();
        let mut children: HashMap<usize, Vec<usize>> = HashMap::new();

        for node in self.iter() {
            if !node.children.is_empty() {
                let _ = children.insert(node.id, node.children.clone());
                for &child in &node.children {
                    // If a child has multiple parents, keep the first and report later.
                    let _ = parents.entry(child).or_insert(node.id);
                }
            }
        }

        let mut validation = validate_tree_structure(&parents, &children, node_count);

        // RAPTOR-specific structure checks.
        // - leaves should be level 0 and have no children
        // - internal nodes should have at least one child
        // - child level should be strictly less than parent level
        for node in self.iter() {
            if node.level == 0 {
                if !node.is_leaf() {
                    validation.add(
                        ValidationIssue::new(Severity::Error, "level 0 node is not a leaf")
                            .with_node(node.id),
                    );
                }
                if !node.children.is_empty() {
                    validation.add(
                        ValidationIssue::new(
                            Severity::Error,
                            "leaf node unexpectedly has children",
                        )
                        .with_node(node.id),
                    );
                }
            } else {
                if node.is_leaf() {
                    validation.add(
                        ValidationIssue::new(
                            Severity::Error,
                            "internal node is unexpectedly a leaf",
                        )
                        .with_node(node.id),
                    );
                }
                if node.children.is_empty() {
                    validation.add(
                        ValidationIssue::new(Severity::Warning, "internal node has no children")
                            .with_node(node.id),
                    );
                }
                for &child_id in &node.children {
                    if let Some(child) = self.get_node(child_id) {
                        if child.level >= node.level {
                            validation.add(
                                ValidationIssue::new(
                                    Severity::Error,
                                    "child level violation (child.level >= parent.level)",
                                )
                                .with_node(node.id)
                                .with_context(format!(
                                    "parent level {}, child {} level {}",
                                    node.level, child_id, child.level
                                )),
                            );
                        }
                    } else {
                        validation.add(
                            ValidationIssue::new(Severity::Error, "child id does not exist")
                                .with_node(node.id)
                                .with_context(format!("missing child id {child_id}")),
                        );
                    }
                }
            }
        }

        // Light heuristic: the number of nodes per level should generally decrease.
        for level in 1..self.depth() {
            let prev = self.get_level(level - 1).map(|v| v.len()).unwrap_or(0);
            let curr = self.get_level(level).map(|v| v.len()).unwrap_or(0);
            if curr > prev {
                validation.warn(format!(
                    "level {level} has more nodes than prior level ({curr} > {prev})"
                ));
            }
        }

        let leaf_count = self.leaves().len();
        let max_depth = self.depth().saturating_sub(1);
        let avg_branching_factor = if node_count == 0 {
            0.0
        } else {
            let total_children: usize = self.iter().map(|n| n.children.len()).sum();
            total_children as f64 / node_count as f64
        };

        HealthReport {
            validation,
            node_count,
            leaf_count,
            max_depth,
            avg_branching_factor,
        }
    }
}

/// Validate that a parent-child relationship forms a proper tree.
///
/// # Arguments
/// * `parents` - Map from node ID to parent ID (root has no entry)
/// * `children` - Map from node ID to child IDs
/// * `node_count` - Total number of nodes
///
/// # Returns
/// A validation report with any issues found.
pub fn validate_tree_structure(
    parents: &HashMap<usize, usize>,
    children: &HashMap<usize, Vec<usize>>,
    node_count: usize,
) -> ValidationReport {
    let mut report = ValidationReport::new();

    // Find root(s) - nodes without parents
    let all_nodes: HashSet<usize> = (0..node_count).collect();
    let nodes_with_parents: HashSet<usize> = parents.keys().copied().collect();
    let roots: Vec<usize> = all_nodes.difference(&nodes_with_parents).copied().collect();

    if roots.is_empty() {
        report.critical("No root node found - tree has cycles");
    } else if roots.len() > 1 {
        report.warn(format!("Multiple roots found: {:?}", roots));
    }

    // Check for orphaned nodes (not reachable from root)
    let mut reachable = HashSet::new();
    let mut stack = roots.clone();

    while let Some(node) = stack.pop() {
        if reachable.insert(node) {
            if let Some(node_children) = children.get(&node) {
                stack.extend(node_children);
            }
        }
    }

    let orphans: Vec<usize> = all_nodes.difference(&reachable).copied().collect();
    if !orphans.is_empty() {
        report.add(
            ValidationIssue::new(
                Severity::Error,
                format!("{} orphaned nodes not reachable from root", orphans.len()),
            )
            .with_context(format!("first few: {:?}", &orphans[..orphans.len().min(5)])),
        );
    }

    // Check parent-child consistency
    for (child, parent) in parents {
        if let Some(parent_children) = children.get(parent) {
            if !parent_children.contains(child) {
                report.add(
                    ValidationIssue::new(
                        Severity::Error,
                        "Parent-child inconsistency: child claims parent but parent doesn't list child",
                    )
                    .with_node(*child)
                    .with_context(format!("parent: {}", parent)),
                );
            }
        } else {
            report.add(
                ValidationIssue::new(Severity::Error, "Parent node has no children list")
                    .with_node(*parent),
            );
        }
    }

    // Check for cycles using DFS with coloring
    let mut visited = HashSet::new();
    let mut in_stack = HashSet::new();

    fn detect_cycle(
        node: usize,
        children: &HashMap<usize, Vec<usize>>,
        visited: &mut HashSet<usize>,
        in_stack: &mut HashSet<usize>,
    ) -> bool {
        if in_stack.contains(&node) {
            return true; // Cycle detected
        }
        if visited.contains(&node) {
            return false;
        }

        let _ = visited.insert(node);
        let _ = in_stack.insert(node);

        if let Some(node_children) = children.get(&node) {
            for &child in node_children {
                if detect_cycle(child, children, visited, in_stack) {
                    return true;
                }
            }
        }

        let _ = in_stack.remove(&node);
        false
    }

    for &root in &roots {
        if detect_cycle(root, children, &mut visited, &mut in_stack) {
            report.critical("Cycle detected in tree structure");
            break;
        }
    }

    report
}

#[cfg(test)]
#[allow(clippy::unwrap_used, unused_results)]
mod tests {
    use super::*;
    use crate::hierarchy::{RaptorTree, TreeConfig};
    use proptest::prelude::*;

    fn chunk_cluster(ids: &[usize], fanout: usize) -> Vec<Vec<usize>> {
        let mut out = Vec::new();
        let mut cur = Vec::new();
        for &id in ids {
            cur.push(id);
            if cur.len() == fanout {
                out.push(cur);
                cur = Vec::new();
            }
        }
        if !cur.is_empty() {
            out.push(cur);
        }
        out
    }

    #[test]
    fn test_severity_ordering() {
        assert!(Severity::Info < Severity::Warning);
        assert!(Severity::Warning < Severity::Error);
        assert!(Severity::Error < Severity::Critical);
    }

    #[test]
    fn test_validation_report_healthy() {
        let mut report = ValidationReport::new();
        report.info("Just some info");
        report.warn("A warning");

        assert!(report.is_healthy()); // No errors or critical

        report.error("An error");
        assert!(!report.is_healthy());
    }

    #[test]
    fn test_validation_issue_display() {
        let issue = ValidationIssue::new(Severity::Error, "Something wrong")
            .with_node(42)
            .with_context("additional info");

        let s = format!("{}", issue);
        assert!(s.contains("ERROR"));
        assert!(s.contains("Something wrong"));
        assert!(s.contains("42"));
        assert!(s.contains("additional info"));
    }

    #[test]
    fn test_validate_valid_tree() {
        // Simple tree: 0 -> [1, 2]
        let parents: HashMap<usize, usize> = [(1, 0), (2, 0)].into_iter().collect();
        let children: HashMap<usize, Vec<usize>> = [(0, vec![1, 2])].into_iter().collect();

        let report = validate_tree_structure(&parents, &children, 3);
        assert!(report.is_healthy());
    }

    #[test]
    fn test_validate_orphaned_nodes() {
        // Orphaned nodes are nodes that are not reachable from any root.
        //
        // Construct:
        // - root: 0 (no parent, no children)
        // - cycle component: 1 <-> 2 (both have parents, so they are not roots)
        //
        // Nodes {1,2} are not reachable from root {0} => orphaned.
        let parents: HashMap<usize, usize> = [(1, 2), (2, 1)].into_iter().collect();
        let children: HashMap<usize, Vec<usize>> =
            [(1, vec![2]), (2, vec![1])].into_iter().collect();

        let report = validate_tree_structure(&parents, &children, 3);
        assert!(!report.is_healthy());
        assert!(report.issues.iter().any(|i| i.message.contains("orphaned")));
    }

    #[test]
    fn test_validate_multiple_roots() {
        // Two separate trees: 0 -> [1], 2 -> [3]
        let parents: HashMap<usize, usize> = [(1, 0), (3, 2)].into_iter().collect();
        let children: HashMap<usize, Vec<usize>> =
            [(0, vec![1]), (2, vec![3])].into_iter().collect();

        let report = validate_tree_structure(&parents, &children, 4);
        // Multiple roots is a warning, not error
        assert!(report.is_healthy());
        assert!(report
            .issues
            .iter()
            .any(|i| i.message.contains("Multiple roots")));
    }

    #[test]
    fn raptor_health_check_is_healthy_for_basic_build() {
        let items = vec![
            "a".to_string(),
            "b".to_string(),
            "c".to_string(),
            "d".to_string(),
        ];
        let tree = RaptorTree::build(
            items,
            TreeConfig::default()
                .with_max_depth(3)
                .with_fanout(2)
                .with_min_cluster_size(2),
            chunk_cluster,
            |group| {
                group
                    .iter()
                    .map(|s| (*s).clone())
                    .collect::<Vec<_>>()
                    .join(" ")
            },
        )
        .unwrap();

        let report = tree.health_check();
        assert!(report.is_healthy(), "{}", report);
    }

    proptest! {
        #[test]
        fn raptor_health_check_is_healthy_for_chunk_cluster(
            items in proptest::collection::vec(".{1,40}", 1..80),
            fanout in 2usize..10,
            max_depth in 1usize..6,
        ) {
            let tree = RaptorTree::build(
                items,
                TreeConfig::default()
                    .with_max_depth(max_depth)
                    .with_fanout(fanout)
                    .with_min_cluster_size(2),
                chunk_cluster,
                |group| group.iter().map(|s| (*s).clone()).collect::<Vec<_>>().join(" "),
            ).unwrap();

            let report = tree.health_check();
            prop_assert!(report.is_healthy(), "{}", report);
        }
    }
}