keyhog 0.5.85

GPU-accelerated secret scanner for code, Git history, cloud, containers, browser assets, and live credential verification
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
//! Regression e2e & performance simulation: guarantee that massive staged diffs
//! (1,000, 5,000, and 10,000 files) execute within milliseconds when served by
//! the perpetual guard daemon, and verify edge cases including staged renames
//! and mode changes (executable +x bits).

use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Mutex;
use std::time::Instant;
use tempfile::TempDir;

fn keyhog() -> PathBuf {
    PathBuf::from(env!("CARGO_BIN_EXE_keyhog"))
}

static DAEMON_TEST_MUTEX: Mutex<()> = Mutex::new(());

fn daemon_slot() -> std::sync::MutexGuard<'static, ()> {
    DAEMON_TEST_MUTEX.lock().unwrap_or_else(|e| e.into_inner())
}

fn init_git_repo(dir: &Path) {
    let out = Command::new("git")
        .args(["init", "-q", "-b", "main"])
        .current_dir(dir)
        .output()
        .expect("git init");
    assert!(out.status.success(), "git init must succeed");
    let _ = Command::new("git")
        .args(["config", "user.email", "guard-sim@test.local"])
        .current_dir(dir)
        .output();
    let _ = Command::new("git")
        .args(["config", "user.name", "Guard Sim"])
        .current_dir(dir)
        .output();
}

/// WHY: Guarantee that 1,000 staged clean files pass pre-commit gating with
/// sub-second initial registration and single-digit millisecond cached passes,
/// and that partial diff mutations (e.g. 5 modified files) only stream the
/// modified blobs while serving the 995 clean entries from daemon memory.
#[cfg(unix)]
#[test]
fn massive_1000_file_diff_guarantees_millisecond_gating() {
    let _daemon_slot = daemon_slot();
    let dir = TempDir::new().unwrap();
    let repo = dir.path();
    init_git_repo(repo);
    let socket = repo.join("guard-massive-1000.sock");

    // 1. Start daemon on dedicated test socket.
    let up_out = Command::new(keyhog())
        .env("NO_COLOR", "1")
        .args(["guard", "up", "--backend", "cpu", "--socket"])
        .arg(&socket)
        .output()
        .expect("guard up");
    assert_eq!(up_out.status.code(), Some(0), "guard up must succeed");

    // 2. Generate 1,000 clean synthetic files.
    for i in 0..1000 {
        let filename = format!("src/module_{i:04}.rs");
        let path = repo.join(&filename);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(&path, format!("pub fn func_{i}() -> usize {{ {i} }}\n")).unwrap();
    }

    // 3. Stage all 1,000 files in Git index.
    let git_add = Command::new("git")
        .args(["add", "."])
        .current_dir(repo)
        .output()
        .expect("git add");
    assert!(git_add.status.success(), "git add 1000 files must succeed");

    // 4. Register repo with guard (indexes baseline into daemon memory).
    let add_out = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args(["guard", "add", ".", "--socket"])
        .arg(&socket)
        .output()
        .expect("guard add");
    assert!(
        matches!(add_out.status.code(), Some(0 | 13)),
        "guard add must succeed"
    );

    // 5. Initial staged scan simulation: 1,000 files in pre-commit hook.
    // Must hit 1,000 cache hits and complete in single-digit ms transaction time.
    let start = Instant::now();
    let scan_out = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("keyhog scan --fast --git-staged");
    let duration = start.elapsed();

    assert_eq!(
        scan_out.status.code(),
        Some(0),
        "initial staged scan must exit 0"
    );
    let scan_stderr = String::from_utf8_lossy(&scan_out.stderr);
    assert!(
        scan_stderr.contains("1000 cache hit(s)") || scan_stderr.contains("1000 blob(s) scanned"),
        "must process all 1000 files via guard; stderr={scan_stderr}"
    );

    // 6. Second staged scan: 100% clean cache hits.
    let start_cached = Instant::now();
    let scan_cached = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("keyhog scan --fast --git-staged (cached)");
    let cached_duration = start_cached.elapsed();

    assert_eq!(
        scan_cached.status.code(),
        Some(0),
        "cached staged scan must exit 0"
    );
    let cached_stderr = String::from_utf8_lossy(&scan_cached.stderr);
    assert!(
        cached_stderr.contains("1000 cache hit(s)"),
        "second pass must be 100% clean cache hits; stderr={cached_stderr}"
    );

    println!("1,000-file initial scan elapsed: {duration:?}");
    println!("1,000-file cached scan elapsed: {cached_duration:?}");

    // 7. Mixed diff test: mutate 5 files out of 1,000.
    for i in 0..5 {
        let filename = format!("src/module_{i:04}.rs");
        let path = repo.join(&filename);
        std::fs::write(
            &path,
            format!("pub fn func_{i}() -> usize {{ {i} + 42 }}\n"),
        )
        .unwrap();
    }
    let git_add_mutated = Command::new("git")
        .args([
            "add",
            "src/module_0000.rs",
            "src/module_0001.rs",
            "src/module_0002.rs",
            "src/module_0003.rs",
            "src/module_0004.rs",
        ])
        .current_dir(repo)
        .output()
        .expect("git add mutated");
    assert!(git_add_mutated.status.success(), "git add mutated files");

    let start_mixed = Instant::now();
    let scan_mixed = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("keyhog scan --fast --git-staged (mixed)");
    let mixed_duration = start_mixed.elapsed();

    assert_eq!(
        scan_mixed.status.code(),
        Some(0),
        "mixed staged scan must exit 0"
    );
    let mixed_stderr = String::from_utf8_lossy(&scan_mixed.stderr);
    assert!(
        mixed_stderr.contains("995 cache hit(s)") && mixed_stderr.contains("5 blob(s) scanned"),
        "mixed diff must serve 995 hits and stream only 5 blobs; stderr={mixed_stderr}"
    );
    println!("1,000-file mixed scan (5 changed, 995 cached) elapsed: {mixed_duration:?}");

    // 8. Clean shutdown.
    let _ = Command::new(keyhog())
        .args(["guard", "down", "--socket"])
        .arg(&socket)
        .output();
}

/// WHY: Guarantee scaling across large 5,000-file staged diffs, ensuring
/// binary DIRC index parsing, memory allocation, and IPC wire serialization
/// scale linearly without timing out or dropping entries.
#[cfg(unix)]
#[test]
fn massive_5000_file_diff_guarantees_millisecond_gating() {
    let _daemon_slot = daemon_slot();
    let dir = TempDir::new().unwrap();
    let repo = dir.path();
    init_git_repo(repo);
    let socket = repo.join("guard-massive-5000.sock");

    // 1. Start daemon on dedicated test socket.
    let up_out = Command::new(keyhog())
        .env("NO_COLOR", "1")
        .args(["guard", "up", "--backend", "cpu", "--socket"])
        .arg(&socket)
        .output()
        .expect("guard up");
    assert_eq!(up_out.status.code(), Some(0), "guard up must succeed");

    // 2. Generate 5,000 clean synthetic files.
    for i in 0..5000 {
        let filename = format!("src/pkg_{}/module_{i:05}.rs", i / 500);
        let path = repo.join(&filename);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(&path, format!("pub fn func_{i}() -> usize {{ {i} * 2 }}\n")).unwrap();
    }

    // 3. Stage all 5,000 files in Git index.
    let git_add = Command::new("git")
        .args(["add", "."])
        .current_dir(repo)
        .output()
        .expect("git add");
    assert!(git_add.status.success(), "git add 5000 files must succeed");

    // 4. Register repo with guard (indexes baseline into daemon memory).
    let add_out = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args(["guard", "add", ".", "--socket"])
        .arg(&socket)
        .output()
        .expect("guard add");
    assert!(
        matches!(add_out.status.code(), Some(0 | 13)),
        "guard add must succeed"
    );

    // 5. Initial staged scan simulation: 5,000 files in pre-commit hook.
    let start = Instant::now();
    let scan_out = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("keyhog scan --fast --git-staged (5000 files)");
    let duration = start.elapsed();

    assert_eq!(
        scan_out.status.code(),
        Some(0),
        "initial staged 5000 scan must exit 0"
    );
    let scan_stderr = String::from_utf8_lossy(&scan_out.stderr);
    assert!(
        scan_stderr.contains("5000 cache hit(s)") || scan_stderr.contains("5000 blob(s) scanned"),
        "must process all 5000 files via guard; stderr={scan_stderr}"
    );

    // 6. Second staged scan: 100% clean cache hits.
    let start_cached = Instant::now();
    let scan_cached = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("keyhog scan --fast --git-staged (5000 cached)");
    let cached_duration = start_cached.elapsed();

    assert_eq!(
        scan_cached.status.code(),
        Some(0),
        "cached staged 5000 scan must exit 0"
    );
    let cached_stderr = String::from_utf8_lossy(&scan_cached.stderr);
    assert!(
        cached_stderr.contains("5000 cache hit(s)"),
        "second pass must be 100% clean cache hits for 5000 files; stderr={cached_stderr}"
    );

    println!("5,000-file initial scan elapsed: {duration:?}");
    println!("5,000-file cached scan elapsed: {cached_duration:?}");

    // 7. Clean shutdown.
    let _ = Command::new(keyhog())
        .args(["guard", "down", "--socket"])
        .arg(&socket)
        .output();
}

/// WHY: Test 10,000-staged-file diff scaling, verifying that massive enterprise
/// repository monorepo diffs with ten thousand staged files stay within the
/// daemon's MAX_GUARD_MANIFEST_ENTRIES capacity limit (100,000 entries), execute
/// the entire manifest acquisition, wire serialization, and cache attestation
/// in rapid time, and serve mixed diffs without memory bloat or race condition errors.
#[cfg(unix)]
#[test]
fn massive_10000_file_diff_scaling_guarantees_millisecond_gating() {
    let _daemon_slot = daemon_slot();
    let dir = TempDir::new().unwrap();
    let repo = dir.path();
    init_git_repo(repo);
    let socket = repo.join("guard-massive-10000.sock");

    // 1. Start daemon on dedicated test socket.
    let up_out = Command::new(keyhog())
        .env("NO_COLOR", "1")
        .args(["guard", "up", "--backend", "cpu", "--socket"])
        .arg(&socket)
        .output()
        .expect("guard up");
    assert_eq!(up_out.status.code(), Some(0), "guard up must succeed");

    // 2. Generate 10,000 clean synthetic files.
    for i in 0..10000 {
        let filename = format!("src/pkg_{:03}/module_{:05}.rs", i / 500, i);
        let path = repo.join(&filename);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(
            &path,
            format!("pub fn compute_{i}() -> usize {{ {i} * 5 }}\n"),
        )
        .unwrap();
    }

    // 3. Stage all 10,000 files in Git index.
    let git_add = Command::new("git")
        .args(["add", "."])
        .current_dir(repo)
        .output()
        .expect("git add 10000");
    assert!(git_add.status.success(), "git add 10000 files must succeed");

    // 4. Register repo with guard (indexes baseline into daemon memory).
    let add_out = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args(["guard", "add", ".", "--socket"])
        .arg(&socket)
        .output()
        .expect("guard add");
    assert!(
        matches!(add_out.status.code(), Some(0 | 13)),
        "guard add 10000 must succeed"
    );

    // 5. Initial staged scan simulation: 10,000 files in pre-commit hook.
    let start = Instant::now();
    let scan_out = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("keyhog scan --fast --git-staged (10000 files)");
    let duration = start.elapsed();

    assert_eq!(
        scan_out.status.code(),
        Some(0),
        "initial staged 10000 scan must exit 0"
    );
    let scan_stderr = String::from_utf8_lossy(&scan_out.stderr);
    assert!(
        scan_stderr.contains("10000 cache hit(s)") || scan_stderr.contains("10000 blob(s) scanned"),
        "must process all 10000 files via guard; stderr={scan_stderr}"
    );

    // 6. Second staged scan: 100% clean cache hits across 10,000 entries.
    let start_cached = Instant::now();
    let scan_cached = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("keyhog scan --fast --git-staged (10000 cached)");
    let cached_duration = start_cached.elapsed();

    assert_eq!(
        scan_cached.status.code(),
        Some(0),
        "cached staged 10000 scan must exit 0"
    );
    let cached_stderr = String::from_utf8_lossy(&scan_cached.stderr);
    assert!(
        cached_stderr.contains("10000 cache hit(s)"),
        "second pass must be 100% clean cache hits for 10000 files; stderr={cached_stderr}"
    );

    println!("10,000-file initial scan elapsed: {duration:?}");
    println!("10,000-file cached scan elapsed: {cached_duration:?}");

    // 7. Mixed diff at scale: mutate 10 files out of 10,000.
    for i in 0..10 {
        let filename = format!("src/pkg_{:03}/module_{:05}.rs", i / 500, i);
        let path = repo.join(&filename);
        std::fs::write(
            &path,
            format!("pub fn compute_{i}() -> usize {{ {i} * 5 + 99 }}\n"),
        )
        .unwrap();
    }
    let git_add_mutated = Command::new("git")
        .args([
            "add",
            "src/pkg_000/module_00000.rs",
            "src/pkg_000/module_00001.rs",
            "src/pkg_000/module_00002.rs",
            "src/pkg_000/module_00003.rs",
            "src/pkg_000/module_00004.rs",
            "src/pkg_000/module_00005.rs",
            "src/pkg_000/module_00006.rs",
            "src/pkg_000/module_00007.rs",
            "src/pkg_000/module_00008.rs",
            "src/pkg_000/module_00009.rs",
        ])
        .current_dir(repo)
        .output()
        .expect("git add mutated 10");
    assert!(git_add_mutated.status.success(), "git add mutated files");

    let start_mixed = Instant::now();
    let scan_mixed = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("keyhog scan --fast --git-staged (10000 mixed)");
    let mixed_duration = start_mixed.elapsed();

    assert_eq!(
        scan_mixed.status.code(),
        Some(0),
        "mixed staged 10000 scan must exit 0"
    );
    let mixed_stderr = String::from_utf8_lossy(&scan_mixed.stderr);
    assert!(
        mixed_stderr.contains("9990 cache hit(s)") && mixed_stderr.contains("10 blob(s) scanned"),
        "mixed diff must serve 9990 hits and stream only 10 blobs; stderr={mixed_stderr}"
    );
    println!("10,000-file mixed scan (10 changed, 9990 cached) elapsed: {mixed_duration:?}");

    // 8. Clean shutdown.
    let _ = Command::new(keyhog())
        .args(["guard", "down", "--socket"])
        .arg(&socket)
        .output();
}

/// WHY: Test staged renames in the guard commit protocol. When clean files are
/// renamed via `git mv`, Git records a deletion at the old path and an addition
/// at the new path. The daemon must process the renamed entries accurately:
/// on initial path occurrence it scans and validates the new path, and on repeat
/// cached passes it serves 100% clean cache hits.
#[cfg(unix)]
#[test]
fn guard_staged_diff_edge_cases_staged_renames() {
    let _daemon_slot = daemon_slot();
    let dir = TempDir::new().unwrap();
    let repo = dir.path();
    init_git_repo(repo);
    let socket = repo.join("guard-renames.sock");

    // 1. Start daemon.
    let up_out = Command::new(keyhog())
        .env("NO_COLOR", "1")
        .args(["guard", "up", "--backend", "cpu", "--socket"])
        .arg(&socket)
        .output()
        .expect("guard up");
    assert_eq!(up_out.status.code(), Some(0), "guard up must succeed");

    // 2. Create 50 clean files and stage them.
    for i in 0..50 {
        let filename = format!("src/file_{i:02}.rs");
        let path = repo.join(&filename);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(&path, format!("pub fn item_{i}() -> u32 {{ {i} }}\n")).unwrap();
    }
    let git_add = Command::new("git")
        .args(["add", "."])
        .current_dir(repo)
        .output()
        .expect("git add initial");
    assert!(git_add.status.success());

    // 3. Register repo with guard daemon.
    let add_out = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        // --no-hook: this test exercises daemon rename handling, not the
        // pre-commit hook. The installed hook shells out to `keyhog` from
        // PATH, which does not exist on CI runners, so an unqualified commit
        // after `guard add` fails with "git commit must succeed".
        .args(["guard", "add", ".", "--no-hook", "--socket"])
        .arg(&socket)
        .output()
        .expect("guard add");
    assert!(matches!(add_out.status.code(), Some(0 | 13)));

    // 4. Initial scan: all 50 files clean.
    let scan_1 = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("scan initial");
    assert_eq!(scan_1.status.code(), Some(0), "initial scan must exit 0");

    // Commit baseline so git mv operates against committed HEAD tree.
    let git_commit = Command::new("git")
        .args(["commit", "-q", "-m", "initial commit"])
        .current_dir(repo)
        .output()
        .expect("git commit initial");
    assert!(git_commit.status.success(), "git commit must succeed");

    // 5. Staged Renames:
    // - Pure rename of 10 files: src/file_00..09 -> src/renamed_00..09
    for i in 0..10 {
        let src = format!("src/file_{i:02}.rs");
        let dst = format!("src/renamed_{i:02}.rs");
        let mv_out = Command::new("git")
            .args(["mv", &src, &dst])
            .current_dir(repo)
            .output()
            .expect("git mv pure");
        assert!(mv_out.status.success(), "git mv {src} -> {dst}");
    }

    // - Rename + modification of 2 files: src/file_10..11 -> src/renamed_mod_10..11
    for i in 10..12 {
        let src = format!("src/file_{i:02}.rs");
        let dst = format!("src/renamed_mod_{i:02}.rs");
        let mv_out = Command::new("git")
            .args(["mv", &src, &dst])
            .current_dir(repo)
            .output()
            .expect("git mv mod");
        assert!(mv_out.status.success());
        // Mutate content of dst
        let path = repo.join(&dst);
        std::fs::write(&path, format!("pub fn item_{i}() -> u32 {{ {i} + 999 }}\n")).unwrap();
        let add_mod = Command::new("git")
            .args(["add", &dst])
            .current_dir(repo)
            .output()
            .expect("git add modified rename");
        assert!(add_mod.status.success());
    }

    // 6. Staged scan with renames.
    // In git diff --cached, we have 12 non-deletion entries (10 pure renames, 2 modified renames).
    let scan_renames = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("scan renames");
    assert_eq!(
        scan_renames.status.code(),
        Some(0),
        "staged rename scan must exit 0"
    );
    let renames_stderr = String::from_utf8_lossy(&scan_renames.stderr);
    assert!(
        renames_stderr.contains("12 blob(s) scanned") || renames_stderr.contains("12 cache hit(s)"),
        "renamed entries must be processed cleanly; stderr={renames_stderr}"
    );

    // 7. Second scan over staged renames: now all 12 entries are 100% clean cache hits.
    let scan_renames_cached = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("scan renames cached");
    assert_eq!(scan_renames_cached.status.code(), Some(0));
    let renames_cached_stderr = String::from_utf8_lossy(&scan_renames_cached.stderr);
    assert!(
        renames_cached_stderr.contains("12 cache hit(s)"),
        "all staged entries must be clean cache hits on repeat pass; stderr={renames_cached_stderr}"
    );

    // 8. Clean shutdown.
    let _ = Command::new(keyhog())
        .args(["guard", "down", "--socket"])
        .arg(&socket)
        .output();
}

/// WHY: Test mode changes (e.g. `chmod +x` changing file mode from 100644 to 100755)
/// in the staged Git index. When a clean file's mode is changed without content
/// modification, the object OID is unchanged and the daemon serves it as a clean hit.
/// When an executable script contains an active credential, the finding must be
/// reported and block pre-commit gating with exit code 1.
#[cfg(unix)]
#[test]
fn guard_staged_diff_edge_cases_mode_changes_executable() {
    let _daemon_slot = daemon_slot();
    let dir = TempDir::new().unwrap();
    let repo = dir.path();
    init_git_repo(repo);
    let socket = repo.join("guard-modes.sock");

    // 1. Start daemon.
    let up_out = Command::new(keyhog())
        .env("NO_COLOR", "1")
        .args(["guard", "up", "--backend", "cpu", "--socket"])
        .arg(&socket)
        .output()
        .expect("guard up");
    assert_eq!(up_out.status.code(), Some(0), "guard up must succeed");

    // 2. Create clean files and scripts.
    let script_clean = repo.join("scripts/deploy_clean.sh");
    let tool_clean = repo.join("tools/helper.py");
    let lib_clean = repo.join("src/lib.rs");
    std::fs::create_dir_all(repo.join("scripts")).unwrap();
    std::fs::create_dir_all(repo.join("tools")).unwrap();
    std::fs::create_dir_all(repo.join("src")).unwrap();

    std::fs::write(
        &script_clean,
        "#!/usr/bin/env bash\necho 'deploying clean'\n",
    )
    .unwrap();
    std::fs::write(&tool_clean, "#!/usr/bin/env python3\nprint('helper')\n").unwrap();
    std::fs::write(&lib_clean, "pub fn add(a: i32, b: i32) -> i32 { a + b }\n").unwrap();

    let git_add = Command::new("git")
        .args(["add", "."])
        .current_dir(repo)
        .output()
        .expect("git add initial");
    assert!(git_add.status.success());

    // Commit baseline.
    let git_commit = Command::new("git")
        .args(["commit", "-q", "-m", "initial baseline"])
        .current_dir(repo)
        .output()
        .expect("git commit initial");
    assert!(git_commit.status.success());

    // Register repo with guard daemon.
    let add_out = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args(["guard", "add", ".", "--socket"])
        .arg(&socket)
        .output()
        .expect("guard add");
    assert!(matches!(add_out.status.code(), Some(0 | 13)));

    // 3. Change mode of clean scripts to executable (+x) in Git index.
    let chmod_out = Command::new("git")
        .args([
            "update-index",
            "--chmod=+x",
            "scripts/deploy_clean.sh",
            "tools/helper.py",
        ])
        .current_dir(repo)
        .output()
        .expect("git update-index --chmod=+x");
    assert!(
        chmod_out.status.success(),
        "chmod +x in git index must succeed"
    );

    // 4. Staged scan with mode changes: object OID is identical, daemon recognizes clean hits.
    let scan_mode_clean = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("scan mode change clean");
    assert_eq!(
        scan_mode_clean.status.code(),
        Some(0),
        "clean mode change scan must exit 0"
    );
    let mode_clean_stderr = String::from_utf8_lossy(&scan_mode_clean.stderr);
    assert!(
        mode_clean_stderr.contains("2 cache hit(s)")
            || mode_clean_stderr.contains("2 blob(s) scanned"),
        "mode changes with identical content must be processed; stderr={mode_clean_stderr}"
    );

    // 5. Stage an executable script containing a credential.
    let secret_script = repo.join("scripts/run_leak.sh");
    // Synthetic AWS access key pattern that triggers AWS detector (non-example pattern).
    std::fs::write(
        &secret_script,
        "#!/bin/bash\nexport AWS_ACCESS_KEY_ID=\"AKIAQYLPMN5HFIQR7XYA\"\n",
    )
    .unwrap();
    let git_add_secret = Command::new("git")
        .args(["add", "scripts/run_leak.sh"])
        .current_dir(repo)
        .output()
        .expect("git add secret script");
    assert!(git_add_secret.status.success());
    let chmod_secret = Command::new("git")
        .args(["update-index", "--chmod=+x", "scripts/run_leak.sh"])
        .current_dir(repo)
        .output()
        .expect("chmod secret");
    assert!(chmod_secret.status.success());

    // 6. Staged scan must detect the secret and block commit with exit code 1 (EXIT_CREDENTIALS_FOUND).
    let scan_secret = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("scan secret");
    assert_eq!(
        scan_secret.status.code(),
        Some(1),
        "scan finding secret must exit 1 (EXIT_CREDENTIALS_FOUND)"
    );
    let secret_stderr = String::from_utf8_lossy(&scan_secret.stderr);
    let secret_stdout = String::from_utf8_lossy(&scan_secret.stdout);
    let combined = format!("{secret_stdout}\n{secret_stderr}");
    assert!(
        combined.contains("run_leak.sh")
            || combined.contains("AWS")
            || combined.contains("finding"),
        "secret finding must be surfaced; output={combined}"
    );

    // 7. Remediate: remove secret from the script, stage clean content, verify exit 0.
    std::fs::write(
        &secret_script,
        "#!/bin/bash\n# Credential removed; reads from secure secret manager at runtime\necho 'running'\n",
    )
    .unwrap();
    let git_add_clean = Command::new("git")
        .args(["add", "scripts/run_leak.sh"])
        .current_dir(repo)
        .output()
        .expect("git add remediated");
    assert!(git_add_clean.status.success());

    let scan_remediated = Command::new(keyhog())
        .current_dir(repo)
        .env("NO_COLOR", "1")
        .args([
            "scan",
            "--fast",
            "--git-staged",
            "--backend",
            "cpu",
            "--daemon-socket",
        ])
        .arg(&socket)
        .output()
        .expect("scan remediated");
    assert_eq!(
        scan_remediated.status.code(),
        Some(0),
        "remediated scan must exit 0"
    );

    // 8. Clean shutdown.
    let _ = Command::new(keyhog())
        .args(["guard", "down", "--socket"])
        .arg(&socket)
        .output();
}