bonsai-ninja-sdk 0.2.4

Ergonomic facade SDK for bonsai-ninja.
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
//! `tree` — workspace navigation surface (SDK aggregator).
//!
//! Composes browse facts (`bonsai_browse::Locator`) with security
//! findings (`bonsai_security::run_taint_analysis`) and the
//! resolved call graph to render a hierarchical view where every
//! file row carries its connections: finding ids, flow ids, the
//! most-severe flow's entry/exit, and cross-file caller/callee
//! edges. Directory rows aggregate severity counts up the tree.
//!
//! Lives in `bonsai_sdk` (not `bonsai_browse`) because it's an
//! aggregator across both browse + security data — the SDK facade
//! is the architectural home for cross-service surfaces per
//! `docs/contributing/architecture.mdx`.

use ahash::AHashMap;
use bonsai_browse::{
    file_path_excluded_by_filters, file_path_matches_filter, workspace_relative_path, Locator,
};
use bonsai_callgraph::EdgeKind;
use bonsai_common::{FuncId, Precision, SymbolId};
use bonsai_security::rule::Severity;
use bonsai_security::{run_taint_analysis, Finding, FindingMatch, Rulepack, TaintAnalysisOptions};
use bonsai_workspace::Workspace;
use serde::Serialize;

#[derive(Clone, Debug, Default)]
pub struct TreeFilters<'a> {
    pub max_depth: Option<usize>,
    pub file: Option<&'a str>,
    pub exclude_files: &'a [String],
    pub severity: Option<Severity>,
    pub limit: usize,
    pub follow: usize,
    pub max_finding_ids_per_file: Option<usize>,
    pub max_flow_ids_per_file: Option<usize>,
    pub max_cross_file_edges_per_file: Option<usize>,
}

#[derive(Clone, Debug, Serialize)]
pub struct TreeOut {
    pub analysis_complete: bool,
    pub analysis_incomplete_reasons: Vec<String>,
    pub roots: Vec<TreeNode>,
    pub summary: TreeSummary,
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct TreeSummary {
    pub total_files: usize,
    /// Total files seen by the underlying scan, regardless of
    /// `--max-depth` truncation. `total_files` only counts files
    /// that survived the depth limit and made it into the rendered
    /// tree, so on a `--max-depth 3` walk of a `crates/X/src/lib.rs`
    /// layout `total_files` is `0`. Renderers should surface this
    /// when it differs from `total_files` so users don't see
    /// "0 files" and assume the workspace is empty.
    #[serde(default, skip_serializing_if = "is_zero_usize")]
    pub total_files_scanned: usize,
    pub total_dirs: usize,
    pub total_findings: usize,
    pub severity_counts: SeverityHistogram,
    pub indexed_complete: usize,
    pub indexed_stale: usize,
    pub indexed_missing: usize,
}

#[allow(clippy::trivially_copy_pass_by_ref)] // serde `skip_serializing_if` callback signature
fn is_zero_usize(n: &usize) -> bool {
    *n == 0
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum NodeKind {
    Dir,
    File,
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct SeverityHistogram {
    pub critical: usize,
    pub high: usize,
    pub medium: usize,
    pub low: usize,
    pub info: usize,
}

impl SeverityHistogram {
    fn add(&mut self, sev: Option<Severity>) {
        match sev {
            Some(Severity::Critical) => self.critical += 1,
            Some(Severity::High) => self.high += 1,
            Some(Severity::Medium) => self.medium += 1,
            Some(Severity::Low) => self.low += 1,
            Some(Severity::Info) => self.info += 1,
            None => {}
        }
    }

    fn merge(&mut self, other: &Self) {
        self.critical += other.critical;
        self.high += other.high;
        self.medium += other.medium;
        self.low += other.low;
        self.info += other.info;
    }

    pub fn total(&self) -> usize {
        self.critical + self.high + self.medium + self.low + self.info
    }

    fn max(&self) -> Option<Severity> {
        if self.critical > 0 {
            Some(Severity::Critical)
        } else if self.high > 0 {
            Some(Severity::High)
        } else if self.medium > 0 {
            Some(Severity::Medium)
        } else if self.low > 0 {
            Some(Severity::Low)
        } else if self.info > 0 {
            Some(Severity::Info)
        } else {
            None
        }
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct TreeNode {
    pub kind: NodeKind,
    pub name: String,
    pub locator: Locator,
    pub depth: usize,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub finding_ids: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub flow_ids: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_severity: Option<Severity>,
    #[serde(default, skip_serializing_if = "is_zero_histogram")]
    pub finding_severity_counts: SeverityHistogram,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub cross_file_callers_in: Vec<CrossEdge>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub cross_file_callees_out: Vec<CrossEdge>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub most_severe_flow: Option<MostSevereFlowSummary>,
    pub indexed: IndexedStatus,
    pub render_priority: u8,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub children: Vec<TreeNode>,
    pub truncated: TreeTruncation,
}

fn is_zero_histogram(h: &SeverityHistogram) -> bool {
    h.total() == 0
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct TreeTruncation {
    pub children_dropped: usize,
    pub finding_ids_dropped: usize,
    pub flow_ids_dropped: usize,
    pub callers_in_dropped: usize,
    pub callees_out_dropped: usize,
}

#[derive(Clone, Debug, Serialize)]
pub struct CrossEdge {
    pub caller: Locator,
    pub callee: Locator,
    pub call_site: Locator,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub edge_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flow_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub finding_id: Option<String>,
    pub precision: Precision,
    pub edge_kind: EdgeKind,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub external: Option<ExternalKind>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ExternalKind {
    Stdlib,
    ThirdParty,
    Ffi,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum IndexedStatus {
    Complete,
    Stale,
    Missing,
}

impl Default for IndexedStatus {
    fn default() -> Self {
        IndexedStatus::Complete
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct MostSevereFlowSummary {
    pub flow_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub finding_id: Option<String>,
    pub severity: Severity,
    pub enters_at: Locator,
    pub exits_at: Locator,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub chain_display: Vec<String>,
    pub extends_beyond_workspace: bool,
}

/// Build the tree view. When a rulepack is configured on the
/// workspace, runs `taint_analysis` to populate finding/flow data.
/// Without one, severity counts are zero and `most_severe_flow` is
/// `None` everywhere — same graceful degradation pattern as every
/// other security-touching command.
pub fn tree(
    ws: &Workspace,
    rulepack: Option<&Rulepack>,
    filters: &TreeFilters<'_>,
) -> anyhow::Result<TreeOut> {
    let max_findings_cap = effective_optional_cap(filters.max_finding_ids_per_file, 3);
    let max_flow_cap = effective_optional_cap(filters.max_flow_ids_per_file, 5);
    let max_edge_cap = effective_optional_cap(filters.max_cross_file_edges_per_file, 3);
    let limit = if filters.limit == 0 {
        usize::MAX
    } else {
        filters.limit
    };

    let report = match rulepack {
        Some(pack) => Some(run_taint_analysis(
            ws,
            pack,
            TaintAnalysisOptions::default().semantic_precision_only(),
        )?),
        None => None,
    };

    let mut findings_by_file: AHashMap<String, Vec<Finding>> = AHashMap::new();
    if let Some(rep) = report.as_ref() {
        for cf in &rep.findings {
            findings_by_file
                .entry(workspace_relative_path(ws, &cf.finding.sink.file))
                .or_default()
                .push(cf.finding.clone());
        }
    }

    let resolved = ws.cached_resolved_call_graph();
    let cross_edges = build_cross_edges(&resolved, ws);

    let mut files: Vec<(String, bonsai_common::FileId)> = ws
        .vfs()
        .all_files()
        .into_iter()
        .filter_map(|fid| {
            let abs = ws.vfs().path(fid).ok()?;
            let absolute = abs.display().to_string();
            let rel = workspace_relative_path(ws, &absolute);
            if let Some(needle) = filters.file {
                if !file_path_matches_filter(ws, &absolute, needle) {
                    return None;
                }
            }
            if file_path_excluded_by_filters(ws, &absolute, filters.exclude_files) {
                return None;
            }
            Some((rel, fid))
        })
        .collect();
    files.sort_by(|a, b| a.0.cmp(&b.0));

    let mut tree_root = DirBuilder::default();
    let mut finding_incomplete_reasons: Vec<String> = Vec::new();
    let root_name = ws
        .db()
        .workspace_root()
        .and_then(|root| root.file_name().map(|name| name.to_string_lossy().into_owned()))
        .unwrap_or_else(|| ".".to_string());
    for (rel, file_id) in &files {
        let relative_segments: Vec<&str> = rel.split('/').filter(|s| !s.is_empty()).collect();
        if relative_segments.is_empty() {
            continue;
        }
        let mut segments = Vec::with_capacity(relative_segments.len() + 1);
        segments.push(root_name.as_str());
        segments.extend(relative_segments);
        let language = ws
            .db()
            .adapter_for(*file_id)
            .map(|a| a.language_id().as_str().to_string());

        let file_findings = findings_by_file.get(rel.as_str()).cloned().unwrap_or_default();
        let mut finding_ids: Vec<String> = file_findings.iter().map(|f| f.finding_id.clone()).collect();
        finding_ids.sort();
        finding_ids.dedup();

        let mut sev_counts = SeverityHistogram::default();
        for f in &file_findings {
            sev_counts.add(f.severity);
        }
        let max_severity = sev_counts.max();

        if let Some(min_sev) = filters.severity {
            if max_severity.is_none_or(|s| s < min_sev) {
                continue;
            }
        }
        for finding in &file_findings {
            finding_incomplete_reasons.extend(finding_analysis_incomplete_reasons(finding));
        }

        let mut flow_ids: Vec<String> = file_findings
            .iter()
            .flat_map(|finding| finding.flow_ids().map(str::to_owned))
            .collect();
        flow_ids.sort();
        flow_ids.dedup();

        let raw_finding_count = finding_ids.len();
        let raw_flow_count = flow_ids.len();
        finding_ids.truncate(max_findings_cap);
        flow_ids.truncate(max_flow_cap);

        let most_severe_flow = file_findings
            .iter()
            .filter(|f| f.severity.is_some())
            .max_by_key(|f| f.severity)
            .map(|finding| build_most_severe_flow(finding, ws));

        let callers_in = cross_edges
            .into_callers
            .get(rel.as_str())
            .cloned()
            .unwrap_or_default();
        let callees_out = cross_edges
            .out_callees
            .get(rel.as_str())
            .cloned()
            .unwrap_or_default();
        let raw_callers_count = callers_in.len();
        let raw_callees_count = callees_out.len();
        let callers_in: Vec<CrossEdge> = callers_in.into_iter().take(max_edge_cap).collect();
        let callees_out: Vec<CrossEdge> = callees_out.into_iter().take(max_edge_cap).collect();

        let render_priority = compute_render_priority(max_severity, sev_counts.total());

        let truncated = TreeTruncation {
            children_dropped: 0,
            finding_ids_dropped: raw_finding_count.saturating_sub(finding_ids.len()),
            flow_ids_dropped: raw_flow_count.saturating_sub(flow_ids.len()),
            callers_in_dropped: raw_callers_count.saturating_sub(callers_in.len()),
            callees_out_dropped: raw_callees_count.saturating_sub(callees_out.len()),
        };

        let basename = segments.last().copied().unwrap_or(rel.as_str()).to_string();
        let depth = segments.len().saturating_sub(1);
        let locator = Locator {
            file: rel.clone(),
            line: 0,
            column: 0,
            language,
            ..Locator::default()
        };

        let node = TreeNode {
            kind: NodeKind::File,
            name: basename,
            locator,
            depth,
            finding_ids,
            flow_ids,
            max_severity,
            finding_severity_counts: sev_counts,
            cross_file_callers_in: callers_in,
            cross_file_callees_out: callees_out,
            most_severe_flow,
            indexed: IndexedStatus::Complete,
            render_priority,
            children: Vec::new(),
            truncated,
        };

        tree_root.insert(&segments, node);
    }

    // Track files scanned before depth truncation. Without this the
    // header on a `--max-depth 3` walk of `crates/X/src/lib.rs`
    // (files at depth 4) reads "tree — 0 files" and looks like the
    // workspace is empty even though hundreds of files were
    // indexed.
    let mut summary = TreeSummary {
        total_files_scanned: files.len(),
        ..TreeSummary::default()
    };
    let max_depth = filters.max_depth.unwrap_or(usize::MAX);
    let roots = tree_root.finalize("", 0, max_depth, limit, &mut summary);
    let mut analysis_incomplete_reasons = tree_analysis_incomplete_reasons(&roots, &summary);
    analysis_incomplete_reasons.extend(finding_incomplete_reasons);
    analysis_incomplete_reasons.sort();
    analysis_incomplete_reasons.dedup();
    let analysis_complete = analysis_incomplete_reasons.is_empty();

    Ok(TreeOut {
        analysis_complete,
        analysis_incomplete_reasons,
        roots,
        summary,
    })
}

fn effective_optional_cap(value: Option<usize>, default: usize) -> usize {
    match value {
        Some(0) => usize::MAX,
        Some(limit) => limit,
        None => default,
    }
}

#[derive(Default)]
#[allow(clippy::struct_field_names)] // Names line up with TreeSummary truncation counters.
struct TreeTruncationTotals {
    children_dropped: usize,
    finding_ids_dropped: usize,
    flow_ids_dropped: usize,
    callers_in_dropped: usize,
    callees_out_dropped: usize,
}

fn tree_analysis_incomplete_reasons(roots: &[TreeNode], summary: &TreeSummary) -> Vec<String> {
    let mut reasons = Vec::new();
    if summary.total_files_scanned > summary.total_files {
        reasons.push(format!(
            "tree-files-truncated:rendered_files={},scanned_files={}",
            summary.total_files, summary.total_files_scanned
        ));
    }

    let mut totals = TreeTruncationTotals::default();
    collect_tree_truncation_totals(roots, &mut totals);
    if totals.children_dropped > 0 {
        reasons.push(format!(
            "tree-children-truncated:children_dropped={}",
            totals.children_dropped
        ));
    }
    if totals.finding_ids_dropped > 0 {
        reasons.push(format!(
            "tree-finding-ids-truncated:finding_ids_dropped={}",
            totals.finding_ids_dropped
        ));
    }
    if totals.flow_ids_dropped > 0 {
        reasons.push(format!(
            "tree-flow-ids-truncated:flow_ids_dropped={}",
            totals.flow_ids_dropped
        ));
    }
    if totals.callers_in_dropped > 0 || totals.callees_out_dropped > 0 {
        reasons.push(format!(
            "tree-cross-file-edges-truncated:callers_in_dropped={},callees_out_dropped={}",
            totals.callers_in_dropped, totals.callees_out_dropped
        ));
    }
    reasons
}

fn collect_tree_truncation_totals(nodes: &[TreeNode], totals: &mut TreeTruncationTotals) {
    for node in nodes {
        totals.children_dropped += node.truncated.children_dropped;
        totals.finding_ids_dropped += node.truncated.finding_ids_dropped;
        totals.flow_ids_dropped += node.truncated.flow_ids_dropped;
        totals.callers_in_dropped += node.truncated.callers_in_dropped;
        totals.callees_out_dropped += node.truncated.callees_out_dropped;
        collect_tree_truncation_totals(&node.children, totals);
    }
}

fn finding_analysis_incomplete_reasons(finding: &Finding) -> Vec<String> {
    if finding.analysis_complete {
        return Vec::new();
    }
    if finding.analysis_incomplete_reasons.is_empty() {
        return vec![format!("finding:{}:analysis-incomplete", finding.finding_id)];
    }
    finding
        .analysis_incomplete_reasons
        .iter()
        .map(|reason| format!("finding:{}:{reason}", finding.finding_id))
        .collect()
}

#[derive(Default)]
struct CrossEdgeIndex {
    into_callers: AHashMap<String, Vec<CrossEdge>>,
    out_callees: AHashMap<String, Vec<CrossEdge>>,
}

fn build_cross_edges(graph: &bonsai_callgraph::ResolvedCallGraph, ws: &Workspace) -> CrossEdgeIndex {
    let mut index = CrossEdgeIndex::default();
    // Cross-file navigation needs symbol linkage, not retained function
    // bodies. Keep this phase on the compact compiler linkage product so an
    // SDK tree never materializes the whole-workspace body index.
    let global = ws.compiler_header_index();
    for edge in graph
        .inner()
        .edges
        .iter()
        .filter(|edge| edge.precision.is_semantic())
    {
        let caller_file = global
            .declaring_file(SymbolId::new(edge.from.raw()))
            .and_then(|fid| ws.vfs().path(fid).ok().map(|p| p.display().to_string()))
            .map(|path| workspace_relative_path(ws, &path));
        let callee_file = global
            .declaring_file(SymbolId::new(edge.to.raw()))
            .and_then(|fid| ws.vfs().path(fid).ok().map(|p| p.display().to_string()))
            .map(|path| workspace_relative_path(ws, &path));
        let (Some(caller_path), Some(callee_path)) = (caller_file, callee_file) else {
            continue;
        };
        if caller_path == callee_path {
            continue;
        }
        let caller_loc = func_to_locator(edge.from, ws);
        let callee_loc = func_to_locator(edge.to, ws);
        let call_site = Locator {
            file: caller_loc.file.clone(),
            line: caller_loc.line,
            column: caller_loc.column,
            ..Locator::default()
        };
        let cross = CrossEdge {
            caller: caller_loc,
            callee: callee_loc,
            call_site,
            edge_id: None,
            flow_id: None,
            finding_id: None,
            precision: edge.precision,
            edge_kind: edge.kind,
            external: None,
        };
        index
            .into_callers
            .entry(callee_path.clone())
            .or_default()
            .push(cross.clone());
        index.out_callees.entry(caller_path).or_default().push(cross);
    }
    for v in index.into_callers.values_mut() {
        let v: &mut Vec<CrossEdge> = v;
        v.sort_by_key(|edge| std::cmp::Reverse(precision_rank(edge.precision)));
    }
    for v in index.out_callees.values_mut() {
        let v: &mut Vec<CrossEdge> = v;
        v.sort_by_key(|edge| std::cmp::Reverse(precision_rank(edge.precision)));
    }
    index
}

fn precision_rank(p: Precision) -> u8 {
    match p {
        Precision::Exact => 4,
        Precision::Narrowed => 3,
        Precision::OverApproximate => 2,
        Precision::Unknown => 1,
    }
}

fn func_to_locator(func: FuncId, ws: &Workspace) -> Locator {
    let global = ws.compiler_header_index();
    let symbol = SymbolId::new(func.raw());
    let Some(decl) = global.decl_of(symbol) else {
        return Locator::external(format!("FuncId({})", func.raw()));
    };
    let mut locator = Locator::from_span(decl.span, ws);
    locator.file = workspace_relative_path(ws, &locator.file);
    locator
}

fn build_most_severe_flow(f: &Finding, ws: &Workspace) -> MostSevereFlowSummary {
    MostSevereFlowSummary {
        flow_id: f
            .representative_flow_id
            .clone()
            .unwrap_or_else(|| "F:0000000000000000".to_string()),
        finding_id: Some(f.finding_id.clone()),
        severity: f.severity.unwrap_or(Severity::Info),
        enters_at: match_to_locator(&f.source, ws),
        exits_at: match_to_locator(&f.sink, ws),
        chain_display: f.chain_display.clone(),
        extends_beyond_workspace: false,
    }
}

fn match_to_locator(m: &FindingMatch, ws: &Workspace) -> Locator {
    Locator {
        file: workspace_relative_path(ws, &m.file),
        line: m.line,
        column: m.column,
        decl: m.enclosing_fn.clone(),
        ..Locator::default()
    }
}

fn compute_render_priority(sev: Option<Severity>, total: usize) -> u8 {
    let sev_score: u32 = match sev {
        Some(Severity::Critical) => 200,
        Some(Severity::High) => 150,
        Some(Severity::Medium) => 100,
        Some(Severity::Low) => 50,
        Some(Severity::Info) => 25,
        None => 0,
    };
    let scaled = sev_score.saturating_add(u32::try_from(total.min(50)).unwrap_or(0));
    scaled.min(255) as u8
}

#[derive(Default)]
struct DirBuilder {
    files: Vec<TreeNode>,
    dirs: AHashMap<String, DirBuilder>,
}

impl DirBuilder {
    fn insert(&mut self, segments: &[&str], file_node: TreeNode) {
        if segments.len() <= 1 {
            self.files.push(file_node);
            return;
        }
        let head = segments[0].to_string();
        self.dirs
            .entry(head)
            .or_default()
            .insert(&segments[1..], file_node);
    }

    fn finalize(
        self,
        path_prefix: &str,
        depth: usize,
        max_depth: usize,
        limit: usize,
        summary: &mut TreeSummary,
    ) -> Vec<TreeNode> {
        if depth > max_depth {
            return Vec::new();
        }
        let mut out: Vec<TreeNode> = Vec::new();
        let mut dirs: Vec<(String, DirBuilder)> = self.dirs.into_iter().collect();
        dirs.sort_by(|a, b| a.0.cmp(&b.0));
        for (name, builder) in dirs {
            let next_path = if path_prefix.is_empty() {
                name.clone()
            } else {
                format!("{path_prefix}/{name}")
            };
            let children = builder.finalize(&next_path, depth + 1, max_depth, limit, summary);
            let mut hist = SeverityHistogram::default();
            for c in &children {
                hist.merge(&c.finding_severity_counts);
            }
            summary.total_dirs += 1;
            let dir_locator = Locator {
                file: next_path.clone(),
                ..Locator::default()
            };
            let max_sev = hist.max();
            let dir_priority = compute_render_priority(max_sev, hist.total());
            out.push(TreeNode {
                kind: NodeKind::Dir,
                name,
                locator: dir_locator,
                depth,
                finding_ids: Vec::new(),
                flow_ids: Vec::new(),
                max_severity: max_sev,
                finding_severity_counts: hist,
                cross_file_callers_in: Vec::new(),
                cross_file_callees_out: Vec::new(),
                most_severe_flow: None,
                indexed: IndexedStatus::Complete,
                render_priority: dir_priority,
                children,
                truncated: TreeTruncation::default(),
            });
        }
        let mut files = self.files;
        files.sort_by(|a, b| a.name.cmp(&b.name));
        let raw_files = files.len();
        let dropped = raw_files.saturating_sub(limit);
        files.truncate(limit);
        for f in &files {
            summary.total_files += 1;
            summary.severity_counts.merge(&f.finding_severity_counts);
            summary.total_findings += f.finding_severity_counts.total();
            match f.indexed {
                IndexedStatus::Complete => summary.indexed_complete += 1,
                IndexedStatus::Stale => summary.indexed_stale += 1,
                IndexedStatus::Missing => summary.indexed_missing += 1,
            }
        }
        out.extend(files);
        if dropped > 0 {
            if let Some(last) = out.last_mut() {
                last.truncated.children_dropped = dropped;
            }
        }
        out
    }
}

#[cfg(test)]
#[path = "tree_tests.rs"]
mod tests;