lore-cli 0.2.0

Capture AI coding sessions and link them to git commits
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
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
//! Sync command - serverless git-ref sync for a repository's lore store.
//!
//! Stores AI reasoning history in the repository's own git repo under
//! `refs/lore/sessions` instead of a hosted service. A store ref points at a
//! commit whose tree holds one encrypted blob per session (the full reasoning
//! record: messages, links, tags, annotations, and summary) plus a plaintext
//! salt and a machine registry. Sharing the repo (plain git) and a passphrase
//! (out of band) is all a teammate needs to read the reasoning.
//!
//! This module implements the per-repo store only:
//!
//! - `lore sync setup` - create or join the store for this repo, set the
//!   passphrase, and write the salt to the ref.
//! - `lore sync` - fetch, merge remote reasoning into the local database, then
//!   build, commit, and push the updated store.
//! - `lore sync status` - report whether the store is set up, the unsynced
//!   count, the last sync time, and local and remote ref state.
//!
//! All git access shells out through [`crate::sync::gitref`], inheriting the
//! user's authentication and remotes.

use std::collections::{BTreeMap, HashSet};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{anyhow, bail, Context, Result};
use chrono::{DateTime, Utc};
use colored::Colorize;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::cli::OutputFormat;
use crate::config::Config;
use crate::storage::models::{Machine, Session, Tombstone};
use crate::storage::Database;
use crate::sync::gitref::{self, TreeEntry};
use crate::sync::keystore::{derive_store_key, generate_store_salt, store_id_from_salt, KeyStore};
use crate::sync::store::{
    decrypt_session_record, decrypt_tombstones, encrypt_session_record, encrypt_tombstones,
    SessionRecord,
};
use crate::sync::SyncError;

/// Full ref name for a repository's per-repo lore store.
///
/// Lives outside `refs/heads/*` so it never checks out into the working tree.
const SESSIONS_REF: &str = "refs/lore/sessions";

/// Remote name used inside the managed global store repo (`~/.lore/sync`).
///
/// The global store is a dedicated repository whose single remote always points
/// at the user's configured `sync_global_remote` URL, so every gitref operation
/// on the global store uses this fixed remote name.
const GLOBAL_REMOTE: &str = "origin";

/// Minimum passphrase length for a newly created store.
const MIN_PASSPHRASE_LEN: usize = 8;

/// Tree path of the encrypted tombstone set inside the store.
///
/// Holds the JSON array of child-record deletions ({child_id, kind, session_id,
/// deleted_at}) so a deletion on one machine suppresses that record everywhere.
const TOMBSTONES_PATH: &str = "meta/tombstones";

/// Maximum number of fetch/merge/build/push attempts before giving up.
///
/// A concurrent local sync (compare-and-swap mismatch) or a remote that moved
/// between our fetch and push (non-fast-forward) triggers a re-fetch and retry.
const MAX_SYNC_ATTEMPTS: usize = 5;

/// Arguments for the sync command.
#[derive(clap::Args)]
#[command(after_help = "EXAMPLES:\n    \
    lore sync setup            Create or join this repo's encrypted lore store\n    \
    lore sync                  Fetch, merge, and push reasoning history\n    \
    lore sync status           Show sync state for this repo\n    \
    lore sync --remote upstream  Sync against a non-default remote")]
pub struct Args {
    #[command(subcommand)]
    pub command: Option<SyncSubcommand>,

    /// Remote to sync the lore store with (default: origin).
    ///
    /// Ignored in the global path (`--global`), which always uses the global
    /// store's own `origin` remote configured from `sync_global_remote`.
    #[arg(long, global = true, default_value = "origin")]
    pub remote: String,

    /// Sync the global personal store instead of this repo's store.
    ///
    /// The global store is a managed git repo at `~/.lore/sync` holding the
    /// user's cross-tool, cross-repo aggregate of encrypted sessions, synced to
    /// a private remote the user owns (configured via `sync_global_remote`).
    #[arg(long, global = true)]
    pub global: bool,

    /// Hook-friendly mode used by the pre-push hook.
    ///
    /// No-ops and exits 0 when this repo's store is not set up or no key is
    /// stored on this machine, never prompts for a passphrase, and keeps output
    /// minimal. Only affects a full sync (no subcommand).
    #[arg(long)]
    pub quiet: bool,
}

/// Sync subcommands. When omitted, a full sync runs.
#[derive(clap::Subcommand)]
pub enum SyncSubcommand {
    /// Set the passphrase for this repo's lore store and write its salt.
    #[command(
        long_about = "Creates a new encrypted store for this repository, or joins an\n\
        existing one already pushed to the remote. When joining, you are\n\
        prompted for the shared passphrase and it is verified against an\n\
        existing session. The derived key is stored locally so later syncs\n\
        do not prompt again."
    )]
    Setup,

    /// Show sync status for this repo's lore store.
    #[command(
        long_about = "Reports whether the store is set up, how many local sessions are\n\
        pending sync, the last sync time, and the local and remote ref state."
    )]
    Status {
        /// Output format: text (default) or json.
        #[arg(short, long, value_enum, default_value = "text")]
        format: OutputFormat,
    },
}

/// A machine's identity for the store's machine registry.
struct MachineIdentity {
    /// Stable machine UUID.
    id: String,
    /// Human-readable machine name.
    name: String,
}

/// Plaintext per-session metadata blob (`sessions/<uuid>.meta.json`).
///
/// Written unencrypted so the store can be listed without the passphrase.
#[derive(Serialize, Deserialize)]
struct SessionMeta {
    /// Session UUID.
    id: Uuid,
    /// Tool that produced the session (for example `claude-code`).
    tool: String,
    /// When the session started.
    started_at: DateTime<Utc>,
    /// When the session ended, if it has.
    ended_at: Option<DateTime<Utc>>,
    /// Number of messages in the session.
    message_count: i32,
    /// Machine that captured the session.
    machine_id: Option<String>,
    /// Git branch active when the session was captured.
    git_branch: Option<String>,
}

/// Summary of a completed sync for reporting.
#[derive(Debug)]
struct SyncSummary {
    /// Sessions merged from the remote into the local database.
    pulled: usize,
    /// Local unsynced sessions written into the store and pushed.
    pushed: usize,
}

/// JSON output for `lore sync status`.
#[derive(Serialize)]
struct StatusOutput {
    set_up: bool,
    keyed: bool,
    unsynced_sessions: i32,
    last_sync_at: Option<String>,
    remote_store_exists: bool,
    local_ref: Option<String>,
    tracking_ref: Option<String>,
    remote: String,
}

/// JSON output for `lore sync --global status`.
#[derive(Serialize)]
struct GlobalStatusOutput {
    set_up: bool,
    keyed: bool,
    unsynced_sessions: i32,
    last_sync_at: Option<String>,
    remote_store_exists: bool,
    local_ref: Option<String>,
    tracking_ref: Option<String>,
    remote: Option<String>,
    store_path: String,
}

/// Identifies which lore store a sync operates on.
///
/// Both stores share the identical on-disk format and the same `refs/lore/sessions`
/// ref name; they differ only in which git repository holds the ref, which
/// sessions are pushed, and which sync-tracking column marks progress. Threading
/// this enum through [`perform_sync_in_store`] lets the per-repo and global paths
/// share all fetch, merge, orphan, carry-forward, CAS, and push machinery.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SyncStore {
    /// This repository's store under `refs/lore/sessions` in the repo itself.
    PerRepo,
    /// The global personal store under `refs/lore/sessions` in `~/.lore/sync`.
    Global,
}

/// Executes the sync command.
pub fn run(args: Args) -> Result<()> {
    if args.global {
        return match args.command {
            Some(SyncSubcommand::Setup) => run_global_setup(),
            Some(SyncSubcommand::Status { format }) => run_global_status(format),
            None => run_global_sync(),
        };
    }

    match args.command {
        Some(SyncSubcommand::Setup) => run_setup(&args.remote),
        Some(SyncSubcommand::Status { format }) => run_status(&args.remote, format),
        None if args.quiet => run_sync_quiet(&args.remote),
        None => run_sync(&args.remote),
    }
}

// ==================== setup ====================

/// Creates or joins the lore store for the current repository.
fn run_setup(remote: &str) -> Result<()> {
    let repo = current_repo()?;
    let mut config = Config::load()?;
    create_or_join_store(&repo, remote, &mut config)?;
    println!("Run 'lore sync' to push your reasoning history.");
    Ok(())
}

/// Creates a new store or joins an existing one at `repo`/`remote`.
///
/// Shared by the per-repo ([`run_setup`]) and global ([`run_global_setup`])
/// setup paths: it fetches the remote-tracking ref to detect an existing store,
/// then either joins it (single passphrase prompt, verified by decrypting an
/// existing session) or creates a new one (confirmed passphrase, minimum
/// length). The derived key is stored locally so later syncs do not prompt.
fn create_or_join_store(repo: &Path, remote: &str, config: &mut Config) -> Result<()> {
    let machine = machine_identity(config)?;
    let keystore = KeyStore::with_keychain(config.use_keychain);

    // Populate the remote-tracking ref so we can tell whether a store already
    // exists on the remote (and read its salt) without touching the local ref.
    gitref::fetch(repo, remote, SESSIONS_REF)
        .with_context(|| format!("Failed to reach remote '{remote}'"))?;

    match read_store_salt(repo, remote)? {
        Some(salt) => {
            println!("{}", "An existing lore store was found. Joining it.".bold());
            println!("Enter the shared passphrase for this lore store.");
            let passphrase = prompt_passphrase()?;
            join_store(repo, remote, &keystore, &machine, &salt, &passphrase)?;
            println!("{} Joined the lore store.", "Success!".green().bold());
        }
        None => {
            println!("{}", "Setting up a new lore store.".bold());
            println!(
                "Your reasoning history is encrypted with a passphrase only you\n\
                 (and any teammates) know. It is never sent to the git host."
            );
            println!();
            let passphrase = prompt_new_passphrase()?;
            create_store(repo, remote, &keystore, &machine, &passphrase)?;
            println!("{} Created the lore store.", "Success!".green().bold());
        }
    }

    Ok(())
}

/// Creates a brand new store: fresh salt, derived key, salt written to the ref.
fn create_store(
    repo: &Path,
    remote: &str,
    keystore: &KeyStore,
    machine: &MachineIdentity,
    passphrase: &str,
) -> Result<()> {
    let salt = generate_store_salt();
    let key = derive_store_key(passphrase, &salt)?;
    establish_store(repo, remote, keystore, machine, &salt, &key)
}

/// Joins an existing store: derive the key from the shared salt and verify it.
fn join_store(
    repo: &Path,
    remote: &str,
    keystore: &KeyStore,
    machine: &MachineIdentity,
    salt: &[u8],
    passphrase: &str,
) -> Result<()> {
    let key = derive_store_key(passphrase, salt)?;

    // Verify the passphrase by decrypting one existing session, if any exist.
    if let Some(blob) = first_session_blob(repo, remote)? {
        decrypt_session_record(&blob, &key).map_err(|_| {
            anyhow!("Wrong passphrase: could not decrypt an existing session in the store")
        })?;
    }

    establish_store(repo, remote, keystore, machine, salt, &key)
}

/// Stores the derived key and writes the store metadata to the local ref.
///
/// The new tree starts from the remote-tracking ref when present (so a join
/// adopts the remote store's sessions and salt), otherwise from the existing
/// local ref, otherwise from scratch. Only `meta/salt` and `meta/machines.json`
/// are written here; session blobs are added by [`run_sync`].
fn establish_store(
    repo: &Path,
    remote: &str,
    keystore: &KeyStore,
    machine: &MachineIdentity,
    salt: &[u8],
    key: &[u8],
) -> Result<()> {
    let store_id = store_id_from_salt(salt);
    keystore.store_key(&store_id, key)?;

    let (base, parent) = store_base(repo, remote)?;

    let mut machines = match &base {
        Some(reference) => read_machines(repo, reference)?,
        None => BTreeMap::new(),
    };
    machines.insert(machine.id.clone(), machine.name.clone());

    let mut changes = BTreeMap::new();
    changes.insert("meta/salt".to_string(), gitref::write_blob(repo, salt)?);
    changes.insert(
        "meta/machines.json".to_string(),
        gitref::write_blob(repo, &serde_json::to_vec(&machines)?)?,
    );

    let tree = gitref::build_tree(repo, base.as_deref(), &changes)?;
    let commit = gitref::commit_tree(repo, &tree, parent.as_deref(), "lore: set up store")?;
    gitref::update_ref(repo, SESSIONS_REF, &commit)?;
    gitref::push(repo, remote, SESSIONS_REF)
        .with_context(|| format!("Failed to push the lore store to '{remote}'"))?;

    // Configure a tracking-namespace fetch refspec so a plain `git pull` keeps
    // the remote-tracking lore refs fresh for this remote. The real merge into
    // the local ref still requires `lore sync`; this only pre-populates the
    // tracking refs. Best-effort: a failure here must not fail setup.
    if let Err(e) = gitref::add_lore_fetch_refspec(repo, remote) {
        tracing::debug!("Could not add lore fetch refspec for '{remote}': {e}");
    }

    Ok(())
}

// ==================== sync ====================

/// Performs a full sync of the current repository's lore store.
fn run_sync(remote: &str) -> Result<()> {
    let repo = current_repo()?;
    let mut config = Config::load()?;
    let machine = machine_identity(&mut config)?;
    let keystore = KeyStore::with_keychain(config.use_keychain);

    let (key, salt) = load_store_credentials(&repo, remote, &keystore)?;

    let mut db = Database::open_default()?;
    // Push only this repo's own sessions so cross-project history is never
    // written into (and shared through) this repo's store.
    let sessions = db.get_unsynced_sessions_for_repo(&repo)?;
    let summary = perform_sync(&mut db, &repo, remote, &key, &salt, &machine, sessions)?;

    println!(
        "{} Pulled {}, pushed {}.",
        "Sync complete.".green().bold(),
        summary.pulled,
        summary.pushed
    );
    Ok(())
}

/// Performs a hook-friendly sync used by the pre-push hook.
///
/// Skips silently (returns Ok without touching the config or database) when the
/// repo's store is not set up or no key is stored on this machine, never
/// prompts, and keeps output minimal. Sync errors are propagated so the caller
/// (the pre-push hook) can surface a brief warning; they never block the push
/// because the hook always exits 0.
fn run_sync_quiet(remote: &str) -> Result<()> {
    let repo = current_repo()?;
    sync_quiet_in_repo(&repo, remote)
}

/// Core of the quiet sync for a resolved repository path.
///
/// Split from [`run_sync_quiet`] so it can be exercised in tests with a
/// temporary repo rather than the process working directory.
fn sync_quiet_in_repo(repo: &Path, remote: &str) -> Result<()> {
    // git hands the pre-push hook the pushed remote's name, but it may be a raw
    // URL or an unknown name; resolve it to a configured remote or skip.
    let remote = match resolve_hook_remote(repo, remote)? {
        Some(remote) => remote,
        None => return Ok(()),
    };

    // Skip silently when the store is not set up. This check reads only local
    // refs, so a repo that never opted into lore sync is a no-op that never
    // touches the config or database.
    let salt = match read_store_salt(repo, &remote)? {
        Some(salt) => salt,
        None => return Ok(()),
    };

    // Resolve the key store read-only. `Config::load` never writes, so this
    // reads `use_keychain` without persisting anything; the mutating machine-id
    // step is deferred to after a key is confirmed present.
    let keystore = KeyStore::with_keychain(Config::load()?.use_keychain);

    quiet_sync_with_keystore(repo, &remote, &salt, &keystore)
}

/// Runs a quiet sync for a set-up store, given an already-resolved key store.
///
/// Returns Ok without mutating the config or opening the database when no key is
/// stored on this machine. Only after a key is confirmed present does it persist
/// this machine's id (the first mutating step), open the database, and sync.
/// Split from [`sync_quiet_in_repo`] so it can be exercised with a key store
/// isolated to a temp directory.
fn quiet_sync_with_keystore(
    repo: &Path,
    remote: &str,
    salt: &[u8],
    keystore: &KeyStore,
) -> Result<()> {
    // Skip silently when no key is stored on this machine. Quiet mode never
    // prompts for a passphrase; the user runs 'lore sync setup' interactively.
    // This precedes every mutating step, so a set-up-but-unkeyed repo never
    // writes the config or opens the database.
    let store_id = store_id_from_salt(salt);
    let key = match keystore.load_key(&store_id)? {
        Some(key) => key,
        None => return Ok(()),
    };

    // A key is present: now perform the mutating work. `machine_identity`
    // persists a generated machine id to the config, so it must not run on the
    // no-key path above.
    let mut config = Config::load()?;
    let machine = machine_identity(&mut config)?;
    let mut db = Database::open_default()?;
    let sessions = db.get_unsynced_sessions_for_repo(repo)?;
    perform_sync(&mut db, repo, remote, &key, salt, &machine, sessions)?;
    Ok(())
}

/// Resolves the remote to sync against for a hook invocation.
///
/// git passes the pushed remote's name (or a raw URL for an anonymous push) to
/// the pre-push hook. When it names a configured remote, use it. Otherwise fall
/// back to the default remote (`origin`) when configured, else return `None` so
/// the caller skips gracefully rather than erroring on an unknown remote.
fn resolve_hook_remote(repo: &Path, remote: &str) -> Result<Option<String>> {
    let remotes = configured_remotes(repo)?;
    if remotes.iter().any(|r| r == remote) {
        return Ok(Some(remote.to_string()));
    }
    if remotes.iter().any(|r| r == "origin") {
        return Ok(Some("origin".to_string()));
    }
    Ok(None)
}

/// Lists the names of remotes configured in the repository.
fn configured_remotes(repo: &Path) -> Result<Vec<String>> {
    let output = Command::new("git")
        .current_dir(repo)
        .args(["remote"])
        .output()
        .context("Failed to run git remote")?;
    if !output.status.success() {
        return Ok(Vec::new());
    }
    Ok(String::from_utf8_lossy(&output.stdout)
        .lines()
        .map(|line| line.trim().to_string())
        .filter(|line| !line.is_empty())
        .collect())
}

/// Fetches, merges remote reasoning into the database, then builds and pushes.
///
/// The caller supplies `sessions`, the exact set of local sessions to push into
/// this store. For a per-repo store that is the repo-scoped unsynced set (see
/// [`Database::get_unsynced_sessions_for_repo`]) so cross-project history never
/// leaks into one repo's store; a future global store can pass all unsynced
/// sessions without any change here. Inbound merge is independent of this set:
/// every remote session is pulled regardless of its working directory.
///
/// Retries the whole cycle on a compare-and-swap mismatch (a concurrent local
/// sync moved the ref) or a non-fast-forward push (the remote moved between our
/// fetch and push), up to [`MAX_SYNC_ATTEMPTS`].
fn perform_sync(
    db: &mut Database,
    repo: &Path,
    remote: &str,
    key: &[u8],
    salt: &[u8],
    machine: &MachineIdentity,
    sessions: Vec<Session>,
) -> Result<SyncSummary> {
    perform_sync_in_store(
        SyncStore::PerRepo,
        db,
        repo,
        remote,
        key,
        salt,
        machine,
        sessions,
    )
}

/// Store-parameterized core of [`perform_sync`].
///
/// `store` selects the three store-specific behaviors while every other step is
/// shared verbatim between the per-repo and global paths:
///
/// - Carry-forward scope: [`SyncStore::PerRepo`] uses this repo's session ids
///   ([`Database::get_session_ids_for_repo`]); [`SyncStore::Global`] uses every
///   session id ([`Database::get_all_session_ids`]).
/// - Merge import marking: per-repo marks the `synced_at` track; global marks the
///   `global_synced_at` track (see [`merge_remote_in_store`]).
/// - Post-push marking: per-repo calls [`Database::mark_sessions_synced`]; global
///   calls [`Database::mark_global_synced`].
///
/// The caller supplies `sessions`, the exact set to push (per-repo passes the
/// repo-scoped unsynced set; global passes all unsynced-global sessions).
#[allow(clippy::too_many_arguments)]
fn perform_sync_in_store(
    store: SyncStore,
    db: &mut Database,
    repo: &Path,
    remote: &str,
    key: &[u8],
    salt: &[u8],
    machine: &MachineIdentity,
    sessions: Vec<Session>,
) -> Result<SyncSummary> {
    let mut pulled_total = 0;

    for attempt in 0..MAX_SYNC_ATTEMPTS {
        let is_last = attempt + 1 == MAX_SYNC_ATTEMPTS;
        let old_local = gitref::resolve_ref(repo, SESSIONS_REF)?;

        // FETCH: bring the remote store into the tracking ref (None when the
        // remote store has never been initialized).
        let fetched = gitref::fetch(repo, remote, SESSIONS_REF)
            .with_context(|| format!("Failed to fetch the lore store from '{remote}'"))?;
        let tracking_entries = if fetched.is_some() {
            gitref::read_tracking_tree(repo, remote, SESSIONS_REF)?
        } else {
            Vec::new()
        };

        // TOMBSTONES: union the remote deletion set into the local table BEFORE
        // the merge so the merge suppresses re-adding any child that was deleted
        // on another machine. Tombstones are never pruned during sync: they are
        // tiny (a few ids plus a timestamp) and dropping one that is still needed
        // for suppression would resurrect the deleted child, so the full set is
        // kept indefinitely. A wrong key makes the remote tombstones
        // undecryptable; that is treated as an empty set here (the merge below
        // surfaces the wrong-key error).
        let remote_tombstones = read_remote_tombstones(repo, &tracking_entries, key)?;
        db.add_tombstones(&remote_tombstones)?;

        // MERGE remote -> local database (full records, newer-wins). A wrong key
        // surfaces here and aborts before anything is built or pushed. The store
        // selects which sync-tracking column an imported session is marked on.
        pulled_total += merge_remote_in_store(store, db, repo, &tracking_entries, key)?;
        merge_machines(db, repo, &tracking_entries)?;

        // APPLY tombstones: remove any child that is present locally but has been
        // deleted (on this or another machine). Suppression above stops a stale
        // remote blob from re-adding it; this removes one already stored here.
        let tombstones = db.list_tombstones()?;
        db.apply_tombstones(&tombstones)?;

        // BUILD the outgoing tree. Separate the TREE BASE (which entries the new
        // tree inherits) from the COMMIT PARENT (which commit it descends from):
        //
        // - Remote present: base the tree on the remote tracking commit so the
        //   push is a fast-forward and remote-only sessions are preserved. The
        //   commit descends from that same tracking commit.
        // - No remote store: base the tree on NOTHING (empty) and make the commit
        //   an ORPHAN (no parent). The local ref may have been written before
        //   per-repo scoping and can hold out-of-scope session artifacts; both
        //   inheriting its tree and descending from its commit would leak that
        //   history. Descending from the local ref keeps old_local reachable from
        //   the pushed ref (e.g. `refs/lore/sessions^`), so out-of-scope blobs
        //   still reach the remote through ancestry even when the tip tree is
        //   clean. Orphaning drops that ancestry so only the scoped tip tree and
        //   its blobs are reachable. Only in-scope local-only sessions are carried
        //   forward below, so no in-scope data is lost by orphaning.
        let tracking_commit = match fetched {
            Some(_) => {
                gitref::resolve_ref(repo, &gitref::tracking_ref_name(remote, SESSIONS_REF)?)?
            }
            None => None,
        };
        let tree_base = tracking_commit.clone();
        let commit_parent = tracking_commit.clone();

        let mut changes = build_session_changes(db, repo, key, &sessions)?;

        // Carry forward already-stored, in-scope session artifacts that live
        // only in the local ref so no stored in-scope session is silently
        // dropped from the outgoing tree:
        //
        // - Remote present: a remote rewind can leave sessions in the local ref
        //   that are absent from the fetched remote tree.
        // - No remote store: the tree base is empty, so every in-scope local-only
        //   session must be re-added from the local ref.
        //
        // Passing an empty tracking set in the no-remote case makes every local
        // session a candidate; `carry_forward_local_sessions` still re-adds only
        // in-scope ids, so out-of-scope artifacts are never carried forward.
        if let Some(local_commit) = &old_local {
            // Carry-forward scope: the per-repo store is limited to this repo's
            // sessions, while the global store spans every session.
            let in_scope = match store {
                SyncStore::PerRepo => db.get_session_ids_for_repo(repo)?,
                SyncStore::Global => db.get_all_session_ids()?,
            };
            let carry_tracking: &[TreeEntry] = if tracking_commit.is_some() {
                &tracking_entries
            } else {
                &[]
            };
            carry_forward_local_sessions(
                repo,
                local_commit,
                carry_tracking,
                &in_scope,
                &mut changes,
            )?;
        }

        add_meta_changes(db, repo, tree_base.as_deref(), salt, machine, &mut changes)?;

        // Write the unioned tombstone set back to the store, but only when it
        // differs from what the base tree already holds so an unchanged set
        // keeps its existing content-addressed blob (no churn from re-encrypting
        // with a fresh nonce every sync).
        add_tombstone_changes(db, repo, &remote_tombstones, key, &mut changes)?;

        let tree = gitref::build_tree(repo, tree_base.as_deref(), &changes)?;
        let message = format!("lore: sync {} session(s)", sessions.len());
        let commit = gitref::commit_tree(repo, &tree, commit_parent.as_deref(), &message)?;

        // CAS-update the local ref, guarding against a concurrent local sync.
        match gitref::update_ref_checked(repo, SESSIONS_REF, &commit, old_local.as_deref()) {
            Ok(()) => {}
            Err(SyncError::RefCasMismatch(_)) if !is_last => continue,
            Err(e) => return Err(e.into()),
        }

        // PUSH to the remote. A non-fast-forward means the remote moved since we
        // fetched; re-fetch and retry.
        match gitref::push(repo, remote, SESSIONS_REF) {
            Ok(()) => {}
            Err(e) if !is_last && is_non_fast_forward(&e) => continue,
            Err(e) => {
                return Err(anyhow::Error::from(e))
                    .with_context(|| format!("Failed to push the lore store to '{remote}'"));
            }
        }

        let ids: Vec<Uuid> = sessions.iter().map(|s| s.id).collect();
        match store {
            SyncStore::PerRepo => db.mark_sessions_synced(&ids, Utc::now())?,
            SyncStore::Global => db.mark_global_synced(&ids, Utc::now())?,
        };

        return Ok(SyncSummary {
            pulled: pulled_total,
            pushed: sessions.len(),
        });
    }

    bail!("Sync did not converge after {MAX_SYNC_ATTEMPTS} attempts due to concurrent updates")
}

/// Merges every encrypted session in `entries` into the database (per-repo).
///
/// Thin per-repo wrapper over [`merge_remote_in_store`], retained for tests.
#[cfg(test)]
fn merge_remote(
    db: &mut Database,
    repo: &Path,
    entries: &[TreeEntry],
    key: &[u8],
) -> Result<usize> {
    merge_remote_in_store(SyncStore::PerRepo, db, repo, entries, key)
}

/// Merges every encrypted session in `entries` into the database.
///
/// Each record is applied atomically by the merge writer: the session row and
/// messages follow newer-wins (by message_count then ended_at) while links,
/// tags, and annotations are always merged (additive, idempotent by id) so a
/// remote addition to an already-synced session is not lost, and the summary is
/// kept only when strictly newer. Returns the number of sessions whose row was
/// imported or updated (the newer-wins branch ran).
///
/// `store` selects which sync-tracking column an imported session is marked on:
/// [`SyncStore::PerRepo`] marks `synced_at` (via [`Database::merge_remote_record`])
/// and [`SyncStore::Global`] marks `global_synced_at` (via
/// [`Database::merge_remote_record_global`]). Marking only the merging store's
/// column keeps the two sync tracks independent.
///
/// A blob that cannot be decrypted is normally skipped (corruption or a single
/// stray entry). But if the store held session blobs and NONE of them decrypted,
/// the stored key is wrong for this store, so this returns an error rather than
/// letting the caller push locally re-encrypted sessions under the wrong key and
/// mark them synced (which would poison the store).
fn merge_remote_in_store(
    store: SyncStore,
    db: &mut Database,
    repo: &Path,
    entries: &[TreeEntry],
    key: &[u8],
) -> Result<usize> {
    let mut pulled = 0;
    let mut session_blobs = 0;
    let mut decrypted = 0;

    for entry in entries {
        if !is_session_blob(&entry.path) {
            continue;
        }
        session_blobs += 1;

        let blob = gitref::read_blob(repo, &entry.sha)?;
        let record = match decrypt_session_record(&blob, key) {
            Ok(record) => record,
            Err(e) => {
                // A blob we cannot decrypt (corrupt or a key mismatch on a
                // single entry) must not abort the whole merge on its own; the
                // wrong-key check below covers the all-failed case.
                tracing::debug!("Skipping undecryptable session blob {}: {e}", entry.path);
                continue;
            }
        };
        decrypted += 1;

        let now = Utc::now();
        let imported = match store {
            SyncStore::PerRepo => db.merge_remote_record(
                &record.session,
                &record.messages,
                &record.links,
                &record.tags,
                &record.annotations,
                record.summary.as_ref(),
                now,
            )?,
            SyncStore::Global => db.merge_remote_record_global(
                &record.session,
                &record.messages,
                &record.links,
                &record.tags,
                &record.annotations,
                record.summary.as_ref(),
                now,
            )?,
        };
        if imported {
            pulled += 1;
        }
    }

    // If the store had sessions but not a single one decrypted, the key is wrong.
    // Bail before the caller builds, pushes, and marks anything synced.
    if session_blobs > 0 && decrypted == 0 {
        bail!(
            "Could not decrypt any of the {session_blobs} session(s) in the remote lore store. \
             The sync key stored on this machine does not match this store's passphrase. \
             Run 'lore sync setup' to re-enter the correct passphrase."
        );
    }

    Ok(pulled)
}

/// Merges the remote machine registry into the local database.
fn merge_machines(db: &Database, repo: &Path, entries: &[TreeEntry]) -> Result<()> {
    if let Some(bytes) = blob_at_path(repo, entries, "meta/machines.json")? {
        let machines: BTreeMap<String, String> = serde_json::from_slice(&bytes).unwrap_or_default();
        let now = Utc::now().to_rfc3339();
        for (id, name) in machines {
            db.upsert_machine(&Machine {
                id,
                name,
                created_at: now.clone(),
            })?;
        }
    }
    Ok(())
}

/// Builds the tree changes for the unsynced sessions.
///
/// Each session is assembled into its full record, encrypted, and written as
/// `sessions/<uuid>.enc` alongside a plaintext `sessions/<uuid>.meta.json`.
/// Already-synced sessions are untouched, preserving their existing git objects.
fn build_session_changes(
    db: &Database,
    repo: &Path,
    key: &[u8],
    sessions: &[Session],
) -> Result<BTreeMap<String, String>> {
    let mut changes = BTreeMap::new();

    for session in sessions {
        let record = assemble_record(db, session)?;
        let blob = encrypt_session_record(&record, key)?;
        let enc_sha = gitref::write_blob(repo, &blob)?;

        let meta = SessionMeta {
            id: session.id,
            tool: session.tool.clone(),
            started_at: session.started_at,
            ended_at: session.ended_at,
            message_count: session.message_count,
            machine_id: session.machine_id.clone(),
            git_branch: session.git_branch.clone(),
        };
        let meta_sha = gitref::write_blob(repo, &serde_json::to_vec(&meta)?)?;

        changes.insert(format!("sessions/{}.enc", session.id), enc_sha);
        changes.insert(format!("sessions/{}.meta.json", session.id), meta_sha);
    }

    Ok(changes)
}

/// Adds the `meta/salt` and `meta/machines.json` entries to the tree changes.
fn add_meta_changes(
    db: &Database,
    repo: &Path,
    base: Option<&str>,
    salt: &[u8],
    machine: &MachineIdentity,
    changes: &mut BTreeMap<String, String>,
) -> Result<()> {
    changes.insert("meta/salt".to_string(), gitref::write_blob(repo, salt)?);

    let mut machines = match base {
        Some(reference) => read_machines(repo, reference)?,
        None => BTreeMap::new(),
    };
    for m in db.list_machines()? {
        machines.insert(m.id, m.name);
    }
    machines.insert(machine.id.clone(), machine.name.clone());

    changes.insert(
        "meta/machines.json".to_string(),
        gitref::write_blob(repo, &serde_json::to_vec(&machines)?)?,
    );
    Ok(())
}

/// Reads and decrypts the remote store's tombstone set.
///
/// Returns an empty set when the store has no `meta/tombstones` blob or the blob
/// cannot be decrypted (a wrong key, which the session merge reports separately;
/// treating it as empty here avoids a confusing early error).
fn read_remote_tombstones(
    repo: &Path,
    entries: &[TreeEntry],
    key: &[u8],
) -> Result<Vec<Tombstone>> {
    match blob_at_path(repo, entries, TOMBSTONES_PATH)? {
        Some(bytes) => match decrypt_tombstones(&bytes, key) {
            Ok(tombstones) => Ok(tombstones),
            Err(e) => {
                tracing::debug!("Could not decrypt remote tombstones: {e}");
                Ok(Vec::new())
            }
        },
        None => Ok(Vec::new()),
    }
}

/// Adds the encrypted tombstone blob to the outgoing tree when it changed.
///
/// Compares the local tombstone set with what the base tree already holds
/// (`remote_tombstones`) by their `(child_id, kind)` keys. When the keys match,
/// nothing is written so the base's existing blob is preserved verbatim
/// (content-addressed dedup). Otherwise the full local set is re-encrypted and
/// written.
fn add_tombstone_changes(
    db: &Database,
    repo: &Path,
    remote_tombstones: &[Tombstone],
    key: &[u8],
    changes: &mut BTreeMap<String, String>,
) -> Result<()> {
    let local = db.list_tombstones()?;
    if tombstone_keys_equal(&local, remote_tombstones) {
        return Ok(());
    }
    let blob = encrypt_tombstones(&local, key)?;
    let sha = gitref::write_blob(repo, &blob)?;
    changes.insert(TOMBSTONES_PATH.to_string(), sha);
    Ok(())
}

/// Returns whether two tombstone sets hold the same `(child_id, kind)` keys.
///
/// Only the identifying keys are compared (not `deleted_at` or `session_id`) so
/// two machines recording the same deletion at different times do not trigger a
/// perpetual rewrite of the store blob.
fn tombstone_keys_equal(a: &[Tombstone], b: &[Tombstone]) -> bool {
    let keys_a: HashSet<(&str, &str)> = a
        .iter()
        .map(|t| (t.child_id.as_str(), t.kind.as_str()))
        .collect();
    let keys_b: HashSet<(&str, &str)> = b
        .iter()
        .map(|t| (t.child_id.as_str(), t.kind.as_str()))
        .collect();
    keys_a == keys_b
}

/// Assembles the complete reasoning record for a session from the database.
fn assemble_record(db: &Database, session: &Session) -> Result<SessionRecord> {
    Ok(SessionRecord {
        session: session.clone(),
        messages: db.get_messages(&session.id)?,
        links: db.get_links_by_session(&session.id)?,
        tags: db.get_tags(&session.id)?,
        annotations: db.get_annotations(&session.id)?,
        summary: db.get_summary(&session.id)?,
    })
}

// ==================== status ====================

/// Shows sync status for the current repository's lore store.
fn run_status(remote: &str, format: OutputFormat) -> Result<()> {
    let repo = current_repo()?;
    let config = Config::load()?;
    let keystore = KeyStore::with_keychain(config.use_keychain);
    let db = Database::open_default()?;

    let salt = read_store_salt(&repo, remote)?;
    let keyed = match &salt {
        Some(salt) => keystore.load_key(&store_id_from_salt(salt))?.is_some(),
        None => false,
    };
    let set_up = salt.is_some();

    // Scope the pending count to this repo so it reflects what a sync will push.
    let unsynced = db.unsynced_session_count_for_repo(&repo)?;
    let last_sync = db.last_sync_time()?;
    let remote_exists = gitref::remote_ref_exists(&repo, remote, SESSIONS_REF).unwrap_or(false);
    let local_ref = gitref::resolve_ref(&repo, SESSIONS_REF)?;
    let tracking_ref =
        gitref::resolve_ref(&repo, &gitref::tracking_ref_name(remote, SESSIONS_REF)?)?;

    match format {
        OutputFormat::Json => {
            let output = StatusOutput {
                set_up,
                keyed,
                unsynced_sessions: unsynced,
                last_sync_at: last_sync.map(|t| t.to_rfc3339()),
                remote_store_exists: remote_exists,
                local_ref: local_ref.clone(),
                tracking_ref: tracking_ref.clone(),
                remote: remote.to_string(),
            };
            println!("{}", serde_json::to_string_pretty(&output)?);
        }
        OutputFormat::Text | OutputFormat::Markdown => {
            println!("{}", "Lore Sync".bold());
            println!();
            if set_up && keyed {
                println!("  Store:          {}", "set up".green());
            } else if set_up {
                println!(
                    "  Store:          {} (run 'lore sync setup')",
                    "no key on this machine".yellow()
                );
            } else {
                println!(
                    "  Store:          {} (run 'lore sync setup')",
                    "not set up".yellow()
                );
            }
            println!("  Remote:         {remote}");
            println!(
                "  Remote store:   {}",
                if remote_exists { "present" } else { "none" }
            );
            println!("  Pending sync:   {unsynced}");
            match last_sync {
                Some(t) => println!("  Last sync:      {}", t.to_rfc3339()),
                None => println!("  Last sync:      {}", "never".dimmed()),
            }
            println!(
                "  Local ref:      {}",
                local_ref.as_deref().unwrap_or("none")
            );
            println!(
                "  Tracking ref:   {}",
                tracking_ref.as_deref().unwrap_or("none")
            );
        }
    }

    Ok(())
}

// ==================== global store ====================

/// Sets up the global personal store.
///
/// Prompts for and stores the remote URL when unset, initializes and configures
/// the managed repo at `~/.lore/sync`, then creates or joins the store there
/// using the same machinery as the per-repo path (against the store's `origin`
/// remote).
fn run_global_setup() -> Result<()> {
    let mut config = Config::load()?;

    let remote_url = match config.sync_global_remote.clone() {
        Some(url) => url,
        None => {
            let url = prompt_global_remote_url()?;
            config.sync_global_remote = Some(url.clone());
            config.save()?;
            url
        }
    };

    let repo = global_store_path()?;
    ensure_global_repo(&repo, &remote_url)?;
    create_or_join_store(&repo, GLOBAL_REMOTE, &mut config)?;

    println!("Run 'lore sync --global' to push your reasoning history.");
    Ok(())
}

/// Performs a full sync of the global personal store, pushing all unsynced-global
/// sessions regardless of their working directory.
fn run_global_sync() -> Result<()> {
    let mut config = Config::load()?;
    let remote_url = config.sync_global_remote.clone().ok_or_else(|| {
        anyhow!("The global store is not set up. Run 'lore sync --global setup' first.")
    })?;
    let machine = machine_identity(&mut config)?;
    let keystore = KeyStore::with_keychain(config.use_keychain);

    let repo = global_store_path()?;
    ensure_global_repo(&repo, &remote_url)?;
    let (key, salt) = load_store_credentials(&repo, GLOBAL_REMOTE, &keystore)?;

    let mut db = Database::open_default()?;
    // The global store aggregates every session, so push all unsynced-global
    // sessions rather than scoping to a repository.
    let sessions = db.get_unsynced_global_sessions()?;
    let summary = perform_sync_in_store(
        SyncStore::Global,
        &mut db,
        &repo,
        GLOBAL_REMOTE,
        &key,
        &salt,
        &machine,
        sessions,
    )?;

    println!(
        "{} Pulled {}, pushed {}.",
        "Global sync complete.".green().bold(),
        summary.pulled,
        summary.pushed
    );
    Ok(())
}

/// Shows sync status for the global personal store.
fn run_global_status(format: OutputFormat) -> Result<()> {
    let config = Config::load()?;
    let keystore = KeyStore::with_keychain(config.use_keychain);
    let db = Database::open_default()?;

    let remote_url = config.sync_global_remote.clone();
    let repo = global_store_path()?;
    // The managed repo may not exist yet if setup was never run; guard every
    // git access so status never errors on an unconfigured global store.
    let repo_ready = repo.join(".git").exists();

    let salt = if repo_ready {
        read_store_salt(&repo, GLOBAL_REMOTE)?
    } else {
        None
    };
    let keyed = match &salt {
        Some(salt) => keystore.load_key(&store_id_from_salt(salt))?.is_some(),
        None => false,
    };
    let set_up = salt.is_some();

    let unsynced = db.unsynced_global_count()?;
    let last_sync = db.last_global_sync_time()?;
    let (remote_exists, local_ref, tracking_ref) = if repo_ready {
        (
            gitref::remote_ref_exists(&repo, GLOBAL_REMOTE, SESSIONS_REF).unwrap_or(false),
            gitref::resolve_ref(&repo, SESSIONS_REF)?,
            gitref::resolve_ref(
                &repo,
                &gitref::tracking_ref_name(GLOBAL_REMOTE, SESSIONS_REF)?,
            )?,
        )
    } else {
        (false, None, None)
    };

    match format {
        OutputFormat::Json => {
            let output = GlobalStatusOutput {
                set_up,
                keyed,
                unsynced_sessions: unsynced,
                last_sync_at: last_sync.map(|t| t.to_rfc3339()),
                remote_store_exists: remote_exists,
                local_ref: local_ref.clone(),
                tracking_ref: tracking_ref.clone(),
                remote: remote_url.clone(),
                store_path: repo.display().to_string(),
            };
            println!("{}", serde_json::to_string_pretty(&output)?);
        }
        OutputFormat::Text | OutputFormat::Markdown => {
            println!("{}", "Lore Global Sync".bold());
            println!();
            if set_up && keyed {
                println!("  Store:          {}", "set up".green());
            } else if set_up {
                println!(
                    "  Store:          {} (run 'lore sync --global setup')",
                    "no key on this machine".yellow()
                );
            } else {
                println!(
                    "  Store:          {} (run 'lore sync --global setup')",
                    "not set up".yellow()
                );
            }
            match &remote_url {
                Some(url) => println!("  Remote:         {url}"),
                None => println!("  Remote:         {}", "not configured".yellow()),
            }
            println!("  Store path:     {}", repo.display());
            println!(
                "  Remote store:   {}",
                if remote_exists { "present" } else { "none" }
            );
            println!("  Pending sync:   {unsynced}");
            match last_sync {
                Some(t) => println!("  Last sync:      {}", t.to_rfc3339()),
                None => println!("  Last sync:      {}", "never".dimmed()),
            }
            println!(
                "  Local ref:      {}",
                local_ref.as_deref().unwrap_or("none")
            );
            println!(
                "  Tracking ref:   {}",
                tracking_ref.as_deref().unwrap_or("none")
            );
        }
    }

    Ok(())
}

/// Returns the path to the managed global store repository (`~/.lore/sync`).
fn global_store_path() -> Result<PathBuf> {
    let home = dirs::home_dir().ok_or_else(|| anyhow!("Could not find home directory"))?;
    Ok(home.join(".lore").join("sync"))
}

/// Ensures the managed global store repo exists and is configured.
///
/// Creates the directory and runs `git init` if absent, configures a local
/// committer identity so `commit-tree` works without relying on the user's
/// global git config, disables commit signing so it never blocks on a GPG
/// prompt, and adds or updates the `origin` remote to point at `remote_url`.
/// Idempotent: safe to call on every setup and sync.
fn ensure_global_repo(repo: &Path, remote_url: &str) -> Result<()> {
    std::fs::create_dir_all(repo).with_context(|| {
        format!(
            "Failed to create the global store directory: {}",
            repo.display()
        )
    })?;

    if !repo.join(".git").exists() {
        run_git_checked(repo, &["init", "-q"], "initialize the global store repo")?;
    }

    run_git_checked(
        repo,
        &["config", "user.name", "lore"],
        "configure the global store committer name",
    )?;
    run_git_checked(
        repo,
        &["config", "user.email", "lore@localhost"],
        "configure the global store committer email",
    )?;
    run_git_checked(
        repo,
        &["config", "commit.gpgsign", "false"],
        "disable commit signing for the global store",
    )?;

    configure_origin_remote(repo, remote_url)?;
    Ok(())
}

/// Adds the `origin` remote, or updates its URL if it already exists.
fn configure_origin_remote(repo: &Path, remote_url: &str) -> Result<()> {
    let has_origin = Command::new("git")
        .current_dir(repo)
        .args(["remote", "get-url", GLOBAL_REMOTE])
        .output()
        .context("Failed to run git remote get-url")?
        .status
        .success();

    let args: [&str; 4] = if has_origin {
        ["remote", "set-url", GLOBAL_REMOTE, remote_url]
    } else {
        ["remote", "add", GLOBAL_REMOTE, remote_url]
    };
    run_git_checked(repo, &args, "configure the global store remote")?;
    Ok(())
}

/// Runs a git command in `repo`, returning a contextual error on failure.
fn run_git_checked(repo: &Path, args: &[&str], action: &str) -> Result<()> {
    let output = Command::new("git")
        .current_dir(repo)
        .args(args)
        .output()
        .with_context(|| format!("Failed to run git to {action}"))?;
    if !output.status.success() {
        bail!(
            "Failed to {action}: {}",
            String::from_utf8_lossy(&output.stderr).trim()
        );
    }
    Ok(())
}

/// Prompts for the git remote URL of the user's private global store repo.
fn prompt_global_remote_url() -> Result<String> {
    println!("{}", "Set up your global personal store.".bold());
    println!(
        "Enter the git remote URL of a private repository only you can access.\n\
         It holds your cross-tool, cross-repo reasoning history for personal\n\
         multi-machine backup and search."
    );
    print!("Remote URL: ");
    io::stdout().flush()?;
    let mut url = String::new();
    io::stdin().read_line(&mut url)?;
    let url = url.trim().to_string();
    if url.is_empty() {
        bail!("A remote URL is required to set up the global store.");
    }
    Ok(url)
}

// ==================== helpers ====================

/// Loads the store's key and salt, or errors pointing the user to setup.
fn load_store_credentials(
    repo: &Path,
    remote: &str,
    keystore: &KeyStore,
) -> Result<(Vec<u8>, Vec<u8>)> {
    let salt = read_store_salt(repo, remote)?.ok_or_else(|| {
        anyhow!("This repo's lore store is not set up. Run 'lore sync setup' first.")
    })?;
    let store_id = store_id_from_salt(&salt);
    let key = keystore.load_key(&store_id)?.ok_or_else(|| {
        anyhow!("No sync key for this repo on this machine. Run 'lore sync setup' first.")
    })?;
    Ok((key, salt))
}

/// Reads the store salt from the local ref, falling back to the tracking ref.
fn read_store_salt(repo: &Path, remote: &str) -> Result<Option<Vec<u8>>> {
    if gitref::ref_exists(repo, SESSIONS_REF)? {
        if let Some(bytes) = read_ref_path(repo, SESSIONS_REF, "meta/salt")? {
            return Ok(Some(bytes));
        }
    }
    let tracking = gitref::tracking_ref_name(remote, SESSIONS_REF)?;
    if gitref::ref_exists(repo, &tracking)? {
        if let Some(bytes) = read_ref_path(repo, &tracking, "meta/salt")? {
            return Ok(Some(bytes));
        }
    }
    Ok(None)
}

/// Returns the bytes of the first encrypted session in the store, if any.
///
/// Prefers the remote-tracking ref (freshly fetched) and falls back to the
/// local ref, so a join can verify the passphrase against real ciphertext.
fn first_session_blob(repo: &Path, remote: &str) -> Result<Option<Vec<u8>>> {
    let tracking = gitref::tracking_ref_name(remote, SESSIONS_REF)?;
    for reference in [tracking.as_str(), SESSIONS_REF] {
        if !gitref::ref_exists(repo, reference)? {
            continue;
        }
        let entries = gitref::read_tree(repo, reference)?;
        if let Some(sha) = entries
            .iter()
            .find(|e| is_session_blob(&e.path))
            .map(|e| e.sha.clone())
        {
            return Ok(Some(gitref::read_blob(repo, &sha)?));
        }
    }
    Ok(None)
}

/// Selects the base tree and parent commit for a store-metadata write.
///
/// Prefers the remote-tracking ref (a join adopts remote state), then the local
/// ref, then none (a fresh store).
fn store_base(repo: &Path, remote: &str) -> Result<(Option<String>, Option<String>)> {
    let tracking = gitref::tracking_ref_name(remote, SESSIONS_REF)?;
    if gitref::ref_exists(repo, &tracking)? {
        return Ok((
            Some(tracking.clone()),
            gitref::resolve_ref(repo, &tracking)?,
        ));
    }
    if let Some(commit) = gitref::resolve_ref(repo, SESSIONS_REF)? {
        return Ok((Some(SESSIONS_REF.to_string()), Some(commit)));
    }
    Ok((None, None))
}

/// Reads and parses `meta/machines.json` from a ref or tree-ish.
fn read_machines(repo: &Path, reference: &str) -> Result<BTreeMap<String, String>> {
    match read_ref_path(repo, reference, "meta/machines.json")? {
        Some(bytes) => Ok(serde_json::from_slice(&bytes).unwrap_or_default()),
        None => Ok(BTreeMap::new()),
    }
}

/// Reads a single path's blob bytes from a ref or tree-ish.
fn read_ref_path(repo: &Path, reference: &str, path: &str) -> Result<Option<Vec<u8>>> {
    let entries = gitref::read_tree(repo, reference)?;
    blob_at_path(repo, &entries, path)
}

/// Reads a single path's blob bytes from an already-read tree.
fn blob_at_path(repo: &Path, entries: &[TreeEntry], path: &str) -> Result<Option<Vec<u8>>> {
    match entries.iter().find(|e| e.path == path) {
        Some(entry) => Ok(Some(gitref::read_blob(repo, &entry.sha)?)),
        None => Ok(None),
    }
}

/// Returns whether a tree path is an encrypted session blob.
fn is_session_blob(path: &str) -> bool {
    path.starts_with("sessions/") && path.ends_with(".enc")
}

/// Returns whether a tree path is a stored session artifact.
///
/// Covers both the encrypted record (`sessions/<id>.enc`) and its plaintext
/// metadata sidecar (`sessions/<id>.meta.json`), the two entries a stored
/// session contributes to the tree.
fn is_session_path(path: &str) -> bool {
    path.starts_with("sessions/") && (path.ends_with(".enc") || path.ends_with(".meta.json"))
}

/// Parses the session UUID from a stored session artifact tree path.
///
/// Handles both artifact forms a session contributes to the tree:
/// `sessions/<uuid>.enc` and `sessions/<uuid>.meta.json`. Returns `None` for any
/// path that is not a recognizable session artifact or whose stem is not a
/// UUID, so callers can conservatively skip it.
fn session_uuid_from_path(path: &str) -> Option<Uuid> {
    let name = path.strip_prefix("sessions/")?;
    let stem = name
        .strip_suffix(".meta.json")
        .or_else(|| name.strip_suffix(".enc"))?;
    Uuid::parse_str(stem).ok()
}

/// Carries forward in-scope, local-only session artifacts when rebasing on the
/// remote.
///
/// When the outgoing tree is based on the remote tracking commit, any session
/// entry present in the local ref but absent from the fetched remote tree (a
/// remote rewind or reset, and whose session is already synced so it is not in
/// the freshly re-encrypted set) would be dropped. This re-adds each such entry
/// at its existing blob SHA. A freshly re-encrypted unsynced session already
/// owns its paths in `changes`, so `or_insert` never overwrites those.
///
/// Only artifacts whose session id is in `in_scope` (the ids of this repo's
/// sessions, from [`Database::get_session_ids_for_repo`]) are carried forward.
/// A local ref can hold out-of-scope sessions when it was written before per-repo
/// scoping existed; carrying those forward would re-push another repo's history
/// and reopen the privacy leak, so any artifact that is out of scope, or whose id
/// cannot be parsed or is not in the local database, is conservatively skipped.
fn carry_forward_local_sessions(
    repo: &Path,
    local_commit: &str,
    tracking_entries: &[TreeEntry],
    in_scope: &HashSet<Uuid>,
    changes: &mut BTreeMap<String, String>,
) -> Result<()> {
    let tracking_paths: HashSet<&str> = tracking_entries.iter().map(|e| e.path.as_str()).collect();

    for entry in gitref::read_tree(repo, local_commit)? {
        if !is_session_path(&entry.path) || tracking_paths.contains(entry.path.as_str()) {
            continue;
        }
        match session_uuid_from_path(&entry.path) {
            Some(id) if in_scope.contains(&id) => {}
            _ => continue,
        }
        changes.entry(entry.path.clone()).or_insert(entry.sha);
    }
    Ok(())
}

/// Heuristically detects a non-fast-forward push rejection for retry.
///
/// Push rejection wording is not localized by git for the machine-readable
/// markers checked here, but this is only used to decide whether to re-fetch and
/// retry; the final attempt surfaces the original error regardless.
fn is_non_fast_forward(err: &SyncError) -> bool {
    let message = err.to_string().to_lowercase();
    message.contains("fast-forward")
        || message.contains("non-fast")
        || message.contains("rejected")
        || message.contains("fetch first")
        || message.contains("stale info")
}

/// Resolves the current git repository's top-level directory.
fn current_repo() -> Result<PathBuf> {
    let output = Command::new("git")
        .args(["rev-parse", "--show-toplevel"])
        .output()
        .context("Failed to run git")?;

    if !output.status.success() {
        bail!("Not inside a git repository. Run this command from within a repo.");
    }

    let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if path.is_empty() {
        bail!("Not inside a git repository. Run this command from within a repo.");
    }
    Ok(PathBuf::from(path))
}

/// Builds this machine's identity from config, persisting a generated id.
fn machine_identity(config: &mut Config) -> Result<MachineIdentity> {
    let id = config.get_or_create_machine_id()?;
    let name = config.get_machine_name();
    Ok(MachineIdentity { id, name })
}

/// Prompts for a new passphrase with confirmation and a minimum length.
fn prompt_new_passphrase() -> Result<String> {
    loop {
        print!("Enter passphrase: ");
        io::stdout().flush()?;
        let passphrase = rpassword::read_password()?;

        if passphrase.len() < MIN_PASSPHRASE_LEN {
            println!(
                "{}",
                format!("Passphrase must be at least {MIN_PASSPHRASE_LEN} characters.").red()
            );
            continue;
        }

        print!("Confirm passphrase: ");
        io::stdout().flush()?;
        let confirm = rpassword::read_password()?;

        if passphrase != confirm {
            println!("{}", "Passphrases do not match.".red());
            continue;
        }

        return Ok(passphrase);
    }
}

/// Prompts for an existing passphrase without confirmation.
fn prompt_passphrase() -> Result<String> {
    print!("Passphrase: ");
    io::stdout().flush()?;
    let passphrase = rpassword::read_password()?;
    Ok(passphrase)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::models::{
        Annotation, LinkCreator, LinkType, Message, MessageContent, MessageRole, SessionLink,
        Summary, Tag,
    };
    use std::process::Command;
    use tempfile::TempDir;

    /// Runs a git command in a test repo, asserting success.
    fn git(repo: &Path, args: &[&str]) {
        let output = Command::new("git")
            .current_dir(repo)
            .args(args)
            .output()
            .expect("failed to spawn git");
        assert!(
            output.status.success(),
            "git {args:?} failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    /// Runs a git command in a test repo, asserting success and returning stdout.
    fn git_out(repo: &Path, args: &[&str]) -> String {
        let output = Command::new("git")
            .current_dir(repo)
            .args(args)
            .output()
            .expect("failed to spawn git");
        assert!(
            output.status.success(),
            "git {args:?} failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        String::from_utf8_lossy(&output.stdout).trim().to_string()
    }

    /// Initializes a temp repo with an identity and signing disabled.
    fn init_repo(repo: &Path) {
        git(repo, &["init", "-q"]);
        git(repo, &["config", "user.name", "Lore Test"]);
        git(repo, &["config", "user.email", "test@example.com"]);
        git(repo, &["config", "commit.gpgsign", "false"]);
        git(repo, &["config", "tag.gpgsign", "false"]);
    }

    /// Creates a bare remote repo and returns its temp dir and URL string.
    fn init_bare_remote() -> (TempDir, String) {
        let dir = tempfile::tempdir().unwrap();
        git(dir.path(), &["init", "--bare", "-q"]);
        let url = dir.path().to_str().unwrap().to_string();
        (dir, url)
    }

    /// Opens a fresh database in a temp directory.
    fn open_db() -> (Database, TempDir) {
        let dir = tempfile::tempdir().unwrap();
        let db = Database::open(&dir.path().join("lore.db")).unwrap();
        (db, dir)
    }

    /// Builds a file-backed key store isolated to a temp directory.
    fn test_keystore() -> (KeyStore, TempDir) {
        let dir = tempfile::tempdir().unwrap();
        let store = KeyStore::with_base_dir(dir.path().to_path_buf(), false);
        (store, dir)
    }

    fn machine(id: &str, name: &str) -> MachineIdentity {
        MachineIdentity {
            id: id.to_string(),
            name: name.to_string(),
        }
    }

    /// Returns the canonical string form of a repo path.
    ///
    /// A session must be seeded with the same path the scoped selector derives
    /// (which canonicalizes the repo, resolving symlinks such as macOS's
    /// `/var` -> `/private/var`) so it stays in scope and is actually pushed.
    fn repo_dir(repo: &Path) -> String {
        repo.canonicalize().unwrap().to_string_lossy().to_string()
    }

    /// Returns this repo's unsynced sessions, the set a real sync would push.
    fn scoped_unsynced(db: &Database, repo: &Path) -> Vec<Session> {
        db.get_unsynced_sessions_for_repo(repo).unwrap()
    }

    /// Seeds a full unsynced session (messages, link, tag, annotation, summary).
    ///
    /// `working_directory` must be inside the repo under test for the session to
    /// be selected by the repo-scoped sync.
    fn seed_full_session(db: &mut Database, machine_id: &str, working_directory: &str) -> Uuid {
        let id = Uuid::new_v4();
        let session = Session {
            id,
            tool: "claude-code".to_string(),
            tool_version: Some("1.0.0".to_string()),
            started_at: Utc::now(),
            ended_at: Some(Utc::now()),
            model: Some("claude-opus".to_string()),
            working_directory: working_directory.to_string(),
            git_branch: Some("main".to_string()),
            source_path: None,
            message_count: 1,
            machine_id: Some(machine_id.to_string()),
        };
        let message = Message {
            id: Uuid::new_v4(),
            session_id: id,
            parent_id: None,
            index: 0,
            timestamp: Utc::now(),
            role: MessageRole::User,
            content: MessageContent::Text("fix the bug".to_string()),
            model: None,
            git_branch: Some("main".to_string()),
            cwd: Some(working_directory.to_string()),
        };
        // synced_at = None so the session is picked up by get_unsynced_sessions.
        db.import_session_with_messages(&session, &[message], None)
            .unwrap();
        db.insert_link(&SessionLink {
            id: Uuid::new_v4(),
            session_id: id,
            link_type: LinkType::Commit,
            commit_sha: Some("deadbeef".to_string()),
            branch: Some("main".to_string()),
            remote: Some("origin".to_string()),
            created_at: Utc::now(),
            created_by: LinkCreator::User,
            confidence: Some(0.9),
        })
        .unwrap();
        db.insert_tag(&Tag {
            id: Uuid::new_v4(),
            session_id: id,
            label: "feature".to_string(),
            created_at: Utc::now(),
        })
        .unwrap();
        db.insert_annotation(&Annotation {
            id: Uuid::new_v4(),
            session_id: id,
            content: "an important note".to_string(),
            created_at: Utc::now(),
        })
        .unwrap();
        db.insert_summary(&Summary {
            id: Uuid::new_v4(),
            session_id: id,
            content: "fixed the parser".to_string(),
            generated_at: Utc::now(),
        })
        .unwrap();
        id
    }

    /// Returns the blob SHA of the single session `.enc` entry in the local ref.
    fn session_blob_sha(repo: &Path) -> String {
        let entries = gitref::read_tree(repo, SESSIONS_REF).unwrap();
        entries
            .iter()
            .find(|e| is_session_blob(&e.path))
            .expect("a session blob should exist")
            .sha
            .clone()
    }

    /// Returns the tree path of the single session `.enc` entry in the local ref.
    fn session_blob_sha_path(repo: &Path) -> String {
        let entries = gitref::read_tree(repo, SESSIONS_REF).unwrap();
        entries
            .iter()
            .find(|e| is_session_blob(&e.path))
            .expect("a session blob should exist")
            .path
            .clone()
    }

    #[test]
    fn test_create_store_writes_salt_and_pushes() {
        let (_remote_dir, remote_url) = init_bare_remote();
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(repo, &["remote", "add", "origin", &remote_url]);

        let (keystore, _kd) = test_keystore();
        let m = machine("machine-a", "Machine A");

        create_store(repo, "origin", &keystore, &m, "correct horse battery").unwrap();

        // The local ref exists and holds the salt.
        assert!(gitref::ref_exists(repo, SESSIONS_REF).unwrap());
        let salt = read_store_salt(repo, "origin")
            .unwrap()
            .expect("salt written");
        // The derived key is stored under the salt-derived store id.
        let store_id = store_id_from_salt(&salt);
        assert!(keystore.load_key(&store_id).unwrap().is_some());
        // The store was pushed to the remote.
        assert!(gitref::remote_ref_exists(repo, "origin", SESSIONS_REF).unwrap());
    }

    #[test]
    fn test_sync_round_trip_between_machines() {
        let (_remote_dir, remote_url) = init_bare_remote();
        let passphrase = "shared team passphrase";

        // Machine A: set up, seed a full session, and sync (push).
        let dir_a = tempfile::tempdir().unwrap();
        let repo_a = dir_a.path();
        init_repo(repo_a);
        git(repo_a, &["remote", "add", "origin", &remote_url]);
        let (keystore_a, _ka) = test_keystore();
        let ma = machine("machine-a", "Machine A");
        create_store(repo_a, "origin", &keystore_a, &ma, passphrase).unwrap();

        let (mut db_a, _da) = open_db();
        let session_id = seed_full_session(&mut db_a, "machine-a", &repo_dir(repo_a));
        let (key_a, salt_a) = load_store_credentials(repo_a, "origin", &keystore_a).unwrap();
        let sessions_a = scoped_unsynced(&db_a, repo_a);
        let summary_a = perform_sync(
            &mut db_a, repo_a, "origin", &key_a, &salt_a, &ma, sessions_a,
        )
        .unwrap();
        assert_eq!(summary_a.pushed, 1);

        // Machine B: join with the same passphrase, then sync (pull).
        let dir_b = tempfile::tempdir().unwrap();
        let repo_b = dir_b.path();
        init_repo(repo_b);
        git(repo_b, &["remote", "add", "origin", &remote_url]);
        let (keystore_b, _kb) = test_keystore();
        let mb = machine("machine-b", "Machine B");
        gitref::fetch(repo_b, "origin", SESSIONS_REF).unwrap();
        let salt_b = read_store_salt(repo_b, "origin").unwrap().unwrap();
        join_store(repo_b, "origin", &keystore_b, &mb, &salt_b, passphrase).unwrap();

        let (mut db_b, _db) = open_db();
        let (key_b, salt_b2) = load_store_credentials(repo_b, "origin", &keystore_b).unwrap();
        // Both machines derive the same key from the shared passphrase and salt.
        assert_eq!(key_a, key_b);
        let sessions_b = scoped_unsynced(&db_b, repo_b);
        let summary_b = perform_sync(
            &mut db_b, repo_b, "origin", &key_b, &salt_b2, &mb, sessions_b,
        )
        .unwrap();
        assert_eq!(summary_b.pulled, 1);

        // Machine B now has the full reasoning record, including links, tags,
        // and the summary.
        let pulled = db_b
            .get_session(&session_id)
            .unwrap()
            .expect("session pulled");
        assert_eq!(pulled.tool, "claude-code");
        assert_eq!(db_b.get_messages(&session_id).unwrap().len(), 1);
        let links = db_b.get_links_by_session(&session_id).unwrap();
        assert_eq!(links.len(), 1);
        assert_eq!(links[0].commit_sha, Some("deadbeef".to_string()));
        assert_eq!(db_b.get_tags(&session_id).unwrap()[0].label, "feature");
        assert_eq!(db_b.get_annotations(&session_id).unwrap().len(), 1);
        assert_eq!(
            db_b.get_summary(&session_id).unwrap().unwrap().content,
            "fixed the parser"
        );
    }

    #[test]
    fn test_incremental_sync_preserves_unchanged_blob() {
        let (_remote_dir, remote_url) = init_bare_remote();
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(repo, &["remote", "add", "origin", &remote_url]);

        let (keystore, _kd) = test_keystore();
        let m = machine("machine-a", "Machine A");
        create_store(repo, "origin", &keystore, &m, "passphrase one two").unwrap();

        let (mut db, _dd) = open_db();
        seed_full_session(&mut db, "machine-a", &repo_dir(repo));
        let (key, salt) = load_store_credentials(repo, "origin", &keystore).unwrap();

        let sessions = scoped_unsynced(&db, repo);
        perform_sync(&mut db, repo, "origin", &key, &salt, &m, sessions).unwrap();
        let sha_first = session_blob_sha(repo);

        // A second sync with nothing new must not re-encrypt the session, so its
        // content-addressed blob object stays byte-identical (near-zero growth).
        let sessions = scoped_unsynced(&db, repo);
        let summary = perform_sync(&mut db, repo, "origin", &key, &salt, &m, sessions).unwrap();
        assert_eq!(summary.pushed, 0);
        let sha_second = session_blob_sha(repo);
        assert_eq!(
            sha_first, sha_second,
            "unchanged session blob must be reused"
        );
    }

    #[test]
    fn test_sync_errors_when_not_set_up() {
        let (_remote_dir, remote_url) = init_bare_remote();
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(repo, &["remote", "add", "origin", &remote_url]);

        let (keystore, _kd) = test_keystore();
        let err = load_store_credentials(repo, "origin", &keystore).unwrap_err();
        assert!(
            err.to_string().contains("lore sync setup"),
            "error should point to setup: {err}"
        );
    }

    #[test]
    fn test_join_with_wrong_passphrase_fails() {
        let (_remote_dir, remote_url) = init_bare_remote();

        // Machine A creates the store and pushes a real session.
        let dir_a = tempfile::tempdir().unwrap();
        let repo_a = dir_a.path();
        init_repo(repo_a);
        git(repo_a, &["remote", "add", "origin", &remote_url]);
        let (keystore_a, _ka) = test_keystore();
        let ma = machine("machine-a", "Machine A");
        create_store(repo_a, "origin", &keystore_a, &ma, "the real passphrase").unwrap();
        let (mut db_a, _da) = open_db();
        seed_full_session(&mut db_a, "machine-a", &repo_dir(repo_a));
        let (key_a, salt_a) = load_store_credentials(repo_a, "origin", &keystore_a).unwrap();
        let sessions_a = scoped_unsynced(&db_a, repo_a);
        perform_sync(
            &mut db_a, repo_a, "origin", &key_a, &salt_a, &ma, sessions_a,
        )
        .unwrap();

        // Machine B tries to join with the wrong passphrase.
        let dir_b = tempfile::tempdir().unwrap();
        let repo_b = dir_b.path();
        init_repo(repo_b);
        git(repo_b, &["remote", "add", "origin", &remote_url]);
        let (keystore_b, _kb) = test_keystore();
        let mb = machine("machine-b", "Machine B");
        gitref::fetch(repo_b, "origin", SESSIONS_REF).unwrap();
        let salt_b = read_store_salt(repo_b, "origin").unwrap().unwrap();

        let err = join_store(
            repo_b,
            "origin",
            &keystore_b,
            &mb,
            &salt_b,
            "the WRONG passphrase",
        )
        .unwrap_err();
        assert!(
            err.to_string().to_lowercase().contains("passphrase"),
            "wrong passphrase should be reported: {err}"
        );
    }

    #[test]
    fn test_merge_skips_older_remote_session() {
        // A remote record that is not newer than the local copy is left as-is.
        let (_remote_dir, remote_url) = init_bare_remote();
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(repo, &["remote", "add", "origin", &remote_url]);
        let (keystore, _kd) = test_keystore();
        let m = machine("machine-a", "Machine A");
        create_store(repo, "origin", &keystore, &m, "passphrase abcdefgh").unwrap();
        let (key, _salt) = load_store_credentials(repo, "origin", &keystore).unwrap();

        // Local session with two messages.
        let (mut db, _dd) = open_db();
        let id = Uuid::new_v4();
        let mut session = Session {
            id,
            tool: "claude-code".to_string(),
            tool_version: None,
            started_at: Utc::now(),
            ended_at: Some(Utc::now()),
            model: None,
            working_directory: "/proj".to_string(),
            git_branch: None,
            source_path: None,
            message_count: 2,
            machine_id: Some("machine-a".to_string()),
        };
        db.import_session_with_messages(&session, &[], Some(Utc::now()))
            .unwrap();

        // Build an older remote record (fewer messages) and encrypt it as a blob.
        session.message_count = 1;
        let record = SessionRecord {
            session: session.clone(),
            messages: vec![],
            links: vec![],
            tags: vec![],
            annotations: vec![],
            summary: None,
        };
        let blob = encrypt_session_record(&record, &key).unwrap();
        let sha = gitref::write_blob(repo, &blob).unwrap();
        let entries = vec![TreeEntry {
            mode: "100644".to_string(),
            sha,
            path: format!("sessions/{id}.enc"),
        }];

        let pulled = merge_remote(&mut db, repo, &entries, &key).unwrap();
        assert_eq!(pulled, 0, "older remote session must be skipped");
        assert_eq!(db.get_session(&id).unwrap().unwrap().message_count, 2);
    }

    #[test]
    fn test_is_session_blob() {
        assert!(is_session_blob("sessions/abc.enc"));
        assert!(!is_session_blob("sessions/abc.meta.json"));
        assert!(!is_session_blob("meta/salt"));
        assert!(!is_session_blob("abc.enc"));
    }

    #[test]
    fn test_is_non_fast_forward_detects_rejection() {
        let rejected = SyncError::Git(
            "git push origin failed: ! [rejected] refs/lore/sessions (fetch first)".to_string(),
        );
        assert!(is_non_fast_forward(&rejected));
        let unrelated = SyncError::Git("git push origin failed: permission denied".to_string());
        assert!(!is_non_fast_forward(&unrelated));
    }

    #[test]
    fn test_is_session_path() {
        assert!(is_session_path("sessions/abc.enc"));
        assert!(is_session_path("sessions/abc.meta.json"));
        assert!(!is_session_path("meta/salt"));
        assert!(!is_session_path("abc.enc"));
    }

    #[test]
    fn test_added_link_reopens_session_and_syncs() {
        // Adding a link to an already-synced session must re-export it, so the
        // new link rides along to a teammate on the next sync.
        let (_remote_dir, remote_url) = init_bare_remote();
        let passphrase = "shared team passphrase";

        // Machine A: set up, seed a session, and sync it (push).
        let dir_a = tempfile::tempdir().unwrap();
        let repo_a = dir_a.path();
        init_repo(repo_a);
        git(repo_a, &["remote", "add", "origin", &remote_url]);
        let (keystore_a, _ka) = test_keystore();
        let ma = machine("machine-a", "Machine A");
        create_store(repo_a, "origin", &keystore_a, &ma, passphrase).unwrap();

        let (mut db_a, _da) = open_db();
        let session_id = seed_full_session(&mut db_a, "machine-a", &repo_dir(repo_a));
        let (key_a, salt_a) = load_store_credentials(repo_a, "origin", &keystore_a).unwrap();
        let sessions_a = scoped_unsynced(&db_a, repo_a);
        let first = perform_sync(
            &mut db_a, repo_a, "origin", &key_a, &salt_a, &ma, sessions_a,
        )
        .unwrap();
        assert_eq!(first.pushed, 1);

        // The session is now synced; adding a link locally must re-open it.
        db_a.insert_link(&SessionLink {
            id: Uuid::new_v4(),
            session_id,
            link_type: LinkType::Commit,
            commit_sha: Some("feedface".to_string()),
            branch: Some("main".to_string()),
            remote: Some("origin".to_string()),
            created_at: Utc::now(),
            created_by: LinkCreator::Auto,
            confidence: Some(0.8),
        })
        .unwrap();

        let sessions_a = scoped_unsynced(&db_a, repo_a);
        let second = perform_sync(
            &mut db_a, repo_a, "origin", &key_a, &salt_a, &ma, sessions_a,
        )
        .unwrap();
        assert_eq!(
            second.pushed, 1,
            "adding a link must re-export the parent session"
        );

        // Machine B joins and pulls: it must see both links.
        let dir_b = tempfile::tempdir().unwrap();
        let repo_b = dir_b.path();
        init_repo(repo_b);
        git(repo_b, &["remote", "add", "origin", &remote_url]);
        let (keystore_b, _kb) = test_keystore();
        let mb = machine("machine-b", "Machine B");
        gitref::fetch(repo_b, "origin", SESSIONS_REF).unwrap();
        let salt_b = read_store_salt(repo_b, "origin").unwrap().unwrap();
        join_store(repo_b, "origin", &keystore_b, &mb, &salt_b, passphrase).unwrap();
        let (mut db_b, _db) = open_db();
        let (key_b, salt_b2) = load_store_credentials(repo_b, "origin", &keystore_b).unwrap();
        let sessions_b = scoped_unsynced(&db_b, repo_b);
        perform_sync(
            &mut db_b, repo_b, "origin", &key_b, &salt_b2, &mb, sessions_b,
        )
        .unwrap();

        let links = db_b.get_links_by_session(&session_id).unwrap();
        assert_eq!(links.len(), 2, "both links must reach the teammate");
    }

    #[test]
    fn test_wrong_key_errors_before_push() {
        // A sync whose stored key does not match a non-empty remote store must
        // error before pushing, and must not mark local sessions synced.
        let (_remote_dir, remote_url) = init_bare_remote();

        // Machine A creates the store and pushes a real (correctly keyed) session.
        let dir_a = tempfile::tempdir().unwrap();
        let repo_a = dir_a.path();
        init_repo(repo_a);
        git(repo_a, &["remote", "add", "origin", &remote_url]);
        let (keystore_a, _ka) = test_keystore();
        let ma = machine("machine-a", "Machine A");
        create_store(repo_a, "origin", &keystore_a, &ma, "the real passphrase").unwrap();
        let (mut db_a, _da) = open_db();
        seed_full_session(&mut db_a, "machine-a", &repo_dir(repo_a));
        let (key_a, salt_a) = load_store_credentials(repo_a, "origin", &keystore_a).unwrap();
        let sessions_a = scoped_unsynced(&db_a, repo_a);
        perform_sync(
            &mut db_a, repo_a, "origin", &key_a, &salt_a, &ma, sessions_a,
        )
        .unwrap();

        // Machine B stores a WRONG key for the same store (same salt, bad pass).
        let dir_b = tempfile::tempdir().unwrap();
        let repo_b = dir_b.path();
        init_repo(repo_b);
        git(repo_b, &["remote", "add", "origin", &remote_url]);
        let (keystore_b, _kb) = test_keystore();
        let mb = machine("machine-b", "Machine B");
        gitref::fetch(repo_b, "origin", SESSIONS_REF).unwrap();
        let salt_b = read_store_salt(repo_b, "origin").unwrap().unwrap();
        let wrong_key = derive_store_key("the WRONG passphrase", &salt_b).unwrap();
        keystore_b
            .store_key(&store_id_from_salt(&salt_b), &wrong_key)
            .unwrap();

        // Machine B has a local unsynced session that must not be pushed/marked.
        let (mut db_b, _db) = open_db();
        let local_id = seed_full_session(&mut db_b, "machine-b", &repo_dir(repo_b));
        let sessions_b = scoped_unsynced(&db_b, repo_b);

        let err = perform_sync(
            &mut db_b, repo_b, "origin", &wrong_key, &salt_b, &mb, sessions_b,
        )
        .unwrap_err();
        let msg = err.to_string().to_lowercase();
        assert!(
            msg.contains("passphrase") || msg.contains("decrypt"),
            "wrong key must be reported clearly: {err}"
        );

        // The local session must remain unsynced (nothing was pushed).
        let unsynced = db_b.get_unsynced_sessions().unwrap();
        assert!(
            unsynced.iter().any(|s| s.id == local_id),
            "local session must not be marked synced after a wrong-key failure"
        );
    }

    #[test]
    fn test_carry_forward_local_sessions_readds_missing() {
        // A session artifact present only in the local ref (missing from the
        // fetched remote tree) must be carried into the outgoing changes.
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);

        let id_a = Uuid::new_v4();
        let id_b = Uuid::new_v4();
        let a_enc = format!("sessions/{id_a}.enc");
        let a_meta = format!("sessions/{id_a}.meta.json");
        let b_enc = format!("sessions/{id_b}.enc");

        let enc_sha = gitref::write_blob(repo, b"local-a-enc").unwrap();
        let meta_sha = gitref::write_blob(repo, b"local-a-meta").unwrap();
        let other_sha = gitref::write_blob(repo, b"remote-b-enc").unwrap();

        let mut local = BTreeMap::new();
        local.insert(a_enc.clone(), enc_sha.clone());
        local.insert(a_meta.clone(), meta_sha.clone());
        local.insert(b_enc.clone(), other_sha.clone());
        let local_tree = gitref::build_tree(repo, None, &local).unwrap();
        let local_commit = gitref::commit_tree(repo, &local_tree, None, "lore: local").unwrap();

        // The remote (tracking) tree only has session b.
        let tracking = vec![TreeEntry {
            mode: "100644".to_string(),
            sha: other_sha,
            path: b_enc.clone(),
        }];

        // A freshly re-encrypted unsynced session owns its own path already.
        let mut changes = BTreeMap::new();
        changes.insert(a_enc.clone(), "fresh-sha".to_string());

        // Both sessions are in scope for this repo.
        let in_scope: HashSet<Uuid> = [id_a, id_b].into_iter().collect();
        carry_forward_local_sessions(repo, &local_commit, &tracking, &in_scope, &mut changes)
            .unwrap();

        // a.meta.json (local-only) is carried forward.
        assert_eq!(changes.get(&a_meta), Some(&meta_sha));
        // a.enc keeps the fresh re-encrypted value (or_insert must not override).
        assert_eq!(changes.get(&a_enc), Some(&"fresh-sha".to_string()));
        // b.enc is in the tracking tree, so it is not re-added.
        assert!(!changes.contains_key(&b_enc));
    }

    #[test]
    fn test_carry_forward_skips_out_of_scope_sessions() {
        // A local-only session artifact whose id is NOT in this repo's scope must
        // not be carried forward. This is the privacy regression the scoped
        // carry-forward closes: a local ref written before scoping can hold other
        // repos' sessions, and re-pushing them would leak that history.
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);

        let id_in = Uuid::new_v4();
        let id_out = Uuid::new_v4();
        let in_enc = format!("sessions/{id_in}.enc");
        let in_meta = format!("sessions/{id_in}.meta.json");
        let out_enc = format!("sessions/{id_out}.enc");
        let out_meta = format!("sessions/{id_out}.meta.json");

        let in_enc_sha = gitref::write_blob(repo, b"in-enc").unwrap();
        let in_meta_sha = gitref::write_blob(repo, b"in-meta").unwrap();
        let out_enc_sha = gitref::write_blob(repo, b"out-enc").unwrap();
        let out_meta_sha = gitref::write_blob(repo, b"out-meta").unwrap();

        let mut local = BTreeMap::new();
        local.insert(in_enc.clone(), in_enc_sha);
        local.insert(in_meta.clone(), in_meta_sha);
        local.insert(out_enc.clone(), out_enc_sha);
        local.insert(out_meta.clone(), out_meta_sha);
        let local_tree = gitref::build_tree(repo, None, &local).unwrap();
        let local_commit = gitref::commit_tree(repo, &local_tree, None, "lore: local").unwrap();

        // The remote tree is empty (a rewind), so nothing is already present.
        let tracking: Vec<TreeEntry> = Vec::new();

        // Only the in-scope session's id is in scope.
        let in_scope: HashSet<Uuid> = [id_in].into_iter().collect();
        let mut changes = BTreeMap::new();
        carry_forward_local_sessions(repo, &local_commit, &tracking, &in_scope, &mut changes)
            .unwrap();

        // In-scope artifacts are carried forward.
        assert!(changes.contains_key(&in_enc), "in-scope .enc carried");
        assert!(changes.contains_key(&in_meta), "in-scope .meta carried");
        // Out-of-scope artifacts are dropped, not re-pushed.
        assert!(
            !changes.contains_key(&out_enc),
            "out-of-scope .enc must not be carried forward"
        );
        assert!(
            !changes.contains_key(&out_meta),
            "out-of-scope .meta must not be carried forward"
        );
    }

    #[test]
    fn test_sync_quiet_noops_when_not_set_up() {
        // A repo with a remote but no lore store must be a silent no-op. It must
        // not error and must not touch the config or database (this test does
        // not open ~/.lore, so reaching that code would surface as a failure).
        let (_remote_dir, remote_url) = init_bare_remote();
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(repo, &["remote", "add", "origin", &remote_url]);

        sync_quiet_in_repo(repo, "origin").expect("quiet sync must no-op when not set up");
    }

    #[test]
    fn test_sync_quiet_noops_without_remote() {
        // No configured remote at all: quiet sync skips gracefully.
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);

        sync_quiet_in_repo(repo, "origin").expect("quiet sync must no-op without a remote");
    }

    #[test]
    fn test_quiet_sync_no_key_returns_ok_without_mutating() {
        // Set up a store (salt in the ref) but present a key store that holds no
        // key for it. The quiet path must return Ok before reaching the mutating
        // machine-id step or opening the database. The key store is isolated to a
        // temp dir, so load_key resolves to None deterministically.
        let (_remote_dir, remote_url) = init_bare_remote();
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(repo, &["remote", "add", "origin", &remote_url]);

        // Create the store with one key store, then run the quiet sync against a
        // different, empty key store to model "set up elsewhere, no key here".
        let (setup_keystore, _sk) = test_keystore();
        let m = machine("machine-a", "Machine A");
        create_store(repo, "origin", &setup_keystore, &m, "passphrase abcdefgh").unwrap();

        let salt = read_store_salt(repo, "origin")
            .unwrap()
            .expect("salt present");
        let (empty_keystore, _ek) = test_keystore();
        assert!(
            empty_keystore
                .load_key(&store_id_from_salt(&salt))
                .unwrap()
                .is_none(),
            "the isolated key store must have no key for this store"
        );

        quiet_sync_with_keystore(repo, "origin", &salt, &empty_keystore)
            .expect("quiet sync must no-op when no key is stored on this machine");
    }

    #[test]
    fn test_resolve_hook_remote_prefers_named_remote() {
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(
            repo,
            &["remote", "add", "origin", "https://example.com/a.git"],
        );
        git(
            repo,
            &["remote", "add", "upstream", "https://example.com/b.git"],
        );

        assert_eq!(
            resolve_hook_remote(repo, "upstream").unwrap(),
            Some("upstream".to_string())
        );
    }

    #[test]
    fn test_resolve_hook_remote_falls_back_to_origin_for_url() {
        // git can pass a raw URL for an anonymous push; fall back to origin.
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(
            repo,
            &["remote", "add", "origin", "https://example.com/a.git"],
        );

        assert_eq!(
            resolve_hook_remote(repo, "https://example.com/other.git").unwrap(),
            Some("origin".to_string())
        );
    }

    #[test]
    fn test_resolve_hook_remote_none_when_unknown_and_no_origin() {
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(
            repo,
            &["remote", "add", "upstream", "https://example.com/b.git"],
        );

        assert_eq!(
            resolve_hook_remote(repo, "https://example.com/x.git").unwrap(),
            None
        );
    }

    #[test]
    fn test_local_only_session_survives_remote_rewind() {
        // If the remote store rewinds to before a session, a sync that rebases on
        // the remote must still keep the already-stored local session.
        let (remote_dir, remote_url) = init_bare_remote();
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(repo, &["remote", "add", "origin", &remote_url]);

        let (keystore, _kd) = test_keystore();
        let m = machine("machine-a", "Machine A");
        create_store(repo, "origin", &keystore, &m, "passphrase abcdefgh").unwrap();

        // The store-setup commit (no sessions) is our rewind target.
        let setup_commit = git_out(remote_dir.path(), &["rev-parse", SESSIONS_REF]);

        let (mut db, _dd) = open_db();
        seed_full_session(&mut db, "machine-a", &repo_dir(repo));
        let (key, salt) = load_store_credentials(repo, "origin", &keystore).unwrap();
        let sessions = scoped_unsynced(&db, repo);
        perform_sync(&mut db, repo, "origin", &key, &salt, &m, sessions).unwrap();

        let session_enc = session_blob_sha_path(repo);

        // Simulate a remote rewind: force the remote lore ref back to setup.
        git(
            remote_dir.path(),
            &["update-ref", SESSIONS_REF, setup_commit.trim()],
        );

        // Sync again. The already-synced session is not rebuilt, so only the
        // carry-forward keeps it in the outgoing tree.
        let sessions = scoped_unsynced(&db, repo);
        perform_sync(&mut db, repo, "origin", &key, &salt, &m, sessions).unwrap();

        let entries = gitref::read_tree(repo, SESSIONS_REF).unwrap();
        assert!(
            entries.iter().any(|e| e.path == session_enc),
            "local-only session must survive a remote rewind"
        );
    }

    #[test]
    fn test_sync_only_pushes_in_scope_sessions() {
        // A per-repo sync must push only sessions whose working directory is
        // inside this repo. A session captured in an unrelated directory must
        // stay out of this repo's store (the privacy bug this scoping fixes) and
        // remain unsynced.
        let (_remote_dir, remote_url) = init_bare_remote();
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(repo, &["remote", "add", "origin", &remote_url]);

        let (keystore, _kd) = test_keystore();
        let m = machine("machine-a", "Machine A");
        create_store(repo, "origin", &keystore, &m, "passphrase abcdefgh").unwrap();

        let (mut db, _dd) = open_db();
        // One session inside a subdirectory of the repo, one in an unrelated dir.
        let repo_sub = format!("{}/crate/src", repo_dir(repo));
        let in_scope = seed_full_session(&mut db, "machine-a", &repo_sub);
        let out_of_scope = seed_full_session(&mut db, "machine-a", "/somewhere/else/project");
        let (key, salt) = load_store_credentials(repo, "origin", &keystore).unwrap();

        let sessions = scoped_unsynced(&db, repo);
        let summary = perform_sync(&mut db, repo, "origin", &key, &salt, &m, sessions).unwrap();
        assert_eq!(
            summary.pushed, 1,
            "only the in-scope session must be pushed"
        );

        // The store holds exactly the in-scope session's encrypted blob.
        let entries = gitref::read_tree(repo, SESSIONS_REF).unwrap();
        let session_blobs = entries.iter().filter(|e| is_session_blob(&e.path)).count();
        assert_eq!(session_blobs, 1, "store must hold exactly one session blob");
        assert!(
            entries
                .iter()
                .any(|e| e.path == format!("sessions/{in_scope}.enc")),
            "the in-scope session must be stored"
        );
        assert!(
            !entries
                .iter()
                .any(|e| e.path == format!("sessions/{out_of_scope}.enc")),
            "the out-of-scope session must not reach this repo's store"
        );

        // The out-of-scope session stays unsynced; the in-scope one is marked
        // synced.
        let unsynced = db.get_unsynced_sessions().unwrap();
        let unsynced_ids: HashSet<Uuid> = unsynced.iter().map(|s| s.id).collect();
        assert!(
            unsynced_ids.contains(&out_of_scope),
            "the out-of-scope session must remain unsynced"
        );
        assert!(
            !unsynced_ids.contains(&in_scope),
            "the in-scope session must be marked synced after the push"
        );
    }

    #[test]
    fn test_sync_does_not_carry_forward_out_of_scope_local_session() {
        // A local ref written before per-repo scoping can hold another repo's
        // session artifact. When such an out-of-scope artifact lives only in the
        // local ref (absent from the remote), a sync that rebases on the remote
        // must NOT carry it forward, or it would re-push another repo's history.
        let (_remote_dir, remote_url) = init_bare_remote();
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(repo, &["remote", "add", "origin", &remote_url]);

        let (keystore, _kd) = test_keystore();
        let m = machine("machine-a", "Machine A");
        create_store(repo, "origin", &keystore, &m, "passphrase abcdefgh").unwrap();

        let (mut db, _dd) = open_db();
        let in_scope = seed_full_session(&mut db, "machine-a", &repo_dir(repo));
        // An out-of-scope session exists in the database (so its id is known) but
        // its working directory is outside the repo, so it is out of scope.
        let out_of_scope = seed_full_session(&mut db, "machine-a", "/somewhere/else/project");

        // Inject the out-of-scope session's artifacts into the LOCAL ref only,
        // simulating a pre-scoping local store that still holds them.
        let base = gitref::resolve_ref(repo, SESSIONS_REF).unwrap();
        let leaked_enc = gitref::write_blob(repo, b"leaked-enc").unwrap();
        let leaked_meta = gitref::write_blob(repo, b"leaked-meta").unwrap();
        let mut inject = BTreeMap::new();
        inject.insert(format!("sessions/{out_of_scope}.enc"), leaked_enc);
        inject.insert(format!("sessions/{out_of_scope}.meta.json"), leaked_meta);
        let tree = gitref::build_tree(repo, base.as_deref(), &inject).unwrap();
        let commit = gitref::commit_tree(repo, &tree, base.as_deref(), "inject leaked").unwrap();
        gitref::update_ref_checked(repo, SESSIONS_REF, &commit, base.as_deref()).unwrap();

        let (key, salt) = load_store_credentials(repo, "origin", &keystore).unwrap();
        let sessions = scoped_unsynced(&db, repo);
        perform_sync(&mut db, repo, "origin", &key, &salt, &m, sessions).unwrap();

        let entries = gitref::read_tree(repo, SESSIONS_REF).unwrap();
        // The in-scope session is stored.
        assert!(
            entries
                .iter()
                .any(|e| e.path == format!("sessions/{in_scope}.enc")),
            "the in-scope session must be stored"
        );
        // The out-of-scope local-only artifacts must not survive the sync.
        assert!(
            !entries
                .iter()
                .any(|e| e.path == format!("sessions/{out_of_scope}.enc")),
            "out-of-scope local-only session must not be carried forward or pushed"
        );
        assert!(
            !entries
                .iter()
                .any(|e| e.path == format!("sessions/{out_of_scope}.meta.json")),
            "out-of-scope local-only metadata must not be carried forward or pushed"
        );
    }

    #[test]
    fn test_sync_no_remote_does_not_inherit_out_of_scope_local_artifacts() {
        // No remote lore ref exists (the remote store was never initialized or was
        // deleted), so the outgoing tree base is empty rather than the local ref.
        // A local ref written before per-repo scoping can still hold out-of-scope
        // session artifacts; the no-remote path must build the tree from an empty
        // base and carry forward only in-scope local-only sessions, so those
        // out-of-scope artifacts are neither kept in the new local ref nor pushed.
        let (remote_dir, remote_url) = init_bare_remote();
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(repo, &["remote", "add", "origin", &remote_url]);

        let (keystore, _kd) = test_keystore();
        let m = machine("machine-a", "Machine A");
        create_store(repo, "origin", &keystore, &m, "passphrase abcdefgh").unwrap();

        let (mut db, _dd) = open_db();
        let in_scope = seed_full_session(&mut db, "machine-a", &repo_dir(repo));
        // An out-of-scope session exists in the database (so its id is known) but
        // its working directory is outside the repo, so it is out of scope.
        let out_of_scope = seed_full_session(&mut db, "machine-a", "/somewhere/else/project");

        // Inject the out-of-scope session's artifacts into the LOCAL ref only,
        // simulating a pre-scoping local store that still holds them.
        let base = gitref::resolve_ref(repo, SESSIONS_REF).unwrap();
        let leaked_enc = gitref::write_blob(repo, b"leaked-enc").unwrap();
        let leaked_meta = gitref::write_blob(repo, b"leaked-meta").unwrap();
        let mut inject = BTreeMap::new();
        inject.insert(format!("sessions/{out_of_scope}.enc"), leaked_enc.clone());
        inject.insert(format!("sessions/{out_of_scope}.meta.json"), leaked_meta);
        let tree = gitref::build_tree(repo, base.as_deref(), &inject).unwrap();
        let commit = gitref::commit_tree(repo, &tree, base.as_deref(), "inject leaked").unwrap();
        gitref::update_ref_checked(repo, SESSIONS_REF, &commit, base.as_deref()).unwrap();

        // Remove the remote lore ref so the sync takes the no-remote base path
        // (fetch returns None and the tree base is empty).
        git(remote_dir.path(), &["update-ref", "-d", SESSIONS_REF]);
        assert!(!gitref::remote_ref_exists(repo, "origin", SESSIONS_REF).unwrap());

        let (key, salt) = load_store_credentials(repo, "origin", &keystore).unwrap();
        let sessions = scoped_unsynced(&db, repo);
        let summary = perform_sync(&mut db, repo, "origin", &key, &salt, &m, sessions).unwrap();
        assert_eq!(summary.pushed, 1, "only the in-scope session is pushed");

        // The remote ref is created fresh by the push.
        assert!(gitref::remote_ref_exists(repo, "origin", SESSIONS_REF).unwrap());

        // Refresh the tracking ref so the pushed remote tree can be inspected.
        gitref::fetch(repo, "origin", SESSIONS_REF).unwrap();

        // The in-scope session is present in the new local ref and on the remote,
        // while the out-of-scope local-only artifacts are gone from both.
        let tracking = gitref::tracking_ref_name("origin", SESSIONS_REF).unwrap();
        for reference in [SESSIONS_REF, tracking.as_str()] {
            let entries = gitref::read_tree(repo, reference).unwrap();
            assert!(
                entries
                    .iter()
                    .any(|e| e.path == format!("sessions/{in_scope}.enc")),
                "the in-scope session must be present in {reference}"
            );
            assert!(
                !entries
                    .iter()
                    .any(|e| e.path == format!("sessions/{out_of_scope}.enc")),
                "out-of-scope .enc must not survive the no-remote sync in {reference}"
            );
            assert!(
                !entries
                    .iter()
                    .any(|e| e.path == format!("sessions/{out_of_scope}.meta.json")),
                "out-of-scope .meta must not survive the no-remote sync in {reference}"
            );
            // The store metadata still lands in the tree in the no-remote case.
            assert!(
                entries.iter().any(|e| e.path == "meta/salt"),
                "meta/salt must be present in {reference}"
            );
            assert!(
                entries.iter().any(|e| e.path == "meta/machines.json"),
                "meta/machines.json must be present in {reference}"
            );
        }

        // A clean tip tree is not enough: the pushed commit must not descend from
        // the pre-scoping local commit, or the out-of-scope blobs would still
        // reach the remote through ancestry (e.g. `refs/lore/sessions^`). Assert
        // the no-remote commit is an ORPHAN on both the local ref and the remote:
        // exactly one commit is reachable, so it has no parent.
        for (label, dir) in [("local", repo), ("remote", remote_dir.path())] {
            assert_eq!(
                git_out(dir, &["rev-list", "--count", SESSIONS_REF]),
                "1",
                "the no-remote sync commit must be an orphan on the {label} ref"
            );
        }

        // The out-of-scope blob object must not be reachable from the pushed ref
        // through any commit in its history. `git rev-list --objects` enumerates
        // every object reachable from the ref, so the leaked blob oid and its path
        // must both be absent.
        for (label, dir) in [("local", repo), ("remote", remote_dir.path())] {
            let objects = git_out(dir, &["rev-list", "--objects", SESSIONS_REF]);
            assert!(
                !objects.contains(&leaked_enc),
                "out-of-scope blob object must not be reachable from the {label} ref"
            );
            assert!(
                !objects.contains(&format!("sessions/{out_of_scope}.enc")),
                "out-of-scope .enc path must not be reachable from the {label} ref"
            );
        }
    }

    // ==================== global store ====================

    /// Initializes a managed global-store repo in a temp dir wired to `remote_url`.
    ///
    /// Mirrors the real `~/.lore/sync` setup ([`ensure_global_repo`]) but on an
    /// injectable path so the user's real global store is never touched.
    fn init_global_store(remote_url: &str) -> (TempDir, PathBuf) {
        let dir = tempfile::tempdir().unwrap();
        let store = dir.path().to_path_buf();
        ensure_global_repo(&store, remote_url).unwrap();
        (dir, store)
    }

    #[test]
    fn test_global_create_store_writes_salt_and_pushes() {
        let (_remote_dir, remote_url) = init_bare_remote();
        let (_store_dir, store) = init_global_store(&remote_url);

        let (keystore, _kd) = test_keystore();
        let m = machine("machine-a", "Machine A");

        create_store(
            &store,
            GLOBAL_REMOTE,
            &keystore,
            &m,
            "correct horse battery",
        )
        .unwrap();

        // The local ref exists and holds the salt.
        assert!(gitref::ref_exists(&store, SESSIONS_REF).unwrap());
        let salt = read_store_salt(&store, GLOBAL_REMOTE)
            .unwrap()
            .expect("salt written");
        // The derived key is stored under the salt-derived store id.
        assert!(keystore
            .load_key(&store_id_from_salt(&salt))
            .unwrap()
            .is_some());
        // The store was pushed to the global remote.
        assert!(gitref::remote_ref_exists(&store, GLOBAL_REMOTE, SESSIONS_REF).unwrap());
    }

    #[test]
    fn test_ensure_global_repo_is_idempotent_and_updates_remote() {
        let (_remote_dir, remote_url) = init_bare_remote();
        let dir = tempfile::tempdir().unwrap();
        let store = dir.path().join("sync");

        // First call creates the repo and configures origin.
        ensure_global_repo(&store, &remote_url).unwrap();
        assert!(store.join(".git").exists());
        assert_eq!(
            git_out(&store, &["remote", "get-url", "origin"]),
            remote_url
        );
        assert_eq!(git_out(&store, &["config", "user.name"]), "lore");

        // A second call with a new URL updates the remote without failing.
        let (_remote_dir2, remote_url2) = init_bare_remote();
        ensure_global_repo(&store, &remote_url2).unwrap();
        assert_eq!(
            git_out(&store, &["remote", "get-url", "origin"]),
            remote_url2
        );
    }

    #[test]
    fn test_global_sync_round_trip_between_machines() {
        let (_remote_dir, remote_url) = init_bare_remote();
        let passphrase = "shared global passphrase";

        // Machine A: set up the global store, seed sessions in UNRELATED
        // directories (the global store aggregates across repos), and sync.
        let (_store_a, store_a) = init_global_store(&remote_url);
        let (keystore_a, _ka) = test_keystore();
        let ma = machine("machine-a", "Machine A");
        create_store(&store_a, GLOBAL_REMOTE, &keystore_a, &ma, passphrase).unwrap();

        let (mut db_a, _da) = open_db();
        let id_one = seed_full_session(&mut db_a, "machine-a", "/projects/repo-one");
        let id_two = seed_full_session(&mut db_a, "machine-a", "/elsewhere/repo-two");
        let (key_a, salt_a) = load_store_credentials(&store_a, GLOBAL_REMOTE, &keystore_a).unwrap();
        let sessions_a = db_a.get_unsynced_global_sessions().unwrap();
        assert_eq!(sessions_a.len(), 2);
        let summary_a = perform_sync_in_store(
            SyncStore::Global,
            &mut db_a,
            &store_a,
            GLOBAL_REMOTE,
            &key_a,
            &salt_a,
            &ma,
            sessions_a,
        )
        .unwrap();
        assert_eq!(summary_a.pushed, 2, "global sync pushes all sessions");

        // Both sessions are now marked synced on the global track.
        assert!(db_a.get_unsynced_global_sessions().unwrap().is_empty());

        // Machine B: join the global store and sync (pull).
        let (_store_b, store_b) = init_global_store(&remote_url);
        let (keystore_b, _kb) = test_keystore();
        let mb = machine("machine-b", "Machine B");
        gitref::fetch(&store_b, GLOBAL_REMOTE, SESSIONS_REF).unwrap();
        let salt_b = read_store_salt(&store_b, GLOBAL_REMOTE).unwrap().unwrap();
        join_store(
            &store_b,
            GLOBAL_REMOTE,
            &keystore_b,
            &mb,
            &salt_b,
            passphrase,
        )
        .unwrap();

        let (mut db_b, _db) = open_db();
        let (key_b, salt_b2) =
            load_store_credentials(&store_b, GLOBAL_REMOTE, &keystore_b).unwrap();
        let sessions_b = db_b.get_unsynced_global_sessions().unwrap();
        let summary_b = perform_sync_in_store(
            SyncStore::Global,
            &mut db_b,
            &store_b,
            GLOBAL_REMOTE,
            &key_b,
            &salt_b2,
            &mb,
            sessions_b,
        )
        .unwrap();
        assert_eq!(summary_b.pulled, 2, "both sessions must be pulled");

        // Machine B has both sessions with their full reasoning records.
        for id in [id_one, id_two] {
            assert!(
                db_b.get_session(&id).unwrap().is_some(),
                "session {id} must be pulled"
            );
            assert_eq!(db_b.get_messages(&id).unwrap().len(), 1);
            assert_eq!(db_b.get_links_by_session(&id).unwrap().len(), 1);
        }
    }

    #[test]
    fn test_global_sync_pushes_all_sessions_regardless_of_directory() {
        // The global store pushes every unsynced-global session no matter its
        // working directory, in contrast to the repo-scoped per-repo path which
        // would push none of these (none live inside the store repo).
        let (_remote_dir, remote_url) = init_bare_remote();
        let (_store_dir, store) = init_global_store(&remote_url);

        let (keystore, _kd) = test_keystore();
        let m = machine("machine-a", "Machine A");
        create_store(&store, GLOBAL_REMOTE, &keystore, &m, "passphrase abcdefgh").unwrap();

        let (mut db, _dd) = open_db();
        let id_a = seed_full_session(&mut db, "machine-a", "/somewhere/project-a");
        let id_b = seed_full_session(&mut db, "machine-a", "/totally/other/project-b");

        // The per-repo scope for the store repo captures neither session.
        assert!(
            db.get_unsynced_sessions_for_repo(&store)
                .unwrap()
                .is_empty(),
            "no session lives inside the store repo, so per-repo scope is empty"
        );

        let (key, salt) = load_store_credentials(&store, GLOBAL_REMOTE, &keystore).unwrap();
        let sessions = db.get_unsynced_global_sessions().unwrap();
        let summary = perform_sync_in_store(
            SyncStore::Global,
            &mut db,
            &store,
            GLOBAL_REMOTE,
            &key,
            &salt,
            &m,
            sessions,
        )
        .unwrap();
        assert_eq!(summary.pushed, 2, "global sync pushes both sessions");

        // Both encrypted blobs are present in the store.
        let entries = gitref::read_tree(&store, SESSIONS_REF).unwrap();
        for id in [id_a, id_b] {
            assert!(
                entries
                    .iter()
                    .any(|e| e.path == format!("sessions/{id}.enc")),
                "session {id} must be stored in the global store"
            );
        }
    }

    #[test]
    fn test_per_repo_sync_does_not_mark_global_track() {
        // A per-repo sync marks only the synced_at track; the session stays
        // pending for the global store (and vice versa is covered by the DB tests).
        let (_remote_dir, remote_url) = init_bare_remote();
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path();
        init_repo(repo);
        git(repo, &["remote", "add", "origin", &remote_url]);

        let (keystore, _kd) = test_keystore();
        let m = machine("machine-a", "Machine A");
        create_store(repo, "origin", &keystore, &m, "passphrase abcdefgh").unwrap();

        let (mut db, _dd) = open_db();
        let id = seed_full_session(&mut db, "machine-a", &repo_dir(repo));
        let (key, salt) = load_store_credentials(repo, "origin", &keystore).unwrap();
        let sessions = scoped_unsynced(&db, repo);
        perform_sync(&mut db, repo, "origin", &key, &salt, &m, sessions).unwrap();

        // Per-repo track is synced, global track is still pending.
        assert!(
            !db.get_unsynced_sessions()
                .unwrap()
                .iter()
                .any(|s| s.id == id),
            "per-repo sync must mark the per-repo track"
        );
        assert!(
            db.get_unsynced_global_sessions()
                .unwrap()
                .iter()
                .any(|s| s.id == id),
            "per-repo sync must NOT mark the global track"
        );
    }

    #[test]
    fn test_global_sync_does_not_mark_per_repo_track() {
        // The reverse direction at the sync level: a global sync marks only the
        // global track, leaving the per-repo track pending.
        let (_remote_dir, remote_url) = init_bare_remote();
        let (_store_dir, store) = init_global_store(&remote_url);

        let (keystore, _kd) = test_keystore();
        let m = machine("machine-a", "Machine A");
        create_store(&store, GLOBAL_REMOTE, &keystore, &m, "passphrase abcdefgh").unwrap();

        let (mut db, _dd) = open_db();
        let id = seed_full_session(&mut db, "machine-a", "/some/project");
        let (key, salt) = load_store_credentials(&store, GLOBAL_REMOTE, &keystore).unwrap();
        let sessions = db.get_unsynced_global_sessions().unwrap();
        perform_sync_in_store(
            SyncStore::Global,
            &mut db,
            &store,
            GLOBAL_REMOTE,
            &key,
            &salt,
            &m,
            sessions,
        )
        .unwrap();

        assert!(
            !db.get_unsynced_global_sessions()
                .unwrap()
                .iter()
                .any(|s| s.id == id),
            "global sync must mark the global track"
        );
        assert!(
            db.get_unsynced_sessions()
                .unwrap()
                .iter()
                .any(|s| s.id == id),
            "global sync must NOT mark the per-repo track"
        );
    }

    // ==================== tombstones ====================

    /// Sets up a per-repo store on a fresh remote and returns (remote, url).
    ///
    /// Kept alive by the caller so the bare remote directory is not dropped.
    fn setup_repo_with_store(
        remote_url: &str,
        passphrase: &str,
    ) -> (TempDir, PathBuf, KeyStore, TempDir, MachineIdentity) {
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path().to_path_buf();
        init_repo(&repo);
        git(&repo, &["remote", "add", "origin", remote_url]);
        let (keystore, kd) = test_keystore();
        let m = machine("machine-a", "Machine A");
        create_store(&repo, "origin", &keystore, &m, passphrase).unwrap();
        (dir, repo, keystore, kd, m)
    }

    #[test]
    fn test_link_deletion_propagates_via_tombstone() {
        // Machine A deletes a link and syncs. Machine B (which has that link)
        // syncs and the link is removed, and a further sync does not resurrect it.
        let (_remote_dir, remote_url) = init_bare_remote();
        let passphrase = "shared team passphrase";

        // Machine A: set up, seed a session with a link, sync.
        let (_da, repo_a, keystore_a, _ka, ma) = setup_repo_with_store(&remote_url, passphrase);
        let (mut db_a, _dba) = open_db();
        let session_id = seed_full_session(&mut db_a, "machine-a", &repo_dir(&repo_a));
        let (key_a, salt_a) = load_store_credentials(&repo_a, "origin", &keystore_a).unwrap();
        let sessions_a = scoped_unsynced(&db_a, &repo_a);
        perform_sync(
            &mut db_a, &repo_a, "origin", &key_a, &salt_a, &ma, sessions_a,
        )
        .unwrap();

        // Machine B: join, sync (pull the session and its link).
        let dir_b = tempfile::tempdir().unwrap();
        let repo_b = dir_b.path();
        init_repo(repo_b);
        git(repo_b, &["remote", "add", "origin", &remote_url]);
        let (keystore_b, _kb) = test_keystore();
        let mb = machine("machine-b", "Machine B");
        gitref::fetch(repo_b, "origin", SESSIONS_REF).unwrap();
        let salt_b = read_store_salt(repo_b, "origin").unwrap().unwrap();
        join_store(repo_b, "origin", &keystore_b, &mb, &salt_b, passphrase).unwrap();
        let (mut db_b, _dbb) = open_db();
        let (key_b, salt_b2) = load_store_credentials(repo_b, "origin", &keystore_b).unwrap();
        let sessions_b = scoped_unsynced(&db_b, repo_b);
        perform_sync(
            &mut db_b, repo_b, "origin", &key_b, &salt_b2, &mb, sessions_b,
        )
        .unwrap();
        assert_eq!(
            db_b.get_links_by_session(&session_id).unwrap().len(),
            1,
            "machine B must have pulled the link"
        );

        // Machine A: delete the link (records a tombstone) and sync.
        assert!(db_a
            .delete_link_by_session_and_commit(&session_id, "deadbeef")
            .unwrap());
        let sessions_a = scoped_unsynced(&db_a, &repo_a);
        perform_sync(
            &mut db_a, &repo_a, "origin", &key_a, &salt_a, &ma, sessions_a,
        )
        .unwrap();

        // Machine B: sync. The tombstone removes the link locally.
        let sessions_b = scoped_unsynced(&db_b, repo_b);
        perform_sync(
            &mut db_b, repo_b, "origin", &key_b, &salt_b2, &mb, sessions_b,
        )
        .unwrap();
        assert!(
            db_b.get_links_by_session(&session_id).unwrap().is_empty(),
            "the deleted link must be removed on machine B"
        );

        // A subsequent sync must not resurrect it.
        let sessions_b = scoped_unsynced(&db_b, repo_b);
        perform_sync(
            &mut db_b, repo_b, "origin", &key_b, &salt_b2, &mb, sessions_b,
        )
        .unwrap();
        assert!(
            db_b.get_links_by_session(&session_id).unwrap().is_empty(),
            "the deleted link must stay removed after another sync"
        );
    }

    #[test]
    fn test_concurrent_add_survives_remote_deletion() {
        // A deletes link X while B adds a different link Y to the same session.
        // After both sync, X is gone everywhere but Y survives everywhere. Uses
        // the global store so both machines can push the shared session (the
        // per-repo store scopes pushes by working directory, which differs per
        // temp repo, so it cannot model a two-way concurrent edit in a test).
        let (_remote_dir, remote_url) = init_bare_remote();
        let passphrase = "shared global passphrase";

        // A: set up the global store, seed a session with link X, sync.
        let (_store_a, store_a) = init_global_store(&remote_url);
        let (keystore_a, _ka) = test_keystore();
        let ma = machine("machine-a", "Machine A");
        create_store(&store_a, GLOBAL_REMOTE, &keystore_a, &ma, passphrase).unwrap();
        let (mut db_a, _dba) = open_db();
        let session_id = seed_full_session(&mut db_a, "machine-a", "/projects/repo-one");
        let (key_a, salt_a) = load_store_credentials(&store_a, GLOBAL_REMOTE, &keystore_a).unwrap();
        let run_a = |db: &mut Database| {
            let sessions = db.get_unsynced_global_sessions().unwrap();
            perform_sync_in_store(
                SyncStore::Global,
                db,
                &store_a,
                GLOBAL_REMOTE,
                &key_a,
                &salt_a,
                &ma,
                sessions,
            )
            .unwrap();
        };
        run_a(&mut db_a);

        // B: join and sync (has X).
        let (_store_b, store_b) = init_global_store(&remote_url);
        let (keystore_b, _kb) = test_keystore();
        let mb = machine("machine-b", "Machine B");
        gitref::fetch(&store_b, GLOBAL_REMOTE, SESSIONS_REF).unwrap();
        let salt_b = read_store_salt(&store_b, GLOBAL_REMOTE).unwrap().unwrap();
        join_store(
            &store_b,
            GLOBAL_REMOTE,
            &keystore_b,
            &mb,
            &salt_b,
            passphrase,
        )
        .unwrap();
        let (mut db_b, _dbb) = open_db();
        let (key_b, salt_b2) =
            load_store_credentials(&store_b, GLOBAL_REMOTE, &keystore_b).unwrap();
        let run_b = |db: &mut Database| {
            let sessions = db.get_unsynced_global_sessions().unwrap();
            perform_sync_in_store(
                SyncStore::Global,
                db,
                &store_b,
                GLOBAL_REMOTE,
                &key_b,
                &salt_b2,
                &mb,
                sessions,
            )
            .unwrap();
        };
        run_b(&mut db_b);

        // A: delete X, sync.
        assert!(db_a
            .delete_link_by_session_and_commit(&session_id, "deadbeef")
            .unwrap());
        run_a(&mut db_a);

        // B: add a DIFFERENT link Y, then sync.
        let y_id = Uuid::new_v4();
        db_b.insert_link(&SessionLink {
            id: y_id,
            session_id,
            link_type: LinkType::Commit,
            commit_sha: Some("feedface".to_string()),
            branch: Some("main".to_string()),
            remote: Some("origin".to_string()),
            created_at: Utc::now(),
            created_by: LinkCreator::User,
            confidence: Some(0.9),
        })
        .unwrap();
        run_b(&mut db_b);

        // B has only Y now (X removed by the tombstone).
        let b_links = db_b.get_links_by_session(&session_id).unwrap();
        assert_eq!(b_links.len(), 1, "B keeps only its own new link");
        assert_eq!(b_links[0].id, y_id);

        // A: sync again to pull Y.
        run_a(&mut db_a);
        let a_links = db_a.get_links_by_session(&session_id).unwrap();
        assert_eq!(a_links.len(), 1, "A gets the concurrently added link");
        assert_eq!(a_links[0].id, y_id, "X stays deleted, Y propagates to A");
    }

    #[test]
    fn test_tombstone_suppresses_stale_remote_blob() {
        // Directly importing an older remote blob that still contains a
        // tombstoned child must not re-add it. This is the resurrection path a
        // tombstone must close even when the parent session blob is stale.
        let (_remote_dir, remote_url) = init_bare_remote();
        let (_da, repo, keystore, _kd, _m) =
            setup_repo_with_store(&remote_url, "passphrase abcdefgh");
        let (key, _salt) = load_store_credentials(&repo, "origin", &keystore).unwrap();

        let (mut db, _dbd) = open_db();
        // A session with a link, then delete the link (records the tombstone).
        let session_id = seed_full_session(&mut db, "machine-a", &repo_dir(&repo));
        let link = db.get_links_by_session(&session_id).unwrap()[0].clone();
        assert!(db
            .delete_link_by_session_and_commit(&session_id, "deadbeef")
            .unwrap());

        // Build a stale remote blob that still holds the deleted link.
        let session = db.get_session(&session_id).unwrap().unwrap();
        let record = SessionRecord {
            session,
            messages: vec![],
            links: vec![link],
            tags: vec![],
            annotations: vec![],
            summary: None,
        };
        let blob = encrypt_session_record(&record, &key).unwrap();
        let sha = gitref::write_blob(&repo, &blob).unwrap();
        let entries = vec![TreeEntry {
            mode: "100644".to_string(),
            sha,
            path: format!("sessions/{session_id}.enc"),
        }];

        // Merging the stale blob must not resurrect the tombstoned link.
        merge_remote(&mut db, &repo, &entries, &key).unwrap();
        assert!(
            db.get_links_by_session(&session_id).unwrap().is_empty(),
            "a stale remote blob must not resurrect a tombstoned link"
        );
    }

    #[test]
    fn test_old_remote_tombstone_suppresses_stale_blob_through_sync() {
        // The exact resurrection repro from the review: the remote store holds a
        // tombstone older than the former 90-day GC window alongside a stale
        // session blob that still contains the deleted child. A fresh machine
        // must keep the child deleted after a full sync. The sync path no longer
        // prunes tombstones, so the old tombstone survives the union and
        // suppresses the stale blob during the merge instead of being dropped
        // first (which would resurrect the child).
        let (_remote_dir, remote_url) = init_bare_remote();
        let passphrase = "shared team passphrase";

        // Machine A: set up, seed a session with a link, sync so the remote
        // holds the session blob (still containing the link).
        let (_da, repo_a, keystore_a, _ka, ma) = setup_repo_with_store(&remote_url, passphrase);
        let (mut db_a, _dba) = open_db();
        let session_id = seed_full_session(&mut db_a, "machine-a", &repo_dir(&repo_a));
        let link = db_a.get_links_by_session(&session_id).unwrap()[0].clone();
        let (key_a, salt_a) = load_store_credentials(&repo_a, "origin", &keystore_a).unwrap();
        let sessions_a = scoped_unsynced(&db_a, &repo_a);
        perform_sync(
            &mut db_a, &repo_a, "origin", &key_a, &salt_a, &ma, sessions_a,
        )
        .unwrap();

        // Machine A: record a tombstone for the link backdated well past the
        // former 90-day window, then sync again. The session blob is already
        // synced so it is carried forward unchanged (still holding the link),
        // making the remote store deliberately stale. Only the tombstone blob is
        // written.
        db_a.add_tombstones(&[Tombstone {
            child_id: link.id.to_string(),
            kind: "link".to_string(),
            session_id: Some(session_id.to_string()),
            deleted_at: Utc::now() - chrono::Duration::days(120),
        }])
        .unwrap();
        let sessions_a = scoped_unsynced(&db_a, &repo_a);
        perform_sync(
            &mut db_a, &repo_a, "origin", &key_a, &salt_a, &ma, sessions_a,
        )
        .unwrap();

        // Machine B: fresh clone, join, sync. It fetches the stale session blob
        // and the 120-day-old tombstone together. The old tombstone must
        // suppress the stale blob so the link is never resurrected.
        let dir_b = tempfile::tempdir().unwrap();
        let repo_b = dir_b.path();
        init_repo(repo_b);
        git(repo_b, &["remote", "add", "origin", &remote_url]);
        let (keystore_b, _kb) = test_keystore();
        let mb = machine("machine-b", "Machine B");
        gitref::fetch(repo_b, "origin", SESSIONS_REF).unwrap();
        let salt_b = read_store_salt(repo_b, "origin").unwrap().unwrap();
        join_store(repo_b, "origin", &keystore_b, &mb, &salt_b, passphrase).unwrap();
        let (mut db_b, _dbb) = open_db();
        let (key_b, salt_b2) = load_store_credentials(repo_b, "origin", &keystore_b).unwrap();
        let sessions_b = scoped_unsynced(&db_b, repo_b);
        perform_sync(
            &mut db_b, repo_b, "origin", &key_b, &salt_b2, &mb, sessions_b,
        )
        .unwrap();

        assert!(
            db_b.get_links_by_session(&session_id).unwrap().is_empty(),
            "an old remote tombstone must still suppress a stale session blob"
        );
    }

    #[test]
    fn test_annotation_deletion_propagates_via_tombstone_global() {
        // The same deletion-propagation guarantee holds for the global store and
        // for annotations, exercising a second store path and child kind.
        let (_remote_dir, remote_url) = init_bare_remote();
        let passphrase = "shared global passphrase";

        // Machine A: set up the global store, seed a session, sync.
        let (_store_a, store_a) = init_global_store(&remote_url);
        let (keystore_a, _ka) = test_keystore();
        let ma = machine("machine-a", "Machine A");
        create_store(&store_a, GLOBAL_REMOTE, &keystore_a, &ma, passphrase).unwrap();
        let (mut db_a, _dba) = open_db();
        let session_id = seed_full_session(&mut db_a, "machine-a", "/projects/repo-one");
        let annotation_id = db_a.get_annotations(&session_id).unwrap()[0].id;
        let (key_a, salt_a) = load_store_credentials(&store_a, GLOBAL_REMOTE, &keystore_a).unwrap();
        let run_a = |db: &mut Database| {
            let sessions = db.get_unsynced_global_sessions().unwrap();
            perform_sync_in_store(
                SyncStore::Global,
                db,
                &store_a,
                GLOBAL_REMOTE,
                &key_a,
                &salt_a,
                &ma,
                sessions,
            )
            .unwrap();
        };
        run_a(&mut db_a);

        // Machine B: join and sync (pull the annotation).
        let (_store_b, store_b) = init_global_store(&remote_url);
        let (keystore_b, _kb) = test_keystore();
        let mb = machine("machine-b", "Machine B");
        gitref::fetch(&store_b, GLOBAL_REMOTE, SESSIONS_REF).unwrap();
        let salt_b = read_store_salt(&store_b, GLOBAL_REMOTE).unwrap().unwrap();
        join_store(
            &store_b,
            GLOBAL_REMOTE,
            &keystore_b,
            &mb,
            &salt_b,
            passphrase,
        )
        .unwrap();
        let (mut db_b, _dbb) = open_db();
        let (key_b, salt_b2) =
            load_store_credentials(&store_b, GLOBAL_REMOTE, &keystore_b).unwrap();
        let run_b = |db: &mut Database| {
            let sessions = db.get_unsynced_global_sessions().unwrap();
            perform_sync_in_store(
                SyncStore::Global,
                db,
                &store_b,
                GLOBAL_REMOTE,
                &key_b,
                &salt_b2,
                &mb,
                sessions,
            )
            .unwrap();
        };
        run_b(&mut db_b);
        assert_eq!(
            db_b.get_annotations(&session_id).unwrap().len(),
            1,
            "machine B must have pulled the annotation"
        );

        // Machine A: delete the annotation and sync.
        assert!(db_a.delete_annotation(&annotation_id).unwrap());
        run_a(&mut db_a);

        // Machine B: sync. The tombstone removes the annotation.
        run_b(&mut db_b);
        assert!(
            db_b.get_annotations(&session_id).unwrap().is_empty(),
            "the deleted annotation must be removed on machine B via the global store"
        );

        // Not resurrected on a subsequent sync.
        run_b(&mut db_b);
        assert!(
            db_b.get_annotations(&session_id).unwrap().is_empty(),
            "the deleted annotation must stay removed"
        );
    }

    #[test]
    fn test_tombstone_blob_is_stable_across_unchanged_syncs() {
        // Once the tombstone set is written, a sync that does not change it must
        // reuse the existing content-addressed blob (no churn from re-encrypting
        // with a fresh nonce every time).
        let (_remote_dir, remote_url) = init_bare_remote();
        let (_da, repo, keystore, _kd, m) =
            setup_repo_with_store(&remote_url, "passphrase abcdefgh");
        let (key, salt) = load_store_credentials(&repo, "origin", &keystore).unwrap();

        let (mut db, _dbd) = open_db();
        let session_id = seed_full_session(&mut db, "machine-a", &repo_dir(&repo));
        let sessions = scoped_unsynced(&db, &repo);
        perform_sync(&mut db, &repo, "origin", &key, &salt, &m, sessions).unwrap();

        // Delete the link and sync so a tombstone blob is written.
        assert!(db
            .delete_link_by_session_and_commit(&session_id, "deadbeef")
            .unwrap());
        let sessions = scoped_unsynced(&db, &repo);
        perform_sync(&mut db, &repo, "origin", &key, &salt, &m, sessions).unwrap();
        let first = tombstone_blob_sha(&repo);

        // A no-op sync must not rewrite the tombstone blob.
        let sessions = scoped_unsynced(&db, &repo);
        perform_sync(&mut db, &repo, "origin", &key, &salt, &m, sessions).unwrap();
        let second = tombstone_blob_sha(&repo);
        assert_eq!(first, second, "unchanged tombstone blob must be reused");
    }

    /// Returns the blob SHA of `meta/tombstones` in the local ref.
    fn tombstone_blob_sha(repo: &Path) -> String {
        let entries = gitref::read_tree(repo, SESSIONS_REF).unwrap();
        entries
            .iter()
            .find(|e| e.path == TOMBSTONES_PATH)
            .expect("a tombstone blob should exist")
            .sha
            .clone()
    }
}