unfault 1.0.11

Unfault — a cognitive context engine for thoughtful engineers
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
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
//! Client-side IR (Intermediate Representation) builder.
//!
//! This module provides functionality to parse source files locally and build
//! a complete IR containing semantics and a code graph. The IR can then be
//! serialized and sent to the API for rule evaluation, without sending
//! source code over the wire.
//!
//! ## Architecture
//!
//! The client-side parsing flow:
//! 1. Discover source files in the workspace
//! 2. Parse each file using tree-sitter (via unfault-core)
//! 3. Extract language-specific semantics (imports, functions, calls, etc.)
//! 4. Build a unified code graph
//! 5. Package into IntermediateRepresentation
//! 6. Serialize to JSON and send to API
//!
//! ## Example
//!
//! ```rust,ignore
//! use unfault::session::ir_builder::build_ir;
//!
//! let ir = build_ir(&workspace_path, None, false)?;
//! let ir_json = serde_json::to_string(&ir)?;
//!
//! // Send ir_json to POST /v1/graph/analyze
//! ```

use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Instant;

use anyhow::Result;
use colored::Colorize;
use log::debug;
use rayon::prelude::*;

use unfault_core::IntermediateRepresentation;
use unfault_core::graph::build_code_graph;
use unfault_core::parse::ast::FileId;
use unfault_core::parse::{go, python, rust as rust_parse, typescript};
use unfault_core::semantics::SourceSemantics;
use unfault_core::semantics::go::model::GoFileSemantics;
use unfault_core::semantics::python::model::PyFileSemantics;
use unfault_core::semantics::rust::{build_rust_semantics, model::RustFileSemantics};
use unfault_core::semantics::typescript::model::TsFileSemantics;
use unfault_core::types::context::{Language, SourceFile};

use super::semantics_cache::{CacheStatsSnapshot, SemanticsCache};

/// Result of building IR with cache statistics
#[derive(Debug)]
pub struct IrBuildResult {
    /// The built intermediate representation
    pub ir: IntermediateRepresentation,
    /// Cache statistics snapshot (hits, misses, etc.)
    pub cache_stats: CacheStatsSnapshot,
}

/// Try to load a cached code graph from disk.
///
/// Returns `Some(graph)` if the cache file exists and its stored aggregate
/// hash matches `expected_hash`. Returns `None` otherwise.
fn try_load_graph_cache(
    path: &std::path::Path,
    expected_hash: u64,
    verbose: bool,
) -> Option<unfault_core::graph::CodeGraph> {
    use std::io::BufReader;

    // The file is: [aggregate_hash: u64 LE][msgpack graph bytes]
    let mut file = std::fs::File::open(path).ok()?;
    use std::io::Read;
    let mut header = [0u8; 8];
    file.read_exact(&mut header).ok()?;
    let stored_hash = u64::from_le_bytes(header);
    if stored_hash != expected_hash {
        return None;
    }

    let reader = BufReader::new(file);
    match rmp_serde::from_read::<_, unfault_core::graph::CodeGraph>(reader) {
        Ok(mut graph) => {
            graph.rebuild_indexes();
            if verbose {
                eprintln!("{} Graph loaded from cache", "TIMING".yellow());
            }
            Some(graph)
        }
        Err(_) => None,
    }
}

/// Save a code graph to the graph cache file.
fn save_graph_cache(
    path: &std::path::Path,
    aggregate_hash: u64,
    graph: &unfault_core::graph::CodeGraph,
    _verbose: bool,
) -> anyhow::Result<()> {
    use std::io::{BufWriter, Write};

    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    let file = std::fs::File::create(path)?;
    let mut writer = BufWriter::new(file);
    // Write 8-byte aggregate hash header, then msgpack graph
    writer.write_all(&aggregate_hash.to_le_bytes())?;
    rmp_serde::encode::write(&mut writer, graph)?;
    Ok(())
}

/// Build an Intermediate Representation from files in a directory.
///
/// This function:
/// 1. Discovers source files in the directory
/// 2. Parses each file using tree-sitter
/// 3. Extracts semantics (imports, functions, calls)
/// 4. Builds a unified code graph
/// 5. Returns a serializable IntermediateRepresentation
///
/// # Arguments
///
/// * `workspace_path` - Path to the workspace directory
/// * `file_paths` - Optional list of specific files to include (if None, discover all)
/// * `verbose` - Enable verbose logging
///
/// # Returns
///
/// An [`IntermediateRepresentation`] containing semantics for each file and
/// a code graph representing the relationships between code elements.
///
/// # Example
///
/// ```rust,ignore
/// use unfault::session::ir_builder::build_ir;
///
/// // Build IR for all files in workspace
/// let ir = build_ir(&workspace_path, None, false)?;
///
/// // Build IR for specific files only
/// let files = vec![PathBuf::from("src/main.py"), PathBuf::from("src/utils.py")];
/// let ir = build_ir(&workspace_path, Some(&files), true)?;
/// ```
pub fn build_ir(
    workspace_path: &Path,
    file_paths: Option<&[PathBuf]>,
    verbose: bool,
) -> Result<IntermediateRepresentation> {
    // Determine files to process
    let files = match file_paths {
        Some(paths) => paths.to_vec(),
        None => discover_source_files(workspace_path)?,
    };

    if verbose {
        eprintln!("Building IR from {} files...", files.len());
    }

    // Parse and extract semantics from each file in parallel
    let results: Vec<Option<(FileId, SourceSemantics)>> = files
        .par_iter()
        .enumerate()
        .map(|(index, file_path)| {
            let Some(language) = detect_language(file_path) else {
                return None;
            };

            let content = match std::fs::read_to_string(file_path) {
                Ok(c) => c,
                Err(e) => {
                    if verbose {
                        eprintln!("Warning: Could not read {}: {}", file_path.display(), e);
                    }
                    return None;
                }
            };

            let relative_path = file_path
                .strip_prefix(workspace_path)
                .unwrap_or(file_path)
                .to_string_lossy()
                .to_string();

            let file_id = FileId((index + 1) as u64);

            let source_file = SourceFile {
                path: relative_path.clone(),
                language,
                content,
            };

            // Parse and build semantics based on language
            let semantics = match language {
                Language::Python => {
                    let parsed = match python::parse_python_file(file_id, &source_file) {
                        Ok(p) => p,
                        Err(e) => {
                            if verbose {
                                eprintln!("Warning: Could not parse {}: {}", relative_path, e);
                            }
                            return None;
                        }
                    };
                    let mut sem = PyFileSemantics::from_parsed(&parsed);
                    // Analyze frameworks (FastAPI, etc.)
                    if let Err(e) = sem.analyze_frameworks(&parsed) {
                        if verbose {
                            eprintln!(
                                "Warning: Framework analysis failed for {}: {}",
                                relative_path, e
                            );
                        }
                    }
                    SourceSemantics::Python(sem)
                }
                Language::Go => {
                    let parsed = match go::parse_go_file(file_id, &source_file) {
                        Ok(p) => p,
                        Err(e) => {
                            if verbose {
                                eprintln!("Warning: Could not parse {}: {}", relative_path, e);
                            }
                            return None;
                        }
                    };
                    let mut sem = GoFileSemantics::from_parsed(&parsed);
                    // Analyze frameworks (Gin, etc.)
                    if let Err(e) = sem.analyze_frameworks(&parsed) {
                        if verbose {
                            eprintln!(
                                "Warning: Framework analysis failed for {}: {}",
                                relative_path, e
                            );
                        }
                    }
                    SourceSemantics::Go(sem)
                }
                Language::Rust => {
                    let parsed = match rust_parse::parse_rust_file(file_id, &source_file) {
                        Ok(p) => p,
                        Err(e) => {
                            if verbose {
                                eprintln!("Warning: Could not parse {}: {}", relative_path, e);
                            }
                            return None;
                        }
                    };
                    let sem = match build_rust_semantics(&parsed) {
                        Ok(s) => s,
                        Err(_) => RustFileSemantics::from_parsed(&parsed),
                    };
                    SourceSemantics::Rust(sem)
                }
                Language::Typescript => {
                    let parsed = match typescript::parse_typescript_file(file_id, &source_file) {
                        Ok(p) => p,
                        Err(e) => {
                            if verbose {
                                eprintln!("Warning: Could not parse {}: {}", relative_path, e);
                            }
                            return None;
                        }
                    };
                    let mut sem = TsFileSemantics::from_parsed(&parsed);
                    // Analyze frameworks (Express, etc.)
                    if let Err(e) = sem.analyze_frameworks(&parsed) {
                        if verbose {
                            eprintln!(
                                "Warning: Framework analysis failed for {}: {}",
                                relative_path, e
                            );
                        }
                    }
                    SourceSemantics::Typescript(sem)
                }
                _ => {
                    if verbose {
                        eprintln!(
                            "Skipping unsupported language for {}: {:?}",
                            relative_path, language
                        );
                    }
                    return None;
                }
            };

            Some((file_id, semantics))
        })
        .collect();

    let mut semantics_entries: Vec<(FileId, Arc<SourceSemantics>)> = Vec::new();
    let mut all_semantics: Vec<SourceSemantics> = Vec::new();

    for result in results {
        if let Some((file_id, semantics)) = result {
            all_semantics.push(semantics.clone());
            semantics_entries.push((file_id, Arc::new(semantics)));
        }
    }

    if verbose {
        eprintln!("Parsed {} files successfully", semantics_entries.len());
    }

    // Build the code graph
    let code_graph = build_code_graph(&semantics_entries);

    if verbose {
        let stats = code_graph.stats();
        eprintln!(
            "Built graph: {} files, {} functions, {} imports, {} library usages",
            stats.file_count,
            stats.function_count,
            stats.import_edge_count,
            stats.uses_library_edge_count
        );
    }

    Ok(IntermediateRepresentation::new(all_semantics, code_graph))
}

/// Build an Intermediate Representation with caching support.
///
/// Similar to [`build_ir`], but uses a file-based cache to skip re-parsing
/// files that haven't changed. On incremental runs, this can reduce parse
/// time from ~2000ms to ~50-100ms.
///
/// # Arguments
///
/// * `workspace_path` - Path to the workspace directory
/// * `file_paths` - Optional list of specific files to include
/// * `verbose` - Enable verbose logging
///
/// # Returns
///
/// An [`IrBuildResult`] containing the IR and cache statistics.
///
/// # Example
///
/// ```rust,ignore
/// use unfault::session::ir_builder::build_ir_cached;
///
/// let result = build_ir_cached(&workspace_path, None, false)?;
/// println!("Cache hit rate: {:.1}%", result.cache_stats.hit_rate());
/// let ir_json = serde_json::to_string(&result.ir)?;
/// ```
pub fn build_ir_cached(
    workspace_path: &Path,
    file_paths: Option<&[PathBuf]>,
    verbose: bool,
) -> Result<IrBuildResult> {
    let _total_start = Instant::now();

    // Open or create the cache
    let cache_start = Instant::now();
    let cache = Arc::new(Mutex::new(SemanticsCache::open(workspace_path)?));
    let cache_open_ms = cache_start.elapsed().as_millis();

    // Determine files to process — test files are always excluded regardless
    // of whether the caller supplied an explicit list or we discover them.
    let discover_start = Instant::now();
    let files = match file_paths {
        Some(paths) => paths.iter().filter(|p| !is_test_file(p)).cloned().collect(),
        None => discover_source_files(workspace_path)?,
    };
    let discover_ms = discover_start.elapsed().as_millis();

    if verbose {
        eprintln!("Building IR from {} files (with cache)...", files.len());
        eprintln!(
            "{} Cache open: {}ms, File discovery: {}ms",
            "TIMING".yellow(),
            cache_open_ms,
            discover_ms
        );
    }

    // Parse and extract semantics from each file in parallel, using cache.
    //
    // On a warm cache this phase is I/O-bound (stat + msgpack read per file),
    // not CPU-bound. Use a dedicated thread pool with more threads than logical
    // CPUs so threads waiting on filesystem I/O don't block CPU-bound threads.
    // 64 threads is a reasonable upper bound: beyond that macOS APFS and SSD
    // queue depths saturate without further benefit.
    let io_pool = rayon::ThreadPoolBuilder::new()
        .num_threads(64.min(files.len()))
        .thread_name(|i| format!("unfault-io-{}", i))
        .build()
        .unwrap_or_else(|_| rayon::ThreadPoolBuilder::new().build().unwrap());

    let parse_start = Instant::now();
    let results: Vec<Option<(FileId, SourceSemantics)>> = io_pool.install(|| {
        files
            .par_iter()
            .enumerate()
            .map(|(index, file_path)| {
                let Some(language) = detect_language(file_path) else {
                    return None;
                };

                let relative_path = file_path
                    .strip_prefix(workspace_path)
                    .unwrap_or(file_path)
                    .to_string_lossy()
                    .to_string();

                let file_id = FileId((index + 1) as u64);

                // Fast path: check mtime + size before reading file content.
                // Phase 1 (under lock): check index, get cache file path if match.
                // Phase 2 (outside lock): read msgpack — parallel I/O, no contention.
                if let Ok(meta) = std::fs::metadata(file_path) {
                    let mtime_secs = meta
                        .modified()
                        .ok()
                        .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                        .map(|d| d.as_secs())
                        .unwrap_or(0);
                    let file_size = meta.len();

                    let cache_file_path = {
                        let cache_guard = cache.lock().unwrap();
                        cache_guard.check_metadata(&relative_path, mtime_secs, file_size)
                    };

                    if let Some((_hash, cache_path)) = cache_file_path {
                        // Read msgpack outside the lock — parallel across threads
                        if let Ok(file) = std::fs::File::open(&cache_path) {
                            let reader = std::io::BufReader::new(file);
                            if let Ok(semantics) =
                                rmp_serde::from_read::<_, SourceSemantics>(reader)
                            {
                                cache.lock().unwrap().record_metadata_hit();
                                return Some((file_id, semantics));
                            }
                        }
                    }
                }

                // Slow path: read file, compute content hash, check cache.
                let content = match std::fs::read_to_string(file_path) {
                    Ok(c) => c,
                    Err(e) => {
                        if verbose {
                            eprintln!("Warning: Could not read {}: {}", file_path.display(), e);
                        }
                        return None;
                    }
                };

                // Compute content hash
                let content_hash = SemanticsCache::hash_content(&content);

                // Try content-hash cache
                {
                    let cache_guard = cache.lock().unwrap();
                    if let Some(cached_semantics) = cache_guard.get(&relative_path, content_hash) {
                        return Some((file_id, cached_semantics));
                    }
                }

                let source_file = SourceFile {
                    path: relative_path.clone(),
                    language,
                    content: content.clone(),
                };

                // Parse and build semantics based on language
                let semantics = match language {
                    Language::Python => {
                        let parsed = match python::parse_python_file(file_id, &source_file) {
                            Ok(p) => p,
                            Err(e) => {
                                if verbose {
                                    eprintln!("Warning: Could not parse {}: {}", relative_path, e);
                                }
                                cache.lock().unwrap().record_miss();
                                return None;
                            }
                        };
                        let mut sem = PyFileSemantics::from_parsed(&parsed);
                        if let Err(e) = sem.analyze_frameworks(&parsed) {
                            if verbose {
                                eprintln!(
                                    "Warning: Framework analysis failed for {}: {}",
                                    relative_path, e
                                );
                            }
                        }
                        SourceSemantics::Python(sem)
                    }
                    Language::Go => {
                        let parsed = match go::parse_go_file(file_id, &source_file) {
                            Ok(p) => p,
                            Err(e) => {
                                if verbose {
                                    eprintln!("Warning: Could not parse {}: {}", relative_path, e);
                                }
                                cache.lock().unwrap().record_miss();
                                return None;
                            }
                        };
                        let mut sem = GoFileSemantics::from_parsed(&parsed);
                        if let Err(e) = sem.analyze_frameworks(&parsed) {
                            if verbose {
                                eprintln!(
                                    "Warning: Framework analysis failed for {}: {}",
                                    relative_path, e
                                );
                            }
                        }
                        SourceSemantics::Go(sem)
                    }
                    Language::Rust => {
                        let parsed = match rust_parse::parse_rust_file(file_id, &source_file) {
                            Ok(p) => p,
                            Err(e) => {
                                if verbose {
                                    eprintln!("Warning: Could not parse {}: {}", relative_path, e);
                                }
                                cache.lock().unwrap().record_miss();
                                return None;
                            }
                        };
                        let sem = match build_rust_semantics(&parsed) {
                            Ok(s) => s,
                            Err(_) => RustFileSemantics::from_parsed(&parsed),
                        };
                        SourceSemantics::Rust(sem)
                    }
                    Language::Typescript => {
                        let parsed = match typescript::parse_typescript_file(file_id, &source_file)
                        {
                            Ok(p) => p,
                            Err(e) => {
                                if verbose {
                                    eprintln!("Warning: Could not parse {}: {}", relative_path, e);
                                }
                                cache.lock().unwrap().record_miss();
                                return None;
                            }
                        };
                        let mut sem = TsFileSemantics::from_parsed(&parsed);
                        if let Err(e) = sem.analyze_frameworks(&parsed) {
                            if verbose {
                                eprintln!(
                                    "Warning: Framework analysis failed for {}: {}",
                                    relative_path, e
                                );
                            }
                        }
                        SourceSemantics::Typescript(sem)
                    }
                    _ => {
                        if verbose {
                            eprintln!(
                                "Skipping unsupported language for {}: {:?}",
                                relative_path, language
                            );
                        }
                        return None;
                    }
                };

                // Store in cache (include mtime+size for fast metadata pre-check next run)
                {
                    let (mtime_secs, file_size) = std::fs::metadata(file_path)
                        .ok()
                        .map(|m| {
                            let mtime = m
                                .modified()
                                .ok()
                                .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                                .map(|d| d.as_secs())
                                .unwrap_or(0);
                            (mtime, m.len())
                        })
                        .unwrap_or((0, 0));
                    let mut cache_guard = cache.lock().unwrap();
                    cache_guard.set(
                        &relative_path,
                        content_hash,
                        mtime_secs,
                        file_size,
                        &semantics,
                    );
                }

                Some((file_id, semantics))
            })
            .collect()
    }); // end io_pool.install
    let parse_ms = parse_start.elapsed().as_millis();

    let mut semantics_entries: Vec<(FileId, Arc<SourceSemantics>)> = Vec::new();
    let mut all_semantics: Vec<SourceSemantics> = Vec::new();
    let mut content_hashes: Vec<u64> = Vec::new();

    for result in results {
        if let Some((file_id, semantics)) = result {
            all_semantics.push(semantics.clone());
            semantics_entries.push((file_id, Arc::new(semantics)));
        }
    }

    // Get final cache stats
    let cache_stats = cache.lock().unwrap().stats_snapshot();

    if verbose {
        eprintln!(
            "Parsed {} files (cache: {} hits, {} misses, {:.1}% hit rate)",
            semantics_entries.len(),
            cache_stats.hits,
            cache_stats.misses,
            cache_stats.hit_rate()
        );
        eprintln!("{} File read + cache: {}ms", "TIMING".yellow(), parse_ms);
    }

    // Build or load the code graph.
    // If all files were cache hits, try to load a pre-built graph from disk
    // to avoid rebuilding petgraph (~1.3s on large workspaces).
    let graph_start = Instant::now();
    let all_cache_hits = cache_stats.misses == 0;
    let graph_cache_path = workspace_path
        .join(".unfault")
        .join("cache")
        .join("graph.msgpack");

    // Compute aggregate hash over sorted content hashes to key the graph cache.
    {
        let cache_guard = cache.lock().unwrap();
        content_hashes = files
            .iter()
            .filter_map(|f| {
                let rel = f
                    .strip_prefix(workspace_path)
                    .unwrap_or(f)
                    .to_string_lossy()
                    .to_string();
                let path_hash = xxhash_rust::xxh3::xxh3_64(rel.as_bytes());
                cache_guard.get_stored_content_hash(path_hash)
            })
            .collect();
    }
    content_hashes.sort_unstable();
    let aggregate_hash = {
        let bytes: Vec<u8> = content_hashes
            .iter()
            .flat_map(|h| h.to_le_bytes())
            .collect();
        xxhash_rust::xxh3::xxh3_64(&bytes)
    };

    let code_graph = if all_cache_hits {
        try_load_graph_cache(&graph_cache_path, aggregate_hash, verbose)
            .unwrap_or_else(|| build_code_graph(&semantics_entries))
    } else {
        build_code_graph(&semantics_entries)
    };

    // Save graph cache when all entries were cache hits (graph is stable)
    // and the cache file doesn't already match.
    if all_cache_hits {
        let _ = save_graph_cache(&graph_cache_path, aggregate_hash, &code_graph, verbose);
    }

    let graph_ms = graph_start.elapsed().as_millis();

    let stats = code_graph.stats();
    if verbose {
        eprintln!(
            "Built graph: {} files, {} functions, {} imports, {} library usages",
            stats.file_count,
            stats.function_count,
            stats.import_edge_count,
            stats.uses_library_edge_count
        );
        eprintln!("{} Graph build: {}ms", "TIMING".yellow(), graph_ms);
    }

    // Log detailed graph information
    debug!("[GRAPH] Graph statistics:");
    debug!("  - Total nodes: {}", code_graph.graph.node_count());
    debug!("  - Total edges: {}", code_graph.graph.edge_count());
    debug!("  - File nodes: {}", stats.file_count);
    debug!("  - Function nodes: {}", stats.function_count);
    debug!("  - Class nodes: {}", stats.class_count);
    debug!("  - Import edges: {}", stats.import_edge_count);
    debug!("  - Contains edges: {}", stats.contains_edge_count);
    debug!("  - Uses library edges: {}", stats.uses_library_edge_count);
    debug!("  - Calls edges: {}", stats.calls_edge_count);
    debug!("  - External modules: {}", stats.external_module_count);

    // Create IR and log serialization details
    let ir = IntermediateRepresentation::new(all_semantics, code_graph);

    let serialization_start = Instant::now();
    match serde_json::to_string(&ir) {
        Ok(json) => {
            let json_size = json.len();
            let json_size_mb = json_size as f64 / (1024.0 * 1024.0);
            let serialization_ms = serialization_start.elapsed().as_millis();
            debug!("[GRAPH] Serialization details:");
            debug!(
                "  - JSON size: {} bytes ({:.2} MB)",
                json_size, json_size_mb
            );
            debug!("  - Serialization time: {}ms", serialization_ms);
            debug!("  - Semantics count: {}", ir.semantics.len());
        }
        Err(e) => {
            debug!("[GRAPH] Failed to serialize IR: {}", e);
        }
    }

    Ok(IrBuildResult { ir, cache_stats })
}

/// Discover source files in a directory using ignore patterns.
///
/// Test files are automatically excluded — they consistently produce false
/// positives when unfault traces cross-file references back to test callers.
fn discover_source_files(workspace_path: &Path) -> Result<Vec<PathBuf>> {
    use ignore::WalkBuilder;

    let mut files = Vec::new();

    let walker = WalkBuilder::new(workspace_path)
        .hidden(true)
        .git_ignore(true)
        .git_exclude(true)
        .build();

    for entry in walker {
        let entry = entry?;
        let path = entry.path();

        if path.is_file() && detect_language(path).is_some() && !is_test_file(path) {
            files.push(path.to_path_buf());
        }
    }

    Ok(files)
}

/// Returns `true` if `path` looks like a test file for any supported language.
///
/// Patterns covered per language:
/// - **Python**: `test_*.py`, `*_test.py`, `*_tests.py`, `conftest.py`,
///   paths containing `/tests/` or `/test/`
/// - **Go**: `*_test.go`, paths containing `/testdata/` or `/test/`
/// - **TypeScript / JavaScript**: `*.test.{ts,tsx,js}`, `*.spec.{ts,tsx,js}`,
///   paths containing `/__tests__/`, `/test/`, or `/tests/`
/// - **Rust**: `test_*.rs`, `*_test.rs`, paths containing `/tests/`
///   (in-file `#[cfg(test)]` blocks are already suppressed by the analysis layer)
pub fn is_test_file(path: &Path) -> bool {
    // Build a forward-slash path string for portable pattern matching.
    let path_str = path.to_string_lossy();
    let path_norm = path_str.replace('\\', "/");

    let filename = path_norm.rsplit('/').next().unwrap_or(&path_norm);
    let filename_lower = filename.to_lowercase();

    // ── Python ──────────────────────────────────────────────────────────────
    if filename_lower.ends_with(".py") {
        return filename_lower.starts_with("test_")
            || filename_lower.ends_with("_test.py")
            || filename_lower.ends_with("_tests.py")
            || filename_lower == "conftest.py"
            || path_norm.contains("/tests/")
            || path_norm.contains("/test/");
    }

    // ── Go ──────────────────────────────────────────────────────────────────
    if filename_lower.ends_with(".go") {
        return filename_lower.ends_with("_test.go")
            || path_norm.contains("/testdata/")
            || path_norm.contains("/test/");
    }

    // ── TypeScript / JavaScript ──────────────────────────────────────────────
    if filename_lower.ends_with(".ts")
        || filename_lower.ends_with(".tsx")
        || filename_lower.ends_with(".js")
        || filename_lower.ends_with(".jsx")
        || filename_lower.ends_with(".mjs")
        || filename_lower.ends_with(".cjs")
    {
        return filename_lower.contains(".test.")
            || filename_lower.contains(".spec.")
            || path_norm.contains("/__tests__/")
            || path_norm.contains("/test/")
            || path_norm.contains("/tests/");
    }

    // ── Rust ────────────────────────────────────────────────────────────────
    if filename_lower.ends_with(".rs") {
        return filename_lower.starts_with("test_")
            || filename_lower.ends_with("_test.rs")
            || path_norm.contains("/tests/");
    }

    false
}

/// Detect language from file extension.
fn detect_language(path: &Path) -> Option<Language> {
    let ext = path.extension()?.to_str()?;
    match ext {
        "py" => Some(Language::Python),
        "go" => Some(Language::Go),
        "rs" => Some(Language::Rust),
        "ts" | "tsx" | "js" | "jsx" => Some(Language::Typescript),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    // ── is_test_file ─────────────────────────────────────────────────────────

    #[test]
    fn is_test_file_python_prefix() {
        assert!(is_test_file(Path::new("test_auth.py")));
        assert!(is_test_file(Path::new("Test_Auth.py"))); // case-insensitive
        assert!(is_test_file(Path::new("src/test_router.py")));
    }

    #[test]
    fn is_test_file_python_suffix() {
        assert!(is_test_file(Path::new("auth_test.py")));
        assert!(is_test_file(Path::new("auth_tests.py")));
    }

    #[test]
    fn is_test_file_python_conftest() {
        assert!(is_test_file(Path::new("conftest.py")));
        assert!(is_test_file(Path::new("src/conftest.py")));
    }

    #[test]
    fn is_test_file_python_test_dir() {
        assert!(is_test_file(Path::new("project/tests/auth.py")));
        assert!(is_test_file(Path::new("src/test/helper.py")));
    }

    #[test]
    fn is_test_file_python_regular_files_pass() {
        assert!(!is_test_file(Path::new("router.py")));
        assert!(!is_test_file(Path::new("testing_utils.py"))); // 'testing_' is not 'test_'
        assert!(!is_test_file(Path::new("src/auth.py")));
    }

    #[test]
    fn is_test_file_go_suffix() {
        assert!(is_test_file(Path::new("handler_test.go")));
        assert!(is_test_file(Path::new("src/handler_test.go")));
    }

    #[test]
    fn is_test_file_go_testdata_dir() {
        assert!(is_test_file(Path::new("project/testdata/fixture.go")));
    }

    #[test]
    fn is_test_file_go_test_dir() {
        assert!(is_test_file(Path::new("project/test/helper.go")));
    }

    #[test]
    fn is_test_file_go_regular_files_pass() {
        assert!(!is_test_file(Path::new("handler.go")));
        assert!(!is_test_file(Path::new("testing.go"))); // common stdlib import file
    }

    #[test]
    fn is_test_file_ts_dot_test() {
        assert!(is_test_file(Path::new("handler.test.ts")));
        assert!(is_test_file(Path::new("handler.test.tsx")));
        assert!(is_test_file(Path::new("handler.test.js")));
    }

    #[test]
    fn is_test_file_ts_dot_spec() {
        assert!(is_test_file(Path::new("handler.spec.ts")));
        assert!(is_test_file(Path::new("handler.spec.tsx")));
        assert!(is_test_file(Path::new("handler.spec.js")));
    }

    #[test]
    fn is_test_file_ts_tests_dirs() {
        assert!(is_test_file(Path::new("src/__tests__/auth.ts")));
        assert!(is_test_file(Path::new("src/test/auth.ts")));
        assert!(is_test_file(Path::new("src/tests/auth.ts")));
    }

    #[test]
    fn is_test_file_ts_regular_files_pass() {
        assert!(!is_test_file(Path::new("handler.ts")));
        assert!(!is_test_file(Path::new("src/router.tsx")));
        // "testing-utils.ts" does NOT contain ".test." or ".spec."
        assert!(!is_test_file(Path::new("testing-utils.ts")));
    }

    #[test]
    fn is_test_file_rust_prefix() {
        assert!(is_test_file(Path::new("test_auth.rs")));
    }

    #[test]
    fn is_test_file_rust_suffix() {
        assert!(is_test_file(Path::new("auth_test.rs")));
    }

    #[test]
    fn is_test_file_rust_tests_dir() {
        // Integration tests live in <crate>/tests/
        assert!(is_test_file(Path::new("mylib/tests/integration.rs")));
    }

    #[test]
    fn is_test_file_rust_regular_files_pass() {
        assert!(!is_test_file(Path::new("handler.rs")));
        assert!(!is_test_file(Path::new("src/router.rs")));
        // src/lib.rs contains #[cfg(test)] but is not a test file
        assert!(!is_test_file(Path::new("src/lib.rs")));
    }

    #[test]
    fn is_test_file_non_source_files_pass() {
        assert!(!is_test_file(Path::new("test_config.json")));
        assert!(!is_test_file(Path::new("README.md")));
    }

    // ── detect_language ──────────────────────────────────────────────────────

    #[test]
    fn test_detect_language() {
        assert_eq!(
            detect_language(Path::new("test.py")),
            Some(Language::Python)
        );
        assert_eq!(detect_language(Path::new("test.go")), Some(Language::Go));
        assert_eq!(detect_language(Path::new("test.rs")), Some(Language::Rust));
        assert_eq!(
            detect_language(Path::new("test.ts")),
            Some(Language::Typescript)
        );
        assert_eq!(
            detect_language(Path::new("test.tsx")),
            Some(Language::Typescript)
        );
        assert_eq!(detect_language(Path::new("test.txt")), None);
    }

    #[test]
    fn test_build_ir_empty_dir() {
        let temp_dir = TempDir::new().unwrap();
        let result = build_ir(temp_dir.path(), None, false);
        assert!(result.is_ok());
        let ir = result.unwrap();
        assert_eq!(ir.file_count(), 0);
    }

    #[test]
    fn test_build_ir_python_file() {
        let temp_dir = TempDir::new().unwrap();
        let py_file = temp_dir.path().join("test.py");
        fs::write(
            &py_file,
            r#"
def hello():
    pass

def caller():
    hello()
"#,
        )
        .unwrap();

        let result = build_ir(temp_dir.path(), None, false);
        assert!(result.is_ok());
        let ir = result.unwrap();

        // Should have 1 file
        assert_eq!(ir.file_count(), 1);

        // Should have functions in the graph
        let stats = ir.graph.stats();
        assert!(stats.function_count >= 2);
    }

    #[test]
    fn test_build_ir_with_imports() {
        let temp_dir = TempDir::new().unwrap();

        // Create a module structure
        let src_dir = temp_dir.path().join("src");
        fs::create_dir(&src_dir).unwrap();

        fs::write(
            src_dir.join("main.py"),
            r#"
from src.utils import helper

def main():
    helper()
"#,
        )
        .unwrap();

        fs::write(
            src_dir.join("utils.py"),
            r#"
def helper():
    print("hello")
"#,
        )
        .unwrap();

        let result = build_ir(temp_dir.path(), None, false);
        assert!(result.is_ok());
        let ir = result.unwrap();

        // Should have 2 files
        assert_eq!(ir.file_count(), 2);

        // Should have functions
        let stats = ir.graph.stats();
        assert!(stats.function_count >= 2);
    }

    #[test]
    fn test_ir_serialization() {
        let temp_dir = TempDir::new().unwrap();
        let py_file = temp_dir.path().join("test.py");
        fs::write(
            &py_file,
            r#"
import requests

def fetch_data():
    return requests.get("http://example.com")
"#,
        )
        .unwrap();

        let result = build_ir(temp_dir.path(), None, false);
        assert!(result.is_ok());
        let ir = result.unwrap();

        // Should be serializable to JSON
        let json = serde_json::to_string(&ir);
        assert!(json.is_ok());
        let json_str = json.unwrap();

        // JSON should contain expected content
        assert!(json_str.contains("fetch_data"));
        assert!(json_str.contains("requests"));
    }

    #[test]
    fn test_ir_deserialization_and_rebuild() {
        let temp_dir = TempDir::new().unwrap();
        let py_file = temp_dir.path().join("test.py");
        fs::write(
            &py_file,
            r#"
import requests

def fetch_data():
    return requests.get("http://example.com")
"#,
        )
        .unwrap();

        let ir = build_ir(temp_dir.path(), None, false).unwrap();

        // Serialize
        let json = serde_json::to_string(&ir).unwrap();

        // Deserialize
        let mut ir_restored: IntermediateRepresentation = serde_json::from_str(&json).unwrap();

        // Rebuild indexes (required after deserialization)
        ir_restored.rebuild_graph_indexes();

        // Stats should match
        let stats_before = ir.graph.stats();
        let stats_after = ir_restored.graph.stats();
        assert_eq!(stats_before.file_count, stats_after.file_count);
        assert_eq!(stats_before.function_count, stats_after.function_count);
        assert_eq!(
            stats_before.external_module_count,
            stats_after.external_module_count
        );
    }

    #[test]
    fn test_build_ir_with_specific_files() {
        let temp_dir = TempDir::new().unwrap();

        // Create multiple files
        let file1 = temp_dir.path().join("include.py");
        let file2 = temp_dir.path().join("exclude.py");
        fs::write(&file1, "def included(): pass").unwrap();
        fs::write(&file2, "def excluded(): pass").unwrap();

        // Build IR with only specific files
        let files = vec![file1];
        let ir = build_ir(temp_dir.path(), Some(&files), false).unwrap();

        // Should only have 1 file
        assert_eq!(ir.file_count(), 1);

        // Check that only the included function is in the graph
        let stats = ir.graph.stats();
        assert_eq!(stats.function_count, 1);
    }

    #[test]
    fn test_build_ir_go_file() {
        let temp_dir = TempDir::new().unwrap();
        let go_file = temp_dir.path().join("main.go");
        fs::write(
            &go_file,
            r#"
package main

import "fmt"

func main() {
    fmt.Println("Hello")
}
"#,
        )
        .unwrap();

        let result = build_ir(temp_dir.path(), None, false);
        assert!(result.is_ok());
        let ir = result.unwrap();

        assert_eq!(ir.file_count(), 1);
        let stats = ir.graph.stats();
        assert!(stats.function_count >= 1);
    }

    #[test]
    fn test_build_ir_typescript_file() {
        let temp_dir = TempDir::new().unwrap();
        let ts_file = temp_dir.path().join("app.ts");
        fs::write(
            &ts_file,
            r#"
import express from 'express';

function handler(req: any, res: any) {
    res.json({ ok: true });
}
"#,
        )
        .unwrap();

        let result = build_ir(temp_dir.path(), None, false);
        assert!(result.is_ok());
        let ir = result.unwrap();

        assert_eq!(ir.file_count(), 1);
        let stats = ir.graph.stats();
        assert!(stats.function_count >= 1);
    }

    #[test]
    fn test_build_ir_rust_file() {
        let temp_dir = TempDir::new().unwrap();
        let rs_file = temp_dir.path().join("lib.rs");
        fs::write(
            &rs_file,
            r#"
use std::io;

fn process_data() -> io::Result<()> {
    Ok(())
}
"#,
        )
        .unwrap();

        let result = build_ir(temp_dir.path(), None, false);
        assert!(result.is_ok());
        let ir = result.unwrap();

        // Rust semantics builder currently uses from_parsed() which creates an empty structure.
        // The full Rust analyzer populates functions separately. For now, we just verify
        // that the file is parsed without error and included in the IR.
        assert_eq!(ir.file_count(), 1);
        let stats = ir.graph.stats();
        assert_eq!(stats.file_count, 1);
        // Note: stats.function_count may be 0 until Rust semantics extraction is fully implemented
    }
}