fff-search 0.9.6

Faboulous & Fast File Finder - a fast and extremely correct file finder SDK with typo resistance, SIMD, prefiltering, and more
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
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
//! Vibe coded stress test: randomized file + git operations driven against the *real*
//! `BackgroundWatcher`, asserting the git-status invariant after every mutation.
//!
//! ## What this test is trying to catch
//!
//! The background watcher is supposed to keep every indexed file's
//! `git_status` in sync with the actual repository state at all times.
//! There are two code paths that can drift:
//!
//!   1. **Per-file updates** — when regular files are created / modified the
//!      watcher queries `git_status_for_paths(&[changed_file])`. If the git
//!      state changes for files that weren't in the batch (rename detection,
//!      submodule paths, index modifications applied by a sibling process),
//!      those other files keep a stale `git_status` until the next full
//!      rescan is triggered.
//!
//!   2. **Full rescans** — triggered by changes under `.git/` (index, HEAD,
//!      MERGE_HEAD, etc.) and by `.gitignore` edits. A missed event here is
//!      the most common failure mode: the watcher coalesced or dropped the
//!      `.git/index` notification and the picker never learns that e.g. a
//!      `git commit` cleared every `WT_MODIFIED`.
//!
//! ## How the test works
//!
//!  * Spin up a real `FilePicker` with `watch: true` on a fresh temp repo.
//!  * Use `proptest` to generate a sequence of 20–40 randomized ops (heavy on
//!    git mutations: add / commit / reset / stash / gitignore edits).
//!  * After **every** op, poll until the picker's per-file `git_status` agrees
//!    with `git2::Repository::statuses()` verbatim, or bail with a rich diff.
//!  * If there is ever a divergence that doesn't resolve within
//!    `CONVERGE_TIMEOUT`, the test fails with the list of `(path, truth,
//!    picker)` mismatches.
//!
//! The shape of the scenario is shrinkable: on failure proptest will shrink
//! the `Vec<AbstractOp>` so the reported diff corresponds to the smallest
//! surviving sequence.
//!
//! ## Runtime
//!
//! Each scenario does a real filesystem scan + watcher setup (~0.5–1 s) and
//! then runs 20–40 ops each of which waits for event propagation
//! (~100–500 ms on macOS FSEvents). With `cases = 2` the test takes
//! ~30–45 s on a typical dev machine — too slow to run as part of the
//! default `cargo test`, so it's gated behind the `stress` cfg.
//!
//! Run it explicitly with:
//! ```sh
//! RUSTFLAGS="--cfg stress" cargo test -p fff-search --test fuzz_git_watcher_stress -- --nocapture
//! ```
//!
//! Or increase coverage via env:
//! ```sh
//! FFF_STRESS_CASES=8 FFF_STRESS_MAX_OPS=60 \
//!     RUSTFLAGS="--cfg stress" cargo test -p fff-search --test fuzz_git_watcher_stress -- --nocapture
//! ```

#![cfg(stress)]
use fff_search::file_picker::{FFFMode, FilePicker};
use fff_search::grep::{GrepMode, GrepSearchOptions, parse_grep_query};
use fff_search::{
    FilePickerOptions, FuzzySearchOptions, PaginationArgs, QueryParser, SharedFilePicker,
    SharedFrecency,
};
use git2::{Repository, Status, StatusOptions};
use proptest::prelude::*;
use proptest::strategy::ValueTree;
use proptest::test_runner::{
    Config as ProptestConfig, FileFailurePersistence, RngAlgorithm, TestRng, TestRunner,
};
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};
use tempfile::TempDir;

/// Upper bound on how long we wait for the picker to converge after any op.
/// The watcher debounce is 50 ms; worst-case FSEvents propagation + queued
/// full-rescan can easily reach several seconds. 15 s is very generous.
const CONVERGE_TIMEOUT: Duration = Duration::from_secs(15);

/// Poll interval while waiting for convergence.
const CONVERGE_POLL: Duration = Duration::from_millis(50);

/// Small pause between back-to-back ops to simulate real user behavior
const PER_OP_SETTLE: Duration = Duration::from_millis(10);

fn stress_cases() -> u32 {
    std::env::var("FFF_STRESS_CASES")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(2)
}

fn stress_max_ops() -> usize {
    std::env::var("FFF_STRESS_MAX_OPS")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(40)
}

fn stress_min_ops() -> usize {
    std::env::var("FFF_STRESS_MIN_OPS")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(20)
}

/// A single randomized action. The runtime interprets abstract handles
/// (`idx`) by modulo against the *current* list of live files, so the same
/// abstract op sequence is always runnable regardless of which files exist.
#[derive(Debug, Clone)]
enum AbstractOp {
    CreateFile {
        seed: u32,
        content_seed: u32,
    },
    EditFile {
        idx: usize,
        content_seed: u32,
    },
    DeleteFile {
        idx: usize,
    },
    RenameFile {
        idx: usize,
        new_seed: u32,
    },
    CreateSubdirFile {
        dir_seed: u16,
        file_seed: u16,
        content_seed: u32,
    },
    GitignoreAppend {
        pattern_seed: u16,
    },
    GitAddAll,
    GitCommit {
        msg_seed: u16,
    },
    GitResetHard,
    GitStashThenPop,
    Touch {
        idx: usize,
    }, // rewrite with same content; should still fire Modify
    Noop,
}

fn op_strategy() -> impl Strategy<Value = AbstractOp> {
    // Weights tuned to mirror a real editing session rather than a
    // contrived git-heavy workload. In descending order:
    //
    //   * edits dominate (~60%) — a real user bangs on Save all day
    //   * creates are moderate (~15%) — new files appear occasionally
    //   * deletes and renames are rare (~10%) — destructive ops happen
    //     but far less often than edits
    //   * explicit git operations are the rarest (~8%) — users stage /
    //     commit / reset in bursts between long stretches of editing
    //
    // The file-mutation path still exercises the per-path git-status
    // update on every op via the watcher, so the git-status updater
    // is *always* under load — the rare explicit `GitAddAll` /
    // `GitCommit` / `GitResetHard` ops layer in coverage of the
    // `.git/index` event → full-rescan path specifically. A 40-op
    // scenario fires that path ~3 times, which is enough to keep the
    // race between worktree writes and `.git/*` updates in rotation.
    prop_oneof![
        40 => (any::<usize>(), any::<u32>()).prop_map(|(i, c)| AbstractOp::EditFile {
            idx: i, content_seed: c,
        }),
        5  => any::<usize>().prop_map(|i| AbstractOp::Touch { idx: i }),
        8  => (any::<u32>(), any::<u32>()).prop_map(|(a, b)| AbstractOp::CreateFile {
            seed: a, content_seed: b,
        }),
        3  => (any::<u16>(), any::<u16>(), any::<u32>()).prop_map(
            |(d, f, c)| AbstractOp::CreateSubdirFile {
                dir_seed: d, file_seed: f, content_seed: c,
            }
        ),
        4  => any::<usize>().prop_map(|i| AbstractOp::DeleteFile { idx: i }),
        3  => (any::<usize>(), any::<u32>()).prop_map(|(i, s)| AbstractOp::RenameFile {
            idx: i, new_seed: s,
        }),
        1  => Just(AbstractOp::GitAddAll),
        1  => any::<u16>().prop_map(|m| AbstractOp::GitCommit { msg_seed: m }),
        1  => Just(AbstractOp::GitStashThenPop),
        1  => Just(AbstractOp::GitResetHard),
        1  => any::<u16>().prop_map(|p| AbstractOp::GitignoreAppend { pattern_seed: p }),
        2  => Just(AbstractOp::Noop),
    ]
}

fn ops_strategy() -> impl Strategy<Value = Vec<AbstractOp>> {
    let min = stress_min_ops();
    let max = stress_max_ops();
    prop::collection::vec(op_strategy(), min..=max)
}

const DEFAULT_STRESS_SEED: u64 = 0xDEAD_BEEF_CAFE_BABE;

fn proptest_config() -> ProptestConfig {
    ProptestConfig {
        cases: stress_cases(),
        // Cap shrinking because each shrink iteration replays the whole
        // scenario (seconds of real IO). Proptest's default 4096 is wild.
        max_shrink_iters: 16,
        // `fork: true` would isolate runs but swallows the panic payload
        // and printed diff through `rusty-fork`'s wire format — we want
        // the convergence report visible on stderr, so we keep in-process.
        // The watcher is fully torn down between cases via `Drop`.
        fork: false,
        // Integration tests don't live alongside a `lib.rs`/`main.rs`, so
        // proptest's default `SourceParallel` persistence strategy can't
        // locate the source tree and logs a noisy warning on every run.
        // Pin the regression file to an explicit path next to this test
        // so failing seeds get checked in and reproduced in CI.
        failure_persistence: Some(Box::new(FileFailurePersistence::Direct(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/tests/fuzz_git_watcher_stress.proptest-regressions",
        )))),
        ..ProptestConfig::default()
    }
}

proptest! {
    #![proptest_config(proptest_config())]

    /// Random-seeded scenario. Proptest picks a fresh seed every run from
    /// system entropy, so CI sees different trajectories on every build
    /// while known-bad seeds remain pinned in the regressions file.
    #[test]
    fn stress_random(ops in ops_strategy()) {
        run_stress_scenario(&ops);
    }
}

/// Deterministic scenario keyed off `FFF_STRESS_SEED` (or
/// [`DEFAULT_STRESS_SEED`] if the env var is not set). Runs the same
/// proptest `ops_strategy()` but uses a ChaCha RNG seeded by the expanded
/// u64, so the exact case sequence is reproducible across machines and CI
/// runs.
///
/// When this test panics, the panic message includes the seed so you can
/// re-run the failing case locally via:
///
/// ```sh
/// RUSTFLAGS="--cfg stress" FFF_STRESS_SEED=0xDEADBEEFCAFEBABE \
///     cargo test -p fff-search --test fuzz_git_watcher_stress seeded -- --nocapture
/// ```
#[test]
fn stress_seeded() {
    let seed = parse_stress_seed();
    let seed_bytes = expand_u64_seed(seed);

    eprintln!("stress_seeded: using deterministic seed {seed:#018x}");

    let mut config = proptest_config();
    // The seeded run should never write to the shared regressions file —
    // its failures are reproducible from the env var alone, and polluting
    // the shared file with the deterministic seed would mask regressions
    // for the random run that actually needs persistence.
    config.failure_persistence = Some(Box::new(FileFailurePersistence::Off));

    let rng = TestRng::from_seed(RngAlgorithm::ChaCha, &seed_bytes);
    let mut runner = TestRunner::new_with_rng(config, rng);
    let strategy = ops_strategy();

    // Mimic `proptest!`'s case loop: run `Config::cases` independent draws
    // from the strategy, failing the test as soon as any one scenario
    // diverges. We drive this ourselves because `runner.run()` can't
    // accept a non-Fn closure, and our scenario runner is side-effectful.
    for case_idx in 0..runner.config().cases {
        let tree = strategy
            .new_tree(&mut runner)
            .expect("ops_strategy::new_tree");
        let ops = tree.current();
        eprintln!(
            "  case {}/{}: {} ops",
            case_idx + 1,
            runner.config().cases,
            ops.len()
        );
        run_stress_scenario(&ops);
    }
}

/// Parse `FFF_STRESS_SEED` as either decimal or `0x`-prefixed hex.
fn parse_stress_seed() -> u64 {
    match std::env::var("FFF_STRESS_SEED") {
        Ok(raw) => {
            let trimmed = raw.trim();
            if let Some(hex) = trimmed
                .strip_prefix("0x")
                .or_else(|| trimmed.strip_prefix("0X"))
            {
                u64::from_str_radix(hex, 16)
                    .unwrap_or_else(|e| panic!("FFF_STRESS_SEED={raw:?} is not valid hex: {e}"))
            } else {
                trimmed
                    .parse::<u64>()
                    .unwrap_or_else(|e| panic!("FFF_STRESS_SEED={raw:?} is not a valid u64: {e}"))
            }
        }
        Err(_) => DEFAULT_STRESS_SEED,
    }
}

/// Expand a u64 into the 32-byte seed ChaCha20 wants, by repeating the
/// little-endian bytes four times. The cycle is deliberate: two different
/// u64 seeds produce completely different byte sequences, so collisions
/// across the expansion are irrelevant in practice.
fn expand_u64_seed(seed: u64) -> [u8; 32] {
    let le = seed.to_le_bytes();
    let mut out = [0u8; 32];
    for i in 0..4 {
        out[i * 8..(i + 1) * 8].copy_from_slice(&le);
    }
    out
}

#[derive(Debug)]
struct Live {
    /// Path relative to the repo root (forward slashes on all platforms).
    relative: String,
    abs: PathBuf,
}

fn run_stress_scenario(ops: &[AbstractOp]) {
    // Opt-in tracing so `RUST_LOG=fff_search=debug` shows the watcher /
    // refresh / update trail when debugging a failing run. Uses
    // `try_init` so proptest can run many scenarios in the same process
    // without double-initialising the subscriber.
    let _ = tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                // Default to DEBUG for fff crates so CI failures on
                // flaky OSes (Windows) include the watcher/event trail
                // without needing to re-run with RUST_LOG set.
                .unwrap_or_else(|_| {
                    tracing_subscriber::EnvFilter::new(
                        "warn,fff_search=debug,notify=debug,notify_debouncer_full=debug",
                    )
                }),
        )
        .with_test_writer()
        .try_init();

    let tmp = TempDir::new().unwrap();
    let base = tmp.path().canonicalize().unwrap();

    seed_repo(&base);

    let (shared_picker, _frecency) = start_watched_picker(&base);
    wait_ready(&shared_picker);

    // Reconcile `live` from disk: after `git reset --hard` or similar the
    // worktree can change under us, so we resync at every step instead of
    // trusting our in-memory list.
    let mut live: Vec<Live> = get_baseline_status_from_git(&base);

    // Sleep past the coarse (1 s) mtime tick so subsequent edits definitely
    // advance mtime. Not strictly required for git status correctness but
    // matches what real users experience.
    std::thread::sleep(Duration::from_millis(1100));

    for (step, op) in ops.iter().enumerate() {
        apply_op(op, &base, &mut live);

        // Re-sync live from disk — ops like GitResetHard / GitStashThenPop
        // may have created or removed files behind our back.
        live = get_baseline_status_from_git(&base);
        std::thread::sleep(PER_OP_SETTLE);

        if let Err(err) = converge_git_status(&shared_picker, &base, &live) {
            panic!(
                "\n──────────────────────────────────────────────────────────\n\
                 ❌ Picker git_status diverged from repository truth.\n\
                 ──────────────────────────────────────────────────────────\n\
                 step:    {step}\n\
                 op:      {op:?}\n\
                 scenario ops:\n{trace}\n\
                 ──────────────────────────────────────────────────────────\n\
                 {err}\n",
                trace = format_ops_trace(ops, step),
            );
        }
    }
}

fn format_ops_trace(ops: &[AbstractOp], up_to_and_including: usize) -> String {
    let mut s = String::new();
    for (i, op) in ops.iter().enumerate().take(up_to_and_including + 1) {
        s.push_str(&format!("   [{i:>3}] {op:?}\n"));
    }
    s
}

fn seed_repo(base: &Path) {
    fs::create_dir_all(base.join("src")).unwrap();
    fs::write(base.join("README.md"), "# seed\n").unwrap();
    fs::write(base.join("src/main.rs"), "fn main() {}\n").unwrap();
    fs::write(base.join("src/lib.rs"), "// lib\n").unwrap();
    fs::write(base.join(".gitignore"), "*.log\ntmp/\n").unwrap();

    git(base, &["init", "-b", "main"]);
    git(base, &["config", "user.email", "fuzz@fff.test"]);
    git(base, &["config", "user.name", "fuzz"]);
    // Disable rename detection noise — libgit2 still computes ranks, but
    // keeping the porcelain behaviour deterministic helps with diff reading.
    git(base, &["config", "status.renames", "false"]);
    git(base, &["add", "-A"]);
    git(base, &["commit", "-m", "seed", "--no-gpg-sign"]);
}

fn apply_op(op: &AbstractOp, base: &Path, live: &mut [Live]) {
    use AbstractOp::*;

    match op {
        CreateFile { seed, content_seed } => {
            let rel = format!("f_{seed:08x}.rs");
            let abs = base.join(&rel);
            if abs.exists() {
                return;
            }
            fs::write(&abs, content_for(*content_seed, &rel)).unwrap();
        }
        EditFile { idx, content_seed } => {
            if live.is_empty() {
                return;
            }
            let i = idx % live.len();
            let abs = &live[i].abs;
            if !abs.is_file() {
                return;
            }
            // Body must differ so git sees WT_MODIFIED (not just atime touch).
            let body = format!(
                "// edited seed={content_seed:08x}\n{}",
                content_for(*content_seed, &live[i].relative)
            );
            fs::write(abs, body).unwrap();
        }
        Touch { idx } => {
            // Rewrite with the same bytes we currently hold on disk. Still
            // generates a Modify event; git status stays the same if the
            // content matches the index, or WT_MODIFIED if it differs.
            if live.is_empty() {
                return;
            }
            let i = idx % live.len();
            let abs = &live[i].abs;
            if let Ok(contents) = fs::read(abs) {
                let _ = fs::write(abs, contents);
            }
        }
        DeleteFile { idx } => {
            if live.is_empty() {
                return;
            }
            let i = idx % live.len();
            let _ = fs::remove_file(&live[i].abs);
        }
        RenameFile { idx, new_seed } => {
            if live.is_empty() {
                return;
            }
            let i = idx % live.len();
            let old = &live[i].abs;
            if !old.is_file() {
                return;
            }
            let new_rel = format!("r_{new_seed:08x}.rs");
            let new_abs = base.join(&new_rel);
            if new_abs.exists() {
                return;
            }
            let _ = fs::rename(old, &new_abs);
        }
        CreateSubdirFile {
            dir_seed,
            file_seed,
            content_seed,
        } => {
            let rel = format!("d_{dir_seed:04x}/inside_{file_seed:04x}.rs");
            let abs = base.join(&rel);
            if abs.exists() {
                return;
            }
            fs::create_dir_all(abs.parent().unwrap()).unwrap();
            fs::write(&abs, content_for(*content_seed, &rel)).unwrap();
        }
        GitignoreAppend { pattern_seed } => {
            // Introduce NEW ignore patterns for a namespace not used by any
            // other op so we never accidentally re-ignore a file under test.
            let pattern = format!("__ignored_{pattern_seed:x}/\n");
            let gi = base.join(".gitignore");
            let mut cur = fs::read_to_string(&gi).unwrap_or_default();
            cur.push_str(&pattern);
            fs::write(&gi, cur).unwrap();
        }
        GitAddAll => {
            git_allow_fail(base, &["add", "-A"]);
        }
        GitCommit { msg_seed } => {
            // `--allow-empty` so we don't depend on there actually being
            // staged changes; this still bumps HEAD and rewrites .git/index.
            let _ = git_output(
                base,
                &[
                    "commit",
                    "-m",
                    &format!("fuzz-{msg_seed:x}"),
                    "--allow-empty",
                    "--allow-empty-message",
                    "--no-gpg-sign",
                ],
            );
        }
        GitResetHard => {
            git_allow_fail(base, &["reset", "--hard"]);
        }
        GitStashThenPop => {
            git_allow_fail(base, &["add", "-A"]);
            let stash = git_output(base, &["stash", "push", "-u", "-m", "fuzz"]);
            let had_stash = stash
                .as_ref()
                .map(|o| {
                    o.status.success()
                        && !String::from_utf8_lossy(&o.stdout).contains("No local changes")
                })
                .unwrap_or(false);
            if had_stash {
                git_allow_fail(base, &["stash", "pop"]);
            }
        }
        Noop => {}
    }
}

/// Block until the picker's git status agrees with `git2::Repository::statuses`.
///
/// Poll until the picker's git-status view matches libgit2's truth AND
/// the real-query probe succeeds, or bail with a rich diff.
///
/// On timeout, returns an `Err` describing every disagreement.
fn converge_git_status(
    shared_picker: &SharedFilePicker,
    base: &Path,
    live: &[Live],
) -> Result<(), String> {
    let deadline = Instant::now() + CONVERGE_TIMEOUT;
    let mut last_mismatches: Vec<Mismatch>;
    let mut last_probe_err: Option<String> = None;

    loop {
        let truth = read_truth_status(base);
        let picker_view = read_picker_status(shared_picker);
        last_mismatches = diff_statuses(&truth, &picker_view);

        // Real-query probe: one random live file per round via fuzzy + grep.
        // We only consider the probe authoritative once the main git-status
        // enumeration agrees — otherwise a probe failure might just be
        // the same debouncer-lag we're already waiting out.
        let probe = if last_mismatches.is_empty() {
            probe_real_queries(shared_picker, live)
        } else {
            None
        };

        match (last_mismatches.is_empty(), probe) {
            (true, None) | (true, Some(Ok(()))) => return Ok(()),
            (true, Some(Err(msg))) => last_probe_err = Some(msg),
            _ => {}
        }

        if Instant::now() >= deadline {
            let mut report = if !last_mismatches.is_empty() {
                format_mismatches(&last_mismatches, shared_picker)
            } else {
                String::from("git_status enumeration converged, but real-query probe failed:\n")
            };
            if let Some(probe_msg) = last_probe_err {
                report.push_str("\n── real-query probe ──\n");
                report.push_str(&probe_msg);
                report.push('\n');
            }
            report.push_str(&debug_dump_environment(
                base,
                shared_picker,
                &last_mismatches,
            ));
            return Err(report);
        }
        std::thread::sleep(CONVERGE_POLL);
    }
}

/// On-failure diagnostic dump. Includes:
///   * base path (raw + OS encoding) so we can spot UNC/`\\?\` prefixes on Windows
///   * picker's own `base_path()` for comparison
///   * direct libgit2 `status_file` probes for every mismatched path
///   * full picker enumeration (first 20 rows) so we can see what keys
///     the picker is returning vs. what git2 reports
fn debug_dump_environment(
    base: &Path,
    shared_picker: &SharedFilePicker,
    mismatches: &[Mismatch],
) -> String {
    let mut s = String::new();
    s.push_str("\n── diagnostic dump ──\n");
    s.push_str(&format!(
        "test base path (display)   : {}\n",
        base.display()
    ));
    s.push_str(&format!("test base path (debug)     : {:?}\n", base));
    s.push_str(&format!(
        "test base path (os bytes)  : {:?}\n",
        base.as_os_str()
    ));

    if let Ok(guard) = shared_picker.read()
        && let Some(picker) = guard.as_ref()
    {
        s.push_str(&format!(
            "picker base_path (display) : {}\n",
            picker.base_path().display()
        ));
        s.push_str(&format!(
            "picker base_path (debug)   : {:?}\n",
            picker.base_path()
        ));
    }

    // Probe libgit2 directly for each mismatched path.
    if let Ok(repo) = Repository::open(base) {
        s.push_str("\nlibgit2 status_file probes (per mismatch path):\n");
        for m in mismatches {
            let path = match m {
                Mismatch::Disagree { path, .. } => path,
                Mismatch::ExtraInPicker { path, .. } => path,
            };
            match repo.status_file(std::path::Path::new(path)) {
                Ok(st) => s.push_str(&format!("{path} -> {st:?}\n")),
                Err(e) => {
                    s.push_str(&format!(
                        "{path} -> ERROR {} (class={:?}, code={:?})\n",
                        e.message(),
                        e.class(),
                        e.code()
                    ));
                }
            }
        }
    } else {
        s.push_str("\nlibgit2: could not open repo at base path\n");
    }

    // Dump the raw picker enumeration for comparison — up to 20 rows.
    s.push_str("\npicker enumeration (first 20 entries, with byte-repr of each relative path):\n");
    if let Ok(guard) = shared_picker.read()
        && let Some(picker) = guard.as_ref()
    {
        let parser = QueryParser::default();
        let parsed = parser.parse("");
        let result = picker.fuzzy_search(
            &parsed,
            None,
            FuzzySearchOptions {
                max_threads: 1,
                pagination: PaginationArgs {
                    offset: 0,
                    limit: 100,
                },
                ..Default::default()
            },
        );
        for (i, f) in result.items.iter().take(20).enumerate() {
            let raw = f.relative_path(picker);
            let norm = normalize(raw.clone());
            s.push_str(&format!(
                "  [{i:>2}] raw={raw:?} norm={norm:?} status={:?}\n",
                f.git_status
            ));
        }
        s.push_str(&format!("  (total: {} items)\n", result.items.len()));
    }

    s
}

#[derive(Debug)]
enum Mismatch {
    /// Git knows about this path with `truth`, picker has `picker` (or None = missing).
    Disagree {
        path: String,
        truth: Status,
        picker: Option<Option<Status>>,
    },
    /// Picker has a non-clean entry for a path git doesn't report at all.
    ExtraInPicker { path: String, picker: Status },
}

fn read_truth_status(base: &Path) -> BTreeMap<String, Status> {
    let repo = Repository::open(base).expect("open repo for truth");
    let mut opts = StatusOptions::new();
    opts.include_untracked(true)
        .recurse_untracked_dirs(true)
        .include_unmodified(true)
        .exclude_submodules(true);
    let statuses = repo.statuses(Some(&mut opts)).expect("read statuses");

    let mut out = BTreeMap::new();
    for entry in statuses.iter() {
        if let Some(p) = entry.path() {
            // git2 returns forward-slash paths; accept as-is.
            out.insert(p.to_string(), entry.status());
        }
    }
    out
}

fn read_picker_status(shared: &SharedFilePicker) -> BTreeMap<String, Option<Status>> {
    let guard = shared.read().expect("picker read lock");
    let picker = guard.as_ref().expect("picker initialized");

    // Use the user-facing `fuzzy_search` API with an empty query to enumerate
    // every file a user could ever see in the picker. An empty fuzzy query
    // falls through to the frecency-only scoring path which returns ALL
    // non-deleted files (base + overflow). A very large `limit` makes sure
    // we don't silently paginate anything away for scenarios with many files.
    //
    // This intentionally mirrors what a real Neovim user observes — soft-
    // deleted tombstones, files filtered out by search-side predicates, etc.
    // are all excluded from the picker's user-facing view.
    let parser = QueryParser::default();
    let parsed = parser.parse("");
    let result = picker.fuzzy_search(
        &parsed,
        None,
        FuzzySearchOptions {
            max_threads: 1,
            pagination: PaginationArgs {
                offset: 0,
                limit: 100_000,
            },
            ..Default::default()
        },
    );

    let mut out = BTreeMap::new();
    for f in &result.items {
        out.insert(normalize(f.relative_path(picker)), f.git_status);
    }
    out
}

fn normalize(s: String) -> String {
    #[cfg(windows)]
    {
        if s.contains('\\') {
            return s.replace('\\', "/");
        }
    }
    s
}

/// Probe a single file by name via the public `fuzzy_search` API and return
/// its status, or `None` if the file is not findable. Used for diagnostics
/// when the top-level enumeration reports a disagreement — confirms that
/// the mismatch reproduces via the exact user-facing lookup path.
fn probe_single_file_status(shared: &SharedFilePicker, relative: &str) -> Option<Option<Status>> {
    let guard = shared.read().ok()?;
    let picker = guard.as_ref()?;
    let parser = QueryParser::default();
    let parsed = parser.parse(relative);
    let result = picker.fuzzy_search(
        &parsed,
        None,
        FuzzySearchOptions {
            max_threads: 1,
            pagination: PaginationArgs {
                offset: 0,
                limit: 200,
            },
            ..Default::default()
        },
    );
    result
        .items
        .iter()
        .find(|f| normalize(f.relative_path(picker)) == relative)
        .map(|f| f.git_status)
}

// ═══════════════════════════════════════════════════════════════════════════
// Real-query probes
// ═══════════════════════════════════════════════════════════════════════════
//
// These exercise the **user-facing** fuzzy + grep surfaces with queries that
// look like what a human actually types, on top of a live file picked at
// random from the on-disk truth. They run once per convergence round in
// addition to the primary git-status enumeration check, and add coverage
// for three paths the empty-query enumeration never touches:
//
//   * fuzzy matching with a non-empty query (bigram prefilter + score)
//   * path-constraint fuzzy queries ("foo src/")
//   * live grep (mmap content cache + bigram overlay for overflow files)
//
// An atomic counter drives the rotation across rounds so the probe spreads
// its attention across every live file over the course of a scenario.
static PROBE_COUNTER: AtomicU64 = AtomicU64::new(0);

/// Read a file on disk and pull out the `FFF_STRESS_MARKER_<hex>` token
/// that [`content_for`] embedded in it. Returns `None` for files we never
/// wrote (seed README.md, .gitignore) — the caller skips the grep probe
/// in that case but still runs the fuzzy probe.
fn extract_marker(abs: &Path) -> Option<String> {
    let content = fs::read_to_string(abs).ok()?;
    let start = content.find("FFF_STRESS_MARKER_")?;
    // Marker is exactly `FFF_STRESS_MARKER_` + 8 hex chars.
    const MARKER_LEN: usize = "FFF_STRESS_MARKER_".len() + 8;
    if start + MARKER_LEN > content.len() {
        return None;
    }
    let marker = &content[start..start + MARKER_LEN];
    // Sanity: the trailing 8 chars must all be hex.
    if !marker.as_bytes()[MARKER_LEN - 8..]
        .iter()
        .all(|b| b.is_ascii_hexdigit())
    {
        return None;
    }
    Some(marker.to_string())
}

/// File-stem → fuzzy search query. Strips extension + leading path
/// components so the probe feeds the picker a typical "I know roughly
/// what I'm looking for" query.
fn stem_for_query(relative: &str) -> String {
    PathBuf::from(relative)
        .file_stem()
        .unwrap_or_default()
        .to_string_lossy()
        .into_owned()
}

/// Run a one-shot fuzzy_search and return the matched items' relative
/// paths paired with their `git_status`. Uses only public APIs.
fn fuzzy_search_items(shared: &SharedFilePicker, query: &str) -> Vec<(String, Option<Status>)> {
    let guard = match shared.read() {
        Ok(g) => g,
        Err(_) => return Vec::new(),
    };
    let Some(picker) = guard.as_ref() else {
        return Vec::new();
    };
    let parser = QueryParser::default();
    let parsed = parser.parse(query);
    let result = picker.fuzzy_search(
        &parsed,
        None,
        FuzzySearchOptions {
            max_threads: 1,
            pagination: PaginationArgs {
                offset: 0,
                limit: 500,
            },
            ..Default::default()
        },
    );
    result
        .items
        .iter()
        .map(|f| (normalize(f.relative_path(picker)), f.git_status))
        .collect()
}

/// Run live grep (plain-text mode) and return the unique set of matched
/// file paths. A file appears in the set iff at least one line inside it
/// matches the query. Uses only public APIs.
fn grep_plain_matches(shared: &SharedFilePicker, query: &str) -> Vec<String> {
    let guard = match shared.read() {
        Ok(g) => g,
        Err(_) => return Vec::new(),
    };
    let Some(picker) = guard.as_ref() else {
        return Vec::new();
    };
    let parsed = parse_grep_query(query);
    let opts = GrepSearchOptions {
        max_file_size: 10 * 1024 * 1024,
        max_matches_per_file: 200,
        smart_case: true,
        file_offset: 0,
        page_limit: 500,
        mode: GrepMode::PlainText,
        time_budget_ms: 0,
        before_context: 0,
        after_context: 0,
        classify_definitions: false,
        trim_whitespace: false,
        abort_signal: None,
    };
    let result = picker.grep(&parsed, &opts);
    // `GrepResult::files` is the already-deduplicated list of files that
    // contained at least one match — exactly what we want.
    result
        .files
        .iter()
        .map(|f| normalize(f.relative_path(picker)))
        .collect()
}

/// Run live grep (fuzzy mode) and return matched file paths.
/// Exercises the `fuzzy_grep_search` code path which resolves content
/// via arena pointers — the path that was silently broken for overflow
/// files before the overflow_arena fix.
fn grep_fuzzy_matches(shared: &SharedFilePicker, query: &str) -> Vec<String> {
    let guard = match shared.read() {
        Ok(g) => g,
        Err(_) => return Vec::new(),
    };
    let Some(picker) = guard.as_ref() else {
        return Vec::new();
    };
    let parsed = parse_grep_query(query);
    let opts = GrepSearchOptions {
        max_file_size: 10 * 1024 * 1024,
        max_matches_per_file: 200,
        smart_case: true,
        file_offset: 0,
        page_limit: 500,
        mode: GrepMode::Fuzzy,
        time_budget_ms: 0,
        before_context: 0,
        after_context: 0,
        classify_definitions: false,
        trim_whitespace: false,
        abort_signal: None,
    };
    let result = picker.grep(&parsed, &opts);
    result
        .files
        .iter()
        .map(|f| normalize(f.relative_path(picker)))
        .collect()
}

/// Run live grep (regex mode) and return matched file paths.
fn grep_regex_matches(shared: &SharedFilePicker, query: &str) -> Vec<String> {
    let guard = match shared.read() {
        Ok(g) => g,
        Err(_) => return Vec::new(),
    };
    let Some(picker) = guard.as_ref() else {
        return Vec::new();
    };
    let parsed = parse_grep_query(query);
    let opts = GrepSearchOptions {
        max_file_size: 10 * 1024 * 1024,
        max_matches_per_file: 200,
        smart_case: true,
        file_offset: 0,
        page_limit: 500,
        mode: GrepMode::Regex,
        time_budget_ms: 0,
        before_context: 0,
        after_context: 0,
        classify_definitions: false,
        trim_whitespace: false,
        abort_signal: None,
    };
    let result = picker.grep(&parsed, &opts);
    result
        .files
        .iter()
        .map(|f| normalize(f.relative_path(picker)))
        .collect()
}

/// Report from [`probe_real_queries`]. `None` means "nothing to probe this
/// round" (empty live set). `Some(Err)` means a probe disagreed with truth
/// — convergence should not treat this as success.
type ProbeOutcome = Option<Result<(), String>>;

/// Real-query verification: for one rotated-live-file per round, run a
/// fuzzy search by stem and (when a marker is present) a grep for its
/// embedded token. Asserts both return the file *and* that its
/// `git_status` matches the picker's main view.
///
/// Return values:
///   * `None`         — nothing to probe (no live files).
///   * `Some(Ok(()))` — the probe agreed with truth on both surfaces.
///   * `Some(Err(s))` — diagnostic describing the disagreement.
fn probe_real_queries(shared: &SharedFilePicker, live: &[Live]) -> ProbeOutcome {
    if live.is_empty() {
        return None;
    }
    let idx = (PROBE_COUNTER.fetch_add(1, Ordering::Relaxed) as usize) % live.len();
    let target = &live[idx];

    // --- Fuzzy probe: search by file stem ---
    let stem = stem_for_query(&target.relative);
    // Tiny stems (< 2 chars) are rejected by the fuzzy scorer and
    // surface via the frecency fallback — skip the assertion in that
    // case, nothing meaningful to verify.
    if stem.len() >= 2 {
        let fuzzy_hits = fuzzy_search_items(shared, &stem);
        let found = fuzzy_hits.iter().find(|(p, _)| p == &target.relative);
        if found.is_none() {
            return Some(Err(format!(
                "fuzzy_search({stem:?}) did not return expected live file {:?}\n\
                 got {} results; first few: {:?}",
                target.relative,
                fuzzy_hits.len(),
                fuzzy_hits.iter().take(5).collect::<Vec<_>>(),
            )));
        }
    }

    // --- Grep probe: search for the content marker using a randomly
    // rotated grep strategy. Each round picks one of PlainText / Fuzzy /
    // Regex so over many rounds all three code paths get exercised,
    // including the overflow-arena resolution that was previously broken
    // in fuzzy grep.
    if let Some(marker) = extract_marker(&target.abs) {
        let probe_round = PROBE_COUNTER.load(Ordering::Relaxed);
        let (mode_name, matches) = match probe_round % 3 {
            0 => ("plain", grep_plain_matches(shared, &marker)),
            1 => ("fuzzy", grep_fuzzy_matches(shared, &marker)),
            _ => ("regex", grep_regex_matches(shared, &marker)),
        };
        if !matches.contains(&target.relative) {
            return Some(Err(format!(
                "grep[{mode_name}]({marker:?}) did not return expected live file {:?}\n\
                 got {} matched files; first few: {:?}",
                target.relative,
                matches.len(),
                matches.iter().take(5).collect::<Vec<_>>(),
            )));
        }
    }

    Some(Ok(()))
}

/// Returns the full list of disagreements. Empty means "in sync".
///
/// We deliberately exclude a few categories from being considered bugs:
///   * Paths that git marks as `WT_DELETED` / `INDEX_DELETED` but the picker
///     has already dropped — this is by design (deleted files leave the
///     index immediately).
///   * Paths that git marks as `IGNORED` but the picker has already dropped.
///   * Paths under `.git/` — never tracked by the picker.
fn diff_statuses(
    truth: &BTreeMap<String, Status>,
    picker: &BTreeMap<String, Option<Status>>,
) -> Vec<Mismatch> {
    let mut out = Vec::new();

    for (path, &truth_status) in truth {
        if path.starts_with(".git/") || path == ".git" {
            continue;
        }
        match picker.get(path) {
            Some(&p) => {
                if !status_equivalent(truth_status, p) {
                    out.push(Mismatch::Disagree {
                        path: path.clone(),
                        truth: truth_status,
                        picker: Some(p),
                    });
                }
            }
            None => {
                // Tolerate picker not having deleted-from-disk files.
                let only_absence_reasons =
                    Status::WT_DELETED | Status::INDEX_DELETED | Status::IGNORED;
                if !truth_status.intersects(only_absence_reasons) {
                    out.push(Mismatch::Disagree {
                        path: path.clone(),
                        truth: truth_status,
                        picker: None,
                    });
                }
            }
        }
    }

    for (path, &p) in picker {
        if path.starts_with(".git/") || path == ".git" {
            continue;
        }
        if !truth.contains_key(path) {
            // Picker thinks a file exists that git has never heard of.
            // Only a bug if the picker also thinks it has a non-clean
            // status for it — otherwise it's a transient during which the
            // picker has indexed a file before the next truth snapshot.
            let non_clean = match p {
                None => false,
                Some(s) => !(s.is_empty() || s == Status::CURRENT),
            };
            if non_clean {
                out.push(Mismatch::ExtraInPicker {
                    path: path.clone(),
                    picker: p.unwrap_or(Status::CURRENT),
                });
            }
        }
    }

    out
}

/// `None` and `Some(CURRENT|empty)` are both "clean"; otherwise the bitsets
/// must match exactly.
fn status_equivalent(truth: Status, picker: Option<Status>) -> bool {
    let picker_bits = picker.unwrap_or(Status::CURRENT);
    let truth_clean = truth.is_empty() || truth == Status::CURRENT;
    let picker_clean = picker_bits.is_empty() || picker_bits == Status::CURRENT;
    if truth_clean && picker_clean {
        return true;
    }
    truth == picker_bits
}

fn format_mismatches(mismatches: &[Mismatch], shared: &SharedFilePicker) -> String {
    let mut s = String::new();
    s.push_str(&format!(
        "{} mismatch(es) after {} of wait:\n",
        mismatches.len(),
        humantime(CONVERGE_TIMEOUT),
    ));
    for m in mismatches {
        match m {
            Mismatch::Disagree {
                path,
                truth,
                picker,
            } => {
                let probe = probe_single_file_status(shared, path);
                s.push_str(&format!(
                    "{path}\n      truth       : {}\n      picker(enum): {}\n      picker(probe): {}\n",
                    format_status(Some(*truth)),
                    match picker {
                        Some(p) => format_status(*p),
                        None => "<missing from enumeration>".into(),
                    },
                    match probe {
                        Some(p) => format_status(p),
                        None => "<not returned by fuzzy_search>".into(),
                    }
                ));
            }
            Mismatch::ExtraInPicker { path, picker } => {
                let probe = probe_single_file_status(shared, path);
                s.push_str(&format!(
                    "{path}\n      truth        : <missing from repo>\n      picker(enum) : {}\n      picker(probe): {}\n",
                    format_status(Some(*picker)),
                    match probe {
                        Some(p) => format_status(p),
                        None => "<not returned by fuzzy_search>".into(),
                    }
                ));
            }
        }
    }
    s
}

fn format_status(s: Option<Status>) -> String {
    match s {
        None => "None (= clean)".into(),
        Some(st) if st.is_empty() || st == Status::CURRENT => "CURRENT (= clean)".into(),
        Some(st) => format!("{st:?}"),
    }
}

fn humantime(d: Duration) -> String {
    format!("{:.1}s", d.as_secs_f64())
}

fn get_baseline_status_from_git(base: &Path) -> Vec<Live> {
    let mut out = Vec::new();
    let repo = match Repository::open(base) {
        Ok(r) => r,
        Err(_) => return out,
    };
    let mut opts = StatusOptions::new();
    opts.include_untracked(true)
        .recurse_untracked_dirs(true)
        .include_unmodified(true)
        .exclude_submodules(true);
    let statuses = match repo.statuses(Some(&mut opts)) {
        Ok(s) => s,
        Err(_) => return out,
    };
    for entry in statuses.iter() {
        if let Some(p) = entry.path() {
            let abs = base.join(p);
            // Must be a real file *right now* — ignore stale WT_DELETED rows.
            if abs.is_file() {
                out.push(Live {
                    relative: p.to_string(),
                    abs,
                });
            }
        }
    }
    out
}

/// Content marker embedded in every generated file body. The probing
/// layer reads this back via `grep` to exercise the live-grep path
/// (which hits the bigram index / mmap cache / overflow content store —
/// code paths the empty-query fuzzy enumeration never touches).
///
/// Format: `FFF_STRESS_MARKER_<32-bit hex>` — namespaced so a repo
/// search for "MARKER" finds nothing from this test outside files we
/// wrote ourselves, which keeps the grep assertion unambiguous.
fn marker_for(seed: u32) -> String {
    format!("FFF_STRESS_MARKER_{seed:08x}")
}

fn content_for(seed: u32, name: &str) -> String {
    let marker = marker_for(seed);
    // Keep it small but distinct so adjacent edits produce different SHAs.
    // The marker appears twice — once in a comment, once in a string
    // literal — so a grep for it always has at least two hits per file
    // and one of them is guaranteed to be on its own line for easy
    // assertion.
    format!(
        "// file: {name}\n\
         // seed: {seed:08x}\n\
         // anchor: {marker}\n\
         pub fn anchor_{seed:x}() {{ let _ = \"{marker}\"; }}\n"
    )
}

fn start_watched_picker(base: &Path) -> (SharedFilePicker, SharedFrecency) {
    let shared_picker = SharedFilePicker::default();
    let shared_frecency = SharedFrecency::noop();

    FilePicker::new_with_shared_state(
        shared_picker.clone(),
        shared_frecency.clone(),
        FilePickerOptions {
            base_path: base.to_string_lossy().to_string(),
            enable_mmap_cache: false,
            enable_content_indexing: false,
            mode: FFFMode::Neovim,
            watch: true,
            ..Default::default()
        },
    )
    .expect("FilePicker::new_with_shared_state");

    (shared_picker, shared_frecency)
}

fn wait_ready(p: &SharedFilePicker) {
    assert!(
        p.wait_for_scan(Duration::from_secs(15)),
        "timed out waiting for initial scan"
    );
    assert!(
        p.wait_for_watcher(Duration::from_secs(15)),
        "timed out waiting for watcher"
    );
    // macOS FSEvents sometimes delivers a burst of "warmup" events right
    // after the stream opens — let them drain before we start fuzzing.
    std::thread::sleep(Duration::from_millis(200));
}

fn git_env() -> [(&'static str, &'static str); 4] {
    [
        ("GIT_AUTHOR_NAME", "fuzz"),
        ("GIT_AUTHOR_EMAIL", "fuzz@fff.test"),
        ("GIT_COMMITTER_NAME", "fuzz"),
        ("GIT_COMMITTER_EMAIL", "fuzz@fff.test"),
    ]
}

fn git(base: &Path, args: &[&str]) {
    let out = Command::new("git")
        .args(args)
        .current_dir(base)
        .envs(git_env())
        .output()
        .unwrap_or_else(|e| panic!("git {args:?}: {e}"));
    assert!(
        out.status.success(),
        "git {args:?} failed:\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
}

fn git_allow_fail(base: &Path, args: &[&str]) {
    let _ = Command::new("git")
        .args(args)
        .current_dir(base)
        .envs(git_env())
        .output();
}

fn git_output(base: &Path, args: &[&str]) -> Option<std::process::Output> {
    Command::new("git")
        .args(args)
        .current_dir(base)
        .envs(git_env())
        .output()
        .ok()
}

// ═══════════════════════════════════════════════════════════════════════════
// Merge-conflict scenario
// ═══════════════════════════════════════════════════════════════════════════
//
// The fuzz alphabet above intentionally avoids branch operations — merge
// conflicts need a very specific topology (two divergent edits to the same
// hunk) that random sampling would almost never hit. This scripted test
// fills that gap with a deterministic end-to-end conflict flow:
//
//   1. seed a single tracked file `conflict.rs`
//   2. branch to `feature`, rewrite the file's inner expression, commit
//   3. back on `main`, rewrite the same hunk differently, commit
//   4. `git merge feature` — leaves `.git/MERGE_HEAD` + conflict markers
//   5. picker must observe `Status::CONFLICTED` for `conflict.rs`, and the
//      same real-query surfaces (fuzzy + grep for `<<<<<<<`) must return
//      the conflicted file
//   6. resolve by writing the merged content, `git add`, `git commit`
//   7. picker must converge back to `Status::CURRENT`
//
// On top of the `--cfg stress` gate the test inherits (so it never runs
// under plain `cargo test`), this is cross-platform: it uses libgit2
// internals + the `git` CLI, both of which work identically on macOS
// FSEvents, Linux inotify, and Windows ReadDirectoryChangesW (if we ever
// add Windows to the matrix). The convergence window is a bit larger than
// the fuzz case because `git merge` touches several `.git/*` files at
// once, producing a heftier event burst.

/// Convergence timeout for the conflict flow. `git merge` fires more FS
/// events than a plain `git add` — `.git/ORIG_HEAD`, `.git/MERGE_HEAD`,
/// `.git/MERGE_MSG`, the worktree file itself — so the debouncer has
/// more work than a one-off refresh. 30 s is still very generous.
const CONFLICT_CONVERGE_TIMEOUT: Duration = Duration::from_secs(30);

#[test]
fn stress_merge_conflict_convergence() {
    // Opt-in tracing for parity with the other stress tests.
    let _ = tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                // Default to DEBUG for fff crates so CI failures on
                // flaky OSes (Windows) include the watcher/event trail
                // without needing to re-run with RUST_LOG set.
                .unwrap_or_else(|_| {
                    tracing_subscriber::EnvFilter::new(
                        "warn,fff_search=debug,notify=debug,notify_debouncer_full=debug",
                    )
                }),
        )
        .with_test_writer()
        .try_init();

    let tmp = TempDir::new().expect("mktemp");
    let base = tmp.path().canonicalize().expect("canonicalize tmp");

    seed_conflict_repo(&base);

    let (shared_picker, _frecency) = start_watched_picker(&base);
    wait_ready(&shared_picker);

    // ─────────────────────────────────────────────────────────────────
    // Stage 1: create divergent commits on `feature` and `main`
    // ─────────────────────────────────────────────────────────────────
    //
    // `conflict.rs` on `main` contains `BASE` text at line 2. We rewrite
    // that line two different ways on two branches, so a merge can't
    // pick a side automatically.
    //
    // Each rewrite is preceded by a 1.1 s sleep so the file's mtime
    // advances past the previous write's — the watcher's mmap cache
    // invalidation is mtime-triggered at 1 s granularity, and without
    // the sleep a follow-up grep on the picker sees stale content from
    // whichever variant was written last-but-one.

    std::thread::sleep(Duration::from_millis(1100));
    git(&base, &["checkout", "-b", "feature"]);
    fs::write(
        base.join("conflict.rs"),
        "fn flavour() {\n    \"FEATURE_VARIANT\"\n}\n",
    )
    .unwrap();
    git(&base, &["add", "conflict.rs"]);
    git(
        &base,
        &["commit", "-m", "feature: rewrite", "--no-gpg-sign"],
    );

    std::thread::sleep(Duration::from_millis(1100));
    git(&base, &["checkout", "main"]);
    fs::write(
        base.join("conflict.rs"),
        "fn flavour() {\n    \"MAIN_VARIANT\"\n}\n",
    )
    .unwrap();
    git(&base, &["add", "conflict.rs"]);
    git(&base, &["commit", "-m", "main: rewrite", "--no-gpg-sign"]);

    // Also sleep before the merge itself so the post-merge worktree
    // write lands in a fresh second.
    std::thread::sleep(Duration::from_millis(1100));

    // Let the divergent commits settle in the picker before we merge.
    expect_file_status(
        &shared_picker,
        &base,
        "conflict.rs",
        |s| {
            // Post-commit: should be clean (CURRENT or None = no row).
            s.is_none() || s.unwrap().is_empty() || s.unwrap().contains(Status::CURRENT)
        },
        Duration::from_secs(10),
        "pre-merge clean",
    )
    .expect("pre-merge state should be clean");

    // ─────────────────────────────────────────────────────────────────
    // Stage 2: trigger the conflicting merge
    // ─────────────────────────────────────────────────────────────────

    // `git merge feature` returns non-zero on conflict — that's fine.
    // We don't use `git(..)` (asserts success) — conflict IS the happy
    // path for this test.
    let merge = git_output(&base, &["merge", "feature", "--no-edit", "--no-gpg-sign"])
        .expect("git merge didn't launch");
    assert!(
        !merge.status.success(),
        "expected `git merge feature` to conflict, but it succeeded:\nstdout:{}\nstderr:{}",
        String::from_utf8_lossy(&merge.stdout),
        String::from_utf8_lossy(&merge.stderr)
    );

    // libgit2 marks both sides' modifications with `CONFLICTED`. We
    // wait for the picker to match.
    expect_file_status(
        &shared_picker,
        &base,
        "conflict.rs",
        |s| s.is_some_and(|st| st.contains(Status::CONFLICTED)),
        CONFLICT_CONVERGE_TIMEOUT,
        "CONFLICTED after merge",
    )
    .expect("picker must surface CONFLICTED after merge");

    // ─────────────────────────────────────────────────────────────────
    // Stage 3: real-query surfaces must still work in conflict state
    // ─────────────────────────────────────────────────────────────────

    // Fuzzy search by stem returns the conflicted file.
    let fuzzy_hits = fuzzy_search_items(&shared_picker, "conflict");
    assert!(
        fuzzy_hits.iter().any(|(p, _)| p == "conflict.rs"),
        "fuzzy_search(\"conflict\") during conflict state returned: {:?}",
        fuzzy_hits
    );

    // Grep for the diff conflict marker `<<<<<<<` — it's literally in
    // the worktree file right now, so live grep must find it.
    // The watcher rewrites the file on-disk during merge, so the mmap
    // cache needs to have been invalidated for this to succeed.
    let grep_hits = grep_plain_matches(&shared_picker, "<<<<<<< ");
    assert!(
        grep_hits.contains(&"conflict.rs".to_string()),
        "grep(\"<<<<<<< \") during conflict state returned: {:?}",
        grep_hits
    );

    // ─────────────────────────────────────────────────────────────────
    // Stage 4: resolve + commit, expect return to clean
    // ─────────────────────────────────────────────────────────────────

    // `on_create_or_modify` invalidates the mmap cache only when `mtime`
    // advances, and mtime has 1 s resolution on every filesystem we
    // ship on. `git merge` in stage 2 just wrote to `conflict.rs`; if
    // we re-write it within the same second the cache keeps its
    // pre-resolve bytes (complete with `<<<<<<<` markers) and grep will
    // keep finding them. Sleep past the mtime tick before re-writing.
    // This matches the pattern in `fuzz_file_operations.rs`.
    std::thread::sleep(Duration::from_millis(1100));

    fs::write(
        base.join("conflict.rs"),
        "fn flavour() {\n    \"RESOLVED_VARIANT\"\n}\n",
    )
    .unwrap();
    git(&base, &["add", "conflict.rs"]);
    git(
        &base,
        &[
            "commit",
            "-m",
            "resolve merge",
            "--no-gpg-sign",
            "--no-edit",
        ],
    );

    expect_file_status(
        &shared_picker,
        &base,
        "conflict.rs",
        |s| s.is_none() || s.unwrap().is_empty() || s.unwrap().contains(Status::CURRENT),
        CONFLICT_CONVERGE_TIMEOUT,
        "CURRENT after resolve",
    )
    .expect("picker must converge back to CURRENT after conflict resolution");

    // Sanity: conflict markers are gone from both the worktree AND the
    // picker's view (grep for `<<<<<<<` now returns 0 hits).
    //
    // The worktree check is an independent truth source — if it fails,
    // the test harness itself is buggy (the resolve write didn't apply).
    // The picker check can lag briefly: `expect_file_status` returned
    // once `.git/index` events made git_status = CURRENT, but the
    // `conflict.rs` Modify event from our resolve `fs::write` can land
    // in a separate debounced batch that hasn't been processed yet.
    // Poll until the mmap cache is flushed rather than asserting once.
    let on_disk = fs::read_to_string(base.join("conflict.rs")).unwrap();
    assert!(
        !on_disk.contains("<<<<<<<"),
        "worktree still has conflict markers after resolve — test harness bug\n\
         on-disk content:\n{on_disk}"
    );
    let deadline = Instant::now() + CONFLICT_CONVERGE_TIMEOUT;
    loop {
        let grep_hits = grep_plain_matches(&shared_picker, "<<<<<<< ");
        if !grep_hits.contains(&"conflict.rs".to_string()) {
            break;
        }
        if Instant::now() >= deadline {
            panic!(
                "picker grep(\"<<<<<<< \") after resolve still returns conflict.rs \
                 after {} — mmap/overlay cache is stale\n\
                 (worktree on disk has no conflict markers — this is a real \
                  content-invalidation bug)\n\
                 last grep hits: {:?}",
                humantime(CONFLICT_CONVERGE_TIMEOUT),
                grep_hits,
            );
        }
        std::thread::sleep(CONVERGE_POLL);
    }
}

/// Seed a repo with a single file that we'll later produce a conflict in.
fn seed_conflict_repo(base: &Path) {
    fs::write(base.join("README.md"), "# merge conflict test\n").unwrap();
    fs::write(
        base.join("conflict.rs"),
        "fn flavour() {\n    \"BASE\"\n}\n",
    )
    .unwrap();
    git(base, &["init", "-b", "main"]);
    git(base, &["config", "user.email", "fuzz@fff.test"]);
    git(base, &["config", "user.name", "fuzz"]);
    // Conflict behaviour must be deterministic regardless of merge.tool.
    git(base, &["config", "merge.conflictstyle", "merge"]);
    git(base, &["add", "-A"]);
    git(base, &["commit", "-m", "seed", "--no-gpg-sign"]);
}

/// Poll the picker for `relative` until `predicate` holds on its
/// `git_status`, or the timeout expires. On expiry returns an `Err` with
/// the last observed status + truth status for diagnostics.
fn expect_file_status(
    shared: &SharedFilePicker,
    base: &Path,
    relative: &str,
    predicate: impl Fn(Option<Status>) -> bool,
    timeout: Duration,
    what: &str,
) -> Result<(), String> {
    let deadline = Instant::now() + timeout;
    let mut last_picker;
    let mut last_truth;
    loop {
        let picker_status = probe_single_file_status(shared, relative).flatten();
        let truth_status = read_truth_status(base).get(relative).copied();
        last_picker = picker_status;
        last_truth = truth_status;
        if predicate(picker_status) {
            return Ok(());
        }
        if Instant::now() >= deadline {
            return Err(format!(
                "timed out after {} waiting for `{relative}` to satisfy `{what}`\n\
                 last picker status: {:?}\n\
                 last truth status : {:?}",
                humantime(timeout),
                last_picker,
                last_truth,
            ));
        }
        std::thread::sleep(CONVERGE_POLL);
    }
}