ai-session-search 1.0.0-rc.4

Local-first search, inspection, export, recovery, and MCP recall across supported AI coding-agent sessions.
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
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
// SPDX-FileCopyrightText: 2026 Andrew Hundt
// SPDX-FileCopyrightText: 2026 Nisarg Patel
// SPDX-License-Identifier: Apache-2.0

use std::io;
use std::num::NonZeroUsize;

use chrono::{DateTime, Utc};
use clap::ValueEnum;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{mpsc, Arc, Condvar, Mutex};
use std::thread;
use std::time::Duration;

use anyhow::Result;
use crossterm::{
    event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

use ratatui::{
    backend::{Backend, CrosstermBackend},
    layout::{Constraint, Direction, Layout},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{
        Block, Borders, Clear, HighlightSpacing, List, ListItem, ListState, Paragraph, Wrap,
    },
    Terminal,
};

use crate::config::Config;
use crate::db::{
    Db, QueryCancellation, QueryCancelled, MIN_READABLE_SCHEMA_VERSION, SCHEMA_VERSION,
};
use crate::keymap::{ActionMode, TuiAction};
#[cfg(test)]
use crate::models::Role;
use crate::models::{Provider, SearchFilters, SessionKind, SessionRecord};
use crate::runtime::ExecutionRuntime;
use crate::search_scope::EffectiveAccessScope;
use crate::service::CatalogService;
use crate::terminal_style::TerminalStyle;
use crate::util::{
    current_repo, highlight_matches, prompt_confirm, relative_age, render_posix_shell_command,
    resume_plan, truncate_for_display,
};

/// Minimum number of sessions loaded into the TUI's in-memory browser.
///
/// The TUI currently browses one materialized result set rather than exposing keyset pages. A
/// small CLI search default would otherwise make navigation appear to stop after only a few
/// sessions. This private floor affects only how many rows the browser can navigate; it is not a
/// public search limit or a hidden cap, because larger configured defaults remain unchanged.
const TUI_MIN_BROWSER_RESULTS: usize = 100;

fn tui_result_limit(configured_default: usize) -> usize {
    configured_default.max(TUI_MIN_BROWSER_RESULTS)
}

/// Convert a configured unsigned step without `as` wrapping values above `isize::MAX` negative.
fn saturating_step(value: usize) -> isize {
    isize::try_from(value).unwrap_or(isize::MAX)
}

/// Absolute result range needed for one frame. Result membership stays complete in AppState;
/// formatting/allocation is proportional only to terminal-height V, never retained K.
fn visible_session_range(
    total: usize,
    selected: usize,
    viewport_rows: usize,
) -> std::ops::Range<usize> {
    let visible = total.min(viewport_rows);
    if visible == 0 {
        return 0..0;
    }
    let selected = selected.min(total - 1);
    let start = selected
        .saturating_sub(visible / 2)
        .min(total.saturating_sub(visible));
    start..start + visible
}

/// Search box height including its border rows; the query line is the middle row.
const SEARCH_BOX_ROWS: u16 = 3;

/// Minimum height of the list/preview body. Below 14 total rows the vertical layout
/// underflows (recorded as D11); the step-5 resize test pins that boundary.
const MIN_BODY_ROWS: u16 = 10;

/// Status/help bar height, shared by both modes.
const STATUS_BAR_ROWS: u16 = 1;

/// Maximum delay before the UI observes current worker output. Idle turns use the configured
/// event interval directly; the 10 ms latency slice is paid only while search/preview work is live.
const ACTIVE_WORKER_POLL_SLICE: Duration = Duration::from_millis(10);

/// Error line height when a keystroke error is being shown. It takes its row from the body,
/// never from the help bar, so REQ047's recovery guidance keeps its line.
const ERROR_LINE_ROWS: u16 = 1;

/// Two border rows plus one content row: the scroll-clamp floor before the first render has
/// recorded a viewport height (D11).
const PREVIEW_VIEWPORT_SLACK: usize = 3;

/// Worst-case rendered width of a list row's " [age]" suffix, from relative_age's output
/// shapes: its longest form is the date branch, " [2026-01-16]".
const AGE_SUFFIX_ALLOWANCE: usize = 13;

/// The `s` binding's time windows in cycle order, as (status label, span in hours). One table,
/// so the binding that sets `filters.since` and the status bar that names the active window
/// cannot disagree about which one is on.
const SINCE_WINDOWS: [(&str, i64); 3] = [("1d", 24), ("7d", 24 * 7), ("30d", 24 * 30)];

/// Which entry of [`SINCE_WINDOWS`] a `since` timestamp names, recovered from the age it implies.
///
/// `SearchFilters::since` is an absolute instant, so the active window has to be read back rather
/// than stored twice. A window matches while its age has not yet reached the next window's span,
/// which leaves a day of slack: a TUI left open overnight keeps labelling its window correctly and
/// keeps advancing the cycle in order.
fn active_since_window(since: Option<DateTime<Utc>>) -> Option<usize> {
    let since = since?;
    let age_hours = (Utc::now() - since).num_hours();
    Some(
        SINCE_WINDOWS
            .iter()
            .position(|(_, span_hours)| age_hours <= span_hours + 24)
            .unwrap_or(SINCE_WINDOWS.len() - 1),
    )
}

/// The session list's provider labels (D6/D10): uppercase throughout — the old table's
/// "GEMINI" and "Gemini" were near-identical, and "AI Studio" overflowed a `{:<6}` field
/// that pads but never truncates.
fn provider_label(provider: Provider) -> (&'static str, Color) {
    match provider {
        Provider::Claude => ("CLAUDE", Color::Green),
        Provider::ClaudeDesktop => ("CL-DESK", Color::Green),
        Provider::Codex => ("CODEX", Color::Cyan),
        Provider::Cursor => ("CURSOR", Color::Magenta),
        Provider::Antigravity => ("ANTIGRAV", Color::Yellow),
        Provider::Pi => ("PI", Color::Green),
        Provider::PrimeAgent => ("PRIME", Color::LightGreen),
        Provider::AiStudio => ("AISTUDIO", Color::Cyan),
        Provider::GeminiCli => ("GEMINICLI", Color::Blue),
    }
}

/// The label-column floor, derived from the table so adding a provider can never produce a
/// truncating width.
fn longest_provider_label() -> usize {
    Provider::value_variants()
        .iter()
        .map(|provider| provider_label(*provider).0.len())
        .max()
        .unwrap_or(0)
}

/// Middle-elide `text` to terminal columns, keeping the tail intact: an anyhow chain ends with
/// recovery guidance, and REQ047 forbids losing it to clipping. Newlines are flattened because
/// the destination is exactly one row; grapheme display width handles CJK and emoji correctly.
fn elide_middle(text: &str, width: usize, ellipsis: &str) -> String {
    let sanitized = text
        .chars()
        .map(|character| {
            if matches!(character, '\r' | '\n') {
                ' '
            } else {
                character
            }
        })
        .collect::<String>();
    if UnicodeWidthStr::width(sanitized.as_str()) <= width {
        return sanitized;
    }
    if width == 0 {
        return String::new();
    }
    let marker_width = UnicodeWidthStr::width(ellipsis);
    if width <= marker_width {
        // No room for both a marker and text: the marker alone at least says text was cut.
        return ellipsis.chars().take(width).collect();
    }
    let head_budget = width.saturating_sub(marker_width) / 2;
    let tail_budget = width - head_budget - marker_width;
    let mut head = String::new();
    let mut used = 0;
    for grapheme in sanitized.graphemes(true) {
        let columns = UnicodeWidthStr::width(grapheme);
        if used + columns > head_budget {
            break;
        }
        head.push_str(grapheme);
        used += columns;
    }
    let mut tail_graphemes = Vec::new();
    used = 0;
    for grapheme in sanitized.graphemes(true).rev() {
        let columns = UnicodeWidthStr::width(grapheme);
        if used + columns > tail_budget {
            break;
        }
        tail_graphemes.push(grapheme);
        used += columns;
    }
    let tail = tail_graphemes.into_iter().rev().collect::<String>();
    format!("{head}{ellipsis}{tail}")
}

/// Separator between status-bar hints.
/// Fit status-bar hints into `width` display columns by dropping whole hints instead of eliding
/// characters out of the middle of the joined line.
///
/// Middle-eliding a help bar is the wrong shape for it: at 80 columns it cut the line to
/// `p:any … │ j/k: move │ P…rs │ /: search`, which names no binding the reader can act on. A hint
/// is only worth its columns while it is readable, so the lowest-priority hints leave first and
/// the rest stay whole. `priority` is drop order, 0 last; ties drop the hint further right, so the
/// ones a reader scans first survive longest. The final hint is always kept, and the caller still
/// elides it for a frame too narrow even for that.
fn fit_status_hints(hints: &[(u8, String)], width: usize, separator: &str) -> String {
    let mut kept: Vec<usize> = (0..hints.len()).collect();
    loop {
        let text = kept
            .iter()
            .map(|index| hints[*index].1.as_str())
            .collect::<Vec<_>>()
            .join(separator);
        if kept.len() == 1 || UnicodeWidthStr::width(text.as_str()) <= width {
            return text;
        }
        let victim = kept
            .iter()
            .enumerate()
            .max_by_key(|(position, index)| (hints[**index].0, *position))
            .map(|(position, _)| position)
            .expect("kept is never empty");
        kept.remove(victim);
    }
}

/// The crossterm event API is a set of free functions over a process-global source, so it
/// cannot be substituted in a test. This is the seam: production wraps those functions, tests
/// replay a script. Mirrors `crossterm::event::{poll, read}` (crossterm 0.29).
trait EventSource {
    fn poll(&mut self, timeout: Duration) -> io::Result<bool>;
    fn read(&mut self) -> io::Result<Event>;
}

struct CrosstermEventSource;

impl EventSource for CrosstermEventSource {
    fn poll(&mut self, timeout: Duration) -> io::Result<bool> {
        event::poll(timeout)
    }

    fn read(&mut self) -> io::Result<Event> {
        event::read()
    }
}

/// RAII guard for the TUI's raw-mode + alternate-screen terminal session.
/// [`TerminalGuard::enter`] switches the terminal into TUI mode; [`Drop`] switches it
/// back on EVERY exit path — a normal return, an early `?` (e.g. `Terminal::new` fails),
/// or a panic inside the event loop — so a failure mid-TUI never leaves the user's
/// terminal corrupted (raw mode still on, stuck on the alternate screen, cursor hidden,
/// requiring a blind `reset`).
struct TerminalGuard;

impl TerminalGuard {
    fn enter() -> Result<Self> {
        enable_raw_mode()?;
        let mut stdout = io::stdout();
        if let Err(err) = execute!(stdout, EnterAlternateScreen) {
            // Raw mode is on but entering the alternate screen failed — undo raw mode so we
            // never return leaving the terminal half-configured.
            let _ = disable_raw_mode();
            return Err(err.into());
        }
        Ok(Self)
    }
}

impl Drop for TerminalGuard {
    fn drop(&mut self) {
        // Best-effort teardown — ignore errors: we may be unwinding from a panic, and there
        // is nothing useful to do if restoration itself fails.
        let _ = disable_raw_mode();
        let mut stdout = io::stdout();
        let _ = execute!(stdout, LeaveAlternateScreen, crossterm::cursor::Show);
    }
}

/// Browse and fuzzy-search session-level title/summary/preview/transcript records.
///
/// This TUI does not expose message-field exact/regex/fuzzy modes. Use `aise messages search` or
/// MCP `search_messages` for content, canonical tool-name, and tool-argument search; keeping that
/// boundary explicit avoids a second interactive message-search contract.
/// Run only the terminal selection lifecycle so the CLI can drop its owning SessionSearch
/// (SQLite connections and Rayon runtime) before prompting or starting a resumed process.
pub(crate) fn select_session(config: &Config, db: &Db) -> Result<Option<SessionRecord>> {
    // Open and validate the worker before entering raw/alternate-screen mode: a slow or failed
    // startup must leave the user's ordinary terminal visible.
    // One resolution for the run: the worker builds preview text out of these symbols and the
    // renderer matches on them, so a second resolution could disagree with the first.
    let style = TerminalStyle::resolve(config.ui.unicode, config.ui.color);
    let (worker, _observed_scope) = spawn_search_worker(db_backed_executor(
        config.clone(),
        db.access_scope().clone(),
        db.execution_runtime(),
        style,
    ))?;
    let mut app = AppState::new(config.clone(), worker)?;

    // `app` is declared before the guard, so panic unwinding restores the terminal before
    // SearchWorker::drop can wait for cancellation. The explicit normal-path drop preserves that
    // order before returning a selection to the caller.
    let terminal_guard = TerminalGuard::enter()?;
    let backend = CrosstermBackend::new(io::stdout());
    let mut terminal = Terminal::new(backend)?;
    let mut events = CrosstermEventSource;
    let action = run_app(&mut terminal, &mut events, &mut app);
    drop(terminal_guard);
    drop(app);

    match action? {
        AppAction::Quit => Ok(None),
        AppAction::Resume(session) => Ok(Some(*session)),
    }
}

pub(crate) fn execute_resume(session: &SessionRecord) -> Result<()> {
    let (command, cwd) = resume_plan(session)?;
    println!(
        "POSIX shell resume command: {}",
        render_posix_shell_command(&command)?
    );
    println!("{}", crate::util::RESUME_COMMAND_POLICY_NOTE);
    if let Some(cwd) = &cwd {
        println!("cwd: {cwd}");
    }
    if !prompt_confirm("Execute resume command?")? {
        println!("resume cancelled");
        return Ok(());
    }
    let mut process = std::process::Command::new(&command[0]);
    process.args(&command[1..]);
    if let Some(cwd) = cwd {
        process.current_dir(cwd);
    }
    let status = process.status()?;
    if !status.success() {
        anyhow::bail!("resume command failed with status {status}");
    }
    Ok(())
}

fn run_app<B: Backend>(
    terminal: &mut Terminal<B>,
    events: &mut dyn EventSource,
    app: &mut AppState,
) -> Result<AppAction>
where
    B::Error: Send + Sync + 'static,
{
    loop {
        if let Some(action) = step(terminal, events, app)? {
            return Ok(action);
        }
    }
}

/// One loop turn: absorb finished work, draw, then drain every pending input event. Split out
/// of `run_app` so tests can advance the loop a bounded number of turns and inspect the rendered
/// buffer between them.
///
/// Complexity (REQ010): `O(1)` plus one frame render and the events already queued. It performs
/// no database work and takes no lock. Draining in a `while` (not an `if`) means a pasted burst
/// of input costs one poll, not one poll per event — itself a latency fix, measured in the plan's
/// step-0 baseline.
fn step<B: Backend>(
    terminal: &mut Terminal<B>,
    events: &mut dyn EventSource,
    app: &mut AppState,
) -> Result<Option<AppAction>>
where
    B::Error: Send + Sync + 'static,
{
    app.drain_responses();
    terminal.draw(|frame| app.render(frame))?;
    // Wait for input in slices no longer than the configured interval, absorbing worker
    // responses between slices. Two measured defects forced this shape (§12): a key's echo
    // must render immediately — not after the interval expires — and a worker response
    // arriving mid-wait must be applied the same way; the first post-fix measurement pinned
    // results p50 at the 150 ms idle interval because nothing drained until the next step.
    // While current worker work is live, slices are capped at 10 ms so pickup latency stays far
    // below configured pacing. Fully idle turns perform one configured wait rather than waking
    // 100 times/second; each handled key restarts the idle window.
    let mut idle_deadline = std::time::Instant::now()
        .checked_add(Duration::from_millis(app.config.ui.idle_poll_interval_ms))
        .ok_or_else(|| anyhow::anyhow!("ui.idle_poll_interval_ms exceeds the monotonic clock"))?;
    loop {
        let now = std::time::Instant::now();
        // A query edited and then left alone becomes a search here, so the wait below is what
        // separates typing from searching. The slice is shortened to the remaining delay so the
        // search starts on time rather than at the next idle turn.
        let mut pending_search_delay = app.edited_query_delay(now);
        if pending_search_delay == Some(Duration::ZERO) {
            app.flush_edited_query();
            terminal.draw(|frame| app.render(frame))?;
            pending_search_delay = None;
        }
        if now >= idle_deadline {
            return Ok(None);
        }
        let remaining = idle_deadline.saturating_duration_since(now);
        let mut slice = if app.searching || app.preview_loading || app.worker.has_work() {
            remaining.min(ACTIVE_WORKER_POLL_SLICE)
        } else {
            remaining
        };
        if let Some(delay) = pending_search_delay {
            slice = slice.min(delay);
        }
        if events.poll(slice)? {
            let event = events.read()?;
            let Event::Key(key) = event else {
                if matches!(event, Event::Resize(_, _)) {
                    terminal.draw(|frame| app.render(frame))?;
                    idle_deadline = std::time::Instant::now()
                        .checked_add(Duration::from_millis(app.config.ui.idle_poll_interval_ms))
                        .ok_or_else(|| {
                            anyhow::anyhow!("ui.idle_poll_interval_ms exceeds the monotonic clock")
                        })?;
                }
                continue;
            };
            if key.kind != KeyEventKind::Press {
                continue;
            }
            if let Some(action) = app.handle_key(key) {
                return Ok(Some(action));
            }
            // Redraw right after handling a key so its echo never waits out a poll slice.
            app.drain_responses();
            terminal.draw(|frame| app.render(frame))?;
            idle_deadline = std::time::Instant::now()
                .checked_add(Duration::from_millis(app.config.ui.idle_poll_interval_ms))
                .ok_or_else(|| {
                    anyhow::anyhow!("ui.idle_poll_interval_ms exceeds the monotonic clock")
                })?;
        } else if app.drain_responses() {
            // A worker response landed mid-wait: apply it and redraw now.
            terminal.draw(|frame| app.render(frame))?;
        }
    }
}

enum AppAction {
    Quit,
    // Boxed: SessionRecord is large; keep the enum small (clippy::large_enum_variant).
    Resume(Box<SessionRecord>),
}

/// Which kind of work a [`WorkerRequest`] asks for. The discriminant is load-bearing: a
/// preview-only response must never replace the result list (C13).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RequestKind {
    Search,
    PreviewOnly,
}

/// Request identity is an allocation token rather than an integer, so it cannot wrap and make
/// an ancient response current again (the same generation-token pattern used by MCP refresh).
#[derive(Clone)]
struct RequestGeneration(Arc<()>);

impl RequestGeneration {
    fn new() -> Self {
        Self(Arc::new(()))
    }

    fn same_as(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

/// A unit of work for the search worker.
struct WorkerRequest {
    kind: RequestKind,
    generation: RequestGeneration,
    query: String,
    filters: SearchFilters,
    selected_id: Option<String>,
}

/// Typed response envelope: successes and errors carry the same operation identity, so a late
/// failure cannot overwrite the current screen any more than late rows can.
struct WorkerOutcome {
    kind: RequestKind,
    generation: RequestGeneration,
    result: Result<WorkerResponse>,
}

/// The standing empty-state preview text; `WorkerResponse::preview` supplies it when no row is
/// selected, exactly as the inline preview path always has.
const NO_SESSIONS_PREVIEW: &str = "No sessions matched the current query.";

/// Exactly one of `results` / `preview` is populated, and which one is decided by `RequestKind`.
///
/// A `Search` response carries **no** preview. It cannot: the selection-preservation rule runs
/// on the UI thread against the new result set, so the worker does not know which row will end
/// up selected and cannot pre-compute the right preview for it. The UI applies the results,
/// then issues a `PreviewOnly` for whatever it actually selected — one extra round trip, off
/// the UI thread, and it keeps preview scan/output work out of the search response
/// (C28). The originating query travels back so a superseded response is dropped without a
/// sequence counter; `previewed_id` says which session the preview is *for*, so a preview
/// overtaken by newer navigation is discarded rather than rendered beside the wrong row.
struct WorkerResponse {
    results: Option<Vec<SessionRecord>>,
    previewed_id: Option<String>,
    preview: Option<String>,
}

impl WorkerResponse {
    /// A search response: results only, no preview.
    fn results(_request: &WorkerRequest, rows: Vec<SessionRecord>) -> Self {
        Self {
            results: Some(rows),
            previewed_id: None,
            preview: None,
        }
    }

    /// A preview response. `None` text means "no selected session" and renders the standing
    /// empty-state message, matching the inline preview path.
    ///
    /// It deliberately carries no line count. The worker can only count logical lines, and the
    /// scroll bound is in terminal rows after word wrapping, which only the renderer knows; a
    /// second count here would clamp a reader out of the wrapped tail.
    fn preview(request: &WorkerRequest, text: Option<String>) -> Self {
        Self {
            results: None,
            previewed_id: request.selected_id.clone(),
            preview: Some(text.unwrap_or_else(|| NO_SESSIONS_PREVIEW.to_string())),
        }
    }
}

/// The cancellation is supplied by the worker, not created by the executor: one slot, one
/// owner (§2.3). A production executor installs it on its own connection.
///
/// It is passed as `&Arc<_>`, not `&QueryCancellation`, for one reason: `cancel_in_flight`
/// does `slot.take()`, so after a supersede the worker's `Arc` is the only one left and a
/// borrow cannot outlive the call. A fake executor clones the `Arc` out to the test thread,
/// which is the only way the supersede and quit tests can observe `is_cancelled()` at all.
type SearchExecutor =
    Box<dyn FnMut(&WorkerRequest, &Arc<QueryCancellation>) -> Result<WorkerResponse> + Send>;

/// Runs INSIDE the worker thread. The production executor owns a `Db` that must be opened
/// there, so it cannot be constructed on the UI thread and moved in; tests return a closure
/// directly. Reports the observed access scope on success so the caller can assert it (P5).
type ExecutorFactory = Box<dyn FnOnce() -> Result<(SearchExecutor, EffectiveAccessScope)> + Send>;

#[derive(Default)]
struct PendingWork {
    search: Option<WorkerRequest>,
    preview: Option<WorkerRequest>,
    in_flight: Option<(RequestKind, Arc<QueryCancellation>)>,
    closed: bool,
}

impl PendingWork {
    /// The one pending slot for `kind`. Constant capacity per kind is the mailbox's contract,
    /// so the slot is named here rather than matched at each call site.
    fn slot_mut(&mut self, kind: RequestKind) -> &mut Option<WorkerRequest> {
        match kind {
            RequestKind::Search => &mut self.search,
            RequestKind::PreviewOnly => &mut self.preview,
        }
    }

    /// Take the in-flight cancellation out of the slot before raising it. One owner, one order:
    /// the interrupt handle is connection-scoped, so clearing and cancelling must stay atomic
    /// under the mailbox lock or a cancel could race its successor's publication.
    fn cancel_in_flight(&mut self) {
        if let Some((_, cancellation)) = self.in_flight.take() {
            cancellation.cancel();
        }
    }

    /// Cancel the in-flight request only when it is `target`. Navigation supersedes an older
    /// preview scan this way without ever cancelling an in-flight search (C21).
    fn cancel_in_flight_if(&mut self, target: RequestKind) {
        if self
            .in_flight
            .as_ref()
            .is_some_and(|(kind, _)| *kind == target)
        {
            self.cancel_in_flight();
        }
    }
}

/// Constant-capacity latest-value mailbox. At most one Search and one PreviewOnly are retained,
/// so a Q-character paste keeps O(Q + F) bytes for the latest query/filters instead of every
/// cumulative prefix (Theta(Q^2) query bytes in the former unbounded FIFO).
struct WorkerMailbox {
    state: Mutex<PendingWork>,
    wake: Condvar,
}

impl WorkerMailbox {
    fn new() -> Self {
        Self {
            state: Mutex::new(PendingWork::default()),
            wake: Condvar::new(),
        }
    }

    fn send(&self, request: WorkerRequest) -> std::result::Result<(), String> {
        let mut state = self
            .state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if state.closed {
            return Err(
                "the search worker stopped; press q to quit and rerun aise tui".to_string(),
            );
        }
        let kind = request.kind;
        match kind {
            // A newer search supersedes whatever is running, search or preview: its result set
            // replaces the list either way.
            RequestKind::Search => state.cancel_in_flight(),
            // Navigation B supersedes preview A's transcript scan, but it must never cancel an
            // in-flight Search (the result-list contract covered by C21).
            RequestKind::PreviewOnly => state.cancel_in_flight_if(RequestKind::PreviewOnly),
        }
        *state.slot_mut(kind) = Some(request);
        self.wake.notify_one();
        Ok(())
    }

    /// Drop the pending request of one kind and stop its in-flight run. Used by the two callers
    /// that abandon work without replacing it: a rejected filter combination, and a preview whose
    /// row is already on screen.
    fn cancel_kind(&self, target: RequestKind) {
        let mut state = self
            .state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        state.slot_mut(target).take();
        state.cancel_in_flight_if(target);
    }

    fn next(&self) -> Option<(WorkerRequest, Arc<QueryCancellation>)> {
        let mut state = self
            .state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        while !state.closed && state.search.is_none() && state.preview.is_none() {
            state = self
                .wake
                .wait(state)
                .unwrap_or_else(std::sync::PoisonError::into_inner);
        }
        if state.closed {
            return None;
        }
        let request = state.search.take().or_else(|| state.preview.take())?;
        let cancellation = Arc::new(QueryCancellation::new());
        state.in_flight = Some((request.kind, Arc::clone(&cancellation)));
        Some((request, cancellation))
    }

    fn finish(&self, cancellation: &Arc<QueryCancellation>, search_succeeded: bool) {
        let mut state = self
            .state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if state
            .in_flight
            .as_ref()
            .is_some_and(|(_, current)| Arc::ptr_eq(current, cancellation))
        {
            state.in_flight.take();
        }
        // A successful result replacement makes a queued old-list preview obsolete; AppState
        // requests the selected new row after applying results. On failure, preserve the latest
        // navigation preview so the old list and its selected row remain coherent.
        if search_succeeded && state.search.is_none() {
            state.preview.take();
        }
    }

    fn close(&self) {
        let mut state = self
            .state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        state.cancel_in_flight();
        state.search.take();
        state.preview.take();
        state.closed = true;
        self.wake.notify_all();
    }

    fn has_work(&self) -> bool {
        let state = self
            .state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        state.search.is_some() || state.preview.is_some() || state.in_flight.is_some()
    }

    #[cfg(test)]
    fn pending_counts(&self) -> (usize, usize) {
        let state = self
            .state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        (
            usize::from(state.search.is_some()),
            usize::from(state.preview.is_some()),
        )
    }
}

struct CloseMailboxOnExit(Arc<WorkerMailbox>);

impl Drop for CloseMailboxOnExit {
    fn drop(&mut self) {
        self.0.close();
    }
}

/// RAII owner for one constant-capacity mailbox, response receiver, and worker thread.
struct SearchWorker {
    mailbox: Arc<WorkerMailbox>,
    responses: mpsc::Receiver<WorkerOutcome>,
    pending_outcomes: Arc<AtomicUsize>,
    handle: Option<thread::JoinHandle<()>>,
}

impl SearchWorker {
    fn send(&self, request: WorkerRequest) -> std::result::Result<(), String> {
        self.mailbox.send(request)
    }

    fn cancel_preview(&self) {
        self.mailbox.cancel_kind(RequestKind::PreviewOnly);
    }

    fn cancel_search(&self) {
        self.mailbox.cancel_kind(RequestKind::Search);
    }

    fn has_work(&self) -> bool {
        self.pending_outcomes.load(Ordering::Acquire) > 0 || self.mailbox.has_work()
    }

    fn acknowledge_outcome(&self) {
        self.pending_outcomes.fetch_sub(1, Ordering::AcqRel);
    }
}

impl Drop for SearchWorker {
    fn drop(&mut self) {
        self.mailbox.close();
        if let Some(handle) = self.handle.take() {
            let _ = handle.join();
        }
    }
}

/// Spawn one search worker. Pending request count is O(1); shared mailbox locks are held only for
/// replacement/cancellation bookkeeping and never while SQLite or scoring executes.
fn spawn_search_worker(
    make_executor: ExecutorFactory,
) -> Result<(SearchWorker, EffectiveAccessScope)> {
    let (ready_tx, ready_rx) =
        mpsc::sync_channel::<std::result::Result<EffectiveAccessScope, String>>(1);
    let (response_tx, response_rx) = mpsc::channel::<WorkerOutcome>();
    let pending_outcomes = Arc::new(AtomicUsize::new(0));
    let worker_pending_outcomes = Arc::clone(&pending_outcomes);
    let mailbox = Arc::new(WorkerMailbox::new());
    let worker_mailbox = Arc::clone(&mailbox);
    let handle = thread::Builder::new()
        .name("aise-tui-search".to_string())
        .spawn(move || {
            let _close_on_exit = CloseMailboxOnExit(Arc::clone(&worker_mailbox));
            let mut execute = match make_executor() {
                Ok((executor, scope)) => {
                    let _ = ready_tx.send(Ok(scope));
                    executor
                }
                Err(error) => {
                    let _ = ready_tx.send(Err(format!("{error:#}")));
                    return;
                }
            };
            while let Some((request, cancellation)) = worker_mailbox.next() {
                let result = execute(&request, &cancellation);
                let search_succeeded = request.kind == RequestKind::Search && result.is_ok();
                let publishable = !cancellation.is_cancelled()
                    && !matches!(&result, Err(error) if is_expected_interruption(error));
                // Publish pending state before clearing mailbox in-flight state so the UI never
                // mistakes the handoff gap for a fully idle worker.
                if publishable {
                    worker_pending_outcomes.fetch_add(1, Ordering::Release);
                }
                worker_mailbox.finish(&cancellation, search_succeeded);
                if !publishable || cancellation.is_cancelled() {
                    if publishable {
                        worker_pending_outcomes.fetch_sub(1, Ordering::AcqRel);
                    }
                    continue;
                }
                if response_tx
                    .send(WorkerOutcome {
                        kind: request.kind,
                        generation: request.generation,
                        result,
                    })
                    .is_err()
                {
                    worker_pending_outcomes.fetch_sub(1, Ordering::AcqRel);
                }
            }
        })?;
    let observed = ready_rx
        .recv()
        .map_err(|_| anyhow::anyhow!("the search worker exited during startup"))?
        .map_err(|message| anyhow::anyhow!("the search worker failed to start: {message}"))?;
    Ok((
        SearchWorker {
            mailbox,
            responses: response_rx,
            pending_outcomes,
            handle: Some(handle),
        },
        observed,
    ))
}

/// Two lines, not `message_search_batches`'s predicate: that one is a private bare `fn` and
/// also matches `MessageSearchCancelled` and `ReadSnapshotCleanupError`, neither of which this
/// worker can produce (E22).
fn is_expected_interruption(error: &anyhow::Error) -> bool {
    error.is::<QueryCancelled>()
        || error.chain().any(|cause| {
            matches!(
                cause.downcast_ref::<rusqlite::Error>(),
                Some(rusqlite::Error::SqliteFailure(inner, _))
                    if inner.code == rusqlite::ErrorCode::OperationInterrupted
            )
        })
}

/// The production executor factory: opens the worker's OWN read-only connection — the
/// message-search worker's recipe (read-only unconditionally, access scope set explicitly,
/// schema version ensured, progress reporter deliberately unset so nothing writes over the
/// alternate screen) — and routes search/list through `CatalogService` (D1), the same seam
/// the CLI, MCP, and Python use. Preview bookends use the canonical transcript projection shared
/// by CLI, MCP, Python/export, and session search; normalized message rows are not substituted
/// because they can contain harness notices or
/// generated mixed-content parts intentionally excluded from that projection.
///
/// Complexity (REQ010): one worker connection (≤64 MiB page-cache ceiling, 256 MiB virtual mmap
/// window) in addition to the caller's idle connection, sharing one `config.resolve_threads()`
/// Rayon pool process-wide. Search/list delegate to their documented
/// bounds. Preview scans canonical transcript bytes once and retains only four borrowed bookend
/// spans plus rendered output, instead of cloning the transcript and collecting every turn.
fn db_backed_executor(
    config: Config,
    access: EffectiveAccessScope,
    runtime: Arc<ExecutionRuntime>,
    style: TerminalStyle,
) -> ExecutorFactory {
    Box::new(move || {
        let worker_threads = NonZeroUsize::new(config.resolve_threads())
            .expect("Config::resolve_threads always returns at least one");
        debug_assert_eq!(runtime.worker_threads(), worker_threads.get());
        let mut db = Db::open_existing_read_only_with_runtime(
            &config.db_path(),
            config.index.busy_timeout_ms,
            runtime,
        )?;
        db.set_access_scope(access);
        let schema_version = db.schema_version()?;
        anyhow::ensure!(
            schema_version <= SCHEMA_VERSION,
            "the index uses schema generation {schema_version}, newer than this aise build \
             supports ({SCHEMA_VERSION}); upgrade aise before opening it"
        );
        anyhow::ensure!(
            schema_version >= MIN_READABLE_SCHEMA_VERSION,
            "the TUI search worker requires readable database schema generation \
             {MIN_READABLE_SCHEMA_VERSION} or newer, got {schema_version}; \
             run `aise reindex --full`, then retry"
        );
        let observed = db.access_scope().clone();
        let repo = current_repo(&config);
        let scoring = config.search.scoring.clone();
        let preview_budget = config.ui.preview_body_lines;
        Ok((
            Box::new(
                move |request: &WorkerRequest, cancellation: &Arc<QueryCancellation>| {
                    // The worker created and published this; the executor only installs it on
                    // its own connection.
                    db.install_query_cancellation(cancellation)?;
                    // Constructed per call: the closure owns `db`, so it cannot also store a
                    // CatalogService borrowing it. Copy newtype over a reference; free.
                    let catalog = CatalogService::new(&db);
                    match request.kind {
                        RequestKind::Search if request.query.trim().is_empty() => {
                            Ok(WorkerResponse::results(
                                request,
                                catalog.list_sessions(&request.filters)?,
                            ))
                        }
                        RequestKind::Search => Ok(WorkerResponse::results(
                            request,
                            catalog
                                .search_sessions_cancellable(
                                    &request.query,
                                    &request.filters,
                                    repo.as_deref(),
                                    &scoring,
                                    cancellation,
                                )?
                                .into_iter()
                                .map(|hit| hit.session)
                                .collect(),
                        )),
                        RequestKind::PreviewOnly => {
                            let text = match request.selected_id.as_deref() {
                                Some(id) => Some(db.inspect_session_transcript(
                                    id,
                                    cancellation,
                                    |session, transcript| {
                                        let summary = build_transcript_summary_cancellable(
                                            transcript,
                                            preview_budget,
                                            style,
                                            cancellation,
                                        )?;
                                        Ok(format!(
                                            "Session: {}\nCWD: {}\n\n{}",
                                            session.id,
                                            session.cwd.as_deref().unwrap_or("-"),
                                            summary
                                        ))
                                    },
                                )?),
                                None => None,
                            };
                            Ok(WorkerResponse::preview(request, text))
                        }
                    }
                },
            ) as SearchExecutor,
            observed,
        ))
    })
}

struct AppState {
    config: Config,
    /// The session filters the browser runs with. The state IS a `SearchFilters` value: no
    /// parallel filter representation exists (R8/§5.5d).
    filters: SearchFilters,
    /// Search worker: owns every database query. The UI thread never blocks on it — it sends
    /// requests and drains responses (§2.3).
    worker: SearchWorker,
    query: String,
    /// Where the next typed character goes, as a byte index into `query` on a character
    /// boundary. The box was append-only, so a typo in the middle meant deleting back to it.
    query_cursor: usize,
    search_mode: bool,
    current_search_generation: RequestGeneration,
    current_preview_generation: RequestGeneration,
    /// When the query was last edited with no search issued for it yet. `step` turns it into one
    /// request once typing has been quiet for `[ui].search_debounce_ms`, so a typed burst costs
    /// one search instead of one cancelled scan per character. `None` means nothing is pending.
    query_edited_at: Option<std::time::Instant>,
    /// True from request submission until the matching search success/error is applied. Rendered
    /// in the list title so the real-terminal benchmark can observe final-generation completion.
    searching: bool,
    preview_loading: bool,
    selected: usize,
    results: Vec<SessionRecord>,
    preview: String,
    preview_scroll: u16,
    /// The preview's height in terminal rows after word wrapping, recorded by `render`. The
    /// worker cannot supply it: wrapping depends on the pane width, so this is the only count
    /// the scroll bound may use.
    preview_line_count: usize,
    /// The session whose preview is currently rendered: the skip guard for rescanning the
    /// canonical transcript per keystroke, and the match check that discards overtaken output.
    previewed_id: Option<String>,
    /// The preview pane's interior height, recorded by `render`, so scroll clamping bounds by
    /// viewport, not just content length (D11). Zero until the first draw; the clamp's slack
    /// floor covers that case.
    preview_viewport_rows: u16,
    /// Last error from a keystroke-triggered operation, shown on its own line. A key press
    /// can never abort the TUI: errors land here instead of propagating through `?`.
    error: Option<String>,
    /// Operation that owns `error`; worker/lifecycle failures use `None`. Search and preview
    /// successes clear only their own failure, so one operation cannot erase another's evidence.
    error_owner: Option<RequestKind>,
    /// A disconnected response producer is terminal for this worker. Remember reporting it so
    /// later active/idle drains do not redraw the same error forever.
    worker_disconnected_reported: bool,
    /// True after one interrupt, cleared by any other key. The next one quits. It is a field
    /// rather than a timer so the state is exactly what the status bar shows.
    interrupt_armed: bool,
    /// True while the key list covers the screen. It scrolls with the preview scroll keys, and
    /// any other key closes it, so nothing else has to be bound to leave.
    showing_help: bool,
    help_scroll: u16,
    /// The key list's interior height, recorded by `render` so its scroll clamps by viewport.
    help_viewport_rows: u16,
    /// What this terminal can draw, resolved once at startup from `[ui].unicode`, `[ui].color`,
    /// and the environment. Every symbol and colour the browser emits comes from here.
    style: TerminalStyle,
}

impl AppState {
    fn new(config: Config, worker: SearchWorker) -> Result<Self> {
        let mut state = Self::new_quiet(config, worker);
        // The initial empty-query search runs on the worker: the first frame draws before the
        // first query completes, and the startup response populates the list (C28).
        state.request_search();
        Ok(state)
    }

    /// Construct without the startup request — the test harness path, where the first
    /// request must wait until the fixture rows are seeded so the startup response and the
    /// seeded state agree.
    fn new_quiet(config: Config, worker: SearchWorker) -> Self {
        let result_limit = tui_result_limit(config.search.default_limit);
        let style = TerminalStyle::resolve(config.ui.unicode, config.ui.color);
        Self {
            config,
            filters: SearchFilters {
                provider: None,
                path_prefix: None,
                exclude_path_prefixes: Vec::new(),
                exclude_session_ids: Vec::new(),
                // Every class, matching the CLI default: the browser shows what is indexed.
                session_kinds: None,
                parent_session_id: None,
                since: None,
                until: None,
                limit: result_limit,
                warnings_only: false,
            },
            worker,
            query: String::new(),
            query_cursor: 0,
            search_mode: false,
            current_search_generation: RequestGeneration::new(),
            current_preview_generation: RequestGeneration::new(),
            query_edited_at: None,
            searching: false,
            preview_loading: false,
            selected: 0,
            results: Vec::new(),
            preview: String::new(),
            preview_scroll: 0,
            preview_line_count: 0,
            previewed_id: None,
            preview_viewport_rows: 0,
            error: None,
            error_owner: None,
            worker_disconnected_reported: false,
            interrupt_armed: false,
            showing_help: false,
            help_scroll: 0,
            help_viewport_rows: 0,
            style,
        }
    }

    /// Handle one key press. Returns `Some(action)` when the loop should stop. Never returns
    /// `Err`: a database failure becomes `self.error` — a keystroke cannot end the TUI.
    ///
    /// What each key means comes from `[ui.keys]`, so the modifiers are part of the match. They
    /// were not: every arm matched a bare character, and Ctrl+Q quit, Ctrl+S moved the time
    /// window, Ctrl+P changed the provider, and Ctrl+H — ASCII backspace on many terminals —
    /// scrolled the preview.
    fn handle_key(&mut self, key: KeyEvent) -> Option<AppAction> {
        let mode = if self.search_mode {
            ActionMode::Search
        } else {
            ActionMode::Browse
        };
        let action = self.config.ui.keys.action_for(&key, mode);

        // The interrupt is answered before anything else and in both modes. Raw mode turns off
        // the terminal's own interrupt character, so Ctrl+C arrives as an ordinary key event and
        // no SIGINT is ever raised: before this, browse mode ignored it and the search box typed
        // a literal `c` into the query, leaving `q` and Esc as the only ways out of a
        // full-screen application. One press arms and says so in the status bar; the next one
        // quits, and any other key disarms, so a stray Ctrl+C cannot end a session by itself.
        if action == Some(TuiAction::Interrupt) {
            if self.interrupt_armed {
                return Some(AppAction::Quit);
            }
            self.interrupt_armed = true;
            return None;
        }
        self.interrupt_armed = false;

        // While the key list is up it owns the keyboard, so a reader can read past its end and
        // then leave with whatever key they reach for. Only scrolling keeps it open.
        if self.showing_help {
            match action {
                Some(TuiAction::PreviewScrollDown) => {
                    let step = saturating_step(self.config.ui.preview_scroll_rows);
                    self.scroll_help(step);
                }
                Some(TuiAction::PreviewScrollUp) => {
                    let step = saturating_step(self.config.ui.preview_scroll_rows);
                    self.scroll_help(-step);
                }
                Some(TuiAction::PreviewPageDown) => {
                    let page = saturating_step(self.config.ui.preview_page_rows);
                    self.scroll_help(page);
                }
                Some(TuiAction::PreviewPageUp) => {
                    let page = saturating_step(self.config.ui.preview_page_rows);
                    self.scroll_help(-page);
                }
                _ => {
                    self.showing_help = false;
                    self.help_scroll = 0;
                }
            }
            return None;
        }

        match action {
            Some(TuiAction::Interrupt) => unreachable!("answered above"),
            Some(TuiAction::Quit) => return Some(AppAction::Quit),
            Some(TuiAction::EnterSearch) => {
                self.search_mode = true;
                // Resuming an existing query puts the caret where a reader would expect to
                // continue typing it.
                self.query_cursor = self.query.len();
            }
            Some(TuiAction::LeaveSearch) => {
                self.search_mode = false;
                // Leaving the box searches whatever is in it now. Waiting out the delay after
                // an explicit Enter is the one case where the reader has already said they are
                // done typing.
                self.flush_edited_query();
            }
            Some(TuiAction::MoveDown) => self.move_selection(1),
            Some(TuiAction::MoveUp) => self.move_selection(-1),
            Some(TuiAction::PageDown) => {
                let page = saturating_step(self.config.ui.list_page_rows);
                self.move_selection(page);
            }
            Some(TuiAction::PageUp) => {
                let page = saturating_step(self.config.ui.list_page_rows);
                self.move_selection(-page);
            }
            Some(TuiAction::Top) => self.select_index(0),
            Some(TuiAction::Bottom) => {
                let last = self.results.len().saturating_sub(1);
                self.select_index(last);
            }
            Some(TuiAction::CycleProvider) => self.cycle_provider(),
            Some(TuiAction::CycleSessionKind) => self.cycle_session_kinds(),
            Some(TuiAction::CycleTimeWindow) => self.cycle_since_window(),
            Some(TuiAction::ToggleWarningsOnly) => self.toggle_warnings_only(),
            Some(TuiAction::PreviewScrollDown) => {
                let step = saturating_step(self.config.ui.preview_scroll_rows);
                self.scroll_preview(step);
            }
            Some(TuiAction::PreviewScrollUp) => {
                let step = saturating_step(self.config.ui.preview_scroll_rows);
                self.scroll_preview(-step);
            }
            Some(TuiAction::PreviewPageDown) => {
                let page = saturating_step(self.config.ui.preview_page_rows);
                self.scroll_preview(page);
            }
            Some(TuiAction::PreviewPageUp) => {
                let page = saturating_step(self.config.ui.preview_page_rows);
                self.scroll_preview(-page);
            }
            Some(TuiAction::Help) => {
                self.showing_help = true;
                self.help_scroll = 0;
            }
            Some(TuiAction::Resume) => {
                if let Some(selected) = self.selected_session() {
                    return Some(AppAction::Resume(Box::new(selected.clone())));
                }
            }
            Some(TuiAction::ClearQuery) => self.clear_query(),
            Some(TuiAction::DeleteBackward) => self.delete_backward(),
            Some(TuiAction::DeleteForward) => self.delete_forward(),
            Some(TuiAction::DeleteWordBackward) => self.delete_word_backward(),
            Some(TuiAction::CursorLeft) => {
                let to = Self::boundary_before(&self.query, self.query_cursor);
                self.move_cursor(to);
            }
            Some(TuiAction::CursorRight) => {
                let to = Self::boundary_after(&self.query, self.query_cursor);
                self.move_cursor(to);
            }
            Some(TuiAction::CursorStart) => self.move_cursor(0),
            Some(TuiAction::CursorEnd) => {
                let to = self.query.len();
                self.move_cursor(to);
            }
            // Not a binding in this mode. In the search box an unmodified character is text;
            // anywhere else, and for a chord that happens to be unbound, it is nothing.
            None => {
                if self.search_mode {
                    if let KeyCode::Char(character) = key.code {
                        if !key.modifiers.intersects(
                            KeyModifiers::CONTROL | KeyModifiers::ALT | KeyModifiers::SUPER,
                        ) {
                            self.insert_character(character);
                        }
                    }
                }
            }
        }
        None
    }

    /// Absorb finished worker responses. Runs at the top of every loop turn, so the UI never
    /// blocks on the worker; a response for a superseded query is dropped without touching
    /// state (P3).
    ///
    /// Complexity (REQ010): `O(responses × K)` for the id lookup; no I/O, no lock held.
    fn drain_responses(&mut self) -> bool {
        let mut applied = false;
        loop {
            match self.worker.responses.try_recv() {
                Ok(outcome) => {
                    self.worker.acknowledge_outcome();
                    applied |= self.apply_outcome(outcome);
                }
                Err(mpsc::TryRecvError::Empty) => break,
                Err(mpsc::TryRecvError::Disconnected) => {
                    if !self.worker_disconnected_reported {
                        self.worker_disconnected_reported = true;
                        self.searching = false;
                        self.preview_loading = false;
                        self.error = Some(
                            "the search worker stopped; press q to quit and rerun aise tui"
                                .to_string(),
                        );
                        self.error_owner = None;
                        applied = true;
                    }
                    break;
                }
            }
        }
        applied
    }

    /// Apply only the current operation generation. Query equality remains useful presentation
    /// context, but it is not request identity: filter-only changes keep the same query (R9-F1).
    fn apply_outcome(&mut self, outcome: WorkerOutcome) -> bool {
        let current = match outcome.kind {
            RequestKind::Search => outcome.generation.same_as(&self.current_search_generation),
            RequestKind::PreviewOnly => {
                outcome.generation.same_as(&self.current_preview_generation)
            }
        };
        if !current {
            return false;
        }
        if outcome.kind == RequestKind::Search {
            self.searching = false;
        } else {
            self.preview_loading = false;
        }
        match outcome.result {
            Ok(response) => self.apply_response(response),
            Err(error) => {
                if outcome.kind == RequestKind::PreviewOnly {
                    let selected = self.selected_session().map(|session| session.id.clone());
                    self.preview = match selected.as_deref() {
                        Some(id) => format!("Preview unavailable for {id}."),
                        None => NO_SESSIONS_PREVIEW.to_string(),
                    };
                    self.previewed_id = selected;
                    self.preview_scroll = 0;
                }
                self.error = Some(format!("{error:#}"));
                self.error_owner = Some(outcome.kind);
            }
        }
        true
    }

    /// The character boundary before `at`, or `at` when it is already the start.
    fn boundary_before(text: &str, at: usize) -> usize {
        text[..at]
            .char_indices()
            .next_back()
            .map_or(0, |(index, _)| index)
    }

    /// The character boundary after `at`, or the end of `text`.
    fn boundary_after(text: &str, at: usize) -> usize {
        text[at..]
            .char_indices()
            .nth(1)
            .map_or(text.len(), |(offset, _)| at + offset)
    }

    /// The start of the run of non-space characters that ends at `at`, skipping any spaces
    /// immediately before it, so a second press deletes the word rather than the gap.
    fn word_start_before(text: &str, at: usize) -> usize {
        let mut cursor = at;
        while cursor > 0 {
            let previous = Self::boundary_before(text, cursor);
            if !text[previous..cursor].chars().all(char::is_whitespace) {
                break;
            }
            cursor = previous;
        }
        while cursor > 0 {
            let previous = Self::boundary_before(text, cursor);
            if text[previous..cursor].chars().all(char::is_whitespace) {
                break;
            }
            cursor = previous;
        }
        cursor
    }

    fn insert_character(&mut self, character: char) {
        self.query.insert(self.query_cursor, character);
        self.query_cursor += character.len_utf8();
        self.note_query_edit();
    }

    fn delete_backward(&mut self) {
        if self.query_cursor == 0 {
            return;
        }
        let start = Self::boundary_before(&self.query, self.query_cursor);
        self.query.replace_range(start..self.query_cursor, "");
        self.query_cursor = start;
        self.note_query_edit();
    }

    fn delete_forward(&mut self) {
        if self.query_cursor >= self.query.len() {
            return;
        }
        let end = Self::boundary_after(&self.query, self.query_cursor);
        self.query.replace_range(self.query_cursor..end, "");
        self.note_query_edit();
    }

    fn delete_word_backward(&mut self) {
        let start = Self::word_start_before(&self.query, self.query_cursor);
        if start == self.query_cursor {
            return;
        }
        self.query.replace_range(start..self.query_cursor, "");
        self.query_cursor = start;
        self.note_query_edit();
    }

    fn clear_query(&mut self) {
        if self.query.is_empty() {
            return;
        }
        self.query.clear();
        self.query_cursor = 0;
        self.note_query_edit();
    }

    /// Moving the cursor is not an edit: it changes nothing to search for, so it must not start
    /// a search or the delay would restart on every arrow key.
    fn move_cursor(&mut self, to: usize) {
        self.query_cursor = to.min(self.query.len());
    }

    /// Record that the query changed. The search itself waits for `[ui].search_debounce_ms` of
    /// quiet, so a typed word costs one search rather than one per character; the keystroke
    /// still echoes on this turn either way.
    ///
    /// `searching` goes true here rather than at submission, because from the reader's side the
    /// search has begun: the list title says so, and the loop starts taking its short wait
    /// slices so the result is picked up as soon as it lands.
    fn note_query_edit(&mut self) {
        self.query_edited_at = Some(std::time::Instant::now());
        // The edited query owns the UI immediately, before its debounce expires. Invalidate and
        // cancel the previous request now so a response arriving during the quiet period cannot
        // replace the list and falsely mark the still-pending query ready.
        self.current_search_generation = RequestGeneration::new();
        self.worker.cancel_search();
        self.searching = true;
        if self.config.ui.search_debounce_ms == 0 {
            self.flush_edited_query();
        }
    }

    /// Issue the pending query's search now, if one is pending.
    fn flush_edited_query(&mut self) {
        if self.query_edited_at.take().is_some() {
            self.request_search();
        }
    }

    /// How long until the pending query is due to be searched for, if one is pending. `None`
    /// means nothing is waiting; `Some(ZERO)` means it is due now.
    fn edited_query_delay(&self, now: std::time::Instant) -> Option<Duration> {
        let edited = self.query_edited_at?;
        Some(
            Duration::from_millis(self.config.ui.search_debounce_ms)
                .saturating_sub(now.saturating_duration_since(edited)),
        )
    }

    /// Queue a search for the current query. `send` cancels any in-flight search first —
    /// this is what makes supersede real (D3). Never `?`: a dead worker sets the error line.
    fn request_search(&mut self) {
        let generation = RequestGeneration::new();
        self.current_search_generation = generation.clone();
        self.searching = true;
        self.preview_loading = false;
        if let Err(message) = self.worker.send(WorkerRequest {
            kind: RequestKind::Search,
            generation,
            query: self.query.clone(),
            filters: self.filters.clone(),
            selected_id: self.selected_session().map(|session| session.id.clone()),
        }) {
            self.searching = false;
            self.error = Some(message);
            self.error_owner = Some(RequestKind::Search);
        }
    }

    /// Ask the worker for the selected row's preview, unless it is already on screen.
    ///
    /// The skip is load-bearing, not an optimisation: every search response calls this, and
    /// without it a preserved selection (D4) would rescan the transcript per keystroke.
    /// A replacement preview cancels only an older preview, never a Search (C21), so held
    /// navigation remains latest-value bounded without invalidating the result list.
    fn request_preview(&mut self) {
        let selected = self.selected_session().map(|session| session.id.clone());
        // Invalidate any outstanding preview success/error even when the already-rendered row is
        // selected again and no replacement I/O is needed (A→B→A fast path).
        let generation = RequestGeneration::new();
        self.current_preview_generation = generation.clone();
        if selected == self.previewed_id && self.error_owner != Some(RequestKind::PreviewOnly) {
            self.worker.cancel_preview();
            self.preview_loading = false;
            return;
        }
        self.preview_loading = true;
        if let Err(message) = self.worker.send(WorkerRequest {
            kind: RequestKind::PreviewOnly,
            generation,
            query: self.query.clone(),
            filters: self.filters.clone(),
            selected_id: selected,
        }) {
            self.preview_loading = false;
            self.error = Some(message);
            self.error_owner = Some(RequestKind::PreviewOnly);
        }
    }

    /// Revalidate after a filter binding and re-run the search. A rejected combination is
    /// rendered on the error line, never sent — the request would be unsatisfiable.
    fn apply_filter_change(&mut self) {
        if let Err(error) = self.filters.validate() {
            self.current_search_generation = RequestGeneration::new();
            self.worker.cancel_search();
            self.searching = false;
            self.error = Some(format!("{error:#}"));
            self.error_owner = Some(RequestKind::Search);
            return;
        }
        self.request_search();
    }

    /// Cycle the provider filter through `value_variants()` and back to unbounded.
    fn cycle_provider(&mut self) {
        let variants = Provider::value_variants();
        self.filters.provider = match self.filters.provider {
            None => Some(variants[0]),
            Some(current) => variants
                .iter()
                .position(|provider| *provider == current)
                .and_then(|index| variants.get(index + 1).copied()),
        };
        self.apply_filter_change();
    }

    /// Cycle the session-class filter: unbounded → both classes (the default search set) →
    /// user → subagent → unbounded. A kind set, never per-class booleans (the field's doc
    /// contract in models.rs).
    fn cycle_session_kinds(&mut self) {
        let both = SessionKind::default_search_set();
        self.filters.session_kinds = match &self.filters.session_kinds {
            None => Some(both),
            Some(kinds) if *kinds == both => Some(vec![SessionKind::User]),
            Some(kinds) if kinds.as_slice() == [SessionKind::User] => {
                Some(vec![SessionKind::Subagent])
            }
            _ => None,
        };
        self.apply_filter_change();
    }

    /// Cycle the time window: unbounded → 1 day → 7 days → 30 days → unbounded. Only `since`
    /// is set; `until` stays open, so the newest sessions always qualify.
    fn cycle_since_window(&mut self) {
        let next = match active_since_window(self.filters.since) {
            None => Some(0),
            Some(index) => Some(index + 1).filter(|next| *next < SINCE_WINDOWS.len()),
        };
        self.filters.since =
            next.map(|index| Utc::now() - chrono::Duration::hours(SINCE_WINDOWS[index].1));
        self.apply_filter_change();
    }

    fn toggle_warnings_only(&mut self) {
        self.filters.warnings_only = !self.filters.warnings_only;
        self.apply_filter_change();
    }

    /// True while nothing has been asked for: no query and every filter at its opening value.
    /// An empty list then means an empty index, which no key in here can fix.
    fn nothing_has_been_narrowed(&self) -> bool {
        self.query.is_empty()
            && self.filters.provider.is_none()
            && self.filters.session_kinds.is_none()
            && self.filters.since.is_none()
            && !self.filters.warnings_only
    }

    /// What to press when the list is empty.
    ///
    /// The keys are read from the bindings for the same reason the status bar reads them: a
    /// message that names `/` after a reader has rebound it sends them to press something that
    /// does nothing, which is worse than saying nothing. An empty index is a separate case and
    /// gets the command that fills it, because no key in the browser will.
    fn empty_state_guidance(&self) -> String {
        let mut lines: Vec<String> = Vec::new();
        if self.nothing_has_been_narrowed() {
            lines.push("No sessions are indexed yet.".to_string());
            lines.push(String::new());
            lines.push("Nothing here was filtered out; the index is empty. Leave the".to_string());
            lines.push("browser and run `aise reindex`, then start it again.".to_string());
        } else {
            lines.push("No sessions matched.".to_string());
            lines.push(String::new());
            lines.push("The query and filters in use are shown in the status bar.".to_string());
            lines.push(String::new());
            let narrowing: [(&str, &[TuiAction]); 2] = [
                ("edit the query", &[TuiAction::EnterSearch]),
                (
                    "change filters",
                    &[
                        TuiAction::CycleProvider,
                        TuiAction::CycleSessionKind,
                        TuiAction::CycleTimeWindow,
                        TuiAction::ToggleWarningsOnly,
                    ],
                ),
            ];
            for (label, actions) in narrowing {
                if let Some(hint) = self.key_hint(label, actions) {
                    lines.push(format!("  {hint}"));
                }
            }
        }
        if let Some(hint) = self.key_hint("every key", &[TuiAction::Help]) {
            lines.push(String::new());
            lines.push(format!("  {hint}"));
        }
        lines.join("\n")
    }

    fn filter_status(&self) -> String {
        let provider = self
            .filters
            .provider
            .map_or("any", |provider| provider.as_str());
        let class = match self.filters.session_kinds.as_deref() {
            None => "any",
            Some([SessionKind::User]) => "user",
            Some([SessionKind::Subagent]) => "subagent",
            Some(kinds) if *kinds == SessionKind::default_search_set() => "user+subagent",
            Some(_) => "custom",
        };
        let window =
            active_since_window(self.filters.since).map_or("any", |index| SINCE_WINDOWS[index].0);
        format!(
            "p:{provider} f:{class} s:{window} w:{}",
            if self.filters.warnings_only {
                "on"
            } else {
                "off"
            }
        )
    }

    /// One line per bound command: its keys, then what it does.
    ///
    /// Built from the same table the loop dispatches through, so a rebound key changes the help
    /// and an unbound command does not claim to exist. Without this the only place a command was
    /// named was the status bar, which sheds most of them on an eighty-column frame — paging,
    /// scrolling, top and bottom, and resume were unreachable for anyone who had not read the
    /// configuration file.
    fn help_lines(&self) -> Vec<String> {
        let mut lines = Vec::new();
        for (heading, mode) in [
            ("Browsing", ActionMode::Browse),
            ("Search box", ActionMode::Search),
        ] {
            let mut section: Vec<String> = Vec::new();
            for action in TuiAction::ALL {
                if action.mode() != mode {
                    continue;
                }
                let keys = self
                    .config
                    .ui
                    .keys
                    .chords(action)
                    .iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>();
                if keys.is_empty() {
                    continue;
                }
                section.push(format!("  {:<18} {}", keys.join(", "), action.name()));
            }
            if section.is_empty() {
                continue;
            }
            if !lines.is_empty() {
                lines.push(String::new());
            }
            lines.push(format!("{heading}:"));
            lines.extend(section);
        }
        // Both modes answer the interrupt, so it is listed once at the end rather than twice.
        let interrupt = self.config.ui.keys.chords(TuiAction::Interrupt);
        if !interrupt.is_empty() {
            let keys = interrupt
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join(", ");
            lines.push(String::new());
            lines.push(format!("  {keys:<18} interrupt (twice to quit)"));
        }
        lines
    }

    /// Scroll the key list, bounded the same way the preview is.
    fn scroll_help(&mut self, delta: isize) {
        let lines = self.help_lines().len();
        let visible = usize::from(self.help_viewport_rows).max(PREVIEW_VIEWPORT_SLACK);
        let max = u16::try_from(lines.saturating_sub(visible)).unwrap_or(u16::MAX);
        self.help_scroll = u16::try_from((self.help_scroll as isize).saturating_add(delta).max(0))
            .unwrap_or(u16::MAX)
            .min(max);
    }

    /// The key list's title, which is the only chrome it has.
    ///
    /// The overlay takes the whole frame, so it covers the status bar that would otherwise name
    /// the scroll keys — and the list is longer than a twenty-four-row terminal, so a reader met
    /// a list cut off at `cursor_left` with nothing saying more existed or how to reach it. The
    /// row count and the keys appear only when the list actually overflows, and the keys are read
    /// from the bindings like everywhere else.
    fn help_title(&self, lines: usize) -> String {
        let viewport = usize::from(self.help_viewport_rows);
        if viewport == 0 || lines <= viewport {
            return " Keys (any other key closes) ".to_string();
        }
        let first = usize::from(self.help_scroll).saturating_add(1);
        let last = first.saturating_add(viewport).saturating_sub(1).min(lines);
        let separator = self.style.title_separator();
        let scroll = self
            .key_hint(
                "scroll",
                &[TuiAction::PreviewScrollUp, TuiAction::PreviewScrollDown],
            )
            .map(|hint| format!("{hint} {separator} "))
            .unwrap_or_default();
        format!(
            " Keys {separator} {first}-{last}/{lines} {separator} {scroll}any other key closes "
        )
    }

    /// The preview pane's title, naming the visible rows when there are more than fit.
    ///
    /// Without it the pane gives no sign that the transcript continues below the fold: a reader
    /// who does not already know the scroll keys has no reason to look for them, and one who
    /// does cannot tell whether scrolling would do anything.
    fn preview_title(&self) -> String {
        let viewport = usize::from(self.preview_viewport_rows);
        if viewport == 0 || self.preview_line_count <= viewport {
            return " Preview ".to_string();
        }
        let first = usize::from(self.preview_scroll).saturating_add(1);
        let last = first
            .saturating_add(viewport)
            .saturating_sub(1)
            .min(self.preview_line_count);
        format!(
            " Preview {} {first}-{last}/{} ",
            self.style.title_separator(),
            self.preview_line_count
        )
    }

    /// `"j/k: move"`, built from the keys bound to `actions` rather than written out, so the
    /// bar teaches whatever `[ui.keys]` says. Only each action's first chord is named: the bar
    /// is one row, and `j` teaches the binding as well as `j/down` does. An action nobody bound
    /// is not advertised.
    fn key_hint(&self, label: &str, actions: &[TuiAction]) -> Option<String> {
        let keys = actions
            .iter()
            .filter_map(|action| self.config.ui.keys.chords(*action).first())
            .map(ToString::to_string)
            .collect::<Vec<_>>();
        (!keys.is_empty()).then(|| format!("{}: {label}", keys.join("/")))
    }

    /// Apply one worker response. Search responses replace the list and preserve the user's
    /// place; preview responses never replace the list (C13) and are discarded when overtaken
    /// by newer navigation.
    fn apply_response(&mut self, response: WorkerResponse) {
        if let Some(results) = response.results {
            // Search and preview failures are independent. A success clears only its own error,
            // so a preview completion cannot hide a failed search (or vice versa).
            if self.error_owner == Some(RequestKind::Search) {
                self.error = None;
                self.error_owner = None;
            }
            let keep = self.selected_session().map(|session| session.id.clone());
            self.results = results;
            // D4: keep the user's place when the same session survives into the new set.
            self.selected = keep
                .and_then(|id| self.results.iter().position(|session| session.id == id))
                .unwrap_or(0);
            // The worker could not know which row preservation would land on, so ask for its
            // preview now — unless D4 retained the already-previewed row, which
            // request_preview's skip guard turns into a no-op (C28).
            self.request_preview();
        }
        if let Some(preview) = response.preview {
            if response.previewed_id != self.selected_session().map(|s| s.id.clone()) {
                return;
            }
            if self.error_owner == Some(RequestKind::PreviewOnly) {
                self.error = None;
                self.error_owner = None;
            }
            let same_session = response.previewed_id == self.previewed_id;
            self.preview = preview;
            self.previewed_id = response.previewed_id;
            // A new row starts at the top. The same row keeps its place: the next draw
            // recomputes the wrapped row count and clamps against it, and every path that
            // applies a response draws before the next key is handled.
            if !same_session {
                self.preview_scroll = 0;
            }
        }
    }

    fn render(&mut self, frame: &mut ratatui::Frame<'_>) {
        let mut constraints = Vec::with_capacity(4);
        constraints.push(Constraint::Length(SEARCH_BOX_ROWS));
        constraints.push(Constraint::Min(MIN_BODY_ROWS));
        if self.error.is_some() {
            constraints.push(Constraint::Length(ERROR_LINE_ROWS));
        }
        constraints.push(Constraint::Length(STATUS_BAR_ROWS));
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints(constraints)
            .split(frame.area());

        // The search box draws the terminal's own cursor rather than a block character. Ratatui
        // shows it wherever a frame asks and hides it otherwise, so this needs no glyph, works
        // on a terminal whose encoding has none, and blinks and takes its shape from the
        // reader's own settings.
        let search_title = if self.search_mode {
            " Search (Enter/Esc to browse) "
        } else {
            " Search (press /) "
        };
        let search_border_style = if self.search_mode {
            Style::default().fg(Color::Yellow)
        } else {
            Style::default()
        };
        // A query wider than the box scrolls under it so the caret stays in view. Without this
        // the text a reader is typing disappears past the right border, cursor and all.
        let search_interior = chunks[0].width.saturating_sub(2);
        let before_cursor = u16::try_from(UnicodeWidthStr::width(&self.query[..self.query_cursor]))
            .unwrap_or(u16::MAX);
        let horizontal_scroll =
            before_cursor.saturating_sub(search_interior.saturating_sub(1).max(1));
        let top = Paragraph::new(self.query.clone())
            .scroll((0, horizontal_scroll))
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_set(self.style.border_set())
                    .border_style(search_border_style)
                    .title(search_title),
            );
        frame.render_widget(top, chunks[0]);
        if self.search_mode && search_interior > 0 && !self.showing_help {
            frame.set_cursor_position((
                chunks[0].x + 1 + before_cursor.saturating_sub(horizontal_scroll),
                chunks[0].y + 1,
            ));
        }

        // The list pane's share comes from [ui].list_pane_percent; the structural clamp keeps
        // both panes alive for out-of-range values instead of panicking on 100 - percent.
        let list_percent = self.config.ui.list_pane_percent.clamp(10, 90);
        let middle = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Percentage(list_percent),
                Constraint::Percentage(100 - list_percent),
            ])
            .split(chunks[1]);
        // The preview pane's interior height, minus its two border rows — recorded every draw
        // so the scroll clamp bounds by the actual viewport (D11).
        self.preview_viewport_rows = middle[1].height.saturating_sub(2);

        // Session list. Normal panes clamp the configured width up to the longest label; an
        // exceptionally narrow pane clamps to its actual interior because geometry must win when
        // the two requirements cannot both fit.
        let longest_label = longest_provider_label();
        let list_interior_width = usize::from(middle[0].width.saturating_sub(2));
        let label_width = self
            .config
            .ui
            .provider_label_width
            .max(longest_label)
            // A config value cannot request an allocation wider than the actual pane.
            .min(list_interior_width);
        // The marker is drawn on every row's worth of width whether or not that row is the
        // selected one, so that moving the selection does not shift the text sideways.
        let selection_symbol = self.style.selection_symbol();
        let selection_width = UnicodeWidthStr::width(selection_symbol);
        let visible_range = visible_session_range(
            self.results.len(),
            self.selected,
            usize::from(middle[0].height.saturating_sub(2)),
        );
        let items = self.results[visible_range.clone()]
            .iter()
            .map(|session| {
                // Title budget derived from the frame at render time (§6.3): the pane's
                // interior minus the label field and the age suffix — no fixed 74.
                let title_budget = (middle[0].width.saturating_sub(2) as usize)
                    .saturating_sub(label_width + 3)
                    .saturating_sub(AGE_SUFFIX_ALLOWANCE)
                    // The marker column is reserved on every row, selected or not, so the row
                    // has that much less width for its title.
                    .saturating_sub(selection_width);
                let title = session
                    .title
                    .as_deref()
                    .map(|value| truncate_for_display(value, title_budget))
                    .unwrap_or_else(|| session.preview_text.clone());
                let age = relative_age(session.updated_at);
                let (provider_name, provider_color) = provider_label(session.provider);
                let provider_name = truncate_for_display(provider_name, label_width);
                let mut spans = vec![Span::styled(
                    format!("[{provider_name:<label_width$}] "),
                    Style::default()
                        .fg(provider_color)
                        .add_modifier(Modifier::BOLD),
                )];
                spans.extend(marked_spans_with_style(
                    &highlight_matches(&title, &self.query),
                    Style::default(),
                    Style::default()
                        .fg(Color::Yellow)
                        .add_modifier(Modifier::BOLD),
                ));
                spans.push(Span::styled(
                    format!(" [{age}]"),
                    Style::default().fg(Color::DarkGray),
                ));
                ListItem::new(Line::from(spans))
            })
            .collect::<Vec<_>>();

        // D7: name the active ordering — the empty query lists `updated_at DESC`, a typed
        // query lists score order, and the screen says which.
        let mode = if self.query.trim().is_empty() {
            "recent"
        } else {
            "ranked"
        };
        let activity = if self.worker_disconnected_reported {
            "stopped"
        } else if self.searching {
            "searching"
        } else {
            "ready"
        };
        let dot = self.style.title_separator();
        let list_title = format!(
            " Sessions {dot} {mode} {dot} {activity} ({}/{}) ",
            if self.results.is_empty() {
                0
            } else {
                self.selected + 1
            },
            self.results.len()
        );
        let list = List::new(items)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_set(self.style.border_set())
                    .title(list_title),
            )
            .highlight_style(
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            )
            // A marker as well as the colour: a monochrome terminal, a colour-blind reader, and
            // a captured log all lose the highlight, and the selected row is the one Enter
            // resumes. `Always` reserves the column so the rows do not shift as it moves.
            .highlight_symbol(selection_symbol)
            .highlight_spacing(HighlightSpacing::Always);
        let mut list_state = ListState::default();
        if !visible_range.is_empty() {
            list_state.select(Some(self.selected - visible_range.start));
        }
        frame.render_stateful_widget(list, middle[0], &mut list_state);

        // Preview with scroll. With nothing in the list there is no session to preview, so the
        // pane carries the way out of the empty state instead of a sentence about it — but only
        // once the search that would fill the list has finished. `AppState::new` issues one and
        // sets `searching`, so an empty list is the ordinary state of the first seconds, and on
        // a large index that is seconds: telling a reader their index is empty while the search
        // that disproves it is still running would send them to rebuild it for nothing.
        let empty_state;
        let preview_body: &str = if self.results.is_empty()
            && !self.searching
            && self.error_owner != Some(RequestKind::Search)
            && !self.worker_disconnected_reported
        {
            empty_state = self.empty_state_guidance();
            &empty_state
        } else {
            &self.preview
        };
        let preview_body_lines = preview_body
            .lines()
            .map(|line| render_preview_line(line, &self.query, self.style))
            .collect::<Vec<_>>();
        let preview = Paragraph::new(preview_body_lines)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_set(self.style.border_set()),
            )
            .wrap(Wrap { trim: false });
        // Use the renderer's own WordWrapper rather than width arithmetic: wrapping at word
        // boundaries can produce more rows than ceil(display_width / pane_width). The count has
        // to exist before the title that reports it, and re-attaching a block does not re-wrap:
        // both blocks have the same borders, and a title does not change the interior width.
        //
        // `line_count` is behind ratatui's `unstable-rendered-line-info`, outside its semver
        // guarantee; Cargo.toml records why that is taken knowingly. If a future ratatui drops
        // it, this line is where the build stops, and the replacement has to measure what the
        // renderer produced rather than what the text implies.
        self.preview_line_count = preview.line_count(middle[1].width);
        self.clamp_preview_scroll();
        let preview = preview
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_set(self.style.border_set())
                    .title(self.preview_title()),
            )
            .scroll((self.preview_scroll, 0));
        frame.render_widget(preview, middle[1]);

        // Error line (own row, taken from the body when present — never the help bar),
        // middle-elided to the frame so the final recovery clause always survives (D9/REQ047).
        let status_index = chunks.len() - 1;
        let frame_width = frame.area().width as usize;
        if let Some(error) = &self.error {
            let error_line = Paragraph::new(Span::styled(
                elide_middle(error.as_str(), frame_width, self.style.ellipsis()),
                Style::default().fg(Color::Red),
            ));
            frame.render_widget(error_line, chunks[status_index - 1]);
        }

        // Status bar (single line, contextual) — also middle-elided to the frame, so the
        // navigation hints at the head and "q: quit" at the tail both survive a narrow frame.
        // Each hint names the keys actually bound to it. Written out, the bar kept teaching the
        // defaults after `[ui.keys]` changed them, which is worse than no bar: a reader presses
        // what it says and nothing happens.
        let mut hints: Vec<(u8, String)> = vec![(1, self.filter_status())];
        let labelled: &[(u8, &str, &[TuiAction])] = if self.search_mode {
            // The box binds eight editing commands and named none of them, so the three a
            // reader is least likely to guess were reachable only by leaving and pressing `?`.
            // Backspace, Delete, and the arrows are left out: they are what a text field does
            // everywhere, and the row is one line. The way out stays priority 0, because a
            // reader who cannot leave the box cannot reach anything else — including `?`,
            // which is a literal `?` in a query and so belongs to browse mode.
            &[
                (1, "type to search", &[]),
                (0, "browse", &[TuiAction::LeaveSearch]),
                (2, "clear", &[TuiAction::ClearQuery]),
                (3, "delete word", &[TuiAction::DeleteWordBackward]),
                (
                    4,
                    "line start/end",
                    &[TuiAction::CursorStart, TuiAction::CursorEnd],
                ),
            ]
        } else {
            &[
                (1, "move", &[TuiAction::MoveDown, TuiAction::MoveUp]),
                (4, "page", &[TuiAction::PageDown, TuiAction::PageUp]),
                (4, "top/bottom", &[TuiAction::Top, TuiAction::Bottom]),
                (
                    3,
                    "scroll",
                    &[TuiAction::PreviewScrollUp, TuiAction::PreviewScrollDown],
                ),
                (
                    2,
                    "filters",
                    &[
                        TuiAction::CycleProvider,
                        TuiAction::CycleSessionKind,
                        TuiAction::CycleTimeWindow,
                        TuiAction::ToggleWarningsOnly,
                    ],
                ),
                (1, "search", &[TuiAction::EnterSearch]),
                (2, "resume", &[TuiAction::Resume]),
                // Above every hint but quit: it is the one that names the rest, so on a frame
                // too narrow for them it is what the reader needs.
                (1, "keys", &[TuiAction::Help]),
                (0, "quit", &[TuiAction::Quit]),
            ]
        };
        for (priority, label, actions) in labelled {
            if actions.is_empty() {
                hints.push((*priority, (*label).to_string()));
            } else if let Some(text) = self.key_hint(label, actions) {
                hints.push((*priority, text));
            }
        }
        // Priority 0 and first, so the one hint that says how to leave cannot be shed to fit.
        if self.interrupt_armed {
            hints.insert(0, (0, "press the interrupt again to quit".to_string()));
        }
        let help_text = fit_status_hints(&hints, frame_width, self.style.hint_separator());
        let bottom = Paragraph::new(Span::styled(
            elide_middle(&help_text, frame_width, self.style.ellipsis()),
            Style::default().fg(Color::DarkGray),
        ));
        frame.render_widget(bottom, chunks[status_index]);

        // Last, over everything, and `Clear` first so the panes underneath do not show through
        // where the list is shorter than the block. It takes the whole frame rather than a
        // centred box: the list is the tallest thing the browser draws, and a terminal with ten
        // rows would otherwise show two of it.
        if self.showing_help {
            let area = frame.area();
            self.help_viewport_rows = area.height.saturating_sub(2);
            let lines = self.help_lines();
            let body = lines.join("\n");
            self.scroll_help(0);
            // The overlay is not wrapped, so ratatui truncates a long row rather than folding it:
            // one entry is one rendered row and the title can count them directly.
            let title = self.help_title(lines.len());
            let overlay = Paragraph::new(body)
                .block(
                    Block::default()
                        .borders(Borders::ALL)
                        .border_set(self.style.border_set())
                        .title(title),
                )
                .scroll((self.help_scroll, 0));
            frame.render_widget(Clear, area);
            frame.render_widget(overlay, area);
        }

        // One choke point rather than a colour decision at every span. Twenty call sites each
        // remembering to ask would be twenty chances to forget, and the next widget somebody
        // adds would forget by default; clearing the finished frame cannot be bypassed. The
        // attributes stay, so bold and italic still carry the emphasis the colour did.
        if !self.style.color() {
            for cell in frame.buffer_mut().content.iter_mut() {
                cell.set_fg(Color::Reset);
                cell.set_bg(Color::Reset);
            }
        }
    }

    /// Move the selection locally — the UI never waits for the worker (G2) — and ask for the
    /// new row's preview.
    fn move_selection(&mut self, delta: isize) {
        if self.results.is_empty() {
            return;
        }
        let new = (self.selected as isize)
            .saturating_add(delta)
            .clamp(0, self.results.len() as isize - 1) as usize;
        if new != self.selected {
            self.selected = new;
            self.preview_scroll = 0;
            self.request_preview();
        }
    }

    fn select_index(&mut self, index: usize) {
        if self.results.is_empty() {
            return;
        }
        let new = index.min(self.results.len() - 1);
        if new != self.selected {
            self.selected = new;
            self.preview_scroll = 0;
            self.request_preview();
        }
    }

    /// The one place the scroll bound is expressed; `apply_response`'s same-session branch
    /// reuses it so the two cannot drift (D11). Bounding by viewport stops the scroll when
    /// the last line reaches the pane bottom — the old content-minus-3 bound let the pane
    /// empty out as you scrolled.
    fn clamp_preview_scroll(&mut self) {
        let visible = usize::from(self.preview_viewport_rows).max(PREVIEW_VIEWPORT_SLACK);
        let max =
            u16::try_from(self.preview_line_count.saturating_sub(visible)).unwrap_or(u16::MAX);
        self.preview_scroll = self.preview_scroll.min(max);
    }

    fn scroll_preview(&mut self, delta: isize) {
        self.preview_scroll =
            u16::try_from((self.preview_scroll as isize).saturating_add(delta).max(0))
                .unwrap_or(u16::MAX);
        self.clamp_preview_scroll();
    }

    fn selected_session(&self) -> Option<&SessionRecord> {
        self.results.get(self.selected)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TurnRole {
    User,
    Assistant,
}

impl TurnRole {
    fn parse(line: &str) -> Option<Self> {
        let close = line.strip_prefix('[')?.find(']')?;
        // close is the index of ']' within the post-'[' slice; role text is after the ']'.
        let after = &line[close + 2..];
        match after.trim() {
            "user" => Some(Self::User),
            "assistant" => Some(Self::Assistant),
            _ => None,
        }
    }
}

#[derive(Clone, Copy)]
struct Turn<'a> {
    ordinal: usize,
    body: &'a str,
}

#[cfg(test)]
struct ParsedTurn<'a> {
    role: TurnRole,
    body: &'a str,
}

#[cfg(test)]
fn parse_turns(transcript: &str) -> Vec<ParsedTurn<'_>> {
    parse_turns_inner(transcript, None).expect("an uncancelled parse cannot fail")
}

#[cfg(test)]
fn parse_turns_inner<'a>(
    transcript: &'a str,
    cancellation: Option<&QueryCancellation>,
) -> Result<Vec<ParsedTurn<'a>>> {
    let mut turns: Vec<ParsedTurn<'a>> = Vec::new();
    let mut current_role: Option<TurnRole> = None;
    let mut body_start: usize = 0;

    let mut cursor = 0usize;
    while cursor < transcript.len() {
        if let Some(cancellation) = cancellation {
            cancellation.ensure_active()?;
        }
        let line_end = transcript[cursor..]
            .find('\n')
            .map(|i| cursor + i)
            .unwrap_or(transcript.len());
        let line = &transcript[cursor..line_end];
        if let Some(role) = TurnRole::parse(line) {
            if let Some(prev) = current_role.take() {
                let body = transcript[body_start..cursor].trim_matches('\n');
                turns.push(ParsedTurn { role: prev, body });
            }
            current_role = Some(role);
            body_start = (line_end + 1).min(transcript.len());
        }
        cursor = if line_end == transcript.len() {
            transcript.len()
        } else {
            line_end + 1
        };
    }
    if let Some(prev) = current_role {
        let body = transcript[body_start..].trim_matches('\n');
        turns.push(ParsedTurn { role: prev, body });
    }
    if let Some(cancellation) = cancellation {
        cancellation.ensure_active()?;
    }
    Ok(turns)
}

#[cfg(test)]
fn truncate_body(body: &str, max_lines: usize) -> String {
    truncate_body_inner(body, max_lines, TerminalStyle::default().ellipsis(), None)
        .expect("an uncancelled copy cannot fail")
}

fn push_cancellable(
    output: &mut String,
    text: &str,
    cancellation: Option<&QueryCancellation>,
) -> Result<()> {
    let mut start = 0;
    while start < text.len() {
        if let Some(cancellation) = cancellation {
            cancellation.ensure_active()?;
        }
        let mut end = start
            .saturating_add(TRANSCRIPT_CANCELLATION_CHUNK_BYTES)
            .min(text.len());
        while end < text.len() && !text.is_char_boundary(end) {
            end -= 1;
        }
        output.push_str(&text[start..end]);
        start = end;
    }
    Ok(())
}

fn trim_end_cancellable<'a>(
    body: &'a str,
    cancellation: Option<&QueryCancellation>,
) -> Result<&'a str> {
    let mut end = body.len();
    let mut checked_at = end;
    for (index, character) in body.char_indices().rev() {
        if checked_at.saturating_sub(index) >= TRANSCRIPT_CANCELLATION_CHUNK_BYTES {
            if let Some(cancellation) = cancellation {
                cancellation.ensure_active()?;
            }
            checked_at = index;
        }
        if character.is_whitespace() {
            end = index;
        } else {
            break;
        }
    }
    Ok(&body[..end])
}

fn truncate_body_inner(
    body: &str,
    max_lines: usize,
    ellipsis: &str,
    cancellation: Option<&QueryCancellation>,
) -> Result<String> {
    let trimmed = trim_end_cancellable(body, cancellation)?;
    if trimmed.is_empty() {
        return Ok("(empty)".to_string());
    }
    let mut lines = Vec::with_capacity(max_lines.min(64));
    let mut cursor = 0;
    while cursor < trimmed.len() && lines.len() < max_lines {
        let line_end = next_transcript_line_end(trimmed, cursor, cancellation)?;
        lines.push(&trimmed[cursor..line_end]);
        cursor = if line_end == trimmed.len() {
            trimmed.len()
        } else {
            line_end + 1
        };
    }
    if cursor == trimmed.len() {
        let mut output = String::with_capacity(trimmed.len());
        push_cancellable(&mut output, trimmed, cancellation)?;
        return Ok(output);
    }
    let selected_bytes = lines.iter().fold(0_usize, |bytes, line| {
        bytes.saturating_add(line.len()).saturating_add(1)
    });
    let mut output = String::with_capacity(selected_bytes);
    for (index, line) in lines.iter().enumerate() {
        if index > 0 {
            output.push('\n');
        }
        push_cancellable(&mut output, line, cancellation)?;
    }
    output.push_str("\n  [");
    output.push_str(ellipsis);
    output.push(']');
    Ok(output)
}

/// Relative weights for the preview summary's sections (Decision 2a): first prompt, first
/// reply, final prompt, final reply. `[ui].preview_body_lines` is the total body budget; each
/// emitted section gets a proportional share (floor-rounded), floored at one line so a small
/// budget cannot erase a bookend. The historical fixed layout was these weights verbatim —
/// a budget of 34 reproduces it exactly.
/// The preview's section headings, without the rule that surrounds them: the rule is a symbol
/// the terminal may not carry, so it is supplied at build time and matched on at render time.
const PREVIEW_SECTION_LABELS: [&str; 4] =
    ["First prompt", "First reply", "Final prompt", "Final reply"];

const PREVIEW_WEIGHT_FIRST_PROMPT: usize = 8;
const PREVIEW_WEIGHT_FIRST_REPLY: usize = 4;
const PREVIEW_WEIGHT_FINAL_PROMPT: usize = 8;
const PREVIEW_WEIGHT_FINAL_REPLY: usize = 14;
const HISTORICAL_PREVIEW_BODY_LINES: usize = PREVIEW_WEIGHT_FIRST_PROMPT
    + PREVIEW_WEIGHT_FIRST_REPLY
    + PREVIEW_WEIGHT_FINAL_PROMPT
    + PREVIEW_WEIGHT_FINAL_REPLY;
/// Maximum transcript bytes scanned between cooperative cancellation checks. Shared with the
/// caseless matcher and whitespace compaction so all three scans answer a cancel on the same
/// bound; see [`crate::util::CANCELLATION_CHECK_BYTES`].
const TRANSCRIPT_CANCELLATION_CHUNK_BYTES: usize = crate::util::CANCELLATION_CHECK_BYTES;

#[cfg(test)]
fn build_transcript_summary(transcript: &str, budget: usize) -> String {
    build_transcript_summary_inner(transcript, budget, TerminalStyle::default(), None)
        .expect("an uncancelled summary cannot fail")
}

fn build_transcript_summary_cancellable(
    transcript: &str,
    budget: usize,
    style: TerminalStyle,
    cancellation: &QueryCancellation,
) -> Result<String> {
    build_transcript_summary_inner(transcript, budget, style, Some(cancellation))
}

#[derive(Default)]
struct TranscriptBookends<'a> {
    total: usize,
    first_user: Option<Turn<'a>>,
    first_assistant: Option<Turn<'a>>,
    last_user: Option<Turn<'a>>,
    last_assistant: Option<Turn<'a>>,
}

fn remember_transcript_turn<'a>(
    bookends: &mut TranscriptBookends<'a>,
    role: TurnRole,
    body: &'a str,
) {
    let turn = Turn {
        ordinal: bookends.total,
        body,
    };
    match role {
        TurnRole::User => {
            bookends.first_user.get_or_insert(turn);
            bookends.last_user = Some(turn);
        }
        TurnRole::Assistant => {
            bookends.first_assistant.get_or_insert(turn);
            bookends.last_assistant = Some(turn);
        }
    }
    bookends.total = bookends.total.saturating_add(1);
}

fn next_transcript_line_end(
    transcript: &str,
    cursor: usize,
    cancellation: Option<&QueryCancellation>,
) -> Result<usize> {
    let bytes = transcript.as_bytes();
    let mut chunk_start = cursor;
    while chunk_start < bytes.len() {
        if let Some(cancellation) = cancellation {
            cancellation.ensure_active()?;
        }
        let mut chunk_end = chunk_start
            .saturating_add(TRANSCRIPT_CANCELLATION_CHUNK_BYTES)
            .min(bytes.len());
        while chunk_end < bytes.len() && !transcript.is_char_boundary(chunk_end) {
            chunk_end -= 1;
        }
        if let Some(relative) = transcript[chunk_start..chunk_end].find('\n') {
            return Ok(chunk_start + relative);
        }
        chunk_start = chunk_end;
    }
    Ok(bytes.len())
}

fn trim_newlines_cancellable<'a>(
    text: &'a str,
    cancellation: Option<&QueryCancellation>,
) -> Result<&'a str> {
    let bytes = text.as_bytes();
    let mut start = 0;
    let mut next_check = 0;
    while start < bytes.len() && bytes[start] == b'\n' {
        if start == next_check {
            if let Some(cancellation) = cancellation {
                cancellation.ensure_active()?;
            }
            next_check = next_check.saturating_add(TRANSCRIPT_CANCELLATION_CHUNK_BYTES);
        }
        start += 1;
    }
    let mut end = bytes.len();
    let mut scanned = 0;
    next_check = 0;
    while end > start && bytes[end - 1] == b'\n' {
        if scanned == next_check {
            if let Some(cancellation) = cancellation {
                cancellation.ensure_active()?;
            }
            next_check = next_check.saturating_add(TRANSCRIPT_CANCELLATION_CHUNK_BYTES);
        }
        end -= 1;
        scanned += 1;
    }
    Ok(&text[start..end])
}

fn transcript_bookends<'a>(
    transcript: &'a str,
    cancellation: Option<&QueryCancellation>,
) -> Result<TranscriptBookends<'a>> {
    let mut bookends = TranscriptBookends::default();
    let mut current_role = None;
    let mut body_start = 0;
    let mut cursor = 0;
    while cursor < transcript.len() {
        let line_end = next_transcript_line_end(transcript, cursor, cancellation)?;
        let line = &transcript[cursor..line_end];
        if let Some(role) = TurnRole::parse(line) {
            if let Some(previous) = current_role.take() {
                let body =
                    trim_newlines_cancellable(&transcript[body_start..cursor], cancellation)?;
                remember_transcript_turn(&mut bookends, previous, body);
            }
            current_role = Some(role);
            body_start = (line_end + 1).min(transcript.len());
        }
        cursor = if line_end == transcript.len() {
            transcript.len()
        } else {
            line_end + 1
        };
    }
    if let Some(role) = current_role {
        let body = trim_newlines_cancellable(&transcript[body_start..], cancellation)?;
        remember_transcript_turn(&mut bookends, role, body);
    }
    if let Some(cancellation) = cancellation {
        cancellation.ensure_active()?;
    }
    Ok(bookends)
}

fn build_transcript_summary_inner(
    transcript: &str,
    budget: usize,
    style: TerminalStyle,
    cancellation: Option<&QueryCancellation>,
) -> Result<String> {
    let bookends = transcript_bookends(transcript, cancellation)?;
    if bookends.total == 0 {
        return Ok("(no transcript content)".to_string());
    }

    let rule = style.section_rule();
    let heading = |index: usize| format!("{rule} {} {rule}", PREVIEW_SECTION_LABELS[index]);
    let candidates = [
        (bookends.first_user, heading(0), PREVIEW_WEIGHT_FIRST_PROMPT),
        (
            bookends.first_assistant,
            heading(1),
            PREVIEW_WEIGHT_FIRST_REPLY,
        ),
        (bookends.last_user, heading(2), PREVIEW_WEIGHT_FINAL_PROMPT),
        (
            bookends.last_assistant,
            heading(3),
            PREVIEW_WEIGHT_FINAL_REPLY,
        ),
    ];
    let mut shown_ordinals = Vec::with_capacity(4);
    let mut sections = Vec::with_capacity(4);
    for (turn, label, weight) in candidates {
        let Some(turn) = turn else { continue };
        if shown_ordinals.contains(&turn.ordinal) {
            continue;
        }
        shown_ordinals.push(turn.ordinal);
        sections.push((turn.ordinal, label, weight, turn.body));
    }
    sections.sort_by_key(|(index, _, _, _)| *index);
    render_summary_sections(&sections, bookends.total, budget, style, cancellation)
}

fn render_summary_sections(
    sections: &[(usize, String, usize, &str)],
    total: usize,
    budget: usize,
    style: TerminalStyle,
    cancellation: Option<&QueryCancellation>,
) -> Result<String> {
    let hidden = total.saturating_sub(sections.len());
    let mut parts = Vec::new();
    let mut last_emitted_idx = None;
    for (idx, label, weight, body) in sections {
        if let Some(prev) = last_emitted_idx {
            if *idx > prev + 1 {
                let gap = *idx - prev - 1;
                let marker = style.elision_marker();
                parts.push(format!(
                    "{marker} {gap} more turn{} hidden {marker}",
                    if gap == 1 { "" } else { "s" }
                ));
            }
        }
        parts.push(label.clone());
        let max_lines = (budget.saturating_mul(*weight) / HISTORICAL_PREVIEW_BODY_LINES).max(1);
        parts.push(truncate_body_inner(
            body,
            max_lines,
            style.ellipsis(),
            cancellation,
        )?);
        last_emitted_idx = Some(*idx);
    }
    if hidden > 0 && sections.len() < 2 {
        let marker = style.elision_marker();
        parts.push(format!(
            "{marker} {hidden} more turn{} hidden {marker}",
            if hidden == 1 { "" } else { "s" }
        ));
    }
    parts.push(format!(
        "({total} turn{} total)",
        if total == 1 { "" } else { "s" }
    ));
    Ok(parts.join("\n\n"))
}

fn render_preview_line(line: &str, query: &str, style: TerminalStyle) -> Line<'static> {
    if let Some(session_id) = line.strip_prefix("Session: ") {
        let mut spans = vec![Span::styled(
            "Session: ",
            Style::default()
                .fg(Color::DarkGray)
                .add_modifier(Modifier::BOLD),
        )];
        spans.extend(marked_spans_with_style(
            &highlight_matches(session_id, query),
            Style::default().fg(Color::Gray),
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        ));
        return Line::from(spans);
    }

    if let Some(cwd) = line.strip_prefix("CWD: ") {
        let mut spans = vec![Span::styled(
            "CWD: ",
            Style::default()
                .fg(Color::DarkGray)
                .add_modifier(Modifier::BOLD),
        )];
        spans.extend(marked_spans_with_style(
            &highlight_matches(cwd, query),
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        ));
        return Line::from(spans);
    }

    if line.starts_with(&format!("{} ", style.section_rule())) {
        let style = if line.contains("prompt") {
            Style::default()
                .fg(Color::Green)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default()
                .fg(Color::Blue)
                .add_modifier(Modifier::BOLD)
        };
        return Line::from(Span::styled(line.to_string(), style));
    }

    if line.starts_with(&format!("{} ", style.elision_marker()))
        || line.starts_with('(') && line.ends_with(" total)")
    {
        return Line::from(Span::styled(
            line.to_string(),
            Style::default()
                .fg(Color::DarkGray)
                .add_modifier(Modifier::ITALIC),
        ));
    }

    Line::from(marked_spans_with_style(
        &highlight_matches(line, query),
        Style::default(),
        Style::default()
            .fg(Color::Yellow)
            .add_modifier(Modifier::BOLD),
    ))
}

fn marked_spans_with_style(
    input: &str,
    base_style: Style,
    highlight_style: Style,
) -> Vec<Span<'static>> {
    let mut spans = Vec::new();
    let mut rest = input;

    while let Some(start) = rest.find("[[") {
        if start > 0 {
            spans.push(Span::styled(rest[..start].to_string(), base_style));
        }
        let after_start = &rest[start + 2..];
        if let Some(end) = after_start.find("]]") {
            spans.push(Span::styled(
                after_start[..end].to_string(),
                highlight_style,
            ));
            rest = &after_start[end + 2..];
        } else {
            spans.push(Span::styled(rest[start..].to_string(), base_style));
            rest = "";
            break;
        }
    }

    if !rest.is_empty() {
        spans.push(Span::styled(rest.to_string(), base_style));
    }

    if spans.is_empty() {
        spans.push(Span::styled(String::new(), base_style));
    }

    spans
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::terminal_style::CapabilityMode;
    use ratatui::backend::TestBackend;
    use std::collections::VecDeque;

    /// Replays a fixed event script and records every poll timeout, so tests can assert the
    /// loop's pacing without a clock.
    struct ScriptedEventSource {
        events: VecDeque<Event>,
        poll_timeouts: Vec<Duration>,
    }

    impl ScriptedEventSource {
        fn new(events: Vec<Event>) -> Self {
            Self {
                events: events.into(),
                poll_timeouts: Vec::new(),
            }
        }
    }

    impl EventSource for ScriptedEventSource {
        fn poll(&mut self, timeout: Duration) -> io::Result<bool> {
            self.poll_timeouts.push(timeout);
            if !self.events.is_empty() {
                return Ok(true);
            }
            // Mirror the real source: reporting no input waits out the requested timeout.
            // The sliced wait in step() would otherwise busy-spin through the idle window.
            std::thread::sleep(timeout);
            Ok(false)
        }

        fn read(&mut self) -> io::Result<Event> {
            self.events
                .pop_front()
                .ok_or_else(|| io::Error::new(io::ErrorKind::UnexpectedEof, "script exhausted"))
        }
    }

    fn key(code: KeyCode) -> Event {
        Event::Key(KeyEvent::new(code, KeyModifiers::NONE))
    }

    fn ctrl_key(code: KeyCode) -> Event {
        Event::Key(KeyEvent::new(code, KeyModifiers::CONTROL))
    }

    /// One seeded row: per-id path, id, provider_session_id, AND title. Every override is
    /// load-bearing — `minimal_record` leaves `title: None` and derives the session id from the
    /// file stem, and `upsert_session` keys on that id, so a shared path collapses rows.
    fn session(id: &str) -> crate::models::ParsedSession {
        let path = std::path::Path::new("/fixture").join(format!("{id}.jsonl"));
        let mut parsed = crate::util::minimal_record(Provider::Claude, &path, String::new());
        parsed.session.id = id.to_string();
        parsed.session.provider_session_id = id.to_string();
        parsed.session.title = Some(id.to_string());
        parsed
    }

    fn screen_text(terminal: &Terminal<TestBackend>) -> String {
        let buffer = terminal.backend().buffer();
        let area = buffer.area;
        let mut lines = Vec::new();
        for y in area.top()..area.bottom() {
            let mut line = String::new();
            for x in area.left()..area.right() {
                line.push_str(buffer[(x, y)].symbol());
            }
            lines.push(line);
        }
        lines.join("\n")
    }

    /// Maximum loop turns a scripted scenario may take before the harness gives up, panicking
    /// with the rendered screen (P9).
    const MAX_TEST_STEPS: usize = 64;

    /// An executor for scenarios that never send requests (pre-step-4 key handling still runs
    /// inline). It answers honestly if invoked, so an accidental send surfaces as a visible
    /// state change instead of a hang.
    fn idle_executor() -> SearchExecutor {
        Box::new(
            |request: &WorkerRequest, _cancellation: &Arc<QueryCancellation>| {
                Ok(match request.kind {
                    // The empty-query startup must answer with navigable rows: an empty
                    // answer would wipe the seeded fixture on the first drain.
                    RequestKind::Search if request.query.trim().is_empty() => {
                        WorkerResponse::results(
                            request,
                            rows(&["claude:idle-one", "claude:idle-two", "claude:idle-three"]),
                        )
                    }
                    RequestKind::Search => WorkerResponse::results(request, Vec::new()),
                    RequestKind::PreviewOnly => WorkerResponse::preview(request, None),
                })
            },
        )
    }

    /// One scripted TUI: owns the terminal, event script, app state, and fixture databases.
    /// No `'static` test leaks: production-executor fixtures attach their TempDir here.
    struct TuiHarness {
        // Field order is drop order: stop/join the worker, close the fixture Db, then remove
        // directories. This matters on Windows, where an open SQLite file cannot be unlinked.
        app: AppState,
        db: Db,
        terminal: Terminal<TestBackend>,
        events: ScriptedEventSource,
        external_fixture: Option<tempfile::TempDir>,
        _dir: tempfile::TempDir,
    }

    impl TuiHarness {
        fn with_executor(executor: SearchExecutor) -> Self {
            Self::with_config(Config::default(), executor)
        }

        fn with_config(config: Config, executor: SearchExecutor) -> Self {
            Self::with_factory(
                config,
                Box::new(move || Ok((executor, EffectiveAccessScope::All))),
            )
        }

        /// Like `with_config`, but the caller owns the whole factory (the digest guard passes
        /// the production `db_backed_executor` against its own fixture).
        fn with_factory(mut config: Config, factory: ExecutorFactory) -> Self {
            // The real 150 ms idle pacing makes every step wait it out against the
            // sleeping scripted source; shrink the default so steps complete fast. A test
            // that sets the field explicitly keeps its value.
            // Compared against the shipped default rather than a literal: a literal that stops
            // matching would silently hand every scripted test the real timings and hang them.
            let shipped = Config::default().ui;
            if config.ui.idle_poll_interval_ms == shipped.idle_poll_interval_ms {
                config.ui.idle_poll_interval_ms = 1;
            }
            // Same reason for the typing delay: a scripted test presses a key and then asserts
            // on what the search did, with no wall clock advancing in between. Zero is the
            // documented "search on every keystroke" setting, so those tests read as they did
            // before the delay existed. The three tests that are about the delay set their own.
            if config.ui.search_debounce_ms == shipped.search_debounce_ms {
                config.ui.search_debounce_ms = 0;
            }
            let (worker, observed) =
                spawn_search_worker(factory).expect("worker startup handshake");
            assert!(matches!(observed, EffectiveAccessScope::All));
            let dir = tempfile::tempdir().unwrap();
            let db = Db::open(&dir.path().join("index.db")).unwrap();
            let app = AppState::new_quiet(config, worker);
            Self {
                _dir: dir,
                external_fixture: None,
                db,
                terminal: Terminal::new(TestBackend::new(100, 24)).unwrap(),
                events: ScriptedEventSource::new(Vec::new()),
                app,
            }
        }

        fn own_external_fixture(mut self, fixture: tempfile::TempDir) -> Self {
            self.external_fixture = Some(fixture);
            self
        }

        /// Install the starting rows on `AppState::results` AND in the fixture database, then
        /// send the startup request — construction is quiet, so the startup response and the
        /// seeded rows always agree.
        fn seeded(mut self, sessions: &[&str]) -> Self {
            let mut records = Vec::new();
            for id in sessions {
                let parsed = session(id);
                self.db.upsert_session(&parsed, 0, 0).unwrap();
                records.push(parsed.session);
            }
            self.app.results = records;
            self.app.request_search();
            self
        }

        /// Send the startup request for a harness that seeded nothing (the digest guard's
        /// real-executor path, whose fixture lives in the factory's own database).
        #[allow(dead_code)]
        fn start(&mut self) {
            self.app.request_search();
        }

        /// Step until the given session's preview is applied — the C28 settled point —
        /// bounded by MAX_TEST_STEPS, panicking with the rendered screen on expiry.
        fn wait_until_previewed(&mut self, id: &str) {
            for _ in 0..MAX_TEST_STEPS {
                if self.app.previewed_id.as_deref() == Some(id) {
                    return;
                }
                self.step();
            }
            panic!(
                "preview for {id} never applied; previewed={:?}, screen:\n{}",
                self.app.previewed_id,
                self.screen()
            );
        }

        fn script(&mut self, events: Vec<Event>) {
            self.events = ScriptedEventSource::new(events);
        }

        fn step(&mut self) -> Option<AppAction> {
            step(&mut self.terminal, &mut self.events, &mut self.app).unwrap()
        }

        /// Step until the script has drained, then one more turn: `step` is draw-then-drain,
        /// so the turn that consumes the last key renders the pre-key state — without the
        /// extra step, post-script assertions read a stale frame (C32).
        fn step_until_script_drained(&mut self) -> Option<AppAction> {
            let mut turns = 0;
            let mut action = None;
            while !self.events.events.is_empty() && action.is_none() {
                action = self.step();
                turns += 1;
                assert!(
                    turns < MAX_TEST_STEPS,
                    "script did not drain after {MAX_TEST_STEPS} steps; screen:\n{}",
                    self.screen()
                );
            }
            action.or_else(|| self.step())
        }

        fn screen(&self) -> String {
            screen_text(&self.terminal)
        }

        fn region_text(&self, rows: std::ops::Range<u16>) -> String {
            let buffer = self.terminal.backend().buffer();
            let width = buffer.area.width;
            let mut lines = Vec::new();
            for y in rows {
                let mut line = String::new();
                for x in 0..width {
                    line.push_str(buffer[(x, y)].symbol());
                }
                lines.push(line.trim_end().to_string());
            }
            lines.join("\n")
        }

        fn search_box(&self) -> String {
            self.region_text(0..SEARCH_BOX_ROWS)
        }

        /// The session-list region: below the search box, above the status bar and the
        /// possible error line, at full width (containment assertions do not need the pane
        /// split).
        fn session_rows(&self) -> String {
            let area = self.terminal.backend().buffer().area;
            let bottom = area.height - STATUS_BAR_ROWS - ERROR_LINE_ROWS;
            self.region_text(SEARCH_BOX_ROWS..bottom)
        }

        /// The row where the error line renders when an error is present. Without an error the
        /// row belongs to the body's bottom border, so only read it while `app.error` is set.
        fn error_line(&self) -> String {
            let area = self.terminal.backend().buffer().area;
            self.region_text(
                area.height - STATUS_BAR_ROWS - ERROR_LINE_ROWS..area.height - STATUS_BAR_ROWS,
            )
        }

        fn status_line(&self) -> String {
            let area = self.terminal.backend().buffer().area;
            self.region_text(area.height - STATUS_BAR_ROWS..area.height)
        }
    }

    #[test]
    fn event_seam_reproduces_current_keybindings() {
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&[
            "claude:alpha",
            "claude:beta",
            "claude:gamma",
        ]);
        harness.wait_until_previewed("claude:idle-one");
        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('a'))]);
        assert!(harness.step_until_script_drained().is_none());
        assert!(harness.app.search_mode);
        assert_eq!(harness.app.query, "a");
        assert!(
            harness.search_box().contains('a'),
            "typed character renders in the box"
        );
        // The caret is the terminal's own, placed by the frame, so it is not a character in the
        // buffer: after `a` it sits one column past it.
        assert_eq!(
            harness.terminal.get_cursor_position().unwrap().x,
            harness.terminal.get_frame().area().x + 2
        );
        assert!(harness.session_rows().contains("Sessions"));

        // Esc in search mode returns to browse without quitting.
        harness.script(vec![key(KeyCode::Esc)]);
        assert!(harness.step_until_script_drained().is_none());
        assert!(!harness.app.search_mode);

        // Backspace to an empty query re-browses without panicking (search mode first:
        // browse mode has no Backspace arm — that IS the current contract).
        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Backspace)]);
        assert!(harness.step_until_script_drained().is_none());
        assert_eq!(harness.app.query, "");
        // Settle the re-browse response before navigating: the empty-query response must
        // re-apply the idle rows, or j runs against the 'a' section's empty result set.
        harness.wait_until_previewed("claude:idle-one");

        // Esc returns to browse so the navigation keys below apply.
        harness.script(vec![key(KeyCode::Esc)]);
        harness.step_until_script_drained();
        assert!(!harness.app.search_mode);

        // j moves the selection and loads the preview of the new row.
        harness.script(vec![key(KeyCode::Char('j'))]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.selected, 1);
        // Settle the row-1 preview BEFORE the manual line count: a late preview response
        // would overwrite it and clamp the scroll back to zero mid-test.
        harness.wait_until_previewed("claude:idle-two");

        // Ctrl-d scrolls the preview by the page step against the content-length bound.
        harness.app.preview = (0..100)
            .map(|line| format!("line {line}"))
            .collect::<Vec<_>>()
            .join("\n");
        harness.script(vec![ctrl_key(KeyCode::Char('d'))]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.preview_scroll, 15);

        // Enter on a selected session yields the resume action; q quits from browse mode.
        harness.script(vec![key(KeyCode::Enter)]);
        assert!(matches!(
            harness.step_until_script_drained(),
            Some(AppAction::Resume(_))
        ));
        harness.script(vec![key(KeyCode::Char('q'))]);
        assert!(matches!(
            harness.step_until_script_drained(),
            Some(AppAction::Quit)
        ));

        // g/G/PageDown/PageUp on an EMPTY result set must not panic.
        let mut empty = TuiHarness::with_executor(idle_executor());
        empty.script(vec![
            key(KeyCode::Char('g')),
            key(KeyCode::Char('G')),
            key(KeyCode::PageDown),
            key(KeyCode::PageUp),
        ]);
        assert!(empty.step_until_script_drained().is_none());
    }

    #[test]
    fn ui_interaction_fields_reach_the_loop() {
        let mut config = Config::default();
        config.ui.idle_poll_interval_ms = 7;
        config.ui.list_page_rows = 2;
        config.ui.preview_scroll_rows = 3;
        config.ui.preview_page_rows = 4;
        let mut harness = TuiHarness::with_config(config, idle_executor()).seeded(&[
            "claude:one",
            "claude:two",
            "claude:three",
            "claude:four",
        ]);
        harness.wait_until_previewed("claude:idle-one");

        // The idle poll paces with the configured interval, observed clock-free through the
        // seam's recorded timeouts (every poll — including the one that returns false).
        harness.script(vec![key(KeyCode::PageDown)]);
        assert!(harness.step_until_script_drained().is_none());
        // The idle wait is sliced (worker responses are picked up between slices), so each
        // recorded poll is the deadline-derived remainder: bounded by the configured
        // interval, never exceeding it, and never a stale default.
        assert!(
            harness.events.poll_timeouts.iter().all(|timeout| {
                *timeout <= Duration::from_millis(7) && *timeout >= Duration::from_millis(5)
            }),
            "poll slices must stay within [ui].idle_poll_interval_ms, got {:?}",
            harness.events.poll_timeouts
        );

        // PageDown moves by the configured list page step, not a hardcoded 10.
        assert_eq!(harness.app.selected, 2);

        // J scrolls by the configured preview scroll step; Ctrl-d adds the page step. Settle
        // the row-2 preview first: a late preview response would overwrite the manual line
        // count mid-scroll.
        harness.wait_until_previewed("claude:idle-three");
        harness.app.preview = (0..100)
            .map(|line| format!("line {line}"))
            .collect::<Vec<_>>()
            .join("\n");
        harness.script(vec![key(KeyCode::Char('J'))]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.preview_scroll, 3);
        harness.script(vec![ctrl_key(KeyCode::Char('d'))]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.preview_scroll, 7);
    }

    #[test]
    fn settled_idle_turn_uses_one_configured_poll_instead_of_ten_ms_wakeups() {
        let mut config = Config::default();
        config.ui.idle_poll_interval_ms = 25;
        let mut harness = TuiHarness::with_config(config, idle_executor());
        harness.step();
        assert_eq!(harness.events.poll_timeouts.len(), 1);
        assert!(
            harness.events.poll_timeouts[0] >= Duration::from_millis(23),
            "idle poll should use the configured interval: {:?}",
            harness.events.poll_timeouts
        );
    }

    #[test]
    fn mailbox_work_keeps_short_polling_even_when_visible_flags_are_clear() {
        let (release, gate) = mpsc::channel::<()>();
        let (entered_tx, entered_rx) = mpsc::channel::<()>();
        let executor = Box::new(
            move |request: &WorkerRequest, cancellation: &Arc<QueryCancellation>| {
                let _ = entered_tx.send(());
                park_until_released_or_cancelled(&gate, cancellation);
                Ok(WorkerResponse::preview(request, None))
            },
        );
        let mut config = Config::default();
        config.ui.idle_poll_interval_ms = 25;
        let mut harness = TuiHarness::with_config(config, executor);
        harness
            .app
            .worker
            .send(WorkerRequest {
                kind: RequestKind::PreviewOnly,
                generation: harness.app.current_preview_generation.clone(),
                query: String::new(),
                filters: harness.app.filters.clone(),
                selected_id: None,
            })
            .unwrap();
        entered_rx.recv_timeout(Duration::from_secs(5)).unwrap();
        harness.app.searching = false;
        harness.app.preview_loading = false;
        harness.step();
        assert!(
            harness.events.poll_timeouts[0] <= ACTIVE_WORKER_POLL_SLICE,
            "mailbox in-flight work must not use the settled idle interval"
        );
        release.send(()).unwrap();
    }

    #[test]
    fn result_limit_preserves_large_config_and_fills_the_browser_for_small_config() {
        assert_eq!(tui_result_limit(25), 100);
        assert_eq!(tui_result_limit(100), 100);
        assert_eq!(tui_result_limit(250), 250);
    }

    fn make_turn(role: &str, body: &str) -> String {
        format!("[2026-05-08 12:00:00 UTC] {role}\n{body}")
    }

    fn join_turns(turns: &[(&str, &str)]) -> String {
        turns
            .iter()
            .map(|(role, body)| make_turn(role, body))
            .collect::<Vec<_>>()
            .join("\n\n")
    }

    #[test]
    fn parses_turns_in_canonical_format() {
        let raw = join_turns(&[
            ("user", "hello"),
            ("assistant", "hi there\nmulti-line"),
            ("user", "bye"),
        ]);
        let turns = parse_turns(&raw);
        assert_eq!(turns.len(), 3);
        assert_eq!(turns[0].role, TurnRole::User);
        assert_eq!(turns[0].body, "hello");
        assert_eq!(turns[1].role, TurnRole::Assistant);
        assert_eq!(turns[1].body, "hi there\nmulti-line");
        assert_eq!(turns[2].body, "bye");
    }

    fn historical_summary_reference(transcript: &str) -> String {
        let turns = parse_turns(transcript);
        if turns.is_empty() {
            return "(no transcript content)".to_string();
        }
        let candidates = [
            (
                turns.iter().position(|turn| turn.role == TurnRole::User),
                "── First prompt ──",
                8,
            ),
            (
                turns
                    .iter()
                    .position(|turn| turn.role == TurnRole::Assistant),
                "── First reply ──",
                4,
            ),
            (
                turns.iter().rposition(|turn| turn.role == TurnRole::User),
                "── Final prompt ──",
                8,
            ),
            (
                turns
                    .iter()
                    .rposition(|turn| turn.role == TurnRole::Assistant),
                "── Final reply ──",
                14,
            ),
        ];
        let mut shown = Vec::new();
        let mut sections = Vec::new();
        for (ordinal, label, max_lines) in candidates {
            let Some(ordinal) = ordinal else { continue };
            if shown.contains(&ordinal) {
                continue;
            }
            shown.push(ordinal);
            sections.push((ordinal, label, max_lines, turns[ordinal].body));
        }
        sections.sort_by_key(|(ordinal, _, _, _)| *ordinal);
        let hidden = turns.len().saturating_sub(sections.len());
        let mut parts = Vec::new();
        let mut previous = None;
        for (ordinal, label, max_lines, body) in sections {
            if previous.is_some_and(|prior| ordinal > prior + 1) {
                let gap = ordinal - previous.unwrap() - 1;
                parts.push(format!(
                    "⋯ {gap} more turn{} hidden ⋯",
                    if gap == 1 { "" } else { "s" }
                ));
            }
            parts.push(label.to_string());
            parts.push(truncate_body(body, max_lines));
            previous = Some(ordinal);
        }
        if hidden > 0 && shown.len() < 2 {
            parts.push(format!(
                "⋯ {hidden} more turn{} hidden ⋯",
                if hidden == 1 { "" } else { "s" }
            ));
        }
        parts.push(format!(
            "({} turn{} total)",
            turns.len(),
            if turns.len() == 1 { "" } else { "s" }
        ));
        parts.join("\n\n")
    }

    #[test]
    fn borrowed_transcript_scan_matches_main_preview_semantics() {
        let long = (0..40)
            .map(|line| format!("line {line}"))
            .collect::<Vec<_>>()
            .join("\n");
        let cases = [
            String::new(),
            join_turns(&[("user", &long)]),
            join_turns(&[("assistant", &long)]),
            join_turns(&[("user", &long), ("assistant", &long)]),
            join_turns(&[
                ("user", "first"),
                ("assistant", "reply"),
                ("user", "middle"),
                ("assistant", "last"),
            ]),
        ];
        for transcript in cases {
            assert_eq!(
                build_transcript_summary(&transcript, HISTORICAL_PREVIEW_BODY_LINES),
                historical_summary_reference(&transcript)
            );
        }
    }

    #[test]
    fn summary_shows_bookends_with_elision() {
        let raw = join_turns(&[
            ("user", "first prompt"),
            ("assistant", "first reply"),
            ("user", "middle 1"),
            ("assistant", "middle 2"),
            ("user", "middle 3"),
            ("assistant", "middle 4"),
            ("user", "final prompt"),
            ("assistant", "final reply"),
        ]);
        let summary = build_transcript_summary(&raw, 34);
        assert!(summary.contains("First prompt"));
        assert!(summary.contains("first prompt"));
        assert!(summary.contains("First reply"));
        assert!(summary.contains("first reply"));
        assert!(summary.contains("Final prompt"));
        assert!(summary.contains("final prompt"));
        assert!(summary.contains("Final reply"));
        assert!(summary.contains("final reply"));
        assert!(summary.contains("4 more turns hidden"));
        assert!(summary.contains("8 turns total"));
    }

    #[test]
    fn summary_handles_short_session() {
        let raw = join_turns(&[("user", "hey"), ("assistant", "yo")]);
        let summary = build_transcript_summary(&raw, 34);
        // first==last for both roles, so we should see exactly 2 sections, no elision.
        assert!(summary.contains("First prompt"));
        assert!(summary.contains("First reply"));
        assert!(!summary.contains("more turn"));
        assert!(summary.contains("2 turns total"));
    }

    #[test]
    fn summary_truncates_long_body() {
        let big_body: String = (0..30)
            .map(|i| format!("line {i}"))
            .collect::<Vec<_>>()
            .join("\n");
        let raw = join_turns(&[("user", &big_body), ("assistant", "ok")]);
        let summary = build_transcript_summary(&raw, 34);
        assert!(summary.contains("[…]"));
    }

    #[test]
    fn summary_for_empty_transcript() {
        assert_eq!(build_transcript_summary("", 30), "(no transcript content)");
    }

    #[test]
    fn transcript_summary_observes_typed_cancellation() {
        let cancellation = QueryCancellation::new();
        cancellation.cancel();
        let error = build_transcript_summary_cancellable(
            "[2026-01-01T00:00:00Z] user\nbody\n",
            30,
            TerminalStyle::default(),
            &cancellation,
        )
        .unwrap_err();
        assert!(error.is::<QueryCancelled>());
        assert!(is_expected_interruption(&error));
    }

    #[test]
    fn error_elision_bounds_terminal_columns_and_flattens_newlines() {
        let rendered = elide_middle("漢字🙂 prefix\npress q and rerun", 16, "…");
        assert!(UnicodeWidthStr::width(rendered.as_str()) <= 16);
        assert!(!rendered.contains(['\r', '\n']));
        assert!(rendered.ends_with("rerun"));
    }

    #[test]
    fn marked_spans_with_style_splits_on_double_bracket_markers() {
        let base = Style::default();
        let hi = Style::default().fg(Color::Yellow);

        // Text before/inside/after a marker yields base, highlight, base spans.
        let spans = marked_spans_with_style("abc[[def]]ghi", base, hi);
        let got: Vec<(&str, Style)> = spans
            .iter()
            .map(|s| (s.content.as_ref(), s.style))
            .collect();
        assert_eq!(got, vec![("abc", base), ("def", hi), ("ghi", base)]);

        // A whole-string match is a single highlighted span.
        let spans = marked_spans_with_style("[[all]]", base, hi);
        assert_eq!(spans.len(), 1);
        assert_eq!(spans[0].content.as_ref(), "all");
        assert_eq!(spans[0].style, hi);

        // No marker is one base span; empty input is one empty base span.
        assert_eq!(marked_spans_with_style("plain", base, hi)[0].style, base);
        let spans = marked_spans_with_style("", base, hi);
        assert_eq!(spans.len(), 1);
        assert_eq!(spans[0].content.as_ref(), "");

        // An unclosed marker keeps the trailing "[[" as base-styled literal text.
        let spans = marked_spans_with_style("ab[[cd", base, hi);
        let got: Vec<(&str, Style)> = spans
            .iter()
            .map(|s| (s.content.as_ref(), s.style))
            .collect();
        assert_eq!(got, vec![("ab", base), ("[[cd", base)]);
    }

    #[test]
    fn render_preview_line_styles_known_prefixes_and_highlights_queries() {
        // A Session: line keeps the label span then the id, text preserved.
        let line = render_preview_line("Session: claude:s1", "", TerminalStyle::default());
        let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
        assert_eq!(text, "Session: claude:s1");
        assert!(line.spans.len() >= 2);

        // A section header renders as a single styled span.
        let line = render_preview_line("── user prompt ──", "", TerminalStyle::default());
        assert_eq!(line.spans.len(), 1);

        // A plain line with a query splits the matched term into its own span
        // while preserving the full text.
        let line = render_preview_line("find the needle here", "needle", TerminalStyle::default());
        let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
        assert_eq!(text, "find the needle here");
        assert!(line.spans.len() >= 2);
    }
    fn rows(ids: &[&str]) -> Vec<SessionRecord> {
        ids.iter().map(|id| session(id).session).collect()
    }

    /// Wait for one executor completion, failing with the reason that carries the RED signal:
    /// the executor can only run if the UI actually sent a request (§5.3).
    /// Wait for the executor to finish a request with this exact query, discarding stale
    /// signals (the startup empty query always fires first). FIFO order makes consecutive
    /// same-query waits consume distinct completions.
    fn wait_for_executed(executed: &mpsc::Receiver<String>, expected: &str) -> String {
        let deadline = std::time::Instant::now() + Duration::from_secs(5);
        while std::time::Instant::now() < deadline {
            match executed.recv_timeout(Duration::from_millis(100)) {
                Ok(query) if query == expected => return query,
                Ok(_) => continue,
                Err(mpsc::RecvTimeoutError::Timeout) => continue,
                Err(mpsc::RecvTimeoutError::Disconnected) => break,
            }
        }
        panic!("executor never finished query {expected:?} (it can only run if the UI sent it)");
    }

    /// Wait for one observed request shape (kind, query, selected_id).
    fn wait_for_request(
        requests: &mpsc::Receiver<(RequestKind, String, Option<String>)>,
    ) -> (RequestKind, String, Option<String>) {
        requests
            .recv_timeout(Duration::from_secs(5))
            .expect("a request reached the executor")
    }

    /// Step until the executor finishes another request with this exact query — for
    /// follow-ups that only issue from apply_response, where a bare wait would deadlock
    /// (no steps run to apply the prior response).
    fn step_until_executed(
        harness: &mut TuiHarness,
        executed: &mpsc::Receiver<String>,
        expected: &str,
    ) -> String {
        for _ in 0..MAX_TEST_STEPS {
            harness.step();
            if let Ok(query) = executed.try_recv() {
                if query == expected {
                    return query;
                }
            }
        }
        panic!("executor never finished another {expected:?} within {MAX_TEST_STEPS} steps");
    }

    /// Step until `condition` holds, bounded by MAX_TEST_STEPS. Needed wherever a test
    /// waits on an executor completion signal: the signal fires inside the executor, BEFORE
    /// the worker sends the response, so a single drain step after the wait can miss it.
    fn step_until<F: Fn(&TuiHarness) -> bool>(harness: &mut TuiHarness, condition: F) {
        for _ in 0..MAX_TEST_STEPS {
            if condition(harness) {
                return;
            }
            harness.step();
        }
        panic!("condition never held within {MAX_TEST_STEPS} steps");
    }

    /// Step until the NEXT request reaches the executor and return it. A bare step may run
    /// before the previous response arrives, and a follow-up request only issues from
    /// apply_response — stepping until it appears removes that race.
    fn step_until_next_request(
        harness: &mut TuiHarness,
        requests: &mpsc::Receiver<(RequestKind, String, Option<String>)>,
    ) -> (RequestKind, String, Option<String>) {
        for _ in 0..MAX_TEST_STEPS {
            harness.step();
            if let Ok(request) = requests.try_recv() {
                return request;
            }
        }
        panic!("no follow-up request arrived within {MAX_TEST_STEPS} steps");
    }

    /// Release parked gates enough times for every queued request to finish, so a harness
    /// holding a parked executor can drop without hanging on the worker join.
    fn finish_gated(release: &mpsc::Sender<()>) {
        for _ in 0..8 {
            let _ = release.send(());
        }
    }

    #[test]
    fn echo_renders_while_the_search_executor_is_still_blocked() {
        let (release, gate) = mpsc::channel::<()>();
        let (executed_tx, executed_rx) = mpsc::channel::<String>();
        let executor = Box::new(
            move |request: &WorkerRequest, cancellation: &Arc<QueryCancellation>| {
                if request.kind == RequestKind::Search && !request.query.trim().is_empty() {
                    park_until_released_or_cancelled(&gate, cancellation);
                }
                let response = match request.kind {
                    // The ungated startup request answers with the seeded rows; answering
                    // "after" here would race the !contains assertion via the stale guard
                    // instead of excluding it (§5.3).
                    RequestKind::Search if request.query.trim().is_empty() => {
                        WorkerResponse::results(request, rows(&["claude:before"]))
                    }
                    RequestKind::Search => {
                        WorkerResponse::results(request, rows(&["claude:after"]))
                    }
                    RequestKind::PreviewOnly => {
                        WorkerResponse::preview(request, Some(String::new()))
                    }
                };
                let _ = executed_tx.send(request.query.clone());
                Ok(response)
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:before"]);
        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('a'))]);
        harness.step_until_script_drained();

        // G1: the character is rendered and the previous result set is intact while the
        // search for "a" is provably still parked inside the executor.
        assert!(
            harness.search_box().contains('a'),
            "typed character must render before the search completes"
        );
        assert!(
            harness.session_rows().contains("before"),
            "previous results must survive while the search is in flight"
        );
        assert!(!harness.session_rows().contains("after"));

        release.send(()).unwrap();
        wait_for_executed(&executed_rx, "a");
        step_until(&mut harness, |harness| {
            harness.session_rows().contains("after")
        });
        assert!(harness.session_rows().contains("after"));
        finish_gated(&release);
    }

    #[test]
    fn backspace_echoes_while_the_executor_is_blocked() {
        let (release, gate) = mpsc::channel::<()>();
        let (executed_tx, executed_rx) = mpsc::channel::<String>();
        let (parked_tx, parked_rx) = mpsc::channel::<String>();
        let executor = Box::new(
            move |request: &WorkerRequest, cancellation: &Arc<QueryCancellation>| {
                if request.kind == RequestKind::Search && !request.query.trim().is_empty() {
                    let _ = parked_tx.send(request.query.clone());
                    park_until_released_or_cancelled(&gate, cancellation);
                }
                let _ = executed_tx.send(request.query.clone());
                Ok(match request.kind {
                    RequestKind::Search => WorkerResponse::results(request, Vec::new()),
                    RequestKind::PreviewOnly => WorkerResponse::preview(request, None),
                })
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:before"]);
        // '/','a' first, and wait until "a" is provably parked: with all three keys in one
        // script the worker's drain-to-latest coalesces the queued pair and "a" never
        // executes at all — correct worker behavior, wrong setup for this contract.
        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('a'))]);
        harness.step_until_script_drained();
        loop {
            let query = parked_rx
                .recv_timeout(Duration::from_secs(5))
                .expect("the 'a' search parked");
            if query == "a" {
                break;
            }
        }
        harness.script(vec![key(KeyCode::Backspace)]);
        harness.step_until_script_drained();
        // Backspace rendered immediately (query lost 'a'); a whitespace-only query counts as
        // empty, matching refresh's trim() contract. The Backspace's supersede cancels the
        // parked "a" — the cancel-aware fake completes it, as a real interrupted query would.
        assert!(!harness.search_box().contains("a█"));
        assert_eq!(harness.app.query, "");
        wait_for_executed(&executed_rx, "a");
        finish_gated(&release);
    }

    #[test]
    fn worker_serves_only_the_latest_of_a_drained_burst() {
        let (release, gate) = mpsc::channel::<()>();
        let (parked_tx, parked_rx) = mpsc::channel::<()>();
        let (executed_tx, executed_rx) = mpsc::channel::<String>();
        let executor = Box::new(
            move |request: &WorkerRequest, _cancellation: &Arc<QueryCancellation>| {
                if request.kind == RequestKind::Search && request.query.is_empty() {
                    // Park the STARTUP request so the burst provably queues behind it: all
                    // five searches sit in the channel before the worker takes any. Without
                    // this the worker races the UI's sends and the coalescing is untestable.
                    let _ = parked_tx.send(());
                    // Ignore cancellation until explicit release so every pasted prefix is
                    // submitted while the worker cannot consume the mailbox.
                    let _ = gate.recv();
                }
                let _ = executed_tx.send(request.query.clone());
                Ok(WorkerResponse::results(request, Vec::new()))
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:before"]);
        parked_rx
            .recv_timeout(Duration::from_secs(5))
            .expect("startup request parked");
        harness.script(vec![
            key(KeyCode::Char('/')),
            key(KeyCode::Char('a')),
            key(KeyCode::Char('b')),
            key(KeyCode::Char('c')),
            key(KeyCode::Char('d')),
            key(KeyCode::Char('e')),
        ]);
        harness.step_until_script_drained();
        assert_eq!(
            harness.app.worker.mailbox.pending_counts(),
            (1, 0),
            "a pasted burst retains one latest Search, never every cumulative prefix"
        );
        release.send(()).unwrap();
        // Collect until quiet: the worker drains the queued burst to its latest.
        let mut executed: Vec<String> = Vec::new();
        while let Ok(query) = executed_rx.recv_timeout(Duration::from_millis(200)) {
            executed.push(query);
        }
        // The queries are cumulative keystrokes: the burst's latest request carries the full
        // "abcde" — one execution for the whole queued burst is the drain-to-latest win.
        assert_eq!(
            executed,
            vec![String::new(), "abcde".to_string()],
            "a queued burst must serve only the latest request (§2.3 drain-to-latest)"
        );
    }

    #[test]
    fn navigation_preview_survives_queued_search_failure() {
        let (release, gate) = mpsc::channel::<()>();
        let (startup_parked_tx, startup_parked_rx) = mpsc::channel::<()>();
        let executor = Box::new(
            move |request: &WorkerRequest, _cancellation: &Arc<QueryCancellation>| match request
                .kind
            {
                RequestKind::Search if request.query.is_empty() => {
                    let _ = startup_parked_tx.send(());
                    // Hold the worker despite supersede so both pending slots are observable.
                    let _ = gate.recv();
                    Ok(WorkerResponse::results(
                        request,
                        rows(&["claude:one", "claude:two"]),
                    ))
                }
                RequestKind::Search => Err(anyhow::anyhow!("search failed")),
                RequestKind::PreviewOnly => Ok(WorkerResponse::preview(
                    request,
                    request
                        .selected_id
                        .as_deref()
                        .map(|id| format!("preview of {id}")),
                )),
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:one", "claude:two"]);
        startup_parked_rx
            .recv_timeout(Duration::from_secs(5))
            .expect("startup search parked");
        harness.script(vec![
            key(KeyCode::Char('/')),
            key(KeyCode::Char('a')),
            key(KeyCode::Esc),
            key(KeyCode::Char('j')),
        ]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.selected, 1);
        assert_eq!(harness.app.worker.mailbox.pending_counts(), (1, 1));
        release.send(()).unwrap();
        step_until(&mut harness, |harness| {
            harness.app.preview.contains("claude:two")
        });
        assert!(harness.app.preview.contains("claude:two"));
    }

    #[test]
    fn superseding_a_query_cancels_the_one_in_flight() {
        let (release, gate) = mpsc::channel::<()>();
        let (observed_tx, observed_rx) = mpsc::channel::<(String, Arc<QueryCancellation>)>();
        let executor = Box::new(
            move |request: &WorkerRequest, cancellation: &Arc<QueryCancellation>| {
                let _ = observed_tx.send((request.query.clone(), Arc::clone(cancellation)));
                if request.kind == RequestKind::Search && !request.query.trim().is_empty() {
                    park_until_released_or_cancelled(&gate, cancellation);
                }
                Ok(WorkerResponse::results(request, Vec::new()))
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:before"]);
        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('a'))]);
        harness.step_until_script_drained();
        // Wait until the "a" search is provably parked (its cancellation published), so the
        // supersede lands on it deterministically. The fake cloned the request's Arc out;
        // the test owns the only other one, which is what makes is_cancelled() assertable
        // at all (C29).
        let first = loop {
            let (query, cancellation) = observed_rx
                .recv_timeout(Duration::from_secs(5))
                .expect("a search reached the executor");
            if query == "a" {
                break cancellation;
            }
        };
        harness.script(vec![key(KeyCode::Char('b'))]);
        harness.step_until_script_drained();
        assert!(
            first.is_cancelled(),
            "typing a newer query must cancel the superseded in-flight search"
        );
        finish_gated(&release);
    }

    #[test]
    fn response_for_a_superseded_query_is_dropped() {
        let (release, gate) = mpsc::channel::<()>();
        let (executed_tx, executed_rx) = mpsc::channel::<String>();
        let (parked_tx, parked_rx) = mpsc::channel::<String>();
        let executor = Box::new(
            move |request: &WorkerRequest, cancellation: &Arc<QueryCancellation>| {
                if request.kind == RequestKind::Search && !request.query.trim().is_empty() {
                    let _ = parked_tx.send(request.query.clone());
                    park_until_released_or_cancelled(&gate, cancellation);
                }
                // Suffix the id so superseded and fresh results are not substrings of each
                // other ("claude:a-result" vs "claude:ab-result").
                let id = format!("claude:{}-result", request.query);
                let _ = executed_tx.send(request.query.clone());
                Ok(WorkerResponse::results(request, rows(&[&id])))
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:before"]);
        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('a'))]);
        harness.step_until_script_drained();
        loop {
            let query = parked_rx
                .recv_timeout(Duration::from_secs(5))
                .expect("a search parked");
            if query == "a" {
                break;
            }
        }
        harness.script(vec![key(KeyCode::Char('b'))]);
        harness.step_until_script_drained();
        // Typing 'b' makes the cumulative query "ab": that request cancels the in-flight
        // "a" — the cancel-aware fake completes it, exactly as a real interrupted query
        // would — then "ab" parks, and one release serves it. The stale "a" response must
        // be dropped by the originating-query guard, never rendered; the fresh "ab"
        // response applies.
        release.send(()).unwrap();
        wait_for_executed(&executed_rx, "a");
        wait_for_executed(&executed_rx, "ab");
        step_until(&mut harness, |harness| {
            harness.session_rows().contains("claude:ab-result")
        });
        assert!(
            !harness.session_rows().contains("claude:a-result"),
            "a response for a superseded query must not mutate state"
        );
        assert!(harness.session_rows().contains("claude:ab-result"));
        finish_gated(&release);
    }

    #[test]
    fn same_query_response_from_superseded_filters_is_dropped() {
        let (release, gate) = mpsc::channel::<()>();
        let executor = Box::new(
            move |request: &WorkerRequest, cancellation: &Arc<QueryCancellation>| {
                if request.kind == RequestKind::Search && request.filters.provider.is_some() {
                    park_until_released_or_cancelled(&gate, cancellation);
                }
                Ok(match request.kind {
                    RequestKind::Search => {
                        WorkerResponse::results(request, rows(&["claude:current-filter-result"]))
                    }
                    RequestKind::PreviewOnly => WorkerResponse::preview(request, None),
                })
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:before"]);
        let old_request = WorkerRequest {
            kind: RequestKind::Search,
            generation: harness.app.current_search_generation.clone(),
            query: harness.app.query.clone(),
            filters: harness.app.filters.clone(),
            selected_id: harness
                .app
                .selected_session()
                .map(|session| session.id.clone()),
        };

        // Provider changes the request semantics without changing the query. Its successor is
        // parked, so an already-queued old response cannot be rescued by a later correct one.
        harness.app.cycle_provider();
        assert_eq!(harness.app.query, old_request.query);
        assert!(!harness.app.apply_outcome(WorkerOutcome {
            kind: RequestKind::Search,
            generation: old_request.generation.clone(),
            result: Ok(WorkerResponse::results(
                &old_request,
                rows(&["claude:stale-unfiltered"]),
            )),
        }));
        assert!(
            !harness
                .app
                .results
                .iter()
                .any(|row| row.id == "claude:stale-unfiltered"),
            "response identity must include the filter generation, not only equal query text"
        );
        finish_gated(&release);
    }

    #[test]
    fn query_edit_invalidates_an_in_flight_response_before_debounce() {
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:keep"]);
        harness.app.config.ui.search_debounce_ms = 50;
        let stale = harness.app.current_search_generation.clone();

        harness.app.insert_character('x');

        assert!(harness.app.searching);
        assert!(!stale.same_as(&harness.app.current_search_generation));
        assert!(!harness.app.apply_outcome(WorkerOutcome {
            kind: RequestKind::Search,
            generation: stale,
            result: Ok(WorkerResponse::results(
                &WorkerRequest {
                    kind: RequestKind::Search,
                    generation: RequestGeneration::new(),
                    query: String::new(),
                    filters: SearchFilters::default(),
                    selected_id: None,
                },
                rows(&["claude:stale"]),
            )),
        }));
        assert!(
            harness.app.searching,
            "a stale response during debounce must not mark the edited query ready"
        );
        assert_eq!(harness.app.results[0].id, "claude:keep");
    }

    #[test]
    fn error_from_superseded_search_is_dropped() {
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:keep"]);
        let stale = harness.app.current_search_generation.clone();
        harness.app.request_search();
        assert!(!harness.app.apply_outcome(WorkerOutcome {
            kind: RequestKind::Search,
            generation: stale,
            result: Err(anyhow::anyhow!("stale failure")),
        }));
        assert!(
            harness.app.error.is_none(),
            "a stale error must not replace the current operation state"
        );
    }

    #[test]
    fn list_title_exposes_current_search_completion() {
        let (release, gate) = mpsc::channel::<()>();
        let executor = Box::new(
            move |request: &WorkerRequest, cancellation: &Arc<QueryCancellation>| {
                if request.kind == RequestKind::Search {
                    park_until_released_or_cancelled(&gate, cancellation);
                }
                Ok(WorkerResponse::results(request, rows(&["claude:done"])))
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:before"]);
        harness.step();
        assert!(screen_text(&harness.terminal).contains("searching"));
        release.send(()).unwrap();
        step_until(&mut harness, |harness| !harness.app.searching);
        harness.step();
        assert!(!screen_text(&harness.terminal).contains("searching"));
        finish_gated(&release);
    }

    #[test]
    fn returning_to_rendered_preview_invalidates_outstanding_preview_error() {
        let mut harness =
            TuiHarness::with_executor(idle_executor()).seeded(&["claude:a", "claude:b"]);
        harness.app.previewed_id = Some("claude:a".to_string());
        harness.app.selected = 1;
        harness.app.request_preview();
        let stale_b = harness.app.current_preview_generation.clone();
        harness.app.selected = 0;
        harness.app.request_preview(); // fast path: A is already rendered, but B must go stale.
        assert!(!harness.app.apply_outcome(WorkerOutcome {
            kind: RequestKind::PreviewOnly,
            generation: stale_b,
            result: Err(anyhow::anyhow!("preview B failed")),
        }));
        assert!(harness.app.error.is_none());
    }

    #[test]
    fn current_preview_error_replaces_stale_content_and_success_recovers() {
        let mut harness =
            TuiHarness::with_executor(idle_executor()).seeded(&["claude:a", "claude:b"]);
        harness.app.preview = "preview of claude:a".to_string();
        harness.app.previewed_id = Some("claude:a".to_string());
        harness.app.selected = 1;
        harness.app.request_preview();
        let generation = harness.app.current_preview_generation.clone();

        assert!(harness.app.apply_outcome(WorkerOutcome {
            kind: RequestKind::PreviewOnly,
            generation: generation.clone(),
            result: Err(anyhow::anyhow!("selected row disappeared")),
        }));
        assert!(!harness.app.preview.contains("claude:a"));
        assert!(harness.app.preview.contains("claude:b"));
        assert!(harness.app.preview.contains("unavailable"));

        let request = WorkerRequest {
            kind: RequestKind::PreviewOnly,
            generation: generation.clone(),
            query: harness.app.query.clone(),
            filters: harness.app.filters.clone(),
            selected_id: Some("claude:b".to_string()),
        };
        assert!(harness.app.apply_outcome(WorkerOutcome {
            kind: RequestKind::PreviewOnly,
            generation,
            result: Ok(WorkerResponse::preview(
                &request,
                Some("preview of claude:b".to_string()),
            )),
        }));
        assert_eq!(harness.app.preview, "preview of claude:b");
        assert!(harness.app.error.is_none());
    }

    #[test]
    fn preview_only_response_does_not_replace_the_result_list() {
        let (executed_tx, executed_rx) = mpsc::channel::<String>();
        let executor = Box::new(
            move |request: &WorkerRequest, _cancellation: &Arc<QueryCancellation>| {
                let _ = executed_tx.send(request.query.clone());
                Ok(match request.kind {
                    RequestKind::Search => {
                        WorkerResponse::results(request, rows(&["claude:keep", "claude:second"]))
                    }
                    RequestKind::PreviewOnly => {
                        WorkerResponse::preview(request, Some("preview body".to_string()))
                    }
                })
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:keep"]);
        harness.step_until_script_drained();
        wait_for_executed(&executed_rx, ""); // startup search (FIFO first)
        harness.wait_until_previewed("claude:keep");
        harness.script(vec![key(KeyCode::Char('j'))]);
        harness.step_until_script_drained();
        wait_for_executed(&executed_rx, ""); // the j preview, after the startup signal
        harness.step();
        assert_eq!(harness.app.selected, 1);
        assert!(
            harness.session_rows().contains("keep"),
            "a preview-only response must never replace the result list (C13)"
        );
    }

    #[test]
    fn startup_and_every_query_change_end_with_a_preview_for_the_selected_row() {
        let (requests_tx, requests_rx) = mpsc::channel::<(RequestKind, String, Option<String>)>();
        let executor = Box::new(
            move |request: &WorkerRequest, _cancellation: &Arc<QueryCancellation>| {
                let _ = requests_tx.send((
                    request.kind,
                    request.query.clone(),
                    request.selected_id.clone(),
                ));
                Ok(match request.kind {
                    RequestKind::Search if request.query.trim().is_empty() => {
                        WorkerResponse::results(request, rows(&["claude:one", "claude:two"]))
                    }
                    RequestKind::Search => WorkerResponse::results(request, rows(&["claude:two"])),
                    RequestKind::PreviewOnly => WorkerResponse::preview(
                        request,
                        request
                            .selected_id
                            .as_deref()
                            .map(|id| format!("preview of {id}")),
                    ),
                })
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:one", "claude:two"]);
        // Startup: empty-query Search, then a preview for the row the UI actually selected.
        let (kind, query, _) = wait_for_request(&requests_rx);
        assert_eq!((kind, query.as_str()), (RequestKind::Search, ""));
        // Apply the startup response: the preview request only issues from apply_response,
        // which runs during a step — the signal alone does not apply it.
        let (kind, _, selected) = step_until_next_request(&mut harness, &requests_rx);
        assert_eq!(
            (kind, selected),
            (RequestKind::PreviewOnly, Some("claude:one".into()))
        );
        step_until(&mut harness, |harness| {
            harness.app.preview.contains("claude:one")
        });
        assert!(harness.app.preview.contains("claude:one"));

        // Query change that drops the selected row: the new first row must get its preview.
        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('z'))]);
        harness.step_until_script_drained();
        let (kind, query, _) = wait_for_request(&requests_rx);
        assert_eq!((kind, query.as_str()), (RequestKind::Search, "z"));
        let (kind, _, selected) = step_until_next_request(&mut harness, &requests_rx);
        assert_eq!(
            (kind, selected),
            (RequestKind::PreviewOnly, Some("claude:two".into())),
            "the preview must follow whatever row the UI actually selected (C28)"
        );
        step_until(&mut harness, |harness| {
            harness.app.preview.contains("claude:two")
        });
        assert!(harness.app.preview.contains("claude:two"));
    }

    #[test]
    fn a_preview_overtaken_by_newer_navigation_is_discarded() {
        let (release, gate) = mpsc::channel::<()>();
        let (executed_tx, executed_rx) = mpsc::channel::<String>();
        let (parking_tx, parking_rx) = mpsc::channel::<(Option<String>, Arc<QueryCancellation>)>();
        let executor = Box::new(
            move |request: &WorkerRequest, cancellation: &Arc<QueryCancellation>| {
                if request.kind == RequestKind::PreviewOnly {
                    // Signal WHICH preview is parking so the test can wait for the startup
                    // preview to provably start — otherwise j's request can queue alongside
                    // it and the worker's drain-to-latest legitimately drops the first.
                    let _ =
                        parking_tx.send((request.selected_id.clone(), Arc::clone(cancellation)));
                    park_until_released_or_cancelled(&gate, cancellation);
                }
                let _ = executed_tx.send(request.query.clone());
                Ok(match request.kind {
                    RequestKind::Search => {
                        WorkerResponse::results(request, rows(&["claude:one", "claude:two"]))
                    }
                    RequestKind::PreviewOnly => WorkerResponse::preview(
                        request,
                        request
                            .selected_id
                            .as_deref()
                            .map(|id| format!("preview of {id}")),
                    ),
                })
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:one", "claude:two"]);
        harness.step_until_script_drained();
        // Wait until the startup preview (row 0) is provably parked, stepping to drive
        // apply_response — the preview request only issues once the search response lands,
        // and a bare wait would deadlock if that single step ran too early. THEN navigate:
        // the parked request must not still be queued, or drain-to-latest may drop it
        // instead of the overtaken-render discard this test pins.
        let mut turns = 0;
        let first_preview = loop {
            match parking_rx.recv_timeout(Duration::from_millis(50)) {
                Ok((selected, cancellation)) if selected.as_deref() == Some("claude:one") => {
                    break cancellation;
                }
                Ok(_) => continue,
                Err(mpsc::RecvTimeoutError::Timeout) => {
                    turns += 1;
                    assert!(turns < MAX_TEST_STEPS, "startup preview never parked");
                    harness.step();
                }
                Err(mpsc::RecvTimeoutError::Disconnected) => {
                    panic!("worker died before the startup preview parked");
                }
            }
        };
        // j selects row 1 and queues its preview.
        harness.script(vec![key(KeyCode::Char('j'))]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.selected, 1);
        assert!(
            first_preview.is_cancelled(),
            "new navigation must cancel the obsolete O(M) preview scan"
        );
        release.send(()).unwrap();
        wait_for_executed(&executed_rx, ""); // startup search (FIFO first)
        wait_for_executed(&executed_rx, ""); // the overtaken row-0 preview
        harness.step();
        assert!(
            !harness.app.preview.contains("claude:one"),
            "a preview overtaken by newer navigation must be discarded, not rendered beside the wrong row"
        );
        // The queued row-1 preview then executes (drained burst) and applies.
        release.send(()).unwrap();
        wait_for_executed(&executed_rx, "");
        step_until(&mut harness, |harness| {
            harness.app.preview.contains("claude:two")
        });
        assert!(harness.app.preview.contains("claude:two"));
        finish_gated(&release);
    }

    #[test]
    fn selection_and_preview_scroll_survive_a_response_that_still_contains_it() {
        let (executed_tx, executed_rx) = mpsc::channel::<String>();
        let executor = Box::new(
            move |request: &WorkerRequest, _cancellation: &Arc<QueryCancellation>| {
                let _ = executed_tx.send(request.query.clone());
                Ok(match request.kind {
                    RequestKind::Search if request.query.trim().is_empty() => {
                        WorkerResponse::results(
                            request,
                            rows(&["claude:one", "claude:two", "claude:three"]),
                        )
                    }
                    // Same set, reordered: the selected id survives, at a new position.
                    RequestKind::Search => WorkerResponse::results(
                        request,
                        rows(&["claude:three", "claude:two", "claude:one"]),
                    ),
                    RequestKind::PreviewOnly => WorkerResponse::preview(
                        request,
                        Some(
                            (0..50)
                                .map(|line| format!("line {line}"))
                                .collect::<Vec<_>>()
                                .join("\n"),
                        ),
                    ),
                })
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&[
            "claude:one",
            "claude:two",
            "claude:three",
        ]);
        harness.step_until_script_drained();
        wait_for_executed(&executed_rx, ""); // startup search executed
                                             // The startup preview only issues once the search response applies — stepping
                                             // drives apply_response, a bare wait would deadlock.
        step_until_executed(&mut harness, &executed_rx, "");
        harness.script(vec![key(KeyCode::Char('j'))]);
        harness.step_until_script_drained();
        wait_for_executed(&executed_rx, ""); // row-1 preview
        harness.step();
        harness.script(vec![
            key(KeyCode::Char('J')),
            key(KeyCode::Char('J')),
            key(KeyCode::Char('J')),
        ]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.preview_scroll, 15);
        assert_eq!(harness.app.selected, 1);

        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('z'))]);
        harness.step_until_script_drained();
        wait_for_executed(&executed_rx, "z"); // the reordered search response
                                              // Wait for the response to APPLY (its first row becomes claude:three), then assert
                                              // preservation — asserting on the pre-response state would prove nothing.
        step_until(&mut harness, |harness| {
            harness.app.results.first().map(|s| s.id.as_str()) == Some("claude:three")
        });
        assert_eq!(
            harness.app.selected, 1,
            "a response still containing the selected session must preserve the selection (D4)"
        );
        assert_eq!(harness.app.preview_scroll, 15);
    }

    #[test]
    fn navigation_moves_selection_while_a_preview_is_outstanding() {
        let (release, gate) = mpsc::channel::<()>();
        let (executed_tx, executed_rx) = mpsc::channel::<String>();
        let executor = Box::new(
            move |request: &WorkerRequest, cancellation: &Arc<QueryCancellation>| {
                if request.kind == RequestKind::PreviewOnly {
                    park_until_released_or_cancelled(&gate, cancellation);
                }
                let _ = executed_tx.send(request.query.clone());
                Ok(match request.kind {
                    RequestKind::Search => WorkerResponse::results(
                        request,
                        rows(&["claude:one", "claude:two", "claude:three"]),
                    ),
                    RequestKind::PreviewOnly => WorkerResponse::preview(
                        request,
                        request
                            .selected_id
                            .as_deref()
                            .map(|id| format!("preview of {id}")),
                    ),
                })
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&[
            "claude:one",
            "claude:two",
            "claude:three",
        ]);
        harness.step_until_script_drained();
        // The startup preview parks (outstanding). j and k must move the selection NOW,
        // without waiting for it (G2).
        harness.script(vec![
            key(KeyCode::Char('j')),
            key(KeyCode::Char('j')),
            key(KeyCode::Char('k')),
        ]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.selected, 1);
        finish_gated(&release);
        wait_for_executed(&executed_rx, "");
    }

    #[test]
    fn worker_disconnect_is_reported_once_without_another_key() {
        let executor = Box::new(
            move |_request: &WorkerRequest, _cancellation: &Arc<QueryCancellation>| {
                panic!("startup executor exploded")
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:keep"]);
        for _ in 0..MAX_TEST_STEPS {
            harness.step();
            if harness.app.error.is_some() {
                break;
            }
        }
        assert!(
            harness
                .app
                .error
                .as_deref()
                .is_some_and(|error| error.contains("worker stopped")),
            "response-channel disconnect must surface without requiring another key"
        );
        assert!(
            !harness.app.drain_responses(),
            "disconnect is reported once"
        );
    }

    #[test]
    fn a_dead_worker_reports_an_error_and_leaves_the_ui_usable() {
        let (entered_tx, entered_rx) = mpsc::channel::<()>();
        let executor = Box::new(
            move |_request: &WorkerRequest, _cancellation: &Arc<QueryCancellation>| {
                // Signal entry so the test knows the panic is in flight — the thread still
                // needs a moment to unwind and close the request channel.
                let _ = entered_tx.send(());
                panic!("executor exploded")
            },
        );
        let mut harness =
            TuiHarness::with_executor(executor).seeded(&["claude:keep", "claude:also"]);
        entered_rx
            .recv_timeout(Duration::from_secs(5))
            .expect("the executor entered (and is now panicking)");
        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('a'))]);
        harness.step_until_script_drained();
        // The worker is mid-unwind when the signal fires; keep typing until a send fails —
        // each keystroke retries, and the closed channel is what sets the error line.
        for _ in 0..MAX_TEST_STEPS {
            if harness.app.error.is_some() {
                break;
            }
            harness.script(vec![key(KeyCode::Char('x'))]);
            harness.step_until_script_drained();
        }
        assert!(
            harness.app.error.is_some(),
            "a dead worker must surface as the error line, not a hang or an exit (C14)"
        );
        harness.step();
        assert!(
            harness.error_line().contains("worker"),
            "the error line must render the failure"
        );
        // The list stays navigable and q still quits. Esc first: 'a' left search mode,
        // where j would type into the query instead of moving the selection.
        harness.script(vec![key(KeyCode::Esc)]);
        harness.step_until_script_drained();
        harness.script(vec![key(KeyCode::Char('j'))]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.selected, 1);
        harness.script(vec![key(KeyCode::Char('q'))]);
        assert!(matches!(
            harness.step_until_script_drained(),
            Some(AppAction::Quit)
        ));
    }
    /// Park like a real query would: until released, or until cancelled (a real SQLite query
    /// unblocks on interrupt). A gate-only fake would hang the worker join in Drop.
    fn park_until_released_or_cancelled(
        gate: &mpsc::Receiver<()>,
        cancellation: &QueryCancellation,
    ) {
        loop {
            if cancellation.is_cancelled() {
                return;
            }
            match gate.try_recv() {
                Ok(()) | Err(mpsc::TryRecvError::Disconnected) => return,
                Err(mpsc::TryRecvError::Empty) => std::thread::sleep(Duration::from_millis(1)),
            }
        }
    }

    #[test]
    fn worker_startup_failure_is_reported_not_swallowed() {
        let factory: ExecutorFactory = Box::new(|| Err(anyhow::anyhow!("cannot open database")));
        let error = spawn_search_worker(factory)
            .err()
            .expect("startup failure must surface through the handshake");
        assert!(
            format!("{error:#}").contains("cannot open database"),
            "unexpected error: {error:#}"
        );
    }

    #[test]
    fn worker_handle_carries_the_main_handles_access_scope() {
        // The factory reports the scope its connection observed; a UI-thread test cannot call
        // access_scope() on a Db living in another thread (P5).
        let factory: ExecutorFactory = Box::new(|| {
            Ok((
                idle_executor(),
                EffectiveAccessScope::AllowedRoots { roots: Vec::new() },
            ))
        });
        let (_worker, observed) = spawn_search_worker(factory).unwrap();
        assert!(
            matches!(observed, EffectiveAccessScope::AllowedRoots { .. }),
            "the observed access scope must travel back to the handle"
        );
    }

    #[test]
    fn worker_refuses_a_mismatched_schema_version() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("index.db");
        let db = Db::open(&db_path).unwrap();
        db.upsert_session(&session("claude:alpha"), 0, 0).unwrap();
        let runtime = db.execution_runtime();
        drop(db);
        // Bump the stamp through a raw connection: every in-crate writer writes the current
        // value only (the service.rs precedent for forging drift).
        let raw = rusqlite::Connection::open(&db_path).unwrap();
        raw.pragma_update(None, "user_version", SCHEMA_VERSION + 1)
            .unwrap();
        drop(raw);
        let mut config = Config::default();
        config.index.db_path = Some(db_path.to_string_lossy().into_owned());
        let factory = db_backed_executor(
            config,
            EffectiveAccessScope::All,
            runtime,
            TerminalStyle::default(),
        );
        let error = factory().err().expect("schema drift must be refused");
        assert!(
            format!("{error:#}").contains("upgrade aise"),
            "unexpected error: {error:#}"
        );
    }

    #[test]
    fn worker_accepts_every_shared_readable_schema_generation() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("index.db");
        let db = Db::open(&db_path).unwrap();
        db.upsert_session(&session("claude:alpha"), 0, 0).unwrap();
        let runtime = db.execution_runtime();
        drop(db);
        let raw = rusqlite::Connection::open(&db_path).unwrap();
        raw.pragma_update(None, "user_version", SCHEMA_VERSION - 1)
            .unwrap();
        drop(raw);
        let mut config = Config::default();
        config.index.db_path = Some(db_path.to_string_lossy().into_owned());
        let factory = db_backed_executor(
            config,
            EffectiveAccessScope::All,
            runtime,
            TerminalStyle::default(),
        );
        let (_executor, observed) = factory().expect("shared-readable schema must open");
        assert!(matches!(observed, EffectiveAccessScope::All));
    }

    #[test]
    fn an_interrupted_query_is_not_reported_as_an_error() {
        // libsqlite3-sys's Error is a plain pub-fields struct; no constructor suits this case.
        let interrupted = rusqlite::Error::SqliteFailure(
            rusqlite::ffi::Error {
                code: rusqlite::ffi::ErrorCode::OperationInterrupted,
                extended_code: rusqlite::ffi::SQLITE_INTERRUPT,
            },
            None,
        );
        assert!(is_expected_interruption(&anyhow::Error::new(interrupted)));
        let other = anyhow::Error::new(rusqlite::Error::QueryReturnedNoRows);
        assert!(!is_expected_interruption(&other));
    }

    #[test]
    fn navigating_during_a_search_does_not_cancel_it() {
        let (release, gate) = mpsc::channel::<()>();
        let (observed_tx, observed_rx) = mpsc::channel::<(String, Arc<QueryCancellation>)>();
        let executor = Box::new(
            move |request: &WorkerRequest, cancellation: &Arc<QueryCancellation>| {
                let _ = observed_tx.send((request.query.clone(), Arc::clone(cancellation)));
                if request.kind == RequestKind::Search && !request.query.trim().is_empty() {
                    park_until_released_or_cancelled(&gate, cancellation);
                }
                Ok(match request.kind {
                    RequestKind::Search => {
                        WorkerResponse::results(request, rows(&["claude:delivered"]))
                    }
                    RequestKind::PreviewOnly => WorkerResponse::preview(request, None),
                })
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:before"]);
        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('a'))]);
        harness.step_until_script_drained();
        let search = loop {
            let (query, cancellation) = observed_rx
                .recv_timeout(Duration::from_secs(5))
                .expect("the search reached the executor");
            if query == "a" {
                break cancellation;
            }
        };
        harness.script(vec![key(KeyCode::Esc), key(KeyCode::Char('j'))]);
        harness.step_until_script_drained();
        assert!(
            !search.is_cancelled(),
            "navigation must not cancel an in-flight search (C21/F2)"
        );
        release.send(()).unwrap();
        for _ in 0..MAX_TEST_STEPS {
            harness.step();
            if harness.session_rows().contains("delivered") {
                break;
            }
        }
        assert!(
            harness.session_rows().contains("delivered"),
            "the search must still deliver after navigation"
        );
    }

    #[test]
    fn quit_joins_the_worker_without_hanging() {
        let (release, gate) = mpsc::channel::<()>();
        let (observed_tx, observed_rx) = mpsc::channel::<(String, Arc<QueryCancellation>)>();
        let executor = Box::new(
            move |request: &WorkerRequest, cancellation: &Arc<QueryCancellation>| {
                let _ = observed_tx.send((request.query.clone(), Arc::clone(cancellation)));
                if request.kind == RequestKind::Search && !request.query.trim().is_empty() {
                    park_until_released_or_cancelled(&gate, cancellation);
                }
                Ok(WorkerResponse::results(request, Vec::new()))
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:before"]);
        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('a'))]);
        harness.step_until_script_drained();
        // Wait for the "a" request's own cancellation (the startup "" fires first).
        let search = loop {
            let (query, cancellation) = observed_rx
                .recv_timeout(Duration::from_secs(5))
                .expect("a search reached the executor");
            if query == "a" {
                break cancellation;
            }
        };
        // Drop cancels the in-flight query, closes the request channel, and joins — the
        // cancellation-aware park is what lets the join return.
        drop(harness);
        assert!(
            search.is_cancelled(),
            "Drop must cancel the in-flight query before joining"
        );
        let _ = release;
    }

    fn ordered_ids(results: &[SessionRecord]) -> Vec<String> {
        results.iter().map(|session| session.id.clone()).collect()
    }

    fn id_digest(ids: &[String]) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};
        let mut hasher = DefaultHasher::new();
        ids.hash(&mut hasher);
        hasher.finish()
    }

    /// Step until the worker's response has been applied (ids match) or steps run out.
    fn wait_for_results(harness: &mut TuiHarness, expected: &[String]) {
        for _ in 0..MAX_TEST_STEPS {
            if ordered_ids(&harness.app.results) == expected {
                return;
            }
            harness.step();
        }
    }

    #[test]
    fn catalog_session_search_refuses_pre_cancelled_work() {
        let dir = tempfile::tempdir().unwrap();
        let db = Db::open(&dir.path().join("index.db")).unwrap();
        db.upsert_session(&session("claude:alpha"), 0, 0).unwrap();
        let cancellation = QueryCancellation::new();
        cancellation.cancel();
        let error = CatalogService::new(&db)
            .search_sessions_cancellable(
                "alpha",
                &SearchFilters {
                    limit: 10,
                    ..Default::default()
                },
                None,
                &Config::default().search.scoring,
                &cancellation,
            )
            .unwrap_err();
        assert!(error.is::<QueryCancelled>());
    }

    #[test]
    fn oversized_real_transcript_scoring_observes_mid_scan_cancellation() {
        let dir = tempfile::tempdir().unwrap();
        let db = Db::open(&dir.path().join("index.db")).unwrap();
        let mut parsed = session("claude:oversized");
        parsed.transcript_text = "x".repeat(16 * 1024 * 1024);
        db.upsert_session(&parsed, 0, 0).unwrap();
        let cancellation = Arc::new(QueryCancellation::new());
        db.install_query_cancellation(&cancellation).unwrap();
        let cancel_from_thread = Arc::clone(&cancellation);
        let canceller = thread::spawn(move || {
            thread::sleep(Duration::from_millis(5));
            cancel_from_thread.cancel();
        });
        let started = std::time::Instant::now();
        let error = CatalogService::new(&db)
            .search_sessions_cancellable(
                "not-present-anywhere",
                &SearchFilters {
                    limit: 10,
                    ..Default::default()
                },
                None,
                &Config::default().search.scoring,
                &cancellation,
            )
            .unwrap_err();
        canceller.join().unwrap();
        assert!(
            is_expected_interruption(&error),
            "unexpected error: {error:#}"
        );
        assert!(
            started.elapsed() < Duration::from_secs(2),
            "oversized-record cancellation exceeded the bounded scan deadline"
        );
    }

    #[test]
    fn tui_and_catalog_service_return_identical_ordered_results() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("index.db");
        let db = Db::open(&db_path).unwrap();
        for index in 0..40 {
            let id = format!("claude:alpha-{index:02}");
            db.upsert_session(&session(&id), 0, 0).unwrap();
        }
        let mut config = Config::default();
        config.index.db_path = Some(db_path.to_string_lossy().into_owned());
        let factory = db_backed_executor(
            config.clone(),
            db.access_scope().clone(),
            db.execution_runtime(),
            TerminalStyle::default(),
        );
        let mut harness = TuiHarness::with_factory(config.clone(), factory);
        harness.start();
        let catalog = CatalogService::new(&db);

        // Startup empty query: the worker's list path equals CatalogService's list path.
        harness.step_until_script_drained();
        let expected = catalog
            .list_sessions(&harness.app.filters)
            .unwrap()
            .into_iter()
            .map(|session| session.id)
            .collect::<Vec<_>>();
        wait_for_results(&mut harness, &expected);
        assert_eq!(ordered_ids(&harness.app.results), expected);
        assert!(
            expected.len() > 22,
            "parity must cover results beyond the terminal viewport"
        );

        // Matching and no-match queries through the worker and the service.
        for (iteration, query) in ["alpha", "zzz"].into_iter().enumerate() {
            if iteration == 0 {
                harness.script(vec![key(KeyCode::Char('/'))]);
                harness.step_until_script_drained();
            } else {
                for _ in 0.."alpha".len() {
                    harness.script(vec![key(KeyCode::Backspace)]);
                    harness.step_until_script_drained();
                }
            }
            for ch in query.chars() {
                harness.script(vec![key(KeyCode::Char(ch))]);
                harness.step_until_script_drained();
            }
            let expected = catalog
                .search_sessions(
                    query,
                    &harness.app.filters,
                    current_repo(&config).as_deref(),
                    &config.search.scoring,
                )
                .unwrap()
                .into_iter()
                .map(|hit| hit.session.id)
                .collect::<Vec<_>>();
            wait_for_results(&mut harness, &expected);
            let got = ordered_ids(&harness.app.results);
            assert_eq!(
                got, expected,
                "TUI vs CatalogService mismatch for {query:?}"
            );
            assert_eq!(id_digest(&got), id_digest(&expected));
        }
    }
    // ---- step 5: filter bindings, layout config, parity, presentation ----

    fn recording_executor() -> (
        mpsc::Receiver<(RequestKind, SearchFilters, String)>,
        SearchExecutor,
    ) {
        let (tx, rx) = mpsc::channel::<(RequestKind, SearchFilters, String)>();
        let executor = Box::new(
            move |request: &WorkerRequest, _cancellation: &Arc<QueryCancellation>| {
                let _ = tx.send((request.kind, request.filters.clone(), request.query.clone()));
                Ok(match request.kind {
                    RequestKind::Search => {
                        WorkerResponse::results(request, rows(&["claude:keep", "claude:also"]))
                    }
                    RequestKind::PreviewOnly => WorkerResponse::preview(request, None),
                })
            },
        );
        (rx, executor)
    }

    fn wait_for_filters(
        rx: &mpsc::Receiver<(RequestKind, SearchFilters, String)>,
        kind: RequestKind,
    ) -> SearchFilters {
        wait_for_recorded_request(rx, kind).0
    }

    fn wait_for_recorded_request(
        rx: &mpsc::Receiver<(RequestKind, SearchFilters, String)>,
        kind: RequestKind,
    ) -> (SearchFilters, String) {
        let deadline = std::time::Instant::now() + Duration::from_secs(5);
        while std::time::Instant::now() < deadline {
            match rx.recv_timeout(Duration::from_millis(100)) {
                Ok((seen, filters, query)) if seen == kind => return (filters, query),
                Ok(_) => continue,
                Err(mpsc::RecvTimeoutError::Timeout) => continue,
                Err(mpsc::RecvTimeoutError::Disconnected) => break,
            }
        }
        panic!("no {kind:?} request arrived");
    }

    #[test]
    fn status_bar_names_every_active_filter_value() {
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        assert_eq!(harness.app.filter_status(), "p:any f:any s:any w:off");
        harness.app.filters.provider = Some(Provider::Codex);
        harness.app.filters.session_kinds = Some(vec![SessionKind::User]);
        harness.app.filters.since = Some(Utc::now() - chrono::Duration::days(7));
        harness.app.filters.warnings_only = true;
        harness.step();
        assert_eq!(harness.app.filter_status(), "p:codex f:user s:7d w:on");
        assert!(harness.status_line().contains("p:codex"));
        assert!(harness.status_line().contains("w:on"));
    }

    #[test]
    fn provider_cycle_key_issues_the_expected_filters() {
        let (filters_rx, executor) = recording_executor();
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:keep"]);
        let startup = wait_for_filters(&filters_rx, RequestKind::Search);
        assert_eq!(startup.provider, None);
        harness.wait_until_previewed("claude:keep");

        // Every press issues one Search with the next provider, cycling through
        // value_variants() and back to None.
        let variants = Provider::value_variants().to_vec();
        let mut expected: Vec<Option<Provider>> = variants.iter().copied().map(Some).collect();
        expected.push(None);
        for want in expected {
            harness.script(vec![key(KeyCode::Char('p'))]);
            harness.step_until_script_drained();
            let filters = wait_for_filters(&filters_rx, RequestKind::Search);
            assert_eq!(
                filters.provider, want,
                "provider cycle must follow value_variants"
            );
        }
    }

    #[test]
    fn session_kind_key_issues_the_expected_filters() {
        let (filters_rx, executor) = recording_executor();
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:keep"]);
        let startup = wait_for_filters(&filters_rx, RequestKind::Search);
        assert_eq!(startup.session_kinds, None);
        harness.wait_until_previewed("claude:keep");

        // None -> both classes (the default search set) -> user only -> subagent only -> None.
        let cycle: [Option<Vec<SessionKind>>; 4] = [
            Some(SessionKind::default_search_set()),
            Some(vec![SessionKind::User]),
            Some(vec![SessionKind::Subagent]),
            None,
        ];
        for want in cycle {
            harness.script(vec![key(KeyCode::Char('f'))]);
            harness.step_until_script_drained();
            let filters = wait_for_filters(&filters_rx, RequestKind::Search);
            assert_eq!(
                filters.session_kinds, want,
                "class cycle must visit both, each, none"
            );
        }
    }

    #[test]
    fn since_window_key_issues_the_expected_filters() {
        let (filters_rx, executor) = recording_executor();
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:keep"]);
        wait_for_filters(&filters_rx, RequestKind::Search);
        harness.wait_until_previewed("claude:keep");

        // 1 day -> 7 days -> 30 days -> off; until stays unset and the bound ages correctly.
        let windows = [24, 24 * 7, 24 * 30];
        for hours in windows {
            harness.script(vec![key(KeyCode::Char('s'))]);
            harness.step_until_script_drained();
            let filters = wait_for_filters(&filters_rx, RequestKind::Search);
            let since = filters.since.expect("window must set since");
            assert_eq!(filters.until, None, "the TUI window cycle sets only since");
            let age_hours = (chrono::Utc::now() - since).num_hours();
            assert!(
                (age_hours - hours).abs() <= 1,
                "since must be ~{hours}h old, got {age_hours}h"
            );
        }
        harness.script(vec![key(KeyCode::Char('s'))]);
        harness.step_until_script_drained();
        let filters = wait_for_filters(&filters_rx, RequestKind::Search);
        assert_eq!(filters.since, None, "the cycle must return to unbounded");
    }

    #[test]
    fn warnings_only_key_issues_the_expected_filters() {
        let (filters_rx, executor) = recording_executor();
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:keep"]);
        let startup = wait_for_filters(&filters_rx, RequestKind::Search);
        assert!(!startup.warnings_only);
        harness.wait_until_previewed("claude:keep");

        harness.script(vec![key(KeyCode::Char('w'))]);
        harness.step_until_script_drained();
        assert!(wait_for_filters(&filters_rx, RequestKind::Search).warnings_only);
        harness.script(vec![key(KeyCode::Char('w'))]);
        harness.step_until_script_drained();
        assert!(!wait_for_filters(&filters_rx, RequestKind::Search).warnings_only);
    }

    #[test]
    fn filter_validation_failure_renders_error_and_sends_nothing() {
        let (filters_rx, executor) = recording_executor();
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:keep"]);
        wait_for_filters(&filters_rx, RequestKind::Search);
        harness.wait_until_previewed("claude:keep");

        // The one validation rule: parent + user-only kinds. The TUI has no parent binding,
        // so drive the state directly — the binding path must still refuse to send.
        harness.app.filters.parent_session_id = Some("claude:parent".to_string());
        harness.app.filters.session_kinds = Some(vec![SessionKind::User]);
        // Drain records already delivered (the startup preview) so the no-send assertion
        // below observes only requests issued after the invalid combination exists.
        while filters_rx.try_recv().is_ok() {}
        harness.script(vec![key(KeyCode::Char('w'))]);
        harness.step_until_script_drained();
        assert!(
            harness.app.error.is_some(),
            "a rejected filter combination must render the error line"
        );
        harness.step();
        assert!(harness.error_line().contains("session_kinds"));
        assert!(
            filters_rx.try_recv().is_err(),
            "no request may be sent for an invalid combination"
        );
    }

    #[test]
    fn provider_labels_are_case_folded_distinct_and_fit_the_width() {
        let labels: Vec<&str> = Provider::value_variants()
            .iter()
            .map(|provider| provider_label(*provider).0)
            .collect();
        assert_eq!(labels.len(), 9, "all nine providers must carry labels");
        let folded: Vec<String> = labels.iter().map(|l| l.to_lowercase()).collect();
        let mut sorted = folded.clone();
        sorted.sort();
        sorted.dedup();
        assert_eq!(
            folded.len(),
            sorted.len(),
            "labels must be case-folded distinct"
        );
        for label in &labels {
            assert!(
                label.len() <= longest_provider_label(),
                "{label} must fit the label column floor"
            );
        }
        // A configured width below the floor clamps up, never truncating.
        assert!(longest_provider_label() >= "GEMINICLI".len());
    }

    #[test]
    fn exceptionally_narrow_terminal_clamps_provider_column_without_panicking() {
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:keep"]);
        harness.terminal.backend_mut().resize(12, 14);
        harness.step();
        assert_eq!(harness.terminal.backend().buffer().area.width, 12);
    }

    #[test]
    fn status_bar_and_error_line_fit_an_eighty_column_frame() {
        let (_filters_rx, executor) = recording_executor();
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:keep"]);
        harness.wait_until_previewed("claude:keep");

        for width in [80u16, 100] {
            let mut narrow = Terminal::new(TestBackend::new(width, 24)).unwrap();
            step(
                &mut narrow,
                &mut ScriptedEventSource::new(Vec::new()),
                &mut harness.app,
            )
            .unwrap();
            let status = screen_rows_text(&narrow, 23..24, width);
            assert!(
                status.chars().count() <= width as usize,
                "status bar must fit {width} columns: {status:?}"
            );

            harness.app.error = Some(
                "search failed: database busy: run `aise reindex --full`, then retry aise tui"
                    .to_string(),
            );
            step(
                &mut narrow,
                &mut ScriptedEventSource::new(Vec::new()),
                &mut harness.app,
            )
            .unwrap();
            let error_row = screen_rows_text(&narrow, 22..23, width);
            assert!(
                error_row.chars().count() <= width as usize,
                "error line must fit {width} columns: {error_row:?}"
            );
            assert!(
                error_row.contains("reindex"),
                "the final recovery clause must survive elision (REQ047)"
            );
            harness.app.error = None;
        }
    }

    fn screen_rows_text(
        terminal: &Terminal<TestBackend>,
        rows: std::ops::Range<u16>,
        _width: u16,
    ) -> String {
        let buffer = terminal.backend().buffer();
        let area = buffer.area;
        let mut lines = Vec::new();
        for y in rows {
            if y >= area.bottom() {
                continue;
            }
            let mut line = String::new();
            for x in area.left()..area.right() {
                line.push_str(buffer[(x, y)].symbol());
            }
            lines.push(line.trim_end().to_string());
        }
        lines.join("\n")
    }

    #[test]
    fn wrapped_single_line_preview_can_scroll_to_its_tail() {
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        harness.terminal.backend_mut().resize(30, 10);
        harness.app.preview = "x".repeat(200);
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        assert!(
            harness.app.preview_line_count > 1,
            "wrapped terminal rows, not logical newline count, own the scroll bound"
        );
        harness.app.scroll_preview(isize::MAX);
        assert!(
            harness.app.preview_scroll > 0,
            "wrapped rows={}, viewport={}",
            harness.app.preview_line_count,
            harness.app.preview_viewport_rows
        );
    }

    #[test]
    fn multiword_preview_uses_actual_word_wrapped_row_count() {
        // Seeded, because the pane shows the empty-state guidance when the list is empty and
        // this measures the wrapping of a real preview.
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        harness.app.preview = std::iter::repeat_n("abcdefghij", 20)
            .collect::<Vec<_>>()
            .join(" ");
        harness.terminal.backend_mut().resize(30, 10);
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        let pane_interior_width = 15_usize;
        let arithmetic_count = harness
            .app
            .preview
            .chars()
            .count()
            .div_ceil(pane_interior_width);
        assert!(
            harness.app.preview_line_count > arithmetic_count,
            "word-boundary wrapping must use Ratatui's actual line composer"
        );
        harness.app.scroll_preview(isize::MAX);
        assert!(harness.app.preview_scroll > 0);
    }

    /// Every non-empty `Search` query the executor was asked for, in order, drained after the
    /// script settles. The startup request carries an empty query and is excluded.
    fn typed_search_queries(
        rx: &mpsc::Receiver<(RequestKind, SearchFilters, String)>,
    ) -> Vec<String> {
        let mut queries = Vec::new();
        while let Ok((kind, _, query)) = rx.recv_timeout(Duration::from_millis(200)) {
            if kind == RequestKind::Search && !query.is_empty() {
                queries.push(query);
            }
        }
        queries
    }

    #[test]
    fn one_interrupt_arms_and_says_so_and_the_next_one_quits() {
        // Raw mode turns off the terminal's interrupt character, so Ctrl+C is an ordinary key
        // event and nothing raises SIGINT. Browse mode ignored it outright, which left `q` and
        // Esc as the only exits from a full-screen application.
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        harness.script(vec![ctrl_key(KeyCode::Char('c'))]);
        assert!(
            harness.step_until_script_drained().is_none(),
            "one interrupt must not quit on its own"
        );
        assert!(
            harness.status_line().contains("interrupt again"),
            "the armed state must be visible, got {:?}",
            harness.status_line()
        );

        harness.script(vec![ctrl_key(KeyCode::Char('c'))]);
        assert!(matches!(
            harness.step_until_script_drained(),
            Some(AppAction::Quit)
        ));
    }

    #[test]
    fn any_other_key_disarms_the_interrupt() {
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        harness.script(vec![ctrl_key(KeyCode::Char('c')), key(KeyCode::Char('j'))]);
        assert!(harness.step_until_script_drained().is_none());
        assert!(
            !harness.status_line().contains("interrupt again"),
            "a key press between the two must clear the armed state"
        );
        harness.script(vec![ctrl_key(KeyCode::Char('c'))]);
        assert!(
            harness.step_until_script_drained().is_none(),
            "the count restarts, so this is the first interrupt again"
        );
    }

    #[test]
    fn the_interrupt_in_the_search_box_does_not_type_a_c() {
        // The search box appended whatever character arrived, modifiers and all, so the one key
        // a reader reaches for to escape put a `c` in their query instead.
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        harness.script(vec![
            key(KeyCode::Char('/')),
            key(KeyCode::Char('a')),
            ctrl_key(KeyCode::Char('c')),
        ]);
        assert!(harness.step_until_script_drained().is_none());
        assert_eq!(harness.app.query, "a", "the interrupt is not text");

        harness.script(vec![ctrl_key(KeyCode::Char('c'))]);
        assert!(matches!(
            harness.step_until_script_drained(),
            Some(AppAction::Quit)
        ));
    }

    #[test]
    fn a_chorded_letter_does_not_fire_its_browse_binding() {
        // Each arm matched a bare character, so Ctrl+Q quit, Ctrl+S moved the time window, and
        // Ctrl+H — ASCII backspace on many terminals — scrolled the preview.
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        harness.script(vec![
            ctrl_key(KeyCode::Char('q')),
            ctrl_key(KeyCode::Char('s')),
            ctrl_key(KeyCode::Char('p')),
        ]);
        assert!(
            harness.step_until_script_drained().is_none(),
            "Ctrl+Q is not the quit binding"
        );
        assert_eq!(
            harness.app.filter_status(),
            "p:any f:any s:any w:off",
            "Ctrl+S and Ctrl+P are not filter bindings"
        );
    }

    #[test]
    fn a_rebound_key_drives_the_tui_and_the_default_it_replaced_does_not() {
        // The point of the table: a key named in `[ui.keys]` reaches the loop, and the key it
        // replaced stops meaning what it did.
        let mut config = Config::default();
        config.ui.keys = toml::from_str("quit = [\"x\"]").expect("a partial table parses");
        config.validate().expect("rebinding quit alone is valid");
        let mut harness = TuiHarness::with_config(config, idle_executor()).seeded(&["claude:one"]);

        harness.script(vec![key(KeyCode::Char('q'))]);
        assert!(
            harness.step_until_script_drained().is_none(),
            "q was rebound away from quit"
        );
        harness.script(vec![key(KeyCode::Char('x'))]);
        assert!(matches!(
            harness.step_until_script_drained(),
            Some(AppAction::Quit)
        ));
    }

    #[test]
    fn a_typed_burst_becomes_one_search_after_the_configured_quiet_period() {
        // Every keystroke used to start a search that the next keystroke cancelled, so typing a
        // five-letter word began five corpus scans to answer one question. On a 36.5 GB index a
        // session search costs 2.3 to 3.6 s, which is what the reader waits for either way, but
        // the four abandoned scans are work nobody asked for.
        let mut config = Config::default();
        config.ui.search_debounce_ms = 40;
        config.ui.idle_poll_interval_ms = 1;
        let (requests, executor) = recording_executor();
        let mut harness = TuiHarness::with_config(config, executor).seeded(&["claude:keep"]);
        wait_for_recorded_request(&requests, RequestKind::Search);

        harness.script(vec![
            key(KeyCode::Char('/')),
            key(KeyCode::Char('a')),
            key(KeyCode::Char('b')),
            key(KeyCode::Char('c')),
        ]);
        harness.step_until_script_drained();
        step_until(&mut harness, |harness| !harness.app.searching);

        assert_eq!(
            typed_search_queries(&requests),
            vec!["abc".to_string()],
            "a burst inside the quiet period must ask one question, and it must be the whole one"
        );
    }

    #[test]
    fn the_quiet_period_is_honored_however_it_compares_to_the_idle_interval() {
        // Two independent millisecond knobs, and every other test pins the interval at 1, so only
        // "quiet period longer than the interval" was ever exercised. The shorter case is the one
        // `slice.min(delay)` in `step` exists for: without it the wait runs to the end of the idle
        // interval and a 5 ms quiet period costs the reader 500 ms. The search still has to carry
        // the whole typed word in every case, so a delay that fires early is a failure too.
        // The wait is sliced, so `interval` shorter than `debounce` takes several turns to reach
        // it and the pairs are chosen to stay well inside MAX_TEST_STEPS.
        for (debounce_ms, interval_ms) in [(5, 500), (40, 40), (20, 5), (0, 500)] {
            let mut config = Config::default();
            config.ui.search_debounce_ms = debounce_ms;
            config.ui.idle_poll_interval_ms = interval_ms;
            let (requests, executor) = recording_executor();
            let mut harness = TuiHarness::with_config(config, executor).seeded(&["claude:keep"]);
            wait_for_recorded_request(&requests, RequestKind::Search);

            let typed = vec![
                key(KeyCode::Char('/')),
                key(KeyCode::Char('a')),
                key(KeyCode::Char('b')),
                key(KeyCode::Char('c')),
            ];
            // Watch every wait the loop asks for from here on, rather than counting polls to a
            // chosen one. The old form indexed by "one poll per key", a turn count the loop does
            // not promise: the index drifted onto a wait taken after the search had already
            // fired, which passed here and failed on a loaded runner.
            let waits_before = harness.events.poll_timeouts.len();
            harness.script(typed);
            harness.step_until_script_drained();
            // `flush_edited_query` clears the edit stamp, so this asks "has the search been
            // issued yet" without touching the recorded requests. Waiting on `typed_search_queries`
            // instead would consume the very request the assertion is about.
            step_until(&mut harness, |harness| {
                harness.app.query_edited_at.is_none()
            });

            // Correctness alone does not pin the timing down: drop the shortening and the search
            // still happens, one whole idle interval late — half a second of silence for a five
            // millisecond quiet period. A zero quiet period is exempt because the keystroke has
            // already searched, leaving no pending delay for the wait to be shortened to.
            if debounce_ms > 0 {
                // Shortening means some wait taken with the edit outstanding is bounded by the
                // quiet period. Drop `slice.min(delay)` and every wait in this window is an idle
                // interval or an active-worker slice instead, so none is left under the bound --
                // which is the regression, stated as the exact thing its absence would produce.
                let waits = &harness.events.poll_timeouts[waits_before..];
                let shortest = waits.iter().min().copied().unwrap_or(Duration::MAX);
                assert!(
                    shortest <= Duration::from_millis(debounce_ms),
                    "a {debounce_ms} ms quiet period with a {interval_ms} ms idle interval never \
                     waited less than {shortest:?} before looking at the edited query again; the \
                     waits it asked for were {waits:?}"
                );
            }

            let queries = typed_search_queries(&requests);
            assert_eq!(
                queries.last().map(String::as_str),
                Some("abc"),
                "a {debounce_ms} ms quiet period with a {interval_ms} ms idle interval searched \
                 {queries:?}"
            );
        }
    }

    #[test]
    fn enter_searches_the_typed_query_without_waiting_out_the_quiet_period() {
        // Leaving the search box is the reader saying they are done typing, so the delay has
        // nothing left to wait for. Without this, a long configured delay would look exactly
        // like a TUI that ignores Enter.
        let mut config = Config::default();
        config.ui.search_debounce_ms = 600_000;
        config.ui.idle_poll_interval_ms = 1;
        let (requests, executor) = recording_executor();
        let mut harness = TuiHarness::with_config(config, executor).seeded(&["claude:keep"]);
        wait_for_recorded_request(&requests, RequestKind::Search);

        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('a'))]);
        harness.step_until_script_drained();
        assert!(
            typed_search_queries(&requests).is_empty(),
            "the ten-minute delay must still be holding the search"
        );

        harness.script(vec![key(KeyCode::Enter)]);
        harness.step_until_script_drained();
        let (_, query) = wait_for_recorded_request(&requests, RequestKind::Search);
        assert_eq!(query, "a", "Enter searches what is in the box");
        assert!(!harness.app.search_mode, "Enter also leaves the search box");
    }

    #[test]
    fn a_zero_quiet_period_searches_on_every_keystroke() {
        // 0 names the behavior this had before the setting existed, so it has to keep meaning
        // that rather than becoming an accidental synonym for the default.
        //
        // The assertion is on the state a keystroke leaves behind, not on the sequence the
        // executor observes. The mailbox keeps one pending search, so whether it sees "a" before
        // "ab" replaces it depends on which thread runs next — an earlier version of this test
        // asserted `["a", "ab"]` and passed only while the worker happened to win that race.
        // What distinguishes zero from a delay is whether the request was issued on the key or
        // held, and `query_edited_at` says so deterministically.
        let mut config = Config::default();
        config.ui.search_debounce_ms = 0;
        config.ui.idle_poll_interval_ms = 1;
        let (requests, executor) = recording_executor();
        let mut harness = TuiHarness::with_config(config, executor).seeded(&["claude:keep"]);
        wait_for_recorded_request(&requests, RequestKind::Search);

        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('a'))]);
        harness.step_until_script_drained();
        assert!(
            harness.app.query_edited_at.is_none(),
            "zero issues the search on the keystroke rather than holding it"
        );
        // `searching` is not asserted here: a fake executor can answer within the same drained
        // steps, so whether the flag is still up is another race between the two threads.

        harness.script(vec![key(KeyCode::Char('b'))]);
        harness.step_until_script_drained();
        assert!(harness.app.query_edited_at.is_none());
        step_until(&mut harness, |harness| !harness.app.searching);

        // Whatever the executor coalesced, the last question asked is the whole query.
        let observed = typed_search_queries(&requests);
        assert_eq!(
            observed.last().map(String::as_str),
            Some("ab"),
            "observed {observed:?}"
        );
    }

    #[test]
    fn a_configured_quiet_period_holds_the_keystroke_rather_than_issuing_it() {
        // The mirror of the test above, and the reason `query_edited_at` is the honest signal:
        // with a delay the key leaves a pending edit rather than a request.
        let mut config = Config::default();
        config.ui.search_debounce_ms = 600_000;
        config.ui.idle_poll_interval_ms = 1;
        let (requests, executor) = recording_executor();
        let mut harness = TuiHarness::with_config(config, executor).seeded(&["claude:keep"]);
        wait_for_recorded_request(&requests, RequestKind::Search);

        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('a'))]);
        harness.step_until_script_drained();
        assert!(
            harness.app.query_edited_at.is_some(),
            "a delay holds the edit until typing goes quiet"
        );
        assert!(typed_search_queries(&requests).is_empty());
    }

    #[test]
    fn since_window_cycle_and_status_label_read_the_same_table() {
        // The binding stores an absolute `since`; the status bar reads the window back out of
        // it. Separate thresholds in the two places would let the cycle advance to the 7-day
        // span while the bar still said 1d, so each press must both move to the next span and
        // rename the window.
        let (filters_rx, executor) = recording_executor();
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:keep"]);
        wait_for_filters(&filters_rx, RequestKind::Search);
        harness.wait_until_previewed("claude:keep");
        assert!(harness.app.filter_status().contains("s:any"));

        for (label, span_hours) in SINCE_WINDOWS {
            harness.script(vec![key(KeyCode::Char('s'))]);
            harness.step_until_script_drained();
            let filters = wait_for_filters(&filters_rx, RequestKind::Search);
            let since = filters.since.expect("the s binding sets a lower bound");
            assert_eq!(
                (Utc::now() - since).num_hours(),
                span_hours,
                "pressing s must move to the {label} span"
            );
            assert!(
                harness.app.filter_status().contains(&format!("s:{label}")),
                "the status bar must name the window the binding just set, got {}",
                harness.app.filter_status()
            );
        }

        harness.script(vec![key(KeyCode::Char('s'))]);
        harness.step_until_script_drained();
        let filters = wait_for_filters(&filters_rx, RequestKind::Search);
        assert_eq!(
            filters.since, None,
            "the last window cycles back to unbounded"
        );
        assert!(harness.app.filter_status().contains("s:any"));
    }

    #[test]
    fn the_status_bar_teaches_the_rebound_key_rather_than_the_default() {
        // A help bar that names keys the configuration replaced is worse than none: the reader
        // presses what it says and nothing happens.
        let mut config = Config::default();
        config.ui.keys = toml::from_str("quit = [\"x\"]").unwrap();
        config.validate().unwrap();
        let mut harness = TuiHarness::with_config(config, idle_executor()).seeded(&["claude:one"]);
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        let status = harness.status_line();
        assert!(status.contains("x: quit"), "{status:?}");
        assert!(!status.contains("q: quit"), "{status:?}");
    }

    #[test]
    fn the_preview_title_reports_the_visible_rows_only_when_some_are_hidden() {
        // A pane that shows two thirds of a transcript looked exactly like one showing all of
        // it, so a reader had no reason to reach for the scroll keys and no way to tell whether
        // pressing them had done anything.
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        harness.terminal.backend_mut().resize(60, 14);
        harness.app.preview = (0..60)
            .map(|line| format!("line {line}"))
            .collect::<Vec<_>>()
            .join("\n");
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        let title = harness.app.preview_title();
        assert!(title.contains("1-"), "{title:?}");
        assert!(
            title.contains(&format!("/{}", harness.app.preview_line_count)),
            "{title:?}"
        );

        harness.app.scroll_preview(5);
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        assert!(
            harness.app.preview_title().contains("6-"),
            "scrolling must move the reported window, got {:?}",
            harness.app.preview_title()
        );

        // Content that fits gets no counter: a number that never changes is noise.
        harness.app.preview = "one short line".to_string();
        harness.app.preview_scroll = 0;
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        assert_eq!(harness.app.preview_title(), " Preview ");
    }

    /// Type `text` into the search box, entering it first.
    fn type_query(harness: &mut TuiHarness, text: &str) {
        let mut script = vec![key(KeyCode::Char('/'))];
        script.extend(text.chars().map(|character| key(KeyCode::Char(character))));
        harness.script(script);
        harness.step_until_script_drained();
    }

    #[test]
    fn the_key_list_names_every_bound_command_including_the_ones_the_status_bar_sheds() {
        // The status bar is one row and drops most hints on an eighty-column frame, so paging,
        // scrolling, top and bottom, and resume had nowhere else to be named.
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        harness.terminal.backend_mut().resize(80, 24);
        harness.script(vec![key(KeyCode::Char('?'))]);
        assert!(harness.step_until_script_drained().is_none());
        assert!(harness.app.showing_help);

        let listed = harness.app.help_lines().join("\n");
        for action in TuiAction::ALL {
            assert!(
                listed.contains(action.name()),
                "{} is bound but not listed:\n{listed}",
                action.name()
            );
        }
        let screen = harness.screen();
        assert!(screen.contains("Keys"), "{screen}");
    }

    #[test]
    fn the_key_list_follows_a_rebinding_and_omits_what_is_unbound() {
        let mut config = Config::default();
        config.ui.keys =
            toml::from_str("quit = [\"x\"]\ncycle_provider = []").expect("a partial table parses");
        config.validate().unwrap();
        let harness = TuiHarness::with_config(config, idle_executor()).seeded(&["claude:one"]);
        let listed = harness.app.help_lines().join("\n");
        assert!(listed.contains("x") && listed.contains("quit"), "{listed}");
        assert!(
            !listed.contains("cycle_provider"),
            "an unbound command must not claim to exist:\n{listed}"
        );
    }

    #[test]
    fn every_key_bound_to_one_command_reaches_it_through_the_loop() {
        // `action_for` resolving all three is not the same claim as the browser answering all
        // three: the loop is what a reader presses. Aliases are the reason the list is a list —
        // a reader adding their own key keeps the shipped one.
        let mut config = Config::default();
        config.ui.keys =
            toml::from_str("quit = [\"x\", \"ctrl+q\", \"f5\"]").expect("aliases parse");
        config
            .validate()
            .expect("several keys for one command is not a conflict");

        for press in [
            key(KeyCode::Char('x')),
            ctrl_key(KeyCode::Char('q')),
            key(KeyCode::F(5)),
        ] {
            let mut harness =
                TuiHarness::with_config(config.clone(), idle_executor()).seeded(&["claude:one"]);
            harness.script(vec![press.clone()]);
            assert!(
                matches!(harness.step_until_script_drained(), Some(AppAction::Quit)),
                "{press:?} is bound to quit but did not quit"
            );
        }

        let harness = TuiHarness::with_config(config, idle_executor()).seeded(&["claude:one"]);
        let listed = harness.app.help_lines().join("\n");
        assert!(
            listed.contains("x, ctrl+q, f5"),
            "the key list has to name every alias, not just the first:\n{listed}"
        );
    }

    #[test]
    fn any_other_key_closes_the_key_list_and_scrolling_keeps_it_open() {
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        harness.terminal.backend_mut().resize(80, 14);
        harness.script(vec![key(KeyCode::Char('?'))]);
        harness.step_until_script_drained();

        // The list is taller than a fourteen-row frame, so it has to scroll rather than clip.
        harness.script(vec![ctrl_key(KeyCode::Char('d'))]);
        harness.step_until_script_drained();
        assert!(harness.app.showing_help, "scrolling must not close it");
        assert!(harness.app.help_scroll > 0, "and must actually move");

        harness.script(vec![key(KeyCode::Char('j'))]);
        harness.step_until_script_drained();
        assert!(!harness.app.showing_help, "any other key closes it");
        assert_eq!(
            harness.app.selected, 0,
            "the key that closed it is spent on closing, not on the browser underneath"
        );
    }

    #[test]
    fn the_caret_moves_and_typing_inserts_where_it_sits() {
        // The box only ever appended, so fixing a typo in the middle of a query meant deleting
        // back to it and retyping the rest, and Left, Right, Home, End, and Delete did nothing
        // at all — keys a reader presses in any other text field.
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        type_query(&mut harness, "rust wrker");

        // Left four times puts the caret between `w` and `r`.
        harness.script(vec![key(KeyCode::Left); 4]);
        harness.step_until_script_drained();
        harness.script(vec![key(KeyCode::Char('o'))]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.query, "rust worker");

        harness.script(vec![key(KeyCode::Home), key(KeyCode::Char('!'))]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.query, "!rust worker");

        harness.script(vec![key(KeyCode::End), key(KeyCode::Char('?'))]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.query, "!rust worker?");

        harness.script(vec![key(KeyCode::Home), key(KeyCode::Delete)]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.query, "rust worker?");
    }

    #[test]
    fn backspace_deletes_before_the_caret_rather_than_at_the_end() {
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        type_query(&mut harness, "abcd");
        harness.script(vec![
            key(KeyCode::Left),
            key(KeyCode::Left),
            key(KeyCode::Backspace),
        ]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.query, "acd", "backspace follows the caret");
    }

    #[test]
    fn clearing_and_deleting_a_word_are_bound_and_leave_the_caret_where_they_cut() {
        // Backspace was the only way to shorten a query, so clearing a long one meant holding
        // it down.
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        type_query(&mut harness, "rust worker thread");
        harness.script(vec![ctrl_key(KeyCode::Char('w'))]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.query, "rust worker ");

        // A second press takes the word and the space that preceded it, not just the space.
        harness.script(vec![ctrl_key(KeyCode::Char('w'))]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.query, "rust ");

        harness.script(vec![ctrl_key(KeyCode::Char('u'))]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.query, "");
        assert_eq!(harness.app.query_cursor, 0);
    }

    #[test]
    fn the_caret_stays_on_a_character_boundary_in_a_multi_byte_query() {
        // A byte index into a `String` that lands mid-character panics on the next slice, and a
        // query is whatever the reader typed.
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        type_query(&mut harness, "café漢字");
        for _ in 0..3 {
            harness.script(vec![key(KeyCode::Left)]);
            harness.step_until_script_drained();
            assert!(
                harness.app.query.is_char_boundary(harness.app.query_cursor),
                "cursor {} is inside a character of {:?}",
                harness.app.query_cursor,
                harness.app.query
            );
        }
        // Three Lefts from the end put the caret between `f` and `é`, so backspace takes the
        // `f`. Each of those characters is a different width in bytes, which is the point.
        harness.script(vec![key(KeyCode::Backspace)]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.query, "caé漢字");

        harness.script(vec![key(KeyCode::End)]);
        harness.step_until_script_drained();
        assert_eq!(harness.app.query_cursor, harness.app.query.len());
    }

    #[test]
    fn moving_the_caret_does_not_start_a_search() {
        // An arrow key changes nothing to search for, so restarting the delay on one would keep
        // a reader who is repositioning from ever seeing a result.
        let mut config = Config::default();
        config.ui.search_debounce_ms = 600_000;
        config.ui.idle_poll_interval_ms = 1;
        let (requests, executor) = recording_executor();
        let mut harness = TuiHarness::with_config(config, executor).seeded(&["claude:keep"]);
        wait_for_recorded_request(&requests, RequestKind::Search);
        type_query(&mut harness, "ab");
        harness.app.flush_edited_query();
        step_until(&mut harness, |harness| !harness.app.searching);
        assert_eq!(
            typed_search_queries(&requests).last().map(String::as_str),
            Some("ab")
        );

        harness.script(vec![
            key(KeyCode::Left),
            key(KeyCode::Right),
            key(KeyCode::Home),
        ]);
        harness.step_until_script_drained();
        assert!(
            harness.app.query_edited_at.is_none(),
            "a caret move is not an edit"
        );
        assert!(typed_search_queries(&requests).is_empty());
    }

    #[test]
    fn a_query_wider_than_the_box_scrolls_under_the_caret() {
        // Without this the text a reader is typing runs past the right border and the caret
        // goes with it, so the box looks like it stopped accepting input.
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        harness.terminal.backend_mut().resize(24, 14);
        type_query(&mut harness, "0123456789abcdefghijklmnop");
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        let area = harness.terminal.get_frame().area();
        let cursor = harness.terminal.get_cursor_position().unwrap();
        assert!(
            cursor.x > area.x && cursor.x < area.x + area.width - 1,
            "caret at {} is outside the box {}..{}",
            cursor.x,
            area.x,
            area.x + area.width
        );
        assert!(
            harness.search_box().contains('p'),
            "the tail being typed must be the part on screen: {:?}",
            harness.search_box()
        );
    }

    #[test]
    fn the_selected_row_is_marked_and_the_marker_does_not_move_the_others() {
        // Colour and bold were the only things saying which row Enter would resume, and both are
        // lost on a monochrome terminal, to a colour-blind reader, and in a captured log.
        let mut harness =
            TuiHarness::with_executor(idle_executor()).seeded(&["claude:one", "claude:two"]);
        harness.app.selected = 1;
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        let rows: Vec<String> = harness
            .session_rows()
            .lines()
            .filter(|line| line.contains('['))
            .map(str::to_string)
            .collect();
        assert_eq!(rows.len(), 2, "{rows:?}");
        // The rendered rows still carry the pane border, so this looks for the marker inside
        // the row rather than at its start.
        let marker = TerminalStyle::default().selection_symbol().trim_end();
        assert!(rows[1].contains(marker), "{rows:?}");
        assert!(!rows[0].contains(marker), "{rows:?}");
        // The unselected row keeps the marker's column, so moving the selection does not slide
        // every other row sideways.
        // Display columns, not byte offsets: the marker is multi-byte, so `find` would report
        // two aligned rows as misaligned.
        let column = |row: &str| {
            let at = row.find('[').expect("every row shows a provider label");
            UnicodeWidthStr::width(&row[..at])
        };
        assert_eq!(column(&rows[0]), column(&rows[1]), "{rows:?}");
    }

    #[test]
    fn an_ascii_terminal_renders_nothing_outside_ascii() {
        // `LANG=C` over ssh is a real terminal. Check complete rendered frames rather than only
        // TerminalStyle's own symbols: transcript truncation and empty-state prose are separate
        // producers and previously bypassed the style.
        fn non_ascii_symbols(terminal: &Terminal<TestBackend>) -> Vec<String> {
            let buffer = terminal.backend().buffer();
            let area = buffer.area;
            let mut offenders = Vec::new();
            for y in area.top()..area.bottom() {
                for x in area.left()..area.right() {
                    let symbol = buffer[(x, y)].symbol().to_string();
                    if !symbol.is_ascii() {
                        offenders.push(symbol);
                    }
                }
            }
            offenders.sort();
            offenders.dedup();
            offenders
        }

        let mut config = Config::default();
        config.ui.unicode = CapabilityMode::Off;
        let ascii = TerminalStyle::resolve(CapabilityMode::Off, CapabilityMode::Auto);
        let mut harness = TuiHarness::with_config(config, idle_executor()).seeded(&["claude:one"]);
        let long_body = (0..40)
            .map(|line| format!("line {line}"))
            .collect::<Vec<_>>()
            .join("\n");
        let transcript = join_turns(&[("user", long_body.as_str())]);
        harness.app.preview = build_transcript_summary_inner(&transcript, 34, ascii, None).unwrap();
        harness.app.error = Some(
            "a failure long enough that the error line has to cut something out of its middle"
                .to_string(),
        );
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();

        let offenders = non_ascii_symbols(&harness.terminal);
        assert!(
            offenders.is_empty(),
            "these are not ASCII: {offenders:?}\n{}",
            harness.screen()
        );
        let screen = harness.screen();
        assert!(screen.contains('+') && screen.contains('|'), "{screen}");
        assert!(screen.contains("[...]"), "{screen}");

        // The empty-index guidance is another complete frame and must obey the same contract.
        harness.app.results.clear();
        harness.app.searching = false;
        harness.app.error = None;
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        let offenders = non_ascii_symbols(&harness.terminal);
        assert!(
            offenders.is_empty(),
            "empty-state frame is not ASCII: {offenders:?}\n{}",
            harness.screen()
        );
    }

    #[test]
    fn a_monochrome_terminal_drops_the_colour_and_keeps_the_emphasis() {
        // Under NO_COLOR or TERM=dumb the selected row was distinguished by colour alone.
        let mut config = Config::default();
        config.ui.color = CapabilityMode::Off;
        let mut harness = TuiHarness::with_config(config, idle_executor()).seeded(&["claude:one"]);
        harness.app.error = Some("a failure".to_string());
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();

        let buffer = harness.terminal.backend().buffer();
        let area = buffer.area;
        let mut bold = false;
        for y in area.top()..area.bottom() {
            for x in area.left()..area.right() {
                let cell = &buffer[(x, y)];
                assert_eq!(cell.fg, Color::Reset, "coloured cell at {x},{y}");
                assert_eq!(cell.bg, Color::Reset, "coloured cell at {x},{y}");
                bold |= cell.modifier.contains(Modifier::BOLD);
            }
        }
        assert!(bold, "emphasis must survive when the colour does not");
    }

    #[test]
    fn the_key_list_says_when_it_continues_below_the_fold_and_how_to_get_there() {
        // The overlay covers the whole frame, status bar included, so its title is the only
        // chrome it has. Twenty-eight commands plus headings do not fit a twenty-four-row
        // terminal: the list stopped mid-way with nothing saying more existed, and the keys
        // that scroll it were behind the bar it had just covered.
        let mut short = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        short.terminal.backend_mut().resize(100, 24);
        short.script(vec![key(KeyCode::Char('?'))]);
        short.step_until_script_drained();
        short
            .terminal
            .draw(|frame| short.app.render(frame))
            .unwrap();
        assert!(short.app.showing_help, "the script did not open the list");
        let title = short.region_text(0..1);
        assert!(
            title.contains("/32"),
            "the key list hid its length: {title:?}"
        );
        assert!(
            title.contains("K/J: scroll"),
            "the key list named no way to reach the rest: {title:?}"
        );

        // A frame tall enough for every command says none of that, because none of it applies.
        let mut tall = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        tall.terminal.backend_mut().resize(100, 60);
        tall.script(vec![key(KeyCode::Char('?'))]);
        tall.step_until_script_drained();
        tall.terminal.draw(|frame| tall.app.render(frame)).unwrap();
        let title = tall.region_text(0..1);
        assert!(
            title.contains("Keys (any other key closes)"),
            "a list that fits gained a scroll hint: {title:?}"
        );
    }

    #[test]
    fn a_search_still_running_is_not_reported_as_an_empty_index() {
        // `AppState::new` issues the startup search and sets `searching`, so the list is empty
        // until the first response lands. On a large index that is seconds: measured at 2.3 to
        // 3.6 s on a 36.5 GB one. Answering an in-flight search with "No sessions are indexed
        // yet. … run `aise reindex`" would send a reader to rebuild an index that is about to
        // return their sessions. The list title already says a search is running; the pane says
        // nothing until there is something to say.
        let mut harness = TuiHarness::with_executor(idle_executor());
        harness.terminal.backend_mut().resize(120, 24);
        harness.app.searching = true;
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        let screen = harness.region_text(0..24);
        assert!(
            !screen.contains("No sessions are indexed yet"),
            "a running search was reported as an empty index: {screen}"
        );
        assert!(
            !screen.contains("No sessions matched"),
            "a running search was reported as no match: {screen}"
        );

        // Once it settles with nothing, the guidance is right and appears.
        harness.app.searching = false;
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        assert!(
            harness
                .region_text(0..24)
                .contains("No sessions are indexed yet"),
            "a settled empty index said nothing"
        );
    }

    #[test]
    fn a_failed_initial_search_is_not_reported_as_an_empty_index() {
        let executor = Box::new(
            |request: &WorkerRequest, _cancellation: &Arc<QueryCancellation>| match request.kind {
                RequestKind::Search => Err(anyhow::anyhow!("search failed")),
                RequestKind::PreviewOnly => Ok(WorkerResponse::preview(request, None)),
            },
        );
        let mut harness = TuiHarness::with_executor(executor);
        harness.terminal.backend_mut().resize(120, 24);
        harness.start();
        step_until(&mut harness, |harness| {
            harness.app.error_owner == Some(RequestKind::Search) && !harness.app.searching
        });
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        let screen = harness.region_text(0..24);
        assert!(
            screen.contains("search failed"),
            "the operation error disappeared: {screen}"
        );
        assert!(
            !screen.contains("No sessions are indexed yet") && !screen.contains("aise reindex"),
            "a failed search was misreported as an empty index: {screen}"
        );
    }

    #[test]
    fn an_empty_result_set_names_the_keys_that_change_it() {
        // "No sessions matched the current query." stated the outcome and offered no way out of
        // it, and it said "query" even when there was none. An empty state that names the next
        // action is the difference between a dead end and a step; the keys come from the
        // bindings, because a message naming `/` after a reader has rebound it is worse than
        // no message.
        //
        // Nothing indexed and nothing asked for is a different problem from a query that
        // excluded everything, and only one of them is fixed with a key.
        let mut empty = TuiHarness::with_executor(idle_executor());
        empty.terminal.backend_mut().resize(120, 24);
        for _ in 0..8 {
            empty.step();
        }
        empty
            .terminal
            .draw(|frame| empty.app.render(frame))
            .unwrap();
        let screen = empty.region_text(0..24);
        assert!(
            screen.contains("No sessions are indexed yet"),
            "an empty index blamed the query: {screen}"
        );
        assert!(
            screen.contains("aise reindex"),
            "an empty index named no way to fill it: {screen}"
        );

        // A query that matched nothing: the keys that change the query and the filters.
        let mut filtered = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        filtered.terminal.backend_mut().resize(120, 24);
        filtered.app.query = "matches-nothing".to_string();
        filtered.app.results.clear();
        // A search that has finished and matched nothing, not one still running: the pane says
        // nothing while the search that would fill it is in flight.
        filtered.app.searching = false;
        filtered
            .terminal
            .draw(|frame| filtered.app.render(frame))
            .unwrap();
        let screen = filtered.region_text(0..24);
        for required in ["No sessions matched", "/: edit the query", "?: every key"] {
            assert!(
                screen.contains(required),
                "{required:?} missing from the empty state: {screen}"
            );
        }
        assert!(
            screen.contains("p/f/s/w: change filters"),
            "the empty state named no filter keys: {screen}"
        );
    }

    #[test]
    fn the_search_box_names_its_own_commands_instead_of_leaving_them_to_be_guessed() {
        // The search box binds eight editing commands and the status bar named none of them:
        // it read `type to search │ enter: browse`, so Ctrl+U, Ctrl+W, Home and End were
        // reachable only by pressing Esc first and then `?`. A focused input naming its own
        // keys is what lazygit's prompt footer and fzf's header do, and the roster here is
        // built from the bindings, so rebinding one changes what the bar teaches.
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        harness.terminal.backend_mut().resize(160, 24);
        harness.script(vec![key(KeyCode::Char('/'))]);
        harness.step_until_script_drained();
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        let status = harness.status_line();
        assert!(harness.app.search_mode, "the script did not enter the box");
        for required in [
            "type to search",
            "enter: browse",
            "ctrl+u: clear",
            "ctrl+w: delete word",
            "home/end: line start/end",
        ] {
            assert!(
                status.contains(required),
                "{required:?} missing from the search box status: {status:?}"
            );
        }

        // Narrow frames shed, but the way out of the box is priority 0 and cannot be shed --
        // a reader who cannot leave the search box cannot reach anything else.
        for width in [120_u16, 100, 80, 60] {
            let mut narrow = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
            narrow.terminal.backend_mut().resize(width, 24);
            narrow.script(vec![key(KeyCode::Char('/'))]);
            narrow.step_until_script_drained();
            narrow
                .terminal
                .draw(|frame| narrow.app.render(frame))
                .unwrap();
            let status = narrow.status_line();
            assert!(
                status.contains("enter: browse"),
                "width {width} dropped the way out of the box: {status:?}"
            );
            assert!(
                !status.contains('…'),
                "width {width} cut a hint mid-word: {status:?}"
            );
            assert!(
                UnicodeWidthStr::width(status.as_str()) <= usize::from(width),
                "width {width} overflowed the frame: {status:?}"
            );
        }
    }

    #[test]
    fn status_bar_drops_whole_hints_instead_of_eliding_them() {
        // Middle-eliding the joined help line rendered `p:any … │ j/k: move │ P…rs │ /: search`
        // at 80 columns: no binding the reader can act on. Whatever fits must fit whole.
        //
        // The roster that survives each width is not asserted, because it changes whenever a
        // hint is added and asserting it would only record today's arithmetic. What must hold
        // is that nothing is cut mid-word, that the line fits, and that the two hints which
        // lead everywhere else survive every width a terminal is likely to have: `q: quit`,
        // and `?: keys`, which names every command the bar had to shed.
        for width in [120_u16, 100, 80, 60] {
            let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
            harness.terminal.backend_mut().resize(width, 24);
            harness
                .terminal
                .draw(|frame| harness.app.render(frame))
                .unwrap();
            let status = harness.status_line();
            assert!(
                !status.contains('…'),
                "width {width} cut a hint mid-word: {status:?}"
            );
            assert!(
                status.contains("q: quit"),
                "width {width} dropped the way out: {status:?}"
            );
            // `?: keys` survives to eighty columns. Below that the filter status outranks it,
            // because a reader who cannot see that a filter is on can misread the result set,
            // while one who cannot see `?` has only lost a shortcut to the key list.
            if width >= 80 {
                assert!(
                    status.contains("?: keys"),
                    "width {width} dropped {:?}: {status:?}",
                    "?: keys"
                );
            }
            assert!(
                UnicodeWidthStr::width(status.as_str()) <= usize::from(width),
                "width {width} overflowed the frame: {status:?}"
            );
        }

        // A wide frame still shows the whole roster, so the shedding above is scarcity rather
        // than a hint that stopped being rendered at all.
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        harness.terminal.backend_mut().resize(160, 24);
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        let status = harness.status_line();
        for required in [
            "j/k: move",
            "K/J: scroll",
            "p/f/s/w: filters",
            "/: search",
            "enter: resume",
            "?: keys",
            "q: quit",
        ] {
            assert!(
                status.contains(required),
                "{required:?} missing: {status:?}"
            );
        }
    }

    #[test]
    fn reapplying_the_same_preview_keeps_the_wrapped_scroll_position() {
        // Two authorities counted preview length: the worker's logical `lines().count()` and the
        // renderer's word-wrapped row count. Clamping on re-apply against the smaller logical
        // count dragged a reader who had scrolled into the wrapped tail back to the top. A
        // preview re-arrives for the row already on screen whenever an earlier preview failure
        // is retried, so the rendered count has to own the bound.
        let mut harness = TuiHarness::with_executor(idle_executor()).seeded(&["claude:one"]);
        harness.terminal.backend_mut().resize(30, 10);
        harness.app.previewed_id = Some("claude:one".to_string());
        harness.app.preview = std::iter::repeat_n("abcdefghij", 20)
            .collect::<Vec<_>>()
            .join(" ");
        harness
            .terminal
            .draw(|frame| harness.app.render(frame))
            .unwrap();
        harness.app.scroll_preview(isize::MAX);
        let scrolled = harness.app.preview_scroll;
        assert!(
            scrolled > 0,
            "the fixture must wrap past its viewport for this to mean anything"
        );

        let request = WorkerRequest {
            kind: RequestKind::PreviewOnly,
            generation: harness.app.current_preview_generation.clone(),
            query: String::new(),
            filters: harness.app.filters.clone(),
            selected_id: Some("claude:one".to_string()),
        };
        let same_text = harness.app.preview.clone();
        harness
            .app
            .apply_response(WorkerResponse::preview(&request, Some(same_text)));

        assert_eq!(
            harness.app.preview_scroll, scrolled,
            "re-applying the identical preview must not move the reader's position"
        );
    }

    #[test]
    fn resize_to_a_short_terminal_keeps_the_last_preview_line_visible() {
        let (release, gate) = mpsc::channel::<()>();
        let executor = Box::new(
            move |request: &WorkerRequest, cancellation: &Arc<QueryCancellation>| {
                if request.kind == RequestKind::PreviewOnly {
                    park_until_released_or_cancelled(&gate, cancellation);
                }
                Ok(match request.kind {
                    RequestKind::Search => WorkerResponse::results(request, rows(&["claude:one"])),
                    RequestKind::PreviewOnly => WorkerResponse::preview(
                        request,
                        Some(
                            (0..50)
                                .map(|line| format!("line {line}"))
                                .collect::<Vec<_>>()
                                .join("\n"),
                        ),
                    ),
                })
            },
        );
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:one"]);
        // Release the parked startup preview and settle it.
        let _ = release.send(());
        for _ in 0..MAX_TEST_STEPS {
            harness.step();
            if harness.app.previewed_id.is_some() {
                break;
            }
        }
        // Shrink to 14 rows, then scroll to the NEW maximum: the final content line must be
        // visible there (D11's strong form — an in-range bound is weaker than the property;
        // the viewport bound makes the last line land at the pane bottom).
        harness.terminal.backend_mut().resize(100, 14);
        harness.step(); // render records the new viewport height
        for _ in 0..64 {
            harness.script(vec![ctrl_key(KeyCode::Char('d'))]);
            harness.step_until_script_drained();
        }
        let text = screen_rows_text(&harness.terminal, 4..12, 100);
        assert!(
            text.contains("line 49"),
            "the last content line must be visible at maximum scroll after shrinking: {text:?}"
        );
        let _ = release;
    }

    #[test]
    fn list_title_names_the_active_mode() {
        let (_filters_rx, executor) = recording_executor();
        let mut harness = TuiHarness::with_executor(executor).seeded(&["claude:keep"]);
        harness.wait_until_previewed("claude:keep");
        let rows = harness.session_rows();
        assert!(
            rows.contains("Sessions"),
            "the registered benchmark title survives"
        );
        assert!(
            rows.contains("recent"),
            "the empty query names its ordering"
        );

        harness.script(vec![key(KeyCode::Char('/')), key(KeyCode::Char('k'))]);
        harness.step_until_script_drained();
        assert!(
            harness.session_rows().contains("ranked"),
            "a typed query names its ordering"
        );
    }

    #[test]
    fn extreme_configured_steps_preserve_navigation_direction() {
        let mut config = Config::default();
        config.ui.list_page_rows = usize::MAX;
        config.ui.preview_page_rows = usize::MAX;
        let mut harness = TuiHarness::with_config(config, idle_executor()).seeded(&[
            "claude:one",
            "claude:two",
            "claude:three",
        ]);
        harness.app.selected = 1;
        harness.app.preview_line_count = usize::from(u16::MAX) + 100;
        harness.app.preview_viewport_rows = 10;
        harness.app.preview_scroll = 1;

        harness
            .app
            .handle_key(KeyEvent::new(KeyCode::PageDown, KeyModifiers::NONE));
        assert_eq!(
            harness.app.selected, 2,
            "PageDown must never wrap into an upward move"
        );
        harness
            .app
            .handle_key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL));
        assert_eq!(
            harness.app.preview_scroll,
            u16::MAX,
            "a huge forward preview step must saturate, never wrap backward"
        );
    }

    #[test]
    fn visible_session_rows_are_bounded_by_the_terminal_viewport() {
        assert_eq!(visible_session_range(10_000, 0, 18), 0..18);
        assert_eq!(visible_session_range(10_000, 5_000, 18), 4_991..5_009);
        assert_eq!(visible_session_range(10_000, 9_999, 18), 9_982..10_000);
        assert_eq!(visible_session_range(10_000, 4, 0), 0..0);
        assert!(visible_session_range(10_000, 5_000, 18).len() <= 18);
    }

    #[test]
    fn provider_label_width_is_capped_to_list_interior() {
        let mut config = Config::default();
        config.ui.provider_label_width = usize::MAX;
        let mut harness = TuiHarness::with_config(config, idle_executor()).seeded(&["claude:one"]);
        harness.step();
        assert!(
            screen_text(&harness.terminal)
                .lines()
                .all(|line| line.chars().count() <= 100),
            "configured formatting width must remain bounded by the rendered pane"
        );
    }

    #[test]
    fn ui_layout_fields_reach_the_render() {
        // A configured list-pane share of 70% moves the divider right of the default 45%,
        // and a provider-label width below the floor still renders every label whole.
        let mut config = Config::default();
        config.ui.list_pane_percent = 70;
        config.ui.provider_label_width = 2;
        let (_filters_rx, executor) = recording_executor();
        let mut harness = TuiHarness::with_config(config, executor).seeded(&["claude:keep"]);
        harness.wait_until_previewed("claude:keep");
        let rows = harness.session_rows();
        assert!(
            rows.contains("CLAUDE"),
            "the label renders whole despite the configured width of 2 (upward clamp)"
        );

        let divider = divider_column(&harness);
        let default_harness_divider = {
            let (_rx2, executor2) = recording_executor();
            let mut h2 = TuiHarness::with_executor(executor2).seeded(&["claude:keep"]);
            h2.wait_until_previewed("claude:keep");
            divider_column(&h2)
        };
        assert!(
            divider > default_harness_divider,
            "list_pane_percent=70 must widen the list pane (divider {divider} vs default {default_harness_divider})"
        );
    }

    fn divider_column(harness: &TuiHarness) -> u16 {
        let buffer = harness.terminal.backend().buffer();
        let area = buffer.area;
        (1..area.width - 1)
            .filter(|&x| {
                (4..area.height - 2)
                    .filter(|y| *y < area.height)
                    .filter(|y| *y < area.height)
                    .all(|y| buffer[(x, y)].symbol() == "│")
            })
            .max()
            .expect("the pane divider must exist")
    }
    // ---- step 6: [ui].preview_body_lines as the preview body budget (Decision 2a) ----

    fn harness_with_preview_source(
        budget: usize,
        transcript: &str,
        normalized_message_source: Option<&str>,
    ) -> (TuiHarness, Config) {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("index.db");
        let db = Db::open(&db_path).unwrap();
        let mut parsed = session("claude:long");
        parsed.session.cwd = Some("/fixture/project".to_string());
        parsed.transcript_text = transcript.to_string();
        if let Some(message_source) = normalized_message_source {
            parsed.messages = parse_turns(message_source)
                .into_iter()
                .enumerate()
                .map(|(seq, turn)| crate::models::Message {
                    seq: seq as i64,
                    role: match turn.role {
                        TurnRole::User => Role::User,
                        TurnRole::Assistant => Role::Assistant,
                    },
                    ts: None,
                    tool_name: None,
                    kind: crate::models::MessageKind::Conversation,
                    tool_call_id: None,
                    is_compaction: false,
                    content: turn.body.to_string(),
                    provenance: crate::models::MessageProvenance::default(),
                })
                .collect();
        }
        db.upsert_session(&parsed, 0, 0).unwrap();
        let runtime = db.execution_runtime();
        drop(db);
        let mut config = Config::default();
        config.ui.preview_body_lines = budget;
        config.index.db_path = Some(db_path.to_string_lossy().into_owned());
        let factory = db_backed_executor(
            config.clone(),
            EffectiveAccessScope::All,
            runtime,
            TerminalStyle::default(),
        );
        let harness = TuiHarness::with_factory(config.clone(), factory).own_external_fixture(dir);
        (harness, config)
    }

    fn harness_with_preview_budget(budget: usize, transcript: &str) -> (TuiHarness, Config) {
        harness_with_preview_source(budget, transcript, Some(transcript))
    }

    #[test]
    fn harness_drop_closes_databases_before_removing_owned_directories() {
        let internal;
        let external;
        {
            let (harness, _config) = harness_with_preview_budget(
                HISTORICAL_PREVIEW_BODY_LINES,
                &join_turns(&[("user", "hello")]),
            );
            internal = harness._dir.path().to_path_buf();
            external = harness
                .external_fixture
                .as_ref()
                .expect("production fixture is owned")
                .path()
                .to_path_buf();
        }
        assert!(!internal.exists());
        assert!(!external.exists());
    }

    #[test]
    fn default_preview_preserves_historical_single_section_limit() {
        let body = (0..40)
            .map(|line| format!("body line {line}"))
            .collect::<Vec<_>>()
            .join("\n");
        let transcript = join_turns(&[("user", &body)]);
        let summary =
            build_transcript_summary(&transcript, Config::default().ui.preview_body_lines);

        assert!(summary.contains("body line 7"));
        assert!(
            !summary.contains("body line 8"),
            "the default preview must preserve main's eight-line first-prompt limit: {summary}"
        );
    }

    #[test]
    fn transcript_preview_falls_back_when_normalized_messages_are_absent() {
        let transcript = join_turns(&[
            ("user", "first prompt"),
            ("assistant", "first reply"),
            ("user", "final prompt"),
            ("assistant", "final reply"),
        ]);
        let budget = Config::default().ui.preview_body_lines;
        let (mut harness, _config) = harness_with_preview_source(budget, &transcript, None);
        harness.start();
        for _ in 0..MAX_TEST_STEPS {
            harness.step();
            if harness.app.previewed_id.is_some() {
                break;
            }
        }

        assert_eq!(
            harness.app.preview,
            format!(
                "Session: claude:long\nCWD: /fixture/project\n\n{}",
                build_transcript_summary(&transcript, budget)
            ),
            "readable indexes with transcript rows but no normalized messages must retain the established preview"
        );
    }

    #[test]
    fn transcript_preview_excludes_noncanonical_normalized_message_content() {
        let transcript = join_turns(&[("user", "direct prompt"), ("assistant", "direct reply")]);
        let normalized = join_turns(&[
            (
                "user",
                "generated tool output that is absent from transcript",
            ),
            ("user", "direct prompt"),
            ("assistant", "direct reply"),
            (
                "user",
                "injected harness notice that is absent from transcript",
            ),
        ]);
        let budget = Config::default().ui.preview_body_lines;
        let (mut harness, _config) =
            harness_with_preview_source(budget, &transcript, Some(&normalized));
        harness.start();
        for _ in 0..MAX_TEST_STEPS {
            harness.step();
            if harness.app.previewed_id.is_some() {
                break;
            }
        }

        assert!(harness.app.preview.contains("Session: claude:long"));
        assert!(harness.app.preview.contains("CWD: /fixture/project"));
        assert!(harness.app.preview.contains("direct prompt"));
        assert!(harness.app.preview.contains("direct reply"));
        assert!(!harness.app.preview.contains("generated tool output"));
        assert!(!harness.app.preview.contains("injected harness notice"));
    }

    #[test]
    fn ui_config_field_reaches_the_rendered_preview() {
        // Eight turns with 30-line bodies: every section truncates under any plausible
        // budget, so the budget's effect on line counts is observable.
        let long_body: String = (0..30)
            .map(|line| format!("body line {line}"))
            .collect::<Vec<_>>()
            .join("\n");
        let transcript = join_turns(&[
            ("user", &long_body),
            ("assistant", &long_body),
            ("user", &long_body),
            ("assistant", &long_body),
            ("user", &long_body),
            ("assistant", &long_body),
            ("user", &long_body),
            ("assistant", &long_body),
        ]);

        let (mut tight, _config) = harness_with_preview_budget(1, &transcript);
        tight.start();
        for _ in 0..MAX_TEST_STEPS {
            tight.step();
            if tight.app.previewed_id.is_some() {
                break;
            }
        }
        let (mut roomy, _config) = harness_with_preview_budget(34, &transcript);
        roomy.start();
        for _ in 0..MAX_TEST_STEPS {
            roomy.step();
            if roomy.app.previewed_id.is_some() {
                break;
            }
        }

        assert!(
            tight.app.preview_line_count < roomy.app.preview_line_count,
            "[ui].preview_body_lines must reach the rendered preview: budget 1 produced {} lines, budget 34 produced {} (D8)",
            tight.app.preview_line_count,
            roomy.app.preview_line_count
        );
        assert!(
            tight.app.preview.contains("[…]"),
            "a tight budget must truncate sections, not erase bookends"
        );
        // Budget 34 reproduces the historical fixed layout (8/4/8/14 weights sum to 34).
        assert!(roomy.app.preview.contains("First prompt"));
        assert!(roomy.app.preview.contains("Final reply"));
        assert_eq!(
            roomy.app.preview,
            format!(
                "Session: claude:long\nCWD: /fixture/project\n\n{}",
                build_transcript_summary(&transcript, 34)
            ),
            "the borrowed canonical-transcript scan must preserve the established complete preview output"
        );
    }
}