loctree 0.8.16

Structural code intelligence for AI agents. Scan once, query everything.
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
//! AI and code analysis command handlers
//!
//! Handles: memex, crowd, twins, sniff, suppress

use super::super::super::command::{
    CrowdOptions, MemexOptions, SniffOptions, SuppressOptions, TagmapOptions, TwinsOptions,
};
use super::super::{DispatchResult, GlobalOptions, is_test_file, load_or_create_snapshot};
use super::deprecation::warn_deprecated;
use crate::progress::Spinner;
use crate::suppressions::{SuppressionType, Suppressions};

/// Handle the memex command - index analysis into AI memory
#[cfg(feature = "memex")]
pub fn handle_memex_command(opts: &MemexOptions, global: &GlobalOptions) -> DispatchResult {
    use crate::memex;

    // Run the async memex indexer using a blocking runtime
    let result = tokio::runtime::Runtime::new()
        .map_err(|e| format!("Failed to create async runtime: {}", e))
        .and_then(|rt| {
            rt.block_on(async { memex::run_memex(opts, global.json, global.verbose).await })
        });

    match result {
        Ok(indexed_count) => {
            if !global.quiet {
                if global.json {
                    let json = serde_json::json!({
                        "status": "success",
                        "indexed_documents": indexed_count,
                        "namespace": &opts.namespace,
                    });
                    println!("{}", serde_json::to_string_pretty(&json).unwrap());
                } else {
                    eprintln!(
                        "[loct][memex] Successfully indexed {} documents into namespace '{}'",
                        indexed_count, opts.namespace
                    );
                }
            }
            DispatchResult::Exit(0)
        }
        Err(e) => {
            eprintln!("[loct][memex][error] {}", e);
            DispatchResult::Exit(1)
        }
    }
}

/// Handle the memex command - stub when feature not enabled
#[cfg(not(feature = "memex"))]
pub fn handle_memex_command(_opts: &MemexOptions, _global: &GlobalOptions) -> DispatchResult {
    eprintln!(
        "[loct][memex][error] memex feature not enabled. Rebuild with: cargo build --features memex"
    );
    DispatchResult::Exit(1)
}

/// Handle the crowd command - detect functional crowds
pub fn handle_crowd_command(opts: &CrowdOptions, global: &GlobalOptions) -> DispatchResult {
    use crate::analyzer::crowd::{
        detect_all_crowds_with_edges, detect_crowd_with_edges, format_crowd, format_crowds_summary,
    };
    use std::path::Path;

    // Show spinner unless in quiet/json mode
    let spinner = if !global.quiet && !global.json {
        Some(Spinner::new("Detecting functional crowds..."))
    } else {
        None
    };

    // Load snapshot (auto-scan if missing)
    let root = opts
        .roots
        .first()
        .map(|p| p.as_path())
        .unwrap_or(Path::new("."));
    let snapshot = match load_or_create_snapshot(root, global) {
        Ok(s) => s,
        Err(e) => {
            if let Some(s) = spinner {
                s.finish_error(&format!("Failed to load snapshot: {}", e));
            } else {
                eprintln!("[loct][error] {}", e);
            }
            return DispatchResult::Exit(1);
        }
    };

    // Filter out test files unless --include-tests is specified
    let files: Vec<_> = if opts.include_tests {
        snapshot.files.clone()
    } else {
        snapshot
            .files
            .iter()
            .filter(|f| !is_test_file(&f.path))
            .cloned()
            .collect()
    };

    // Detect crowds (using edges for accurate transitive importer counting)
    let crowds = if let Some(ref pattern) = opts.pattern {
        // Single pattern mode
        vec![detect_crowd_with_edges(&files, pattern, &snapshot.edges)]
    } else {
        // Auto-detect mode
        let mut all_crowds = detect_all_crowds_with_edges(&files, &snapshot.edges);

        // Apply min_size filter
        if let Some(min_size) = opts.min_size {
            all_crowds.retain(|c| c.members.len() >= min_size);
        }

        // Apply limit
        if let Some(limit) = opts.limit {
            all_crowds.truncate(limit);
        }

        all_crowds
    };

    // Filter out empty crowds
    let crowds: Vec<_> = crowds
        .into_iter()
        .filter(|c| !c.members.is_empty())
        .collect();

    if crowds.is_empty() {
        if let Some(s) = spinner {
            if let Some(ref pattern) = opts.pattern {
                s.finish_warning(&format!("No files found matching pattern '{}'", pattern));
            } else {
                s.finish_warning("No crowds detected in codebase");
            }
        } else if !global.quiet {
            if let Some(ref pattern) = opts.pattern {
                eprintln!(
                    "[loct][crowd] No files found matching pattern '{}'",
                    pattern
                );
            } else {
                eprintln!("[loct][crowd] No crowds detected in codebase");
            }
        }
        return DispatchResult::Exit(0);
    }

    // Finish spinner with success message
    if let Some(s) = spinner {
        let total_members: usize = crowds.iter().map(|c| c.members.len()).sum();
        s.finish_success(&format!(
            "Found {} crowd(s) with {} total members",
            crowds.len(),
            total_members
        ));
    }

    // Output results
    if global.json {
        match serde_json::to_string_pretty(&crowds) {
            Ok(json) => println!("{}", json),
            Err(e) => {
                eprintln!("[loct][error] Failed to serialize crowds: {}", e);
                return DispatchResult::Exit(1);
            }
        }
    } else {
        // Human-readable output
        if crowds.len() == 1 {
            // Single crowd - detailed view
            println!("{}", format_crowd(&crowds[0], global.verbose));
        } else {
            // Multiple crowds - summary view
            println!("{}", format_crowds_summary(&crowds));
        }
    }

    DispatchResult::Exit(0)
}

/// Handle the tagmap command - unified search around a keyword
pub fn handle_tagmap_command(opts: &TagmapOptions, global: &GlobalOptions) -> DispatchResult {
    use crate::analyzer::crowd::detect_crowd_with_edges;
    use crate::analyzer::dead_parrots::{DeadFilterConfig, find_dead_exports};
    use std::path::Path;

    // Show spinner unless in quiet/json mode
    let spinner = if !global.quiet && !global.json {
        Some(Spinner::new(&format!(
            "Analyzing tagmap '{}'...",
            opts.keyword
        )))
    } else {
        None
    };

    // Load snapshot (auto-scan if missing)
    let root = opts
        .roots
        .first()
        .map(|p| p.as_path())
        .unwrap_or(Path::new("."));

    let snapshot = match load_or_create_snapshot(root, global) {
        Ok(s) => s,
        Err(e) => {
            if let Some(s) = spinner {
                s.finish_error(&format!("Failed to load snapshot: {}", e));
            } else {
                eprintln!("[loct][error] {}", e);
            }
            return DispatchResult::Exit(1);
        }
    };

    // Filter test files unless --include-tests
    let files: Vec<_> = if opts.include_tests {
        snapshot.files.clone()
    } else {
        snapshot
            .files
            .iter()
            .filter(|f| !is_test_file(&f.path))
            .cloned()
            .collect()
    };

    let keyword_lower = opts.keyword.to_lowercase();

    // 1. Find files matching keyword (in path or filename)
    let matching_files: Vec<_> = files
        .iter()
        .filter(|f| {
            let path_lower = f.path.to_lowercase();
            path_lower.contains(&keyword_lower)
        })
        .collect();

    // 2. Detect crowd for this keyword
    let crowd = detect_crowd_with_edges(&files, &opts.keyword, &snapshot.edges);

    // 3. Find dead exports related to keyword
    let dead_ok_globs = crate::fs_utils::load_loctignore_dead_ok_globs(root);
    let all_dead = find_dead_exports(
        &files,
        false,
        None,
        DeadFilterConfig {
            include_tests: opts.include_tests,
            dead_ok_globs,
            ..Default::default()
        },
    );
    let dead_for_keyword: Vec<_> = all_dead
        .iter()
        .filter(|d| {
            d.symbol.to_lowercase().contains(&keyword_lower)
                || d.file.to_lowercase().contains(&keyword_lower)
        })
        .collect();

    // Finish spinner
    if let Some(s) = spinner {
        s.finish_success(&format!(
            "Found {} files, {} crowd members, {} dead exports",
            matching_files.len(),
            crowd.members.len(),
            dead_for_keyword.len()
        ));
    }

    let limit = opts.limit.unwrap_or(usize::MAX);
    let files_omitted = matching_files.len().saturating_sub(limit);
    let crowd_omitted = crowd.members.len().saturating_sub(limit);
    let dead_omitted = dead_for_keyword.len().saturating_sub(limit);

    // Output results
    if global.json {
        let json = serde_json::json!({
            "keyword": opts.keyword,
            "files": {
                "count": matching_files.len(),
                "omitted_by_limit": if opts.limit.is_some() && files_omitted > 0 {
                    Some(files_omitted)
                } else {
                    None
                },
                "items": matching_files.iter().take(limit).map(|f| {
                    serde_json::json!({
                        "path": f.path,
                        "loc": f.loc,
                        "language": f.language
                    })
                }).collect::<Vec<_>>()
            },
            "crowd": {
                "pattern": crowd.pattern,
                "members": crowd.members.len(),
                "omitted_by_limit": if opts.limit.is_some() && crowd_omitted > 0 {
                    Some(crowd_omitted)
                } else {
                    None
                },
                "score": crowd.score,
                "issues": crowd.issues.len()
            },
            "dead_exports": {
                "count": dead_for_keyword.len(),
                "omitted_by_limit": if opts.limit.is_some() && dead_omitted > 0 {
                    Some(dead_omitted)
                } else {
                    None
                },
                "items": dead_for_keyword.iter().take(limit).map(|d| {
                    serde_json::json!({
                        "file": d.file,
                        "symbol": d.symbol,
                        "confidence": d.confidence
                    })
                }).collect::<Vec<_>>()
            }
        });
        println!(
            "{}",
            serde_json::to_string_pretty(&json).unwrap_or_default()
        );
    } else {
        // Human-readable output
        println!();
        println!("=== TAGMAP: '{}' ===", opts.keyword);

        println!("\nFILES MATCHING KEYWORD ({}):", matching_files.len());
        if matching_files.is_empty() {
            println!("  (none)");
        } else {
            for file in matching_files.iter().take(limit) {
                println!("  {} ({} LOC, {})", file.path, file.loc, file.language);
            }
            if opts.limit.is_some() && files_omitted > 0 {
                println!("  {} additional file(s) omitted by --limit", files_omitted);
            }
        }

        if !crowd.members.is_empty() {
            println!(
                "\nCROWD ANALYSIS ({} files, score {:.1}/10):",
                crowd.members.len(),
                crowd.score
            );
            for member in crowd.members.iter().take(limit) {
                println!("  {} ({} importers)", member.file, member.importer_count);
            }
            if opts.limit.is_some() && crowd_omitted > 0 {
                println!(
                    "  {} additional crowd member(s) omitted by --limit",
                    crowd_omitted
                );
            }
            if !crowd.issues.is_empty() {
                println!(
                    "  Issues: {} detected (use 'loct crowd {}' for details)",
                    crowd.issues.len(),
                    opts.keyword
                );
            }
        } else {
            println!("\nCROWD ANALYSIS: (no cluster found)");
        }

        if !dead_for_keyword.is_empty() {
            println!("\nDEAD EXPORTS ({}):", dead_for_keyword.len());
            for dead in dead_for_keyword.iter().take(limit) {
                println!("  {} in {} [{}]", dead.symbol, dead.file, dead.confidence);
            }
            if opts.limit.is_some() && dead_omitted > 0 {
                println!(
                    "  {} additional dead export(s) omitted by --limit",
                    dead_omitted
                );
            }
        } else {
            println!("\nDEAD EXPORTS: (none)");
        }

        println!();
    }

    DispatchResult::Exit(0)
}

pub fn handle_twins_command(opts: &TwinsOptions, global: &GlobalOptions) -> DispatchResult {
    use crate::analyzer::barrels::{analyze_barrel_chaos, format_barrel_analysis};
    use crate::analyzer::frameworks::detect_frameworks;
    use crate::analyzer::twins::{
        TwinCategory, categorize_twin, detect_exact_twins_with_frameworks, detect_language,
        find_dead_parrots, print_exact_twins_human, print_twins_human,
    };
    use std::path::Path;

    // Show spinner unless in quiet/json mode
    let spinner = if !global.quiet && !global.json {
        Some(Spinner::new("Analyzing semantic duplicates..."))
    } else {
        None
    };

    // Load snapshot (auto-scan if missing)
    let root = opts.path.as_deref().unwrap_or(Path::new("."));

    let snapshot = match load_or_create_snapshot(root, global) {
        Ok(s) => s,
        Err(e) => {
            if let Some(s) = spinner {
                s.finish_error(&format!("Failed to load snapshot: {}", e));
            } else {
                eprintln!("[loct][error] {}", e);
            }
            return DispatchResult::Exit(1);
        }
    };

    // Detect frameworks for convention filtering (unless --ignore-conventions)
    let detected_frameworks = if opts.ignore_conventions {
        Vec::new()
    } else {
        detect_frameworks(root)
    };

    // Log detected frameworks if verbose
    if global.verbose && !detected_frameworks.is_empty() {
        let fw_names: Vec<_> = detected_frameworks.iter().map(|f| f.to_string()).collect();
        eprintln!(
            "[loct][twins] Detected frameworks: {} (use --ignore-conventions to show all)",
            fw_names.join(", ")
        );
    }

    // Load suppressions (unless --include-suppressed)
    let suppressions = if opts.include_suppressed {
        Suppressions::default()
    } else {
        Suppressions::load(root)
    };

    // Run dead parrot analysis
    let mut dead_result = find_dead_parrots(&snapshot.files, opts.dead_only, opts.include_tests);

    // Filter out suppressed dead parrots
    if !opts.include_suppressed {
        dead_result.dead_parrots.retain(|dp| {
            !suppressions.is_suppressed(&SuppressionType::DeadParrot, &dp.name, Some(&dp.file_path))
                && !suppressions.is_suppressed(
                    &SuppressionType::DeadExport,
                    &dp.name,
                    Some(&dp.file_path),
                )
        });
    }

    // Run exact twins detection with framework awareness (unless dead_only)
    let frameworks_opt = if detected_frameworks.is_empty() {
        None
    } else {
        Some(detected_frameworks.as_slice())
    };

    let mut twins = if !opts.dead_only {
        detect_exact_twins_with_frameworks(&snapshot.files, opts.include_tests, frameworks_opt)
    } else {
        Vec::new()
    };

    // Filter out suppressed twins
    if !opts.include_suppressed {
        twins.retain(|twin| !suppressions.is_suppressed(&SuppressionType::Twins, &twin.name, None));
    }

    // Run barrel chaos analysis (unless dead_only)
    let barrel_analysis = if !opts.dead_only {
        Some(analyze_barrel_chaos(&snapshot))
    } else {
        None
    };

    // Finish spinner before printing results
    if let Some(s) = spinner {
        s.finish_success(&format!(
            "Found {} dead parrot(s), {} twin group(s)",
            dead_result.dead_parrots.len(),
            twins.len()
        ));
    }

    // JSON mode: emit single combined JSON object
    if global.json {
        // Categorize twins
        let (same_lang, cross_lang): (Vec<_>, Vec<_>) = twins
            .iter()
            .partition(|twin| matches!(categorize_twin(twin), TwinCategory::SameLanguage(_)));

        // Count twins with high signature similarity
        let high_similarity_count = twins
            .iter()
            .filter(|t| t.signature_similarity.map(|s| s >= 0.8).unwrap_or(false))
            .count();

        let twin_to_json = |twin: &crate::analyzer::twins::ExactTwin| {
            let category = categorize_twin(twin);
            let mut json = serde_json::json!({
                "name": twin.name,
                "category": match category {
                    TwinCategory::SameLanguage(ref lang) => format!("same_language:{:?}", lang).to_lowercase(),
                    TwinCategory::CrossLanguage => "cross_language".to_string(),
                },
                "locations": twin.locations.iter().map(|loc| {
                    let mut loc_json = serde_json::json!({
                        "file": loc.file_path,
                        "line": loc.line,
                        "kind": loc.kind,
                        "imports": loc.import_count,
                        "canonical": loc.is_canonical,
                        "language": format!("{:?}", detect_language(&loc.file_path)).to_lowercase(),
                    });
                    if let Some(ref fp) = loc.signature_fingerprint {
                        loc_json["signature_fingerprint"] = serde_json::json!(fp);
                    }
                    loc_json
                }).collect::<Vec<_>>(),
            });
            if let Some(sim) = twin.signature_similarity {
                json["signature_similarity"] = serde_json::json!(sim);
            }
            json
        };

        // Build barrel chaos JSON if available
        let barrel_json = barrel_analysis.as_ref().map(|ba| {
            serde_json::json!({
                "missing_barrels": ba.missing_barrels,
                "deep_chains": ba.deep_chains.iter().map(|c| {
                    serde_json::json!({
                        "symbol": c.symbol,
                        "depth": c.depth,
                        "chain": c.chain,
                    })
                }).collect::<Vec<_>>(),
                "inconsistent_paths": ba.inconsistent_paths,
            })
        });

        // Combined output
        let output = serde_json::json!({
            "dead_parrots": dead_result.dead_parrots.iter().map(|e| {
                serde_json::json!({
                    "name": e.name,
                    "file": e.file_path,
                    "line": e.line,
                    "kind": e.kind,
                    "import_count": e.import_count,
                })
            }).collect::<Vec<_>>(),
            "exact_twins": twins.iter().map(twin_to_json).collect::<Vec<_>>(),
            "barrel_chaos": barrel_json,
            "summary": {
                "total_symbols": dead_result.total_symbols,
                "total_files": dead_result.total_files,
                "dead_parrots": dead_result.dead_parrots.len(),
                "twin_groups": twins.len(),
                "same_language_groups": same_lang.len(),
                "cross_language_groups": cross_lang.len(),
                "high_similarity_groups": high_similarity_count,
            }
        });

        println!("{}", serde_json::to_string_pretty(&output).unwrap());
    } else {
        // Human-readable mode: separate sections
        print_twins_human(&dead_result);

        if !twins.is_empty() {
            print_exact_twins_human(&twins);
        }

        if let Some(ref ba) = barrel_analysis {
            let has_issues = !ba.missing_barrels.is_empty()
                || !ba.deep_chains.is_empty()
                || !ba.inconsistent_paths.is_empty();

            if has_issues {
                println!("{}", format_barrel_analysis(ba));
            }
        }
    }

    DispatchResult::Exit(0)
}

/// Handle the sniff command - aggregate code smells
pub fn handle_sniff_command(opts: &SniffOptions, global: &GlobalOptions) -> DispatchResult {
    use crate::analyzer::crowd::detect_all_crowds_with_edges;
    use crate::analyzer::twins::{detect_exact_twins, find_dead_parrots};
    use std::path::Path;

    // Deprecation warning (goes to stderr, won't break piped output)
    warn_deprecated("sniff", "loct findings");

    // Show spinner unless in quiet/json mode
    let spinner = if !global.quiet && !global.json {
        Some(Spinner::new("Sniffing for code smells..."))
    } else {
        None
    };

    // Load snapshot (auto-scan if missing)
    let root = opts.path.as_deref().unwrap_or(Path::new("."));

    let snapshot = match load_or_create_snapshot(root, global) {
        Ok(s) => s,
        Err(e) => {
            if let Some(s) = spinner {
                s.finish_error(&format!("Failed to load snapshot: {}", e));
            } else {
                eprintln!("[loct][error] {}", e);
            }
            return DispatchResult::Exit(1);
        }
    };

    // Filter files based on include_tests
    let files: Vec<_> = if opts.include_tests {
        snapshot.files.clone()
    } else {
        snapshot
            .files
            .iter()
            .filter(|f| !is_test_file(&f.path))
            .cloned()
            .collect()
    };

    // Collect all findings
    let mut twins_count = 0;
    let mut dead_count = 0;
    let mut crowds_count = 0;

    let twins = if !opts.crowds_only && !opts.dead_only {
        let t = detect_exact_twins(&files, opts.include_tests);
        twins_count = t.len();
        Some(t)
    } else {
        None
    };

    let dead_parrots = if !opts.crowds_only && !opts.twins_only {
        let result = find_dead_parrots(&files, false, opts.include_tests);
        dead_count = result.dead_parrots.len();
        Some(result)
    } else {
        None
    };

    let crowds = if !opts.twins_only && !opts.dead_only {
        let mut c = detect_all_crowds_with_edges(&files, &snapshot.edges);
        if let Some(min_size) = opts.min_crowd_size {
            c.retain(|crowd| crowd.members.len() >= min_size);
        }
        crowds_count = c.len();
        Some(c)
    } else {
        None
    };

    let total_smells = twins_count + dead_count + crowds_count;

    if let Some(s) = spinner {
        s.finish_success(&format!("Found {} code smell(s)", total_smells));
    }

    // Output results
    if global.json {
        // JSON output
        let output = serde_json::json!({
            "twins": twins.as_ref().map(|t| t.iter().map(|twin| {
                serde_json::json!({
                    "name": twin.name,
                    "locations": twin.locations.iter().map(|loc| {
                        serde_json::json!({
                            "file": loc.file_path,
                            "line": loc.line,
                            "kind": loc.kind,
                        })
                    }).collect::<Vec<_>>(),
                })
            }).collect::<Vec<_>>()),
            "dead_parrots": dead_parrots.as_ref().map(|dp| dp.dead_parrots.iter().map(|e| {
                serde_json::json!({
                    "name": e.name,
                    "file": e.file_path,
                    "line": e.line,
                    "kind": e.kind,
                })
            }).collect::<Vec<_>>()),
            "crowds": crowds.as_ref().map(|c| c.iter().map(|crowd| {
                serde_json::json!({
                    "pattern": crowd.pattern,
                    "size": crowd.members.len(),
                    "members": crowd.members.iter().map(|m| m.file.clone()).collect::<Vec<_>>(),
                })
            }).collect::<Vec<_>>()),
            "summary": {
                "twins": twins_count,
                "dead_parrots": dead_count,
                "crowds": crowds_count,
                "total": total_smells,
            }
        });

        match serde_json::to_string_pretty(&output) {
            Ok(json) => println!("{}", json),
            Err(e) => {
                eprintln!("[loct][error] Failed to serialize results: {}", e);
                return DispatchResult::Exit(1);
            }
        }
    } else {
        // Human-readable output with friendly tone
        println!("🐕 SNIFFING FOR CODE SMELLS...\n");

        // Twins section
        if let Some(ref twins_list) = twins
            && !twins_list.is_empty()
        {
            println!(
                "📍 TWINS (same name, different files) - {} found",
                twins_count
            );
            println!("   Consider: consolidate or rename to avoid import confusion\n");

            for twin in twins_list.iter().take(20) {
                println!("   {} ({} locations)", twin.name, twin.locations.len());
                for loc in &twin.locations {
                    println!("   ├─ {}:{}", loc.file_path, loc.line);
                }
                println!();
            }

            if twins_list.len() > 20 {
                println!(
                    "   {} additional twin group(s) omitted in sniff summary; use `loct findings` for full truth\n",
                    twins_list.len() - 20
                );
            }
        }

        // Dead parrots section
        if let Some(ref dp_result) = dead_parrots
            && !dp_result.dead_parrots.is_empty()
        {
            println!("📍 DEAD PARROTS (unused exports) - {} found", dead_count);
            println!("   Consider: remove if truly unused, or document if external API\n");

            // Group by file
            let mut by_file: std::collections::HashMap<
                String,
                Vec<&crate::analyzer::twins::SymbolEntry>,
            > = std::collections::HashMap::new();
            for entry in &dp_result.dead_parrots {
                by_file
                    .entry(entry.file_path.clone())
                    .or_default()
                    .push(entry);
            }

            let mut files: Vec<_> = by_file.keys().collect();
            files.sort();

            for file in files.iter().take(10) {
                let entries = &by_file[*file];
                for entry in entries.iter().take(3) {
                    println!("   {} in {}:{}", entry.name, entry.file_path, entry.line);
                }
                if entries.len() > 3 {
                    println!(
                        "   {} additional dead export(s) omitted for {} in sniff summary",
                        entries.len() - 3,
                        file
                    );
                }
            }

            if files.len() > 10 {
                println!(
                    "   {} additional file(s) with dead exports omitted in sniff summary",
                    files.len() - 10
                );
            }
            println!();
        }

        // Crowds section
        if let Some(ref crowds_list) = crowds
            && !crowds_list.is_empty()
        {
            println!("📍 CROWDS (similar files) - {} groups", crowds_count);
            println!("   Consider: these files share many dependencies, possible duplication\n");

            for (idx, crowd) in crowds_list.iter().take(5).enumerate() {
                println!("   Group {}: {} pattern", idx + 1, crowd.pattern);
                for member in crowd.members.iter().take(5) {
                    println!("   ├─ {}", member.file);
                }
                if crowd.members.len() > 5 {
                    println!(
                        "   └─ {} additional member(s) omitted in sniff summary",
                        crowd.members.len() - 5
                    );
                }
                println!();
            }

            if crowds_list.len() > 5 {
                println!(
                    "   {} additional crowd group(s) omitted in sniff summary; use `loct findings` for full truth\n",
                    crowds_list.len() - 5
                );
            }
        }

        // Summary
        println!(
            "Summary: {} smells found. These are hints, not verdicts - you decide what matters.",
            total_smells
        );

        if total_smells == 0 {
            println!("\n[OK] No code smells detected - codebase looks clean!");
        }
    }

    DispatchResult::Exit(0)
}

/// Handle the suppress command - manage false positive suppressions
pub fn handle_suppress_command(opts: &SuppressOptions, _global: &GlobalOptions) -> DispatchResult {
    use std::path::Path;

    let root = Path::new(".");

    // Handle --clear
    if opts.clear {
        let mut suppressions = Suppressions::load(root);
        suppressions.clear();
        if let Err(e) = suppressions.save(root) {
            eprintln!("[loct][error] Failed to save suppressions: {}", e);
            return DispatchResult::Exit(1);
        }
        println!("All suppressions cleared.");
        return DispatchResult::Exit(0);
    }

    // Handle --list
    if opts.list {
        let suppressions = Suppressions::load(root);
        if suppressions.items.is_empty() {
            println!("No suppressions configured.");
            println!("Tip: Use `loct suppress twins <symbol>` to suppress false positives.");
        } else {
            println!("Current suppressions ({}):\n", suppressions.len());
            for s in &suppressions.items {
                let file_info = s
                    .file
                    .as_ref()
                    .map(|f| format!(" @ {}", f))
                    .unwrap_or_default();
                let reason_info = s
                    .reason
                    .as_ref()
                    .map(|r| format!(" - {}", r))
                    .unwrap_or_default();
                println!(
                    "  {} {}{}{}  ({})",
                    s.suppression_type, s.symbol, file_info, reason_info, s.added
                );
            }
        }
        return DispatchResult::Exit(0);
    }

    // Handle --remove
    if opts.remove {
        let suppression_type = match opts.suppression_type.as_deref() {
            Some("twins") => SuppressionType::Twins,
            Some("dead_parrot") => SuppressionType::DeadParrot,
            Some("dead_export") => SuppressionType::DeadExport,
            Some("circular") => SuppressionType::Circular,
            Some(other) => {
                eprintln!(
                    "[loct][error] Unknown suppression type '{}'. Valid: twins, dead_parrot, dead_export, circular",
                    other
                );
                return DispatchResult::Exit(1);
            }
            None => {
                eprintln!("[loct][error] --remove requires a type and symbol");
                return DispatchResult::Exit(1);
            }
        };

        let symbol = match &opts.symbol {
            Some(s) => s.clone(),
            None => {
                eprintln!("[loct][error] --remove requires a symbol name");
                return DispatchResult::Exit(1);
            }
        };

        let mut suppressions = Suppressions::load(root);
        if suppressions.remove(&suppression_type, &symbol) {
            if let Err(e) = suppressions.save(root) {
                eprintln!("[loct][error] Failed to save suppressions: {}", e);
                return DispatchResult::Exit(1);
            }
            println!("Removed suppression for {} '{}'", suppression_type, symbol);
        } else {
            println!("No matching suppression found.");
        }
        return DispatchResult::Exit(0);
    }

    // Handle adding a suppression
    let suppression_type = match opts.suppression_type.as_deref() {
        Some("twins") => SuppressionType::Twins,
        Some("dead_parrot") => SuppressionType::DeadParrot,
        Some("dead_export") => SuppressionType::DeadExport,
        Some("circular") => SuppressionType::Circular,
        Some(other) => {
            eprintln!(
                "[loct][error] Unknown suppression type '{}'. Valid: twins, dead_parrot, dead_export, circular",
                other
            );
            return DispatchResult::Exit(1);
        }
        None => {
            eprintln!("[loct][error] Usage: loct suppress <type> <symbol>");
            eprintln!("       loct suppress --list");
            eprintln!("       loct suppress --clear");
            return DispatchResult::Exit(1);
        }
    };

    let symbol = match &opts.symbol {
        Some(s) => s.clone(),
        None => {
            eprintln!(
                "[loct][error] Symbol name required. Usage: loct suppress {} <symbol>",
                suppression_type
            );
            return DispatchResult::Exit(1);
        }
    };

    let mut suppressions = Suppressions::load(root);
    suppressions.add(
        suppression_type.clone(),
        symbol.clone(),
        opts.file.clone(),
        opts.reason.clone(),
    );

    if let Err(e) = suppressions.save(root) {
        eprintln!("[loct][error] Failed to save suppressions: {}", e);
        return DispatchResult::Exit(1);
    }

    let file_info = opts
        .file
        .as_ref()
        .map(|f| format!(" in {}", f))
        .unwrap_or_default();
    println!(
        "Added suppression: {} '{}'{}",
        suppression_type, symbol, file_info
    );
    println!("This finding will be hidden from future runs.");
    println!("Use --include-suppressed to show suppressed items.");

    DispatchResult::Exit(0)
}