fallow-api 3.19.0

Programmatic API contract types for fallow
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
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
//! Programmatic API contract types for fallow.
//!
//! Runtime execution for dead-code and duplication lives here. Health output
//! assembly is also API-owned, with the concrete runner injected while the
//! remaining health pipeline moves out of the CLI crate. This crate owns the
//! CLI-independent option, error, and output contracts so NAPI, future Rust
//! embedders, and the engine facade can share them without depending on the
//! CLI crate.
#![warn(missing_docs)]
#![cfg_attr(
    test,
    allow(
        clippy::expect_used,
        reason = "tests use expect to keep fixture setup concise"
    )
)]

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

use fallow_config::EmailMode;
use fallow_output::EffortEstimate;
use serde::Serialize;

mod analysis_context;
/// Stable per-finding keys and audit ledgers that compare head results against
/// a base snapshot, plus helpers that annotate output JSON with
/// introduced-vs-pre-existing attribution.
pub mod audit_keys;
pub mod audit_output;
pub mod combined_output;
/// One-line-per-finding compact text builders for dead-code, grouped, health,
/// and duplication output.
pub mod compact_output;
pub mod coverage;
pub mod dead_code_codeclimate;
pub mod dead_code_sarif;
pub mod decision_surface;
pub mod dupes_output;
mod duplication_filters;
pub mod editor;
pub mod explain;
pub mod grouped_output;
pub mod health_codeclimate;
pub mod json_output;
pub mod list_output;
mod list_runtime;
/// Markdown report builders for dead-code, grouped, duplication, health, and
/// walkthrough output.
pub mod markdown_output;
mod next_steps;
pub mod output_contracts;
pub mod review_deltas;
pub mod routing;
pub mod runtime;
mod runtime_json;
mod runtime_output;
pub mod sarif_output;
/// JSON Schema documents and zero-config rule defaults re-exported from
/// `fallow-config` for embedders such as the MCP resource surface.
pub mod schemas;
pub mod security_output;
/// Local-only advisory similar-code discovery and provider status.
pub mod similar_code;
mod type_aware;
pub mod ci_output {
    //! Compatibility re-exports for CI output builders now owned by
    //! `fallow-output`.

    pub use fallow_output::{
        CiIssue, CiProvider, GroupedReviewIssues, MARKER_PREFIX_V2, MARKER_SUFFIX_V2,
        MAX_COMMENT_BODY_BYTES, PROJECT_LEVEL_RULE_IDS, PrCommentRenderInput,
        ReviewCommentRenderInput, ReviewEnvelopeRenderInput, ReviewEnvelopeRenderResult,
        ReviewEnvelopeTruncation, ReviewGitlabDiffRefs, cap_body_with_marker, command_title,
        composite_fingerprint, escape_md, github_check_conclusion,
        group_review_issues_by_path_line, is_project_level_rule, issues_from_codeclimate,
        issues_from_codeclimate_issues, render_pr_comment, render_review_comment_for_group,
        render_review_envelope, review_label_from_codeclimate, summary_fingerprint, summary_label,
    };
}
pub use analysis_context::{ProgrammaticAnalysisContext, resolve_programmatic_analysis_context};
pub use audit_output::{
    AuditAttribution, AuditCodeClimateOutputInput, AuditJsonHeaderInput, AuditJsonOutputInput,
    AuditSarifOutputInput, AuditSummary, AuditVerdict,
    attach_audit_duplication_demotion_attribution, attach_audit_styling_attribution,
    attach_audit_wire_attribution, build_audit_codeclimate, build_audit_codeclimate_issues,
    build_audit_header_json, build_audit_header_map, build_audit_sarif, build_review_brief_header,
    serialize_audit_json,
};
pub use ci_output::{
    CiIssue, CiProvider, GroupedReviewIssues, MARKER_PREFIX_V2, MARKER_SUFFIX_V2,
    MAX_COMMENT_BODY_BYTES, PROJECT_LEVEL_RULE_IDS, PrCommentRenderInput, ReviewCommentRenderInput,
    ReviewEnvelopeRenderInput, ReviewEnvelopeRenderResult, ReviewEnvelopeTruncation,
    ReviewGitlabDiffRefs, cap_body_with_marker, command_title, composite_fingerprint, escape_md,
    github_check_conclusion, group_review_issues_by_path_line, is_project_level_rule,
    issues_from_codeclimate, issues_from_codeclimate_issues, render_pr_comment,
    render_review_comment_for_group, render_review_envelope, review_label_from_codeclimate,
    summary_fingerprint, summary_label,
};
pub use combined_output::{
    CombinedCheckJsonSection, CombinedJsonOutputInput, serialize_combined_dupes_json,
    serialize_combined_health_json, serialize_combined_json,
};
pub use compact_output::{
    build_compact_lines, build_duplication_compact_lines, build_grouped_compact_lines,
    build_health_compact_lines,
};
pub use coverage::{
    CoverageInputError, CoverageInputSource, CoverageInputs, resolve_coverage_inputs,
};
pub use dead_code_codeclimate::build_codeclimate;
pub use dead_code_sarif::build_sarif;
pub use dupes_output::{
    AttributedCloneGroup, AttributedCloneGroupFinding, AttributedInstance, CloneDemotionReason,
    CloneFamilyFinding, CloneGroupFinding, DupesReportPayload, DuplicationGroup,
    DuplicationGrouping, build_duplication_codeclimate,
};
pub use editor::{
    ChangedFilesError, EditorAnalysisOutput, EditorAnalysisResults, EditorAnalysisSession,
    EditorCloneFamily, EditorCloneFingerprintSet, EditorCloneGroup, EditorCloneInstance,
    EditorDeadCodeAnalysisOutput, EditorDuplicationReport, EditorDuplicationStats,
    EditorInlineComplexityExceeded, EditorInlineComplexityFinding, EditorMirroredDirectory,
    EditorProjectAnalysisOutput, EditorRefactoringKind, EditorRefactoringSuggestion,
    collect_inline_complexity, editor_duplicates, editor_extract, editor_results, editor_security,
    editor_suppress, filter_inline_complexity_by_changed_files, resolve_git_toplevel,
    try_get_changed_files_with_toplevel,
};
pub use explain::{
    CHECK_RULES, DUPES_RULES, FLAGS_RULES, HEALTH_RULES, RuleDef, RuleGuide, SECURITY_RULES,
    all_rules, bare_rule_id, coverage_analyze_meta, coverage_setup_meta, explain_issue_type,
    rule_by_id, rule_by_token, rule_command, rule_docs_url, rule_guide, rule_severity_key,
    security_meta, serialize_explain_programmatic_json, unknown_explain_error,
};
pub use fallow_config::{AuditGate, HealthConfig, TypeAwareRequire};
pub use fallow_output::{RootEnvelopeMode, serialize_similar_code_json_output};
pub use fallow_types::trace::{
    CloneTrace, DependencyTrace, ExportReference, ExportTrace, FileTrace, ReExportChain,
    TracedCloneGroup, TracedExport, TracedReExport,
};
pub use grouped_output::{
    ResultGroup, UNOWNED_GROUP_LABEL, build_duplication_grouping_with, group_analysis_results_with,
    largest_clone_group_owner_with,
};
pub use health_codeclimate::build_health_codeclimate;
pub use json_output::{
    CheckJsonExtraOutputs, CheckJsonOutputInput, CheckJsonPayloadInput, DuplicationJsonOutputInput,
    GroupedCheckJsonOutputInput, GroupedDuplicationJsonOutputInput, serialize_check_json,
    serialize_check_json_payload, serialize_duplication_json, serialize_grouped_check_json,
    serialize_grouped_duplication_json,
};
pub use list_output::{
    ListJsonEnvelope, ListJsonOutputInput, build_list_json_output, serialize_list_json_output,
};
pub use list_runtime::{
    BoundaryData, ListBoundariesOptions, ListBoundariesProgrammaticOutput, LogicalGroupInfo,
    ProjectInfoOptions, ProjectInfoProgrammaticOutput, RuleInfo, ZoneInfo, boundary_data_to_output,
    compute_boundary_data, run_list_boundaries, run_project_info,
    serialize_list_boundaries_programmatic_json, serialize_project_info_programmatic_json,
};
pub use markdown_output::{
    build_duplication_markdown, build_grouped_markdown, build_health_markdown, build_markdown,
    build_walkthrough_markdown,
};
pub use output_contracts::{
    AuditOutput, BoundariesListLogicalGroup, BoundariesListRule, BoundariesListZone,
    BoundariesListing, CombinedOutput, FallowOutput, ImpactOutput, ListBoundariesOutput,
    ListEntryPointOutput, ListOutput, ListPluginOutput, ReviewBriefWireOutput, SecurityGate,
    SecurityOutput, SecurityOutputConfig, SecuritySummaryOutput, SimilarCodeCandidateSnapshot,
    SimilarCodeOutput, TraceOutput, WorkspacesOutput,
};
pub use runtime::{
    AuditProgrammaticKeySnapshot, AuditProgrammaticOutput, BoundaryViolationsOutput,
    BoundaryViolationsProgrammaticOutput, CircularDependenciesOutput,
    CircularDependenciesProgrammaticOutput, CombinedProgrammaticOutput, DeadCodeOutput,
    DeadCodeProgrammaticOutput, DecisionSurfaceProgrammaticOutput, DuplicationOutput,
    DuplicationProgrammaticOutput, EngineHealthRunner, FeatureFlagsOutput,
    FeatureFlagsProgrammaticOutput, HealthJsonReportInput, HealthProgrammaticOutput,
    ProgrammaticHealthAnalysis, ProgrammaticHealthNextStepFacts, ProgrammaticHealthRun,
    ProgrammaticHealthRunner, TraceClassMemberOutput, TraceCloneBenchmarkResult, TraceCloneOutput,
    TraceCloneProgrammaticOutput, TraceDependencyOutput, TraceDependencyProgrammaticOutput,
    TraceExportOutput, TraceExportProgrammaticOutput, TraceExportTargetOutput, TraceFileOutput,
    TraceFileProgrammaticOutput, benchmark_trace_clone_compact_json,
    benchmark_trace_graph_family_compact_json, inspect_similar_code, load_health_config,
    parse_similar_code_candidate_snapshot, review_similar_code, run_audit, run_boundary_violations,
    run_circular_dependencies, run_combined, run_complexity_with_runner, run_dead_code,
    run_decision_surface, run_duplication, run_feature_flags, run_health, run_health_with_runner,
    run_similar_code, run_trace_clone, run_trace_dependency, run_trace_export, run_trace_file,
    select_similar_code_candidate_snapshot, serialize_health_report_json,
};
pub use runtime_json::{
    serialize_audit_programmatic_json, serialize_boundary_violations_programmatic_json,
    serialize_circular_dependencies_programmatic_json, serialize_combined_programmatic_json,
    serialize_dead_code_programmatic_json, serialize_decision_surface_programmatic_json,
    serialize_duplication_programmatic_json, serialize_feature_flags_programmatic_json,
    serialize_health_programmatic_json, serialize_trace_clone_programmatic_json,
    serialize_trace_dependency_programmatic_json, serialize_trace_export_programmatic_json,
    serialize_trace_file_programmatic_json,
};
pub use sarif_output::{
    annotate_sarif_results, build_duplication_sarif, build_grouped_duplication_sarif,
    build_health_sarif,
};
pub use security_output::SecurityGateMode;
pub use type_aware::{
    SemanticCouplingOutcome, SemanticDeadCodeOutcome, SemanticInspectOutcome, TypeAwareError,
    TypeAwareFileChanges, TypeAwareOutcome, TypeAwareSession, TypeAwareStatus,
    discard_unverified_semantic_candidates, inspect_symbol as inspect_type_aware_symbol,
    merge_type_aware_meta,
    refine_configured_dead_code_results as refine_type_aware_results_with_config,
    refine_configured_dead_code_results_in_session as refine_type_aware_results_in_session_with_config,
    refine_dead_code_results as refine_type_aware_results,
    refine_dead_code_results_in_session as refine_type_aware_results_in_session,
    refine_programmatic_dead_code as refine_type_aware_dead_code, shutdown_type_aware_sidecars,
    status as type_aware_status, symbol_impact as run_type_aware_symbol_impact,
    symbol_impact as type_aware_symbol_impact, terminate_active_type_aware_sidecars,
    trace_symbol as run_type_aware_symbol_trace, trace_symbol as trace_type_aware_symbol,
    type_coupling as analyze_type_coupling,
};

/// Long names of the analysis-affecting global CLI flags that
/// [`AnalysisOptions`] mirrors for embedders.
///
/// A contract test in the CLI crate asserts this list stays in sync with the
/// clap globals, so drift between the CLI surface and the programmatic
/// options is caught at test time.
pub const COMMON_ANALYSIS_OPTION_FLAGS: &[&str] = &[
    "root",
    "config",
    "no-cache",
    "threads",
    "changed-since",
    "diff-file",
    "production",
    "workspace",
    "changed-workspaces",
    "explain",
    "allow-remote-extends",
];

/// Structured error surface for the programmatic API.
#[derive(Debug, Clone, Serialize)]
pub struct ProgrammaticError {
    /// Human-readable description; also the `Display` output.
    pub message: String,
    /// Process exit code the CLI maps this failure to.
    pub exit_code: u8,
    /// Stable machine-readable code such as `FALLOW_INVALID_COVERAGE_PATH`.
    pub code: Option<String>,
    /// Optional remediation hint for the caller.
    pub help: Option<String>,
    /// Dotted path of the offending input, such as `health.coverage`.
    pub context: Option<String>,
}

impl ProgrammaticError {
    /// Create an error from the required message and exit code, with all
    /// optional fields left empty.
    #[must_use]
    pub fn new(message: impl Into<String>, exit_code: u8) -> Self {
        Self {
            message: message.into(),
            exit_code,
            code: None,
            help: None,
            context: None,
        }
    }

    /// Attach a remediation hint shown to the caller alongside the message.
    #[must_use]
    pub fn with_help(mut self, help: impl Into<String>) -> Self {
        self.help = Some(help.into());
        self
    }

    /// Attach a stable machine-readable code such as
    /// `FALLOW_INVALID_COVERAGE_PATH`.
    #[must_use]
    pub fn with_code(mut self, code: impl Into<String>) -> Self {
        self.code = Some(code.into());
        self
    }

    /// Attach the dotted path of the offending input, such as
    /// `health.coverage`.
    #[must_use]
    pub fn with_context(mut self, context: impl Into<String>) -> Self {
        self.context = Some(context.into());
        self
    }
}

impl std::fmt::Display for ProgrammaticError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl std::error::Error for ProgrammaticError {}

/// Shared options for all one-shot analyses.
#[derive(Debug, Clone, Default)]
pub struct AnalysisOptions {
    /// Project root to analyze. `None` resolves to the current working
    /// directory.
    pub root: Option<PathBuf>,
    /// Explicit config file path. `None` uses config discovery from the root.
    pub config_path: Option<PathBuf>,
    /// Permit `https://` config inheritance for this analysis call.
    pub allow_remote_extends: bool,
    /// Bypass the on-disk analysis cache for this call.
    pub no_cache: bool,
    /// Worker thread count. `None` picks the default; `Some(0)` is rejected.
    pub threads: Option<usize>,
    /// Explicit unified diff file that scopes changed-code analysis.
    pub diff_file: Option<PathBuf>,
    /// Legacy convenience override. `true` forces production mode; `false`
    /// defers to config unless `production_override` is set.
    pub production: bool,
    /// Explicit production override from an embedder option. `None` means
    /// use the project config for the current analysis.
    pub production_override: Option<bool>,
    /// Git base reference that scopes analysis to files changed since it.
    pub changed_since: Option<String>,
    /// Restrict analysis to the named workspace packages.
    pub workspace: Option<Vec<String>>,
    /// Restrict analysis to workspaces changed since the given git reference.
    pub changed_workspaces: Option<String>,
    /// Include rule and metric explanations (`_meta`) in machine output.
    pub explain: bool,
    /// Optional project-wide TypeScript semantic analysis. Disabled by default
    /// and never changes compiler or typed-lint ownership.
    pub type_aware: TypeAwareOptions,
}

/// Typed options for Fallow's optional TypeScript semantic companion.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TypeAwareOptions {
    /// Turn on TypeScript semantic refinement for this call.
    pub enabled: bool,
    /// Explicit TypeScript project paths handed to the semantic sidecar.
    /// Empty defers to project discovery.
    pub projects: Vec<PathBuf>,
    /// How strictly semantic completion is required before results are kept.
    pub require: fallow_config::TypeAwareRequire,
}

/// Issue-type filters for the dead-code analysis.
///
/// Each flag opts one issue type into the report. When no flag is enabled the
/// analysis reports every issue type.
#[derive(Debug, Clone, Default)]
pub struct DeadCodeFilters {
    /// Files never reached from any entry point.
    pub unused_files: bool,
    /// Exported symbols never imported elsewhere.
    pub unused_exports: bool,
    /// Declared dependencies never imported.
    pub unused_deps: bool,
    /// Exported types never referenced.
    pub unused_types: bool,
    /// Exported APIs that expose non-exported types.
    pub private_type_leaks: bool,
    /// Enum members never read.
    pub unused_enum_members: bool,
    /// Class members never used outside their declaration.
    pub unused_class_members: bool,
    /// Store members (for example Pinia) never used outside the store.
    pub unused_store_members: bool,
    /// `inject` calls with no matching `provide`.
    pub unprovided_injects: bool,
    /// Components never rendered by any template or JSX.
    pub unrendered_components: bool,
    /// Declared component props never used.
    pub unused_component_props: bool,
    /// Declared component emits never used.
    pub unused_component_emits: bool,
    /// Declared component inputs never bound.
    pub unused_component_inputs: bool,
    /// Declared component outputs never listened to.
    pub unused_component_outputs: bool,
    /// Svelte component events never listened to.
    pub unused_svelte_events: bool,
    /// Server actions never invoked.
    pub unused_server_actions: bool,
    /// `load` data keys never read by the consuming page.
    pub unused_load_data_keys: bool,
    /// Imports that do not resolve to a file or package.
    pub unresolved_imports: bool,
    /// Imported packages missing from the dependency manifest.
    pub unlisted_deps: bool,
    /// The same symbol exported more than once.
    pub duplicate_exports: bool,
    /// Circular import chains.
    pub circular_deps: bool,
    /// Cycles formed through re-export chains.
    pub re_export_cycles: bool,
    /// Imports that cross configured architecture boundaries.
    pub boundary_violations: bool,
    /// Violations of configured dependency policy rules.
    pub policy_violations: bool,
    /// Suppression comments that no longer match a finding.
    pub stale_suppressions: bool,
    /// Catalog entries never referenced by a workspace package.
    pub unused_catalog_entries: bool,
    /// Catalog groups that contain no entries.
    pub empty_catalog_groups: bool,
    /// `catalog:` references without a matching catalog entry.
    pub unresolved_catalog_references: bool,
    /// Dependency overrides that never affect a resolved package.
    pub unused_dependency_overrides: bool,
    /// Dependency overrides that cannot apply as written.
    pub misconfigured_dependency_overrides: bool,
}

impl DeadCodeFilters {
    fn any_active(&self) -> bool {
        self.unused_files
            || self.unused_exports
            || self.unused_deps
            || self.unused_types
            || self.private_type_leaks
            || self.unused_enum_members
            || self.unused_class_members
            || self.unused_store_members
            || self.unprovided_injects
            || self.unrendered_components
            || self.unused_component_props
            || self.unused_component_emits
            || self.unused_component_inputs
            || self.unused_component_outputs
            || self.unused_svelte_events
            || self.unused_server_actions
            || self.unused_load_data_keys
            || self.unresolved_imports
            || self.unlisted_deps
            || self.duplicate_exports
            || self.circular_deps
            || self.re_export_cycles
            || self.boundary_violations
            || self.policy_violations
            || self.stale_suppressions
            || self.unused_catalog_entries
            || self.empty_catalog_groups
            || self.unresolved_catalog_references
            || self.unused_dependency_overrides
            || self.misconfigured_dependency_overrides
    }

    /// Enable the issue filter addressed by a shared registry selector.
    ///
    /// Returns `false` when the selector is not registered for dead-code
    /// filtering. Callers that expose user input should surface their own
    /// validation error with the accepted registry values.
    pub fn enable_registry_selector(&mut self, selector: &str) -> bool {
        let Some(flag) = fallow_types::issue_meta::MCP_ISSUE_TYPE_FLAGS
            .iter()
            .find_map(|&(name, flag)| (name == selector).then_some(flag))
        else {
            return false;
        };
        self.enable_cli_filter_flag(flag);
        true
    }

    fn enable_cli_filter_flag(&mut self, flag: &str) {
        match flag {
            "--unused-files" => self.unused_files = true,
            "--unused-exports" => self.unused_exports = true,
            "--unused-types" => self.unused_types = true,
            "--private-type-leaks" => self.private_type_leaks = true,
            "--unused-deps" => self.unused_deps = true,
            "--unused-enum-members" => self.unused_enum_members = true,
            "--unused-class-members" => self.unused_class_members = true,
            "--unused-store-members" => self.unused_store_members = true,
            "--unprovided-injects" => self.unprovided_injects = true,
            "--unrendered-components" => self.unrendered_components = true,
            "--unused-component-props" => self.unused_component_props = true,
            "--unused-component-emits" => self.unused_component_emits = true,
            "--unused-component-inputs" => self.unused_component_inputs = true,
            "--unused-component-outputs" => self.unused_component_outputs = true,
            "--unused-svelte-events" => self.unused_svelte_events = true,
            "--unused-server-actions" => self.unused_server_actions = true,
            "--unused-load-data-keys" => self.unused_load_data_keys = true,
            "--unresolved-imports" => self.unresolved_imports = true,
            "--unlisted-deps" => self.unlisted_deps = true,
            "--duplicate-exports" => self.duplicate_exports = true,
            "--circular-deps" => self.circular_deps = true,
            "--re-export-cycles" => self.re_export_cycles = true,
            "--boundary-violations" => self.boundary_violations = true,
            "--policy-violations" => self.policy_violations = true,
            "--stale-suppressions" => self.stale_suppressions = true,
            "--unused-catalog-entries" => self.unused_catalog_entries = true,
            "--empty-catalog-groups" => self.empty_catalog_groups = true,
            "--unresolved-catalog-references" => self.unresolved_catalog_references = true,
            "--unused-dependency-overrides" => self.unused_dependency_overrides = true,
            "--misconfigured-dependency-overrides" => {
                self.misconfigured_dependency_overrides = true;
            }
            _ => unreachable!("registry emitted unsupported dead-code filter flag: {flag}"),
        }
    }
}

/// Options for dead-code-oriented analyses.
#[derive(Debug, Clone, Default)]
pub struct DeadCodeOptions {
    /// Shared analysis options.
    pub analysis: AnalysisOptions,
    /// Issue-type selection; everything is reported when no filter is set.
    pub filters: DeadCodeFilters,
    /// Restrict findings to these files when non-empty.
    pub files: Vec<PathBuf>,
    /// Also report unused exports declared in entry-point files.
    pub include_entry_exports: bool,
}

/// Options for changed-code audit analysis.
#[derive(Debug, Clone, Default)]
pub struct AuditOptions {
    /// Shared analysis options.
    pub analysis: AnalysisOptions,
    /// Git base reference for the changed-code comparison. `None` lets the
    /// audit detect a base itself.
    pub base: Option<String>,
    /// Force production mode for every audit domain.
    pub production: bool,
    /// Production override for the dead-code domain; `None` defers to config.
    pub production_dead_code: Option<bool>,
    /// Production override for the health domain; `None` defers to config.
    pub production_health: Option<bool>,
    /// Production override for the duplication domain; `None` defers to
    /// config.
    pub production_dupes: Option<bool>,
    /// Enable CSS / styling analysis; `None` defers to config.
    pub css: Option<bool>,
    /// Enable deep cross-file CSS analysis; `None` defers to config.
    pub css_deep: Option<bool>,
    /// Gate mode deciding which findings fail the audit.
    pub gate: fallow_config::AuditGate,
    /// Fail the gate when a changed function exceeds this CRAP score.
    pub max_crap: Option<f64>,
    /// Test coverage report path for coverage-aware findings.
    pub coverage: Option<PathBuf>,
    /// Absolute path prefix the coverage report recorded its files under.
    pub coverage_root: Option<PathBuf>,
    /// Also report unused exports declared in entry-point files.
    pub include_entry_exports: bool,
    /// Runtime coverage capture merged into the audit.
    pub runtime_coverage: Option<PathBuf>,
    /// Minimum recorded invocations for a code path to count as hot.
    pub min_invocations_hot: u64,
}

/// Options for bare combined analysis through the programmatic API.
#[derive(Debug, Clone)]
pub struct CombinedOptions {
    /// Shared analysis options.
    pub analysis: AnalysisOptions,
    /// Run the dead-code domain.
    pub dead_code: bool,
    /// Run the duplication domain.
    pub duplication: bool,
    /// Run the health domain.
    pub health: bool,
    /// Also report unused exports declared in entry-point files.
    pub include_entry_exports: bool,
    /// Options for the duplication domain.
    pub duplication_options: DuplicationOptions,
    /// Options for the health domain.
    pub health_options: ComplexityOptions,
}

impl Default for CombinedOptions {
    fn default() -> Self {
        Self {
            analysis: AnalysisOptions::default(),
            dead_code: true,
            duplication: true,
            health: true,
            include_entry_exports: false,
            duplication_options: DuplicationOptions::default(),
            health_options: ComplexityOptions::default(),
        }
    }
}

/// Options for changed-code decision-surface analysis.
#[derive(Debug, Clone, Default)]
pub struct DecisionSurfaceOptions {
    /// Shared analysis options.
    pub analysis: AnalysisOptions,
    /// Git base reference for the changed-code comparison. `None` lets the
    /// analysis detect a base itself.
    pub base: Option<String>,
    /// Cap on the number of decisions surfaced.
    pub max_decisions: Option<usize>,
}

/// Options for feature-flag analysis.
#[derive(Debug, Clone, Default)]
pub struct FeatureFlagsOptions {
    /// Shared analysis options.
    pub analysis: AnalysisOptions,
    /// Cap on the number of reported flags.
    pub top: Option<usize>,
}

/// Programmatic duplication mode selection.
#[derive(Debug, Clone, Copy, Default)]
pub enum DuplicationMode {
    /// Preserve all tokens, including identifier names and literal values
    /// (Type-1 clones only).
    Strict,
    /// Default mode, equivalent to strict for AST-based tokenization.
    #[default]
    Mild,
    /// Blind string literal values while preserving structure.
    Weak,
    /// Blind all identifiers and literal values for structural (Type-2)
    /// detection.
    Semantic,
}

/// Options for duplication analysis.
#[derive(Debug, Clone, Default)]
pub struct DuplicationOptions {
    /// Shared analysis options.
    pub analysis: AnalysisOptions,
    /// Detection mode; `None` defers to the project config.
    pub mode: Option<DuplicationMode>,
    /// Detect function-scoped near-miss clones in addition to exact clones.
    /// `None` defers to the project config.
    pub near: Option<bool>,
    /// Minimum number of tokens for a clone.
    pub min_tokens: Option<usize>,
    /// Minimum number of lines for a clone.
    pub min_lines: Option<usize>,
    /// Minimum number of occurrences before a clone group is reported.
    /// Values below 2 are silently treated as 2 by the engine-facing adapter.
    pub min_occurrences: Option<usize>,
    /// Maximum allowed duplication percentage before the gate fails; 0 means
    /// no limit. `None` defers to the project config.
    pub threshold: Option<f64>,
    /// Only report cross-directory duplicates. `None` defers to the project
    /// config.
    pub skip_local: Option<bool>,
    /// Match clones across languages. `None` defers to the project config.
    pub cross_language: Option<bool>,
    /// Exclude module wiring from clone detection. `None` defers to the project
    /// config.
    pub ignore_imports: Option<bool>,
    /// Cap on the number of reported clone groups.
    pub top: Option<usize>,
}

/// Options for local advisory similar-code discovery.
#[derive(Debug, Clone, Default)]
pub struct SimilarCodeOptions {
    /// Shared project, config, cache, diff, and workspace options.
    pub analysis: AnalysisOptions,
    /// Model-specific cosine floor. `None` uses `similarCode.threshold`.
    pub threshold: Option<f64>,
    /// Minimum source lines per function. `None` uses
    /// `similarCode.minLines`.
    pub min_lines: Option<usize>,
    /// Cap on displayed candidates after the full bounded comparison.
    pub top: Option<usize>,
    /// Restrict work and reported pairs to those where at least one side matches
    /// one of these project-relative files. Deterministic background context is
    /// retained only within the bounded scoped comparison budget.
    pub files: Vec<PathBuf>,
    /// Exact companion binary resolved and signature-verified by an official
    /// distribution adapter such as the Node loader. `None` uses trusted
    /// sibling discovery. Project config, PATH commands, and remote providers
    /// must never populate this field.
    #[doc(hidden)]
    pub adapter_provider_path: Option<PathBuf>,
}

/// Options for one similar-code candidate inspection packet.
#[derive(Debug, Clone)]
pub struct SimilarCodeInspectOptions {
    /// Shared project and configuration options used only for source validation
    /// and deterministic enrichment. Inspect never reruns provider retrieval.
    pub analysis: AnalysisOptions,
    /// Bounded candidate handoff selected from the original discovery output.
    pub snapshot: fallow_output::SimilarCodeCandidateSnapshot,
}

/// Options for export trace analysis.
#[derive(Debug, Clone, Default)]
pub struct TraceExportOptions {
    /// Shared analysis options.
    pub analysis: AnalysisOptions,
    /// Path of the module that declares the export.
    pub file: String,
    /// Name of the export to trace.
    pub export_name: String,
}

/// Options for file trace analysis.
#[derive(Debug, Clone, Default)]
pub struct TraceFileOptions {
    /// Shared analysis options.
    pub analysis: AnalysisOptions,
    /// Path of the file to trace.
    pub file: String,
}

/// Options for dependency trace analysis.
#[derive(Debug, Clone, Default)]
pub struct TraceDependencyOptions {
    /// Shared analysis options.
    pub analysis: AnalysisOptions,
    /// Package whose importers are traced.
    pub package_name: String,
}

/// Duplicate-code trace target.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TraceCloneTarget {
    /// Select the clone group covering this file and line.
    Location {
        /// Path of the file containing the clone instance.
        file: String,
        /// One-based line inside the clone instance.
        line: usize,
    },
    /// Select the clone group by its fingerprint.
    Fingerprint(String),
}

/// Options for duplicate-code trace analysis.
#[derive(Debug, Clone)]
pub struct TraceCloneOptions {
    /// Duplication options controlling detection before the trace.
    pub duplication: DuplicationOptions,
    /// Clone group to trace.
    pub target: TraceCloneTarget,
}

/// Sort criteria for complexity findings.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ComplexitySort {
    /// Sort by cyclomatic complexity (default).
    #[default]
    Cyclomatic,
    /// Sort by cognitive complexity.
    Cognitive,
    /// Sort by function length in lines.
    Lines,
    /// Sort by finding severity.
    Severity,
}

/// Privacy mode for ownership-aware hotspot output.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum OwnershipEmailMode {
    /// Show the raw email address as it appears in git history.
    Raw,
    /// Show only the local part before the `@` (default).
    #[default]
    Handle,
    /// Show a stable non-cryptographic pseudonym derived from the raw email.
    Anonymized,
    /// Legacy spelling retained for embedders that already pass `hash`.
    Hash,
}

/// Effort filter for refactoring targets.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TargetEffort {
    /// Low estimated refactoring effort.
    Low,
    /// Medium estimated refactoring effort.
    Medium,
    /// High estimated refactoring effort.
    High,
}

/// Options for complexity / health analysis.
#[derive(Debug, Clone, Default)]
pub struct ComplexityOptions {
    /// Shared analysis options.
    pub analysis: AnalysisOptions,
    /// Override for the cyclomatic complexity threshold.
    pub max_cyclomatic: Option<u16>,
    /// Override for the cognitive complexity threshold.
    pub max_cognitive: Option<u16>,
    /// Override for the CRAP score threshold.
    pub max_crap: Option<f64>,
    /// Cap on the number of reported findings.
    pub top: Option<usize>,
    /// Sort order for complexity findings.
    pub sort: ComplexitySort,
    /// Include the per-metric complexity breakdown with each finding.
    pub complexity_breakdown: bool,
    /// Request the complexity findings section.
    pub complexity: bool,
    /// Request the per-file score section.
    pub file_scores: bool,
    /// Request the coverage-gap section.
    pub coverage_gaps: bool,
    /// Request the churn hotspot section.
    pub hotspots: bool,
    /// Include ownership data with hotspots; implies the hotspot section.
    pub ownership: bool,
    /// Email privacy mode for ownership output; implies ownership when set.
    pub ownership_emails: Option<OwnershipEmailMode>,
    /// Request the refactoring targets section.
    pub targets: bool,
    /// Include CSS / styling health.
    pub css: bool,
    /// Enable deep cross-file CSS analysis.
    pub css_deep: bool,
    /// Filter refactoring targets by estimated effort; implies the targets
    /// section when set.
    pub effort: Option<TargetEffort>,
    /// Request the overall health score.
    pub score: bool,
    /// Git time window (for example `30d`) for churn-based sections.
    pub since: Option<String>,
    /// Minimum commit count for a file to count as a hotspot.
    pub min_commits: Option<u32>,
    /// Test coverage report path for coverage-aware sections.
    pub coverage: Option<PathBuf>,
    /// Absolute path prefix the coverage report recorded its files under.
    pub coverage_root: Option<PathBuf>,
    /// The coverage map was recorded against a different checkout of this
    /// project, so function line numbers may have drifted arbitrarily.
    /// Set internally by the audit base-worktree pass; leave `false` for
    /// same-checkout coverage.
    pub coverage_relocated: bool,
}

/// Health threshold overrides accepted by the programmatic API.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct ComplexityThresholdOverrides {
    /// Override for the cyclomatic complexity threshold.
    pub max_cyclomatic: Option<u16>,
    /// Override for the cognitive complexity threshold.
    pub max_cognitive: Option<u16>,
    /// Override for the CRAP score threshold.
    pub max_crap: Option<f64>,
}

/// Coverage inputs accepted by the programmatic API.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ComplexityCoverageInputs<'a> {
    /// Test coverage report path.
    pub coverage: Option<&'a Path>,
    /// Absolute path prefix the coverage report recorded its files under.
    pub coverage_root: Option<&'a Path>,
    /// The coverage map was recorded against a different checkout of this
    /// project; tolerate arbitrary line drift for unambiguous name matches.
    pub coverage_relocated: bool,
}

/// Input for deriving effective health sections from API-owned flags.
#[derive(Debug, Clone)]
pub struct HealthSectionOptions {
    /// Requested output format; `Badge` implies the score section.
    pub output: fallow_types::output_format::OutputFormat,
    /// The complexity findings section was requested.
    pub complexity: bool,
    /// The per-file score section was requested.
    pub file_scores: bool,
    /// The coverage-gap section was requested.
    pub coverage_gaps: bool,
    /// The churn hotspot section was requested.
    pub hotspots: bool,
    /// The refactoring targets section was requested.
    pub targets: bool,
    /// CSS / styling health was requested.
    pub css: bool,
    /// The overall health score was requested.
    pub score: bool,
    /// A score gate is active; implies the score section.
    pub score_gate: bool,
    /// A snapshot write was requested; forces full hidden inputs.
    pub snapshot_requested: bool,
    /// Trend output was requested; implies score and hotspot inputs.
    pub trend: bool,
}

/// Derived section selection for health runs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DerivedHealthSections {
    /// At least one section was explicitly requested.
    pub any_section: bool,
    /// Emit the complexity findings section.
    pub complexity: bool,
    /// Compute the per-file score section.
    pub file_scores: bool,
    /// Emit the coverage-gap section.
    pub coverage_gaps: bool,
    /// Compute the churn hotspot section.
    pub hotspots: bool,
    /// Emit the refactoring targets section.
    pub targets: bool,
    /// Include CSS / styling health.
    pub css: bool,
    /// Compute the overall health score.
    pub score: bool,
    /// Compute full inputs even for sections that are not emitted.
    pub force_full: bool,
    /// Only the score should be printed.
    pub score_only_output: bool,
}

/// Input for deriving effective programmatic complexity sections.
#[derive(Debug, Clone)]
pub struct ComplexitySectionOptions {
    /// The complexity findings section was requested.
    pub complexity: bool,
    /// The per-file score section was requested.
    pub file_scores: bool,
    /// The coverage-gap section was requested.
    pub coverage_gaps: bool,
    /// The churn hotspot section was requested.
    pub hotspots: bool,
    /// Ownership data was requested; implies the hotspot section.
    pub ownership: bool,
    /// The refactoring targets section was requested.
    pub targets: bool,
    /// CSS / styling health was requested.
    pub css: bool,
    /// The overall health score was requested.
    pub score: bool,
}

/// Derived section selection for programmatic health / complexity runs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DerivedComplexityOptions {
    /// At least one section was explicitly requested.
    pub any_section: bool,
    /// Emit the complexity findings section.
    pub complexity: bool,
    /// Compute the per-file score section.
    pub file_scores: bool,
    /// Emit the coverage-gap section.
    pub coverage_gaps: bool,
    /// Compute the churn hotspot section.
    pub hotspots: bool,
    /// Include ownership data with hotspots.
    pub ownership: bool,
    /// Emit the refactoring targets section.
    pub targets: bool,
    /// Compute full inputs even for sections that are not emitted.
    pub force_full: bool,
    /// Only the score should be printed.
    pub score_only_output: bool,
    /// Compute the overall health score.
    pub score: bool,
}

/// Normalized programmatic complexity / health inputs owned by `fallow-api`.
#[derive(Debug, Clone, PartialEq)]
pub struct ComplexityRunOptions<'a> {
    /// Complexity threshold overrides.
    pub thresholds: ComplexityThresholdOverrides,
    /// Cap on the number of reported findings.
    pub top: Option<usize>,
    /// Sort order for complexity findings.
    pub sort: ComplexitySort,
    /// Include the per-metric complexity breakdown with each finding.
    pub complexity_breakdown: bool,
    /// Derived effective section selection.
    pub sections: DerivedComplexityOptions,
    /// Email privacy mode for ownership output.
    pub ownership_emails: Option<OwnershipEmailMode>,
    /// Filter refactoring targets by estimated effort.
    pub effort: Option<TargetEffort>,
    /// Include CSS / styling health.
    pub css: bool,
    /// Enable deep cross-file CSS analysis.
    pub css_deep: bool,
    /// Git time window (for example `30d`) for churn-based sections.
    pub since: Option<&'a str>,
    /// Minimum commit count for a file to count as a hotspot.
    pub min_commits: Option<u32>,
    /// Test coverage inputs.
    pub coverage_inputs: ComplexityCoverageInputs<'a>,
}

/// Derive effective health section flags for API consumers.
#[must_use]
pub fn derive_health_sections(options: &HealthSectionOptions) -> DerivedHealthSections {
    let score = options.score
        || options.score_gate
        || options.trend
        || matches!(
            options.output,
            fallow_types::output_format::OutputFormat::Badge
        );
    let any_section = options.complexity
        || options.file_scores
        || options.coverage_gaps
        || options.hotspots
        || options.targets
        || score;
    let effective_score = if any_section { score } else { true } || options.snapshot_requested;
    let force_full = options.snapshot_requested || effective_score;

    DerivedHealthSections {
        any_section,
        complexity: if any_section {
            options.complexity
        } else {
            true
        },
        file_scores: if any_section {
            options.file_scores
        } else {
            true
        } || force_full,
        coverage_gaps: if any_section {
            options.coverage_gaps
        } else {
            false
        },
        hotspots: if any_section { options.hotspots } else { true }
            || options.snapshot_requested
            || options.trend,
        targets: if any_section { options.targets } else { true },
        css: options.css,
        score: effective_score,
        force_full,
        score_only_output: is_health_score_only_output(options, score),
    }
}

/// Derive effective programmatic health / complexity section flags.
#[must_use]
pub fn derive_complexity_sections(options: &ComplexitySectionOptions) -> DerivedComplexityOptions {
    let requested_hotspots = options.hotspots || options.ownership;
    let sections = derive_health_sections(&HealthSectionOptions {
        output: fallow_types::output_format::OutputFormat::Human,
        complexity: options.complexity,
        file_scores: options.file_scores,
        coverage_gaps: options.coverage_gaps,
        hotspots: requested_hotspots,
        targets: options.targets,
        css: options.css,
        score: options.score,
        score_gate: false,
        snapshot_requested: false,
        trend: false,
    });

    DerivedComplexityOptions {
        any_section: sections.any_section,
        complexity: sections.complexity,
        file_scores: sections.file_scores,
        coverage_gaps: sections.coverage_gaps,
        hotspots: sections.hotspots,
        ownership: options.ownership && sections.hotspots,
        targets: sections.targets,
        force_full: sections.force_full,
        score_only_output: sections.score_only_output,
        score: sections.score,
    }
}

/// Derive effective programmatic health / complexity section flags.
#[must_use]
pub fn derive_complexity_options(options: &ComplexityOptions) -> DerivedComplexityOptions {
    derive_complexity_sections(&complexity_section_options(options))
}

/// Normalize public API complexity options into engine-owned run contracts.
#[must_use]
pub fn derive_complexity_run_options(options: &ComplexityOptions) -> ComplexityRunOptions<'_> {
    ComplexityRunOptions {
        thresholds: ComplexityThresholdOverrides {
            max_cyclomatic: options.max_cyclomatic,
            max_cognitive: options.max_cognitive,
            max_crap: options.max_crap,
        },
        top: options.top,
        sort: options.sort,
        complexity_breakdown: options.complexity_breakdown,
        sections: derive_complexity_options(options),
        ownership_emails: options.ownership_emails,
        effort: options.effort,
        css: options.css,
        css_deep: options.css_deep,
        since: options.since.as_deref(),
        min_commits: options.min_commits,
        coverage_inputs: ComplexityCoverageInputs {
            coverage: options.coverage.as_deref(),
            coverage_root: options.coverage_root.as_deref(),
            coverage_relocated: options.coverage_relocated,
        },
    }
}

/// Validate programmatic complexity / health inputs before invoking a concrete
/// runner.
///
/// These option contracts belong to the API boundary because NAPI and future
/// Rust embedders construct the same [`ComplexityOptions`] type.
///
/// A relative coverage path resolves against `analysis.root` (the cwd when
/// unset), exactly as the engine resolves it at read time, so a
/// project-relative `health.coverage` validates against the project and not
/// against the embedder's working directory.
///
/// The options carry no record of which layer supplied `coverage`, so the
/// path error's `context` is always `health.coverage`, whichever of the tool
/// parameter, `FALLOW_COVERAGE`, or the config field won
/// ([`crate::coverage::resolve_coverage_inputs`] resolves that order). The
/// sibling root error names its layer because
/// [`crate::coverage::CoverageInputError`] carries it.
///
/// # Errors
///
/// Returns a structured programmatic error when a coverage path does not exist
/// or when `coverage_root` is not an absolute prefix from the coverage data.
pub fn validate_complexity_options(options: &ComplexityOptions) -> Result<(), ProgrammaticError> {
    if let Some(path) = &options.coverage {
        let resolved = fallow_engine::health::scoring::resolve_relative_to_root(
            path,
            options.analysis.root.as_deref(),
        );
        if !resolved.exists() {
            return Err(ProgrammaticError::new(
                format!("coverage path does not exist: {}", resolved.display()),
                2,
            )
            .with_code("FALLOW_INVALID_COVERAGE_PATH")
            .with_context("health.coverage"));
        }
    }
    if let Err(message) =
        fallow_engine::health::validate_coverage_root_absolute(options.coverage_root.as_deref())
    {
        return Err(ProgrammaticError::new(message, 2)
            .with_code("FALLOW_INVALID_COVERAGE_ROOT")
            .with_context("health.coverage_root"));
    }

    Ok(())
}

fn complexity_section_options(options: &ComplexityOptions) -> ComplexitySectionOptions {
    let ownership = options.ownership || options.ownership_emails.is_some();
    let requested_targets = options.targets || options.effort.is_some();
    ComplexitySectionOptions {
        complexity: options.complexity,
        file_scores: options.file_scores,
        coverage_gaps: options.coverage_gaps,
        hotspots: options.hotspots,
        ownership,
        targets: requested_targets,
        css: options.css,
        score: options.score,
    }
}

fn is_health_score_only_output(options: &HealthSectionOptions, score: bool) -> bool {
    score
        && !options.complexity
        && !options.file_scores
        && !options.coverage_gaps
        && !options.hotspots
        && !options.targets
        && !options.trend
}

const fn thresholds_to_engine(
    thresholds: ComplexityThresholdOverrides,
) -> fallow_engine::health::HealthThresholdOverrides {
    fallow_engine::health::HealthThresholdOverrides {
        max_cyclomatic: thresholds.max_cyclomatic,
        max_cognitive: thresholds.max_cognitive,
        max_crap: thresholds.max_crap,
    }
}

const fn complexity_sort_to_engine(sort: ComplexitySort) -> fallow_engine::health::HealthSort {
    match sort {
        ComplexitySort::Severity => fallow_engine::health::HealthSort::Severity,
        ComplexitySort::Cyclomatic => fallow_engine::health::HealthSort::Cyclomatic,
        ComplexitySort::Cognitive => fallow_engine::health::HealthSort::Cognitive,
        ComplexitySort::Lines => fallow_engine::health::HealthSort::Lines,
    }
}

const fn coverage_inputs_to_engine(
    coverage_inputs: ComplexityCoverageInputs<'_>,
) -> fallow_engine::health::HealthCoverageInputs<'_> {
    fallow_engine::health::HealthCoverageInputs {
        coverage: coverage_inputs.coverage,
        coverage_root: coverage_inputs.coverage_root,
        coverage_relocated: coverage_inputs.coverage_relocated,
    }
}

const fn ownership_email_mode_to_config(mode: OwnershipEmailMode) -> EmailMode {
    match mode {
        OwnershipEmailMode::Raw => EmailMode::Raw,
        OwnershipEmailMode::Handle => EmailMode::Handle,
        OwnershipEmailMode::Anonymized => EmailMode::Anonymized,
        OwnershipEmailMode::Hash => EmailMode::Hash,
    }
}

const fn target_effort_to_output(effort: TargetEffort) -> EffortEstimate {
    match effort {
        TargetEffort::Low => EffortEstimate::Low,
        TargetEffort::Medium => EffortEstimate::Medium,
        TargetEffort::High => EffortEstimate::High,
    }
}

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

    #[test]
    fn duplication_defaults_match_cli_contract() {
        let options = DuplicationOptions::default();
        assert!(options.mode.is_none());
        assert!(options.min_tokens.is_none());
        assert!(options.min_lines.is_none());
        assert!(options.min_occurrences.is_none());
    }

    #[test]
    fn programmatic_error_builder_keeps_optional_fields() {
        let error = ProgrammaticError::new("boom", 2)
            .with_code("FALLOW_TEST")
            .with_help("Try again")
            .with_context("analysis.root");

        assert_eq!(error.message, "boom");
        assert_eq!(error.exit_code, 2);
        assert_eq!(error.code.as_deref(), Some("FALLOW_TEST"));
        assert_eq!(error.help.as_deref(), Some("Try again"));
        assert_eq!(error.context.as_deref(), Some("analysis.root"));
    }

    #[test]
    fn dead_code_filters_accept_shared_registry_selectors() {
        for (selector, _) in fallow_types::issue_meta::MCP_ISSUE_TYPE_FLAGS.iter() {
            let mut filters = DeadCodeFilters::default();
            assert!(
                filters.enable_registry_selector(selector),
                "{selector} should be accepted"
            );
        }

        let mut filters = DeadCodeFilters::default();
        assert!(filters.enable_registry_selector("unused-files"));
        assert!(filters.unused_files);
        assert!(filters.enable_registry_selector("boundary-violations"));
        assert!(filters.boundary_violations);
        assert!(!filters.enable_registry_selector("not-a-real-selector"));
    }

    #[test]
    fn default_complexity_options_match_programmatic_health_defaults() {
        let derived = derive_complexity_options(&ComplexityOptions::default());

        assert!(!derived.any_section);
        assert!(derived.complexity);
        assert!(derived.file_scores);
        assert!(!derived.coverage_gaps);
        assert!(derived.hotspots);
        assert!(!derived.ownership);
        assert!(derived.targets);
        assert!(derived.force_full);
        assert!(!derived.score_only_output);
        assert!(derived.score);
    }

    #[test]
    fn score_only_complexity_options_request_score_only_output() {
        let derived = derive_complexity_options(&ComplexityOptions {
            score: true,
            ..ComplexityOptions::default()
        });

        assert!(derived.any_section);
        assert!(!derived.complexity);
        assert!(derived.file_scores);
        assert!(!derived.hotspots);
        assert!(!derived.targets);
        assert!(derived.force_full);
        assert!(derived.score_only_output);
        assert!(derived.score);
    }

    #[test]
    fn ownership_implies_hotspots_when_requested() {
        let derived = derive_complexity_options(&ComplexityOptions {
            ownership: true,
            ..ComplexityOptions::default()
        });

        assert!(derived.any_section);
        assert!(derived.hotspots);
        assert!(derived.ownership);
        assert!(!derived.targets);
    }

    #[test]
    fn complexity_run_options_normalize_public_api_options() {
        let options = ComplexityOptions {
            max_cyclomatic: Some(42),
            max_cognitive: Some(21),
            max_crap: Some(18.5),
            top: Some(7),
            sort: ComplexitySort::Severity,
            complexity_breakdown: true,
            ownership_emails: Some(OwnershipEmailMode::Hash),
            effort: Some(TargetEffort::High),
            coverage: Some(PathBuf::from("coverage/coverage-final.json")),
            coverage_root: Some(PathBuf::from("/ci/workspace")),
            since: Some("30d".to_string()),
            min_commits: Some(4),
            ..ComplexityOptions::default()
        };

        let run = derive_complexity_run_options(&options);

        assert_eq!(run.thresholds.max_cyclomatic, Some(42));
        assert_eq!(run.thresholds.max_cognitive, Some(21));
        assert_eq!(run.thresholds.max_crap, Some(18.5));
        assert_eq!(run.top, Some(7));
        assert!(matches!(run.sort, ComplexitySort::Severity));
        assert!(run.complexity_breakdown);
        assert!(run.sections.hotspots);
        assert!(run.sections.ownership);
        assert!(run.sections.targets);
        assert!(matches!(
            run.ownership_emails,
            Some(OwnershipEmailMode::Hash)
        ));
        assert!(matches!(run.effort, Some(TargetEffort::High)));
        assert_eq!(run.since, Some("30d"));
        assert_eq!(run.min_commits, Some(4));
        assert_eq!(run.coverage_inputs.coverage, options.coverage.as_deref());
        assert_eq!(
            run.coverage_inputs.coverage_root,
            options.coverage_root.as_deref()
        );
    }

    #[test]
    fn complexity_options_validation_accepts_existing_coverage_path_and_absolute_root() {
        let dir = tempfile::tempdir().expect("tempdir");
        let coverage = dir.path().join("coverage-final.json");
        std::fs::write(&coverage, "{}").expect("coverage fixture");

        let result = validate_complexity_options(&ComplexityOptions {
            coverage: Some(coverage),
            coverage_root: Some(PathBuf::from("/ci/workspace")),
            ..ComplexityOptions::default()
        });

        assert!(result.is_ok());
    }

    #[test]
    fn complexity_options_validation_keeps_missing_coverage_error_contract() {
        let err = validate_complexity_options(&ComplexityOptions {
            coverage: Some(PathBuf::from("/missing/coverage-final.json")),
            ..ComplexityOptions::default()
        })
        .expect_err("missing coverage path should fail");

        assert_eq!(err.exit_code, 2);
        assert_eq!(err.code.as_deref(), Some("FALLOW_INVALID_COVERAGE_PATH"));
        assert_eq!(err.context.as_deref(), Some("health.coverage"));
    }

    #[test]
    fn complexity_options_validation_keeps_relative_coverage_root_error_contract() {
        let err = validate_complexity_options(&ComplexityOptions {
            coverage_root: Some(PathBuf::from("coverage")),
            ..ComplexityOptions::default()
        })
        .expect_err("relative coverage root should fail");

        assert_eq!(err.exit_code, 2);
        assert_eq!(err.code.as_deref(), Some("FALLOW_INVALID_COVERAGE_ROOT"));
        assert_eq!(err.context.as_deref(), Some("health.coverage_root"));
    }

    /// #2368: a project-relative coverage path (the documented form of
    /// `health.coverage`) must be checked under the analysis root, not under
    /// the embedder's working directory.
    #[test]
    fn complexity_options_validation_resolves_relative_coverage_against_root() {
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::create_dir_all(dir.path().join("artifacts")).expect("artifacts dir");
        std::fs::write(dir.path().join("artifacts/coverage-final.json"), "{}")
            .expect("coverage fixture");
        let relative = PathBuf::from("artifacts/coverage-final.json");
        assert!(
            !relative.exists(),
            "the fixture must not also exist under the test cwd"
        );

        let result = validate_complexity_options(&ComplexityOptions {
            analysis: AnalysisOptions {
                root: Some(dir.path().to_path_buf()),
                ..AnalysisOptions::default()
            },
            coverage: Some(relative.clone()),
            ..ComplexityOptions::default()
        });
        assert!(result.is_ok(), "{result:?}");

        let err = validate_complexity_options(&ComplexityOptions {
            analysis: AnalysisOptions {
                root: Some(dir.path().join("elsewhere")),
                ..AnalysisOptions::default()
            },
            coverage: Some(relative),
            ..ComplexityOptions::default()
        })
        .expect_err("the path does not exist under the other root");
        assert_eq!(err.code.as_deref(), Some("FALLOW_INVALID_COVERAGE_PATH"));
        assert!(
            err.message.contains("elsewhere"),
            "the message names the resolved path: {}",
            err.message
        );
    }

    #[test]
    fn default_health_sections_match_full_health_output() {
        let derived = derive_health_sections(&HealthSectionOptions {
            output: fallow_types::output_format::OutputFormat::Human,
            complexity: false,
            file_scores: false,
            coverage_gaps: false,
            hotspots: false,
            targets: false,
            css: false,
            score: false,
            score_gate: false,
            snapshot_requested: false,
            trend: false,
        });

        assert!(!derived.any_section);
        assert!(derived.complexity);
        assert!(derived.file_scores);
        assert!(!derived.coverage_gaps);
        assert!(derived.hotspots);
        assert!(derived.targets);
        assert!(derived.score);
        assert!(derived.force_full);
        assert!(!derived.score_only_output);
    }

    #[test]
    fn health_score_gate_requests_score_only_output() {
        let derived = derive_health_sections(&HealthSectionOptions {
            output: fallow_types::output_format::OutputFormat::Human,
            complexity: false,
            file_scores: false,
            coverage_gaps: false,
            hotspots: false,
            targets: false,
            css: false,
            score: false,
            score_gate: true,
            snapshot_requested: false,
            trend: false,
        });

        assert!(derived.any_section);
        assert!(!derived.complexity);
        assert!(derived.file_scores);
        assert!(!derived.hotspots);
        assert!(!derived.targets);
        assert!(derived.score);
        assert!(derived.force_full);
        assert!(derived.score_only_output);
    }

    #[test]
    fn health_snapshot_keeps_full_hidden_inputs_without_section_request() {
        let derived = derive_health_sections(&HealthSectionOptions {
            output: fallow_types::output_format::OutputFormat::Human,
            complexity: false,
            file_scores: false,
            coverage_gaps: false,
            hotspots: false,
            targets: false,
            css: true,
            score: false,
            score_gate: false,
            snapshot_requested: true,
            trend: false,
        });

        assert!(!derived.any_section);
        assert!(derived.css);
        assert!(derived.file_scores);
        assert!(derived.hotspots);
        assert!(derived.score);
        assert!(derived.force_full);
    }
}