croft-software 0.2.232

VSCode-style TUI written in Rust
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
use anyhow::{Context, Result};
use crossterm::{
    event::{
        self, DisableBracketedPaste, DisableFocusChange, DisableMouseCapture, EnableBracketedPaste,
        EnableFocusChange, EnableMouseCapture, Event, KeyCode, KeyEvent, KeyEventKind,
        KeyModifiers, KeyboardEnhancementFlags, MouseButton, MouseEvent, MouseEventKind,
        PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags,
    },
    execute,
    terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use ratatui::{
    Terminal,
    backend::CrosstermBackend,
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::Paragraph,
};
use std::collections::BTreeSet;
use std::io::{Stdout, stdout};
use std::path::{Path, PathBuf};
use std::time::Duration;

mod agents_collab;
mod bottom_panel;
mod click;
mod code_scanning;
mod codeql;
mod commands;
mod completion;
mod cursor_blink;
mod debugger;
mod diff_merge;
mod editor_input;
mod editor_layout;
mod editor_tabs;
mod explorer;
mod frame;
mod fs_watch;
mod git_worker;
mod host_integration;
mod hover;
mod inline_images;
mod key_input;
mod language_servers;
mod memory_report;
mod menus;
mod message_card;
mod mouse_input;
mod nav;
mod overlay;
mod perf_hud;
mod popups;
mod prompts;
mod remote_sessions;
mod review;
mod right_click;
mod runners;
mod save_restore;
mod scm;
mod screencast;
mod search;
mod settings;
pub(crate) mod sidebar_dwell;
mod terminal_panes;
mod terminal_tools;
mod tick;
mod viewers;
mod welcome;
mod welcome_screen;
mod workbench;
use agents_collab::*;
use bottom_panel::*;
use click::{ClickTracker, ModifiedClickTracker};
use code_scanning::*;
use codeql::*;
#[cfg(test)]
use commands::*;
use completion::*;
use cursor_blink::CursorBlink;
use debugger::*;
use editor_input::*;
use editor_tabs::*;
use explorer::*;
use frame::*;
use fs_watch::FsWatch;
use git_worker::GitWorker;
pub(crate) use host_integration::reattach_reassert_seq;
pub(crate) use host_integration::reset_host_colors_seq;
pub(crate) use host_integration::reset_session_bg_seq;
pub(crate) use host_integration::takeover_mode_seq;
use host_integration::*;
use hover::{HOVER_DELAY, HoverDwell};
use inline_images::*;
use key_input::*;
use language_servers::*;
use menus::*;
use nav::{NavHistory, NavLoc};
use overlay::OverlayManager;
use perf_hud::PerfHud;
use prompts::*;
use remote_sessions::*;
use review::*;
use runners::*;
use save_restore::*;
use scm::*;
use screencast::Screencast;
use search::*;
use settings::*;
use sidebar_dwell::{AUTO_HIDE_DWELL, SidebarDwell};
use terminal_panes::*;
#[cfg(test)]
use terminal_tools::*;
use tick::*;
use viewers::*;
use welcome::WelcomeState;
#[cfg(test)]
use welcome_screen::*;
pub(crate) use workbench::PanelAlignment;
pub(crate) use workbench::QuickInputPosition;
pub(crate) use workbench::SideBarPosition;
use workbench::*;

use crate::lsp::manager::CompletionTrigger;
use crate::lsp::manager::{diagnostics_by_source, diagnostics_source, diagnostics_store_key};
use crate::paths::croft_cache_dir;
use crate::proc::info::cwd_of_pid;
use crate::theme::BRAND_ORANGE;

use crate::dependencies::{Dependency, Ecosystem};
use crate::search::SearchRequest;
use crate::widgets::{
    dependencies::DependenciesPanel,
    editor::{Editor, EditorTabs},
    file_tree::FileTree,
    open_editors::{OpenEditorItem, OpenEditorsPanel},
    outline::OutlinePanel,
    remote::RemotePanel,
    run_debug::RunDebugPanel,
    search::{SearchPanel, SearchProgress},
    source_control::SourceControlPanel,
    terminal::{MouseAction, MouseButtonKind, MouseMods, PtyTerminal},
    timeline::TimelinePanel,
};

/// Which sidebar view is active in the left side panel.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SidebarView {
    Explorer,
    Search,
    SourceControl,
    Remote,
    RunDebug,
    Extensions,
    Testing,
    /// CodeQL (#578): databases, queries, query history, AST viewer.
    CodeQL,
}

/// Which Explorer sub-views are shown, toggled from the ⋯ menu. Wraps the
/// persisted [`crate::prefs::ExplorerViewsPrefs`] so the app can query/flip a
/// view by its [`ExplorerView`] without touching the serde struct's fields
/// directly.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub struct ExplorerViewVisibility {
    flags: crate::prefs::ExplorerViewsPrefs,
}

/// One in-flight `textDocument/selectionRange` request (#254): the
/// Expand Selection gesture waiting on its chains. `positions` snapshots
/// the exact cursors the request was fired for, so a reply (or a reuse
/// attempt) after ANY caret movement is recognised as stale even though
/// `edit_seq` did not move.
struct PendingSelectionRanges {
    id: u64,
    path: PathBuf,
    seq: u64,
    presses: usize,
    positions: Vec<(u32, u32)>,
}

/// Where a row of Bookmarks: View Bookmarks points: the tab holding the
/// mark (its editor group by depth-first index, then its index in that
/// group), the file it shows and the 1-based line.
#[derive(Clone, Debug)]
struct BookmarkTarget {
    group: usize,
    tab: usize,
    path: PathBuf,
    line: usize,
}

/// Recording a new shortcut in the Keyboard Shortcuts editor (#612): one
/// value for both phases, so the second replaces the first and the two can
/// never be pending at once.
#[derive(Clone, Debug)]
enum ShortcutCapture {
    /// The next keystroke is `cmd`'s new chord, if it comes within
    /// [`SHORTCUT_RECORD_WINDOW`] of `since`.
    Recording {
        cmd: crate::widgets::command_palette::Command,
        since: std::time::Instant,
    },
    /// The recorded chord is taken (#1482): Enter gives it to `cmd`, taking
    /// it from every command in `owners` (a built-in chord can be shared by
    /// several); any other key keeps both shortcuts as they were.
    Confirming {
        cmd: crate::widgets::command_palette::Command,
        chord: KeyEvent,
        owners: Vec<crate::widgets::command_palette::Command>,
    },
}

/// Pre-Zen visibility snapshot, restored verbatim when Zen Mode is toggled
/// off so the workspace returns to exactly the chrome it had before.
#[derive(Clone, Copy, Debug, Default)]
struct PreZenLayout {
    activity_bar: bool,
    status_bar: bool,
    side_bar: bool,
    secondary_side_bar: bool,
    panel: bool,
}

/// Hit-test rects for the three layout-control icons (Toggle Primary Side
/// Bar, Toggle Panel, Customize Layout) rendered at the top-right of the
/// editor tab strip and the welcome screen — croft's analog of VS Code's
/// title-bar layout controls.
#[derive(Clone, Copy, Default)]
struct LayoutIconAreas {
    toggle_side_bar: Rect,
    toggle_panel: Rect,
    customize: Rect,
}

/// Where each clickable item of the status bar was drawn this frame. Empty
/// when the bar is hidden or the item does not fit.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct StatusHitAreas {
    /// The status message's own cells; a click opens the Messages output
    /// channel, where every message is kept in full (#1609).
    message: Rect,
    /// The problem counts; a click opens PROBLEMS.
    diagnostics: Rect,
    /// The running CodeQL query's chip; a click cancels it (#578).
    codeql: Rect,
    /// The croft update pills ("Update ready", the drift rebuild hint); a
    /// click does what the updater chord does (#865).
    update: Rect,
    /// The "Update Available" pill under the update card; a click opens or
    /// folds the card.
    offer: Rect,
    /// The `Ln X, Col Y` readout. A click opens the Go to Line prompt: a
    /// readout becomes a control, as in VS Code.
    position: Rect,
    /// The indentation, encoding, line-ending and language items; a click
    /// opens the picker that changes each.
    indent: Rect,
    encoding: Rect,
    eol: Rect,
    language: Rect,
    /// The agents chip; empty when no agent is seated.
    agents: Rect,
}

/// Where each tab and button of the panel was drawn this frame. The lists are
/// in lock-step with `terminals`: a pane that is hidden or has no such button
/// holds an empty rect, so a button's position names its terminal.
#[derive(Debug, Default, PartialEq)]
struct PanelHitAreas {
    /// The "PROBLEMS", "OUTPUT", "PORTS", "CAPTURES" and "TERMINAL" tabs.
    problems_tab: Rect,
    output_tab: Rect,
    ports_tab: Rect,
    captures_tab: Rect,
    terminal_tab: Rect,
    /// The panel's maximize / restore button (#1980), the click form of
    /// `Ctrl+Shift+J`: grow the panel over the editor, or give the editor its
    /// rows back. Rightmost on the panel's tab row, on every tab, since it
    /// sizes the whole panel.
    maximize_button: Rect,
    /// The focus-mode (`⛶`) button at the right of the panel's tab row
    /// (#1980): it maximizes the ACTIVE terminal across the panel, or restores
    /// the split. One button for the panel rather than one per pane, because
    /// the mode rearranges every pane at once. Empty with fewer than two
    /// terminals or off the TERMINAL tab.
    focus_button: Rect,
    /// The "[+]" buttons. Empty when every pane is too narrow for the label.
    add_buttons: Vec<Rect>,
    /// The profile-caret (`⌄`) buttons. Clicking one opens the profile picker.
    profile_buttons: Vec<Rect>,
    /// The "[-]" buttons. Empty when only one terminal is open: closing the
    /// last one would leave the pane empty, which croft forbids.
    close_buttons: Vec<Rect>,
    /// The pane name pills at each pane's top-left. A press on one starts a
    /// drag-reorder. Empty with one terminal.
    pane_labels: Vec<Rect>,
    /// The collapse (`‹`) buttons (#313). A pane that is already collapsed,
    /// too narrow for the glyph, or maximized holds an empty rect.
    collapse_buttons: Vec<Rect>,
    /// The one-column strips collapsed panes leave behind (#313). Clicking
    /// anywhere down a strip gives that pane its width back; expanded panes
    /// hold an empty rect. The WHOLE strip is the target, not just the
    /// chevron: one column is already a small thing to hit without also
    /// demanding the right row.
    collapsed_strips: Vec<Rect>,
    /// The maximize-mode rail's rows down the panel's right edge. Clicking one
    /// hands that terminal the maximized pane. Empty outside maximize mode.
    rail_rows: Vec<Rect>,
    /// The maximize rail's own column, so the wheel can scroll it.
    rail: Rect,
}

impl PanelHitAreas {
    /// Empty every area, keeping the lists' room so a frame allocates
    /// nothing. The fields are named without `..`, so a new one is a compile
    /// error here until it is reset too.
    fn reset(&mut self) {
        let Self {
            problems_tab,
            output_tab,
            ports_tab,
            captures_tab,
            terminal_tab,
            maximize_button,
            focus_button,
            add_buttons,
            profile_buttons,
            close_buttons,
            pane_labels,
            collapse_buttons,
            collapsed_strips,
            rail_rows,
            rail,
        } = self;
        for area in [
            problems_tab,
            output_tab,
            ports_tab,
            captures_tab,
            terminal_tab,
            maximize_button,
            focus_button,
            rail,
        ] {
            *area = Rect::default();
        }
        for list in [
            add_buttons,
            profile_buttons,
            close_buttons,
            pane_labels,
            collapse_buttons,
            collapsed_strips,
            rail_rows,
        ] {
            list.clear();
        }
    }
}

/// Result of [`compute_chrome_layout`]: the rectangles for each workspace
/// region within the horizontal `main` band. Hidden regions are `None` and
/// consume no width.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
struct ChromeLayout {
    activity: Option<Rect>,
    primary_side: Option<Rect>,
    secondary_side: Option<Rect>,
    /// The editor + terminal column (always present).
    right: Rect,
    /// Column of the seam between the editor and the primary side bar, where a
    /// resize drag starts. `None` when the primary side bar is hidden.
    sidebar_splitter_x: Option<u16>,
}

/// Centered popup geometry for frames of any size. Min bounds cap at the
/// frame because Ord::clamp panics on min > max, and a frame with no
/// cells on an axis gets no popup at all.
fn fallback_popup_rect(full: Rect, max_w_cap: u16) -> Rect {
    if full.width == 0 || full.height == 0 {
        return Rect {
            width: 0,
            height: 0,
            ..full
        };
    }
    let max_w = max_w_cap.min(full.width);
    let max_h = full.height;
    let width = (full.width.saturating_mul(7) / 10).clamp(40.min(max_w), max_w);
    let height = (full.height.saturating_mul(6) / 10).clamp(10.min(max_h), max_h);
    Rect {
        x: full.x + (full.width.saturating_sub(width)) / 2,
        y: full.y + (full.height.saturating_sub(height)) / 4,
        width,
        height,
    }
}

#[derive(Default, Clone, Copy)]
struct SidebarAreas {
    /// Where each activity-bar icon sits. Clicking the sponsor heart opens
    /// croft's GitHub Sponsors page; the settings gear, bottom-anchored like
    /// VS Code's "Manage" button, opens the settings menu.
    activity_icons: ActivityBlocks,
}

/// One activity-bar icon's pre-encoded pictures, one per state, so a hover
/// only swaps which one is sent and never bakes again.
#[cfg_attr(test, derive(Default))]
pub struct ActivityIconImages {
    /// Shown while the icon is selected: its view is open, or for the
    /// settings gear its menu is, so the button reads as pressed. `None` for
    /// the sponsor heart, which opens a web page and is never selected.
    active: Option<String>,
    /// At rest.
    inactive: String,
    /// Brightened while the pointer rests on it.
    hovered: String,
}

/// Pre-encoded iTerm2 OSC-1337 inline-image escape sequences for each icon
/// state. Encoded once in `App::init_graphics` (no PNG re-encoding per
/// frame) and rewritten under the activity-bar block after every ratatui
/// frame draw, since ratatui's bg-clear overdraws the image.
#[cfg_attr(test, derive(Default))]
pub struct ActivityBarImages {
    /// Each activity-bar icon's pictures, indexed by [`ActivityIcon`].
    icons: [ActivityIconImages; ActivityIcon::ALL.len()],
    /// Customize Layout toolbar icons (top-right of the editor / welcome),
    /// baked OPAQUE at the 4×2-cell toolbar box so they paint in front of the
    /// container's top line. The side-bar / panel icons carry an on/off pair
    /// (filled vs. hollow) matching their visibility; every icon also has a
    /// brighter `_hover` variant emitted while the pointer rests on it, exactly
    /// like the activity-bar icons.
    layout_sidebar_on: String,
    layout_sidebar_on_hover: String,
    layout_sidebar_off: String,
    layout_sidebar_off_hover: String,
    layout_panel_on: String,
    layout_panel_on_hover: String,
    layout_panel_off: String,
    layout_panel_off_hover: String,
    layout_customize: String,
    layout_customize_hover: String,
}

/// Everything [`App::bake_chrome_images`] needs, resolved on the UI thread so
/// the bake can run on one of its own.
#[derive(Clone, Copy)]
struct ChromeBakeInput {
    protocol: crate::iterm2_inline::InlineImageProtocol,
    cell: (u32, u32),
    is_tmux: bool,
    icon_bg: ::image::Rgba<u8>,
    icon_ink: crate::iterm2_inline::IconInk,
    sponsor_ink: crate::iterm2_inline::IconInk,
}

/// The pictures [`App::init_graphics`] bakes off the UI thread; `None` for
/// any that failed to bake.
struct ChromeImages {
    activity: Option<ActivityBarImages>,
    run_debug: Option<String>,
    ssh: Option<String>,
}

/// What the welcome wordmark is baked for. A picture baked for any other (a
/// relayout, a theme switch, a font zoom since) is stale.
#[derive(Clone, Copy, PartialEq)]
struct WelcomeBakeKey {
    layout: WelcomeLayout,
    cell: (u32, u32),
    bg: ::image::Rgba<u8>,
    light: bool,
    protocol: crate::iterm2_inline::InlineImageProtocol,
}

/// Single source of truth for the application's user-facing name.
pub const APP_NAME: &str = "croft";

/// The status bar's three text tones, so hierarchy comes from brightness
/// rather than from dividers or a colour per item: `text` for the values you
/// read (branch, line and column, language), `muted` for settings and
/// commentary, and `dim` for labels and for counts that are zero.
#[derive(Clone, Copy)]
struct StatusTones {
    text: Color,
    muted: Color,
    dim: Color,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Pane {
    Tree,
    Editor,
    Terminal,
}

/// Which tab the bottom panel group shows. The panel is the "terminal group"
/// VS Code-style: a tab strip over a shared region. `Terminal` shows the live
/// shell(s); `Problems` shows aggregated workspace diagnostics; `Output` is the
/// read-only per-channel log viewer.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum BottomPanelTab {
    Terminal,
    Problems,
    Output,
    Ports,
    Captures,
}

impl BottomPanelTab {
    /// Every tab, in the strip's left-to-right order (VS Code's panel-group
    /// order). The strip paints from this list, so a tab cannot be
    /// clickable without being in it.
    const ALL: [BottomPanelTab; 5] = [
        BottomPanelTab::Problems,
        BottomPanelTab::Output,
        BottomPanelTab::Terminal,
        BottomPanelTab::Ports,
        BottomPanelTab::Captures,
    ];

    /// The tab's strip label in English, the catalog key it is drawn
    /// through (#1583).
    fn label(self) -> &'static str {
        match self {
            BottomPanelTab::Problems => "PROBLEMS",
            BottomPanelTab::Output => "OUTPUT",
            BottomPanelTab::Terminal => "TERMINAL",
            BottomPanelTab::Ports => "PORTS",
            BottomPanelTab::Captures => "CAPTURES",
        }
    }

    /// The palette command that shows this tab (#852). No catch-all: a new
    /// tab fails to compile until it names one, so no tab ships reachable
    /// only by a click on the strip.
    fn show_command(self) -> crate::widgets::command_palette::Command {
        use crate::widgets::command_palette::Command;
        match self {
            BottomPanelTab::Terminal => Command::FocusTerminal,
            BottomPanelTab::Problems => Command::ShowProblems,
            BottomPanelTab::Output => Command::ShowOutput,
            BottomPanelTab::Ports => Command::ShowPorts,
            BottomPanelTab::Captures => Command::ShowCaptures,
        }
    }
}

/// A transient, click-only notification that a browsable port just appeared in
/// the terminal. It never captures keystrokes — the terminal owns those — so
/// the user clicks an action or lets it auto-dismiss. The forward actions only
/// apply on a remote session (a local port is already reachable).
struct PortToast {
    port: u16,
    process: Option<String>,
    origin_remote: bool,
    shown_at: std::time::Instant,
    /// Action button hit rects, recomputed each render.
    buttons: Vec<(Rect, PortToastAction)>,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum CreateKind {
    File,
    Folder,
}

/// The bottom-left "a newer croft is available" popup (#333), a card in
/// the manner of Ghostty's (`widgets/update_popup.rs`) over its status-bar
/// pill. Click-only like the port toast: it never takes a keystroke from
/// the editor or a terminal. It is down while the staged build runs (the
/// status bar shows that, #1852) and comes back with `ready` set, offering
/// Relaunch instead of Update, once the build has landed.
struct UpdateToast {
    release: crate::update_check::Release,
    ready: bool,
    /// Why the last Update did not stage the version (#1844): said in the
    /// popup, which then offers Retry.
    failed: Option<&'static str>,
    /// False once Later folded the card into its status-bar pill, which
    /// opens it again.
    open: bool,
    /// Action hit rects, recomputed each render.
    buttons: Vec<(Rect, UpdateToastAction)>,
    /// What the card painted in each of `buttons`, to tell whether a later
    /// overlay covered it before the frame went out.
    painted: Vec<Vec<ratatui::buffer::Cell>>,
}

/// A fenced block about to run (#353), parked behind the confirm popup.
#[derive(Clone, Debug)]
struct PendingRunBlock {
    pane_name: String,
    cwd: PathBuf,
    command: String,
    /// What the block runs, shown whole in the popup: the block as written,
    /// or a transcript's commands without their prompts (#1473). The fence
    /// was refused if it carried control characters, so it renders as typed.
    code: String,
    /// Turns the popup red: the block looks destructive or said `{confirm}`.
    destructive: bool,
    /// The document this block came from, for `{persist}`'s write-back.
    doc: Option<PathBuf>,
    /// The fence opener's SOURCE line, which keys the captured output.
    block_line: usize,
    /// `{persist}`: write the capture back into the document.
    persist: bool,
    /// `{timeout=N}`: stop CAPTURING after N seconds. Never stops the
    /// process; the block keeps running in its pane.
    capture_timeout: Option<std::time::Duration>,
}

/// A block whose output croft is waiting for (#354).
///
/// Armed when the block is typed into its pane and settled by
/// [`App::settle_block_captures`] when the shell reports the command
/// finished.
///
/// IDENTITY, NOT POSITION (#440, landed as #441). `after` is the id of the
/// last command the pane had already finished when the block was typed, so
/// this block's command is the first decoration whose id is greater. Ids
/// are monotonic and the collector never renumbers them, so an evicted
/// command reads as GONE rather than as its neighbour - which is what makes
/// the `{persist}` write safe rather than merely guarded.
///
/// The previous shape indexed the decoration list, which is a sliding
/// window: an eviction shifted every index down and index `seen` came to
/// name a stranger. That was narrowed with a high-water sample and could
/// not be closed by one, because any length-derived signal samples a value
/// that can move twice between observations. An id cannot move at all.
struct PendingCapture {
    pane_name: String,
    doc: Option<PathBuf>,
    block_line: usize,
    /// Ids at or below this belong to commands that finished BEFORE the
    /// block was typed.
    ///
    /// `None` is a sentinel, not a special case: ids start at 1, so `None`
    /// and `Some(0)` select identically and a fresh pane needs no separate
    /// handling.
    ///
    /// THE RESIDUAL IS IN THE SELECTION, NOT HERE. Settling takes the
    /// EARLIEST qualifying id, which is this block's command only if
    /// nothing else finished in the gap between arming and the keystrokes
    /// reaching the shell. A command completing in that gap is taken
    /// instead, on a pane with a full history exactly as on a fresh one -
    /// `after` does not enter into it. The window is much smaller than the
    /// eviction one it replaced, since a foreign command must now COMPLETE
    /// in the gap rather than merely exist, and the outcome self-corrects
    /// on the next run. Closing it properly means arming after the
    /// keystrokes are known to have landed, which is a different change.
    after: Option<u64>,
    started: std::time::Instant,
    timeout: Option<std::time::Duration>,
    persist: bool,
}

#[derive(Clone, PartialEq, Eq, Debug)]
enum MenuAction {
    /// A row of Source Control's `⋯` actions menu (#871).
    Scm(crate::widgets::scm_menu::ScmAction),
    /// A row of the Commit button's `▾` dropdown (#871).
    Commit(crate::widgets::source_control::CommitMenuItem),
    Create(CreateKind),
    /// Run a palette command: a row of the user's menu (`menu.json`).
    RunCommand(crate::widgets::command_palette::Command),
    /// Move every path in `paths` to the OS trash (recoverable). One entry
    /// for a single right-click, more when a multi-selection is active.
    Delete {
        paths: Vec<PathBuf>,
    },
    /// Open the rename prompt pre-filled with the entry's current name.
    Rename(PathBuf),
    /// Cut the listed paths to the explorer clipboard for later Paste.
    Cut(Vec<PathBuf>),
    /// Copy the listed paths to the explorer clipboard for later Paste.
    Copy(Vec<PathBuf>),
    /// Paste the explorer clipboard's payload into `target_dir`.
    Paste(PathBuf),
    /// Stash this path as the "compare anchor" so the next file the user
    /// right-clicks can be diffed against it.
    SelectForCompare(PathBuf),
    /// Open a side-by-side diff between the previously-selected anchor and
    /// the file the user just right-clicked.
    CompareWithSelected {
        anchor: PathBuf,
        other: PathBuf,
    },
    /// Re-root the workspace at the right-clicked folder. Folder-only -
    /// files can't be roots. Used to dive into a nested git repo without
    /// relaunching croft.
    MakeRoot(PathBuf),
    /// Add another folder to the workspace (multi-root, #147): opens the
    /// fuzzy directory picker in add-folder mode.
    AddWorkspaceFolder,
    /// Drop a SECONDARY workspace root from the folder set (#147).
    RemoveWorkspaceFolder(PathBuf),
    /// Copy the absolute path of the right-clicked editor tab to the system
    /// clipboard (VS Code's "Copy Path"). Available wherever the tab is backed
    /// by a real on-disk path, local or remote.
    CopyTabPath(PathBuf),
    /// Copy the right-clicked editor tab's path relative to the workspace root
    /// (VS Code's "Copy Relative Path"). Falls back to the absolute path when
    /// the file lives outside the workspace root. Local or remote.
    CopyTabRelativePath(PathBuf),
    /// Reveal the right-clicked editor tab's file in the Explorer tree:
    /// expand its parent folders, select its row, and focus the Explorer.
    /// Mirrors VS Code's "Reveal in Explorer View". Works local and remote.
    RevealInExplorer(PathBuf),
    /// Reveal the path in macOS Finder via `open -R`. Local-macOS only:
    /// on a remote SSH session croft runs on the headless host where no
    /// Finder exists, so the menu omits this entry entirely rather than
    /// offering a no-op.
    RevealInFinder(PathBuf),
    /// Open the file in an extension-contributed viewer (#465): the viewer's
    /// id and the file.
    OpenInViewer(String, PathBuf),
    /// Close the editor tab at `idx`. Mirrors clicking the tab's `×`
    /// glyph; surfaces in the tab-strip right-click menu so the four
    /// close actions (Close / Others / Right / All) live in one place.
    CloseTab(usize),
    /// Close every editor tab whose index ≠ `keep_idx`.
    CloseOtherTabs(usize),
    /// Close every editor tab whose index > `from_idx`.
    CloseTabsToRight(usize),
    /// Close every editor tab and reset the pane to a blank state.
    CloseAllTabs,
    /// Close every saved (non-dirty) editor tab, keeping any with unsaved
    /// changes. Mirrors VS Code's "Close Saved".
    CloseSavedTabs,
    /// Promote the preview tab at `idx` to a permanent tab so a later
    /// single-click open won't replace it (VS Code's "Keep Open"). Only
    /// surfaced for a tab that is currently the replaceable preview slot.
    KeepTabOpen(usize),
    /// Toggle the pinned state of the tab at `idx` (VS Code's "Pin" / "Unpin").
    /// Pinned tabs stay leftmost and survive Close Others / Close to the Right.
    ToggleTabPin(usize),
    /// Split the editor side by side, the new (focused) duplicate to the RIGHT
    /// (VS Code "Split Right"). The default split.
    SplitEditor,
    /// Split side by side, the new focused duplicate to the LEFT ("Split Left").
    SplitEditorLeft,
    /// Split stacked, the new focused duplicate ABOVE ("Split Up").
    SplitEditorUp,
    /// Split stacked, the new focused duplicate BELOW ("Split Down").
    SplitEditorDown,
    /// Move the active editor into the group above ("Move Above").
    MoveEditorUp,
    /// Move the active editor into the group below ("Move Below").
    MoveEditorDown,
    /// Move the active editor into the group to the left ("Move Left").
    MoveEditorLeft,
    /// Move the active editor into the group to the right ("Move Right").
    MoveEditorRight,
    /// Open a second view of the active file beside it ("Split in Group").
    SplitInGroup,
    /// Move keyboard focus to the left editor group (while split).
    FocusGroupLeft,
    /// Move keyboard focus to the right editor group (while split).
    FocusGroupRight,
    /// VS Code "Move into New Window": detach the tab at `idx` into a
    /// full-screen, chrome-free editor window (Esc returns it). The tab leaves
    /// its group, which collapses if it was the group's last tab.
    MoveIntoNewWindow(usize),
    /// VS Code "Copy into New Window" (⌘K O): open a duplicate view of the tab
    /// at `idx` in a full-screen editor window, leaving the original in place.
    CopyIntoNewWindow(usize),
    /// Editor body: LSP "Rename Symbol" of the identifier at buffer `(row,
    /// col)`. Captured at right-click so the later dispatch targets the same
    /// symbol the user clicked.
    RenameSymbolAt {
        row: usize,
        col: usize,
    },
    /// Editor body: "Copy Permalink" / "Open on Remote" (#1610) for the
    /// selected lines, else the caret's.
    CopyPermalink,
    OpenOnRemote,
    /// Editor body: "Change All Occurrences" of the word at buffer `(row,
    /// col)` (multi-cursor select within the current file).
    ChangeAllOccurrencesAt {
        row: usize,
        col: usize,
    },
    /// Editor body: LSP "Go to Definition" of the identifier at buffer
    /// `(row, col)`.
    GoToDefinitionAt {
        row: usize,
        col: usize,
    },
    /// Editor body: LSP "Go to Declaration" of the identifier at buffer
    /// `(row, col)`. Distinct from definition for languages that separate the
    /// two (C/C++ header prototype vs `.c` body, TypeScript `.d.ts` vs source).
    GoToDeclarationAt {
        row: usize,
        col: usize,
    },
    /// Editor body: LSP "Go to Type Definition" of the identifier at buffer
    /// `(row, col)`. Jumps to where the *type* of the expression under the
    /// cursor is defined (e.g. the `User` struct for `let u = get_user()`),
    /// as opposed to where the symbol itself is defined.
    GoToTypeDefinitionAt {
        row: usize,
        col: usize,
    },
    /// Editor body: LSP "Go to Implementations" of the abstraction at buffer
    /// `(row, col)`. Walks down from a trait / interface / abstract method to
    /// the concrete implementors; the server often returns many, in which case
    /// the results are offered as a picker built from `GoToLocation` items.
    GoToImplementationAt {
        row: usize,
        col: usize,
    },
    /// Editor body: LSP "Go to References" of the symbol at buffer `(row, col)`.
    /// Finds every use of the symbol project-wide (declaration included, like VS
    /// Code); the server almost always returns many, which are offered as a
    /// picker built from `GoToLocation` items.
    GoToReferencesAt {
        row: usize,
        col: usize,
    },
    /// Return to the location before the last navigation jump (#31).
    NavigateBack,
    NavigateForward,
    /// Editor body: LSP "Quick Fix" of the position at buffer `(row, col)`.
    QuickFixAt {
        row: usize,
        col: usize,
    },
    /// A breadcrumb picker's file (#1608): open it as Quick Open would.
    OpenCrumbFile(PathBuf),
    /// A breadcrumb picker's folder (#1608): list it in the picker of
    /// `crumb`, where the listing started.
    BrowseCrumbFolder {
        dir: PathBuf,
        crumb: usize,
    },
    /// Jump straight to an already-resolved location (LSP line/character). Used
    /// for the entries of the multi-result "Go to Implementations" / "Go to
    /// References" pickers, where each row is one location; selecting it records
    /// nav history and opens the file at that position.
    GoToLocation {
        path: PathBuf,
        line: u32,
        col: u32,
    },
    /// Editor gutter: toggle a bookmark on 1-based `line`. The gutter menu has
    /// no cursor to act on, so the line rides the action; the label reads
    /// "Toggle Bookmark" either way, as the Bookmarks extension's does.
    ToggleBookmarkAt {
        line: usize,
    },
    /// Editor gutter: jump to the next bookmark in the open file, wrapping at
    /// the end. Not line-scoped, so it rides the menu as a command.
    NextBookmark,
    /// Editor gutter: mirror of [`MenuAction::NextBookmark`] walking upwards.
    PreviousBookmark,
    /// Editor gutter: toggle a breakpoint on 1-based `line`. Built from a
    /// glyph-margin right-click; the label reads "Add Breakpoint" or "Remove
    /// Breakpoint" depending on whether one already sits on that line, but both
    /// resolve to the same toggle. Mirrors VS Code's glyph-margin menu.
    ToggleBreakpointAt {
        line: usize,
    },
    /// Editor gutter: open the breakpoint-condition editor for 1-based `line`,
    /// creating the breakpoint if absent. VS Code's "Add Conditional
    /// Breakpoint" / "Edit Condition".
    EditBreakpointConditionAt {
        line: usize,
    },
    /// Editor gutter: open the logpoint editor for 1-based `line`, creating
    /// the breakpoint if absent. VS Code's "Add Logpoint" / "Edit Logpoint".
    EditLogpointAt {
        line: usize,
    },
    /// Editor gutter: ask the resident navigator about 0-based `line`
    /// (opens the ask box scoped to that line).
    AskNavigatorAt {
        line: usize,
    },
    /// Editor body: ask the navigator about the selected 0-based inclusive
    /// line span (the selection's text rides the turn).
    AskNavigatorSelection {
        start: usize,
        end: usize,
    },
    /// CAPTURES row: ask the navigator about the selected captured line,
    /// with the pane rows around it (#372).
    AskNavigatorAboutCapture,
    /// VARIABLES or WATCH row: copy its value (#1546). With `expression`
    /// and an adapter that offers it, the full value comes from an
    /// `evaluate` in the `clipboard` context, which is not cut as the
    /// row's `value` may be; otherwise `value` itself is copied.
    CopyDebugValue {
        expression: Option<String>,
        value: String,
    },
    /// VARIABLES row: copy its `evaluateName`; WATCH row: its expression.
    CopyDebugExpression(String),
    /// VARIABLES row: watch its `evaluateName`.
    AddDebugWatch(String),
    /// WATCH row: stop watching expression `index`.
    RemoveDebugWatch(usize),
    /// CALL STACK row: copy the whole stack, a frame a line.
    CopyCallStack,
    /// PROBLEMS row: have the navigator fix this diagnostic as a streamed,
    /// cancellable edit (#374).
    FixProblemWithNavigator {
        path: PathBuf,
        item: crate::widgets::problems::ProblemItem,
    },
    /// OUTLINE row: open lines `first..=last` of the Outline's file as a
    /// symbol tab named `name` (#369).
    OpenSymbolTab {
        path: PathBuf,
        name: String,
        first: usize,
        last: usize,
    },
    /// OUTLINE row: jump to the symbol, as a left click does.
    GoToOutlineSymbol {
        path: PathBuf,
        line: u32,
        col: u32,
    },
    /// Editor body: one level of the call hierarchy for the symbol at buffer
    /// `(row, col)` — callers when `incoming`, callees otherwise.
    ShowCallsAt {
        row: usize,
        col: usize,
        incoming: bool,
    },
    /// Settings gear → "Color Theme": replace the gear menu with the theme
    /// picker (the list of themes with a check on the active one).
    OpenThemePicker,
    /// Theme picker entry: switch the IDE color theme to `theme`, persist it,
    /// re-emit the session background, and re-bake the icon images.
    SetTheme(crate::theme::Theme),
    /// Explorer "Views and More Actions" (⋯) entry: flip the visibility of one
    /// stacked sub-view (Open Editors / Folders / Outline / Timeline / Rust
    /// Dependencies) and persist the choice.
    ToggleExplorerView(ExplorerView),
    /// Customize Layout: re-open the Customize Layout popup (so a toggle the
    /// user just flipped stays visible for the next one, like VS Code).
    OpenCustomizeLayout,
    /// Customize Layout: show/hide the left activity bar.
    ToggleActivityBar,
    /// Customize Layout: show/hide the primary side bar (same as ⌘B).
    ToggleSideBar,
    ToggleAutoHideSideBar,
    /// Customize Layout: show/hide the secondary side bar (the Outline).
    ToggleSecondarySideBar,
    /// Customize Layout: show/hide the bottom panel (the terminal, same as ⌃J).
    TogglePanel,
    /// Customize Layout: show/hide the bottom status bar.
    ToggleStatusBar,
    /// Customize Layout: dock the primary side bar to `position`.
    SetSideBarPosition(SideBarPosition),
    /// Customize Layout: set the bottom panel's horizontal alignment.
    SetPanelAlignment(PanelAlignment),
    /// Customize Layout: anchor the quick input at `position`.
    SetQuickInputPosition(QuickInputPosition),
    /// Customize Layout: toggle Zen Mode (hide all chrome / restore).
    ToggleZenMode,
    /// Customize Layout: centre the editor (#1585).
    ToggleCenteredLayout,
    /// Run the active editor's code lens at this index (#608).
    RunCodeLens(usize),
    /// Terminal pane right-click: rename the pane at `idx`.
    RenameTerminal(usize),
    /// Terminal pane right-click: clear the pane at `idx`'s screen + scrollback.
    ClearTerminal(usize),
    /// Terminal pane right-click: maximize the pane at `idx` across the
    /// panel (or restore the even split when already maximized).
    ToggleMaximizeTerminal(usize),
    /// Terminal pane right-click: collapse the pane at `idx` to a strip, or
    /// expand it again (#313).
    ToggleCollapseTerminal(usize),
    /// Terminal pane right-click: copy the pane's selection to the clipboard.
    /// Offered only while the pane HAS one, so the entry never copies nothing.
    TerminalCopySelection,
    /// Terminal pane right-click: paste the host clipboard into the pane.
    TerminalPaste,
    /// Editor right-click (#1117): the editor's own Cut, Copy and Paste, as
    /// its keys do them. Cut and Copy are offered only with a selection.
    EditorCut,
    EditorCopy,
    EditorPaste,
    /// Editor right-click (#1117): open the Command Palette.
    ShowCommandPalette,
    /// Terminal pane right-click: enter quick-select hint mode on the pane.
    TerminalQuickSelect,
    /// Terminal pane right-click: enter copy mode (keyboard selection with
    /// vi motions) on the pane.
    TerminalCopyMode,
    /// Terminal pane right-click: open the durable command-history search.
    TerminalCommandHistory,
    /// Terminal pane right-click: dump the pane's scrollback into a scratch
    /// editor tab.
    OpenScrollbackInEditor,
    /// Terminal pane right-click: toggle broadcast input (keystrokes go to
    /// every pane at once; enabling routes through the confirm popup).
    ToggleBroadcastInput,
    /// Terminal pane right-click while broadcasting: toggle whether the
    /// active pane receives the mirrored input.
    ToggleBroadcastExclusion,
    /// Terminal pane right-click: restore the most recently closed pane
    /// while it is still inside the undo-close grace window.
    UndoCloseTerminal,
    /// Terminal pane right-click: open another terminal next to the active
    /// one, as `Cmd+T` does (#1598).
    SplitTerminal,
    /// Terminal pane right-click: close the pane at `idx`, as `Cmd+W` does
    /// for the active one (#1598).
    KillTerminal(usize),
    /// Command decoration menu: copy the command's output span to the
    /// clipboard (pane `idx`, the clicked decoration embedded).
    CopyCommandOutput(usize, crate::widgets::terminal::CommandDecoration),
    /// Command decoration menu: select the command's output span in the pane.
    SelectCommandOutput(usize, crate::widgets::terminal::CommandDecoration),
    /// Command decoration menu: type the command at the shell again.
    RerunCommand(usize, crate::widgets::terminal::CommandDecoration),
    /// Terminal profile dropdown (`⌄`): open a new pane running this shell path.
    NewTerminalWithProfile(String),
    /// Minimap right-click menu: show/hide the editor minimap.
    ToggleMinimap,
    /// Minimap right-click menu: move the strip to the given editor edge.
    SetMinimapSide(MinimapSide),
    /// Status-bar language picker: set the active buffer's language mode.
    SetLanguage(Option<crate::highlight::LangKind>),
    /// Status-bar EOL picker: switch the buffer's line-ending style.
    SetLineEnding(crate::widgets::editor::LineEnding),
    /// Status-bar encoding picker: reopen the file decoded with this encoding.
    SetEncoding(&'static encoding_rs::Encoding),
    /// Status-bar indentation picker: pin spaces-vs-tabs and width for newly
    /// typed indentation.
    SetIndentStyle(crate::widgets::editor::IndentStyle),
    /// Status-bar indentation picker: rewrite existing leading indentation.
    /// `true` converts tabs to spaces, `false` converts spaces to tabs.
    ConvertIndentation(bool),
    /// A COMMITS graph row's right-click menu (#1627): act on the clicked
    /// commit, by full hash, with its short hash for the status line.
    CommitAction {
        action: CommitAction,
        hash: String,
        short: String,
    },
}

/// A compound's members and the name to report them under (#310).
struct CompoundLaunch {
    members: Vec<crate::dap::configs::DebugConfig>,
    compound: String,
    /// The compound's `stopAll`, carried to the moment the set is replaced.
    stop_all: bool,
}

/// One viewed mark waiting to reach GitHub (#365).
struct PrViewedWrite {
    /// The review tab's `owner/repo#n`, so a later tab's failure is not
    /// reported against this one.
    key: String,
    pr_id: String,
    path: String,
    viewed: bool,
}

/// A code scanning lookup in flight (#577).
struct CodeScanList {
    branch: String,
    /// Load what it finds (`on`, or the palette command), rather than only
    /// saying it is there (`prompt`).
    load: bool,
    rx: std::sync::mpsc::Receiver<Result<Vec<crate::sarif::github::Analysis>, String>>,
}

/// A pull request head checked out into a sibling worktree (#365).
struct PrCheckout {
    number: u64,
    path: PathBuf,
    /// The commit checked out: a HEAD anywhere else means new commits.
    oid: String,
    /// Whether review mode created the worktree; one that was already there
    /// is never removed by leaving.
    created: bool,
}

/// `${input:…}` questions waiting on the user (#1424): asked one at a
/// time, then `then` runs with the answers.
struct InputRun {
    ask: Vec<crate::dap::configs::LaunchInput>,
    answers: std::collections::BTreeMap<String, String>,
    then: InputThen,
}

/// A launch.json config launch parked behind its `preLaunchTask` (#250): the
/// task runs in a terminal pane, and the FinishedCommand sweep decides.
struct PendingDebugLaunch {
    /// Uid of the pane the task command was written to.
    pane: u64,
    /// The task's command line, matched against the pane's next finished
    /// command (OSC 133 reports what actually ran).
    command: String,
    /// What to do once the task exits 0.
    step: PendingLaunchStep,
    /// When the task was written, for the no-command-mark fallback below.
    started: std::time::Instant,
    /// A background task (#1541): settled by the end of its first build
    /// cycle, which its watch matcher reports, since it never exits.
    watch: bool,
}

#[derive(Clone, Debug)]
struct ExplorerClipboard {
    mode: ExplorerClipMode,
    paths: Vec<PathBuf>,
}

#[derive(Clone, Debug)]
struct PendingFileMove {
    request_id: u64,
    op: FileMove,
    renames: Vec<crate::lsp::manager::FileRenameOp>,
    deadline: std::time::Instant,
    /// The open buffers' `edit_seq`s when the request went out (#1151).
    baseline: Vec<(PathBuf, u64)>,
}

/// Numbers each App's scratch keybindings file under test (#612), so tests
/// running in parallel never share one.
static TEST_KEYBINDINGS_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

/// Accumulated type-to-jump state for the Explorer. `prefix` lowercase
/// only; `last` is the instant of the most recent keystroke.
#[derive(Clone, Debug)]
struct TreeTypeahead {
    prefix: String,
    last: std::time::Instant,
}

struct ContextMenu {
    /// Top-left of the menu in absolute terminal coordinates.
    origin: (u16, u16),
    /// Items, in display order.
    items: Vec<MenuEntry>,
    /// Highlighted top-level row.
    selected: usize,
    /// Index into `items` of the currently open submenu (a `Submenu` entry), or
    /// `None` when no submenu is open.
    open_submenu: Option<usize>,
    /// Highlighted row WITHIN the open submenu (only meaningful when
    /// `open_submenu` is `Some`).
    submenu_selected: usize,
    /// Where any New File / New Folder should be created.
    target_dir: PathBuf,
    /// The breadcrumb whose picker this is (#1608): Left / Right step to
    /// the neighbouring crumb's picker.
    crumb: Option<usize>,
    /// The first top-level row on screen: a menu taller than the terminal
    /// (a big folder's breadcrumb picker) scrolls to keep the highlight in
    /// view. Kept by `scroll_for`.
    scroll: std::cell::Cell<usize>,
    /// The first child row on screen inside the open submenu, driven by the
    /// wheel (`wheel_active`) and kept in range by
    /// render_context_menu.
    submenu_scroll: std::cell::Cell<usize>,
    /// How the menu is drawn and where it opens.
    look: MenuLook,
    /// The keyboard (or the wheel) has moved the highlight, so a look that
    /// otherwise shows none draws it. A [`MenuLook::Panel`] menu always
    /// shows its highlight; Source Control's menus show it only once the
    /// keyboard drives them, so one opened with a click looks as it did.
    keyed: bool,
}

/// How a [`ContextMenu`] is drawn and placed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum MenuLook {
    /// croft's menus: a bordered panel at the menu's origin (a card in a
    /// `surfaces` theme), with each row's shortcut on its right.
    Panel,
    /// Source Control's `⋯` actions menu (#871): rows with a coloured
    /// glyph and no border, opened under the header's `⋯` pill, each
    /// fly-out beside its row. A click on a fly-out's row toggles it. `op`
    /// is the operation the repository was in when it opened, whose rows
    /// lead the menu (#1356); the rows are drawn from it.
    ScmActions { op: Option<crate::git::RepoOp> },
    /// The Commit button's `▾` dropdown (#871): the same rows, under the
    /// `▾`. A click outside it closes it and goes on to what it lands on.
    CommitActions,
}

#[derive(Clone, PartialEq, Eq, Debug)]
enum PromptKind {
    Create(CreateKind),
    /// Rename the entry at `path`. The prompt's `target_dir` holds the
    /// entry's parent; `buffer` is pre-filled with the current basename.
    Rename(PathBuf),
    /// File: Save As… (#1203): a path for the active tab's buffer, relative
    /// to `target_dir` (the workspace root) unless absolute. `buffer` starts
    /// as the tab's own path.
    SaveAs,
    /// LSP "Rename Symbol" (F2 in the editor): rename the identifier at
    /// `(row, col)` in `path`. `buffer` is pre-filled with the symbol; on
    /// commit the new name drives a `textDocument/rename` request.
    RenameSymbol {
        path: PathBuf,
        row: usize,
        col: usize,
    },
    /// Debugger "Add Conditional Breakpoint" / "Edit Condition": edit the
    /// boolean condition for the breakpoint on 1-based `line` of `path`. The
    /// buffer is pre-filled with any existing condition; on commit the
    /// breakpoint is created (if absent) and the condition attached/cleared,
    /// then pushed to a live session. A popup, never the status line.
    BreakpointCondition {
        path: PathBuf,
        line: usize,
    },
    /// Debugger "Add Logpoint" / "Edit Logpoint": edit the log message for
    /// the breakpoint on 1-based `line` of `path` (`{expr}` holes are
    /// interpolated by the adapter, which prints instead of pausing). Same
    /// commit shape as [`PromptKind::BreakpointCondition`].
    Logpoint {
        path: PathBuf,
        line: usize,
    },
    /// Debugger "Add Hit Count Breakpoint" (#611): the DAP `hitCondition`
    /// for the breakpoint on 1-based `line` of `path`. Same commit shape as
    /// [`PromptKind::BreakpointCondition`].
    HitCondition {
        path: PathBuf,
        line: usize,
    },
    /// Debugger "Add Function Breakpoint" (#611): a function name to pause
    /// on entry to.
    FunctionBreakpoint,
    /// Debugger "Break When Value Changes" (#611): a variable name from the
    /// paused frame's scopes.
    DataBreakpoint,
    /// Profiles: New Profile (#618): the new profile's name.
    NewProfile,
    ReviewComment,
    ReviewSummary,
    StickyNote,
    /// Rename the terminal pane at `idx`. The buffer is pre-filled with its
    /// current label; on commit the name overrides the auto label.
    RenameTerminal(usize),
    /// Annotate a span of terminal output on pane `pane` (Cmd+K N with a
    /// selection). `existing` edits that annotation in place; otherwise
    /// `line`/`start`/`len` pin a new one. A blank commit cancels (or, for
    /// an existing note, deletes it).
    AnnotateTerminal {
        pane: usize,
        line: i32,
        /// Scroll-clock reading `line` was captured against when the prompt
        /// OPENED. The commit anchors with this pair: sampling a fresh
        /// baseline at commit would mis-pin the note by every row that
        /// streamed while the user typed it.
        clock: i64,
        start: u16,
        len: u16,
        existing: Option<usize>,
    },
}

struct Prompt {
    label: String,
    buffer: String,
    /// Caret as a char index into `buffer`; `usize::MAX` is the end, which is
    /// where every prompt opens (read it through [`Prompt::caret`]).
    caret: usize,
    kind: PromptKind,
    target_dir: PathBuf,
    error: Option<String>,
}

/// The unsaved-changes prompt in front of a commit or stash (#1438): `op`
/// waits while `files` (dirty tabs under the repository) are unsaved.
/// `root` is the repository the request was made against: the mouse stays
/// live while the prompt is open, so focus can move Source Control to
/// another root before the choice, and the write must not follow it.
#[derive(Clone, Debug, PartialEq, Eq)]
struct PendingUnsavedScm {
    op: ScmWrite,
    root: PathBuf,
    files: Vec<PathBuf>,
}

/// A Drop Stash pick waiting on its confirm popup (#1128): a stash is
/// usually work kept nowhere else, and `git stash drop` has no undo. `root`
/// is the repository it was picked in, held in case Source Control moves
/// to another root before the answer. `oid` is the stash's commit, not its
/// index: a stash pushed or dropped while the question is up shifts every
/// `stash@{N}`, and the drop must still hit the stash that was named.
#[derive(Clone, Debug, PartialEq, Eq)]
struct PendingStashDrop {
    root: PathBuf,
    oid: String,
    label: String,
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct PendingDiscard {
    /// The repository the discard was asked in, which it acts in.
    root: PathBuf,
    rel_path: String,
    untracked: bool,
    staged: bool,
}

/// A revert-hunk request waiting on its confirm popup. Holds the
/// single-hunk patch built when the user pressed R, so the confirm
/// reverse-applies exactly what they were looking at.
#[derive(Clone, Debug, PartialEq, Eq)]
struct PendingRevertHunk {
    rel_path: String,
    patch: String,
}

/// A save refused because the file's encoding cannot hold the buffer
/// (#1053), waiting on the user's answer: what the write would destroy.
/// Asked in a modal, not the status bar, which trims a long message from the
/// middle and used to cut exactly the warning out of it.
#[derive(Clone, Debug, PartialEq, Eq)]
struct LossySave {
    /// The tab's file when asked, so the answer never writes another tab.
    path: Option<PathBuf>,
    encoding: &'static str,
    /// The characters `encoding` cannot represent. Empty when the loss is
    /// bytes that were not valid when the file was read, shown as U+FFFD.
    chars: Vec<char>,
}

/// A tour in progress (#377).
pub struct TourRun {
    pub tour: crate::tour::Tour,
    pub scratch: PathBuf,
    pub previous_root: PathBuf,
    /// The current step opened a palette, Quick Open or a picker, whose Esc
    /// then leaves the tour rather than only closing it (#863).
    pub step_opened_modal: bool,
}

/// The live ssh-pane workspace offer (#364): which pane's foreground became
/// `ssh <host>`, the resolved config alias, and when the offer appeared (it
/// expires on its own if neither accepted nor dismissed).
#[derive(Clone, Debug, PartialEq, Eq)]
struct SshOffer {
    pane: u64,
    host: String,
    since: std::time::Instant,
    /// The pane's OSC 7 report when the offer appeared, so accepting can
    /// tell the remote shell's directory from one left by an earlier shell.
    cwd_at_offer: Option<crate::remote::CwdReport>,
}

/// Active quick-select session on the focused terminal pane: the labelled
/// matches overlaid on the viewport, the label prefix typed so far, and
/// whether any typed char was UPPERCASE (commit then pastes the match into
/// the shell as well as copying it, WezTerm's convention).
pub struct QuickSelectState {
    /// Index into `App::terminals` of the pane the labels were scraped
    /// from: hints, pastes and re-pushes are bound to it, and any gesture
    /// that activates a sibling pane closes the mode (the copy-mode rule).
    pane: usize,
    /// Scroll-clock reading the hint lines were captured at (one snapshot
    /// with the lines themselves); every re-push reuses it so the pane's
    /// render-time translation keeps labels on their scrolled matches.
    clock: i64,
    hints: Vec<QuickHint>,
    typed: String,
    shifted: bool,
}

/// One labelled quick-select match: where it sits (absolute grid line plus
/// char span in the spacer-skipped row text), the text a committed label
/// copies, and the label itself.
#[derive(Clone)]
struct QuickHint {
    line: i32,
    start: usize,
    len: usize,
    text: String,
    label: String,
    /// Continuation segments `(absolute line, start, len)` when the match
    /// wraps the pane edge: highlighted alongside the head span, but the
    /// label glyph only ever paints on the head.
    tail: Vec<(i32, usize, usize)>,
}

/// Active copy-mode session on the focused terminal pane (WezTerm / tmux
/// copy mode): a keyboard cursor over the grid plus the selection anchor
/// and kind once v / V / Ctrl+V arms one. The pane paints the cursor and
/// the selection; this state owns the motions.
pub struct CopyModeState {
    /// Pane index the mode opened on. The coordinates and clock below are
    /// meaningless against any other pane (each pane owns an independent
    /// scroll clock), so a gesture that moves `active_terminal` while the
    /// mode is up closes it instead of hijacking the new pane.
    pane: usize,
    /// Cursor cell: absolute grid line + grid column.
    line: i32,
    col: u16,
    /// Selection anchor cell; `None` until v / V / Ctrl+V arms a selection.
    anchor: Option<(i32, u16)>,
    kind: CopySelKind,
    /// Scroll-clock reading the coordinates above are valid against. The
    /// pane re-anchors its painted selection to streaming content between
    /// keypresses; every key handler folds the clock movement into these
    /// coordinates first, or the keystroke would teleport the highlight
    /// back to the stale absolute lines.
    clock: i64,
}

/// A closed editor tab, reopenable via Cmd+K ⇧W (VS Code "Reopen Closed
/// Editor"): the file plus the view state needed to restore the tab as the
/// user left it. Unlike [`ClosedTerminal`] there is no live process to
/// park, so entries never expire; the stack is just bounded.
pub struct ClosedTab {
    pub path: PathBuf,
    pub pinned: bool,
    pub cursor: (usize, usize),
    pub scroll: usize,
}

/// A terminal pane parked by close for the undo-close grace window: the
/// live pane, the slot it occupied (so restore puts it back where it was),
/// and when it was closed (for expiry).
pub struct ClosedTerminal {
    pub term: crate::widgets::terminal::PtyTerminal,
    pub idx: usize,
    pub closed_at: std::time::Instant,
}

/// A "Running …" status an operation put in the status bar (#856), kept
/// until the operation ends so the bar can say how it went instead of
/// claiming it still runs: a test run, a task, a runnable fenced block.
/// All of them end through [`RunningStatus::settle`].
struct RunningStatus {
    /// The status text the operation set; replaced only while the bar
    /// still shows it, so a newer message is never clobbered.
    shown: String,
    /// What ran, as the outcome names it: a test's own name, `suite X`,
    /// `CodeQL tests`, a task's label, a fenced block's pane.
    label: String,
    started: std::time::Instant,
}

pub struct App {
    pub tree: FileTree,
    pub search: SearchPanel,
    pub remote: RemotePanel,
    pub source_control: SourceControlPanel,
    /// The Source Control COMMITS section: the repo-wide commit graph with
    /// lane rails, stacked under the change list. See [`CommitGraphPanel`].
    pub commit_graph: crate::widgets::commit_graph::CommitGraphPanel,
    /// The COMMITS section's fetch in flight, if one is: background `git log
    /// --topo-order` plus the lane layout. Its reply is tagged by the root it
    /// describes, so a stale reply after a Make Root is ignored on drain.
    /// While it runs, a burst of StatusAndChanges responses spawns no
    /// duplicates; one whose thread crashed is no longer running (#2564).
    graph_fetch: Option<crate::jobs::Job<GraphReply>>,
    /// What the COMMITS panel is currently showing: `None` for the root's
    /// own history, `Some("base..lane")` for a worktree lane's diff (#348).
    ///
    /// The reply tag carries this too, so a history refresh landing while a
    /// lane diff is up is discarded rather than replacing it - the panel
    /// already discarded replies for a since-left ROOT, and this is the same
    /// rule at the resolution the lane diff needs.
    graph_revspec: Option<String>,
    /// A refresh was requested while a fetch was in flight (Make Root, a
    /// newer HEAD): re-fire once the stale reply drains instead of dropping
    /// the request (see [`Self::refresh_commit_graph`]).
    graph_refetch_queued: bool,
    pub run_debug: RunDebugPanel,
    /// The Testing side panel: the suite tree with live pass/fail status.
    pub testing: crate::widgets::testing::TestingPanel,
    /// The CodeQL side bar (#578).
    pub codeql: crate::widgets::codeql::CodeqlPanel,
    /// Background `cargo test` worker feeding the Testing panel.
    test_worker: crate::testing::worker::TestWorker,
    /// The run whose "Running test …" status awaits its outcome (#856).
    test_run_status: Option<RunningStatus>,
    /// The last test run started, for Testing: Rerun Last Run (#1535).
    last_test_run: Option<LastTestRun>,
    /// Tasks and fenced blocks whose "Running …" status awaits their pane's
    /// command-finished report (#856), by pane uid.
    pane_run_statuses: Vec<(u64, RunningStatus)>,
    /// The Extensions side panel: bundled + installed extensions with toggles.
    pub extensions: crate::widgets::extensions::ExtensionsPanel,
    /// Extension ids VS Code reported installed, from the last
    /// "Extensions: Compare with VS Code" (#352). Empty until the user asks,
    /// since listing them spawns `code`. Workspace recommendations are read
    /// from `.vscode/extensions.json` on every refresh instead.
    pub vscode_listed: Vec<String>,
    /// The Explorer's OPEN EDITORS section: the open editor tabs, projected
    /// from the active `editor` group each frame. See [`OpenEditorsPanel`].
    pub open_editors: OpenEditorsPanel,
    /// The Explorer's OUTLINE section, stacked below the file tree: the active
    /// editor's `documentSymbol` tree. See [`OutlinePanel`].
    pub outline: OutlinePanel,
    /// The Explorer's TIMELINE section: the active file's git history, fetched
    /// off-thread via [`crate::git::file_history`]. See [`TimelinePanel`].
    pub timeline: TimelinePanel,
    /// The Explorer's AGENT LANE section (#345).
    pub agent_lane_panel: crate::widgets::agent_lane::AgentLanePanel,
    /// Per lane file, the `+n −m` its row shows and what it was counted
    /// from: (reviewed snapshot, last agent write, disk mtime).
    lane_change_counts: std::collections::HashMap<PathBuf, (LaneCountKey, Option<(usize, usize)>)>,
    /// The Explorer's DEPENDENCIES section: the workspace's packages, resolved
    /// off-thread per detected ecosystem. See [`DependenciesPanel`].
    pub dependencies: DependenciesPanel,
    /// Package ecosystems detected at the workspace root (by manifest file),
    /// recomputed on every re-root. Empty means no recognised manifest, which
    /// hides the DEPENDENCIES section entirely and drops it from the ⋯ menu —
    /// the rule every reference editor follows. Drives the section's header
    /// label too. Cached so the cheap detection never runs on the render path.
    dep_ecosystems: Vec<Ecosystem>,
    /// Which Explorer sub-views are shown, toggled from the ⋯ menu. Persisted.
    pub explorer_views: ExplorerViewVisibility,
    /// Every file of the layered-settings chain (#251), for hot-reload
    /// matching on save; rebuilt by `remerge_settings`.
    settings_chain: Vec<PathBuf>,
    /// Which layer set each top-level settings key, for the hub's
    /// provenance suffix ("· workspace").
    settings_provenance: std::collections::BTreeMap<String, crate::config_layers::LayerKind>,
    /// The merged settings last applied, at start or by a reload: what a
    /// reload compares against, so a setting read once at start applies
    /// when its value changes and leaves a session toggle alone otherwise
    /// (#2907).
    applied_settings: crate::prefs::Prefs,
    /// Extension ids the user has disabled in the Extensions panel. Loaded once
    /// from prefs at startup and persisted on every toggle, mirroring how
    /// `explorer_views` is held in memory rather than re-read each access. The
    /// gates for the bundled features (PDF/CSV/Vim/LSPs) consult this set.
    disabled_extensions: std::collections::BTreeSet<String>,
    /// Ids of the installed extensions: the bundled ones plus those in the
    /// user extensions dir, read at launch and again whenever the Extensions
    /// panel refreshes. An add-on's feature, such as CodeQL's side bar
    /// (#1842), stays off until it is added.
    installed_extensions: std::collections::BTreeSet<String>,
    /// User key bindings from `~/.config/croft/keybindings.json`, consulted
    /// ahead of the built-in chords so a rebind wins over the default.
    keymap: crate::keymap::Keymap,
    /// Live macro recording (#255), `None` when not recording. Carries the
    /// vim register it will be stored under, or `None` for the palette
    /// recorder, whose macro is the unnamed "last recording".
    macro_recording: Option<crate::macros::Recording>,
    /// The most recent finished recording: what "Replay Last" and vim's `@@`
    /// run, and what a `q` with no register stores into.
    macro_last: Option<crate::macros::Macro>,
    /// Named registers (`qa` … `q`), loaded from macros.json at startup.
    macro_registers: std::collections::HashMap<String, crate::macros::Macro>,
    /// True while a replay is feeding keys back through `handle_key`, so a
    /// replayed key is never recorded and a macro cannot invoke itself.
    macro_replaying: bool,
    /// The store path, held so tests can point saves at a tempdir.
    macros_path: std::path::PathBuf,
    /// croft's own config files, checked for changes made outside croft,
    /// such as a file arriving by config sync (#262).
    config_watch: crate::config_sync::ConfigWatch,
    /// The click a user mouse binding is running for (#259), set only for
    /// the duration of that dispatch. Position-carrying commands read it;
    /// `None` everywhere else, so a keyboard invocation of the same command
    /// can never act on a stale click.
    last_mouse_pos: Option<(u16, u16)>,
    /// The tab a tab-strip mouse binding was clicked on (#1427), set only
    /// for that dispatch and taken by `run_command` as it starts, so only
    /// the directly bound command sees it: a command it runs in turn (an
    /// aborted rebase closing its tab, a replayed macro) acts on the active
    /// tab as it would from the keyboard.
    tab_click_target: Option<usize>,
    /// User snippets from `~/.config/croft/snippets.json`. Typing a snippet's
    /// prefix and pressing Tab expands it with tab-stop navigation.
    snippets: crate::snippets::SnippetSet,
    /// VS Code `editor.formatOnSave`: when true, `Cmd+S` first asks the language
    /// server to format the buffer, then writes once the reply lands. Loaded
    /// from prefs at startup, toggled from the palette / `Cmd+K F`.
    format_on_save: bool,
    /// The formatter the settings name for every file and per language
    /// (VS Code's `editor.defaultFormatter`, #1499). Format on save skips a
    /// file whose named formatter croft does not run.
    default_formatter: Option<String>,
    language_formatters: std::collections::BTreeMap<String, String>,
    /// Named formatters whose skipped save has been explained once this
    /// session (#1499); later saves say only "Saved".
    foreign_formatters_noted: std::collections::BTreeSet<String>,
    /// Per-language overrides (#1440), VS Code's `"[python]": { … }`, keyed
    /// by VS Code's language id. Read through the `*_for` helpers, which
    /// fall back to the global setting.
    language_prefs: std::collections::BTreeMap<String, crate::prefs::LanguagePrefs>,
    /// VS Code `terminal.integrated.copyOnSelection`: a finished terminal
    /// mouse selection lands on the clipboard without an explicit Cmd+C.
    /// Loaded from prefs at startup, toggled in the Settings hub.
    copy_on_select: bool,
    /// `multi_cursor_persist`: keep every caret through arrows, Home/End,
    /// word-wise moves and Enter rather than collapsing on the first move.
    /// Loaded from prefs at startup, toggled in the Settings hub.
    multi_cursor_persist: bool,
    /// The project's `files_exclude` globs (#1345), as last applied to the
    /// Explorer, so a settings reload re-lists the tree only on a change.
    files_exclude: Vec<String>,
    /// The `files_associations` in effect (#1574), to tell a change.
    files_associations: std::collections::BTreeMap<String, String>,
    /// The workspace roots `files_associations` were installed for, so a
    /// root added, removed or replaced gets them or loses them (#1574).
    files_associations_roots: Vec<PathBuf>,
    /// The `files_encoding` default (#1432) as last applied, for a folder
    /// added to the workspace later.
    files_encoding: Option<crate::file_encoding::Charset>,
    /// Tailspin highlighting in rendered log views (#466): the inverse of
    /// the `disable_log_highlight` preference. Pushed into `log_view`'s
    /// default for views opened later and into every open view on toggle.
    log_highlight: bool,
    /// Built-in secret redaction rules are in the trigger set (#360).
    /// Settings "Terminal: Redact Secrets"; stored inverted in config.json
    /// as `disable_secret_redaction` so the default is on.
    secret_redaction: bool,
    /// "Terminal: Reveal Redacted Secrets": masks paint as their real text
    /// until this instant, then mask again on the next frame.
    redaction_reveal_until: Option<std::time::Instant>,
    /// VS Code's `files.autoSave: afterDelay`: dirty buffers write
    /// themselves to disk once the delay elapses after the last edit.
    auto_save: bool,
    /// VS Code's `files.autoSave: onFocusChange`: a dirty buffer writes
    /// itself the moment it loses focus. Independent of `auto_save`.
    pub auto_save_on_focus_change: bool,
    /// The focus signature (pane, group, active path) seen by the last
    /// auto-save tick. A change means something lost focus, which is the
    /// only trigger `onFocusChange` fires on. `None` until the first tick
    /// so opening croft never writes anything on its own.
    last_focus_signature: Option<(usize, usize, Option<PathBuf>)>,
    /// Latch set while a save is waiting on a format reply. `drain_lsp_format`
    /// consumes it to write the (now formatted) buffer to disk.
    save_after_format: Option<PathBuf>,
    /// The project's Prettier formatting the active file (#1255), whose
    /// reply completes a deferred save like an LSP format reply does.
    prettier_job: Option<crate::prettier::Job>,

    /// When a format-on-save stops waiting on its formatter and writes the
    /// file unformatted (#1491). Set with `save_after_format`.
    format_save_deadline: Option<std::time::Instant>,
    /// The TIMELINE lookup for the active file, while it runs, tagged with
    /// the file it describes. Starting another drops it, so an older list can
    /// never replace a newer one (#2575).
    timeline_fetch: Option<crate::jobs::Job<(PathBuf, Vec<crate::git::FileHistoryEntry>)>>,
    /// CodeQL's CLI, its running jobs and queue, and what they last reported
    /// (see [`codeql::CodeqlJobs`]); the view is [`Self::codeql`].
    codeql_jobs: codeql::CodeqlJobs,
    /// The file the TIMELINE has been fetched (or is being fetched) for, so the
    /// fetch fires exactly once per active-file change.
    timeline_fetched: Option<PathBuf>,
    /// When and for which folder the subfolder-repo scan behind the
    /// Source Control empty state last ran (#1227); rescanned at most
    /// every couple of seconds while that card shows.
    nested_repo_scan: Option<(PathBuf, std::time::Instant)>,
    /// Off-thread per-line git blame for the active editor (GitLens-style
    /// inline annotation). Fetched once per (file, HEAD) like the gutter.
    blame_rx: std::sync::mpsc::Receiver<(PathBuf, crate::git::Blame)>,
    blame_tx: std::sync::mpsc::Sender<(PathBuf, crate::git::Blame)>,
    /// The file blame has been fetched (or is in flight) for; reset to `None`
    /// on HEAD move so the annotation refreshes after a commit / checkout.
    blame_fetched: Option<PathBuf>,
    /// The commit whose card the pointer, resting on the inline blame, asked
    /// for (#1556), and where `git log` sends it. Kept once the card shows,
    /// so the card stays while the pointer moves along the annotation.
    blame_hover: Option<(String, std::sync::mpsc::Receiver<Result<String, String>>)>,
    /// The network git operation in flight (push, pull, fetch, clone...),
    /// named for the status line, the folder whose repository Source
    /// Control showed when it started, and where its finisher arrives. One
    /// that lands once another repository is shown (a workspace switch, or
    /// another folder's) leaves the line under the commit box alone (#2901).
    git_net_job: Option<(String, PathBuf, std::sync::mpsc::Receiver<GitNetDone>)>,
    /// A read-only git view being computed off the UI thread (a TIMELINE
    /// commit diff, a COMMITS patch): a large repo's `git show` or rename
    /// walk froze the editor when run inline. A newer click replaces it.
    git_view_job: Option<std::sync::mpsc::Receiver<GitNetDone>>,
    /// A *Git: … Changes* command waiting on its read of the repository's
    /// changes (#871). A newer one replaces it.
    git_file_lookup: Option<scm::GitFileLookup>,
    /// *Git: … Changes* commands run while one was reading, each with its
    /// open file and folder, waiting their turn in order (#871).
    git_file_queue: std::collections::VecDeque<(scm::GitFileCommand, PathBuf, PathBuf)>,
    /// The `vim_mode` pref as last applied (#1286). A settings reload
    /// changes the live mode only when the pref itself changed, so an
    /// unrelated edit to config.json does not reset the mode in use.
    vim_mode_pref: bool,
    /// User pref: show the current-line inline blame annotation (default on).
    inline_blame_enabled: bool,
    /// The provenance lens (#349): who typed each line, in the gutter and
    /// the blame annotation. Session-only for now; the persisted half of
    /// #349 (the map beside local history) is a later slice. Pushed onto
    /// every editor in every group by `sync_focus_flags`, since the gutter
    /// bar paints in every pane, not only the focused one.
    provenance_overlay: bool,
    /// Indentation guides (#129), VS Code `editor.guides.indentation`: on by
    /// default, copied to every open tab by `apply_view_prefs`.
    indent_guides_enabled: bool,
    /// Home walks back one wrapped row per press (`home_walks_wrapped_rows`).
    home_walks_rows: bool,
    /// Bracket-pair colorization (#131): on by default, copied to every
    /// open tab by `apply_view_prefs` like the indent guides.
    bracket_colors_enabled: bool,
    /// Whitespace rendering mode (#133), copied to every open tab (under each
    /// tab's language override) by `apply_view_prefs`;
    /// "selection" by default, the palette command cycles it.
    whitespace_mode: crate::widgets::editor::WhitespaceMode,
    /// Ruler columns (#1524) from the `rulers` setting, copied to every open
    /// tab by `apply_view_prefs` like `whitespace_mode`.
    rulers: Vec<u16>,
    /// Gutter line numbers (#1576): on, relative, interval or off.
    line_numbers: crate::widgets::editor::LineNumbers,
    /// Default ignore-whitespace mode applied to newly opened diffs (#258).
    diff_ws_default: crate::widgets::diff::DiffWhitespace,
    /// Debugger inline values (#135): on by default; rebuilt on every
    /// `InspectionUpdated`, cleared wherever the stop arrow clears.
    inline_values_enabled: bool,
    /// Save unsaved editors before every debug or run launch (#1400), so
    /// the debuggee runs the text its breakpoints were set against. Off
    /// only by `disable_save_before_debug` (`debug.saveBeforeStart: none`).
    save_before_debug: bool,
    /// Auto-closing pairs (#121), persisted; synced onto the active editor
    /// beside the blame flag.
    auto_close_pairs: bool,
    /// Re-indent a multi-line paste under the caret line (#1218), on unless
    /// `disable_paste_reindent` is set.
    paste_reindent: bool,
    /// The global on-save clean-ups (#1657), handed to each editor as it
    /// saves. A `.editorconfig` property still wins for its file.
    save_cleanup: crate::widgets::editor::SaveCleanup,
    /// LSP inlay hints (inline type / parameter annotations), on by default
    /// like VS Code's `editor.inlayHints.enabled`; toggled from the palette
    /// and persisted as `disable_inlay_hints` in config.json.
    inlay_hints_enabled: bool,
    /// Local-history root (`~/.config/croft/history`), cached so tests can
    /// redirect snapshots to a tempdir.
    history_root: PathBuf,
    /// The snapshot currently shown in a local-history diff, `(live file,
    /// snapshot millis, snapshot contents)`, so "Local History: Restore
    /// Snapshot" can write it back and carry its seats (#349). Cleared when a
    /// non-snapshot diff opens.
    history_restore: Option<(PathBuf, u64, Vec<u8>)>,
    /// Completed off-thread history recordings, drained to refresh the
    /// TIMELINE once the snapshot actually exists on disk.
    history_done_rx: std::sync::mpsc::Receiver<PathBuf>,
    history_done_tx: std::sync::mpsc::Sender<PathBuf>,
    /// The whole-project check in flight, if one is (#256). Its result is
    /// where it ran and what the process did; the `io::Result` carries a
    /// spawn failure separately from a run that produced output, because
    /// those need different messages. While it runs, a second invocation
    /// says so rather than stacking compilers; one whose thread crashed is
    /// no longer running (#2562).
    #[allow(clippy::type_complexity)]
    project_check: Option<crate::jobs::Job<(PathBuf, std::io::Result<std::process::Output>)>>,
    /// How many rows the last sweep produced (#256), counted before its
    /// items join a store shared with every other producer.
    project_check_rows: usize,
    /// Whether the sweep may run unasked (#256): `auto`, `on` or `off`.
    /// Read at startup; an unrecognised value behaves as `auto`.
    problems_project_scope: String,
    /// What gcc/clang-shaped build messages count their column in
    /// (`build_column_unit`), applied when a scan installs its rows.
    build_column_unit: crate::build_matchers::ColumnUnit,
    /// The PROBLEMS entry Go to Next/Previous Problem last opened, as (its
    /// file, line, column, place in the walked list) plus where that left
    /// the caret (row, column). Repeating the command from that caret steps
    /// from this entry, not from the caret: an entry past its line's end
    /// leaves the caret clamped short of it, which compared as still ahead
    /// and was picked again, and entries sharing a position were skipped.
    last_problem_jump: Option<(PathBuf, u32, u32, usize, usize, usize)>,
    /// Signals that this workspace is too large for `auto` to run the sweep
    /// unasked (#256). Sent from the sweep's worker, since deciding it is a
    /// whole-workspace walk.
    project_hint_rx: std::sync::mpsc::Receiver<()>,
    project_hint_tx: std::sync::mpsc::Sender<()>,
    /// One hint per session, not per run: repeating it every sweep would
    /// train the user to ignore the status line.
    project_check_hinted: bool,
    /// Replies from the background test-source locator (the workspace grep is
    /// too slow for the render thread): the root the search ran under, the
    /// test name, and the hit. A reply from before a re-root is dropped.
    #[allow(clippy::type_complexity)]
    test_jump_rx: std::sync::mpsc::Receiver<(PathBuf, String, Option<(PathBuf, u32)>)>,
    #[allow(clippy::type_complexity)]
    test_jump_tx: std::sync::mpsc::Sender<(PathBuf, String, Option<(PathBuf, u32)>)>,
    /// The DEPENDENCIES lookup for the current root, while it runs. A
    /// re-root replaces it, so a slower lookup for the folder just left can
    /// never land in the new folder's list (#2573).
    deps_fetch: Option<crate::jobs::Job<Vec<Dependency>>>,
    /// The workspace root DEPENDENCIES was detected/fetched for, so detection
    /// and the fetch fire once per root (re-fired by Make Root / re-root).
    deps_fetched: Option<PathBuf>,
    /// Push-to-talk voice input (Termux OSK mic): the live session, whose
    /// result `drain_voice` reads and which a second mic tap cancels.
    voice_handle: Option<crate::voice::VoiceHandle>,
    /// Set when a mic tap cancels a live session, so the resulting empty result
    /// is reported as "canceled" rather than "no speech detected", and a
    /// transcript is dropped rather than typed.
    voice_canceling: bool,
    /// A finished session's transcript, held from the drain that received it
    /// to the next one so the input queued in between is read first: a mic tap
    /// among it cancels the transcript. `voice_handle` stays set meanwhile, so
    /// that tap counts as a cancel rather than a new session.
    voice_ready: Option<String>,
    pub editor: EditorTabs,
    /// The editor group layout tree. `editor` (above) is ALWAYS the active
    /// (focused) group, hoisted out of the tree's single active leaf, so the
    /// 430-odd `self.editor` call sites keep operating on whichever group has
    /// focus. The tree holds every OTHER group plus the spatial split
    /// structure; a single unsplit leaf is the just-launched state. Splitting,
    /// moving focus, and collapsing all go through [`editor_layout`].
    pub editor_layout: editor_layout::EditorLayout,
    /// One-shot latch armed when a save is refused because the file changed
    /// on disk. The next `save()` overwrites anyway, giving the user a
    /// "press Cmd+S again to overwrite" escape hatch without a modal.
    force_save_armed: bool,
    /// One-shot latch armed when a save failed for want of permission
    /// (#1379): the next Cmd+S of that file saves it with sudo instead.
    sudo_save_armed: Option<PathBuf>,
    /// Saves handed to `sudo tee` in a terminal pane (#1379): each file and
    /// the bytes it should end up holding, settled once it holds them.
    pending_sudo_saves: Vec<(PathBuf, Vec<u8>, Option<i32>)>,
    pub terminals: Vec<PtyTerminal>,
    pub active_terminal: usize,
    perf: PerfHud,
    /// Developer: Toggle Screencast Mode: the keys pressed, shown on screen
    /// for a recording. Off at every launch, as VS Code's is.
    screencast: Screencast,
    /// The workspace's root-folder set (#143). One entry today; the
    /// multi-root slices grow it. Read the primary via `workspace_root()`.
    roots: crate::workspace::WorkspaceRoots,
    sidebar_view: SidebarView,
    sidebar_areas: SidebarAreas,
    /// Active IDE color theme. Drives every explicit-background surface
    /// (baked icon PNGs, editor image/sheet/diff canvases, the `SetColors`
    /// session fill); `Color::Reset` surfaces follow the session bg for free.
    /// Toggled from the activity-bar gear menu; persisted via `crate::prefs`.
    theme: crate::theme::Theme,
    focus: Pane,
    show_tree: bool,
    show_terminal: bool,
    /// `Ctrl+Shift+J` maximizes the terminal pane: the editor / welcome
    /// collapses to zero height and the terminal fills the entire right
    /// column next to the Explorer. Pressing the chord again, or hiding
    /// the terminal entirely with `Ctrl+J`, clears the flag.
    terminal_maximized: bool,
    /// Layout (Customize Layout popup): whether the left activity bar is
    /// shown. Persisted via `crate::prefs`.
    activity_bar_visible: bool,
    /// Layout: whether the bottom status bar is shown. Persisted.
    status_bar_visible: bool,
    /// Layout: which edge the primary side bar (+ activity bar) docks to.
    /// Persisted.
    side_bar_position: SideBarPosition,
    /// Layout: whether the secondary side bar (the active file's Outline) is
    /// shown, on the edge opposite the primary side bar (⌥⌘B). Persisted.
    secondary_side_bar_visible: bool,
    /// Layout: horizontal span/alignment of the bottom panel. Persisted.
    panel_alignment: PanelAlignment,
    /// Layout: where the quick input (command palette / Go to File) anchors.
    /// Persisted.
    quick_input_position: QuickInputPosition,
    /// View: Toggle Centered Layout (#1585), persisted: the editor keeps
    /// `centered_layout_width` columns in the middle of its column.
    centered_layout: bool,
    /// Zen Mode starts centred (`zen_center_layout`, persisted, on by
    /// default), as VS Code's `zenMode.centerLayout`.
    zen_center_layout: bool,
    /// Whether this Zen session is centred: `zen_center_layout` on entry,
    /// until Toggle Centered Layout turns it off.
    zen_centered: bool,
    /// The centred editor's width in columns (#1585).
    centered_layout_width: u16,
    /// When the last Esc in Zen Mode that did nothing else landed: a
    /// second such Esc inside [`ZEN_ESC_WINDOW`] leaves Zen (#1585).
    zen_esc_at: Option<std::time::Instant>,
    /// Until when the "how to leave Zen" hint shows (#1585). Zen hides the
    /// status bar, so the hint draws on its own.
    zen_hint_until: Option<std::time::Instant>,
    /// Auto-hide the sidebar when focus moves to the editor or a terminal
    /// (#260, `sidebar_auto_hide`). Off by default. The sidebar comes back
    /// on any deliberate sidebar action — Cmd+B, an activity-bar icon, or a
    /// sidebar-targeting command like reveal-in-explorer.
    sidebar_auto_hide: bool,
    /// Set while a focus move is happening for a reason other than the user
    /// asking for it (async results, launch, sidebar-internal activation).
    sidebar_auto_hide_suspended: bool,
    /// One-shot exemption: the next auto-hide collapse is skipped and the
    /// flag consumed. Cmd+B reveals the sidebar WITHOUT moving focus, so a
    /// sticky flag would stay set (focus never lands on the tree to clear
    /// it) and silently disable auto-hide for the rest of the session.
    /// Consuming it on the next attempt keeps the reveal usable while
    /// guaranteeing the feature re-arms itself.
    sidebar_pinned_open: bool,
    /// Grace delay between focus leaving the sidebar and auto-hide collapsing
    /// it (#302), so a click passing through the panel does not take it away
    /// under the pointer.
    sidebar_dwell: SidebarDwell,
    /// Zen Mode (hides the activity bar, both side bars, the panel and the
    /// status bar): the chrome it hid, to restore when it ends. Zen is on
    /// exactly while this is `Some` (`zen_mode()`), so the two cannot
    /// disagree.
    pre_zen: Option<PreZenLayout>,
    /// Hit-test rects for the top-right layout-control icons.
    layout_icon_areas: LayoutIconAreas,
    status: String,
    /// Orange status-bar badge shown for the whole session when this is a
    /// remote (SSH) croft not running under dtach, so it can't survive a
    /// transport drop. Set once at startup; rendered separately from `status`
    /// so transient messages never clobber it. `None` otherwise.
    persistence_warning: Option<&'static str>,
    quit: bool,
    drop_to_local: bool,
    /// Session origin and launcher-exported capabilities (remote SSH, relay
    /// key, persistent wrapper, update-check flags), resolved once at startup.
    session: HostSession,
    context_menu: Option<ContextMenu>,
    /// The type-to-filter state of an open breadcrumb picker: the typed text
    /// and the picker's full, unfiltered rows. Reset whenever a picker opens
    /// (`show_crumb_picker`) and read only while `context_menu.crumb` is set.
    crumb_filter: (String, Vec<MenuEntry>),
    prompt: Option<Prompt>,
    fs_watch: FsWatch,
    /// One extra watcher per SECONDARY workspace root (#147), sharing the
    /// primary's watch-cap budget. The primary `fs_watch` also owns the
    /// adaptive poll (its dir-mtime snapshot walks the whole tree, all
    /// roots included) and the open-file stats; extras are event-only.
    fs_watch_extra: Vec<FsWatch>,
    /// One git worker per SECONDARY workspace root (#149), keyed by the
    /// root path. `git` stays the primary's worker. Each discovers its
    /// own repo toplevel; replies are root-scoped by construction.
    git_extra: Vec<(PathBuf, GitWorker)>,
    /// The workspace root whose repository the Source Control panel and
    /// the status line follow: the root owning the focused editor's
    /// file, falling back to the primary. Recomputed each drain tick; a
    /// change re-seeds the panel from that worker and re-requests its
    /// change list.
    active_scm_root: PathBuf,
    /// A repositories-overview click pins the panel to that folder's repo
    /// (#161); the pin clears when the FOCUS-derived folder changes, so
    /// the panel resumes following the file the user works in.
    scm_pin: Option<PathBuf>,
    /// The last focus-derived folder, for detecting the change that
    /// releases `scm_pin`.
    scm_follow_root: PathBuf,
    /// The workspace root the Testing panel and worker are bound to —
    /// follows the focused file's folder like the SCM surfaces (#151).
    active_test_root: PathBuf,
    /// The last `followed_test_root` answer and the workspace folder and
    /// focused file it was for (#1520).
    test_root_follow: Option<((PathBuf, Option<PathBuf>), PathBuf)>,
    /// The folder and focused file a `poll_test_root_rebind` thread is
    /// resolving the Testing folder for, and where its answer arrives. With
    /// the CodeQL runner added, the answer walks the workspace four folders
    /// deep, seconds in a home folder (#1973), so it never runs on the UI
    /// thread.
    test_root_pending: Option<PendingTestRoot>,
    /// Folder set from an explicitly opened `.code-workspace` (#163):
    /// consumed by `change_workspace_root` INSTEAD of the automatic
    /// per-primary restore, so the file defines the arrangement exactly.
    pending_workspace_folders: Option<Vec<PathBuf>>,
    /// Per-workspace-root last-seen HEAD oid, for gutter-baseline and
    /// commit-graph invalidation per repository (#149).
    git_head_oids: std::collections::BTreeMap<PathBuf, Option<String>>,
    git: GitWorker,
    cursor_blink: CursorBlink,
    /// Overwrite (Replace) mode, toggled by the Insert key. One switch for the
    /// whole app, as in VS Code: it follows you from tab to tab.
    overwrite_mode: bool,
    /// Whether the terminal's caret was last set to the overwrite block, so
    /// the style escape is sent only when the wanted shape changes.
    caret_block_sent: bool,
    editor_click: ClickTracker,
    tree_click: ClickTracker,
    terminal_click: ClickTracker,
    /// Modified clicks, so `ctrl+double_click` and friends can be recognised
    /// without arming the unmodified trackers above (see the type's docs).
    /// One tracker, not one per region: it is only ever read by the binding
    /// dispatch, and a double needs both clicks at the same cell, so two
    /// regions cannot be confused for one another.
    modified_click: ModifiedClickTracker,
    /// Double-click tracking for PORTS rows: a second click on the same row
    /// opens that port, the same as pressing Enter on it.
    ports_click: ClickTracker,
    /// The drag a left press started (splitter, scrollbar, minimap,
    /// breakpoint lane, scrubber, Explorer drag, editor line/tab drag,
    /// terminal pane reorder, forwarded terminal press, or terminal
    /// drag-selection with edge auto-scroll), until its release. One value,
    /// so two cannot be under way at once; a new press replaces it (#2859).
    gesture: Option<Gesture>,
    /// The secondary side bar's own view of the Outline (scroll and hit
    /// rects), swapped into `outline` around that bar's render and mouse
    /// handling so the two hosts scroll and click independently (#1326).
    secondary_outline: crate::widgets::outline::OutlineView,
    welcome: WelcomeState,
    /// One-shot background refresh of the remote extension index. Polled in the
    /// main loop; rebuilds the Extensions panel once if the verified cache
    /// changed. `None` once consumed (or on a disarmed build, immediately).
    ext_index_refresh: Option<std::sync::mpsc::Receiver<bool>>,
    /// True when the in-flight refresh was triggered by the user (the ⟳ button),
    /// so its completion reports a status line; the silent startup refresh
    /// doesn't, to avoid clobbering more useful startup messages.
    ext_index_manual_refresh: bool,
    /// Channel to the background search worker. Each keystroke or toggle
    /// flip pushes a `(query, opts)` request here; the worker debounces
    /// and runs `search_workspace` off the UI thread.
    search_query_tx: std::sync::mpsc::Sender<crate::search::SearchRequest>,
    /// Results coming back from the search worker: `(query, hits)`. The
    /// query is echoed so we can drop stale results when the user has
    /// typed past the query that produced them.
    search_results_rx: std::sync::mpsc::Receiver<crate::search::SearchEvent>,
    /// Pixel size of one terminal cell, captured in `init_graphics`.
    /// Required to bake OSC-1337 images at exact viewport pixel size so
    /// iTerm draws them with no stretching or letterboxing.
    cell_pixel: Option<(u32, u32)>,
    /// The activity-bar icons and panel illustrations `init_graphics` is
    /// baking off the UI thread (#1973); `None` once they are up.
    chrome_bake: Option<std::sync::mpsc::Receiver<(ChromeBakeInput, ChromeImages)>>,
    /// What the activity-bar icons and panel illustrations on screen were
    /// baked for; `None` while none are up.
    chrome_shown: Option<ChromeBakeInput>,
    /// The bake asked for while `chrome_bake` was still running: it runs
    /// when that one lands, in its place.
    chrome_bake_next: Option<ChromeBakeInput>,
    /// The welcome wordmark being baked off the UI thread (#1973), and what
    /// for; `None` once it is up.
    welcome_bake: Option<(WelcomeBakeKey, std::sync::mpsc::Receiver<Option<String>>)>,
    /// Last wheel step applied to a PDF preview (when, in which direction,
    /// and on which document), so a momentum flick's same-direction burst
    /// coalesces instead of queueing one blocking rasteriser run per event.
    /// Keyed by document: switching to another PDF starts fresh - a reader's
    /// first gesture there is not a repeat.
    last_pdf_wheel: Option<(std::time::Instant, i32, Option<PathBuf>)>,
    /// One-shot consent to overwrite FORMULA cells on the next xlsx
    /// save (#178/#194): armed by the save that held them back, distinct
    /// from `force_save_armed` (the disk-conflict consent).
    formula_overwrite_armed: bool,
    /// The activity-bar count badges, indexed by [`ActivityIcon`] (VS Code's
    /// `activityBarBadge`): unsaved files on the Explorer, Source Control
    /// changes, forwarded ports on Remote and failed tests on Testing (see
    /// [`ActivityIcon::badge`]). Kept current by `refresh_activity_badges`.
    activity_badges: [CountBadge; ActivityIcon::ALL.len()],
    /// Problem count the cached `overlays.problems_badge` circle was built for;
    /// gates the per-frame rebuild in `refresh_problems_badge`.
    problems_badge_count: usize,
    /// Anchor cell (top-left) where the PROBLEMS-tab circle badge should be
    /// emitted this frame, recorded by `paint_panel_tabs` in images mode. `None`
    /// when the panel strip isn't drawn or the count is zero; the post-frame
    /// `flush_problems_badge_overlay` clears the image when it's `None`.
    problems_badge_cell: Option<(u16, u16)>,
    /// Which inline-image protocol the host terminal speaks, resolved once at
    /// startup (env-fixed). Drives the [`crate::iterm2_inline::build_inline_image`]
    /// dispatch (iTerm2 OSC-1337 vs Kitty graphics) and the Kitty-only
    /// delete-all eviction on the clear chain.
    inline_protocol: crate::iterm2_inline::InlineImageProtocol,
    /// Set once by the runtime DA1 probe (`run`) when the host terminal lacks an
    /// env-advertised protocol (iTerm2/Kitty) but answers DA1 with sixel
    /// support. `init_graphics` promotes `inline_protocol` to `Sixel` when this
    /// is true, and preserves it across the theme-switch re-detect that would
    /// otherwise reset to `None` (sixel has no env signal to re-derive from).
    sixel_supported: bool,
    /// One-shot: a WINCH arrived, so the next frame must wipe and repaint every
    /// cell on EVERY protocol. A dtach reattach fires WINCH (`-r winch`) against
    /// a blank physical screen while ratatui's buffer still says everything is
    /// painted, so the diff alone emits nothing; the Kitty path skips the
    /// image-eviction wipe (the typing-blink fix), so resize owns its own.
    resize_repaint_needed: bool,
    /// The editor minimap's side, raster, text strip and hit rect
    /// (see [`inline_images::MinimapState`]).
    minimap: inline_images::MinimapState,
    /// Which pictures a pop-up was drawn over this frame (#2663).
    picture_cover: popups::PictureCover,
    /// What each small chrome image (the PROBLEMS badge, Run and Debug
    /// icon, sidebar illustrations) was last sent over, keyed by overlay,
    /// for [`App::chrome_image_due`]. Keys not checked in a frame (the image
    /// was hidden) are dropped.
    chrome_sent: std::collections::HashMap<&'static str, u64>,
    chrome_seen: std::collections::HashSet<&'static str>,
    /// Clipboard read entrypoint. Production uses the host clipboard; tests
    /// can swap in a deterministic reader for Cmd+V routing assertions.
    clipboard_reader: fn() -> Option<String>,
    remote_launch: Option<RemoteLaunch>,
    pending_remote_launch_host: Option<String>,
    pending_remote_launch_path: Option<String>,
    /// The ssh-pane offer currently shown in the status bar (#364), if any.
    /// Accepted with Cmd+K G, dismissed with Esc, dropped when the pane's
    /// ssh ends or after `SSH_OFFER_TTL`.
    ssh_offer: Option<SshOffer>,
    /// What each pane (by uid) was last seen connected to, so an ssh session
    /// is offered once when it starts rather than on every sample.
    ssh_offer_seen: std::collections::BTreeMap<u64, Option<String>>,
    /// `disable_remote_offer` from prefs (#364).
    remote_offer_disabled: bool,
    /// `remote_offer_excluded_hosts` from prefs (#364).
    remote_offer_excluded: Vec<String>,
    /// `fleet_groups` from prefs (#363): named host sets a fleet run can
    /// expand, so a fleet is named once rather than retyped per run.
    fleet_groups: std::collections::BTreeMap<String, Vec<String>>,
    /// The `code_scanning` setting (#577).
    code_scanning: crate::sarif::github::CodeScanningMode,
    /// The branch (or detached HEAD) code scanning last looked at, so a
    /// checkout of another one is noticed.
    code_scan_seen: Option<String>,
    /// A code scanning lookup in flight: the branch it is for, whether to
    /// load what it finds, and where the chosen analyses arrive.
    code_scan_list: Option<CodeScanList>,
    /// Chosen analyses fetching as SARIF: where their saved files arrive.
    code_scan_fetch: Option<std::sync::mpsc::Receiver<Vec<Result<PathBuf, String>>>>,
    /// Hosts whose provisioning failed recently (#364), loaded from the
    /// cache file at startup and extended when an install fails.
    remote_offer_refused: crate::remote::RefusedHosts,
    /// The `agents.json` row a new worktree lane starts in its pane (#348);
    /// `None` opens a plain shell. From `lane_agent` in settings.json.
    lane_agent: Option<String>,
    /// The colour depth each frame is fitted to before it is written
    /// (#1433): the `color_depth` setting, else what the terminal's
    /// environment advertises.
    pub(crate) color_depth: crate::color_depth::ColorDepth,
    /// Whether the terminal reads underline colours (#2661); when it does
    /// not, each frame drops them before it is written.
    pub(crate) underline_colors: bool,
    /// Which panes are worktree lanes' (#348), by pane uid: saved with the
    /// terminal session so a relaunch seats the agent again, and consulted
    /// when a lane closes so its pane goes with it.
    lane_panes: std::collections::BTreeMap<u64, crate::terminal_session::LaneRecord>,
    /// Explorer-scoped Cut/Copy buffer. Independent from the OS clipboard
    /// (which carries text), this stores filesystem paths and the intent
    /// (move vs. copy) until the next Paste consumes it.
    tree_clipboard: Option<ExplorerClipboard>,
    /// Explorer type-to-jump buffer. Each printable keystroke within
    /// `TREE_TYPEAHEAD_WINDOW` of the previous one accumulates; a
    /// keystroke after the gap resets the buffer and cycles past the
    /// current selection. Matches VS Code's tree typeahead.
    tree_typeahead: Option<TreeTypeahead>,
    /// File the user picked via the explorer's "Select for Compare" menu
    /// action. None until they pick one; cleared once they invoke
    /// "Compare with Selected" against another file.
    compare_anchor: Option<PathBuf>,
    /// Whole-screen rect captured at the start of every `render`. The
    /// context-menu hit-test reads this to clamp the menu's bounds the
    /// same way the renderer does, so a menu that gets shifted up to fit
    /// on screen still maps clicks to the right item.
    last_frame_area: Rect,
    /// The Explorer/sidebar column rect from the last render (`None` when
    /// the sidebar is collapsed). The zoxide jump popup anchors over this
    /// so it drops from the Explorer rather than centering on the frame.
    last_sidebar_area: Option<Rect>,
    /// Pending SCP uploads queued by a Finder drag-drop onto the Remote
    /// Explorer. These are intentionally NOT run inline — the main loop
    /// drains the queue after suspending the alt-screen so scp can use
    /// the host shell for password / FIDO / known_hosts prompts and the
    /// user actually sees what's happening.
    pending_scp_uploads: Vec<ScpUpload>,
    /// Drops awaiting reverse-pull from the user's local Mac via the
    /// drop-relay launched by the local croft parent. Polled each frame.
    pending_remote_pulls: Vec<PendingRemotePull>,
    /// Copies sent home over the relay whose pump answer is still due; see
    /// `drain_relay_copies`.
    pending_relay_copies: Vec<PendingRelayCopy>,
    /// This croft's `croft view` listener (#362).
    ///
    /// `None` when the bind failed: a croft that cannot offer `croft view`
    /// must still start, so the failure costs the feature and nothing else.
    /// Panes then see no `CROFT_VIEW_SOCK` and the client says so plainly,
    /// which is the same message a pane outside croft gets.
    ///
    /// No path is kept alongside it. The doc used to promise "the socket path
    /// to unlink when it goes away" and nothing unlinked it: there is no
    /// `Drop for App`, both readers discarded the second element, and the
    /// launch-time sweep is what actually reclaims a dead croft's socket -
    /// deliberately, since an unclean exit leaves no `Drop` to run either.
    /// A field carrying a promise the code does not keep is worse than no
    /// field.
    view_listener: Option<std::os::unix::net::UnixListener>,
    /// Agent edit approvals (#347): this workspace's hook socket, the
    /// proposals waiting on the user in arrival order, and the popup's
    /// state for the one at the head (`Some` exactly while one waits).
    hook_listener: Option<std::os::unix::net::UnixListener>,
    /// The workspace the hook socket was bound for: an approval
    /// notification's links name it, so `croft decide` finds this croft
    /// (#359).
    hook_root: PathBuf,
    approvals: std::collections::VecDeque<crate::agent_approval::Pending>,
    approval_ui: Option<crate::agent_approval::ApprovalUi>,
    /// The language server's check of the proposal at the head of the
    /// queue (#347): its diagnostics show in the popup before approval.
    approval_check: Option<crate::agent_approval::ProposalCheck>,
    /// A proposal being edited before approval (#347): its token, and the
    /// scratch file whose save approves the edited text. The popup stays
    /// down while it lasts.
    approval_edit: Option<(String, PathBuf)>,
    /// When that edit is a three-way merge with a dirty tab's unsaved
    /// edits, the tab's file: once approved, the tab takes the merged text.
    approval_merge_into: Option<PathBuf>,
    /// One Jupyter kernel per notebook that has run a cell (#355), by path.
    /// Dropping one (the map entry going) shuts its kernel down.
    notebook_kernels: std::collections::HashMap<PathBuf, crate::notebook_kernel::NotebookRun>,
    /// Notebook: Select Kernel asking for the installed kernels (#1377):
    /// the notebook it is for and the bridge listing them.
    kernel_listing: Option<(PathBuf, crate::notebook_kernel::KernelSession)>,
    /// The kernel picker's notebook, the kernelspecs its rows name, and
    /// the kernel the notebook asked for that is not installed, if that is
    /// why it opened.
    kernel_choice: Option<(
        PathBuf,
        Vec<crate::notebook_kernel::KernelSpec>,
        Option<String>,
    )>,
    /// When the shown coverage report was taken, the file its editor lens
    /// was built for, and the install command a missing coverage tool
    /// needs (#263).
    coverage_at: Option<std::time::SystemTime>,
    coverage_lens_path: Option<PathBuf>,
    coverage_install: Option<String>,
    /// How long one frame may spend serving `croft view` clients (see
    /// `drain_view_requests`). A field so a test on a loaded machine can
    /// stretch it: the fixed 20ms is shorter than one file open there.
    view_drain_budget: std::time::Duration,
    /// URL awaiting the user's local-browser confirmation (remote-
    /// launched croft only). When `Some`, a modal asks Y/A/N and all
    /// other keys are swallowed.
    pending_local_open: Option<String>,
    /// Source Control entry awaiting Y/N confirmation for destructive
    /// `Discard Changes`. Holds the relative path and whether it was an
    /// Untracked entry (Untracked discard deletes the file from disk;
    /// tracked discard runs `git checkout HEAD -- <path>`). Both paths
    /// destroy uncommitted work, so the modal must always run first.
    pending_discard: Option<PendingDiscard>,
    /// A commit or stash asked for while tabs under the repository held
    /// unsaved edits (#1438). The write records the files on disk, so the
    /// modal asks first: save them all, go ahead without them, or cancel.
    pending_unsaved_scm: Option<PendingUnsavedScm>,
    /// The repository the open Delete Remote Tag picker listed (#1582),
    /// so the deletion stays bound to it if Source Control moves.
    remote_tag_root: Option<PathBuf>,
    /// Set for the one call that runs a write the prompt already cleared.
    scm_unsaved_cleared: bool,
    /// A write waiting on a format-on-save reply: Save All armed the
    /// active tab's deferred write, and the commit runs once it lands.
    scm_after_format_save: Option<(ScmWrite, PathBuf)>,
    /// Armed by R in a Source-Control diff; reverting a hunk destroys
    /// uncommitted work, so the confirm modal always runs first.
    pending_revert_hunk: Option<PendingRevertHunk>,
    /// An unsaved-changes prompt (#862) waiting on S / D / Esc, holding what
    /// it goes on to do. While `Some`, the modal eats every key and click.
    pending_unsaved: Option<UnsavedExit>,
    /// A save that would lose characters or bytes to the file's encoding
    /// (#1053), waiting on S / Esc. While `Some`, the modal eats every key
    /// and click, so a second Cmd+S can no longer write it.
    pending_lossy_save: Option<LossySave>,
    /// The unsaved state (see [`App::unsaved_stamp`]) the user chose to
    /// leave behind when asked about a remote connect (#862): the handoff
    /// that ends this croft later goes without asking again only while
    /// nothing has changed since.
    remote_handoff_ok: Option<u64>,
    /// Hot exit (#862): the directory this croft backs its unsaved buffers
    /// up under (`~/.local/state/croft/hot-exit`, see [`user_data_dir`]).
    /// `None` keeps no backups, as
    /// under test unless a test points it at a tempdir.
    pub hot_exit_dir: Option<PathBuf>,
    /// The [`App::unsaved_stamp`] this croft's backup on disk was written
    /// at, `None` while it has none: the tick writes only when they differ.
    /// A write that fails leaves it alone, so a later tick tries again.
    hot_exit_written: Option<u64>,
    /// When a failed hot-exit write may be tried again (#862): a full disk
    /// is retried every [`App::HOT_EXIT_DELAY`], not every frame. `None`
    /// once a write lands.
    hot_exit_retry_at: Option<std::time::Instant>,
    /// Backups of crofts that are gone whose text this croft restored
    /// (#862). Removed once its own backup holds that text, so a write that
    /// fails never leaves the text in memory only.
    hot_exit_consumed: Vec<PathBuf>,
    /// Consumed backups that could not be removed once this croft's own
    /// backup landed (#862 review). They may still hold text, so the quit
    /// tries them again and says which are left.
    hot_exit_unremoved: Vec<PathBuf>,
    /// Lines for stderr once the terminal is the shell's again, about what
    /// the user must still hear after croft has gone: a hot-exit backup a
    /// quit could not remove (#862).
    exit_notes: Vec<String>,
    /// Where each clean exit saves the session for the next launch, one
    /// file per workspace (`~/.local/state/croft/sessions`, see
    /// [`crate::session_state::saved_path`]). `None` saves none, as under
    /// test unless a test points it at a tempdir.
    pub session_dir: Option<PathBuf>,
    /// The saved session being read off the UI thread once the first frame
    /// is up, and whether its tabs may come back (hot exit brought none) or
    /// only its bookmarks.
    saved_session: Option<(
        std::sync::mpsc::Receiver<crate::session_state::SessionState>,
        bool,
    )>,
    /// The bookmarks of files no tab holds marks for any more, on their
    /// saved lines: what the last tab holding them left when it closed
    /// (see [`Self::settle_bookmarks`]).
    closed_bookmarks: std::collections::HashMap<PathBuf, std::collections::BTreeSet<usize>>,
    /// The tasks backing the open Run Task picker; the picker row `id`
    /// indexes into this list.
    run_tasks: Vec<crate::tasks::Task>,
    /// The most recently run task, for "Tasks: Rerun Last Task".
    last_task: Option<crate::tasks::Task>,
    /// Set on startup (after `init_graphics` resolves the inline-image
    /// protocol) when the host terminal can't render croft's icons/images
    /// and the user hasn't silenced the nudge. Drawn as a dismissible modal
    /// recommending iTerm2 (macOS) / Ghostty (macOS/Linux).
    pending_terminal_warning: bool,
    /// A message shown whole in a centred card (see `message_card.rs`).
    message_card: Option<crate::widgets::message_card::MessageCard>,
    /// Cached default-branch name (e.g. "main" / "master"), refreshed
    /// when the workspace root changes. Surfaced in the commit dropdown's
    /// "View Changes vs <name>" label so users know which branch the
    /// diff compares against. `None` until the resolver succeeds.
    default_branch_label: Option<String>,
    /// The resolver running off the UI thread for the repository at the
    /// path (#1973): it runs up to five `git` processes, and on macOS each
    /// goes through the Xcode shim.
    default_branch_ask: Option<(PathBuf, std::sync::mpsc::Receiver<Option<String>>)>,
    /// Last HEAD oid the git-gutter baselines were synced against. When the
    /// git worker reports a different oid (commit, checkout, pull), the cached
    /// per-tab HEAD snapshots are invalidated so the gutter refetches.
    /// True after the user has chosen "Always for this session" on the
    /// local-browser confirmation. Subsequent link clicks dispatch to
    /// the relay silently.
    trust_local_browser: bool,
    /// Width in cells of the sidebar (Explorer / Search / Remote pane).
    /// Defaults to 32 cells; user can drag the splitter between sidebar
    /// and editor to widen or narrow.
    sidebar_width: u16,
    /// Height in cells of the bottom terminal pane. `None` = use the
    /// default 35% split; Some = a user-specified pinned height. Stored
    /// in cells (not percent) so it doesn't drift on window resize.
    terminal_height: Option<u16>,
    /// Last-rendered geometry of the vertical splitter column (between
    /// sidebar and editor) and the horizontal splitter row (between
    /// editor and terminal). Used by the mouse handler to hit-test cleanly
    /// without recomputing layout outside of `render`.
    sidebar_splitter_x: Option<u16>,
    terminal_splitter_y: Option<u16>,
    /// Every draggable seam between editor groups, as laid out by the most
    /// recent render. Empty when unsplit. Drives both the focus-on-click guard
    /// (a press on a seam must not steal focus) and the seam-drag hit-test, for
    /// a grid of any depth.
    editor_seams: Vec<editor_layout::Seam>,
    /// The whole editor area (every group) as the most recent render laid
    /// it out: what `leaf_rects` splits into the groups' rects. The active
    /// group's own `last_full_area` is only its share once the editor is
    /// split, so a geometric hit-test must start from this instead.
    editor_layout_area: Rect,
    /// First terminal shown in the rail when there are more terminals than
    /// rows. Clamped during paint, so nothing else has to keep it honest.
    terminal_rail_scroll: usize,
    /// Which terminal the rail last scrolled into view, so the reveal fires
    /// when the active pane changes and never fights a deliberate wheel.
    terminal_rail_revealed: Option<usize>,
    /// Extensions whose programs the user has allowed to run (the MCP
    /// consent set, loaded once from prefs and kept current when a consent
    /// is granted here), so a viewer's gate is a set lookup rather than a
    /// prefs read per click, and a test can seed it without a prefs file.
    consented_extensions: std::collections::BTreeSet<String>,
    /// The config dir this app reads extensions and consent from. Carried
    /// rather than re-derived so a test can point one app at a scratch dir
    /// instead of mutating the process-wide environment, which this repo
    /// has twice seen take an unrelated test down.
    config_dir: PathBuf,
    /// The profile whose settings this croft started with (#1587), whose
    /// `config.json` under `config_dir` is the user layer the Settings
    /// editor reads and writes. `None` for the Default profile, and always
    /// under test, so a developer's own profile never steers the suite.
    settings_profile: Option<String>,
    /// Where "open scrollback in an editor tab" writes the pane's coloured
    /// scrollback so the rendered log view can index it from disk (#257).
    /// `~/.cache/croft/scrollback` normally; a test points it at a temp dir.
    scrollback_dir: PathBuf,
    /// True while one terminal pane fills the panel's width and the others
    /// are listed in the right-side rail (the per-pane `⛶` button / Cmd+K M).
    /// Distinct from `terminal_maximized`, which grows the panel vertically
    /// over the editor. Only meaningful with 2+ terminals; closing down to
    /// one resets it.
    terminal_pane_maximized: bool,
    /// Which tab the bottom panel group is showing (TERMINAL or PROBLEMS).
    bottom_panel_tab: BottomPanelTab,
    /// The PROBLEMS view: aggregated workspace diagnostics, projected from
    /// `lsp_diagnostics`. Rendered into the panel content when its tab is active.
    problems: crate::widgets::problems::ProblemsPanel,
    /// The OUTPUT view: a read-only per-channel log viewer over the in-process
    /// `crate::output` bus. Rendered into the panel content when its tab active.
    output: crate::widgets::output::OutputPanel,
    /// The PORTS view: the registry of detected loopback ports, fed by the
    /// terminal output scrape and the periodic socket poll. Rendered into the
    /// panel content when its tab is active.
    ports: crate::widgets::ports::PortsPanel,
    /// The CAPTURES view: output lines collected by `capture` triggers
    /// (iTerm2's Capture Output); Enter / click jumps back to the line.
    pub captures: crate::widgets::captures::CapturesPanel,
    /// Transient click-only notification for a freshly detected browsable port,
    /// offering forward / open. `None` when nothing is pending.
    port_toast: Option<PortToast>,
    /// Last time the socket poll was kicked off, so it fires on a low cadence
    /// rather than every tick.
    last_port_poll: std::time::Instant,
    /// The socket poll in flight, if one is: a one-shot background job (one
    /// complete listener set per cycle), so the blocking `lsof`/`ps` never
    /// runs on the event loop (keeps input at 0ms). A full set, not
    /// individual hits, so the app can reconcile ports that stopped
    /// listening. While one runs, cadence ticks start no overlapping `lsof`;
    /// one whose thread crashed is no longer running (#2568).
    port_poll: Option<crate::jobs::Job<PortPoll>>,
    /// The fleet command in flight (#363), if one is, run off the event
    /// loop. A fleet run waits on the network for up to `FLEET_TIMEOUT`, and
    /// blocking the loop for that would freeze the editor — the user could
    /// not scroll, type, or cancel while their fleet answered. While it
    /// runs, a second invocation says so rather than starting a competing
    /// fleet against the same hosts; one whose thread crashed is no longer
    /// running (#2566).
    fleet_run: Option<crate::jobs::Job<Vec<crate::fleet::HostResult>>>,
    /// The command the fleet run in flight is running, for its tab (#363).
    fleet_command: String,
    /// When the terminal panes' labels were last looked up.
    last_label_refresh: std::time::Instant,
    /// The pane-label lookup in flight, if one is: each pane's foreground
    /// process resolved off the event loop (see [`terminal_panes::LabelPass`]).
    /// While it runs, cadence ticks start no other; one whose thread crashed
    /// is no longer running (#2569).
    label_pass: Option<crate::jobs::Job<terminal_panes::LabelPass>>,
    /// Where the panel's tabs and buttons are this frame. The frame resets it
    /// whole before it draws the panel, so a tab or button not drawn this
    /// frame has no area and a click there does nothing.
    panel_hits: PanelHitAreas,
    /// Last pointer cell reported by a `Moved` event. Render-time hover
    /// hit-testing (the Black theme's teal pill behind the terminal `-`/`+`
    /// buttons) reads this instead of tracking enter/leave transitions.
    pointer_cell: Option<(u16, u16)>,
    /// Which activity-bar icon the pointer is currently over, if any. Drives
    /// the hover variant of the icon (brightened glyph, no selection pill).
    /// Changing it arms `overlays.activity.mark_dirty()` so the swapped
    /// pre-baked image is re-emitted; it never triggers a re-bake.
    hovered_activity_icon: Option<ActivityIcon>,
    /// Which layout toolbar icon the pointer is over (0 = side bar, 1 = panel,
    /// 2 = customize), so a hover change can re-emit the brighter image variant
    /// like [`Self::hovered_activity_icon`] does for the activity bar.
    hovered_layout_icon: Option<usize>,
    /// On-screen keyboard for touch-only environments (Termux). `Some`
    /// while the bottom keyboard band is visible; taps on its keys
    /// synthesize `KeyEvent`s through `handle_key`, so they reach the
    /// editor, terminal PTY, and modals exactly like hardware keystrokes.
    osk: Option<crate::widgets::osk::Osk>,
    /// True when taps in editable areas (editor text, terminal panes, the
    /// Search input) auto-raise the on-screen keyboard. Armed on Termux,
    /// where active mouse tracking permanently suppresses the native soft
    /// keyboard; `CROFT_FORCE_OSK=1` arms it elsewhere for testing.
    osk_auto: bool,
    /// Total width / height of the right-hand content area, captured on
    /// every render so a splitter drag can clamp to the live viewport.
    last_content_width: u16,
    last_content_height: u16,
    /// The language servers: manager, sync, diagnostics, progress, questions
    /// (see [`language_servers::LspState`]).
    lsp: language_servers::LspState,
    /// Whether this session already looked for shellcheck when a shell
    /// script was opened (#1511), so its notice is shown at most once.
    shell_lint_notice_shown: bool,
    /// The PATH the helper tools servers shell out to are looked up in
    /// (#1511): croft's own, which its servers inherit.
    tool_search_path: Option<std::ffi::OsString>,
    /// Open-buffer paths as of the last PROBLEMS rebuild. Open Files scope
    /// filters on this set, so the panel must refresh when the set changes —
    /// not only when diagnostics do, or opening a file with stored
    /// diagnostics would show nothing until unrelated churn happened by.
    problems_open_set: std::collections::BTreeSet<PathBuf>,
    /// Markdown-lint's own edit-seq cursor, mirroring `lsp_last_seen` but
    /// independent of the LSP: `.md` files get linted whether or not any
    /// language server is running (there is none for Markdown), so this
    /// cannot share the LSP's cursor map. Keyed by path; a path's entry is
    /// removed when its tab closes so a later re-open re-lints instead of
    /// trusting a stale seq (a different file could reuse a `PathBuf` after
    /// a rename-in-place on disk). Each entry holds the lines that were
    /// linted as well as their seq, because two split panes count edits
    /// separately and can share a seq while holding different text.
    markdown_lint_last_seen: std::collections::HashMap<PathBuf, (u64, Vec<String>)>,
    /// Live Run (`crate::live_run`): the files armed to run as they are
    /// edited. Per file, never global, so no script runs unasked.
    live_run_files: std::collections::HashSet<PathBuf>,
    /// The buffer text last sent per armed file, so an unchanged buffer is
    /// not re-run on every tick.
    live_run_sent: std::collections::HashMap<PathBuf, Vec<String>>,
    /// The background runner, spawned on the first toggle.
    live_run_runner: Option<crate::live_run::Runner>,
    /// The last position [`App::sync_markdown_scroll`] pushed across split
    /// panes: (path, whether the preview led, source line), so a still
    /// viewport costs nothing per tick.
    md_scroll_synced: Option<(PathBuf, bool, usize)>,
    /// Code lenses on or off (#608, `Editor: Toggle CodeLens`), and the
    /// edit seq last requested per path, so a still buffer asks once.
    code_lens_enabled: bool,
    code_lens_requested: std::collections::HashMap<PathBuf, u64>,
    /// Function breakpoints (#611), handed to each session as it launches.
    function_breakpoints: Vec<String>,
    /// Screen reader mode (#621), its speech command, the announcer, and
    /// where the status bar drew the announcement this frame (the cursor's
    /// resting place when no text has focus).
    screen_reader: bool,
    screen_reader_command: Option<String>,
    announcer: crate::a11y::Announcer,
    announce_pos: Option<(u16, u16)>,
    /// The latest event screen reader mode reads out whatever has focus
    /// (#1547), numbered so a repeat is still read.
    a11y_event: Option<(u64, String)>,
    /// Run and Debug's sessions, configs, console and what a stop last
    /// reported (see [`debugger::DebugState`]); the view is [`Self::run_debug`].
    debug: debugger::DebugState,
    /// Terminal command suggestions (#614): on or off, and the one painted
    /// this frame as (pane index, the text Right arrow would type).
    term_suggest_enabled: bool,
    term_suggestion: Option<(usize, String, String)>,
    /// A new shortcut being recorded in the Keyboard Shortcuts editor
    /// (#612), in whichever of its two phases it is.
    shortcut_capture: Option<ShortcutCapture>,
    /// Inline AI suggestions (#607): off until the user turns them on, since
    /// they send code to a model. The worker, the backend it was started
    /// for, the request in flight (id plus what it answers for), and the
    /// last request's key so a still caret asks once.
    inline_enabled: bool,
    inline_worker: Option<crate::inline_complete::Worker>,
    inline_config: Option<crate::inline_complete::Config>,
    inline_job: Option<(u64, PathBuf, u64, usize, usize)>,
    /// The language server's inline completion in flight, keyed like
    /// `inline_job`: a server that offers them answers for its files.
    inline_lsp: Option<(u64, PathBuf, u64, usize, usize)>,
    inline_asked: Option<(PathBuf, u64, usize, usize)>,
    inline_next_id: u64,
    /// Where recorded shortcuts are written: the user's keybindings.json,
    /// or under test a scratch file, so a test run never edits the real one.
    keybindings_file: PathBuf,
    /// A Search Editor rerun in flight (#615): the tab it lands in, the
    /// header it ran, and where its hits arrive.
    search_editor_job: Option<(
        PathBuf,
        crate::search_editor::Header,
        std::sync::mpsc::Receiver<Vec<crate::search::SearchHit>>,
    )>,
    /// The open SARIF logs (path, disk stamp) the published diagnostics were
    /// built from (#577); a different set means publish again.
    sarif_diag_signature: Vec<(PathBuf, Option<(std::time::SystemTime, u64)>)>,
    /// When the SARIF viewers' added logs were last checked for changes.
    sarif_added_polled: Option<std::time::Instant>,
    /// The selection and open files the SARIF step marks were drawn for.
    sarif_step_signature: Option<SarifStepSignature>,
    /// Every (file, source key) SARIF diagnostics were published under, so a
    /// closed log's results can be withdrawn exactly.
    sarif_published: Vec<(PathBuf, String)>,
    /// Code completion's popup, request, re-asks and resolve
    /// (see [`completion::CompletionState`]).
    completion: completion::CompletionState,
    /// `disable_quick_suggestions` unset: a word begun opens completion.
    quick_suggestions: bool,
    /// `word_based_suggestions`: which open documents word completion
    /// draws on (#1566).
    pub word_suggestions: crate::word_suggest::WordSuggestions,
    /// Signature Help (parameter hints) popup, anchored to the caret while the
    /// user is inside a call. Populated by `drain_lsp_signature_help`.
    pub signature_help_popup: Option<crate::widgets::signature_help_popup::SignatureHelpPopup>,
    signature_help_request_id: Option<u64>,
    /// (cursor_row, cursor_col, edit_seq) at the last signatureHelp request, so
    /// the tick can re-query when the caret moves or the buffer changes while
    /// the popup is open and close it once the caret leaves the call.
    signature_help_anchor: Option<(usize, usize, u64)>,
    /// The editor's word hover: dwell, popup, request and what it was asked about
    /// (see [`mouse_input::HoverState`]).
    hover: mouse_input::HoverState,
    /// The rust-analyzer request a `Rust:` command is waiting on (#1626);
    /// a reply to any other id is stale and dropped.
    rust_analyzer_request: Option<(u64, crate::lsp::rust_analyzer::RaRequest)>,
    /// Separate dwell timer for the editor tab strip. Hovering a tab for
    /// `HOVER_DELAY` surfaces `tab_tooltip` with the tab's full path so
    /// same-named files can be told apart. Kept distinct from `hover.dwell`
    /// (which drives LSP word hover in the editor body) so the two never
    /// fight over the same timer.
    tab_hover: HoverDwell,
    tab_tooltip: Option<crate::widgets::hover_popup::HoverPopup>,
    tab_hover_idx: Option<usize>,
    /// Shared dwell + popup for chrome button hints (search-panel actions and
    /// toggles, activity-bar icons, …). One timer for all chrome controls,
    /// keyed on the label so the dwell only restarts when the pointer crosses
    /// to a different control; extending coverage to a new surface is one arm
    /// in `ui_tooltip_at`.
    ui_hover: HoverDwell,
    ui_tooltip: Option<crate::widgets::hover_popup::HoverPopup>,
    ui_tooltip_label: Option<std::borrow::Cow<'static, str>>,
    /// The Search list's `(scroll, hits, shown)` when the hint was last read
    /// against it: [`Self::reread_ui_tooltip`] reads it again on a change.
    search_hint_view: (usize, usize, bool),
    /// The chrome tooltip's final on-screen rect from the most recent render
    /// (after the activity-bar shift / viewport clamp), or `None` when no
    /// tooltip is showing. Read by the post-draw flush so the box can be
    /// re-painted on top of any sidebar illustration it overlaps, since
    /// OSC-1337 images composite above ratatui's text layer.
    ui_tooltip_area: Option<Rect>,
    /// Where the update card sat on the last frame, `None` while it is
    /// folded, gone or too big for the screen. The activity bar's gear and
    /// heart and the side bar's illustrations are inline images, which land
    /// above text, so they hold back wherever the card is (#1958).
    update_card_rect: Option<Rect>,
    definition_request_id: Option<u64>,
    /// In-flight Peek Definition request (#115): same LSP request as the
    /// jump, different consumer — the response opens the peek popup at the
    /// caret instead of moving it.
    peek_definition_request_id: Option<u64>,
    /// The open peek popup, rendered beside `hover.popup` against the whole
    /// frame; `peek_target` is where Enter jumps.
    peek_popup: Option<crate::widgets::hover_popup::HoverPopup>,
    peek_target: Option<(PathBuf, u32, u32)>,
    /// Peek References (#616): every reference and the one the popup shows.
    /// Up / Down step through them; any close of the popup clears this.
    peek_refs: Option<(Vec<PeekLocation>, usize)>,
    /// Set when the in-flight references request came from Peek References,
    /// so its reply opens the popup instead of the picker.
    references_want_peek: bool,
    declaration_request_id: Option<u64>,
    type_definition_request_id: Option<u64>,
    /// The `(active file, last-seen LSP edit seq)` the OUTLINE was last synced
    /// to, so `sync_outline` re-requests symbols exactly once per tab switch or
    /// edit batch. `None` seq means the file has no language server.
    outline_synced: Option<(PathBuf, Option<u64>)>,
    /// A large buffer's syntax outline being parsed off the UI thread
    /// (#1150): the `outline_synced` key its snapshot was taken at, and
    /// where the symbols land. One at a time; typing meanwhile coalesces
    /// into one fresh parse when it lands.
    outline_parse: Option<(OutlineKey, OutlineParse)>,
    /// The `outline_synced` key the last off-thread parse was started for.
    outline_parsed_for: Option<(PathBuf, Option<u64>)>,
    /// The file and edit seq whose language-server outline last landed: a
    /// syntax parse of that same buffer finishing later must not replace
    /// the richer symbols.
    outline_lsp_landed: Option<(PathBuf, u64)>,
    implementation_request_id: Option<u64>,
    references_request_id: Option<u64>,
    call_hierarchy_request_id: Option<u64>,
    /// In-flight documentHighlight request, with the caret spot it was
    /// fired for so a reply for a caret or buffer state the editor has
    /// since left is dropped.
    occurrences_request: Option<CaretRequest>,
    /// In-flight linkedEditingRange request (#254), with the file and edit
    /// seq it was asked for.
    linked_request: Option<FileRequest>,
    /// Last observed caret spot for the linked-editing debounce, and when
    /// it settled — occurrences' idle pattern.
    linked_observed: Option<CaretSpot>,
    linked_observed_at: std::time::Instant,
    /// A request already went out for `linked_observed` (#1299). Kept
    /// apart from `linked_request`, which the reply clears: a `null`
    /// answer left nothing to say the position was asked, so every UI
    /// wake asked again while the caret stayed put.
    linked_asked: bool,
    /// In-flight Expand Selection request (#254). Extra Shift+Alt+Right
    /// presses while the chains are in flight stack up and apply
    /// together on drain; an `unsupported` verdict resolves them via
    /// tree-sitter instead. The reply is gated on id + path + edit_seq
    /// AND the exact cursor positions the request was fired for — a
    /// caret move without an edit leaves `edit_seq` unchanged, and a
    /// chain computed for the old caret must not apply at the new one.
    selection_range_request: Option<PendingSelectionRanges>,
    /// The caret state `tick_occurrences` last observed, and when it changed.
    /// A caret resting on one spot for `OCCURRENCES_IDLE` fires one request.
    occ_observed: Option<CaretSpot>,
    occ_observed_at: std::time::Instant,
    rename_request_id: Option<u64>,
    /// The open buffers' `edit_seq`s when the in-flight rename went out
    /// (#1151): a reply for a buffer edited since is refused.
    rename_baseline: Vec<(PathBuf, u64)>,
    /// An Explorer rename or move waiting on the servers' `willRenameFiles`
    /// edit (#610); it runs when the answer lands or its deadline passes.
    pending_file_move: Option<PendingFileMove>,
    /// In-flight prepareRename (#254), with the caret spot that is the
    /// deferred rename prompt's context.
    prepare_rename_request: Option<CaretRequest>,
    format_request_id: Option<u64>,
    /// True when the in-flight format request is Format Selection (#254)
    /// — the drain words its statuses accordingly.
    format_request_selection: bool,
    /// The `edit_seq` of the tab the in-flight format request was computed
    /// against (#1127): a reply for a buffer typed into since is dropped.
    format_request_seq: Option<u64>,
    /// VS Code's `editor.formatOnType` (#254): typing a server trigger
    /// character runs onTypeFormatting at the spot. Off by default.
    format_on_type: bool,
    /// In-flight on-type request, with the tab's path and the edit_seq it
    /// was computed against. The reply applies only while that same tab is
    /// active and its buffer has not moved since — `edit_seq` is per-tab,
    /// so the seq alone cannot tell tabs apart.
    on_type_request: Option<FileRequest>,
    code_action_request_id: Option<u64>,
    /// True when the in-flight code-action request is a `codeAction/resolve`
    /// (the reply is one resolved action to apply directly) rather than the
    /// initial `textDocument/codeAction` (whose reply opens the picker).
    code_action_pending_resolve: bool,
    /// The open buffers' `edit_seq`s when the code-action (or resolve)
    /// request whose edits are pending went out (#1151).
    code_action_baseline: Vec<(PathBuf, u64)>,
    /// The actions from the in-flight `textDocument/codeAction` reply, indexed
    /// by the row `id` the Quick Fix [`ListPicker`] presents.
    pending_code_actions: Vec<crate::lsp::manager::CodeActionItem>,
    /// Pull-request review's comment boxes, jobs and `gh` reads and writes
    /// (see [`review::ReviewState`]).
    review: review::ReviewState,
    /// Sticky notes (#367): every note in the workspace, where the owner
    /// keeps them (`None` under test), and which box id is which note.
    notes: crate::sticky_notes::Notes,
    notes_path: Option<PathBuf>,
    note_box_ids: std::collections::HashMap<u64, String>,
    notes_shown_gen: u64,
    /// An open asciicast recording (#356): the writer, the file it appends
    /// to, and when it started. `None` when nothing is being recorded.
    recording: Option<(
        crate::asciicast::Recorder,
        std::fs::File,
        std::time::Instant,
    )>,
    /// The terminal size the open recording last wrote (#356). A resize is
    /// an `r` event, and a player that never receives one keeps rendering
    /// the header's size — so a session resized mid-recording plays back
    /// with every later frame wrapped at the wrong width.
    recorded_size: (u16, u16),
    /// The history scrubber's position, view, slider and caches
    /// (see [`diff_merge::ScrubState`]).
    scrub: diff_merge::ScrubState,
    /// `a` in the approval popup (#347): the agent whose edits are
    /// approved without asking, until when.
    auto_approve: Option<(String, std::time::Instant)>,
    /// The running `croft demo` tour (#377), with its scratch project and
    /// the workspace to return to.
    pub tour: Option<TourRun>,
    /// The tour was finished or skipped once (#377), so the welcome panel
    /// no longer offers it.
    pub tour_done: bool,
    /// The welcome panel's Take the Tour button, from the last frame.
    pub welcome_tour_button: Rect,
    /// The `gh` executable GitHub features run; tests point it at a stand-in.
    gh_program: PathBuf,
    /// How long one code scanning `gh` call may take (#2786); tests shorten it.
    gh_limit: std::time::Duration,
    /// Code scanning's asks of GitHub in flight (#2786): the analyses to
    /// offer, the alert behind a result, a dismissal, an analysis to open.
    code_scan_asks: Vec<crate::jobs::Job<code_scanning::CodeScanAnswer>>,
    /// The SARIF result a code scanning dismissal is for (#577): its log's
    /// path and its run and result indices, marked suppressed on success.
    dismiss_target: Option<code_scanning::DismissTarget>,
    /// The workspace folder the code scanning analysis picker was filled
    /// from, which the analysis picked is fetched from (#2786).
    code_scan_picker_root: Option<PathBuf>,
    /// The SARIF location a Locate prompt is open for (#577): its URI and
    /// the 1-based line and column to land on.
    sarif_locate_pending: Option<(String, i64, i64)>,
    /// A SARIF source download in flight (#577): the location's URI, its
    /// 1-based line and column, and the body or why it failed.
    sarif_download: Option<std::sync::mpsc::Receiver<SarifDownload>>,
    /// Fetches a URL's body; a field so tests can stand in for the network.
    sarif_fetch: fn(&str) -> Result<Vec<u8>, String>,
    /// Per-agent ledger of files changed while an agent was working (#345),
    /// and the review baselines that decide which of them still need a
    /// look.
    pub agent_ledger: crate::agent_lane::AgentLedger,
    /// Where the ledger is saved per workspace (#345); `None` keeps it in
    /// memory only (tests, unless one sets it).
    agent_ledger_store: Option<PathBuf>,
    /// The ledger generation last saved, and when.
    agent_ledger_saved: (u64, std::time::Instant),
    /// The (agent, file) behind each row of the lane-file picker.
    pending_lane_picks: Vec<(String, PathBuf)>,
    /// The tab and line behind each row of Bookmarks: View Bookmarks.
    pending_bookmarks: Vec<BookmarkTarget>,
    /// When the running Fix with Navigator turn started (#374): the clock
    /// its PROBLEMS-row spinner runs on, and the flag that keeps the main
    /// loop repainting while it runs. `None` when no fix is streaming.
    problems_fix_started: Option<std::time::Instant>,
    /// The in-flight `.http` request's channel (#370). One at a time: the
    /// worker owns the socket, the drain owns the tab, and a second send
    /// while one runs is refused rather than queued.
    http_run: Option<std::sync::mpsc::Receiver<HttpRunOutcome>>,
    /// The `.http` env files' named environment whose values requests use
    /// (#924), picked with HTTP: Switch Environment; `None` uses only
    /// `$shared` and flat values, as REST Client's "No Environment" does.
    http_environment: Option<String>,
    /// The presentation edits behind the open Change Color Presentation
    /// picker (#254), indexed by row id; cleared after a pick.
    pending_color_presentations: Vec<(String, Vec<crate::widgets::editor::TextSpanEdit>)>,
    /// The (path, edit_seq) the pending presentations were computed
    /// against; a pick is refused when either moved while the picker was
    /// open (an FS-sync reload bumps the seq without user edits).
    pending_color_context: Option<(PathBuf, u64)>,
    /// In-flight colorPresentation request: (id, path, edit_seq).
    color_presentations_request: Option<(u64, PathBuf, u64)>,
    nav: NavHistory,
    editor_vim_chord: EditorVimChord,
    /// `Cmd+K` leader: armed when the user presses the prefix, holding the
    /// instant it started so the next key within `CMD_K_LEADER_TIMEOUT`
    /// completes a `Cmd+K`-prefixed chord (VS Code's two-key model), and the
    /// leader's name for the status bar's "Waiting for second key" line.
    /// `None` when no chord is in flight.
    cmd_k_leader: Option<CmdKLeader>,
    /// VS Code's "The key combination (Ctrl+K, X) is not a command." note:
    /// when it comes off the bar, and its text, so a message that replaced
    /// it in the meantime stays.
    cmd_k_miss_note: Option<(std::time::Instant, String)>,
    /// Whether croft asked the terminal for the kitty keyboard protocol at
    /// startup (what `croft keys` reports as `kitty=`). A terminal that
    /// honours it reports `Shift` held with `Ctrl` explicitly, so
    /// `Ctrl+Shift+S` can be told from `Ctrl+S` (#857); one that ignores it
    /// sends the same control byte for both, never a `Shift`. False until
    /// `run` asks.
    kitty_keys: bool,
    /// The terminal answered the startup probe but not its kitty keyboard
    /// query, so Ctrl+\, Ctrl+] and Ctrl+/ arrive as the bytes crossterm
    /// reads as Ctrl+4, Ctrl+5 and Ctrl+7 (#1007): see `legacy_ctrl_punct`.
    legacy_ctrl_punct: bool,
    /// Native modal (vim-style) editing for the editor pane. When enabled it
    /// supersedes the always-on `editor_vim_chord` convenience layer.
    vim: crate::vim::VimState,
    /// Last `f`/`t`/`F`/`T` target, replayed by `;` and `,`.
    vim_last_find: Option<(crate::vim::FindKind, char)>,
    /// Whether the active visual selection is linewise (`V`), so the pending
    /// visual operator deletes/yanks whole lines.
    vim_visual_line: bool,
    shortcuts_modal: Option<crate::widgets::shortcuts::ShortcutsModal>,
    /// The Settings editor (#612).
    settings_editor: Option<crate::widgets::settings_editor::SettingsEditor>,
    /// Where the status bar's clickable items are this frame. The status bar
    /// resets it whole before it draws, so an item not drawn this frame has no
    /// area and a click there does nothing.
    status_hits: StatusHitAreas,
    /// The last status message logged to the Messages channel, so each one
    /// is logged once, when it first shows (#1609).
    status_logged: String,
    /// A new status message is showing and nothing has been pressed since,
    /// so a message too long for the room left by the right-hand cluster
    /// (Ln/Col, encoding, language) takes the whole bar (#1609). The next
    /// key or click hands the room back.
    status_wide: bool,
    connect_dialog: Option<crate::widgets::connect_dialog::ConnectDialog>,
    connect_auth: Option<crate::remote_connect::SshAuth>,
    install_session: Option<crate::install_session::InstallSession>,
    /// Background self-update watcher, present only on a remote-launched
    /// croft (gated by `CROFT_REMOTE_AUTOUPDATE`). Polls the install-stamp
    /// the local cross-build writes when it finishes shipping a newer
    /// binary, so the running croft can re-exec into it in place.
    update_watch: Option<crate::update_watch::UpdateWatch>,
    /// When the build marker was last checked: it is a file read, so at
    /// most once a second rather than every loop iteration.
    build_marker_checked: Option<std::time::Instant>,
    /// One-shot startup probe (#242): does the repo this binary was built
    /// from now sit at a different commit/dirty state than the binary has
    /// baked in? Local-only — a remote-launched croft is re-stamped by its
    /// launcher instead. Dropped once its verdict is drained.
    drift_probe: Option<crate::update_watch::DriftProbe>,
    /// The repo's current hash label when the probe found drift ("def456",
    /// "def456-dirty"). Arms the "Ctrl+Shift+F9 to rebuild" status-bar
    /// hint. Kept through a running rebuild — the Idle guards hide the hint
    /// while it runs, and a failure then re-arms the updater for a retry
    /// instead of silently dropping the offer.
    local_drift: Option<String>,
    /// Background `cargo install --path <repo>` reinstalling the local
    /// croft after the user took the drift hint's offer. Reported through
    /// the same UpdateEvent lifecycle as the remote watcher.
    self_install: Option<crate::update_watch::SelfInstall>,
    /// One-shot startup check (#333): is a newer release published than
    /// the version this binary carries? Local-only, throttled to a daily
    /// network request by its cache file. Dropped once its verdict lands.
    update_check: Option<crate::update_check::UpdateCheck>,
    /// A check the user asked for with Help: Update croft when nothing was
    /// on offer (#1844), and how it asks the network (stubbed in tests).
    manual_update_check: Option<crate::update_check::ManualCheck>,
    update_fetch: fn() -> Result<Option<crate::update_check::Release>, String>,
    /// The "vX available" popup, present from the offer landing until the
    /// user picks Update, Skip, or (once staged) Relaunch; Later only folds
    /// it into its status-bar pill.
    update_toast: Option<UpdateToast>,
    /// Whether this binary may update itself, or belongs to a package
    /// manager that must do it (#375). Read once at startup.
    install_source: crate::update_check::InstallSource,
    /// A background `cargo install --root <cache>/staged` of the offered
    /// release. Reported through the same UpdateEvent lifecycle as the
    /// remote watcher; the binary on PATH is untouched until Relaunch.
    staged_install: Option<crate::update_check::StagedInstall>,
    /// The staged release build's own lifecycle (#333). `update_status`
    /// belongs to the remote watcher and the drift rebuild; the two cells
    /// are never written by the other's producer, so a rebuild starting or
    /// failing after the release landed cannot disarm Relaunch, and the
    /// reverse. Readers that mean "any update" consult both.
    staged_status: UpdateStatus,
    update_status: UpdateStatus,
    /// When a background self-update is in progress, the instant it started.
    /// Drives the spinning update glyph in the status bar (and the redraw
    /// cadence that animates it). `None` whenever no update is running.
    update_spinner_start: Option<std::time::Instant>,
    /// Set when an update landed and croft should re-exec after the main
    /// loop unwinds and the alt-screen is surrendered. Read by `run`.
    pending_reexec: bool,
    overlays: OverlayManager,
    /// VS Code-style Cmd+F in-editor find overlay. None when closed.
    pub editor_find: Option<crate::widgets::editor_find::EditorFind>,
    /// Cmd+F find-in-terminal overlay: searches the active pane's grid +
    /// scrollback (like iTerm2 / Ghostty scrollback search). Reuses the
    /// editor find widget; the replace row is never shown. None when closed.
    pub terminal_find: Option<crate::widgets::editor_find::EditorFind>,
    /// The pane index `terminal_find` opened on. Find is bound to that pane
    /// like quick-select and copy mode: a gesture that activates a sibling
    /// closes it (per-frame invariant in `sync_focus_flags`), so its
    /// highlight and jump anchor never stray onto another pane's grid.
    terminal_find_pane: usize,
    /// The active terminal-find match as `(clock, abs_grid_line, col, len)`,
    /// the anchor next/prev navigation walks from — the line is stamped with
    /// the scroll-clock reading of the snapshot it was found in, and every
    /// use re-bases it first, so output streaming under an open find bar
    /// cannot leave navigation walking from a phantom row. None until the
    /// first match.
    terminal_find_match: Option<(i64, i32, usize, usize)>,
    /// Ctrl+Shift+Space quick-select hint mode on the active terminal pane
    /// (WezTerm's quick-select): every labelled match on the viewport plus
    /// the label prefix typed so far. None when the mode is off.
    pub terminal_quick_select: Option<QuickSelectState>,
    /// Durable cross-session command history (atuin's model): every
    /// finished command from a shell-integrated pane, with cwd / exit /
    /// duration / timestamp, persisted as JSONL under the config dir.
    pub command_history: crate::command_history::CommandHistory,
    /// Ctrl+Shift+H search popup over [`Self::command_history`]. None when
    /// closed.
    pub command_history_popup: Option<crate::widgets::history_popup::HistoryPopup>,
    /// Where the terminal-session snapshot lives
    /// (`terminal_sessions.json`); a field so tests point it at a tempdir.
    pub terminal_session_path: PathBuf,
    /// Whether a restore brings back plain panes whose shells have ended as
    /// fresh shells (#1458): only over SSH, where a dropped connection should
    /// come back to its panel. Locally they would return empty, so they stay
    /// closed.
    pub terminal_restore_respawns: bool,
    /// The most recent terminal-session persistence failure, latched by
    /// `save_terminal_session` and cleared by a later success; composed
    /// into structural-change statuses by `terminal_status` so a corrupt
    /// store is never silently ignored (#157/#158).
    terminal_store_error: Option<String>,
    /// The persisted folder-set store (#153); a field so tests redirect it.
    pub workspace_folders_path: PathBuf,
    /// A pane reorder is pending a session write. `move_terminal` runs once
    /// per motion report during a drag, and the write is a full
    /// read-modify-write of the session file plus a cwd syscall per pane, so
    /// it is marked here and flushed once per tick instead of inline.
    pub terminal_session_dirty: bool,
    /// Ctrl+Shift+Y copy mode on the active terminal pane (WezTerm / tmux):
    /// vi keys walk a cursor through the scrollback, v / V / Ctrl+V select,
    /// y copies. None when the mode is off.
    pub terminal_copy_mode: Option<CopyModeState>,
    /// The user's terminal trigger set (`triggers.json`), pushed into every
    /// pane on the drain tick and swapped wholesale on config reload.
    pub triggers: std::sync::Arc<crate::triggers::TriggerSet>,
    /// The agents croft recognises in a pane's foreground (#344): built in
    /// plus `agents.json`, reloaded on save.
    agents: crate::agents::AgentTable,
    /// Agent transitions from the last sample, drained on the tick.
    agent_events: std::collections::VecDeque<crate::agents::AgentEvent>,
    /// User-defined problem matchers (#252): `matchers.json` (user config
    /// dir + workspace `.croft/`), batch-scanned at the FinishedCommand
    /// boundary alongside the built-in table. Swapped wholesale on reload.
    pub matchers: std::sync::Arc<crate::problem_matchers::MatcherSet>,
    /// The background-capable subset of `matchers`, pushed into every pane
    /// on the drain tick exactly like the trigger set.
    pub watch_set: std::sync::Arc<crate::problem_matchers::WatchSet>,
    /// tasks.json `problemMatcher` assignments: pane uid → the matcher the
    /// task running there declared. Exclusive for that pane's batch scans
    /// (VS Code's model: an explicit matcher replaces the defaults).
    task_matcher_by_pane:
        std::collections::HashMap<u64, std::sync::Arc<crate::problem_matchers::CompiledMatcher>>,
    /// Panes whose PROBLEMS batch a watch matcher currently owns. The
    /// watcher eventually exiting fires a FinishedCommand whose whole-
    /// history rescan would resurrect every error of every past cycle, so
    /// that one scan is skipped (the flag clears with it — the pane's next
    /// ordinary command scans normally).
    watch_published_panes: std::collections::HashSet<u64>,
    /// Broadcast input (Cmd+K I, iTerm2's Cmd+Opt+I): while on, shell-bound
    /// keystrokes and pastes in the terminal go to every pane instead of
    /// just the focused one. Session-scoped; never persisted.
    pub broadcast_input: bool,
    /// True while the "enable broadcast?" confirm popup is up (enabling is
    /// gated: the classic footgun is typing into panes you forgot were
    /// listening).
    pub pending_broadcast_enable: bool,
    /// A multi-line paste waiting on its confirm popup (#1274): written to
    /// the pane only once the user picks Paste or Paste as one line.
    pub pending_terminal_paste: Option<Vec<u8>>,
    /// Stable [`PtyTerminal::uid`] that requested `pending_terminal_paste`
    /// when it arrived asynchronously over the remote relay (#2967), so
    /// confirming after switching panes still writes to the requesting pane.
    pending_terminal_paste_target: Option<u64>,
    /// The `terminal_multiline_paste_warning` setting (#1274).
    pub multiline_paste_warning: crate::prefs::MultilinePasteWarning,
    /// A runnable fence waiting on the confirm popup (#353): destructive-
    /// looking blocks and `{confirm}` fences ask before they type.
    pending_run_block: Option<PendingRunBlock>,
    /// Blocks whose output croft is still waiting for (#354).
    pending_captures: Vec<PendingCapture>,
    /// Captured output per fence, keyed by the pane name (which already
    /// encodes the document's path under the workspace) and the fence
    /// opener's source line. In memory for the session unless the fence
    /// said `{persist}`.
    block_outputs: std::collections::HashMap<(String, usize), crate::markdown::BlockOutput>,
    /// Paint the terminal panes' right-edge arrival-time gutter ("Terminal:
    /// Toggle Timestamps"). Session-scoped, off by default.
    pub show_terminal_timestamps: bool,
    /// Notification sinks from config.json `notifications` (#358).
    notifier: crate::notifications::Notifier,
    /// Compiled per-host pane accent rules from config.json `host_accents`.
    /// First match wins.
    host_accents: Vec<HostAccent>,
    /// Recently closed terminal panes parked for the undo-close grace window
    /// (Ghostty's undo, iTerm2's Undo Close Session): the PTY and child stay
    /// alive but hidden until Cmd+K ⇧T restores the newest or the tick reaps
    /// entries older than [`UNDO_CLOSE_GRACE`]. Newest last.
    pub closed_terminals: Vec<ClosedTerminal>,
    /// Panes being ended on background jobs ([`App::end_terminal`]): their
    /// shell's hang-up grace and the wait for their reader never stop the
    /// UI. The tick clears the finished ones; quitting waits for them all.
    ending_terminals: Vec<crate::jobs::Job<()>>,
    /// Recently closed editor tabs (VS Code "Reopen Closed Editor"): the
    /// path plus enough view state (pin, cursor, scroll) to restore the tab
    /// as it was. Newest last; Cmd+K ⇧W pops. Bounded at
    /// [`CLOSED_TAB_STACK_CAP`]; entries whose file has since vanished are
    /// skipped at reopen time.
    pub closed_tabs: Vec<ClosedTab>,
    /// VS Code-style Cmd+P / Ctrl+P quick-open file finder. None when
    /// the modal is closed.
    pub file_finder: Option<crate::widgets::file_finder::FileFinder>,
    /// The range the last hinted quick-open pick installed, and the file it
    /// was installed in: the next pick clears it if it is still the
    /// selection there, and nothing else, so a selection the user made by
    /// hand (or extended from it) survives a re-pick of the same file.
    quick_open_range: Option<(PathBuf, crate::widgets::editor::EditorSelection)>,
    /// VS Code-style Cmd+Shift+P / Ctrl+Shift+P command palette. None when
    /// the modal is closed.
    pub command_palette: Option<crate::widgets::command_palette::CommandPalette>,
    /// VS Code-style Cmd+Shift+O / Ctrl+Shift+O "Go to Symbol in Editor"
    /// picker (also Go to Line via a `:` prefix). None when the modal is closed.
    pub go_to_symbol: Option<crate::widgets::symbol_picker::SymbolPicker>,
    /// "Go to Symbol in Workspace" (`#` in Quick Open): query + merged
    /// `workspace/symbol` results across every running language server.
    pub workspace_symbols: Option<crate::widgets::workspace_symbols::WorkspaceSymbolPicker>,
    /// The newest `workspace/symbol` request id; stale replies are dropped.
    ws_symbols_request_id: Option<u64>,
    /// Armed by a picker keystroke; `workspace_symbols_dispatch_due` fires
    /// the request once the query has been still for the debounce window.
    ws_symbols_pending: Option<std::time::Instant>,
    /// Receiver for an in-flight MCP sidecar command's result (the off-thread
    /// worker delivers one [`crate::mcp::McpOutcome`] then disconnects). `None`
    /// when no extension command is running; polled each frame by `poll_mcp`.
    mcp_rx: Option<std::sync::mpsc::Receiver<crate::mcp::McpOutcome>>,
    /// While an extension command runs: when it started (drives the status-bar
    /// spinner frame) and its human label (the animated badge text). Both `None`
    /// when idle; set in `spawn_mcp_command`, cleared in `poll_mcp`.
    mcp_started: Option<std::time::Instant>,
    mcp_busy_label: Option<String>,
    /// "Debug: Attach to Python Process" picker (PEP 768). None when closed.
    /// Lists running CPython 3.14+ processes; selecting one spawns
    /// `pdb -p <pid>` in a PTY.
    pub process_picker: Option<crate::widgets::process_picker::ProcessPicker>,
    /// Name of the launch.json configuration F5 starts. `None` = the
    /// zero-config "Debug active file" behavior.
    selected_debug_config: Option<String>,
    /// Name of the compound F5 starts, when the last picker launch was a
    /// compound. Exclusive with `selected_debug_config`: a compound and a
    /// configuration may share a name, so one string cannot say which (#567).
    selected_debug_compound: Option<String>,
    /// A config launch parked behind its `preLaunchTask`: the task runs in
    /// its pane, and the FinishedCommand sweep launches (exit 0) or aborts
    /// (non-zero) when the matching command completes. Replaced by a newer
    /// F5, so only one launch can be pending.
    pending_debug_launch: Option<PendingDebugLaunch>,
    /// The `${input:…}` values a launch or task is asking for (#1424).
    input_run: Option<InputRun>,
    /// An attach launch waiting for the user to pick its process (#250):
    /// the resolved config and where it launches once `processId` is known.
    pending_attach: Option<(crate::dap::configs::ResolvedConfig, LaunchSlot)>,
    /// In-flight background `cargo test --no-run` for debug-a-test: the
    /// receiver yields the picked binary (or the build error) and the test
    /// name rides along for the launch. Drained per tick; a second request
    /// while one is pending is refused with a status line.
    pending_test_debug: Option<PendingTestDebug>,
    /// CodeLLDB `cargo` launches building their programs on background
    /// threads (#1392): each receiver yields the executable (or the build
    /// error), and its config launches into its slot when it lands. One per
    /// compound member that builds; Stop Debugging drops them all.
    pending_cargo_launches: Vec<PendingCargoLaunch>,
    /// Debugger watch expressions (#112), in display order. Session-scoped.
    pub watch_exprs: Vec<String>,
    /// Latest evaluate result per expression: `(value, success)`.
    pub watch_vals: std::collections::HashMap<String, (String, bool)>,
    /// The previous stop's values, for the changed-value highlight.
    watch_prev: std::collections::HashMap<String, String>,
    /// Armed by every `Stopped` event, consumed by the first
    /// `InspectionUpdated` that follows: the baseline rotates exactly once
    /// per stop. `InspectionUpdated` also fires for frame switches and
    /// variable expansions within the same stop, and rotating on those
    /// clobbered the baseline with the stop's own values — the changed
    /// highlight never fired (#112).
    watch_baseline_pending: bool,
    /// zoxide-backed Explorer jump popup (Cmd+Z). None when closed. Opens
    /// only from the Explorer pane, so it never collides with the editor's
    /// Cmd+Z undo.
    pub zoxide_jump: Option<crate::widgets::zoxide_jump::ZoxideJump>,
    /// True while the zoxide popup is picking a folder to ADD to the
    /// workspace (#147) rather than re-rooting; reset on close.
    pub zoxide_add_folder: bool,
    pub branch_picker: Option<crate::widgets::branch_picker::BranchPicker>,
    /// Why the branch picker is open, so its Enter dispatches the right
    /// verb (checkout vs merge vs rebase vs delete vs create-from base).
    pub branch_purpose: BranchPurpose,
    /// The switch local changes blocked (#925), with the repository it is
    /// in and the branch it was refused on (None when detached), while the
    /// Stash & Checkout picker offers a way through.
    blocked_switch: Option<(
        PathBuf,
        Option<String>,
        crate::widgets::branch_picker::BranchAction,
    )>,
    /// Single-line input modal for SCM ops that need a typed value
    /// (clone URL, branch rename, remote name/URL, tag name).
    pub input_prompt: Option<crate::widgets::input_prompt::InputPrompt>,
    /// Single-choice picker for SCM ops that act on one of a git-owned list
    /// (apply/pop/drop a stash, delete a tag, remove/push-to a remote).
    pub list_picker: Option<crate::widgets::list_picker::ListPicker>,
    /// Privileged control channel to this session's host when running under
    /// a persistent multiplayer session (docs/MULTIPLAYER.md); drives the
    /// participants picker's grant/revoke/disconnect actions. None outside
    /// a session host.
    pub session_channel: Option<crate::session_host::InnerChannel>,
    /// Roster last read from the session host's presence sidecar.
    pub session_participants: Vec<crate::session_host::Participant>,
    /// Whether the host's stale-image marker (#238) has already been
    /// announced, so the status hint fires once per session, not per poll.
    session_host_stale_seen: bool,
    /// The stale-client notice the current roster warrants (#626, #652), so
    /// it is raised once when it changes, not on every roster churn.
    session_stale_notice: Option<String>,
    /// Multiplayer: the participant whose keystrokes are currently flowing
    /// into this croft (from the host's typing attribution frames).
    session_typist: Option<u64>,
    /// Multiplayer: every participant's last-known caret, stashed when the
    /// typist changes and restored when they type again, so interleaved
    /// input drives per-participant carets in the shared buffer.
    session_carets: std::collections::HashMap<u64, SessionCaret>,
    /// Sidecar mtime at the last roster read, so the poll is a cheap stat.
    session_presence_mtime: Option<std::time::SystemTime>,
    /// Cadence gate for the presence poll (stat every 500ms, not every tick).
    last_session_presence_poll: std::time::Instant,
    /// The multiplayer session: relay, merge bases, peer carets, AI stream
    /// (see [`agents_collab::CollabState`]).
    collab: agents_collab::CollabState,
    /// The AI navigator's seat, pair record, host lock, turn and notes
    /// (see [`agents_collab::PairState`]).
    pair: agents_collab::PairState,
    /// Test seam: the kernel bridge Select Kernel starts, by kernel name
    /// (the real one runs Python).
    #[cfg(test)]
    #[allow(clippy::type_complexity)]
    kernel_start_override: Option<Box<dyn Fn(&str) -> crate::notebook_kernel::KernelSession>>,
    /// Rolling log of git commands croft has run and their summaries, shown
    /// read-only by the "Show Git Output" action (VS Code's Git channel).
    pub git_output_log: Vec<String>,
    /// Build diagnostics from the problem matchers (#119): finished-command
    /// output scanned into PROBLEMS entries, keyed by resolved file path.
    /// Paired with the producer that installed it (the pane uid, or
    /// `PROJECT_CHECK_ID` for the whole-project sweep). Without the pairing
    /// a re-run removed the whole entry and took the OTHER producer's rows
    /// with it, and the second producer to reach a file could not clear its
    /// own rows at all (#532).
    build_diagnostics:
        std::collections::HashMap<PathBuf, Vec<(u64, crate::widgets::problems::ProblemItem)>>,
    /// Which files each terminal pane contributed, keyed by the pane's
    /// STABLE uid (Vec positions shift on close/insert/reorder), so a
    /// pane's next scanned command replaces its previous run's diagnostics
    /// (VS Code clears a task's problems when the task re-runs).
    build_diag_files_by_pane: std::collections::HashMap<u64, Vec<PathBuf>>,
    /// The HEAD a warned Undo Last Commit (#1350) is armed for: a commit
    /// already on its upstream is undone on the second ask at that HEAD.
    undo_pushed_commit_armed: Option<String>,
    /// What a warned Reset Current Branch to Here > Hard (#1627) is armed
    /// for: the repo root, the commit, and the working tree's
    /// [`crate::git::worktree_fingerprint`] when it warned. It runs on the
    /// second ask only while all three still match, so work made since the
    /// warning is warned about afresh.
    reset_hard_armed: Option<(PathBuf, String, String)>,
    /// True while the "Discard All Changes" confirmation modal is up.
    pub pending_discard_all: bool,
    /// Drop Stash, picked but not yet confirmed (#1128).
    pending_stash_drop: Option<PendingStashDrop>,
    /// The Replace All confirmation (#123): `(occurrences, files, dirty
    /// files to be skipped)` counted without writing; the modal shows them,
    /// Enter/Y commits, Esc cancels.
    pending_replace_all: Option<(usize, usize, usize)>,
    /// Cached workspace file index. Built lazily on first Cmd+P and
    /// shared across reopens so repeated invocations are instant.
    file_finder_index: Option<std::sync::Arc<Vec<crate::file_index::FileEntry>>>,
    /// The background build of that index: App::new starts one that walks
    /// the workspace, so Cmd+P opens against the hot index instead of
    /// walking on the UI thread, and file changes start rebuilds. None once
    /// the index has landed, or the build's thread died (#2618), or no build
    /// was started.
    file_finder_index_job: Option<crate::jobs::Job<Vec<crate::file_index::FileEntry>>>,
    /// Set when a rebuild was requested while one was already in flight.
    /// poll_file_finder_index starts it once that one ends, landed or
    /// crashed (#2618), so a long-running rebuild (multi-GB monorepo) can
    /// never miss a later FS event that landed mid-walk.
    file_finder_index_dirty: bool,
}

#[derive(Clone, Copy, Debug, Default)]
struct EditorVimChord {
    op: VimOp,
    count: usize,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EditorImageLayout {
    pub cell_x: u16,
    pub cell_y: u16,
    pub cell_w: u16,
    pub cell_h: u16,
    pub path: PathBuf,
    /// Which page of a PDF is baked into this overlay (1 for anything else).
    /// Part of the re-emit key so paging re-bakes on its own: without it the
    /// path and rect are unchanged on a page turn and the stale picture stays
    /// on screen unless every call site remembers to invalidate the layout.
    pub page: u32,
    /// Content stamp of the baked bytes ([`crate::widgets::editor::ImageView::generation`]).
    /// Part of the re-emit key so a file rebuilt on disk re-bakes even when
    /// the path, rect and page are all unchanged (a pdflatex rebuild lands
    /// on the same page the reader was on).
    pub generation: u64,
}

/// Re-emit key for the terminal pane's inline-image overlay: the cell rect
/// the picture occupies plus which image (per-pane `seq`) and pane it is.
/// Scrolling moves the anchor row, changing `cell_y` — a layout mismatch —
/// so the overlay clears and re-emits at the new position.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TerminalImageLayout {
    pub cell_x: u16,
    pub cell_y: u16,
    pub cell_w: u16,
    pub cell_h: u16,
    pub seq: u64,
    pub pane: usize,
}

/// Markdown-preview inline image placement (#176): geometry plus the
/// bake identity (path + mtime), so an externally rewritten picture
/// re-bakes and a pure scroll reuses the payload.
#[derive(Clone, Debug, PartialEq)]
pub struct MarkdownImageLayout {
    pub cell_x: u16,
    pub cell_y: u16,
    pub cell_w: u16,
    pub cell_h: u16,
    pub path: PathBuf,
    pub mtime_ms: u128,
}

/// Cached full-document raster for the minimap. The expensive part (walking
/// every line's syntax spans) runs only when `sig` changes; scrolling reuses
/// `rgba` and just re-composites the viewport box on top.
struct MinimapBase {
    rgba: Vec<u8>,
    w: u32,
    h: u32,
    /// (path, edit_seq, canvas_w_px, canvas_h_px, bg, lines drawn,
    /// highlight generation) — a change rebakes. The lines drawn tell a
    /// symbol tab (#369) from its file's tab; the highlight generation moves
    /// when a syntax pass lands after its edit (#850).
    sig: MinimapSig,
}

/// Re-emit key for the minimap overlay. Differs from the base sig: the
/// viewport fields move on scroll (recomposite, no rebake), the cell fields
/// move on resize/layout.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MinimapLayout {
    cell_x: u16,
    cell_y: u16,
    cell_w: u16,
    cell_h: u16,
    top: usize,
    rows: usize,
    total: usize,
    edit_seq: u64,
    /// The editor's `highlight_generation`: a background syntax pass that
    /// lands after the edit (#850) recolors the strip.
    highlight_generation: u64,
    side: MinimapSide,
    /// Theme editor bg; a change here (theme switch) forces a rebake.
    bg: (u8, u8, u8),
    /// Active selection `(start_row, end_row)`; a change recomposites the band.
    selection: Option<(usize, usize)>,
    /// Which document and which of its lines the strip draws: a hash of
    /// the path (no per-frame allocation) and the drawn span, so switching
    /// between two tabs whose other fields agree (two same-length symbol
    /// tabs, #369, or two same-length files) still re-emits. A tab without
    /// a path is only ever the empty fallback left by closing the last
    /// tab, whose strip is the same for every one.
    doc: (u64, (usize, usize)),
}

/// One socket-poll result. `hits` is the pane-subtree scan that decides what to
/// surface; `listening` is every loopback port on the box, the only set that can
/// say a port has stopped. `None` when neither probe ran.
struct PortPoll {
    hits: Vec<crate::port_detect::PortHit>,
    listening: Option<std::collections::HashSet<u16>>,
}

/// The per-frame minimap overlays (everything not baked into the raster).
struct MinimapOverlay {
    /// First visible line, and number of visible lines (the viewport box).
    top: usize,
    rows: usize,
    total: usize,
    light: bool,
    /// Active selection `(start_row, end_row)`, marked by a thin left accent.
    selection: Option<(usize, usize)>,
    sel_rgb: (u8, u8, u8),
    /// Pixel height the lines are mapped into (top-aligned); `<= h` for short
    /// files so the viewport box and selection accent track the raster.
    content_h: u32,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct WelcomeLayout {
    cell_x: u16,
    cell_y: u16,
    cell_w: u16,
    cell_h: u16,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ScpUpload {
    pub alias: String,
    pub src: PathBuf,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PendingRemotePull {
    pub request_id: String,
    pub src_display: String,
    pub basename: String,
    pub dest_dir: PathBuf,
    pub started_at: std::time::Instant,
    pub kind: RemotePullKind,
    /// When a `Forward` pull was triggered by a loopback URL (e.g.
    /// `https://localhost:8443/docs?x=1#sec`), open this original URL
    /// rewritten to `127.0.0.1:<local_port>` once the tunnel comes up so the
    /// scheme, path, query and fragment are preserved.
    pub open_after_forward: Option<String>,
    /// True when a `Forward` pull targets remote IPv6 loopback (`[::1]`)
    /// rather than IPv4 (`127.0.0.1`).
    pub remote_ipv6: bool,
}

struct RemoteLaunch {
    host: String,
    path: Option<String>,
    adopted: Option<crate::remote::AdoptedMaster>,
}

/// A finished SARIF source download (#577).
struct SarifDownload {
    uri: String,
    /// 1-based position to open at.
    line: i64,
    col: i64,
    body: Result<Vec<u8>, String>,
}

fn split_at_char_count(s: &str, count: usize) -> (String, String) {
    let left: String = s.chars().take(count).collect();
    let right: String = s.chars().skip(count).collect();
    (left, right)
}

fn wrap_cells_variable_width(text: &str, first_width: u16, rest_width: u16) -> Vec<String> {
    let first_width = first_width.max(1) as usize;
    let rest_width = rest_width.max(1) as usize;
    let trimmed = text.trim();
    if trimmed.is_empty() {
        return vec![String::new()];
    }

    let mut lines = Vec::new();
    let mut current = String::new();
    for word in trimmed.split_whitespace() {
        let mut remaining = word.to_string();
        while !remaining.is_empty() {
            let width = if lines.is_empty() {
                first_width
            } else {
                rest_width
            };
            let sep = usize::from(!current.is_empty());
            let remaining_len = remaining.chars().count();
            let current_len = current.chars().count();
            if current_len + sep + remaining_len <= width {
                if sep == 1 {
                    current.push(' ');
                }
                current.push_str(&remaining);
                break;
            }
            if !current.is_empty() {
                lines.push(std::mem::take(&mut current));
                continue;
            }
            let (head, tail) = split_at_char_count(&remaining, width);
            lines.push(head);
            remaining = tail;
        }
    }
    if !current.is_empty() {
        lines.push(current);
    }
    if lines.is_empty() {
        vec![String::new()]
    } else {
        lines
    }
}

/// Where Croft Loch's welcome stack puts each block, in rows from the top of
/// the welcome area. Computed by [`loch_welcome_plan`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct LochWelcomePlan {
    /// The logo's first row and its height: one row for the text wordmark,
    /// 4..=14 for the picture.
    logo_y: u16,
    logo_h: u16,
    tagline_y: u16,
    /// The Start list's heading row, when the list shows.
    start_y: Option<u16>,
    /// The Take the Tour chip's row, when the Start list cannot show it.
    chip_y: Option<u16>,
    /// The release card's top row and height, and how many notes it shows
    /// whole (`notes == 0`: no card).
    card_y: u16,
    card_h: u16,
    notes: usize,
}

use crate::gradient::{GRAD_TL, GRAD_TR, POPUP_SEL_BG, paint_gradient_box, rgb_color};

/// A language server's `window/showMessageRequest` waiting for an answer
/// (#1599). Dropping it answers "no action".
struct LspQuestion {
    server: String,
    text: String,
    actions: Vec<lsp_types::MessageActionItem>,
    reply: tokio::sync::oneshot::Sender<Option<lsp_types::MessageActionItem>>,
}

/// An accepted completion waiting on the auto-import its server sends only
/// from `completionItem/resolve` (#928).
struct PendingCompletionResolve {
    request_id: u64,
    path: PathBuf,
    /// Where the accepted text begins; the import must end before it.
    start: (usize, usize),
    /// The caret after the accept, and a hash of the text before it.
    upto: (usize, usize),
    text_hash: u64,
    undo: ImportUndo,
}

fn open_url(url: &str) -> Result<()> {
    #[cfg(target_os = "macos")]
    let mut cmd = {
        let mut c = std::process::Command::new("open");
        c.arg(url);
        c
    };
    #[cfg(target_os = "linux")]
    let mut cmd = {
        let mut c = std::process::Command::new("xdg-open");
        c.arg(url);
        c
    };
    #[cfg(target_os = "windows")]
    let mut cmd = {
        let mut c = std::process::Command::new("cmd");
        c.args(["/C", "start", "", url]);
        c
    };
    #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
    let mut cmd = {
        let mut c = std::process::Command::new("open");
        c.arg(url);
        c
    };
    // Detach the opener's stdio: croft is on the alternate screen in raw
    // mode, and `open`/`xdg-open` chatter ("No application knows how to
    // open URL ...") would land straight on the grid.
    cmd.stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null());
    cmd.spawn().with_context(|| format!("opening {url}"))?;
    Ok(())
}

/// Short label for this machine, shown as the origin of a remote port in the
/// PORTS panel (`⇄ build-box`). Resolved once via `hostname`, falling back to a
/// generic word so the column is never blank.
fn remote_host_label() -> String {
    static HOST: std::sync::OnceLock<String> = std::sync::OnceLock::new();
    HOST.get_or_init(|| {
        std::process::Command::new("hostname")
            .output()
            .ok()
            .and_then(|o| String::from_utf8(o.stdout).ok())
            .map(|s| s.trim().split('.').next().unwrap_or("remote").to_string())
            .filter(|s| !s.is_empty())
            .unwrap_or_else(|| String::from("remote"))
    })
    .clone()
}

impl App {
    pub fn new(root: PathBuf) -> Result<Self> {
        // Reap any js-debug adapter tree a previous croft left orphaned (a
        // crash or force-quit skips `DapTransport`'s `Drop`, so the server +
        // its detached watchdog get reparented to init and linger). Runs on a
        // detached thread to stay off the time-to-first-frame critical path,
        // and is safe even while another croft session is live: only processes
        // reparented to init are touched. See `crate::dap::reaper`.
        crate::dap::reaper::reap_orphaned_js_debug_async(std::time::Duration::ZERO);
        let tree = FileTree::new(root.clone());
        let search = SearchPanel::new(root.clone());
        let remote = RemotePanel::new();
        let source_control = SourceControlPanel::new();
        let run_debug = RunDebugPanel::new();
        let extensions = crate::widgets::extensions::ExtensionsPanel::new();
        let outline = OutlinePanel::new();
        let open_editors = OpenEditorsPanel::new();
        let timeline = TimelinePanel::new();
        let mut dependencies = DependenciesPanel::new();
        // Detect the workspace's package ecosystems up front so the DEPENDENCIES
        // section's visibility, ⋯-menu entry, and header are correct before the
        // first sync tick. Re-detected on every re-root (see sync_explorer_panels).
        let dep_ecosystems = crate::dependencies::detect_ecosystems(&root);
        dependencies.set_header(crate::widgets::dependencies::header_label(&dep_ecosystems));
        // The active profile (#618) decides which settings, keybindings and
        // snippets files are read below, so it is chosen first. Never under
        // test: a developer's own profile choice must not leak into the suite.
        if !cfg!(test) {
            crate::profiles::activate_for_workspace(&root);
        }
        // Layered settings (#251): defaults ← user ← user-local ← workspace
        // layers of the primary root, one merged view. Warnings (refused
        // workspace keys, parse errors) land in the Settings OUTPUT channel.
        let merged_settings = crate::config_layers::load_merged(Some(&root));
        for w in &merged_settings.warnings {
            crate::output::push("Settings", crate::output::OutputLevel::Warn, w);
        }
        let loaded_prefs = merged_settings.prefs.clone();
        // Built with the defaults, then given the loaded settings by the one
        // apply step a reload also runs, at the end of this function: a
        // setting read here and missed on reload would show at once
        // (#2062 settings card step 2). `settings_to_build_with` names the
        // few that hold their loaded value while the App is built, and why.
        let built = settings_to_build_with(&loaded_prefs);
        // Before the first pane opens below: it takes its settings from here,
        // reading no settings file of its own (#2905).
        crate::widgets::terminal::publish_pane_settings(&loaded_prefs);
        let data_dir =
            (!cfg!(test)).then(|| user_data_dir(&croft_cache_dir(), &crate::paths::state_dir()));
        let notes_path = data_dir
            .as_deref()
            .map(|dir| crate::sticky_notes::Notes::store_path(dir, &root));
        if !cfg!(test) {
            crate::i18n::init(loaded_prefs.locale.as_deref());
        }
        // Seed the log view's opening default from the saved preference
        // BEFORE any log can be opened (#466): the toggle keeps the two in
        // step afterwards, but a startup that set only `App::log_highlight`
        // opened the first log highlighted despite a saved opt-out. Test-
        // inert like the keymap and matcher loads below: the suite builds
        // hundreds of apps on parallel threads, and an unguarded write to
        // the process-wide default from each would race the tests that
        // read it; those call `seed_log_highlight_default` explicitly.
        seed_log_highlight_default_at_startup(!built.disable_log_highlight);
        // Same treatment for keybindings: a row croft refused (an unknown
        // command id, a gesture that can never fire, a reserved bare click)
        // used to vanish silently, which reads as croft being broken rather
        // than the binding being wrong (#259).
        // Under test an empty keymap, exactly like the matcher set below: the
        // developer's real keybindings.json must never steer app tests. That
        // mattered less when a user keymap only rebound key chords, which no
        // test dispatches; a mouse row now gates an early return at the top
        // of `handle_mouse` that every mouse test flows through, so one line
        // in a file the suite does not control could redirect thousands of
        // tests. Tests that want a keymap assign `app.keymap` themselves.
        let loaded_keymap = if cfg!(test) {
            crate::keymap::Keymap::default()
        } else {
            crate::keymap::Keymap::load(&crate::keymap::keybindings_path())
        };
        // `Keymap::load` already reports its own warnings to OUTPUT ·
        // Keybindings, so nothing to do here.
        // Problem matchers (#252). Under test an empty set: the developer's
        // real matchers.json must never steer app tests; tests inject their
        // own. Compile warnings surface once, in OUTPUT · Matchers.
        let matchers = std::sync::Arc::new(if cfg!(test) {
            crate::problem_matchers::MatcherSet::default()
        } else {
            crate::problem_matchers::MatcherSet::load(
                &crate::problem_matchers::matchers_path(),
                Some(&crate::problem_matchers::workspace_matchers_path(&root)),
            )
        });
        for w in &matchers.warnings {
            crate::output::push("Matchers", crate::output::OutputLevel::Warn, w);
        }
        let watch_set = std::sync::Arc::new(matchers.watch_set());
        crate::startup_trace::mark("settings, keymap and matchers loaded");
        let explorer_views = ExplorerViewVisibility::from_prefs(built.explorer_views);
        let disabled_extensions = loaded_prefs.disabled_extensions.clone();
        // Under test only the bundled set: an extension the developer added
        // must never steer app tests; a test adds what it needs.
        let installed_extensions = if cfg!(test) {
            crate::mcp::catalog::bundled_ids()
        } else {
            crate::mcp::catalog::installed_ids(&crate::lsp::manifest::user_extensions_dir())
        };
        let (history_done_tx, history_done_rx) = std::sync::mpsc::channel();
        let (project_hint_tx, project_hint_rx) = std::sync::mpsc::channel();
        let (test_jump_tx, test_jump_rx) = std::sync::mpsc::channel();
        let (blame_tx, blame_rx) = std::sync::mpsc::channel();
        let editor = EditorTabs::new();
        // Bound before the first pane, so that pane's shell carries the
        // socket like every later one. `spawn_with_preamble` reads
        // `view_ipc::SOCK_PATH` at spawn time, so this ordering is what
        // decides whether the startup pane can use `croft view` at all (#362).
        let view_listener = Self::bind_view_socket();
        // A second croft on the same workspace finds the socket live and
        // leaves it to the first: one editor answers a workspace's agents.
        let (hook_listener, hook_bind_error) = if cfg!(test) {
            (None, None)
        } else {
            match crate::agent_approval::bind(&root) {
                Ok(listener) => (Some(listener), None),
                Err(e) => (None, hook_bind_status(&e)),
            }
        };
        let view_bind_error = VIEW_BIND_ERROR.lock().unwrap().take();
        crate::startup_trace::mark("sockets bound");
        let term = PtyTerminal::new(&root).context("spawning terminal")?;
        crate::startup_trace::mark("terminal shell spawned");

        // Background git worker: every `git status` / `git status
        // --porcelain` shell-out goes through this channel instead of
        // running on the UI thread. The very first request seeds the
        // status-bar badge; subsequent ones come from the FS watcher
        // and from sidebar-icon clicks (see `refresh_source_control`,
        // `refresh_git_status_debounced`). Removes the 15-50 ms
        // input-to-paint stall that used to fire on every Source
        // Control click.
        let git = GitWorker::spawn(root.clone());

        let welcome = WelcomeState::new();
        let ext_index_refresh = Some(crate::mcp::registry_index::spawn_refresh());

        let (search_query_tx, search_query_rx) = std::sync::mpsc::channel();
        let (search_results_tx, search_results_rx) = std::sync::mpsc::channel();
        let search_root = root.clone();
        std::thread::spawn(move || {
            crate::search::search_worker_loop(search_root, search_query_rx, search_results_tx);
        });
        // Pre-build the Cmd+P file index on a background job so the
        // first Cmd+P keystroke does not stall the UI for the length of a
        // workspace walk (a home folder's included), started after the
        // first frame. open_file_finder calls poll_file_finder_index to
        // take it; if no thread can start, the first Cmd+P walks instead.
        let index_root = root.clone();
        let file_finder_index_job = crate::jobs::Job::spawn(
            FILE_INDEX_THREAD,
            crate::jobs::Start::AfterFirstFrame,
            move || crate::file_index::build_file_index(&index_root),
        )
        .ok();
        let fs_watch = FsWatch::spawn(&root, &tree);
        crate::startup_trace::mark("file watcher started");
        // Persisted layout preferences (Customize Layout popup). Skipped under
        // test so the suite never reads/writes the developer's ~/.config,
        // mirroring the theme load above.
        let layout_prefs = if cfg!(test) {
            crate::prefs::LayoutPrefs::default()
        } else {
            loaded_prefs.layout
        };
        let session = HostSession::from_env();
        let persistence_warning = remote_persistence_status(session.is_remote, session.persistent);
        let terminal_restore_respawns = session.is_remote;
        let mut app = Self {
            tree,
            search,
            remote,
            source_control,
            commit_graph: crate::widgets::commit_graph::CommitGraphPanel::new(),
            graph_revspec: None,
            graph_fetch: None,
            graph_refetch_queued: false,
            run_debug,
            testing: crate::widgets::testing::TestingPanel::new(),
            codeql: crate::widgets::codeql::CodeqlPanel::new(),
            test_worker: crate::startup_trace::after(
                "test runner started",
                crate::testing::worker::TestWorker::spawn(root.clone()),
            ),
            test_run_status: None,
            last_test_run: None,
            pane_run_statuses: Vec::new(),
            extensions,
            vscode_listed: Vec::new(),
            disabled_extensions,
            installed_extensions,
            // Built from `loaded_keymap` above, which applies the same
            // cfg!(test) guard main landed here independently: the
            // developer's real keybindings.json must never steer app tests.
            // Hoisted rather than inlined so the parse warnings can be
            // reported once, in OUTPUT · Keybindings.
            keymap: loaded_keymap,
            last_mouse_pos: None,
            tab_click_target: None,
            macro_recording: None,
            macro_last: None,
            macro_registers: crate::startup_trace::after(
                "macros loaded",
                crate::macros::load(&crate::macros::macros_path()),
            ),
            macro_replaying: false,
            macros_path: crate::macros::macros_path(),
            config_watch: crate::startup_trace::after(
                "settings watcher set up",
                crate::config_sync::ConfigWatch::new(config_watch_paths(&merged_settings.chain)),
            ),
            snippets: crate::startup_trace::after(
                "snippets loaded",
                load_all_snippets([root.as_path()]),
            ),
            format_on_save: built.format_on_save,
            default_formatter: built.default_formatter.clone(),
            language_formatters: built.language_formatters.clone(),
            foreign_formatters_noted: std::collections::BTreeSet::new(),
            language_prefs: built.languages.clone(),
            copy_on_select: built.copy_on_select,
            multi_cursor_persist: built.multi_cursor_persist,
            files_exclude: Vec::new(),
            files_associations: std::collections::BTreeMap::new(),
            files_associations_roots: Vec::new(),
            files_encoding: None,
            log_highlight: !built.disable_log_highlight,
            secret_redaction: !built.disable_secret_redaction,
            redaction_reveal_until: None,
            auto_save: built.auto_save,
            auto_save_on_focus_change: built.auto_save_on_focus_change,
            last_focus_signature: None,
            save_after_format: None,
            prettier_job: None,

            format_save_deadline: None,
            outline,
            open_editors,
            timeline,
            agent_lane_panel: crate::widgets::agent_lane::AgentLanePanel::new(),
            lane_change_counts: std::collections::HashMap::new(),
            dependencies,
            dep_ecosystems,
            explorer_views,
            settings_chain: merged_settings.chain,
            settings_provenance: merged_settings.provenance,
            applied_settings: built.clone(),
            timeline_fetch: None,
            codeql_jobs: codeql::CodeqlJobs::new(built.codeql_cli_path.clone()),
            history_done_rx,
            history_done_tx,
            project_check: None,
            project_check_rows: 0,
            project_hint_rx,
            project_hint_tx,
            project_check_hinted: false,
            problems_project_scope: if built.problems_project_scope.is_empty() {
                String::from("auto")
            } else {
                built.problems_project_scope.clone()
            },
            build_column_unit: crate::build_matchers::ColumnUnit::from_pref(
                &built.build_column_unit,
            ),
            last_problem_jump: None,
            test_jump_rx,
            test_jump_tx,
            timeline_fetched: None,
            nested_repo_scan: None,
            blame_rx,
            blame_tx,
            blame_fetched: None,
            blame_hover: None,
            git_net_job: None,
            git_view_job: None,
            git_file_lookup: None,
            git_file_queue: std::collections::VecDeque::new(),
            vim_mode_pref: false,
            inline_blame_enabled: !built.disable_inline_blame,
            provenance_overlay: false,
            indent_guides_enabled: !built.disable_indent_guides,
            home_walks_rows: built.home_walks_wrapped_rows,
            bracket_colors_enabled: !built.disable_bracket_colors,
            whitespace_mode: crate::widgets::editor::WhitespaceMode::from_pref(
                &built.render_whitespace,
            ),
            rulers: crate::widgets::editor::normalize_rulers(&built.rulers),
            line_numbers: crate::widgets::editor::LineNumbers::from_pref(&built.line_numbers),
            diff_ws_default: crate::widgets::diff::DiffWhitespace::from_config(
                &built.diff_ignore_whitespace,
            ),
            inline_values_enabled: !built.disable_inline_values,
            save_before_debug: !built.disable_save_before_debug,
            auto_close_pairs: !built.disable_auto_close_pairs,
            paste_reindent: !built.disable_paste_reindent,
            save_cleanup: crate::widgets::editor::SaveCleanup::from_prefs(&built),
            inlay_hints_enabled: !built.disable_inlay_hints,
            // Keep the suite off the user's real ~/.config/croft/history: a
            // per-process temp root in test builds, the real dir otherwise.
            history_root: if cfg!(test) {
                std::env::temp_dir().join(format!("croft-test-history-{}", std::process::id()))
            } else {
                crate::history::history_dir()
            },
            history_restore: None,
            deps_fetch: None,
            deps_fetched: None,
            voice_handle: None,
            voice_canceling: false,
            voice_ready: None,
            editor,
            editor_layout: editor_layout::EditorLayout::single(),
            force_save_armed: false,
            sudo_save_armed: None,
            pending_sudo_saves: Vec::new(),
            terminals: vec![term],
            active_terminal: 0,
            perf: PerfHud::default(),
            screencast: Screencast::default(),
            roots: crate::workspace::WorkspaceRoots::single(root.clone()),
            sidebar_view: SidebarView::Explorer,
            sidebar_areas: SidebarAreas::default(),
            // Read the persisted theme from the real config only outside tests;
            // under test fall back to the default so the suite stays hermetic
            // and never depends on (nor mutates) the developer's ~/.config. This
            // mirrors the cfg!(test) write-skip in `apply_theme`.
            theme: if cfg!(test) {
                crate::theme::Theme::default()
            } else {
                built.theme()
            },
            focus: Pane::Tree,
            show_tree: true,
            show_terminal: true,
            terminal_maximized: false,
            activity_bar_visible: layout_prefs.activity_bar,
            status_bar_visible: layout_prefs.status_bar,
            side_bar_position: layout_prefs.side_bar_position,
            secondary_side_bar_visible: layout_prefs.secondary_side_bar,
            panel_alignment: layout_prefs.panel_alignment,
            quick_input_position: layout_prefs.quick_input_position,
            centered_layout: layout_prefs.centered_layout,
            zen_center_layout: layout_prefs.zen_center_layout,
            zen_centered: false,
            centered_layout_width: layout_prefs.centered_layout_width,
            zen_esc_at: None,
            zen_hint_until: None,
            sidebar_auto_hide: built.sidebar_auto_hide,
            sidebar_auto_hide_suspended: false,
            sidebar_pinned_open: false,
            sidebar_dwell: SidebarDwell::default(),
            pre_zen: None,
            layout_icon_areas: LayoutIconAreas::default(),
            status: match (
                merged_settings.user_config_error.as_deref(),
                hook_bind_error,
            ) {
                (Some(error), _) => settings_not_loaded_status(error),
                (None, Some(hook)) if view_bind_error.is_none() => hook,
                _ => view_status(view_bind_error.as_deref()),
            },
            persistence_warning,
            quit: false,
            drop_to_local: false,
            session,
            context_menu: None,
            crumb_filter: (String::new(), Vec::new()),
            prompt: None,
            fs_watch,
            fs_watch_extra: Vec::new(),
            git_extra: Vec::new(),
            active_scm_root: root.clone(),
            scm_pin: None,
            scm_follow_root: root.clone(),
            active_test_root: root.clone(),
            test_root_follow: None,
            test_root_pending: None,
            pending_workspace_folders: None,
            git_head_oids: std::collections::BTreeMap::new(),
            git,
            cursor_blink: CursorBlink::new(),
            overwrite_mode: false,
            caret_block_sent: false,
            editor_click: ClickTracker::default(),
            tree_click: ClickTracker::default(),
            terminal_click: ClickTracker::default(),
            modified_click: ModifiedClickTracker::default(),
            ports_click: ClickTracker::default(),
            gesture: None,
            secondary_outline: Default::default(),
            welcome,
            ext_index_refresh,
            ext_index_manual_refresh: false,
            search_query_tx,
            search_results_rx,
            cell_pixel: None,
            chrome_bake: None,
            chrome_shown: None,
            chrome_bake_next: None,
            welcome_bake: None,
            last_pdf_wheel: None,
            formula_overwrite_armed: false,
            activity_badges: Default::default(),
            problems_badge_count: 0,
            problems_badge_cell: None,
            inline_protocol: crate::iterm2_inline::detect_inline_image_protocol(),
            sixel_supported: false,
            resize_repaint_needed: false,
            minimap: inline_images::MinimapState::new(layout_prefs.minimap),
            picture_cover: popups::PictureCover::default(),
            chrome_sent: std::collections::HashMap::new(),
            chrome_seen: std::collections::HashSet::new(),
            clipboard_reader: read_system_clipboard,
            remote_launch: None,
            pending_remote_launch_host: None,
            pending_remote_launch_path: None,
            ssh_offer: None,
            ssh_offer_seen: std::collections::BTreeMap::new(),
            remote_offer_disabled: built.disable_remote_offer,
            remote_offer_excluded: built.remote_offer_excluded_hosts.clone(),
            fleet_groups: built.fleet_groups.clone(),
            code_scanning: built.code_scanning,
            code_scan_seen: None,
            code_scan_list: None,
            code_scan_fetch: None,
            remote_offer_refused: crate::startup_trace::after(
                "refused remote hosts read",
                crate::remote::load_refused_hosts(
                    &crate::remote::refused_hosts_path(&croft_cache_dir()),
                    std::time::SystemTime::now(),
                ),
            ),
            lane_agent: built.lane_agent.clone(),
            color_depth: built
                .color_depth
                .as_deref()
                .and_then(crate::color_depth::ColorDepth::from_setting)
                .unwrap_or_else(|| {
                    crate::color_depth::ColorDepth::detect(|name| std::env::var(name).ok())
                }),
            underline_colors: crate::color_depth::underline_colors_supported(|name| {
                std::env::var(name).ok()
            }),
            lane_panes: std::collections::BTreeMap::new(),
            tree_clipboard: None,
            tree_typeahead: None,
            compare_anchor: None,
            last_frame_area: Rect::default(),
            last_sidebar_area: None,
            pending_scp_uploads: Vec::new(),
            pending_remote_pulls: Vec::new(),
            pending_relay_copies: Vec::new(),
            view_listener,
            hook_listener,
            hook_root: root.clone(),
            approvals: std::collections::VecDeque::new(),
            approval_check: None,
            approval_ui: None,
            approval_edit: None,
            approval_merge_into: None,
            notebook_kernels: std::collections::HashMap::new(),
            kernel_listing: None,
            kernel_choice: None,
            coverage_at: None,
            coverage_lens_path: None,
            coverage_install: None,
            view_drain_budget: std::time::Duration::from_millis(20),
            pending_local_open: None,
            pending_discard: None,
            pending_unsaved_scm: None,
            remote_tag_root: None,
            scm_unsaved_cleared: false,
            scm_after_format_save: None,
            pending_revert_hunk: None,
            pending_unsaved: None,
            pending_lossy_save: None,
            remote_handoff_ok: None,
            hot_exit_dir: data_dir.as_deref().map(|dir| dir.join("hot-exit")),
            hot_exit_written: None,
            hot_exit_retry_at: None,
            hot_exit_consumed: Vec::new(),
            hot_exit_unremoved: Vec::new(),
            exit_notes: Vec::new(),
            session_dir: data_dir.as_deref().map(|dir| dir.join("sessions")),
            saved_session: None,
            closed_bookmarks: std::collections::HashMap::new(),
            run_tasks: Vec::new(),
            last_task: None,
            pending_terminal_warning: false,
            message_card: None,
            default_branch_label: None,
            default_branch_ask: None,
            trust_local_browser: false,
            sidebar_width: SIDEBAR_WIDTH_DEFAULT,
            terminal_height: None,
            editor_seams: Vec::new(),
            editor_layout_area: Rect::default(),
            sidebar_splitter_x: None,
            terminal_splitter_y: None,
            // From the merged settings read above, as a reload takes them
            // (#2920): reading `config.json` again here missed
            // `config.local.json` and cost a second read before the first frame.
            consented_extensions: built.mcp_consented.clone(),
            config_dir: crate::paths::config_dir(),
            settings_profile: if cfg!(test) {
                None
            } else {
                crate::profiles::active()
            },
            scrollback_dir: crate::session_state::dirs_cache_croft().join("scrollback"),
            terminal_rail_scroll: 0,
            terminal_rail_revealed: None,
            terminal_pane_maximized: false,
            bottom_panel_tab: BottomPanelTab::Terminal,
            problems: crate::widgets::problems::ProblemsPanel::new(),
            output: crate::widgets::output::OutputPanel::new(),
            ports: crate::widgets::ports::PortsPanel::new(),
            captures: crate::widgets::captures::CapturesPanel::new(),
            port_toast: None,
            last_port_poll: std::time::Instant::now(),
            port_poll: None,
            fleet_run: None,
            fleet_command: String::new(),
            last_label_refresh: std::time::Instant::now(),
            label_pass: None,
            panel_hits: PanelHitAreas::default(),
            pointer_cell: None,
            hovered_activity_icon: None,
            hovered_layout_icon: None,
            osk: None,
            osk_auto: crate::iterm2_inline::detect_osk_auto(),
            last_content_width: 0,
            last_content_height: 0,
            lsp: language_servers::LspState::new(crate::startup_trace::after(
                "language server manager built",
                match crate::lsp::LspManager::new(root.clone()) {
                    Ok(m) => Some(m),
                    Err(e) => {
                        eprintln!("lsp manager init failed: {e}");
                        None
                    }
                },
            )),
            shell_lint_notice_shown: false,
            tool_search_path: std::env::var_os("PATH"),
            problems_open_set: std::collections::BTreeSet::new(),
            markdown_lint_last_seen: std::collections::HashMap::new(),
            live_run_files: std::collections::HashSet::new(),
            live_run_sent: std::collections::HashMap::new(),
            live_run_runner: None,
            md_scroll_synced: None,
            code_lens_enabled: true,
            function_breakpoints: Vec::new(),
            screen_reader: built.screen_reader,
            screen_reader_command: built.screen_reader_command.clone(),
            announcer: crate::a11y::Announcer::default(),
            announce_pos: None,
            a11y_event: None,
            debug: debugger::DebugState::new(),
            term_suggest_enabled: true,
            term_suggestion: None,
            shortcut_capture: None,
            inline_enabled: false,
            inline_worker: None,
            inline_config: None,
            inline_job: None,
            inline_lsp: None,
            inline_asked: None,
            inline_next_id: 0,
            keybindings_file: if cfg!(test) {
                std::env::temp_dir().join(format!(
                    "croft-test-keybindings-{}-{}.json",
                    std::process::id(),
                    TEST_KEYBINDINGS_SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
                ))
            } else {
                crate::keymap::keybindings_path()
            },
            search_editor_job: None,
            code_lens_requested: std::collections::HashMap::new(),
            sarif_diag_signature: Vec::new(),
            sarif_added_polled: None,
            sarif_step_signature: None,
            sarif_published: Vec::new(),
            completion: completion::CompletionState::new(),
            editor_vim_chord: EditorVimChord::default(),
            cmd_k_leader: None,
            cmd_k_miss_note: None,
            kitty_keys: false,
            legacy_ctrl_punct: false,
            vim: crate::vim::VimState::new(),
            vim_last_find: None,
            vim_visual_line: false,
            shortcuts_modal: None,
            settings_editor: None,
            status_hits: StatusHitAreas::default(),
            status_logged: String::new(),
            status_wide: false,
            connect_dialog: None,
            connect_auth: None,
            install_session: None,
            update_watch: None,
            build_marker_checked: None,
            drift_probe: None,
            local_drift: None,
            self_install: None,
            update_check: None,
            manual_update_check: None,
            update_fetch: if cfg!(test) {
                || Err(String::from("tests never ask the network"))
            } else {
                crate::update_check::fetch_latest_checked
            },
            update_toast: None,
            install_source: crate::update_check::current_install_source(),
            staged_install: None,
            staged_status: UpdateStatus::Idle,
            update_status: UpdateStatus::Idle,
            update_spinner_start: None,
            pending_reexec: false,
            overlays: {
                let mut overlays = OverlayManager::default();
                overlays.welcome.mark_dirty();
                overlays.hero.mark_dirty();
                overlays.activity.mark_dirty();
                overlays
            },
            editor_find: None,
            terminal_find: None,
            terminal_find_pane: 0,
            terminal_find_match: None,
            terminal_quick_select: None,
            // Under test, both durable stores point at per-process scratch
            // files so app tests that run commands or split panes can never
            // touch (or race on) the user's real history / session files;
            // tests that exercise the stores inject their own paths.
            command_history: crate::startup_trace::after(
                "command history read",
                crate::command_history::CommandHistory::load(&if cfg!(test) {
                    std::env::temp_dir().join(format!(
                        "croft-test-command-history-{}.jsonl",
                        std::process::id()
                    ))
                } else {
                    crate::command_history::history_path()
                }),
            ),
            command_history_popup: None,
            terminal_session_dirty: false,
            terminal_store_error: None,
            terminal_restore_respawns,
            terminal_session_path: if cfg!(test) {
                std::env::temp_dir().join(format!(
                    "croft-test-terminal-sessions-{}.json",
                    std::process::id()
                ))
            } else {
                crate::terminal_session::path()
            },
            workspace_folders_path: if cfg!(test) {
                std::env::temp_dir().join(format!(
                    "croft-test-workspace-folders-{}.json",
                    std::process::id()
                ))
            } else {
                crate::workspace::folders_store_path()
            },
            terminal_copy_mode: None,
            triggers: crate::startup_trace::after(
                "triggers loaded",
                load_trigger_set(!built.disable_secret_redaction),
            ),
            agents: crate::startup_trace::after(
                "agents loaded",
                crate::agents::AgentTable::load(&crate::agents::agents_path()),
            ),
            agent_events: std::collections::VecDeque::new(),
            matchers,
            watch_set,
            task_matcher_by_pane: std::collections::HashMap::new(),
            watch_published_panes: std::collections::HashSet::new(),
            broadcast_input: false,
            pending_broadcast_enable: false,
            pending_terminal_paste: None,
            pending_terminal_paste_target: None,
            multiline_paste_warning: built.terminal_multiline_paste_warning,
            pending_run_block: None,
            pending_captures: Vec::new(),
            block_outputs: std::collections::HashMap::new(),
            show_terminal_timestamps: false,
            notifier: crate::notifications::Notifier::new(&built.notifications),
            host_accents: compile_host_accents(&built.host_accents),
            closed_terminals: Vec::new(),
            ending_terminals: Vec::new(),
            closed_tabs: Vec::new(),
            file_finder: None,
            quick_open_range: None,
            command_palette: None,
            go_to_symbol: None,
            workspace_symbols: None,
            ws_symbols_request_id: None,
            ws_symbols_pending: None,
            mcp_rx: None,
            mcp_started: None,
            mcp_busy_label: None,
            process_picker: None,
            selected_debug_config: None,
            selected_debug_compound: None,
            pending_debug_launch: None,
            input_run: None,
            pending_attach: None,
            pending_test_debug: None,
            pending_cargo_launches: Vec::new(),
            watch_exprs: Vec::new(),
            watch_vals: std::collections::HashMap::new(),
            watch_prev: std::collections::HashMap::new(),
            watch_baseline_pending: false,
            zoxide_jump: None,
            zoxide_add_folder: false,
            branch_picker: None,
            branch_purpose: BranchPurpose::Checkout,
            blocked_switch: None,
            input_prompt: None,
            list_picker: None,
            session_channel: crate::session_host::InnerChannel::from_env(),
            session_participants: Vec::new(),
            session_host_stale_seen: false,
            session_stale_notice: None,
            session_typist: None,
            session_carets: std::collections::HashMap::new(),
            session_presence_mtime: None,
            last_session_presence_poll: std::time::Instant::now(),
            collab: agents_collab::CollabState::new(),
            pair: agents_collab::PairState::new(&root, !built.disable_proactive_navigator),
            #[cfg(test)]
            kernel_start_override: None,
            git_output_log: Vec::new(),
            build_diagnostics: std::collections::HashMap::new(),
            build_diag_files_by_pane: std::collections::HashMap::new(),
            undo_pushed_commit_armed: None,
            reset_hard_armed: None,
            pending_discard_all: false,
            pending_stash_drop: None,
            pending_replace_all: None,
            file_finder_index: None,
            file_finder_index_job,
            file_finder_index_dirty: false,
            quick_suggestions: !built.disable_quick_suggestions,
            word_suggestions: crate::word_suggest::WordSuggestions::from_pref(
                &built.word_based_suggestions,
            ),
            signature_help_popup: None,
            signature_help_request_id: None,
            signature_help_anchor: None,
            rename_request_id: None,
            rename_baseline: Vec::new(),
            pending_file_move: None,
            prepare_rename_request: None,
            format_request_id: None,
            format_request_seq: None,
            format_request_selection: false,
            format_on_type: built.format_on_type,
            on_type_request: None,
            code_action_request_id: None,
            code_action_pending_resolve: false,
            code_action_baseline: Vec::new(),
            pending_code_actions: Vec::new(),
            review: review::ReviewState::new(
                built
                    .review_ai_prefix
                    .clone()
                    .unwrap_or_else(|| String::from(crate::review_threads::DEFAULT_AI_PREFIX)),
            ),
            notes: crate::startup_trace::after(
                "sticky notes read",
                notes_path
                    .as_deref()
                    .map(crate::sticky_notes::Notes::load)
                    .unwrap_or_default(),
            ),
            notes_path,
            note_box_ids: std::collections::HashMap::new(),
            notes_shown_gen: 0,
            recording: None,
            recorded_size: (0, 0),
            scrub: diff_merge::ScrubState::new(),
            auto_approve: None,
            tour: None,
            tour_done: loaded_prefs.tour_done,
            welcome_tour_button: Rect::default(),
            gh_program: PathBuf::from("gh"),
            gh_limit: code_scanning::CODE_SCAN_GH_LIMIT,
            code_scan_asks: Vec::new(),
            dismiss_target: None,
            code_scan_picker_root: None,
            sarif_locate_pending: None,
            sarif_download: None,
            sarif_fetch: fetch_sarif_source,
            agent_ledger: crate::agent_lane::AgentLedger::new(),
            agent_ledger_store: (!cfg!(test)).then(agent_ledger_store_path),
            agent_ledger_saved: (0, std::time::Instant::now()),
            pending_lane_picks: Vec::new(),
            pending_bookmarks: Vec::new(),
            problems_fix_started: None,
            http_run: None,
            http_environment: None,
            pending_color_presentations: Vec::new(),
            pending_color_context: None,
            color_presentations_request: None,
            hover: mouse_input::HoverState::new(),
            rust_analyzer_request: None,
            tab_hover: HoverDwell::default(),
            tab_tooltip: None,
            tab_hover_idx: None,
            ui_hover: HoverDwell::default(),
            ui_tooltip: None,
            ui_tooltip_label: None,
            search_hint_view: (0, 0, false),
            ui_tooltip_area: None,
            update_card_rect: None,
            definition_request_id: None,
            peek_definition_request_id: None,
            peek_popup: None,
            peek_target: None,
            peek_refs: None,
            references_want_peek: false,
            outline_synced: None,
            outline_parse: None,
            outline_parsed_for: None,
            outline_lsp_landed: None,
            declaration_request_id: None,
            type_definition_request_id: None,
            implementation_request_id: None,
            references_request_id: None,
            call_hierarchy_request_id: None,
            occurrences_request: None,
            linked_request: None,
            linked_observed: None,
            linked_observed_at: std::time::Instant::now(),
            linked_asked: false,
            selection_range_request: None,
            occ_observed: None,
            occ_observed_at: std::time::Instant::now(),
            nav: NavHistory::default(),
        };
        crate::startup_trace::mark("editor state built");
        // The loaded settings, through the one step every reload runs
        // (#2062 settings card step 2): Vim mode when it was saved as the
        // default (#1286), the PROBLEMS scope (#256), the project's excludes,
        // file associations and encoding, and every other live setting.
        app.apply_merged_settings(&loaded_prefs);
        // #578: the Testing view runs CodeQL tests with the same `codeql`
        // as the side bar, from the first tree click on. Tests keep plain
        // `codeql` rather than whatever the machine has installed.
        if cfg!(test) {
            app.test_worker
                .set_codeql_program(app.codeql_jobs.program.clone());
        } else {
            app.resolve_codeql_cli();
        }
        // And finds runners among the extensions the app has added: CodeQL's
        // comes with the CodeQL extension (#1842).
        app.test_worker.set_extensions_dir(app.extensions_dir());
        // Initialise the per-pane focus/gradient flags to match the starting
        // pane (Tree) and persisted theme. Without this, the explorer boots
        // focused-but-not-gradient: the Black-theme gradient border only
        // arms on the first focus change because nothing ran the sync at
        // construction time.
        app.sync_focus_flags();
        // Seed the highlighter with the persisted theme's code palette so a
        // file opened before the first theme switch already highlights in the
        // active theme's colors (not the Base16 default).
        crate::highlight::set_syntax_palette(app.theme.syntax());
        // The agent review queue as this workspace last left it (#345).
        let root = app.workspace_root().to_path_buf();
        app.watch_project_snippets();
        app.agent_ledger = app.load_agent_ledger(&root);
        app.sync_agent_lane_decorations();
        Ok(app)
    }

    /// Post-draw flush of the activity-bar icons. Re-emits the pre-encoded
    /// OSC-1337 image bytes each frame (ratatui doesn't track image cells, so
    /// neighbouring SGR traffic can evict them from iTerm2's cache) — but
    /// first applies the same emit-time move-detection as the run-debug
    /// headline icon: if the icon cells shifted since the last emit (a
    /// terminal resize, or any future layout change that recenters the bar),
    /// a plain re-emit stacks a fresh image on top of the one iTerm2 still
    /// caches at the old cell, the doubled-icon ghost. On a move it arms one
    /// `terminal.clear()` (the proven `\x1b[2J` eviction the main loop folds
    /// into its OR chain), forgets the emit positions, and skips painting
    /// this frame; the next frame clears, full-redraws, and re-emits a single
    /// clean set at the new cells.
    pub fn flush_activity_image_overlays(&mut self, around: &[u64]) {
        // The keepalive re-emit fights iTerm2's image-cache eviction. Neither
        // the sixel cell buffer nor Kitty's image layer evicts, so there the
        // icons re-emit only when dirty: on Kitty the keepalive cost about
        // 13 KB every two seconds, idle, over SSH (#682).
        let dirty = self.overlays.activity.dirty();
        let allow_keepalive = self.activity_keepalive_allowed()
            && (0..around.len()).any(|i| self.overlays.activity.neighbourhood_changed(i, around));
        if !self.overlays.activity.should_refresh(allow_keepalive) {
            return;
        }
        let mut overlays = self.pending_activity_image_overlays();
        let positions: Vec<(u16, u16)> = overlays.iter().map(|(cell, _)| *cell).collect();
        // Checked before the empty return below: an update card over every
        // icon left nothing to send, and Kitty kept the old ones over it.
        if self.overlays.activity.positions_moved(&positions) {
            self.overlays.activity.request_clear();
            self.overlays.activity.forget_positions();
            self.overlays.activity.forget_sent();
            return;
        }
        if overlays.is_empty() {
            return;
        }
        let hashes: Vec<u64> = overlays
            .iter()
            .map(|(_, seq)| {
                use std::hash::{Hash, Hasher};
                let mut h = std::collections::hash_map::DefaultHasher::new();
                seq.hash(&mut h);
                h.finish()
            })
            .collect();
        let sent: Vec<((u16, u16), u64)> = positions.iter().copied().zip(hashes).collect();
        // Everything goes out after a wipe or on the first frame. Otherwise
        // a dirty flush sends only the slots whose cell or image changed,
        // and a keepalive only the icons whose surroundings changed (`around`
        // is in the order of the unchanged positions).
        if !self.overlays.activity.nothing_sent() {
            let same = positions.as_slice() == self.overlays.activity.last_positions();
            let mut i = 0;
            overlays.retain(|_| {
                let keep = if dirty {
                    !self.overlays.activity.was_sent(sent[i].0, sent[i].1)
                } else {
                    same && self.overlays.activity.neighbourhood_changed(i, around)
                };
                i += 1;
                keep
            });
        }
        if overlays.is_empty() {
            self.overlays.activity.mark_emitted();
            self.overlays.activity.store_positions(positions);
            self.overlays.activity.set_neighbourhood(around.to_vec());
            self.overlays.activity.set_sent(sent);
            return;
        }
        let cursor_on = self.cursor_should_be_visible();
        let on_screen = overlays
            .iter()
            .filter(|((x, y), _)| {
                // Same screen bound as the image writers (#513): a raw
                // absolute-CUP write that ratatui cannot repair, so the origin
                // is checked before it is used.
                //
                // 1x1 is NARROWER than some payloads here, and knowingly so.
                // `pending_activity_image_overlays` discards each block's
                // `Rect` and returns only its origin, and a count badge is 2-3
                // cells wide (`count_badge_cells_w`), so a badge whose left
                // edge is on screen and right edge is not would pass this.
                // Not reachable today -- the badges anchor to the left-hand
                // activity bar -- and making it exact means returning the
                // `Rect` from that function, which is a wider change than this
                // guard. Recorded rather than left to be rediscovered.
                self.overlay_fits_on_screen(*x, *y, 1, 1)
            })
            .map(|((x, y), seq)| (*x, *y, seq.as_bytes()));
        write_inline_images(&mut stdout(), on_screen, cursor_on);
        self.overlays.activity.mark_emitted();
        self.overlays.activity.store_positions(positions);
        self.overlays.activity.set_neighbourhood(around.to_vec());
        self.overlays.activity.set_sent(sent);
    }

    /// Reset the blink phase so the caret is solidly visible for the next
    /// 530ms. Call after any edit, cursor movement, or focus change so the
    /// user always sees where the cursor just landed before it starts to
    /// blink off.
    fn poke_cursor(&mut self) {
        self.cursor_blink.poke();
    }

    fn sync_open_file_poll_mtime(&mut self) {
        let paths = self.polled_open_file_paths();
        self.fs_watch.sync_open_file_mtime(&paths);
        self.sync_run_debug_if_file_changed();
    }

    /// The repository toplevel owning the workspace, from the git worker's
    /// last status reply — the only correct base for porcelain-derived
    /// paths and `HEAD:<rel>` pathspecs, which git resolves against the
    /// toplevel however `-C` is set (#139). Identical to the workspace
    /// root whenever that root IS the toplevel; falls back to it outside
    /// a repo (where the ops fail with their own message) and before the
    /// first status reply lands.
    /// The primary workspace root — what `workspace_root` the field meant
    /// before the set model (#143). Single-root semantics throughout.
    pub fn workspace_root(&self) -> &Path {
        self.roots.primary()
    }

    fn scm_root(&self) -> PathBuf {
        let ws = self.active_scm_root.clone();
        self.git_worker_for_root(&ws)
            .status()
            .repo_root
            .clone()
            .unwrap_or(ws)
    }

    /// The workspace root the focus-following surfaces resolve against:
    /// the root owning the focused editor's file, else the primary
    /// (#149 git, #151 testing/tasks/run).
    fn active_workspace_root(&self) -> PathBuf {
        self.active_workspace_root_path().to_path_buf()
    }

    /// [`Self::active_workspace_root`], borrowed: what the per-tick checks
    /// compare against, so a pass where focus stayed put copies nothing.
    fn active_workspace_root_path(&self) -> &Path {
        self.editor
            .path
            .as_deref()
            .and_then(|p| self.roots.owning_root(p))
            .unwrap_or_else(|| self.roots.primary())
    }

    /// Every workspace folder as `(name, root)`, for a compound member's
    /// `"folder"` and `${workspaceFolder:name}` (#1425).
    fn workspace_folders(&self) -> Vec<(String, PathBuf)> {
        crate::dap::configs::workspace_folders(self.roots.iter())
    }

    /// The lines of every text buffer open in the active group, by path
    /// (#2127): a Quick Open `:line` query counts these, since a pick opens
    /// into the active group and reuses its tab for the file, unsaved edits
    /// and all. Groups share no buffers, so a file open only in another
    /// group opens from disk, and its saved file is what counts.
    fn open_text_line_counts(&self) -> std::collections::HashMap<PathBuf, usize> {
        self.editor
            .editors
            .iter()
            .filter(|e| !e.has_non_text_view())
            .filter_map(|e| Some((e.path.clone()?, e.lines.len())))
            .collect()
    }

    /// Keep an open Quick Open's buffer line counts current (#2127). A
    /// buffer can change while Quick Open is up (the file watcher reloads
    /// it, an agent or a collab peer edits it), and a `:line` query must
    /// count it as a pick would open it now. Cheap: one entry per tab of the
    /// active group, and the check reruns only when a count changed. True
    /// when the list changed.
    pub(super) fn sync_file_finder_open_lines(&mut self) -> bool {
        if self.file_finder.is_none() {
            return false;
        }
        let open = self.open_text_line_counts();
        self.file_finder
            .as_mut()
            .is_some_and(|f| f.set_open_lines(open))
    }

    /// Every open buffer's path, across the active leaf and every inactive
    /// one. `App::editor` is the hoisted active leaf and `inactive_groups`
    /// walks the rest, so together they cover each leaf exactly once.
    fn open_buffer_paths(&self) -> std::collections::BTreeSet<PathBuf> {
        self.buffer_paths().map(Path::to_path_buf).collect()
    }

    /// [`Self::open_buffer_paths`] in tab order, borrowed, a file once per
    /// tab holding it.
    fn buffer_paths(&self) -> impl Iterator<Item = &Path> + '_ {
        self.editor_layout
            .all_groups(&self.editor)
            .flat_map(|g| g.editors.iter())
            .filter_map(|e| e.path.as_deref())
    }

    fn cycle_focus(&mut self) {
        // Skip hidden panes when cycling.
        let mut next = self.focus;
        for _ in 0..3 {
            next = match next {
                Pane::Tree => Pane::Editor,
                Pane::Editor => Pane::Terminal,
                Pane::Terminal => Pane::Tree,
            };
            if self.pane_visible(next) {
                break;
            }
        }
        // Through `focus_pane`, never a bare assignment: every route into a
        // pane shares its guarantees (terminal find/copy-mode teardown, and
        // restoring the editor from under a maximized terminal).
        self.focus_pane(next);
    }

    fn pane_visible(&self, p: Pane) -> bool {
        match p {
            Pane::Tree => self.show_tree,
            Pane::Terminal => self.show_terminal,
            Pane::Editor => true,
        }
    }

    pub fn terminal(&self) -> &PtyTerminal {
        &self.terminals[self.active_terminal]
    }

    pub fn terminal_mut(&mut self) -> &mut PtyTerminal {
        &mut self.terminals[self.active_terminal]
    }

    fn focus_pane(&mut self, p: Pane) {
        // Leaving the terminal closes its find bar so a stale match highlight
        // never lingers on the grid once input goes elsewhere. Quick-select
        // labels are torn down for the same reason.
        if p != Pane::Terminal && self.terminal_find.is_some() {
            self.close_terminal_find();
        }
        if p != Pane::Terminal && self.terminal_quick_select.is_some() {
            self.close_terminal_quick_select();
        }
        if p != Pane::Terminal && self.terminal_copy_mode.is_some() {
            self.close_terminal_copy_mode();
        }
        // A maximized terminal gives the editor zero rows; focusing the
        // editor is always an intent to use it, so restore it (VS Code's
        // convention when a file is revealed over a maximized panel).
        // `terminal_pane_maximized` (width, among terminal panes) is
        // unrelated and stays.
        if p == Pane::Editor && self.terminal_maximized {
            self.terminal_maximized = false;
        }
        // Auto-hide (#260): moving into the editor or a terminal collapses
        // the sidebar; moving INTO the sidebar releases the pin a reveal
        // command took, so the next move out can collapse it again.
        match p {
            Pane::Tree => {
                self.sidebar_pinned_open = false;
                // Focus came back inside the grace window: the click was
                // passing through, or landed here. Cancel the pending
                // collapse (#302) rather than letting it fire under the user.
                self.sidebar_dwell.disarm();
            }
            Pane::Editor | Pane::Terminal => self.maybe_auto_hide_sidebar(),
        }
        self.focus = p;
        self.sync_focus_flags();
        if self.editor.focused {
            self.poke_cursor();
        }
    }

    /// Copy the editor view settings onto every open tab, in every group:
    /// indent guides, bracket-pair colours, rendered whitespace (with each
    /// tab's language override), the Home walk, rulers and line numbers.
    /// Each tab draws from its own copy, and nothing copies them again per
    /// frame, so a settings reload, a toggle and a focus change all come
    /// through here: one that set only the active tab left the rest, and
    /// every tab of another group, on the old look (#2075).
    pub(super) fn apply_view_prefs(&mut self) {
        let (langs, ws_global) = (&self.language_prefs, self.whitespace_mode);
        for group in self.editor_layout.all_groups_mut(&mut self.editor) {
            for ed in &mut group.editors {
                ed.show_indent_guides = self.indent_guides_enabled;
                ed.show_bracket_colors = self.bracket_colors_enabled;
                ed.whitespace_mode = whitespace_mode_in(langs, ws_global, editor_scope_id(ed));
                ed.home_walks_rows = self.home_walks_rows;
                ed.rulers.clone_from(&self.rulers);
                ed.line_numbers = self.line_numbers;
            }
        }
    }

    fn sync_focus_flags(&mut self) {
        // Focus must never rest on a COLLAPSED pane (#313). A folded pane is
        // one column wide and is never rendered, so it paints no cursor and no
        // focus border: the user would be typing into a pane they cannot see
        // receiving it.
        //
        // Enforced HERE rather than at each gesture. The first attempt guarded
        // `cycle_terminal` alone, which left closing the pane beside a folded
        // one, the maximize rail, and a right-click on a strip - the last of
        // which that same round OPENED, since making a strip right-clickable
        // also made it focusable. A fix at the call site closes the path you
        // were looking at; a fix at the invariant closes the ones you were not.
        //
        // Two things this function does NOT do, contrary to an earlier
        // version of this comment. It is not called per frame - there are
        // eleven call sites, none in a render path - and it is not on every
        // path that moves `active_terminal`: `activate_terminal_pane` and
        // `move_terminal` both assign without calling it. Those two are safe
        // for reasons of their own (the first takes its index from
        // `terminal_at_pos`, which cannot name a folded pane because its
        // `last_area` is cleared; the second keeps the active pane pointed at
        // its own terminal), so the invariant's totality rests on those
        // reasons rather than on this being a universal funnel. A change to
        // what feeds `terminal_at_pos` would break it silently.
        //
        // And this is the rule for focus that DRIFTS. A caller deliberately
        // choosing a pane by index wants it made visible instead, which is
        // `reveal_terminal_pane`.
        //
        // Skipped while a pane is maximized: the flags are deliberately
        // ignored there and the active pane is painted full width whatever it
        // holds, so moving focus would fight the user's own selection. Leaving
        // maximize calls back through here, which is where it is repaired.
        if !self.terminal_pane_maximized
            && self
                .terminals
                .get(self.active_terminal)
                .is_some_and(|t| t.collapsed)
            && let Some(next) = self
                .terminals
                .iter()
                .enumerate()
                .filter(|(_, t)| !t.collapsed)
                .min_by_key(|(candidate, _)| candidate.abs_diff(self.active_terminal))
                .map(|(candidate, _)| candidate)
        {
            self.active_terminal = next;
        }
        // Quick-select is bound to the pane it opened on; this per-frame
        // invariant covers every gesture that can move `active_terminal`
        // (clicks, chords, pane closes shifting indices) in one place, so
        // stale labels never survive a pane switch to hijack the new pane.
        if self
            .terminal_quick_select
            .as_ref()
            .is_some_and(|st| st.pane != self.active_terminal)
        {
            self.close_terminal_quick_select();
        }
        // Terminal find is pane-bound the same way.
        if self.terminal_find.is_some() && self.terminal_find_pane != self.active_terminal {
            self.close_terminal_find();
        }
        self.editor.focused = self.focus == Pane::Editor;
        // Inactive groups are by definition NOT the focused editor group, so
        // their borders never light up even while the editor pane holds focus.
        // `editor` (the active group) carries the highlight.
        for group in self.editor_layout.inactive_groups_mut() {
            group.focused = false;
        }
        let focused_pane = self.focus == Pane::Terminal;
        let active = self.active_terminal;
        for (i, t) in self.terminals.iter_mut().enumerate() {
            t.focused = focused_pane && i == active;
        }
        // Only the Black theme dresses the focused pane in the gradient
        // border; Croft Dark keeps the historical solid-blue highlight.
        let gradient = self.theme.gradient();
        // Every editor in both groups, not just the active one (deref would
        // only hit the active editor): a tab switch or the split group must
        // not resurrect the legacy navy active-tab chip between syncs.
        // The bundled PDF / CSV viewers are gated by the Extensions panel:
        // when disabled, the editor opens those file types as plain text.
        let pdf_on = self.is_extension_enabled("pdf");
        let csv_on = self.is_extension_enabled("csv");
        for ed in self
            .editor_layout
            .all_groups_mut(&mut self.editor)
            .flat_map(|g| g.editors.iter_mut())
        {
            ed.focus_gradient = gradient;
            ed.theme = self.theme;
            ed.pdf_viewer_enabled = pdf_on;
            ed.csv_viewer_enabled = csv_on;
            ed.provenance_overlay = self.provenance_overlay;
            ed.diff_ws_default = self.diff_ws_default;
        }
        self.apply_view_prefs();
        self.refresh_scm_repositories();
        self.problems.fix_glyph = match self.problems_fix_started {
            Some(t) => update_spinner_frame(t.elapsed().as_millis()),
            None => "",
        };
        self.output.sync();
        for t in self.terminals.iter_mut() {
            t.show_timestamps = self.show_terminal_timestamps;
        }
        for t in self.terminals.iter_mut() {
            t.focus_gradient = gradient;
            t.theme = self.theme;
            t.set_palette(self.theme.ansi());
        }
        self.dress_host_accents();
    }

    /// Whether popups/menus/tooltips should wear the orange→green gradient
    /// border and the muted dark-teal selection fill instead of the legacy
    /// bright-blue accent. Gated on the Black theme to mirror the focused-pane
    /// border, so Croft Dark keeps its coherent all-blue look.
    fn popup_gradient(&self) -> bool {
        self.theme.gradient()
    }

    /// Open `url` in the user's browser: through the relay or a detected
    /// URL on a remote session, and never from a test.
    fn open_url_for_user(&mut self, url: &str) {
        self.try_open_url_for_user(url);
    }

    /// `open_url_for_user`, returning false when the browser could not be
    /// started.
    fn try_open_url_for_user(&mut self, url: &str) -> bool {
        self.open_web_url(
            url,
            WebOpenOpts {
                inert_in_tests: true,
                ..WebOpenOpts::default()
            },
        )
    }

    /// A path as typed: `~` expanded, relative to the workspace root.
    fn typed_path(&self, value: &str) -> PathBuf {
        self.typed_path_checked(value).unwrap_or_else(|| {
            let v = value.trim();
            self.workspace_root()
                .join(v.strip_prefix("~/").unwrap_or(v))
        })
    }

    /// As `typed_path`, but `None` for `~/…` when HOME is unset, so the
    /// caller can refuse it rather than write under the workspace.
    fn typed_path_checked(&self, value: &str) -> Option<PathBuf> {
        resolve_typed_path(
            value,
            std::env::var_os("HOME").map(PathBuf::from).as_deref(),
            self.workspace_root(),
        )
    }

    /// True when the active editor tab holds an editable text buffer (not a
    /// read-only diff / spreadsheet / image preview), so text-editing chords
    /// know whether to act.
    fn editor_is_text(&self) -> bool {
        !self.editor.has_non_text_view() && self.editor.markdown_preview.is_none()
    }

    /// Workspace-relative rendering for status transients and prompt
    /// labels: tabs and breadcrumbs already speak root-relative, and an
    /// absolute path under a deep root pushed the one informative part —
    /// the filename — off the visible end of the bar (#100). Paths outside
    /// the root (`~/.ssh/config`, files opened by absolute path) keep
    /// their absolute form.
    fn status_path(&self, path: &Path) -> String {
        // Folder-relative via the OWNING root (#143): identical to a plain
        // primary-root strip while the set holds one entry, and already the
        // multi-root label rule once it holds more.
        let rel = self
            .roots
            .owning_root(path)
            .and_then(|r| path.strip_prefix(r).ok())
            .unwrap_or(path);
        if rel.as_os_str().is_empty() {
            String::from(".")
        } else {
            rel.display().to_string()
        }
    }
}

/// Shrink `s` to at most `max` terminal cells by cutting the middle,
/// keeping roughly a third of the head and two thirds of the tail — for
/// path-shaped messages the tail carries the filename, the part worth
/// keeping. Never appends a trailing ellipsis (the cut is interior by
/// construction). Measured in cells, not chars, so a CJK name is not
/// overdrawn by the status bar's right cluster.
fn elide_middle(s: &str, max: usize) -> String {
    use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
    if s.width() <= max {
        return s.to_string();
    }
    if max < 3 {
        let mut out = String::new();
        let mut used = 0;
        for c in s.chars() {
            let w = c.width().unwrap_or(0);
            if used + w > max {
                break;
            }
            used += w;
            out.push(c);
        }
        return out;
    }
    crate::widgets::process_picker::elide_middle_cols(s, max)
}

/// `text` on the status bar's one row (#925): each line break or tab, with
/// the spaces around it, becomes one space. Drawn as is, the control
/// characters were dropped and the words either side ran together
/// ("…switch branches.Aborting"). The Messages channel keeps the lines.
fn one_line<'a>(text: std::borrow::Cow<'a, str>) -> std::borrow::Cow<'a, str> {
    if !text.contains(char::is_control) {
        return text;
    }
    let pieces = text.split(char::is_control).map(str::trim);
    std::borrow::Cow::Owned(
        pieces
            .filter(|p| !p.is_empty())
            .collect::<Vec<_>>()
            .join(" "),
    )
}

/// `text` with each absolute path under `root` written relative to it, for
/// the status bar: `Saved /home/me/proj/src/main.rs` reads `Saved
/// src/main.rs`. A path only counts where it starts a word (after the start,
/// a space, a quote or a bracket), so `root` found inside some other
/// absolute path is left alone; the root on its own is kept as is.
fn workspace_relative<'a>(text: &'a str, root: &Path) -> std::borrow::Cow<'a, str> {
    use std::borrow::Cow;
    let root = root.to_string_lossy();
    let root = root.trim_end_matches('/');
    if root.is_empty() || !text.contains(root) {
        return Cow::Borrowed(text);
    }
    let needle = format!("{root}/");
    if !text.contains(&needle) {
        return Cow::Borrowed(text);
    }
    let mut out = String::with_capacity(text.len());
    let mut rest = text;
    while let Some(i) = rest.find(&needle) {
        let starts_word = rest[..i]
            .chars()
            .next_back()
            .or_else(|| out.chars().next_back())
            .is_none_or(|c| c.is_whitespace() || "'\"`([{<".contains(c));
        out.push_str(&rest[..i]);
        if !starts_word {
            out.push_str(&needle);
        }
        rest = &rest[i + needle.len()..];
    }
    out.push_str(rest);
    Cow::Owned(out)
}

/// Milliseconds since the Unix epoch, for stamping local-history snapshots
/// and dating TIMELINE rows.
fn now_millis() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

/// `n` with the word for one or for many, as screen reader mode reads a
/// count: "1 error", "3 matches".
fn a11y_count(n: usize, one: &str, many: &str) -> String {
    format!("{n} {}", if n == 1 { one } else { many })
}

/// Whitespace rendering for a file in language `scope` (#1440): its
/// language block's `editor.renderWhitespace`, else `global`. A free fn so
/// the render loops can call it while they hold the editors mutably.
fn whitespace_mode_in(
    langs: &std::collections::BTreeMap<String, crate::prefs::LanguagePrefs>,
    global: crate::widgets::editor::WhitespaceMode,
    scope: &str,
) -> crate::widgets::editor::WhitespaceMode {
    langs
        .get(scope)
        .and_then(|l| l.render_whitespace.as_deref())
        .map_or(global, crate::widgets::editor::WhitespaceMode::from_pref)
}

/// The VS Code language id of the file at `path`, as its tab would have
/// it (#1440: which `languages` block applies).
fn path_scope_id(path: &Path) -> &'static str {
    settings_scope_id(
        crate::widgets::editor::language_scope_id(crate::highlight::lang_for_path(path)),
        Some(path),
    )
}

/// The VS Code language id of `ed`'s buffer for settings lookup (#1440).
fn editor_scope_id(ed: &crate::widgets::editor::Editor) -> &'static str {
    settings_scope_id(ed.scope_id(), ed.path.as_deref())
}

/// Refines the highlighter's language id `base` to the VS Code id the
/// file's extension carries where several ids share one grammar (#1440):
/// `.jsx` is `javascriptreact`, `.jsonc` is `jsonc`, `.scss` is `scss`, so
/// their `"[…]"` blocks apply. A buffer switched to another language keeps
/// that language's id.
fn settings_scope_id(base: &'static str, path: Option<&Path>) -> &'static str {
    let ext_is = |want: &str| {
        path.and_then(|p| p.extension())
            .and_then(|e| e.to_str())
            .is_some_and(|e| e.eq_ignore_ascii_case(want))
    };
    match base {
        "javascript" if ext_is("jsx") => "javascriptreact",
        "json" if ext_is("jsonc") => "jsonc",
        "css" if ext_is("scss") => "scss",
        "css" if ext_is("sass") => "sass",
        _ => base,
    }
}

/// Inlay hints for a file in language `scope` (#1440): its language
/// block's `disable_inlay_hints`, else `global`. A free fn so callers can
/// use it while they hold the editors mutably.
fn inlay_hints_on_in(
    langs: &std::collections::BTreeMap<String, crate::prefs::LanguagePrefs>,
    global: bool,
    scope: &str,
) -> bool {
    langs
        .get(scope)
        .and_then(|l| l.disable_inlay_hints)
        .map_or(global, |off| !off)
}

use crate::proc::info::cwd_is_observable;

/// The cells of `r` that lie inside `buf`, row by row.
fn cells_in(buf: &ratatui::buffer::Buffer, r: Rect) -> Vec<ratatui::buffer::Cell> {
    let r = r.intersection(buf.area);
    r.positions().map(|p| buf[p].clone()).collect()
}

/// Cap a string at `max` characters by inserting an ellipsis in the
/// end so the start (scheme/host) stays visible. No ellipsis marker anywhere in
/// croft, so overflow is cut plainly from the right rather than elided in the
/// middle. `max <= 8` returns the original.
fn truncate_for_display(s: &str, max: usize) -> String {
    if s.chars().count() <= max || max <= 8 {
        return s.to_string();
    }
    s.chars().take(max).collect()
}

/// Whether a process with `pid` exists. Signal 0 delivers nothing and only
/// checks; a process this user may not signal still exists, so `EPERM`
/// reads as alive. Off unix nothing is reaped, on the conservative side.
/// Existence, not liveness, is enough here (unlike `session::is_alive`,
/// which probes by connecting): a pid that was reused reads as alive and
/// only leaks a file, and every wrong answer this can give errs that way.
#[cfg(unix)]
fn process_is_alive(pid: u32) -> bool {
    let Ok(pid) = libc::pid_t::try_from(pid) else {
        return true;
    };
    // SAFETY: `kill` with signal 0 only checks for the process; no signal is
    // delivered and no memory is touched.
    if unsafe { libc::kill(pid, 0) } == 0 {
        return true;
    }
    std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)
}

#[cfg(not(unix))]
fn process_is_alive(_pid: u32) -> bool {
    true
}

fn rect_contains(r: Rect, x: u16, y: u16) -> bool {
    r.width > 0 && r.height > 0 && x >= r.x && x < r.x + r.width && y >= r.y && y < r.y + r.height
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RunSpec {
    pub program: String,
    pub args: Vec<String>,
    pub cwd: PathBuf,
}

/// What the `.http` worker hands back (#370): the file it ran from, the
/// RAW request line (variables unresolved, so a secret can never land in a
/// status line, history entry, or response tab through it), and the result.
struct HttpRunOutcome {
    file: PathBuf,
    raw_line: String,
    result: Result<crate::http_file::HttpResponse, String>,
}

/// What fetching a variant analysis's results made (#578): the combined
/// SARIF log and table, how many repositories they hold, and the
/// repositories whose results could not be fetched, with why.
struct VariantResultFiles {
    sarif: Option<PathBuf>,
    csv: Option<PathBuf>,
    repos: usize,
    failed: Vec<(String, String)>,
}

/// Where an export of variant analysis results went (#578).
struct VariantExport {
    /// The summary file of a folder export.
    summary: Option<PathBuf>,
    /// The gist a gist export made.
    gist: Option<String>,
    repos: usize,
    failed: Vec<(String, String)>,
}

/// Hit rects for one terminal pane's header buttons, as laid out by the most
/// recent render. Any may be `None` when the pane is too narrow.
#[derive(Default)]
struct TerminalPaneButtons {
    add: Option<Rect>,
    profile: Option<Rect>,
    close: Option<Rect>,
    collapse: Option<Rect>,
}

#[cfg(test)]
mod tests;

/// One participant's parked caret in a multiplayer session: where they were
/// when someone else took over typing (docs/MULTIPLAYER.md).
#[derive(Clone, Debug, PartialEq, Eq)]
struct SessionCaret {
    path: PathBuf,
    row: usize,
    col: usize,
}

/// A collab peer's last-known caret: where it is, whose it is, and when it
/// last moved (the name tag shows while `last_moved` is inside
/// [`CARET_LABEL_FADE`]). Keyed by site id, which is per-file, so one peer
/// legitimately holds one entry per shared file.
struct CollabCaret {
    file: String,
    row: usize,
    col: usize,
    name: String,
    last_moved: std::time::Instant,
}

/// A live AI stream announced over the relay (`CollabMsg::StreamState`):
/// who is streaming, into which file, and the pilot's site id in that file
/// (keys the pilot's ghost caret for the gutter stop button's row).
struct CollabStreamInfo {
    file: String,
    name: String,
    site: u64,
    /// The undo group the stream's edits share (#1207), so the whole
    /// stream undoes in one step.
    undo_group: u64,
}

/// The stdout wrapper that is ratatui's writer. It counts the bytes each
/// frame ships, to surface the per-frame payload size in the F8 perf HUD
/// (which reads and resets the count once per frame), and holds them until
/// the frame flushes, so the whole frame reaches the terminal in one write.
/// Straight through to stdout, a full repaint went out a kilobyte at a time,
/// tens of writes the terminal could draw between, so a launch's first frame
/// visibly painted in; and every few-byte cursor move or colour change took
/// stdout's lock on its way.
struct CountingWriter {
    inner: Stdout,
    pending: Vec<u8>,
    bytes: std::sync::Arc<std::sync::atomic::AtomicUsize>,
}

impl CountingWriter {
    fn new(inner: Stdout, bytes: std::sync::Arc<std::sync::atomic::AtomicUsize>) -> Self {
        Self {
            inner,
            pending: Vec::with_capacity(64 * 1024),
            bytes,
        }
    }
}

impl std::io::Write for CountingWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.pending.extend_from_slice(buf);
        self.bytes
            .fetch_add(buf.len(), std::sync::atomic::Ordering::Relaxed);
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        if !self.pending.is_empty() {
            let sent = self.inner.write_all(&self.pending);
            self.pending.clear();
            sent?;
        }
        self.inner.flush()
    }
}

/// DEC private mode 2026, synchronized output: the terminal shows nothing a
/// frame draws until the frame ends, then all of it at once, so a repaint
/// never appears half done. Terminals without it ignore the mode.
const BEGIN_SYNCHRONIZED_UPDATE: &[u8] = b"\x1b[?2026h";
const END_SYNCHRONIZED_UPDATE: &[u8] = b"\x1b[?2026l";

type CroftTerminal = Terminal<CrosstermBackend<CountingWriter>>;

/// Queue `sources` for an scp upload to `alias` as a drop on the Remote
/// Explorer does, then run them as the main loop does, on a screen of a
/// fixed size that asks the terminal nothing. For the saved record of every
/// ssh, rsync and scp call (`src/remote/recorded_calls.rs`, #2062): the
/// upload needs an app and croft's own screen, and neither leaves this
/// module.
#[cfg(all(test, target_os = "linux"))]
pub(crate) fn run_scp_uploads_for_record(
    workspace: PathBuf,
    alias: &str,
    sources: &[PathBuf],
) -> Result<()> {
    let mut app = App::new(workspace)?;
    app.remote.targets = vec![crate::remote::RemoteTarget {
        alias: alias.to_string(),
        host_name: None,
        user: None,
    }];
    app.remote.selected = 0;
    app.import_paths_into_remote(sources);
    let backend = CrosstermBackend::new(CountingWriter::new(stdout(), Default::default()));
    let mut terminal: CroftTerminal = Terminal::with_options(
        backend,
        ratatui::TerminalOptions {
            viewport: ratatui::Viewport::Fixed(Rect::new(0, 0, 80, 24)),
        },
    )?;
    remote_sessions::run_pending_scp_uploads(&mut app, &mut terminal)
}

pub fn run(
    root: PathBuf,
    restore_session: Option<PathBuf>,
    open_file: Option<PathBuf>,
    open_at: Option<(usize, usize)>,
    zen: bool,
    workspace_folders: Vec<PathBuf>,
) -> Result<()> {
    // What building the editor reads that takes real work to make: the
    // bundled extension manifests (one TOML parse each) and the built-in
    // secret and agent-prompt regexes. Together they were most of what a
    // launch did before its first frame, all on this thread. Each starts on
    // a thread of its own now, while this one binds sockets and spawns the
    // shell, and whatever reaches one before it is done waits only for the
    // rest of it.
    std::thread::spawn(crate::lsp::manifest::prewarm_bundled);
    std::thread::spawn(crate::triggers::prewarm_builtin_redactions);
    // Every job from here on that the first frame never shows waits for it
    // (see `startup_gate`), so the UI thread has the CPU to itself.
    crate::startup_gate::arm();
    crate::startup_gate::spawn_after_first_frame(crate::agents::prewarm_builtin);
    // Termux ships no font with codicon glyphs, so the activity-bar glyph
    // fallback renders blank cells out of the box. Install the Meslo Nerd
    // Font Mono into ~/.termux/font.ttf in the background (no-op on every
    // other host, and when the user already has a font.ttf).
    crate::termux::ensure_codicon_font_in_background();
    // Build the tree-sitter highlight configurations on a background thread so
    // the first file open paints its syntax (keywords, strings, comments)
    // instantly without the per-tab query-compilation snap. That compile is
    // hundreds of milliseconds of CPU, so it starts once the first frame is
    // up rather than competing with it. The configs land in a process-wide
    // cache, so the UI thread reads the finished build straight out of it on
    // first open (or builds on demand if the user opens a file before the
    // prewarm finishes).
    crate::startup_gate::spawn_after_first_frame(crate::highlight::prewarm_configs);
    // Hand the heap a large file's parse passes freed back to the OS
    // (#1707), so croft doesn't stay resident at the parse's peak.
    crate::mem_relief::start();
    let title = build_title(&root);
    crate::startup_trace::mark("background work started");
    // Raw mode, and the host terminal's capability questions sent, before
    // the editor is built: the answers travel while App::new runs instead of
    // after it, which over SSH is a network round trip off the first frame.
    enable_raw_mode().context("enable raw mode")?;
    #[cfg(unix)]
    let host_probe = StartupProbe(Some(crate::iterm2_inline::ask_host()));
    let mut app = App::new(root.clone())?;
    crate::startup_trace::mark("editor built");
    // Resurrect this workspace's terminal panel from the last session
    // (pane layout, cwds, names, focus) before the first frame paints.
    app.restore_terminal_session();
    // A hosted default pane (#694) is recorded at once, restored or not.
    app.save_terminal_session_if_hosted();
    // A launch from a .code-workspace file (#163) defines the folder set
    // exactly; otherwise the automatic per-primary store restores.
    if workspace_folders.is_empty() {
        let restored_folders = app.restore_workspace_folders();
        if restored_folders > 0 {
            app.status = format!("Restored {restored_folders} workspace folder(s)");
        }
    } else {
        for folder in workspace_folders {
            app.add_workspace_folder(folder);
        }
    }
    // Restore the tabs / layout carried across a self-update re-exec, then
    // delete the handoff file so a later normal launch starts clean.
    if let Some(session_path) = restore_session.as_ref() {
        match crate::session_state::SessionState::load(session_path) {
            Ok(state) => {
                let left = app.apply_session_state(&state);
                if app.hold_unrestored_tabs(left) {
                    let _ = std::fs::remove_file(session_path);
                } else {
                    // Kept: it holds the only copy of cells not put back.
                    app.status = format!(
                        "Some unsaved xlsx cells could not be restored; the session is kept at {}",
                        session_path.display()
                    );
                }
                // The hot-exit backup follows what the relaunch brought
                // back (#862), at once rather than after the first edit.
                let _ = app.write_hot_exit();
            }
            // Kept: it may hold the only copy of unsaved text.
            Err(e) => {
                app.status = format!(
                    "Could not restore the session ({e:#}); it is kept at {}",
                    session_path.display()
                );
            }
        }
    } else {
        // A normal launch brings back what hot exit kept of this workspace
        // when croft last ended without a clean quit (#862). The session
        // the last clean exit saved brings back its tabs when hot exit had
        // none, and its bookmarks either way; it is read once the first
        // frame is up, off this thread.
        let restored = app.restore_hot_exit();
        app.read_saved_session(!restored);
    }
    crate::startup_trace::mark("session restored");
    // `--zen`: hide the Explorer sidebar and terminal so the editor fills the
    // window. "Move/Copy into New Window" launches the new window this way so it
    // opens focused on just the handed-off file. Toggle back with Cmd+B / Cmd+J.
    if zen {
        app.show_tree = false;
        app.show_terminal = false;
    }
    // `croft <root> --open-file <path>`: open the handed-off file (used by the
    // new window that Move / Copy into New Window spawns).
    if let Some(file) = open_file.as_ref() {
        // `croft file:line[:col]` (#1606).
        if app.open_file_at_launch(file)
            && let Some((row, col)) = open_at
        {
            app.without_auto_hide(|app| app.place_caret_at(row, col));
        }
    }
    // A `croft://` link (#359) names what to land on.
    match std::env::var("CROFT_FOCUS").as_deref() {
        Ok("terminal") => app.focus_pane(Pane::Terminal),
        Ok("editor") => app.focus_pane(Pane::Editor),
        // The approval popup is modal and opens by itself for a waiting
        // edit, so landing on it needs no focus change; say so when there
        // is nothing to land on (answered or expired on the way here).
        Ok("approval") => {
            app.drain_hook_requests();
            if app.approvals.is_empty() {
                app.status = String::from("No agent edit is waiting for approval");
            }
        }
        _ => {}
    }
    app.start_update_watch_if_remote();
    app.start_drift_probe_if_local();
    app.start_update_check_if_local();
    // `croft pr <n>` (#365): open the review once the app is up.
    if let Some(selector) = crate::pr_review::take_startup_pr() {
        app.start_pr_review(selector);
    }
    // `croft demo` (#377): start the tour once the app is up.
    if crate::tour::take_startup_demo() {
        app.start_demo();
    }

    // Sixel has no env var, and whether the terminal speaks the kitty
    // keyboard protocol has none either, so probe the host: the kitty query,
    // then DA1. This runs in raw mode but before the alt-screen switch and
    // long before the event loop's first poll, so crossterm's (lazily-started)
    // event reader can't swallow the reply. A non-sixel terminal (e.g. Apple
    // Terminal) answers DA1 without attribute 4; init_graphics then promotes
    // the protocol to Sixel only when neither iTerm2 nor Kitty was detected
    // from the environment and the reply lists it. tmux answers DA1 itself,
    // listing sixel when it was built with it whatever the terminal around
    // it can draw, so there the reply says nothing about the screen.
    #[cfg(unix)]
    {
        let host = host_probe.answer();
        if app.inline_protocol == crate::iterm2_inline::InlineImageProtocol::None {
            app.sixel_supported = host.sixel && !crate::iterm2_inline::detect_tmux();
            crate::widgets::terminal::SIXEL_HOST
                .store(app.sixel_supported, std::sync::atomic::Ordering::Relaxed);
        }
        app.legacy_ctrl_punct = host.answered && !host.kitty_keyboard;
    }
    crate::startup_trace::mark("host terminal answered");
    let mut out = stdout();
    // Alt screen + mouse + bracketed paste + SteadyBar cursor (thin vertical
    // line, no hardware blink: the blink is done in software so it works even
    // when the host terminal's own "Blinking cursor" preference is off). The
    // shared takeover block keeps this list identical to the dtach-reattach
    // re-assert; add new modes in takeover_mode_seq, not here.
    {
        use std::io::Write;
        out.write_all(&takeover_mode_seq())
            .and_then(|()| out.flush())
            .context("enter alt screen")?;
    }
    // From here the tty is in raw mode + alt-screen + mouse + bracketed
    // paste. The normal teardown below restores all of it, but a panic
    // unwinds straight past it and would drop the user back to a shell
    // with mouse-tracking still on (stray `^[[<35;…M` on every mouse
    // move). Install a hook that restores the tty first, then chains to
    // the original so the panic message + backtrace still print.
    install_terminal_restore_panic_hook();
    // Best-effort: terminals that don't speak the kitty keyboard protocol just
    // ignore this; ones that do (iTerm2 >=3.5, Ghostty, kitty, WezTerm) start
    // delivering Cmd/Super as a real modifier so cmd+s reaches the app.
    let kbd_enhanced = execute!(
        out,
        PushKeyboardEnhancementFlags(keyboard_enhancement_flags())
    )
    .is_ok();
    app.kitty_keys = kbd_enhanced;
    {
        use std::io::Write;
        out.write_all(&set_title_seq(&title)).ok();
        // Force iTerm2's session bg to sRGB(EDITOR_BG_RGB). Combined with
        // `Color::Reset` on welcome-pane cells and the same sRGB hex baked
        // into the OSC-1337 PNG canvas, both surfaces flow through iTerm2's
        // sRGB → display path and the welcome image bg matches the
        // surrounding pane bg pixel-for-pixel
        // (https://gitlab.com/gnachman/iterm2/-/issues/12529).
        let theme = crate::prefs::Prefs::load_or_default().theme();
        out.write_all(theme_host_colors_seq(theme).as_bytes()).ok();
        out.flush().ok();
    }
    // Env-var-only iTerm2 detection: no stdin queries, so this can't
    // contend with the crossterm event reader.
    app.init_graphics();
    // With the inline-image protocol fully resolved (env detection + the sixel
    // DA1 probe above), decide whether to nudge the user toward a terminal that
    // can render croft's icons and images. Runs identically on the local Mac
    // and the remote Linux box (GOLDEN RULE): both reach `run`, and `croft
    // remote` forwards the local terminal's identity so an SSH session from
    // iTerm2/Ghostty is never falsely warned.
    app.arm_terminal_warning();

    // Wrap stdout so we can count exactly how many bytes ratatui's diff
    // ships per frame — over SSH that byte count, alongside the per-frame
    // render time, is the signal that tells a fat repaint from a slow
    // remote CPU. Toggled on screen with F8.
    let perf_bytes = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
    app.perf.attach_counter(perf_bytes.clone());
    let backend = CrosstermBackend::new(CountingWriter::new(out, perf_bytes));
    let mut terminal: CroftTerminal = Terminal::new(backend).context("create terminal")?;
    crate::startup_trace::mark("screen set up");

    let result = main_loop(&mut app, &mut terminal);
    // An owner's goodbye goes out now (#1147): the drop-to-local and
    // self-update exits below never drop the App.
    drop(app.collab.session.take());

    // Snapshot the terminal panel for the next launch (cwds are read live
    // here, so plain `cd`s during the session are captured at quit).
    app.save_terminal_session();
    // A test run still in flight ends with croft (#1534): its runner was
    // left running under PID 1.
    if app.testing.is_busy() {
        app.test_worker.cancel();
    }

    // Quitting mid-debug ends the program as Stop Debugging does (#1536):
    // nothing sent `disconnect` here, and the debuggee outlived croft.
    // The teardowns run while the terminal is restored and are joined
    // below, before any exit path.
    if !app.debug.sessions.is_empty() {
        app.debug_stop();
    }

    disable_raw_mode().ok();
    {
        use std::io::Write;
        let mut out = stdout();
        out.write_all(&set_title_seq("")).ok();
        // Revert the host terminal's default colors (OSC 110/111, plus the
        // iTerm2 sRGB override where present) so the user's shell after
        // croft exits doesn't keep our forced theme colours.
        out.write_all(reset_host_colors_seq().as_bytes()).ok();
        if crate::iterm2_inline::detect_iterm2_inline_support() {
            out.write_all(reset_session_bg_seq().as_bytes()).ok();
        }
        out.flush().ok();
    }
    // A frame cut short by an error never sent its end; the shell must not
    // inherit a terminal still holding its updates back.
    {
        use std::io::Write;
        let backend = terminal.backend_mut();
        let _ = backend.write_all(END_SYNCHRONIZED_UPDATE);
        let _ = backend.flush();
    }
    if kbd_enhanced {
        execute!(terminal.backend_mut(), PopKeyboardEnhancementFlags).ok();
    }
    execute!(
        terminal.backend_mut(),
        crossterm::cursor::SetCursorStyle::DefaultUserShape,
        LeaveAlternateScreen,
        DisableMouseCapture,
        DisableBracketedPaste,
        DisableFocusChange,
    )
    .ok();
    terminal.show_cursor().ok();

    // Reap the navigator's claude child before any exit: drop-to-local
    // (process::exit) and self-update (exec) below both bypass Drop, and even
    // a normal return races the detached Drop thread against process exit.
    // shutdown_blocking reverts, hangs up, and grace-kills synchronously.
    if let Some(host) = app.pair.host.take() {
        host.shutdown_blocking();
    }
    // And every teardown a recent unseat DETACHED (toggle off, record
    // disabled, pilot death): those threads race process exit, and a thread
    // that dies mid-grace-kill leaves the claude child running.
    crate::pair_host::join_teardowns();
    crate::dap::transport::join_teardowns();

    // What croft could not do on the way out, said once the shell is the
    // user's again (#862).
    for note in app.exit_notes.drain(..) {
        eprintln!("croft: {note}");
    }
    result?;
    if app.drop_to_local {
        crate::widgets::editor::delete_own_gunzip_copies();
        // `exit` skips dropping the app: finish ending the panes already
        // closed, or a shell that ignores hang-up outlives croft (#2831).
        app.wait_for_ending_terminals();
        std::process::exit(crate::remote::DROP_TO_LOCAL_EXIT_CODE);
    }
    // A background self-update landed: serialize the session and replace
    // this process image with the freshly-installed binary. The unix
    // process-image swap keeps the same controlling tty (the ssh PTY), so
    // there is no reconnect - the user sees a redraw, not a dropped
    // session. No shell is involved; args are passed as an argv vector.
    if app.pending_reexec {
        let state = app.capture_session_state();
        let session_path = crate::session_state::handoff_path();
        // Saved once already when the relaunch was armed; this refresh is
        // best-effort, and a failure leaves that copy in place.
        let _ = state.save(&session_path);
        use std::os::unix::process::CommandExt;
        // A staged release (#333) is swapped over the installed binary
        // only now, on the user's Relaunch: until this line PATH still
        // held the old version.
        let target = reexec_binary_path(app.self_install.as_ref());
        if let Some(staged) = app.staged_update_binary()
            && let Err(err) = crate::update_check::apply_staged(&staged, &target)
        {
            // The installed binary is untouched (copy-beside + rename), so
            // relaunching it is still right: the editor must never simply
            // exit because an update could not be applied.
            eprintln!(
                "croft update failed: could not replace {} with {}: {err} - relaunching the current version",
                target.display(),
                staged.display()
            );
        }
        let mut cmd = std::process::Command::new(target);
        cmd.arg(app.workspace_root())
            .arg("--restore-session")
            .arg(&session_path);
        // The relaunched croft reopens each archive from the saved session.
        crate::widgets::editor::delete_own_gunzip_copies();
        // Replacing the process skips dropping the app too. Only the panes
        // already closed are waited for: the live ones stay, and a hosted
        // pane is found again by the relaunched croft (#2831).
        app.wait_for_ending_terminals();
        let err = CommandExt::exec(&mut cmd);
        eprintln!("croft self-relaunch failed: {err}");
        return Ok(());
    }
    if let Some(remote) = app.remote_launch.take() {
        app.fs_watch.disable();
        let launch_result = if let Some(adopted) = remote.adopted {
            crate::remote::launch_only(adopted, remote.path.as_deref())
        } else {
            crate::remote::launch_croft_with(&remote.host, remote.path.as_deref(), None, false)
        };
        restore_host_terminal_state();
        match launch_result? {
            crate::remote::RemoteOutcome::ReturnToLocal => {
                return run(root, None, None, None, false, Vec::new());
            }
            crate::remote::RemoteOutcome::Exited => return Ok(()),
        }
    }
    // A clean exit: what the next launch of this workspace opens with.
    if let Err(e) = app.save_session_for_next_launch() {
        eprintln!("croft: could not save the session: {e:#}");
    }
    Ok(())
}

/// The host probe `run` sent before building the editor. Should building it
/// fail, the replies are still read off the tty, so they never land at the
/// shell's prompt, and raw mode is undone before the error prints.
#[cfg(unix)]
struct StartupProbe(Option<crate::iterm2_inline::PendingHostProbe>);

#[cfg(unix)]
impl StartupProbe {
    fn answer(mut self) -> crate::iterm2_inline::HostProbe {
        self.0
            .take()
            .map(crate::iterm2_inline::PendingHostProbe::answer)
            .unwrap_or_default()
    }
}

#[cfg(unix)]
impl Drop for StartupProbe {
    fn drop(&mut self) {
        if let Some(pending) = self.0.take() {
            let _ = pending.answer();
            disable_raw_mode().ok();
        }
    }
}

/// Fingerprints of the text cells under each large post-frame image this
/// frame (see [`overlay::ImageOverlay::needs_emit`]).
struct ImageUnderlays {
    /// Around each activity-bar and toolbar icon, for iTerm2's keepalive.
    activity: Vec<u64>,
    editor: [u64; 2],
    terminal: u64,
    markdown: u64,
    minimap: u64,
}

/// Consume every overlay's one-shot image-clear latch; true when any fired.
/// Shared by the pre-draw and post-draw eviction checks in [`main_loop`].
fn consume_any_image_clear(app: &mut App) -> bool {
    let fired = [
        app.consume_welcome_image_clear(),
        app.consume_editor_image_clear(),
        app.consume_terminal_image_clear(),
        app.consume_markdown_image_clear(),
        app.consume_run_debug_image_clear(),
        app.consume_problems_badge_image_clear(),
        app.consume_no_repo_hero_image_clear(),
        app.consume_popup_image_clear(),
        app.consume_ssh_empty_state_image_clear(),
        app.consume_minimap_image_clear(),
        app.consume_activity_image_clear(),
    ];
    fired.into_iter().any(|f| f)
}

fn main_loop(app: &mut App, terminal: &mut CroftTerminal) -> Result<()> {
    // Force the very first frame to render so the user sees the UI even
    // before the first event arrives or any timer fires.
    let mut needs_redraw = true;
    let mut first_frame = true;
    let mut last_blink_visible = app.cursor_visible_phase();
    // Advances every UPDATE_SPINNER_FRAME_MS while a background update runs;
    // forces a status-bar redraw so the spinner animates. Constant otherwise.
    let mut last_spinner_phase = app.update_spinner_phase();
    // Frame-rate accounting for the F8 HUD: count draws over a rolling 1s
    // window so a high idle FPS (always-dirty redraw loop saturating the
    // SSH link) is visible at a glance.
    let mut fps_window_start = std::time::Instant::now();
    let mut frames_in_window: u32 = 0;
    let mut keys_in_window: u32 = 0;
    let mut iters_in_window: u32 = 0;
    let mut work_max_us: u128 = 0;
    let mut poll_max_us: u128 = 0;
    // PTY-only redraws are capped so a chatty embedded shell (Claude Code,
    // npm install, log streams) cannot saturate stdout and starve mouse /
    // key events on the same thread. Input + FS + search + git + remote
    // signals always bypass the cap, so clicks and keystrokes stay at 0ms.
    // Over SSH the user's link is the bottleneck, so we drop further to
    // ~20 Hz; locally ~30 Hz is plenty for shell text.
    let pty_min_interval = if app.session.is_remote {
        Duration::from_millis(50)
    } else {
        Duration::from_millis(33)
    };
    // A keystroke echo, a shell line-rewrite, or an input-box update
    // advances only a handful of bytes into the grid; a bulk stream (cat,
    // build logs, a full-pane scroll, a TUI's streaming output) advances
    // many. We let small updates redraw immediately so echo feels native
    // even when croft itself is the remote end of an SSH link, and keep
    // the cap on large updates so a flood can't saturate the pipe and
    // starve input. A full-pane repaint runs tens of KB; interactive
    // echo stays well under this, so the two never alias.
    const PTY_SMALL_UPDATE_BYTES: usize = 4096;
    let mut last_pty_redraw = std::time::Instant::now()
        .checked_sub(pty_min_interval)
        .unwrap_or_else(std::time::Instant::now);

    while !app.quit {
        iters_in_window += 1;
        if fps_window_start.elapsed() >= Duration::from_secs(1) {
            app.perf.record_window(
                frames_in_window,
                keys_in_window,
                iters_in_window,
                work_max_us,
                poll_max_us,
            );
            frames_in_window = 0;
            keys_in_window = 0;
            iters_in_window = 0;
            work_max_us = 0;
            poll_max_us = 0;
            fps_window_start = std::time::Instant::now();
        }
        let iter_start = std::time::Instant::now();
        // If the user dropped files onto the Remote Explorer, suspend the
        // alt-screen and run scp in the host shell so the user sees its
        // progress, can answer password / FIDO / known_hosts prompts,
        // and gets explicit success / failure output before we resume.
        if !app.pending_scp_uploads.is_empty() {
            run_pending_scp_uploads(app, terminal)?;
            needs_redraw = true;
        }
        // What the background work finished this tick, collected before
        // anything is drawn (#2062).
        let pass = tick_background(app);
        let pty_pending = pass.pty_pending;
        let blink_visible = pass.blink_visible;
        let blink_changed = blink_visible != last_blink_visible;
        let spinner_phase = pass.spinner_phase;
        let spinner_changed = spinner_phase != last_spinner_phase;

        let non_pty_dirty = needs_redraw || pass.changed || blink_changed || spinner_changed;
        let pty_eligible = pty_pending
            && (app.peek_terminals_pending_bytes() <= PTY_SMALL_UPDATE_BYTES
                || last_pty_redraw.elapsed() >= pty_min_interval);
        let do_redraw = non_pty_dirty || pty_eligible;
        // Consume the PTY dirty flags only when we are actually redrawing.
        // Skipping a frame must leave them set so the next eligible tick
        // emits the deferred output.
        let pty_changed = if do_redraw {
            app.drain_terminals_dirty()
        } else {
            false
        };

        if do_redraw {
            if pty_changed {
                last_pty_redraw = std::time::Instant::now();
            }
            // Everything this frame sends, the diff and the pictures after
            // it, goes up as one update.
            {
                use std::io::Write;
                let _ = terminal.backend_mut().write_all(BEGIN_SYNCHRONIZED_UPDATE);
            }
            // If the welcome OSC-1337 image was painted earlier and the
            // user has just opened a file, wipe the screen so iTerm drops
            // its cached image cells AND ratatui repaints every cell on
            // the next draw (its diff alone misses cells whose content
            // didn't change between welcome and editor buffers).
            // A resize/WINCH is not image eviction: it invalidated the text on
            // screen (dtach reattach repaints into a blank terminal), so it
            // wipes unconditionally, Kitty included. It never fires while the
            // user merely types, so the blink fix below is untouched.
            let resize_repaint = app.consume_resize_repaint();
            if consume_any_image_clear(app) || resize_repaint {
                // iTerm2/sixel image cells live in the normal cell buffer, so
                // evicting them takes a full `2J` + repaint. Kitty images are
                // deleted on their own layer below - wiping the text there
                // blanks the whole app for an SSH round trip per latch (the
                // every-keystroke blink), so Kitty skips the screen clear.
                if resize_repaint || app.image_eviction_needs_screen_wipe() {
                    terminal.clear()?;
                }
                app.evict_inline_images_on_clear();
                // Activity-bar icons live outside ratatui too; re-emit
                // them on the next post-draw flush. The welcome wordmark
                // and the source-control hero also live in iTerm's image
                // cache that just got wiped - re-arm them so the next
                // render re-emits whichever one is currently visible.
                app.overlays.activity.mark_dirty();
                app.overlays.welcome.mark_dirty();
                app.overlays.hero.mark_dirty();
                app.forget_sent_images();
            }
            crate::startup_trace::mark("loop work done");
            let draw_start = std::time::Instant::now();
            // The frame ratatui just drew, read in place by the picture
            // flushes below rather than copied (#2062).
            let mut drawn = terminal.draw(|f| app.render_for_terminal(f))?.buffer;
            crate::startup_trace::mark("frame drawn");
            app.sync_caret_style();
            let mut underlays = app.image_underlays(drawn);
            // Record render+flush time and the bytes ratatui shipped this
            // frame so the F8 HUD can show where remote latency goes.
            app.perf.record_draw(draw_start.elapsed().as_micros());
            frames_in_window += 1;
            if consume_any_image_clear(app) {
                if app.image_eviction_needs_screen_wipe() {
                    terminal.clear()?;
                }
                app.evict_inline_images_on_clear();
                app.overlays.activity.mark_dirty();
                app.overlays.welcome.mark_dirty();
                app.overlays.hero.mark_dirty();
                app.forget_sent_images();
                drawn = terminal.draw(|f| app.render_for_terminal(f))?.buffer;
                underlays = app.image_underlays(drawn);
            }
            // After ratatui flushes its diff, paint the activity-bar icons
            // directly via OSC-1337 on every redraw. We previously gated
            // this on a `dirty` flag and only re-emitted on resize / view
            // change, but the icons would intermittently vanish: iTerm2
            // can drop cached OSC-1337 image cells under heavy SGR traffic
            // on adjacent cells (cursor-blink redraws, search updates,
            // mouse motion bursts) and the dirty gate had no way to detect
            // that. Since `render_activity_bar` writes nothing to the
            // buffer in image-mode, ratatui's diff produces zero per-cell
            // writes here — re-emitting the pre-encoded OSC bytes every
            // frame is cheap and locks the images in.
            app.flush_activity_image_overlays(&underlays.activity);
            // Active editor image preview: baked once, and sent after
            // ratatui's diff only when it could be missing from the screen
            // (`ImageOverlay::needs_emit`), never on every frame: a picture
            // is megabytes, and over SSH resending it per keystroke stalls
            // the session (#682). One slot per editor split column (0 =
            // left, 1 = right); when not split only slot 0 has a payload.
            for side in 0..2 {
                if let Some((osc, layout)) = app.editor_image_payload(side)
                    && app.overlay_fits_on_screen(
                        layout.cell_x,
                        layout.cell_y,
                        layout.cell_w,
                        layout.cell_h,
                    )
                {
                    if !app.overlays.editor[side].needs_emit(underlays.editor[side]) {
                        continue;
                    }
                    write_inline_image(
                        &mut stdout(),
                        layout.cell_x,
                        layout.cell_y,
                        osc.as_bytes(),
                        app.cursor_should_be_visible(),
                    );
                    app.mark_editor_image_displayed(side);
                    app.overlays.editor[side].mark_sent_over(underlays.editor[side]);
                } else {
                    app.overlays.editor[side].forget_sent();
                }
            }
            // Terminal-pane inline image (captured imgcat output): same
            // bake-once / send-when-missing overlay, anchored to its grid row.
            if let Some((osc, layout)) = app.terminal_image_payload()
                && app.overlay_fits_on_screen(
                    layout.cell_x,
                    layout.cell_y,
                    layout.cell_w,
                    layout.cell_h,
                )
            {
                if app.overlays.terminal_image.needs_emit(underlays.terminal) {
                    write_inline_image(
                        &mut stdout(),
                        layout.cell_x,
                        layout.cell_y,
                        osc.as_bytes(),
                        app.cursor_should_be_visible(),
                    );
                    app.mark_terminal_image_displayed();
                    app.overlays
                        .terminal_image
                        .mark_sent_over(underlays.terminal);
                }
            } else {
                app.overlays.terminal_image.forget_sent();
            }
            // Markdown-preview inline image (#176): same bake-once /
            // send-when-missing overlay, anchored at its reserved rows.
            if let Some((osc, layout)) = app.markdown_image_payload()
                && app.overlay_fits_on_screen(
                    layout.cell_x,
                    layout.cell_y,
                    layout.cell_w,
                    layout.cell_h,
                )
            {
                if app.overlays.markdown_image.needs_emit(underlays.markdown) {
                    write_inline_image(
                        &mut stdout(),
                        layout.cell_x,
                        layout.cell_y,
                        osc.as_bytes(),
                        app.cursor_should_be_visible(),
                    );
                    app.mark_markdown_image_displayed();
                    app.overlays
                        .markdown_image
                        .mark_sent_over(underlays.markdown);
                }
            } else {
                app.overlays.markdown_image.forget_sent();
            }
            // Editor minimap: same bake-once / send-when-missing overlay,
            // painted after ratatui's diff so the raster lands on the strip.
            if let Some((osc, layout)) = app.minimap_image_payload()
                && app.overlay_fits_on_screen(
                    layout.cell_x,
                    layout.cell_y,
                    layout.cell_w,
                    layout.cell_h,
                )
            {
                if app.overlays.minimap.needs_emit(underlays.minimap) {
                    write_inline_image(
                        &mut stdout(),
                        layout.cell_x,
                        layout.cell_y,
                        osc.as_bytes(),
                        app.cursor_should_be_visible(),
                    );
                    app.mark_minimap_image_displayed();
                    app.overlays.minimap.mark_sent_over(underlays.minimap);
                }
            } else {
                app.overlays.minimap.forget_sent();
            }
            // The small chrome pictures: the Run-and-Debug headline icon,
            // the PROBLEMS badge and the source-control and SSH
            // illustrations. The icon only fires while the sidebar is on
            // the Run-Debug view and the panel reserved a cell for it on
            // its last render. On view change the
            // `consume_run_debug_image_clear` gate above already armed
            // `terminal.clear()`, evicting the cached image cells before
            // the next sidebar paints.
            app.flush_chrome_images(drawn);
            // Welcome-screen logo: same OSC-1337 trick, gated by its own
            // dirty flag and only emitted while the editor pane is in its
            // blank initial state.
            if let Some((img, layout)) = app.welcome_image_emit_payload()
                && app.overlay_fits_on_screen(
                    layout.cell_x,
                    layout.cell_y,
                    layout.cell_w,
                    layout.cell_h,
                )
            {
                write_inline_image(
                    &mut stdout(),
                    layout.cell_x,
                    layout.cell_y,
                    img.as_bytes(),
                    app.cursor_should_be_visible(),
                );
                app.mark_welcome_image_emitted();
            }
            // Last of all: re-paint the chrome tooltip on top of any sidebar
            // illustration it overlaps. Must run after every inline-image flush
            // above so the box wins those cells (last writer per cell wins).
            app.flush_tooltip_over_image();
            {
                use std::io::Write;
                let backend = terminal.backend_mut();
                let _ = backend.write_all(END_SYNCHRONIZED_UPDATE);
                let _ = backend.flush();
            }
            if std::mem::take(&mut first_frame) {
                crate::startup_trace::mark("first frame on screen");
                // The work held back for the frame starts now.
                crate::startup_gate::open();
                // Pre-warm the workspace's evident language servers
                // (Cargo.toml -> rust-analyzer, and so on), VS Code's
                // `workspaceContains` activation: the cold indexing overlaps
                // with the user browsing the tree instead of starting only at
                // the first file open (#27). Asked now rather than while the
                // editor was built, so a server's start never competes with
                // the first frame.
                if let Some(lsp) = app.lsp.manager.as_ref() {
                    lsp.prewarm_workspace();
                }
                crate::startup_trace::finish();
                // A GUI launch asks the login shell for its PATH again, for
                // the next launch (#1973), only now that the frame is up:
                // the shell runs the user's whole rc, which would otherwise
                // compete for the CPU with the frame and the pane's shell.
                crate::gui_path::refresh_in_background();
            }
            needs_redraw = false;
            last_blink_visible = blink_visible;
            last_spinner_phase = spinner_phase;
        }

        // 8 ms (~125 Hz) keeps echo lag tight when the embedded pty is the
        // hot path (remote-launched croft over SSH). Idle cost is still
        // negligible because the redraw branch above gates on dirty flags;
        // this poll just decides how often we wake up to *check*.
        work_max_us = work_max_us.max(iter_start.elapsed().as_micros());
        let poll_start = std::time::Instant::now();
        if event::poll(Duration::from_millis(8))? {
            // Drain every event already queued so a click burst (Down + zero-
            // movement Drag + Up, all delivered in <50ms by the terminal)
            // coalesces into a single redraw. Otherwise each event triggers
            // its own terminal.draw cycle, which Hides+Shows the hardware
            // caret each time and the user sees the cursor blink twice
            // rapidly before settling into the normal 530ms blink.
            loop {
                match event::read()? {
                    Event::Key(key) => {
                        if key.kind == KeyEventKind::Press || key.kind == KeyEventKind::Repeat {
                            keys_in_window += 1;
                        }
                        app.handle_key(key)?;
                    }
                    Event::Mouse(m) => app.handle_mouse(m),
                    Event::Paste(s) => app.handle_paste(&s),
                    Event::FocusLost => {
                        app.on_window_focus_lost();
                    }
                    Event::Resize(_, _) => {
                        // Alt-screen reflow blanks the activity bar's SGR cells
                        // but iTerm2's OSC-1337 image layer survives, so a plain
                        // re-emit ghosts a fresh icon over the stale one. Re-mark
                        // dirty AND arm a one-shot terminal.clear() to evict it.
                        app.on_resize();
                        // Under dtach a reattach arrives as a bare WINCH into a
                        // terminal that never saw the startup DECSETs: without
                        // this re-assert the mouse (and bracketed paste, kitty
                        // keyboard flags, alt screen) stays dead until the next
                        // updater relaunch happens to re-exec croft.
                        if app.session.persistent {
                            use std::io::Write;
                            let out = terminal.backend_mut();
                            let _ = out.write_all(&reattach_reassert_seq(app.theme));
                            let _ = out.flush();
                            // The re-assert set the steady bar; resend the
                            // overwrite block on the next draw if wanted.
                            app.caret_block_sent = false;
                        }
                    }
                    _ => {}
                }
                if !event::poll(Duration::ZERO)? {
                    break;
                }
            }
            needs_redraw = true;
        }
        poll_max_us = poll_max_us.max(poll_start.elapsed().as_micros());
        // Drive edge auto-scroll for an in-progress terminal drag-selection
        // even when the pointer is held still (no fresh events): the poll
        // above wakes us every 8 ms, and this advances the selection while
        // the pointer stays pinned past the pane edge.
        if app.tick_terminal_autoscroll() {
            needs_redraw = true;
        }
    }
    Ok(())
}