keyhog 0.2.0

CLI binary for the secret scanner
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
//! KeyHog CLI: the command-line interface for the secret scanner.
//!
//! Orchestrates source selection, parallel scanning, decode-through analysis,
//! ML confidence scoring, optional live verification, and output formatting.

use std::collections::HashMap;
use std::io;
use std::io::IsTerminal;
use std::path::PathBuf;
use std::process::ExitCode;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Instant;

use anyhow::{Context, Result};
use clap::{Parser, ValueEnum};
use rayon::prelude::*;

use keyhog_core::{
    DedupScope, JsonReporter, JsonlReporter, RawMatch, Reporter,
    SarifReporter, Source, TextReporter, VerificationResult, dedup_matches, load_detectors,
};
use keyhog_scanner::CompiledScanner;
#[cfg(feature = "full")]
use keyhog_scanner::decode;
#[cfg(feature = "full")]
use keyhog_scanner::entropy;
#[cfg(feature = "verify")]
use keyhog_verifier::{VerificationEngine, VerifyConfig};

#[cfg(feature = "full")]
const ENTROPY_ML_THRESHOLD: f64 = 0.5;
const EXIT_LIVE_CREDENTIALS: u8 = 10;
const EXIT_RUNTIME_ERROR: u8 = 2;
const MIN_CONFIDENCE_LOWER_BOUND: f64 = 0.0;
const MIN_CONFIDENCE_UPPER_BOUND: f64 = 1.0;
const INLINE_SUPPRESSION_DIRECTIVE: &str = "keyhog:ignore";
const DETECTOR_DIRECTIVE_PREFIX: &str = "detector=";
const INLINE_COMMENT_MARKERS: &[&str] = &["//", "#", "--", "/*", "<!--"];

/// Amber gradient colors matching the website design (deep amber → light).
const BANNER_COLORS: [(u8, u8, u8); 5] = [
    (245, 158, 11),  // #F59E0B
    (251, 191, 36),  // #FBBF24
    (252, 211, 77),  // #FCD34D
    (253, 230, 138), // #FDE68A
    (254, 243, 199), // #FEF3C7
];

/// Banner line delay in milliseconds for the reveal animation.
const BANNER_LINE_DELAY_MS: u64 = 40;

const BANNER_LINES: [&str; 5] = [
    "  ██   ██ ████████ ██    ██ ██   ██  ██████   ██████",
    "  ██  ██  ██        ██  ██  ██   ██ ██    ██ ██",
    "  █████   █████      ████   ███████ ██    ██ ██   ███",
    "  ██  ██  ██          ██    ██   ██ ██    ██ ██    ██",
    "  ██   ██ ████████    ██    ██   ██  ██████   ██████",
];

/// Print the animated amber-gradient KEYHOG banner to stderr when running
/// interactively. No-ops when stderr is piped or redirected.
fn print_banner(detector_count: usize) {
    if !std::io::stderr().is_terminal() {
        return;
    }

    eprintln!();
    for (line, (r, g, b)) in BANNER_LINES.iter().zip(BANNER_COLORS.iter()) {
        eprint!("\x1b[38;2;{r};{g};{b}m{line}\x1b[0m");
        eprintln!();
        std::thread::sleep(std::time::Duration::from_millis(BANNER_LINE_DELAY_MS));
    }

    let dim = "\x1b[38;2;100;100;100m";
    let reset = "\x1b[0m";
    eprintln!(
        "{dim}  v{version} · Secret Scanner · {detector_count} detectors{reset}",
        version = env!("CARGO_PKG_VERSION"),
    );
    eprintln!("{dim}  by SanthSecurity{reset}");
    eprintln!();
}

// DedupedMatch is now imported from keyhog_core.

#[derive(Parser)]
#[command(
    name = "keyhog",
    about = "KeyHog: The developer-first secret scanner.\nFind leaked credentials in your code before hackers do. Fast, accurate, and verifying.",
    disable_version_flag = true
)]
struct Cli {
    #[command(subcommand)]
    command: Option<Command>,

    /// Print version, build information, and statistics
    #[arg(short = 'V', long)]
    version: bool,
}

#[derive(clap::Subcommand)]
enum Command {
    /// 🔍 Scan files, directories, or repositories for secrets
    ///
    /// Examples:
    ///   keyhog scan --path .
    ///   keyhog scan --path src/ --verify
    ///   keyhog scan --git-diff main
    #[command(verbatim_doc_comment)]
    Scan(Box<ScanArgs>),

    /// 📋 List all loaded secret detectors
    ///
    /// Examples:
    ///   keyhog detectors
    #[command(verbatim_doc_comment)]
    Detectors(DetectorArgs),
}

#[derive(Parser)]
struct ScanArgs {
    /// Detector TOML directory
    #[arg(short, long, default_value = "detectors")]
    detectors: PathBuf,

    /// Positional shorthand for `--path`
    #[arg(value_name = "PATH", conflicts_with = "path")]
    input: Option<PathBuf>,

    /// Scan a directory or file
    #[arg(short, long)]
    path: Option<PathBuf>,

    /// Scan binary files for hardcoded strings
    #[cfg(feature = "binary")]
    #[arg(long)]
    binary: bool,

    /// Scan stdin
    #[arg(long)]
    stdin: bool,

    /// Scan reachable git blobs from repository history (deduplicated by blob ID)
    #[cfg(feature = "git")]
    #[arg(long)]
    git: Option<PathBuf>,

    /// Scan only changed lines between two git refs (e.g., --git-diff main)
    #[cfg(feature = "git")]
    #[arg(long, value_name = "BASE_REF")]
    git_diff: Option<String>,

    /// Scan full git history commit-by-commit using added lines from patches
    #[cfg(feature = "git")]
    #[arg(long, value_name = "PATH")]
    git_history: Option<PathBuf>,

    /// Path to git repository for --git-diff (defaults to current directory)
    #[cfg(feature = "git")]
    #[arg(long, requires = "git_diff")]
    git_diff_path: Option<PathBuf>,

    /// Scan all repositories in a GitHub organization
    #[cfg(feature = "github")]
    #[arg(long, requires = "github_token", value_name = "ORG")]
    github_org: Option<String>,

    /// GitHub personal access token for --github-org
    #[cfg(feature = "github")]
    #[arg(long, requires = "github_org", value_name = "PAT")]
    github_token: Option<String>,

    /// Scan a public or path-style S3 bucket via ListObjectsV2
    #[cfg(feature = "s3")]
    #[arg(long, value_name = "BUCKET")]
    s3_bucket: Option<String>,

    /// Optional S3 object prefix to limit the scan
    #[cfg(feature = "s3")]
    #[arg(long, requires = "s3_bucket", value_name = "PREFIX")]
    s3_prefix: Option<String>,

    /// Optional S3 endpoint for S3-compatible APIs
    #[cfg(feature = "s3")]
    #[arg(long, requires = "s3_bucket", value_name = "URL")]
    s3_endpoint: Option<String>,

    /// Scan a Docker image by unpacking `docker image save`
    #[cfg(feature = "docker")]
    #[arg(long, value_name = "IMAGE")]
    docker_image: Option<String>,

    /// Scan JavaScript, source maps, or WASM binaries at URLs for secrets
    #[cfg(feature = "web")]
    #[arg(long, value_name = "URL", num_args = 1..)]
    url: Option<Vec<String>>,

    /// Max git commits to traverse
    #[cfg(feature = "git")]
    #[arg(long, default_value = "1000")]
    max_commits: usize,

    /// Verify discovered credentials via API calls
    #[cfg(feature = "verify")]
    #[arg(long)]
    verify: bool,

    /// Show full credentials (default: redacted)
    #[arg(long)]
    show_secrets: bool,

    /// Output format
    #[arg(long, default_value = "text", value_enum)]
    format: OutputFormat,

    /// Write findings to file
    #[arg(short, long)]
    output: Option<PathBuf>,

    /// Verification timeout in seconds
    #[arg(long, default_value = "5")]
    timeout: u64,

    /// Max concurrent verification requests per service
    #[arg(long, default_value = "5")]
    rate: usize,

    /// Min severity to report: info, low, medium, high, critical
    #[arg(short, long, value_enum)]
    severity: Option<SeverityFilter>,

    /// Fast mode: pattern matching only. No decode, no entropy. Maximum speed.
    /// Use for pre-commit hooks and quick scans.
    #[arg(long, conflicts_with_all = ["deep", "no_decode", "no_entropy"])]
    fast: bool,

    /// Deep mode: all features enabled. Decode-through, entropy on all files
    /// (ML-gated), multiline joining. Maximum recall. Use for audits.
    #[arg(long, conflicts_with_all = ["fast", "no_decode", "no_entropy"])]
    deep: bool,

    /// Skip decoding base64/hex encoded content
    #[arg(long)]
    no_decode: bool,

    /// Skip entropy-based secret detection
    #[arg(long)]
    no_entropy: bool,

    /// Minimum confidence score (0.0 - 1.0) to report findings
    #[arg(long, value_name = "FLOAT", value_parser = parse_min_confidence)]
    min_confidence: Option<f64>,

    /// Number of parallel scanning threads (default: number of CPU cores)
    #[arg(long, value_name = "N")]
    threads: Option<usize>,

    /// Deduplication scope for findings.
    /// - `credential` (default): same credential across files = one finding
    /// - `file`: same credential in different files = separate findings
    /// - `none`: no dedup, report every match
    #[arg(long, default_value = "credential", value_enum)]
    dedup: CliDedupScope,
}

#[derive(Parser)]
struct DetectorArgs {
    /// Detector TOML directory
    #[arg(short, long, default_value = "detectors")]
    detectors: PathBuf,
}

#[derive(Clone, ValueEnum)]
enum SeverityFilter {
    Info,
    Low,
    Medium,
    High,
    Critical,
}

impl SeverityFilter {
    fn to_severity(&self) -> keyhog_core::Severity {
        match self {
            Self::Info => keyhog_core::Severity::Info,
            Self::Low => keyhog_core::Severity::Low,
            Self::Medium => keyhog_core::Severity::Medium,
            Self::High => keyhog_core::Severity::High,
            Self::Critical => keyhog_core::Severity::Critical,
        }
    }
}

#[derive(Clone, ValueEnum)]
enum OutputFormat {
    Text,
    Json,
    Jsonl,
    Sarif,
}

/// CLI-level dedup scope that maps to [`keyhog_core::DedupScope`].
#[derive(Clone, ValueEnum, PartialEq)]
enum CliDedupScope {
    /// Same credential across all files = one finding (default, best for git history)
    Credential,
    /// Same credential in different files = separate findings (best for filesystem)
    File,
    /// No deduplication — report every pattern match
    None,
}

impl CliDedupScope {
    /// Convert to the core library's [`DedupScope`].
    fn to_core(&self) -> DedupScope {
        match self {
            Self::Credential => DedupScope::Credential,
            Self::File => DedupScope::File,
            Self::None => DedupScope::None,
        }
    }
}

/// On-disk `.keyhog.toml` configuration file that mirrors CLI arguments.
/// CLI flags always override values from the config file.
#[derive(Debug, Default, serde::Deserialize)]
#[serde(deny_unknown_fields, default)]
struct ConfigFile {
    /// Path to detector TOMLs directory.
    detectors: Option<String>,
    /// Minimum severity to report: info, low, medium, high, critical.
    severity: Option<String>,
    /// Output format: text, json, jsonl, sarif.
    format: Option<String>,
    /// Enable fast mode (pattern matching only).
    fast: Option<bool>,
    /// Enable deep mode (all features).
    deep: Option<bool>,
    /// Skip decode-through scanning.
    no_decode: Option<bool>,
    /// Skip entropy-based detection.
    no_entropy: Option<bool>,
    /// Minimum confidence score (0.0 - 1.0).
    min_confidence: Option<f64>,
    /// Number of parallel scanning threads.
    threads: Option<usize>,
    /// Deduplication scope: credential, file, none.
    dedup: Option<String>,
    /// Whether to verify discovered credentials.
    verify: Option<bool>,
    /// Verification timeout in seconds.
    timeout: Option<u64>,
    /// Max concurrent verification requests per service.
    rate: Option<usize>,
    /// Maximum git commits to traverse.
    max_commits: Option<usize>,
    /// Show full credentials (not redacted).
    show_secrets: Option<bool>,
}

/// Search for `.keyhog.toml` starting from the scan root, walking up to the
/// filesystem root. Returns `None` when no config file is found.
fn find_config_file(start: Option<&std::path::Path>) -> Option<PathBuf> {
    let mut dir = start
        .and_then(|p| {
            if p.is_dir() {
                Some(p.to_path_buf())
            } else {
                p.parent().map(std::path::Path::to_path_buf)
            }
        })
        .or_else(|| std::env::current_dir().ok())?;

    loop {
        let candidate = dir.join(".keyhog.toml");
        if candidate.is_file() {
            return Some(candidate);
        }
        if !dir.pop() {
            break;
        }
    }
    None
}

/// Load and merge a `.keyhog.toml` config file into the parsed `ScanArgs`.
/// CLI flags always take precedence over the config file.
#[allow(clippy::collapsible_if, clippy::cmp_owned)]
fn apply_config_file(args: &mut ScanArgs) {
    let config_path = find_config_file(args.path.as_deref());

    let config_path = match config_path {
        Some(path) => path,
        None => return,
    };

    let raw = match std::fs::read_to_string(&config_path) {
        Ok(content) => content,
        Err(error) => {
            tracing::warn!(
                path = %config_path.display(),
                "failed to read .keyhog.toml: {error}"
            );
            return;
        }
    };

    let config: ConfigFile = match toml::from_str(&raw) {
        Ok(parsed) => parsed,
        Err(error) => {
            tracing::warn!(
                path = %config_path.display(),
                "failed to parse .keyhog.toml: {error}"
            );
            return;
        }
    };

    tracing::debug!(path = %config_path.display(), "loaded .keyhog.toml");

    // Apply config values only when no explicit CLI flag was given.
    // clap defaults are identifiable by checking the user-set state via
    // the struct field values matching their defaults.
    if let Some(ref detectors_str) = config.detectors {
        if args.detectors == PathBuf::from("detectors") {
            args.detectors = PathBuf::from(detectors_str);
        }
    }
    if let Some(ref severity_str) = config.severity {
        if args.severity.is_none() {
            args.severity = match severity_str.to_ascii_lowercase().as_str() {
                "info" => Some(SeverityFilter::Info),
                "low" => Some(SeverityFilter::Low),
                "medium" => Some(SeverityFilter::Medium),
                "high" => Some(SeverityFilter::High),
                "critical" => Some(SeverityFilter::Critical),
                other => {
                    tracing::warn!("unknown severity '{other}' in .keyhog.toml");
                    None
                }
            };
        }
    }
    if let Some(ref format_str) = config.format {
        // Only override when the CLI default "text" was not explicitly set.
        // Since we can't distinguish "user passed --format text" from the
        // default, we only apply for non-text values in the config.
        if !matches!(
            args.format,
            OutputFormat::Json | OutputFormat::Jsonl | OutputFormat::Sarif
        ) {
            match format_str.to_ascii_lowercase().as_str() {
                "json" => args.format = OutputFormat::Json,
                "jsonl" => args.format = OutputFormat::Jsonl,
                "sarif" => args.format = OutputFormat::Sarif,
                "text" => {}
                other => tracing::warn!("unknown format '{other}' in .keyhog.toml"),
            }
        }
    }
    if let Some(fast) = config.fast {
        if !args.fast && !args.deep {
            args.fast = fast;
        }
    }
    if let Some(deep) = config.deep {
        if !args.fast && !args.deep {
            args.deep = deep;
        }
    }
    if let Some(no_decode) = config.no_decode {
        if !args.no_decode {
            args.no_decode = no_decode;
        }
    }
    if let Some(no_entropy) = config.no_entropy {
        if !args.no_entropy {
            args.no_entropy = no_entropy;
        }
    }
    if let Some(min_conf) = config.min_confidence {
        if args.min_confidence.is_none() {
            args.min_confidence = Some(min_conf.clamp(0.0, 1.0));
        }
    }
    if let Some(threads) = config.threads {
        if args.threads.is_none() {
            args.threads = Some(threads);
        }
    }
    if let Some(ref dedup_str) = config.dedup {
        if args.dedup == CliDedupScope::Credential {
            match dedup_str.to_ascii_lowercase().as_str() {
                "credential" => {}
                "file" => args.dedup = CliDedupScope::File,
                "none" => args.dedup = CliDedupScope::None,
                other => tracing::warn!("unknown dedup '{other}' in .keyhog.toml"),
            }
        }
    }
    #[cfg(feature = "verify")]
    if let Some(verify) = config.verify {
        if !args.verify {
            args.verify = verify;
        }
    }
    if let Some(timeout) = config.timeout {
        if args.timeout == 5 {
            args.timeout = timeout;
        }
    }
    if let Some(rate) = config.rate {
        if args.rate == 5 {
            args.rate = rate;
        }
    }
    #[cfg(feature = "git")]
    if let Some(max_commits) = config.max_commits {
        if args.max_commits == 1000 {
            args.max_commits = max_commits;
        }
    }
    if let Some(show_secrets) = config.show_secrets {
        if !args.show_secrets {
            args.show_secrets = show_secrets;
        }
    }
}

#[tokio::main]
async fn main() -> ExitCode {
    let is_version = std::env::args().any(|a| a == "-V" || a == "--version");

    tracing_subscriber::fmt()
        .with_writer(std::io::stderr)
        .with_env_filter(
            tracing_subscriber::EnvFilter::from_default_env().add_directive(if is_version {
                "keyhog=error".parse().unwrap_or_else(|_| {
                    tracing_subscriber::filter::Directive::from(tracing::Level::ERROR)
                })
            } else {
                "keyhog=warn".parse().unwrap_or_else(|_| {
                    tracing_subscriber::filter::Directive::from(tracing::Level::INFO)
                })
            }),
        )
        .with_target(false)
        .init();

    let cli = Cli::parse();

    if cli.version {
        println!("KeyHog v{}", env!("CARGO_PKG_VERSION"));
        println!(
            "Build Target: {}-{}",
            std::env::consts::ARCH,
            std::env::consts::OS
        );
        #[cfg(feature = "full")]
        println!(
            "ML Model Version: {}",
            keyhog_scanner::ml_scorer::model_version()
        );
        #[cfg(not(feature = "full"))]
        println!("ML Model Version: disabled");
        #[cfg(feature = "gpu")]
        println!(
            "GPU Acceleration: {}",
            if keyhog_scanner::gpu::gpu_available() {
                "available"
            } else {
                "not available (no compatible GPU)"
            }
        );
        #[cfg(not(feature = "gpu"))]
        println!("GPU Acceleration: not compiled (build with -F gpu)");
        #[cfg(feature = "simd")]
        println!("SIMD Regex:       vectorscan/hyperscan (active)");
        #[cfg(not(feature = "simd"))]
        println!("SIMD Regex:       not compiled (build with -F simd)");
        // Try to count detectors if path exists
        let default_detectors = PathBuf::from("detectors");
        if default_detectors.exists()
            && let Ok(detectors) = keyhog_core::load_detectors(&default_detectors)
        {
            println!("Loaded Detectors: {}", detectors.len());
        }
        return ExitCode::SUCCESS;
    }

    let command_outcome = match cli.command {
        Some(Command::Scan(args)) => run_scan(*args).await,
        Some(Command::Detectors(args)) => list_detectors(args).map(|()| ExitCode::SUCCESS),
        None => {
            use clap::CommandFactory;
            let mut cmd = Cli::command();
            let _ = cmd.print_help();
            return ExitCode::from(0);
        }
    };

    match command_outcome {
        Ok(outcome) => outcome,
        Err(error) => {
            eprintln!("{error:?}");
            ExitCode::from(EXIT_RUNTIME_ERROR)
        }
    }
}

async fn run_scan(mut args: ScanArgs) -> Result<ExitCode> {
    let start = Instant::now();
    if args.path.is_none() {
        args.path = args.input.clone();
    }
    apply_config_file(&mut args);
    // Show banner early so the user sees it during detector loading.
    // The detector count is populated after loading.
    let show_banner = std::io::stderr().is_terminal();
    configure_threads(args.threads);

    let allowlist = load_allowlist(args.path.as_deref());
    validate_cli_path_arg(&args.detectors, "detectors")?;
    // Try cache first for 10x faster startup, fall back to TOML loading
    let cache_path = args.detectors.join(".keyhog-cache.json");
    let detectors = if let Some(cached) =
        keyhog_core::load_detector_cache(&cache_path, &args.detectors)
    {
        tracing::debug!(count = cached.len(), "loaded detectors from cache");
        cached
    } else {
        let loaded = load_detectors(&args.detectors)
            .with_context(|| format!(
                "Oops! We couldn't find the detectors directory at '{}'.\n\n\
                💡 Hint: If this is your first time running KeyHog, you might need to download the default detectors.\n\
                Run `git clone https://github.com/sourcegraph/keyhog-detectors detectors` or specify the correct path using `--detectors <PATH>`.",
                args.detectors.display()
            ))?;
        // Save cache for next run
        if let Err(e) = keyhog_core::save_detector_cache(&loaded, &cache_path) {
            tracing::debug!("failed to save detector cache: {e}");
        }
        loaded
    };
    if show_banner {
        print_banner(detectors.len());
    }
    let scanner = std::sync::Arc::new(
        CompiledScanner::compile(detectors.clone()).context("compiling scanner")?,
    );

    let chunks = collect_chunks(&args)?;

    let (do_decode, do_entropy) = if args.fast {
        (false, false)
    } else if args.deep {
        (true, true)
    } else {
        (!args.no_decode, !args.no_entropy)
    };

    let show_progress = std::io::stderr().is_terminal();
    let all_matches = scan_parallel(&scanner, &chunks, do_decode, do_entropy, show_progress);
    let filtered = filter_and_resolve(all_matches, &args, &allowlist);
    let findings = finalize(filtered, &detectors, &args).await?;
    let has_live_credentials = findings
        .iter()
        .any(|f| matches!(f.verification, VerificationResult::Live));

    report_findings(&findings, &args)?;

    tracing::info!(
        "Done in {:.1}s — {} findings",
        start.elapsed().as_secs_f64(),
        findings.len()
    );

    Ok(if has_live_credentials {
        ExitCode::from(EXIT_LIVE_CREDENTIALS)
    } else if !findings.is_empty() {
        ExitCode::from(1) // Secrets found (unverified or verification skipped)
    } else {
        ExitCode::SUCCESS
    })
}

/// Configure the global rayon thread pool with the user-specified thread count.
///
/// # Limitation
///
/// `rayon::ThreadPoolBuilder::build_global()` can only succeed once per process.
/// Subsequent calls (e.g., when `run_scan` is invoked multiple times in a test
/// or library context) will fail. This is a known rayon design constraint — the
/// global pool is immutable once initialized. The failure is logged at warn
/// level but is non-fatal: rayon falls back to its default pool (num_cpus).
fn configure_threads(threads: Option<usize>) {
    if let Some(n) = threads
        && let Err(error) = rayon::ThreadPoolBuilder::new()
            .num_threads(n)
            .build_global()
    {
        tracing::warn!(
            requested_threads = n,
            "failed to configure rayon thread pool (may already be initialized): {error}"
        );
    }
}

fn load_allowlist(scan_path: Option<&std::path::Path>) -> keyhog_core::allowlist::Allowlist {
    let base_path = scan_path
        .map(allowlist_root)
        .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
    let ignore_path = base_path.join(".keyhogignore");
    if ignore_path.exists() {
        keyhog_core::allowlist::Allowlist::load(&ignore_path)
            .unwrap_or_else(|_| keyhog_core::allowlist::Allowlist::empty())
    } else {
        keyhog_core::allowlist::Allowlist::empty()
    }
}

fn allowlist_root(path: &std::path::Path) -> PathBuf {
    if path.is_dir() {
        path.to_path_buf()
    } else {
        path.parent()
            .map(std::path::Path::to_path_buf)
            .unwrap_or_else(|| PathBuf::from("."))
    }
}

fn collect_chunks(args: &ScanArgs) -> Result<Vec<keyhog_core::Chunk>> {
    let sources: Vec<Box<dyn Source>> = build_sources(args)?;
    if sources.is_empty() {
        anyhow::bail!(
            "no input source specified — use --path, --stdin, --git, --git-diff, --git-history, --github-org, --s3-bucket, or --docker-image"
        );
    }

    let mut chunks = Vec::new();
    for source in &sources {
        for chunk_result in source.chunks() {
            match chunk_result {
                Ok(chunk) => chunks.push(chunk),
                Err(error) => {
                    tracing::warn!("{} source warning: {}", source.name(), error);
                    eprintln!("warning: {} source: {}", source.name(), error);
                }
            }
        }
    }
    Ok(chunks)
}

/// Maximum total findings across all chunks to prevent unbounded memory growth.
const MAX_TOTAL_FINDINGS: usize = 100_000;

/// Per-chunk scan time budget. The `regex` crate guarantees O(n) matching
/// (Thompson NFA, no backtracking), but multiline preprocessing, decode-through
/// loops, and entropy scanning can still produce unbounded wall time on
/// pathological content. Chunks exceeding this budget are skipped.
const PER_CHUNK_SCAN_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);

fn scan_parallel(
    scanner: &std::sync::Arc<CompiledScanner>,
    chunks: &[keyhog_core::Chunk],
    do_decode: bool,
    do_entropy: bool,
    show_progress: bool,
) -> Vec<RawMatch> {
    let progress = ScanProgress::new(chunks.len(), show_progress);
    let timed_out_count = std::sync::atomic::AtomicUsize::new(0);
    let mut result: Vec<RawMatch> = chunks
        .par_iter()
        .flat_map(|chunk| {
            let chunk_start = std::time::Instant::now();

            #[cfg(not(feature = "full"))]
            let _ = (do_decode, do_entropy);

            let matches = scanner.scan(chunk);
            #[cfg(feature = "full")]
            let mut matches = matches;

            // Check timeout after pattern scan
            if chunk_start.elapsed() > PER_CHUNK_SCAN_TIMEOUT {
                timed_out_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                tracing::warn!(
                    path = ?chunk.metadata.path,
                    elapsed = ?chunk_start.elapsed(),
                    "chunk scan exceeded time budget, skipping decode/entropy"
                );
                progress.tick();
                return matches;
            }

            #[cfg(feature = "full")]
            if do_decode {
                for decoded in decode::decode_chunk(chunk) {
                    matches.extend(scanner.scan(&decoded));
                    if chunk_start.elapsed() > PER_CHUNK_SCAN_TIMEOUT {
                        tracing::warn!(
                            path = ?chunk.metadata.path,
                            "chunk decode-through exceeded time budget"
                        );
                        break;
                    }
                }
            }

            #[cfg(feature = "full")]
            let is_config = entropy::is_entropy_appropriate(chunk.metadata.path.as_deref());

            #[cfg(feature = "full")]
            if do_entropy && is_config && chunk_start.elapsed() <= PER_CHUNK_SCAN_TIMEOUT {
                for em in entropy::find_entropy_secrets(&chunk.data, 16, 2) {
                    let ml_context = entropy_context_window(&chunk.data, em.line, 2);
                    let ml_score = keyhog_scanner::ml_scorer::score(&em.value, &ml_context);
                    if ml_score < ENTROPY_ML_THRESHOLD {
                        continue;
                    }

                    let conf = compute_entropy_confidence(&em);
                    let blended_conf = 0.6 * ml_score + 0.4 * conf;
                    matches.push(RawMatch {
                        detector_id: "entropy".into(),
                        detector_name: "High-Entropy String".into(),
                        service: "generic".into(),
                        severity: keyhog_core::Severity::Medium,
                        credential: em.value.clone(),
                        companion: None,
                        location: MatchLocation {
                            source: chunk.metadata.source_type.clone(),
                            file_path: chunk.metadata.path.clone(),
                            line: Some(em.line),
                            offset: em.offset,
                            commit: chunk.metadata.commit.clone(),
                            author: chunk.metadata.author.clone(),
                            date: chunk.metadata.date.clone(),
                        },
                        entropy: Some(em.entropy),
                        confidence: Some(blended_conf),
                    });
                }
            }

            progress.tick();
            matches
        })
        .collect::<Vec<_>>();

    let timed_out = timed_out_count.load(std::sync::atomic::Ordering::Relaxed);
    if timed_out > 0 {
        tracing::warn!("{timed_out} chunk(s) exceeded the {PER_CHUNK_SCAN_TIMEOUT:?} scan budget");
    }

    if result.len() > MAX_TOTAL_FINDINGS {
        // Sort by severity (Critical first) so truncation keeps the most
        // important findings. Without sorting, rayon's parallel iteration
        // order is non-deterministic — truncation would discard findings
        // arbitrarily.
        result.sort_by(|a, b| b.severity.cmp(&a.severity));
        tracing::warn!(
            "findings truncated: {} found, keeping top {} by severity",
            result.len(),
            MAX_TOTAL_FINDINGS
        );
        result.truncate(MAX_TOTAL_FINDINGS);
    }
    result
}

#[cfg(feature = "full")]
fn entropy_context_window(data: &str, line: usize, radius: usize) -> String {
    let lines: Vec<&str> = data.lines().collect();
    if lines.is_empty() {
        return String::new();
    }
    let start = line.saturating_sub(radius + 1);
    let end = (line + radius).min(lines.len());
    lines[start..end].join("\n")
}

fn filter_and_resolve(
    mut matches: Vec<RawMatch>,
    args: &ScanArgs,
    allowlist: &keyhog_core::allowlist::Allowlist,
) -> Vec<RawMatch> {
    // Severity filter.
    if let Some(ref min_sev) = args.severity {
        let min = min_sev.to_severity();
        matches.retain(|m| m.severity >= min);
    }
    // Confidence filter.
    if let Some(min_conf) = args.min_confidence {
        matches.retain(|m| m.confidence.map(|c| c >= min_conf).unwrap_or(true));
    }
    // Resolution: most specific detector wins.
    matches = keyhog_scanner::resolution::resolve_matches(matches);
    // Allowlist.
    matches.retain(|m| {
        if let Some(ref path) = m.location.file_path
            && allowlist.is_path_ignored(path)
        {
            return false;
        }
        !allowlist.ignored_detectors.contains(&m.detector_id)
            && !allowlist.is_hash_allowed(&m.credential)
    });
    matches = filter_inline_suppressions(matches);
    matches
}

// dedup_matches is now imported from keyhog_core.

fn parse_min_confidence(value: &str) -> Result<f64, String> {
    let parsed = value
        .parse::<f64>()
        .map_err(|_| format!("invalid float value: {value}"))?;
    if !parsed.is_finite() {
        return Err("min_confidence must be finite".into());
    }
    if !(MIN_CONFIDENCE_LOWER_BOUND..=MIN_CONFIDENCE_UPPER_BOUND).contains(&parsed) {
        return Err(format!(
            "min_confidence must be between {MIN_CONFIDENCE_LOWER_BOUND} and {MIN_CONFIDENCE_UPPER_BOUND}"
        ));
    }
    Ok(parsed)
}

struct ScanProgress {
    total: usize,
    current: AtomicUsize,
    print_lock: Mutex<()>,
    enabled: bool,
}

impl ScanProgress {
    fn new(total: usize, enabled: bool) -> Arc<Self> {
        Arc::new(Self {
            total,
            current: AtomicUsize::new(0),
            print_lock: Mutex::new(()),
            enabled: enabled && total > 0,
        })
    }

    fn tick(&self) {
        if !self.enabled {
            return;
        }
        let current = self.current.fetch_add(1, Ordering::SeqCst) + 1;
        if current == 1 || current == self.total || current.is_multiple_of(100) {
            // SAFETY: terminal writes stay serialized through `print_lock`, so
            // concurrent workers cannot interleave partial progress output.
            let _guard = match self.print_lock.lock() {
                Ok(guard) => guard,
                // SAFETY: if a rayon worker panicked while holding the lock,
                // recover the inner guard instead of cascade-panicking all
                // remaining workers.
                Err(poisoned) => poisoned.into_inner(),
            };
            eprint!("\rscanning {current}/{} chunks", self.total);
            if current == self.total {
                eprintln!();
            }
        }
    }
}

fn filter_inline_suppressions(matches: Vec<RawMatch>) -> Vec<RawMatch> {
    let mut line_cache: HashMap<String, Option<Vec<String>>> = HashMap::new();

    matches
        .into_iter()
        .filter(|m| {
            if m.location.source != "filesystem" {
                return true;
            }
            let Some(path) = m.location.file_path.as_ref() else {
                return true;
            };
            let Some(line) = m.location.line else {
                return true;
            };
            let lines = line_cache.entry(path.clone()).or_insert_with(|| {
                std::fs::read_to_string(path)
                    .ok()
                    .map(|text| text.lines().map(str::to_string).collect::<Vec<_>>())
            });
            !is_inline_suppressed(lines.as_ref().map(Vec::as_slice), line, &m.detector_id)
        })
        .collect()
}

fn is_inline_suppressed(lines: Option<&[String]>, line: usize, detector_id: &str) -> bool {
    let Some(lines) = lines else {
        return false;
    };
    [line.saturating_sub(1), line]
        .into_iter()
        .filter(|line_number| *line_number > 0)
        .filter_map(|line_number| lines.get(line_number - 1))
        .any(|line_text| line_has_inline_suppression(line_text, detector_id))
}

fn line_has_inline_suppression(line: &str, detector_id: &str) -> bool {
    let Some(directive) = inline_suppression_directive(line) else {
        return false;
    };
    let detector = detector_id.to_ascii_lowercase();
    match directive
        .split(|ch: char| ch.is_whitespace() || matches!(ch, ',' | ';'))
        .find_map(|token| token.strip_prefix(DETECTOR_DIRECTIVE_PREFIX))
    {
        Some(expected) => expected == detector,
        None => true,
    }
}

fn inline_suppression_directive(line: &str) -> Option<String> {
    let lower = line.to_ascii_lowercase();
    comment_segments(&lower).find_map(extract_directive_from_comment)
}

fn comment_segments(line: &str) -> impl Iterator<Item = &str> {
    INLINE_COMMENT_MARKERS
        .iter()
        .filter_map(|marker| line.find(marker).map(|index| &line[index + marker.len()..]))
}

fn extract_directive_from_comment(comment: &str) -> Option<String> {
    let directive_index = comment.find(INLINE_SUPPRESSION_DIRECTIVE)?;
    if comment[..directive_index]
        .chars()
        .any(|character| !character.is_whitespace())
    {
        return None;
    }
    let directive = &comment[directive_index..];
    let token_end = directive
        .find(char::is_whitespace)
        .map_or(directive.len(), |index| index);
    if &directive[..token_end] != INLINE_SUPPRESSION_DIRECTIVE {
        return None;
    }
    Some(directive.to_string())
}

async fn finalize(
    matches: Vec<RawMatch>,
    detectors: &[keyhog_core::DetectorSpec],
    args: &ScanArgs,
) -> Result<Vec<keyhog_core::VerifiedFinding>> {
    let mut groups = dedup_matches(matches, &args.dedup.to_core());
    groups.sort_by(|a, b| b.severity.cmp(&a.severity));

    #[cfg(feature = "verify")]
    if args.verify {
        let secrets: Vec<String> = groups
            .iter()
            .map(|group| group.credential.clone())
            .collect();
        let config = VerifyConfig {
            timeout: std::time::Duration::from_secs(args.timeout),
            max_concurrent_per_service: args.rate,
            ..Default::default()
        };
        let verify_groups = groups;
        let mut findings = VerificationEngine::new(detectors, config)?
            .verify_all(verify_groups)
            .await;

        if args.show_secrets {
            for (finding, secret) in findings.iter_mut().zip(secrets) {
                finding.credential_redacted = secret;
            }
        }

        return Ok(findings);
    }

    let _ = detectors;
    Ok(groups
        .into_iter()
        .map(|g| keyhog_core::VerifiedFinding {
            detector_id: g.detector_id,
            detector_name: g.detector_name,
            service: g.service,
            severity: g.severity,
            credential_redacted: if args.show_secrets {
                g.credential.clone()
            } else {
                keyhog_core::redact(&g.credential)
            },
            location: g.primary_location,
            verification: VerificationResult::Skipped,
            metadata: HashMap::new(),
            additional_locations: g.additional_locations,
            confidence: g.confidence,
        })
        .collect())
}

fn report_findings(findings: &[keyhog_core::VerifiedFinding], args: &ScanArgs) -> Result<()> {
    if let Some(ref path) = args.output {
        let file = std::fs::File::create(path)
            .with_context(|| format!("creating output file {}", path.display()))?;
        let w = io::BufWriter::new(file);
        report_with(w, &args.format, false, findings)
    } else {
        let w = io::BufWriter::new(io::stdout().lock());
        report_with(w, &args.format, io::stdout().is_terminal(), findings)
    }
}

fn report_with<W: std::io::Write + 'static>(
    w: W,
    format: &OutputFormat,
    color: bool,
    findings: &[keyhog_core::VerifiedFinding],
) -> Result<()> {
    match format {
        OutputFormat::Text => finish_reporter(TextReporter::with_color(w, color), findings),
        OutputFormat::Json => finish_reporter(JsonReporter::new(w), findings),
        OutputFormat::Jsonl => finish_reporter(JsonlReporter::new(w), findings),
        OutputFormat::Sarif => finish_reporter(SarifReporter::new(w), findings),
    }
}

fn finish_reporter<R: Reporter>(
    mut reporter: R,
    findings: &[keyhog_core::VerifiedFinding],
) -> Result<()> {
    for finding in findings {
        reporter.report(finding)?;
    }
    reporter.finish()?;
    Ok(())
}

fn build_sources(args: &ScanArgs) -> Result<Vec<Box<dyn Source>>> {
    let mut sources: Vec<Box<dyn Source>> = Vec::new();

    if let Some(ref path) = args.path {
        // Always scan filesystem (text files)
        sources.push(Box::new(keyhog_sources::FilesystemSource::new(
            path.clone(),
        )));
        // Additionally scan binary files when --binary flag is set
        #[cfg(feature = "binary")]
        if args.binary {
            sources.push(Box::new(keyhog_sources::BinarySource::new(path.clone())));
        }
    }

    if args.stdin {
        sources.push(Box::new(keyhog_sources::StdinSource));
    }

    #[cfg(feature = "git")]
    if let Some(ref path) = args.git {
        sources.push(Box::new(
            keyhog_sources::GitSource::new(path.clone()).with_max_commits(args.max_commits),
        ));
    }

    #[cfg(feature = "git")]
    if let Some(ref base_ref) = args.git_diff {
        let repo_path = args
            .git_diff_path
            .clone()
            .unwrap_or_else(|| PathBuf::from("."));
        sources.push(Box::new(keyhog_sources::GitDiffSource::new(
            repo_path,
            base_ref.clone(),
        )));
    }

    #[cfg(feature = "git")]
    if let Some(ref path) = args.git_history {
        sources.push(Box::new(
            keyhog_sources::GitHistorySource::new(path.clone()).with_max_commits(args.max_commits),
        ));
    }

    #[cfg(feature = "github")]
    if let (Some(org), Some(token)) = (&args.github_org, &args.github_token) {
        sources.push(Box::new(keyhog_sources::GitHubOrgSource::new(
            org.clone(),
            token.clone(),
        )));
    }

    #[cfg(feature = "s3")]
    if let Some(bucket) = &args.s3_bucket {
        let mut source = keyhog_sources::S3Source::new(bucket.clone());
        if let Some(prefix) = &args.s3_prefix {
            source = source.with_prefix(prefix.clone());
        }
        if let Some(endpoint) = &args.s3_endpoint {
            source = source.with_endpoint(endpoint.clone());
        }
        sources.push(Box::new(source));
    }

    #[cfg(feature = "docker")]
    if let Some(image) = &args.docker_image {
        sources.push(Box::new(keyhog_sources::DockerImageSource::new(
            image.clone(),
        )));
    }

    #[cfg(feature = "web")]
    if let Some(urls) = &args.url {
        sources.push(Box::new(keyhog_sources::WebSource::new(urls.clone())));
    }

    Ok(sources)
}

/// Compute confidence score for entropy-based findings.
/// Based on entropy value (higher = more confident) and keyword proximity.
#[cfg(feature = "full")]
fn compute_entropy_confidence(em: &entropy::EntropyMatch) -> f64 {
    // Base confidence from entropy: scale 4.0-5.5 to 0.5-0.9
    let entropy_score = if em.entropy >= 5.5 {
        0.9
    } else if em.entropy >= 4.5 {
        0.7 + (em.entropy - 4.5) * 0.2
    } else {
        0.5 + (em.entropy - 4.0) * 0.4
    };

    // Boost for keyword proximity (already factored into finding the match)
    // and reasonable length (16-64 chars is ideal for secrets)
    let length_boost = if em.value.len() >= 16 && em.value.len() <= 64 {
        0.1
    } else {
        0.0
    };

    (entropy_score + length_boost).clamp(0.0, 1.0)
}

fn validate_cli_path_arg(path: &std::path::Path, arg_name: &str) -> Result<()> {
    let path_display = path.to_string_lossy();
    // SAFETY: these paths are forwarded to filesystem and subprocess APIs as
    // trusted argv/path inputs later, so reject option-like and control-char
    // values at the CLI boundary before deeper code tries to interpret them.
    if path_display.starts_with('-') || path_display.chars().any(char::is_control) {
        anyhow::bail!("{arg_name} path contains unsafe characters");
    }
    Ok(())
}

fn list_detectors(args: DetectorArgs) -> Result<()> {
    validate_cli_path_arg(&args.detectors, "detectors")?;
    let detectors = load_detectors(&args.detectors)
        .with_context(|| format!("loading detectors from {}", args.detectors.display()))?;

    println!("{:<30} {:<25} {:<10} NAME", "ID", "SERVICE", "SEVERITY");
    println!("{}", "-".repeat(80));

    for d in &detectors {
        let has_verify = if d.verify.is_some() { "" } else { " " };
        println!(
            "{:<30} {:<25} {:<10} {} {}",
            d.id, d.service, d.severity, has_verify, d.name
        );
    }

    println!("\n{} detectors loaded", detectors.len());
    Ok(())
}

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

    #[test]
    fn allowlist_root_uses_parent_for_files() {
        let path = std::path::Path::new("/tmp/project/src/main.rs");
        assert_eq!(allowlist_root(path), PathBuf::from("/tmp/project/src"));
    }

    #[test]
    fn load_allowlist_falls_back_to_current_dir() {
        let temp_dir = tempfile::tempdir().unwrap();
        let original_dir = std::env::current_dir().unwrap();
        std::fs::write(
            temp_dir.path().join(".keyhogignore"),
            "detector:test-detector\n",
        )
        .unwrap();

        std::env::set_current_dir(temp_dir.path()).unwrap();
        let allowlist = load_allowlist(None);
        std::env::set_current_dir(original_dir).unwrap();

        assert!(allowlist.ignored_detectors.contains("test-detector"));
    }

    #[test]
    fn detector_path_validation_rejects_option_like_paths() {
        let error =
            validate_cli_path_arg(std::path::Path::new("-detectors"), "detectors").unwrap_err();
        assert!(error.to_string().contains("unsafe"));
    }

    #[test]
    fn detector_path_validation_rejects_control_chars() {
        let error =
            validate_cli_path_arg(std::path::Path::new("bad\npath"), "detectors").unwrap_err();
        assert!(error.to_string().contains("unsafe"));
    }

    #[test]
    fn generic_inline_suppression_matches_same_or_previous_line() {
        let lines = vec![
            "# keyhog:ignore".to_string(),
            "GITHUB_TOKEN=ghp_abcdefghijklmnopqrstuvwxyz1234567890".to_string(),
        ];
        assert!(is_inline_suppressed(Some(&lines), 2, "github-token"));

        let inline = vec![
            "GITHUB_TOKEN=ghp_abcdefghijklmnopqrstuvwxyz1234567890 // keyhog:ignore".to_string(),
        ];
        assert!(is_inline_suppressed(Some(&inline), 1, "github-token"));
    }

    #[test]
    fn detector_specific_inline_suppression_is_scoped() {
        let lines = ["# keyhog:ignore detector=github-token".to_string()];
        assert!(line_has_inline_suppression(&lines[0], "github-token"));
        assert!(!line_has_inline_suppression(&lines[0], "openai-api-key"));
    }

    #[test]
    fn inline_suppression_is_case_insensitive_and_comment_scoped() {
        assert!(line_has_inline_suppression(
            "token=ghp_abc // KeyHog:Ignore detector=GitHub-Token",
            "github-token"
        ));
        assert!(!line_has_inline_suppression(
            "token=\"KeyHog:Ignore detector=github-token\"",
            "github-token"
        ));
    }

    #[test]
    fn min_confidence_rejects_nan_and_infinity() {
        assert!(parse_min_confidence("NaN").is_err());
        assert!(parse_min_confidence("inf").is_err());
        assert!(parse_min_confidence("-inf").is_err());
        assert_eq!(parse_min_confidence("0.5").unwrap(), 0.5);
    }

    #[test]
    #[cfg(feature = "full")]
    fn entropy_context_window_includes_neighboring_lines() {
        let text = "one\ntwo\nthree\nfour\nfive";
        assert_eq!(
            super::entropy_context_window(text, 3, 1),
            "two\nthree\nfour"
        );
    }

    // =========================================================================
    // .keyhog.toml config file tests
    // =========================================================================

    #[test]
    fn config_file_deserialization_full() {
        let toml_str = r#"
            detectors = "custom/detectors"
            severity = "high"
            format = "json"
            fast = false
            deep = true
            no_decode = false
            no_entropy = false
            min_confidence = 0.7
            threads = 8
            dedup = "file"
            verify = true
            timeout = 10
            rate = 3
            max_commits = 500
            show_secrets = true
        "#;
        let config: ConfigFile = toml::from_str(toml_str).unwrap();
        assert_eq!(config.detectors.as_deref(), Some("custom/detectors"));
        assert_eq!(config.severity.as_deref(), Some("high"));
        assert_eq!(config.format.as_deref(), Some("json"));
        assert_eq!(config.deep, Some(true));
        assert_eq!(config.fast, Some(false));
        assert_eq!(config.min_confidence, Some(0.7));
        assert_eq!(config.threads, Some(8));
        assert_eq!(config.dedup.as_deref(), Some("file"));
        assert_eq!(config.timeout, Some(10));
        assert_eq!(config.rate, Some(3));
        assert_eq!(config.max_commits, Some(500));
        assert_eq!(config.show_secrets, Some(true));
    }

    #[test]
    fn config_file_deserialization_empty() {
        let config: ConfigFile = toml::from_str("").unwrap();
        assert!(config.detectors.is_none());
        assert!(config.severity.is_none());
        assert!(config.fast.is_none());
    }

    #[test]
    fn config_file_deserialization_rejects_unknown_fields() {
        let toml_str = r#"unknown_field = "value""#;
        let result: Result<ConfigFile, _> = toml::from_str(toml_str);
        assert!(result.is_err());
    }

    #[test]
    fn config_file_partial_fields() {
        let toml_str = r#"
            severity = "medium"
            threads = 4
        "#;
        let config: ConfigFile = toml::from_str(toml_str).unwrap();
        assert_eq!(config.severity.as_deref(), Some("medium"));
        assert_eq!(config.threads, Some(4));
        assert!(config.detectors.is_none());
        assert!(config.fast.is_none());
    }

    #[test]
    fn find_config_walks_parent_directories() {
        let tmp = tempfile::tempdir().unwrap();
        let nested = tmp.path().join("a").join("b").join("c");
        std::fs::create_dir_all(&nested).unwrap();
        let config_path = tmp.path().join(".keyhog.toml");
        std::fs::write(&config_path, "severity = \"high\"").unwrap();

        let found = find_config_file(Some(&nested));
        assert_eq!(found, Some(config_path));
    }

    #[test]
    fn find_config_returns_none_when_absent() {
        let tmp = tempfile::tempdir().unwrap();
        let found = find_config_file(Some(tmp.path()));
        // The config could exist in parent dirs, but in /tmp it almost certainly won't
        // match the project root. Best-effort: verify the function doesn't panic.
        let _ = found;
    }
}