heal-cli 0.3.1

Hook-driven Evaluation & Autonomous Loop — code-health harness CLI for AI coding agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
//! LCOM (Lack of Cohesion of Methods) approximation.
//!
//! Counts the number of disjoint method clusters per class — Tornhill's
//! "internal split" companion to Change Coupling. Two methods belong
//! to the same cluster if they touch a shared field or one calls the
//! other; LCOM is the resulting connected-component count. A class
//! with `cluster_count == 1` is cohesive; `>= 2` means the class is
//! mechanically separable into smaller pieces.
//!
//! ## Approximation
//!
//! HEAL's v0.2 backend is `tree-sitter-approx`: pure syntactic walk,
//! no type resolution. That means:
//! - `this.foo` / `self.foo` references count, but inherited fields
//!   from a base class don't (we never see the base).
//! - Dynamic property access (`this[name]`, computed keys) is invisible.
//! - Methods that share state via a *helper function* outside the class
//!   look unrelated. Static methods on the same class look unrelated to
//!   instance methods.
//!
//! These limitations bias toward over-reporting (false positives — a
//! class that's actually cohesive looks split). The renderer is meant
//! to surface candidates for review, not to make automatic decisions.
//! `backend = "lsp"` (v0.5+) replaces this with a typed implementation.
//!
//! ## Scope
//!
//! - **TypeScript**: `class_declaration` (and TSX `class_declaration`).
//!   Methods are `method_definition` nodes inside the class body.
//!   Field references: `member_expression` whose object is `this`.
//! - **Rust**: `impl_item` (inherent impl + trait impl, both treated
//!   the same). Methods are `function_item` inside the impl body.
//!   Field references: `field_expression` whose value is `self`.
//!
//! Module-scope LCOM (Rust file-level free functions, TS named-export
//! groups) is deferred — the grouping rules are project-specific and
//! adding them now would produce more noise than signal.

use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use tree_sitter::{Node, QueryCursor, StreamingIterator};

use crate::core::config::Config;
use crate::core::finding::{Finding, IntoFindings, Location};
use crate::core::severity::Severity;
use crate::feature::{decorate, Feature, FeatureKind, FeatureMeta, HotspotIndex};
use crate::observer::complexity::{parse, ParsedFile};
use crate::observer::lang::Language;
use crate::observer::walk::{walk_supported_files_under, ExcludeMatcher};
use crate::observer::{impl_workspace_builder, ObservationMeta, Observer};
use crate::observers::ObserverReports;

impl_workspace_builder!(LcomObserver);

#[derive(Debug, Clone, Default)]
pub struct LcomObserver {
    pub enabled: bool,
    pub excluded: Vec<String>,
    pub min_cluster_count: u32,
    /// Optional workspace sub-path; see `ComplexityObserver::workspace`.
    pub workspace: Option<PathBuf>,
}

impl LcomObserver {
    #[must_use]
    pub fn from_config(cfg: &Config) -> Self {
        Self {
            enabled: cfg.metrics.lcom.enabled,
            excluded: cfg.exclude_lines(),
            min_cluster_count: cfg.metrics.lcom.min_cluster_count,
            workspace: None,
        }
    }

    #[must_use]
    pub fn scan(&self, root: &Path) -> LcomReport {
        let mut report = LcomReport {
            min_cluster_count: self.min_cluster_count,
            ..LcomReport::default()
        };
        if !self.enabled {
            return report;
        }
        let matcher = ExcludeMatcher::compile(root, &self.excluded)
            .expect("exclude patterns validated at config load");
        for path in walk_supported_files_under(root, &matcher, self.workspace.as_deref()) {
            let Some(lang) = Language::from_path(&path) else {
                continue;
            };
            if !lang.supports_lcom() {
                continue;
            }
            let Ok(source) = std::fs::read_to_string(&path) else {
                continue;
            };
            let Ok(parsed) = parse(source, lang) else {
                continue;
            };
            let rel = path
                .strip_prefix(root)
                .map_or_else(|_| path.clone(), Path::to_path_buf);
            for class in classes_in(&parsed, &rel) {
                report.classes.push(class);
            }
        }
        report.classes.sort_by(|a, b| {
            b.cluster_count
                .cmp(&a.cluster_count)
                .then_with(|| a.file.cmp(&b.file))
        });

        let totals = LcomTotals {
            classes_scanned: report.classes.len(),
            classes_with_lcom: report
                .classes
                .iter()
                .filter(|c| c.cluster_count >= self.min_cluster_count)
                .count(),
            max_cluster_count: report
                .classes
                .iter()
                .map(|c| c.cluster_count)
                .max()
                .unwrap_or(0),
        };
        report.totals = totals;
        report
    }
}

impl Observer for LcomObserver {
    type Output = LcomReport;

    fn meta(&self) -> ObservationMeta {
        ObservationMeta {
            name: "lcom",
            version: 1,
        }
    }

    fn observe(&self, project_root: &Path) -> anyhow::Result<Self::Output> {
        Ok(self.scan(project_root))
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct LcomReport {
    pub classes: Vec<ClassLcom>,
    pub totals: LcomTotals,
    pub min_cluster_count: u32,
}

impl LcomReport {
    /// Top-N classes by `cluster_count` desc. The underlying Vec is
    /// already sorted at scan time; this just truncates a clone.
    #[must_use]
    pub fn worst_n(&self, n: usize) -> Vec<ClassLcom> {
        self.classes.iter().take(n).cloned().collect()
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct LcomTotals {
    pub classes_scanned: usize,
    pub classes_with_lcom: usize,
    pub max_cluster_count: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ClassLcom {
    pub file: PathBuf,
    pub language: String,
    pub class_name: String,
    pub start_line: u32,
    pub end_line: u32,
    pub method_count: u32,
    pub cluster_count: u32,
    /// Each cluster is a list of method names that share state /
    /// call-graph reachability. Sorted within each cluster + across
    /// clusters for deterministic output.
    pub clusters: Vec<MethodCluster>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct MethodCluster {
    pub methods: Vec<String>,
}

impl IntoFindings for LcomReport {
    fn into_findings(&self) -> Vec<Finding> {
        self.classes
            .iter()
            .filter(|c| c.cluster_count >= self.min_cluster_count.max(1))
            .map(|c| {
                let summary = format!(
                    "LCOM={} clusters across {} methods in {} ({})",
                    c.cluster_count, c.method_count, c.class_name, c.language,
                );
                let location = Location {
                    file: c.file.clone(),
                    line: Some(c.start_line),
                    symbol: Some(c.class_name.clone()),
                };
                let seed = format!("lcom:{}:{}", c.cluster_count, c.method_count);
                Finding::new("lcom", location, summary, &seed)
            })
            .collect()
    }
}

/// Walk every class scope in the parsed file, computing per-class LCOM.
fn classes_in(parsed: &ParsedFile, file: &Path) -> Vec<ClassLcom> {
    let q = parsed.lang.lcom_query();
    let mut cursor = QueryCursor::new();
    let mut matches = cursor.matches(&q.query, parsed.tree.root_node(), parsed.source.as_bytes());

    let mut out: Vec<ClassLcom> = Vec::new();
    while let Some(m) = matches.next() {
        for cap in m
            .captures
            .iter()
            .filter(|c| c.index == q.captures.class_scope)
        {
            if let Some(class) = analyze_class(cap.node, parsed, file) {
                out.push(class);
            }
        }
    }
    out
}

fn analyze_class(class_node: Node<'_>, parsed: &ParsedFile, file: &Path) -> Option<ClassLcom> {
    let methods = collect_methods(class_node, parsed.lang, parsed.source.as_bytes());
    if methods.is_empty() {
        return None;
    }
    let class_name = class_name_for(class_node, parsed.source.as_bytes(), parsed.lang);

    // field-name → distinct method indices that touch it. The
    // `BTreeSet` dedupes within a method (a method touching `this.foo`
    // ten times still produces one entry) so the union-find pass
    // doesn't waste work re-unioning the same pair.
    let mut field_to_methods: HashMap<String, BTreeSet<usize>> = HashMap::new();
    let mut method_calls: Vec<BTreeSet<usize>> = vec![BTreeSet::new(); methods.len()];
    let method_name_to_index: HashMap<&str, usize> = methods
        .iter()
        .enumerate()
        .map(|(i, m)| (m.name.as_str(), i))
        .collect();

    for (i, method) in methods.iter().enumerate() {
        let refs = collect_self_refs(method.body, parsed.source.as_bytes(), parsed.lang);
        for field in refs.fields {
            field_to_methods.entry(field).or_default().insert(i);
        }
        for callee in refs.method_calls {
            if let Some(&j) = method_name_to_index.get(callee.as_str()) {
                if j != i {
                    method_calls[i].insert(j);
                }
            }
        }
    }

    let mut uf = UnionFind::new(methods.len());
    for members in field_to_methods.values() {
        let mut iter = members.iter();
        if let Some(&first) = iter.next() {
            for &m in iter {
                uf.union(first, m);
            }
        }
    }
    for (i, callees) in method_calls.iter().enumerate() {
        for &j in callees {
            uf.union(i, j);
        }
    }

    let mut clusters: BTreeMap<usize, BTreeSet<String>> = BTreeMap::new();
    for (i, m) in methods.iter().enumerate() {
        clusters
            .entry(uf.find(i))
            .or_default()
            .insert(m.name.clone());
    }
    let mut clusters_vec: Vec<MethodCluster> = clusters
        .into_values()
        .map(|set| MethodCluster {
            methods: set.into_iter().collect(),
        })
        .collect();
    clusters_vec.sort_by(|a, b| {
        b.methods
            .len()
            .cmp(&a.methods.len())
            .then_with(|| a.methods.cmp(&b.methods))
    });

    Some(ClassLcom {
        file: file.to_path_buf(),
        language: parsed.lang.name().to_owned(),
        class_name,
        start_line: u32::try_from(class_node.start_position().row + 1).unwrap_or(u32::MAX),
        end_line: u32::try_from(class_node.end_position().row + 1).unwrap_or(u32::MAX),
        method_count: u32::try_from(methods.len()).unwrap_or(u32::MAX),
        cluster_count: u32::try_from(clusters_vec.len()).unwrap_or(u32::MAX),
        clusters: clusters_vec,
    })
}

struct MethodEntry<'a> {
    name: String,
    body: Node<'a>,
}

/// Gather every method definition inside a class body, in source order.
/// Anonymous / static-init / constructor blocks without a name are
/// skipped (they participate in cohesion but are awkward to label).
fn collect_methods<'a>(
    class_node: Node<'a>,
    lang: Language,
    source: &[u8],
) -> Vec<MethodEntry<'a>> {
    let Some(body) = class_node.child_by_field_name("body") else {
        return Vec::new();
    };
    let mut methods = Vec::new();
    let mut cursor = body.walk();
    for child in body.named_children(&mut cursor) {
        if !is_method_kind(child, lang) {
            continue;
        }
        let Some(name_node) = child.child_by_field_name("name") else {
            continue;
        };
        let Ok(name) = name_node.utf8_text(source) else {
            continue;
        };
        let Some(body_node) = child.child_by_field_name("body") else {
            continue;
        };
        methods.push(MethodEntry {
            name: name.to_owned(),
            body: body_node,
        });
    }
    methods
}

// `node` is unused when only no-LCOM languages (Go/Scala) are enabled.
#[cfg_attr(
    not(any(
        feature = "lang-ts",
        feature = "lang-js",
        feature = "lang-py",
        feature = "lang-rust"
    )),
    allow(unused_variables)
)]
fn is_method_kind(node: Node<'_>, lang: Language) -> bool {
    match lang {
        #[cfg(feature = "lang-ts")]
        Language::TypeScript | Language::Tsx => {
            matches!(node.kind(), "method_definition" | "method_signature")
        }
        #[cfg(feature = "lang-js")]
        Language::JavaScript | Language::Jsx => node.kind() == "method_definition",
        #[cfg(feature = "lang-py")]
        Language::Python => node.kind() == "function_definition",
        // Go has no class scope; methods attach to types via receivers
        // and live at module scope. Receiver-grouped LCOM lands in
        // v0.3+. `Language::supports_lcom` short-circuits before this
        // is reached, but the variant must still be matched.
        #[cfg(feature = "lang-go")]
        Language::Go => false,
        // Scala spans class / trait / object / case-class / given
        // constructs and uses bare-name field access more than
        // `this.field`. A class-aware LCOM that handles this richness
        // needs the LSP backend (v0.5+); skipped via `supports_lcom`.
        #[cfg(feature = "lang-scala")]
        Language::Scala => false,
        #[cfg(feature = "lang-rust")]
        Language::Rust => node.kind() == "function_item",
    }
}

fn class_name_for(class_node: Node<'_>, source: &[u8], lang: Language) -> String {
    // Rust trait impls have both `trait` and `type` fields; we deliberately
    // pick `type` so `impl Foo for Bar` and `impl Bar` collapse to `Bar`.
    let lookup = match lang {
        #[cfg(feature = "lang-ts")]
        Language::TypeScript | Language::Tsx => "name",
        #[cfg(feature = "lang-js")]
        Language::JavaScript | Language::Jsx => "name",
        #[cfg(feature = "lang-py")]
        Language::Python => "name",
        // Go's LCOM is a no-op (see `is_method_kind`); the field name
        // here doesn't matter.
        #[cfg(feature = "lang-go")]
        Language::Go => "name",
        #[cfg(feature = "lang-scala")]
        Language::Scala => "name",
        #[cfg(feature = "lang-rust")]
        Language::Rust => "type",
    };
    if let Some(n) = class_node.child_by_field_name(lookup) {
        if let Ok(text) = n.utf8_text(source) {
            return text.to_owned();
        }
    }
    format!("<class@{}>", class_node.start_position().row + 1)
}

#[derive(Default)]
struct SelfRefs {
    fields: Vec<String>,
    method_calls: Vec<String>,
}

/// Per-language tree-sitter shape of "receiver-bound member access".
/// One row replaces an entire `visit_*` function — adding a third
/// language is a table entry, not new code.
#[derive(Clone, Copy)]
struct SelfRefShape {
    /// Outer node kind that names the access (TS: `member_expression`,
    /// Rust: `field_expression`, Python: `attribute`).
    access_kind: &'static str,
    /// Field name on the access for the receiver.
    receiver_field: &'static str,
    /// Predicate identifying the receiver. TS / JS / Rust filter by
    /// node kind alone (`this` / `self` are dedicated keyword nodes).
    /// Python uses an `identifier` whose source text is `"self"`, so
    /// the predicate is text-based.
    is_receiver: fn(Node<'_>, &[u8]) -> bool,
    /// Field name on the access for the property.
    property_field: &'static str,
    /// Kind of the parent call expression. Most grammars use
    /// `call_expression`; Python's `call` node is the outlier.
    call_kind: &'static str,
}

#[cfg(any(feature = "lang-ts", feature = "lang-js"))]
fn ts_js_is_receiver(node: Node<'_>, _: &[u8]) -> bool {
    node.kind() == "this"
}
#[cfg(feature = "lang-rust")]
fn rust_is_receiver(node: Node<'_>, _: &[u8]) -> bool {
    node.kind() == "self"
}
#[cfg(feature = "lang-py")]
fn py_is_receiver(node: Node<'_>, source: &[u8]) -> bool {
    node.kind() == "identifier" && node.utf8_text(source).is_ok_and(|t| t == "self")
}

#[cfg(any(feature = "lang-ts", feature = "lang-js"))]
const SELF_REF_TS_JS: SelfRefShape = SelfRefShape {
    access_kind: "member_expression",
    receiver_field: "object",
    is_receiver: ts_js_is_receiver,
    property_field: "property",
    call_kind: "call_expression",
};
#[cfg(feature = "lang-rust")]
const SELF_REF_RUST: SelfRefShape = SelfRefShape {
    access_kind: "field_expression",
    receiver_field: "value",
    is_receiver: rust_is_receiver,
    property_field: "field",
    call_kind: "call_expression",
};
#[cfg(feature = "lang-py")]
const SELF_REF_PY: SelfRefShape = SelfRefShape {
    access_kind: "attribute",
    receiver_field: "object",
    is_receiver: py_is_receiver,
    property_field: "attribute",
    call_kind: "call",
};

// Returns `None` for languages whose LCOM is a no-op (Go, Scala);
// when neither feature is enabled the function trivially returns
// `Some`, but we keep the Option so the no-op case stays expressible.
#[cfg_attr(
    not(any(feature = "lang-go", feature = "lang-scala")),
    allow(clippy::unnecessary_wraps)
)]
fn self_ref_shape(lang: Language) -> Option<SelfRefShape> {
    match lang {
        #[cfg(feature = "lang-ts")]
        Language::TypeScript | Language::Tsx => Some(SELF_REF_TS_JS),
        #[cfg(feature = "lang-js")]
        Language::JavaScript | Language::Jsx => Some(SELF_REF_TS_JS),
        #[cfg(feature = "lang-py")]
        Language::Python => Some(SELF_REF_PY),
        // Go has no class scope and Scala's class story needs the LSP
        // backend; both languages opt out of LCOM via
        // `Language::supports_lcom`. `is_method_kind` also returns
        // false, so this branch is only reached if a future caller
        // forgets the supports_lcom gate.
        #[cfg(feature = "lang-go")]
        Language::Go => None,
        #[cfg(feature = "lang-scala")]
        Language::Scala => None,
        #[cfg(feature = "lang-rust")]
        Language::Rust => Some(SELF_REF_RUST),
    }
}

fn collect_self_refs(body: Node<'_>, source: &[u8], lang: Language) -> SelfRefs {
    let mut refs = SelfRefs::default();
    let Some(shape) = self_ref_shape(lang) else {
        return refs;
    };
    let mut cursor = body.walk();
    let mut stack = vec![body];
    while let Some(node) = stack.pop() {
        visit_self_ref(node, shape, source, &mut refs);
        for child in node.named_children(&mut cursor) {
            stack.push(child);
        }
    }
    refs
}

fn visit_self_ref(node: Node<'_>, shape: SelfRefShape, source: &[u8], refs: &mut SelfRefs) {
    if node.kind() != shape.access_kind {
        return;
    }
    let Some(receiver) = node.child_by_field_name(shape.receiver_field) else {
        return;
    };
    if !(shape.is_receiver)(receiver, source) {
        return;
    }
    let Some(prop) = node.child_by_field_name(shape.property_field) else {
        return;
    };
    let Ok(name) = prop.utf8_text(source) else {
        return;
    };
    let name = name.to_owned();
    if is_call_target(node, shape) {
        refs.method_calls.push(name);
    } else {
        refs.fields.push(name);
    }
}

/// `node` is the receiver expression of a member/field access.
/// Returns true when the parent is a call node whose `function` field
/// is exactly `node`. The parent kind is grammar-specific
/// (`call_expression` for TS / JS / Rust, `call` for Python).
fn is_call_target(node: Node<'_>, shape: SelfRefShape) -> bool {
    let Some(parent) = node.parent() else {
        return false;
    };
    if parent.kind() != shape.call_kind {
        return false;
    }
    let function = parent.child_by_field_name("function");
    matches!(function, Some(f) if f == node)
}

/// Disjoint-set union with path compression. Methods start in their
/// own singleton; each shared-field or call edge merges two sets. The
/// final cluster count is the number of unique roots.
struct UnionFind {
    parent: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> Self {
        Self {
            parent: (0..n).collect(),
        }
    }

    fn find(&mut self, mut i: usize) -> usize {
        while self.parent[i] != i {
            self.parent[i] = self.parent[self.parent[i]];
            i = self.parent[i];
        }
        i
    }

    fn union(&mut self, a: usize, b: usize) {
        let ra = self.find(a);
        let rb = self.find(b);
        if ra != rb {
            self.parent[ra] = rb;
        }
    }
}

pub struct LcomFeature;

impl Feature for LcomFeature {
    fn meta(&self) -> FeatureMeta {
        FeatureMeta {
            name: "lcom",
            version: 1,
            kind: FeatureKind::Observer,
        }
    }
    fn enabled(&self, cfg: &Config) -> bool {
        cfg.metrics.lcom.enabled
    }
    fn lower(
        &self,
        reports: &ObserverReports,
        cfg: &Config,
        cal: &crate::core::calibration::Calibration,
        hotspot: &HotspotIndex,
    ) -> Vec<Finding> {
        let Some(lc) = reports.lcom.as_ref() else {
            return Vec::new();
        };
        let workspaces = cfg.project.workspaces.as_slice();
        let kept: Vec<_> = lc
            .classes
            .iter()
            .filter(|c| c.cluster_count >= lc.min_cluster_count.max(1))
            .collect();
        let mut out = Vec::with_capacity(kept.len());
        for (class, finding) in kept.iter().zip(lc.into_findings()) {
            let cal_lcom = cal.metrics_for_file(&class.file, workspaces).lcom.as_ref();
            let severity =
                cal_lcom.map_or(Severity::Ok, |c| c.classify(f64::from(class.cluster_count)));
            out.push(decorate(finding, severity, hotspot));
        }
        out
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(feature = "lang-ts")]
    fn run_ts(source: &str) -> Vec<ClassLcom> {
        let parsed = parse(source.to_owned(), Language::TypeScript).unwrap();
        classes_in(&parsed, Path::new("test.ts"))
    }

    #[cfg(feature = "lang-js")]
    fn run_js(source: &str) -> Vec<ClassLcom> {
        let parsed = parse(source.to_owned(), Language::JavaScript).unwrap();
        classes_in(&parsed, Path::new("test.js"))
    }

    #[cfg(feature = "lang-py")]
    fn run_py(source: &str) -> Vec<ClassLcom> {
        let parsed = parse(source.to_owned(), Language::Python).unwrap();
        classes_in(&parsed, Path::new("test.py"))
    }

    #[cfg(feature = "lang-go")]
    fn run_go(source: &str) -> Vec<ClassLcom> {
        let parsed = parse(source.to_owned(), Language::Go).unwrap();
        classes_in(&parsed, Path::new("test.go"))
    }

    #[cfg(feature = "lang-scala")]
    fn run_scala(source: &str) -> Vec<ClassLcom> {
        let parsed = parse(source.to_owned(), Language::Scala).unwrap();
        classes_in(&parsed, Path::new("test.scala"))
    }

    #[cfg(feature = "lang-rust")]
    fn run_rust(source: &str) -> Vec<ClassLcom> {
        let parsed = parse(source.to_owned(), Language::Rust).unwrap();
        classes_in(&parsed, Path::new("test.rs"))
    }

    #[cfg(feature = "lang-ts")]
    #[test]
    fn ts_cohesive_class_has_one_cluster() {
        let src = r"
            class Counter {
                private value = 0;
                inc() { this.value += 1; }
                dec() { this.value -= 1; }
                get() { return this.value; }
            }
        ";
        let classes = run_ts(src);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].class_name, "Counter");
        assert_eq!(classes[0].method_count, 3);
        assert_eq!(classes[0].cluster_count, 1, "all methods touch `value`");
    }

    #[cfg(feature = "lang-ts")]
    #[test]
    fn ts_split_class_has_multiple_clusters() {
        // Two unrelated responsibility groups: counter (a/b touch `count`)
        // and logger (c/d touch `log`). They don't share fields and don't
        // call each other, so LCOM = 2.
        let src = r"
            class Mixed {
                private count = 0;
                private log: string[] = [];
                inc() { this.count += 1; }
                value() { return this.count; }
                push(msg: string) { this.log.push(msg); }
                tail() { return this.log[this.log.length - 1]; }
            }
        ";
        let classes = run_ts(src);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].cluster_count, 2);
    }

    #[cfg(feature = "lang-ts")]
    #[test]
    fn ts_method_call_merges_clusters() {
        // `tail()` reaches into both responsibility groups — touches
        // `log` directly *and* calls `value()` — so the union-find
        // merges {inc, value} with {push, tail} into one cluster.
        let src = r"
            class Bridged {
                private count = 0;
                private log: string[] = [];
                inc() { this.count += 1; }
                value() { return this.count; }
                push(msg: string) { this.log.push(msg); }
                tail() {
                    this.value();
                    return this.log[0];
                }
            }
        ";
        let classes = run_ts(src);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].cluster_count, 1);
    }

    #[cfg(feature = "lang-rust")]
    #[test]
    fn rust_cohesive_impl_has_one_cluster() {
        let src = r"
            struct Counter { value: i32 }
            impl Counter {
                fn inc(&mut self) { self.value += 1; }
                fn dec(&mut self) { self.value -= 1; }
                fn get(&self) -> i32 { self.value }
            }
        ";
        let classes = run_rust(src);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].class_name, "Counter");
        assert_eq!(classes[0].cluster_count, 1);
    }

    #[cfg(feature = "lang-rust")]
    #[test]
    fn rust_split_impl_has_multiple_clusters() {
        let src = r"
            struct Mixed { count: i32, log: Vec<String> }
            impl Mixed {
                fn inc(&mut self) { self.count += 1; }
                fn value(&self) -> i32 { self.count }
                fn push(&mut self, m: String) { self.log.push(m); }
                fn tail(&self) -> Option<&String> { self.log.last() }
            }
        ";
        let classes = run_rust(src);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].cluster_count, 2);
    }

    #[cfg(feature = "lang-rust")]
    #[test]
    fn rust_method_call_merges_clusters() {
        // `tail` touches `log` *and* calls `value()` — the call-graph
        // edge plus the shared field stitch the two clusters into one.
        let src = r"
            struct Bridged { count: i32, log: Vec<String> }
            impl Bridged {
                fn inc(&mut self) { self.count += 1; }
                fn value(&self) -> i32 { self.count }
                fn push(&mut self, m: String) { self.log.push(m); }
                fn tail(&self) -> i32 {
                    let _ = self.log.last();
                    self.value()
                }
            }
        ";
        let classes = run_rust(src);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].cluster_count, 1);
    }

    #[cfg(feature = "lang-ts")]
    #[test]
    fn empty_class_is_skipped() {
        let src = "class Empty {}";
        let classes = run_ts(src);
        assert!(classes.is_empty());
    }

    #[cfg(feature = "lang-js")]
    #[test]
    fn js_cohesive_class_has_one_cluster() {
        let src = r"
            class Counter {
                constructor() { this.value = 0; }
                inc() { this.value += 1; }
                dec() { this.value -= 1; }
                get() { return this.value; }
            }
        ";
        let classes = run_js(src);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].class_name, "Counter");
        assert_eq!(classes[0].cluster_count, 1);
    }

    #[cfg(feature = "lang-js")]
    #[test]
    fn js_split_class_has_multiple_clusters() {
        let src = r"
            class Mixed {
                inc() { this.count = (this.count || 0) + 1; }
                value() { return this.count; }
                push(msg) { (this.log = this.log || []).push(msg); }
                tail() { return this.log && this.log[0]; }
            }
        ";
        let classes = run_js(src);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].cluster_count, 2);
    }

    #[cfg(feature = "lang-js")]
    #[test]
    fn js_method_call_merges_clusters() {
        // `tail()` bridges {push, tail} (log) ∪ {inc, value} (count).
        let src = r"
            class Bridged {
                inc() { this.count = (this.count || 0) + 1; }
                value() { return this.count; }
                push(msg) { (this.log = this.log || []).push(msg); }
                tail() {
                    this.value();
                    return this.log && this.log[0];
                }
            }
        ";
        let classes = run_js(src);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].cluster_count, 1);
    }

    #[cfg(feature = "lang-py")]
    #[test]
    fn py_cohesive_class_has_one_cluster() {
        let src = r"
class Counter:
    def __init__(self):
        self.value = 0

    def inc(self):
        self.value += 1

    def get(self):
        return self.value
";
        let classes = run_py(src);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].class_name, "Counter");
        assert_eq!(classes[0].cluster_count, 1);
    }

    #[cfg(feature = "lang-py")]
    #[test]
    fn py_split_class_has_multiple_clusters() {
        let src = r"
class Mixed:
    def inc(self):
        self.count = (self.count or 0) + 1

    def value(self):
        return self.count

    def push(self, msg):
        self.log.append(msg)

    def tail(self):
        return self.log[-1]
";
        let classes = run_py(src);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].cluster_count, 2);
    }

    #[cfg(feature = "lang-go")]
    #[test]
    fn go_lcom_is_a_noop_in_v0_2() {
        // Go's class-aware LCOM is deferred — `is_method_kind` returns
        // false for every node, so even a file with multiple methods
        // produces no Findings.
        let src = r"
package x

type Counter struct { value int }

func (c *Counter) Inc() { c.value += 1 }
func (c *Counter) Get() int { return c.value }
";
        assert!(run_go(src).is_empty());
    }

    #[cfg(feature = "lang-scala")]
    #[test]
    fn scala_lcom_is_a_noop_in_v0_2() {
        // Same shape as Go — Scala's class story is too rich for the
        // tree-sitter-approx backend; deferred to v0.3+ LSP.
        let src = r"
class Counter {
  private var value = 0
  def inc(): Unit = value += 1
  def get: Int = value
}
";
        assert!(run_scala(src).is_empty());
    }

    #[cfg(feature = "lang-py")]
    #[test]
    fn py_method_call_merges_clusters() {
        // `tail` touches `log` AND calls `value()` — the bridge.
        let src = r"
class Bridged:
    def inc(self):
        self.count = (self.count or 0) + 1

    def value(self):
        return self.count

    def push(self, msg):
        self.log.append(msg)

    def tail(self):
        self.value()
        return self.log[-1]
";
        let classes = run_py(src);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].cluster_count, 1);
    }

    #[cfg(feature = "lang-rust")]
    #[test]
    fn scan_strips_project_root_from_class_file() {
        // Regression: LCOM observer used to store absolute paths in
        // `ClassLcom.file` because `walk_supported_files_under` returns
        // absolute paths and `scan` forwarded them verbatim. Absolute
        // paths flow into `Finding.location.file` and `Finding::make_id`,
        // breaking deterministic id stability across machines and
        // teammates' `fixed.json` reconciliation. Every other observer
        // calls `path.strip_prefix(root)` after the walk; this pins
        // that contract for `LcomObserver::scan`.
        use std::fs;

        let tmp = tempfile::tempdir().unwrap();
        let src = "
struct Counter { a: u32, b: u32 }
impl Counter {
    fn use_a(&self) -> u32 { self.a }
    fn use_b(&self) -> u32 { self.b }
}
";
        fs::write(tmp.path().join("counter.rs"), src).unwrap();

        let observer = LcomObserver {
            enabled: true,
            min_cluster_count: 2,
            ..LcomObserver::default()
        };
        let report = observer.scan(tmp.path());

        assert!(
            !report.classes.is_empty(),
            "fixture must produce at least one class",
        );
        for class in &report.classes {
            assert!(
                class.file.is_relative(),
                "ClassLcom.file must be project-relative; got {:?}",
                class.file,
            );
        }
    }
}