nabu-core 0.1.1

Core storage, indexing, and search for nabu: append-only JSONL capture and a rebuildable SQLite FTS5 index for coding-agent history.
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
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
//! Unit and integration tests for nabu-core.
//!
//! Swept verbatim from the lib.rs `mod tests` block during the module split;
//! kept as a sibling test module so lib.rs stays a thin facade.

use super::*;
#[cfg(feature = "semantic")]
use rusqlite::params;
use rusqlite::Connection;
use serde::Deserialize;
use serde_json::{json, Value};
use std::fs;
use tempfile::tempdir;

const SEMANTIC_RETRIEVAL_FIXTURE_JSON: &str =
    include_str!("../../../fixtures/semantic/retrieval.json");

#[derive(Debug, Deserialize)]
struct SemanticRetrievalFixture {
    schema_version: u32,
    tool: Tool,
    session_id: String,
    cwd: String,
    project_root: String,
    events: Vec<SemanticRetrievalEvent>,
    queries: Vec<SemanticRetrievalQuery>,
}

#[derive(Debug, Deserialize)]
struct SemanticRetrievalEvent {
    event_id: String,
    role: String,
    text: String,
}

#[derive(Debug, Deserialize)]
struct SemanticRetrievalQuery {
    query: String,
    relevant_event_ids: Vec<String>,
}

#[cfg(feature = "semantic")]
struct FakeEmbedder {
    batch_size: usize,
    intra_threads: usize,
    fail_on_call: Option<usize>,
    calls: std::cell::Cell<usize>,
}

#[cfg(feature = "semantic")]
impl FakeEmbedder {
    fn new(batch_size: usize, intra_threads: usize, fail_on_call: Option<usize>) -> Self {
        Self {
            batch_size,
            intra_threads,
            fail_on_call,
            calls: std::cell::Cell::new(0),
        }
    }
}

#[cfg(feature = "semantic")]
impl Embedder for FakeEmbedder {
    fn embed_documents(&self, documents: &[String]) -> Result<Vec<Vec<f32>>> {
        let call = self.calls.get().saturating_add(1);
        self.calls.set(call);
        if self.fail_on_call == Some(call) {
            return Err(Error::SemanticUnavailable(format!(
                "fake embed failure on call {call}"
            )));
        }
        Ok(documents
            .iter()
            .map(|document| {
                let mut vector = vec![0.0; SEMANTIC_VECTOR_DIMENSIONS];
                vector[0] = document.len().max(1) as f32;
                vector
            })
            .collect())
    }

    fn embed_query(&self, _query: &str) -> Result<Vec<f32>> {
        let mut vector = vec![0.0; SEMANTIC_VECTOR_DIMENSIONS];
        vector[0] = 1.0;
        Ok(vector)
    }

    fn document_batch_size(&self) -> usize {
        self.batch_size
    }

    fn intra_threads(&self) -> usize {
        self.intra_threads
    }
}

#[test]
fn envelope_validation_rejects_invalid_enum_values() {
    for (field, value) in [
        ("tool", "bad-tool"),
        ("source", "bad-source"),
        ("canonical_type", "bad.type"),
    ] {
        let mut envelope = valid_envelope_json();
        envelope[field] = json!(value);

        let result = serde_json::from_value::<EventEnvelope>(envelope);
        assert!(result.is_err(), "{field} should reject {value}");
    }
}

#[test]
fn envelope_validation_rejects_mismatched_filename_session_id() {
    let mut envelope: EventEnvelope = serde_json::from_value(valid_envelope_json()).unwrap();
    envelope.filename_session_id = "wrong".to_string();

    assert!(envelope.validate().is_err());
}

fn purge_opts(keep_model: bool, keep_config: bool, dry_run: bool) -> PurgeAllOptions {
    PurgeAllOptions {
        keep_model,
        keep_config,
        dry_run,
    }
}

#[test]
fn purge_all_dry_run_removes_nothing() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    fs::write(home.join("raw/claude/x.jsonl"), b"{}\n").unwrap();

    let report = purge_all(&home, purge_opts(false, false, true)).unwrap();

    assert!(report.dry_run);
    assert!(home.join("raw").is_dir(), "dry run must not delete raw");
    assert!(home.join("index").is_dir());
    assert!(home.join("config.toml").is_file());
    assert!(report.authoritative_in_scope);
    assert_eq!(report.bytes_reclaimed, 0);
    assert!(report.bytes_in_scope > 0);
    let raw = report.artifacts.iter().find(|a| a.name == "raw").unwrap();
    assert_eq!(raw.action, PurgeAction::WouldRemove);
    assert_eq!(raw.tier, PurgeTier::Authoritative);
}

#[test]
fn purge_all_removes_known_artifacts_but_keeps_home_and_foreign_files() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    fs::write(home.join("NOTES.txt"), b"keep me").unwrap();

    let report = purge_all(&home, purge_opts(false, false, false)).unwrap();

    assert!(home.is_dir(), "home directory itself must remain");
    for gone in crate::purge::PURGE_KNOWN_ENTRIES {
        assert!(!home.join(gone).exists(), "{gone} should be removed");
    }
    assert!(
        home.join("NOTES.txt").is_file(),
        "foreign files must be left untouched"
    );
    assert!(report
        .unknown_entries
        .iter()
        .any(|p| p.ends_with("NOTES.txt")));
    assert!(report.authoritative_in_scope);
    assert_eq!(
        report
            .artifacts
            .iter()
            .find(|a| a.name == "raw")
            .unwrap()
            .action,
        PurgeAction::Removed
    );
    assert!(report.bytes_reclaimed > 0);
}

#[test]
fn purge_all_keep_model_and_config_preserves_them() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    fs::write(home.join("models/model.bin"), b"weights").unwrap();

    let report = purge_all(&home, purge_opts(true, true, false)).unwrap();

    assert!(home.join("models").is_dir(), "models kept");
    assert!(home.join("models/model.bin").is_file());
    assert!(home.join("config.toml").is_file(), "config kept");
    assert!(!home.join("raw").exists());
    assert!(!home.join("index").exists());
    assert_eq!(
        report
            .artifacts
            .iter()
            .find(|a| a.name == "models")
            .unwrap()
            .action,
        PurgeAction::Preserved
    );
    assert_eq!(
        report
            .artifacts
            .iter()
            .find(|a| a.name == "config.toml")
            .unwrap()
            .action,
        PurgeAction::Preserved
    );
}

#[test]
fn purge_all_refuses_non_store_directory() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("not-a-store");
    fs::create_dir_all(&home).unwrap();
    fs::write(home.join("random.txt"), b"x").unwrap();

    let err = purge_all(&home, purge_opts(false, false, true)).unwrap_err();
    assert!(matches!(err, Error::Validation(_)));
    assert!(
        home.join("random.txt").is_file(),
        "nothing removed on refusal"
    );
}

#[test]
fn purge_all_missing_home_is_idempotent_noop() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("never-created");

    let report = purge_all(&home, purge_opts(false, false, false)).unwrap();
    assert!(report.artifacts.is_empty());
    assert_eq!(report.bytes_reclaimed, 0);
    assert!(!report.authoritative_in_scope);
}

#[cfg(unix)]
#[test]
fn purge_all_removes_model_symlink_not_its_target() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    let external = temp.path().join("external-models");
    fs::create_dir_all(&external).unwrap();
    fs::write(external.join("weights.bin"), b"important").unwrap();
    fs::remove_dir_all(home.join("models")).unwrap();
    std::os::unix::fs::symlink(&external, home.join("models")).unwrap();

    let report = purge_all(&home, purge_opts(false, false, false)).unwrap();

    assert!(!home.join("models").exists(), "symlink unlinked");
    assert!(external.is_dir(), "symlink target preserved");
    assert!(
        external.join("weights.bin").is_file(),
        "target contents preserved"
    );
    assert_eq!(
        report
            .artifacts
            .iter()
            .find(|a| a.name == "models")
            .unwrap()
            .action,
        PurgeAction::Removed
    );
}

#[test]
fn init_home_creates_required_layout_and_valid_sqlite_database() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");

    let report = init_home(&home).unwrap();

    for relative in [
        ".",
        "raw",
        "raw/codex",
        "raw/claude",
        "raw/opencode",
        "spool",
        "spool/dedupe",
        "checkpoints",
        "blobs/sha256",
        "models",
        "logs",
        "backups",
    ] {
        assert!(home.join(relative).is_dir(), "{relative} should exist");
        assert_private_dir_mode(&home.join(relative));
    }
    assert_private_file_mode(&home.join("config.toml"));
    assert!(report.db_path.is_file());
    assert_private_file_mode(&report.db_path);
    if report.db_path.with_file_name("harness.db-wal").exists() {
        assert_private_file_mode(&report.db_path.with_file_name("harness.db-wal"));
    }
    if report.db_path.with_file_name("harness.db-shm").exists() {
        assert_private_file_mode(&report.db_path.with_file_name("harness.db-shm"));
    }

    let conn = Connection::open(&report.db_path).unwrap();
    let integrity: String = conn
        .query_row("PRAGMA integrity_check;", [], |row| row.get(0))
        .unwrap();
    let user_version: i64 = conn
        .query_row("PRAGMA user_version;", [], |row| row.get(0))
        .unwrap();

    assert_eq!(integrity, "ok");
    assert_eq!(user_version, 1);
    assert_eq!(opencode_server_url(&home).unwrap(), None);
}

#[test]
fn open_index_rebuilds_current_shaped_but_empty_fts_table() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "fts-recovery-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "fts-recovery-1",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "prompt": "interrupted fts rebuild recovery marker"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    let db_path = home.join("index").join("harness.db");
    {
        let conn = Connection::open(&db_path).unwrap();
        conn.execute_batch("DROP TABLE IF EXISTS events_fts;")
            .unwrap();
        conn.execute_batch(crate::db::EVENTS_FTS_SCHEMA).unwrap();
    }

    open_index(&db_path).unwrap();

    let results = search_history(&home, "interrupted fts rebuild recovery", 10).unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].session_id, "fts-recovery-session");
}

#[cfg(feature = "semantic")]
#[test]
fn semantic_feature_loads_sqlite_vec_with_bundled_rusqlite() {
    unsafe {
        rusqlite::ffi::sqlite3_auto_extension(Some(crate::db::sqlite_vec_auto_extension()));
    }

    let conn = Connection::open_in_memory().unwrap();
    let version: String = conn
        .query_row("select vec_version()", [], |row| row.get(0))
        .unwrap();
    assert!(version.starts_with('v'), "{version}");

    conn.execute(
        "create virtual table vectors using vec0(embedding float[4])",
        [],
    )
    .unwrap();
}

#[cfg(feature = "semantic")]
#[test]
fn semantic_feature_initializes_derived_vector_schema() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    let db_path = home.join("index").join("harness.db");
    let conn = open_index(&db_path).unwrap();

    assert!(table_exists(&conn, &db_path, "vector_units").unwrap());
    assert!(table_exists(&conn, &db_path, "vector_unit_texts").unwrap());
    assert!(table_exists(&conn, &db_path, "vector_unit_embeddings").unwrap());
    assert_eq!(
        table_count(&conn, &db_path, "vector_unit_embeddings").unwrap(),
        0
    );
    let version: String = conn
        .query_row("select vec_version()", [], |row| row.get(0))
        .unwrap();
    assert!(version.starts_with('v'), "{version}");

    let footprint = storage_footprint(&home);
    assert_eq!(footprint.vectors_bytes, 0);
    assert!(footprint.index_bytes > 0);
}

#[test]
fn embeddinggemma_prompt_prefixes_are_pinned() {
    assert_eq!(
        query_embedding_input("  auth bug  "),
        "task: search result | query: auth bug"
    );
    assert_eq!(
        document_embedding_input("  fixed login timeout  "),
        "title: none | text: fixed login timeout"
    );
    assert_ne!(
        query_embedding_input("same text"),
        document_embedding_input("same text")
    );
}

#[cfg(feature = "semantic")]
#[test]
fn semantic_vectors_persist_in_sqlite_vec() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    let db_path = home.join("index").join("harness.db");
    let conn = open_index(&db_path).unwrap();
    ensure_semantic_vector_schema(&conn, &db_path).unwrap();

    let mut vector = vec![0.0_f32; SEMANTIC_VECTOR_DIMENSIONS];
    vector[0] = 1.0;
    let vector_blob = vector_to_blob(&vector).unwrap();
    conn.execute(
        "INSERT INTO vector_unit_embeddings(unit_id, embedding) VALUES (?1, ?2)",
        params![1_i64, vector_blob.clone()],
    )
    .unwrap();

    let unit_id: i64 = conn
        .query_row(
            "SELECT unit_id FROM vector_unit_embeddings
                 WHERE embedding MATCH ?1 AND k = 1
                 ORDER BY distance",
            params![vector_blob],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(unit_id, 1);
    assert_eq!(storage_footprint(&home).vectors_bytes, 1024);
}

#[cfg(feature = "semantic")]
#[test]
fn semantic_index_materializes_units_without_model_or_payload_duplication() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "semantic-units",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "semantic-units-1",
            "prompt": "remember the fuzzy auth regression fix",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture"
        }),
    )
    .unwrap();

    index_once(&home).unwrap();
    let db_path = home.join("index").join("harness.db");
    let conn = open_index(&db_path).unwrap();

    assert_eq!(table_count(&conn, &db_path, "vector_units").unwrap(), 1);
    assert_eq!(
        table_count(&conn, &db_path, "vector_unit_texts").unwrap(),
        1
    );
    assert_eq!(
        table_count(&conn, &db_path, "vector_unit_embeddings").unwrap(),
        0
    );
    let payload_json: Option<String> = conn
        .query_row("SELECT payload_json FROM events LIMIT 1", [], |row| {
            row.get(0)
        })
        .unwrap();
    assert!(payload_json.is_none());
    assert!(!embedding_model_status(&home).semantic_available);
}

#[test]
fn opencode_hook_resolves_session_id_from_native_event_shapes() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    // Message/part/tool/etc. events carry top-level `sessionID`; live plugin
    // payloads may nest the object under `info`/`part`.
    let by_session_id = ingest_hook_event(
        &home,
        Tool::Opencode,
        json!({
            "hook_event_name": "message.updated",
            "id": "msg_abc",
            "sessionID": "ses_top_level",
            "role": "assistant"
        }),
    )
    .unwrap();
    assert!(by_session_id.appended);
    assert!(by_session_id
        .raw_file
        .to_string_lossy()
        .contains("ses_top_level"));

    let nested_part = ingest_hook_event(
        &home,
        Tool::Opencode,
        json!({
            "hook_event_name": "message.part.updated",
            "part": { "id": "prt_1", "sessionID": "ses_nested_part", "type": "text" }
        }),
    )
    .unwrap();
    assert!(nested_part
        .raw_file
        .to_string_lossy()
        .contains("ses_nested_part"));

    // `session.*` events have no `sessionID`; the session id is `id`.
    let session_created = ingest_hook_event(
        &home,
        Tool::Opencode,
        json!({
            "hook_event_name": "session.created",
            "id": "ses_from_id",
            "directory": "/tmp/project"
        }),
    )
    .unwrap();
    assert!(session_created
        .raw_file
        .to_string_lossy()
        .contains("ses_from_id"));
}

#[test]
fn opencode_hook_does_not_mistake_message_id_for_session_id() {
    // A non-session event with `id` but no `sessionID` must NOT fall back to
    // `id` (that would be the message id, not the session id).
    let payload = json!({
        "hook_event_name": "message.updated",
        "id": "msg_no_session"
    });
    let result = opencode_hook_session_id(&payload, "message.updated");
    assert!(matches!(result, Err(Error::Validation(_))));
}

#[test]
fn opencode_hook_rejects_event_without_resolvable_session_id() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    let result = ingest_hook_event(
        &home,
        Tool::Opencode,
        json!({ "hook_event_name": "file.edited", "filename": "src/lib.rs" }),
    );
    assert!(matches!(result, Err(Error::Validation(_))));
}

#[cfg(feature = "semantic")]
#[test]
fn semantic_index_no_embed_skips_fake_model_and_leaves_vectors_empty() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    write_fake_semantic_model_files(&home);
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "semantic-no-embed",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "semantic-no-embed-1",
            "prompt": "deferred semantic fake model marker",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture"
        }),
    )
    .unwrap();

    let report = index_once_with_options(&home, IndexOptions { embed: false }).unwrap();
    let db_path = home.join("index").join("harness.db");
    let conn = open_index(&db_path).unwrap();

    assert_eq!(report.indexed_events, 1);
    assert_eq!(
        search_history(&home, "deferred semantic fake model", 10)
            .unwrap()
            .len(),
        1
    );
    assert_eq!(table_count(&conn, &db_path, "vector_units").unwrap(), 1);
    assert_eq!(
        table_count(&conn, &db_path, "vector_unit_embeddings").unwrap(),
        0
    );
    assert!(!embedding_model_status(&home).semantic_available);
}

#[cfg(feature = "semantic")]
#[test]
fn semantic_index_discloses_unembedded_count_before_model_load() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    write_fake_semantic_model_files(&home);
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "semantic-plan",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "semantic-plan-1",
            "prompt": "semantic plan progress marker",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture"
        }),
    )
    .unwrap();

    let mut progress = Vec::new();
    let result = index_once_with_options_and_progress(&home, IndexOptions::default(), |event| {
        progress.push(event)
    });

    assert!(
        result.is_err(),
        "fake model files should make model loading fail after the plan is emitted"
    );
    assert_eq!(progress.first().unwrap().phase, "embedding_plan");
    assert_eq!(progress.first().unwrap().status, "ready");
    assert_eq!(progress.first().unwrap().total_units, 1);
    assert!(progress
        .iter()
        .any(|event| event.phase == "loading_model" && event.status == "started"));
}

#[cfg(feature = "semantic")]
#[test]
fn semantic_collect_uses_compact_unit_texts_and_backfills_legacy_rows() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "semantic-texts",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "semantic-texts-1",
            "prompt": "compact vector unit text marker",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    let db_path = home.join("index").join("harness.db");
    let conn = open_index(&db_path).unwrap();
    assert_eq!(
        table_count(&conn, &db_path, "vector_unit_texts").unwrap(),
        1
    );
    conn.execute("DELETE FROM vector_unit_texts", []).unwrap();
    assert_eq!(
        table_count(&conn, &db_path, "vector_unit_texts").unwrap(),
        0
    );

    let units = collect_unembedded_units(&conn, &db_path).unwrap();
    assert_eq!(units.len(), 1);
    assert!(units[0].text.contains("compact vector unit text marker"));
    assert_eq!(
        table_count(&conn, &db_path, "vector_unit_texts").unwrap(),
        1
    );
}

#[cfg(feature = "semantic")]
#[test]
fn semantic_index_does_not_load_fake_model_when_no_units_need_embedding() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    write_fake_semantic_model_files(&home);

    let mut progress = Vec::new();
    let embedded =
        embed_index_if_available_with_progress(&home, |event| progress.push(event)).unwrap();

    assert_eq!(embedded, 0);
    assert!(progress.is_empty());
}

#[cfg(feature = "semantic")]
#[test]
fn semantic_model_download_is_noop_when_cache_is_complete() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    write_fake_semantic_model_files(&home);

    let mut progress = Vec::new();
    let report = download_embedding_model_with_progress(&home, SEMANTIC_MODEL_ID, |event| {
        progress.push(event)
    })
    .unwrap();

    assert!(progress.is_empty());
    assert_eq!(report.downloaded_files, 0);
    assert_eq!(report.total_files, SEMANTIC_MODEL_REMOTE_FILES.len());
    assert!(report.on_disk_bytes > 0);
}

#[cfg(feature = "semantic")]
#[test]
fn semantic_embedding_batches_by_length_and_streams_progress() {
    let mut units = vec![
        UnembeddedUnit {
            unit_id: 2,
            text: "x ".repeat(200),
            estimated_tokens: 200,
        },
        UnembeddedUnit {
            unit_id: 3,
            text: "tiny".to_string(),
            estimated_tokens: estimated_embedding_token_count("tiny"),
        },
        UnembeddedUnit {
            unit_id: 1,
            text: "short unit".to_string(),
            estimated_tokens: estimated_embedding_token_count("short unit"),
        },
    ];
    bucket_unembedded_units(&mut units);
    assert_eq!(
        units.iter().map(|unit| unit.unit_id).collect::<Vec<_>>(),
        vec![1, 3, 2]
    );

    let progress = embedding_index_progress(
        "embedding",
        "running",
        50,
        100,
        Instant::now() - StdDuration::from_secs(2),
        &FakeEmbedder::new(64, 8, None),
        2048,
    );
    assert_eq!(progress.batch_size, 64);
    assert_eq!(progress.write_chunk_size, 2048);
    assert_eq!(progress.intra_threads, 8);
    assert!(progress.units_per_second > 0.0);
    assert!(progress.eta_seconds.is_some());
}

#[cfg(feature = "semantic")]
#[test]
fn semantic_embedding_writes_commit_in_resumable_chunks() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    for index in 0..3 {
        ingest_hook_event(
            &home,
            Tool::Claude,
            json!({
                "session_id": "semantic-resume",
                "hook_event_name": "UserPromptSubmit",
                "message_id": format!("semantic-resume-{index}"),
                "sequence": index,
                "prompt": format!("semantic resumable embedding unit {index}"),
                "cwd": "/tmp/nabu-fixture",
                "project_root": "/tmp/nabu-fixture"
            }),
        )
        .unwrap();
    }
    index_once(&home).unwrap();

    let db_path = home.join("index").join("harness.db");
    let mut conn = open_index(&db_path).unwrap();
    assert_eq!(table_count(&conn, &db_path, "vector_units").unwrap(), 3);

    let failing = FakeEmbedder::new(1, 4, Some(3));
    let mut failed_progress = Vec::new();
    let result = embed_unembedded_units_with_config(
        &mut conn,
        &db_path,
        &failing,
        EmbeddingWriteConfig {
            write_chunk_size: 2,
        },
        |event| failed_progress.push(event),
    );
    assert!(result.is_err());
    assert_eq!(
        table_count(&conn, &db_path, "vector_unit_embeddings").unwrap(),
        2
    );
    assert_eq!(failed_progress.first().unwrap().status, "started");

    let succeeding = FakeEmbedder::new(1, 4, None);
    let mut resumed_progress = Vec::new();
    let embedded = embed_unembedded_units_with_config(
        &mut conn,
        &db_path,
        &succeeding,
        EmbeddingWriteConfig {
            write_chunk_size: 2,
        },
        |event| resumed_progress.push(event),
    )
    .unwrap();
    assert_eq!(embedded, 1);
    assert_eq!(
        table_count(&conn, &db_path, "vector_unit_embeddings").unwrap(),
        3
    );
    assert_eq!(resumed_progress.first().unwrap().status, "started");
    assert_eq!(resumed_progress.last().unwrap().status, "completed");
    assert_eq!(resumed_progress.last().unwrap().embedded_units, 1);
    assert_eq!(resumed_progress.last().unwrap().total_units, 1);
}

#[cfg(feature = "semantic")]
#[test]
#[ignore = "semantic acceptance requires a local embedding model cache"]
fn semantic_acceptance_no_embed_defers_vectors_until_later_default_index() {
    let model_home = required_semantic_test_model_home();
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    attach_semantic_model_cache(&home, &model_home);
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "semantic-deferred-real-model",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "semantic-deferred-real-model-1",
            "prompt": "defer semantic embedding until the later default index pass",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture"
        }),
    )
    .unwrap();

    let first = index_once_with_options(&home, IndexOptions { embed: false }).unwrap();
    let db_path = home.join("index").join("harness.db");
    let conn = open_index(&db_path).unwrap();
    assert_eq!(first.indexed_events, 1);
    assert_eq!(table_count(&conn, &db_path, "vector_units").unwrap(), 1);
    assert_eq!(
        table_count(&conn, &db_path, "vector_unit_embeddings").unwrap(),
        0
    );
    assert!(!embedding_model_status(&home).semantic_available);

    let mut progress = Vec::new();
    let second = index_once_with_options_and_progress(&home, IndexOptions::default(), |event| {
        progress.push(event)
    })
    .unwrap();
    assert_eq!(second.indexed_events, 0);
    assert!(
        table_count(&conn, &db_path, "vector_unit_embeddings").unwrap() > 0,
        "default index should embed units deferred by --no-embed"
    );
    assert!(embedding_model_status(&home).semantic_available);
    assert_eq!(progress.first().unwrap().phase, "embedding_plan");
    assert_eq!(progress.first().unwrap().total_units, 1);
}

#[cfg(feature = "semantic")]
#[test]
#[ignore = "semantic acceptance requires a local embedding model cache"]
fn semantic_acceptance_hybrid_beats_lexical_on_labeled_retrieval_fixture() {
    let model_home = required_semantic_test_model_home();
    let fixture = semantic_retrieval_fixture();
    assert!(!fixture.events.is_empty());
    assert!(!fixture.queries.is_empty());

    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    attach_semantic_model_cache(&home, &model_home);
    seed_semantic_retrieval_fixture(&home, &fixture);
    index_once(&home).unwrap();
    assert!(embedding_model_status(&home).semantic_available);

    let k = 3usize;
    let first_results = hybrid_result_ids_by_query(&home, &fixture, k);
    let first_vectors = vector_snapshot(&home);
    assert!(!first_vectors.is_empty());

    let mut strict_wins = 0usize;
    let mut aggregate_lexical_precision = 0.0;
    let mut aggregate_hybrid_precision = 0.0;
    let mut aggregate_lexical_recall = 0.0;
    let mut aggregate_hybrid_recall = 0.0;
    for query in &fixture.queries {
        let relevant = query
            .relevant_event_ids
            .iter()
            .cloned()
            .collect::<BTreeSet<_>>();
        let lexical = result_event_ids(
            &home,
            search_history_page(
                &home,
                &query.query,
                SearchOptions {
                    mode: SearchMode::Lexical,
                    limit: k,
                    dedupe: false,
                    ..SearchOptions::default()
                },
            )
            .unwrap()
            .results,
        );
        let hybrid = result_event_ids(
            &home,
            search_history_page(
                &home,
                &query.query,
                SearchOptions {
                    mode: SearchMode::Hybrid,
                    limit: k,
                    dedupe: false,
                    ..SearchOptions::default()
                },
            )
            .unwrap()
            .results,
        );
        let lexical_precision = precision_at_k(&lexical, &relevant, k);
        let hybrid_precision = precision_at_k(&hybrid, &relevant, k);
        let lexical_recall = recall_at_k(&lexical, &relevant, k);
        let hybrid_recall = recall_at_k(&hybrid, &relevant, k);
        aggregate_lexical_precision += lexical_precision;
        aggregate_hybrid_precision += hybrid_precision;
        aggregate_lexical_recall += lexical_recall;
        aggregate_hybrid_recall += hybrid_recall;

        eprintln!(
                "semantic fixture query={:?} lexical_ids={:?} hybrid_ids={:?} precision@{} lexical={:.3} hybrid={:.3} recall@{} lexical={:.3} hybrid={:.3}",
                query.query,
                lexical,
                hybrid,
                k,
                lexical_precision,
                hybrid_precision,
                k,
                lexical_recall,
                hybrid_recall
            );
        assert!(
            hybrid_precision >= lexical_precision,
            "hybrid precision regressed for query {:?}: lexical {:?} ({:.3}), hybrid {:?} ({:.3})",
            query.query,
            lexical,
            lexical_precision,
            hybrid,
            hybrid_precision
        );
        assert!(
            hybrid_recall >= lexical_recall,
            "hybrid recall regressed for query {:?}: lexical {:?} ({:.3}), hybrid {:?} ({:.3})",
            query.query,
            lexical,
            lexical_recall,
            hybrid,
            hybrid_recall
        );
        if hybrid_precision > lexical_precision || hybrid_recall > lexical_recall {
            strict_wins += 1;
        }
    }
    let query_count = fixture.queries.len() as f64;
    eprintln!(
            "semantic fixture aggregate precision@{} lexical={:.3} hybrid={:.3} recall@{} lexical={:.3} hybrid={:.3} strict_wins={}/{}",
            k,
            aggregate_lexical_precision / query_count,
            aggregate_hybrid_precision / query_count,
            k,
            aggregate_lexical_recall / query_count,
            aggregate_hybrid_recall / query_count,
            strict_wins,
            fixture.queries.len()
        );
    assert!(
            strict_wins > 0,
            "hybrid tied lexical on every labeled semantic query; this does not prove the M5 retrieval-quality win"
        );

    remove_index_database(&home);
    index_once(&home).unwrap();
    let second_vectors = vector_snapshot(&home);
    let second_results = hybrid_result_ids_by_query(&home, &fixture, k);

    assert_eq!(first_vectors, second_vectors);
    assert_eq!(first_results, second_results);
}

fn semantic_retrieval_fixture() -> SemanticRetrievalFixture {
    serde_json::from_str(SEMANTIC_RETRIEVAL_FIXTURE_JSON)
        .expect("semantic retrieval fixture must be valid JSON")
}

#[cfg(feature = "semantic")]
fn write_fake_semantic_model_files(home: &Path) {
    let model_root = semantic_model_cache_path(home);
    for (_, local) in SEMANTIC_MODEL_REMOTE_FILES {
        let path = model_root.join(local);
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(path, b"not a real model").unwrap();
    }
}

#[cfg(feature = "semantic")]
fn required_semantic_test_model_home() -> PathBuf {
    semantic_test_model_home().expect(
        "semantic acceptance tests require NABU_SEMANTIC_MODEL_DIR or \
             NABU_SEMANTIC_TEST_HOME to point at a downloaded embeddinggemma-300m-q4 cache",
    )
}

#[cfg(feature = "semantic")]
fn semantic_test_model_home() -> Option<PathBuf> {
    if let Ok(model_dir) = std::env::var("NABU_SEMANTIC_MODEL_DIR") {
        let model_dir = PathBuf::from(model_dir);
        if semantic_model_files_present_at(&model_dir) {
            return Some(model_dir);
        }
    }

    let mut candidates = Vec::new();
    if let Ok(home) = std::env::var("NABU_SEMANTIC_TEST_HOME") {
        candidates.push(PathBuf::from(home));
    }
    if let Ok(home) = std::env::var("NABU_HOME") {
        candidates.push(PathBuf::from(home));
    }
    if let Ok(home) = resolve_home(None) {
        candidates.push(home);
    }

    candidates.into_iter().find_map(|home| {
        let cache_path = semantic_model_cache_path(&home);
        semantic_model_files_present_at(&cache_path).then_some(cache_path)
    })
}

#[cfg(feature = "semantic")]
fn semantic_model_files_present_at(cache_path: &Path) -> bool {
    SEMANTIC_MODEL_REMOTE_FILES
        .iter()
        .all(|(_, local)| cache_path.join(local).is_file())
}

#[cfg(feature = "semantic")]
fn attach_semantic_model_cache(home: &Path, source_cache_path: &Path) {
    let model_root = home.join("models");
    fs::create_dir_all(&model_root).unwrap();
    let target = semantic_model_cache_path(home);
    if target.exists() {
        return;
    }
    #[cfg(unix)]
    {
        std::os::unix::fs::symlink(source_cache_path, &target).unwrap();
    }
    #[cfg(not(unix))]
    {
        let _ = source_cache_path;
        panic!("semantic model cache symlink test requires a Unix platform");
    }
}

#[cfg(feature = "semantic")]
fn seed_semantic_retrieval_fixture(home: &Path, fixture: &SemanticRetrievalFixture) {
    for (index, event) in fixture.events.iter().enumerate() {
        let mut payload = json!({
            "session_id": fixture.session_id,
            "message_id": event.event_id,
            "cwd": fixture.cwd,
            "project_root": fixture.project_root,
        });
        match event.role.as_str() {
            "user" => {
                payload["hook_event_name"] = json!("UserPromptSubmit");
                payload["prompt"] = json!(event.text);
            }
            "assistant" => {
                payload["hook_event_name"] = json!("MessageDisplay");
                payload["text"] = json!(event.text);
                payload["index"] = json!(index as i64);
                payload["final"] = json!(true);
            }
            role => panic!("unsupported semantic fixture role: {role}"),
        }
        ingest_hook_event(home, Tool::Claude, payload).unwrap();
    }
}

#[cfg(feature = "semantic")]
fn result_event_ids(home: &Path, results: Vec<SearchResult>) -> Vec<String> {
    results
        .iter()
        .map(|result| result_event_id(home, result))
        .collect()
}

#[cfg(feature = "semantic")]
fn result_event_id(home: &Path, result: &SearchResult) -> String {
    let db_path = home.join("index").join("harness.db");
    let conn = open_index(&db_path).unwrap();
    conn.query_row(
        "SELECT COALESCE(message_id, source_event_id, CAST(id AS TEXT))
             FROM events
             WHERE tool = ?1
               AND session_id = ?2
               AND raw_file = ?3
               AND raw_line = ?4
             ORDER BY id
             LIMIT 1",
        params![
            result.tool.as_str(),
            &result.session_id,
            &result.raw_file,
            result.raw_line,
        ],
        |row| row.get(0),
    )
    .unwrap()
}

#[cfg(feature = "semantic")]
fn precision_at_k(ids: &[String], relevant: &BTreeSet<String>, k: usize) -> f64 {
    if k == 0 {
        return 0.0;
    }
    relevant_hits_at_k(ids, relevant, k) as f64 / k as f64
}

#[cfg(feature = "semantic")]
fn recall_at_k(ids: &[String], relevant: &BTreeSet<String>, k: usize) -> f64 {
    if relevant.is_empty() {
        return 0.0;
    }
    relevant_hits_at_k(ids, relevant, k) as f64 / relevant.len() as f64
}

#[cfg(feature = "semantic")]
fn relevant_hits_at_k(ids: &[String], relevant: &BTreeSet<String>, k: usize) -> usize {
    ids.iter()
        .take(k)
        .filter(|event_id| relevant.contains(*event_id))
        .count()
}

#[cfg(feature = "semantic")]
fn hybrid_result_ids_by_query(
    home: &Path,
    fixture: &SemanticRetrievalFixture,
    k: usize,
) -> Vec<Vec<String>> {
    fixture
        .queries
        .iter()
        .map(|query| {
            result_event_ids(
                home,
                search_history_page(
                    home,
                    &query.query,
                    SearchOptions {
                        mode: SearchMode::Hybrid,
                        limit: k,
                        dedupe: false,
                        ..SearchOptions::default()
                    },
                )
                .unwrap()
                .results,
            )
        })
        .collect()
}

#[cfg(feature = "semantic")]
fn vector_snapshot(home: &Path) -> Vec<(i64, Vec<u8>)> {
    let db_path = home.join("index").join("harness.db");
    let conn = open_index(&db_path).unwrap();
    let mut statement = conn
        .prepare("SELECT unit_id, embedding FROM vector_unit_embeddings ORDER BY unit_id")
        .unwrap();
    let rows = statement
        .query_map([], |row| {
            Ok((row.get::<_, i64>(0)?, row.get::<_, Vec<u8>>(1)?))
        })
        .unwrap();
    rows.map(|row| row.unwrap()).collect()
}

#[cfg(feature = "semantic")]
fn remove_index_database(home: &Path) {
    let db_path = home.join("index").join("harness.db");
    for path in [
        db_path.clone(),
        db_path.with_file_name("harness.db-wal"),
        db_path.with_file_name("harness.db-shm"),
    ] {
        match fs::remove_file(&path) {
            Ok(()) => {}
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
            Err(error) => panic!("failed to remove {}: {error}", path.display()),
        }
    }
}

#[test]
fn opencode_server_url_reads_config_toml_key() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    fs::write(
        home.join("config.toml"),
        "schema_version = 1\n\n[opencode]\nserver_url = \"http://127.0.0.1:4096\"\n",
    )
    .unwrap();

    assert_eq!(
        opencode_server_url(&home).unwrap(),
        Some("http://127.0.0.1:4096".to_string())
    );
}

#[cfg(unix)]
fn assert_private_dir_mode(path: &Path) {
    use std::os::unix::fs::PermissionsExt;

    let mode = fs::metadata(path).unwrap().permissions().mode() & 0o777;
    assert_eq!(mode, 0o700, "{} should be 0700", path.display());
}

#[cfg(not(unix))]
fn assert_private_dir_mode(_path: &Path) {}

#[cfg(unix)]
fn assert_private_file_mode(path: &Path) {
    use std::os::unix::fs::PermissionsExt;

    let mode = fs::metadata(path).unwrap().permissions().mode() & 0o777;
    assert_eq!(mode, 0o600, "{} should be 0600", path.display());
}

#[cfg(not(unix))]
fn assert_private_file_mode(_path: &Path) {}

#[test]
fn redaction_rules_match_contract_and_preserve_safe_text() {
    let fixture = include_str!("../../../fixtures/redaction/secrets.txt");
    let redacted = redact_export_text(fixture);

    for expected in [
        "[REDACTED:PRIVATE_KEY]",
        "Bearer [REDACTED:BEARER_TOKEN]",
        "[REDACTED:API_KEY]",
        "DATABASE_PASSWORD=[REDACTED:ENV_VALUE]",
    ] {
        assert!(redacted.contains(expected), "{expected}");
    }
    for secret in [
        "private-key-material",
        "abcdefghijklmnopqrstuvwxyz123456",
        "supersecretvalue",
        "AKIA1234567890ABCDEF",
    ] {
        assert!(!redacted.contains(secret), "{secret}");
    }
    assert!(redacted.contains("redaction fixture marker keeps safe surrounding text"));
    assert!(redacted.contains("redaction fixture marker keeps trailing safe text"));
}

#[test]
fn oversized_payloads_are_spilled_and_indexed_from_blob() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    let prompt =
        "oversized payload fixture marker ".repeat((MAX_INLINE_ENVELOPE_BYTES / 32) + 1024);

    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "fixture-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "oversized-payload-1",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "prompt": prompt
        }),
    )
    .unwrap();

    let raw_path = canonical_raw_path(&home, Tool::Claude, "fixture-session");
    let raw = fs::read_to_string(&raw_path).unwrap();
    let envelope: EventEnvelope = serde_json::from_str(raw.trim_end()).unwrap();
    let payload_ref = envelope.payload_ref.as_deref().unwrap();
    assert!(payload_ref.starts_with("sha256:"));
    assert!(envelope.payload.is_null());
    let hash = payload_ref.trim_start_matches("sha256:");
    assert!(home
        .join("blobs")
        .join("sha256")
        .join(format!("{hash}.json"))
        .is_file());
    assert!(raw.len() < MAX_INLINE_ENVELOPE_BYTES);

    index_once(&home).unwrap();
    let page = search_history_page(
        &home,
        "oversized payload fixture marker",
        SearchOptions {
            limit: 1,
            include_payload: true,
            dedupe: false,
            max_snippet_chars: 80,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(page.results.len(), 1);
    assert_eq!(page.results[0].session_id, "fixture-session");
    assert!(page.results[0]
        .payload
        .get("prompt")
        .and_then(Value::as_str)
        .unwrap()
        .contains("oversized payload fixture marker"));
}

#[test]
fn markdown_export_includes_sensitivity_warning() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "warning-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "warning-1",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "prompt": "markdown warning marker"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    let full = export_session_markdown_with_options(&home, Tool::Claude, "warning-session", false)
        .unwrap();
    let redacted =
        export_session_markdown_with_options(&home, Tool::Claude, "warning-session", true).unwrap();

    assert!(full.contains("Sensitivity: this export is not redacted."));
    assert!(redacted.contains("Sensitivity: redacted export."));
}

#[test]
fn raw_append_and_index_dedupe_same_native_event_across_sources() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    let payload = json!({
        "session_id": "fixture-session",
        "hook_event_name": "UserPromptSubmit",
        "event_id": "same-native-event-1",
        "cwd": "/tmp/nabu-fixture",
        "project_root": "/tmp/nabu-fixture",
        "prompt": "cross source dedupe fixture marker"
    });

    let first = ingest_hook_event(&home, Tool::Codex, payload.clone()).unwrap();
    let backfill_event = envelope_from_backfill_payload(
        Tool::Codex,
        Path::new("/tmp/codex.jsonl"),
        0,
        payload,
        &BackfillParseContext::default(),
    )
    .unwrap();
    let second = append_prepared_event(&home, backfill_event).unwrap();

    let raw =
        fs::read_to_string(canonical_raw_path(&home, Tool::Codex, "fixture-session")).unwrap();
    assert!(first.appended);
    assert!(!second.appended);
    assert_eq!(raw.lines().count(), 1);
    index_once(&home).unwrap();
    let results = search_history(&home, "cross source dedupe fixture marker", 10).unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].session_id, "fixture-session");
}

#[test]
fn raw_append_dedupes_unsequenced_event_across_observation_time_and_route() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    let hook_payload = json!({
        "session_id": "fixture-session",
        "hook_event_name": "UserPromptSubmit",
        "captured_at": "2026-06-17T12:00:59Z",
        "cwd": "/tmp/nabu-fixture",
        "project_root": "/tmp/nabu-fixture",
        "prompt": "unsequenced duplicate marker"
    });
    let backfill_payload = json!({
        "session_id": "fixture-session",
        "hook_event_name": "UserPromptSubmit",
        "captured_at": "2026-06-17T12:01:01Z",
        "cwd": "/tmp/nabu-fixture",
        "project_root": "/tmp/nabu-fixture",
        "prompt": "unsequenced duplicate marker"
    });

    let first = ingest_hook_event(&home, Tool::Claude, hook_payload).unwrap();
    let mut event = envelope_from_backfill_payload(
        Tool::Claude,
        Path::new("/tmp/claude-transcript.jsonl"),
        42,
        backfill_payload,
        &BackfillParseContext::default(),
    )
    .unwrap();
    event.sequence = None;
    let second = append_prepared_event(&home, event).unwrap();

    let raw =
        fs::read_to_string(canonical_raw_path(&home, Tool::Claude, "fixture-session")).unwrap();
    assert!(first.appended);
    assert!(!second.appended);
    assert_eq!(raw.lines().count(), 1);

    index_once(&home).unwrap();
    let results = search_history(&home, "unsequenced duplicate marker", 10).unwrap();
    assert_eq!(results.len(), 1);
}

#[test]
fn dedupe_sidecar_covers_large_session_and_self_heals() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    let session_id = "large-sidecar-session";
    let seed_events = (0..10_000)
        .map(|index| {
            envelope_from_backfill_payload(
                Tool::Claude,
                Path::new("/tmp/large-sidecar.jsonl"),
                index as u64,
                json!({
                    "session_id": session_id,
                    "hook_event_name": "UserPromptSubmit",
                    "message_id": format!("large-sidecar-{index}"),
                    "sequence": index as i64,
                    "cwd": "/tmp/nabu-fixture",
                    "project_root": "/tmp/nabu-fixture",
                    "prompt": format!("large sidecar marker {index}")
                }),
                &BackfillParseContext::default(),
            )
            .unwrap()
        })
        .collect::<Vec<_>>();

    append_prepared_events(&home, seed_events).unwrap();

    let raw_path = canonical_raw_path(&home, Tool::Claude, session_id);
    let sidecar = DedupeSidecarFiles::for_raw_file(&home, &raw_path);
    assert_eq!(raw_line_count(&raw_path), 10_000);
    assert_eq!(dedupe_sidecar_entry_count(&sidecar), 10_000);

    let duplicate = ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": session_id,
            "hook_event_name": "UserPromptSubmit",
            "message_id": "large-sidecar-1234",
            "sequence": 1234,
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "prompt": "large sidecar marker 1234"
        }),
    )
    .unwrap();
    assert!(!duplicate.appended);
    assert_eq!(duplicate.raw_offset, raw_offset_for_line(&raw_path, 1234));
    assert_eq!(raw_line_count(&raw_path), 10_000);

    fs::remove_dir_all(&sidecar.buckets_dir).unwrap();
    let duplicate_after_delete = ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": session_id,
            "hook_event_name": "UserPromptSubmit",
            "message_id": "large-sidecar-4321",
            "sequence": 4321,
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "prompt": "large sidecar marker 4321"
        }),
    )
    .unwrap();
    assert!(!duplicate_after_delete.appended);
    assert_eq!(raw_line_count(&raw_path), 10_000);
    assert_eq!(dedupe_sidecar_entry_count(&sidecar), 10_000);

    let corrupt_payload = json!({
        "session_id": session_id,
        "hook_event_name": "UserPromptSubmit",
        "message_id": "large-sidecar-9876",
        "sequence": 9876,
        "cwd": "/tmp/nabu-fixture",
        "project_root": "/tmp/nabu-fixture",
        "prompt": "large sidecar marker 9876"
    });
    let corrupt_event = envelope_from_backfill_payload(
        Tool::Claude,
        Path::new("/tmp/large-sidecar.jsonl"),
        9876,
        corrupt_payload.clone(),
        &BackfillParseContext::default(),
    )
    .unwrap();
    let corrupt_key = dedupe_key(DedupeParts {
        tool: corrupt_event.tool,
        session_id: &corrupt_event.session_id,
        canonical_type: corrupt_event.canonical_type,
        source_event_id: corrupt_event.source_event_id.as_deref(),
        sequence: corrupt_event.sequence,
        payload: &corrupt_event.payload,
    })
    .unwrap();
    let corrupt_bucket = crate::ingest::dedupe_bucket_index(&corrupt_key).unwrap();
    fs::write(sidecar.bucket_path(corrupt_bucket), b"sha256:truncated").unwrap();
    let duplicate_after_corruption =
        ingest_hook_event(&home, Tool::Claude, corrupt_payload).unwrap();
    assert!(!duplicate_after_corruption.appended);
    assert_eq!(raw_line_count(&raw_path), 10_000);
    assert_eq!(dedupe_sidecar_entry_count(&sidecar), 10_000);

    index_once(&home).unwrap();
    // Under OR semantics the shared "large sidecar marker" terms match every
    // seeded event; the unique "1234" term is rare, so bm25 ranks that event
    // first. Assert the targeted event is the top hit rather than the sole one.
    let results = search_history(&home, "large sidecar marker 1234", 10).unwrap();
    assert!(results[0].snippet.contains("large sidecar marker 1234"));
}

#[test]
fn concurrent_appends_keep_raw_and_sidecar_consistent() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    let session_id = "concurrent-sidecar-session";
    let mut handles = Vec::new();

    for index in 0..64 {
        let home = home.clone();
        handles.push(std::thread::spawn(move || {
            ingest_hook_event(
                &home,
                Tool::Codex,
                json!({
                    "session_id": session_id,
                    "hook_event_name": "UserPromptSubmit",
                    "message_id": format!("concurrent-sidecar-{index}"),
                    "sequence": index as i64,
                    "cwd": "/tmp/nabu-fixture",
                    "project_root": "/tmp/nabu-fixture",
                    "prompt": format!("concurrent sidecar marker {index}")
                }),
            )
            .unwrap()
        }));
    }

    for handle in handles {
        assert!(handle.join().unwrap().appended);
    }

    let raw_path = canonical_raw_path(&home, Tool::Codex, session_id);
    let sidecar = DedupeSidecarFiles::for_raw_file(&home, &raw_path);
    assert_eq!(raw_line_count(&raw_path), 64);
    assert_eq!(dedupe_sidecar_entry_count(&sidecar), 64);
}

#[test]
fn native_order_preserves_identical_content_and_unordered_still_collapses() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    for sequence in [1, 2] {
        let report = ingest_hook_event(
            &home,
            Tool::Codex,
            json!({
                "session_id": "ordered-identical-session",
                "hook_event_name": "UserPromptSubmit",
                "sequence": sequence,
                "cwd": "/tmp/nabu-fixture",
                "project_root": "/tmp/nabu-fixture",
                "prompt": "identical ordered content marker"
            }),
        )
        .unwrap();
        assert!(report.appended);
    }
    assert_eq!(
        raw_line_count(&canonical_raw_path(
            &home,
            Tool::Codex,
            "ordered-identical-session"
        )),
        2
    );

    let first = ingest_hook_event(
        &home,
        Tool::Codex,
        json!({
            "session_id": "unordered-identical-session",
            "hook_event_name": "UserPromptSubmit",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "prompt": "identical unordered content marker"
        }),
    )
    .unwrap();
    let second = ingest_hook_event(
        &home,
        Tool::Codex,
        json!({
            "session_id": "unordered-identical-session",
            "hook_event_name": "UserPromptSubmit",
            "captured_at": "2099-01-01T00:00:00Z",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "prompt": "identical unordered content marker"
        }),
    )
    .unwrap();
    assert!(first.appended);
    assert!(!second.appended);
    assert_eq!(
        raw_line_count(&canonical_raw_path(
            &home,
            Tool::Codex,
            "unordered-identical-session"
        )),
        1
    );
}

#[test]
fn source_specific_ordering_fields_are_mapped_to_sequence() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    for part_index in [7, 8] {
        let report = ingest_hook_event(
            &home,
            Tool::Opencode,
            json!({
                "session_id": "opencode-part-order-session",
                "hook_event_name": "message.part.updated",
                "message_id": "shared-opencode-message",
                "part": {
                    "index": part_index,
                    "text": "same opencode part text marker"
                },
                "cwd": "/tmp/nabu-fixture",
                "project_root": "/tmp/nabu-fixture",
                "delta": "same opencode part text marker"
            }),
        )
        .unwrap();
        assert!(report.appended);
    }

    for item_index in [3, 4] {
        let report = ingest_hook_event(
            &home,
            Tool::Codex,
            json!({
                "session_id": "codex-item-order-session",
                "type": "response_item",
                "payload": {
                    "type": "message",
                    "role": "assistant",
                    "item_index": item_index,
                    "content": [{"type": "output_text", "text": "same codex item text marker"}]
                },
                "cwd": "/tmp/nabu-fixture",
                "project_root": "/tmp/nabu-fixture"
            }),
        )
        .unwrap();
        assert!(report.appended);
    }

    let first = envelope_from_backfill_payload(
        Tool::Claude,
        Path::new("/tmp/transcript.jsonl"),
        10,
        json!({
            "session_id": "backfill-offset-order-session",
            "hook_event_name": "UserPromptSubmit",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "prompt": "same backfill offset text marker"
        }),
        &BackfillParseContext::default(),
    )
    .unwrap();
    let second = envelope_from_backfill_payload(
        Tool::Claude,
        Path::new("/tmp/transcript.jsonl"),
        20,
        json!({
            "session_id": "backfill-offset-order-session",
            "hook_event_name": "UserPromptSubmit",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "prompt": "same backfill offset text marker"
        }),
        &BackfillParseContext::default(),
    )
    .unwrap();
    assert!(append_prepared_event(&home, first).unwrap().appended);
    assert!(append_prepared_event(&home, second).unwrap().appended);

    assert_eq!(
        raw_line_count(&canonical_raw_path(
            &home,
            Tool::Opencode,
            "opencode-part-order-session"
        )),
        2
    );
    assert_eq!(
        raw_line_count(&canonical_raw_path(
            &home,
            Tool::Codex,
            "codex-item-order-session"
        )),
        2
    );
    assert_eq!(
        raw_line_count(&canonical_raw_path(
            &home,
            Tool::Claude,
            "backfill-offset-order-session"
        )),
        2
    );
}

#[test]
fn codex_native_transcript_backfill_derives_session_id_from_metadata() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let source = temp.path().join("codex-sessions");
    init_home(&home).unwrap();
    fs::create_dir_all(&source).unwrap();

    let session_id = "019a4b44-cc3b-7c51-8944-a7d7ebb9e6fe";
    fs::write(
            source.join(format!("rollout-2025-11-03T20-49-51-{session_id}.jsonl")),
            format!(
                "{{\"timestamp\":\"2025-11-03T19:49:51.304Z\",\"type\":\"session_meta\",\"payload\":{{\"id\":\"{session_id}\",\"cwd\":\"/tmp/native-codex\"}}}}\n\
                 {{\"timestamp\":\"2025-11-03T19:50:01.966Z\",\"type\":\"response_item\",\"payload\":{{\"type\":\"message\",\"role\":\"user\",\"content\":[{{\"type\":\"input_text\",\"text\":\"native codex backfill marker\"}}]}}}}\n"
            ),
        )
        .unwrap();

    let report = backfill_since(&home, Some(Tool::Codex), &source, None).unwrap();
    assert_eq!(report.source_files, 1);
    assert_eq!(report.appended_events, 2);
    assert_eq!(report.checkpoint_files, 1);

    index_once(&home).unwrap();
    let results = search_history(&home, "native codex backfill marker", 10).unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].tool, Tool::Codex);
    assert_eq!(results[0].session_id, session_id);
    assert_eq!(results[0].canonical_type, "user.message");
}

#[cfg(unix)]
#[test]
fn backfill_skips_source_file_that_vanishes_before_read() {
    // A session file discovered during the scan can be deleted/rotated by the
    // live tool before backfill reads it (os error 2). One vanished file must
    // not abort the whole backfill. A dangling symlink reproduces a candidate
    // that passes discovery but fails with NotFound on read.
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let source = temp.path().join("codex-sessions");
    init_home(&home).unwrap();
    fs::create_dir_all(&source).unwrap();

    let session_id = "019a4f57-3d5f-7f52-96cc-cb2e1eacb7a9";
    fs::write(
            source.join(format!("rollout-2025-11-04T15-48-28-{session_id}.jsonl")),
            "{\"timestamp\":\"2025-11-04T14:48:28.000Z\",\"type\":\"response_item\",\"payload\":{\"type\":\"message\",\"role\":\"user\",\"content\":[{\"type\":\"input_text\",\"text\":\"surviving codex marker\"}]}}\n",
        )
        .unwrap();
    // Discovered by extension, but reading it yields NotFound.
    std::os::unix::fs::symlink(
        temp.path().join("does-not-exist.jsonl"),
        source.join("rollout-2025-11-04T16-00-00-vanished.jsonl"),
    )
    .unwrap();

    // Dry run (the wizard's "Scanning past sessions…") must not fail.
    let dry = backfill_dry_run(&home, Some(Tool::Codex), &source, None).unwrap();
    assert_eq!(dry.source_files, 1);

    // The real backfill must skip the vanished file and import the valid one.
    let report = backfill_since(&home, Some(Tool::Codex), &source, None).unwrap();
    assert_eq!(report.source_files, 1);
    assert_eq!(report.appended_events, 1);

    index_once(&home).unwrap();
    let results = search_history(&home, "surviving codex marker", 10).unwrap();
    assert_eq!(results.len(), 1);
}

#[test]
fn codex_native_transcript_backfill_derives_session_id_from_filename() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let source = temp.path().join("codex-sessions");
    init_home(&home).unwrap();
    fs::create_dir_all(&source).unwrap();

    let session_id = "019a4f57-3d5f-7f52-96cc-cb2e1eacb7a9";
    fs::write(
            source.join(format!("rollout-2025-11-04T15-48-28-{session_id}.jsonl")),
            "{\"timestamp\":\"2025-11-04T14:48:28.000Z\",\"type\":\"response_item\",\"payload\":{\"type\":\"message\",\"role\":\"user\",\"content\":[{\"type\":\"input_text\",\"text\":\"filename derived codex marker\"}]}}\n",
        )
        .unwrap();

    let report = backfill_since(&home, Some(Tool::Codex), &source, None).unwrap();
    assert_eq!(report.appended_events, 1);

    index_once(&home).unwrap();
    let results = search_history(&home, "filename derived codex marker", 10).unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].session_id, session_id);
}

#[test]
fn claude_native_backfill_ignores_project_sidecars() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let source = temp.path().join("claude-projects");
    init_home(&home).unwrap();
    fs::create_dir_all(source.join("session/subagents")).unwrap();

    let session_id = "15a53bfd-c382-488d-b890-687021285e49";
    fs::write(
            source.join(format!("{session_id}.jsonl")),
            format!(
                "{{\"session_id\":\"{session_id}\",\"cwd\":\"/tmp/native-claude\",\"project_root\":\"/tmp/native-claude\",\"type\":\"claude.transcript.user\",\"canonical_type\":\"user.message\",\"event_id\":\"claude-native-1\",\"message\":\"native claude marker\"}}\n"
            ),
        )
        .unwrap();
    fs::write(source.join("sessions-index.json"), "{\"sessions\":[]}").unwrap();
    fs::write(
        source.join("session/subagents/agent-a676598cc8f883f73.meta.json"),
        "{\"agent_id\":\"agent-a676598cc8f883f73\"}",
    )
    .unwrap();
    fs::write(
        source.join("session/subagents/skill-injections.jsonl"),
        "{\"kind\":\"plugin-config\",\"type\":\"skill-injection\"}\n",
    )
    .unwrap();

    let report = backfill_since(&home, Some(Tool::Claude), &source, None).unwrap();
    assert_eq!(report.source_files, 1);
    assert_eq!(report.appended_events, 1);
    assert_eq!(report.checkpoint_files, 1);

    index_once(&home).unwrap();
    let results = search_history(&home, "native claude marker", 10).unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].session_id, session_id);
}

#[test]
fn sanitized_real_native_fixtures_import_defensively_for_all_tools() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let repo = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
    init_home(&home).unwrap();

    let claude_report = backfill_since(
        &home,
        Some(Tool::Claude),
        &repo.join("fixtures/native/claude/projects"),
        None,
    )
    .unwrap();
    let codex_report = backfill_since(
        &home,
        Some(Tool::Codex),
        &repo.join("fixtures/native/codex/sessions"),
        None,
    )
    .unwrap();
    let opencode_report = backfill_since(
        &home,
        Some(Tool::Opencode),
        &repo.join("fixtures/native/opencode"),
        None,
    )
    .unwrap();

    assert_eq!(claude_report.source_files, 1);
    assert_eq!(claude_report.appended_events, 5);
    assert_eq!(codex_report.source_files, 1);
    assert_eq!(codex_report.appended_events, 4);
    assert_eq!(opencode_report.source_files, 5);
    assert_eq!(opencode_report.appended_events, 8);
    assert_eq!(checkpoint_row_count(&home), 7);

    index_once(&home).unwrap();
    assert_eq!(
        search_history(&home, "sanitized native claude user marker", 10).unwrap()[0].session_id,
        "11111111-1111-4111-8111-111111111111"
    );
    assert_eq!(
        search_history(&home, "sanitized native codex assistant marker", 10).unwrap()[0].session_id,
        "22222222-2222-4222-8222-222222222222"
    );
    assert_eq!(
        search_history(&home, "sanitized native opencode assistant marker", 10).unwrap()[0]
            .session_id,
        "33333333-3333-4333-8333-333333333333"
    );

    let claude_raw =
        canonical_raw_path(&home, Tool::Claude, "11111111-1111-4111-8111-111111111111");
    let envelopes = raw_envelopes(&claude_raw);
    let parse_error = envelopes
        .iter()
        .find(|event| event.canonical_type == CanonicalType::Error)
        .expect("malformed native line should import as error");
    assert_eq!(
        parse_error.payload.get("type").and_then(Value::as_str),
        Some("parse_error")
    );
    assert!(parse_error.payload.get("raw_line").is_some());
}

#[test]
fn opencode_native_fixture_maps_m8_types_worktree_and_metadata_session() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let repo = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
    let fixture = repo.join("fixtures/native/opencode");
    let session_id = "33333333-3333-4333-8333-333333333333";
    init_home(&home).unwrap();

    let first = backfill_since(&home, Some(Tool::Opencode), &fixture, None).unwrap();
    let second = backfill_since(&home, Some(Tool::Opencode), &fixture, None).unwrap();

    assert_eq!(first.source_files, 5);
    assert_eq!(first.appended_events, 8);
    assert_eq!(second.appended_events, 0);

    let raw_path = canonical_raw_path(&home, Tool::Opencode, session_id);
    let envelopes = raw_envelopes(&raw_path);
    assert_eq!(envelopes.len(), 8);
    let error_count = envelopes
        .iter()
        .filter(|event| event.canonical_type == CanonicalType::Error)
        .count();
    assert_eq!(error_count, 0);

    assert!(envelopes.iter().any(|event| {
        event.source_event_type == "reasoning"
            && event.canonical_type == CanonicalType::AssistantDelta
    }));
    assert!(envelopes.iter().any(|event| {
        event.source_event_type == "step-start"
            && event.canonical_type == CanonicalType::AssistantDelta
    }));
    assert!(envelopes.iter().any(|event| {
        event.source_event_type == "step-finish"
            && event.canonical_type == CanonicalType::AssistantDelta
    }));
    assert!(envelopes.iter().any(|event| {
        event.source_event_type == "patch" && event.canonical_type == CanonicalType::FileChanged
    }));
    assert!(envelopes.iter().any(|event| {
        event.source_event_type == "session.created"
            && event.canonical_type == CanonicalType::SessionStarted
    }));
    assert!(envelopes
        .iter()
        .all(|event| event.project_root.is_some() && event.cwd.is_some()));
    assert!(envelopes.iter().any(|event| {
        event.source_event_type == "reasoning"
            && event.project_root.as_deref() == Some("/Users/example/opencode-project")
            && event.cwd.as_deref() == Some("/Users/example/opencode-project")
    }));
    assert!(!canonical_raw_path(&home, Tool::Opencode, "project_meta").exists());
}

#[test]
fn backfill_uses_sqlite_checkpoints_and_incremental_rerun_is_noop() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let source = temp.path().join("codex-sessions");
    let session_id = "44444444-4444-4444-8444-444444444444";
    init_home(&home).unwrap();
    fs::create_dir_all(&source).unwrap();
    fs::write(
            source.join(format!("rollout-2026-06-18T10-00-00-{session_id}.jsonl")),
            format!(
                "{{\"timestamp\":\"2026-06-18T10:00:00.000Z\",\"type\":\"session_meta\",\"payload\":{{\"id\":\"{session_id}\",\"cwd\":\"/tmp/native-codex\"}}}}\n\
                 {{\"timestamp\":\"2026-06-18T10:00:01.000Z\",\"type\":\"response_item\",\"payload\":{{\"id\":\"checkpoint-user-1\",\"type\":\"message\",\"role\":\"user\",\"content\":[{{\"type\":\"input_text\",\"text\":\"checkpoint native marker\"}}]}}}}\n"
            ),
        )
        .unwrap();

    let first = backfill_since(&home, Some(Tool::Codex), &source, None).unwrap();
    let second = backfill_since(&home, Some(Tool::Codex), &source, None).unwrap();

    assert_eq!(first.appended_events, 2);
    assert_eq!(first.checkpoint_files, 1);
    assert_eq!(second.appended_events, 0);
    assert_eq!(checkpoint_row_count(&home), 1);
    assert_eq!(checkpoint_sidecar_count(&home), 0);
    assert_eq!(
        raw_line_count(&canonical_raw_path(&home, Tool::Codex, session_id)),
        2
    );
}

#[test]
fn raw_index_checkpoints_skip_unchanged_canonical_files_and_refresh_on_append() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let session_id = "raw-index-checkpoint-session";
    init_home(&home).unwrap();
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": session_id,
            "hook_event_name": "UserPromptSubmit",
            "message_id": "raw-index-checkpoint-1",
            "prompt": "raw checkpoint first marker"
        }),
    )
    .unwrap();

    let raw_path = canonical_raw_path(&home, Tool::Claude, session_id);
    let first = index_once(&home).unwrap();
    let second = index_once(&home).unwrap();
    let db_path = home.join("index").join("harness.db");
    let conn = open_index(&db_path).unwrap();
    let source_meta = source_file_metadata(&raw_path).unwrap();

    assert_eq!(first.indexed_events, 1);
    assert_eq!(second.indexed_events, 0);
    assert!(raw_index_checkpoint_is_current(
        &conn,
        &db_path,
        Tool::Claude,
        &raw_path,
        &source_meta
    )
    .unwrap());
    let raw_checkpoint_count: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM checkpoints WHERE source_kind = 'raw_jsonl'",
            [],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(raw_checkpoint_count, 1);

    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": session_id,
            "hook_event_name": "UserPromptSubmit",
            "message_id": "raw-index-checkpoint-2",
            "prompt": "raw checkpoint second marker"
        }),
    )
    .unwrap();
    let changed_meta = source_file_metadata(&raw_path).unwrap();
    assert!(!raw_index_checkpoint_is_current(
        &conn,
        &db_path,
        Tool::Claude,
        &raw_path,
        &changed_meta
    )
    .unwrap());

    let third = index_once(&home).unwrap();
    assert_eq!(third.indexed_events, 1);
}

#[test]
fn fts_schema_migration_rebuilds_without_reindexing_raw_files() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "fts-migration-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "fts-migration-1",
            "prompt": "streaming fts rebuild marker"
        }),
    )
    .unwrap();

    assert_eq!(index_once(&home).unwrap().indexed_events, 1);
    let db_path = home.join("index").join("harness.db");
    Connection::open(&db_path)
        .unwrap()
        .execute_batch(
            "DROP TABLE IF EXISTS events_fts;
                 CREATE VIRTUAL TABLE events_fts USING fts5(searchable_text);",
        )
        .unwrap();

    assert_eq!(index_once(&home).unwrap().indexed_events, 0);
    let results = search_history(&home, "streaming fts rebuild marker", 10).unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].session_id, "fts-migration-session");
}

#[test]
fn discontinuities_emit_once_for_truncation_rotation_and_deletion() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let root = temp.path().join("claude-projects");
    init_home(&home).unwrap();
    fs::create_dir_all(&root).unwrap();

    let truncated_session = "55555555-5555-4555-8555-555555555555";
    let truncated = root.join(format!("{truncated_session}.jsonl"));
    fs::write(
        &truncated,
        claude_user_line(truncated_session, "truncate-1", "truncation original one")
            + &claude_user_line(truncated_session, "truncate-2", "truncation original two"),
    )
    .unwrap();
    backfill_since(&home, Some(Tool::Claude), &root, None).unwrap();
    fs::write(
        &truncated,
        claude_user_line(truncated_session, "truncate-1", "truncation original one"),
    )
    .unwrap();
    assert_eq!(
        backfill_since(&home, Some(Tool::Claude), &root, None)
            .unwrap()
            .discontinuities,
        1
    );
    assert_eq!(
        backfill_since(&home, Some(Tool::Claude), &root, None)
            .unwrap()
            .discontinuities,
        0
    );
    assert_eq!(
        discontinuity_count(&home, Tool::Claude, truncated_session, "source.truncated"),
        1
    );

    let rotated_session = "66666666-6666-4666-8666-666666666666";
    let rotated = root.join(format!("{rotated_session}.jsonl"));
    fs::write(
        &rotated,
        claude_user_line(rotated_session, "rotate-1", "rotation original marker"),
    )
    .unwrap();
    backfill_since(&home, Some(Tool::Claude), &root, None).unwrap();
    fs::remove_file(&rotated).unwrap();
    fs::write(
        &rotated,
        claude_user_line(
            rotated_session,
            "rotate-2",
            "rotation replacement marker with enough bytes to avoid truncation precedence",
        ),
    )
    .unwrap();
    assert_eq!(
        backfill_since(&home, Some(Tool::Claude), &root, None)
            .unwrap()
            .discontinuities,
        1
    );
    assert_eq!(
        backfill_since(&home, Some(Tool::Claude), &root, None)
            .unwrap()
            .discontinuities,
        0
    );
    assert_eq!(
        discontinuity_count(&home, Tool::Claude, rotated_session, "source.rotated"),
        1
    );

    let deleted_session = "77777777-7777-4777-8777-777777777777";
    let deleted = root.join(format!("{deleted_session}.jsonl"));
    fs::write(
        &deleted,
        claude_user_line(deleted_session, "delete-1", "deletion original marker"),
    )
    .unwrap();
    backfill_since(&home, Some(Tool::Claude), &root, None).unwrap();
    fs::remove_file(&deleted).unwrap();
    assert_eq!(
        backfill_since(&home, Some(Tool::Claude), &root, None)
            .unwrap()
            .discontinuities,
        1
    );
    assert_eq!(
        backfill_since(&home, Some(Tool::Claude), &root, None)
            .unwrap()
            .discontinuities,
        0
    );
    assert_eq!(
        discontinuity_count(&home, Tool::Claude, deleted_session, "source.deleted"),
        1
    );
}

#[test]
fn dry_run_reports_missing_events_and_writes_nothing() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let source = temp.path().join("codex-sessions");
    let session_id = "88888888-8888-4888-8888-888888888888";
    init_home(&home).unwrap();
    fs::create_dir_all(&source).unwrap();
    fs::write(
            source.join(format!("rollout-2026-06-18T11-00-00-{session_id}.jsonl")),
            "{\"timestamp\":\"2026-06-18T11:00:00.000Z\",\"type\":\"response_item\",\"payload\":{\"id\":\"dry-run-shared\",\"type\":\"message\",\"role\":\"user\",\"content\":[{\"type\":\"input_text\",\"text\":\"dry run shared marker\"}]}}\n\
                 {\"timestamp\":\"2026-06-18T11:00:01.000Z\",\"type\":\"response_item\",\"payload\":{\"id\":\"dry-run-gap\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"dry run gap marker\"}]}}\n",
        )
        .unwrap();
    ingest_hook_event(
        &home,
        Tool::Codex,
        json!({
            "session_id": session_id,
            "type": "response_item",
            "payload": {
                "id": "dry-run-shared",
                "type": "message",
                "role": "user",
                "content": [{"type": "input_text", "text": "dry run shared marker"}]
            }
        }),
    )
    .unwrap();

    let before_raw_lines = raw_line_count(&canonical_raw_path(&home, Tool::Codex, session_id));
    let report = backfill_dry_run(&home, Some(Tool::Codex), &source, None).unwrap();
    let after_raw_lines = raw_line_count(&canonical_raw_path(&home, Tool::Codex, session_id));

    assert_eq!(report.source_files, 1);
    assert_eq!(report.on_disk_events, 2);
    assert_eq!(report.captured_events, 1);
    assert_eq!(report.missing_events, 1);
    assert_eq!(report.partial_sessions, 1);
    assert_eq!(report.sessions[0].would_import.len(), 1);
    assert_eq!(
        report.sessions[0].would_import[0].canonical_type,
        "assistant.message"
    );
    assert_eq!(before_raw_lines, after_raw_lines);
    assert_eq!(checkpoint_row_count(&home), 0);
}

#[test]
fn partial_live_capture_then_backfill_reconciles_for_each_tool() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    assert_reconciles_claude(&home, temp.path());
    assert_reconciles_codex(&home, temp.path());
    assert_reconciles_opencode(&home, temp.path());
}

#[test]
fn doctor_reports_compact_coverage_summary() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let source = temp.path().join("claude-projects");
    let session_id = "99999999-9999-4999-8999-999999999999";
    init_home(&home).unwrap();
    fs::create_dir_all(&source).unwrap();
    fs::write(
        source.join(format!("{session_id}.jsonl")),
        claude_user_line(session_id, "doctor-coverage-1", "doctor coverage marker"),
    )
    .unwrap();
    backfill_since(&home, Some(Tool::Claude), &source, None).unwrap();
    index_once(&home).unwrap();

    let report = doctor_with_options(&home, false);

    assert_eq!(report.coverage.checkpointed_sources, 2);
    assert_eq!(report.coverage.captured_sessions, 1);
    assert_eq!(report.coverage.captured_events, 1);
    assert!(report.storage_footprint.raw_bytes > 0);
    assert!(report.storage_footprint.index_bytes > 0);
    assert_eq!(report.storage_footprint.vectors_bytes, 0);
    assert_eq!(report.storage_footprint.models_bytes, 0);
    assert_eq!(
        report.storage_footprint.canonical_total,
        report
            .storage_footprint
            .raw_bytes
            .saturating_add(report.storage_footprint.blobs_bytes)
    );
    assert_eq!(
        report.storage_footprint.derived_total,
        report
            .storage_footprint
            .index_bytes
            .saturating_add(report.storage_footprint.spool_bytes)
            .saturating_add(report.storage_footprint.models_bytes)
    );
    assert!(report.storage_footprint.total_bytes >= report.storage_footprint.raw_bytes);
}

#[cfg(not(feature = "semantic"))]
#[test]
fn default_build_reports_no_semantic_model_without_touching_network() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    let status = embedding_model_status(&home);

    assert!(!status.feature_enabled);
    assert_eq!(status.model_id, "embeddinggemma-300m-q4");
    assert_eq!(status.expected_dimensions, 256);
    assert!(!status.model_present);
    assert!(!status.semantic_available);
    assert!(status.message.contains("semantic feature is disabled"));
}

#[test]
fn embedding_model_disclosure_reports_terms_and_measured_local_footprint() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    let cache_path = semantic_model_cache_path(&home);
    fs::create_dir_all(&cache_path).unwrap();
    fs::write(cache_path.join("partial-file.bin"), b"partial model bytes").unwrap();

    let disclosure = embedding_model_disclosure(&home, SEMANTIC_MODEL_ID).unwrap();

    assert_eq!(disclosure.model_id, SEMANTIC_MODEL_ID);
    assert_eq!(disclosure.repository, SEMANTIC_MODEL_REPO);
    assert_eq!(disclosure.total_files, SEMANTIC_MODEL_REMOTE_FILES.len());
    assert!(disclosure.current_on_disk_bytes >= "partial model bytes".len() as u64);
    assert!(!disclosure.model_present);
    assert!(disclosure.license_summary.contains("Gemma Terms of Use"));
    assert!(disclosure.cache_path.ends_with(SEMANTIC_MODEL_ID));
}

#[test]
fn semantic_retrieval_fixture_is_labeled_without_requiring_model() {
    let fixture = semantic_retrieval_fixture();
    assert_eq!(fixture.schema_version, 1);
    assert_eq!(fixture.tool, Tool::Claude);
    assert!(!fixture.session_id.trim().is_empty());
    assert!(!fixture.cwd.trim().is_empty());
    assert!(!fixture.project_root.trim().is_empty());
    assert!(!fixture.events.is_empty());
    assert!(!fixture.queries.is_empty());

    let event_ids = fixture
        .events
        .iter()
        .map(|event| {
            assert!(!event.event_id.trim().is_empty());
            assert!(matches!(event.role.as_str(), "user" | "assistant"));
            assert!(!event.text.trim().is_empty());
            event.event_id.clone()
        })
        .collect::<BTreeSet<_>>();
    assert_eq!(event_ids.len(), fixture.events.len());

    for query in &fixture.queries {
        assert!(!query.query.trim().is_empty());
        assert!(!query.relevant_event_ids.is_empty());
        for event_id in &query.relevant_event_ids {
            assert!(
                event_ids.contains(event_id),
                "query {:?} references unknown event id {event_id}",
                query.query
            );
        }
    }
}

#[test]
fn corroboration_extracts_and_resolves_refs_read_only_against_local_git() {
    let extraction_refs = extract_corroboration_candidates(
            "commit abcdef1 landed on branch feature/corroborate, touched src/lib.rs, and referenced PR #42.",
        )
        .into_iter()
        .map(|candidate| (candidate.kind.as_str().to_string(), candidate.reference))
        .collect::<BTreeSet<_>>();
    assert_eq!(
        extraction_refs,
        BTreeSet::from([
            ("branch".to_string(), "feature/corroborate".to_string()),
            ("commit".to_string(), "abcdef1".to_string()),
            ("file".to_string(), "src/lib.rs".to_string()),
            ("pr".to_string(), "#42".to_string()),
        ])
    );

    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let repo = temp.path().join("repo");
    fs::create_dir_all(repo.join("src")).unwrap();
    run_git_setup(temp.path(), &["init", repo.to_str().unwrap()]);
    run_git(&repo, &["config", "user.email", "nabu@example.invalid"]);
    run_git(&repo, &["config", "user.name", "Nabu Test"]);
    fs::write(repo.join("src/lib.rs"), "pub fn corroborated() {}\n").unwrap();
    run_git(&repo, &["add", "src/lib.rs"]);
    run_git(&repo, &["commit", "-m", "initial corroboration fixture"]);
    run_git(&repo, &["branch", "feature/corroborate"]);
    fs::create_dir_all(repo.join("notes")).unwrap();
    fs::write(
        repo.join("notes/trace.txt"),
        "untracked corroboration note\n",
    )
    .unwrap();
    let commit = run_git(&repo, &["rev-parse", "HEAD"]);
    let commit_prefix = &commit[..12];
    let missing_commit = "ffffffffffffffffffffffffffffffffffffffff";
    let before_snapshot = git_snapshot(&repo);

    init_home(&home).unwrap();
    let text = format!(
            "corroboration marker commit {commit_prefix} and missing commit {missing_commit}; branch feature/corroborate and branch missing/branch; files src/lib.rs notes/trace.txt src/missing.txt; PR #123."
        );
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "corroboration-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "corroboration-message",
            "cwd": repo,
            "project_root": repo,
            "prompt": text,
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    clear_git_invocations();
    let default_page = search_history_page(
        &home,
        "corroboration marker",
        SearchOptions {
            limit: 1,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(default_page.returned, 1);
    assert!(default_page.results[0].corroboration.is_none());
    assert!(captured_git_invocations().is_empty());

    let page = search_history_page(
        &home,
        "corroboration marker",
        SearchOptions {
            limit: 1,
            corroborate: true,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    let corroboration = page.results[0].corroboration.as_ref().unwrap();
    let canonical_repo = fs::canonicalize(&repo).unwrap();
    assert_eq!(
        corroboration.repo.as_deref(),
        Some(canonical_repo.to_str().unwrap())
    );
    assert_ref_status(corroboration, "commit", commit_prefix, "present", None);
    assert_ref_status(corroboration, "commit", missing_commit, "missing", None);
    assert_ref_status(
        corroboration,
        "branch",
        "feature/corroborate",
        "present",
        None,
    );
    assert_ref_status(corroboration, "branch", "missing/branch", "missing", None);
    assert_ref_status(corroboration, "file", "src/lib.rs", "present", None);
    assert_ref_status(corroboration, "file", "notes/trace.txt", "untracked", None);
    assert_ref_status(corroboration, "file", "src/missing.txt", "missing", None);
    assert_ref_status(
        corroboration,
        "pr",
        "#123",
        "unresolved",
        Some("needs_network"),
    );
    assert_eq!(git_snapshot(&repo), before_snapshot);
    assert_no_network_git_commands(&captured_git_invocations());

    let no_repo = temp.path().join("no-repo");
    fs::create_dir_all(&no_repo).unwrap();
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "no-repo-corroboration-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "no-repo-corroboration-message",
            "cwd": no_repo,
            "project_root": no_repo,
            "prompt": "no repo marker commit deadbee file src/lib.rs PR #7",
        }),
    )
    .unwrap();
    index_once(&home).unwrap();
    let no_repo_page = search_history_page(
        &home,
        "no repo marker",
        SearchOptions {
            limit: 1,
            corroborate: true,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    let no_repo_corroboration = no_repo_page.results[0].corroboration.as_ref().unwrap();
    assert_eq!(no_repo_corroboration.repo, None);
    assert_ref_status(
        no_repo_corroboration,
        "commit",
        "deadbee",
        "unresolved",
        Some("no_repo"),
    );
    assert_ref_status(
        no_repo_corroboration,
        "file",
        "src/lib.rs",
        "unresolved",
        Some("no_repo"),
    );
    assert_ref_status(
        no_repo_corroboration,
        "pr",
        "#7",
        "unresolved",
        Some("needs_network"),
    );
}

fn run_git_setup(cwd: &Path, args: &[&str]) -> String {
    let output = std::process::Command::new("git")
        .args(args)
        .current_dir(cwd)
        .env("GIT_TERMINAL_PROMPT", "0")
        .env("GIT_PAGER", "cat")
        .env("PAGER", "cat")
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "git setup failed: {}\n{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).trim().to_string()
}

fn run_git(repo: &Path, args: &[&str]) -> String {
    let output = std::process::Command::new("git")
        .arg("-C")
        .arg(repo)
        .args(args)
        .env("GIT_TERMINAL_PROMPT", "0")
        .env("GIT_PAGER", "cat")
        .env("PAGER", "cat")
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "git command failed: git -C {} {}\n{}\n{}",
        repo.display(),
        args.join(" "),
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).trim().to_string()
}

#[derive(Debug, PartialEq, Eq)]
struct GitSnapshot {
    head: String,
    refs: String,
    index: String,
    status: String,
}

fn git_snapshot(repo: &Path) -> GitSnapshot {
    GitSnapshot {
        head: run_git(repo, &["rev-parse", "HEAD"]),
        refs: run_git(repo, &["for-each-ref", "--format=%(refname):%(objectname)"]),
        index: run_git(repo, &["ls-files", "-s"]),
        status: run_git(repo, &["status", "--porcelain=v1", "-z"]),
    }
}

fn assert_ref_status(
    corroboration: &Corroboration,
    kind: &str,
    reference: &str,
    status: &str,
    reason: Option<&str>,
) {
    let found = corroboration
        .refs
        .iter()
        .find(|candidate| candidate.kind == kind && candidate.reference == reference)
        .unwrap_or_else(|| panic!("missing corroborated ref {kind} {reference}"));
    assert_eq!(found.status, status);
    assert_eq!(found.reason.as_deref(), reason);
}

fn clear_git_invocations() {
    git_invocations().lock().unwrap().clear();
}

fn captured_git_invocations() -> Vec<Vec<String>> {
    git_invocations().lock().unwrap().clone()
}

fn assert_no_network_git_commands(commands: &[Vec<String>]) {
    assert!(
        !commands.is_empty(),
        "corroboration should have used local git read commands"
    );
    for command in commands {
        let Some(operation) = command.first().map(String::as_str) else {
            continue;
        };
        assert!(
            matches!(operation, "rev-parse" | "cat-file" | "log" | "ls-files"),
            "unexpected git operation in corroboration path: {command:?}"
        );
        assert!(
            !matches!(operation, "fetch" | "pull" | "ls-remote"),
            "network-capable git command must not run: {command:?}"
        );
    }
}

#[test]
fn date_or_duration_filters_and_purge_before_use_normalized_thresholds() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    for (session_id, message_id, captured_at, prompt) in [
        (
            "old-session",
            "old-message",
            "2020-01-01T00:00:00Z",
            "datefilter old marker",
        ),
        (
            "new-session",
            "new-message",
            "2099-01-01T00:00:00Z",
            "datefilter new marker",
        ),
    ] {
        let event = envelope_from_backfill_payload(
            Tool::Claude,
            Path::new("/tmp/datefilter.jsonl"),
            0,
            json!({
                "session_id": session_id,
                "hook_event_name": "UserPromptSubmit",
                "message_id": message_id,
                "captured_at": captured_at,
                "cwd": "/tmp/nabu-fixture",
                "project_root": "/tmp/nabu-fixture",
                "prompt": prompt
            }),
            &BackfillParseContext::default(),
        )
        .unwrap();
        append_prepared_event(&home, event).unwrap();
    }

    index_once(&home).unwrap();

    let recent = search_history_filtered(
        &home,
        "datefilter",
        SearchOptions {
            since: Some("1d".to_string()),
            limit: 10,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(recent.len(), 1);
    assert_eq!(recent[0].session_id, "new-session");

    let sessions = list_sessions(&home, Some(Tool::Claude), None, Some("1d"), 10).unwrap();
    assert_eq!(sessions.len(), 1);
    assert_eq!(sessions[0].session_id, "new-session");

    let report = purge_before(&home, "2021-01-01").unwrap();
    assert_eq!(report.indexed_events_removed, 1);
    // Purge dropped the old event; only the new one survives. Under OR
    // semantics the shared "datefilter"/"marker" terms match the survivor, so
    // assert on the surviving session rather than an AND-coupled zero count.
    let surviving = search_history(&home, "datefilter marker", 10).unwrap();
    assert_eq!(surviving.len(), 1);
    assert_eq!(surviving[0].session_id, "new-session");
}

// P0 bug #1: a more-specific multi-word query must not collapse recall to
// zero. Controlled by varying only term count with no session filter.
#[test]
fn search_multi_word_query_keeps_recall_with_or_semantics() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    // Target matches four of the long query's six terms; the distractor
    // matches only two. Neither matches all six.
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "recall-target",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "recall-target-1",
            "prompt": "nabu reduce memory usage while profiling the index"
        }),
    )
    .unwrap();
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "recall-distractor",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "recall-distractor-1",
            "prompt": "nabu memory note"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    // Two terms: both events surface.
    let short = search_history(&home, "nabu memory", 10).unwrap();
    assert_eq!(short.len(), 2);

    // Six terms, more specific. AND semantics required every term in one event
    // and returned zero; OR keeps recall and bm25 ranks the event satisfying
    // more terms first.
    let long = search_history(
        &home,
        "nabu reduce memory usage performance optimization",
        10,
    )
    .unwrap();
    assert!(
        !long.is_empty(),
        "more-specific query must not collapse recall to zero"
    );
    assert_eq!(long[0].session_id, "recall-target");
}

#[test]
fn concept_expansion_retrieves_synonym_only_document_opt_in() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    // The target records the concept under a synonym only: it says "error" and
    // "latency", never the literal query words "bug" or "perf".
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "concept-target",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "concept-target-1",
            "prompt": "the index reported an error and high latency during rebuild"
        }),
    )
    .unwrap();
    // A distractor with neither the literal terms nor any synonym, so expansion
    // must not start matching unrelated documents.
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "concept-distractor",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "concept-distractor-1",
            "prompt": "session export wrote markdown to disk"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    let query = "bug perf";

    // Baseline lexical search has no concept knowledge: the synonym-only target
    // is invisible because neither "bug" nor "perf" appears in it.
    let baseline = search_history_page(
        &home,
        query,
        SearchOptions {
            mode: SearchMode::Lexical,
            expand_concepts: false,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert!(
        !baseline
            .results
            .iter()
            .any(|result| result.session_id == "concept-target"),
        "without expansion the synonym-only document must be missed"
    );

    // With the opt-in flag, "bug" expands to include "error" and "perf" to
    // include "latency", so the target is retrieved.
    let expanded = search_history_page(
        &home,
        query,
        SearchOptions {
            mode: SearchMode::Lexical,
            expand_concepts: true,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert!(expanded.expand_concepts, "page echoes the applied flag");
    assert!(
        expanded
            .results
            .iter()
            .any(|result| result.session_id == "concept-target"),
        "expansion must retrieve the synonym-only document"
    );
    // Expansion widens recall without dragging in the unrelated distractor.
    assert!(
        !expanded
            .results
            .iter()
            .any(|result| result.session_id == "concept-distractor"),
        "expansion must not start matching unrelated documents"
    );
}

#[test]
fn search_filters_apply_session_type_file_and_command() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "filter-session",
            "hook_event_name": "PreToolUse",
            "message_id": "command-filter-1",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "tool_name": "bash",
            "command": "cargo test --workspace",
            "input": "command filter marker"
        }),
    )
    .unwrap();
    ingest_hook_event(
        &home,
        Tool::Opencode,
        json!({
            "session_id": "file-session",
            "event": "file.edited",
            "id": "file-filter-1",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "path": "/tmp/nabu-fixture/src/auth.rs",
            "diff": "file filter marker"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    let command_results = search_history_filtered(
        &home,
        "command filter marker",
        SearchOptions {
            session_id: Some("filter-session".to_string()),
            canonical_type: Some("tool.call".to_string()),
            command: Some("cargo test".to_string()),
            limit: 10,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(command_results.len(), 1);
    assert_eq!(command_results[0].canonical_type, "tool.call");

    let wrong_command = search_history_filtered(
        &home,
        "command filter marker",
        SearchOptions {
            command: Some("npm install".to_string()),
            limit: 10,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert!(wrong_command.is_empty());

    let file_results = search_history_filtered(
        &home,
        "file filter marker",
        SearchOptions {
            file: Some("src/auth.rs".to_string()),
            canonical_type: Some("file.changed".to_string()),
            limit: 10,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(file_results.len(), 1);
    assert_eq!(file_results[0].session_id, "file-session");
}

// Provenance ref indexing (issue #1, item 8). Seeds two sessions that mention a
// PR ref and a commit SHA, indexes them, and asserts: the refs are queryable in
// event_refs, a `ref=` search returns the right events with full coordinates
// (invariant #13), and the commit prefix filter resolves abbreviated SHAs.
#[test]
fn provenance_refs_are_indexed_and_searchable() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    // Discovery session: a PR reference and a full commit SHA in one message.
    let sha = "100a8704bf3c2d1e5a6f7b8c9d0e1f2a3b4c5d6e";
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "discovery-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "prov-discovery-1",
            "prompt": format!("provenance marker: bug found, fixed in #54 at commit {sha}")
        }),
    )
    .unwrap();
    // An unrelated session that must not match the ref filters.
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "unrelated-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "prov-unrelated-1",
            "prompt": "provenance marker: nothing to see here, just prose"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    // The discovery session's event carries exactly the two refs in event_refs.
    let db_path = home.join("index").join("harness.db");
    let conn = Connection::open(&db_path).unwrap();
    let mut stored: Vec<(String, String)> = conn
        .prepare(
            "SELECT er.ref_kind, er.ref_value
             FROM event_refs er
             JOIN events e ON e.id = er.event_id
             WHERE e.session_id = 'discovery-session'
             ORDER BY er.ref_kind, er.ref_value",
        )
        .unwrap()
        .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
        .unwrap()
        .map(|row| row.unwrap())
        .collect();
    stored.sort();
    assert_eq!(
        stored,
        vec![
            ("commit".to_string(), sha.to_string()),
            ("pr".to_string(), "#54".to_string()),
        ]
    );
    // The unrelated session has no refs.
    let unrelated_refs: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM event_refs er
             JOIN events e ON e.id = er.event_id
             WHERE e.session_id = 'unrelated-session'",
            [],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(unrelated_refs, 0);
    drop(conn);

    // A `ref=#54` filter returns the discovery event with full coordinates.
    let pr_hits = search_history_filtered(
        &home,
        "provenance marker",
        SearchOptions {
            ref_filter: Some("#54".to_string()),
            limit: 10,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(pr_hits.len(), 1);
    let hit = &pr_hits[0];
    assert_eq!(hit.session_id, "discovery-session");
    // Invariant #13: the hit carries raw_file/raw_line/raw_offset/session_id.
    assert!(!hit.raw_file.is_empty());
    assert!(hit.raw_line > 0);
    assert!(hit.raw_offset.is_some());
    assert!(!hit.session_id.is_empty());

    // A bare PR number (`ref=54`) resolves to the same `#54` row.
    let pr_bare = search_history_filtered(
        &home,
        "provenance marker",
        SearchOptions {
            ref_filter: Some("54".to_string()),
            limit: 10,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(pr_bare.len(), 1);
    assert_eq!(pr_bare[0].session_id, "discovery-session");

    // An abbreviated commit SHA prefix matches the stored full SHA.
    let commit_hits = search_history_filtered(
        &home,
        "provenance marker",
        SearchOptions {
            ref_filter: Some("100a8704".to_string()),
            limit: 10,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(commit_hits.len(), 1);
    assert_eq!(commit_hits[0].session_id, "discovery-session");

    // A PR number that is not present yields nothing.
    let absent = search_history_filtered(
        &home,
        "provenance marker",
        SearchOptions {
            ref_filter: Some("#999".to_string()),
            limit: 10,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert!(absent.is_empty());
}

// The event_refs migration must backfill a pre-provenance index: an index built
// without the table (simulated by dropping it) regains its refs on the next
// open, without a full reindex.
#[test]
fn provenance_refs_backfill_on_open_of_legacy_index() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "legacy-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "legacy-prov-1",
            "prompt": "legacy backfill marker: shipped in #73"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    // Simulate a pre-provenance index: drop the event_refs table outright. A
    // raw Connection::open does not run the ensure_* migrations, so the table
    // stays absent until we reopen through open_index.
    let db_path = home.join("index").join("harness.db");
    {
        let conn = Connection::open(&db_path).unwrap();
        conn.execute_batch("DROP TABLE IF EXISTS event_refs;")
            .unwrap();
        let still_present: i64 = conn
            .query_row(
                "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE name = 'event_refs')",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(still_present, 0, "event_refs should be dropped");
    }

    // open_index runs ensure_event_refs_schema, which recreates the table and
    // backfills from the already-indexed events' searchable_text.
    let conn = open_index(&db_path).unwrap();
    let backfilled: Vec<(String, String)> = conn
        .prepare("SELECT ref_kind, ref_value FROM event_refs ORDER BY ref_kind, ref_value")
        .unwrap()
        .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
        .unwrap()
        .map(|row| row.unwrap())
        .collect();
    assert_eq!(backfilled, vec![("pr".to_string(), "#73".to_string())]);
    drop(conn);

    // The backfilled ref is searchable end-to-end.
    let hits = search_history_filtered(
        &home,
        "legacy backfill marker",
        SearchOptions {
            ref_filter: Some("#73".to_string()),
            limit: 10,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(hits.len(), 1);
    assert_eq!(hits[0].session_id, "legacy-session");
}

// P0 bug #2: a session-scoped search must fail open. Controlled by varying
// only session_id while holding the query fixed. Also covers invariant #13
// (every hit carries raw_file, raw_line, raw_offset, session_id) and that the
// filter never silently requires a tool arg.
#[test]
fn search_session_filter_fails_open_on_prefix_and_filename() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    // A full UUIDv7-style id (sanitizes to itself) and a session whose
    // filename-sanitized form differs from its canonical id.
    let full_id = "019d40b2-5250-7e40-9dc7-f2fb593bc2a8";
    let slashed_id = "team/raven-session";
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": full_id,
            "hook_event_name": "UserPromptSubmit",
            "message_id": "scope-alpha-1",
            "prompt": "session scope marker alpha"
        }),
    )
    .unwrap();
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": slashed_id,
            "hook_event_name": "UserPromptSubmit",
            "message_id": "scope-beta-1",
            "prompt": "session scope marker beta"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    let query = "session scope marker";
    let scoped = |session: &str| {
        search_history_filtered(
            &home,
            query,
            SearchOptions {
                session_id: Some(session.to_string()),
                limit: 10,
                ..SearchOptions::default()
            },
        )
        .unwrap()
    };

    // No session filter: both events are present.
    assert_eq!(search_history(&home, query, 10).unwrap().len(), 2);

    // Exact canonical id.
    let exact = scoped(full_id);
    assert_eq!(exact.len(), 1);
    assert_eq!(exact[0].session_id, full_id);

    // Short id prefix — the failing case from the issue (a present session that
    // returned empty under exact match). No tool arg supplied.
    let by_prefix = scoped("019d40b2");
    assert_eq!(by_prefix.len(), 1);
    assert_eq!(by_prefix[0].session_id, full_id);

    // Filename-sanitized form resolves back to the canonical id.
    let by_filename = scoped("team_raven-session");
    assert_eq!(by_filename.len(), 1);
    assert_eq!(by_filename[0].session_id, slashed_id);

    // A genuinely absent session still returns empty — correct, not false-empty.
    assert!(scoped("ffffffff-0000-0000-0000-000000000000").is_empty());

    // Invariant #13: every hit carries the full coordinate contract.
    let hit = &exact[0];
    assert!(!hit.raw_file.is_empty());
    assert!(hit.raw_line > 0);
    assert!(hit.raw_offset.is_some());
    assert!(!hit.session_id.is_empty());

    // Item 14: the native handoff command is populated for a real hit, names the
    // hit's raw_file, and addresses the hit's raw_line. The path is shell-quoted.
    let command = hit
        .native_command
        .as_deref()
        .expect("indexed hit carries a native handoff command");
    assert_eq!(
        command,
        format!("sed -n '{}p' '{}' | jq .", hit.raw_line, hit.raw_file)
    );
    assert!(command.contains(&format!("'{}p'", hit.raw_line)));
    assert!(command.ends_with("| jq ."));
}

// Item 14: the native handoff command must be a single, safe, correct shell
// command — the path shell-quoted (paths can contain spaces), the actual
// raw_line used, and the raw_file targeted. Invalid line numbers yield None.
#[test]
fn native_handoff_command_is_line_addressed_and_shell_quoted() {
    // Path with a space stays one argument via single quotes.
    let cmd = native_jsonl_line_command("/var/log/my sessions/raven.jsonl", 42)
        .expect("positive line yields a command");
    assert_eq!(
        cmd,
        "sed -n '42p' '/var/log/my sessions/raven.jsonl' | jq ."
    );
    assert!(cmd.contains("'42p'"));
    assert!(cmd.contains("'/var/log/my sessions/raven.jsonl'"));

    // Embedded single quote is escaped via the '\'' idiom, keeping one argument.
    let tricky = native_jsonl_line_command("/tmp/o'brien.jsonl", 3).unwrap();
    assert_eq!(tricky, "sed -n '3p' '/tmp/o'\\''brien.jsonl' | jq .");

    // Non-positive line numbers have no valid 1-based address.
    assert_eq!(native_jsonl_line_command("/tmp/a.jsonl", 0), None);
    assert_eq!(native_jsonl_line_command("/tmp/a.jsonl", -1), None);
}

#[test]
fn search_defaults_are_citation_first_and_full_payload_is_opt_in() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    ingest_hook_event(
            &home,
            Tool::Claude,
            json!({
                "session_id": "citation-session",
                "hook_event_name": "UserPromptSubmit",
                "message_id": "citation-1",
                "cwd": "/tmp/nabu-fixture",
                "project_root": "/tmp/nabu-fixture",
                "prompt": format!("{} needle-centered citation marker {}", "prefix ".repeat(80), "suffix ".repeat(80))
            }),
        )
        .unwrap();
    index_once(&home).unwrap();
    let db_path = home.join("index").join("harness.db");
    let conn = open_index(&db_path).unwrap();
    let payload_json: Option<String> = conn
        .query_row("SELECT payload_json FROM events LIMIT 1", [], |row| {
            row.get(0)
        })
        .unwrap();
    assert!(payload_json.is_none());

    let default_page = search_history_page(
        &home,
        "needle-centered citation marker",
        SearchOptions {
            max_snippet_chars: 48,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(default_page.returned, 1);
    assert_eq!(default_page.max_snippet_chars_applied, 48);
    assert!(default_page.results[0].payload.is_null());
    assert!(default_page.results[0].score > 0.0);
    assert!(default_page.results[0].snippet.contains("needle-centered"));
    assert!(default_page.results[0].snippet.chars().count() <= 48);

    let full_page = search_history_page(
        &home,
        "needle-centered citation marker",
        SearchOptions {
            include_payload: true,
            max_snippet_chars: 5_000,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(full_page.max_snippet_chars_applied, 1_000);
    assert!(full_page.results[0].payload.get("prompt").is_some());
}

#[test]
fn search_default_snippet_length_is_triage_sized() {
    assert_eq!(DEFAULT_SEARCH_SNIPPET_CHARS, 500);
    assert_eq!(SearchOptions::default().max_snippet_chars, 500);

    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "snippet-default-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "snippet-default-1",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "prompt": format!("{} triage needle marker {}", "lead ".repeat(120), "tail ".repeat(120))
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    // Omitting max_snippet_chars applies the default and yields a longer,
    // match-centered snippet than the previous 240-char default would have.
    let page =
        search_history_page(&home, "triage needle marker", SearchOptions::default()).unwrap();
    assert_eq!(page.max_snippet_chars_applied, 500);
    let snippet = &page.results[0].snippet;
    assert!(snippet.contains("triage needle marker"));
    let len = snippet.chars().count();
    assert!(
        len > 240,
        "default snippet should exceed the old 240 cap, got {len}"
    );
    assert!(
        len <= 500,
        "default snippet should honor the 500 cap, got {len}"
    );
}

#[test]
fn payload_hydration_uses_raw_offset_and_falls_back_to_line_scan() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    for line in 1..=4 {
        ingest_hook_event(
            &home,
            Tool::Claude,
            json!({
                "session_id": "offset-payload-session",
                "hook_event_name": "UserPromptSubmit",
                "message_id": format!("offset-payload-{line}"),
                "cwd": "/tmp/nabu-fixture",
                "project_root": "/tmp/nabu-fixture",
                "prompt": format!("offset payload marker {line}")
            }),
        )
        .unwrap();
    }
    index_once(&home).unwrap();

    let raw_path = canonical_raw_path(&home, Tool::Claude, "offset-payload-session");
    let raw_file = raw_path.display().to_string();
    let offset = raw_offset_for_line(&raw_path, 3) as i64;
    let scanned = raw_envelope_for_line_scan(&raw_path, 4).unwrap();
    let sought = raw_envelope_for_pointer(&raw_file, 4, Some(offset)).unwrap();
    let fallback = raw_envelope_for_pointer(&raw_file, 4, Some(offset + 1)).unwrap();

    assert_eq!(sought, scanned);
    assert_eq!(fallback, scanned);
    assert_eq!(
        payload_for_raw_pointer(&raw_file, 4, Some(offset))
            .unwrap()
            .get("prompt"),
        Some(&json!("offset payload marker 4"))
    );
}

#[test]
fn search_payload_hydration_uses_grouped_raw_offsets() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    for line in 1..=3 {
        ingest_hook_event(
            &home,
            Tool::Claude,
            json!({
                "session_id": "grouped-payload-session",
                "hook_event_name": "UserPromptSubmit",
                "message_id": format!("grouped-payload-{line}"),
                "cwd": "/tmp/nabu-fixture",
                "project_root": "/tmp/nabu-fixture",
                "prompt": format!("grouped payload shared marker {line}")
            }),
        )
        .unwrap();
    }
    index_once(&home).unwrap();

    let page = search_history_page(
        &home,
        "grouped payload shared marker",
        SearchOptions {
            include_payload: true,
            limit: 3,
            dedupe: false,
            ..SearchOptions::default()
        },
    )
    .unwrap();

    assert_eq!(page.returned, 3);
    let prompts = page
        .results
        .iter()
        .map(|result| {
            result
                .payload
                .get("prompt")
                .and_then(Value::as_str)
                .unwrap()
                .to_string()
        })
        .collect::<BTreeSet<_>>();
    assert_eq!(prompts.len(), 3);
    assert!(prompts.contains("grouped payload shared marker 1"));
    assert!(prompts.contains("grouped payload shared marker 2"));
    assert!(prompts.contains("grouped payload shared marker 3"));
}

#[test]
fn search_auto_falls_back_to_lexical_and_forced_hybrid_errors_without_semantic_backend() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "mode-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "mode-1",
            "prompt": "search mode lexical fallback marker"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    let auto_page = search_history_page(
        &home,
        "search mode lexical fallback marker",
        SearchOptions {
            mode: SearchMode::Auto,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(auto_page.mode_requested, SearchMode::Auto);
    assert_eq!(auto_page.mode_applied, SearchMode::Lexical);
    assert!(!auto_page.semantic_available);
    assert_eq!(auto_page.returned, 1);

    let lexical_page = search_history_page(
        &home,
        "search mode lexical fallback marker",
        SearchOptions {
            mode: SearchMode::Lexical,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(lexical_page.mode_applied, SearchMode::Lexical);
    assert_eq!(lexical_page.returned, 1);

    let error = search_history_page(
        &home,
        "search mode lexical fallback marker",
        SearchOptions {
            mode: SearchMode::Hybrid,
            ..SearchOptions::default()
        },
    )
    .unwrap_err();
    assert!(matches!(error, Error::SemanticUnavailable(_)));
}

#[test]
fn embedding_units_are_structured_and_exclude_tool_output_noise() {
    let payload = json!({
        "tool_name": "shell",
        "command": "cargo test --workspace",
        "status": "failed",
        "stdout": "very long compiler output that should remain lexical-only",
        "stderr": "more noisy output that should not become a vector unit"
    });
    let document = search_document_for_event(CanonicalType::ToolResult, &payload);

    let units = embedding_units_for_document(&document);

    assert_eq!(units.len(), 1);
    assert_eq!(units[0].kind, EmbeddingUnitKind::ToolIntent);
    assert!(units[0].text.contains("cargo test --workspace"));
    assert!(!units[0].text.contains("compiler output"));
    assert_eq!(units[0].text_hash, sha256_hex(units[0].text.as_bytes()));
}

#[test]
fn search_and_session_exclude_deltas_by_default_and_restore_on_opt_in() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "delta-session",
            "hook_event_name": "MessageDisplay",
            "message_id": "delta-message",
            "index": 0,
            "final": false,
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "delta": "delta-only fixture marker"
        }),
    )
    .unwrap();
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "delta-session",
            "hook_event_name": "MessageDisplay",
            "message_id": "final-message",
            "index": 1,
            "final": true,
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "message": "final fixture marker"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    // Default search excludes deltas. Under OR semantics the shared
    // "fixture"/"marker" terms also surface the non-delta final message, so
    // assert the delta is absent rather than an AND-coupled empty result.
    let default_search = search_history(&home, "delta-only fixture marker", 10).unwrap();
    assert!(default_search
        .iter()
        .all(|result| result.canonical_type != "assistant.delta"));
    let delta_search = search_history_page(
        &home,
        "delta-only fixture marker",
        SearchOptions {
            include_deltas: true,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(delta_search.results[0].canonical_type, "assistant.delta");

    let default_session = get_session_page(
        &home,
        Tool::Claude,
        "delta-session",
        SessionOptions::default(),
    )
    .unwrap();
    assert!(default_session
        .events
        .iter()
        .all(|event| event.canonical_type != "assistant.delta"));

    let full_session = get_session_page(
        &home,
        Tool::Claude,
        "delta-session",
        SessionOptions {
            include_deltas: true,
            ..SessionOptions::default()
        },
    )
    .unwrap();
    assert_eq!(full_session.events[0].canonical_type, "assistant.delta");
    assert!(
        export_session_markdown_with_options(&home, Tool::Claude, "delta-session", false)
            .unwrap()
            .contains("delta-only fixture marker")
    );
}

#[test]
fn session_context_window_clamps_and_wins_over_after_raw_line() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    for line in 1..=5 {
        ingest_hook_event(
            &home,
            Tool::Claude,
            json!({
                "session_id": "window-session",
                "hook_event_name": "UserPromptSubmit",
                "message_id": format!("window-{line}"),
                "cwd": "/tmp/nabu-fixture",
                "project_root": "/tmp/nabu-fixture",
                "prompt": format!("window marker line {line}")
            }),
        )
        .unwrap();
    }
    index_once(&home).unwrap();

    let window = get_session_page(
        &home,
        Tool::Claude,
        "window-session",
        SessionOptions {
            around_raw_line: Some(3),
            after_raw_line: Some(4),
            before: 1,
            after: 1,
            ..SessionOptions::default()
        },
    )
    .unwrap();
    assert_eq!(window.mode, "window");
    assert_eq!(
        window
            .events
            .iter()
            .map(|event| event.raw_line)
            .collect::<Vec<_>>(),
        vec![2, 3, 4]
    );

    let clamped = get_session_page(
        &home,
        Tool::Claude,
        "window-session",
        SessionOptions {
            around_raw_line: Some(1),
            before: 10,
            after: 0,
            ..SessionOptions::default()
        },
    )
    .unwrap();
    assert_eq!(clamped.events[0].raw_line, 1);
}

#[test]
fn session_page_cursor_resumes_without_gap_or_overlap() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    let total = 7usize;
    for line in 1..=total {
        ingest_hook_event(
            &home,
            Tool::Claude,
            json!({
                "session_id": "paged-session",
                "hook_event_name": "UserPromptSubmit",
                "message_id": format!("paged-{line}"),
                "cwd": "/tmp/nabu-fixture",
                "project_root": "/tmp/nabu-fixture",
                "prompt": format!("paged marker line {line}")
            }),
        )
        .unwrap();
    }
    index_once(&home).unwrap();

    // First page is smaller than the session, so it must truncate and hand back
    // a forward cursor.
    let first = get_session_page(
        &home,
        Tool::Claude,
        "paged-session",
        SessionOptions {
            limit_events: 3,
            ..SessionOptions::default()
        },
    )
    .unwrap();
    assert_eq!(first.mode, "page");
    assert!(first.truncated);
    assert_eq!(first.events.len(), 3);
    let cursor = first
        .next_after_raw_line
        .expect("truncated page must return a cursor");
    assert_eq!(cursor, first.events.last().unwrap().raw_line);

    // Walk the rest of the session through the cursor, collecting every raw_line.
    let mut seen: Vec<i64> = first.events.iter().map(|event| event.raw_line).collect();
    let mut after = cursor;
    loop {
        let page = get_session_page(
            &home,
            Tool::Claude,
            "paged-session",
            SessionOptions {
                limit_events: 3,
                after_raw_line: Some(after),
                ..SessionOptions::default()
            },
        )
        .unwrap();
        // No overlap: every line is strictly past the previous cursor.
        assert!(page.events.iter().all(|event| event.raw_line > after));
        seen.extend(page.events.iter().map(|event| event.raw_line));
        match page.next_after_raw_line {
            Some(next) => {
                assert!(page.truncated);
                after = next;
            }
            None => {
                assert!(!page.truncated);
                break;
            }
        }
    }

    // The concatenated pages reconstruct the session exactly: no gap, no overlap.
    let full: Vec<i64> = get_session_page(
        &home,
        Tool::Claude,
        "paged-session",
        SessionOptions {
            limit_events: 500,
            ..SessionOptions::default()
        },
    )
    .unwrap()
    .events
    .iter()
    .map(|event| event.raw_line)
    .collect();
    assert_eq!(seen, full);
    assert_eq!(seen.len(), total);
}

#[test]
fn search_dedupes_twins_only_at_retrieval_layer() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let source = temp.path().join("codex-sessions");
    init_home(&home).unwrap();
    fs::create_dir_all(&source).unwrap();

    let session_id = "019b0000-0000-7000-8000-000000000001";
    fs::write(
            source.join(format!("rollout-2026-06-18T00-00-00-{session_id}.jsonl")),
            format!(
                "{{\"timestamp\":\"2026-06-18T00:00:00Z\",\"type\":\"session_meta\",\"payload\":{{\"id\":\"{session_id}\",\"cwd\":\"/tmp/native-codex\"}}}}\n\
                 {{\"timestamp\":\"2026-06-18T00:00:01Z\",\"type\":\"response_item\",\"payload\":{{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{{\"type\":\"output_text\",\"text\":\"twinned codex answer marker\"}}]}}}}\n\
                 {{\"timestamp\":\"2026-06-18T00:00:01Z\",\"type\":\"event_msg\",\"payload\":{{\"type\":\"agent_message\",\"message\":\"twinned codex answer marker\"}}}}\n"
            ),
        )
        .unwrap();
    backfill_since(&home, Some(Tool::Codex), &source, None).unwrap();
    index_once(&home).unwrap();

    let deduped = search_history_page(
        &home,
        "twinned codex answer marker",
        SearchOptions::default(),
    )
    .unwrap();
    assert_eq!(deduped.results.len(), 1);
    assert_eq!(deduped.results[0].also_at.len(), 1);

    let not_deduped = search_history_page(
        &home,
        "twinned codex answer marker",
        SearchOptions {
            dedupe: false,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(not_deduped.results.len(), 2);

    let session =
        get_session_page(&home, Tool::Codex, session_id, SessionOptions::default()).unwrap();
    assert_eq!(
        session
            .events
            .iter()
            .filter(|event| event.text.contains("twinned codex answer marker"))
            .count(),
        2
    );
}

#[test]
fn search_dedupes_adjacent_whitespace_divergent_twins() {
    // Two adjacent native captures of the same assistant answer that differ
    // only by internal whitespace (one has the body split across a newline,
    // the other collapses it to a single space). Before normalization the
    // retrieval_key was sha256 over the raw rendered text, so the embedded
    // whitespace produced two distinct hashes and the twin survived dedupe.
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let source = temp.path().join("codex-sessions");
    init_home(&home).unwrap();
    fs::create_dir_all(&source).unwrap();

    let session_id = "019b0000-0000-7000-8000-000000000077";
    fs::write(
        source.join(format!("rollout-2026-06-18T00-00-00-{session_id}.jsonl")),
        format!(
            "{{\"timestamp\":\"2026-06-18T00:00:00Z\",\"type\":\"session_meta\",\"payload\":{{\"id\":\"{session_id}\",\"cwd\":\"/tmp/native-codex\"}}}}\n\
             {{\"timestamp\":\"2026-06-18T00:00:01Z\",\"type\":\"response_item\",\"payload\":{{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{{\"type\":\"output_text\",\"text\":\"whitespace twin answer\\nmarker line\"}}]}}}}\n\
             {{\"timestamp\":\"2026-06-18T00:00:01Z\",\"type\":\"event_msg\",\"payload\":{{\"type\":\"agent_message\",\"message\":\"whitespace twin answer   marker line\"}}}}\n"
        ),
    )
    .unwrap();
    backfill_since(&home, Some(Tool::Codex), &source, None).unwrap();
    index_once(&home).unwrap();

    let deduped = search_history_page(
        &home,
        "whitespace twin answer marker",
        SearchOptions::default(),
    )
    .unwrap();
    assert_eq!(
        deduped.results.len(),
        1,
        "whitespace-divergent twin must collapse to one hit"
    );
    assert_eq!(
        deduped.results[0].also_at.len(),
        1,
        "the collapsed twin's raw_line must be recorded in also_at"
    );
    let primary_line = deduped.results[0].raw_line;
    let collapsed_line = deduped.results[0].also_at[0];
    assert_ne!(
        primary_line, collapsed_line,
        "also_at records the other event's raw_line, not the primary's"
    );

    let not_deduped = search_history_page(
        &home,
        "whitespace twin answer marker",
        SearchOptions {
            dedupe: false,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(
        not_deduped.results.len(),
        2,
        "without dedupe both adjacent twins remain visible"
    );
}

#[test]
fn search_dedupe_keeps_distinct_events_separate() {
    // Guard against over-collapsing: two genuinely different assistant answers
    // in the same session must NOT be merged by the normalized retrieval_key.
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let source = temp.path().join("codex-sessions");
    init_home(&home).unwrap();
    fs::create_dir_all(&source).unwrap();

    let session_id = "019b0000-0000-7000-8000-000000000088";
    fs::write(
        source.join(format!("rollout-2026-06-18T00-00-00-{session_id}.jsonl")),
        format!(
            "{{\"timestamp\":\"2026-06-18T00:00:00Z\",\"type\":\"session_meta\",\"payload\":{{\"id\":\"{session_id}\",\"cwd\":\"/tmp/native-codex\"}}}}\n\
             {{\"timestamp\":\"2026-06-18T00:00:01Z\",\"type\":\"response_item\",\"payload\":{{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{{\"type\":\"output_text\",\"text\":\"distinct marker alpha answer\"}}]}}}}\n\
             {{\"timestamp\":\"2026-06-18T00:00:02Z\",\"type\":\"response_item\",\"payload\":{{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{{\"type\":\"output_text\",\"text\":\"distinct marker beta answer\"}}]}}}}\n"
        ),
    )
    .unwrap();
    backfill_since(&home, Some(Tool::Codex), &source, None).unwrap();
    index_once(&home).unwrap();

    let deduped =
        search_history_page(&home, "distinct marker answer", SearchOptions::default()).unwrap();
    assert_eq!(
        deduped.results.len(),
        2,
        "distinct answers must not collapse under normalized dedupe"
    );
    for result in &deduped.results {
        assert!(result.also_at.is_empty());
    }
}

#[test]
fn doctor_fast_and_deep_report_their_integrity_scope() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "doctor-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "doctor-1",
            "prompt": "doctor marker"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    let fast = doctor_with_options(&home, false);
    assert_eq!(fast.level, "fast");
    assert_eq!(fast.integrity, "structural");
    assert!(fast.index.ok);
    assert!(fast.index.message.contains("core tables"));
    assert!(fast.stats.is_none());
    assert!(fast.latest_captured_events["claude"].is_some());

    let deep = doctor_with_options(&home, true);
    assert_eq!(deep.level, "deep");
    assert_eq!(deep.integrity, "full");
    assert!(deep.index.message.contains("integrity_check"));
    assert_eq!(deep.stats.unwrap().events, 1);

    let db_path = home.join("index").join("harness.db");
    let conn = Connection::open(&db_path).unwrap();
    let plan = conn
            .query_row(
                "EXPLAIN QUERY PLAN
                 SELECT tool, session_id, canonical_type, captured_at, searchable_text, raw_file, raw_line, raw_offset
                 FROM events
                 WHERE tool = 'claude'
                 ORDER BY captured_at DESC, id DESC
                 LIMIT 1",
                [],
                |row| row.get::<_, String>(3),
            )
            .unwrap();
    assert!(plan.contains("idx_events_tool_captured"), "{plan}");
}

#[test]
fn schema_helpers_reject_non_identifier_names() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    let db_path = home.join("index").join("harness.db");
    let conn = open_index(&db_path).unwrap();

    let count_error = table_count(&conn, &db_path, "events;DROP TABLE events").unwrap_err();
    assert!(matches!(count_error, Error::Validation(_)));

    let column_error =
        crate::db::ensure_table_column(&conn, &db_path, "checkpoints", "bad-column", "TEXT")
            .unwrap_err();
    assert!(matches!(column_error, Error::Validation(_)));
}

#[cfg(unix)]
#[test]
fn directory_size_does_not_follow_symlink_cycles() {
    use std::os::unix::fs::symlink;

    let temp = tempdir().unwrap();
    let root = temp.path().join("root");
    fs::create_dir_all(&root).unwrap();
    fs::write(root.join("payload.txt"), b"12345").unwrap();
    symlink(&root, root.join("cycle")).unwrap();

    assert_eq!(directory_size(&root).unwrap(), 5);
}

#[test]
fn set_opencode_server_url_round_trips_and_preserves_other_settings() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    let config = home.join("config.toml");

    // A config with an unrelated key and a comment that must survive edits.
    fs::write(
        &config,
        "schema_version = 1\n# keep me\n\n[opencode]\n# server_url = \"http://127.0.0.1:4096\"\n",
    )
    .unwrap();

    // Set activates the commented seed and is readable through the reader.
    // Read via the config parser directly so ambient env vars can't shadow it.
    set_opencode_server_url(&home, Some("http://localhost:9999")).unwrap();
    assert_eq!(
        crate::config::read_opencode_server_url_from_config(&config)
            .unwrap()
            .as_deref(),
        Some("http://localhost:9999")
    );
    let after_set = fs::read_to_string(&config).unwrap();
    assert!(after_set.contains("schema_version = 1"));
    assert!(after_set.contains("# keep me"));
    assert!(after_set.contains("server_url = \"http://localhost:9999\""));

    // Idempotent: setting the same value does not rewrite the file.
    set_opencode_server_url(&home, Some("http://localhost:9999")).unwrap();
    assert_eq!(fs::read_to_string(&config).unwrap(), after_set);

    // Clear removes the active line but keeps the rest.
    set_opencode_server_url(&home, None).unwrap();
    assert_eq!(
        crate::config::read_opencode_server_url_from_config(&config).unwrap(),
        None
    );
    let after_clear = fs::read_to_string(&config).unwrap();
    assert!(after_clear.contains("schema_version = 1"));
    assert!(after_clear.contains("# keep me"));
    assert!(!after_clear.contains("server_url = \"http://localhost:9999\""));
}

#[test]
fn set_opencode_server_url_appends_section_when_absent() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    // No config.toml yet — writer must create it and append the section.
    set_opencode_server_url(&home, Some("http://127.0.0.1:4096")).unwrap();
    assert_eq!(
        crate::config::read_opencode_server_url_from_config(&home.join("config.toml"))
            .unwrap()
            .as_deref(),
        Some("http://127.0.0.1:4096")
    );
}

#[test]
fn set_opencode_server_url_rejects_invalid_values_without_rewriting_config() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();
    let config = home.join("config.toml");
    let before = fs::read_to_string(&config).unwrap();

    for invalid in [
        "https://127.0.0.1:4096",
        "http://",
        "http://127.0.0.1:not-a-port",
        "http://127.0.0.1:4096\nserver_url = \"http://evil\"",
        "http://127.0.0.1:4096\\broken",
        " http://127.0.0.1:4096",
    ] {
        let error = set_opencode_server_url(&home, Some(invalid)).unwrap_err();
        assert!(matches!(error, Error::Validation(_)), "{invalid}: {error}");
        assert_eq!(fs::read_to_string(&config).unwrap(), before);
    }
}

#[test]
fn search_treats_hyphenated_queries_as_plain_text() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "hyphen-search-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "hyphen-search-1",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "prompt": "nabu project setup goals and tasks"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    let results = search_history(&home, "nabu project setup goals and tasks", 10).unwrap();

    assert_eq!(results.len(), 1);
    assert_eq!(results[0].session_id, "hyphen-search-session");
}

#[test]
fn search_rejects_queries_without_searchable_text() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    let error = search_history(&home, "-- : ()", 10).unwrap_err();

    assert!(
        matches!(error, Error::Validation(message) if message == "query must contain searchable text")
    );
}

#[test]
fn codex_exec_json_ingest_preserves_delta_order_and_usage_metadata() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let repo = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
    init_home(&home).unwrap();

    let report = ingest_file(
        &home,
        Tool::Codex,
        Source::ExecJson,
        &repo.join("fixtures/codex/exec-json.jsonl"),
    )
    .unwrap();

    assert_eq!(report.appended_events, 5);
    let raw_path = canonical_raw_path(&home, Tool::Codex, "codex-exec-stream-session");
    let envelopes = raw_envelopes(&raw_path);
    assert!(envelopes
        .iter()
        .all(|event| event.source == Source::ExecJson));
    let deltas = envelopes
        .iter()
        .filter(|event| event.canonical_type == CanonicalType::AssistantDelta)
        .collect::<Vec<_>>();
    assert_eq!(
        deltas
            .iter()
            .map(|event| event.sequence)
            .collect::<Vec<_>>(),
        vec![Some(0), Some(1)]
    );
    assert!(deltas[0]
        .payload
        .get("delta")
        .and_then(Value::as_str)
        .unwrap()
        .ends_with("delta one"));
    assert!(deltas[1]
        .payload
        .get("delta")
        .and_then(Value::as_str)
        .unwrap()
        .ends_with("delta two"));

    index_once(&home).unwrap();
    let usage_results = search_history_page(
        &home,
        "total_tokens 42",
        SearchOptions {
            tool: Some(Tool::Codex),
            limit: 10,
            ..SearchOptions::default()
        },
    )
    .unwrap();
    assert_eq!(usage_results.results.len(), 1);
    assert_eq!(usage_results.results[0].canonical_type, "session.ended");
    let export =
        export_session_jsonl_with_options(&home, Tool::Codex, "codex-exec-stream-session", false)
            .unwrap();
    assert!(export.contains("\"total_tokens\":42"));
}

#[test]
fn codex_app_server_ingest_preserves_jsonrpc_payloads_and_delta_order() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let repo = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
    init_home(&home).unwrap();

    let report = ingest_file(
        &home,
        Tool::Codex,
        Source::AppServer,
        &repo.join("fixtures/codex/app-server-notifications.jsonl"),
    )
    .unwrap();

    assert_eq!(report.appended_events, 6);
    let raw_path = canonical_raw_path(&home, Tool::Codex, "codex-app-server-session");
    let envelopes = raw_envelopes(&raw_path);
    assert!(envelopes
        .iter()
        .all(|event| event.source == Source::AppServer));
    assert_eq!(envelopes[0].source_event_type, "thread/started");
    assert!(envelopes[0].payload.get("jsonrpc").is_some());
    assert!(envelopes
        .iter()
        .any(|event| event.canonical_type == CanonicalType::ToolCall));
    let deltas = envelopes
        .iter()
        .filter(|event| event.canonical_type == CanonicalType::AssistantDelta)
        .collect::<Vec<_>>();
    assert_eq!(
        deltas
            .iter()
            .map(|event| event.sequence)
            .collect::<Vec<_>>(),
        vec![Some(0), Some(1)]
    );
    assert!(deltas.iter().all(|event| event
        .source_event_id
        .as_deref()
        .unwrap()
        .contains(":delta")));
}

#[test]
fn codex_streaming_and_hook_identity_dedupe_same_event_but_keep_deltas() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let stream = temp.path().join("codex-stream.jsonl");
    let session_id = "codex-stream-identity-session";
    init_home(&home).unwrap();

    ingest_hook_event(
        &home,
        Tool::Codex,
        json!({
            "session_id": session_id,
            "type": "response_item",
            "payload": {
                "id": "codex-stream-shared-item",
                "type": "message",
                "role": "user",
                "content": [{"type": "input_text", "text": "codex stream shared identity marker"}]
            }
        }),
    )
    .unwrap();
    fs::write(
            &stream,
            format!(
                "{}\n{}\n",
                serde_json::to_string(&json!({
                    "timestamp": "2026-06-18T10:00:00Z",
                    "type": "item.completed",
                    "thread_id": session_id,
                    "item": {
                        "id": "codex-stream-shared-item",
                        "type": "message",
                        "role": "user",
                        "content": [{"type": "input_text", "text": "codex stream shared identity marker"}]
                    }
                }))
                .unwrap(),
                serde_json::to_string(&json!({
                    "timestamp": "2026-06-18T10:00:01Z",
                    "type": "item/agentMessage/delta",
                    "thread_id": session_id,
                    "turn_id": "codex-stream-turn",
                    "message_id": "codex-stream-delta-message",
                    "sequence": 0,
                    "delta": "codex stream granularity marker"
                }))
                .unwrap()
            ),
        )
        .unwrap();

    let report = ingest_file(&home, Tool::Codex, Source::ExecJson, &stream).unwrap();

    assert_eq!(report.appended_events, 1);
    let envelopes = raw_envelopes(&canonical_raw_path(&home, Tool::Codex, session_id));
    assert_eq!(
        envelopes
            .iter()
            .filter(|event| event.source_event_id.as_deref() == Some("codex-stream-shared-item"))
            .count(),
        1
    );
    assert!(envelopes
        .iter()
        .any(|event| event.canonical_type == CanonicalType::AssistantDelta));
}

#[test]
fn opencode_server_messages_reconcile_gaps_without_spool_copy() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let session_id = "opencode-server-reconcile-session";
    init_home(&home).unwrap();
    ingest_hook_event(
        &home,
        Tool::Opencode,
        json!({
            "session_id": session_id,
            "hook_event_name": "message.updated",
            "id": "opencode-server-shared-message",
            "role": "assistant",
            "text": "opencode server shared marker"
        }),
    )
    .unwrap();

    let report = ingest_opencode_server_messages(
        &home,
        session_id,
        json!([
            {
                "id": "opencode-server-shared-message",
                "sessionID": session_id,
                "role": "assistant",
                "text": "opencode server shared marker"
            },
            {
                "id": "opencode-server-gap-message",
                "sessionID": session_id,
                "role": "assistant",
                "worktree": "/Users/example/opencode-server-worktree",
                "parts": [
                    {
                        "id": "opencode-server-gap-part",
                        "type": "text",
                        "text": "opencode server recovered part marker"
                    }
                ]
            }
        ]),
    )
    .unwrap();

    assert_eq!(report.appended_events, 1);
    assert!(!home.join("spool/opencode-api").exists());
    let envelopes = raw_envelopes(&canonical_raw_path(&home, Tool::Opencode, session_id));
    assert_eq!(
        envelopes
            .iter()
            .filter(
                |event| event.source_event_id.as_deref() == Some("opencode-server-shared-message")
            )
            .count(),
        1
    );
    let gap = envelopes
        .iter()
        .find(|event| {
            event.source_event_type == "message.part.updated"
                && event.payload.pointer("/part/text").and_then(Value::as_str)
                    == Some("opencode server recovered part marker")
        })
        .unwrap();
    assert_eq!(
        gap.project_root.as_deref(),
        Some("/Users/example/opencode-server-worktree")
    );
    assert_eq!(
        gap.cwd.as_deref(),
        Some("/Users/example/opencode-server-worktree")
    );
    assert!(envelopes.iter().any(|event| {
        event.source_event_type == "message.part.updated"
            && event.payload.pointer("/part/text").and_then(Value::as_str)
                == Some("opencode server recovered part marker")
    }));
}

fn raw_line_count(path: &Path) -> usize {
    fs::read_to_string(path).unwrap().lines().count()
}

fn dedupe_sidecar_entry_count(sidecar: &DedupeSidecarFiles) -> usize {
    fs::read_dir(&sidecar.buckets_dir)
        .unwrap()
        .map(|entry| raw_line_count(&entry.unwrap().path()))
        .sum()
}

fn raw_envelopes(path: &Path) -> Vec<EventEnvelope> {
    fs::read_to_string(path)
        .unwrap()
        .lines()
        .map(|line| serde_json::from_str(line).unwrap())
        .collect()
}

fn raw_offset_for_line(path: &Path, zero_based_line: usize) -> u64 {
    let content = fs::read_to_string(path).unwrap();
    content
        .lines()
        .take(zero_based_line)
        .map(|line| line.len() as u64 + 1)
        .sum()
}

fn checkpoint_row_count(home: &Path) -> i64 {
    let db_path = home.join("index").join("harness.db");
    let conn = Connection::open(db_path).unwrap();
    conn.query_row("SELECT COUNT(*) FROM checkpoints", [], |row| row.get(0))
        .unwrap()
}

fn checkpoint_sidecar_count(home: &Path) -> usize {
    let dir = home.join("checkpoints");
    fs::read_dir(dir)
        .unwrap()
        .filter(|entry| {
            entry
                .as_ref()
                .ok()
                .map(|entry| entry.path().is_file())
                .unwrap_or(false)
        })
        .count()
}

fn discontinuity_count(home: &Path, tool: Tool, session_id: &str, reason: &str) -> usize {
    raw_envelopes(&canonical_raw_path(home, tool, session_id))
        .into_iter()
        .filter(|event| {
            event.canonical_type == CanonicalType::SourceDiscontinuity
                && event.payload.get("reason").and_then(Value::as_str) == Some(reason)
        })
        .count()
}

fn claude_user_line(session_id: &str, uuid: &str, text: &str) -> String {
    serde_json::to_string(&json!({
        "type": "user",
        "sessionId": session_id,
        "uuid": uuid,
        "timestamp": "2026-06-18T12:00:00.000Z",
        "cwd": "/tmp/native-claude",
        "message": {
            "role": "user",
            "content": text
        }
    }))
    .unwrap()
        + "\n"
}

fn assert_reconciles_claude(home: &Path, temp_root: &Path) {
    let source = temp_root.join("reconcile-claude");
    fs::create_dir_all(&source).unwrap();
    let session_id = "aaaaaaa1-aaaa-4aaa-8aaa-aaaaaaaaaaa1";
    ingest_hook_event(
        home,
        Tool::Claude,
        json!({
            "session_id": session_id,
            "hook_event_name": "UserPromptSubmit",
            "event_id": "claude-reconcile-shared",
            "prompt": "claude reconcile shared marker"
        }),
    )
    .unwrap();
    ingest_hook_event(
        home,
        Tool::Claude,
        json!({
            "session_id": session_id,
            "hook_event_name": "MessageDisplay",
            "message_id": "claude-reconcile-delta",
            "index": 0,
            "final": false,
            "delta": "claude reconcile granularity marker"
        }),
    )
    .unwrap();
    fs::write(
        source.join(format!("{session_id}.jsonl")),
        format!(
            "{}{}{}",
            claude_user_line(
                session_id,
                "claude-reconcile-shared",
                "claude reconcile shared marker"
            ),
            serde_json::to_string(&json!({
                "type": "assistant",
                "sessionId": session_id,
                "timestamp": "2026-06-18T12:00:01.000Z",
                "message": {
                    "id": "claude-reconcile-gap",
                    "role": "assistant",
                    "content": [{"type": "text", "text": "claude reconcile gap marker"}]
                }
            }))
            .unwrap()
                + "\n",
            serde_json::to_string(&json!({
                "type": "assistant",
                "sessionId": session_id,
                "timestamp": "2026-06-18T12:00:02.000Z",
                "message": {
                    "id": "claude-reconcile-final",
                    "role": "assistant",
                    "content": [{"type": "text", "text": "claude reconcile granularity marker"}]
                }
            }))
            .unwrap()
                + "\n"
        ),
    )
    .unwrap();

    let report = backfill_since(home, Some(Tool::Claude), &source, None).unwrap();
    assert_eq!(report.appended_events, 2);
    let envelopes = raw_envelopes(&canonical_raw_path(home, Tool::Claude, session_id));
    assert_eq!(
        envelopes
            .iter()
            .filter(|event| event.source_event_id.as_deref() == Some("claude-reconcile-shared"))
            .count(),
        1
    );
    assert!(envelopes.iter().any(|event| {
        event.canonical_type == CanonicalType::AssistantMessage
            && event.source_event_id.as_deref() == Some("claude-reconcile-gap")
    }));
    assert!(envelopes
        .iter()
        .any(|event| event.canonical_type == CanonicalType::AssistantDelta));
    assert!(envelopes.iter().any(
        |event| event.canonical_type == CanonicalType::AssistantMessage
            && event.source_event_id.as_deref() == Some("claude-reconcile-final")
    ));
}

fn assert_reconciles_codex(home: &Path, temp_root: &Path) {
    let source = temp_root.join("reconcile-codex");
    fs::create_dir_all(&source).unwrap();
    let session_id = "bbbbbbb2-bbbb-4bbb-8bbb-bbbbbbbbbbb2";
    ingest_hook_event(
        home,
        Tool::Codex,
        json!({
            "session_id": session_id,
            "type": "response_item",
            "payload": {
                "id": "codex-reconcile-shared",
                "type": "message",
                "role": "user",
                "content": [{"type": "input_text", "text": "codex reconcile shared marker"}]
            }
        }),
    )
    .unwrap();
    fs::write(
            source.join(format!("rollout-2026-06-18T12-00-00-{session_id}.jsonl")),
            "{\"timestamp\":\"2026-06-18T12:00:00.000Z\",\"type\":\"response_item\",\"payload\":{\"id\":\"codex-reconcile-shared\",\"type\":\"message\",\"role\":\"user\",\"content\":[{\"type\":\"input_text\",\"text\":\"codex reconcile shared marker\"}]}}\n\
                 {\"timestamp\":\"2026-06-18T12:00:01.000Z\",\"type\":\"response_item\",\"payload\":{\"id\":\"codex-reconcile-gap\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"codex reconcile gap marker\"}]}}\n",
        )
        .unwrap();

    let report = backfill_since(home, Some(Tool::Codex), &source, None).unwrap();
    assert_eq!(report.appended_events, 1);
    let envelopes = raw_envelopes(&canonical_raw_path(home, Tool::Codex, session_id));
    assert_eq!(
        envelopes
            .iter()
            .filter(|event| event.source_event_id.as_deref() == Some("codex-reconcile-shared"))
            .count(),
        1
    );
    assert!(envelopes
        .iter()
        .any(|event| event.source_event_id.as_deref() == Some("codex-reconcile-gap")));
}

fn assert_reconciles_opencode(home: &Path, temp_root: &Path) {
    let root = temp_root.join("reconcile-opencode");
    let session_id = "ccccccc3-cccc-4ccc-8ccc-ccccccccccc3";
    let message_dir = root.join("storage/message").join(session_id);
    fs::create_dir_all(&message_dir).unwrap();
    ingest_hook_event(
        home,
        Tool::Opencode,
        json!({
            "session_id": session_id,
            "event": "message.updated",
            "id": "opencode-reconcile-shared",
            "text": "opencode reconcile shared marker"
        }),
    )
    .unwrap();
    fs::write(
        message_dir.join("opencode-reconcile-shared.json"),
        serde_json::to_string_pretty(&json!({
            "id": "opencode-reconcile-shared",
            "sessionID": session_id,
            "role": "assistant",
            "text": "opencode reconcile shared marker"
        }))
        .unwrap(),
    )
    .unwrap();
    fs::write(
        message_dir.join("opencode-reconcile-gap.json"),
        serde_json::to_string_pretty(&json!({
            "id": "opencode-reconcile-gap",
            "sessionID": session_id,
            "role": "assistant",
            "text": "opencode reconcile gap marker"
        }))
        .unwrap(),
    )
    .unwrap();

    let report = backfill_since(home, Some(Tool::Opencode), &root, None).unwrap();
    assert_eq!(report.appended_events, 1);
    let envelopes = raw_envelopes(&canonical_raw_path(home, Tool::Opencode, session_id));
    assert_eq!(
        envelopes
            .iter()
            .filter(|event| event.source_event_id.as_deref() == Some("opencode-reconcile-shared"))
            .count(),
        1
    );
    assert!(envelopes
        .iter()
        .any(|event| event.source_event_id.as_deref() == Some("opencode-reconcile-gap")));
}

#[test]
fn summary_kind_classifies_phase_handover_types_only() {
    // The summary-bearing canonical types that actually exist.
    assert_eq!(
        CanonicalType::SessionStarted.summary_kind(),
        Some(SummaryKind::SessionStart)
    );
    assert_eq!(
        CanonicalType::SessionEnded.summary_kind(),
        Some(SummaryKind::SessionEnd)
    );
    assert_eq!(
        CanonicalType::CompactionAfter.summary_kind(),
        Some(SummaryKind::Compaction)
    );

    // Ordinary events and non-summary lifecycle events are not flagged.
    for canonical_type in [
        CanonicalType::SessionResumed,
        CanonicalType::UserMessage,
        CanonicalType::AssistantDelta,
        CanonicalType::AssistantMessage,
        CanonicalType::ToolCall,
        CanonicalType::ToolResult,
        CanonicalType::PermissionRequested,
        CanonicalType::PermissionReplied,
        CanonicalType::FileChanged,
        CanonicalType::CompactionBefore,
        CanonicalType::SourceDiscontinuity,
        CanonicalType::Error,
    ] {
        assert_eq!(
            canonical_type.summary_kind(),
            None,
            "{canonical_type:?} must not be a summary"
        );
    }

    // String resolution matches the typed classifier; unknown types are None.
    assert_eq!(
        summary_kind_for_canonical_str("session.started"),
        Some(SummaryKind::SessionStart)
    );
    assert_eq!(summary_kind_for_canonical_str("user.message"), None);
    assert_eq!(summary_kind_for_canonical_str("not.a.real.type"), None);
}

#[test]
fn search_flags_summary_events_and_not_ordinary_events() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    // A session-start handover (summary-bearing canonical type).
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "summary-surface-session",
            "hook_event_name": "SessionStart",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "matter": "phase handover surfacing marker"
        }),
    )
    .unwrap();
    // An ordinary user message sharing the search term.
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "summary-surface-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "summary-surface-user-1",
            "cwd": "/tmp/nabu-fixture",
            "project_root": "/tmp/nabu-fixture",
            "prompt": "phase handover surfacing marker followup"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    let results = search_history(&home, "phase handover surfacing marker", 10).unwrap();
    assert!(results.len() >= 2, "expected both events, got {results:?}");

    let summary_hit = results
        .iter()
        .find(|result| result.canonical_type == "session.started")
        .expect("session.started hit present");
    assert_eq!(summary_hit.summary_kind, Some(SummaryKind::SessionStart));
    // Invariant #13: coordinates always present on every hit.
    assert!(!summary_hit.raw_file.is_empty());
    assert!(summary_hit.raw_offset.is_some());
    assert_eq!(summary_hit.session_id, "summary-surface-session");

    let ordinary_hit = results
        .iter()
        .find(|result| result.canonical_type == "user.message")
        .expect("user.message hit present");
    assert_eq!(ordinary_hit.summary_kind, None);
}

#[test]
fn session_page_flags_summary_events() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "summary-page-session",
            "hook_event_name": "SessionStart",
            "cwd": "/tmp/nabu-fixture",
            "matter": "session page summary marker"
        }),
    )
    .unwrap();
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "summary-page-session",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "summary-page-user-1",
            "cwd": "/tmp/nabu-fixture",
            "prompt": "session page ordinary marker"
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    let page = get_session_page(
        &home,
        Tool::Claude,
        "summary-page-session",
        SessionOptions::default(),
    )
    .unwrap();

    let started = page
        .events
        .iter()
        .find(|event| event.canonical_type == "session.started")
        .expect("session.started event present");
    assert_eq!(started.summary_kind, Some(SummaryKind::SessionStart));

    let user = page
        .events
        .iter()
        .find(|event| event.canonical_type == "user.message")
        .expect("user.message event present");
    assert_eq!(user.summary_kind, None);
}

// Issue #1 item 7: list_sessions must carry triage metadata derived from
// already-indexed tables so a caller can pick a session without loading it.
// Controlled by seeding a multi-event session with a known first prompt, a
// known tool-name mix, and a final compaction, then asserting the derived
// fields reflect exactly that.
#[test]
fn list_sessions_surfaces_triage_metadata_for_multi_event_session() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    // A rich Claude session: first user prompt, three tool calls (bash x2,
    // edit x1), then a compaction. The first prompt is the triage signal.
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "triage-rich",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "triage-rich-prompt",
            "cwd": "/tmp/nabu-triage",
            "project_root": "/tmp/nabu-triage",
            "prompt": "Refactor the auth module and add tests"
        }),
    )
    .unwrap();
    // A later prompt must NOT win: first_user_prompt is the earliest one.
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "triage-rich",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "triage-rich-prompt-2",
            "prompt": "Now also update the README"
        }),
    )
    .unwrap();
    for (idx, tool_name, command) in [
        (0, "bash", "cargo test"),
        (1, "bash", "cargo clippy"),
        (2, "edit", ""),
    ] {
        ingest_hook_event(
            &home,
            Tool::Claude,
            json!({
                "session_id": "triage-rich",
                "hook_event_name": "PreToolUse",
                "message_id": format!("triage-rich-tool-{idx}"),
                "tool_name": tool_name,
                "command": command
            }),
        )
        .unwrap();
    }
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "triage-rich",
            "hook_event_name": "PreCompact",
            "message_id": "triage-rich-compact",
            "trigger": "auto"
        }),
    )
    .unwrap();

    // A second session that edits two files (one file twice) so top_files is
    // exercised. Opencode's file.edited maps to file.changed which records the
    // 'edited' relationship that top_files counts.
    ingest_hook_event(
        &home,
        Tool::Opencode,
        json!({
            "hook_event_name": "session.created",
            "id": "triage-files",
            "directory": "/tmp/nabu-triage"
        }),
    )
    .unwrap();
    for (idx, path) in [
        "/tmp/nabu-triage/src/auth.rs",
        "/tmp/nabu-triage/src/auth.rs",
        "/tmp/nabu-triage/src/lib.rs",
    ]
    .into_iter()
    .enumerate()
    {
        // Distinct `diff` per edit so the two auth.rs edits are distinct events
        // (byte-identical events dedupe by design).
        ingest_hook_event(
            &home,
            Tool::Opencode,
            json!({
                "hook_event_name": "file.edited",
                "sessionID": "triage-files",
                "path": path,
                "diff": format!("edit marker {idx}")
            }),
        )
        .unwrap();
    }
    index_once(&home).unwrap();

    let sessions = list_sessions(&home, None, None, None, 20).unwrap();

    let rich = sessions
        .iter()
        .find(|session| session.session_id == "triage-rich")
        .expect("rich session listed");
    // Triage from the list row alone: what it was about, what it did, how it
    // ended.
    assert_eq!(
        rich.first_user_prompt.as_deref(),
        Some("Refactor the auth module and add tests")
    );
    assert_eq!(
        rich.last_canonical_type.as_deref(),
        Some("compaction.before")
    );
    assert_eq!(rich.compaction_count, 1);
    assert_eq!(rich.tool_event_count, 3);
    // bash (2) outranks edit (1); ties would break alphabetically.
    assert_eq!(
        rich.top_tools,
        vec![
            ToolUsage {
                tool_name: "bash".to_string(),
                count: 2,
            },
            ToolUsage {
                tool_name: "edit".to_string(),
                count: 1,
            },
        ]
    );

    let files = sessions
        .iter()
        .find(|session| session.session_id == "triage-files")
        .expect("file session listed");
    // auth.rs edited twice outranks lib.rs edited once.
    assert_eq!(
        files.top_files,
        vec![
            FileTouch {
                path: "/tmp/nabu-triage/src/auth.rs".to_string(),
                edits: 2,
            },
            FileTouch {
                path: "/tmp/nabu-triage/src/lib.rs".to_string(),
                edits: 1,
            },
        ]
    );

    // Triage fields serialize for MCP and omit when empty (the file session has
    // no tool calls, so top_tools is skipped, not emitted as []).
    let json = serde_json::to_value(files).unwrap();
    assert!(json.get("top_tools").is_none());
    assert!(json.get("top_files").is_some());
    assert!(json.get("first_user_prompt").is_none());
}

// Issue #1 item 7: the first-user-prompt snippet must truncate long prompts on
// a char boundary so the list row stays compact and never panics on multibyte
// input. Controlled by varying only prompt length around the cap.
#[test]
fn list_sessions_truncates_long_first_user_prompt() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    init_home(&home).unwrap();

    let long_prompt = "x".repeat(SESSION_PROMPT_SNIPPET_CHARS + 50);
    ingest_hook_event(
        &home,
        Tool::Claude,
        json!({
            "session_id": "triage-long",
            "hook_event_name": "UserPromptSubmit",
            "message_id": "triage-long-1",
            "prompt": long_prompt
        }),
    )
    .unwrap();
    index_once(&home).unwrap();

    let sessions = list_sessions(&home, Some(Tool::Claude), None, None, 10).unwrap();
    let snippet = sessions[0]
        .first_user_prompt
        .as_deref()
        .expect("prompt present");
    // SESSION_PROMPT_SNIPPET_CHARS chars plus the single ellipsis char.
    assert_eq!(snippet.chars().count(), SESSION_PROMPT_SNIPPET_CHARS + 1);
    assert!(snippet.ends_with('\u{2026}'));
}

fn valid_envelope_json() -> Value {
    json!({
        "schema_version": 1,
        "captured_at": "2026-06-17T12:00:00Z",
        "tool": "codex",
        "tool_version": null,
        "session_id": "session/one",
        "filename_session_id": "session_one",
        "turn_id": null,
        "message_id": null,
        "project_root": null,
        "cwd": "/tmp/nabu-fixture",
        "source": "hook",
        "source_event_type": "UserPromptSubmit",
        "canonical_type": "user.message",
        "source_event_id": null,
        "dedupe_key": "sha256:abc",
        "sequence": null,
        "raw_file": null,
        "raw_offset": null,
        "payload": {}
    })
}

#[test]
fn codex_exec_wrapper_intent_strips_scaffolding_and_keeps_command() {
    // codex emits the shell command as a JSON-encoded exec wrapper under
    // `arguments`; the searchable text must carry the command, not the envelope.
    let string_encoded = json!({
        "type": "function_call",
        "name": "shell",
        "arguments": "{\"command\":[\"bash\",\"-lc\",\"cargo test --workspace\"],\"workdir\":\"/repo\",\"yield_time_ms\":250,\"with_escalated_permissions\":true,\"justification\":\"run the suite codex justification marker\"}",
        "call_id": "call-1"
    });
    let rendered = search_document_for_event(CanonicalType::ToolCall, &string_encoded).render();
    for key in ["workdir", "yield_time_ms", "with_escalated_permissions"] {
        assert!(
            !rendered.contains(key),
            "scaffolding key {key} leaked: {rendered}"
        );
    }
    assert!(
        rendered.contains("cargo test --workspace"),
        "missing command: {rendered}"
    );
    assert!(
        rendered.contains("run the suite codex justification marker"),
        "missing justification: {rendered}"
    );

    // Same wrapper as an inline object (not a JSON-encoded string).
    let inline = json!({
        "type": "function_call",
        "name": "shell",
        "arguments": {
            "command": ["bash", "-lc", "cargo test --workspace"],
            "workdir": "/repo",
            "timeout_ms": 1000
        }
    });
    let rendered = search_document_for_event(CanonicalType::ToolCall, &inline).render();
    assert!(
        !rendered.contains("workdir"),
        "scaffolding leaked: {rendered}"
    );
    assert!(
        !rendered.contains("timeout_ms"),
        "scaffolding leaked: {rendered}"
    );
    assert!(
        rendered.contains("cargo test --workspace"),
        "missing command: {rendered}"
    );

    // Non-wrapper tool input keeps its existing plain-string rendering.
    let plain = json!({"tool_name": "shell", "input": "printf plain codex marker"});
    let rendered = search_document_for_event(CanonicalType::ToolCall, &plain).render();
    assert!(
        rendered.contains("printf plain codex marker"),
        "plain input lost: {rendered}"
    );
}

#[test]
fn codex_exec_wrapper_tool_call_is_searchable_by_command_without_scaffolding() {
    let temp = tempdir().unwrap();
    let home = temp.path().join("home");
    let source = temp.path().join("codex-sessions");
    init_home(&home).unwrap();
    fs::create_dir_all(&source).unwrap();

    let session_id = "019b0000-0000-7000-8000-000000000077";
    let arguments = serde_json::to_string(&json!({
        "command": ["bash", "-lc", "cargo build --release codexcmdmarker"],
        "workdir": "/tmp/native-codex",
        "yield_time_ms": 500
    }))
    .unwrap();
    let line = serde_json::to_string(&json!({
        "timestamp": "2026-06-18T00:00:01Z",
        "type": "response_item",
        "payload": {
            "type": "function_call",
            "name": "shell",
            "call_id": "codex-exec-call-1",
            "arguments": arguments
        }
    }))
    .unwrap();
    fs::write(
        source.join(format!("rollout-2026-06-18T00-00-00-{session_id}.jsonl")),
        format!(
            "{{\"timestamp\":\"2026-06-18T00:00:00Z\",\"type\":\"session_meta\",\"payload\":{{\"id\":\"{session_id}\",\"cwd\":\"/tmp/native-codex\"}}}}\n{line}\n"
        ),
    )
    .unwrap();

    backfill_since(&home, Some(Tool::Codex), &source, None).unwrap();
    index_once(&home).unwrap();

    // A search for the real command returns the event.
    let hits = search_history(&home, "codexcmdmarker", 10).unwrap();
    assert_eq!(hits.len(), 1, "command not searchable: {hits:?}");
    let hit = &hits[0];
    assert_eq!(hit.session_id, session_id);
    // Invariant #13: raw provenance is intact.
    assert!(hit.raw_line > 0);
    assert!(!hit.raw_file.is_empty());
    // The snippet carries the command and excludes exec scaffolding.
    assert!(
        hit.snippet.contains("cargo build --release codexcmdmarker"),
        "snippet missing command: {:?}",
        hit.snippet
    );
    for key in ["workdir", "yield_time_ms"] {
        assert!(
            !hit.snippet.contains(key),
            "scaffolding key {key} leaked into snippet: {:?}",
            hit.snippet
        );
    }

    // A search for a scaffolding key does not surface this event.
    assert!(
        search_history(&home, "yield_time_ms", 10)
            .unwrap()
            .is_empty(),
        "scaffolding key became searchable"
    );
}