open-loops 1.5.0

Recupere o contexto de trabalhos pausados: o que começou, onde parou, qual o próximo passo
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
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
//! Repository and unmerged-branch discovery via git shell-out.
//! Design decision: shell-out (not git2/gix) — simple and debuggable;
//! the product performance bottleneck is the LLM, not git.
use anyhow::{bail, Context, Result};
use chrono::{DateTime, Utc};
use std::path::{Path, PathBuf};
use std::process::Command;

use crate::index::Index;
use crate::inventory::{self, InventoryFile, InventoryStore, LoopMemo};

/// Inventory update produced by one `open_loops` call: `(common-dir hash, file)`.
type InvUpdate = (String, InventoryFile);

/// Options controlling a scan (light phase always; heavy phase optional + memoised).
#[derive(Debug, Clone, Default)]
pub struct ScanOptions {
    /// Whether to compute ahead/behind counts (heavy phase via `rev-list`).
    pub need_ahead_behind: bool,
    /// When true, skip any cached inventory memo and recompute `rev-list`.
    pub fresh: bool,
    /// Directory for the inventory JSON files. `None` disables memoisation.
    pub inventory_dir: Option<PathBuf>,
    /// Seconds before a cached entry expires; 0 = SHA-only validation.
    pub inventory_ttl_secs: u64,
}

/// Runs a git subcommand in `repo` and returns trimmed stdout.
///
/// # Errors
///
/// Returns `Err` if git is not in PATH or if the command fails.
pub(crate) fn git(repo: &Path, args: &[&str]) -> Result<String> {
    let out = Command::new("git")
        .arg("-C")
        .arg(repo)
        .args(args)
        .output()
        .context("git not found in PATH — install git")?;
    if !out.status.success() {
        bail!(
            "git {:?} failed in {}: {}",
            args,
            repo.display(),
            String::from_utf8_lossy(&out.stderr).trim()
        );
    }
    Ok(String::from_utf8_lossy(&out.stdout).trim().to_string())
}

/// Default branch: origin/HEAD's target if it resolves locally; otherwise main;
/// otherwise master.
///
/// # Errors
///
/// Returns `Err` if no default branch is found.
pub fn default_branch(repo: &Path) -> Result<String> {
    let (name, _) = default_branch_and_sha(repo)?;
    Ok(name)
}

/// Default branch name and its SHA, resolved in a single rev-parse call.
/// Used internally to avoid redundant git calls in the heavy phase.
///
/// origin/HEAD only wins when its target branch exists locally: a stale or
/// `--single-branch` origin/HEAD can name a branch with no local ref, and we
/// must fall through to main/master rather than hide the whole repo.
///
/// # Errors
///
/// Returns `Err` if no default branch is found.
fn default_branch_and_sha(repo: &Path) -> Result<(String, String)> {
    if let Ok(sym) = git(
        repo,
        &["symbolic-ref", "--short", "refs/remotes/origin/HEAD"],
    ) {
        if let Some(branch) = sym.strip_prefix("origin/") {
            // Only honour origin/HEAD when its branch resolves locally; otherwise
            // fall through so a stale pointer doesn't make the repo disappear.
            if let Ok(sha) = git(repo, &["rev-parse", &format!("refs/heads/{branch}")]) {
                return Ok((branch.to_string(), sha));
            }
        }
    }
    for candidate in ["main", "master"] {
        if let Ok(sha) = git(
            repo,
            &["rev-parse", "--verify", &format!("refs/heads/{candidate}")],
        ) {
            return Ok((candidate.to_string(), sha));
        }
    }
    bail!(
        "couldn't find the default branch in {} (expected origin/HEAD, main or master)",
        repo.display()
    )
}

/// A git repository discovered under a configured root (deduped by common-dir).
#[derive(Debug, Clone)]
pub struct RepoCandidate {
    pub path: PathBuf,
    /// Canonical repo name from `--git-common-dir` (computed once during dedup).
    pub repo_name: String,
}

/// An open loop: an unmerged branch with its own commits.
#[derive(Debug, Clone)]
pub struct OpenLoop {
    pub root_label: String,
    pub repo_name: String,
    pub repo_path: PathBuf,
    pub branch: String,
    pub head_sha: String,
    pub last_commit: DateTime<Utc>,
    pub ahead: Option<u32>,
    pub behind: Option<u32>,
}

impl OpenLoop {
    /// Canonical key used in resume/ignore: "root-label/repo/branch".
    pub fn key(&self) -> String {
        format!("{}/{}/{}", self.root_label, self.repo_name, self.branch)
    }
}

const SKIP_DIRS: [&str; 2] = ["node_modules", "target"];

fn looks_like_bare(dir: &Path) -> bool {
    dir.join("HEAD").is_file() && dir.join("objects").is_dir() && dir.join("refs").is_dir()
}

fn is_repo_candidate(dir: &Path) -> bool {
    dir.join(".git").exists() || looks_like_bare(dir)
}

/// Derives a stable repo name from the absolute git common-dir (§5 of Spec Fase A).
pub fn repo_name_from_common_dir(common_dir: &Path) -> String {
    let base = common_dir
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default();
    if base == ".git" || base == ".bare" {
        return common_dir
            .parent()
            .and_then(|p| p.file_name())
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or(base);
    }
    base.strip_suffix(".git").map(str::to_owned).unwrap_or(base)
}

/// Absolute path of the git common-dir for `path` (bare store / `.git` dir).
///
/// # Errors
///
/// Returns `Err` when `path` is not inside a git repository.
pub fn git_common_dir(path: &Path) -> Result<PathBuf> {
    let raw = git(
        path,
        &["rev-parse", "--path-format=absolute", "--git-common-dir"],
    )?;
    Ok(PathBuf::from(raw))
}

/// Cheap fingerprint of a repo's refs: the MAX mtime (unix nanoseconds since the
/// epoch) of `<common_dir>/HEAD`, `<common_dir>/packed-refs` (when present), the
/// newest entry anywhere under the `<common_dir>/refs` tree, and the newest entry
/// anywhere under the `<common_dir>/worktrees/` tree. Missing files or directories
/// contribute 0.
///
/// This is the refs-fingerprint gate (#13): when it is unchanged since the last
/// index write, the cached loops are still valid and the heavy git phase
/// (`for-each-ref`, `branch --merged`, per-branch `rev-list`) can be skipped
/// entirely.
///
/// Precision note: the brief specified whole-second mtimes, but a branch created
/// (or advanced) within the same wall-clock second as the previous index write
/// would then leave the fingerprint unchanged and silently serve stale loops —
/// e.g. a brand-new branch would not appear. Using nanosecond precision closes
/// that window so a new/advanced ref is always detected, regardless of timing.
/// `i64` nanos-since-epoch overflow only in the year 2262, so the range is safe.
/// On filesystems that expose only second-granularity mtimes the sub-second part
/// is simply 0 — the gate then degrades to whole-second behaviour, never worse.
///
/// Other notes:
/// - the gate is additionally paired with `default_sha` in `cached_loops`, so a
///   moved default branch invalidates even if mtimes somehow collide.
/// - `git gc`/repacking rewrites `packed-refs`, which bumps the fingerprint
///   without a semantic change. That is acceptable — it only forces one
///   recompute, never stale data.
/// - `git worktree add`/`remove` mutates `<common_dir>/worktrees/`, which is now
///   covered: any worktree change bumps the fingerprint → gate invalidates →
///   recompute → fresh `worktree_path` values are served.
pub fn refs_fingerprint(common_dir: &Path) -> i64 {
    let mut max = 0_i64;
    max = max.max(file_mtime_nanos(&common_dir.join("HEAD")));
    max = max.max(file_mtime_nanos(&common_dir.join("packed-refs")));
    max = max.max(newest_mtime_in_tree(&common_dir.join("refs")));
    max = max.max(newest_mtime_in_tree(&common_dir.join("worktrees")));
    max
}

/// Unix mtime of a single path in nanoseconds since the epoch, or 0 when it is
/// missing / unreadable. Saturates at `i64::MAX` (year 2262) rather than wrap.
fn file_mtime_nanos(path: &Path) -> i64 {
    std::fs::metadata(path)
        .and_then(|m| m.modified())
        .ok()
        .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
        .map(|d| i64::try_from(d.as_nanos()).unwrap_or(i64::MAX))
        .unwrap_or(0)
}

/// Newest mtime (unix nanos) found by walking `dir` recursively (files and the
/// directories themselves). Returns 0 for a missing/unreadable tree.
///
/// Loose refs live as files under `refs/`; a brand-new branch creates a new file
/// (and bumps the containing directory's mtime), and advancing a branch rewrites
/// its ref file — both raise this value, which is exactly the signal the gate
/// wants. Packed refs are covered separately via `packed-refs`.
fn newest_mtime_in_tree(dir: &Path) -> i64 {
    let mut max = file_mtime_nanos(dir);
    let Ok(entries) = std::fs::read_dir(dir) else {
        return max;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        match entry.file_type() {
            Ok(ft) if ft.is_dir() => max = max.max(newest_mtime_in_tree(&path)),
            _ => max = max.max(file_mtime_nanos(&path)),
        }
    }
    max
}

// PERF-1: git_common_dir is called twice per repo — once in dedup_candidates
// (computed but not stored), and again in open_loops. Reusing the value from
// dedup would require changing RepoCandidate or open_loops's public signature.
// Both are internal, but threading the common_dir through without altering the
// public API would require wrapping it in a private helper that cli.rs doesn't call.
// Current cost: negligible (one extra git call per repo per scan), acceptable
// trade-off for keeping the public signature stable. Revisit if scan latency
// becomes dominated by this call (measure: `time loops scan --fresh`).

/// One entry from `git worktree list --porcelain`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorktreeEntry {
    pub path: PathBuf,
    /// Short branch name (`refs/heads/` stripped). `None` when detached or bare.
    pub branch: Option<String>,
    pub bare: bool,
    pub prunable: bool,
}

/// Parses `git worktree list --porcelain` into entries.
///
/// Pure over the git output: a new entry starts at each `worktree ` line; the
/// `HEAD`/`detached`/`locked` lines leave `branch` as `None`. Tolerant — unknown
/// or blank lines are ignored, never panics.
pub fn parse_worktree_porcelain(out: &str) -> Vec<WorktreeEntry> {
    let mut entries = Vec::new();
    let mut current: Option<WorktreeEntry> = None;
    for line in out.lines() {
        if let Some(p) = line.strip_prefix("worktree ") {
            if let Some(e) = current.take() {
                entries.push(e);
            }
            current = Some(WorktreeEntry {
                path: PathBuf::from(p),
                branch: None,
                bare: false,
                prunable: false,
            });
        } else if let Some(e) = current.as_mut() {
            if let Some(b) = line.strip_prefix("branch ") {
                e.branch = Some(b.strip_prefix("refs/heads/").unwrap_or(b).to_string());
            } else if line == "bare" {
                e.bare = true;
            } else if line == "prunable" || line.starts_with("prunable ") {
                e.prunable = true;
            }
        }
    }
    if let Some(e) = current.take() {
        entries.push(e);
    }
    entries
}

fn normalize_path(path: PathBuf) -> PathBuf {
    std::fs::canonicalize(&path).unwrap_or(path)
}

/// Maps each checked-out branch to the absolute path of its worktree.
///
/// Bare and detached entries are dropped (no branch to key on). git proscribes
/// the same branch in two worktrees, so the map is 1:1.
///
/// # Errors
///
/// Returns `Err` if `git worktree list` fails.
pub fn worktree_map(repo: &Path) -> Result<std::collections::HashMap<String, PathBuf>> {
    let raw = git(repo, &["worktree", "list", "--porcelain"])?;
    Ok(parse_worktree_porcelain(&raw)
        .into_iter()
        .filter(|e| !e.bare)
        .filter_map(|e| e.branch.map(|b| (b, normalize_path(e.path))))
        .collect())
}

/// Walks roots up to `scan_depth` looking for git repo candidates, then
/// deduplicates by absolute `--git-common-dir`.
pub fn find_repos(roots: &[PathBuf], scan_depth: usize) -> (Vec<RepoCandidate>, Vec<String>) {
    find_repos_cached(roots, scan_depth, None)
}

/// Like [`find_repos`] but optionally consults `index` to skip `git rev-parse`
/// for already-known paths (resolves #17).
pub fn find_repos_cached(
    roots: &[PathBuf],
    scan_depth: usize,
    index: Option<&Index>,
) -> (Vec<RepoCandidate>, Vec<String>) {
    let mut candidates = Vec::new();
    for root in roots {
        walk(root, 0, scan_depth, &mut candidates);
    }
    dedup_candidates_cached(candidates, index)
}

/// Like `dedup_candidates` but optionally uses `index` to cache/reuse
/// `--git-common-dir` results, skipping the git shell-out on cache hits.
fn dedup_candidates_cached(
    candidates: Vec<PathBuf>,
    index: Option<&Index>,
) -> (Vec<RepoCandidate>, Vec<String>) {
    use std::collections::HashMap;
    let mut by_common: HashMap<PathBuf, RepoCandidate> = HashMap::new();
    let mut warnings = Vec::new();
    for candidate in candidates {
        // Try index cache first (hit = skip shell-out).
        let cached = index.and_then(|idx| idx.cached_common_dir(&candidate));
        let common_result = if let Some((_hash, common_dir)) = cached {
            Ok(common_dir)
        } else {
            // Cache miss: call git and store the result back.
            match git_common_dir(&candidate) {
                Ok(common) => {
                    if let Some(idx) = index {
                        let hash = crate::inventory::common_dir_hash(&common);
                        idx.put_repo_common_dir(&candidate, &hash, &common);
                    }
                    Ok(common)
                }
                Err(e) => Err(e),
            }
        };

        match common_result {
            Ok(common) => {
                let repo_name = repo_name_from_common_dir(&common);
                by_common.entry(common).or_insert(RepoCandidate {
                    path: candidate,
                    repo_name,
                });
            }
            Err(e) => {
                warnings.push(format!("{}: {e:#}", candidate.display()));
            }
        }
    }
    let mut repos: Vec<RepoCandidate> = by_common.into_values().collect();
    repos.sort_by(|a, b| a.path.cmp(&b.path));
    (repos, warnings)
}

fn walk(dir: &Path, depth: usize, scan_depth: usize, candidates: &mut Vec<PathBuf>) {
    if is_repo_candidate(dir) {
        candidates.push(dir.to_path_buf());
        return;
    }
    if depth >= scan_depth {
        return;
    }
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        let name = entry.file_name();
        let name = name.to_string_lossy();
        if !path.is_dir() || name.starts_with('.') || SKIP_DIRS.contains(&name.as_ref()) {
            continue;
        }
        walk(&path, depth + 1, scan_depth, candidates);
    }
}

/// Path-based repo name guess when `git rev-parse --git-common-dir` fails.
/// Primary naming comes from common-dir during dedup; this is the error fallback only.
pub fn repo_name_hint(path: &Path) -> String {
    let base = path
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default();
    base.strip_suffix(".git").map(str::to_owned).unwrap_or(base)
}

/// Returns all unmerged branches (except default) in a repo, optionally reading
/// and updating the inventory memo for ahead/behind.
///
/// Light phase (default branch, merged set, `for-each-ref`) always runs. The
/// heavy phase (`rev-list` for ahead/behind) runs only when
/// `opts.need_ahead_behind` is true, and consults the inventory memo unless
/// `opts.fresh` is set.
///
/// Returns the open loops and, when memoisation is active, the updated
/// `(hash, InventoryFile)` pair for write-through by the caller.
///
/// # Errors
///
/// Returns `Err` if git fails or if the default branch is not found.
pub fn open_loops(
    repo: &Path,
    root_label: &str,
    opts: &ScanOptions,
) -> Result<(Vec<OpenLoop>, Option<InvUpdate>)> {
    open_loops_indexed(repo, root_label, opts, None)
}

/// Like [`open_loops`] but optionally consults a SQLite `index` for the
/// refs-fingerprint gate (#13).
///
/// When `index` is `Some` and `opts.fresh` is false, the gate is checked first:
/// if the repo's `refs_fingerprint` and `default_sha` are unchanged since the
/// last index write, the cached loops are returned and the heavy git phase
/// (`for-each-ref`, `branch --merged`, per-branch `rev-list`) is skipped
/// entirely. On a miss (or `opts.fresh`), the full logic runs and the result is
/// written through to the index for the next call.
///
/// When `index` is `None`, the behaviour is byte-for-byte identical to the
/// pre-index code path.
///
/// Index errors never abort a scan: git is the source of truth and the index is
/// disposable, so a degraded index simply forces a recompute.
///
/// # Errors
///
/// Returns `Err` if git fails or if the default branch is not found.
pub fn open_loops_indexed(
    repo: &Path,
    root_label: &str,
    opts: &ScanOptions,
    index: Option<&Index>,
) -> Result<(Vec<OpenLoop>, Option<InvUpdate>)> {
    // Resolve default branch and its SHA once (PERF-2: avoid duplicate rev-parse).
    let (default, default_sha) = default_branch_and_sha(repo)?;

    let common_dir = git_common_dir(repo)?;
    let repo_name = repo_name_from_common_dir(&common_dir);

    // -- Refs-fingerprint gate (#13) ---------------------------------------
    // Compute the fingerprint once; reused for both the read gate and the
    // write-through below.
    let refs_fp = refs_fingerprint(&common_dir);
    let gate_hash = inventory::common_dir_hash(&common_dir);

    if let Some(idx) = index {
        if !opts.fresh {
            if let Some(rows) = idx.cached_loops(&gate_hash, refs_fp, &default_sha) {
                // A hit must serve what the caller needs: if ahead/behind were
                // requested but the cached rows lack them, treat as a miss and
                // recompute (never hand back None to a caller that asked).
                let serves = !opts.need_ahead_behind || rows.iter().all(|r| r.ahead.is_some());
                if serves {
                    let loops = rows
                        .into_iter()
                        .map(|r| OpenLoop {
                            root_label: root_label.to_string(),
                            repo_name: repo_name.clone(),
                            repo_path: r.worktree_path,
                            branch: r.branch,
                            head_sha: r.head_sha,
                            last_commit: r.last_commit,
                            ahead: r.ahead,
                            behind: r.behind,
                        })
                        .collect();
                    // Cache hit returns no inventory update: the heavy phase did
                    // not run, so there is nothing new to memoise.
                    return Ok((loops, None));
                }
            }
        }
    }
    let worktrees = worktree_map(repo).unwrap_or_else(|e| {
        eprintln!(
            "warning: git worktree list failed in {}: {e:#}; session matching falls back to the repo path",
            repo.display()
        );
        std::collections::HashMap::new()
    });
    let merged: std::collections::HashSet<String> = git(
        repo,
        &["branch", "--merged", &default, "--format=%(refname:short)"],
    )?
    .lines()
    .map(|s| s.trim().to_string())
    .collect();
    let raw = git(
        repo,
        &[
            "for-each-ref",
            "refs/heads",
            "--format=%(refname:short)%09%(objectname)%09%(committerdate:iso8601-strict)",
        ],
    )?;

    // Determine whether to use the inventory memo for this scan.
    let use_inventory = opts.need_ahead_behind && opts.inventory_dir.is_some();

    // Robustness: if default_sha is empty, skip memoisation to avoid poisoning the cache.
    let use_inventory = use_inventory && !default_sha.is_empty();

    let hash = if use_inventory {
        inventory::common_dir_hash(&common_dir)
    } else {
        String::new()
    };

    // Load the existing inventory file unless `--fresh` was requested.
    // Destructure inventory_dir once to avoid .unwrap() landmine.
    let existing: Option<InventoryFile> = if use_inventory && !opts.fresh {
        if let Some(inv_dir) = &opts.inventory_dir {
            let store = InventoryStore {
                dir: inv_dir.clone(),
            };
            store.load(&hash)
        } else {
            None
        }
    } else {
        None
    };

    let now = Utc::now();
    let repo_canonical = std::fs::canonicalize(repo).unwrap_or_else(|_| repo.to_path_buf());
    let mut new_memos: Vec<LoopMemo> = Vec::new();
    let mut result = Vec::new();

    for line in raw.lines() {
        let mut parts = line.split('\t');
        let (Some(branch), Some(sha), Some(date)) = (parts.next(), parts.next(), parts.next())
        else {
            eprintln!("warning: unexpected line from git for-each-ref ignored: {line:?}");
            continue;
        };
        if branch == default || merged.contains(branch) {
            continue;
        }

        let (ahead, behind) = if opts.need_ahead_behind {
            let cached = if use_inventory {
                existing.as_ref().and_then(|f| {
                    inventory::lookup_ahead_behind(
                        f,
                        branch,
                        sha,
                        &default_sha,
                        opts.inventory_ttl_secs,
                        now,
                    )
                })
            } else {
                None
            };

            let (a, b) = if let Some(hit) = cached {
                hit
            } else {
                let counts = git(
                    repo,
                    &[
                        "rev-list",
                        "--left-right",
                        "--count",
                        &format!("{default}...{branch}"),
                    ],
                )?;
                let mut c = counts.split_whitespace();
                let behind_val: u32 = c.next().unwrap_or("0").parse().unwrap_or(0);
                let ahead_val: u32 = c.next().unwrap_or("0").parse().unwrap_or(0);
                (ahead_val, behind_val)
            };

            if use_inventory {
                new_memos.push(LoopMemo {
                    branch: branch.to_string(),
                    head_sha: sha.to_string(),
                    ab_base_sha: default_sha.clone(),
                    ahead: a,
                    behind: b,
                });
            }
            (Some(a), Some(b))
        } else {
            (None, None)
        };

        let last_commit = DateTime::parse_from_rfc3339(date)
            .with_context(|| format!("invalid date from git: {date}"))?
            .with_timezone(&Utc);
        let repo_path = worktrees
            .get(branch)
            .cloned()
            .unwrap_or_else(|| repo.to_path_buf());
        result.push(OpenLoop {
            root_label: root_label.to_string(),
            repo_name: repo_name.clone(),
            repo_path,
            branch: branch.to_string(),
            head_sha: sha.to_string(),
            last_commit,
            ahead,
            behind,
        });
    }

    let inventory_update = if use_inventory {
        Some((
            hash,
            InventoryFile {
                repo_path: repo_canonical,
                indexed_at: now,
                loops: new_memos,
            },
        ))
    } else {
        None
    };

    // -- Write-through to the index (#13) ----------------------------------
    // A miss (or `--fresh`) just recomputed everything: persist it so the next
    // unchanged-refs scan hits the gate and skips the heavy git phase. Index
    // errors are swallowed inside `put_loops` (git is the source of truth).
    if let Some(idx) = index {
        let rows: Vec<crate::index::LoopRow> = result
            .iter()
            .map(|l| crate::index::LoopRow {
                branch: l.branch.clone(),
                head_sha: l.head_sha.clone(),
                base_sha: default_sha.clone(),
                ahead: l.ahead,
                behind: l.behind,
                last_commit: l.last_commit,
                worktree_path: l.repo_path.clone(),
            })
            .collect();
        idx.put_loops(
            &gate_hash,
            repo,
            &common_dir,
            &default,
            &default_sha,
            refs_fp,
            &rows,
        );
    }

    Ok((result, inventory_update))
}

/// Scans all repos found under the roots in parallel.
///
/// `repo_filter`, when set, retains only repos whose canonical name (from dedup)
/// matches before `open_loops` runs. Individual repo failures become warnings and
/// never abort the scan.
///
/// Returns `(loops, warnings, inventory_updates)` where `inventory_updates` is a
/// vec of `(hash, file)` pairs ready for write-through by the caller.
pub fn scan(
    roots: &[PathBuf],
    labels: &[(PathBuf, String)],
    scan_depth: usize,
    opts: &ScanOptions,
    repo_filter: Option<&str>,
) -> (Vec<OpenLoop>, Vec<String>, Vec<InvUpdate>) {
    scan_indexed(roots, labels, scan_depth, opts, repo_filter, None)
}

/// Cheap per-repo git values computed once, in parallel, for the gate.
///
/// All three are derived from git subprocess calls (`default_branch_and_sha`,
/// `git_common_dir`) plus a recursive ref-tree stat (`refs_fingerprint`). They
/// are computed exactly ONCE per repo and threaded through both the gate read
/// and the write-through so the git work never runs more than once per cold repo.
struct GateInputs {
    default: String,
    default_sha: String,
    common_dir: PathBuf,
    refs_fp: i64,
    gate_hash: String,
}

/// Like [`scan`] but optionally consults a SQLite `index` for the
/// refs-fingerprint gate (#13) and to cache `--git-common-dir` during repo
/// discovery.
///
/// `rusqlite::Connection` is `Send` but `!Sync`, so a single `&Index` cannot be
/// shared across the parallel scan threads. We therefore keep the cheap SQLite
/// reads/writes on the calling thread while running ALL git work in parallel —
/// the same parallelism the pre-index `scan` had. The shape is three phases:
///
/// 1. **Parallel git probes** (`thread::scope`): for every repo, compute the
///    cheap gate inputs (`default_branch_and_sha`, `git_common_dir`,
///    `refs_fingerprint`) ONCE. This replaces the old serial `git` fan-out that
///    serialized ~2 subprocess spawns per repo on the calling thread.
/// 2. **Sequential SQLite gate** on the calling thread: one indexed
///    `cached_loops` read per repo, using the precomputed inputs. Hits are
///    served directly; misses are deferred.
/// 3. **Parallel recompute** of the misses (`thread::scope`), followed by a
///    sequential write-through that REUSES the already-computed gate inputs
///    (no third re-shell of the git values).
///
/// `--fresh` runs phase 1 (so the write-through still has its inputs) but skips
/// the phase-2 gate read, treating every repo as a miss — the gate is bypassed
/// yet the index is still refreshed, matching the pre-index behaviour.
///
/// The `None` path is byte-for-byte identical to the pre-index `scan`: it skips
/// phases 1–2 entirely and recomputes every repo in parallel with no gate and
/// no write-through.
pub fn scan_indexed(
    roots: &[PathBuf],
    labels: &[(PathBuf, String)],
    scan_depth: usize,
    opts: &ScanOptions,
    repo_filter: Option<&str>,
    index: Option<&Index>,
) -> (Vec<OpenLoop>, Vec<String>, Vec<InvUpdate>) {
    let (mut repos, mut warnings) = find_repos_cached(roots, scan_depth, index);
    if let Some(filter) = repo_filter {
        let needle = filter.to_lowercase();
        repos.retain(|r| r.repo_name.to_lowercase().contains(&needle));
    }

    let mut all = Vec::new();
    let mut inventory_updates = Vec::new();

    // No index: the gate is inert. Recompute everything in parallel exactly like
    // the pre-index `scan` (the `None`-path contract — no gate inputs, no
    // write-through).
    let Some(idx) = index else {
        let misses: Vec<&RepoCandidate> = repos.iter().collect();
        recompute_misses(&misses, &[], labels, opts, None, &mut all, &mut warnings)
            .into_iter()
            .for_each(|u| inventory_updates.push(u));
        return (all, warnings, inventory_updates);
    };

    // Phase 1: compute the cheap gate inputs for every repo IN PARALLEL. This is
    // the git work the old code ran serially in `try_gate`. Results are
    // positionally aligned with `repos`; `Err` is a fatal git error reported
    // once. The inputs are reused for both the gate read and the write-through.
    let gate_inputs: Vec<Result<GateInputs>> = std::thread::scope(|s| {
        let handles: Vec<_> = repos
            .iter()
            .map(|repo| {
                let path = repo.path.clone();
                s.spawn(move || compute_gate_inputs(&path))
            })
            .collect();
        handles
            .into_iter()
            .map(|h| {
                h.join()
                    .unwrap_or_else(|_| Err(anyhow::anyhow!("panic while probing repository")))
            })
            .collect()
    });

    // Phase 2: sequential SQLite gate read on the calling thread (skipped under
    // `--fresh`, which still recomputes and writes through). Hits are served
    // from cache; misses (and their precomputed inputs) are deferred.
    let mut misses: Vec<&RepoCandidate> = Vec::new();
    let mut miss_inputs: Vec<GateInputs> = Vec::new();
    for (repo, inputs) in repos.iter().zip(gate_inputs) {
        let inputs = match inputs {
            Ok(i) => i,
            // Propagate the git error so the repo is reported once, not retried.
            Err(e) => {
                warnings.push(format!("{}: {e:#}", repo.path.display()));
                continue;
            }
        };
        let label = crate::config::label_for_repo(labels, &repo.path);
        // `--fresh` bypasses the gate read but keeps the write-through below.
        let hit = if opts.fresh {
            None
        } else {
            gate_lookup(&label, opts, idx, &inputs)
        };
        match hit {
            Some(mut loops) => all.append(&mut loops),
            None => {
                misses.push(repo);
                miss_inputs.push(inputs);
            }
        }
    }

    // Phase 3: recompute the misses in parallel and write them through using the
    // gate inputs already computed in phase 1 (no re-shell of the git values).
    recompute_misses(
        &misses,
        &miss_inputs,
        labels,
        opts,
        index,
        &mut all,
        &mut warnings,
    )
    .into_iter()
    .for_each(|u| inventory_updates.push(u));

    (all, warnings, inventory_updates)
}

/// Recomputes `misses` in parallel (heavy git phase) and, when an index is
/// present, writes each result through using the matching precomputed
/// `gate_inputs` (positionally aligned with `misses`; empty when there is no
/// index / `--fresh`, in which case write-through is skipped). Appends loops to
/// `all`, warnings to `warnings`, and returns the inventory updates.
fn recompute_misses(
    misses: &[&RepoCandidate],
    gate_inputs: &[GateInputs],
    labels: &[(PathBuf, String)],
    opts: &ScanOptions,
    index: Option<&Index>,
    all: &mut Vec<OpenLoop>,
    warnings: &mut Vec<String>,
) -> Vec<InvUpdate> {
    let mut inventory_updates = Vec::new();
    let results: Vec<Result<(Vec<OpenLoop>, Option<InvUpdate>)>> = std::thread::scope(|s| {
        let handles: Vec<_> = misses
            .iter()
            .map(|repo| {
                let label = crate::config::label_for_repo(labels, &repo.path);
                let path = repo.path.clone();
                s.spawn(move || open_loops(&path, &label, opts))
            })
            .collect();
        handles
            .into_iter()
            .map(|h| {
                h.join()
                    .unwrap_or_else(|_| Err(anyhow::anyhow!("panic while scanning repository")))
            })
            .collect()
    });

    for (i, (repo, res)) in misses.iter().zip(results).enumerate() {
        match res {
            Ok((loops, inv)) => {
                // Write the freshly computed loops through to the index using the
                // gate inputs already computed in phase 1 (no re-shell).
                if let Some(idx) = index {
                    if let Some(inputs) = gate_inputs.get(i) {
                        write_through(&repo.path, &loops, idx, inputs);
                    }
                }
                all.extend(loops);
                if let Some(update) = inv {
                    inventory_updates.push(update);
                }
            }
            Err(e) => warnings.push(format!("{}: {e:#}", repo.path.display())),
        }
    }
    inventory_updates
}

/// Computes the cheap per-repo gate inputs once (called in parallel, phase 1).
///
/// # Errors
///
/// Returns `Err` if the default branch or common-dir cannot be resolved.
fn compute_gate_inputs(repo: &Path) -> Result<GateInputs> {
    let (default, default_sha) = default_branch_and_sha(repo)?;
    let common_dir = git_common_dir(repo)?;
    let refs_fp = refs_fingerprint(&common_dir);
    let gate_hash = inventory::common_dir_hash(&common_dir);
    Ok(GateInputs {
        default,
        default_sha,
        common_dir,
        refs_fp,
        gate_hash,
    })
}

/// Sequential SQLite gate read for one repo using its precomputed inputs.
/// `Some(loops)` is a hit the caller can use directly; `None` is a cache miss to
/// be recomputed in parallel. No git work happens here — only one indexed read.
fn gate_lookup(
    label: &str,
    opts: &ScanOptions,
    idx: &Index,
    inputs: &GateInputs,
) -> Option<Vec<OpenLoop>> {
    let rows = idx.cached_loops(&inputs.gate_hash, inputs.refs_fp, &inputs.default_sha)?;
    // Serve only if the cached rows satisfy the caller's ahead/behind need.
    if opts.need_ahead_behind && !rows.iter().all(|r| r.ahead.is_some()) {
        return None;
    }
    let repo_name = repo_name_from_common_dir(&inputs.common_dir);
    let loops = rows
        .into_iter()
        .map(|r| OpenLoop {
            root_label: label.to_string(),
            repo_name: repo_name.clone(),
            repo_path: r.worktree_path,
            branch: r.branch,
            head_sha: r.head_sha,
            last_commit: r.last_commit,
            ahead: r.ahead,
            behind: r.behind,
        })
        .collect();
    Some(loops)
}

/// Persists freshly recomputed `loops` for `repo` to the index after a miss,
/// reusing the gate inputs already computed in phase 1 (no re-shell of git).
fn write_through(repo: &Path, loops: &[OpenLoop], idx: &Index, inputs: &GateInputs) {
    let rows: Vec<crate::index::LoopRow> = loops
        .iter()
        .map(|l| crate::index::LoopRow {
            branch: l.branch.clone(),
            head_sha: l.head_sha.clone(),
            base_sha: inputs.default_sha.clone(),
            ahead: l.ahead,
            behind: l.behind,
            last_commit: l.last_commit,
            worktree_path: l.repo_path.clone(),
        })
        .collect();
    idx.put_loops(
        &inputs.gate_hash,
        repo,
        &inputs.common_dir,
        &inputs.default,
        &inputs.default_sha,
        inputs.refs_fp,
        &rows,
    );
}

/// Branch-exclusive commits relative to the default (for the distillation prompt).
///
/// # Errors
///
/// Returns `Err` if git fails.
pub fn git_log(repo: &Path, default: &str, branch: &str) -> Result<String> {
    git(repo, &["log", "--oneline", &format!("{default}..{branch}")])
}

/// Diffstat of the branch against the base (for the distillation prompt).
///
/// # Errors
///
/// Returns `Err` if git fails.
pub fn diffstat(repo: &Path, default: &str, branch: &str) -> Result<String> {
    git(repo, &["diff", "--stat", &format!("{default}...{branch}")])
}

/// Time window of the branch-exclusive commits.
///
/// Used to filter out AI sessions that predate the branch work.
///
/// # Errors
///
/// Returns `Err` if git fails or if there are no commits on the branch.
pub fn commit_window(
    repo: &Path,
    default: &str,
    branch: &str,
) -> Result<(DateTime<Utc>, DateTime<Utc>)> {
    let raw = git(
        repo,
        &["log", "--format=%cI", &format!("{default}..{branch}")],
    )?;
    let mut dates: Vec<DateTime<Utc>> = raw
        .lines()
        .filter_map(|l| DateTime::parse_from_rfc3339(l.trim()).ok())
        .map(|d| d.with_timezone(&Utc))
        .collect();
    if dates.is_empty() {
        // branch has no exclusive commit: fall back to its latest commit
        let head = git(repo, &["log", "-1", "--format=%cI", branch])?;
        dates.push(DateTime::parse_from_rfc3339(head.trim())?.with_timezone(&Utc));
    }
    let min = dates
        .iter()
        .min()
        .copied()
        .ok_or_else(|| anyhow::anyhow!("no commit dates for {branch}"))?;
    let max = dates
        .iter()
        .max()
        .copied()
        .ok_or_else(|| anyhow::anyhow!("no commit dates for {branch}"))?;
    Ok((min, max))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::index::Index;
    use crate::testutil;

    /// Helper: call `open_loops` without inventory, returning only the loops vec.
    fn open_loops_simple(
        repo: &std::path::Path,
        root_label: &str,
        need_ahead_behind: bool,
    ) -> Vec<OpenLoop> {
        let opts = ScanOptions {
            need_ahead_behind,
            ..ScanOptions::default()
        };
        open_loops(repo, root_label, &opts).unwrap().0
    }

    /// Helper: call `scan` without inventory, returning only `(loops, warnings)`.
    fn scan_simple(
        roots: &[PathBuf],
        labels: &[(PathBuf, String)],
        depth: usize,
        need_ahead_behind: bool,
        filter: Option<&str>,
    ) -> (Vec<OpenLoop>, Vec<String>) {
        let opts = ScanOptions {
            need_ahead_behind,
            ..ScanOptions::default()
        };
        let (loops, warnings, _inv) = scan(roots, labels, depth, &opts, filter);
        (loops, warnings)
    }

    fn assert_same_path(actual: &std::path::Path, expected: &std::path::Path) {
        let a = std::fs::canonicalize(actual).unwrap_or_else(|_| actual.to_path_buf());
        let b = std::fs::canonicalize(expected).unwrap_or_else(|_| expected.to_path_buf());
        assert_eq!(a, b);
    }

    #[test]
    fn default_branch_detects_main() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        assert_eq!(default_branch(&repo).unwrap(), "main");
    }

    #[test]
    fn default_branch_honours_origin_head_when_target_is_local() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo); // main + commit
        testutil::git(&repo, &["branch", "develop"]); // local develop exists
        testutil::git(
            &repo,
            &[
                "symbolic-ref",
                "refs/remotes/origin/HEAD",
                "refs/remotes/origin/develop",
            ],
        );
        // origin/HEAD wins over main because its target resolves locally.
        assert_eq!(default_branch(&repo).unwrap(), "develop");
    }

    #[test]
    fn default_branch_falls_back_when_origin_head_target_missing() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo); // main + commit, no local "ghost"
        testutil::git(
            &repo,
            &[
                "symbolic-ref",
                "refs/remotes/origin/HEAD",
                "refs/remotes/origin/ghost",
            ],
        );
        // Stale origin/HEAD target → fall through to main, not an error.
        assert_eq!(default_branch(&repo).unwrap(), "main");
    }

    #[test]
    fn git_fails_with_contextual_message() {
        let tmp = tempfile::tempdir().unwrap();
        // directory is not a git repo
        let err = git(tmp.path(), &["status"]).unwrap_err();
        assert!(err.to_string().contains(&tmp.path().display().to_string()));
    }

    #[test]
    fn find_repos_dedups_container_and_worktrees() {
        let tmp = tempfile::tempdir().unwrap();
        let container = tmp.path().join("my-app");
        testutil::init_bare_worktree_container(&container);
        let dev = container.join("dev");
        testutil::add_named_worktree(&container, "dev", "dev");
        let (repos, warnings) = find_repos(&[container.clone(), dev], 4);
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(repos.len(), 1);
        assert_eq!(repos[0].path, container);
    }

    #[test]
    fn find_repos_respects_scan_depth_and_skips_hidden() {
        let tmp = tempfile::tempdir().unwrap();
        testutil::init_repo(&tmp.path().join("a/b/c/repo-deep"));
        testutil::init_repo(&tmp.path().join("a/b/repo-mid"));
        testutil::init_repo(&tmp.path().join("repo-shallow"));
        testutil::init_repo(&tmp.path().join(".hidden/repo3"));

        let (repos, _) = find_repos(&[tmp.path().to_path_buf()], 4);
        let names: Vec<_> = repos
            .iter()
            .filter_map(|r| r.path.file_name())
            .map(|n| n.to_string_lossy().into_owned())
            .collect();
        assert!(names.contains(&"repo-deep".to_string()));
        assert!(names.contains(&"repo-mid".to_string()));
        assert!(names.contains(&"repo-shallow".to_string()));
        assert!(!names.contains(&"repo3".to_string()));

        let (shallow, _) = find_repos(&[tmp.path().to_path_buf()], 2);
        let shallow_names: Vec<_> = shallow
            .iter()
            .filter_map(|r| r.path.file_name())
            .map(|n| n.to_string_lossy().into_owned())
            .collect();
        assert!(!shallow_names.contains(&"repo-deep".to_string()));
        assert!(shallow_names.contains(&"repo-shallow".to_string()));
    }

    #[test]
    fn find_repos_finds_normal_git_dir_repo() {
        let tmp = tempfile::tempdir().unwrap();
        testutil::init_repo(&tmp.path().join("app"));
        let (repos, _) = find_repos(&[tmp.path().to_path_buf()], 4);
        assert_eq!(repos.len(), 1);
    }

    #[test]
    fn find_repos_finds_bare_worktree_container_via_git_file() {
        let tmp = tempfile::tempdir().unwrap();
        let container = tmp.path().join("my-app");
        testutil::init_bare_worktree_container(&container);
        let (repos, _) = find_repos(&[tmp.path().to_path_buf()], 4);
        assert_eq!(repos.len(), 1);
        assert_eq!(repos[0].path, container);
    }

    #[test]
    fn find_repos_finds_pure_bare_repo() {
        let tmp = tempfile::tempdir().unwrap();
        let bare = tmp.path().join("foo.git");
        testutil::init_bare_repo(&bare);
        testutil::seed_bare_main(&bare);
        let (repos, _) = find_repos(&[tmp.path().to_path_buf()], 4);
        assert_eq!(repos.len(), 1);
        assert_eq!(repos[0].path, bare);
    }

    #[test]
    fn open_loops_uses_common_dir_repo_name_in_bare_layout() {
        let tmp = tempfile::tempdir().unwrap();
        let container = tmp.path().join("my-app");
        testutil::init_bare_worktree_container(&container);
        testutil::add_named_worktree(&container, "dev", "dev");
        testutil::add_branch_on_bare(&container.join(".bare"), "feat/x", "x.txt");

        let loops = open_loops_simple(&container, "root", true);
        assert_eq!(loops.len(), 1);
        assert_eq!(loops[0].repo_name, "my-app");
        assert_eq!(loops[0].branch, "feat/x");
        assert_eq!(loops[0].key(), "root/my-app/feat/x");
    }

    #[test]
    fn open_loops_bare_root_repo_name_strips_dot_git_suffix() {
        let tmp = tempfile::tempdir().unwrap();
        let bare = tmp.path().join("foo.git");
        testutil::init_bare_repo(&bare);
        testutil::seed_bare_main(&bare);
        testutil::add_branch_on_bare(&bare, "feat/y", "y.txt");

        let loops = open_loops_simple(&bare, "r", true);
        assert_eq!(loops[0].repo_name, "foo");
    }

    #[test]
    fn open_loops_finds_unmerged_ignores_merged_and_default() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt");
        testutil::git(&repo, &["branch", "merged"]); // points to main => merged

        let loops = open_loops_simple(&repo, "root", true);
        assert_eq!(loops.len(), 1);
        let l = &loops[0];
        assert_eq!(l.branch, "feat/x");
        assert_eq!(l.repo_name, "app");
        assert_eq!(l.root_label, "root");
        assert_eq!(l.key(), "root/app/feat/x");
        assert_eq!(l.ahead, Some(1));
        assert_eq!(l.behind, Some(0));
        assert_eq!(l.head_sha.len(), 40);
    }

    #[test]
    fn open_loops_sets_repo_path_to_worktree_when_branch_checked_out() {
        let tmp = tempfile::tempdir().unwrap();
        let container = tmp.path().join("my-app");
        testutil::init_bare_worktree_container(&container);
        testutil::add_worktree_with_commit(&container, "feat-x", "feat/x", "x.txt");

        let loops = open_loops_simple(&container, "root", true);
        let lp = loops
            .iter()
            .find(|l| l.branch == "feat/x")
            .expect("feat/x loop");
        assert_same_path(&lp.repo_path, &container.join("feat-x"));
    }

    #[test]
    fn open_loops_falls_back_to_container_when_branch_has_no_worktree() {
        let tmp = tempfile::tempdir().unwrap();
        let container = tmp.path().join("my-app");
        testutil::init_bare_worktree_container(&container);
        // feat/y exists in the store but is NOT checked out in any worktree
        testutil::add_branch_on_bare(&container.join(".bare"), "feat/y", "y.txt");

        let loops = open_loops_simple(&container, "root", true);
        let lp = loops
            .iter()
            .find(|l| l.branch == "feat/y")
            .expect("feat/y loop");
        assert_eq!(lp.repo_path, container);
    }

    #[test]
    fn open_loops_normal_repo_keeps_repo_path_as_repo_dir() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt"); // checks out feat/x then back to main
        let loops = open_loops_simple(&repo, "root", true);
        assert_eq!(loops[0].branch, "feat/x");
        assert_eq!(loops[0].repo_path, repo); // not checked out in a worktree → fallback
    }

    #[test]
    fn open_loops_skips_rev_list_when_need_ahead_behind_false() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt");

        let loops = open_loops_simple(&repo, "root", false);
        assert_eq!(loops.len(), 1);
        assert_eq!(loops[0].ahead, None);
        assert_eq!(loops[0].behind, None);
    }

    #[test]
    fn open_loops_computes_ahead_behind_when_need_ahead_behind_true() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt");

        let loops = open_loops_simple(&repo, "root", true);
        assert_eq!(loops.len(), 1);
        assert_eq!(loops[0].ahead, Some(1));
        assert_eq!(loops[0].behind, Some(0));
    }

    #[test]
    fn open_loops_reuses_inventory_memo_on_repeated_scan() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt");
        let inv_dir = tmp.path().join("inv");

        let opts = ScanOptions {
            need_ahead_behind: true,
            fresh: false,
            inventory_dir: Some(inv_dir.clone()),
            inventory_ttl_secs: 0,
        };

        // First call: no cache → runs rev-list and writes inventory.
        let (loops1, inv1) = open_loops(&repo, "root", &opts).unwrap();
        assert_eq!(loops1.len(), 1);
        assert_eq!(loops1[0].ahead, Some(1));
        let (hash, file) = inv1.unwrap();
        let store = InventoryStore {
            dir: inv_dir.clone(),
        };
        store.save(&hash, &file).unwrap();

        // Second call: memo present → cache hit; ahead/behind same.
        let (loops2, inv2) = open_loops(&repo, "root", &opts).unwrap();
        assert_eq!(loops2.len(), 1);
        assert_eq!(loops2[0].ahead, Some(1));
        assert_eq!(loops2[0].behind, Some(0));
        // inventory update is still returned (for write-through)
        assert!(inv2.is_some());
    }

    #[test]
    fn open_loops_fresh_ignores_inventory_memo() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt");
        let inv_dir = tmp.path().join("inv");

        // Pre-seed inventory with wrong ahead/behind values to detect if it's
        // being used.
        let common = git_common_dir(&repo).unwrap();
        let hash = crate::inventory::common_dir_hash(&common);
        let store = InventoryStore {
            dir: inv_dir.clone(),
        };
        let fake_sha = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
        let stub_file = InventoryFile {
            repo_path: repo.clone(),
            indexed_at: chrono::Utc::now(),
            loops: vec![LoopMemo {
                branch: "feat/x".to_string(),
                head_sha: fake_sha.to_string(),
                ab_base_sha: fake_sha.to_string(),
                ahead: 99,
                behind: 99,
            }],
        };
        store.save(&hash, &stub_file).unwrap();

        let opts = ScanOptions {
            need_ahead_behind: true,
            fresh: true, // <-- bypass cache
            inventory_dir: Some(inv_dir.clone()),
            inventory_ttl_secs: 0,
        };
        let (loops, _) = open_loops(&repo, "root", &opts).unwrap();
        // real values, not the stubbed 99/99
        assert_eq!(loops[0].ahead, Some(1));
        assert_eq!(loops[0].behind, Some(0));
    }

    #[test]
    fn scan_repo_filter_pushdown_skips_non_matching_repos() {
        let tmp = tempfile::tempdir().unwrap();
        let api = tmp.path().join("api-service");
        let web = tmp.path().join("web-app");
        testutil::init_repo(&api);
        testutil::init_repo(&web);
        testutil::add_branch_with_commit(&api, "feat/api", "a.txt");
        testutil::add_branch_with_commit(&web, "feat/web", "w.txt");

        let labels = vec![(tmp.path().to_path_buf(), "r".to_string())];
        let (loops, _) = scan_simple(&[tmp.path().to_path_buf()], &labels, 4, false, Some("api"));
        assert_eq!(loops.len(), 1);
        assert_eq!(loops[0].repo_name, "api-service");
        assert_eq!(loops[0].branch, "feat/api");
    }

    #[test]
    fn repo_name_hint_strips_dot_git_suffix() {
        assert_eq!(repo_name_hint(std::path::Path::new("/srv/foo.git")), "foo");
    }

    #[test]
    fn scan_repo_filter_is_case_insensitive() {
        let tmp = tempfile::tempdir().unwrap();
        let api = tmp.path().join("API-Service");
        testutil::init_repo(&api);
        testutil::add_branch_with_commit(&api, "feat/api", "a.txt");

        let labels = vec![(tmp.path().to_path_buf(), "r".to_string())];
        // lowercase filter must match a mixed-case repo dir (both sides lowered)
        let (loops, _) = scan_simple(&[tmp.path().to_path_buf()], &labels, 4, false, Some("api"));
        assert_eq!(loops.len(), 1);
        assert_eq!(loops[0].repo_name, "API-Service");
    }

    #[test]
    fn scan_repo_filter_matching_nothing_yields_no_loops() {
        let tmp = tempfile::tempdir().unwrap();
        let api = tmp.path().join("api-service");
        testutil::init_repo(&api);
        testutil::add_branch_with_commit(&api, "feat/api", "a.txt");

        let labels = vec![(tmp.path().to_path_buf(), "r".to_string())];
        let (loops, warnings) = scan_simple(
            &[tmp.path().to_path_buf()],
            &labels,
            4,
            false,
            Some("zzz-nope"),
        );
        assert!(loops.is_empty());
        assert!(
            warnings.is_empty(),
            "filtered-out repos must not warn: {warnings:?}"
        );
    }

    #[test]
    fn scan_aggregates_repos_and_reports_warning_without_aborting() {
        let tmp = tempfile::tempdir().unwrap();
        let good = tmp.path().join("good");
        testutil::init_repo(&good);
        testutil::add_branch_with_commit(&good, "feat/ok", "ok.txt");
        // truly broken repo: no commits, so default_branch fails
        let empty = tmp.path().join("empty");
        std::fs::create_dir_all(&empty).unwrap();
        testutil::git(&empty, &["init", "-b", "main"]);

        let labels = vec![(tmp.path().to_path_buf(), "r".to_string())];
        let (loops, warnings) = scan_simple(&[tmp.path().to_path_buf()], &labels, 4, true, None);
        assert_eq!(loops.len(), 1);
        assert_eq!(loops[0].key(), "r/good/feat/ok");
        assert_eq!(warnings.len(), 1);
        assert!(warnings[0].contains("empty"));
    }

    #[test]
    fn context_helpers_return_commits_and_window() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt");

        let log = git_log(&repo, "main", "feat/x").unwrap();
        assert!(log.contains("wip feat/x"));
        let stat = diffstat(&repo, "main", "feat/x").unwrap();
        assert!(stat.contains("x.txt"));
        let (start, end) = commit_window(&repo, "main", "feat/x").unwrap();
        assert!(start <= end);
    }

    #[test]
    fn default_branch_detects_master_fallback() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();
        testutil::git(repo, &["init", "-b", "master"]);
        std::fs::write(repo.join("a.txt"), "a").unwrap();
        testutil::git(repo, &["add", "."]);
        testutil::git(repo, &["commit", "-m", "init"]);
        assert_eq!(default_branch(repo).unwrap(), "master");
    }

    #[test]
    fn default_branch_errors_without_main_or_master() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();
        testutil::git(repo, &["init", "-b", "trunk"]);
        // no commits: refs/heads/main and refs/heads/master do not exist
        let err = default_branch(repo).unwrap_err();
        assert!(err.to_string().contains("couldn't find the default branch"));
    }

    #[test]
    fn git_common_dir_resolves_normal_and_bare_pointer() {
        let tmp = tempfile::tempdir().unwrap();
        let normal = tmp.path().join("app");
        testutil::init_repo(&normal);
        let normal_common = git_common_dir(&normal).unwrap();
        assert!(normal_common.ends_with(".git"));

        let container = tmp.path().join("container");
        testutil::init_bare_worktree_container(&container);
        let bare_common = git_common_dir(&container).unwrap();
        assert!(bare_common.ends_with(".bare"));
    }

    #[test]
    fn parse_worktree_porcelain_extracts_branches_and_flags() {
        let out = "\
worktree /home/u/app/main
HEAD aaaaaaaa
branch refs/heads/main

worktree /home/u/app/feat-x
HEAD bbbbbbbb
branch refs/heads/feat/x

worktree /home/u/app/detached
HEAD cccccccc
detached

worktree /home/u/app/.bare
bare
";
        let entries = parse_worktree_porcelain(out);
        assert_eq!(entries.len(), 4);
        assert_eq!(entries[0].branch.as_deref(), Some("main"));
        assert_eq!(
            entries[0].path,
            std::path::PathBuf::from("/home/u/app/main")
        );
        assert_eq!(entries[1].branch.as_deref(), Some("feat/x")); // slash preserved
        assert_eq!(entries[2].branch, None); // detached
        assert!(entries[3].bare);
        assert_eq!(entries[3].branch, None);
    }

    #[test]
    fn parse_worktree_porcelain_marks_prunable_and_handles_empty() {
        assert!(parse_worktree_porcelain("").is_empty());
        let out = "worktree /gone\nprunable gitdir file points to non-existent location\n";
        let entries = parse_worktree_porcelain(out);
        assert_eq!(entries.len(), 1);
        assert!(entries[0].prunable);
        assert_eq!(entries[0].branch, None);
    }

    #[test]
    fn worktree_map_maps_checked_out_branches_to_paths() {
        let tmp = tempfile::tempdir().unwrap();
        let container = tmp.path().join("my-app");
        testutil::init_bare_worktree_container(&container); // main worktree at container/main
        testutil::add_named_worktree(&container, "dev", "dev"); // dev worktree at container/dev

        let map = worktree_map(&container).unwrap();
        assert_same_path(map.get("main").unwrap(), &container.join("main"));
        assert_same_path(map.get("dev").unwrap(), &container.join("dev"));
        // the `.bare` entry is filtered out (no branch / bare)
        assert!(!map.values().any(|p| p.ends_with(".bare")));
    }

    #[test]
    fn worktree_map_errors_on_non_git_dir() {
        let tmp = tempfile::tempdir().unwrap();
        // a plain directory is not a git repo → git worktree list fails
        assert!(worktree_map(tmp.path()).is_err());
    }

    #[test]
    fn parse_worktree_porcelain_ignores_lines_before_first_worktree() {
        let out = "branch refs/heads/orphan\nHEAD deadbeef\nworktree /home/u/app/main\nbranch refs/heads/main\n";
        let entries = parse_worktree_porcelain(out);
        assert_eq!(entries.len(), 1);
        assert_eq!(
            entries[0].path,
            std::path::PathBuf::from("/home/u/app/main")
        );
        assert_eq!(entries[0].branch.as_deref(), Some("main"));
    }

    #[test]
    fn repo_name_from_common_dir_table() {
        use std::path::Path;

        let cases: &[(&str, &str)] = &[
            ("/home/u/my-app/.bare", "my-app"),
            ("/home/u/app/.git", "app"),
            ("/srv/git/foo.git", "foo"),
            ("/srv/git/myproject", "myproject"),
        ];
        for (common, want) in cases {
            assert_eq!(
                repo_name_from_common_dir(Path::new(common)),
                *want,
                "common_dir={common}"
            );
        }
    }

    // -----------------------------------------------------------------------
    // Task 2: cached find_repos / dedup tests
    // -----------------------------------------------------------------------

    /// (a) find_repos_cached with a fresh in-memory index populates repos.common_dir.
    #[test]
    fn find_repos_cached_populates_index() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);

        let index = Index::open_in_memory();
        let (repos, warnings) = find_repos_cached(&[tmp.path().to_path_buf()], 4, Some(&index));

        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(repos.len(), 1);

        // The index must now have an entry for this path.
        let (hash, cd) = index
            .cached_common_dir(&repo)
            .expect("index should have cached the common_dir after find_repos_cached");
        assert!(!hash.is_empty());
        assert!(
            cd.ends_with(".git"),
            "common_dir should end with .git, got: {cd:?}"
        );
    }

    /// (b) Second dedup_candidates_cached call reads from cache (SENTINEL proves
    ///     git_common_dir was NOT called again).
    ///
    /// We pre-seed the index with a fake/sentinel common_dir_hash for the repo
    /// path. If the cache is consulted, we get the sentinel back; if git is
    /// called instead, we get the real hash — different values prove the path
    /// taken.
    #[test]
    fn dedup_candidates_cached_uses_index_on_second_call() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);

        let index = Index::open_in_memory();

        // Pre-seed the index with a SENTINEL hash and a real-looking common_dir
        // so dedup can still build a RepoCandidate (repo_name_from_common_dir
        // only needs the path shape, not a real dir).
        let sentinel_hash = "sentinel_hash_no_git";
        let sentinel_cd = repo.join(".git"); // same shape as reality
        index.put_repo_common_dir(&repo, sentinel_hash, &sentinel_cd);

        // Now call dedup; it should hit the cache and return the sentinel.
        let (repos, warnings) = dedup_candidates_cached(vec![repo.clone()], Some(&index));

        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(repos.len(), 1);

        // The cached entry must still be the sentinel (git was not called to overwrite it).
        let (got_hash, _) = index
            .cached_common_dir(&repo)
            .expect("index entry must still exist");
        assert_eq!(
            got_hash, sentinel_hash,
            "sentinel hash changed — git was called instead of using cache"
        );
    }

    /// (c) N worktrees of the same repo → 1 RepoCandidate on the cached path.
    #[test]
    fn dedup_cached_n_worktrees_yields_one_repo() {
        let tmp = tempfile::tempdir().unwrap();
        let container = tmp.path().join("my-app");
        testutil::init_bare_worktree_container(&container);
        let dev = container.join("dev");
        testutil::add_named_worktree(&container, "dev", "dev");

        let index = Index::open_in_memory();

        // Both the container and the dev worktree point to the same common_dir.
        let candidates = vec![container.clone(), dev.clone()];
        let (repos, warnings) = dedup_candidates_cached(candidates, Some(&index));

        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(
            repos.len(),
            1,
            "N worktrees must dedup to 1 repo, got: {repos:?}"
        );
    }

    // -----------------------------------------------------------------------
    // Task 3: refs-fingerprint gate (#13)
    // -----------------------------------------------------------------------

    /// Helper: indexed open_loops with ahead/behind, returning only the loops.
    fn open_loops_indexed_simple(
        repo: &std::path::Path,
        idx: Option<&Index>,
        fresh: bool,
    ) -> Vec<OpenLoop> {
        let opts = ScanOptions {
            need_ahead_behind: true,
            fresh,
            ..ScanOptions::default()
        };
        open_loops_indexed(repo, "root", &opts, idx).unwrap().0
    }

    /// (a) ZERO rev-list proof. After warming the cache with the real value
    /// (ahead=1), we overwrite the *cached* ahead/behind with an impossible
    /// sentinel (999/888) while keeping the SAME refs_fingerprint + default_sha.
    /// A second indexed scan returns the sentinel — which the live git repo can
    /// never produce — proving the gate served cached data and `rev-list` (and
    /// for-each-ref / branch --merged) were not re-run.
    #[test]
    fn warm_scan_unchanged_refs_skips_rev_list() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt");

        let index = Index::open_in_memory();

        // First scan: miss → real compute (ahead=1) + write-through.
        let first = open_loops_indexed_simple(&repo, Some(&index), false);
        assert_eq!(first.len(), 1);
        assert_eq!(first[0].ahead, Some(1));
        assert_eq!(first[0].behind, Some(0));

        // Capture the live fingerprint/hash/default_sha, then poison the cached
        // row's ahead/behind with a value the repo cannot produce.
        let common = git_common_dir(&repo).unwrap();
        let hash = crate::inventory::common_dir_hash(&common);
        let refs_fp = refs_fingerprint(&common);
        let (default, default_sha) = default_branch_and_sha(&repo).unwrap();
        let poisoned = vec![crate::index::LoopRow {
            branch: "feat/x".into(),
            head_sha: first[0].head_sha.clone(),
            base_sha: default_sha.clone(),
            ahead: Some(999),
            behind: Some(888),
            last_commit: first[0].last_commit,
            worktree_path: first[0].repo_path.clone(),
        }];
        index.put_loops(
            &hash,
            &repo,
            &common,
            &default,
            &default_sha,
            refs_fp,
            &poisoned,
        );

        // Second scan: refs unchanged → gate HIT → returns the sentinel.
        let second = open_loops_indexed_simple(&repo, Some(&index), false);
        assert_eq!(second.len(), 1);
        assert_eq!(
            second[0].ahead,
            Some(999),
            "gate must serve cached ahead — git was re-run if this is 1"
        );
        assert_eq!(second[0].behind, Some(888));
    }

    /// (b) Advancing HEAD changes the fingerprint → recompute → fresh values.
    #[test]
    fn advancing_head_invalidates_and_recomputes() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt"); // ahead=1

        let index = Index::open_in_memory();

        // Warm + poison with a sentinel under the OLD fingerprint.
        let first = open_loops_indexed_simple(&repo, Some(&index), false);
        assert_eq!(first[0].ahead, Some(1));
        let common = git_common_dir(&repo).unwrap();
        let hash = crate::inventory::common_dir_hash(&common);
        let old_fp = refs_fingerprint(&common);
        let (default, default_sha) = default_branch_and_sha(&repo).unwrap();
        index.put_loops(
            &hash,
            &repo,
            &common,
            &default,
            &default_sha,
            old_fp,
            &[crate::index::LoopRow {
                branch: "feat/x".into(),
                head_sha: first[0].head_sha.clone(),
                base_sha: default_sha.clone(),
                ahead: Some(999),
                behind: Some(888),
                last_commit: first[0].last_commit,
                worktree_path: first[0].repo_path.clone(),
            }],
        );

        // Add a second commit on feat/x → new loose ref mtime → fingerprint bumps.
        testutil::git(&repo, &["checkout", "feat/x"]);
        std::fs::write(repo.join("x2.txt"), "x2").unwrap();
        testutil::git(&repo, &["add", "."]);
        testutil::git(&repo, &["commit", "-m", "wip more"]);
        testutil::git(&repo, &["checkout", "main"]);

        let new_fp = refs_fingerprint(&common);
        assert!(
            new_fp >= old_fp,
            "fingerprint must not go backwards: {old_fp} -> {new_fp}"
        );

        // Second scan: fingerprint differs → MISS → real recompute (ahead=2).
        let second = open_loops_indexed_simple(&repo, Some(&index), false);
        assert_eq!(
            second[0].ahead,
            Some(2),
            "must recompute after HEAD advance"
        );
        assert_eq!(second[0].behind, Some(0));
    }

    /// (c) Changing the default-branch SHA invalidates the cache even when the
    /// branch's own refs and the gross fingerprint match the stored default_sha.
    #[test]
    fn default_sha_change_invalidates() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt");

        let index = Index::open_in_memory();
        let first = open_loops_indexed_simple(&repo, Some(&index), false);
        assert_eq!(first[0].ahead, Some(1));

        let common = git_common_dir(&repo).unwrap();
        let hash = crate::inventory::common_dir_hash(&common);
        let refs_fp = refs_fingerprint(&common);
        let (default, _real_sha) = default_branch_and_sha(&repo).unwrap();

        // Store a poisoned row under a STALE default_sha but the live fingerprint.
        index.put_loops(
            &hash,
            &repo,
            &common,
            &default,
            "stale_default_sha_0000000000000000000000",
            refs_fp,
            &[crate::index::LoopRow {
                branch: "feat/x".into(),
                head_sha: first[0].head_sha.clone(),
                base_sha: "stale_default_sha_0000000000000000000000".into(),
                ahead: Some(999),
                behind: Some(888),
                last_commit: first[0].last_commit,
                worktree_path: first[0].repo_path.clone(),
            }],
        );

        // The live default_sha != stored stale default_sha → MISS → recompute.
        let second = open_loops_indexed_simple(&repo, Some(&index), false);
        assert_eq!(
            second[0].ahead,
            Some(1),
            "stale default_sha must force recompute, not serve 999"
        );
    }

    /// (d) `fresh: true` bypasses the gate even when a (poisoned) cache exists.
    #[test]
    fn fresh_bypasses_the_gate() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt");

        let index = Index::open_in_memory();
        let first = open_loops_indexed_simple(&repo, Some(&index), false);

        // Poison the cache under the live fingerprint/default_sha.
        let common = git_common_dir(&repo).unwrap();
        let hash = crate::inventory::common_dir_hash(&common);
        let refs_fp = refs_fingerprint(&common);
        let (default, default_sha) = default_branch_and_sha(&repo).unwrap();
        index.put_loops(
            &hash,
            &repo,
            &common,
            &default,
            &default_sha,
            refs_fp,
            &[crate::index::LoopRow {
                branch: "feat/x".into(),
                head_sha: first[0].head_sha.clone(),
                base_sha: default_sha.clone(),
                ahead: Some(999),
                behind: Some(888),
                last_commit: first[0].last_commit,
                worktree_path: first[0].repo_path.clone(),
            }],
        );

        // fresh=true must IGNORE the poisoned cache and recompute real values.
        let fresh = open_loops_indexed_simple(&repo, Some(&index), true);
        assert_eq!(
            fresh[0].ahead,
            Some(1),
            "fresh must recompute, not serve 999"
        );
        assert_eq!(fresh[0].behind, Some(0));
    }

    /// (e) A brand-new branch after caching → fingerprint changes → it appears.
    #[test]
    fn new_branch_after_caching_appears() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt");

        let index = Index::open_in_memory();
        let first = open_loops_indexed_simple(&repo, Some(&index), false);
        assert_eq!(first.len(), 1);

        // Add a brand-new unmerged branch.
        testutil::add_branch_with_commit(&repo, "feat/y", "y.txt");

        // Fingerprint must have changed (new loose ref under refs/heads).
        let second = open_loops_indexed_simple(&repo, Some(&index), false);
        let mut names: Vec<_> = second.iter().map(|l| l.branch.clone()).collect();
        names.sort();
        assert_eq!(names, vec!["feat/x".to_string(), "feat/y".to_string()]);
    }

    /// A cache HIT whose rows lack ahead/behind must NOT be served to a caller
    /// that needs them — it degrades to a recompute (correctness guard).
    #[test]
    fn hit_with_null_ahead_behind_recomputes_when_needed() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt");

        let index = Index::open_in_memory();
        // Warm the index with a LIGHT-phase scan (no ahead/behind) so the cached
        // rows have NULL ahead/behind under the live fingerprint.
        let light_opts = ScanOptions {
            need_ahead_behind: false,
            ..ScanOptions::default()
        };
        let light = open_loops_indexed(&repo, "root", &light_opts, Some(&index))
            .unwrap()
            .0;
        assert_eq!(light[0].ahead, None);

        // Now ask WITH ahead/behind: the NULL-ahead cache must not be served.
        let full = open_loops_indexed_simple(&repo, Some(&index), false);
        assert_eq!(
            full[0].ahead,
            Some(1),
            "must recompute when cached rows lack the requested ahead/behind"
        );
        assert_eq!(full[0].behind, Some(0));
    }

    /// `scan_indexed(None)` is identical to `scan`: the gate is inert.
    #[test]
    fn scan_indexed_none_matches_scan() {
        let tmp = tempfile::tempdir().unwrap();
        let api = tmp.path().join("api-service");
        testutil::init_repo(&api);
        testutil::add_branch_with_commit(&api, "feat/api", "a.txt");
        let labels = vec![(tmp.path().to_path_buf(), "r".to_string())];
        let opts = ScanOptions {
            need_ahead_behind: true,
            ..ScanOptions::default()
        };
        let (loops, warnings, _) =
            scan_indexed(&[tmp.path().to_path_buf()], &labels, 4, &opts, None, None);
        assert!(warnings.is_empty(), "warnings: {warnings:?}");
        assert_eq!(loops.len(), 1);
        assert_eq!(loops[0].branch, "feat/api");
        assert_eq!(loops[0].ahead, Some(1));
    }

    /// Adding a worktree for an ALREADY-EXISTING branch (so no new loose ref is
    /// written under `refs/`) must still bump the fingerprint, because
    /// `<common_dir>/worktrees/<name>/` is created. This guards against
    /// `git worktree remove` leaving a stale `worktree_path` in the index that
    /// points at a deleted directory.
    ///
    /// We assert on ADD (not remove) because creating a new directory entry
    /// reliably bumps the parent dir mtime even on coarse filesystems, whereas
    /// remove leaves a gap the OS may or may not fill before the next read.
    #[test]
    fn adding_worktree_for_existing_branch_bumps_fingerprint() {
        let tmp = tempfile::tempdir().unwrap();
        let container = tmp.path().join("proj");
        // init_bare_worktree_container gives us: .bare/ (common_dir), a `main`
        // worktree, and one commit — a branch ref already exists.
        testutil::init_bare_worktree_container(&container);

        let common = git_common_dir(&container).unwrap();
        let fp_before = refs_fingerprint(&common);

        // Add a worktree for the existing `main` branch ref reusing `-b` on a
        // fresh name so no new ref is created (we just add a worktrees/<name>/ entry).
        // `add_named_worktree` uses `git worktree add -b <branch> <path>`, which
        // creates a brand-new branch. To avoid creating a new ref we use
        // `git worktree add --detach` on an existing commit instead.
        let wt_path = container.join("extra");
        testutil::git(
            &container,
            &[
                "worktree",
                "add",
                "--detach",
                wt_path.to_str().unwrap(),
                "HEAD",
            ],
        );

        let fp_after = refs_fingerprint(&common);
        assert!(
            fp_after > fp_before,
            "fingerprint must increase after git worktree add (before={fp_before}, after={fp_after})"
        );
    }

    /// End-to-end through `scan_indexed`: a warm scan with an index serves the
    /// poisoned cache (gate hit on the sequential path), proving the heavy phase
    /// was skipped at the scan level too.
    #[test]
    fn scan_indexed_warm_serves_cache() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("app");
        testutil::init_repo(&repo);
        testutil::add_branch_with_commit(&repo, "feat/x", "x.txt");
        let labels = vec![(tmp.path().to_path_buf(), "root".to_string())];
        let opts = ScanOptions {
            need_ahead_behind: true,
            ..ScanOptions::default()
        };
        let index = Index::open_in_memory();

        // Cold scan warms the index (write-through).
        let (cold, _, _) = scan_indexed(
            &[tmp.path().to_path_buf()],
            &labels,
            4,
            &opts,
            None,
            Some(&index),
        );
        assert_eq!(cold.len(), 1);
        assert_eq!(cold[0].ahead, Some(1));

        // Poison the cache under the live gate.
        let common = git_common_dir(&repo).unwrap();
        let hash = crate::inventory::common_dir_hash(&common);
        let refs_fp = refs_fingerprint(&common);
        let (default, default_sha) = default_branch_and_sha(&repo).unwrap();
        index.put_loops(
            &hash,
            &repo,
            &common,
            &default,
            &default_sha,
            refs_fp,
            &[crate::index::LoopRow {
                branch: "feat/x".into(),
                head_sha: cold[0].head_sha.clone(),
                base_sha: default_sha.clone(),
                ahead: Some(999),
                behind: Some(888),
                last_commit: cold[0].last_commit,
                worktree_path: cold[0].repo_path.clone(),
            }],
        );

        // Warm scan: gate hit on the sequential path → sentinel served.
        let (warm, warnings, _) = scan_indexed(
            &[tmp.path().to_path_buf()],
            &labels,
            4,
            &opts,
            None,
            Some(&index),
        );
        assert!(warnings.is_empty(), "warnings: {warnings:?}");
        assert_eq!(warm.len(), 1);
        assert_eq!(
            warm[0].ahead,
            Some(999),
            "scan_indexed warm path must serve cached loops, not re-run git"
        );
    }
}