fallow-engine 3.34.0

Typed analysis engine facade for fallow consumers
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
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
//! Module graph contracts owned by the engine boundary.

#![allow(
    clippy::implicit_hasher,
    reason = "engine graph helpers use FxHashSet changed-file sets consistently with the rest of fallow"
)]

use std::path::{Path, PathBuf};

use fallow_output::TestAdjacency;
use fallow_types::discover::FileId;
use rustc_hash::{FxHashMap, FxHashSet};

use fallow_graph::graph::{
    CoordinationGapPaths as GraphCoordinationGapPaths,
    FocusFileFactsPaths as GraphFocusFileFactsPaths, ImpactClosurePaths as GraphImpactClosurePaths,
    ModuleGraph, PartitionOrderPaths as GraphPartitionOrderPaths,
    ReviewUnitPaths as GraphReviewUnitPaths,
};
use fallow_graph::graph::{
    DirectImporterSummary as GraphDirectImporterSummary,
    ImportedSymbolSummary as GraphImportedSymbolSummary,
};
use fallow_graph::graph::{
    ExportTestReach as GraphExportTestReach, ExportTestReachResult as GraphExportTestReachResult,
    TestMaskingState as GraphTestMaskingState,
};

/// Engine-owned retained graph handle.
///
/// Downstream crates can request stable graph facts through engine helpers
/// without depending on `fallow-graph` node internals.
#[derive(Debug)]
pub struct RetainedModuleGraph {
    inner: ModuleGraph,
}

impl RetainedModuleGraph {
    /// Wrap a freshly built module graph for engine result contracts.
    #[must_use]
    const fn new(inner: ModuleGraph) -> Self {
        Self { inner }
    }

    pub(crate) const fn as_graph(&self) -> &ModuleGraph {
        &self.inner
    }

    /// Borrow the shared static test-coverage view for health analysis.
    pub(crate) const fn static_test_coverage(&self) -> StaticTestCoverage<'_> {
        StaticTestCoverage::new(&self.inner)
    }

    /// Number of modules in the retained graph.
    #[must_use]
    pub fn module_count(&self) -> usize {
        self.inner.module_count()
    }

    /// Number of edges in the retained graph.
    #[must_use]
    pub fn edge_count(&self) -> usize {
        self.inner.edge_count()
    }

    /// Build public export keys for a precomputed public-entry set.
    #[must_use]
    pub(crate) fn public_export_keys(
        &self,
        public_entries: &FxHashSet<FileId>,
        root: &Path,
    ) -> FxHashSet<String> {
        self.inner.public_export_keys(public_entries, root)
    }

    /// Count direct importer modules for one file id.
    #[must_use]
    pub fn direct_importer_count(&self, file_id: FileId) -> usize {
        self.inner
            .reverse_deps
            .get(file_id.0 as usize)
            .map_or(0, Vec::len)
    }

    /// Summaries for modules that directly import one file.
    #[must_use]
    pub fn direct_importer_summaries(&self, target: FileId) -> Vec<DirectImporterSummary> {
        self.inner
            .direct_importer_summaries(target)
            .into_iter()
            .map(DirectImporterSummary::from)
            .collect()
    }
}

/// Engine-owned view of root-correlated static test reachability.
///
/// This keeps health consumers on one contract while the graph crate owns the
/// traversal and replacement-mask representation.
#[derive(Clone, Copy)]
pub(crate) struct StaticTestCoverage<'a> {
    graph: &'a ModuleGraph,
}

impl<'a> StaticTestCoverage<'a> {
    pub(crate) const fn new(graph: &'a ModuleGraph) -> Self {
        Self { graph }
    }

    pub(crate) fn covers_file(self, file_id: FileId) -> bool {
        self.graph.is_test_reachable(file_id)
    }

    pub(crate) fn covers_any_reference(self, export: &fallow_graph::graph::ExportSymbol) -> bool {
        self.graph.is_any_test_reference_covered(export)
    }

    /// Whether only tests that mock a module on the reference path reference
    /// `export`. Call it for an export without a covered reference.
    pub(crate) fn is_mocked_only(
        self,
        file_id: FileId,
        export: &fallow_graph::graph::ExportSymbol,
    ) -> bool {
        self.graph.test_masking_state() == GraphTestMaskingState::Active
            && self
                .graph
                .export_test_reach(file_id, export, &|_| false)
                .reach
                == GraphExportTestReach::MockedOnly
    }
}

impl From<ModuleGraph> for RetainedModuleGraph {
    fn from(inner: ModuleGraph) -> Self {
        Self::new(inner)
    }
}

/// Engine-owned importer details for one file that directly imports a target module.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DirectImporterSummary {
    /// File containing the import statement.
    pub source: FileId,
    /// Symbols this importer pulls from the target module.
    pub symbols: Vec<ImportedSymbolSummary>,
}

impl From<GraphDirectImporterSummary> for DirectImporterSummary {
    fn from(summary: GraphDirectImporterSummary) -> Self {
        Self {
            source: summary.source,
            symbols: summary.symbols.into_iter().map(Into::into).collect(),
        }
    }
}

/// Engine-owned symbol details for a direct import edge.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImportedSymbolSummary {
    /// Exported name as declared by the target module.
    pub imported: String,
    /// Local binding name at the import site.
    pub local: String,
    /// True for type-only imports, which do not count as value usage.
    pub type_only: bool,
}

impl From<GraphImportedSymbolSummary> for ImportedSymbolSummary {
    fn from(symbol: GraphImportedSymbolSummary) -> Self {
        Self {
            imported: symbol.imported,
            local: symbol.local,
            type_only: symbol.type_only,
        }
    }
}

/// Engine-owned snapshot of one value export in a module graph.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleValueExport {
    /// File declaring the export.
    pub file_id: FileId,
    /// Exported name.
    pub name: String,
    /// Byte offset where the export declaration starts.
    pub span_start: u32,
    /// True when a test file references this export.
    pub test_referenced: bool,
}

/// Engine-owned impact closure with file ids resolved to paths.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ImpactClosurePaths {
    /// Changed files that are part of the diff under review.
    pub in_diff: Vec<String>,
    /// Files affected through the import closure but absent from the diff.
    pub affected_not_shown: Vec<String>,
    /// Changed contracts whose consumers were not updated in the same diff.
    pub coordination_gap: Vec<CoordinationGapPaths>,
}

impl From<GraphImpactClosurePaths> for ImpactClosurePaths {
    fn from(paths: GraphImpactClosurePaths) -> Self {
        Self {
            in_diff: paths.in_diff,
            affected_not_shown: paths.affected_not_shown,
            coordination_gap: paths
                .coordination_gap
                .into_iter()
                .map(CoordinationGapPaths::from)
                .collect(),
        }
    }
}

/// Engine-owned coordination gap between a changed contract and consumer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CoordinationGapPaths {
    /// Path of the changed file whose contract moved.
    pub changed_file: String,
    /// Path of the consumer that was not updated alongside it.
    pub consumer_file: String,
    /// Symbols the consumer imports from the changed file.
    pub consumed_symbols: Vec<String>,
}

impl From<GraphCoordinationGapPaths> for CoordinationGapPaths {
    fn from(paths: GraphCoordinationGapPaths) -> Self {
        Self {
            changed_file: paths.changed_file,
            consumer_file: paths.consumer_file,
            consumed_symbols: paths.consumed_symbols,
        }
    }
}

/// Engine-owned review partition and dependency-sensible order.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PartitionOrderPaths {
    /// Changed files partitioned into per-module review units.
    pub units: Vec<ReviewUnitPaths>,
    /// File paths in dependency-sensible review order (dependencies first).
    pub order: Vec<String>,
    /// Connected components of the inter-unit dependency graph: units that
    /// share no import edge with any unit outside their slice.
    pub independent_slices: Vec<Vec<String>>,
}

impl From<GraphPartitionOrderPaths> for PartitionOrderPaths {
    fn from(paths: GraphPartitionOrderPaths) -> Self {
        Self {
            units: paths.units.into_iter().map(ReviewUnitPaths::from).collect(),
            order: paths.order,
            independent_slices: paths.independent_slices,
        }
    }
}

/// Engine-owned changed-file review unit.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReviewUnitPaths {
    /// Directory the unit's files share.
    pub module_dir: String,
    /// Changed files grouped into this unit.
    pub files: Vec<String>,
}

impl From<GraphReviewUnitPaths> for ReviewUnitPaths {
    fn from(paths: GraphReviewUnitPaths) -> Self {
        Self {
            module_dir: paths.module_dir,
            files: paths.files,
        }
    }
}

/// Engine-owned focus facts for one changed file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FocusFileFactsPaths {
    /// Path of the changed file the facts describe.
    pub file: String,
    /// Count of distinct files importing this file (blast radius), excluding
    /// the file itself.
    pub fan_in: u32,
    /// Count of distinct files this file imports, excluding the file itself.
    pub fan_out: u32,
    /// True when the file has a dynamic-import edge in either direction, so
    /// its static reachability signal is not complete. Conservative: a file
    /// that MAY be dynamically wired carries the flag.
    pub dynamic_dispatch: bool,
    /// True when the file's reachability runs through re-export indirection,
    /// so direct importer counts understate its reach.
    pub re_export_indirection: bool,
}

impl From<GraphFocusFileFactsPaths> for FocusFileFactsPaths {
    fn from(paths: GraphFocusFileFactsPaths) -> Self {
        Self {
            file: paths.file,
            fan_in: paths.fan_in,
            fan_out: paths.fan_out,
            dynamic_dispatch: paths.dynamic_dispatch,
            re_export_indirection: paths.re_export_indirection,
        }
    }
}

/// Return value exports with test-reference state without exposing graph node
/// internals to downstream crates.
#[must_use]
pub fn module_value_exports(graph: &RetainedModuleGraph) -> Vec<ModuleValueExport> {
    let test_coverage = graph.static_test_coverage();
    let graph = graph.as_graph();

    graph
        .modules
        .iter()
        .flat_map(|node| {
            node.exports
                .iter()
                .filter(|export| !export.is_type_only)
                .map(|export| ModuleValueExport {
                    file_id: node.file_id,
                    name: export.name.to_string(),
                    span_start: export.span.start,
                    test_referenced: test_coverage.covers_any_reference(export),
                })
        })
        .collect()
}

/// Compute a path-resolved impact closure for absolute changed paths.
#[must_use]
pub fn impact_closure_for_changed_paths(
    graph: &RetainedModuleGraph,
    root: &Path,
    changed_files: &FxHashSet<PathBuf>,
) -> Option<ImpactClosurePaths> {
    let graph = graph.as_graph();
    let changed_ids = changed_file_ids(graph, changed_files);
    if changed_ids.is_empty() {
        return None;
    }

    let closure = graph.impact_closure(&changed_ids);
    Some(graph.closure_with_paths(&closure, root).into())
}

/// Compute path-resolved partition order for absolute changed paths.
#[must_use]
pub fn partition_order_for_changed_paths(
    graph: &RetainedModuleGraph,
    root: &Path,
    changed_files: &FxHashSet<PathBuf>,
) -> Option<PartitionOrderPaths> {
    let graph = graph.as_graph();
    let changed_ids = changed_file_ids(graph, changed_files);
    if changed_ids.is_empty() {
        return None;
    }

    let partition = graph.partition_order(&changed_ids);
    Some(graph.partition_order_with_paths(&partition, root).into())
}

/// Compute path-resolved focus graph facts for absolute changed paths.
#[must_use]
pub fn focus_facts_for_changed_paths(
    graph: &RetainedModuleGraph,
    root: &Path,
    changed_files: &FxHashSet<PathBuf>,
) -> Option<Vec<FocusFileFactsPaths>> {
    let graph = graph.as_graph();
    let changed_ids = changed_file_ids(graph, changed_files);
    if changed_ids.is_empty() {
        return None;
    }

    let facts = graph.focus_file_facts(&changed_ids);
    Some(
        graph
            .focus_facts_with_paths(&facts, root)
            .into_iter()
            .map(FocusFileFactsPaths::from)
            .collect(),
    )
}

/// Compute changed-file export line anchors without exposing graph nodes.
#[must_use]
pub fn export_lines_for_changed_paths(
    graph: &RetainedModuleGraph,
    root: &Path,
    changed_files: &FxHashSet<PathBuf>,
) -> Option<FxHashMap<String, Vec<(String, u32)>>> {
    let graph = graph.as_graph();
    let changed_norm = normalized_changed_paths(changed_files);
    let mut map: FxHashMap<String, Vec<(String, u32)>> = FxHashMap::default();
    for module in &graph.modules {
        let abs = normalize_path(&module.path);
        if !changed_norm.contains(&abs) || module.exports.is_empty() {
            continue;
        }
        let Ok(content) = std::fs::read_to_string(&module.path) else {
            continue;
        };
        let offsets = fallow_types::extract::compute_line_offsets(&content);
        let exports: Vec<(String, u32)> = module
            .exports
            .iter()
            .map(|export| {
                let (line, _) =
                    fallow_types::extract::byte_offset_to_line_col(&offsets, export.span.start);
                (export.name.to_string(), line)
            })
            .collect();
        map.insert(relative_key_path(&module.path, root), exports);
    }
    Some(map)
}

/// One consumer that a whole-word text search for an export name does not
/// find, or finds only under another local name.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConsumerSearchGapPaths {
    /// Root-relative path of the consumer, forward-slashed.
    pub consumer_file: String,
    /// The literal-search check for the consumer.
    pub check: crate::search_visibility::SearchCheck,
}

/// Literal-search gaps for one export of a changed file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExportSearchGapPaths {
    /// Root-relative path of the changed file, forward-slashed.
    pub file: String,
    /// The export name, `default` for a default export.
    pub export_name: String,
    /// 1-based line of the export.
    pub line: u32,
    /// 1-based first and last line of the top-level statement that declares
    /// the export.
    pub declaration_lines: (u32, u32),
    /// The `missed` and `renamed` consumers outside the changed files, sorted
    /// by path.
    pub consumers: Vec<ConsumerSearchGapPaths>,
}

/// The names of the exports that `source` declares, parsed as the file at
/// `path`. The names use the format of [`ChangedExport::export_name`].
#[must_use]
pub fn export_names_in_source(path: &Path, source: &str) -> FxHashSet<String> {
    let source = source.strip_prefix('\u{feff}').unwrap_or(source);
    let module = fallow_extract::parse_source_to_module(FileId(0), path, source, 0, false);
    module
        .exports
        .iter()
        .map(|export| export.name.to_string())
        .collect()
}

/// One export of a changed file, with the line facts that the review brief
/// joins on the diff.
///
/// [`changed_exports_for_changed_paths`] reads and parses each changed file
/// once. The literal-search gap pass and the test reach pass share the list.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChangedExport {
    file_id: FileId,
    export_index: usize,
    /// Root-relative path of the changed file, forward-slashed.
    pub file: String,
    /// The export name, `default` for a default export.
    pub export_name: String,
    /// 1-based line of the export.
    pub line: u32,
    /// 1-based first and last line of the top-level statement that declares
    /// the export.
    pub declaration_lines: (u32, u32),
    /// True for a type-only export.
    pub is_type_only: bool,
    /// True when the export is a variable whose initializer is a literal, or
    /// an object or array of literals.
    pub literal_value: bool,
    /// True when a re-export statement (`export { x } from`) makes the export.
    pub is_re_export: bool,
}

/// List the exports of absolute changed paths with their declaration lines.
///
/// Each changed file is read and parsed once. The exports keep graph order
/// within a file, and the files are in `FileId` order. `None` when no module
/// matches a changed path.
#[must_use]
pub fn changed_exports_for_changed_paths(
    graph: &RetainedModuleGraph,
    root: &Path,
    changed_files: &FxHashSet<PathBuf>,
) -> Option<Vec<ChangedExport>> {
    let graph = graph.as_graph();
    let mut changed_ids = changed_file_ids(graph, changed_files);
    if changed_ids.is_empty() {
        return None;
    }
    changed_ids.sort_unstable_by_key(|id| id.0);
    let mut changed_exports = Vec::new();
    for file_id in changed_ids {
        let Some(module) = graph.modules.get(file_id.0 as usize) else {
            continue;
        };
        if module.exports.is_empty() {
            continue;
        }
        let Ok(source) = std::fs::read_to_string(&module.path) else {
            continue;
        };
        changed_exports.extend(module_exports(graph, root, file_id, &source));
    }
    Some(changed_exports)
}

/// List the exports of one module with their declaration lines. `source` is
/// the current text of the module. The exports keep graph order.
pub(crate) fn module_exports(
    graph: &ModuleGraph,
    root: &Path,
    file_id: FileId,
    source: &str,
) -> Vec<ChangedExport> {
    let Some(module) = graph.modules.get(file_id.0 as usize) else {
        return Vec::new();
    };
    if module.exports.is_empty() {
        return Vec::new();
    }
    let offsets = fallow_types::extract::compute_line_offsets(source);
    let starts: Vec<u32> = module
        .exports
        .iter()
        .map(|export| export.span.start)
        .collect();
    let declarations = crate::search_visibility::export_declarations(&module.path, source, &starts);
    let file = relative_key_path(&module.path, root);
    module
        .exports
        .iter()
        .enumerate()
        .map(|(export_index, export)| {
            let line =
                fallow_types::extract::byte_offset_to_line_col(&offsets, export.span.start).0;
            let declaration = declarations.get(&export.span.start);
            ChangedExport {
                file_id,
                export_index,
                file: file.clone(),
                export_name: export.name.to_string(),
                line,
                declaration_lines: declaration.map_or((line, line), |found| found.lines),
                is_type_only: export.is_type_only,
                literal_value: declaration.is_some_and(|found| found.literal_value),
                is_re_export: declaration.is_some_and(|found| found.is_re_export),
            }
        })
        .collect()
}

/// Compute the literal-search gaps of the changed exports.
///
/// For each export of a changed file, the consumers outside the changed set
/// are checked against a whole-word text search for the export name. Only
/// exports with at least one `missed` or `renamed` consumer are returned. The
/// consumer sources are read on demand.
#[must_use]
pub fn search_gaps_for_changed_paths(
    graph: &RetainedModuleGraph,
    root: &Path,
    changed_files: &FxHashSet<PathBuf>,
    changed_exports: &[ChangedExport],
) -> Option<Vec<ExportSearchGapPaths>> {
    use crate::search_visibility::{SearchTarget, SourceCache};
    use fallow_graph::graph::ExportNamespace;
    use fallow_types::trace::SearchVisibility;

    let graph = graph.as_graph();
    let changed_set: FxHashSet<FileId> =
        changed_file_ids(graph, changed_files).into_iter().collect();
    if changed_set.is_empty() {
        return None;
    }
    let mut sources = SourceCache::new(graph);
    let mut gaps = Vec::new();
    for changed in changed_exports {
        let Some(export) = graph
            .modules
            .get(changed.file_id.0 as usize)
            .and_then(|module| module.exports.get(changed.export_index))
        else {
            continue;
        };
        let mut seen = FxHashSet::default();
        let candidates: Vec<_> = export
            .references
            .iter()
            .filter(|reference| {
                !changed_set.contains(&reference.from_file)
                    && graph
                        .modules
                        .get(reference.from_file.0 as usize)
                        .is_some_and(fallow_graph::graph::ModuleNode::is_reachable)
                    && seen.insert(reference.from_file)
            })
            .collect();
        if candidates.is_empty() {
            continue;
        }
        let namespace = if export.is_type_only {
            ExportNamespace::Type
        } else {
            ExportNamespace::Value
        };
        let target = SearchTarget::new(
            graph,
            &mut sources,
            changed.file_id,
            &changed.export_name,
            namespace,
        );
        let mut consumers: Vec<ConsumerSearchGapPaths> = candidates
            .into_iter()
            .filter_map(|reference| {
                let check = target.check(graph, &mut sources, reference)?;
                if check.visibility == SearchVisibility::Found {
                    return None;
                }
                let consumer = graph.modules.get(reference.from_file.0 as usize)?;
                Some(ConsumerSearchGapPaths {
                    consumer_file: relative_key_path(&consumer.path, root),
                    check,
                })
            })
            .collect();
        if consumers.is_empty() {
            continue;
        }
        consumers.sort_by(|a, b| a.consumer_file.cmp(&b.consumer_file));
        gaps.push(ExportSearchGapPaths {
            file: changed.file.clone(),
            export_name: changed.export_name.clone(),
            line: changed.line,
            declaration_lines: changed.declaration_lines,
            consumers,
        });
    }
    Some(gaps)
}

/// Package names whose presence marks an end-to-end test runner. Such tests
/// drive a running app and do not import the source files.
const E2E_RUNNER_PACKAGES: &[&str] = &["@playwright/test", "playwright", "cypress"];

/// How tests reach one export.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ExportTestReach {
    /// A test file references the export, and a test root covers it.
    Direct,
    /// A covered reference exists, but no test file names the export.
    Transitive,
    /// No reference is covered, and a mock replaces a module on a reference
    /// path.
    MockedOnly,
    /// A test root reaches the declaring file, but no reference is covered.
    None,
    /// No test root reaches the declaring file.
    NoneFile,
}

impl From<GraphExportTestReach> for ExportTestReach {
    fn from(reach: GraphExportTestReach) -> Self {
        match reach {
            GraphExportTestReach::Direct => Self::Direct,
            GraphExportTestReach::Transitive => Self::Transitive,
            GraphExportTestReach::MockedOnly => Self::MockedOnly,
            GraphExportTestReach::None => Self::None,
            GraphExportTestReach::NoneFile => Self::NoneFile,
        }
    }
}

/// Why replacement masking is or is not active for test reachability.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TestMaskingState {
    /// At least one test root replaces a module with a closed factory mock.
    Active,
    /// No test root declares a closed factory mock.
    NoMocks,
    /// Too many distinct mock sets, so masking is off.
    Capped,
}

impl From<GraphTestMaskingState> for TestMaskingState {
    fn from(state: GraphTestMaskingState) -> Self {
        match state {
            GraphTestMaskingState::Active => Self::Active,
            GraphTestMaskingState::NoMocks => Self::NoMocks,
            GraphTestMaskingState::Capped => Self::Capped,
        }
    }
}

/// How tests reach one changed value export.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExportTestReachPaths {
    /// Root-relative path of the changed file, forward-slashed.
    pub file: String,
    /// The export name, `default` for a default export.
    pub export_name: String,
    /// 1-based line of the export.
    pub line: u32,
    /// 1-based first and last line of the declaring statement.
    pub declaration_lines: (u32, u32),
    /// True when the export is a variable with a literal initializer.
    pub literal_value: bool,
    /// The reach class.
    pub reach: ExportTestReach,
    /// Test files that name the export, sorted: the covered test references
    /// for `Direct`, the masked test references for `MockedOnly`.
    pub tests: Vec<String>,
    /// For `MockedOnly`: the masked modules on the reference paths, sorted.
    pub mocked_targets: Vec<String>,
}

/// Test reach of the changed value exports of a diff.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TestReachPaths {
    /// False when the project has no test entry points. Every export is then
    /// `NoneFile`.
    pub has_tests: bool,
    /// Whether test reachability applies replacement masks.
    pub masking: TestMaskingState,
    /// True when the project imports an end-to-end test runner package.
    pub has_e2e_runner: bool,
    /// One row per changed value export of a changed source file.
    pub exports: Vec<ExportTestReachPaths>,
}

/// Classify how tests reach the value exports in `changed_exports`.
///
/// Type-only exports, re-exports, exports of test files, of declaration files
/// and of stylesheets are skipped. `is_test_path` classifies a root-relative,
/// forward-slashed path; a test entry point always counts as a test file.
/// The rows keep the order of `changed_exports`. `None` when the list holds no
/// such export.
#[must_use]
pub fn test_reach_for_changed_paths(
    graph: &RetainedModuleGraph,
    root: &Path,
    changed_exports: &[ChangedExport],
    is_test_path: &dyn Fn(&str) -> bool,
) -> Option<TestReachPaths> {
    let graph = graph.as_graph();
    let mut test_files: FxHashMap<FileId, bool> = FxHashMap::default();
    let mut is_test_file = |file_id: FileId| -> bool {
        *test_files.entry(file_id).or_insert_with(|| {
            graph.test_entry_points.contains(&file_id)
                || graph
                    .modules
                    .get(file_id.0 as usize)
                    .is_some_and(|module| is_test_path(&relative_key_path(&module.path, root)))
        })
    };
    let has_tests = !graph.test_entry_points.is_empty();
    let exports: Vec<ExportTestReachPaths> = changed_exports
        .iter()
        .filter_map(|changed| classify_export_test_reach(graph, root, changed, &mut is_test_file))
        .collect();
    if exports.is_empty() {
        return None;
    }
    let has_e2e_runner = E2E_RUNNER_PACKAGES.iter().any(|name| {
        graph.package_usage.contains_key(*name) || graph.type_only_package_usage.contains_key(*name)
    });
    Some(TestReachPaths {
        has_tests,
        masking: graph.test_masking_state().into(),
        has_e2e_runner,
        exports,
    })
}

/// Classify how tests reach one export. `None` for a type-only export, a
/// re-export, an export of a test file, of a declaration file or of a
/// stylesheet. `is_test_file` decides which referencing files are tests. In a
/// project with no test entry points, the class is `NoneFile`.
pub(crate) fn classify_export_test_reach(
    graph: &ModuleGraph,
    root: &Path,
    changed: &ChangedExport,
    is_test_file: &mut dyn FnMut(FileId) -> bool,
) -> Option<ExportTestReachPaths> {
    if changed.is_type_only || changed.is_re_export {
        return None;
    }
    let module = graph.modules.get(changed.file_id.0 as usize)?;
    let export = module.exports.get(changed.export_index)?;
    if !is_reach_subject(&module.path) || is_test_file(changed.file_id) {
        return None;
    }
    let result = if graph.test_entry_points.is_empty() {
        GraphExportTestReachResult {
            reach: GraphExportTestReach::NoneFile,
            tests: Vec::new(),
            mocked_targets: Vec::new(),
        }
    } else {
        let test_flags: FxHashMap<FileId, bool> = export
            .references
            .iter()
            .map(|reference| (reference.from_file, is_test_file(reference.from_file)))
            .collect();
        graph.export_test_reach(changed.file_id, export, &|file_id| {
            test_flags.get(&file_id).copied().unwrap_or(false)
        })
    };
    let paths_of = |files: &[FileId]| -> Vec<String> {
        let mut paths: Vec<String> = files
            .iter()
            .filter_map(|file_id| graph.modules.get(file_id.0 as usize))
            .map(|node| relative_key_path(&node.path, root))
            .collect();
        paths.sort_unstable();
        paths
    };
    Some(ExportTestReachPaths {
        file: changed.file.clone(),
        export_name: changed.export_name.clone(),
        line: changed.line,
        declaration_lines: changed.declaration_lines,
        literal_value: changed.literal_value,
        reach: result.reach.into(),
        tests: paths_of(&result.tests),
        mocked_targets: paths_of(&result.mocked_targets),
    })
}

/// Whether test reach applies to a file: runtime source code, not a
/// declaration file or a stylesheet.
fn is_reach_subject(path: &Path) -> bool {
    if fallow_graph::graph::is_declaration_file_path(path) {
        return false;
    }
    !path
        .extension()
        .and_then(|extension| extension.to_str())
        .is_some_and(|extension| matches!(extension, "css" | "scss" | "less" | "sass"))
}

/// Compute direct non-diff internal consumer counts for absolute changed paths.
#[must_use]
pub fn internal_consumers_for_changed_paths(
    graph: &RetainedModuleGraph,
    root: &Path,
    changed_files: &FxHashSet<PathBuf>,
) -> Option<FxHashMap<String, u64>> {
    let graph = graph.as_graph();
    let changed_norm = normalized_changed_paths(changed_files);
    let id_to_norm: FxHashMap<FileId, String> = graph
        .modules
        .iter()
        .map(|module| (module.file_id, normalize_path(&module.path)))
        .collect();

    let mut map: FxHashMap<String, u64> = FxHashMap::default();
    for module in &graph.modules {
        let abs = normalize_path(&module.path);
        if !changed_norm.contains(&abs) {
            continue;
        }
        let count = graph
            .importers_of(module.file_id)
            .iter()
            .filter(|imp| {
                id_to_norm
                    .get(imp)
                    .is_none_or(|p| !changed_norm.contains(p))
            })
            .count() as u64;
        map.insert(relative_key_path(&module.path, root), count);
    }
    Some(map)
}

/// Compute per-changed-file test adjacency for the review direction: whether a
/// test file imports the changed module directly, and whether one of those tests
/// is itself in the changed set. `is_test_path` classifies a root-relative,
/// forward-slashed path; the caller owns that definition so the brief's
/// weakening and direction surfaces agree on it. Changed test files get no
/// entry of their own. `None` when no module matches a changed path.
#[must_use]
pub fn test_adjacency_for_changed_paths(
    graph: &RetainedModuleGraph,
    root: &Path,
    changed_files: &FxHashSet<PathBuf>,
    is_test_path: &dyn Fn(&str) -> bool,
) -> Option<FxHashMap<String, TestAdjacency>> {
    let graph = graph.as_graph();
    // A project with no test files at all gets no adjacency claims: "no direct
    // test" on every unit would say nothing about the change.
    if !graph
        .modules
        .iter()
        .any(|module| is_test_path(&relative_key_path(&module.path, root)))
    {
        return None;
    }
    let changed_norm = normalized_changed_paths(changed_files);
    let mut map: FxHashMap<String, TestAdjacency> = FxHashMap::default();
    for module in &graph.modules {
        if !changed_norm.contains(&normalize_path(&module.path)) {
            continue;
        }
        let rel = relative_key_path(&module.path, root);
        if is_test_path(&rel) {
            continue;
        }
        let mut adjacency = TestAdjacency::None;
        for importer in graph.importers_of(module.file_id) {
            let Some(node) = graph.modules.get(importer.0 as usize) else {
                continue;
            };
            if !is_test_path(&relative_key_path(&node.path, root)) {
                continue;
            }
            if changed_norm.contains(&normalize_path(&node.path)) {
                adjacency = TestAdjacency::Changed;
                break;
            }
            adjacency = TestAdjacency::Untouched;
        }
        map.insert(rel, adjacency);
    }
    if map.is_empty() {
        return None;
    }
    Some(map)
}

/// In-repo importer counts for one third-party package.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PackageImporters {
    /// Modules that import the package (value imports only), id-sorted. Kept
    /// as ids rather than a count so a decision that batches several packages
    /// can take the union instead of double-counting a module that imports
    /// more than one of them.
    pub importers: Vec<FileId>,
    /// The subset of `importers` outside the changed set, id-sorted.
    pub out_of_diff: Vec<FileId>,
}

/// Compute per-package in-repo importer counts for every third-party package
/// the graph saw an import of, value or type-only, split by whether the
/// importer is in the changed set. Type-only importers count because a major
/// bump of a types package is a compile-wide change even when no value
/// crosses the boundary. The dependency decision arm reads these so a new or
/// bumped `package.json` entry carries the modules it actually reaches. `None`
/// when the graph recorded no package usage.
#[must_use]
pub fn package_importers_for_changed_paths(
    graph: &RetainedModuleGraph,
    changed_files: &FxHashSet<PathBuf>,
) -> Option<FxHashMap<String, PackageImporters>> {
    let graph = graph.as_graph();
    if graph.package_usage.is_empty() && graph.type_only_package_usage.is_empty() {
        return None;
    }
    let changed_norm = normalized_changed_paths(changed_files);
    let id_to_norm: FxHashMap<FileId, String> = graph
        .modules
        .iter()
        .map(|module| (module.file_id, normalize_path(&module.path)))
        .collect();
    let mut usage: FxHashMap<&str, Vec<FileId>> = FxHashMap::default();
    for (package, files) in graph
        .package_usage
        .iter()
        .chain(graph.type_only_package_usage.iter())
    {
        usage
            .entry(package.as_str())
            .or_default()
            .extend(files.iter().copied());
    }
    let map = usage
        .into_iter()
        .map(|(package, files)| {
            let mut importers: Vec<FileId> = files;
            importers.sort_unstable_by_key(|id| id.0);
            importers.dedup();
            let out_of_diff: Vec<FileId> = importers
                .iter()
                .copied()
                .filter(|id| id_to_norm.get(id).is_none_or(|p| !changed_norm.contains(p)))
                .collect();
            (
                package.to_string(),
                PackageImporters {
                    importers,
                    out_of_diff,
                },
            )
        })
        .collect();
    Some(map)
}

fn changed_file_ids(graph: &ModuleGraph, changed_files: &FxHashSet<PathBuf>) -> Vec<FileId> {
    let path_to_id: FxHashMap<String, FileId> = graph
        .modules
        .iter()
        .map(|module| (normalize_path(&module.path), module.file_id))
        .collect();

    changed_files
        .iter()
        .filter_map(|path| path_to_id.get(&normalize_path(path)).copied())
        .collect()
}

fn normalized_changed_paths(changed_files: &FxHashSet<PathBuf>) -> FxHashSet<String> {
    changed_files
        .iter()
        .map(|path| normalize_path(path))
        .collect()
}

fn normalize_path(path: &Path) -> String {
    path.to_string_lossy().replace('\\', "/")
}

/// Canonical path form used inside audit attribution keys: root-relative with
/// forward slashes on every platform. Paths outside `root` keep their
/// simplified absolute form so keys stay unique.
///
/// The engine keys its per-module maps with this function, and
/// `fallow-api` builds its lookup keys with the same function, so both sides
/// of the join always agree.
pub fn relative_key_path(path: &Path, root: &Path) -> String {
    let simple_path = dunce::simplified(path);
    let simple_root = dunce::simplified(root);
    simple_path
        .strip_prefix(simple_root)
        .unwrap_or(simple_path)
        .to_string_lossy()
        .replace('\\', "/")
}

#[cfg(test)]
mod tests {
    use super::{RetainedModuleGraph, module_value_exports};
    use fallow_graph::graph::ModuleGraph;
    use fallow_graph::resolve::{
        ResolveResult, ResolvedImport, ResolvedModule, ResolvedReplacedModuleTarget,
    };
    use fallow_types::discover::{DiscoveredFile, EntryPoint, EntryPointSource, FileId};
    use fallow_types::extract::{ExportInfo, ExportName, ImportInfo, ImportedName, VisibilityTag};
    use std::path::PathBuf;

    fn import(target: FileId, imported_name: ImportedName) -> ResolvedImport {
        import_with_mechanism(target, imported_name, false)
    }

    fn import_with_mechanism(
        target: FileId,
        imported_name: ImportedName,
        commonjs: bool,
    ) -> ResolvedImport {
        ResolvedImport {
            info: ImportInfo {
                source: "./target".to_string(),
                imported_name,
                local_name: "target".to_string(),
                is_type_only: false,
                is_type_only_star: false,
                from_style: false,
                span: oxc_span::Span::new(0, 10),
                source_span: oxc_span::Span::default(),
            },
            target: if commonjs {
                ResolveResult::CommonJsInternalModule(target)
            } else {
                ResolveResult::InternalModule(target)
            },
        }
    }

    fn value_export(name: &str, span_start: u32) -> ExportInfo {
        ExportInfo {
            name: ExportName::Named(name.to_string()),
            local_name: Some(name.to_string()),
            is_type_only: false,
            visibility: VisibilityTag::None,
            expected_unused_reason: None,
            span: oxc_span::Span::new(span_start, span_start + 10),
            members: Vec::new(),
            is_side_effect_used: false,
            super_class: None,
            deprecated: false,
            deprecated_reason: None,
        }
    }

    fn mixed_root_graph(unmasked_root_imports_export: bool) -> RetainedModuleGraph {
        let files: Vec<_> = (0..3)
            .map(|id| DiscoveredFile {
                id: FileId(id),
                path: PathBuf::from(format!("/project/file{id}.ts")),
                size_bytes: 1,
            })
            .collect();
        let modules = vec![
            ResolvedModule {
                file_id: FileId(0),
                path: files[0].path.clone(),
                resolved_imports: vec![import(
                    FileId(2),
                    ImportedName::Named("target".to_string()),
                )],
                ..ResolvedModule::default()
            },
            ResolvedModule {
                file_id: FileId(1),
                path: files[1].path.clone(),
                resolved_imports: vec![import(
                    FileId(2),
                    if unmasked_root_imports_export {
                        ImportedName::Named("target".to_string())
                    } else {
                        ImportedName::SideEffect
                    },
                )],
                ..ResolvedModule::default()
            },
            ResolvedModule {
                file_id: FileId(2),
                path: files[2].path.clone(),
                exports: vec![value_export("target", 0)].into(),
                ..ResolvedModule::default()
            },
        ];
        let test_entry_points = vec![
            EntryPoint {
                path: files[0].path.clone(),
                source: EntryPointSource::TestFile,
            },
            EntryPoint {
                path: files[1].path.clone(),
                source: EntryPointSource::TestFile,
            },
        ];
        let graph = ModuleGraph::build_with_reachability_roots_and_replacements(
            &modules,
            &[ResolvedReplacedModuleTarget {
                source_file: FileId(0),
                target_file: FileId(2),
            }],
            &test_entry_points,
            &[],
            &test_entry_points,
            &files,
        );
        RetainedModuleGraph::from(graph)
    }

    #[test]
    fn export_coverage_requires_one_root_to_reach_consumer_and_target() {
        let graph = mixed_root_graph(false);

        let exports = module_value_exports(&graph);

        assert_eq!(exports.len(), 1);
        assert!(!exports[0].test_referenced);
    }

    #[test]
    fn export_coverage_accepts_an_unmasked_correlated_reference() {
        let graph = mixed_root_graph(true);

        let exports = module_value_exports(&graph);

        assert_eq!(exports.len(), 1);
        assert!(exports[0].test_referenced);
    }

    #[test]
    fn commonjs_reference_does_not_credit_a_mocked_esm_export() {
        let files: Vec<_> = (0..2)
            .map(|id| DiscoveredFile {
                id: FileId(id),
                path: PathBuf::from(format!("/project/file{id}.ts")),
                size_bytes: 1,
            })
            .collect();
        let modules = vec![
            ResolvedModule {
                file_id: FileId(0),
                path: files[0].path.clone(),
                resolved_imports: vec![
                    import_with_mechanism(
                        FileId(1),
                        ImportedName::Named("esmOnly".to_string()),
                        false,
                    ),
                    import_with_mechanism(
                        FileId(1),
                        ImportedName::Named("required".to_string()),
                        true,
                    ),
                ],
                ..ResolvedModule::default()
            },
            ResolvedModule {
                file_id: FileId(1),
                path: files[1].path.clone(),
                exports: vec![value_export("esmOnly", 0), value_export("required", 20)].into(),
                ..ResolvedModule::default()
            },
        ];
        let test_entry_points = vec![EntryPoint {
            path: files[0].path.clone(),
            source: EntryPointSource::TestFile,
        }];
        let graph =
            RetainedModuleGraph::from(ModuleGraph::build_with_reachability_roots_and_replacements(
                &modules,
                &[ResolvedReplacedModuleTarget {
                    source_file: FileId(0),
                    target_file: FileId(1),
                }],
                &test_entry_points,
                &[],
                &test_entry_points,
                &files,
            ));

        let exports = module_value_exports(&graph);
        let coverage: rustc_hash::FxHashMap<_, _> = exports
            .into_iter()
            .map(|export| (export.name, export.test_referenced))
            .collect();

        assert_eq!(coverage.get("esmOnly"), Some(&false));
        assert_eq!(coverage.get("required"), Some(&true));
    }

    #[test]
    fn type_only_test_import_does_not_credit_a_value_export() {
        let files: Vec<_> = (0..3)
            .map(|id| DiscoveredFile {
                id: FileId(id),
                path: PathBuf::from(format!("/project/file{id}.ts")),
                size_bytes: 1,
            })
            .collect();
        let mut type_only = import(FileId(1), ImportedName::Named("typed".to_string()));
        type_only.info.is_type_only = true;
        let modules = vec![
            ResolvedModule {
                file_id: FileId(0),
                path: files[0].path.clone(),
                resolved_imports: vec![
                    type_only,
                    import(FileId(2), ImportedName::Named("called".to_string())),
                ],
                ..ResolvedModule::default()
            },
            ResolvedModule {
                file_id: FileId(1),
                path: files[1].path.clone(),
                exports: vec![value_export("typed", 0)].into(),
                ..ResolvedModule::default()
            },
            ResolvedModule {
                file_id: FileId(2),
                path: files[2].path.clone(),
                exports: vec![value_export("called", 0)].into(),
                ..ResolvedModule::default()
            },
        ];
        let test_entry_points = vec![EntryPoint {
            path: files[0].path.clone(),
            source: EntryPointSource::TestFile,
        }];
        let graph =
            RetainedModuleGraph::from(ModuleGraph::build_with_reachability_roots_and_replacements(
                &modules,
                &[],
                &test_entry_points,
                &[],
                &test_entry_points,
                &files,
            ));

        let coverage: rustc_hash::FxHashMap<_, _> = module_value_exports(&graph)
            .into_iter()
            .map(|export| (export.name, export.test_referenced))
            .collect();

        assert_eq!(coverage.get("typed"), Some(&false));
        assert_eq!(coverage.get("called"), Some(&true));
    }
}